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 #endif /* LIBC_SCCS and not lint */
60 #include <sys/cdefs.h>
61 __FBSDID("$FreeBSD$");
63 #include <sys/types.h>
64 #include <sys/param.h>
65 #include <sys/socket.h>
66 #include <netinet/in.h>
67 #include <arpa/inet.h>
68 #include <arpa/nameser.h>
81 #include "res_config.h"
83 #define SPRINTF(x) ((size_t)sprintf x)
88 static const char AskedForGot[] =
89 "gethostby*.gethostanswer: asked for \"%s\", got \"%s\"";
91 static char *h_addr_ptrs[MAXADDRS + 1];
93 static struct hostent host;
94 static char *host_aliases[MAXALIASES];
95 static char hostbuf[8*1024];
96 static u_char host_addr[16]; /* IPv4 or IPv6 */
99 static void addrsort(char **, int);
103 static void dprintf(char *, int) __printflike(1, 0);
107 #define MAXPACKET PACKETSZ
109 #define MAXPACKET 1024
114 u_char buf[MAXPACKET];
131 if (_res.options & RES_DEBUG) {
139 # define dprintf(msg, num) /*nada*/
142 #define BOUNDED_INCR(x) \
146 h_errno = NO_RECOVERY; \
151 #define BOUNDS_CHECK(ptr, count) \
153 if ((ptr) + (count) > eom) { \
154 h_errno = NO_RECOVERY; \
159 static struct hostent *
160 gethostanswer(answer, anslen, qname, qtype)
161 const querybuf *answer;
169 const u_char *eom, *erdata;
170 char *bp, *ep, **ap, **hap;
171 int type, class, ancount, qdcount;
172 int haveanswer, had_error;
176 int (*name_ok)(const char *);
180 eom = answer->buf + anslen;
190 h_errno = NO_RECOVERY;
191 return (NULL); /* XXX should be abort(); */
194 * find first satisfactory answer
197 ancount = ntohs(hp->ancount);
198 qdcount = ntohs(hp->qdcount);
200 ep = hostbuf + sizeof hostbuf;
202 BOUNDED_INCR(HFIXEDSZ);
204 h_errno = NO_RECOVERY;
207 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
208 if ((n < 0) || !(*name_ok)(bp)) {
209 h_errno = NO_RECOVERY;
212 BOUNDED_INCR(n + QFIXEDSZ);
213 if (qtype == T_A || qtype == T_AAAA) {
214 /* res_send() has already verified that the query name is the
215 * same as the one we sent; this just gets the expanded name
216 * (i.e., with the succeeding search-domain tacked on).
218 n = strlen(bp) + 1; /* for the \0 */
219 if (n >= MAXHOSTNAMELEN) {
220 h_errno = NO_RECOVERY;
225 /* The qname can be abbreviated, but h_name is now absolute. */
230 host.h_aliases = host_aliases;
233 host.h_addr_list = h_addr_ptrs;
237 while (ancount-- > 0 && cp < eom && !had_error) {
238 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
239 if ((n < 0) || !(*name_ok)(bp)) {
244 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
245 type = _getshort(cp);
246 cp += INT16SZ; /* type */
247 class = _getshort(cp);
248 cp += INT16SZ; /* class */
249 if (qtype == T_A && type == T_A)
250 _dns_ttl_ = _getlong(cp);
251 cp += INT32SZ; /* TTL */
253 cp += INT16SZ; /* len */
257 /* XXX - debug? syslog? */
259 continue; /* XXX - had_error++ ? */
261 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
262 if (ap >= &host_aliases[MAXALIASES-1])
264 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
265 if ((n < 0) || !(*name_ok)(tbuf)) {
271 h_errno = NO_RECOVERY;
276 n = strlen(bp) + 1; /* for the \0 */
277 if (n >= MAXHOSTNAMELEN) {
282 /* Get canonical name. */
283 n = strlen(tbuf) + 1; /* for the \0 */
284 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
293 if (qtype == T_PTR && type == T_CNAME) {
294 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
295 if (n < 0 || !res_dnok(tbuf)) {
301 h_errno = NO_RECOVERY;
304 /* Get canonical name. */
305 n = strlen(tbuf) + 1; /* for the \0 */
306 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
317 syslog(LOG_NOTICE|LOG_AUTH,
318 "gethostby*.gethostanswer: asked for \"%s %s %s\", got type \"%s\"",
319 qname, p_class(C_IN), p_type(qtype),
322 continue; /* XXX - had_error++ ? */
326 if (strcasecmp(tname, bp) != 0) {
327 syslog(LOG_NOTICE|LOG_AUTH,
328 AskedForGot, qname, bp);
330 continue; /* XXX - had_error++ ? */
332 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
333 if ((n < 0) || !res_hnok(bp)) {
337 #if MULTI_PTRS_ARE_ALIASES
340 h_errno = NO_RECOVERY;
345 else if (ap < &host_aliases[MAXALIASES-1])
350 n = strlen(bp) + 1; /* for the \0 */
351 if (n >= MAXHOSTNAMELEN) {
360 if (_res.options & RES_USE_INET6) {
361 n = strlen(bp) + 1; /* for the \0 */
362 if (n >= MAXHOSTNAMELEN) {
367 _map_v4v6_hostent(&host, &bp, &ep);
369 h_errno = NETDB_SUCCESS;
374 if (strcasecmp(host.h_name, bp) != 0) {
375 syslog(LOG_NOTICE|LOG_AUTH,
376 AskedForGot, host.h_name, bp);
378 continue; /* XXX - had_error++ ? */
380 if (n != host.h_length) {
388 nn = strlen(bp) + 1; /* for the \0 */
392 bp += sizeof(align) - ((u_long)bp % sizeof(align));
395 dprintf("size (%d) too big\n", n);
399 if (hap >= &h_addr_ptrs[MAXADDRS-1]) {
401 dprintf("Too many addresses (%d)\n",
406 bcopy(cp, *hap++ = bp, n);
410 h_errno = NO_RECOVERY;
415 dprintf("Impossible condition (type=%d)\n", type);
416 h_errno = NO_RECOVERY;
418 /* BIND has abort() here, too risky on bad data */
426 # if defined(RESOLVSORT)
428 * Note: we sort even if host can take only one address
429 * in its return structures - should give it the "best"
430 * address in that case, not some random one
432 if (_res.nsort && haveanswer > 1 && qtype == T_A)
433 addrsort(h_addr_ptrs, haveanswer);
434 # endif /*RESOLVSORT*/
436 n = strlen(qname) + 1; /* for the \0 */
437 if (n > ep - bp || n >= MAXHOSTNAMELEN)
443 if (_res.options & RES_USE_INET6)
444 _map_v4v6_hostent(&host, &bp, &ep);
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 _dns_gethostbyname(void *rval, void *cb_data, va_list ap)
483 int n, size, type, len;
485 name = va_arg(ap, const char *);
486 af = va_arg(ap, int);
487 *(struct hostent **)rval = NULL;
489 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
490 h_errno = NETDB_INTERNAL;
504 h_errno = NETDB_INTERNAL;
505 errno = EAFNOSUPPORT;
509 host.h_addrtype = af;
510 host.h_length = size;
513 * if there aren't any dots, it could be a user-level alias.
514 * this is also done in res_query() since we are not the only
515 * function that looks up host names.
517 if (!strchr(name, '.') && (cp = __hostalias(name)))
521 * disallow names consisting only of digits/dots, unless
524 if (isdigit((unsigned char)name[0]))
525 for (cp = name;; ++cp) {
530 * All-numeric, no dot at the end.
531 * Fake up a hostent as if we'd actually
534 if (inet_pton(af, name, host_addr) <= 0) {
535 h_errno = HOST_NOT_FOUND;
538 strncpy(hostbuf, name, MAXDNAME);
539 hostbuf[MAXDNAME] = '\0';
540 bp = hostbuf + MAXDNAME;
541 ep = hostbuf + sizeof hostbuf;
542 host.h_name = hostbuf;
543 host.h_aliases = host_aliases;
544 host_aliases[0] = NULL;
545 h_addr_ptrs[0] = (char *)host_addr;
546 h_addr_ptrs[1] = NULL;
547 host.h_addr_list = h_addr_ptrs;
548 if (_res.options & RES_USE_INET6)
549 _map_v4v6_hostent(&host, &bp, &ep);
550 h_errno = NETDB_SUCCESS;
551 *(struct hostent **)rval = &host;
554 if (!isdigit((unsigned char)*cp) && *cp != '.')
557 if ((isxdigit((unsigned char)name[0]) && strchr(name, ':') != NULL) ||
559 for (cp = name;; ++cp) {
564 * All-IPv6-legal, no dot at the end.
565 * Fake up a hostent as if we'd actually
568 if (inet_pton(af, name, host_addr) <= 0) {
569 h_errno = HOST_NOT_FOUND;
572 strncpy(hostbuf, name, MAXDNAME);
573 hostbuf[MAXDNAME] = '\0';
574 bp = hostbuf + MAXDNAME;
575 len = sizeof hostbuf - MAXDNAME;
576 host.h_name = hostbuf;
577 host.h_aliases = host_aliases;
578 host_aliases[0] = NULL;
579 h_addr_ptrs[0] = (char *)host_addr;
580 h_addr_ptrs[1] = NULL;
581 host.h_addr_list = h_addr_ptrs;
582 h_errno = NETDB_SUCCESS;
583 *(struct hostent **)rval = &host;
586 if (!isxdigit((unsigned char)*cp) && *cp != ':' && *cp != '.')
590 if ((n = res_search(name, C_IN, type, buf.buf, sizeof(buf))) < 0) {
591 dprintf("res_search failed (%d)\n", n);
594 *(struct hostent **)rval = gethostanswer(&buf, n, name, type);
595 return (*(struct hostent **)rval != NULL) ? NS_SUCCESS : NS_NOTFOUND;
599 _dns_gethostbyaddr(void *rval, void *cb_data, va_list ap)
601 const char *addr; /* XXX should have been def'd as u_char! */
604 static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
605 static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
609 char qbuf[MAXDNAME+1], *qp;
614 char hname2[MAXDNAME+1];
615 #endif /*SUNSECURITY*/
617 addr = va_arg(ap, const char *);
618 uaddr = (const u_char *)addr;
619 len = va_arg(ap, int);
620 af = va_arg(ap, int);
622 *(struct hostent **)rval = NULL;
624 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
625 h_errno = NETDB_INTERNAL;
628 if (af == AF_INET6 && len == IN6ADDRSZ &&
629 (!bcmp(uaddr, mapped, sizeof mapped) ||
630 !bcmp(uaddr, tunnelled, sizeof tunnelled))) {
632 addr += sizeof mapped;
633 uaddr += sizeof mapped;
645 errno = EAFNOSUPPORT;
646 h_errno = NETDB_INTERNAL;
651 h_errno = NETDB_INTERNAL;
656 (void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
664 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
665 qp += SPRINTF((qp, "%x.%x.",
667 (uaddr[n] >> 4) & 0xf));
669 strcpy(qp, "ip6.int");
674 n = res_query(qbuf, C_IN, T_PTR, (u_char *)buf.buf, sizeof buf.buf);
676 dprintf("res_query failed (%d)\n", n);
679 if (n > sizeof buf.buf) {
680 dprintf("static buffer is too small (%d)\n", n);
683 if (!(hp = gethostanswer(&buf, n, qbuf, T_PTR)))
684 return NS_NOTFOUND; /* h_errno was set by gethostanswer() */
688 * turn off search as the name should be absolute,
689 * 'localhost' should be matched by defnames
691 strncpy(hname2, hp->h_name, MAXDNAME);
692 hname2[MAXDNAME] = '\0';
693 old_options = _res.options;
694 _res.options &= ~RES_DNSRCH;
695 _res.options |= RES_DEFNAMES;
696 if (!(rhp = gethostbyname(hname2))) {
697 syslog(LOG_NOTICE|LOG_AUTH,
698 "gethostbyaddr: No A record for %s (verifying [%s])",
699 hname2, inet_ntoa(*((struct in_addr *)addr)));
700 _res.options = old_options;
701 h_errno = HOST_NOT_FOUND;
704 _res.options = old_options;
705 for (haddr = rhp->h_addr_list; *haddr; haddr++)
706 if (!memcmp(*haddr, addr, INADDRSZ))
709 syslog(LOG_NOTICE|LOG_AUTH,
710 "gethostbyaddr: A record of %s != PTR record [%s]",
711 hname2, inet_ntoa(*((struct in_addr *)addr)));
712 h_errno = HOST_NOT_FOUND;
716 #endif /*SUNSECURITY*/
719 bcopy(addr, host_addr, len);
720 h_addr_ptrs[0] = (char *)host_addr;
721 h_addr_ptrs[1] = NULL;
722 if (af == AF_INET && (_res.options & RES_USE_INET6)) {
723 _map_v4v6_address((char*)host_addr, (char*)host_addr);
724 hp->h_addrtype = AF_INET6;
725 hp->h_length = IN6ADDRSZ;
727 h_errno = NETDB_SUCCESS;
728 *(struct hostent **)rval = hp;
729 return (hp != NULL) ? NS_SUCCESS : NS_NOTFOUND;
740 short aval[MAXADDRS];
744 for (i = 0; i < num; i++, p++) {
745 for (j = 0 ; (unsigned)j < _res.nsort; j++)
746 if (_res.sort_list[j].addr.s_addr ==
747 (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask))
750 if (needsort == 0 && i > 0 && j < aval[i-1])
756 while (needsort < num) {
757 for (j = needsort - 1; j >= 0; j--) {
758 if (aval[j] > aval[j+1]) {
777 _sethostdnsent(stayopen)
780 if ((_res.options & RES_INIT) == 0 && res_init() == -1)
783 _res.options |= RES_STAYOPEN | RES_USEVC;
789 _res.options &= ~(RES_STAYOPEN | RES_USEVC);