17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * Copyright (c) 1989, 1993, 1995
37c478bd9Sstevel@tonic-gate * The Regents of the University of California. All rights reserved.
47c478bd9Sstevel@tonic-gate *
57c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
67c478bd9Sstevel@tonic-gate * modification, are permitted provided that the following conditions
77c478bd9Sstevel@tonic-gate * are met:
87c478bd9Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
97c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
107c478bd9Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
117c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
127c478bd9Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
137c478bd9Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
147c478bd9Sstevel@tonic-gate * must display the following acknowledgement:
157c478bd9Sstevel@tonic-gate * This product includes software developed by the University of
167c478bd9Sstevel@tonic-gate * California, Berkeley and its contributors.
177c478bd9Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors
187c478bd9Sstevel@tonic-gate * may be used to endorse or promote products derived from this software
197c478bd9Sstevel@tonic-gate * without specific prior written permission.
207c478bd9Sstevel@tonic-gate *
217c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
227c478bd9Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
237c478bd9Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
247c478bd9Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
257c478bd9Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
267c478bd9Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
277c478bd9Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
287c478bd9Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
297c478bd9Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
307c478bd9Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
317c478bd9Sstevel@tonic-gate * SUCH DAMAGE.
327c478bd9Sstevel@tonic-gate */
337c478bd9Sstevel@tonic-gate
347c478bd9Sstevel@tonic-gate /*
35*9525b14bSRao Shoaib * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
367c478bd9Sstevel@tonic-gate * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
377c478bd9Sstevel@tonic-gate *
387c478bd9Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any
397c478bd9Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above
407c478bd9Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies.
417c478bd9Sstevel@tonic-gate *
42*9525b14bSRao Shoaib * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
43*9525b14bSRao Shoaib * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
44*9525b14bSRao Shoaib * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
45*9525b14bSRao Shoaib * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
46*9525b14bSRao Shoaib * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
47*9525b14bSRao Shoaib * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
48*9525b14bSRao Shoaib * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
497c478bd9Sstevel@tonic-gate */
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
52*9525b14bSRao Shoaib static const char rcsid[] = "$Id: lcl_nw.c,v 1.4 2005/04/27 04:56:31 sra Exp $";
537c478bd9Sstevel@tonic-gate /* from getgrent.c 8.2 (Berkeley) 3/21/94"; */
547c478bd9Sstevel@tonic-gate /* from BSDI Id: getgrent.c,v 2.8 1996/05/28 18:15:14 bostic Exp $ */
557c478bd9Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate /* Imports */
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate #include "port_before.h"
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate #include <sys/types.h>
627c478bd9Sstevel@tonic-gate #include <sys/socket.h>
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate #include <netinet/in.h>
657c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
667c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate #include <errno.h>
697c478bd9Sstevel@tonic-gate #include <fcntl.h>
707c478bd9Sstevel@tonic-gate #include <resolv.h>
717c478bd9Sstevel@tonic-gate #include <stdio.h>
727c478bd9Sstevel@tonic-gate #include <stdlib.h>
737c478bd9Sstevel@tonic-gate #include <string.h>
747c478bd9Sstevel@tonic-gate
757c478bd9Sstevel@tonic-gate #include <irs.h>
767c478bd9Sstevel@tonic-gate #include <isc/memcluster.h>
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate #include "port_after.h"
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate #include <isc/misc.h>
817c478bd9Sstevel@tonic-gate #include "irs_p.h"
827c478bd9Sstevel@tonic-gate #include "lcl_p.h"
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate #define MAXALIASES 35
857c478bd9Sstevel@tonic-gate #define MAXADDRSIZE 4
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate struct pvt {
887c478bd9Sstevel@tonic-gate FILE * fp;
897c478bd9Sstevel@tonic-gate char line[BUFSIZ+1];
907c478bd9Sstevel@tonic-gate struct nwent net;
917c478bd9Sstevel@tonic-gate char * aliases[MAXALIASES];
927c478bd9Sstevel@tonic-gate char addr[MAXADDRSIZE];
937c478bd9Sstevel@tonic-gate struct __res_state * res;
947c478bd9Sstevel@tonic-gate void (*free_res)(void *);
957c478bd9Sstevel@tonic-gate };
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gate /* Forward */
987c478bd9Sstevel@tonic-gate
997c478bd9Sstevel@tonic-gate static void nw_close(struct irs_nw *);
1007c478bd9Sstevel@tonic-gate static struct nwent * nw_byname(struct irs_nw *, const char *, int);
1017c478bd9Sstevel@tonic-gate static struct nwent * nw_byaddr(struct irs_nw *, void *, int, int);
1027c478bd9Sstevel@tonic-gate static struct nwent * nw_next(struct irs_nw *);
1037c478bd9Sstevel@tonic-gate static void nw_rewind(struct irs_nw *);
1047c478bd9Sstevel@tonic-gate static void nw_minimize(struct irs_nw *);
1057c478bd9Sstevel@tonic-gate static struct __res_state * nw_res_get(struct irs_nw *this);
1067c478bd9Sstevel@tonic-gate static void nw_res_set(struct irs_nw *this,
1077c478bd9Sstevel@tonic-gate struct __res_state *res,
1087c478bd9Sstevel@tonic-gate void (*free_res)(void *));
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate static int init(struct irs_nw *this);
1117c478bd9Sstevel@tonic-gate
1127c478bd9Sstevel@tonic-gate /* Portability. */
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate #ifndef SEEK_SET
1157c478bd9Sstevel@tonic-gate # define SEEK_SET 0
1167c478bd9Sstevel@tonic-gate #endif
1177c478bd9Sstevel@tonic-gate
1187c478bd9Sstevel@tonic-gate /* Public */
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate struct irs_nw *
irs_lcl_nw(struct irs_acc * this)1217c478bd9Sstevel@tonic-gate irs_lcl_nw(struct irs_acc *this) {
1227c478bd9Sstevel@tonic-gate struct irs_nw *nw;
1237c478bd9Sstevel@tonic-gate struct pvt *pvt;
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate UNUSED(this);
1267c478bd9Sstevel@tonic-gate
1277c478bd9Sstevel@tonic-gate if (!(pvt = memget(sizeof *pvt))) {
1287c478bd9Sstevel@tonic-gate errno = ENOMEM;
1297c478bd9Sstevel@tonic-gate return (NULL);
1307c478bd9Sstevel@tonic-gate }
1317c478bd9Sstevel@tonic-gate memset(pvt, 0, sizeof *pvt);
1327c478bd9Sstevel@tonic-gate if (!(nw = memget(sizeof *nw))) {
1337c478bd9Sstevel@tonic-gate memput(pvt, sizeof *pvt);
1347c478bd9Sstevel@tonic-gate errno = ENOMEM;
1357c478bd9Sstevel@tonic-gate return (NULL);
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate memset(nw, 0x5e, sizeof *nw);
1387c478bd9Sstevel@tonic-gate nw->private = pvt;
1397c478bd9Sstevel@tonic-gate nw->close = nw_close;
1407c478bd9Sstevel@tonic-gate nw->byname = nw_byname;
1417c478bd9Sstevel@tonic-gate nw->byaddr = nw_byaddr;
1427c478bd9Sstevel@tonic-gate nw->next = nw_next;
1437c478bd9Sstevel@tonic-gate nw->rewind = nw_rewind;
1447c478bd9Sstevel@tonic-gate nw->minimize = nw_minimize;
1457c478bd9Sstevel@tonic-gate nw->res_get = nw_res_get;
1467c478bd9Sstevel@tonic-gate nw->res_set = nw_res_set;
1477c478bd9Sstevel@tonic-gate return (nw);
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate /* Methods */
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate static void
nw_close(struct irs_nw * this)1537c478bd9Sstevel@tonic-gate nw_close(struct irs_nw *this) {
1547c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate nw_minimize(this);
1577c478bd9Sstevel@tonic-gate if (pvt->res && pvt->free_res)
1587c478bd9Sstevel@tonic-gate (*pvt->free_res)(pvt->res);
1597c478bd9Sstevel@tonic-gate if (pvt->fp)
1607c478bd9Sstevel@tonic-gate (void)fclose(pvt->fp);
1617c478bd9Sstevel@tonic-gate memput(pvt, sizeof *pvt);
1627c478bd9Sstevel@tonic-gate memput(this, sizeof *this);
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate static struct nwent *
nw_byaddr(struct irs_nw * this,void * net,int length,int type)1667c478bd9Sstevel@tonic-gate nw_byaddr(struct irs_nw *this, void *net, int length, int type) {
1677c478bd9Sstevel@tonic-gate struct nwent *p;
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate if (init(this) == -1)
1707c478bd9Sstevel@tonic-gate return(NULL);
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate nw_rewind(this);
1737c478bd9Sstevel@tonic-gate while ((p = nw_next(this)) != NULL)
1747c478bd9Sstevel@tonic-gate if (p->n_addrtype == type && p->n_length == length)
1757c478bd9Sstevel@tonic-gate if (bitncmp(p->n_addr, net, length) == 0)
1767c478bd9Sstevel@tonic-gate break;
1777c478bd9Sstevel@tonic-gate return (p);
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gate static struct nwent *
nw_byname(struct irs_nw * this,const char * name,int type)1817c478bd9Sstevel@tonic-gate nw_byname(struct irs_nw *this, const char *name, int type) {
1827c478bd9Sstevel@tonic-gate struct nwent *p;
1837c478bd9Sstevel@tonic-gate char **ap;
1847c478bd9Sstevel@tonic-gate
1857c478bd9Sstevel@tonic-gate if (init(this) == -1)
1867c478bd9Sstevel@tonic-gate return(NULL);
1877c478bd9Sstevel@tonic-gate
1887c478bd9Sstevel@tonic-gate nw_rewind(this);
1897c478bd9Sstevel@tonic-gate while ((p = nw_next(this)) != NULL) {
1907c478bd9Sstevel@tonic-gate if (ns_samename(p->n_name, name) == 1 &&
1917c478bd9Sstevel@tonic-gate p->n_addrtype == type)
1927c478bd9Sstevel@tonic-gate break;
1937c478bd9Sstevel@tonic-gate for (ap = p->n_aliases; *ap; ap++)
1947c478bd9Sstevel@tonic-gate if ((ns_samename(*ap, name) == 1) &&
1957c478bd9Sstevel@tonic-gate (p->n_addrtype == type))
1967c478bd9Sstevel@tonic-gate goto found;
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate found:
1997c478bd9Sstevel@tonic-gate return (p);
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate
2027c478bd9Sstevel@tonic-gate static void
nw_rewind(struct irs_nw * this)2037c478bd9Sstevel@tonic-gate nw_rewind(struct irs_nw *this) {
2047c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate if (pvt->fp) {
2077c478bd9Sstevel@tonic-gate if (fseek(pvt->fp, 0L, SEEK_SET) == 0)
2087c478bd9Sstevel@tonic-gate return;
2097c478bd9Sstevel@tonic-gate (void)fclose(pvt->fp);
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate if (!(pvt->fp = fopen(_PATH_NETWORKS, "r")))
2127c478bd9Sstevel@tonic-gate return;
2137c478bd9Sstevel@tonic-gate if (fcntl(fileno(pvt->fp), F_SETFD, 1) < 0) {
2147c478bd9Sstevel@tonic-gate (void)fclose(pvt->fp);
2157c478bd9Sstevel@tonic-gate pvt->fp = NULL;
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate
2197c478bd9Sstevel@tonic-gate static struct nwent *
nw_next(struct irs_nw * this)2207c478bd9Sstevel@tonic-gate nw_next(struct irs_nw *this) {
2217c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
2227c478bd9Sstevel@tonic-gate struct nwent *ret = NULL;
2237c478bd9Sstevel@tonic-gate char *p, *cp, **q;
2247c478bd9Sstevel@tonic-gate char *bufp, *ndbuf, *dbuf = NULL;
2257c478bd9Sstevel@tonic-gate int c, bufsiz, offset = 0;
2267c478bd9Sstevel@tonic-gate
2277c478bd9Sstevel@tonic-gate if (init(this) == -1)
2287c478bd9Sstevel@tonic-gate return(NULL);
2297c478bd9Sstevel@tonic-gate
2307c478bd9Sstevel@tonic-gate if (pvt->fp == NULL)
2317c478bd9Sstevel@tonic-gate nw_rewind(this);
2327c478bd9Sstevel@tonic-gate if (pvt->fp == NULL) {
2337c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
2347c478bd9Sstevel@tonic-gate return (NULL);
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate bufp = pvt->line;
2377c478bd9Sstevel@tonic-gate bufsiz = sizeof(pvt->line);
2387c478bd9Sstevel@tonic-gate
2397c478bd9Sstevel@tonic-gate again:
2407c478bd9Sstevel@tonic-gate p = fgets(bufp + offset, bufsiz - offset, pvt->fp);
2417c478bd9Sstevel@tonic-gate if (p == NULL)
2427c478bd9Sstevel@tonic-gate goto cleanup;
2437c478bd9Sstevel@tonic-gate if (!strchr(p, '\n') && !feof(pvt->fp)) {
2447c478bd9Sstevel@tonic-gate #define GROWBUF 1024
2457c478bd9Sstevel@tonic-gate /* allocate space for longer line */
2467c478bd9Sstevel@tonic-gate if (dbuf == NULL) {
2477c478bd9Sstevel@tonic-gate if ((ndbuf = malloc(bufsiz + GROWBUF)) != NULL)
2487c478bd9Sstevel@tonic-gate strcpy(ndbuf, bufp);
2497c478bd9Sstevel@tonic-gate } else
2507c478bd9Sstevel@tonic-gate ndbuf = realloc(dbuf, bufsiz + GROWBUF);
2517c478bd9Sstevel@tonic-gate if (ndbuf) {
2527c478bd9Sstevel@tonic-gate dbuf = ndbuf;
2537c478bd9Sstevel@tonic-gate bufp = dbuf;
2547c478bd9Sstevel@tonic-gate bufsiz += GROWBUF;
2557c478bd9Sstevel@tonic-gate offset = strlen(dbuf);
2567c478bd9Sstevel@tonic-gate } else {
2577c478bd9Sstevel@tonic-gate /* allocation failed; skip this long line */
2587c478bd9Sstevel@tonic-gate while ((c = getc(pvt->fp)) != EOF)
2597c478bd9Sstevel@tonic-gate if (c == '\n')
2607c478bd9Sstevel@tonic-gate break;
2617c478bd9Sstevel@tonic-gate if (c != EOF)
2627c478bd9Sstevel@tonic-gate ungetc(c, pvt->fp);
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate goto again;
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate
2677c478bd9Sstevel@tonic-gate p -= offset;
2687c478bd9Sstevel@tonic-gate offset = 0;
2697c478bd9Sstevel@tonic-gate
2707c478bd9Sstevel@tonic-gate if (*p == '#')
2717c478bd9Sstevel@tonic-gate goto again;
2727c478bd9Sstevel@tonic-gate
2737c478bd9Sstevel@tonic-gate cp = strpbrk(p, "#\n");
2747c478bd9Sstevel@tonic-gate if (cp != NULL)
2757c478bd9Sstevel@tonic-gate *cp = '\0';
2767c478bd9Sstevel@tonic-gate pvt->net.n_name = p;
2777c478bd9Sstevel@tonic-gate cp = strpbrk(p, " \t");
2787c478bd9Sstevel@tonic-gate if (cp == NULL)
2797c478bd9Sstevel@tonic-gate goto again;
2807c478bd9Sstevel@tonic-gate *cp++ = '\0';
2817c478bd9Sstevel@tonic-gate while (*cp == ' ' || *cp == '\t')
2827c478bd9Sstevel@tonic-gate cp++;
2837c478bd9Sstevel@tonic-gate p = strpbrk(cp, " \t");
2847c478bd9Sstevel@tonic-gate if (p != NULL)
2857c478bd9Sstevel@tonic-gate *p++ = '\0';
2867c478bd9Sstevel@tonic-gate pvt->net.n_length = inet_net_pton(AF_INET, cp, pvt->addr,
2877c478bd9Sstevel@tonic-gate sizeof pvt->addr);
2887c478bd9Sstevel@tonic-gate if (pvt->net.n_length < 0)
2897c478bd9Sstevel@tonic-gate goto again;
2907c478bd9Sstevel@tonic-gate pvt->net.n_addrtype = AF_INET;
2917c478bd9Sstevel@tonic-gate pvt->net.n_addr = pvt->addr;
2927c478bd9Sstevel@tonic-gate q = pvt->net.n_aliases = pvt->aliases;
2937c478bd9Sstevel@tonic-gate if (p != NULL) {
2947c478bd9Sstevel@tonic-gate cp = p;
2957c478bd9Sstevel@tonic-gate while (cp && *cp) {
2967c478bd9Sstevel@tonic-gate if (*cp == ' ' || *cp == '\t') {
2977c478bd9Sstevel@tonic-gate cp++;
2987c478bd9Sstevel@tonic-gate continue;
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate if (q < &pvt->aliases[MAXALIASES - 1])
3017c478bd9Sstevel@tonic-gate *q++ = cp;
3027c478bd9Sstevel@tonic-gate cp = strpbrk(cp, " \t");
3037c478bd9Sstevel@tonic-gate if (cp != NULL)
3047c478bd9Sstevel@tonic-gate *cp++ = '\0';
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate *q = NULL;
3087c478bd9Sstevel@tonic-gate ret = &pvt->net;
3097c478bd9Sstevel@tonic-gate
3107c478bd9Sstevel@tonic-gate cleanup:
3117c478bd9Sstevel@tonic-gate if (dbuf)
3127c478bd9Sstevel@tonic-gate free(dbuf);
3137c478bd9Sstevel@tonic-gate
3147c478bd9Sstevel@tonic-gate return (ret);
3157c478bd9Sstevel@tonic-gate }
3167c478bd9Sstevel@tonic-gate
3177c478bd9Sstevel@tonic-gate static void
nw_minimize(struct irs_nw * this)3187c478bd9Sstevel@tonic-gate nw_minimize(struct irs_nw *this) {
3197c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
3207c478bd9Sstevel@tonic-gate
3217c478bd9Sstevel@tonic-gate if (pvt->res)
3227c478bd9Sstevel@tonic-gate res_nclose(pvt->res);
3237c478bd9Sstevel@tonic-gate if (pvt->fp != NULL) {
3247c478bd9Sstevel@tonic-gate (void)fclose(pvt->fp);
3257c478bd9Sstevel@tonic-gate pvt->fp = NULL;
3267c478bd9Sstevel@tonic-gate }
3277c478bd9Sstevel@tonic-gate }
3287c478bd9Sstevel@tonic-gate
3297c478bd9Sstevel@tonic-gate static struct __res_state *
nw_res_get(struct irs_nw * this)3307c478bd9Sstevel@tonic-gate nw_res_get(struct irs_nw *this) {
3317c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
3327c478bd9Sstevel@tonic-gate
3337c478bd9Sstevel@tonic-gate if (!pvt->res) {
3347c478bd9Sstevel@tonic-gate struct __res_state *res;
3357c478bd9Sstevel@tonic-gate res = (struct __res_state *)malloc(sizeof *res);
3367c478bd9Sstevel@tonic-gate if (!res) {
3377c478bd9Sstevel@tonic-gate errno = ENOMEM;
3387c478bd9Sstevel@tonic-gate return (NULL);
3397c478bd9Sstevel@tonic-gate }
3407c478bd9Sstevel@tonic-gate memset(res, 0, sizeof *res);
3417c478bd9Sstevel@tonic-gate nw_res_set(this, res, free);
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate
3447c478bd9Sstevel@tonic-gate return (pvt->res);
3457c478bd9Sstevel@tonic-gate }
3467c478bd9Sstevel@tonic-gate
3477c478bd9Sstevel@tonic-gate static void
nw_res_set(struct irs_nw * this,struct __res_state * res,void (* free_res)(void *))3487c478bd9Sstevel@tonic-gate nw_res_set(struct irs_nw *this, struct __res_state *res,
3497c478bd9Sstevel@tonic-gate void (*free_res)(void *)) {
3507c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
3517c478bd9Sstevel@tonic-gate
3527c478bd9Sstevel@tonic-gate if (pvt->res && pvt->free_res) {
3537c478bd9Sstevel@tonic-gate res_nclose(pvt->res);
3547c478bd9Sstevel@tonic-gate (*pvt->free_res)(pvt->res);
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate
3577c478bd9Sstevel@tonic-gate pvt->res = res;
3587c478bd9Sstevel@tonic-gate pvt->free_res = free_res;
3597c478bd9Sstevel@tonic-gate }
3607c478bd9Sstevel@tonic-gate
3617c478bd9Sstevel@tonic-gate static int
init(struct irs_nw * this)3627c478bd9Sstevel@tonic-gate init(struct irs_nw *this) {
3637c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
3647c478bd9Sstevel@tonic-gate
3657c478bd9Sstevel@tonic-gate if (!pvt->res && !nw_res_get(this))
3667c478bd9Sstevel@tonic-gate return (-1);
367*9525b14bSRao Shoaib if (((pvt->res->options & RES_INIT) == 0U) &&
3687c478bd9Sstevel@tonic-gate res_ninit(pvt->res) == -1)
3697c478bd9Sstevel@tonic-gate return (-1);
3707c478bd9Sstevel@tonic-gate return (0);
3717c478bd9Sstevel@tonic-gate }
372*9525b14bSRao Shoaib
373*9525b14bSRao Shoaib /*! \file */
374