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>
82 #include "netdb_private.h"
83 #include "res_config.h"
85 #define SPRINTF(x) ((size_t)sprintf x)
87 static const char AskedForGot[] =
88 "gethostby*.gethostanswer: asked for \"%s\", got \"%s\"";
91 static void addrsort(char **, int, res_state);
95 static void dprintf(char *, int, res_state) __printflike(1, 0);
98 #define MAXPACKET (64*1024)
102 u_char buf[MAXPACKET];
114 dprintf(msg, num, res)
119 if (res->options & RES_DEBUG) {
127 # define dprintf(msg, num, res) /*nada*/
130 #define BOUNDED_INCR(x) \
134 RES_SET_H_ERRNO(statp, NO_RECOVERY); \
139 #define BOUNDS_CHECK(ptr, count) \
141 if ((ptr) + (count) > eom) { \
142 RES_SET_H_ERRNO(statp, NO_RECOVERY); \
148 gethostanswer(const querybuf *answer, int anslen, const char *qname, int qtype,
149 struct hostent *he, struct hostent_data *hed, res_state statp)
154 const u_char *eom, *erdata;
155 char *bp, *ep, **ap, **hap;
156 int type, class, ancount, qdcount;
157 int haveanswer, had_error;
161 int (*name_ok)(const char *);
165 eom = answer->buf + anslen;
175 RES_SET_H_ERRNO(statp, NO_RECOVERY);
176 return (-1); /* XXX should be abort(); */
179 * find first satisfactory answer
182 ancount = ntohs(hp->ancount);
183 qdcount = ntohs(hp->qdcount);
185 ep = hed->hostbuf + sizeof hed->hostbuf;
187 BOUNDED_INCR(HFIXEDSZ);
189 RES_SET_H_ERRNO(statp, NO_RECOVERY);
192 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
193 if ((n < 0) || !(*name_ok)(bp)) {
194 RES_SET_H_ERRNO(statp, 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 RES_SET_H_ERRNO(statp, NO_RECOVERY);
210 /* The qname can be abbreviated, but h_name is now absolute. */
213 ap = hed->host_aliases;
215 he->h_aliases = hed->host_aliases;
216 hap = hed->h_addr_ptrs;
218 he->h_addr_list = hed->h_addr_ptrs;
222 while (ancount-- > 0 && cp < eom && !had_error) {
223 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
224 if ((n < 0) || !(*name_ok)(bp)) {
229 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
230 type = _getshort(cp);
231 cp += INT16SZ; /* type */
232 class = _getshort(cp);
233 cp += INT16SZ; /* class */
234 if (qtype == T_A && type == T_A)
235 _dns_ttl_ = _getlong(cp);
236 cp += INT32SZ; /* TTL */
238 cp += INT16SZ; /* len */
242 /* XXX - debug? syslog? */
244 continue; /* XXX - had_error++ ? */
246 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
247 if (ap >= &hed->host_aliases[_MAXALIASES-1])
249 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
250 if ((n < 0) || !(*name_ok)(tbuf)) {
256 RES_SET_H_ERRNO(statp, NO_RECOVERY);
261 n = strlen(bp) + 1; /* for the \0 */
262 if (n >= MAXHOSTNAMELEN) {
267 /* Get canonical name. */
268 n = strlen(tbuf) + 1; /* for the \0 */
269 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
278 if (qtype == T_PTR && type == T_CNAME) {
279 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
280 if (n < 0 || !res_dnok(tbuf)) {
286 RES_SET_H_ERRNO(statp, NO_RECOVERY);
289 /* Get canonical name. */
290 n = strlen(tbuf) + 1; /* for the \0 */
291 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
302 syslog(LOG_NOTICE|LOG_AUTH,
303 "gethostby*.gethostanswer: asked for \"%s %s %s\", got type \"%s\"",
304 qname, p_class(C_IN), p_type(qtype),
307 continue; /* XXX - had_error++ ? */
311 if (strcasecmp(tname, bp) != 0) {
312 syslog(LOG_NOTICE|LOG_AUTH,
313 AskedForGot, qname, bp);
315 continue; /* XXX - had_error++ ? */
317 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
318 if ((n < 0) || !res_hnok(bp)) {
322 #if MULTI_PTRS_ARE_ALIASES
325 RES_SET_H_ERRNO(statp, NO_RECOVERY);
330 else if (ap < &hed->host_aliases[_MAXALIASES-1])
335 n = strlen(bp) + 1; /* for the \0 */
336 if (n >= MAXHOSTNAMELEN) {
345 if (statp->options & RES_USE_INET6) {
346 n = strlen(bp) + 1; /* for the \0 */
347 if (n >= MAXHOSTNAMELEN) {
352 _map_v4v6_hostent(he, &bp, ep);
354 RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
359 if (strcasecmp(he->h_name, bp) != 0) {
360 syslog(LOG_NOTICE|LOG_AUTH,
361 AskedForGot, he->h_name, bp);
363 continue; /* XXX - had_error++ ? */
365 if (n != he->h_length) {
373 nn = strlen(bp) + 1; /* for the \0 */
377 bp += sizeof(align) - ((u_long)bp % sizeof(align));
380 dprintf("size (%d) too big\n", n, statp);
384 if (hap >= &hed->h_addr_ptrs[_MAXADDRS-1]) {
386 dprintf("Too many addresses (%d)\n",
391 memcpy(*hap++ = bp, cp, n);
395 RES_SET_H_ERRNO(statp, NO_RECOVERY);
400 dprintf("Impossible condition (type=%d)\n", type,
402 RES_SET_H_ERRNO(statp, NO_RECOVERY);
404 /* BIND has abort() here, too risky on bad data */
412 # if defined(RESOLVSORT)
414 * Note: we sort even if host can take only one address
415 * in its return structures - should give it the "best"
416 * address in that case, not some random one
418 if (statp->nsort && haveanswer > 1 && qtype == T_A)
419 addrsort(hed->h_addr_ptrs, haveanswer, statp);
420 # endif /*RESOLVSORT*/
422 n = strlen(qname) + 1; /* for the \0 */
423 if (n > ep - bp || n >= MAXHOSTNAMELEN)
429 if (statp->options & RES_USE_INET6)
430 _map_v4v6_hostent(he, &bp, ep);
431 RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
435 RES_SET_H_ERRNO(statp, NO_RECOVERY);
439 /* XXX: for async DNS resolver in ypserv */
441 __dns_getanswer(const char *answer, int anslen, const char *qname, int qtype)
444 struct hostent_data *hed;
448 statp = __res_state();
449 if ((he = __hostent_init()) == NULL ||
450 (hed = __hostent_data_init()) == NULL) {
451 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
456 he->h_addrtype = AF_INET6;
457 he->h_length = NS_IN6ADDRSZ;
461 he->h_addrtype = AF_INET;
462 he->h_length = NS_INADDRSZ;
466 error = gethostanswer((const querybuf *)answer, anslen, qname, qtype,
468 return (error == 0) ? he : NULL;
472 _dns_gethostbyname(void *rval, void *cb_data, va_list ap)
478 int *errnop, *h_errnop;
479 struct hostent *hptr, he;
480 struct hostent_data *hed;
485 name = va_arg(ap, const char *);
486 af = va_arg(ap, int);
487 hptr = va_arg(ap, struct hostent *);
488 buffer = va_arg(ap, char *);
489 buflen = va_arg(ap, size_t);
490 errnop = va_arg(ap, int *);
491 h_errnop = va_arg(ap, int *);
493 *((struct hostent **)rval) = NULL;
495 statp = __res_state();
496 if ((hed = __hostent_data_init()) == NULL) {
497 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
498 *h_errnop = statp->res_h_errno;
499 return (NS_NOTFOUND);
505 he.h_length = NS_INADDRSZ;
509 he.h_length = NS_IN6ADDRSZ;
513 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
514 *h_errnop = statp->res_h_errno;
515 errno = EAFNOSUPPORT;
519 if ((buf = malloc(sizeof(*buf))) == NULL) {
520 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
521 *h_errnop = statp->res_h_errno;
522 return (NS_NOTFOUND);
524 n = res_nsearch(statp, name, C_IN, type, buf->buf, sizeof(buf->buf));
527 dprintf("res_nsearch failed (%d)\n", n, statp);
528 *h_errnop = statp->res_h_errno;
530 } else if (n > sizeof(buf->buf)) {
532 dprintf("static buffer is too small (%d)\n", n, statp);
533 *h_errnop = statp->res_h_errno;
536 error = gethostanswer(buf, n, name, type, &he, hed, statp);
539 *h_errnop = statp->res_h_errno;
540 return (NS_NOTFOUND);
542 if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
543 *h_errnop = statp->res_h_errno;
544 return (NS_NOTFOUND);
546 *((struct hostent **)rval) = hptr;
551 _dns_gethostbyaddr(void *rval, void *cb_data, va_list ap)
558 int *errnop, *h_errnop;
560 struct hostent *hptr, he;
561 struct hostent_data *hed;
564 char qbuf[MAXDNAME+1], *qp;
571 char hname2[MAXDNAME+1], numaddr[46];
573 #endif /*SUNSECURITY*/
575 addr = va_arg(ap, const void *);
576 len = va_arg(ap, socklen_t);
577 af = va_arg(ap, int);
578 hptr = va_arg(ap, struct hostent *);
579 buffer = va_arg(ap, char *);
580 buflen = va_arg(ap, size_t);
581 errnop = va_arg(ap, int *);
582 h_errnop = va_arg(ap, int *);
583 uaddr = (const u_char *)addr;
585 *((struct hostent **)rval) = NULL;
587 statp = __res_state();
588 if ((hed = __hostent_data_init()) == NULL) {
589 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
590 *h_errnop = statp->res_h_errno;
591 return (NS_NOTFOUND);
596 (void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
604 for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) {
605 qp += SPRINTF((qp, "%x.%x.",
607 (uaddr[n] >> 4) & 0xf));
609 strlcat(qbuf, "ip6.arpa", sizeof(qbuf));
614 if ((buf = malloc(sizeof(*buf))) == NULL) {
615 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
616 *h_errnop = statp->res_h_errno;
619 n = res_nquery(statp, qbuf, C_IN, T_PTR, (u_char *)buf->buf,
623 dprintf("res_nquery failed (%d)\n", n, statp);
624 *h_errnop = statp->res_h_errno;
627 if (n > sizeof buf->buf) {
629 dprintf("static buffer is too small (%d)\n", n, statp);
630 *h_errnop = statp->res_h_errno;
633 if (gethostanswer(buf, n, qbuf, T_PTR, &he, hed, statp) != 0) {
635 *h_errnop = statp->res_h_errno;
636 return (NS_NOTFOUND); /* h_errno was set by gethostanswer() */
642 * turn off search as the name should be absolute,
643 * 'localhost' should be matched by defnames
645 strncpy(hname2, he.h_name, MAXDNAME);
646 hname2[MAXDNAME] = '\0';
647 old_options = statp->options;
648 statp->options &= ~RES_DNSRCH;
649 statp->options |= RES_DEFNAMES;
650 memset(&rhd, 0, sizeof rhd);
651 rhe = gethostbyname_r(hname2, &rhd.host, &rhd.data,
652 sizeof(rhd.data), &ret_h_error);
654 if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL)
655 strlcpy(numaddr, "UNKNOWN", sizeof(numaddr));
656 syslog(LOG_NOTICE|LOG_AUTH,
657 "gethostbyaddr: No A record for %s (verifying [%s])",
659 statp->options = old_options;
660 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
661 *h_errnop = statp->res_h_errno;
662 return (NS_NOTFOUND);
664 statp->options = old_options;
665 for (haddr = rhe->h_addr_list; *haddr; haddr++)
666 if (!memcmp(*haddr, addr, NS_INADDRSZ))
669 if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL)
670 strlcpy(numaddr, "UNKNOWN", sizeof(numaddr));
671 syslog(LOG_NOTICE|LOG_AUTH,
672 "gethostbyaddr: A record of %s != PTR record [%s]",
674 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
675 *h_errnop = statp->res_h_errno;
676 return (NS_NOTFOUND);
679 #endif /*SUNSECURITY*/
682 memcpy(hed->host_addr, uaddr, len);
683 hed->h_addr_ptrs[0] = (char *)hed->host_addr;
684 hed->h_addr_ptrs[1] = NULL;
685 if (af == AF_INET && (statp->options & RES_USE_INET6)) {
686 _map_v4v6_address((char*)hed->host_addr, (char*)hed->host_addr);
687 he.h_addrtype = AF_INET6;
688 he.h_length = NS_IN6ADDRSZ;
690 RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
691 if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
692 *h_errnop = statp->res_h_errno;
693 return (NS_NOTFOUND);
695 *((struct hostent **)rval) = hptr;
701 addrsort(char **ap, int num, res_state res)
705 short aval[_MAXADDRS];
709 for (i = 0; i < num; i++, p++) {
710 for (j = 0 ; (unsigned)j < res->nsort; j++)
711 if (res->sort_list[j].addr.s_addr ==
712 (((struct in_addr *)(*p))->s_addr & res->sort_list[j].mask))
715 if (needsort == 0 && i > 0 && j < aval[i-1])
721 while (needsort < num) {
722 for (j = needsort - 1; j >= 0; j--) {
723 if (aval[j] > aval[j+1]) {
743 _sethostdnsent(int stayopen)
747 statp = __res_state();
748 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1)
751 statp->options |= RES_STAYOPEN | RES_USEVC;
759 statp = __res_state();
760 statp->options &= ~(RES_STAYOPEN | RES_USEVC);