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 /* XXX This file is not used by any of the resolver functions implemented by
53 glibc (i.e. get*info and gethostby*). It cannot be removed however because
54 it exports symbols in the libresolv ABI. The file is not maintained any
55 more, nor are these functions. */
57 #include <shlib-compat.h>
58 #if SHLIB_COMPAT (libresolv, GLIBC_2_0, GLIBC_2_25)
60 # include <sys/types.h>
61 # include <sys/param.h>
62 # include <sys/socket.h>
63 # include <netinet/in.h>
64 # include <arpa/inet.h>
65 # include <arpa/nameser.h>
69 # include <resolv/resolv-internal.h>
70 # include <resolv/resolv_context.h>
76 # define MAXALIASES 35
79 static char *h_addr_ptrs[MAXADDRS + 1];
81 static struct hostent host;
82 static char *host_aliases[MAXALIASES];
83 static char hostbuf[8*1024];
84 static u_char host_addr[16]; /* IPv4 or IPv6 */
85 static FILE *hostf = NULL;
86 static int stayopen = 0;
88 static struct hostent *res_gethostbyname2_context (struct resolv_context *,
89 const char *name, int af);
91 static void map_v4v6_address (const char *src, char *dst) __THROW;
92 static void map_v4v6_hostent (struct hostent *hp, char **bp, int *len) __THROW;
94 extern void addrsort (char **, int) __THROW;
97 # define MAXPACKET PACKETSZ
99 # define MAXPACKET 65536
102 /* As per RFC 1034 and 1035 a host name cannot exceed 255 octets in length. */
103 # ifdef MAXHOSTNAMELEN
104 # undef MAXHOSTNAMELEN
106 # define MAXHOSTNAMELEN 256
110 u_char buf[MAXPACKET];
122 # define BOUNDED_INCR(x) \
126 __set_h_errno (NO_RECOVERY); \
131 # define BOUNDS_CHECK(ptr, count) \
133 if ((ptr) + (count) > eom) { \
134 __set_h_errno (NO_RECOVERY); \
140 static struct hostent *
141 getanswer (const querybuf *answer, int anslen, const char *qname, int qtype)
146 const u_char *eom, *erdata;
147 char *bp, **ap, **hap;
148 int type, class, buflen, ancount, qdcount;
149 int haveanswer, had_error;
152 int (*name_ok) (const char *);
156 eom = answer->buf + anslen;
166 return (NULL); /* XXX should be abort(); */
169 * find first satisfactory answer
172 ancount = ntohs(hp->ancount);
173 qdcount = ntohs(hp->qdcount);
175 buflen = sizeof hostbuf;
177 BOUNDED_INCR(HFIXEDSZ);
179 __set_h_errno (NO_RECOVERY);
182 n = dn_expand(answer->buf, eom, cp, bp, buflen);
183 if ((n < 0) || !(*name_ok)(bp)) {
184 __set_h_errno (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 __set_h_errno (NO_RECOVERY);
201 /* The qname can be abbreviated, but h_name is now absolute. */
206 host.h_aliases = host_aliases;
209 host.h_addr_list = h_addr_ptrs;
212 while (ancount-- > 0 && cp < eom && !had_error) {
213 n = dn_expand(answer->buf, eom, cp, bp, buflen);
214 if ((n < 0) || !(*name_ok)(bp)) {
219 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
221 cp += INT16SZ; /* type */
222 class = ns_get16(cp);
223 cp += INT16SZ + INT32SZ; /* class, TTL */
225 cp += INT16SZ; /* len */
229 /* XXX - debug? syslog? */
231 continue; /* XXX - had_error++ ? */
233 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
234 if (ap >= &host_aliases[MAXALIASES-1])
236 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
237 if ((n < 0) || !(*name_ok)(tbuf)) {
243 __set_h_errno (NO_RECOVERY);
248 n = strlen(bp) + 1; /* for the \0 */
249 if (n >= MAXHOSTNAMELEN) {
255 /* Get canonical name. */
256 n = strlen(tbuf) + 1; /* for the \0 */
257 if (n > buflen || n >= MAXHOSTNAMELEN) {
267 if (qtype == T_PTR && type == T_CNAME) {
268 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
269 if (n < 0 || !res_dnok(tbuf)) {
275 __set_h_errno (NO_RECOVERY);
278 /* Get canonical name. */
279 n = strlen(tbuf) + 1; /* for the \0 */
280 if (n > buflen || n >= MAXHOSTNAMELEN) {
291 /* Log a low priority message if we get an unexpected
292 * record, but skip it if we are using DNSSEC since it
293 * uses many different types in responses that do not
297 continue; /* XXX - had_error++ ? */
301 if (strcasecmp(tname, bp) != 0) {
303 continue; /* XXX - had_error++ ? */
305 n = dn_expand(answer->buf, eom, cp, bp, buflen);
306 if ((n < 0) || !res_hnok(bp)) {
312 __set_h_errno (NO_RECOVERY);
317 else if (ap < &host_aliases[MAXALIASES-1])
322 n = strlen(bp) + 1; /* for the \0 */
323 if (n >= MAXHOSTNAMELEN) {
333 if (strcasecmp(host.h_name, bp) != 0) {
335 continue; /* XXX - had_error++ ? */
337 if (n != host.h_length) {
345 nn = strlen(bp) + 1; /* for the \0 */
350 /* XXX: when incrementing bp, we have to decrement
351 * buflen by the same amount --okir */
352 buflen -= sizeof(align) - ((u_long)bp % sizeof(align));
354 bp += sizeof(align) - ((u_long)bp % sizeof(align));
356 if (bp + n >= &hostbuf[sizeof hostbuf]) {
360 if (hap >= &h_addr_ptrs[MAXADDRS-1]) {
364 memmove(*hap++ = bp, cp, n);
369 __set_h_errno (NO_RECOVERY);
383 * Note: we sort even if host can take only one address
384 * in its return structures - should give it the "best"
385 * address in that case, not some random one
387 if (_res.nsort && haveanswer > 1 && qtype == T_A)
388 addrsort(h_addr_ptrs, haveanswer);
390 n = strlen(qname) + 1; /* for the \0 */
391 if (n > buflen || n >= MAXHOSTNAMELEN)
398 if (res_use_inet6 ())
399 map_v4v6_hostent(&host, &bp, &buflen);
400 __set_h_errno (NETDB_SUCCESS);
404 __set_h_errno (NO_RECOVERY);
408 extern struct hostent *res_gethostbyname2(const char *name, int af);
409 libresolv_hidden_proto (res_gethostbyname2)
412 res_gethostbyname (const char *name)
414 struct resolv_context *ctx = __resolv_context_get ();
417 __set_h_errno (NETDB_INTERNAL);
421 if (res_use_inet6 ())
423 struct hostent *hp = res_gethostbyname2_context (ctx, name, AF_INET6);
426 __resolv_context_put (ctx);
430 struct hostent *hp = res_gethostbyname2_context (ctx, name, AF_INET);
431 __resolv_context_put (ctx);
434 compat_symbol (libresolv, res_gethostbyname, res_gethostbyname, GLIBC_2_0);
436 static struct hostent *
437 res_gethostbyname2_context (struct resolv_context *ctx,
438 const char *name, int af)
448 int n, size, type, len;
461 __set_h_errno (NETDB_INTERNAL);
462 __set_errno (EAFNOSUPPORT);
466 host.h_addrtype = af;
467 host.h_length = size;
470 * if there aren't any dots, it could be a user-level alias.
471 * this is also done in res_query() since we are not the only
472 * function that looks up host names.
475 if (strchr (name, '.') != NULL
476 && (cp = __res_context_hostalias (ctx, name, abuf, sizeof (abuf))))
480 * disallow names consisting only of digits/dots, unless
483 if (isdigit(name[0]))
484 for (cp = name;; ++cp) {
489 * All-numeric, no dot at the end.
490 * Fake up a hostent as if we'd actually
493 if (inet_pton(af, name, host_addr) <= 0) {
494 __set_h_errno (HOST_NOT_FOUND);
497 strncpy(hostbuf, name, MAXDNAME);
498 hostbuf[MAXDNAME] = '\0';
499 bp = hostbuf + MAXDNAME;
500 len = sizeof hostbuf - MAXDNAME;
501 host.h_name = hostbuf;
502 host.h_aliases = host_aliases;
503 host_aliases[0] = NULL;
504 h_addr_ptrs[0] = (char *)host_addr;
505 h_addr_ptrs[1] = NULL;
506 host.h_addr_list = h_addr_ptrs;
507 if (res_use_inet6 ())
508 map_v4v6_hostent(&host, &bp, &len);
509 __set_h_errno (NETDB_SUCCESS);
512 if (!isdigit(*cp) && *cp != '.')
515 if ((isxdigit(name[0]) && strchr(name, ':') != NULL) ||
517 for (cp = name;; ++cp) {
522 * All-IPv6-legal, no dot at the end.
523 * Fake up a hostent as if we'd actually
526 if (inet_pton(af, name, host_addr) <= 0) {
527 __set_h_errno (HOST_NOT_FOUND);
530 strncpy(hostbuf, name, MAXDNAME);
531 hostbuf[MAXDNAME] = '\0';
532 bp = hostbuf + MAXDNAME;
533 len = sizeof hostbuf - MAXDNAME;
534 host.h_name = hostbuf;
535 host.h_aliases = host_aliases;
536 host_aliases[0] = NULL;
537 h_addr_ptrs[0] = (char *)host_addr;
538 h_addr_ptrs[1] = NULL;
539 host.h_addr_list = h_addr_ptrs;
540 __set_h_errno (NETDB_SUCCESS);
543 if (!isxdigit(*cp) && *cp != ':' && *cp != '.')
547 buf.buf = origbuf = (querybuf *) alloca (1024);
549 if ((n = __res_context_search
550 (ctx, name, C_IN, type, buf.buf->buf, 1024,
551 &buf.ptr, NULL, NULL, NULL, NULL)) < 0) {
552 if (buf.buf != origbuf)
554 if (errno == ECONNREFUSED)
555 return (_gethtbyname2(name, af));
558 ret = getanswer(buf.buf, n, name, type);
559 if (buf.buf != origbuf)
565 res_gethostbyname2 (const char *name, int af)
567 struct resolv_context *ctx = __resolv_context_get ();
570 __set_h_errno (NETDB_INTERNAL);
573 struct hostent *hp = res_gethostbyname2_context (ctx, name, AF_INET);
574 __resolv_context_put (ctx);
577 libresolv_hidden_def (res_gethostbyname2)
578 compat_symbol (libresolv, res_gethostbyname2, res_gethostbyname2, GLIBC_2_0);
580 static struct hostent *
581 res_gethostbyaddr_context (struct resolv_context *ctx,
582 const void *addr, socklen_t len, int af)
584 const u_char *uaddr = (const u_char *)addr;
585 static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
586 static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
596 char qbuf[MAXDNAME+1], *qp = NULL;
598 if (af == AF_INET6 && len == IN6ADDRSZ &&
599 (!memcmp(uaddr, mapped, sizeof mapped) ||
600 !memcmp(uaddr, tunnelled, sizeof tunnelled))) {
602 addr += sizeof mapped;
603 uaddr += sizeof mapped;
615 __set_errno (EAFNOSUPPORT);
616 __set_h_errno (NETDB_INTERNAL);
620 __set_errno (EINVAL);
621 __set_h_errno (NETDB_INTERNAL);
626 (void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
634 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
635 qp += sprintf(qp, "%x.%x.",
637 (uaddr[n] >> 4) & 0xf);
639 strcpy(qp, "ip6.arpa");
645 buf.buf = orig_buf = (querybuf *) alloca (1024);
647 n = __res_context_query (ctx, qbuf, C_IN, T_PTR, buf.buf->buf, 1024,
648 &buf.ptr, NULL, NULL, NULL, NULL);
650 if (buf.buf != orig_buf)
652 if (errno == ECONNREFUSED)
653 return (_gethtbyaddr(addr, len, af));
656 hp = getanswer(buf.buf, n, qbuf, T_PTR);
657 if (buf.buf != orig_buf)
660 return (NULL); /* h_errno was set by getanswer() */
663 memmove(host_addr, addr, len);
664 h_addr_ptrs[0] = (char *)host_addr;
665 h_addr_ptrs[1] = NULL;
666 if (af == AF_INET && res_use_inet6 ()) {
667 map_v4v6_address((char*)host_addr, (char*)host_addr);
668 hp->h_addrtype = AF_INET6;
669 hp->h_length = IN6ADDRSZ;
671 __set_h_errno (NETDB_SUCCESS);
676 res_gethostbyaddr (const void *addr, socklen_t len, int af)
678 struct resolv_context *ctx = __resolv_context_get ();
681 __set_h_errno (NETDB_INTERNAL);
684 struct hostent *hp = res_gethostbyaddr_context (ctx, addr, len, af);
685 __resolv_context_put (ctx);
688 compat_symbol (libresolv, res_gethostbyaddr, res_gethostbyaddr, GLIBC_2_0);
694 hostf = fopen(_PATH_HOSTS, "rce" );
699 libresolv_hidden_def (_sethtent)
700 compat_symbol (libresolv, _sethtent, _sethtent, GLIBC_2_0);
705 if (hostf && !stayopen) {
706 (void) fclose(hostf);
718 if (!hostf && !(hostf = fopen(_PATH_HOSTS, "rce" ))) {
719 __set_h_errno (NETDB_INTERNAL);
723 if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) {
724 __set_h_errno (HOST_NOT_FOUND);
729 if (!(cp = strpbrk(p, "#\n")))
732 if (!(cp = strpbrk(p, " \t")))
735 if (inet_pton(AF_INET6, p, host_addr) > 0) {
738 } else if (inet_pton(AF_INET, p, host_addr) > 0) {
739 if (res_use_inet6 ()) {
740 map_v4v6_address((char*)host_addr, (char*)host_addr);
750 h_addr_ptrs[0] = (char *)host_addr;
751 h_addr_ptrs[1] = NULL;
752 host.h_addr_list = h_addr_ptrs;
754 host.h_addrtype = af;
755 while (*cp == ' ' || *cp == '\t')
758 q = host.h_aliases = host_aliases;
759 if ((cp = strpbrk(cp, " \t")))
762 if (*cp == ' ' || *cp == '\t') {
766 if (q < &host_aliases[MAXALIASES - 1])
768 if ((cp = strpbrk(cp, " \t")))
772 __set_h_errno (NETDB_SUCCESS);
775 libresolv_hidden_def (_gethtent)
776 compat_symbol (libresolv, _gethtent, _gethtent, GLIBC_2_0);
779 _gethtbyname (const char *name)
783 if (res_use_inet6 ()) {
784 hp = _gethtbyname2(name, AF_INET6);
788 return (_gethtbyname2(name, AF_INET));
790 compat_symbol (libresolv, _gethtbyname, _gethtbyname, GLIBC_2_0);
793 _gethtbyname2 (const char *name, int af)
799 while ((p = _gethtent())) {
800 if (p->h_addrtype != af)
802 if (strcasecmp(p->h_name, name) == 0)
804 for (cp = p->h_aliases; *cp != 0; cp++)
805 if (strcasecmp(*cp, name) == 0)
812 libresolv_hidden_def (_gethtbyname2)
813 compat_symbol (libresolv, _gethtbyname2, _gethtbyname2, GLIBC_2_0);
816 _gethtbyaddr (const char *addr, size_t len, int af)
821 while ((p = _gethtent()))
822 if (p->h_addrtype == af && !memcmp(p->h_addr, addr, len))
827 libresolv_hidden_def (_gethtbyaddr)
828 compat_symbol (libresolv, _gethtbyaddr, _gethtbyaddr, GLIBC_2_0);
831 map_v4v6_address (const char *src, char *dst)
833 u_char *p = (u_char *)dst;
837 /* Stash a temporary copy so our caller can update in place. */
838 memcpy(tmp, src, INADDRSZ);
839 /* Mark this ipv6 addr as a mapped ipv4. */
840 for (i = 0; i < 10; i++)
844 /* Retrieve the saved copy and we're done. */
845 memcpy((void*)p, tmp, INADDRSZ);
849 map_v4v6_hostent (struct hostent *hp, char **bpp, int *lenp)
853 if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
855 hp->h_addrtype = AF_INET6;
856 hp->h_length = IN6ADDRSZ;
857 for (ap = hp->h_addr_list; *ap; ap++) {
858 int i = sizeof(align) - ((u_long)*bpp % sizeof(align));
860 if (*lenp < (i + IN6ADDRSZ)) {
861 /* Out of memory. Truncate address list here. XXX */
867 map_v4v6_address(*ap, *bpp);
875 addrsort (char **ap, int num)
879 short aval[MAXADDRS];
883 for (i = 0; i < num; i++, p++) {
884 for (j = 0 ; (unsigned)j < _res.nsort; j++)
885 if (_res.sort_list[j].addr.s_addr ==
886 (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask))
889 if (needsort == 0 && i > 0 && j < aval[i-1])
895 while (needsort < num) {
896 for (j = needsort - 1; j >= 0; j--) {
897 if (aval[j] > aval[j+1]) {
915 #endif /* SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_25) */