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
];
144 if (_res
.options
& RES_DEBUG
) {
152 # define dprintf(msg, num) /*nada*/
155 static struct hostent
*
156 getanswer(answer
, anslen
, qname
, qtype
)
157 const querybuf
*answer
;
162 register const HEADER
*hp
;
163 register const u_char
*cp
;
166 char *bp
, **ap
, **hap
;
167 int type
, class, buflen
, ancount
, qdcount
;
168 int haveanswer
, had_error
;
172 int (*name_ok
) __P((const char *));
176 eom
= answer
->buf
+ anslen
;
186 return (NULL
); /* XXX should be abort(); */
189 * find first satisfactory answer
192 ancount
= ntohs(hp
->ancount
);
193 qdcount
= ntohs(hp
->qdcount
);
195 buflen
= sizeof hostbuf
;
196 cp
= answer
->buf
+ HFIXEDSZ
;
198 h_errno
= NO_RECOVERY
;
201 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
202 if ((n
< 0) || !(*name_ok
)(bp
)) {
203 h_errno
= NO_RECOVERY
;
207 if (qtype
== T_A
|| qtype
== T_AAAA
) {
208 /* res_send() has already verified that the query name is the
209 * same as the one we sent; this just gets the expanded name
210 * (i.e., with the succeeding search-domain tacked on).
212 n
= strlen(bp
) + 1; /* for the \0 */
216 /* The qname can be abbreviated, but h_name is now absolute. */
221 host
.h_aliases
= host_aliases
;
224 host
.h_addr_list
= h_addr_ptrs
;
227 while (ancount
-- > 0 && cp
< eom
&& !had_error
) {
228 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
229 if ((n
< 0) || !(*name_ok
)(bp
)) {
234 type
= _getshort(cp
);
235 cp
+= INT16SZ
; /* type */
236 class = _getshort(cp
);
237 cp
+= INT16SZ
+ INT32SZ
; /* class, TTL */
239 cp
+= INT16SZ
; /* len */
241 /* XXX - debug? syslog? */
243 continue; /* XXX - had_error++ ? */
245 if ((qtype
== T_A
|| qtype
== T_AAAA
) && type
== T_CNAME
) {
246 if (ap
>= &host_aliases
[MAXALIASES
-1])
248 n
= dn_expand(answer
->buf
, eom
, cp
, tbuf
, sizeof tbuf
);
249 if ((n
< 0) || !(*name_ok
)(tbuf
)) {
256 n
= strlen(bp
) + 1; /* for the \0 */
259 /* Get canonical name. */
260 n
= strlen(tbuf
) + 1; /* for the \0 */
271 if (qtype
== T_PTR
&& type
== T_CNAME
) {
272 n
= dn_expand(answer
->buf
, eom
, cp
, tbuf
, sizeof tbuf
);
273 if ((n
< 0) || !res_hnok(tbuf
)) {
278 /* Get canonical name. */
279 n
= strlen(tbuf
) + 1; /* for the \0 */
291 syslog(LOG_NOTICE
|LOG_AUTH
,
292 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
293 qname
, p_class(C_IN
), p_type(qtype
),
296 continue; /* XXX - had_error++ ? */
300 if (strcasecmp(tname
, bp
) != 0) {
301 syslog(LOG_NOTICE
|LOG_AUTH
,
302 AskedForGot
, qname
, bp
);
304 continue; /* XXX - had_error++ ? */
306 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
307 if ((n
< 0) || !res_hnok(bp
)) {
311 #if MULTI_PTRS_ARE_ALIASES
315 else if (ap
< &host_aliases
[MAXALIASES
-1])
320 n
= strlen(bp
) + 1; /* for the \0 */
327 if (_res
.options
& RES_USE_INET6
) {
328 n
= strlen(bp
) + 1; /* for the \0 */
331 map_v4v6_hostent(&host
, &bp
, &buflen
);
333 h_errno
= NETDB_SUCCESS
;
338 if (strcasecmp(host
.h_name
, bp
) != 0) {
339 syslog(LOG_NOTICE
|LOG_AUTH
,
340 AskedForGot
, host
.h_name
, bp
);
342 continue; /* XXX - had_error++ ? */
345 if (n
!= host
.h_length
) {
353 nn
= strlen(bp
) + 1; /* for the \0 */
358 bp
+= sizeof(align
) - ((u_long
)bp
% sizeof(align
));
360 if (bp
+ n
>= &hostbuf
[sizeof hostbuf
]) {
361 dprintf("size (%d) too big\n", n
);
365 if (hap
>= &h_addr_ptrs
[MAXADDRS
-1]) {
367 dprintf("Too many addresses (%d)\n",
372 bcopy(cp
, *hap
++ = bp
, n
);
386 # if defined(RESOLVSORT)
388 * Note: we sort even if host can take only one address
389 * in its return structures - should give it the "best"
390 * address in that case, not some random one
392 if (_res
.nsort
&& haveanswer
> 1 && qtype
== T_A
)
393 addrsort(h_addr_ptrs
, haveanswer
);
394 # endif /*RESOLVSORT*/
396 n
= strlen(qname
) + 1; /* for the \0 */
404 if (_res
.options
& RES_USE_INET6
)
405 map_v4v6_hostent(&host
, &bp
, &buflen
);
406 h_errno
= NETDB_SUCCESS
;
420 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
421 h_errno
= NETDB_INTERNAL
;
424 if (_res
.options
& RES_USE_INET6
) {
425 hp
= gethostbyname2(name
, AF_INET6
);
429 return (gethostbyname2(name
, AF_INET
));
433 gethostbyname2(name
, af
)
438 register const char *cp
;
440 int n
, size
, type
, len
;
441 extern struct hostent
*_gethtbyname2();
443 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
444 h_errno
= NETDB_INTERNAL
;
458 h_errno
= NETDB_INTERNAL
;
459 errno
= EAFNOSUPPORT
;
463 host
.h_addrtype
= af
;
464 host
.h_length
= size
;
467 * if there aren't any dots, it could be a user-level alias.
468 * this is also done in res_query() since we are not the only
469 * function that looks up host names.
471 if (!strchr(name
, '.') && (cp
= __hostalias(name
)))
475 * disallow names consisting only of digits/dots, unless
478 if (isdigit(name
[0]))
479 for (cp
= name
;; ++cp
) {
484 * All-numeric, no dot at the end.
485 * Fake up a hostent as if we'd actually
488 if (inet_pton(af
, name
, host_addr
) <= 0) {
489 h_errno
= HOST_NOT_FOUND
;
492 strncpy(hostbuf
, name
, MAXDNAME
);
493 hostbuf
[MAXDNAME
] = '\0';
494 bp
= hostbuf
+ MAXDNAME
;
495 len
= sizeof hostbuf
- MAXDNAME
;
496 host
.h_name
= hostbuf
;
497 host
.h_aliases
= host_aliases
;
498 host_aliases
[0] = NULL
;
499 h_addr_ptrs
[0] = (char *)host_addr
;
500 h_addr_ptrs
[1] = NULL
;
501 host
.h_addr_list
= h_addr_ptrs
;
502 if (_res
.options
& RES_USE_INET6
)
503 map_v4v6_hostent(&host
, &bp
, &len
);
504 h_errno
= NETDB_SUCCESS
;
507 if (!isdigit(*cp
) && *cp
!= '.')
510 if (isxdigit(name
[0]) || name
[0] == ':')
511 for (cp
= name
;; ++cp
) {
516 * All-IPv6-legal, no dot at the end.
517 * Fake up a hostent as if we'd actually
520 if (inet_pton(af
, name
, host_addr
) <= 0) {
521 h_errno
= HOST_NOT_FOUND
;
524 strncpy(hostbuf
, name
, MAXDNAME
);
525 hostbuf
[MAXDNAME
] = '\0';
526 bp
= hostbuf
+ MAXDNAME
;
527 len
= sizeof hostbuf
- MAXDNAME
;
528 host
.h_name
= hostbuf
;
529 host
.h_aliases
= host_aliases
;
530 host_aliases
[0] = NULL
;
531 h_addr_ptrs
[0] = (char *)host_addr
;
532 h_addr_ptrs
[1] = NULL
;
533 host
.h_addr_list
= h_addr_ptrs
;
534 h_errno
= NETDB_SUCCESS
;
537 if (!isxdigit(*cp
) && *cp
!= ':' && *cp
!= '.')
541 if ((n
= res_search(name
, C_IN
, type
, buf
.buf
, sizeof(buf
))) < 0) {
542 dprintf("res_search failed (%d)\n", n
);
543 if (errno
== ECONNREFUSED
)
544 return (_gethtbyname2(name
, af
));
547 return (getanswer(&buf
, n
, name
, type
));
551 gethostbyaddr(addr
, len
, af
)
552 const char *addr
; /* XXX should have been def'd as u_char! */
555 const u_char
*uaddr
= (const u_char
*)addr
;
556 static const u_char mapped
[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
557 static const u_char tunnelled
[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
560 register struct hostent
*hp
;
561 char qbuf
[MAXDNAME
+1], *qp
;
563 register struct hostent
*rhp
;
566 char hname2
[MAXDNAME
+1];
567 #endif /*SUNSECURITY*/
568 extern struct hostent
*_gethtbyaddr();
570 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
571 h_errno
= NETDB_INTERNAL
;
574 if (af
== AF_INET6
&& len
== IN6ADDRSZ
&&
575 (!bcmp(uaddr
, mapped
, sizeof mapped
) ||
576 !bcmp(uaddr
, tunnelled
, sizeof tunnelled
))) {
578 addr
+= sizeof mapped
;
579 uaddr
+= sizeof mapped
;
591 errno
= EAFNOSUPPORT
;
592 h_errno
= NETDB_INTERNAL
;
597 h_errno
= NETDB_INTERNAL
;
602 (void) sprintf(qbuf
, "%u.%u.%u.%u.in-addr.arpa",
610 for (n
= IN6ADDRSZ
- 1; n
>= 0; n
--) {
611 qp
+= SPRINTF((qp
, "%x.%x.",
613 (uaddr
[n
] >> 4) & 0xf));
615 strcpy(qp
, "ip6.int");
620 n
= res_query(qbuf
, C_IN
, T_PTR
, (u_char
*)buf
.buf
, sizeof buf
.buf
);
622 dprintf("res_query failed (%d)\n", n
);
623 if (errno
== ECONNREFUSED
)
624 return (_gethtbyaddr(addr
, len
, af
));
627 if (!(hp
= getanswer(&buf
, n
, qbuf
, T_PTR
)))
628 return (NULL
); /* h_errno was set by getanswer() */
632 * turn off search as the name should be absolute,
633 * 'localhost' should be matched by defnames
635 strncpy(hname2
, hp
->h_name
, MAXDNAME
);
636 hname2
[MAXDNAME
] = '\0';
637 old_options
= _res
.options
;
638 _res
.options
&= ~RES_DNSRCH
;
639 _res
.options
|= RES_DEFNAMES
;
640 if (!(rhp
= gethostbyname(hname2
))) {
641 syslog(LOG_NOTICE
|LOG_AUTH
,
642 "gethostbyaddr: No A record for %s (verifying [%s])",
643 hname2
, inet_ntoa(*((struct in_addr
*)addr
)));
644 _res
.options
= old_options
;
645 h_errno
= HOST_NOT_FOUND
;
648 _res
.options
= old_options
;
649 for (haddr
= rhp
->h_addr_list
; *haddr
; haddr
++)
650 if (!memcmp(*haddr
, addr
, INADDRSZ
))
653 syslog(LOG_NOTICE
|LOG_AUTH
,
654 "gethostbyaddr: A record of %s != PTR record [%s]",
655 hname2
, inet_ntoa(*((struct in_addr
*)addr
)));
656 h_errno
= HOST_NOT_FOUND
;
660 #endif /*SUNSECURITY*/
663 bcopy(addr
, host_addr
, len
);
664 h_addr_ptrs
[0] = (char *)host_addr
;
665 h_addr_ptrs
[1] = NULL
;
666 if (af
== AF_INET
&& (_res
.options
& RES_USE_INET6
)) {
667 map_v4v6_address((char*)host_addr
, (char*)host_addr
);
668 hp
->h_addrtype
= AF_INET6
;
669 hp
->h_length
= IN6ADDRSZ
;
671 h_errno
= NETDB_SUCCESS
;
680 hostf
= fopen(_PATH_HOSTS
, "r" );
689 if (hostf
&& !stayopen
) {
690 (void) fclose(hostf
);
699 register char *cp
, **q
;
702 if (!hostf
&& !(hostf
= fopen(_PATH_HOSTS
, "r" ))) {
703 h_errno
= NETDB_INTERNAL
;
707 if (!(p
= fgets(hostbuf
, sizeof hostbuf
, hostf
))) {
708 h_errno
= HOST_NOT_FOUND
;
713 if (!(cp
= strpbrk(p
, "#\n")))
716 if (!(cp
= strpbrk(p
, " \t")))
719 if ((_res
.options
& RES_USE_INET6
) &&
720 inet_pton(AF_INET6
, p
, host_addr
) > 0) {
723 } else if (inet_pton(AF_INET
, p
, host_addr
) > 0) {
724 if (_res
.options
& RES_USE_INET6
) {
725 map_v4v6_address((char*)host_addr
, (char*)host_addr
);
735 h_addr_ptrs
[0] = (char *)host_addr
;
736 h_addr_ptrs
[1] = NULL
;
737 host
.h_addr_list
= h_addr_ptrs
;
739 host
.h_addrtype
= af
;
740 while (*cp
== ' ' || *cp
== '\t')
743 q
= host
.h_aliases
= host_aliases
;
744 if (cp
= strpbrk(cp
, " \t"))
747 if (*cp
== ' ' || *cp
== '\t') {
751 if (q
< &host_aliases
[MAXALIASES
- 1])
753 if (cp
= strpbrk(cp
, " \t"))
757 if (_res
.options
& RES_USE_INET6
) {
759 int buflen
= sizeof hostbuf
;
761 map_v4v6_hostent(&host
, &bp
, &buflen
);
763 h_errno
= NETDB_SUCCESS
;
771 extern struct hostent
*_gethtbyname2();
774 if (_res
.options
& RES_USE_INET6
) {
775 hp
= _gethtbyname2(name
, AF_INET6
);
779 return (_gethtbyname2(name
, AF_INET
));
783 _gethtbyname2(name
, af
)
787 register struct hostent
*p
;
791 while (p
= _gethtent()) {
792 if (p
->h_addrtype
!= af
)
794 if (strcasecmp(p
->h_name
, name
) == 0)
796 for (cp
= p
->h_aliases
; *cp
!= 0; cp
++)
797 if (strcasecmp(*cp
, name
) == 0)
806 _gethtbyaddr(addr
, len
, af
)
810 register struct hostent
*p
;
813 while (p
= _gethtent())
814 if (p
->h_addrtype
== af
&& !bcmp(p
->h_addr
, addr
, len
))
821 map_v4v6_address(src
, dst
)
825 u_char
*p
= (u_char
*)dst
;
829 /* Stash a temporary copy so our caller can update in place. */
830 bcopy(src
, tmp
, INADDRSZ
);
831 /* Mark this ipv6 addr as a mapped ipv4. */
832 for (i
= 0; i
< 10; i
++)
836 /* Retrieve the saved copy and we're done. */
837 bcopy(tmp
, (void*)p
, INADDRSZ
);
841 map_v4v6_hostent(hp
, bpp
, lenp
)
848 if (hp
->h_addrtype
!= AF_INET
|| hp
->h_length
!= INADDRSZ
)
850 hp
->h_addrtype
= AF_INET6
;
851 hp
->h_length
= IN6ADDRSZ
;
852 for (ap
= hp
->h_addr_list
; *ap
; ap
++) {
853 int i
= sizeof(align
) - ((u_long
)*bpp
% sizeof(align
));
855 if (*lenp
< (i
+ IN6ADDRSZ
)) {
856 /* Out of memory. Truncate address list here. XXX */
862 map_v4v6_address(*ap
, *bpp
);
877 short aval
[MAXADDRS
];
881 for (i
= 0; i
< num
; i
++, p
++) {
882 for (j
= 0 ; (unsigned)j
< _res
.nsort
; j
++)
883 if (_res
.sort_list
[j
].addr
.s_addr
==
884 (((struct in_addr
*)(*p
))->s_addr
& _res
.sort_list
[j
].mask
))
887 if (needsort
== 0 && i
> 0 && j
< aval
[i
-1])
893 while (needsort
< num
) {
894 for (j
= needsort
- 1; j
>= 0; j
--) {
895 if (aval
[j
] > aval
[j
+1]) {
914 #if defined(BSD43_BSD43_NFS) || defined(sun)
915 /* some libc's out there are bound internally to these names (UMIPS) */
917 ht_sethostent(stayopen
)
930 ht_gethostbyname(name
)
933 return (_gethtbyname(name
));
937 ht_gethostbyaddr(addr
, len
, af
)
941 return (_gethtbyaddr(addr
, len
, af
));
947 return (_gethtent());
957 dn_skipname(comp_dn
, eom
)
958 const u_char
*comp_dn
, *eom
;
960 return (__dn_skipname(comp_dn
, eom
));
962 #endif /*old-style libc with yp junk in it*/