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