2 * Copyright (c) 1985, 1988, 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 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
31 * Permission to use, copy, modify, and distribute this software for any
32 * purpose with or without fee is hereby granted, provided that the above
33 * copyright notice and this permission notice appear in all copies, and that
34 * the name of Digital Equipment Corporation not be used in advertising or
35 * publicity pertaining to distribution of the document or software without
36 * specific, written prior permission.
38 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
39 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
40 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
41 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
42 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
43 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
44 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
49 * @(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93
50 * $FreeBSD: src/lib/libc/net/gethostbyht.c,v 1.27 2007/01/09 00:28:02 imp Exp $
53 #include <sys/param.h>
54 #include <sys/socket.h>
55 #include <netinet/in.h>
56 #include <arpa/inet.h>
63 #include <arpa/nameser.h> /* XXX */
64 #include <resolv.h> /* XXX */
65 #include "netdb_private.h"
68 _sethosthtent(int f
, struct hostent_data
*hed
)
71 hed
->hostf
= fopen(_PATH_HOSTS
, "r");
78 _endhosthtent(struct hostent_data
*hed
)
80 if (hed
->hostf
&& !hed
->stayopen
) {
87 gethostent_p(struct hostent
*he
, struct hostent_data
*hed
, int mapped
,
93 char hostbuf
[BUFSIZ
+ 1];
95 if (!hed
->hostf
&& !(hed
->hostf
= fopen(_PATH_HOSTS
, "r"))) {
96 RES_SET_H_ERRNO(statp
, NETDB_INTERNAL
);
100 if (!(p
= fgets(hostbuf
, sizeof hostbuf
, hed
->hostf
))) {
101 RES_SET_H_ERRNO(statp
, HOST_NOT_FOUND
);
106 cp
= strpbrk(p
, "#\n");
109 if (!(cp
= strpbrk(p
, " \t")))
112 if (inet_pton(AF_INET6
, p
, hed
->host_addr
) > 0) {
115 } else if (inet_pton(AF_INET
, p
, hed
->host_addr
) > 0) {
117 _map_v4v6_address((char *)hed
->host_addr
,
118 (char *)hed
->host_addr
);
128 hed
->h_addr_ptrs
[0] = (char *)hed
->host_addr
;
129 hed
->h_addr_ptrs
[1] = NULL
;
130 he
->h_addr_list
= hed
->h_addr_ptrs
;
133 while (*cp
== ' ' || *cp
== '\t')
136 ep
= hed
->hostbuf
+ sizeof hed
->hostbuf
;
138 q
= he
->h_aliases
= hed
->host_aliases
;
139 if ((p
= strpbrk(cp
, " \t")) != NULL
)
141 len
= strlen(cp
) + 1;
143 RES_SET_H_ERRNO(statp
, NO_RECOVERY
);
146 strlcpy(bp
, cp
, ep
- bp
);
150 if (*cp
== ' ' || *cp
== '\t') {
154 if (q
>= &hed
->host_aliases
[_MAXALIASES
- 1])
156 if ((p
= strpbrk(cp
, " \t")) != NULL
)
158 len
= strlen(cp
) + 1;
161 strlcpy(bp
, cp
, ep
- bp
);
167 RES_SET_H_ERRNO(statp
, NETDB_SUCCESS
);
172 gethostent_r(struct hostent
*hptr
, char *buffer
, size_t buflen
,
173 struct hostent
**result
, int *h_errnop
)
175 struct hostent_data
*hed
;
179 statp
= __res_state();
180 if ((statp
->options
& RES_INIT
) == 0 && res_ninit(statp
) == -1) {
181 RES_SET_H_ERRNO(statp
, NETDB_INTERNAL
);
182 *h_errnop
= statp
->res_h_errno
;
185 if ((hed
= __hostent_data_init()) == NULL
) {
186 RES_SET_H_ERRNO(statp
, NETDB_INTERNAL
);
187 *h_errnop
= statp
->res_h_errno
;
190 if (gethostent_p(&he
, hed
, statp
->options
& RES_USE_INET6
, statp
) != 0)
192 if (__copy_hostent(&he
, hptr
, buffer
, buflen
) != 0)
202 struct hostent
*rval
;
205 if ((hd
= __hostdata_init()) == NULL
)
207 if (gethostent_r(&hd
->host
, hd
->data
, sizeof(hd
->data
), &rval
,
214 _ht_gethostbyname(void *rval
, void *cb_data __unused
, va_list ap
)
220 int *errnop __unused
;
222 struct hostent
*hptr
, he
;
223 struct hostent_data
*hed
;
228 name
= va_arg(ap
, const char *);
229 af
= va_arg(ap
, int);
230 hptr
= va_arg(ap
, struct hostent
*);
231 buffer
= va_arg(ap
, char *);
232 buflen
= va_arg(ap
, size_t);
233 errnop
= va_arg(ap
, int *);
234 h_errnop
= va_arg(ap
, int *);
236 *((struct hostent
**)rval
) = NULL
;
238 statp
= __res_state();
239 if ((hed
= __hostent_data_init()) == NULL
) {
240 RES_SET_H_ERRNO(statp
, NETDB_INTERNAL
);
241 *h_errnop
= statp
->res_h_errno
;
242 return (NS_NOTFOUND
);
245 _sethosthtent(0, hed
);
246 while ((error
= gethostent_p(&he
, hed
, 0, statp
)) == 0) {
247 if (he
.h_addrtype
!= af
)
249 if (he
.h_addrtype
== AF_INET
&&
250 statp
->options
& RES_USE_INET6
) {
251 _map_v4v6_address(he
.h_addr
, he
.h_addr
);
252 he
.h_length
= IN6ADDRSZ
;
253 he
.h_addrtype
= AF_INET6
;
255 if (strcasecmp(he
.h_name
, name
) == 0)
257 for (cp
= he
.h_aliases
; *cp
!= NULL
; cp
++)
258 if (strcasecmp(*cp
, name
) == 0)
265 *h_errnop
= statp
->res_h_errno
;
266 return (NS_NOTFOUND
);
268 if (__copy_hostent(&he
, hptr
, buffer
, buflen
) != 0) {
269 *h_errnop
= statp
->res_h_errno
;
270 return (NS_NOTFOUND
);
272 *((struct hostent
**)rval
) = hptr
;
277 _ht_gethostbyaddr(void *rval
, void *cb_data __unused
, va_list ap
)
284 int *errnop __unused
;
286 struct hostent
*hptr
, he
;
287 struct hostent_data
*hed
;
291 addr
= va_arg(ap
, const void *);
292 len
= va_arg(ap
, socklen_t
);
293 af
= va_arg(ap
, int);
294 hptr
= va_arg(ap
, struct hostent
*);
295 buffer
= va_arg(ap
, char *);
296 buflen
= va_arg(ap
, size_t);
297 errnop
= va_arg(ap
, int *);
298 h_errnop
= va_arg(ap
, int *);
300 *((struct hostent
**)rval
) = NULL
;
302 statp
= __res_state();
303 if ((hed
= __hostent_data_init()) == NULL
) {
304 RES_SET_H_ERRNO(statp
, NETDB_INTERNAL
);
305 *h_errnop
= statp
->res_h_errno
;
306 return (NS_NOTFOUND
);
309 _sethosthtent(0, hed
);
310 while ((error
= gethostent_p(&he
, hed
, 0, statp
)) == 0)
311 if (he
.h_addrtype
== af
&& !bcmp(he
.h_addr
, addr
, len
)) {
312 if (he
.h_addrtype
== AF_INET
&&
313 statp
->options
& RES_USE_INET6
) {
314 _map_v4v6_address(he
.h_addr
, he
.h_addr
);
315 he
.h_length
= IN6ADDRSZ
;
316 he
.h_addrtype
= AF_INET6
;
323 return (NS_NOTFOUND
);
324 if (__copy_hostent(&he
, hptr
, buffer
, buflen
) != 0) {
325 *h_errnop
= statp
->res_h_errno
;
326 return (NS_NOTFOUND
);
328 *((struct hostent
**)rval
) = hptr
;