2 * ++Copyright++ 1985, 1988, 1993
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
51 * @(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93
52 * $From: Id: gethnamaddr.c,v 8.23 1998/04/07 04:59:46 vixie Exp $
53 * $FreeBSD: src/lib/libc/net/gethostbydns.c,v 1.27.2.5 2002/11/02 18:54:57 ume Exp $
54 * $DragonFly: src/lib/libc/net/gethostbydns.c,v 1.6 2005/11/13 02:04:47 swildner Exp $
57 #include <sys/types.h>
58 #include <sys/param.h>
59 #include <sys/socket.h>
60 #include <netinet/in.h>
61 #include <arpa/inet.h>
62 #include <arpa/nameser.h>
76 #include "res_config.h"
78 #define SPRINTF(x) ((size_t)sprintf x)
83 static const char AskedForGot[] =
84 "gethostby*.gethostanswer: asked for \"%s\", got \"%s\"";
86 static char *h_addr_ptrs[MAXADDRS + 1];
88 static struct hostent host;
89 static char *host_aliases[MAXALIASES];
90 static char hostbuf[8*1024];
91 static u_char host_addr[16]; /* IPv4 or IPv6 */
94 static void addrsort (char **, int);
97 #define MAXPACKET (64*1024)
101 u_char buf[MAXPACKET];
114 dprintf(char *msg, int num)
116 if (_res.options & RES_DEBUG) {
124 # define dprintf(msg, num) /*nada*/
127 #define BOUNDED_INCR(x) \
131 h_errno = NO_RECOVERY; \
136 #define BOUNDS_CHECK(ptr, count) \
138 if ((ptr) + (count) > eom) { \
139 h_errno = NO_RECOVERY; \
144 static struct hostent *
145 gethostanswer(const querybuf *answer, int anslen, const char *qname, int qtype)
150 const u_char *eom, *erdata;
151 char *bp, **ap, **hap;
152 int type, class, buflen, ancount, qdcount;
153 int haveanswer, had_error;
157 int (*name_ok) (const char *);
161 eom = answer->buf + anslen;
171 h_errno = NO_RECOVERY;
172 return (NULL); /* XXX should be abort(); */
175 * find first satisfactory answer
178 ancount = ntohs(hp->ancount);
179 qdcount = ntohs(hp->qdcount);
181 buflen = sizeof hostbuf;
183 BOUNDED_INCR(HFIXEDSZ);
185 h_errno = NO_RECOVERY;
188 n = dn_expand(answer->buf, eom, cp, bp, buflen);
189 if ((n < 0) || !(*name_ok)(bp)) {
190 h_errno = NO_RECOVERY;
193 BOUNDED_INCR(n + QFIXEDSZ);
194 if (qtype == T_A || qtype == T_AAAA) {
195 /* res_send() has already verified that the query name is the
196 * same as the one we sent; this just gets the expanded name
197 * (i.e., with the succeeding search-domain tacked on).
199 n = strlen(bp) + 1; /* for the \0 */
200 if (n >= MAXHOSTNAMELEN) {
201 h_errno = NO_RECOVERY;
207 /* The qname can be abbreviated, but h_name is now absolute. */
212 host.h_aliases = host_aliases;
215 host.h_addr_list = h_addr_ptrs;
219 while (ancount-- > 0 && cp < eom && !had_error) {
220 n = dn_expand(answer->buf, eom, cp, bp, buflen);
221 if ((n < 0) || !(*name_ok)(bp)) {
226 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
227 type = _getshort(cp);
228 cp += INT16SZ; /* type */
229 class = _getshort(cp);
230 cp += INT16SZ; /* class */
231 if (qtype == T_A && type == T_A)
232 _dns_ttl_ = _getlong(cp);
233 cp += INT32SZ; /* TTL */
235 cp += INT16SZ; /* len */
239 /* XXX - debug? syslog? */
241 continue; /* XXX - had_error++ ? */
243 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
244 if (ap >= &host_aliases[MAXALIASES-1])
246 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
247 if ((n < 0) || !(*name_ok)(tbuf)) {
253 h_errno = NO_RECOVERY;
258 n = strlen(bp) + 1; /* for the \0 */
259 if (n >= MAXHOSTNAMELEN) {
265 /* Get canonical name. */
266 n = strlen(tbuf) + 1; /* for the \0 */
267 if (n > buflen || n >= MAXHOSTNAMELEN) {
277 if (qtype == T_PTR && type == T_CNAME) {
278 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
279 if (n < 0 || !res_dnok(tbuf)) {
285 h_errno = NO_RECOVERY;
288 /* Get canonical name. */
289 n = strlen(tbuf) + 1; /* for the \0 */
290 if (n > buflen || n >= MAXHOSTNAMELEN) {
302 syslog(LOG_NOTICE|LOG_AUTH,
303 "gethostby*.gethostanswer: asked for \"%s %s %s\", got type \"%s\"",
304 qname, p_class(C_IN), p_type(qtype),
307 continue; /* XXX - had_error++ ? */
311 if (strcasecmp(tname, bp) != 0) {
312 syslog(LOG_NOTICE|LOG_AUTH,
313 AskedForGot, qname, bp);
315 continue; /* XXX - had_error++ ? */
317 n = dn_expand(answer->buf, eom, cp, bp, buflen);
318 if ((n < 0) || !res_hnok(bp)) {
322 #if MULTI_PTRS_ARE_ALIASES
325 h_errno = NO_RECOVERY;
330 else if (ap < &host_aliases[MAXALIASES-1])
335 n = strlen(bp) + 1; /* for the \0 */
336 if (n >= MAXHOSTNAMELEN) {
346 if (_res.options & RES_USE_INET6) {
347 n = strlen(bp) + 1; /* for the \0 */
348 if (n >= MAXHOSTNAMELEN) {
354 _map_v4v6_hostent(&host, &bp, &buflen);
356 h_errno = NETDB_SUCCESS;
361 if (strcasecmp(host.h_name, bp) != 0) {
362 syslog(LOG_NOTICE|LOG_AUTH,
363 AskedForGot, host.h_name, bp);
365 continue; /* XXX - had_error++ ? */
367 if (n != host.h_length) {
375 nn = strlen(bp) + 1; /* for the \0 */
380 buflen -= sizeof(align) - ((u_long)bp % sizeof(align));
381 bp += sizeof(align) - ((u_long)bp % sizeof(align));
383 if (bp + n >= &hostbuf[sizeof hostbuf]) {
384 dprintf("size (%d) too big\n", n);
388 if (hap >= &h_addr_ptrs[MAXADDRS-1]) {
390 dprintf("Too many addresses (%d)\n",
395 bcopy(cp, *hap++ = bp, n);
400 h_errno = NO_RECOVERY;
405 dprintf("Impossible condition (type=%d)\n", type);
406 h_errno = NO_RECOVERY;
408 /* BIND has abort() here, too risky on bad data */
416 # if defined(RESOLVSORT)
418 * Note: we sort even if host can take only one address
419 * in its return structures - should give it the "best"
420 * address in that case, not some random one
422 if (_res.nsort && haveanswer > 1 && qtype == T_A)
423 addrsort(h_addr_ptrs, haveanswer);
424 # endif /*RESOLVSORT*/
426 n = strlen(qname) + 1; /* for the \0 */
427 if (n > buflen || n >= MAXHOSTNAMELEN)
434 if (_res.options & RES_USE_INET6)
435 _map_v4v6_hostent(&host, &bp, &buflen);
436 h_errno = NETDB_SUCCESS;
440 h_errno = NO_RECOVERY;
445 __dns_getanswer(const char *answer, int anslen, const char *qname, int qtype)
449 host.h_addrtype = AF_INET6;
450 host.h_length = IN6ADDRSZ;
454 host.h_addrtype = AF_INET;
455 host.h_length = INADDRSZ;
459 return(gethostanswer((const querybuf *)answer, anslen, qname, qtype));
463 _dns_gethostbyname(void *rval, void *cb_data, va_list ap)
466 const char *cp, *name;
468 int af, n, size, type, len;
471 name = va_arg(ap, const char *);
472 af = va_arg(ap, int);
473 *(struct hostent **)rval = NULL;
475 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
476 h_errno = NETDB_INTERNAL;
490 h_errno = NETDB_INTERNAL;
491 errno = EAFNOSUPPORT;
495 host.h_addrtype = af;
496 host.h_length = size;
499 * if there aren't any dots, it could be a user-level alias.
500 * this is also done in res_query() since we are not the only
501 * function that looks up host names.
503 if (!strchr(name, '.') && (cp = __hostalias(name)))
507 * disallow names consisting only of digits/dots, unless
510 if (isdigit((unsigned char)name[0]))
511 for (cp = name;; ++cp) {
516 * All-numeric, no dot at the end.
517 * Fake up a hostent as if we'd actually
520 if (inet_pton(af, name, host_addr) <= 0) {
521 h_errno = HOST_NOT_FOUND;
524 strncpy(hostbuf, name, MAXDNAME);
525 hostbuf[MAXDNAME] = '\0';
526 bp = hostbuf + MAXDNAME;
527 len = sizeof hostbuf - MAXDNAME;
528 host.h_name = hostbuf;
529 host.h_aliases = host_aliases;
530 host_aliases[0] = NULL;
531 h_addr_ptrs[0] = (char *)host_addr;
532 h_addr_ptrs[1] = NULL;
533 host.h_addr_list = h_addr_ptrs;
534 if (_res.options & RES_USE_INET6)
535 _map_v4v6_hostent(&host, &bp, &len);
536 h_errno = NETDB_SUCCESS;
537 *(struct hostent **)rval = &host;
540 if (!isdigit((unsigned char)*cp) && *cp != '.')
543 if ((isxdigit((unsigned char)name[0]) && strchr(name, ':') != NULL) ||
545 for (cp = name;; ++cp) {
550 * All-IPv6-legal, no dot at the end.
551 * Fake up a hostent as if we'd actually
554 if (inet_pton(af, name, host_addr) <= 0) {
555 h_errno = HOST_NOT_FOUND;
558 strncpy(hostbuf, name, MAXDNAME);
559 hostbuf[MAXDNAME] = '\0';
560 bp = hostbuf + MAXDNAME;
561 len = sizeof hostbuf - MAXDNAME;
562 host.h_name = hostbuf;
563 host.h_aliases = host_aliases;
564 host_aliases[0] = NULL;
565 h_addr_ptrs[0] = (char *)host_addr;
566 h_addr_ptrs[1] = NULL;
567 host.h_addr_list = h_addr_ptrs;
568 h_errno = NETDB_SUCCESS;
569 *(struct hostent **)rval = &host;
572 if (!isxdigit((unsigned char)*cp) && *cp != ':' && *cp != '.')
576 if ((buf = malloc(sizeof(*buf))) == NULL) {
577 h_errno = NETDB_INTERNAL;
580 n = res_search(name, C_IN, type, buf->buf, sizeof(buf->buf));
583 dprintf("res_search failed (%d)\n", n);
585 } else if (n > sizeof(buf->buf)) {
587 dprintf("static buffer is too small (%d)\n", n);
590 *(struct hostent **)rval = gethostanswer(buf, n, name, type);
592 return (*(struct hostent **)rval != NULL) ? NS_SUCCESS : NS_NOTFOUND;
596 _dns_gethostbyaddr(void *rval, void *cb_data, va_list ap)
598 const char *addr; /* XXX should have been def'd as u_char! */
602 static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
603 static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
607 char qbuf[MAXDNAME+1], *qp;
612 char hname2[MAXDNAME+1];
613 #endif /*SUNSECURITY*/
615 addr = va_arg(ap, const char *);
616 uaddr = (const u_char *)addr;
617 len = va_arg(ap, int);
618 af = va_arg(ap, int);
620 *(struct hostent **)rval = NULL;
622 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
623 h_errno = NETDB_INTERNAL;
626 if (af == AF_INET6 && len == IN6ADDRSZ &&
627 (!bcmp(uaddr, mapped, sizeof mapped) ||
628 !bcmp(uaddr, tunnelled, sizeof tunnelled))) {
630 addr += sizeof mapped;
631 uaddr += sizeof mapped;
643 errno = EAFNOSUPPORT;
644 h_errno = NETDB_INTERNAL;
649 h_errno = NETDB_INTERNAL;
654 sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
662 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
663 qp += SPRINTF((qp, "%x.%x.",
665 (uaddr[n] >> 4) & 0xf));
667 strlcat(qbuf, "ip6.arpa", sizeof(qbuf));
672 if ((buf = malloc(sizeof(*buf))) == NULL) {
673 h_errno = NETDB_INTERNAL;
676 n = res_query(qbuf, C_IN, T_PTR, (u_char *)buf->buf, sizeof buf->buf);
677 if (n < 0 && af == AF_INET6) {
679 strlcat(qbuf, "ip6.int", sizeof(qbuf));
680 n = res_query(qbuf, C_IN, T_PTR, (u_char *)buf->buf,
685 dprintf("res_query failed (%d)\n", n);
688 if (n > sizeof buf->buf) {
690 dprintf("static buffer is too small (%d)\n", n);
693 if (!(hp = gethostanswer(buf, n, qbuf, T_PTR))) {
695 return NS_NOTFOUND; /* h_errno was set by gethostanswer() */
701 * turn off search as the name should be absolute,
702 * 'localhost' should be matched by defnames
704 strncpy(hname2, hp->h_name, MAXDNAME);
705 hname2[MAXDNAME] = '\0';
706 old_options = _res.options;
707 _res.options &= ~RES_DNSRCH;
708 _res.options |= RES_DEFNAMES;
709 if (!(rhp = gethostbyname(hname2))) {
710 syslog(LOG_NOTICE|LOG_AUTH,
711 "gethostbyaddr: No A record for %s (verifying [%s])",
712 hname2, inet_ntoa(*((struct in_addr *)addr)));
713 _res.options = old_options;
714 h_errno = HOST_NOT_FOUND;
717 _res.options = old_options;
718 for (haddr = rhp->h_addr_list; *haddr; haddr++)
719 if (!memcmp(*haddr, addr, INADDRSZ))
722 syslog(LOG_NOTICE|LOG_AUTH,
723 "gethostbyaddr: A record of %s != PTR record [%s]",
724 hname2, inet_ntoa(*((struct in_addr *)addr)));
725 h_errno = HOST_NOT_FOUND;
729 #endif /*SUNSECURITY*/
732 bcopy(addr, host_addr, len);
733 h_addr_ptrs[0] = (char *)host_addr;
734 h_addr_ptrs[1] = NULL;
735 if (af == AF_INET && (_res.options & RES_USE_INET6)) {
736 _map_v4v6_address((char*)host_addr, (char*)host_addr);
737 hp->h_addrtype = AF_INET6;
738 hp->h_length = IN6ADDRSZ;
740 h_errno = NETDB_SUCCESS;
741 *(struct hostent **)rval = hp;
742 return (hp != NULL) ? NS_SUCCESS : NS_NOTFOUND;
747 addrsort(char **ap, int num)
751 short aval[MAXADDRS];
755 for (i = 0; i < num; i++, p++) {
756 for (j = 0 ; (unsigned)j < _res.nsort; j++)
757 if (_res.sort_list[j].addr.s_addr ==
758 (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask))
761 if (needsort == 0 && i > 0 && j < aval[i-1])
767 while (needsort < num) {
768 for (j = needsort - 1; j >= 0; j--) {
769 if (aval[j] > aval[j+1]) {
788 _sethostdnsent(int stayopen)
790 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
793 _res.options |= RES_STAYOPEN | RES_USEVC;
799 _res.options &= ~(RES_STAYOPEN | RES_USEVC);