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
29 * @(#)getnetent.c 8.1 (Berkeley) 6/4/93
30 * From: Id: getnetent.c,v 8.4 1997/06/01 20:34:37 vixie Exp
31 * $FreeBSD: src/lib/libc/net/getnetbyht.c,v 1.19 2007/01/09 00:28:02 imp Exp $
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 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
48 #include <arpa/nameser.h>
55 #include "netdb_private.h"
58 _setnethtent(int f
, struct netent_data
*ned
)
61 if (ned
->netf
== NULL
)
62 ned
->netf
= fopen(_PATH_NETWORKS
, "r");
69 _endnethtent(struct netent_data
*ned
)
80 getnetent_p(struct netent
*ne
, struct netent_data
*ned
)
85 char line
[BUFSIZ
+ 1];
87 if (ned
->netf
== NULL
&&
88 (ned
->netf
= fopen(_PATH_NETWORKS
, "r")) == NULL
)
91 p
= fgets(line
, sizeof line
, ned
->netf
);
96 cp
= strpbrk(p
, "#\n");
100 ep
= ned
->netbuf
+ sizeof ned
->netbuf
;
102 cp
= strpbrk(p
, " \t");
108 RES_SET_H_ERRNO(__res_state(), NO_RECOVERY
);
111 strlcpy(bp
, p
, ep
- bp
);
113 while (*cp
== ' ' || *cp
== '\t')
115 p
= strpbrk(cp
, " \t");
118 ne
->n_net
= inet_network(cp
);
119 ne
->n_addrtype
= AF_INET
;
120 q
= ne
->n_aliases
= ned
->net_aliases
;
124 if (*cp
== ' ' || *cp
== '\t') {
128 if (q
>= &ned
->net_aliases
[_MAXALIASES
- 1])
130 p
= strpbrk(cp
, " \t");
133 len
= strlen(cp
) + 1;
136 strlcpy(bp
, cp
, ep
- bp
);
147 getnetent_r(struct netent
*nptr
, char *buffer
, size_t buflen
,
148 struct netent
**result
, int *h_errnop
)
150 struct netent_data
*ned
;
154 statp
= __res_state();
155 if ((ned
= __netent_data_init()) == NULL
) {
156 RES_SET_H_ERRNO(statp
, NETDB_INTERNAL
);
157 *h_errnop
= statp
->res_h_errno
;
160 if (getnetent_p(&ne
, ned
) != 0)
162 if (__copy_netent(&ne
, nptr
, buffer
, buflen
) != 0)
175 if ((nd
= __netdata_init()) == NULL
)
177 if (getnetent_r(&nd
->net
, nd
->data
, sizeof(nd
->data
), &rval
,
184 _ht_getnetbyname(void *rval
, void *cb_data __unused
, va_list ap
)
189 int *errnop __unused
;
191 struct netent
*nptr
, ne
;
192 struct netent_data
*ned
;
197 name
= va_arg(ap
, const char *);
198 nptr
= va_arg(ap
, struct netent
*);
199 buffer
= va_arg(ap
, char *);
200 buflen
= va_arg(ap
, size_t);
201 errnop
= va_arg(ap
, int *);
202 h_errnop
= va_arg(ap
, int *);
204 statp
= __res_state();
205 if ((ned
= __netent_data_init()) == NULL
) {
206 RES_SET_H_ERRNO(statp
, NETDB_INTERNAL
);
207 *h_errnop
= statp
->res_h_errno
;
211 _setnethtent(ned
->stayopen
, ned
);
212 while ((error
= getnetent_p(&ne
, ned
)) == 0) {
213 if (strcasecmp(ne
.n_name
, name
) == 0)
215 for (cp
= ne
.n_aliases
; *cp
!= NULL
; cp
++)
216 if (strcasecmp(*cp
, name
) == 0)
223 *h_errnop
= statp
->res_h_errno
;
224 return (NS_NOTFOUND
);
226 if (__copy_netent(&ne
, nptr
, buffer
, buflen
) != 0) {
227 *h_errnop
= statp
->res_h_errno
;
228 return (NS_NOTFOUND
);
230 *((struct netent
**)rval
) = nptr
;
235 _ht_getnetbyaddr(void *rval
, void *cb_data __unused
, va_list ap
)
241 int *errnop __unused
;
243 struct netent
*nptr
, ne
;
244 struct netent_data
*ned
;
248 net
= va_arg(ap
, uint32_t);
249 type
= va_arg(ap
, int);
250 nptr
= va_arg(ap
, struct netent
*);
251 buffer
= va_arg(ap
, char *);
252 buflen
= va_arg(ap
, size_t);
253 errnop
= va_arg(ap
, int *);
254 h_errnop
= va_arg(ap
, int *);
256 statp
= __res_state();
257 if ((ned
= __netent_data_init()) == NULL
) {
258 RES_SET_H_ERRNO(statp
, NETDB_INTERNAL
);
259 *h_errnop
= statp
->res_h_errno
;
263 _setnethtent(ned
->stayopen
, ned
);
264 while ((error
= getnetent_p(&ne
, ned
)) == 0)
265 if (ne
.n_addrtype
== type
&& ne
.n_net
== net
)
270 *h_errnop
= statp
->res_h_errno
;
271 return (NS_NOTFOUND
);
273 if (__copy_netent(&ne
, nptr
, buffer
, buflen
) != 0) {
274 *h_errnop
= statp
->res_h_errno
;
275 return (NS_NOTFOUND
);
277 *((struct netent
**)rval
) = nptr
;