1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 1997-2002 Sun Microsystems, Inc.  All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate /*
7*0Sstevel@tonic-gate  * Copyright (c) 1989, 1993, 1995
8*0Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
9*0Sstevel@tonic-gate  *
10*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
11*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
12*0Sstevel@tonic-gate  * are met:
13*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
14*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
15*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
16*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
17*0Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
18*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
19*0Sstevel@tonic-gate  *    must display the following acknowledgement:
20*0Sstevel@tonic-gate  *	This product includes software developed by the University of
21*0Sstevel@tonic-gate  *	California, Berkeley and its contributors.
22*0Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
23*0Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
24*0Sstevel@tonic-gate  *    without specific prior written permission.
25*0Sstevel@tonic-gate  *
26*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27*0Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29*0Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30*0Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31*0Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32*0Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34*0Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35*0Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36*0Sstevel@tonic-gate  * SUCH DAMAGE.
37*0Sstevel@tonic-gate  */
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate /*
40*0Sstevel@tonic-gate  * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
41*0Sstevel@tonic-gate  *
42*0Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
43*0Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
44*0Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
45*0Sstevel@tonic-gate  *
46*0Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
47*0Sstevel@tonic-gate  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
48*0Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
49*0Sstevel@tonic-gate  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
50*0Sstevel@tonic-gate  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
51*0Sstevel@tonic-gate  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
52*0Sstevel@tonic-gate  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
53*0Sstevel@tonic-gate  * SOFTWARE.
54*0Sstevel@tonic-gate  */
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
59*0Sstevel@tonic-gate static const char rcsid[] = "$Id: lcl_nw.c,v 1.22 2001/05/29 05:49:07 marka Exp $";
60*0Sstevel@tonic-gate /* from getgrent.c 8.2 (Berkeley) 3/21/94"; */
61*0Sstevel@tonic-gate /* from BSDI Id: getgrent.c,v 2.8 1996/05/28 18:15:14 bostic Exp $	*/
62*0Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate /* Imports */
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate #include "port_before.h"
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate #include <sys/types.h>
69*0Sstevel@tonic-gate #include <sys/socket.h>
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate #include <netinet/in.h>
72*0Sstevel@tonic-gate #include <arpa/inet.h>
73*0Sstevel@tonic-gate #include <arpa/nameser.h>
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate #include <errno.h>
76*0Sstevel@tonic-gate #include <fcntl.h>
77*0Sstevel@tonic-gate #include <resolv.h>
78*0Sstevel@tonic-gate #include <stdio.h>
79*0Sstevel@tonic-gate #include <stdlib.h>
80*0Sstevel@tonic-gate #include <string.h>
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate #include <irs.h>
83*0Sstevel@tonic-gate #include <isc/memcluster.h>
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate #include "port_after.h"
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate #include <isc/misc.h>
88*0Sstevel@tonic-gate #include "irs_p.h"
89*0Sstevel@tonic-gate #include "lcl_p.h"
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate #define MAXALIASES 35
92*0Sstevel@tonic-gate #define MAXADDRSIZE 4
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate struct pvt {
95*0Sstevel@tonic-gate 	FILE *		fp;
96*0Sstevel@tonic-gate 	char 		line[BUFSIZ+1];
97*0Sstevel@tonic-gate 	struct nwent 	net;
98*0Sstevel@tonic-gate 	char *		aliases[MAXALIASES];
99*0Sstevel@tonic-gate 	char		addr[MAXADDRSIZE];
100*0Sstevel@tonic-gate 	struct __res_state *  res;
101*0Sstevel@tonic-gate 	void		(*free_res)(void *);
102*0Sstevel@tonic-gate };
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate /* Forward */
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate static void 		nw_close(struct irs_nw *);
107*0Sstevel@tonic-gate static struct nwent *	nw_byname(struct irs_nw *, const char *, int);
108*0Sstevel@tonic-gate static struct nwent *	nw_byaddr(struct irs_nw *, void *, int, int);
109*0Sstevel@tonic-gate static struct nwent *	nw_next(struct irs_nw *);
110*0Sstevel@tonic-gate static void		nw_rewind(struct irs_nw *);
111*0Sstevel@tonic-gate static void		nw_minimize(struct irs_nw *);
112*0Sstevel@tonic-gate static struct __res_state * nw_res_get(struct irs_nw *this);
113*0Sstevel@tonic-gate static void		nw_res_set(struct irs_nw *this,
114*0Sstevel@tonic-gate 				   struct __res_state *res,
115*0Sstevel@tonic-gate 				   void (*free_res)(void *));
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate static int		init(struct irs_nw *this);
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate /* Portability. */
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate #ifndef SEEK_SET
122*0Sstevel@tonic-gate # define SEEK_SET 0
123*0Sstevel@tonic-gate #endif
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate /* Public */
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate struct irs_nw *
128*0Sstevel@tonic-gate irs_lcl_nw(struct irs_acc *this) {
129*0Sstevel@tonic-gate 	struct irs_nw *nw;
130*0Sstevel@tonic-gate 	struct pvt *pvt;
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 	UNUSED(this);
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 	if (!(pvt = memget(sizeof *pvt))) {
135*0Sstevel@tonic-gate 		errno = ENOMEM;
136*0Sstevel@tonic-gate 		return (NULL);
137*0Sstevel@tonic-gate 	}
138*0Sstevel@tonic-gate 	memset(pvt, 0, sizeof *pvt);
139*0Sstevel@tonic-gate 	if (!(nw = memget(sizeof *nw))) {
140*0Sstevel@tonic-gate 		memput(pvt, sizeof *pvt);
141*0Sstevel@tonic-gate 		errno = ENOMEM;
142*0Sstevel@tonic-gate 		return (NULL);
143*0Sstevel@tonic-gate 	}
144*0Sstevel@tonic-gate 	memset(nw, 0x5e, sizeof *nw);
145*0Sstevel@tonic-gate 	nw->private = pvt;
146*0Sstevel@tonic-gate 	nw->close = nw_close;
147*0Sstevel@tonic-gate 	nw->byname = nw_byname;
148*0Sstevel@tonic-gate 	nw->byaddr = nw_byaddr;
149*0Sstevel@tonic-gate 	nw->next = nw_next;
150*0Sstevel@tonic-gate 	nw->rewind = nw_rewind;
151*0Sstevel@tonic-gate 	nw->minimize = nw_minimize;
152*0Sstevel@tonic-gate 	nw->res_get = nw_res_get;
153*0Sstevel@tonic-gate 	nw->res_set = nw_res_set;
154*0Sstevel@tonic-gate 	return (nw);
155*0Sstevel@tonic-gate }
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate /* Methods */
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate static void
160*0Sstevel@tonic-gate nw_close(struct irs_nw *this) {
161*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate 	nw_minimize(this);
164*0Sstevel@tonic-gate 	if (pvt->res && pvt->free_res)
165*0Sstevel@tonic-gate 		(*pvt->free_res)(pvt->res);
166*0Sstevel@tonic-gate 	if (pvt->fp)
167*0Sstevel@tonic-gate 		(void)fclose(pvt->fp);
168*0Sstevel@tonic-gate 	memput(pvt, sizeof *pvt);
169*0Sstevel@tonic-gate 	memput(this, sizeof *this);
170*0Sstevel@tonic-gate }
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate static struct nwent *
173*0Sstevel@tonic-gate nw_byaddr(struct irs_nw *this, void *net, int length, int type) {
174*0Sstevel@tonic-gate 	struct nwent *p;
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate 	if (init(this) == -1)
177*0Sstevel@tonic-gate 		return(NULL);
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate 	nw_rewind(this);
180*0Sstevel@tonic-gate 	while ((p = nw_next(this)) != NULL)
181*0Sstevel@tonic-gate 		if (p->n_addrtype == type && p->n_length == length)
182*0Sstevel@tonic-gate 			if (bitncmp(p->n_addr, net, length) == 0)
183*0Sstevel@tonic-gate 				break;
184*0Sstevel@tonic-gate 	return (p);
185*0Sstevel@tonic-gate }
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate static struct nwent *
188*0Sstevel@tonic-gate nw_byname(struct irs_nw *this, const char *name, int type) {
189*0Sstevel@tonic-gate 	struct nwent *p;
190*0Sstevel@tonic-gate 	char **ap;
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 	if (init(this) == -1)
193*0Sstevel@tonic-gate 		return(NULL);
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 	nw_rewind(this);
196*0Sstevel@tonic-gate 	while ((p = nw_next(this)) != NULL) {
197*0Sstevel@tonic-gate 		if (ns_samename(p->n_name, name) == 1 &&
198*0Sstevel@tonic-gate 		    p->n_addrtype == type)
199*0Sstevel@tonic-gate 			break;
200*0Sstevel@tonic-gate 		for (ap = p->n_aliases; *ap; ap++)
201*0Sstevel@tonic-gate 			if ((ns_samename(*ap, name) == 1) &&
202*0Sstevel@tonic-gate 			    (p->n_addrtype == type))
203*0Sstevel@tonic-gate 				goto found;
204*0Sstevel@tonic-gate 	}
205*0Sstevel@tonic-gate  found:
206*0Sstevel@tonic-gate 	return (p);
207*0Sstevel@tonic-gate }
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate static void
210*0Sstevel@tonic-gate nw_rewind(struct irs_nw *this) {
211*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate 	if (pvt->fp) {
214*0Sstevel@tonic-gate 		if (fseek(pvt->fp, 0L, SEEK_SET) == 0)
215*0Sstevel@tonic-gate 			return;
216*0Sstevel@tonic-gate 		(void)fclose(pvt->fp);
217*0Sstevel@tonic-gate 	}
218*0Sstevel@tonic-gate 	if (!(pvt->fp = fopen(_PATH_NETWORKS, "r")))
219*0Sstevel@tonic-gate 		return;
220*0Sstevel@tonic-gate 	if (fcntl(fileno(pvt->fp), F_SETFD, 1) < 0) {
221*0Sstevel@tonic-gate 		(void)fclose(pvt->fp);
222*0Sstevel@tonic-gate 		pvt->fp = NULL;
223*0Sstevel@tonic-gate 	}
224*0Sstevel@tonic-gate }
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate static struct nwent *
227*0Sstevel@tonic-gate nw_next(struct irs_nw *this) {
228*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
229*0Sstevel@tonic-gate 	struct nwent *ret = NULL;
230*0Sstevel@tonic-gate 	char *p, *cp, **q;
231*0Sstevel@tonic-gate 	char *bufp, *ndbuf, *dbuf = NULL;
232*0Sstevel@tonic-gate 	int c, bufsiz, offset = 0;
233*0Sstevel@tonic-gate 
234*0Sstevel@tonic-gate 	if (init(this) == -1)
235*0Sstevel@tonic-gate 		return(NULL);
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate 	if (pvt->fp == NULL)
238*0Sstevel@tonic-gate 		nw_rewind(this);
239*0Sstevel@tonic-gate 	if (pvt->fp == NULL) {
240*0Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
241*0Sstevel@tonic-gate 		return (NULL);
242*0Sstevel@tonic-gate 	}
243*0Sstevel@tonic-gate 	bufp = pvt->line;
244*0Sstevel@tonic-gate 	bufsiz = sizeof(pvt->line);
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate  again:
247*0Sstevel@tonic-gate 	p = fgets(bufp + offset, bufsiz - offset, pvt->fp);
248*0Sstevel@tonic-gate 	if (p == NULL)
249*0Sstevel@tonic-gate 		goto cleanup;
250*0Sstevel@tonic-gate 	if (!strchr(p, '\n') && !feof(pvt->fp)) {
251*0Sstevel@tonic-gate #define GROWBUF 1024
252*0Sstevel@tonic-gate 		/* allocate space for longer line */
253*0Sstevel@tonic-gate 	  	if (dbuf == NULL) {
254*0Sstevel@tonic-gate 			if ((ndbuf = malloc(bufsiz + GROWBUF)) != NULL)
255*0Sstevel@tonic-gate 				strcpy(ndbuf, bufp);
256*0Sstevel@tonic-gate 		} else
257*0Sstevel@tonic-gate 			ndbuf = realloc(dbuf, bufsiz + GROWBUF);
258*0Sstevel@tonic-gate 		if (ndbuf) {
259*0Sstevel@tonic-gate 			dbuf = ndbuf;
260*0Sstevel@tonic-gate 			bufp = dbuf;
261*0Sstevel@tonic-gate 			bufsiz += GROWBUF;
262*0Sstevel@tonic-gate 			offset = strlen(dbuf);
263*0Sstevel@tonic-gate 		} else {
264*0Sstevel@tonic-gate 			/* allocation failed; skip this long line */
265*0Sstevel@tonic-gate 			while ((c = getc(pvt->fp)) != EOF)
266*0Sstevel@tonic-gate 				if (c == '\n')
267*0Sstevel@tonic-gate 					break;
268*0Sstevel@tonic-gate 			if (c != EOF)
269*0Sstevel@tonic-gate 				ungetc(c, pvt->fp);
270*0Sstevel@tonic-gate 		}
271*0Sstevel@tonic-gate 		goto again;
272*0Sstevel@tonic-gate 	}
273*0Sstevel@tonic-gate 
274*0Sstevel@tonic-gate 	p -= offset;
275*0Sstevel@tonic-gate 	offset = 0;
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate 	if (*p == '#')
278*0Sstevel@tonic-gate 		goto again;
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate 	cp = strpbrk(p, "#\n");
281*0Sstevel@tonic-gate 	if (cp != NULL)
282*0Sstevel@tonic-gate 		*cp = '\0';
283*0Sstevel@tonic-gate 	pvt->net.n_name = p;
284*0Sstevel@tonic-gate 	cp = strpbrk(p, " \t");
285*0Sstevel@tonic-gate 	if (cp == NULL)
286*0Sstevel@tonic-gate 		goto again;
287*0Sstevel@tonic-gate 	*cp++ = '\0';
288*0Sstevel@tonic-gate 	while (*cp == ' ' || *cp == '\t')
289*0Sstevel@tonic-gate 		cp++;
290*0Sstevel@tonic-gate 	p = strpbrk(cp, " \t");
291*0Sstevel@tonic-gate 	if (p != NULL)
292*0Sstevel@tonic-gate 		*p++ = '\0';
293*0Sstevel@tonic-gate 	pvt->net.n_length = inet_net_pton(AF_INET, cp, pvt->addr,
294*0Sstevel@tonic-gate 					  sizeof pvt->addr);
295*0Sstevel@tonic-gate 	if (pvt->net.n_length < 0)
296*0Sstevel@tonic-gate 		goto again;
297*0Sstevel@tonic-gate 	pvt->net.n_addrtype = AF_INET;
298*0Sstevel@tonic-gate 	pvt->net.n_addr = pvt->addr;
299*0Sstevel@tonic-gate 	q = pvt->net.n_aliases = pvt->aliases;
300*0Sstevel@tonic-gate 	if (p != NULL) {
301*0Sstevel@tonic-gate 		cp = p;
302*0Sstevel@tonic-gate 		while (cp && *cp) {
303*0Sstevel@tonic-gate 			if (*cp == ' ' || *cp == '\t') {
304*0Sstevel@tonic-gate 				cp++;
305*0Sstevel@tonic-gate 				continue;
306*0Sstevel@tonic-gate 			}
307*0Sstevel@tonic-gate 			if (q < &pvt->aliases[MAXALIASES - 1])
308*0Sstevel@tonic-gate 				*q++ = cp;
309*0Sstevel@tonic-gate 			cp = strpbrk(cp, " \t");
310*0Sstevel@tonic-gate 			if (cp != NULL)
311*0Sstevel@tonic-gate 				*cp++ = '\0';
312*0Sstevel@tonic-gate 		}
313*0Sstevel@tonic-gate 	}
314*0Sstevel@tonic-gate 	*q = NULL;
315*0Sstevel@tonic-gate 	ret = &pvt->net;
316*0Sstevel@tonic-gate 
317*0Sstevel@tonic-gate  cleanup:
318*0Sstevel@tonic-gate 	if (dbuf)
319*0Sstevel@tonic-gate 		free(dbuf);
320*0Sstevel@tonic-gate 
321*0Sstevel@tonic-gate 	return (ret);
322*0Sstevel@tonic-gate }
323*0Sstevel@tonic-gate 
324*0Sstevel@tonic-gate static void
325*0Sstevel@tonic-gate nw_minimize(struct irs_nw *this) {
326*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate 	if (pvt->res)
329*0Sstevel@tonic-gate 		res_nclose(pvt->res);
330*0Sstevel@tonic-gate 	if (pvt->fp != NULL) {
331*0Sstevel@tonic-gate 		(void)fclose(pvt->fp);
332*0Sstevel@tonic-gate 		pvt->fp = NULL;
333*0Sstevel@tonic-gate 	}
334*0Sstevel@tonic-gate }
335*0Sstevel@tonic-gate 
336*0Sstevel@tonic-gate static struct __res_state *
337*0Sstevel@tonic-gate nw_res_get(struct irs_nw *this) {
338*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
339*0Sstevel@tonic-gate 
340*0Sstevel@tonic-gate 	if (!pvt->res) {
341*0Sstevel@tonic-gate 		struct __res_state *res;
342*0Sstevel@tonic-gate 		res = (struct __res_state *)malloc(sizeof *res);
343*0Sstevel@tonic-gate 		if (!res) {
344*0Sstevel@tonic-gate 			errno = ENOMEM;
345*0Sstevel@tonic-gate 			return (NULL);
346*0Sstevel@tonic-gate 		}
347*0Sstevel@tonic-gate 		memset(res, 0, sizeof *res);
348*0Sstevel@tonic-gate 		nw_res_set(this, res, free);
349*0Sstevel@tonic-gate 	}
350*0Sstevel@tonic-gate 
351*0Sstevel@tonic-gate 	return (pvt->res);
352*0Sstevel@tonic-gate }
353*0Sstevel@tonic-gate 
354*0Sstevel@tonic-gate static void
355*0Sstevel@tonic-gate nw_res_set(struct irs_nw *this, struct __res_state *res,
356*0Sstevel@tonic-gate 		void (*free_res)(void *)) {
357*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate 	if (pvt->res && pvt->free_res) {
360*0Sstevel@tonic-gate 		res_nclose(pvt->res);
361*0Sstevel@tonic-gate 		(*pvt->free_res)(pvt->res);
362*0Sstevel@tonic-gate 	}
363*0Sstevel@tonic-gate 
364*0Sstevel@tonic-gate 	pvt->res = res;
365*0Sstevel@tonic-gate 	pvt->free_res = free_res;
366*0Sstevel@tonic-gate }
367*0Sstevel@tonic-gate 
368*0Sstevel@tonic-gate static int
369*0Sstevel@tonic-gate init(struct irs_nw *this) {
370*0Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate 	if (!pvt->res && !nw_res_get(this))
373*0Sstevel@tonic-gate 		return (-1);
374*0Sstevel@tonic-gate 	if (((pvt->res->options & RES_INIT) == 0) &&
375*0Sstevel@tonic-gate 	    res_ninit(pvt->res) == -1)
376*0Sstevel@tonic-gate 		return (-1);
377*0Sstevel@tonic-gate 	return (0);
378*0Sstevel@tonic-gate }
379