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
56 #if defined(LIBC_SCCS) && !defined(lint)
57 static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
58 static char fromrcsid[] = "From: Id: gethnamaddr.c,v 8.23 1998/04/07 04:59:46 vixie Exp $";
59 static char rcsid[] = "$FreeBSD: src/lib/libc/net/gethostbydns.c,v 1.27.2.5 2002/11/02 18:54:57 ume Exp $";
60 #endif /* LIBC_SCCS and not lint */
62 #include <sys/types.h>
63 #include <sys/param.h>
64 #include <sys/socket.h>
65 #include <netinet/in.h>
66 #include <arpa/inet.h>
67 #include <arpa/nameser.h>
79 #include "res_config.h"
81 #define SPRINTF(x) ((size_t)sprintf x)
86 static const char AskedForGot[] =
87 "gethostby*.gethostanswer: asked for \"%s\", got \"%s\"";
89 static char *h_addr_ptrs[MAXADDRS + 1];
91 static struct hostent host;
92 static char *host_aliases[MAXALIASES];
93 static char hostbuf[8*1024];
94 static u_char host_addr[16]; /* IPv4 or IPv6 */
97 static void addrsort __P((char **, int));
100 #define MAXPACKET (64*1024)
104 u_char buf[MAXPACKET];
121 if (_res.options & RES_DEBUG) {
129 # define dprintf(msg, num) /*nada*/
132 #define BOUNDED_INCR(x) \
136 h_errno = NO_RECOVERY; \
141 #define BOUNDS_CHECK(ptr, count) \
143 if ((ptr) + (count) > eom) { \
144 h_errno = NO_RECOVERY; \
149 static struct hostent *
150 gethostanswer(answer, anslen, qname, qtype)
151 const querybuf *answer;
156 register const HEADER *hp;
157 register const u_char *cp;
159 const u_char *eom, *erdata;
160 char *bp, **ap, **hap;
161 int type, class, buflen, ancount, qdcount;
162 int haveanswer, had_error;
166 int (*name_ok) __P((const char *));
170 eom = answer->buf + anslen;
180 h_errno = NO_RECOVERY;
181 return (NULL); /* XXX should be abort(); */
184 * find first satisfactory answer
187 ancount = ntohs(hp->ancount);
188 qdcount = ntohs(hp->qdcount);
190 buflen = sizeof hostbuf;
192 BOUNDED_INCR(HFIXEDSZ);
194 h_errno = NO_RECOVERY;
197 n = dn_expand(answer->buf, eom, cp, bp, buflen);
198 if ((n < 0) || !(*name_ok)(bp)) {
199 h_errno = NO_RECOVERY;
202 BOUNDED_INCR(n + QFIXEDSZ);
203 if (qtype == T_A || qtype == T_AAAA) {
204 /* res_send() has already verified that the query name is the
205 * same as the one we sent; this just gets the expanded name
206 * (i.e., with the succeeding search-domain tacked on).
208 n = strlen(bp) + 1; /* for the \0 */
209 if (n >= MAXHOSTNAMELEN) {
210 h_errno = NO_RECOVERY;
216 /* The qname can be abbreviated, but h_name is now absolute. */
221 host.h_aliases = host_aliases;
224 host.h_addr_list = h_addr_ptrs;
228 while (ancount-- > 0 && cp < eom && !had_error) {
229 n = dn_expand(answer->buf, eom, cp, bp, buflen);
230 if ((n < 0) || !(*name_ok)(bp)) {
235 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
236 type = _getshort(cp);
237 cp += INT16SZ; /* type */
238 class = _getshort(cp);
239 cp += INT16SZ; /* class */
240 if (qtype == T_A && type == T_A)
241 _dns_ttl_ = _getlong(cp);
242 cp += INT32SZ; /* TTL */
244 cp += INT16SZ; /* len */
248 /* XXX - debug? syslog? */
250 continue; /* XXX - had_error++ ? */
252 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
253 if (ap >= &host_aliases[MAXALIASES-1])
255 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
256 if ((n < 0) || !(*name_ok)(tbuf)) {
262 h_errno = NO_RECOVERY;
267 n = strlen(bp) + 1; /* for the \0 */
268 if (n >= MAXHOSTNAMELEN) {
274 /* Get canonical name. */
275 n = strlen(tbuf) + 1; /* for the \0 */
276 if (n > buflen || n >= MAXHOSTNAMELEN) {
286 if (qtype == T_PTR && type == T_CNAME) {
287 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
288 if (n < 0 || !res_dnok(tbuf)) {
294 h_errno = NO_RECOVERY;
297 /* Get canonical name. */
298 n = strlen(tbuf) + 1; /* for the \0 */
299 if (n > buflen || n >= MAXHOSTNAMELEN) {
311 syslog(LOG_NOTICE|LOG_AUTH,
312 "gethostby*.gethostanswer: asked for \"%s %s %s\", got type \"%s\"",
313 qname, p_class(C_IN), p_type(qtype),
316 continue; /* XXX - had_error++ ? */
320 if (strcasecmp(tname, bp) != 0) {
321 syslog(LOG_NOTICE|LOG_AUTH,
322 AskedForGot, qname, bp);
324 continue; /* XXX - had_error++ ? */
326 n = dn_expand(answer->buf, eom, cp, bp, buflen);
327 if ((n < 0) || !res_hnok(bp)) {
331 #if MULTI_PTRS_ARE_ALIASES
334 h_errno = NO_RECOVERY;
339 else if (ap < &host_aliases[MAXALIASES-1])
344 n = strlen(bp) + 1; /* for the \0 */
345 if (n >= MAXHOSTNAMELEN) {
355 if (_res.options & RES_USE_INET6) {
356 n = strlen(bp) + 1; /* for the \0 */
357 if (n >= MAXHOSTNAMELEN) {
363 _map_v4v6_hostent(&host, &bp, &buflen);
365 h_errno = NETDB_SUCCESS;
370 if (strcasecmp(host.h_name, bp) != 0) {
371 syslog(LOG_NOTICE|LOG_AUTH,
372 AskedForGot, host.h_name, bp);
374 continue; /* XXX - had_error++ ? */
376 if (n != host.h_length) {
384 nn = strlen(bp) + 1; /* for the \0 */
389 buflen -= sizeof(align) - ((u_long)bp % sizeof(align));
390 bp += sizeof(align) - ((u_long)bp % sizeof(align));
392 if (bp + n >= &hostbuf[sizeof hostbuf]) {
393 dprintf("size (%d) too big\n", n);
397 if (hap >= &h_addr_ptrs[MAXADDRS-1]) {
399 dprintf("Too many addresses (%d)\n",
404 bcopy(cp, *hap++ = bp, n);
409 h_errno = NO_RECOVERY;
414 dprintf("Impossible condition (type=%d)\n", type);
415 h_errno = NO_RECOVERY;
417 /* BIND has abort() here, too risky on bad data */
425 # if defined(RESOLVSORT)
427 * Note: we sort even if host can take only one address
428 * in its return structures - should give it the "best"
429 * address in that case, not some random one
431 if (_res.nsort && haveanswer > 1 && qtype == T_A)
432 addrsort(h_addr_ptrs, haveanswer);
433 # endif /*RESOLVSORT*/
435 n = strlen(qname) + 1; /* for the \0 */
436 if (n > buflen || n >= MAXHOSTNAMELEN)
443 if (_res.options & RES_USE_INET6)
444 _map_v4v6_hostent(&host, &bp, &buflen);
445 h_errno = NETDB_SUCCESS;
449 h_errno = NO_RECOVERY;
454 __dns_getanswer(answer, anslen, qname, qtype)
462 host.h_addrtype = AF_INET6;
463 host.h_length = IN6ADDRSZ;
467 host.h_addrtype = AF_INET;
468 host.h_length = INADDRSZ;
472 return(gethostanswer((const querybuf *)answer, anslen, qname, qtype));
476 _gethostbydnsname(name, af)
481 register const char *cp;
483 int n, size, type, len;
486 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
487 h_errno = NETDB_INTERNAL;
501 h_errno = NETDB_INTERNAL;
502 errno = EAFNOSUPPORT;
506 host.h_addrtype = af;
507 host.h_length = size;
510 * if there aren't any dots, it could be a user-level alias.
511 * this is also done in res_query() since we are not the only
512 * function that looks up host names.
514 if (!strchr(name, '.') && (cp = __hostalias(name)))
518 * disallow names consisting only of digits/dots, unless
521 if (isdigit((unsigned char)name[0]))
522 for (cp = name;; ++cp) {
527 * All-numeric, no dot at the end.
528 * Fake up a hostent as if we'd actually
531 if (inet_pton(af, name, host_addr) <= 0) {
532 h_errno = HOST_NOT_FOUND;
535 strncpy(hostbuf, name, MAXDNAME);
536 hostbuf[MAXDNAME] = '\0';
537 bp = hostbuf + MAXDNAME;
538 len = sizeof hostbuf - MAXDNAME;
539 host.h_name = hostbuf;
540 host.h_aliases = host_aliases;
541 host_aliases[0] = NULL;
542 h_addr_ptrs[0] = (char *)host_addr;
543 h_addr_ptrs[1] = NULL;
544 host.h_addr_list = h_addr_ptrs;
545 if (_res.options & RES_USE_INET6)
546 _map_v4v6_hostent(&host, &bp, &len);
547 h_errno = NETDB_SUCCESS;
550 if (!isdigit((unsigned char)*cp) && *cp != '.')
553 if ((isxdigit((unsigned char)name[0]) && strchr(name, ':') != NULL) ||
555 for (cp = name;; ++cp) {
560 * All-IPv6-legal, no dot at the end.
561 * Fake up a hostent as if we'd actually
564 if (inet_pton(af, name, host_addr) <= 0) {
565 h_errno = HOST_NOT_FOUND;
568 strncpy(hostbuf, name, MAXDNAME);
569 hostbuf[MAXDNAME] = '\0';
570 bp = hostbuf + MAXDNAME;
571 len = sizeof hostbuf - MAXDNAME;
572 host.h_name = hostbuf;
573 host.h_aliases = host_aliases;
574 host_aliases[0] = NULL;
575 h_addr_ptrs[0] = (char *)host_addr;
576 h_addr_ptrs[1] = NULL;
577 host.h_addr_list = h_addr_ptrs;
578 h_errno = NETDB_SUCCESS;
581 if (!isxdigit((unsigned char)*cp) && *cp != ':' && *cp != '.')
585 if ((buf = malloc(sizeof(*buf))) == NULL) {
586 h_errno = NETDB_INTERNAL;
589 n = res_search(name, C_IN, type, buf->buf, sizeof(buf->buf));
592 dprintf("res_search failed (%d)\n", n);
594 } else if (n > sizeof(buf->buf)) {
596 dprintf("static buffer is too small (%d)\n", n);
599 hp = gethostanswer(buf, n, name, type);
605 _gethostbydnsaddr(addr, len, af)
606 const char *addr; /* XXX should have been def'd as u_char! */
609 const u_char *uaddr = (const u_char *)addr;
610 static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
611 static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
614 register struct hostent *hp;
615 char qbuf[MAXDNAME+1], *qp;
617 register struct hostent *rhp;
620 char hname2[MAXDNAME+1];
621 #endif /*SUNSECURITY*/
623 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
624 h_errno = NETDB_INTERNAL;
627 if (af == AF_INET6 && len == IN6ADDRSZ &&
628 (!bcmp(uaddr, mapped, sizeof mapped) ||
629 !bcmp(uaddr, tunnelled, sizeof tunnelled))) {
631 addr += sizeof mapped;
632 uaddr += sizeof mapped;
644 errno = EAFNOSUPPORT;
645 h_errno = NETDB_INTERNAL;
650 h_errno = NETDB_INTERNAL;
655 (void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
663 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
664 qp += SPRINTF((qp, "%x.%x.",
666 (uaddr[n] >> 4) & 0xf));
668 strlcat(qbuf, "ip6.arpa", sizeof(qbuf));
673 if ((buf = malloc(sizeof(*buf))) == NULL) {
674 h_errno = NETDB_INTERNAL;
677 n = res_query(qbuf, C_IN, T_PTR, (u_char *)buf->buf, sizeof buf->buf);
678 if (n < 0 && af == AF_INET6) {
680 strlcat(qbuf, "ip6.int", sizeof(qbuf));
681 n = res_query(qbuf, C_IN, T_PTR, (u_char *)buf->buf,
686 dprintf("res_query failed (%d)\n", n);
689 if (n > sizeof buf->buf) {
691 dprintf("static buffer is too small (%d)\n", n);
694 if (!(hp = gethostanswer(buf, n, qbuf, T_PTR))) {
696 return (NULL); /* h_errno was set by gethostanswer() */
702 * turn off search as the name should be absolute,
703 * 'localhost' should be matched by defnames
705 strncpy(hname2, hp->h_name, MAXDNAME);
706 hname2[MAXDNAME] = '\0';
707 old_options = _res.options;
708 _res.options &= ~RES_DNSRCH;
709 _res.options |= RES_DEFNAMES;
710 if (!(rhp = gethostbyname(hname2))) {
711 syslog(LOG_NOTICE|LOG_AUTH,
712 "gethostbyaddr: No A record for %s (verifying [%s])",
713 hname2, inet_ntoa(*((struct in_addr *)addr)));
714 _res.options = old_options;
715 h_errno = HOST_NOT_FOUND;
718 _res.options = old_options;
719 for (haddr = rhp->h_addr_list; *haddr; haddr++)
720 if (!memcmp(*haddr, addr, INADDRSZ))
723 syslog(LOG_NOTICE|LOG_AUTH,
724 "gethostbyaddr: A record of %s != PTR record [%s]",
725 hname2, inet_ntoa(*((struct in_addr *)addr)));
726 h_errno = HOST_NOT_FOUND;
730 #endif /*SUNSECURITY*/
733 bcopy(addr, host_addr, len);
734 h_addr_ptrs[0] = (char *)host_addr;
735 h_addr_ptrs[1] = NULL;
736 if (af == AF_INET && (_res.options & RES_USE_INET6)) {
737 _map_v4v6_address((char*)host_addr, (char*)host_addr);
738 hp->h_addrtype = AF_INET6;
739 hp->h_length = IN6ADDRSZ;
741 h_errno = NETDB_SUCCESS;
753 short aval[MAXADDRS];
757 for (i = 0; i < num; i++, p++) {
758 for (j = 0 ; (unsigned)j < _res.nsort; j++)
759 if (_res.sort_list[j].addr.s_addr ==
760 (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask))
763 if (needsort == 0 && i > 0 && j < aval[i-1])
769 while (needsort < num) {
770 for (j = needsort - 1; j >= 0; j--) {
771 if (aval[j] > aval[j+1]) {
790 _sethostdnsent(stayopen)
793 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
796 _res.options |= RES_STAYOPEN | RES_USEVC;
802 _res.options &= ~(RES_STAYOPEN | RES_USEVC);