xref: /freebsd/lib/libc/net/getaddrinfo.c (revision ea24ff473440cdc0cf55d922dee0af72a21bb889)
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  * Issues to be discussed:
347d56d374SYoshinobu Inoue  * - Return values.  There are nonstandard return values defined and used
357d56d374SYoshinobu Inoue  *   in the source code.  This is because RFC2553 is silent about which error
367d56d374SYoshinobu Inoue  *   code must be returned for which situation.
37a42af91cSHajimu UMEMOTO  * - freeaddrinfo(NULL).  RFC2553 is silent about it.  XNET 5.2 says it is
38dd521ef1SHajimu UMEMOTO  *   invalid.  current code - SEGV on freeaddrinfo(NULL)
39dd521ef1SHajimu UMEMOTO  *
4000a8a579SYoshinobu Inoue  * Note:
4100a8a579SYoshinobu Inoue  * - The code filters out AFs that are not supported by the kernel,
4200a8a579SYoshinobu Inoue  *   when globbing NULL hostname (to loopback, or wildcard).  Is it the right
4300a8a579SYoshinobu Inoue  *   thing to do?  What is the relationship with post-RFC2553 AI_ADDRCONFIG
4400a8a579SYoshinobu Inoue  *   in ai_flags?
45a42af91cSHajimu UMEMOTO  * - (post-2553) semantics of AI_ADDRCONFIG itself is too vague.
46a42af91cSHajimu UMEMOTO  *   (1) what should we do against numeric hostname (2) what should we do
47a42af91cSHajimu UMEMOTO  *   against NULL hostname (3) what is AI_ADDRCONFIG itself.  AF not ready?
48a42af91cSHajimu UMEMOTO  *   non-loopback address configured?  global address configured?
49dd521ef1SHajimu UMEMOTO  *
50dd521ef1SHajimu UMEMOTO  * OS specific notes for freebsd4:
51dd521ef1SHajimu UMEMOTO  * - FreeBSD supported $GAI.  The code does not.
527d56d374SYoshinobu Inoue  */
537d56d374SYoshinobu Inoue 
54333fc21eSDavid E. O'Brien #include <sys/cdefs.h>
55333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$");
56333fc21eSDavid E. O'Brien 
57d201fe46SDaniel Eischen #include "namespace.h"
587d56d374SYoshinobu Inoue #include <sys/types.h>
597d56d374SYoshinobu Inoue #include <sys/param.h>
607d56d374SYoshinobu Inoue #include <sys/socket.h>
617d56d374SYoshinobu Inoue #include <net/if.h>
627d56d374SYoshinobu Inoue #include <netinet/in.h>
636f63a114SHajimu UMEMOTO #include <net/if_types.h>
646f63a114SHajimu UMEMOTO #include <ifaddrs.h>
654c6867a8SHajimu UMEMOTO #include <sys/queue.h>
664c6867a8SHajimu UMEMOTO #ifdef INET6
674c6867a8SHajimu UMEMOTO #include <sys/sysctl.h>
684d489f47SHajimu UMEMOTO #include <sys/ioctl.h>
696f63a114SHajimu UMEMOTO #include <netinet6/in6_var.h>
706f63a114SHajimu UMEMOTO #include <netinet6/nd6.h>
714c6867a8SHajimu UMEMOTO #endif
727d56d374SYoshinobu Inoue #include <arpa/inet.h>
737d56d374SYoshinobu Inoue #include <arpa/nameser.h>
74bf838688SHajimu UMEMOTO #include <rpc/rpc.h>
75bf838688SHajimu UMEMOTO #include <rpcsvc/yp_prot.h>
76bf838688SHajimu UMEMOTO #include <rpcsvc/ypclnt.h>
777d56d374SYoshinobu Inoue #include <netdb.h>
787d56d374SYoshinobu Inoue #include <resolv.h>
797d56d374SYoshinobu Inoue #include <string.h>
807d56d374SYoshinobu Inoue #include <stdlib.h>
817d56d374SYoshinobu Inoue #include <stddef.h>
827d56d374SYoshinobu Inoue #include <ctype.h>
837d56d374SYoshinobu Inoue #include <unistd.h>
847d56d374SYoshinobu Inoue #include <stdio.h>
85a42af91cSHajimu UMEMOTO #include <errno.h>
8669b0a4b6SJim Pirzyk 
8769b0a4b6SJim Pirzyk #include "res_config.h"
8869b0a4b6SJim Pirzyk 
89ce6282dbSHajimu UMEMOTO #ifdef DEBUG
90ce6282dbSHajimu UMEMOTO #include <syslog.h>
91ce6282dbSHajimu UMEMOTO #endif
927d56d374SYoshinobu Inoue 
93248aee62SJacques Vidrine #include <stdarg.h>
94248aee62SJacques Vidrine #include <nsswitch.h>
95d201fe46SDaniel Eischen #include "un-namespace.h"
962bbd7cf8SJacques Vidrine #include "libc_private.h"
9706a99fe3SHajimu UMEMOTO #ifdef NS_CACHING
9806a99fe3SHajimu UMEMOTO #include "nscache.h"
9906a99fe3SHajimu UMEMOTO #endif
100248aee62SJacques Vidrine 
1017d56d374SYoshinobu Inoue #define ANY 0
1027d56d374SYoshinobu Inoue #define YES 1
1037d56d374SYoshinobu Inoue #define NO  0
1047d56d374SYoshinobu Inoue 
1057d56d374SYoshinobu Inoue static const char in_addrany[] = { 0, 0, 0, 0 };
106dd521ef1SHajimu UMEMOTO static const char in_loopback[] = { 127, 0, 0, 1 };
107dd521ef1SHajimu UMEMOTO #ifdef INET6
1087d56d374SYoshinobu Inoue static const char in6_addrany[] = {
1097d56d374SYoshinobu Inoue 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1107d56d374SYoshinobu Inoue };
1117d56d374SYoshinobu Inoue static const char in6_loopback[] = {
1127d56d374SYoshinobu Inoue 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
1137d56d374SYoshinobu Inoue };
114dd521ef1SHajimu UMEMOTO #endif
1157d56d374SYoshinobu Inoue 
1164c6867a8SHajimu UMEMOTO struct policyqueue {
1174c6867a8SHajimu UMEMOTO 	TAILQ_ENTRY(policyqueue) pc_entry;
1184c6867a8SHajimu UMEMOTO #ifdef INET6
1194c6867a8SHajimu UMEMOTO 	struct in6_addrpolicy pc_policy;
1204c6867a8SHajimu UMEMOTO #endif
1214c6867a8SHajimu UMEMOTO };
1224c6867a8SHajimu UMEMOTO TAILQ_HEAD(policyhead, policyqueue);
1234c6867a8SHajimu UMEMOTO 
1247d56d374SYoshinobu Inoue static const struct afd {
1257d56d374SYoshinobu Inoue 	int a_af;
1267d56d374SYoshinobu Inoue 	int a_addrlen;
127036ae3ddSHajimu UMEMOTO 	socklen_t a_socklen;
1287d56d374SYoshinobu Inoue 	int a_off;
1297d56d374SYoshinobu Inoue 	const char *a_addrany;
1307d56d374SYoshinobu Inoue 	const char *a_loopback;
1317d56d374SYoshinobu Inoue 	int a_scoped;
1327d56d374SYoshinobu Inoue } afdl [] = {
1337d56d374SYoshinobu Inoue #ifdef INET6
1347d56d374SYoshinobu Inoue #define	N_INET6 0
1357d56d374SYoshinobu Inoue 	{PF_INET6, sizeof(struct in6_addr),
1367d56d374SYoshinobu Inoue 	 sizeof(struct sockaddr_in6),
1377d56d374SYoshinobu Inoue 	 offsetof(struct sockaddr_in6, sin6_addr),
1387d56d374SYoshinobu Inoue 	 in6_addrany, in6_loopback, 1},
1397d56d374SYoshinobu Inoue #define	N_INET 1
140bb957021SHiroki Sato #define	N_LOCAL 2
1417d56d374SYoshinobu Inoue #else
1427d56d374SYoshinobu Inoue #define	N_INET 0
143bb957021SHiroki Sato #define	N_LOCAL 1
1447d56d374SYoshinobu Inoue #endif
1457d56d374SYoshinobu Inoue 	{PF_INET, sizeof(struct in_addr),
1467d56d374SYoshinobu Inoue 	 sizeof(struct sockaddr_in),
1477d56d374SYoshinobu Inoue 	 offsetof(struct sockaddr_in, sin_addr),
1487d56d374SYoshinobu Inoue 	 in_addrany, in_loopback, 0},
149bb957021SHiroki Sato #define	sizeofmember(type, member)	(sizeof(((type *)0)->member))
150bb957021SHiroki Sato 	{PF_LOCAL, sizeofmember(struct sockaddr_un, sun_path),
151bb957021SHiroki Sato 	 sizeof(struct sockaddr_un),
152bb957021SHiroki Sato 	 offsetof(struct sockaddr_un, sun_path),
153bb957021SHiroki Sato 	 NULL, NULL, 0},
1547d56d374SYoshinobu Inoue 	{0, 0, 0, 0, NULL, NULL, 0},
1557d56d374SYoshinobu Inoue };
1567d56d374SYoshinobu Inoue 
1577d56d374SYoshinobu Inoue struct explore {
158a42af91cSHajimu UMEMOTO 	int e_af;
1597d56d374SYoshinobu Inoue 	int e_socktype;
1607d56d374SYoshinobu Inoue 	int e_protocol;
1617d56d374SYoshinobu Inoue 	int e_wild;
162bb957021SHiroki Sato #define	AF_ANY		0x01
163bb957021SHiroki Sato #define	SOCKTYPE_ANY	0x02
164bb957021SHiroki Sato #define	PROTOCOL_ANY	0x04
165bb957021SHiroki Sato #define WILD_AF(ex)		((ex)->e_wild & AF_ANY)
166bb957021SHiroki Sato #define WILD_SOCKTYPE(ex)	((ex)->e_wild & SOCKTYPE_ANY)
167bb957021SHiroki Sato #define WILD_PROTOCOL(ex)	((ex)->e_wild & PROTOCOL_ANY)
1687d56d374SYoshinobu Inoue };
1697d56d374SYoshinobu Inoue 
1707d56d374SYoshinobu Inoue static const struct explore explore[] = {
171a42af91cSHajimu UMEMOTO #ifdef INET6
172bb957021SHiroki Sato 	{ PF_INET6, SOCK_DGRAM,	 IPPROTO_UDP,
173bb957021SHiroki Sato 	    AF_ANY | SOCKTYPE_ANY | PROTOCOL_ANY },
174bb957021SHiroki Sato 	{ PF_INET6, SOCK_STREAM, IPPROTO_TCP,
175bb957021SHiroki Sato 	    AF_ANY | SOCKTYPE_ANY | PROTOCOL_ANY },
176bb957021SHiroki Sato 	{ PF_INET6, SOCK_STREAM, IPPROTO_SCTP,
177bb957021SHiroki Sato 	    AF_ANY | SOCKTYPE_ANY },
178bb957021SHiroki Sato 	{ PF_INET6, SOCK_SEQPACKET, IPPROTO_SCTP,
179bb957021SHiroki Sato 	    AF_ANY | SOCKTYPE_ANY | PROTOCOL_ANY },
180bb957021SHiroki Sato 	{ PF_INET6, SOCK_DGRAM,	IPPROTO_UDPLITE,
181bb957021SHiroki Sato 	    AF_ANY | SOCKTYPE_ANY },
182bb957021SHiroki Sato 	{ PF_INET6, SOCK_RAW, ANY,
183bb957021SHiroki Sato 	    AF_ANY | PROTOCOL_ANY },
184a42af91cSHajimu UMEMOTO #endif
185bb957021SHiroki Sato 	{ PF_INET, SOCK_DGRAM, IPPROTO_UDP,
186bb957021SHiroki Sato 	    AF_ANY | SOCKTYPE_ANY | PROTOCOL_ANY },
187bb957021SHiroki Sato 	{ PF_INET, SOCK_STREAM,	IPPROTO_TCP,
188bb957021SHiroki Sato 	    AF_ANY | SOCKTYPE_ANY | PROTOCOL_ANY },
189bb957021SHiroki Sato 	{ PF_INET, SOCK_STREAM, IPPROTO_SCTP,
190bb957021SHiroki Sato 	    AF_ANY | SOCKTYPE_ANY },
191bb957021SHiroki Sato 	{ PF_INET, SOCK_SEQPACKET, IPPROTO_SCTP,
192bb957021SHiroki Sato 	    AF_ANY | SOCKTYPE_ANY | PROTOCOL_ANY },
193bb957021SHiroki Sato 	{ PF_INET, SOCK_DGRAM, IPPROTO_UDPLITE,
194bb957021SHiroki Sato 	    AF_ANY | SOCKTYPE_ANY },
195bb957021SHiroki Sato 	{ PF_INET, SOCK_RAW, ANY,
196bb957021SHiroki Sato 	    AF_ANY | PROTOCOL_ANY },
1974c60a05dSHiroki Sato 	{ PF_LOCAL, SOCK_DGRAM,	ANY,
1984c60a05dSHiroki Sato 	    AF_ANY | SOCKTYPE_ANY | PROTOCOL_ANY },
1994c60a05dSHiroki Sato 	{ PF_LOCAL, SOCK_STREAM, ANY,
2004c60a05dSHiroki Sato 	    AF_ANY | SOCKTYPE_ANY | PROTOCOL_ANY },
2014c60a05dSHiroki Sato 	{ PF_LOCAL, SOCK_SEQPACKET, ANY,
2024c60a05dSHiroki Sato 	    AF_ANY | SOCKTYPE_ANY | PROTOCOL_ANY },
2032bd56eacSJilles Tjoelker 	{ -1, 0, 0, 0 },
2047d56d374SYoshinobu Inoue };
2057d56d374SYoshinobu Inoue 
2067d56d374SYoshinobu Inoue #ifdef INET6
2077d56d374SYoshinobu Inoue #define PTON_MAX	16
2087d56d374SYoshinobu Inoue #else
2097d56d374SYoshinobu Inoue #define PTON_MAX	4
2107d56d374SYoshinobu Inoue #endif
2117d56d374SYoshinobu Inoue 
2124c6867a8SHajimu UMEMOTO #define AIO_SRCFLAG_DEPRECATED	0x1
2134c6867a8SHajimu UMEMOTO 
2144c6867a8SHajimu UMEMOTO struct ai_order {
2154c6867a8SHajimu UMEMOTO 	union {
2164c6867a8SHajimu UMEMOTO 		struct sockaddr_storage aiou_ss;
2174c6867a8SHajimu UMEMOTO 		struct sockaddr aiou_sa;
2184c6867a8SHajimu UMEMOTO 	} aio_src_un;
2194c6867a8SHajimu UMEMOTO #define aio_srcsa aio_src_un.aiou_sa
2204c6867a8SHajimu UMEMOTO 	u_int32_t aio_srcflag;
2214c6867a8SHajimu UMEMOTO 	int aio_srcscope;
2224c6867a8SHajimu UMEMOTO 	int aio_dstscope;
2234c6867a8SHajimu UMEMOTO 	struct policyqueue *aio_srcpolicy;
2244c6867a8SHajimu UMEMOTO 	struct policyqueue *aio_dstpolicy;
2254c6867a8SHajimu UMEMOTO 	struct addrinfo *aio_ai;
2264c6867a8SHajimu UMEMOTO 	int aio_matchlen;
2274c6867a8SHajimu UMEMOTO };
2284c6867a8SHajimu UMEMOTO 
229248aee62SJacques Vidrine static const ns_src default_dns_files[] = {
230248aee62SJacques Vidrine 	{ NSSRC_FILES, 	NS_SUCCESS },
231248aee62SJacques Vidrine 	{ NSSRC_DNS, 	NS_SUCCESS },
232248aee62SJacques Vidrine 	{ 0 }
233248aee62SJacques Vidrine };
234248aee62SJacques Vidrine 
235a42af91cSHajimu UMEMOTO struct res_target {
236a42af91cSHajimu UMEMOTO 	struct res_target *next;
237a42af91cSHajimu UMEMOTO 	const char *name;	/* domain name */
238b826397aSJun-ichiro itojun Hagino 	int qclass, qtype;	/* class and type of query */
239a42af91cSHajimu UMEMOTO 	u_char *answer;		/* buffer to put answer */
240a42af91cSHajimu UMEMOTO 	int anslen;		/* size of answer buffer */
241a42af91cSHajimu UMEMOTO 	int n;			/* result length */
242a42af91cSHajimu UMEMOTO };
243a42af91cSHajimu UMEMOTO 
244688a5c3eSHajimu UMEMOTO #define MAXPACKET	(64*1024)
245688a5c3eSHajimu UMEMOTO 
246688a5c3eSHajimu UMEMOTO typedef union {
247688a5c3eSHajimu UMEMOTO 	HEADER hdr;
248688a5c3eSHajimu UMEMOTO 	u_char buf[MAXPACKET];
249688a5c3eSHajimu UMEMOTO } querybuf;
250688a5c3eSHajimu UMEMOTO 
251e24e9d99SHajimu UMEMOTO static int str2number(const char *, int *);
252bc421492SHajimu UMEMOTO static int explore_copy(const struct addrinfo *, const struct addrinfo *,
253bc421492SHajimu UMEMOTO 	struct addrinfo **);
2541372519bSDavid E. O'Brien static int explore_null(const struct addrinfo *,
2551372519bSDavid E. O'Brien 	const char *, struct addrinfo **);
2561372519bSDavid E. O'Brien static int explore_numeric(const struct addrinfo *, const char *,
2578755b918SHajimu UMEMOTO 	const char *, struct addrinfo **, const char *);
2581372519bSDavid E. O'Brien static int explore_numeric_scope(const struct addrinfo *, const char *,
2591372519bSDavid E. O'Brien 	const char *, struct addrinfo **);
2601372519bSDavid E. O'Brien static int get_canonname(const struct addrinfo *,
2611372519bSDavid E. O'Brien 	struct addrinfo *, const char *);
2621372519bSDavid E. O'Brien static struct addrinfo *get_ai(const struct addrinfo *,
2631372519bSDavid E. O'Brien 	const struct afd *, const char *);
264bc421492SHajimu UMEMOTO static struct addrinfo *copy_ai(const struct addrinfo *);
265c05ac53bSDavid E. O'Brien static int get_portmatch(const struct addrinfo *, const char *);
266c05ac53bSDavid E. O'Brien static int get_port(struct addrinfo *, const char *, int);
267c05ac53bSDavid E. O'Brien static const struct afd *find_afd(int);
268bf838688SHajimu UMEMOTO static int addrconfig(struct addrinfo *);
2696f63a114SHajimu UMEMOTO #ifdef INET6
2706f63a114SHajimu UMEMOTO static int is_ifdisabled(char *);
2716f63a114SHajimu UMEMOTO #endif
2724d489f47SHajimu UMEMOTO static void set_source(struct ai_order *, struct policyhead *);
2734c6867a8SHajimu UMEMOTO static int comp_dst(const void *, const void *);
274a42af91cSHajimu UMEMOTO #ifdef INET6
275a89c1d30SHajimu UMEMOTO static int ip6_str2scopeid(char *, struct sockaddr_in6 *, u_int32_t *);
276a42af91cSHajimu UMEMOTO #endif
2774c6867a8SHajimu UMEMOTO static int gai_addr2scopetype(struct sockaddr *);
278a42af91cSHajimu UMEMOTO 
279688a5c3eSHajimu UMEMOTO static int explore_fqdn(const struct addrinfo *, const char *,
280688a5c3eSHajimu UMEMOTO 	const char *, struct addrinfo **);
281688a5c3eSHajimu UMEMOTO 
2824c6867a8SHajimu UMEMOTO static int reorder(struct addrinfo *);
2834c6867a8SHajimu UMEMOTO static int get_addrselectpolicy(struct policyhead *);
2844c6867a8SHajimu UMEMOTO static void free_addrselectpolicy(struct policyhead *);
2854c6867a8SHajimu UMEMOTO static struct policyqueue *match_addrselectpolicy(struct sockaddr *,
2864c6867a8SHajimu UMEMOTO 	struct policyhead *);
2874d489f47SHajimu UMEMOTO static int matchlen(struct sockaddr *, struct sockaddr *);
2884c6867a8SHajimu UMEMOTO 
2891372519bSDavid E. O'Brien static struct addrinfo *getanswer(const querybuf *, int, const char *, int,
2905342d17fSHajimu UMEMOTO 	const struct addrinfo *, res_state);
291688a5c3eSHajimu UMEMOTO #if defined(RESOLVSORT)
2925342d17fSHajimu UMEMOTO static int addr4sort(struct addrinfo *, res_state);
293688a5c3eSHajimu UMEMOTO #endif
294a89c1d30SHajimu UMEMOTO static int _dns_getaddrinfo(void *, void *, va_list);
2959def31dcSHajimu UMEMOTO static void _sethtent(FILE **);
2969def31dcSHajimu UMEMOTO static void _endhtent(FILE **);
2979def31dcSHajimu UMEMOTO static struct addrinfo *_gethtent(FILE **, const char *,
2989def31dcSHajimu UMEMOTO 	const struct addrinfo *);
299c05ac53bSDavid E. O'Brien static int _files_getaddrinfo(void *, void *, va_list);
300a42af91cSHajimu UMEMOTO #ifdef YP
301c05ac53bSDavid E. O'Brien static struct addrinfo *_yphostent(char *, const struct addrinfo *);
302c05ac53bSDavid E. O'Brien static int _yp_getaddrinfo(void *, void *, va_list);
303a42af91cSHajimu UMEMOTO #endif
30406a99fe3SHajimu UMEMOTO #ifdef NS_CACHING
30506a99fe3SHajimu UMEMOTO static int addrinfo_id_func(char *, size_t *, va_list, void *);
30606a99fe3SHajimu UMEMOTO static int addrinfo_marshal_func(char *, size_t *, void *, va_list, void *);
30706a99fe3SHajimu UMEMOTO static int addrinfo_unmarshal_func(char *, size_t, void *, va_list, void *);
30806a99fe3SHajimu UMEMOTO #endif
309a42af91cSHajimu UMEMOTO 
3105342d17fSHajimu UMEMOTO static int res_queryN(const char *, struct res_target *, res_state);
3115342d17fSHajimu UMEMOTO static int res_searchN(const char *, struct res_target *, res_state);
3121372519bSDavid E. O'Brien static int res_querydomainN(const char *, const char *,
3135342d17fSHajimu UMEMOTO 	struct res_target *, res_state);
314a42af91cSHajimu UMEMOTO 
3157d56d374SYoshinobu Inoue /* XXX macros that make external reference is BAD. */
3167d56d374SYoshinobu Inoue 
3177d56d374SYoshinobu Inoue #define GET_AI(ai, afd, addr) \
3187d56d374SYoshinobu Inoue do { \
3197d56d374SYoshinobu Inoue 	/* external reference: pai, error, and label free */ \
3207d56d374SYoshinobu Inoue 	(ai) = get_ai(pai, (afd), (addr)); \
3217d56d374SYoshinobu Inoue 	if ((ai) == NULL) { \
3227d56d374SYoshinobu Inoue 		error = EAI_MEMORY; \
3237d56d374SYoshinobu Inoue 		goto free; \
3247d56d374SYoshinobu Inoue 	} \
325a42af91cSHajimu UMEMOTO } while (/*CONSTCOND*/0)
3267d56d374SYoshinobu Inoue 
3277d56d374SYoshinobu Inoue #define GET_PORT(ai, serv) \
3287d56d374SYoshinobu Inoue do { \
3297d56d374SYoshinobu Inoue 	/* external reference: error and label free */ \
3307d56d374SYoshinobu Inoue 	error = get_port((ai), (serv), 0); \
3317d56d374SYoshinobu Inoue 	if (error != 0) \
3327d56d374SYoshinobu Inoue 		goto free; \
333a42af91cSHajimu UMEMOTO } while (/*CONSTCOND*/0)
3347d56d374SYoshinobu Inoue 
3357d56d374SYoshinobu Inoue #define GET_CANONNAME(ai, str) \
3367d56d374SYoshinobu Inoue do { \
3377d56d374SYoshinobu Inoue 	/* external reference: pai, error and label free */ \
3387d56d374SYoshinobu Inoue 	error = get_canonname(pai, (ai), (str)); \
3397d56d374SYoshinobu Inoue 	if (error != 0) \
3407d56d374SYoshinobu Inoue 		goto free; \
341a42af91cSHajimu UMEMOTO } while (/*CONSTCOND*/0)
3427d56d374SYoshinobu Inoue 
3437d56d374SYoshinobu Inoue #define ERR(err) \
3447d56d374SYoshinobu Inoue do { \
3457d56d374SYoshinobu Inoue 	/* external reference: error, and label bad */ \
3467d56d374SYoshinobu Inoue 	error = (err); \
3477d56d374SYoshinobu Inoue 	goto bad; \
348a42af91cSHajimu UMEMOTO 	/*NOTREACHED*/ \
349a42af91cSHajimu UMEMOTO } while (/*CONSTCOND*/0)
3507d56d374SYoshinobu Inoue 
3517d56d374SYoshinobu Inoue #define MATCH_FAMILY(x, y, w) \
352a42af91cSHajimu UMEMOTO 	((x) == (y) || (/*CONSTCOND*/(w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
3537d56d374SYoshinobu Inoue #define MATCH(x, y, w) \
354a42af91cSHajimu UMEMOTO 	((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY)))
3557d56d374SYoshinobu Inoue 
3567d56d374SYoshinobu Inoue void
35790d1d883SHajimu UMEMOTO freeaddrinfo(struct addrinfo *ai)
3587d56d374SYoshinobu Inoue {
3597d56d374SYoshinobu Inoue 	struct addrinfo *next;
3607d56d374SYoshinobu Inoue 
3617d56d374SYoshinobu Inoue 	do {
3627d56d374SYoshinobu Inoue 		next = ai->ai_next;
3637d56d374SYoshinobu Inoue 		if (ai->ai_canonname)
3647d56d374SYoshinobu Inoue 			free(ai->ai_canonname);
3657d56d374SYoshinobu Inoue 		/* no need to free(ai->ai_addr) */
3667d56d374SYoshinobu Inoue 		free(ai);
367a42af91cSHajimu UMEMOTO 		ai = next;
368a42af91cSHajimu UMEMOTO 	} while (ai);
3697d56d374SYoshinobu Inoue }
3707d56d374SYoshinobu Inoue 
3717d56d374SYoshinobu Inoue static int
372e24e9d99SHajimu UMEMOTO str2number(const char *p, int *portp)
3737d56d374SYoshinobu Inoue {
374ec20fe00SJun-ichiro itojun Hagino 	char *ep;
375c224435eSHajimu UMEMOTO 	unsigned long v;
376ec20fe00SJun-ichiro itojun Hagino 
377ec20fe00SJun-ichiro itojun Hagino 	if (*p == '\0')
378c224435eSHajimu UMEMOTO 		return -1;
379ec20fe00SJun-ichiro itojun Hagino 	ep = NULL;
380a89c1d30SHajimu UMEMOTO 	errno = 0;
381c224435eSHajimu UMEMOTO 	v = strtoul(p, &ep, 10);
382e24e9d99SHajimu UMEMOTO 	if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX) {
383e24e9d99SHajimu UMEMOTO 		*portp = v;
384e24e9d99SHajimu UMEMOTO 		return 0;
385e24e9d99SHajimu UMEMOTO 	} else
386c224435eSHajimu UMEMOTO 		return -1;
3877d56d374SYoshinobu Inoue }
3887d56d374SYoshinobu Inoue 
3897d56d374SYoshinobu Inoue int
39090d1d883SHajimu UMEMOTO getaddrinfo(const char *hostname, const char *servname,
39190d1d883SHajimu UMEMOTO     const struct addrinfo *hints, struct addrinfo **res)
3927d56d374SYoshinobu Inoue {
3937d56d374SYoshinobu Inoue 	struct addrinfo sentinel;
3947d56d374SYoshinobu Inoue 	struct addrinfo *cur;
3957d56d374SYoshinobu Inoue 	int error = 0;
396bc421492SHajimu UMEMOTO 	struct addrinfo ai, ai0, *afai;
3977d56d374SYoshinobu Inoue 	struct addrinfo *pai;
398bc421492SHajimu UMEMOTO 	const struct afd *afd;
3997d56d374SYoshinobu Inoue 	const struct explore *ex;
400bc421492SHajimu UMEMOTO 	struct addrinfo *afailist[sizeof(afdl)/sizeof(afdl[0])];
401bc421492SHajimu UMEMOTO 	struct addrinfo *afai_unspec;
402bc421492SHajimu UMEMOTO 	int found;
4034c6867a8SHajimu UMEMOTO 	int numeric = 0;
4047d56d374SYoshinobu Inoue 
405bc421492SHajimu UMEMOTO 	/* ensure we return NULL on errors */
406bc421492SHajimu UMEMOTO 	*res = NULL;
407bc421492SHajimu UMEMOTO 
408bc421492SHajimu UMEMOTO 	memset(&ai, 0, sizeof(ai));
409bc421492SHajimu UMEMOTO 
410bc421492SHajimu UMEMOTO 	memset(afailist, 0, sizeof(afailist));
411bc421492SHajimu UMEMOTO 	afai_unspec = NULL;
412bc421492SHajimu UMEMOTO 
413a42af91cSHajimu UMEMOTO 	memset(&sentinel, 0, sizeof(sentinel));
4147d56d374SYoshinobu Inoue 	cur = &sentinel;
4157d56d374SYoshinobu Inoue 	pai = &ai;
4167d56d374SYoshinobu Inoue 	pai->ai_flags = 0;
4177d56d374SYoshinobu Inoue 	pai->ai_family = PF_UNSPEC;
4187d56d374SYoshinobu Inoue 	pai->ai_socktype = ANY;
4197d56d374SYoshinobu Inoue 	pai->ai_protocol = ANY;
4207d56d374SYoshinobu Inoue 	pai->ai_addrlen = 0;
4217d56d374SYoshinobu Inoue 	pai->ai_canonname = NULL;
4227d56d374SYoshinobu Inoue 	pai->ai_addr = NULL;
4237d56d374SYoshinobu Inoue 	pai->ai_next = NULL;
4247d56d374SYoshinobu Inoue 
4257d56d374SYoshinobu Inoue 	if (hostname == NULL && servname == NULL)
4267d56d374SYoshinobu Inoue 		return EAI_NONAME;
4277d56d374SYoshinobu Inoue 	if (hints) {
4287d56d374SYoshinobu Inoue 		/* error check for hints */
4297d56d374SYoshinobu Inoue 		if (hints->ai_addrlen || hints->ai_canonname ||
4307d56d374SYoshinobu Inoue 		    hints->ai_addr || hints->ai_next)
4317d56d374SYoshinobu Inoue 			ERR(EAI_BADHINTS); /* xxx */
4327d56d374SYoshinobu Inoue 		if (hints->ai_flags & ~AI_MASK)
4337d56d374SYoshinobu Inoue 			ERR(EAI_BADFLAGS);
4347d56d374SYoshinobu Inoue 		switch (hints->ai_family) {
4357d56d374SYoshinobu Inoue 		case PF_UNSPEC:
436bb957021SHiroki Sato 		case PF_LOCAL:
4377d56d374SYoshinobu Inoue 		case PF_INET:
4387d56d374SYoshinobu Inoue #ifdef INET6
4397d56d374SYoshinobu Inoue 		case PF_INET6:
4407d56d374SYoshinobu Inoue #endif
4417d56d374SYoshinobu Inoue 			break;
4427d56d374SYoshinobu Inoue 		default:
4437d56d374SYoshinobu Inoue 			ERR(EAI_FAMILY);
4447d56d374SYoshinobu Inoue 		}
4457d56d374SYoshinobu Inoue 		memcpy(pai, hints, sizeof(*pai));
4467d56d374SYoshinobu Inoue 
4477d56d374SYoshinobu Inoue 		/*
4487d56d374SYoshinobu Inoue 		 * if both socktype/protocol are specified, check if they
4497d56d374SYoshinobu Inoue 		 * are meaningful combination.
4507d56d374SYoshinobu Inoue 		 */
4517d56d374SYoshinobu Inoue 		if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
452a42af91cSHajimu UMEMOTO 			for (ex = explore; ex->e_af >= 0; ex++) {
453bc421492SHajimu UMEMOTO 				if (!MATCH_FAMILY(pai->ai_family, ex->e_af,
454bc421492SHajimu UMEMOTO 				    WILD_AF(ex)))
455a42af91cSHajimu UMEMOTO 					continue;
456bc421492SHajimu UMEMOTO 				if (!MATCH(pai->ai_socktype, ex->e_socktype,
457bc421492SHajimu UMEMOTO 				    WILD_SOCKTYPE(ex)))
4587d56d374SYoshinobu Inoue 					continue;
459bc421492SHajimu UMEMOTO 				if (!MATCH(pai->ai_protocol, ex->e_protocol,
460bc421492SHajimu UMEMOTO 				    WILD_PROTOCOL(ex)))
4617d56d374SYoshinobu Inoue 					continue;
462bc421492SHajimu UMEMOTO 
463bc421492SHajimu UMEMOTO 				/* matched */
4644d756307SHajimu UMEMOTO 				break;
4654d756307SHajimu UMEMOTO 			}
4664d756307SHajimu UMEMOTO 
4674d756307SHajimu UMEMOTO 			if (ex->e_af < 0)
4687d56d374SYoshinobu Inoue 				ERR(EAI_BADHINTS);
4697d56d374SYoshinobu Inoue 		}
4707d56d374SYoshinobu Inoue 	}
4717d56d374SYoshinobu Inoue 
4727d56d374SYoshinobu Inoue 	/*
473a42af91cSHajimu UMEMOTO 	 * check for special cases.  (1) numeric servname is disallowed if
474a42af91cSHajimu UMEMOTO 	 * socktype/protocol are left unspecified. (2) servname is disallowed
475a42af91cSHajimu UMEMOTO 	 * for raw and other inet{,6} sockets.
4767d56d374SYoshinobu Inoue 	 */
4777d56d374SYoshinobu Inoue 	if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
478a42af91cSHajimu UMEMOTO #ifdef PF_INET6
4797d56d374SYoshinobu Inoue 	    || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)
4807d56d374SYoshinobu Inoue #endif
4817d56d374SYoshinobu Inoue 	    ) {
482a42af91cSHajimu UMEMOTO 		ai0 = *pai;	/* backup *pai */
4837d56d374SYoshinobu Inoue 
484a42af91cSHajimu UMEMOTO 		if (pai->ai_family == PF_UNSPEC) {
485a42af91cSHajimu UMEMOTO #ifdef PF_INET6
4867d56d374SYoshinobu Inoue 			pai->ai_family = PF_INET6;
4877d56d374SYoshinobu Inoue #else
4887d56d374SYoshinobu Inoue 			pai->ai_family = PF_INET;
4897d56d374SYoshinobu Inoue #endif
490a42af91cSHajimu UMEMOTO 		}
4917d56d374SYoshinobu Inoue 		error = get_portmatch(pai, servname);
4927d56d374SYoshinobu Inoue 		if (error)
493f61ac9d9SDimitry Andric 			goto bad;
494a42af91cSHajimu UMEMOTO 
495a42af91cSHajimu UMEMOTO 		*pai = ai0;
4967d56d374SYoshinobu Inoue 	}
4977d56d374SYoshinobu Inoue 
498a42af91cSHajimu UMEMOTO 	ai0 = *pai;
499a42af91cSHajimu UMEMOTO 
500bc421492SHajimu UMEMOTO 	/*
501bc421492SHajimu UMEMOTO 	 * NULL hostname, or numeric hostname.
502bc421492SHajimu UMEMOTO 	 * If numeric representation of AF1 can be interpreted as FQDN
503bc421492SHajimu UMEMOTO 	 * representation of AF2, we need to think again about the code below.
504bc421492SHajimu UMEMOTO 	 */
505bc421492SHajimu UMEMOTO 	found = 0;
506bc421492SHajimu UMEMOTO 	for (afd = afdl; afd->a_af; afd++) {
5077d56d374SYoshinobu Inoue 		*pai = ai0;
5087d56d374SYoshinobu Inoue 
509bc421492SHajimu UMEMOTO 		if (!MATCH_FAMILY(pai->ai_family, afd->a_af, 1))
5107d56d374SYoshinobu Inoue 			continue;
5117d56d374SYoshinobu Inoue 
5127d56d374SYoshinobu Inoue 		if (pai->ai_family == PF_UNSPEC)
513bc421492SHajimu UMEMOTO 			pai->ai_family = afd->a_af;
5147d56d374SYoshinobu Inoue 
515bc421492SHajimu UMEMOTO 		if (hostname == NULL) {
516bc421492SHajimu UMEMOTO 			error = explore_null(pai, servname,
517bc421492SHajimu UMEMOTO 			    &afailist[afd - afdl]);
5187d56d374SYoshinobu Inoue 
519bf838688SHajimu UMEMOTO 			/*
520bc421492SHajimu UMEMOTO 			 * Errors from explore_null should be unexpected and
521bc421492SHajimu UMEMOTO 			 * be caught to avoid returning an incomplete result.
522bf838688SHajimu UMEMOTO 			 */
523bc421492SHajimu UMEMOTO 			if (error != 0)
524bc421492SHajimu UMEMOTO 				goto bad;
525bc421492SHajimu UMEMOTO 		} else {
526bc421492SHajimu UMEMOTO 			error = explore_numeric_scope(pai, hostname, servname,
527bc421492SHajimu UMEMOTO 			    &afailist[afd - afdl]);
528bc421492SHajimu UMEMOTO 
529bc421492SHajimu UMEMOTO 			/*
530bc421492SHajimu UMEMOTO 			 * explore_numeric_scope returns an error for address
531bc421492SHajimu UMEMOTO 			 * families that do not match that of hostname.
532bc421492SHajimu UMEMOTO 			 * Thus we should not catch the error at this moment.
533bc421492SHajimu UMEMOTO 			 */
534bc421492SHajimu UMEMOTO 		}
535bc421492SHajimu UMEMOTO 
536bc421492SHajimu UMEMOTO 		if (!error && afailist[afd - afdl])
537bc421492SHajimu UMEMOTO 			found++;
538bc421492SHajimu UMEMOTO 	}
539bc421492SHajimu UMEMOTO 	if (found) {
5404c6867a8SHajimu UMEMOTO 		numeric = 1;
541bc421492SHajimu UMEMOTO 		goto globcopy;
5424c6867a8SHajimu UMEMOTO 	}
543bf838688SHajimu UMEMOTO 
544bf838688SHajimu UMEMOTO 	if (hostname == NULL)
545bf838688SHajimu UMEMOTO 		ERR(EAI_NONAME);	/* used to be EAI_NODATA */
546bf838688SHajimu UMEMOTO 	if (pai->ai_flags & AI_NUMERICHOST)
547bf838688SHajimu UMEMOTO 		ERR(EAI_NONAME);
548bf838688SHajimu UMEMOTO 
549bf838688SHajimu UMEMOTO 	if ((pai->ai_flags & AI_ADDRCONFIG) != 0 && !addrconfig(&ai0))
550bf838688SHajimu UMEMOTO 		ERR(EAI_FAIL);
551a42af91cSHajimu UMEMOTO 
552f95d4633SHajimu UMEMOTO 	/*
553bf838688SHajimu UMEMOTO 	 * hostname as alphabetical name.
554f95d4633SHajimu UMEMOTO 	 */
555bc421492SHajimu UMEMOTO 	*pai = ai0;
556bc421492SHajimu UMEMOTO 	error = explore_fqdn(pai, hostname, servname, &afai_unspec);
557bc421492SHajimu UMEMOTO 
558bc421492SHajimu UMEMOTO globcopy:
559bf838688SHajimu UMEMOTO 	for (ex = explore; ex->e_af >= 0; ex++) {
560bf838688SHajimu UMEMOTO 		*pai = ai0;
561f95d4633SHajimu UMEMOTO 
562bc421492SHajimu UMEMOTO 		if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex)))
563f95d4633SHajimu UMEMOTO 			continue;
564bf838688SHajimu UMEMOTO 		if (!MATCH(pai->ai_socktype, ex->e_socktype,
565bc421492SHajimu UMEMOTO 		    WILD_SOCKTYPE(ex)))
566bf838688SHajimu UMEMOTO 			continue;
567bf838688SHajimu UMEMOTO 		if (!MATCH(pai->ai_protocol, ex->e_protocol,
568bc421492SHajimu UMEMOTO 		    WILD_PROTOCOL(ex)))
569bf838688SHajimu UMEMOTO 			continue;
570f95d4633SHajimu UMEMOTO 
571bc421492SHajimu UMEMOTO 		if (pai->ai_family == PF_UNSPEC)
572bc421492SHajimu UMEMOTO 			pai->ai_family = ex->e_af;
573bf838688SHajimu UMEMOTO 		if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
574bf838688SHajimu UMEMOTO 			pai->ai_socktype = ex->e_socktype;
575bf838688SHajimu UMEMOTO 		if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
576bf838688SHajimu UMEMOTO 			pai->ai_protocol = ex->e_protocol;
577bf838688SHajimu UMEMOTO 
578bc421492SHajimu UMEMOTO 		/*
579bc421492SHajimu UMEMOTO 		 * if the servname does not match socktype/protocol, ignore it.
580bc421492SHajimu UMEMOTO 		 */
581bc421492SHajimu UMEMOTO 		if (get_portmatch(pai, servname) != 0)
582bc421492SHajimu UMEMOTO 			continue;
583bc421492SHajimu UMEMOTO 
584bc421492SHajimu UMEMOTO 		if (afai_unspec)
585bc421492SHajimu UMEMOTO 			afai = afai_unspec;
586bc421492SHajimu UMEMOTO 		else {
587bc421492SHajimu UMEMOTO 			if ((afd = find_afd(pai->ai_family)) == NULL)
588bc421492SHajimu UMEMOTO 				continue;
589bc421492SHajimu UMEMOTO 			/* XXX assumes that afd points inside afdl[] */
590bc421492SHajimu UMEMOTO 			afai = afailist[afd - afdl];
591bc421492SHajimu UMEMOTO 		}
592bc421492SHajimu UMEMOTO 		if (!afai)
593bc421492SHajimu UMEMOTO 			continue;
594bc421492SHajimu UMEMOTO 
595bc421492SHajimu UMEMOTO 		error = explore_copy(pai, afai, &cur->ai_next);
596bc421492SHajimu UMEMOTO 		if (error != 0)
597bc421492SHajimu UMEMOTO 			goto bad;
598bf838688SHajimu UMEMOTO 
599bf838688SHajimu UMEMOTO 		while (cur && cur->ai_next)
600f95d4633SHajimu UMEMOTO 			cur = cur->ai_next;
601f95d4633SHajimu UMEMOTO 	}
602f95d4633SHajimu UMEMOTO 
6034c6867a8SHajimu UMEMOTO 	/*
6044c6867a8SHajimu UMEMOTO 	 * ensure we return either:
6054c6867a8SHajimu UMEMOTO 	 * - error == 0, non-NULL *res
6064c6867a8SHajimu UMEMOTO 	 * - error != 0, NULL *res
6074c6867a8SHajimu UMEMOTO 	 */
608bf838688SHajimu UMEMOTO 	if (error == 0) {
609bf838688SHajimu UMEMOTO 		if (sentinel.ai_next) {
6104c6867a8SHajimu UMEMOTO 			/*
6114c6867a8SHajimu UMEMOTO 			 * If the returned entry is for an active connection,
6124c6867a8SHajimu UMEMOTO 			 * and the given name is not numeric, reorder the
6134c6867a8SHajimu UMEMOTO 			 * list, so that the application would try the list
614e47c82edSJINMEI Tatuya 			 * in the most efficient order.  Since the head entry
615e47c82edSJINMEI Tatuya 			 * of the original list may contain ai_canonname and
616e47c82edSJINMEI Tatuya 			 * that entry may be moved elsewhere in the new list,
617e47c82edSJINMEI Tatuya 			 * we keep the pointer and will  restore it in the new
618e47c82edSJINMEI Tatuya 			 * head entry.  (Note that RFC3493 requires the head
619e47c82edSJINMEI Tatuya 			 * entry store it when requested by the caller).
6204c6867a8SHajimu UMEMOTO 			 */
6214c6867a8SHajimu UMEMOTO 			if (hints == NULL || !(hints->ai_flags & AI_PASSIVE)) {
622e47c82edSJINMEI Tatuya 				if (!numeric) {
623e47c82edSJINMEI Tatuya 					char *canonname;
624e47c82edSJINMEI Tatuya 
625e47c82edSJINMEI Tatuya 					canonname =
626e47c82edSJINMEI Tatuya 					    sentinel.ai_next->ai_canonname;
627e47c82edSJINMEI Tatuya 					sentinel.ai_next->ai_canonname = NULL;
6284c6867a8SHajimu UMEMOTO 					(void)reorder(&sentinel);
629e47c82edSJINMEI Tatuya 					if (sentinel.ai_next->ai_canonname ==
630e47c82edSJINMEI Tatuya 					    NULL) {
631e47c82edSJINMEI Tatuya 						sentinel.ai_next->ai_canonname
632e47c82edSJINMEI Tatuya 						    = canonname;
633e47c82edSJINMEI Tatuya 					} else if (canonname != NULL)
634e47c82edSJINMEI Tatuya 						free(canonname);
635e47c82edSJINMEI Tatuya 				}
6364c6867a8SHajimu UMEMOTO 			}
637bf838688SHajimu UMEMOTO 			*res = sentinel.ai_next;
638bf838688SHajimu UMEMOTO 		} else
639bf838688SHajimu UMEMOTO 			error = EAI_FAIL;
640bf838688SHajimu UMEMOTO 	}
641bc421492SHajimu UMEMOTO 
642bf838688SHajimu UMEMOTO bad:
643bc421492SHajimu UMEMOTO 	if (afai_unspec)
644bc421492SHajimu UMEMOTO 		freeaddrinfo(afai_unspec);
645bc421492SHajimu UMEMOTO 	for (afd = afdl; afd->a_af; afd++) {
646bc421492SHajimu UMEMOTO 		if (afailist[afd - afdl])
647bc421492SHajimu UMEMOTO 			freeaddrinfo(afailist[afd - afdl]);
648bc421492SHajimu UMEMOTO 	}
649bc421492SHajimu UMEMOTO 	if (!*res)
650bf838688SHajimu UMEMOTO 		if (sentinel.ai_next)
651f95d4633SHajimu UMEMOTO 			freeaddrinfo(sentinel.ai_next);
652bc421492SHajimu UMEMOTO 
653bc421492SHajimu UMEMOTO 	return (error);
6547d56d374SYoshinobu Inoue }
6557d56d374SYoshinobu Inoue 
6564c6867a8SHajimu UMEMOTO static int
65790d1d883SHajimu UMEMOTO reorder(struct addrinfo *sentinel)
6584c6867a8SHajimu UMEMOTO {
6594c6867a8SHajimu UMEMOTO 	struct addrinfo *ai, **aip;
6604c6867a8SHajimu UMEMOTO 	struct ai_order *aio;
6614c6867a8SHajimu UMEMOTO 	int i, n;
6624c6867a8SHajimu UMEMOTO 	struct policyhead policyhead;
6634c6867a8SHajimu UMEMOTO 
6644c6867a8SHajimu UMEMOTO 	/* count the number of addrinfo elements for sorting. */
6654c6867a8SHajimu UMEMOTO 	for (n = 0, ai = sentinel->ai_next; ai != NULL; ai = ai->ai_next, n++)
6664c6867a8SHajimu UMEMOTO 		;
6674c6867a8SHajimu UMEMOTO 
6684c6867a8SHajimu UMEMOTO 	/*
6694c6867a8SHajimu UMEMOTO 	 * If the number is small enough, we can skip the reordering process.
6704c6867a8SHajimu UMEMOTO 	 */
6714c6867a8SHajimu UMEMOTO 	if (n <= 1)
6724c6867a8SHajimu UMEMOTO 		return(n);
6734c6867a8SHajimu UMEMOTO 
6744c6867a8SHajimu UMEMOTO 	/* allocate a temporary array for sort and initialization of it. */
6754c6867a8SHajimu UMEMOTO 	if ((aio = malloc(sizeof(*aio) * n)) == NULL)
6764c6867a8SHajimu UMEMOTO 		return(n);	/* give up reordering */
6774c6867a8SHajimu UMEMOTO 	memset(aio, 0, sizeof(*aio) * n);
6784c6867a8SHajimu UMEMOTO 
6794c6867a8SHajimu UMEMOTO 	/* retrieve address selection policy from the kernel */
6804c6867a8SHajimu UMEMOTO 	TAILQ_INIT(&policyhead);
6814c6867a8SHajimu UMEMOTO 	if (!get_addrselectpolicy(&policyhead)) {
6824c6867a8SHajimu UMEMOTO 		/* no policy is installed into kernel, we don't sort. */
6834c6867a8SHajimu UMEMOTO 		free(aio);
6844c6867a8SHajimu UMEMOTO 		return (n);
6854c6867a8SHajimu UMEMOTO 	}
6864c6867a8SHajimu UMEMOTO 
6874c6867a8SHajimu UMEMOTO 	for (i = 0, ai = sentinel->ai_next; i < n; ai = ai->ai_next, i++) {
6884c6867a8SHajimu UMEMOTO 		aio[i].aio_ai = ai;
6894c6867a8SHajimu UMEMOTO 		aio[i].aio_dstscope = gai_addr2scopetype(ai->ai_addr);
6904c6867a8SHajimu UMEMOTO 		aio[i].aio_dstpolicy = match_addrselectpolicy(ai->ai_addr,
6914c6867a8SHajimu UMEMOTO 							      &policyhead);
6924d489f47SHajimu UMEMOTO 		set_source(&aio[i], &policyhead);
6934c6867a8SHajimu UMEMOTO 	}
6944c6867a8SHajimu UMEMOTO 
6954c6867a8SHajimu UMEMOTO 	/* perform sorting. */
6964c6867a8SHajimu UMEMOTO 	qsort(aio, n, sizeof(*aio), comp_dst);
6974c6867a8SHajimu UMEMOTO 
6984c6867a8SHajimu UMEMOTO 	/* reorder the addrinfo chain. */
6994c6867a8SHajimu UMEMOTO 	for (i = 0, aip = &sentinel->ai_next; i < n; i++) {
7004c6867a8SHajimu UMEMOTO 		*aip = aio[i].aio_ai;
7014c6867a8SHajimu UMEMOTO 		aip = &aio[i].aio_ai->ai_next;
7024c6867a8SHajimu UMEMOTO 	}
7034c6867a8SHajimu UMEMOTO 	*aip = NULL;
7044c6867a8SHajimu UMEMOTO 
7054c6867a8SHajimu UMEMOTO 	/* cleanup and return */
7064c6867a8SHajimu UMEMOTO 	free(aio);
7074c6867a8SHajimu UMEMOTO 	free_addrselectpolicy(&policyhead);
7084c6867a8SHajimu UMEMOTO 	return(n);
7094c6867a8SHajimu UMEMOTO }
7104c6867a8SHajimu UMEMOTO 
7114c6867a8SHajimu UMEMOTO static int
71290d1d883SHajimu UMEMOTO get_addrselectpolicy(struct policyhead *head)
7134c6867a8SHajimu UMEMOTO {
7144c6867a8SHajimu UMEMOTO #ifdef INET6
7154c6867a8SHajimu UMEMOTO 	int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_ADDRCTLPOLICY };
7164c6867a8SHajimu UMEMOTO 	size_t l;
7174c6867a8SHajimu UMEMOTO 	char *buf;
7184c6867a8SHajimu UMEMOTO 	struct in6_addrpolicy *pol, *ep;
7194c6867a8SHajimu UMEMOTO 
7204c6867a8SHajimu UMEMOTO 	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0)
7214c6867a8SHajimu UMEMOTO 		return (0);
7220c19b5deSMaksim Yevmenkin 	if (l == 0)
7230c19b5deSMaksim Yevmenkin 		return (0);
7244c6867a8SHajimu UMEMOTO 	if ((buf = malloc(l)) == NULL)
7254c6867a8SHajimu UMEMOTO 		return (0);
7264c6867a8SHajimu UMEMOTO 	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
7274c6867a8SHajimu UMEMOTO 		free(buf);
7284c6867a8SHajimu UMEMOTO 		return (0);
7294c6867a8SHajimu UMEMOTO 	}
7304c6867a8SHajimu UMEMOTO 
7314c6867a8SHajimu UMEMOTO 	ep = (struct in6_addrpolicy *)(buf + l);
7324c6867a8SHajimu UMEMOTO 	for (pol = (struct in6_addrpolicy *)buf; pol + 1 <= ep; pol++) {
7334c6867a8SHajimu UMEMOTO 		struct policyqueue *new;
7344c6867a8SHajimu UMEMOTO 
7354c6867a8SHajimu UMEMOTO 		if ((new = malloc(sizeof(*new))) == NULL) {
7364c6867a8SHajimu UMEMOTO 			free_addrselectpolicy(head); /* make the list empty */
7374c6867a8SHajimu UMEMOTO 			break;
7384c6867a8SHajimu UMEMOTO 		}
7394c6867a8SHajimu UMEMOTO 		new->pc_policy = *pol;
7404c6867a8SHajimu UMEMOTO 		TAILQ_INSERT_TAIL(head, new, pc_entry);
7414c6867a8SHajimu UMEMOTO 	}
7424c6867a8SHajimu UMEMOTO 
7434c6867a8SHajimu UMEMOTO 	free(buf);
7444c6867a8SHajimu UMEMOTO 	return (1);
7454c6867a8SHajimu UMEMOTO #else
7464c6867a8SHajimu UMEMOTO 	return (0);
7474c6867a8SHajimu UMEMOTO #endif
7484c6867a8SHajimu UMEMOTO }
7494c6867a8SHajimu UMEMOTO 
7504c6867a8SHajimu UMEMOTO static void
75190d1d883SHajimu UMEMOTO free_addrselectpolicy(struct policyhead *head)
7524c6867a8SHajimu UMEMOTO {
7534c6867a8SHajimu UMEMOTO 	struct policyqueue *ent, *nent;
7544c6867a8SHajimu UMEMOTO 
7554c6867a8SHajimu UMEMOTO 	for (ent = TAILQ_FIRST(head); ent; ent = nent) {
7564c6867a8SHajimu UMEMOTO 		nent = TAILQ_NEXT(ent, pc_entry);
7574c6867a8SHajimu UMEMOTO 		TAILQ_REMOVE(head, ent, pc_entry);
7584c6867a8SHajimu UMEMOTO 		free(ent);
7594c6867a8SHajimu UMEMOTO 	}
7604c6867a8SHajimu UMEMOTO }
7614c6867a8SHajimu UMEMOTO 
7624c6867a8SHajimu UMEMOTO static struct policyqueue *
76390d1d883SHajimu UMEMOTO match_addrselectpolicy(struct sockaddr *addr, struct policyhead *head)
7644c6867a8SHajimu UMEMOTO {
7654c6867a8SHajimu UMEMOTO #ifdef INET6
7664c6867a8SHajimu UMEMOTO 	struct policyqueue *ent, *bestent = NULL;
7674c6867a8SHajimu UMEMOTO 	struct in6_addrpolicy *pol;
7684c6867a8SHajimu UMEMOTO 	int matchlen, bestmatchlen = -1;
7694c6867a8SHajimu UMEMOTO 	u_char *mp, *ep, *k, *p, m;
7704c6867a8SHajimu UMEMOTO 	struct sockaddr_in6 key;
7714c6867a8SHajimu UMEMOTO 
7724c6867a8SHajimu UMEMOTO 	switch(addr->sa_family) {
7734c6867a8SHajimu UMEMOTO 	case AF_INET6:
7744c6867a8SHajimu UMEMOTO 		key = *(struct sockaddr_in6 *)addr;
7754c6867a8SHajimu UMEMOTO 		break;
7764c6867a8SHajimu UMEMOTO 	case AF_INET:
7774c6867a8SHajimu UMEMOTO 		/* convert the address into IPv4-mapped IPv6 address. */
7784c6867a8SHajimu UMEMOTO 		memset(&key, 0, sizeof(key));
7794c6867a8SHajimu UMEMOTO 		key.sin6_family = AF_INET6;
7804c6867a8SHajimu UMEMOTO 		key.sin6_len = sizeof(key);
7814c6867a8SHajimu UMEMOTO 		key.sin6_addr.s6_addr[10] = 0xff;
7824c6867a8SHajimu UMEMOTO 		key.sin6_addr.s6_addr[11] = 0xff;
7834c6867a8SHajimu UMEMOTO 		memcpy(&key.sin6_addr.s6_addr[12],
7844c6867a8SHajimu UMEMOTO 		       &((struct sockaddr_in *)addr)->sin_addr, 4);
7854c6867a8SHajimu UMEMOTO 		break;
7864c6867a8SHajimu UMEMOTO 	default:
7874c6867a8SHajimu UMEMOTO 		return(NULL);
7884c6867a8SHajimu UMEMOTO 	}
7894c6867a8SHajimu UMEMOTO 
7904c6867a8SHajimu UMEMOTO 	for (ent = TAILQ_FIRST(head); ent; ent = TAILQ_NEXT(ent, pc_entry)) {
7914c6867a8SHajimu UMEMOTO 		pol = &ent->pc_policy;
7924c6867a8SHajimu UMEMOTO 		matchlen = 0;
7934c6867a8SHajimu UMEMOTO 
7944c6867a8SHajimu UMEMOTO 		mp = (u_char *)&pol->addrmask.sin6_addr;
7954c6867a8SHajimu UMEMOTO 		ep = mp + 16;	/* XXX: scope field? */
7964c6867a8SHajimu UMEMOTO 		k = (u_char *)&key.sin6_addr;
7974c6867a8SHajimu UMEMOTO 		p = (u_char *)&pol->addr.sin6_addr;
7984c6867a8SHajimu UMEMOTO 		for (; mp < ep && *mp; mp++, k++, p++) {
7994c6867a8SHajimu UMEMOTO 			m = *mp;
8004c6867a8SHajimu UMEMOTO 			if ((*k & m) != *p)
8014c6867a8SHajimu UMEMOTO 				goto next; /* not match */
8024c6867a8SHajimu UMEMOTO 			if (m == 0xff) /* short cut for a typical case */
8034c6867a8SHajimu UMEMOTO 				matchlen += 8;
8044c6867a8SHajimu UMEMOTO 			else {
8054c6867a8SHajimu UMEMOTO 				while (m >= 0x80) {
8064c6867a8SHajimu UMEMOTO 					matchlen++;
8074c6867a8SHajimu UMEMOTO 					m <<= 1;
8084c6867a8SHajimu UMEMOTO 				}
8094c6867a8SHajimu UMEMOTO 			}
8104c6867a8SHajimu UMEMOTO 		}
8114c6867a8SHajimu UMEMOTO 
8124c6867a8SHajimu UMEMOTO 		/* matched.  check if this is better than the current best. */
8134c6867a8SHajimu UMEMOTO 		if (matchlen > bestmatchlen) {
8144c6867a8SHajimu UMEMOTO 			bestent = ent;
8154c6867a8SHajimu UMEMOTO 			bestmatchlen = matchlen;
8164c6867a8SHajimu UMEMOTO 		}
8174c6867a8SHajimu UMEMOTO 
8184c6867a8SHajimu UMEMOTO 	  next:
8194c6867a8SHajimu UMEMOTO 		continue;
8204c6867a8SHajimu UMEMOTO 	}
8214c6867a8SHajimu UMEMOTO 
8224c6867a8SHajimu UMEMOTO 	return(bestent);
8234c6867a8SHajimu UMEMOTO #else
8244c6867a8SHajimu UMEMOTO 	return(NULL);
8254c6867a8SHajimu UMEMOTO #endif
8264c6867a8SHajimu UMEMOTO 
8274c6867a8SHajimu UMEMOTO }
8284c6867a8SHajimu UMEMOTO 
8294d489f47SHajimu UMEMOTO static void
83090d1d883SHajimu UMEMOTO set_source(struct ai_order *aio, struct policyhead *ph)
8314d489f47SHajimu UMEMOTO {
8324d489f47SHajimu UMEMOTO 	struct addrinfo ai = *aio->aio_ai;
8334d489f47SHajimu UMEMOTO 	struct sockaddr_storage ss;
834943db5a2SHajimu UMEMOTO 	socklen_t srclen;
835943db5a2SHajimu UMEMOTO 	int s;
8364d489f47SHajimu UMEMOTO 
8374d489f47SHajimu UMEMOTO 	/* set unspec ("no source is available"), just in case */
8384d489f47SHajimu UMEMOTO 	aio->aio_srcsa.sa_family = AF_UNSPEC;
8394d489f47SHajimu UMEMOTO 	aio->aio_srcscope = -1;
8404d489f47SHajimu UMEMOTO 
8414d489f47SHajimu UMEMOTO 	switch(ai.ai_family) {
8424d489f47SHajimu UMEMOTO 	case AF_INET:
8434d489f47SHajimu UMEMOTO #ifdef INET6
8444d489f47SHajimu UMEMOTO 	case AF_INET6:
8454d489f47SHajimu UMEMOTO #endif
8464d489f47SHajimu UMEMOTO 		break;
8474d489f47SHajimu UMEMOTO 	default:		/* ignore unsupported AFs explicitly */
8484d489f47SHajimu UMEMOTO 		return;
8494d489f47SHajimu UMEMOTO 	}
8504d489f47SHajimu UMEMOTO 
8514d489f47SHajimu UMEMOTO 	/* XXX: make a dummy addrinfo to call connect() */
8524d489f47SHajimu UMEMOTO 	ai.ai_socktype = SOCK_DGRAM;
8534d489f47SHajimu UMEMOTO 	ai.ai_protocol = IPPROTO_UDP; /* is UDP too specific? */
8544d489f47SHajimu UMEMOTO 	ai.ai_next = NULL;
8554d489f47SHajimu UMEMOTO 	memset(&ss, 0, sizeof(ss));
8564d489f47SHajimu UMEMOTO 	memcpy(&ss, ai.ai_addr, ai.ai_addrlen);
8574d489f47SHajimu UMEMOTO 	ai.ai_addr = (struct sockaddr *)&ss;
8584d489f47SHajimu UMEMOTO 	get_port(&ai, "1", 0);
8594d489f47SHajimu UMEMOTO 
8604d489f47SHajimu UMEMOTO 	/* open a socket to get the source address for the given dst */
86172531978SJilles Tjoelker 	if ((s = _socket(ai.ai_family, ai.ai_socktype | SOCK_CLOEXEC,
86272531978SJilles Tjoelker 	    ai.ai_protocol)) < 0)
8634d489f47SHajimu UMEMOTO 		return;		/* give up */
8644d489f47SHajimu UMEMOTO 	if (_connect(s, ai.ai_addr, ai.ai_addrlen) < 0)
8654d489f47SHajimu UMEMOTO 		goto cleanup;
8664d489f47SHajimu UMEMOTO 	srclen = ai.ai_addrlen;
8674d489f47SHajimu UMEMOTO 	if (_getsockname(s, &aio->aio_srcsa, &srclen) < 0) {
8684d489f47SHajimu UMEMOTO 		aio->aio_srcsa.sa_family = AF_UNSPEC;
8694d489f47SHajimu UMEMOTO 		goto cleanup;
8704d489f47SHajimu UMEMOTO 	}
8714d489f47SHajimu UMEMOTO 	aio->aio_srcscope = gai_addr2scopetype(&aio->aio_srcsa);
8724d489f47SHajimu UMEMOTO 	aio->aio_srcpolicy = match_addrselectpolicy(&aio->aio_srcsa, ph);
8734d489f47SHajimu UMEMOTO 	aio->aio_matchlen = matchlen(&aio->aio_srcsa, aio->aio_ai->ai_addr);
8744d489f47SHajimu UMEMOTO #ifdef INET6
8754d489f47SHajimu UMEMOTO 	if (ai.ai_family == AF_INET6) {
8764d489f47SHajimu UMEMOTO 		struct in6_ifreq ifr6;
8774d489f47SHajimu UMEMOTO 		u_int32_t flags6;
8784d489f47SHajimu UMEMOTO 
8794d489f47SHajimu UMEMOTO 		memset(&ifr6, 0, sizeof(ifr6));
8804d489f47SHajimu UMEMOTO 		memcpy(&ifr6.ifr_addr, ai.ai_addr, ai.ai_addrlen);
8814d489f47SHajimu UMEMOTO 		if (_ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == 0) {
8824d489f47SHajimu UMEMOTO 			flags6 = ifr6.ifr_ifru.ifru_flags6;
8834d489f47SHajimu UMEMOTO 			if ((flags6 & IN6_IFF_DEPRECATED))
8844d489f47SHajimu UMEMOTO 				aio->aio_srcflag |= AIO_SRCFLAG_DEPRECATED;
8854d489f47SHajimu UMEMOTO 		}
8864d489f47SHajimu UMEMOTO 	}
8874d489f47SHajimu UMEMOTO #endif
8884d489f47SHajimu UMEMOTO 
8894d489f47SHajimu UMEMOTO   cleanup:
8904d489f47SHajimu UMEMOTO 	_close(s);
8914d489f47SHajimu UMEMOTO 	return;
8924d489f47SHajimu UMEMOTO }
8934d489f47SHajimu UMEMOTO 
8944d489f47SHajimu UMEMOTO static int
89590d1d883SHajimu UMEMOTO matchlen(struct sockaddr *src, struct sockaddr *dst)
8964d489f47SHajimu UMEMOTO {
8974d489f47SHajimu UMEMOTO 	int match = 0;
8984d489f47SHajimu UMEMOTO 	u_char *s, *d;
8994d489f47SHajimu UMEMOTO 	u_char *lim, r;
9004d489f47SHajimu UMEMOTO 	int addrlen;
9014d489f47SHajimu UMEMOTO 
9024d489f47SHajimu UMEMOTO 	switch (src->sa_family) {
9034d489f47SHajimu UMEMOTO #ifdef INET6
9044d489f47SHajimu UMEMOTO 	case AF_INET6:
9054d489f47SHajimu UMEMOTO 		s = (u_char *)&((struct sockaddr_in6 *)src)->sin6_addr;
9064d489f47SHajimu UMEMOTO 		d = (u_char *)&((struct sockaddr_in6 *)dst)->sin6_addr;
9074d489f47SHajimu UMEMOTO 		addrlen = sizeof(struct in6_addr);
9084d489f47SHajimu UMEMOTO 		lim = s + addrlen;
9094d489f47SHajimu UMEMOTO 		break;
9104d489f47SHajimu UMEMOTO #endif
9114d489f47SHajimu UMEMOTO 	case AF_INET:
912fd57e549SGeorge V. Neville-Neil 		s = (u_char *)&((struct sockaddr_in *)src)->sin_addr;
913fd57e549SGeorge V. Neville-Neil 		d = (u_char *)&((struct sockaddr_in *)dst)->sin_addr;
9144d489f47SHajimu UMEMOTO 		addrlen = sizeof(struct in_addr);
9154d489f47SHajimu UMEMOTO 		lim = s + addrlen;
9164d489f47SHajimu UMEMOTO 		break;
9174d489f47SHajimu UMEMOTO 	default:
9184d489f47SHajimu UMEMOTO 		return(0);
9194d489f47SHajimu UMEMOTO 	}
9204d489f47SHajimu UMEMOTO 
9214d489f47SHajimu UMEMOTO 	while (s < lim)
9224d489f47SHajimu UMEMOTO 		if ((r = (*d++ ^ *s++)) != 0) {
9234d489f47SHajimu UMEMOTO 			while (r < addrlen * 8) {
9244d489f47SHajimu UMEMOTO 				match++;
9254d489f47SHajimu UMEMOTO 				r <<= 1;
9264d489f47SHajimu UMEMOTO 			}
9274d489f47SHajimu UMEMOTO 			break;
9284d489f47SHajimu UMEMOTO 		} else
9294d489f47SHajimu UMEMOTO 			match += 8;
9304d489f47SHajimu UMEMOTO 	return(match);
9314d489f47SHajimu UMEMOTO }
9324d489f47SHajimu UMEMOTO 
9334c6867a8SHajimu UMEMOTO static int
93490d1d883SHajimu UMEMOTO comp_dst(const void *arg1, const void *arg2)
9354c6867a8SHajimu UMEMOTO {
9364c6867a8SHajimu UMEMOTO 	const struct ai_order *dst1 = arg1, *dst2 = arg2;
9374c6867a8SHajimu UMEMOTO 
9384c6867a8SHajimu UMEMOTO 	/*
9394c6867a8SHajimu UMEMOTO 	 * Rule 1: Avoid unusable destinations.
9404c6867a8SHajimu UMEMOTO 	 * XXX: we currently do not consider if an appropriate route exists.
9414c6867a8SHajimu UMEMOTO 	 */
9424c6867a8SHajimu UMEMOTO 	if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
9434c6867a8SHajimu UMEMOTO 	    dst2->aio_srcsa.sa_family == AF_UNSPEC) {
9444c6867a8SHajimu UMEMOTO 		return(-1);
9454c6867a8SHajimu UMEMOTO 	}
9464c6867a8SHajimu UMEMOTO 	if (dst1->aio_srcsa.sa_family == AF_UNSPEC &&
9474c6867a8SHajimu UMEMOTO 	    dst2->aio_srcsa.sa_family != AF_UNSPEC) {
9484c6867a8SHajimu UMEMOTO 		return(1);
9494c6867a8SHajimu UMEMOTO 	}
9504c6867a8SHajimu UMEMOTO 
9514c6867a8SHajimu UMEMOTO 	/* Rule 2: Prefer matching scope. */
9524c6867a8SHajimu UMEMOTO 	if (dst1->aio_dstscope == dst1->aio_srcscope &&
9534c6867a8SHajimu UMEMOTO 	    dst2->aio_dstscope != dst2->aio_srcscope) {
9544c6867a8SHajimu UMEMOTO 		return(-1);
9554c6867a8SHajimu UMEMOTO 	}
9564c6867a8SHajimu UMEMOTO 	if (dst1->aio_dstscope != dst1->aio_srcscope &&
9574c6867a8SHajimu UMEMOTO 	    dst2->aio_dstscope == dst2->aio_srcscope) {
9584c6867a8SHajimu UMEMOTO 		return(1);
9594c6867a8SHajimu UMEMOTO 	}
9604c6867a8SHajimu UMEMOTO 
9614c6867a8SHajimu UMEMOTO 	/* Rule 3: Avoid deprecated addresses. */
9624c6867a8SHajimu UMEMOTO 	if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
9634c6867a8SHajimu UMEMOTO 	    dst2->aio_srcsa.sa_family != AF_UNSPEC) {
9644c6867a8SHajimu UMEMOTO 		if (!(dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
9654c6867a8SHajimu UMEMOTO 		    (dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
9664c6867a8SHajimu UMEMOTO 			return(-1);
9674c6867a8SHajimu UMEMOTO 		}
9684c6867a8SHajimu UMEMOTO 		if ((dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
9694c6867a8SHajimu UMEMOTO 		    !(dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
9704c6867a8SHajimu UMEMOTO 			return(1);
9714c6867a8SHajimu UMEMOTO 		}
9724c6867a8SHajimu UMEMOTO 	}
9734c6867a8SHajimu UMEMOTO 
9744c6867a8SHajimu UMEMOTO 	/* Rule 4: Prefer home addresses. */
9754c6867a8SHajimu UMEMOTO 	/* XXX: not implemented yet */
9764c6867a8SHajimu UMEMOTO 
9774c6867a8SHajimu UMEMOTO 	/* Rule 5: Prefer matching label. */
9784c6867a8SHajimu UMEMOTO #ifdef INET6
9794c6867a8SHajimu UMEMOTO 	if (dst1->aio_srcpolicy && dst1->aio_dstpolicy &&
9804c6867a8SHajimu UMEMOTO 	    dst1->aio_srcpolicy->pc_policy.label ==
9814c6867a8SHajimu UMEMOTO 	    dst1->aio_dstpolicy->pc_policy.label &&
9824c6867a8SHajimu UMEMOTO 	    (dst2->aio_srcpolicy == NULL || dst2->aio_dstpolicy == NULL ||
9834c6867a8SHajimu UMEMOTO 	     dst2->aio_srcpolicy->pc_policy.label !=
9844c6867a8SHajimu UMEMOTO 	     dst2->aio_dstpolicy->pc_policy.label)) {
9854c6867a8SHajimu UMEMOTO 		return(-1);
9864c6867a8SHajimu UMEMOTO 	}
9874c6867a8SHajimu UMEMOTO 	if (dst2->aio_srcpolicy && dst2->aio_dstpolicy &&
9884c6867a8SHajimu UMEMOTO 	    dst2->aio_srcpolicy->pc_policy.label ==
9894c6867a8SHajimu UMEMOTO 	    dst2->aio_dstpolicy->pc_policy.label &&
9904c6867a8SHajimu UMEMOTO 	    (dst1->aio_srcpolicy == NULL || dst1->aio_dstpolicy == NULL ||
9914c6867a8SHajimu UMEMOTO 	     dst1->aio_srcpolicy->pc_policy.label !=
9924c6867a8SHajimu UMEMOTO 	     dst1->aio_dstpolicy->pc_policy.label)) {
9934c6867a8SHajimu UMEMOTO 		return(1);
9944c6867a8SHajimu UMEMOTO 	}
9954c6867a8SHajimu UMEMOTO #endif
9964c6867a8SHajimu UMEMOTO 
9974c6867a8SHajimu UMEMOTO 	/* Rule 6: Prefer higher precedence. */
9984c6867a8SHajimu UMEMOTO #ifdef INET6
9994c6867a8SHajimu UMEMOTO 	if (dst1->aio_dstpolicy &&
10004c6867a8SHajimu UMEMOTO 	    (dst2->aio_dstpolicy == NULL ||
10014c6867a8SHajimu UMEMOTO 	     dst1->aio_dstpolicy->pc_policy.preced >
10024c6867a8SHajimu UMEMOTO 	     dst2->aio_dstpolicy->pc_policy.preced)) {
10034c6867a8SHajimu UMEMOTO 		return(-1);
10044c6867a8SHajimu UMEMOTO 	}
10054c6867a8SHajimu UMEMOTO 	if (dst2->aio_dstpolicy &&
10064c6867a8SHajimu UMEMOTO 	    (dst1->aio_dstpolicy == NULL ||
10074c6867a8SHajimu UMEMOTO 	     dst2->aio_dstpolicy->pc_policy.preced >
10084c6867a8SHajimu UMEMOTO 	     dst1->aio_dstpolicy->pc_policy.preced)) {
10094c6867a8SHajimu UMEMOTO 		return(1);
10104c6867a8SHajimu UMEMOTO 	}
10114c6867a8SHajimu UMEMOTO #endif
10124c6867a8SHajimu UMEMOTO 
10134c6867a8SHajimu UMEMOTO 	/* Rule 7: Prefer native transport. */
10144c6867a8SHajimu UMEMOTO 	/* XXX: not implemented yet */
10154c6867a8SHajimu UMEMOTO 
10164c6867a8SHajimu UMEMOTO 	/* Rule 8: Prefer smaller scope. */
10174c6867a8SHajimu UMEMOTO 	if (dst1->aio_dstscope >= 0 &&
10184c6867a8SHajimu UMEMOTO 	    dst1->aio_dstscope < dst2->aio_dstscope) {
10194c6867a8SHajimu UMEMOTO 		return(-1);
10204c6867a8SHajimu UMEMOTO 	}
10214c6867a8SHajimu UMEMOTO 	if (dst2->aio_dstscope >= 0 &&
10224c6867a8SHajimu UMEMOTO 	    dst2->aio_dstscope < dst1->aio_dstscope) {
10234c6867a8SHajimu UMEMOTO 		return(1);
10244c6867a8SHajimu UMEMOTO 	}
10254c6867a8SHajimu UMEMOTO 
10264c6867a8SHajimu UMEMOTO 	/*
10274c6867a8SHajimu UMEMOTO 	 * Rule 9: Use longest matching prefix.
10284c6867a8SHajimu UMEMOTO 	 * We compare the match length in a same AF only.
10294c6867a8SHajimu UMEMOTO 	 */
10304c6867a8SHajimu UMEMOTO 	if (dst1->aio_ai->ai_addr->sa_family ==
103129078909SHajimu UMEMOTO 	    dst2->aio_ai->ai_addr->sa_family &&
103229078909SHajimu UMEMOTO 	    dst1->aio_ai->ai_addr->sa_family != AF_INET) {
10334c6867a8SHajimu UMEMOTO 		if (dst1->aio_matchlen > dst2->aio_matchlen) {
10344c6867a8SHajimu UMEMOTO 			return(-1);
10354c6867a8SHajimu UMEMOTO 		}
10364c6867a8SHajimu UMEMOTO 		if (dst1->aio_matchlen < dst2->aio_matchlen) {
10374c6867a8SHajimu UMEMOTO 			return(1);
10384c6867a8SHajimu UMEMOTO 		}
10394c6867a8SHajimu UMEMOTO 	}
10404c6867a8SHajimu UMEMOTO 
10414c6867a8SHajimu UMEMOTO 	/* Rule 10: Otherwise, leave the order unchanged. */
10424c6867a8SHajimu UMEMOTO 	return(-1);
10434c6867a8SHajimu UMEMOTO }
10444c6867a8SHajimu UMEMOTO 
10454c6867a8SHajimu UMEMOTO /*
10464c6867a8SHajimu UMEMOTO  * Copy from scope.c.
10474c6867a8SHajimu UMEMOTO  * XXX: we should standardize the functions and link them as standard
10484c6867a8SHajimu UMEMOTO  * library.
10494c6867a8SHajimu UMEMOTO  */
10504c6867a8SHajimu UMEMOTO static int
105190d1d883SHajimu UMEMOTO gai_addr2scopetype(struct sockaddr *sa)
10524c6867a8SHajimu UMEMOTO {
10534c6867a8SHajimu UMEMOTO #ifdef INET6
10544c6867a8SHajimu UMEMOTO 	struct sockaddr_in6 *sa6;
10554c6867a8SHajimu UMEMOTO #endif
10564c6867a8SHajimu UMEMOTO 	struct sockaddr_in *sa4;
10574c6867a8SHajimu UMEMOTO 
10584c6867a8SHajimu UMEMOTO 	switch(sa->sa_family) {
10594c6867a8SHajimu UMEMOTO #ifdef INET6
10604c6867a8SHajimu UMEMOTO 	case AF_INET6:
10614c6867a8SHajimu UMEMOTO 		sa6 = (struct sockaddr_in6 *)sa;
10624c6867a8SHajimu UMEMOTO 		if (IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
10634c6867a8SHajimu UMEMOTO 			/* just use the scope field of the multicast address */
10644c6867a8SHajimu UMEMOTO 			return(sa6->sin6_addr.s6_addr[2] & 0x0f);
10654c6867a8SHajimu UMEMOTO 		}
10664c6867a8SHajimu UMEMOTO 		/*
10674c6867a8SHajimu UMEMOTO 		 * Unicast addresses: map scope type to corresponding scope
10684c6867a8SHajimu UMEMOTO 		 * value defined for multcast addresses.
10694c6867a8SHajimu UMEMOTO 		 * XXX: hardcoded scope type values are bad...
10704c6867a8SHajimu UMEMOTO 		 */
10714c6867a8SHajimu UMEMOTO 		if (IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr))
10724c6867a8SHajimu UMEMOTO 			return(1); /* node local scope */
10734c6867a8SHajimu UMEMOTO 		if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr))
10744c6867a8SHajimu UMEMOTO 			return(2); /* link-local scope */
10754c6867a8SHajimu UMEMOTO 		if (IN6_IS_ADDR_SITELOCAL(&sa6->sin6_addr))
10764c6867a8SHajimu UMEMOTO 			return(5); /* site-local scope */
10774c6867a8SHajimu UMEMOTO 		return(14);	/* global scope */
10784c6867a8SHajimu UMEMOTO 		break;
10794c6867a8SHajimu UMEMOTO #endif
10804c6867a8SHajimu UMEMOTO 	case AF_INET:
10814c6867a8SHajimu UMEMOTO 		/*
10824c6867a8SHajimu UMEMOTO 		 * IPv4 pseudo scoping according to RFC 3484.
10834c6867a8SHajimu UMEMOTO 		 */
10844c6867a8SHajimu UMEMOTO 		sa4 = (struct sockaddr_in *)sa;
10854c6867a8SHajimu UMEMOTO 		/* IPv4 autoconfiguration addresses have link-local scope. */
10864c6867a8SHajimu UMEMOTO 		if (((u_char *)&sa4->sin_addr)[0] == 169 &&
10874c6867a8SHajimu UMEMOTO 		    ((u_char *)&sa4->sin_addr)[1] == 254)
10884c6867a8SHajimu UMEMOTO 			return(2);
10894c6867a8SHajimu UMEMOTO 		/* Private addresses have site-local scope. */
10904c6867a8SHajimu UMEMOTO 		if (((u_char *)&sa4->sin_addr)[0] == 10 ||
10914c6867a8SHajimu UMEMOTO 		    (((u_char *)&sa4->sin_addr)[0] == 172 &&
10924c6867a8SHajimu UMEMOTO 		     (((u_char *)&sa4->sin_addr)[1] & 0xf0) == 16) ||
10934c6867a8SHajimu UMEMOTO 		    (((u_char *)&sa4->sin_addr)[0] == 192 &&
10944c6867a8SHajimu UMEMOTO 		     ((u_char *)&sa4->sin_addr)[1] == 168))
1095251b48a1SHajimu UMEMOTO 			return(14);	/* XXX: It should be 5 unless NAT */
10964c6867a8SHajimu UMEMOTO 		/* Loopback addresses have link-local scope. */
10974c6867a8SHajimu UMEMOTO 		if (((u_char *)&sa4->sin_addr)[0] == 127)
10984c6867a8SHajimu UMEMOTO 			return(2);
10994c6867a8SHajimu UMEMOTO 		return(14);
11004c6867a8SHajimu UMEMOTO 		break;
11014c6867a8SHajimu UMEMOTO 	default:
11024c6867a8SHajimu UMEMOTO 		errno = EAFNOSUPPORT; /* is this a good error? */
11034c6867a8SHajimu UMEMOTO 		return(-1);
11044c6867a8SHajimu UMEMOTO 	}
11054c6867a8SHajimu UMEMOTO }
11064c6867a8SHajimu UMEMOTO 
1107bc421492SHajimu UMEMOTO static int
1108bc421492SHajimu UMEMOTO explore_copy(const struct addrinfo *pai, const struct addrinfo *src0,
1109bc421492SHajimu UMEMOTO     struct addrinfo **res)
1110bc421492SHajimu UMEMOTO {
1111bc421492SHajimu UMEMOTO 	int error;
1112bc421492SHajimu UMEMOTO 	struct addrinfo sentinel, *cur;
1113bc421492SHajimu UMEMOTO 	const struct addrinfo *src;
1114bc421492SHajimu UMEMOTO 
1115bc421492SHajimu UMEMOTO 	error = 0;
1116bc421492SHajimu UMEMOTO 	sentinel.ai_next = NULL;
1117bc421492SHajimu UMEMOTO 	cur = &sentinel;
1118bc421492SHajimu UMEMOTO 
1119bc421492SHajimu UMEMOTO 	for (src = src0; src != NULL; src = src->ai_next) {
1120bc421492SHajimu UMEMOTO 		if (src->ai_family != pai->ai_family)
1121bc421492SHajimu UMEMOTO 			continue;
1122bc421492SHajimu UMEMOTO 
1123bc421492SHajimu UMEMOTO 		cur->ai_next = copy_ai(src);
1124bc421492SHajimu UMEMOTO 		if (!cur->ai_next) {
1125bc421492SHajimu UMEMOTO 			error = EAI_MEMORY;
1126bc421492SHajimu UMEMOTO 			goto fail;
1127bc421492SHajimu UMEMOTO 		}
1128bc421492SHajimu UMEMOTO 
1129bc421492SHajimu UMEMOTO 		cur->ai_next->ai_socktype = pai->ai_socktype;
1130bc421492SHajimu UMEMOTO 		cur->ai_next->ai_protocol = pai->ai_protocol;
1131bc421492SHajimu UMEMOTO 		cur = cur->ai_next;
1132bc421492SHajimu UMEMOTO 	}
1133bc421492SHajimu UMEMOTO 
1134bc421492SHajimu UMEMOTO 	*res = sentinel.ai_next;
1135bc421492SHajimu UMEMOTO 	return 0;
1136bc421492SHajimu UMEMOTO 
1137bc421492SHajimu UMEMOTO fail:
1138bc421492SHajimu UMEMOTO 	freeaddrinfo(sentinel.ai_next);
1139bc421492SHajimu UMEMOTO 	return error;
1140bc421492SHajimu UMEMOTO }
1141bc421492SHajimu UMEMOTO 
11427d56d374SYoshinobu Inoue /*
11437d56d374SYoshinobu Inoue  * hostname == NULL.
11447d56d374SYoshinobu Inoue  * passive socket -> anyaddr (0.0.0.0 or ::)
11457d56d374SYoshinobu Inoue  * non-passive socket -> localhost (127.0.0.1 or ::1)
11467d56d374SYoshinobu Inoue  */
11477d56d374SYoshinobu Inoue static int
114890d1d883SHajimu UMEMOTO explore_null(const struct addrinfo *pai, const char *servname,
114990d1d883SHajimu UMEMOTO     struct addrinfo **res)
11507d56d374SYoshinobu Inoue {
1151bf838688SHajimu UMEMOTO 	int s;
11527d56d374SYoshinobu Inoue 	const struct afd *afd;
11530aceddceSHajimu UMEMOTO 	struct addrinfo *ai;
11547d56d374SYoshinobu Inoue 	int error;
11557d56d374SYoshinobu Inoue 
11567d56d374SYoshinobu Inoue 	*res = NULL;
11570aceddceSHajimu UMEMOTO 	ai = NULL;
11587d56d374SYoshinobu Inoue 
1159bb957021SHiroki Sato 	if (pai->ai_family == PF_LOCAL)
1160bb957021SHiroki Sato 		return (0);
1161bb957021SHiroki Sato 
11627d56d374SYoshinobu Inoue 	/*
1163bf838688SHajimu UMEMOTO 	 * filter out AFs that are not supported by the kernel
1164bf838688SHajimu UMEMOTO 	 * XXX errno?
1165bf838688SHajimu UMEMOTO 	 */
116672531978SJilles Tjoelker 	s = _socket(pai->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
1167bf838688SHajimu UMEMOTO 	if (s < 0) {
1168bf838688SHajimu UMEMOTO 		if (errno != EMFILE)
1169bf838688SHajimu UMEMOTO 			return 0;
1170bf838688SHajimu UMEMOTO 	} else
1171bf838688SHajimu UMEMOTO 		_close(s);
1172bf838688SHajimu UMEMOTO 
11737d56d374SYoshinobu Inoue 	afd = find_afd(pai->ai_family);
11747d56d374SYoshinobu Inoue 	if (afd == NULL)
11757d56d374SYoshinobu Inoue 		return 0;
11767d56d374SYoshinobu Inoue 
1177a42af91cSHajimu UMEMOTO 	if (pai->ai_flags & AI_PASSIVE) {
11780aceddceSHajimu UMEMOTO 		GET_AI(ai, afd, afd->a_addrany);
11790aceddceSHajimu UMEMOTO 		GET_PORT(ai, servname);
1180a42af91cSHajimu UMEMOTO 	} else {
11810aceddceSHajimu UMEMOTO 		GET_AI(ai, afd, afd->a_loopback);
11820aceddceSHajimu UMEMOTO 		GET_PORT(ai, servname);
1183a42af91cSHajimu UMEMOTO 	}
11847d56d374SYoshinobu Inoue 
11850aceddceSHajimu UMEMOTO 	*res = ai;
11867d56d374SYoshinobu Inoue 	return 0;
11877d56d374SYoshinobu Inoue 
11887d56d374SYoshinobu Inoue free:
11890aceddceSHajimu UMEMOTO 	if (ai != NULL)
11900aceddceSHajimu UMEMOTO 		freeaddrinfo(ai);
11917d56d374SYoshinobu Inoue 	return error;
11927d56d374SYoshinobu Inoue }
11937d56d374SYoshinobu Inoue 
11947d56d374SYoshinobu Inoue /*
11957d56d374SYoshinobu Inoue  * numeric hostname
11967d56d374SYoshinobu Inoue  */
11977d56d374SYoshinobu Inoue static int
119890d1d883SHajimu UMEMOTO explore_numeric(const struct addrinfo *pai, const char *hostname,
119990d1d883SHajimu UMEMOTO     const char *servname, struct addrinfo **res, const char *canonname)
12007d56d374SYoshinobu Inoue {
12017d56d374SYoshinobu Inoue 	const struct afd *afd;
12020aceddceSHajimu UMEMOTO 	struct addrinfo *ai;
12037d56d374SYoshinobu Inoue 	int error;
1204bb957021SHiroki Sato 	char pton[PTON_MAX], path[PATH_MAX], *p;
12057d56d374SYoshinobu Inoue 
1206bb957021SHiroki Sato #ifdef CTASSERT
1207bb957021SHiroki Sato 	CTASSERT(sizeofmember(struct sockaddr_un, sun_path) <= PATH_MAX);
1208bb957021SHiroki Sato #endif
12097d56d374SYoshinobu Inoue 	*res = NULL;
12100aceddceSHajimu UMEMOTO 	ai = NULL;
12117d56d374SYoshinobu Inoue 
12127d56d374SYoshinobu Inoue 	afd = find_afd(pai->ai_family);
12137d56d374SYoshinobu Inoue 	if (afd == NULL)
12147d56d374SYoshinobu Inoue 		return 0;
12157d56d374SYoshinobu Inoue 
1216b826397aSJun-ichiro itojun Hagino 	switch (afd->a_af) {
1217bb957021SHiroki Sato 	case AF_LOCAL:
1218bb957021SHiroki Sato 		if (hostname[0] != '/')
1219bb957021SHiroki Sato 			ERR(EAI_NONAME);
1220bb957021SHiroki Sato 		if (strlen(hostname) > afd->a_addrlen)
1221bb957021SHiroki Sato 			ERR(EAI_MEMORY);
1222bb957021SHiroki Sato 		/* NUL-termination does not need to be guaranteed. */
1223bb957021SHiroki Sato 		strncpy(path, hostname, afd->a_addrlen);
1224bb957021SHiroki Sato 		p = &path[0];
1225bb957021SHiroki Sato 		break;
1226b826397aSJun-ichiro itojun Hagino 	case AF_INET:
12270cf41987SHajimu UMEMOTO 		/*
12280cf41987SHajimu UMEMOTO 		 * RFC3493 requires getaddrinfo() to accept AF_INET formats
12290cf41987SHajimu UMEMOTO 		 * that are accepted by inet_addr() and its family.  The
12300cf41987SHajimu UMEMOTO 		 * accepted forms includes the "classful" one, which inet_pton
12310cf41987SHajimu UMEMOTO 		 * does not accept.  So we need to separate the case for
12320cf41987SHajimu UMEMOTO 		 * AF_INET.
12330cf41987SHajimu UMEMOTO 		 */
1234da83bf18SHajimu UMEMOTO 		if (inet_aton(hostname, (struct in_addr *)pton) != 1)
1235da83bf18SHajimu UMEMOTO 			return 0;
1236bb957021SHiroki Sato 		p = pton;
1237b826397aSJun-ichiro itojun Hagino 		break;
1238b826397aSJun-ichiro itojun Hagino 	default:
1239da83bf18SHajimu UMEMOTO 		if (inet_pton(afd->a_af, hostname, pton) != 1)
1240da83bf18SHajimu UMEMOTO 			return 0;
1241bb957021SHiroki Sato 		p = pton;
1242da83bf18SHajimu UMEMOTO 		break;
1243da83bf18SHajimu UMEMOTO 	}
1244da83bf18SHajimu UMEMOTO 
1245da83bf18SHajimu UMEMOTO 	if (pai->ai_family == afd->a_af) {
1246bb957021SHiroki Sato 		GET_AI(ai, afd, p);
12470aceddceSHajimu UMEMOTO 		GET_PORT(ai, servname);
12484c60a05dSHiroki Sato 		if ((pai->ai_family == AF_INET ||
12494c60a05dSHiroki Sato 		     pai->ai_family == AF_INET6) &&
12504c60a05dSHiroki Sato 		    (pai->ai_flags & AI_CANONNAME)) {
12518755b918SHajimu UMEMOTO 			/*
1252da83bf18SHajimu UMEMOTO 			 * Set the numeric address itself as the canonical
1253da83bf18SHajimu UMEMOTO 			 * name, based on a clarification in RFC3493.
12548755b918SHajimu UMEMOTO 			 */
12550aceddceSHajimu UMEMOTO 			GET_CANONNAME(ai, canonname);
12568755b918SHajimu UMEMOTO 		}
1257da83bf18SHajimu UMEMOTO 	} else {
1258da83bf18SHajimu UMEMOTO 		/*
1259da83bf18SHajimu UMEMOTO 		 * XXX: This should not happen since we already matched the AF
1260da83bf18SHajimu UMEMOTO 		 * by find_afd.
1261da83bf18SHajimu UMEMOTO 		 */
1262da83bf18SHajimu UMEMOTO 		ERR(EAI_FAMILY);
1263b826397aSJun-ichiro itojun Hagino 	}
12647d56d374SYoshinobu Inoue 
12650aceddceSHajimu UMEMOTO 	*res = ai;
12667d56d374SYoshinobu Inoue 	return 0;
12677d56d374SYoshinobu Inoue 
12687d56d374SYoshinobu Inoue free:
12697d56d374SYoshinobu Inoue bad:
12700aceddceSHajimu UMEMOTO 	if (ai != NULL)
12710aceddceSHajimu UMEMOTO 		freeaddrinfo(ai);
12727d56d374SYoshinobu Inoue 	return error;
12737d56d374SYoshinobu Inoue }
12747d56d374SYoshinobu Inoue 
12757d56d374SYoshinobu Inoue /*
12767d56d374SYoshinobu Inoue  * numeric hostname with scope
12777d56d374SYoshinobu Inoue  */
12787d56d374SYoshinobu Inoue static int
127990d1d883SHajimu UMEMOTO explore_numeric_scope(const struct addrinfo *pai, const char *hostname,
128090d1d883SHajimu UMEMOTO     const char *servname, struct addrinfo **res)
12817d56d374SYoshinobu Inoue {
1282a42af91cSHajimu UMEMOTO #if !defined(SCOPE_DELIMITER) || !defined(INET6)
12838755b918SHajimu UMEMOTO 	return explore_numeric(pai, hostname, servname, res, hostname);
12847d56d374SYoshinobu Inoue #else
12857d56d374SYoshinobu Inoue 	const struct afd *afd;
12867d56d374SYoshinobu Inoue 	struct addrinfo *cur;
12877d56d374SYoshinobu Inoue 	int error;
1288a42af91cSHajimu UMEMOTO 	char *cp, *hostname2 = NULL, *scope, *addr;
12897d56d374SYoshinobu Inoue 	struct sockaddr_in6 *sin6;
12907d56d374SYoshinobu Inoue 
12917d56d374SYoshinobu Inoue 	afd = find_afd(pai->ai_family);
12927d56d374SYoshinobu Inoue 	if (afd == NULL)
12937d56d374SYoshinobu Inoue 		return 0;
1294248aee62SJacques Vidrine 
12957d56d374SYoshinobu Inoue 	if (!afd->a_scoped)
12968755b918SHajimu UMEMOTO 		return explore_numeric(pai, hostname, servname, res, hostname);
12977d56d374SYoshinobu Inoue 
12987d56d374SYoshinobu Inoue 	cp = strchr(hostname, SCOPE_DELIMITER);
12997d56d374SYoshinobu Inoue 	if (cp == NULL)
13008755b918SHajimu UMEMOTO 		return explore_numeric(pai, hostname, servname, res, hostname);
13017d56d374SYoshinobu Inoue 
13027d56d374SYoshinobu Inoue 	/*
13037d56d374SYoshinobu Inoue 	 * Handle special case of <scoped_address><delimiter><scope id>
13047d56d374SYoshinobu Inoue 	 */
13057d56d374SYoshinobu Inoue 	hostname2 = strdup(hostname);
13067d56d374SYoshinobu Inoue 	if (hostname2 == NULL)
13077d56d374SYoshinobu Inoue 		return EAI_MEMORY;
13087d56d374SYoshinobu Inoue 	/* terminate at the delimiter */
13097d56d374SYoshinobu Inoue 	hostname2[cp - hostname] = '\0';
1310a42af91cSHajimu UMEMOTO 	addr = hostname2;
1311a42af91cSHajimu UMEMOTO 	scope = cp + 1;
13127d56d374SYoshinobu Inoue 
13138755b918SHajimu UMEMOTO 	error = explore_numeric(pai, addr, servname, res, hostname);
13147d56d374SYoshinobu Inoue 	if (error == 0) {
1315a89c1d30SHajimu UMEMOTO 		u_int32_t scopeid;
1316a42af91cSHajimu UMEMOTO 
13177d56d374SYoshinobu Inoue 		for (cur = *res; cur; cur = cur->ai_next) {
13187d56d374SYoshinobu Inoue 			if (cur->ai_family != AF_INET6)
13197d56d374SYoshinobu Inoue 				continue;
1320a42af91cSHajimu UMEMOTO 			sin6 = (struct sockaddr_in6 *)(void *)cur->ai_addr;
1321a89c1d30SHajimu UMEMOTO 			if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
1322a42af91cSHajimu UMEMOTO 				free(hostname2);
1323bc421492SHajimu UMEMOTO 				freeaddrinfo(*res);
1324bc421492SHajimu UMEMOTO 				*res = NULL;
132537b3e941SHajimu UMEMOTO 				return(EAI_NONAME); /* XXX: is return OK? */
1326a42af91cSHajimu UMEMOTO 			}
1327a42af91cSHajimu UMEMOTO 			sin6->sin6_scope_id = scopeid;
13287d56d374SYoshinobu Inoue 		}
13297d56d374SYoshinobu Inoue 	}
13307d56d374SYoshinobu Inoue 
13317d56d374SYoshinobu Inoue 	free(hostname2);
13327d56d374SYoshinobu Inoue 
1333bc421492SHajimu UMEMOTO 	if (error && *res) {
1334bc421492SHajimu UMEMOTO 		freeaddrinfo(*res);
1335bc421492SHajimu UMEMOTO 		*res = NULL;
1336bc421492SHajimu UMEMOTO 	}
13377d56d374SYoshinobu Inoue 	return error;
13387d56d374SYoshinobu Inoue #endif
13397d56d374SYoshinobu Inoue }
13407d56d374SYoshinobu Inoue 
13417d56d374SYoshinobu Inoue static int
134290d1d883SHajimu UMEMOTO get_canonname(const struct addrinfo *pai, struct addrinfo *ai, const char *str)
13437d56d374SYoshinobu Inoue {
13447d56d374SYoshinobu Inoue 	if ((pai->ai_flags & AI_CANONNAME) != 0) {
13458743b270SHajimu UMEMOTO 		ai->ai_canonname = strdup(str);
13467d56d374SYoshinobu Inoue 		if (ai->ai_canonname == NULL)
13477d56d374SYoshinobu Inoue 			return EAI_MEMORY;
13487d56d374SYoshinobu Inoue 	}
13497d56d374SYoshinobu Inoue 	return 0;
13507d56d374SYoshinobu Inoue }
13517d56d374SYoshinobu Inoue 
13527d56d374SYoshinobu Inoue static struct addrinfo *
135390d1d883SHajimu UMEMOTO get_ai(const struct addrinfo *pai, const struct afd *afd, const char *addr)
13547d56d374SYoshinobu Inoue {
13557d56d374SYoshinobu Inoue 	char *p;
13567d56d374SYoshinobu Inoue 	struct addrinfo *ai;
13577d56d374SYoshinobu Inoue 
13587d56d374SYoshinobu Inoue 	ai = (struct addrinfo *)malloc(sizeof(struct addrinfo)
13597d56d374SYoshinobu Inoue 		+ (afd->a_socklen));
13607d56d374SYoshinobu Inoue 	if (ai == NULL)
13617d56d374SYoshinobu Inoue 		return NULL;
13627d56d374SYoshinobu Inoue 
13637d56d374SYoshinobu Inoue 	memcpy(ai, pai, sizeof(struct addrinfo));
1364a42af91cSHajimu UMEMOTO 	ai->ai_addr = (struct sockaddr *)(void *)(ai + 1);
1365a42af91cSHajimu UMEMOTO 	memset(ai->ai_addr, 0, (size_t)afd->a_socklen);
13667d56d374SYoshinobu Inoue 	ai->ai_addr->sa_len = afd->a_socklen;
13677d56d374SYoshinobu Inoue 	ai->ai_addrlen = afd->a_socklen;
1368bb957021SHiroki Sato 	if (ai->ai_family == PF_LOCAL) {
1369bb957021SHiroki Sato 		size_t n = strnlen(addr, afd->a_addrlen);
1370bb957021SHiroki Sato 
1371bb957021SHiroki Sato 		ai->ai_addrlen -= afd->a_addrlen - n;
1372bb957021SHiroki Sato 		ai->ai_addr->sa_len -= afd->a_addrlen - n;
1373bb957021SHiroki Sato 	}
13747d56d374SYoshinobu Inoue 	ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
1375248aee62SJacques Vidrine 	p = (char *)(void *)(ai->ai_addr);
1376248aee62SJacques Vidrine 	memcpy(p + afd->a_off, addr, (size_t)afd->a_addrlen);
13777d56d374SYoshinobu Inoue 	return ai;
13787d56d374SYoshinobu Inoue }
13797d56d374SYoshinobu Inoue 
1380bc421492SHajimu UMEMOTO /* XXX need to malloc() the same way we do from other functions! */
1381bc421492SHajimu UMEMOTO static struct addrinfo *
1382bc421492SHajimu UMEMOTO copy_ai(const struct addrinfo *pai)
1383bc421492SHajimu UMEMOTO {
1384bc421492SHajimu UMEMOTO 	struct addrinfo *ai;
1385bc421492SHajimu UMEMOTO 	size_t l;
1386bc421492SHajimu UMEMOTO 
1387bc421492SHajimu UMEMOTO 	l = sizeof(*ai) + pai->ai_addrlen;
1388bc421492SHajimu UMEMOTO 	if ((ai = (struct addrinfo *)malloc(l)) == NULL)
1389bc421492SHajimu UMEMOTO 		return NULL;
1390bc421492SHajimu UMEMOTO 	memset(ai, 0, l);
1391bc421492SHajimu UMEMOTO 	memcpy(ai, pai, sizeof(*ai));
1392bc421492SHajimu UMEMOTO 	ai->ai_addr = (struct sockaddr *)(void *)(ai + 1);
1393bc421492SHajimu UMEMOTO 	memcpy(ai->ai_addr, pai->ai_addr, pai->ai_addrlen);
1394bc421492SHajimu UMEMOTO 
1395bc421492SHajimu UMEMOTO 	if (pai->ai_canonname) {
1396bc421492SHajimu UMEMOTO 		l = strlen(pai->ai_canonname) + 1;
1397bc421492SHajimu UMEMOTO 		if ((ai->ai_canonname = malloc(l)) == NULL) {
1398bc421492SHajimu UMEMOTO 			free(ai);
1399bc421492SHajimu UMEMOTO 			return NULL;
1400bc421492SHajimu UMEMOTO 		}
1401bc421492SHajimu UMEMOTO 		strlcpy(ai->ai_canonname, pai->ai_canonname, l);
1402bc421492SHajimu UMEMOTO 	} else {
1403bc421492SHajimu UMEMOTO 		/* just to make sure */
1404bc421492SHajimu UMEMOTO 		ai->ai_canonname = NULL;
1405bc421492SHajimu UMEMOTO 	}
1406bc421492SHajimu UMEMOTO 
1407bc421492SHajimu UMEMOTO 	ai->ai_next = NULL;
1408bc421492SHajimu UMEMOTO 
1409bc421492SHajimu UMEMOTO 	return ai;
1410bc421492SHajimu UMEMOTO }
1411bc421492SHajimu UMEMOTO 
14127d56d374SYoshinobu Inoue static int
141390d1d883SHajimu UMEMOTO get_portmatch(const struct addrinfo *ai, const char *servname)
14147d56d374SYoshinobu Inoue {
1415a42af91cSHajimu UMEMOTO 
1416c224435eSHajimu UMEMOTO 	/* get_port does not touch first argument when matchonly == 1. */
1417a42af91cSHajimu UMEMOTO 	/* LINTED const cast */
14187d56d374SYoshinobu Inoue 	return get_port((struct addrinfo *)ai, servname, 1);
14197d56d374SYoshinobu Inoue }
14207d56d374SYoshinobu Inoue 
14217d56d374SYoshinobu Inoue static int
142290d1d883SHajimu UMEMOTO get_port(struct addrinfo *ai, const char *servname, int matchonly)
14237d56d374SYoshinobu Inoue {
14247d56d374SYoshinobu Inoue 	const char *proto;
14257d56d374SYoshinobu Inoue 	struct servent *sp;
1426e24e9d99SHajimu UMEMOTO 	int port, error;
14277d56d374SYoshinobu Inoue 	int allownumeric;
14287d56d374SYoshinobu Inoue 
14297d56d374SYoshinobu Inoue 	if (servname == NULL)
14307d56d374SYoshinobu Inoue 		return 0;
1431a42af91cSHajimu UMEMOTO 	switch (ai->ai_family) {
1432bb957021SHiroki Sato 	case AF_LOCAL:
1433bb957021SHiroki Sato 		/* AF_LOCAL ignores servname silently. */
1434bb957021SHiroki Sato 		return (0);
1435a42af91cSHajimu UMEMOTO 	case AF_INET:
1436a42af91cSHajimu UMEMOTO #ifdef AF_INET6
1437a42af91cSHajimu UMEMOTO 	case AF_INET6:
14387d56d374SYoshinobu Inoue #endif
1439a42af91cSHajimu UMEMOTO 		break;
1440a42af91cSHajimu UMEMOTO 	default:
14417d56d374SYoshinobu Inoue 		return 0;
1442a42af91cSHajimu UMEMOTO 	}
14437d56d374SYoshinobu Inoue 
14447d56d374SYoshinobu Inoue 	switch (ai->ai_socktype) {
14457d56d374SYoshinobu Inoue 	case SOCK_RAW:
14467d56d374SYoshinobu Inoue 		return EAI_SERVICE;
14477d56d374SYoshinobu Inoue 	case SOCK_DGRAM:
14487d56d374SYoshinobu Inoue 	case SOCK_STREAM:
14494d756307SHajimu UMEMOTO 	case SOCK_SEQPACKET:
14507d56d374SYoshinobu Inoue 		allownumeric = 1;
14517d56d374SYoshinobu Inoue 		break;
14527d56d374SYoshinobu Inoue 	case ANY:
1453d53978e3SHajimu UMEMOTO 		switch (ai->ai_family) {
1454d53978e3SHajimu UMEMOTO 		case AF_INET:
1455d53978e3SHajimu UMEMOTO #ifdef AF_INET6
1456d53978e3SHajimu UMEMOTO 		case AF_INET6:
1457d53978e3SHajimu UMEMOTO #endif
1458d53978e3SHajimu UMEMOTO 			allownumeric = 1;
1459d53978e3SHajimu UMEMOTO 			break;
1460d53978e3SHajimu UMEMOTO 		default:
14617d56d374SYoshinobu Inoue 			allownumeric = 0;
14627d56d374SYoshinobu Inoue 			break;
1463d53978e3SHajimu UMEMOTO 		}
1464d53978e3SHajimu UMEMOTO 		break;
14657d56d374SYoshinobu Inoue 	default:
14667d56d374SYoshinobu Inoue 		return EAI_SOCKTYPE;
14677d56d374SYoshinobu Inoue 	}
14687d56d374SYoshinobu Inoue 
1469e24e9d99SHajimu UMEMOTO 	error = str2number(servname, &port);
1470e24e9d99SHajimu UMEMOTO 	if (error == 0) {
14717d56d374SYoshinobu Inoue 		if (!allownumeric)
14727d56d374SYoshinobu Inoue 			return EAI_SERVICE;
14737d56d374SYoshinobu Inoue 		if (port < 0 || port > 65535)
14747d56d374SYoshinobu Inoue 			return EAI_SERVICE;
1475dd521ef1SHajimu UMEMOTO 		port = htons(port);
14767d56d374SYoshinobu Inoue 	} else {
1477c224435eSHajimu UMEMOTO 		if (ai->ai_flags & AI_NUMERICSERV)
1478c224435eSHajimu UMEMOTO 			return EAI_NONAME;
14794d756307SHajimu UMEMOTO 
14804d756307SHajimu UMEMOTO 		switch (ai->ai_protocol) {
14814d756307SHajimu UMEMOTO 		case IPPROTO_UDP:
14827d56d374SYoshinobu Inoue 			proto = "udp";
14837d56d374SYoshinobu Inoue 			break;
14844d756307SHajimu UMEMOTO 		case IPPROTO_TCP:
14857d56d374SYoshinobu Inoue 			proto = "tcp";
14867d56d374SYoshinobu Inoue 			break;
14874d756307SHajimu UMEMOTO 		case IPPROTO_SCTP:
14884d756307SHajimu UMEMOTO 			proto = "sctp";
14894d756307SHajimu UMEMOTO 			break;
1490e06e816fSKevin Lo 		case IPPROTO_UDPLITE:
1491e06e816fSKevin Lo 			proto = "udplite";
1492e06e816fSKevin Lo 			break;
14937d56d374SYoshinobu Inoue 		default:
14947d56d374SYoshinobu Inoue 			proto = NULL;
14957d56d374SYoshinobu Inoue 			break;
14967d56d374SYoshinobu Inoue 		}
14977d56d374SYoshinobu Inoue 
149862e8b17dSHajimu UMEMOTO 		if ((sp = getservbyname(servname, proto)) == NULL)
14997d56d374SYoshinobu Inoue 			return EAI_SERVICE;
15007d56d374SYoshinobu Inoue 		port = sp->s_port;
15017d56d374SYoshinobu Inoue 	}
15027d56d374SYoshinobu Inoue 
15037d56d374SYoshinobu Inoue 	if (!matchonly) {
15047d56d374SYoshinobu Inoue 		switch (ai->ai_family) {
15057d56d374SYoshinobu Inoue 		case AF_INET:
1506a42af91cSHajimu UMEMOTO 			((struct sockaddr_in *)(void *)
1507a42af91cSHajimu UMEMOTO 			    ai->ai_addr)->sin_port = port;
15087d56d374SYoshinobu Inoue 			break;
15097d56d374SYoshinobu Inoue #ifdef INET6
15107d56d374SYoshinobu Inoue 		case AF_INET6:
1511a42af91cSHajimu UMEMOTO 			((struct sockaddr_in6 *)(void *)
1512a42af91cSHajimu UMEMOTO 			    ai->ai_addr)->sin6_port = port;
15137d56d374SYoshinobu Inoue 			break;
15147d56d374SYoshinobu Inoue #endif
15157d56d374SYoshinobu Inoue 		}
15167d56d374SYoshinobu Inoue 	}
15177d56d374SYoshinobu Inoue 
15187d56d374SYoshinobu Inoue 	return 0;
15197d56d374SYoshinobu Inoue }
15207d56d374SYoshinobu Inoue 
15217d56d374SYoshinobu Inoue static const struct afd *
152290d1d883SHajimu UMEMOTO find_afd(int af)
15237d56d374SYoshinobu Inoue {
15247d56d374SYoshinobu Inoue 	const struct afd *afd;
15257d56d374SYoshinobu Inoue 
15267d56d374SYoshinobu Inoue 	if (af == PF_UNSPEC)
15277d56d374SYoshinobu Inoue 		return NULL;
15287d56d374SYoshinobu Inoue 	for (afd = afdl; afd->a_af; afd++) {
15297d56d374SYoshinobu Inoue 		if (afd->a_af == af)
15307d56d374SYoshinobu Inoue 			return afd;
15317d56d374SYoshinobu Inoue 	}
15327d56d374SYoshinobu Inoue 	return NULL;
15337d56d374SYoshinobu Inoue }
1534a42af91cSHajimu UMEMOTO 
1535a42af91cSHajimu UMEMOTO /*
153617f53388SHajimu UMEMOTO  * RFC 3493: AI_ADDRCONFIG check.  Determines which address families are
15376f63a114SHajimu UMEMOTO  * configured on the local system and correlates with pai->ai_family value.
15386f63a114SHajimu UMEMOTO  * If an address family is not configured on the system, it will not be
15396f63a114SHajimu UMEMOTO  * queried for.  For this purpose, loopback addresses are not considered
15406f63a114SHajimu UMEMOTO  * configured addresses.
1541bf838688SHajimu UMEMOTO  *
1542bf838688SHajimu UMEMOTO  * XXX PF_UNSPEC -> PF_INET6 + PF_INET mapping needs to be in sync with
1543bf838688SHajimu UMEMOTO  * _dns_getaddrinfo.
1544a42af91cSHajimu UMEMOTO  */
1545a42af91cSHajimu UMEMOTO static int
154690d1d883SHajimu UMEMOTO addrconfig(struct addrinfo *pai)
1547a42af91cSHajimu UMEMOTO {
15486f63a114SHajimu UMEMOTO 	struct ifaddrs *ifaddrs, *ifa;
154917f53388SHajimu UMEMOTO 	struct sockaddr_in *sin;
15505d9d18f2SHajimu UMEMOTO #ifdef INET6
155117f53388SHajimu UMEMOTO 	struct sockaddr_in6 *sin6;
15525d9d18f2SHajimu UMEMOTO #endif
15536f63a114SHajimu UMEMOTO 	int seen_inet = 0, seen_inet6 = 0;
1554a42af91cSHajimu UMEMOTO 
15556f63a114SHajimu UMEMOTO 	if (getifaddrs(&ifaddrs) != 0)
155617f53388SHajimu UMEMOTO 		return (0);
15576f63a114SHajimu UMEMOTO 
15586f63a114SHajimu UMEMOTO 	for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) {
15596f63a114SHajimu UMEMOTO 		if (ifa->ifa_addr == NULL || (ifa->ifa_flags & IFF_UP) == 0)
15606f63a114SHajimu UMEMOTO 			continue;
15616f63a114SHajimu UMEMOTO 		switch (ifa->ifa_addr->sa_family) {
15626f63a114SHajimu UMEMOTO 		case AF_INET:
156317f53388SHajimu UMEMOTO 			if (seen_inet)
156417f53388SHajimu UMEMOTO 				continue;
156517f53388SHajimu UMEMOTO 			sin = (struct sockaddr_in *)(ifa->ifa_addr);
1566*ea24ff47SHajimu UMEMOTO 			if (htonl(sin->sin_addr.s_addr) == INADDR_LOOPBACK)
156717f53388SHajimu UMEMOTO 				continue;
15686f63a114SHajimu UMEMOTO 			seen_inet = 1;
15696f63a114SHajimu UMEMOTO 			break;
15706f63a114SHajimu UMEMOTO #ifdef INET6
15716f63a114SHajimu UMEMOTO 		case AF_INET6:
157217f53388SHajimu UMEMOTO 			if (seen_inet6)
157317f53388SHajimu UMEMOTO 				continue;
157417f53388SHajimu UMEMOTO 			sin6 = (struct sockaddr_in6 *)(ifa->ifa_addr);
157517f53388SHajimu UMEMOTO 			if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
157617f53388SHajimu UMEMOTO 				continue;
157717f53388SHajimu UMEMOTO 			if ((ifa->ifa_flags & IFT_LOOP) != 0 &&
157817f53388SHajimu UMEMOTO 			    IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
157917f53388SHajimu UMEMOTO 				continue;
158017f53388SHajimu UMEMOTO 			if (is_ifdisabled(ifa->ifa_name))
158117f53388SHajimu UMEMOTO 				continue;
15826f63a114SHajimu UMEMOTO 			seen_inet6 = 1;
15836f63a114SHajimu UMEMOTO 			break;
15846f63a114SHajimu UMEMOTO #endif
1585bf838688SHajimu UMEMOTO 		}
15866f63a114SHajimu UMEMOTO 	}
15876f63a114SHajimu UMEMOTO 	freeifaddrs(ifaddrs);
15886f63a114SHajimu UMEMOTO 
15896f63a114SHajimu UMEMOTO 	switch(pai->ai_family) {
15906f63a114SHajimu UMEMOTO 	case AF_INET6:
159117f53388SHajimu UMEMOTO 		return (seen_inet6);
15926f63a114SHajimu UMEMOTO 	case AF_INET:
159317f53388SHajimu UMEMOTO 		return (seen_inet);
15946f63a114SHajimu UMEMOTO 	case AF_UNSPEC:
15956f63a114SHajimu UMEMOTO 		if (seen_inet == seen_inet6)
159617f53388SHajimu UMEMOTO 			return (seen_inet);
15976f63a114SHajimu UMEMOTO 		pai->ai_family = seen_inet ? AF_INET : AF_INET6;
159817f53388SHajimu UMEMOTO 		return (1);
15996f63a114SHajimu UMEMOTO 	}
160017f53388SHajimu UMEMOTO 	return (1);
1601a42af91cSHajimu UMEMOTO }
1602a42af91cSHajimu UMEMOTO 
1603a42af91cSHajimu UMEMOTO #ifdef INET6
16046f63a114SHajimu UMEMOTO static int
16056f63a114SHajimu UMEMOTO is_ifdisabled(char *name)
16066f63a114SHajimu UMEMOTO {
16076f63a114SHajimu UMEMOTO 	struct in6_ndireq nd;
16086f63a114SHajimu UMEMOTO 	int fd;
16096f63a114SHajimu UMEMOTO 
1610cfaa2a09SHajimu UMEMOTO 	if ((fd = _socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0)) < 0)
161117f53388SHajimu UMEMOTO 		return (-1);
16126f63a114SHajimu UMEMOTO 	memset(&nd, 0, sizeof(nd));
16136f63a114SHajimu UMEMOTO 	strlcpy(nd.ifname, name, sizeof(nd.ifname));
16146f63a114SHajimu UMEMOTO 	if (_ioctl(fd, SIOCGIFINFO_IN6, &nd) < 0) {
16156f63a114SHajimu UMEMOTO 		_close(fd);
161617f53388SHajimu UMEMOTO 		return (-1);
16176f63a114SHajimu UMEMOTO 	}
16186f63a114SHajimu UMEMOTO 	_close(fd);
16196f63a114SHajimu UMEMOTO 	return ((nd.ndi.flags & ND6_IFF_IFDISABLED) != 0);
16206f63a114SHajimu UMEMOTO }
16216f63a114SHajimu UMEMOTO 
1622a42af91cSHajimu UMEMOTO /* convert a string to a scope identifier. XXX: IPv6 specific */
1623a42af91cSHajimu UMEMOTO static int
162490d1d883SHajimu UMEMOTO ip6_str2scopeid(char *scope, struct sockaddr_in6 *sin6, u_int32_t *scopeid)
1625a42af91cSHajimu UMEMOTO {
1626a89c1d30SHajimu UMEMOTO 	u_long lscopeid;
1627bf838688SHajimu UMEMOTO 	struct in6_addr *a6;
1628a42af91cSHajimu UMEMOTO 	char *ep;
1629a42af91cSHajimu UMEMOTO 
1630bf838688SHajimu UMEMOTO 	a6 = &sin6->sin6_addr;
1631bf838688SHajimu UMEMOTO 
1632ec20fe00SJun-ichiro itojun Hagino 	/* empty scopeid portion is invalid */
1633ec20fe00SJun-ichiro itojun Hagino 	if (*scope == '\0')
1634ec20fe00SJun-ichiro itojun Hagino 		return -1;
1635ec20fe00SJun-ichiro itojun Hagino 
1636324930d7SHajimu UMEMOTO 	if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6) ||
1637324930d7SHajimu UMEMOTO 	    IN6_IS_ADDR_MC_NODELOCAL(a6)) {
1638a42af91cSHajimu UMEMOTO 		/*
1639a42af91cSHajimu UMEMOTO 		 * We currently assume a one-to-one mapping between links
1640a42af91cSHajimu UMEMOTO 		 * and interfaces, so we simply use interface indices for
1641a42af91cSHajimu UMEMOTO 		 * like-local scopes.
1642a42af91cSHajimu UMEMOTO 		 */
1643a89c1d30SHajimu UMEMOTO 		*scopeid = if_nametoindex(scope);
1644a89c1d30SHajimu UMEMOTO 		if (*scopeid == 0)
1645a42af91cSHajimu UMEMOTO 			goto trynumeric;
1646a89c1d30SHajimu UMEMOTO 		return 0;
1647a42af91cSHajimu UMEMOTO 	}
1648a42af91cSHajimu UMEMOTO 
1649a42af91cSHajimu UMEMOTO 	/* still unclear about literal, allow numeric only - placeholder */
1650a42af91cSHajimu UMEMOTO 	if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6))
1651a42af91cSHajimu UMEMOTO 		goto trynumeric;
1652a42af91cSHajimu UMEMOTO 	if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
1653a42af91cSHajimu UMEMOTO 		goto trynumeric;
1654a42af91cSHajimu UMEMOTO 	else
1655a42af91cSHajimu UMEMOTO 		goto trynumeric;	/* global */
1656a42af91cSHajimu UMEMOTO 
1657a42af91cSHajimu UMEMOTO 	/* try to convert to a numeric id as a last resort */
1658a42af91cSHajimu UMEMOTO   trynumeric:
1659a89c1d30SHajimu UMEMOTO 	errno = 0;
1660a89c1d30SHajimu UMEMOTO 	lscopeid = strtoul(scope, &ep, 10);
1661a89c1d30SHajimu UMEMOTO 	*scopeid = (u_int32_t)(lscopeid & 0xffffffffUL);
1662a89c1d30SHajimu UMEMOTO 	if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
1663a89c1d30SHajimu UMEMOTO 		return 0;
1664a42af91cSHajimu UMEMOTO 	else
1665a42af91cSHajimu UMEMOTO 		return -1;
1666a42af91cSHajimu UMEMOTO }
1667a42af91cSHajimu UMEMOTO #endif
1668a42af91cSHajimu UMEMOTO 
166906a99fe3SHajimu UMEMOTO 
167006a99fe3SHajimu UMEMOTO #ifdef NS_CACHING
167106a99fe3SHajimu UMEMOTO static int
167206a99fe3SHajimu UMEMOTO addrinfo_id_func(char *buffer, size_t *buffer_size, va_list ap,
167306a99fe3SHajimu UMEMOTO     void *cache_mdata)
167406a99fe3SHajimu UMEMOTO {
167506a99fe3SHajimu UMEMOTO 	res_state statp;
167606a99fe3SHajimu UMEMOTO 	u_long res_options;
167706a99fe3SHajimu UMEMOTO 
167806a99fe3SHajimu UMEMOTO 	const int op_id = 0;	/* identifies the getaddrinfo for the cache */
167906a99fe3SHajimu UMEMOTO 	char *hostname;
168006a99fe3SHajimu UMEMOTO 	struct addrinfo *hints;
168106a99fe3SHajimu UMEMOTO 
168206a99fe3SHajimu UMEMOTO 	char *p;
168306a99fe3SHajimu UMEMOTO 	int ai_flags, ai_family, ai_socktype, ai_protocol;
168406a99fe3SHajimu UMEMOTO 	size_t desired_size, size;
168506a99fe3SHajimu UMEMOTO 
168606a99fe3SHajimu UMEMOTO 	statp = __res_state();
168706a99fe3SHajimu UMEMOTO 	res_options = statp->options & (RES_RECURSE | RES_DEFNAMES |
168806a99fe3SHajimu UMEMOTO 	    RES_DNSRCH | RES_NOALIASES | RES_USE_INET6);
168906a99fe3SHajimu UMEMOTO 
169006a99fe3SHajimu UMEMOTO 	hostname = va_arg(ap, char *);
169106a99fe3SHajimu UMEMOTO 	hints = va_arg(ap, struct addrinfo *);
169206a99fe3SHajimu UMEMOTO 
169306a99fe3SHajimu UMEMOTO 	desired_size = sizeof(res_options) + sizeof(int) + sizeof(int) * 4;
169406a99fe3SHajimu UMEMOTO 	if (hostname != NULL) {
169506a99fe3SHajimu UMEMOTO 		size = strlen(hostname);
169606a99fe3SHajimu UMEMOTO 		desired_size += size + 1;
169706a99fe3SHajimu UMEMOTO 	} else
169806a99fe3SHajimu UMEMOTO 		size = 0;
169906a99fe3SHajimu UMEMOTO 
170006a99fe3SHajimu UMEMOTO 	if (desired_size > *buffer_size) {
170106a99fe3SHajimu UMEMOTO 		*buffer_size = desired_size;
170206a99fe3SHajimu UMEMOTO 		return (NS_RETURN);
170306a99fe3SHajimu UMEMOTO 	}
170406a99fe3SHajimu UMEMOTO 
170506a99fe3SHajimu UMEMOTO 	if (hints == NULL)
170606a99fe3SHajimu UMEMOTO 		ai_flags = ai_family = ai_socktype = ai_protocol = 0;
170706a99fe3SHajimu UMEMOTO 	else {
170806a99fe3SHajimu UMEMOTO 		ai_flags = hints->ai_flags;
170906a99fe3SHajimu UMEMOTO 		ai_family = hints->ai_family;
171006a99fe3SHajimu UMEMOTO 		ai_socktype = hints->ai_socktype;
171106a99fe3SHajimu UMEMOTO 		ai_protocol = hints->ai_protocol;
171206a99fe3SHajimu UMEMOTO 	}
171306a99fe3SHajimu UMEMOTO 
171406a99fe3SHajimu UMEMOTO 	p = buffer;
171506a99fe3SHajimu UMEMOTO 	memcpy(p, &res_options, sizeof(res_options));
171606a99fe3SHajimu UMEMOTO 	p += sizeof(res_options);
171706a99fe3SHajimu UMEMOTO 
171806a99fe3SHajimu UMEMOTO 	memcpy(p, &op_id, sizeof(int));
171906a99fe3SHajimu UMEMOTO 	p += sizeof(int);
172006a99fe3SHajimu UMEMOTO 
172106a99fe3SHajimu UMEMOTO 	memcpy(p, &ai_flags, sizeof(int));
172206a99fe3SHajimu UMEMOTO 	p += sizeof(int);
172306a99fe3SHajimu UMEMOTO 
172406a99fe3SHajimu UMEMOTO 	memcpy(p, &ai_family, sizeof(int));
172506a99fe3SHajimu UMEMOTO 	p += sizeof(int);
172606a99fe3SHajimu UMEMOTO 
172706a99fe3SHajimu UMEMOTO 	memcpy(p, &ai_socktype, sizeof(int));
172806a99fe3SHajimu UMEMOTO 	p += sizeof(int);
172906a99fe3SHajimu UMEMOTO 
173006a99fe3SHajimu UMEMOTO 	memcpy(p, &ai_protocol, sizeof(int));
173106a99fe3SHajimu UMEMOTO 	p += sizeof(int);
173206a99fe3SHajimu UMEMOTO 
173306a99fe3SHajimu UMEMOTO 	if (hostname != NULL)
173406a99fe3SHajimu UMEMOTO 		memcpy(p, hostname, size);
173506a99fe3SHajimu UMEMOTO 
173606a99fe3SHajimu UMEMOTO 	*buffer_size = desired_size;
173706a99fe3SHajimu UMEMOTO 	return (NS_SUCCESS);
173806a99fe3SHajimu UMEMOTO }
173906a99fe3SHajimu UMEMOTO 
174006a99fe3SHajimu UMEMOTO static int
174106a99fe3SHajimu UMEMOTO addrinfo_marshal_func(char *buffer, size_t *buffer_size, void *retval,
174206a99fe3SHajimu UMEMOTO     va_list ap, void *cache_mdata)
174306a99fe3SHajimu UMEMOTO {
174406a99fe3SHajimu UMEMOTO 	struct addrinfo	*ai, *cai;
174506a99fe3SHajimu UMEMOTO 	char *p;
174606a99fe3SHajimu UMEMOTO 	size_t desired_size, size, ai_size;
174706a99fe3SHajimu UMEMOTO 
174806a99fe3SHajimu UMEMOTO 	ai = *((struct addrinfo **)retval);
174906a99fe3SHajimu UMEMOTO 
175006a99fe3SHajimu UMEMOTO 	desired_size = sizeof(size_t);
175106a99fe3SHajimu UMEMOTO 	ai_size = 0;
175206a99fe3SHajimu UMEMOTO 	for (cai = ai; cai != NULL; cai = cai->ai_next) {
175306a99fe3SHajimu UMEMOTO 		desired_size += sizeof(struct addrinfo) + cai->ai_addrlen;
175406a99fe3SHajimu UMEMOTO 		if (cai->ai_canonname != NULL)
175506a99fe3SHajimu UMEMOTO 			desired_size += sizeof(size_t) +
175606a99fe3SHajimu UMEMOTO 			    strlen(cai->ai_canonname);
175706a99fe3SHajimu UMEMOTO 		++ai_size;
175806a99fe3SHajimu UMEMOTO 	}
175906a99fe3SHajimu UMEMOTO 
176006a99fe3SHajimu UMEMOTO 	if (desired_size > *buffer_size) {
176106a99fe3SHajimu UMEMOTO 		/* this assignment is here for future use */
176206a99fe3SHajimu UMEMOTO 		errno = ERANGE;
176306a99fe3SHajimu UMEMOTO 		*buffer_size = desired_size;
176406a99fe3SHajimu UMEMOTO 		return (NS_RETURN);
176506a99fe3SHajimu UMEMOTO 	}
176606a99fe3SHajimu UMEMOTO 
176706a99fe3SHajimu UMEMOTO 	memset(buffer, 0, desired_size);
176806a99fe3SHajimu UMEMOTO 	p = buffer;
176906a99fe3SHajimu UMEMOTO 
177006a99fe3SHajimu UMEMOTO 	memcpy(p, &ai_size, sizeof(size_t));
177106a99fe3SHajimu UMEMOTO 	p += sizeof(size_t);
177206a99fe3SHajimu UMEMOTO 	for (cai = ai; cai != NULL; cai = cai->ai_next) {
177306a99fe3SHajimu UMEMOTO 		memcpy(p, cai, sizeof(struct addrinfo));
177406a99fe3SHajimu UMEMOTO 		p += sizeof(struct addrinfo);
177506a99fe3SHajimu UMEMOTO 
177606a99fe3SHajimu UMEMOTO 		memcpy(p, cai->ai_addr, cai->ai_addrlen);
177706a99fe3SHajimu UMEMOTO 		p += cai->ai_addrlen;
177806a99fe3SHajimu UMEMOTO 
177906a99fe3SHajimu UMEMOTO 		if (cai->ai_canonname != NULL) {
178006a99fe3SHajimu UMEMOTO 			size = strlen(cai->ai_canonname);
178106a99fe3SHajimu UMEMOTO 			memcpy(p, &size, sizeof(size_t));
178206a99fe3SHajimu UMEMOTO 			p += sizeof(size_t);
178306a99fe3SHajimu UMEMOTO 
178406a99fe3SHajimu UMEMOTO 			memcpy(p, cai->ai_canonname, size);
178506a99fe3SHajimu UMEMOTO 			p += size;
178606a99fe3SHajimu UMEMOTO 		}
178706a99fe3SHajimu UMEMOTO 	}
178806a99fe3SHajimu UMEMOTO 
178906a99fe3SHajimu UMEMOTO 	return (NS_SUCCESS);
179006a99fe3SHajimu UMEMOTO }
179106a99fe3SHajimu UMEMOTO 
179206a99fe3SHajimu UMEMOTO static int
179306a99fe3SHajimu UMEMOTO addrinfo_unmarshal_func(char *buffer, size_t buffer_size, void *retval,
179406a99fe3SHajimu UMEMOTO     va_list ap, void *cache_mdata)
179506a99fe3SHajimu UMEMOTO {
179606a99fe3SHajimu UMEMOTO 	struct addrinfo	new_ai, *result, *sentinel, *lasts;
179706a99fe3SHajimu UMEMOTO 
179806a99fe3SHajimu UMEMOTO 	char *p;
179906a99fe3SHajimu UMEMOTO 	size_t ai_size, ai_i, size;
180006a99fe3SHajimu UMEMOTO 
180106a99fe3SHajimu UMEMOTO 	p = buffer;
180206a99fe3SHajimu UMEMOTO 	memcpy(&ai_size, p, sizeof(size_t));
180306a99fe3SHajimu UMEMOTO 	p += sizeof(size_t);
180406a99fe3SHajimu UMEMOTO 
180506a99fe3SHajimu UMEMOTO 	result = NULL;
180606a99fe3SHajimu UMEMOTO 	lasts = NULL;
180706a99fe3SHajimu UMEMOTO 	for (ai_i = 0; ai_i < ai_size; ++ai_i) {
180806a99fe3SHajimu UMEMOTO 		memcpy(&new_ai, p, sizeof(struct addrinfo));
180906a99fe3SHajimu UMEMOTO 		p += sizeof(struct addrinfo);
181006a99fe3SHajimu UMEMOTO 		size = new_ai.ai_addrlen + sizeof(struct addrinfo) +
181106a99fe3SHajimu UMEMOTO 			_ALIGNBYTES;
181206a99fe3SHajimu UMEMOTO 
181306a99fe3SHajimu UMEMOTO 		sentinel = (struct addrinfo *)malloc(size);
181406a99fe3SHajimu UMEMOTO 		memset(sentinel, 0, size);
181506a99fe3SHajimu UMEMOTO 
181606a99fe3SHajimu UMEMOTO 		memcpy(sentinel, &new_ai, sizeof(struct addrinfo));
181706a99fe3SHajimu UMEMOTO 		sentinel->ai_addr = (struct sockaddr *)_ALIGN((char *)sentinel +
181806a99fe3SHajimu UMEMOTO 		    sizeof(struct addrinfo));
181906a99fe3SHajimu UMEMOTO 
182006a99fe3SHajimu UMEMOTO 		memcpy(sentinel->ai_addr, p, new_ai.ai_addrlen);
182106a99fe3SHajimu UMEMOTO 		p += new_ai.ai_addrlen;
182206a99fe3SHajimu UMEMOTO 
182306a99fe3SHajimu UMEMOTO 		if (new_ai.ai_canonname != NULL) {
182406a99fe3SHajimu UMEMOTO 			memcpy(&size, p, sizeof(size_t));
182506a99fe3SHajimu UMEMOTO 			p += sizeof(size_t);
182606a99fe3SHajimu UMEMOTO 
182706a99fe3SHajimu UMEMOTO 			sentinel->ai_canonname = (char *)malloc(size + 1);
182806a99fe3SHajimu UMEMOTO 			memset(sentinel->ai_canonname, 0, size + 1);
182906a99fe3SHajimu UMEMOTO 
183006a99fe3SHajimu UMEMOTO 			memcpy(sentinel->ai_canonname, p, size);
183106a99fe3SHajimu UMEMOTO 			p += size;
183206a99fe3SHajimu UMEMOTO 		}
183306a99fe3SHajimu UMEMOTO 
183406a99fe3SHajimu UMEMOTO 		if (result == NULL) {
183506a99fe3SHajimu UMEMOTO 			result = sentinel;
183606a99fe3SHajimu UMEMOTO 			lasts = sentinel;
183706a99fe3SHajimu UMEMOTO 		} else {
183806a99fe3SHajimu UMEMOTO 			lasts->ai_next = sentinel;
183906a99fe3SHajimu UMEMOTO 			lasts = sentinel;
184006a99fe3SHajimu UMEMOTO 		}
184106a99fe3SHajimu UMEMOTO 	}
184206a99fe3SHajimu UMEMOTO 
184306a99fe3SHajimu UMEMOTO 	*((struct addrinfo **)retval) = result;
184406a99fe3SHajimu UMEMOTO 	return (NS_SUCCESS);
184506a99fe3SHajimu UMEMOTO }
184606a99fe3SHajimu UMEMOTO #endif /* NS_CACHING */
184706a99fe3SHajimu UMEMOTO 
1848688a5c3eSHajimu UMEMOTO /*
1849688a5c3eSHajimu UMEMOTO  * FQDN hostname, DNS lookup
1850688a5c3eSHajimu UMEMOTO  */
1851688a5c3eSHajimu UMEMOTO static int
185290d1d883SHajimu UMEMOTO explore_fqdn(const struct addrinfo *pai, const char *hostname,
185390d1d883SHajimu UMEMOTO     const char *servname, struct addrinfo **res)
1854688a5c3eSHajimu UMEMOTO {
1855688a5c3eSHajimu UMEMOTO 	struct addrinfo *result;
1856688a5c3eSHajimu UMEMOTO 	struct addrinfo *cur;
1857688a5c3eSHajimu UMEMOTO 	int error = 0;
185806a99fe3SHajimu UMEMOTO 
185906a99fe3SHajimu UMEMOTO #ifdef NS_CACHING
186006a99fe3SHajimu UMEMOTO 	static const nss_cache_info cache_info =
186106a99fe3SHajimu UMEMOTO 	NS_COMMON_CACHE_INFO_INITIALIZER(
186206a99fe3SHajimu UMEMOTO 		hosts, NULL, addrinfo_id_func, addrinfo_marshal_func,
186306a99fe3SHajimu UMEMOTO 		addrinfo_unmarshal_func);
186406a99fe3SHajimu UMEMOTO #endif
1865688a5c3eSHajimu UMEMOTO 	static const ns_dtab dtab[] = {
1866688a5c3eSHajimu UMEMOTO 		NS_FILES_CB(_files_getaddrinfo, NULL)
1867688a5c3eSHajimu UMEMOTO 		{ NSSRC_DNS, _dns_getaddrinfo, NULL },	/* force -DHESIOD */
1868688a5c3eSHajimu UMEMOTO 		NS_NIS_CB(_yp_getaddrinfo, NULL)
186906a99fe3SHajimu UMEMOTO #ifdef NS_CACHING
187006a99fe3SHajimu UMEMOTO 		NS_CACHE_CB(&cache_info)
187106a99fe3SHajimu UMEMOTO #endif
1872688a5c3eSHajimu UMEMOTO 		{ 0 }
187369b0a4b6SJim Pirzyk 	};
187469b0a4b6SJim Pirzyk 
1875688a5c3eSHajimu UMEMOTO 	result = NULL;
187669b0a4b6SJim Pirzyk 
1877688a5c3eSHajimu UMEMOTO 	/*
1878688a5c3eSHajimu UMEMOTO 	 * if the servname does not match socktype/protocol, ignore it.
1879688a5c3eSHajimu UMEMOTO 	 */
188033dee819SBrian Feldman 	if (get_portmatch(pai, servname) != 0)
188169b0a4b6SJim Pirzyk 		return 0;
188269b0a4b6SJim Pirzyk 
1883688a5c3eSHajimu UMEMOTO 	switch (_nsdispatch(&result, dtab, NSDB_HOSTS, "getaddrinfo",
1884688a5c3eSHajimu UMEMOTO 			default_dns_files, hostname, pai)) {
1885688a5c3eSHajimu UMEMOTO 	case NS_TRYAGAIN:
1886688a5c3eSHajimu UMEMOTO 		error = EAI_AGAIN;
1887688a5c3eSHajimu UMEMOTO 		goto free;
1888688a5c3eSHajimu UMEMOTO 	case NS_UNAVAIL:
1889688a5c3eSHajimu UMEMOTO 		error = EAI_FAIL;
1890688a5c3eSHajimu UMEMOTO 		goto free;
1891688a5c3eSHajimu UMEMOTO 	case NS_NOTFOUND:
1892688a5c3eSHajimu UMEMOTO 		error = EAI_NONAME;
1893688a5c3eSHajimu UMEMOTO 		goto free;
1894688a5c3eSHajimu UMEMOTO 	case NS_SUCCESS:
1895688a5c3eSHajimu UMEMOTO 		error = 0;
1896688a5c3eSHajimu UMEMOTO 		for (cur = result; cur; cur = cur->ai_next) {
1897688a5c3eSHajimu UMEMOTO 			GET_PORT(cur, servname);
1898688a5c3eSHajimu UMEMOTO 			/* canonname should be filled already */
1899688a5c3eSHajimu UMEMOTO 		}
190069b0a4b6SJim Pirzyk 		break;
190169b0a4b6SJim Pirzyk 	}
190269b0a4b6SJim Pirzyk 
1903688a5c3eSHajimu UMEMOTO 	*res = result;
1904688a5c3eSHajimu UMEMOTO 
190569b0a4b6SJim Pirzyk 	return 0;
1906688a5c3eSHajimu UMEMOTO 
1907688a5c3eSHajimu UMEMOTO free:
1908688a5c3eSHajimu UMEMOTO 	if (result)
1909688a5c3eSHajimu UMEMOTO 		freeaddrinfo(result);
1910688a5c3eSHajimu UMEMOTO 	return error;
191169b0a4b6SJim Pirzyk }
191269b0a4b6SJim Pirzyk 
1913a42af91cSHajimu UMEMOTO #ifdef DEBUG
1914a42af91cSHajimu UMEMOTO static const char AskedForGot[] =
1915a42af91cSHajimu UMEMOTO 	"gethostby*.getanswer: asked for \"%s\", got \"%s\"";
1916a42af91cSHajimu UMEMOTO #endif
1917a42af91cSHajimu UMEMOTO 
1918a42af91cSHajimu UMEMOTO static struct addrinfo *
191990d1d883SHajimu UMEMOTO getanswer(const querybuf *answer, int anslen, const char *qname, int qtype,
192090d1d883SHajimu UMEMOTO     const struct addrinfo *pai, res_state res)
1921a42af91cSHajimu UMEMOTO {
1922a42af91cSHajimu UMEMOTO 	struct addrinfo sentinel, *cur;
1923a42af91cSHajimu UMEMOTO 	struct addrinfo ai;
1924a42af91cSHajimu UMEMOTO 	const struct afd *afd;
1925a42af91cSHajimu UMEMOTO 	char *canonname;
1926a42af91cSHajimu UMEMOTO 	const HEADER *hp;
1927a42af91cSHajimu UMEMOTO 	const u_char *cp;
1928a42af91cSHajimu UMEMOTO 	int n;
1929a42af91cSHajimu UMEMOTO 	const u_char *eom;
1930dd521ef1SHajimu UMEMOTO 	char *bp, *ep;
1931dd521ef1SHajimu UMEMOTO 	int type, class, ancount, qdcount;
1932a42af91cSHajimu UMEMOTO 	int haveanswer, had_error;
1933a42af91cSHajimu UMEMOTO 	char tbuf[MAXDNAME];
1934c05ac53bSDavid E. O'Brien 	int (*name_ok)(const char *);
1935a42af91cSHajimu UMEMOTO 	char hostbuf[8*1024];
1936a42af91cSHajimu UMEMOTO 
1937a42af91cSHajimu UMEMOTO 	memset(&sentinel, 0, sizeof(sentinel));
1938a42af91cSHajimu UMEMOTO 	cur = &sentinel;
1939a42af91cSHajimu UMEMOTO 
1940a42af91cSHajimu UMEMOTO 	canonname = NULL;
1941a42af91cSHajimu UMEMOTO 	eom = answer->buf + anslen;
1942a42af91cSHajimu UMEMOTO 	switch (qtype) {
1943a42af91cSHajimu UMEMOTO 	case T_A:
1944a42af91cSHajimu UMEMOTO 	case T_AAAA:
1945a42af91cSHajimu UMEMOTO 	case T_ANY:	/*use T_ANY only for T_A/T_AAAA lookup*/
1946a42af91cSHajimu UMEMOTO 		name_ok = res_hnok;
1947a42af91cSHajimu UMEMOTO 		break;
1948a42af91cSHajimu UMEMOTO 	default:
1949a42af91cSHajimu UMEMOTO 		return (NULL);	/* XXX should be abort(); */
1950a42af91cSHajimu UMEMOTO 	}
1951a42af91cSHajimu UMEMOTO 	/*
1952a42af91cSHajimu UMEMOTO 	 * find first satisfactory answer
1953a42af91cSHajimu UMEMOTO 	 */
1954a42af91cSHajimu UMEMOTO 	hp = &answer->hdr;
1955a42af91cSHajimu UMEMOTO 	ancount = ntohs(hp->ancount);
1956a42af91cSHajimu UMEMOTO 	qdcount = ntohs(hp->qdcount);
1957a42af91cSHajimu UMEMOTO 	bp = hostbuf;
1958dd521ef1SHajimu UMEMOTO 	ep = hostbuf + sizeof hostbuf;
1959a42af91cSHajimu UMEMOTO 	cp = answer->buf + HFIXEDSZ;
1960a42af91cSHajimu UMEMOTO 	if (qdcount != 1) {
19615342d17fSHajimu UMEMOTO 		RES_SET_H_ERRNO(res, NO_RECOVERY);
1962a42af91cSHajimu UMEMOTO 		return (NULL);
1963a42af91cSHajimu UMEMOTO 	}
1964dd521ef1SHajimu UMEMOTO 	n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1965a42af91cSHajimu UMEMOTO 	if ((n < 0) || !(*name_ok)(bp)) {
19665342d17fSHajimu UMEMOTO 		RES_SET_H_ERRNO(res, NO_RECOVERY);
1967a42af91cSHajimu UMEMOTO 		return (NULL);
1968a42af91cSHajimu UMEMOTO 	}
1969a42af91cSHajimu UMEMOTO 	cp += n + QFIXEDSZ;
1970a42af91cSHajimu UMEMOTO 	if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
1971a42af91cSHajimu UMEMOTO 		/* res_send() has already verified that the query name is the
1972a42af91cSHajimu UMEMOTO 		 * same as the one we sent; this just gets the expanded name
1973a42af91cSHajimu UMEMOTO 		 * (i.e., with the succeeding search-domain tacked on).
1974a42af91cSHajimu UMEMOTO 		 */
1975a42af91cSHajimu UMEMOTO 		n = strlen(bp) + 1;		/* for the \0 */
1976a42af91cSHajimu UMEMOTO 		if (n >= MAXHOSTNAMELEN) {
19775342d17fSHajimu UMEMOTO 			RES_SET_H_ERRNO(res, NO_RECOVERY);
1978a42af91cSHajimu UMEMOTO 			return (NULL);
1979a42af91cSHajimu UMEMOTO 		}
1980a42af91cSHajimu UMEMOTO 		canonname = bp;
1981a42af91cSHajimu UMEMOTO 		bp += n;
1982a42af91cSHajimu UMEMOTO 		/* The qname can be abbreviated, but h_name is now absolute. */
1983a42af91cSHajimu UMEMOTO 		qname = canonname;
1984a42af91cSHajimu UMEMOTO 	}
1985a42af91cSHajimu UMEMOTO 	haveanswer = 0;
1986a42af91cSHajimu UMEMOTO 	had_error = 0;
1987a42af91cSHajimu UMEMOTO 	while (ancount-- > 0 && cp < eom && !had_error) {
1988dd521ef1SHajimu UMEMOTO 		n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1989a42af91cSHajimu UMEMOTO 		if ((n < 0) || !(*name_ok)(bp)) {
1990a42af91cSHajimu UMEMOTO 			had_error++;
1991a42af91cSHajimu UMEMOTO 			continue;
1992a42af91cSHajimu UMEMOTO 		}
1993a42af91cSHajimu UMEMOTO 		cp += n;			/* name */
1994a42af91cSHajimu UMEMOTO 		type = _getshort(cp);
1995a42af91cSHajimu UMEMOTO  		cp += INT16SZ;			/* type */
1996a42af91cSHajimu UMEMOTO 		class = _getshort(cp);
1997a42af91cSHajimu UMEMOTO  		cp += INT16SZ + INT32SZ;	/* class, TTL */
1998a42af91cSHajimu UMEMOTO 		n = _getshort(cp);
1999a42af91cSHajimu UMEMOTO 		cp += INT16SZ;			/* len */
2000a42af91cSHajimu UMEMOTO 		if (class != C_IN) {
2001a42af91cSHajimu UMEMOTO 			/* XXX - debug? syslog? */
2002a42af91cSHajimu UMEMOTO 			cp += n;
2003a42af91cSHajimu UMEMOTO 			continue;		/* XXX - had_error++ ? */
2004a42af91cSHajimu UMEMOTO 		}
2005a42af91cSHajimu UMEMOTO 		if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) &&
2006a42af91cSHajimu UMEMOTO 		    type == T_CNAME) {
2007a42af91cSHajimu UMEMOTO 			n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
2008a42af91cSHajimu UMEMOTO 			if ((n < 0) || !(*name_ok)(tbuf)) {
2009a42af91cSHajimu UMEMOTO 				had_error++;
2010a42af91cSHajimu UMEMOTO 				continue;
2011a42af91cSHajimu UMEMOTO 			}
2012a42af91cSHajimu UMEMOTO 			cp += n;
2013a42af91cSHajimu UMEMOTO 			/* Get canonical name. */
2014a42af91cSHajimu UMEMOTO 			n = strlen(tbuf) + 1;	/* for the \0 */
2015dd521ef1SHajimu UMEMOTO 			if (n > ep - bp || n >= MAXHOSTNAMELEN) {
2016a42af91cSHajimu UMEMOTO 				had_error++;
2017a42af91cSHajimu UMEMOTO 				continue;
2018a42af91cSHajimu UMEMOTO 			}
2019d0509082SJacques Vidrine 			strlcpy(bp, tbuf, ep - bp);
2020a42af91cSHajimu UMEMOTO 			canonname = bp;
2021a42af91cSHajimu UMEMOTO 			bp += n;
2022a42af91cSHajimu UMEMOTO 			continue;
2023a42af91cSHajimu UMEMOTO 		}
2024a42af91cSHajimu UMEMOTO 		if (qtype == T_ANY) {
2025a42af91cSHajimu UMEMOTO 			if (!(type == T_A || type == T_AAAA)) {
2026a42af91cSHajimu UMEMOTO 				cp += n;
2027a42af91cSHajimu UMEMOTO 				continue;
2028a42af91cSHajimu UMEMOTO 			}
2029a42af91cSHajimu UMEMOTO 		} else if (type != qtype) {
2030a42af91cSHajimu UMEMOTO #ifdef DEBUG
2031aba831deSHajimu UMEMOTO 			if (type != T_KEY && type != T_SIG &&
2032aba831deSHajimu UMEMOTO 			    type != ns_t_dname)
2033a42af91cSHajimu UMEMOTO 				syslog(LOG_NOTICE|LOG_AUTH,
2034a42af91cSHajimu UMEMOTO 	       "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
2035a42af91cSHajimu UMEMOTO 				       qname, p_class(C_IN), p_type(qtype),
2036a42af91cSHajimu UMEMOTO 				       p_type(type));
2037a42af91cSHajimu UMEMOTO #endif
2038a42af91cSHajimu UMEMOTO 			cp += n;
2039a42af91cSHajimu UMEMOTO 			continue;		/* XXX - had_error++ ? */
2040a42af91cSHajimu UMEMOTO 		}
2041a42af91cSHajimu UMEMOTO 		switch (type) {
2042a42af91cSHajimu UMEMOTO 		case T_A:
2043a42af91cSHajimu UMEMOTO 		case T_AAAA:
2044a42af91cSHajimu UMEMOTO 			if (strcasecmp(canonname, bp) != 0) {
2045a42af91cSHajimu UMEMOTO #ifdef DEBUG
2046a42af91cSHajimu UMEMOTO 				syslog(LOG_NOTICE|LOG_AUTH,
2047a42af91cSHajimu UMEMOTO 				       AskedForGot, canonname, bp);
2048a42af91cSHajimu UMEMOTO #endif
2049a42af91cSHajimu UMEMOTO 				cp += n;
2050a42af91cSHajimu UMEMOTO 				continue;	/* XXX - had_error++ ? */
2051a42af91cSHajimu UMEMOTO 			}
2052a42af91cSHajimu UMEMOTO 			if (type == T_A && n != INADDRSZ) {
2053a42af91cSHajimu UMEMOTO 				cp += n;
2054a42af91cSHajimu UMEMOTO 				continue;
2055a42af91cSHajimu UMEMOTO 			}
2056a42af91cSHajimu UMEMOTO 			if (type == T_AAAA && n != IN6ADDRSZ) {
2057a42af91cSHajimu UMEMOTO 				cp += n;
2058a42af91cSHajimu UMEMOTO 				continue;
2059a42af91cSHajimu UMEMOTO 			}
2060a42af91cSHajimu UMEMOTO #ifdef FILTER_V4MAPPED
2061a42af91cSHajimu UMEMOTO 			if (type == T_AAAA) {
2062a42af91cSHajimu UMEMOTO 				struct in6_addr in6;
2063a42af91cSHajimu UMEMOTO 				memcpy(&in6, cp, sizeof(in6));
2064a42af91cSHajimu UMEMOTO 				if (IN6_IS_ADDR_V4MAPPED(&in6)) {
2065a42af91cSHajimu UMEMOTO 					cp += n;
2066a42af91cSHajimu UMEMOTO 					continue;
2067a42af91cSHajimu UMEMOTO 				}
2068a42af91cSHajimu UMEMOTO 			}
2069a42af91cSHajimu UMEMOTO #endif
2070a42af91cSHajimu UMEMOTO 			if (!haveanswer) {
2071a42af91cSHajimu UMEMOTO 				int nn;
2072a42af91cSHajimu UMEMOTO 
2073a42af91cSHajimu UMEMOTO 				canonname = bp;
2074a42af91cSHajimu UMEMOTO 				nn = strlen(bp) + 1;	/* for the \0 */
2075a42af91cSHajimu UMEMOTO 				bp += nn;
2076a42af91cSHajimu UMEMOTO 			}
2077a42af91cSHajimu UMEMOTO 
2078a42af91cSHajimu UMEMOTO 			/* don't overwrite pai */
2079a42af91cSHajimu UMEMOTO 			ai = *pai;
2080a42af91cSHajimu UMEMOTO 			ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
2081a42af91cSHajimu UMEMOTO 			afd = find_afd(ai.ai_family);
2082a42af91cSHajimu UMEMOTO 			if (afd == NULL) {
2083a42af91cSHajimu UMEMOTO 				cp += n;
2084a42af91cSHajimu UMEMOTO 				continue;
2085a42af91cSHajimu UMEMOTO 			}
2086a42af91cSHajimu UMEMOTO 			cur->ai_next = get_ai(&ai, afd, (const char *)cp);
2087a42af91cSHajimu UMEMOTO 			if (cur->ai_next == NULL)
2088a42af91cSHajimu UMEMOTO 				had_error++;
2089a42af91cSHajimu UMEMOTO 			while (cur && cur->ai_next)
2090a42af91cSHajimu UMEMOTO 				cur = cur->ai_next;
2091a42af91cSHajimu UMEMOTO 			cp += n;
2092a42af91cSHajimu UMEMOTO 			break;
2093a42af91cSHajimu UMEMOTO 		default:
2094a42af91cSHajimu UMEMOTO 			abort();
2095a42af91cSHajimu UMEMOTO 		}
2096a42af91cSHajimu UMEMOTO 		if (!had_error)
2097a42af91cSHajimu UMEMOTO 			haveanswer++;
2098a42af91cSHajimu UMEMOTO 	}
2099a42af91cSHajimu UMEMOTO 	if (haveanswer) {
210069b0a4b6SJim Pirzyk #if defined(RESOLVSORT)
210169b0a4b6SJim Pirzyk 		/*
210269b0a4b6SJim Pirzyk 		 * We support only IPv4 address for backward
210369b0a4b6SJim Pirzyk 		 * compatibility against gethostbyname(3).
210469b0a4b6SJim Pirzyk 		 */
21055342d17fSHajimu UMEMOTO 		if (res->nsort && qtype == T_A) {
21065342d17fSHajimu UMEMOTO 			if (addr4sort(&sentinel, res) < 0) {
210769b0a4b6SJim Pirzyk 				freeaddrinfo(sentinel.ai_next);
21085342d17fSHajimu UMEMOTO 				RES_SET_H_ERRNO(res, NO_RECOVERY);
210969b0a4b6SJim Pirzyk 				return NULL;
211069b0a4b6SJim Pirzyk 			}
211169b0a4b6SJim Pirzyk 		}
211269b0a4b6SJim Pirzyk #endif /*RESOLVSORT*/
2113a42af91cSHajimu UMEMOTO 		if (!canonname)
2114a42af91cSHajimu UMEMOTO 			(void)get_canonname(pai, sentinel.ai_next, qname);
2115a42af91cSHajimu UMEMOTO 		else
2116a42af91cSHajimu UMEMOTO 			(void)get_canonname(pai, sentinel.ai_next, canonname);
21175342d17fSHajimu UMEMOTO 		RES_SET_H_ERRNO(res, NETDB_SUCCESS);
2118a42af91cSHajimu UMEMOTO 		return sentinel.ai_next;
2119a42af91cSHajimu UMEMOTO 	}
2120a42af91cSHajimu UMEMOTO 
21215342d17fSHajimu UMEMOTO 	RES_SET_H_ERRNO(res, NO_RECOVERY);
2122a42af91cSHajimu UMEMOTO 	return NULL;
2123a42af91cSHajimu UMEMOTO }
2124a42af91cSHajimu UMEMOTO 
2125688a5c3eSHajimu UMEMOTO #ifdef RESOLVSORT
2126688a5c3eSHajimu UMEMOTO struct addr_ptr {
2127688a5c3eSHajimu UMEMOTO 	struct addrinfo *ai;
2128688a5c3eSHajimu UMEMOTO 	int aval;
2129688a5c3eSHajimu UMEMOTO };
2130688a5c3eSHajimu UMEMOTO 
2131688a5c3eSHajimu UMEMOTO static int
21325342d17fSHajimu UMEMOTO addr4sort(struct addrinfo *sentinel, res_state res)
2133688a5c3eSHajimu UMEMOTO {
2134688a5c3eSHajimu UMEMOTO 	struct addrinfo *ai;
2135688a5c3eSHajimu UMEMOTO 	struct addr_ptr *addrs, addr;
2136688a5c3eSHajimu UMEMOTO 	struct sockaddr_in *sin;
2137688a5c3eSHajimu UMEMOTO 	int naddrs, i, j;
2138688a5c3eSHajimu UMEMOTO 	int needsort = 0;
2139688a5c3eSHajimu UMEMOTO 
2140688a5c3eSHajimu UMEMOTO 	if (!sentinel)
2141688a5c3eSHajimu UMEMOTO 		return -1;
2142688a5c3eSHajimu UMEMOTO 	naddrs = 0;
2143688a5c3eSHajimu UMEMOTO 	for (ai = sentinel->ai_next; ai; ai = ai->ai_next)
2144688a5c3eSHajimu UMEMOTO 		naddrs++;
2145688a5c3eSHajimu UMEMOTO 	if (naddrs < 2)
2146688a5c3eSHajimu UMEMOTO 		return 0;		/* We don't need sorting. */
2147688a5c3eSHajimu UMEMOTO 	if ((addrs = malloc(sizeof(struct addr_ptr) * naddrs)) == NULL)
2148688a5c3eSHajimu UMEMOTO 		return -1;
2149688a5c3eSHajimu UMEMOTO 	i = 0;
2150688a5c3eSHajimu UMEMOTO 	for (ai = sentinel->ai_next; ai; ai = ai->ai_next) {
2151688a5c3eSHajimu UMEMOTO 		sin = (struct sockaddr_in *)ai->ai_addr;
21525342d17fSHajimu UMEMOTO 		for (j = 0; (unsigned)j < res->nsort; j++) {
21535342d17fSHajimu UMEMOTO 			if (res->sort_list[j].addr.s_addr ==
21545342d17fSHajimu UMEMOTO 			    (sin->sin_addr.s_addr & res->sort_list[j].mask))
2155688a5c3eSHajimu UMEMOTO 				break;
2156688a5c3eSHajimu UMEMOTO 		}
2157688a5c3eSHajimu UMEMOTO 		addrs[i].ai = ai;
2158688a5c3eSHajimu UMEMOTO 		addrs[i].aval = j;
2159688a5c3eSHajimu UMEMOTO 		if (needsort == 0 && i > 0 && j < addrs[i - 1].aval)
2160688a5c3eSHajimu UMEMOTO 			needsort = i;
2161688a5c3eSHajimu UMEMOTO 		i++;
2162688a5c3eSHajimu UMEMOTO 	}
2163688a5c3eSHajimu UMEMOTO 	if (!needsort) {
2164688a5c3eSHajimu UMEMOTO 		free(addrs);
2165688a5c3eSHajimu UMEMOTO 		return 0;
2166688a5c3eSHajimu UMEMOTO 	}
2167688a5c3eSHajimu UMEMOTO 
2168688a5c3eSHajimu UMEMOTO 	while (needsort < naddrs) {
2169688a5c3eSHajimu UMEMOTO 		for (j = needsort - 1; j >= 0; j--) {
2170688a5c3eSHajimu UMEMOTO 			if (addrs[j].aval > addrs[j+1].aval) {
2171688a5c3eSHajimu UMEMOTO 				addr = addrs[j];
2172688a5c3eSHajimu UMEMOTO 				addrs[j] = addrs[j + 1];
2173688a5c3eSHajimu UMEMOTO 				addrs[j + 1] = addr;
2174688a5c3eSHajimu UMEMOTO 			} else
2175688a5c3eSHajimu UMEMOTO 				break;
2176688a5c3eSHajimu UMEMOTO 		}
2177688a5c3eSHajimu UMEMOTO 		needsort++;
2178688a5c3eSHajimu UMEMOTO 	}
2179688a5c3eSHajimu UMEMOTO 
2180688a5c3eSHajimu UMEMOTO 	ai = sentinel;
2181688a5c3eSHajimu UMEMOTO 	for (i = 0; i < naddrs; ++i) {
2182688a5c3eSHajimu UMEMOTO 		ai->ai_next = addrs[i].ai;
2183688a5c3eSHajimu UMEMOTO 		ai = ai->ai_next;
2184688a5c3eSHajimu UMEMOTO 	}
2185688a5c3eSHajimu UMEMOTO 	ai->ai_next = NULL;
2186688a5c3eSHajimu UMEMOTO 	free(addrs);
2187688a5c3eSHajimu UMEMOTO 	return 0;
2188688a5c3eSHajimu UMEMOTO }
2189688a5c3eSHajimu UMEMOTO #endif /*RESOLVSORT*/
2190688a5c3eSHajimu UMEMOTO 
2191a42af91cSHajimu UMEMOTO /*ARGSUSED*/
2192a42af91cSHajimu UMEMOTO static int
219390d1d883SHajimu UMEMOTO _dns_getaddrinfo(void *rv, void *cb_data, va_list ap)
2194a42af91cSHajimu UMEMOTO {
2195a42af91cSHajimu UMEMOTO 	struct addrinfo *ai;
21965cd588cbSHajimu UMEMOTO 	querybuf *buf, *buf2;
2197a214cd1aSHajimu UMEMOTO 	const char *hostname;
2198248aee62SJacques Vidrine 	const struct addrinfo *pai;
2199a42af91cSHajimu UMEMOTO 	struct addrinfo sentinel, *cur;
2200a42af91cSHajimu UMEMOTO 	struct res_target q, q2;
22015342d17fSHajimu UMEMOTO 	res_state res;
2202a42af91cSHajimu UMEMOTO 
2203a214cd1aSHajimu UMEMOTO 	hostname = va_arg(ap, char *);
2204248aee62SJacques Vidrine 	pai = va_arg(ap, const struct addrinfo *);
2205248aee62SJacques Vidrine 
2206f1fa5b4fSXin LI 	memset(&q, 0, sizeof(q));
2207a42af91cSHajimu UMEMOTO 	memset(&q2, 0, sizeof(q2));
2208a42af91cSHajimu UMEMOTO 	memset(&sentinel, 0, sizeof(sentinel));
2209a42af91cSHajimu UMEMOTO 	cur = &sentinel;
2210a42af91cSHajimu UMEMOTO 
22114eb17fd7SHajimu UMEMOTO 	res = __res_state();
22124eb17fd7SHajimu UMEMOTO 
22135cd588cbSHajimu UMEMOTO 	buf = malloc(sizeof(*buf));
22145cd588cbSHajimu UMEMOTO 	if (!buf) {
22155342d17fSHajimu UMEMOTO 		RES_SET_H_ERRNO(res, NETDB_INTERNAL);
22165cd588cbSHajimu UMEMOTO 		return NS_NOTFOUND;
22175cd588cbSHajimu UMEMOTO 	}
22185cd588cbSHajimu UMEMOTO 	buf2 = malloc(sizeof(*buf2));
22195cd588cbSHajimu UMEMOTO 	if (!buf2) {
22205cd588cbSHajimu UMEMOTO 		free(buf);
22215342d17fSHajimu UMEMOTO 		RES_SET_H_ERRNO(res, NETDB_INTERNAL);
22225cd588cbSHajimu UMEMOTO 		return NS_NOTFOUND;
22235cd588cbSHajimu UMEMOTO 	}
22245cd588cbSHajimu UMEMOTO 
2225a42af91cSHajimu UMEMOTO 	switch (pai->ai_family) {
2226a42af91cSHajimu UMEMOTO 	case AF_UNSPEC:
2227a214cd1aSHajimu UMEMOTO 		q.name = hostname;
2228b826397aSJun-ichiro itojun Hagino 		q.qclass = C_IN;
2229d0d6a2c7SHajimu UMEMOTO 		q.qtype = T_A;
22305cd588cbSHajimu UMEMOTO 		q.answer = buf->buf;
22315cd588cbSHajimu UMEMOTO 		q.anslen = sizeof(buf->buf);
2232a42af91cSHajimu UMEMOTO 		q.next = &q2;
2233a214cd1aSHajimu UMEMOTO 		q2.name = hostname;
2234b826397aSJun-ichiro itojun Hagino 		q2.qclass = C_IN;
2235d0d6a2c7SHajimu UMEMOTO 		q2.qtype = T_AAAA;
22365cd588cbSHajimu UMEMOTO 		q2.answer = buf2->buf;
22375cd588cbSHajimu UMEMOTO 		q2.anslen = sizeof(buf2->buf);
2238a42af91cSHajimu UMEMOTO 		break;
2239a42af91cSHajimu UMEMOTO 	case AF_INET:
2240a214cd1aSHajimu UMEMOTO 		q.name = hostname;
2241b826397aSJun-ichiro itojun Hagino 		q.qclass = C_IN;
2242b826397aSJun-ichiro itojun Hagino 		q.qtype = T_A;
22435cd588cbSHajimu UMEMOTO 		q.answer = buf->buf;
22445cd588cbSHajimu UMEMOTO 		q.anslen = sizeof(buf->buf);
2245a42af91cSHajimu UMEMOTO 		break;
2246a42af91cSHajimu UMEMOTO 	case AF_INET6:
2247a214cd1aSHajimu UMEMOTO 		q.name = hostname;
2248b826397aSJun-ichiro itojun Hagino 		q.qclass = C_IN;
2249b826397aSJun-ichiro itojun Hagino 		q.qtype = T_AAAA;
22505cd588cbSHajimu UMEMOTO 		q.answer = buf->buf;
22515cd588cbSHajimu UMEMOTO 		q.anslen = sizeof(buf->buf);
2252a42af91cSHajimu UMEMOTO 		break;
2253a42af91cSHajimu UMEMOTO 	default:
22545cd588cbSHajimu UMEMOTO 		free(buf);
22555cd588cbSHajimu UMEMOTO 		free(buf2);
2256248aee62SJacques Vidrine 		return NS_UNAVAIL;
2257a42af91cSHajimu UMEMOTO 	}
22585342d17fSHajimu UMEMOTO 
22595342d17fSHajimu UMEMOTO 	if ((res->options & RES_INIT) == 0 && res_ninit(res) == -1) {
22605342d17fSHajimu UMEMOTO 		RES_SET_H_ERRNO(res, NETDB_INTERNAL);
22615342d17fSHajimu UMEMOTO 		free(buf);
22625342d17fSHajimu UMEMOTO 		free(buf2);
22635342d17fSHajimu UMEMOTO 		return NS_NOTFOUND;
22645342d17fSHajimu UMEMOTO 	}
22655342d17fSHajimu UMEMOTO 
22665342d17fSHajimu UMEMOTO 	if (res_searchN(hostname, &q, res) < 0) {
22675cd588cbSHajimu UMEMOTO 		free(buf);
22685cd588cbSHajimu UMEMOTO 		free(buf2);
2269248aee62SJacques Vidrine 		return NS_NOTFOUND;
22705cd588cbSHajimu UMEMOTO 	}
2271d0d6a2c7SHajimu UMEMOTO 	/* prefer IPv6 */
2272d0d6a2c7SHajimu UMEMOTO 	if (q.next) {
22735342d17fSHajimu UMEMOTO 		ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai, res);
2274a42af91cSHajimu UMEMOTO 		if (ai) {
2275a42af91cSHajimu UMEMOTO 			cur->ai_next = ai;
2276a42af91cSHajimu UMEMOTO 			while (cur && cur->ai_next)
2277a42af91cSHajimu UMEMOTO 				cur = cur->ai_next;
2278a42af91cSHajimu UMEMOTO 		}
2279d0d6a2c7SHajimu UMEMOTO 	}
22805342d17fSHajimu UMEMOTO 	ai = getanswer(buf, q.n, q.name, q.qtype, pai, res);
2281a42af91cSHajimu UMEMOTO 	if (ai)
2282a42af91cSHajimu UMEMOTO 		cur->ai_next = ai;
22835cd588cbSHajimu UMEMOTO 	free(buf);
22845cd588cbSHajimu UMEMOTO 	free(buf2);
2285a42af91cSHajimu UMEMOTO 	if (sentinel.ai_next == NULL)
22865342d17fSHajimu UMEMOTO 		switch (res->res_h_errno) {
2287a42af91cSHajimu UMEMOTO 		case HOST_NOT_FOUND:
2288248aee62SJacques Vidrine 			return NS_NOTFOUND;
2289a42af91cSHajimu UMEMOTO 		case TRY_AGAIN:
2290248aee62SJacques Vidrine 			return NS_TRYAGAIN;
2291a42af91cSHajimu UMEMOTO 		default:
2292248aee62SJacques Vidrine 			return NS_UNAVAIL;
2293a42af91cSHajimu UMEMOTO 		}
2294248aee62SJacques Vidrine 	*((struct addrinfo **)rv) = sentinel.ai_next;
2295248aee62SJacques Vidrine 	return NS_SUCCESS;
2296248aee62SJacques Vidrine }
2297248aee62SJacques Vidrine 
2298248aee62SJacques Vidrine static void
22999def31dcSHajimu UMEMOTO _sethtent(FILE **hostf)
2300248aee62SJacques Vidrine {
23019def31dcSHajimu UMEMOTO 	if (!*hostf)
2302a93705b0SJilles Tjoelker 		*hostf = fopen(_PATH_HOSTS, "re");
2303248aee62SJacques Vidrine 	else
23049def31dcSHajimu UMEMOTO 		rewind(*hostf);
2305248aee62SJacques Vidrine }
2306248aee62SJacques Vidrine 
2307248aee62SJacques Vidrine static void
23089def31dcSHajimu UMEMOTO _endhtent(FILE **hostf)
2309248aee62SJacques Vidrine {
23109def31dcSHajimu UMEMOTO 	if (*hostf) {
23119def31dcSHajimu UMEMOTO 		(void) fclose(*hostf);
23129def31dcSHajimu UMEMOTO 		*hostf = NULL;
2313248aee62SJacques Vidrine 	}
2314a42af91cSHajimu UMEMOTO }
2315a42af91cSHajimu UMEMOTO 
2316a42af91cSHajimu UMEMOTO static struct addrinfo *
23179def31dcSHajimu UMEMOTO _gethtent(FILE **hostf, const char *name, const struct addrinfo *pai)
2318a42af91cSHajimu UMEMOTO {
2319a42af91cSHajimu UMEMOTO 	char *p;
2320a42af91cSHajimu UMEMOTO 	char *cp, *tname, *cname;
2321a42af91cSHajimu UMEMOTO 	struct addrinfo hints, *res0, *res;
2322a42af91cSHajimu UMEMOTO 	int error;
2323a42af91cSHajimu UMEMOTO 	const char *addr;
2324a42af91cSHajimu UMEMOTO 	char hostbuf[8*1024];
2325a42af91cSHajimu UMEMOTO 
2326a93705b0SJilles Tjoelker 	if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re")))
2327248aee62SJacques Vidrine 		return (NULL);
2328a42af91cSHajimu UMEMOTO again:
23299def31dcSHajimu UMEMOTO 	if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf)))
2330a42af91cSHajimu UMEMOTO 		return (NULL);
2331a42af91cSHajimu UMEMOTO 	if (*p == '#')
2332a42af91cSHajimu UMEMOTO 		goto again;
23337b6cc404SMaxim Sobolev 	cp = strpbrk(p, "#\n");
23347b6cc404SMaxim Sobolev 	if (cp != NULL)
2335a42af91cSHajimu UMEMOTO 		*cp = '\0';
2336a42af91cSHajimu UMEMOTO 	if (!(cp = strpbrk(p, " \t")))
2337a42af91cSHajimu UMEMOTO 		goto again;
2338a42af91cSHajimu UMEMOTO 	*cp++ = '\0';
2339a42af91cSHajimu UMEMOTO 	addr = p;
2340a42af91cSHajimu UMEMOTO 	cname = NULL;
2341a42af91cSHajimu UMEMOTO 	/* if this is not something we're looking for, skip it. */
2342a42af91cSHajimu UMEMOTO 	while (cp && *cp) {
2343a42af91cSHajimu UMEMOTO 		if (*cp == ' ' || *cp == '\t') {
2344a42af91cSHajimu UMEMOTO 			cp++;
2345a42af91cSHajimu UMEMOTO 			continue;
2346a42af91cSHajimu UMEMOTO 		}
2347a42af91cSHajimu UMEMOTO 		tname = cp;
2348a42af91cSHajimu UMEMOTO 		if (cname == NULL)
2349a42af91cSHajimu UMEMOTO 			cname = cp;
2350a42af91cSHajimu UMEMOTO 		if ((cp = strpbrk(cp, " \t")) != NULL)
2351a42af91cSHajimu UMEMOTO 			*cp++ = '\0';
2352a42af91cSHajimu UMEMOTO 		if (strcasecmp(name, tname) == 0)
2353a42af91cSHajimu UMEMOTO 			goto found;
2354a42af91cSHajimu UMEMOTO 	}
2355a42af91cSHajimu UMEMOTO 	goto again;
2356a42af91cSHajimu UMEMOTO 
2357a42af91cSHajimu UMEMOTO found:
2358dd521ef1SHajimu UMEMOTO 	/* we should not glob socktype/protocol here */
2359dd521ef1SHajimu UMEMOTO 	memset(&hints, 0, sizeof(hints));
2360dd521ef1SHajimu UMEMOTO 	hints.ai_family = pai->ai_family;
2361dd521ef1SHajimu UMEMOTO 	hints.ai_socktype = SOCK_DGRAM;
2362dd521ef1SHajimu UMEMOTO 	hints.ai_protocol = 0;
2363a42af91cSHajimu UMEMOTO 	hints.ai_flags = AI_NUMERICHOST;
2364dd521ef1SHajimu UMEMOTO 	error = getaddrinfo(addr, "0", &hints, &res0);
2365a42af91cSHajimu UMEMOTO 	if (error)
2366a42af91cSHajimu UMEMOTO 		goto again;
2367a42af91cSHajimu UMEMOTO #ifdef FILTER_V4MAPPED
2368a42af91cSHajimu UMEMOTO 	/* XXX should check all items in the chain */
2369a42af91cSHajimu UMEMOTO 	if (res0->ai_family == AF_INET6 &&
2370a42af91cSHajimu UMEMOTO 	    IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)res0->ai_addr)->sin6_addr)) {
2371a42af91cSHajimu UMEMOTO 		freeaddrinfo(res0);
2372a42af91cSHajimu UMEMOTO 		goto again;
2373a42af91cSHajimu UMEMOTO 	}
2374a42af91cSHajimu UMEMOTO #endif
2375a42af91cSHajimu UMEMOTO 	for (res = res0; res; res = res->ai_next) {
2376a42af91cSHajimu UMEMOTO 		/* cover it up */
2377a42af91cSHajimu UMEMOTO 		res->ai_flags = pai->ai_flags;
2378dd521ef1SHajimu UMEMOTO 		res->ai_socktype = pai->ai_socktype;
2379dd521ef1SHajimu UMEMOTO 		res->ai_protocol = pai->ai_protocol;
2380a42af91cSHajimu UMEMOTO 
2381a42af91cSHajimu UMEMOTO 		if (pai->ai_flags & AI_CANONNAME) {
2382a42af91cSHajimu UMEMOTO 			if (get_canonname(pai, res, cname) != 0) {
2383a42af91cSHajimu UMEMOTO 				freeaddrinfo(res0);
2384a42af91cSHajimu UMEMOTO 				goto again;
2385a42af91cSHajimu UMEMOTO 			}
2386a42af91cSHajimu UMEMOTO 		}
2387a42af91cSHajimu UMEMOTO 	}
2388a42af91cSHajimu UMEMOTO 	return res0;
2389a42af91cSHajimu UMEMOTO }
2390a42af91cSHajimu UMEMOTO 
2391a42af91cSHajimu UMEMOTO /*ARGSUSED*/
2392a42af91cSHajimu UMEMOTO static int
239390d1d883SHajimu UMEMOTO _files_getaddrinfo(void *rv, void *cb_data, va_list ap)
2394a42af91cSHajimu UMEMOTO {
2395248aee62SJacques Vidrine 	const char *name;
2396248aee62SJacques Vidrine 	const struct addrinfo *pai;
2397a42af91cSHajimu UMEMOTO 	struct addrinfo sentinel, *cur;
2398a42af91cSHajimu UMEMOTO 	struct addrinfo *p;
23999def31dcSHajimu UMEMOTO 	FILE *hostf = NULL;
2400a42af91cSHajimu UMEMOTO 
2401248aee62SJacques Vidrine 	name = va_arg(ap, char *);
2402248aee62SJacques Vidrine 	pai = va_arg(ap, struct addrinfo *);
2403248aee62SJacques Vidrine 
2404248aee62SJacques Vidrine 	memset(&sentinel, 0, sizeof(sentinel));
2405a42af91cSHajimu UMEMOTO 	cur = &sentinel;
2406a42af91cSHajimu UMEMOTO 
24079def31dcSHajimu UMEMOTO 	_sethtent(&hostf);
24089def31dcSHajimu UMEMOTO 	while ((p = _gethtent(&hostf, name, pai)) != NULL) {
2409a42af91cSHajimu UMEMOTO 		cur->ai_next = p;
2410a42af91cSHajimu UMEMOTO 		while (cur && cur->ai_next)
2411a42af91cSHajimu UMEMOTO 			cur = cur->ai_next;
2412a42af91cSHajimu UMEMOTO 	}
24139def31dcSHajimu UMEMOTO 	_endhtent(&hostf);
2414a42af91cSHajimu UMEMOTO 
2415248aee62SJacques Vidrine 	*((struct addrinfo **)rv) = sentinel.ai_next;
2416248aee62SJacques Vidrine 	if (sentinel.ai_next == NULL)
2417248aee62SJacques Vidrine 		return NS_NOTFOUND;
2418248aee62SJacques Vidrine 	return NS_SUCCESS;
2419a42af91cSHajimu UMEMOTO }
2420a42af91cSHajimu UMEMOTO 
2421a42af91cSHajimu UMEMOTO #ifdef YP
2422248aee62SJacques Vidrine /*ARGSUSED*/
2423248aee62SJacques Vidrine static struct addrinfo *
242490d1d883SHajimu UMEMOTO _yphostent(char *line, const struct addrinfo *pai)
2425248aee62SJacques Vidrine {
2426248aee62SJacques Vidrine 	struct addrinfo sentinel, *cur;
2427248aee62SJacques Vidrine 	struct addrinfo hints, *res, *res0;
2428248aee62SJacques Vidrine 	int error;
2429248aee62SJacques Vidrine 	char *p = line;
2430248aee62SJacques Vidrine 	const char *addr, *canonname;
2431248aee62SJacques Vidrine 	char *nextline;
2432248aee62SJacques Vidrine 	char *cp;
2433248aee62SJacques Vidrine 
2434248aee62SJacques Vidrine 	addr = canonname = NULL;
2435248aee62SJacques Vidrine 
2436248aee62SJacques Vidrine 	memset(&sentinel, 0, sizeof(sentinel));
2437a42af91cSHajimu UMEMOTO 	cur = &sentinel;
2438a42af91cSHajimu UMEMOTO 
2439248aee62SJacques Vidrine nextline:
2440248aee62SJacques Vidrine 	/* terminate line */
2441248aee62SJacques Vidrine 	cp = strchr(p, '\n');
2442248aee62SJacques Vidrine 	if (cp) {
2443248aee62SJacques Vidrine 		*cp++ = '\0';
2444248aee62SJacques Vidrine 		nextline = cp;
2445248aee62SJacques Vidrine 	} else
2446248aee62SJacques Vidrine 		nextline = NULL;
2447a42af91cSHajimu UMEMOTO 
2448248aee62SJacques Vidrine 	cp = strpbrk(p, " \t");
2449248aee62SJacques Vidrine 	if (cp == NULL) {
2450248aee62SJacques Vidrine 		if (canonname == NULL)
2451248aee62SJacques Vidrine 			return (NULL);
2452248aee62SJacques Vidrine 		else
2453248aee62SJacques Vidrine 			goto done;
2454a42af91cSHajimu UMEMOTO 	}
2455248aee62SJacques Vidrine 	*cp++ = '\0';
2456a42af91cSHajimu UMEMOTO 
2457248aee62SJacques Vidrine 	addr = p;
2458a42af91cSHajimu UMEMOTO 
2459248aee62SJacques Vidrine 	while (cp && *cp) {
2460248aee62SJacques Vidrine 		if (*cp == ' ' || *cp == '\t') {
2461248aee62SJacques Vidrine 			cp++;
2462a42af91cSHajimu UMEMOTO 			continue;
2463248aee62SJacques Vidrine 		}
2464248aee62SJacques Vidrine 		if (!canonname)
2465248aee62SJacques Vidrine 			canonname = cp;
2466248aee62SJacques Vidrine 		if ((cp = strpbrk(cp, " \t")) != NULL)
2467248aee62SJacques Vidrine 			*cp++ = '\0';
2468a42af91cSHajimu UMEMOTO 	}
2469a42af91cSHajimu UMEMOTO 
2470bf838688SHajimu UMEMOTO 	hints = *pai;
2471248aee62SJacques Vidrine 	hints.ai_flags = AI_NUMERICHOST;
2472bf838688SHajimu UMEMOTO 	error = getaddrinfo(addr, NULL, &hints, &res0);
2473248aee62SJacques Vidrine 	if (error == 0) {
2474248aee62SJacques Vidrine 		for (res = res0; res; res = res->ai_next) {
2475248aee62SJacques Vidrine 			/* cover it up */
2476248aee62SJacques Vidrine 			res->ai_flags = pai->ai_flags;
2477248aee62SJacques Vidrine 
2478248aee62SJacques Vidrine 			if (pai->ai_flags & AI_CANONNAME)
2479248aee62SJacques Vidrine 				(void)get_canonname(pai, res, canonname);
2480248aee62SJacques Vidrine 		}
2481248aee62SJacques Vidrine 	} else
2482248aee62SJacques Vidrine 		res0 = NULL;
2483248aee62SJacques Vidrine 	if (res0) {
2484248aee62SJacques Vidrine 		cur->ai_next = res0;
2485a42af91cSHajimu UMEMOTO 		while (cur && cur->ai_next)
2486a42af91cSHajimu UMEMOTO 			cur = cur->ai_next;
2487a42af91cSHajimu UMEMOTO 	}
2488a42af91cSHajimu UMEMOTO 
2489248aee62SJacques Vidrine 	if (nextline) {
2490248aee62SJacques Vidrine 		p = nextline;
2491248aee62SJacques Vidrine 		goto nextline;
2492248aee62SJacques Vidrine 	}
2493a42af91cSHajimu UMEMOTO 
2494248aee62SJacques Vidrine done:
2495248aee62SJacques Vidrine 	return sentinel.ai_next;
2496248aee62SJacques Vidrine }
2497248aee62SJacques Vidrine 
2498248aee62SJacques Vidrine /*ARGSUSED*/
2499248aee62SJacques Vidrine static int
250090d1d883SHajimu UMEMOTO _yp_getaddrinfo(void *rv, void *cb_data, va_list ap)
2501248aee62SJacques Vidrine {
2502248aee62SJacques Vidrine 	struct addrinfo sentinel, *cur;
2503248aee62SJacques Vidrine 	struct addrinfo *ai = NULL;
2504d7057edbSHajimu UMEMOTO 	char *ypbuf;
2505d7057edbSHajimu UMEMOTO 	int ypbuflen, r;
2506248aee62SJacques Vidrine 	const char *name;
2507248aee62SJacques Vidrine 	const struct addrinfo *pai;
2508d7057edbSHajimu UMEMOTO 	char *ypdomain;
2509d7057edbSHajimu UMEMOTO 
2510d7057edbSHajimu UMEMOTO 	if (_yp_check(&ypdomain) == 0)
2511d7057edbSHajimu UMEMOTO 		return NS_UNAVAIL;
2512248aee62SJacques Vidrine 
2513248aee62SJacques Vidrine 	name = va_arg(ap, char *);
2514248aee62SJacques Vidrine 	pai = va_arg(ap, const struct addrinfo *);
2515248aee62SJacques Vidrine 
2516248aee62SJacques Vidrine 	memset(&sentinel, 0, sizeof(sentinel));
2517248aee62SJacques Vidrine 	cur = &sentinel;
2518248aee62SJacques Vidrine 
2519248aee62SJacques Vidrine 	/* hosts.byname is only for IPv4 (Solaris8) */
2520248aee62SJacques Vidrine 	if (pai->ai_family == PF_UNSPEC || pai->ai_family == PF_INET) {
2521d7057edbSHajimu UMEMOTO 		r = yp_match(ypdomain, "hosts.byname", name,
2522d7057edbSHajimu UMEMOTO 			(int)strlen(name), &ypbuf, &ypbuflen);
2523248aee62SJacques Vidrine 		if (r == 0) {
2524248aee62SJacques Vidrine 			struct addrinfo ai4;
2525248aee62SJacques Vidrine 
2526248aee62SJacques Vidrine 			ai4 = *pai;
2527248aee62SJacques Vidrine 			ai4.ai_family = AF_INET;
2528d7057edbSHajimu UMEMOTO 			ai = _yphostent(ypbuf, &ai4);
2529248aee62SJacques Vidrine 			if (ai) {
2530248aee62SJacques Vidrine 				cur->ai_next = ai;
2531248aee62SJacques Vidrine 				while (cur && cur->ai_next)
2532248aee62SJacques Vidrine 					cur = cur->ai_next;
2533248aee62SJacques Vidrine 			}
2534d7057edbSHajimu UMEMOTO 			free(ypbuf);
2535248aee62SJacques Vidrine 		}
2536f8997ca2SHajimu UMEMOTO 	}
2537248aee62SJacques Vidrine 
2538248aee62SJacques Vidrine 	/* ipnodes.byname can hold both IPv4/v6 */
2539d7057edbSHajimu UMEMOTO 	r = yp_match(ypdomain, "ipnodes.byname", name,
2540d7057edbSHajimu UMEMOTO 		(int)strlen(name), &ypbuf, &ypbuflen);
2541248aee62SJacques Vidrine 	if (r == 0) {
2542d7057edbSHajimu UMEMOTO 		ai = _yphostent(ypbuf, pai);
2543d7057edbSHajimu UMEMOTO 		if (ai)
2544248aee62SJacques Vidrine 			cur->ai_next = ai;
2545d7057edbSHajimu UMEMOTO 		free(ypbuf);
2546248aee62SJacques Vidrine 	}
2547248aee62SJacques Vidrine 
2548248aee62SJacques Vidrine 	if (sentinel.ai_next == NULL) {
25495342d17fSHajimu UMEMOTO 		RES_SET_H_ERRNO(__res_state(), HOST_NOT_FOUND);
2550248aee62SJacques Vidrine 		return NS_NOTFOUND;
2551248aee62SJacques Vidrine 	}
2552248aee62SJacques Vidrine 	*((struct addrinfo **)rv) = sentinel.ai_next;
2553248aee62SJacques Vidrine 	return NS_SUCCESS;
2554a42af91cSHajimu UMEMOTO }
2555a42af91cSHajimu UMEMOTO #endif
2556a42af91cSHajimu UMEMOTO 
2557a42af91cSHajimu UMEMOTO /* resolver logic */
2558a42af91cSHajimu UMEMOTO 
2559a42af91cSHajimu UMEMOTO /*
2560a42af91cSHajimu UMEMOTO  * Formulate a normal query, send, and await answer.
2561a42af91cSHajimu UMEMOTO  * Returned answer is placed in supplied buffer "answer".
2562a42af91cSHajimu UMEMOTO  * Perform preliminary check of answer, returning success only
2563a42af91cSHajimu UMEMOTO  * if no error is indicated and the answer count is nonzero.
2564a42af91cSHajimu UMEMOTO  * Return the size of the response on success, -1 on error.
2565a42af91cSHajimu UMEMOTO  * Error number is left in h_errno.
2566a42af91cSHajimu UMEMOTO  *
2567a42af91cSHajimu UMEMOTO  * Caller must parse answer and determine whether it answers the question.
2568a42af91cSHajimu UMEMOTO  */
2569a42af91cSHajimu UMEMOTO static int
257090d1d883SHajimu UMEMOTO res_queryN(const char *name, struct res_target *target, res_state res)
2571a42af91cSHajimu UMEMOTO {
25725cd588cbSHajimu UMEMOTO 	u_char *buf;
2573a42af91cSHajimu UMEMOTO 	HEADER *hp;
2574a42af91cSHajimu UMEMOTO 	int n;
25751259a17aSHajimu UMEMOTO 	u_int oflags;
2576a42af91cSHajimu UMEMOTO 	struct res_target *t;
2577a42af91cSHajimu UMEMOTO 	int rcode;
2578a42af91cSHajimu UMEMOTO 	int ancount;
2579a42af91cSHajimu UMEMOTO 
2580a42af91cSHajimu UMEMOTO 	rcode = NOERROR;
2581a42af91cSHajimu UMEMOTO 	ancount = 0;
2582a42af91cSHajimu UMEMOTO 
25835cd588cbSHajimu UMEMOTO 	buf = malloc(MAXPACKET);
25845cd588cbSHajimu UMEMOTO 	if (!buf) {
25855342d17fSHajimu UMEMOTO 		RES_SET_H_ERRNO(res, NETDB_INTERNAL);
25865cd588cbSHajimu UMEMOTO 		return -1;
25875cd588cbSHajimu UMEMOTO 	}
25885cd588cbSHajimu UMEMOTO 
2589a42af91cSHajimu UMEMOTO 	for (t = target; t; t = t->next) {
2590a42af91cSHajimu UMEMOTO 		int class, type;
2591a42af91cSHajimu UMEMOTO 		u_char *answer;
2592a42af91cSHajimu UMEMOTO 		int anslen;
2593a42af91cSHajimu UMEMOTO 
2594a42af91cSHajimu UMEMOTO 		hp = (HEADER *)(void *)t->answer;
2595a42af91cSHajimu UMEMOTO 
2596a42af91cSHajimu UMEMOTO 		/* make it easier... */
2597b826397aSJun-ichiro itojun Hagino 		class = t->qclass;
2598b826397aSJun-ichiro itojun Hagino 		type = t->qtype;
2599a42af91cSHajimu UMEMOTO 		answer = t->answer;
2600a42af91cSHajimu UMEMOTO 		anslen = t->anslen;
26011259a17aSHajimu UMEMOTO 
26021259a17aSHajimu UMEMOTO 		oflags = res->_flags;
26031259a17aSHajimu UMEMOTO 
26041259a17aSHajimu UMEMOTO again:
26051259a17aSHajimu UMEMOTO 		hp->rcode = NOERROR;	/* default */
26061259a17aSHajimu UMEMOTO 
2607a42af91cSHajimu UMEMOTO #ifdef DEBUG
26085342d17fSHajimu UMEMOTO 		if (res->options & RES_DEBUG)
2609a42af91cSHajimu UMEMOTO 			printf(";; res_query(%s, %d, %d)\n", name, class, type);
2610a42af91cSHajimu UMEMOTO #endif
2611a42af91cSHajimu UMEMOTO 
26125342d17fSHajimu UMEMOTO 		n = res_nmkquery(res, QUERY, name, class, type, NULL, 0, NULL,
26135cd588cbSHajimu UMEMOTO 		    buf, MAXPACKET);
26141259a17aSHajimu UMEMOTO 		if (n > 0 && (res->_flags & RES_F_EDNS0ERR) == 0 &&
26151259a17aSHajimu UMEMOTO 		    (res->options & (RES_USE_EDNS0|RES_USE_DNSSEC)) != 0U)
26165342d17fSHajimu UMEMOTO 			n = res_nopt(res, n, buf, MAXPACKET, anslen);
2617a42af91cSHajimu UMEMOTO 		if (n <= 0) {
2618a42af91cSHajimu UMEMOTO #ifdef DEBUG
26195342d17fSHajimu UMEMOTO 			if (res->options & RES_DEBUG)
2620a42af91cSHajimu UMEMOTO 				printf(";; res_query: mkquery failed\n");
2621a42af91cSHajimu UMEMOTO #endif
26225cd588cbSHajimu UMEMOTO 			free(buf);
26235342d17fSHajimu UMEMOTO 			RES_SET_H_ERRNO(res, NO_RECOVERY);
2624a42af91cSHajimu UMEMOTO 			return (n);
2625a42af91cSHajimu UMEMOTO 		}
26265342d17fSHajimu UMEMOTO 		n = res_nsend(res, buf, n, answer, anslen);
2627a42af91cSHajimu UMEMOTO 		if (n < 0) {
26281259a17aSHajimu UMEMOTO 			/*
26291259a17aSHajimu UMEMOTO 			 * if the query choked with EDNS0, retry
26301259a17aSHajimu UMEMOTO 			 * without EDNS0
26311259a17aSHajimu UMEMOTO 			 */
26321259a17aSHajimu UMEMOTO 			if ((res->options & (RES_USE_EDNS0|RES_USE_DNSSEC))
26331259a17aSHajimu UMEMOTO 			    != 0U &&
26341259a17aSHajimu UMEMOTO 			    ((oflags ^ res->_flags) & RES_F_EDNS0ERR) != 0) {
26351259a17aSHajimu UMEMOTO 				res->_flags |= RES_F_EDNS0ERR;
26361259a17aSHajimu UMEMOTO 				if (res->options & RES_DEBUG)
26371259a17aSHajimu UMEMOTO 					printf(";; res_nquery: retry without EDNS0\n");
26381259a17aSHajimu UMEMOTO 				goto again;
26391259a17aSHajimu UMEMOTO 			}
26401259a17aSHajimu UMEMOTO 			rcode = hp->rcode;	/* record most recent error */
2641a42af91cSHajimu UMEMOTO #ifdef DEBUG
26425342d17fSHajimu UMEMOTO 			if (res->options & RES_DEBUG)
2643a42af91cSHajimu UMEMOTO 				printf(";; res_query: send error\n");
2644a42af91cSHajimu UMEMOTO #endif
26451259a17aSHajimu UMEMOTO 			continue;
2646a42af91cSHajimu UMEMOTO 		}
2647a42af91cSHajimu UMEMOTO 
2648d05ad2abSHajimu UMEMOTO 		if (n > anslen)
264954384cf3SJacques Vidrine 			hp->rcode = FORMERR; /* XXX not very informative */
26501259a17aSHajimu UMEMOTO 		if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
2651a42af91cSHajimu UMEMOTO 			rcode = hp->rcode;	/* record most recent error */
2652a42af91cSHajimu UMEMOTO #ifdef DEBUG
26535342d17fSHajimu UMEMOTO 			if (res->options & RES_DEBUG)
2654dd521ef1SHajimu UMEMOTO 				printf(";; rcode = %u, ancount=%u\n", hp->rcode,
2655a42af91cSHajimu UMEMOTO 				    ntohs(hp->ancount));
2656a42af91cSHajimu UMEMOTO #endif
2657a42af91cSHajimu UMEMOTO 			continue;
2658a42af91cSHajimu UMEMOTO 		}
2659a42af91cSHajimu UMEMOTO 
2660a42af91cSHajimu UMEMOTO 		ancount += ntohs(hp->ancount);
2661a42af91cSHajimu UMEMOTO 
2662a42af91cSHajimu UMEMOTO 		t->n = n;
2663a42af91cSHajimu UMEMOTO 	}
2664a42af91cSHajimu UMEMOTO 
26655cd588cbSHajimu UMEMOTO 	free(buf);
26665cd588cbSHajimu UMEMOTO 
2667a42af91cSHajimu UMEMOTO 	if (ancount == 0) {
2668a42af91cSHajimu UMEMOTO 		switch (rcode) {
2669a42af91cSHajimu UMEMOTO 		case NXDOMAIN:
26705342d17fSHajimu UMEMOTO 			RES_SET_H_ERRNO(res, HOST_NOT_FOUND);
2671a42af91cSHajimu UMEMOTO 			break;
2672a42af91cSHajimu UMEMOTO 		case SERVFAIL:
26735342d17fSHajimu UMEMOTO 			RES_SET_H_ERRNO(res, TRY_AGAIN);
2674a42af91cSHajimu UMEMOTO 			break;
2675a42af91cSHajimu UMEMOTO 		case NOERROR:
26765342d17fSHajimu UMEMOTO 			RES_SET_H_ERRNO(res, NO_DATA);
2677a42af91cSHajimu UMEMOTO 			break;
2678a42af91cSHajimu UMEMOTO 		case FORMERR:
2679a42af91cSHajimu UMEMOTO 		case NOTIMP:
2680a42af91cSHajimu UMEMOTO 		case REFUSED:
2681a42af91cSHajimu UMEMOTO 		default:
26825342d17fSHajimu UMEMOTO 			RES_SET_H_ERRNO(res, NO_RECOVERY);
2683a42af91cSHajimu UMEMOTO 			break;
2684a42af91cSHajimu UMEMOTO 		}
2685a42af91cSHajimu UMEMOTO 		return (-1);
2686a42af91cSHajimu UMEMOTO 	}
2687a42af91cSHajimu UMEMOTO 	return (ancount);
2688a42af91cSHajimu UMEMOTO }
2689a42af91cSHajimu UMEMOTO 
2690a42af91cSHajimu UMEMOTO /*
2691a42af91cSHajimu UMEMOTO  * Formulate a normal query, send, and retrieve answer in supplied buffer.
2692a42af91cSHajimu UMEMOTO  * Return the size of the response on success, -1 on error.
2693a42af91cSHajimu UMEMOTO  * If enabled, implement search rules until answer or unrecoverable failure
2694a42af91cSHajimu UMEMOTO  * is detected.  Error code, if any, is left in h_errno.
2695a42af91cSHajimu UMEMOTO  */
2696a42af91cSHajimu UMEMOTO static int
269790d1d883SHajimu UMEMOTO res_searchN(const char *name, struct res_target *target, res_state res)
2698a42af91cSHajimu UMEMOTO {
2699a42af91cSHajimu UMEMOTO 	const char *cp, * const *domain;
2700a42af91cSHajimu UMEMOTO 	HEADER *hp = (HEADER *)(void *)target->answer;	/*XXX*/
2701a42af91cSHajimu UMEMOTO 	u_int dots;
2702a42af91cSHajimu UMEMOTO 	int trailing_dot, ret, saved_herrno;
27035ecccefbSHajimu UMEMOTO 	int got_nodata = 0, got_servfail = 0, root_on_list = 0;
27045ecccefbSHajimu UMEMOTO 	int tried_as_is = 0;
27055ecccefbSHajimu UMEMOTO 	int searched = 0;
2706159d2a98SHajimu UMEMOTO 	char abuf[MAXDNAME];
2707a42af91cSHajimu UMEMOTO 
2708a42af91cSHajimu UMEMOTO 	errno = 0;
27095342d17fSHajimu UMEMOTO 	RES_SET_H_ERRNO(res, HOST_NOT_FOUND); /* default, if we never query */
2710a42af91cSHajimu UMEMOTO 	dots = 0;
2711a42af91cSHajimu UMEMOTO 	for (cp = name; *cp; cp++)
2712a42af91cSHajimu UMEMOTO 		dots += (*cp == '.');
2713a42af91cSHajimu UMEMOTO 	trailing_dot = 0;
2714a42af91cSHajimu UMEMOTO 	if (cp > name && *--cp == '.')
2715a42af91cSHajimu UMEMOTO 		trailing_dot++;
2716a42af91cSHajimu UMEMOTO 
2717a42af91cSHajimu UMEMOTO 	/*
2718a42af91cSHajimu UMEMOTO 	 * if there aren't any dots, it could be a user-level alias
2719a42af91cSHajimu UMEMOTO 	 */
27205342d17fSHajimu UMEMOTO 	if (!dots &&
27215342d17fSHajimu UMEMOTO 	    (cp = res_hostalias(res, name, abuf, sizeof(abuf))) != NULL)
27225342d17fSHajimu UMEMOTO 		return (res_queryN(cp, target, res));
2723a42af91cSHajimu UMEMOTO 
2724a42af91cSHajimu UMEMOTO 	/*
27255ecccefbSHajimu UMEMOTO 	 * If there are enough dots in the name, let's just give it a
27265ecccefbSHajimu UMEMOTO 	 * try 'as is'. The threshold can be set with the "ndots" option.
27275ecccefbSHajimu UMEMOTO 	 * Also, query 'as is', if there is a trailing dot in the name.
2728a42af91cSHajimu UMEMOTO 	 */
2729a42af91cSHajimu UMEMOTO 	saved_herrno = -1;
27305342d17fSHajimu UMEMOTO 	if (dots >= res->ndots || trailing_dot) {
27315342d17fSHajimu UMEMOTO 		ret = res_querydomainN(name, NULL, target, res);
27325ecccefbSHajimu UMEMOTO 		if (ret > 0 || trailing_dot)
2733a42af91cSHajimu UMEMOTO 			return (ret);
27343a0b3b67SHajimu UMEMOTO 		if (errno == ECONNREFUSED) {
27355342d17fSHajimu UMEMOTO 			RES_SET_H_ERRNO(res, TRY_AGAIN);
27363a0b3b67SHajimu UMEMOTO 			return (-1);
27373a0b3b67SHajimu UMEMOTO 		}
27385342d17fSHajimu UMEMOTO 		switch (res->res_h_errno) {
27393a0b3b67SHajimu UMEMOTO 		case NO_DATA:
27403a0b3b67SHajimu UMEMOTO 		case HOST_NOT_FOUND:
27413a0b3b67SHajimu UMEMOTO 			break;
2742aebc6ec2SHajimu UMEMOTO 		case TRY_AGAIN:
2743aebc6ec2SHajimu UMEMOTO 			if (hp->rcode == SERVFAIL)
2744aebc6ec2SHajimu UMEMOTO 				break;
2745aebc6ec2SHajimu UMEMOTO 			/* FALLTHROUGH */
27463a0b3b67SHajimu UMEMOTO 		default:
27473a0b3b67SHajimu UMEMOTO 			return (-1);
27483a0b3b67SHajimu UMEMOTO 		}
27495342d17fSHajimu UMEMOTO 		saved_herrno = res->res_h_errno;
2750a42af91cSHajimu UMEMOTO 		tried_as_is++;
2751a42af91cSHajimu UMEMOTO 	}
2752a42af91cSHajimu UMEMOTO 
2753a42af91cSHajimu UMEMOTO 	/*
2754a42af91cSHajimu UMEMOTO 	 * We do at least one level of search if
2755a42af91cSHajimu UMEMOTO 	 *	- there is no dot and RES_DEFNAME is set, or
2756a42af91cSHajimu UMEMOTO 	 *	- there is at least one dot, there is no trailing dot,
2757a42af91cSHajimu UMEMOTO 	 *	  and RES_DNSRCH is set.
2758a42af91cSHajimu UMEMOTO 	 */
27595342d17fSHajimu UMEMOTO 	if ((!dots && (res->options & RES_DEFNAMES)) ||
27605342d17fSHajimu UMEMOTO 	    (dots && !trailing_dot && (res->options & RES_DNSRCH))) {
2761a42af91cSHajimu UMEMOTO 		int done = 0;
2762a42af91cSHajimu UMEMOTO 
27635342d17fSHajimu UMEMOTO 		for (domain = (const char * const *)res->dnsrch;
2764a42af91cSHajimu UMEMOTO 		   *domain && !done;
2765a42af91cSHajimu UMEMOTO 		   domain++) {
27665ecccefbSHajimu UMEMOTO 			searched = 1;
27675ecccefbSHajimu UMEMOTO 
27685ecccefbSHajimu UMEMOTO 			if (domain[0][0] == '\0' ||
27695ecccefbSHajimu UMEMOTO 			    (domain[0][0] == '.' && domain[0][1] == '\0'))
27705ecccefbSHajimu UMEMOTO 				root_on_list++;
27715ecccefbSHajimu UMEMOTO 
27725ecccefbSHajimu UMEMOTO 			if (root_on_list && tried_as_is)
27735ecccefbSHajimu UMEMOTO 				continue;
2774a42af91cSHajimu UMEMOTO 
27755342d17fSHajimu UMEMOTO 			ret = res_querydomainN(name, *domain, target, res);
2776a42af91cSHajimu UMEMOTO 			if (ret > 0)
2777a42af91cSHajimu UMEMOTO 				return (ret);
2778a42af91cSHajimu UMEMOTO 
2779a42af91cSHajimu UMEMOTO 			/*
2780a42af91cSHajimu UMEMOTO 			 * If no server present, give up.
2781a42af91cSHajimu UMEMOTO 			 * If name isn't found in this domain,
2782a42af91cSHajimu UMEMOTO 			 * keep trying higher domains in the search list
2783a42af91cSHajimu UMEMOTO 			 * (if that's enabled).
2784a42af91cSHajimu UMEMOTO 			 * On a NO_DATA error, keep trying, otherwise
2785a42af91cSHajimu UMEMOTO 			 * a wildcard entry of another type could keep us
2786a42af91cSHajimu UMEMOTO 			 * from finding this entry higher in the domain.
2787a42af91cSHajimu UMEMOTO 			 * If we get some other error (negative answer or
2788a42af91cSHajimu UMEMOTO 			 * server failure), then stop searching up,
2789a42af91cSHajimu UMEMOTO 			 * but try the input name below in case it's
2790a42af91cSHajimu UMEMOTO 			 * fully-qualified.
2791a42af91cSHajimu UMEMOTO 			 */
2792a42af91cSHajimu UMEMOTO 			if (errno == ECONNREFUSED) {
27935342d17fSHajimu UMEMOTO 				RES_SET_H_ERRNO(res, TRY_AGAIN);
2794a42af91cSHajimu UMEMOTO 				return (-1);
2795a42af91cSHajimu UMEMOTO 			}
2796a42af91cSHajimu UMEMOTO 
27975342d17fSHajimu UMEMOTO 			switch (res->res_h_errno) {
2798a42af91cSHajimu UMEMOTO 			case NO_DATA:
2799a42af91cSHajimu UMEMOTO 				got_nodata++;
2800a42af91cSHajimu UMEMOTO 				/* FALLTHROUGH */
2801a42af91cSHajimu UMEMOTO 			case HOST_NOT_FOUND:
2802a42af91cSHajimu UMEMOTO 				/* keep trying */
2803a42af91cSHajimu UMEMOTO 				break;
2804a42af91cSHajimu UMEMOTO 			case TRY_AGAIN:
2805aebc6ec2SHajimu UMEMOTO 				got_servfail++;
2806a42af91cSHajimu UMEMOTO 				if (hp->rcode == SERVFAIL) {
2807a42af91cSHajimu UMEMOTO 					/* try next search element, if any */
2808a42af91cSHajimu UMEMOTO 					break;
2809a42af91cSHajimu UMEMOTO 				}
2810a42af91cSHajimu UMEMOTO 				/* FALLTHROUGH */
2811a42af91cSHajimu UMEMOTO 			default:
2812a42af91cSHajimu UMEMOTO 				/* anything else implies that we're done */
2813a42af91cSHajimu UMEMOTO 				done++;
2814a42af91cSHajimu UMEMOTO 			}
2815a42af91cSHajimu UMEMOTO 			/*
2816a42af91cSHajimu UMEMOTO 			 * if we got here for some reason other than DNSRCH,
2817a42af91cSHajimu UMEMOTO 			 * we only wanted one iteration of the loop, so stop.
2818a42af91cSHajimu UMEMOTO 			 */
28195342d17fSHajimu UMEMOTO 			if (!(res->options & RES_DNSRCH))
2820a42af91cSHajimu UMEMOTO 			        done++;
2821a42af91cSHajimu UMEMOTO 		}
2822a42af91cSHajimu UMEMOTO 	}
2823a42af91cSHajimu UMEMOTO 
28245342d17fSHajimu UMEMOTO 	switch (res->res_h_errno) {
28253a0b3b67SHajimu UMEMOTO 	case NO_DATA:
28263a0b3b67SHajimu UMEMOTO 	case HOST_NOT_FOUND:
28273a0b3b67SHajimu UMEMOTO 		break;
2828aebc6ec2SHajimu UMEMOTO 	case TRY_AGAIN:
2829aebc6ec2SHajimu UMEMOTO 		if (hp->rcode == SERVFAIL)
2830aebc6ec2SHajimu UMEMOTO 			break;
2831aebc6ec2SHajimu UMEMOTO 		/* FALLTHROUGH */
28323a0b3b67SHajimu UMEMOTO 	default:
28333a0b3b67SHajimu UMEMOTO 		goto giveup;
28343a0b3b67SHajimu UMEMOTO 	}
28353a0b3b67SHajimu UMEMOTO 
2836a42af91cSHajimu UMEMOTO 	/*
28375ecccefbSHajimu UMEMOTO 	 * If the query has not already been tried as is then try it
28385ecccefbSHajimu UMEMOTO 	 * unless RES_NOTLDQUERY is set and there were no dots.
2839a42af91cSHajimu UMEMOTO 	 */
28405342d17fSHajimu UMEMOTO 	if ((dots || !searched || !(res->options & RES_NOTLDQUERY)) &&
28415ecccefbSHajimu UMEMOTO 	    !(tried_as_is || root_on_list)) {
28425342d17fSHajimu UMEMOTO 		ret = res_querydomainN(name, NULL, target, res);
2843a42af91cSHajimu UMEMOTO 		if (ret > 0)
2844a42af91cSHajimu UMEMOTO 			return (ret);
2845a42af91cSHajimu UMEMOTO 	}
2846a42af91cSHajimu UMEMOTO 
2847a42af91cSHajimu UMEMOTO 	/*
2848a42af91cSHajimu UMEMOTO 	 * if we got here, we didn't satisfy the search.
2849a42af91cSHajimu UMEMOTO 	 * if we did an initial full query, return that query's h_errno
2850a42af91cSHajimu UMEMOTO 	 * (note that we wouldn't be here if that query had succeeded).
2851a42af91cSHajimu UMEMOTO 	 * else if we ever got a nodata, send that back as the reason.
2852a42af91cSHajimu UMEMOTO 	 * else send back meaningless h_errno, that being the one from
2853a42af91cSHajimu UMEMOTO 	 * the last DNSRCH we did.
2854a42af91cSHajimu UMEMOTO 	 */
28553a0b3b67SHajimu UMEMOTO giveup:
2856a42af91cSHajimu UMEMOTO 	if (saved_herrno != -1)
28575342d17fSHajimu UMEMOTO 		RES_SET_H_ERRNO(res, saved_herrno);
2858a42af91cSHajimu UMEMOTO 	else if (got_nodata)
28595342d17fSHajimu UMEMOTO 		RES_SET_H_ERRNO(res, NO_DATA);
2860a42af91cSHajimu UMEMOTO 	else if (got_servfail)
28615342d17fSHajimu UMEMOTO 		RES_SET_H_ERRNO(res, TRY_AGAIN);
2862a42af91cSHajimu UMEMOTO 	return (-1);
2863a42af91cSHajimu UMEMOTO }
2864a42af91cSHajimu UMEMOTO 
2865a42af91cSHajimu UMEMOTO /*
2866a42af91cSHajimu UMEMOTO  * Perform a call on res_query on the concatenation of name and domain,
2867a42af91cSHajimu UMEMOTO  * removing a trailing dot from name if domain is NULL.
2868a42af91cSHajimu UMEMOTO  */
2869a42af91cSHajimu UMEMOTO static int
287090d1d883SHajimu UMEMOTO res_querydomainN(const char *name, const char *domain,
287190d1d883SHajimu UMEMOTO     struct res_target *target, res_state res)
2872a42af91cSHajimu UMEMOTO {
2873a42af91cSHajimu UMEMOTO 	char nbuf[MAXDNAME];
2874a42af91cSHajimu UMEMOTO 	const char *longname = nbuf;
2875a42af91cSHajimu UMEMOTO 	size_t n, d;
2876a42af91cSHajimu UMEMOTO 
2877a42af91cSHajimu UMEMOTO #ifdef DEBUG
28785342d17fSHajimu UMEMOTO 	if (res->options & RES_DEBUG)
2879a42af91cSHajimu UMEMOTO 		printf(";; res_querydomain(%s, %s)\n",
2880a42af91cSHajimu UMEMOTO 			name, domain?domain:"<Nil>");
2881a42af91cSHajimu UMEMOTO #endif
2882a42af91cSHajimu UMEMOTO 	if (domain == NULL) {
2883a42af91cSHajimu UMEMOTO 		/*
2884a42af91cSHajimu UMEMOTO 		 * Check for trailing '.';
2885a42af91cSHajimu UMEMOTO 		 * copy without '.' if present.
2886a42af91cSHajimu UMEMOTO 		 */
2887a42af91cSHajimu UMEMOTO 		n = strlen(name);
2888a42af91cSHajimu UMEMOTO 		if (n >= MAXDNAME) {
28895342d17fSHajimu UMEMOTO 			RES_SET_H_ERRNO(res, NO_RECOVERY);
2890a42af91cSHajimu UMEMOTO 			return (-1);
2891a42af91cSHajimu UMEMOTO 		}
2892a42af91cSHajimu UMEMOTO 		if (n > 0 && name[--n] == '.') {
2893a42af91cSHajimu UMEMOTO 			strncpy(nbuf, name, n);
2894a42af91cSHajimu UMEMOTO 			nbuf[n] = '\0';
2895a42af91cSHajimu UMEMOTO 		} else
2896a42af91cSHajimu UMEMOTO 			longname = name;
2897a42af91cSHajimu UMEMOTO 	} else {
2898a42af91cSHajimu UMEMOTO 		n = strlen(name);
2899a42af91cSHajimu UMEMOTO 		d = strlen(domain);
2900a42af91cSHajimu UMEMOTO 		if (n + d + 1 >= MAXDNAME) {
29015342d17fSHajimu UMEMOTO 			RES_SET_H_ERRNO(res, NO_RECOVERY);
2902a42af91cSHajimu UMEMOTO 			return (-1);
2903a42af91cSHajimu UMEMOTO 		}
2904dd521ef1SHajimu UMEMOTO 		snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
2905a42af91cSHajimu UMEMOTO 	}
29065342d17fSHajimu UMEMOTO 	return (res_queryN(longname, target, res));
2907a42af91cSHajimu UMEMOTO }
2908