xref: /freebsd/lib/libc/net/getaddrinfo.c (revision 90d1d883e9bc2fda7a740176e14220e08a705d0e)
1bf838688SHajimu UMEMOTO /*	$KAME: getaddrinfo.c,v 1.15 2000/07/09 04:37:24 itojun Exp $	*/
2b826397aSJun-ichiro itojun Hagino 
37d56d374SYoshinobu Inoue /*
47d56d374SYoshinobu Inoue  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
57d56d374SYoshinobu Inoue  * All rights reserved.
67d56d374SYoshinobu Inoue  *
77d56d374SYoshinobu Inoue  * Redistribution and use in source and binary forms, with or without
87d56d374SYoshinobu Inoue  * modification, are permitted provided that the following conditions
97d56d374SYoshinobu Inoue  * are met:
107d56d374SYoshinobu Inoue  * 1. Redistributions of source code must retain the above copyright
117d56d374SYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer.
127d56d374SYoshinobu Inoue  * 2. Redistributions in binary form must reproduce the above copyright
137d56d374SYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer in the
147d56d374SYoshinobu Inoue  *    documentation and/or other materials provided with the distribution.
157d56d374SYoshinobu Inoue  * 3. Neither the name of the project nor the names of its contributors
167d56d374SYoshinobu Inoue  *    may be used to endorse or promote products derived from this software
177d56d374SYoshinobu Inoue  *    without specific prior written permission.
187d56d374SYoshinobu Inoue  *
197d56d374SYoshinobu Inoue  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
207d56d374SYoshinobu Inoue  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
217d56d374SYoshinobu Inoue  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
227d56d374SYoshinobu Inoue  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
237d56d374SYoshinobu Inoue  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
247d56d374SYoshinobu Inoue  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
257d56d374SYoshinobu Inoue  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
267d56d374SYoshinobu Inoue  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
277d56d374SYoshinobu Inoue  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
287d56d374SYoshinobu Inoue  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
297d56d374SYoshinobu Inoue  * SUCH DAMAGE.
307d56d374SYoshinobu Inoue  */
317d56d374SYoshinobu Inoue 
327d56d374SYoshinobu Inoue /*
337d56d374SYoshinobu Inoue  * "#ifdef FAITH" part is local hack for supporting IPv4-v6 translator.
347d56d374SYoshinobu Inoue  *
357d56d374SYoshinobu Inoue  * Issues to be discussed:
367d56d374SYoshinobu Inoue  * - Thread safe-ness must be checked.
377d56d374SYoshinobu Inoue  * - Return values.  There are nonstandard return values defined and used
387d56d374SYoshinobu Inoue  *   in the source code.  This is because RFC2553 is silent about which error
397d56d374SYoshinobu Inoue  *   code must be returned for which situation.
40a42af91cSHajimu UMEMOTO  * - freeaddrinfo(NULL).  RFC2553 is silent about it.  XNET 5.2 says it is
41dd521ef1SHajimu UMEMOTO  *   invalid.  current code - SEGV on freeaddrinfo(NULL)
42dd521ef1SHajimu UMEMOTO  *
4300a8a579SYoshinobu Inoue  * Note:
4400a8a579SYoshinobu Inoue  * - The code filters out AFs that are not supported by the kernel,
4500a8a579SYoshinobu Inoue  *   when globbing NULL hostname (to loopback, or wildcard).  Is it the right
4600a8a579SYoshinobu Inoue  *   thing to do?  What is the relationship with post-RFC2553 AI_ADDRCONFIG
4700a8a579SYoshinobu Inoue  *   in ai_flags?
48a42af91cSHajimu UMEMOTO  * - (post-2553) semantics of AI_ADDRCONFIG itself is too vague.
49a42af91cSHajimu UMEMOTO  *   (1) what should we do against numeric hostname (2) what should we do
50a42af91cSHajimu UMEMOTO  *   against NULL hostname (3) what is AI_ADDRCONFIG itself.  AF not ready?
51a42af91cSHajimu UMEMOTO  *   non-loopback address configured?  global address configured?
52dd521ef1SHajimu UMEMOTO  *
53dd521ef1SHajimu UMEMOTO  * OS specific notes for netbsd/openbsd/freebsd4/bsdi4:
54a42af91cSHajimu UMEMOTO  * - To avoid search order issue, we have a big amount of code duplicate
55a42af91cSHajimu UMEMOTO  *   from gethnamaddr.c and some other places.  The issues that there's no
56a42af91cSHajimu UMEMOTO  *   lower layer function to lookup "IPv4 or IPv6" record.  Calling
57a42af91cSHajimu UMEMOTO  *   gethostbyname2 from getaddrinfo will end up in wrong search order, as
58dd521ef1SHajimu UMEMOTO  *   presented above.
59dd521ef1SHajimu UMEMOTO  *
60dd521ef1SHajimu UMEMOTO  * OS specific notes for freebsd4:
61dd521ef1SHajimu UMEMOTO  * - FreeBSD supported $GAI.  The code does not.
62dd521ef1SHajimu UMEMOTO  * - FreeBSD allowed classful IPv4 numeric (127.1), the code does not.
637d56d374SYoshinobu Inoue  */
647d56d374SYoshinobu Inoue 
65333fc21eSDavid E. O'Brien #include <sys/cdefs.h>
66333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$");
67333fc21eSDavid E. O'Brien 
68d201fe46SDaniel Eischen #include "namespace.h"
697d56d374SYoshinobu Inoue #include <sys/types.h>
707d56d374SYoshinobu Inoue #include <sys/param.h>
717d56d374SYoshinobu Inoue #include <sys/socket.h>
727d56d374SYoshinobu Inoue #include <net/if.h>
737d56d374SYoshinobu Inoue #include <netinet/in.h>
744c6867a8SHajimu UMEMOTO #include <sys/queue.h>
754c6867a8SHajimu UMEMOTO #ifdef INET6
764c6867a8SHajimu UMEMOTO #include <net/if_var.h>
774c6867a8SHajimu UMEMOTO #include <sys/sysctl.h>
784d489f47SHajimu UMEMOTO #include <sys/ioctl.h>
794c6867a8SHajimu UMEMOTO #include <netinet6/in6_var.h>	/* XXX */
804c6867a8SHajimu UMEMOTO #endif
817d56d374SYoshinobu Inoue #include <arpa/inet.h>
827d56d374SYoshinobu Inoue #include <arpa/nameser.h>
83bf838688SHajimu UMEMOTO #include <rpc/rpc.h>
84bf838688SHajimu UMEMOTO #include <rpcsvc/yp_prot.h>
85bf838688SHajimu UMEMOTO #include <rpcsvc/ypclnt.h>
867d56d374SYoshinobu Inoue #include <netdb.h>
877d56d374SYoshinobu Inoue #include <resolv.h>
887d56d374SYoshinobu Inoue #include <string.h>
897d56d374SYoshinobu Inoue #include <stdlib.h>
907d56d374SYoshinobu Inoue #include <stddef.h>
917d56d374SYoshinobu Inoue #include <ctype.h>
927d56d374SYoshinobu Inoue #include <unistd.h>
937d56d374SYoshinobu Inoue #include <stdio.h>
94a42af91cSHajimu UMEMOTO #include <errno.h>
9569b0a4b6SJim Pirzyk 
9669b0a4b6SJim Pirzyk #include "res_config.h"
9769b0a4b6SJim Pirzyk 
98ce6282dbSHajimu UMEMOTO #ifdef DEBUG
99ce6282dbSHajimu UMEMOTO #include <syslog.h>
100ce6282dbSHajimu UMEMOTO #endif
1017d56d374SYoshinobu Inoue 
102248aee62SJacques Vidrine #include <stdarg.h>
103248aee62SJacques Vidrine #include <nsswitch.h>
104d201fe46SDaniel Eischen #include "un-namespace.h"
1052bbd7cf8SJacques Vidrine #include "libc_private.h"
106248aee62SJacques Vidrine 
1077d56d374SYoshinobu Inoue #if defined(__KAME__) && defined(INET6)
1087d56d374SYoshinobu Inoue # define FAITH
1097d56d374SYoshinobu Inoue #endif
1107d56d374SYoshinobu Inoue 
1117d56d374SYoshinobu Inoue #define SUCCESS 0
1127d56d374SYoshinobu Inoue #define ANY 0
1137d56d374SYoshinobu Inoue #define YES 1
1147d56d374SYoshinobu Inoue #define NO  0
1157d56d374SYoshinobu Inoue 
1167d56d374SYoshinobu Inoue static const char in_addrany[] = { 0, 0, 0, 0 };
117dd521ef1SHajimu UMEMOTO static const char in_loopback[] = { 127, 0, 0, 1 };
118dd521ef1SHajimu UMEMOTO #ifdef INET6
1197d56d374SYoshinobu Inoue static const char in6_addrany[] = {
1207d56d374SYoshinobu Inoue 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1217d56d374SYoshinobu Inoue };
1227d56d374SYoshinobu Inoue static const char in6_loopback[] = {
1237d56d374SYoshinobu Inoue 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
1247d56d374SYoshinobu Inoue };
125dd521ef1SHajimu UMEMOTO #endif
1267d56d374SYoshinobu Inoue 
1274c6867a8SHajimu UMEMOTO struct policyqueue {
1284c6867a8SHajimu UMEMOTO 	TAILQ_ENTRY(policyqueue) pc_entry;
1294c6867a8SHajimu UMEMOTO #ifdef INET6
1304c6867a8SHajimu UMEMOTO 	struct in6_addrpolicy pc_policy;
1314c6867a8SHajimu UMEMOTO #endif
1324c6867a8SHajimu UMEMOTO };
1334c6867a8SHajimu UMEMOTO TAILQ_HEAD(policyhead, policyqueue);
1344c6867a8SHajimu UMEMOTO 
1357d56d374SYoshinobu Inoue static const struct afd {
1367d56d374SYoshinobu Inoue 	int a_af;
1377d56d374SYoshinobu Inoue 	int a_addrlen;
138036ae3ddSHajimu UMEMOTO 	socklen_t a_socklen;
1397d56d374SYoshinobu Inoue 	int a_off;
1407d56d374SYoshinobu Inoue 	const char *a_addrany;
1417d56d374SYoshinobu Inoue 	const char *a_loopback;
1427d56d374SYoshinobu Inoue 	int a_scoped;
1437d56d374SYoshinobu Inoue } afdl [] = {
1447d56d374SYoshinobu Inoue #ifdef INET6
1457d56d374SYoshinobu Inoue #define	N_INET6 0
1467d56d374SYoshinobu Inoue 	{PF_INET6, sizeof(struct in6_addr),
1477d56d374SYoshinobu Inoue 	 sizeof(struct sockaddr_in6),
1487d56d374SYoshinobu Inoue 	 offsetof(struct sockaddr_in6, sin6_addr),
1497d56d374SYoshinobu Inoue 	 in6_addrany, in6_loopback, 1},
1507d56d374SYoshinobu Inoue #define	N_INET 1
1517d56d374SYoshinobu Inoue #else
1527d56d374SYoshinobu Inoue #define	N_INET 0
1537d56d374SYoshinobu Inoue #endif
1547d56d374SYoshinobu Inoue 	{PF_INET, sizeof(struct in_addr),
1557d56d374SYoshinobu Inoue 	 sizeof(struct sockaddr_in),
1567d56d374SYoshinobu Inoue 	 offsetof(struct sockaddr_in, sin_addr),
1577d56d374SYoshinobu Inoue 	 in_addrany, in_loopback, 0},
1587d56d374SYoshinobu Inoue 	{0, 0, 0, 0, NULL, NULL, 0},
1597d56d374SYoshinobu Inoue };
1607d56d374SYoshinobu Inoue 
1617d56d374SYoshinobu Inoue struct explore {
162a42af91cSHajimu UMEMOTO 	int e_af;
1637d56d374SYoshinobu Inoue 	int e_socktype;
1647d56d374SYoshinobu Inoue 	int e_protocol;
1657d56d374SYoshinobu Inoue 	const char *e_protostr;
1667d56d374SYoshinobu Inoue 	int e_wild;
1677d56d374SYoshinobu Inoue #define WILD_AF(ex)		((ex)->e_wild & 0x01)
1687d56d374SYoshinobu Inoue #define WILD_SOCKTYPE(ex)	((ex)->e_wild & 0x02)
1697d56d374SYoshinobu Inoue #define WILD_PROTOCOL(ex)	((ex)->e_wild & 0x04)
1707d56d374SYoshinobu Inoue };
1717d56d374SYoshinobu Inoue 
1727d56d374SYoshinobu Inoue static const struct explore explore[] = {
173a42af91cSHajimu UMEMOTO #if 0
174bf838688SHajimu UMEMOTO 	{ PF_LOCAL, 0, ANY, ANY, NULL, 0x01 },
175a42af91cSHajimu UMEMOTO #endif
176a42af91cSHajimu UMEMOTO #ifdef INET6
177bf838688SHajimu UMEMOTO 	{ PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
178bf838688SHajimu UMEMOTO 	{ PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
179bf838688SHajimu UMEMOTO 	{ PF_INET6, SOCK_RAW, ANY, NULL, 0x05 },
180a42af91cSHajimu UMEMOTO #endif
181bf838688SHajimu UMEMOTO 	{ PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
182bf838688SHajimu UMEMOTO 	{ PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
183bf838688SHajimu UMEMOTO 	{ PF_INET, SOCK_RAW, ANY, NULL, 0x05 },
184bf838688SHajimu UMEMOTO 	{ PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
185bf838688SHajimu UMEMOTO 	{ PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
186bf838688SHajimu UMEMOTO 	{ PF_UNSPEC, SOCK_RAW, ANY, NULL, 0x05 },
187a42af91cSHajimu UMEMOTO 	{ -1, 0, 0, NULL, 0 },
1887d56d374SYoshinobu Inoue };
1897d56d374SYoshinobu Inoue 
1907d56d374SYoshinobu Inoue #ifdef INET6
1917d56d374SYoshinobu Inoue #define PTON_MAX	16
1927d56d374SYoshinobu Inoue #else
1937d56d374SYoshinobu Inoue #define PTON_MAX	4
1947d56d374SYoshinobu Inoue #endif
1957d56d374SYoshinobu Inoue 
1964c6867a8SHajimu UMEMOTO #define AIO_SRCFLAG_DEPRECATED	0x1
1974c6867a8SHajimu UMEMOTO 
1984c6867a8SHajimu UMEMOTO struct ai_order {
1994c6867a8SHajimu UMEMOTO 	union {
2004c6867a8SHajimu UMEMOTO 		struct sockaddr_storage aiou_ss;
2014c6867a8SHajimu UMEMOTO 		struct sockaddr aiou_sa;
2024c6867a8SHajimu UMEMOTO 	} aio_src_un;
2034c6867a8SHajimu UMEMOTO #define aio_srcsa aio_src_un.aiou_sa
2044c6867a8SHajimu UMEMOTO 	u_int32_t aio_srcflag;
2054c6867a8SHajimu UMEMOTO 	int aio_srcscope;
2064c6867a8SHajimu UMEMOTO 	int aio_dstscope;
2074c6867a8SHajimu UMEMOTO 	struct policyqueue *aio_srcpolicy;
2084c6867a8SHajimu UMEMOTO 	struct policyqueue *aio_dstpolicy;
2094c6867a8SHajimu UMEMOTO 	struct addrinfo *aio_ai;
2104c6867a8SHajimu UMEMOTO 	int aio_matchlen;
2114c6867a8SHajimu UMEMOTO };
2124c6867a8SHajimu UMEMOTO 
213248aee62SJacques Vidrine static const ns_src default_dns_files[] = {
214248aee62SJacques Vidrine 	{ NSSRC_FILES, 	NS_SUCCESS },
215248aee62SJacques Vidrine 	{ NSSRC_DNS, 	NS_SUCCESS },
216248aee62SJacques Vidrine 	{ 0 }
217248aee62SJacques Vidrine };
218248aee62SJacques Vidrine 
219a42af91cSHajimu UMEMOTO struct res_target {
220a42af91cSHajimu UMEMOTO 	struct res_target *next;
221a42af91cSHajimu UMEMOTO 	const char *name;	/* domain name */
222b826397aSJun-ichiro itojun Hagino 	int qclass, qtype;	/* class and type of query */
223a42af91cSHajimu UMEMOTO 	u_char *answer;		/* buffer to put answer */
224a42af91cSHajimu UMEMOTO 	int anslen;		/* size of answer buffer */
225a42af91cSHajimu UMEMOTO 	int n;			/* result length */
226a42af91cSHajimu UMEMOTO };
227a42af91cSHajimu UMEMOTO 
228688a5c3eSHajimu UMEMOTO #define MAXPACKET	(64*1024)
229688a5c3eSHajimu UMEMOTO 
230688a5c3eSHajimu UMEMOTO typedef union {
231688a5c3eSHajimu UMEMOTO 	HEADER hdr;
232688a5c3eSHajimu UMEMOTO 	u_char buf[MAXPACKET];
233688a5c3eSHajimu UMEMOTO } querybuf;
234688a5c3eSHajimu UMEMOTO 
235c224435eSHajimu UMEMOTO static int str2number(const char *);
2361372519bSDavid E. O'Brien static int explore_null(const struct addrinfo *,
2371372519bSDavid E. O'Brien 	const char *, struct addrinfo **);
2381372519bSDavid E. O'Brien static int explore_numeric(const struct addrinfo *, const char *,
2398755b918SHajimu UMEMOTO 	const char *, struct addrinfo **, const char *);
2401372519bSDavid E. O'Brien static int explore_numeric_scope(const struct addrinfo *, const char *,
2411372519bSDavid E. O'Brien 	const char *, struct addrinfo **);
2421372519bSDavid E. O'Brien static int get_canonname(const struct addrinfo *,
2431372519bSDavid E. O'Brien 	struct addrinfo *, const char *);
2441372519bSDavid E. O'Brien static struct addrinfo *get_ai(const struct addrinfo *,
2451372519bSDavid E. O'Brien 	const struct afd *, const char *);
246c05ac53bSDavid E. O'Brien static int get_portmatch(const struct addrinfo *, const char *);
247c05ac53bSDavid E. O'Brien static int get_port(struct addrinfo *, const char *, int);
248c05ac53bSDavid E. O'Brien static const struct afd *find_afd(int);
249bf838688SHajimu UMEMOTO static int addrconfig(struct addrinfo *);
2504d489f47SHajimu UMEMOTO static void set_source(struct ai_order *, struct policyhead *);
2514c6867a8SHajimu UMEMOTO static int comp_dst(const void *, const void *);
252a42af91cSHajimu UMEMOTO #ifdef INET6
253a89c1d30SHajimu UMEMOTO static int ip6_str2scopeid(char *, struct sockaddr_in6 *, u_int32_t *);
254a42af91cSHajimu UMEMOTO #endif
2554c6867a8SHajimu UMEMOTO static int gai_addr2scopetype(struct sockaddr *);
256a42af91cSHajimu UMEMOTO 
257688a5c3eSHajimu UMEMOTO static int explore_fqdn(const struct addrinfo *, const char *,
258688a5c3eSHajimu UMEMOTO 	const char *, struct addrinfo **);
259688a5c3eSHajimu UMEMOTO 
2604c6867a8SHajimu UMEMOTO static int reorder(struct addrinfo *);
2614c6867a8SHajimu UMEMOTO static int get_addrselectpolicy(struct policyhead *);
2624c6867a8SHajimu UMEMOTO static void free_addrselectpolicy(struct policyhead *);
2634c6867a8SHajimu UMEMOTO static struct policyqueue *match_addrselectpolicy(struct sockaddr *,
2644c6867a8SHajimu UMEMOTO 	struct policyhead *);
2654d489f47SHajimu UMEMOTO static int matchlen(struct sockaddr *, struct sockaddr *);
2664c6867a8SHajimu UMEMOTO 
2671372519bSDavid E. O'Brien static struct addrinfo *getanswer(const querybuf *, int, const char *, int,
2685342d17fSHajimu UMEMOTO 	const struct addrinfo *, res_state);
269688a5c3eSHajimu UMEMOTO #if defined(RESOLVSORT)
2705342d17fSHajimu UMEMOTO static int addr4sort(struct addrinfo *, res_state);
271688a5c3eSHajimu UMEMOTO #endif
272a89c1d30SHajimu UMEMOTO static int _dns_getaddrinfo(void *, void *, va_list);
2739def31dcSHajimu UMEMOTO static void _sethtent(FILE **);
2749def31dcSHajimu UMEMOTO static void _endhtent(FILE **);
2759def31dcSHajimu UMEMOTO static struct addrinfo *_gethtent(FILE **, const char *,
2769def31dcSHajimu UMEMOTO 	const struct addrinfo *);
277c05ac53bSDavid E. O'Brien static int _files_getaddrinfo(void *, void *, va_list);
278a42af91cSHajimu UMEMOTO #ifdef YP
279c05ac53bSDavid E. O'Brien static struct addrinfo *_yphostent(char *, const struct addrinfo *);
280c05ac53bSDavid E. O'Brien static int _yp_getaddrinfo(void *, void *, va_list);
281a42af91cSHajimu UMEMOTO #endif
282a42af91cSHajimu UMEMOTO 
2835342d17fSHajimu UMEMOTO static int res_queryN(const char *, struct res_target *, res_state);
2845342d17fSHajimu UMEMOTO static int res_searchN(const char *, struct res_target *, res_state);
2851372519bSDavid E. O'Brien static int res_querydomainN(const char *, const char *,
2865342d17fSHajimu UMEMOTO 	struct res_target *, res_state);
287a42af91cSHajimu UMEMOTO 
2887d56d374SYoshinobu Inoue /* XXX macros that make external reference is BAD. */
2897d56d374SYoshinobu Inoue 
2907d56d374SYoshinobu Inoue #define GET_AI(ai, afd, addr) \
2917d56d374SYoshinobu Inoue do { \
2927d56d374SYoshinobu Inoue 	/* external reference: pai, error, and label free */ \
2937d56d374SYoshinobu Inoue 	(ai) = get_ai(pai, (afd), (addr)); \
2947d56d374SYoshinobu Inoue 	if ((ai) == NULL) { \
2957d56d374SYoshinobu Inoue 		error = EAI_MEMORY; \
2967d56d374SYoshinobu Inoue 		goto free; \
2977d56d374SYoshinobu Inoue 	} \
298a42af91cSHajimu UMEMOTO } while (/*CONSTCOND*/0)
2997d56d374SYoshinobu Inoue 
3007d56d374SYoshinobu Inoue #define GET_PORT(ai, serv) \
3017d56d374SYoshinobu Inoue do { \
3027d56d374SYoshinobu Inoue 	/* external reference: error and label free */ \
3037d56d374SYoshinobu Inoue 	error = get_port((ai), (serv), 0); \
3047d56d374SYoshinobu Inoue 	if (error != 0) \
3057d56d374SYoshinobu Inoue 		goto free; \
306a42af91cSHajimu UMEMOTO } while (/*CONSTCOND*/0)
3077d56d374SYoshinobu Inoue 
3087d56d374SYoshinobu Inoue #define GET_CANONNAME(ai, str) \
3097d56d374SYoshinobu Inoue do { \
3107d56d374SYoshinobu Inoue 	/* external reference: pai, error and label free */ \
3117d56d374SYoshinobu Inoue 	error = get_canonname(pai, (ai), (str)); \
3127d56d374SYoshinobu Inoue 	if (error != 0) \
3137d56d374SYoshinobu Inoue 		goto free; \
314a42af91cSHajimu UMEMOTO } while (/*CONSTCOND*/0)
3157d56d374SYoshinobu Inoue 
3167d56d374SYoshinobu Inoue #define ERR(err) \
3177d56d374SYoshinobu Inoue do { \
3187d56d374SYoshinobu Inoue 	/* external reference: error, and label bad */ \
3197d56d374SYoshinobu Inoue 	error = (err); \
3207d56d374SYoshinobu Inoue 	goto bad; \
321a42af91cSHajimu UMEMOTO 	/*NOTREACHED*/ \
322a42af91cSHajimu UMEMOTO } while (/*CONSTCOND*/0)
3237d56d374SYoshinobu Inoue 
3247d56d374SYoshinobu Inoue #define MATCH_FAMILY(x, y, w) \
325a42af91cSHajimu UMEMOTO 	((x) == (y) || (/*CONSTCOND*/(w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
3267d56d374SYoshinobu Inoue #define MATCH(x, y, w) \
327a42af91cSHajimu UMEMOTO 	((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY)))
3287d56d374SYoshinobu Inoue 
3297d56d374SYoshinobu Inoue void
33090d1d883SHajimu UMEMOTO freeaddrinfo(struct addrinfo *ai)
3317d56d374SYoshinobu Inoue {
3327d56d374SYoshinobu Inoue 	struct addrinfo *next;
3337d56d374SYoshinobu Inoue 
3347d56d374SYoshinobu Inoue 	do {
3357d56d374SYoshinobu Inoue 		next = ai->ai_next;
3367d56d374SYoshinobu Inoue 		if (ai->ai_canonname)
3377d56d374SYoshinobu Inoue 			free(ai->ai_canonname);
3387d56d374SYoshinobu Inoue 		/* no need to free(ai->ai_addr) */
3397d56d374SYoshinobu Inoue 		free(ai);
340a42af91cSHajimu UMEMOTO 		ai = next;
341a42af91cSHajimu UMEMOTO 	} while (ai);
3427d56d374SYoshinobu Inoue }
3437d56d374SYoshinobu Inoue 
3447d56d374SYoshinobu Inoue static int
34590d1d883SHajimu UMEMOTO str2number(const char *p)
3467d56d374SYoshinobu Inoue {
347ec20fe00SJun-ichiro itojun Hagino 	char *ep;
348c224435eSHajimu UMEMOTO 	unsigned long v;
349ec20fe00SJun-ichiro itojun Hagino 
350ec20fe00SJun-ichiro itojun Hagino 	if (*p == '\0')
351c224435eSHajimu UMEMOTO 		return -1;
352ec20fe00SJun-ichiro itojun Hagino 	ep = NULL;
353a89c1d30SHajimu UMEMOTO 	errno = 0;
354c224435eSHajimu UMEMOTO 	v = strtoul(p, &ep, 10);
355c224435eSHajimu UMEMOTO 	if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX)
356c224435eSHajimu UMEMOTO 		return v;
357ec20fe00SJun-ichiro itojun Hagino 	else
358c224435eSHajimu UMEMOTO 		return -1;
3597d56d374SYoshinobu Inoue }
3607d56d374SYoshinobu Inoue 
3617d56d374SYoshinobu Inoue int
36290d1d883SHajimu UMEMOTO getaddrinfo(const char *hostname, const char *servname,
36390d1d883SHajimu UMEMOTO     const struct addrinfo *hints, struct addrinfo **res)
3647d56d374SYoshinobu Inoue {
3657d56d374SYoshinobu Inoue 	struct addrinfo sentinel;
3667d56d374SYoshinobu Inoue 	struct addrinfo *cur;
3677d56d374SYoshinobu Inoue 	int error = 0;
368bf838688SHajimu UMEMOTO 	struct addrinfo ai;
369bf838688SHajimu UMEMOTO 	struct addrinfo ai0;
3707d56d374SYoshinobu Inoue 	struct addrinfo *pai;
3717d56d374SYoshinobu Inoue 	const struct explore *ex;
3724c6867a8SHajimu UMEMOTO 	int numeric = 0;
3737d56d374SYoshinobu Inoue 
374a42af91cSHajimu UMEMOTO 	memset(&sentinel, 0, sizeof(sentinel));
3757d56d374SYoshinobu Inoue 	cur = &sentinel;
3767d56d374SYoshinobu Inoue 	pai = &ai;
3777d56d374SYoshinobu Inoue 	pai->ai_flags = 0;
3787d56d374SYoshinobu Inoue 	pai->ai_family = PF_UNSPEC;
3797d56d374SYoshinobu Inoue 	pai->ai_socktype = ANY;
3807d56d374SYoshinobu Inoue 	pai->ai_protocol = ANY;
3817d56d374SYoshinobu Inoue 	pai->ai_addrlen = 0;
3827d56d374SYoshinobu Inoue 	pai->ai_canonname = NULL;
3837d56d374SYoshinobu Inoue 	pai->ai_addr = NULL;
3847d56d374SYoshinobu Inoue 	pai->ai_next = NULL;
3857d56d374SYoshinobu Inoue 
3867d56d374SYoshinobu Inoue 	if (hostname == NULL && servname == NULL)
3877d56d374SYoshinobu Inoue 		return EAI_NONAME;
3887d56d374SYoshinobu Inoue 	if (hints) {
3897d56d374SYoshinobu Inoue 		/* error check for hints */
3907d56d374SYoshinobu Inoue 		if (hints->ai_addrlen || hints->ai_canonname ||
3917d56d374SYoshinobu Inoue 		    hints->ai_addr || hints->ai_next)
3927d56d374SYoshinobu Inoue 			ERR(EAI_BADHINTS); /* xxx */
3937d56d374SYoshinobu Inoue 		if (hints->ai_flags & ~AI_MASK)
3947d56d374SYoshinobu Inoue 			ERR(EAI_BADFLAGS);
3957d56d374SYoshinobu Inoue 		switch (hints->ai_family) {
3967d56d374SYoshinobu Inoue 		case PF_UNSPEC:
3977d56d374SYoshinobu Inoue 		case PF_INET:
3987d56d374SYoshinobu Inoue #ifdef INET6
3997d56d374SYoshinobu Inoue 		case PF_INET6:
4007d56d374SYoshinobu Inoue #endif
4017d56d374SYoshinobu Inoue 			break;
4027d56d374SYoshinobu Inoue 		default:
4037d56d374SYoshinobu Inoue 			ERR(EAI_FAMILY);
4047d56d374SYoshinobu Inoue 		}
4057d56d374SYoshinobu Inoue 		memcpy(pai, hints, sizeof(*pai));
4067d56d374SYoshinobu Inoue 
4077d56d374SYoshinobu Inoue 		/*
4087d56d374SYoshinobu Inoue 		 * if both socktype/protocol are specified, check if they
4097d56d374SYoshinobu Inoue 		 * are meaningful combination.
4107d56d374SYoshinobu Inoue 		 */
4117d56d374SYoshinobu Inoue 		if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
412a42af91cSHajimu UMEMOTO 			for (ex = explore; ex->e_af >= 0; ex++) {
413bf838688SHajimu UMEMOTO 				if (pai->ai_family != ex->e_af)
414a42af91cSHajimu UMEMOTO 					continue;
415bf838688SHajimu UMEMOTO 				if (ex->e_socktype == ANY)
4167d56d374SYoshinobu Inoue 					continue;
417bf838688SHajimu UMEMOTO 				if (ex->e_protocol == ANY)
4187d56d374SYoshinobu Inoue 					continue;
419bf838688SHajimu UMEMOTO 				if (pai->ai_socktype == ex->e_socktype &&
420bf838688SHajimu UMEMOTO 				    pai->ai_protocol != ex->e_protocol) {
4217d56d374SYoshinobu Inoue 					ERR(EAI_BADHINTS);
4227d56d374SYoshinobu Inoue 				}
4237d56d374SYoshinobu Inoue 			}
4247d56d374SYoshinobu Inoue 		}
425bf838688SHajimu UMEMOTO 	}
4267d56d374SYoshinobu Inoue 
4277d56d374SYoshinobu Inoue 	/*
428a42af91cSHajimu UMEMOTO 	 * post-2553: AI_ALL and AI_V4MAPPED are effective only against
429dd521ef1SHajimu UMEMOTO 	 * AF_INET6 query.  They need to be ignored if specified in other
430a42af91cSHajimu UMEMOTO 	 * occassions.
431a42af91cSHajimu UMEMOTO 	 */
432a42af91cSHajimu UMEMOTO 	switch (pai->ai_flags & (AI_ALL | AI_V4MAPPED)) {
433a42af91cSHajimu UMEMOTO 	case AI_V4MAPPED:
434a42af91cSHajimu UMEMOTO 	case AI_ALL | AI_V4MAPPED:
435a42af91cSHajimu UMEMOTO 		if (pai->ai_family != AF_INET6)
436a42af91cSHajimu UMEMOTO 			pai->ai_flags &= ~(AI_ALL | AI_V4MAPPED);
437a42af91cSHajimu UMEMOTO 		break;
438a42af91cSHajimu UMEMOTO 	case AI_ALL:
439a42af91cSHajimu UMEMOTO #if 1
440a42af91cSHajimu UMEMOTO 		/* illegal */
441a42af91cSHajimu UMEMOTO 		ERR(EAI_BADFLAGS);
442a42af91cSHajimu UMEMOTO #else
443a42af91cSHajimu UMEMOTO 		pai->ai_flags &= ~(AI_ALL | AI_V4MAPPED);
444a42af91cSHajimu UMEMOTO #endif
445a42af91cSHajimu UMEMOTO 		break;
446a42af91cSHajimu UMEMOTO 	}
447a42af91cSHajimu UMEMOTO 
448a42af91cSHajimu UMEMOTO 	/*
449a42af91cSHajimu UMEMOTO 	 * check for special cases.  (1) numeric servname is disallowed if
450a42af91cSHajimu UMEMOTO 	 * socktype/protocol are left unspecified. (2) servname is disallowed
451a42af91cSHajimu UMEMOTO 	 * for raw and other inet{,6} sockets.
4527d56d374SYoshinobu Inoue 	 */
4537d56d374SYoshinobu Inoue 	if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
454a42af91cSHajimu UMEMOTO #ifdef PF_INET6
4557d56d374SYoshinobu Inoue 	    || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)
4567d56d374SYoshinobu Inoue #endif
4577d56d374SYoshinobu Inoue 	    ) {
458a42af91cSHajimu UMEMOTO 		ai0 = *pai;	/* backup *pai */
4597d56d374SYoshinobu Inoue 
460a42af91cSHajimu UMEMOTO 		if (pai->ai_family == PF_UNSPEC) {
461a42af91cSHajimu UMEMOTO #ifdef PF_INET6
4627d56d374SYoshinobu Inoue 			pai->ai_family = PF_INET6;
4637d56d374SYoshinobu Inoue #else
4647d56d374SYoshinobu Inoue 			pai->ai_family = PF_INET;
4657d56d374SYoshinobu Inoue #endif
466a42af91cSHajimu UMEMOTO 		}
4677d56d374SYoshinobu Inoue 		error = get_portmatch(pai, servname);
4687d56d374SYoshinobu Inoue 		if (error)
4697d56d374SYoshinobu Inoue 			ERR(error);
470a42af91cSHajimu UMEMOTO 
471a42af91cSHajimu UMEMOTO 		*pai = ai0;
4727d56d374SYoshinobu Inoue 	}
4737d56d374SYoshinobu Inoue 
474a42af91cSHajimu UMEMOTO 	ai0 = *pai;
475a42af91cSHajimu UMEMOTO 
476bf838688SHajimu UMEMOTO 	/* NULL hostname, or numeric hostname */
477a42af91cSHajimu UMEMOTO 	for (ex = explore; ex->e_af >= 0; ex++) {
4787d56d374SYoshinobu Inoue 		*pai = ai0;
4797d56d374SYoshinobu Inoue 
480bf838688SHajimu UMEMOTO 		/* PF_UNSPEC entries are prepared for DNS queries only */
481bf838688SHajimu UMEMOTO 		if (ex->e_af == PF_UNSPEC)
482bf838688SHajimu UMEMOTO 			continue;
483a42af91cSHajimu UMEMOTO 
484a42af91cSHajimu UMEMOTO 		if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex)))
4857d56d374SYoshinobu Inoue 			continue;
486248aee62SJacques Vidrine 		if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex)))
487a42af91cSHajimu UMEMOTO 			continue;
488248aee62SJacques Vidrine 		if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex)))
4897d56d374SYoshinobu Inoue 			continue;
4907d56d374SYoshinobu Inoue 
4917d56d374SYoshinobu Inoue 		if (pai->ai_family == PF_UNSPEC)
492a42af91cSHajimu UMEMOTO 			pai->ai_family = ex->e_af;
4937d56d374SYoshinobu Inoue 		if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
4947d56d374SYoshinobu Inoue 			pai->ai_socktype = ex->e_socktype;
4957d56d374SYoshinobu Inoue 		if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
4967d56d374SYoshinobu Inoue 			pai->ai_protocol = ex->e_protocol;
4977d56d374SYoshinobu Inoue 
498bf838688SHajimu UMEMOTO 		if (hostname == NULL)
499bf838688SHajimu UMEMOTO 			error = explore_null(pai, servname, &cur->ai_next);
500bf838688SHajimu UMEMOTO 		else
5018755b918SHajimu UMEMOTO 			error = explore_numeric_scope(pai, hostname, servname,
5028755b918SHajimu UMEMOTO 			    &cur->ai_next);
5037d56d374SYoshinobu Inoue 
504bf838688SHajimu UMEMOTO 		if (error)
505bf838688SHajimu UMEMOTO 			goto free;
5067d56d374SYoshinobu Inoue 
5077d56d374SYoshinobu Inoue 		while (cur && cur->ai_next)
5087d56d374SYoshinobu Inoue 			cur = cur->ai_next;
5097d56d374SYoshinobu Inoue 	}
5107d56d374SYoshinobu Inoue 
511bf838688SHajimu UMEMOTO 	/*
512bf838688SHajimu UMEMOTO 	 * XXX
513bf838688SHajimu UMEMOTO 	 * If numreic representation of AF1 can be interpreted as FQDN
514bf838688SHajimu UMEMOTO 	 * representation of AF2, we need to think again about the code below.
515bf838688SHajimu UMEMOTO 	 */
5164c6867a8SHajimu UMEMOTO 	if (sentinel.ai_next) {
5174c6867a8SHajimu UMEMOTO 		numeric = 1;
518bf838688SHajimu UMEMOTO 		goto good;
5194c6867a8SHajimu UMEMOTO 	}
520bf838688SHajimu UMEMOTO 
521bf838688SHajimu UMEMOTO 	if (hostname == NULL)
522bf838688SHajimu UMEMOTO 		ERR(EAI_NONAME);	/* used to be EAI_NODATA */
523bf838688SHajimu UMEMOTO 	if (pai->ai_flags & AI_NUMERICHOST)
524bf838688SHajimu UMEMOTO 		ERR(EAI_NONAME);
525bf838688SHajimu UMEMOTO 
526bf838688SHajimu UMEMOTO 	if ((pai->ai_flags & AI_ADDRCONFIG) != 0 && !addrconfig(&ai0))
527bf838688SHajimu UMEMOTO 		ERR(EAI_FAIL);
528a42af91cSHajimu UMEMOTO 
529f95d4633SHajimu UMEMOTO 	/*
530bf838688SHajimu UMEMOTO 	 * hostname as alphabetical name.
531bf838688SHajimu UMEMOTO 	 * we would like to prefer AF_INET6 than AF_INET, so we'll make a
532bf838688SHajimu UMEMOTO 	 * outer loop by AFs.
533f95d4633SHajimu UMEMOTO 	 */
534bf838688SHajimu UMEMOTO 	for (ex = explore; ex->e_af >= 0; ex++) {
535bf838688SHajimu UMEMOTO 		*pai = ai0;
536f95d4633SHajimu UMEMOTO 
537bf838688SHajimu UMEMOTO 		/* require exact match for family field */
538bf838688SHajimu UMEMOTO 		if (pai->ai_family != ex->e_af)
539f95d4633SHajimu UMEMOTO 			continue;
540f95d4633SHajimu UMEMOTO 
541bf838688SHajimu UMEMOTO 		if (!MATCH(pai->ai_socktype, ex->e_socktype,
542bf838688SHajimu UMEMOTO 				WILD_SOCKTYPE(ex))) {
543bf838688SHajimu UMEMOTO 			continue;
544bf838688SHajimu UMEMOTO 		}
545bf838688SHajimu UMEMOTO 		if (!MATCH(pai->ai_protocol, ex->e_protocol,
546bf838688SHajimu UMEMOTO 				WILD_PROTOCOL(ex))) {
547bf838688SHajimu UMEMOTO 			continue;
548f95d4633SHajimu UMEMOTO 		}
549f95d4633SHajimu UMEMOTO 
550bf838688SHajimu UMEMOTO 		if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
551bf838688SHajimu UMEMOTO 			pai->ai_socktype = ex->e_socktype;
552bf838688SHajimu UMEMOTO 		if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
553bf838688SHajimu UMEMOTO 			pai->ai_protocol = ex->e_protocol;
554bf838688SHajimu UMEMOTO 
555bf838688SHajimu UMEMOTO 		error = explore_fqdn(pai, hostname, servname,
556bf838688SHajimu UMEMOTO 			&cur->ai_next);
557bf838688SHajimu UMEMOTO 
558bf838688SHajimu UMEMOTO 		while (cur && cur->ai_next)
559f95d4633SHajimu UMEMOTO 			cur = cur->ai_next;
560f95d4633SHajimu UMEMOTO 	}
561f95d4633SHajimu UMEMOTO 
5624c6867a8SHajimu UMEMOTO 	/* XXX inhibit errors if we have the result */
563bf838688SHajimu UMEMOTO 	if (sentinel.ai_next)
564bf838688SHajimu UMEMOTO 		error = 0;
565f95d4633SHajimu UMEMOTO 
5664c6867a8SHajimu UMEMOTO good:
5674c6867a8SHajimu UMEMOTO 	/*
5684c6867a8SHajimu UMEMOTO 	 * ensure we return either:
5694c6867a8SHajimu UMEMOTO 	 * - error == 0, non-NULL *res
5704c6867a8SHajimu UMEMOTO 	 * - error != 0, NULL *res
5714c6867a8SHajimu UMEMOTO 	 */
572bf838688SHajimu UMEMOTO 	if (error == 0) {
573bf838688SHajimu UMEMOTO 		if (sentinel.ai_next) {
5744c6867a8SHajimu UMEMOTO 			/*
5754c6867a8SHajimu UMEMOTO 			 * If the returned entry is for an active connection,
5764c6867a8SHajimu UMEMOTO 			 * and the given name is not numeric, reorder the
5774c6867a8SHajimu UMEMOTO 			 * list, so that the application would try the list
5784c6867a8SHajimu UMEMOTO 			 * in the most efficient order.
5794c6867a8SHajimu UMEMOTO 			 */
5804c6867a8SHajimu UMEMOTO 			if (hints == NULL || !(hints->ai_flags & AI_PASSIVE)) {
5814c6867a8SHajimu UMEMOTO 				if (!numeric)
5824c6867a8SHajimu UMEMOTO 					(void)reorder(&sentinel);
5834c6867a8SHajimu UMEMOTO 			}
584bf838688SHajimu UMEMOTO 			*res = sentinel.ai_next;
585bf838688SHajimu UMEMOTO 			return SUCCESS;
586bf838688SHajimu UMEMOTO 		} else
587bf838688SHajimu UMEMOTO 			error = EAI_FAIL;
588bf838688SHajimu UMEMOTO 	}
589bf838688SHajimu UMEMOTO free:
590bf838688SHajimu UMEMOTO bad:
591bf838688SHajimu UMEMOTO 	if (sentinel.ai_next)
592f95d4633SHajimu UMEMOTO 		freeaddrinfo(sentinel.ai_next);
593bf838688SHajimu UMEMOTO 	*res = NULL;
5947d56d374SYoshinobu Inoue 	return error;
5957d56d374SYoshinobu Inoue }
5967d56d374SYoshinobu Inoue 
5974c6867a8SHajimu UMEMOTO static int
59890d1d883SHajimu UMEMOTO reorder(struct addrinfo *sentinel)
5994c6867a8SHajimu UMEMOTO {
6004c6867a8SHajimu UMEMOTO 	struct addrinfo *ai, **aip;
6014c6867a8SHajimu UMEMOTO 	struct ai_order *aio;
6024c6867a8SHajimu UMEMOTO 	int i, n;
6034c6867a8SHajimu UMEMOTO 	struct policyhead policyhead;
6044c6867a8SHajimu UMEMOTO 
6054c6867a8SHajimu UMEMOTO 	/* count the number of addrinfo elements for sorting. */
6064c6867a8SHajimu UMEMOTO 	for (n = 0, ai = sentinel->ai_next; ai != NULL; ai = ai->ai_next, n++)
6074c6867a8SHajimu UMEMOTO 		;
6084c6867a8SHajimu UMEMOTO 
6094c6867a8SHajimu UMEMOTO 	/*
6104c6867a8SHajimu UMEMOTO 	 * If the number is small enough, we can skip the reordering process.
6114c6867a8SHajimu UMEMOTO 	 */
6124c6867a8SHajimu UMEMOTO 	if (n <= 1)
6134c6867a8SHajimu UMEMOTO 		return(n);
6144c6867a8SHajimu UMEMOTO 
6154c6867a8SHajimu UMEMOTO 	/* allocate a temporary array for sort and initialization of it. */
6164c6867a8SHajimu UMEMOTO 	if ((aio = malloc(sizeof(*aio) * n)) == NULL)
6174c6867a8SHajimu UMEMOTO 		return(n);	/* give up reordering */
6184c6867a8SHajimu UMEMOTO 	memset(aio, 0, sizeof(*aio) * n);
6194c6867a8SHajimu UMEMOTO 
6204c6867a8SHajimu UMEMOTO 	/* retrieve address selection policy from the kernel */
6214c6867a8SHajimu UMEMOTO 	TAILQ_INIT(&policyhead);
6224c6867a8SHajimu UMEMOTO 	if (!get_addrselectpolicy(&policyhead)) {
6234c6867a8SHajimu UMEMOTO 		/* no policy is installed into kernel, we don't sort. */
6244c6867a8SHajimu UMEMOTO 		free(aio);
6254c6867a8SHajimu UMEMOTO 		return (n);
6264c6867a8SHajimu UMEMOTO 	}
6274c6867a8SHajimu UMEMOTO 
6284c6867a8SHajimu UMEMOTO 	for (i = 0, ai = sentinel->ai_next; i < n; ai = ai->ai_next, i++) {
6294c6867a8SHajimu UMEMOTO 		aio[i].aio_ai = ai;
6304c6867a8SHajimu UMEMOTO 		aio[i].aio_dstscope = gai_addr2scopetype(ai->ai_addr);
6314c6867a8SHajimu UMEMOTO 		aio[i].aio_dstpolicy = match_addrselectpolicy(ai->ai_addr,
6324c6867a8SHajimu UMEMOTO 							      &policyhead);
6334d489f47SHajimu UMEMOTO 		set_source(&aio[i], &policyhead);
6344c6867a8SHajimu UMEMOTO 	}
6354c6867a8SHajimu UMEMOTO 
6364c6867a8SHajimu UMEMOTO 	/* perform sorting. */
6374c6867a8SHajimu UMEMOTO 	qsort(aio, n, sizeof(*aio), comp_dst);
6384c6867a8SHajimu UMEMOTO 
6394c6867a8SHajimu UMEMOTO 	/* reorder the addrinfo chain. */
6404c6867a8SHajimu UMEMOTO 	for (i = 0, aip = &sentinel->ai_next; i < n; i++) {
6414c6867a8SHajimu UMEMOTO 		*aip = aio[i].aio_ai;
6424c6867a8SHajimu UMEMOTO 		aip = &aio[i].aio_ai->ai_next;
6434c6867a8SHajimu UMEMOTO 	}
6444c6867a8SHajimu UMEMOTO 	*aip = NULL;
6454c6867a8SHajimu UMEMOTO 
6464c6867a8SHajimu UMEMOTO 	/* cleanup and return */
6474c6867a8SHajimu UMEMOTO 	free(aio);
6484c6867a8SHajimu UMEMOTO 	free_addrselectpolicy(&policyhead);
6494c6867a8SHajimu UMEMOTO 	return(n);
6504c6867a8SHajimu UMEMOTO }
6514c6867a8SHajimu UMEMOTO 
6524c6867a8SHajimu UMEMOTO static int
65390d1d883SHajimu UMEMOTO get_addrselectpolicy(struct policyhead *head)
6544c6867a8SHajimu UMEMOTO {
6554c6867a8SHajimu UMEMOTO #ifdef INET6
6564c6867a8SHajimu UMEMOTO 	int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_ADDRCTLPOLICY };
6574c6867a8SHajimu UMEMOTO 	size_t l;
6584c6867a8SHajimu UMEMOTO 	char *buf;
6594c6867a8SHajimu UMEMOTO 	struct in6_addrpolicy *pol, *ep;
6604c6867a8SHajimu UMEMOTO 
6614c6867a8SHajimu UMEMOTO 	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0)
6624c6867a8SHajimu UMEMOTO 		return (0);
6634c6867a8SHajimu UMEMOTO 	if ((buf = malloc(l)) == NULL)
6644c6867a8SHajimu UMEMOTO 		return (0);
6654c6867a8SHajimu UMEMOTO 	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
6664c6867a8SHajimu UMEMOTO 		free(buf);
6674c6867a8SHajimu UMEMOTO 		return (0);
6684c6867a8SHajimu UMEMOTO 	}
6694c6867a8SHajimu UMEMOTO 
6704c6867a8SHajimu UMEMOTO 	ep = (struct in6_addrpolicy *)(buf + l);
6714c6867a8SHajimu UMEMOTO 	for (pol = (struct in6_addrpolicy *)buf; pol + 1 <= ep; pol++) {
6724c6867a8SHajimu UMEMOTO 		struct policyqueue *new;
6734c6867a8SHajimu UMEMOTO 
6744c6867a8SHajimu UMEMOTO 		if ((new = malloc(sizeof(*new))) == NULL) {
6754c6867a8SHajimu UMEMOTO 			free_addrselectpolicy(head); /* make the list empty */
6764c6867a8SHajimu UMEMOTO 			break;
6774c6867a8SHajimu UMEMOTO 		}
6784c6867a8SHajimu UMEMOTO 		new->pc_policy = *pol;
6794c6867a8SHajimu UMEMOTO 		TAILQ_INSERT_TAIL(head, new, pc_entry);
6804c6867a8SHajimu UMEMOTO 	}
6814c6867a8SHajimu UMEMOTO 
6824c6867a8SHajimu UMEMOTO 	free(buf);
6834c6867a8SHajimu UMEMOTO 	return (1);
6844c6867a8SHajimu UMEMOTO #else
6854c6867a8SHajimu UMEMOTO 	return (0);
6864c6867a8SHajimu UMEMOTO #endif
6874c6867a8SHajimu UMEMOTO }
6884c6867a8SHajimu UMEMOTO 
6894c6867a8SHajimu UMEMOTO static void
69090d1d883SHajimu UMEMOTO free_addrselectpolicy(struct policyhead *head)
6914c6867a8SHajimu UMEMOTO {
6924c6867a8SHajimu UMEMOTO 	struct policyqueue *ent, *nent;
6934c6867a8SHajimu UMEMOTO 
6944c6867a8SHajimu UMEMOTO 	for (ent = TAILQ_FIRST(head); ent; ent = nent) {
6954c6867a8SHajimu UMEMOTO 		nent = TAILQ_NEXT(ent, pc_entry);
6964c6867a8SHajimu UMEMOTO 		TAILQ_REMOVE(head, ent, pc_entry);
6974c6867a8SHajimu UMEMOTO 		free(ent);
6984c6867a8SHajimu UMEMOTO 	}
6994c6867a8SHajimu UMEMOTO }
7004c6867a8SHajimu UMEMOTO 
7014c6867a8SHajimu UMEMOTO static struct policyqueue *
70290d1d883SHajimu UMEMOTO match_addrselectpolicy(struct sockaddr *addr, struct policyhead *head)
7034c6867a8SHajimu UMEMOTO {
7044c6867a8SHajimu UMEMOTO #ifdef INET6
7054c6867a8SHajimu UMEMOTO 	struct policyqueue *ent, *bestent = NULL;
7064c6867a8SHajimu UMEMOTO 	struct in6_addrpolicy *pol;
7074c6867a8SHajimu UMEMOTO 	int matchlen, bestmatchlen = -1;
7084c6867a8SHajimu UMEMOTO 	u_char *mp, *ep, *k, *p, m;
7094c6867a8SHajimu UMEMOTO 	struct sockaddr_in6 key;
7104c6867a8SHajimu UMEMOTO 
7114c6867a8SHajimu UMEMOTO 	switch(addr->sa_family) {
7124c6867a8SHajimu UMEMOTO 	case AF_INET6:
7134c6867a8SHajimu UMEMOTO 		key = *(struct sockaddr_in6 *)addr;
7144c6867a8SHajimu UMEMOTO 		break;
7154c6867a8SHajimu UMEMOTO 	case AF_INET:
7164c6867a8SHajimu UMEMOTO 		/* convert the address into IPv4-mapped IPv6 address. */
7174c6867a8SHajimu UMEMOTO 		memset(&key, 0, sizeof(key));
7184c6867a8SHajimu UMEMOTO 		key.sin6_family = AF_INET6;
7194c6867a8SHajimu UMEMOTO 		key.sin6_len = sizeof(key);
7204c6867a8SHajimu UMEMOTO 		key.sin6_addr.s6_addr[10] = 0xff;
7214c6867a8SHajimu UMEMOTO 		key.sin6_addr.s6_addr[11] = 0xff;
7224c6867a8SHajimu UMEMOTO 		memcpy(&key.sin6_addr.s6_addr[12],
7234c6867a8SHajimu UMEMOTO 		       &((struct sockaddr_in *)addr)->sin_addr, 4);
7244c6867a8SHajimu UMEMOTO 		break;
7254c6867a8SHajimu UMEMOTO 	default:
7264c6867a8SHajimu UMEMOTO 		return(NULL);
7274c6867a8SHajimu UMEMOTO 	}
7284c6867a8SHajimu UMEMOTO 
7294c6867a8SHajimu UMEMOTO 	for (ent = TAILQ_FIRST(head); ent; ent = TAILQ_NEXT(ent, pc_entry)) {
7304c6867a8SHajimu UMEMOTO 		pol = &ent->pc_policy;
7314c6867a8SHajimu UMEMOTO 		matchlen = 0;
7324c6867a8SHajimu UMEMOTO 
7334c6867a8SHajimu UMEMOTO 		mp = (u_char *)&pol->addrmask.sin6_addr;
7344c6867a8SHajimu UMEMOTO 		ep = mp + 16;	/* XXX: scope field? */
7354c6867a8SHajimu UMEMOTO 		k = (u_char *)&key.sin6_addr;
7364c6867a8SHajimu UMEMOTO 		p = (u_char *)&pol->addr.sin6_addr;
7374c6867a8SHajimu UMEMOTO 		for (; mp < ep && *mp; mp++, k++, p++) {
7384c6867a8SHajimu UMEMOTO 			m = *mp;
7394c6867a8SHajimu UMEMOTO 			if ((*k & m) != *p)
7404c6867a8SHajimu UMEMOTO 				goto next; /* not match */
7414c6867a8SHajimu UMEMOTO 			if (m == 0xff) /* short cut for a typical case */
7424c6867a8SHajimu UMEMOTO 				matchlen += 8;
7434c6867a8SHajimu UMEMOTO 			else {
7444c6867a8SHajimu UMEMOTO 				while (m >= 0x80) {
7454c6867a8SHajimu UMEMOTO 					matchlen++;
7464c6867a8SHajimu UMEMOTO 					m <<= 1;
7474c6867a8SHajimu UMEMOTO 				}
7484c6867a8SHajimu UMEMOTO 			}
7494c6867a8SHajimu UMEMOTO 		}
7504c6867a8SHajimu UMEMOTO 
7514c6867a8SHajimu UMEMOTO 		/* matched.  check if this is better than the current best. */
7524c6867a8SHajimu UMEMOTO 		if (matchlen > bestmatchlen) {
7534c6867a8SHajimu UMEMOTO 			bestent = ent;
7544c6867a8SHajimu UMEMOTO 			bestmatchlen = matchlen;
7554c6867a8SHajimu UMEMOTO 		}
7564c6867a8SHajimu UMEMOTO 
7574c6867a8SHajimu UMEMOTO 	  next:
7584c6867a8SHajimu UMEMOTO 		continue;
7594c6867a8SHajimu UMEMOTO 	}
7604c6867a8SHajimu UMEMOTO 
7614c6867a8SHajimu UMEMOTO 	return(bestent);
7624c6867a8SHajimu UMEMOTO #else
7634c6867a8SHajimu UMEMOTO 	return(NULL);
7644c6867a8SHajimu UMEMOTO #endif
7654c6867a8SHajimu UMEMOTO 
7664c6867a8SHajimu UMEMOTO }
7674c6867a8SHajimu UMEMOTO 
7684d489f47SHajimu UMEMOTO static void
76990d1d883SHajimu UMEMOTO set_source(struct ai_order *aio, struct policyhead *ph)
7704d489f47SHajimu UMEMOTO {
7714d489f47SHajimu UMEMOTO 	struct addrinfo ai = *aio->aio_ai;
7724d489f47SHajimu UMEMOTO 	struct sockaddr_storage ss;
773943db5a2SHajimu UMEMOTO 	socklen_t srclen;
774943db5a2SHajimu UMEMOTO 	int s;
7754d489f47SHajimu UMEMOTO 
7764d489f47SHajimu UMEMOTO 	/* set unspec ("no source is available"), just in case */
7774d489f47SHajimu UMEMOTO 	aio->aio_srcsa.sa_family = AF_UNSPEC;
7784d489f47SHajimu UMEMOTO 	aio->aio_srcscope = -1;
7794d489f47SHajimu UMEMOTO 
7804d489f47SHajimu UMEMOTO 	switch(ai.ai_family) {
7814d489f47SHajimu UMEMOTO 	case AF_INET:
7824d489f47SHajimu UMEMOTO #ifdef INET6
7834d489f47SHajimu UMEMOTO 	case AF_INET6:
7844d489f47SHajimu UMEMOTO #endif
7854d489f47SHajimu UMEMOTO 		break;
7864d489f47SHajimu UMEMOTO 	default:		/* ignore unsupported AFs explicitly */
7874d489f47SHajimu UMEMOTO 		return;
7884d489f47SHajimu UMEMOTO 	}
7894d489f47SHajimu UMEMOTO 
7904d489f47SHajimu UMEMOTO 	/* XXX: make a dummy addrinfo to call connect() */
7914d489f47SHajimu UMEMOTO 	ai.ai_socktype = SOCK_DGRAM;
7924d489f47SHajimu UMEMOTO 	ai.ai_protocol = IPPROTO_UDP; /* is UDP too specific? */
7934d489f47SHajimu UMEMOTO 	ai.ai_next = NULL;
7944d489f47SHajimu UMEMOTO 	memset(&ss, 0, sizeof(ss));
7954d489f47SHajimu UMEMOTO 	memcpy(&ss, ai.ai_addr, ai.ai_addrlen);
7964d489f47SHajimu UMEMOTO 	ai.ai_addr = (struct sockaddr *)&ss;
7974d489f47SHajimu UMEMOTO 	get_port(&ai, "1", 0);
7984d489f47SHajimu UMEMOTO 
7994d489f47SHajimu UMEMOTO 	/* open a socket to get the source address for the given dst */
8004d489f47SHajimu UMEMOTO 	if ((s = _socket(ai.ai_family, ai.ai_socktype, ai.ai_protocol)) < 0)
8014d489f47SHajimu UMEMOTO 		return;		/* give up */
8024d489f47SHajimu UMEMOTO 	if (_connect(s, ai.ai_addr, ai.ai_addrlen) < 0)
8034d489f47SHajimu UMEMOTO 		goto cleanup;
8044d489f47SHajimu UMEMOTO 	srclen = ai.ai_addrlen;
8054d489f47SHajimu UMEMOTO 	if (_getsockname(s, &aio->aio_srcsa, &srclen) < 0) {
8064d489f47SHajimu UMEMOTO 		aio->aio_srcsa.sa_family = AF_UNSPEC;
8074d489f47SHajimu UMEMOTO 		goto cleanup;
8084d489f47SHajimu UMEMOTO 	}
8094d489f47SHajimu UMEMOTO 	aio->aio_srcscope = gai_addr2scopetype(&aio->aio_srcsa);
8104d489f47SHajimu UMEMOTO 	aio->aio_srcpolicy = match_addrselectpolicy(&aio->aio_srcsa, ph);
8114d489f47SHajimu UMEMOTO 	aio->aio_matchlen = matchlen(&aio->aio_srcsa, aio->aio_ai->ai_addr);
8124d489f47SHajimu UMEMOTO #ifdef INET6
8134d489f47SHajimu UMEMOTO 	if (ai.ai_family == AF_INET6) {
8144d489f47SHajimu UMEMOTO 		struct in6_ifreq ifr6;
8154d489f47SHajimu UMEMOTO 		u_int32_t flags6;
8164d489f47SHajimu UMEMOTO 
8174d489f47SHajimu UMEMOTO 		/* XXX: interface name should not be hardcoded */
8184d489f47SHajimu UMEMOTO 		strncpy(ifr6.ifr_name, "lo0", sizeof(ifr6.ifr_name));
8194d489f47SHajimu UMEMOTO 		memset(&ifr6, 0, sizeof(ifr6));
8204d489f47SHajimu UMEMOTO 		memcpy(&ifr6.ifr_addr, ai.ai_addr, ai.ai_addrlen);
8214d489f47SHajimu UMEMOTO 		if (_ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == 0) {
8224d489f47SHajimu UMEMOTO 			flags6 = ifr6.ifr_ifru.ifru_flags6;
8234d489f47SHajimu UMEMOTO 			if ((flags6 & IN6_IFF_DEPRECATED))
8244d489f47SHajimu UMEMOTO 				aio->aio_srcflag |= AIO_SRCFLAG_DEPRECATED;
8254d489f47SHajimu UMEMOTO 		}
8264d489f47SHajimu UMEMOTO 	}
8274d489f47SHajimu UMEMOTO #endif
8284d489f47SHajimu UMEMOTO 
8294d489f47SHajimu UMEMOTO   cleanup:
8304d489f47SHajimu UMEMOTO 	_close(s);
8314d489f47SHajimu UMEMOTO 	return;
8324d489f47SHajimu UMEMOTO }
8334d489f47SHajimu UMEMOTO 
8344d489f47SHajimu UMEMOTO static int
83590d1d883SHajimu UMEMOTO matchlen(struct sockaddr *src, struct sockaddr *dst)
8364d489f47SHajimu UMEMOTO {
8374d489f47SHajimu UMEMOTO 	int match = 0;
8384d489f47SHajimu UMEMOTO 	u_char *s, *d;
8394d489f47SHajimu UMEMOTO 	u_char *lim, r;
8404d489f47SHajimu UMEMOTO 	int addrlen;
8414d489f47SHajimu UMEMOTO 
8424d489f47SHajimu UMEMOTO 	switch (src->sa_family) {
8434d489f47SHajimu UMEMOTO #ifdef INET6
8444d489f47SHajimu UMEMOTO 	case AF_INET6:
8454d489f47SHajimu UMEMOTO 		s = (u_char *)&((struct sockaddr_in6 *)src)->sin6_addr;
8464d489f47SHajimu UMEMOTO 		d = (u_char *)&((struct sockaddr_in6 *)dst)->sin6_addr;
8474d489f47SHajimu UMEMOTO 		addrlen = sizeof(struct in6_addr);
8484d489f47SHajimu UMEMOTO 		lim = s + addrlen;
8494d489f47SHajimu UMEMOTO 		break;
8504d489f47SHajimu UMEMOTO #endif
8514d489f47SHajimu UMEMOTO 	case AF_INET:
852fd57e549SGeorge V. Neville-Neil 		s = (u_char *)&((struct sockaddr_in *)src)->sin_addr;
853fd57e549SGeorge V. Neville-Neil 		d = (u_char *)&((struct sockaddr_in *)dst)->sin_addr;
8544d489f47SHajimu UMEMOTO 		addrlen = sizeof(struct in_addr);
8554d489f47SHajimu UMEMOTO 		lim = s + addrlen;
8564d489f47SHajimu UMEMOTO 		break;
8574d489f47SHajimu UMEMOTO 	default:
8584d489f47SHajimu UMEMOTO 		return(0);
8594d489f47SHajimu UMEMOTO 	}
8604d489f47SHajimu UMEMOTO 
8614d489f47SHajimu UMEMOTO 	while (s < lim)
8624d489f47SHajimu UMEMOTO 		if ((r = (*d++ ^ *s++)) != 0) {
8634d489f47SHajimu UMEMOTO 			while (r < addrlen * 8) {
8644d489f47SHajimu UMEMOTO 				match++;
8654d489f47SHajimu UMEMOTO 				r <<= 1;
8664d489f47SHajimu UMEMOTO 			}
8674d489f47SHajimu UMEMOTO 			break;
8684d489f47SHajimu UMEMOTO 		} else
8694d489f47SHajimu UMEMOTO 			match += 8;
8704d489f47SHajimu UMEMOTO 	return(match);
8714d489f47SHajimu UMEMOTO }
8724d489f47SHajimu UMEMOTO 
8734c6867a8SHajimu UMEMOTO static int
87490d1d883SHajimu UMEMOTO comp_dst(const void *arg1, const void *arg2)
8754c6867a8SHajimu UMEMOTO {
8764c6867a8SHajimu UMEMOTO 	const struct ai_order *dst1 = arg1, *dst2 = arg2;
8774c6867a8SHajimu UMEMOTO 
8784c6867a8SHajimu UMEMOTO 	/*
8794c6867a8SHajimu UMEMOTO 	 * Rule 1: Avoid unusable destinations.
8804c6867a8SHajimu UMEMOTO 	 * XXX: we currently do not consider if an appropriate route exists.
8814c6867a8SHajimu UMEMOTO 	 */
8824c6867a8SHajimu UMEMOTO 	if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
8834c6867a8SHajimu UMEMOTO 	    dst2->aio_srcsa.sa_family == AF_UNSPEC) {
8844c6867a8SHajimu UMEMOTO 		return(-1);
8854c6867a8SHajimu UMEMOTO 	}
8864c6867a8SHajimu UMEMOTO 	if (dst1->aio_srcsa.sa_family == AF_UNSPEC &&
8874c6867a8SHajimu UMEMOTO 	    dst2->aio_srcsa.sa_family != AF_UNSPEC) {
8884c6867a8SHajimu UMEMOTO 		return(1);
8894c6867a8SHajimu UMEMOTO 	}
8904c6867a8SHajimu UMEMOTO 
8914c6867a8SHajimu UMEMOTO 	/* Rule 2: Prefer matching scope. */
8924c6867a8SHajimu UMEMOTO 	if (dst1->aio_dstscope == dst1->aio_srcscope &&
8934c6867a8SHajimu UMEMOTO 	    dst2->aio_dstscope != dst2->aio_srcscope) {
8944c6867a8SHajimu UMEMOTO 		return(-1);
8954c6867a8SHajimu UMEMOTO 	}
8964c6867a8SHajimu UMEMOTO 	if (dst1->aio_dstscope != dst1->aio_srcscope &&
8974c6867a8SHajimu UMEMOTO 	    dst2->aio_dstscope == dst2->aio_srcscope) {
8984c6867a8SHajimu UMEMOTO 		return(1);
8994c6867a8SHajimu UMEMOTO 	}
9004c6867a8SHajimu UMEMOTO 
9014c6867a8SHajimu UMEMOTO 	/* Rule 3: Avoid deprecated addresses. */
9024c6867a8SHajimu UMEMOTO 	if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
9034c6867a8SHajimu UMEMOTO 	    dst2->aio_srcsa.sa_family != AF_UNSPEC) {
9044c6867a8SHajimu UMEMOTO 		if (!(dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
9054c6867a8SHajimu UMEMOTO 		    (dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
9064c6867a8SHajimu UMEMOTO 			return(-1);
9074c6867a8SHajimu UMEMOTO 		}
9084c6867a8SHajimu UMEMOTO 		if ((dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
9094c6867a8SHajimu UMEMOTO 		    !(dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
9104c6867a8SHajimu UMEMOTO 			return(1);
9114c6867a8SHajimu UMEMOTO 		}
9124c6867a8SHajimu UMEMOTO 	}
9134c6867a8SHajimu UMEMOTO 
9144c6867a8SHajimu UMEMOTO 	/* Rule 4: Prefer home addresses. */
9154c6867a8SHajimu UMEMOTO 	/* XXX: not implemented yet */
9164c6867a8SHajimu UMEMOTO 
9174c6867a8SHajimu UMEMOTO 	/* Rule 5: Prefer matching label. */
9184c6867a8SHajimu UMEMOTO #ifdef INET6
9194c6867a8SHajimu UMEMOTO 	if (dst1->aio_srcpolicy && dst1->aio_dstpolicy &&
9204c6867a8SHajimu UMEMOTO 	    dst1->aio_srcpolicy->pc_policy.label ==
9214c6867a8SHajimu UMEMOTO 	    dst1->aio_dstpolicy->pc_policy.label &&
9224c6867a8SHajimu UMEMOTO 	    (dst2->aio_srcpolicy == NULL || dst2->aio_dstpolicy == NULL ||
9234c6867a8SHajimu UMEMOTO 	     dst2->aio_srcpolicy->pc_policy.label !=
9244c6867a8SHajimu UMEMOTO 	     dst2->aio_dstpolicy->pc_policy.label)) {
9254c6867a8SHajimu UMEMOTO 		return(-1);
9264c6867a8SHajimu UMEMOTO 	}
9274c6867a8SHajimu UMEMOTO 	if (dst2->aio_srcpolicy && dst2->aio_dstpolicy &&
9284c6867a8SHajimu UMEMOTO 	    dst2->aio_srcpolicy->pc_policy.label ==
9294c6867a8SHajimu UMEMOTO 	    dst2->aio_dstpolicy->pc_policy.label &&
9304c6867a8SHajimu UMEMOTO 	    (dst1->aio_srcpolicy == NULL || dst1->aio_dstpolicy == NULL ||
9314c6867a8SHajimu UMEMOTO 	     dst1->aio_srcpolicy->pc_policy.label !=
9324c6867a8SHajimu UMEMOTO 	     dst1->aio_dstpolicy->pc_policy.label)) {
9334c6867a8SHajimu UMEMOTO 		return(1);
9344c6867a8SHajimu UMEMOTO 	}
9354c6867a8SHajimu UMEMOTO #endif
9364c6867a8SHajimu UMEMOTO 
9374c6867a8SHajimu UMEMOTO 	/* Rule 6: Prefer higher precedence. */
9384c6867a8SHajimu UMEMOTO #ifdef INET6
9394c6867a8SHajimu UMEMOTO 	if (dst1->aio_dstpolicy &&
9404c6867a8SHajimu UMEMOTO 	    (dst2->aio_dstpolicy == NULL ||
9414c6867a8SHajimu UMEMOTO 	     dst1->aio_dstpolicy->pc_policy.preced >
9424c6867a8SHajimu UMEMOTO 	     dst2->aio_dstpolicy->pc_policy.preced)) {
9434c6867a8SHajimu UMEMOTO 		return(-1);
9444c6867a8SHajimu UMEMOTO 	}
9454c6867a8SHajimu UMEMOTO 	if (dst2->aio_dstpolicy &&
9464c6867a8SHajimu UMEMOTO 	    (dst1->aio_dstpolicy == NULL ||
9474c6867a8SHajimu UMEMOTO 	     dst2->aio_dstpolicy->pc_policy.preced >
9484c6867a8SHajimu UMEMOTO 	     dst1->aio_dstpolicy->pc_policy.preced)) {
9494c6867a8SHajimu UMEMOTO 		return(1);
9504c6867a8SHajimu UMEMOTO 	}
9514c6867a8SHajimu UMEMOTO #endif
9524c6867a8SHajimu UMEMOTO 
9534c6867a8SHajimu UMEMOTO 	/* Rule 7: Prefer native transport. */
9544c6867a8SHajimu UMEMOTO 	/* XXX: not implemented yet */
9554c6867a8SHajimu UMEMOTO 
9564c6867a8SHajimu UMEMOTO 	/* Rule 8: Prefer smaller scope. */
9574c6867a8SHajimu UMEMOTO 	if (dst1->aio_dstscope >= 0 &&
9584c6867a8SHajimu UMEMOTO 	    dst1->aio_dstscope < dst2->aio_dstscope) {
9594c6867a8SHajimu UMEMOTO 		return(-1);
9604c6867a8SHajimu UMEMOTO 	}
9614c6867a8SHajimu UMEMOTO 	if (dst2->aio_dstscope >= 0 &&
9624c6867a8SHajimu UMEMOTO 	    dst2->aio_dstscope < dst1->aio_dstscope) {
9634c6867a8SHajimu UMEMOTO 		return(1);
9644c6867a8SHajimu UMEMOTO 	}
9654c6867a8SHajimu UMEMOTO 
9664c6867a8SHajimu UMEMOTO 	/*
9674c6867a8SHajimu UMEMOTO 	 * Rule 9: Use longest matching prefix.
9684c6867a8SHajimu UMEMOTO 	 * We compare the match length in a same AF only.
9694c6867a8SHajimu UMEMOTO 	 */
9704c6867a8SHajimu UMEMOTO 	if (dst1->aio_ai->ai_addr->sa_family ==
9714c6867a8SHajimu UMEMOTO 	    dst2->aio_ai->ai_addr->sa_family) {
9724c6867a8SHajimu UMEMOTO 		if (dst1->aio_matchlen > dst2->aio_matchlen) {
9734c6867a8SHajimu UMEMOTO 			return(-1);
9744c6867a8SHajimu UMEMOTO 		}
9754c6867a8SHajimu UMEMOTO 		if (dst1->aio_matchlen < dst2->aio_matchlen) {
9764c6867a8SHajimu UMEMOTO 			return(1);
9774c6867a8SHajimu UMEMOTO 		}
9784c6867a8SHajimu UMEMOTO 	}
9794c6867a8SHajimu UMEMOTO 
9804c6867a8SHajimu UMEMOTO 	/* Rule 10: Otherwise, leave the order unchanged. */
9814c6867a8SHajimu UMEMOTO 	return(-1);
9824c6867a8SHajimu UMEMOTO }
9834c6867a8SHajimu UMEMOTO 
9844c6867a8SHajimu UMEMOTO /*
9854c6867a8SHajimu UMEMOTO  * Copy from scope.c.
9864c6867a8SHajimu UMEMOTO  * XXX: we should standardize the functions and link them as standard
9874c6867a8SHajimu UMEMOTO  * library.
9884c6867a8SHajimu UMEMOTO  */
9894c6867a8SHajimu UMEMOTO static int
99090d1d883SHajimu UMEMOTO gai_addr2scopetype(struct sockaddr *sa)
9914c6867a8SHajimu UMEMOTO {
9924c6867a8SHajimu UMEMOTO #ifdef INET6
9934c6867a8SHajimu UMEMOTO 	struct sockaddr_in6 *sa6;
9944c6867a8SHajimu UMEMOTO #endif
9954c6867a8SHajimu UMEMOTO 	struct sockaddr_in *sa4;
9964c6867a8SHajimu UMEMOTO 
9974c6867a8SHajimu UMEMOTO 	switch(sa->sa_family) {
9984c6867a8SHajimu UMEMOTO #ifdef INET6
9994c6867a8SHajimu UMEMOTO 	case AF_INET6:
10004c6867a8SHajimu UMEMOTO 		sa6 = (struct sockaddr_in6 *)sa;
10014c6867a8SHajimu UMEMOTO 		if (IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
10024c6867a8SHajimu UMEMOTO 			/* just use the scope field of the multicast address */
10034c6867a8SHajimu UMEMOTO 			return(sa6->sin6_addr.s6_addr[2] & 0x0f);
10044c6867a8SHajimu UMEMOTO 		}
10054c6867a8SHajimu UMEMOTO 		/*
10064c6867a8SHajimu UMEMOTO 		 * Unicast addresses: map scope type to corresponding scope
10074c6867a8SHajimu UMEMOTO 		 * value defined for multcast addresses.
10084c6867a8SHajimu UMEMOTO 		 * XXX: hardcoded scope type values are bad...
10094c6867a8SHajimu UMEMOTO 		 */
10104c6867a8SHajimu UMEMOTO 		if (IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr))
10114c6867a8SHajimu UMEMOTO 			return(1); /* node local scope */
10124c6867a8SHajimu UMEMOTO 		if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr))
10134c6867a8SHajimu UMEMOTO 			return(2); /* link-local scope */
10144c6867a8SHajimu UMEMOTO 		if (IN6_IS_ADDR_SITELOCAL(&sa6->sin6_addr))
10154c6867a8SHajimu UMEMOTO 			return(5); /* site-local scope */
10164c6867a8SHajimu UMEMOTO 		return(14);	/* global scope */
10174c6867a8SHajimu UMEMOTO 		break;
10184c6867a8SHajimu UMEMOTO #endif
10194c6867a8SHajimu UMEMOTO 	case AF_INET:
10204c6867a8SHajimu UMEMOTO 		/*
10214c6867a8SHajimu UMEMOTO 		 * IPv4 pseudo scoping according to RFC 3484.
10224c6867a8SHajimu UMEMOTO 		 */
10234c6867a8SHajimu UMEMOTO 		sa4 = (struct sockaddr_in *)sa;
10244c6867a8SHajimu UMEMOTO 		/* IPv4 autoconfiguration addresses have link-local scope. */
10254c6867a8SHajimu UMEMOTO 		if (((u_char *)&sa4->sin_addr)[0] == 169 &&
10264c6867a8SHajimu UMEMOTO 		    ((u_char *)&sa4->sin_addr)[1] == 254)
10274c6867a8SHajimu UMEMOTO 			return(2);
10284c6867a8SHajimu UMEMOTO 		/* Private addresses have site-local scope. */
10294c6867a8SHajimu UMEMOTO 		if (((u_char *)&sa4->sin_addr)[0] == 10 ||
10304c6867a8SHajimu UMEMOTO 		    (((u_char *)&sa4->sin_addr)[0] == 172 &&
10314c6867a8SHajimu UMEMOTO 		     (((u_char *)&sa4->sin_addr)[1] & 0xf0) == 16) ||
10324c6867a8SHajimu UMEMOTO 		    (((u_char *)&sa4->sin_addr)[0] == 192 &&
10334c6867a8SHajimu UMEMOTO 		     ((u_char *)&sa4->sin_addr)[1] == 168))
1034251b48a1SHajimu UMEMOTO 			return(14);	/* XXX: It should be 5 unless NAT */
10354c6867a8SHajimu UMEMOTO 		/* Loopback addresses have link-local scope. */
10364c6867a8SHajimu UMEMOTO 		if (((u_char *)&sa4->sin_addr)[0] == 127)
10374c6867a8SHajimu UMEMOTO 			return(2);
10384c6867a8SHajimu UMEMOTO 		return(14);
10394c6867a8SHajimu UMEMOTO 		break;
10404c6867a8SHajimu UMEMOTO 	default:
10414c6867a8SHajimu UMEMOTO 		errno = EAFNOSUPPORT; /* is this a good error? */
10424c6867a8SHajimu UMEMOTO 		return(-1);
10434c6867a8SHajimu UMEMOTO 	}
10444c6867a8SHajimu UMEMOTO }
10454c6867a8SHajimu UMEMOTO 
10467d56d374SYoshinobu Inoue /*
10477d56d374SYoshinobu Inoue  * hostname == NULL.
10487d56d374SYoshinobu Inoue  * passive socket -> anyaddr (0.0.0.0 or ::)
10497d56d374SYoshinobu Inoue  * non-passive socket -> localhost (127.0.0.1 or ::1)
10507d56d374SYoshinobu Inoue  */
10517d56d374SYoshinobu Inoue static int
105290d1d883SHajimu UMEMOTO explore_null(const struct addrinfo *pai, const char *servname,
105390d1d883SHajimu UMEMOTO     struct addrinfo **res)
10547d56d374SYoshinobu Inoue {
1055bf838688SHajimu UMEMOTO 	int s;
10567d56d374SYoshinobu Inoue 	const struct afd *afd;
10577d56d374SYoshinobu Inoue 	struct addrinfo *cur;
10587d56d374SYoshinobu Inoue 	struct addrinfo sentinel;
10597d56d374SYoshinobu Inoue 	int error;
10607d56d374SYoshinobu Inoue 
10617d56d374SYoshinobu Inoue 	*res = NULL;
10627d56d374SYoshinobu Inoue 	sentinel.ai_next = NULL;
10637d56d374SYoshinobu Inoue 	cur = &sentinel;
10647d56d374SYoshinobu Inoue 
10657d56d374SYoshinobu Inoue 	/*
1066bf838688SHajimu UMEMOTO 	 * filter out AFs that are not supported by the kernel
1067bf838688SHajimu UMEMOTO 	 * XXX errno?
1068bf838688SHajimu UMEMOTO 	 */
1069bf838688SHajimu UMEMOTO 	s = _socket(pai->ai_family, SOCK_DGRAM, 0);
1070bf838688SHajimu UMEMOTO 	if (s < 0) {
1071bf838688SHajimu UMEMOTO 		if (errno != EMFILE)
1072bf838688SHajimu UMEMOTO 			return 0;
1073bf838688SHajimu UMEMOTO 	} else
1074bf838688SHajimu UMEMOTO 		_close(s);
1075bf838688SHajimu UMEMOTO 
1076bf838688SHajimu UMEMOTO 	/*
1077a42af91cSHajimu UMEMOTO 	 * if the servname does not match socktype/protocol, ignore it.
1078a42af91cSHajimu UMEMOTO 	 */
1079a42af91cSHajimu UMEMOTO 	if (get_portmatch(pai, servname) != 0)
1080a42af91cSHajimu UMEMOTO 		return 0;
1081a42af91cSHajimu UMEMOTO 
10827d56d374SYoshinobu Inoue 	afd = find_afd(pai->ai_family);
10837d56d374SYoshinobu Inoue 	if (afd == NULL)
10847d56d374SYoshinobu Inoue 		return 0;
10857d56d374SYoshinobu Inoue 
1086a42af91cSHajimu UMEMOTO 	if (pai->ai_flags & AI_PASSIVE) {
1087a42af91cSHajimu UMEMOTO 		GET_AI(cur->ai_next, afd, afd->a_addrany);
10887d56d374SYoshinobu Inoue 		/* xxx meaningless?
10897d56d374SYoshinobu Inoue 		 * GET_CANONNAME(cur->ai_next, "anyaddr");
1090a42af91cSHajimu UMEMOTO 		 */
1091a42af91cSHajimu UMEMOTO 		GET_PORT(cur->ai_next, servname);
1092a42af91cSHajimu UMEMOTO 	} else {
1093a42af91cSHajimu UMEMOTO 		GET_AI(cur->ai_next, afd, afd->a_loopback);
1094a42af91cSHajimu UMEMOTO 		/* xxx meaningless?
10957d56d374SYoshinobu Inoue 		 * GET_CANONNAME(cur->ai_next, "localhost");
10967d56d374SYoshinobu Inoue 		 */
10977d56d374SYoshinobu Inoue 		GET_PORT(cur->ai_next, servname);
1098a42af91cSHajimu UMEMOTO 	}
1099a42af91cSHajimu UMEMOTO 	cur = cur->ai_next;
11007d56d374SYoshinobu Inoue 
11017d56d374SYoshinobu Inoue 	*res = sentinel.ai_next;
11027d56d374SYoshinobu Inoue 	return 0;
11037d56d374SYoshinobu Inoue 
11047d56d374SYoshinobu Inoue free:
11057d56d374SYoshinobu Inoue 	if (sentinel.ai_next)
11067d56d374SYoshinobu Inoue 		freeaddrinfo(sentinel.ai_next);
11077d56d374SYoshinobu Inoue 	return error;
11087d56d374SYoshinobu Inoue }
11097d56d374SYoshinobu Inoue 
11107d56d374SYoshinobu Inoue /*
11117d56d374SYoshinobu Inoue  * numeric hostname
11127d56d374SYoshinobu Inoue  */
11137d56d374SYoshinobu Inoue static int
111490d1d883SHajimu UMEMOTO explore_numeric(const struct addrinfo *pai, const char *hostname,
111590d1d883SHajimu UMEMOTO     const char *servname, struct addrinfo **res, const char *canonname)
11167d56d374SYoshinobu Inoue {
11177d56d374SYoshinobu Inoue 	const struct afd *afd;
11187d56d374SYoshinobu Inoue 	struct addrinfo *cur;
11197d56d374SYoshinobu Inoue 	struct addrinfo sentinel;
11207d56d374SYoshinobu Inoue 	int error;
11217d56d374SYoshinobu Inoue 	char pton[PTON_MAX];
11227d56d374SYoshinobu Inoue 
11237d56d374SYoshinobu Inoue 	*res = NULL;
11247d56d374SYoshinobu Inoue 	sentinel.ai_next = NULL;
11257d56d374SYoshinobu Inoue 	cur = &sentinel;
11267d56d374SYoshinobu Inoue 
1127bf838688SHajimu UMEMOTO 	/*
1128bf838688SHajimu UMEMOTO 	 * if the servname does not match socktype/protocol, ignore it.
1129bf838688SHajimu UMEMOTO 	 */
1130bf838688SHajimu UMEMOTO 	if (get_portmatch(pai, servname) != 0)
1131bf838688SHajimu UMEMOTO 		return 0;
1132bf838688SHajimu UMEMOTO 
11337d56d374SYoshinobu Inoue 	afd = find_afd(pai->ai_family);
11347d56d374SYoshinobu Inoue 	if (afd == NULL)
11357d56d374SYoshinobu Inoue 		return 0;
11367d56d374SYoshinobu Inoue 
1137b826397aSJun-ichiro itojun Hagino 	switch (afd->a_af) {
1138b826397aSJun-ichiro itojun Hagino #if 1 /*X/Open spec*/
1139b826397aSJun-ichiro itojun Hagino 	case AF_INET:
1140b826397aSJun-ichiro itojun Hagino 		if (inet_aton(hostname, (struct in_addr *)pton) == 1) {
11417d56d374SYoshinobu Inoue 			if (pai->ai_family == afd->a_af ||
11427d56d374SYoshinobu Inoue 			    pai->ai_family == PF_UNSPEC /*?*/) {
11437d56d374SYoshinobu Inoue 				GET_AI(cur->ai_next, afd, pton);
11447d56d374SYoshinobu Inoue 				GET_PORT(cur->ai_next, servname);
11458755b918SHajimu UMEMOTO 				if ((pai->ai_flags & AI_CANONNAME)) {
11468755b918SHajimu UMEMOTO 					/*
11478755b918SHajimu UMEMOTO 					 * Set the numeric address itself as
11488755b918SHajimu UMEMOTO 					 * the canonical name, based on a
11498755b918SHajimu UMEMOTO 					 * clarification in rfc3493.
11508755b918SHajimu UMEMOTO 					 */
11518755b918SHajimu UMEMOTO 					GET_CANONNAME(cur->ai_next, canonname);
11528755b918SHajimu UMEMOTO 				}
11537d56d374SYoshinobu Inoue 				while (cur && cur->ai_next)
11547d56d374SYoshinobu Inoue 					cur = cur->ai_next;
11557d56d374SYoshinobu Inoue 			} else
11567d56d374SYoshinobu Inoue 				ERR(EAI_FAMILY);	/*xxx*/
11577d56d374SYoshinobu Inoue 		}
1158b826397aSJun-ichiro itojun Hagino 		break;
1159b826397aSJun-ichiro itojun Hagino #endif
1160b826397aSJun-ichiro itojun Hagino 	default:
1161b826397aSJun-ichiro itojun Hagino 		if (inet_pton(afd->a_af, hostname, pton) == 1) {
1162b826397aSJun-ichiro itojun Hagino 			if (pai->ai_family == afd->a_af ||
1163b826397aSJun-ichiro itojun Hagino 			    pai->ai_family == PF_UNSPEC /*?*/) {
1164b826397aSJun-ichiro itojun Hagino 				GET_AI(cur->ai_next, afd, pton);
1165b826397aSJun-ichiro itojun Hagino 				GET_PORT(cur->ai_next, servname);
11668755b918SHajimu UMEMOTO 				if ((pai->ai_flags & AI_CANONNAME)) {
11678755b918SHajimu UMEMOTO 					/*
11688755b918SHajimu UMEMOTO 					 * Set the numeric address itself as
11698755b918SHajimu UMEMOTO 					 * the canonical name, based on a
11708755b918SHajimu UMEMOTO 					 * clarification in rfc3493.
11718755b918SHajimu UMEMOTO 					 */
11728755b918SHajimu UMEMOTO 					GET_CANONNAME(cur->ai_next, canonname);
11738755b918SHajimu UMEMOTO 				}
1174b826397aSJun-ichiro itojun Hagino 				while (cur && cur->ai_next)
1175b826397aSJun-ichiro itojun Hagino 					cur = cur->ai_next;
1176b826397aSJun-ichiro itojun Hagino 			} else
1177dd521ef1SHajimu UMEMOTO 				ERR(EAI_FAMILY);	/* XXX */
1178b826397aSJun-ichiro itojun Hagino 		}
1179b826397aSJun-ichiro itojun Hagino 		break;
1180b826397aSJun-ichiro itojun Hagino 	}
11817d56d374SYoshinobu Inoue 
11827d56d374SYoshinobu Inoue 	*res = sentinel.ai_next;
11837d56d374SYoshinobu Inoue 	return 0;
11847d56d374SYoshinobu Inoue 
11857d56d374SYoshinobu Inoue free:
11867d56d374SYoshinobu Inoue bad:
11877d56d374SYoshinobu Inoue 	if (sentinel.ai_next)
11887d56d374SYoshinobu Inoue 		freeaddrinfo(sentinel.ai_next);
11897d56d374SYoshinobu Inoue 	return error;
11907d56d374SYoshinobu Inoue }
11917d56d374SYoshinobu Inoue 
11927d56d374SYoshinobu Inoue /*
11937d56d374SYoshinobu Inoue  * numeric hostname with scope
11947d56d374SYoshinobu Inoue  */
11957d56d374SYoshinobu Inoue static int
119690d1d883SHajimu UMEMOTO explore_numeric_scope(const struct addrinfo *pai, const char *hostname,
119790d1d883SHajimu UMEMOTO     const char *servname, struct addrinfo **res)
11987d56d374SYoshinobu Inoue {
1199a42af91cSHajimu UMEMOTO #if !defined(SCOPE_DELIMITER) || !defined(INET6)
12008755b918SHajimu UMEMOTO 	return explore_numeric(pai, hostname, servname, res, hostname);
12017d56d374SYoshinobu Inoue #else
12027d56d374SYoshinobu Inoue 	const struct afd *afd;
12037d56d374SYoshinobu Inoue 	struct addrinfo *cur;
12047d56d374SYoshinobu Inoue 	int error;
1205a42af91cSHajimu UMEMOTO 	char *cp, *hostname2 = NULL, *scope, *addr;
12067d56d374SYoshinobu Inoue 	struct sockaddr_in6 *sin6;
12077d56d374SYoshinobu Inoue 
1208bf838688SHajimu UMEMOTO 	/*
1209bf838688SHajimu UMEMOTO 	 * if the servname does not match socktype/protocol, ignore it.
1210bf838688SHajimu UMEMOTO 	 */
1211bf838688SHajimu UMEMOTO 	if (get_portmatch(pai, servname) != 0)
1212bf838688SHajimu UMEMOTO 		return 0;
1213bf838688SHajimu UMEMOTO 
12147d56d374SYoshinobu Inoue 	afd = find_afd(pai->ai_family);
12157d56d374SYoshinobu Inoue 	if (afd == NULL)
12167d56d374SYoshinobu Inoue 		return 0;
1217248aee62SJacques Vidrine 
12187d56d374SYoshinobu Inoue 	if (!afd->a_scoped)
12198755b918SHajimu UMEMOTO 		return explore_numeric(pai, hostname, servname, res, hostname);
12207d56d374SYoshinobu Inoue 
12217d56d374SYoshinobu Inoue 	cp = strchr(hostname, SCOPE_DELIMITER);
12227d56d374SYoshinobu Inoue 	if (cp == NULL)
12238755b918SHajimu UMEMOTO 		return explore_numeric(pai, hostname, servname, res, hostname);
12247d56d374SYoshinobu Inoue 
12257d56d374SYoshinobu Inoue 	/*
12267d56d374SYoshinobu Inoue 	 * Handle special case of <scoped_address><delimiter><scope id>
12277d56d374SYoshinobu Inoue 	 */
12287d56d374SYoshinobu Inoue 	hostname2 = strdup(hostname);
12297d56d374SYoshinobu Inoue 	if (hostname2 == NULL)
12307d56d374SYoshinobu Inoue 		return EAI_MEMORY;
12317d56d374SYoshinobu Inoue 	/* terminate at the delimiter */
12327d56d374SYoshinobu Inoue 	hostname2[cp - hostname] = '\0';
1233a42af91cSHajimu UMEMOTO 	addr = hostname2;
1234a42af91cSHajimu UMEMOTO 	scope = cp + 1;
12357d56d374SYoshinobu Inoue 
12368755b918SHajimu UMEMOTO 	error = explore_numeric(pai, addr, servname, res, hostname);
12377d56d374SYoshinobu Inoue 	if (error == 0) {
1238a89c1d30SHajimu UMEMOTO 		u_int32_t scopeid;
1239a42af91cSHajimu UMEMOTO 
12407d56d374SYoshinobu Inoue 		for (cur = *res; cur; cur = cur->ai_next) {
12417d56d374SYoshinobu Inoue 			if (cur->ai_family != AF_INET6)
12427d56d374SYoshinobu Inoue 				continue;
1243a42af91cSHajimu UMEMOTO 			sin6 = (struct sockaddr_in6 *)(void *)cur->ai_addr;
1244a89c1d30SHajimu UMEMOTO 			if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
1245a42af91cSHajimu UMEMOTO 				free(hostname2);
124637b3e941SHajimu UMEMOTO 				return(EAI_NONAME); /* XXX: is return OK? */
1247a42af91cSHajimu UMEMOTO 			}
1248a42af91cSHajimu UMEMOTO 			sin6->sin6_scope_id = scopeid;
12497d56d374SYoshinobu Inoue 		}
12507d56d374SYoshinobu Inoue 	}
12517d56d374SYoshinobu Inoue 
12527d56d374SYoshinobu Inoue 	free(hostname2);
12537d56d374SYoshinobu Inoue 
12547d56d374SYoshinobu Inoue 	return error;
12557d56d374SYoshinobu Inoue #endif
12567d56d374SYoshinobu Inoue }
12577d56d374SYoshinobu Inoue 
12587d56d374SYoshinobu Inoue static int
125990d1d883SHajimu UMEMOTO get_canonname(const struct addrinfo *pai, struct addrinfo *ai, const char *str)
12607d56d374SYoshinobu Inoue {
12617d56d374SYoshinobu Inoue 	if ((pai->ai_flags & AI_CANONNAME) != 0) {
12628743b270SHajimu UMEMOTO 		ai->ai_canonname = strdup(str);
12637d56d374SYoshinobu Inoue 		if (ai->ai_canonname == NULL)
12647d56d374SYoshinobu Inoue 			return EAI_MEMORY;
12657d56d374SYoshinobu Inoue 	}
12667d56d374SYoshinobu Inoue 	return 0;
12677d56d374SYoshinobu Inoue }
12687d56d374SYoshinobu Inoue 
12697d56d374SYoshinobu Inoue static struct addrinfo *
127090d1d883SHajimu UMEMOTO get_ai(const struct addrinfo *pai, const struct afd *afd, const char *addr)
12717d56d374SYoshinobu Inoue {
12727d56d374SYoshinobu Inoue 	char *p;
12737d56d374SYoshinobu Inoue 	struct addrinfo *ai;
12747d56d374SYoshinobu Inoue #ifdef FAITH
12757d56d374SYoshinobu Inoue 	struct in6_addr faith_prefix;
12767d56d374SYoshinobu Inoue 	char *fp_str;
12777d56d374SYoshinobu Inoue 	int translate = 0;
12787d56d374SYoshinobu Inoue #endif
12797d56d374SYoshinobu Inoue 
12807d56d374SYoshinobu Inoue #ifdef FAITH
12817d56d374SYoshinobu Inoue 	/*
12827d56d374SYoshinobu Inoue 	 * Transfrom an IPv4 addr into a special IPv6 addr format for
12837d56d374SYoshinobu Inoue 	 * IPv6->IPv4 translation gateway. (only TCP is supported now)
12847d56d374SYoshinobu Inoue 	 *
12857d56d374SYoshinobu Inoue 	 * +-----------------------------------+------------+
12867d56d374SYoshinobu Inoue 	 * | faith prefix part (12 bytes)      | embedded   |
12877d56d374SYoshinobu Inoue 	 * |                                   | IPv4 addr part (4 bytes)
12887d56d374SYoshinobu Inoue 	 * +-----------------------------------+------------+
12897d56d374SYoshinobu Inoue 	 *
12907d56d374SYoshinobu Inoue 	 * faith prefix part is specified as ascii IPv6 addr format
12917d56d374SYoshinobu Inoue 	 * in environmental variable GAI.
12927d56d374SYoshinobu Inoue 	 * For FAITH to work correctly, routing to faith prefix must be
12937d56d374SYoshinobu Inoue 	 * setup toward a machine where a FAITH daemon operates.
12947d56d374SYoshinobu Inoue 	 * Also, the machine must enable some mechanizm
12957d56d374SYoshinobu Inoue 	 * (e.g. faith interface hack) to divert those packet with
12967d56d374SYoshinobu Inoue 	 * faith prefixed destination addr to user-land FAITH daemon.
12977d56d374SYoshinobu Inoue 	 */
12987d56d374SYoshinobu Inoue 	fp_str = getenv("GAI");
12997d56d374SYoshinobu Inoue 	if (fp_str && inet_pton(AF_INET6, fp_str, &faith_prefix) == 1 &&
13007d56d374SYoshinobu Inoue 	    afd->a_af == AF_INET && pai->ai_socktype == SOCK_STREAM) {
13017d56d374SYoshinobu Inoue 		u_int32_t v4a;
13027d56d374SYoshinobu Inoue 		u_int8_t v4a_top;
13037d56d374SYoshinobu Inoue 
13047d56d374SYoshinobu Inoue 		memcpy(&v4a, addr, sizeof v4a);
13057d56d374SYoshinobu Inoue 		v4a_top = v4a >> IN_CLASSA_NSHIFT;
13067d56d374SYoshinobu Inoue 		if (!IN_MULTICAST(v4a) && !IN_EXPERIMENTAL(v4a) &&
13077d56d374SYoshinobu Inoue 		    v4a_top != 0 && v4a != IN_LOOPBACKNET) {
13087d56d374SYoshinobu Inoue 			afd = &afdl[N_INET6];
13097d56d374SYoshinobu Inoue 			memcpy(&faith_prefix.s6_addr[12], addr,
13107d56d374SYoshinobu Inoue 			       sizeof(struct in_addr));
13117d56d374SYoshinobu Inoue 			translate = 1;
13127d56d374SYoshinobu Inoue 		}
13137d56d374SYoshinobu Inoue 	}
13147d56d374SYoshinobu Inoue #endif
13157d56d374SYoshinobu Inoue 
13167d56d374SYoshinobu Inoue 	ai = (struct addrinfo *)malloc(sizeof(struct addrinfo)
13177d56d374SYoshinobu Inoue 		+ (afd->a_socklen));
13187d56d374SYoshinobu Inoue 	if (ai == NULL)
13197d56d374SYoshinobu Inoue 		return NULL;
13207d56d374SYoshinobu Inoue 
13217d56d374SYoshinobu Inoue 	memcpy(ai, pai, sizeof(struct addrinfo));
1322a42af91cSHajimu UMEMOTO 	ai->ai_addr = (struct sockaddr *)(void *)(ai + 1);
1323a42af91cSHajimu UMEMOTO 	memset(ai->ai_addr, 0, (size_t)afd->a_socklen);
13247d56d374SYoshinobu Inoue 	ai->ai_addr->sa_len = afd->a_socklen;
13257d56d374SYoshinobu Inoue 	ai->ai_addrlen = afd->a_socklen;
13267d56d374SYoshinobu Inoue 	ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
1327248aee62SJacques Vidrine 	p = (char *)(void *)(ai->ai_addr);
13287d56d374SYoshinobu Inoue #ifdef FAITH
13297d56d374SYoshinobu Inoue 	if (translate == 1)
1330248aee62SJacques Vidrine 		memcpy(p + afd->a_off, &faith_prefix, (size_t)afd->a_addrlen);
13317d56d374SYoshinobu Inoue 	else
13327d56d374SYoshinobu Inoue #endif
1333248aee62SJacques Vidrine 	memcpy(p + afd->a_off, addr, (size_t)afd->a_addrlen);
13347d56d374SYoshinobu Inoue 	return ai;
13357d56d374SYoshinobu Inoue }
13367d56d374SYoshinobu Inoue 
13377d56d374SYoshinobu Inoue static int
133890d1d883SHajimu UMEMOTO get_portmatch(const struct addrinfo *ai, const char *servname)
13397d56d374SYoshinobu Inoue {
1340a42af91cSHajimu UMEMOTO 
1341c224435eSHajimu UMEMOTO 	/* get_port does not touch first argument when matchonly == 1. */
1342a42af91cSHajimu UMEMOTO 	/* LINTED const cast */
13437d56d374SYoshinobu Inoue 	return get_port((struct addrinfo *)ai, servname, 1);
13447d56d374SYoshinobu Inoue }
13457d56d374SYoshinobu Inoue 
13467d56d374SYoshinobu Inoue static int
134790d1d883SHajimu UMEMOTO get_port(struct addrinfo *ai, const char *servname, int matchonly)
13487d56d374SYoshinobu Inoue {
13497d56d374SYoshinobu Inoue 	const char *proto;
13507d56d374SYoshinobu Inoue 	struct servent *sp;
13517d56d374SYoshinobu Inoue 	int port;
13527d56d374SYoshinobu Inoue 	int allownumeric;
13537d56d374SYoshinobu Inoue 
13547d56d374SYoshinobu Inoue 	if (servname == NULL)
13557d56d374SYoshinobu Inoue 		return 0;
1356a42af91cSHajimu UMEMOTO 	switch (ai->ai_family) {
1357a42af91cSHajimu UMEMOTO 	case AF_INET:
1358a42af91cSHajimu UMEMOTO #ifdef AF_INET6
1359a42af91cSHajimu UMEMOTO 	case AF_INET6:
13607d56d374SYoshinobu Inoue #endif
1361a42af91cSHajimu UMEMOTO 		break;
1362a42af91cSHajimu UMEMOTO 	default:
13637d56d374SYoshinobu Inoue 		return 0;
1364a42af91cSHajimu UMEMOTO 	}
13657d56d374SYoshinobu Inoue 
13667d56d374SYoshinobu Inoue 	switch (ai->ai_socktype) {
13677d56d374SYoshinobu Inoue 	case SOCK_RAW:
13687d56d374SYoshinobu Inoue 		return EAI_SERVICE;
13697d56d374SYoshinobu Inoue 	case SOCK_DGRAM:
13707d56d374SYoshinobu Inoue 	case SOCK_STREAM:
13717d56d374SYoshinobu Inoue 		allownumeric = 1;
13727d56d374SYoshinobu Inoue 		break;
13737d56d374SYoshinobu Inoue 	case ANY:
13747d56d374SYoshinobu Inoue 		allownumeric = 0;
13757d56d374SYoshinobu Inoue 		break;
13767d56d374SYoshinobu Inoue 	default:
13777d56d374SYoshinobu Inoue 		return EAI_SOCKTYPE;
13787d56d374SYoshinobu Inoue 	}
13797d56d374SYoshinobu Inoue 
1380c224435eSHajimu UMEMOTO 	port = str2number(servname);
1381c224435eSHajimu UMEMOTO 	if (port >= 0) {
13827d56d374SYoshinobu Inoue 		if (!allownumeric)
13837d56d374SYoshinobu Inoue 			return EAI_SERVICE;
13847d56d374SYoshinobu Inoue 		if (port < 0 || port > 65535)
13857d56d374SYoshinobu Inoue 			return EAI_SERVICE;
1386dd521ef1SHajimu UMEMOTO 		port = htons(port);
13877d56d374SYoshinobu Inoue 	} else {
1388c224435eSHajimu UMEMOTO 		if (ai->ai_flags & AI_NUMERICSERV)
1389c224435eSHajimu UMEMOTO 			return EAI_NONAME;
1390bf838688SHajimu UMEMOTO 		switch (ai->ai_socktype) {
1391bf838688SHajimu UMEMOTO 		case SOCK_DGRAM:
13927d56d374SYoshinobu Inoue 			proto = "udp";
13937d56d374SYoshinobu Inoue 			break;
1394bf838688SHajimu UMEMOTO 		case SOCK_STREAM:
13957d56d374SYoshinobu Inoue 			proto = "tcp";
13967d56d374SYoshinobu Inoue 			break;
13977d56d374SYoshinobu Inoue 		default:
13987d56d374SYoshinobu Inoue 			proto = NULL;
13997d56d374SYoshinobu Inoue 			break;
14007d56d374SYoshinobu Inoue 		}
14017d56d374SYoshinobu Inoue 
140262e8b17dSHajimu UMEMOTO 		if ((sp = getservbyname(servname, proto)) == NULL)
14037d56d374SYoshinobu Inoue 			return EAI_SERVICE;
14047d56d374SYoshinobu Inoue 		port = sp->s_port;
14057d56d374SYoshinobu Inoue 	}
14067d56d374SYoshinobu Inoue 
14077d56d374SYoshinobu Inoue 	if (!matchonly) {
14087d56d374SYoshinobu Inoue 		switch (ai->ai_family) {
14097d56d374SYoshinobu Inoue 		case AF_INET:
1410a42af91cSHajimu UMEMOTO 			((struct sockaddr_in *)(void *)
1411a42af91cSHajimu UMEMOTO 			    ai->ai_addr)->sin_port = port;
14127d56d374SYoshinobu Inoue 			break;
14137d56d374SYoshinobu Inoue #ifdef INET6
14147d56d374SYoshinobu Inoue 		case AF_INET6:
1415a42af91cSHajimu UMEMOTO 			((struct sockaddr_in6 *)(void *)
1416a42af91cSHajimu UMEMOTO 			    ai->ai_addr)->sin6_port = port;
14177d56d374SYoshinobu Inoue 			break;
14187d56d374SYoshinobu Inoue #endif
14197d56d374SYoshinobu Inoue 		}
14207d56d374SYoshinobu Inoue 	}
14217d56d374SYoshinobu Inoue 
14227d56d374SYoshinobu Inoue 	return 0;
14237d56d374SYoshinobu Inoue }
14247d56d374SYoshinobu Inoue 
14257d56d374SYoshinobu Inoue static const struct afd *
142690d1d883SHajimu UMEMOTO find_afd(int af)
14277d56d374SYoshinobu Inoue {
14287d56d374SYoshinobu Inoue 	const struct afd *afd;
14297d56d374SYoshinobu Inoue 
14307d56d374SYoshinobu Inoue 	if (af == PF_UNSPEC)
14317d56d374SYoshinobu Inoue 		return NULL;
14327d56d374SYoshinobu Inoue 	for (afd = afdl; afd->a_af; afd++) {
14337d56d374SYoshinobu Inoue 		if (afd->a_af == af)
14347d56d374SYoshinobu Inoue 			return afd;
14357d56d374SYoshinobu Inoue 	}
14367d56d374SYoshinobu Inoue 	return NULL;
14377d56d374SYoshinobu Inoue }
1438a42af91cSHajimu UMEMOTO 
1439a42af91cSHajimu UMEMOTO /*
1440a42af91cSHajimu UMEMOTO  * post-2553: AI_ADDRCONFIG check.  if we use getipnodeby* as backend, backend
1441a42af91cSHajimu UMEMOTO  * will take care of it.
1442a42af91cSHajimu UMEMOTO  * the semantics of AI_ADDRCONFIG is not defined well.  we are not sure
1443a42af91cSHajimu UMEMOTO  * if the code is right or not.
1444bf838688SHajimu UMEMOTO  *
1445bf838688SHajimu UMEMOTO  * XXX PF_UNSPEC -> PF_INET6 + PF_INET mapping needs to be in sync with
1446bf838688SHajimu UMEMOTO  * _dns_getaddrinfo.
1447a42af91cSHajimu UMEMOTO  */
1448a42af91cSHajimu UMEMOTO static int
144990d1d883SHajimu UMEMOTO addrconfig(struct addrinfo *pai)
1450a42af91cSHajimu UMEMOTO {
1451bf838688SHajimu UMEMOTO 	int s, af;
1452a42af91cSHajimu UMEMOTO 
1453bf838688SHajimu UMEMOTO 	/*
1454bf838688SHajimu UMEMOTO 	 * TODO:
1455bf838688SHajimu UMEMOTO 	 * Note that implementation dependent test for address
1456bf838688SHajimu UMEMOTO 	 * configuration should be done everytime called
1457bf838688SHajimu UMEMOTO 	 * (or apropriate interval),
1458bf838688SHajimu UMEMOTO 	 * because addresses will be dynamically assigned or deleted.
1459bf838688SHajimu UMEMOTO 	 */
1460bf838688SHajimu UMEMOTO 	af = pai->ai_family;
1461bf838688SHajimu UMEMOTO 	if (af == AF_UNSPEC) {
1462bf838688SHajimu UMEMOTO 		if ((s = _socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1463bf838688SHajimu UMEMOTO 			af = AF_INET;
1464bf838688SHajimu UMEMOTO 		else {
1465bf838688SHajimu UMEMOTO 			_close(s);
1466bf838688SHajimu UMEMOTO 			if ((s = _socket(AF_INET, SOCK_DGRAM, 0)) < 0)
1467bf838688SHajimu UMEMOTO 				af = AF_INET6;
1468bf838688SHajimu UMEMOTO 			else
1469bf838688SHajimu UMEMOTO 				_close(s);
1470bf838688SHajimu UMEMOTO 		}
1471bf838688SHajimu UMEMOTO 	}
1472bf838688SHajimu UMEMOTO 	if (af != AF_UNSPEC) {
1473bf838688SHajimu UMEMOTO 		if ((s = _socket(af, SOCK_DGRAM, 0)) < 0)
1474a42af91cSHajimu UMEMOTO 			return 0;
1475bf838688SHajimu UMEMOTO 		_close(s);
1476bf838688SHajimu UMEMOTO 	}
1477bf838688SHajimu UMEMOTO 	pai->ai_family = af;
1478a42af91cSHajimu UMEMOTO 	return 1;
1479a42af91cSHajimu UMEMOTO }
1480a42af91cSHajimu UMEMOTO 
1481a42af91cSHajimu UMEMOTO #ifdef INET6
1482a42af91cSHajimu UMEMOTO /* convert a string to a scope identifier. XXX: IPv6 specific */
1483a42af91cSHajimu UMEMOTO static int
148490d1d883SHajimu UMEMOTO ip6_str2scopeid(char *scope, struct sockaddr_in6 *sin6, u_int32_t *scopeid)
1485a42af91cSHajimu UMEMOTO {
1486a89c1d30SHajimu UMEMOTO 	u_long lscopeid;
1487bf838688SHajimu UMEMOTO 	struct in6_addr *a6;
1488a42af91cSHajimu UMEMOTO 	char *ep;
1489a42af91cSHajimu UMEMOTO 
1490bf838688SHajimu UMEMOTO 	a6 = &sin6->sin6_addr;
1491bf838688SHajimu UMEMOTO 
1492ec20fe00SJun-ichiro itojun Hagino 	/* empty scopeid portion is invalid */
1493ec20fe00SJun-ichiro itojun Hagino 	if (*scope == '\0')
1494ec20fe00SJun-ichiro itojun Hagino 		return -1;
1495ec20fe00SJun-ichiro itojun Hagino 
1496bf838688SHajimu UMEMOTO 	if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
1497a42af91cSHajimu UMEMOTO 		/*
1498a42af91cSHajimu UMEMOTO 		 * We currently assume a one-to-one mapping between links
1499a42af91cSHajimu UMEMOTO 		 * and interfaces, so we simply use interface indices for
1500a42af91cSHajimu UMEMOTO 		 * like-local scopes.
1501a42af91cSHajimu UMEMOTO 		 */
1502a89c1d30SHajimu UMEMOTO 		*scopeid = if_nametoindex(scope);
1503a89c1d30SHajimu UMEMOTO 		if (*scopeid == 0)
1504a42af91cSHajimu UMEMOTO 			goto trynumeric;
1505a89c1d30SHajimu UMEMOTO 		return 0;
1506a42af91cSHajimu UMEMOTO 	}
1507a42af91cSHajimu UMEMOTO 
1508a42af91cSHajimu UMEMOTO 	/* still unclear about literal, allow numeric only - placeholder */
1509a42af91cSHajimu UMEMOTO 	if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6))
1510a42af91cSHajimu UMEMOTO 		goto trynumeric;
1511a42af91cSHajimu UMEMOTO 	if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
1512a42af91cSHajimu UMEMOTO 		goto trynumeric;
1513a42af91cSHajimu UMEMOTO 	else
1514a42af91cSHajimu UMEMOTO 		goto trynumeric;	/* global */
1515a42af91cSHajimu UMEMOTO 
1516a42af91cSHajimu UMEMOTO 	/* try to convert to a numeric id as a last resort */
1517a42af91cSHajimu UMEMOTO   trynumeric:
1518a89c1d30SHajimu UMEMOTO 	errno = 0;
1519a89c1d30SHajimu UMEMOTO 	lscopeid = strtoul(scope, &ep, 10);
1520a89c1d30SHajimu UMEMOTO 	*scopeid = (u_int32_t)(lscopeid & 0xffffffffUL);
1521a89c1d30SHajimu UMEMOTO 	if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
1522a89c1d30SHajimu UMEMOTO 		return 0;
1523a42af91cSHajimu UMEMOTO 	else
1524a42af91cSHajimu UMEMOTO 		return -1;
1525a42af91cSHajimu UMEMOTO }
1526a42af91cSHajimu UMEMOTO #endif
1527a42af91cSHajimu UMEMOTO 
1528688a5c3eSHajimu UMEMOTO /*
1529688a5c3eSHajimu UMEMOTO  * FQDN hostname, DNS lookup
1530688a5c3eSHajimu UMEMOTO  */
1531688a5c3eSHajimu UMEMOTO static int
153290d1d883SHajimu UMEMOTO explore_fqdn(const struct addrinfo *pai, const char *hostname,
153390d1d883SHajimu UMEMOTO     const char *servname, struct addrinfo **res)
1534688a5c3eSHajimu UMEMOTO {
1535688a5c3eSHajimu UMEMOTO 	struct addrinfo *result;
1536688a5c3eSHajimu UMEMOTO 	struct addrinfo *cur;
1537688a5c3eSHajimu UMEMOTO 	int error = 0;
1538688a5c3eSHajimu UMEMOTO 	static const ns_dtab dtab[] = {
1539688a5c3eSHajimu UMEMOTO 		NS_FILES_CB(_files_getaddrinfo, NULL)
1540688a5c3eSHajimu UMEMOTO 		{ NSSRC_DNS, _dns_getaddrinfo, NULL },	/* force -DHESIOD */
1541688a5c3eSHajimu UMEMOTO 		NS_NIS_CB(_yp_getaddrinfo, NULL)
1542688a5c3eSHajimu UMEMOTO 		{ 0 }
154369b0a4b6SJim Pirzyk 	};
154469b0a4b6SJim Pirzyk 
1545688a5c3eSHajimu UMEMOTO 	result = NULL;
154669b0a4b6SJim Pirzyk 
1547688a5c3eSHajimu UMEMOTO 	/*
1548688a5c3eSHajimu UMEMOTO 	 * if the servname does not match socktype/protocol, ignore it.
1549688a5c3eSHajimu UMEMOTO 	 */
155033dee819SBrian Feldman 	if (get_portmatch(pai, servname) != 0)
155169b0a4b6SJim Pirzyk 		return 0;
155269b0a4b6SJim Pirzyk 
1553688a5c3eSHajimu UMEMOTO 	switch (_nsdispatch(&result, dtab, NSDB_HOSTS, "getaddrinfo",
1554688a5c3eSHajimu UMEMOTO 			default_dns_files, hostname, pai)) {
1555688a5c3eSHajimu UMEMOTO 	case NS_TRYAGAIN:
1556688a5c3eSHajimu UMEMOTO 		error = EAI_AGAIN;
1557688a5c3eSHajimu UMEMOTO 		goto free;
1558688a5c3eSHajimu UMEMOTO 	case NS_UNAVAIL:
1559688a5c3eSHajimu UMEMOTO 		error = EAI_FAIL;
1560688a5c3eSHajimu UMEMOTO 		goto free;
1561688a5c3eSHajimu UMEMOTO 	case NS_NOTFOUND:
1562688a5c3eSHajimu UMEMOTO 		error = EAI_NONAME;
1563688a5c3eSHajimu UMEMOTO 		goto free;
1564688a5c3eSHajimu UMEMOTO 	case NS_SUCCESS:
1565688a5c3eSHajimu UMEMOTO 		error = 0;
1566688a5c3eSHajimu UMEMOTO 		for (cur = result; cur; cur = cur->ai_next) {
1567688a5c3eSHajimu UMEMOTO 			GET_PORT(cur, servname);
1568688a5c3eSHajimu UMEMOTO 			/* canonname should be filled already */
1569688a5c3eSHajimu UMEMOTO 		}
157069b0a4b6SJim Pirzyk 		break;
157169b0a4b6SJim Pirzyk 	}
157269b0a4b6SJim Pirzyk 
1573688a5c3eSHajimu UMEMOTO 	*res = result;
1574688a5c3eSHajimu UMEMOTO 
157569b0a4b6SJim Pirzyk 	return 0;
1576688a5c3eSHajimu UMEMOTO 
1577688a5c3eSHajimu UMEMOTO free:
1578688a5c3eSHajimu UMEMOTO 	if (result)
1579688a5c3eSHajimu UMEMOTO 		freeaddrinfo(result);
1580688a5c3eSHajimu UMEMOTO 	return error;
158169b0a4b6SJim Pirzyk }
158269b0a4b6SJim Pirzyk 
1583a42af91cSHajimu UMEMOTO #ifdef DEBUG
1584a42af91cSHajimu UMEMOTO static const char AskedForGot[] =
1585a42af91cSHajimu UMEMOTO 	"gethostby*.getanswer: asked for \"%s\", got \"%s\"";
1586a42af91cSHajimu UMEMOTO #endif
1587a42af91cSHajimu UMEMOTO 
1588a42af91cSHajimu UMEMOTO static struct addrinfo *
158990d1d883SHajimu UMEMOTO getanswer(const querybuf *answer, int anslen, const char *qname, int qtype,
159090d1d883SHajimu UMEMOTO     const struct addrinfo *pai, res_state res)
1591a42af91cSHajimu UMEMOTO {
1592a42af91cSHajimu UMEMOTO 	struct addrinfo sentinel, *cur;
1593a42af91cSHajimu UMEMOTO 	struct addrinfo ai;
1594a42af91cSHajimu UMEMOTO 	const struct afd *afd;
1595a42af91cSHajimu UMEMOTO 	char *canonname;
1596a42af91cSHajimu UMEMOTO 	const HEADER *hp;
1597a42af91cSHajimu UMEMOTO 	const u_char *cp;
1598a42af91cSHajimu UMEMOTO 	int n;
1599a42af91cSHajimu UMEMOTO 	const u_char *eom;
1600dd521ef1SHajimu UMEMOTO 	char *bp, *ep;
1601dd521ef1SHajimu UMEMOTO 	int type, class, ancount, qdcount;
1602a42af91cSHajimu UMEMOTO 	int haveanswer, had_error;
1603a42af91cSHajimu UMEMOTO 	char tbuf[MAXDNAME];
1604c05ac53bSDavid E. O'Brien 	int (*name_ok)(const char *);
1605a42af91cSHajimu UMEMOTO 	char hostbuf[8*1024];
1606a42af91cSHajimu UMEMOTO 
1607a42af91cSHajimu UMEMOTO 	memset(&sentinel, 0, sizeof(sentinel));
1608a42af91cSHajimu UMEMOTO 	cur = &sentinel;
1609a42af91cSHajimu UMEMOTO 
1610a42af91cSHajimu UMEMOTO 	canonname = NULL;
1611a42af91cSHajimu UMEMOTO 	eom = answer->buf + anslen;
1612a42af91cSHajimu UMEMOTO 	switch (qtype) {
1613a42af91cSHajimu UMEMOTO 	case T_A:
1614a42af91cSHajimu UMEMOTO 	case T_AAAA:
1615a42af91cSHajimu UMEMOTO 	case T_ANY:	/*use T_ANY only for T_A/T_AAAA lookup*/
1616a42af91cSHajimu UMEMOTO 		name_ok = res_hnok;
1617a42af91cSHajimu UMEMOTO 		break;
1618a42af91cSHajimu UMEMOTO 	default:
1619a42af91cSHajimu UMEMOTO 		return (NULL);	/* XXX should be abort(); */
1620a42af91cSHajimu UMEMOTO 	}
1621a42af91cSHajimu UMEMOTO 	/*
1622a42af91cSHajimu UMEMOTO 	 * find first satisfactory answer
1623a42af91cSHajimu UMEMOTO 	 */
1624a42af91cSHajimu UMEMOTO 	hp = &answer->hdr;
1625a42af91cSHajimu UMEMOTO 	ancount = ntohs(hp->ancount);
1626a42af91cSHajimu UMEMOTO 	qdcount = ntohs(hp->qdcount);
1627a42af91cSHajimu UMEMOTO 	bp = hostbuf;
1628dd521ef1SHajimu UMEMOTO 	ep = hostbuf + sizeof hostbuf;
1629a42af91cSHajimu UMEMOTO 	cp = answer->buf + HFIXEDSZ;
1630a42af91cSHajimu UMEMOTO 	if (qdcount != 1) {
16315342d17fSHajimu UMEMOTO 		RES_SET_H_ERRNO(res, NO_RECOVERY);
1632a42af91cSHajimu UMEMOTO 		return (NULL);
1633a42af91cSHajimu UMEMOTO 	}
1634dd521ef1SHajimu UMEMOTO 	n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1635a42af91cSHajimu UMEMOTO 	if ((n < 0) || !(*name_ok)(bp)) {
16365342d17fSHajimu UMEMOTO 		RES_SET_H_ERRNO(res, NO_RECOVERY);
1637a42af91cSHajimu UMEMOTO 		return (NULL);
1638a42af91cSHajimu UMEMOTO 	}
1639a42af91cSHajimu UMEMOTO 	cp += n + QFIXEDSZ;
1640a42af91cSHajimu UMEMOTO 	if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
1641a42af91cSHajimu UMEMOTO 		/* res_send() has already verified that the query name is the
1642a42af91cSHajimu UMEMOTO 		 * same as the one we sent; this just gets the expanded name
1643a42af91cSHajimu UMEMOTO 		 * (i.e., with the succeeding search-domain tacked on).
1644a42af91cSHajimu UMEMOTO 		 */
1645a42af91cSHajimu UMEMOTO 		n = strlen(bp) + 1;		/* for the \0 */
1646a42af91cSHajimu UMEMOTO 		if (n >= MAXHOSTNAMELEN) {
16475342d17fSHajimu UMEMOTO 			RES_SET_H_ERRNO(res, NO_RECOVERY);
1648a42af91cSHajimu UMEMOTO 			return (NULL);
1649a42af91cSHajimu UMEMOTO 		}
1650a42af91cSHajimu UMEMOTO 		canonname = bp;
1651a42af91cSHajimu UMEMOTO 		bp += n;
1652a42af91cSHajimu UMEMOTO 		/* The qname can be abbreviated, but h_name is now absolute. */
1653a42af91cSHajimu UMEMOTO 		qname = canonname;
1654a42af91cSHajimu UMEMOTO 	}
1655a42af91cSHajimu UMEMOTO 	haveanswer = 0;
1656a42af91cSHajimu UMEMOTO 	had_error = 0;
1657a42af91cSHajimu UMEMOTO 	while (ancount-- > 0 && cp < eom && !had_error) {
1658dd521ef1SHajimu UMEMOTO 		n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1659a42af91cSHajimu UMEMOTO 		if ((n < 0) || !(*name_ok)(bp)) {
1660a42af91cSHajimu UMEMOTO 			had_error++;
1661a42af91cSHajimu UMEMOTO 			continue;
1662a42af91cSHajimu UMEMOTO 		}
1663a42af91cSHajimu UMEMOTO 		cp += n;			/* name */
1664a42af91cSHajimu UMEMOTO 		type = _getshort(cp);
1665a42af91cSHajimu UMEMOTO  		cp += INT16SZ;			/* type */
1666a42af91cSHajimu UMEMOTO 		class = _getshort(cp);
1667a42af91cSHajimu UMEMOTO  		cp += INT16SZ + INT32SZ;	/* class, TTL */
1668a42af91cSHajimu UMEMOTO 		n = _getshort(cp);
1669a42af91cSHajimu UMEMOTO 		cp += INT16SZ;			/* len */
1670a42af91cSHajimu UMEMOTO 		if (class != C_IN) {
1671a42af91cSHajimu UMEMOTO 			/* XXX - debug? syslog? */
1672a42af91cSHajimu UMEMOTO 			cp += n;
1673a42af91cSHajimu UMEMOTO 			continue;		/* XXX - had_error++ ? */
1674a42af91cSHajimu UMEMOTO 		}
1675a42af91cSHajimu UMEMOTO 		if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) &&
1676a42af91cSHajimu UMEMOTO 		    type == T_CNAME) {
1677a42af91cSHajimu UMEMOTO 			n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
1678a42af91cSHajimu UMEMOTO 			if ((n < 0) || !(*name_ok)(tbuf)) {
1679a42af91cSHajimu UMEMOTO 				had_error++;
1680a42af91cSHajimu UMEMOTO 				continue;
1681a42af91cSHajimu UMEMOTO 			}
1682a42af91cSHajimu UMEMOTO 			cp += n;
1683a42af91cSHajimu UMEMOTO 			/* Get canonical name. */
1684a42af91cSHajimu UMEMOTO 			n = strlen(tbuf) + 1;	/* for the \0 */
1685dd521ef1SHajimu UMEMOTO 			if (n > ep - bp || n >= MAXHOSTNAMELEN) {
1686a42af91cSHajimu UMEMOTO 				had_error++;
1687a42af91cSHajimu UMEMOTO 				continue;
1688a42af91cSHajimu UMEMOTO 			}
1689d0509082SJacques Vidrine 			strlcpy(bp, tbuf, ep - bp);
1690a42af91cSHajimu UMEMOTO 			canonname = bp;
1691a42af91cSHajimu UMEMOTO 			bp += n;
1692a42af91cSHajimu UMEMOTO 			continue;
1693a42af91cSHajimu UMEMOTO 		}
1694a42af91cSHajimu UMEMOTO 		if (qtype == T_ANY) {
1695a42af91cSHajimu UMEMOTO 			if (!(type == T_A || type == T_AAAA)) {
1696a42af91cSHajimu UMEMOTO 				cp += n;
1697a42af91cSHajimu UMEMOTO 				continue;
1698a42af91cSHajimu UMEMOTO 			}
1699a42af91cSHajimu UMEMOTO 		} else if (type != qtype) {
1700a42af91cSHajimu UMEMOTO #ifdef DEBUG
1701a42af91cSHajimu UMEMOTO 			if (type != T_KEY && type != T_SIG)
1702a42af91cSHajimu UMEMOTO 				syslog(LOG_NOTICE|LOG_AUTH,
1703a42af91cSHajimu UMEMOTO 	       "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
1704a42af91cSHajimu UMEMOTO 				       qname, p_class(C_IN), p_type(qtype),
1705a42af91cSHajimu UMEMOTO 				       p_type(type));
1706a42af91cSHajimu UMEMOTO #endif
1707a42af91cSHajimu UMEMOTO 			cp += n;
1708a42af91cSHajimu UMEMOTO 			continue;		/* XXX - had_error++ ? */
1709a42af91cSHajimu UMEMOTO 		}
1710a42af91cSHajimu UMEMOTO 		switch (type) {
1711a42af91cSHajimu UMEMOTO 		case T_A:
1712a42af91cSHajimu UMEMOTO 		case T_AAAA:
1713a42af91cSHajimu UMEMOTO 			if (strcasecmp(canonname, bp) != 0) {
1714a42af91cSHajimu UMEMOTO #ifdef DEBUG
1715a42af91cSHajimu UMEMOTO 				syslog(LOG_NOTICE|LOG_AUTH,
1716a42af91cSHajimu UMEMOTO 				       AskedForGot, canonname, bp);
1717a42af91cSHajimu UMEMOTO #endif
1718a42af91cSHajimu UMEMOTO 				cp += n;
1719a42af91cSHajimu UMEMOTO 				continue;	/* XXX - had_error++ ? */
1720a42af91cSHajimu UMEMOTO 			}
1721a42af91cSHajimu UMEMOTO 			if (type == T_A && n != INADDRSZ) {
1722a42af91cSHajimu UMEMOTO 				cp += n;
1723a42af91cSHajimu UMEMOTO 				continue;
1724a42af91cSHajimu UMEMOTO 			}
1725a42af91cSHajimu UMEMOTO 			if (type == T_AAAA && n != IN6ADDRSZ) {
1726a42af91cSHajimu UMEMOTO 				cp += n;
1727a42af91cSHajimu UMEMOTO 				continue;
1728a42af91cSHajimu UMEMOTO 			}
1729a42af91cSHajimu UMEMOTO #ifdef FILTER_V4MAPPED
1730a42af91cSHajimu UMEMOTO 			if (type == T_AAAA) {
1731a42af91cSHajimu UMEMOTO 				struct in6_addr in6;
1732a42af91cSHajimu UMEMOTO 				memcpy(&in6, cp, sizeof(in6));
1733a42af91cSHajimu UMEMOTO 				if (IN6_IS_ADDR_V4MAPPED(&in6)) {
1734a42af91cSHajimu UMEMOTO 					cp += n;
1735a42af91cSHajimu UMEMOTO 					continue;
1736a42af91cSHajimu UMEMOTO 				}
1737a42af91cSHajimu UMEMOTO 			}
1738a42af91cSHajimu UMEMOTO #endif
1739a42af91cSHajimu UMEMOTO 			if (!haveanswer) {
1740a42af91cSHajimu UMEMOTO 				int nn;
1741a42af91cSHajimu UMEMOTO 
1742a42af91cSHajimu UMEMOTO 				canonname = bp;
1743a42af91cSHajimu UMEMOTO 				nn = strlen(bp) + 1;	/* for the \0 */
1744a42af91cSHajimu UMEMOTO 				bp += nn;
1745a42af91cSHajimu UMEMOTO 			}
1746a42af91cSHajimu UMEMOTO 
1747a42af91cSHajimu UMEMOTO 			/* don't overwrite pai */
1748a42af91cSHajimu UMEMOTO 			ai = *pai;
1749a42af91cSHajimu UMEMOTO 			ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
1750a42af91cSHajimu UMEMOTO 			afd = find_afd(ai.ai_family);
1751a42af91cSHajimu UMEMOTO 			if (afd == NULL) {
1752a42af91cSHajimu UMEMOTO 				cp += n;
1753a42af91cSHajimu UMEMOTO 				continue;
1754a42af91cSHajimu UMEMOTO 			}
1755a42af91cSHajimu UMEMOTO 			cur->ai_next = get_ai(&ai, afd, (const char *)cp);
1756a42af91cSHajimu UMEMOTO 			if (cur->ai_next == NULL)
1757a42af91cSHajimu UMEMOTO 				had_error++;
1758a42af91cSHajimu UMEMOTO 			while (cur && cur->ai_next)
1759a42af91cSHajimu UMEMOTO 				cur = cur->ai_next;
1760a42af91cSHajimu UMEMOTO 			cp += n;
1761a42af91cSHajimu UMEMOTO 			break;
1762a42af91cSHajimu UMEMOTO 		default:
1763a42af91cSHajimu UMEMOTO 			abort();
1764a42af91cSHajimu UMEMOTO 		}
1765a42af91cSHajimu UMEMOTO 		if (!had_error)
1766a42af91cSHajimu UMEMOTO 			haveanswer++;
1767a42af91cSHajimu UMEMOTO 	}
1768a42af91cSHajimu UMEMOTO 	if (haveanswer) {
176969b0a4b6SJim Pirzyk #if defined(RESOLVSORT)
177069b0a4b6SJim Pirzyk 		/*
177169b0a4b6SJim Pirzyk 		 * We support only IPv4 address for backward
177269b0a4b6SJim Pirzyk 		 * compatibility against gethostbyname(3).
177369b0a4b6SJim Pirzyk 		 */
17745342d17fSHajimu UMEMOTO 		if (res->nsort && qtype == T_A) {
17755342d17fSHajimu UMEMOTO 			if (addr4sort(&sentinel, res) < 0) {
177669b0a4b6SJim Pirzyk 				freeaddrinfo(sentinel.ai_next);
17775342d17fSHajimu UMEMOTO 				RES_SET_H_ERRNO(res, NO_RECOVERY);
177869b0a4b6SJim Pirzyk 				return NULL;
177969b0a4b6SJim Pirzyk 			}
178069b0a4b6SJim Pirzyk 		}
178169b0a4b6SJim Pirzyk #endif /*RESOLVSORT*/
1782a42af91cSHajimu UMEMOTO 		if (!canonname)
1783a42af91cSHajimu UMEMOTO 			(void)get_canonname(pai, sentinel.ai_next, qname);
1784a42af91cSHajimu UMEMOTO 		else
1785a42af91cSHajimu UMEMOTO 			(void)get_canonname(pai, sentinel.ai_next, canonname);
17865342d17fSHajimu UMEMOTO 		RES_SET_H_ERRNO(res, NETDB_SUCCESS);
1787a42af91cSHajimu UMEMOTO 		return sentinel.ai_next;
1788a42af91cSHajimu UMEMOTO 	}
1789a42af91cSHajimu UMEMOTO 
17905342d17fSHajimu UMEMOTO 	RES_SET_H_ERRNO(res, NO_RECOVERY);
1791a42af91cSHajimu UMEMOTO 	return NULL;
1792a42af91cSHajimu UMEMOTO }
1793a42af91cSHajimu UMEMOTO 
1794688a5c3eSHajimu UMEMOTO #ifdef RESOLVSORT
1795688a5c3eSHajimu UMEMOTO struct addr_ptr {
1796688a5c3eSHajimu UMEMOTO 	struct addrinfo *ai;
1797688a5c3eSHajimu UMEMOTO 	int aval;
1798688a5c3eSHajimu UMEMOTO };
1799688a5c3eSHajimu UMEMOTO 
1800688a5c3eSHajimu UMEMOTO static int
18015342d17fSHajimu UMEMOTO addr4sort(struct addrinfo *sentinel, res_state res)
1802688a5c3eSHajimu UMEMOTO {
1803688a5c3eSHajimu UMEMOTO 	struct addrinfo *ai;
1804688a5c3eSHajimu UMEMOTO 	struct addr_ptr *addrs, addr;
1805688a5c3eSHajimu UMEMOTO 	struct sockaddr_in *sin;
1806688a5c3eSHajimu UMEMOTO 	int naddrs, i, j;
1807688a5c3eSHajimu UMEMOTO 	int needsort = 0;
1808688a5c3eSHajimu UMEMOTO 
1809688a5c3eSHajimu UMEMOTO 	if (!sentinel)
1810688a5c3eSHajimu UMEMOTO 		return -1;
1811688a5c3eSHajimu UMEMOTO 	naddrs = 0;
1812688a5c3eSHajimu UMEMOTO 	for (ai = sentinel->ai_next; ai; ai = ai->ai_next)
1813688a5c3eSHajimu UMEMOTO 		naddrs++;
1814688a5c3eSHajimu UMEMOTO 	if (naddrs < 2)
1815688a5c3eSHajimu UMEMOTO 		return 0;		/* We don't need sorting. */
1816688a5c3eSHajimu UMEMOTO 	if ((addrs = malloc(sizeof(struct addr_ptr) * naddrs)) == NULL)
1817688a5c3eSHajimu UMEMOTO 		return -1;
1818688a5c3eSHajimu UMEMOTO 	i = 0;
1819688a5c3eSHajimu UMEMOTO 	for (ai = sentinel->ai_next; ai; ai = ai->ai_next) {
1820688a5c3eSHajimu UMEMOTO 		sin = (struct sockaddr_in *)ai->ai_addr;
18215342d17fSHajimu UMEMOTO 		for (j = 0; (unsigned)j < res->nsort; j++) {
18225342d17fSHajimu UMEMOTO 			if (res->sort_list[j].addr.s_addr ==
18235342d17fSHajimu UMEMOTO 			    (sin->sin_addr.s_addr & res->sort_list[j].mask))
1824688a5c3eSHajimu UMEMOTO 				break;
1825688a5c3eSHajimu UMEMOTO 		}
1826688a5c3eSHajimu UMEMOTO 		addrs[i].ai = ai;
1827688a5c3eSHajimu UMEMOTO 		addrs[i].aval = j;
1828688a5c3eSHajimu UMEMOTO 		if (needsort == 0 && i > 0 && j < addrs[i - 1].aval)
1829688a5c3eSHajimu UMEMOTO 			needsort = i;
1830688a5c3eSHajimu UMEMOTO 		i++;
1831688a5c3eSHajimu UMEMOTO 	}
1832688a5c3eSHajimu UMEMOTO 	if (!needsort) {
1833688a5c3eSHajimu UMEMOTO 		free(addrs);
1834688a5c3eSHajimu UMEMOTO 		return 0;
1835688a5c3eSHajimu UMEMOTO 	}
1836688a5c3eSHajimu UMEMOTO 
1837688a5c3eSHajimu UMEMOTO 	while (needsort < naddrs) {
1838688a5c3eSHajimu UMEMOTO 	    for (j = needsort - 1; j >= 0; j--) {
1839688a5c3eSHajimu UMEMOTO 		if (addrs[j].aval > addrs[j+1].aval) {
1840688a5c3eSHajimu UMEMOTO 		    addr = addrs[j];
1841688a5c3eSHajimu UMEMOTO 		    addrs[j] = addrs[j + 1];
1842688a5c3eSHajimu UMEMOTO 		    addrs[j + 1] = addr;
1843688a5c3eSHajimu UMEMOTO 		} else
1844688a5c3eSHajimu UMEMOTO 		    break;
1845688a5c3eSHajimu UMEMOTO 	    }
1846688a5c3eSHajimu UMEMOTO 	    needsort++;
1847688a5c3eSHajimu UMEMOTO 	}
1848688a5c3eSHajimu UMEMOTO 
1849688a5c3eSHajimu UMEMOTO 	ai = sentinel;
1850688a5c3eSHajimu UMEMOTO 	for (i = 0; i < naddrs; ++i) {
1851688a5c3eSHajimu UMEMOTO 		ai->ai_next = addrs[i].ai;
1852688a5c3eSHajimu UMEMOTO 		ai = ai->ai_next;
1853688a5c3eSHajimu UMEMOTO 	}
1854688a5c3eSHajimu UMEMOTO 	ai->ai_next = NULL;
1855688a5c3eSHajimu UMEMOTO 	free(addrs);
1856688a5c3eSHajimu UMEMOTO 	return 0;
1857688a5c3eSHajimu UMEMOTO }
1858688a5c3eSHajimu UMEMOTO #endif /*RESOLVSORT*/
1859688a5c3eSHajimu UMEMOTO 
1860a42af91cSHajimu UMEMOTO /*ARGSUSED*/
1861a42af91cSHajimu UMEMOTO static int
186290d1d883SHajimu UMEMOTO _dns_getaddrinfo(void *rv, void *cb_data, va_list ap)
1863a42af91cSHajimu UMEMOTO {
1864a42af91cSHajimu UMEMOTO 	struct addrinfo *ai;
18655cd588cbSHajimu UMEMOTO 	querybuf *buf, *buf2;
1866a214cd1aSHajimu UMEMOTO 	const char *hostname;
1867248aee62SJacques Vidrine 	const struct addrinfo *pai;
1868a42af91cSHajimu UMEMOTO 	struct addrinfo sentinel, *cur;
1869a42af91cSHajimu UMEMOTO 	struct res_target q, q2;
18705342d17fSHajimu UMEMOTO 	res_state res;
1871a42af91cSHajimu UMEMOTO 
1872a214cd1aSHajimu UMEMOTO 	hostname = va_arg(ap, char *);
1873248aee62SJacques Vidrine 	pai = va_arg(ap, const struct addrinfo *);
1874248aee62SJacques Vidrine 
1875f1fa5b4fSXin LI 	memset(&q, 0, sizeof(q));
1876a42af91cSHajimu UMEMOTO 	memset(&q2, 0, sizeof(q2));
1877a42af91cSHajimu UMEMOTO 	memset(&sentinel, 0, sizeof(sentinel));
1878a42af91cSHajimu UMEMOTO 	cur = &sentinel;
1879a42af91cSHajimu UMEMOTO 
18805cd588cbSHajimu UMEMOTO 	buf = malloc(sizeof(*buf));
18815cd588cbSHajimu UMEMOTO 	if (!buf) {
18825342d17fSHajimu UMEMOTO 		RES_SET_H_ERRNO(res, NETDB_INTERNAL);
18835cd588cbSHajimu UMEMOTO 		return NS_NOTFOUND;
18845cd588cbSHajimu UMEMOTO 	}
18855cd588cbSHajimu UMEMOTO 	buf2 = malloc(sizeof(*buf2));
18865cd588cbSHajimu UMEMOTO 	if (!buf2) {
18875cd588cbSHajimu UMEMOTO 		free(buf);
18885342d17fSHajimu UMEMOTO 		RES_SET_H_ERRNO(res, NETDB_INTERNAL);
18895cd588cbSHajimu UMEMOTO 		return NS_NOTFOUND;
18905cd588cbSHajimu UMEMOTO 	}
18915cd588cbSHajimu UMEMOTO 
1892a42af91cSHajimu UMEMOTO 	switch (pai->ai_family) {
1893a42af91cSHajimu UMEMOTO 	case AF_UNSPEC:
1894a214cd1aSHajimu UMEMOTO 		q.name = hostname;
1895b826397aSJun-ichiro itojun Hagino 		q.qclass = C_IN;
1896d0d6a2c7SHajimu UMEMOTO 		q.qtype = T_A;
18975cd588cbSHajimu UMEMOTO 		q.answer = buf->buf;
18985cd588cbSHajimu UMEMOTO 		q.anslen = sizeof(buf->buf);
1899a42af91cSHajimu UMEMOTO 		q.next = &q2;
1900a214cd1aSHajimu UMEMOTO 		q2.name = hostname;
1901b826397aSJun-ichiro itojun Hagino 		q2.qclass = C_IN;
1902d0d6a2c7SHajimu UMEMOTO 		q2.qtype = T_AAAA;
19035cd588cbSHajimu UMEMOTO 		q2.answer = buf2->buf;
19045cd588cbSHajimu UMEMOTO 		q2.anslen = sizeof(buf2->buf);
1905a42af91cSHajimu UMEMOTO 		break;
1906a42af91cSHajimu UMEMOTO 	case AF_INET:
1907a214cd1aSHajimu UMEMOTO 		q.name = hostname;
1908b826397aSJun-ichiro itojun Hagino 		q.qclass = C_IN;
1909b826397aSJun-ichiro itojun Hagino 		q.qtype = T_A;
19105cd588cbSHajimu UMEMOTO 		q.answer = buf->buf;
19115cd588cbSHajimu UMEMOTO 		q.anslen = sizeof(buf->buf);
1912a42af91cSHajimu UMEMOTO 		break;
1913a42af91cSHajimu UMEMOTO 	case AF_INET6:
1914a214cd1aSHajimu UMEMOTO 		q.name = hostname;
1915b826397aSJun-ichiro itojun Hagino 		q.qclass = C_IN;
1916b826397aSJun-ichiro itojun Hagino 		q.qtype = T_AAAA;
19175cd588cbSHajimu UMEMOTO 		q.answer = buf->buf;
19185cd588cbSHajimu UMEMOTO 		q.anslen = sizeof(buf->buf);
1919a42af91cSHajimu UMEMOTO 		break;
1920a42af91cSHajimu UMEMOTO 	default:
19215cd588cbSHajimu UMEMOTO 		free(buf);
19225cd588cbSHajimu UMEMOTO 		free(buf2);
1923248aee62SJacques Vidrine 		return NS_UNAVAIL;
1924a42af91cSHajimu UMEMOTO 	}
19255342d17fSHajimu UMEMOTO 
19265342d17fSHajimu UMEMOTO 	res = __res_state();
19275342d17fSHajimu UMEMOTO 	if ((res->options & RES_INIT) == 0 && res_ninit(res) == -1) {
19285342d17fSHajimu UMEMOTO 		RES_SET_H_ERRNO(res, NETDB_INTERNAL);
19295342d17fSHajimu UMEMOTO 		free(buf);
19305342d17fSHajimu UMEMOTO 		free(buf2);
19315342d17fSHajimu UMEMOTO 		return NS_NOTFOUND;
19325342d17fSHajimu UMEMOTO 	}
19335342d17fSHajimu UMEMOTO 
19345342d17fSHajimu UMEMOTO 	if (res_searchN(hostname, &q, res) < 0) {
19355cd588cbSHajimu UMEMOTO 		free(buf);
19365cd588cbSHajimu UMEMOTO 		free(buf2);
1937248aee62SJacques Vidrine 		return NS_NOTFOUND;
19385cd588cbSHajimu UMEMOTO 	}
1939d0d6a2c7SHajimu UMEMOTO 	/* prefer IPv6 */
1940d0d6a2c7SHajimu UMEMOTO 	if (q.next) {
19415342d17fSHajimu UMEMOTO 		ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai, res);
1942a42af91cSHajimu UMEMOTO 		if (ai) {
1943a42af91cSHajimu UMEMOTO 			cur->ai_next = ai;
1944a42af91cSHajimu UMEMOTO 			while (cur && cur->ai_next)
1945a42af91cSHajimu UMEMOTO 				cur = cur->ai_next;
1946a42af91cSHajimu UMEMOTO 		}
1947d0d6a2c7SHajimu UMEMOTO 	}
19485342d17fSHajimu UMEMOTO 	ai = getanswer(buf, q.n, q.name, q.qtype, pai, res);
1949a42af91cSHajimu UMEMOTO 	if (ai)
1950a42af91cSHajimu UMEMOTO 		cur->ai_next = ai;
19515cd588cbSHajimu UMEMOTO 	free(buf);
19525cd588cbSHajimu UMEMOTO 	free(buf2);
1953a42af91cSHajimu UMEMOTO 	if (sentinel.ai_next == NULL)
19545342d17fSHajimu UMEMOTO 		switch (res->res_h_errno) {
1955a42af91cSHajimu UMEMOTO 		case HOST_NOT_FOUND:
1956248aee62SJacques Vidrine 			return NS_NOTFOUND;
1957a42af91cSHajimu UMEMOTO 		case TRY_AGAIN:
1958248aee62SJacques Vidrine 			return NS_TRYAGAIN;
1959a42af91cSHajimu UMEMOTO 		default:
1960248aee62SJacques Vidrine 			return NS_UNAVAIL;
1961a42af91cSHajimu UMEMOTO 		}
1962248aee62SJacques Vidrine 	*((struct addrinfo **)rv) = sentinel.ai_next;
1963248aee62SJacques Vidrine 	return NS_SUCCESS;
1964248aee62SJacques Vidrine }
1965248aee62SJacques Vidrine 
1966248aee62SJacques Vidrine static void
19679def31dcSHajimu UMEMOTO _sethtent(FILE **hostf)
1968248aee62SJacques Vidrine {
19699def31dcSHajimu UMEMOTO 	if (!*hostf)
19709def31dcSHajimu UMEMOTO 		*hostf = fopen(_PATH_HOSTS, "r");
1971248aee62SJacques Vidrine 	else
19729def31dcSHajimu UMEMOTO 		rewind(*hostf);
1973248aee62SJacques Vidrine }
1974248aee62SJacques Vidrine 
1975248aee62SJacques Vidrine static void
19769def31dcSHajimu UMEMOTO _endhtent(FILE **hostf)
1977248aee62SJacques Vidrine {
19789def31dcSHajimu UMEMOTO 	if (*hostf) {
19799def31dcSHajimu UMEMOTO 		(void) fclose(*hostf);
19809def31dcSHajimu UMEMOTO 		*hostf = NULL;
1981248aee62SJacques Vidrine 	}
1982a42af91cSHajimu UMEMOTO }
1983a42af91cSHajimu UMEMOTO 
1984a42af91cSHajimu UMEMOTO static struct addrinfo *
19859def31dcSHajimu UMEMOTO _gethtent(FILE **hostf, const char *name, const struct addrinfo *pai)
1986a42af91cSHajimu UMEMOTO {
1987a42af91cSHajimu UMEMOTO 	char *p;
1988a42af91cSHajimu UMEMOTO 	char *cp, *tname, *cname;
1989a42af91cSHajimu UMEMOTO 	struct addrinfo hints, *res0, *res;
1990a42af91cSHajimu UMEMOTO 	int error;
1991a42af91cSHajimu UMEMOTO 	const char *addr;
1992a42af91cSHajimu UMEMOTO 	char hostbuf[8*1024];
1993a42af91cSHajimu UMEMOTO 
19949def31dcSHajimu UMEMOTO 	if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "r")))
1995248aee62SJacques Vidrine 		return (NULL);
1996a42af91cSHajimu UMEMOTO again:
19979def31dcSHajimu UMEMOTO 	if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf)))
1998a42af91cSHajimu UMEMOTO 		return (NULL);
1999a42af91cSHajimu UMEMOTO 	if (*p == '#')
2000a42af91cSHajimu UMEMOTO 		goto again;
20017b6cc404SMaxim Sobolev 	cp = strpbrk(p, "#\n");
20027b6cc404SMaxim Sobolev 	if (cp != NULL)
2003a42af91cSHajimu UMEMOTO 		*cp = '\0';
2004a42af91cSHajimu UMEMOTO 	if (!(cp = strpbrk(p, " \t")))
2005a42af91cSHajimu UMEMOTO 		goto again;
2006a42af91cSHajimu UMEMOTO 	*cp++ = '\0';
2007a42af91cSHajimu UMEMOTO 	addr = p;
2008a42af91cSHajimu UMEMOTO 	cname = NULL;
2009a42af91cSHajimu UMEMOTO 	/* if this is not something we're looking for, skip it. */
2010a42af91cSHajimu UMEMOTO 	while (cp && *cp) {
2011a42af91cSHajimu UMEMOTO 		if (*cp == ' ' || *cp == '\t') {
2012a42af91cSHajimu UMEMOTO 			cp++;
2013a42af91cSHajimu UMEMOTO 			continue;
2014a42af91cSHajimu UMEMOTO 		}
2015a42af91cSHajimu UMEMOTO 		tname = cp;
2016a42af91cSHajimu UMEMOTO 		if (cname == NULL)
2017a42af91cSHajimu UMEMOTO 			cname = cp;
2018a42af91cSHajimu UMEMOTO 		if ((cp = strpbrk(cp, " \t")) != NULL)
2019a42af91cSHajimu UMEMOTO 			*cp++ = '\0';
2020a42af91cSHajimu UMEMOTO 		if (strcasecmp(name, tname) == 0)
2021a42af91cSHajimu UMEMOTO 			goto found;
2022a42af91cSHajimu UMEMOTO 	}
2023a42af91cSHajimu UMEMOTO 	goto again;
2024a42af91cSHajimu UMEMOTO 
2025a42af91cSHajimu UMEMOTO found:
2026dd521ef1SHajimu UMEMOTO 	/* we should not glob socktype/protocol here */
2027dd521ef1SHajimu UMEMOTO 	memset(&hints, 0, sizeof(hints));
2028dd521ef1SHajimu UMEMOTO 	hints.ai_family = pai->ai_family;
2029dd521ef1SHajimu UMEMOTO 	hints.ai_socktype = SOCK_DGRAM;
2030dd521ef1SHajimu UMEMOTO 	hints.ai_protocol = 0;
2031a42af91cSHajimu UMEMOTO 	hints.ai_flags = AI_NUMERICHOST;
2032dd521ef1SHajimu UMEMOTO 	error = getaddrinfo(addr, "0", &hints, &res0);
2033a42af91cSHajimu UMEMOTO 	if (error)
2034a42af91cSHajimu UMEMOTO 		goto again;
2035a42af91cSHajimu UMEMOTO #ifdef FILTER_V4MAPPED
2036a42af91cSHajimu UMEMOTO 	/* XXX should check all items in the chain */
2037a42af91cSHajimu UMEMOTO 	if (res0->ai_family == AF_INET6 &&
2038a42af91cSHajimu UMEMOTO 	    IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)res0->ai_addr)->sin6_addr)) {
2039a42af91cSHajimu UMEMOTO 		freeaddrinfo(res0);
2040a42af91cSHajimu UMEMOTO 		goto again;
2041a42af91cSHajimu UMEMOTO 	}
2042a42af91cSHajimu UMEMOTO #endif
2043a42af91cSHajimu UMEMOTO 	for (res = res0; res; res = res->ai_next) {
2044a42af91cSHajimu UMEMOTO 		/* cover it up */
2045a42af91cSHajimu UMEMOTO 		res->ai_flags = pai->ai_flags;
2046dd521ef1SHajimu UMEMOTO 		res->ai_socktype = pai->ai_socktype;
2047dd521ef1SHajimu UMEMOTO 		res->ai_protocol = pai->ai_protocol;
2048a42af91cSHajimu UMEMOTO 
2049a42af91cSHajimu UMEMOTO 		if (pai->ai_flags & AI_CANONNAME) {
2050a42af91cSHajimu UMEMOTO 			if (get_canonname(pai, res, cname) != 0) {
2051a42af91cSHajimu UMEMOTO 				freeaddrinfo(res0);
2052a42af91cSHajimu UMEMOTO 				goto again;
2053a42af91cSHajimu UMEMOTO 			}
2054a42af91cSHajimu UMEMOTO 		}
2055a42af91cSHajimu UMEMOTO 	}
2056a42af91cSHajimu UMEMOTO 	return res0;
2057a42af91cSHajimu UMEMOTO }
2058a42af91cSHajimu UMEMOTO 
2059a42af91cSHajimu UMEMOTO /*ARGSUSED*/
2060a42af91cSHajimu UMEMOTO static int
206190d1d883SHajimu UMEMOTO _files_getaddrinfo(void *rv, void *cb_data, va_list ap)
2062a42af91cSHajimu UMEMOTO {
2063248aee62SJacques Vidrine 	const char *name;
2064248aee62SJacques Vidrine 	const struct addrinfo *pai;
2065a42af91cSHajimu UMEMOTO 	struct addrinfo sentinel, *cur;
2066a42af91cSHajimu UMEMOTO 	struct addrinfo *p;
20679def31dcSHajimu UMEMOTO 	FILE *hostf = NULL;
2068a42af91cSHajimu UMEMOTO 
2069248aee62SJacques Vidrine 	name = va_arg(ap, char *);
2070248aee62SJacques Vidrine 	pai = va_arg(ap, struct addrinfo *);
2071248aee62SJacques Vidrine 
2072248aee62SJacques Vidrine 	memset(&sentinel, 0, sizeof(sentinel));
2073a42af91cSHajimu UMEMOTO 	cur = &sentinel;
2074a42af91cSHajimu UMEMOTO 
20759def31dcSHajimu UMEMOTO 	_sethtent(&hostf);
20769def31dcSHajimu UMEMOTO 	while ((p = _gethtent(&hostf, name, pai)) != NULL) {
2077a42af91cSHajimu UMEMOTO 		cur->ai_next = p;
2078a42af91cSHajimu UMEMOTO 		while (cur && cur->ai_next)
2079a42af91cSHajimu UMEMOTO 			cur = cur->ai_next;
2080a42af91cSHajimu UMEMOTO 	}
20819def31dcSHajimu UMEMOTO 	_endhtent(&hostf);
2082a42af91cSHajimu UMEMOTO 
2083248aee62SJacques Vidrine 	*((struct addrinfo **)rv) = sentinel.ai_next;
2084248aee62SJacques Vidrine 	if (sentinel.ai_next == NULL)
2085248aee62SJacques Vidrine 		return NS_NOTFOUND;
2086248aee62SJacques Vidrine 	return NS_SUCCESS;
2087a42af91cSHajimu UMEMOTO }
2088a42af91cSHajimu UMEMOTO 
2089a42af91cSHajimu UMEMOTO #ifdef YP
2090248aee62SJacques Vidrine /*ARGSUSED*/
2091248aee62SJacques Vidrine static struct addrinfo *
209290d1d883SHajimu UMEMOTO _yphostent(char *line, const struct addrinfo *pai)
2093248aee62SJacques Vidrine {
2094248aee62SJacques Vidrine 	struct addrinfo sentinel, *cur;
2095248aee62SJacques Vidrine 	struct addrinfo hints, *res, *res0;
2096248aee62SJacques Vidrine 	int error;
2097248aee62SJacques Vidrine 	char *p = line;
2098248aee62SJacques Vidrine 	const char *addr, *canonname;
2099248aee62SJacques Vidrine 	char *nextline;
2100248aee62SJacques Vidrine 	char *cp;
2101248aee62SJacques Vidrine 
2102248aee62SJacques Vidrine 	addr = canonname = NULL;
2103248aee62SJacques Vidrine 
2104248aee62SJacques Vidrine 	memset(&sentinel, 0, sizeof(sentinel));
2105a42af91cSHajimu UMEMOTO 	cur = &sentinel;
2106a42af91cSHajimu UMEMOTO 
2107248aee62SJacques Vidrine nextline:
2108248aee62SJacques Vidrine 	/* terminate line */
2109248aee62SJacques Vidrine 	cp = strchr(p, '\n');
2110248aee62SJacques Vidrine 	if (cp) {
2111248aee62SJacques Vidrine 		*cp++ = '\0';
2112248aee62SJacques Vidrine 		nextline = cp;
2113248aee62SJacques Vidrine 	} else
2114248aee62SJacques Vidrine 		nextline = NULL;
2115a42af91cSHajimu UMEMOTO 
2116248aee62SJacques Vidrine 	cp = strpbrk(p, " \t");
2117248aee62SJacques Vidrine 	if (cp == NULL) {
2118248aee62SJacques Vidrine 		if (canonname == NULL)
2119248aee62SJacques Vidrine 			return (NULL);
2120248aee62SJacques Vidrine 		else
2121248aee62SJacques Vidrine 			goto done;
2122a42af91cSHajimu UMEMOTO 	}
2123248aee62SJacques Vidrine 	*cp++ = '\0';
2124a42af91cSHajimu UMEMOTO 
2125248aee62SJacques Vidrine 	addr = p;
2126a42af91cSHajimu UMEMOTO 
2127248aee62SJacques Vidrine 	while (cp && *cp) {
2128248aee62SJacques Vidrine 		if (*cp == ' ' || *cp == '\t') {
2129248aee62SJacques Vidrine 			cp++;
2130a42af91cSHajimu UMEMOTO 			continue;
2131248aee62SJacques Vidrine 		}
2132248aee62SJacques Vidrine 		if (!canonname)
2133248aee62SJacques Vidrine 			canonname = cp;
2134248aee62SJacques Vidrine 		if ((cp = strpbrk(cp, " \t")) != NULL)
2135248aee62SJacques Vidrine 			*cp++ = '\0';
2136a42af91cSHajimu UMEMOTO 	}
2137a42af91cSHajimu UMEMOTO 
2138bf838688SHajimu UMEMOTO 	hints = *pai;
2139248aee62SJacques Vidrine 	hints.ai_flags = AI_NUMERICHOST;
2140bf838688SHajimu UMEMOTO 	error = getaddrinfo(addr, NULL, &hints, &res0);
2141248aee62SJacques Vidrine 	if (error == 0) {
2142248aee62SJacques Vidrine 		for (res = res0; res; res = res->ai_next) {
2143248aee62SJacques Vidrine 			/* cover it up */
2144248aee62SJacques Vidrine 			res->ai_flags = pai->ai_flags;
2145248aee62SJacques Vidrine 
2146248aee62SJacques Vidrine 			if (pai->ai_flags & AI_CANONNAME)
2147248aee62SJacques Vidrine 				(void)get_canonname(pai, res, canonname);
2148248aee62SJacques Vidrine 		}
2149248aee62SJacques Vidrine 	} else
2150248aee62SJacques Vidrine 		res0 = NULL;
2151248aee62SJacques Vidrine 	if (res0) {
2152248aee62SJacques Vidrine 		cur->ai_next = res0;
2153a42af91cSHajimu UMEMOTO 		while (cur && cur->ai_next)
2154a42af91cSHajimu UMEMOTO 			cur = cur->ai_next;
2155a42af91cSHajimu UMEMOTO 	}
2156a42af91cSHajimu UMEMOTO 
2157248aee62SJacques Vidrine 	if (nextline) {
2158248aee62SJacques Vidrine 		p = nextline;
2159248aee62SJacques Vidrine 		goto nextline;
2160248aee62SJacques Vidrine 	}
2161a42af91cSHajimu UMEMOTO 
2162248aee62SJacques Vidrine done:
2163248aee62SJacques Vidrine 	return sentinel.ai_next;
2164248aee62SJacques Vidrine }
2165248aee62SJacques Vidrine 
2166248aee62SJacques Vidrine /*ARGSUSED*/
2167248aee62SJacques Vidrine static int
216890d1d883SHajimu UMEMOTO _yp_getaddrinfo(void *rv, void *cb_data, va_list ap)
2169248aee62SJacques Vidrine {
2170248aee62SJacques Vidrine 	struct addrinfo sentinel, *cur;
2171248aee62SJacques Vidrine 	struct addrinfo *ai = NULL;
2172d7057edbSHajimu UMEMOTO 	char *ypbuf;
2173d7057edbSHajimu UMEMOTO 	int ypbuflen, r;
2174248aee62SJacques Vidrine 	const char *name;
2175248aee62SJacques Vidrine 	const struct addrinfo *pai;
2176d7057edbSHajimu UMEMOTO 	char *ypdomain;
2177d7057edbSHajimu UMEMOTO 
2178d7057edbSHajimu UMEMOTO 	if (_yp_check(&ypdomain) == 0)
2179d7057edbSHajimu UMEMOTO 		return NS_UNAVAIL;
2180248aee62SJacques Vidrine 
2181248aee62SJacques Vidrine 	name = va_arg(ap, char *);
2182248aee62SJacques Vidrine 	pai = va_arg(ap, const struct addrinfo *);
2183248aee62SJacques Vidrine 
2184248aee62SJacques Vidrine 	memset(&sentinel, 0, sizeof(sentinel));
2185248aee62SJacques Vidrine 	cur = &sentinel;
2186248aee62SJacques Vidrine 
2187248aee62SJacques Vidrine 	/* hosts.byname is only for IPv4 (Solaris8) */
2188248aee62SJacques Vidrine 	if (pai->ai_family == PF_UNSPEC || pai->ai_family == PF_INET) {
2189d7057edbSHajimu UMEMOTO 		r = yp_match(ypdomain, "hosts.byname", name,
2190d7057edbSHajimu UMEMOTO 			(int)strlen(name), &ypbuf, &ypbuflen);
2191248aee62SJacques Vidrine 		if (r == 0) {
2192248aee62SJacques Vidrine 			struct addrinfo ai4;
2193248aee62SJacques Vidrine 
2194248aee62SJacques Vidrine 			ai4 = *pai;
2195248aee62SJacques Vidrine 			ai4.ai_family = AF_INET;
2196d7057edbSHajimu UMEMOTO 			ai = _yphostent(ypbuf, &ai4);
2197248aee62SJacques Vidrine 			if (ai) {
2198248aee62SJacques Vidrine 				cur->ai_next = ai;
2199248aee62SJacques Vidrine 				while (cur && cur->ai_next)
2200248aee62SJacques Vidrine 					cur = cur->ai_next;
2201248aee62SJacques Vidrine 			}
2202d7057edbSHajimu UMEMOTO 			free(ypbuf);
2203248aee62SJacques Vidrine 		}
2204f8997ca2SHajimu UMEMOTO 	}
2205248aee62SJacques Vidrine 
2206248aee62SJacques Vidrine 	/* ipnodes.byname can hold both IPv4/v6 */
2207d7057edbSHajimu UMEMOTO 	r = yp_match(ypdomain, "ipnodes.byname", name,
2208d7057edbSHajimu UMEMOTO 		(int)strlen(name), &ypbuf, &ypbuflen);
2209248aee62SJacques Vidrine 	if (r == 0) {
2210d7057edbSHajimu UMEMOTO 		ai = _yphostent(ypbuf, pai);
2211d7057edbSHajimu UMEMOTO 		if (ai)
2212248aee62SJacques Vidrine 			cur->ai_next = ai;
2213d7057edbSHajimu UMEMOTO 		free(ypbuf);
2214248aee62SJacques Vidrine 	}
2215248aee62SJacques Vidrine 
2216248aee62SJacques Vidrine 	if (sentinel.ai_next == NULL) {
22175342d17fSHajimu UMEMOTO 		RES_SET_H_ERRNO(__res_state(), HOST_NOT_FOUND);
2218248aee62SJacques Vidrine 		return NS_NOTFOUND;
2219248aee62SJacques Vidrine 	}
2220248aee62SJacques Vidrine 	*((struct addrinfo **)rv) = sentinel.ai_next;
2221248aee62SJacques Vidrine 	return NS_SUCCESS;
2222a42af91cSHajimu UMEMOTO }
2223a42af91cSHajimu UMEMOTO #endif
2224a42af91cSHajimu UMEMOTO 
2225a42af91cSHajimu UMEMOTO /* resolver logic */
2226a42af91cSHajimu UMEMOTO 
2227a42af91cSHajimu UMEMOTO /*
2228a42af91cSHajimu UMEMOTO  * Formulate a normal query, send, and await answer.
2229a42af91cSHajimu UMEMOTO  * Returned answer is placed in supplied buffer "answer".
2230a42af91cSHajimu UMEMOTO  * Perform preliminary check of answer, returning success only
2231a42af91cSHajimu UMEMOTO  * if no error is indicated and the answer count is nonzero.
2232a42af91cSHajimu UMEMOTO  * Return the size of the response on success, -1 on error.
2233a42af91cSHajimu UMEMOTO  * Error number is left in h_errno.
2234a42af91cSHajimu UMEMOTO  *
2235a42af91cSHajimu UMEMOTO  * Caller must parse answer and determine whether it answers the question.
2236a42af91cSHajimu UMEMOTO  */
2237a42af91cSHajimu UMEMOTO static int
223890d1d883SHajimu UMEMOTO res_queryN(const char *name, struct res_target *target, res_state res)
2239a42af91cSHajimu UMEMOTO {
22405cd588cbSHajimu UMEMOTO 	u_char *buf;
2241a42af91cSHajimu UMEMOTO 	HEADER *hp;
2242a42af91cSHajimu UMEMOTO 	int n;
2243a42af91cSHajimu UMEMOTO 	struct res_target *t;
2244a42af91cSHajimu UMEMOTO 	int rcode;
2245a42af91cSHajimu UMEMOTO 	int ancount;
2246a42af91cSHajimu UMEMOTO 
2247a42af91cSHajimu UMEMOTO 	rcode = NOERROR;
2248a42af91cSHajimu UMEMOTO 	ancount = 0;
2249a42af91cSHajimu UMEMOTO 
22505cd588cbSHajimu UMEMOTO 	buf = malloc(MAXPACKET);
22515cd588cbSHajimu UMEMOTO 	if (!buf) {
22525342d17fSHajimu UMEMOTO 		RES_SET_H_ERRNO(res, NETDB_INTERNAL);
22535cd588cbSHajimu UMEMOTO 		return -1;
22545cd588cbSHajimu UMEMOTO 	}
22555cd588cbSHajimu UMEMOTO 
2256a42af91cSHajimu UMEMOTO 	for (t = target; t; t = t->next) {
2257a42af91cSHajimu UMEMOTO 		int class, type;
2258a42af91cSHajimu UMEMOTO 		u_char *answer;
2259a42af91cSHajimu UMEMOTO 		int anslen;
2260a42af91cSHajimu UMEMOTO 
2261a42af91cSHajimu UMEMOTO 		hp = (HEADER *)(void *)t->answer;
2262a42af91cSHajimu UMEMOTO 		hp->rcode = NOERROR;	/* default */
2263a42af91cSHajimu UMEMOTO 
2264a42af91cSHajimu UMEMOTO 		/* make it easier... */
2265b826397aSJun-ichiro itojun Hagino 		class = t->qclass;
2266b826397aSJun-ichiro itojun Hagino 		type = t->qtype;
2267a42af91cSHajimu UMEMOTO 		answer = t->answer;
2268a42af91cSHajimu UMEMOTO 		anslen = t->anslen;
2269a42af91cSHajimu UMEMOTO #ifdef DEBUG
22705342d17fSHajimu UMEMOTO 		if (res->options & RES_DEBUG)
2271a42af91cSHajimu UMEMOTO 			printf(";; res_query(%s, %d, %d)\n", name, class, type);
2272a42af91cSHajimu UMEMOTO #endif
2273a42af91cSHajimu UMEMOTO 
22745342d17fSHajimu UMEMOTO 		n = res_nmkquery(res, QUERY, name, class, type, NULL, 0, NULL,
22755cd588cbSHajimu UMEMOTO 		    buf, MAXPACKET);
22765342d17fSHajimu UMEMOTO 		if (n > 0 && (res->options & RES_USE_EDNS0) != 0)
22775342d17fSHajimu UMEMOTO 			n = res_nopt(res, n, buf, MAXPACKET, anslen);
2278a42af91cSHajimu UMEMOTO 		if (n <= 0) {
2279a42af91cSHajimu UMEMOTO #ifdef DEBUG
22805342d17fSHajimu UMEMOTO 			if (res->options & RES_DEBUG)
2281a42af91cSHajimu UMEMOTO 				printf(";; res_query: mkquery failed\n");
2282a42af91cSHajimu UMEMOTO #endif
22835cd588cbSHajimu UMEMOTO 			free(buf);
22845342d17fSHajimu UMEMOTO 			RES_SET_H_ERRNO(res, NO_RECOVERY);
2285a42af91cSHajimu UMEMOTO 			return (n);
2286a42af91cSHajimu UMEMOTO 		}
22875342d17fSHajimu UMEMOTO 		n = res_nsend(res, buf, n, answer, anslen);
2288a42af91cSHajimu UMEMOTO #if 0
2289a42af91cSHajimu UMEMOTO 		if (n < 0) {
2290a42af91cSHajimu UMEMOTO #ifdef DEBUG
22915342d17fSHajimu UMEMOTO 			if (res->options & RES_DEBUG)
2292a42af91cSHajimu UMEMOTO 				printf(";; res_query: send error\n");
2293a42af91cSHajimu UMEMOTO #endif
22945cd588cbSHajimu UMEMOTO 			free(buf);
22955342d17fSHajimu UMEMOTO 			RES_SET_H_ERRNO(res, TRY_AGAIN);
2296a42af91cSHajimu UMEMOTO 			return (n);
2297a42af91cSHajimu UMEMOTO 		}
2298a42af91cSHajimu UMEMOTO #endif
2299a42af91cSHajimu UMEMOTO 
2300d05ad2abSHajimu UMEMOTO 		if (n > anslen)
230154384cf3SJacques Vidrine 			hp->rcode = FORMERR; /* XXX not very informative */
2302d05ad2abSHajimu UMEMOTO 		if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
2303a42af91cSHajimu UMEMOTO 			rcode = hp->rcode;	/* record most recent error */
2304a42af91cSHajimu UMEMOTO #ifdef DEBUG
23055342d17fSHajimu UMEMOTO 			if (res->options & RES_DEBUG)
2306dd521ef1SHajimu UMEMOTO 				printf(";; rcode = %u, ancount=%u\n", hp->rcode,
2307a42af91cSHajimu UMEMOTO 				    ntohs(hp->ancount));
2308a42af91cSHajimu UMEMOTO #endif
2309a42af91cSHajimu UMEMOTO 			continue;
2310a42af91cSHajimu UMEMOTO 		}
2311a42af91cSHajimu UMEMOTO 
2312a42af91cSHajimu UMEMOTO 		ancount += ntohs(hp->ancount);
2313a42af91cSHajimu UMEMOTO 
2314a42af91cSHajimu UMEMOTO 		t->n = n;
2315a42af91cSHajimu UMEMOTO 	}
2316a42af91cSHajimu UMEMOTO 
23175cd588cbSHajimu UMEMOTO 	free(buf);
23185cd588cbSHajimu UMEMOTO 
2319a42af91cSHajimu UMEMOTO 	if (ancount == 0) {
2320a42af91cSHajimu UMEMOTO 		switch (rcode) {
2321a42af91cSHajimu UMEMOTO 		case NXDOMAIN:
23225342d17fSHajimu UMEMOTO 			RES_SET_H_ERRNO(res, HOST_NOT_FOUND);
2323a42af91cSHajimu UMEMOTO 			break;
2324a42af91cSHajimu UMEMOTO 		case SERVFAIL:
23255342d17fSHajimu UMEMOTO 			RES_SET_H_ERRNO(res, TRY_AGAIN);
2326a42af91cSHajimu UMEMOTO 			break;
2327a42af91cSHajimu UMEMOTO 		case NOERROR:
23285342d17fSHajimu UMEMOTO 			RES_SET_H_ERRNO(res, NO_DATA);
2329a42af91cSHajimu UMEMOTO 			break;
2330a42af91cSHajimu UMEMOTO 		case FORMERR:
2331a42af91cSHajimu UMEMOTO 		case NOTIMP:
2332a42af91cSHajimu UMEMOTO 		case REFUSED:
2333a42af91cSHajimu UMEMOTO 		default:
23345342d17fSHajimu UMEMOTO 			RES_SET_H_ERRNO(res, NO_RECOVERY);
2335a42af91cSHajimu UMEMOTO 			break;
2336a42af91cSHajimu UMEMOTO 		}
2337a42af91cSHajimu UMEMOTO 		return (-1);
2338a42af91cSHajimu UMEMOTO 	}
2339a42af91cSHajimu UMEMOTO 	return (ancount);
2340a42af91cSHajimu UMEMOTO }
2341a42af91cSHajimu UMEMOTO 
2342a42af91cSHajimu UMEMOTO /*
2343a42af91cSHajimu UMEMOTO  * Formulate a normal query, send, and retrieve answer in supplied buffer.
2344a42af91cSHajimu UMEMOTO  * Return the size of the response on success, -1 on error.
2345a42af91cSHajimu UMEMOTO  * If enabled, implement search rules until answer or unrecoverable failure
2346a42af91cSHajimu UMEMOTO  * is detected.  Error code, if any, is left in h_errno.
2347a42af91cSHajimu UMEMOTO  */
2348a42af91cSHajimu UMEMOTO static int
234990d1d883SHajimu UMEMOTO res_searchN(const char *name, struct res_target *target, res_state res)
2350a42af91cSHajimu UMEMOTO {
2351a42af91cSHajimu UMEMOTO 	const char *cp, * const *domain;
2352a42af91cSHajimu UMEMOTO 	HEADER *hp = (HEADER *)(void *)target->answer;	/*XXX*/
2353a42af91cSHajimu UMEMOTO 	u_int dots;
2354a42af91cSHajimu UMEMOTO 	int trailing_dot, ret, saved_herrno;
23555ecccefbSHajimu UMEMOTO 	int got_nodata = 0, got_servfail = 0, root_on_list = 0;
23565ecccefbSHajimu UMEMOTO 	int tried_as_is = 0;
23575ecccefbSHajimu UMEMOTO 	int searched = 0;
2358159d2a98SHajimu UMEMOTO 	char abuf[MAXDNAME];
2359a42af91cSHajimu UMEMOTO 
2360a42af91cSHajimu UMEMOTO 	errno = 0;
23615342d17fSHajimu UMEMOTO 	RES_SET_H_ERRNO(res, HOST_NOT_FOUND); /* default, if we never query */
2362a42af91cSHajimu UMEMOTO 	dots = 0;
2363a42af91cSHajimu UMEMOTO 	for (cp = name; *cp; cp++)
2364a42af91cSHajimu UMEMOTO 		dots += (*cp == '.');
2365a42af91cSHajimu UMEMOTO 	trailing_dot = 0;
2366a42af91cSHajimu UMEMOTO 	if (cp > name && *--cp == '.')
2367a42af91cSHajimu UMEMOTO 		trailing_dot++;
2368a42af91cSHajimu UMEMOTO 
2369a42af91cSHajimu UMEMOTO 	/*
2370a42af91cSHajimu UMEMOTO 	 * if there aren't any dots, it could be a user-level alias
2371a42af91cSHajimu UMEMOTO 	 */
23725342d17fSHajimu UMEMOTO 	if (!dots &&
23735342d17fSHajimu UMEMOTO 	    (cp = res_hostalias(res, name, abuf, sizeof(abuf))) != NULL)
23745342d17fSHajimu UMEMOTO 		return (res_queryN(cp, target, res));
2375a42af91cSHajimu UMEMOTO 
2376a42af91cSHajimu UMEMOTO 	/*
23775ecccefbSHajimu UMEMOTO 	 * If there are enough dots in the name, let's just give it a
23785ecccefbSHajimu UMEMOTO 	 * try 'as is'. The threshold can be set with the "ndots" option.
23795ecccefbSHajimu UMEMOTO 	 * Also, query 'as is', if there is a trailing dot in the name.
2380a42af91cSHajimu UMEMOTO 	 */
2381a42af91cSHajimu UMEMOTO 	saved_herrno = -1;
23825342d17fSHajimu UMEMOTO 	if (dots >= res->ndots || trailing_dot) {
23835342d17fSHajimu UMEMOTO 		ret = res_querydomainN(name, NULL, target, res);
23845ecccefbSHajimu UMEMOTO 		if (ret > 0 || trailing_dot)
2385a42af91cSHajimu UMEMOTO 			return (ret);
23863a0b3b67SHajimu UMEMOTO 		if (errno == ECONNREFUSED) {
23875342d17fSHajimu UMEMOTO 			RES_SET_H_ERRNO(res, TRY_AGAIN);
23883a0b3b67SHajimu UMEMOTO 			return (-1);
23893a0b3b67SHajimu UMEMOTO 		}
23905342d17fSHajimu UMEMOTO 		switch (res->res_h_errno) {
23913a0b3b67SHajimu UMEMOTO 		case NO_DATA:
23923a0b3b67SHajimu UMEMOTO 		case HOST_NOT_FOUND:
23933a0b3b67SHajimu UMEMOTO 			break;
2394aebc6ec2SHajimu UMEMOTO 		case TRY_AGAIN:
2395aebc6ec2SHajimu UMEMOTO 			if (hp->rcode == SERVFAIL)
2396aebc6ec2SHajimu UMEMOTO 				break;
2397aebc6ec2SHajimu UMEMOTO 			/* FALLTHROUGH */
23983a0b3b67SHajimu UMEMOTO 		default:
23993a0b3b67SHajimu UMEMOTO 			return (-1);
24003a0b3b67SHajimu UMEMOTO 		}
24015342d17fSHajimu UMEMOTO 		saved_herrno = res->res_h_errno;
2402a42af91cSHajimu UMEMOTO 		tried_as_is++;
2403a42af91cSHajimu UMEMOTO 	}
2404a42af91cSHajimu UMEMOTO 
2405a42af91cSHajimu UMEMOTO 	/*
2406a42af91cSHajimu UMEMOTO 	 * We do at least one level of search if
2407a42af91cSHajimu UMEMOTO 	 *	- there is no dot and RES_DEFNAME is set, or
2408a42af91cSHajimu UMEMOTO 	 *	- there is at least one dot, there is no trailing dot,
2409a42af91cSHajimu UMEMOTO 	 *	  and RES_DNSRCH is set.
2410a42af91cSHajimu UMEMOTO 	 */
24115342d17fSHajimu UMEMOTO 	if ((!dots && (res->options & RES_DEFNAMES)) ||
24125342d17fSHajimu UMEMOTO 	    (dots && !trailing_dot && (res->options & RES_DNSRCH))) {
2413a42af91cSHajimu UMEMOTO 		int done = 0;
2414a42af91cSHajimu UMEMOTO 
24155342d17fSHajimu UMEMOTO 		for (domain = (const char * const *)res->dnsrch;
2416a42af91cSHajimu UMEMOTO 		   *domain && !done;
2417a42af91cSHajimu UMEMOTO 		   domain++) {
24185ecccefbSHajimu UMEMOTO 			searched = 1;
24195ecccefbSHajimu UMEMOTO 
24205ecccefbSHajimu UMEMOTO 			if (domain[0][0] == '\0' ||
24215ecccefbSHajimu UMEMOTO 			    (domain[0][0] == '.' && domain[0][1] == '\0'))
24225ecccefbSHajimu UMEMOTO 				root_on_list++;
24235ecccefbSHajimu UMEMOTO 
24245ecccefbSHajimu UMEMOTO 			if (root_on_list && tried_as_is)
24255ecccefbSHajimu UMEMOTO 				continue;
2426a42af91cSHajimu UMEMOTO 
24275342d17fSHajimu UMEMOTO 			ret = res_querydomainN(name, *domain, target, res);
2428a42af91cSHajimu UMEMOTO 			if (ret > 0)
2429a42af91cSHajimu UMEMOTO 				return (ret);
2430a42af91cSHajimu UMEMOTO 
2431a42af91cSHajimu UMEMOTO 			/*
2432a42af91cSHajimu UMEMOTO 			 * If no server present, give up.
2433a42af91cSHajimu UMEMOTO 			 * If name isn't found in this domain,
2434a42af91cSHajimu UMEMOTO 			 * keep trying higher domains in the search list
2435a42af91cSHajimu UMEMOTO 			 * (if that's enabled).
2436a42af91cSHajimu UMEMOTO 			 * On a NO_DATA error, keep trying, otherwise
2437a42af91cSHajimu UMEMOTO 			 * a wildcard entry of another type could keep us
2438a42af91cSHajimu UMEMOTO 			 * from finding this entry higher in the domain.
2439a42af91cSHajimu UMEMOTO 			 * If we get some other error (negative answer or
2440a42af91cSHajimu UMEMOTO 			 * server failure), then stop searching up,
2441a42af91cSHajimu UMEMOTO 			 * but try the input name below in case it's
2442a42af91cSHajimu UMEMOTO 			 * fully-qualified.
2443a42af91cSHajimu UMEMOTO 			 */
2444a42af91cSHajimu UMEMOTO 			if (errno == ECONNREFUSED) {
24455342d17fSHajimu UMEMOTO 				RES_SET_H_ERRNO(res, TRY_AGAIN);
2446a42af91cSHajimu UMEMOTO 				return (-1);
2447a42af91cSHajimu UMEMOTO 			}
2448a42af91cSHajimu UMEMOTO 
24495342d17fSHajimu UMEMOTO 			switch (res->res_h_errno) {
2450a42af91cSHajimu UMEMOTO 			case NO_DATA:
2451a42af91cSHajimu UMEMOTO 				got_nodata++;
2452a42af91cSHajimu UMEMOTO 				/* FALLTHROUGH */
2453a42af91cSHajimu UMEMOTO 			case HOST_NOT_FOUND:
2454a42af91cSHajimu UMEMOTO 				/* keep trying */
2455a42af91cSHajimu UMEMOTO 				break;
2456a42af91cSHajimu UMEMOTO 			case TRY_AGAIN:
2457aebc6ec2SHajimu UMEMOTO 				got_servfail++;
2458a42af91cSHajimu UMEMOTO 				if (hp->rcode == SERVFAIL) {
2459a42af91cSHajimu UMEMOTO 					/* try next search element, if any */
2460a42af91cSHajimu UMEMOTO 					break;
2461a42af91cSHajimu UMEMOTO 				}
2462a42af91cSHajimu UMEMOTO 				/* FALLTHROUGH */
2463a42af91cSHajimu UMEMOTO 			default:
2464a42af91cSHajimu UMEMOTO 				/* anything else implies that we're done */
2465a42af91cSHajimu UMEMOTO 				done++;
2466a42af91cSHajimu UMEMOTO 			}
2467a42af91cSHajimu UMEMOTO 			/*
2468a42af91cSHajimu UMEMOTO 			 * if we got here for some reason other than DNSRCH,
2469a42af91cSHajimu UMEMOTO 			 * we only wanted one iteration of the loop, so stop.
2470a42af91cSHajimu UMEMOTO 			 */
24715342d17fSHajimu UMEMOTO 			if (!(res->options & RES_DNSRCH))
2472a42af91cSHajimu UMEMOTO 			        done++;
2473a42af91cSHajimu UMEMOTO 		}
2474a42af91cSHajimu UMEMOTO 	}
2475a42af91cSHajimu UMEMOTO 
24765342d17fSHajimu UMEMOTO 	switch (res->res_h_errno) {
24773a0b3b67SHajimu UMEMOTO 	case NO_DATA:
24783a0b3b67SHajimu UMEMOTO 	case HOST_NOT_FOUND:
24793a0b3b67SHajimu UMEMOTO 		break;
2480aebc6ec2SHajimu UMEMOTO 	case TRY_AGAIN:
2481aebc6ec2SHajimu UMEMOTO 		if (hp->rcode == SERVFAIL)
2482aebc6ec2SHajimu UMEMOTO 			break;
2483aebc6ec2SHajimu UMEMOTO 		/* FALLTHROUGH */
24843a0b3b67SHajimu UMEMOTO 	default:
24853a0b3b67SHajimu UMEMOTO 		goto giveup;
24863a0b3b67SHajimu UMEMOTO 	}
24873a0b3b67SHajimu UMEMOTO 
2488a42af91cSHajimu UMEMOTO 	/*
24895ecccefbSHajimu UMEMOTO 	 * If the query has not already been tried as is then try it
24905ecccefbSHajimu UMEMOTO 	 * unless RES_NOTLDQUERY is set and there were no dots.
2491a42af91cSHajimu UMEMOTO 	 */
24925342d17fSHajimu UMEMOTO 	if ((dots || !searched || !(res->options & RES_NOTLDQUERY)) &&
24935ecccefbSHajimu UMEMOTO 	    !(tried_as_is || root_on_list)) {
24945342d17fSHajimu UMEMOTO 		ret = res_querydomainN(name, NULL, target, res);
2495a42af91cSHajimu UMEMOTO 		if (ret > 0)
2496a42af91cSHajimu UMEMOTO 			return (ret);
2497a42af91cSHajimu UMEMOTO 	}
2498a42af91cSHajimu UMEMOTO 
2499a42af91cSHajimu UMEMOTO 	/*
2500a42af91cSHajimu UMEMOTO 	 * if we got here, we didn't satisfy the search.
2501a42af91cSHajimu UMEMOTO 	 * if we did an initial full query, return that query's h_errno
2502a42af91cSHajimu UMEMOTO 	 * (note that we wouldn't be here if that query had succeeded).
2503a42af91cSHajimu UMEMOTO 	 * else if we ever got a nodata, send that back as the reason.
2504a42af91cSHajimu UMEMOTO 	 * else send back meaningless h_errno, that being the one from
2505a42af91cSHajimu UMEMOTO 	 * the last DNSRCH we did.
2506a42af91cSHajimu UMEMOTO 	 */
25073a0b3b67SHajimu UMEMOTO giveup:
2508a42af91cSHajimu UMEMOTO 	if (saved_herrno != -1)
25095342d17fSHajimu UMEMOTO 		RES_SET_H_ERRNO(res, saved_herrno);
2510a42af91cSHajimu UMEMOTO 	else if (got_nodata)
25115342d17fSHajimu UMEMOTO 		RES_SET_H_ERRNO(res, NO_DATA);
2512a42af91cSHajimu UMEMOTO 	else if (got_servfail)
25135342d17fSHajimu UMEMOTO 		RES_SET_H_ERRNO(res, TRY_AGAIN);
2514a42af91cSHajimu UMEMOTO 	return (-1);
2515a42af91cSHajimu UMEMOTO }
2516a42af91cSHajimu UMEMOTO 
2517a42af91cSHajimu UMEMOTO /*
2518a42af91cSHajimu UMEMOTO  * Perform a call on res_query on the concatenation of name and domain,
2519a42af91cSHajimu UMEMOTO  * removing a trailing dot from name if domain is NULL.
2520a42af91cSHajimu UMEMOTO  */
2521a42af91cSHajimu UMEMOTO static int
252290d1d883SHajimu UMEMOTO res_querydomainN(const char *name, const char *domain,
252390d1d883SHajimu UMEMOTO     struct res_target *target, res_state res)
2524a42af91cSHajimu UMEMOTO {
2525a42af91cSHajimu UMEMOTO 	char nbuf[MAXDNAME];
2526a42af91cSHajimu UMEMOTO 	const char *longname = nbuf;
2527a42af91cSHajimu UMEMOTO 	size_t n, d;
2528a42af91cSHajimu UMEMOTO 
2529a42af91cSHajimu UMEMOTO #ifdef DEBUG
25305342d17fSHajimu UMEMOTO 	if (res->options & RES_DEBUG)
2531a42af91cSHajimu UMEMOTO 		printf(";; res_querydomain(%s, %s)\n",
2532a42af91cSHajimu UMEMOTO 			name, domain?domain:"<Nil>");
2533a42af91cSHajimu UMEMOTO #endif
2534a42af91cSHajimu UMEMOTO 	if (domain == NULL) {
2535a42af91cSHajimu UMEMOTO 		/*
2536a42af91cSHajimu UMEMOTO 		 * Check for trailing '.';
2537a42af91cSHajimu UMEMOTO 		 * copy without '.' if present.
2538a42af91cSHajimu UMEMOTO 		 */
2539a42af91cSHajimu UMEMOTO 		n = strlen(name);
2540a42af91cSHajimu UMEMOTO 		if (n >= MAXDNAME) {
25415342d17fSHajimu UMEMOTO 			RES_SET_H_ERRNO(res, NO_RECOVERY);
2542a42af91cSHajimu UMEMOTO 			return (-1);
2543a42af91cSHajimu UMEMOTO 		}
2544a42af91cSHajimu UMEMOTO 		if (n > 0 && name[--n] == '.') {
2545a42af91cSHajimu UMEMOTO 			strncpy(nbuf, name, n);
2546a42af91cSHajimu UMEMOTO 			nbuf[n] = '\0';
2547a42af91cSHajimu UMEMOTO 		} else
2548a42af91cSHajimu UMEMOTO 			longname = name;
2549a42af91cSHajimu UMEMOTO 	} else {
2550a42af91cSHajimu UMEMOTO 		n = strlen(name);
2551a42af91cSHajimu UMEMOTO 		d = strlen(domain);
2552a42af91cSHajimu UMEMOTO 		if (n + d + 1 >= MAXDNAME) {
25535342d17fSHajimu UMEMOTO 			RES_SET_H_ERRNO(res, NO_RECOVERY);
2554a42af91cSHajimu UMEMOTO 			return (-1);
2555a42af91cSHajimu UMEMOTO 		}
2556dd521ef1SHajimu UMEMOTO 		snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
2557a42af91cSHajimu UMEMOTO 	}
25585342d17fSHajimu UMEMOTO 	return (res_queryN(longname, target, res));
2559a42af91cSHajimu UMEMOTO }
2560