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 static struct hostent
*
164 getanswer(answer
, anslen
, qname
, qtype
)
165 const querybuf
*answer
;
170 register const HEADER
*hp
;
171 register const u_char
*cp
;
174 char *bp
, **ap
, **hap
;
175 int type
, class, buflen
, ancount
, qdcount
;
176 int haveanswer
, had_error
;
180 int (*name_ok
) __P((const char *));
184 eom
= answer
->buf
+ anslen
;
194 return (NULL
); /* XXX should be abort(); */
197 * find first satisfactory answer
200 ancount
= ntohs(hp
->ancount
);
201 qdcount
= ntohs(hp
->qdcount
);
203 buflen
= sizeof hostbuf
;
204 cp
= answer
->buf
+ HFIXEDSZ
;
206 __set_h_errno (NO_RECOVERY
);
209 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
210 if ((n
< 0) || !(*name_ok
)(bp
)) {
211 __set_h_errno (NO_RECOVERY
);
215 if (qtype
== T_A
|| qtype
== T_AAAA
) {
216 /* res_send() has already verified that the query name is the
217 * same as the one we sent; this just gets the expanded name
218 * (i.e., with the succeeding search-domain tacked on).
220 n
= strlen(bp
) + 1; /* for the \0 */
221 if (n
>= MAXHOSTNAMELEN
) {
222 __set_h_errno (NO_RECOVERY
);
228 /* The qname can be abbreviated, but h_name is now absolute. */
233 host
.h_aliases
= host_aliases
;
236 host
.h_addr_list
= h_addr_ptrs
;
239 while (ancount
-- > 0 && cp
< eom
&& !had_error
) {
240 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
241 if ((n
< 0) || !(*name_ok
)(bp
)) {
246 type
= _getshort(cp
);
247 cp
+= INT16SZ
; /* type */
248 class = _getshort(cp
);
249 cp
+= INT16SZ
+ INT32SZ
; /* class, TTL */
251 cp
+= INT16SZ
; /* len */
253 /* XXX - debug? syslog? */
255 continue; /* XXX - had_error++ ? */
257 if ((qtype
== T_A
|| qtype
== T_AAAA
) && type
== T_CNAME
) {
258 if (ap
>= &host_aliases
[MAXALIASES
-1])
260 n
= dn_expand(answer
->buf
, eom
, cp
, tbuf
, sizeof tbuf
);
261 if ((n
< 0) || !(*name_ok
)(tbuf
)) {
268 n
= strlen(bp
) + 1; /* for the \0 */
269 if (n
>= MAXHOSTNAMELEN
) {
275 /* Get canonical name. */
276 n
= strlen(tbuf
) + 1; /* for the \0 */
277 if (n
> buflen
|| n
>= MAXHOSTNAMELEN
) {
287 if (qtype
== T_PTR
&& type
== T_CNAME
) {
288 n
= dn_expand(answer
->buf
, eom
, cp
, tbuf
, sizeof tbuf
);
289 if (n
< 0 || !res_dnok(tbuf
)) {
294 /* Get canonical name. */
295 n
= strlen(tbuf
) + 1; /* for the \0 */
296 if (n
> buflen
|| n
>= MAXHOSTNAMELEN
) {
307 syslog(LOG_NOTICE
|LOG_AUTH
,
308 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
309 qname
, p_class(C_IN
), p_type(qtype
),
312 continue; /* XXX - had_error++ ? */
316 if (strcasecmp(tname
, bp
) != 0) {
317 syslog(LOG_NOTICE
|LOG_AUTH
,
318 AskedForGot
, qname
, bp
);
320 continue; /* XXX - had_error++ ? */
322 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
323 if ((n
< 0) || !res_hnok(bp
)) {
327 #if MULTI_PTRS_ARE_ALIASES
331 else if (ap
< &host_aliases
[MAXALIASES
-1])
336 n
= strlen(bp
) + 1; /* for the \0 */
337 if (n
>= MAXHOSTNAMELEN
) {
347 if (_res
.options
& RES_USE_INET6
) {
348 n
= strlen(bp
) + 1; /* for the \0 */
349 if (n
>= MAXHOSTNAMELEN
) {
355 map_v4v6_hostent(&host
, &bp
, &buflen
);
357 __set_h_errno (NETDB_SUCCESS
);
362 if (strcasecmp(host
.h_name
, bp
) != 0) {
363 syslog(LOG_NOTICE
|LOG_AUTH
,
364 AskedForGot
, host
.h_name
, bp
);
366 continue; /* XXX - had_error++ ? */
368 if (n
!= host
.h_length
) {
376 nn
= strlen(bp
) + 1; /* for the \0 */
381 bp
+= sizeof(align
) - ((u_long
)bp
% sizeof(align
));
383 if (bp
+ n
>= &hostbuf
[sizeof hostbuf
]) {
384 dprintf("size (%d) too big\n", n
);
388 if (hap
>= &h_addr_ptrs
[MAXADDRS
-1]) {
390 dprintf("Too many addresses (%d)\n",
396 bcopy(cp
, *hap
++ = bp
, n
);
410 # if defined(RESOLVSORT)
412 * Note: we sort even if host can take only one address
413 * in its return structures - should give it the "best"
414 * address in that case, not some random one
416 if (_res
.nsort
&& haveanswer
> 1 && qtype
== T_A
)
417 addrsort(h_addr_ptrs
, haveanswer
);
418 # endif /*RESOLVSORT*/
420 n
= strlen(qname
) + 1; /* for the \0 */
421 if (n
> buflen
|| n
>= MAXHOSTNAMELEN
)
428 if (_res
.options
& RES_USE_INET6
)
429 map_v4v6_hostent(&host
, &bp
, &buflen
);
430 __set_h_errno (NETDB_SUCCESS
);
434 __set_h_errno (NO_RECOVERY
);
444 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
445 __set_h_errno (NETDB_INTERNAL
);
448 if (_res
.options
& RES_USE_INET6
) {
449 hp
= gethostbyname2(name
, AF_INET6
);
453 return (gethostbyname2(name
, AF_INET
));
457 gethostbyname2(name
, af
)
462 register const char *cp
;
464 int n
, size
, type
, len
;
465 extern struct hostent
*_gethtbyname2();
467 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
468 __set_h_errno (NETDB_INTERNAL
);
482 __set_h_errno (NETDB_INTERNAL
);
483 __set_errno (EAFNOSUPPORT
);
487 host
.h_addrtype
= af
;
488 host
.h_length
= size
;
491 * if there aren't any dots, it could be a user-level alias.
492 * this is also done in res_query() since we are not the only
493 * function that looks up host names.
495 if (!strchr(name
, '.') && (cp
= __hostalias(name
)))
499 * disallow names consisting only of digits/dots, unless
502 if (isdigit(name
[0]))
503 for (cp
= name
;; ++cp
) {
508 * All-numeric, no dot at the end.
509 * Fake up a hostent as if we'd actually
512 if (inet_pton(af
, name
, host_addr
) <= 0) {
513 __set_h_errno (HOST_NOT_FOUND
);
516 strncpy(hostbuf
, name
, MAXDNAME
);
517 hostbuf
[MAXDNAME
] = '\0';
518 bp
= hostbuf
+ MAXDNAME
;
519 len
= sizeof hostbuf
- MAXDNAME
;
520 host
.h_name
= hostbuf
;
521 host
.h_aliases
= host_aliases
;
522 host_aliases
[0] = NULL
;
523 h_addr_ptrs
[0] = (char *)host_addr
;
524 h_addr_ptrs
[1] = NULL
;
525 host
.h_addr_list
= h_addr_ptrs
;
526 if (_res
.options
& RES_USE_INET6
)
527 map_v4v6_hostent(&host
, &bp
, &len
);
528 __set_h_errno (NETDB_SUCCESS
);
531 if (!isdigit(*cp
) && *cp
!= '.')
534 if ((isxdigit(name
[0]) && strchr(name
, ':') != NULL
) ||
536 for (cp
= name
;; ++cp
) {
541 * All-IPv6-legal, no dot at the end.
542 * Fake up a hostent as if we'd actually
545 if (inet_pton(af
, name
, host_addr
) <= 0) {
546 __set_h_errno (HOST_NOT_FOUND
);
549 strncpy(hostbuf
, name
, MAXDNAME
);
550 hostbuf
[MAXDNAME
] = '\0';
551 bp
= hostbuf
+ MAXDNAME
;
552 len
= sizeof hostbuf
- MAXDNAME
;
553 host
.h_name
= hostbuf
;
554 host
.h_aliases
= host_aliases
;
555 host_aliases
[0] = NULL
;
556 h_addr_ptrs
[0] = (char *)host_addr
;
557 h_addr_ptrs
[1] = NULL
;
558 host
.h_addr_list
= h_addr_ptrs
;
559 __set_h_errno (NETDB_SUCCESS
);
562 if (!isxdigit(*cp
) && *cp
!= ':' && *cp
!= '.')
566 if ((n
= res_search(name
, C_IN
, type
, buf
.buf
, sizeof(buf
.buf
))) < 0) {
567 dprintf("res_search failed (%d)\n", n
);
568 if (errno
== ECONNREFUSED
)
569 return (_gethtbyname2(name
, af
));
572 return (getanswer(&buf
, n
, name
, type
));
576 gethostbyaddr(addr
, len
, af
)
577 const char *addr
; /* XXX should have been def'd as u_char! */
580 const u_char
*uaddr
= (const u_char
*)addr
;
581 static const u_char mapped
[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
582 static const u_char tunnelled
[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
585 register struct hostent
*hp
;
586 char qbuf
[MAXDNAME
+1], *qp
;
588 register struct hostent
*rhp
;
591 char hname2
[MAXDNAME
+1];
592 #endif /*SUNSECURITY*/
593 extern struct hostent
*_gethtbyaddr();
595 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
596 __set_h_errno (NETDB_INTERNAL
);
599 if (af
== AF_INET6
&& len
== IN6ADDRSZ
&&
600 (!bcmp(uaddr
, mapped
, sizeof mapped
) ||
601 !bcmp(uaddr
, tunnelled
, sizeof tunnelled
))) {
603 addr
+= sizeof mapped
;
604 uaddr
+= sizeof mapped
;
616 __set_errno (EAFNOSUPPORT
);
617 __set_h_errno (NETDB_INTERNAL
);
621 __set_errno (EINVAL
);
622 __set_h_errno (NETDB_INTERNAL
);
627 (void) sprintf(qbuf
, "%u.%u.%u.%u.in-addr.arpa",
635 for (n
= IN6ADDRSZ
- 1; n
>= 0; n
--) {
636 qp
+= SPRINTF((qp
, "%x.%x.",
638 (uaddr
[n
] >> 4) & 0xf));
640 strcpy(qp
, "ip6.int");
645 n
= res_query(qbuf
, C_IN
, T_PTR
, (u_char
*)buf
.buf
, sizeof buf
.buf
);
647 dprintf("res_query failed (%d)\n", n
);
648 if (errno
== ECONNREFUSED
)
649 return (_gethtbyaddr(addr
, len
, af
));
652 if (!(hp
= getanswer(&buf
, n
, qbuf
, T_PTR
)))
653 return (NULL
); /* h_errno was set by getanswer() */
657 * turn off search as the name should be absolute,
658 * 'localhost' should be matched by defnames
660 strncpy(hname2
, hp
->h_name
, MAXDNAME
);
661 hname2
[MAXDNAME
] = '\0';
662 old_options
= _res
.options
;
663 _res
.options
&= ~RES_DNSRCH
;
664 _res
.options
|= RES_DEFNAMES
;
665 if (!(rhp
= gethostbyname(hname2
))) {
666 syslog(LOG_NOTICE
|LOG_AUTH
,
667 "gethostbyaddr: No A record for %s (verifying [%s])",
668 hname2
, inet_ntoa(*((struct in_addr
*)addr
)));
669 _res
.options
= old_options
;
670 __set_h_errno (HOST_NOT_FOUND
);
673 _res
.options
= old_options
;
674 for (haddr
= rhp
->h_addr_list
; *haddr
; haddr
++)
675 if (!memcmp(*haddr
, addr
, INADDRSZ
))
678 syslog(LOG_NOTICE
|LOG_AUTH
,
679 "gethostbyaddr: A record of %s != PTR record [%s]",
680 hname2
, inet_ntoa(*((struct in_addr
*)addr
)));
681 __set_h_errno (HOST_NOT_FOUND
);
685 #endif /*SUNSECURITY*/
688 bcopy(addr
, host_addr
, len
);
689 h_addr_ptrs
[0] = (char *)host_addr
;
690 h_addr_ptrs
[1] = NULL
;
691 if (af
== AF_INET
&& (_res
.options
& RES_USE_INET6
)) {
692 map_v4v6_address((char*)host_addr
, (char*)host_addr
);
693 hp
->h_addrtype
= AF_INET6
;
694 hp
->h_length
= IN6ADDRSZ
;
696 __set_h_errno (NETDB_SUCCESS
);
705 hostf
= fopen(_PATH_HOSTS
, "r" );
714 if (hostf
&& !stayopen
) {
715 (void) fclose(hostf
);
724 register char *cp
, **q
;
727 if (!hostf
&& !(hostf
= fopen(_PATH_HOSTS
, "r" ))) {
728 __set_h_errno (NETDB_INTERNAL
);
732 if (!(p
= fgets(hostbuf
, sizeof hostbuf
, hostf
))) {
733 __set_h_errno (HOST_NOT_FOUND
);
738 if (!(cp
= strpbrk(p
, "#\n")))
741 if (!(cp
= strpbrk(p
, " \t")))
744 if (inet_pton(AF_INET6
, p
, host_addr
) > 0) {
747 } else if (inet_pton(AF_INET
, p
, host_addr
) > 0) {
748 if (_res
.options
& RES_USE_INET6
) {
749 map_v4v6_address((char*)host_addr
, (char*)host_addr
);
759 h_addr_ptrs
[0] = (char *)host_addr
;
760 h_addr_ptrs
[1] = NULL
;
761 host
.h_addr_list
= h_addr_ptrs
;
763 host
.h_addrtype
= af
;
764 while (*cp
== ' ' || *cp
== '\t')
767 q
= host
.h_aliases
= host_aliases
;
768 if (cp
= strpbrk(cp
, " \t"))
771 if (*cp
== ' ' || *cp
== '\t') {
775 if (q
< &host_aliases
[MAXALIASES
- 1])
777 if (cp
= strpbrk(cp
, " \t"))
781 __set_h_errno (NETDB_SUCCESS
);
789 extern struct hostent
*_gethtbyname2();
792 if (_res
.options
& RES_USE_INET6
) {
793 hp
= _gethtbyname2(name
, AF_INET6
);
797 return (_gethtbyname2(name
, AF_INET
));
801 _gethtbyname2(name
, af
)
805 register struct hostent
*p
;
809 while (p
= _gethtent()) {
810 if (p
->h_addrtype
!= af
)
812 if (strcasecmp(p
->h_name
, name
) == 0)
814 for (cp
= p
->h_aliases
; *cp
!= 0; cp
++)
815 if (strcasecmp(*cp
, name
) == 0)
824 _gethtbyaddr(addr
, len
, af
)
828 register struct hostent
*p
;
831 while (p
= _gethtent())
832 if (p
->h_addrtype
== af
&& !bcmp(p
->h_addr
, addr
, len
))
839 map_v4v6_address(src
, dst
)
843 u_char
*p
= (u_char
*)dst
;
847 /* Stash a temporary copy so our caller can update in place. */
848 bcopy(src
, tmp
, INADDRSZ
);
849 /* Mark this ipv6 addr as a mapped ipv4. */
850 for (i
= 0; i
< 10; i
++)
854 /* Retrieve the saved copy and we're done. */
855 bcopy(tmp
, (void*)p
, INADDRSZ
);
859 map_v4v6_hostent(hp
, bpp
, lenp
)
866 if (hp
->h_addrtype
!= AF_INET
|| hp
->h_length
!= INADDRSZ
)
868 hp
->h_addrtype
= AF_INET6
;
869 hp
->h_length
= IN6ADDRSZ
;
870 for (ap
= hp
->h_addr_list
; *ap
; ap
++) {
871 int i
= sizeof(align
) - ((u_long
)*bpp
% sizeof(align
));
873 if (*lenp
< (i
+ IN6ADDRSZ
)) {
874 /* Out of memory. Truncate address list here. XXX */
880 map_v4v6_address(*ap
, *bpp
);
895 short aval
[MAXADDRS
];
899 for (i
= 0; i
< num
; i
++, p
++) {
900 for (j
= 0 ; (unsigned)j
< _res
.nsort
; j
++)
901 if (_res
.sort_list
[j
].addr
.s_addr
==
902 (((struct in_addr
*)(*p
))->s_addr
& _res
.sort_list
[j
].mask
))
905 if (needsort
== 0 && i
> 0 && j
< aval
[i
-1])
911 while (needsort
< num
) {
912 for (j
= needsort
- 1; j
>= 0; j
--) {
913 if (aval
[j
] > aval
[j
+1]) {
932 #if defined(BSD43_BSD43_NFS) || defined(sun)
933 /* some libc's out there are bound internally to these names (UMIPS) */
935 ht_sethostent(stayopen
)
948 ht_gethostbyname(name
)
951 return (_gethtbyname(name
));
955 ht_gethostbyaddr(addr
, len
, af
)
959 return (_gethtbyaddr(addr
, len
, af
));
965 return (_gethtent());
975 dn_skipname(comp_dn
, eom
)
976 const u_char
*comp_dn
, *eom
;
978 return (__dn_skipname(comp_dn
, eom
));
980 #endif /*old-style libc with yp junk in it*/