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.5 2005/09/19 09:34:53 asmodai 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];
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(answer, anslen, qname, qtype)
146 const querybuf *answer;
154 const u_char *eom, *erdata;
155 char *bp, **ap, **hap;
156 int type, class, buflen, ancount, qdcount;
157 int haveanswer, had_error;
161 int (*name_ok) (const char *);
165 eom = answer->buf + anslen;
175 h_errno = NO_RECOVERY;
176 return (NULL); /* XXX should be abort(); */
179 * find first satisfactory answer
182 ancount = ntohs(hp->ancount);
183 qdcount = ntohs(hp->qdcount);
185 buflen = sizeof hostbuf;
187 BOUNDED_INCR(HFIXEDSZ);
189 h_errno = NO_RECOVERY;
192 n = dn_expand(answer->buf, eom, cp, bp, buflen);
193 if ((n < 0) || !(*name_ok)(bp)) {
194 h_errno = NO_RECOVERY;
197 BOUNDED_INCR(n + QFIXEDSZ);
198 if (qtype == T_A || qtype == T_AAAA) {
199 /* res_send() has already verified that the query name is the
200 * same as the one we sent; this just gets the expanded name
201 * (i.e., with the succeeding search-domain tacked on).
203 n = strlen(bp) + 1; /* for the \0 */
204 if (n >= MAXHOSTNAMELEN) {
205 h_errno = NO_RECOVERY;
211 /* The qname can be abbreviated, but h_name is now absolute. */
216 host.h_aliases = host_aliases;
219 host.h_addr_list = h_addr_ptrs;
223 while (ancount-- > 0 && cp < eom && !had_error) {
224 n = dn_expand(answer->buf, eom, cp, bp, buflen);
225 if ((n < 0) || !(*name_ok)(bp)) {
230 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
231 type = _getshort(cp);
232 cp += INT16SZ; /* type */
233 class = _getshort(cp);
234 cp += INT16SZ; /* class */
235 if (qtype == T_A && type == T_A)
236 _dns_ttl_ = _getlong(cp);
237 cp += INT32SZ; /* TTL */
239 cp += INT16SZ; /* len */
243 /* XXX - debug? syslog? */
245 continue; /* XXX - had_error++ ? */
247 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
248 if (ap >= &host_aliases[MAXALIASES-1])
250 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
251 if ((n < 0) || !(*name_ok)(tbuf)) {
257 h_errno = NO_RECOVERY;
262 n = strlen(bp) + 1; /* for the \0 */
263 if (n >= MAXHOSTNAMELEN) {
269 /* Get canonical name. */
270 n = strlen(tbuf) + 1; /* for the \0 */
271 if (n > buflen || n >= MAXHOSTNAMELEN) {
281 if (qtype == T_PTR && type == T_CNAME) {
282 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
283 if (n < 0 || !res_dnok(tbuf)) {
289 h_errno = NO_RECOVERY;
292 /* Get canonical name. */
293 n = strlen(tbuf) + 1; /* for the \0 */
294 if (n > buflen || n >= MAXHOSTNAMELEN) {
306 syslog(LOG_NOTICE|LOG_AUTH,
307 "gethostby*.gethostanswer: asked for \"%s %s %s\", got type \"%s\"",
308 qname, p_class(C_IN), p_type(qtype),
311 continue; /* XXX - had_error++ ? */
315 if (strcasecmp(tname, bp) != 0) {
316 syslog(LOG_NOTICE|LOG_AUTH,
317 AskedForGot, qname, bp);
319 continue; /* XXX - had_error++ ? */
321 n = dn_expand(answer->buf, eom, cp, bp, buflen);
322 if ((n < 0) || !res_hnok(bp)) {
326 #if MULTI_PTRS_ARE_ALIASES
329 h_errno = NO_RECOVERY;
334 else if (ap < &host_aliases[MAXALIASES-1])
339 n = strlen(bp) + 1; /* for the \0 */
340 if (n >= MAXHOSTNAMELEN) {
350 if (_res.options & RES_USE_INET6) {
351 n = strlen(bp) + 1; /* for the \0 */
352 if (n >= MAXHOSTNAMELEN) {
358 _map_v4v6_hostent(&host, &bp, &buflen);
360 h_errno = NETDB_SUCCESS;
365 if (strcasecmp(host.h_name, bp) != 0) {
366 syslog(LOG_NOTICE|LOG_AUTH,
367 AskedForGot, host.h_name, bp);
369 continue; /* XXX - had_error++ ? */
371 if (n != host.h_length) {
379 nn = strlen(bp) + 1; /* for the \0 */
384 buflen -= sizeof(align) - ((u_long)bp % sizeof(align));
385 bp += sizeof(align) - ((u_long)bp % sizeof(align));
387 if (bp + n >= &hostbuf[sizeof hostbuf]) {
388 dprintf("size (%d) too big\n", n);
392 if (hap >= &h_addr_ptrs[MAXADDRS-1]) {
394 dprintf("Too many addresses (%d)\n",
399 bcopy(cp, *hap++ = bp, n);
404 h_errno = NO_RECOVERY;
409 dprintf("Impossible condition (type=%d)\n", type);
410 h_errno = NO_RECOVERY;
412 /* BIND has abort() here, too risky on bad data */
420 # if defined(RESOLVSORT)
422 * Note: we sort even if host can take only one address
423 * in its return structures - should give it the "best"
424 * address in that case, not some random one
426 if (_res.nsort && haveanswer > 1 && qtype == T_A)
427 addrsort(h_addr_ptrs, haveanswer);
428 # endif /*RESOLVSORT*/
430 n = strlen(qname) + 1; /* for the \0 */
431 if (n > buflen || n >= MAXHOSTNAMELEN)
438 if (_res.options & RES_USE_INET6)
439 _map_v4v6_hostent(&host, &bp, &buflen);
440 h_errno = NETDB_SUCCESS;
444 h_errno = NO_RECOVERY;
449 __dns_getanswer(answer, anslen, qname, qtype)
457 host.h_addrtype = AF_INET6;
458 host.h_length = IN6ADDRSZ;
462 host.h_addrtype = AF_INET;
463 host.h_length = INADDRSZ;
467 return(gethostanswer((const querybuf *)answer, anslen, qname, qtype));
471 _gethostbydnsname(name, af)
478 int n, size, type, len;
481 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
482 h_errno = NETDB_INTERNAL;
496 h_errno = NETDB_INTERNAL;
497 errno = EAFNOSUPPORT;
501 host.h_addrtype = af;
502 host.h_length = size;
505 * if there aren't any dots, it could be a user-level alias.
506 * this is also done in res_query() since we are not the only
507 * function that looks up host names.
509 if (!strchr(name, '.') && (cp = __hostalias(name)))
513 * disallow names consisting only of digits/dots, unless
516 if (isdigit((unsigned char)name[0]))
517 for (cp = name;; ++cp) {
522 * All-numeric, no dot at the end.
523 * Fake up a hostent as if we'd actually
526 if (inet_pton(af, name, host_addr) <= 0) {
527 h_errno = HOST_NOT_FOUND;
530 strncpy(hostbuf, name, MAXDNAME);
531 hostbuf[MAXDNAME] = '\0';
532 bp = hostbuf + MAXDNAME;
533 len = sizeof hostbuf - MAXDNAME;
534 host.h_name = hostbuf;
535 host.h_aliases = host_aliases;
536 host_aliases[0] = NULL;
537 h_addr_ptrs[0] = (char *)host_addr;
538 h_addr_ptrs[1] = NULL;
539 host.h_addr_list = h_addr_ptrs;
540 if (_res.options & RES_USE_INET6)
541 _map_v4v6_hostent(&host, &bp, &len);
542 h_errno = NETDB_SUCCESS;
545 if (!isdigit((unsigned char)*cp) && *cp != '.')
548 if ((isxdigit((unsigned char)name[0]) && strchr(name, ':') != NULL) ||
550 for (cp = name;; ++cp) {
555 * All-IPv6-legal, no dot at the end.
556 * Fake up a hostent as if we'd actually
559 if (inet_pton(af, name, host_addr) <= 0) {
560 h_errno = HOST_NOT_FOUND;
563 strncpy(hostbuf, name, MAXDNAME);
564 hostbuf[MAXDNAME] = '\0';
565 bp = hostbuf + MAXDNAME;
566 len = sizeof hostbuf - MAXDNAME;
567 host.h_name = hostbuf;
568 host.h_aliases = host_aliases;
569 host_aliases[0] = NULL;
570 h_addr_ptrs[0] = (char *)host_addr;
571 h_addr_ptrs[1] = NULL;
572 host.h_addr_list = h_addr_ptrs;
573 h_errno = NETDB_SUCCESS;
576 if (!isxdigit((unsigned char)*cp) && *cp != ':' && *cp != '.')
580 if ((buf = malloc(sizeof(*buf))) == NULL) {
581 h_errno = NETDB_INTERNAL;
584 n = res_search(name, C_IN, type, buf->buf, sizeof(buf->buf));
587 dprintf("res_search failed (%d)\n", n);
589 } else if (n > sizeof(buf->buf)) {
591 dprintf("static buffer is too small (%d)\n", n);
594 hp = gethostanswer(buf, n, name, type);
600 _gethostbydnsaddr(addr, len, af)
601 const char *addr; /* XXX should have been def'd as u_char! */
604 const u_char *uaddr = (const u_char *)addr;
605 static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
606 static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
610 char qbuf[MAXDNAME+1], *qp;
615 char hname2[MAXDNAME+1];
616 #endif /*SUNSECURITY*/
618 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
619 h_errno = NETDB_INTERNAL;
622 if (af == AF_INET6 && len == IN6ADDRSZ &&
623 (!bcmp(uaddr, mapped, sizeof mapped) ||
624 !bcmp(uaddr, tunnelled, sizeof tunnelled))) {
626 addr += sizeof mapped;
627 uaddr += sizeof mapped;
639 errno = EAFNOSUPPORT;
640 h_errno = NETDB_INTERNAL;
645 h_errno = NETDB_INTERNAL;
650 (void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
658 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
659 qp += SPRINTF((qp, "%x.%x.",
661 (uaddr[n] >> 4) & 0xf));
663 strlcat(qbuf, "ip6.arpa", sizeof(qbuf));
668 if ((buf = malloc(sizeof(*buf))) == NULL) {
669 h_errno = NETDB_INTERNAL;
672 n = res_query(qbuf, C_IN, T_PTR, (u_char *)buf->buf, sizeof buf->buf);
673 if (n < 0 && af == AF_INET6) {
675 strlcat(qbuf, "ip6.int", sizeof(qbuf));
676 n = res_query(qbuf, C_IN, T_PTR, (u_char *)buf->buf,
681 dprintf("res_query failed (%d)\n", n);
684 if (n > sizeof buf->buf) {
686 dprintf("static buffer is too small (%d)\n", n);
689 if (!(hp = gethostanswer(buf, n, qbuf, T_PTR))) {
691 return (NULL); /* h_errno was set by gethostanswer() */
697 * turn off search as the name should be absolute,
698 * 'localhost' should be matched by defnames
700 strncpy(hname2, hp->h_name, MAXDNAME);
701 hname2[MAXDNAME] = '\0';
702 old_options = _res.options;
703 _res.options &= ~RES_DNSRCH;
704 _res.options |= RES_DEFNAMES;
705 if (!(rhp = gethostbyname(hname2))) {
706 syslog(LOG_NOTICE|LOG_AUTH,
707 "gethostbyaddr: No A record for %s (verifying [%s])",
708 hname2, inet_ntoa(*((struct in_addr *)addr)));
709 _res.options = old_options;
710 h_errno = HOST_NOT_FOUND;
713 _res.options = old_options;
714 for (haddr = rhp->h_addr_list; *haddr; haddr++)
715 if (!memcmp(*haddr, addr, INADDRSZ))
718 syslog(LOG_NOTICE|LOG_AUTH,
719 "gethostbyaddr: A record of %s != PTR record [%s]",
720 hname2, inet_ntoa(*((struct in_addr *)addr)));
721 h_errno = HOST_NOT_FOUND;
725 #endif /*SUNSECURITY*/
728 bcopy(addr, host_addr, len);
729 h_addr_ptrs[0] = (char *)host_addr;
730 h_addr_ptrs[1] = NULL;
731 if (af == AF_INET && (_res.options & RES_USE_INET6)) {
732 _map_v4v6_address((char*)host_addr, (char*)host_addr);
733 hp->h_addrtype = AF_INET6;
734 hp->h_length = IN6ADDRSZ;
736 h_errno = NETDB_SUCCESS;
748 short aval[MAXADDRS];
752 for (i = 0; i < num; i++, p++) {
753 for (j = 0 ; (unsigned)j < _res.nsort; j++)
754 if (_res.sort_list[j].addr.s_addr ==
755 (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask))
758 if (needsort == 0 && i > 0 && j < aval[i-1])
764 while (needsort < num) {
765 for (j = needsort - 1; j >= 0; j--) {
766 if (aval[j] > aval[j+1]) {
785 _sethostdnsent(stayopen)
788 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
791 _res.options |= RES_STAYOPEN | RES_USEVC;
797 _res.options &= ~(RES_STAYOPEN | RES_USEVC);