xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.lib/in.dhcpd/misc.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <stdio.h>
30*0Sstevel@tonic-gate #include <string.h>
31*0Sstevel@tonic-gate #include <stdarg.h>
32*0Sstevel@tonic-gate #include <syslog.h>
33*0Sstevel@tonic-gate #include <errno.h>
34*0Sstevel@tonic-gate #include <sys/types.h>
35*0Sstevel@tonic-gate #include <sys/sysmacros.h>
36*0Sstevel@tonic-gate #include <time.h>
37*0Sstevel@tonic-gate #include <locale.h>
38*0Sstevel@tonic-gate #include <stdlib.h>
39*0Sstevel@tonic-gate #include <unistd.h>
40*0Sstevel@tonic-gate #include <errno.h>
41*0Sstevel@tonic-gate #include <assert.h>
42*0Sstevel@tonic-gate #include <sys/socket.h>
43*0Sstevel@tonic-gate #include <net/if.h>
44*0Sstevel@tonic-gate #include <netinet/in.h>
45*0Sstevel@tonic-gate #include <netinet/if_ether.h>
46*0Sstevel@tonic-gate #include <arpa/inet.h>
47*0Sstevel@tonic-gate #include <netinet/dhcp.h>
48*0Sstevel@tonic-gate #include <netdb.h>
49*0Sstevel@tonic-gate #include <sys/mman.h>
50*0Sstevel@tonic-gate #include <locale.h>
51*0Sstevel@tonic-gate #include <tnf/probe.h>
52*0Sstevel@tonic-gate #include <resolv.h>
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate #include "dhcpd.h"
55*0Sstevel@tonic-gate #include "per_dnet.h"
56*0Sstevel@tonic-gate #include "interfaces.h"
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate #ifdef	DEBUG
59*0Sstevel@tonic-gate /*
60*0Sstevel@tonic-gate  * Datastore debugging functions.
61*0Sstevel@tonic-gate  *
62*0Sstevel@tonic-gate  * A simple datastore simulation, using the magic DBG_MEMORY_NET network,
63*0Sstevel@tonic-gate  * is provided, to test the program with minimal datastore overhead.
64*0Sstevel@tonic-gate  * A concatenated reclist is used to speed record manipulation.
65*0Sstevel@tonic-gate  * Note that other networks continue to pass-thru to libdhcpsvc, to allow
66*0Sstevel@tonic-gate  * live comparison.
67*0Sstevel@tonic-gate  */
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate /* Simple datastore database. */
70*0Sstevel@tonic-gate typedef struct db {
71*0Sstevel@tonic-gate 	lease_t dn_lease;
72*0Sstevel@tonic-gate 	char	dn_cid[128];
73*0Sstevel@tonic-gate 	char	dn_macro[2];
74*0Sstevel@tonic-gate 	uchar_t	dn_cid_len;
75*0Sstevel@tonic-gate 	uchar_t	dn_flags;
76*0Sstevel@tonic-gate } dbg_t;
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate typedef struct reclist {
79*0Sstevel@tonic-gate 	dn_rec_list_t d_reclist;
80*0Sstevel@tonic-gate 	dn_rec_t d_rec;
81*0Sstevel@tonic-gate } dbg_rec_t;
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate #define		DBG_MAXTABLE	16
84*0Sstevel@tonic-gate static dsvc_handle_t dbg_handle[DBG_MAXTABLE];	/* simulated handle */
85*0Sstevel@tonic-gate static rwlock_t	dbg_lock[DBG_MAXTABLE];		/* locks */
86*0Sstevel@tonic-gate static uint32_t	dbg_size = 4096;		/* table size */
87*0Sstevel@tonic-gate static uint32_t	dbg_msize;			/* mapped size */
88*0Sstevel@tonic-gate static uint32_t	dbg_mask = 0xFFFF;		/* table mask */
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate static uint32_t	dbg_cid = 0;			/* starting cid */
91*0Sstevel@tonic-gate static uint32_t	dbg_flags = 0;			/* starting flags */
92*0Sstevel@tonic-gate static uint32_t	dbg_lease = 0;			/* starting lease */
93*0Sstevel@tonic-gate static char	dbg_macro = '1';		/* macro */
94*0Sstevel@tonic-gate #endif	/* DEBUG */
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate int
dhcp_open_dd(dsvc_handle_t * handp,dsvc_datastore_t * ddp,dsvc_contype_t type,const char * name,uint_t flags)97*0Sstevel@tonic-gate dhcp_open_dd(dsvc_handle_t *handp, dsvc_datastore_t *ddp, dsvc_contype_t type,
98*0Sstevel@tonic-gate     const char *name, uint_t flags)
99*0Sstevel@tonic-gate {
100*0Sstevel@tonic-gate #ifndef	DEBUG
101*0Sstevel@tonic-gate 	return (open_dd(handp, ddp, type, name, flags));
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate #else	/* DEBUG */
104*0Sstevel@tonic-gate 	int ret;
105*0Sstevel@tonic-gate 	int hind;
106*0Sstevel@tonic-gate 	int net;
107*0Sstevel@tonic-gate 	int pgmsk = sysconf(_SC_PAGESIZE) - 1;
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	if (dbg_net && memcmp(name, dbg_net, strlen(dbg_net)) == 0) {
110*0Sstevel@tonic-gate 		for (net = 0, hind = strlen(dbg_net); name[hind] != '.'; hind++)
111*0Sstevel@tonic-gate 			net += (net * 10) + (name[hind] - '0');
112*0Sstevel@tonic-gate 		if (net > DBG_MAXTABLE)
113*0Sstevel@tonic-gate 			return (DSVC_NO_TABLE);
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 		if (dbg_handle[net] == NULL) {
116*0Sstevel@tonic-gate 			dbg_msize = (sizeof (dbg_t) * dbg_size + pgmsk) &
117*0Sstevel@tonic-gate 			    ~pgmsk;
118*0Sstevel@tonic-gate 			/* LINTED [alignment ok] */
119*0Sstevel@tonic-gate 			dbg_handle[net] = (dsvc_handle_t)mmap(0, dbg_msize,
120*0Sstevel@tonic-gate 			    PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
121*0Sstevel@tonic-gate 			if ((char *)dbg_handle[net] == MAP_FAILED) {
122*0Sstevel@tonic-gate 				dbg_handle[net] = NULL;
123*0Sstevel@tonic-gate 				return (DSVC_INVAL);
124*0Sstevel@tonic-gate 			}
125*0Sstevel@tonic-gate 		}
126*0Sstevel@tonic-gate 		*handp = (void *)net;
127*0Sstevel@tonic-gate 		ret = DSVC_SUCCESS;
128*0Sstevel@tonic-gate 	} else
129*0Sstevel@tonic-gate 		ret = open_dd(handp, ddp, type, name, flags);
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 	return (ret);
132*0Sstevel@tonic-gate #endif	/* DEBUG */
133*0Sstevel@tonic-gate }
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate int
dhcp_close_dd(dsvc_handle_t * handp)136*0Sstevel@tonic-gate dhcp_close_dd(dsvc_handle_t *handp)
137*0Sstevel@tonic-gate {
138*0Sstevel@tonic-gate #ifndef	DEBUG
139*0Sstevel@tonic-gate 	return (close_dd(handp));
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate #else	/* DEBUG */
142*0Sstevel@tonic-gate 	int ret;
143*0Sstevel@tonic-gate 	int hind = (int)*handp;
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 	if (dbg_net && hind >= 0 && hind < DBG_MAXTABLE) {
146*0Sstevel@tonic-gate 		if (dbg_handle[hind] != NULL) {
147*0Sstevel@tonic-gate 			(void) munmap((char *)dbg_handle[hind], dbg_msize);
148*0Sstevel@tonic-gate 			dbg_handle[hind] = (dsvc_handle_t)NULL;
149*0Sstevel@tonic-gate 			ret = DSVC_SUCCESS;
150*0Sstevel@tonic-gate 		}
151*0Sstevel@tonic-gate 	} else
152*0Sstevel@tonic-gate 		ret = close_dd(handp);
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 	return (ret);
155*0Sstevel@tonic-gate #endif	/* DEBUG */
156*0Sstevel@tonic-gate }
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate /*
159*0Sstevel@tonic-gate  * Detach the element from a list, and return it. If the list is empty, NULL is
160*0Sstevel@tonic-gate  * returned.
161*0Sstevel@tonic-gate  */
162*0Sstevel@tonic-gate dn_rec_list_t *
detach_dnrec_from_list(dn_rec_list_t * prevp,dn_rec_list_t * elemp,dn_rec_list_t ** listpp)163*0Sstevel@tonic-gate detach_dnrec_from_list(dn_rec_list_t *prevp, dn_rec_list_t *elemp,
164*0Sstevel@tonic-gate     dn_rec_list_t **listpp)
165*0Sstevel@tonic-gate {
166*0Sstevel@tonic-gate 	if (prevp == NULL) {
167*0Sstevel@tonic-gate 		if (elemp == *listpp && elemp != NULL) {
168*0Sstevel@tonic-gate 			/* head of the list */
169*0Sstevel@tonic-gate 			*listpp = (*listpp)->dnl_next;
170*0Sstevel@tonic-gate 			elemp->dnl_next = NULL;
171*0Sstevel@tonic-gate 		}
172*0Sstevel@tonic-gate 	} else if (prevp->dnl_next != NULL) {
173*0Sstevel@tonic-gate 		/* somewhere in the middle */
174*0Sstevel@tonic-gate 		prevp->dnl_next = elemp->dnl_next;
175*0Sstevel@tonic-gate 		elemp->dnl_next = NULL;
176*0Sstevel@tonic-gate 	} else
177*0Sstevel@tonic-gate 		assert(elemp == NULL);
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate 	return (elemp);
180*0Sstevel@tonic-gate }
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate /*
183*0Sstevel@tonic-gate  * Attach an unattached element (elemp) to a list (*listpp).
184*0Sstevel@tonic-gate  */
185*0Sstevel@tonic-gate static void
attach_dnrec_to_list(dn_rec_list_t * elemp,dn_rec_list_t ** listpp)186*0Sstevel@tonic-gate attach_dnrec_to_list(dn_rec_list_t *elemp, dn_rec_list_t **listpp)
187*0Sstevel@tonic-gate {
188*0Sstevel@tonic-gate 	if (*listpp != NULL)
189*0Sstevel@tonic-gate 		elemp->dnl_next = *listpp;
190*0Sstevel@tonic-gate 	*listpp = elemp;
191*0Sstevel@tonic-gate }
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate /*
194*0Sstevel@tonic-gate  * dhcp_lookup_dd: perform lookup_dd.
195*0Sstevel@tonic-gate  */
196*0Sstevel@tonic-gate static int
dhcp_lookup_dd(dsvc_handle_t hand,boolean_t partial,uint_t query,int count,const void * targetp,void ** recordsp,uint_t * nrecordsp)197*0Sstevel@tonic-gate dhcp_lookup_dd(dsvc_handle_t hand, boolean_t partial, uint_t query,
198*0Sstevel@tonic-gate     int count, const void *targetp, void **recordsp, uint_t *nrecordsp)
199*0Sstevel@tonic-gate {
200*0Sstevel@tonic-gate #ifndef	DEBUG
201*0Sstevel@tonic-gate 	return (lookup_dd(hand, partial, query, count, targetp, recordsp,
202*0Sstevel@tonic-gate 	    nrecordsp));
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate #else	/* DEBUG */
205*0Sstevel@tonic-gate 	int		ret;
206*0Sstevel@tonic-gate 	int 		hind = (int)hand;
207*0Sstevel@tonic-gate 	int 		ind;
208*0Sstevel@tonic-gate 	dn_rec_t	*inp;
209*0Sstevel@tonic-gate 	dn_rec_list_t	**outp = (dn_rec_list_t **)recordsp;
210*0Sstevel@tonic-gate 	dbg_t		*dbp;
211*0Sstevel@tonic-gate 	dbg_t		*endp;
212*0Sstevel@tonic-gate 	dbg_rec_t	*rp;
213*0Sstevel@tonic-gate 	dn_rec_t	*recp;
214*0Sstevel@tonic-gate 	dn_rec_list_t	*reclp;
215*0Sstevel@tonic-gate 	dbg_t		*dbg_db;
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate 	if (dbg_net && hind >= 0 && hind < DBG_MAXTABLE) {
218*0Sstevel@tonic-gate 		dbg_db = (dbg_t *)dbg_handle[hind];
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate 		if (outp)
221*0Sstevel@tonic-gate 			*outp = NULL;
222*0Sstevel@tonic-gate 		if (nrecordsp)
223*0Sstevel@tonic-gate 			*nrecordsp = 0;
224*0Sstevel@tonic-gate 		inp = (dn_rec_t *)targetp;
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate 		(void) rw_rdlock(&dbg_lock[hind]);
227*0Sstevel@tonic-gate 		/*
228*0Sstevel@tonic-gate 		 * Simple linear search, aided by the fact that
229*0Sstevel@tonic-gate 		 * the server currently checks flags.
230*0Sstevel@tonic-gate 		 */
231*0Sstevel@tonic-gate 		if (DSVC_QISEQ(query, DN_QCIP)) {
232*0Sstevel@tonic-gate 			ind = inp->dn_cip.s_addr & dbg_mask;
233*0Sstevel@tonic-gate 			dbp = &dbg_db[ind];
234*0Sstevel@tonic-gate 			endp = &dbg_db[ind + 1];
235*0Sstevel@tonic-gate 		} else {
236*0Sstevel@tonic-gate 			ind = 0;
237*0Sstevel@tonic-gate 			dbp = dbg_db;
238*0Sstevel@tonic-gate 			endp = &dbg_db[dbg_size];
239*0Sstevel@tonic-gate 		}
240*0Sstevel@tonic-gate 		for (; dbp < endp; dbp++, ind++) {
241*0Sstevel@tonic-gate 			/*
242*0Sstevel@tonic-gate 			 * Initialize record. fields will be zero'd
243*0Sstevel@tonic-gate 			 * when initially mmap'd.
244*0Sstevel@tonic-gate 			 */
245*0Sstevel@tonic-gate 			if (dbp->dn_cid_len == 0) {
246*0Sstevel@tonic-gate 				/* Skip server address to avoid arp issues. */
247*0Sstevel@tonic-gate 				if (ind == (ntohl(server_ip.s_addr) % 256))
248*0Sstevel@tonic-gate 					continue;
249*0Sstevel@tonic-gate 				if (dbg_cid)
250*0Sstevel@tonic-gate 					(void) snprintf(dbp->dn_cid,
251*0Sstevel@tonic-gate 					    sizeof (dbp->dn_cid), "%8X",
252*0Sstevel@tonic-gate 					    dbg_cid++);
253*0Sstevel@tonic-gate 				dbp->dn_flags = dbg_flags;
254*0Sstevel@tonic-gate 				dbp->dn_lease = dbg_lease;
255*0Sstevel@tonic-gate 				dbp->dn_macro[0] = dbg_macro;
256*0Sstevel@tonic-gate 				dbp->dn_macro[1] = '\0';
257*0Sstevel@tonic-gate 				dbp->dn_cid_len = 1;
258*0Sstevel@tonic-gate 			}
259*0Sstevel@tonic-gate 			if (DSVC_QISEQ(query, DN_QCID) &&
260*0Sstevel@tonic-gate 			    (inp->dn_cid[0] != dbp->dn_cid[0] ||
261*0Sstevel@tonic-gate 			    memcmp(dbp->dn_cid, inp->dn_cid, inp->dn_cid_len)))
262*0Sstevel@tonic-gate 				continue;
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate 			rp = (dbg_rec_t *)smalloc(sizeof (dbg_rec_t));
265*0Sstevel@tonic-gate 			reclp = &rp->d_reclist;
266*0Sstevel@tonic-gate 			recp = &rp->d_rec;
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate 			if (nrecordsp)
269*0Sstevel@tonic-gate 				(*nrecordsp)++;
270*0Sstevel@tonic-gate 
271*0Sstevel@tonic-gate 			reclp->dnl_rec = recp;
272*0Sstevel@tonic-gate 			recp->dn_lease = dbp->dn_lease;
273*0Sstevel@tonic-gate 			recp->dn_sip.s_addr = ntohl(owner_ip->s_addr);
274*0Sstevel@tonic-gate 			recp->dn_cip.s_addr = 0xd000000 + (hind << 16) + ind;
275*0Sstevel@tonic-gate 			recp->dn_cid_len = dbp->dn_cid_len;
276*0Sstevel@tonic-gate 			recp->dn_flags = dbp->dn_flags;
277*0Sstevel@tonic-gate 			recp->dn_macro[0] = dbp->dn_macro[0];
278*0Sstevel@tonic-gate 			recp->dn_macro[1] = '\0';
279*0Sstevel@tonic-gate 			if ((recp->dn_cid[0] = dbp->dn_cid[0]) != '\0')
280*0Sstevel@tonic-gate 				(void) memcpy(recp->dn_cid, dbp->dn_cid,
281*0Sstevel@tonic-gate 				    dbp->dn_cid_len);
282*0Sstevel@tonic-gate 			if (*outp == NULL)
283*0Sstevel@tonic-gate 				*outp = reclp;
284*0Sstevel@tonic-gate 			else {
285*0Sstevel@tonic-gate 				reclp->dnl_next = *outp;
286*0Sstevel@tonic-gate 				*outp = reclp;
287*0Sstevel@tonic-gate 			}
288*0Sstevel@tonic-gate 			if (count > 0 && nrecordsp && *nrecordsp >= count)
289*0Sstevel@tonic-gate 				break;
290*0Sstevel@tonic-gate 		}
291*0Sstevel@tonic-gate 		(void) rw_unlock(&dbg_lock[hind]);
292*0Sstevel@tonic-gate 		ret = DSVC_SUCCESS;
293*0Sstevel@tonic-gate 	} else
294*0Sstevel@tonic-gate 		ret = lookup_dd(hand, partial, query, count, targetp,
295*0Sstevel@tonic-gate 		    recordsp, nrecordsp);
296*0Sstevel@tonic-gate 
297*0Sstevel@tonic-gate 	return (ret);
298*0Sstevel@tonic-gate #endif	/* DEBUG */
299*0Sstevel@tonic-gate }
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate int
dhcp_modify_dd_entry(dsvc_handle_t hand,const void * origp,void * newp)302*0Sstevel@tonic-gate dhcp_modify_dd_entry(dsvc_handle_t hand, const void *origp, void *newp)
303*0Sstevel@tonic-gate {
304*0Sstevel@tonic-gate #ifndef	DEBUG
305*0Sstevel@tonic-gate 	return (modify_dd_entry(hand, origp, newp));
306*0Sstevel@tonic-gate 
307*0Sstevel@tonic-gate #else	/* DEBUG */
308*0Sstevel@tonic-gate 	int 		ret;
309*0Sstevel@tonic-gate 	int 		hind = (int)hand;
310*0Sstevel@tonic-gate 	int 		ind;
311*0Sstevel@tonic-gate 	dn_rec_t	*dnp;
312*0Sstevel@tonic-gate 	dbg_t		*dbp;
313*0Sstevel@tonic-gate 	dbg_t		*dbg_db;
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate 	if (dbg_net && hind >= 0 && hind < DBG_MAXTABLE) {
316*0Sstevel@tonic-gate 		dbg_db = (dbg_t *)dbg_handle[hind];
317*0Sstevel@tonic-gate 
318*0Sstevel@tonic-gate 		dnp = (dn_rec_t *)newp;
319*0Sstevel@tonic-gate 		ind = dnp->dn_cip.s_addr & dbg_mask;
320*0Sstevel@tonic-gate 		dbp = &dbg_db[ind];
321*0Sstevel@tonic-gate 		(void) rw_wrlock(&dbg_lock[hind]);
322*0Sstevel@tonic-gate 		dbp->dn_lease = dnp->dn_lease;
323*0Sstevel@tonic-gate 		dbp->dn_cid_len = dnp->dn_cid_len;
324*0Sstevel@tonic-gate 		dbp->dn_flags = dnp->dn_flags;
325*0Sstevel@tonic-gate 		/*
326*0Sstevel@tonic-gate 		 * Performance: avoid routine call when NULL string
327*0Sstevel@tonic-gate 		 * is being copied.
328*0Sstevel@tonic-gate 		 */
329*0Sstevel@tonic-gate 		if ((dbp->dn_cid[0] = dnp->dn_cid[0]) != '\0')
330*0Sstevel@tonic-gate 			(void) memcpy(dbp->dn_cid, dnp->dn_cid,
331*0Sstevel@tonic-gate 			    dnp->dn_cid_len);
332*0Sstevel@tonic-gate 		(void) rw_unlock(&dbg_lock[hind]);
333*0Sstevel@tonic-gate 		ret = DSVC_SUCCESS;
334*0Sstevel@tonic-gate 	} else
335*0Sstevel@tonic-gate 		ret = modify_dd_entry(hand, origp, newp);
336*0Sstevel@tonic-gate 
337*0Sstevel@tonic-gate 	return (ret);
338*0Sstevel@tonic-gate #endif	/* DEBUG */
339*0Sstevel@tonic-gate }
340*0Sstevel@tonic-gate 
341*0Sstevel@tonic-gate void
dhcp_free_dd_list(dsvc_handle_t hand,void * listp)342*0Sstevel@tonic-gate dhcp_free_dd_list(dsvc_handle_t hand, void *listp)
343*0Sstevel@tonic-gate {
344*0Sstevel@tonic-gate #ifndef	DEBUG
345*0Sstevel@tonic-gate 	free_dd_list(hand, listp);
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate #else	/* DEBUG */
348*0Sstevel@tonic-gate 	dn_rec_list_t *ptr;
349*0Sstevel@tonic-gate 	int hind = (int)hand;
350*0Sstevel@tonic-gate 
351*0Sstevel@tonic-gate 	if (dbg_net && hind >= 0 && hind < DBG_MAXTABLE) {
352*0Sstevel@tonic-gate 		while ((ptr = listp) != NULL) {
353*0Sstevel@tonic-gate 			listp = ptr->dnl_next;
354*0Sstevel@tonic-gate 			free(ptr);
355*0Sstevel@tonic-gate 		}
356*0Sstevel@tonic-gate 	} else
357*0Sstevel@tonic-gate 		free_dd_list(hand, listp);
358*0Sstevel@tonic-gate #endif	/* DEBUG */
359*0Sstevel@tonic-gate }
360*0Sstevel@tonic-gate 
361*0Sstevel@tonic-gate /*
362*0Sstevel@tonic-gate  * cmp_lrusort: qsort() comparison routine to sort lru list.
363*0Sstevel@tonic-gate  */
364*0Sstevel@tonic-gate static int
cmp_lrusort(const void * a,const void * b)365*0Sstevel@tonic-gate cmp_lrusort(const void *a, const void *b)
366*0Sstevel@tonic-gate {
367*0Sstevel@tonic-gate 	dn_rec_list_t *r1 = *(dn_rec_list_t **)a;
368*0Sstevel@tonic-gate 	dn_rec_list_t *r2 = *(dn_rec_list_t **)b;
369*0Sstevel@tonic-gate 
370*0Sstevel@tonic-gate 	if (r1->dnl_rec->dn_lease < r2->dnl_rec->dn_lease)
371*0Sstevel@tonic-gate 		return (-1);
372*0Sstevel@tonic-gate 	else if (r1->dnl_rec->dn_lease == r2->dnl_rec->dn_lease)
373*0Sstevel@tonic-gate 		return (0);
374*0Sstevel@tonic-gate 	else
375*0Sstevel@tonic-gate 		return (1);
376*0Sstevel@tonic-gate }
377*0Sstevel@tonic-gate 
378*0Sstevel@tonic-gate /*
379*0Sstevel@tonic-gate  * get_lrusort: quick sort of eligible lru container entries.
380*0Sstevel@tonic-gate  */
381*0Sstevel@tonic-gate static dn_rec_list_t *
get_lrusort(dsvc_dnet_t * pnd,dn_rec_list_t * lrup,uint_t * lrecords)382*0Sstevel@tonic-gate get_lrusort(dsvc_dnet_t *pnd, dn_rec_list_t *lrup, uint_t *lrecords)
383*0Sstevel@tonic-gate {
384*0Sstevel@tonic-gate 	size_t nel;
385*0Sstevel@tonic-gate 	size_t size = *lrecords * sizeof (dn_rec_list_t *);
386*0Sstevel@tonic-gate 	dn_rec_list_t *from, **to, *next, *freerec = NULL;
387*0Sstevel@tonic-gate 	dn_rec_list_t *lrupage;
388*0Sstevel@tonic-gate 	dn_rec_t *rp;
389*0Sstevel@tonic-gate 	time_t reuse_time = time(NULL) - min_lru;
390*0Sstevel@tonic-gate 	uint_t records = 0;
391*0Sstevel@tonic-gate #ifndef	NDEBUG
392*0Sstevel@tonic-gate 	int cnt = 0;
393*0Sstevel@tonic-gate #endif	/* !NDEBUG */
394*0Sstevel@tonic-gate 
395*0Sstevel@tonic-gate 	(void) mutex_lock(&pnd->lrupage_mtx);
396*0Sstevel@tonic-gate 	if (pnd->lrupage == NULL || pnd->lrusize < size) {
397*0Sstevel@tonic-gate 		if (pnd->lrupage != NULL)
398*0Sstevel@tonic-gate 			free(pnd->lrupage);
399*0Sstevel@tonic-gate 		pnd->lrupage = (dn_rec_list_t **)smalloc(size);
400*0Sstevel@tonic-gate 		pnd->lrusize = size;
401*0Sstevel@tonic-gate 	}
402*0Sstevel@tonic-gate 	if ((to = pnd->lrupage) == NULL) {
403*0Sstevel@tonic-gate 		pnd->lrusize = 0;
404*0Sstevel@tonic-gate 		(void) mutex_unlock(&pnd->lrupage_mtx);
405*0Sstevel@tonic-gate 		return (lrup);
406*0Sstevel@tonic-gate 	}
407*0Sstevel@tonic-gate 
408*0Sstevel@tonic-gate 	/*
409*0Sstevel@tonic-gate 	 * Build a list of entries, discarding those which are in use.
410*0Sstevel@tonic-gate 	 */
411*0Sstevel@tonic-gate 	*to = NULL;
412*0Sstevel@tonic-gate 	for (from = lrup; from != NULL; from = next) {
413*0Sstevel@tonic-gate 		next = from->dnl_next;
414*0Sstevel@tonic-gate 		rp = from->dnl_rec;
415*0Sstevel@tonic-gate 		if (rp->dn_lease > reuse_time ||
416*0Sstevel@tonic-gate 		    (rp->dn_flags & DN_FAUTOMATIC) ||
417*0Sstevel@tonic-gate 		    rp->dn_lease == DHCP_PERM) {
418*0Sstevel@tonic-gate 			from->dnl_next = freerec;
419*0Sstevel@tonic-gate 			freerec = from;
420*0Sstevel@tonic-gate 		} else {
421*0Sstevel@tonic-gate 			records++;
422*0Sstevel@tonic-gate 			*(to++) = from;
423*0Sstevel@tonic-gate 		}
424*0Sstevel@tonic-gate 		assert(++cnt <= *lrecords);
425*0Sstevel@tonic-gate 	}
426*0Sstevel@tonic-gate 
427*0Sstevel@tonic-gate 	/*
428*0Sstevel@tonic-gate 	 * Sort any usable elements, and relink.
429*0Sstevel@tonic-gate 	 */
430*0Sstevel@tonic-gate 	nel = (int)(to - pnd->lrupage);
431*0Sstevel@tonic-gate 	if (nel > 0) {
432*0Sstevel@tonic-gate 		if (nel > 1)
433*0Sstevel@tonic-gate 			qsort(pnd->lrupage, nel, sizeof (dn_rec_list_t *),
434*0Sstevel@tonic-gate 			    cmp_lrusort);
435*0Sstevel@tonic-gate 		for (to = pnd->lrupage; nel > 0; to++, nel--)
436*0Sstevel@tonic-gate 			(*to)->dnl_next = *(to + 1);
437*0Sstevel@tonic-gate 		to--;
438*0Sstevel@tonic-gate 		(*to)->dnl_next = NULL;
439*0Sstevel@tonic-gate 	}
440*0Sstevel@tonic-gate 
441*0Sstevel@tonic-gate 	/*
442*0Sstevel@tonic-gate 	 * Free any unusable elements, return any usable elements.
443*0Sstevel@tonic-gate 	 */
444*0Sstevel@tonic-gate 	if (freerec)
445*0Sstevel@tonic-gate 		dhcp_free_dd_list(pnd->dh, freerec);
446*0Sstevel@tonic-gate 	*lrecords = records;
447*0Sstevel@tonic-gate 
448*0Sstevel@tonic-gate 	lrupage = *(pnd->lrupage);
449*0Sstevel@tonic-gate 	(void) mutex_unlock(&pnd->lrupage_mtx);
450*0Sstevel@tonic-gate 	return (lrupage);
451*0Sstevel@tonic-gate }
452*0Sstevel@tonic-gate 
453*0Sstevel@tonic-gate /*
454*0Sstevel@tonic-gate  * dhcp_lookup_dd_classify: perform lookup_dd(), or use existing records
455*0Sstevel@tonic-gate  * if supplied, and classify the results based on the type of search criteria
456*0Sstevel@tonic-gate  * being employed. Centralized policy for DN_FMANUAL and DN_FUNUSABLE flag
457*0Sstevel@tonic-gate  * processing are implemented here. Classification is specialized
458*0Sstevel@tonic-gate  * based on these specific search criteria:
459*0Sstevel@tonic-gate  *
460*0Sstevel@tonic-gate  *	S_CID		A CID match is requested. Perform DN_FMANUAL and
461*0Sstevel@tonic-gate  *			DN_FUNUSABLE processing.
462*0Sstevel@tonic-gate  *	S_FREE		A search for free records. Only examine first
463*0Sstevel@tonic-gate  *			matching record.
464*0Sstevel@tonic-gate  *	S_LRU		A search for lru records. Perform sort if needed,
465*0Sstevel@tonic-gate  *			and only examine first matching record.
466*0Sstevel@tonic-gate  *
467*0Sstevel@tonic-gate  * A matching record is detached and returned if found (ok ||
468*0Sstevel@tonic-gate  * manual + unusable). Other successful matches are returned in recordsp as
469*0Sstevel@tonic-gate  * a cache.
470*0Sstevel@tonic-gate  */
471*0Sstevel@tonic-gate void *
dhcp_lookup_dd_classify(dsvc_dnet_t * pnd,boolean_t partial,uint_t query,int count,const dn_rec_t * targetp,void ** recordsp,int searchtype)472*0Sstevel@tonic-gate dhcp_lookup_dd_classify(dsvc_dnet_t *pnd, boolean_t partial, uint_t query,
473*0Sstevel@tonic-gate     int count, const dn_rec_t *targetp, void **recordsp, int searchtype)
474*0Sstevel@tonic-gate {
475*0Sstevel@tonic-gate 	int		err;
476*0Sstevel@tonic-gate 	uint_t		rec_cnt = 0, manual = 0;
477*0Sstevel@tonic-gate 	dn_rec_t	*dnp;
478*0Sstevel@tonic-gate 	dn_rec_list_t	*nlp = NULL, *dnlp = NULL;
479*0Sstevel@tonic-gate 	dn_rec_list_t	*unulp = NULL;		/* list of unusables, !manual */
480*0Sstevel@tonic-gate 	dn_rec_list_t	*unu_m_lp = NULL;	/* list of unusable + manual */
481*0Sstevel@tonic-gate 	dn_rec_list_t	*m_lp = NULL;		/* list of manual records */
482*0Sstevel@tonic-gate 	dn_rec_list_t	*cachep = NULL;		/* match cache */
483*0Sstevel@tonic-gate 	struct in_addr	swapaddr;
484*0Sstevel@tonic-gate 	char		ntoab[INET_ADDRSTRLEN];
485*0Sstevel@tonic-gate 
486*0Sstevel@tonic-gate 	/*
487*0Sstevel@tonic-gate 	 * Lookup records matching the specified criteria, or use
488*0Sstevel@tonic-gate 	 * records from a previous lookup supplied for classification.
489*0Sstevel@tonic-gate 	 */
490*0Sstevel@tonic-gate 	if (*recordsp == NULL) {
491*0Sstevel@tonic-gate 
492*0Sstevel@tonic-gate 		TNF_PROBE_1_DEBUG(classify, "classify classify",
493*0Sstevel@tonic-gate 		    "classify_query%debug 'in func classify'",
494*0Sstevel@tonic-gate 		    tnf_long, query, query);
495*0Sstevel@tonic-gate 
496*0Sstevel@tonic-gate 		err = dhcp_lookup_dd(pnd->dh, partial, query, count, targetp,
497*0Sstevel@tonic-gate 		    (void **)recordsp, &rec_cnt);
498*0Sstevel@tonic-gate 
499*0Sstevel@tonic-gate 		TNF_PROBE_1_DEBUG(classify_cid_end, "classify classify_end",
500*0Sstevel@tonic-gate 		    "classify_end%debug 'in func classify'",
501*0Sstevel@tonic-gate 		    tnf_long, rec_cnt, rec_cnt);
502*0Sstevel@tonic-gate 
503*0Sstevel@tonic-gate 		/*
504*0Sstevel@tonic-gate 		 * If any error occurs, mark the dsvc_dnet_t table
505*0Sstevel@tonic-gate 		 * for immediate close and reopen. Let the protocol
506*0Sstevel@tonic-gate 		 * perform recover, rather than attempting time-consuming
507*0Sstevel@tonic-gate 		 * in-place error recovery.
508*0Sstevel@tonic-gate 		 */
509*0Sstevel@tonic-gate 		if (err != DSVC_SUCCESS) {
510*0Sstevel@tonic-gate 			(void) mutex_lock(&pnd->pnd_mtx);
511*0Sstevel@tonic-gate 			pnd->flags |= DHCP_PND_ERROR;
512*0Sstevel@tonic-gate 			hash_Dtime(pnd->hand, 0);
513*0Sstevel@tonic-gate 			(void) mutex_unlock(&pnd->pnd_mtx);
514*0Sstevel@tonic-gate #ifdef	DEBUG
515*0Sstevel@tonic-gate 			dhcpmsg(LOG_DEBUG, "classify failure %s\n",
516*0Sstevel@tonic-gate 				dhcpsvc_errmsg(err));
517*0Sstevel@tonic-gate #endif	/* DEBUG */
518*0Sstevel@tonic-gate 			*recordsp = NULL;
519*0Sstevel@tonic-gate 			return (NULL);
520*0Sstevel@tonic-gate 		}
521*0Sstevel@tonic-gate 
522*0Sstevel@tonic-gate 		/*
523*0Sstevel@tonic-gate 		 * For LRU classification, sort returned records based
524*0Sstevel@tonic-gate 		 * on dn_lease field. Discards records with valid lease
525*0Sstevel@tonic-gate 		 * times; adjusts rec_cnt accordingly.
526*0Sstevel@tonic-gate 		 */
527*0Sstevel@tonic-gate 		if (searchtype & S_LRU)
528*0Sstevel@tonic-gate 			*recordsp = get_lrusort(pnd, *recordsp, &rec_cnt);
529*0Sstevel@tonic-gate 
530*0Sstevel@tonic-gate 	}
531*0Sstevel@tonic-gate 
532*0Sstevel@tonic-gate 	/*
533*0Sstevel@tonic-gate 	 * Record classification: scan through all records, performing
534*0Sstevel@tonic-gate 	 * DN_FUNUSABLE and DN_FMANUAL processing. Note that most of the
535*0Sstevel@tonic-gate 	 * work has been performed by the datastore query. Remove the matching
536*0Sstevel@tonic-gate 	 * entry from the singlely-linked record list, for return. Free any
537*0Sstevel@tonic-gate 	 * non-matching entries prior to the match. Pass back any additional
538*0Sstevel@tonic-gate 	 * entries after the match in the recordsp pointer for possible re-use
539*0Sstevel@tonic-gate 	 * by the caching code.
540*0Sstevel@tonic-gate 	 */
541*0Sstevel@tonic-gate 
542*0Sstevel@tonic-gate 	for (nlp = detach_dnrec_from_list(NULL, *recordsp,
543*0Sstevel@tonic-gate 	    (dn_rec_list_t **)recordsp); nlp != NULL;
544*0Sstevel@tonic-gate 	    nlp = detach_dnrec_from_list(NULL, *recordsp,
545*0Sstevel@tonic-gate 	    (dn_rec_list_t **)recordsp)) {
546*0Sstevel@tonic-gate 		/*
547*0Sstevel@tonic-gate 		 * If we find that there is a DN_FMANUAL entry that is
548*0Sstevel@tonic-gate 		 * DN_FUNUSABLE, we fail the request, when performing a
549*0Sstevel@tonic-gate 		 * CID search, even though there may be other CID matches. In
550*0Sstevel@tonic-gate 		 * the CID case, those other CID matches are errors, because
551*0Sstevel@tonic-gate 		 * there should be one and only one record for a client if that
552*0Sstevel@tonic-gate 		 * record is marked as being DN_FMANUALly assigned. We tell
553*0Sstevel@tonic-gate 		 * the user how many of those CID matches there are. If there
554*0Sstevel@tonic-gate 		 * are no DN_FMANUAL records, the first matching record which
555*0Sstevel@tonic-gate 		 * is USABLE wins.
556*0Sstevel@tonic-gate 		 */
557*0Sstevel@tonic-gate 		dnp = nlp->dnl_rec;
558*0Sstevel@tonic-gate 		if (dnp->dn_flags & DN_FUNUSABLE) {
559*0Sstevel@tonic-gate 			if ((searchtype & (S_CID|S_FREE|S_LRU)) == S_CID) {
560*0Sstevel@tonic-gate 				char	cidbuf[DHCP_MAX_OPT_SIZE];
561*0Sstevel@tonic-gate 				uint_t	blen = sizeof (cidbuf);
562*0Sstevel@tonic-gate 
563*0Sstevel@tonic-gate 				(void) octet_to_hexascii(targetp->dn_cid,
564*0Sstevel@tonic-gate 				    targetp->dn_cid_len,
565*0Sstevel@tonic-gate 				    cidbuf, &blen);
566*0Sstevel@tonic-gate 
567*0Sstevel@tonic-gate 				swapaddr.s_addr = htonl(dnp->dn_cip.s_addr);
568*0Sstevel@tonic-gate 
569*0Sstevel@tonic-gate 				dhcpmsg(LOG_NOTICE, "(%1$s,%2$s) "
570*0Sstevel@tonic-gate 				    "currently marked as unusable.\n", cidbuf,
571*0Sstevel@tonic-gate 				    inet_ntop(AF_INET, &swapaddr, ntoab,
572*0Sstevel@tonic-gate 				    sizeof (ntoab)));
573*0Sstevel@tonic-gate 			}
574*0Sstevel@tonic-gate 
575*0Sstevel@tonic-gate 			/* build list of unusable records */
576*0Sstevel@tonic-gate 			if (dnp->dn_flags & DN_FMANUAL) {
577*0Sstevel@tonic-gate 				attach_dnrec_to_list(nlp, &unu_m_lp);
578*0Sstevel@tonic-gate 				manual++;
579*0Sstevel@tonic-gate 			} else
580*0Sstevel@tonic-gate 				attach_dnrec_to_list(nlp, &unulp);
581*0Sstevel@tonic-gate 		} else {
582*0Sstevel@tonic-gate 			if (dnp->dn_flags & DN_FMANUAL) {
583*0Sstevel@tonic-gate 				attach_dnrec_to_list(nlp, &m_lp);
584*0Sstevel@tonic-gate 				manual++;
585*0Sstevel@tonic-gate 			} else
586*0Sstevel@tonic-gate 				attach_dnrec_to_list(nlp, &cachep);
587*0Sstevel@tonic-gate 			/*
588*0Sstevel@tonic-gate 			 * These searches do not require examining all
589*0Sstevel@tonic-gate 			 * matches.
590*0Sstevel@tonic-gate 			 */
591*0Sstevel@tonic-gate 			if (searchtype & (S_FREE|S_LRU))
592*0Sstevel@tonic-gate 				break;
593*0Sstevel@tonic-gate 		}
594*0Sstevel@tonic-gate 	}
595*0Sstevel@tonic-gate 
596*0Sstevel@tonic-gate 	/*
597*0Sstevel@tonic-gate 	 * Warnings are printed for CID searches which end with
598*0Sstevel@tonic-gate 	 * DN_FUNUSABLE|DN_FMANUAL match(es).
599*0Sstevel@tonic-gate 	 */
600*0Sstevel@tonic-gate 	if (m_lp != NULL || unu_m_lp != NULL) {
601*0Sstevel@tonic-gate 		if (manual > 1) {
602*0Sstevel@tonic-gate 			char	cidbuf[DHCP_MAX_OPT_SIZE];
603*0Sstevel@tonic-gate 			uint_t	blen = sizeof (cidbuf);
604*0Sstevel@tonic-gate 
605*0Sstevel@tonic-gate 			(void) octet_to_hexascii(targetp->dn_cid,
606*0Sstevel@tonic-gate 				targetp->dn_cid_len,
607*0Sstevel@tonic-gate 				cidbuf, &blen);
608*0Sstevel@tonic-gate 			dhcpmsg(LOG_WARNING,
609*0Sstevel@tonic-gate 			    "Manual allocation (%1$s) has %2$d other MANUAL"
610*0Sstevel@tonic-gate 			    " records. It should have 0.\n", cidbuf,
611*0Sstevel@tonic-gate 			    manual - 1);
612*0Sstevel@tonic-gate 		}
613*0Sstevel@tonic-gate 		if (unu_m_lp != NULL) {
614*0Sstevel@tonic-gate 			dnlp = detach_dnrec_from_list(NULL, unu_m_lp,
615*0Sstevel@tonic-gate 			    &unu_m_lp);
616*0Sstevel@tonic-gate 		} else
617*0Sstevel@tonic-gate 			dnlp = detach_dnrec_from_list(NULL, m_lp, &m_lp);
618*0Sstevel@tonic-gate 	}
619*0Sstevel@tonic-gate 
620*0Sstevel@tonic-gate 	/* Free any unusable entries */
621*0Sstevel@tonic-gate 	if (unulp != NULL)
622*0Sstevel@tonic-gate 		dhcp_free_dd_list(pnd->dh, unulp);
623*0Sstevel@tonic-gate 
624*0Sstevel@tonic-gate 	/* any other... */
625*0Sstevel@tonic-gate 	if (dnlp == NULL)
626*0Sstevel@tonic-gate 		dnlp = detach_dnrec_from_list(NULL, cachep, &cachep);
627*0Sstevel@tonic-gate 
628*0Sstevel@tonic-gate 	/*
629*0Sstevel@tonic-gate 	 * Return any unused elements for possible caching use. These are
630*0Sstevel@tonic-gate 	 * the  additional manual + unusable (as punishment for having
631*0Sstevel@tonic-gate 	 * multiple items), manual, and and any others.
632*0Sstevel@tonic-gate 	 */
633*0Sstevel@tonic-gate 	if (cachep != NULL)
634*0Sstevel@tonic-gate 		attach_dnrec_to_list(cachep, (dn_rec_list_t **)recordsp);
635*0Sstevel@tonic-gate 	if (m_lp != NULL)
636*0Sstevel@tonic-gate 		attach_dnrec_to_list(m_lp, (dn_rec_list_t **)recordsp);
637*0Sstevel@tonic-gate 	if (unu_m_lp != NULL)
638*0Sstevel@tonic-gate 		attach_dnrec_to_list(unu_m_lp, (dn_rec_list_t **)recordsp);
639*0Sstevel@tonic-gate 
640*0Sstevel@tonic-gate 	/*
641*0Sstevel@tonic-gate 	 * Return one of the matching record(s).
642*0Sstevel@tonic-gate 	 */
643*0Sstevel@tonic-gate 	return (dnlp);
644*0Sstevel@tonic-gate }
645*0Sstevel@tonic-gate 
646*0Sstevel@tonic-gate /*
647*0Sstevel@tonic-gate  * Error message function. If debugging off, then logging goes to
648*0Sstevel@tonic-gate  * syslog.
649*0Sstevel@tonic-gate  *
650*0Sstevel@tonic-gate  * Must be MT SAFE - called by various threads as well as the main thread.
651*0Sstevel@tonic-gate  */
652*0Sstevel@tonic-gate 
653*0Sstevel@tonic-gate /*VARARGS2*/
654*0Sstevel@tonic-gate void
dhcpmsg(int errlevel,const char * fmtp,...)655*0Sstevel@tonic-gate dhcpmsg(int errlevel, const char *fmtp, ...)
656*0Sstevel@tonic-gate {
657*0Sstevel@tonic-gate 	char		buff[BUFSIZ], errbuf[BUFSIZ];
658*0Sstevel@tonic-gate 	const char *f = buff;
659*0Sstevel@tonic-gate 	va_list		ap;
660*0Sstevel@tonic-gate 
661*0Sstevel@tonic-gate 	if (debug < 0)
662*0Sstevel@tonic-gate 		return;
663*0Sstevel@tonic-gate 
664*0Sstevel@tonic-gate 	va_start(ap, fmtp);
665*0Sstevel@tonic-gate 
666*0Sstevel@tonic-gate 	if (debug > 0)  {
667*0Sstevel@tonic-gate 		if (errlevel != LOG_ERR)
668*0Sstevel@tonic-gate 			(void) snprintf(errbuf, sizeof (errbuf),
669*0Sstevel@tonic-gate 			    "%lx: ", time(NULL));
670*0Sstevel@tonic-gate 		else
671*0Sstevel@tonic-gate 			(void) snprintf(errbuf, sizeof (errbuf),
672*0Sstevel@tonic-gate 			    "%lx: (%s)", time(NULL), strerror(errno));
673*0Sstevel@tonic-gate 		(void) snprintf(buff, sizeof (buff), "%s %s", errbuf,
674*0Sstevel@tonic-gate 		    gettext(fmtp));
675*0Sstevel@tonic-gate 		(void) vfprintf(stderr, f, ap);
676*0Sstevel@tonic-gate 	} else if (debug == 0)
677*0Sstevel@tonic-gate 		(void) vsyslog(errlevel, gettext(fmtp), ap);
678*0Sstevel@tonic-gate 
679*0Sstevel@tonic-gate 	va_end(ap);
680*0Sstevel@tonic-gate }
681*0Sstevel@tonic-gate 
682*0Sstevel@tonic-gate /*
683*0Sstevel@tonic-gate  * smalloc()  --  safe malloc()
684*0Sstevel@tonic-gate  *
685*0Sstevel@tonic-gate  * Always returns a valid pointer(if it returns at all).  The allocated
686*0Sstevel@tonic-gate  * memory is initialized to all zeros.  If malloc() returns an error, a
687*0Sstevel@tonic-gate  * message is printed using the syslog() function and the program aborts
688*0Sstevel@tonic-gate  * with a status of 1.
689*0Sstevel@tonic-gate  *
690*0Sstevel@tonic-gate  * Must be MT SAFE - called by threads other than the main thread.
691*0Sstevel@tonic-gate  */
692*0Sstevel@tonic-gate void *
smalloc(uint_t nbytes)693*0Sstevel@tonic-gate smalloc(uint_t nbytes)
694*0Sstevel@tonic-gate {
695*0Sstevel@tonic-gate 	char		*retvalue;
696*0Sstevel@tonic-gate 
697*0Sstevel@tonic-gate 	if ((retvalue = calloc(nbytes, sizeof (char))) == NULL) {
698*0Sstevel@tonic-gate 		dhcpmsg(LOG_ERR, "Cannot allocate memory (%s), exiting\n",
699*0Sstevel@tonic-gate 		    strerror(errno));
700*0Sstevel@tonic-gate 		exit(1);
701*0Sstevel@tonic-gate 	}
702*0Sstevel@tonic-gate 	return (retvalue);
703*0Sstevel@tonic-gate }
704*0Sstevel@tonic-gate 
705*0Sstevel@tonic-gate /*
706*0Sstevel@tonic-gate  * srealloc()  --  safe realloc()
707*0Sstevel@tonic-gate  *
708*0Sstevel@tonic-gate  * Always returns a valid pointer(if it returns at all).
709*0Sstevel@tonic-gate  * If realloc() returns an error, a message is printed using the syslog()
710*0Sstevel@tonic-gate  * function and the program aborts with a status of 1.
711*0Sstevel@tonic-gate  * Unlike smalloc(), does not initialize the buffer to all zeros.
712*0Sstevel@tonic-gate  *
713*0Sstevel@tonic-gate  * Must be MT SAFE - called by threads other than the main thread.
714*0Sstevel@tonic-gate  */
715*0Sstevel@tonic-gate void *
srealloc(void * arg,uint_t nbytes)716*0Sstevel@tonic-gate srealloc(void *arg, uint_t nbytes)
717*0Sstevel@tonic-gate {
718*0Sstevel@tonic-gate 	if ((arg = realloc(arg, nbytes)) == NULL) {
719*0Sstevel@tonic-gate 		dhcpmsg(LOG_ERR, "Cannot allocate memory (%s), exiting\n",
720*0Sstevel@tonic-gate 		    strerror(errno));
721*0Sstevel@tonic-gate 		exit(1);
722*0Sstevel@tonic-gate 	}
723*0Sstevel@tonic-gate 	return (arg);
724*0Sstevel@tonic-gate }
725*0Sstevel@tonic-gate 
726*0Sstevel@tonic-gate /*
727*0Sstevel@tonic-gate  * Matches the speficied ip address with our owner_ip addresses.
728*0Sstevel@tonic-gate  * Returns NULL if no match is found.
729*0Sstevel@tonic-gate  */
730*0Sstevel@tonic-gate struct in_addr *
match_ownerip(in_addr_t new)731*0Sstevel@tonic-gate match_ownerip(in_addr_t new)
732*0Sstevel@tonic-gate {
733*0Sstevel@tonic-gate 	struct in_addr *oip = owner_ip;
734*0Sstevel@tonic-gate 
735*0Sstevel@tonic-gate 	while (oip->s_addr != INADDR_ANY && oip->s_addr != new)
736*0Sstevel@tonic-gate 		oip++;
737*0Sstevel@tonic-gate 	return ((oip->s_addr != INADDR_ANY) ? oip : NULL);
738*0Sstevel@tonic-gate }
739*0Sstevel@tonic-gate 
740*0Sstevel@tonic-gate /*
741*0Sstevel@tonic-gate  * qualify_hostname()  --  concatenate host  "."  domain  "."  NULL
742*0Sstevel@tonic-gate  */
743*0Sstevel@tonic-gate int
qualify_hostname(char * fqname,const char * host,const char * domain,int host_length,int domain_length)744*0Sstevel@tonic-gate qualify_hostname(char *fqname, const char *host, const char *domain,
745*0Sstevel@tonic-gate     int host_length, int domain_length)
746*0Sstevel@tonic-gate {
747*0Sstevel@tonic-gate 	char *fqptr;
748*0Sstevel@tonic-gate 
749*0Sstevel@tonic-gate 	if (domain_length + host_length + 2 > NS_MAXDNAME) {
750*0Sstevel@tonic-gate 		dhcpmsg(LOG_ERR, "qualify_hostname: FQDN too long\n");
751*0Sstevel@tonic-gate 		return (-1);
752*0Sstevel@tonic-gate 	}
753*0Sstevel@tonic-gate 
754*0Sstevel@tonic-gate 	fqptr = fqname;
755*0Sstevel@tonic-gate 
756*0Sstevel@tonic-gate 	(void) memcpy(fqptr, host, host_length);
757*0Sstevel@tonic-gate 	fqptr += host_length;
758*0Sstevel@tonic-gate 
759*0Sstevel@tonic-gate 	*fqptr = '.';
760*0Sstevel@tonic-gate 	fqptr++;
761*0Sstevel@tonic-gate 
762*0Sstevel@tonic-gate 	(void) memcpy(fqptr, domain, domain_length);
763*0Sstevel@tonic-gate 	fqptr += domain_length;
764*0Sstevel@tonic-gate 
765*0Sstevel@tonic-gate 	*fqptr = '.';
766*0Sstevel@tonic-gate 	*(fqptr+1) = '\0';
767*0Sstevel@tonic-gate 
768*0Sstevel@tonic-gate 	return (0);
769*0Sstevel@tonic-gate }
770