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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 /* Portions Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
31 * Dep. Matematica Universidade de Coimbra, Portugal, Europe
33 * Permission to use, copy, modify, and distribute this software for any
34 * purpose with or without fee is hereby granted, provided that the above
35 * copyright notice and this permission notice appear in all copies.
37 * from getnetent.c 1.1 (Coimbra) 93/06/02
40 #if defined(LIBC_SCCS) && !defined(lint)
41 static char sccsid[] = "@(#)getnetent.c 8.1 (Berkeley) 6/4/93";
42 static char orig_rcsid[] = "From: Id: getnetent.c,v 8.4 1997/06/01 20:34:37 vixie Exp";
43 #endif /* LIBC_SCCS and not lint */
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <sys/types.h>
48 #include <sys/socket.h>
49 #include <netinet/in.h>
50 #include <arpa/inet.h>
51 #include <arpa/nameser.h>
59 #include "netdb_private.h"
62 _setnethtent(int f, struct netent_data *ned)
65 if (ned->netf == NULL)
66 ned->netf = fopen(_PATH_NETWORKS, "re");
73 _endnethtent(struct netent_data *ned)
84 getnetent_p(struct netent *ne, struct netent_data *ned)
89 char line[BUFSIZ + 1];
91 if (ned->netf == NULL &&
92 (ned->netf = fopen(_PATH_NETWORKS, "re")) == NULL)
95 p = fgets(line, sizeof line, ned->netf);
100 cp = strpbrk(p, "#\n");
104 ep = ned->netbuf + sizeof ned->netbuf;
106 cp = strpbrk(p, " \t");
112 RES_SET_H_ERRNO(__res_state(), NO_RECOVERY);
115 strlcpy(bp, p, ep - bp);
117 while (*cp == ' ' || *cp == '\t')
119 p = strpbrk(cp, " \t");
122 ne->n_net = inet_network(cp);
123 ne->n_addrtype = AF_INET;
124 q = ne->n_aliases = ned->net_aliases;
128 if (*cp == ' ' || *cp == '\t') {
132 if (q >= &ned->net_aliases[_MAXALIASES - 1])
134 p = strpbrk(cp, " \t");
137 len = strlen(cp) + 1;
140 strlcpy(bp, cp, ep - bp);
151 getnetent_r(struct netent *nptr, char *buffer, size_t buflen,
152 struct netent **result, int *h_errnop)
154 struct netent_data *ned;
158 statp = __res_state();
159 if ((ned = __netent_data_init()) == NULL) {
160 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
161 *h_errnop = statp->res_h_errno;
164 if (getnetent_p(&ne, ned) != 0)
166 if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
167 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
168 *h_errnop = statp->res_h_errno;
169 return ((errno != 0) ? errno : -1);
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) {
234 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
235 *h_errnop = statp->res_h_errno;
238 *((struct netent **)rval) = nptr;
243 _ht_getnetbyaddr(void *rval, void *cb_data, va_list ap)
249 int *errnop, *h_errnop;
250 struct netent *nptr, ne;
251 struct netent_data *ned;
255 net = va_arg(ap, uint32_t);
256 type = va_arg(ap, int);
257 nptr = va_arg(ap, struct netent *);
258 buffer = va_arg(ap, char *);
259 buflen = va_arg(ap, size_t);
260 errnop = va_arg(ap, int *);
261 h_errnop = va_arg(ap, int *);
263 statp = __res_state();
264 if ((ned = __netent_data_init()) == NULL) {
265 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
266 *h_errnop = statp->res_h_errno;
270 _setnethtent(ned->stayopen, ned);
271 while ((error = getnetent_p(&ne, ned)) == 0)
272 if (ne.n_addrtype == type && ne.n_net == net)
277 *h_errnop = statp->res_h_errno;
278 return (NS_NOTFOUND);
280 if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
282 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
283 *h_errnop = statp->res_h_errno;
286 *((struct netent **)rval) = nptr;