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