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 * 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 * 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
50 #if defined(LIBC_SCCS) && !defined(lint)
51 static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
52 #endif /* LIBC_SCCS and not lint */
53 #include <sys/cdefs.h>
54 __FBSDID("$FreeBSD$");
56 #include <sys/param.h>
57 #include <sys/socket.h>
58 #include <netinet/in.h>
59 #include <arpa/inet.h>
66 #include <arpa/nameser.h> /* XXX */
67 #include <resolv.h> /* XXX */
68 #include "netdb_private.h"
71 _sethosthtent(int f, struct hostent_data *hed)
74 hed->hostf = fopen(_PATH_HOSTS, "r");
81 _endhosthtent(struct hostent_data *hed)
83 if (hed->hostf && !hed->stayopen) {
84 (void) fclose(hed->hostf);
90 gethostent_p(struct hostent *he, struct hostent_data *hed, int mapped,
96 char hostbuf[BUFSIZ + 1];
98 if (!hed->hostf && !(hed->hostf = fopen(_PATH_HOSTS, "r"))) {
99 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
103 if (!(p = fgets(hostbuf, sizeof hostbuf, hed->hostf))) {
104 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
109 cp = strpbrk(p, "#\n");
112 if (!(cp = strpbrk(p, " \t")))
115 if (inet_pton(AF_INET6, p, hed->host_addr) > 0) {
118 } else if (inet_pton(AF_INET, p, hed->host_addr) > 0) {
120 _map_v4v6_address((char *)hed->host_addr,
121 (char *)hed->host_addr);
131 hed->h_addr_ptrs[0] = (char *)hed->host_addr;
132 hed->h_addr_ptrs[1] = NULL;
133 he->h_addr_list = hed->h_addr_ptrs;
136 while (*cp == ' ' || *cp == '\t')
139 ep = hed->hostbuf + sizeof hed->hostbuf;
141 q = he->h_aliases = hed->host_aliases;
142 if ((p = strpbrk(cp, " \t")) != NULL)
144 len = strlen(cp) + 1;
146 RES_SET_H_ERRNO(statp, NO_RECOVERY);
149 strlcpy(bp, cp, ep - bp);
153 if (*cp == ' ' || *cp == '\t') {
157 if (q >= &hed->host_aliases[_MAXALIASES - 1])
159 if ((p = strpbrk(cp, " \t")) != NULL)
161 len = strlen(cp) + 1;
164 strlcpy(bp, cp, ep - bp);
170 RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
175 gethostent_r(struct hostent *hptr, char *buffer, size_t buflen,
176 struct hostent **result, int *h_errnop)
178 struct hostent_data *hed;
182 statp = __res_state();
183 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
184 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
185 *h_errnop = statp->res_h_errno;
188 if ((hed = __hostent_data_init()) == NULL) {
189 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
190 *h_errnop = statp->res_h_errno;
193 if (gethostent_p(&he, hed, statp->options & RES_USE_INET6, statp) != 0)
195 if (__copy_hostent(&he, hptr, buffer, buflen) != 0)
205 struct hostent *rval;
208 if ((hd = __hostdata_init()) == NULL)
210 if (gethostent_r(&hd->host, hd->data, sizeof(hd->data), &rval,
217 _ht_gethostbyname(void *rval, void *cb_data, va_list ap)
223 int *errnop, *h_errnop;
224 struct hostent *hptr, he;
225 struct hostent_data *hed;
230 name = va_arg(ap, const char *);
231 af = va_arg(ap, int);
232 hptr = va_arg(ap, struct hostent *);
233 buffer = va_arg(ap, char *);
234 buflen = va_arg(ap, size_t);
235 errnop = va_arg(ap, int *);
236 h_errnop = va_arg(ap, int *);
238 *((struct hostent **)rval) = NULL;
240 statp = __res_state();
241 if ((hed = __hostent_data_init()) == NULL) {
242 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
243 *h_errnop = statp->res_h_errno;
244 return (NS_NOTFOUND);
247 _sethosthtent(0, hed);
248 while ((error = gethostent_p(&he, hed, 0, statp)) == 0) {
249 if (he.h_addrtype != af)
251 if (he.h_addrtype == AF_INET &&
252 statp->options & RES_USE_INET6) {
253 _map_v4v6_address(he.h_addr, he.h_addr);
254 he.h_length = IN6ADDRSZ;
255 he.h_addrtype = AF_INET6;
257 if (strcasecmp(he.h_name, name) == 0)
259 for (cp = he.h_aliases; *cp != 0; cp++)
260 if (strcasecmp(*cp, name) == 0)
267 *h_errnop = statp->res_h_errno;
268 return (NS_NOTFOUND);
270 if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
271 *h_errnop = statp->res_h_errno;
272 return (NS_NOTFOUND);
274 *((struct hostent **)rval) = hptr;
279 _ht_gethostbyaddr(void *rval, void *cb_data, va_list ap)
286 int *errnop, *h_errnop;
287 struct hostent *hptr, he;
288 struct hostent_data *hed;
292 addr = va_arg(ap, const void *);
293 len = va_arg(ap, socklen_t);
294 af = va_arg(ap, int);
295 hptr = va_arg(ap, struct hostent *);
296 buffer = va_arg(ap, char *);
297 buflen = va_arg(ap, size_t);
298 errnop = va_arg(ap, int *);
299 h_errnop = va_arg(ap, int *);
301 *((struct hostent **)rval) = NULL;
303 statp = __res_state();
304 if ((hed = __hostent_data_init()) == NULL) {
305 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
306 *h_errnop = statp->res_h_errno;
307 return (NS_NOTFOUND);
310 _sethosthtent(0, hed);
311 while ((error = gethostent_p(&he, hed, 0, statp)) == 0)
312 if (he.h_addrtype == af && !bcmp(he.h_addr, addr, len)) {
313 if (he.h_addrtype == AF_INET &&
314 statp->options & RES_USE_INET6) {
315 _map_v4v6_address(he.h_addr, he.h_addr);
316 he.h_length = IN6ADDRSZ;
317 he.h_addrtype = AF_INET6;
324 return (NS_NOTFOUND);
325 if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
326 *h_errnop = statp->res_h_errno;
327 return (NS_NOTFOUND);
329 *((struct hostent **)rval) = hptr;