getaddrinfo.c revision 140947
1169695Skan/*	$KAME: getaddrinfo.c,v 1.15 2000/07/09 04:37:24 itojun Exp $	*/
2169695Skan
3169695Skan/*
4169695Skan * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5169695Skan * All rights reserved.
6169695Skan *
7169695Skan * Redistribution and use in source and binary forms, with or without
8169695Skan * modification, are permitted provided that the following conditions
9169695Skan * are met:
10169695Skan * 1. Redistributions of source code must retain the above copyright
11169695Skan *    notice, this list of conditions and the following disclaimer.
12169695Skan * 2. Redistributions in binary form must reproduce the above copyright
13169695Skan *    notice, this list of conditions and the following disclaimer in the
14169695Skan *    documentation and/or other materials provided with the distribution.
15169695Skan * 3. Neither the name of the project nor the names of its contributors
16169695Skan *    may be used to endorse or promote products derived from this software
17169695Skan *    without specific prior written permission.
18169695Skan *
19169695Skan * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20169695Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21169695Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22169695Skan * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23169695Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24169695Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25169695Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26169695Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27169695Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28169695Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29169695Skan * SUCH DAMAGE.
30169695Skan */
31169695Skan
32169695Skan/*
33169695Skan * "#ifdef FAITH" part is local hack for supporting IPv4-v6 translator.
34169695Skan *
35169695Skan * Issues to be discussed:
36169695Skan * - Thread safe-ness must be checked.
37169695Skan * - Return values.  There are nonstandard return values defined and used
38169695Skan *   in the source code.  This is because RFC2553 is silent about which error
39169695Skan *   code must be returned for which situation.
40169695Skan * - freeaddrinfo(NULL).  RFC2553 is silent about it.  XNET 5.2 says it is
41169695Skan *   invalid.  current code - SEGV on freeaddrinfo(NULL)
42169695Skan *
43169695Skan * Note:
44169695Skan * - The code filters out AFs that are not supported by the kernel,
45169695Skan *   when globbing NULL hostname (to loopback, or wildcard).  Is it the right
46169695Skan *   thing to do?  What is the relationship with post-RFC2553 AI_ADDRCONFIG
47169695Skan *   in ai_flags?
48169695Skan * - (post-2553) semantics of AI_ADDRCONFIG itself is too vague.
49169695Skan *   (1) what should we do against numeric hostname (2) what should we do
50169695Skan *   against NULL hostname (3) what is AI_ADDRCONFIG itself.  AF not ready?
51169695Skan *   non-loopback address configured?  global address configured?
52169695Skan *
53169695Skan * OS specific notes for netbsd/openbsd/freebsd4/bsdi4:
54169695Skan * - To avoid search order issue, we have a big amount of code duplicate
55169695Skan *   from gethnamaddr.c and some other places.  The issues that there's no
56169695Skan *   lower layer function to lookup "IPv4 or IPv6" record.  Calling
57169695Skan *   gethostbyname2 from getaddrinfo will end up in wrong search order, as
58169695Skan *   presented above.
59169695Skan *
60169695Skan * OS specific notes for freebsd4:
61169695Skan * - FreeBSD supported $GAI.  The code does not.
62169695Skan * - FreeBSD allowed classful IPv4 numeric (127.1), the code does not.
63169695Skan */
64169695Skan
65169695Skan#include <sys/cdefs.h>
66169695Skan__FBSDID("$FreeBSD: head/lib/libc/net/getaddrinfo.c 140947 2005-01-28 19:35:42Z ume $");
67169695Skan
68169695Skan#include "namespace.h"
69169695Skan#include <sys/types.h>
70169695Skan#include <sys/param.h>
71169695Skan#include <sys/socket.h>
72169695Skan#include <net/if.h>
73169695Skan#include <netinet/in.h>
74169695Skan#include <sys/queue.h>
75169695Skan#ifdef INET6
76169695Skan#include <net/if_var.h>
77169695Skan#include <sys/sysctl.h>
78169695Skan#include <sys/ioctl.h>
79169695Skan#include <netinet6/in6_var.h>	/* XXX */
80169695Skan#endif
81169695Skan#include <arpa/inet.h>
82169695Skan#include <arpa/nameser.h>
83169695Skan#include <rpc/rpc.h>
84169695Skan#include <rpcsvc/yp_prot.h>
85169695Skan#include <rpcsvc/ypclnt.h>
86169695Skan#include <netdb.h>
87169695Skan#include <pthread.h>
88169695Skan#include <resolv.h>
89169695Skan#include <string.h>
90169695Skan#include <stdlib.h>
91169695Skan#include <stddef.h>
92169695Skan#include <ctype.h>
93169695Skan#include <unistd.h>
94169695Skan#include <stdio.h>
95169695Skan#include <errno.h>
96169695Skan
97169695Skan#include "res_config.h"
98169695Skan
99169695Skan#ifdef DEBUG
100169695Skan#include <syslog.h>
101169695Skan#endif
102169695Skan
103169695Skan#include <stdarg.h>
104169695Skan#include <nsswitch.h>
105169695Skan#include "un-namespace.h"
106169695Skan#include "libc_private.h"
107169695Skan
108169695Skan#if defined(__KAME__) && defined(INET6)
109169695Skan# define FAITH
110169695Skan#endif
111169695Skan
112169695Skan#define SUCCESS 0
113169695Skan#define ANY 0
114169695Skan#define YES 1
115169695Skan#define NO  0
116169695Skan
117169695Skanstatic const char in_addrany[] = { 0, 0, 0, 0 };
118169695Skanstatic const char in_loopback[] = { 127, 0, 0, 1 };
119169695Skan#ifdef INET6
120169695Skanstatic const char in6_addrany[] = {
121169695Skan	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
122169695Skan};
123169695Skanstatic const char in6_loopback[] = {
124169695Skan	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
125169695Skan};
126169695Skan#endif
127169695Skan
128169695Skanstruct policyqueue {
129169695Skan	TAILQ_ENTRY(policyqueue) pc_entry;
130169695Skan#ifdef INET6
131169695Skan	struct in6_addrpolicy pc_policy;
132169695Skan#endif
133169695Skan};
134169695SkanTAILQ_HEAD(policyhead, policyqueue);
135169695Skan
136169695Skanstatic const struct afd {
137169695Skan	int a_af;
138169695Skan	int a_addrlen;
139169695Skan	int a_socklen;
140169695Skan	int a_off;
141169695Skan	const char *a_addrany;
142169695Skan	const char *a_loopback;
143169695Skan	int a_scoped;
144169695Skan} afdl [] = {
145169695Skan#ifdef INET6
146169695Skan#define	N_INET6 0
147169695Skan	{PF_INET6, sizeof(struct in6_addr),
148169695Skan	 sizeof(struct sockaddr_in6),
149169695Skan	 offsetof(struct sockaddr_in6, sin6_addr),
150169695Skan	 in6_addrany, in6_loopback, 1},
151169695Skan#define	N_INET 1
152169695Skan#else
153169695Skan#define	N_INET 0
154169695Skan#endif
155169695Skan	{PF_INET, sizeof(struct in_addr),
156169695Skan	 sizeof(struct sockaddr_in),
157169695Skan	 offsetof(struct sockaddr_in, sin_addr),
158169695Skan	 in_addrany, in_loopback, 0},
159169695Skan	{0, 0, 0, 0, NULL, NULL, 0},
160169695Skan};
161169695Skan
162169695Skanstruct explore {
163169695Skan	int e_af;
164169695Skan	int e_socktype;
165169695Skan	int e_protocol;
166169695Skan	const char *e_protostr;
167169695Skan	int e_wild;
168169695Skan#define WILD_AF(ex)		((ex)->e_wild & 0x01)
169169695Skan#define WILD_SOCKTYPE(ex)	((ex)->e_wild & 0x02)
170169695Skan#define WILD_PROTOCOL(ex)	((ex)->e_wild & 0x04)
171169695Skan};
172169695Skan
173169695Skanstatic const struct explore explore[] = {
174169695Skan#if 0
175169695Skan	{ PF_LOCAL, 0, ANY, ANY, NULL, 0x01 },
176169695Skan#endif
177169695Skan#ifdef INET6
178169695Skan	{ PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
179169695Skan	{ PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
180169695Skan	{ PF_INET6, SOCK_RAW, ANY, NULL, 0x05 },
181169695Skan#endif
182169695Skan	{ PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
183169695Skan	{ PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
184169695Skan	{ PF_INET, SOCK_RAW, ANY, NULL, 0x05 },
185169695Skan	{ PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
186169695Skan	{ PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
187169695Skan	{ PF_UNSPEC, SOCK_RAW, ANY, NULL, 0x05 },
188169695Skan	{ -1, 0, 0, NULL, 0 },
189169695Skan};
190169695Skan
191169695Skan#ifdef INET6
192169695Skan#define PTON_MAX	16
193169695Skan#else
194169695Skan#define PTON_MAX	4
195169695Skan#endif
196169695Skan
197169695Skan#define AIO_SRCFLAG_DEPRECATED	0x1
198169695Skan
199169695Skanstruct ai_order {
200169695Skan	union {
201169695Skan		struct sockaddr_storage aiou_ss;
202169695Skan		struct sockaddr aiou_sa;
203169695Skan	} aio_src_un;
204169695Skan#define aio_srcsa aio_src_un.aiou_sa
205169695Skan	u_int32_t aio_srcflag;
206169695Skan	int aio_srcscope;
207169695Skan	int aio_dstscope;
208169695Skan	struct policyqueue *aio_srcpolicy;
209169695Skan	struct policyqueue *aio_dstpolicy;
210169695Skan	struct addrinfo *aio_ai;
211169695Skan	int aio_matchlen;
212169695Skan};
213169695Skan
214169695Skanstatic const ns_src default_dns_files[] = {
215169695Skan	{ NSSRC_FILES, 	NS_SUCCESS },
216169695Skan	{ NSSRC_DNS, 	NS_SUCCESS },
217169695Skan	{ 0 }
218169695Skan};
219169695Skan
220169695Skanstruct res_target {
221169695Skan	struct res_target *next;
222169695Skan	const char *name;	/* domain name */
223169695Skan	int qclass, qtype;	/* class and type of query */
224169695Skan	u_char *answer;		/* buffer to put answer */
225169695Skan	int anslen;		/* size of answer buffer */
226169695Skan	int n;			/* result length */
227169695Skan};
228169695Skan
229169695Skan#define MAXPACKET	(64*1024)
230169695Skan
231169695Skantypedef union {
232169695Skan	HEADER hdr;
233169695Skan	u_char buf[MAXPACKET];
234169695Skan} querybuf;
235169695Skan
236169695Skanstatic int str2number(const char *);
237169695Skanstatic int explore_null(const struct addrinfo *,
238169695Skan	const char *, struct addrinfo **);
239169695Skanstatic int explore_numeric(const struct addrinfo *, const char *,
240169695Skan	const char *, struct addrinfo **, const char *);
241169695Skanstatic int explore_numeric_scope(const struct addrinfo *, const char *,
242169695Skan	const char *, struct addrinfo **);
243169695Skanstatic int get_canonname(const struct addrinfo *,
244169695Skan	struct addrinfo *, const char *);
245169695Skanstatic struct addrinfo *get_ai(const struct addrinfo *,
246169695Skan	const struct afd *, const char *);
247169695Skanstatic int get_portmatch(const struct addrinfo *, const char *);
248169695Skanstatic int get_port(struct addrinfo *, const char *, int);
249169695Skanstatic const struct afd *find_afd(int);
250169695Skanstatic int addrconfig(struct addrinfo *);
251169695Skanstatic void set_source(struct ai_order *, struct policyhead *);
252169695Skanstatic int comp_dst(const void *, const void *);
253169695Skan#ifdef INET6
254169695Skanstatic int ip6_str2scopeid(char *, struct sockaddr_in6 *, u_int32_t *);
255169695Skan#endif
256169695Skanstatic int gai_addr2scopetype(struct sockaddr *);
257169695Skan
258169695Skanstatic int explore_fqdn(const struct addrinfo *, const char *,
259169695Skan	const char *, struct addrinfo **);
260169695Skan
261169695Skanstatic int reorder(struct addrinfo *);
262169695Skanstatic int get_addrselectpolicy(struct policyhead *);
263169695Skanstatic void free_addrselectpolicy(struct policyhead *);
264169695Skanstatic struct policyqueue *match_addrselectpolicy(struct sockaddr *,
265169695Skan	struct policyhead *);
266169695Skanstatic int matchlen(struct sockaddr *, struct sockaddr *);
267169695Skan
268169695Skanstatic struct addrinfo *getanswer(const querybuf *, int, const char *, int,
269169695Skan	const struct addrinfo *);
270169695Skan#if defined(RESOLVSORT)
271169695Skanstatic int addr4sort(struct addrinfo *);
272169695Skan#endif
273169695Skanstatic int _dns_getaddrinfo(void *, void *, va_list);
274169695Skanstatic void _sethtent(void);
275169695Skanstatic void _endhtent(void);
276169695Skanstatic struct addrinfo *_gethtent(const char *, const struct addrinfo *);
277169695Skanstatic int _files_getaddrinfo(void *, void *, va_list);
278169695Skan#ifdef YP
279169695Skanstatic struct addrinfo *_yphostent(char *, const struct addrinfo *);
280169695Skanstatic int _yp_getaddrinfo(void *, void *, va_list);
281169695Skan#endif
282169695Skan
283169695Skanstatic int res_queryN(const char *, struct res_target *);
284169695Skanstatic int res_searchN(const char *, struct res_target *);
285169695Skanstatic int res_querydomainN(const char *, const char *,
286169695Skan	struct res_target *);
287169695Skan
288169695Skanstatic struct ai_errlist {
289169695Skan	const char *str;
290169695Skan	int code;
291169695Skan} ai_errlist[] = {
292169695Skan	{ "Success",					0, },
293169695Skan	{ "Temporary failure in name resolution",	EAI_AGAIN, },
294169695Skan	{ "Invalid value for ai_flags",		       	EAI_BADFLAGS, },
295169695Skan	{ "Non-recoverable failure in name resolution", EAI_FAIL, },
296169695Skan	{ "ai_family not supported",			EAI_FAMILY, },
297169695Skan	{ "Memory allocation failure", 			EAI_MEMORY, },
298169695Skan	{ "hostname nor servname provided, or not known", EAI_NONAME, },
299169695Skan	{ "servname not supported for ai_socktype",	EAI_SERVICE, },
300169695Skan	{ "ai_socktype not supported", 			EAI_SOCKTYPE, },
301169695Skan	{ "System error returned in errno", 		EAI_SYSTEM, },
302169695Skan	{ "Invalid value for hints",			EAI_BADHINTS, },
303169695Skan	{ "Resolved protocol is unknown",		EAI_PROTOCOL, },
304169695Skan	/* backward compatibility with userland code prior to 2553bis-02 */
305169695Skan	{ "Address family for hostname not supported",	1, },
306169695Skan	{ "No address associated with hostname", 	7, },
307169695Skan	{ NULL,						-1, },
308169695Skan};
309169695Skan
310169695Skan/*
311228474Sed * XXX: Many dependencies are not thread-safe.  So, we share lock between
312169695Skan * getaddrinfo() and getipnodeby*().  Still, we cannot use
313169695Skan * getaddrinfo() and getipnodeby*() in conjunction with other
314169695Skan * functions which call them.
315169695Skan */
316169695Skanpthread_mutex_t __getaddrinfo_thread_lock = PTHREAD_MUTEX_INITIALIZER;
317169695Skan#define THREAD_LOCK() \
318169695Skan	if (__isthreaded) _pthread_mutex_lock(&__getaddrinfo_thread_lock);
319169695Skan#define THREAD_UNLOCK() \
320169695Skan	if (__isthreaded) _pthread_mutex_unlock(&__getaddrinfo_thread_lock);
321169695Skan
322169695Skan/* XXX macros that make external reference is BAD. */
323169695Skan
324169695Skan#define GET_AI(ai, afd, addr) \
325169695Skando { \
326169695Skan	/* external reference: pai, error, and label free */ \
327169695Skan	(ai) = get_ai(pai, (afd), (addr)); \
328169695Skan	if ((ai) == NULL) { \
329169695Skan		error = EAI_MEMORY; \
330169695Skan		goto free; \
331169695Skan	} \
332169695Skan} while (/*CONSTCOND*/0)
333169695Skan
334169695Skan#define GET_PORT(ai, serv) \
335169695Skando { \
336169695Skan	/* external reference: error and label free */ \
337169695Skan	error = get_port((ai), (serv), 0); \
338169695Skan	if (error != 0) \
339169695Skan		goto free; \
340169695Skan} while (/*CONSTCOND*/0)
341169695Skan
342169695Skan#define GET_CANONNAME(ai, str) \
343169695Skando { \
344169695Skan	/* external reference: pai, error and label free */ \
345169695Skan	error = get_canonname(pai, (ai), (str)); \
346169695Skan	if (error != 0) \
347169695Skan		goto free; \
348169695Skan} while (/*CONSTCOND*/0)
349169695Skan
350169695Skan#define ERR(err) \
351169695Skando { \
352169695Skan	/* external reference: error, and label bad */ \
353169695Skan	error = (err); \
354169695Skan	goto bad; \
355169695Skan	/*NOTREACHED*/ \
356169695Skan} while (/*CONSTCOND*/0)
357169695Skan
358169695Skan#define MATCH_FAMILY(x, y, w) \
359169695Skan	((x) == (y) || (/*CONSTCOND*/(w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
360169695Skan#define MATCH(x, y, w) \
361169695Skan	((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY)))
362169695Skan
363169695Skanchar *
364169695Skangai_strerror(ecode)
365169695Skan	int ecode;
366169695Skan{
367169695Skan	struct ai_errlist *p;
368169695Skan
369169695Skan	for (p = ai_errlist; p->str; p++) {
370169695Skan		if (p->code == ecode)
371169695Skan			return (char *)p->str;
372169695Skan	}
373169695Skan	return "Unknown error";
374169695Skan}
375169695Skan
376169695Skanvoid
377169695Skanfreeaddrinfo(ai)
378169695Skan	struct addrinfo *ai;
379169695Skan{
380169695Skan	struct addrinfo *next;
381169695Skan
382169695Skan	do {
383169695Skan		next = ai->ai_next;
384169695Skan		if (ai->ai_canonname)
385169695Skan			free(ai->ai_canonname);
386169695Skan		/* no need to free(ai->ai_addr) */
387169695Skan		free(ai);
388169695Skan		ai = next;
389169695Skan	} while (ai);
390169695Skan}
391169695Skan
392169695Skanstatic int
393169695Skanstr2number(p)
394169695Skan	const char *p;
395169695Skan{
396169695Skan	char *ep;
397169695Skan	unsigned long v;
398169695Skan
399169695Skan	if (*p == '\0')
400169695Skan		return -1;
401169695Skan	ep = NULL;
402169695Skan	errno = 0;
403169695Skan	v = strtoul(p, &ep, 10);
404169695Skan	if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX)
405169695Skan		return v;
406169695Skan	else
407169695Skan		return -1;
408169695Skan}
409169695Skan
410169695Skanint
411169695Skangetaddrinfo(hostname, servname, hints, res)
412169695Skan	const char *hostname, *servname;
413169695Skan	const struct addrinfo *hints;
414169695Skan	struct addrinfo **res;
415169695Skan{
416169695Skan	struct addrinfo sentinel;
417169695Skan	struct addrinfo *cur;
418169695Skan	int error = 0;
419169695Skan	struct addrinfo ai;
420169695Skan	struct addrinfo ai0;
421169695Skan	struct addrinfo *pai;
422169695Skan	const struct explore *ex;
423169695Skan	int numeric = 0;
424169695Skan
425169695Skan	memset(&sentinel, 0, sizeof(sentinel));
426169695Skan	cur = &sentinel;
427169695Skan	pai = &ai;
428169695Skan	pai->ai_flags = 0;
429169695Skan	pai->ai_family = PF_UNSPEC;
430169695Skan	pai->ai_socktype = ANY;
431169695Skan	pai->ai_protocol = ANY;
432169695Skan	pai->ai_addrlen = 0;
433169695Skan	pai->ai_canonname = NULL;
434169695Skan	pai->ai_addr = NULL;
435169695Skan	pai->ai_next = NULL;
436169695Skan
437169695Skan	if (hostname == NULL && servname == NULL)
438169695Skan		return EAI_NONAME;
439169695Skan	if (hints) {
440169695Skan		/* error check for hints */
441169695Skan		if (hints->ai_addrlen || hints->ai_canonname ||
442169695Skan		    hints->ai_addr || hints->ai_next)
443169695Skan			ERR(EAI_BADHINTS); /* xxx */
444169695Skan		if (hints->ai_flags & ~AI_MASK)
445169695Skan			ERR(EAI_BADFLAGS);
446169695Skan		switch (hints->ai_family) {
447169695Skan		case PF_UNSPEC:
448169695Skan		case PF_INET:
449169695Skan#ifdef INET6
450169695Skan		case PF_INET6:
451169695Skan#endif
452169695Skan			break;
453169695Skan		default:
454169695Skan			ERR(EAI_FAMILY);
455169695Skan		}
456169695Skan		memcpy(pai, hints, sizeof(*pai));
457169695Skan
458169695Skan		/*
459169695Skan		 * if both socktype/protocol are specified, check if they
460169695Skan		 * are meaningful combination.
461169695Skan		 */
462169695Skan		if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
463169695Skan			for (ex = explore; ex->e_af >= 0; ex++) {
464169695Skan				if (pai->ai_family != ex->e_af)
465169695Skan					continue;
466169695Skan				if (ex->e_socktype == ANY)
467169695Skan					continue;
468169695Skan				if (ex->e_protocol == ANY)
469169695Skan					continue;
470169695Skan				if (pai->ai_socktype == ex->e_socktype &&
471169695Skan				    pai->ai_protocol != ex->e_protocol) {
472169695Skan					ERR(EAI_BADHINTS);
473169695Skan				}
474169695Skan			}
475169695Skan		}
476169695Skan	}
477169695Skan
478169695Skan	/*
479169695Skan	 * post-2553: AI_ALL and AI_V4MAPPED are effective only against
480169695Skan	 * AF_INET6 query.  They need to be ignored if specified in other
481169695Skan	 * occassions.
482169695Skan	 */
483169695Skan	switch (pai->ai_flags & (AI_ALL | AI_V4MAPPED)) {
484169695Skan	case AI_V4MAPPED:
485169695Skan	case AI_ALL | AI_V4MAPPED:
486169695Skan		if (pai->ai_family != AF_INET6)
487169695Skan			pai->ai_flags &= ~(AI_ALL | AI_V4MAPPED);
488169695Skan		break;
489169695Skan	case AI_ALL:
490169695Skan#if 1
491169695Skan		/* illegal */
492169695Skan		ERR(EAI_BADFLAGS);
493169695Skan#else
494169695Skan		pai->ai_flags &= ~(AI_ALL | AI_V4MAPPED);
495169695Skan#endif
496169695Skan		break;
497169695Skan	}
498169695Skan
499169695Skan	/*
500169695Skan	 * check for special cases.  (1) numeric servname is disallowed if
501169695Skan	 * socktype/protocol are left unspecified. (2) servname is disallowed
502169695Skan	 * for raw and other inet{,6} sockets.
503169695Skan	 */
504169695Skan	if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
505169695Skan#ifdef PF_INET6
506169695Skan	    || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)
507169695Skan#endif
508169695Skan	    ) {
509169695Skan		ai0 = *pai;	/* backup *pai */
510169695Skan
511169695Skan		if (pai->ai_family == PF_UNSPEC) {
512169695Skan#ifdef PF_INET6
513169695Skan			pai->ai_family = PF_INET6;
514169695Skan#else
515169695Skan			pai->ai_family = PF_INET;
516169695Skan#endif
517169695Skan		}
518169695Skan		error = get_portmatch(pai, servname);
519169695Skan		if (error)
520169695Skan			ERR(error);
521169695Skan
522169695Skan		*pai = ai0;
523169695Skan	}
524169695Skan
525169695Skan	ai0 = *pai;
526169695Skan
527169695Skan	/* NULL hostname, or numeric hostname */
528169695Skan	for (ex = explore; ex->e_af >= 0; ex++) {
529169695Skan		*pai = ai0;
530169695Skan
531169695Skan		/* PF_UNSPEC entries are prepared for DNS queries only */
532169695Skan		if (ex->e_af == PF_UNSPEC)
533169695Skan			continue;
534169695Skan
535169695Skan		if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex)))
536169695Skan			continue;
537169695Skan		if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex)))
538169695Skan			continue;
539169695Skan		if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex)))
540169695Skan			continue;
541169695Skan
542169695Skan		if (pai->ai_family == PF_UNSPEC)
543169695Skan			pai->ai_family = ex->e_af;
544169695Skan		if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
545169695Skan			pai->ai_socktype = ex->e_socktype;
546169695Skan		if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
547169695Skan			pai->ai_protocol = ex->e_protocol;
548169695Skan
549169695Skan		if (hostname == NULL)
550169695Skan			error = explore_null(pai, servname, &cur->ai_next);
551169695Skan		else
552169695Skan			error = explore_numeric_scope(pai, hostname, servname,
553169695Skan			    &cur->ai_next);
554169695Skan
555169695Skan		if (error)
556169695Skan			goto free;
557169695Skan
558169695Skan		while (cur && cur->ai_next)
559169695Skan			cur = cur->ai_next;
560169695Skan	}
561169695Skan
562169695Skan	/*
563169695Skan	 * XXX
564169695Skan	 * If numreic representation of AF1 can be interpreted as FQDN
565169695Skan	 * representation of AF2, we need to think again about the code below.
566169695Skan	 */
567169695Skan	if (sentinel.ai_next) {
568169695Skan		numeric = 1;
569169695Skan		goto good;
570169695Skan	}
571169695Skan
572169695Skan	if (hostname == NULL)
573169695Skan		ERR(EAI_NONAME);	/* used to be EAI_NODATA */
574169695Skan	if (pai->ai_flags & AI_NUMERICHOST)
575169695Skan		ERR(EAI_NONAME);
576169695Skan
577169695Skan	if ((pai->ai_flags & AI_ADDRCONFIG) != 0 && !addrconfig(&ai0))
578169695Skan		ERR(EAI_FAIL);
579169695Skan
580169695Skan	/*
581169695Skan	 * hostname as alphabetical name.
582169695Skan	 * we would like to prefer AF_INET6 than AF_INET, so we'll make a
583169695Skan	 * outer loop by AFs.
584169695Skan	 */
585169695Skan	for (ex = explore; ex->e_af >= 0; ex++) {
586169695Skan		*pai = ai0;
587169695Skan
588169695Skan		/* require exact match for family field */
589169695Skan		if (pai->ai_family != ex->e_af)
590169695Skan			continue;
591169695Skan
592169695Skan		if (!MATCH(pai->ai_socktype, ex->e_socktype,
593169695Skan				WILD_SOCKTYPE(ex))) {
594169695Skan			continue;
595169695Skan		}
596169695Skan		if (!MATCH(pai->ai_protocol, ex->e_protocol,
597169695Skan				WILD_PROTOCOL(ex))) {
598169695Skan			continue;
599169695Skan		}
600169695Skan
601169695Skan		if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
602169695Skan			pai->ai_socktype = ex->e_socktype;
603169695Skan		if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
604169695Skan			pai->ai_protocol = ex->e_protocol;
605169695Skan
606169695Skan		error = explore_fqdn(pai, hostname, servname,
607169695Skan			&cur->ai_next);
608169695Skan
609169695Skan		while (cur && cur->ai_next)
610169695Skan			cur = cur->ai_next;
611169695Skan	}
612169695Skan
613169695Skan	/* XXX inhibit errors if we have the result */
614169695Skan	if (sentinel.ai_next)
615169695Skan		error = 0;
616169695Skan
617169695Skangood:
618169695Skan	/*
619169695Skan	 * ensure we return either:
620169695Skan	 * - error == 0, non-NULL *res
621169695Skan	 * - error != 0, NULL *res
622169695Skan	 */
623169695Skan	if (error == 0) {
624169695Skan		if (sentinel.ai_next) {
625169695Skan			/*
626169695Skan			 * If the returned entry is for an active connection,
627169695Skan			 * and the given name is not numeric, reorder the
628169695Skan			 * list, so that the application would try the list
629169695Skan			 * in the most efficient order.
630169695Skan			 */
631169695Skan			if (hints == NULL || !(hints->ai_flags & AI_PASSIVE)) {
632169695Skan				if (!numeric)
633169695Skan					(void)reorder(&sentinel);
634169695Skan			}
635169695Skan			*res = sentinel.ai_next;
636169695Skan			return SUCCESS;
637169695Skan		} else
638169695Skan			error = EAI_FAIL;
639169695Skan	}
640free:
641bad:
642	if (sentinel.ai_next)
643		freeaddrinfo(sentinel.ai_next);
644	*res = NULL;
645	return error;
646}
647
648static int
649reorder(sentinel)
650	struct addrinfo *sentinel;
651{
652	struct addrinfo *ai, **aip;
653	struct ai_order *aio;
654	int i, n;
655	struct policyhead policyhead;
656
657	/* count the number of addrinfo elements for sorting. */
658	for (n = 0, ai = sentinel->ai_next; ai != NULL; ai = ai->ai_next, n++)
659		;
660
661	/*
662	 * If the number is small enough, we can skip the reordering process.
663	 */
664	if (n <= 1)
665		return(n);
666
667	/* allocate a temporary array for sort and initialization of it. */
668	if ((aio = malloc(sizeof(*aio) * n)) == NULL)
669		return(n);	/* give up reordering */
670	memset(aio, 0, sizeof(*aio) * n);
671
672	/* retrieve address selection policy from the kernel */
673	TAILQ_INIT(&policyhead);
674	if (!get_addrselectpolicy(&policyhead)) {
675		/* no policy is installed into kernel, we don't sort. */
676		free(aio);
677		return (n);
678	}
679
680	for (i = 0, ai = sentinel->ai_next; i < n; ai = ai->ai_next, i++) {
681		aio[i].aio_ai = ai;
682		aio[i].aio_dstscope = gai_addr2scopetype(ai->ai_addr);
683		aio[i].aio_dstpolicy = match_addrselectpolicy(ai->ai_addr,
684							      &policyhead);
685		set_source(&aio[i], &policyhead);
686	}
687
688	/* perform sorting. */
689	qsort(aio, n, sizeof(*aio), comp_dst);
690
691	/* reorder the addrinfo chain. */
692	for (i = 0, aip = &sentinel->ai_next; i < n; i++) {
693		*aip = aio[i].aio_ai;
694		aip = &aio[i].aio_ai->ai_next;
695	}
696	*aip = NULL;
697
698	/* cleanup and return */
699	free(aio);
700	free_addrselectpolicy(&policyhead);
701	return(n);
702}
703
704static int
705get_addrselectpolicy(head)
706	struct policyhead *head;
707{
708#ifdef INET6
709	int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_ADDRCTLPOLICY };
710	size_t l;
711	char *buf;
712	struct in6_addrpolicy *pol, *ep;
713
714	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0)
715		return (0);
716	if ((buf = malloc(l)) == NULL)
717		return (0);
718	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
719		free(buf);
720		return (0);
721	}
722
723	ep = (struct in6_addrpolicy *)(buf + l);
724	for (pol = (struct in6_addrpolicy *)buf; pol + 1 <= ep; pol++) {
725		struct policyqueue *new;
726
727		if ((new = malloc(sizeof(*new))) == NULL) {
728			free_addrselectpolicy(head); /* make the list empty */
729			break;
730		}
731		new->pc_policy = *pol;
732		TAILQ_INSERT_TAIL(head, new, pc_entry);
733	}
734
735	free(buf);
736	return (1);
737#else
738	return (0);
739#endif
740}
741
742static void
743free_addrselectpolicy(head)
744	struct policyhead *head;
745{
746	struct policyqueue *ent, *nent;
747
748	for (ent = TAILQ_FIRST(head); ent; ent = nent) {
749		nent = TAILQ_NEXT(ent, pc_entry);
750		TAILQ_REMOVE(head, ent, pc_entry);
751		free(ent);
752	}
753}
754
755static struct policyqueue *
756match_addrselectpolicy(addr, head)
757	struct sockaddr *addr;
758	struct policyhead *head;
759{
760#ifdef INET6
761	struct policyqueue *ent, *bestent = NULL;
762	struct in6_addrpolicy *pol;
763	int matchlen, bestmatchlen = -1;
764	u_char *mp, *ep, *k, *p, m;
765	struct sockaddr_in6 key;
766
767	switch(addr->sa_family) {
768	case AF_INET6:
769		key = *(struct sockaddr_in6 *)addr;
770		break;
771	case AF_INET:
772		/* convert the address into IPv4-mapped IPv6 address. */
773		memset(&key, 0, sizeof(key));
774		key.sin6_family = AF_INET6;
775		key.sin6_len = sizeof(key);
776		key.sin6_addr.s6_addr[10] = 0xff;
777		key.sin6_addr.s6_addr[11] = 0xff;
778		memcpy(&key.sin6_addr.s6_addr[12],
779		       &((struct sockaddr_in *)addr)->sin_addr, 4);
780		break;
781	default:
782		return(NULL);
783	}
784
785	for (ent = TAILQ_FIRST(head); ent; ent = TAILQ_NEXT(ent, pc_entry)) {
786		pol = &ent->pc_policy;
787		matchlen = 0;
788
789		mp = (u_char *)&pol->addrmask.sin6_addr;
790		ep = mp + 16;	/* XXX: scope field? */
791		k = (u_char *)&key.sin6_addr;
792		p = (u_char *)&pol->addr.sin6_addr;
793		for (; mp < ep && *mp; mp++, k++, p++) {
794			m = *mp;
795			if ((*k & m) != *p)
796				goto next; /* not match */
797			if (m == 0xff) /* short cut for a typical case */
798				matchlen += 8;
799			else {
800				while (m >= 0x80) {
801					matchlen++;
802					m <<= 1;
803				}
804			}
805		}
806
807		/* matched.  check if this is better than the current best. */
808		if (matchlen > bestmatchlen) {
809			bestent = ent;
810			bestmatchlen = matchlen;
811		}
812
813	  next:
814		continue;
815	}
816
817	return(bestent);
818#else
819	return(NULL);
820#endif
821
822}
823
824static void
825set_source(aio, ph)
826	struct ai_order *aio;
827	struct policyhead *ph;
828{
829	struct addrinfo ai = *aio->aio_ai;
830	struct sockaddr_storage ss;
831	int s, srclen;
832
833	/* set unspec ("no source is available"), just in case */
834	aio->aio_srcsa.sa_family = AF_UNSPEC;
835	aio->aio_srcscope = -1;
836
837	switch(ai.ai_family) {
838	case AF_INET:
839#ifdef INET6
840	case AF_INET6:
841#endif
842		break;
843	default:		/* ignore unsupported AFs explicitly */
844		return;
845	}
846
847	/* XXX: make a dummy addrinfo to call connect() */
848	ai.ai_socktype = SOCK_DGRAM;
849	ai.ai_protocol = IPPROTO_UDP; /* is UDP too specific? */
850	ai.ai_next = NULL;
851	memset(&ss, 0, sizeof(ss));
852	memcpy(&ss, ai.ai_addr, ai.ai_addrlen);
853	ai.ai_addr = (struct sockaddr *)&ss;
854	get_port(&ai, "1", 0);
855
856	/* open a socket to get the source address for the given dst */
857	if ((s = _socket(ai.ai_family, ai.ai_socktype, ai.ai_protocol)) < 0)
858		return;		/* give up */
859	if (_connect(s, ai.ai_addr, ai.ai_addrlen) < 0)
860		goto cleanup;
861	srclen = ai.ai_addrlen;
862	if (_getsockname(s, &aio->aio_srcsa, &srclen) < 0) {
863		aio->aio_srcsa.sa_family = AF_UNSPEC;
864		goto cleanup;
865	}
866	aio->aio_srcscope = gai_addr2scopetype(&aio->aio_srcsa);
867	aio->aio_srcpolicy = match_addrselectpolicy(&aio->aio_srcsa, ph);
868	aio->aio_matchlen = matchlen(&aio->aio_srcsa, aio->aio_ai->ai_addr);
869#ifdef INET6
870	if (ai.ai_family == AF_INET6) {
871		struct in6_ifreq ifr6;
872		u_int32_t flags6;
873
874		/* XXX: interface name should not be hardcoded */
875		strncpy(ifr6.ifr_name, "lo0", sizeof(ifr6.ifr_name));
876		memset(&ifr6, 0, sizeof(ifr6));
877		memcpy(&ifr6.ifr_addr, ai.ai_addr, ai.ai_addrlen);
878		if (_ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == 0) {
879			flags6 = ifr6.ifr_ifru.ifru_flags6;
880			if ((flags6 & IN6_IFF_DEPRECATED))
881				aio->aio_srcflag |= AIO_SRCFLAG_DEPRECATED;
882		}
883	}
884#endif
885
886  cleanup:
887	_close(s);
888	return;
889}
890
891static int
892matchlen(src, dst)
893	struct sockaddr *src, *dst;
894{
895	int match = 0;
896	u_char *s, *d;
897	u_char *lim, r;
898	int addrlen;
899
900	switch (src->sa_family) {
901#ifdef INET6
902	case AF_INET6:
903		s = (u_char *)&((struct sockaddr_in6 *)src)->sin6_addr;
904		d = (u_char *)&((struct sockaddr_in6 *)dst)->sin6_addr;
905		addrlen = sizeof(struct in6_addr);
906		lim = s + addrlen;
907		break;
908#endif
909	case AF_INET:
910		s = (u_char *)&((struct sockaddr_in6 *)src)->sin6_addr;
911		d = (u_char *)&((struct sockaddr_in6 *)dst)->sin6_addr;
912		addrlen = sizeof(struct in_addr);
913		lim = s + addrlen;
914		break;
915	default:
916		return(0);
917	}
918
919	while (s < lim)
920		if ((r = (*d++ ^ *s++)) != 0) {
921			while (r < addrlen * 8) {
922				match++;
923				r <<= 1;
924			}
925			break;
926		} else
927			match += 8;
928	return(match);
929}
930
931static int
932comp_dst(arg1, arg2)
933	const void *arg1, *arg2;
934{
935	const struct ai_order *dst1 = arg1, *dst2 = arg2;
936
937	/*
938	 * Rule 1: Avoid unusable destinations.
939	 * XXX: we currently do not consider if an appropriate route exists.
940	 */
941	if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
942	    dst2->aio_srcsa.sa_family == AF_UNSPEC) {
943		return(-1);
944	}
945	if (dst1->aio_srcsa.sa_family == AF_UNSPEC &&
946	    dst2->aio_srcsa.sa_family != AF_UNSPEC) {
947		return(1);
948	}
949
950	/* Rule 2: Prefer matching scope. */
951	if (dst1->aio_dstscope == dst1->aio_srcscope &&
952	    dst2->aio_dstscope != dst2->aio_srcscope) {
953		return(-1);
954	}
955	if (dst1->aio_dstscope != dst1->aio_srcscope &&
956	    dst2->aio_dstscope == dst2->aio_srcscope) {
957		return(1);
958	}
959
960	/* Rule 3: Avoid deprecated addresses. */
961	if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
962	    dst2->aio_srcsa.sa_family != AF_UNSPEC) {
963		if (!(dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
964		    (dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
965			return(-1);
966		}
967		if ((dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
968		    !(dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
969			return(1);
970		}
971	}
972
973	/* Rule 4: Prefer home addresses. */
974	/* XXX: not implemented yet */
975
976	/* Rule 5: Prefer matching label. */
977#ifdef INET6
978	if (dst1->aio_srcpolicy && dst1->aio_dstpolicy &&
979	    dst1->aio_srcpolicy->pc_policy.label ==
980	    dst1->aio_dstpolicy->pc_policy.label &&
981	    (dst2->aio_srcpolicy == NULL || dst2->aio_dstpolicy == NULL ||
982	     dst2->aio_srcpolicy->pc_policy.label !=
983	     dst2->aio_dstpolicy->pc_policy.label)) {
984		return(-1);
985	}
986	if (dst2->aio_srcpolicy && dst2->aio_dstpolicy &&
987	    dst2->aio_srcpolicy->pc_policy.label ==
988	    dst2->aio_dstpolicy->pc_policy.label &&
989	    (dst1->aio_srcpolicy == NULL || dst1->aio_dstpolicy == NULL ||
990	     dst1->aio_srcpolicy->pc_policy.label !=
991	     dst1->aio_dstpolicy->pc_policy.label)) {
992		return(1);
993	}
994#endif
995
996	/* Rule 6: Prefer higher precedence. */
997#ifdef INET6
998	if (dst1->aio_dstpolicy &&
999	    (dst2->aio_dstpolicy == NULL ||
1000	     dst1->aio_dstpolicy->pc_policy.preced >
1001	     dst2->aio_dstpolicy->pc_policy.preced)) {
1002		return(-1);
1003	}
1004	if (dst2->aio_dstpolicy &&
1005	    (dst1->aio_dstpolicy == NULL ||
1006	     dst2->aio_dstpolicy->pc_policy.preced >
1007	     dst1->aio_dstpolicy->pc_policy.preced)) {
1008		return(1);
1009	}
1010#endif
1011
1012	/* Rule 7: Prefer native transport. */
1013	/* XXX: not implemented yet */
1014
1015	/* Rule 8: Prefer smaller scope. */
1016	if (dst1->aio_dstscope >= 0 &&
1017	    dst1->aio_dstscope < dst2->aio_dstscope) {
1018		return(-1);
1019	}
1020	if (dst2->aio_dstscope >= 0 &&
1021	    dst2->aio_dstscope < dst1->aio_dstscope) {
1022		return(1);
1023	}
1024
1025	/*
1026	 * Rule 9: Use longest matching prefix.
1027	 * We compare the match length in a same AF only.
1028	 */
1029	if (dst1->aio_ai->ai_addr->sa_family ==
1030	    dst2->aio_ai->ai_addr->sa_family) {
1031		if (dst1->aio_matchlen > dst2->aio_matchlen) {
1032			return(-1);
1033		}
1034		if (dst1->aio_matchlen < dst2->aio_matchlen) {
1035			return(1);
1036		}
1037	}
1038
1039	/* Rule 10: Otherwise, leave the order unchanged. */
1040	return(-1);
1041}
1042
1043/*
1044 * Copy from scope.c.
1045 * XXX: we should standardize the functions and link them as standard
1046 * library.
1047 */
1048static int
1049gai_addr2scopetype(sa)
1050	struct sockaddr *sa;
1051{
1052#ifdef INET6
1053	struct sockaddr_in6 *sa6;
1054#endif
1055	struct sockaddr_in *sa4;
1056
1057	switch(sa->sa_family) {
1058#ifdef INET6
1059	case AF_INET6:
1060		sa6 = (struct sockaddr_in6 *)sa;
1061		if (IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
1062			/* just use the scope field of the multicast address */
1063			return(sa6->sin6_addr.s6_addr[2] & 0x0f);
1064		}
1065		/*
1066		 * Unicast addresses: map scope type to corresponding scope
1067		 * value defined for multcast addresses.
1068		 * XXX: hardcoded scope type values are bad...
1069		 */
1070		if (IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr))
1071			return(1); /* node local scope */
1072		if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr))
1073			return(2); /* link-local scope */
1074		if (IN6_IS_ADDR_SITELOCAL(&sa6->sin6_addr))
1075			return(5); /* site-local scope */
1076		return(14);	/* global scope */
1077		break;
1078#endif
1079	case AF_INET:
1080		/*
1081		 * IPv4 pseudo scoping according to RFC 3484.
1082		 */
1083		sa4 = (struct sockaddr_in *)sa;
1084		/* IPv4 autoconfiguration addresses have link-local scope. */
1085		if (((u_char *)&sa4->sin_addr)[0] == 169 &&
1086		    ((u_char *)&sa4->sin_addr)[1] == 254)
1087			return(2);
1088		/* Private addresses have site-local scope. */
1089		if (((u_char *)&sa4->sin_addr)[0] == 10 ||
1090		    (((u_char *)&sa4->sin_addr)[0] == 172 &&
1091		     (((u_char *)&sa4->sin_addr)[1] & 0xf0) == 16) ||
1092		    (((u_char *)&sa4->sin_addr)[0] == 192 &&
1093		     ((u_char *)&sa4->sin_addr)[1] == 168))
1094			return(14);	/* XXX: It should be 5 unless NAT */
1095		/* Loopback addresses have link-local scope. */
1096		if (((u_char *)&sa4->sin_addr)[0] == 127)
1097			return(2);
1098		return(14);
1099		break;
1100	default:
1101		errno = EAFNOSUPPORT; /* is this a good error? */
1102		return(-1);
1103	}
1104}
1105
1106/*
1107 * hostname == NULL.
1108 * passive socket -> anyaddr (0.0.0.0 or ::)
1109 * non-passive socket -> localhost (127.0.0.1 or ::1)
1110 */
1111static int
1112explore_null(pai, servname, res)
1113	const struct addrinfo *pai;
1114	const char *servname;
1115	struct addrinfo **res;
1116{
1117	int s;
1118	const struct afd *afd;
1119	struct addrinfo *cur;
1120	struct addrinfo sentinel;
1121	int error;
1122
1123	*res = NULL;
1124	sentinel.ai_next = NULL;
1125	cur = &sentinel;
1126
1127	/*
1128	 * filter out AFs that are not supported by the kernel
1129	 * XXX errno?
1130	 */
1131	s = _socket(pai->ai_family, SOCK_DGRAM, 0);
1132	if (s < 0) {
1133		if (errno != EMFILE)
1134			return 0;
1135	} else
1136		_close(s);
1137
1138	/*
1139	 * if the servname does not match socktype/protocol, ignore it.
1140	 */
1141	if (get_portmatch(pai, servname) != 0)
1142		return 0;
1143
1144	afd = find_afd(pai->ai_family);
1145	if (afd == NULL)
1146		return 0;
1147
1148	if (pai->ai_flags & AI_PASSIVE) {
1149		GET_AI(cur->ai_next, afd, afd->a_addrany);
1150		/* xxx meaningless?
1151		 * GET_CANONNAME(cur->ai_next, "anyaddr");
1152		 */
1153		GET_PORT(cur->ai_next, servname);
1154	} else {
1155		GET_AI(cur->ai_next, afd, afd->a_loopback);
1156		/* xxx meaningless?
1157		 * GET_CANONNAME(cur->ai_next, "localhost");
1158		 */
1159		GET_PORT(cur->ai_next, servname);
1160	}
1161	cur = cur->ai_next;
1162
1163	*res = sentinel.ai_next;
1164	return 0;
1165
1166free:
1167	if (sentinel.ai_next)
1168		freeaddrinfo(sentinel.ai_next);
1169	return error;
1170}
1171
1172/*
1173 * numeric hostname
1174 */
1175static int
1176explore_numeric(pai, hostname, servname, res, canonname)
1177	const struct addrinfo *pai;
1178	const char *hostname;
1179	const char *servname;
1180	struct addrinfo **res;
1181	const char *canonname;
1182{
1183	const struct afd *afd;
1184	struct addrinfo *cur;
1185	struct addrinfo sentinel;
1186	int error;
1187	char pton[PTON_MAX];
1188
1189	*res = NULL;
1190	sentinel.ai_next = NULL;
1191	cur = &sentinel;
1192
1193	/*
1194	 * if the servname does not match socktype/protocol, ignore it.
1195	 */
1196	if (get_portmatch(pai, servname) != 0)
1197		return 0;
1198
1199	afd = find_afd(pai->ai_family);
1200	if (afd == NULL)
1201		return 0;
1202
1203	switch (afd->a_af) {
1204#if 1 /*X/Open spec*/
1205	case AF_INET:
1206		if (inet_aton(hostname, (struct in_addr *)pton) == 1) {
1207			if (pai->ai_family == afd->a_af ||
1208			    pai->ai_family == PF_UNSPEC /*?*/) {
1209				GET_AI(cur->ai_next, afd, pton);
1210				GET_PORT(cur->ai_next, servname);
1211				if ((pai->ai_flags & AI_CANONNAME)) {
1212					/*
1213					 * Set the numeric address itself as
1214					 * the canonical name, based on a
1215					 * clarification in rfc3493.
1216					 */
1217					GET_CANONNAME(cur->ai_next, canonname);
1218				}
1219				while (cur && cur->ai_next)
1220					cur = cur->ai_next;
1221			} else
1222				ERR(EAI_FAMILY);	/*xxx*/
1223		}
1224		break;
1225#endif
1226	default:
1227		if (inet_pton(afd->a_af, hostname, pton) == 1) {
1228			if (pai->ai_family == afd->a_af ||
1229			    pai->ai_family == PF_UNSPEC /*?*/) {
1230				GET_AI(cur->ai_next, afd, pton);
1231				GET_PORT(cur->ai_next, servname);
1232				if ((pai->ai_flags & AI_CANONNAME)) {
1233					/*
1234					 * Set the numeric address itself as
1235					 * the canonical name, based on a
1236					 * clarification in rfc3493.
1237					 */
1238					GET_CANONNAME(cur->ai_next, canonname);
1239				}
1240				while (cur && cur->ai_next)
1241					cur = cur->ai_next;
1242			} else
1243				ERR(EAI_FAMILY);	/* XXX */
1244		}
1245		break;
1246	}
1247
1248	*res = sentinel.ai_next;
1249	return 0;
1250
1251free:
1252bad:
1253	if (sentinel.ai_next)
1254		freeaddrinfo(sentinel.ai_next);
1255	return error;
1256}
1257
1258/*
1259 * numeric hostname with scope
1260 */
1261static int
1262explore_numeric_scope(pai, hostname, servname, res)
1263	const struct addrinfo *pai;
1264	const char *hostname;
1265	const char *servname;
1266	struct addrinfo **res;
1267{
1268#if !defined(SCOPE_DELIMITER) || !defined(INET6)
1269	return explore_numeric(pai, hostname, servname, res, hostname);
1270#else
1271	const struct afd *afd;
1272	struct addrinfo *cur;
1273	int error;
1274	char *cp, *hostname2 = NULL, *scope, *addr;
1275	struct sockaddr_in6 *sin6;
1276
1277	/*
1278	 * if the servname does not match socktype/protocol, ignore it.
1279	 */
1280	if (get_portmatch(pai, servname) != 0)
1281		return 0;
1282
1283	afd = find_afd(pai->ai_family);
1284	if (afd == NULL)
1285		return 0;
1286
1287	if (!afd->a_scoped)
1288		return explore_numeric(pai, hostname, servname, res, hostname);
1289
1290	cp = strchr(hostname, SCOPE_DELIMITER);
1291	if (cp == NULL)
1292		return explore_numeric(pai, hostname, servname, res, hostname);
1293
1294	/*
1295	 * Handle special case of <scoped_address><delimiter><scope id>
1296	 */
1297	hostname2 = strdup(hostname);
1298	if (hostname2 == NULL)
1299		return EAI_MEMORY;
1300	/* terminate at the delimiter */
1301	hostname2[cp - hostname] = '\0';
1302	addr = hostname2;
1303	scope = cp + 1;
1304
1305	error = explore_numeric(pai, addr, servname, res, hostname);
1306	if (error == 0) {
1307		u_int32_t scopeid;
1308
1309		for (cur = *res; cur; cur = cur->ai_next) {
1310			if (cur->ai_family != AF_INET6)
1311				continue;
1312			sin6 = (struct sockaddr_in6 *)(void *)cur->ai_addr;
1313			if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
1314				free(hostname2);
1315				return(EAI_NONAME); /* XXX: is return OK? */
1316			}
1317			sin6->sin6_scope_id = scopeid;
1318		}
1319	}
1320
1321	free(hostname2);
1322
1323	return error;
1324#endif
1325}
1326
1327static int
1328get_canonname(pai, ai, str)
1329	const struct addrinfo *pai;
1330	struct addrinfo *ai;
1331	const char *str;
1332{
1333	if ((pai->ai_flags & AI_CANONNAME) != 0) {
1334		ai->ai_canonname = strdup(str);
1335		if (ai->ai_canonname == NULL)
1336			return EAI_MEMORY;
1337	}
1338	return 0;
1339}
1340
1341static struct addrinfo *
1342get_ai(pai, afd, addr)
1343	const struct addrinfo *pai;
1344	const struct afd *afd;
1345	const char *addr;
1346{
1347	char *p;
1348	struct addrinfo *ai;
1349#ifdef FAITH
1350	struct in6_addr faith_prefix;
1351	char *fp_str;
1352	int translate = 0;
1353#endif
1354
1355#ifdef FAITH
1356	/*
1357	 * Transfrom an IPv4 addr into a special IPv6 addr format for
1358	 * IPv6->IPv4 translation gateway. (only TCP is supported now)
1359	 *
1360	 * +-----------------------------------+------------+
1361	 * | faith prefix part (12 bytes)      | embedded   |
1362	 * |                                   | IPv4 addr part (4 bytes)
1363	 * +-----------------------------------+------------+
1364	 *
1365	 * faith prefix part is specified as ascii IPv6 addr format
1366	 * in environmental variable GAI.
1367	 * For FAITH to work correctly, routing to faith prefix must be
1368	 * setup toward a machine where a FAITH daemon operates.
1369	 * Also, the machine must enable some mechanizm
1370	 * (e.g. faith interface hack) to divert those packet with
1371	 * faith prefixed destination addr to user-land FAITH daemon.
1372	 */
1373	fp_str = getenv("GAI");
1374	if (fp_str && inet_pton(AF_INET6, fp_str, &faith_prefix) == 1 &&
1375	    afd->a_af == AF_INET && pai->ai_socktype == SOCK_STREAM) {
1376		u_int32_t v4a;
1377		u_int8_t v4a_top;
1378
1379		memcpy(&v4a, addr, sizeof v4a);
1380		v4a_top = v4a >> IN_CLASSA_NSHIFT;
1381		if (!IN_MULTICAST(v4a) && !IN_EXPERIMENTAL(v4a) &&
1382		    v4a_top != 0 && v4a != IN_LOOPBACKNET) {
1383			afd = &afdl[N_INET6];
1384			memcpy(&faith_prefix.s6_addr[12], addr,
1385			       sizeof(struct in_addr));
1386			translate = 1;
1387		}
1388	}
1389#endif
1390
1391	ai = (struct addrinfo *)malloc(sizeof(struct addrinfo)
1392		+ (afd->a_socklen));
1393	if (ai == NULL)
1394		return NULL;
1395
1396	memcpy(ai, pai, sizeof(struct addrinfo));
1397	ai->ai_addr = (struct sockaddr *)(void *)(ai + 1);
1398	memset(ai->ai_addr, 0, (size_t)afd->a_socklen);
1399	ai->ai_addr->sa_len = afd->a_socklen;
1400	ai->ai_addrlen = afd->a_socklen;
1401	ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
1402	p = (char *)(void *)(ai->ai_addr);
1403#ifdef FAITH
1404	if (translate == 1)
1405		memcpy(p + afd->a_off, &faith_prefix, (size_t)afd->a_addrlen);
1406	else
1407#endif
1408	memcpy(p + afd->a_off, addr, (size_t)afd->a_addrlen);
1409	return ai;
1410}
1411
1412static int
1413get_portmatch(ai, servname)
1414	const struct addrinfo *ai;
1415	const char *servname;
1416{
1417
1418	/* get_port does not touch first argument when matchonly == 1. */
1419	/* LINTED const cast */
1420	return get_port((struct addrinfo *)ai, servname, 1);
1421}
1422
1423static int
1424get_port(ai, servname, matchonly)
1425	struct addrinfo *ai;
1426	const char *servname;
1427	int matchonly;
1428{
1429	const char *proto;
1430	struct servent *sp;
1431	int port;
1432	int allownumeric;
1433
1434	if (servname == NULL)
1435		return 0;
1436	switch (ai->ai_family) {
1437	case AF_INET:
1438#ifdef AF_INET6
1439	case AF_INET6:
1440#endif
1441		break;
1442	default:
1443		return 0;
1444	}
1445
1446	switch (ai->ai_socktype) {
1447	case SOCK_RAW:
1448		return EAI_SERVICE;
1449	case SOCK_DGRAM:
1450	case SOCK_STREAM:
1451		allownumeric = 1;
1452		break;
1453	case ANY:
1454		allownumeric = 0;
1455		break;
1456	default:
1457		return EAI_SOCKTYPE;
1458	}
1459
1460	port = str2number(servname);
1461	if (port >= 0) {
1462		if (!allownumeric)
1463			return EAI_SERVICE;
1464		if (port < 0 || port > 65535)
1465			return EAI_SERVICE;
1466		port = htons(port);
1467	} else {
1468		if (ai->ai_flags & AI_NUMERICSERV)
1469			return EAI_NONAME;
1470		switch (ai->ai_socktype) {
1471		case SOCK_DGRAM:
1472			proto = "udp";
1473			break;
1474		case SOCK_STREAM:
1475			proto = "tcp";
1476			break;
1477		default:
1478			proto = NULL;
1479			break;
1480		}
1481
1482		THREAD_LOCK();
1483		if ((sp = getservbyname(servname, proto)) == NULL) {
1484			THREAD_UNLOCK();
1485			return EAI_SERVICE;
1486		}
1487		port = sp->s_port;
1488		THREAD_UNLOCK();
1489	}
1490
1491	if (!matchonly) {
1492		switch (ai->ai_family) {
1493		case AF_INET:
1494			((struct sockaddr_in *)(void *)
1495			    ai->ai_addr)->sin_port = port;
1496			break;
1497#ifdef INET6
1498		case AF_INET6:
1499			((struct sockaddr_in6 *)(void *)
1500			    ai->ai_addr)->sin6_port = port;
1501			break;
1502#endif
1503		}
1504	}
1505
1506	return 0;
1507}
1508
1509static const struct afd *
1510find_afd(af)
1511	int af;
1512{
1513	const struct afd *afd;
1514
1515	if (af == PF_UNSPEC)
1516		return NULL;
1517	for (afd = afdl; afd->a_af; afd++) {
1518		if (afd->a_af == af)
1519			return afd;
1520	}
1521	return NULL;
1522}
1523
1524/*
1525 * post-2553: AI_ADDRCONFIG check.  if we use getipnodeby* as backend, backend
1526 * will take care of it.
1527 * the semantics of AI_ADDRCONFIG is not defined well.  we are not sure
1528 * if the code is right or not.
1529 *
1530 * XXX PF_UNSPEC -> PF_INET6 + PF_INET mapping needs to be in sync with
1531 * _dns_getaddrinfo.
1532 */
1533static int
1534addrconfig(pai)
1535	struct addrinfo *pai;
1536{
1537	int s, af;
1538
1539	/*
1540	 * TODO:
1541	 * Note that implementation dependent test for address
1542	 * configuration should be done everytime called
1543	 * (or apropriate interval),
1544	 * because addresses will be dynamically assigned or deleted.
1545	 */
1546	af = pai->ai_family;
1547	if (af == AF_UNSPEC) {
1548		if ((s = _socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1549			af = AF_INET;
1550		else {
1551			_close(s);
1552			if ((s = _socket(AF_INET, SOCK_DGRAM, 0)) < 0)
1553				af = AF_INET6;
1554			else
1555				_close(s);
1556		}
1557	}
1558	if (af != AF_UNSPEC) {
1559		if ((s = _socket(af, SOCK_DGRAM, 0)) < 0)
1560			return 0;
1561		_close(s);
1562	}
1563	pai->ai_family = af;
1564	return 1;
1565}
1566
1567#ifdef INET6
1568/* convert a string to a scope identifier. XXX: IPv6 specific */
1569static int
1570ip6_str2scopeid(scope, sin6, scopeid)
1571	char *scope;
1572	struct sockaddr_in6 *sin6;
1573	u_int32_t *scopeid;
1574{
1575	u_long lscopeid;
1576	struct in6_addr *a6;
1577	char *ep;
1578
1579	a6 = &sin6->sin6_addr;
1580
1581	/* empty scopeid portion is invalid */
1582	if (*scope == '\0')
1583		return -1;
1584
1585	if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
1586		/*
1587		 * We currently assume a one-to-one mapping between links
1588		 * and interfaces, so we simply use interface indices for
1589		 * like-local scopes.
1590		 */
1591		*scopeid = if_nametoindex(scope);
1592		if (*scopeid == 0)
1593			goto trynumeric;
1594		return 0;
1595	}
1596
1597	/* still unclear about literal, allow numeric only - placeholder */
1598	if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6))
1599		goto trynumeric;
1600	if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
1601		goto trynumeric;
1602	else
1603		goto trynumeric;	/* global */
1604
1605	/* try to convert to a numeric id as a last resort */
1606  trynumeric:
1607	errno = 0;
1608	lscopeid = strtoul(scope, &ep, 10);
1609	*scopeid = (u_int32_t)(lscopeid & 0xffffffffUL);
1610	if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
1611		return 0;
1612	else
1613		return -1;
1614}
1615#endif
1616
1617/*
1618 * FQDN hostname, DNS lookup
1619 */
1620static int
1621explore_fqdn(pai, hostname, servname, res)
1622	const struct addrinfo *pai;
1623	const char *hostname;
1624	const char *servname;
1625	struct addrinfo **res;
1626{
1627	struct addrinfo *result;
1628	struct addrinfo *cur;
1629	int error = 0;
1630	static const ns_dtab dtab[] = {
1631		NS_FILES_CB(_files_getaddrinfo, NULL)
1632		{ NSSRC_DNS, _dns_getaddrinfo, NULL },	/* force -DHESIOD */
1633		NS_NIS_CB(_yp_getaddrinfo, NULL)
1634		{ 0 }
1635	};
1636
1637	result = NULL;
1638
1639	/*
1640	 * if the servname does not match socktype/protocol, ignore it.
1641	 */
1642	if (get_portmatch(pai, servname) != 0)
1643		return 0;
1644
1645	switch (_nsdispatch(&result, dtab, NSDB_HOSTS, "getaddrinfo",
1646			default_dns_files, hostname, pai)) {
1647	case NS_TRYAGAIN:
1648		error = EAI_AGAIN;
1649		goto free;
1650	case NS_UNAVAIL:
1651		error = EAI_FAIL;
1652		goto free;
1653	case NS_NOTFOUND:
1654		error = EAI_NONAME;
1655		goto free;
1656	case NS_SUCCESS:
1657		error = 0;
1658		for (cur = result; cur; cur = cur->ai_next) {
1659			GET_PORT(cur, servname);
1660			/* canonname should be filled already */
1661		}
1662		break;
1663	}
1664
1665	*res = result;
1666
1667	return 0;
1668
1669free:
1670	if (result)
1671		freeaddrinfo(result);
1672	return error;
1673}
1674
1675#ifdef DEBUG
1676static const char AskedForGot[] =
1677	"gethostby*.getanswer: asked for \"%s\", got \"%s\"";
1678#endif
1679static FILE *hostf = NULL;
1680
1681static struct addrinfo *
1682getanswer(answer, anslen, qname, qtype, pai)
1683	const querybuf *answer;
1684	int anslen;
1685	const char *qname;
1686	int qtype;
1687	const struct addrinfo *pai;
1688{
1689	struct addrinfo sentinel, *cur;
1690	struct addrinfo ai;
1691	const struct afd *afd;
1692	char *canonname;
1693	const HEADER *hp;
1694	const u_char *cp;
1695	int n;
1696	const u_char *eom;
1697	char *bp, *ep;
1698	int type, class, ancount, qdcount;
1699	int haveanswer, had_error;
1700	char tbuf[MAXDNAME];
1701	int (*name_ok)(const char *);
1702	char hostbuf[8*1024];
1703
1704	memset(&sentinel, 0, sizeof(sentinel));
1705	cur = &sentinel;
1706
1707	canonname = NULL;
1708	eom = answer->buf + anslen;
1709	switch (qtype) {
1710	case T_A:
1711	case T_AAAA:
1712	case T_ANY:	/*use T_ANY only for T_A/T_AAAA lookup*/
1713		name_ok = res_hnok;
1714		break;
1715	default:
1716		return (NULL);	/* XXX should be abort(); */
1717	}
1718	/*
1719	 * find first satisfactory answer
1720	 */
1721	hp = &answer->hdr;
1722	ancount = ntohs(hp->ancount);
1723	qdcount = ntohs(hp->qdcount);
1724	bp = hostbuf;
1725	ep = hostbuf + sizeof hostbuf;
1726	cp = answer->buf + HFIXEDSZ;
1727	if (qdcount != 1) {
1728		h_errno = NO_RECOVERY;
1729		return (NULL);
1730	}
1731	n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1732	if ((n < 0) || !(*name_ok)(bp)) {
1733		h_errno = NO_RECOVERY;
1734		return (NULL);
1735	}
1736	cp += n + QFIXEDSZ;
1737	if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
1738		/* res_send() has already verified that the query name is the
1739		 * same as the one we sent; this just gets the expanded name
1740		 * (i.e., with the succeeding search-domain tacked on).
1741		 */
1742		n = strlen(bp) + 1;		/* for the \0 */
1743		if (n >= MAXHOSTNAMELEN) {
1744			h_errno = NO_RECOVERY;
1745			return (NULL);
1746		}
1747		canonname = bp;
1748		bp += n;
1749		/* The qname can be abbreviated, but h_name is now absolute. */
1750		qname = canonname;
1751	}
1752	haveanswer = 0;
1753	had_error = 0;
1754	while (ancount-- > 0 && cp < eom && !had_error) {
1755		n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1756		if ((n < 0) || !(*name_ok)(bp)) {
1757			had_error++;
1758			continue;
1759		}
1760		cp += n;			/* name */
1761		type = _getshort(cp);
1762 		cp += INT16SZ;			/* type */
1763		class = _getshort(cp);
1764 		cp += INT16SZ + INT32SZ;	/* class, TTL */
1765		n = _getshort(cp);
1766		cp += INT16SZ;			/* len */
1767		if (class != C_IN) {
1768			/* XXX - debug? syslog? */
1769			cp += n;
1770			continue;		/* XXX - had_error++ ? */
1771		}
1772		if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) &&
1773		    type == T_CNAME) {
1774			n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
1775			if ((n < 0) || !(*name_ok)(tbuf)) {
1776				had_error++;
1777				continue;
1778			}
1779			cp += n;
1780			/* Get canonical name. */
1781			n = strlen(tbuf) + 1;	/* for the \0 */
1782			if (n > ep - bp || n >= MAXHOSTNAMELEN) {
1783				had_error++;
1784				continue;
1785			}
1786			strlcpy(bp, tbuf, ep - bp);
1787			canonname = bp;
1788			bp += n;
1789			continue;
1790		}
1791		if (qtype == T_ANY) {
1792			if (!(type == T_A || type == T_AAAA)) {
1793				cp += n;
1794				continue;
1795			}
1796		} else if (type != qtype) {
1797#ifdef DEBUG
1798			if (type != T_KEY && type != T_SIG)
1799				syslog(LOG_NOTICE|LOG_AUTH,
1800	       "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
1801				       qname, p_class(C_IN), p_type(qtype),
1802				       p_type(type));
1803#endif
1804			cp += n;
1805			continue;		/* XXX - had_error++ ? */
1806		}
1807		switch (type) {
1808		case T_A:
1809		case T_AAAA:
1810			if (strcasecmp(canonname, bp) != 0) {
1811#ifdef DEBUG
1812				syslog(LOG_NOTICE|LOG_AUTH,
1813				       AskedForGot, canonname, bp);
1814#endif
1815				cp += n;
1816				continue;	/* XXX - had_error++ ? */
1817			}
1818			if (type == T_A && n != INADDRSZ) {
1819				cp += n;
1820				continue;
1821			}
1822			if (type == T_AAAA && n != IN6ADDRSZ) {
1823				cp += n;
1824				continue;
1825			}
1826#ifdef FILTER_V4MAPPED
1827			if (type == T_AAAA) {
1828				struct in6_addr in6;
1829				memcpy(&in6, cp, sizeof(in6));
1830				if (IN6_IS_ADDR_V4MAPPED(&in6)) {
1831					cp += n;
1832					continue;
1833				}
1834			}
1835#endif
1836			if (!haveanswer) {
1837				int nn;
1838
1839				canonname = bp;
1840				nn = strlen(bp) + 1;	/* for the \0 */
1841				bp += nn;
1842			}
1843
1844			/* don't overwrite pai */
1845			ai = *pai;
1846			ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
1847			afd = find_afd(ai.ai_family);
1848			if (afd == NULL) {
1849				cp += n;
1850				continue;
1851			}
1852			cur->ai_next = get_ai(&ai, afd, (const char *)cp);
1853			if (cur->ai_next == NULL)
1854				had_error++;
1855			while (cur && cur->ai_next)
1856				cur = cur->ai_next;
1857			cp += n;
1858			break;
1859		default:
1860			abort();
1861		}
1862		if (!had_error)
1863			haveanswer++;
1864	}
1865	if (haveanswer) {
1866#if defined(RESOLVSORT)
1867		/*
1868		 * We support only IPv4 address for backward
1869		 * compatibility against gethostbyname(3).
1870		 */
1871		if (_res.nsort && qtype == T_A) {
1872			if (addr4sort(&sentinel) < 0) {
1873				freeaddrinfo(sentinel.ai_next);
1874				h_errno = NO_RECOVERY;
1875				return NULL;
1876			}
1877		}
1878#endif /*RESOLVSORT*/
1879		if (!canonname)
1880			(void)get_canonname(pai, sentinel.ai_next, qname);
1881		else
1882			(void)get_canonname(pai, sentinel.ai_next, canonname);
1883		h_errno = NETDB_SUCCESS;
1884		return sentinel.ai_next;
1885	}
1886
1887	h_errno = NO_RECOVERY;
1888	return NULL;
1889}
1890
1891#ifdef RESOLVSORT
1892struct addr_ptr {
1893	struct addrinfo *ai;
1894	int aval;
1895};
1896
1897static int
1898addr4sort(struct addrinfo *sentinel)
1899{
1900	struct addrinfo *ai;
1901	struct addr_ptr *addrs, addr;
1902	struct sockaddr_in *sin;
1903	int naddrs, i, j;
1904	int needsort = 0;
1905
1906	if (!sentinel)
1907		return -1;
1908	naddrs = 0;
1909	for (ai = sentinel->ai_next; ai; ai = ai->ai_next)
1910		naddrs++;
1911	if (naddrs < 2)
1912		return 0;		/* We don't need sorting. */
1913	if ((addrs = malloc(sizeof(struct addr_ptr) * naddrs)) == NULL)
1914		return -1;
1915	i = 0;
1916	for (ai = sentinel->ai_next; ai; ai = ai->ai_next) {
1917		sin = (struct sockaddr_in *)ai->ai_addr;
1918		for (j = 0; (unsigned)j < _res.nsort; j++) {
1919			if (_res.sort_list[j].addr.s_addr ==
1920			    (sin->sin_addr.s_addr & _res.sort_list[j].mask))
1921				break;
1922		}
1923		addrs[i].ai = ai;
1924		addrs[i].aval = j;
1925		if (needsort == 0 && i > 0 && j < addrs[i - 1].aval)
1926			needsort = i;
1927		i++;
1928	}
1929	if (!needsort) {
1930		free(addrs);
1931		return 0;
1932	}
1933
1934	while (needsort < naddrs) {
1935	    for (j = needsort - 1; j >= 0; j--) {
1936		if (addrs[j].aval > addrs[j+1].aval) {
1937		    addr = addrs[j];
1938		    addrs[j] = addrs[j + 1];
1939		    addrs[j + 1] = addr;
1940		} else
1941		    break;
1942	    }
1943	    needsort++;
1944	}
1945
1946	ai = sentinel;
1947	for (i = 0; i < naddrs; ++i) {
1948		ai->ai_next = addrs[i].ai;
1949		ai = ai->ai_next;
1950	}
1951	ai->ai_next = NULL;
1952	free(addrs);
1953	return 0;
1954}
1955#endif /*RESOLVSORT*/
1956
1957/*ARGSUSED*/
1958static int
1959_dns_getaddrinfo(rv, cb_data, ap)
1960	void	*rv;
1961	void	*cb_data;
1962	va_list	 ap;
1963{
1964	struct addrinfo *ai;
1965	querybuf *buf, *buf2;
1966	const char *hostname;
1967	const struct addrinfo *pai;
1968	struct addrinfo sentinel, *cur;
1969	struct res_target q, q2;
1970
1971	hostname = va_arg(ap, char *);
1972	pai = va_arg(ap, const struct addrinfo *);
1973
1974	memset(&q, 0, sizeof(q2));
1975	memset(&q2, 0, sizeof(q2));
1976	memset(&sentinel, 0, sizeof(sentinel));
1977	cur = &sentinel;
1978
1979	buf = malloc(sizeof(*buf));
1980	if (!buf) {
1981		h_errno = NETDB_INTERNAL;
1982		return NS_NOTFOUND;
1983	}
1984	buf2 = malloc(sizeof(*buf2));
1985	if (!buf2) {
1986		free(buf);
1987		h_errno = NETDB_INTERNAL;
1988		return NS_NOTFOUND;
1989	}
1990
1991	switch (pai->ai_family) {
1992	case AF_UNSPEC:
1993		q.name = hostname;
1994		q.qclass = C_IN;
1995		q.qtype = T_A;
1996		q.answer = buf->buf;
1997		q.anslen = sizeof(buf->buf);
1998		q.next = &q2;
1999		q2.name = hostname;
2000		q2.qclass = C_IN;
2001		q2.qtype = T_AAAA;
2002		q2.answer = buf2->buf;
2003		q2.anslen = sizeof(buf2->buf);
2004		break;
2005	case AF_INET:
2006		q.name = hostname;
2007		q.qclass = C_IN;
2008		q.qtype = T_A;
2009		q.answer = buf->buf;
2010		q.anslen = sizeof(buf->buf);
2011		break;
2012	case AF_INET6:
2013		q.name = hostname;
2014		q.qclass = C_IN;
2015		q.qtype = T_AAAA;
2016		q.answer = buf->buf;
2017		q.anslen = sizeof(buf->buf);
2018		break;
2019	default:
2020		free(buf);
2021		free(buf2);
2022		return NS_UNAVAIL;
2023	}
2024	if (res_searchN(hostname, &q) < 0) {
2025		free(buf);
2026		free(buf2);
2027		return NS_NOTFOUND;
2028	}
2029	/* prefer IPv6 */
2030	if (q.next) {
2031		ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai);
2032		if (ai) {
2033			cur->ai_next = ai;
2034			while (cur && cur->ai_next)
2035				cur = cur->ai_next;
2036		}
2037	}
2038	ai = getanswer(buf, q.n, q.name, q.qtype, pai);
2039	if (ai)
2040		cur->ai_next = ai;
2041	free(buf);
2042	free(buf2);
2043	if (sentinel.ai_next == NULL)
2044		switch (h_errno) {
2045		case HOST_NOT_FOUND:
2046			return NS_NOTFOUND;
2047		case TRY_AGAIN:
2048			return NS_TRYAGAIN;
2049		default:
2050			return NS_UNAVAIL;
2051		}
2052	*((struct addrinfo **)rv) = sentinel.ai_next;
2053	return NS_SUCCESS;
2054}
2055
2056static void
2057_sethtent()
2058{
2059	if (!hostf)
2060		hostf = fopen(_PATH_HOSTS, "r" );
2061	else
2062		rewind(hostf);
2063}
2064
2065static void
2066_endhtent()
2067{
2068	if (hostf) {
2069		(void) fclose(hostf);
2070		hostf = NULL;
2071	}
2072}
2073
2074static struct addrinfo *
2075_gethtent(name, pai)
2076	const char *name;
2077	const struct addrinfo *pai;
2078{
2079	char *p;
2080	char *cp, *tname, *cname;
2081	struct addrinfo hints, *res0, *res;
2082	int error;
2083	const char *addr;
2084	char hostbuf[8*1024];
2085
2086	if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" )))
2087		return (NULL);
2088again:
2089	if (!(p = fgets(hostbuf, sizeof hostbuf, hostf)))
2090		return (NULL);
2091	if (*p == '#')
2092		goto again;
2093	cp = strpbrk(p, "#\n");
2094	if (cp != NULL)
2095		*cp = '\0';
2096	if (!(cp = strpbrk(p, " \t")))
2097		goto again;
2098	*cp++ = '\0';
2099	addr = p;
2100	cname = NULL;
2101	/* if this is not something we're looking for, skip it. */
2102	while (cp && *cp) {
2103		if (*cp == ' ' || *cp == '\t') {
2104			cp++;
2105			continue;
2106		}
2107		tname = cp;
2108		if (cname == NULL)
2109			cname = cp;
2110		if ((cp = strpbrk(cp, " \t")) != NULL)
2111			*cp++ = '\0';
2112		if (strcasecmp(name, tname) == 0)
2113			goto found;
2114	}
2115	goto again;
2116
2117found:
2118	/* we should not glob socktype/protocol here */
2119	memset(&hints, 0, sizeof(hints));
2120	hints.ai_family = pai->ai_family;
2121	hints.ai_socktype = SOCK_DGRAM;
2122	hints.ai_protocol = 0;
2123	hints.ai_flags = AI_NUMERICHOST;
2124	error = getaddrinfo(addr, "0", &hints, &res0);
2125	if (error)
2126		goto again;
2127#ifdef FILTER_V4MAPPED
2128	/* XXX should check all items in the chain */
2129	if (res0->ai_family == AF_INET6 &&
2130	    IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)res0->ai_addr)->sin6_addr)) {
2131		freeaddrinfo(res0);
2132		goto again;
2133	}
2134#endif
2135	for (res = res0; res; res = res->ai_next) {
2136		/* cover it up */
2137		res->ai_flags = pai->ai_flags;
2138		res->ai_socktype = pai->ai_socktype;
2139		res->ai_protocol = pai->ai_protocol;
2140
2141		if (pai->ai_flags & AI_CANONNAME) {
2142			if (get_canonname(pai, res, cname) != 0) {
2143				freeaddrinfo(res0);
2144				goto again;
2145			}
2146		}
2147	}
2148	return res0;
2149}
2150
2151/*ARGSUSED*/
2152static int
2153_files_getaddrinfo(rv, cb_data, ap)
2154	void	*rv;
2155	void	*cb_data;
2156	va_list	 ap;
2157{
2158	const char *name;
2159	const struct addrinfo *pai;
2160	struct addrinfo sentinel, *cur;
2161	struct addrinfo *p;
2162
2163	name = va_arg(ap, char *);
2164	pai = va_arg(ap, struct addrinfo *);
2165
2166	memset(&sentinel, 0, sizeof(sentinel));
2167	cur = &sentinel;
2168
2169	THREAD_LOCK();
2170	_sethtent();
2171	while ((p = _gethtent(name, pai)) != NULL) {
2172		cur->ai_next = p;
2173		while (cur && cur->ai_next)
2174			cur = cur->ai_next;
2175	}
2176	_endhtent();
2177	THREAD_UNLOCK();
2178
2179	*((struct addrinfo **)rv) = sentinel.ai_next;
2180	if (sentinel.ai_next == NULL)
2181		return NS_NOTFOUND;
2182	return NS_SUCCESS;
2183}
2184
2185#ifdef YP
2186static char *__ypdomain;
2187
2188/*ARGSUSED*/
2189static struct addrinfo *
2190_yphostent(line, pai)
2191	char *line;
2192	const struct addrinfo *pai;
2193{
2194	struct addrinfo sentinel, *cur;
2195	struct addrinfo hints, *res, *res0;
2196	int error;
2197	char *p = line;
2198	const char *addr, *canonname;
2199	char *nextline;
2200	char *cp;
2201
2202	addr = canonname = NULL;
2203
2204	memset(&sentinel, 0, sizeof(sentinel));
2205	cur = &sentinel;
2206
2207nextline:
2208	/* terminate line */
2209	cp = strchr(p, '\n');
2210	if (cp) {
2211		*cp++ = '\0';
2212		nextline = cp;
2213	} else
2214		nextline = NULL;
2215
2216	cp = strpbrk(p, " \t");
2217	if (cp == NULL) {
2218		if (canonname == NULL)
2219			return (NULL);
2220		else
2221			goto done;
2222	}
2223	*cp++ = '\0';
2224
2225	addr = p;
2226
2227	while (cp && *cp) {
2228		if (*cp == ' ' || *cp == '\t') {
2229			cp++;
2230			continue;
2231		}
2232		if (!canonname)
2233			canonname = cp;
2234		if ((cp = strpbrk(cp, " \t")) != NULL)
2235			*cp++ = '\0';
2236	}
2237
2238	hints = *pai;
2239	hints.ai_flags = AI_NUMERICHOST;
2240	error = getaddrinfo(addr, NULL, &hints, &res0);
2241	if (error == 0) {
2242		for (res = res0; res; res = res->ai_next) {
2243			/* cover it up */
2244			res->ai_flags = pai->ai_flags;
2245
2246			if (pai->ai_flags & AI_CANONNAME)
2247				(void)get_canonname(pai, res, canonname);
2248		}
2249	} else
2250		res0 = NULL;
2251	if (res0) {
2252		cur->ai_next = res0;
2253		while (cur && cur->ai_next)
2254			cur = cur->ai_next;
2255	}
2256
2257	if (nextline) {
2258		p = nextline;
2259		goto nextline;
2260	}
2261
2262done:
2263	return sentinel.ai_next;
2264}
2265
2266/*ARGSUSED*/
2267static int
2268_yp_getaddrinfo(rv, cb_data, ap)
2269	void	*rv;
2270	void	*cb_data;
2271	va_list	 ap;
2272{
2273	struct addrinfo sentinel, *cur;
2274	struct addrinfo *ai = NULL;
2275	static char *__ypcurrent;
2276	int __ypcurrentlen, r;
2277	const char *name;
2278	const struct addrinfo *pai;
2279
2280	name = va_arg(ap, char *);
2281	pai = va_arg(ap, const struct addrinfo *);
2282
2283	memset(&sentinel, 0, sizeof(sentinel));
2284	cur = &sentinel;
2285
2286	THREAD_LOCK();
2287	if (!__ypdomain) {
2288		if (_yp_check(&__ypdomain) == 0) {
2289			THREAD_UNLOCK();
2290			return NS_UNAVAIL;
2291		}
2292	}
2293	if (__ypcurrent)
2294		free(__ypcurrent);
2295	__ypcurrent = NULL;
2296
2297	/* hosts.byname is only for IPv4 (Solaris8) */
2298	if (pai->ai_family == PF_UNSPEC || pai->ai_family == PF_INET) {
2299		r = yp_match(__ypdomain, "hosts.byname", name,
2300			(int)strlen(name), &__ypcurrent, &__ypcurrentlen);
2301		if (r == 0) {
2302			struct addrinfo ai4;
2303
2304			ai4 = *pai;
2305			ai4.ai_family = AF_INET;
2306			ai = _yphostent(__ypcurrent, &ai4);
2307			if (ai) {
2308				cur->ai_next = ai;
2309				while (cur && cur->ai_next)
2310					cur = cur->ai_next;
2311			}
2312		}
2313	}
2314
2315	/* ipnodes.byname can hold both IPv4/v6 */
2316	r = yp_match(__ypdomain, "ipnodes.byname", name,
2317		(int)strlen(name), &__ypcurrent, &__ypcurrentlen);
2318	if (r == 0) {
2319		ai = _yphostent(__ypcurrent, pai);
2320		if (ai) {
2321			cur->ai_next = ai;
2322			while (cur && cur->ai_next)
2323				cur = cur->ai_next;
2324		}
2325	}
2326	THREAD_UNLOCK();
2327
2328	if (sentinel.ai_next == NULL) {
2329		h_errno = HOST_NOT_FOUND;
2330		return NS_NOTFOUND;
2331	}
2332	*((struct addrinfo **)rv) = sentinel.ai_next;
2333	return NS_SUCCESS;
2334}
2335#endif
2336
2337/* resolver logic */
2338
2339extern const char *__hostalias(const char *);
2340
2341/*
2342 * Formulate a normal query, send, and await answer.
2343 * Returned answer is placed in supplied buffer "answer".
2344 * Perform preliminary check of answer, returning success only
2345 * if no error is indicated and the answer count is nonzero.
2346 * Return the size of the response on success, -1 on error.
2347 * Error number is left in h_errno.
2348 *
2349 * Caller must parse answer and determine whether it answers the question.
2350 */
2351static int
2352res_queryN(name, target)
2353	const char *name;	/* domain name */
2354	struct res_target *target;
2355{
2356	u_char *buf;
2357	HEADER *hp;
2358	int n;
2359	struct res_target *t;
2360	int rcode;
2361	int ancount;
2362
2363	rcode = NOERROR;
2364	ancount = 0;
2365
2366	if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
2367		h_errno = NETDB_INTERNAL;
2368		return (-1);
2369	}
2370
2371	buf = malloc(MAXPACKET);
2372	if (!buf) {
2373		h_errno = NETDB_INTERNAL;
2374		return -1;
2375	}
2376
2377	for (t = target; t; t = t->next) {
2378		int class, type;
2379		u_char *answer;
2380		int anslen;
2381
2382		hp = (HEADER *)(void *)t->answer;
2383		hp->rcode = NOERROR;	/* default */
2384
2385		/* make it easier... */
2386		class = t->qclass;
2387		type = t->qtype;
2388		answer = t->answer;
2389		anslen = t->anslen;
2390#ifdef DEBUG
2391		if (_res.options & RES_DEBUG)
2392			printf(";; res_query(%s, %d, %d)\n", name, class, type);
2393#endif
2394
2395		n = res_mkquery(QUERY, name, class, type, NULL, 0, NULL,
2396		    buf, MAXPACKET);
2397		if (n > 0 && (_res.options & RES_USE_EDNS0) != 0)
2398			n = res_opt(n, buf, MAXPACKET, anslen);
2399		if (n <= 0) {
2400#ifdef DEBUG
2401			if (_res.options & RES_DEBUG)
2402				printf(";; res_query: mkquery failed\n");
2403#endif
2404			free(buf);
2405			h_errno = NO_RECOVERY;
2406			return (n);
2407		}
2408		n = res_send(buf, n, answer, anslen);
2409#if 0
2410		if (n < 0) {
2411#ifdef DEBUG
2412			if (_res.options & RES_DEBUG)
2413				printf(";; res_query: send error\n");
2414#endif
2415			free(buf);
2416			h_errno = TRY_AGAIN;
2417			return (n);
2418		}
2419#endif
2420
2421		if (n < 0 || n > anslen)
2422			hp->rcode = FORMERR; /* XXX not very informative */
2423		if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
2424			rcode = hp->rcode;	/* record most recent error */
2425#ifdef DEBUG
2426			if (_res.options & RES_DEBUG)
2427				printf(";; rcode = %u, ancount=%u\n", hp->rcode,
2428				    ntohs(hp->ancount));
2429#endif
2430			continue;
2431		}
2432
2433		ancount += ntohs(hp->ancount);
2434
2435		t->n = n;
2436	}
2437
2438	free(buf);
2439
2440	if (ancount == 0) {
2441		switch (rcode) {
2442		case NXDOMAIN:
2443			h_errno = HOST_NOT_FOUND;
2444			break;
2445		case SERVFAIL:
2446			h_errno = TRY_AGAIN;
2447			break;
2448		case NOERROR:
2449			h_errno = NO_DATA;
2450			break;
2451		case FORMERR:
2452		case NOTIMP:
2453		case REFUSED:
2454		default:
2455			h_errno = NO_RECOVERY;
2456			break;
2457		}
2458		return (-1);
2459	}
2460	return (ancount);
2461}
2462
2463/*
2464 * Formulate a normal query, send, and retrieve answer in supplied buffer.
2465 * Return the size of the response on success, -1 on error.
2466 * If enabled, implement search rules until answer or unrecoverable failure
2467 * is detected.  Error code, if any, is left in h_errno.
2468 */
2469static int
2470res_searchN(name, target)
2471	const char *name;	/* domain name */
2472	struct res_target *target;
2473{
2474	const char *cp, * const *domain;
2475	HEADER *hp = (HEADER *)(void *)target->answer;	/*XXX*/
2476	u_int dots;
2477	int trailing_dot, ret, saved_herrno;
2478	int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
2479
2480	if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
2481		h_errno = NETDB_INTERNAL;
2482		return (-1);
2483	}
2484
2485	errno = 0;
2486	h_errno = HOST_NOT_FOUND;	/* default, if we never query */
2487	dots = 0;
2488	for (cp = name; *cp; cp++)
2489		dots += (*cp == '.');
2490	trailing_dot = 0;
2491	if (cp > name && *--cp == '.')
2492		trailing_dot++;
2493
2494	/*
2495	 * if there aren't any dots, it could be a user-level alias
2496	 */
2497	if (!dots && (cp = __hostalias(name)) != NULL)
2498		return (res_queryN(cp, target));
2499
2500	/*
2501	 * If there are dots in the name already, let's just give it a try
2502	 * 'as is'.  The threshold can be set with the "ndots" option.
2503	 */
2504	saved_herrno = -1;
2505	if (dots >= _res.ndots) {
2506		ret = res_querydomainN(name, NULL, target);
2507		if (ret > 0)
2508			return (ret);
2509		saved_herrno = h_errno;
2510		tried_as_is++;
2511	}
2512
2513	/*
2514	 * We do at least one level of search if
2515	 *	- there is no dot and RES_DEFNAME is set, or
2516	 *	- there is at least one dot, there is no trailing dot,
2517	 *	  and RES_DNSRCH is set.
2518	 */
2519	if ((!dots && (_res.options & RES_DEFNAMES)) ||
2520	    (dots && !trailing_dot && (_res.options & RES_DNSRCH))) {
2521		int done = 0;
2522
2523		for (domain = (const char * const *)_res.dnsrch;
2524		   *domain && !done;
2525		   domain++) {
2526
2527			ret = res_querydomainN(name, *domain, target);
2528			if (ret > 0)
2529				return (ret);
2530
2531			/*
2532			 * If no server present, give up.
2533			 * If name isn't found in this domain,
2534			 * keep trying higher domains in the search list
2535			 * (if that's enabled).
2536			 * On a NO_DATA error, keep trying, otherwise
2537			 * a wildcard entry of another type could keep us
2538			 * from finding this entry higher in the domain.
2539			 * If we get some other error (negative answer or
2540			 * server failure), then stop searching up,
2541			 * but try the input name below in case it's
2542			 * fully-qualified.
2543			 */
2544			if (errno == ECONNREFUSED) {
2545				h_errno = TRY_AGAIN;
2546				return (-1);
2547			}
2548
2549			switch (h_errno) {
2550			case NO_DATA:
2551				got_nodata++;
2552				/* FALLTHROUGH */
2553			case HOST_NOT_FOUND:
2554				/* keep trying */
2555				break;
2556			case TRY_AGAIN:
2557				if (hp->rcode == SERVFAIL) {
2558					/* try next search element, if any */
2559					got_servfail++;
2560					break;
2561				}
2562				/* FALLTHROUGH */
2563			default:
2564				/* anything else implies that we're done */
2565				done++;
2566			}
2567			/*
2568			 * if we got here for some reason other than DNSRCH,
2569			 * we only wanted one iteration of the loop, so stop.
2570			 */
2571			if (!(_res.options & RES_DNSRCH))
2572			        done++;
2573		}
2574	}
2575
2576	/*
2577	 * if we have not already tried the name "as is", do that now.
2578	 * note that we do this regardless of how many dots were in the
2579	 * name or whether it ends with a dot.
2580	 */
2581	if (!tried_as_is && (dots || !(_res.options & RES_NOTLDQUERY))) {
2582		ret = res_querydomainN(name, NULL, target);
2583		if (ret > 0)
2584			return (ret);
2585	}
2586
2587	/*
2588	 * if we got here, we didn't satisfy the search.
2589	 * if we did an initial full query, return that query's h_errno
2590	 * (note that we wouldn't be here if that query had succeeded).
2591	 * else if we ever got a nodata, send that back as the reason.
2592	 * else send back meaningless h_errno, that being the one from
2593	 * the last DNSRCH we did.
2594	 */
2595	if (saved_herrno != -1)
2596		h_errno = saved_herrno;
2597	else if (got_nodata)
2598		h_errno = NO_DATA;
2599	else if (got_servfail)
2600		h_errno = TRY_AGAIN;
2601	return (-1);
2602}
2603
2604/*
2605 * Perform a call on res_query on the concatenation of name and domain,
2606 * removing a trailing dot from name if domain is NULL.
2607 */
2608static int
2609res_querydomainN(name, domain, target)
2610	const char *name, *domain;
2611	struct res_target *target;
2612{
2613	char nbuf[MAXDNAME];
2614	const char *longname = nbuf;
2615	size_t n, d;
2616
2617	if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
2618		h_errno = NETDB_INTERNAL;
2619		return (-1);
2620	}
2621#ifdef DEBUG
2622	if (_res.options & RES_DEBUG)
2623		printf(";; res_querydomain(%s, %s)\n",
2624			name, domain?domain:"<Nil>");
2625#endif
2626	if (domain == NULL) {
2627		/*
2628		 * Check for trailing '.';
2629		 * copy without '.' if present.
2630		 */
2631		n = strlen(name);
2632		if (n >= MAXDNAME) {
2633			h_errno = NO_RECOVERY;
2634			return (-1);
2635		}
2636		if (n > 0 && name[--n] == '.') {
2637			strncpy(nbuf, name, n);
2638			nbuf[n] = '\0';
2639		} else
2640			longname = name;
2641	} else {
2642		n = strlen(name);
2643		d = strlen(domain);
2644		if (n + d + 1 >= MAXDNAME) {
2645			h_errno = NO_RECOVERY;
2646			return (-1);
2647		}
2648		snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
2649	}
2650	return (res_queryN(longname, target));
2651}
2652