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
51 * @(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93
52 * From: Id: gethnamaddr.c,v 8.23 1998/04/07 04:59:46 vixie Exp $
53 * $FreeBSD: src/lib/libc/net/gethostbydns.c,v 1.58 2007/01/09 00:28:02 imp Exp $
56 #include <sys/types.h>
57 #include <sys/param.h>
58 #include <sys/socket.h>
59 #include <netinet/in.h>
60 #include <arpa/inet.h>
61 #include <arpa/nameser.h>
75 #include "netdb_private.h"
76 #include "res_config.h"
78 #define SPRINTF(x) ((size_t)sprintf x)
80 static const char AskedForGot[] =
81 "gethostby*.gethostanswer: asked for \"%s\", got \"%s\"";
84 static void addrsort(char **, int, res_state);
88 static void dprintf(char *, int, res_state) __printflike(1, 0);
91 #define MAXPACKET (64*1024)
95 u_char buf[MAXPACKET];
107 dprintf(char *msg, int num, res_state res)
109 if (res->options & RES_DEBUG) {
117 # define dprintf(msg, num, res) /*nada*/
120 #define BOUNDED_INCR(x) \
124 RES_SET_H_ERRNO(statp, NO_RECOVERY); \
129 #define BOUNDS_CHECK(ptr, count) \
131 if ((ptr) + (count) > eom) { \
132 RES_SET_H_ERRNO(statp, NO_RECOVERY); \
138 gethostanswer(const querybuf *answer, int anslen, const char *qname, int qtype,
139 struct hostent *he, struct hostent_data *hed, res_state statp)
144 const u_char *eom, *erdata;
145 char *bp, *ep, **ap, **hap;
146 int type, class, ancount, qdcount;
147 int haveanswer, had_error;
151 int (*name_ok)(const char *);
155 eom = answer->buf + anslen;
165 RES_SET_H_ERRNO(statp, NO_RECOVERY);
166 return (-1); /* XXX should be abort(); */
169 * find first satisfactory answer
172 ancount = ntohs(hp->ancount);
173 qdcount = ntohs(hp->qdcount);
175 ep = hed->hostbuf + sizeof hed->hostbuf;
177 BOUNDED_INCR(HFIXEDSZ);
179 RES_SET_H_ERRNO(statp, NO_RECOVERY);
182 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
183 if ((n < 0) || !(*name_ok)(bp)) {
184 RES_SET_H_ERRNO(statp, NO_RECOVERY);
187 BOUNDED_INCR(n + QFIXEDSZ);
188 if (qtype == T_A || qtype == T_AAAA) {
189 /* res_send() has already verified that the query name is the
190 * same as the one we sent; this just gets the expanded name
191 * (i.e., with the succeeding search-domain tacked on).
193 n = strlen(bp) + 1; /* for the \0 */
194 if (n >= MAXHOSTNAMELEN) {
195 RES_SET_H_ERRNO(statp, NO_RECOVERY);
200 /* The qname can be abbreviated, but h_name is now absolute. */
203 ap = hed->host_aliases;
205 he->h_aliases = hed->host_aliases;
206 hap = hed->h_addr_ptrs;
208 he->h_addr_list = hed->h_addr_ptrs;
212 while (ancount-- > 0 && cp < eom && !had_error) {
213 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
214 if ((n < 0) || !(*name_ok)(bp)) {
219 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
220 type = _getshort(cp);
221 cp += INT16SZ; /* type */
222 class = _getshort(cp);
223 cp += INT16SZ; /* class */
224 if (qtype == T_A && type == T_A)
225 _dns_ttl_ = _getlong(cp);
226 cp += INT32SZ; /* TTL */
228 cp += INT16SZ; /* len */
232 /* XXX - debug? syslog? */
234 continue; /* XXX - had_error++ ? */
236 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
237 if (ap >= &hed->host_aliases[_MAXALIASES-1])
239 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
240 if ((n < 0) || !(*name_ok)(tbuf)) {
246 RES_SET_H_ERRNO(statp, NO_RECOVERY);
251 n = strlen(bp) + 1; /* for the \0 */
252 if (n >= MAXHOSTNAMELEN) {
257 /* Get canonical name. */
258 n = strlen(tbuf) + 1; /* for the \0 */
259 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
268 if (qtype == T_PTR && type == T_CNAME) {
269 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
270 if (n < 0 || !res_dnok(tbuf)) {
276 RES_SET_H_ERRNO(statp, NO_RECOVERY);
279 /* Get canonical name. */
280 n = strlen(tbuf) + 1; /* for the \0 */
281 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
292 syslog(LOG_NOTICE|LOG_AUTH,
293 "gethostby*.gethostanswer: asked for \"%s %s %s\", got type \"%s\"",
294 qname, p_class(C_IN), p_type(qtype),
297 continue; /* XXX - had_error++ ? */
301 if (strcasecmp(tname, bp) != 0) {
302 syslog(LOG_NOTICE|LOG_AUTH,
303 AskedForGot, qname, bp);
305 continue; /* XXX - had_error++ ? */
307 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
308 if ((n < 0) || !res_hnok(bp)) {
312 #if MULTI_PTRS_ARE_ALIASES
315 RES_SET_H_ERRNO(statp, NO_RECOVERY);
320 else if (ap < &hed->host_aliases[_MAXALIASES-1])
325 n = strlen(bp) + 1; /* for the \0 */
326 if (n >= MAXHOSTNAMELEN) {
335 if (statp->options & RES_USE_INET6) {
336 n = strlen(bp) + 1; /* for the \0 */
337 if (n >= MAXHOSTNAMELEN) {
342 _map_v4v6_hostent(he, &bp, ep);
344 RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
349 if (strcasecmp(he->h_name, bp) != 0) {
350 syslog(LOG_NOTICE|LOG_AUTH,
351 AskedForGot, he->h_name, bp);
353 continue; /* XXX - had_error++ ? */
355 if (n != he->h_length) {
363 nn = strlen(bp) + 1; /* for the \0 */
367 bp += sizeof(align) - ((u_long)bp % sizeof(align));
370 dprintf("size (%d) too big\n", n, statp);
374 if (hap >= &hed->h_addr_ptrs[_MAXADDRS-1]) {
376 dprintf("Too many addresses (%d)\n",
381 memcpy(*hap++ = bp, cp, n);
385 RES_SET_H_ERRNO(statp, NO_RECOVERY);
390 dprintf("Impossible condition (type=%d)\n", type,
392 RES_SET_H_ERRNO(statp, NO_RECOVERY);
394 /* BIND has abort() here, too risky on bad data */
402 # if defined(RESOLVSORT)
404 * Note: we sort even if host can take only one address
405 * in its return structures - should give it the "best"
406 * address in that case, not some random one
408 if (statp->nsort && haveanswer > 1 && qtype == T_A)
409 addrsort(hed->h_addr_ptrs, haveanswer, statp);
410 # endif /*RESOLVSORT*/
412 n = strlen(qname) + 1; /* for the \0 */
413 if (n > ep - bp || n >= MAXHOSTNAMELEN)
419 if (statp->options & RES_USE_INET6)
420 _map_v4v6_hostent(he, &bp, ep);
421 RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
425 RES_SET_H_ERRNO(statp, NO_RECOVERY);
429 /* XXX: for async DNS resolver in ypserv */
431 __dns_getanswer(const char *answer, int anslen, const char *qname, int qtype)
434 struct hostent_data *hed;
438 statp = __res_state();
439 if ((he = __hostent_init()) == NULL ||
440 (hed = __hostent_data_init()) == NULL) {
441 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
446 he->h_addrtype = AF_INET6;
447 he->h_length = NS_IN6ADDRSZ;
451 he->h_addrtype = AF_INET;
452 he->h_length = NS_INADDRSZ;
456 error = gethostanswer((const querybuf *)answer, anslen, qname, qtype,
458 return (error == 0) ? he : NULL;
462 _dns_gethostbyname(void *rval, void *cb_data __unused, va_list ap)
468 int *errnop __unused;
470 struct hostent *hptr, he;
471 struct hostent_data *hed;
476 name = va_arg(ap, const char *);
477 af = va_arg(ap, int);
478 hptr = va_arg(ap, struct hostent *);
479 buffer = va_arg(ap, char *);
480 buflen = va_arg(ap, size_t);
481 errnop = va_arg(ap, int *);
482 h_errnop = va_arg(ap, int *);
484 *((struct hostent **)rval) = NULL;
486 statp = __res_state();
487 if ((hed = __hostent_data_init()) == NULL) {
488 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
489 *h_errnop = statp->res_h_errno;
490 return (NS_NOTFOUND);
496 he.h_length = NS_INADDRSZ;
500 he.h_length = NS_IN6ADDRSZ;
504 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
505 *h_errnop = statp->res_h_errno;
506 errno = EAFNOSUPPORT;
510 if ((buf = malloc(sizeof(*buf))) == NULL) {
511 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
512 *h_errnop = statp->res_h_errno;
513 return (NS_NOTFOUND);
515 n = res_nsearch(statp, name, C_IN, type, buf->buf, sizeof(buf->buf));
518 dprintf("res_nsearch failed (%d)\n", n, statp);
519 *h_errnop = statp->res_h_errno;
521 } else if (n > sizeof(buf->buf)) {
523 dprintf("static buffer is too small (%d)\n", n, statp);
524 *h_errnop = statp->res_h_errno;
527 error = gethostanswer(buf, n, name, type, &he, hed, statp);
530 *h_errnop = statp->res_h_errno;
531 return (NS_NOTFOUND);
533 if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
534 *h_errnop = statp->res_h_errno;
535 return (NS_NOTFOUND);
537 *((struct hostent **)rval) = hptr;
542 _dns_gethostbyaddr(void *rval, void *cb_data __unused, va_list ap)
549 int *errnop __unused;
552 struct hostent *hptr, he;
553 struct hostent_data *hed;
556 char qbuf[MAXDNAME+1], *qp;
563 char hname2[MAXDNAME+1], numaddr[46];
565 #endif /*SUNSECURITY*/
567 addr = va_arg(ap, const void *);
568 len = va_arg(ap, socklen_t);
569 af = va_arg(ap, int);
570 hptr = va_arg(ap, struct hostent *);
571 buffer = va_arg(ap, char *);
572 buflen = va_arg(ap, size_t);
573 errnop = va_arg(ap, int *);
574 h_errnop = va_arg(ap, int *);
575 uaddr = (const u_char *)addr;
577 *((struct hostent **)rval) = NULL;
579 statp = __res_state();
580 if ((hed = __hostent_data_init()) == NULL) {
581 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
582 *h_errnop = statp->res_h_errno;
583 return (NS_NOTFOUND);
588 sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
596 for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) {
597 qp += SPRINTF((qp, "%x.%x.",
599 (uaddr[n] >> 4) & 0xf));
601 strlcat(qbuf, "ip6.arpa", sizeof(qbuf));
606 if ((buf = malloc(sizeof(*buf))) == NULL) {
607 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
608 *h_errnop = statp->res_h_errno;
611 n = res_nquery(statp, qbuf, C_IN, T_PTR, (u_char *)buf->buf,
615 dprintf("res_nquery failed (%d)\n", n, statp);
616 *h_errnop = statp->res_h_errno;
619 if (n > sizeof buf->buf) {
621 dprintf("static buffer is too small (%d)\n", n, statp);
622 *h_errnop = statp->res_h_errno;
625 if (gethostanswer(buf, n, qbuf, T_PTR, &he, hed, statp) != 0) {
627 *h_errnop = statp->res_h_errno;
628 return (NS_NOTFOUND); /* h_errno was set by gethostanswer() */
634 * turn off search as the name should be absolute,
635 * 'localhost' should be matched by defnames
637 strncpy(hname2, he.h_name, MAXDNAME);
638 hname2[MAXDNAME] = '\0';
639 old_options = statp->options;
640 statp->options &= ~RES_DNSRCH;
641 statp->options |= RES_DEFNAMES;
642 memset(&rhd, 0, sizeof rhd);
643 rhe = gethostbyname_r(hname2, &rhd.host, &rhd.data,
644 sizeof(rhd.data), &ret_h_error);
646 if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL)
647 strlcpy(numaddr, "UNKNOWN", sizeof(numaddr));
648 syslog(LOG_NOTICE|LOG_AUTH,
649 "gethostbyaddr: No A record for %s (verifying [%s])",
651 statp->options = old_options;
652 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
653 *h_errnop = statp->res_h_errno;
654 return (NS_NOTFOUND);
656 statp->options = old_options;
657 for (haddr = rhe->h_addr_list; *haddr; haddr++)
658 if (!memcmp(*haddr, addr, NS_INADDRSZ))
661 if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL)
662 strlcpy(numaddr, "UNKNOWN", sizeof(numaddr));
663 syslog(LOG_NOTICE|LOG_AUTH,
664 "gethostbyaddr: A record of %s != PTR record [%s]",
666 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
667 *h_errnop = statp->res_h_errno;
668 return (NS_NOTFOUND);
671 #endif /*SUNSECURITY*/
674 memcpy(hed->host_addr, uaddr, len);
675 hed->h_addr_ptrs[0] = (char *)hed->host_addr;
676 hed->h_addr_ptrs[1] = NULL;
677 if (af == AF_INET && (statp->options & RES_USE_INET6)) {
678 _map_v4v6_address((char*)hed->host_addr, (char*)hed->host_addr);
679 he.h_addrtype = AF_INET6;
680 he.h_length = NS_IN6ADDRSZ;
682 RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
683 if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
684 *h_errnop = statp->res_h_errno;
685 return (NS_NOTFOUND);
687 *((struct hostent **)rval) = hptr;
693 addrsort(char **ap, int num, res_state res)
697 short aval[_MAXADDRS];
701 for (i = 0; i < num; i++, p++) {
702 for (j = 0 ; (unsigned)j < res->nsort; j++)
703 if (res->sort_list[j].addr.s_addr ==
704 (((struct in_addr *)(*p))->s_addr & res->sort_list[j].mask))
707 if (needsort == 0 && i > 0 && j < aval[i-1])
713 while (needsort < num) {
714 for (j = needsort - 1; j >= 0; j--) {
715 if (aval[j] > aval[j+1]) {
735 _sethostdnsent(int stayopen)
739 statp = __res_state();
740 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1)
743 statp->options |= RES_STAYOPEN | RES_USEVC;
751 statp = __res_state();
752 statp->options &= ~(RES_STAYOPEN | RES_USEVC);