]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/BsdSocketLib/gethostbydns.c
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
57 * Portions copyright (c) 1999, 2000
59 * All rights reserved.
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in the
70 * documentation and/or other materials provided with the distribution.
72 * 3. All advertising materials mentioning features or use of this software
73 * must display the following acknowledgement:
75 * This product includes software developed by Intel Corporation and
78 * 4. Neither the name of Intel Corporation or its contributors may be
79 * used to endorse or promote products derived from this software
80 * without specific prior written permission.
82 * THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION AND CONTRIBUTORS ``AS IS''
83 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
84 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
85 * ARE DISCLAIMED. IN NO EVENT SHALL INTEL CORPORATION OR CONTRIBUTORS BE
86 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
87 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
88 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
89 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
90 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
91 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
92 * THE POSSIBILITY OF SUCH DAMAGE.
96 #if defined(LIBC_SCCS) && !defined(lint)
97 static char sccsid
[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
98 static char fromrcsid
[] = "From: Id: gethnamaddr.c,v 8.23 1998/04/07 04:59:46 vixie Exp $";
99 static char rcsid
[] = "$Id: gethostbydns.c,v 1.1.1.1 2003/11/19 01:51:27 kyu3 Exp $";
100 #endif /* LIBC_SCCS and not lint */
102 #include <sys/types.h>
103 #include <sys/param.h>
104 #include <sys/socket.h>
105 #include <netinet/in.h>
106 #include <arpa/inet.h>
107 #include <arpa/nameser.h>
120 u_int32_t
_getlong(const u_char
*src
);
121 u_int16_t
_getshort(const u_char
*src
);
124 #include "res_config.h"
125 #include "Socklib_internals.h"
127 #define SPRINTF(x) ((size_t)sprintf x)
129 #define MAXALIASES 35
133 static const char AskedForGot
[] =
134 "gethostby*.gethostanswer: asked for \"%s\", got \"%s\"";
137 static char *h_addr_ptrs
[MAXADDRS
+ 1];
139 static struct hostent host
;
140 static char *host_aliases
[MAXALIASES
];
141 static char hostbuf
[8*1024];
142 static u_char host_addr
[16]; /* IPv4 or IPv6 */
145 static void addrsort(char **, int);
149 #define MAXPACKET PACKETSZ
151 #define MAXPACKET 1024
156 u_char buf
[MAXPACKET
];
169 dprintf(char *msg
, int num
)
171 if (_res
.options
& RES_DEBUG
) {
179 # define dprintf(msg, num) /*nada*/
182 #define BOUNDED_INCR(x) \
186 h_errno = NO_RECOVERY; \
191 #define BOUNDS_CHECK(ptr, count) \
193 if ((ptr) + (count) > eom) { \
194 h_errno = NO_RECOVERY; \
199 static struct hostent
*
200 gethostanswer(const querybuf
*answer
, int anslen
, const char *qname
, int qtype
)
202 register const HEADER
*hp
;
203 register const u_char
*cp
;
205 const u_char
*eom
, *erdata
;
206 char *bp
, **ap
, **hap
;
207 int type
, class, buflen
, ancount
, qdcount
;
208 int haveanswer
, had_error
;
212 int (*name_ok
)(const char *);
216 eom
= answer
->buf
+ anslen
;
226 h_errno
= NO_RECOVERY
;
227 return (NULL
); /* XXX should be abort(); */
230 * find first satisfactory answer
233 ancount
= ntohs(hp
->ancount
);
234 qdcount
= ntohs(hp
->qdcount
);
236 buflen
= sizeof hostbuf
;
238 BOUNDED_INCR(HFIXEDSZ
);
240 h_errno
= NO_RECOVERY
;
243 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
244 if ((n
< 0) || !(*name_ok
)(bp
)) {
245 h_errno
= NO_RECOVERY
;
248 BOUNDED_INCR(n
+ QFIXEDSZ
);
249 if (qtype
== T_A
|| qtype
== T_AAAA
) {
250 /* res_send() has already verified that the query name is the
251 * same as the one we sent; this just gets the expanded name
252 * (i.e., with the succeeding search-domain tacked on).
254 n
= (int)strlen(bp
) + 1; /* for the \0 */
255 if (n
>= MAXHOSTNAMELEN
) {
256 h_errno
= NO_RECOVERY
;
262 /* The qname can be abbreviated, but h_name is now absolute. */
267 host
.h_aliases
= host_aliases
;
270 host
.h_addr_list
= h_addr_ptrs
;
274 while (ancount
-- > 0 && cp
< eom
&& !had_error
) {
275 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
276 if ((n
< 0) || !(*name_ok
)(bp
)) {
281 BOUNDS_CHECK(cp
, 3 * INT16SZ
+ INT32SZ
);
282 type
= _getshort(cp
);
283 cp
+= INT16SZ
; /* type */
284 class = _getshort(cp
);
285 cp
+= INT16SZ
; /* class */
286 if (qtype
== T_A
&& type
== T_A
)
287 _dns_ttl_
= _getlong(cp
);
288 cp
+= INT32SZ
; /* TTL */
290 cp
+= INT16SZ
; /* len */
294 /* XXX - debug? syslog? */
296 continue; /* XXX - had_error++ ? */
298 if ((qtype
== T_A
|| qtype
== T_AAAA
) && type
== T_CNAME
) {
299 if (ap
>= &host_aliases
[MAXALIASES
-1])
301 n
= dn_expand(answer
->buf
, eom
, cp
, tbuf
, sizeof tbuf
);
302 if ((n
< 0) || !(*name_ok
)(tbuf
)) {
308 h_errno
= NO_RECOVERY
;
313 n
= (int)strlen(bp
) + 1; /* for the \0 */
314 if (n
>= MAXHOSTNAMELEN
) {
320 /* Get canonical name. */
321 n
= (int)strlen(tbuf
) + 1; /* for the \0 */
322 if (n
> buflen
|| n
>= MAXHOSTNAMELEN
) {
332 if (qtype
== T_PTR
&& type
== T_CNAME
) {
333 n
= dn_expand(answer
->buf
, eom
, cp
, tbuf
, sizeof tbuf
);
334 if (n
< 0 || !res_dnok(tbuf
)) {
340 h_errno
= NO_RECOVERY
;
343 /* Get canonical name. */
344 n
= (int)strlen(tbuf
) + 1; /* for the \0 */
345 if (n
> buflen
|| n
>= MAXHOSTNAMELEN
) {
357 syslog(LOG_NOTICE
|LOG_AUTH
,
358 "gethostby*.gethostanswer: asked for \"%s %s %s\", got type \"%s\"",
359 qname
, p_class(C_IN
), p_type(qtype
),
363 continue; /* XXX - had_error++ ? */
367 if (strcasecmp(tname
, bp
) != 0) {
369 syslog(LOG_NOTICE
|LOG_AUTH
,
370 AskedForGot
, qname
, bp
);
373 continue; /* XXX - had_error++ ? */
375 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, buflen
);
376 if ((n
< 0) || !res_hnok(bp
)) {
380 #if MULTI_PTRS_ARE_ALIASES
383 h_errno
= NO_RECOVERY
;
388 else if (ap
< &host_aliases
[MAXALIASES
-1])
393 n
= (int)strlen(bp
) + 1; /* for the \0 */
394 if (n
>= MAXHOSTNAMELEN
) {
404 if (_res
.options
& RES_USE_INET6
) {
405 n
= strlen(bp
) + 1; /* for the \0 */
406 if (n
>= MAXHOSTNAMELEN
) {
412 _map_v4v6_hostent(&host
, &bp
, &buflen
);
414 h_errno
= NETDB_SUCCESS
;
419 if (strcasecmp(host
.h_name
, bp
) != 0) {
421 syslog(LOG_NOTICE
|LOG_AUTH
,
422 AskedForGot
, host
.h_name
, bp
);
425 continue; /* XXX - had_error++ ? */
427 if (n
!= host
.h_length
) {
435 nn
= (int)strlen(bp
) + 1; /* for the \0 */
440 bp
+= sizeof(align
) - ((size_t)bp
% sizeof(align
));
442 if (bp
+ n
>= &hostbuf
[sizeof hostbuf
]) {
443 dprintf("size (%d) too big\n", n
);
447 if (hap
>= &h_addr_ptrs
[MAXADDRS
-1]) {
449 dprintf("Too many addresses (%d)\n",
460 h_errno
= NO_RECOVERY
;
465 dprintf("Impossible condition (type=%d)\n", type
);
466 h_errno
= NO_RECOVERY
;
468 /* BIND has abort() here, too risky on bad data */
476 # if defined(RESOLVSORT)
478 * Note: we sort even if host can take only one address
479 * in its return structures - should give it the "best"
480 * address in that case, not some random one
482 if (_res
.nsort
&& haveanswer
> 1 && qtype
== T_A
)
483 addrsort(h_addr_ptrs
, haveanswer
);
484 # endif /*RESOLVSORT*/
486 n
= (int)strlen(qname
) + 1; /* for the \0 */
487 if (n
> buflen
|| n
>= MAXHOSTNAMELEN
)
494 if (_res
.options
& RES_USE_INET6
)
495 _map_v4v6_hostent(&host
, &bp
, &buflen
);
496 h_errno
= NETDB_SUCCESS
;
500 h_errno
= NO_RECOVERY
;
505 __dns_getanswer(const char *answer
, int anslen
, const char *qname
, int qtype
)
509 host
.h_addrtype
= AF_INET6
;
510 host
.h_length
= IN6ADDRSZ
;
514 host
.h_addrtype
= AF_INET
;
515 host
.h_length
= INADDRSZ
;
519 return(gethostanswer((const querybuf
*)answer
, anslen
, qname
, qtype
));
523 _gethostbydnsname(const char *name
, int af
)
526 register const char *cp
;
528 int n
, size
, type
, len
;
530 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
531 h_errno
= NETDB_INTERNAL
;
545 h_errno
= NETDB_INTERNAL
;
546 errno
= EAFNOSUPPORT
;
550 host
.h_addrtype
= af
;
551 host
.h_length
= size
;
554 * if there aren't any dots, it could be a user-level alias.
555 * this is also done in res_query() since we are not the only
556 * function that looks up host names.
558 if (!strchr(name
, '.') && ( NULL
!= (cp
= __hostalias(name
))))
562 * disallow names consisting only of digits/dots, unless
565 if (isdigit(name
[0]))
566 for (cp
= name
;; ++cp
) {
571 * All-numeric, no dot at the end.
572 * Fake up a hostent as if we'd actually
575 if (inet_pton(af
, name
, host_addr
) <= 0) {
576 h_errno
= HOST_NOT_FOUND
;
579 strncpy(hostbuf
, name
, MAXDNAME
);
580 hostbuf
[MAXDNAME
] = '\0';
581 bp
= hostbuf
+ MAXDNAME
;
582 len
= sizeof hostbuf
- MAXDNAME
;
583 host
.h_name
= hostbuf
;
584 host
.h_aliases
= host_aliases
;
585 host_aliases
[0] = NULL
;
586 h_addr_ptrs
[0] = (char *)host_addr
;
587 h_addr_ptrs
[1] = NULL
;
588 host
.h_addr_list
= h_addr_ptrs
;
589 if (_res
.options
& RES_USE_INET6
)
590 _map_v4v6_hostent(&host
, &bp
, &len
);
591 h_errno
= NETDB_SUCCESS
;
594 if (!isdigit(*cp
) && *cp
!= '.')
597 if ((isxdigit(name
[0]) && strchr(name
, ':') != NULL
) ||
599 for (cp
= name
;; ++cp
) {
604 * All-IPv6-legal, no dot at the end.
605 * Fake up a hostent as if we'd actually
608 if (inet_pton(af
, name
, host_addr
) <= 0) {
609 h_errno
= HOST_NOT_FOUND
;
612 strncpy(hostbuf
, name
, MAXDNAME
);
613 hostbuf
[MAXDNAME
] = '\0';
614 bp
= hostbuf
+ MAXDNAME
;
615 len
= sizeof hostbuf
- MAXDNAME
;
616 host
.h_name
= hostbuf
;
617 host
.h_aliases
= host_aliases
;
618 host_aliases
[0] = NULL
;
619 h_addr_ptrs
[0] = (char *)host_addr
;
620 h_addr_ptrs
[1] = NULL
;
621 host
.h_addr_list
= h_addr_ptrs
;
622 h_errno
= NETDB_SUCCESS
;
625 if (!isxdigit(*cp
) && *cp
!= ':' && *cp
!= '.')
629 if ((n
= res_search(name
, C_IN
, type
, buf
.buf
, sizeof(buf
))) < 0) {
630 dprintf("res_search failed (%d)\n", n
);
633 return (gethostanswer(&buf
, n
, name
, type
));
637 _gethostbydnsaddr(const char *addr
, int len
, int af
)
639 const u_char
*uaddr
= (const u_char
*)addr
;
640 static const u_char mapped
[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
641 static const u_char tunnelled
[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
644 register struct hostent
*hp
;
645 char qbuf
[MAXDNAME
+1], *qp
;
647 register struct hostent
*rhp
;
650 char hname2
[MAXDNAME
+1];
651 #endif /*SUNSECURITY*/
653 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
654 h_errno
= NETDB_INTERNAL
;
657 if (af
== AF_INET6
&& len
== IN6ADDRSZ
&&
658 (!bcmp(uaddr
, mapped
, sizeof mapped
) ||
659 !bcmp(uaddr
, tunnelled
, sizeof tunnelled
))) {
661 addr
+= sizeof mapped
;
662 uaddr
+= sizeof mapped
;
674 errno
= EAFNOSUPPORT
;
675 h_errno
= NETDB_INTERNAL
;
680 h_errno
= NETDB_INTERNAL
;
685 (void) sprintf(qbuf
, "%u.%u.%u.%u.in-addr.arpa",
693 for (n
= IN6ADDRSZ
- 1; n
>= 0; n
--) {
694 qp
+= SPRINTF((qp
, "%x.%x.",
696 (uaddr
[n
] >> 4) & 0xf));
698 strcpy(qp
, "ip6.int");
703 n
= res_query(qbuf
, C_IN
, T_PTR
, (u_char
*)buf
.buf
, sizeof buf
.buf
);
705 dprintf("res_query failed (%d)\n", n
);
708 if ( NULL
== (hp
= gethostanswer(&buf
, n
, qbuf
, T_PTR
)))
709 return (NULL
); /* h_errno was set by gethostanswer() */
713 * turn off search as the name should be absolute,
714 * 'localhost' should be matched by defnames
716 strncpy(hname2
, hp
->h_name
, MAXDNAME
);
717 hname2
[MAXDNAME
] = '\0';
718 old_options
= _res
.options
;
719 _res
.options
&= ~RES_DNSRCH
;
720 _res
.options
|= RES_DEFNAMES
;
721 if (!(rhp
= gethostbyname(hname2
))) {
723 syslog(LOG_NOTICE
|LOG_AUTH
,
724 "gethostbyaddr: No A record for %s (verifying [%s])",
725 hname2
, inet_ntoa(*((struct in_addr
*)addr
)));
727 _res
.options
= old_options
;
728 h_errno
= HOST_NOT_FOUND
;
731 _res
.options
= old_options
;
732 for (haddr
= rhp
->h_addr_list
; *haddr
; haddr
++)
733 if (!memcmp(*haddr
, addr
, INADDRSZ
))
737 syslog(LOG_NOTICE
|LOG_AUTH
,
738 "gethostbyaddr: A record of %s != PTR record [%s]",
739 hname2
, inet_ntoa(*((struct in_addr
*)addr
)));
741 h_errno
= HOST_NOT_FOUND
;
745 #endif /*SUNSECURITY*/
748 bcopy(addr
, host_addr
, len
);
749 h_addr_ptrs
[0] = (char *)host_addr
;
750 h_addr_ptrs
[1] = NULL
;
751 if (af
== AF_INET
&& (_res
.options
& RES_USE_INET6
)) {
752 _map_v4v6_address((char*)host_addr
, (char*)host_addr
);
753 hp
->h_addrtype
= AF_INET6
;
754 hp
->h_length
= IN6ADDRSZ
;
756 h_errno
= NETDB_SUCCESS
;
762 addrsort(char **ap
, int num
)
766 short aval
[MAXADDRS
];
770 for (i
= 0; i
< num
; i
++, p
++) {
771 for (j
= 0 ; (unsigned)j
< _res
.nsort
; j
++)
772 if (_res
.sort_list
[j
].addr
.s_addr
==
773 (((struct in_addr
*)(*p
))->s_addr
& _res
.sort_list
[j
].mask
))
776 if (needsort
== 0 && i
> 0 && j
< aval
[i
-1])
782 while (needsort
< num
) {
783 for (j
= needsort
- 1; j
>= 0; j
--) {
784 if (aval
[j
] > aval
[j
+1]) {
803 _sethostdnsent(int stayopen
)
805 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1)
808 _res
.options
|= RES_STAYOPEN
| RES_USEVC
;
814 _res
.options
&= ~(RES_STAYOPEN
| RES_USEVC
);