]>
git.saurik.com Git - apple/libinfo.git/blob - dns.subproj/gethnamaddr.c
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.2 1999/10/14 21:56:44 wsanchez 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/nameser.h>
101 #define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */
103 #if defined(BSD) && (BSD >= 199103)
106 # include "portability.h"
108 #if defined(USE_OPTIONS_H)
109 # include "options.h"
112 #define MAXALIASES 35
115 static const char AskedForGot
[] =
116 "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
118 static char *h_addr_ptrs
[MAXADDRS
+ 1];
119 static struct hostent
*gethostbyname_ipv4
__P((const char *));
121 static struct hostent host
;
122 static char *host_aliases
[MAXALIASES
];
123 static char hostbuf
[8*1024];
124 static struct in_addr host_addr
;
125 static FILE *hostf
= NULL
;
126 static int stayopen
= 0;
129 static void addrsort
__P((char **, int));
133 #define MAXPACKET PACKETSZ
135 #define MAXPACKET 1024
140 u_char buf
[MAXPACKET
];
156 if (_res
.options
& RES_DEBUG
) {
164 # define dprintf(msg, num) /*nada*/
167 static struct hostent
*
168 getanswer(answer
, anslen
, qname
, qclass
, qtype
)
169 const querybuf
*answer
;
174 register const HEADER
*hp
;
175 register const u_char
*cp
;
178 char *bp
, **ap
, **hap
;
179 int type
, class, buflen
, ancount
, qdcount
;
180 int haveanswer
, had_error
;
182 char tbuf
[MAXDNAME
+1];
187 eom
= answer
->buf
+ anslen
;
189 * find first satisfactory answer
192 ancount
= ntohs(hp
->ancount
);
193 qdcount
= ntohs(hp
->qdcount
);
195 buflen
= sizeof hostbuf
;
196 cp
= answer
->buf
+ HFIXEDSZ
;
198 h_errno
= NO_RECOVERY
;
201 if ((n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
)) < 0) {
202 h_errno
= NO_RECOVERY
;
207 /* res_send() has already verified that the query name is the
208 * same as the one we sent; this just gets the expanded name
209 * (i.e., with the succeeding search-domain tacked on).
211 n
= strlen(bp
) + 1; /* for the \0 */
215 /* The qname can be abbreviated, but h_name is now absolute. */
220 host
.h_aliases
= host_aliases
;
223 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
224 host
.h_addr_list
= h_addr_ptrs
;
228 while (ancount
-- > 0 && cp
< eom
&& !had_error
) {
229 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
235 type
= _getshort(cp
);
236 cp
+= INT16SZ
; /* type */
237 class = _getshort(cp
);
238 cp
+= INT16SZ
+ INT32SZ
; /* class, TTL */
240 cp
+= INT16SZ
; /* len */
241 if (class != qclass
) {
242 /* XXX - debug? syslog? */
244 continue; /* XXX - had_error++ ? */
246 if (qtype
== T_A
&& type
== T_CNAME
) {
247 if (ap
>= &host_aliases
[MAXALIASES
-1])
249 n
= dn_expand(answer
->buf
, eom
, cp
, tbuf
, sizeof tbuf
);
255 if (host
.h_name
&& strcasecmp(host
.h_name
, bp
) != 0) {
256 syslog(LOG_NOTICE
|LOG_AUTH
,
257 "gethostby*.getanswer: asked for \"%s\", got CNAME for \"%s\"",
259 continue; /* XXX - had_error++ ? */
263 n
= strlen(bp
) + 1; /* for the \0 */
266 /* Get canonical name. */
267 n
= strlen(tbuf
) + 1; /* for the \0 */
278 if (qtype
== T_PTR
&& type
== T_CNAME
) {
279 n
= dn_expand(answer
->buf
, eom
, cp
, tbuf
, sizeof tbuf
);
285 /* Get canonical name. */
286 n
= strlen(tbuf
) + 1; /* for the \0 */
298 syslog(LOG_NOTICE
|LOG_AUTH
,
299 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
300 qname
, p_class(qclass
), p_type(qtype
),
303 continue; /* XXX - had_error++ ? */
307 if (strcasecmp(tname
, bp
) != 0) {
308 syslog(LOG_NOTICE
|LOG_AUTH
,
309 AskedForGot
, qname
, bp
);
311 continue; /* XXX - had_error++ ? */
313 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
318 #if MULTI_PTRS_ARE_ALIASES
322 else if (ap
< &host_aliases
[MAXALIASES
-1])
327 n
= strlen(bp
) + 1; /* for the \0 */
334 h_errno
= NETDB_SUCCESS
;
338 if (strcasecmp(host
.h_name
, bp
) != 0) {
339 syslog(LOG_NOTICE
|LOG_AUTH
,
340 AskedForGot
, host
.h_name
, bp
);
342 continue; /* XXX - had_error++ ? */
345 if (n
!= host
.h_length
) {
353 host
.h_addrtype
= (class == C_IN
)
357 nn
= strlen(bp
) + 1; /* for the \0 */
362 bp
+= sizeof(align
) - ((u_long
)bp
% sizeof(align
));
364 if (bp
+ n
>= &hostbuf
[sizeof hostbuf
]) {
365 dprintf("size (%d) too big\n", n
);
369 if (hap
>= &h_addr_ptrs
[MAXADDRS
-1]) {
371 dprintf("Too many addresses (%d)\n",
376 bcopy(cp
, *hap
++ = bp
, n
);
381 dprintf("Impossible condition (type=%d)\n", type
);
382 h_errno
= NO_RECOVERY
;
391 # if defined(RESOLVSORT)
393 * Note: we sort even if host can take only one address
394 * in its return structures - should give it the "best"
395 * address in that case, not some random one
397 if (_res
.nsort
&& haveanswer
> 1 &&
398 qclass
== C_IN
&& qtype
== T_A
)
399 addrsort(h_addr_ptrs
, haveanswer
);
400 # endif /*RESOLVSORT*/
401 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
404 host
.h_addr
= h_addr_ptrs
[0];
407 n
= strlen(qname
) + 1; /* for the \0 */
411 h_errno
= NETDB_SUCCESS
;
423 #if defined(AF_INET6) && defined(RES_TRY_INET6)
426 if (_res
.options
& RES_TRY_INET6
) {
427 hp
= gethostbyname2(name
, AF_INET6
);
432 return (gethostbyname2(name
, AF_INET
));
436 gethostbyname2(name
, af
)
442 return (gethostbyname_ipv4(name
));
444 errno
= EAFNOSUPPORT
;
445 h_errno
= NETDB_INTERNAL
;
449 static struct hostent
*
450 gethostbyname_ipv4(name
)
454 register const char *cp
;
456 #if !defined(__APPLE__)
457 extern struct hostent
*_gethtbyname();
460 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
461 h_errno
= NETDB_INTERNAL
;
466 * if there aren't any dots, it could be a user-level alias.
467 * this is also done in res_query() since we are not the only
468 * function that looks up host names.
470 if (!strchr(name
, '.') && (cp
= __hostalias(name
)))
474 * disallow names consisting only of digits/dots, unless
477 if (isdigit(name
[0]))
478 for (cp
= name
;; ++cp
) {
483 * All-numeric, no dot at the end.
484 * Fake up a hostent as if we'd actually
487 if (!inet_aton(name
, &host_addr
)) {
488 h_errno
= HOST_NOT_FOUND
;
491 strncpy(hostbuf
, name
, MAXDNAME
);
492 hostbuf
[MAXDNAME
] = '\0';
493 host
.h_name
= hostbuf
;
494 host
.h_aliases
= host_aliases
;
495 host_aliases
[0] = NULL
;
496 host
.h_addrtype
= AF_INET
;
497 host
.h_length
= INT32SZ
;
498 h_addr_ptrs
[0] = (char *)&host_addr
;
499 h_addr_ptrs
[1] = NULL
;
500 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
501 host
.h_addr_list
= h_addr_ptrs
;
503 host
.h_addr
= h_addr_ptrs
[0];
505 h_errno
= NETDB_SUCCESS
;
508 if (!isdigit(*cp
) && *cp
!= '.')
512 if ((n
= res_search(name
, C_IN
, T_A
, buf
.buf
, sizeof(buf
))) < 0) {
513 dprintf("res_search failed (%d)\n", n
);
514 #if !defined(__APPLE__)
515 if (errno
== ECONNREFUSED
)
516 return (_gethtbyname(name
));
520 return (getanswer(&buf
, n
, name
, C_IN
, T_A
));
524 gethostbyaddr(addr
, len
, type
)
530 register struct hostent
*hp
;
531 char qbuf
[MAXDNAME
+1];
533 register struct hostent
*rhp
;
536 char hname2
[MAXDNAME
+1];
537 #endif /*SUNSECURITY*/
538 #if !defined(__APPLE__)
539 extern struct hostent
*_gethtbyaddr();
542 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
543 h_errno
= NETDB_INTERNAL
;
546 if (type
!= AF_INET
) {
547 errno
= EAFNOSUPPORT
;
548 h_errno
= NETDB_INTERNAL
;
551 (void)sprintf(qbuf
, "%u.%u.%u.%u.in-addr.arpa",
552 ((unsigned)addr
[3] & 0xff),
553 ((unsigned)addr
[2] & 0xff),
554 ((unsigned)addr
[1] & 0xff),
555 ((unsigned)addr
[0] & 0xff));
556 n
= res_query(qbuf
, C_IN
, T_PTR
, (u_char
*)buf
.buf
, sizeof buf
.buf
);
558 dprintf("res_query failed (%d)\n", n
);
559 #if !defined(__APPLE__)
560 if (errno
== ECONNREFUSED
)
561 return (_gethtbyaddr(addr
, len
, type
));
565 if (!(hp
= getanswer(&buf
, n
, qbuf
, C_IN
, T_PTR
)))
566 return (NULL
); /* h_errno was set by getanswer() */
569 * turn off search as the name should be absolute,
570 * 'localhost' should be matched by defnames
572 strncpy(hname2
, hp
->h_name
, MAXDNAME
);
573 hname2
[MAXDNAME
] = '\0';
574 old_options
= _res
.options
;
575 _res
.options
&= ~RES_DNSRCH
;
576 _res
.options
|= RES_DEFNAMES
;
577 if (!(rhp
= gethostbyname(hname2
))) {
578 syslog(LOG_NOTICE
|LOG_AUTH
,
579 "gethostbyaddr: No A record for %s (verifying [%s])",
580 hname2
, inet_ntoa(*((struct in_addr
*)addr
)));
581 _res
.options
= old_options
;
582 h_errno
= HOST_NOT_FOUND
;
585 _res
.options
= old_options
;
586 for (haddr
= rhp
->h_addr_list
; *haddr
; haddr
++)
587 if (!memcmp(*haddr
, addr
, INADDRSZ
))
590 syslog(LOG_NOTICE
|LOG_AUTH
,
591 "gethostbyaddr: A record of %s != PTR record [%s]",
592 hname2
, inet_ntoa(*((struct in_addr
*)addr
)));
593 h_errno
= HOST_NOT_FOUND
;
596 #endif /*SUNSECURITY*/
597 hp
->h_addrtype
= type
;
599 h_addr_ptrs
[0] = (char *)&host_addr
;
600 h_addr_ptrs
[1] = NULL
;
601 host_addr
= *(struct in_addr
*)addr
;
602 h_errno
= NETDB_SUCCESS
;
611 hostf
= fopen(_PATH_HOSTS
, "r" );
620 if (hostf
&& !stayopen
) {
621 (void) fclose(hostf
);
630 register char *cp
, **q
;
632 if (!hostf
&& !(hostf
= fopen(_PATH_HOSTS
, "r" ))) {
633 h_errno
= NETDB_INTERNAL
;
637 if (!(p
= fgets(hostbuf
, sizeof hostbuf
, hostf
))) {
638 h_errno
= HOST_NOT_FOUND
;
643 if (!(cp
= strpbrk(p
, "#\n")))
646 if (!(cp
= strpbrk(p
, " \t")))
649 /* THIS STUFF IS INTERNET SPECIFIC */
650 if (!inet_aton(p
, &host_addr
))
652 h_addr_ptrs
[0] = (char *)&host_addr
;
653 h_addr_ptrs
[1] = NULL
;
654 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */
655 host
.h_addr_list
= h_addr_ptrs
;
657 host
.h_addr
= h_addr_ptrs
[0];
659 host
.h_length
= INT32SZ
;
660 host
.h_addrtype
= AF_INET
;
661 while (*cp
== ' ' || *cp
== '\t')
664 q
= host
.h_aliases
= host_aliases
;
665 if (cp
= strpbrk(cp
, " \t"))
668 if (*cp
== ' ' || *cp
== '\t') {
672 if (q
< &host_aliases
[MAXALIASES
- 1])
674 if (cp
= strpbrk(cp
, " \t"))
678 h_errno
= NETDB_SUCCESS
;
686 register struct hostent
*p
;
690 while (p
= _gethtent()) {
691 if (strcasecmp(p
->h_name
, name
) == 0)
693 for (cp
= p
->h_aliases
; *cp
!= 0; cp
++)
694 if (strcasecmp(*cp
, name
) == 0)
703 _gethtbyaddr(addr
, len
, type
)
707 register struct hostent
*p
;
710 while (p
= _gethtent())
711 if (p
->h_addrtype
== type
&& !bcmp(p
->h_addr
, addr
, len
))
725 short aval
[MAXADDRS
];
729 for (i
= 0; i
< num
; i
++, p
++) {
730 for (j
= 0 ; (unsigned)j
< _res
.nsort
; j
++)
731 if (_res
.sort_list
[j
].addr
.s_addr
==
732 (((struct in_addr
*)(*p
))->s_addr
& _res
.sort_list
[j
].mask
))
735 if (needsort
== 0 && i
> 0 && j
< aval
[i
-1])
741 while (needsort
< num
) {
742 for (j
= needsort
- 1; j
>= 0; j
--) {
743 if (aval
[j
] > aval
[j
+1]) {
762 #if defined(BSD43_BSD43_NFS) || defined(sun)
763 /* some libc's out there are bound internally to these names (UMIPS) */
765 ht_sethostent(stayopen
)
778 ht_gethostbyname(name
)
781 return (_gethtbyname(name
));
785 ht_gethostbyaddr(addr
, len
, type
)
789 return (_gethtbyaddr(addr
, len
, type
));
795 return (_gethtent());
805 dn_skipname(comp_dn
, eom
)
806 const u_char
*comp_dn
, *eom
;
808 return (__dn_skipname(comp_dn
, eom
));
810 #endif /*old-style libc with yp junk in it*/