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 * 4. 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/types.h>
60 #include <sys/param.h>
61 #include <sys/socket.h>
62 #include <netinet/in.h>
63 #include <arpa/inet.h>
64 #include <arpa/nameser.h>
78 #include "netdb_private.h"
79 #include "res_config.h"
81 #define SPRINTF(x) ((size_t)sprintf x)
83 static const char AskedForGot[] =
84 "gethostby*.gethostanswer: asked for \"%s\", got \"%s\"";
87 static void addrsort(char **, int, res_state);
91 static void dprintf(char *, int, res_state) __printflike(1, 0);
94 #define MAXPACKET (64*1024)
98 u_char buf[MAXPACKET];
110 dprintf(char *msg, int num, res_state res)
112 if (res->options & RES_DEBUG) {
120 # define dprintf(msg, num, res) /*nada*/
123 #define BOUNDED_INCR(x) \
127 RES_SET_H_ERRNO(statp, NO_RECOVERY); \
132 #define BOUNDS_CHECK(ptr, count) \
134 if ((ptr) + (count) > eom) { \
135 RES_SET_H_ERRNO(statp, NO_RECOVERY); \
141 gethostanswer(const querybuf *answer, int anslen, const char *qname, int qtype,
142 struct hostent *he, struct hostent_data *hed, res_state statp)
147 const u_char *eom, *erdata;
148 char *bp, *ep, **ap, **hap;
149 int type, class, ancount, qdcount;
150 int haveanswer, had_error;
154 int (*name_ok)(const char *);
158 eom = answer->buf + anslen;
168 RES_SET_H_ERRNO(statp, NO_RECOVERY);
169 return (-1); /* XXX should be abort(); */
172 * find first satisfactory answer
175 ancount = ntohs(hp->ancount);
176 qdcount = ntohs(hp->qdcount);
178 ep = hed->hostbuf + sizeof hed->hostbuf;
180 BOUNDED_INCR(HFIXEDSZ);
182 RES_SET_H_ERRNO(statp, NO_RECOVERY);
185 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
186 if ((n < 0) || !(*name_ok)(bp)) {
187 RES_SET_H_ERRNO(statp, NO_RECOVERY);
190 BOUNDED_INCR(n + QFIXEDSZ);
191 if (qtype == T_A || qtype == T_AAAA) {
192 /* res_send() has already verified that the query name is the
193 * same as the one we sent; this just gets the expanded name
194 * (i.e., with the succeeding search-domain tacked on).
196 n = strlen(bp) + 1; /* for the \0 */
197 if (n >= MAXHOSTNAMELEN) {
198 RES_SET_H_ERRNO(statp, NO_RECOVERY);
203 /* The qname can be abbreviated, but h_name is now absolute. */
206 ap = hed->host_aliases;
208 he->h_aliases = hed->host_aliases;
209 hap = hed->h_addr_ptrs;
211 he->h_addr_list = hed->h_addr_ptrs;
215 while (ancount-- > 0 && cp < eom && !had_error) {
216 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
217 if ((n < 0) || !(*name_ok)(bp)) {
222 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
223 type = _getshort(cp);
224 cp += INT16SZ; /* type */
225 class = _getshort(cp);
226 cp += INT16SZ; /* class */
227 if (qtype == T_A && type == T_A)
228 _dns_ttl_ = _getlong(cp);
229 cp += INT32SZ; /* TTL */
231 cp += INT16SZ; /* len */
235 /* XXX - debug? syslog? */
237 continue; /* XXX - had_error++ ? */
239 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
240 if (ap >= &hed->host_aliases[_MAXALIASES-1])
242 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
243 if ((n < 0) || !(*name_ok)(tbuf)) {
249 RES_SET_H_ERRNO(statp, NO_RECOVERY);
254 n = strlen(bp) + 1; /* for the \0 */
255 if (n >= MAXHOSTNAMELEN) {
260 /* Get canonical name. */
261 n = strlen(tbuf) + 1; /* for the \0 */
262 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
271 if (qtype == T_PTR && type == T_CNAME) {
272 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
273 if (n < 0 || !res_dnok(tbuf)) {
279 RES_SET_H_ERRNO(statp, NO_RECOVERY);
282 /* Get canonical name. */
283 n = strlen(tbuf) + 1; /* for the \0 */
284 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
294 if (type != T_SIG && type != ns_t_dname)
295 syslog(LOG_NOTICE|LOG_AUTH,
296 "gethostby*.gethostanswer: asked for \"%s %s %s\", got type \"%s\"",
297 qname, p_class(C_IN), p_type(qtype),
300 continue; /* XXX - had_error++ ? */
304 if (strcasecmp(tname, bp) != 0) {
305 syslog(LOG_NOTICE|LOG_AUTH,
306 AskedForGot, qname, bp);
308 continue; /* XXX - had_error++ ? */
310 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
311 if ((n < 0) || !res_hnok(bp)) {
315 #if MULTI_PTRS_ARE_ALIASES
318 RES_SET_H_ERRNO(statp, NO_RECOVERY);
323 else if (ap < &hed->host_aliases[_MAXALIASES-1])
328 n = strlen(bp) + 1; /* for the \0 */
329 if (n >= MAXHOSTNAMELEN) {
338 if (statp->options & RES_USE_INET6) {
339 n = strlen(bp) + 1; /* for the \0 */
340 if (n >= MAXHOSTNAMELEN) {
345 _map_v4v6_hostent(he, &bp, ep);
347 RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
352 if (strcasecmp(he->h_name, bp) != 0) {
353 syslog(LOG_NOTICE|LOG_AUTH,
354 AskedForGot, he->h_name, bp);
356 continue; /* XXX - had_error++ ? */
358 if (n != he->h_length) {
366 nn = strlen(bp) + 1; /* for the \0 */
370 bp += sizeof(align) - ((u_long)bp % sizeof(align));
373 dprintf("size (%d) too big\n", n, statp);
377 if (hap >= &hed->h_addr_ptrs[_MAXADDRS-1]) {
379 dprintf("Too many addresses (%d)\n",
384 memcpy(*hap++ = bp, cp, n);
388 RES_SET_H_ERRNO(statp, NO_RECOVERY);
393 dprintf("Impossible condition (type=%d)\n", type,
395 RES_SET_H_ERRNO(statp, NO_RECOVERY);
397 /* BIND has abort() here, too risky on bad data */
405 # if defined(RESOLVSORT)
407 * Note: we sort even if host can take only one address
408 * in its return structures - should give it the "best"
409 * address in that case, not some random one
411 if (statp->nsort && haveanswer > 1 && qtype == T_A)
412 addrsort(hed->h_addr_ptrs, haveanswer, statp);
413 # endif /*RESOLVSORT*/
415 n = strlen(qname) + 1; /* for the \0 */
416 if (n > ep - bp || n >= MAXHOSTNAMELEN)
422 if (statp->options & RES_USE_INET6)
423 _map_v4v6_hostent(he, &bp, ep);
424 RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
428 RES_SET_H_ERRNO(statp, NO_RECOVERY);
432 /* XXX: for async DNS resolver in ypserv */
434 __dns_getanswer(const char *answer, int anslen, const char *qname, int qtype)
437 struct hostent_data *hed;
441 statp = __res_state();
442 if ((he = __hostent_init()) == NULL ||
443 (hed = __hostent_data_init()) == NULL) {
444 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
449 he->h_addrtype = AF_INET6;
450 he->h_length = NS_IN6ADDRSZ;
454 he->h_addrtype = AF_INET;
455 he->h_length = NS_INADDRSZ;
459 error = gethostanswer((const querybuf *)answer, anslen, qname, qtype,
461 return (error == 0) ? he : NULL;
465 _dns_gethostbyname(void *rval, void *cb_data, va_list ap)
471 int *errnop, *h_errnop;
472 struct hostent *hptr, he;
473 struct hostent_data *hed;
478 name = va_arg(ap, const char *);
479 af = va_arg(ap, int);
480 hptr = va_arg(ap, struct hostent *);
481 buffer = va_arg(ap, char *);
482 buflen = va_arg(ap, size_t);
483 errnop = va_arg(ap, int *);
484 h_errnop = va_arg(ap, int *);
486 *((struct hostent **)rval) = NULL;
488 statp = __res_state();
489 if ((hed = __hostent_data_init()) == NULL) {
490 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
491 *h_errnop = statp->res_h_errno;
492 return (NS_NOTFOUND);
498 he.h_length = NS_INADDRSZ;
502 he.h_length = NS_IN6ADDRSZ;
506 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
507 *h_errnop = statp->res_h_errno;
508 errno = EAFNOSUPPORT;
512 if ((buf = malloc(sizeof(*buf))) == NULL) {
513 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
514 *h_errnop = statp->res_h_errno;
515 return (NS_NOTFOUND);
517 n = res_nsearch(statp, name, C_IN, type, buf->buf, sizeof(buf->buf));
520 dprintf("res_nsearch failed (%d)\n", n, statp);
521 *h_errnop = statp->res_h_errno;
522 return (NS_NOTFOUND);
523 } else if (n > sizeof(buf->buf)) {
525 dprintf("static buffer is too small (%d)\n", n, statp);
526 *h_errnop = statp->res_h_errno;
529 error = gethostanswer(buf, n, name, type, &he, hed, statp);
532 *h_errnop = statp->res_h_errno;
533 switch (statp->res_h_errno) {
535 return (NS_NOTFOUND);
537 return (NS_TRYAGAIN);
543 if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
545 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
546 *h_errnop = statp->res_h_errno;
549 RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
550 *((struct hostent **)rval) = hptr;
555 _dns_gethostbyaddr(void *rval, void *cb_data, va_list ap)
562 int *errnop, *h_errnop;
564 struct hostent *hptr, he;
565 struct hostent_data *hed;
568 char qbuf[MAXDNAME+1], *qp;
575 char hname2[MAXDNAME+1], numaddr[46];
577 #endif /*SUNSECURITY*/
579 addr = va_arg(ap, const void *);
580 len = va_arg(ap, socklen_t);
581 af = va_arg(ap, int);
582 hptr = va_arg(ap, struct hostent *);
583 buffer = va_arg(ap, char *);
584 buflen = va_arg(ap, size_t);
585 errnop = va_arg(ap, int *);
586 h_errnop = va_arg(ap, int *);
587 uaddr = (const u_char *)addr;
589 *((struct hostent **)rval) = NULL;
591 statp = __res_state();
592 if ((hed = __hostent_data_init()) == NULL) {
593 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
594 *h_errnop = statp->res_h_errno;
595 return (NS_NOTFOUND);
600 (void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
608 for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) {
609 qp += SPRINTF((qp, "%x.%x.",
611 (uaddr[n] >> 4) & 0xf));
613 strlcat(qbuf, "ip6.arpa", sizeof(qbuf));
618 if ((buf = malloc(sizeof(*buf))) == NULL) {
619 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
620 *h_errnop = statp->res_h_errno;
623 n = res_nquery(statp, qbuf, C_IN, T_PTR, (u_char *)buf->buf,
627 dprintf("res_nquery failed (%d)\n", n, statp);
628 *h_errnop = statp->res_h_errno;
631 if (n > sizeof buf->buf) {
633 dprintf("static buffer is too small (%d)\n", n, statp);
634 *h_errnop = statp->res_h_errno;
637 if (gethostanswer(buf, n, qbuf, T_PTR, &he, hed, statp) != 0) {
639 *h_errnop = statp->res_h_errno;
640 switch (statp->res_h_errno) {
642 return (NS_NOTFOUND);
644 return (NS_TRYAGAIN);
654 * turn off search as the name should be absolute,
655 * 'localhost' should be matched by defnames
657 strncpy(hname2, he.h_name, MAXDNAME);
658 hname2[MAXDNAME] = '\0';
659 old_options = statp->options;
660 statp->options &= ~RES_DNSRCH;
661 statp->options |= RES_DEFNAMES;
662 memset(&rhd, 0, sizeof rhd);
663 rhe = gethostbyname_r(hname2, &rhd.host, &rhd.data,
664 sizeof(rhd.data), &ret_h_error);
666 if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL)
667 strlcpy(numaddr, "UNKNOWN", sizeof(numaddr));
668 syslog(LOG_NOTICE|LOG_AUTH,
669 "gethostbyaddr: No A record for %s (verifying [%s])",
671 statp->options = old_options;
672 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
673 *h_errnop = statp->res_h_errno;
674 return (NS_NOTFOUND);
676 statp->options = old_options;
677 for (haddr = rhe->h_addr_list; *haddr; haddr++)
678 if (!memcmp(*haddr, addr, NS_INADDRSZ))
681 if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL)
682 strlcpy(numaddr, "UNKNOWN", sizeof(numaddr));
683 syslog(LOG_NOTICE|LOG_AUTH,
684 "gethostbyaddr: A record of %s != PTR record [%s]",
686 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
687 *h_errnop = statp->res_h_errno;
688 return (NS_NOTFOUND);
691 #endif /*SUNSECURITY*/
694 memcpy(hed->host_addr, uaddr, len);
695 hed->h_addr_ptrs[0] = (char *)hed->host_addr;
696 hed->h_addr_ptrs[1] = NULL;
697 if (af == AF_INET && (statp->options & RES_USE_INET6)) {
698 _map_v4v6_address((char*)hed->host_addr, (char*)hed->host_addr);
699 he.h_addrtype = AF_INET6;
700 he.h_length = NS_IN6ADDRSZ;
702 if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
704 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
705 *h_errnop = statp->res_h_errno;
708 RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
709 *((struct hostent **)rval) = hptr;
715 addrsort(char **ap, int num, res_state res)
719 short aval[_MAXADDRS];
723 for (i = 0; i < num; i++, p++) {
724 for (j = 0 ; (unsigned)j < res->nsort; j++)
725 if (res->sort_list[j].addr.s_addr ==
726 (((struct in_addr *)(*p))->s_addr & res->sort_list[j].mask))
729 if (needsort == 0 && i > 0 && j < aval[i-1])
735 while (needsort < num) {
736 for (j = needsort - 1; j >= 0; j--) {
737 if (aval[j] > aval[j+1]) {
757 _sethostdnsent(int stayopen)
761 statp = __res_state();
762 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1)
765 statp->options |= RES_STAYOPEN | RES_USEVC;
773 statp = __res_state();
774 statp->options &= ~(RES_STAYOPEN | RES_USEVC);