2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * ++Copyright++ 1985, 1988, 1993
28 * Copyright (c) 1985, 1988, 1993
29 * The Regents of the University of California. All rights reserved.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
61 * Permission to use, copy, modify, and distribute this software for any
62 * purpose with or without fee is hereby granted, provided that the above
63 * copyright notice and this permission notice appear in all copies, and that
64 * the name of Digital Equipment Corporation not be used in advertising or
65 * publicity pertaining to distribution of the document or software without
66 * specific, written prior permission.
68 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
69 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
70 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
71 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
72 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
73 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
74 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
80 #if defined(LIBC_SCCS) && !defined(lint)
81 static char sccsid
[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
82 static char rcsid
[] = "$Id: gethnamaddr.c,v 1.8 2003/07/03 21:56:17 majka Exp $";
83 #endif /* LIBC_SCCS and not lint */
85 #include <sys/param.h>
86 #include <sys/socket.h>
87 #include <netinet/in.h>
88 #include <arpa/inet.h>
89 #include <arpa/nameser8_compat.h>
94 #include <resolv8_compat.h>
103 #define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */
105 #if defined(BSD) && (BSD >= 199103)
108 # include "portability.h"
110 #if defined(USE_OPTIONS_H)
111 # include "options.h"
114 #define MAXALIASES 35
116 #define MAXHOSTBUF 8*1024
118 static const char AskedForGot
[] =
119 "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
121 static char *h_addr_ptrs
[MAXADDRS
+ 1];
122 static struct hostent
*gethostbyname_ipv4
__P((const char *));
124 static struct hostent host
;
125 static char *host_aliases
[MAXALIASES
];
126 static char *hostbuf
= NULL
;
127 static struct in_addr host_addr
;
128 static FILE *hostf
= NULL
;
129 static int stayopen
= 0;
132 static void addrsort
__P((char **, int));
136 #define MAXPACKET PACKETSZ
138 #define MAXPACKET 1024
143 u_char buf
[MAXPACKET
];
153 extern int _lu_running(void);
161 if (_res
.options
& RES_DEBUG
) {
169 # define dprintf(msg, num) /*nada*/
172 static struct hostent
*
173 getanswer(answer
, anslen
, qname
, qclass
, qtype
)
174 const querybuf
*answer
;
179 register const HEADER
*hp
;
180 register const u_char
*cp
;
183 char *bp
, **ap
, **hap
;
184 int type
, class, buflen
, ancount
, qdcount
;
185 int haveanswer
, had_error
;
187 char tbuf
[MAXDNAME
+1];
190 if (hostbuf
== NULL
) {
191 hostbuf
= malloc(MAXHOSTBUF
);
199 eom
= answer
->buf
+ anslen
;
201 * find first satisfactory answer
204 ancount
= ntohs(hp
->ancount
);
205 qdcount
= ntohs(hp
->qdcount
);
207 cp
= answer
->buf
+ HFIXEDSZ
;
209 h_errno
= NO_RECOVERY
;
212 if ((n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
)) < 0) {
213 h_errno
= NO_RECOVERY
;
218 /* res_send() has already verified that the query name is the
219 * same as the one we sent; this just gets the expanded name
220 * (i.e., with the succeeding search-domain tacked on).
222 n
= strlen(bp
) + 1; /* for the \0 */
226 /* The qname can be abbreviated, but h_name is now absolute. */
231 host
.h_aliases
= host_aliases
;
234 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
235 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
);
246 type
= _getshort(cp
);
247 cp
+= INT16SZ
; /* type */
248 class = _getshort(cp
);
249 cp
+= INT16SZ
+ INT32SZ
; /* class, TTL */
251 cp
+= INT16SZ
; /* len */
252 if (class != qclass
) {
253 /* XXX - debug? syslog? */
255 continue; /* XXX - had_error++ ? */
257 if (qtype
== T_A
&& type
== T_CNAME
) {
258 if (ap
>= &host_aliases
[MAXALIASES
-1])
260 n
= dn_expand(answer
->buf
, eom
, cp
, tbuf
, sizeof tbuf
);
266 if (host
.h_name
&& strcasecmp(host
.h_name
, bp
) != 0) {
267 syslog(LOG_NOTICE
|LOG_AUTH
,
268 "gethostby*.getanswer: asked for \"%s\", got CNAME for \"%s\"",
270 continue; /* XXX - had_error++ ? */
274 n
= strlen(bp
) + 1; /* for the \0 */
277 /* Get canonical name. */
278 n
= strlen(tbuf
) + 1; /* for the \0 */
289 if (qtype
== T_PTR
&& type
== T_CNAME
) {
290 n
= dn_expand(answer
->buf
, eom
, cp
, tbuf
, sizeof tbuf
);
296 /* Get canonical name. */
297 n
= strlen(tbuf
) + 1; /* for the \0 */
309 syslog(LOG_NOTICE
|LOG_AUTH
,
310 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
311 qname
, p_class(qclass
), p_type(qtype
),
314 continue; /* XXX - had_error++ ? */
318 if (strcasecmp(tname
, bp
) != 0) {
319 syslog(LOG_NOTICE
|LOG_AUTH
,
320 AskedForGot
, qname
, bp
);
322 continue; /* XXX - had_error++ ? */
324 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
329 #if MULTI_PTRS_ARE_ALIASES
333 else if (ap
< &host_aliases
[MAXALIASES
-1])
338 n
= strlen(bp
) + 1; /* for the \0 */
345 h_errno
= NETDB_SUCCESS
;
349 if (strcasecmp(host
.h_name
, bp
) != 0) {
350 syslog(LOG_NOTICE
|LOG_AUTH
,
351 AskedForGot
, host
.h_name
, bp
);
353 continue; /* XXX - had_error++ ? */
356 if (n
!= host
.h_length
) {
364 host
.h_addrtype
= (class == C_IN
)
368 nn
= strlen(bp
) + 1; /* for the \0 */
373 bp
+= sizeof(align
) - ((u_long
)bp
% sizeof(align
));
375 if (bp
+ n
>= &hostbuf
[MAXHOSTBUF
]) {
376 dprintf("size (%d) too big\n", n
);
380 if (hap
>= &h_addr_ptrs
[MAXADDRS
-1]) {
382 dprintf("Too many addresses (%d)\n",
387 bcopy(cp
, *hap
++ = bp
, n
);
392 dprintf("Impossible condition (type=%d)\n", type
);
393 h_errno
= NO_RECOVERY
;
402 # if defined(RESOLVSORT)
404 * Note: we sort even if host can take only one address
405 * in its return structures - should give it the "best"
406 * address in that case, not some random one
408 if (_res
.nsort
&& haveanswer
> 1 &&
409 qclass
== C_IN
&& qtype
== T_A
)
410 addrsort(h_addr_ptrs
, haveanswer
);
411 # endif /*RESOLVSORT*/
412 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
415 host
.h_addr
= h_addr_ptrs
[0];
418 n
= strlen(qname
) + 1; /* for the \0 */
422 h_errno
= NETDB_SUCCESS
;
434 #if defined(AF_INET6) && defined(RES_TRY_INET6)
437 if (_res
.options
& RES_TRY_INET6
) {
438 hp
= gethostbyname2(name
, AF_INET6
);
443 return (gethostbyname2(name
, AF_INET
));
446 static struct hostent
*
447 gethostbyname_ipv4(name
)
451 register const char *cp
;
453 #if !defined(__APPLE__)
454 extern struct hostent
*_gethtbyname();
459 hostbuf
= malloc(MAXHOSTBUF
);
460 if (hostbuf
== NULL
) {
461 h_errno
= NETDB_INTERNAL
;
466 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
467 h_errno
= NETDB_INTERNAL
;
472 * if there aren't any dots, it could be a user-level alias.
473 * this is also done in res_query() since we are not the only
474 * function that looks up host names.
476 if (!strchr(name
, '.') && (cp
= __hostalias(name
)))
480 * disallow names consisting only of digits/dots, unless
483 if (isdigit(name
[0]))
484 for (cp
= name
;; ++cp
) {
489 * All-numeric, no dot at the end.
490 * Fake up a hostent as if we'd actually
493 if (!inet_aton(name
, &host_addr
)) {
494 h_errno
= HOST_NOT_FOUND
;
497 strncpy(hostbuf
, name
, MAXDNAME
);
498 hostbuf
[MAXDNAME
] = '\0';
499 host
.h_name
= hostbuf
;
500 host
.h_aliases
= host_aliases
;
501 host_aliases
[0] = NULL
;
502 host
.h_addrtype
= AF_INET
;
503 host
.h_length
= INT32SZ
;
504 h_addr_ptrs
[0] = (char *)&host_addr
;
505 h_addr_ptrs
[1] = NULL
;
506 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
507 host
.h_addr_list
= h_addr_ptrs
;
509 host
.h_addr
= h_addr_ptrs
[0];
511 h_errno
= NETDB_SUCCESS
;
514 if (!isdigit(*cp
) && *cp
!= '.')
518 if ((n
= res_search(name
, C_IN
, T_A
, buf
.buf
, sizeof(buf
))) < 0) {
519 dprintf("res_search failed (%d)\n", n
);
520 #if !defined(__APPLE__)
521 if (errno
== ECONNREFUSED
)
522 return (_gethtbyname(name
));
526 return (getanswer(&buf
, n
, name
, C_IN
, T_A
));
530 gethostbyaddr(addr
, len
, type
)
536 register struct hostent
*hp
;
537 char qbuf
[MAXDNAME
+1];
539 register struct hostent
*rhp
;
542 char hname2
[MAXDNAME
+1];
543 #endif /*SUNSECURITY*/
544 #if !defined(__APPLE__)
545 extern struct hostent
*_gethtbyaddr();
548 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
549 h_errno
= NETDB_INTERNAL
;
552 if (type
!= AF_INET
) {
553 errno
= EAFNOSUPPORT
;
554 h_errno
= NETDB_INTERNAL
;
557 (void)sprintf(qbuf
, "%u.%u.%u.%u.in-addr.arpa",
558 ((unsigned)addr
[3] & 0xff),
559 ((unsigned)addr
[2] & 0xff),
560 ((unsigned)addr
[1] & 0xff),
561 ((unsigned)addr
[0] & 0xff));
562 n
= res_query(qbuf
, C_IN
, T_PTR
, (u_char
*)buf
.buf
, sizeof buf
.buf
);
564 dprintf("res_query failed (%d)\n", n
);
565 #if !defined(__APPLE__)
566 if (errno
== ECONNREFUSED
)
567 return (_gethtbyaddr(addr
, len
, type
));
571 if (!(hp
= getanswer(&buf
, n
, qbuf
, C_IN
, T_PTR
)))
572 return (NULL
); /* h_errno was set by getanswer() */
575 * turn off search as the name should be absolute,
576 * 'localhost' should be matched by defnames
578 strncpy(hname2
, hp
->h_name
, MAXDNAME
);
579 hname2
[MAXDNAME
] = '\0';
580 old_options
= _res
.options
;
581 _res
.options
&= ~RES_DNSRCH
;
582 _res
.options
|= RES_DEFNAMES
;
583 if (!(rhp
= gethostbyname(hname2
))) {
584 syslog(LOG_NOTICE
|LOG_AUTH
,
585 "gethostbyaddr: No A record for %s (verifying [%s])",
586 hname2
, inet_ntoa(*((struct in_addr
*)addr
)));
587 _res
.options
= old_options
;
588 h_errno
= HOST_NOT_FOUND
;
591 _res
.options
= old_options
;
592 for (haddr
= rhp
->h_addr_list
; *haddr
; haddr
++)
593 if (!memcmp(*haddr
, addr
, INADDRSZ
))
596 syslog(LOG_NOTICE
|LOG_AUTH
,
597 "gethostbyaddr: A record of %s != PTR record [%s]",
598 hname2
, inet_ntoa(*((struct in_addr
*)addr
)));
599 h_errno
= HOST_NOT_FOUND
;
602 #endif /*SUNSECURITY*/
603 hp
->h_addrtype
= type
;
605 h_addr_ptrs
[0] = (char *)&host_addr
;
606 h_addr_ptrs
[1] = NULL
;
607 host_addr
= *(struct in_addr
*)addr
;
608 h_errno
= NETDB_SUCCESS
;
617 hostf
= fopen(_PATH_HOSTS
, "r" );
626 if (hostf
&& !stayopen
) {
627 (void) fclose(hostf
);
636 register char *cp
, **q
;
638 if (!hostf
&& !(hostf
= fopen(_PATH_HOSTS
, "r" ))) {
639 h_errno
= NETDB_INTERNAL
;
642 if (hostbuf
== NULL
) {
643 hostbuf
= malloc(MAXHOSTBUF
);
644 if (hostbuf
== NULL
) {
645 h_errno
= NETDB_INTERNAL
;
650 if (!(p
= fgets(hostbuf
, MAXHOSTBUF
, hostf
))) {
651 h_errno
= HOST_NOT_FOUND
;
656 if (!(cp
= strpbrk(p
, "#\n")))
659 if (!(cp
= strpbrk(p
, " \t")))
662 /* THIS STUFF IS INTERNET SPECIFIC */
663 if (!inet_aton(p
, &host_addr
))
665 h_addr_ptrs
[0] = (char *)&host_addr
;
666 h_addr_ptrs
[1] = NULL
;
667 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
668 host
.h_addr_list
= h_addr_ptrs
;
670 host
.h_addr
= h_addr_ptrs
[0];
672 host
.h_length
= INT32SZ
;
673 host
.h_addrtype
= AF_INET
;
674 while (*cp
== ' ' || *cp
== '\t')
677 q
= host
.h_aliases
= host_aliases
;
678 if ((cp
= strpbrk(cp
, " \t")))
681 if (*cp
== ' ' || *cp
== '\t') {
685 if (q
< &host_aliases
[MAXALIASES
- 1])
687 if ((cp
= strpbrk(cp
, " \t")))
691 h_errno
= NETDB_SUCCESS
;
699 register struct hostent
*p
;
703 while ((p
= _gethtent())) {
704 if (strcasecmp(p
->h_name
, name
) == 0)
706 for (cp
= p
->h_aliases
; *cp
!= 0; cp
++)
707 if (strcasecmp(*cp
, name
) == 0)
716 _gethtbyaddr(addr
, len
, type
)
720 register struct hostent
*p
;
723 while ((p
= _gethtent()))
724 if (p
->h_addrtype
== type
&& !bcmp(p
->h_addr
, addr
, len
))
738 short aval
[MAXADDRS
];
742 for (i
= 0; i
< num
; i
++, p
++) {
743 for (j
= 0 ; (unsigned)j
< _res
.nsort
; j
++)
744 if (_res
.sort_list
[j
].addr
.s_addr
==
745 (((struct in_addr
*)(*p
))->s_addr
& _res
.sort_list
[j
].mask
))
748 if (needsort
== 0 && i
> 0 && j
< aval
[i
-1])
754 while (needsort
< num
) {
755 for (j
= needsort
- 1; j
>= 0; j
--) {
756 if (aval
[j
] > aval
[j
+1]) {
775 #if defined(BSD43_BSD43_NFS) || defined(sun)
776 /* some libc's out there are bound internally to these names (UMIPS) */
778 ht_sethostent(stayopen
)
791 ht_gethostbyname(name
)
794 return (_gethtbyname(name
));
798 ht_gethostbyaddr(addr
, len
, type
)
802 return (_gethtbyaddr(addr
, len
, type
));
808 return (_gethtent());
818 dn_skipname(comp_dn
, eom
)
819 const u_char
*comp_dn
, *eom
;
821 return (__dn_skipname(comp_dn
, eom
));
823 #endif /*old-style libc with yp junk in it*/