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 * 4. 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>
58 #include "netdb_private.h"
61 _setnethtent(int f, struct netent_data *ned)
64 if (ned->netf == NULL)
65 ned->netf = fopen(_PATH_NETWORKS, "r");
72 _endnethtent(struct netent_data *ned)
83 getnetent_p(struct netent *ne, struct netent_data *ned)
88 char line[BUFSIZ + 1];
90 if (ned->netf == NULL &&
91 (ned->netf = fopen(_PATH_NETWORKS, "r")) == NULL)
94 p = fgets(line, sizeof line, ned->netf);
99 cp = strpbrk(p, "#\n");
103 ep = ned->netbuf + sizeof ned->netbuf;
105 cp = strpbrk(p, " \t");
111 RES_SET_H_ERRNO(__res_state(), NO_RECOVERY);
114 strlcpy(bp, p, ep - bp);
116 while (*cp == ' ' || *cp == '\t')
118 p = strpbrk(cp, " \t");
121 ne->n_net = inet_network(cp);
122 ne->n_addrtype = AF_INET;
123 q = ne->n_aliases = ned->net_aliases;
127 if (*cp == ' ' || *cp == '\t') {
131 if (q >= &ned->net_aliases[_MAXALIASES - 1])
133 p = strpbrk(cp, " \t");
136 len = strlen(cp) + 1;
139 strlcpy(bp, cp, ep - bp);
150 getnetent_r(struct netent *nptr, char *buffer, size_t buflen,
151 struct netent **result, int *h_errnop)
153 struct netent_data *ned;
157 statp = __res_state();
158 if ((ned = __netent_data_init()) == NULL) {
159 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
160 *h_errnop = statp->res_h_errno;
163 if (getnetent_p(&ne, ned) != 0)
165 if (__copy_netent(&ne, nptr, buffer, buflen) != 0)
178 if ((nd = __netdata_init()) == NULL)
180 if (getnetent_r(&nd->net, nd->data, sizeof(nd->data), &rval,
187 _ht_getnetbyname(void *rval, void *cb_data, va_list ap)
192 int *errnop, *h_errnop;
193 struct netent *nptr, ne;
194 struct netent_data *ned;
199 name = va_arg(ap, const char *);
200 nptr = va_arg(ap, struct netent *);
201 buffer = va_arg(ap, char *);
202 buflen = va_arg(ap, size_t);
203 errnop = va_arg(ap, int *);
204 h_errnop = va_arg(ap, int *);
206 statp = __res_state();
207 if ((ned = __netent_data_init()) == NULL) {
208 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
209 *h_errnop = statp->res_h_errno;
213 _setnethtent(ned->stayopen, ned);
214 while ((error = getnetent_p(&ne, ned)) == 0) {
215 if (strcasecmp(ne.n_name, name) == 0)
217 for (cp = ne.n_aliases; *cp != 0; cp++)
218 if (strcasecmp(*cp, name) == 0)
225 *h_errnop = statp->res_h_errno;
226 return (NS_NOTFOUND);
228 if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
229 *h_errnop = statp->res_h_errno;
230 return (NS_NOTFOUND);
232 *((struct netent **)rval) = nptr;
237 _ht_getnetbyaddr(void *rval, void *cb_data, va_list ap)
243 int *errnop, *h_errnop;
244 struct netent *nptr, ne;
245 struct netent_data *ned;
249 net = va_arg(ap, uint32_t);
250 type = va_arg(ap, int);
251 nptr = va_arg(ap, struct netent *);
252 buffer = va_arg(ap, char *);
253 buflen = va_arg(ap, size_t);
254 errnop = va_arg(ap, int *);
255 h_errnop = va_arg(ap, int *);
257 statp = __res_state();
258 if ((ned = __netent_data_init()) == NULL) {
259 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
260 *h_errnop = statp->res_h_errno;
264 _setnethtent(ned->stayopen, ned);
265 while ((error = getnetent_p(&ne, ned)) == 0)
266 if (ne.n_addrtype == type && ne.n_net == net)
271 *h_errnop = statp->res_h_errno;
272 return (NS_NOTFOUND);
274 if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
275 *h_errnop = statp->res_h_errno;
276 return (NS_NOTFOUND);
278 *((struct netent **)rval) = nptr;