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