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, *h_errnop;
469 struct hostent *hptr, he;
470 struct hostent_data *hed;
475 name = va_arg(ap, const char *);
476 af = va_arg(ap, int);
477 hptr = va_arg(ap, struct hostent *);
478 buffer = va_arg(ap, char *);
479 buflen = va_arg(ap, size_t);
480 errnop = va_arg(ap, int *);
481 h_errnop = va_arg(ap, int *);
483 *((struct hostent **)rval) = NULL;
485 statp = __res_state();
486 if ((hed = __hostent_data_init()) == NULL) {
487 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
488 *h_errnop = statp->res_h_errno;
489 return (NS_NOTFOUND);
495 he.h_length = NS_INADDRSZ;
499 he.h_length = NS_IN6ADDRSZ;
503 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
504 *h_errnop = statp->res_h_errno;
505 errno = EAFNOSUPPORT;
509 if ((buf = malloc(sizeof(*buf))) == NULL) {
510 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
511 *h_errnop = statp->res_h_errno;
512 return (NS_NOTFOUND);
514 n = res_nsearch(statp, name, C_IN, type, buf->buf, sizeof(buf->buf));
517 dprintf("res_nsearch failed (%d)\n", n, statp);
518 *h_errnop = statp->res_h_errno;
520 } else if (n > sizeof(buf->buf)) {
522 dprintf("static buffer is too small (%d)\n", n, statp);
523 *h_errnop = statp->res_h_errno;
526 error = gethostanswer(buf, n, name, type, &he, hed, statp);
529 *h_errnop = statp->res_h_errno;
530 return (NS_NOTFOUND);
532 if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
533 *h_errnop = statp->res_h_errno;
534 return (NS_NOTFOUND);
536 *((struct hostent **)rval) = hptr;
541 _dns_gethostbyaddr(void *rval, void *cb_data __unused, va_list ap)
548 int *errnop, *h_errnop;
550 struct hostent *hptr, he;
551 struct hostent_data *hed;
554 char qbuf[MAXDNAME+1], *qp;
561 char hname2[MAXDNAME+1], numaddr[46];
563 #endif /*SUNSECURITY*/
565 addr = va_arg(ap, const void *);
566 len = va_arg(ap, socklen_t);
567 af = va_arg(ap, int);
568 hptr = va_arg(ap, struct hostent *);
569 buffer = va_arg(ap, char *);
570 buflen = va_arg(ap, size_t);
571 errnop = va_arg(ap, int *);
572 h_errnop = va_arg(ap, int *);
573 uaddr = (const u_char *)addr;
575 *((struct hostent **)rval) = NULL;
577 statp = __res_state();
578 if ((hed = __hostent_data_init()) == NULL) {
579 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
580 *h_errnop = statp->res_h_errno;
581 return (NS_NOTFOUND);
586 sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
594 for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) {
595 qp += SPRINTF((qp, "%x.%x.",
597 (uaddr[n] >> 4) & 0xf));
599 strlcat(qbuf, "ip6.arpa", sizeof(qbuf));
604 if ((buf = malloc(sizeof(*buf))) == NULL) {
605 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
606 *h_errnop = statp->res_h_errno;
609 n = res_nquery(statp, qbuf, C_IN, T_PTR, (u_char *)buf->buf,
613 dprintf("res_nquery failed (%d)\n", n, statp);
614 *h_errnop = statp->res_h_errno;
617 if (n > sizeof buf->buf) {
619 dprintf("static buffer is too small (%d)\n", n, statp);
620 *h_errnop = statp->res_h_errno;
623 if (gethostanswer(buf, n, qbuf, T_PTR, &he, hed, statp) != 0) {
625 *h_errnop = statp->res_h_errno;
626 return (NS_NOTFOUND); /* h_errno was set by gethostanswer() */
632 * turn off search as the name should be absolute,
633 * 'localhost' should be matched by defnames
635 strncpy(hname2, he.h_name, MAXDNAME);
636 hname2[MAXDNAME] = '\0';
637 old_options = statp->options;
638 statp->options &= ~RES_DNSRCH;
639 statp->options |= RES_DEFNAMES;
640 memset(&rhd, 0, sizeof rhd);
641 rhe = gethostbyname_r(hname2, &rhd.host, &rhd.data,
642 sizeof(rhd.data), &ret_h_error);
644 if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL)
645 strlcpy(numaddr, "UNKNOWN", sizeof(numaddr));
646 syslog(LOG_NOTICE|LOG_AUTH,
647 "gethostbyaddr: No A record for %s (verifying [%s])",
649 statp->options = old_options;
650 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
651 *h_errnop = statp->res_h_errno;
652 return (NS_NOTFOUND);
654 statp->options = old_options;
655 for (haddr = rhe->h_addr_list; *haddr; haddr++)
656 if (!memcmp(*haddr, addr, NS_INADDRSZ))
659 if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL)
660 strlcpy(numaddr, "UNKNOWN", sizeof(numaddr));
661 syslog(LOG_NOTICE|LOG_AUTH,
662 "gethostbyaddr: A record of %s != PTR record [%s]",
664 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
665 *h_errnop = statp->res_h_errno;
666 return (NS_NOTFOUND);
669 #endif /*SUNSECURITY*/
672 memcpy(hed->host_addr, uaddr, len);
673 hed->h_addr_ptrs[0] = (char *)hed->host_addr;
674 hed->h_addr_ptrs[1] = NULL;
675 if (af == AF_INET && (statp->options & RES_USE_INET6)) {
676 _map_v4v6_address((char*)hed->host_addr, (char*)hed->host_addr);
677 he.h_addrtype = AF_INET6;
678 he.h_length = NS_IN6ADDRSZ;
680 RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
681 if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
682 *h_errnop = statp->res_h_errno;
683 return (NS_NOTFOUND);
685 *((struct hostent **)rval) = hptr;
691 addrsort(char **ap, int num, res_state res)
695 short aval[_MAXADDRS];
699 for (i = 0; i < num; i++, p++) {
700 for (j = 0 ; (unsigned)j < res->nsort; j++)
701 if (res->sort_list[j].addr.s_addr ==
702 (((struct in_addr *)(*p))->s_addr & res->sort_list[j].mask))
705 if (needsort == 0 && i > 0 && j < aval[i-1])
711 while (needsort < num) {
712 for (j = needsort - 1; j >= 0; j--) {
713 if (aval[j] > aval[j+1]) {
733 _sethostdnsent(int stayopen)
737 statp = __res_state();
738 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1)
741 statp->options |= RES_STAYOPEN | RES_USEVC;
749 statp = __res_state();
750 statp->options &= ~(RES_STAYOPEN | RES_USEVC);