2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1983, 1993
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 /* Portions Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
33 * Dep. Matematica Universidade de Coimbra, Portugal, Europe
35 * Permission to use, copy, modify, and distribute this software for any
36 * purpose with or without fee is hereby granted, provided that the above
37 * copyright notice and this permission notice appear in all copies.
39 * from getnetent.c 1.1 (Coimbra) 93/06/02
42 #if defined(LIBC_SCCS) && !defined(lint)
43 static char sccsid[] = "@(#)getnetent.c 8.1 (Berkeley) 6/4/93";
44 static char orig_rcsid[] = "From: Id: getnetent.c,v 8.4 1997/06/01 20:34:37 vixie Exp";
45 #endif /* LIBC_SCCS and not lint */
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h>
52 #include <arpa/inet.h>
53 #include <arpa/nameser.h>
61 #include "netdb_private.h"
64 _setnethtent(int f, struct netent_data *ned)
67 if (ned->netf == NULL)
68 ned->netf = fopen(_PATH_NETWORKS, "re");
75 _endnethtent(struct netent_data *ned)
86 getnetent_p(struct netent *ne, struct netent_data *ned)
91 char line[BUFSIZ + 1];
93 if (ned->netf == NULL &&
94 (ned->netf = fopen(_PATH_NETWORKS, "re")) == NULL)
97 p = fgets(line, sizeof line, ned->netf);
102 cp = strpbrk(p, "#\n");
106 ep = ned->netbuf + sizeof ned->netbuf;
108 cp = strpbrk(p, " \t");
114 RES_SET_H_ERRNO(__res_state(), NO_RECOVERY);
117 strlcpy(bp, p, ep - bp);
119 while (*cp == ' ' || *cp == '\t')
121 p = strpbrk(cp, " \t");
124 ne->n_net = inet_network(cp);
125 ne->n_addrtype = AF_INET;
126 q = ne->n_aliases = ned->net_aliases;
130 if (*cp == ' ' || *cp == '\t') {
134 if (q >= &ned->net_aliases[_MAXALIASES - 1])
136 p = strpbrk(cp, " \t");
139 len = strlen(cp) + 1;
142 strlcpy(bp, cp, ep - bp);
153 getnetent_r(struct netent *nptr, char *buffer, size_t buflen,
154 struct netent **result, int *h_errnop)
156 struct netent_data *ned;
160 statp = __res_state();
161 if ((ned = __netent_data_init()) == NULL) {
162 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
163 *h_errnop = statp->res_h_errno;
166 if (getnetent_p(&ne, ned) != 0)
168 if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
169 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
170 *h_errnop = statp->res_h_errno;
171 return ((errno != 0) ? errno : -1);
184 if ((nd = __netdata_init()) == NULL)
186 if (getnetent_r(&nd->net, nd->data, sizeof(nd->data), &rval,
193 _ht_getnetbyname(void *rval, void *cb_data, va_list ap)
198 int *errnop, *h_errnop;
199 struct netent *nptr, ne;
200 struct netent_data *ned;
205 name = va_arg(ap, const char *);
206 nptr = va_arg(ap, struct netent *);
207 buffer = va_arg(ap, char *);
208 buflen = va_arg(ap, size_t);
209 errnop = va_arg(ap, int *);
210 h_errnop = va_arg(ap, int *);
212 statp = __res_state();
213 if ((ned = __netent_data_init()) == NULL) {
214 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
215 *h_errnop = statp->res_h_errno;
219 _setnethtent(ned->stayopen, ned);
220 while ((error = getnetent_p(&ne, ned)) == 0) {
221 if (strcasecmp(ne.n_name, name) == 0)
223 for (cp = ne.n_aliases; *cp != 0; cp++)
224 if (strcasecmp(*cp, name) == 0)
231 *h_errnop = statp->res_h_errno;
232 return (NS_NOTFOUND);
234 if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
236 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
237 *h_errnop = statp->res_h_errno;
240 *((struct netent **)rval) = nptr;
245 _ht_getnetbyaddr(void *rval, void *cb_data, va_list ap)
251 int *errnop, *h_errnop;
252 struct netent *nptr, ne;
253 struct netent_data *ned;
257 net = va_arg(ap, uint32_t);
258 type = va_arg(ap, int);
259 nptr = va_arg(ap, struct netent *);
260 buffer = va_arg(ap, char *);
261 buflen = va_arg(ap, size_t);
262 errnop = va_arg(ap, int *);
263 h_errnop = va_arg(ap, int *);
265 statp = __res_state();
266 if ((ned = __netent_data_init()) == NULL) {
267 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
268 *h_errnop = statp->res_h_errno;
272 _setnethtent(ned->stayopen, ned);
273 while ((error = getnetent_p(&ne, ned)) == 0)
274 if (ne.n_addrtype == type && ne.n_net == net)
279 *h_errnop = statp->res_h_errno;
280 return (NS_NOTFOUND);
282 if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
284 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
285 *h_errnop = statp->res_h_errno;
288 *((struct netent **)rval) = nptr;