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 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
37 * Permission to use, copy, modify, and distribute this software for any
38 * purpose with or without fee is hereby granted, provided that the above
39 * copyright notice and this permission notice appear in all copies, and that
40 * the name of Digital Equipment Corporation not be used in advertising or
41 * publicity pertaining to distribution of the document or software without
42 * specific, written prior permission.
44 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
45 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
46 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
47 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
48 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
49 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
50 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
56 #if defined(LIBC_SCCS) && !defined(lint)
57 static char sccsid
[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
58 static char rcsid
[] = "$Id$";
59 #endif /* LIBC_SCCS and not lint */
61 #include <sys/types.h>
62 #include <sys/param.h>
63 #include <sys/socket.h>
64 #include <netinet/in.h>
65 #include <arpa/inet.h>
66 #include <arpa/nameser.h>
79 #define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */
81 #if defined(BSD) && (BSD >= 199103) && defined(AF_INET6)
85 # include "../conf/portability.h"
88 #if defined(USE_OPTIONS_H)
89 # include <../conf/options.h>
93 # define SPRINTF(x) strlen(sprintf/**/x)
95 # define SPRINTF(x) ((size_t)sprintf x)
101 static const char AskedForGot
[] =
102 "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
104 static char *h_addr_ptrs
[MAXADDRS
+ 1];
106 static struct hostent host
;
107 static char *host_aliases
[MAXALIASES
];
108 static char hostbuf
[8*1024];
109 static u_char host_addr
[16]; /* IPv4 or IPv6 */
110 static FILE *hostf
= NULL
;
111 static int stayopen
= 0;
113 static void map_v4v6_address
__P((const char *src
, char *dst
));
114 static void map_v4v6_hostent
__P((struct hostent
*hp
, char **bp
, int *len
));
117 extern void addrsort
__P((char **, int));
121 #define MAXPACKET PACKETSZ
123 #define MAXPACKET 1024
126 /* As per RFC 1034 and 1035 a host name cannot exceed 255 octets in length. */
127 #ifdef MAXHOSTNAMELEN
128 # undef MAXHOSTNAMELEN
130 #define MAXHOSTNAMELEN 256
134 u_char buf
[MAXPACKET
];
152 if (_res
.options
& RES_DEBUG
) {
160 # define dprintf(msg, num) /*nada*/
163 #define BOUNDED_INCR(x) \
167 __set_h_errno (NO_RECOVERY); \
172 #define BOUNDS_CHECK(ptr, count) \
174 if ((ptr) + (count) > eom) { \
175 __set_h_errno (NO_RECOVERY); \
181 static struct hostent
*
182 getanswer(answer
, anslen
, qname
, qtype
)
183 const querybuf
*answer
;
188 register const HEADER
*hp
;
189 register const u_char
*cp
;
191 const u_char
*eom
, *erdata
;
192 char *bp
, **ap
, **hap
;
193 int type
, class, buflen
, ancount
, qdcount
;
194 int haveanswer
, had_error
;
198 int (*name_ok
) __P((const char *));
202 eom
= answer
->buf
+ anslen
;
212 return (NULL
); /* XXX should be abort(); */
215 * find first satisfactory answer
218 ancount
= ntohs(hp
->ancount
);
219 qdcount
= ntohs(hp
->qdcount
);
221 buflen
= sizeof hostbuf
;
223 BOUNDED_INCR(HFIXEDSZ
);
225 __set_h_errno (NO_RECOVERY
);
228 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
229 if ((n
< 0) || !(*name_ok
)(bp
)) {
230 __set_h_errno (NO_RECOVERY
);
233 BOUNDED_INCR(n
+ QFIXEDSZ
);
234 if (qtype
== T_A
|| qtype
== T_AAAA
) {
235 /* res_send() has already verified that the query name is the
236 * same as the one we sent; this just gets the expanded name
237 * (i.e., with the succeeding search-domain tacked on).
239 n
= strlen(bp
) + 1; /* for the \0 */
240 if (n
>= MAXHOSTNAMELEN
) {
241 __set_h_errno (NO_RECOVERY
);
247 /* The qname can be abbreviated, but h_name is now absolute. */
252 host
.h_aliases
= host_aliases
;
255 host
.h_addr_list
= h_addr_ptrs
;
258 while (ancount
-- > 0 && cp
< eom
&& !had_error
) {
259 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
260 if ((n
< 0) || !(*name_ok
)(bp
)) {
265 BOUNDS_CHECK(cp
, 3 * INT16SZ
+ INT32SZ
);
266 type
= _getshort(cp
);
267 cp
+= INT16SZ
; /* type */
268 class = _getshort(cp
);
269 cp
+= INT16SZ
+ INT32SZ
; /* class, TTL */
271 cp
+= INT16SZ
; /* len */
275 /* XXX - debug? syslog? */
277 continue; /* XXX - had_error++ ? */
279 if ((qtype
== T_A
|| qtype
== T_AAAA
) && type
== T_CNAME
) {
280 if (ap
>= &host_aliases
[MAXALIASES
-1])
282 n
= dn_expand(answer
->buf
, eom
, cp
, tbuf
, sizeof tbuf
);
283 if ((n
< 0) || !(*name_ok
)(tbuf
)) {
289 __set_h_errno (NO_RECOVERY
);
294 n
= strlen(bp
) + 1; /* for the \0 */
295 if (n
>= MAXHOSTNAMELEN
) {
301 /* Get canonical name. */
302 n
= strlen(tbuf
) + 1; /* for the \0 */
303 if (n
> buflen
|| n
>= MAXHOSTNAMELEN
) {
313 if (qtype
== T_PTR
&& type
== T_CNAME
) {
314 n
= dn_expand(answer
->buf
, eom
, cp
, tbuf
, sizeof tbuf
);
315 if (n
< 0 || !res_dnok(tbuf
)) {
321 __set_h_errno (NO_RECOVERY
);
324 /* Get canonical name. */
325 n
= strlen(tbuf
) + 1; /* for the \0 */
326 if (n
> buflen
|| n
>= MAXHOSTNAMELEN
) {
336 if ((type
== T_SIG
) || (type
== T_KEY
) || (type
== T_NXT
)) {
337 /* We don't support DNSSEC yet. For now, ignore
338 * the record and send a low priority message
341 syslog(LOG_DEBUG
|LOG_AUTH
,
342 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
343 qname
, p_class(C_IN
), p_type(qtype
),
349 syslog(LOG_NOTICE
|LOG_AUTH
,
350 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
351 qname
, p_class(C_IN
), p_type(qtype
),
354 continue; /* XXX - had_error++ ? */
358 if (strcasecmp(tname
, bp
) != 0) {
359 syslog(LOG_NOTICE
|LOG_AUTH
,
360 AskedForGot
, qname
, bp
);
362 continue; /* XXX - had_error++ ? */
364 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
365 if ((n
< 0) || !res_hnok(bp
)) {
369 #if MULTI_PTRS_ARE_ALIASES
372 __set_h_errno (NO_RECOVERY
);
377 else if (ap
< &host_aliases
[MAXALIASES
-1])
382 n
= strlen(bp
) + 1; /* for the \0 */
383 if (n
>= MAXHOSTNAMELEN
) {
393 if (_res
.options
& RES_USE_INET6
) {
394 n
= strlen(bp
) + 1; /* for the \0 */
395 if (n
>= MAXHOSTNAMELEN
) {
401 map_v4v6_hostent(&host
, &bp
, &buflen
);
403 __set_h_errno (NETDB_SUCCESS
);
408 if (strcasecmp(host
.h_name
, bp
) != 0) {
409 syslog(LOG_NOTICE
|LOG_AUTH
,
410 AskedForGot
, host
.h_name
, bp
);
412 continue; /* XXX - had_error++ ? */
414 if (n
!= host
.h_length
) {
422 nn
= strlen(bp
) + 1; /* for the \0 */
427 bp
+= sizeof(align
) - ((u_long
)bp
% sizeof(align
));
429 if (bp
+ n
>= &hostbuf
[sizeof hostbuf
]) {
430 dprintf("size (%d) too big\n", n
);
434 if (hap
>= &h_addr_ptrs
[MAXADDRS
-1]) {
436 dprintf("Too many addresses (%d)\n",
442 bcopy(cp
, *hap
++ = bp
, n
);
447 __set_h_errno (NO_RECOVERY
);
460 # if defined(RESOLVSORT)
462 * Note: we sort even if host can take only one address
463 * in its return structures - should give it the "best"
464 * address in that case, not some random one
466 if (_res
.nsort
&& haveanswer
> 1 && qtype
== T_A
)
467 addrsort(h_addr_ptrs
, haveanswer
);
468 # endif /*RESOLVSORT*/
470 n
= strlen(qname
) + 1; /* for the \0 */
471 if (n
> buflen
|| n
>= MAXHOSTNAMELEN
)
478 if (_res
.options
& RES_USE_INET6
)
479 map_v4v6_hostent(&host
, &bp
, &buflen
);
480 __set_h_errno (NETDB_SUCCESS
);
484 __set_h_errno (NO_RECOVERY
);
494 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -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
)
512 register const char *cp
;
514 int n
, size
, type
, len
;
515 extern struct hostent
*_gethtbyname2();
517 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
518 __set_h_errno (NETDB_INTERNAL
);
532 __set_h_errno (NETDB_INTERNAL
);
533 __set_errno (EAFNOSUPPORT
);
537 host
.h_addrtype
= af
;
538 host
.h_length
= size
;
541 * if there aren't any dots, it could be a user-level alias.
542 * this is also done in res_query() since we are not the only
543 * function that looks up host names.
545 if (!strchr(name
, '.') && (cp
= __hostalias(name
)))
549 * disallow names consisting only of digits/dots, unless
552 if (isdigit(name
[0]))
553 for (cp
= name
;; ++cp
) {
558 * All-numeric, no dot at the end.
559 * Fake up a hostent as if we'd actually
562 if (inet_pton(af
, name
, host_addr
) <= 0) {
563 __set_h_errno (HOST_NOT_FOUND
);
566 strncpy(hostbuf
, name
, MAXDNAME
);
567 hostbuf
[MAXDNAME
] = '\0';
568 bp
= hostbuf
+ MAXDNAME
;
569 len
= sizeof hostbuf
- MAXDNAME
;
570 host
.h_name
= hostbuf
;
571 host
.h_aliases
= host_aliases
;
572 host_aliases
[0] = NULL
;
573 h_addr_ptrs
[0] = (char *)host_addr
;
574 h_addr_ptrs
[1] = NULL
;
575 host
.h_addr_list
= h_addr_ptrs
;
576 if (_res
.options
& RES_USE_INET6
)
577 map_v4v6_hostent(&host
, &bp
, &len
);
578 __set_h_errno (NETDB_SUCCESS
);
581 if (!isdigit(*cp
) && *cp
!= '.')
584 if ((isxdigit(name
[0]) && strchr(name
, ':') != NULL
) ||
586 for (cp
= name
;; ++cp
) {
591 * All-IPv6-legal, no dot at the end.
592 * Fake up a hostent as if we'd actually
595 if (inet_pton(af
, name
, host_addr
) <= 0) {
596 __set_h_errno (HOST_NOT_FOUND
);
599 strncpy(hostbuf
, name
, MAXDNAME
);
600 hostbuf
[MAXDNAME
] = '\0';
601 bp
= hostbuf
+ MAXDNAME
;
602 len
= sizeof hostbuf
- MAXDNAME
;
603 host
.h_name
= hostbuf
;
604 host
.h_aliases
= host_aliases
;
605 host_aliases
[0] = NULL
;
606 h_addr_ptrs
[0] = (char *)host_addr
;
607 h_addr_ptrs
[1] = NULL
;
608 host
.h_addr_list
= h_addr_ptrs
;
609 __set_h_errno (NETDB_SUCCESS
);
612 if (!isxdigit(*cp
) && *cp
!= ':' && *cp
!= '.')
616 if ((n
= res_search(name
, C_IN
, type
, buf
.buf
, sizeof(buf
.buf
))) < 0) {
617 dprintf("res_search failed (%d)\n", n
);
618 if (errno
== ECONNREFUSED
)
619 return (_gethtbyname2(name
, af
));
622 return (getanswer(&buf
, n
, name
, type
));
626 gethostbyaddr(addr
, len
, af
)
627 const char *addr
; /* XXX should have been def'd as u_char! */
630 const u_char
*uaddr
= (const u_char
*)addr
;
631 static const u_char mapped
[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
632 static const u_char tunnelled
[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
635 register struct hostent
*hp
;
636 char qbuf
[MAXDNAME
+1], *qp
;
638 register struct hostent
*rhp
;
641 char hname2
[MAXDNAME
+1];
642 #endif /*SUNSECURITY*/
643 extern struct hostent
*_gethtbyaddr();
645 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
646 __set_h_errno (NETDB_INTERNAL
);
649 if (af
== AF_INET6
&& len
== IN6ADDRSZ
&&
650 (!bcmp(uaddr
, mapped
, sizeof mapped
) ||
651 !bcmp(uaddr
, tunnelled
, sizeof tunnelled
))) {
653 addr
+= sizeof mapped
;
654 uaddr
+= sizeof mapped
;
666 __set_errno (EAFNOSUPPORT
);
667 __set_h_errno (NETDB_INTERNAL
);
671 __set_errno (EINVAL
);
672 __set_h_errno (NETDB_INTERNAL
);
677 (void) sprintf(qbuf
, "%u.%u.%u.%u.in-addr.arpa",
685 for (n
= IN6ADDRSZ
- 1; n
>= 0; n
--) {
686 qp
+= SPRINTF((qp
, "%x.%x.",
688 (uaddr
[n
] >> 4) & 0xf));
690 strcpy(qp
, "ip6.int");
695 n
= res_query(qbuf
, C_IN
, T_PTR
, (u_char
*)buf
.buf
, sizeof buf
.buf
);
697 dprintf("res_query failed (%d)\n", n
);
698 if (errno
== ECONNREFUSED
)
699 return (_gethtbyaddr(addr
, len
, af
));
702 if (!(hp
= getanswer(&buf
, n
, qbuf
, T_PTR
)))
703 return (NULL
); /* h_errno was set by getanswer() */
707 * turn off search as the name should be absolute,
708 * 'localhost' should be matched by defnames
710 strncpy(hname2
, hp
->h_name
, MAXDNAME
);
711 hname2
[MAXDNAME
] = '\0';
712 old_options
= _res
.options
;
713 _res
.options
&= ~RES_DNSRCH
;
714 _res
.options
|= RES_DEFNAMES
;
715 if (!(rhp
= gethostbyname(hname2
))) {
716 syslog(LOG_NOTICE
|LOG_AUTH
,
717 "gethostbyaddr: No A record for %s (verifying [%s])",
718 hname2
, inet_ntoa(*((struct in_addr
*)addr
)));
719 _res
.options
= old_options
;
720 __set_h_errno (HOST_NOT_FOUND
);
723 _res
.options
= old_options
;
724 for (haddr
= rhp
->h_addr_list
; *haddr
; haddr
++)
725 if (!memcmp(*haddr
, addr
, INADDRSZ
))
728 syslog(LOG_NOTICE
|LOG_AUTH
,
729 "gethostbyaddr: A record of %s != PTR record [%s]",
730 hname2
, inet_ntoa(*((struct in_addr
*)addr
)));
731 __set_h_errno (HOST_NOT_FOUND
);
735 #endif /*SUNSECURITY*/
738 bcopy(addr
, host_addr
, len
);
739 h_addr_ptrs
[0] = (char *)host_addr
;
740 h_addr_ptrs
[1] = NULL
;
741 if (af
== AF_INET
&& (_res
.options
& RES_USE_INET6
)) {
742 map_v4v6_address((char*)host_addr
, (char*)host_addr
);
743 hp
->h_addrtype
= AF_INET6
;
744 hp
->h_length
= IN6ADDRSZ
;
746 __set_h_errno (NETDB_SUCCESS
);
755 hostf
= fopen(_PATH_HOSTS
, "r" );
764 if (hostf
&& !stayopen
) {
765 (void) fclose(hostf
);
774 register char *cp
, **q
;
777 if (!hostf
&& !(hostf
= fopen(_PATH_HOSTS
, "r" ))) {
778 __set_h_errno (NETDB_INTERNAL
);
782 if (!(p
= fgets(hostbuf
, sizeof hostbuf
, hostf
))) {
783 __set_h_errno (HOST_NOT_FOUND
);
788 if (!(cp
= strpbrk(p
, "#\n")))
791 if (!(cp
= strpbrk(p
, " \t")))
794 if (inet_pton(AF_INET6
, p
, host_addr
) > 0) {
797 } else if (inet_pton(AF_INET
, p
, host_addr
) > 0) {
798 if (_res
.options
& RES_USE_INET6
) {
799 map_v4v6_address((char*)host_addr
, (char*)host_addr
);
809 h_addr_ptrs
[0] = (char *)host_addr
;
810 h_addr_ptrs
[1] = NULL
;
811 host
.h_addr_list
= h_addr_ptrs
;
813 host
.h_addrtype
= af
;
814 while (*cp
== ' ' || *cp
== '\t')
817 q
= host
.h_aliases
= host_aliases
;
818 if ((cp
= strpbrk(cp
, " \t")))
821 if (*cp
== ' ' || *cp
== '\t') {
825 if (q
< &host_aliases
[MAXALIASES
- 1])
827 if ((cp
= strpbrk(cp
, " \t")))
831 __set_h_errno (NETDB_SUCCESS
);
839 extern struct hostent
*_gethtbyname2();
842 if (_res
.options
& RES_USE_INET6
) {
843 hp
= _gethtbyname2(name
, AF_INET6
);
847 return (_gethtbyname2(name
, AF_INET
));
851 _gethtbyname2(name
, af
)
855 register struct hostent
*p
;
859 while ((p
= _gethtent())) {
860 if (p
->h_addrtype
!= af
)
862 if (strcasecmp(p
->h_name
, name
) == 0)
864 for (cp
= p
->h_aliases
; *cp
!= 0; cp
++)
865 if (strcasecmp(*cp
, name
) == 0)
874 _gethtbyaddr(addr
, len
, af
)
878 register struct hostent
*p
;
881 while ((p
= _gethtent()))
882 if (p
->h_addrtype
== af
&& !bcmp(p
->h_addr
, addr
, len
))
889 map_v4v6_address(src
, dst
)
893 u_char
*p
= (u_char
*)dst
;
897 /* Stash a temporary copy so our caller can update in place. */
898 bcopy(src
, tmp
, INADDRSZ
);
899 /* Mark this ipv6 addr as a mapped ipv4. */
900 for (i
= 0; i
< 10; i
++)
904 /* Retrieve the saved copy and we're done. */
905 bcopy(tmp
, (void*)p
, INADDRSZ
);
909 map_v4v6_hostent(hp
, bpp
, lenp
)
916 if (hp
->h_addrtype
!= AF_INET
|| hp
->h_length
!= INADDRSZ
)
918 hp
->h_addrtype
= AF_INET6
;
919 hp
->h_length
= IN6ADDRSZ
;
920 for (ap
= hp
->h_addr_list
; *ap
; ap
++) {
921 int i
= sizeof(align
) - ((u_long
)*bpp
% sizeof(align
));
923 if (*lenp
< (i
+ IN6ADDRSZ
)) {
924 /* Out of memory. Truncate address list here. XXX */
930 map_v4v6_address(*ap
, *bpp
);
945 short aval
[MAXADDRS
];
949 for (i
= 0; i
< num
; i
++, p
++) {
950 for (j
= 0 ; (unsigned)j
< _res
.nsort
; j
++)
951 if (_res
.sort_list
[j
].addr
.s_addr
==
952 (((struct in_addr
*)(*p
))->s_addr
& _res
.sort_list
[j
].mask
))
955 if (needsort
== 0 && i
> 0 && j
< aval
[i
-1])
961 while (needsort
< num
) {
962 for (j
= needsort
- 1; j
>= 0; j
--) {
963 if (aval
[j
] > aval
[j
+1]) {
982 #if defined(BSD43_BSD43_NFS) || defined(sun)
983 /* some libc's out there are bound internally to these names (UMIPS) */
985 ht_sethostent(stayopen
)
998 ht_gethostbyname(name
)
1001 return (_gethtbyname(name
));
1005 ht_gethostbyaddr(addr
, len
, af
)
1009 return (_gethtbyaddr(addr
, len
, af
));
1015 return (_gethtent());
1025 dn_skipname(comp_dn
, eom
)
1026 const u_char
*comp_dn
, *eom
;
1028 return (__dn_skipname(comp_dn
, eom
));
1030 #endif /*old-style libc with yp junk in it*/