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