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 #endif /* LIBC_SCCS and not lint */
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>
76 #define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */
78 #if defined(BSD) && (BSD >= 199103) && defined(AF_INET6)
82 # include "../conf/portability.h"
85 #if defined(USE_OPTIONS_H)
86 # include <../conf/options.h>
90 # define SPRINTF(x) strlen(sprintf/**/x)
92 # define SPRINTF(x) ((size_t)sprintf x)
98 static const char AskedForGot[] =
99 "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
101 static char *h_addr_ptrs[MAXADDRS + 1];
103 static struct hostent host;
104 static char *host_aliases[MAXALIASES];
105 static char hostbuf[8*1024];
106 static u_char host_addr[16]; /* IPv4 or IPv6 */
107 static FILE *hostf = NULL;
108 static int stayopen = 0;
110 static void map_v4v6_address (const char *src, char *dst) __THROW;
111 static void map_v4v6_hostent (struct hostent *hp, char **bp, int *len) __THROW;
114 extern void addrsort (char **, int) __THROW;
118 #define MAXPACKET PACKETSZ
120 #define MAXPACKET 65536
123 /* As per RFC 1034 and 1035 a host name cannot exceed 255 octets in length. */
124 #ifdef MAXHOSTNAMELEN
125 # undef MAXHOSTNAMELEN
127 #define MAXHOSTNAMELEN 256
131 u_char buf[MAXPACKET];
149 if (_res.options & RES_DEBUG) {
157 # define Dprintf(msg, num) /*nada*/
160 #define BOUNDED_INCR(x) \
164 __set_h_errno (NO_RECOVERY); \
169 #define BOUNDS_CHECK(ptr, count) \
171 if ((ptr) + (count) > eom) { \
172 __set_h_errno (NO_RECOVERY); \
178 static struct hostent *
179 getanswer (const querybuf *answer, int anslen, const char *qname, int qtype)
181 register const HEADER *hp;
182 register const u_char *cp;
184 const u_char *eom, *erdata;
185 char *bp, **ap, **hap;
186 int type, class, buflen, ancount, qdcount;
187 int haveanswer, had_error;
191 int (*name_ok) (const char *);
195 eom = answer->buf + anslen;
205 return (NULL); /* XXX should be abort(); */
208 * find first satisfactory answer
211 ancount = ntohs(hp->ancount);
212 qdcount = ntohs(hp->qdcount);
214 buflen = sizeof hostbuf;
216 BOUNDED_INCR(HFIXEDSZ);
218 __set_h_errno (NO_RECOVERY);
221 n = dn_expand(answer->buf, eom, cp, bp, buflen);
222 if ((n < 0) || !(*name_ok)(bp)) {
223 __set_h_errno (NO_RECOVERY);
226 BOUNDED_INCR(n + QFIXEDSZ);
227 if (qtype == T_A || qtype == T_AAAA) {
228 /* res_send() has already verified that the query name is the
229 * same as the one we sent; this just gets the expanded name
230 * (i.e., with the succeeding search-domain tacked on).
232 n = strlen(bp) + 1; /* for the \0 */
233 if (n >= MAXHOSTNAMELEN) {
234 __set_h_errno (NO_RECOVERY);
240 /* The qname can be abbreviated, but h_name is now absolute. */
245 host.h_aliases = host_aliases;
248 host.h_addr_list = h_addr_ptrs;
251 while (ancount-- > 0 && cp < eom && !had_error) {
252 n = dn_expand(answer->buf, eom, cp, bp, buflen);
253 if ((n < 0) || !(*name_ok)(bp)) {
258 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
260 cp += INT16SZ; /* type */
261 class = ns_get16(cp);
262 cp += INT16SZ + INT32SZ; /* class, TTL */
264 cp += INT16SZ; /* len */
268 /* XXX - debug? syslog? */
270 continue; /* XXX - had_error++ ? */
272 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
273 if (ap >= &host_aliases[MAXALIASES-1])
275 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
276 if ((n < 0) || !(*name_ok)(tbuf)) {
282 __set_h_errno (NO_RECOVERY);
287 n = strlen(bp) + 1; /* for the \0 */
288 if (n >= MAXHOSTNAMELEN) {
294 /* Get canonical name. */
295 n = strlen(tbuf) + 1; /* for the \0 */
296 if (n > buflen || n >= MAXHOSTNAMELEN) {
306 if (qtype == T_PTR && type == T_CNAME) {
307 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
308 if (n < 0 || !res_dnok(tbuf)) {
314 __set_h_errno (NO_RECOVERY);
317 /* Get canonical name. */
318 n = strlen(tbuf) + 1; /* for the \0 */
319 if (n > buflen || n >= MAXHOSTNAMELEN) {
329 if ((type == T_SIG) || (type == T_KEY) || (type == T_NXT)) {
330 /* We don't support DNSSEC yet. For now, ignore
331 * the record and send a low priority message
334 syslog(LOG_DEBUG|LOG_AUTH,
335 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
336 qname, p_class(C_IN), p_type(qtype),
342 syslog(LOG_NOTICE|LOG_AUTH,
343 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
344 qname, p_class(C_IN), p_type(qtype),
347 continue; /* XXX - had_error++ ? */
351 if (strcasecmp(tname, bp) != 0) {
352 syslog(LOG_NOTICE|LOG_AUTH,
353 AskedForGot, qname, bp);
355 continue; /* XXX - had_error++ ? */
357 n = dn_expand(answer->buf, eom, cp, bp, buflen);
358 if ((n < 0) || !res_hnok(bp)) {
362 #if MULTI_PTRS_ARE_ALIASES
365 __set_h_errno (NO_RECOVERY);
370 else if (ap < &host_aliases[MAXALIASES-1])
375 n = strlen(bp) + 1; /* for the \0 */
376 if (n >= MAXHOSTNAMELEN) {
386 if (_res.options & RES_USE_INET6) {
387 n = strlen(bp) + 1; /* for the \0 */
388 if (n >= MAXHOSTNAMELEN) {
394 map_v4v6_hostent(&host, &bp, &buflen);
396 __set_h_errno (NETDB_SUCCESS);
401 if (strcasecmp(host.h_name, bp) != 0) {
402 syslog(LOG_NOTICE|LOG_AUTH,
403 AskedForGot, host.h_name, bp);
405 continue; /* XXX - had_error++ ? */
407 if (n != host.h_length) {
415 nn = strlen(bp) + 1; /* for the \0 */
420 /* XXX: when incrementing bp, we have to decrement
421 * buflen by the same amount --okir */
422 buflen -= sizeof(align) - ((u_long)bp % sizeof(align));
424 bp += sizeof(align) - ((u_long)bp % sizeof(align));
426 if (bp + n >= &hostbuf[sizeof hostbuf]) {
427 Dprintf("size (%d) too big\n", n);
431 if (hap >= &h_addr_ptrs[MAXADDRS-1]) {
433 Dprintf("Too many addresses (%d)\n",
439 memmove(*hap++ = bp, cp, n);
444 __set_h_errno (NO_RECOVERY);
457 # if defined(RESOLVSORT)
459 * Note: we sort even if host can take only one address
460 * in its return structures - should give it the "best"
461 * address in that case, not some random one
463 if (_res.nsort && haveanswer > 1 && qtype == T_A)
464 addrsort(h_addr_ptrs, haveanswer);
465 # endif /*RESOLVSORT*/
467 n = strlen(qname) + 1; /* for the \0 */
468 if (n > buflen || n >= MAXHOSTNAMELEN)
475 if (_res.options & RES_USE_INET6)
476 map_v4v6_hostent(&host, &bp, &buflen);
477 __set_h_errno (NETDB_SUCCESS);
481 __set_h_errno (NO_RECOVERY);
485 extern struct hostent *gethostbyname2(const char *name, int af);
486 libresolv_hidden_proto (gethostbyname2)
494 if (__res_maybe_init (&_res, 0) == -1) {
495 __set_h_errno (NETDB_INTERNAL);
498 if (_res.options & RES_USE_INET6) {
499 hp = gethostbyname2(name, AF_INET6);
503 return (gethostbyname2(name, AF_INET));
507 gethostbyname2(name, af)
517 register const char *cp;
519 int n, size, type, len;
522 if (__res_maybe_init (&_res, 0) == -1) {
523 __set_h_errno (NETDB_INTERNAL);
537 __set_h_errno (NETDB_INTERNAL);
538 __set_errno (EAFNOSUPPORT);
542 host.h_addrtype = af;
543 host.h_length = size;
546 * if there aren't any dots, it could be a user-level alias.
547 * this is also done in res_query() since we are not the only
548 * function that looks up host names.
550 if (!strchr(name, '.') && (cp = __hostalias(name)))
554 * disallow names consisting only of digits/dots, unless
557 if (isdigit(name[0]))
558 for (cp = name;; ++cp) {
563 * All-numeric, no dot at the end.
564 * Fake up a hostent as if we'd actually
567 if (inet_pton(af, name, host_addr) <= 0) {
568 __set_h_errno (HOST_NOT_FOUND);
571 strncpy(hostbuf, name, MAXDNAME);
572 hostbuf[MAXDNAME] = '\0';
573 bp = hostbuf + MAXDNAME;
574 len = sizeof hostbuf - MAXDNAME;
575 host.h_name = hostbuf;
576 host.h_aliases = host_aliases;
577 host_aliases[0] = NULL;
578 h_addr_ptrs[0] = (char *)host_addr;
579 h_addr_ptrs[1] = NULL;
580 host.h_addr_list = h_addr_ptrs;
581 if (_res.options & RES_USE_INET6)
582 map_v4v6_hostent(&host, &bp, &len);
583 __set_h_errno (NETDB_SUCCESS);
586 if (!isdigit(*cp) && *cp != '.')
589 if ((isxdigit(name[0]) && strchr(name, ':') != NULL) ||
591 for (cp = name;; ++cp) {
596 * All-IPv6-legal, no dot at the end.
597 * Fake up a hostent as if we'd actually
600 if (inet_pton(af, name, host_addr) <= 0) {
601 __set_h_errno (HOST_NOT_FOUND);
604 strncpy(hostbuf, name, MAXDNAME);
605 hostbuf[MAXDNAME] = '\0';
606 bp = hostbuf + MAXDNAME;
607 len = sizeof hostbuf - MAXDNAME;
608 host.h_name = hostbuf;
609 host.h_aliases = host_aliases;
610 host_aliases[0] = NULL;
611 h_addr_ptrs[0] = (char *)host_addr;
612 h_addr_ptrs[1] = NULL;
613 host.h_addr_list = h_addr_ptrs;
614 __set_h_errno (NETDB_SUCCESS);
617 if (!isxdigit(*cp) && *cp != ':' && *cp != '.')
621 buf.buf = origbuf = (querybuf *) alloca (1024);
623 if ((n = __libc_res_nsearch(&_res, name, C_IN, type, buf.buf->buf, 1024,
624 &buf.ptr, NULL, NULL, NULL)) < 0) {
625 if (buf.buf != origbuf)
627 Dprintf("res_nsearch failed (%d)\n", n);
628 if (errno == ECONNREFUSED)
629 return (_gethtbyname2(name, af));
632 ret = getanswer(buf.buf, n, name, type);
633 if (buf.buf != origbuf)
637 libresolv_hidden_def (gethostbyname2)
640 gethostbyaddr(addr, len, af)
645 const u_char *uaddr = (const u_char *)addr;
646 static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
647 static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
656 register struct hostent *hp;
657 char qbuf[MAXDNAME+1], *qp = NULL;
659 register struct hostent *rhp;
662 char hname2[MAXDNAME+1];
663 #endif /*SUNSECURITY*/
665 if (__res_maybe_init (&_res, 0) == -1) {
666 __set_h_errno (NETDB_INTERNAL);
669 if (af == AF_INET6 && len == IN6ADDRSZ &&
670 (!bcmp(uaddr, mapped, sizeof mapped) ||
671 !bcmp(uaddr, tunnelled, sizeof tunnelled))) {
673 addr += sizeof mapped;
674 uaddr += sizeof mapped;
686 __set_errno (EAFNOSUPPORT);
687 __set_h_errno (NETDB_INTERNAL);
691 __set_errno (EINVAL);
692 __set_h_errno (NETDB_INTERNAL);
697 (void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
705 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
706 qp += SPRINTF((qp, "%x.%x.",
708 (uaddr[n] >> 4) & 0xf));
710 strcpy(qp, "ip6.arpa");
716 buf.buf = orig_buf = (querybuf *) alloca (1024);
718 n = __libc_res_nquery(&_res, qbuf, C_IN, T_PTR, buf.buf->buf, 1024,
719 &buf.ptr, NULL, NULL, NULL);
720 if (n < 0 && af == AF_INET6 && (_res.options & RES_NOIP6DOTINT) == 0) {
721 strcpy(qp, "ip6.int");
722 n = __libc_res_nquery(&_res, qbuf, C_IN, T_PTR, buf.buf->buf,
723 buf.buf != orig_buf ? MAXPACKET : 1024,
724 &buf.ptr, NULL, NULL, NULL);
727 if (buf.buf != orig_buf)
729 Dprintf("res_nquery failed (%d)\n", n);
730 if (errno == ECONNREFUSED)
731 return (_gethtbyaddr(addr, len, af));
734 hp = getanswer(buf.buf, n, qbuf, T_PTR);
735 if (buf.buf != orig_buf)
738 return (NULL); /* h_errno was set by getanswer() */
742 * turn off search as the name should be absolute,
743 * 'localhost' should be matched by defnames
745 strncpy(hname2, hp->h_name, MAXDNAME);
746 hname2[MAXDNAME] = '\0';
747 old_options = _res.options;
748 _res.options &= ~RES_DNSRCH;
749 _res.options |= RES_DEFNAMES;
750 if (!(rhp = gethostbyname(hname2))) {
751 syslog(LOG_NOTICE|LOG_AUTH,
752 "gethostbyaddr: No A record for %s (verifying [%s])",
753 hname2, inet_ntoa(*((struct in_addr *)addr)));
754 _res.options = old_options;
755 __set_h_errno (HOST_NOT_FOUND);
758 _res.options = old_options;
759 for (haddr = rhp->h_addr_list; *haddr; haddr++)
760 if (!memcmp(*haddr, addr, INADDRSZ))
763 syslog(LOG_NOTICE|LOG_AUTH,
764 "gethostbyaddr: A record of %s != PTR record [%s]",
765 hname2, inet_ntoa(*((struct in_addr *)addr)));
766 __set_h_errno (HOST_NOT_FOUND);
770 #endif /*SUNSECURITY*/
773 memmove(host_addr, addr, len);
774 h_addr_ptrs[0] = (char *)host_addr;
775 h_addr_ptrs[1] = NULL;
776 if (af == AF_INET && (_res.options & RES_USE_INET6)) {
777 map_v4v6_address((char*)host_addr, (char*)host_addr);
778 hp->h_addrtype = AF_INET6;
779 hp->h_length = IN6ADDRSZ;
781 __set_h_errno (NETDB_SUCCESS);
790 hostf = fopen(_PATH_HOSTS, "rce" );
795 libresolv_hidden_def (_sethtent)
800 if (hostf && !stayopen) {
801 (void) fclose(hostf);
810 register char *cp, **q;
813 if (!hostf && !(hostf = fopen(_PATH_HOSTS, "rce" ))) {
814 __set_h_errno (NETDB_INTERNAL);
818 if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) {
819 __set_h_errno (HOST_NOT_FOUND);
824 if (!(cp = strpbrk(p, "#\n")))
827 if (!(cp = strpbrk(p, " \t")))
830 if (inet_pton(AF_INET6, p, host_addr) > 0) {
833 } else if (inet_pton(AF_INET, p, host_addr) > 0) {
834 if (_res.options & RES_USE_INET6) {
835 map_v4v6_address((char*)host_addr, (char*)host_addr);
845 h_addr_ptrs[0] = (char *)host_addr;
846 h_addr_ptrs[1] = NULL;
847 host.h_addr_list = h_addr_ptrs;
849 host.h_addrtype = af;
850 while (*cp == ' ' || *cp == '\t')
853 q = host.h_aliases = host_aliases;
854 if ((cp = strpbrk(cp, " \t")))
857 if (*cp == ' ' || *cp == '\t') {
861 if (q < &host_aliases[MAXALIASES - 1])
863 if ((cp = strpbrk(cp, " \t")))
867 __set_h_errno (NETDB_SUCCESS);
870 libresolv_hidden_def (_gethtent)
878 if (_res.options & RES_USE_INET6) {
879 hp = _gethtbyname2(name, AF_INET6);
883 return (_gethtbyname2(name, AF_INET));
887 _gethtbyname2(name, af)
891 register struct hostent *p;
895 while ((p = _gethtent())) {
896 if (p->h_addrtype != af)
898 if (strcasecmp(p->h_name, name) == 0)
900 for (cp = p->h_aliases; *cp != 0; cp++)
901 if (strcasecmp(*cp, name) == 0)
908 libresolv_hidden_def (_gethtbyname2)
911 _gethtbyaddr(addr, len, af)
916 register struct hostent *p;
919 while ((p = _gethtent()))
920 if (p->h_addrtype == af && !bcmp(p->h_addr, addr, len))
925 libresolv_hidden_def (_gethtbyaddr)
928 map_v4v6_address(src, dst)
932 u_char *p = (u_char *)dst;
936 /* Stash a temporary copy so our caller can update in place. */
937 memcpy(tmp, src, INADDRSZ);
938 /* Mark this ipv6 addr as a mapped ipv4. */
939 for (i = 0; i < 10; i++)
943 /* Retrieve the saved copy and we're done. */
944 memcpy((void*)p, tmp, INADDRSZ);
948 map_v4v6_hostent(hp, bpp, lenp)
955 if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
957 hp->h_addrtype = AF_INET6;
958 hp->h_length = IN6ADDRSZ;
959 for (ap = hp->h_addr_list; *ap; ap++) {
960 int i = sizeof(align) - ((u_long)*bpp % sizeof(align));
962 if (*lenp < (i + IN6ADDRSZ)) {
963 /* Out of memory. Truncate address list here. XXX */
969 map_v4v6_address(*ap, *bpp);
984 short aval[MAXADDRS];
988 for (i = 0; i < num; i++, p++) {
989 for (j = 0 ; (unsigned)j < _res.nsort; j++)
990 if (_res.sort_list[j].addr.s_addr ==
991 (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask))
994 if (needsort == 0 && i > 0 && j < aval[i-1])
1000 while (needsort < num) {
1001 for (j = needsort - 1; j >= 0; j--) {
1002 if (aval[j] > aval[j+1]) {
1006 aval[j] = aval[j+1];
1021 #if defined(BSD43_BSD43_NFS) || defined(sun)
1022 /* some libc's out there are bound internally to these names (UMIPS) */
1024 ht_sethostent(stayopen)
1027 _sethtent(stayopen);
1037 ht_gethostbyname(name)
1040 return (_gethtbyname(name));
1044 ht_gethostbyaddr(addr, len, af)
1049 return (_gethtbyaddr(addr, len, af));
1055 return (_gethtent());
1065 dn_skipname(comp_dn, eom)
1066 const u_char *comp_dn, *eom;
1068 return (__dn_skipname(comp_dn, eom));
1070 #endif /*old-style libc with yp junk in it*/