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 <sys/types.h>
58 #include <sys/param.h>
59 #include <sys/socket.h>
60 #include <netinet/in.h>
61 #include <arpa/inet.h>
62 #include <arpa/nameser.h>
75 static char *h_addr_ptrs
[MAXADDRS
+ 1];
77 static struct hostent host
;
78 static char *host_aliases
[MAXALIASES
];
79 static char hostbuf
[8*1024];
80 static u_char host_addr
[16]; /* IPv4 or IPv6 */
81 static FILE *hostf
= NULL
;
82 static int stayopen
= 0;
84 static void map_v4v6_address (const char *src
, char *dst
) __THROW
;
85 static void map_v4v6_hostent (struct hostent
*hp
, char **bp
, int *len
) __THROW
;
87 extern void addrsort (char **, int) __THROW
;
90 #define MAXPACKET PACKETSZ
92 #define MAXPACKET 65536
95 /* As per RFC 1034 and 1035 a host name cannot exceed 255 octets in length. */
97 # undef MAXHOSTNAMELEN
99 #define MAXHOSTNAMELEN 256
103 u_char buf
[MAXPACKET
];
117 Dprintf (char *msg
, int num
)
119 if (_res
.options
& RES_DEBUG
) {
127 # define Dprintf(msg, num) /*nada*/
130 #define BOUNDED_INCR(x) \
134 __set_h_errno (NO_RECOVERY); \
139 #define BOUNDS_CHECK(ptr, count) \
141 if ((ptr) + (count) > eom) { \
142 __set_h_errno (NO_RECOVERY); \
148 static struct hostent
*
149 getanswer (const querybuf
*answer
, int anslen
, const char *qname
, int qtype
)
154 const u_char
*eom
, *erdata
;
155 char *bp
, **ap
, **hap
;
156 int type
, class, buflen
, ancount
, qdcount
;
157 int haveanswer
, had_error
;
161 int (*name_ok
) (const char *);
165 eom
= answer
->buf
+ anslen
;
175 return (NULL
); /* XXX should be abort(); */
178 * find first satisfactory answer
181 ancount
= ntohs(hp
->ancount
);
182 qdcount
= ntohs(hp
->qdcount
);
184 buflen
= sizeof hostbuf
;
186 BOUNDED_INCR(HFIXEDSZ
);
188 __set_h_errno (NO_RECOVERY
);
191 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
192 if ((n
< 0) || !(*name_ok
)(bp
)) {
193 __set_h_errno (NO_RECOVERY
);
196 BOUNDED_INCR(n
+ QFIXEDSZ
);
197 if (qtype
== T_A
|| qtype
== T_AAAA
) {
198 /* res_send() has already verified that the query name is the
199 * same as the one we sent; this just gets the expanded name
200 * (i.e., with the succeeding search-domain tacked on).
202 n
= strlen(bp
) + 1; /* for the \0 */
203 if (n
>= MAXHOSTNAMELEN
) {
204 __set_h_errno (NO_RECOVERY
);
210 /* The qname can be abbreviated, but h_name is now absolute. */
215 host
.h_aliases
= host_aliases
;
218 host
.h_addr_list
= h_addr_ptrs
;
221 while (ancount
-- > 0 && cp
< eom
&& !had_error
) {
222 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
223 if ((n
< 0) || !(*name_ok
)(bp
)) {
228 BOUNDS_CHECK(cp
, 3 * INT16SZ
+ INT32SZ
);
230 cp
+= INT16SZ
; /* type */
231 class = ns_get16(cp
);
232 cp
+= INT16SZ
+ INT32SZ
; /* class, 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
>= &host_aliases
[MAXALIASES
-1])
245 n
= dn_expand(answer
->buf
, eom
, cp
, tbuf
, sizeof tbuf
);
246 if ((n
< 0) || !(*name_ok
)(tbuf
)) {
252 __set_h_errno (NO_RECOVERY
);
257 n
= strlen(bp
) + 1; /* for the \0 */
258 if (n
>= MAXHOSTNAMELEN
) {
264 /* Get canonical name. */
265 n
= strlen(tbuf
) + 1; /* for the \0 */
266 if (n
> buflen
|| n
>= MAXHOSTNAMELEN
) {
276 if (qtype
== T_PTR
&& type
== T_CNAME
) {
277 n
= dn_expand(answer
->buf
, eom
, cp
, tbuf
, sizeof tbuf
);
278 if (n
< 0 || !res_dnok(tbuf
)) {
284 __set_h_errno (NO_RECOVERY
);
287 /* Get canonical name. */
288 n
= strlen(tbuf
) + 1; /* for the \0 */
289 if (n
> buflen
|| n
>= MAXHOSTNAMELEN
) {
300 /* Log a low priority message if we get an unexpected
301 * record, but skip it if we are using DNSSEC since it
302 * uses many different types in responses that do not
306 continue; /* XXX - had_error++ ? */
310 if (strcasecmp(tname
, bp
) != 0) {
312 continue; /* XXX - had_error++ ? */
314 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
315 if ((n
< 0) || !res_hnok(bp
)) {
321 __set_h_errno (NO_RECOVERY
);
326 else if (ap
< &host_aliases
[MAXALIASES
-1])
331 n
= strlen(bp
) + 1; /* for the \0 */
332 if (n
>= MAXHOSTNAMELEN
) {
342 if (strcasecmp(host
.h_name
, bp
) != 0) {
344 continue; /* XXX - had_error++ ? */
346 if (n
!= host
.h_length
) {
354 nn
= strlen(bp
) + 1; /* for the \0 */
359 /* XXX: when incrementing bp, we have to decrement
360 * buflen by the same amount --okir */
361 buflen
-= sizeof(align
) - ((u_long
)bp
% sizeof(align
));
363 bp
+= sizeof(align
) - ((u_long
)bp
% sizeof(align
));
365 if (bp
+ n
>= &hostbuf
[sizeof hostbuf
]) {
366 Dprintf("size (%d) too big\n", n
);
370 if (hap
>= &h_addr_ptrs
[MAXADDRS
-1]) {
372 Dprintf("Too many addresses (%d)\n",
378 memmove(*hap
++ = bp
, cp
, n
);
383 __set_h_errno (NO_RECOVERY
);
397 * Note: we sort even if host can take only one address
398 * in its return structures - should give it the "best"
399 * address in that case, not some random one
401 if (_res
.nsort
&& haveanswer
> 1 && qtype
== T_A
)
402 addrsort(h_addr_ptrs
, haveanswer
);
404 n
= strlen(qname
) + 1; /* for the \0 */
405 if (n
> buflen
|| n
>= MAXHOSTNAMELEN
)
412 if (_res
.options
& RES_USE_INET6
)
413 map_v4v6_hostent(&host
, &bp
, &buflen
);
414 __set_h_errno (NETDB_SUCCESS
);
418 __set_h_errno (NO_RECOVERY
);
422 extern struct hostent
*gethostbyname2(const char *name
, int af
);
423 libresolv_hidden_proto (gethostbyname2
)
426 gethostbyname (const char *name
)
430 if (__res_maybe_init (&_res
, 0) == -1) {
431 __set_h_errno (NETDB_INTERNAL
);
434 if (_res
.options
& RES_USE_INET6
) {
435 hp
= gethostbyname2(name
, AF_INET6
);
439 return (gethostbyname2(name
, AF_INET
));
443 gethostbyname2 (const char *name
, int af
)
453 int n
, size
, type
, len
;
456 if (__res_maybe_init (&_res
, 0) == -1) {
457 __set_h_errno (NETDB_INTERNAL
);
471 __set_h_errno (NETDB_INTERNAL
);
472 __set_errno (EAFNOSUPPORT
);
476 host
.h_addrtype
= af
;
477 host
.h_length
= size
;
480 * if there aren't any dots, it could be a user-level alias.
481 * this is also done in res_query() since we are not the only
482 * function that looks up host names.
484 if (!strchr(name
, '.') && (cp
= __hostalias(name
)))
488 * disallow names consisting only of digits/dots, unless
491 if (isdigit(name
[0]))
492 for (cp
= name
;; ++cp
) {
497 * All-numeric, no dot at the end.
498 * Fake up a hostent as if we'd actually
501 if (inet_pton(af
, name
, host_addr
) <= 0) {
502 __set_h_errno (HOST_NOT_FOUND
);
505 strncpy(hostbuf
, name
, MAXDNAME
);
506 hostbuf
[MAXDNAME
] = '\0';
507 bp
= hostbuf
+ MAXDNAME
;
508 len
= sizeof hostbuf
- MAXDNAME
;
509 host
.h_name
= hostbuf
;
510 host
.h_aliases
= host_aliases
;
511 host_aliases
[0] = NULL
;
512 h_addr_ptrs
[0] = (char *)host_addr
;
513 h_addr_ptrs
[1] = NULL
;
514 host
.h_addr_list
= h_addr_ptrs
;
515 if (_res
.options
& RES_USE_INET6
)
516 map_v4v6_hostent(&host
, &bp
, &len
);
517 __set_h_errno (NETDB_SUCCESS
);
520 if (!isdigit(*cp
) && *cp
!= '.')
523 if ((isxdigit(name
[0]) && strchr(name
, ':') != NULL
) ||
525 for (cp
= name
;; ++cp
) {
530 * All-IPv6-legal, no dot at the end.
531 * Fake up a hostent as if we'd actually
534 if (inet_pton(af
, name
, host_addr
) <= 0) {
535 __set_h_errno (HOST_NOT_FOUND
);
538 strncpy(hostbuf
, name
, MAXDNAME
);
539 hostbuf
[MAXDNAME
] = '\0';
540 bp
= hostbuf
+ MAXDNAME
;
541 len
= sizeof hostbuf
- MAXDNAME
;
542 host
.h_name
= hostbuf
;
543 host
.h_aliases
= host_aliases
;
544 host_aliases
[0] = NULL
;
545 h_addr_ptrs
[0] = (char *)host_addr
;
546 h_addr_ptrs
[1] = NULL
;
547 host
.h_addr_list
= h_addr_ptrs
;
548 __set_h_errno (NETDB_SUCCESS
);
551 if (!isxdigit(*cp
) && *cp
!= ':' && *cp
!= '.')
555 buf
.buf
= origbuf
= (querybuf
*) alloca (1024);
557 if ((n
= __libc_res_nsearch(&_res
, name
, C_IN
, type
, buf
.buf
->buf
, 1024,
558 &buf
.ptr
, NULL
, NULL
, NULL
, NULL
)) < 0) {
559 if (buf
.buf
!= origbuf
)
561 Dprintf("res_nsearch failed (%d)\n", n
);
562 if (errno
== ECONNREFUSED
)
563 return (_gethtbyname2(name
, af
));
566 ret
= getanswer(buf
.buf
, n
, name
, type
);
567 if (buf
.buf
!= origbuf
)
571 libresolv_hidden_def (gethostbyname2
)
574 gethostbyaddr (const void *addr
, socklen_t len
, int af
)
576 const u_char
*uaddr
= (const u_char
*)addr
;
577 static const u_char mapped
[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
578 static const u_char tunnelled
[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
588 char qbuf
[MAXDNAME
+1], *qp
= NULL
;
590 if (__res_maybe_init (&_res
, 0) == -1) {
591 __set_h_errno (NETDB_INTERNAL
);
594 if (af
== AF_INET6
&& len
== IN6ADDRSZ
&&
595 (!memcmp(uaddr
, mapped
, sizeof mapped
) ||
596 !memcmp(uaddr
, tunnelled
, sizeof tunnelled
))) {
598 addr
+= sizeof mapped
;
599 uaddr
+= sizeof mapped
;
611 __set_errno (EAFNOSUPPORT
);
612 __set_h_errno (NETDB_INTERNAL
);
616 __set_errno (EINVAL
);
617 __set_h_errno (NETDB_INTERNAL
);
622 (void) sprintf(qbuf
, "%u.%u.%u.%u.in-addr.arpa",
630 for (n
= IN6ADDRSZ
- 1; n
>= 0; n
--) {
631 qp
+= sprintf(qp
, "%x.%x.",
633 (uaddr
[n
] >> 4) & 0xf);
635 strcpy(qp
, "ip6.arpa");
641 buf
.buf
= orig_buf
= (querybuf
*) alloca (1024);
643 n
= __libc_res_nquery(&_res
, qbuf
, C_IN
, T_PTR
, buf
.buf
->buf
, 1024,
644 &buf
.ptr
, NULL
, NULL
, NULL
, NULL
);
646 if (buf
.buf
!= orig_buf
)
648 Dprintf("res_nquery failed (%d)\n", n
);
649 if (errno
== ECONNREFUSED
)
650 return (_gethtbyaddr(addr
, len
, af
));
653 hp
= getanswer(buf
.buf
, n
, qbuf
, T_PTR
);
654 if (buf
.buf
!= orig_buf
)
657 return (NULL
); /* h_errno was set by getanswer() */
660 memmove(host_addr
, addr
, len
);
661 h_addr_ptrs
[0] = (char *)host_addr
;
662 h_addr_ptrs
[1] = NULL
;
663 if (af
== AF_INET
&& (_res
.options
& RES_USE_INET6
)) {
664 map_v4v6_address((char*)host_addr
, (char*)host_addr
);
665 hp
->h_addrtype
= AF_INET6
;
666 hp
->h_length
= IN6ADDRSZ
;
668 __set_h_errno (NETDB_SUCCESS
);
676 hostf
= fopen(_PATH_HOSTS
, "rce" );
681 libresolv_hidden_def (_sethtent
)
686 if (hostf
&& !stayopen
) {
687 (void) fclose(hostf
);
699 if (!hostf
&& !(hostf
= fopen(_PATH_HOSTS
, "rce" ))) {
700 __set_h_errno (NETDB_INTERNAL
);
704 if (!(p
= fgets(hostbuf
, sizeof hostbuf
, hostf
))) {
705 __set_h_errno (HOST_NOT_FOUND
);
710 if (!(cp
= strpbrk(p
, "#\n")))
713 if (!(cp
= strpbrk(p
, " \t")))
716 if (inet_pton(AF_INET6
, p
, host_addr
) > 0) {
719 } else if (inet_pton(AF_INET
, p
, host_addr
) > 0) {
720 if (_res
.options
& RES_USE_INET6
) {
721 map_v4v6_address((char*)host_addr
, (char*)host_addr
);
731 h_addr_ptrs
[0] = (char *)host_addr
;
732 h_addr_ptrs
[1] = NULL
;
733 host
.h_addr_list
= h_addr_ptrs
;
735 host
.h_addrtype
= af
;
736 while (*cp
== ' ' || *cp
== '\t')
739 q
= host
.h_aliases
= host_aliases
;
740 if ((cp
= strpbrk(cp
, " \t")))
743 if (*cp
== ' ' || *cp
== '\t') {
747 if (q
< &host_aliases
[MAXALIASES
- 1])
749 if ((cp
= strpbrk(cp
, " \t")))
753 __set_h_errno (NETDB_SUCCESS
);
756 libresolv_hidden_def (_gethtent
)
759 _gethtbyname (const char *name
)
763 if (_res
.options
& RES_USE_INET6
) {
764 hp
= _gethtbyname2(name
, AF_INET6
);
768 return (_gethtbyname2(name
, AF_INET
));
772 _gethtbyname2 (const char *name
, int af
)
778 while ((p
= _gethtent())) {
779 if (p
->h_addrtype
!= af
)
781 if (strcasecmp(p
->h_name
, name
) == 0)
783 for (cp
= p
->h_aliases
; *cp
!= 0; cp
++)
784 if (strcasecmp(*cp
, name
) == 0)
791 libresolv_hidden_def (_gethtbyname2
)
794 _gethtbyaddr (const char *addr
, size_t len
, int af
)
799 while ((p
= _gethtent()))
800 if (p
->h_addrtype
== af
&& !memcmp(p
->h_addr
, addr
, len
))
805 libresolv_hidden_def (_gethtbyaddr
)
808 map_v4v6_address (const char *src
, char *dst
)
810 u_char
*p
= (u_char
*)dst
;
814 /* Stash a temporary copy so our caller can update in place. */
815 memcpy(tmp
, src
, INADDRSZ
);
816 /* Mark this ipv6 addr as a mapped ipv4. */
817 for (i
= 0; i
< 10; i
++)
821 /* Retrieve the saved copy and we're done. */
822 memcpy((void*)p
, tmp
, INADDRSZ
);
826 map_v4v6_hostent (struct hostent
*hp
, char **bpp
, int *lenp
)
830 if (hp
->h_addrtype
!= AF_INET
|| hp
->h_length
!= INADDRSZ
)
832 hp
->h_addrtype
= AF_INET6
;
833 hp
->h_length
= IN6ADDRSZ
;
834 for (ap
= hp
->h_addr_list
; *ap
; ap
++) {
835 int i
= sizeof(align
) - ((u_long
)*bpp
% sizeof(align
));
837 if (*lenp
< (i
+ IN6ADDRSZ
)) {
838 /* Out of memory. Truncate address list here. XXX */
844 map_v4v6_address(*ap
, *bpp
);
852 addrsort (char **ap
, int num
)
856 short aval
[MAXADDRS
];
860 for (i
= 0; i
< num
; i
++, p
++) {
861 for (j
= 0 ; (unsigned)j
< _res
.nsort
; j
++)
862 if (_res
.sort_list
[j
].addr
.s_addr
==
863 (((struct in_addr
*)(*p
))->s_addr
& _res
.sort_list
[j
].mask
))
866 if (needsort
== 0 && i
> 0 && j
< aval
[i
-1])
872 while (needsort
< num
) {
873 for (j
= needsort
- 1; j
>= 0; j
--) {
874 if (aval
[j
] > aval
[j
+1]) {