2 * SPDX-License-Identifier: BSD-3-Clause
4 * ++Copyright++ 1985, 1988, 1993
6 * Copyright (c) 1985, 1988, 1993
7 * The Regents of the University of California. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
35 * Permission to use, copy, modify, and distribute this software for any
36 * purpose with or without fee is hereby granted, provided that the above
37 * copyright notice and this permission notice appear in all copies, and that
38 * the name of Digital Equipment Corporation not be used in advertising or
39 * publicity pertaining to distribution of the document or software without
40 * specific, written prior permission.
42 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
43 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
44 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
45 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
46 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
47 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
48 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
54 #if defined(LIBC_SCCS) && !defined(lint)
55 static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
56 static char fromrcsid[] = "From: Id: gethnamaddr.c,v 8.23 1998/04/07 04:59:46 vixie Exp $";
57 #endif /* LIBC_SCCS and not lint */
58 #include <sys/cdefs.h>
59 __FBSDID("$FreeBSD$");
61 #include <sys/param.h>
62 #include <sys/socket.h>
63 #include <netinet/in.h>
64 #include <arpa/inet.h>
65 #include <arpa/nameser.h>
79 #include "netdb_private.h"
80 #include "res_config.h"
82 #define SPRINTF(x) ((size_t)sprintf x)
84 static const char AskedForGot[] =
85 "gethostby*.gethostanswer: asked for \"%s\", got \"%s\"";
88 static void addrsort(char **, int, res_state);
92 static void dbg_printf(char *, int, res_state) __printflike(1, 0);
95 #define MAXPACKET (64*1024)
99 u_char buf[MAXPACKET];
111 dbg_printf(char *msg, int num, res_state res)
113 if (res->options & RES_DEBUG) {
121 # define dbg_printf(msg, num, res) /*nada*/
124 #define BOUNDED_INCR(x) \
128 RES_SET_H_ERRNO(statp, NO_RECOVERY); \
133 #define BOUNDS_CHECK(ptr, count) \
135 if ((ptr) + (count) > eom) { \
136 RES_SET_H_ERRNO(statp, NO_RECOVERY); \
142 gethostanswer(const querybuf *answer, int anslen, const char *qname, int qtype,
143 struct hostent *he, struct hostent_data *hed, res_state statp)
148 const u_char *eom, *erdata;
149 char *bp, *ep, **ap, **hap;
150 int type, class, ancount, qdcount;
151 int haveanswer, had_error;
155 int (*name_ok)(const char *);
159 eom = answer->buf + anslen;
169 RES_SET_H_ERRNO(statp, NO_RECOVERY);
170 return (-1); /* XXX should be abort(); */
173 * find first satisfactory answer
176 ancount = ntohs(hp->ancount);
177 qdcount = ntohs(hp->qdcount);
179 ep = hed->hostbuf + sizeof hed->hostbuf;
181 BOUNDED_INCR(HFIXEDSZ);
183 RES_SET_H_ERRNO(statp, NO_RECOVERY);
186 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
187 if ((n < 0) || !(*name_ok)(bp)) {
188 RES_SET_H_ERRNO(statp, NO_RECOVERY);
191 BOUNDED_INCR(n + QFIXEDSZ);
192 if (qtype == T_A || qtype == T_AAAA) {
193 /* res_send() has already verified that the query name is the
194 * same as the one we sent; this just gets the expanded name
195 * (i.e., with the succeeding search-domain tacked on).
197 n = strlen(bp) + 1; /* for the \0 */
198 if (n >= MAXHOSTNAMELEN) {
199 RES_SET_H_ERRNO(statp, NO_RECOVERY);
204 /* The qname can be abbreviated, but h_name is now absolute. */
207 ap = hed->host_aliases;
209 he->h_aliases = hed->host_aliases;
210 hap = hed->h_addr_ptrs;
212 he->h_addr_list = hed->h_addr_ptrs;
216 while (ancount-- > 0 && cp < eom && !had_error) {
217 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
218 if ((n < 0) || !(*name_ok)(bp)) {
223 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
224 type = _getshort(cp);
225 cp += INT16SZ; /* type */
226 class = _getshort(cp);
227 cp += INT16SZ; /* class */
228 if (qtype == T_A && type == T_A)
229 _dns_ttl_ = _getlong(cp);
230 cp += INT32SZ; /* TTL */
232 cp += INT16SZ; /* len */
236 /* XXX - debug? syslog? */
238 continue; /* XXX - had_error++ ? */
240 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
241 if (ap >= &hed->host_aliases[_MAXALIASES-1])
243 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
244 if ((n < 0) || !(*name_ok)(tbuf)) {
250 RES_SET_H_ERRNO(statp, NO_RECOVERY);
255 n = strlen(bp) + 1; /* for the \0 */
256 if (n >= MAXHOSTNAMELEN) {
261 /* Get canonical name. */
262 n = strlen(tbuf) + 1; /* for the \0 */
263 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
272 if (qtype == T_PTR && type == T_CNAME) {
273 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
274 if (n < 0 || !res_dnok(tbuf)) {
280 RES_SET_H_ERRNO(statp, NO_RECOVERY);
283 /* Get canonical name. */
284 n = strlen(tbuf) + 1; /* for the \0 */
285 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
295 if (type != T_SIG && type != ns_t_dname)
296 syslog(LOG_NOTICE|LOG_AUTH,
297 "gethostby*.gethostanswer: asked for \"%s %s %s\", got type \"%s\"",
298 qname, p_class(C_IN), p_type(qtype),
301 continue; /* XXX - had_error++ ? */
305 if (strcasecmp(tname, bp) != 0) {
306 syslog(LOG_NOTICE|LOG_AUTH,
307 AskedForGot, qname, bp);
309 continue; /* XXX - had_error++ ? */
311 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
312 if ((n < 0) || !res_hnok(bp)) {
316 #if MULTI_PTRS_ARE_ALIASES
319 RES_SET_H_ERRNO(statp, NO_RECOVERY);
324 else if (ap < &hed->host_aliases[_MAXALIASES-1])
329 n = strlen(bp) + 1; /* for the \0 */
330 if (n >= MAXHOSTNAMELEN) {
339 if (statp->options & RES_USE_INET6) {
340 n = strlen(bp) + 1; /* for the \0 */
341 if (n >= MAXHOSTNAMELEN) {
346 _map_v4v6_hostent(he, &bp, ep);
348 RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
353 if (strcasecmp(he->h_name, bp) != 0) {
354 syslog(LOG_NOTICE|LOG_AUTH,
355 AskedForGot, he->h_name, bp);
357 continue; /* XXX - had_error++ ? */
359 if (n != he->h_length) {
367 nn = strlen(bp) + 1; /* for the \0 */
371 bp += sizeof(align) - ((u_long)bp % sizeof(align));
374 dbg_printf("size (%d) too big\n", n, statp);
378 if (hap >= &hed->h_addr_ptrs[_MAXADDRS-1]) {
380 dbg_printf("Too many addresses (%d)\n",
385 memcpy(*hap++ = bp, cp, n);
389 RES_SET_H_ERRNO(statp, NO_RECOVERY);
394 dbg_printf("Impossible condition (type=%d)\n", type,
396 RES_SET_H_ERRNO(statp, NO_RECOVERY);
398 /* BIND has abort() here, too risky on bad data */
406 # if defined(RESOLVSORT)
408 * Note: we sort even if host can take only one address
409 * in its return structures - should give it the "best"
410 * address in that case, not some random one
412 if (statp->nsort && haveanswer > 1 && qtype == T_A)
413 addrsort(hed->h_addr_ptrs, haveanswer, statp);
414 # endif /*RESOLVSORT*/
416 n = strlen(qname) + 1; /* for the \0 */
417 if (n > ep - bp || n >= MAXHOSTNAMELEN)
423 if (statp->options & RES_USE_INET6)
424 _map_v4v6_hostent(he, &bp, ep);
425 RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
429 RES_SET_H_ERRNO(statp, NO_RECOVERY);
433 /* XXX: for async DNS resolver in ypserv */
435 __dns_getanswer(const char *answer, int anslen, const char *qname, int qtype)
438 struct hostent_data *hed;
442 statp = __res_state();
443 if ((he = __hostent_init()) == NULL ||
444 (hed = __hostent_data_init()) == NULL) {
445 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
450 he->h_addrtype = AF_INET6;
451 he->h_length = NS_IN6ADDRSZ;
455 he->h_addrtype = AF_INET;
456 he->h_length = NS_INADDRSZ;
460 error = gethostanswer((const querybuf *)answer, anslen, qname, qtype,
462 return (error == 0) ? he : NULL;
466 _dns_gethostbyname(void *rval, void *cb_data, va_list ap)
472 int *errnop, *h_errnop;
473 struct hostent *hptr, he;
474 struct hostent_data *hed;
479 name = va_arg(ap, const char *);
480 af = va_arg(ap, int);
481 hptr = va_arg(ap, struct hostent *);
482 buffer = va_arg(ap, char *);
483 buflen = va_arg(ap, size_t);
484 errnop = va_arg(ap, int *);
485 h_errnop = va_arg(ap, int *);
487 *((struct hostent **)rval) = NULL;
489 statp = __res_state();
490 if ((hed = __hostent_data_init()) == NULL) {
491 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
492 *h_errnop = statp->res_h_errno;
493 return (NS_NOTFOUND);
499 he.h_length = NS_INADDRSZ;
503 he.h_length = NS_IN6ADDRSZ;
507 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
508 *h_errnop = statp->res_h_errno;
509 errno = EAFNOSUPPORT;
513 if ((buf = malloc(sizeof(*buf))) == NULL) {
514 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
515 *h_errnop = statp->res_h_errno;
516 return (NS_NOTFOUND);
518 n = res_nsearch(statp, name, C_IN, type, buf->buf, sizeof(buf->buf));
521 dbg_printf("res_nsearch failed (%d)\n", n, statp);
522 *h_errnop = statp->res_h_errno;
523 return (NS_NOTFOUND);
524 } else if (n > sizeof(buf->buf)) {
526 dbg_printf("static buffer is too small (%d)\n", n, statp);
527 *h_errnop = statp->res_h_errno;
530 error = gethostanswer(buf, n, name, type, &he, hed, statp);
533 *h_errnop = statp->res_h_errno;
534 switch (statp->res_h_errno) {
536 return (NS_NOTFOUND);
538 return (NS_TRYAGAIN);
544 if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
546 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
547 *h_errnop = statp->res_h_errno;
550 RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
551 *((struct hostent **)rval) = hptr;
556 _dns_gethostbyaddr(void *rval, void *cb_data, va_list ap)
563 int *errnop, *h_errnop;
565 struct hostent *hptr, he;
566 struct hostent_data *hed;
569 char qbuf[MAXDNAME+1], *qp;
576 char hname2[MAXDNAME+1], numaddr[46];
578 #endif /*SUNSECURITY*/
580 addr = va_arg(ap, const void *);
581 len = va_arg(ap, socklen_t);
582 af = va_arg(ap, int);
583 hptr = va_arg(ap, struct hostent *);
584 buffer = va_arg(ap, char *);
585 buflen = va_arg(ap, size_t);
586 errnop = va_arg(ap, int *);
587 h_errnop = va_arg(ap, int *);
588 uaddr = (const u_char *)addr;
590 *((struct hostent **)rval) = NULL;
592 statp = __res_state();
593 if ((hed = __hostent_data_init()) == NULL) {
594 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
595 *h_errnop = statp->res_h_errno;
596 return (NS_NOTFOUND);
601 (void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
609 for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) {
610 qp += SPRINTF((qp, "%x.%x.",
612 (uaddr[n] >> 4) & 0xf));
614 strlcat(qbuf, "ip6.arpa", sizeof(qbuf));
619 if ((buf = malloc(sizeof(*buf))) == NULL) {
620 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
621 *h_errnop = statp->res_h_errno;
624 n = res_nquery(statp, qbuf, C_IN, T_PTR, (u_char *)buf->buf,
628 dbg_printf("res_nquery failed (%d)\n", n, statp);
629 *h_errnop = statp->res_h_errno;
632 if (n > sizeof buf->buf) {
634 dbg_printf("static buffer is too small (%d)\n", n, statp);
635 *h_errnop = statp->res_h_errno;
638 if (gethostanswer(buf, n, qbuf, T_PTR, &he, hed, statp) != 0) {
640 *h_errnop = statp->res_h_errno;
641 switch (statp->res_h_errno) {
643 return (NS_NOTFOUND);
645 return (NS_TRYAGAIN);
655 * turn off search as the name should be absolute,
656 * 'localhost' should be matched by defnames
658 strncpy(hname2, he.h_name, MAXDNAME);
659 hname2[MAXDNAME] = '\0';
660 old_options = statp->options;
661 statp->options &= ~RES_DNSRCH;
662 statp->options |= RES_DEFNAMES;
663 memset(&rhd, 0, sizeof rhd);
664 rhe = gethostbyname_r(hname2, &rhd.host, &rhd.data,
665 sizeof(rhd.data), &ret_h_error);
667 if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL)
668 strlcpy(numaddr, "UNKNOWN", sizeof(numaddr));
669 syslog(LOG_NOTICE|LOG_AUTH,
670 "gethostbyaddr: No A record for %s (verifying [%s])",
672 statp->options = old_options;
673 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
674 *h_errnop = statp->res_h_errno;
675 return (NS_NOTFOUND);
677 statp->options = old_options;
678 for (haddr = rhe->h_addr_list; *haddr; haddr++)
679 if (!memcmp(*haddr, addr, NS_INADDRSZ))
682 if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL)
683 strlcpy(numaddr, "UNKNOWN", sizeof(numaddr));
684 syslog(LOG_NOTICE|LOG_AUTH,
685 "gethostbyaddr: A record of %s != PTR record [%s]",
687 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
688 *h_errnop = statp->res_h_errno;
689 return (NS_NOTFOUND);
692 #endif /*SUNSECURITY*/
695 memcpy(hed->host_addr, uaddr, len);
696 hed->h_addr_ptrs[0] = (char *)hed->host_addr;
697 hed->h_addr_ptrs[1] = NULL;
698 if (af == AF_INET && (statp->options & RES_USE_INET6)) {
699 _map_v4v6_address((char*)hed->host_addr, (char*)hed->host_addr);
700 he.h_addrtype = AF_INET6;
701 he.h_length = NS_IN6ADDRSZ;
703 if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
705 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
706 *h_errnop = statp->res_h_errno;
709 RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
710 *((struct hostent **)rval) = hptr;
716 addrsort(char **ap, int num, res_state res)
720 short aval[_MAXADDRS];
724 for (i = 0; i < num; i++, p++) {
725 for (j = 0 ; (unsigned)j < res->nsort; j++)
726 if (res->sort_list[j].addr.s_addr ==
727 (((struct in_addr *)(*p))->s_addr & res->sort_list[j].mask))
730 if (needsort == 0 && i > 0 && j < aval[i-1])
736 while (needsort < num) {
737 for (j = needsort - 1; j >= 0; j--) {
738 if (aval[j] > aval[j+1]) {
758 _sethostdnsent(int stayopen)
762 statp = __res_state();
763 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1)
766 statp->options |= RES_STAYOPEN | RES_USEVC;
774 statp = __res_state();
775 statp->options &= ~(RES_STAYOPEN | RES_USEVC);