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
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 $
32 * $DragonFly: src/lib/libc/net/getnetbyht.c,v 1.5 2005/11/13 02:04:47 swildner Exp $
35 /* Portions Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
36 * Dep. Matematica Universidade de Coimbra, Portugal, Europe
38 * Permission to use, copy, modify, and distribute this software for any
39 * purpose with or without fee is hereby granted, provided that the above
40 * copyright notice and this permission notice appear in all copies.
42 * from getnetent.c 1.1 (Coimbra) 93/06/02
45 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
49 #include <arpa/nameser.h>
56 #include "netdb_private.h"
59 _setnethtent(int f
, struct netent_data
*ned
)
62 if (ned
->netf
== NULL
)
63 ned
->netf
= fopen(_PATH_NETWORKS
, "r");
70 _endnethtent(struct netent_data
*ned
)
81 getnetent_p(struct netent
*ne
, struct netent_data
*ned
)
86 char line
[BUFSIZ
+ 1];
88 if (ned
->netf
== NULL
&&
89 (ned
->netf
= fopen(_PATH_NETWORKS
, "r")) == NULL
)
92 p
= fgets(line
, sizeof line
, ned
->netf
);
97 cp
= strpbrk(p
, "#\n");
101 ep
= ned
->netbuf
+ sizeof ned
->netbuf
;
103 cp
= strpbrk(p
, " \t");
109 RES_SET_H_ERRNO(__res_state(), NO_RECOVERY
);
112 strlcpy(bp
, p
, ep
- bp
);
114 while (*cp
== ' ' || *cp
== '\t')
116 p
= strpbrk(cp
, " \t");
119 ne
->n_net
= inet_network(cp
);
120 ne
->n_addrtype
= AF_INET
;
121 q
= ne
->n_aliases
= ned
->net_aliases
;
125 if (*cp
== ' ' || *cp
== '\t') {
129 if (q
>= &ned
->net_aliases
[_MAXALIASES
- 1])
131 p
= strpbrk(cp
, " \t");
134 len
= strlen(cp
) + 1;
137 strlcpy(bp
, cp
, ep
- bp
);
148 getnetent_r(struct netent
*nptr
, char *buffer
, size_t buflen
,
149 struct netent
**result
, int *h_errnop
)
151 struct netent_data
*ned
;
155 statp
= __res_state();
156 if ((ned
= __netent_data_init()) == NULL
) {
157 RES_SET_H_ERRNO(statp
, NETDB_INTERNAL
);
158 *h_errnop
= statp
->res_h_errno
;
161 if (getnetent_p(&ne
, ned
) != 0)
163 if (__copy_netent(&ne
, nptr
, buffer
, buflen
) != 0)
176 if ((nd
= __netdata_init()) == NULL
)
178 if (getnetent_r(&nd
->net
, nd
->data
, sizeof(nd
->data
), &rval
,
185 _ht_getnetbyname(void *rval
, void *cb_data
, va_list ap
)
190 int *errnop
, *h_errnop
;
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
!= 0; 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
, va_list ap
)
241 int *errnop
, *h_errnop
;
242 struct netent
*nptr
, ne
;
243 struct netent_data
*ned
;
247 net
= va_arg(ap
, uint32_t);
248 type
= va_arg(ap
, int);
249 nptr
= va_arg(ap
, struct netent
*);
250 buffer
= va_arg(ap
, char *);
251 buflen
= va_arg(ap
, size_t);
252 errnop
= va_arg(ap
, int *);
253 h_errnop
= va_arg(ap
, int *);
255 statp
= __res_state();
256 if ((ned
= __netent_data_init()) == NULL
) {
257 RES_SET_H_ERRNO(statp
, NETDB_INTERNAL
);
258 *h_errnop
= statp
->res_h_errno
;
262 _setnethtent(ned
->stayopen
, ned
);
263 while ((error
= getnetent_p(&ne
, ned
)) == 0)
264 if (ne
.n_addrtype
== type
&& ne
.n_net
== net
)
269 *h_errnop
= statp
->res_h_errno
;
270 return (NS_NOTFOUND
);
272 if (__copy_netent(&ne
, nptr
, buffer
, buflen
) != 0) {
273 *h_errnop
= statp
->res_h_errno
;
274 return (NS_NOTFOUND
);
276 *((struct netent
**)rval
) = nptr
;