4 * Copyright (c) 1989, 1993, 1995
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
38 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
40 * Permission to use, copy, modify, and distribute this software for any
41 * purpose with or without fee is hereby granted, provided that the above
42 * copyright notice and this permission notice appear in all copies.
44 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
45 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
46 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
47 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
48 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
49 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
50 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
53 #if defined(LIBC_SCCS) && !defined(lint)
54 static const char rcsid
[] = "Id: lcl_nw.c,v 1.4 2005/04/27 04:56:31 sra Exp";
55 /* from getgrent.c 8.2 (Berkeley) 3/21/94"; */
56 /* from BSDI Id: getgrent.c,v 2.8 1996/05/28 18:15:14 bostic Exp $ */
57 #endif /* LIBC_SCCS and not lint */
61 #include "port_before.h"
63 #include <sys/types.h>
64 #include <sys/socket.h>
66 #include <netinet/in.h>
67 #include <arpa/inet.h>
68 #include <arpa/nameser.h>
78 #include <isc/memcluster.h>
80 #include "port_after.h"
93 char * aliases
[MAXALIASES
];
94 char addr
[MAXADDRSIZE
];
95 struct __res_state
* res
;
96 void (*free_res
)(void *);
101 static void nw_close(struct irs_nw
*);
102 static struct nwent
* nw_byname(struct irs_nw
*, const char *, int);
103 static struct nwent
* nw_byaddr(struct irs_nw
*, void *, int, int);
104 static struct nwent
* nw_next(struct irs_nw
*);
105 static void nw_rewind(struct irs_nw
*);
106 static void nw_minimize(struct irs_nw
*);
107 static struct __res_state
* nw_res_get(struct irs_nw
*this);
108 static void nw_res_set(struct irs_nw
*this,
109 struct __res_state
*res
,
110 void (*free_res
)(void *));
112 static int init(struct irs_nw
*this);
123 irs_lcl_nw(struct irs_acc
*this) {
129 if (!(pvt
= memget(sizeof *pvt
))) {
133 memset(pvt
, 0, sizeof *pvt
);
134 if (!(nw
= memget(sizeof *nw
))) {
135 memput(pvt
, sizeof *pvt
);
139 memset(nw
, 0x5e, sizeof *nw
);
141 nw
->close
= nw_close
;
142 nw
->byname
= nw_byname
;
143 nw
->byaddr
= nw_byaddr
;
145 nw
->rewind
= nw_rewind
;
146 nw
->minimize
= nw_minimize
;
147 nw
->res_get
= nw_res_get
;
148 nw
->res_set
= nw_res_set
;
155 nw_close(struct irs_nw
*this) {
156 struct pvt
*pvt
= (struct pvt
*)this->private;
159 if (pvt
->res
&& pvt
->free_res
)
160 (*pvt
->free_res
)(pvt
->res
);
162 (void)fclose(pvt
->fp
);
163 memput(pvt
, sizeof *pvt
);
164 memput(this, sizeof *this);
167 static struct nwent
*
168 nw_byaddr(struct irs_nw
*this, void *net
, int length
, int type
) {
171 if (init(this) == -1)
175 while ((p
= nw_next(this)) != NULL
)
176 if (p
->n_addrtype
== type
&& p
->n_length
== length
)
177 if (bitncmp(p
->n_addr
, net
, length
) == 0)
182 static struct nwent
*
183 nw_byname(struct irs_nw
*this, const char *name
, int type
) {
187 if (init(this) == -1)
191 while ((p
= nw_next(this)) != NULL
) {
192 if (ns_samename(p
->n_name
, name
) == 1 &&
193 p
->n_addrtype
== type
)
195 for (ap
= p
->n_aliases
; *ap
; ap
++)
196 if ((ns_samename(*ap
, name
) == 1) &&
197 (p
->n_addrtype
== type
))
205 nw_rewind(struct irs_nw
*this) {
206 struct pvt
*pvt
= (struct pvt
*)this->private;
209 if (fseek(pvt
->fp
, 0L, SEEK_SET
) == 0)
211 (void)fclose(pvt
->fp
);
213 if (!(pvt
->fp
= fopen(_PATH_NETWORKS
, "r")))
215 if (fcntl(fileno(pvt
->fp
), F_SETFD
, 1) < 0) {
216 (void)fclose(pvt
->fp
);
221 static struct nwent
*
222 nw_next(struct irs_nw
*this) {
223 struct pvt
*pvt
= (struct pvt
*)this->private;
224 struct nwent
*ret
= NULL
;
226 char *bufp
, *ndbuf
, *dbuf
= NULL
;
227 int c
, bufsiz
, offset
= 0;
229 if (init(this) == -1)
234 if (pvt
->fp
== NULL
) {
235 RES_SET_H_ERRNO(pvt
->res
, NETDB_INTERNAL
);
239 bufsiz
= sizeof(pvt
->line
);
242 p
= fgets(bufp
+ offset
, bufsiz
- offset
, pvt
->fp
);
245 if (!strchr(p
, '\n') && !feof(pvt
->fp
)) {
247 /* allocate space for longer line */
249 if ((ndbuf
= malloc(bufsiz
+ GROWBUF
)) != NULL
)
252 ndbuf
= realloc(dbuf
, bufsiz
+ GROWBUF
);
257 offset
= strlen(dbuf
);
259 /* allocation failed; skip this long line */
260 while ((c
= getc(pvt
->fp
)) != EOF
)
275 cp
= strpbrk(p
, "#\n");
279 cp
= strpbrk(p
, " \t");
283 while (*cp
== ' ' || *cp
== '\t')
285 p
= strpbrk(cp
, " \t");
288 pvt
->net
.n_length
= inet_net_pton(AF_INET
, cp
, pvt
->addr
,
290 if (pvt
->net
.n_length
< 0)
292 pvt
->net
.n_addrtype
= AF_INET
;
293 pvt
->net
.n_addr
= pvt
->addr
;
294 q
= pvt
->net
.n_aliases
= pvt
->aliases
;
298 if (*cp
== ' ' || *cp
== '\t') {
302 if (q
< &pvt
->aliases
[MAXALIASES
- 1])
304 cp
= strpbrk(cp
, " \t");
320 nw_minimize(struct irs_nw
*this) {
321 struct pvt
*pvt
= (struct pvt
*)this->private;
324 res_nclose(pvt
->res
);
325 if (pvt
->fp
!= NULL
) {
326 (void)fclose(pvt
->fp
);
331 static struct __res_state
*
332 nw_res_get(struct irs_nw
*this) {
333 struct pvt
*pvt
= (struct pvt
*)this->private;
336 struct __res_state
*res
;
337 res
= (struct __res_state
*)malloc(sizeof *res
);
342 memset(res
, 0, sizeof *res
);
343 nw_res_set(this, res
, free
);
350 nw_res_set(struct irs_nw
*this, struct __res_state
*res
,
351 void (*free_res
)(void *)) {
352 struct pvt
*pvt
= (struct pvt
*)this->private;
354 if (pvt
->res
&& pvt
->free_res
) {
355 res_nclose(pvt
->res
);
356 (*pvt
->free_res
)(pvt
->res
);
360 pvt
->free_res
= free_res
;
364 init(struct irs_nw
*this) {
365 struct pvt
*pvt
= (struct pvt
*)this->private;
367 if (!pvt
->res
&& !nw_res_get(this))
369 if (((pvt
->res
->options
& RES_INIT
) == 0U) &&
370 res_ninit(pvt
->res
) == -1)