2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 /* Portions Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
35 * Dep. Matematica Universidade de Coimbra, Portugal, Europe
37 * Permission to use, copy, modify, and distribute this software for any
38 * purpose with or without fee is hereby granted, provided that the above
39 * copyright notice and this permission notice appear in all copies.
41 * from getnetent.c 1.1 (Coimbra) 93/06/02
44 #if defined(LIBC_SCCS) && !defined(lint)
45 static char sccsid[] = "@(#)getnetent.c 8.1 (Berkeley) 6/4/93";
46 static char orig_rcsid[] = "From: Id: getnetent.c,v 8.4 1997/06/01 20:34:37 vixie Exp";
47 #endif /* LIBC_SCCS and not lint */
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
51 #include <sys/types.h>
52 #include <sys/socket.h>
53 #include <netinet/in.h>
54 #include <arpa/inet.h>
55 #include <arpa/nameser.h>
62 #include "netdb_private.h"
65 _setnethtent(int f, struct netent_data *ned)
68 if (ned->netf == NULL)
69 ned->netf = fopen(_PATH_NETWORKS, "r");
76 _endnethtent(struct netent_data *ned)
87 getnetent_p(struct netent *ne, struct netent_data *ned)
92 char line[BUFSIZ + 1];
94 if (ned->netf == NULL &&
95 (ned->netf = fopen(_PATH_NETWORKS, "r")) == NULL)
98 p = fgets(line, sizeof line, ned->netf);
103 cp = strpbrk(p, "#\n");
107 ep = ned->netbuf + sizeof ned->netbuf;
109 cp = strpbrk(p, " \t");
115 RES_SET_H_ERRNO(__res_state(), NO_RECOVERY);
118 strlcpy(bp, p, ep - bp);
120 while (*cp == ' ' || *cp == '\t')
122 p = strpbrk(cp, " \t");
125 ne->n_net = inet_network(cp);
126 ne->n_addrtype = AF_INET;
127 q = ne->n_aliases = ned->net_aliases;
131 if (*cp == ' ' || *cp == '\t') {
135 if (q >= &ned->net_aliases[_MAXALIASES - 1])
137 p = strpbrk(cp, " \t");
140 len = strlen(cp) + 1;
143 strlcpy(bp, cp, ep - bp);
154 getnetent_r(struct netent *nptr, char *buffer, size_t buflen,
155 struct netent **result, int *h_errnop)
157 struct netent_data *ned;
161 statp = __res_state();
162 if ((ned = __netent_data_init()) == NULL) {
163 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
164 *h_errnop = statp->res_h_errno;
167 if (getnetent_p(&ne, ned) != 0)
169 if (__copy_netent(&ne, nptr, buffer, buflen) != 0)
182 if ((nd = __netdata_init()) == NULL)
184 if (getnetent_r(&nd->net, nd->data, sizeof(nd->data), &rval,
191 _ht_getnetbyname(void *rval, void *cb_data, va_list ap)
196 int *errnop, *h_errnop;
197 struct netent *nptr, ne;
198 struct netent_data *ned;
203 name = va_arg(ap, const char *);
204 nptr = va_arg(ap, struct netent *);
205 buffer = va_arg(ap, char *);
206 buflen = va_arg(ap, size_t);
207 errnop = va_arg(ap, int *);
208 h_errnop = va_arg(ap, int *);
210 statp = __res_state();
211 if ((ned = __netent_data_init()) == NULL) {
212 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
213 *h_errnop = statp->res_h_errno;
217 _setnethtent(ned->stayopen, ned);
218 while ((error = getnetent_p(&ne, ned)) == 0) {
219 if (strcasecmp(ne.n_name, name) == 0)
221 for (cp = ne.n_aliases; *cp != 0; cp++)
222 if (strcasecmp(*cp, name) == 0)
229 *h_errnop = statp->res_h_errno;
230 return (NS_NOTFOUND);
232 if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
233 *h_errnop = statp->res_h_errno;
234 return (NS_NOTFOUND);
236 *((struct netent **)rval) = nptr;
241 _ht_getnetbyaddr(void *rval, void *cb_data, va_list ap)
247 int *errnop, *h_errnop;
248 struct netent *nptr, ne;
249 struct netent_data *ned;
253 net = va_arg(ap, uint32_t);
254 type = va_arg(ap, int);
255 nptr = va_arg(ap, struct netent *);
256 buffer = va_arg(ap, char *);
257 buflen = va_arg(ap, size_t);
258 errnop = va_arg(ap, int *);
259 h_errnop = va_arg(ap, int *);
261 statp = __res_state();
262 if ((ned = __netent_data_init()) == NULL) {
263 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
264 *h_errnop = statp->res_h_errno;
268 _setnethtent(ned->stayopen, ned);
269 while ((error = getnetent_p(&ne, ned)) == 0)
270 if (ne.n_addrtype == type && ne.n_net == net)
275 *h_errnop = statp->res_h_errno;
276 return (NS_NOTFOUND);
278 if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
279 *h_errnop = statp->res_h_errno;
280 return (NS_NOTFOUND);
282 *((struct netent **)rval) = nptr;