xref: /titanic_50/usr/src/cmd/nscd/getnode.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 2003 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
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  * Routines to handle getipnode* calls in nscd. Note that the
31*7c478bd9Sstevel@tonic-gate  * getnodeby* APIs were renamed getipnodeby*. The interfaces
32*7c478bd9Sstevel@tonic-gate  * related to them in the nscd will remain as getnode*.
33*7c478bd9Sstevel@tonic-gate  */
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #include <assert.h>
36*7c478bd9Sstevel@tonic-gate #include <errno.h>
37*7c478bd9Sstevel@tonic-gate #include <memory.h>
38*7c478bd9Sstevel@tonic-gate #include <signal.h>
39*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
40*7c478bd9Sstevel@tonic-gate #include <stdio.h>
41*7c478bd9Sstevel@tonic-gate #include <string.h>
42*7c478bd9Sstevel@tonic-gate #include <strings.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/door.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/wait.h>
48*7c478bd9Sstevel@tonic-gate #include <thread.h>
49*7c478bd9Sstevel@tonic-gate #include <unistd.h>
50*7c478bd9Sstevel@tonic-gate #include <ucred.h>
51*7c478bd9Sstevel@tonic-gate #include <nss_common.h>
52*7c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate #include "getxby_door.h"
55*7c478bd9Sstevel@tonic-gate #include "server_door.h"
56*7c478bd9Sstevel@tonic-gate #include "nscd.h"
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate static hash_t *addr_hash;		/* node address hash */
59*7c478bd9Sstevel@tonic-gate static hash_t *nnam_hash;		/* node name hash */
60*7c478bd9Sstevel@tonic-gate static mutex_t  node_lock = DEFAULTMUTEX;
61*7c478bd9Sstevel@tonic-gate static waiter_t node_wait;
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate static void getnode_addrkeepalive(int keep, int interval);
64*7c478bd9Sstevel@tonic-gate static void getnode_invalidate_unlocked(void);
65*7c478bd9Sstevel@tonic-gate static void getnode_namekeepalive(int keep, int interval);
66*7c478bd9Sstevel@tonic-gate static void update_node_bucket(nsc_bucket_t ** old, nsc_bucket_t *new,
67*7c478bd9Sstevel@tonic-gate 		int callnumber);
68*7c478bd9Sstevel@tonic-gate static nsc_bucket_t *fixbuffer(nsc_return_t *in, int maxlen);
69*7c478bd9Sstevel@tonic-gate static void do_findnaddrs(nsc_bucket_t *ptr, int *table, char *addr);
70*7c478bd9Sstevel@tonic-gate static void do_findnnams(nsc_bucket_t *ptr, int *table, char *name);
71*7c478bd9Sstevel@tonic-gate static void do_invalidate(nsc_bucket_t ** ptr, int callnumber);
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate /*
74*7c478bd9Sstevel@tonic-gate  * Create a combined key
75*7c478bd9Sstevel@tonic-gate  * combined key =
76*7c478bd9Sstevel@tonic-gate  * name + ":" + af_family(in %d format) + ":" + flags(in %d format)
77*7c478bd9Sstevel@tonic-gate  * name = foo, af_family = 26, flags = 3, addrconfig = 1
78*7c478bd9Sstevel@tonic-gate  * key = "foo:26:3"
79*7c478bd9Sstevel@tonic-gate  * The string is allocated and caller has to free it.
80*7c478bd9Sstevel@tonic-gate  *
81*7c478bd9Sstevel@tonic-gate  * Insert ":" in between to ensure the uniqueness of the key.
82*7c478bd9Sstevel@tonic-gate  *
83*7c478bd9Sstevel@tonic-gate  * af_family: [2 | 26] ([AF_INET  | AF_INET6])
84*7c478bd9Sstevel@tonic-gate  * flags: 0 - strict
85*7c478bd9Sstevel@tonic-gate  *	  1 - AI_V4MAPPED
86*7c478bd9Sstevel@tonic-gate  *	  3 - (AI_V4MAPPED | AI_ALL)
87*7c478bd9Sstevel@tonic-gate  *	  4 - AI_ADDRCONFIG
88*7c478bd9Sstevel@tonic-gate  *	  5 - AI_DEFAULT (AI_V4MAPPED | AI_ADDRCONFIG)
89*7c478bd9Sstevel@tonic-gate  *	  7 - (AI_V4MAPPED | AI_ALL | AI_ADDRCONFIG)
90*7c478bd9Sstevel@tonic-gate  */
91*7c478bd9Sstevel@tonic-gate static char *
92*7c478bd9Sstevel@tonic-gate get_ipnode_combined_key(nsc_call_t *in) {
93*7c478bd9Sstevel@tonic-gate 	int	len;
94*7c478bd9Sstevel@tonic-gate 	char	*key;
95*7c478bd9Sstevel@tonic-gate 	char	af_str[8];		/* in %d  format */
96*7c478bd9Sstevel@tonic-gate 	char	flags_str[8];		/* in %d  format */
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 	snprintf(af_str, 8, "%d", in->nsc_u.ipnode.af_family);
99*7c478bd9Sstevel@tonic-gate 	snprintf(flags_str, 8, "%d", in->nsc_u.ipnode.flags);
100*7c478bd9Sstevel@tonic-gate 	/* name + ":" + af_str + ":" + flags_str + 1 */
101*7c478bd9Sstevel@tonic-gate 	len = strlen(in->nsc_u.ipnode.name) + strlen(af_str) +
102*7c478bd9Sstevel@tonic-gate 		strlen(flags_str) +  3;
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 	if ((key = (char *)malloc(len)) == NULL)
105*7c478bd9Sstevel@tonic-gate 		return (NULL);
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 	snprintf(key, len, "%s:%s:%s", in->nsc_u.ipnode.name, af_str,
108*7c478bd9Sstevel@tonic-gate 		flags_str);
109*7c478bd9Sstevel@tonic-gate 	return (key);
110*7c478bd9Sstevel@tonic-gate }
111*7c478bd9Sstevel@tonic-gate /*
112*7c478bd9Sstevel@tonic-gate  * Parse key and copy the values to out
113*7c478bd9Sstevel@tonic-gate  */
114*7c478bd9Sstevel@tonic-gate static void
115*7c478bd9Sstevel@tonic-gate copy_ipnode_combined_key(char *key, nsc_call_t *out) {
116*7c478bd9Sstevel@tonic-gate 	char	*token, *lasts;
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate 	if ((token = strtok_r(key, ":", &lasts)) != NULL) {
119*7c478bd9Sstevel@tonic-gate 		/* copy name */
120*7c478bd9Sstevel@tonic-gate 		(void) strncpy(out->nsc_u.ipnode.name, token,
121*7c478bd9Sstevel@tonic-gate 						NSCDMAXNAMELEN);
122*7c478bd9Sstevel@tonic-gate 		if ((token = strtok_r(NULL, ":", &lasts)) != NULL) {
123*7c478bd9Sstevel@tonic-gate 			/* Copy af_family */
124*7c478bd9Sstevel@tonic-gate 			out->nsc_u.ipnode.af_family = atoi(token);
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 			if ((token = strtok_r(NULL, ":", &lasts)) != NULL) {
127*7c478bd9Sstevel@tonic-gate 				/* Copy flags */
128*7c478bd9Sstevel@tonic-gate 				out->nsc_u.ipnode.flags = atoi(token);
129*7c478bd9Sstevel@tonic-gate 			}
130*7c478bd9Sstevel@tonic-gate 		}
131*7c478bd9Sstevel@tonic-gate 	}
132*7c478bd9Sstevel@tonic-gate }
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate static void
135*7c478bd9Sstevel@tonic-gate safe_inet_ntop(int af, const void  *addr,  char  *cp, size_t size)
136*7c478bd9Sstevel@tonic-gate {
137*7c478bd9Sstevel@tonic-gate 	if (inet_ntop(af, addr, cp, size) != cp) {
138*7c478bd9Sstevel@tonic-gate 		/*
139*7c478bd9Sstevel@tonic-gate 		 * Called only for logging purposes...
140*7c478bd9Sstevel@tonic-gate 		 * this should never happen in the nscd... but
141*7c478bd9Sstevel@tonic-gate 		 * just in case we'll make sure we have a
142*7c478bd9Sstevel@tonic-gate 		 * plausible error message.
143*7c478bd9Sstevel@tonic-gate 		 */
144*7c478bd9Sstevel@tonic-gate 		(void) snprintf(cp, size, "<inet_ntop returned %s>",
145*7c478bd9Sstevel@tonic-gate 		    strerror(errno));
146*7c478bd9Sstevel@tonic-gate 	}
147*7c478bd9Sstevel@tonic-gate }
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate void
150*7c478bd9Sstevel@tonic-gate getnode_init(void)
151*7c478bd9Sstevel@tonic-gate {
152*7c478bd9Sstevel@tonic-gate 	addr_hash = make_hash(current_admin.node.nsc_suggestedsize);
153*7c478bd9Sstevel@tonic-gate 	nnam_hash = make_hash(current_admin.node.nsc_suggestedsize);
154*7c478bd9Sstevel@tonic-gate }
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate static void
157*7c478bd9Sstevel@tonic-gate do_invalidate(nsc_bucket_t ** ptr, int callnumber)
158*7c478bd9Sstevel@tonic-gate {
159*7c478bd9Sstevel@tonic-gate 	if (*ptr != NULL && *ptr != (nsc_bucket_t *)-1) {
160*7c478bd9Sstevel@tonic-gate 		/* leave pending calls alone */
161*7c478bd9Sstevel@tonic-gate 		update_node_bucket(ptr, NULL, callnumber);
162*7c478bd9Sstevel@tonic-gate 	}
163*7c478bd9Sstevel@tonic-gate }
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate static void
166*7c478bd9Sstevel@tonic-gate do_findnnams(nsc_bucket_t *ptr, int *table, char *name)
167*7c478bd9Sstevel@tonic-gate {
168*7c478bd9Sstevel@tonic-gate 	/*
169*7c478bd9Sstevel@tonic-gate 	 * be careful with ptr - it may be -1 or NULL.
170*7c478bd9Sstevel@tonic-gate 	 */
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate 	if (ptr != NULL && ptr != (nsc_bucket_t *)-1) {
173*7c478bd9Sstevel@tonic-gate 		/* leave pending calls alone */
174*7c478bd9Sstevel@tonic-gate 		char *tmp = (char *)insertn(table, ptr->nsc_hits,
175*7c478bd9Sstevel@tonic-gate 			(int)strdup(name));
176*7c478bd9Sstevel@tonic-gate 		if (tmp != (char *)-1)
177*7c478bd9Sstevel@tonic-gate 			free(tmp);
178*7c478bd9Sstevel@tonic-gate 	}
179*7c478bd9Sstevel@tonic-gate }
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate static void
182*7c478bd9Sstevel@tonic-gate do_findnaddrs(nsc_bucket_t *ptr, int *table, char *addr)
183*7c478bd9Sstevel@tonic-gate {
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate 	if (ptr != NULL && ptr != (nsc_bucket_t *)-1) {
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate 		/* leave pending calls alone */
188*7c478bd9Sstevel@tonic-gate 		char *tmp = (char *)insertn(table, ptr->nsc_hits,
189*7c478bd9Sstevel@tonic-gate 			(int)strdup(addr));
190*7c478bd9Sstevel@tonic-gate 		if (tmp != (char *)-1)
191*7c478bd9Sstevel@tonic-gate 			free(tmp);
192*7c478bd9Sstevel@tonic-gate 	}
193*7c478bd9Sstevel@tonic-gate }
194*7c478bd9Sstevel@tonic-gate 
195*7c478bd9Sstevel@tonic-gate void
196*7c478bd9Sstevel@tonic-gate getnode_revalidate(void)
197*7c478bd9Sstevel@tonic-gate {
198*7c478bd9Sstevel@tonic-gate 	for (;;) {
199*7c478bd9Sstevel@tonic-gate 		int slp;
200*7c478bd9Sstevel@tonic-gate 		int interval;
201*7c478bd9Sstevel@tonic-gate 		int count;
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate 		slp = current_admin.node.nsc_pos_ttl;
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate 		if (slp < 60)
206*7c478bd9Sstevel@tonic-gate 			slp = 60;
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 		if ((count = current_admin.node.nsc_keephot) != 0) {
209*7c478bd9Sstevel@tonic-gate 			interval = (slp/2)/count;
210*7c478bd9Sstevel@tonic-gate 			if (interval == 0) interval = 1;
211*7c478bd9Sstevel@tonic-gate 			(void) sleep(slp*2/3);
212*7c478bd9Sstevel@tonic-gate 			getnode_namekeepalive(count, interval);
213*7c478bd9Sstevel@tonic-gate 			getnode_addrkeepalive(count, interval);
214*7c478bd9Sstevel@tonic-gate 		} else {
215*7c478bd9Sstevel@tonic-gate 			(void) sleep(slp);
216*7c478bd9Sstevel@tonic-gate 		}
217*7c478bd9Sstevel@tonic-gate 	}
218*7c478bd9Sstevel@tonic-gate }
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate static void
221*7c478bd9Sstevel@tonic-gate getnode_namekeepalive(int keep, int interval)
222*7c478bd9Sstevel@tonic-gate {
223*7c478bd9Sstevel@tonic-gate 	int *table;
224*7c478bd9Sstevel@tonic-gate 	union {
225*7c478bd9Sstevel@tonic-gate 		nsc_data_t  ping;
226*7c478bd9Sstevel@tonic-gate 		char space[sizeof (nsc_data_t) + NSCDMAXNAMELEN];
227*7c478bd9Sstevel@tonic-gate 	} u;
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate 	int i;
230*7c478bd9Sstevel@tonic-gate 
231*7c478bd9Sstevel@tonic-gate 	if (!keep)
232*7c478bd9Sstevel@tonic-gate 		return;
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate 	table = maken(keep);
235*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&node_lock);
236*7c478bd9Sstevel@tonic-gate 	operate_hash(nnam_hash, do_findnnams, (char *)table);
237*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&node_lock);
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate 	for (i = 1; i <= keep; i++) {
240*7c478bd9Sstevel@tonic-gate 		char *tmp;
241*7c478bd9Sstevel@tonic-gate 		u.ping.nsc_call.nsc_callnumber = GETIPNODEBYNAME;
242*7c478bd9Sstevel@tonic-gate 
243*7c478bd9Sstevel@tonic-gate 		if ((tmp = (char *)table[keep + 1 + i]) == (char *)-1)
244*7c478bd9Sstevel@tonic-gate 			continue; /* unused slot in table */
245*7c478bd9Sstevel@tonic-gate 		if (current_admin.debug_level >= DBG_ALL)
246*7c478bd9Sstevel@tonic-gate 			logit("keepalive: reviving node %s\n", tmp);
247*7c478bd9Sstevel@tonic-gate 		copy_ipnode_combined_key(tmp, &u.ping.nsc_call);
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate 		launch_update(&u.ping.nsc_call);
250*7c478bd9Sstevel@tonic-gate 		(void) sleep(interval);
251*7c478bd9Sstevel@tonic-gate 	}
252*7c478bd9Sstevel@tonic-gate 
253*7c478bd9Sstevel@tonic-gate 	for (i = 1; i <= keep; i++) {
254*7c478bd9Sstevel@tonic-gate 		char *tmp;
255*7c478bd9Sstevel@tonic-gate 		if ((tmp = (char *)table[keep + 1 + i]) != (char *)-1)
256*7c478bd9Sstevel@tonic-gate 			free(tmp);
257*7c478bd9Sstevel@tonic-gate 	}
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate 	free(table);
260*7c478bd9Sstevel@tonic-gate }
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate static void
263*7c478bd9Sstevel@tonic-gate getnode_addrkeepalive(int keep, int interval)
264*7c478bd9Sstevel@tonic-gate {
265*7c478bd9Sstevel@tonic-gate 	int *table;
266*7c478bd9Sstevel@tonic-gate 	union {
267*7c478bd9Sstevel@tonic-gate 		nsc_data_t  ping;
268*7c478bd9Sstevel@tonic-gate 		char space[sizeof (nsc_data_t) + 80];
269*7c478bd9Sstevel@tonic-gate 	} u;
270*7c478bd9Sstevel@tonic-gate 
271*7c478bd9Sstevel@tonic-gate 	int i;
272*7c478bd9Sstevel@tonic-gate 
273*7c478bd9Sstevel@tonic-gate 	if (!keep)
274*7c478bd9Sstevel@tonic-gate 		return;
275*7c478bd9Sstevel@tonic-gate 
276*7c478bd9Sstevel@tonic-gate 	table = maken(keep);
277*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&node_lock);
278*7c478bd9Sstevel@tonic-gate 	operate_hash(addr_hash, do_findnaddrs, (char *)table);
279*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&node_lock);
280*7c478bd9Sstevel@tonic-gate 
281*7c478bd9Sstevel@tonic-gate 	for (i = 1; i <= keep; i++) {
282*7c478bd9Sstevel@tonic-gate 		char *tmp;
283*7c478bd9Sstevel@tonic-gate 		char addr[IPV6_ADDR_LEN];
284*7c478bd9Sstevel@tonic-gate 
285*7c478bd9Sstevel@tonic-gate 		u.ping.nsc_call.nsc_callnumber = GETIPNODEBYADDR;
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate 		if ((tmp = (char *)table[keep + 1 + i]) == (char *)-1)
288*7c478bd9Sstevel@tonic-gate 			continue; /* enused slot in table */
289*7c478bd9Sstevel@tonic-gate 		u.ping.nsc_call.nsc_u.addr.a_type = AF_INET6;
290*7c478bd9Sstevel@tonic-gate 		u.ping.nsc_call.nsc_u.addr.a_length = IPV6_ADDR_LEN;
291*7c478bd9Sstevel@tonic-gate 		if (inet_pton(AF_INET6, (const char *) tmp, (void *) addr) !=
292*7c478bd9Sstevel@tonic-gate 		    1)
293*7c478bd9Sstevel@tonic-gate 			continue; /* illegal address - skip it */
294*7c478bd9Sstevel@tonic-gate 		else {
295*7c478bd9Sstevel@tonic-gate 			(void) memcpy(u.ping.nsc_call.nsc_u.addr.a_data,
296*7c478bd9Sstevel@tonic-gate 			    addr, IPV6_ADDR_LEN);
297*7c478bd9Sstevel@tonic-gate 			if (current_admin.debug_level >= DBG_ALL)
298*7c478bd9Sstevel@tonic-gate 				logit("keepalive: reviving address %s\n", tmp);
299*7c478bd9Sstevel@tonic-gate 			launch_update(&u.ping.nsc_call);
300*7c478bd9Sstevel@tonic-gate 		}
301*7c478bd9Sstevel@tonic-gate 		(void) sleep(interval);
302*7c478bd9Sstevel@tonic-gate 	}
303*7c478bd9Sstevel@tonic-gate 
304*7c478bd9Sstevel@tonic-gate 	for (i = 1; i <= keep; i++) {
305*7c478bd9Sstevel@tonic-gate 		char *tmp;
306*7c478bd9Sstevel@tonic-gate 		if ((tmp = (char *)table[keep + 1 + i]) != (char *)-1)
307*7c478bd9Sstevel@tonic-gate 		free(tmp);
308*7c478bd9Sstevel@tonic-gate 	}
309*7c478bd9Sstevel@tonic-gate 
310*7c478bd9Sstevel@tonic-gate 	free(table);
311*7c478bd9Sstevel@tonic-gate }
312*7c478bd9Sstevel@tonic-gate 
313*7c478bd9Sstevel@tonic-gate /*
314*7c478bd9Sstevel@tonic-gate  *   This routine marks all entries as invalid
315*7c478bd9Sstevel@tonic-gate  *
316*7c478bd9Sstevel@tonic-gate  */
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate void
319*7c478bd9Sstevel@tonic-gate getnode_invalidate(void)
320*7c478bd9Sstevel@tonic-gate {
321*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&node_lock);
322*7c478bd9Sstevel@tonic-gate 	getnode_invalidate_unlocked();
323*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&node_lock);
324*7c478bd9Sstevel@tonic-gate }
325*7c478bd9Sstevel@tonic-gate 
326*7c478bd9Sstevel@tonic-gate static void
327*7c478bd9Sstevel@tonic-gate getnode_invalidate_unlocked(void)
328*7c478bd9Sstevel@tonic-gate {
329*7c478bd9Sstevel@tonic-gate 	operate_hash_addr(nnam_hash, do_invalidate, (char *)GETIPNODEBYNAME);
330*7c478bd9Sstevel@tonic-gate 	operate_hash_addr(addr_hash, do_invalidate, (char *)GETIPNODEBYADDR);
331*7c478bd9Sstevel@tonic-gate 	current_admin.node.nsc_invalidate_count++;
332*7c478bd9Sstevel@tonic-gate }
333*7c478bd9Sstevel@tonic-gate 
334*7c478bd9Sstevel@tonic-gate /*
335*7c478bd9Sstevel@tonic-gate  * Mark only the ipnodebyname cache entries as invalid
336*7c478bd9Sstevel@tonic-gate  */
337*7c478bd9Sstevel@tonic-gate 
338*7c478bd9Sstevel@tonic-gate void
339*7c478bd9Sstevel@tonic-gate getnode_name_invalidate(void)
340*7c478bd9Sstevel@tonic-gate {
341*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&node_lock);
342*7c478bd9Sstevel@tonic-gate 	operate_hash_addr(nnam_hash, do_invalidate, (char *)GETIPNODEBYNAME);
343*7c478bd9Sstevel@tonic-gate 	current_admin.node.nsc_invalidate_count++;
344*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&node_lock);
345*7c478bd9Sstevel@tonic-gate }
346*7c478bd9Sstevel@tonic-gate 
347*7c478bd9Sstevel@tonic-gate void
348*7c478bd9Sstevel@tonic-gate getnode_lookup(nsc_return_t *out, int maxsize, nsc_call_t *in, time_t now)
349*7c478bd9Sstevel@tonic-gate {
350*7c478bd9Sstevel@tonic-gate 	int		out_of_date;
351*7c478bd9Sstevel@tonic-gate 	nsc_bucket_t	*retb;
352*7c478bd9Sstevel@tonic-gate 	char 		**bucket, *key;
353*7c478bd9Sstevel@tonic-gate 
354*7c478bd9Sstevel@tonic-gate 	static time_t	lastmod;
355*7c478bd9Sstevel@tonic-gate 
356*7c478bd9Sstevel@tonic-gate 	int bufferspace = maxsize - sizeof (nsc_return_t);
357*7c478bd9Sstevel@tonic-gate 
358*7c478bd9Sstevel@tonic-gate 	if (current_admin.node.nsc_enabled == 0) {
359*7c478bd9Sstevel@tonic-gate 		out->nsc_return_code = NOSERVER;
360*7c478bd9Sstevel@tonic-gate 		out->nsc_bufferbytesused = sizeof (*out);
361*7c478bd9Sstevel@tonic-gate 		return;
362*7c478bd9Sstevel@tonic-gate 	}
363*7c478bd9Sstevel@tonic-gate 
364*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&node_lock);
365*7c478bd9Sstevel@tonic-gate 
366*7c478bd9Sstevel@tonic-gate 	if (current_admin.node.nsc_check_files) {
367*7c478bd9Sstevel@tonic-gate 		struct stat buf;
368*7c478bd9Sstevel@tonic-gate 
369*7c478bd9Sstevel@tonic-gate 		if (stat("/etc/inet/ipnodes", &buf) < 0) {
370*7c478bd9Sstevel@tonic-gate 			/*EMPTY*/;
371*7c478bd9Sstevel@tonic-gate 		} else if (lastmod == 0) {
372*7c478bd9Sstevel@tonic-gate 			lastmod = buf.st_mtime;
373*7c478bd9Sstevel@tonic-gate 		} else if (lastmod < buf.st_mtime) {
374*7c478bd9Sstevel@tonic-gate 			getnode_invalidate_unlocked();
375*7c478bd9Sstevel@tonic-gate 			lastmod = buf.st_mtime;
376*7c478bd9Sstevel@tonic-gate 		}
377*7c478bd9Sstevel@tonic-gate 	}
378*7c478bd9Sstevel@tonic-gate 
379*7c478bd9Sstevel@tonic-gate 
380*7c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_ALL) {
381*7c478bd9Sstevel@tonic-gate 		if (MASKUPDATEBIT(in->nsc_callnumber) == GETIPNODEBYADDR) {
382*7c478bd9Sstevel@tonic-gate 			char addr[INET6_ADDRSTRLEN];
383*7c478bd9Sstevel@tonic-gate 			safe_inet_ntop(AF_INET6,
384*7c478bd9Sstevel@tonic-gate 			    (const void *)in->nsc_u.addr.a_data, addr,
385*7c478bd9Sstevel@tonic-gate 			    sizeof (addr));
386*7c478bd9Sstevel@tonic-gate 			logit("getnode_lookup: looking for address %s\n", addr);
387*7c478bd9Sstevel@tonic-gate 		} else {
388*7c478bd9Sstevel@tonic-gate 			logit("getnode_lookup: looking for nodename %s:%d:%d\n",
389*7c478bd9Sstevel@tonic-gate 				in->nsc_u.ipnode.name,
390*7c478bd9Sstevel@tonic-gate 				in->nsc_u.ipnode.af_family,
391*7c478bd9Sstevel@tonic-gate 				in->nsc_u.ipnode.flags);
392*7c478bd9Sstevel@tonic-gate 		}
393*7c478bd9Sstevel@tonic-gate 	}
394*7c478bd9Sstevel@tonic-gate 
395*7c478bd9Sstevel@tonic-gate 	for (;;) {
396*7c478bd9Sstevel@tonic-gate 		if (MASKUPDATEBIT(in->nsc_callnumber) == GETIPNODEBYADDR) {
397*7c478bd9Sstevel@tonic-gate 			char addr[INET6_ADDRSTRLEN];
398*7c478bd9Sstevel@tonic-gate 			if (inet_ntop(AF_INET6,
399*7c478bd9Sstevel@tonic-gate 			    (const void *)in->nsc_u.addr.a_data,
400*7c478bd9Sstevel@tonic-gate 			    addr, sizeof (addr)) == NULL) {
401*7c478bd9Sstevel@tonic-gate 				out->nsc_errno = NSS_NOTFOUND;
402*7c478bd9Sstevel@tonic-gate 				out->nsc_return_code = NOTFOUND;
403*7c478bd9Sstevel@tonic-gate 				out->nsc_bufferbytesused = sizeof (*out);
404*7c478bd9Sstevel@tonic-gate 				goto getout;
405*7c478bd9Sstevel@tonic-gate 			}
406*7c478bd9Sstevel@tonic-gate 			bucket = get_hash(addr_hash, addr);
407*7c478bd9Sstevel@tonic-gate 		} else { /* bounce excessively long requests */
408*7c478bd9Sstevel@tonic-gate 			if (strlen(in->nsc_u.ipnode.name) > NSCDMAXNAMELEN) {
409*7c478bd9Sstevel@tonic-gate 				ucred_t *uc = NULL;
410*7c478bd9Sstevel@tonic-gate 
411*7c478bd9Sstevel@tonic-gate 				if (door_ucred(&uc) != 0) {
412*7c478bd9Sstevel@tonic-gate 					logit("getnode_lookup: Name too long, "
413*7c478bd9Sstevel@tonic-gate 					    "but no user credential: %s\n",
414*7c478bd9Sstevel@tonic-gate 					    strerror(errno));
415*7c478bd9Sstevel@tonic-gate 				} else {
416*7c478bd9Sstevel@tonic-gate 					logit("getnode_lookup: Name too long "
417*7c478bd9Sstevel@tonic-gate 					    "from pid %d uid %d\n",
418*7c478bd9Sstevel@tonic-gate 					    ucred_getpid(uc),
419*7c478bd9Sstevel@tonic-gate 					    ucred_getruid(uc));
420*7c478bd9Sstevel@tonic-gate 					ucred_free(uc);
421*7c478bd9Sstevel@tonic-gate 				}
422*7c478bd9Sstevel@tonic-gate 
423*7c478bd9Sstevel@tonic-gate 				out->nsc_errno = NSS_NOTFOUND;
424*7c478bd9Sstevel@tonic-gate 				out->nsc_return_code = NOTFOUND;
425*7c478bd9Sstevel@tonic-gate 				out->nsc_bufferbytesused = sizeof (*out);
426*7c478bd9Sstevel@tonic-gate 				goto getout;
427*7c478bd9Sstevel@tonic-gate 			}
428*7c478bd9Sstevel@tonic-gate 			if ((key = get_ipnode_combined_key(in)) == NULL) {
429*7c478bd9Sstevel@tonic-gate 
430*7c478bd9Sstevel@tonic-gate 				out->nsc_errno = NSS_NOTFOUND;
431*7c478bd9Sstevel@tonic-gate 				out->nsc_return_code = NOTFOUND;
432*7c478bd9Sstevel@tonic-gate 				out->nsc_bufferbytesused = sizeof (*out);
433*7c478bd9Sstevel@tonic-gate 				goto getout;
434*7c478bd9Sstevel@tonic-gate 			}
435*7c478bd9Sstevel@tonic-gate 
436*7c478bd9Sstevel@tonic-gate 			bucket = get_hash(nnam_hash, key);
437*7c478bd9Sstevel@tonic-gate 			free(key);
438*7c478bd9Sstevel@tonic-gate 		}
439*7c478bd9Sstevel@tonic-gate 
440*7c478bd9Sstevel@tonic-gate 		if (*bucket == (char *)-1) {	/* pending lookup */
441*7c478bd9Sstevel@tonic-gate 			if (get_clearance(in->nsc_callnumber) != 0) {
442*7c478bd9Sstevel@tonic-gate 				/* no threads available */
443*7c478bd9Sstevel@tonic-gate 				out->nsc_return_code = NOSERVER;
444*7c478bd9Sstevel@tonic-gate 				/* cannot process now */
445*7c478bd9Sstevel@tonic-gate 				out->nsc_bufferbytesused = sizeof (*out);
446*7c478bd9Sstevel@tonic-gate 				current_admin.node.nsc_throttle_count++;
447*7c478bd9Sstevel@tonic-gate 				goto getout;
448*7c478bd9Sstevel@tonic-gate 			}
449*7c478bd9Sstevel@tonic-gate 			nscd_wait(&node_wait, &node_lock, bucket);
450*7c478bd9Sstevel@tonic-gate 			release_clearance(in->nsc_callnumber);
451*7c478bd9Sstevel@tonic-gate 			continue; /* go back and relookup hash bucket */
452*7c478bd9Sstevel@tonic-gate 		}
453*7c478bd9Sstevel@tonic-gate 		break;
454*7c478bd9Sstevel@tonic-gate 	}
455*7c478bd9Sstevel@tonic-gate 
456*7c478bd9Sstevel@tonic-gate 	/*
457*7c478bd9Sstevel@tonic-gate 	 * check for no name_service mode
458*7c478bd9Sstevel@tonic-gate 	 */
459*7c478bd9Sstevel@tonic-gate 
460*7c478bd9Sstevel@tonic-gate 	if (*bucket == NULL && current_admin.avoid_nameservice) {
461*7c478bd9Sstevel@tonic-gate 		out->nsc_return_code = NOTFOUND;
462*7c478bd9Sstevel@tonic-gate 		out->nsc_bufferbytesused = sizeof (*out);
463*7c478bd9Sstevel@tonic-gate 	} else if ((*bucket == NULL) ||	/* New entry in name service */
464*7c478bd9Sstevel@tonic-gate 	    (in->nsc_callnumber & UPDATEBIT) || /* needs updating */
465*7c478bd9Sstevel@tonic-gate 	    (out_of_date = (!current_admin.avoid_nameservice &&
466*7c478bd9Sstevel@tonic-gate 		(current_admin.node.nsc_old_data_ok == 0) &&
467*7c478bd9Sstevel@tonic-gate 		(((nsc_bucket_t *)*bucket)->nsc_timestamp < now)))) {
468*7c478bd9Sstevel@tonic-gate 		/* time has expired */
469*7c478bd9Sstevel@tonic-gate 		int saved_errno;
470*7c478bd9Sstevel@tonic-gate 		int saved_hits = 0;
471*7c478bd9Sstevel@tonic-gate 		struct hostent *p;
472*7c478bd9Sstevel@tonic-gate 
473*7c478bd9Sstevel@tonic-gate 		if (get_clearance(in->nsc_callnumber) != 0) {
474*7c478bd9Sstevel@tonic-gate 			/* no threads available */
475*7c478bd9Sstevel@tonic-gate 			out->nsc_return_code = NOSERVER;
476*7c478bd9Sstevel@tonic-gate 			/* cannot process now */
477*7c478bd9Sstevel@tonic-gate 			out->nsc_bufferbytesused = sizeof (* out);
478*7c478bd9Sstevel@tonic-gate 			current_admin.node.nsc_throttle_count++;
479*7c478bd9Sstevel@tonic-gate 			goto getout;
480*7c478bd9Sstevel@tonic-gate 		}
481*7c478bd9Sstevel@tonic-gate 
482*7c478bd9Sstevel@tonic-gate 		if (*bucket != NULL) {
483*7c478bd9Sstevel@tonic-gate 			saved_hits = ((nsc_bucket_t *)*bucket)->nsc_hits;
484*7c478bd9Sstevel@tonic-gate 		}
485*7c478bd9Sstevel@tonic-gate 
486*7c478bd9Sstevel@tonic-gate 		/*
487*7c478bd9Sstevel@tonic-gate 		 * block any threads accessing this bucket if data is
488*7c478bd9Sstevel@tonic-gate 		 * non-existent or out of date
489*7c478bd9Sstevel@tonic-gate 		 */
490*7c478bd9Sstevel@tonic-gate 
491*7c478bd9Sstevel@tonic-gate 		if (*bucket == NULL || out_of_date) {
492*7c478bd9Sstevel@tonic-gate 			update_node_bucket((nsc_bucket_t **)bucket,
493*7c478bd9Sstevel@tonic-gate 					(nsc_bucket_t *)-1,
494*7c478bd9Sstevel@tonic-gate 					in->nsc_callnumber);
495*7c478bd9Sstevel@tonic-gate 		} else {
496*7c478bd9Sstevel@tonic-gate 		/*
497*7c478bd9Sstevel@tonic-gate 		 * if still not -1 bucket we are doing update... mark to
498*7c478bd9Sstevel@tonic-gate 		 * prevent pileups of threads if the name service is hanging...
499*7c478bd9Sstevel@tonic-gate 		 */
500*7c478bd9Sstevel@tonic-gate 			((nsc_bucket_t *)(*bucket))->nsc_status |=
501*7c478bd9Sstevel@tonic-gate 				ST_UPDATE_PENDING;
502*7c478bd9Sstevel@tonic-gate 			/* cleared by deletion of old data */
503*7c478bd9Sstevel@tonic-gate 		}
504*7c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&node_lock);
505*7c478bd9Sstevel@tonic-gate 
506*7c478bd9Sstevel@tonic-gate 		if (MASKUPDATEBIT(in->nsc_callnumber) == GETIPNODEBYADDR) {
507*7c478bd9Sstevel@tonic-gate 			p = _uncached_getipnodebyaddr(in->nsc_u.addr.a_data,
508*7c478bd9Sstevel@tonic-gate 					in->nsc_u.addr.a_length,
509*7c478bd9Sstevel@tonic-gate 					in->nsc_u.addr.a_type,
510*7c478bd9Sstevel@tonic-gate 					&out->nsc_u.hst,
511*7c478bd9Sstevel@tonic-gate 					out->nsc_u.buff+sizeof (struct hostent),
512*7c478bd9Sstevel@tonic-gate 					bufferspace,
513*7c478bd9Sstevel@tonic-gate 					&saved_errno);
514*7c478bd9Sstevel@tonic-gate 		} else {
515*7c478bd9Sstevel@tonic-gate 			p = _uncached_getipnodebyname(in->nsc_u.ipnode.name,
516*7c478bd9Sstevel@tonic-gate 					&out->nsc_u.hst,
517*7c478bd9Sstevel@tonic-gate 					out->nsc_u.buff+sizeof (struct hostent),
518*7c478bd9Sstevel@tonic-gate 					bufferspace,
519*7c478bd9Sstevel@tonic-gate 					in->nsc_u.ipnode.af_family,
520*7c478bd9Sstevel@tonic-gate 					in->nsc_u.ipnode.flags,
521*7c478bd9Sstevel@tonic-gate 					&saved_errno);
522*7c478bd9Sstevel@tonic-gate 		}
523*7c478bd9Sstevel@tonic-gate 
524*7c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&node_lock);
525*7c478bd9Sstevel@tonic-gate 
526*7c478bd9Sstevel@tonic-gate 		release_clearance(in->nsc_callnumber);
527*7c478bd9Sstevel@tonic-gate 
528*7c478bd9Sstevel@tonic-gate 		if (p == NULL) { /* data not found */
529*7c478bd9Sstevel@tonic-gate 			if (current_admin.debug_level >= DBG_CANT_FIND) {
530*7c478bd9Sstevel@tonic-gate 				if (MASKUPDATEBIT(in->nsc_callnumber) ==
531*7c478bd9Sstevel@tonic-gate 				    GETIPNODEBYADDR) {
532*7c478bd9Sstevel@tonic-gate 					char addr[INET6_ADDRSTRLEN];
533*7c478bd9Sstevel@tonic-gate 					safe_inet_ntop(AF_INET6,
534*7c478bd9Sstevel@tonic-gate 					(const void *)in->nsc_u.addr.a_data,
535*7c478bd9Sstevel@tonic-gate 					addr, sizeof (addr));
536*7c478bd9Sstevel@tonic-gate 					logit("getnode_lookup: nscd COULDN'T "\
537*7c478bd9Sstevel@tonic-gate 						"FIND address %s\n", addr);
538*7c478bd9Sstevel@tonic-gate 				} else {
539*7c478bd9Sstevel@tonic-gate 					logit("getnode_lookup: nscd COULDN'T "\
540*7c478bd9Sstevel@tonic-gate 						"FIND node name %s:%d:%d\n",
541*7c478bd9Sstevel@tonic-gate 						in->nsc_u.ipnode.name,
542*7c478bd9Sstevel@tonic-gate 						in->nsc_u.ipnode.af_family,
543*7c478bd9Sstevel@tonic-gate 						in->nsc_u.ipnode.flags);
544*7c478bd9Sstevel@tonic-gate 				}
545*7c478bd9Sstevel@tonic-gate 			}
546*7c478bd9Sstevel@tonic-gate 
547*7c478bd9Sstevel@tonic-gate 			if (!(UPDATEBIT & in->nsc_callnumber))
548*7c478bd9Sstevel@tonic-gate 				current_admin.node.nsc_neg_cache_misses++;
549*7c478bd9Sstevel@tonic-gate 
550*7c478bd9Sstevel@tonic-gate 			retb = (nsc_bucket_t *)malloc(sizeof (nsc_bucket_t));
551*7c478bd9Sstevel@tonic-gate 			retb->nsc_refcount = 1;
552*7c478bd9Sstevel@tonic-gate 			retb->nsc_data.nsc_return_code = NOTFOUND;
553*7c478bd9Sstevel@tonic-gate 			retb->nsc_data.nsc_bufferbytesused =
554*7c478bd9Sstevel@tonic-gate 				sizeof (nsc_return_t);
555*7c478bd9Sstevel@tonic-gate 			retb->nsc_data.nsc_errno = saved_errno;
556*7c478bd9Sstevel@tonic-gate 			(void) memcpy(out, &(retb->nsc_data),
557*7c478bd9Sstevel@tonic-gate 				retb->nsc_data.nsc_bufferbytesused);
558*7c478bd9Sstevel@tonic-gate 			update_node_bucket((nsc_bucket_t **)bucket, retb,
559*7c478bd9Sstevel@tonic-gate 				in->nsc_callnumber);
560*7c478bd9Sstevel@tonic-gate 			goto getout;
561*7c478bd9Sstevel@tonic-gate 		}
562*7c478bd9Sstevel@tonic-gate 
563*7c478bd9Sstevel@tonic-gate 		else {
564*7c478bd9Sstevel@tonic-gate 			if (current_admin.debug_level >= DBG_ALL) {
565*7c478bd9Sstevel@tonic-gate 				if (MASKUPDATEBIT(in->nsc_callnumber) ==
566*7c478bd9Sstevel@tonic-gate 				    GETIPNODEBYADDR) {
567*7c478bd9Sstevel@tonic-gate 					char addr[INET6_ADDRSTRLEN];
568*7c478bd9Sstevel@tonic-gate 					safe_inet_ntop(AF_INET6,
569*7c478bd9Sstevel@tonic-gate 					(const void *)in->nsc_u.addr.a_data,
570*7c478bd9Sstevel@tonic-gate 					addr, sizeof (addr));
571*7c478bd9Sstevel@tonic-gate 					logit("getnode_lookup: nscd FOUND "\
572*7c478bd9Sstevel@tonic-gate 					    "addr %s\n", addr);
573*7c478bd9Sstevel@tonic-gate 				} else {
574*7c478bd9Sstevel@tonic-gate 					logit("getnode_lookup: nscd FOUND "\
575*7c478bd9Sstevel@tonic-gate 					    "node name %s:%d:%d\n",
576*7c478bd9Sstevel@tonic-gate 					    in->nsc_u.ipnode.name,
577*7c478bd9Sstevel@tonic-gate 					    in->nsc_u.ipnode.af_family,
578*7c478bd9Sstevel@tonic-gate 					    in->nsc_u.ipnode.flags);
579*7c478bd9Sstevel@tonic-gate 				}
580*7c478bd9Sstevel@tonic-gate 			}
581*7c478bd9Sstevel@tonic-gate 			if (!(UPDATEBIT & in->nsc_callnumber))
582*7c478bd9Sstevel@tonic-gate 			    current_admin.node.nsc_pos_cache_misses++;
583*7c478bd9Sstevel@tonic-gate 
584*7c478bd9Sstevel@tonic-gate 			retb = fixbuffer(out, bufferspace);
585*7c478bd9Sstevel@tonic-gate 
586*7c478bd9Sstevel@tonic-gate 			update_node_bucket((nsc_bucket_t **)bucket, retb,
587*7c478bd9Sstevel@tonic-gate 				in->nsc_callnumber);
588*7c478bd9Sstevel@tonic-gate 			if (saved_hits)
589*7c478bd9Sstevel@tonic-gate 				retb->nsc_hits = saved_hits;
590*7c478bd9Sstevel@tonic-gate 		}
591*7c478bd9Sstevel@tonic-gate 	} else { 	/* found entry in cache */
592*7c478bd9Sstevel@tonic-gate 		retb = (nsc_bucket_t *)*bucket;
593*7c478bd9Sstevel@tonic-gate 
594*7c478bd9Sstevel@tonic-gate 		retb->nsc_hits++;
595*7c478bd9Sstevel@tonic-gate 
596*7c478bd9Sstevel@tonic-gate 		(void) memcpy(out, &(retb->nsc_data),
597*7c478bd9Sstevel@tonic-gate 			retb->nsc_data.nsc_bufferbytesused);
598*7c478bd9Sstevel@tonic-gate 
599*7c478bd9Sstevel@tonic-gate 		if (out->nsc_return_code == SUCCESS) {
600*7c478bd9Sstevel@tonic-gate 			if (!(UPDATEBIT & in->nsc_callnumber))
601*7c478bd9Sstevel@tonic-gate 			    current_admin.node.nsc_pos_cache_hits++;
602*7c478bd9Sstevel@tonic-gate 			if (current_admin.debug_level >= DBG_ALL) {
603*7c478bd9Sstevel@tonic-gate 				if (MASKUPDATEBIT(in->nsc_callnumber) ==
604*7c478bd9Sstevel@tonic-gate 				    GETIPNODEBYADDR) {
605*7c478bd9Sstevel@tonic-gate 					char addr[INET6_ADDRSTRLEN];
606*7c478bd9Sstevel@tonic-gate 					safe_inet_ntop(AF_INET6,
607*7c478bd9Sstevel@tonic-gate 					    (const void *)in->nsc_u.addr.a_data,
608*7c478bd9Sstevel@tonic-gate 					    addr, sizeof (addr));
609*7c478bd9Sstevel@tonic-gate 					logit("getnode_lookup: found address "\
610*7c478bd9Sstevel@tonic-gate 					    "%s in cache\n", addr);
611*7c478bd9Sstevel@tonic-gate 				} else {
612*7c478bd9Sstevel@tonic-gate 					logit("getnode_lookup: found node "\
613*7c478bd9Sstevel@tonic-gate 					    "name %s:%d:%d in cache\n",
614*7c478bd9Sstevel@tonic-gate 					    in->nsc_u.ipnode.name,
615*7c478bd9Sstevel@tonic-gate 					    in->nsc_u.ipnode.af_family,
616*7c478bd9Sstevel@tonic-gate 					    in->nsc_u.ipnode.flags);
617*7c478bd9Sstevel@tonic-gate 				}
618*7c478bd9Sstevel@tonic-gate 			}
619*7c478bd9Sstevel@tonic-gate 		} else {
620*7c478bd9Sstevel@tonic-gate 			if (!(UPDATEBIT & in->nsc_callnumber))
621*7c478bd9Sstevel@tonic-gate 			    current_admin.node.nsc_neg_cache_hits++;
622*7c478bd9Sstevel@tonic-gate 			if (current_admin.debug_level >= DBG_ALL) {
623*7c478bd9Sstevel@tonic-gate 				if (MASKUPDATEBIT(in->nsc_callnumber) ==
624*7c478bd9Sstevel@tonic-gate 				    GETIPNODEBYADDR) {
625*7c478bd9Sstevel@tonic-gate 					char addr[INET6_ADDRSTRLEN];
626*7c478bd9Sstevel@tonic-gate 					safe_inet_ntop(AF_INET6,
627*7c478bd9Sstevel@tonic-gate 					    (const void *)in->nsc_u.addr.a_data,
628*7c478bd9Sstevel@tonic-gate 					    addr, sizeof (addr));
629*7c478bd9Sstevel@tonic-gate 					logit("getnode_lookup: %s marked as "\
630*7c478bd9Sstevel@tonic-gate 					    "NOT FOUND in cache.\n", addr);
631*7c478bd9Sstevel@tonic-gate 				} else {
632*7c478bd9Sstevel@tonic-gate 					logit("getnode_lookup: %s:%d:%d marked"\
633*7c478bd9Sstevel@tonic-gate 					    " as NOT FOUND in cache.\n",
634*7c478bd9Sstevel@tonic-gate 					    in->nsc_u.ipnode.name,
635*7c478bd9Sstevel@tonic-gate 					    in->nsc_u.ipnode.af_family,
636*7c478bd9Sstevel@tonic-gate 					    in->nsc_u.ipnode.flags);
637*7c478bd9Sstevel@tonic-gate 				}
638*7c478bd9Sstevel@tonic-gate 			}
639*7c478bd9Sstevel@tonic-gate 		}
640*7c478bd9Sstevel@tonic-gate 
641*7c478bd9Sstevel@tonic-gate 		if ((retb->nsc_timestamp < now) &&
642*7c478bd9Sstevel@tonic-gate 		    !(in->nsc_callnumber & UPDATEBIT) &&
643*7c478bd9Sstevel@tonic-gate 		    !(retb->nsc_status & ST_UPDATE_PENDING)) {
644*7c478bd9Sstevel@tonic-gate 			logit("launch update since time = %d\n",
645*7c478bd9Sstevel@tonic-gate 			    retb->nsc_timestamp);
646*7c478bd9Sstevel@tonic-gate 			retb->nsc_status |= ST_UPDATE_PENDING;
647*7c478bd9Sstevel@tonic-gate 			/* cleared by deletion of old data */
648*7c478bd9Sstevel@tonic-gate 			launch_update(in);
649*7c478bd9Sstevel@tonic-gate 		}
650*7c478bd9Sstevel@tonic-gate 	}
651*7c478bd9Sstevel@tonic-gate 
652*7c478bd9Sstevel@tonic-gate getout:
653*7c478bd9Sstevel@tonic-gate 
654*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&node_lock);
655*7c478bd9Sstevel@tonic-gate }
656*7c478bd9Sstevel@tonic-gate 
657*7c478bd9Sstevel@tonic-gate 
658*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
659*7c478bd9Sstevel@tonic-gate static void
660*7c478bd9Sstevel@tonic-gate update_node_bucket(nsc_bucket_t ** old, nsc_bucket_t *new, int callnumber)
661*7c478bd9Sstevel@tonic-gate {
662*7c478bd9Sstevel@tonic-gate 	if (*old != NULL && *old != (nsc_bucket_t *)-1) { /* old data exists */
663*7c478bd9Sstevel@tonic-gate 		free(*old);
664*7c478bd9Sstevel@tonic-gate 		current_admin.node.nsc_entries--;
665*7c478bd9Sstevel@tonic-gate 	}
666*7c478bd9Sstevel@tonic-gate 
667*7c478bd9Sstevel@tonic-gate 	/*
668*7c478bd9Sstevel@tonic-gate 	 *  we can do this before reseting *old since we're holding the lock
669*7c478bd9Sstevel@tonic-gate 	 */
670*7c478bd9Sstevel@tonic-gate 
671*7c478bd9Sstevel@tonic-gate 	else if (*old == (nsc_bucket_t *)-1) {
672*7c478bd9Sstevel@tonic-gate 		nscd_signal(&node_wait, (char **)old);
673*7c478bd9Sstevel@tonic-gate 	}
674*7c478bd9Sstevel@tonic-gate 
675*7c478bd9Sstevel@tonic-gate 	*old = new;
676*7c478bd9Sstevel@tonic-gate 
677*7c478bd9Sstevel@tonic-gate 	if ((new != NULL) &&
678*7c478bd9Sstevel@tonic-gate 	    (new != (nsc_bucket_t *)-1)) {
679*7c478bd9Sstevel@tonic-gate 		/* real data, not just update pending or invalidate */
680*7c478bd9Sstevel@tonic-gate 
681*7c478bd9Sstevel@tonic-gate 		new->nsc_hits = 1;
682*7c478bd9Sstevel@tonic-gate 		new->nsc_status = 0;
683*7c478bd9Sstevel@tonic-gate 		new->nsc_refcount = 1;
684*7c478bd9Sstevel@tonic-gate 		current_admin.node.nsc_entries++;
685*7c478bd9Sstevel@tonic-gate 
686*7c478bd9Sstevel@tonic-gate 		if (new->nsc_data.nsc_return_code == SUCCESS) {
687*7c478bd9Sstevel@tonic-gate 			new->nsc_timestamp = time(NULL) +
688*7c478bd9Sstevel@tonic-gate 				current_admin.node.nsc_pos_ttl;
689*7c478bd9Sstevel@tonic-gate 		} else {
690*7c478bd9Sstevel@tonic-gate 			new->nsc_timestamp = time(NULL) +
691*7c478bd9Sstevel@tonic-gate 				current_admin.node.nsc_neg_ttl;
692*7c478bd9Sstevel@tonic-gate 		}
693*7c478bd9Sstevel@tonic-gate 	}
694*7c478bd9Sstevel@tonic-gate }
695*7c478bd9Sstevel@tonic-gate 
696*7c478bd9Sstevel@tonic-gate /* Allocate a bucket to fit the data(nsc_return_t *in size) */
697*7c478bd9Sstevel@tonic-gate /* copy the data into the bucket and return the bucket */
698*7c478bd9Sstevel@tonic-gate 
699*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
700*7c478bd9Sstevel@tonic-gate static nsc_bucket_t *
701*7c478bd9Sstevel@tonic-gate fixbuffer(nsc_return_t *in, int maxlen)
702*7c478bd9Sstevel@tonic-gate {
703*7c478bd9Sstevel@tonic-gate 	nsc_return_t *out;
704*7c478bd9Sstevel@tonic-gate 	nsc_bucket_t *retb;
705*7c478bd9Sstevel@tonic-gate 	char *dest;
706*7c478bd9Sstevel@tonic-gate 	char ** aliaseslist;
707*7c478bd9Sstevel@tonic-gate 	char ** addrlist;
708*7c478bd9Sstevel@tonic-gate 	int offset;
709*7c478bd9Sstevel@tonic-gate 	int strs;
710*7c478bd9Sstevel@tonic-gate 	int i;
711*7c478bd9Sstevel@tonic-gate 	int numaliases;
712*7c478bd9Sstevel@tonic-gate 	int numaddrs;
713*7c478bd9Sstevel@tonic-gate 
714*7c478bd9Sstevel@tonic-gate 	/* find out the size of the data block we're going to need */
715*7c478bd9Sstevel@tonic-gate 
716*7c478bd9Sstevel@tonic-gate 	strs = 1 + strlen(in->nsc_u.hst.h_name);
717*7c478bd9Sstevel@tonic-gate 	for (numaliases = 0; in->nsc_u.hst.h_aliases[numaliases]; numaliases++)
718*7c478bd9Sstevel@tonic-gate 		strs += 1 + strlen(in->nsc_u.hst.h_aliases[numaliases]);
719*7c478bd9Sstevel@tonic-gate 	strs += sizeof (char *) * (numaliases+1);
720*7c478bd9Sstevel@tonic-gate 	for (numaddrs = 0; in->nsc_u.hst.h_addr_list[numaddrs]; numaddrs++)
721*7c478bd9Sstevel@tonic-gate 		strs += in->nsc_u.hst.h_length;
722*7c478bd9Sstevel@tonic-gate 	strs += sizeof (char *) * (numaddrs+1+3);
723*7c478bd9Sstevel@tonic-gate 
724*7c478bd9Sstevel@tonic-gate 	/* allocate it and copy it in code doesn't assume packing */
725*7c478bd9Sstevel@tonic-gate 	/* order in original buffer */
726*7c478bd9Sstevel@tonic-gate 
727*7c478bd9Sstevel@tonic-gate 	if ((retb = (nsc_bucket_t *)malloc(sizeof (*retb) + strs)) == NULL) {
728*7c478bd9Sstevel@tonic-gate 		return (NULL);
729*7c478bd9Sstevel@tonic-gate 	}
730*7c478bd9Sstevel@tonic-gate 
731*7c478bd9Sstevel@tonic-gate 	out = &(retb->nsc_data);
732*7c478bd9Sstevel@tonic-gate 	out->nsc_bufferbytesused = sizeof (*in) + strs;
733*7c478bd9Sstevel@tonic-gate 	out->nsc_return_code 	= SUCCESS;
734*7c478bd9Sstevel@tonic-gate 	out->nsc_errno 		= 0;
735*7c478bd9Sstevel@tonic-gate 
736*7c478bd9Sstevel@tonic-gate 	dest = retb->nsc_data.nsc_u.buff + sizeof (struct hostent);
737*7c478bd9Sstevel@tonic-gate 	offset = (int)dest;
738*7c478bd9Sstevel@tonic-gate 
739*7c478bd9Sstevel@tonic-gate 	/* allocat the h_aliases list and the h_addr_list first to align 'em. */
740*7c478bd9Sstevel@tonic-gate 	aliaseslist = (char **)dest;
741*7c478bd9Sstevel@tonic-gate 
742*7c478bd9Sstevel@tonic-gate 	dest += sizeof (char *) * (numaliases+1);
743*7c478bd9Sstevel@tonic-gate 
744*7c478bd9Sstevel@tonic-gate 	addrlist = (char **)dest;
745*7c478bd9Sstevel@tonic-gate 
746*7c478bd9Sstevel@tonic-gate 	dest += sizeof (char *) * (numaddrs+1);
747*7c478bd9Sstevel@tonic-gate 
748*7c478bd9Sstevel@tonic-gate 	(void) strcpy(dest, in->nsc_u.hst.h_name);
749*7c478bd9Sstevel@tonic-gate 	strs = 1 + strlen(in->nsc_u.hst.h_name);
750*7c478bd9Sstevel@tonic-gate 	out->nsc_u.hst.h_name = dest - offset;
751*7c478bd9Sstevel@tonic-gate 	dest += strs;
752*7c478bd9Sstevel@tonic-gate 
753*7c478bd9Sstevel@tonic-gate 
754*7c478bd9Sstevel@tonic-gate 	/* fill out the h_aliases list */
755*7c478bd9Sstevel@tonic-gate 
756*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < numaliases; i++) {
757*7c478bd9Sstevel@tonic-gate 		(void) strcpy(dest, in->nsc_u.hst.h_aliases[i]);
758*7c478bd9Sstevel@tonic-gate 		strs = 1 + strlen(in->nsc_u.hst.h_aliases[i]);
759*7c478bd9Sstevel@tonic-gate 		aliaseslist[i] = dest - offset;
760*7c478bd9Sstevel@tonic-gate 		dest += strs;
761*7c478bd9Sstevel@tonic-gate 	}
762*7c478bd9Sstevel@tonic-gate 	aliaseslist[i] = 0;	/* null term ptr chain */
763*7c478bd9Sstevel@tonic-gate 
764*7c478bd9Sstevel@tonic-gate 	out->nsc_u.hst.h_aliases = (char **)((int)aliaseslist-offset);
765*7c478bd9Sstevel@tonic-gate 
766*7c478bd9Sstevel@tonic-gate 	/* fill out the h_addr list */
767*7c478bd9Sstevel@tonic-gate 
768*7c478bd9Sstevel@tonic-gate 	dest = (char *)(((int)dest + 3) & ~3);
769*7c478bd9Sstevel@tonic-gate 
770*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < numaddrs; i++) {
771*7c478bd9Sstevel@tonic-gate 		(void) memcpy(dest, in->nsc_u.hst.h_addr_list[i],
772*7c478bd9Sstevel@tonic-gate 			in->nsc_u.hst.h_length);
773*7c478bd9Sstevel@tonic-gate 		strs = in->nsc_u.hst.h_length;
774*7c478bd9Sstevel@tonic-gate 		addrlist[i] = dest - offset;
775*7c478bd9Sstevel@tonic-gate 		dest += strs;
776*7c478bd9Sstevel@tonic-gate 		dest = (char *)(((int)dest + 3) & ~3);
777*7c478bd9Sstevel@tonic-gate 	}
778*7c478bd9Sstevel@tonic-gate 
779*7c478bd9Sstevel@tonic-gate 	addrlist[i] = 0;	/* null term ptr chain */
780*7c478bd9Sstevel@tonic-gate 
781*7c478bd9Sstevel@tonic-gate 	out->nsc_u.hst.h_addr_list = (char **)((int)addrlist-offset);
782*7c478bd9Sstevel@tonic-gate 
783*7c478bd9Sstevel@tonic-gate 	out->nsc_u.hst.h_length = in->nsc_u.hst.h_length;
784*7c478bd9Sstevel@tonic-gate 	out->nsc_u.hst.h_addrtype = in->nsc_u.hst.h_addrtype;
785*7c478bd9Sstevel@tonic-gate 
786*7c478bd9Sstevel@tonic-gate 	(void) memcpy(in, &(retb->nsc_data),
787*7c478bd9Sstevel@tonic-gate 	    retb->nsc_data.nsc_bufferbytesused);
788*7c478bd9Sstevel@tonic-gate 
789*7c478bd9Sstevel@tonic-gate 	return (retb);
790*7c478bd9Sstevel@tonic-gate 
791*7c478bd9Sstevel@tonic-gate }
792*7c478bd9Sstevel@tonic-gate 
793*7c478bd9Sstevel@tonic-gate void
794*7c478bd9Sstevel@tonic-gate getnode_nam_reaper()
795*7c478bd9Sstevel@tonic-gate {
796*7c478bd9Sstevel@tonic-gate 	nsc_reaper("getnode_nam", nnam_hash, &current_admin.node, &node_lock);
797*7c478bd9Sstevel@tonic-gate }
798*7c478bd9Sstevel@tonic-gate 
799*7c478bd9Sstevel@tonic-gate void
800*7c478bd9Sstevel@tonic-gate getnode_addr_reaper()
801*7c478bd9Sstevel@tonic-gate {
802*7c478bd9Sstevel@tonic-gate 	nsc_reaper("getnode_addr", addr_hash, &current_admin.node, &node_lock);
803*7c478bd9Sstevel@tonic-gate }
804