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