1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1999 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 /*
28  * Implements SLPGetRefreshInterval. This call is an AttrRqst with
29  * the special service type service:directory-agent.sun, sent
30  * only to slpd via loopback, so it mimics the course of a normal
31  * SLPFindAttrs call but reroutes the message to slpd.
32  */
33 
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <syslog.h>
37 #include <netdb.h>
38 #include <slp-internal.h>
39 
40 static SLPBoolean refresh_interval_cb(SLPHandle, const char *,
41 					SLPError, void *);
42 
SLPGetRefreshInterval()43 unsigned short SLPGetRefreshInterval() {
44 	slp_handle_impl_t *hp;	/* SLP handle for this request */
45 	SLPError err;		/* any SLPError */
46 	char *reply = NULL;	/* reply from slpd */
47 	void *collator = NULL;	/* attr collation handle */
48 	int mr = 0;		/* max results placeholder */
49 	unsigned short max = 0;	/* max interval result cookie */
50 	char *msg = NULL;	/* attrrqst msg */
51 	char hostname[MAXHOSTNAMELEN];	/* name of this host */
52 
53 	if ((err = SLPOpen("en", SLP_FALSE, (void **)&hp)) != SLP_OK) {
54 	    slp_err(LOG_INFO, 0, "SLPGetRefreshInterval",
55 		    "Could not get SLPHandle: %s", slp_strerror(err));
56 	    return (0);
57 	}
58 
59 	/* tag this as an internal call */
60 	hp->internal_call = SLP_TRUE;
61 
62 	/* scope is name of this host */
63 	(void) gethostname(hostname, MAXHOSTNAMELEN);
64 
65 	if (slp_packAttrRqst_single(SLP_SUN_DA_TYPE,
66 				    hostname,
67 				    "min-refresh-interval",
68 				    &msg, "en") != SLP_OK) {
69 	    goto done;
70 	}
71 
72 	if (slp_send2slpd(msg, &reply) != SLP_OK) {
73 	    goto done;
74 	}
75 
76 	(void) slp_UnpackAttrReply(hp, reply, refresh_interval_cb,
77 				    &max, &collator, &mr);
78 
79 	/* clean up by invoking last call */
80 	(void) slp_UnpackAttrReply(hp, NULL, refresh_interval_cb,
81 				    &max, &collator, &mr);
82 
83 done:
84 	if (msg) free(msg);
85 	if (reply) free(reply);
86 
87 	SLPClose(hp);
88 
89 	return (max);
90 }
91 
92 /*ARGSUSED*/
refresh_interval_cb(SLPHandle h,const char * attrs,SLPError err,void * cookie)93 static SLPBoolean refresh_interval_cb(SLPHandle h, const char *attrs,
94 					SLPError err, void *cookie) {
95 	char *p, *next;
96 	unsigned short *max = (unsigned short *)cookie;
97 
98 	if (err != SLP_OK) {
99 	    return (SLP_TRUE);
100 	}
101 
102 	p = strchr(attrs, '=');
103 	if (!p) {
104 	    *max = 0;
105 	}
106 
107 	/* walk through all intervals, looking for the greatest */
108 	for (p++; p; p = next) {
109 	    unsigned short anint;
110 
111 	    next = strchr(p, ',');
112 	    if (next) {
113 		*next++ = 0;
114 	    }
115 
116 	    anint = (unsigned short)atoi(p);
117 	    if (anint > *max) {
118 		*max = anint;
119 	    }
120 	}
121 
122 	return (SLP_TRUE);
123 }
124