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