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/param.h>
62 #include <sys/socket.h>
63 #include <netinet/in.h>
64 #include <arpa/inet.h>
65 #include <arpa/nameser.h>
78 #define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */
80 #if defined(BSD) && (BSD >= 199103)
83 # include "../conf/portability.h"
85 #if defined(USE_OPTIONS_H)
86 # include <../conf/options.h>
92 static const char AskedForGot
[] =
93 "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
95 static char *h_addr_ptrs
[MAXADDRS
+ 1];
96 static struct hostent
*gethostbyname_ipv4
__P((const char *));
98 static struct hostent host
;
99 static char *host_aliases
[MAXALIASES
];
100 static char hostbuf
[8*1024];
101 static struct in_addr host_addr
;
102 static FILE *hostf
= NULL
;
103 static int stayopen
= 0;
106 static void addrsort
__P((char **, int));
110 #define MAXPACKET PACKETSZ
112 #define MAXPACKET 1024
117 u_char buf
[MAXPACKET
];
133 if (_res
.options
& RES_DEBUG
) {
141 # define dprintf(msg, num) /*nada*/
144 static struct hostent
*
145 getanswer(answer
, anslen
, qname
, qclass
, qtype
)
146 const querybuf
*answer
;
151 register const HEADER
*hp
;
152 register const u_char
*cp
;
155 char *bp
, **ap
, **hap
;
156 int type
, class, buflen
, ancount
, qdcount
;
157 int haveanswer
, had_error
;
159 char tbuf
[MAXDNAME
+1];
161 int (*name_ok
) __P((const char *));
165 eom
= answer
->buf
+ anslen
;
171 name_ok
= dn_isvalid
;
177 * find first satisfactory answer
180 ancount
= ntohs(hp
->ancount
);
181 qdcount
= ntohs(hp
->qdcount
);
183 buflen
= sizeof hostbuf
;
184 cp
= answer
->buf
+ HFIXEDSZ
;
186 h_errno
= NO_RECOVERY
;
189 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
190 if ((n
< 0) || !(*name_ok
)(bp
)) {
191 h_errno
= NO_RECOVERY
;
196 /* res_send() has already verified that the query name is the
197 * same as the one we sent; this just gets the expanded name
198 * (i.e., with the succeeding search-domain tacked on).
200 n
= strlen(bp
) + 1; /* for the \0 */
204 /* The qname can be abbreviated, but h_name is now absolute. */
209 host
.h_aliases
= host_aliases
;
212 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
213 host
.h_addr_list
= h_addr_ptrs
;
217 while (ancount
-- > 0 && cp
< eom
&& !had_error
) {
218 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
219 if ((n
< 0) || !(*name_ok
)(bp
)) {
224 type
= _getshort(cp
);
225 cp
+= INT16SZ
; /* type */
226 class = _getshort(cp
);
227 cp
+= INT16SZ
+ INT32SZ
; /* class, TTL */
229 cp
+= INT16SZ
; /* len */
230 if (class != qclass
) {
231 /* XXX - debug? syslog? */
233 continue; /* XXX - had_error++ ? */
235 if (qtype
== T_A
&& type
== T_CNAME
) {
236 if (ap
>= &host_aliases
[MAXALIASES
-1])
238 n
= dn_expand(answer
->buf
, eom
, cp
, tbuf
, sizeof tbuf
);
239 if ((n
< 0) || !(*name_ok
)(tbuf
)) {
244 if (host
.h_name
&& strcasecmp(host
.h_name
, bp
) != 0) {
245 syslog(LOG_NOTICE
|LOG_AUTH
,
246 "gethostby*.getanswer: asked for \"%s\", got CNAME for \"%s\"",
248 continue; /* XXX - had_error++ ? */
252 n
= strlen(bp
) + 1; /* for the \0 */
255 /* Get canonical name. */
256 n
= strlen(tbuf
) + 1; /* for the \0 */
267 if (qtype
== T_PTR
&& type
== T_CNAME
) {
268 n
= dn_expand(answer
->buf
, eom
, cp
, tbuf
, sizeof tbuf
);
269 if ((n
< 0) || !res_hnok(tbuf
)) {
274 /* Get canonical name. */
275 n
= strlen(tbuf
) + 1; /* for the \0 */
287 syslog(LOG_NOTICE
|LOG_AUTH
,
288 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
289 qname
, p_class(qclass
), p_type(qtype
),
292 continue; /* XXX - had_error++ ? */
296 if (strcasecmp(tname
, bp
) != 0) {
297 syslog(LOG_NOTICE
|LOG_AUTH
,
298 AskedForGot
, qname
, bp
);
300 continue; /* XXX - had_error++ ? */
302 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
303 if ((n
< 0) || !res_hnok(bp
)) {
307 #if MULTI_PTRS_ARE_ALIASES
311 else if (ap
< &host_aliases
[MAXALIASES
-1])
316 n
= strlen(bp
) + 1; /* for the \0 */
323 h_errno
= NETDB_SUCCESS
;
327 if (strcasecmp(host
.h_name
, bp
) != 0) {
328 syslog(LOG_NOTICE
|LOG_AUTH
,
329 AskedForGot
, host
.h_name
, bp
);
331 continue; /* XXX - had_error++ ? */
334 if (n
!= host
.h_length
) {
342 host
.h_addrtype
= (class == C_IN
)
346 nn
= strlen(bp
) + 1; /* for the \0 */
351 bp
+= sizeof(align
) - ((u_long
)bp
% sizeof(align
));
353 if (bp
+ n
>= &hostbuf
[sizeof hostbuf
]) {
354 dprintf("size (%d) too big\n", n
);
358 if (hap
>= &h_addr_ptrs
[MAXADDRS
-1]) {
360 dprintf("Too many addresses (%d)\n",
365 bcopy(cp
, *hap
++ = bp
, n
);
370 dprintf("Impossible condition (type=%d)\n", type
);
371 h_errno
= NO_RECOVERY
;
380 # if defined(RESOLVSORT)
382 * Note: we sort even if host can take only one address
383 * in its return structures - should give it the "best"
384 * address in that case, not some random one
386 if (_res
.nsort
&& haveanswer
> 1 &&
387 qclass
== C_IN
&& qtype
== T_A
)
388 addrsort(h_addr_ptrs
, haveanswer
);
389 # endif /*RESOLVSORT*/
390 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
393 host
.h_addr
= h_addr_ptrs
[0];
396 n
= strlen(qname
) + 1; /* for the \0 */
400 h_errno
= NETDB_SUCCESS
;
412 /* Moved #if line to here because declararing HP would lead to a
413 warining. --drepper@gnu */
414 #if defined(AF_INET6) && defined(RES_TRY_INET6)
417 /* #if defined(AF_INET6) && defined(RES_TRY_INET6) */
418 if (_res
.options
& RES_TRY_INET6
) {
419 hp
= gethostbyname2(name
, AF_INET6
);
424 return (gethostbyname2(name
, AF_INET
));
428 gethostbyname2(name
, af
)
434 return (gethostbyname_ipv4(name
));
436 errno
= EAFNOSUPPORT
;
437 h_errno
= NETDB_INTERNAL
;
441 static struct hostent
*
442 gethostbyname_ipv4(name
)
446 register const char *cp
;
448 extern struct hostent
*_gethtbyname();
450 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
451 h_errno
= NETDB_INTERNAL
;
456 * if there aren't any dots, it could be a user-level alias.
457 * this is also done in res_query() since we are not the only
458 * function that looks up host names.
460 if (!strchr(name
, '.') && (cp
= __hostalias(name
)))
464 * disallow names consisting only of digits/dots, unless
467 if (isdigit(name
[0]))
468 for (cp
= name
;; ++cp
) {
473 * All-numeric, no dot at the end.
474 * Fake up a hostent as if we'd actually
477 if (!inet_aton(name
, &host_addr
)) {
478 h_errno
= HOST_NOT_FOUND
;
481 strncpy(hostbuf
, name
, MAXDNAME
);
482 hostbuf
[MAXDNAME
] = '\0';
483 host
.h_name
= hostbuf
;
484 host
.h_aliases
= host_aliases
;
485 host_aliases
[0] = NULL
;
486 host
.h_addrtype
= AF_INET
;
487 host
.h_length
= INT32SZ
;
488 h_addr_ptrs
[0] = (char *)&host_addr
;
489 h_addr_ptrs
[1] = NULL
;
490 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
491 host
.h_addr_list
= h_addr_ptrs
;
493 host
.h_addr
= h_addr_ptrs
[0];
495 h_errno
= NETDB_SUCCESS
;
498 if (!isdigit(*cp
) && *cp
!= '.')
502 if ((n
= res_search(name
, C_IN
, T_A
, buf
.buf
, sizeof(buf
))) < 0) {
503 dprintf("res_search failed (%d)\n", n
);
504 if (errno
== ECONNREFUSED
)
505 return (_gethtbyname(name
));
508 return (getanswer(&buf
, n
, name
, C_IN
, T_A
));
512 gethostbyaddr(addr
, len
, type
)
518 register struct hostent
*hp
;
519 char qbuf
[MAXDNAME
+1];
521 register struct hostent
*rhp
;
524 char hname2
[MAXDNAME
+1];
525 #endif /*SUNSECURITY*/
526 extern struct hostent
*_gethtbyaddr();
528 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
529 h_errno
= NETDB_INTERNAL
;
532 if (type
!= AF_INET
|| len
!= INADDRSZ
) {
533 errno
= EAFNOSUPPORT
;
534 h_errno
= NETDB_INTERNAL
;
537 (void)sprintf(qbuf
, "%u.%u.%u.%u.in-addr.arpa",
538 ((unsigned)addr
[3] & 0xff),
539 ((unsigned)addr
[2] & 0xff),
540 ((unsigned)addr
[1] & 0xff),
541 ((unsigned)addr
[0] & 0xff));
542 n
= res_query(qbuf
, C_IN
, T_PTR
, (u_char
*)buf
.buf
, sizeof buf
.buf
);
544 dprintf("res_query failed (%d)\n", n
);
545 if (errno
== ECONNREFUSED
)
546 return (_gethtbyaddr(addr
, len
, type
));
549 if (!(hp
= getanswer(&buf
, n
, qbuf
, C_IN
, T_PTR
)))
550 return (NULL
); /* h_errno was set by getanswer() */
553 * turn off search as the name should be absolute,
554 * 'localhost' should be matched by defnames
556 strncpy(hname2
, hp
->h_name
, MAXDNAME
);
557 hname2
[MAXDNAME
] = '\0';
558 old_options
= _res
.options
;
559 _res
.options
&= ~RES_DNSRCH
;
560 _res
.options
|= RES_DEFNAMES
;
561 if (!(rhp
= gethostbyname(hname2
))) {
562 syslog(LOG_NOTICE
|LOG_AUTH
,
563 "gethostbyaddr: No A record for %s (verifying [%s])",
564 hname2
, inet_ntoa(*((struct in_addr
*)addr
)));
565 _res
.options
= old_options
;
566 h_errno
= HOST_NOT_FOUND
;
569 _res
.options
= old_options
;
570 for (haddr
= rhp
->h_addr_list
; *haddr
; haddr
++)
571 if (!memcmp(*haddr
, addr
, INADDRSZ
))
574 syslog(LOG_NOTICE
|LOG_AUTH
,
575 "gethostbyaddr: A record of %s != PTR record [%s]",
576 hname2
, inet_ntoa(*((struct in_addr
*)addr
)));
577 h_errno
= HOST_NOT_FOUND
;
580 #endif /*SUNSECURITY*/
581 hp
->h_addrtype
= type
;
583 h_addr_ptrs
[0] = (char *)&host_addr
;
584 h_addr_ptrs
[1] = NULL
;
585 host_addr
= *(struct in_addr
*)addr
;
586 h_errno
= NETDB_SUCCESS
;
595 hostf
= fopen(_PATH_HOSTS
, "r" );
604 if (hostf
&& !stayopen
) {
605 (void) fclose(hostf
);
614 register char *cp
, **q
;
616 if (!hostf
&& !(hostf
= fopen(_PATH_HOSTS
, "r" ))) {
617 h_errno
= NETDB_INTERNAL
;
621 if (!(p
= fgets(hostbuf
, sizeof hostbuf
, hostf
))) {
622 h_errno
= HOST_NOT_FOUND
;
627 if (!(cp
= strpbrk(p
, "#\n")))
630 if (!(cp
= strpbrk(p
, " \t")))
633 /* THIS STUFF IS INTERNET SPECIFIC */
634 if (!inet_aton(p
, &host_addr
))
636 h_addr_ptrs
[0] = (char *)&host_addr
;
637 h_addr_ptrs
[1] = NULL
;
638 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
639 host
.h_addr_list
= h_addr_ptrs
;
641 host
.h_addr
= h_addr_ptrs
[0];
643 host
.h_length
= INT32SZ
;
644 host
.h_addrtype
= AF_INET
;
645 while (*cp
== ' ' || *cp
== '\t')
648 q
= host
.h_aliases
= host_aliases
;
649 if (cp
= strpbrk(cp
, " \t"))
652 if (*cp
== ' ' || *cp
== '\t') {
656 if (q
< &host_aliases
[MAXALIASES
- 1])
658 if (cp
= strpbrk(cp
, " \t"))
662 h_errno
= NETDB_SUCCESS
;
670 register struct hostent
*p
;
674 while (p
= _gethtent()) {
675 if (strcasecmp(p
->h_name
, name
) == 0)
677 for (cp
= p
->h_aliases
; *cp
!= 0; cp
++)
678 if (strcasecmp(*cp
, name
) == 0)
687 _gethtbyaddr(addr
, len
, type
)
691 register struct hostent
*p
;
694 while (p
= _gethtent())
695 if (p
->h_addrtype
== type
&& !bcmp(p
->h_addr
, addr
, len
))
709 short aval
[MAXADDRS
];
713 for (i
= 0; i
< num
; i
++, p
++) {
714 for (j
= 0 ; (unsigned)j
< _res
.nsort
; j
++)
715 if (_res
.sort_list
[j
].addr
.s_addr
==
716 (((struct in_addr
*)(*p
))->s_addr
& _res
.sort_list
[j
].mask
))
719 if (needsort
== 0 && i
> 0 && j
< aval
[i
-1])
725 while (needsort
< num
) {
726 for (j
= needsort
- 1; j
>= 0; j
--) {
727 if (aval
[j
] > aval
[j
+1]) {
746 #if defined(BSD43_BSD43_NFS) || defined(sun)
747 /* some libc's out there are bound internally to these names (UMIPS) */
749 ht_sethostent(stayopen
)
762 ht_gethostbyname(name
)
765 return (_gethtbyname(name
));
769 ht_gethostbyaddr(addr
, len
, type
)
773 return (_gethtbyaddr(addr
, len
, type
));
779 return (_gethtent());
789 dn_skipname(comp_dn
, eom
)
790 const u_char
*comp_dn
, *eom
;
792 return (__dn_skipname(comp_dn
, eom
));
794 #endif /*old-style libc with yp junk in it*/