1 /* $NetBSD: getaddrinfo.c,v 1.95 2009/10/02 07:41:08 wiz Exp $ */
2 /* $KAME: getaddrinfo.c,v 1.29 2000/08/31 17:26:57 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Issues to be discussed:
35 * - Return values. There are nonstandard return values defined and used
36 * in the source code. This is because RFC2553 is silent about which error
37 * code must be returned for which situation.
38 * - IPv4 classful (shortened) form. RFC2553 is silent about it. XNET 5.2
39 * says to use inet_aton() to convert IPv4 numeric to binary (alows
40 * classful form as a result).
41 * current code - disallow classful form for IPv4 (due to use of inet_pton).
42 * - freeaddrinfo(NULL). RFC2553 is silent about it. XNET 5.2 says it is
44 * current code - SEGV on freeaddrinfo(NULL)
46 * - The code filters out AFs that are not supported by the kernel,
47 * when globbing NULL hostname (to loopback, or wildcard). Is it the right
48 * thing to do? What is the relationship with post-RFC2553 AI_ADDRCONFIG
50 * - (post-2553) semantics of AI_ADDRCONFIG itself is too vague.
51 * (1) what should we do against numeric hostname (2) what should we do
52 * against NULL hostname (3) what is AI_ADDRCONFIG itself. AF not ready?
53 * non-loopback address configured? global address configured?
56 #include <sys/cdefs.h>
57 #if defined(LIBC_SCCS) && !defined(lint)
58 __RCSID("$NetBSD: getaddrinfo.c,v 1.95 2009/10/02 07:41:08 wiz Exp $");
59 #endif /* LIBC_SCCS and not lint */
61 #include "namespace.h"
62 #include <sys/types.h>
63 #include <sys/param.h>
64 #include <sys/socket.h>
66 #include <netinet/in.h>
67 #include <arpa/inet.h>
68 #include <arpa/nameser.h>
86 #include <rpcsvc/yp_prot.h>
87 #include <rpcsvc/ypclnt.h>
93 __weak_alias(getaddrinfo
,_getaddrinfo
)
94 __weak_alias(freeaddrinfo
,_freeaddrinfo
)
95 __weak_alias(gai_strerror
,_gai_strerror
)
103 static const char in_addrany
[] = { 0, 0, 0, 0 };
104 static const char in_loopback
[] = { 127, 0, 0, 1 };
106 static const char in6_addrany
[] = {
107 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
109 static const char in6_loopback
[] = {
110 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
114 static const struct afd
{
119 const char *a_addrany
;
120 const char *a_loopback
;
124 {PF_INET6
, sizeof(struct in6_addr
),
125 sizeof(struct sockaddr_in6
),
126 offsetof(struct sockaddr_in6
, sin6_addr
),
127 in6_addrany
, in6_loopback
, 1},
129 {PF_INET
, sizeof(struct in_addr
),
130 sizeof(struct sockaddr_in
),
131 offsetof(struct sockaddr_in
, sin_addr
),
132 in_addrany
, in_loopback
, 0},
133 {0, 0, 0, 0, NULL
, NULL
, 0},
140 const char *e_protostr
;
142 #define WILD_AF(ex) ((ex)->e_wild & 0x01)
143 #define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02)
144 #define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04)
147 static const struct explore explore
[] = {
149 { PF_LOCAL
, 0, ANY
, ANY
, NULL
, 0x01 },
152 { PF_INET6
, SOCK_DGRAM
, IPPROTO_UDP
, "udp", 0x07 },
153 { PF_INET6
, SOCK_STREAM
, IPPROTO_TCP
, "tcp", 0x07 },
154 { PF_INET6
, SOCK_RAW
, ANY
, NULL
, 0x05 },
156 { PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, "udp", 0x07 },
157 { PF_INET
, SOCK_STREAM
, IPPROTO_TCP
, "tcp", 0x07 },
158 { PF_INET
, SOCK_RAW
, ANY
, NULL
, 0x05 },
159 { PF_UNSPEC
, SOCK_DGRAM
, IPPROTO_UDP
, "udp", 0x07 },
160 { PF_UNSPEC
, SOCK_STREAM
, IPPROTO_TCP
, "tcp", 0x07 },
161 { PF_UNSPEC
, SOCK_RAW
, ANY
, NULL
, 0x05 },
162 { -1, 0, 0, NULL
, 0 },
171 static const ns_src default_dns_files
[] = {
172 { NSSRC_FILES
, NS_SUCCESS
},
173 { NSSRC_DNS
, NS_SUCCESS
},
177 #define MAXPACKET (64*1024)
181 u_char buf
[MAXPACKET
];
185 struct res_target
*next
;
186 const char *name
; /* domain name */
187 int qclass
, qtype
; /* class and type of query */
188 u_char
*answer
; /* buffer to put answer */
189 int anslen
; /* size of answer buffer */
190 int n
; /* result length */
193 static int str2number(const char *);
194 static int explore_fqdn(const struct addrinfo
*, const char *,
195 const char *, struct addrinfo
**, struct servent_data
*);
196 static int explore_null(const struct addrinfo
*,
197 const char *, struct addrinfo
**, struct servent_data
*);
198 static int explore_numeric(const struct addrinfo
*, const char *,
199 const char *, struct addrinfo
**, const char *, struct servent_data
*);
200 static int explore_numeric_scope(const struct addrinfo
*, const char *,
201 const char *, struct addrinfo
**, struct servent_data
*);
202 static int get_canonname(const struct addrinfo
*,
203 struct addrinfo
*, const char *);
204 static struct addrinfo
*get_ai(const struct addrinfo
*,
205 const struct afd
*, const char *);
206 static int get_portmatch(const struct addrinfo
*, const char *,
207 struct servent_data
*);
208 static int get_port(const struct addrinfo
*, const char *, int,
209 struct servent_data
*);
210 static const struct afd
*find_afd(int);
212 static int ip6_str2scopeid(char *, struct sockaddr_in6
*, u_int32_t
*);
215 static struct addrinfo
*getanswer(const querybuf
*, int, const char *, int,
216 const struct addrinfo
*);
217 static void aisort(struct addrinfo
*s
, res_state res
);
218 static int _dns_getaddrinfo(void *, void *, va_list);
219 static void _sethtent(FILE **);
220 static void _endhtent(FILE **);
221 static struct addrinfo
*_gethtent(FILE **, const char *,
222 const struct addrinfo
*);
223 static int _files_getaddrinfo(void *, void *, va_list);
225 static struct addrinfo
*_yphostent(char *, const struct addrinfo
*);
226 static int _yp_getaddrinfo(void *, void *, va_list);
229 static int res_queryN(const char *, struct res_target
*, res_state
);
230 static int res_searchN(const char *, struct res_target
*, res_state
);
231 static int res_querydomainN(const char *, const char *,
232 struct res_target
*, res_state
);
234 static const char * const ai_errlist
[] = {
236 "Address family for hostname not supported", /* EAI_ADDRFAMILY */
237 "Temporary failure in name resolution", /* EAI_AGAIN */
238 "Invalid value for ai_flags", /* EAI_BADFLAGS */
239 "Non-recoverable failure in name resolution", /* EAI_FAIL */
240 "ai_family not supported", /* EAI_FAMILY */
241 "Memory allocation failure", /* EAI_MEMORY */
242 "No address associated with hostname", /* EAI_NODATA */
243 "hostname nor servname provided, or not known", /* EAI_NONAME */
244 "servname not supported for ai_socktype", /* EAI_SERVICE */
245 "ai_socktype not supported", /* EAI_SOCKTYPE */
246 "System error returned in errno", /* EAI_SYSTEM */
247 "Invalid value for hints", /* EAI_BADHINTS */
248 "Resolved protocol is unknown", /* EAI_PROTOCOL */
249 "Argument buffer overflow", /* EAI_OVERFLOW */
250 "Unknown error", /* EAI_MAX */
253 /* XXX macros that make external reference is BAD. */
255 #define GET_AI(ai, afd, addr) \
257 /* external reference: pai, error, and label free */ \
258 (ai) = get_ai(pai, (afd), (addr)); \
259 if ((ai) == NULL) { \
260 error = EAI_MEMORY; \
263 } while (/*CONSTCOND*/0)
265 #define GET_PORT(ai, serv, svd) \
267 /* external reference: error and label free */ \
268 error = get_port((ai), (serv), 0, (svd)); \
271 } while (/*CONSTCOND*/0)
273 #define GET_CANONNAME(ai, str) \
275 /* external reference: pai, error and label free */ \
276 error = get_canonname(pai, (ai), (str)); \
279 } while (/*CONSTCOND*/0)
283 /* external reference: error, and label bad */ \
287 } while (/*CONSTCOND*/0)
289 #define MATCH_FAMILY(x, y, w) \
290 ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == PF_UNSPEC || \
292 #define MATCH(x, y, w) \
293 ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY)))
296 gai_strerror(int ecode
)
298 if (ecode
< 0 || ecode
> EAI_MAX
)
300 return ai_errlist
[ecode
];
304 freeaddrinfo(struct addrinfo
*ai
)
306 struct addrinfo
*next
;
308 _DIAGASSERT(ai
!= NULL
);
312 if (ai
->ai_canonname
)
313 free(ai
->ai_canonname
);
314 /* no need to free(ai->ai_addr) */
321 str2number(const char *p
)
326 _DIAGASSERT(p
!= NULL
);
332 v
= strtoul(p
, &ep
, 10);
333 if (errno
== 0 && ep
&& *ep
== '\0' && v
<= UINT_MAX
)
340 getaddrinfo(const char *hostname
, const char *servname
,
341 const struct addrinfo
*hints
, struct addrinfo
**res
)
343 struct addrinfo sentinel
;
344 struct addrinfo
*cur
;
348 struct addrinfo
*pai
;
349 const struct explore
*ex
;
350 struct servent_data svd
;
352 /* hostname is allowed to be NULL */
353 /* servname is allowed to be NULL */
354 /* hints is allowed to be NULL */
355 _DIAGASSERT(res
!= NULL
);
357 (void)memset(&svd
, 0, sizeof(svd
));
358 memset(&sentinel
, 0, sizeof(sentinel
));
360 memset(&ai
, 0, sizeof(ai
));
363 pai
->ai_family
= PF_UNSPEC
;
364 pai
->ai_socktype
= ANY
;
365 pai
->ai_protocol
= ANY
;
367 pai
->ai_canonname
= NULL
;
371 if (hostname
== NULL
&& servname
== NULL
)
374 /* error check for hints */
375 if (hints
->ai_addrlen
|| hints
->ai_canonname
||
376 hints
->ai_addr
|| hints
->ai_next
)
377 ERR(EAI_BADHINTS
); /* xxx */
378 if (hints
->ai_flags
& ~AI_MASK
)
380 switch (hints
->ai_family
) {
390 memcpy(pai
, hints
, sizeof(*pai
));
393 * if both socktype/protocol are specified, check if they
394 * are meaningful combination.
396 if (pai
->ai_socktype
!= ANY
&& pai
->ai_protocol
!= ANY
) {
397 for (ex
= explore
; ex
->e_af
>= 0; ex
++) {
398 if (pai
->ai_family
!= ex
->e_af
)
400 if (ex
->e_socktype
== ANY
)
402 if (ex
->e_protocol
== ANY
)
404 if (pai
->ai_socktype
== ex
->e_socktype
405 && pai
->ai_protocol
!= ex
->e_protocol
) {
413 * check for special cases. (1) numeric servname is disallowed if
414 * socktype/protocol are left unspecified. (2) servname is disallowed
415 * for raw and other inet{,6} sockets.
417 if (MATCH_FAMILY(pai
->ai_family
, PF_INET
, 1)
419 || MATCH_FAMILY(pai
->ai_family
, PF_INET6
, 1)
422 ai0
= *pai
; /* backup *pai */
424 if (pai
->ai_family
== PF_UNSPEC
) {
426 pai
->ai_family
= PF_INET6
;
428 pai
->ai_family
= PF_INET
;
431 error
= get_portmatch(pai
, servname
, &svd
);
440 /* NULL hostname, or numeric hostname */
441 for (ex
= explore
; ex
->e_af
>= 0; ex
++) {
444 /* PF_UNSPEC entries are prepared for DNS queries only */
445 if (ex
->e_af
== PF_UNSPEC
)
448 if (!MATCH_FAMILY(pai
->ai_family
, ex
->e_af
, WILD_AF(ex
)))
450 if (!MATCH(pai
->ai_socktype
, ex
->e_socktype
, WILD_SOCKTYPE(ex
)))
452 if (!MATCH(pai
->ai_protocol
, ex
->e_protocol
, WILD_PROTOCOL(ex
)))
455 if (pai
->ai_family
== PF_UNSPEC
)
456 pai
->ai_family
= ex
->e_af
;
457 if (pai
->ai_socktype
== ANY
&& ex
->e_socktype
!= ANY
)
458 pai
->ai_socktype
= ex
->e_socktype
;
459 if (pai
->ai_protocol
== ANY
&& ex
->e_protocol
!= ANY
)
460 pai
->ai_protocol
= ex
->e_protocol
;
462 if (hostname
== NULL
)
463 error
= explore_null(pai
, servname
, &cur
->ai_next
,
466 error
= explore_numeric_scope(pai
, hostname
, servname
,
467 &cur
->ai_next
, &svd
);
478 * If numeric representation of AF1 can be interpreted as FQDN
479 * representation of AF2, we need to think again about the code below.
481 if (sentinel
.ai_next
)
484 if (hostname
== NULL
)
486 if (pai
->ai_flags
& AI_NUMERICHOST
)
490 * hostname as alphabetical name.
491 * we would like to prefer AF_INET6 than AF_INET, so we'll make a
494 for (ex
= explore
; ex
->e_af
>= 0; ex
++) {
497 /* require exact match for family field */
498 if (pai
->ai_family
!= ex
->e_af
)
501 if (!MATCH(pai
->ai_socktype
, ex
->e_socktype
,
502 WILD_SOCKTYPE(ex
))) {
505 if (!MATCH(pai
->ai_protocol
, ex
->e_protocol
,
506 WILD_PROTOCOL(ex
))) {
510 if (pai
->ai_socktype
== ANY
&& ex
->e_socktype
!= ANY
)
511 pai
->ai_socktype
= ex
->e_socktype
;
512 if (pai
->ai_protocol
== ANY
&& ex
->e_protocol
!= ANY
)
513 pai
->ai_protocol
= ex
->e_protocol
;
515 error
= explore_fqdn(pai
, hostname
, servname
, &cur
->ai_next
,
518 while (cur
&& cur
->ai_next
)
523 if (sentinel
.ai_next
)
529 if (sentinel
.ai_next
) {
532 *res
= sentinel
.ai_next
;
539 if (sentinel
.ai_next
)
540 freeaddrinfo(sentinel
.ai_next
);
546 * FQDN hostname, DNS lookup
549 explore_fqdn(const struct addrinfo
*pai
, const char *hostname
,
550 const char *servname
, struct addrinfo
**res
, struct servent_data
*svd
)
552 struct addrinfo
*result
;
553 struct addrinfo
*cur
;
555 static const ns_dtab dtab
[] = {
556 NS_FILES_CB(_files_getaddrinfo
, NULL
)
557 { NSSRC_DNS
, _dns_getaddrinfo
, NULL
}, /* force -DHESIOD */
558 NS_NIS_CB(_yp_getaddrinfo
, NULL
)
562 _DIAGASSERT(pai
!= NULL
);
563 /* hostname may be NULL */
564 /* servname may be NULL */
565 _DIAGASSERT(res
!= NULL
);
570 * if the servname does not match socktype/protocol, ignore it.
572 if (get_portmatch(pai
, servname
, svd
) != 0)
575 switch (nsdispatch(&result
, dtab
, NSDB_HOSTS
, "getaddrinfo",
576 default_dns_files
, hostname
, pai
)) {
588 for (cur
= result
; cur
; cur
= cur
->ai_next
) {
589 GET_PORT(cur
, servname
, svd
);
590 /* canonname should be filled already */
601 freeaddrinfo(result
);
607 * passive socket -> anyaddr (0.0.0.0 or ::)
608 * non-passive socket -> localhost (127.0.0.1 or ::1)
611 explore_null(const struct addrinfo
*pai
, const char *servname
,
612 struct addrinfo
**res
, struct servent_data
*svd
)
615 const struct afd
*afd
;
616 struct addrinfo
*cur
;
617 struct addrinfo sentinel
;
620 _DIAGASSERT(pai
!= NULL
);
621 /* servname may be NULL */
622 _DIAGASSERT(res
!= NULL
);
625 sentinel
.ai_next
= NULL
;
629 * filter out AFs that are not supported by the kernel
632 s
= socket(pai
->ai_family
, SOCK_DGRAM
, 0);
640 * if the servname does not match socktype/protocol, ignore it.
642 if (get_portmatch(pai
, servname
, svd
) != 0)
645 afd
= find_afd(pai
->ai_family
);
649 if (pai
->ai_flags
& AI_PASSIVE
) {
650 GET_AI(cur
->ai_next
, afd
, afd
->a_addrany
);
652 * GET_CANONNAME(cur->ai_next, "anyaddr");
654 GET_PORT(cur
->ai_next
, servname
, svd
);
656 GET_AI(cur
->ai_next
, afd
, afd
->a_loopback
);
658 * GET_CANONNAME(cur->ai_next, "localhost");
660 GET_PORT(cur
->ai_next
, servname
, svd
);
664 *res
= sentinel
.ai_next
;
668 if (sentinel
.ai_next
)
669 freeaddrinfo(sentinel
.ai_next
);
677 explore_numeric(const struct addrinfo
*pai
, const char *hostname
,
678 const char *servname
, struct addrinfo
**res
, const char *canonname
,
679 struct servent_data
*svd
)
681 const struct afd
*afd
;
682 struct addrinfo
*cur
;
683 struct addrinfo sentinel
;
687 _DIAGASSERT(pai
!= NULL
);
688 /* hostname may be NULL */
689 /* servname may be NULL */
690 _DIAGASSERT(res
!= NULL
);
693 sentinel
.ai_next
= NULL
;
697 * if the servname does not match socktype/protocol, ignore it.
699 if (get_portmatch(pai
, servname
, svd
) != 0)
702 afd
= find_afd(pai
->ai_family
);
707 #if 0 /*X/Open spec*/
709 if (inet_aton(hostname
, (struct in_addr
*)pton
) == 1) {
710 if (pai
->ai_family
== afd
->a_af
||
711 pai
->ai_family
== PF_UNSPEC
/*?*/) {
712 GET_AI(cur
->ai_next
, afd
, pton
);
713 GET_PORT(cur
->ai_next
, servname
, svd
);
714 if ((pai
->ai_flags
& AI_CANONNAME
)) {
716 * Set the numeric address itself as
717 * the canonical name, based on a
718 * clarification in rfc2553bis-03.
720 GET_CANONNAME(cur
->ai_next
, canonname
);
722 while (cur
&& cur
->ai_next
)
725 ERR(EAI_FAMILY
); /*xxx*/
730 if (inet_pton(afd
->a_af
, hostname
, pton
) == 1) {
731 if (pai
->ai_family
== afd
->a_af
||
732 pai
->ai_family
== PF_UNSPEC
/*?*/) {
733 GET_AI(cur
->ai_next
, afd
, pton
);
734 GET_PORT(cur
->ai_next
, servname
, svd
);
735 if ((pai
->ai_flags
& AI_CANONNAME
)) {
737 * Set the numeric address itself as
738 * the canonical name, based on a
739 * clarification in rfc2553bis-03.
741 GET_CANONNAME(cur
->ai_next
, canonname
);
746 ERR(EAI_FAMILY
); /*xxx*/
751 *res
= sentinel
.ai_next
;
756 if (sentinel
.ai_next
)
757 freeaddrinfo(sentinel
.ai_next
);
762 * numeric hostname with scope
765 explore_numeric_scope(const struct addrinfo
*pai
, const char *hostname
,
766 const char *servname
, struct addrinfo
**res
, struct servent_data
*svd
)
768 #if !defined(SCOPE_DELIMITER) || !defined(INET6)
769 return explore_numeric(pai
, hostname
, servname
, res
, hostname
, svd
);
771 const struct afd
*afd
;
772 struct addrinfo
*cur
;
774 char *cp
, *hostname2
= NULL
, *scope
, *addr
;
775 struct sockaddr_in6
*sin6
;
777 _DIAGASSERT(pai
!= NULL
);
778 /* hostname may be NULL */
779 /* servname may be NULL */
780 _DIAGASSERT(res
!= NULL
);
783 * if the servname does not match socktype/protocol, ignore it.
785 if (get_portmatch(pai
, servname
, svd
) != 0)
788 afd
= find_afd(pai
->ai_family
);
793 return explore_numeric(pai
, hostname
, servname
, res
, hostname
,
796 cp
= strchr(hostname
, SCOPE_DELIMITER
);
798 return explore_numeric(pai
, hostname
, servname
, res
, hostname
,
802 * Handle special case of <scoped_address><delimiter><scope id>
804 hostname2
= strdup(hostname
);
805 if (hostname2
== NULL
)
807 /* terminate at the delimiter */
808 hostname2
[cp
- hostname
] = '\0';
812 error
= explore_numeric(pai
, addr
, servname
, res
, hostname
, svd
);
816 for (cur
= *res
; cur
; cur
= cur
->ai_next
) {
817 if (cur
->ai_family
!= AF_INET6
)
819 sin6
= (struct sockaddr_in6
*)(void *)cur
->ai_addr
;
820 if (ip6_str2scopeid(scope
, sin6
, &scopeid
) == -1) {
822 return(EAI_NODATA
); /* XXX: is return OK? */
824 sin6
->sin6_scope_id
= scopeid
;
835 get_canonname(const struct addrinfo
*pai
, struct addrinfo
*ai
, const char *str
)
838 _DIAGASSERT(pai
!= NULL
);
839 _DIAGASSERT(ai
!= NULL
);
840 _DIAGASSERT(str
!= NULL
);
842 if ((pai
->ai_flags
& AI_CANONNAME
) != 0) {
843 ai
->ai_canonname
= strdup(str
);
844 if (ai
->ai_canonname
== NULL
)
851 allocaddrinfo(socklen_t addrlen
)
855 ai
= calloc(sizeof(struct addrinfo
) + addrlen
, 1);
857 ai
->ai_addr
= (void *)(ai
+1);
859 ai
->ai_addrlen
= ai
->ai_addr
->sa_len
= addrlen
;
861 ai
->ai_addrlen
= addrlen
;
869 static struct addrinfo
*
870 get_ai(const struct addrinfo
*pai
, const struct afd
*afd
, const char *addr
)
874 struct sockaddr
*save
;
876 _DIAGASSERT(pai
!= NULL
);
877 _DIAGASSERT(afd
!= NULL
);
878 _DIAGASSERT(addr
!= NULL
);
880 ai
= allocaddrinfo((socklen_t
)afd
->a_socklen
);
885 memcpy(ai
, pai
, sizeof(struct addrinfo
));
887 /* since we just overwrote all of ai, we have
888 to restore ai_addr and ai_addrlen */
890 ai
->ai_addrlen
= (socklen_t
)afd
->a_socklen
;
892 ai
->ai_addr
->sa_family
= ai
->ai_family
= afd
->a_af
;
893 p
= (char *)(void *)(ai
->ai_addr
);
894 memcpy(p
+ afd
->a_off
, addr
, (size_t)afd
->a_addrlen
);
899 get_portmatch(const struct addrinfo
*ai
, const char *servname
,
900 struct servent_data
*svd
)
903 _DIAGASSERT(ai
!= NULL
);
904 /* servname may be NULL */
906 return get_port(ai
, servname
, 1, svd
);
910 get_port(const struct addrinfo
*ai
, const char *servname
, int matchonly
,
911 struct servent_data
*svd
)
918 _DIAGASSERT(ai
!= NULL
);
919 /* servname may be NULL */
921 if (servname
== NULL
)
923 switch (ai
->ai_family
) {
933 switch (ai
->ai_socktype
) {
942 * This was 0. It is now 1 so that queries specifying
943 * a NULL hint, or hint without socktype (but, hopefully,
944 * with protocol) and numeric address actually work.
952 port
= str2number(servname
);
956 if (port
< 0 || port
> 65535)
961 if (ai
->ai_flags
& AI_NUMERICSERV
)
964 switch (ai
->ai_socktype
) {
976 sp
= getservbyname_r(servname
, proto
, &sv
, svd
);
983 switch (ai
->ai_family
) {
985 ((struct sockaddr_in
*)(void *)
986 ai
->ai_addr
)->sin_port
= port
;
990 ((struct sockaddr_in6
*)(void *)
991 ai
->ai_addr
)->sin6_port
= port
;
1000 static const struct afd
*
1003 const struct afd
*afd
;
1005 if (af
== PF_UNSPEC
)
1007 for (afd
= afdl
; afd
->a_af
; afd
++) {
1008 if (afd
->a_af
== af
)
1015 /* convert a string to a scope identifier. XXX: IPv6 specific */
1017 ip6_str2scopeid(char *scope
, struct sockaddr_in6
*sin6
, u_int32_t
*scopeid
)
1020 struct in6_addr
*a6
;
1023 _DIAGASSERT(scope
!= NULL
);
1024 _DIAGASSERT(sin6
!= NULL
);
1025 _DIAGASSERT(scopeid
!= NULL
);
1027 a6
= &sin6
->sin6_addr
;
1029 /* empty scopeid portion is invalid */
1033 if (IN6_IS_ADDR_LINKLOCAL(a6
) || IN6_IS_ADDR_MC_LINKLOCAL(a6
)) {
1035 * We currently assume a one-to-one mapping between links
1036 * and interfaces, so we simply use interface indices for
1037 * like-local scopes.
1039 *scopeid
= if_nametoindex(scope
);
1045 /* still unclear about literal, allow numeric only - placeholder */
1046 if (IN6_IS_ADDR_SITELOCAL(a6
) || IN6_IS_ADDR_MC_SITELOCAL(a6
))
1048 if (IN6_IS_ADDR_MC_ORGLOCAL(a6
))
1051 goto trynumeric
; /* global */
1053 /* try to convert to a numeric id as a last resort */
1056 lscopeid
= strtoul(scope
, &ep
, 10);
1057 *scopeid
= (u_int32_t
)(lscopeid
& 0xffffffffUL
);
1058 if (errno
== 0 && ep
&& *ep
== '\0' && *scopeid
== lscopeid
)
1065 /* code duplicate with gethnamaddr.c */
1067 static const char AskedForGot
[] =
1068 "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
1070 static struct addrinfo
*
1071 getanswer(const querybuf
*answer
, int anslen
, const char *qname
, int qtype
,
1072 const struct addrinfo
*pai
)
1074 struct addrinfo sentinel
, *cur
;
1076 const struct afd
*afd
;
1083 int type
, class, ancount
, qdcount
;
1084 int haveanswer
, had_error
;
1085 char tbuf
[MAXDNAME
];
1086 int (*name_ok
) (const char *);
1087 char hostbuf
[8*1024];
1089 _DIAGASSERT(answer
!= NULL
);
1090 _DIAGASSERT(qname
!= NULL
);
1091 _DIAGASSERT(pai
!= NULL
);
1093 memset(&sentinel
, 0, sizeof(sentinel
));
1097 eom
= answer
->buf
+ anslen
;
1101 case T_ANY
: /*use T_ANY only for T_A/T_AAAA lookup*/
1105 return NULL
; /* XXX should be abort(); */
1108 * find first satisfactory answer
1111 ancount
= ntohs(hp
->ancount
);
1112 qdcount
= ntohs(hp
->qdcount
);
1114 ep
= hostbuf
+ sizeof hostbuf
;
1115 cp
= answer
->buf
+ HFIXEDSZ
;
1117 h_errno
= NO_RECOVERY
;
1120 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, ep
- bp
);
1121 if ((n
< 0) || !(*name_ok
)(bp
)) {
1122 h_errno
= NO_RECOVERY
;
1126 if (qtype
== T_A
|| qtype
== T_AAAA
|| qtype
== T_ANY
) {
1127 /* res_send() has already verified that the query name is the
1128 * same as the one we sent; this just gets the expanded name
1129 * (i.e., with the succeeding search-domain tacked on).
1131 n
= strlen(bp
) + 1; /* for the \0 */
1132 if (n
>= MAXHOSTNAMELEN
) {
1133 h_errno
= NO_RECOVERY
;
1138 /* The qname can be abbreviated, but h_name is now absolute. */
1143 while (ancount
-- > 0 && cp
< eom
&& !had_error
) {
1144 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, ep
- bp
);
1145 if ((n
< 0) || !(*name_ok
)(bp
)) {
1150 type
= _getshort(cp
);
1151 cp
+= INT16SZ
; /* type */
1152 class = _getshort(cp
);
1153 cp
+= INT16SZ
+ INT32SZ
; /* class, TTL */
1155 cp
+= INT16SZ
; /* len */
1156 if (class != C_IN
) {
1157 /* XXX - debug? syslog? */
1159 continue; /* XXX - had_error++ ? */
1161 if ((qtype
== T_A
|| qtype
== T_AAAA
|| qtype
== T_ANY
) &&
1163 n
= dn_expand(answer
->buf
, eom
, cp
, tbuf
, sizeof tbuf
);
1164 if ((n
< 0) || !(*name_ok
)(tbuf
)) {
1169 /* Get canonical name. */
1170 n
= strlen(tbuf
) + 1; /* for the \0 */
1171 if (n
> ep
- bp
|| n
>= MAXHOSTNAMELEN
) {
1175 strlcpy(bp
, tbuf
, (size_t)(ep
- bp
));
1180 if (qtype
== T_ANY
) {
1181 if (!(type
== T_A
|| type
== T_AAAA
)) {
1185 } else if (type
!= qtype
) {
1186 if (type
!= T_KEY
&& type
!= T_SIG
) {
1187 struct syslog_data sd
= SYSLOG_DATA_INIT
;
1188 syslog_r(LOG_NOTICE
|LOG_AUTH
, &sd
,
1189 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
1190 qname
, p_class(C_IN
), p_type(qtype
),
1194 continue; /* XXX - had_error++ ? */
1199 if (strcasecmp(canonname
, bp
) != 0) {
1200 struct syslog_data sd
= SYSLOG_DATA_INIT
;
1201 syslog_r(LOG_NOTICE
|LOG_AUTH
, &sd
,
1202 AskedForGot
, canonname
, bp
);
1204 continue; /* XXX - had_error++ ? */
1206 if (type
== T_A
&& n
!= INADDRSZ
) {
1210 if (type
== T_AAAA
&& n
!= IN6ADDRSZ
) {
1214 if (type
== T_AAAA
) {
1215 struct in6_addr in6
;
1216 memcpy(&in6
, cp
, IN6ADDRSZ
);
1217 if (IN6_IS_ADDR_V4MAPPED(&in6
)) {
1226 nn
= strlen(bp
) + 1; /* for the \0 */
1230 /* don't overwrite pai */
1232 ai
.ai_family
= (type
== T_A
) ? AF_INET
: AF_INET6
;
1233 afd
= find_afd(ai
.ai_family
);
1238 cur
->ai_next
= get_ai(&ai
, afd
, (const char *)cp
);
1239 if (cur
->ai_next
== NULL
)
1241 while (cur
&& cur
->ai_next
)
1253 (void)get_canonname(pai
, sentinel
.ai_next
, qname
);
1255 (void)get_canonname(pai
, sentinel
.ai_next
, canonname
);
1256 h_errno
= NETDB_SUCCESS
;
1257 return sentinel
.ai_next
;
1260 h_errno
= NO_RECOVERY
;
1264 #define SORTEDADDR(p) (((struct sockaddr_in *)(void *)(p->ai_next->ai_addr))->sin_addr.s_addr)
1265 #define SORTMATCH(p, s) ((SORTEDADDR(p) & (s).mask) == (s).addr.s_addr)
1268 aisort(struct addrinfo
*s
, res_state res
)
1270 struct addrinfo head
, *t
, *p
;
1273 head
.ai_next
= NULL
;
1276 for (i
= 0; i
< res
->nsort
; i
++) {
1278 while (p
->ai_next
) {
1279 if ((p
->ai_next
->ai_family
!= AF_INET
)
1280 || SORTMATCH(p
, res
->sort_list
[i
])) {
1281 t
->ai_next
= p
->ai_next
;
1283 p
->ai_next
= p
->ai_next
->ai_next
;
1290 /* add rest of list and reset s to the new list*/
1291 t
->ai_next
= s
->ai_next
;
1292 s
->ai_next
= head
.ai_next
;
1297 _dns_getaddrinfo(void *rv
, void *cb_data
, va_list ap
)
1299 struct addrinfo
*ai
;
1300 querybuf
*buf
, *buf2
;
1302 const struct addrinfo
*pai
;
1303 struct addrinfo sentinel
, *cur
;
1304 struct res_target q
, q2
;
1307 name
= va_arg(ap
, char *);
1308 pai
= va_arg(ap
, const struct addrinfo
*);
1310 memset(&q
, 0, sizeof(q
));
1311 memset(&q2
, 0, sizeof(q2
));
1312 memset(&sentinel
, 0, sizeof(sentinel
));
1315 buf
= malloc(sizeof(*buf
));
1317 h_errno
= NETDB_INTERNAL
;
1320 buf2
= malloc(sizeof(*buf2
));
1323 h_errno
= NETDB_INTERNAL
;
1327 switch (pai
->ai_family
) {
1333 q
.answer
= buf
->buf
;
1334 q
.anslen
= sizeof(buf
->buf
);
1339 q2
.answer
= buf2
->buf
;
1340 q2
.anslen
= sizeof(buf2
->buf
);
1346 q
.answer
= buf
->buf
;
1347 q
.anslen
= sizeof(buf
->buf
);
1353 q
.answer
= buf
->buf
;
1354 q
.anslen
= sizeof(buf
->buf
);
1362 res
= __res_get_state();
1369 if (res_searchN(name
, &q
, res
) < 0) {
1370 __res_put_state(res
);
1375 ai
= getanswer(buf
, q
.n
, q
.name
, q
.qtype
, pai
);
1378 while (cur
&& cur
->ai_next
)
1382 ai
= getanswer(buf2
, q2
.n
, q2
.name
, q2
.qtype
, pai
);
1388 if (sentinel
.ai_next
== NULL
) {
1389 __res_put_state(res
);
1391 case HOST_NOT_FOUND
:
1401 aisort(&sentinel
, res
);
1403 __res_put_state(res
);
1405 *((struct addrinfo
**)rv
) = sentinel
.ai_next
;
1410 _sethtent(FILE **hostf
)
1414 *hostf
= fopen(_PATH_HOSTS
, "r" );
1420 _endhtent(FILE **hostf
)
1424 (void) fclose(*hostf
);
1429 static struct addrinfo
*
1430 _gethtent(FILE **hostf
, const char *name
, const struct addrinfo
*pai
)
1433 char *cp
, *tname
, *cname
;
1434 struct addrinfo hints
, *res0
, *res
;
1437 char hostbuf
[8*1024];
1439 _DIAGASSERT(name
!= NULL
);
1440 _DIAGASSERT(pai
!= NULL
);
1442 if (!*hostf
&& !(*hostf
= fopen(_PATH_HOSTS
, "r" )))
1445 if (!(p
= fgets(hostbuf
, sizeof hostbuf
, *hostf
)))
1449 if (!(cp
= strpbrk(p
, "#\n")))
1452 if (!(cp
= strpbrk(p
, " \t")))
1456 /* if this is not something we're looking for, skip it. */
1459 if (*cp
== ' ' || *cp
== '\t') {
1466 if ((cp
= strpbrk(cp
, " \t")) != NULL
)
1468 if (strcasecmp(name
, tname
) == 0)
1475 hints
.ai_flags
= AI_NUMERICHOST
;
1476 error
= getaddrinfo(addr
, NULL
, &hints
, &res0
);
1479 for (res
= res0
; res
; res
= res
->ai_next
) {
1481 res
->ai_flags
= pai
->ai_flags
;
1483 if (pai
->ai_flags
& AI_CANONNAME
) {
1484 if (get_canonname(pai
, res
, cname
) != 0) {
1495 _files_getaddrinfo(void *rv
, void *cb_data
, va_list ap
)
1498 const struct addrinfo
*pai
;
1499 struct addrinfo sentinel
, *cur
;
1506 name
= va_arg(ap
, char *);
1507 pai
= va_arg(ap
, const struct addrinfo
*);
1509 memset(&sentinel
, 0, sizeof(sentinel
));
1513 while ((p
= _gethtent(&hostf
, name
, pai
)) != NULL
) {
1515 while (cur
&& cur
->ai_next
)
1520 *((struct addrinfo
**)rv
) = sentinel
.ai_next
;
1521 if (sentinel
.ai_next
== NULL
)
1528 static struct addrinfo
*
1529 _yphostent(char *line
, const struct addrinfo
*pai
)
1531 struct addrinfo sentinel
, *cur
;
1532 struct addrinfo hints
, *res
, *res0
;
1535 const char *addr
, *canonname
;
1539 _DIAGASSERT(line
!= NULL
);
1540 _DIAGASSERT(pai
!= NULL
);
1543 addr
= canonname
= NULL
;
1545 memset(&sentinel
, 0, sizeof(sentinel
));
1549 /* terminate line */
1550 cp
= strchr(p
, '\n');
1557 cp
= strpbrk(p
, " \t");
1559 if (canonname
== NULL
)
1569 if (*cp
== ' ' || *cp
== '\t') {
1575 if ((cp
= strpbrk(cp
, " \t")) != NULL
)
1580 hints
.ai_flags
= AI_NUMERICHOST
;
1581 error
= getaddrinfo(addr
, NULL
, &hints
, &res0
);
1583 for (res
= res0
; res
; res
= res
->ai_next
) {
1585 res
->ai_flags
= pai
->ai_flags
;
1587 if (pai
->ai_flags
& AI_CANONNAME
)
1588 (void)get_canonname(pai
, res
, canonname
);
1593 cur
->ai_next
= res0
;
1594 while (cur
->ai_next
)
1604 return sentinel
.ai_next
;
1609 _yp_getaddrinfo(void *rv
, void *cb_data
, va_list ap
)
1611 struct addrinfo sentinel
, *cur
;
1612 struct addrinfo
*ai
= NULL
;
1616 const struct addrinfo
*pai
;
1619 if (_yp_check(&ypdomain
) == 0)
1622 name
= va_arg(ap
, char *);
1623 pai
= va_arg(ap
, const struct addrinfo
*);
1625 memset(&sentinel
, 0, sizeof(sentinel
));
1628 /* hosts.byname is only for IPv4 (Solaris8) */
1629 if (pai
->ai_family
== PF_UNSPEC
|| pai
->ai_family
== PF_INET
) {
1630 r
= yp_match(ypdomain
, "hosts.byname", name
,
1631 (int)strlen(name
), &ypbuf
, &ypbuflen
);
1633 struct addrinfo ai4
;
1636 ai4
.ai_family
= AF_INET
;
1637 ai
= _yphostent(ypbuf
, &ai4
);
1640 while (cur
&& cur
->ai_next
)
1647 /* ipnodes.byname can hold both IPv4/v6 */
1648 r
= yp_match(ypdomain
, "ipnodes.byname", name
,
1649 (int)strlen(name
), &ypbuf
, &ypbuflen
);
1651 ai
= _yphostent(ypbuf
, pai
);
1657 if (sentinel
.ai_next
== NULL
) {
1658 h_errno
= HOST_NOT_FOUND
;
1661 *((struct addrinfo
**)rv
) = sentinel
.ai_next
;
1666 /* resolver logic */
1669 * Formulate a normal query, send, and await answer.
1670 * Returned answer is placed in supplied buffer "answer".
1671 * Perform preliminary check of answer, returning success only
1672 * if no error is indicated and the answer count is nonzero.
1673 * Return the size of the response on success, -1 on error.
1674 * Error number is left in h_errno.
1676 * Caller must parse answer and determine whether it answers the question.
1679 res_queryN(const char *name
, /* domain name */ struct res_target
*target
,
1682 u_char buf
[MAXPACKET
];
1685 struct res_target
*t
;
1689 _DIAGASSERT(name
!= NULL
);
1690 /* XXX: target may be NULL??? */
1695 for (t
= target
; t
; t
= t
->next
) {
1700 hp
= (HEADER
*)(void *)t
->answer
;
1701 hp
->rcode
= NOERROR
; /* default */
1703 /* make it easier... */
1709 if (res
->options
& RES_DEBUG
)
1710 printf(";; res_nquery(%s, %d, %d)\n", name
, class, type
);
1713 n
= res_nmkquery(res
, QUERY
, name
, class, type
, NULL
, 0, NULL
,
1715 #ifdef RES_USE_EDNS0
1716 if (n
> 0 && (res
->options
& RES_USE_EDNS0
) != 0)
1717 n
= res_nopt(res
, n
, buf
, sizeof(buf
), anslen
);
1721 if (res
->options
& RES_DEBUG
)
1722 printf(";; res_nquery: mkquery failed\n");
1724 h_errno
= NO_RECOVERY
;
1727 n
= res_nsend(res
, buf
, n
, answer
, anslen
);
1731 if (res
->options
& RES_DEBUG
)
1732 printf(";; res_query: send error\n");
1734 h_errno
= TRY_AGAIN
;
1739 if (n
< 0 || hp
->rcode
!= NOERROR
|| ntohs(hp
->ancount
) == 0) {
1740 rcode
= hp
->rcode
; /* record most recent error */
1742 if (res
->options
& RES_DEBUG
)
1743 printf(";; rcode = %u, ancount=%u\n", hp
->rcode
,
1744 ntohs(hp
->ancount
));
1749 ancount
+= ntohs(hp
->ancount
);
1757 h_errno
= HOST_NOT_FOUND
;
1760 h_errno
= TRY_AGAIN
;
1769 h_errno
= NO_RECOVERY
;
1778 * Formulate a normal query, send, and retrieve answer in supplied buffer.
1779 * Return the size of the response on success, -1 on error.
1780 * If enabled, implement search rules until answer or unrecoverable failure
1781 * is detected. Error code, if any, is left in h_errno.
1784 res_searchN(const char *name
, struct res_target
*target
, res_state res
)
1786 const char *cp
, * const *domain
;
1789 int trailing_dot
, ret
, saved_herrno
;
1790 int got_nodata
= 0, got_servfail
= 0, tried_as_is
= 0;
1792 _DIAGASSERT(name
!= NULL
);
1793 _DIAGASSERT(target
!= NULL
);
1795 hp
= (HEADER
*)(void *)target
->answer
; /*XXX*/
1798 h_errno
= HOST_NOT_FOUND
; /* default, if we never query */
1800 for (cp
= name
; *cp
; cp
++)
1801 dots
+= (*cp
== '.');
1803 if (cp
> name
&& *--cp
== '.')
1807 * if there aren't any dots, it could be a user-level alias
1809 if (!dots
&& (cp
= __hostalias(name
)) != NULL
) {
1810 ret
= res_queryN(cp
, target
, res
);
1815 * If there are dots in the name already, let's just give it a try
1816 * 'as is'. The threshold can be set with the "ndots" option.
1819 if (dots
>= res
->ndots
) {
1820 ret
= res_querydomainN(name
, NULL
, target
, res
);
1823 saved_herrno
= h_errno
;
1828 * We do at least one level of search if
1829 * - there is no dot and RES_DEFNAME is set, or
1830 * - there is at least one dot, there is no trailing dot,
1831 * and RES_DNSRCH is set.
1833 if ((!dots
&& (res
->options
& RES_DEFNAMES
)) ||
1834 (dots
&& !trailing_dot
&& (res
->options
& RES_DNSRCH
))) {
1837 for (domain
= (const char * const *)res
->dnsrch
;
1841 ret
= res_querydomainN(name
, *domain
, target
, res
);
1846 * If no server present, give up.
1847 * If name isn't found in this domain,
1848 * keep trying higher domains in the search list
1849 * (if that's enabled).
1850 * On a NO_DATA error, keep trying, otherwise
1851 * a wildcard entry of another type could keep us
1852 * from finding this entry higher in the domain.
1853 * If we get some other error (negative answer or
1854 * server failure), then stop searching up,
1855 * but try the input name below in case it's
1858 if (errno
== ECONNREFUSED
) {
1859 h_errno
= TRY_AGAIN
;
1867 case HOST_NOT_FOUND
:
1871 if (hp
->rcode
== SERVFAIL
) {
1872 /* try next search element, if any */
1878 /* anything else implies that we're done */
1882 * if we got here for some reason other than DNSRCH,
1883 * we only wanted one iteration of the loop, so stop.
1885 if (!(res
->options
& RES_DNSRCH
))
1891 * if we have not already tried the name "as is", do that now.
1892 * note that we do this regardless of how many dots were in the
1893 * name or whether it ends with a dot.
1896 ret
= res_querydomainN(name
, NULL
, target
, res
);
1902 * if we got here, we didn't satisfy the search.
1903 * if we did an initial full query, return that query's h_errno
1904 * (note that we wouldn't be here if that query had succeeded).
1905 * else if we ever got a nodata, send that back as the reason.
1906 * else send back meaningless h_errno, that being the one from
1907 * the last DNSRCH we did.
1909 if (saved_herrno
!= -1)
1910 h_errno
= saved_herrno
;
1911 else if (got_nodata
)
1913 else if (got_servfail
)
1914 h_errno
= TRY_AGAIN
;
1919 * Perform a call on res_query on the concatenation of name and domain,
1920 * removing a trailing dot from name if domain is NULL.
1923 res_querydomainN(const char *name
, const char *domain
,
1924 struct res_target
*target
, res_state res
)
1926 char nbuf
[MAXDNAME
];
1927 const char *longname
= nbuf
;
1930 _DIAGASSERT(name
!= NULL
);
1931 /* XXX: target may be NULL??? */
1934 if (res
->options
& RES_DEBUG
)
1935 printf(";; res_querydomain(%s, %s)\n",
1936 name
, domain
?domain
:"<Nil>");
1938 if (domain
== NULL
) {
1940 * Check for trailing '.';
1941 * copy without '.' if present.
1944 if (n
+ 1 > sizeof(nbuf
)) {
1945 h_errno
= NO_RECOVERY
;
1948 if (n
> 0 && name
[--n
] == '.') {
1949 strncpy(nbuf
, name
, n
);
1956 if (n
+ 1 + d
+ 1 > sizeof(nbuf
)) {
1957 h_errno
= NO_RECOVERY
;
1960 snprintf(nbuf
, sizeof(nbuf
), "%s.%s", name
, domain
);
1962 return res_queryN(longname
, target
, res
);