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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
37 * Permission to use, copy, modify, and distribute this software for any
38 * purpose with or without fee is hereby granted, provided that the above
39 * copyright notice and this permission notice appear in all copies, and that
40 * the name of Digital Equipment Corporation not be used in advertising or
41 * publicity pertaining to distribution of the document or software without
42 * specific, written prior permission.
44 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
45 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
46 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
47 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
48 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
49 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
50 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
55 * @(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93
56 * $From: Id: gethnamaddr.c,v 8.23 1998/04/07 04:59:46 vixie Exp $
57 * $FreeBSD: src/lib/libc/net/gethostbydns.c,v 1.27.2.5 2002/11/02 18:54:57 ume Exp $
58 * $DragonFly: src/lib/libcr/net/Attic/gethostbydns.c,v 1.4 2004/10/25 19:38:25 drhodus Exp $
61 #include <sys/types.h>
62 #include <sys/param.h>
63 #include <sys/socket.h>
64 #include <netinet/in.h>
65 #include <arpa/inet.h>
66 #include <arpa/nameser.h>
78 #include "res_config.h"
80 #define SPRINTF(x) ((size_t)sprintf x)
85 static const char AskedForGot[] =
86 "gethostby*.gethostanswer: asked for \"%s\", got \"%s\"";
88 static char *h_addr_ptrs[MAXADDRS + 1];
90 static struct hostent host;
91 static char *host_aliases[MAXALIASES];
92 static char hostbuf[8*1024];
93 static u_char host_addr[16]; /* IPv4 or IPv6 */
96 static void addrsort (char **, int);
99 #define MAXPACKET (64*1024)
103 u_char buf[MAXPACKET];
120 if (_res.options & RES_DEBUG) {
128 # define dprintf(msg, num) /*nada*/
131 #define BOUNDED_INCR(x) \
135 h_errno = NO_RECOVERY; \
140 #define BOUNDS_CHECK(ptr, count) \
142 if ((ptr) + (count) > eom) { \
143 h_errno = NO_RECOVERY; \
148 static struct hostent *
149 gethostanswer(answer, anslen, qname, qtype)
150 const querybuf *answer;
158 const u_char *eom, *erdata;
159 char *bp, **ap, **hap;
160 int type, class, buflen, ancount, qdcount;
161 int haveanswer, had_error;
165 int (*name_ok) (const char *);
169 eom = answer->buf + anslen;
179 h_errno = NO_RECOVERY;
180 return (NULL); /* XXX should be abort(); */
183 * find first satisfactory answer
186 ancount = ntohs(hp->ancount);
187 qdcount = ntohs(hp->qdcount);
189 buflen = sizeof hostbuf;
191 BOUNDED_INCR(HFIXEDSZ);
193 h_errno = NO_RECOVERY;
196 n = dn_expand(answer->buf, eom, cp, bp, buflen);
197 if ((n < 0) || !(*name_ok)(bp)) {
198 h_errno = NO_RECOVERY;
201 BOUNDED_INCR(n + QFIXEDSZ);
202 if (qtype == T_A || qtype == T_AAAA) {
203 /* res_send() has already verified that the query name is the
204 * same as the one we sent; this just gets the expanded name
205 * (i.e., with the succeeding search-domain tacked on).
207 n = strlen(bp) + 1; /* for the \0 */
208 if (n >= MAXHOSTNAMELEN) {
209 h_errno = NO_RECOVERY;
215 /* The qname can be abbreviated, but h_name is now absolute. */
220 host.h_aliases = host_aliases;
223 host.h_addr_list = h_addr_ptrs;
227 while (ancount-- > 0 && cp < eom && !had_error) {
228 n = dn_expand(answer->buf, eom, cp, bp, buflen);
229 if ((n < 0) || !(*name_ok)(bp)) {
234 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
235 type = _getshort(cp);
236 cp += INT16SZ; /* type */
237 class = _getshort(cp);
238 cp += INT16SZ; /* class */
239 if (qtype == T_A && type == T_A)
240 _dns_ttl_ = _getlong(cp);
241 cp += INT32SZ; /* TTL */
243 cp += INT16SZ; /* len */
247 /* XXX - debug? syslog? */
249 continue; /* XXX - had_error++ ? */
251 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
252 if (ap >= &host_aliases[MAXALIASES-1])
254 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
255 if ((n < 0) || !(*name_ok)(tbuf)) {
261 h_errno = NO_RECOVERY;
266 n = strlen(bp) + 1; /* for the \0 */
267 if (n >= MAXHOSTNAMELEN) {
273 /* Get canonical name. */
274 n = strlen(tbuf) + 1; /* for the \0 */
275 if (n > buflen || n >= MAXHOSTNAMELEN) {
285 if (qtype == T_PTR && type == T_CNAME) {
286 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
287 if (n < 0 || !res_dnok(tbuf)) {
293 h_errno = NO_RECOVERY;
296 /* Get canonical name. */
297 n = strlen(tbuf) + 1; /* for the \0 */
298 if (n > buflen || n >= MAXHOSTNAMELEN) {
310 syslog(LOG_NOTICE|LOG_AUTH,
311 "gethostby*.gethostanswer: asked for \"%s %s %s\", got type \"%s\"",
312 qname, p_class(C_IN), p_type(qtype),
315 continue; /* XXX - had_error++ ? */
319 if (strcasecmp(tname, bp) != 0) {
320 syslog(LOG_NOTICE|LOG_AUTH,
321 AskedForGot, qname, bp);
323 continue; /* XXX - had_error++ ? */
325 n = dn_expand(answer->buf, eom, cp, bp, buflen);
326 if ((n < 0) || !res_hnok(bp)) {
330 #if MULTI_PTRS_ARE_ALIASES
333 h_errno = NO_RECOVERY;
338 else if (ap < &host_aliases[MAXALIASES-1])
343 n = strlen(bp) + 1; /* for the \0 */
344 if (n >= MAXHOSTNAMELEN) {
354 if (_res.options & RES_USE_INET6) {
355 n = strlen(bp) + 1; /* for the \0 */
356 if (n >= MAXHOSTNAMELEN) {
362 _map_v4v6_hostent(&host, &bp, &buflen);
364 h_errno = NETDB_SUCCESS;
369 if (strcasecmp(host.h_name, bp) != 0) {
370 syslog(LOG_NOTICE|LOG_AUTH,
371 AskedForGot, host.h_name, bp);
373 continue; /* XXX - had_error++ ? */
375 if (n != host.h_length) {
383 nn = strlen(bp) + 1; /* for the \0 */
388 buflen -= sizeof(align) - ((u_long)bp % sizeof(align));
389 bp += sizeof(align) - ((u_long)bp % sizeof(align));
391 if (bp + n >= &hostbuf[sizeof hostbuf]) {
392 dprintf("size (%d) too big\n", n);
396 if (hap >= &h_addr_ptrs[MAXADDRS-1]) {
398 dprintf("Too many addresses (%d)\n",
403 bcopy(cp, *hap++ = bp, n);
408 h_errno = NO_RECOVERY;
413 dprintf("Impossible condition (type=%d)\n", type);
414 h_errno = NO_RECOVERY;
416 /* BIND has abort() here, too risky on bad data */
424 # if defined(RESOLVSORT)
426 * Note: we sort even if host can take only one address
427 * in its return structures - should give it the "best"
428 * address in that case, not some random one
430 if (_res.nsort && haveanswer > 1 && qtype == T_A)
431 addrsort(h_addr_ptrs, haveanswer);
432 # endif /*RESOLVSORT*/
434 n = strlen(qname) + 1; /* for the \0 */
435 if (n > buflen || n >= MAXHOSTNAMELEN)
442 if (_res.options & RES_USE_INET6)
443 _map_v4v6_hostent(&host, &bp, &buflen);
444 h_errno = NETDB_SUCCESS;
448 h_errno = NO_RECOVERY;
453 __dns_getanswer(answer, anslen, qname, qtype)
461 host.h_addrtype = AF_INET6;
462 host.h_length = IN6ADDRSZ;
466 host.h_addrtype = AF_INET;
467 host.h_length = INADDRSZ;
471 return(gethostanswer((const querybuf *)answer, anslen, qname, qtype));
475 _gethostbydnsname(name, af)
482 int n, size, type, len;
485 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
486 h_errno = NETDB_INTERNAL;
500 h_errno = NETDB_INTERNAL;
501 errno = EAFNOSUPPORT;
505 host.h_addrtype = af;
506 host.h_length = size;
509 * if there aren't any dots, it could be a user-level alias.
510 * this is also done in res_query() since we are not the only
511 * function that looks up host names.
513 if (!strchr(name, '.') && (cp = __hostalias(name)))
517 * disallow names consisting only of digits/dots, unless
520 if (isdigit((unsigned char)name[0]))
521 for (cp = name;; ++cp) {
526 * All-numeric, no dot at the end.
527 * Fake up a hostent as if we'd actually
530 if (inet_pton(af, name, host_addr) <= 0) {
531 h_errno = HOST_NOT_FOUND;
534 strncpy(hostbuf, name, MAXDNAME);
535 hostbuf[MAXDNAME] = '\0';
536 bp = hostbuf + MAXDNAME;
537 len = sizeof hostbuf - MAXDNAME;
538 host.h_name = hostbuf;
539 host.h_aliases = host_aliases;
540 host_aliases[0] = NULL;
541 h_addr_ptrs[0] = (char *)host_addr;
542 h_addr_ptrs[1] = NULL;
543 host.h_addr_list = h_addr_ptrs;
544 if (_res.options & RES_USE_INET6)
545 _map_v4v6_hostent(&host, &bp, &len);
546 h_errno = NETDB_SUCCESS;
549 if (!isdigit((unsigned char)*cp) && *cp != '.')
552 if ((isxdigit((unsigned char)name[0]) && strchr(name, ':') != NULL) ||
554 for (cp = name;; ++cp) {
559 * All-IPv6-legal, no dot at the end.
560 * Fake up a hostent as if we'd actually
563 if (inet_pton(af, name, host_addr) <= 0) {
564 h_errno = HOST_NOT_FOUND;
567 strncpy(hostbuf, name, MAXDNAME);
568 hostbuf[MAXDNAME] = '\0';
569 bp = hostbuf + MAXDNAME;
570 len = sizeof hostbuf - MAXDNAME;
571 host.h_name = hostbuf;
572 host.h_aliases = host_aliases;
573 host_aliases[0] = NULL;
574 h_addr_ptrs[0] = (char *)host_addr;
575 h_addr_ptrs[1] = NULL;
576 host.h_addr_list = h_addr_ptrs;
577 h_errno = NETDB_SUCCESS;
580 if (!isxdigit((unsigned char)*cp) && *cp != ':' && *cp != '.')
584 if ((buf = malloc(sizeof(*buf))) == NULL) {
585 h_errno = NETDB_INTERNAL;
588 n = res_search(name, C_IN, type, buf->buf, sizeof(buf->buf));
591 dprintf("res_search failed (%d)\n", n);
593 } else if (n > sizeof(buf->buf)) {
595 dprintf("static buffer is too small (%d)\n", n);
598 hp = gethostanswer(buf, n, name, type);
604 _gethostbydnsaddr(addr, len, af)
605 const char *addr; /* XXX should have been def'd as u_char! */
608 const u_char *uaddr = (const u_char *)addr;
609 static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
610 static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
614 char qbuf[MAXDNAME+1], *qp;
619 char hname2[MAXDNAME+1];
620 #endif /*SUNSECURITY*/
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 (void) 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 (NULL); /* 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;
752 short aval[MAXADDRS];
756 for (i = 0; i < num; i++, p++) {
757 for (j = 0 ; (unsigned)j < _res.nsort; j++)
758 if (_res.sort_list[j].addr.s_addr ==
759 (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask))
762 if (needsort == 0 && i > 0 && j < aval[i-1])
768 while (needsort < num) {
769 for (j = needsort - 1; j >= 0; j--) {
770 if (aval[j] > aval[j+1]) {
789 _sethostdnsent(stayopen)
792 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
795 _res.options |= RES_STAYOPEN | RES_USEVC;
801 _res.options &= ~(RES_STAYOPEN | RES_USEVC);