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];
164 eom
= answer
->buf
+ anslen
;
166 * find first satisfactory answer
169 ancount
= ntohs(hp
->ancount
);
170 qdcount
= ntohs(hp
->qdcount
);
172 buflen
= sizeof hostbuf
;
173 cp
= answer
->buf
+ HFIXEDSZ
;
175 h_errno
= NO_RECOVERY
;
178 if ((n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
)) < 0) {
179 h_errno
= NO_RECOVERY
;
184 /* res_send() has already verified that the query name is the
185 * same as the one we sent; this just gets the expanded name
186 * (i.e., with the succeeding search-domain tacked on).
188 n
= strlen(bp
) + 1; /* for the \0 */
192 /* The qname can be abbreviated, but h_name is now absolute. */
197 host
.h_aliases
= host_aliases
;
200 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
201 host
.h_addr_list
= h_addr_ptrs
;
205 while (ancount
-- > 0 && cp
< eom
&& !had_error
) {
206 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
212 type
= _getshort(cp
);
213 cp
+= INT16SZ
; /* type */
214 class = _getshort(cp
);
215 cp
+= INT16SZ
+ INT32SZ
; /* class, TTL */
217 cp
+= INT16SZ
; /* len */
218 if (class != qclass
) {
219 /* XXX - debug? syslog? */
221 continue; /* XXX - had_error++ ? */
223 if (qtype
== T_A
&& type
== T_CNAME
) {
224 if (ap
>= &host_aliases
[MAXALIASES
-1])
226 n
= dn_expand(answer
->buf
, eom
, cp
, tbuf
, sizeof tbuf
);
232 if (host
.h_name
&& strcasecmp(host
.h_name
, bp
) != 0) {
233 syslog(LOG_NOTICE
|LOG_AUTH
,
234 "gethostby*.getanswer: asked for \"%s\", got CNAME for \"%s\"",
236 continue; /* XXX - had_error++ ? */
240 n
= strlen(bp
) + 1; /* for the \0 */
243 /* Get canonical name. */
244 n
= strlen(tbuf
) + 1; /* for the \0 */
255 if (qtype
== T_PTR
&& type
== T_CNAME
) {
256 n
= dn_expand(answer
->buf
, eom
, cp
, tbuf
, sizeof tbuf
);
262 /* Get canonical name. */
263 n
= strlen(tbuf
) + 1; /* for the \0 */
275 syslog(LOG_NOTICE
|LOG_AUTH
,
276 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
277 qname
, p_class(qclass
), p_type(qtype
),
280 continue; /* XXX - had_error++ ? */
284 if (strcasecmp(tname
, bp
) != 0) {
285 syslog(LOG_NOTICE
|LOG_AUTH
,
286 AskedForGot
, qname
, bp
);
288 continue; /* XXX - had_error++ ? */
290 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
295 #if MULTI_PTRS_ARE_ALIASES
299 else if (ap
< &host_aliases
[MAXALIASES
-1])
304 n
= strlen(bp
) + 1; /* for the \0 */
311 h_errno
= NETDB_SUCCESS
;
315 if (strcasecmp(host
.h_name
, bp
) != 0) {
316 syslog(LOG_NOTICE
|LOG_AUTH
,
317 AskedForGot
, host
.h_name
, bp
);
319 continue; /* XXX - had_error++ ? */
322 if (n
!= host
.h_length
) {
330 host
.h_addrtype
= (class == C_IN
)
334 nn
= strlen(bp
) + 1; /* for the \0 */
339 bp
+= sizeof(align
) - ((u_long
)bp
% sizeof(align
));
341 if (bp
+ n
>= &hostbuf
[sizeof hostbuf
]) {
342 dprintf("size (%d) too big\n", n
);
346 if (hap
>= &h_addr_ptrs
[MAXADDRS
-1]) {
348 dprintf("Too many addresses (%d)\n",
353 bcopy(cp
, *hap
++ = bp
, n
);
358 dprintf("Impossible condition (type=%d)\n", type
);
359 h_errno
= NO_RECOVERY
;
368 # if defined(RESOLVSORT)
370 * Note: we sort even if host can take only one address
371 * in its return structures - should give it the "best"
372 * address in that case, not some random one
374 if (_res
.nsort
&& haveanswer
> 1 &&
375 qclass
== C_IN
&& qtype
== T_A
)
376 addrsort(h_addr_ptrs
, haveanswer
);
377 # endif /*RESOLVSORT*/
378 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
381 host
.h_addr
= h_addr_ptrs
[0];
384 n
= strlen(qname
) + 1; /* for the \0 */
388 h_errno
= NETDB_SUCCESS
;
402 #if defined(AF_INET6) && defined(RES_TRY_INET6)
403 if (_res
.options
& RES_TRY_INET6
) {
404 hp
= gethostbyname2(name
, AF_INET6
);
409 return (gethostbyname2(name
, AF_INET
));
413 gethostbyname2(name
, af
)
419 return (gethostbyname_ipv4(name
));
421 errno
= EAFNOSUPPORT
;
422 h_errno
= NETDB_INTERNAL
;
426 static struct hostent
*
427 gethostbyname_ipv4(name
)
431 register const char *cp
;
433 extern struct hostent
*_gethtbyname();
435 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
436 h_errno
= NETDB_INTERNAL
;
441 * if there aren't any dots, it could be a user-level alias.
442 * this is also done in res_query() since we are not the only
443 * function that looks up host names.
445 if (!strchr(name
, '.') && (cp
= __hostalias(name
)))
449 * disallow names consisting only of digits/dots, unless
452 if (isdigit(name
[0]))
453 for (cp
= name
;; ++cp
) {
458 * All-numeric, no dot at the end.
459 * Fake up a hostent as if we'd actually
462 if (!inet_aton(name
, &host_addr
)) {
463 h_errno
= HOST_NOT_FOUND
;
466 strncpy(hostbuf
, name
, MAXDNAME
);
467 hostbuf
[MAXDNAME
] = '\0';
468 host
.h_name
= hostbuf
;
469 host
.h_aliases
= host_aliases
;
470 host_aliases
[0] = NULL
;
471 host
.h_addrtype
= AF_INET
;
472 host
.h_length
= INT32SZ
;
473 h_addr_ptrs
[0] = (char *)&host_addr
;
474 h_addr_ptrs
[1] = NULL
;
475 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
476 host
.h_addr_list
= h_addr_ptrs
;
478 host
.h_addr
= h_addr_ptrs
[0];
480 h_errno
= NETDB_SUCCESS
;
483 if (!isdigit(*cp
) && *cp
!= '.')
487 if ((n
= res_search(name
, C_IN
, T_A
, buf
.buf
, sizeof(buf
))) < 0) {
488 dprintf("res_search failed (%d)\n", n
);
489 if (errno
== ECONNREFUSED
)
490 return (_gethtbyname(name
));
493 return (getanswer(&buf
, n
, name
, C_IN
, T_A
));
497 gethostbyaddr(addr
, len
, type
)
503 register struct hostent
*hp
;
504 char qbuf
[MAXDNAME
+1];
506 register struct hostent
*rhp
;
509 char hname2
[MAXDNAME
+1];
510 #endif /*SUNSECURITY*/
511 extern struct hostent
*_gethtbyaddr();
513 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
514 h_errno
= NETDB_INTERNAL
;
517 if (type
!= AF_INET
) {
518 errno
= EAFNOSUPPORT
;
519 h_errno
= NETDB_INTERNAL
;
522 (void)sprintf(qbuf
, "%u.%u.%u.%u.in-addr.arpa",
523 ((unsigned)addr
[3] & 0xff),
524 ((unsigned)addr
[2] & 0xff),
525 ((unsigned)addr
[1] & 0xff),
526 ((unsigned)addr
[0] & 0xff));
527 n
= res_query(qbuf
, C_IN
, T_PTR
, (u_char
*)buf
.buf
, sizeof buf
.buf
);
529 dprintf("res_query failed (%d)\n", n
);
530 if (errno
== ECONNREFUSED
)
531 return (_gethtbyaddr(addr
, len
, type
));
534 if (!(hp
= getanswer(&buf
, n
, qbuf
, C_IN
, T_PTR
)))
535 return (NULL
); /* h_errno was set by getanswer() */
538 * turn off search as the name should be absolute,
539 * 'localhost' should be matched by defnames
541 strncpy(hname2
, hp
->h_name
, MAXDNAME
);
542 hname2
[MAXDNAME
] = '\0';
543 old_options
= _res
.options
;
544 _res
.options
&= ~RES_DNSRCH
;
545 _res
.options
|= RES_DEFNAMES
;
546 if (!(rhp
= gethostbyname(hname2
))) {
547 syslog(LOG_NOTICE
|LOG_AUTH
,
548 "gethostbyaddr: No A record for %s (verifying [%s])",
549 hname2
, inet_ntoa(*((struct in_addr
*)addr
)));
550 _res
.options
= old_options
;
551 h_errno
= HOST_NOT_FOUND
;
554 _res
.options
= old_options
;
555 for (haddr
= rhp
->h_addr_list
; *haddr
; haddr
++)
556 if (!memcmp(*haddr
, addr
, INADDRSZ
))
559 syslog(LOG_NOTICE
|LOG_AUTH
,
560 "gethostbyaddr: A record of %s != PTR record [%s]",
561 hname2
, inet_ntoa(*((struct in_addr
*)addr
)));
562 h_errno
= HOST_NOT_FOUND
;
565 #endif /*SUNSECURITY*/
566 hp
->h_addrtype
= type
;
568 h_addr_ptrs
[0] = (char *)&host_addr
;
569 h_addr_ptrs
[1] = NULL
;
570 host_addr
= *(struct in_addr
*)addr
;
571 h_errno
= NETDB_SUCCESS
;
580 hostf
= fopen(_PATH_HOSTS
, "r" );
589 if (hostf
&& !stayopen
) {
590 (void) fclose(hostf
);
599 register char *cp
, **q
;
601 if (!hostf
&& !(hostf
= fopen(_PATH_HOSTS
, "r" ))) {
602 h_errno
= NETDB_INTERNAL
;
606 if (!(p
= fgets(hostbuf
, sizeof hostbuf
, hostf
))) {
607 h_errno
= HOST_NOT_FOUND
;
612 if (!(cp
= strpbrk(p
, "#\n")))
615 if (!(cp
= strpbrk(p
, " \t")))
618 /* THIS STUFF IS INTERNET SPECIFIC */
619 if (!inet_aton(p
, &host_addr
))
621 h_addr_ptrs
[0] = (char *)&host_addr
;
622 h_addr_ptrs
[1] = NULL
;
623 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
624 host
.h_addr_list
= h_addr_ptrs
;
626 host
.h_addr
= h_addr_ptrs
[0];
628 host
.h_length
= INT32SZ
;
629 host
.h_addrtype
= AF_INET
;
630 while (*cp
== ' ' || *cp
== '\t')
633 q
= host
.h_aliases
= host_aliases
;
634 if (cp
= strpbrk(cp
, " \t"))
637 if (*cp
== ' ' || *cp
== '\t') {
641 if (q
< &host_aliases
[MAXALIASES
- 1])
643 if (cp
= strpbrk(cp
, " \t"))
647 h_errno
= NETDB_SUCCESS
;
655 register struct hostent
*p
;
659 while (p
= _gethtent()) {
660 if (strcasecmp(p
->h_name
, name
) == 0)
662 for (cp
= p
->h_aliases
; *cp
!= 0; cp
++)
663 if (strcasecmp(*cp
, name
) == 0)
672 _gethtbyaddr(addr
, len
, type
)
676 register struct hostent
*p
;
679 while (p
= _gethtent())
680 if (p
->h_addrtype
== type
&& !bcmp(p
->h_addr
, addr
, len
))
694 short aval
[MAXADDRS
];
698 for (i
= 0; i
< num
; i
++, p
++) {
699 for (j
= 0 ; (unsigned)j
< _res
.nsort
; j
++)
700 if (_res
.sort_list
[j
].addr
.s_addr
==
701 (((struct in_addr
*)(*p
))->s_addr
& _res
.sort_list
[j
].mask
))
704 if (needsort
== 0 && i
> 0 && j
< aval
[i
-1])
710 while (needsort
< num
) {
711 for (j
= needsort
- 1; j
>= 0; j
--) {
712 if (aval
[j
] > aval
[j
+1]) {
731 #if defined(BSD43_BSD43_NFS) || defined(sun)
732 /* some libc's out there are bound internally to these names (UMIPS) */
734 ht_sethostent(stayopen
)
747 ht_gethostbyname(name
)
750 return (_gethtbyname(name
));
754 ht_gethostbyaddr(addr
, len
, type
)
758 return (_gethtbyaddr(addr
, len
, type
));
764 return (_gethtent());
774 dn_skipname(comp_dn
, eom
)
775 const u_char
*comp_dn
, *eom
;
777 return (__dn_skipname(comp_dn
, eom
));
779 #endif /*old-style libc with yp junk in it*/