1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate * This file contains all functions pertaining to registrations:
31*0Sstevel@tonic-gate * SLPReg
32*0Sstevel@tonic-gate * SLPDereg
33*0Sstevel@tonic-gate * SLPDelAttrs
34*0Sstevel@tonic-gate *
35*0Sstevel@tonic-gate * Each function talks only to the local slpd, and receives a SrvAck
36*0Sstevel@tonic-gate * reply.
37*0Sstevel@tonic-gate *
38*0Sstevel@tonic-gate * These calls can operate in sync or async mode. Sync mode operates
39*0Sstevel@tonic-gate * as follows:
40*0Sstevel@tonic-gate * format params into a char *msg
41*0Sstevel@tonic-gate * send this msg to slpd
42*0Sstevel@tonic-gate * invoke the SLPRegReport callback with the error code found in the
43*0Sstevel@tonic-gate * reply from slpd
44*0Sstevel@tonic-gate * return
45*0Sstevel@tonic-gate *
46*0Sstevel@tonic-gate * Async mode operates as follows:
47*0Sstevel@tonic-gate * format the params into a char *msg
48*0Sstevel@tonic-gate * there is one thread per process which handles async regs
49*0Sstevel@tonic-gate * make sure this thread is running
50*0Sstevel@tonic-gate * the reg_thread monitors the global static reg_q for messages
51*0Sstevel@tonic-gate * a queue message is represented as a struct reg_q_msg
52*0Sstevel@tonic-gate * caller thread places the reg msg on the reg_q, and returns
53*0Sstevel@tonic-gate * the reg_thread reads the message from the reg_q, and sends the
54*0Sstevel@tonic-gate * msg to slpd
55*0Sstevel@tonic-gate * the reg_thread then invokes the SLPRegReport callback with the error
56*0Sstevel@tonic-gate * code found in the reply from slpd
57*0Sstevel@tonic-gate * once started, the reg_thread manages registration refreshing.
58*0Sstevel@tonic-gate * If there are no registrations to refresh, the thread exits.
59*0Sstevel@tonic-gate */
60*0Sstevel@tonic-gate
61*0Sstevel@tonic-gate #include <stdio.h>
62*0Sstevel@tonic-gate #include <stdlib.h>
63*0Sstevel@tonic-gate #include <thread.h>
64*0Sstevel@tonic-gate #include <synch.h>
65*0Sstevel@tonic-gate #include <syslog.h>
66*0Sstevel@tonic-gate #include <slp-internal.h>
67*0Sstevel@tonic-gate #include <sys/time.h>
68*0Sstevel@tonic-gate #include <time.h>
69*0Sstevel@tonic-gate
70*0Sstevel@tonic-gate /* Indices into a reg_msg iovec for auth blocks */
71*0Sstevel@tonic-gate #define SLP_URL_AUTH 1
72*0Sstevel@tonic-gate #define SLP_ATTR_AUTH 3
73*0Sstevel@tonic-gate
74*0Sstevel@tonic-gate /* A registration / de-registration message */
75*0Sstevel@tonic-gate struct reg_msg {
76*0Sstevel@tonic-gate struct iovec *msgiov; /* msg contents */
77*0Sstevel@tonic-gate int msgiov_len; /* number of iovec components in msgiov */
78*0Sstevel@tonic-gate struct iovec urlbytes;
79*0Sstevel@tonic-gate struct iovec attrbytes;
80*0Sstevel@tonic-gate int urlauth; /* index into authiov for URL auth blocks */
81*0Sstevel@tonic-gate int attrauth; /* index into authiov for attr auth blocks */
82*0Sstevel@tonic-gate };
83*0Sstevel@tonic-gate
84*0Sstevel@tonic-gate /*
85*0Sstevel@tonic-gate * This is the message bundle passed to the reg thread via a queue.
86*0Sstevel@tonic-gate */
87*0Sstevel@tonic-gate struct reg_q_msg {
88*0Sstevel@tonic-gate struct reg_msg *msg;
89*0Sstevel@tonic-gate slp_handle_impl_t *hp;
90*0Sstevel@tonic-gate SLPRegReport *cb;
91*0Sstevel@tonic-gate void *cookie;
92*0Sstevel@tonic-gate };
93*0Sstevel@tonic-gate
94*0Sstevel@tonic-gate /*
95*0Sstevel@tonic-gate * These structures and vars are used for automatic re-registering.
96*0Sstevel@tonic-gate */
97*0Sstevel@tonic-gate static struct rereg_entry {
98*0Sstevel@tonic-gate char *url;
99*0Sstevel@tonic-gate struct reg_msg *msg;
100*0Sstevel@tonic-gate time_t wake_time;
101*0Sstevel@tonic-gate unsigned short lifetime;
102*0Sstevel@tonic-gate struct rereg_entry *next;
103*0Sstevel@tonic-gate } *reregs;
104*0Sstevel@tonic-gate
105*0Sstevel@tonic-gate static time_t next_wake_time;
106*0Sstevel@tonic-gate static unsigned short granularity = 3600;
107*0Sstevel@tonic-gate static mutex_t rereg_lock = DEFAULTMUTEX; /* protects the rereg struct */
108*0Sstevel@tonic-gate static mutex_t start_lock = DEFAULTMUTEX; /* protects reg_thr creation */
109*0Sstevel@tonic-gate
110*0Sstevel@tonic-gate static slp_queue_t *reg_q; /* the global registration queue */
111*0Sstevel@tonic-gate static int slp_reg_thr_running; /* positive if reg_thread is running */
112*0Sstevel@tonic-gate
113*0Sstevel@tonic-gate /* Private Utility Routines */
114*0Sstevel@tonic-gate
115*0Sstevel@tonic-gate static SLPBoolean check_reregs();
116*0Sstevel@tonic-gate static SLPError add_rereg(const char *, struct reg_msg *, unsigned short);
117*0Sstevel@tonic-gate static unsigned short dereg_rereg(const char *);
118*0Sstevel@tonic-gate
119*0Sstevel@tonic-gate static SLPError enqueue_reg(slp_handle_impl_t *, struct reg_msg *,
120*0Sstevel@tonic-gate void *, SLPRegReport *);
121*0Sstevel@tonic-gate static SLPError reg_impl(slp_handle_impl_t *, struct reg_msg *,
122*0Sstevel@tonic-gate void *, SLPRegReport *);
123*0Sstevel@tonic-gate static void reg_thread();
124*0Sstevel@tonic-gate static SLPError start_reg_thr();
125*0Sstevel@tonic-gate static SLPError reg_common(slp_handle_impl_t *, struct reg_msg *,
126*0Sstevel@tonic-gate void *, SLPRegReport *);
127*0Sstevel@tonic-gate static SLPError UnpackSrvAck(char *, SLPError *);
128*0Sstevel@tonic-gate static SLPError packSrvReg(slp_handle_impl_t *, const char *,
129*0Sstevel@tonic-gate unsigned short, const char *, const char *,
130*0Sstevel@tonic-gate const char *, SLPBoolean, struct reg_msg **);
131*0Sstevel@tonic-gate static SLPError packSrvDereg(slp_handle_impl_t *, const char *,
132*0Sstevel@tonic-gate const char *, const char *, struct reg_msg **);
133*0Sstevel@tonic-gate static SLPError find_SAscopes(char **scopes);
134*0Sstevel@tonic-gate static void free_msgiov(struct iovec *, int);
135*0Sstevel@tonic-gate
136*0Sstevel@tonic-gate /* Public API SA functionality */
137*0Sstevel@tonic-gate
SLPReg(SLPHandle hSLP,const char * pcSrvURL,const unsigned short usLifetime,const char * pcSrvType,const char * pcAttrs,SLPBoolean fresh,SLPRegReport callback,void * pvUser)138*0Sstevel@tonic-gate SLPError SLPReg(SLPHandle hSLP, const char *pcSrvURL,
139*0Sstevel@tonic-gate const unsigned short usLifetime,
140*0Sstevel@tonic-gate const char *pcSrvType,
141*0Sstevel@tonic-gate const char *pcAttrs, SLPBoolean fresh,
142*0Sstevel@tonic-gate SLPRegReport callback, void *pvUser) {
143*0Sstevel@tonic-gate SLPError err;
144*0Sstevel@tonic-gate char *pcScopeList;
145*0Sstevel@tonic-gate struct reg_msg *msg;
146*0Sstevel@tonic-gate
147*0Sstevel@tonic-gate if (!hSLP || !pcSrvURL || !*pcSrvURL || !pcSrvType ||
148*0Sstevel@tonic-gate !pcAttrs || !callback) {
149*0Sstevel@tonic-gate return (SLP_PARAMETER_BAD);
150*0Sstevel@tonic-gate }
151*0Sstevel@tonic-gate
152*0Sstevel@tonic-gate if ((strlen(pcSrvURL) > SLP_MAX_STRINGLEN) ||
153*0Sstevel@tonic-gate (strlen(pcSrvType) > SLP_MAX_STRINGLEN) ||
154*0Sstevel@tonic-gate (strlen(pcAttrs) > SLP_MAX_STRINGLEN)) {
155*0Sstevel@tonic-gate return (SLP_PARAMETER_BAD);
156*0Sstevel@tonic-gate }
157*0Sstevel@tonic-gate
158*0Sstevel@tonic-gate if ((err = find_SAscopes(&pcScopeList)) != SLP_OK) {
159*0Sstevel@tonic-gate return (err);
160*0Sstevel@tonic-gate }
161*0Sstevel@tonic-gate
162*0Sstevel@tonic-gate if ((err = slp_start_call(hSLP)) != SLP_OK)
163*0Sstevel@tonic-gate return (err);
164*0Sstevel@tonic-gate
165*0Sstevel@tonic-gate /* format params into msg */
166*0Sstevel@tonic-gate if ((err = packSrvReg(
167*0Sstevel@tonic-gate hSLP, pcSrvURL, usLifetime, pcSrvType,
168*0Sstevel@tonic-gate pcScopeList, pcAttrs, fresh, &msg)) != SLP_OK) {
169*0Sstevel@tonic-gate free(pcScopeList);
170*0Sstevel@tonic-gate slp_end_call(hSLP);
171*0Sstevel@tonic-gate return (err);
172*0Sstevel@tonic-gate }
173*0Sstevel@tonic-gate
174*0Sstevel@tonic-gate if ((err = reg_common(hSLP, msg, pvUser, callback)) == SLP_OK &&
175*0Sstevel@tonic-gate usLifetime == SLP_LIFETIME_MAXIMUM) {
176*0Sstevel@tonic-gate struct reg_msg *rereg_msg;
177*0Sstevel@tonic-gate
178*0Sstevel@tonic-gate /* create a rereg message, with no attrs */
179*0Sstevel@tonic-gate err = packSrvReg(
180*0Sstevel@tonic-gate hSLP, pcSrvURL, usLifetime,
181*0Sstevel@tonic-gate pcSrvType, pcScopeList, "", SLP_TRUE, &rereg_msg);
182*0Sstevel@tonic-gate if (err == SLP_OK) {
183*0Sstevel@tonic-gate err = add_rereg(pcSrvURL, rereg_msg, usLifetime);
184*0Sstevel@tonic-gate }
185*0Sstevel@tonic-gate }
186*0Sstevel@tonic-gate
187*0Sstevel@tonic-gate free(pcScopeList);
188*0Sstevel@tonic-gate return (err);
189*0Sstevel@tonic-gate }
190*0Sstevel@tonic-gate
packSrvReg(slp_handle_impl_t * hp,const char * url,unsigned short lifetime,const char * type,const char * scope,const char * attrs,SLPBoolean fresh,struct reg_msg ** msg)191*0Sstevel@tonic-gate static SLPError packSrvReg(slp_handle_impl_t *hp, const char *url,
192*0Sstevel@tonic-gate unsigned short lifetime, const char *type,
193*0Sstevel@tonic-gate const char *scope, const char *attrs,
194*0Sstevel@tonic-gate SLPBoolean fresh, struct reg_msg **msg) {
195*0Sstevel@tonic-gate char *m = NULL;
196*0Sstevel@tonic-gate SLPError err;
197*0Sstevel@tonic-gate size_t msgLen, tmplen, len = 0;
198*0Sstevel@tonic-gate time_t ts;
199*0Sstevel@tonic-gate struct timeval tp[1];
200*0Sstevel@tonic-gate
201*0Sstevel@tonic-gate /* calculate the timestamp */
202*0Sstevel@tonic-gate (void) gettimeofday(tp, NULL);
203*0Sstevel@tonic-gate ts = tp->tv_sec + lifetime;
204*0Sstevel@tonic-gate
205*0Sstevel@tonic-gate /* create the reg_msg */
206*0Sstevel@tonic-gate *msg = NULL;
207*0Sstevel@tonic-gate if (!(*msg = calloc(1, sizeof (**msg)))) {
208*0Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "packSrvReg", "out of memory");
209*0Sstevel@tonic-gate return (SLP_MEMORY_ALLOC_FAILED);
210*0Sstevel@tonic-gate }
211*0Sstevel@tonic-gate
212*0Sstevel@tonic-gate /* compute the total messge length */
213*0Sstevel@tonic-gate msgLen =
214*0Sstevel@tonic-gate slp_hdrlang_length(hp) +
215*0Sstevel@tonic-gate /* URL entry */
216*0Sstevel@tonic-gate 5 + strlen(url) +
217*0Sstevel@tonic-gate /* srv reg msg */
218*0Sstevel@tonic-gate 2 + strlen(type) +
219*0Sstevel@tonic-gate 2 + strlen(scope) +
220*0Sstevel@tonic-gate 2 + strlen(attrs);
221*0Sstevel@tonic-gate
222*0Sstevel@tonic-gate /*
223*0Sstevel@tonic-gate * Allocate memory for all the message except the auth blocks.
224*0Sstevel@tonic-gate * The iovec msgiov actually contains only pointers into this
225*0Sstevel@tonic-gate * memory.
226*0Sstevel@tonic-gate */
227*0Sstevel@tonic-gate if (!(m = calloc(msgLen, 1))) {
228*0Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "packSrvReg", "out of memory");
229*0Sstevel@tonic-gate err = SLP_MEMORY_ALLOC_FAILED;
230*0Sstevel@tonic-gate goto error;
231*0Sstevel@tonic-gate }
232*0Sstevel@tonic-gate
233*0Sstevel@tonic-gate /*
234*0Sstevel@tonic-gate * Create iovec for the msg. The iovec components are layed out thus:
235*0Sstevel@tonic-gate * 0: header + URL
236*0Sstevel@tonic-gate * 1: URL auth block count, URL auth block
237*0Sstevel@tonic-gate * 2: attrs
238*0Sstevel@tonic-gate * 3: attrs auth block count, attr auth block
239*0Sstevel@tonic-gate */
240*0Sstevel@tonic-gate if (!((*msg)->msgiov = calloc(4, sizeof (*((*msg)->msgiov))))) {
241*0Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "packSrvReg", "out of memory");
242*0Sstevel@tonic-gate err = SLP_MEMORY_ALLOC_FAILED;
243*0Sstevel@tonic-gate goto error;
244*0Sstevel@tonic-gate }
245*0Sstevel@tonic-gate (*msg)->msgiov_len = 4;
246*0Sstevel@tonic-gate
247*0Sstevel@tonic-gate if ((err = slp_add_header(hp->locale, m, msgLen, SRVREG, 0, &len))
248*0Sstevel@tonic-gate != SLP_OK)
249*0Sstevel@tonic-gate goto error;
250*0Sstevel@tonic-gate /* set fresh flag */
251*0Sstevel@tonic-gate if (fresh)
252*0Sstevel@tonic-gate slp_set_fresh(m);
253*0Sstevel@tonic-gate
254*0Sstevel@tonic-gate /* URL entry */
255*0Sstevel@tonic-gate len++; /* skip reserved byte in URL entry */
256*0Sstevel@tonic-gate if ((err = slp_add_sht(m, msgLen, lifetime, &len)) != SLP_OK)
257*0Sstevel@tonic-gate goto error;
258*0Sstevel@tonic-gate
259*0Sstevel@tonic-gate /* save pointer to URL for signing */
260*0Sstevel@tonic-gate tmplen = len;
261*0Sstevel@tonic-gate (*msg)->urlbytes.iov_base = m + len;
262*0Sstevel@tonic-gate
263*0Sstevel@tonic-gate if ((err = slp_add_string(m, msgLen, url, &len)) != SLP_OK)
264*0Sstevel@tonic-gate goto error;
265*0Sstevel@tonic-gate
266*0Sstevel@tonic-gate (*msg)->urlbytes.iov_len = len - tmplen;
267*0Sstevel@tonic-gate
268*0Sstevel@tonic-gate (*msg)->msgiov[0].iov_base = m;
269*0Sstevel@tonic-gate (*msg)->msgiov[0].iov_len = len;
270*0Sstevel@tonic-gate
271*0Sstevel@tonic-gate /* add auth blocks for URL */
272*0Sstevel@tonic-gate err = slp_sign(&((*msg)->urlbytes), 1, ts,
273*0Sstevel@tonic-gate (*msg)->msgiov, SLP_URL_AUTH);
274*0Sstevel@tonic-gate if (err != SLP_OK) {
275*0Sstevel@tonic-gate goto error;
276*0Sstevel@tonic-gate }
277*0Sstevel@tonic-gate
278*0Sstevel@tonic-gate (*msg)->msgiov[2].iov_base = m + len;
279*0Sstevel@tonic-gate
280*0Sstevel@tonic-gate /* type, scopes, and attrs */
281*0Sstevel@tonic-gate if ((err = slp_add_string(m, msgLen, type, &len)) != SLP_OK)
282*0Sstevel@tonic-gate goto error;
283*0Sstevel@tonic-gate if ((err = slp_add_string(m, msgLen, scope, &len)) != SLP_OK)
284*0Sstevel@tonic-gate goto error;
285*0Sstevel@tonic-gate
286*0Sstevel@tonic-gate /* save pointer to attr for signing */
287*0Sstevel@tonic-gate tmplen = len;
288*0Sstevel@tonic-gate (*msg)->attrbytes.iov_base = m + len;
289*0Sstevel@tonic-gate
290*0Sstevel@tonic-gate if ((err = slp_add_string(m, msgLen, attrs, &len)) != SLP_OK)
291*0Sstevel@tonic-gate goto error;
292*0Sstevel@tonic-gate
293*0Sstevel@tonic-gate (*msg)->attrbytes.iov_len = len - tmplen;
294*0Sstevel@tonic-gate
295*0Sstevel@tonic-gate /* length of 2nd portion is len - length of 1st portion */
296*0Sstevel@tonic-gate (*msg)->msgiov[2].iov_len = len - (*msg)->msgiov[0].iov_len;
297*0Sstevel@tonic-gate
298*0Sstevel@tonic-gate /* add auth blocks for attrs */
299*0Sstevel@tonic-gate err = slp_sign(&((*msg)->attrbytes), 1, ts,
300*0Sstevel@tonic-gate (*msg)->msgiov, SLP_ATTR_AUTH);
301*0Sstevel@tonic-gate if (err != SLP_OK) {
302*0Sstevel@tonic-gate goto error;
303*0Sstevel@tonic-gate }
304*0Sstevel@tonic-gate
305*0Sstevel@tonic-gate /* adjust msgLen with authblocks, and set header length */
306*0Sstevel@tonic-gate msgLen += (*msg)->msgiov[SLP_URL_AUTH].iov_len;
307*0Sstevel@tonic-gate msgLen += (*msg)->msgiov[SLP_ATTR_AUTH].iov_len;
308*0Sstevel@tonic-gate
309*0Sstevel@tonic-gate /* make sure msgLen is valid */
310*0Sstevel@tonic-gate if (msgLen > SLP_MAX_MSGLEN) {
311*0Sstevel@tonic-gate err = SLP_PARAMETER_BAD;
312*0Sstevel@tonic-gate goto error;
313*0Sstevel@tonic-gate }
314*0Sstevel@tonic-gate slp_set_length(m, msgLen);
315*0Sstevel@tonic-gate
316*0Sstevel@tonic-gate return (SLP_OK);
317*0Sstevel@tonic-gate error:
318*0Sstevel@tonic-gate if (m) free(m);
319*0Sstevel@tonic-gate if (*msg) {
320*0Sstevel@tonic-gate if ((*msg)->msgiov) free_msgiov((*msg)->msgiov, 4);
321*0Sstevel@tonic-gate free(*msg);
322*0Sstevel@tonic-gate }
323*0Sstevel@tonic-gate *msg = NULL;
324*0Sstevel@tonic-gate return (err);
325*0Sstevel@tonic-gate }
326*0Sstevel@tonic-gate
SLPDereg(SLPHandle hSLP,const char * pURL,SLPRegReport callback,void * pvUser)327*0Sstevel@tonic-gate SLPError SLPDereg(SLPHandle hSLP, const char *pURL,
328*0Sstevel@tonic-gate SLPRegReport callback, void *pvUser) {
329*0Sstevel@tonic-gate char *pcScopeList;
330*0Sstevel@tonic-gate struct reg_msg *msg;
331*0Sstevel@tonic-gate SLPError err;
332*0Sstevel@tonic-gate
333*0Sstevel@tonic-gate if (!hSLP || !pURL || !*pURL || !callback) {
334*0Sstevel@tonic-gate return (SLP_PARAMETER_BAD);
335*0Sstevel@tonic-gate }
336*0Sstevel@tonic-gate
337*0Sstevel@tonic-gate if (strlen(pURL) > SLP_MAX_STRINGLEN) {
338*0Sstevel@tonic-gate return (SLP_PARAMETER_BAD);
339*0Sstevel@tonic-gate }
340*0Sstevel@tonic-gate
341*0Sstevel@tonic-gate if ((err = find_SAscopes(&pcScopeList))
342*0Sstevel@tonic-gate != SLP_OK) {
343*0Sstevel@tonic-gate return (err);
344*0Sstevel@tonic-gate }
345*0Sstevel@tonic-gate
346*0Sstevel@tonic-gate if ((err = slp_start_call(hSLP)) != SLP_OK)
347*0Sstevel@tonic-gate return (err);
348*0Sstevel@tonic-gate
349*0Sstevel@tonic-gate /* format params into msg */
350*0Sstevel@tonic-gate if ((err = packSrvDereg(hSLP, pURL, pcScopeList, NULL, &msg))
351*0Sstevel@tonic-gate != SLP_OK) {
352*0Sstevel@tonic-gate free(pcScopeList);
353*0Sstevel@tonic-gate slp_end_call(hSLP);
354*0Sstevel@tonic-gate return (err);
355*0Sstevel@tonic-gate }
356*0Sstevel@tonic-gate
357*0Sstevel@tonic-gate if ((err = reg_common(hSLP, msg, pvUser, callback)) == SLP_OK) {
358*0Sstevel@tonic-gate (void) dereg_rereg(pURL);
359*0Sstevel@tonic-gate }
360*0Sstevel@tonic-gate
361*0Sstevel@tonic-gate free(pcScopeList);
362*0Sstevel@tonic-gate return (err);
363*0Sstevel@tonic-gate }
364*0Sstevel@tonic-gate
SLPDelAttrs(SLPHandle hSLP,const char * pURL,const char * pcAttrs,SLPRegReport callback,void * pvUser)365*0Sstevel@tonic-gate SLPError SLPDelAttrs(SLPHandle hSLP, const char *pURL,
366*0Sstevel@tonic-gate const char *pcAttrs,
367*0Sstevel@tonic-gate SLPRegReport callback, void *pvUser) {
368*0Sstevel@tonic-gate SLPError err;
369*0Sstevel@tonic-gate char *pcScopeList;
370*0Sstevel@tonic-gate struct reg_msg *msg;
371*0Sstevel@tonic-gate
372*0Sstevel@tonic-gate if (!hSLP || !pURL || !*pURL || !pcAttrs || !callback) {
373*0Sstevel@tonic-gate return (SLP_PARAMETER_BAD);
374*0Sstevel@tonic-gate }
375*0Sstevel@tonic-gate
376*0Sstevel@tonic-gate if ((strlen(pURL) > SLP_MAX_STRINGLEN) ||
377*0Sstevel@tonic-gate (strlen(pcAttrs) > SLP_MAX_STRINGLEN)) {
378*0Sstevel@tonic-gate return (SLP_PARAMETER_BAD);
379*0Sstevel@tonic-gate }
380*0Sstevel@tonic-gate
381*0Sstevel@tonic-gate if ((err = find_SAscopes(&pcScopeList))
382*0Sstevel@tonic-gate != SLP_OK) {
383*0Sstevel@tonic-gate return (err);
384*0Sstevel@tonic-gate }
385*0Sstevel@tonic-gate
386*0Sstevel@tonic-gate if ((err = slp_start_call(hSLP)) != SLP_OK)
387*0Sstevel@tonic-gate return (err);
388*0Sstevel@tonic-gate
389*0Sstevel@tonic-gate /* format params into msg */
390*0Sstevel@tonic-gate if ((err = packSrvDereg(hSLP, pURL, pcScopeList, pcAttrs, &msg))
391*0Sstevel@tonic-gate != SLP_OK) {
392*0Sstevel@tonic-gate free(pcScopeList);
393*0Sstevel@tonic-gate slp_end_call(hSLP);
394*0Sstevel@tonic-gate return (err);
395*0Sstevel@tonic-gate }
396*0Sstevel@tonic-gate
397*0Sstevel@tonic-gate free(pcScopeList);
398*0Sstevel@tonic-gate return (reg_common(hSLP, msg, pvUser, callback));
399*0Sstevel@tonic-gate }
400*0Sstevel@tonic-gate
packSrvDereg(slp_handle_impl_t * hp,const char * url,const char * scopes,const char * attrs,struct reg_msg ** msg)401*0Sstevel@tonic-gate static SLPError packSrvDereg(slp_handle_impl_t *hp, const char *url,
402*0Sstevel@tonic-gate const char *scopes, const char *attrs,
403*0Sstevel@tonic-gate struct reg_msg **msg) {
404*0Sstevel@tonic-gate char *m = NULL;
405*0Sstevel@tonic-gate SLPError err;
406*0Sstevel@tonic-gate size_t msgLen, tmplen, len = 0;
407*0Sstevel@tonic-gate
408*0Sstevel@tonic-gate /* create the reg_msg */
409*0Sstevel@tonic-gate *msg = NULL;
410*0Sstevel@tonic-gate if (!(*msg = calloc(1, sizeof (**msg)))) {
411*0Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "packSrvReg", "out of memory");
412*0Sstevel@tonic-gate return (SLP_MEMORY_ALLOC_FAILED);
413*0Sstevel@tonic-gate }
414*0Sstevel@tonic-gate
415*0Sstevel@tonic-gate /* compute the total message length */
416*0Sstevel@tonic-gate attrs = (attrs ? attrs : "");
417*0Sstevel@tonic-gate msgLen =
418*0Sstevel@tonic-gate slp_hdrlang_length(hp) +
419*0Sstevel@tonic-gate 2 + strlen(scopes) +
420*0Sstevel@tonic-gate /* URL entry */
421*0Sstevel@tonic-gate 5 + strlen(url) +
422*0Sstevel@tonic-gate 2 + strlen(attrs);
423*0Sstevel@tonic-gate
424*0Sstevel@tonic-gate if (!(m = calloc(msgLen, 1))) {
425*0Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "packSrvDereg", "out of memory");
426*0Sstevel@tonic-gate return (SLP_MEMORY_ALLOC_FAILED);
427*0Sstevel@tonic-gate }
428*0Sstevel@tonic-gate
429*0Sstevel@tonic-gate /*
430*0Sstevel@tonic-gate * Create iovec for the msg. The iovec components are layed out thus:
431*0Sstevel@tonic-gate * 0: header + URL
432*0Sstevel@tonic-gate * 1: URL auth block count, URL auth block
433*0Sstevel@tonic-gate * 2: attrs
434*0Sstevel@tonic-gate */
435*0Sstevel@tonic-gate if (!((*msg)->msgiov = calloc(3, sizeof (*((*msg)->msgiov))))) {
436*0Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "packSrvDereg", "out of memory");
437*0Sstevel@tonic-gate err = SLP_MEMORY_ALLOC_FAILED;
438*0Sstevel@tonic-gate goto error;
439*0Sstevel@tonic-gate }
440*0Sstevel@tonic-gate (*msg)->msgiov_len = 3;
441*0Sstevel@tonic-gate
442*0Sstevel@tonic-gate if ((err = slp_add_header(
443*0Sstevel@tonic-gate hp->locale, m, msgLen, SRVDEREG, 0, &len)) != SLP_OK)
444*0Sstevel@tonic-gate goto error;
445*0Sstevel@tonic-gate
446*0Sstevel@tonic-gate /* scopes */
447*0Sstevel@tonic-gate if ((err = slp_add_string(m, msgLen, scopes, &len)) != SLP_OK)
448*0Sstevel@tonic-gate goto error;
449*0Sstevel@tonic-gate
450*0Sstevel@tonic-gate /* URL Entry */
451*0Sstevel@tonic-gate len++; /* skip reserved byte in URL entry */
452*0Sstevel@tonic-gate if ((err = slp_add_sht(m, msgLen, 0, &len)) != SLP_OK)
453*0Sstevel@tonic-gate goto error;
454*0Sstevel@tonic-gate
455*0Sstevel@tonic-gate /* save pointer to URL for signing */
456*0Sstevel@tonic-gate tmplen = len;
457*0Sstevel@tonic-gate (*msg)->urlbytes.iov_base = m + len;
458*0Sstevel@tonic-gate
459*0Sstevel@tonic-gate if ((err = slp_add_string(m, msgLen, url, &len)) != SLP_OK)
460*0Sstevel@tonic-gate goto error;
461*0Sstevel@tonic-gate
462*0Sstevel@tonic-gate (*msg)->urlbytes.iov_len = len - tmplen;
463*0Sstevel@tonic-gate
464*0Sstevel@tonic-gate (*msg)->msgiov[0].iov_base = m;
465*0Sstevel@tonic-gate (*msg)->msgiov[0].iov_len = len;
466*0Sstevel@tonic-gate
467*0Sstevel@tonic-gate /* add auth blocks for URL */
468*0Sstevel@tonic-gate err = slp_sign(&((*msg)->urlbytes), 1, 0,
469*0Sstevel@tonic-gate (*msg)->msgiov, SLP_URL_AUTH);
470*0Sstevel@tonic-gate if (err != SLP_OK) {
471*0Sstevel@tonic-gate goto error;
472*0Sstevel@tonic-gate }
473*0Sstevel@tonic-gate
474*0Sstevel@tonic-gate (*msg)->msgiov[2].iov_base = m + len;
475*0Sstevel@tonic-gate
476*0Sstevel@tonic-gate /* tag list */
477*0Sstevel@tonic-gate if ((err = slp_add_string(m, msgLen, attrs, &len)) != SLP_OK)
478*0Sstevel@tonic-gate goto error;
479*0Sstevel@tonic-gate
480*0Sstevel@tonic-gate /* length of 2nd portion is len - length of 1st portion */
481*0Sstevel@tonic-gate (*msg)->msgiov[2].iov_len = len - (*msg)->msgiov[0].iov_len;
482*0Sstevel@tonic-gate
483*0Sstevel@tonic-gate /* adjust msgLen with authblocks, and set header length */
484*0Sstevel@tonic-gate msgLen += (*msg)->msgiov[SLP_URL_AUTH].iov_len;
485*0Sstevel@tonic-gate
486*0Sstevel@tonic-gate /* make sure msgLen is valid */
487*0Sstevel@tonic-gate if (msgLen > SLP_MAX_MSGLEN) {
488*0Sstevel@tonic-gate err = SLP_PARAMETER_BAD;
489*0Sstevel@tonic-gate goto error;
490*0Sstevel@tonic-gate }
491*0Sstevel@tonic-gate slp_set_length(m, msgLen);
492*0Sstevel@tonic-gate
493*0Sstevel@tonic-gate return (SLP_OK);
494*0Sstevel@tonic-gate error:
495*0Sstevel@tonic-gate if (m) free(m);
496*0Sstevel@tonic-gate if (*msg) {
497*0Sstevel@tonic-gate if ((*msg)->msgiov) free_msgiov((*msg)->msgiov, 3);
498*0Sstevel@tonic-gate free(*msg);
499*0Sstevel@tonic-gate }
500*0Sstevel@tonic-gate *msg = NULL;
501*0Sstevel@tonic-gate return (err);
502*0Sstevel@tonic-gate }
503*0Sstevel@tonic-gate
504*0Sstevel@tonic-gate /*
505*0Sstevel@tonic-gate * Passes the packed message to the routines which talk to slpd.
506*0Sstevel@tonic-gate */
reg_common(slp_handle_impl_t * hp,struct reg_msg * msg,void * cookie,SLPRegReport callback)507*0Sstevel@tonic-gate static SLPError reg_common(slp_handle_impl_t *hp, struct reg_msg *msg,
508*0Sstevel@tonic-gate void *cookie, SLPRegReport callback) {
509*0Sstevel@tonic-gate SLPError err;
510*0Sstevel@tonic-gate
511*0Sstevel@tonic-gate if (!slp_reg_thr_running)
512*0Sstevel@tonic-gate if ((err = start_reg_thr()) != SLP_OK)
513*0Sstevel@tonic-gate goto reg_done;
514*0Sstevel@tonic-gate
515*0Sstevel@tonic-gate if (hp->async)
516*0Sstevel@tonic-gate err = enqueue_reg(hp, msg, cookie, callback);
517*0Sstevel@tonic-gate else
518*0Sstevel@tonic-gate err = reg_impl(hp, msg, cookie, callback);
519*0Sstevel@tonic-gate
520*0Sstevel@tonic-gate reg_done:
521*0Sstevel@tonic-gate /* If an error occurred, end_call() will not have happened */
522*0Sstevel@tonic-gate if (err != SLP_OK)
523*0Sstevel@tonic-gate slp_end_call(hp);
524*0Sstevel@tonic-gate return (err);
525*0Sstevel@tonic-gate }
526*0Sstevel@tonic-gate
527*0Sstevel@tonic-gate /*
528*0Sstevel@tonic-gate * Put a reg message on the queue. Assumes reg_thread is running.
529*0Sstevel@tonic-gate */
enqueue_reg(slp_handle_impl_t * hp,struct reg_msg * msg,void * cookie,SLPRegReport cb)530*0Sstevel@tonic-gate static SLPError enqueue_reg(slp_handle_impl_t *hp, struct reg_msg *msg,
531*0Sstevel@tonic-gate void *cookie, SLPRegReport cb) {
532*0Sstevel@tonic-gate struct reg_q_msg *rmsg;
533*0Sstevel@tonic-gate
534*0Sstevel@tonic-gate if (!(rmsg = malloc(sizeof (*rmsg)))) {
535*0Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "enqueue_reg", "out of memory");
536*0Sstevel@tonic-gate return (SLP_MEMORY_ALLOC_FAILED);
537*0Sstevel@tonic-gate }
538*0Sstevel@tonic-gate
539*0Sstevel@tonic-gate rmsg->msg = msg;
540*0Sstevel@tonic-gate rmsg->hp = hp;
541*0Sstevel@tonic-gate rmsg->cb = cb;
542*0Sstevel@tonic-gate rmsg->cookie = cookie;
543*0Sstevel@tonic-gate
544*0Sstevel@tonic-gate return (slp_enqueue(reg_q, rmsg));
545*0Sstevel@tonic-gate }
546*0Sstevel@tonic-gate
547*0Sstevel@tonic-gate /*
548*0Sstevel@tonic-gate * Create a new reg_q and start the reg thread.
549*0Sstevel@tonic-gate */
start_reg_thr()550*0Sstevel@tonic-gate static SLPError start_reg_thr() {
551*0Sstevel@tonic-gate SLPError err = SLP_OK;
552*0Sstevel@tonic-gate int terr;
553*0Sstevel@tonic-gate
554*0Sstevel@tonic-gate (void) mutex_lock(&start_lock);
555*0Sstevel@tonic-gate /* make sure someone else hasn't already intialized the thread */
556*0Sstevel@tonic-gate if (slp_reg_thr_running) {
557*0Sstevel@tonic-gate goto start_done;
558*0Sstevel@tonic-gate }
559*0Sstevel@tonic-gate
560*0Sstevel@tonic-gate /* create the reg queue */
561*0Sstevel@tonic-gate reg_q = slp_new_queue(&err);
562*0Sstevel@tonic-gate if (err != SLP_OK) {
563*0Sstevel@tonic-gate goto start_done;
564*0Sstevel@tonic-gate }
565*0Sstevel@tonic-gate
566*0Sstevel@tonic-gate /* start the reg thread */
567*0Sstevel@tonic-gate if ((terr = thr_create(
568*0Sstevel@tonic-gate 0, NULL, (void *(*)(void *)) reg_thread,
569*0Sstevel@tonic-gate NULL, 0, NULL)) != 0) {
570*0Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "start_reg_thr",
571*0Sstevel@tonic-gate "could not start thread: %s",
572*0Sstevel@tonic-gate strerror(terr));
573*0Sstevel@tonic-gate slp_destroy_queue(reg_q);
574*0Sstevel@tonic-gate err = SLP_INTERNAL_SYSTEM_ERROR;
575*0Sstevel@tonic-gate goto start_done;
576*0Sstevel@tonic-gate }
577*0Sstevel@tonic-gate slp_reg_thr_running = 1;
578*0Sstevel@tonic-gate
579*0Sstevel@tonic-gate start_done:
580*0Sstevel@tonic-gate (void) mutex_unlock(&start_lock);
581*0Sstevel@tonic-gate return (err);
582*0Sstevel@tonic-gate }
583*0Sstevel@tonic-gate
584*0Sstevel@tonic-gate /*
585*0Sstevel@tonic-gate * This is what the permanent reg thread runs; it just sits in a loop
586*0Sstevel@tonic-gate * monitoring the reg_q for new reg messages.
587*0Sstevel@tonic-gate *
588*0Sstevel@tonic-gate * To conserve resources,
589*0Sstevel@tonic-gate * if there are no more registrations to refresh, it will exit.
590*0Sstevel@tonic-gate */
reg_thread()591*0Sstevel@tonic-gate static void reg_thread() {
592*0Sstevel@tonic-gate timestruc_t timeout;
593*0Sstevel@tonic-gate timeout.tv_nsec = 0;
594*0Sstevel@tonic-gate
595*0Sstevel@tonic-gate for (;;) {
596*0Sstevel@tonic-gate SLPBoolean etimed;
597*0Sstevel@tonic-gate struct reg_q_msg *rmsg;
598*0Sstevel@tonic-gate
599*0Sstevel@tonic-gate /* get the next message from the queue */
600*0Sstevel@tonic-gate timeout.tv_sec =
601*0Sstevel@tonic-gate next_wake_time ? next_wake_time : time(NULL) + 5;
602*0Sstevel@tonic-gate rmsg = slp_dequeue_timed(reg_q, &timeout, &etimed);
603*0Sstevel@tonic-gate if (!rmsg && etimed == SLP_TRUE) {
604*0Sstevel@tonic-gate /* timed out */
605*0Sstevel@tonic-gate if (!check_reregs()) {
606*0Sstevel@tonic-gate /* no more reregs; shut down this thread */
607*0Sstevel@tonic-gate (void) mutex_lock(&start_lock);
608*0Sstevel@tonic-gate slp_destroy_queue(reg_q);
609*0Sstevel@tonic-gate slp_reg_thr_running = 0;
610*0Sstevel@tonic-gate (void) mutex_unlock(&start_lock);
611*0Sstevel@tonic-gate thr_exit(NULL);
612*0Sstevel@tonic-gate }
613*0Sstevel@tonic-gate continue;
614*0Sstevel@tonic-gate }
615*0Sstevel@tonic-gate if (!rmsg)
616*0Sstevel@tonic-gate continue;
617*0Sstevel@tonic-gate
618*0Sstevel@tonic-gate /* got a new message */
619*0Sstevel@tonic-gate (void) reg_impl(rmsg->hp, rmsg->msg, rmsg->cookie, rmsg->cb);
620*0Sstevel@tonic-gate free(rmsg);
621*0Sstevel@tonic-gate (void) check_reregs();
622*0Sstevel@tonic-gate }
623*0Sstevel@tonic-gate }
624*0Sstevel@tonic-gate
625*0Sstevel@tonic-gate /*
626*0Sstevel@tonic-gate * Unpacks a SrvAck.
627*0Sstevel@tonic-gate * 'reply' should point to the beginning of the header.
628*0Sstevel@tonic-gate */
UnpackSrvAck(char * reply,SLPError * ans)629*0Sstevel@tonic-gate static SLPError UnpackSrvAck(char *reply, SLPError *ans) {
630*0Sstevel@tonic-gate SLPError err;
631*0Sstevel@tonic-gate unsigned short langlen, call_err;
632*0Sstevel@tonic-gate char *p = reply + SLP_HDRLEN;
633*0Sstevel@tonic-gate
634*0Sstevel@tonic-gate langlen = slp_get_langlen(reply);
635*0Sstevel@tonic-gate p += langlen;
636*0Sstevel@tonic-gate if ((err = slp_get_sht(p, 0, NULL, &call_err)) != SLP_OK)
637*0Sstevel@tonic-gate return (err);
638*0Sstevel@tonic-gate
639*0Sstevel@tonic-gate *ans = slp_map_err(call_err);
640*0Sstevel@tonic-gate
641*0Sstevel@tonic-gate return (SLP_OK);
642*0Sstevel@tonic-gate }
643*0Sstevel@tonic-gate
644*0Sstevel@tonic-gate /*
645*0Sstevel@tonic-gate * The dispatcher for SA messages. Sends a message to slpd, unpacks and
646*0Sstevel@tonic-gate * dispatches the reply to the user callback.
647*0Sstevel@tonic-gate */
reg_impl(slp_handle_impl_t * hp,struct reg_msg * msg,void * cookie,SLPRegReport cb)648*0Sstevel@tonic-gate static SLPError reg_impl(slp_handle_impl_t *hp, struct reg_msg *msg,
649*0Sstevel@tonic-gate void *cookie, SLPRegReport cb) {
650*0Sstevel@tonic-gate char *reply = NULL;
651*0Sstevel@tonic-gate SLPError err, call_err;
652*0Sstevel@tonic-gate
653*0Sstevel@tonic-gate if (hp->cancel)
654*0Sstevel@tonic-gate goto transaction_complete;
655*0Sstevel@tonic-gate
656*0Sstevel@tonic-gate if ((err = slp_send2slpd_iov(msg->msgiov, msg->msgiov_len, &reply))
657*0Sstevel@tonic-gate != SLP_OK)
658*0Sstevel@tonic-gate goto transaction_complete;
659*0Sstevel@tonic-gate
660*0Sstevel@tonic-gate /* through with msg, so free it now */
661*0Sstevel@tonic-gate free_msgiov(msg->msgiov, msg->msgiov_len);
662*0Sstevel@tonic-gate free(msg);
663*0Sstevel@tonic-gate
664*0Sstevel@tonic-gate if ((err = UnpackSrvAck(reply, &call_err)) != SLP_OK)
665*0Sstevel@tonic-gate goto transaction_complete;
666*0Sstevel@tonic-gate
667*0Sstevel@tonic-gate /* the reg thread doubles as the consumer thread for SA calls */
668*0Sstevel@tonic-gate hp->consumer_tid = thr_self();
669*0Sstevel@tonic-gate
670*0Sstevel@tonic-gate cb(hp, call_err, cookie);
671*0Sstevel@tonic-gate
672*0Sstevel@tonic-gate transaction_complete:
673*0Sstevel@tonic-gate if (reply) {
674*0Sstevel@tonic-gate free(reply);
675*0Sstevel@tonic-gate }
676*0Sstevel@tonic-gate slp_end_call(hp);
677*0Sstevel@tonic-gate return (err);
678*0Sstevel@tonic-gate }
679*0Sstevel@tonic-gate
680*0Sstevel@tonic-gate /*
681*0Sstevel@tonic-gate * Re-registration routines
682*0Sstevel@tonic-gate */
683*0Sstevel@tonic-gate
684*0Sstevel@tonic-gate /*
685*0Sstevel@tonic-gate * Adds the registration contained in 'msg' to the refresh registration
686*0Sstevel@tonic-gate * list managed by reg_thread.
687*0Sstevel@tonic-gate * Only registrations which are meant to be permanent are refreshed,
688*0Sstevel@tonic-gate * so we only allow reg's with lifetime == SLP_LIFETIME_PERMANENT into
689*0Sstevel@tonic-gate * the rereg table.
690*0Sstevel@tonic-gate */
add_rereg(const char * url,struct reg_msg * msg,unsigned short lifetime)691*0Sstevel@tonic-gate static SLPError add_rereg(const char *url, struct reg_msg *msg,
692*0Sstevel@tonic-gate unsigned short lifetime) {
693*0Sstevel@tonic-gate struct rereg_entry *reg;
694*0Sstevel@tonic-gate SLPError err = SLP_OK;
695*0Sstevel@tonic-gate
696*0Sstevel@tonic-gate if (lifetime != SLP_LIFETIME_MAXIMUM) {
697*0Sstevel@tonic-gate return (SLP_OK);
698*0Sstevel@tonic-gate }
699*0Sstevel@tonic-gate
700*0Sstevel@tonic-gate (void) mutex_lock(&rereg_lock);
701*0Sstevel@tonic-gate /* alloc a new rereg entry */
702*0Sstevel@tonic-gate if (!(reg = malloc(sizeof (*reg)))) {
703*0Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "add_rereg", "out of memory");
704*0Sstevel@tonic-gate err = SLP_MEMORY_ALLOC_FAILED;
705*0Sstevel@tonic-gate goto done;
706*0Sstevel@tonic-gate }
707*0Sstevel@tonic-gate
708*0Sstevel@tonic-gate if (!(reg->url = strdup(url))) {
709*0Sstevel@tonic-gate free(reg);
710*0Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "add_rereg", "out of memory");
711*0Sstevel@tonic-gate err = SLP_MEMORY_ALLOC_FAILED;
712*0Sstevel@tonic-gate goto done;
713*0Sstevel@tonic-gate }
714*0Sstevel@tonic-gate
715*0Sstevel@tonic-gate reg->msg = msg;
716*0Sstevel@tonic-gate reg->lifetime = lifetime;
717*0Sstevel@tonic-gate reg->wake_time = (time(NULL) + lifetime) - 60;
718*0Sstevel@tonic-gate reg->next = NULL;
719*0Sstevel@tonic-gate
720*0Sstevel@tonic-gate /* adjust the next wake time if necessary */
721*0Sstevel@tonic-gate next_wake_time =
722*0Sstevel@tonic-gate reg->wake_time < next_wake_time ?
723*0Sstevel@tonic-gate reg->wake_time : next_wake_time;
724*0Sstevel@tonic-gate
725*0Sstevel@tonic-gate /* add the rereg to the list */
726*0Sstevel@tonic-gate if (!reregs) {
727*0Sstevel@tonic-gate /* first one */
728*0Sstevel@tonic-gate reregs = reg;
729*0Sstevel@tonic-gate goto done;
730*0Sstevel@tonic-gate }
731*0Sstevel@tonic-gate
732*0Sstevel@tonic-gate /* else add it to the beginning of the list */
733*0Sstevel@tonic-gate reg->next = reregs;
734*0Sstevel@tonic-gate reregs = reg;
735*0Sstevel@tonic-gate
736*0Sstevel@tonic-gate done:
737*0Sstevel@tonic-gate (void) mutex_unlock(&rereg_lock);
738*0Sstevel@tonic-gate return (err);
739*0Sstevel@tonic-gate }
740*0Sstevel@tonic-gate
741*0Sstevel@tonic-gate /*
742*0Sstevel@tonic-gate * Walks through the rereg list and re-registers any which will expire
743*0Sstevel@tonic-gate * before the reg thread wakes up and checks again.
744*0Sstevel@tonic-gate * Returns true if there are more reregs on the list, false if none.
745*0Sstevel@tonic-gate */
check_reregs()746*0Sstevel@tonic-gate static SLPBoolean check_reregs() {
747*0Sstevel@tonic-gate struct rereg_entry *p;
748*0Sstevel@tonic-gate time_t now, shortest_wait;
749*0Sstevel@tonic-gate SLPBoolean more = SLP_TRUE;
750*0Sstevel@tonic-gate
751*0Sstevel@tonic-gate (void) mutex_lock(&rereg_lock);
752*0Sstevel@tonic-gate
753*0Sstevel@tonic-gate if (!reregs) {
754*0Sstevel@tonic-gate more = SLP_FALSE;
755*0Sstevel@tonic-gate goto done;
756*0Sstevel@tonic-gate }
757*0Sstevel@tonic-gate
758*0Sstevel@tonic-gate now = time(NULL);
759*0Sstevel@tonic-gate shortest_wait = now + reregs->lifetime;
760*0Sstevel@tonic-gate
761*0Sstevel@tonic-gate for (p = reregs; p; p = p->next) {
762*0Sstevel@tonic-gate if (now > (p->wake_time - granularity)) {
763*0Sstevel@tonic-gate char *reply;
764*0Sstevel@tonic-gate
765*0Sstevel@tonic-gate /* rereg it, first recalculating signature */
766*0Sstevel@tonic-gate (void) slp_sign(&(p->msg->urlbytes), 1, now + p->lifetime,
767*0Sstevel@tonic-gate p->msg->msgiov, 1);
768*0Sstevel@tonic-gate (void) slp_sign(&(p->msg->attrbytes), 1, now + p->lifetime,
769*0Sstevel@tonic-gate p->msg->msgiov, 3);
770*0Sstevel@tonic-gate
771*0Sstevel@tonic-gate (void) slp_send2slpd_iov(
772*0Sstevel@tonic-gate p->msg->msgiov, p->msg->msgiov_len, &reply);
773*0Sstevel@tonic-gate if (reply)
774*0Sstevel@tonic-gate free(reply);
775*0Sstevel@tonic-gate
776*0Sstevel@tonic-gate p->wake_time = now + p->lifetime;
777*0Sstevel@tonic-gate }
778*0Sstevel@tonic-gate
779*0Sstevel@tonic-gate if (p->wake_time < shortest_wait)
780*0Sstevel@tonic-gate shortest_wait = p->wake_time;
781*0Sstevel@tonic-gate }
782*0Sstevel@tonic-gate next_wake_time = shortest_wait;
783*0Sstevel@tonic-gate
784*0Sstevel@tonic-gate done:
785*0Sstevel@tonic-gate (void) mutex_unlock(&rereg_lock);
786*0Sstevel@tonic-gate return (more);
787*0Sstevel@tonic-gate }
788*0Sstevel@tonic-gate
789*0Sstevel@tonic-gate /*
790*0Sstevel@tonic-gate * Removes the refresh registration for 'url'.
791*0Sstevel@tonic-gate */
dereg_rereg(const char * url)792*0Sstevel@tonic-gate static unsigned short dereg_rereg(const char *url) {
793*0Sstevel@tonic-gate struct rereg_entry *p, *q;
794*0Sstevel@tonic-gate unsigned short lifetime = 0;
795*0Sstevel@tonic-gate
796*0Sstevel@tonic-gate (void) mutex_lock(&rereg_lock);
797*0Sstevel@tonic-gate for (p = q = reregs; p; p = p->next) {
798*0Sstevel@tonic-gate if (slp_strcasecmp(p->url, url) == 0) {
799*0Sstevel@tonic-gate /* found it; remove it from the list */
800*0Sstevel@tonic-gate if (p == q) {
801*0Sstevel@tonic-gate /* first one on list */
802*0Sstevel@tonic-gate reregs = p->next;
803*0Sstevel@tonic-gate } else {
804*0Sstevel@tonic-gate q->next = p->next;
805*0Sstevel@tonic-gate }
806*0Sstevel@tonic-gate
807*0Sstevel@tonic-gate /* free the entry */
808*0Sstevel@tonic-gate lifetime = p->lifetime;
809*0Sstevel@tonic-gate free(p->url);
810*0Sstevel@tonic-gate /* free the message memory */
811*0Sstevel@tonic-gate free(p->msg->msgiov[0].iov_base);
812*0Sstevel@tonic-gate /* free the URL auth block */
813*0Sstevel@tonic-gate free(p->msg->msgiov[SLP_URL_AUTH].iov_base);
814*0Sstevel@tonic-gate /* free the attr auth block */
815*0Sstevel@tonic-gate free(p->msg->msgiov[SLP_ATTR_AUTH].iov_base);
816*0Sstevel@tonic-gate /* free the message iovec */
817*0Sstevel@tonic-gate free(p->msg->msgiov);
818*0Sstevel@tonic-gate /* finally, free the message structure */
819*0Sstevel@tonic-gate free(p->msg);
820*0Sstevel@tonic-gate free(p);
821*0Sstevel@tonic-gate
822*0Sstevel@tonic-gate goto done;
823*0Sstevel@tonic-gate }
824*0Sstevel@tonic-gate
825*0Sstevel@tonic-gate q = p;
826*0Sstevel@tonic-gate }
827*0Sstevel@tonic-gate
828*0Sstevel@tonic-gate done:
829*0Sstevel@tonic-gate (void) mutex_unlock(&rereg_lock);
830*0Sstevel@tonic-gate return (lifetime);
831*0Sstevel@tonic-gate }
832*0Sstevel@tonic-gate
833*0Sstevel@tonic-gate /*
834*0Sstevel@tonic-gate * Returns configured scopes in scopes. Caller should free *scopes
835*0Sstevel@tonic-gate * when done. If the scope string is too long for an SLP string, the
836*0Sstevel@tonic-gate * string is truncated.
837*0Sstevel@tonic-gate */
find_SAscopes(char ** scopes)838*0Sstevel@tonic-gate static SLPError find_SAscopes(char **scopes) {
839*0Sstevel@tonic-gate SLPError err;
840*0Sstevel@tonic-gate
841*0Sstevel@tonic-gate if ((err = slp_administrative_scopes(scopes, SLP_TRUE))
842*0Sstevel@tonic-gate != SLP_OK) {
843*0Sstevel@tonic-gate return (err);
844*0Sstevel@tonic-gate }
845*0Sstevel@tonic-gate
846*0Sstevel@tonic-gate /* Ensure string is not too long */
847*0Sstevel@tonic-gate if (strlen(*scopes) > SLP_MAX_STRINGLEN) {
848*0Sstevel@tonic-gate /* truncate the string */
849*0Sstevel@tonic-gate if ((*scopes)[SLP_MAX_STRINGLEN - 1] == ',') {
850*0Sstevel@tonic-gate /* scopes can't end with ',' */
851*0Sstevel@tonic-gate (*scopes)[SLP_MAX_STRINGLEN - 1] = 0;
852*0Sstevel@tonic-gate } else {
853*0Sstevel@tonic-gate (*scopes)[SLP_MAX_STRINGLEN] = 0;
854*0Sstevel@tonic-gate }
855*0Sstevel@tonic-gate }
856*0Sstevel@tonic-gate
857*0Sstevel@tonic-gate return (SLP_OK);
858*0Sstevel@tonic-gate }
859*0Sstevel@tonic-gate
860*0Sstevel@tonic-gate /*
861*0Sstevel@tonic-gate * Does all the dirty work of freeing a msgiov.
862*0Sstevel@tonic-gate */
free_msgiov(struct iovec * msgiov,int iovlen)863*0Sstevel@tonic-gate static void free_msgiov(struct iovec *msgiov, int iovlen) {
864*0Sstevel@tonic-gate /* free the message memory */
865*0Sstevel@tonic-gate free(msgiov[0].iov_base);
866*0Sstevel@tonic-gate /* free the URL auth block */
867*0Sstevel@tonic-gate free(msgiov[SLP_URL_AUTH].iov_base);
868*0Sstevel@tonic-gate if (iovlen == 4) {
869*0Sstevel@tonic-gate /* free the attr auth block */
870*0Sstevel@tonic-gate free(msgiov[SLP_ATTR_AUTH].iov_base);
871*0Sstevel@tonic-gate }
872*0Sstevel@tonic-gate /* free the message iovec */
873*0Sstevel@tonic-gate free(msgiov);
874*0Sstevel@tonic-gate }
875