xref: /freebsd/lib/libc/net/getaddrinfo.c (revision fd57e549e0b1ba3e6294d6b6fabfd2d3cac7d4da)
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;
1387d56d374SYoshinobu Inoue 	int 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,
2681372519bSDavid E. O'Brien 	const struct addrinfo *);
269688a5c3eSHajimu UMEMOTO #if defined(RESOLVSORT)
270688a5c3eSHajimu UMEMOTO static int addr4sort(struct addrinfo *);
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 
283c05ac53bSDavid E. O'Brien static int res_queryN(const char *, struct res_target *);
284c05ac53bSDavid E. O'Brien static int res_searchN(const char *, struct res_target *);
2851372519bSDavid E. O'Brien static int res_querydomainN(const char *, const char *,
2861372519bSDavid E. O'Brien 	struct res_target *);
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
3307d56d374SYoshinobu Inoue freeaddrinfo(ai)
3317d56d374SYoshinobu Inoue 	struct addrinfo *ai;
3327d56d374SYoshinobu Inoue {
3337d56d374SYoshinobu Inoue 	struct addrinfo *next;
3347d56d374SYoshinobu Inoue 
3357d56d374SYoshinobu Inoue 	do {
3367d56d374SYoshinobu Inoue 		next = ai->ai_next;
3377d56d374SYoshinobu Inoue 		if (ai->ai_canonname)
3387d56d374SYoshinobu Inoue 			free(ai->ai_canonname);
3397d56d374SYoshinobu Inoue 		/* no need to free(ai->ai_addr) */
3407d56d374SYoshinobu Inoue 		free(ai);
341a42af91cSHajimu UMEMOTO 		ai = next;
342a42af91cSHajimu UMEMOTO 	} while (ai);
3437d56d374SYoshinobu Inoue }
3447d56d374SYoshinobu Inoue 
3457d56d374SYoshinobu Inoue static int
346c224435eSHajimu UMEMOTO str2number(p)
3477d56d374SYoshinobu Inoue 	const char *p;
3487d56d374SYoshinobu Inoue {
349ec20fe00SJun-ichiro itojun Hagino 	char *ep;
350c224435eSHajimu UMEMOTO 	unsigned long v;
351ec20fe00SJun-ichiro itojun Hagino 
352ec20fe00SJun-ichiro itojun Hagino 	if (*p == '\0')
353c224435eSHajimu UMEMOTO 		return -1;
354ec20fe00SJun-ichiro itojun Hagino 	ep = NULL;
355a89c1d30SHajimu UMEMOTO 	errno = 0;
356c224435eSHajimu UMEMOTO 	v = strtoul(p, &ep, 10);
357c224435eSHajimu UMEMOTO 	if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX)
358c224435eSHajimu UMEMOTO 		return v;
359ec20fe00SJun-ichiro itojun Hagino 	else
360c224435eSHajimu UMEMOTO 		return -1;
3617d56d374SYoshinobu Inoue }
3627d56d374SYoshinobu Inoue 
3637d56d374SYoshinobu Inoue int
3647d56d374SYoshinobu Inoue getaddrinfo(hostname, servname, hints, res)
3657d56d374SYoshinobu Inoue 	const char *hostname, *servname;
3667d56d374SYoshinobu Inoue 	const struct addrinfo *hints;
3677d56d374SYoshinobu Inoue 	struct addrinfo **res;
3687d56d374SYoshinobu Inoue {
3697d56d374SYoshinobu Inoue 	struct addrinfo sentinel;
3707d56d374SYoshinobu Inoue 	struct addrinfo *cur;
3717d56d374SYoshinobu Inoue 	int error = 0;
372bf838688SHajimu UMEMOTO 	struct addrinfo ai;
373bf838688SHajimu UMEMOTO 	struct addrinfo ai0;
3747d56d374SYoshinobu Inoue 	struct addrinfo *pai;
3757d56d374SYoshinobu Inoue 	const struct explore *ex;
3764c6867a8SHajimu UMEMOTO 	int numeric = 0;
3777d56d374SYoshinobu Inoue 
378a42af91cSHajimu UMEMOTO 	memset(&sentinel, 0, sizeof(sentinel));
3797d56d374SYoshinobu Inoue 	cur = &sentinel;
3807d56d374SYoshinobu Inoue 	pai = &ai;
3817d56d374SYoshinobu Inoue 	pai->ai_flags = 0;
3827d56d374SYoshinobu Inoue 	pai->ai_family = PF_UNSPEC;
3837d56d374SYoshinobu Inoue 	pai->ai_socktype = ANY;
3847d56d374SYoshinobu Inoue 	pai->ai_protocol = ANY;
3857d56d374SYoshinobu Inoue 	pai->ai_addrlen = 0;
3867d56d374SYoshinobu Inoue 	pai->ai_canonname = NULL;
3877d56d374SYoshinobu Inoue 	pai->ai_addr = NULL;
3887d56d374SYoshinobu Inoue 	pai->ai_next = NULL;
3897d56d374SYoshinobu Inoue 
3907d56d374SYoshinobu Inoue 	if (hostname == NULL && servname == NULL)
3917d56d374SYoshinobu Inoue 		return EAI_NONAME;
3927d56d374SYoshinobu Inoue 	if (hints) {
3937d56d374SYoshinobu Inoue 		/* error check for hints */
3947d56d374SYoshinobu Inoue 		if (hints->ai_addrlen || hints->ai_canonname ||
3957d56d374SYoshinobu Inoue 		    hints->ai_addr || hints->ai_next)
3967d56d374SYoshinobu Inoue 			ERR(EAI_BADHINTS); /* xxx */
3977d56d374SYoshinobu Inoue 		if (hints->ai_flags & ~AI_MASK)
3987d56d374SYoshinobu Inoue 			ERR(EAI_BADFLAGS);
3997d56d374SYoshinobu Inoue 		switch (hints->ai_family) {
4007d56d374SYoshinobu Inoue 		case PF_UNSPEC:
4017d56d374SYoshinobu Inoue 		case PF_INET:
4027d56d374SYoshinobu Inoue #ifdef INET6
4037d56d374SYoshinobu Inoue 		case PF_INET6:
4047d56d374SYoshinobu Inoue #endif
4057d56d374SYoshinobu Inoue 			break;
4067d56d374SYoshinobu Inoue 		default:
4077d56d374SYoshinobu Inoue 			ERR(EAI_FAMILY);
4087d56d374SYoshinobu Inoue 		}
4097d56d374SYoshinobu Inoue 		memcpy(pai, hints, sizeof(*pai));
4107d56d374SYoshinobu Inoue 
4117d56d374SYoshinobu Inoue 		/*
4127d56d374SYoshinobu Inoue 		 * if both socktype/protocol are specified, check if they
4137d56d374SYoshinobu Inoue 		 * are meaningful combination.
4147d56d374SYoshinobu Inoue 		 */
4157d56d374SYoshinobu Inoue 		if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
416a42af91cSHajimu UMEMOTO 			for (ex = explore; ex->e_af >= 0; ex++) {
417bf838688SHajimu UMEMOTO 				if (pai->ai_family != ex->e_af)
418a42af91cSHajimu UMEMOTO 					continue;
419bf838688SHajimu UMEMOTO 				if (ex->e_socktype == ANY)
4207d56d374SYoshinobu Inoue 					continue;
421bf838688SHajimu UMEMOTO 				if (ex->e_protocol == ANY)
4227d56d374SYoshinobu Inoue 					continue;
423bf838688SHajimu UMEMOTO 				if (pai->ai_socktype == ex->e_socktype &&
424bf838688SHajimu UMEMOTO 				    pai->ai_protocol != ex->e_protocol) {
4257d56d374SYoshinobu Inoue 					ERR(EAI_BADHINTS);
4267d56d374SYoshinobu Inoue 				}
4277d56d374SYoshinobu Inoue 			}
4287d56d374SYoshinobu Inoue 		}
429bf838688SHajimu UMEMOTO 	}
4307d56d374SYoshinobu Inoue 
4317d56d374SYoshinobu Inoue 	/*
432a42af91cSHajimu UMEMOTO 	 * post-2553: AI_ALL and AI_V4MAPPED are effective only against
433dd521ef1SHajimu UMEMOTO 	 * AF_INET6 query.  They need to be ignored if specified in other
434a42af91cSHajimu UMEMOTO 	 * occassions.
435a42af91cSHajimu UMEMOTO 	 */
436a42af91cSHajimu UMEMOTO 	switch (pai->ai_flags & (AI_ALL | AI_V4MAPPED)) {
437a42af91cSHajimu UMEMOTO 	case AI_V4MAPPED:
438a42af91cSHajimu UMEMOTO 	case AI_ALL | AI_V4MAPPED:
439a42af91cSHajimu UMEMOTO 		if (pai->ai_family != AF_INET6)
440a42af91cSHajimu UMEMOTO 			pai->ai_flags &= ~(AI_ALL | AI_V4MAPPED);
441a42af91cSHajimu UMEMOTO 		break;
442a42af91cSHajimu UMEMOTO 	case AI_ALL:
443a42af91cSHajimu UMEMOTO #if 1
444a42af91cSHajimu UMEMOTO 		/* illegal */
445a42af91cSHajimu UMEMOTO 		ERR(EAI_BADFLAGS);
446a42af91cSHajimu UMEMOTO #else
447a42af91cSHajimu UMEMOTO 		pai->ai_flags &= ~(AI_ALL | AI_V4MAPPED);
448a42af91cSHajimu UMEMOTO #endif
449a42af91cSHajimu UMEMOTO 		break;
450a42af91cSHajimu UMEMOTO 	}
451a42af91cSHajimu UMEMOTO 
452a42af91cSHajimu UMEMOTO 	/*
453a42af91cSHajimu UMEMOTO 	 * check for special cases.  (1) numeric servname is disallowed if
454a42af91cSHajimu UMEMOTO 	 * socktype/protocol are left unspecified. (2) servname is disallowed
455a42af91cSHajimu UMEMOTO 	 * for raw and other inet{,6} sockets.
4567d56d374SYoshinobu Inoue 	 */
4577d56d374SYoshinobu Inoue 	if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
458a42af91cSHajimu UMEMOTO #ifdef PF_INET6
4597d56d374SYoshinobu Inoue 	    || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)
4607d56d374SYoshinobu Inoue #endif
4617d56d374SYoshinobu Inoue 	    ) {
462a42af91cSHajimu UMEMOTO 		ai0 = *pai;	/* backup *pai */
4637d56d374SYoshinobu Inoue 
464a42af91cSHajimu UMEMOTO 		if (pai->ai_family == PF_UNSPEC) {
465a42af91cSHajimu UMEMOTO #ifdef PF_INET6
4667d56d374SYoshinobu Inoue 			pai->ai_family = PF_INET6;
4677d56d374SYoshinobu Inoue #else
4687d56d374SYoshinobu Inoue 			pai->ai_family = PF_INET;
4697d56d374SYoshinobu Inoue #endif
470a42af91cSHajimu UMEMOTO 		}
4717d56d374SYoshinobu Inoue 		error = get_portmatch(pai, servname);
4727d56d374SYoshinobu Inoue 		if (error)
4737d56d374SYoshinobu Inoue 			ERR(error);
474a42af91cSHajimu UMEMOTO 
475a42af91cSHajimu UMEMOTO 		*pai = ai0;
4767d56d374SYoshinobu Inoue 	}
4777d56d374SYoshinobu Inoue 
478a42af91cSHajimu UMEMOTO 	ai0 = *pai;
479a42af91cSHajimu UMEMOTO 
480bf838688SHajimu UMEMOTO 	/* NULL hostname, or numeric hostname */
481a42af91cSHajimu UMEMOTO 	for (ex = explore; ex->e_af >= 0; ex++) {
4827d56d374SYoshinobu Inoue 		*pai = ai0;
4837d56d374SYoshinobu Inoue 
484bf838688SHajimu UMEMOTO 		/* PF_UNSPEC entries are prepared for DNS queries only */
485bf838688SHajimu UMEMOTO 		if (ex->e_af == PF_UNSPEC)
486bf838688SHajimu UMEMOTO 			continue;
487a42af91cSHajimu UMEMOTO 
488a42af91cSHajimu UMEMOTO 		if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex)))
4897d56d374SYoshinobu Inoue 			continue;
490248aee62SJacques Vidrine 		if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex)))
491a42af91cSHajimu UMEMOTO 			continue;
492248aee62SJacques Vidrine 		if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex)))
4937d56d374SYoshinobu Inoue 			continue;
4947d56d374SYoshinobu Inoue 
4957d56d374SYoshinobu Inoue 		if (pai->ai_family == PF_UNSPEC)
496a42af91cSHajimu UMEMOTO 			pai->ai_family = ex->e_af;
4977d56d374SYoshinobu Inoue 		if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
4987d56d374SYoshinobu Inoue 			pai->ai_socktype = ex->e_socktype;
4997d56d374SYoshinobu Inoue 		if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
5007d56d374SYoshinobu Inoue 			pai->ai_protocol = ex->e_protocol;
5017d56d374SYoshinobu Inoue 
502bf838688SHajimu UMEMOTO 		if (hostname == NULL)
503bf838688SHajimu UMEMOTO 			error = explore_null(pai, servname, &cur->ai_next);
504bf838688SHajimu UMEMOTO 		else
5058755b918SHajimu UMEMOTO 			error = explore_numeric_scope(pai, hostname, servname,
5068755b918SHajimu UMEMOTO 			    &cur->ai_next);
5077d56d374SYoshinobu Inoue 
508bf838688SHajimu UMEMOTO 		if (error)
509bf838688SHajimu UMEMOTO 			goto free;
5107d56d374SYoshinobu Inoue 
5117d56d374SYoshinobu Inoue 		while (cur && cur->ai_next)
5127d56d374SYoshinobu Inoue 			cur = cur->ai_next;
5137d56d374SYoshinobu Inoue 	}
5147d56d374SYoshinobu Inoue 
515bf838688SHajimu UMEMOTO 	/*
516bf838688SHajimu UMEMOTO 	 * XXX
517bf838688SHajimu UMEMOTO 	 * If numreic representation of AF1 can be interpreted as FQDN
518bf838688SHajimu UMEMOTO 	 * representation of AF2, we need to think again about the code below.
519bf838688SHajimu UMEMOTO 	 */
5204c6867a8SHajimu UMEMOTO 	if (sentinel.ai_next) {
5214c6867a8SHajimu UMEMOTO 		numeric = 1;
522bf838688SHajimu UMEMOTO 		goto good;
5234c6867a8SHajimu UMEMOTO 	}
524bf838688SHajimu UMEMOTO 
525bf838688SHajimu UMEMOTO 	if (hostname == NULL)
526bf838688SHajimu UMEMOTO 		ERR(EAI_NONAME);	/* used to be EAI_NODATA */
527bf838688SHajimu UMEMOTO 	if (pai->ai_flags & AI_NUMERICHOST)
528bf838688SHajimu UMEMOTO 		ERR(EAI_NONAME);
529bf838688SHajimu UMEMOTO 
530bf838688SHajimu UMEMOTO 	if ((pai->ai_flags & AI_ADDRCONFIG) != 0 && !addrconfig(&ai0))
531bf838688SHajimu UMEMOTO 		ERR(EAI_FAIL);
532a42af91cSHajimu UMEMOTO 
533f95d4633SHajimu UMEMOTO 	/*
534bf838688SHajimu UMEMOTO 	 * hostname as alphabetical name.
535bf838688SHajimu UMEMOTO 	 * we would like to prefer AF_INET6 than AF_INET, so we'll make a
536bf838688SHajimu UMEMOTO 	 * outer loop by AFs.
537f95d4633SHajimu UMEMOTO 	 */
538bf838688SHajimu UMEMOTO 	for (ex = explore; ex->e_af >= 0; ex++) {
539bf838688SHajimu UMEMOTO 		*pai = ai0;
540f95d4633SHajimu UMEMOTO 
541bf838688SHajimu UMEMOTO 		/* require exact match for family field */
542bf838688SHajimu UMEMOTO 		if (pai->ai_family != ex->e_af)
543f95d4633SHajimu UMEMOTO 			continue;
544f95d4633SHajimu UMEMOTO 
545bf838688SHajimu UMEMOTO 		if (!MATCH(pai->ai_socktype, ex->e_socktype,
546bf838688SHajimu UMEMOTO 				WILD_SOCKTYPE(ex))) {
547bf838688SHajimu UMEMOTO 			continue;
548bf838688SHajimu UMEMOTO 		}
549bf838688SHajimu UMEMOTO 		if (!MATCH(pai->ai_protocol, ex->e_protocol,
550bf838688SHajimu UMEMOTO 				WILD_PROTOCOL(ex))) {
551bf838688SHajimu UMEMOTO 			continue;
552f95d4633SHajimu UMEMOTO 		}
553f95d4633SHajimu UMEMOTO 
554bf838688SHajimu UMEMOTO 		if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
555bf838688SHajimu UMEMOTO 			pai->ai_socktype = ex->e_socktype;
556bf838688SHajimu UMEMOTO 		if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
557bf838688SHajimu UMEMOTO 			pai->ai_protocol = ex->e_protocol;
558bf838688SHajimu UMEMOTO 
559bf838688SHajimu UMEMOTO 		error = explore_fqdn(pai, hostname, servname,
560bf838688SHajimu UMEMOTO 			&cur->ai_next);
561bf838688SHajimu UMEMOTO 
562bf838688SHajimu UMEMOTO 		while (cur && cur->ai_next)
563f95d4633SHajimu UMEMOTO 			cur = cur->ai_next;
564f95d4633SHajimu UMEMOTO 	}
565f95d4633SHajimu UMEMOTO 
5664c6867a8SHajimu UMEMOTO 	/* XXX inhibit errors if we have the result */
567bf838688SHajimu UMEMOTO 	if (sentinel.ai_next)
568bf838688SHajimu UMEMOTO 		error = 0;
569f95d4633SHajimu UMEMOTO 
5704c6867a8SHajimu UMEMOTO good:
5714c6867a8SHajimu UMEMOTO 	/*
5724c6867a8SHajimu UMEMOTO 	 * ensure we return either:
5734c6867a8SHajimu UMEMOTO 	 * - error == 0, non-NULL *res
5744c6867a8SHajimu UMEMOTO 	 * - error != 0, NULL *res
5754c6867a8SHajimu UMEMOTO 	 */
576bf838688SHajimu UMEMOTO 	if (error == 0) {
577bf838688SHajimu UMEMOTO 		if (sentinel.ai_next) {
5784c6867a8SHajimu UMEMOTO 			/*
5794c6867a8SHajimu UMEMOTO 			 * If the returned entry is for an active connection,
5804c6867a8SHajimu UMEMOTO 			 * and the given name is not numeric, reorder the
5814c6867a8SHajimu UMEMOTO 			 * list, so that the application would try the list
5824c6867a8SHajimu UMEMOTO 			 * in the most efficient order.
5834c6867a8SHajimu UMEMOTO 			 */
5844c6867a8SHajimu UMEMOTO 			if (hints == NULL || !(hints->ai_flags & AI_PASSIVE)) {
5854c6867a8SHajimu UMEMOTO 				if (!numeric)
5864c6867a8SHajimu UMEMOTO 					(void)reorder(&sentinel);
5874c6867a8SHajimu UMEMOTO 			}
588bf838688SHajimu UMEMOTO 			*res = sentinel.ai_next;
589bf838688SHajimu UMEMOTO 			return SUCCESS;
590bf838688SHajimu UMEMOTO 		} else
591bf838688SHajimu UMEMOTO 			error = EAI_FAIL;
592bf838688SHajimu UMEMOTO 	}
593bf838688SHajimu UMEMOTO free:
594bf838688SHajimu UMEMOTO bad:
595bf838688SHajimu UMEMOTO 	if (sentinel.ai_next)
596f95d4633SHajimu UMEMOTO 		freeaddrinfo(sentinel.ai_next);
597bf838688SHajimu UMEMOTO 	*res = NULL;
5987d56d374SYoshinobu Inoue 	return error;
5997d56d374SYoshinobu Inoue }
6007d56d374SYoshinobu Inoue 
6014c6867a8SHajimu UMEMOTO static int
6024c6867a8SHajimu UMEMOTO reorder(sentinel)
6034c6867a8SHajimu UMEMOTO 	struct addrinfo *sentinel;
6044c6867a8SHajimu UMEMOTO {
6054c6867a8SHajimu UMEMOTO 	struct addrinfo *ai, **aip;
6064c6867a8SHajimu UMEMOTO 	struct ai_order *aio;
6074c6867a8SHajimu UMEMOTO 	int i, n;
6084c6867a8SHajimu UMEMOTO 	struct policyhead policyhead;
6094c6867a8SHajimu UMEMOTO 
6104c6867a8SHajimu UMEMOTO 	/* count the number of addrinfo elements for sorting. */
6114c6867a8SHajimu UMEMOTO 	for (n = 0, ai = sentinel->ai_next; ai != NULL; ai = ai->ai_next, n++)
6124c6867a8SHajimu UMEMOTO 		;
6134c6867a8SHajimu UMEMOTO 
6144c6867a8SHajimu UMEMOTO 	/*
6154c6867a8SHajimu UMEMOTO 	 * If the number is small enough, we can skip the reordering process.
6164c6867a8SHajimu UMEMOTO 	 */
6174c6867a8SHajimu UMEMOTO 	if (n <= 1)
6184c6867a8SHajimu UMEMOTO 		return(n);
6194c6867a8SHajimu UMEMOTO 
6204c6867a8SHajimu UMEMOTO 	/* allocate a temporary array for sort and initialization of it. */
6214c6867a8SHajimu UMEMOTO 	if ((aio = malloc(sizeof(*aio) * n)) == NULL)
6224c6867a8SHajimu UMEMOTO 		return(n);	/* give up reordering */
6234c6867a8SHajimu UMEMOTO 	memset(aio, 0, sizeof(*aio) * n);
6244c6867a8SHajimu UMEMOTO 
6254c6867a8SHajimu UMEMOTO 	/* retrieve address selection policy from the kernel */
6264c6867a8SHajimu UMEMOTO 	TAILQ_INIT(&policyhead);
6274c6867a8SHajimu UMEMOTO 	if (!get_addrselectpolicy(&policyhead)) {
6284c6867a8SHajimu UMEMOTO 		/* no policy is installed into kernel, we don't sort. */
6294c6867a8SHajimu UMEMOTO 		free(aio);
6304c6867a8SHajimu UMEMOTO 		return (n);
6314c6867a8SHajimu UMEMOTO 	}
6324c6867a8SHajimu UMEMOTO 
6334c6867a8SHajimu UMEMOTO 	for (i = 0, ai = sentinel->ai_next; i < n; ai = ai->ai_next, i++) {
6344c6867a8SHajimu UMEMOTO 		aio[i].aio_ai = ai;
6354c6867a8SHajimu UMEMOTO 		aio[i].aio_dstscope = gai_addr2scopetype(ai->ai_addr);
6364c6867a8SHajimu UMEMOTO 		aio[i].aio_dstpolicy = match_addrselectpolicy(ai->ai_addr,
6374c6867a8SHajimu UMEMOTO 							      &policyhead);
6384d489f47SHajimu UMEMOTO 		set_source(&aio[i], &policyhead);
6394c6867a8SHajimu UMEMOTO 	}
6404c6867a8SHajimu UMEMOTO 
6414c6867a8SHajimu UMEMOTO 	/* perform sorting. */
6424c6867a8SHajimu UMEMOTO 	qsort(aio, n, sizeof(*aio), comp_dst);
6434c6867a8SHajimu UMEMOTO 
6444c6867a8SHajimu UMEMOTO 	/* reorder the addrinfo chain. */
6454c6867a8SHajimu UMEMOTO 	for (i = 0, aip = &sentinel->ai_next; i < n; i++) {
6464c6867a8SHajimu UMEMOTO 		*aip = aio[i].aio_ai;
6474c6867a8SHajimu UMEMOTO 		aip = &aio[i].aio_ai->ai_next;
6484c6867a8SHajimu UMEMOTO 	}
6494c6867a8SHajimu UMEMOTO 	*aip = NULL;
6504c6867a8SHajimu UMEMOTO 
6514c6867a8SHajimu UMEMOTO 	/* cleanup and return */
6524c6867a8SHajimu UMEMOTO 	free(aio);
6534c6867a8SHajimu UMEMOTO 	free_addrselectpolicy(&policyhead);
6544c6867a8SHajimu UMEMOTO 	return(n);
6554c6867a8SHajimu UMEMOTO }
6564c6867a8SHajimu UMEMOTO 
6574c6867a8SHajimu UMEMOTO static int
6584c6867a8SHajimu UMEMOTO get_addrselectpolicy(head)
6594c6867a8SHajimu UMEMOTO 	struct policyhead *head;
6604c6867a8SHajimu UMEMOTO {
6614c6867a8SHajimu UMEMOTO #ifdef INET6
6624c6867a8SHajimu UMEMOTO 	int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_ADDRCTLPOLICY };
6634c6867a8SHajimu UMEMOTO 	size_t l;
6644c6867a8SHajimu UMEMOTO 	char *buf;
6654c6867a8SHajimu UMEMOTO 	struct in6_addrpolicy *pol, *ep;
6664c6867a8SHajimu UMEMOTO 
6674c6867a8SHajimu UMEMOTO 	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0)
6684c6867a8SHajimu UMEMOTO 		return (0);
6694c6867a8SHajimu UMEMOTO 	if ((buf = malloc(l)) == NULL)
6704c6867a8SHajimu UMEMOTO 		return (0);
6714c6867a8SHajimu UMEMOTO 	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
6724c6867a8SHajimu UMEMOTO 		free(buf);
6734c6867a8SHajimu UMEMOTO 		return (0);
6744c6867a8SHajimu UMEMOTO 	}
6754c6867a8SHajimu UMEMOTO 
6764c6867a8SHajimu UMEMOTO 	ep = (struct in6_addrpolicy *)(buf + l);
6774c6867a8SHajimu UMEMOTO 	for (pol = (struct in6_addrpolicy *)buf; pol + 1 <= ep; pol++) {
6784c6867a8SHajimu UMEMOTO 		struct policyqueue *new;
6794c6867a8SHajimu UMEMOTO 
6804c6867a8SHajimu UMEMOTO 		if ((new = malloc(sizeof(*new))) == NULL) {
6814c6867a8SHajimu UMEMOTO 			free_addrselectpolicy(head); /* make the list empty */
6824c6867a8SHajimu UMEMOTO 			break;
6834c6867a8SHajimu UMEMOTO 		}
6844c6867a8SHajimu UMEMOTO 		new->pc_policy = *pol;
6854c6867a8SHajimu UMEMOTO 		TAILQ_INSERT_TAIL(head, new, pc_entry);
6864c6867a8SHajimu UMEMOTO 	}
6874c6867a8SHajimu UMEMOTO 
6884c6867a8SHajimu UMEMOTO 	free(buf);
6894c6867a8SHajimu UMEMOTO 	return (1);
6904c6867a8SHajimu UMEMOTO #else
6914c6867a8SHajimu UMEMOTO 	return (0);
6924c6867a8SHajimu UMEMOTO #endif
6934c6867a8SHajimu UMEMOTO }
6944c6867a8SHajimu UMEMOTO 
6954c6867a8SHajimu UMEMOTO static void
6964c6867a8SHajimu UMEMOTO free_addrselectpolicy(head)
6974c6867a8SHajimu UMEMOTO 	struct policyhead *head;
6984c6867a8SHajimu UMEMOTO {
6994c6867a8SHajimu UMEMOTO 	struct policyqueue *ent, *nent;
7004c6867a8SHajimu UMEMOTO 
7014c6867a8SHajimu UMEMOTO 	for (ent = TAILQ_FIRST(head); ent; ent = nent) {
7024c6867a8SHajimu UMEMOTO 		nent = TAILQ_NEXT(ent, pc_entry);
7034c6867a8SHajimu UMEMOTO 		TAILQ_REMOVE(head, ent, pc_entry);
7044c6867a8SHajimu UMEMOTO 		free(ent);
7054c6867a8SHajimu UMEMOTO 	}
7064c6867a8SHajimu UMEMOTO }
7074c6867a8SHajimu UMEMOTO 
7084c6867a8SHajimu UMEMOTO static struct policyqueue *
7094c6867a8SHajimu UMEMOTO match_addrselectpolicy(addr, head)
7104c6867a8SHajimu UMEMOTO 	struct sockaddr *addr;
7114c6867a8SHajimu UMEMOTO 	struct policyhead *head;
7124c6867a8SHajimu UMEMOTO {
7134c6867a8SHajimu UMEMOTO #ifdef INET6
7144c6867a8SHajimu UMEMOTO 	struct policyqueue *ent, *bestent = NULL;
7154c6867a8SHajimu UMEMOTO 	struct in6_addrpolicy *pol;
7164c6867a8SHajimu UMEMOTO 	int matchlen, bestmatchlen = -1;
7174c6867a8SHajimu UMEMOTO 	u_char *mp, *ep, *k, *p, m;
7184c6867a8SHajimu UMEMOTO 	struct sockaddr_in6 key;
7194c6867a8SHajimu UMEMOTO 
7204c6867a8SHajimu UMEMOTO 	switch(addr->sa_family) {
7214c6867a8SHajimu UMEMOTO 	case AF_INET6:
7224c6867a8SHajimu UMEMOTO 		key = *(struct sockaddr_in6 *)addr;
7234c6867a8SHajimu UMEMOTO 		break;
7244c6867a8SHajimu UMEMOTO 	case AF_INET:
7254c6867a8SHajimu UMEMOTO 		/* convert the address into IPv4-mapped IPv6 address. */
7264c6867a8SHajimu UMEMOTO 		memset(&key, 0, sizeof(key));
7274c6867a8SHajimu UMEMOTO 		key.sin6_family = AF_INET6;
7284c6867a8SHajimu UMEMOTO 		key.sin6_len = sizeof(key);
7294c6867a8SHajimu UMEMOTO 		key.sin6_addr.s6_addr[10] = 0xff;
7304c6867a8SHajimu UMEMOTO 		key.sin6_addr.s6_addr[11] = 0xff;
7314c6867a8SHajimu UMEMOTO 		memcpy(&key.sin6_addr.s6_addr[12],
7324c6867a8SHajimu UMEMOTO 		       &((struct sockaddr_in *)addr)->sin_addr, 4);
7334c6867a8SHajimu UMEMOTO 		break;
7344c6867a8SHajimu UMEMOTO 	default:
7354c6867a8SHajimu UMEMOTO 		return(NULL);
7364c6867a8SHajimu UMEMOTO 	}
7374c6867a8SHajimu UMEMOTO 
7384c6867a8SHajimu UMEMOTO 	for (ent = TAILQ_FIRST(head); ent; ent = TAILQ_NEXT(ent, pc_entry)) {
7394c6867a8SHajimu UMEMOTO 		pol = &ent->pc_policy;
7404c6867a8SHajimu UMEMOTO 		matchlen = 0;
7414c6867a8SHajimu UMEMOTO 
7424c6867a8SHajimu UMEMOTO 		mp = (u_char *)&pol->addrmask.sin6_addr;
7434c6867a8SHajimu UMEMOTO 		ep = mp + 16;	/* XXX: scope field? */
7444c6867a8SHajimu UMEMOTO 		k = (u_char *)&key.sin6_addr;
7454c6867a8SHajimu UMEMOTO 		p = (u_char *)&pol->addr.sin6_addr;
7464c6867a8SHajimu UMEMOTO 		for (; mp < ep && *mp; mp++, k++, p++) {
7474c6867a8SHajimu UMEMOTO 			m = *mp;
7484c6867a8SHajimu UMEMOTO 			if ((*k & m) != *p)
7494c6867a8SHajimu UMEMOTO 				goto next; /* not match */
7504c6867a8SHajimu UMEMOTO 			if (m == 0xff) /* short cut for a typical case */
7514c6867a8SHajimu UMEMOTO 				matchlen += 8;
7524c6867a8SHajimu UMEMOTO 			else {
7534c6867a8SHajimu UMEMOTO 				while (m >= 0x80) {
7544c6867a8SHajimu UMEMOTO 					matchlen++;
7554c6867a8SHajimu UMEMOTO 					m <<= 1;
7564c6867a8SHajimu UMEMOTO 				}
7574c6867a8SHajimu UMEMOTO 			}
7584c6867a8SHajimu UMEMOTO 		}
7594c6867a8SHajimu UMEMOTO 
7604c6867a8SHajimu UMEMOTO 		/* matched.  check if this is better than the current best. */
7614c6867a8SHajimu UMEMOTO 		if (matchlen > bestmatchlen) {
7624c6867a8SHajimu UMEMOTO 			bestent = ent;
7634c6867a8SHajimu UMEMOTO 			bestmatchlen = matchlen;
7644c6867a8SHajimu UMEMOTO 		}
7654c6867a8SHajimu UMEMOTO 
7664c6867a8SHajimu UMEMOTO 	  next:
7674c6867a8SHajimu UMEMOTO 		continue;
7684c6867a8SHajimu UMEMOTO 	}
7694c6867a8SHajimu UMEMOTO 
7704c6867a8SHajimu UMEMOTO 	return(bestent);
7714c6867a8SHajimu UMEMOTO #else
7724c6867a8SHajimu UMEMOTO 	return(NULL);
7734c6867a8SHajimu UMEMOTO #endif
7744c6867a8SHajimu UMEMOTO 
7754c6867a8SHajimu UMEMOTO }
7764c6867a8SHajimu UMEMOTO 
7774d489f47SHajimu UMEMOTO static void
7784d489f47SHajimu UMEMOTO set_source(aio, ph)
7794d489f47SHajimu UMEMOTO 	struct ai_order *aio;
7804d489f47SHajimu UMEMOTO 	struct policyhead *ph;
7814d489f47SHajimu UMEMOTO {
7824d489f47SHajimu UMEMOTO 	struct addrinfo ai = *aio->aio_ai;
7834d489f47SHajimu UMEMOTO 	struct sockaddr_storage ss;
784943db5a2SHajimu UMEMOTO 	socklen_t srclen;
785943db5a2SHajimu UMEMOTO 	int s;
7864d489f47SHajimu UMEMOTO 
7874d489f47SHajimu UMEMOTO 	/* set unspec ("no source is available"), just in case */
7884d489f47SHajimu UMEMOTO 	aio->aio_srcsa.sa_family = AF_UNSPEC;
7894d489f47SHajimu UMEMOTO 	aio->aio_srcscope = -1;
7904d489f47SHajimu UMEMOTO 
7914d489f47SHajimu UMEMOTO 	switch(ai.ai_family) {
7924d489f47SHajimu UMEMOTO 	case AF_INET:
7934d489f47SHajimu UMEMOTO #ifdef INET6
7944d489f47SHajimu UMEMOTO 	case AF_INET6:
7954d489f47SHajimu UMEMOTO #endif
7964d489f47SHajimu UMEMOTO 		break;
7974d489f47SHajimu UMEMOTO 	default:		/* ignore unsupported AFs explicitly */
7984d489f47SHajimu UMEMOTO 		return;
7994d489f47SHajimu UMEMOTO 	}
8004d489f47SHajimu UMEMOTO 
8014d489f47SHajimu UMEMOTO 	/* XXX: make a dummy addrinfo to call connect() */
8024d489f47SHajimu UMEMOTO 	ai.ai_socktype = SOCK_DGRAM;
8034d489f47SHajimu UMEMOTO 	ai.ai_protocol = IPPROTO_UDP; /* is UDP too specific? */
8044d489f47SHajimu UMEMOTO 	ai.ai_next = NULL;
8054d489f47SHajimu UMEMOTO 	memset(&ss, 0, sizeof(ss));
8064d489f47SHajimu UMEMOTO 	memcpy(&ss, ai.ai_addr, ai.ai_addrlen);
8074d489f47SHajimu UMEMOTO 	ai.ai_addr = (struct sockaddr *)&ss;
8084d489f47SHajimu UMEMOTO 	get_port(&ai, "1", 0);
8094d489f47SHajimu UMEMOTO 
8104d489f47SHajimu UMEMOTO 	/* open a socket to get the source address for the given dst */
8114d489f47SHajimu UMEMOTO 	if ((s = _socket(ai.ai_family, ai.ai_socktype, ai.ai_protocol)) < 0)
8124d489f47SHajimu UMEMOTO 		return;		/* give up */
8134d489f47SHajimu UMEMOTO 	if (_connect(s, ai.ai_addr, ai.ai_addrlen) < 0)
8144d489f47SHajimu UMEMOTO 		goto cleanup;
8154d489f47SHajimu UMEMOTO 	srclen = ai.ai_addrlen;
8164d489f47SHajimu UMEMOTO 	if (_getsockname(s, &aio->aio_srcsa, &srclen) < 0) {
8174d489f47SHajimu UMEMOTO 		aio->aio_srcsa.sa_family = AF_UNSPEC;
8184d489f47SHajimu UMEMOTO 		goto cleanup;
8194d489f47SHajimu UMEMOTO 	}
8204d489f47SHajimu UMEMOTO 	aio->aio_srcscope = gai_addr2scopetype(&aio->aio_srcsa);
8214d489f47SHajimu UMEMOTO 	aio->aio_srcpolicy = match_addrselectpolicy(&aio->aio_srcsa, ph);
8224d489f47SHajimu UMEMOTO 	aio->aio_matchlen = matchlen(&aio->aio_srcsa, aio->aio_ai->ai_addr);
8234d489f47SHajimu UMEMOTO #ifdef INET6
8244d489f47SHajimu UMEMOTO 	if (ai.ai_family == AF_INET6) {
8254d489f47SHajimu UMEMOTO 		struct in6_ifreq ifr6;
8264d489f47SHajimu UMEMOTO 		u_int32_t flags6;
8274d489f47SHajimu UMEMOTO 
8284d489f47SHajimu UMEMOTO 		/* XXX: interface name should not be hardcoded */
8294d489f47SHajimu UMEMOTO 		strncpy(ifr6.ifr_name, "lo0", sizeof(ifr6.ifr_name));
8304d489f47SHajimu UMEMOTO 		memset(&ifr6, 0, sizeof(ifr6));
8314d489f47SHajimu UMEMOTO 		memcpy(&ifr6.ifr_addr, ai.ai_addr, ai.ai_addrlen);
8324d489f47SHajimu UMEMOTO 		if (_ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == 0) {
8334d489f47SHajimu UMEMOTO 			flags6 = ifr6.ifr_ifru.ifru_flags6;
8344d489f47SHajimu UMEMOTO 			if ((flags6 & IN6_IFF_DEPRECATED))
8354d489f47SHajimu UMEMOTO 				aio->aio_srcflag |= AIO_SRCFLAG_DEPRECATED;
8364d489f47SHajimu UMEMOTO 		}
8374d489f47SHajimu UMEMOTO 	}
8384d489f47SHajimu UMEMOTO #endif
8394d489f47SHajimu UMEMOTO 
8404d489f47SHajimu UMEMOTO   cleanup:
8414d489f47SHajimu UMEMOTO 	_close(s);
8424d489f47SHajimu UMEMOTO 	return;
8434d489f47SHajimu UMEMOTO }
8444d489f47SHajimu UMEMOTO 
8454d489f47SHajimu UMEMOTO static int
8464d489f47SHajimu UMEMOTO matchlen(src, dst)
8474d489f47SHajimu UMEMOTO 	struct sockaddr *src, *dst;
8484d489f47SHajimu UMEMOTO {
8494d489f47SHajimu UMEMOTO 	int match = 0;
8504d489f47SHajimu UMEMOTO 	u_char *s, *d;
8514d489f47SHajimu UMEMOTO 	u_char *lim, r;
8524d489f47SHajimu UMEMOTO 	int addrlen;
8534d489f47SHajimu UMEMOTO 
8544d489f47SHajimu UMEMOTO 	switch (src->sa_family) {
8554d489f47SHajimu UMEMOTO #ifdef INET6
8564d489f47SHajimu UMEMOTO 	case AF_INET6:
8574d489f47SHajimu UMEMOTO 		s = (u_char *)&((struct sockaddr_in6 *)src)->sin6_addr;
8584d489f47SHajimu UMEMOTO 		d = (u_char *)&((struct sockaddr_in6 *)dst)->sin6_addr;
8594d489f47SHajimu UMEMOTO 		addrlen = sizeof(struct in6_addr);
8604d489f47SHajimu UMEMOTO 		lim = s + addrlen;
8614d489f47SHajimu UMEMOTO 		break;
8624d489f47SHajimu UMEMOTO #endif
8634d489f47SHajimu UMEMOTO 	case AF_INET:
864fd57e549SGeorge V. Neville-Neil 		s = (u_char *)&((struct sockaddr_in *)src)->sin_addr;
865fd57e549SGeorge V. Neville-Neil 		d = (u_char *)&((struct sockaddr_in *)dst)->sin_addr;
8664d489f47SHajimu UMEMOTO 		addrlen = sizeof(struct in_addr);
8674d489f47SHajimu UMEMOTO 		lim = s + addrlen;
8684d489f47SHajimu UMEMOTO 		break;
8694d489f47SHajimu UMEMOTO 	default:
8704d489f47SHajimu UMEMOTO 		return(0);
8714d489f47SHajimu UMEMOTO 	}
8724d489f47SHajimu UMEMOTO 
8734d489f47SHajimu UMEMOTO 	while (s < lim)
8744d489f47SHajimu UMEMOTO 		if ((r = (*d++ ^ *s++)) != 0) {
8754d489f47SHajimu UMEMOTO 			while (r < addrlen * 8) {
8764d489f47SHajimu UMEMOTO 				match++;
8774d489f47SHajimu UMEMOTO 				r <<= 1;
8784d489f47SHajimu UMEMOTO 			}
8794d489f47SHajimu UMEMOTO 			break;
8804d489f47SHajimu UMEMOTO 		} else
8814d489f47SHajimu UMEMOTO 			match += 8;
8824d489f47SHajimu UMEMOTO 	return(match);
8834d489f47SHajimu UMEMOTO }
8844d489f47SHajimu UMEMOTO 
8854c6867a8SHajimu UMEMOTO static int
8864c6867a8SHajimu UMEMOTO comp_dst(arg1, arg2)
8874c6867a8SHajimu UMEMOTO 	const void *arg1, *arg2;
8884c6867a8SHajimu UMEMOTO {
8894c6867a8SHajimu UMEMOTO 	const struct ai_order *dst1 = arg1, *dst2 = arg2;
8904c6867a8SHajimu UMEMOTO 
8914c6867a8SHajimu UMEMOTO 	/*
8924c6867a8SHajimu UMEMOTO 	 * Rule 1: Avoid unusable destinations.
8934c6867a8SHajimu UMEMOTO 	 * XXX: we currently do not consider if an appropriate route exists.
8944c6867a8SHajimu UMEMOTO 	 */
8954c6867a8SHajimu UMEMOTO 	if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
8964c6867a8SHajimu UMEMOTO 	    dst2->aio_srcsa.sa_family == AF_UNSPEC) {
8974c6867a8SHajimu UMEMOTO 		return(-1);
8984c6867a8SHajimu UMEMOTO 	}
8994c6867a8SHajimu UMEMOTO 	if (dst1->aio_srcsa.sa_family == AF_UNSPEC &&
9004c6867a8SHajimu UMEMOTO 	    dst2->aio_srcsa.sa_family != AF_UNSPEC) {
9014c6867a8SHajimu UMEMOTO 		return(1);
9024c6867a8SHajimu UMEMOTO 	}
9034c6867a8SHajimu UMEMOTO 
9044c6867a8SHajimu UMEMOTO 	/* Rule 2: Prefer matching scope. */
9054c6867a8SHajimu UMEMOTO 	if (dst1->aio_dstscope == dst1->aio_srcscope &&
9064c6867a8SHajimu UMEMOTO 	    dst2->aio_dstscope != dst2->aio_srcscope) {
9074c6867a8SHajimu UMEMOTO 		return(-1);
9084c6867a8SHajimu UMEMOTO 	}
9094c6867a8SHajimu UMEMOTO 	if (dst1->aio_dstscope != dst1->aio_srcscope &&
9104c6867a8SHajimu UMEMOTO 	    dst2->aio_dstscope == dst2->aio_srcscope) {
9114c6867a8SHajimu UMEMOTO 		return(1);
9124c6867a8SHajimu UMEMOTO 	}
9134c6867a8SHajimu UMEMOTO 
9144c6867a8SHajimu UMEMOTO 	/* Rule 3: Avoid deprecated addresses. */
9154c6867a8SHajimu UMEMOTO 	if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
9164c6867a8SHajimu UMEMOTO 	    dst2->aio_srcsa.sa_family != AF_UNSPEC) {
9174c6867a8SHajimu UMEMOTO 		if (!(dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
9184c6867a8SHajimu UMEMOTO 		    (dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
9194c6867a8SHajimu UMEMOTO 			return(-1);
9204c6867a8SHajimu UMEMOTO 		}
9214c6867a8SHajimu UMEMOTO 		if ((dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
9224c6867a8SHajimu UMEMOTO 		    !(dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
9234c6867a8SHajimu UMEMOTO 			return(1);
9244c6867a8SHajimu UMEMOTO 		}
9254c6867a8SHajimu UMEMOTO 	}
9264c6867a8SHajimu UMEMOTO 
9274c6867a8SHajimu UMEMOTO 	/* Rule 4: Prefer home addresses. */
9284c6867a8SHajimu UMEMOTO 	/* XXX: not implemented yet */
9294c6867a8SHajimu UMEMOTO 
9304c6867a8SHajimu UMEMOTO 	/* Rule 5: Prefer matching label. */
9314c6867a8SHajimu UMEMOTO #ifdef INET6
9324c6867a8SHajimu UMEMOTO 	if (dst1->aio_srcpolicy && dst1->aio_dstpolicy &&
9334c6867a8SHajimu UMEMOTO 	    dst1->aio_srcpolicy->pc_policy.label ==
9344c6867a8SHajimu UMEMOTO 	    dst1->aio_dstpolicy->pc_policy.label &&
9354c6867a8SHajimu UMEMOTO 	    (dst2->aio_srcpolicy == NULL || dst2->aio_dstpolicy == NULL ||
9364c6867a8SHajimu UMEMOTO 	     dst2->aio_srcpolicy->pc_policy.label !=
9374c6867a8SHajimu UMEMOTO 	     dst2->aio_dstpolicy->pc_policy.label)) {
9384c6867a8SHajimu UMEMOTO 		return(-1);
9394c6867a8SHajimu UMEMOTO 	}
9404c6867a8SHajimu UMEMOTO 	if (dst2->aio_srcpolicy && dst2->aio_dstpolicy &&
9414c6867a8SHajimu UMEMOTO 	    dst2->aio_srcpolicy->pc_policy.label ==
9424c6867a8SHajimu UMEMOTO 	    dst2->aio_dstpolicy->pc_policy.label &&
9434c6867a8SHajimu UMEMOTO 	    (dst1->aio_srcpolicy == NULL || dst1->aio_dstpolicy == NULL ||
9444c6867a8SHajimu UMEMOTO 	     dst1->aio_srcpolicy->pc_policy.label !=
9454c6867a8SHajimu UMEMOTO 	     dst1->aio_dstpolicy->pc_policy.label)) {
9464c6867a8SHajimu UMEMOTO 		return(1);
9474c6867a8SHajimu UMEMOTO 	}
9484c6867a8SHajimu UMEMOTO #endif
9494c6867a8SHajimu UMEMOTO 
9504c6867a8SHajimu UMEMOTO 	/* Rule 6: Prefer higher precedence. */
9514c6867a8SHajimu UMEMOTO #ifdef INET6
9524c6867a8SHajimu UMEMOTO 	if (dst1->aio_dstpolicy &&
9534c6867a8SHajimu UMEMOTO 	    (dst2->aio_dstpolicy == NULL ||
9544c6867a8SHajimu UMEMOTO 	     dst1->aio_dstpolicy->pc_policy.preced >
9554c6867a8SHajimu UMEMOTO 	     dst2->aio_dstpolicy->pc_policy.preced)) {
9564c6867a8SHajimu UMEMOTO 		return(-1);
9574c6867a8SHajimu UMEMOTO 	}
9584c6867a8SHajimu UMEMOTO 	if (dst2->aio_dstpolicy &&
9594c6867a8SHajimu UMEMOTO 	    (dst1->aio_dstpolicy == NULL ||
9604c6867a8SHajimu UMEMOTO 	     dst2->aio_dstpolicy->pc_policy.preced >
9614c6867a8SHajimu UMEMOTO 	     dst1->aio_dstpolicy->pc_policy.preced)) {
9624c6867a8SHajimu UMEMOTO 		return(1);
9634c6867a8SHajimu UMEMOTO 	}
9644c6867a8SHajimu UMEMOTO #endif
9654c6867a8SHajimu UMEMOTO 
9664c6867a8SHajimu UMEMOTO 	/* Rule 7: Prefer native transport. */
9674c6867a8SHajimu UMEMOTO 	/* XXX: not implemented yet */
9684c6867a8SHajimu UMEMOTO 
9694c6867a8SHajimu UMEMOTO 	/* Rule 8: Prefer smaller scope. */
9704c6867a8SHajimu UMEMOTO 	if (dst1->aio_dstscope >= 0 &&
9714c6867a8SHajimu UMEMOTO 	    dst1->aio_dstscope < dst2->aio_dstscope) {
9724c6867a8SHajimu UMEMOTO 		return(-1);
9734c6867a8SHajimu UMEMOTO 	}
9744c6867a8SHajimu UMEMOTO 	if (dst2->aio_dstscope >= 0 &&
9754c6867a8SHajimu UMEMOTO 	    dst2->aio_dstscope < dst1->aio_dstscope) {
9764c6867a8SHajimu UMEMOTO 		return(1);
9774c6867a8SHajimu UMEMOTO 	}
9784c6867a8SHajimu UMEMOTO 
9794c6867a8SHajimu UMEMOTO 	/*
9804c6867a8SHajimu UMEMOTO 	 * Rule 9: Use longest matching prefix.
9814c6867a8SHajimu UMEMOTO 	 * We compare the match length in a same AF only.
9824c6867a8SHajimu UMEMOTO 	 */
9834c6867a8SHajimu UMEMOTO 	if (dst1->aio_ai->ai_addr->sa_family ==
9844c6867a8SHajimu UMEMOTO 	    dst2->aio_ai->ai_addr->sa_family) {
9854c6867a8SHajimu UMEMOTO 		if (dst1->aio_matchlen > dst2->aio_matchlen) {
9864c6867a8SHajimu UMEMOTO 			return(-1);
9874c6867a8SHajimu UMEMOTO 		}
9884c6867a8SHajimu UMEMOTO 		if (dst1->aio_matchlen < dst2->aio_matchlen) {
9894c6867a8SHajimu UMEMOTO 			return(1);
9904c6867a8SHajimu UMEMOTO 		}
9914c6867a8SHajimu UMEMOTO 	}
9924c6867a8SHajimu UMEMOTO 
9934c6867a8SHajimu UMEMOTO 	/* Rule 10: Otherwise, leave the order unchanged. */
9944c6867a8SHajimu UMEMOTO 	return(-1);
9954c6867a8SHajimu UMEMOTO }
9964c6867a8SHajimu UMEMOTO 
9974c6867a8SHajimu UMEMOTO /*
9984c6867a8SHajimu UMEMOTO  * Copy from scope.c.
9994c6867a8SHajimu UMEMOTO  * XXX: we should standardize the functions and link them as standard
10004c6867a8SHajimu UMEMOTO  * library.
10014c6867a8SHajimu UMEMOTO  */
10024c6867a8SHajimu UMEMOTO static int
10034c6867a8SHajimu UMEMOTO gai_addr2scopetype(sa)
10044c6867a8SHajimu UMEMOTO 	struct sockaddr *sa;
10054c6867a8SHajimu UMEMOTO {
10064c6867a8SHajimu UMEMOTO #ifdef INET6
10074c6867a8SHajimu UMEMOTO 	struct sockaddr_in6 *sa6;
10084c6867a8SHajimu UMEMOTO #endif
10094c6867a8SHajimu UMEMOTO 	struct sockaddr_in *sa4;
10104c6867a8SHajimu UMEMOTO 
10114c6867a8SHajimu UMEMOTO 	switch(sa->sa_family) {
10124c6867a8SHajimu UMEMOTO #ifdef INET6
10134c6867a8SHajimu UMEMOTO 	case AF_INET6:
10144c6867a8SHajimu UMEMOTO 		sa6 = (struct sockaddr_in6 *)sa;
10154c6867a8SHajimu UMEMOTO 		if (IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
10164c6867a8SHajimu UMEMOTO 			/* just use the scope field of the multicast address */
10174c6867a8SHajimu UMEMOTO 			return(sa6->sin6_addr.s6_addr[2] & 0x0f);
10184c6867a8SHajimu UMEMOTO 		}
10194c6867a8SHajimu UMEMOTO 		/*
10204c6867a8SHajimu UMEMOTO 		 * Unicast addresses: map scope type to corresponding scope
10214c6867a8SHajimu UMEMOTO 		 * value defined for multcast addresses.
10224c6867a8SHajimu UMEMOTO 		 * XXX: hardcoded scope type values are bad...
10234c6867a8SHajimu UMEMOTO 		 */
10244c6867a8SHajimu UMEMOTO 		if (IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr))
10254c6867a8SHajimu UMEMOTO 			return(1); /* node local scope */
10264c6867a8SHajimu UMEMOTO 		if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr))
10274c6867a8SHajimu UMEMOTO 			return(2); /* link-local scope */
10284c6867a8SHajimu UMEMOTO 		if (IN6_IS_ADDR_SITELOCAL(&sa6->sin6_addr))
10294c6867a8SHajimu UMEMOTO 			return(5); /* site-local scope */
10304c6867a8SHajimu UMEMOTO 		return(14);	/* global scope */
10314c6867a8SHajimu UMEMOTO 		break;
10324c6867a8SHajimu UMEMOTO #endif
10334c6867a8SHajimu UMEMOTO 	case AF_INET:
10344c6867a8SHajimu UMEMOTO 		/*
10354c6867a8SHajimu UMEMOTO 		 * IPv4 pseudo scoping according to RFC 3484.
10364c6867a8SHajimu UMEMOTO 		 */
10374c6867a8SHajimu UMEMOTO 		sa4 = (struct sockaddr_in *)sa;
10384c6867a8SHajimu UMEMOTO 		/* IPv4 autoconfiguration addresses have link-local scope. */
10394c6867a8SHajimu UMEMOTO 		if (((u_char *)&sa4->sin_addr)[0] == 169 &&
10404c6867a8SHajimu UMEMOTO 		    ((u_char *)&sa4->sin_addr)[1] == 254)
10414c6867a8SHajimu UMEMOTO 			return(2);
10424c6867a8SHajimu UMEMOTO 		/* Private addresses have site-local scope. */
10434c6867a8SHajimu UMEMOTO 		if (((u_char *)&sa4->sin_addr)[0] == 10 ||
10444c6867a8SHajimu UMEMOTO 		    (((u_char *)&sa4->sin_addr)[0] == 172 &&
10454c6867a8SHajimu UMEMOTO 		     (((u_char *)&sa4->sin_addr)[1] & 0xf0) == 16) ||
10464c6867a8SHajimu UMEMOTO 		    (((u_char *)&sa4->sin_addr)[0] == 192 &&
10474c6867a8SHajimu UMEMOTO 		     ((u_char *)&sa4->sin_addr)[1] == 168))
1048251b48a1SHajimu UMEMOTO 			return(14);	/* XXX: It should be 5 unless NAT */
10494c6867a8SHajimu UMEMOTO 		/* Loopback addresses have link-local scope. */
10504c6867a8SHajimu UMEMOTO 		if (((u_char *)&sa4->sin_addr)[0] == 127)
10514c6867a8SHajimu UMEMOTO 			return(2);
10524c6867a8SHajimu UMEMOTO 		return(14);
10534c6867a8SHajimu UMEMOTO 		break;
10544c6867a8SHajimu UMEMOTO 	default:
10554c6867a8SHajimu UMEMOTO 		errno = EAFNOSUPPORT; /* is this a good error? */
10564c6867a8SHajimu UMEMOTO 		return(-1);
10574c6867a8SHajimu UMEMOTO 	}
10584c6867a8SHajimu UMEMOTO }
10594c6867a8SHajimu UMEMOTO 
10607d56d374SYoshinobu Inoue /*
10617d56d374SYoshinobu Inoue  * hostname == NULL.
10627d56d374SYoshinobu Inoue  * passive socket -> anyaddr (0.0.0.0 or ::)
10637d56d374SYoshinobu Inoue  * non-passive socket -> localhost (127.0.0.1 or ::1)
10647d56d374SYoshinobu Inoue  */
10657d56d374SYoshinobu Inoue static int
1066a42af91cSHajimu UMEMOTO explore_null(pai, servname, res)
10677d56d374SYoshinobu Inoue 	const struct addrinfo *pai;
10687d56d374SYoshinobu Inoue 	const char *servname;
10697d56d374SYoshinobu Inoue 	struct addrinfo **res;
10707d56d374SYoshinobu Inoue {
1071bf838688SHajimu UMEMOTO 	int s;
10727d56d374SYoshinobu Inoue 	const struct afd *afd;
10737d56d374SYoshinobu Inoue 	struct addrinfo *cur;
10747d56d374SYoshinobu Inoue 	struct addrinfo sentinel;
10757d56d374SYoshinobu Inoue 	int error;
10767d56d374SYoshinobu Inoue 
10777d56d374SYoshinobu Inoue 	*res = NULL;
10787d56d374SYoshinobu Inoue 	sentinel.ai_next = NULL;
10797d56d374SYoshinobu Inoue 	cur = &sentinel;
10807d56d374SYoshinobu Inoue 
10817d56d374SYoshinobu Inoue 	/*
1082bf838688SHajimu UMEMOTO 	 * filter out AFs that are not supported by the kernel
1083bf838688SHajimu UMEMOTO 	 * XXX errno?
1084bf838688SHajimu UMEMOTO 	 */
1085bf838688SHajimu UMEMOTO 	s = _socket(pai->ai_family, SOCK_DGRAM, 0);
1086bf838688SHajimu UMEMOTO 	if (s < 0) {
1087bf838688SHajimu UMEMOTO 		if (errno != EMFILE)
1088bf838688SHajimu UMEMOTO 			return 0;
1089bf838688SHajimu UMEMOTO 	} else
1090bf838688SHajimu UMEMOTO 		_close(s);
1091bf838688SHajimu UMEMOTO 
1092bf838688SHajimu UMEMOTO 	/*
1093a42af91cSHajimu UMEMOTO 	 * if the servname does not match socktype/protocol, ignore it.
1094a42af91cSHajimu UMEMOTO 	 */
1095a42af91cSHajimu UMEMOTO 	if (get_portmatch(pai, servname) != 0)
1096a42af91cSHajimu UMEMOTO 		return 0;
1097a42af91cSHajimu UMEMOTO 
10987d56d374SYoshinobu Inoue 	afd = find_afd(pai->ai_family);
10997d56d374SYoshinobu Inoue 	if (afd == NULL)
11007d56d374SYoshinobu Inoue 		return 0;
11017d56d374SYoshinobu Inoue 
1102a42af91cSHajimu UMEMOTO 	if (pai->ai_flags & AI_PASSIVE) {
1103a42af91cSHajimu UMEMOTO 		GET_AI(cur->ai_next, afd, afd->a_addrany);
11047d56d374SYoshinobu Inoue 		/* xxx meaningless?
11057d56d374SYoshinobu Inoue 		 * GET_CANONNAME(cur->ai_next, "anyaddr");
1106a42af91cSHajimu UMEMOTO 		 */
1107a42af91cSHajimu UMEMOTO 		GET_PORT(cur->ai_next, servname);
1108a42af91cSHajimu UMEMOTO 	} else {
1109a42af91cSHajimu UMEMOTO 		GET_AI(cur->ai_next, afd, afd->a_loopback);
1110a42af91cSHajimu UMEMOTO 		/* xxx meaningless?
11117d56d374SYoshinobu Inoue 		 * GET_CANONNAME(cur->ai_next, "localhost");
11127d56d374SYoshinobu Inoue 		 */
11137d56d374SYoshinobu Inoue 		GET_PORT(cur->ai_next, servname);
1114a42af91cSHajimu UMEMOTO 	}
1115a42af91cSHajimu UMEMOTO 	cur = cur->ai_next;
11167d56d374SYoshinobu Inoue 
11177d56d374SYoshinobu Inoue 	*res = sentinel.ai_next;
11187d56d374SYoshinobu Inoue 	return 0;
11197d56d374SYoshinobu Inoue 
11207d56d374SYoshinobu Inoue free:
11217d56d374SYoshinobu Inoue 	if (sentinel.ai_next)
11227d56d374SYoshinobu Inoue 		freeaddrinfo(sentinel.ai_next);
11237d56d374SYoshinobu Inoue 	return error;
11247d56d374SYoshinobu Inoue }
11257d56d374SYoshinobu Inoue 
11267d56d374SYoshinobu Inoue /*
11277d56d374SYoshinobu Inoue  * numeric hostname
11287d56d374SYoshinobu Inoue  */
11297d56d374SYoshinobu Inoue static int
11308755b918SHajimu UMEMOTO explore_numeric(pai, hostname, servname, res, canonname)
11317d56d374SYoshinobu Inoue 	const struct addrinfo *pai;
11327d56d374SYoshinobu Inoue 	const char *hostname;
11337d56d374SYoshinobu Inoue 	const char *servname;
11347d56d374SYoshinobu Inoue 	struct addrinfo **res;
11358755b918SHajimu UMEMOTO 	const char *canonname;
11367d56d374SYoshinobu Inoue {
11377d56d374SYoshinobu Inoue 	const struct afd *afd;
11387d56d374SYoshinobu Inoue 	struct addrinfo *cur;
11397d56d374SYoshinobu Inoue 	struct addrinfo sentinel;
11407d56d374SYoshinobu Inoue 	int error;
11417d56d374SYoshinobu Inoue 	char pton[PTON_MAX];
11427d56d374SYoshinobu Inoue 
11437d56d374SYoshinobu Inoue 	*res = NULL;
11447d56d374SYoshinobu Inoue 	sentinel.ai_next = NULL;
11457d56d374SYoshinobu Inoue 	cur = &sentinel;
11467d56d374SYoshinobu Inoue 
1147bf838688SHajimu UMEMOTO 	/*
1148bf838688SHajimu UMEMOTO 	 * if the servname does not match socktype/protocol, ignore it.
1149bf838688SHajimu UMEMOTO 	 */
1150bf838688SHajimu UMEMOTO 	if (get_portmatch(pai, servname) != 0)
1151bf838688SHajimu UMEMOTO 		return 0;
1152bf838688SHajimu UMEMOTO 
11537d56d374SYoshinobu Inoue 	afd = find_afd(pai->ai_family);
11547d56d374SYoshinobu Inoue 	if (afd == NULL)
11557d56d374SYoshinobu Inoue 		return 0;
11567d56d374SYoshinobu Inoue 
1157b826397aSJun-ichiro itojun Hagino 	switch (afd->a_af) {
1158b826397aSJun-ichiro itojun Hagino #if 1 /*X/Open spec*/
1159b826397aSJun-ichiro itojun Hagino 	case AF_INET:
1160b826397aSJun-ichiro itojun Hagino 		if (inet_aton(hostname, (struct in_addr *)pton) == 1) {
11617d56d374SYoshinobu Inoue 			if (pai->ai_family == afd->a_af ||
11627d56d374SYoshinobu Inoue 			    pai->ai_family == PF_UNSPEC /*?*/) {
11637d56d374SYoshinobu Inoue 				GET_AI(cur->ai_next, afd, pton);
11647d56d374SYoshinobu Inoue 				GET_PORT(cur->ai_next, servname);
11658755b918SHajimu UMEMOTO 				if ((pai->ai_flags & AI_CANONNAME)) {
11668755b918SHajimu UMEMOTO 					/*
11678755b918SHajimu UMEMOTO 					 * Set the numeric address itself as
11688755b918SHajimu UMEMOTO 					 * the canonical name, based on a
11698755b918SHajimu UMEMOTO 					 * clarification in rfc3493.
11708755b918SHajimu UMEMOTO 					 */
11718755b918SHajimu UMEMOTO 					GET_CANONNAME(cur->ai_next, canonname);
11728755b918SHajimu UMEMOTO 				}
11737d56d374SYoshinobu Inoue 				while (cur && cur->ai_next)
11747d56d374SYoshinobu Inoue 					cur = cur->ai_next;
11757d56d374SYoshinobu Inoue 			} else
11767d56d374SYoshinobu Inoue 				ERR(EAI_FAMILY);	/*xxx*/
11777d56d374SYoshinobu Inoue 		}
1178b826397aSJun-ichiro itojun Hagino 		break;
1179b826397aSJun-ichiro itojun Hagino #endif
1180b826397aSJun-ichiro itojun Hagino 	default:
1181b826397aSJun-ichiro itojun Hagino 		if (inet_pton(afd->a_af, hostname, pton) == 1) {
1182b826397aSJun-ichiro itojun Hagino 			if (pai->ai_family == afd->a_af ||
1183b826397aSJun-ichiro itojun Hagino 			    pai->ai_family == PF_UNSPEC /*?*/) {
1184b826397aSJun-ichiro itojun Hagino 				GET_AI(cur->ai_next, afd, pton);
1185b826397aSJun-ichiro itojun Hagino 				GET_PORT(cur->ai_next, servname);
11868755b918SHajimu UMEMOTO 				if ((pai->ai_flags & AI_CANONNAME)) {
11878755b918SHajimu UMEMOTO 					/*
11888755b918SHajimu UMEMOTO 					 * Set the numeric address itself as
11898755b918SHajimu UMEMOTO 					 * the canonical name, based on a
11908755b918SHajimu UMEMOTO 					 * clarification in rfc3493.
11918755b918SHajimu UMEMOTO 					 */
11928755b918SHajimu UMEMOTO 					GET_CANONNAME(cur->ai_next, canonname);
11938755b918SHajimu UMEMOTO 				}
1194b826397aSJun-ichiro itojun Hagino 				while (cur && cur->ai_next)
1195b826397aSJun-ichiro itojun Hagino 					cur = cur->ai_next;
1196b826397aSJun-ichiro itojun Hagino 			} else
1197dd521ef1SHajimu UMEMOTO 				ERR(EAI_FAMILY);	/* XXX */
1198b826397aSJun-ichiro itojun Hagino 		}
1199b826397aSJun-ichiro itojun Hagino 		break;
1200b826397aSJun-ichiro itojun Hagino 	}
12017d56d374SYoshinobu Inoue 
12027d56d374SYoshinobu Inoue 	*res = sentinel.ai_next;
12037d56d374SYoshinobu Inoue 	return 0;
12047d56d374SYoshinobu Inoue 
12057d56d374SYoshinobu Inoue free:
12067d56d374SYoshinobu Inoue bad:
12077d56d374SYoshinobu Inoue 	if (sentinel.ai_next)
12087d56d374SYoshinobu Inoue 		freeaddrinfo(sentinel.ai_next);
12097d56d374SYoshinobu Inoue 	return error;
12107d56d374SYoshinobu Inoue }
12117d56d374SYoshinobu Inoue 
12127d56d374SYoshinobu Inoue /*
12137d56d374SYoshinobu Inoue  * numeric hostname with scope
12147d56d374SYoshinobu Inoue  */
12157d56d374SYoshinobu Inoue static int
12167d56d374SYoshinobu Inoue explore_numeric_scope(pai, hostname, servname, res)
12177d56d374SYoshinobu Inoue 	const struct addrinfo *pai;
12187d56d374SYoshinobu Inoue 	const char *hostname;
12197d56d374SYoshinobu Inoue 	const char *servname;
12207d56d374SYoshinobu Inoue 	struct addrinfo **res;
12217d56d374SYoshinobu Inoue {
1222a42af91cSHajimu UMEMOTO #if !defined(SCOPE_DELIMITER) || !defined(INET6)
12238755b918SHajimu UMEMOTO 	return explore_numeric(pai, hostname, servname, res, hostname);
12247d56d374SYoshinobu Inoue #else
12257d56d374SYoshinobu Inoue 	const struct afd *afd;
12267d56d374SYoshinobu Inoue 	struct addrinfo *cur;
12277d56d374SYoshinobu Inoue 	int error;
1228a42af91cSHajimu UMEMOTO 	char *cp, *hostname2 = NULL, *scope, *addr;
12297d56d374SYoshinobu Inoue 	struct sockaddr_in6 *sin6;
12307d56d374SYoshinobu Inoue 
1231bf838688SHajimu UMEMOTO 	/*
1232bf838688SHajimu UMEMOTO 	 * if the servname does not match socktype/protocol, ignore it.
1233bf838688SHajimu UMEMOTO 	 */
1234bf838688SHajimu UMEMOTO 	if (get_portmatch(pai, servname) != 0)
1235bf838688SHajimu UMEMOTO 		return 0;
1236bf838688SHajimu UMEMOTO 
12377d56d374SYoshinobu Inoue 	afd = find_afd(pai->ai_family);
12387d56d374SYoshinobu Inoue 	if (afd == NULL)
12397d56d374SYoshinobu Inoue 		return 0;
1240248aee62SJacques Vidrine 
12417d56d374SYoshinobu Inoue 	if (!afd->a_scoped)
12428755b918SHajimu UMEMOTO 		return explore_numeric(pai, hostname, servname, res, hostname);
12437d56d374SYoshinobu Inoue 
12447d56d374SYoshinobu Inoue 	cp = strchr(hostname, SCOPE_DELIMITER);
12457d56d374SYoshinobu Inoue 	if (cp == NULL)
12468755b918SHajimu UMEMOTO 		return explore_numeric(pai, hostname, servname, res, hostname);
12477d56d374SYoshinobu Inoue 
12487d56d374SYoshinobu Inoue 	/*
12497d56d374SYoshinobu Inoue 	 * Handle special case of <scoped_address><delimiter><scope id>
12507d56d374SYoshinobu Inoue 	 */
12517d56d374SYoshinobu Inoue 	hostname2 = strdup(hostname);
12527d56d374SYoshinobu Inoue 	if (hostname2 == NULL)
12537d56d374SYoshinobu Inoue 		return EAI_MEMORY;
12547d56d374SYoshinobu Inoue 	/* terminate at the delimiter */
12557d56d374SYoshinobu Inoue 	hostname2[cp - hostname] = '\0';
1256a42af91cSHajimu UMEMOTO 	addr = hostname2;
1257a42af91cSHajimu UMEMOTO 	scope = cp + 1;
12587d56d374SYoshinobu Inoue 
12598755b918SHajimu UMEMOTO 	error = explore_numeric(pai, addr, servname, res, hostname);
12607d56d374SYoshinobu Inoue 	if (error == 0) {
1261a89c1d30SHajimu UMEMOTO 		u_int32_t scopeid;
1262a42af91cSHajimu UMEMOTO 
12637d56d374SYoshinobu Inoue 		for (cur = *res; cur; cur = cur->ai_next) {
12647d56d374SYoshinobu Inoue 			if (cur->ai_family != AF_INET6)
12657d56d374SYoshinobu Inoue 				continue;
1266a42af91cSHajimu UMEMOTO 			sin6 = (struct sockaddr_in6 *)(void *)cur->ai_addr;
1267a89c1d30SHajimu UMEMOTO 			if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
1268a42af91cSHajimu UMEMOTO 				free(hostname2);
126937b3e941SHajimu UMEMOTO 				return(EAI_NONAME); /* XXX: is return OK? */
1270a42af91cSHajimu UMEMOTO 			}
1271a42af91cSHajimu UMEMOTO 			sin6->sin6_scope_id = scopeid;
12727d56d374SYoshinobu Inoue 		}
12737d56d374SYoshinobu Inoue 	}
12747d56d374SYoshinobu Inoue 
12757d56d374SYoshinobu Inoue 	free(hostname2);
12767d56d374SYoshinobu Inoue 
12777d56d374SYoshinobu Inoue 	return error;
12787d56d374SYoshinobu Inoue #endif
12797d56d374SYoshinobu Inoue }
12807d56d374SYoshinobu Inoue 
12817d56d374SYoshinobu Inoue static int
12827d56d374SYoshinobu Inoue get_canonname(pai, ai, str)
12837d56d374SYoshinobu Inoue 	const struct addrinfo *pai;
12847d56d374SYoshinobu Inoue 	struct addrinfo *ai;
12857d56d374SYoshinobu Inoue 	const char *str;
12867d56d374SYoshinobu Inoue {
12877d56d374SYoshinobu Inoue 	if ((pai->ai_flags & AI_CANONNAME) != 0) {
12888743b270SHajimu UMEMOTO 		ai->ai_canonname = strdup(str);
12897d56d374SYoshinobu Inoue 		if (ai->ai_canonname == NULL)
12907d56d374SYoshinobu Inoue 			return EAI_MEMORY;
12917d56d374SYoshinobu Inoue 	}
12927d56d374SYoshinobu Inoue 	return 0;
12937d56d374SYoshinobu Inoue }
12947d56d374SYoshinobu Inoue 
12957d56d374SYoshinobu Inoue static struct addrinfo *
12967d56d374SYoshinobu Inoue get_ai(pai, afd, addr)
12977d56d374SYoshinobu Inoue 	const struct addrinfo *pai;
12987d56d374SYoshinobu Inoue 	const struct afd *afd;
12997d56d374SYoshinobu Inoue 	const char *addr;
13007d56d374SYoshinobu Inoue {
13017d56d374SYoshinobu Inoue 	char *p;
13027d56d374SYoshinobu Inoue 	struct addrinfo *ai;
13037d56d374SYoshinobu Inoue #ifdef FAITH
13047d56d374SYoshinobu Inoue 	struct in6_addr faith_prefix;
13057d56d374SYoshinobu Inoue 	char *fp_str;
13067d56d374SYoshinobu Inoue 	int translate = 0;
13077d56d374SYoshinobu Inoue #endif
13087d56d374SYoshinobu Inoue 
13097d56d374SYoshinobu Inoue #ifdef FAITH
13107d56d374SYoshinobu Inoue 	/*
13117d56d374SYoshinobu Inoue 	 * Transfrom an IPv4 addr into a special IPv6 addr format for
13127d56d374SYoshinobu Inoue 	 * IPv6->IPv4 translation gateway. (only TCP is supported now)
13137d56d374SYoshinobu Inoue 	 *
13147d56d374SYoshinobu Inoue 	 * +-----------------------------------+------------+
13157d56d374SYoshinobu Inoue 	 * | faith prefix part (12 bytes)      | embedded   |
13167d56d374SYoshinobu Inoue 	 * |                                   | IPv4 addr part (4 bytes)
13177d56d374SYoshinobu Inoue 	 * +-----------------------------------+------------+
13187d56d374SYoshinobu Inoue 	 *
13197d56d374SYoshinobu Inoue 	 * faith prefix part is specified as ascii IPv6 addr format
13207d56d374SYoshinobu Inoue 	 * in environmental variable GAI.
13217d56d374SYoshinobu Inoue 	 * For FAITH to work correctly, routing to faith prefix must be
13227d56d374SYoshinobu Inoue 	 * setup toward a machine where a FAITH daemon operates.
13237d56d374SYoshinobu Inoue 	 * Also, the machine must enable some mechanizm
13247d56d374SYoshinobu Inoue 	 * (e.g. faith interface hack) to divert those packet with
13257d56d374SYoshinobu Inoue 	 * faith prefixed destination addr to user-land FAITH daemon.
13267d56d374SYoshinobu Inoue 	 */
13277d56d374SYoshinobu Inoue 	fp_str = getenv("GAI");
13287d56d374SYoshinobu Inoue 	if (fp_str && inet_pton(AF_INET6, fp_str, &faith_prefix) == 1 &&
13297d56d374SYoshinobu Inoue 	    afd->a_af == AF_INET && pai->ai_socktype == SOCK_STREAM) {
13307d56d374SYoshinobu Inoue 		u_int32_t v4a;
13317d56d374SYoshinobu Inoue 		u_int8_t v4a_top;
13327d56d374SYoshinobu Inoue 
13337d56d374SYoshinobu Inoue 		memcpy(&v4a, addr, sizeof v4a);
13347d56d374SYoshinobu Inoue 		v4a_top = v4a >> IN_CLASSA_NSHIFT;
13357d56d374SYoshinobu Inoue 		if (!IN_MULTICAST(v4a) && !IN_EXPERIMENTAL(v4a) &&
13367d56d374SYoshinobu Inoue 		    v4a_top != 0 && v4a != IN_LOOPBACKNET) {
13377d56d374SYoshinobu Inoue 			afd = &afdl[N_INET6];
13387d56d374SYoshinobu Inoue 			memcpy(&faith_prefix.s6_addr[12], addr,
13397d56d374SYoshinobu Inoue 			       sizeof(struct in_addr));
13407d56d374SYoshinobu Inoue 			translate = 1;
13417d56d374SYoshinobu Inoue 		}
13427d56d374SYoshinobu Inoue 	}
13437d56d374SYoshinobu Inoue #endif
13447d56d374SYoshinobu Inoue 
13457d56d374SYoshinobu Inoue 	ai = (struct addrinfo *)malloc(sizeof(struct addrinfo)
13467d56d374SYoshinobu Inoue 		+ (afd->a_socklen));
13477d56d374SYoshinobu Inoue 	if (ai == NULL)
13487d56d374SYoshinobu Inoue 		return NULL;
13497d56d374SYoshinobu Inoue 
13507d56d374SYoshinobu Inoue 	memcpy(ai, pai, sizeof(struct addrinfo));
1351a42af91cSHajimu UMEMOTO 	ai->ai_addr = (struct sockaddr *)(void *)(ai + 1);
1352a42af91cSHajimu UMEMOTO 	memset(ai->ai_addr, 0, (size_t)afd->a_socklen);
13537d56d374SYoshinobu Inoue 	ai->ai_addr->sa_len = afd->a_socklen;
13547d56d374SYoshinobu Inoue 	ai->ai_addrlen = afd->a_socklen;
13557d56d374SYoshinobu Inoue 	ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
1356248aee62SJacques Vidrine 	p = (char *)(void *)(ai->ai_addr);
13577d56d374SYoshinobu Inoue #ifdef FAITH
13587d56d374SYoshinobu Inoue 	if (translate == 1)
1359248aee62SJacques Vidrine 		memcpy(p + afd->a_off, &faith_prefix, (size_t)afd->a_addrlen);
13607d56d374SYoshinobu Inoue 	else
13617d56d374SYoshinobu Inoue #endif
1362248aee62SJacques Vidrine 	memcpy(p + afd->a_off, addr, (size_t)afd->a_addrlen);
13637d56d374SYoshinobu Inoue 	return ai;
13647d56d374SYoshinobu Inoue }
13657d56d374SYoshinobu Inoue 
13667d56d374SYoshinobu Inoue static int
13677d56d374SYoshinobu Inoue get_portmatch(ai, servname)
13687d56d374SYoshinobu Inoue 	const struct addrinfo *ai;
13697d56d374SYoshinobu Inoue 	const char *servname;
13707d56d374SYoshinobu Inoue {
1371a42af91cSHajimu UMEMOTO 
1372c224435eSHajimu UMEMOTO 	/* get_port does not touch first argument when matchonly == 1. */
1373a42af91cSHajimu UMEMOTO 	/* LINTED const cast */
13747d56d374SYoshinobu Inoue 	return get_port((struct addrinfo *)ai, servname, 1);
13757d56d374SYoshinobu Inoue }
13767d56d374SYoshinobu Inoue 
13777d56d374SYoshinobu Inoue static int
13787d56d374SYoshinobu Inoue get_port(ai, servname, matchonly)
13797d56d374SYoshinobu Inoue 	struct addrinfo *ai;
13807d56d374SYoshinobu Inoue 	const char *servname;
13817d56d374SYoshinobu Inoue 	int matchonly;
13827d56d374SYoshinobu Inoue {
13837d56d374SYoshinobu Inoue 	const char *proto;
13847d56d374SYoshinobu Inoue 	struct servent *sp;
13857d56d374SYoshinobu Inoue 	int port;
13867d56d374SYoshinobu Inoue 	int allownumeric;
13877d56d374SYoshinobu Inoue 
13887d56d374SYoshinobu Inoue 	if (servname == NULL)
13897d56d374SYoshinobu Inoue 		return 0;
1390a42af91cSHajimu UMEMOTO 	switch (ai->ai_family) {
1391a42af91cSHajimu UMEMOTO 	case AF_INET:
1392a42af91cSHajimu UMEMOTO #ifdef AF_INET6
1393a42af91cSHajimu UMEMOTO 	case AF_INET6:
13947d56d374SYoshinobu Inoue #endif
1395a42af91cSHajimu UMEMOTO 		break;
1396a42af91cSHajimu UMEMOTO 	default:
13977d56d374SYoshinobu Inoue 		return 0;
1398a42af91cSHajimu UMEMOTO 	}
13997d56d374SYoshinobu Inoue 
14007d56d374SYoshinobu Inoue 	switch (ai->ai_socktype) {
14017d56d374SYoshinobu Inoue 	case SOCK_RAW:
14027d56d374SYoshinobu Inoue 		return EAI_SERVICE;
14037d56d374SYoshinobu Inoue 	case SOCK_DGRAM:
14047d56d374SYoshinobu Inoue 	case SOCK_STREAM:
14057d56d374SYoshinobu Inoue 		allownumeric = 1;
14067d56d374SYoshinobu Inoue 		break;
14077d56d374SYoshinobu Inoue 	case ANY:
14087d56d374SYoshinobu Inoue 		allownumeric = 0;
14097d56d374SYoshinobu Inoue 		break;
14107d56d374SYoshinobu Inoue 	default:
14117d56d374SYoshinobu Inoue 		return EAI_SOCKTYPE;
14127d56d374SYoshinobu Inoue 	}
14137d56d374SYoshinobu Inoue 
1414c224435eSHajimu UMEMOTO 	port = str2number(servname);
1415c224435eSHajimu UMEMOTO 	if (port >= 0) {
14167d56d374SYoshinobu Inoue 		if (!allownumeric)
14177d56d374SYoshinobu Inoue 			return EAI_SERVICE;
14187d56d374SYoshinobu Inoue 		if (port < 0 || port > 65535)
14197d56d374SYoshinobu Inoue 			return EAI_SERVICE;
1420dd521ef1SHajimu UMEMOTO 		port = htons(port);
14217d56d374SYoshinobu Inoue 	} else {
1422c224435eSHajimu UMEMOTO 		if (ai->ai_flags & AI_NUMERICSERV)
1423c224435eSHajimu UMEMOTO 			return EAI_NONAME;
1424bf838688SHajimu UMEMOTO 		switch (ai->ai_socktype) {
1425bf838688SHajimu UMEMOTO 		case SOCK_DGRAM:
14267d56d374SYoshinobu Inoue 			proto = "udp";
14277d56d374SYoshinobu Inoue 			break;
1428bf838688SHajimu UMEMOTO 		case SOCK_STREAM:
14297d56d374SYoshinobu Inoue 			proto = "tcp";
14307d56d374SYoshinobu Inoue 			break;
14317d56d374SYoshinobu Inoue 		default:
14327d56d374SYoshinobu Inoue 			proto = NULL;
14337d56d374SYoshinobu Inoue 			break;
14347d56d374SYoshinobu Inoue 		}
14357d56d374SYoshinobu Inoue 
143662e8b17dSHajimu UMEMOTO 		if ((sp = getservbyname(servname, proto)) == NULL)
14377d56d374SYoshinobu Inoue 			return EAI_SERVICE;
14387d56d374SYoshinobu Inoue 		port = sp->s_port;
14397d56d374SYoshinobu Inoue 	}
14407d56d374SYoshinobu Inoue 
14417d56d374SYoshinobu Inoue 	if (!matchonly) {
14427d56d374SYoshinobu Inoue 		switch (ai->ai_family) {
14437d56d374SYoshinobu Inoue 		case AF_INET:
1444a42af91cSHajimu UMEMOTO 			((struct sockaddr_in *)(void *)
1445a42af91cSHajimu UMEMOTO 			    ai->ai_addr)->sin_port = port;
14467d56d374SYoshinobu Inoue 			break;
14477d56d374SYoshinobu Inoue #ifdef INET6
14487d56d374SYoshinobu Inoue 		case AF_INET6:
1449a42af91cSHajimu UMEMOTO 			((struct sockaddr_in6 *)(void *)
1450a42af91cSHajimu UMEMOTO 			    ai->ai_addr)->sin6_port = port;
14517d56d374SYoshinobu Inoue 			break;
14527d56d374SYoshinobu Inoue #endif
14537d56d374SYoshinobu Inoue 		}
14547d56d374SYoshinobu Inoue 	}
14557d56d374SYoshinobu Inoue 
14567d56d374SYoshinobu Inoue 	return 0;
14577d56d374SYoshinobu Inoue }
14587d56d374SYoshinobu Inoue 
14597d56d374SYoshinobu Inoue static const struct afd *
14607d56d374SYoshinobu Inoue find_afd(af)
14617d56d374SYoshinobu Inoue 	int af;
14627d56d374SYoshinobu Inoue {
14637d56d374SYoshinobu Inoue 	const struct afd *afd;
14647d56d374SYoshinobu Inoue 
14657d56d374SYoshinobu Inoue 	if (af == PF_UNSPEC)
14667d56d374SYoshinobu Inoue 		return NULL;
14677d56d374SYoshinobu Inoue 	for (afd = afdl; afd->a_af; afd++) {
14687d56d374SYoshinobu Inoue 		if (afd->a_af == af)
14697d56d374SYoshinobu Inoue 			return afd;
14707d56d374SYoshinobu Inoue 	}
14717d56d374SYoshinobu Inoue 	return NULL;
14727d56d374SYoshinobu Inoue }
1473a42af91cSHajimu UMEMOTO 
1474a42af91cSHajimu UMEMOTO /*
1475a42af91cSHajimu UMEMOTO  * post-2553: AI_ADDRCONFIG check.  if we use getipnodeby* as backend, backend
1476a42af91cSHajimu UMEMOTO  * will take care of it.
1477a42af91cSHajimu UMEMOTO  * the semantics of AI_ADDRCONFIG is not defined well.  we are not sure
1478a42af91cSHajimu UMEMOTO  * if the code is right or not.
1479bf838688SHajimu UMEMOTO  *
1480bf838688SHajimu UMEMOTO  * XXX PF_UNSPEC -> PF_INET6 + PF_INET mapping needs to be in sync with
1481bf838688SHajimu UMEMOTO  * _dns_getaddrinfo.
1482a42af91cSHajimu UMEMOTO  */
1483a42af91cSHajimu UMEMOTO static int
1484bf838688SHajimu UMEMOTO addrconfig(pai)
1485bf838688SHajimu UMEMOTO 	struct addrinfo *pai;
1486a42af91cSHajimu UMEMOTO {
1487bf838688SHajimu UMEMOTO 	int s, af;
1488a42af91cSHajimu UMEMOTO 
1489bf838688SHajimu UMEMOTO 	/*
1490bf838688SHajimu UMEMOTO 	 * TODO:
1491bf838688SHajimu UMEMOTO 	 * Note that implementation dependent test for address
1492bf838688SHajimu UMEMOTO 	 * configuration should be done everytime called
1493bf838688SHajimu UMEMOTO 	 * (or apropriate interval),
1494bf838688SHajimu UMEMOTO 	 * because addresses will be dynamically assigned or deleted.
1495bf838688SHajimu UMEMOTO 	 */
1496bf838688SHajimu UMEMOTO 	af = pai->ai_family;
1497bf838688SHajimu UMEMOTO 	if (af == AF_UNSPEC) {
1498bf838688SHajimu UMEMOTO 		if ((s = _socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1499bf838688SHajimu UMEMOTO 			af = AF_INET;
1500bf838688SHajimu UMEMOTO 		else {
1501bf838688SHajimu UMEMOTO 			_close(s);
1502bf838688SHajimu UMEMOTO 			if ((s = _socket(AF_INET, SOCK_DGRAM, 0)) < 0)
1503bf838688SHajimu UMEMOTO 				af = AF_INET6;
1504bf838688SHajimu UMEMOTO 			else
1505bf838688SHajimu UMEMOTO 				_close(s);
1506bf838688SHajimu UMEMOTO 		}
1507bf838688SHajimu UMEMOTO 	}
1508bf838688SHajimu UMEMOTO 	if (af != AF_UNSPEC) {
1509bf838688SHajimu UMEMOTO 		if ((s = _socket(af, SOCK_DGRAM, 0)) < 0)
1510a42af91cSHajimu UMEMOTO 			return 0;
1511bf838688SHajimu UMEMOTO 		_close(s);
1512bf838688SHajimu UMEMOTO 	}
1513bf838688SHajimu UMEMOTO 	pai->ai_family = af;
1514a42af91cSHajimu UMEMOTO 	return 1;
1515a42af91cSHajimu UMEMOTO }
1516a42af91cSHajimu UMEMOTO 
1517a42af91cSHajimu UMEMOTO #ifdef INET6
1518a42af91cSHajimu UMEMOTO /* convert a string to a scope identifier. XXX: IPv6 specific */
1519a42af91cSHajimu UMEMOTO static int
1520a89c1d30SHajimu UMEMOTO ip6_str2scopeid(scope, sin6, scopeid)
1521a42af91cSHajimu UMEMOTO 	char *scope;
1522a42af91cSHajimu UMEMOTO 	struct sockaddr_in6 *sin6;
1523a89c1d30SHajimu UMEMOTO 	u_int32_t *scopeid;
1524a42af91cSHajimu UMEMOTO {
1525a89c1d30SHajimu UMEMOTO 	u_long lscopeid;
1526bf838688SHajimu UMEMOTO 	struct in6_addr *a6;
1527a42af91cSHajimu UMEMOTO 	char *ep;
1528a42af91cSHajimu UMEMOTO 
1529bf838688SHajimu UMEMOTO 	a6 = &sin6->sin6_addr;
1530bf838688SHajimu UMEMOTO 
1531ec20fe00SJun-ichiro itojun Hagino 	/* empty scopeid portion is invalid */
1532ec20fe00SJun-ichiro itojun Hagino 	if (*scope == '\0')
1533ec20fe00SJun-ichiro itojun Hagino 		return -1;
1534ec20fe00SJun-ichiro itojun Hagino 
1535bf838688SHajimu UMEMOTO 	if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
1536a42af91cSHajimu UMEMOTO 		/*
1537a42af91cSHajimu UMEMOTO 		 * We currently assume a one-to-one mapping between links
1538a42af91cSHajimu UMEMOTO 		 * and interfaces, so we simply use interface indices for
1539a42af91cSHajimu UMEMOTO 		 * like-local scopes.
1540a42af91cSHajimu UMEMOTO 		 */
1541a89c1d30SHajimu UMEMOTO 		*scopeid = if_nametoindex(scope);
1542a89c1d30SHajimu UMEMOTO 		if (*scopeid == 0)
1543a42af91cSHajimu UMEMOTO 			goto trynumeric;
1544a89c1d30SHajimu UMEMOTO 		return 0;
1545a42af91cSHajimu UMEMOTO 	}
1546a42af91cSHajimu UMEMOTO 
1547a42af91cSHajimu UMEMOTO 	/* still unclear about literal, allow numeric only - placeholder */
1548a42af91cSHajimu UMEMOTO 	if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6))
1549a42af91cSHajimu UMEMOTO 		goto trynumeric;
1550a42af91cSHajimu UMEMOTO 	if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
1551a42af91cSHajimu UMEMOTO 		goto trynumeric;
1552a42af91cSHajimu UMEMOTO 	else
1553a42af91cSHajimu UMEMOTO 		goto trynumeric;	/* global */
1554a42af91cSHajimu UMEMOTO 
1555a42af91cSHajimu UMEMOTO 	/* try to convert to a numeric id as a last resort */
1556a42af91cSHajimu UMEMOTO   trynumeric:
1557a89c1d30SHajimu UMEMOTO 	errno = 0;
1558a89c1d30SHajimu UMEMOTO 	lscopeid = strtoul(scope, &ep, 10);
1559a89c1d30SHajimu UMEMOTO 	*scopeid = (u_int32_t)(lscopeid & 0xffffffffUL);
1560a89c1d30SHajimu UMEMOTO 	if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
1561a89c1d30SHajimu UMEMOTO 		return 0;
1562a42af91cSHajimu UMEMOTO 	else
1563a42af91cSHajimu UMEMOTO 		return -1;
1564a42af91cSHajimu UMEMOTO }
1565a42af91cSHajimu UMEMOTO #endif
1566a42af91cSHajimu UMEMOTO 
1567688a5c3eSHajimu UMEMOTO /*
1568688a5c3eSHajimu UMEMOTO  * FQDN hostname, DNS lookup
1569688a5c3eSHajimu UMEMOTO  */
1570688a5c3eSHajimu UMEMOTO static int
1571688a5c3eSHajimu UMEMOTO explore_fqdn(pai, hostname, servname, res)
1572688a5c3eSHajimu UMEMOTO 	const struct addrinfo *pai;
1573688a5c3eSHajimu UMEMOTO 	const char *hostname;
1574688a5c3eSHajimu UMEMOTO 	const char *servname;
1575688a5c3eSHajimu UMEMOTO 	struct addrinfo **res;
1576688a5c3eSHajimu UMEMOTO {
1577688a5c3eSHajimu UMEMOTO 	struct addrinfo *result;
1578688a5c3eSHajimu UMEMOTO 	struct addrinfo *cur;
1579688a5c3eSHajimu UMEMOTO 	int error = 0;
1580688a5c3eSHajimu UMEMOTO 	static const ns_dtab dtab[] = {
1581688a5c3eSHajimu UMEMOTO 		NS_FILES_CB(_files_getaddrinfo, NULL)
1582688a5c3eSHajimu UMEMOTO 		{ NSSRC_DNS, _dns_getaddrinfo, NULL },	/* force -DHESIOD */
1583688a5c3eSHajimu UMEMOTO 		NS_NIS_CB(_yp_getaddrinfo, NULL)
1584688a5c3eSHajimu UMEMOTO 		{ 0 }
158569b0a4b6SJim Pirzyk 	};
158669b0a4b6SJim Pirzyk 
1587688a5c3eSHajimu UMEMOTO 	result = NULL;
158869b0a4b6SJim Pirzyk 
1589688a5c3eSHajimu UMEMOTO 	/*
1590688a5c3eSHajimu UMEMOTO 	 * if the servname does not match socktype/protocol, ignore it.
1591688a5c3eSHajimu UMEMOTO 	 */
159233dee819SBrian Feldman 	if (get_portmatch(pai, servname) != 0)
159369b0a4b6SJim Pirzyk 		return 0;
159469b0a4b6SJim Pirzyk 
1595688a5c3eSHajimu UMEMOTO 	switch (_nsdispatch(&result, dtab, NSDB_HOSTS, "getaddrinfo",
1596688a5c3eSHajimu UMEMOTO 			default_dns_files, hostname, pai)) {
1597688a5c3eSHajimu UMEMOTO 	case NS_TRYAGAIN:
1598688a5c3eSHajimu UMEMOTO 		error = EAI_AGAIN;
1599688a5c3eSHajimu UMEMOTO 		goto free;
1600688a5c3eSHajimu UMEMOTO 	case NS_UNAVAIL:
1601688a5c3eSHajimu UMEMOTO 		error = EAI_FAIL;
1602688a5c3eSHajimu UMEMOTO 		goto free;
1603688a5c3eSHajimu UMEMOTO 	case NS_NOTFOUND:
1604688a5c3eSHajimu UMEMOTO 		error = EAI_NONAME;
1605688a5c3eSHajimu UMEMOTO 		goto free;
1606688a5c3eSHajimu UMEMOTO 	case NS_SUCCESS:
1607688a5c3eSHajimu UMEMOTO 		error = 0;
1608688a5c3eSHajimu UMEMOTO 		for (cur = result; cur; cur = cur->ai_next) {
1609688a5c3eSHajimu UMEMOTO 			GET_PORT(cur, servname);
1610688a5c3eSHajimu UMEMOTO 			/* canonname should be filled already */
1611688a5c3eSHajimu UMEMOTO 		}
161269b0a4b6SJim Pirzyk 		break;
161369b0a4b6SJim Pirzyk 	}
161469b0a4b6SJim Pirzyk 
1615688a5c3eSHajimu UMEMOTO 	*res = result;
1616688a5c3eSHajimu UMEMOTO 
161769b0a4b6SJim Pirzyk 	return 0;
1618688a5c3eSHajimu UMEMOTO 
1619688a5c3eSHajimu UMEMOTO free:
1620688a5c3eSHajimu UMEMOTO 	if (result)
1621688a5c3eSHajimu UMEMOTO 		freeaddrinfo(result);
1622688a5c3eSHajimu UMEMOTO 	return error;
162369b0a4b6SJim Pirzyk }
162469b0a4b6SJim Pirzyk 
1625a42af91cSHajimu UMEMOTO #ifdef DEBUG
1626a42af91cSHajimu UMEMOTO static const char AskedForGot[] =
1627a42af91cSHajimu UMEMOTO 	"gethostby*.getanswer: asked for \"%s\", got \"%s\"";
1628a42af91cSHajimu UMEMOTO #endif
1629a42af91cSHajimu UMEMOTO 
1630a42af91cSHajimu UMEMOTO static struct addrinfo *
1631a42af91cSHajimu UMEMOTO getanswer(answer, anslen, qname, qtype, pai)
1632a42af91cSHajimu UMEMOTO 	const querybuf *answer;
1633a42af91cSHajimu UMEMOTO 	int anslen;
1634a42af91cSHajimu UMEMOTO 	const char *qname;
1635a42af91cSHajimu UMEMOTO 	int qtype;
1636a42af91cSHajimu UMEMOTO 	const struct addrinfo *pai;
1637a42af91cSHajimu UMEMOTO {
1638a42af91cSHajimu UMEMOTO 	struct addrinfo sentinel, *cur;
1639a42af91cSHajimu UMEMOTO 	struct addrinfo ai;
1640a42af91cSHajimu UMEMOTO 	const struct afd *afd;
1641a42af91cSHajimu UMEMOTO 	char *canonname;
1642a42af91cSHajimu UMEMOTO 	const HEADER *hp;
1643a42af91cSHajimu UMEMOTO 	const u_char *cp;
1644a42af91cSHajimu UMEMOTO 	int n;
1645a42af91cSHajimu UMEMOTO 	const u_char *eom;
1646dd521ef1SHajimu UMEMOTO 	char *bp, *ep;
1647dd521ef1SHajimu UMEMOTO 	int type, class, ancount, qdcount;
1648a42af91cSHajimu UMEMOTO 	int haveanswer, had_error;
1649a42af91cSHajimu UMEMOTO 	char tbuf[MAXDNAME];
1650c05ac53bSDavid E. O'Brien 	int (*name_ok)(const char *);
1651a42af91cSHajimu UMEMOTO 	char hostbuf[8*1024];
1652a42af91cSHajimu UMEMOTO 
1653a42af91cSHajimu UMEMOTO 	memset(&sentinel, 0, sizeof(sentinel));
1654a42af91cSHajimu UMEMOTO 	cur = &sentinel;
1655a42af91cSHajimu UMEMOTO 
1656a42af91cSHajimu UMEMOTO 	canonname = NULL;
1657a42af91cSHajimu UMEMOTO 	eom = answer->buf + anslen;
1658a42af91cSHajimu UMEMOTO 	switch (qtype) {
1659a42af91cSHajimu UMEMOTO 	case T_A:
1660a42af91cSHajimu UMEMOTO 	case T_AAAA:
1661a42af91cSHajimu UMEMOTO 	case T_ANY:	/*use T_ANY only for T_A/T_AAAA lookup*/
1662a42af91cSHajimu UMEMOTO 		name_ok = res_hnok;
1663a42af91cSHajimu UMEMOTO 		break;
1664a42af91cSHajimu UMEMOTO 	default:
1665a42af91cSHajimu UMEMOTO 		return (NULL);	/* XXX should be abort(); */
1666a42af91cSHajimu UMEMOTO 	}
1667a42af91cSHajimu UMEMOTO 	/*
1668a42af91cSHajimu UMEMOTO 	 * find first satisfactory answer
1669a42af91cSHajimu UMEMOTO 	 */
1670a42af91cSHajimu UMEMOTO 	hp = &answer->hdr;
1671a42af91cSHajimu UMEMOTO 	ancount = ntohs(hp->ancount);
1672a42af91cSHajimu UMEMOTO 	qdcount = ntohs(hp->qdcount);
1673a42af91cSHajimu UMEMOTO 	bp = hostbuf;
1674dd521ef1SHajimu UMEMOTO 	ep = hostbuf + sizeof hostbuf;
1675a42af91cSHajimu UMEMOTO 	cp = answer->buf + HFIXEDSZ;
1676a42af91cSHajimu UMEMOTO 	if (qdcount != 1) {
1677a42af91cSHajimu UMEMOTO 		h_errno = NO_RECOVERY;
1678a42af91cSHajimu UMEMOTO 		return (NULL);
1679a42af91cSHajimu UMEMOTO 	}
1680dd521ef1SHajimu UMEMOTO 	n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1681a42af91cSHajimu UMEMOTO 	if ((n < 0) || !(*name_ok)(bp)) {
1682a42af91cSHajimu UMEMOTO 		h_errno = NO_RECOVERY;
1683a42af91cSHajimu UMEMOTO 		return (NULL);
1684a42af91cSHajimu UMEMOTO 	}
1685a42af91cSHajimu UMEMOTO 	cp += n + QFIXEDSZ;
1686a42af91cSHajimu UMEMOTO 	if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
1687a42af91cSHajimu UMEMOTO 		/* res_send() has already verified that the query name is the
1688a42af91cSHajimu UMEMOTO 		 * same as the one we sent; this just gets the expanded name
1689a42af91cSHajimu UMEMOTO 		 * (i.e., with the succeeding search-domain tacked on).
1690a42af91cSHajimu UMEMOTO 		 */
1691a42af91cSHajimu UMEMOTO 		n = strlen(bp) + 1;		/* for the \0 */
1692a42af91cSHajimu UMEMOTO 		if (n >= MAXHOSTNAMELEN) {
1693a42af91cSHajimu UMEMOTO 			h_errno = NO_RECOVERY;
1694a42af91cSHajimu UMEMOTO 			return (NULL);
1695a42af91cSHajimu UMEMOTO 		}
1696a42af91cSHajimu UMEMOTO 		canonname = bp;
1697a42af91cSHajimu UMEMOTO 		bp += n;
1698a42af91cSHajimu UMEMOTO 		/* The qname can be abbreviated, but h_name is now absolute. */
1699a42af91cSHajimu UMEMOTO 		qname = canonname;
1700a42af91cSHajimu UMEMOTO 	}
1701a42af91cSHajimu UMEMOTO 	haveanswer = 0;
1702a42af91cSHajimu UMEMOTO 	had_error = 0;
1703a42af91cSHajimu UMEMOTO 	while (ancount-- > 0 && cp < eom && !had_error) {
1704dd521ef1SHajimu UMEMOTO 		n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1705a42af91cSHajimu UMEMOTO 		if ((n < 0) || !(*name_ok)(bp)) {
1706a42af91cSHajimu UMEMOTO 			had_error++;
1707a42af91cSHajimu UMEMOTO 			continue;
1708a42af91cSHajimu UMEMOTO 		}
1709a42af91cSHajimu UMEMOTO 		cp += n;			/* name */
1710a42af91cSHajimu UMEMOTO 		type = _getshort(cp);
1711a42af91cSHajimu UMEMOTO  		cp += INT16SZ;			/* type */
1712a42af91cSHajimu UMEMOTO 		class = _getshort(cp);
1713a42af91cSHajimu UMEMOTO  		cp += INT16SZ + INT32SZ;	/* class, TTL */
1714a42af91cSHajimu UMEMOTO 		n = _getshort(cp);
1715a42af91cSHajimu UMEMOTO 		cp += INT16SZ;			/* len */
1716a42af91cSHajimu UMEMOTO 		if (class != C_IN) {
1717a42af91cSHajimu UMEMOTO 			/* XXX - debug? syslog? */
1718a42af91cSHajimu UMEMOTO 			cp += n;
1719a42af91cSHajimu UMEMOTO 			continue;		/* XXX - had_error++ ? */
1720a42af91cSHajimu UMEMOTO 		}
1721a42af91cSHajimu UMEMOTO 		if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) &&
1722a42af91cSHajimu UMEMOTO 		    type == T_CNAME) {
1723a42af91cSHajimu UMEMOTO 			n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
1724a42af91cSHajimu UMEMOTO 			if ((n < 0) || !(*name_ok)(tbuf)) {
1725a42af91cSHajimu UMEMOTO 				had_error++;
1726a42af91cSHajimu UMEMOTO 				continue;
1727a42af91cSHajimu UMEMOTO 			}
1728a42af91cSHajimu UMEMOTO 			cp += n;
1729a42af91cSHajimu UMEMOTO 			/* Get canonical name. */
1730a42af91cSHajimu UMEMOTO 			n = strlen(tbuf) + 1;	/* for the \0 */
1731dd521ef1SHajimu UMEMOTO 			if (n > ep - bp || n >= MAXHOSTNAMELEN) {
1732a42af91cSHajimu UMEMOTO 				had_error++;
1733a42af91cSHajimu UMEMOTO 				continue;
1734a42af91cSHajimu UMEMOTO 			}
1735d0509082SJacques Vidrine 			strlcpy(bp, tbuf, ep - bp);
1736a42af91cSHajimu UMEMOTO 			canonname = bp;
1737a42af91cSHajimu UMEMOTO 			bp += n;
1738a42af91cSHajimu UMEMOTO 			continue;
1739a42af91cSHajimu UMEMOTO 		}
1740a42af91cSHajimu UMEMOTO 		if (qtype == T_ANY) {
1741a42af91cSHajimu UMEMOTO 			if (!(type == T_A || type == T_AAAA)) {
1742a42af91cSHajimu UMEMOTO 				cp += n;
1743a42af91cSHajimu UMEMOTO 				continue;
1744a42af91cSHajimu UMEMOTO 			}
1745a42af91cSHajimu UMEMOTO 		} else if (type != qtype) {
1746a42af91cSHajimu UMEMOTO #ifdef DEBUG
1747a42af91cSHajimu UMEMOTO 			if (type != T_KEY && type != T_SIG)
1748a42af91cSHajimu UMEMOTO 				syslog(LOG_NOTICE|LOG_AUTH,
1749a42af91cSHajimu UMEMOTO 	       "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
1750a42af91cSHajimu UMEMOTO 				       qname, p_class(C_IN), p_type(qtype),
1751a42af91cSHajimu UMEMOTO 				       p_type(type));
1752a42af91cSHajimu UMEMOTO #endif
1753a42af91cSHajimu UMEMOTO 			cp += n;
1754a42af91cSHajimu UMEMOTO 			continue;		/* XXX - had_error++ ? */
1755a42af91cSHajimu UMEMOTO 		}
1756a42af91cSHajimu UMEMOTO 		switch (type) {
1757a42af91cSHajimu UMEMOTO 		case T_A:
1758a42af91cSHajimu UMEMOTO 		case T_AAAA:
1759a42af91cSHajimu UMEMOTO 			if (strcasecmp(canonname, bp) != 0) {
1760a42af91cSHajimu UMEMOTO #ifdef DEBUG
1761a42af91cSHajimu UMEMOTO 				syslog(LOG_NOTICE|LOG_AUTH,
1762a42af91cSHajimu UMEMOTO 				       AskedForGot, canonname, bp);
1763a42af91cSHajimu UMEMOTO #endif
1764a42af91cSHajimu UMEMOTO 				cp += n;
1765a42af91cSHajimu UMEMOTO 				continue;	/* XXX - had_error++ ? */
1766a42af91cSHajimu UMEMOTO 			}
1767a42af91cSHajimu UMEMOTO 			if (type == T_A && n != INADDRSZ) {
1768a42af91cSHajimu UMEMOTO 				cp += n;
1769a42af91cSHajimu UMEMOTO 				continue;
1770a42af91cSHajimu UMEMOTO 			}
1771a42af91cSHajimu UMEMOTO 			if (type == T_AAAA && n != IN6ADDRSZ) {
1772a42af91cSHajimu UMEMOTO 				cp += n;
1773a42af91cSHajimu UMEMOTO 				continue;
1774a42af91cSHajimu UMEMOTO 			}
1775a42af91cSHajimu UMEMOTO #ifdef FILTER_V4MAPPED
1776a42af91cSHajimu UMEMOTO 			if (type == T_AAAA) {
1777a42af91cSHajimu UMEMOTO 				struct in6_addr in6;
1778a42af91cSHajimu UMEMOTO 				memcpy(&in6, cp, sizeof(in6));
1779a42af91cSHajimu UMEMOTO 				if (IN6_IS_ADDR_V4MAPPED(&in6)) {
1780a42af91cSHajimu UMEMOTO 					cp += n;
1781a42af91cSHajimu UMEMOTO 					continue;
1782a42af91cSHajimu UMEMOTO 				}
1783a42af91cSHajimu UMEMOTO 			}
1784a42af91cSHajimu UMEMOTO #endif
1785a42af91cSHajimu UMEMOTO 			if (!haveanswer) {
1786a42af91cSHajimu UMEMOTO 				int nn;
1787a42af91cSHajimu UMEMOTO 
1788a42af91cSHajimu UMEMOTO 				canonname = bp;
1789a42af91cSHajimu UMEMOTO 				nn = strlen(bp) + 1;	/* for the \0 */
1790a42af91cSHajimu UMEMOTO 				bp += nn;
1791a42af91cSHajimu UMEMOTO 			}
1792a42af91cSHajimu UMEMOTO 
1793a42af91cSHajimu UMEMOTO 			/* don't overwrite pai */
1794a42af91cSHajimu UMEMOTO 			ai = *pai;
1795a42af91cSHajimu UMEMOTO 			ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
1796a42af91cSHajimu UMEMOTO 			afd = find_afd(ai.ai_family);
1797a42af91cSHajimu UMEMOTO 			if (afd == NULL) {
1798a42af91cSHajimu UMEMOTO 				cp += n;
1799a42af91cSHajimu UMEMOTO 				continue;
1800a42af91cSHajimu UMEMOTO 			}
1801a42af91cSHajimu UMEMOTO 			cur->ai_next = get_ai(&ai, afd, (const char *)cp);
1802a42af91cSHajimu UMEMOTO 			if (cur->ai_next == NULL)
1803a42af91cSHajimu UMEMOTO 				had_error++;
1804a42af91cSHajimu UMEMOTO 			while (cur && cur->ai_next)
1805a42af91cSHajimu UMEMOTO 				cur = cur->ai_next;
1806a42af91cSHajimu UMEMOTO 			cp += n;
1807a42af91cSHajimu UMEMOTO 			break;
1808a42af91cSHajimu UMEMOTO 		default:
1809a42af91cSHajimu UMEMOTO 			abort();
1810a42af91cSHajimu UMEMOTO 		}
1811a42af91cSHajimu UMEMOTO 		if (!had_error)
1812a42af91cSHajimu UMEMOTO 			haveanswer++;
1813a42af91cSHajimu UMEMOTO 	}
1814a42af91cSHajimu UMEMOTO 	if (haveanswer) {
181569b0a4b6SJim Pirzyk #if defined(RESOLVSORT)
181669b0a4b6SJim Pirzyk 		/*
181769b0a4b6SJim Pirzyk 		 * We support only IPv4 address for backward
181869b0a4b6SJim Pirzyk 		 * compatibility against gethostbyname(3).
181969b0a4b6SJim Pirzyk 		 */
182069b0a4b6SJim Pirzyk 		if (_res.nsort && qtype == T_A) {
182169b0a4b6SJim Pirzyk 			if (addr4sort(&sentinel) < 0) {
182269b0a4b6SJim Pirzyk 				freeaddrinfo(sentinel.ai_next);
182369b0a4b6SJim Pirzyk 				h_errno = NO_RECOVERY;
182469b0a4b6SJim Pirzyk 				return NULL;
182569b0a4b6SJim Pirzyk 			}
182669b0a4b6SJim Pirzyk 		}
182769b0a4b6SJim Pirzyk #endif /*RESOLVSORT*/
1828a42af91cSHajimu UMEMOTO 		if (!canonname)
1829a42af91cSHajimu UMEMOTO 			(void)get_canonname(pai, sentinel.ai_next, qname);
1830a42af91cSHajimu UMEMOTO 		else
1831a42af91cSHajimu UMEMOTO 			(void)get_canonname(pai, sentinel.ai_next, canonname);
1832a42af91cSHajimu UMEMOTO 		h_errno = NETDB_SUCCESS;
1833a42af91cSHajimu UMEMOTO 		return sentinel.ai_next;
1834a42af91cSHajimu UMEMOTO 	}
1835a42af91cSHajimu UMEMOTO 
1836a42af91cSHajimu UMEMOTO 	h_errno = NO_RECOVERY;
1837a42af91cSHajimu UMEMOTO 	return NULL;
1838a42af91cSHajimu UMEMOTO }
1839a42af91cSHajimu UMEMOTO 
1840688a5c3eSHajimu UMEMOTO #ifdef RESOLVSORT
1841688a5c3eSHajimu UMEMOTO struct addr_ptr {
1842688a5c3eSHajimu UMEMOTO 	struct addrinfo *ai;
1843688a5c3eSHajimu UMEMOTO 	int aval;
1844688a5c3eSHajimu UMEMOTO };
1845688a5c3eSHajimu UMEMOTO 
1846688a5c3eSHajimu UMEMOTO static int
1847688a5c3eSHajimu UMEMOTO addr4sort(struct addrinfo *sentinel)
1848688a5c3eSHajimu UMEMOTO {
1849688a5c3eSHajimu UMEMOTO 	struct addrinfo *ai;
1850688a5c3eSHajimu UMEMOTO 	struct addr_ptr *addrs, addr;
1851688a5c3eSHajimu UMEMOTO 	struct sockaddr_in *sin;
1852688a5c3eSHajimu UMEMOTO 	int naddrs, i, j;
1853688a5c3eSHajimu UMEMOTO 	int needsort = 0;
1854688a5c3eSHajimu UMEMOTO 
1855688a5c3eSHajimu UMEMOTO 	if (!sentinel)
1856688a5c3eSHajimu UMEMOTO 		return -1;
1857688a5c3eSHajimu UMEMOTO 	naddrs = 0;
1858688a5c3eSHajimu UMEMOTO 	for (ai = sentinel->ai_next; ai; ai = ai->ai_next)
1859688a5c3eSHajimu UMEMOTO 		naddrs++;
1860688a5c3eSHajimu UMEMOTO 	if (naddrs < 2)
1861688a5c3eSHajimu UMEMOTO 		return 0;		/* We don't need sorting. */
1862688a5c3eSHajimu UMEMOTO 	if ((addrs = malloc(sizeof(struct addr_ptr) * naddrs)) == NULL)
1863688a5c3eSHajimu UMEMOTO 		return -1;
1864688a5c3eSHajimu UMEMOTO 	i = 0;
1865688a5c3eSHajimu UMEMOTO 	for (ai = sentinel->ai_next; ai; ai = ai->ai_next) {
1866688a5c3eSHajimu UMEMOTO 		sin = (struct sockaddr_in *)ai->ai_addr;
1867688a5c3eSHajimu UMEMOTO 		for (j = 0; (unsigned)j < _res.nsort; j++) {
1868688a5c3eSHajimu UMEMOTO 			if (_res.sort_list[j].addr.s_addr ==
1869688a5c3eSHajimu UMEMOTO 			    (sin->sin_addr.s_addr & _res.sort_list[j].mask))
1870688a5c3eSHajimu UMEMOTO 				break;
1871688a5c3eSHajimu UMEMOTO 		}
1872688a5c3eSHajimu UMEMOTO 		addrs[i].ai = ai;
1873688a5c3eSHajimu UMEMOTO 		addrs[i].aval = j;
1874688a5c3eSHajimu UMEMOTO 		if (needsort == 0 && i > 0 && j < addrs[i - 1].aval)
1875688a5c3eSHajimu UMEMOTO 			needsort = i;
1876688a5c3eSHajimu UMEMOTO 		i++;
1877688a5c3eSHajimu UMEMOTO 	}
1878688a5c3eSHajimu UMEMOTO 	if (!needsort) {
1879688a5c3eSHajimu UMEMOTO 		free(addrs);
1880688a5c3eSHajimu UMEMOTO 		return 0;
1881688a5c3eSHajimu UMEMOTO 	}
1882688a5c3eSHajimu UMEMOTO 
1883688a5c3eSHajimu UMEMOTO 	while (needsort < naddrs) {
1884688a5c3eSHajimu UMEMOTO 	    for (j = needsort - 1; j >= 0; j--) {
1885688a5c3eSHajimu UMEMOTO 		if (addrs[j].aval > addrs[j+1].aval) {
1886688a5c3eSHajimu UMEMOTO 		    addr = addrs[j];
1887688a5c3eSHajimu UMEMOTO 		    addrs[j] = addrs[j + 1];
1888688a5c3eSHajimu UMEMOTO 		    addrs[j + 1] = addr;
1889688a5c3eSHajimu UMEMOTO 		} else
1890688a5c3eSHajimu UMEMOTO 		    break;
1891688a5c3eSHajimu UMEMOTO 	    }
1892688a5c3eSHajimu UMEMOTO 	    needsort++;
1893688a5c3eSHajimu UMEMOTO 	}
1894688a5c3eSHajimu UMEMOTO 
1895688a5c3eSHajimu UMEMOTO 	ai = sentinel;
1896688a5c3eSHajimu UMEMOTO 	for (i = 0; i < naddrs; ++i) {
1897688a5c3eSHajimu UMEMOTO 		ai->ai_next = addrs[i].ai;
1898688a5c3eSHajimu UMEMOTO 		ai = ai->ai_next;
1899688a5c3eSHajimu UMEMOTO 	}
1900688a5c3eSHajimu UMEMOTO 	ai->ai_next = NULL;
1901688a5c3eSHajimu UMEMOTO 	free(addrs);
1902688a5c3eSHajimu UMEMOTO 	return 0;
1903688a5c3eSHajimu UMEMOTO }
1904688a5c3eSHajimu UMEMOTO #endif /*RESOLVSORT*/
1905688a5c3eSHajimu UMEMOTO 
1906a42af91cSHajimu UMEMOTO /*ARGSUSED*/
1907a42af91cSHajimu UMEMOTO static int
1908248aee62SJacques Vidrine _dns_getaddrinfo(rv, cb_data, ap)
1909248aee62SJacques Vidrine 	void	*rv;
1910248aee62SJacques Vidrine 	void	*cb_data;
1911248aee62SJacques Vidrine 	va_list	 ap;
1912a42af91cSHajimu UMEMOTO {
1913a42af91cSHajimu UMEMOTO 	struct addrinfo *ai;
19145cd588cbSHajimu UMEMOTO 	querybuf *buf, *buf2;
1915a214cd1aSHajimu UMEMOTO 	const char *hostname;
1916248aee62SJacques Vidrine 	const struct addrinfo *pai;
1917a42af91cSHajimu UMEMOTO 	struct addrinfo sentinel, *cur;
1918a42af91cSHajimu UMEMOTO 	struct res_target q, q2;
1919a42af91cSHajimu UMEMOTO 
1920a214cd1aSHajimu UMEMOTO 	hostname = va_arg(ap, char *);
1921248aee62SJacques Vidrine 	pai = va_arg(ap, const struct addrinfo *);
1922248aee62SJacques Vidrine 
1923a42af91cSHajimu UMEMOTO 	memset(&q, 0, sizeof(q2));
1924a42af91cSHajimu UMEMOTO 	memset(&q2, 0, sizeof(q2));
1925a42af91cSHajimu UMEMOTO 	memset(&sentinel, 0, sizeof(sentinel));
1926a42af91cSHajimu UMEMOTO 	cur = &sentinel;
1927a42af91cSHajimu UMEMOTO 
19285cd588cbSHajimu UMEMOTO 	buf = malloc(sizeof(*buf));
19295cd588cbSHajimu UMEMOTO 	if (!buf) {
19305cd588cbSHajimu UMEMOTO 		h_errno = NETDB_INTERNAL;
19315cd588cbSHajimu UMEMOTO 		return NS_NOTFOUND;
19325cd588cbSHajimu UMEMOTO 	}
19335cd588cbSHajimu UMEMOTO 	buf2 = malloc(sizeof(*buf2));
19345cd588cbSHajimu UMEMOTO 	if (!buf2) {
19355cd588cbSHajimu UMEMOTO 		free(buf);
19365cd588cbSHajimu UMEMOTO 		h_errno = NETDB_INTERNAL;
19375cd588cbSHajimu UMEMOTO 		return NS_NOTFOUND;
19385cd588cbSHajimu UMEMOTO 	}
19395cd588cbSHajimu UMEMOTO 
1940a42af91cSHajimu UMEMOTO 	switch (pai->ai_family) {
1941a42af91cSHajimu UMEMOTO 	case AF_UNSPEC:
1942a214cd1aSHajimu UMEMOTO 		q.name = hostname;
1943b826397aSJun-ichiro itojun Hagino 		q.qclass = C_IN;
1944d0d6a2c7SHajimu UMEMOTO 		q.qtype = T_A;
19455cd588cbSHajimu UMEMOTO 		q.answer = buf->buf;
19465cd588cbSHajimu UMEMOTO 		q.anslen = sizeof(buf->buf);
1947a42af91cSHajimu UMEMOTO 		q.next = &q2;
1948a214cd1aSHajimu UMEMOTO 		q2.name = hostname;
1949b826397aSJun-ichiro itojun Hagino 		q2.qclass = C_IN;
1950d0d6a2c7SHajimu UMEMOTO 		q2.qtype = T_AAAA;
19515cd588cbSHajimu UMEMOTO 		q2.answer = buf2->buf;
19525cd588cbSHajimu UMEMOTO 		q2.anslen = sizeof(buf2->buf);
1953a42af91cSHajimu UMEMOTO 		break;
1954a42af91cSHajimu UMEMOTO 	case AF_INET:
1955a214cd1aSHajimu UMEMOTO 		q.name = hostname;
1956b826397aSJun-ichiro itojun Hagino 		q.qclass = C_IN;
1957b826397aSJun-ichiro itojun Hagino 		q.qtype = T_A;
19585cd588cbSHajimu UMEMOTO 		q.answer = buf->buf;
19595cd588cbSHajimu UMEMOTO 		q.anslen = sizeof(buf->buf);
1960a42af91cSHajimu UMEMOTO 		break;
1961a42af91cSHajimu UMEMOTO 	case AF_INET6:
1962a214cd1aSHajimu UMEMOTO 		q.name = hostname;
1963b826397aSJun-ichiro itojun Hagino 		q.qclass = C_IN;
1964b826397aSJun-ichiro itojun Hagino 		q.qtype = T_AAAA;
19655cd588cbSHajimu UMEMOTO 		q.answer = buf->buf;
19665cd588cbSHajimu UMEMOTO 		q.anslen = sizeof(buf->buf);
1967a42af91cSHajimu UMEMOTO 		break;
1968a42af91cSHajimu UMEMOTO 	default:
19695cd588cbSHajimu UMEMOTO 		free(buf);
19705cd588cbSHajimu UMEMOTO 		free(buf2);
1971248aee62SJacques Vidrine 		return NS_UNAVAIL;
1972a42af91cSHajimu UMEMOTO 	}
1973a214cd1aSHajimu UMEMOTO 	if (res_searchN(hostname, &q) < 0) {
19745cd588cbSHajimu UMEMOTO 		free(buf);
19755cd588cbSHajimu UMEMOTO 		free(buf2);
1976248aee62SJacques Vidrine 		return NS_NOTFOUND;
19775cd588cbSHajimu UMEMOTO 	}
1978d0d6a2c7SHajimu UMEMOTO 	/* prefer IPv6 */
1979d0d6a2c7SHajimu UMEMOTO 	if (q.next) {
1980d0d6a2c7SHajimu UMEMOTO 		ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai);
1981a42af91cSHajimu UMEMOTO 		if (ai) {
1982a42af91cSHajimu UMEMOTO 			cur->ai_next = ai;
1983a42af91cSHajimu UMEMOTO 			while (cur && cur->ai_next)
1984a42af91cSHajimu UMEMOTO 				cur = cur->ai_next;
1985a42af91cSHajimu UMEMOTO 		}
1986d0d6a2c7SHajimu UMEMOTO 	}
1987d0d6a2c7SHajimu UMEMOTO 	ai = getanswer(buf, q.n, q.name, q.qtype, pai);
1988a42af91cSHajimu UMEMOTO 	if (ai)
1989a42af91cSHajimu UMEMOTO 		cur->ai_next = ai;
19905cd588cbSHajimu UMEMOTO 	free(buf);
19915cd588cbSHajimu UMEMOTO 	free(buf2);
1992a42af91cSHajimu UMEMOTO 	if (sentinel.ai_next == NULL)
1993a42af91cSHajimu UMEMOTO 		switch (h_errno) {
1994a42af91cSHajimu UMEMOTO 		case HOST_NOT_FOUND:
1995248aee62SJacques Vidrine 			return NS_NOTFOUND;
1996a42af91cSHajimu UMEMOTO 		case TRY_AGAIN:
1997248aee62SJacques Vidrine 			return NS_TRYAGAIN;
1998a42af91cSHajimu UMEMOTO 		default:
1999248aee62SJacques Vidrine 			return NS_UNAVAIL;
2000a42af91cSHajimu UMEMOTO 		}
2001248aee62SJacques Vidrine 	*((struct addrinfo **)rv) = sentinel.ai_next;
2002248aee62SJacques Vidrine 	return NS_SUCCESS;
2003248aee62SJacques Vidrine }
2004248aee62SJacques Vidrine 
2005248aee62SJacques Vidrine static void
20069def31dcSHajimu UMEMOTO _sethtent(FILE **hostf)
2007248aee62SJacques Vidrine {
20089def31dcSHajimu UMEMOTO 	if (!*hostf)
20099def31dcSHajimu UMEMOTO 		*hostf = fopen(_PATH_HOSTS, "r");
2010248aee62SJacques Vidrine 	else
20119def31dcSHajimu UMEMOTO 		rewind(*hostf);
2012248aee62SJacques Vidrine }
2013248aee62SJacques Vidrine 
2014248aee62SJacques Vidrine static void
20159def31dcSHajimu UMEMOTO _endhtent(FILE **hostf)
2016248aee62SJacques Vidrine {
20179def31dcSHajimu UMEMOTO 	if (*hostf) {
20189def31dcSHajimu UMEMOTO 		(void) fclose(*hostf);
20199def31dcSHajimu UMEMOTO 		*hostf = NULL;
2020248aee62SJacques Vidrine 	}
2021a42af91cSHajimu UMEMOTO }
2022a42af91cSHajimu UMEMOTO 
2023a42af91cSHajimu UMEMOTO static struct addrinfo *
20249def31dcSHajimu UMEMOTO _gethtent(FILE **hostf, const char *name, const struct addrinfo *pai)
2025a42af91cSHajimu UMEMOTO {
2026a42af91cSHajimu UMEMOTO 	char *p;
2027a42af91cSHajimu UMEMOTO 	char *cp, *tname, *cname;
2028a42af91cSHajimu UMEMOTO 	struct addrinfo hints, *res0, *res;
2029a42af91cSHajimu UMEMOTO 	int error;
2030a42af91cSHajimu UMEMOTO 	const char *addr;
2031a42af91cSHajimu UMEMOTO 	char hostbuf[8*1024];
2032a42af91cSHajimu UMEMOTO 
20339def31dcSHajimu UMEMOTO 	if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "r")))
2034248aee62SJacques Vidrine 		return (NULL);
2035a42af91cSHajimu UMEMOTO again:
20369def31dcSHajimu UMEMOTO 	if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf)))
2037a42af91cSHajimu UMEMOTO 		return (NULL);
2038a42af91cSHajimu UMEMOTO 	if (*p == '#')
2039a42af91cSHajimu UMEMOTO 		goto again;
20407b6cc404SMaxim Sobolev 	cp = strpbrk(p, "#\n");
20417b6cc404SMaxim Sobolev 	if (cp != NULL)
2042a42af91cSHajimu UMEMOTO 		*cp = '\0';
2043a42af91cSHajimu UMEMOTO 	if (!(cp = strpbrk(p, " \t")))
2044a42af91cSHajimu UMEMOTO 		goto again;
2045a42af91cSHajimu UMEMOTO 	*cp++ = '\0';
2046a42af91cSHajimu UMEMOTO 	addr = p;
2047a42af91cSHajimu UMEMOTO 	cname = NULL;
2048a42af91cSHajimu UMEMOTO 	/* if this is not something we're looking for, skip it. */
2049a42af91cSHajimu UMEMOTO 	while (cp && *cp) {
2050a42af91cSHajimu UMEMOTO 		if (*cp == ' ' || *cp == '\t') {
2051a42af91cSHajimu UMEMOTO 			cp++;
2052a42af91cSHajimu UMEMOTO 			continue;
2053a42af91cSHajimu UMEMOTO 		}
2054a42af91cSHajimu UMEMOTO 		tname = cp;
2055a42af91cSHajimu UMEMOTO 		if (cname == NULL)
2056a42af91cSHajimu UMEMOTO 			cname = cp;
2057a42af91cSHajimu UMEMOTO 		if ((cp = strpbrk(cp, " \t")) != NULL)
2058a42af91cSHajimu UMEMOTO 			*cp++ = '\0';
2059a42af91cSHajimu UMEMOTO 		if (strcasecmp(name, tname) == 0)
2060a42af91cSHajimu UMEMOTO 			goto found;
2061a42af91cSHajimu UMEMOTO 	}
2062a42af91cSHajimu UMEMOTO 	goto again;
2063a42af91cSHajimu UMEMOTO 
2064a42af91cSHajimu UMEMOTO found:
2065dd521ef1SHajimu UMEMOTO 	/* we should not glob socktype/protocol here */
2066dd521ef1SHajimu UMEMOTO 	memset(&hints, 0, sizeof(hints));
2067dd521ef1SHajimu UMEMOTO 	hints.ai_family = pai->ai_family;
2068dd521ef1SHajimu UMEMOTO 	hints.ai_socktype = SOCK_DGRAM;
2069dd521ef1SHajimu UMEMOTO 	hints.ai_protocol = 0;
2070a42af91cSHajimu UMEMOTO 	hints.ai_flags = AI_NUMERICHOST;
2071dd521ef1SHajimu UMEMOTO 	error = getaddrinfo(addr, "0", &hints, &res0);
2072a42af91cSHajimu UMEMOTO 	if (error)
2073a42af91cSHajimu UMEMOTO 		goto again;
2074a42af91cSHajimu UMEMOTO #ifdef FILTER_V4MAPPED
2075a42af91cSHajimu UMEMOTO 	/* XXX should check all items in the chain */
2076a42af91cSHajimu UMEMOTO 	if (res0->ai_family == AF_INET6 &&
2077a42af91cSHajimu UMEMOTO 	    IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)res0->ai_addr)->sin6_addr)) {
2078a42af91cSHajimu UMEMOTO 		freeaddrinfo(res0);
2079a42af91cSHajimu UMEMOTO 		goto again;
2080a42af91cSHajimu UMEMOTO 	}
2081a42af91cSHajimu UMEMOTO #endif
2082a42af91cSHajimu UMEMOTO 	for (res = res0; res; res = res->ai_next) {
2083a42af91cSHajimu UMEMOTO 		/* cover it up */
2084a42af91cSHajimu UMEMOTO 		res->ai_flags = pai->ai_flags;
2085dd521ef1SHajimu UMEMOTO 		res->ai_socktype = pai->ai_socktype;
2086dd521ef1SHajimu UMEMOTO 		res->ai_protocol = pai->ai_protocol;
2087a42af91cSHajimu UMEMOTO 
2088a42af91cSHajimu UMEMOTO 		if (pai->ai_flags & AI_CANONNAME) {
2089a42af91cSHajimu UMEMOTO 			if (get_canonname(pai, res, cname) != 0) {
2090a42af91cSHajimu UMEMOTO 				freeaddrinfo(res0);
2091a42af91cSHajimu UMEMOTO 				goto again;
2092a42af91cSHajimu UMEMOTO 			}
2093a42af91cSHajimu UMEMOTO 		}
2094a42af91cSHajimu UMEMOTO 	}
2095a42af91cSHajimu UMEMOTO 	return res0;
2096a42af91cSHajimu UMEMOTO }
2097a42af91cSHajimu UMEMOTO 
2098a42af91cSHajimu UMEMOTO /*ARGSUSED*/
2099a42af91cSHajimu UMEMOTO static int
2100248aee62SJacques Vidrine _files_getaddrinfo(rv, cb_data, ap)
2101248aee62SJacques Vidrine 	void	*rv;
2102248aee62SJacques Vidrine 	void	*cb_data;
2103248aee62SJacques Vidrine 	va_list	 ap;
2104a42af91cSHajimu UMEMOTO {
2105248aee62SJacques Vidrine 	const char *name;
2106248aee62SJacques Vidrine 	const struct addrinfo *pai;
2107a42af91cSHajimu UMEMOTO 	struct addrinfo sentinel, *cur;
2108a42af91cSHajimu UMEMOTO 	struct addrinfo *p;
21099def31dcSHajimu UMEMOTO 	FILE *hostf = NULL;
2110a42af91cSHajimu UMEMOTO 
2111248aee62SJacques Vidrine 	name = va_arg(ap, char *);
2112248aee62SJacques Vidrine 	pai = va_arg(ap, struct addrinfo *);
2113248aee62SJacques Vidrine 
2114248aee62SJacques Vidrine 	memset(&sentinel, 0, sizeof(sentinel));
2115a42af91cSHajimu UMEMOTO 	cur = &sentinel;
2116a42af91cSHajimu UMEMOTO 
21179def31dcSHajimu UMEMOTO 	_sethtent(&hostf);
21189def31dcSHajimu UMEMOTO 	while ((p = _gethtent(&hostf, name, pai)) != NULL) {
2119a42af91cSHajimu UMEMOTO 		cur->ai_next = p;
2120a42af91cSHajimu UMEMOTO 		while (cur && cur->ai_next)
2121a42af91cSHajimu UMEMOTO 			cur = cur->ai_next;
2122a42af91cSHajimu UMEMOTO 	}
21239def31dcSHajimu UMEMOTO 	_endhtent(&hostf);
2124a42af91cSHajimu UMEMOTO 
2125248aee62SJacques Vidrine 	*((struct addrinfo **)rv) = sentinel.ai_next;
2126248aee62SJacques Vidrine 	if (sentinel.ai_next == NULL)
2127248aee62SJacques Vidrine 		return NS_NOTFOUND;
2128248aee62SJacques Vidrine 	return NS_SUCCESS;
2129a42af91cSHajimu UMEMOTO }
2130a42af91cSHajimu UMEMOTO 
2131a42af91cSHajimu UMEMOTO #ifdef YP
2132248aee62SJacques Vidrine /*ARGSUSED*/
2133248aee62SJacques Vidrine static struct addrinfo *
2134248aee62SJacques Vidrine _yphostent(line, pai)
2135248aee62SJacques Vidrine 	char *line;
2136248aee62SJacques Vidrine 	const struct addrinfo *pai;
2137248aee62SJacques Vidrine {
2138248aee62SJacques Vidrine 	struct addrinfo sentinel, *cur;
2139248aee62SJacques Vidrine 	struct addrinfo hints, *res, *res0;
2140248aee62SJacques Vidrine 	int error;
2141248aee62SJacques Vidrine 	char *p = line;
2142248aee62SJacques Vidrine 	const char *addr, *canonname;
2143248aee62SJacques Vidrine 	char *nextline;
2144248aee62SJacques Vidrine 	char *cp;
2145248aee62SJacques Vidrine 
2146248aee62SJacques Vidrine 	addr = canonname = NULL;
2147248aee62SJacques Vidrine 
2148248aee62SJacques Vidrine 	memset(&sentinel, 0, sizeof(sentinel));
2149a42af91cSHajimu UMEMOTO 	cur = &sentinel;
2150a42af91cSHajimu UMEMOTO 
2151248aee62SJacques Vidrine nextline:
2152248aee62SJacques Vidrine 	/* terminate line */
2153248aee62SJacques Vidrine 	cp = strchr(p, '\n');
2154248aee62SJacques Vidrine 	if (cp) {
2155248aee62SJacques Vidrine 		*cp++ = '\0';
2156248aee62SJacques Vidrine 		nextline = cp;
2157248aee62SJacques Vidrine 	} else
2158248aee62SJacques Vidrine 		nextline = NULL;
2159a42af91cSHajimu UMEMOTO 
2160248aee62SJacques Vidrine 	cp = strpbrk(p, " \t");
2161248aee62SJacques Vidrine 	if (cp == NULL) {
2162248aee62SJacques Vidrine 		if (canonname == NULL)
2163248aee62SJacques Vidrine 			return (NULL);
2164248aee62SJacques Vidrine 		else
2165248aee62SJacques Vidrine 			goto done;
2166a42af91cSHajimu UMEMOTO 	}
2167248aee62SJacques Vidrine 	*cp++ = '\0';
2168a42af91cSHajimu UMEMOTO 
2169248aee62SJacques Vidrine 	addr = p;
2170a42af91cSHajimu UMEMOTO 
2171248aee62SJacques Vidrine 	while (cp && *cp) {
2172248aee62SJacques Vidrine 		if (*cp == ' ' || *cp == '\t') {
2173248aee62SJacques Vidrine 			cp++;
2174a42af91cSHajimu UMEMOTO 			continue;
2175248aee62SJacques Vidrine 		}
2176248aee62SJacques Vidrine 		if (!canonname)
2177248aee62SJacques Vidrine 			canonname = cp;
2178248aee62SJacques Vidrine 		if ((cp = strpbrk(cp, " \t")) != NULL)
2179248aee62SJacques Vidrine 			*cp++ = '\0';
2180a42af91cSHajimu UMEMOTO 	}
2181a42af91cSHajimu UMEMOTO 
2182bf838688SHajimu UMEMOTO 	hints = *pai;
2183248aee62SJacques Vidrine 	hints.ai_flags = AI_NUMERICHOST;
2184bf838688SHajimu UMEMOTO 	error = getaddrinfo(addr, NULL, &hints, &res0);
2185248aee62SJacques Vidrine 	if (error == 0) {
2186248aee62SJacques Vidrine 		for (res = res0; res; res = res->ai_next) {
2187248aee62SJacques Vidrine 			/* cover it up */
2188248aee62SJacques Vidrine 			res->ai_flags = pai->ai_flags;
2189248aee62SJacques Vidrine 
2190248aee62SJacques Vidrine 			if (pai->ai_flags & AI_CANONNAME)
2191248aee62SJacques Vidrine 				(void)get_canonname(pai, res, canonname);
2192248aee62SJacques Vidrine 		}
2193248aee62SJacques Vidrine 	} else
2194248aee62SJacques Vidrine 		res0 = NULL;
2195248aee62SJacques Vidrine 	if (res0) {
2196248aee62SJacques Vidrine 		cur->ai_next = res0;
2197a42af91cSHajimu UMEMOTO 		while (cur && cur->ai_next)
2198a42af91cSHajimu UMEMOTO 			cur = cur->ai_next;
2199a42af91cSHajimu UMEMOTO 	}
2200a42af91cSHajimu UMEMOTO 
2201248aee62SJacques Vidrine 	if (nextline) {
2202248aee62SJacques Vidrine 		p = nextline;
2203248aee62SJacques Vidrine 		goto nextline;
2204248aee62SJacques Vidrine 	}
2205a42af91cSHajimu UMEMOTO 
2206248aee62SJacques Vidrine done:
2207248aee62SJacques Vidrine 	return sentinel.ai_next;
2208248aee62SJacques Vidrine }
2209248aee62SJacques Vidrine 
2210248aee62SJacques Vidrine /*ARGSUSED*/
2211248aee62SJacques Vidrine static int
2212248aee62SJacques Vidrine _yp_getaddrinfo(rv, cb_data, ap)
2213248aee62SJacques Vidrine 	void	*rv;
2214248aee62SJacques Vidrine 	void	*cb_data;
2215248aee62SJacques Vidrine 	va_list	 ap;
2216248aee62SJacques Vidrine {
2217248aee62SJacques Vidrine 	struct addrinfo sentinel, *cur;
2218248aee62SJacques Vidrine 	struct addrinfo *ai = NULL;
2219d7057edbSHajimu UMEMOTO 	char *ypbuf;
2220d7057edbSHajimu UMEMOTO 	int ypbuflen, r;
2221248aee62SJacques Vidrine 	const char *name;
2222248aee62SJacques Vidrine 	const struct addrinfo *pai;
2223d7057edbSHajimu UMEMOTO 	char *ypdomain;
2224d7057edbSHajimu UMEMOTO 
2225d7057edbSHajimu UMEMOTO 	if (_yp_check(&ypdomain) == 0)
2226d7057edbSHajimu UMEMOTO 		return NS_UNAVAIL;
2227248aee62SJacques Vidrine 
2228248aee62SJacques Vidrine 	name = va_arg(ap, char *);
2229248aee62SJacques Vidrine 	pai = va_arg(ap, const struct addrinfo *);
2230248aee62SJacques Vidrine 
2231248aee62SJacques Vidrine 	memset(&sentinel, 0, sizeof(sentinel));
2232248aee62SJacques Vidrine 	cur = &sentinel;
2233248aee62SJacques Vidrine 
2234248aee62SJacques Vidrine 	/* hosts.byname is only for IPv4 (Solaris8) */
2235248aee62SJacques Vidrine 	if (pai->ai_family == PF_UNSPEC || pai->ai_family == PF_INET) {
2236d7057edbSHajimu UMEMOTO 		r = yp_match(ypdomain, "hosts.byname", name,
2237d7057edbSHajimu UMEMOTO 			(int)strlen(name), &ypbuf, &ypbuflen);
2238248aee62SJacques Vidrine 		if (r == 0) {
2239248aee62SJacques Vidrine 			struct addrinfo ai4;
2240248aee62SJacques Vidrine 
2241248aee62SJacques Vidrine 			ai4 = *pai;
2242248aee62SJacques Vidrine 			ai4.ai_family = AF_INET;
2243d7057edbSHajimu UMEMOTO 			ai = _yphostent(ypbuf, &ai4);
2244248aee62SJacques Vidrine 			if (ai) {
2245248aee62SJacques Vidrine 				cur->ai_next = ai;
2246248aee62SJacques Vidrine 				while (cur && cur->ai_next)
2247248aee62SJacques Vidrine 					cur = cur->ai_next;
2248248aee62SJacques Vidrine 			}
2249d7057edbSHajimu UMEMOTO 			free(ypbuf);
2250248aee62SJacques Vidrine 		}
2251f8997ca2SHajimu UMEMOTO 	}
2252248aee62SJacques Vidrine 
2253248aee62SJacques Vidrine 	/* ipnodes.byname can hold both IPv4/v6 */
2254d7057edbSHajimu UMEMOTO 	r = yp_match(ypdomain, "ipnodes.byname", name,
2255d7057edbSHajimu UMEMOTO 		(int)strlen(name), &ypbuf, &ypbuflen);
2256248aee62SJacques Vidrine 	if (r == 0) {
2257d7057edbSHajimu UMEMOTO 		ai = _yphostent(ypbuf, pai);
2258d7057edbSHajimu UMEMOTO 		if (ai)
2259248aee62SJacques Vidrine 			cur->ai_next = ai;
2260d7057edbSHajimu UMEMOTO 		free(ypbuf);
2261248aee62SJacques Vidrine 	}
2262248aee62SJacques Vidrine 
2263248aee62SJacques Vidrine 	if (sentinel.ai_next == NULL) {
2264248aee62SJacques Vidrine 		h_errno = HOST_NOT_FOUND;
2265248aee62SJacques Vidrine 		return NS_NOTFOUND;
2266248aee62SJacques Vidrine 	}
2267248aee62SJacques Vidrine 	*((struct addrinfo **)rv) = sentinel.ai_next;
2268248aee62SJacques Vidrine 	return NS_SUCCESS;
2269a42af91cSHajimu UMEMOTO }
2270a42af91cSHajimu UMEMOTO #endif
2271a42af91cSHajimu UMEMOTO 
2272a42af91cSHajimu UMEMOTO /* resolver logic */
2273a42af91cSHajimu UMEMOTO 
2274159d2a98SHajimu UMEMOTO extern const char *_res_hostalias(const char *, char *, size_t);
2275a42af91cSHajimu UMEMOTO 
2276a42af91cSHajimu UMEMOTO /*
2277a42af91cSHajimu UMEMOTO  * Formulate a normal query, send, and await answer.
2278a42af91cSHajimu UMEMOTO  * Returned answer is placed in supplied buffer "answer".
2279a42af91cSHajimu UMEMOTO  * Perform preliminary check of answer, returning success only
2280a42af91cSHajimu UMEMOTO  * if no error is indicated and the answer count is nonzero.
2281a42af91cSHajimu UMEMOTO  * Return the size of the response on success, -1 on error.
2282a42af91cSHajimu UMEMOTO  * Error number is left in h_errno.
2283a42af91cSHajimu UMEMOTO  *
2284a42af91cSHajimu UMEMOTO  * Caller must parse answer and determine whether it answers the question.
2285a42af91cSHajimu UMEMOTO  */
2286a42af91cSHajimu UMEMOTO static int
2287a42af91cSHajimu UMEMOTO res_queryN(name, target)
2288a42af91cSHajimu UMEMOTO 	const char *name;	/* domain name */
2289a42af91cSHajimu UMEMOTO 	struct res_target *target;
2290a42af91cSHajimu UMEMOTO {
22915cd588cbSHajimu UMEMOTO 	u_char *buf;
2292a42af91cSHajimu UMEMOTO 	HEADER *hp;
2293a42af91cSHajimu UMEMOTO 	int n;
2294a42af91cSHajimu UMEMOTO 	struct res_target *t;
2295a42af91cSHajimu UMEMOTO 	int rcode;
2296a42af91cSHajimu UMEMOTO 	int ancount;
2297a42af91cSHajimu UMEMOTO 
2298a42af91cSHajimu UMEMOTO 	rcode = NOERROR;
2299a42af91cSHajimu UMEMOTO 	ancount = 0;
2300a42af91cSHajimu UMEMOTO 
23015cd588cbSHajimu UMEMOTO 	buf = malloc(MAXPACKET);
23025cd588cbSHajimu UMEMOTO 	if (!buf) {
23035cd588cbSHajimu UMEMOTO 		h_errno = NETDB_INTERNAL;
23045cd588cbSHajimu UMEMOTO 		return -1;
23055cd588cbSHajimu UMEMOTO 	}
23065cd588cbSHajimu UMEMOTO 
2307a42af91cSHajimu UMEMOTO 	for (t = target; t; t = t->next) {
2308a42af91cSHajimu UMEMOTO 		int class, type;
2309a42af91cSHajimu UMEMOTO 		u_char *answer;
2310a42af91cSHajimu UMEMOTO 		int anslen;
2311a42af91cSHajimu UMEMOTO 
2312a42af91cSHajimu UMEMOTO 		hp = (HEADER *)(void *)t->answer;
2313a42af91cSHajimu UMEMOTO 		hp->rcode = NOERROR;	/* default */
2314a42af91cSHajimu UMEMOTO 
2315a42af91cSHajimu UMEMOTO 		/* make it easier... */
2316b826397aSJun-ichiro itojun Hagino 		class = t->qclass;
2317b826397aSJun-ichiro itojun Hagino 		type = t->qtype;
2318a42af91cSHajimu UMEMOTO 		answer = t->answer;
2319a42af91cSHajimu UMEMOTO 		anslen = t->anslen;
2320a42af91cSHajimu UMEMOTO #ifdef DEBUG
2321a42af91cSHajimu UMEMOTO 		if (_res.options & RES_DEBUG)
2322a42af91cSHajimu UMEMOTO 			printf(";; res_query(%s, %d, %d)\n", name, class, type);
2323a42af91cSHajimu UMEMOTO #endif
2324a42af91cSHajimu UMEMOTO 
2325a42af91cSHajimu UMEMOTO 		n = res_mkquery(QUERY, name, class, type, NULL, 0, NULL,
23265cd588cbSHajimu UMEMOTO 		    buf, MAXPACKET);
2327ce6282dbSHajimu UMEMOTO 		if (n > 0 && (_res.options & RES_USE_EDNS0) != 0)
23285cd588cbSHajimu UMEMOTO 			n = res_opt(n, buf, MAXPACKET, anslen);
2329a42af91cSHajimu UMEMOTO 		if (n <= 0) {
2330a42af91cSHajimu UMEMOTO #ifdef DEBUG
2331a42af91cSHajimu UMEMOTO 			if (_res.options & RES_DEBUG)
2332a42af91cSHajimu UMEMOTO 				printf(";; res_query: mkquery failed\n");
2333a42af91cSHajimu UMEMOTO #endif
23345cd588cbSHajimu UMEMOTO 			free(buf);
2335a42af91cSHajimu UMEMOTO 			h_errno = NO_RECOVERY;
2336a42af91cSHajimu UMEMOTO 			return (n);
2337a42af91cSHajimu UMEMOTO 		}
2338a42af91cSHajimu UMEMOTO 		n = res_send(buf, n, answer, anslen);
2339a42af91cSHajimu UMEMOTO #if 0
2340a42af91cSHajimu UMEMOTO 		if (n < 0) {
2341a42af91cSHajimu UMEMOTO #ifdef DEBUG
2342a42af91cSHajimu UMEMOTO 			if (_res.options & RES_DEBUG)
2343a42af91cSHajimu UMEMOTO 				printf(";; res_query: send error\n");
2344a42af91cSHajimu UMEMOTO #endif
23455cd588cbSHajimu UMEMOTO 			free(buf);
2346a42af91cSHajimu UMEMOTO 			h_errno = TRY_AGAIN;
2347a42af91cSHajimu UMEMOTO 			return (n);
2348a42af91cSHajimu UMEMOTO 		}
2349a42af91cSHajimu UMEMOTO #endif
2350a42af91cSHajimu UMEMOTO 
235154384cf3SJacques Vidrine 		if (n < 0 || n > anslen)
235254384cf3SJacques Vidrine 			hp->rcode = FORMERR; /* XXX not very informative */
235354384cf3SJacques Vidrine 		if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
2354a42af91cSHajimu UMEMOTO 			rcode = hp->rcode;	/* record most recent error */
2355a42af91cSHajimu UMEMOTO #ifdef DEBUG
2356a42af91cSHajimu UMEMOTO 			if (_res.options & RES_DEBUG)
2357dd521ef1SHajimu UMEMOTO 				printf(";; rcode = %u, ancount=%u\n", hp->rcode,
2358a42af91cSHajimu UMEMOTO 				    ntohs(hp->ancount));
2359a42af91cSHajimu UMEMOTO #endif
2360a42af91cSHajimu UMEMOTO 			continue;
2361a42af91cSHajimu UMEMOTO 		}
2362a42af91cSHajimu UMEMOTO 
2363a42af91cSHajimu UMEMOTO 		ancount += ntohs(hp->ancount);
2364a42af91cSHajimu UMEMOTO 
2365a42af91cSHajimu UMEMOTO 		t->n = n;
2366a42af91cSHajimu UMEMOTO 	}
2367a42af91cSHajimu UMEMOTO 
23685cd588cbSHajimu UMEMOTO 	free(buf);
23695cd588cbSHajimu UMEMOTO 
2370a42af91cSHajimu UMEMOTO 	if (ancount == 0) {
2371a42af91cSHajimu UMEMOTO 		switch (rcode) {
2372a42af91cSHajimu UMEMOTO 		case NXDOMAIN:
2373a42af91cSHajimu UMEMOTO 			h_errno = HOST_NOT_FOUND;
2374a42af91cSHajimu UMEMOTO 			break;
2375a42af91cSHajimu UMEMOTO 		case SERVFAIL:
2376a42af91cSHajimu UMEMOTO 			h_errno = TRY_AGAIN;
2377a42af91cSHajimu UMEMOTO 			break;
2378a42af91cSHajimu UMEMOTO 		case NOERROR:
2379a42af91cSHajimu UMEMOTO 			h_errno = NO_DATA;
2380a42af91cSHajimu UMEMOTO 			break;
2381a42af91cSHajimu UMEMOTO 		case FORMERR:
2382a42af91cSHajimu UMEMOTO 		case NOTIMP:
2383a42af91cSHajimu UMEMOTO 		case REFUSED:
2384a42af91cSHajimu UMEMOTO 		default:
2385a42af91cSHajimu UMEMOTO 			h_errno = NO_RECOVERY;
2386a42af91cSHajimu UMEMOTO 			break;
2387a42af91cSHajimu UMEMOTO 		}
2388a42af91cSHajimu UMEMOTO 		return (-1);
2389a42af91cSHajimu UMEMOTO 	}
2390a42af91cSHajimu UMEMOTO 	return (ancount);
2391a42af91cSHajimu UMEMOTO }
2392a42af91cSHajimu UMEMOTO 
2393a42af91cSHajimu UMEMOTO /*
2394a42af91cSHajimu UMEMOTO  * Formulate a normal query, send, and retrieve answer in supplied buffer.
2395a42af91cSHajimu UMEMOTO  * Return the size of the response on success, -1 on error.
2396a42af91cSHajimu UMEMOTO  * If enabled, implement search rules until answer or unrecoverable failure
2397a42af91cSHajimu UMEMOTO  * is detected.  Error code, if any, is left in h_errno.
2398a42af91cSHajimu UMEMOTO  */
2399a42af91cSHajimu UMEMOTO static int
2400a42af91cSHajimu UMEMOTO res_searchN(name, target)
2401a42af91cSHajimu UMEMOTO 	const char *name;	/* domain name */
2402a42af91cSHajimu UMEMOTO 	struct res_target *target;
2403a42af91cSHajimu UMEMOTO {
2404a42af91cSHajimu UMEMOTO 	const char *cp, * const *domain;
2405a42af91cSHajimu UMEMOTO 	HEADER *hp = (HEADER *)(void *)target->answer;	/*XXX*/
2406a42af91cSHajimu UMEMOTO 	u_int dots;
2407a42af91cSHajimu UMEMOTO 	int trailing_dot, ret, saved_herrno;
2408a42af91cSHajimu UMEMOTO 	int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
2409159d2a98SHajimu UMEMOTO 	char abuf[MAXDNAME];
2410a42af91cSHajimu UMEMOTO 
2411a42af91cSHajimu UMEMOTO 	if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
2412a42af91cSHajimu UMEMOTO 		h_errno = NETDB_INTERNAL;
2413a42af91cSHajimu UMEMOTO 		return (-1);
2414a42af91cSHajimu UMEMOTO 	}
2415a42af91cSHajimu UMEMOTO 
2416a42af91cSHajimu UMEMOTO 	errno = 0;
2417a42af91cSHajimu UMEMOTO 	h_errno = HOST_NOT_FOUND;	/* default, if we never query */
2418a42af91cSHajimu UMEMOTO 	dots = 0;
2419a42af91cSHajimu UMEMOTO 	for (cp = name; *cp; cp++)
2420a42af91cSHajimu UMEMOTO 		dots += (*cp == '.');
2421a42af91cSHajimu UMEMOTO 	trailing_dot = 0;
2422a42af91cSHajimu UMEMOTO 	if (cp > name && *--cp == '.')
2423a42af91cSHajimu UMEMOTO 		trailing_dot++;
2424a42af91cSHajimu UMEMOTO 
2425a42af91cSHajimu UMEMOTO 	/*
2426a42af91cSHajimu UMEMOTO 	 * if there aren't any dots, it could be a user-level alias
2427a42af91cSHajimu UMEMOTO 	 */
2428159d2a98SHajimu UMEMOTO 	if (!dots && (cp = _res_hostalias(name, abuf, sizeof(abuf))) != NULL)
2429a42af91cSHajimu UMEMOTO 		return (res_queryN(cp, target));
2430a42af91cSHajimu UMEMOTO 
2431a42af91cSHajimu UMEMOTO 	/*
2432a42af91cSHajimu UMEMOTO 	 * If there are dots in the name already, let's just give it a try
2433a42af91cSHajimu UMEMOTO 	 * 'as is'.  The threshold can be set with the "ndots" option.
2434a42af91cSHajimu UMEMOTO 	 */
2435a42af91cSHajimu UMEMOTO 	saved_herrno = -1;
2436a42af91cSHajimu UMEMOTO 	if (dots >= _res.ndots) {
2437a42af91cSHajimu UMEMOTO 		ret = res_querydomainN(name, NULL, target);
2438a42af91cSHajimu UMEMOTO 		if (ret > 0)
2439a42af91cSHajimu UMEMOTO 			return (ret);
2440a42af91cSHajimu UMEMOTO 		saved_herrno = h_errno;
2441a42af91cSHajimu UMEMOTO 		tried_as_is++;
2442a42af91cSHajimu UMEMOTO 	}
2443a42af91cSHajimu UMEMOTO 
2444a42af91cSHajimu UMEMOTO 	/*
2445a42af91cSHajimu UMEMOTO 	 * We do at least one level of search if
2446a42af91cSHajimu UMEMOTO 	 *	- there is no dot and RES_DEFNAME is set, or
2447a42af91cSHajimu UMEMOTO 	 *	- there is at least one dot, there is no trailing dot,
2448a42af91cSHajimu UMEMOTO 	 *	  and RES_DNSRCH is set.
2449a42af91cSHajimu UMEMOTO 	 */
2450a42af91cSHajimu UMEMOTO 	if ((!dots && (_res.options & RES_DEFNAMES)) ||
2451a42af91cSHajimu UMEMOTO 	    (dots && !trailing_dot && (_res.options & RES_DNSRCH))) {
2452a42af91cSHajimu UMEMOTO 		int done = 0;
2453a42af91cSHajimu UMEMOTO 
2454a42af91cSHajimu UMEMOTO 		for (domain = (const char * const *)_res.dnsrch;
2455a42af91cSHajimu UMEMOTO 		   *domain && !done;
2456a42af91cSHajimu UMEMOTO 		   domain++) {
2457a42af91cSHajimu UMEMOTO 
2458a42af91cSHajimu UMEMOTO 			ret = res_querydomainN(name, *domain, target);
2459a42af91cSHajimu UMEMOTO 			if (ret > 0)
2460a42af91cSHajimu UMEMOTO 				return (ret);
2461a42af91cSHajimu UMEMOTO 
2462a42af91cSHajimu UMEMOTO 			/*
2463a42af91cSHajimu UMEMOTO 			 * If no server present, give up.
2464a42af91cSHajimu UMEMOTO 			 * If name isn't found in this domain,
2465a42af91cSHajimu UMEMOTO 			 * keep trying higher domains in the search list
2466a42af91cSHajimu UMEMOTO 			 * (if that's enabled).
2467a42af91cSHajimu UMEMOTO 			 * On a NO_DATA error, keep trying, otherwise
2468a42af91cSHajimu UMEMOTO 			 * a wildcard entry of another type could keep us
2469a42af91cSHajimu UMEMOTO 			 * from finding this entry higher in the domain.
2470a42af91cSHajimu UMEMOTO 			 * If we get some other error (negative answer or
2471a42af91cSHajimu UMEMOTO 			 * server failure), then stop searching up,
2472a42af91cSHajimu UMEMOTO 			 * but try the input name below in case it's
2473a42af91cSHajimu UMEMOTO 			 * fully-qualified.
2474a42af91cSHajimu UMEMOTO 			 */
2475a42af91cSHajimu UMEMOTO 			if (errno == ECONNREFUSED) {
2476a42af91cSHajimu UMEMOTO 				h_errno = TRY_AGAIN;
2477a42af91cSHajimu UMEMOTO 				return (-1);
2478a42af91cSHajimu UMEMOTO 			}
2479a42af91cSHajimu UMEMOTO 
2480a42af91cSHajimu UMEMOTO 			switch (h_errno) {
2481a42af91cSHajimu UMEMOTO 			case NO_DATA:
2482a42af91cSHajimu UMEMOTO 				got_nodata++;
2483a42af91cSHajimu UMEMOTO 				/* FALLTHROUGH */
2484a42af91cSHajimu UMEMOTO 			case HOST_NOT_FOUND:
2485a42af91cSHajimu UMEMOTO 				/* keep trying */
2486a42af91cSHajimu UMEMOTO 				break;
2487a42af91cSHajimu UMEMOTO 			case TRY_AGAIN:
2488a42af91cSHajimu UMEMOTO 				if (hp->rcode == SERVFAIL) {
2489a42af91cSHajimu UMEMOTO 					/* try next search element, if any */
2490a42af91cSHajimu UMEMOTO 					got_servfail++;
2491a42af91cSHajimu UMEMOTO 					break;
2492a42af91cSHajimu UMEMOTO 				}
2493a42af91cSHajimu UMEMOTO 				/* FALLTHROUGH */
2494a42af91cSHajimu UMEMOTO 			default:
2495a42af91cSHajimu UMEMOTO 				/* anything else implies that we're done */
2496a42af91cSHajimu UMEMOTO 				done++;
2497a42af91cSHajimu UMEMOTO 			}
2498a42af91cSHajimu UMEMOTO 			/*
2499a42af91cSHajimu UMEMOTO 			 * if we got here for some reason other than DNSRCH,
2500a42af91cSHajimu UMEMOTO 			 * we only wanted one iteration of the loop, so stop.
2501a42af91cSHajimu UMEMOTO 			 */
2502a42af91cSHajimu UMEMOTO 			if (!(_res.options & RES_DNSRCH))
2503a42af91cSHajimu UMEMOTO 			        done++;
2504a42af91cSHajimu UMEMOTO 		}
2505a42af91cSHajimu UMEMOTO 	}
2506a42af91cSHajimu UMEMOTO 
2507a42af91cSHajimu UMEMOTO 	/*
2508a42af91cSHajimu UMEMOTO 	 * if we have not already tried the name "as is", do that now.
2509a42af91cSHajimu UMEMOTO 	 * note that we do this regardless of how many dots were in the
2510a42af91cSHajimu UMEMOTO 	 * name or whether it ends with a dot.
2511a42af91cSHajimu UMEMOTO 	 */
2512a42af91cSHajimu UMEMOTO 	if (!tried_as_is && (dots || !(_res.options & RES_NOTLDQUERY))) {
2513a42af91cSHajimu UMEMOTO 		ret = res_querydomainN(name, NULL, target);
2514a42af91cSHajimu UMEMOTO 		if (ret > 0)
2515a42af91cSHajimu UMEMOTO 			return (ret);
2516a42af91cSHajimu UMEMOTO 	}
2517a42af91cSHajimu UMEMOTO 
2518a42af91cSHajimu UMEMOTO 	/*
2519a42af91cSHajimu UMEMOTO 	 * if we got here, we didn't satisfy the search.
2520a42af91cSHajimu UMEMOTO 	 * if we did an initial full query, return that query's h_errno
2521a42af91cSHajimu UMEMOTO 	 * (note that we wouldn't be here if that query had succeeded).
2522a42af91cSHajimu UMEMOTO 	 * else if we ever got a nodata, send that back as the reason.
2523a42af91cSHajimu UMEMOTO 	 * else send back meaningless h_errno, that being the one from
2524a42af91cSHajimu UMEMOTO 	 * the last DNSRCH we did.
2525a42af91cSHajimu UMEMOTO 	 */
2526a42af91cSHajimu UMEMOTO 	if (saved_herrno != -1)
2527a42af91cSHajimu UMEMOTO 		h_errno = saved_herrno;
2528a42af91cSHajimu UMEMOTO 	else if (got_nodata)
2529a42af91cSHajimu UMEMOTO 		h_errno = NO_DATA;
2530a42af91cSHajimu UMEMOTO 	else if (got_servfail)
2531a42af91cSHajimu UMEMOTO 		h_errno = TRY_AGAIN;
2532a42af91cSHajimu UMEMOTO 	return (-1);
2533a42af91cSHajimu UMEMOTO }
2534a42af91cSHajimu UMEMOTO 
2535a42af91cSHajimu UMEMOTO /*
2536a42af91cSHajimu UMEMOTO  * Perform a call on res_query on the concatenation of name and domain,
2537a42af91cSHajimu UMEMOTO  * removing a trailing dot from name if domain is NULL.
2538a42af91cSHajimu UMEMOTO  */
2539a42af91cSHajimu UMEMOTO static int
2540a42af91cSHajimu UMEMOTO res_querydomainN(name, domain, target)
2541a42af91cSHajimu UMEMOTO 	const char *name, *domain;
2542a42af91cSHajimu UMEMOTO 	struct res_target *target;
2543a42af91cSHajimu UMEMOTO {
2544a42af91cSHajimu UMEMOTO 	char nbuf[MAXDNAME];
2545a42af91cSHajimu UMEMOTO 	const char *longname = nbuf;
2546a42af91cSHajimu UMEMOTO 	size_t n, d;
2547a42af91cSHajimu UMEMOTO 
2548a42af91cSHajimu UMEMOTO #ifdef DEBUG
2549a42af91cSHajimu UMEMOTO 	if (_res.options & RES_DEBUG)
2550a42af91cSHajimu UMEMOTO 		printf(";; res_querydomain(%s, %s)\n",
2551a42af91cSHajimu UMEMOTO 			name, domain?domain:"<Nil>");
2552a42af91cSHajimu UMEMOTO #endif
2553a42af91cSHajimu UMEMOTO 	if (domain == NULL) {
2554a42af91cSHajimu UMEMOTO 		/*
2555a42af91cSHajimu UMEMOTO 		 * Check for trailing '.';
2556a42af91cSHajimu UMEMOTO 		 * copy without '.' if present.
2557a42af91cSHajimu UMEMOTO 		 */
2558a42af91cSHajimu UMEMOTO 		n = strlen(name);
2559a42af91cSHajimu UMEMOTO 		if (n >= MAXDNAME) {
2560a42af91cSHajimu UMEMOTO 			h_errno = NO_RECOVERY;
2561a42af91cSHajimu UMEMOTO 			return (-1);
2562a42af91cSHajimu UMEMOTO 		}
2563a42af91cSHajimu UMEMOTO 		if (n > 0 && name[--n] == '.') {
2564a42af91cSHajimu UMEMOTO 			strncpy(nbuf, name, n);
2565a42af91cSHajimu UMEMOTO 			nbuf[n] = '\0';
2566a42af91cSHajimu UMEMOTO 		} else
2567a42af91cSHajimu UMEMOTO 			longname = name;
2568a42af91cSHajimu UMEMOTO 	} else {
2569a42af91cSHajimu UMEMOTO 		n = strlen(name);
2570a42af91cSHajimu UMEMOTO 		d = strlen(domain);
2571a42af91cSHajimu UMEMOTO 		if (n + d + 1 >= MAXDNAME) {
2572a42af91cSHajimu UMEMOTO 			h_errno = NO_RECOVERY;
2573a42af91cSHajimu UMEMOTO 			return (-1);
2574a42af91cSHajimu UMEMOTO 		}
2575dd521ef1SHajimu UMEMOTO 		snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
2576a42af91cSHajimu UMEMOTO 	}
2577a42af91cSHajimu UMEMOTO 	return (res_queryN(longname, target));
2578a42af91cSHajimu UMEMOTO }
2579