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 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
46 #include <arpa/nameser.h>
54 #include "netdb_private.h"
57 _setnethtent(int f, struct netent_data *ned)
60 if (ned->netf == NULL)
61 ned->netf = fopen(_PATH_NETWORKS, "re");
68 _endnethtent(struct netent_data *ned)
79 getnetent_p(struct netent *ne, struct netent_data *ned)
84 char line[BUFSIZ + 1];
86 if (ned->netf == NULL &&
87 (ned->netf = fopen(_PATH_NETWORKS, "re")) == NULL)
90 p = fgets(line, sizeof line, ned->netf);
95 cp = strpbrk(p, "#\n");
99 ep = ned->netbuf + sizeof ned->netbuf;
101 cp = strpbrk(p, " \t");
107 RES_SET_H_ERRNO(__res_state(), NO_RECOVERY);
110 strlcpy(bp, p, ep - bp);
112 while (*cp == ' ' || *cp == '\t')
114 p = strpbrk(cp, " \t");
117 ne->n_net = inet_network(cp);
118 ne->n_addrtype = AF_INET;
119 q = ne->n_aliases = ned->net_aliases;
123 if (*cp == ' ' || *cp == '\t') {
127 if (q >= &ned->net_aliases[_MAXALIASES - 1])
129 p = strpbrk(cp, " \t");
132 len = strlen(cp) + 1;
135 strlcpy(bp, cp, ep - bp);
146 getnetent_r(struct netent *nptr, char *buffer, size_t buflen,
147 struct netent **result, int *h_errnop)
149 struct netent_data *ned;
153 statp = __res_state();
154 if ((ned = __netent_data_init()) == NULL) {
155 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
156 *h_errnop = statp->res_h_errno;
159 if (getnetent_p(&ne, ned) != 0)
161 if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
162 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
163 *h_errnop = statp->res_h_errno;
164 return ((errno != 0) ? errno : -1);
177 if ((nd = __netdata_init()) == NULL)
179 if (getnetent_r(&nd->net, nd->data, sizeof(nd->data), &rval,
186 _ht_getnetbyname(void *rval, void *cb_data, va_list ap)
191 int *errnop, *h_errnop;
192 struct netent *nptr, ne;
193 struct netent_data *ned;
198 name = va_arg(ap, const char *);
199 nptr = va_arg(ap, struct netent *);
200 buffer = va_arg(ap, char *);
201 buflen = va_arg(ap, size_t);
202 errnop = va_arg(ap, int *);
203 h_errnop = va_arg(ap, int *);
205 statp = __res_state();
206 if ((ned = __netent_data_init()) == NULL) {
207 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
208 *h_errnop = statp->res_h_errno;
212 _setnethtent(ned->stayopen, ned);
213 while ((error = getnetent_p(&ne, ned)) == 0) {
214 if (strcasecmp(ne.n_name, name) == 0)
216 for (cp = ne.n_aliases; *cp != 0; cp++)
217 if (strcasecmp(*cp, name) == 0)
224 *h_errnop = statp->res_h_errno;
225 return (NS_NOTFOUND);
227 if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
229 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
230 *h_errnop = statp->res_h_errno;
233 *((struct netent **)rval) = nptr;
238 _ht_getnetbyaddr(void *rval, void *cb_data, va_list ap)
244 int *errnop, *h_errnop;
245 struct netent *nptr, ne;
246 struct netent_data *ned;
250 net = va_arg(ap, uint32_t);
251 type = va_arg(ap, int);
252 nptr = va_arg(ap, struct netent *);
253 buffer = va_arg(ap, char *);
254 buflen = va_arg(ap, size_t);
255 errnop = va_arg(ap, int *);
256 h_errnop = va_arg(ap, int *);
258 statp = __res_state();
259 if ((ned = __netent_data_init()) == NULL) {
260 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
261 *h_errnop = statp->res_h_errno;
265 _setnethtent(ned->stayopen, ned);
266 while ((error = getnetent_p(&ne, ned)) == 0)
267 if (ne.n_addrtype == type && ne.n_net == net)
272 *h_errnop = statp->res_h_errno;
273 return (NS_NOTFOUND);
275 if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
277 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
278 *h_errnop = statp->res_h_errno;
281 *((struct netent **)rval) = nptr;