getaddrinfo.c revision 158115
1/*	$KAME: getaddrinfo.c,v 1.15 2000/07/09 04:37:24 itojun Exp $	*/
2
3/*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32/*
33 * "#ifdef FAITH" part is local hack for supporting IPv4-v6 translator.
34 *
35 * Issues to be discussed:
36 * - Thread safe-ness must be checked.
37 * - Return values.  There are nonstandard return values defined and used
38 *   in the source code.  This is because RFC2553 is silent about which error
39 *   code must be returned for which situation.
40 * - freeaddrinfo(NULL).  RFC2553 is silent about it.  XNET 5.2 says it is
41 *   invalid.  current code - SEGV on freeaddrinfo(NULL)
42 *
43 * Note:
44 * - The code filters out AFs that are not supported by the kernel,
45 *   when globbing NULL hostname (to loopback, or wildcard).  Is it the right
46 *   thing to do?  What is the relationship with post-RFC2553 AI_ADDRCONFIG
47 *   in ai_flags?
48 * - (post-2553) semantics of AI_ADDRCONFIG itself is too vague.
49 *   (1) what should we do against numeric hostname (2) what should we do
50 *   against NULL hostname (3) what is AI_ADDRCONFIG itself.  AF not ready?
51 *   non-loopback address configured?  global address configured?
52 *
53 * OS specific notes for netbsd/openbsd/freebsd4/bsdi4:
54 * - To avoid search order issue, we have a big amount of code duplicate
55 *   from gethnamaddr.c and some other places.  The issues that there's no
56 *   lower layer function to lookup "IPv4 or IPv6" record.  Calling
57 *   gethostbyname2 from getaddrinfo will end up in wrong search order, as
58 *   presented above.
59 *
60 * OS specific notes for freebsd4:
61 * - FreeBSD supported $GAI.  The code does not.
62 * - FreeBSD allowed classful IPv4 numeric (127.1), the code does not.
63 */
64
65#include <sys/cdefs.h>
66__FBSDID("$FreeBSD: head/lib/libc/net/getaddrinfo.c 158115 2006-04-28 12:03:38Z ume $");
67
68#include "namespace.h"
69#include <sys/types.h>
70#include <sys/param.h>
71#include <sys/socket.h>
72#include <net/if.h>
73#include <netinet/in.h>
74#include <sys/queue.h>
75#ifdef INET6
76#include <net/if_var.h>
77#include <sys/sysctl.h>
78#include <sys/ioctl.h>
79#include <netinet6/in6_var.h>	/* XXX */
80#endif
81#include <arpa/inet.h>
82#include <arpa/nameser.h>
83#include <rpc/rpc.h>
84#include <rpcsvc/yp_prot.h>
85#include <rpcsvc/ypclnt.h>
86#include <netdb.h>
87#include <resolv.h>
88#include <string.h>
89#include <stdlib.h>
90#include <stddef.h>
91#include <ctype.h>
92#include <unistd.h>
93#include <stdio.h>
94#include <errno.h>
95
96#include "res_config.h"
97
98#ifdef DEBUG
99#include <syslog.h>
100#endif
101
102#include <stdarg.h>
103#include <nsswitch.h>
104#include "un-namespace.h"
105#include "libc_private.h"
106#ifdef NS_CACHING
107#include "nscache.h"
108#endif
109
110#if defined(__KAME__) && defined(INET6)
111# define FAITH
112#endif
113
114#define SUCCESS 0
115#define ANY 0
116#define YES 1
117#define NO  0
118
119static const char in_addrany[] = { 0, 0, 0, 0 };
120static const char in_loopback[] = { 127, 0, 0, 1 };
121#ifdef INET6
122static const char in6_addrany[] = {
123	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
124};
125static const char in6_loopback[] = {
126	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
127};
128#endif
129
130struct policyqueue {
131	TAILQ_ENTRY(policyqueue) pc_entry;
132#ifdef INET6
133	struct in6_addrpolicy pc_policy;
134#endif
135};
136TAILQ_HEAD(policyhead, policyqueue);
137
138static const struct afd {
139	int a_af;
140	int a_addrlen;
141	socklen_t a_socklen;
142	int a_off;
143	const char *a_addrany;
144	const char *a_loopback;
145	int a_scoped;
146} afdl [] = {
147#ifdef INET6
148#define	N_INET6 0
149	{PF_INET6, sizeof(struct in6_addr),
150	 sizeof(struct sockaddr_in6),
151	 offsetof(struct sockaddr_in6, sin6_addr),
152	 in6_addrany, in6_loopback, 1},
153#define	N_INET 1
154#else
155#define	N_INET 0
156#endif
157	{PF_INET, sizeof(struct in_addr),
158	 sizeof(struct sockaddr_in),
159	 offsetof(struct sockaddr_in, sin_addr),
160	 in_addrany, in_loopback, 0},
161	{0, 0, 0, 0, NULL, NULL, 0},
162};
163
164struct explore {
165	int e_af;
166	int e_socktype;
167	int e_protocol;
168	const char *e_protostr;
169	int e_wild;
170#define WILD_AF(ex)		((ex)->e_wild & 0x01)
171#define WILD_SOCKTYPE(ex)	((ex)->e_wild & 0x02)
172#define WILD_PROTOCOL(ex)	((ex)->e_wild & 0x04)
173};
174
175static const struct explore explore[] = {
176#if 0
177	{ PF_LOCAL, 0, ANY, ANY, NULL, 0x01 },
178#endif
179#ifdef INET6
180	{ PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
181	{ PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
182	{ PF_INET6, SOCK_RAW, ANY, NULL, 0x05 },
183#endif
184	{ PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
185	{ PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
186	{ PF_INET, SOCK_RAW, ANY, NULL, 0x05 },
187	{ PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
188	{ PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
189	{ PF_UNSPEC, SOCK_RAW, ANY, NULL, 0x05 },
190	{ -1, 0, 0, NULL, 0 },
191};
192
193#ifdef INET6
194#define PTON_MAX	16
195#else
196#define PTON_MAX	4
197#endif
198
199#define AIO_SRCFLAG_DEPRECATED	0x1
200
201struct ai_order {
202	union {
203		struct sockaddr_storage aiou_ss;
204		struct sockaddr aiou_sa;
205	} aio_src_un;
206#define aio_srcsa aio_src_un.aiou_sa
207	u_int32_t aio_srcflag;
208	int aio_srcscope;
209	int aio_dstscope;
210	struct policyqueue *aio_srcpolicy;
211	struct policyqueue *aio_dstpolicy;
212	struct addrinfo *aio_ai;
213	int aio_matchlen;
214};
215
216static const ns_src default_dns_files[] = {
217	{ NSSRC_FILES, 	NS_SUCCESS },
218	{ NSSRC_DNS, 	NS_SUCCESS },
219	{ 0 }
220};
221
222struct res_target {
223	struct res_target *next;
224	const char *name;	/* domain name */
225	int qclass, qtype;	/* class and type of query */
226	u_char *answer;		/* buffer to put answer */
227	int anslen;		/* size of answer buffer */
228	int n;			/* result length */
229};
230
231#define MAXPACKET	(64*1024)
232
233typedef union {
234	HEADER hdr;
235	u_char buf[MAXPACKET];
236} querybuf;
237
238static int str2number(const char *);
239static int explore_null(const struct addrinfo *,
240	const char *, struct addrinfo **);
241static int explore_numeric(const struct addrinfo *, const char *,
242	const char *, struct addrinfo **, const char *);
243static int explore_numeric_scope(const struct addrinfo *, const char *,
244	const char *, struct addrinfo **);
245static int get_canonname(const struct addrinfo *,
246	struct addrinfo *, const char *);
247static struct addrinfo *get_ai(const struct addrinfo *,
248	const struct afd *, const char *);
249static int get_portmatch(const struct addrinfo *, const char *);
250static int get_port(struct addrinfo *, const char *, int);
251static const struct afd *find_afd(int);
252static int addrconfig(struct addrinfo *);
253static void set_source(struct ai_order *, struct policyhead *);
254static int comp_dst(const void *, const void *);
255#ifdef INET6
256static int ip6_str2scopeid(char *, struct sockaddr_in6 *, u_int32_t *);
257#endif
258static int gai_addr2scopetype(struct sockaddr *);
259
260static int explore_fqdn(const struct addrinfo *, const char *,
261	const char *, struct addrinfo **);
262
263static int reorder(struct addrinfo *);
264static int get_addrselectpolicy(struct policyhead *);
265static void free_addrselectpolicy(struct policyhead *);
266static struct policyqueue *match_addrselectpolicy(struct sockaddr *,
267	struct policyhead *);
268static int matchlen(struct sockaddr *, struct sockaddr *);
269
270static struct addrinfo *getanswer(const querybuf *, int, const char *, int,
271	const struct addrinfo *, res_state);
272#if defined(RESOLVSORT)
273static int addr4sort(struct addrinfo *, res_state);
274#endif
275static int _dns_getaddrinfo(void *, void *, va_list);
276static void _sethtent(FILE **);
277static void _endhtent(FILE **);
278static struct addrinfo *_gethtent(FILE **, const char *,
279	const struct addrinfo *);
280static int _files_getaddrinfo(void *, void *, va_list);
281#ifdef YP
282static struct addrinfo *_yphostent(char *, const struct addrinfo *);
283static int _yp_getaddrinfo(void *, void *, va_list);
284#endif
285#ifdef NS_CACHING
286static int addrinfo_id_func(char *, size_t *, va_list, void *);
287static int addrinfo_marshal_func(char *, size_t *, void *, va_list, void *);
288static int addrinfo_unmarshal_func(char *, size_t, void *, va_list, void *);
289#endif
290
291static int res_queryN(const char *, struct res_target *, res_state);
292static int res_searchN(const char *, struct res_target *, res_state);
293static int res_querydomainN(const char *, const char *,
294	struct res_target *, res_state);
295
296/* XXX macros that make external reference is BAD. */
297
298#define GET_AI(ai, afd, addr) \
299do { \
300	/* external reference: pai, error, and label free */ \
301	(ai) = get_ai(pai, (afd), (addr)); \
302	if ((ai) == NULL) { \
303		error = EAI_MEMORY; \
304		goto free; \
305	} \
306} while (/*CONSTCOND*/0)
307
308#define GET_PORT(ai, serv) \
309do { \
310	/* external reference: error and label free */ \
311	error = get_port((ai), (serv), 0); \
312	if (error != 0) \
313		goto free; \
314} while (/*CONSTCOND*/0)
315
316#define GET_CANONNAME(ai, str) \
317do { \
318	/* external reference: pai, error and label free */ \
319	error = get_canonname(pai, (ai), (str)); \
320	if (error != 0) \
321		goto free; \
322} while (/*CONSTCOND*/0)
323
324#define ERR(err) \
325do { \
326	/* external reference: error, and label bad */ \
327	error = (err); \
328	goto bad; \
329	/*NOTREACHED*/ \
330} while (/*CONSTCOND*/0)
331
332#define MATCH_FAMILY(x, y, w) \
333	((x) == (y) || (/*CONSTCOND*/(w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
334#define MATCH(x, y, w) \
335	((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY)))
336
337void
338freeaddrinfo(struct addrinfo *ai)
339{
340	struct addrinfo *next;
341
342	do {
343		next = ai->ai_next;
344		if (ai->ai_canonname)
345			free(ai->ai_canonname);
346		/* no need to free(ai->ai_addr) */
347		free(ai);
348		ai = next;
349	} while (ai);
350}
351
352static int
353str2number(const char *p)
354{
355	char *ep;
356	unsigned long v;
357
358	if (*p == '\0')
359		return -1;
360	ep = NULL;
361	errno = 0;
362	v = strtoul(p, &ep, 10);
363	if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX)
364		return v;
365	else
366		return -1;
367}
368
369int
370getaddrinfo(const char *hostname, const char *servname,
371    const struct addrinfo *hints, struct addrinfo **res)
372{
373	struct addrinfo sentinel;
374	struct addrinfo *cur;
375	int error = 0;
376	struct addrinfo ai;
377	struct addrinfo ai0;
378	struct addrinfo *pai;
379	const struct explore *ex;
380	int numeric = 0;
381
382	memset(&sentinel, 0, sizeof(sentinel));
383	cur = &sentinel;
384	pai = &ai;
385	pai->ai_flags = 0;
386	pai->ai_family = PF_UNSPEC;
387	pai->ai_socktype = ANY;
388	pai->ai_protocol = ANY;
389	pai->ai_addrlen = 0;
390	pai->ai_canonname = NULL;
391	pai->ai_addr = NULL;
392	pai->ai_next = NULL;
393
394	if (hostname == NULL && servname == NULL)
395		return EAI_NONAME;
396	if (hints) {
397		/* error check for hints */
398		if (hints->ai_addrlen || hints->ai_canonname ||
399		    hints->ai_addr || hints->ai_next)
400			ERR(EAI_BADHINTS); /* xxx */
401		if (hints->ai_flags & ~AI_MASK)
402			ERR(EAI_BADFLAGS);
403		switch (hints->ai_family) {
404		case PF_UNSPEC:
405		case PF_INET:
406#ifdef INET6
407		case PF_INET6:
408#endif
409			break;
410		default:
411			ERR(EAI_FAMILY);
412		}
413		memcpy(pai, hints, sizeof(*pai));
414
415		/*
416		 * if both socktype/protocol are specified, check if they
417		 * are meaningful combination.
418		 */
419		if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
420			for (ex = explore; ex->e_af >= 0; ex++) {
421				if (pai->ai_family != ex->e_af)
422					continue;
423				if (ex->e_socktype == ANY)
424					continue;
425				if (ex->e_protocol == ANY)
426					continue;
427				if (pai->ai_socktype == ex->e_socktype &&
428				    pai->ai_protocol != ex->e_protocol) {
429					ERR(EAI_BADHINTS);
430				}
431			}
432		}
433	}
434
435	/*
436	 * post-2553: AI_ALL and AI_V4MAPPED are effective only against
437	 * AF_INET6 query.  They need to be ignored if specified in other
438	 * occassions.
439	 */
440	switch (pai->ai_flags & (AI_ALL | AI_V4MAPPED)) {
441	case AI_V4MAPPED:
442	case AI_ALL | AI_V4MAPPED:
443		if (pai->ai_family != AF_INET6)
444			pai->ai_flags &= ~(AI_ALL | AI_V4MAPPED);
445		break;
446	case AI_ALL:
447#if 1
448		/* illegal */
449		ERR(EAI_BADFLAGS);
450#else
451		pai->ai_flags &= ~(AI_ALL | AI_V4MAPPED);
452#endif
453		break;
454	}
455
456	/*
457	 * check for special cases.  (1) numeric servname is disallowed if
458	 * socktype/protocol are left unspecified. (2) servname is disallowed
459	 * for raw and other inet{,6} sockets.
460	 */
461	if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
462#ifdef PF_INET6
463	    || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)
464#endif
465	    ) {
466		ai0 = *pai;	/* backup *pai */
467
468		if (pai->ai_family == PF_UNSPEC) {
469#ifdef PF_INET6
470			pai->ai_family = PF_INET6;
471#else
472			pai->ai_family = PF_INET;
473#endif
474		}
475		error = get_portmatch(pai, servname);
476		if (error)
477			ERR(error);
478
479		*pai = ai0;
480	}
481
482	ai0 = *pai;
483
484	/* NULL hostname, or numeric hostname */
485	for (ex = explore; ex->e_af >= 0; ex++) {
486		*pai = ai0;
487
488		/* PF_UNSPEC entries are prepared for DNS queries only */
489		if (ex->e_af == PF_UNSPEC)
490			continue;
491
492		if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex)))
493			continue;
494		if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex)))
495			continue;
496		if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex)))
497			continue;
498
499		if (pai->ai_family == PF_UNSPEC)
500			pai->ai_family = ex->e_af;
501		if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
502			pai->ai_socktype = ex->e_socktype;
503		if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
504			pai->ai_protocol = ex->e_protocol;
505
506		if (hostname == NULL)
507			error = explore_null(pai, servname, &cur->ai_next);
508		else
509			error = explore_numeric_scope(pai, hostname, servname,
510			    &cur->ai_next);
511
512		if (error)
513			goto free;
514
515		while (cur && cur->ai_next)
516			cur = cur->ai_next;
517	}
518
519	/*
520	 * XXX
521	 * If numreic representation of AF1 can be interpreted as FQDN
522	 * representation of AF2, we need to think again about the code below.
523	 */
524	if (sentinel.ai_next) {
525		numeric = 1;
526		goto good;
527	}
528
529	if (hostname == NULL)
530		ERR(EAI_NONAME);	/* used to be EAI_NODATA */
531	if (pai->ai_flags & AI_NUMERICHOST)
532		ERR(EAI_NONAME);
533
534	if ((pai->ai_flags & AI_ADDRCONFIG) != 0 && !addrconfig(&ai0))
535		ERR(EAI_FAIL);
536
537	/*
538	 * hostname as alphabetical name.
539	 * we would like to prefer AF_INET6 than AF_INET, so we'll make a
540	 * outer loop by AFs.
541	 */
542	for (ex = explore; ex->e_af >= 0; ex++) {
543		*pai = ai0;
544
545		/* require exact match for family field */
546		if (pai->ai_family != ex->e_af)
547			continue;
548
549		if (!MATCH(pai->ai_socktype, ex->e_socktype,
550				WILD_SOCKTYPE(ex))) {
551			continue;
552		}
553		if (!MATCH(pai->ai_protocol, ex->e_protocol,
554				WILD_PROTOCOL(ex))) {
555			continue;
556		}
557
558		if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
559			pai->ai_socktype = ex->e_socktype;
560		if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
561			pai->ai_protocol = ex->e_protocol;
562
563		error = explore_fqdn(pai, hostname, servname,
564			&cur->ai_next);
565
566		while (cur && cur->ai_next)
567			cur = cur->ai_next;
568	}
569
570	/* XXX inhibit errors if we have the result */
571	if (sentinel.ai_next)
572		error = 0;
573
574good:
575	/*
576	 * ensure we return either:
577	 * - error == 0, non-NULL *res
578	 * - error != 0, NULL *res
579	 */
580	if (error == 0) {
581		if (sentinel.ai_next) {
582			/*
583			 * If the returned entry is for an active connection,
584			 * and the given name is not numeric, reorder the
585			 * list, so that the application would try the list
586			 * in the most efficient order.
587			 */
588			if (hints == NULL || !(hints->ai_flags & AI_PASSIVE)) {
589				if (!numeric)
590					(void)reorder(&sentinel);
591			}
592			*res = sentinel.ai_next;
593			return SUCCESS;
594		} else
595			error = EAI_FAIL;
596	}
597free:
598bad:
599	if (sentinel.ai_next)
600		freeaddrinfo(sentinel.ai_next);
601	*res = NULL;
602	return error;
603}
604
605static int
606reorder(struct addrinfo *sentinel)
607{
608	struct addrinfo *ai, **aip;
609	struct ai_order *aio;
610	int i, n;
611	struct policyhead policyhead;
612
613	/* count the number of addrinfo elements for sorting. */
614	for (n = 0, ai = sentinel->ai_next; ai != NULL; ai = ai->ai_next, n++)
615		;
616
617	/*
618	 * If the number is small enough, we can skip the reordering process.
619	 */
620	if (n <= 1)
621		return(n);
622
623	/* allocate a temporary array for sort and initialization of it. */
624	if ((aio = malloc(sizeof(*aio) * n)) == NULL)
625		return(n);	/* give up reordering */
626	memset(aio, 0, sizeof(*aio) * n);
627
628	/* retrieve address selection policy from the kernel */
629	TAILQ_INIT(&policyhead);
630	if (!get_addrselectpolicy(&policyhead)) {
631		/* no policy is installed into kernel, we don't sort. */
632		free(aio);
633		return (n);
634	}
635
636	for (i = 0, ai = sentinel->ai_next; i < n; ai = ai->ai_next, i++) {
637		aio[i].aio_ai = ai;
638		aio[i].aio_dstscope = gai_addr2scopetype(ai->ai_addr);
639		aio[i].aio_dstpolicy = match_addrselectpolicy(ai->ai_addr,
640							      &policyhead);
641		set_source(&aio[i], &policyhead);
642	}
643
644	/* perform sorting. */
645	qsort(aio, n, sizeof(*aio), comp_dst);
646
647	/* reorder the addrinfo chain. */
648	for (i = 0, aip = &sentinel->ai_next; i < n; i++) {
649		*aip = aio[i].aio_ai;
650		aip = &aio[i].aio_ai->ai_next;
651	}
652	*aip = NULL;
653
654	/* cleanup and return */
655	free(aio);
656	free_addrselectpolicy(&policyhead);
657	return(n);
658}
659
660static int
661get_addrselectpolicy(struct policyhead *head)
662{
663#ifdef INET6
664	int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_ADDRCTLPOLICY };
665	size_t l;
666	char *buf;
667	struct in6_addrpolicy *pol, *ep;
668
669	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0)
670		return (0);
671	if ((buf = malloc(l)) == NULL)
672		return (0);
673	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
674		free(buf);
675		return (0);
676	}
677
678	ep = (struct in6_addrpolicy *)(buf + l);
679	for (pol = (struct in6_addrpolicy *)buf; pol + 1 <= ep; pol++) {
680		struct policyqueue *new;
681
682		if ((new = malloc(sizeof(*new))) == NULL) {
683			free_addrselectpolicy(head); /* make the list empty */
684			break;
685		}
686		new->pc_policy = *pol;
687		TAILQ_INSERT_TAIL(head, new, pc_entry);
688	}
689
690	free(buf);
691	return (1);
692#else
693	return (0);
694#endif
695}
696
697static void
698free_addrselectpolicy(struct policyhead *head)
699{
700	struct policyqueue *ent, *nent;
701
702	for (ent = TAILQ_FIRST(head); ent; ent = nent) {
703		nent = TAILQ_NEXT(ent, pc_entry);
704		TAILQ_REMOVE(head, ent, pc_entry);
705		free(ent);
706	}
707}
708
709static struct policyqueue *
710match_addrselectpolicy(struct sockaddr *addr, struct policyhead *head)
711{
712#ifdef INET6
713	struct policyqueue *ent, *bestent = NULL;
714	struct in6_addrpolicy *pol;
715	int matchlen, bestmatchlen = -1;
716	u_char *mp, *ep, *k, *p, m;
717	struct sockaddr_in6 key;
718
719	switch(addr->sa_family) {
720	case AF_INET6:
721		key = *(struct sockaddr_in6 *)addr;
722		break;
723	case AF_INET:
724		/* convert the address into IPv4-mapped IPv6 address. */
725		memset(&key, 0, sizeof(key));
726		key.sin6_family = AF_INET6;
727		key.sin6_len = sizeof(key);
728		key.sin6_addr.s6_addr[10] = 0xff;
729		key.sin6_addr.s6_addr[11] = 0xff;
730		memcpy(&key.sin6_addr.s6_addr[12],
731		       &((struct sockaddr_in *)addr)->sin_addr, 4);
732		break;
733	default:
734		return(NULL);
735	}
736
737	for (ent = TAILQ_FIRST(head); ent; ent = TAILQ_NEXT(ent, pc_entry)) {
738		pol = &ent->pc_policy;
739		matchlen = 0;
740
741		mp = (u_char *)&pol->addrmask.sin6_addr;
742		ep = mp + 16;	/* XXX: scope field? */
743		k = (u_char *)&key.sin6_addr;
744		p = (u_char *)&pol->addr.sin6_addr;
745		for (; mp < ep && *mp; mp++, k++, p++) {
746			m = *mp;
747			if ((*k & m) != *p)
748				goto next; /* not match */
749			if (m == 0xff) /* short cut for a typical case */
750				matchlen += 8;
751			else {
752				while (m >= 0x80) {
753					matchlen++;
754					m <<= 1;
755				}
756			}
757		}
758
759		/* matched.  check if this is better than the current best. */
760		if (matchlen > bestmatchlen) {
761			bestent = ent;
762			bestmatchlen = matchlen;
763		}
764
765	  next:
766		continue;
767	}
768
769	return(bestent);
770#else
771	return(NULL);
772#endif
773
774}
775
776static void
777set_source(struct ai_order *aio, struct policyhead *ph)
778{
779	struct addrinfo ai = *aio->aio_ai;
780	struct sockaddr_storage ss;
781	socklen_t srclen;
782	int s;
783
784	/* set unspec ("no source is available"), just in case */
785	aio->aio_srcsa.sa_family = AF_UNSPEC;
786	aio->aio_srcscope = -1;
787
788	switch(ai.ai_family) {
789	case AF_INET:
790#ifdef INET6
791	case AF_INET6:
792#endif
793		break;
794	default:		/* ignore unsupported AFs explicitly */
795		return;
796	}
797
798	/* XXX: make a dummy addrinfo to call connect() */
799	ai.ai_socktype = SOCK_DGRAM;
800	ai.ai_protocol = IPPROTO_UDP; /* is UDP too specific? */
801	ai.ai_next = NULL;
802	memset(&ss, 0, sizeof(ss));
803	memcpy(&ss, ai.ai_addr, ai.ai_addrlen);
804	ai.ai_addr = (struct sockaddr *)&ss;
805	get_port(&ai, "1", 0);
806
807	/* open a socket to get the source address for the given dst */
808	if ((s = _socket(ai.ai_family, ai.ai_socktype, ai.ai_protocol)) < 0)
809		return;		/* give up */
810	if (_connect(s, ai.ai_addr, ai.ai_addrlen) < 0)
811		goto cleanup;
812	srclen = ai.ai_addrlen;
813	if (_getsockname(s, &aio->aio_srcsa, &srclen) < 0) {
814		aio->aio_srcsa.sa_family = AF_UNSPEC;
815		goto cleanup;
816	}
817	aio->aio_srcscope = gai_addr2scopetype(&aio->aio_srcsa);
818	aio->aio_srcpolicy = match_addrselectpolicy(&aio->aio_srcsa, ph);
819	aio->aio_matchlen = matchlen(&aio->aio_srcsa, aio->aio_ai->ai_addr);
820#ifdef INET6
821	if (ai.ai_family == AF_INET6) {
822		struct in6_ifreq ifr6;
823		u_int32_t flags6;
824
825		/* XXX: interface name should not be hardcoded */
826		strncpy(ifr6.ifr_name, "lo0", sizeof(ifr6.ifr_name));
827		memset(&ifr6, 0, sizeof(ifr6));
828		memcpy(&ifr6.ifr_addr, ai.ai_addr, ai.ai_addrlen);
829		if (_ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == 0) {
830			flags6 = ifr6.ifr_ifru.ifru_flags6;
831			if ((flags6 & IN6_IFF_DEPRECATED))
832				aio->aio_srcflag |= AIO_SRCFLAG_DEPRECATED;
833		}
834	}
835#endif
836
837  cleanup:
838	_close(s);
839	return;
840}
841
842static int
843matchlen(struct sockaddr *src, struct sockaddr *dst)
844{
845	int match = 0;
846	u_char *s, *d;
847	u_char *lim, r;
848	int addrlen;
849
850	switch (src->sa_family) {
851#ifdef INET6
852	case AF_INET6:
853		s = (u_char *)&((struct sockaddr_in6 *)src)->sin6_addr;
854		d = (u_char *)&((struct sockaddr_in6 *)dst)->sin6_addr;
855		addrlen = sizeof(struct in6_addr);
856		lim = s + addrlen;
857		break;
858#endif
859	case AF_INET:
860		s = (u_char *)&((struct sockaddr_in *)src)->sin_addr;
861		d = (u_char *)&((struct sockaddr_in *)dst)->sin_addr;
862		addrlen = sizeof(struct in_addr);
863		lim = s + addrlen;
864		break;
865	default:
866		return(0);
867	}
868
869	while (s < lim)
870		if ((r = (*d++ ^ *s++)) != 0) {
871			while (r < addrlen * 8) {
872				match++;
873				r <<= 1;
874			}
875			break;
876		} else
877			match += 8;
878	return(match);
879}
880
881static int
882comp_dst(const void *arg1, const void *arg2)
883{
884	const struct ai_order *dst1 = arg1, *dst2 = arg2;
885
886	/*
887	 * Rule 1: Avoid unusable destinations.
888	 * XXX: we currently do not consider if an appropriate route exists.
889	 */
890	if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
891	    dst2->aio_srcsa.sa_family == AF_UNSPEC) {
892		return(-1);
893	}
894	if (dst1->aio_srcsa.sa_family == AF_UNSPEC &&
895	    dst2->aio_srcsa.sa_family != AF_UNSPEC) {
896		return(1);
897	}
898
899	/* Rule 2: Prefer matching scope. */
900	if (dst1->aio_dstscope == dst1->aio_srcscope &&
901	    dst2->aio_dstscope != dst2->aio_srcscope) {
902		return(-1);
903	}
904	if (dst1->aio_dstscope != dst1->aio_srcscope &&
905	    dst2->aio_dstscope == dst2->aio_srcscope) {
906		return(1);
907	}
908
909	/* Rule 3: Avoid deprecated addresses. */
910	if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
911	    dst2->aio_srcsa.sa_family != AF_UNSPEC) {
912		if (!(dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
913		    (dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
914			return(-1);
915		}
916		if ((dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
917		    !(dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
918			return(1);
919		}
920	}
921
922	/* Rule 4: Prefer home addresses. */
923	/* XXX: not implemented yet */
924
925	/* Rule 5: Prefer matching label. */
926#ifdef INET6
927	if (dst1->aio_srcpolicy && dst1->aio_dstpolicy &&
928	    dst1->aio_srcpolicy->pc_policy.label ==
929	    dst1->aio_dstpolicy->pc_policy.label &&
930	    (dst2->aio_srcpolicy == NULL || dst2->aio_dstpolicy == NULL ||
931	     dst2->aio_srcpolicy->pc_policy.label !=
932	     dst2->aio_dstpolicy->pc_policy.label)) {
933		return(-1);
934	}
935	if (dst2->aio_srcpolicy && dst2->aio_dstpolicy &&
936	    dst2->aio_srcpolicy->pc_policy.label ==
937	    dst2->aio_dstpolicy->pc_policy.label &&
938	    (dst1->aio_srcpolicy == NULL || dst1->aio_dstpolicy == NULL ||
939	     dst1->aio_srcpolicy->pc_policy.label !=
940	     dst1->aio_dstpolicy->pc_policy.label)) {
941		return(1);
942	}
943#endif
944
945	/* Rule 6: Prefer higher precedence. */
946#ifdef INET6
947	if (dst1->aio_dstpolicy &&
948	    (dst2->aio_dstpolicy == NULL ||
949	     dst1->aio_dstpolicy->pc_policy.preced >
950	     dst2->aio_dstpolicy->pc_policy.preced)) {
951		return(-1);
952	}
953	if (dst2->aio_dstpolicy &&
954	    (dst1->aio_dstpolicy == NULL ||
955	     dst2->aio_dstpolicy->pc_policy.preced >
956	     dst1->aio_dstpolicy->pc_policy.preced)) {
957		return(1);
958	}
959#endif
960
961	/* Rule 7: Prefer native transport. */
962	/* XXX: not implemented yet */
963
964	/* Rule 8: Prefer smaller scope. */
965	if (dst1->aio_dstscope >= 0 &&
966	    dst1->aio_dstscope < dst2->aio_dstscope) {
967		return(-1);
968	}
969	if (dst2->aio_dstscope >= 0 &&
970	    dst2->aio_dstscope < dst1->aio_dstscope) {
971		return(1);
972	}
973
974	/*
975	 * Rule 9: Use longest matching prefix.
976	 * We compare the match length in a same AF only.
977	 */
978	if (dst1->aio_ai->ai_addr->sa_family ==
979	    dst2->aio_ai->ai_addr->sa_family) {
980		if (dst1->aio_matchlen > dst2->aio_matchlen) {
981			return(-1);
982		}
983		if (dst1->aio_matchlen < dst2->aio_matchlen) {
984			return(1);
985		}
986	}
987
988	/* Rule 10: Otherwise, leave the order unchanged. */
989	return(-1);
990}
991
992/*
993 * Copy from scope.c.
994 * XXX: we should standardize the functions and link them as standard
995 * library.
996 */
997static int
998gai_addr2scopetype(struct sockaddr *sa)
999{
1000#ifdef INET6
1001	struct sockaddr_in6 *sa6;
1002#endif
1003	struct sockaddr_in *sa4;
1004
1005	switch(sa->sa_family) {
1006#ifdef INET6
1007	case AF_INET6:
1008		sa6 = (struct sockaddr_in6 *)sa;
1009		if (IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
1010			/* just use the scope field of the multicast address */
1011			return(sa6->sin6_addr.s6_addr[2] & 0x0f);
1012		}
1013		/*
1014		 * Unicast addresses: map scope type to corresponding scope
1015		 * value defined for multcast addresses.
1016		 * XXX: hardcoded scope type values are bad...
1017		 */
1018		if (IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr))
1019			return(1); /* node local scope */
1020		if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr))
1021			return(2); /* link-local scope */
1022		if (IN6_IS_ADDR_SITELOCAL(&sa6->sin6_addr))
1023			return(5); /* site-local scope */
1024		return(14);	/* global scope */
1025		break;
1026#endif
1027	case AF_INET:
1028		/*
1029		 * IPv4 pseudo scoping according to RFC 3484.
1030		 */
1031		sa4 = (struct sockaddr_in *)sa;
1032		/* IPv4 autoconfiguration addresses have link-local scope. */
1033		if (((u_char *)&sa4->sin_addr)[0] == 169 &&
1034		    ((u_char *)&sa4->sin_addr)[1] == 254)
1035			return(2);
1036		/* Private addresses have site-local scope. */
1037		if (((u_char *)&sa4->sin_addr)[0] == 10 ||
1038		    (((u_char *)&sa4->sin_addr)[0] == 172 &&
1039		     (((u_char *)&sa4->sin_addr)[1] & 0xf0) == 16) ||
1040		    (((u_char *)&sa4->sin_addr)[0] == 192 &&
1041		     ((u_char *)&sa4->sin_addr)[1] == 168))
1042			return(14);	/* XXX: It should be 5 unless NAT */
1043		/* Loopback addresses have link-local scope. */
1044		if (((u_char *)&sa4->sin_addr)[0] == 127)
1045			return(2);
1046		return(14);
1047		break;
1048	default:
1049		errno = EAFNOSUPPORT; /* is this a good error? */
1050		return(-1);
1051	}
1052}
1053
1054/*
1055 * hostname == NULL.
1056 * passive socket -> anyaddr (0.0.0.0 or ::)
1057 * non-passive socket -> localhost (127.0.0.1 or ::1)
1058 */
1059static int
1060explore_null(const struct addrinfo *pai, const char *servname,
1061    struct addrinfo **res)
1062{
1063	int s;
1064	const struct afd *afd;
1065	struct addrinfo *cur;
1066	struct addrinfo sentinel;
1067	int error;
1068
1069	*res = NULL;
1070	sentinel.ai_next = NULL;
1071	cur = &sentinel;
1072
1073	/*
1074	 * filter out AFs that are not supported by the kernel
1075	 * XXX errno?
1076	 */
1077	s = _socket(pai->ai_family, SOCK_DGRAM, 0);
1078	if (s < 0) {
1079		if (errno != EMFILE)
1080			return 0;
1081	} else
1082		_close(s);
1083
1084	/*
1085	 * if the servname does not match socktype/protocol, ignore it.
1086	 */
1087	if (get_portmatch(pai, servname) != 0)
1088		return 0;
1089
1090	afd = find_afd(pai->ai_family);
1091	if (afd == NULL)
1092		return 0;
1093
1094	if (pai->ai_flags & AI_PASSIVE) {
1095		GET_AI(cur->ai_next, afd, afd->a_addrany);
1096		/* xxx meaningless?
1097		 * GET_CANONNAME(cur->ai_next, "anyaddr");
1098		 */
1099		GET_PORT(cur->ai_next, servname);
1100	} else {
1101		GET_AI(cur->ai_next, afd, afd->a_loopback);
1102		/* xxx meaningless?
1103		 * GET_CANONNAME(cur->ai_next, "localhost");
1104		 */
1105		GET_PORT(cur->ai_next, servname);
1106	}
1107	cur = cur->ai_next;
1108
1109	*res = sentinel.ai_next;
1110	return 0;
1111
1112free:
1113	if (sentinel.ai_next)
1114		freeaddrinfo(sentinel.ai_next);
1115	return error;
1116}
1117
1118/*
1119 * numeric hostname
1120 */
1121static int
1122explore_numeric(const struct addrinfo *pai, const char *hostname,
1123    const char *servname, struct addrinfo **res, const char *canonname)
1124{
1125	const struct afd *afd;
1126	struct addrinfo *cur;
1127	struct addrinfo sentinel;
1128	int error;
1129	char pton[PTON_MAX];
1130
1131	*res = NULL;
1132	sentinel.ai_next = NULL;
1133	cur = &sentinel;
1134
1135	/*
1136	 * if the servname does not match socktype/protocol, ignore it.
1137	 */
1138	if (get_portmatch(pai, servname) != 0)
1139		return 0;
1140
1141	afd = find_afd(pai->ai_family);
1142	if (afd == NULL)
1143		return 0;
1144
1145	switch (afd->a_af) {
1146#if 1 /*X/Open spec*/
1147	case AF_INET:
1148		if (inet_aton(hostname, (struct in_addr *)pton) == 1) {
1149			if (pai->ai_family == afd->a_af ||
1150			    pai->ai_family == PF_UNSPEC /*?*/) {
1151				GET_AI(cur->ai_next, afd, pton);
1152				GET_PORT(cur->ai_next, servname);
1153				if ((pai->ai_flags & AI_CANONNAME)) {
1154					/*
1155					 * Set the numeric address itself as
1156					 * the canonical name, based on a
1157					 * clarification in rfc3493.
1158					 */
1159					GET_CANONNAME(cur->ai_next, canonname);
1160				}
1161				while (cur && cur->ai_next)
1162					cur = cur->ai_next;
1163			} else
1164				ERR(EAI_FAMILY);	/*xxx*/
1165		}
1166		break;
1167#endif
1168	default:
1169		if (inet_pton(afd->a_af, hostname, pton) == 1) {
1170			if (pai->ai_family == afd->a_af ||
1171			    pai->ai_family == PF_UNSPEC /*?*/) {
1172				GET_AI(cur->ai_next, afd, pton);
1173				GET_PORT(cur->ai_next, servname);
1174				if ((pai->ai_flags & AI_CANONNAME)) {
1175					/*
1176					 * Set the numeric address itself as
1177					 * the canonical name, based on a
1178					 * clarification in rfc3493.
1179					 */
1180					GET_CANONNAME(cur->ai_next, canonname);
1181				}
1182				while (cur && cur->ai_next)
1183					cur = cur->ai_next;
1184			} else
1185				ERR(EAI_FAMILY);	/* XXX */
1186		}
1187		break;
1188	}
1189
1190	*res = sentinel.ai_next;
1191	return 0;
1192
1193free:
1194bad:
1195	if (sentinel.ai_next)
1196		freeaddrinfo(sentinel.ai_next);
1197	return error;
1198}
1199
1200/*
1201 * numeric hostname with scope
1202 */
1203static int
1204explore_numeric_scope(const struct addrinfo *pai, const char *hostname,
1205    const char *servname, struct addrinfo **res)
1206{
1207#if !defined(SCOPE_DELIMITER) || !defined(INET6)
1208	return explore_numeric(pai, hostname, servname, res, hostname);
1209#else
1210	const struct afd *afd;
1211	struct addrinfo *cur;
1212	int error;
1213	char *cp, *hostname2 = NULL, *scope, *addr;
1214	struct sockaddr_in6 *sin6;
1215
1216	/*
1217	 * if the servname does not match socktype/protocol, ignore it.
1218	 */
1219	if (get_portmatch(pai, servname) != 0)
1220		return 0;
1221
1222	afd = find_afd(pai->ai_family);
1223	if (afd == NULL)
1224		return 0;
1225
1226	if (!afd->a_scoped)
1227		return explore_numeric(pai, hostname, servname, res, hostname);
1228
1229	cp = strchr(hostname, SCOPE_DELIMITER);
1230	if (cp == NULL)
1231		return explore_numeric(pai, hostname, servname, res, hostname);
1232
1233	/*
1234	 * Handle special case of <scoped_address><delimiter><scope id>
1235	 */
1236	hostname2 = strdup(hostname);
1237	if (hostname2 == NULL)
1238		return EAI_MEMORY;
1239	/* terminate at the delimiter */
1240	hostname2[cp - hostname] = '\0';
1241	addr = hostname2;
1242	scope = cp + 1;
1243
1244	error = explore_numeric(pai, addr, servname, res, hostname);
1245	if (error == 0) {
1246		u_int32_t scopeid;
1247
1248		for (cur = *res; cur; cur = cur->ai_next) {
1249			if (cur->ai_family != AF_INET6)
1250				continue;
1251			sin6 = (struct sockaddr_in6 *)(void *)cur->ai_addr;
1252			if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
1253				free(hostname2);
1254				return(EAI_NONAME); /* XXX: is return OK? */
1255			}
1256			sin6->sin6_scope_id = scopeid;
1257		}
1258	}
1259
1260	free(hostname2);
1261
1262	return error;
1263#endif
1264}
1265
1266static int
1267get_canonname(const struct addrinfo *pai, struct addrinfo *ai, const char *str)
1268{
1269	if ((pai->ai_flags & AI_CANONNAME) != 0) {
1270		ai->ai_canonname = strdup(str);
1271		if (ai->ai_canonname == NULL)
1272			return EAI_MEMORY;
1273	}
1274	return 0;
1275}
1276
1277static struct addrinfo *
1278get_ai(const struct addrinfo *pai, const struct afd *afd, const char *addr)
1279{
1280	char *p;
1281	struct addrinfo *ai;
1282#ifdef FAITH
1283	struct in6_addr faith_prefix;
1284	char *fp_str;
1285	int translate = 0;
1286#endif
1287
1288#ifdef FAITH
1289	/*
1290	 * Transfrom an IPv4 addr into a special IPv6 addr format for
1291	 * IPv6->IPv4 translation gateway. (only TCP is supported now)
1292	 *
1293	 * +-----------------------------------+------------+
1294	 * | faith prefix part (12 bytes)      | embedded   |
1295	 * |                                   | IPv4 addr part (4 bytes)
1296	 * +-----------------------------------+------------+
1297	 *
1298	 * faith prefix part is specified as ascii IPv6 addr format
1299	 * in environmental variable GAI.
1300	 * For FAITH to work correctly, routing to faith prefix must be
1301	 * setup toward a machine where a FAITH daemon operates.
1302	 * Also, the machine must enable some mechanizm
1303	 * (e.g. faith interface hack) to divert those packet with
1304	 * faith prefixed destination addr to user-land FAITH daemon.
1305	 */
1306	fp_str = getenv("GAI");
1307	if (fp_str && inet_pton(AF_INET6, fp_str, &faith_prefix) == 1 &&
1308	    afd->a_af == AF_INET && pai->ai_socktype == SOCK_STREAM) {
1309		u_int32_t v4a;
1310		u_int8_t v4a_top;
1311
1312		memcpy(&v4a, addr, sizeof v4a);
1313		v4a_top = v4a >> IN_CLASSA_NSHIFT;
1314		if (!IN_MULTICAST(v4a) && !IN_EXPERIMENTAL(v4a) &&
1315		    v4a_top != 0 && v4a != IN_LOOPBACKNET) {
1316			afd = &afdl[N_INET6];
1317			memcpy(&faith_prefix.s6_addr[12], addr,
1318			       sizeof(struct in_addr));
1319			translate = 1;
1320		}
1321	}
1322#endif
1323
1324	ai = (struct addrinfo *)malloc(sizeof(struct addrinfo)
1325		+ (afd->a_socklen));
1326	if (ai == NULL)
1327		return NULL;
1328
1329	memcpy(ai, pai, sizeof(struct addrinfo));
1330	ai->ai_addr = (struct sockaddr *)(void *)(ai + 1);
1331	memset(ai->ai_addr, 0, (size_t)afd->a_socklen);
1332	ai->ai_addr->sa_len = afd->a_socklen;
1333	ai->ai_addrlen = afd->a_socklen;
1334	ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
1335	p = (char *)(void *)(ai->ai_addr);
1336#ifdef FAITH
1337	if (translate == 1)
1338		memcpy(p + afd->a_off, &faith_prefix, (size_t)afd->a_addrlen);
1339	else
1340#endif
1341	memcpy(p + afd->a_off, addr, (size_t)afd->a_addrlen);
1342	return ai;
1343}
1344
1345static int
1346get_portmatch(const struct addrinfo *ai, const char *servname)
1347{
1348
1349	/* get_port does not touch first argument when matchonly == 1. */
1350	/* LINTED const cast */
1351	return get_port((struct addrinfo *)ai, servname, 1);
1352}
1353
1354static int
1355get_port(struct addrinfo *ai, const char *servname, int matchonly)
1356{
1357	const char *proto;
1358	struct servent *sp;
1359	int port;
1360	int allownumeric;
1361
1362	if (servname == NULL)
1363		return 0;
1364	switch (ai->ai_family) {
1365	case AF_INET:
1366#ifdef AF_INET6
1367	case AF_INET6:
1368#endif
1369		break;
1370	default:
1371		return 0;
1372	}
1373
1374	switch (ai->ai_socktype) {
1375	case SOCK_RAW:
1376		return EAI_SERVICE;
1377	case SOCK_DGRAM:
1378	case SOCK_STREAM:
1379		allownumeric = 1;
1380		break;
1381	case ANY:
1382		allownumeric = 0;
1383		break;
1384	default:
1385		return EAI_SOCKTYPE;
1386	}
1387
1388	port = str2number(servname);
1389	if (port >= 0) {
1390		if (!allownumeric)
1391			return EAI_SERVICE;
1392		if (port < 0 || port > 65535)
1393			return EAI_SERVICE;
1394		port = htons(port);
1395	} else {
1396		if (ai->ai_flags & AI_NUMERICSERV)
1397			return EAI_NONAME;
1398		switch (ai->ai_socktype) {
1399		case SOCK_DGRAM:
1400			proto = "udp";
1401			break;
1402		case SOCK_STREAM:
1403			proto = "tcp";
1404			break;
1405		default:
1406			proto = NULL;
1407			break;
1408		}
1409
1410		if ((sp = getservbyname(servname, proto)) == NULL)
1411			return EAI_SERVICE;
1412		port = sp->s_port;
1413	}
1414
1415	if (!matchonly) {
1416		switch (ai->ai_family) {
1417		case AF_INET:
1418			((struct sockaddr_in *)(void *)
1419			    ai->ai_addr)->sin_port = port;
1420			break;
1421#ifdef INET6
1422		case AF_INET6:
1423			((struct sockaddr_in6 *)(void *)
1424			    ai->ai_addr)->sin6_port = port;
1425			break;
1426#endif
1427		}
1428	}
1429
1430	return 0;
1431}
1432
1433static const struct afd *
1434find_afd(int af)
1435{
1436	const struct afd *afd;
1437
1438	if (af == PF_UNSPEC)
1439		return NULL;
1440	for (afd = afdl; afd->a_af; afd++) {
1441		if (afd->a_af == af)
1442			return afd;
1443	}
1444	return NULL;
1445}
1446
1447/*
1448 * post-2553: AI_ADDRCONFIG check.  if we use getipnodeby* as backend, backend
1449 * will take care of it.
1450 * the semantics of AI_ADDRCONFIG is not defined well.  we are not sure
1451 * if the code is right or not.
1452 *
1453 * XXX PF_UNSPEC -> PF_INET6 + PF_INET mapping needs to be in sync with
1454 * _dns_getaddrinfo.
1455 */
1456static int
1457addrconfig(struct addrinfo *pai)
1458{
1459	int s, af;
1460
1461	/*
1462	 * TODO:
1463	 * Note that implementation dependent test for address
1464	 * configuration should be done everytime called
1465	 * (or apropriate interval),
1466	 * because addresses will be dynamically assigned or deleted.
1467	 */
1468	af = pai->ai_family;
1469	if (af == AF_UNSPEC) {
1470		if ((s = _socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1471			af = AF_INET;
1472		else {
1473			_close(s);
1474			if ((s = _socket(AF_INET, SOCK_DGRAM, 0)) < 0)
1475				af = AF_INET6;
1476			else
1477				_close(s);
1478		}
1479	}
1480	if (af != AF_UNSPEC) {
1481		if ((s = _socket(af, SOCK_DGRAM, 0)) < 0)
1482			return 0;
1483		_close(s);
1484	}
1485	pai->ai_family = af;
1486	return 1;
1487}
1488
1489#ifdef INET6
1490/* convert a string to a scope identifier. XXX: IPv6 specific */
1491static int
1492ip6_str2scopeid(char *scope, struct sockaddr_in6 *sin6, u_int32_t *scopeid)
1493{
1494	u_long lscopeid;
1495	struct in6_addr *a6;
1496	char *ep;
1497
1498	a6 = &sin6->sin6_addr;
1499
1500	/* empty scopeid portion is invalid */
1501	if (*scope == '\0')
1502		return -1;
1503
1504	if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
1505		/*
1506		 * We currently assume a one-to-one mapping between links
1507		 * and interfaces, so we simply use interface indices for
1508		 * like-local scopes.
1509		 */
1510		*scopeid = if_nametoindex(scope);
1511		if (*scopeid == 0)
1512			goto trynumeric;
1513		return 0;
1514	}
1515
1516	/* still unclear about literal, allow numeric only - placeholder */
1517	if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6))
1518		goto trynumeric;
1519	if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
1520		goto trynumeric;
1521	else
1522		goto trynumeric;	/* global */
1523
1524	/* try to convert to a numeric id as a last resort */
1525  trynumeric:
1526	errno = 0;
1527	lscopeid = strtoul(scope, &ep, 10);
1528	*scopeid = (u_int32_t)(lscopeid & 0xffffffffUL);
1529	if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
1530		return 0;
1531	else
1532		return -1;
1533}
1534#endif
1535
1536
1537#ifdef NS_CACHING
1538static int
1539addrinfo_id_func(char *buffer, size_t *buffer_size, va_list ap,
1540    void *cache_mdata)
1541{
1542	res_state statp;
1543	u_long res_options;
1544
1545	const int op_id = 0;	/* identifies the getaddrinfo for the cache */
1546	char *hostname;
1547	struct addrinfo *hints;
1548
1549	char *p;
1550	int ai_flags, ai_family, ai_socktype, ai_protocol;
1551	size_t desired_size, size;
1552
1553	statp = __res_state();
1554	res_options = statp->options & (RES_RECURSE | RES_DEFNAMES |
1555	    RES_DNSRCH | RES_NOALIASES | RES_USE_INET6);
1556
1557	hostname = va_arg(ap, char *);
1558	hints = va_arg(ap, struct addrinfo *);
1559
1560	desired_size = sizeof(res_options) + sizeof(int) + sizeof(int) * 4;
1561	if (hostname != NULL) {
1562		size = strlen(hostname);
1563		desired_size += size + 1;
1564	} else
1565		size = 0;
1566
1567	if (desired_size > *buffer_size) {
1568		*buffer_size = desired_size;
1569		return (NS_RETURN);
1570	}
1571
1572	if (hints == NULL)
1573		ai_flags = ai_family = ai_socktype = ai_protocol = 0;
1574	else {
1575		ai_flags = hints->ai_flags;
1576		ai_family = hints->ai_family;
1577		ai_socktype = hints->ai_socktype;
1578		ai_protocol = hints->ai_protocol;
1579	}
1580
1581	p = buffer;
1582	memcpy(p, &res_options, sizeof(res_options));
1583	p += sizeof(res_options);
1584
1585	memcpy(p, &op_id, sizeof(int));
1586	p += sizeof(int);
1587
1588	memcpy(p, &ai_flags, sizeof(int));
1589	p += sizeof(int);
1590
1591	memcpy(p, &ai_family, sizeof(int));
1592	p += sizeof(int);
1593
1594	memcpy(p, &ai_socktype, sizeof(int));
1595	p += sizeof(int);
1596
1597	memcpy(p, &ai_protocol, sizeof(int));
1598	p += sizeof(int);
1599
1600	if (hostname != NULL)
1601		memcpy(p, hostname, size);
1602
1603	*buffer_size = desired_size;
1604	return (NS_SUCCESS);
1605}
1606
1607static int
1608addrinfo_marshal_func(char *buffer, size_t *buffer_size, void *retval,
1609    va_list ap, void *cache_mdata)
1610{
1611	struct addrinfo	*ai, *cai;
1612	char *p;
1613	size_t desired_size, size, ai_size;
1614
1615	ai = *((struct addrinfo **)retval);
1616
1617	desired_size = sizeof(size_t);
1618	ai_size = 0;
1619	for (cai = ai; cai != NULL; cai = cai->ai_next) {
1620		desired_size += sizeof(struct addrinfo) + cai->ai_addrlen;
1621		if (cai->ai_canonname != NULL)
1622			desired_size += sizeof(size_t) +
1623			    strlen(cai->ai_canonname);
1624		++ai_size;
1625	}
1626
1627	if (desired_size > *buffer_size) {
1628		/* this assignment is here for future use */
1629		errno = ERANGE;
1630		*buffer_size = desired_size;
1631		return (NS_RETURN);
1632	}
1633
1634	memset(buffer, 0, desired_size);
1635	p = buffer;
1636
1637	memcpy(p, &ai_size, sizeof(size_t));
1638	p += sizeof(size_t);
1639	for (cai = ai; cai != NULL; cai = cai->ai_next) {
1640		memcpy(p, cai, sizeof(struct addrinfo));
1641		p += sizeof(struct addrinfo);
1642
1643		memcpy(p, cai->ai_addr, cai->ai_addrlen);
1644		p += cai->ai_addrlen;
1645
1646		if (cai->ai_canonname != NULL) {
1647			size = strlen(cai->ai_canonname);
1648			memcpy(p, &size, sizeof(size_t));
1649			p += sizeof(size_t);
1650
1651			memcpy(p, cai->ai_canonname, size);
1652			p += size;
1653		}
1654	}
1655
1656	return (NS_SUCCESS);
1657}
1658
1659static int
1660addrinfo_unmarshal_func(char *buffer, size_t buffer_size, void *retval,
1661    va_list ap, void *cache_mdata)
1662{
1663	struct addrinfo	new_ai, *result, *sentinel, *lasts;
1664
1665	char *p;
1666	size_t ai_size, ai_i, size;
1667
1668	p = buffer;
1669	memcpy(&ai_size, p, sizeof(size_t));
1670	p += sizeof(size_t);
1671
1672	result = NULL;
1673	lasts = NULL;
1674	for (ai_i = 0; ai_i < ai_size; ++ai_i) {
1675		memcpy(&new_ai, p, sizeof(struct addrinfo));
1676		p += sizeof(struct addrinfo);
1677		size = new_ai.ai_addrlen + sizeof(struct addrinfo) +
1678			_ALIGNBYTES;
1679
1680		sentinel = (struct addrinfo *)malloc(size);
1681		memset(sentinel, 0, size);
1682
1683		memcpy(sentinel, &new_ai, sizeof(struct addrinfo));
1684		sentinel->ai_addr = (struct sockaddr *)_ALIGN((char *)sentinel +
1685		    sizeof(struct addrinfo));
1686
1687		memcpy(sentinel->ai_addr, p, new_ai.ai_addrlen);
1688		p += new_ai.ai_addrlen;
1689
1690		if (new_ai.ai_canonname != NULL) {
1691			memcpy(&size, p, sizeof(size_t));
1692			p += sizeof(size_t);
1693
1694			sentinel->ai_canonname = (char *)malloc(size + 1);
1695			memset(sentinel->ai_canonname, 0, size + 1);
1696
1697			memcpy(sentinel->ai_canonname, p, size);
1698			p += size;
1699		}
1700
1701		if (result == NULL) {
1702			result = sentinel;
1703			lasts = sentinel;
1704		} else {
1705			lasts->ai_next = sentinel;
1706			lasts = sentinel;
1707		}
1708	}
1709
1710	*((struct addrinfo **)retval) = result;
1711	return (NS_SUCCESS);
1712}
1713#endif /* NS_CACHING */
1714
1715/*
1716 * FQDN hostname, DNS lookup
1717 */
1718static int
1719explore_fqdn(const struct addrinfo *pai, const char *hostname,
1720    const char *servname, struct addrinfo **res)
1721{
1722	struct addrinfo *result;
1723	struct addrinfo *cur;
1724	int error = 0;
1725
1726#ifdef NS_CACHING
1727	static const nss_cache_info cache_info =
1728	NS_COMMON_CACHE_INFO_INITIALIZER(
1729		hosts, NULL, addrinfo_id_func, addrinfo_marshal_func,
1730		addrinfo_unmarshal_func);
1731#endif
1732	static const ns_dtab dtab[] = {
1733		NS_FILES_CB(_files_getaddrinfo, NULL)
1734		{ NSSRC_DNS, _dns_getaddrinfo, NULL },	/* force -DHESIOD */
1735		NS_NIS_CB(_yp_getaddrinfo, NULL)
1736#ifdef NS_CACHING
1737		NS_CACHE_CB(&cache_info)
1738#endif
1739		{ 0 }
1740	};
1741
1742	result = NULL;
1743
1744	/*
1745	 * if the servname does not match socktype/protocol, ignore it.
1746	 */
1747	if (get_portmatch(pai, servname) != 0)
1748		return 0;
1749
1750	switch (_nsdispatch(&result, dtab, NSDB_HOSTS, "getaddrinfo",
1751			default_dns_files, hostname, pai)) {
1752	case NS_TRYAGAIN:
1753		error = EAI_AGAIN;
1754		goto free;
1755	case NS_UNAVAIL:
1756		error = EAI_FAIL;
1757		goto free;
1758	case NS_NOTFOUND:
1759		error = EAI_NONAME;
1760		goto free;
1761	case NS_SUCCESS:
1762		error = 0;
1763		for (cur = result; cur; cur = cur->ai_next) {
1764			GET_PORT(cur, servname);
1765			/* canonname should be filled already */
1766		}
1767		break;
1768	}
1769
1770	*res = result;
1771
1772	return 0;
1773
1774free:
1775	if (result)
1776		freeaddrinfo(result);
1777	return error;
1778}
1779
1780#ifdef DEBUG
1781static const char AskedForGot[] =
1782	"gethostby*.getanswer: asked for \"%s\", got \"%s\"";
1783#endif
1784
1785static struct addrinfo *
1786getanswer(const querybuf *answer, int anslen, const char *qname, int qtype,
1787    const struct addrinfo *pai, res_state res)
1788{
1789	struct addrinfo sentinel, *cur;
1790	struct addrinfo ai;
1791	const struct afd *afd;
1792	char *canonname;
1793	const HEADER *hp;
1794	const u_char *cp;
1795	int n;
1796	const u_char *eom;
1797	char *bp, *ep;
1798	int type, class, ancount, qdcount;
1799	int haveanswer, had_error;
1800	char tbuf[MAXDNAME];
1801	int (*name_ok)(const char *);
1802	char hostbuf[8*1024];
1803
1804	memset(&sentinel, 0, sizeof(sentinel));
1805	cur = &sentinel;
1806
1807	canonname = NULL;
1808	eom = answer->buf + anslen;
1809	switch (qtype) {
1810	case T_A:
1811	case T_AAAA:
1812	case T_ANY:	/*use T_ANY only for T_A/T_AAAA lookup*/
1813		name_ok = res_hnok;
1814		break;
1815	default:
1816		return (NULL);	/* XXX should be abort(); */
1817	}
1818	/*
1819	 * find first satisfactory answer
1820	 */
1821	hp = &answer->hdr;
1822	ancount = ntohs(hp->ancount);
1823	qdcount = ntohs(hp->qdcount);
1824	bp = hostbuf;
1825	ep = hostbuf + sizeof hostbuf;
1826	cp = answer->buf + HFIXEDSZ;
1827	if (qdcount != 1) {
1828		RES_SET_H_ERRNO(res, NO_RECOVERY);
1829		return (NULL);
1830	}
1831	n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1832	if ((n < 0) || !(*name_ok)(bp)) {
1833		RES_SET_H_ERRNO(res, NO_RECOVERY);
1834		return (NULL);
1835	}
1836	cp += n + QFIXEDSZ;
1837	if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
1838		/* res_send() has already verified that the query name is the
1839		 * same as the one we sent; this just gets the expanded name
1840		 * (i.e., with the succeeding search-domain tacked on).
1841		 */
1842		n = strlen(bp) + 1;		/* for the \0 */
1843		if (n >= MAXHOSTNAMELEN) {
1844			RES_SET_H_ERRNO(res, NO_RECOVERY);
1845			return (NULL);
1846		}
1847		canonname = bp;
1848		bp += n;
1849		/* The qname can be abbreviated, but h_name is now absolute. */
1850		qname = canonname;
1851	}
1852	haveanswer = 0;
1853	had_error = 0;
1854	while (ancount-- > 0 && cp < eom && !had_error) {
1855		n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1856		if ((n < 0) || !(*name_ok)(bp)) {
1857			had_error++;
1858			continue;
1859		}
1860		cp += n;			/* name */
1861		type = _getshort(cp);
1862 		cp += INT16SZ;			/* type */
1863		class = _getshort(cp);
1864 		cp += INT16SZ + INT32SZ;	/* class, TTL */
1865		n = _getshort(cp);
1866		cp += INT16SZ;			/* len */
1867		if (class != C_IN) {
1868			/* XXX - debug? syslog? */
1869			cp += n;
1870			continue;		/* XXX - had_error++ ? */
1871		}
1872		if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) &&
1873		    type == T_CNAME) {
1874			n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
1875			if ((n < 0) || !(*name_ok)(tbuf)) {
1876				had_error++;
1877				continue;
1878			}
1879			cp += n;
1880			/* Get canonical name. */
1881			n = strlen(tbuf) + 1;	/* for the \0 */
1882			if (n > ep - bp || n >= MAXHOSTNAMELEN) {
1883				had_error++;
1884				continue;
1885			}
1886			strlcpy(bp, tbuf, ep - bp);
1887			canonname = bp;
1888			bp += n;
1889			continue;
1890		}
1891		if (qtype == T_ANY) {
1892			if (!(type == T_A || type == T_AAAA)) {
1893				cp += n;
1894				continue;
1895			}
1896		} else if (type != qtype) {
1897#ifdef DEBUG
1898			if (type != T_KEY && type != T_SIG)
1899				syslog(LOG_NOTICE|LOG_AUTH,
1900	       "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
1901				       qname, p_class(C_IN), p_type(qtype),
1902				       p_type(type));
1903#endif
1904			cp += n;
1905			continue;		/* XXX - had_error++ ? */
1906		}
1907		switch (type) {
1908		case T_A:
1909		case T_AAAA:
1910			if (strcasecmp(canonname, bp) != 0) {
1911#ifdef DEBUG
1912				syslog(LOG_NOTICE|LOG_AUTH,
1913				       AskedForGot, canonname, bp);
1914#endif
1915				cp += n;
1916				continue;	/* XXX - had_error++ ? */
1917			}
1918			if (type == T_A && n != INADDRSZ) {
1919				cp += n;
1920				continue;
1921			}
1922			if (type == T_AAAA && n != IN6ADDRSZ) {
1923				cp += n;
1924				continue;
1925			}
1926#ifdef FILTER_V4MAPPED
1927			if (type == T_AAAA) {
1928				struct in6_addr in6;
1929				memcpy(&in6, cp, sizeof(in6));
1930				if (IN6_IS_ADDR_V4MAPPED(&in6)) {
1931					cp += n;
1932					continue;
1933				}
1934			}
1935#endif
1936			if (!haveanswer) {
1937				int nn;
1938
1939				canonname = bp;
1940				nn = strlen(bp) + 1;	/* for the \0 */
1941				bp += nn;
1942			}
1943
1944			/* don't overwrite pai */
1945			ai = *pai;
1946			ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
1947			afd = find_afd(ai.ai_family);
1948			if (afd == NULL) {
1949				cp += n;
1950				continue;
1951			}
1952			cur->ai_next = get_ai(&ai, afd, (const char *)cp);
1953			if (cur->ai_next == NULL)
1954				had_error++;
1955			while (cur && cur->ai_next)
1956				cur = cur->ai_next;
1957			cp += n;
1958			break;
1959		default:
1960			abort();
1961		}
1962		if (!had_error)
1963			haveanswer++;
1964	}
1965	if (haveanswer) {
1966#if defined(RESOLVSORT)
1967		/*
1968		 * We support only IPv4 address for backward
1969		 * compatibility against gethostbyname(3).
1970		 */
1971		if (res->nsort && qtype == T_A) {
1972			if (addr4sort(&sentinel, res) < 0) {
1973				freeaddrinfo(sentinel.ai_next);
1974				RES_SET_H_ERRNO(res, NO_RECOVERY);
1975				return NULL;
1976			}
1977		}
1978#endif /*RESOLVSORT*/
1979		if (!canonname)
1980			(void)get_canonname(pai, sentinel.ai_next, qname);
1981		else
1982			(void)get_canonname(pai, sentinel.ai_next, canonname);
1983		RES_SET_H_ERRNO(res, NETDB_SUCCESS);
1984		return sentinel.ai_next;
1985	}
1986
1987	RES_SET_H_ERRNO(res, NO_RECOVERY);
1988	return NULL;
1989}
1990
1991#ifdef RESOLVSORT
1992struct addr_ptr {
1993	struct addrinfo *ai;
1994	int aval;
1995};
1996
1997static int
1998addr4sort(struct addrinfo *sentinel, res_state res)
1999{
2000	struct addrinfo *ai;
2001	struct addr_ptr *addrs, addr;
2002	struct sockaddr_in *sin;
2003	int naddrs, i, j;
2004	int needsort = 0;
2005
2006	if (!sentinel)
2007		return -1;
2008	naddrs = 0;
2009	for (ai = sentinel->ai_next; ai; ai = ai->ai_next)
2010		naddrs++;
2011	if (naddrs < 2)
2012		return 0;		/* We don't need sorting. */
2013	if ((addrs = malloc(sizeof(struct addr_ptr) * naddrs)) == NULL)
2014		return -1;
2015	i = 0;
2016	for (ai = sentinel->ai_next; ai; ai = ai->ai_next) {
2017		sin = (struct sockaddr_in *)ai->ai_addr;
2018		for (j = 0; (unsigned)j < res->nsort; j++) {
2019			if (res->sort_list[j].addr.s_addr ==
2020			    (sin->sin_addr.s_addr & res->sort_list[j].mask))
2021				break;
2022		}
2023		addrs[i].ai = ai;
2024		addrs[i].aval = j;
2025		if (needsort == 0 && i > 0 && j < addrs[i - 1].aval)
2026			needsort = i;
2027		i++;
2028	}
2029	if (!needsort) {
2030		free(addrs);
2031		return 0;
2032	}
2033
2034	while (needsort < naddrs) {
2035		for (j = needsort - 1; j >= 0; j--) {
2036			if (addrs[j].aval > addrs[j+1].aval) {
2037				addr = addrs[j];
2038				addrs[j] = addrs[j + 1];
2039				addrs[j + 1] = addr;
2040			} else
2041				break;
2042		}
2043		needsort++;
2044	}
2045
2046	ai = sentinel;
2047	for (i = 0; i < naddrs; ++i) {
2048		ai->ai_next = addrs[i].ai;
2049		ai = ai->ai_next;
2050	}
2051	ai->ai_next = NULL;
2052	free(addrs);
2053	return 0;
2054}
2055#endif /*RESOLVSORT*/
2056
2057/*ARGSUSED*/
2058static int
2059_dns_getaddrinfo(void *rv, void *cb_data, va_list ap)
2060{
2061	struct addrinfo *ai;
2062	querybuf *buf, *buf2;
2063	const char *hostname;
2064	const struct addrinfo *pai;
2065	struct addrinfo sentinel, *cur;
2066	struct res_target q, q2;
2067	res_state res;
2068
2069	hostname = va_arg(ap, char *);
2070	pai = va_arg(ap, const struct addrinfo *);
2071
2072	memset(&q, 0, sizeof(q));
2073	memset(&q2, 0, sizeof(q2));
2074	memset(&sentinel, 0, sizeof(sentinel));
2075	cur = &sentinel;
2076
2077	buf = malloc(sizeof(*buf));
2078	if (!buf) {
2079		RES_SET_H_ERRNO(res, NETDB_INTERNAL);
2080		return NS_NOTFOUND;
2081	}
2082	buf2 = malloc(sizeof(*buf2));
2083	if (!buf2) {
2084		free(buf);
2085		RES_SET_H_ERRNO(res, NETDB_INTERNAL);
2086		return NS_NOTFOUND;
2087	}
2088
2089	switch (pai->ai_family) {
2090	case AF_UNSPEC:
2091		q.name = hostname;
2092		q.qclass = C_IN;
2093		q.qtype = T_A;
2094		q.answer = buf->buf;
2095		q.anslen = sizeof(buf->buf);
2096		q.next = &q2;
2097		q2.name = hostname;
2098		q2.qclass = C_IN;
2099		q2.qtype = T_AAAA;
2100		q2.answer = buf2->buf;
2101		q2.anslen = sizeof(buf2->buf);
2102		break;
2103	case AF_INET:
2104		q.name = hostname;
2105		q.qclass = C_IN;
2106		q.qtype = T_A;
2107		q.answer = buf->buf;
2108		q.anslen = sizeof(buf->buf);
2109		break;
2110	case AF_INET6:
2111		q.name = hostname;
2112		q.qclass = C_IN;
2113		q.qtype = T_AAAA;
2114		q.answer = buf->buf;
2115		q.anslen = sizeof(buf->buf);
2116		break;
2117	default:
2118		free(buf);
2119		free(buf2);
2120		return NS_UNAVAIL;
2121	}
2122
2123	res = __res_state();
2124	if ((res->options & RES_INIT) == 0 && res_ninit(res) == -1) {
2125		RES_SET_H_ERRNO(res, NETDB_INTERNAL);
2126		free(buf);
2127		free(buf2);
2128		return NS_NOTFOUND;
2129	}
2130
2131	if (res_searchN(hostname, &q, res) < 0) {
2132		free(buf);
2133		free(buf2);
2134		return NS_NOTFOUND;
2135	}
2136	/* prefer IPv6 */
2137	if (q.next) {
2138		ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai, res);
2139		if (ai) {
2140			cur->ai_next = ai;
2141			while (cur && cur->ai_next)
2142				cur = cur->ai_next;
2143		}
2144	}
2145	ai = getanswer(buf, q.n, q.name, q.qtype, pai, res);
2146	if (ai)
2147		cur->ai_next = ai;
2148	free(buf);
2149	free(buf2);
2150	if (sentinel.ai_next == NULL)
2151		switch (res->res_h_errno) {
2152		case HOST_NOT_FOUND:
2153			return NS_NOTFOUND;
2154		case TRY_AGAIN:
2155			return NS_TRYAGAIN;
2156		default:
2157			return NS_UNAVAIL;
2158		}
2159	*((struct addrinfo **)rv) = sentinel.ai_next;
2160	return NS_SUCCESS;
2161}
2162
2163static void
2164_sethtent(FILE **hostf)
2165{
2166	if (!*hostf)
2167		*hostf = fopen(_PATH_HOSTS, "r");
2168	else
2169		rewind(*hostf);
2170}
2171
2172static void
2173_endhtent(FILE **hostf)
2174{
2175	if (*hostf) {
2176		(void) fclose(*hostf);
2177		*hostf = NULL;
2178	}
2179}
2180
2181static struct addrinfo *
2182_gethtent(FILE **hostf, const char *name, const struct addrinfo *pai)
2183{
2184	char *p;
2185	char *cp, *tname, *cname;
2186	struct addrinfo hints, *res0, *res;
2187	int error;
2188	const char *addr;
2189	char hostbuf[8*1024];
2190
2191	if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "r")))
2192		return (NULL);
2193again:
2194	if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf)))
2195		return (NULL);
2196	if (*p == '#')
2197		goto again;
2198	cp = strpbrk(p, "#\n");
2199	if (cp != NULL)
2200		*cp = '\0';
2201	if (!(cp = strpbrk(p, " \t")))
2202		goto again;
2203	*cp++ = '\0';
2204	addr = p;
2205	cname = NULL;
2206	/* if this is not something we're looking for, skip it. */
2207	while (cp && *cp) {
2208		if (*cp == ' ' || *cp == '\t') {
2209			cp++;
2210			continue;
2211		}
2212		tname = cp;
2213		if (cname == NULL)
2214			cname = cp;
2215		if ((cp = strpbrk(cp, " \t")) != NULL)
2216			*cp++ = '\0';
2217		if (strcasecmp(name, tname) == 0)
2218			goto found;
2219	}
2220	goto again;
2221
2222found:
2223	/* we should not glob socktype/protocol here */
2224	memset(&hints, 0, sizeof(hints));
2225	hints.ai_family = pai->ai_family;
2226	hints.ai_socktype = SOCK_DGRAM;
2227	hints.ai_protocol = 0;
2228	hints.ai_flags = AI_NUMERICHOST;
2229	error = getaddrinfo(addr, "0", &hints, &res0);
2230	if (error)
2231		goto again;
2232#ifdef FILTER_V4MAPPED
2233	/* XXX should check all items in the chain */
2234	if (res0->ai_family == AF_INET6 &&
2235	    IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)res0->ai_addr)->sin6_addr)) {
2236		freeaddrinfo(res0);
2237		goto again;
2238	}
2239#endif
2240	for (res = res0; res; res = res->ai_next) {
2241		/* cover it up */
2242		res->ai_flags = pai->ai_flags;
2243		res->ai_socktype = pai->ai_socktype;
2244		res->ai_protocol = pai->ai_protocol;
2245
2246		if (pai->ai_flags & AI_CANONNAME) {
2247			if (get_canonname(pai, res, cname) != 0) {
2248				freeaddrinfo(res0);
2249				goto again;
2250			}
2251		}
2252	}
2253	return res0;
2254}
2255
2256/*ARGSUSED*/
2257static int
2258_files_getaddrinfo(void *rv, void *cb_data, va_list ap)
2259{
2260	const char *name;
2261	const struct addrinfo *pai;
2262	struct addrinfo sentinel, *cur;
2263	struct addrinfo *p;
2264	FILE *hostf = NULL;
2265
2266	name = va_arg(ap, char *);
2267	pai = va_arg(ap, struct addrinfo *);
2268
2269	memset(&sentinel, 0, sizeof(sentinel));
2270	cur = &sentinel;
2271
2272	_sethtent(&hostf);
2273	while ((p = _gethtent(&hostf, name, pai)) != NULL) {
2274		cur->ai_next = p;
2275		while (cur && cur->ai_next)
2276			cur = cur->ai_next;
2277	}
2278	_endhtent(&hostf);
2279
2280	*((struct addrinfo **)rv) = sentinel.ai_next;
2281	if (sentinel.ai_next == NULL)
2282		return NS_NOTFOUND;
2283	return NS_SUCCESS;
2284}
2285
2286#ifdef YP
2287/*ARGSUSED*/
2288static struct addrinfo *
2289_yphostent(char *line, const struct addrinfo *pai)
2290{
2291	struct addrinfo sentinel, *cur;
2292	struct addrinfo hints, *res, *res0;
2293	int error;
2294	char *p = line;
2295	const char *addr, *canonname;
2296	char *nextline;
2297	char *cp;
2298
2299	addr = canonname = NULL;
2300
2301	memset(&sentinel, 0, sizeof(sentinel));
2302	cur = &sentinel;
2303
2304nextline:
2305	/* terminate line */
2306	cp = strchr(p, '\n');
2307	if (cp) {
2308		*cp++ = '\0';
2309		nextline = cp;
2310	} else
2311		nextline = NULL;
2312
2313	cp = strpbrk(p, " \t");
2314	if (cp == NULL) {
2315		if (canonname == NULL)
2316			return (NULL);
2317		else
2318			goto done;
2319	}
2320	*cp++ = '\0';
2321
2322	addr = p;
2323
2324	while (cp && *cp) {
2325		if (*cp == ' ' || *cp == '\t') {
2326			cp++;
2327			continue;
2328		}
2329		if (!canonname)
2330			canonname = cp;
2331		if ((cp = strpbrk(cp, " \t")) != NULL)
2332			*cp++ = '\0';
2333	}
2334
2335	hints = *pai;
2336	hints.ai_flags = AI_NUMERICHOST;
2337	error = getaddrinfo(addr, NULL, &hints, &res0);
2338	if (error == 0) {
2339		for (res = res0; res; res = res->ai_next) {
2340			/* cover it up */
2341			res->ai_flags = pai->ai_flags;
2342
2343			if (pai->ai_flags & AI_CANONNAME)
2344				(void)get_canonname(pai, res, canonname);
2345		}
2346	} else
2347		res0 = NULL;
2348	if (res0) {
2349		cur->ai_next = res0;
2350		while (cur && cur->ai_next)
2351			cur = cur->ai_next;
2352	}
2353
2354	if (nextline) {
2355		p = nextline;
2356		goto nextline;
2357	}
2358
2359done:
2360	return sentinel.ai_next;
2361}
2362
2363/*ARGSUSED*/
2364static int
2365_yp_getaddrinfo(void *rv, void *cb_data, va_list ap)
2366{
2367	struct addrinfo sentinel, *cur;
2368	struct addrinfo *ai = NULL;
2369	char *ypbuf;
2370	int ypbuflen, r;
2371	const char *name;
2372	const struct addrinfo *pai;
2373	char *ypdomain;
2374
2375	if (_yp_check(&ypdomain) == 0)
2376		return NS_UNAVAIL;
2377
2378	name = va_arg(ap, char *);
2379	pai = va_arg(ap, const struct addrinfo *);
2380
2381	memset(&sentinel, 0, sizeof(sentinel));
2382	cur = &sentinel;
2383
2384	/* hosts.byname is only for IPv4 (Solaris8) */
2385	if (pai->ai_family == PF_UNSPEC || pai->ai_family == PF_INET) {
2386		r = yp_match(ypdomain, "hosts.byname", name,
2387			(int)strlen(name), &ypbuf, &ypbuflen);
2388		if (r == 0) {
2389			struct addrinfo ai4;
2390
2391			ai4 = *pai;
2392			ai4.ai_family = AF_INET;
2393			ai = _yphostent(ypbuf, &ai4);
2394			if (ai) {
2395				cur->ai_next = ai;
2396				while (cur && cur->ai_next)
2397					cur = cur->ai_next;
2398			}
2399			free(ypbuf);
2400		}
2401	}
2402
2403	/* ipnodes.byname can hold both IPv4/v6 */
2404	r = yp_match(ypdomain, "ipnodes.byname", name,
2405		(int)strlen(name), &ypbuf, &ypbuflen);
2406	if (r == 0) {
2407		ai = _yphostent(ypbuf, pai);
2408		if (ai)
2409			cur->ai_next = ai;
2410		free(ypbuf);
2411	}
2412
2413	if (sentinel.ai_next == NULL) {
2414		RES_SET_H_ERRNO(__res_state(), HOST_NOT_FOUND);
2415		return NS_NOTFOUND;
2416	}
2417	*((struct addrinfo **)rv) = sentinel.ai_next;
2418	return NS_SUCCESS;
2419}
2420#endif
2421
2422/* resolver logic */
2423
2424/*
2425 * Formulate a normal query, send, and await answer.
2426 * Returned answer is placed in supplied buffer "answer".
2427 * Perform preliminary check of answer, returning success only
2428 * if no error is indicated and the answer count is nonzero.
2429 * Return the size of the response on success, -1 on error.
2430 * Error number is left in h_errno.
2431 *
2432 * Caller must parse answer and determine whether it answers the question.
2433 */
2434static int
2435res_queryN(const char *name, struct res_target *target, res_state res)
2436{
2437	u_char *buf;
2438	HEADER *hp;
2439	int n;
2440	u_int oflags;
2441	struct res_target *t;
2442	int rcode;
2443	int ancount;
2444
2445	rcode = NOERROR;
2446	ancount = 0;
2447
2448	buf = malloc(MAXPACKET);
2449	if (!buf) {
2450		RES_SET_H_ERRNO(res, NETDB_INTERNAL);
2451		return -1;
2452	}
2453
2454	for (t = target; t; t = t->next) {
2455		int class, type;
2456		u_char *answer;
2457		int anslen;
2458
2459		hp = (HEADER *)(void *)t->answer;
2460
2461		/* make it easier... */
2462		class = t->qclass;
2463		type = t->qtype;
2464		answer = t->answer;
2465		anslen = t->anslen;
2466
2467		oflags = res->_flags;
2468
2469again:
2470		hp->rcode = NOERROR;	/* default */
2471
2472#ifdef DEBUG
2473		if (res->options & RES_DEBUG)
2474			printf(";; res_query(%s, %d, %d)\n", name, class, type);
2475#endif
2476
2477		n = res_nmkquery(res, QUERY, name, class, type, NULL, 0, NULL,
2478		    buf, MAXPACKET);
2479		if (n > 0 && (res->_flags & RES_F_EDNS0ERR) == 0 &&
2480		    (res->options & (RES_USE_EDNS0|RES_USE_DNSSEC)) != 0U)
2481			n = res_nopt(res, n, buf, MAXPACKET, anslen);
2482		if (n <= 0) {
2483#ifdef DEBUG
2484			if (res->options & RES_DEBUG)
2485				printf(";; res_query: mkquery failed\n");
2486#endif
2487			free(buf);
2488			RES_SET_H_ERRNO(res, NO_RECOVERY);
2489			return (n);
2490		}
2491		n = res_nsend(res, buf, n, answer, anslen);
2492		if (n < 0) {
2493			/*
2494			 * if the query choked with EDNS0, retry
2495			 * without EDNS0
2496			 */
2497			if ((res->options & (RES_USE_EDNS0|RES_USE_DNSSEC))
2498			    != 0U &&
2499			    ((oflags ^ res->_flags) & RES_F_EDNS0ERR) != 0) {
2500				res->_flags |= RES_F_EDNS0ERR;
2501				if (res->options & RES_DEBUG)
2502					printf(";; res_nquery: retry without EDNS0\n");
2503				goto again;
2504			}
2505			rcode = hp->rcode;	/* record most recent error */
2506#ifdef DEBUG
2507			if (res->options & RES_DEBUG)
2508				printf(";; res_query: send error\n");
2509#endif
2510			continue;
2511		}
2512
2513		if (n > anslen)
2514			hp->rcode = FORMERR; /* XXX not very informative */
2515		if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
2516			rcode = hp->rcode;	/* record most recent error */
2517#ifdef DEBUG
2518			if (res->options & RES_DEBUG)
2519				printf(";; rcode = %u, ancount=%u\n", hp->rcode,
2520				    ntohs(hp->ancount));
2521#endif
2522			continue;
2523		}
2524
2525		ancount += ntohs(hp->ancount);
2526
2527		t->n = n;
2528	}
2529
2530	free(buf);
2531
2532	if (ancount == 0) {
2533		switch (rcode) {
2534		case NXDOMAIN:
2535			RES_SET_H_ERRNO(res, HOST_NOT_FOUND);
2536			break;
2537		case SERVFAIL:
2538			RES_SET_H_ERRNO(res, TRY_AGAIN);
2539			break;
2540		case NOERROR:
2541			RES_SET_H_ERRNO(res, NO_DATA);
2542			break;
2543		case FORMERR:
2544		case NOTIMP:
2545		case REFUSED:
2546		default:
2547			RES_SET_H_ERRNO(res, NO_RECOVERY);
2548			break;
2549		}
2550		return (-1);
2551	}
2552	return (ancount);
2553}
2554
2555/*
2556 * Formulate a normal query, send, and retrieve answer in supplied buffer.
2557 * Return the size of the response on success, -1 on error.
2558 * If enabled, implement search rules until answer or unrecoverable failure
2559 * is detected.  Error code, if any, is left in h_errno.
2560 */
2561static int
2562res_searchN(const char *name, struct res_target *target, res_state res)
2563{
2564	const char *cp, * const *domain;
2565	HEADER *hp = (HEADER *)(void *)target->answer;	/*XXX*/
2566	u_int dots;
2567	int trailing_dot, ret, saved_herrno;
2568	int got_nodata = 0, got_servfail = 0, root_on_list = 0;
2569	int tried_as_is = 0;
2570	int searched = 0;
2571	char abuf[MAXDNAME];
2572
2573	errno = 0;
2574	RES_SET_H_ERRNO(res, HOST_NOT_FOUND); /* default, if we never query */
2575	dots = 0;
2576	for (cp = name; *cp; cp++)
2577		dots += (*cp == '.');
2578	trailing_dot = 0;
2579	if (cp > name && *--cp == '.')
2580		trailing_dot++;
2581
2582	/*
2583	 * if there aren't any dots, it could be a user-level alias
2584	 */
2585	if (!dots &&
2586	    (cp = res_hostalias(res, name, abuf, sizeof(abuf))) != NULL)
2587		return (res_queryN(cp, target, res));
2588
2589	/*
2590	 * If there are enough dots in the name, let's just give it a
2591	 * try 'as is'. The threshold can be set with the "ndots" option.
2592	 * Also, query 'as is', if there is a trailing dot in the name.
2593	 */
2594	saved_herrno = -1;
2595	if (dots >= res->ndots || trailing_dot) {
2596		ret = res_querydomainN(name, NULL, target, res);
2597		if (ret > 0 || trailing_dot)
2598			return (ret);
2599		if (errno == ECONNREFUSED) {
2600			RES_SET_H_ERRNO(res, TRY_AGAIN);
2601			return (-1);
2602		}
2603		switch (res->res_h_errno) {
2604		case NO_DATA:
2605		case HOST_NOT_FOUND:
2606			break;
2607		case TRY_AGAIN:
2608			if (hp->rcode == SERVFAIL)
2609				break;
2610			/* FALLTHROUGH */
2611		default:
2612			return (-1);
2613		}
2614		saved_herrno = res->res_h_errno;
2615		tried_as_is++;
2616	}
2617
2618	/*
2619	 * We do at least one level of search if
2620	 *	- there is no dot and RES_DEFNAME is set, or
2621	 *	- there is at least one dot, there is no trailing dot,
2622	 *	  and RES_DNSRCH is set.
2623	 */
2624	if ((!dots && (res->options & RES_DEFNAMES)) ||
2625	    (dots && !trailing_dot && (res->options & RES_DNSRCH))) {
2626		int done = 0;
2627
2628		for (domain = (const char * const *)res->dnsrch;
2629		   *domain && !done;
2630		   domain++) {
2631			searched = 1;
2632
2633			if (domain[0][0] == '\0' ||
2634			    (domain[0][0] == '.' && domain[0][1] == '\0'))
2635				root_on_list++;
2636
2637			if (root_on_list && tried_as_is)
2638				continue;
2639
2640			ret = res_querydomainN(name, *domain, target, res);
2641			if (ret > 0)
2642				return (ret);
2643
2644			/*
2645			 * If no server present, give up.
2646			 * If name isn't found in this domain,
2647			 * keep trying higher domains in the search list
2648			 * (if that's enabled).
2649			 * On a NO_DATA error, keep trying, otherwise
2650			 * a wildcard entry of another type could keep us
2651			 * from finding this entry higher in the domain.
2652			 * If we get some other error (negative answer or
2653			 * server failure), then stop searching up,
2654			 * but try the input name below in case it's
2655			 * fully-qualified.
2656			 */
2657			if (errno == ECONNREFUSED) {
2658				RES_SET_H_ERRNO(res, TRY_AGAIN);
2659				return (-1);
2660			}
2661
2662			switch (res->res_h_errno) {
2663			case NO_DATA:
2664				got_nodata++;
2665				/* FALLTHROUGH */
2666			case HOST_NOT_FOUND:
2667				/* keep trying */
2668				break;
2669			case TRY_AGAIN:
2670				got_servfail++;
2671				if (hp->rcode == SERVFAIL) {
2672					/* try next search element, if any */
2673					break;
2674				}
2675				/* FALLTHROUGH */
2676			default:
2677				/* anything else implies that we're done */
2678				done++;
2679			}
2680			/*
2681			 * if we got here for some reason other than DNSRCH,
2682			 * we only wanted one iteration of the loop, so stop.
2683			 */
2684			if (!(res->options & RES_DNSRCH))
2685			        done++;
2686		}
2687	}
2688
2689	switch (res->res_h_errno) {
2690	case NO_DATA:
2691	case HOST_NOT_FOUND:
2692		break;
2693	case TRY_AGAIN:
2694		if (hp->rcode == SERVFAIL)
2695			break;
2696		/* FALLTHROUGH */
2697	default:
2698		goto giveup;
2699	}
2700
2701	/*
2702	 * If the query has not already been tried as is then try it
2703	 * unless RES_NOTLDQUERY is set and there were no dots.
2704	 */
2705	if ((dots || !searched || !(res->options & RES_NOTLDQUERY)) &&
2706	    !(tried_as_is || root_on_list)) {
2707		ret = res_querydomainN(name, NULL, target, res);
2708		if (ret > 0)
2709			return (ret);
2710	}
2711
2712	/*
2713	 * if we got here, we didn't satisfy the search.
2714	 * if we did an initial full query, return that query's h_errno
2715	 * (note that we wouldn't be here if that query had succeeded).
2716	 * else if we ever got a nodata, send that back as the reason.
2717	 * else send back meaningless h_errno, that being the one from
2718	 * the last DNSRCH we did.
2719	 */
2720giveup:
2721	if (saved_herrno != -1)
2722		RES_SET_H_ERRNO(res, saved_herrno);
2723	else if (got_nodata)
2724		RES_SET_H_ERRNO(res, NO_DATA);
2725	else if (got_servfail)
2726		RES_SET_H_ERRNO(res, TRY_AGAIN);
2727	return (-1);
2728}
2729
2730/*
2731 * Perform a call on res_query on the concatenation of name and domain,
2732 * removing a trailing dot from name if domain is NULL.
2733 */
2734static int
2735res_querydomainN(const char *name, const char *domain,
2736    struct res_target *target, res_state res)
2737{
2738	char nbuf[MAXDNAME];
2739	const char *longname = nbuf;
2740	size_t n, d;
2741
2742#ifdef DEBUG
2743	if (res->options & RES_DEBUG)
2744		printf(";; res_querydomain(%s, %s)\n",
2745			name, domain?domain:"<Nil>");
2746#endif
2747	if (domain == NULL) {
2748		/*
2749		 * Check for trailing '.';
2750		 * copy without '.' if present.
2751		 */
2752		n = strlen(name);
2753		if (n >= MAXDNAME) {
2754			RES_SET_H_ERRNO(res, NO_RECOVERY);
2755			return (-1);
2756		}
2757		if (n > 0 && name[--n] == '.') {
2758			strncpy(nbuf, name, n);
2759			nbuf[n] = '\0';
2760		} else
2761			longname = name;
2762	} else {
2763		n = strlen(name);
2764		d = strlen(domain);
2765		if (n + d + 1 >= MAXDNAME) {
2766			RES_SET_H_ERRNO(res, NO_RECOVERY);
2767			return (-1);
2768		}
2769		snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
2770	}
2771	return (res_queryN(longname, target, res));
2772}
2773