1 /* $NetBSD: getaddrinfo.c,v 1.105 2013/05/13 17:54:55 christos 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.105 2013/05/13 17:54:55 christos 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>
87 #include <rpcsvc/yp_prot.h>
88 #include <rpcsvc/ypclnt.h>
94 __weak_alias(getaddrinfo
,_getaddrinfo
)
95 __weak_alias(freeaddrinfo
,_freeaddrinfo
)
96 __weak_alias(gai_strerror
,_gai_strerror
)
104 static const char in_addrany
[] = { 0, 0, 0, 0 };
105 static const char in_loopback
[] = { 127, 0, 0, 1 };
107 static const char in6_addrany
[] = {
108 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
110 static const char in6_loopback
[] = {
111 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
115 static const struct afd
{
120 const char *a_addrany
;
121 const char *a_loopback
;
125 {PF_INET6
, sizeof(struct in6_addr
),
126 sizeof(struct sockaddr_in6
),
127 offsetof(struct sockaddr_in6
, sin6_addr
),
128 in6_addrany
, in6_loopback
, 1},
130 {PF_INET
, sizeof(struct in_addr
),
131 sizeof(struct sockaddr_in
),
132 offsetof(struct sockaddr_in
, sin_addr
),
133 in_addrany
, in_loopback
, 0},
134 {0, 0, 0, 0, NULL
, NULL
, 0},
141 const char *e_protostr
;
143 #define WILD_AF(ex) ((ex)->e_wild & 0x01)
144 #define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02)
145 #define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04)
148 static const struct explore explore
[] = {
150 { PF_LOCAL
, 0, ANY
, ANY
, NULL
, 0x01 },
153 { PF_INET6
, SOCK_DGRAM
, IPPROTO_UDP
, "udp", 0x07 },
154 { PF_INET6
, SOCK_STREAM
, IPPROTO_TCP
, "tcp", 0x07 },
155 { PF_INET6
, SOCK_RAW
, ANY
, NULL
, 0x05 },
157 { PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, "udp", 0x07 },
158 { PF_INET
, SOCK_STREAM
, IPPROTO_TCP
, "tcp", 0x07 },
159 { PF_INET
, SOCK_RAW
, ANY
, NULL
, 0x05 },
160 { PF_UNSPEC
, SOCK_DGRAM
, IPPROTO_UDP
, "udp", 0x07 },
161 { PF_UNSPEC
, SOCK_STREAM
, IPPROTO_TCP
, "tcp", 0x07 },
162 { PF_UNSPEC
, SOCK_RAW
, ANY
, NULL
, 0x05 },
163 { -1, 0, 0, NULL
, 0 },
172 static const ns_src default_dns_files
[] = {
173 { NSSRC_FILES
, NS_SUCCESS
},
174 { NSSRC_DNS
, NS_SUCCESS
},
178 #define MAXPACKET (64*1024)
182 u_char buf
[MAXPACKET
];
186 struct res_target
*next
;
187 const char *name
; /* domain name */
188 int qclass
, qtype
; /* class and type of query */
189 u_char
*answer
; /* buffer to put answer */
190 int anslen
; /* size of answer buffer */
191 int n
; /* result length */
195 struct srvinfo
*next
;
197 int port
, pri
, weight
;
200 static int gai_srvok(const char *);
201 static int str2number(const char *);
202 static int explore_fqdn(const struct addrinfo
*, const char *,
203 const char *, struct addrinfo
**, struct servent_data
*);
204 static int explore_null(const struct addrinfo
*,
205 const char *, struct addrinfo
**, struct servent_data
*);
206 static int explore_numeric(const struct addrinfo
*, const char *,
207 const char *, struct addrinfo
**, const char *, struct servent_data
*);
208 static int explore_numeric_scope(const struct addrinfo
*, const char *,
209 const char *, struct addrinfo
**, struct servent_data
*);
210 static int get_canonname(const struct addrinfo
*,
211 struct addrinfo
*, const char *);
212 static struct addrinfo
*get_ai(const struct addrinfo
*,
213 const struct afd
*, const char *);
214 static int get_portmatch(const struct addrinfo
*, const char *,
215 struct servent_data
*);
216 static int get_port(const struct addrinfo
*, const char *, int,
217 struct servent_data
*);
218 static const struct afd
*find_afd(int);
219 static int addrconfig(uint64_t *);
221 static int ip6_str2scopeid(char *, struct sockaddr_in6
*, u_int32_t
*);
224 static struct addrinfo
*getanswer(const querybuf
*, int, const char *, int,
225 const struct addrinfo
*);
226 static void aisort(struct addrinfo
*s
, res_state res
);
227 static struct addrinfo
* _dns_query(struct res_target
*,
228 const struct addrinfo
*, res_state
, int);
229 static struct addrinfo
* _dns_srv_lookup(const char *, const char *,
230 const struct addrinfo
*);
231 static struct addrinfo
* _dns_host_lookup(const char *,
232 const struct addrinfo
*);
233 static int _dns_getaddrinfo(void *, void *, va_list);
234 static void _sethtent(FILE **);
235 static void _endhtent(FILE **);
236 static struct addrinfo
*_gethtent(FILE **, const char *,
237 const struct addrinfo
*);
238 static int _files_getaddrinfo(void *, void *, va_list);
240 static struct addrinfo
*_yphostent(char *, const struct addrinfo
*);
241 static int _yp_getaddrinfo(void *, void *, va_list);
244 static int res_queryN(const char *, struct res_target
*, res_state
);
245 static int res_searchN(const char *, struct res_target
*, res_state
);
246 static int res_querydomainN(const char *, const char *,
247 struct res_target
*, res_state
);
249 static const char * const ai_errlist
[] = {
251 "Address family for hostname not supported", /* EAI_ADDRFAMILY */
252 "Temporary failure in name resolution", /* EAI_AGAIN */
253 "Invalid value for ai_flags", /* EAI_BADFLAGS */
254 "Non-recoverable failure in name resolution", /* EAI_FAIL */
255 "ai_family not supported", /* EAI_FAMILY */
256 "Memory allocation failure", /* EAI_MEMORY */
257 "No address associated with hostname", /* EAI_NODATA */
258 "hostname nor servname provided, or not known", /* EAI_NONAME */
259 "servname not supported for ai_socktype", /* EAI_SERVICE */
260 "ai_socktype not supported", /* EAI_SOCKTYPE */
261 "System error returned in errno", /* EAI_SYSTEM */
262 "Invalid value for hints", /* EAI_BADHINTS */
263 "Resolved protocol is unknown", /* EAI_PROTOCOL */
264 "Argument buffer overflow", /* EAI_OVERFLOW */
265 "Unknown error", /* EAI_MAX */
268 /* XXX macros that make external reference is BAD. */
270 #define GET_AI(ai, afd, addr) \
272 /* external reference: pai, error, and label free */ \
273 (ai) = get_ai(pai, (afd), (addr)); \
274 if ((ai) == NULL) { \
275 error = EAI_MEMORY; \
278 } while (/*CONSTCOND*/0)
280 #define GET_PORT(ai, serv, svd) \
282 /* external reference: error and label free */ \
283 error = get_port((ai), (serv), 0, (svd)); \
286 } while (/*CONSTCOND*/0)
288 #define GET_CANONNAME(ai, str) \
290 /* external reference: pai, error and label free */ \
291 error = get_canonname(pai, (ai), (str)); \
294 } while (/*CONSTCOND*/0)
298 /* external reference: error, and label bad */ \
302 } while (/*CONSTCOND*/0)
304 #define MATCH_FAMILY(x, y, w) \
305 ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == PF_UNSPEC || \
307 #define MATCH(x, y, w) \
308 ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY)))
311 gai_strerror(int ecode
)
313 if (ecode
< 0 || ecode
> EAI_MAX
)
315 return ai_errlist
[ecode
];
319 freeaddrinfo(struct addrinfo
*ai
)
321 struct addrinfo
*next
;
323 _DIAGASSERT(ai
!= NULL
);
327 if (ai
->ai_canonname
)
328 free(ai
->ai_canonname
);
329 /* no need to free(ai->ai_addr) */
336 * We don't want localization to affect us
339 #define hyphenchar(c) ((c) == '-')
340 #define periodchar(c) ((c) == PERIOD)
341 #define underschar(c) ((c) == '_')
342 #define alphachar(c) (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
343 #define digitchar(c) ((c) >= '0' && (c) <= '9')
345 #define firstchar(c) (alphachar(c) || digitchar(c) || underschar(c))
346 #define lastchar(c) (alphachar(c) || digitchar(c))
347 #define middlechar(c) (lastchar(c) || hyphenchar(c))
350 gai_srvok(const char *dn
)
354 for (pch
= PERIOD
, nch
= ch
= *dn
++; ch
!= '\0'; pch
= ch
, ch
= nch
) {
357 if (periodchar(pch
)) {
360 } else if (periodchar(nch
) || nch
== '\0') {
363 } else if (!middlechar(ch
))
370 getport(struct addrinfo
*ai
) {
373 switch (ai
->ai_family
) {
375 return &((struct sockaddr_in
*)(void *)ai
->ai_addr
)->sin_port
;
378 return &((struct sockaddr_in6
*)(void *)ai
->ai_addr
)->sin6_port
;
388 str2number(const char *p
)
393 _DIAGASSERT(p
!= NULL
);
399 v
= strtoul(p
, &ep
, 10);
400 if (errno
== 0 && ep
&& *ep
== '\0' && v
<= INT_MAX
)
407 getaddrinfo(const char *hostname
, const char *servname
,
408 const struct addrinfo
*hints
, struct addrinfo
**res
)
410 struct addrinfo sentinel
;
411 struct addrinfo
*cur
;
415 struct addrinfo
*pai
;
416 const struct explore
*ex
;
417 struct servent_data svd
;
418 uint64_t mask
= (uint64_t)~0ULL;
420 /* hostname is allowed to be NULL */
421 /* servname is allowed to be NULL */
422 /* hints is allowed to be NULL */
423 _DIAGASSERT(res
!= NULL
);
425 (void)memset(&svd
, 0, sizeof(svd
));
426 memset(&sentinel
, 0, sizeof(sentinel
));
428 memset(&ai
, 0, sizeof(ai
));
431 pai
->ai_family
= PF_UNSPEC
;
432 pai
->ai_socktype
= ANY
;
433 pai
->ai_protocol
= ANY
;
435 pai
->ai_canonname
= NULL
;
439 if (hostname
== NULL
&& servname
== NULL
)
442 /* error check for hints */
443 if (hints
->ai_addrlen
|| hints
->ai_canonname
||
444 hints
->ai_addr
|| hints
->ai_next
)
445 ERR(EAI_BADHINTS
); /* xxx */
446 if (hints
->ai_flags
& ~AI_MASK
)
448 switch (hints
->ai_family
) {
458 memcpy(pai
, hints
, sizeof(*pai
));
461 * if both socktype/protocol are specified, check if they
462 * are meaningful combination.
464 if (pai
->ai_socktype
!= ANY
&& pai
->ai_protocol
!= ANY
) {
465 for (ex
= explore
; ex
->e_af
>= 0; ex
++) {
466 if (pai
->ai_family
!= ex
->e_af
)
468 if (ex
->e_socktype
== ANY
)
470 if (ex
->e_protocol
== ANY
)
472 if (pai
->ai_socktype
== ex
->e_socktype
473 && pai
->ai_protocol
!= ex
->e_protocol
) {
480 if ((pai
->ai_flags
& AI_ADDRCONFIG
) != 0 && addrconfig(&mask
) == -1)
484 * check for special cases. (1) numeric servname is disallowed if
485 * socktype/protocol are left unspecified. (2) servname is disallowed
486 * for raw and other inet{,6} sockets.
488 if (MATCH_FAMILY(pai
->ai_family
, PF_INET
, 1)
490 || MATCH_FAMILY(pai
->ai_family
, PF_INET6
, 1)
493 ai0
= *pai
; /* backup *pai */
495 if (pai
->ai_family
== PF_UNSPEC
) {
497 pai
->ai_family
= PF_INET6
;
499 pai
->ai_family
= PF_INET
;
502 error
= get_portmatch(pai
, servname
, &svd
);
511 /* NULL hostname, or numeric hostname */
512 for (ex
= explore
; ex
->e_af
>= 0; ex
++) {
515 /* ADDRCONFIG check */
516 if ((((uint64_t)1 << ex
->e_af
) & mask
) == 0)
519 /* PF_UNSPEC entries are prepared for DNS queries only */
520 if (ex
->e_af
== PF_UNSPEC
)
523 if (!MATCH_FAMILY(pai
->ai_family
, ex
->e_af
, WILD_AF(ex
)))
525 if (!MATCH(pai
->ai_socktype
, ex
->e_socktype
, WILD_SOCKTYPE(ex
)))
527 if (!MATCH(pai
->ai_protocol
, ex
->e_protocol
, WILD_PROTOCOL(ex
)))
529 if (pai
->ai_family
== PF_UNSPEC
)
530 pai
->ai_family
= ex
->e_af
;
531 if (pai
->ai_socktype
== ANY
&& ex
->e_socktype
!= ANY
)
532 pai
->ai_socktype
= ex
->e_socktype
;
533 if (pai
->ai_protocol
== ANY
&& ex
->e_protocol
!= ANY
)
534 pai
->ai_protocol
= ex
->e_protocol
;
536 if (hostname
== NULL
)
537 error
= explore_null(pai
, servname
, &cur
->ai_next
,
540 error
= explore_numeric_scope(pai
, hostname
, servname
,
541 &cur
->ai_next
, &svd
);
552 * If numeric representation of AF1 can be interpreted as FQDN
553 * representation of AF2, we need to think again about the code below.
555 if (sentinel
.ai_next
)
558 if (hostname
== NULL
)
560 if (pai
->ai_flags
& AI_NUMERICHOST
)
564 * hostname as alphabetical name.
565 * we would like to prefer AF_INET6 than AF_INET, so we'll make a
568 for (ex
= explore
; ex
->e_af
>= 0; ex
++) {
572 /* ADDRCONFIG check */
573 /* PF_UNSPEC entries are prepared for DNS queries only */
574 if (ex
->e_af
!= PF_UNSPEC
&&
575 (((uint64_t)1 << ex
->e_af
) & mask
) == 0)
578 /* require exact match for family field */
579 if (pai
->ai_family
!= ex
->e_af
)
582 if (!MATCH(pai
->ai_socktype
, ex
->e_socktype
,
583 WILD_SOCKTYPE(ex
))) {
586 if (!MATCH(pai
->ai_protocol
, ex
->e_protocol
,
587 WILD_PROTOCOL(ex
))) {
591 if (pai
->ai_socktype
== ANY
&& ex
->e_socktype
!= ANY
)
592 pai
->ai_socktype
= ex
->e_socktype
;
593 if (pai
->ai_protocol
== ANY
&& ex
->e_protocol
!= ANY
)
594 pai
->ai_protocol
= ex
->e_protocol
;
596 error
= explore_fqdn(pai
, hostname
, servname
, &cur
->ai_next
,
599 while (cur
&& cur
->ai_next
)
604 if (sentinel
.ai_next
)
610 if (sentinel
.ai_next
) {
613 *res
= sentinel
.ai_next
;
620 if (sentinel
.ai_next
)
621 freeaddrinfo(sentinel
.ai_next
);
627 * FQDN hostname, DNS lookup
630 explore_fqdn(const struct addrinfo
*pai
, const char *hostname
,
631 const char *servname
, struct addrinfo
**res
, struct servent_data
*svd
)
633 struct addrinfo
*result
;
634 struct addrinfo
*cur
;
636 static const ns_dtab dtab
[] = {
637 NS_FILES_CB(_files_getaddrinfo
, NULL
)
638 { NSSRC_DNS
, _dns_getaddrinfo
, NULL
}, /* force -DHESIOD */
639 NS_NIS_CB(_yp_getaddrinfo
, NULL
)
643 _DIAGASSERT(pai
!= NULL
);
644 /* hostname may be NULL */
645 /* servname may be NULL */
646 _DIAGASSERT(res
!= NULL
);
651 * if the servname does not match socktype/protocol, ignore it.
653 if (get_portmatch(pai
, servname
, svd
) != 0)
656 switch (nsdispatch(&result
, dtab
, NSDB_HOSTS
, "getaddrinfo",
657 default_dns_files
, hostname
, pai
, servname
)) {
669 for (cur
= result
; cur
; cur
= cur
->ai_next
) {
670 /* Check for already filled port. */
673 GET_PORT(cur
, servname
, svd
);
674 /* canonname should be filled already */
685 freeaddrinfo(result
);
691 * passive socket -> anyaddr (0.0.0.0 or ::)
692 * non-passive socket -> localhost (127.0.0.1 or ::1)
695 explore_null(const struct addrinfo
*pai
, const char *servname
,
696 struct addrinfo
**res
, struct servent_data
*svd
)
699 const struct afd
*afd
;
700 struct addrinfo
*cur
;
701 struct addrinfo sentinel
;
704 _DIAGASSERT(pai
!= NULL
);
705 /* servname may be NULL */
706 _DIAGASSERT(res
!= NULL
);
709 sentinel
.ai_next
= NULL
;
713 * filter out AFs that are not supported by the kernel
716 s
= socket(pai
->ai_family
, SOCK_DGRAM
, 0);
724 * if the servname does not match socktype/protocol, ignore it.
726 if (get_portmatch(pai
, servname
, svd
) != 0)
729 afd
= find_afd(pai
->ai_family
);
733 if (pai
->ai_flags
& AI_PASSIVE
) {
734 GET_AI(cur
->ai_next
, afd
, afd
->a_addrany
);
736 * GET_CANONNAME(cur->ai_next, "anyaddr");
738 GET_PORT(cur
->ai_next
, servname
, svd
);
740 GET_AI(cur
->ai_next
, afd
, afd
->a_loopback
);
742 * GET_CANONNAME(cur->ai_next, "localhost");
744 GET_PORT(cur
->ai_next
, servname
, svd
);
748 *res
= sentinel
.ai_next
;
752 if (sentinel
.ai_next
)
753 freeaddrinfo(sentinel
.ai_next
);
761 explore_numeric(const struct addrinfo
*pai
, const char *hostname
,
762 const char *servname
, struct addrinfo
**res
, const char *canonname
,
763 struct servent_data
*svd
)
765 const struct afd
*afd
;
766 struct addrinfo
*cur
;
767 struct addrinfo sentinel
;
771 _DIAGASSERT(pai
!= NULL
);
772 /* hostname may be NULL */
773 /* servname may be NULL */
774 _DIAGASSERT(res
!= NULL
);
777 sentinel
.ai_next
= NULL
;
781 * if the servname does not match socktype/protocol, ignore it.
783 if (get_portmatch(pai
, servname
, svd
) != 0)
786 afd
= find_afd(pai
->ai_family
);
791 #if 0 /*X/Open spec*/
793 if (inet_aton(hostname
, (struct in_addr
*)pton
) == 1) {
794 if (pai
->ai_family
== afd
->a_af
||
795 pai
->ai_family
== PF_UNSPEC
/*?*/) {
796 GET_AI(cur
->ai_next
, afd
, pton
);
797 GET_PORT(cur
->ai_next
, servname
, svd
);
798 if ((pai
->ai_flags
& AI_CANONNAME
)) {
800 * Set the numeric address itself as
801 * the canonical name, based on a
802 * clarification in rfc2553bis-03.
804 GET_CANONNAME(cur
->ai_next
, canonname
);
806 while (cur
&& cur
->ai_next
)
809 ERR(EAI_FAMILY
); /*xxx*/
814 if (inet_pton(afd
->a_af
, hostname
, pton
) == 1) {
815 if (pai
->ai_family
== afd
->a_af
||
816 pai
->ai_family
== PF_UNSPEC
/*?*/) {
817 GET_AI(cur
->ai_next
, afd
, pton
);
818 GET_PORT(cur
->ai_next
, servname
, svd
);
819 if ((pai
->ai_flags
& AI_CANONNAME
)) {
821 * Set the numeric address itself as
822 * the canonical name, based on a
823 * clarification in rfc2553bis-03.
825 GET_CANONNAME(cur
->ai_next
, canonname
);
830 ERR(EAI_FAMILY
); /*xxx*/
835 *res
= sentinel
.ai_next
;
840 if (sentinel
.ai_next
)
841 freeaddrinfo(sentinel
.ai_next
);
846 * numeric hostname with scope
849 explore_numeric_scope(const struct addrinfo
*pai
, const char *hostname
,
850 const char *servname
, struct addrinfo
**res
, struct servent_data
*svd
)
852 #if !defined(SCOPE_DELIMITER) || !defined(INET6)
853 return explore_numeric(pai
, hostname
, servname
, res
, hostname
, svd
);
855 const struct afd
*afd
;
856 struct addrinfo
*cur
;
858 char *cp
, *hostname2
= NULL
, *scope
, *addr
;
859 struct sockaddr_in6
*sin6
;
861 _DIAGASSERT(pai
!= NULL
);
862 /* hostname may be NULL */
863 /* servname may be NULL */
864 _DIAGASSERT(res
!= NULL
);
867 * if the servname does not match socktype/protocol, ignore it.
869 if (get_portmatch(pai
, servname
, svd
) != 0)
872 afd
= find_afd(pai
->ai_family
);
877 return explore_numeric(pai
, hostname
, servname
, res
, hostname
,
880 cp
= strchr(hostname
, SCOPE_DELIMITER
);
882 return explore_numeric(pai
, hostname
, servname
, res
, hostname
,
886 * Handle special case of <scoped_address><delimiter><scope id>
888 hostname2
= strdup(hostname
);
889 if (hostname2
== NULL
)
891 /* terminate at the delimiter */
892 hostname2
[cp
- hostname
] = '\0';
896 error
= explore_numeric(pai
, addr
, servname
, res
, hostname
, svd
);
900 for (cur
= *res
; cur
; cur
= cur
->ai_next
) {
901 if (cur
->ai_family
!= AF_INET6
)
903 sin6
= (struct sockaddr_in6
*)(void *)cur
->ai_addr
;
904 if (ip6_str2scopeid(scope
, sin6
, &scopeid
) == -1) {
906 return(EAI_NODATA
); /* XXX: is return OK? */
908 sin6
->sin6_scope_id
= scopeid
;
919 get_canonname(const struct addrinfo
*pai
, struct addrinfo
*ai
, const char *str
)
922 _DIAGASSERT(pai
!= NULL
);
923 _DIAGASSERT(ai
!= NULL
);
924 _DIAGASSERT(str
!= NULL
);
926 if ((pai
->ai_flags
& AI_CANONNAME
) != 0) {
927 ai
->ai_canonname
= strdup(str
);
928 if (ai
->ai_canonname
== NULL
)
935 allocaddrinfo(socklen_t addrlen
)
939 ai
= calloc(sizeof(struct addrinfo
) + addrlen
, 1);
941 ai
->ai_addr
= (void *)(ai
+1);
942 #if !defined(__minix)
943 ai
->ai_addrlen
= ai
->ai_addr
->sa_len
= addrlen
;
945 ai
->ai_addrlen
= addrlen
;
946 #endif /* !defined(__minix) */
953 static struct addrinfo
*
954 get_ai(const struct addrinfo
*pai
, const struct afd
*afd
, const char *addr
)
958 struct sockaddr
*save
;
960 _DIAGASSERT(pai
!= NULL
);
961 _DIAGASSERT(afd
!= NULL
);
962 _DIAGASSERT(addr
!= NULL
);
964 ai
= allocaddrinfo((socklen_t
)afd
->a_socklen
);
969 memcpy(ai
, pai
, sizeof(struct addrinfo
));
971 /* since we just overwrote all of ai, we have
972 to restore ai_addr and ai_addrlen */
974 ai
->ai_addrlen
= (socklen_t
)afd
->a_socklen
;
976 ai
->ai_addr
->sa_family
= ai
->ai_family
= afd
->a_af
;
977 p
= (char *)(void *)(ai
->ai_addr
);
978 memcpy(p
+ afd
->a_off
, addr
, (size_t)afd
->a_addrlen
);
983 get_portmatch(const struct addrinfo
*ai
, const char *servname
,
984 struct servent_data
*svd
)
987 _DIAGASSERT(ai
!= NULL
);
988 /* servname may be NULL */
990 return get_port(ai
, servname
, 1, svd
);
994 get_port(const struct addrinfo
*ai
, const char *servname
, int matchonly
,
995 struct servent_data
*svd
)
1002 _DIAGASSERT(ai
!= NULL
);
1003 /* servname may be NULL */
1005 if (servname
== NULL
)
1007 switch (ai
->ai_family
) {
1017 switch (ai
->ai_socktype
) {
1026 * This was 0. It is now 1 so that queries specifying
1027 * a NULL hint, or hint without socktype (but, hopefully,
1028 * with protocol) and numeric address actually work.
1033 return EAI_SOCKTYPE
;
1036 port
= str2number(servname
);
1040 if (port
< 0 || port
> 65535)
1045 if (ai
->ai_flags
& AI_NUMERICSERV
)
1048 switch (ai
->ai_socktype
) {
1060 sp
= getservbyname_r(servname
, proto
, &sv
, svd
);
1067 *getport(__UNCONST(ai
)) = port
;
1071 static const struct afd
*
1074 const struct afd
*afd
;
1076 if (af
== PF_UNSPEC
)
1078 for (afd
= afdl
; afd
->a_af
; afd
++) {
1079 if (afd
->a_af
== af
)
1086 * AI_ADDRCONFIG check: Build a mask containing a bit set for each address
1087 * family configured in the system.
1091 addrconfig(uint64_t *mask
)
1093 struct ifaddrs
*ifaddrs
, *ifa
;
1095 if (getifaddrs(&ifaddrs
) == -1)
1099 for (ifa
= ifaddrs
; ifa
!= NULL
; ifa
= ifa
->ifa_next
)
1100 if (ifa
->ifa_addr
&& (ifa
->ifa_flags
& IFF_UP
)) {
1101 _DIAGASSERT(ifa
->ifa_addr
->sa_family
< 64);
1102 *mask
|= (uint64_t)1 << ifa
->ifa_addr
->sa_family
;
1105 freeifaddrs(ifaddrs
);
1110 /* convert a string to a scope identifier. XXX: IPv6 specific */
1112 ip6_str2scopeid(char *scope
, struct sockaddr_in6
*sin6
, u_int32_t
*scopeid
)
1115 struct in6_addr
*a6
;
1118 _DIAGASSERT(scope
!= NULL
);
1119 _DIAGASSERT(sin6
!= NULL
);
1120 _DIAGASSERT(scopeid
!= NULL
);
1122 a6
= &sin6
->sin6_addr
;
1124 /* empty scopeid portion is invalid */
1128 if (IN6_IS_ADDR_LINKLOCAL(a6
) || IN6_IS_ADDR_MC_LINKLOCAL(a6
)) {
1130 * We currently assume a one-to-one mapping between links
1131 * and interfaces, so we simply use interface indices for
1132 * like-local scopes.
1134 *scopeid
= if_nametoindex(scope
);
1140 /* still unclear about literal, allow numeric only - placeholder */
1141 if (IN6_IS_ADDR_SITELOCAL(a6
) || IN6_IS_ADDR_MC_SITELOCAL(a6
))
1143 if (IN6_IS_ADDR_MC_ORGLOCAL(a6
))
1146 goto trynumeric
; /* global */
1148 /* try to convert to a numeric id as a last resort */
1151 lscopeid
= strtoul(scope
, &ep
, 10);
1152 *scopeid
= (u_int32_t
)(lscopeid
& 0xffffffffUL
);
1153 if (errno
== 0 && ep
&& *ep
== '\0' && *scopeid
== lscopeid
)
1160 /* code duplicate with gethnamaddr.c */
1162 static const char AskedForGot
[] =
1163 "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
1165 static struct addrinfo
*
1166 getanswer(const querybuf
*answer
, int anslen
, const char *qname
, int qtype
,
1167 const struct addrinfo
*pai
)
1169 struct addrinfo sentinel
, *cur
;
1170 struct addrinfo ai
, *aip
;
1171 const struct afd
*afd
;
1178 int type
, class, ancount
, qdcount
;
1179 int haveanswer
, had_error
;
1180 char tbuf
[MAXDNAME
];
1181 int (*name_ok
) (const char *);
1182 char hostbuf
[8*1024];
1183 int port
, pri
, weight
;
1184 struct srvinfo
*srvlist
, *srv
, *csrv
;
1186 _DIAGASSERT(answer
!= NULL
);
1187 _DIAGASSERT(qname
!= NULL
);
1188 _DIAGASSERT(pai
!= NULL
);
1190 memset(&sentinel
, 0, sizeof(sentinel
));
1194 eom
= answer
->buf
+ anslen
;
1198 case T_ANY
: /*use T_ANY only for T_A/T_AAAA lookup*/
1202 name_ok
= gai_srvok
;
1205 return NULL
; /* XXX should be abort(); */
1208 * find first satisfactory answer
1211 ancount
= ntohs(hp
->ancount
);
1212 qdcount
= ntohs(hp
->qdcount
);
1214 ep
= hostbuf
+ sizeof hostbuf
;
1215 cp
= answer
->buf
+ HFIXEDSZ
;
1217 h_errno
= NO_RECOVERY
;
1220 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, (int)(ep
- bp
));
1221 if ((n
< 0) || !(*name_ok
)(bp
)) {
1222 h_errno
= NO_RECOVERY
;
1226 if (qtype
== T_A
|| qtype
== T_AAAA
|| qtype
== T_ANY
) {
1227 /* res_send() has already verified that the query name is the
1228 * same as the one we sent; this just gets the expanded name
1229 * (i.e., with the succeeding search-domain tacked on).
1231 n
= (int)strlen(bp
) + 1; /* for the \0 */
1232 if (n
>= MAXHOSTNAMELEN
) {
1233 h_errno
= NO_RECOVERY
;
1238 /* The qname can be abbreviated, but h_name is now absolute. */
1244 while (ancount
-- > 0 && cp
< eom
&& !had_error
) {
1245 n
= dn_expand(answer
->buf
, eom
, cp
, bp
, (int)(ep
- bp
));
1246 if ((n
< 0) || !(*name_ok
)(bp
)) {
1251 type
= _getshort(cp
);
1252 cp
+= INT16SZ
; /* type */
1253 class = _getshort(cp
);
1254 cp
+= INT16SZ
+ INT32SZ
; /* class, TTL */
1256 cp
+= INT16SZ
; /* len */
1257 if (class != C_IN
) {
1258 /* XXX - debug? syslog? */
1260 continue; /* XXX - had_error++ ? */
1262 if ((qtype
== T_A
|| qtype
== T_AAAA
|| qtype
== T_ANY
) &&
1264 n
= dn_expand(answer
->buf
, eom
, cp
, tbuf
, (int)sizeof tbuf
);
1265 if ((n
< 0) || !(*name_ok
)(tbuf
)) {
1270 /* Get canonical name. */
1271 n
= (int)strlen(tbuf
) + 1; /* for the \0 */
1272 if (n
> ep
- bp
|| n
>= MAXHOSTNAMELEN
) {
1276 strlcpy(bp
, tbuf
, (size_t)(ep
- bp
));
1281 if (qtype
== T_ANY
) {
1282 if (!(type
== T_A
|| type
== T_AAAA
)) {
1286 } else if (type
!= qtype
) {
1287 if (type
!= T_KEY
&& type
!= T_SIG
) {
1288 struct syslog_data sd
= SYSLOG_DATA_INIT
;
1289 syslog_r(LOG_NOTICE
|LOG_AUTH
, &sd
,
1290 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
1291 qname
, p_class(C_IN
), p_type(qtype
),
1295 continue; /* XXX - had_error++ ? */
1300 if (strcasecmp(canonname
, bp
) != 0) {
1301 struct syslog_data sd
= SYSLOG_DATA_INIT
;
1302 syslog_r(LOG_NOTICE
|LOG_AUTH
, &sd
,
1303 AskedForGot
, canonname
, bp
);
1305 continue; /* XXX - had_error++ ? */
1307 if (type
== T_A
&& n
!= INADDRSZ
) {
1311 if (type
== T_AAAA
&& n
!= IN6ADDRSZ
) {
1315 if (type
== T_AAAA
) {
1316 struct in6_addr in6
;
1317 memcpy(&in6
, cp
, IN6ADDRSZ
);
1318 if (IN6_IS_ADDR_V4MAPPED(&in6
)) {
1327 nn
= (int)strlen(bp
) + 1; /* for the \0 */
1331 /* don't overwrite pai */
1333 ai
.ai_family
= (type
== T_A
) ? AF_INET
: AF_INET6
;
1334 afd
= find_afd(ai
.ai_family
);
1339 cur
->ai_next
= get_ai(&ai
, afd
, (const char *)cp
);
1340 if (cur
->ai_next
== NULL
)
1342 while (cur
&& cur
->ai_next
)
1347 /* Add to SRV list. Insertion sort on priority. */
1348 pri
= _getshort(cp
);
1350 weight
= _getshort(cp
);
1352 port
= _getshort(cp
);
1354 n
= dn_expand(answer
->buf
, eom
, cp
, tbuf
,
1356 if ((n
< 0) || !res_hnok(tbuf
)) {
1361 if (strlen(tbuf
) + 1 >= MAXDNAME
) {
1365 srv
= malloc(sizeof(*srv
));
1370 strlcpy(srv
->name
, tbuf
, sizeof(srv
->name
));
1372 srv
->weight
= weight
;
1374 /* Weight 0 is sorted before other weights. */
1376 || srv
->pri
< srvlist
->pri
1377 || (srv
->pri
== srvlist
->pri
&&
1378 (!srv
->weight
|| srvlist
->weight
))) {
1379 srv
->next
= srvlist
;
1382 for (csrv
= srvlist
;
1383 csrv
->next
&& csrv
->next
->pri
<= srv
->pri
;
1384 csrv
= csrv
->next
) {
1385 if (csrv
->next
->pri
== srv
->pri
1387 csrv
->next
->weight
))
1390 srv
->next
= csrv
->next
;
1393 continue; /* Don't add to haveanswer yet. */
1404 * Check for explicit rejection.
1406 if (!srvlist
->next
&& !srvlist
->name
[0]) {
1408 h_errno
= HOST_NOT_FOUND
;
1411 res
= __res_get_state();
1413 while (srvlist
!= NULL
) {
1415 srvlist
= srvlist
->next
;
1418 h_errno
= NETDB_INTERNAL
;
1423 struct res_target q
, q2
;
1426 srvlist
= srvlist
->next
;
1429 * Since res_* doesn't give the additional
1430 * section, we always look up.
1432 memset(&q
, 0, sizeof(q
));
1433 memset(&q2
, 0, sizeof(q2
));
1439 q2
.name
= srv
->name
;
1443 aip
= _dns_query(&q
, pai
, res
, 0);
1447 while (cur
&& cur
->ai_next
) {
1449 *getport(cur
) = htons(srv
->port
);
1455 __res_put_state(res
);
1458 if (!sentinel
.ai_next
->ai_canonname
)
1459 (void)get_canonname(pai
, sentinel
.ai_next
,
1460 canonname
? canonname
: qname
);
1461 h_errno
= NETDB_SUCCESS
;
1462 return sentinel
.ai_next
;
1465 h_errno
= NO_RECOVERY
;
1469 #define SORTEDADDR(p) (((struct sockaddr_in *)(void *)(p->ai_next->ai_addr))->sin_addr.s_addr)
1470 #define SORTMATCH(p, s) ((SORTEDADDR(p) & (s).mask) == (s).addr.s_addr)
1473 aisort(struct addrinfo
*s
, res_state res
)
1475 struct addrinfo head
, *t
, *p
;
1478 head
.ai_next
= NULL
;
1481 for (i
= 0; i
< res
->nsort
; i
++) {
1483 while (p
->ai_next
) {
1484 if ((p
->ai_next
->ai_family
!= AF_INET
)
1485 || SORTMATCH(p
, res
->sort_list
[i
])) {
1486 t
->ai_next
= p
->ai_next
;
1488 p
->ai_next
= p
->ai_next
->ai_next
;
1495 /* add rest of list and reset s to the new list*/
1496 t
->ai_next
= s
->ai_next
;
1497 s
->ai_next
= head
.ai_next
;
1500 static struct addrinfo
*
1501 _dns_query(struct res_target
*q
, const struct addrinfo
*pai
,
1502 res_state res
, int dosearch
)
1504 struct res_target
*q2
= q
->next
;
1505 querybuf
*buf
, *buf2
;
1506 struct addrinfo sentinel
, *cur
, *ai
;
1509 struct res_target
*iter
;
1510 for (iter
= q
; iter
; iter
= iter
->next
)
1511 printf("Query type %d for %s\n", iter
->qtype
, iter
->name
);
1514 buf
= malloc(sizeof(*buf
));
1516 h_errno
= NETDB_INTERNAL
;
1519 buf2
= malloc(sizeof(*buf2
));
1522 h_errno
= NETDB_INTERNAL
;
1526 memset(&sentinel
, 0, sizeof(sentinel
));
1529 q
->answer
= buf
->buf
;
1530 q
->anslen
= sizeof(buf
->buf
);
1532 q2
->answer
= buf2
->buf
;
1533 q2
->anslen
= sizeof(buf2
->buf
);
1537 if (res_searchN(q
->name
, q
, res
) < 0)
1540 if (res_queryN(q
->name
, q
, res
) < 0)
1544 ai
= getanswer(buf
, q
->n
, q
->name
, q
->qtype
, pai
);
1547 while (cur
&& cur
->ai_next
)
1551 ai
= getanswer(buf2
, q2
->n
, q2
->name
, q2
->qtype
, pai
);
1557 return sentinel
.ai_next
;
1565 static struct addrinfo
*
1566 _dns_srv_lookup(const char *name
, const char *servname
,
1567 const struct addrinfo
*pai
)
1569 static const char * const srvprotos
[] = { "tcp", "udp" };
1570 static const int srvnottype
[] = { SOCK_DGRAM
, SOCK_STREAM
};
1571 static const int nsrvprotos
= 2;
1572 struct addrinfo sentinel
, *cur
, *ai
;
1573 struct servent
*serv
, sv
;
1574 struct servent_data svd
;
1575 struct res_target q
;
1580 res
= __res_get_state();
1584 memset(&svd
, 0, sizeof(svd
));
1585 memset(&sentinel
, 0, sizeof(sentinel
));
1589 * Iterate over supported SRV protocols.
1590 * (currently UDP and TCP only)
1592 for (i
= 0; i
< nsrvprotos
; i
++) {
1594 * Check that the caller didn't specify a hint
1595 * which precludes this protocol.
1597 if (pai
->ai_socktype
== srvnottype
[i
])
1600 * If the caller specified a port,
1601 * then lookup the database for the
1602 * official service name.
1604 serv
= getservbyname_r(servname
, srvprotos
[i
], &sv
, &svd
);
1609 * Construct service DNS name.
1611 if (asprintf(&tname
, "_%s._%s.%s", serv
->s_name
, serv
->s_proto
,
1615 memset(&q
, 0, sizeof(q
));
1623 ai
= _dns_query(&q
, pai
, res
, 1);
1626 while (cur
&& cur
->ai_next
)
1633 aisort(&sentinel
, res
);
1635 __res_put_state(res
);
1637 return sentinel
.ai_next
;
1641 static struct addrinfo
*
1642 _dns_host_lookup(const char *name
, const struct addrinfo
*pai
)
1644 struct res_target q
, q2
;
1645 struct addrinfo sentinel
, *ai
;
1648 res
= __res_get_state();
1652 memset(&q
, 0, sizeof(q2
));
1653 memset(&q2
, 0, sizeof(q2
));
1655 switch (pai
->ai_family
) {
1677 __res_put_state(res
);
1678 h_errno
= NETDB_INTERNAL
;
1682 ai
= _dns_query(&q
, pai
, res
, 1);
1684 memset(&sentinel
, 0, sizeof(sentinel
));
1685 sentinel
.ai_next
= ai
;
1687 if (ai
!= NULL
&& res
->nsort
)
1688 aisort(&sentinel
, res
);
1690 __res_put_state(res
);
1692 return sentinel
.ai_next
;
1697 _dns_getaddrinfo(void *rv
, void *cb_data
, va_list ap
)
1699 struct addrinfo
*ai
= NULL
;
1700 const char *name
, *servname
;
1701 const struct addrinfo
*pai
;
1703 name
= va_arg(ap
, char *);
1704 pai
= va_arg(ap
, const struct addrinfo
*);
1705 servname
= va_arg(ap
, char *);
1708 * Try doing SRV lookup on service first.
1712 && (pai
->ai_flags
& AI_SRV
)
1714 && !(pai
->ai_flags
& AI_NUMERICSERV
)
1715 && str2number(servname
) == -1) {
1718 printf("%s: try SRV lookup\n", __func__
);
1720 ai
= _dns_srv_lookup(name
, servname
, pai
);
1724 * Do lookup on name.
1729 printf("%s: try HOST lookup\n", __func__
);
1731 ai
= _dns_host_lookup(name
, pai
);
1735 case HOST_NOT_FOUND
:
1745 *((struct addrinfo
**)rv
) = ai
;
1750 _sethtent(FILE **hostf
)
1754 *hostf
= fopen(_PATH_HOSTS
, "re");
1760 _endhtent(FILE **hostf
)
1764 (void) fclose(*hostf
);
1769 static struct addrinfo
*
1770 _gethtent(FILE **hostf
, const char *name
, const struct addrinfo
*pai
)
1773 char *cp
, *tname
, *cname
;
1774 struct addrinfo hints
, *res0
, *res
;
1777 char hostbuf
[8*1024];
1779 _DIAGASSERT(name
!= NULL
);
1780 _DIAGASSERT(pai
!= NULL
);
1782 if (!*hostf
&& !(*hostf
= fopen(_PATH_HOSTS
, "re")))
1785 if (!(p
= fgets(hostbuf
, (int)sizeof hostbuf
, *hostf
)))
1789 if (!(cp
= strpbrk(p
, "#\n")))
1792 if (!(cp
= strpbrk(p
, " \t")))
1796 /* if this is not something we're looking for, skip it. */
1799 if (*cp
== ' ' || *cp
== '\t') {
1806 if ((cp
= strpbrk(cp
, " \t")) != NULL
)
1808 if (strcasecmp(name
, tname
) == 0)
1815 hints
.ai_flags
= AI_NUMERICHOST
;
1816 error
= getaddrinfo(addr
, NULL
, &hints
, &res0
);
1819 for (res
= res0
; res
; res
= res
->ai_next
) {
1821 res
->ai_flags
= pai
->ai_flags
;
1823 if (pai
->ai_flags
& AI_CANONNAME
) {
1824 if (get_canonname(pai
, res
, cname
) != 0) {
1835 _files_getaddrinfo(void *rv
, void *cb_data
, va_list ap
)
1838 const struct addrinfo
*pai
;
1839 struct addrinfo sentinel
, *cur
;
1846 name
= va_arg(ap
, char *);
1847 pai
= va_arg(ap
, const struct addrinfo
*);
1849 memset(&sentinel
, 0, sizeof(sentinel
));
1853 while ((p
= _gethtent(&hostf
, name
, pai
)) != NULL
) {
1855 while (cur
&& cur
->ai_next
)
1860 *((struct addrinfo
**)rv
) = sentinel
.ai_next
;
1861 if (sentinel
.ai_next
== NULL
)
1868 static struct addrinfo
*
1869 _yphostent(char *line
, const struct addrinfo
*pai
)
1871 struct addrinfo sentinel
, *cur
;
1872 struct addrinfo hints
, *res
, *res0
;
1875 const char *addr
, *canonname
;
1879 _DIAGASSERT(line
!= NULL
);
1880 _DIAGASSERT(pai
!= NULL
);
1883 addr
= canonname
= NULL
;
1885 memset(&sentinel
, 0, sizeof(sentinel
));
1889 /* terminate line */
1890 cp
= strchr(p
, '\n');
1897 cp
= strpbrk(p
, " \t");
1899 if (canonname
== NULL
)
1909 if (*cp
== ' ' || *cp
== '\t') {
1915 if ((cp
= strpbrk(cp
, " \t")) != NULL
)
1920 hints
.ai_flags
= AI_NUMERICHOST
;
1921 error
= getaddrinfo(addr
, NULL
, &hints
, &res0
);
1923 for (res
= res0
; res
; res
= res
->ai_next
) {
1925 res
->ai_flags
= pai
->ai_flags
;
1927 if (pai
->ai_flags
& AI_CANONNAME
)
1928 (void)get_canonname(pai
, res
, canonname
);
1933 cur
->ai_next
= res0
;
1934 while (cur
->ai_next
)
1944 return sentinel
.ai_next
;
1949 _yp_getaddrinfo(void *rv
, void *cb_data
, va_list ap
)
1951 struct addrinfo sentinel
, *cur
;
1952 struct addrinfo
*ai
= NULL
;
1956 const struct addrinfo
*pai
;
1959 if (_yp_check(&ypdomain
) == 0)
1962 name
= va_arg(ap
, char *);
1963 pai
= va_arg(ap
, const struct addrinfo
*);
1965 memset(&sentinel
, 0, sizeof(sentinel
));
1968 /* hosts.byname is only for IPv4 (Solaris8) */
1969 if (pai
->ai_family
== PF_UNSPEC
|| pai
->ai_family
== PF_INET
) {
1970 r
= yp_match(ypdomain
, "hosts.byname", name
,
1971 (int)strlen(name
), &ypbuf
, &ypbuflen
);
1973 struct addrinfo ai4
;
1976 ai4
.ai_family
= AF_INET
;
1977 ai
= _yphostent(ypbuf
, &ai4
);
1980 while (cur
&& cur
->ai_next
)
1987 /* ipnodes.byname can hold both IPv4/v6 */
1988 r
= yp_match(ypdomain
, "ipnodes.byname", name
,
1989 (int)strlen(name
), &ypbuf
, &ypbuflen
);
1991 ai
= _yphostent(ypbuf
, pai
);
1997 if (sentinel
.ai_next
== NULL
) {
1998 h_errno
= HOST_NOT_FOUND
;
2001 *((struct addrinfo
**)rv
) = sentinel
.ai_next
;
2006 /* resolver logic */
2009 * Formulate a normal query, send, and await answer.
2010 * Returned answer is placed in supplied buffer "answer".
2011 * Perform preliminary check of answer, returning success only
2012 * if no error is indicated and the answer count is nonzero.
2013 * Return the size of the response on success, -1 on error.
2014 * Error number is left in h_errno.
2016 * Caller must parse answer and determine whether it answers the question.
2019 res_queryN(const char *name
, /* domain name */ struct res_target
*target
,
2022 u_char buf
[MAXPACKET
];
2025 struct res_target
*t
;
2029 _DIAGASSERT(name
!= NULL
);
2030 /* XXX: target may be NULL??? */
2035 for (t
= target
; t
; t
= t
->next
) {
2040 hp
= (HEADER
*)(void *)t
->answer
;
2041 hp
->rcode
= NOERROR
; /* default */
2043 /* make it easier... */
2049 if (res
->options
& RES_DEBUG
)
2050 printf(";; res_nquery(%s, %d, %d)\n", name
, class, type
);
2053 n
= res_nmkquery(res
, QUERY
, name
, class, type
, NULL
, 0, NULL
,
2054 buf
, (int)sizeof(buf
));
2055 #ifdef RES_USE_EDNS0
2056 if (n
> 0 && (res
->options
& RES_USE_EDNS0
) != 0)
2057 n
= res_nopt(res
, n
, buf
, (int)sizeof(buf
), anslen
);
2061 if (res
->options
& RES_DEBUG
)
2062 printf(";; res_nquery: mkquery failed\n");
2064 h_errno
= NO_RECOVERY
;
2067 n
= res_nsend(res
, buf
, n
, answer
, anslen
);
2071 if (res
->options
& RES_DEBUG
)
2072 printf(";; res_query: send error\n");
2074 h_errno
= TRY_AGAIN
;
2079 if (n
< 0 || hp
->rcode
!= NOERROR
|| ntohs(hp
->ancount
) == 0) {
2080 rcode
= hp
->rcode
; /* record most recent error */
2082 if (res
->options
& RES_DEBUG
)
2083 printf(";; rcode = %u, ancount=%u\n", hp
->rcode
,
2084 ntohs(hp
->ancount
));
2089 ancount
+= ntohs(hp
->ancount
);
2097 h_errno
= HOST_NOT_FOUND
;
2100 h_errno
= TRY_AGAIN
;
2109 h_errno
= NO_RECOVERY
;
2118 * Formulate a normal query, send, and retrieve answer in supplied buffer.
2119 * Return the size of the response on success, -1 on error.
2120 * If enabled, implement search rules until answer or unrecoverable failure
2121 * is detected. Error code, if any, is left in h_errno.
2124 res_searchN(const char *name
, struct res_target
*target
, res_state res
)
2126 const char *cp
, * const *domain
;
2129 int trailing_dot
, ret
, saved_herrno
;
2130 int got_nodata
= 0, got_servfail
= 0, tried_as_is
= 0;
2132 _DIAGASSERT(name
!= NULL
);
2133 _DIAGASSERT(target
!= NULL
);
2135 hp
= (HEADER
*)(void *)target
->answer
; /*XXX*/
2138 h_errno
= HOST_NOT_FOUND
; /* default, if we never query */
2140 for (cp
= name
; *cp
; cp
++)
2141 dots
+= (*cp
== '.');
2143 if (cp
> name
&& *--cp
== '.')
2147 * if there aren't any dots, it could be a user-level alias
2149 if (!dots
&& (cp
= __hostalias(name
)) != NULL
) {
2150 ret
= res_queryN(cp
, target
, res
);
2155 * If there are dots in the name already, let's just give it a try
2156 * 'as is'. The threshold can be set with the "ndots" option.
2159 if (dots
>= res
->ndots
) {
2160 ret
= res_querydomainN(name
, NULL
, target
, res
);
2163 saved_herrno
= h_errno
;
2168 * We do at least one level of search if
2169 * - there is no dot and RES_DEFNAME is set, or
2170 * - there is at least one dot, there is no trailing dot,
2171 * and RES_DNSRCH is set.
2173 if ((!dots
&& (res
->options
& RES_DEFNAMES
)) ||
2174 (dots
&& !trailing_dot
&& (res
->options
& RES_DNSRCH
))) {
2177 for (domain
= (const char * const *)res
->dnsrch
;
2181 ret
= res_querydomainN(name
, *domain
, target
, res
);
2186 * If no server present, give up.
2187 * If name isn't found in this domain,
2188 * keep trying higher domains in the search list
2189 * (if that's enabled).
2190 * On a NO_DATA error, keep trying, otherwise
2191 * a wildcard entry of another type could keep us
2192 * from finding this entry higher in the domain.
2193 * If we get some other error (negative answer or
2194 * server failure), then stop searching up,
2195 * but try the input name below in case it's
2198 if (errno
== ECONNREFUSED
) {
2199 h_errno
= TRY_AGAIN
;
2207 case HOST_NOT_FOUND
:
2211 if (hp
->rcode
== SERVFAIL
) {
2212 /* try next search element, if any */
2218 /* anything else implies that we're done */
2222 * if we got here for some reason other than DNSRCH,
2223 * we only wanted one iteration of the loop, so stop.
2225 if (!(res
->options
& RES_DNSRCH
))
2231 * if we have not already tried the name "as is", do that now.
2232 * note that we do this regardless of how many dots were in the
2233 * name or whether it ends with a dot.
2236 ret
= res_querydomainN(name
, NULL
, target
, res
);
2242 * if we got here, we didn't satisfy the search.
2243 * if we did an initial full query, return that query's h_errno
2244 * (note that we wouldn't be here if that query had succeeded).
2245 * else if we ever got a nodata, send that back as the reason.
2246 * else send back meaningless h_errno, that being the one from
2247 * the last DNSRCH we did.
2249 if (saved_herrno
!= -1)
2250 h_errno
= saved_herrno
;
2251 else if (got_nodata
)
2253 else if (got_servfail
)
2254 h_errno
= TRY_AGAIN
;
2259 * Perform a call on res_query on the concatenation of name and domain,
2260 * removing a trailing dot from name if domain is NULL.
2263 res_querydomainN(const char *name
, const char *domain
,
2264 struct res_target
*target
, res_state res
)
2266 char nbuf
[MAXDNAME
];
2267 const char *longname
= nbuf
;
2270 _DIAGASSERT(name
!= NULL
);
2271 /* XXX: target may be NULL??? */
2274 if (res
->options
& RES_DEBUG
)
2275 printf(";; res_querydomain(%s, %s)\n",
2276 name
, domain
?domain
:"<Nil>");
2278 if (domain
== NULL
) {
2280 * Check for trailing '.';
2281 * copy without '.' if present.
2284 if (n
+ 1 > sizeof(nbuf
)) {
2285 h_errno
= NO_RECOVERY
;
2288 if (n
> 0 && name
[--n
] == '.') {
2289 strncpy(nbuf
, name
, n
);
2296 if (n
+ 1 + d
+ 1 > sizeof(nbuf
)) {
2297 h_errno
= NO_RECOVERY
;
2300 snprintf(nbuf
, sizeof(nbuf
), "%s.%s", name
, domain
);
2302 return res_queryN(longname
, target
, res
);
2307 main(int argc
, char *argv
[]) {
2308 struct addrinfo
*ai
, *sai
;
2312 for (i
= 1; i
< argc
; i
++) {
2313 if ((e
= getaddrinfo(argv
[i
], NULL
, NULL
, &sai
)) != 0)
2314 warnx("%s: %s", argv
[i
], gai_strerror(e
));
2315 for (ai
= sai
; ai
; ai
= ai
->ai_next
) {
2316 sockaddr_snprintf(buf
, sizeof(buf
), "%a", ai
->ai_addr
);
2317 printf("flags=0x%x family=%d socktype=%d protocol=%d "
2318 "addrlen=%zu addr=%s canonname=%s next=%p\n",
2323 (size_t)ai
->ai_addrlen
,