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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Implements SLPGetRefreshInterval. This call is an AttrRqst with
31  * the special service type service:directory-agent.sun, sent
32  * only to slpd via loopback, so it mimics the course of a normal
33  * SLPFindAttrs call but reroutes the message to slpd.
34  */
35 
36 #include <stdlib.h>
37 #include <unistd.h>
38 #include <syslog.h>
39 #include <netdb.h>
40 #include <slp-internal.h>
41 
42 static SLPBoolean refresh_interval_cb(SLPHandle, const char *,
43 					SLPError, void *);
44 
SLPGetRefreshInterval()45 unsigned short SLPGetRefreshInterval() {
46 	slp_handle_impl_t *hp;	/* SLP handle for this request */
47 	SLPError err;		/* any SLPError */
48 	char *reply = NULL;	/* reply from slpd */
49 	void *collator = NULL;	/* attr collation handle */
50 	int mr = 0;		/* max results placeholder */
51 	unsigned short max = 0;	/* max interval result cookie */
52 	char *msg = NULL;	/* attrrqst msg */
53 	char hostname[MAXHOSTNAMELEN];	/* name of this host */
54 
55 	if ((err = SLPOpen("en", SLP_FALSE, (void **)&hp)) != SLP_OK) {
56 	    slp_err(LOG_INFO, 0, "SLPGetRefreshInterval",
57 		    "Could not get SLPHandle: %s", slp_strerror(err));
58 	    return (0);
59 	}
60 
61 	/* tag this as an internal call */
62 	hp->internal_call = SLP_TRUE;
63 
64 	/* scope is name of this host */
65 	(void) gethostname(hostname, MAXHOSTNAMELEN);
66 
67 	if (slp_packAttrRqst_single(SLP_SUN_DA_TYPE,
68 				    hostname,
69 				    "min-refresh-interval",
70 				    &msg, "en") != SLP_OK) {
71 	    goto done;
72 	}
73 
74 	if (slp_send2slpd(msg, &reply) != SLP_OK) {
75 	    goto done;
76 	}
77 
78 	(void) slp_UnpackAttrReply(hp, reply, refresh_interval_cb,
79 				    &max, &collator, &mr);
80 
81 	/* clean up by invoking last call */
82 	(void) slp_UnpackAttrReply(hp, NULL, refresh_interval_cb,
83 				    &max, &collator, &mr);
84 
85 done:
86 	if (msg) free(msg);
87 	if (reply) free(reply);
88 
89 	SLPClose(hp);
90 
91 	return (max);
92 }
93 
94 /*ARGSUSED*/
refresh_interval_cb(SLPHandle h,const char * attrs,SLPError err,void * cookie)95 static SLPBoolean refresh_interval_cb(SLPHandle h, const char *attrs,
96 					SLPError err, void *cookie) {
97 	char *p, *next;
98 	unsigned short *max = (unsigned short *)cookie;
99 
100 	if (err != SLP_OK) {
101 	    return (SLP_TRUE);
102 	}
103 
104 	p = strchr(attrs, '=');
105 	if (!p) {
106 	    *max = 0;
107 	}
108 
109 	/* walk through all intervals, looking for the greatest */
110 	for (p++; p; p = next) {
111 	    unsigned short anint;
112 
113 	    next = strchr(p, ',');
114 	    if (next) {
115 		*next++ = 0;
116 	    }
117 
118 	    anint = (unsigned short)atoi(p);
119 	    if (anint > *max) {
120 		*max = anint;
121 	    }
122 	}
123 
124 	return (SLP_TRUE);
125 }
126