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
= __libc_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
= __libc_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
= __libc_dn_expand (answer
->buf
, eom
, cp
,
238 if ((n
< 0) || !(*name_ok
)(tbuf
)) {
244 __set_h_errno (NO_RECOVERY
);
249 n
= strlen(bp
) + 1; /* for the \0 */
250 if (n
>= MAXHOSTNAMELEN
) {
256 /* Get canonical name. */
257 n
= strlen(tbuf
) + 1; /* for the \0 */
258 if (n
> buflen
|| n
>= MAXHOSTNAMELEN
) {
268 if (qtype
== T_PTR
&& type
== T_CNAME
) {
269 n
= __libc_dn_expand (answer
->buf
, eom
, cp
,
271 if (n
< 0 || !res_dnok(tbuf
)) {
277 __set_h_errno (NO_RECOVERY
);
280 /* Get canonical name. */
281 n
= strlen(tbuf
) + 1; /* for the \0 */
282 if (n
> buflen
|| n
>= MAXHOSTNAMELEN
) {
293 /* Log a low priority message if we get an unexpected
294 * record, but skip it if we are using DNSSEC since it
295 * uses many different types in responses that do not
299 continue; /* XXX - had_error++ ? */
303 if (strcasecmp(tname
, bp
) != 0) {
305 continue; /* XXX - had_error++ ? */
307 n
= __libc_dn_expand (answer
->buf
, eom
, cp
,
309 if ((n
< 0) || !res_hnok(bp
)) {
315 __set_h_errno (NO_RECOVERY
);
320 else if (ap
< &host_aliases
[MAXALIASES
-1])
325 n
= strlen(bp
) + 1; /* for the \0 */
326 if (n
>= MAXHOSTNAMELEN
) {
336 if (strcasecmp(host
.h_name
, bp
) != 0) {
338 continue; /* XXX - had_error++ ? */
340 if (n
!= host
.h_length
) {
348 nn
= strlen(bp
) + 1; /* for the \0 */
353 /* XXX: when incrementing bp, we have to decrement
354 * buflen by the same amount --okir */
355 buflen
-= sizeof(align
) - ((u_long
)bp
% sizeof(align
));
357 bp
+= sizeof(align
) - ((u_long
)bp
% sizeof(align
));
359 if (bp
+ n
>= &hostbuf
[sizeof hostbuf
]) {
363 if (hap
>= &h_addr_ptrs
[MAXADDRS
-1]) {
367 memmove(*hap
++ = bp
, cp
, n
);
372 __set_h_errno (NO_RECOVERY
);
386 * Note: we sort even if host can take only one address
387 * in its return structures - should give it the "best"
388 * address in that case, not some random one
390 if (_res
.nsort
&& haveanswer
> 1 && qtype
== T_A
)
391 addrsort(h_addr_ptrs
, haveanswer
);
393 n
= strlen(qname
) + 1; /* for the \0 */
394 if (n
> buflen
|| n
>= MAXHOSTNAMELEN
)
401 if (res_use_inet6 ())
402 map_v4v6_hostent(&host
, &bp
, &buflen
);
403 __set_h_errno (NETDB_SUCCESS
);
407 __set_h_errno (NO_RECOVERY
);
411 extern struct hostent
*res_gethostbyname2(const char *name
, int af
);
412 libresolv_hidden_proto (res_gethostbyname2
)
415 res_gethostbyname (const char *name
)
417 struct resolv_context
*ctx
= __resolv_context_get ();
420 __set_h_errno (NETDB_INTERNAL
);
424 if (res_use_inet6 ())
426 struct hostent
*hp
= res_gethostbyname2_context (ctx
, name
, AF_INET6
);
429 __resolv_context_put (ctx
);
433 struct hostent
*hp
= res_gethostbyname2_context (ctx
, name
, AF_INET
);
434 __resolv_context_put (ctx
);
437 compat_symbol (libresolv
, res_gethostbyname
, res_gethostbyname
, GLIBC_2_0
);
439 static struct hostent
*
440 res_gethostbyname2_context (struct resolv_context
*ctx
,
441 const char *name
, int af
)
451 int n
, size
, type
, len
;
464 __set_h_errno (NETDB_INTERNAL
);
465 __set_errno (EAFNOSUPPORT
);
469 host
.h_addrtype
= af
;
470 host
.h_length
= size
;
473 * if there aren't any dots, it could be a user-level alias.
474 * this is also done in res_query() since we are not the only
475 * function that looks up host names.
478 if (strchr (name
, '.') != NULL
479 && (cp
= __res_context_hostalias (ctx
, name
, abuf
, sizeof (abuf
))))
483 * disallow names consisting only of digits/dots, unless
486 if (isdigit(name
[0]))
487 for (cp
= name
;; ++cp
) {
492 * All-numeric, no dot at the end.
493 * Fake up a hostent as if we'd actually
496 if (inet_pton(af
, name
, host_addr
) <= 0) {
497 __set_h_errno (HOST_NOT_FOUND
);
500 strncpy(hostbuf
, name
, MAXDNAME
);
501 hostbuf
[MAXDNAME
] = '\0';
502 bp
= hostbuf
+ MAXDNAME
;
503 len
= sizeof hostbuf
- MAXDNAME
;
504 host
.h_name
= hostbuf
;
505 host
.h_aliases
= host_aliases
;
506 host_aliases
[0] = NULL
;
507 h_addr_ptrs
[0] = (char *)host_addr
;
508 h_addr_ptrs
[1] = NULL
;
509 host
.h_addr_list
= h_addr_ptrs
;
510 if (res_use_inet6 ())
511 map_v4v6_hostent(&host
, &bp
, &len
);
512 __set_h_errno (NETDB_SUCCESS
);
515 if (!isdigit(*cp
) && *cp
!= '.')
518 if ((isxdigit(name
[0]) && strchr(name
, ':') != NULL
) ||
520 for (cp
= name
;; ++cp
) {
525 * All-IPv6-legal, no dot at the end.
526 * Fake up a hostent as if we'd actually
529 if (inet_pton(af
, name
, host_addr
) <= 0) {
530 __set_h_errno (HOST_NOT_FOUND
);
533 strncpy(hostbuf
, name
, MAXDNAME
);
534 hostbuf
[MAXDNAME
] = '\0';
535 bp
= hostbuf
+ MAXDNAME
;
536 len
= sizeof hostbuf
- MAXDNAME
;
537 host
.h_name
= hostbuf
;
538 host
.h_aliases
= host_aliases
;
539 host_aliases
[0] = NULL
;
540 h_addr_ptrs
[0] = (char *)host_addr
;
541 h_addr_ptrs
[1] = NULL
;
542 host
.h_addr_list
= h_addr_ptrs
;
543 __set_h_errno (NETDB_SUCCESS
);
546 if (!isxdigit(*cp
) && *cp
!= ':' && *cp
!= '.')
550 buf
.buf
= origbuf
= (querybuf
*) alloca (1024);
552 if ((n
= __res_context_search
553 (ctx
, name
, C_IN
, type
, buf
.buf
->buf
, 1024,
554 &buf
.ptr
, NULL
, NULL
, NULL
, NULL
)) < 0) {
555 if (buf
.buf
!= origbuf
)
557 if (errno
== ECONNREFUSED
)
558 return (_gethtbyname2(name
, af
));
561 ret
= getanswer(buf
.buf
, n
, name
, type
);
562 if (buf
.buf
!= origbuf
)
568 res_gethostbyname2 (const char *name
, int af
)
570 struct resolv_context
*ctx
= __resolv_context_get ();
573 __set_h_errno (NETDB_INTERNAL
);
576 struct hostent
*hp
= res_gethostbyname2_context (ctx
, name
, AF_INET
);
577 __resolv_context_put (ctx
);
580 libresolv_hidden_def (res_gethostbyname2
)
581 compat_symbol (libresolv
, res_gethostbyname2
, res_gethostbyname2
, GLIBC_2_0
);
583 static struct hostent
*
584 res_gethostbyaddr_context (struct resolv_context
*ctx
,
585 const void *addr
, socklen_t len
, int af
)
587 const u_char
*uaddr
= (const u_char
*)addr
;
588 static const u_char mapped
[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
589 static const u_char tunnelled
[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
599 char qbuf
[MAXDNAME
+1], *qp
= NULL
;
601 if (af
== AF_INET6
&& len
== IN6ADDRSZ
&&
602 (!memcmp(uaddr
, mapped
, sizeof mapped
) ||
603 !memcmp(uaddr
, tunnelled
, sizeof tunnelled
))) {
605 addr
+= sizeof mapped
;
606 uaddr
+= sizeof mapped
;
618 __set_errno (EAFNOSUPPORT
);
619 __set_h_errno (NETDB_INTERNAL
);
623 __set_errno (EINVAL
);
624 __set_h_errno (NETDB_INTERNAL
);
629 (void) sprintf(qbuf
, "%u.%u.%u.%u.in-addr.arpa",
637 for (n
= IN6ADDRSZ
- 1; n
>= 0; n
--) {
638 qp
+= sprintf(qp
, "%x.%x.",
640 (uaddr
[n
] >> 4) & 0xf);
642 strcpy(qp
, "ip6.arpa");
648 buf
.buf
= orig_buf
= (querybuf
*) alloca (1024);
650 n
= __res_context_query (ctx
, qbuf
, C_IN
, T_PTR
, buf
.buf
->buf
, 1024,
651 &buf
.ptr
, NULL
, NULL
, NULL
, NULL
);
653 if (buf
.buf
!= orig_buf
)
655 if (errno
== ECONNREFUSED
)
656 return (_gethtbyaddr(addr
, len
, af
));
659 hp
= getanswer(buf
.buf
, n
, qbuf
, T_PTR
);
660 if (buf
.buf
!= orig_buf
)
663 return (NULL
); /* h_errno was set by getanswer() */
666 memmove(host_addr
, addr
, len
);
667 h_addr_ptrs
[0] = (char *)host_addr
;
668 h_addr_ptrs
[1] = NULL
;
669 if (af
== AF_INET
&& res_use_inet6 ()) {
670 map_v4v6_address((char*)host_addr
, (char*)host_addr
);
671 hp
->h_addrtype
= AF_INET6
;
672 hp
->h_length
= IN6ADDRSZ
;
674 __set_h_errno (NETDB_SUCCESS
);
679 res_gethostbyaddr (const void *addr
, socklen_t len
, int af
)
681 struct resolv_context
*ctx
= __resolv_context_get ();
684 __set_h_errno (NETDB_INTERNAL
);
687 struct hostent
*hp
= res_gethostbyaddr_context (ctx
, addr
, len
, af
);
688 __resolv_context_put (ctx
);
691 compat_symbol (libresolv
, res_gethostbyaddr
, res_gethostbyaddr
, GLIBC_2_0
);
697 hostf
= fopen(_PATH_HOSTS
, "rce" );
702 libresolv_hidden_def (_sethtent
)
703 compat_symbol (libresolv
, _sethtent
, _sethtent
, GLIBC_2_0
);
708 if (hostf
&& !stayopen
) {
709 (void) fclose(hostf
);
721 if (!hostf
&& !(hostf
= fopen(_PATH_HOSTS
, "rce" ))) {
722 __set_h_errno (NETDB_INTERNAL
);
726 if (!(p
= fgets(hostbuf
, sizeof hostbuf
, hostf
))) {
727 __set_h_errno (HOST_NOT_FOUND
);
732 if (!(cp
= strpbrk(p
, "#\n")))
735 if (!(cp
= strpbrk(p
, " \t")))
738 if (inet_pton(AF_INET6
, p
, host_addr
) > 0) {
741 } else if (inet_pton(AF_INET
, p
, host_addr
) > 0) {
742 if (res_use_inet6 ()) {
743 map_v4v6_address((char*)host_addr
, (char*)host_addr
);
753 h_addr_ptrs
[0] = (char *)host_addr
;
754 h_addr_ptrs
[1] = NULL
;
755 host
.h_addr_list
= h_addr_ptrs
;
757 host
.h_addrtype
= af
;
758 while (*cp
== ' ' || *cp
== '\t')
761 q
= host
.h_aliases
= host_aliases
;
762 if ((cp
= strpbrk(cp
, " \t")))
765 if (*cp
== ' ' || *cp
== '\t') {
769 if (q
< &host_aliases
[MAXALIASES
- 1])
771 if ((cp
= strpbrk(cp
, " \t")))
775 __set_h_errno (NETDB_SUCCESS
);
778 libresolv_hidden_def (_gethtent
)
779 compat_symbol (libresolv
, _gethtent
, _gethtent
, GLIBC_2_0
);
782 _gethtbyname (const char *name
)
786 if (res_use_inet6 ()) {
787 hp
= _gethtbyname2(name
, AF_INET6
);
791 return (_gethtbyname2(name
, AF_INET
));
793 compat_symbol (libresolv
, _gethtbyname
, _gethtbyname
, GLIBC_2_0
);
796 _gethtbyname2 (const char *name
, int af
)
802 while ((p
= _gethtent())) {
803 if (p
->h_addrtype
!= af
)
805 if (strcasecmp(p
->h_name
, name
) == 0)
807 for (cp
= p
->h_aliases
; *cp
!= 0; cp
++)
808 if (strcasecmp(*cp
, name
) == 0)
815 libresolv_hidden_def (_gethtbyname2
)
816 compat_symbol (libresolv
, _gethtbyname2
, _gethtbyname2
, GLIBC_2_0
);
819 _gethtbyaddr (const char *addr
, size_t len
, int af
)
824 while ((p
= _gethtent()))
825 if (p
->h_addrtype
== af
&& !memcmp(p
->h_addr
, addr
, len
))
830 libresolv_hidden_def (_gethtbyaddr
)
831 compat_symbol (libresolv
, _gethtbyaddr
, _gethtbyaddr
, GLIBC_2_0
);
834 map_v4v6_address (const char *src
, char *dst
)
836 u_char
*p
= (u_char
*)dst
;
840 /* Stash a temporary copy so our caller can update in place. */
841 memcpy(tmp
, src
, INADDRSZ
);
842 /* Mark this ipv6 addr as a mapped ipv4. */
843 for (i
= 0; i
< 10; i
++)
847 /* Retrieve the saved copy and we're done. */
848 memcpy((void*)p
, tmp
, INADDRSZ
);
852 map_v4v6_hostent (struct hostent
*hp
, char **bpp
, int *lenp
)
856 if (hp
->h_addrtype
!= AF_INET
|| hp
->h_length
!= INADDRSZ
)
858 hp
->h_addrtype
= AF_INET6
;
859 hp
->h_length
= IN6ADDRSZ
;
860 for (ap
= hp
->h_addr_list
; *ap
; ap
++) {
861 int i
= sizeof(align
) - ((u_long
)*bpp
% sizeof(align
));
863 if (*lenp
< (i
+ IN6ADDRSZ
)) {
864 /* Out of memory. Truncate address list here. XXX */
870 map_v4v6_address(*ap
, *bpp
);
878 addrsort (char **ap
, int num
)
882 short aval
[MAXADDRS
];
886 for (i
= 0; i
< num
; i
++, p
++) {
887 for (j
= 0 ; (unsigned)j
< _res
.nsort
; j
++)
888 if (_res
.sort_list
[j
].addr
.s_addr
==
889 (((struct in_addr
*)(*p
))->s_addr
& _res
.sort_list
[j
].mask
))
892 if (needsort
== 0 && i
> 0 && j
< aval
[i
-1])
898 while (needsort
< num
) {
899 for (j
= needsort
- 1; j
>= 0; j
--) {
900 if (aval
[j
] > aval
[j
+1]) {
918 #endif /* SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_25) */