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>
74 #include "res_config.h"
76 #define SPRINTF(x) ((size_t)sprintf x)
81 static const char AskedForGot[] =
82 "gethostby*.gethostanswer: asked for \"%s\", got \"%s\"";
84 static char *h_addr_ptrs[MAXADDRS + 1];
86 static struct hostent host;
87 static char *host_aliases[MAXALIASES];
88 static char hostbuf[8*1024];
89 static u_char host_addr[16]; /* IPv4 or IPv6 */
92 static void addrsort (char **, int);
95 #define MAXPACKET (64*1024)
99 u_char buf[MAXPACKET];
112 dprintf(char *msg, int num)
114 if (_res.options & RES_DEBUG) {
122 # define dprintf(msg, num) /*nada*/
125 #define BOUNDED_INCR(x) \
129 h_errno = NO_RECOVERY; \
134 #define BOUNDS_CHECK(ptr, count) \
136 if ((ptr) + (count) > eom) { \
137 h_errno = NO_RECOVERY; \
142 static struct hostent *
143 gethostanswer(const querybuf *answer, int anslen, const char *qname, int qtype)
148 const u_char *eom, *erdata;
149 char *bp, **ap, **hap;
150 int type, class, buflen, ancount, qdcount;
151 int haveanswer, had_error;
155 int (*name_ok) (const char *);
159 eom = answer->buf + anslen;
169 h_errno = NO_RECOVERY;
170 return (NULL); /* XXX should be abort(); */
173 * find first satisfactory answer
176 ancount = ntohs(hp->ancount);
177 qdcount = ntohs(hp->qdcount);
179 buflen = sizeof hostbuf;
181 BOUNDED_INCR(HFIXEDSZ);
183 h_errno = NO_RECOVERY;
186 n = dn_expand(answer->buf, eom, cp, bp, buflen);
187 if ((n < 0) || !(*name_ok)(bp)) {
188 h_errno = 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 h_errno = NO_RECOVERY;
205 /* The qname can be abbreviated, but h_name is now absolute. */
210 host.h_aliases = host_aliases;
213 host.h_addr_list = h_addr_ptrs;
217 while (ancount-- > 0 && cp < eom && !had_error) {
218 n = dn_expand(answer->buf, eom, cp, bp, buflen);
219 if ((n < 0) || !(*name_ok)(bp)) {
224 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
225 type = _getshort(cp);
226 cp += INT16SZ; /* type */
227 class = _getshort(cp);
228 cp += INT16SZ; /* class */
229 if (qtype == T_A && type == T_A)
230 _dns_ttl_ = _getlong(cp);
231 cp += INT32SZ; /* TTL */
233 cp += INT16SZ; /* len */
237 /* XXX - debug? syslog? */
239 continue; /* XXX - had_error++ ? */
241 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
242 if (ap >= &host_aliases[MAXALIASES-1])
244 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
245 if ((n < 0) || !(*name_ok)(tbuf)) {
251 h_errno = NO_RECOVERY;
256 n = strlen(bp) + 1; /* for the \0 */
257 if (n >= MAXHOSTNAMELEN) {
263 /* Get canonical name. */
264 n = strlen(tbuf) + 1; /* for the \0 */
265 if (n > buflen || n >= MAXHOSTNAMELEN) {
275 if (qtype == T_PTR && type == T_CNAME) {
276 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
277 if (n < 0 || !res_dnok(tbuf)) {
283 h_errno = NO_RECOVERY;
286 /* Get canonical name. */
287 n = strlen(tbuf) + 1; /* for the \0 */
288 if (n > buflen || n >= MAXHOSTNAMELEN) {
300 syslog(LOG_NOTICE|LOG_AUTH,
301 "gethostby*.gethostanswer: asked for \"%s %s %s\", got type \"%s\"",
302 qname, p_class(C_IN), p_type(qtype),
305 continue; /* XXX - had_error++ ? */
309 if (strcasecmp(tname, bp) != 0) {
310 syslog(LOG_NOTICE|LOG_AUTH,
311 AskedForGot, qname, bp);
313 continue; /* XXX - had_error++ ? */
315 n = dn_expand(answer->buf, eom, cp, bp, buflen);
316 if ((n < 0) || !res_hnok(bp)) {
320 #if MULTI_PTRS_ARE_ALIASES
323 h_errno = NO_RECOVERY;
328 else if (ap < &host_aliases[MAXALIASES-1])
333 n = strlen(bp) + 1; /* for the \0 */
334 if (n >= MAXHOSTNAMELEN) {
344 if (_res.options & RES_USE_INET6) {
345 n = strlen(bp) + 1; /* for the \0 */
346 if (n >= MAXHOSTNAMELEN) {
352 _map_v4v6_hostent(&host, &bp, &buflen);
354 h_errno = NETDB_SUCCESS;
359 if (strcasecmp(host.h_name, bp) != 0) {
360 syslog(LOG_NOTICE|LOG_AUTH,
361 AskedForGot, host.h_name, bp);
363 continue; /* XXX - had_error++ ? */
365 if (n != host.h_length) {
373 nn = strlen(bp) + 1; /* for the \0 */
378 buflen -= sizeof(align) - ((u_long)bp % sizeof(align));
379 bp += sizeof(align) - ((u_long)bp % sizeof(align));
381 if (bp + n >= &hostbuf[sizeof hostbuf]) {
382 dprintf("size (%d) too big\n", n);
386 if (hap >= &h_addr_ptrs[MAXADDRS-1]) {
388 dprintf("Too many addresses (%d)\n",
393 bcopy(cp, *hap++ = bp, n);
398 h_errno = NO_RECOVERY;
403 dprintf("Impossible condition (type=%d)\n", type);
404 h_errno = NO_RECOVERY;
406 /* BIND has abort() here, too risky on bad data */
414 # if defined(RESOLVSORT)
416 * Note: we sort even if host can take only one address
417 * in its return structures - should give it the "best"
418 * address in that case, not some random one
420 if (_res.nsort && haveanswer > 1 && qtype == T_A)
421 addrsort(h_addr_ptrs, haveanswer);
422 # endif /*RESOLVSORT*/
424 n = strlen(qname) + 1; /* for the \0 */
425 if (n > buflen || n >= MAXHOSTNAMELEN)
432 if (_res.options & RES_USE_INET6)
433 _map_v4v6_hostent(&host, &bp, &buflen);
434 h_errno = NETDB_SUCCESS;
438 h_errno = NO_RECOVERY;
443 __dns_getanswer(const char *answer, int anslen, const char *qname, int qtype)
447 host.h_addrtype = AF_INET6;
448 host.h_length = IN6ADDRSZ;
452 host.h_addrtype = AF_INET;
453 host.h_length = INADDRSZ;
457 return(gethostanswer((const querybuf *)answer, anslen, qname, qtype));
461 _gethostbydnsname(const char *name, int af)
466 int n, size, type, len;
469 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
470 h_errno = NETDB_INTERNAL;
484 h_errno = NETDB_INTERNAL;
485 errno = EAFNOSUPPORT;
489 host.h_addrtype = af;
490 host.h_length = size;
493 * if there aren't any dots, it could be a user-level alias.
494 * this is also done in res_query() since we are not the only
495 * function that looks up host names.
497 if (!strchr(name, '.') && (cp = __hostalias(name)))
501 * disallow names consisting only of digits/dots, unless
504 if (isdigit((unsigned char)name[0]))
505 for (cp = name;; ++cp) {
510 * All-numeric, no dot at the end.
511 * Fake up a hostent as if we'd actually
514 if (inet_pton(af, name, host_addr) <= 0) {
515 h_errno = HOST_NOT_FOUND;
518 strncpy(hostbuf, name, MAXDNAME);
519 hostbuf[MAXDNAME] = '\0';
520 bp = hostbuf + MAXDNAME;
521 len = sizeof hostbuf - MAXDNAME;
522 host.h_name = hostbuf;
523 host.h_aliases = host_aliases;
524 host_aliases[0] = NULL;
525 h_addr_ptrs[0] = (char *)host_addr;
526 h_addr_ptrs[1] = NULL;
527 host.h_addr_list = h_addr_ptrs;
528 if (_res.options & RES_USE_INET6)
529 _map_v4v6_hostent(&host, &bp, &len);
530 h_errno = NETDB_SUCCESS;
533 if (!isdigit((unsigned char)*cp) && *cp != '.')
536 if ((isxdigit((unsigned char)name[0]) && strchr(name, ':') != NULL) ||
538 for (cp = name;; ++cp) {
543 * All-IPv6-legal, no dot at the end.
544 * Fake up a hostent as if we'd actually
547 if (inet_pton(af, name, host_addr) <= 0) {
548 h_errno = HOST_NOT_FOUND;
551 strncpy(hostbuf, name, MAXDNAME);
552 hostbuf[MAXDNAME] = '\0';
553 bp = hostbuf + MAXDNAME;
554 len = sizeof hostbuf - MAXDNAME;
555 host.h_name = hostbuf;
556 host.h_aliases = host_aliases;
557 host_aliases[0] = NULL;
558 h_addr_ptrs[0] = (char *)host_addr;
559 h_addr_ptrs[1] = NULL;
560 host.h_addr_list = h_addr_ptrs;
561 h_errno = NETDB_SUCCESS;
564 if (!isxdigit((unsigned char)*cp) && *cp != ':' && *cp != '.')
568 if ((buf = malloc(sizeof(*buf))) == NULL) {
569 h_errno = NETDB_INTERNAL;
572 n = res_search(name, C_IN, type, buf->buf, sizeof(buf->buf));
575 dprintf("res_search failed (%d)\n", n);
577 } else if (n > sizeof(buf->buf)) {
579 dprintf("static buffer is too small (%d)\n", n);
582 hp = gethostanswer(buf, n, name, type);
588 _gethostbydnsaddr(const char *addr, /* XXX should have been def'd as u_char! */
592 const u_char *uaddr = (const u_char *)addr;
593 static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
594 static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
598 char qbuf[MAXDNAME+1], *qp;
603 char hname2[MAXDNAME+1];
604 #endif /*SUNSECURITY*/
606 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
607 h_errno = NETDB_INTERNAL;
610 if (af == AF_INET6 && len == IN6ADDRSZ &&
611 (!bcmp(uaddr, mapped, sizeof mapped) ||
612 !bcmp(uaddr, tunnelled, sizeof tunnelled))) {
614 addr += sizeof mapped;
615 uaddr += sizeof mapped;
627 errno = EAFNOSUPPORT;
628 h_errno = NETDB_INTERNAL;
633 h_errno = NETDB_INTERNAL;
638 sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
646 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
647 qp += SPRINTF((qp, "%x.%x.",
649 (uaddr[n] >> 4) & 0xf));
651 strlcat(qbuf, "ip6.arpa", sizeof(qbuf));
656 if ((buf = malloc(sizeof(*buf))) == NULL) {
657 h_errno = NETDB_INTERNAL;
660 n = res_query(qbuf, C_IN, T_PTR, (u_char *)buf->buf, sizeof buf->buf);
661 if (n < 0 && af == AF_INET6) {
663 strlcat(qbuf, "ip6.int", sizeof(qbuf));
664 n = res_query(qbuf, C_IN, T_PTR, (u_char *)buf->buf,
669 dprintf("res_query failed (%d)\n", n);
672 if (n > sizeof buf->buf) {
674 dprintf("static buffer is too small (%d)\n", n);
677 if (!(hp = gethostanswer(buf, n, qbuf, T_PTR))) {
679 return (NULL); /* h_errno was set by gethostanswer() */
685 * turn off search as the name should be absolute,
686 * 'localhost' should be matched by defnames
688 strncpy(hname2, hp->h_name, MAXDNAME);
689 hname2[MAXDNAME] = '\0';
690 old_options = _res.options;
691 _res.options &= ~RES_DNSRCH;
692 _res.options |= RES_DEFNAMES;
693 if (!(rhp = gethostbyname(hname2))) {
694 syslog(LOG_NOTICE|LOG_AUTH,
695 "gethostbyaddr: No A record for %s (verifying [%s])",
696 hname2, inet_ntoa(*((struct in_addr *)addr)));
697 _res.options = old_options;
698 h_errno = HOST_NOT_FOUND;
701 _res.options = old_options;
702 for (haddr = rhp->h_addr_list; *haddr; haddr++)
703 if (!memcmp(*haddr, addr, INADDRSZ))
706 syslog(LOG_NOTICE|LOG_AUTH,
707 "gethostbyaddr: A record of %s != PTR record [%s]",
708 hname2, inet_ntoa(*((struct in_addr *)addr)));
709 h_errno = HOST_NOT_FOUND;
713 #endif /*SUNSECURITY*/
716 bcopy(addr, host_addr, len);
717 h_addr_ptrs[0] = (char *)host_addr;
718 h_addr_ptrs[1] = NULL;
719 if (af == AF_INET && (_res.options & RES_USE_INET6)) {
720 _map_v4v6_address((char*)host_addr, (char*)host_addr);
721 hp->h_addrtype = AF_INET6;
722 hp->h_length = IN6ADDRSZ;
724 h_errno = NETDB_SUCCESS;
730 addrsort(char **ap, int num)
734 short aval[MAXADDRS];
738 for (i = 0; i < num; i++, p++) {
739 for (j = 0 ; (unsigned)j < _res.nsort; j++)
740 if (_res.sort_list[j].addr.s_addr ==
741 (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask))
744 if (needsort == 0 && i > 0 && j < aval[i-1])
750 while (needsort < num) {
751 for (j = needsort - 1; j >= 0; j--) {
752 if (aval[j] > aval[j+1]) {
771 _sethostdnsent(int stayopen)
773 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
776 _res.options |= RES_STAYOPEN | RES_USEVC;
782 _res.options &= ~(RES_STAYOPEN | RES_USEVC);