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 #if defined(LIBC_SCCS) && !defined(lint)
53 static char sccsid
[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
54 static char rcsid
[] = "$Id$";
55 #endif /* LIBC_SCCS and not lint */
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 #define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */
77 #if defined(BSD) && (BSD >= 199103) && defined(AF_INET6)
81 # include "../conf/portability.h"
84 #if defined(USE_OPTIONS_H)
85 # include <../conf/options.h>
89 # define SPRINTF(x) strlen(sprintf/**/x)
91 # define SPRINTF(x) ((size_t)sprintf x)
97 static const char AskedForGot
[] =
98 "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
100 static char *h_addr_ptrs
[MAXADDRS
+ 1];
102 static struct hostent host
;
103 static char *host_aliases
[MAXALIASES
];
104 static char hostbuf
[8*1024];
105 static u_char host_addr
[16]; /* IPv4 or IPv6 */
106 static FILE *hostf
= NULL
;
107 static int stayopen
= 0;
109 static void map_v4v6_address
__P((const char *src
, char *dst
));
110 static void map_v4v6_hostent
__P((struct hostent
*hp
, char **bp
, int *len
));
113 extern void addrsort
__P((char **, int));
117 #define MAXPACKET PACKETSZ
119 #define MAXPACKET 1024
122 /* As per RFC 1034 and 1035 a host name cannot exceed 255 octets in length. */
123 #ifdef MAXHOSTNAMELEN
124 # undef MAXHOSTNAMELEN
126 #define MAXHOSTNAMELEN 256
130 u_char buf
[MAXPACKET
];
148 if (_res
.options
& RES_DEBUG
) {
156 # define dprintf(msg, num) /*nada*/
159 #define BOUNDED_INCR(x) \
163 __set_h_errno (NO_RECOVERY); \
168 #define BOUNDS_CHECK(ptr, count) \
170 if ((ptr) + (count) > eom) { \
171 __set_h_errno (NO_RECOVERY); \
177 static struct hostent
*
178 getanswer(answer
, anslen
, qname
, qtype
)
179 const querybuf
*answer
;
184 register const HEADER
*hp
;
185 register const u_char
*cp
;
187 const u_char
*eom
, *erdata
;
188 char *bp
, **ap
, **hap
;
189 int type
, class, buflen
, ancount
, qdcount
;
190 int haveanswer
, had_error
;
194 int (*name_ok
) __P((const char *));
198 eom
= answer
->buf
+ anslen
;
208 return (NULL
); /* XXX should be abort(); */
211 * find first satisfactory answer
214 ancount
= ntohs(hp
->ancount
);
215 qdcount
= ntohs(hp
->qdcount
);
217 buflen
= sizeof hostbuf
;
219 BOUNDED_INCR(HFIXEDSZ
);
221 __set_h_errno (NO_RECOVERY
);
224 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
225 if ((n
< 0) || !(*name_ok
)(bp
)) {
226 __set_h_errno (NO_RECOVERY
);
229 BOUNDED_INCR(n
+ QFIXEDSZ
);
230 if (qtype
== T_A
|| qtype
== T_AAAA
) {
231 /* res_send() has already verified that the query name is the
232 * same as the one we sent; this just gets the expanded name
233 * (i.e., with the succeeding search-domain tacked on).
235 n
= strlen(bp
) + 1; /* for the \0 */
236 if (n
>= MAXHOSTNAMELEN
) {
237 __set_h_errno (NO_RECOVERY
);
243 /* The qname can be abbreviated, but h_name is now absolute. */
248 host
.h_aliases
= host_aliases
;
251 host
.h_addr_list
= h_addr_ptrs
;
254 while (ancount
-- > 0 && cp
< eom
&& !had_error
) {
255 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
256 if ((n
< 0) || !(*name_ok
)(bp
)) {
261 BOUNDS_CHECK(cp
, 3 * INT16SZ
+ INT32SZ
);
262 type
= _getshort(cp
);
263 cp
+= INT16SZ
; /* type */
264 class = _getshort(cp
);
265 cp
+= INT16SZ
+ INT32SZ
; /* class, TTL */
267 cp
+= INT16SZ
; /* len */
271 /* XXX - debug? syslog? */
273 continue; /* XXX - had_error++ ? */
275 if ((qtype
== T_A
|| qtype
== T_AAAA
) && type
== T_CNAME
) {
276 if (ap
>= &host_aliases
[MAXALIASES
-1])
278 n
= dn_expand(answer
->buf
, eom
, cp
, tbuf
, sizeof tbuf
);
279 if ((n
< 0) || !(*name_ok
)(tbuf
)) {
285 __set_h_errno (NO_RECOVERY
);
290 n
= strlen(bp
) + 1; /* for the \0 */
291 if (n
>= MAXHOSTNAMELEN
) {
297 /* Get canonical name. */
298 n
= strlen(tbuf
) + 1; /* for the \0 */
299 if (n
> buflen
|| n
>= MAXHOSTNAMELEN
) {
309 if (qtype
== T_PTR
&& type
== T_CNAME
) {
310 n
= dn_expand(answer
->buf
, eom
, cp
, tbuf
, sizeof tbuf
);
311 if (n
< 0 || !res_dnok(tbuf
)) {
317 __set_h_errno (NO_RECOVERY
);
320 /* Get canonical name. */
321 n
= strlen(tbuf
) + 1; /* for the \0 */
322 if (n
> buflen
|| n
>= MAXHOSTNAMELEN
) {
332 if ((type
== T_SIG
) || (type
== T_KEY
) || (type
== T_NXT
)) {
333 /* We don't support DNSSEC yet. For now, ignore
334 * the record and send a low priority message
337 syslog(LOG_DEBUG
|LOG_AUTH
,
338 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
339 qname
, p_class(C_IN
), p_type(qtype
),
345 syslog(LOG_NOTICE
|LOG_AUTH
,
346 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
347 qname
, p_class(C_IN
), p_type(qtype
),
350 continue; /* XXX - had_error++ ? */
354 if (strcasecmp(tname
, bp
) != 0) {
355 syslog(LOG_NOTICE
|LOG_AUTH
,
356 AskedForGot
, qname
, bp
);
358 continue; /* XXX - had_error++ ? */
360 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
361 if ((n
< 0) || !res_hnok(bp
)) {
365 #if MULTI_PTRS_ARE_ALIASES
368 __set_h_errno (NO_RECOVERY
);
373 else if (ap
< &host_aliases
[MAXALIASES
-1])
378 n
= strlen(bp
) + 1; /* for the \0 */
379 if (n
>= MAXHOSTNAMELEN
) {
389 if (_res
.options
& RES_USE_INET6
) {
390 n
= strlen(bp
) + 1; /* for the \0 */
391 if (n
>= MAXHOSTNAMELEN
) {
397 map_v4v6_hostent(&host
, &bp
, &buflen
);
399 __set_h_errno (NETDB_SUCCESS
);
404 if (strcasecmp(host
.h_name
, bp
) != 0) {
405 syslog(LOG_NOTICE
|LOG_AUTH
,
406 AskedForGot
, host
.h_name
, bp
);
408 continue; /* XXX - had_error++ ? */
410 if (n
!= host
.h_length
) {
418 nn
= strlen(bp
) + 1; /* for the \0 */
423 bp
+= sizeof(align
) - ((u_long
)bp
% sizeof(align
));
425 if (bp
+ n
>= &hostbuf
[sizeof hostbuf
]) {
426 dprintf("size (%d) too big\n", n
);
430 if (hap
>= &h_addr_ptrs
[MAXADDRS
-1]) {
432 dprintf("Too many addresses (%d)\n",
438 bcopy(cp
, *hap
++ = bp
, n
);
443 __set_h_errno (NO_RECOVERY
);
456 # if defined(RESOLVSORT)
458 * Note: we sort even if host can take only one address
459 * in its return structures - should give it the "best"
460 * address in that case, not some random one
462 if (_res
.nsort
&& haveanswer
> 1 && qtype
== T_A
)
463 addrsort(h_addr_ptrs
, haveanswer
);
464 # endif /*RESOLVSORT*/
466 n
= strlen(qname
) + 1; /* for the \0 */
467 if (n
> buflen
|| n
>= MAXHOSTNAMELEN
)
474 if (_res
.options
& RES_USE_INET6
)
475 map_v4v6_hostent(&host
, &bp
, &buflen
);
476 __set_h_errno (NETDB_SUCCESS
);
480 __set_h_errno (NO_RECOVERY
);
490 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
491 __set_h_errno (NETDB_INTERNAL
);
494 if (_res
.options
& RES_USE_INET6
) {
495 hp
= gethostbyname2(name
, AF_INET6
);
499 return (gethostbyname2(name
, AF_INET
));
503 gethostbyname2(name
, af
)
508 register const char *cp
;
510 int n
, size
, type
, len
;
511 extern struct hostent
*_gethtbyname2();
513 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
514 __set_h_errno (NETDB_INTERNAL
);
528 __set_h_errno (NETDB_INTERNAL
);
529 __set_errno (EAFNOSUPPORT
);
533 host
.h_addrtype
= af
;
534 host
.h_length
= size
;
537 * if there aren't any dots, it could be a user-level alias.
538 * this is also done in res_query() since we are not the only
539 * function that looks up host names.
541 if (!strchr(name
, '.') && (cp
= __hostalias(name
)))
545 * disallow names consisting only of digits/dots, unless
548 if (isdigit(name
[0]))
549 for (cp
= name
;; ++cp
) {
554 * All-numeric, no dot at the end.
555 * Fake up a hostent as if we'd actually
558 if (inet_pton(af
, name
, host_addr
) <= 0) {
559 __set_h_errno (HOST_NOT_FOUND
);
562 strncpy(hostbuf
, name
, MAXDNAME
);
563 hostbuf
[MAXDNAME
] = '\0';
564 bp
= hostbuf
+ MAXDNAME
;
565 len
= sizeof hostbuf
- MAXDNAME
;
566 host
.h_name
= hostbuf
;
567 host
.h_aliases
= host_aliases
;
568 host_aliases
[0] = NULL
;
569 h_addr_ptrs
[0] = (char *)host_addr
;
570 h_addr_ptrs
[1] = NULL
;
571 host
.h_addr_list
= h_addr_ptrs
;
572 if (_res
.options
& RES_USE_INET6
)
573 map_v4v6_hostent(&host
, &bp
, &len
);
574 __set_h_errno (NETDB_SUCCESS
);
577 if (!isdigit(*cp
) && *cp
!= '.')
580 if ((isxdigit(name
[0]) && strchr(name
, ':') != NULL
) ||
582 for (cp
= name
;; ++cp
) {
587 * All-IPv6-legal, no dot at the end.
588 * Fake up a hostent as if we'd actually
591 if (inet_pton(af
, name
, host_addr
) <= 0) {
592 __set_h_errno (HOST_NOT_FOUND
);
595 strncpy(hostbuf
, name
, MAXDNAME
);
596 hostbuf
[MAXDNAME
] = '\0';
597 bp
= hostbuf
+ MAXDNAME
;
598 len
= sizeof hostbuf
- MAXDNAME
;
599 host
.h_name
= hostbuf
;
600 host
.h_aliases
= host_aliases
;
601 host_aliases
[0] = NULL
;
602 h_addr_ptrs
[0] = (char *)host_addr
;
603 h_addr_ptrs
[1] = NULL
;
604 host
.h_addr_list
= h_addr_ptrs
;
605 __set_h_errno (NETDB_SUCCESS
);
608 if (!isxdigit(*cp
) && *cp
!= ':' && *cp
!= '.')
612 if ((n
= res_search(name
, C_IN
, type
, buf
.buf
, sizeof(buf
.buf
))) < 0) {
613 dprintf("res_search failed (%d)\n", n
);
614 if (errno
== ECONNREFUSED
)
615 return (_gethtbyname2(name
, af
));
618 return (getanswer(&buf
, n
, name
, type
));
622 gethostbyaddr(addr
, len
, af
)
623 const char *addr
; /* XXX should have been def'd as u_char! */
627 const u_char
*uaddr
= (const u_char
*)addr
;
628 static const u_char mapped
[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
629 static const u_char tunnelled
[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
632 register struct hostent
*hp
;
633 char qbuf
[MAXDNAME
+1], *qp
;
635 register struct hostent
*rhp
;
638 char hname2
[MAXDNAME
+1];
639 #endif /*SUNSECURITY*/
640 extern struct hostent
*_gethtbyaddr();
642 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
643 __set_h_errno (NETDB_INTERNAL
);
646 if (af
== AF_INET6
&& len
== IN6ADDRSZ
&&
647 (!bcmp(uaddr
, mapped
, sizeof mapped
) ||
648 !bcmp(uaddr
, tunnelled
, sizeof tunnelled
))) {
650 addr
+= sizeof mapped
;
651 uaddr
+= sizeof mapped
;
663 __set_errno (EAFNOSUPPORT
);
664 __set_h_errno (NETDB_INTERNAL
);
668 __set_errno (EINVAL
);
669 __set_h_errno (NETDB_INTERNAL
);
674 (void) sprintf(qbuf
, "%u.%u.%u.%u.in-addr.arpa",
682 for (n
= IN6ADDRSZ
- 1; n
>= 0; n
--) {
683 qp
+= SPRINTF((qp
, "%x.%x.",
685 (uaddr
[n
] >> 4) & 0xf));
687 strcpy(qp
, "ip6.int");
692 n
= res_query(qbuf
, C_IN
, T_PTR
, (u_char
*)buf
.buf
, sizeof buf
.buf
);
694 dprintf("res_query failed (%d)\n", n
);
695 if (errno
== ECONNREFUSED
)
696 return (_gethtbyaddr(addr
, len
, af
));
699 if (!(hp
= getanswer(&buf
, n
, qbuf
, T_PTR
)))
700 return (NULL
); /* h_errno was set by getanswer() */
704 * turn off search as the name should be absolute,
705 * 'localhost' should be matched by defnames
707 strncpy(hname2
, hp
->h_name
, MAXDNAME
);
708 hname2
[MAXDNAME
] = '\0';
709 old_options
= _res
.options
;
710 _res
.options
&= ~RES_DNSRCH
;
711 _res
.options
|= RES_DEFNAMES
;
712 if (!(rhp
= gethostbyname(hname2
))) {
713 syslog(LOG_NOTICE
|LOG_AUTH
,
714 "gethostbyaddr: No A record for %s (verifying [%s])",
715 hname2
, inet_ntoa(*((struct in_addr
*)addr
)));
716 _res
.options
= old_options
;
717 __set_h_errno (HOST_NOT_FOUND
);
720 _res
.options
= old_options
;
721 for (haddr
= rhp
->h_addr_list
; *haddr
; haddr
++)
722 if (!memcmp(*haddr
, addr
, INADDRSZ
))
725 syslog(LOG_NOTICE
|LOG_AUTH
,
726 "gethostbyaddr: A record of %s != PTR record [%s]",
727 hname2
, inet_ntoa(*((struct in_addr
*)addr
)));
728 __set_h_errno (HOST_NOT_FOUND
);
732 #endif /*SUNSECURITY*/
735 bcopy(addr
, host_addr
, len
);
736 h_addr_ptrs
[0] = (char *)host_addr
;
737 h_addr_ptrs
[1] = NULL
;
738 if (af
== AF_INET
&& (_res
.options
& RES_USE_INET6
)) {
739 map_v4v6_address((char*)host_addr
, (char*)host_addr
);
740 hp
->h_addrtype
= AF_INET6
;
741 hp
->h_length
= IN6ADDRSZ
;
743 __set_h_errno (NETDB_SUCCESS
);
752 hostf
= fopen(_PATH_HOSTS
, "r" );
761 if (hostf
&& !stayopen
) {
762 (void) fclose(hostf
);
771 register char *cp
, **q
;
774 if (!hostf
&& !(hostf
= fopen(_PATH_HOSTS
, "r" ))) {
775 __set_h_errno (NETDB_INTERNAL
);
779 if (!(p
= fgets(hostbuf
, sizeof hostbuf
, hostf
))) {
780 __set_h_errno (HOST_NOT_FOUND
);
785 if (!(cp
= strpbrk(p
, "#\n")))
788 if (!(cp
= strpbrk(p
, " \t")))
791 if (inet_pton(AF_INET6
, p
, host_addr
) > 0) {
794 } else if (inet_pton(AF_INET
, p
, host_addr
) > 0) {
795 if (_res
.options
& RES_USE_INET6
) {
796 map_v4v6_address((char*)host_addr
, (char*)host_addr
);
806 h_addr_ptrs
[0] = (char *)host_addr
;
807 h_addr_ptrs
[1] = NULL
;
808 host
.h_addr_list
= h_addr_ptrs
;
810 host
.h_addrtype
= af
;
811 while (*cp
== ' ' || *cp
== '\t')
814 q
= host
.h_aliases
= host_aliases
;
815 if ((cp
= strpbrk(cp
, " \t")))
818 if (*cp
== ' ' || *cp
== '\t') {
822 if (q
< &host_aliases
[MAXALIASES
- 1])
824 if ((cp
= strpbrk(cp
, " \t")))
828 __set_h_errno (NETDB_SUCCESS
);
836 extern struct hostent
*_gethtbyname2();
839 if (_res
.options
& RES_USE_INET6
) {
840 hp
= _gethtbyname2(name
, AF_INET6
);
844 return (_gethtbyname2(name
, AF_INET
));
848 _gethtbyname2(name
, af
)
852 register struct hostent
*p
;
856 while ((p
= _gethtent())) {
857 if (p
->h_addrtype
!= af
)
859 if (strcasecmp(p
->h_name
, name
) == 0)
861 for (cp
= p
->h_aliases
; *cp
!= 0; cp
++)
862 if (strcasecmp(*cp
, name
) == 0)
871 _gethtbyaddr(addr
, len
, af
)
876 register struct hostent
*p
;
879 while ((p
= _gethtent()))
880 if (p
->h_addrtype
== af
&& !bcmp(p
->h_addr
, addr
, len
))
887 map_v4v6_address(src
, dst
)
891 u_char
*p
= (u_char
*)dst
;
895 /* Stash a temporary copy so our caller can update in place. */
896 bcopy(src
, tmp
, INADDRSZ
);
897 /* Mark this ipv6 addr as a mapped ipv4. */
898 for (i
= 0; i
< 10; i
++)
902 /* Retrieve the saved copy and we're done. */
903 bcopy(tmp
, (void*)p
, INADDRSZ
);
907 map_v4v6_hostent(hp
, bpp
, lenp
)
914 if (hp
->h_addrtype
!= AF_INET
|| hp
->h_length
!= INADDRSZ
)
916 hp
->h_addrtype
= AF_INET6
;
917 hp
->h_length
= IN6ADDRSZ
;
918 for (ap
= hp
->h_addr_list
; *ap
; ap
++) {
919 int i
= sizeof(align
) - ((u_long
)*bpp
% sizeof(align
));
921 if (*lenp
< (i
+ IN6ADDRSZ
)) {
922 /* Out of memory. Truncate address list here. XXX */
928 map_v4v6_address(*ap
, *bpp
);
943 short aval
[MAXADDRS
];
947 for (i
= 0; i
< num
; i
++, p
++) {
948 for (j
= 0 ; (unsigned)j
< _res
.nsort
; j
++)
949 if (_res
.sort_list
[j
].addr
.s_addr
==
950 (((struct in_addr
*)(*p
))->s_addr
& _res
.sort_list
[j
].mask
))
953 if (needsort
== 0 && i
> 0 && j
< aval
[i
-1])
959 while (needsort
< num
) {
960 for (j
= needsort
- 1; j
>= 0; j
--) {
961 if (aval
[j
] > aval
[j
+1]) {
980 #if defined(BSD43_BSD43_NFS) || defined(sun)
981 /* some libc's out there are bound internally to these names (UMIPS) */
983 ht_sethostent(stayopen
)
996 ht_gethostbyname(name
)
999 return (_gethtbyname(name
));
1003 ht_gethostbyaddr(addr
, len
, af
)
1008 return (_gethtbyaddr(addr
, len
, af
));
1014 return (_gethtent());
1024 dn_skipname(comp_dn
, eom
)
1025 const u_char
*comp_dn
, *eom
;
1027 return (__dn_skipname(comp_dn
, eom
));
1029 #endif /*old-style libc with yp junk in it*/