2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * ++Copyright++ 1985, 1988, 1993
27 * Copyright (c) 1985, 1988, 1993
28 * The Regents of the University of California. All rights reserved.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
60 * Permission to use, copy, modify, and distribute this software for any
61 * purpose with or without fee is hereby granted, provided that the above
62 * copyright notice and this permission notice appear in all copies, and that
63 * the name of Digital Equipment Corporation not be used in advertising or
64 * publicity pertaining to distribution of the document or software without
65 * specific, written prior permission.
67 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
68 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
69 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
70 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
71 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
72 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
73 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
79 #if defined(LIBC_SCCS) && !defined(lint)
80 static char sccsid
[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
81 static char rcsid
[] = "$Id: gethnamaddr.c,v 1.9 2004/10/07 17:33:33 majka Exp $";
82 #endif /* LIBC_SCCS and not lint */
84 #include <sys/param.h>
85 #include <sys/socket.h>
86 #include <netinet/in.h>
87 #include <arpa/inet.h>
88 #include <arpa/nameser8_compat.h>
93 #include <resolv8_compat.h>
102 #define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */
104 #if defined(BSD) && (BSD >= 199103)
107 # include "portability.h"
109 #if defined(USE_OPTIONS_H)
110 # include "options.h"
113 #define MAXALIASES 35
115 #define MAXHOSTBUF 8*1024
117 static const char AskedForGot
[] =
118 "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
120 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
];
159 if (_res
.options
& RES_DEBUG
) {
167 # define dprintf(msg, num) /*nada*/
170 static struct hostent
*
171 getanswer(answer
, anslen
, qname
, qclass
, qtype
)
172 const querybuf
*answer
;
177 register const HEADER
*hp
;
178 register const u_char
*cp
;
181 char *bp
, **ap
, **hap
;
182 int type
, class, buflen
, ancount
, qdcount
;
183 int haveanswer
, had_error
;
185 char tbuf
[MAXDNAME
+1];
188 if (hostbuf
== NULL
) {
189 hostbuf
= malloc(MAXHOSTBUF
);
197 eom
= answer
->buf
+ anslen
;
199 * find first satisfactory answer
202 ancount
= ntohs(hp
->ancount
);
203 qdcount
= ntohs(hp
->qdcount
);
205 cp
= answer
->buf
+ HFIXEDSZ
;
207 h_errno
= NO_RECOVERY
;
210 if ((n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
)) < 0) {
211 h_errno
= NO_RECOVERY
;
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 */
224 /* The qname can be abbreviated, but h_name is now absolute. */
229 host
.h_aliases
= host_aliases
;
232 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
233 host
.h_addr_list
= h_addr_ptrs
;
237 while (ancount
-- > 0 && cp
< eom
&& !had_error
) {
238 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
244 type
= _getshort(cp
);
245 cp
+= INT16SZ
; /* type */
246 class = _getshort(cp
);
247 cp
+= INT16SZ
+ INT32SZ
; /* class, TTL */
249 cp
+= INT16SZ
; /* len */
250 if (class != qclass
) {
251 /* XXX - debug? syslog? */
253 continue; /* XXX - had_error++ ? */
255 if (qtype
== T_A
&& type
== T_CNAME
) {
256 if (ap
>= &host_aliases
[MAXALIASES
-1])
258 n
= dn_expand(answer
->buf
, eom
, cp
, tbuf
, sizeof tbuf
);
264 if (host
.h_name
&& strcasecmp(host
.h_name
, bp
) != 0) {
265 syslog(LOG_NOTICE
|LOG_AUTH
,
266 "gethostby*.getanswer: asked for \"%s\", got CNAME for \"%s\"",
268 continue; /* XXX - had_error++ ? */
272 n
= strlen(bp
) + 1; /* for the \0 */
275 /* Get canonical name. */
276 n
= strlen(tbuf
) + 1; /* for the \0 */
287 if (qtype
== T_PTR
&& type
== T_CNAME
) {
288 n
= dn_expand(answer
->buf
, eom
, cp
, tbuf
, sizeof tbuf
);
294 /* Get canonical name. */
295 n
= strlen(tbuf
) + 1; /* for the \0 */
307 syslog(LOG_NOTICE
|LOG_AUTH
,
308 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
309 qname
, p_class(qclass
), 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
);
327 #if MULTI_PTRS_ARE_ALIASES
331 else if (ap
< &host_aliases
[MAXALIASES
-1])
336 n
= strlen(bp
) + 1; /* for the \0 */
343 h_errno
= NETDB_SUCCESS
;
347 if (strcasecmp(host
.h_name
, bp
) != 0) {
348 syslog(LOG_NOTICE
|LOG_AUTH
,
349 AskedForGot
, host
.h_name
, bp
);
351 continue; /* XXX - had_error++ ? */
354 if (n
!= host
.h_length
) {
362 host
.h_addrtype
= (class == C_IN
)
366 nn
= strlen(bp
) + 1; /* for the \0 */
371 bp
+= sizeof(align
) - ((u_long
)bp
% sizeof(align
));
373 if (bp
+ n
>= &hostbuf
[MAXHOSTBUF
]) {
374 dprintf("size (%d) too big\n", n
);
378 if (hap
>= &h_addr_ptrs
[MAXADDRS
-1]) {
380 dprintf("Too many addresses (%d)\n",
385 bcopy(cp
, *hap
++ = bp
, n
);
390 dprintf("Impossible condition (type=%d)\n", type
);
391 h_errno
= NO_RECOVERY
;
400 # if defined(RESOLVSORT)
402 * Note: we sort even if host can take only one address
403 * in its return structures - should give it the "best"
404 * address in that case, not some random one
406 if (_res
.nsort
&& haveanswer
> 1 &&
407 qclass
== C_IN
&& qtype
== T_A
)
408 addrsort(h_addr_ptrs
, haveanswer
);
409 # endif /*RESOLVSORT*/
410 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
413 host
.h_addr
= h_addr_ptrs
[0];
416 n
= strlen(qname
) + 1; /* for the \0 */
420 h_errno
= NETDB_SUCCESS
;
432 #if defined(AF_INET6) && defined(RES_TRY_INET6)
435 if (_res
.options
& RES_TRY_INET6
) {
436 hp
= gethostbyname2(name
, AF_INET6
);
441 return (gethostbyname2(name
, AF_INET
));
445 static struct hostent
*
446 gethostbyname_ipv4(name
)
450 register const char *cp
;
452 #if !defined(__APPLE__)
453 extern struct hostent
*_gethtbyname();
458 hostbuf
= malloc(MAXHOSTBUF
);
459 if (hostbuf
== NULL
) {
460 h_errno
= NETDB_INTERNAL
;
465 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
466 h_errno
= NETDB_INTERNAL
;
471 * if there aren't any dots, it could be a user-level alias.
472 * this is also done in res_query() since we are not the only
473 * function that looks up host names.
475 if (!strchr(name
, '.') && (cp
= __hostalias(name
)))
479 * disallow names consisting only of digits/dots, unless
482 if (isdigit(name
[0]))
483 for (cp
= name
;; ++cp
) {
488 * All-numeric, no dot at the end.
489 * Fake up a hostent as if we'd actually
492 if (!inet_aton(name
, &host_addr
)) {
493 h_errno
= HOST_NOT_FOUND
;
496 strncpy(hostbuf
, name
, MAXDNAME
);
497 hostbuf
[MAXDNAME
] = '\0';
498 host
.h_name
= hostbuf
;
499 host
.h_aliases
= host_aliases
;
500 host_aliases
[0] = NULL
;
501 host
.h_addrtype
= AF_INET
;
502 host
.h_length
= INT32SZ
;
503 h_addr_ptrs
[0] = (char *)&host_addr
;
504 h_addr_ptrs
[1] = NULL
;
505 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
506 host
.h_addr_list
= h_addr_ptrs
;
508 host
.h_addr
= h_addr_ptrs
[0];
510 h_errno
= NETDB_SUCCESS
;
513 if (!isdigit(*cp
) && *cp
!= '.')
517 if ((n
= res_search(name
, C_IN
, T_A
, buf
.buf
, sizeof(buf
))) < 0) {
518 dprintf("res_search failed (%d)\n", n
);
519 #if !defined(__APPLE__)
520 if (errno
== ECONNREFUSED
)
521 return (_gethtbyname(name
));
525 return (getanswer(&buf
, n
, name
, C_IN
, T_A
));
530 gethostbyaddr(vaddr
, len
, type
)
537 register struct hostent
*hp
;
538 const char* addr
= (const char *)vaddr
;
539 char qbuf
[MAXDNAME
+1];
541 register struct hostent
*rhp
;
544 char hname2
[MAXDNAME
+1];
545 #endif /*SUNSECURITY*/
546 #if !defined(__APPLE__)
547 extern struct hostent
*_gethtbyaddr();
550 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
551 h_errno
= NETDB_INTERNAL
;
554 if (type
!= AF_INET
) {
555 errno
= EAFNOSUPPORT
;
556 h_errno
= NETDB_INTERNAL
;
559 (void)sprintf(qbuf
, "%u.%u.%u.%u.in-addr.arpa",
560 ((unsigned)addr
[3] & 0xff),
561 ((unsigned)addr
[2] & 0xff),
562 ((unsigned)addr
[1] & 0xff),
563 ((unsigned)addr
[0] & 0xff));
564 n
= res_query(qbuf
, C_IN
, T_PTR
, (u_char
*)buf
.buf
, sizeof buf
.buf
);
566 dprintf("res_query failed (%d)\n", n
);
567 #if !defined(__APPLE__)
568 if (errno
== ECONNREFUSED
)
569 return (_gethtbyaddr(addr
, len
, type
));
573 if (!(hp
= getanswer(&buf
, n
, qbuf
, C_IN
, T_PTR
)))
574 return (NULL
); /* h_errno was set by getanswer() */
577 * turn off search as the name should be absolute,
578 * 'localhost' should be matched by defnames
580 strncpy(hname2
, hp
->h_name
, MAXDNAME
);
581 hname2
[MAXDNAME
] = '\0';
582 old_options
= _res
.options
;
583 _res
.options
&= ~RES_DNSRCH
;
584 _res
.options
|= RES_DEFNAMES
;
585 if (!(rhp
= gethostbyname(hname2
))) {
586 syslog(LOG_NOTICE
|LOG_AUTH
,
587 "gethostbyaddr: No A record for %s (verifying [%s])",
588 hname2
, inet_ntoa(*((struct in_addr
*)addr
)));
589 _res
.options
= old_options
;
590 h_errno
= HOST_NOT_FOUND
;
593 _res
.options
= old_options
;
594 for (haddr
= rhp
->h_addr_list
; *haddr
; haddr
++)
595 if (!memcmp(*haddr
, addr
, INADDRSZ
))
598 syslog(LOG_NOTICE
|LOG_AUTH
,
599 "gethostbyaddr: A record of %s != PTR record [%s]",
600 hname2
, inet_ntoa(*((struct in_addr
*)addr
)));
601 h_errno
= HOST_NOT_FOUND
;
604 #endif /*SUNSECURITY*/
605 hp
->h_addrtype
= type
;
607 h_addr_ptrs
[0] = (char *)&host_addr
;
608 h_addr_ptrs
[1] = NULL
;
609 host_addr
= *(struct in_addr
*)addr
;
610 h_errno
= NETDB_SUCCESS
;
619 hostf
= fopen(_PATH_HOSTS
, "r" );
628 if (hostf
&& !stayopen
) {
629 (void) fclose(hostf
);
638 register char *cp
, **q
;
640 if (!hostf
&& !(hostf
= fopen(_PATH_HOSTS
, "r" ))) {
641 h_errno
= NETDB_INTERNAL
;
644 if (hostbuf
== NULL
) {
645 hostbuf
= malloc(MAXHOSTBUF
);
646 if (hostbuf
== NULL
) {
647 h_errno
= NETDB_INTERNAL
;
652 if (!(p
= fgets(hostbuf
, MAXHOSTBUF
, hostf
))) {
653 h_errno
= HOST_NOT_FOUND
;
658 if (!(cp
= strpbrk(p
, "#\n")))
661 if (!(cp
= strpbrk(p
, " \t")))
664 /* THIS STUFF IS INTERNET SPECIFIC */
665 if (!inet_aton(p
, &host_addr
))
667 h_addr_ptrs
[0] = (char *)&host_addr
;
668 h_addr_ptrs
[1] = NULL
;
669 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
670 host
.h_addr_list
= h_addr_ptrs
;
672 host
.h_addr
= h_addr_ptrs
[0];
674 host
.h_length
= INT32SZ
;
675 host
.h_addrtype
= AF_INET
;
676 while (*cp
== ' ' || *cp
== '\t')
679 q
= host
.h_aliases
= host_aliases
;
680 if ((cp
= strpbrk(cp
, " \t")))
683 if (*cp
== ' ' || *cp
== '\t') {
687 if (q
< &host_aliases
[MAXALIASES
- 1])
689 if ((cp
= strpbrk(cp
, " \t")))
693 h_errno
= NETDB_SUCCESS
;
701 register struct hostent
*p
;
705 while ((p
= _gethtent())) {
706 if (strcasecmp(p
->h_name
, name
) == 0)
708 for (cp
= p
->h_aliases
; *cp
!= 0; cp
++)
709 if (strcasecmp(*cp
, name
) == 0)
718 _gethtbyaddr(addr
, len
, type
)
722 register struct hostent
*p
;
725 while ((p
= _gethtent()))
726 if (p
->h_addrtype
== type
&& !bcmp(p
->h_addr
, addr
, len
))
740 short aval
[MAXADDRS
];
744 for (i
= 0; i
< num
; i
++, p
++) {
745 for (j
= 0 ; (unsigned)j
< _res
.nsort
; j
++)
746 if (_res
.sort_list
[j
].addr
.s_addr
==
747 (((struct in_addr
*)(*p
))->s_addr
& _res
.sort_list
[j
].mask
))
750 if (needsort
== 0 && i
> 0 && j
< aval
[i
-1])
756 while (needsort
< num
) {
757 for (j
= needsort
- 1; j
>= 0; j
--) {
758 if (aval
[j
] > aval
[j
+1]) {
777 #if defined(BSD43_BSD43_NFS) || defined(sun)
778 /* some libc's out there are bound internally to these names (UMIPS) */
780 ht_sethostent(stayopen
)
793 ht_gethostbyname(name
)
796 return (_gethtbyname(name
));
800 ht_gethostbyaddr(addr
, len
, type
)
804 return (_gethtbyaddr(addr
, len
, type
));
810 return (_gethtent());
820 dn_skipname(comp_dn
, eom
)
821 const u_char
*comp_dn
, *eom
;
823 return (__dn_skipname(comp_dn
, eom
));
825 #endif /*old-style libc with yp junk in it*/