name6.c revision 126052
1/*	$KAME: name6.c,v 1.25 2000/06/26 16:44:40 itojun Exp $	*/
2
3/*
4 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 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 * ++Copyright++ 1985, 1988, 1993
33 * -
34 * Copyright (c) 1985, 1988, 1993
35 *    The Regents of the University of California.  All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 *    notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 *    notice, this list of conditions and the following disclaimer in the
44 *    documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 *    must display the following acknowledgement:
47 * 	This product includes software developed by the University of
48 * 	California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 *    may be used to endorse or promote products derived from this software
51 *    without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 * -
65 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
66 *
67 * Permission to use, copy, modify, and distribute this software for any
68 * purpose with or without fee is hereby granted, provided that the above
69 * copyright notice and this permission notice appear in all copies, and that
70 * the name of Digital Equipment Corporation not be used in advertising or
71 * publicity pertaining to distribution of the document or software without
72 * specific, written prior permission.
73 *
74 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
75 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
76 * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
77 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
78 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
79 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
80 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
81 * SOFTWARE.
82 * -
83 * --Copyright--
84 */
85
86/*
87 *	Atsushi Onoe <onoe@sm.sony.co.jp>
88 */
89
90/*
91 * TODO for thread safe
92 *	use mutex for _hostconf, _hostconf_init.
93 *	rewrite resolvers to be thread safe
94 */
95
96#include <sys/cdefs.h>
97__FBSDID("$FreeBSD: head/lib/libc/net/name6.c 126052 2004-02-20 17:59:33Z ume $");
98
99#include "namespace.h"
100#include <sys/param.h>
101#include <sys/socket.h>
102#include <sys/time.h>
103#include <sys/queue.h>
104#include <netinet/in.h>
105#ifdef INET6
106#include <net/if.h>
107#include <net/if_var.h>
108#include <sys/sysctl.h>
109#include <netinet6/in6_var.h>	/* XXX */
110#endif
111
112#include <arpa/inet.h>
113#include <arpa/nameser.h>
114
115#include <errno.h>
116#include <netdb.h>
117#include <resolv.h>
118#include <stdio.h>
119#include <stdlib.h>
120#include <string.h>
121#include <stdarg.h>
122#include <nsswitch.h>
123#include <pthread.h>
124#include <unistd.h>
125#include "un-namespace.h"
126
127#ifndef _PATH_HOSTS
128#define	_PATH_HOSTS	"/etc/hosts"
129#endif
130
131#ifndef MAXALIASES
132#define	MAXALIASES	10
133#endif
134#ifndef	MAXADDRS
135#define	MAXADDRS	20
136#endif
137#ifndef MAXDNAME
138#define	MAXDNAME	1025
139#endif
140
141#ifdef INET6
142#define	ADDRLEN(af)	((af) == AF_INET6 ? sizeof(struct in6_addr) : \
143					    sizeof(struct in_addr))
144#else
145#define	ADDRLEN(af)	sizeof(struct in_addr)
146#endif
147
148#define	MAPADDR(ab, ina) \
149do {									\
150	memcpy(&(ab)->map_inaddr, ina, sizeof(struct in_addr));		\
151	memset((ab)->map_zero, 0, sizeof((ab)->map_zero));		\
152	memset((ab)->map_one, 0xff, sizeof((ab)->map_one));		\
153} while (0)
154#define	MAPADDRENABLED(flags) \
155	(((flags) & AI_V4MAPPED) || \
156	 (((flags) & AI_V4MAPPED_CFG) && _mapped_addr_enabled()))
157
158union inx_addr {
159	struct in_addr	in_addr;
160#ifdef INET6
161	struct in6_addr	in6_addr;
162#endif
163	struct {
164		u_char	mau_zero[10];
165		u_char	mau_one[2];
166		struct in_addr mau_inaddr;
167	}		map_addr_un;
168#define	map_zero	map_addr_un.mau_zero
169#define	map_one		map_addr_un.mau_one
170#define	map_inaddr	map_addr_un.mau_inaddr
171};
172
173struct policyqueue {
174	TAILQ_ENTRY(policyqueue) pc_entry;
175#ifdef INET6
176	struct in6_addrpolicy pc_policy;
177#endif
178};
179TAILQ_HEAD(policyhead, policyqueue);
180
181static struct	 hostent *_hpcopy(struct hostent *hp, int *errp);
182static struct	 hostent *_hpaddr(int af, const char *name, void *addr, int *errp);
183static struct	 hostent *_hpmerge(struct hostent *hp1, struct hostent *hp2, int *errp);
184#ifdef INET6
185static struct	 hostent *_hpmapv6(struct hostent *hp, int *errp);
186#endif
187static struct	 hostent *_hpsort(struct hostent *hp);
188static struct	 hostent *_ghbyname(const char *name, int af, int flags, int *errp);
189static char	*_hgetword(char **pp);
190static int	 _mapped_addr_enabled(void);
191
192static struct	 hostent *_hpreorder(struct hostent *hp);
193static int	 get_addrselectpolicy(struct policyhead *);
194static void	 free_addrselectpolicy(struct policyhead *);
195static struct	 policyqueue *match_addrselectpolicy(struct sockaddr *,
196	struct policyhead *);
197
198static FILE	*_files_open(int *errp);
199static int	 _files_ghbyname(void *, void *, va_list);
200static int	 _files_ghbyaddr(void *, void *, va_list);
201#ifdef YP
202static int	 _nis_ghbyname(void *, void *, va_list);
203static int	 _nis_ghbyaddr(void *, void *, va_list);
204#endif
205static int	 _dns_ghbyname(void *, void *, va_list);
206static int	 _dns_ghbyaddr(void *, void *, va_list);
207static void	 _dns_shent(int stayopen) __unused;
208static void	 _dns_ehent(void) __unused;
209#ifdef ICMPNL
210static int	 _icmp_ghbyaddr(void *, void *, va_list);
211#endif /* ICMPNL */
212
213/*
214 * XXX: Our res_*() is not thread-safe.  So, we share lock between
215 * getaddrinfo() and getipnodeby*().  Still, we cannot use
216 * getaddrinfo() and getipnodeby*() in conjunction with other
217 * functions which call res_*().
218 */
219#include "libc_private.h"
220extern pthread_mutex_t __getaddrinfo_thread_lock;
221#define THREAD_LOCK() \
222	if (__isthreaded) _pthread_mutex_lock(&__getaddrinfo_thread_lock);
223#define THREAD_UNLOCK() \
224	if (__isthreaded) _pthread_mutex_unlock(&__getaddrinfo_thread_lock);
225
226/* Host lookup order if nsswitch.conf is broken or nonexistant */
227static const ns_src default_src[] = {
228	{ NSSRC_FILES, NS_SUCCESS },
229	{ NSSRC_DNS, NS_SUCCESS },
230#ifdef ICMPNL
231#define NSSRC_ICMP "icmp"
232	{ NSSRC_ICMP, NS_SUCCESS },
233#endif
234	{ 0 }
235};
236
237/*
238 * Check if kernel supports mapped address.
239 *	implementation dependent
240 */
241#ifdef __KAME__
242#include <sys/sysctl.h>
243#endif /* __KAME__ */
244
245static int
246_mapped_addr_enabled(void)
247{
248	/* implementation dependent check */
249#if defined(__KAME__) && defined(IPV6CTL_MAPPED_ADDR)
250	int mib[4];
251	size_t len;
252	int val;
253
254	mib[0] = CTL_NET;
255	mib[1] = PF_INET6;
256	mib[2] = IPPROTO_IPV6;
257	mib[3] = IPV6CTL_MAPPED_ADDR;
258	len = sizeof(val);
259	if (sysctl(mib, 4, &val, &len, 0, 0) == 0 && val != 0)
260		return 1;
261#endif /* __KAME__ && IPV6CTL_MAPPED_ADDR */
262	return 0;
263}
264
265/*
266 * Functions defined in RFC2553
267 *	getipnodebyname, getipnodebyaddr, freehostent
268 */
269
270static struct hostent *
271_ghbyname(const char *name, int af, int flags, int *errp)
272{
273	struct hostent *hp;
274	int rval;
275
276	static const ns_dtab dtab[] = {
277		NS_FILES_CB(_files_ghbyname, NULL)
278		{ NSSRC_DNS, _dns_ghbyname, NULL },
279		NS_NIS_CB(_nis_ghbyname, NULL)
280		{ 0 }
281	};
282
283	if (flags & AI_ADDRCONFIG) {
284		int s;
285
286		/*
287		 * TODO:
288		 * Note that implementation dependent test for address
289		 * configuration should be done everytime called
290		 * (or apropriate interval),
291		 * because addresses will be dynamically assigned or deleted.
292		 */
293		if (af == AF_UNSPEC) {
294			if ((s = _socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
295				af = AF_INET;
296			else {
297				_close(s);
298				if ((s = _socket(AF_INET, SOCK_DGRAM, 0)) < 0)
299					af = AF_INET6;
300				else
301				_close(s);
302			}
303
304		}
305		if (af != AF_UNSPEC) {
306			if ((s = _socket(af, SOCK_DGRAM, 0)) < 0)
307				return NULL;
308			_close(s);
309		}
310	}
311
312	THREAD_LOCK();
313	rval = _nsdispatch(&hp, dtab, NSDB_HOSTS, "ghbyname", default_src,
314			  name, af, errp);
315	THREAD_UNLOCK();
316	return (rval == NS_SUCCESS) ? hp : NULL;
317}
318
319/* getipnodebyname() internal routine for multiple query(PF_UNSPEC) support. */
320struct hostent *
321_getipnodebyname_multi(const char *name, int af, int flags, int *errp)
322{
323	struct hostent *hp;
324	union inx_addr addrbuf;
325
326	/* XXX: PF_UNSPEC is only supposed to be passed from getaddrinfo() */
327	if (af != AF_INET
328#ifdef INET6
329	    && af != AF_INET6
330#endif
331	    && af != PF_UNSPEC
332		)
333	{
334		*errp = NO_RECOVERY;
335		return NULL;
336	}
337
338#ifdef INET6
339	/* special case for literal address */
340	if (inet_pton(AF_INET6, name, &addrbuf) == 1) {
341		if (af != AF_INET6) {
342			*errp = HOST_NOT_FOUND;
343			return NULL;
344		}
345		return _hpaddr(af, name, &addrbuf, errp);
346	}
347#endif
348	if (inet_aton(name, (struct in_addr *)&addrbuf) == 1) {
349		if (af != AF_INET) {
350			if (MAPADDRENABLED(flags)) {
351				MAPADDR(&addrbuf, &addrbuf.in_addr);
352			} else {
353				*errp = HOST_NOT_FOUND;
354				return NULL;
355			}
356		}
357		return _hpaddr(af, name, &addrbuf, errp);
358	}
359
360	*errp = HOST_NOT_FOUND;
361	hp = _ghbyname(name, af, flags, errp);
362
363#ifdef INET6
364	if (af == AF_INET6
365	&&  ((flags & AI_ALL) || hp == NULL)
366	&&  (MAPADDRENABLED(flags))) {
367		struct hostent *hp2 = _ghbyname(name, AF_INET, flags, errp);
368		if (hp == NULL)
369			hp = _hpmapv6(hp2, errp);
370		else {
371			if (hp2 && strcmp(hp->h_name, hp2->h_name) != 0) {
372				freehostent(hp2);
373				hp2 = NULL;
374			}
375			hp = _hpmerge(hp, hp2, errp);
376		}
377	}
378#endif
379	return _hpreorder(_hpsort(hp));
380}
381
382struct hostent *
383getipnodebyname(const char *name, int af, int flags, int *errp)
384{
385	if (af != AF_INET
386#ifdef INET6
387	    && af != AF_INET6
388#endif
389		)
390	{
391		*errp = NO_RECOVERY;
392		return NULL;
393	}
394	return(_getipnodebyname_multi(name, af ,flags, errp));
395}
396
397struct hostent *
398getipnodebyaddr(const void *src, size_t len, int af, int *errp)
399{
400	struct hostent *hp;
401	int rval;
402#ifdef INET6
403	struct in6_addr addrbuf;
404#else
405	struct in_addr addrbuf;
406#endif
407
408	static const ns_dtab dtab[] = {
409		NS_FILES_CB(_files_ghbyaddr, NULL)
410		{ NSSRC_DNS, _dns_ghbyaddr, NULL },
411		NS_NIS_CB(_nis_ghbyaddr, NULL)
412#ifdef ICMPNL
413		{ NSSRC_ICMP, _icmp_ghbyaddr, NULL },
414#endif
415		{ 0 }
416	};
417
418	*errp = HOST_NOT_FOUND;
419
420	switch (af) {
421	case AF_INET:
422		if (len != sizeof(struct in_addr)) {
423			*errp = NO_RECOVERY;
424			return NULL;
425		}
426		if ((long)src & ~(sizeof(struct in_addr) - 1)) {
427			memcpy(&addrbuf, src, len);
428			src = &addrbuf;
429		}
430		if (((struct in_addr *)src)->s_addr == 0)
431			return NULL;
432		break;
433#ifdef INET6
434	case AF_INET6:
435		if (len != sizeof(struct in6_addr)) {
436			*errp = NO_RECOVERY;
437			return NULL;
438		}
439		if ((long)src & ~(sizeof(struct in6_addr) / 2 - 1)) {	/*XXX*/
440			memcpy(&addrbuf, src, len);
441			src = &addrbuf;
442		}
443		if (IN6_IS_ADDR_UNSPECIFIED((struct in6_addr *)src))
444			return NULL;
445		if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)src)
446		||  IN6_IS_ADDR_V4COMPAT((struct in6_addr *)src)) {
447			src = (char *)src +
448			    (sizeof(struct in6_addr) - sizeof(struct in_addr));
449			af = AF_INET;
450			len = sizeof(struct in_addr);
451		}
452		break;
453#endif
454	default:
455		*errp = NO_RECOVERY;
456		return NULL;
457	}
458
459	THREAD_LOCK();
460	rval = _nsdispatch(&hp, dtab, NSDB_HOSTS, "ghbyaddr", default_src,
461			  src, len, af, errp);
462	THREAD_UNLOCK();
463	return (rval == NS_SUCCESS) ? hp : NULL;
464}
465
466void
467freehostent(struct hostent *ptr)
468{
469	free(ptr);
470}
471
472#if 0
473
474/* XXX: should be deprecated */
475struct hostent *
476getnodebyname(const char *name, int af, int flags)
477{
478	return getipnodebyname(name, af, flags, &h_errno);
479}
480
481#ifdef __warn_references
482__warn_references(getnodebyname,
483	"warning: getnodebyname() deprecated, "
484	"should use getaddrinfo() or getipnodebyname()");
485#endif
486
487struct hostent *
488getnodebyaddr(const void *src, size_t len, int af)
489{
490	return getipnodebyaddr(src, len, af, &h_errno);
491}
492
493#ifdef __warn_references
494__warn_references(getnodebyaddr,
495	"warning: getnodebyaddr() deprecated, "
496	"should use getnameinfo() or getipnodebyaddr()");
497#endif
498
499#endif
500
501/*
502 * Private utility functions
503 */
504
505/*
506 * _hpcopy: allocate and copy hostent structure
507 */
508static struct hostent *
509_hpcopy(struct hostent *hp, int *errp)
510{
511	struct hostent *nhp;
512	char *cp, **pp;
513	int size, addrsize;
514	int nalias = 0, naddr = 0;
515	int al_off;
516	int i;
517
518	if (hp == NULL)
519		return hp;
520
521	/* count size to be allocated */
522	size = sizeof(struct hostent);
523	if (hp->h_name != NULL)
524		size += strlen(hp->h_name) + 1;
525	if ((pp = hp->h_aliases) != NULL) {
526		for (i = 0; *pp != NULL; i++, pp++) {
527			if (**pp != '\0') {
528				size += strlen(*pp) + 1;
529				nalias++;
530			}
531		}
532	}
533	/* adjust alignment */
534	size = ALIGN(size);
535	al_off = size;
536	size += sizeof(char *) * (nalias + 1);
537	addrsize = ALIGN(hp->h_length);
538	if ((pp = hp->h_addr_list) != NULL) {
539		while (*pp++ != NULL)
540			naddr++;
541	}
542	size += addrsize * naddr;
543	size += sizeof(char *) * (naddr + 1);
544
545	/* copy */
546	if ((nhp = (struct hostent *)malloc(size)) == NULL) {
547		*errp = TRY_AGAIN;
548		return NULL;
549	}
550	cp = (char *)&nhp[1];
551	if (hp->h_name != NULL) {
552		nhp->h_name = cp;
553		strcpy(cp, hp->h_name);
554		cp += strlen(cp) + 1;
555	} else
556		nhp->h_name = NULL;
557	nhp->h_aliases = (char **)((char *)nhp + al_off);
558	if ((pp = hp->h_aliases) != NULL) {
559		for (i = 0; *pp != NULL; pp++) {
560			if (**pp != '\0') {
561				nhp->h_aliases[i++] = cp;
562				strcpy(cp, *pp);
563				cp += strlen(cp) + 1;
564			}
565		}
566	}
567	nhp->h_aliases[nalias] = NULL;
568	cp = (char *)&nhp->h_aliases[nalias + 1];
569	nhp->h_addrtype = hp->h_addrtype;
570	nhp->h_length = hp->h_length;
571	nhp->h_addr_list = (char **)cp;
572	if ((pp = hp->h_addr_list) != NULL) {
573		cp = (char *)&nhp->h_addr_list[naddr + 1];
574		for (i = 0; *pp != NULL; pp++) {
575			nhp->h_addr_list[i++] = cp;
576			memcpy(cp, *pp, hp->h_length);
577			cp += addrsize;
578		}
579	}
580	nhp->h_addr_list[naddr] = NULL;
581	return nhp;
582}
583
584/*
585 * _hpaddr: construct hostent structure with one address
586 */
587static struct hostent *
588_hpaddr(int af, const char *name, void *addr, int *errp)
589{
590	struct hostent *hp, hpbuf;
591	char *addrs[2];
592
593	hp = &hpbuf;
594	hp->h_name = (char *)name;
595	hp->h_aliases = NULL;
596	hp->h_addrtype = af;
597	hp->h_length = ADDRLEN(af);
598	hp->h_addr_list = addrs;
599	addrs[0] = (char *)addr;
600	addrs[1] = NULL;
601	return _hpcopy(hp, errp);
602}
603
604/*
605 * _hpmerge: merge 2 hostent structure, arguments will be freed
606 */
607static struct hostent *
608_hpmerge(struct hostent *hp1, struct hostent *hp2, int *errp)
609{
610	int i, j;
611	int naddr, nalias;
612	char **pp;
613	struct hostent *hp, hpbuf;
614	char *aliases[MAXALIASES + 1], *addrs[MAXADDRS + 1];
615	union inx_addr addrbuf[MAXADDRS];
616
617	if (hp1 == NULL)
618		return hp2;
619	if (hp2 == NULL)
620		return hp1;
621
622#define	HP(i)	(i == 1 ? hp1 : hp2)
623	hp = &hpbuf;
624	hp->h_name = (hp1->h_name != NULL ? hp1->h_name : hp2->h_name);
625	hp->h_aliases = aliases;
626	nalias = 0;
627	for (i = 1; i <= 2; i++) {
628		if ((pp = HP(i)->h_aliases) == NULL)
629			continue;
630		for (; nalias < MAXALIASES && *pp != NULL; pp++) {
631			/* check duplicates */
632			for (j = 0; j < nalias; j++)
633				if (strcasecmp(*pp, aliases[j]) == 0)
634					break;
635			if (j == nalias)
636				aliases[nalias++] = *pp;
637		}
638	}
639	aliases[nalias] = NULL;
640#ifdef INET6
641	if (hp1->h_length != hp2->h_length) {
642		hp->h_addrtype = AF_INET6;
643		hp->h_length = sizeof(struct in6_addr);
644	} else {
645#endif
646		hp->h_addrtype = hp1->h_addrtype;
647		hp->h_length = hp1->h_length;
648#ifdef INET6
649	}
650#endif
651	hp->h_addr_list = addrs;
652	naddr = 0;
653	for (i = 1; i <= 2; i++) {
654		if ((pp = HP(i)->h_addr_list) == NULL)
655			continue;
656		if (HP(i)->h_length == hp->h_length) {
657			while (naddr < MAXADDRS && *pp != NULL)
658				addrs[naddr++] = *pp++;
659		} else {
660			/* copy IPv4 addr as mapped IPv6 addr */
661			while (naddr < MAXADDRS && *pp != NULL) {
662				MAPADDR(&addrbuf[naddr], *pp++);
663				addrs[naddr] = (char *)&addrbuf[naddr];
664				naddr++;
665			}
666		}
667	}
668	addrs[naddr] = NULL;
669	hp = _hpcopy(hp, errp);
670	freehostent(hp1);
671	freehostent(hp2);
672	return hp;
673}
674
675/*
676 * _hpmapv6: convert IPv4 hostent into IPv4-mapped IPv6 addresses
677 */
678#ifdef INET6
679static struct hostent *
680_hpmapv6(struct hostent *hp, int *errp)
681{
682	struct hostent *hp6;
683
684	if (hp == NULL)
685		return NULL;
686	if (hp->h_addrtype == AF_INET6)
687		return hp;
688
689	/* make dummy hostent to convert IPv6 address */
690	if ((hp6 = (struct hostent *)malloc(sizeof(struct hostent))) == NULL) {
691		*errp = TRY_AGAIN;
692		return NULL;
693	}
694	hp6->h_name = NULL;
695	hp6->h_aliases = NULL;
696	hp6->h_addrtype = AF_INET6;
697	hp6->h_length = sizeof(struct in6_addr);
698	hp6->h_addr_list = NULL;
699	return _hpmerge(hp6, hp, errp);
700}
701#endif
702
703/*
704 * _hpsort: sort address by sortlist
705 */
706static struct hostent *
707_hpsort(struct hostent *hp)
708{
709	int i, j, n;
710	u_char *ap, *sp, *mp, **pp;
711	char t;
712	char order[MAXADDRS];
713	int nsort = _res.nsort;
714
715	if (hp == NULL || hp->h_addr_list[1] == NULL || nsort == 0)
716		return hp;
717	for (i = 0; (ap = (u_char *)hp->h_addr_list[i]); i++) {
718		for (j = 0; j < nsort; j++) {
719#ifdef INET6
720			if (_res_ext.sort_list[j].af != hp->h_addrtype)
721				continue;
722			sp = (u_char *)&_res_ext.sort_list[j].addr;
723			mp = (u_char *)&_res_ext.sort_list[j].mask;
724#else
725			sp = (u_char *)&_res.sort_list[j].addr;
726			mp = (u_char *)&_res.sort_list[j].mask;
727#endif
728			for (n = 0; n < hp->h_length; n++) {
729				if ((ap[n] & mp[n]) != sp[n])
730					break;
731			}
732			if (n == hp->h_length)
733				break;
734		}
735		order[i] = j;
736	}
737	n = i;
738	pp = (u_char **)hp->h_addr_list;
739	for (i = 0; i < n - 1; i++) {
740		for (j = i + 1; j < n; j++) {
741			if (order[i] > order[j]) {
742				ap = pp[i];
743				pp[i] = pp[j];
744				pp[j] = ap;
745				t = order[i];
746				order[i] = order[j];
747				order[j] = t;
748			}
749		}
750	}
751	return hp;
752}
753
754static char *
755_hgetword(char **pp)
756{
757	char c, *p, *ret;
758	const char *sp;
759	static const char sep[] = "# \t\n";
760
761	ret = NULL;
762	for (p = *pp; (c = *p) != '\0'; p++) {
763		for (sp = sep; *sp != '\0'; sp++) {
764			if (c == *sp)
765				break;
766		}
767		if (c == '#')
768			p[1] = '\0';	/* ignore rest of line */
769		if (ret == NULL) {
770			if (*sp == '\0')
771				ret = p;
772		} else {
773			if (*sp != '\0') {
774				*p++ = '\0';
775				break;
776			}
777		}
778	}
779	*pp = p;
780	if (ret == NULL || *ret == '\0')
781		return NULL;
782	return ret;
783}
784
785/*
786 * _hpreorder: sort address by default address selection
787 */
788static struct hostent *
789_hpreorder(struct hostent *hp)
790{
791	int i, j, n;
792	u_char *ap, **pp;
793	struct policyqueue *dstpolicy[MAXADDRS], *t;
794	struct sockaddr_storage ss;
795	struct policyhead policyhead;
796
797	if (hp == NULL || hp->h_addr_list[1] == NULL)
798		return hp;
799
800	/* retrieve address selection policy from the kernel */
801	TAILQ_INIT(&policyhead);
802	if (!get_addrselectpolicy(&policyhead)) {
803		/* no policy is installed into kernel, we don't sort. */
804		return hp;
805	}
806
807	switch (hp->h_addrtype) {
808	case AF_INET:
809		for (i = 0; (ap = (u_char *)hp->h_addr_list[i]); i++) {
810			memset(&ss, 0, sizeof(ss));
811			ss.ss_family = AF_INET;
812			ss.ss_len = sizeof(struct sockaddr_in);
813			memcpy(&((struct sockaddr_in *)&ss)->sin_addr, ap,
814			    sizeof(struct in_addr));
815			dstpolicy[i] = match_addrselectpolicy(
816			    (struct sockaddr *)&ss, &policyhead);
817		}
818		break;
819#ifdef INET6
820	case AF_INET6:
821		for (i = 0; (ap = (u_char *)hp->h_addr_list[i]); i++) {
822			memset(&ss, 0, sizeof(ss));
823			if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)ap)) {
824				ss.ss_family = AF_INET;
825				ss.ss_len = sizeof(struct sockaddr_in);
826				memcpy(&((struct sockaddr_in *)&ss)->sin_addr,
827				    &ap[12], sizeof(struct in_addr));
828			} else {
829				ss.ss_family = AF_INET6;
830				ss.ss_len = sizeof(struct sockaddr_in6);
831				memcpy(
832				    &((struct sockaddr_in6 *)&ss)->sin6_addr,
833				    ap, sizeof(struct in6_addr));
834			}
835			dstpolicy[i] = match_addrselectpolicy(
836			    (struct sockaddr *)&ss, &policyhead);
837		}
838		break;
839#endif
840	default:
841		free_addrselectpolicy(&policyhead);
842		return hp;
843	}
844
845	/* perform sorting. */
846	n = i;
847	pp = (u_char **)hp->h_addr_list;
848	for (i = 0; i < n - 1; i++) {
849		for (j = i + 1; j < n; j++) {
850			if (dstpolicy[j] &&
851			    (dstpolicy[i] == NULL ||
852			     dstpolicy[j]->pc_policy.preced >
853			     dstpolicy[i]->pc_policy.preced)) {
854				ap = pp[i];
855				pp[i] = pp[j];
856				pp[j] = ap;
857				t = dstpolicy[i];
858				dstpolicy[i] = dstpolicy[j];
859				dstpolicy[j] = t;
860			}
861		}
862	}
863
864	/* cleanup and return */
865	free_addrselectpolicy(&policyhead);
866	return hp;
867}
868
869static int
870get_addrselectpolicy(head)
871	struct policyhead *head;
872{
873#ifdef INET6
874	int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_ADDRCTLPOLICY };
875	size_t l;
876	char *buf;
877	struct in6_addrpolicy *pol, *ep;
878
879	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0)
880		return (0);
881	if ((buf = malloc(l)) == NULL)
882		return (0);
883	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
884		free(buf);
885		return (0);
886	}
887
888	ep = (struct in6_addrpolicy *)(buf + l);
889	for (pol = (struct in6_addrpolicy *)buf; pol + 1 <= ep; pol++) {
890		struct policyqueue *new;
891
892		if ((new = malloc(sizeof(*new))) == NULL) {
893			free_addrselectpolicy(head); /* make the list empty */
894			break;
895		}
896		new->pc_policy = *pol;
897		TAILQ_INSERT_TAIL(head, new, pc_entry);
898	}
899
900	free(buf);
901	return (1);
902#else
903	return (0);
904#endif
905}
906
907static void
908free_addrselectpolicy(head)
909	struct policyhead *head;
910{
911	struct policyqueue *ent, *nent;
912
913	for (ent = TAILQ_FIRST(head); ent; ent = nent) {
914		nent = TAILQ_NEXT(ent, pc_entry);
915		TAILQ_REMOVE(head, ent, pc_entry);
916		free(ent);
917	}
918}
919
920static struct policyqueue *
921match_addrselectpolicy(addr, head)
922	struct sockaddr *addr;
923	struct policyhead *head;
924{
925#ifdef INET6
926	struct policyqueue *ent, *bestent = NULL;
927	struct in6_addrpolicy *pol;
928	int matchlen, bestmatchlen = -1;
929	u_char *mp, *ep, *k, *p, m;
930	struct sockaddr_in6 key;
931
932	switch(addr->sa_family) {
933	case AF_INET6:
934		key = *(struct sockaddr_in6 *)addr;
935		break;
936	case AF_INET:
937		/* convert the address into IPv4-mapped IPv6 address. */
938		memset(&key, 0, sizeof(key));
939		key.sin6_family = AF_INET6;
940		key.sin6_len = sizeof(key);
941		key.sin6_addr.s6_addr[10] = 0xff;
942		key.sin6_addr.s6_addr[11] = 0xff;
943		memcpy(&key.sin6_addr.s6_addr[12],
944		       &((struct sockaddr_in *)addr)->sin_addr, 4);
945		break;
946	default:
947		return(NULL);
948	}
949
950	for (ent = TAILQ_FIRST(head); ent; ent = TAILQ_NEXT(ent, pc_entry)) {
951		pol = &ent->pc_policy;
952		matchlen = 0;
953
954		mp = (u_char *)&pol->addrmask.sin6_addr;
955		ep = mp + 16;	/* XXX: scope field? */
956		k = (u_char *)&key.sin6_addr;
957		p = (u_char *)&pol->addr.sin6_addr;
958		for (; mp < ep && *mp; mp++, k++, p++) {
959			m = *mp;
960			if ((*k & m) != *p)
961				goto next; /* not match */
962			if (m == 0xff) /* short cut for a typical case */
963				matchlen += 8;
964			else {
965				while (m >= 0x80) {
966					matchlen++;
967					m <<= 1;
968				}
969			}
970		}
971
972		/* matched.  check if this is better than the current best. */
973		if (matchlen > bestmatchlen) {
974			bestent = ent;
975			bestmatchlen = matchlen;
976		}
977
978	  next:
979		continue;
980	}
981
982	return(bestent);
983#else
984	return(NULL);
985#endif
986
987}
988
989/*
990 * FILES (/etc/hosts)
991 */
992
993static FILE *
994_files_open(int *errp)
995{
996	FILE *fp;
997	fp = fopen(_PATH_HOSTS, "r");
998	if (fp == NULL)
999		*errp = NO_RECOVERY;
1000	return fp;
1001}
1002
1003static int
1004_files_ghbyname(void *rval, void *cb_data, va_list ap)
1005{
1006	const char *name;
1007	int af;
1008	int *errp;
1009	int match, nalias;
1010	char *p, *line, *addrstr, *cname;
1011	FILE *fp;
1012	struct hostent *rethp, *hp, hpbuf;
1013	char *aliases[MAXALIASES + 1], *addrs[2];
1014	union inx_addr addrbuf;
1015	char buf[BUFSIZ];
1016	int af0;
1017
1018	name = va_arg(ap, const char *);
1019	af = va_arg(ap, int);
1020	errp = va_arg(ap, int *);
1021
1022	*(struct hostent **)rval = NULL;
1023
1024	if ((fp = _files_open(errp)) == NULL)
1025		return NS_UNAVAIL;
1026	rethp = hp = NULL;
1027
1028	af0 = af;
1029	while (fgets(buf, sizeof(buf), fp)) {
1030		line = buf;
1031		if ((addrstr = _hgetword(&line)) == NULL
1032		||  (cname = _hgetword(&line)) == NULL)
1033			continue;
1034		match = (strcasecmp(cname, name) == 0);
1035		nalias = 0;
1036		while ((p = _hgetword(&line)) != NULL) {
1037			if (!match)
1038				match = (strcasecmp(p, name) == 0);
1039			if (nalias < MAXALIASES)
1040				aliases[nalias++] = p;
1041		}
1042		if (!match)
1043			continue;
1044		switch (af0) {
1045		case AF_INET:
1046			if (inet_aton(addrstr, (struct in_addr *)&addrbuf)
1047			    != 1) {
1048				*errp = NO_DATA;	/* name found */
1049				continue;
1050			}
1051			af = af0;
1052			break;
1053#ifdef INET6
1054		case AF_INET6:
1055			if (inet_pton(af, addrstr, &addrbuf) != 1) {
1056				*errp = NO_DATA;	/* name found */
1057				continue;
1058			}
1059			af = af0;
1060			break;
1061#endif
1062		case AF_UNSPEC:
1063			if (inet_aton(addrstr, (struct in_addr *)&addrbuf)
1064			    == 1) {
1065				af = AF_INET;
1066				break;
1067			}
1068#ifdef INET6
1069			if (inet_pton(AF_INET6, addrstr, &addrbuf) == 1) {
1070				af = AF_INET6;
1071				break;
1072			}
1073#endif
1074			*errp = NO_DATA;	/* name found */
1075			continue;
1076			/* NOTREACHED */
1077		}
1078		hp = &hpbuf;
1079		hp->h_name = cname;
1080		hp->h_aliases = aliases;
1081		aliases[nalias] = NULL;
1082		hp->h_addrtype = af;
1083		hp->h_length = ADDRLEN(af);
1084		hp->h_addr_list = addrs;
1085		addrs[0] = (char *)&addrbuf;
1086		addrs[1] = NULL;
1087		hp = _hpcopy(hp, errp);
1088		rethp = _hpmerge(rethp, hp, errp);
1089	}
1090	fclose(fp);
1091	*(struct hostent **)rval = rethp;
1092	return (rethp != NULL) ? NS_SUCCESS : NS_NOTFOUND;
1093}
1094
1095static int
1096_files_ghbyaddr(void *rval, void *cb_data, va_list ap)
1097{
1098	const void *addr;
1099	int addrlen;
1100	int af;
1101	int *errp;
1102	int nalias;
1103	char *p, *line;
1104	FILE *fp;
1105	struct hostent *hp, hpbuf;
1106	char *aliases[MAXALIASES + 1], *addrs[2];
1107	union inx_addr addrbuf;
1108	char buf[BUFSIZ];
1109
1110	addr = va_arg(ap, const void *);
1111	addrlen = va_arg(ap, int);
1112	af = va_arg(ap, int);
1113	errp = va_arg(ap, int *);
1114
1115	*(struct hostent**)rval = NULL;
1116
1117	if ((fp = _files_open(errp)) == NULL)
1118		return NS_UNAVAIL;
1119	hp = NULL;
1120	while (fgets(buf, sizeof(buf), fp)) {
1121		line = buf;
1122		if ((p = _hgetword(&line)) == NULL
1123		||  (af == AF_INET
1124		     ? inet_aton(p, (struct in_addr *)&addrbuf)
1125		     : inet_pton(af, p, &addrbuf)) != 1
1126		||  memcmp(addr, &addrbuf, addrlen) != 0
1127		||  (p = _hgetword(&line)) == NULL)
1128			continue;
1129		hp = &hpbuf;
1130		hp->h_name = p;
1131		hp->h_aliases = aliases;
1132		nalias = 0;
1133		while ((p = _hgetword(&line)) != NULL) {
1134			if (nalias < MAXALIASES)
1135				aliases[nalias++] = p;
1136		}
1137		aliases[nalias] = NULL;
1138		hp->h_addrtype = af;
1139		hp->h_length = addrlen;
1140		hp->h_addr_list = addrs;
1141		addrs[0] = (char *)&addrbuf;
1142		addrs[1] = NULL;
1143		hp = _hpcopy(hp, errp);
1144		break;
1145	}
1146	fclose(fp);
1147	*(struct hostent **)rval = hp;
1148	return (hp != NULL) ? NS_SUCCESS : NS_NOTFOUND;
1149}
1150
1151#ifdef YP
1152/*
1153 * NIS
1154 *
1155 * XXX actually a hack, these are INET4 specific.
1156 */
1157static int
1158_nis_ghbyname(void *rval, void *cb_data, va_list ap)
1159{
1160	const char *name;
1161	int af;
1162	int *errp;
1163	struct hostent *hp = NULL;
1164
1165	name = va_arg(ap, const char *);
1166	af = va_arg(ap, int);
1167	errp = va_arg(ap, int *);
1168
1169	if (af == AF_UNSPEC)
1170		af = AF_INET;
1171	if (af == AF_INET) {
1172		hp = _gethostbynisname(name, af);
1173		if (hp != NULL)
1174			hp = _hpcopy(hp, errp);
1175	}
1176
1177	*(struct hostent **)rval = hp;
1178	return (hp != NULL) ? NS_SUCCESS : NS_NOTFOUND;
1179
1180}
1181
1182static int
1183_nis_ghbyaddr(void *rval, void *cb_data, va_list ap)
1184{
1185	const void *addr;
1186	int addrlen;
1187	int af;
1188	int *errp;
1189	struct hostent *hp = NULL;
1190
1191	addr = va_arg(ap, const void *);
1192	addrlen = va_arg(ap, int);
1193	af = va_arg(ap, int);
1194
1195	if (af == AF_INET) {
1196		hp = _gethostbynisaddr(addr, addrlen, af);
1197		if (hp != NULL)
1198			hp = _hpcopy(hp, errp);
1199	}
1200	*(struct hostent **)rval = hp;
1201	return (hp != NULL) ? NS_SUCCESS : NS_NOTFOUND;
1202}
1203#endif
1204
1205struct __res_type_list {
1206        SLIST_ENTRY(__res_type_list) rtl_entry;
1207        int     rtl_type;
1208};
1209
1210#define	MAXPACKET	(64*1024)
1211
1212typedef union {
1213	HEADER hdr;
1214	u_char buf[MAXPACKET];
1215} querybuf;
1216
1217static struct hostent *getanswer(const querybuf *, int, const char *, int,
1218	    struct hostent *, int *);
1219
1220/*
1221 * we don't need to take care about sorting, nor IPv4 mapped address here.
1222 */
1223static struct hostent *
1224getanswer(answer, anslen, qname, qtype, template, errp)
1225	const querybuf *answer;
1226	int anslen;
1227	const char *qname;
1228	int qtype;
1229	struct hostent *template;
1230	int *errp;
1231{
1232	const HEADER *hp;
1233	const u_char *cp;
1234	int n;
1235	const u_char *eom, *erdata;
1236	char *bp, *ep, **ap, **hap;
1237	int type, class, ancount, qdcount;
1238	int haveanswer, had_error;
1239	char tbuf[MAXDNAME];
1240	const char *tname;
1241	int (*name_ok)(const char *);
1242	static char *h_addr_ptrs[MAXADDRS + 1];
1243	static char *host_aliases[MAXALIASES];
1244	static char hostbuf[8*1024];
1245
1246#define BOUNDED_INCR(x) \
1247	do { \
1248		cp += x; \
1249		if (cp > eom) { \
1250			*errp = NO_RECOVERY; \
1251			return (NULL); \
1252		} \
1253	} while (0)
1254
1255#define BOUNDS_CHECK(ptr, count) \
1256	do { \
1257		if ((ptr) + (count) > eom) { \
1258			*errp = NO_RECOVERY; \
1259			return (NULL); \
1260		} \
1261	} while (0)
1262
1263/* XXX do {} while (0) cannot be put here */
1264#define DNS_ASSERT(x) \
1265	{				\
1266		if (!(x)) {		\
1267			cp += n;	\
1268			continue;	\
1269		}			\
1270	}
1271
1272/* XXX do {} while (0) cannot be put here */
1273#define DNS_FATAL(x) \
1274	{				\
1275		if (!(x)) {		\
1276			had_error++;	\
1277			continue;	\
1278		}			\
1279	}
1280
1281	tname = qname;
1282	template->h_name = NULL;
1283	eom = answer->buf + anslen;
1284	switch (qtype) {
1285	case T_A:
1286	case T_AAAA:
1287		name_ok = res_hnok;
1288		break;
1289	case T_PTR:
1290		name_ok = res_dnok;
1291		break;
1292	default:
1293		return (NULL);	/* XXX should be abort(); */
1294	}
1295	/*
1296	 * find first satisfactory answer
1297	 */
1298	hp = &answer->hdr;
1299	ancount = ntohs(hp->ancount);
1300	qdcount = ntohs(hp->qdcount);
1301	bp = hostbuf;
1302	ep = hostbuf + sizeof hostbuf;
1303	cp = answer->buf;
1304	BOUNDED_INCR(HFIXEDSZ);
1305	if (qdcount != 1) {
1306		*errp = NO_RECOVERY;
1307		return (NULL);
1308	}
1309	n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1310	if ((n < 0) || !(*name_ok)(bp)) {
1311		*errp = NO_RECOVERY;
1312		return (NULL);
1313	}
1314	BOUNDED_INCR(n + QFIXEDSZ);
1315	if (qtype == T_A || qtype == T_AAAA) {
1316		/* res_send() has already verified that the query name is the
1317		 * same as the one we sent; this just gets the expanded name
1318		 * (i.e., with the succeeding search-domain tacked on).
1319		 */
1320		n = strlen(bp) + 1;		/* for the \0 */
1321		if (n >= MAXHOSTNAMELEN) {
1322			*errp = NO_RECOVERY;
1323			return (NULL);
1324		}
1325		template->h_name = bp;
1326		bp += n;
1327		/* The qname can be abbreviated, but h_name is now absolute. */
1328		qname = template->h_name;
1329	}
1330	ap = host_aliases;
1331	*ap = NULL;
1332	template->h_aliases = host_aliases;
1333	hap = h_addr_ptrs;
1334	*hap = NULL;
1335	template->h_addr_list = h_addr_ptrs;
1336	haveanswer = 0;
1337	had_error = 0;
1338	while (ancount-- > 0 && cp < eom && !had_error) {
1339		n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1340		DNS_FATAL(n >= 0);
1341		DNS_FATAL((*name_ok)(bp));
1342		cp += n;			/* name */
1343		BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
1344		type = _getshort(cp);
1345 		cp += INT16SZ;			/* type */
1346		class = _getshort(cp);
1347 		cp += INT16SZ + INT32SZ;	/* class, TTL */
1348		n = _getshort(cp);
1349		cp += INT16SZ;			/* len */
1350		BOUNDS_CHECK(cp, n);
1351		erdata = cp + n;
1352		DNS_ASSERT(class == C_IN);
1353		if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
1354			if (ap >= &host_aliases[MAXALIASES-1])
1355				continue;
1356			n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
1357			DNS_FATAL(n >= 0);
1358			DNS_FATAL((*name_ok)(tbuf));
1359			cp += n;
1360			if (cp != erdata) {
1361				*errp = NO_RECOVERY;
1362				return (NULL);
1363			}
1364			/* Store alias. */
1365			*ap++ = bp;
1366			n = strlen(bp) + 1;	/* for the \0 */
1367			DNS_FATAL(n < MAXHOSTNAMELEN);
1368			bp += n;
1369			/* Get canonical name. */
1370			n = strlen(tbuf) + 1;	/* for the \0 */
1371			DNS_FATAL(n <= ep - bp);
1372			DNS_FATAL(n < MAXHOSTNAMELEN);
1373			strcpy(bp, tbuf);
1374			template->h_name = bp;
1375			bp += n;
1376			continue;
1377		}
1378		if (qtype == T_PTR && type == T_CNAME) {
1379			n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
1380			if (n < 0 || !res_dnok(tbuf)) {
1381				had_error++;
1382				continue;
1383			}
1384			cp += n;
1385			if (cp != erdata) {
1386				*errp = NO_RECOVERY;
1387				return (NULL);
1388			}
1389			/* Get canonical name. */
1390			n = strlen(tbuf) + 1;	/* for the \0 */
1391			if (n > ep - bp || n >= MAXHOSTNAMELEN) {
1392				had_error++;
1393				continue;
1394			}
1395			strcpy(bp, tbuf);
1396			tname = bp;
1397			bp += n;
1398			continue;
1399		}
1400		DNS_ASSERT(type == qtype);
1401		switch (type) {
1402		case T_PTR:
1403			DNS_ASSERT(strcasecmp(tname, bp) == 0);
1404			n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1405			DNS_FATAL(n >= 0);
1406			DNS_FATAL(res_hnok(bp));
1407#if MULTI_PTRS_ARE_ALIASES
1408			cp += n;
1409			if (cp != erdata) {
1410				*errp = NO_RECOVERY;
1411				return (NULL);
1412			}
1413			if (!haveanswer)
1414				template->h_name = bp;
1415			else if (ap < &host_aliases[MAXALIASES-1])
1416				*ap++ = bp;
1417			else
1418				n = -1;
1419			if (n != -1) {
1420				n = strlen(bp) + 1;	/* for the \0 */
1421				if (n >= MAXHOSTNAMELEN) {
1422					had_error++;
1423					break;
1424				}
1425				bp += n;
1426			}
1427			break;
1428#else
1429			template->h_name = bp;
1430			*errp = NETDB_SUCCESS;
1431			return (template);
1432#endif
1433		case T_A:
1434		case T_AAAA:
1435			DNS_ASSERT(strcasecmp(template->h_name, bp) == 0);
1436			DNS_ASSERT(n == template->h_length);
1437			if (!haveanswer) {
1438				int nn;
1439
1440				template->h_name = bp;
1441				nn = strlen(bp) + 1;	/* for the \0 */
1442				bp += nn;
1443			}
1444			bp = (char *)ALIGN(bp);
1445
1446			DNS_FATAL(bp + n < ep);
1447			DNS_ASSERT(hap < &h_addr_ptrs[MAXADDRS-1]);
1448#ifdef FILTER_V4MAPPED
1449			if (type == T_AAAA) {
1450				struct in6_addr in6;
1451				memcpy(&in6, cp, sizeof(in6));
1452				DNS_ASSERT(IN6_IS_ADDR_V4MAPPED(&in6) == 0);
1453			}
1454#endif
1455			bcopy(cp, *hap++ = bp, n);
1456			bp += n;
1457			cp += n;
1458			if (cp != erdata) {
1459				*errp = NO_RECOVERY;
1460				return (NULL);
1461			}
1462			break;
1463		default:
1464			abort();
1465		}
1466		if (!had_error)
1467			haveanswer++;
1468	}
1469	if (haveanswer) {
1470		*ap = NULL;
1471		*hap = NULL;
1472		if (!template->h_name) {
1473			n = strlen(qname) + 1;	/* for the \0 */
1474			if (n > ep - bp || n >= MAXHOSTNAMELEN)
1475				goto no_recovery;
1476			strcpy(bp, qname);
1477			template->h_name = bp;
1478			bp += n;
1479		}
1480		*errp = NETDB_SUCCESS;
1481		return (template);
1482	}
1483 no_recovery:
1484	*errp = NO_RECOVERY;
1485	return (NULL);
1486
1487#undef BOUNDED_INCR
1488#undef BOUNDS_CHECK
1489#undef DNS_ASSERT
1490#undef DNS_FATAL
1491}
1492
1493/* res_search() variant with multiple query support. */
1494static struct hostent *
1495_res_search_multi(name, rtl, errp)
1496	const char *name;	/* domain name */
1497	struct	__res_type_list *rtl; /* list of query types */
1498	int *errp;
1499{
1500	const char *cp, * const *domain;
1501	struct hostent *hp0 = NULL, *hp;
1502	struct hostent hpbuf;
1503	u_int dots;
1504	int trailing_dot, ret, saved_herrno;
1505	int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
1506	struct __res_type_list *rtl0 = rtl;
1507	querybuf *buf;
1508
1509	if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
1510		*errp = NETDB_INTERNAL;
1511		return (NULL);
1512	}
1513	dots = 0;
1514	for (cp = name; *cp; cp++)
1515		dots += (*cp == '.');
1516	trailing_dot = 0;
1517	if (cp > name && *--cp == '.')
1518		trailing_dot++;
1519
1520	buf = malloc(sizeof(*buf));
1521	if (buf == NULL) {
1522		*errp = NETDB_INTERNAL;
1523		return NULL;
1524	}
1525
1526	/* If there aren't any dots, it could be a user-level alias */
1527	if (!dots && (cp = hostalias(name)) != NULL) {
1528		for(rtl = rtl0; rtl != NULL;
1529		    rtl = SLIST_NEXT(rtl, rtl_entry)) {
1530			ret = res_query(cp, C_IN, rtl->rtl_type, buf->buf,
1531					     sizeof(buf->buf));
1532			if (ret > 0 && ret < sizeof(buf->buf)) {
1533				hpbuf.h_addrtype = (rtl->rtl_type == T_AAAA)
1534				    ? AF_INET6 : AF_INET;
1535				hpbuf.h_length = ADDRLEN(hpbuf.h_addrtype);
1536				hp = getanswer(buf, ret, name, rtl->rtl_type,
1537						    &hpbuf, errp);
1538				if (!hp)
1539					continue;
1540				hp = _hpcopy(&hpbuf, errp);
1541				hp0 = _hpmerge(hp0, hp, errp);
1542			}
1543		}
1544		free(buf);
1545		return (hp0);
1546	}
1547
1548	/*
1549	 * If there are dots in the name already, let's just give it a try
1550	 * 'as is'.  The threshold can be set with the "ndots" option.
1551	 */
1552	saved_herrno = -1;
1553	if (dots >= _res.ndots) {
1554		for(rtl = rtl0; rtl != NULL;
1555		    rtl = SLIST_NEXT(rtl, rtl_entry)) {
1556			ret = res_querydomain(name, NULL, C_IN, rtl->rtl_type,
1557					      buf->buf, sizeof(buf->buf));
1558			if (ret > 0 && ret < sizeof(buf->buf)) {
1559				hpbuf.h_addrtype = (rtl->rtl_type == T_AAAA)
1560				    ? AF_INET6 : AF_INET;
1561				hpbuf.h_length = ADDRLEN(hpbuf.h_addrtype);
1562				hp = getanswer(buf, ret, name, rtl->rtl_type,
1563						    &hpbuf, errp);
1564				if (!hp)
1565					continue;
1566				hp = _hpcopy(&hpbuf, errp);
1567				hp0 = _hpmerge(hp0, hp, errp);
1568			}
1569		}
1570		if (hp0 != NULL) {
1571			free(buf);
1572			return (hp0);
1573		}
1574		saved_herrno = *errp;
1575		tried_as_is++;
1576	}
1577
1578	/*
1579	 * We do at least one level of search if
1580	 *	- there is no dot and RES_DEFNAME is set, or
1581	 *	- there is at least one dot, there is no trailing dot,
1582	 *	  and RES_DNSRCH is set.
1583	 */
1584	if ((!dots && (_res.options & RES_DEFNAMES)) ||
1585	    (dots && !trailing_dot && (_res.options & RES_DNSRCH))) {
1586		int done = 0;
1587
1588		for (domain = (const char * const *)_res.dnsrch;
1589		     *domain && !done;
1590		     domain++) {
1591
1592			for(rtl = rtl0; rtl != NULL;
1593			    rtl = SLIST_NEXT(rtl, rtl_entry)) {
1594				ret = res_querydomain(name, *domain, C_IN,
1595						      rtl->rtl_type,
1596						      buf->buf, sizeof(buf->buf));
1597				if (ret > 0 && ret < sizeof(buf->buf)) {
1598					hpbuf.h_addrtype = (rtl->rtl_type == T_AAAA)
1599					    ? AF_INET6 : AF_INET;
1600					hpbuf.h_length = ADDRLEN(hpbuf.h_addrtype);
1601					hp = getanswer(buf, ret, name,
1602					    rtl->rtl_type, &hpbuf, errp);
1603					if (!hp)
1604						continue;
1605					hp = _hpcopy(&hpbuf, errp);
1606					hp0 = _hpmerge(hp0, hp, errp);
1607				}
1608			}
1609			if (hp0 != NULL) {
1610				free(buf);
1611				return (hp0);
1612			}
1613
1614			/*
1615			 * If no server present, give up.
1616			 * If name isn't found in this domain,
1617			 * keep trying higher domains in the search list
1618			 * (if that's enabled).
1619			 * On a NO_DATA error, keep trying, otherwise
1620			 * a wildcard entry of another type could keep us
1621			 * from finding this entry higher in the domain.
1622			 * If we get some other error (negative answer or
1623			 * server failure), then stop searching up,
1624			 * but try the input name below in case it's
1625			 * fully-qualified.
1626			 */
1627			if (errno == ECONNREFUSED) {
1628				free(buf);
1629				*errp = TRY_AGAIN;
1630				return (NULL);
1631			}
1632
1633			switch (*errp) {
1634			case NO_DATA:
1635				got_nodata++;
1636				/* FALLTHROUGH */
1637			case HOST_NOT_FOUND:
1638				/* keep trying */
1639				break;
1640			case TRY_AGAIN:
1641				if (buf->hdr.rcode == SERVFAIL) {
1642					/* try next search element, if any */
1643					got_servfail++;
1644					break;
1645				}
1646				/* FALLTHROUGH */
1647			default:
1648				/* anything else implies that we're done */
1649				done++;
1650			}
1651
1652			/* if we got here for some reason other than DNSRCH,
1653			 * we only wanted one iteration of the loop, so stop.
1654			 */
1655			if (!(_res.options & RES_DNSRCH))
1656				done++;
1657		}
1658	}
1659
1660	/*
1661	 * If we have not already tried the name "as is", do that now.
1662	 * note that we do this regardless of how many dots were in the
1663	 * name or whether it ends with a dot unless NOTLDQUERY is set.
1664	 */
1665	if (!tried_as_is && (dots || !(_res.options & RES_NOTLDQUERY))) {
1666		for(rtl = rtl0; rtl != NULL;
1667		    rtl = SLIST_NEXT(rtl, rtl_entry)) {
1668			ret = res_querydomain(name, NULL, C_IN, rtl->rtl_type,
1669					      buf->buf, sizeof(buf->buf));
1670			if (ret > 0 && ret < sizeof(buf->buf)) {
1671				hpbuf.h_addrtype = (rtl->rtl_type == T_AAAA)
1672				    ? AF_INET6 : AF_INET;
1673				hpbuf.h_length = ADDRLEN(hpbuf.h_addrtype);
1674				hp = getanswer(buf, ret, name, rtl->rtl_type,
1675				    &hpbuf, errp);
1676				if (!hp)
1677					continue;
1678				hp = _hpcopy(&hpbuf, errp);
1679				hp0 = _hpmerge(hp0, hp, errp);
1680			}
1681		}
1682		if (hp0 != NULL) {
1683			free(buf);
1684			return (hp0);
1685		}
1686	}
1687
1688	free(buf);
1689
1690	/* if we got here, we didn't satisfy the search.
1691	 * if we did an initial full query, return that query's h_errno
1692	 * (note that we wouldn't be here if that query had succeeded).
1693	 * else if we ever got a nodata, send that back as the reason.
1694	 * else send back meaningless h_errno, that being the one from
1695	 * the last DNSRCH we did.
1696	 */
1697	if (saved_herrno != -1)
1698		*errp = saved_herrno;
1699	else if (got_nodata)
1700		*errp = NO_DATA;
1701	else if (got_servfail)
1702		*errp = TRY_AGAIN;
1703	return (NULL);
1704}
1705
1706static int
1707_dns_ghbyname(void *rval, void *cb_data, va_list ap)
1708{
1709	const char *name;
1710	int af;
1711	int *errp;
1712	struct __res_type_list *rtl, rtl4;
1713#ifdef INET6
1714	struct __res_type_list rtl6;
1715#endif
1716
1717	name = va_arg(ap, const char *);
1718	af = va_arg(ap, int);
1719	errp = va_arg(ap, int *);
1720
1721#ifdef INET6
1722	switch (af) {
1723	case AF_UNSPEC:
1724		SLIST_NEXT(&rtl4, rtl_entry) = NULL; rtl4.rtl_type = T_A;
1725		SLIST_NEXT(&rtl6, rtl_entry) = &rtl4; rtl6.rtl_type = T_AAAA;
1726		rtl = &rtl6;
1727		break;
1728	case AF_INET6:
1729		SLIST_NEXT(&rtl6, rtl_entry) = NULL; rtl6.rtl_type = T_AAAA;
1730		rtl = &rtl6;
1731		break;
1732	case AF_INET:
1733		SLIST_NEXT(&rtl4, rtl_entry) = NULL; rtl4.rtl_type = T_A;
1734		rtl = &rtl4;
1735		break;
1736	}
1737#else
1738	SLIST_NEXT(&rtl4, rtl_entry) = NULL; rtl4.rtl_type = T_A;
1739	rtl = &rtl4;
1740#endif
1741	*(struct hostent **)rval = _res_search_multi(name, rtl, errp);
1742	return (*(struct hostent **)rval != NULL) ? NS_SUCCESS : NS_NOTFOUND;
1743}
1744
1745static int
1746_dns_ghbyaddr(void *rval, void *cb_data, va_list ap)
1747{
1748	const void *addr;
1749	int addrlen;
1750	int af;
1751	int *errp;
1752	int n;
1753	int err;
1754	struct hostent *hp;
1755	u_char c, *cp;
1756	char *bp;
1757	struct hostent hbuf;
1758	int na;
1759#ifdef INET6
1760	static const char hex[] = "0123456789abcdef";
1761#endif
1762	querybuf *buf;
1763	char qbuf[MAXDNAME+1];
1764	char *hlist[2];
1765	char *tld6[] = { "ip6.arpa", "ip6.int", NULL };
1766	char *tld4[] = { "in-addr.arpa", NULL };
1767	char **tld;
1768
1769	addr = va_arg(ap, const void *);
1770	addrlen = va_arg(ap, int);
1771	af = va_arg(ap, int);
1772	errp = va_arg(ap, int *);
1773
1774	*(struct hostent **)rval = NULL;
1775
1776#ifdef INET6
1777	/* XXX */
1778	if (af == AF_INET6 && IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)addr))
1779		return NS_NOTFOUND;
1780#endif
1781
1782	switch (af) {
1783#ifdef INET6
1784	case AF_INET6:
1785		tld = tld6;
1786		break;
1787#endif
1788	case AF_INET:
1789		tld = tld4;
1790		break;
1791	default:
1792		return NS_NOTFOUND;
1793	}
1794
1795	if ((_res.options & RES_INIT) == 0) {
1796		if (res_init() < 0) {
1797			*errp = h_errno;
1798			return NS_UNAVAIL;
1799		}
1800	}
1801	memset(&hbuf, 0, sizeof(hbuf));
1802	hbuf.h_name = NULL;
1803	hbuf.h_addrtype = af;
1804	hbuf.h_length = addrlen;
1805	na = 0;
1806
1807	buf = malloc(sizeof(*buf));
1808	if (buf == NULL) {
1809		*errp = NETDB_INTERNAL;
1810		return NS_UNAVAIL;
1811	}
1812	err = NS_SUCCESS;
1813	for (/* nothing */; *tld; tld++) {
1814		/*
1815		 * XXX assumes that MAXDNAME is big enough - error checks
1816		 * has been made by callers
1817		 */
1818		n = 0;
1819		bp = qbuf;
1820		cp = (u_char *)addr+addrlen-1;
1821		switch (af) {
1822#ifdef INET6
1823		case AF_INET6:
1824			for (; n < addrlen; n++, cp--) {
1825				c = *cp;
1826				*bp++ = hex[c & 0xf];
1827				*bp++ = '.';
1828				*bp++ = hex[c >> 4];
1829				*bp++ = '.';
1830			}
1831			strcpy(bp, *tld);
1832			break;
1833#endif
1834		case AF_INET:
1835			for (; n < addrlen; n++, cp--) {
1836				c = *cp;
1837				if (c >= 100)
1838					*bp++ = '0' + c / 100;
1839				if (c >= 10)
1840					*bp++ = '0' + (c % 100) / 10;
1841				*bp++ = '0' + c % 10;
1842				*bp++ = '.';
1843			}
1844			strcpy(bp, *tld);
1845			break;
1846		}
1847
1848		n = res_query(qbuf, C_IN, T_PTR, buf->buf, sizeof buf->buf);
1849		if (n < 0) {
1850			*errp = h_errno;
1851			err = NS_UNAVAIL;
1852			continue;
1853		} else if (n > sizeof(buf->buf)) {
1854#if 0
1855			errno = ERANGE; /* XXX is it OK to set errno here? */
1856#endif
1857			*errp = NETDB_INTERNAL;
1858			err = NS_UNAVAIL;
1859			continue;
1860		}
1861		hp = getanswer(buf, n, qbuf, T_PTR, &hbuf, errp);
1862		if (!hp) {
1863			err = NS_NOTFOUND;
1864			continue;
1865		}
1866		free(buf);
1867		hbuf.h_addrtype = af;
1868		hbuf.h_length = addrlen;
1869		hbuf.h_addr_list = hlist;
1870		hlist[0] = (char *)addr;
1871		hlist[1] = NULL;
1872		*(struct hostent **)rval = _hpcopy(&hbuf, errp);
1873		return NS_SUCCESS;
1874	}
1875	free(buf);
1876	return err;
1877}
1878
1879static void
1880_dns_shent(int stayopen)
1881{
1882	if ((_res.options & RES_INIT) == 0) {
1883		if (res_init() < 0)
1884			return;
1885	}
1886	if (stayopen)
1887		_res.options |= RES_STAYOPEN | RES_USEVC;
1888}
1889
1890static void
1891_dns_ehent(void)
1892{
1893	_res.options &= ~(RES_STAYOPEN | RES_USEVC);
1894	res_close();
1895}
1896
1897#ifdef ICMPNL
1898
1899/*
1900 * experimental:
1901 *	draft-ietf-ipngwg-icmp-namelookups-02.txt
1902 *	ifindex is assumed to be encoded in addr.
1903 */
1904#include <sys/uio.h>
1905#include <netinet/ip6.h>
1906#include <netinet/icmp6.h>
1907
1908struct _icmp_host_cache {
1909	struct _icmp_host_cache *hc_next;
1910	int hc_ifindex;
1911	struct in6_addr hc_addr;
1912	char *hc_name;
1913};
1914
1915static char *
1916_icmp_fqdn_query(const struct in6_addr *addr, int ifindex)
1917{
1918	int s;
1919	struct icmp6_filter filter;
1920	struct msghdr msg;
1921	struct cmsghdr *cmsg;
1922	struct in6_pktinfo *pkt;
1923	char cbuf[256];
1924	char buf[1024];
1925	int cc;
1926	struct icmp6_fqdn_query *fq;
1927	struct icmp6_fqdn_reply *fr;
1928	struct _icmp_host_cache *hc;
1929	struct sockaddr_in6 sin6;
1930	struct iovec iov;
1931	fd_set s_fds, fds;
1932	struct timeval tout;
1933	int len;
1934	char *name;
1935	static int pid;
1936	static struct _icmp_host_cache *hc_head;
1937
1938	for (hc = hc_head; hc; hc = hc->hc_next) {
1939		if (hc->hc_ifindex == ifindex
1940		&&  IN6_ARE_ADDR_EQUAL(&hc->hc_addr, addr))
1941			return hc->hc_name;
1942	}
1943
1944	if (pid == 0)
1945		pid = getpid();
1946
1947	ICMP6_FILTER_SETBLOCKALL(&filter);
1948	ICMP6_FILTER_SETPASS(ICMP6_FQDN_REPLY, &filter);
1949
1950	FD_ZERO(&s_fds);
1951	tout.tv_sec = 0;
1952	tout.tv_usec = 200000;	/*XXX: 200ms*/
1953
1954	fq = (struct icmp6_fqdn_query *)buf;
1955	fq->icmp6_fqdn_type = ICMP6_FQDN_QUERY;
1956	fq->icmp6_fqdn_code = 0;
1957	fq->icmp6_fqdn_cksum = 0;
1958	fq->icmp6_fqdn_id = (u_short)pid;
1959	fq->icmp6_fqdn_unused = 0;
1960	fq->icmp6_fqdn_cookie[0] = 0;
1961	fq->icmp6_fqdn_cookie[1] = 0;
1962
1963	memset(&sin6, 0, sizeof(sin6));
1964	sin6.sin6_family = AF_INET6;
1965	sin6.sin6_addr = *addr;
1966
1967	memset(&msg, 0, sizeof(msg));
1968	msg.msg_name = (caddr_t)&sin6;
1969	msg.msg_namelen = sizeof(sin6);
1970	msg.msg_iov = &iov;
1971	msg.msg_iovlen = 1;
1972	msg.msg_control = NULL;
1973	msg.msg_controllen = 0;
1974	iov.iov_base = (caddr_t)buf;
1975	iov.iov_len = sizeof(struct icmp6_fqdn_query);
1976
1977	if (ifindex) {
1978		msg.msg_control = cbuf;
1979		msg.msg_controllen = sizeof(cbuf);
1980		cmsg = CMSG_FIRSTHDR(&msg);
1981		cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
1982		cmsg->cmsg_level = IPPROTO_IPV6;
1983		cmsg->cmsg_type = IPV6_PKTINFO;
1984		pkt = (struct in6_pktinfo *)&cmsg[1];
1985		memset(&pkt->ipi6_addr, 0, sizeof(struct in6_addr));
1986		pkt->ipi6_ifindex = ifindex;
1987		cmsg = CMSG_NXTHDR(&msg, cmsg);
1988		msg.msg_controllen = (char *)cmsg - cbuf;
1989	}
1990
1991	if ((s = _socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0)
1992		return NULL;
1993	(void)_setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER,
1994			 (char *)&filter, sizeof(filter));
1995	cc = _sendmsg(s, &msg, 0);
1996	if (cc < 0) {
1997		_close(s);
1998		return NULL;
1999	}
2000	FD_SET(s, &s_fds);
2001	for (;;) {
2002		fds = s_fds;
2003		if (_select(s + 1, &fds, NULL, NULL, &tout) <= 0) {
2004			_close(s);
2005			return NULL;
2006		}
2007		len = sizeof(sin6);
2008		cc = _recvfrom(s, buf, sizeof(buf), 0,
2009			      (struct sockaddr *)&sin6, &len);
2010		if (cc <= 0) {
2011			_close(s);
2012			return NULL;
2013		}
2014		if (cc < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr))
2015			continue;
2016		if (!IN6_ARE_ADDR_EQUAL(addr, &sin6.sin6_addr))
2017			continue;
2018		fr = (struct icmp6_fqdn_reply *)(buf + sizeof(struct ip6_hdr));
2019		if (fr->icmp6_fqdn_type == ICMP6_FQDN_REPLY)
2020			break;
2021	}
2022	_close(s);
2023	if (fr->icmp6_fqdn_cookie[1] != 0) {
2024		/* rfc1788 type */
2025		name = buf + sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) + 4;
2026		len = (buf + cc) - name;
2027	} else {
2028		len = fr->icmp6_fqdn_namelen;
2029		name = fr->icmp6_fqdn_name;
2030	}
2031	if (len <= 0)
2032		return NULL;
2033	name[len] = 0;
2034
2035	if ((hc = (struct _icmp_host_cache *)malloc(sizeof(*hc))) == NULL)
2036		return NULL;
2037	/* XXX: limit number of cached entries */
2038	hc->hc_ifindex = ifindex;
2039	hc->hc_addr = *addr;
2040	hc->hc_name = strdup(name);
2041	hc->hc_next = hc_head;
2042	hc_head = hc;
2043	return hc->hc_name;
2044}
2045
2046static struct hostent *
2047_icmp_ghbyaddr(const void *addr, int addrlen, int af, int *errp)
2048{
2049	char *hname;
2050	int ifindex;
2051	struct in6_addr addr6;
2052
2053	if (af != AF_INET6) {
2054		/*
2055		 * Note: rfc1788 defines Who Are You for IPv4,
2056		 * but no one implements it.
2057		 */
2058		return NULL;
2059	}
2060
2061	memcpy(&addr6, addr, addrlen);
2062	ifindex = (addr6.s6_addr[2] << 8) | addr6.s6_addr[3];
2063	addr6.s6_addr[2] = addr6.s6_addr[3] = 0;
2064
2065	if (!IN6_IS_ADDR_LINKLOCAL(&addr6))
2066		return NULL;	/*XXX*/
2067
2068	if ((hname = _icmp_fqdn_query(&addr6, ifindex)) == NULL)
2069		return NULL;
2070	return _hpaddr(af, hname, &addr6, errp);
2071}
2072#endif /* ICMPNL */
2073