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",
395 bcopy(cp
, *hap
++ = bp
, n
);
409 # if defined(RESOLVSORT)
411 * Note: we sort even if host can take only one address
412 * in its return structures - should give it the "best"
413 * address in that case, not some random one
415 if (_res
.nsort
&& haveanswer
> 1 && qtype
== T_A
)
416 addrsort(h_addr_ptrs
, haveanswer
);
417 # endif /*RESOLVSORT*/
419 n
= strlen(qname
) + 1; /* for the \0 */
420 if (n
> buflen
|| n
>= MAXHOSTNAMELEN
)
427 if (_res
.options
& RES_USE_INET6
)
428 map_v4v6_hostent(&host
, &bp
, &buflen
);
429 __set_h_errno (NETDB_SUCCESS
);
433 __set_h_errno (NO_RECOVERY
);
443 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
444 __set_h_errno (NETDB_INTERNAL
);
447 if (_res
.options
& RES_USE_INET6
) {
448 hp
= gethostbyname2(name
, AF_INET6
);
452 return (gethostbyname2(name
, AF_INET
));
456 gethostbyname2(name
, af
)
461 register const char *cp
;
463 int n
, size
, type
, len
;
464 extern struct hostent
*_gethtbyname2();
466 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
467 __set_h_errno (NETDB_INTERNAL
);
481 __set_h_errno (NETDB_INTERNAL
);
482 __set_errno (EAFNOSUPPORT
);
486 host
.h_addrtype
= af
;
487 host
.h_length
= size
;
490 * if there aren't any dots, it could be a user-level alias.
491 * this is also done in res_query() since we are not the only
492 * function that looks up host names.
494 if (!strchr(name
, '.') && (cp
= __hostalias(name
)))
498 * disallow names consisting only of digits/dots, unless
501 if (isdigit(name
[0]))
502 for (cp
= name
;; ++cp
) {
507 * All-numeric, no dot at the end.
508 * Fake up a hostent as if we'd actually
511 if (inet_pton(af
, name
, host_addr
) <= 0) {
512 __set_h_errno (HOST_NOT_FOUND
);
515 strncpy(hostbuf
, name
, MAXDNAME
);
516 hostbuf
[MAXDNAME
] = '\0';
517 bp
= hostbuf
+ MAXDNAME
;
518 len
= sizeof hostbuf
- MAXDNAME
;
519 host
.h_name
= hostbuf
;
520 host
.h_aliases
= host_aliases
;
521 host_aliases
[0] = NULL
;
522 h_addr_ptrs
[0] = (char *)host_addr
;
523 h_addr_ptrs
[1] = NULL
;
524 host
.h_addr_list
= h_addr_ptrs
;
525 if (_res
.options
& RES_USE_INET6
)
526 map_v4v6_hostent(&host
, &bp
, &len
);
527 __set_h_errno (NETDB_SUCCESS
);
530 if (!isdigit(*cp
) && *cp
!= '.')
533 if ((isxdigit(name
[0]) && strchr(name
, ':') != NULL
) ||
535 for (cp
= name
;; ++cp
) {
540 * All-IPv6-legal, no dot at the end.
541 * Fake up a hostent as if we'd actually
544 if (inet_pton(af
, name
, host_addr
) <= 0) {
545 __set_h_errno (HOST_NOT_FOUND
);
548 strncpy(hostbuf
, name
, MAXDNAME
);
549 hostbuf
[MAXDNAME
] = '\0';
550 bp
= hostbuf
+ MAXDNAME
;
551 len
= sizeof hostbuf
- MAXDNAME
;
552 host
.h_name
= hostbuf
;
553 host
.h_aliases
= host_aliases
;
554 host_aliases
[0] = NULL
;
555 h_addr_ptrs
[0] = (char *)host_addr
;
556 h_addr_ptrs
[1] = NULL
;
557 host
.h_addr_list
= h_addr_ptrs
;
558 __set_h_errno (NETDB_SUCCESS
);
561 if (!isxdigit(*cp
) && *cp
!= ':' && *cp
!= '.')
565 if ((n
= res_search(name
, C_IN
, type
, buf
.buf
, sizeof(buf
.buf
))) < 0) {
566 dprintf("res_search failed (%d)\n", n
);
567 if (errno
== ECONNREFUSED
)
568 return (_gethtbyname2(name
, af
));
571 return (getanswer(&buf
, n
, name
, type
));
575 gethostbyaddr(addr
, len
, af
)
576 const char *addr
; /* XXX should have been def'd as u_char! */
579 const u_char
*uaddr
= (const u_char
*)addr
;
580 static const u_char mapped
[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
581 static const u_char tunnelled
[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
584 register struct hostent
*hp
;
585 char qbuf
[MAXDNAME
+1], *qp
;
587 register struct hostent
*rhp
;
590 char hname2
[MAXDNAME
+1];
591 #endif /*SUNSECURITY*/
592 extern struct hostent
*_gethtbyaddr();
594 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
595 __set_h_errno (NETDB_INTERNAL
);
598 if (af
== AF_INET6
&& len
== IN6ADDRSZ
&&
599 (!bcmp(uaddr
, mapped
, sizeof mapped
) ||
600 !bcmp(uaddr
, tunnelled
, sizeof tunnelled
))) {
602 addr
+= sizeof mapped
;
603 uaddr
+= sizeof mapped
;
615 __set_errno (EAFNOSUPPORT
);
616 __set_h_errno (NETDB_INTERNAL
);
620 __set_errno (EINVAL
);
621 __set_h_errno (NETDB_INTERNAL
);
626 (void) sprintf(qbuf
, "%u.%u.%u.%u.in-addr.arpa",
634 for (n
= IN6ADDRSZ
- 1; n
>= 0; n
--) {
635 qp
+= SPRINTF((qp
, "%x.%x.",
637 (uaddr
[n
] >> 4) & 0xf));
639 strcpy(qp
, "ip6.int");
644 n
= res_query(qbuf
, C_IN
, T_PTR
, (u_char
*)buf
.buf
, sizeof buf
.buf
);
646 dprintf("res_query failed (%d)\n", n
);
647 if (errno
== ECONNREFUSED
)
648 return (_gethtbyaddr(addr
, len
, af
));
651 if (!(hp
= getanswer(&buf
, n
, qbuf
, T_PTR
)))
652 return (NULL
); /* h_errno was set by getanswer() */
656 * turn off search as the name should be absolute,
657 * 'localhost' should be matched by defnames
659 strncpy(hname2
, hp
->h_name
, MAXDNAME
);
660 hname2
[MAXDNAME
] = '\0';
661 old_options
= _res
.options
;
662 _res
.options
&= ~RES_DNSRCH
;
663 _res
.options
|= RES_DEFNAMES
;
664 if (!(rhp
= gethostbyname(hname2
))) {
665 syslog(LOG_NOTICE
|LOG_AUTH
,
666 "gethostbyaddr: No A record for %s (verifying [%s])",
667 hname2
, inet_ntoa(*((struct in_addr
*)addr
)));
668 _res
.options
= old_options
;
669 __set_h_errno (HOST_NOT_FOUND
);
672 _res
.options
= old_options
;
673 for (haddr
= rhp
->h_addr_list
; *haddr
; haddr
++)
674 if (!memcmp(*haddr
, addr
, INADDRSZ
))
677 syslog(LOG_NOTICE
|LOG_AUTH
,
678 "gethostbyaddr: A record of %s != PTR record [%s]",
679 hname2
, inet_ntoa(*((struct in_addr
*)addr
)));
680 __set_h_errno (HOST_NOT_FOUND
);
684 #endif /*SUNSECURITY*/
687 bcopy(addr
, host_addr
, len
);
688 h_addr_ptrs
[0] = (char *)host_addr
;
689 h_addr_ptrs
[1] = NULL
;
690 if (af
== AF_INET
&& (_res
.options
& RES_USE_INET6
)) {
691 map_v4v6_address((char*)host_addr
, (char*)host_addr
);
692 hp
->h_addrtype
= AF_INET6
;
693 hp
->h_length
= IN6ADDRSZ
;
695 __set_h_errno (NETDB_SUCCESS
);
704 hostf
= fopen(_PATH_HOSTS
, "r" );
713 if (hostf
&& !stayopen
) {
714 (void) fclose(hostf
);
723 register char *cp
, **q
;
726 if (!hostf
&& !(hostf
= fopen(_PATH_HOSTS
, "r" ))) {
727 __set_h_errno (NETDB_INTERNAL
);
731 if (!(p
= fgets(hostbuf
, sizeof hostbuf
, hostf
))) {
732 __set_h_errno (HOST_NOT_FOUND
);
737 if (!(cp
= strpbrk(p
, "#\n")))
740 if (!(cp
= strpbrk(p
, " \t")))
743 if (inet_pton(AF_INET6
, p
, host_addr
) > 0) {
746 } else if (inet_pton(AF_INET
, p
, host_addr
) > 0) {
747 if (_res
.options
& RES_USE_INET6
) {
748 map_v4v6_address((char*)host_addr
, (char*)host_addr
);
758 h_addr_ptrs
[0] = (char *)host_addr
;
759 h_addr_ptrs
[1] = NULL
;
760 host
.h_addr_list
= h_addr_ptrs
;
762 host
.h_addrtype
= af
;
763 while (*cp
== ' ' || *cp
== '\t')
766 q
= host
.h_aliases
= host_aliases
;
767 if (cp
= strpbrk(cp
, " \t"))
770 if (*cp
== ' ' || *cp
== '\t') {
774 if (q
< &host_aliases
[MAXALIASES
- 1])
776 if (cp
= strpbrk(cp
, " \t"))
780 __set_h_errno (NETDB_SUCCESS
);
788 extern struct hostent
*_gethtbyname2();
791 if (_res
.options
& RES_USE_INET6
) {
792 hp
= _gethtbyname2(name
, AF_INET6
);
796 return (_gethtbyname2(name
, AF_INET
));
800 _gethtbyname2(name
, af
)
804 register struct hostent
*p
;
808 while (p
= _gethtent()) {
809 if (p
->h_addrtype
!= af
)
811 if (strcasecmp(p
->h_name
, name
) == 0)
813 for (cp
= p
->h_aliases
; *cp
!= 0; cp
++)
814 if (strcasecmp(*cp
, name
) == 0)
823 _gethtbyaddr(addr
, len
, af
)
827 register struct hostent
*p
;
830 while (p
= _gethtent())
831 if (p
->h_addrtype
== af
&& !bcmp(p
->h_addr
, addr
, len
))
838 map_v4v6_address(src
, dst
)
842 u_char
*p
= (u_char
*)dst
;
846 /* Stash a temporary copy so our caller can update in place. */
847 bcopy(src
, tmp
, INADDRSZ
);
848 /* Mark this ipv6 addr as a mapped ipv4. */
849 for (i
= 0; i
< 10; i
++)
853 /* Retrieve the saved copy and we're done. */
854 bcopy(tmp
, (void*)p
, INADDRSZ
);
858 map_v4v6_hostent(hp
, bpp
, lenp
)
865 if (hp
->h_addrtype
!= AF_INET
|| hp
->h_length
!= INADDRSZ
)
867 hp
->h_addrtype
= AF_INET6
;
868 hp
->h_length
= IN6ADDRSZ
;
869 for (ap
= hp
->h_addr_list
; *ap
; ap
++) {
870 int i
= sizeof(align
) - ((u_long
)*bpp
% sizeof(align
));
872 if (*lenp
< (i
+ IN6ADDRSZ
)) {
873 /* Out of memory. Truncate address list here. XXX */
879 map_v4v6_address(*ap
, *bpp
);
894 short aval
[MAXADDRS
];
898 for (i
= 0; i
< num
; i
++, p
++) {
899 for (j
= 0 ; (unsigned)j
< _res
.nsort
; j
++)
900 if (_res
.sort_list
[j
].addr
.s_addr
==
901 (((struct in_addr
*)(*p
))->s_addr
& _res
.sort_list
[j
].mask
))
904 if (needsort
== 0 && i
> 0 && j
< aval
[i
-1])
910 while (needsort
< num
) {
911 for (j
= needsort
- 1; j
>= 0; j
--) {
912 if (aval
[j
] > aval
[j
+1]) {
931 #if defined(BSD43_BSD43_NFS) || defined(sun)
932 /* some libc's out there are bound internally to these names (UMIPS) */
934 ht_sethostent(stayopen
)
947 ht_gethostbyname(name
)
950 return (_gethtbyname(name
));
954 ht_gethostbyaddr(addr
, len
, af
)
958 return (_gethtbyaddr(addr
, len
, af
));
964 return (_gethtent());
974 dn_skipname(comp_dn
, eom
)
975 const u_char
*comp_dn
, *eom
;
977 return (__dn_skipname(comp_dn
, eom
));
979 #endif /*old-style libc with yp junk in it*/