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 static void addrsort
__P((char **, int));
121 #define MAXPACKET PACKETSZ
123 #define MAXPACKET 1024
128 u_char buf
[MAXPACKET
];
146 if (_res
.options
& RES_DEBUG
) {
154 # define dprintf(msg, num) /*nada*/
157 static struct hostent
*
158 getanswer(answer
, anslen
, qname
, qtype
)
159 const querybuf
*answer
;
164 register const HEADER
*hp
;
165 register const u_char
*cp
;
168 char *bp
, **ap
, **hap
;
169 int type
, class, buflen
, ancount
, qdcount
;
170 int haveanswer
, had_error
;
174 int (*name_ok
) __P((const char *));
178 eom
= answer
->buf
+ anslen
;
188 return (NULL
); /* XXX should be abort(); */
191 * find first satisfactory answer
194 ancount
= ntohs(hp
->ancount
);
195 qdcount
= ntohs(hp
->qdcount
);
197 buflen
= sizeof hostbuf
;
198 cp
= answer
->buf
+ HFIXEDSZ
;
200 __set_h_errno (NO_RECOVERY
);
203 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
204 if ((n
< 0) || !(*name_ok
)(bp
)) {
205 __set_h_errno (NO_RECOVERY
);
209 if (qtype
== T_A
|| qtype
== T_AAAA
) {
210 /* res_send() has already verified that the query name is the
211 * same as the one we sent; this just gets the expanded name
212 * (i.e., with the succeeding search-domain tacked on).
214 n
= strlen(bp
) + 1; /* for the \0 */
218 /* The qname can be abbreviated, but h_name is now absolute. */
223 host
.h_aliases
= host_aliases
;
226 host
.h_addr_list
= h_addr_ptrs
;
229 while (ancount
-- > 0 && cp
< eom
&& !had_error
) {
230 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
231 if ((n
< 0) || !(*name_ok
)(bp
)) {
236 type
= _getshort(cp
);
237 cp
+= INT16SZ
; /* type */
238 class = _getshort(cp
);
239 cp
+= INT16SZ
+ INT32SZ
; /* class, TTL */
241 cp
+= INT16SZ
; /* len */
243 /* XXX - debug? syslog? */
245 continue; /* XXX - had_error++ ? */
247 if ((qtype
== T_A
|| qtype
== T_AAAA
) && type
== T_CNAME
) {
248 if (ap
>= &host_aliases
[MAXALIASES
-1])
250 n
= dn_expand(answer
->buf
, eom
, cp
, tbuf
, sizeof tbuf
);
251 if ((n
< 0) || !(*name_ok
)(tbuf
)) {
258 n
= strlen(bp
) + 1; /* for the \0 */
261 /* Get canonical name. */
262 n
= strlen(tbuf
) + 1; /* for the \0 */
273 if (qtype
== T_PTR
&& type
== T_CNAME
) {
274 n
= dn_expand(answer
->buf
, eom
, cp
, tbuf
, sizeof tbuf
);
275 if ((n
< 0) || !res_hnok(tbuf
)) {
280 /* Get canonical name. */
281 n
= strlen(tbuf
) + 1; /* for the \0 */
293 syslog(LOG_NOTICE
|LOG_AUTH
,
294 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
295 qname
, p_class(C_IN
), p_type(qtype
),
298 continue; /* XXX - had_error++ ? */
302 if (strcasecmp(tname
, bp
) != 0) {
303 syslog(LOG_NOTICE
|LOG_AUTH
,
304 AskedForGot
, qname
, bp
);
306 continue; /* XXX - had_error++ ? */
308 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
309 if ((n
< 0) || !res_hnok(bp
)) {
313 #if MULTI_PTRS_ARE_ALIASES
317 else if (ap
< &host_aliases
[MAXALIASES
-1])
322 n
= strlen(bp
) + 1; /* for the \0 */
329 if (_res
.options
& RES_USE_INET6
) {
330 n
= strlen(bp
) + 1; /* for the \0 */
333 map_v4v6_hostent(&host
, &bp
, &buflen
);
335 __set_h_errno (NETDB_SUCCESS
);
340 if (strcasecmp(host
.h_name
, bp
) != 0) {
341 syslog(LOG_NOTICE
|LOG_AUTH
,
342 AskedForGot
, host
.h_name
, bp
);
344 continue; /* XXX - had_error++ ? */
346 if (n
!= host
.h_length
) {
354 nn
= strlen(bp
) + 1; /* for the \0 */
359 bp
+= sizeof(align
) - ((u_long
)bp
% sizeof(align
));
361 if (bp
+ n
>= &hostbuf
[sizeof hostbuf
]) {
362 dprintf("size (%d) too big\n", n
);
366 if (hap
>= &h_addr_ptrs
[MAXADDRS
-1]) {
368 dprintf("Too many addresses (%d)\n",
373 bcopy(cp
, *hap
++ = bp
, n
);
387 # if defined(RESOLVSORT)
389 * Note: we sort even if host can take only one address
390 * in its return structures - should give it the "best"
391 * address in that case, not some random one
393 if (_res
.nsort
&& haveanswer
> 1 && qtype
== T_A
)
394 addrsort(h_addr_ptrs
, haveanswer
);
395 # endif /*RESOLVSORT*/
397 n
= strlen(qname
) + 1; /* for the \0 */
405 if (_res
.options
& RES_USE_INET6
)
406 map_v4v6_hostent(&host
, &bp
, &buflen
);
407 __set_h_errno (NETDB_SUCCESS
);
411 __set_h_errno (TRY_AGAIN
);
421 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
422 __set_h_errno (NETDB_INTERNAL
);
425 if (_res
.options
& RES_USE_INET6
) {
426 hp
= gethostbyname2(name
, AF_INET6
);
430 return (gethostbyname2(name
, AF_INET
));
434 gethostbyname2(name
, af
)
439 register const char *cp
;
441 int n
, size
, type
, len
;
442 extern struct hostent
*_gethtbyname2();
444 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
445 __set_h_errno (NETDB_INTERNAL
);
459 __set_h_errno (NETDB_INTERNAL
);
460 __set_errno (EAFNOSUPPORT
);
464 host
.h_addrtype
= af
;
465 host
.h_length
= size
;
468 * if there aren't any dots, it could be a user-level alias.
469 * this is also done in res_query() since we are not the only
470 * function that looks up host names.
472 if (!strchr(name
, '.') && (cp
= __hostalias(name
)))
476 * disallow names consisting only of digits/dots, unless
479 if (isdigit(name
[0]))
480 for (cp
= name
;; ++cp
) {
485 * All-numeric, no dot at the end.
486 * Fake up a hostent as if we'd actually
489 if (inet_pton(af
, name
, host_addr
) <= 0) {
490 __set_h_errno (HOST_NOT_FOUND
);
493 strncpy(hostbuf
, name
, MAXDNAME
);
494 hostbuf
[MAXDNAME
] = '\0';
495 bp
= hostbuf
+ MAXDNAME
;
496 len
= sizeof hostbuf
- MAXDNAME
;
497 host
.h_name
= hostbuf
;
498 host
.h_aliases
= host_aliases
;
499 host_aliases
[0] = NULL
;
500 h_addr_ptrs
[0] = (char *)host_addr
;
501 h_addr_ptrs
[1] = NULL
;
502 host
.h_addr_list
= h_addr_ptrs
;
503 if (_res
.options
& RES_USE_INET6
)
504 map_v4v6_hostent(&host
, &bp
, &len
);
505 __set_h_errno (NETDB_SUCCESS
);
508 if (!isdigit(*cp
) && *cp
!= '.')
511 if (isxdigit(name
[0]) || name
[0] == ':')
512 for (cp
= name
;; ++cp
) {
517 * All-IPv6-legal, no dot at the end.
518 * Fake up a hostent as if we'd actually
521 if (inet_pton(af
, name
, host_addr
) <= 0) {
522 __set_h_errno (HOST_NOT_FOUND
);
525 strncpy(hostbuf
, name
, MAXDNAME
);
526 hostbuf
[MAXDNAME
] = '\0';
527 bp
= hostbuf
+ MAXDNAME
;
528 len
= sizeof hostbuf
- MAXDNAME
;
529 host
.h_name
= hostbuf
;
530 host
.h_aliases
= host_aliases
;
531 host_aliases
[0] = NULL
;
532 h_addr_ptrs
[0] = (char *)host_addr
;
533 h_addr_ptrs
[1] = NULL
;
534 host
.h_addr_list
= h_addr_ptrs
;
535 __set_h_errno (NETDB_SUCCESS
);
538 if (!isxdigit(*cp
) && *cp
!= ':' && *cp
!= '.')
542 if ((n
= res_search(name
, C_IN
, type
, buf
.buf
, sizeof(buf
))) < 0) {
543 dprintf("res_search failed (%d)\n", n
);
544 if (errno
== ECONNREFUSED
)
545 return (_gethtbyname2(name
, af
));
548 return (getanswer(&buf
, n
, name
, type
));
552 gethostbyaddr(addr
, len
, af
)
553 const char *addr
; /* XXX should have been def'd as u_char! */
556 const u_char
*uaddr
= (const u_char
*)addr
;
557 static const u_char mapped
[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
558 static const u_char tunnelled
[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
561 register struct hostent
*hp
;
562 char qbuf
[MAXDNAME
+1], *qp
;
564 register struct hostent
*rhp
;
567 char hname2
[MAXDNAME
+1];
568 #endif /*SUNSECURITY*/
569 extern struct hostent
*_gethtbyaddr();
571 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
572 __set_h_errno (NETDB_INTERNAL
);
575 if (af
== AF_INET6
&& len
== IN6ADDRSZ
&&
576 (!bcmp(uaddr
, mapped
, sizeof mapped
) ||
577 !bcmp(uaddr
, tunnelled
, sizeof tunnelled
))) {
579 addr
+= sizeof mapped
;
580 uaddr
+= sizeof mapped
;
592 __set_errno (EAFNOSUPPORT
);
593 __set_h_errno (NETDB_INTERNAL
);
597 __set_errno (EINVAL
);
598 __set_h_errno (NETDB_INTERNAL
);
603 (void) sprintf(qbuf
, "%u.%u.%u.%u.in-addr.arpa",
611 for (n
= IN6ADDRSZ
- 1; n
>= 0; n
--) {
612 qp
+= SPRINTF((qp
, "%x.%x.",
614 (uaddr
[n
] >> 4) & 0xf));
616 strcpy(qp
, "ip6.int");
621 n
= res_query(qbuf
, C_IN
, T_PTR
, (u_char
*)buf
.buf
, sizeof buf
.buf
);
623 dprintf("res_query failed (%d)\n", n
);
624 if (errno
== ECONNREFUSED
)
625 return (_gethtbyaddr(addr
, len
, af
));
628 if (!(hp
= getanswer(&buf
, n
, qbuf
, T_PTR
)))
629 return (NULL
); /* h_errno was set by getanswer() */
633 * turn off search as the name should be absolute,
634 * 'localhost' should be matched by defnames
636 strncpy(hname2
, hp
->h_name
, MAXDNAME
);
637 hname2
[MAXDNAME
] = '\0';
638 old_options
= _res
.options
;
639 _res
.options
&= ~RES_DNSRCH
;
640 _res
.options
|= RES_DEFNAMES
;
641 if (!(rhp
= gethostbyname(hname2
))) {
642 syslog(LOG_NOTICE
|LOG_AUTH
,
643 "gethostbyaddr: No A record for %s (verifying [%s])",
644 hname2
, inet_ntoa(*((struct in_addr
*)addr
)));
645 _res
.options
= old_options
;
646 __set_h_errno (HOST_NOT_FOUND
);
649 _res
.options
= old_options
;
650 for (haddr
= rhp
->h_addr_list
; *haddr
; haddr
++)
651 if (!memcmp(*haddr
, addr
, INADDRSZ
))
654 syslog(LOG_NOTICE
|LOG_AUTH
,
655 "gethostbyaddr: A record of %s != PTR record [%s]",
656 hname2
, inet_ntoa(*((struct in_addr
*)addr
)));
657 __set_h_errno (HOST_NOT_FOUND
);
661 #endif /*SUNSECURITY*/
664 bcopy(addr
, host_addr
, len
);
665 h_addr_ptrs
[0] = (char *)host_addr
;
666 h_addr_ptrs
[1] = NULL
;
667 if (af
== AF_INET
&& (_res
.options
& RES_USE_INET6
)) {
668 map_v4v6_address((char*)host_addr
, (char*)host_addr
);
669 hp
->h_addrtype
= AF_INET6
;
670 hp
->h_length
= IN6ADDRSZ
;
672 __set_h_errno (NETDB_SUCCESS
);
681 hostf
= fopen(_PATH_HOSTS
, "r" );
690 if (hostf
&& !stayopen
) {
691 (void) fclose(hostf
);
700 register char *cp
, **q
;
703 if (!hostf
&& !(hostf
= fopen(_PATH_HOSTS
, "r" ))) {
704 __set_h_errno (NETDB_INTERNAL
);
708 if (!(p
= fgets(hostbuf
, sizeof hostbuf
, hostf
))) {
709 __set_h_errno (HOST_NOT_FOUND
);
714 if (!(cp
= strpbrk(p
, "#\n")))
717 if (!(cp
= strpbrk(p
, " \t")))
720 if ((_res
.options
& RES_USE_INET6
) &&
721 inet_pton(AF_INET6
, p
, host_addr
) > 0) {
724 } else if (inet_pton(AF_INET
, p
, host_addr
) > 0) {
725 if (_res
.options
& RES_USE_INET6
) {
726 map_v4v6_address((char*)host_addr
, (char*)host_addr
);
736 h_addr_ptrs
[0] = (char *)host_addr
;
737 h_addr_ptrs
[1] = NULL
;
738 host
.h_addr_list
= h_addr_ptrs
;
740 host
.h_addrtype
= af
;
741 while (*cp
== ' ' || *cp
== '\t')
744 q
= host
.h_aliases
= host_aliases
;
745 if (cp
= strpbrk(cp
, " \t"))
748 if (*cp
== ' ' || *cp
== '\t') {
752 if (q
< &host_aliases
[MAXALIASES
- 1])
754 if (cp
= strpbrk(cp
, " \t"))
758 if (_res
.options
& RES_USE_INET6
) {
760 int buflen
= sizeof hostbuf
;
762 map_v4v6_hostent(&host
, &bp
, &buflen
);
764 __set_h_errno (NETDB_SUCCESS
);
772 extern struct hostent
*_gethtbyname2();
775 if (_res
.options
& RES_USE_INET6
) {
776 hp
= _gethtbyname2(name
, AF_INET6
);
780 return (_gethtbyname2(name
, AF_INET
));
784 _gethtbyname2(name
, af
)
788 register struct hostent
*p
;
792 while (p
= _gethtent()) {
793 if (p
->h_addrtype
!= af
)
795 if (strcasecmp(p
->h_name
, name
) == 0)
797 for (cp
= p
->h_aliases
; *cp
!= 0; cp
++)
798 if (strcasecmp(*cp
, name
) == 0)
807 _gethtbyaddr(addr
, len
, af
)
811 register struct hostent
*p
;
814 while (p
= _gethtent())
815 if (p
->h_addrtype
== af
&& !bcmp(p
->h_addr
, addr
, len
))
822 map_v4v6_address(src
, dst
)
826 u_char
*p
= (u_char
*)dst
;
830 /* Stash a temporary copy so our caller can update in place. */
831 bcopy(src
, tmp
, INADDRSZ
);
832 /* Mark this ipv6 addr as a mapped ipv4. */
833 for (i
= 0; i
< 10; i
++)
837 /* Retrieve the saved copy and we're done. */
838 bcopy(tmp
, (void*)p
, INADDRSZ
);
842 map_v4v6_hostent(hp
, bpp
, lenp
)
849 if (hp
->h_addrtype
!= AF_INET
|| hp
->h_length
!= INADDRSZ
)
851 hp
->h_addrtype
= AF_INET6
;
852 hp
->h_length
= IN6ADDRSZ
;
853 for (ap
= hp
->h_addr_list
; *ap
; ap
++) {
854 int i
= sizeof(align
) - ((u_long
)*bpp
% sizeof(align
));
856 if (*lenp
< (i
+ IN6ADDRSZ
)) {
857 /* Out of memory. Truncate address list here. XXX */
863 map_v4v6_address(*ap
, *bpp
);
878 short aval
[MAXADDRS
];
882 for (i
= 0; i
< num
; i
++, p
++) {
883 for (j
= 0 ; (unsigned)j
< _res
.nsort
; j
++)
884 if (_res
.sort_list
[j
].addr
.s_addr
==
885 (((struct in_addr
*)(*p
))->s_addr
& _res
.sort_list
[j
].mask
))
888 if (needsort
== 0 && i
> 0 && j
< aval
[i
-1])
894 while (needsort
< num
) {
895 for (j
= needsort
- 1; j
>= 0; j
--) {
896 if (aval
[j
] > aval
[j
+1]) {
915 #if defined(BSD43_BSD43_NFS) || defined(sun)
916 /* some libc's out there are bound internally to these names (UMIPS) */
918 ht_sethostent(stayopen
)
931 ht_gethostbyname(name
)
934 return (_gethtbyname(name
));
938 ht_gethostbyaddr(addr
, len
, af
)
942 return (_gethtbyaddr(addr
, len
, af
));
948 return (_gethtent());
958 dn_skipname(comp_dn
, eom
)
959 const u_char
*comp_dn
, *eom
;
961 return (__dn_skipname(comp_dn
, eom
));
963 #endif /*old-style libc with yp junk in it*/