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>
75 # define MAXALIASES 35
78 static char *h_addr_ptrs
[MAXADDRS
+ 1];
80 static struct hostent host
;
81 static char *host_aliases
[MAXALIASES
];
82 static char hostbuf
[8*1024];
83 static u_char host_addr
[16]; /* IPv4 or IPv6 */
84 static FILE *hostf
= NULL
;
85 static int stayopen
= 0;
87 static void map_v4v6_address (const char *src
, char *dst
) __THROW
;
88 static void map_v4v6_hostent (struct hostent
*hp
, char **bp
, int *len
) __THROW
;
90 extern void addrsort (char **, int) __THROW
;
93 # define MAXPACKET PACKETSZ
95 # define MAXPACKET 65536
98 /* As per RFC 1034 and 1035 a host name cannot exceed 255 octets in length. */
99 # ifdef MAXHOSTNAMELEN
100 # undef MAXHOSTNAMELEN
102 # define MAXHOSTNAMELEN 256
106 u_char buf
[MAXPACKET
];
120 Dprintf (char *msg
, int num
)
122 if (_res
.options
& RES_DEBUG
) {
130 # define Dprintf(msg, num) /*nada*/
133 # define BOUNDED_INCR(x) \
137 __set_h_errno (NO_RECOVERY); \
142 # define BOUNDS_CHECK(ptr, count) \
144 if ((ptr) + (count) > eom) { \
145 __set_h_errno (NO_RECOVERY); \
151 static struct hostent
*
152 getanswer (const querybuf
*answer
, int anslen
, const char *qname
, int qtype
)
157 const u_char
*eom
, *erdata
;
158 char *bp
, **ap
, **hap
;
159 int type
, class, buflen
, ancount
, qdcount
;
160 int haveanswer
, had_error
;
164 int (*name_ok
) (const char *);
168 eom
= answer
->buf
+ anslen
;
178 return (NULL
); /* XXX should be abort(); */
181 * find first satisfactory answer
184 ancount
= ntohs(hp
->ancount
);
185 qdcount
= ntohs(hp
->qdcount
);
187 buflen
= sizeof hostbuf
;
189 BOUNDED_INCR(HFIXEDSZ
);
191 __set_h_errno (NO_RECOVERY
);
194 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
195 if ((n
< 0) || !(*name_ok
)(bp
)) {
196 __set_h_errno (NO_RECOVERY
);
199 BOUNDED_INCR(n
+ QFIXEDSZ
);
200 if (qtype
== T_A
|| qtype
== T_AAAA
) {
201 /* res_send() has already verified that the query name is the
202 * same as the one we sent; this just gets the expanded name
203 * (i.e., with the succeeding search-domain tacked on).
205 n
= strlen(bp
) + 1; /* for the \0 */
206 if (n
>= MAXHOSTNAMELEN
) {
207 __set_h_errno (NO_RECOVERY
);
213 /* The qname can be abbreviated, but h_name is now absolute. */
218 host
.h_aliases
= host_aliases
;
221 host
.h_addr_list
= h_addr_ptrs
;
224 while (ancount
-- > 0 && cp
< eom
&& !had_error
) {
225 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
226 if ((n
< 0) || !(*name_ok
)(bp
)) {
231 BOUNDS_CHECK(cp
, 3 * INT16SZ
+ INT32SZ
);
233 cp
+= INT16SZ
; /* type */
234 class = ns_get16(cp
);
235 cp
+= INT16SZ
+ INT32SZ
; /* class, TTL */
237 cp
+= INT16SZ
; /* len */
241 /* XXX - debug? syslog? */
243 continue; /* XXX - had_error++ ? */
245 if ((qtype
== T_A
|| qtype
== T_AAAA
) && type
== T_CNAME
) {
246 if (ap
>= &host_aliases
[MAXALIASES
-1])
248 n
= dn_expand(answer
->buf
, eom
, cp
, tbuf
, sizeof tbuf
);
249 if ((n
< 0) || !(*name_ok
)(tbuf
)) {
255 __set_h_errno (NO_RECOVERY
);
260 n
= strlen(bp
) + 1; /* for the \0 */
261 if (n
>= MAXHOSTNAMELEN
) {
267 /* Get canonical name. */
268 n
= strlen(tbuf
) + 1; /* for the \0 */
269 if (n
> buflen
|| n
>= MAXHOSTNAMELEN
) {
279 if (qtype
== T_PTR
&& type
== T_CNAME
) {
280 n
= dn_expand(answer
->buf
, eom
, cp
, tbuf
, sizeof tbuf
);
281 if (n
< 0 || !res_dnok(tbuf
)) {
287 __set_h_errno (NO_RECOVERY
);
290 /* Get canonical name. */
291 n
= strlen(tbuf
) + 1; /* for the \0 */
292 if (n
> buflen
|| n
>= MAXHOSTNAMELEN
) {
303 /* Log a low priority message if we get an unexpected
304 * record, but skip it if we are using DNSSEC since it
305 * uses many different types in responses that do not
309 continue; /* XXX - had_error++ ? */
313 if (strcasecmp(tname
, bp
) != 0) {
315 continue; /* XXX - had_error++ ? */
317 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
318 if ((n
< 0) || !res_hnok(bp
)) {
324 __set_h_errno (NO_RECOVERY
);
329 else if (ap
< &host_aliases
[MAXALIASES
-1])
334 n
= strlen(bp
) + 1; /* for the \0 */
335 if (n
>= MAXHOSTNAMELEN
) {
345 if (strcasecmp(host
.h_name
, bp
) != 0) {
347 continue; /* XXX - had_error++ ? */
349 if (n
!= host
.h_length
) {
357 nn
= strlen(bp
) + 1; /* for the \0 */
362 /* XXX: when incrementing bp, we have to decrement
363 * buflen by the same amount --okir */
364 buflen
-= sizeof(align
) - ((u_long
)bp
% sizeof(align
));
366 bp
+= sizeof(align
) - ((u_long
)bp
% sizeof(align
));
368 if (bp
+ n
>= &hostbuf
[sizeof hostbuf
]) {
369 Dprintf("size (%d) too big\n", n
);
373 if (hap
>= &h_addr_ptrs
[MAXADDRS
-1]) {
375 Dprintf("Too many addresses (%d)\n",
381 memmove(*hap
++ = bp
, cp
, n
);
386 __set_h_errno (NO_RECOVERY
);
400 * Note: we sort even if host can take only one address
401 * in its return structures - should give it the "best"
402 * address in that case, not some random one
404 if (_res
.nsort
&& haveanswer
> 1 && qtype
== T_A
)
405 addrsort(h_addr_ptrs
, haveanswer
);
407 n
= strlen(qname
) + 1; /* for the \0 */
408 if (n
> buflen
|| n
>= MAXHOSTNAMELEN
)
415 if (res_use_inet6 ())
416 map_v4v6_hostent(&host
, &bp
, &buflen
);
417 __set_h_errno (NETDB_SUCCESS
);
421 __set_h_errno (NO_RECOVERY
);
425 extern struct hostent
*res_gethostbyname2(const char *name
, int af
);
426 libresolv_hidden_proto (res_gethostbyname2
)
429 res_gethostbyname (const char *name
)
433 if (__res_maybe_init (&_res
, 0) == -1) {
434 __set_h_errno (NETDB_INTERNAL
);
437 if (res_use_inet6 ()) {
438 hp
= res_gethostbyname2(name
, AF_INET6
);
442 return (res_gethostbyname2(name
, AF_INET
));
444 compat_symbol (libresolv
, res_gethostbyname
, res_gethostbyname
, GLIBC_2_0
);
447 res_gethostbyname2 (const char *name
, int af
)
457 int n
, size
, type
, len
;
460 if (__res_maybe_init (&_res
, 0) == -1) {
461 __set_h_errno (NETDB_INTERNAL
);
475 __set_h_errno (NETDB_INTERNAL
);
476 __set_errno (EAFNOSUPPORT
);
480 host
.h_addrtype
= af
;
481 host
.h_length
= size
;
484 * if there aren't any dots, it could be a user-level alias.
485 * this is also done in res_query() since we are not the only
486 * function that looks up host names.
488 if (!strchr(name
, '.') && (cp
= __hostalias(name
)))
492 * disallow names consisting only of digits/dots, unless
495 if (isdigit(name
[0]))
496 for (cp
= name
;; ++cp
) {
501 * All-numeric, no dot at the end.
502 * Fake up a hostent as if we'd actually
505 if (inet_pton(af
, name
, host_addr
) <= 0) {
506 __set_h_errno (HOST_NOT_FOUND
);
509 strncpy(hostbuf
, name
, MAXDNAME
);
510 hostbuf
[MAXDNAME
] = '\0';
511 bp
= hostbuf
+ MAXDNAME
;
512 len
= sizeof hostbuf
- MAXDNAME
;
513 host
.h_name
= hostbuf
;
514 host
.h_aliases
= host_aliases
;
515 host_aliases
[0] = NULL
;
516 h_addr_ptrs
[0] = (char *)host_addr
;
517 h_addr_ptrs
[1] = NULL
;
518 host
.h_addr_list
= h_addr_ptrs
;
519 if (res_use_inet6 ())
520 map_v4v6_hostent(&host
, &bp
, &len
);
521 __set_h_errno (NETDB_SUCCESS
);
524 if (!isdigit(*cp
) && *cp
!= '.')
527 if ((isxdigit(name
[0]) && strchr(name
, ':') != NULL
) ||
529 for (cp
= name
;; ++cp
) {
534 * All-IPv6-legal, no dot at the end.
535 * Fake up a hostent as if we'd actually
538 if (inet_pton(af
, name
, host_addr
) <= 0) {
539 __set_h_errno (HOST_NOT_FOUND
);
542 strncpy(hostbuf
, name
, MAXDNAME
);
543 hostbuf
[MAXDNAME
] = '\0';
544 bp
= hostbuf
+ MAXDNAME
;
545 len
= sizeof hostbuf
- MAXDNAME
;
546 host
.h_name
= hostbuf
;
547 host
.h_aliases
= host_aliases
;
548 host_aliases
[0] = NULL
;
549 h_addr_ptrs
[0] = (char *)host_addr
;
550 h_addr_ptrs
[1] = NULL
;
551 host
.h_addr_list
= h_addr_ptrs
;
552 __set_h_errno (NETDB_SUCCESS
);
555 if (!isxdigit(*cp
) && *cp
!= ':' && *cp
!= '.')
559 buf
.buf
= origbuf
= (querybuf
*) alloca (1024);
561 if ((n
= __libc_res_nsearch(&_res
, name
, C_IN
, type
, buf
.buf
->buf
, 1024,
562 &buf
.ptr
, NULL
, NULL
, NULL
, NULL
)) < 0) {
563 if (buf
.buf
!= origbuf
)
565 Dprintf("res_nsearch failed (%d)\n", n
);
566 if (errno
== ECONNREFUSED
)
567 return (_gethtbyname2(name
, af
));
570 ret
= getanswer(buf
.buf
, n
, name
, type
);
571 if (buf
.buf
!= origbuf
)
575 libresolv_hidden_def (res_gethostbyname2
)
576 compat_symbol (libresolv
, res_gethostbyname2
, res_gethostbyname2
, GLIBC_2_0
);
579 res_gethostbyaddr (const void *addr
, socklen_t len
, int af
)
581 const u_char
*uaddr
= (const u_char
*)addr
;
582 static const u_char mapped
[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
583 static const u_char tunnelled
[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
593 char qbuf
[MAXDNAME
+1], *qp
= NULL
;
595 if (__res_maybe_init (&_res
, 0) == -1) {
596 __set_h_errno (NETDB_INTERNAL
);
599 if (af
== AF_INET6
&& len
== IN6ADDRSZ
&&
600 (!memcmp(uaddr
, mapped
, sizeof mapped
) ||
601 !memcmp(uaddr
, tunnelled
, sizeof tunnelled
))) {
603 addr
+= sizeof mapped
;
604 uaddr
+= sizeof mapped
;
616 __set_errno (EAFNOSUPPORT
);
617 __set_h_errno (NETDB_INTERNAL
);
621 __set_errno (EINVAL
);
622 __set_h_errno (NETDB_INTERNAL
);
627 (void) sprintf(qbuf
, "%u.%u.%u.%u.in-addr.arpa",
635 for (n
= IN6ADDRSZ
- 1; n
>= 0; n
--) {
636 qp
+= sprintf(qp
, "%x.%x.",
638 (uaddr
[n
] >> 4) & 0xf);
640 strcpy(qp
, "ip6.arpa");
646 buf
.buf
= orig_buf
= (querybuf
*) alloca (1024);
648 n
= __libc_res_nquery(&_res
, qbuf
, C_IN
, T_PTR
, buf
.buf
->buf
, 1024,
649 &buf
.ptr
, NULL
, NULL
, NULL
, NULL
);
651 if (buf
.buf
!= orig_buf
)
653 Dprintf("res_nquery failed (%d)\n", n
);
654 if (errno
== ECONNREFUSED
)
655 return (_gethtbyaddr(addr
, len
, af
));
658 hp
= getanswer(buf
.buf
, n
, qbuf
, T_PTR
);
659 if (buf
.buf
!= orig_buf
)
662 return (NULL
); /* h_errno was set by getanswer() */
665 memmove(host_addr
, addr
, len
);
666 h_addr_ptrs
[0] = (char *)host_addr
;
667 h_addr_ptrs
[1] = NULL
;
668 if (af
== AF_INET
&& res_use_inet6 ()) {
669 map_v4v6_address((char*)host_addr
, (char*)host_addr
);
670 hp
->h_addrtype
= AF_INET6
;
671 hp
->h_length
= IN6ADDRSZ
;
673 __set_h_errno (NETDB_SUCCESS
);
676 compat_symbol (libresolv
, res_gethostbyaddr
, res_gethostbyaddr
, GLIBC_2_0
);
682 hostf
= fopen(_PATH_HOSTS
, "rce" );
687 libresolv_hidden_def (_sethtent
)
688 compat_symbol (libresolv
, _sethtent
, _sethtent
, GLIBC_2_0
);
693 if (hostf
&& !stayopen
) {
694 (void) fclose(hostf
);
706 if (!hostf
&& !(hostf
= fopen(_PATH_HOSTS
, "rce" ))) {
707 __set_h_errno (NETDB_INTERNAL
);
711 if (!(p
= fgets(hostbuf
, sizeof hostbuf
, hostf
))) {
712 __set_h_errno (HOST_NOT_FOUND
);
717 if (!(cp
= strpbrk(p
, "#\n")))
720 if (!(cp
= strpbrk(p
, " \t")))
723 if (inet_pton(AF_INET6
, p
, host_addr
) > 0) {
726 } else if (inet_pton(AF_INET
, p
, host_addr
) > 0) {
727 if (res_use_inet6 ()) {
728 map_v4v6_address((char*)host_addr
, (char*)host_addr
);
738 h_addr_ptrs
[0] = (char *)host_addr
;
739 h_addr_ptrs
[1] = NULL
;
740 host
.h_addr_list
= h_addr_ptrs
;
742 host
.h_addrtype
= af
;
743 while (*cp
== ' ' || *cp
== '\t')
746 q
= host
.h_aliases
= host_aliases
;
747 if ((cp
= strpbrk(cp
, " \t")))
750 if (*cp
== ' ' || *cp
== '\t') {
754 if (q
< &host_aliases
[MAXALIASES
- 1])
756 if ((cp
= strpbrk(cp
, " \t")))
760 __set_h_errno (NETDB_SUCCESS
);
763 libresolv_hidden_def (_gethtent
)
764 compat_symbol (libresolv
, _gethtent
, _gethtent
, GLIBC_2_0
);
767 _gethtbyname (const char *name
)
771 if (res_use_inet6 ()) {
772 hp
= _gethtbyname2(name
, AF_INET6
);
776 return (_gethtbyname2(name
, AF_INET
));
778 compat_symbol (libresolv
, _gethtbyname
, _gethtbyname
, GLIBC_2_0
);
781 _gethtbyname2 (const char *name
, int af
)
787 while ((p
= _gethtent())) {
788 if (p
->h_addrtype
!= af
)
790 if (strcasecmp(p
->h_name
, name
) == 0)
792 for (cp
= p
->h_aliases
; *cp
!= 0; cp
++)
793 if (strcasecmp(*cp
, name
) == 0)
800 libresolv_hidden_def (_gethtbyname2
)
801 compat_symbol (libresolv
, _gethtbyname2
, _gethtbyname2
, GLIBC_2_0
);
804 _gethtbyaddr (const char *addr
, size_t len
, int af
)
809 while ((p
= _gethtent()))
810 if (p
->h_addrtype
== af
&& !memcmp(p
->h_addr
, addr
, len
))
815 libresolv_hidden_def (_gethtbyaddr
)
816 compat_symbol (libresolv
, _gethtbyaddr
, _gethtbyaddr
, GLIBC_2_0
);
819 map_v4v6_address (const char *src
, char *dst
)
821 u_char
*p
= (u_char
*)dst
;
825 /* Stash a temporary copy so our caller can update in place. */
826 memcpy(tmp
, src
, INADDRSZ
);
827 /* Mark this ipv6 addr as a mapped ipv4. */
828 for (i
= 0; i
< 10; i
++)
832 /* Retrieve the saved copy and we're done. */
833 memcpy((void*)p
, tmp
, INADDRSZ
);
837 map_v4v6_hostent (struct hostent
*hp
, char **bpp
, int *lenp
)
841 if (hp
->h_addrtype
!= AF_INET
|| hp
->h_length
!= INADDRSZ
)
843 hp
->h_addrtype
= AF_INET6
;
844 hp
->h_length
= IN6ADDRSZ
;
845 for (ap
= hp
->h_addr_list
; *ap
; ap
++) {
846 int i
= sizeof(align
) - ((u_long
)*bpp
% sizeof(align
));
848 if (*lenp
< (i
+ IN6ADDRSZ
)) {
849 /* Out of memory. Truncate address list here. XXX */
855 map_v4v6_address(*ap
, *bpp
);
863 addrsort (char **ap
, int num
)
867 short aval
[MAXADDRS
];
871 for (i
= 0; i
< num
; i
++, p
++) {
872 for (j
= 0 ; (unsigned)j
< _res
.nsort
; j
++)
873 if (_res
.sort_list
[j
].addr
.s_addr
==
874 (((struct in_addr
*)(*p
))->s_addr
& _res
.sort_list
[j
].mask
))
877 if (needsort
== 0 && i
> 0 && j
< aval
[i
-1])
883 while (needsort
< num
) {
884 for (j
= needsort
- 1; j
>= 0; j
--) {
885 if (aval
[j
] > aval
[j
+1]) {
903 #endif /* SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_25) */