2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1985, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
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, and that
36 * the name of Digital Equipment Corporation not be used in advertising or
37 * publicity pertaining to distribution of the document or software without
38 * specific, written prior permission.
40 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
41 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
42 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
43 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
44 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
45 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
46 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
52 #if defined(LIBC_SCCS) && !defined(lint)
53 static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
54 #endif /* LIBC_SCCS and not lint */
55 #include <sys/cdefs.h>
56 __FBSDID("$FreeBSD$");
58 #include <sys/param.h>
59 #include <sys/socket.h>
60 #include <netinet/in.h>
61 #include <arpa/inet.h>
69 #include <arpa/nameser.h> /* XXX */
70 #include <resolv.h> /* XXX */
71 #include "netdb_private.h"
74 _sethosthtent(int f, struct hostent_data *hed)
77 hed->hostf = fopen(_PATH_HOSTS, "re");
84 _endhosthtent(struct hostent_data *hed)
86 if (hed->hostf && !hed->stayopen) {
87 (void) fclose(hed->hostf);
93 gethostent_p(struct hostent *he, struct hostent_data *hed, int mapped,
99 char hostbuf[BUFSIZ + 1];
101 if (!hed->hostf && !(hed->hostf = fopen(_PATH_HOSTS, "re"))) {
102 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
106 if (!(p = fgets(hostbuf, sizeof hostbuf, hed->hostf))) {
107 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
112 cp = strpbrk(p, "#\n");
115 if (!(cp = strpbrk(p, " \t")))
118 if (inet_pton(AF_INET6, p, hed->host_addr) > 0) {
121 } else if (inet_pton(AF_INET, p, hed->host_addr) > 0) {
123 _map_v4v6_address((char *)hed->host_addr,
124 (char *)hed->host_addr);
134 hed->h_addr_ptrs[0] = (char *)hed->host_addr;
135 hed->h_addr_ptrs[1] = NULL;
136 he->h_addr_list = hed->h_addr_ptrs;
139 while (*cp == ' ' || *cp == '\t')
142 ep = hed->hostbuf + sizeof hed->hostbuf;
144 q = he->h_aliases = hed->host_aliases;
145 if ((p = strpbrk(cp, " \t")) != NULL)
147 len = strlen(cp) + 1;
149 RES_SET_H_ERRNO(statp, NO_RECOVERY);
152 strlcpy(bp, cp, ep - bp);
156 if (*cp == ' ' || *cp == '\t') {
160 if (q >= &hed->host_aliases[_MAXALIASES - 1])
162 if ((p = strpbrk(cp, " \t")) != NULL)
164 len = strlen(cp) + 1;
167 strlcpy(bp, cp, ep - bp);
173 RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
178 gethostent_r(struct hostent *hptr, char *buffer, size_t buflen,
179 struct hostent **result, int *h_errnop)
181 struct hostent_data *hed;
185 statp = __res_state();
186 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
187 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
188 *h_errnop = statp->res_h_errno;
191 if ((hed = __hostent_data_init()) == NULL) {
192 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
193 *h_errnop = statp->res_h_errno;
196 if (gethostent_p(&he, hed, statp->options & RES_USE_INET6, statp) != 0)
198 if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
199 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
200 *h_errnop = statp->res_h_errno;
201 return ((errno != 0) ? errno : -1);
211 struct hostent *rval;
214 if ((hd = __hostdata_init()) == NULL)
216 if (gethostent_r(&hd->host, hd->data, sizeof(hd->data), &rval,
223 _ht_gethostbyname(void *rval, void *cb_data, va_list ap)
229 int *errnop, *h_errnop;
230 struct hostent *hptr, he;
231 struct hostent_data *hed;
236 name = va_arg(ap, const char *);
237 af = va_arg(ap, int);
238 hptr = va_arg(ap, struct hostent *);
239 buffer = va_arg(ap, char *);
240 buflen = va_arg(ap, size_t);
241 errnop = va_arg(ap, int *);
242 h_errnop = va_arg(ap, int *);
244 *((struct hostent **)rval) = NULL;
246 statp = __res_state();
247 if ((hed = __hostent_data_init()) == NULL) {
248 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
249 *h_errnop = statp->res_h_errno;
250 return (NS_NOTFOUND);
253 _sethosthtent(0, hed);
254 while ((error = gethostent_p(&he, hed, 0, statp)) == 0) {
255 if (he.h_addrtype != af)
257 if (he.h_addrtype == AF_INET &&
258 statp->options & RES_USE_INET6) {
259 _map_v4v6_address(he.h_addr, he.h_addr);
260 he.h_length = IN6ADDRSZ;
261 he.h_addrtype = AF_INET6;
263 if (strcasecmp(he.h_name, name) == 0)
265 for (cp = he.h_aliases; *cp != 0; cp++)
266 if (strcasecmp(*cp, name) == 0)
273 *h_errnop = statp->res_h_errno;
274 return (NS_NOTFOUND);
276 if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
278 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
279 *h_errnop = statp->res_h_errno;
282 *((struct hostent **)rval) = hptr;
287 _ht_gethostbyaddr(void *rval, void *cb_data, va_list ap)
294 int *errnop, *h_errnop;
295 struct hostent *hptr, he;
296 struct hostent_data *hed;
300 addr = va_arg(ap, const void *);
301 len = va_arg(ap, socklen_t);
302 af = va_arg(ap, int);
303 hptr = va_arg(ap, struct hostent *);
304 buffer = va_arg(ap, char *);
305 buflen = va_arg(ap, size_t);
306 errnop = va_arg(ap, int *);
307 h_errnop = va_arg(ap, int *);
309 *((struct hostent **)rval) = NULL;
311 statp = __res_state();
312 if ((hed = __hostent_data_init()) == NULL) {
313 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
314 *h_errnop = statp->res_h_errno;
315 return (NS_NOTFOUND);
318 _sethosthtent(0, hed);
319 while ((error = gethostent_p(&he, hed, 0, statp)) == 0)
320 if (he.h_addrtype == af && !bcmp(he.h_addr, addr, len)) {
321 if (he.h_addrtype == AF_INET &&
322 statp->options & RES_USE_INET6) {
323 _map_v4v6_address(he.h_addr, he.h_addr);
324 he.h_length = IN6ADDRSZ;
325 he.h_addrtype = AF_INET6;
332 return (NS_NOTFOUND);
333 if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
335 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
336 *h_errnop = statp->res_h_errno;
339 *((struct hostent **)rval) = hptr;