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
52 #if defined(LIBC_SCCS) && !defined(lint)
53 static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
54 static char fromrcsid[] = "From: Id: gethnamaddr.c,v 8.23 1998/04/07 04:59:46 vixie Exp $";
55 #endif /* LIBC_SCCS and not lint */
56 #include <sys/cdefs.h>
57 __FBSDID("$FreeBSD$");
59 #include <sys/param.h>
60 #include <sys/socket.h>
61 #include <netinet/in.h>
62 #include <arpa/inet.h>
63 #include <arpa/nameser.h>
77 #include "netdb_private.h"
78 #include "res_config.h"
80 #define SPRINTF(x) ((size_t)sprintf x)
82 static const char AskedForGot[] =
83 "gethostby*.gethostanswer: asked for \"%s\", got \"%s\"";
86 static void addrsort(char **, int, res_state);
90 static void dbg_printf(char *, int, res_state) __printflike(1, 0);
93 #define MAXPACKET (64*1024)
97 u_char buf[MAXPACKET];
109 dbg_printf(char *msg, int num, res_state res)
111 if (res->options & RES_DEBUG) {
119 # define dbg_printf(msg, num, res) /*nada*/
122 #define BOUNDED_INCR(x) \
126 RES_SET_H_ERRNO(statp, NO_RECOVERY); \
131 #define BOUNDS_CHECK(ptr, count) \
133 if ((ptr) + (count) > eom) { \
134 RES_SET_H_ERRNO(statp, NO_RECOVERY); \
140 gethostanswer(const querybuf *answer, int anslen, const char *qname, int qtype,
141 struct hostent *he, struct hostent_data *hed, res_state statp)
146 const u_char *eom, *erdata;
147 char *bp, *ep, **ap, **hap;
148 int type, class, ancount, qdcount;
149 int haveanswer, had_error;
153 int (*name_ok)(const char *);
157 eom = answer->buf + anslen;
167 RES_SET_H_ERRNO(statp, NO_RECOVERY);
168 return (-1); /* XXX should be abort(); */
171 * find first satisfactory answer
174 ancount = ntohs(hp->ancount);
175 qdcount = ntohs(hp->qdcount);
177 ep = hed->hostbuf + sizeof hed->hostbuf;
179 BOUNDED_INCR(HFIXEDSZ);
181 RES_SET_H_ERRNO(statp, NO_RECOVERY);
184 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
185 if ((n < 0) || !(*name_ok)(bp)) {
186 RES_SET_H_ERRNO(statp, NO_RECOVERY);
189 BOUNDED_INCR(n + QFIXEDSZ);
190 if (qtype == T_A || qtype == T_AAAA) {
191 /* res_send() has already verified that the query name is the
192 * same as the one we sent; this just gets the expanded name
193 * (i.e., with the succeeding search-domain tacked on).
195 n = strlen(bp) + 1; /* for the \0 */
196 if (n >= MAXHOSTNAMELEN) {
197 RES_SET_H_ERRNO(statp, NO_RECOVERY);
202 /* The qname can be abbreviated, but h_name is now absolute. */
205 ap = hed->host_aliases;
207 he->h_aliases = hed->host_aliases;
208 hap = hed->h_addr_ptrs;
210 he->h_addr_list = hed->h_addr_ptrs;
214 while (ancount-- > 0 && cp < eom && !had_error) {
215 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
216 if ((n < 0) || !(*name_ok)(bp)) {
221 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
222 type = _getshort(cp);
223 cp += INT16SZ; /* type */
224 class = _getshort(cp);
225 cp += INT16SZ; /* class */
226 if (qtype == T_A && type == T_A)
227 _dns_ttl_ = _getlong(cp);
228 cp += INT32SZ; /* TTL */
230 cp += INT16SZ; /* len */
234 /* XXX - debug? syslog? */
236 continue; /* XXX - had_error++ ? */
238 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
239 if (ap >= &hed->host_aliases[_MAXALIASES-1])
241 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
242 if ((n < 0) || !(*name_ok)(tbuf)) {
248 RES_SET_H_ERRNO(statp, NO_RECOVERY);
253 n = strlen(bp) + 1; /* for the \0 */
254 if (n >= MAXHOSTNAMELEN) {
259 /* Get canonical name. */
260 n = strlen(tbuf) + 1; /* for the \0 */
261 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
270 if (qtype == T_PTR && type == T_CNAME) {
271 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
272 if (n < 0 || !res_dnok(tbuf)) {
278 RES_SET_H_ERRNO(statp, NO_RECOVERY);
281 /* Get canonical name. */
282 n = strlen(tbuf) + 1; /* for the \0 */
283 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
293 if (type != T_SIG && type != ns_t_dname)
294 syslog(LOG_NOTICE|LOG_AUTH,
295 "gethostby*.gethostanswer: asked for \"%s %s %s\", got type \"%s\"",
296 qname, p_class(C_IN), p_type(qtype),
299 continue; /* XXX - had_error++ ? */
303 if (strcasecmp(tname, bp) != 0) {
304 syslog(LOG_NOTICE|LOG_AUTH,
305 AskedForGot, qname, bp);
307 continue; /* XXX - had_error++ ? */
309 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
310 if ((n < 0) || !res_hnok(bp)) {
314 #if MULTI_PTRS_ARE_ALIASES
317 RES_SET_H_ERRNO(statp, NO_RECOVERY);
322 else if (ap < &hed->host_aliases[_MAXALIASES-1])
327 n = strlen(bp) + 1; /* for the \0 */
328 if (n >= MAXHOSTNAMELEN) {
337 if (statp->options & RES_USE_INET6) {
338 n = strlen(bp) + 1; /* for the \0 */
339 if (n >= MAXHOSTNAMELEN) {
344 _map_v4v6_hostent(he, &bp, ep);
346 RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
351 if (strcasecmp(he->h_name, bp) != 0) {
352 syslog(LOG_NOTICE|LOG_AUTH,
353 AskedForGot, he->h_name, bp);
355 continue; /* XXX - had_error++ ? */
357 if (n != he->h_length) {
365 nn = strlen(bp) + 1; /* for the \0 */
369 bp += sizeof(align) - ((u_long)bp % sizeof(align));
372 dbg_printf("size (%d) too big\n", n, statp);
376 if (hap >= &hed->h_addr_ptrs[_MAXADDRS-1]) {
378 dbg_printf("Too many addresses (%d)\n",
383 memcpy(*hap++ = bp, cp, n);
387 RES_SET_H_ERRNO(statp, NO_RECOVERY);
392 dbg_printf("Impossible condition (type=%d)\n", type,
394 RES_SET_H_ERRNO(statp, NO_RECOVERY);
396 /* BIND has abort() here, too risky on bad data */
404 # if defined(RESOLVSORT)
406 * Note: we sort even if host can take only one address
407 * in its return structures - should give it the "best"
408 * address in that case, not some random one
410 if (statp->nsort && haveanswer > 1 && qtype == T_A)
411 addrsort(hed->h_addr_ptrs, haveanswer, statp);
412 # endif /*RESOLVSORT*/
414 n = strlen(qname) + 1; /* for the \0 */
415 if (n > ep - bp || n >= MAXHOSTNAMELEN)
421 if (statp->options & RES_USE_INET6)
422 _map_v4v6_hostent(he, &bp, ep);
423 RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
427 RES_SET_H_ERRNO(statp, NO_RECOVERY);
431 /* XXX: for async DNS resolver in ypserv */
433 __dns_getanswer(const char *answer, int anslen, const char *qname, int qtype)
436 struct hostent_data *hed;
440 statp = __res_state();
441 if ((he = __hostent_init()) == NULL ||
442 (hed = __hostent_data_init()) == NULL) {
443 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
448 he->h_addrtype = AF_INET6;
449 he->h_length = NS_IN6ADDRSZ;
453 he->h_addrtype = AF_INET;
454 he->h_length = NS_INADDRSZ;
458 error = gethostanswer((const querybuf *)answer, anslen, qname, qtype,
460 return (error == 0) ? he : NULL;
464 _dns_gethostbyname(void *rval, void *cb_data, va_list ap)
470 int *errnop, *h_errnop;
471 struct hostent *hptr, he;
472 struct hostent_data *hed;
477 name = va_arg(ap, const char *);
478 af = va_arg(ap, int);
479 hptr = va_arg(ap, struct hostent *);
480 buffer = va_arg(ap, char *);
481 buflen = va_arg(ap, size_t);
482 errnop = va_arg(ap, int *);
483 h_errnop = va_arg(ap, int *);
485 *((struct hostent **)rval) = NULL;
487 statp = __res_state();
488 if ((hed = __hostent_data_init()) == NULL) {
489 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
490 *h_errnop = statp->res_h_errno;
491 return (NS_NOTFOUND);
497 he.h_length = NS_INADDRSZ;
501 he.h_length = NS_IN6ADDRSZ;
505 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
506 *h_errnop = statp->res_h_errno;
507 errno = EAFNOSUPPORT;
511 if ((buf = malloc(sizeof(*buf))) == NULL) {
512 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
513 *h_errnop = statp->res_h_errno;
514 return (NS_NOTFOUND);
516 n = res_nsearch(statp, name, C_IN, type, buf->buf, sizeof(buf->buf));
519 dbg_printf("res_nsearch failed (%d)\n", n, statp);
520 *h_errnop = statp->res_h_errno;
521 return (NS_NOTFOUND);
522 } else if (n > sizeof(buf->buf)) {
524 dbg_printf("static buffer is too small (%d)\n", n, statp);
525 *h_errnop = statp->res_h_errno;
528 error = gethostanswer(buf, n, name, type, &he, hed, statp);
531 *h_errnop = statp->res_h_errno;
532 switch (statp->res_h_errno) {
534 return (NS_NOTFOUND);
536 return (NS_TRYAGAIN);
542 if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
544 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
545 *h_errnop = statp->res_h_errno;
548 RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
549 *((struct hostent **)rval) = hptr;
554 _dns_gethostbyaddr(void *rval, void *cb_data, va_list ap)
561 int *errnop, *h_errnop;
563 struct hostent *hptr, he;
564 struct hostent_data *hed;
567 char qbuf[MAXDNAME+1], *qp;
574 char hname2[MAXDNAME+1], numaddr[46];
576 #endif /*SUNSECURITY*/
578 addr = va_arg(ap, const void *);
579 len = va_arg(ap, socklen_t);
580 af = va_arg(ap, int);
581 hptr = va_arg(ap, struct hostent *);
582 buffer = va_arg(ap, char *);
583 buflen = va_arg(ap, size_t);
584 errnop = va_arg(ap, int *);
585 h_errnop = va_arg(ap, int *);
586 uaddr = (const u_char *)addr;
588 *((struct hostent **)rval) = NULL;
590 statp = __res_state();
591 if ((hed = __hostent_data_init()) == NULL) {
592 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
593 *h_errnop = statp->res_h_errno;
594 return (NS_NOTFOUND);
599 (void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
607 for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) {
608 qp += SPRINTF((qp, "%x.%x.",
610 (uaddr[n] >> 4) & 0xf));
612 strlcat(qbuf, "ip6.arpa", sizeof(qbuf));
617 if ((buf = malloc(sizeof(*buf))) == NULL) {
618 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
619 *h_errnop = statp->res_h_errno;
622 n = res_nquery(statp, qbuf, C_IN, T_PTR, (u_char *)buf->buf,
626 dbg_printf("res_nquery failed (%d)\n", n, statp);
627 *h_errnop = statp->res_h_errno;
630 if (n > sizeof buf->buf) {
632 dbg_printf("static buffer is too small (%d)\n", n, statp);
633 *h_errnop = statp->res_h_errno;
636 if (gethostanswer(buf, n, qbuf, T_PTR, &he, hed, statp) != 0) {
638 *h_errnop = statp->res_h_errno;
639 switch (statp->res_h_errno) {
641 return (NS_NOTFOUND);
643 return (NS_TRYAGAIN);
653 * turn off search as the name should be absolute,
654 * 'localhost' should be matched by defnames
656 strncpy(hname2, he.h_name, MAXDNAME);
657 hname2[MAXDNAME] = '\0';
658 old_options = statp->options;
659 statp->options &= ~RES_DNSRCH;
660 statp->options |= RES_DEFNAMES;
661 memset(&rhd, 0, sizeof rhd);
662 rhe = gethostbyname_r(hname2, &rhd.host, &rhd.data,
663 sizeof(rhd.data), &ret_h_error);
665 if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL)
666 strlcpy(numaddr, "UNKNOWN", sizeof(numaddr));
667 syslog(LOG_NOTICE|LOG_AUTH,
668 "gethostbyaddr: No A record for %s (verifying [%s])",
670 statp->options = old_options;
671 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
672 *h_errnop = statp->res_h_errno;
673 return (NS_NOTFOUND);
675 statp->options = old_options;
676 for (haddr = rhe->h_addr_list; *haddr; haddr++)
677 if (!memcmp(*haddr, addr, NS_INADDRSZ))
680 if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL)
681 strlcpy(numaddr, "UNKNOWN", sizeof(numaddr));
682 syslog(LOG_NOTICE|LOG_AUTH,
683 "gethostbyaddr: A record of %s != PTR record [%s]",
685 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
686 *h_errnop = statp->res_h_errno;
687 return (NS_NOTFOUND);
690 #endif /*SUNSECURITY*/
693 memcpy(hed->host_addr, uaddr, len);
694 hed->h_addr_ptrs[0] = (char *)hed->host_addr;
695 hed->h_addr_ptrs[1] = NULL;
696 if (af == AF_INET && (statp->options & RES_USE_INET6)) {
697 _map_v4v6_address((char*)hed->host_addr, (char*)hed->host_addr);
698 he.h_addrtype = AF_INET6;
699 he.h_length = NS_IN6ADDRSZ;
701 if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
703 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
704 *h_errnop = statp->res_h_errno;
707 RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
708 *((struct hostent **)rval) = hptr;
714 addrsort(char **ap, int num, res_state res)
718 short aval[_MAXADDRS];
722 for (i = 0; i < num; i++, p++) {
723 for (j = 0 ; (unsigned)j < res->nsort; j++)
724 if (res->sort_list[j].addr.s_addr ==
725 (((struct in_addr *)(*p))->s_addr & res->sort_list[j].mask))
728 if (needsort == 0 && i > 0 && j < aval[i-1])
734 while (needsort < num) {
735 for (j = needsort - 1; j >= 0; j--) {
736 if (aval[j] > aval[j+1]) {
756 _sethostdnsent(int stayopen)
760 statp = __res_state();
761 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1)
764 statp->options |= RES_STAYOPEN | RES_USEVC;
772 statp = __res_state();
773 statp->options &= ~(RES_STAYOPEN | RES_USEVC);