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(msg, num, res)
115 if (res->options & RES_DEBUG) {
123 # define dprintf(msg, num, res) /*nada*/
126 #define BOUNDED_INCR(x) \
130 RES_SET_H_ERRNO(statp, NO_RECOVERY); \
135 #define BOUNDS_CHECK(ptr, count) \
137 if ((ptr) + (count) > eom) { \
138 RES_SET_H_ERRNO(statp, NO_RECOVERY); \
144 gethostanswer(const querybuf *answer, int anslen, const char *qname, int qtype,
145 struct hostent *he, struct hostent_data *hed, res_state statp)
150 const u_char *eom, *erdata;
151 char *bp, *ep, **ap, **hap;
152 int type, class, ancount, qdcount;
153 int haveanswer, had_error;
157 int (*name_ok)(const char *);
161 eom = answer->buf + anslen;
171 RES_SET_H_ERRNO(statp, NO_RECOVERY);
172 return (-1); /* XXX should be abort(); */
175 * find first satisfactory answer
178 ancount = ntohs(hp->ancount);
179 qdcount = ntohs(hp->qdcount);
181 ep = hed->hostbuf + sizeof hed->hostbuf;
183 BOUNDED_INCR(HFIXEDSZ);
185 RES_SET_H_ERRNO(statp, NO_RECOVERY);
188 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
189 if ((n < 0) || !(*name_ok)(bp)) {
190 RES_SET_H_ERRNO(statp, NO_RECOVERY);
193 BOUNDED_INCR(n + QFIXEDSZ);
194 if (qtype == T_A || qtype == T_AAAA) {
195 /* res_send() has already verified that the query name is the
196 * same as the one we sent; this just gets the expanded name
197 * (i.e., with the succeeding search-domain tacked on).
199 n = strlen(bp) + 1; /* for the \0 */
200 if (n >= MAXHOSTNAMELEN) {
201 RES_SET_H_ERRNO(statp, NO_RECOVERY);
206 /* The qname can be abbreviated, but h_name is now absolute. */
209 ap = hed->host_aliases;
211 he->h_aliases = hed->host_aliases;
212 hap = hed->h_addr_ptrs;
214 he->h_addr_list = hed->h_addr_ptrs;
218 while (ancount-- > 0 && cp < eom && !had_error) {
219 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
220 if ((n < 0) || !(*name_ok)(bp)) {
225 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
226 type = _getshort(cp);
227 cp += INT16SZ; /* type */
228 class = _getshort(cp);
229 cp += INT16SZ; /* class */
230 if (qtype == T_A && type == T_A)
231 _dns_ttl_ = _getlong(cp);
232 cp += INT32SZ; /* TTL */
234 cp += INT16SZ; /* len */
238 /* XXX - debug? syslog? */
240 continue; /* XXX - had_error++ ? */
242 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
243 if (ap >= &hed->host_aliases[_MAXALIASES-1])
245 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
246 if ((n < 0) || !(*name_ok)(tbuf)) {
252 RES_SET_H_ERRNO(statp, NO_RECOVERY);
257 n = strlen(bp) + 1; /* for the \0 */
258 if (n >= MAXHOSTNAMELEN) {
263 /* Get canonical name. */
264 n = strlen(tbuf) + 1; /* for the \0 */
265 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
274 if (qtype == T_PTR && type == T_CNAME) {
275 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
276 if (n < 0 || !res_dnok(tbuf)) {
282 RES_SET_H_ERRNO(statp, NO_RECOVERY);
285 /* Get canonical name. */
286 n = strlen(tbuf) + 1; /* for the \0 */
287 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
297 if (type != T_SIG && type != ns_t_dname)
298 syslog(LOG_NOTICE|LOG_AUTH,
299 "gethostby*.gethostanswer: asked for \"%s %s %s\", got type \"%s\"",
300 qname, p_class(C_IN), p_type(qtype),
303 continue; /* XXX - had_error++ ? */
307 if (strcasecmp(tname, bp) != 0) {
308 syslog(LOG_NOTICE|LOG_AUTH,
309 AskedForGot, qname, bp);
311 continue; /* XXX - had_error++ ? */
313 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
314 if ((n < 0) || !res_hnok(bp)) {
318 #if MULTI_PTRS_ARE_ALIASES
321 RES_SET_H_ERRNO(statp, NO_RECOVERY);
326 else if (ap < &hed->host_aliases[_MAXALIASES-1])
331 n = strlen(bp) + 1; /* for the \0 */
332 if (n >= MAXHOSTNAMELEN) {
341 if (statp->options & RES_USE_INET6) {
342 n = strlen(bp) + 1; /* for the \0 */
343 if (n >= MAXHOSTNAMELEN) {
348 _map_v4v6_hostent(he, &bp, ep);
350 RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
355 if (strcasecmp(he->h_name, bp) != 0) {
356 syslog(LOG_NOTICE|LOG_AUTH,
357 AskedForGot, he->h_name, bp);
359 continue; /* XXX - had_error++ ? */
361 if (n != he->h_length) {
369 nn = strlen(bp) + 1; /* for the \0 */
373 bp += sizeof(align) - ((u_long)bp % sizeof(align));
376 dprintf("size (%d) too big\n", n, statp);
380 if (hap >= &hed->h_addr_ptrs[_MAXADDRS-1]) {
382 dprintf("Too many addresses (%d)\n",
387 memcpy(*hap++ = bp, cp, n);
391 RES_SET_H_ERRNO(statp, NO_RECOVERY);
396 dprintf("Impossible condition (type=%d)\n", type,
398 RES_SET_H_ERRNO(statp, NO_RECOVERY);
400 /* BIND has abort() here, too risky on bad data */
408 # if defined(RESOLVSORT)
410 * Note: we sort even if host can take only one address
411 * in its return structures - should give it the "best"
412 * address in that case, not some random one
414 if (statp->nsort && haveanswer > 1 && qtype == T_A)
415 addrsort(hed->h_addr_ptrs, haveanswer, statp);
416 # endif /*RESOLVSORT*/
418 n = strlen(qname) + 1; /* for the \0 */
419 if (n > ep - bp || n >= MAXHOSTNAMELEN)
425 if (statp->options & RES_USE_INET6)
426 _map_v4v6_hostent(he, &bp, ep);
427 RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
431 RES_SET_H_ERRNO(statp, NO_RECOVERY);
435 /* XXX: for async DNS resolver in ypserv */
437 __dns_getanswer(const char *answer, int anslen, const char *qname, int qtype)
440 struct hostent_data *hed;
444 statp = __res_state();
445 if ((he = __hostent_init()) == NULL ||
446 (hed = __hostent_data_init()) == NULL) {
447 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
452 he->h_addrtype = AF_INET6;
453 he->h_length = NS_IN6ADDRSZ;
457 he->h_addrtype = AF_INET;
458 he->h_length = NS_INADDRSZ;
462 error = gethostanswer((const querybuf *)answer, anslen, qname, qtype,
464 return (error == 0) ? he : NULL;
468 _dns_gethostbyname(void *rval, void *cb_data, va_list ap)
474 int *errnop, *h_errnop;
475 struct hostent *hptr, he;
476 struct hostent_data *hed;
481 name = va_arg(ap, const char *);
482 af = va_arg(ap, int);
483 hptr = va_arg(ap, struct hostent *);
484 buffer = va_arg(ap, char *);
485 buflen = va_arg(ap, size_t);
486 errnop = va_arg(ap, int *);
487 h_errnop = va_arg(ap, int *);
489 *((struct hostent **)rval) = NULL;
491 statp = __res_state();
492 if ((hed = __hostent_data_init()) == NULL) {
493 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
494 *h_errnop = statp->res_h_errno;
495 return (NS_NOTFOUND);
501 he.h_length = NS_INADDRSZ;
505 he.h_length = NS_IN6ADDRSZ;
509 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
510 *h_errnop = statp->res_h_errno;
511 errno = EAFNOSUPPORT;
515 if ((buf = malloc(sizeof(*buf))) == NULL) {
516 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
517 *h_errnop = statp->res_h_errno;
518 return (NS_NOTFOUND);
520 n = res_nsearch(statp, name, C_IN, type, buf->buf, sizeof(buf->buf));
523 dprintf("res_nsearch failed (%d)\n", n, statp);
524 *h_errnop = statp->res_h_errno;
526 } else if (n > sizeof(buf->buf)) {
528 dprintf("static buffer is too small (%d)\n", n, statp);
529 *h_errnop = statp->res_h_errno;
532 error = gethostanswer(buf, n, name, type, &he, hed, statp);
535 *h_errnop = statp->res_h_errno;
536 return (NS_NOTFOUND);
538 if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
539 *h_errnop = statp->res_h_errno;
540 return (NS_NOTFOUND);
542 *((struct hostent **)rval) = hptr;
547 _dns_gethostbyaddr(void *rval, void *cb_data, va_list ap)
554 int *errnop, *h_errnop;
556 struct hostent *hptr, he;
557 struct hostent_data *hed;
560 char qbuf[MAXDNAME+1], *qp;
567 char hname2[MAXDNAME+1], numaddr[46];
569 #endif /*SUNSECURITY*/
571 addr = va_arg(ap, const void *);
572 len = va_arg(ap, socklen_t);
573 af = va_arg(ap, int);
574 hptr = va_arg(ap, struct hostent *);
575 buffer = va_arg(ap, char *);
576 buflen = va_arg(ap, size_t);
577 errnop = va_arg(ap, int *);
578 h_errnop = va_arg(ap, int *);
579 uaddr = (const u_char *)addr;
581 *((struct hostent **)rval) = NULL;
583 statp = __res_state();
584 if ((hed = __hostent_data_init()) == NULL) {
585 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
586 *h_errnop = statp->res_h_errno;
587 return (NS_NOTFOUND);
592 (void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
600 for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) {
601 qp += SPRINTF((qp, "%x.%x.",
603 (uaddr[n] >> 4) & 0xf));
605 strlcat(qbuf, "ip6.arpa", sizeof(qbuf));
610 if ((buf = malloc(sizeof(*buf))) == NULL) {
611 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
612 *h_errnop = statp->res_h_errno;
615 n = res_nquery(statp, qbuf, C_IN, T_PTR, (u_char *)buf->buf,
619 dprintf("res_nquery failed (%d)\n", n, statp);
620 *h_errnop = statp->res_h_errno;
623 if (n > sizeof buf->buf) {
625 dprintf("static buffer is too small (%d)\n", n, statp);
626 *h_errnop = statp->res_h_errno;
629 if (gethostanswer(buf, n, qbuf, T_PTR, &he, hed, statp) != 0) {
631 *h_errnop = statp->res_h_errno;
632 return (NS_NOTFOUND); /* h_errno was set by gethostanswer() */
638 * turn off search as the name should be absolute,
639 * 'localhost' should be matched by defnames
641 strncpy(hname2, he.h_name, MAXDNAME);
642 hname2[MAXDNAME] = '\0';
643 old_options = statp->options;
644 statp->options &= ~RES_DNSRCH;
645 statp->options |= RES_DEFNAMES;
646 memset(&rhd, 0, sizeof rhd);
647 rhe = gethostbyname_r(hname2, &rhd.host, &rhd.data,
648 sizeof(rhd.data), &ret_h_error);
650 if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL)
651 strlcpy(numaddr, "UNKNOWN", sizeof(numaddr));
652 syslog(LOG_NOTICE|LOG_AUTH,
653 "gethostbyaddr: No A record for %s (verifying [%s])",
655 statp->options = old_options;
656 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
657 *h_errnop = statp->res_h_errno;
658 return (NS_NOTFOUND);
660 statp->options = old_options;
661 for (haddr = rhe->h_addr_list; *haddr; haddr++)
662 if (!memcmp(*haddr, addr, NS_INADDRSZ))
665 if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL)
666 strlcpy(numaddr, "UNKNOWN", sizeof(numaddr));
667 syslog(LOG_NOTICE|LOG_AUTH,
668 "gethostbyaddr: A record of %s != PTR record [%s]",
670 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
671 *h_errnop = statp->res_h_errno;
672 return (NS_NOTFOUND);
675 #endif /*SUNSECURITY*/
678 memcpy(hed->host_addr, uaddr, len);
679 hed->h_addr_ptrs[0] = (char *)hed->host_addr;
680 hed->h_addr_ptrs[1] = NULL;
681 if (af == AF_INET && (statp->options & RES_USE_INET6)) {
682 _map_v4v6_address((char*)hed->host_addr, (char*)hed->host_addr);
683 he.h_addrtype = AF_INET6;
684 he.h_length = NS_IN6ADDRSZ;
686 RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
687 if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
688 *h_errnop = statp->res_h_errno;
689 return (NS_NOTFOUND);
691 *((struct hostent **)rval) = hptr;
697 addrsort(char **ap, int num, res_state res)
701 short aval[_MAXADDRS];
705 for (i = 0; i < num; i++, p++) {
706 for (j = 0 ; (unsigned)j < res->nsort; j++)
707 if (res->sort_list[j].addr.s_addr ==
708 (((struct in_addr *)(*p))->s_addr & res->sort_list[j].mask))
711 if (needsort == 0 && i > 0 && j < aval[i-1])
717 while (needsort < num) {
718 for (j = needsort - 1; j >= 0; j--) {
719 if (aval[j] > aval[j+1]) {
739 _sethostdnsent(int stayopen)
743 statp = __res_state();
744 if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1)
747 statp->options |= RES_STAYOPEN | RES_USEVC;
755 statp = __res_state();
756 statp->options &= ~(RES_STAYOPEN | RES_USEVC);