10Sstevel@tonic-gate /*
2*9433SStacey.Marshall@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
30Sstevel@tonic-gate  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
60Sstevel@tonic-gate /*
70Sstevel@tonic-gate  * Copyright (c) 1985, 1988, 1993
80Sstevel@tonic-gate  *    The Regents of the University of California.  All rights reserved.
90Sstevel@tonic-gate  *
100Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
110Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
120Sstevel@tonic-gate  * are met:
130Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
140Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
150Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
160Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
170Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
180Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
190Sstevel@tonic-gate  *    must display the following acknowledgement:
200Sstevel@tonic-gate  * 	This product includes software developed by the University of
210Sstevel@tonic-gate  * 	California, Berkeley and its contributors.
220Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
230Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
240Sstevel@tonic-gate  *    without specific prior written permission.
250Sstevel@tonic-gate  *
260Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
270Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
280Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
290Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
300Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
310Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
320Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
330Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
340Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
350Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
360Sstevel@tonic-gate  * SUCH DAMAGE.
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate /*
400Sstevel@tonic-gate  * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
410Sstevel@tonic-gate  *
420Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
430Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
440Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
450Sstevel@tonic-gate  *
460Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
470Sstevel@tonic-gate  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
480Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
490Sstevel@tonic-gate  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
500Sstevel@tonic-gate  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
510Sstevel@tonic-gate  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
520Sstevel@tonic-gate  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
530Sstevel@tonic-gate  * SOFTWARE.
540Sstevel@tonic-gate  */
550Sstevel@tonic-gate 
560Sstevel@tonic-gate /* from gethostnamadr.c	8.1 (Berkeley) 6/4/93 */
570Sstevel@tonic-gate /* BIND Id: gethnamaddr.c,v 8.15 1996/05/22 04:56:30 vixie Exp $ */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
600Sstevel@tonic-gate static const char rcsid[] = "$Id: dns_ho.c,v 1.43 2003/05/27 23:36:52 marka Exp $";
610Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
620Sstevel@tonic-gate 
630Sstevel@tonic-gate /* Imports. */
640Sstevel@tonic-gate 
650Sstevel@tonic-gate #include "port_before.h"
660Sstevel@tonic-gate 
670Sstevel@tonic-gate #include <sys/types.h>
680Sstevel@tonic-gate #include <sys/param.h>
690Sstevel@tonic-gate #include <sys/socket.h>
700Sstevel@tonic-gate 
710Sstevel@tonic-gate #include <netinet/in.h>
720Sstevel@tonic-gate #include <arpa/inet.h>
730Sstevel@tonic-gate #include <arpa/nameser.h>
740Sstevel@tonic-gate 
750Sstevel@tonic-gate #include <ctype.h>
760Sstevel@tonic-gate #include <errno.h>
770Sstevel@tonic-gate #include <stdlib.h>
780Sstevel@tonic-gate #include <netdb.h>
790Sstevel@tonic-gate #include <resolv.h>
800Sstevel@tonic-gate #include <stdio.h>
810Sstevel@tonic-gate #include <string.h>
820Sstevel@tonic-gate #include <syslog.h>
830Sstevel@tonic-gate 
840Sstevel@tonic-gate #include <isc/memcluster.h>
850Sstevel@tonic-gate #include <irs.h>
860Sstevel@tonic-gate 
870Sstevel@tonic-gate #include "port_after.h"
880Sstevel@tonic-gate 
890Sstevel@tonic-gate #include "irs_p.h"
900Sstevel@tonic-gate #include "dns_p.h"
910Sstevel@tonic-gate 
920Sstevel@tonic-gate #ifdef SPRINTF_CHAR
930Sstevel@tonic-gate # define SPRINTF(x) strlen(sprintf/**/x)
940Sstevel@tonic-gate #else
950Sstevel@tonic-gate # define SPRINTF(x) sprintf x
960Sstevel@tonic-gate #endif
970Sstevel@tonic-gate 
980Sstevel@tonic-gate /* Definitions. */
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate #define	MAXALIASES	35
1010Sstevel@tonic-gate #define	MAXADDRS	35
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate #define MAXPACKET (65535)	/* Maximum TCP message size */
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate #define BOUNDS_CHECK(ptr, count) \
1060Sstevel@tonic-gate 	if ((ptr) + (count) > eom) { \
1070Sstevel@tonic-gate 		had_error++; \
1080Sstevel@tonic-gate 		continue; \
1090Sstevel@tonic-gate 	} else (void)0
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate typedef union {
1120Sstevel@tonic-gate 	HEADER hdr;
1130Sstevel@tonic-gate 	u_char buf[MAXPACKET];
1140Sstevel@tonic-gate } querybuf;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate struct dns_res_target {
1170Sstevel@tonic-gate 	struct dns_res_target *next;
1180Sstevel@tonic-gate 	querybuf qbuf;		/* query buffer */
1190Sstevel@tonic-gate 	u_char *answer;		/* buffer to put answer */
1200Sstevel@tonic-gate 	int anslen;		/* size of answer buffer */
1210Sstevel@tonic-gate 	int qclass, qtype;	/* class and type of query */
1220Sstevel@tonic-gate 	int action;		/* condition whether query is really issued */
1230Sstevel@tonic-gate 	char qname[MAXDNAME +1]; /* domain name */
1240Sstevel@tonic-gate #if 0
1250Sstevel@tonic-gate 	int n;			/* result length */
1260Sstevel@tonic-gate #endif
1270Sstevel@tonic-gate };
1280Sstevel@tonic-gate enum {RESTGT_DOALWAYS, RESTGT_AFTERFAILURE, RESTGT_IGNORE};
1290Sstevel@tonic-gate enum {RESQRY_SUCCESS, RESQRY_FAIL};
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate struct pvt {
1320Sstevel@tonic-gate 	struct hostent	host;
1330Sstevel@tonic-gate 	char *		h_addr_ptrs[MAXADDRS + 1];
1340Sstevel@tonic-gate 	char *		host_aliases[MAXALIASES];
1350Sstevel@tonic-gate 	char		hostbuf[8*1024];
1360Sstevel@tonic-gate 	u_char		host_addr[16];	/* IPv4 or IPv6 */
1370Sstevel@tonic-gate 	struct __res_state  *res;
1380Sstevel@tonic-gate 	void		(*free_res)(void *);
1390Sstevel@tonic-gate };
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate typedef union {
1420Sstevel@tonic-gate 	int32_t al;
1430Sstevel@tonic-gate 	char ac;
1440Sstevel@tonic-gate } align;
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
1470Sstevel@tonic-gate static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
1480Sstevel@tonic-gate /* Note: the IPv6 loopback address is in the "tunnel" space */
1490Sstevel@tonic-gate static const u_char v6local[] = { 0,0, 0,1 }; /* last 4 bytes of IPv6 addr */
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate /* Forwards. */
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate static void		ho_close(struct irs_ho *this);
1540Sstevel@tonic-gate static struct hostent *	ho_byname(struct irs_ho *this, const char *name);
1550Sstevel@tonic-gate static struct hostent *	ho_byname2(struct irs_ho *this, const char *name,
1560Sstevel@tonic-gate 				   int af);
1570Sstevel@tonic-gate static struct hostent *	ho_byaddr(struct irs_ho *this, const void *addr,
1580Sstevel@tonic-gate 				  int len, int af);
1590Sstevel@tonic-gate static struct hostent *	ho_next(struct irs_ho *this);
1600Sstevel@tonic-gate static void		ho_rewind(struct irs_ho *this);
1610Sstevel@tonic-gate static void		ho_minimize(struct irs_ho *this);
1620Sstevel@tonic-gate static struct __res_state * ho_res_get(struct irs_ho *this);
1630Sstevel@tonic-gate static void		ho_res_set(struct irs_ho *this,
1640Sstevel@tonic-gate 				   struct __res_state *res,
1650Sstevel@tonic-gate 				   void (*free_res)(void *));
1660Sstevel@tonic-gate static struct addrinfo * ho_addrinfo(struct irs_ho *this, const char *name,
1670Sstevel@tonic-gate 				     const struct addrinfo *pai);
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate static void		map_v4v6_hostent(struct hostent *hp, char **bp,
1700Sstevel@tonic-gate 					 char *ep);
1710Sstevel@tonic-gate static void		addrsort(res_state, char **, int);
1720Sstevel@tonic-gate static struct hostent *	gethostans(struct irs_ho *this,
1730Sstevel@tonic-gate 				   const u_char *ansbuf, int anslen,
1740Sstevel@tonic-gate 				   const char *qname, int qtype,
1750Sstevel@tonic-gate 				   int af, int size,
1760Sstevel@tonic-gate 				   struct addrinfo **ret_aip,
1770Sstevel@tonic-gate 				   const struct addrinfo *pai);
1780Sstevel@tonic-gate static int add_hostent(struct pvt *pvt, char *bp, char **hap,
1790Sstevel@tonic-gate 		       struct addrinfo *ai);
1800Sstevel@tonic-gate static int		init(struct irs_ho *this);
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate /* Exports. */
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate struct irs_ho *
1850Sstevel@tonic-gate irs_dns_ho(struct irs_acc *this) {
1860Sstevel@tonic-gate 	struct irs_ho *ho;
1870Sstevel@tonic-gate 	struct pvt *pvt;
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	UNUSED(this);
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	if (!(pvt = memget(sizeof *pvt))) {
1920Sstevel@tonic-gate 		errno = ENOMEM;
1930Sstevel@tonic-gate 		return (NULL);
1940Sstevel@tonic-gate 	}
1950Sstevel@tonic-gate 	memset(pvt, 0, sizeof *pvt);
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	if (!(ho = memget(sizeof *ho))) {
1980Sstevel@tonic-gate 		memput(pvt, sizeof *pvt);
1990Sstevel@tonic-gate 		errno = ENOMEM;
2000Sstevel@tonic-gate 		return (NULL);
2010Sstevel@tonic-gate 	}
2020Sstevel@tonic-gate 	memset(ho, 0x5e, sizeof *ho);
2030Sstevel@tonic-gate 	ho->private = pvt;
2040Sstevel@tonic-gate 	ho->close = ho_close;
2050Sstevel@tonic-gate 	ho->byname = ho_byname;
2060Sstevel@tonic-gate 	ho->byname2 = ho_byname2;
2070Sstevel@tonic-gate 	ho->byaddr = ho_byaddr;
2080Sstevel@tonic-gate 	ho->next = ho_next;
2090Sstevel@tonic-gate 	ho->rewind = ho_rewind;
2100Sstevel@tonic-gate 	ho->minimize = ho_minimize;
2110Sstevel@tonic-gate 	ho->res_get = ho_res_get;
2120Sstevel@tonic-gate 	ho->res_set = ho_res_set;
2130Sstevel@tonic-gate 	ho->addrinfo = ho_addrinfo;
2140Sstevel@tonic-gate 	return (ho);
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate /* Methods. */
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate static void
2200Sstevel@tonic-gate ho_close(struct irs_ho *this) {
2210Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	ho_minimize(this);
2240Sstevel@tonic-gate 	if (pvt->res && pvt->free_res)
2250Sstevel@tonic-gate 		(*pvt->free_res)(pvt->res);
2260Sstevel@tonic-gate 	if (pvt)
2270Sstevel@tonic-gate 		memput(pvt, sizeof *pvt);
2280Sstevel@tonic-gate 	memput(this, sizeof *this);
2290Sstevel@tonic-gate }
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate static struct hostent *
2320Sstevel@tonic-gate ho_byname(struct irs_ho *this, const char *name) {
2330Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2340Sstevel@tonic-gate 	struct hostent *hp;
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	if (init(this) == -1)
2370Sstevel@tonic-gate 		return (NULL);
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	if (pvt->res->options & RES_USE_INET6) {
2400Sstevel@tonic-gate 		hp = ho_byname2(this, name, AF_INET6);
2410Sstevel@tonic-gate 		if (hp)
2420Sstevel@tonic-gate 			return (hp);
2430Sstevel@tonic-gate 	}
2440Sstevel@tonic-gate 	return (ho_byname2(this, name, AF_INET));
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate static struct hostent *
2480Sstevel@tonic-gate ho_byname2(struct irs_ho *this, const char *name, int af)
2490Sstevel@tonic-gate {
2500Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2510Sstevel@tonic-gate 	struct hostent *hp = NULL;
2520Sstevel@tonic-gate 	int n, size;
2530Sstevel@tonic-gate 	char tmp[NS_MAXDNAME];
2540Sstevel@tonic-gate 	const char *cp;
2550Sstevel@tonic-gate 	struct addrinfo ai;
2560Sstevel@tonic-gate 	struct dns_res_target *q, *p;
2570Sstevel@tonic-gate 	int querystate = RESQRY_FAIL;
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	if (init(this) == -1)
2600Sstevel@tonic-gate 		return (NULL);
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	q = memget(sizeof(*q));
2630Sstevel@tonic-gate 	if (q == NULL) {
2640Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
2650Sstevel@tonic-gate 		errno = ENOMEM;
2660Sstevel@tonic-gate 		goto cleanup;
2670Sstevel@tonic-gate 	}
2680Sstevel@tonic-gate 	memset(q, 0, sizeof(q));
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	switch (af) {
2710Sstevel@tonic-gate 	case AF_INET:
2720Sstevel@tonic-gate 		size = INADDRSZ;
2730Sstevel@tonic-gate 		q->qclass = C_IN;
2740Sstevel@tonic-gate 		q->qtype = T_A;
2750Sstevel@tonic-gate 		q->answer = q->qbuf.buf;
2760Sstevel@tonic-gate 		q->anslen = sizeof(q->qbuf);
2770Sstevel@tonic-gate 		q->action = RESTGT_DOALWAYS;
2780Sstevel@tonic-gate 		break;
2790Sstevel@tonic-gate 	case AF_INET6:
2800Sstevel@tonic-gate 		size = IN6ADDRSZ;
2810Sstevel@tonic-gate 		q->qclass = C_IN;
2820Sstevel@tonic-gate 		q->qtype = T_AAAA;
2830Sstevel@tonic-gate 		q->answer = q->qbuf.buf;
2840Sstevel@tonic-gate 		q->anslen = sizeof(q->qbuf);
2850Sstevel@tonic-gate 		q->action = RESTGT_DOALWAYS;
2860Sstevel@tonic-gate 		break;
2870Sstevel@tonic-gate 	default:
2880Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
2890Sstevel@tonic-gate 		errno = EAFNOSUPPORT;
2900Sstevel@tonic-gate 		hp = NULL;
2910Sstevel@tonic-gate 		goto cleanup;
2920Sstevel@tonic-gate 	}
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	/*
2950Sstevel@tonic-gate 	 * if there aren't any dots, it could be a user-level alias.
2960Sstevel@tonic-gate 	 * this is also done in res_nquery() since we are not the only
2970Sstevel@tonic-gate 	 * function that looks up host names.
2980Sstevel@tonic-gate 	 */
2990Sstevel@tonic-gate 	if (!strchr(name, '.') && (cp = res_hostalias(pvt->res, name,
3000Sstevel@tonic-gate 						      tmp, sizeof tmp)))
3010Sstevel@tonic-gate 		name = cp;
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	for (p = q; p; p = p->next) {
3040Sstevel@tonic-gate 		switch(p->action) {
3050Sstevel@tonic-gate 		case RESTGT_DOALWAYS:
3060Sstevel@tonic-gate 			break;
3070Sstevel@tonic-gate 		case RESTGT_AFTERFAILURE:
3080Sstevel@tonic-gate 			if (querystate == RESQRY_SUCCESS)
3090Sstevel@tonic-gate 				continue;
3100Sstevel@tonic-gate 			break;
3110Sstevel@tonic-gate 		case RESTGT_IGNORE:
3120Sstevel@tonic-gate 			continue;
3130Sstevel@tonic-gate 		}
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 		if ((n = res_nsearch(pvt->res, name, p->qclass, p->qtype,
3160Sstevel@tonic-gate 				     p->answer, p->anslen)) < 0) {
3170Sstevel@tonic-gate 			querystate = RESQRY_FAIL;
3180Sstevel@tonic-gate 			continue;
3190Sstevel@tonic-gate 		}
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 		memset(&ai, 0, sizeof(ai));
3220Sstevel@tonic-gate 		ai.ai_family = af;
3230Sstevel@tonic-gate 		if ((hp = gethostans(this, p->answer, n, name, p->qtype,
3240Sstevel@tonic-gate 				     af, size, NULL,
3250Sstevel@tonic-gate 				     (const struct addrinfo *)&ai)) != NULL)
3260Sstevel@tonic-gate 			goto cleanup;	/* no more loop is necessary */
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 		querystate = RESQRY_FAIL;
3290Sstevel@tonic-gate 		continue;
3300Sstevel@tonic-gate 	}
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate  cleanup:
3330Sstevel@tonic-gate 	if (q != NULL)
3340Sstevel@tonic-gate 		memput(q, sizeof(*q));
3350Sstevel@tonic-gate 	return(hp);
3360Sstevel@tonic-gate }
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate static struct hostent *
3390Sstevel@tonic-gate ho_byaddr(struct irs_ho *this, const void *addr, int len, int af)
3400Sstevel@tonic-gate {
3410Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
3420Sstevel@tonic-gate 	const u_char *uaddr = addr;
3430Sstevel@tonic-gate 	char *qp;
3440Sstevel@tonic-gate 	struct hostent *hp = NULL;
3450Sstevel@tonic-gate 	struct addrinfo ai;
3460Sstevel@tonic-gate 	struct dns_res_target *q, *q2, *p;
3470Sstevel@tonic-gate 	int n, size, i;
3480Sstevel@tonic-gate 	int querystate = RESQRY_FAIL;
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	if (init(this) == -1)
3510Sstevel@tonic-gate 		return (NULL);
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	q = memget(sizeof(*q));
3540Sstevel@tonic-gate 	q2 = memget(sizeof(*q2));
3550Sstevel@tonic-gate 	if (q == NULL || q2 == NULL) {
3560Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
3570Sstevel@tonic-gate 		errno = ENOMEM;
3580Sstevel@tonic-gate 		goto cleanup;
3590Sstevel@tonic-gate 	}
3600Sstevel@tonic-gate 	memset(q, 0, sizeof(q));
3610Sstevel@tonic-gate 	memset(q2, 0, sizeof(q2));
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	if (af == AF_INET6 && len == IN6ADDRSZ &&
3640Sstevel@tonic-gate 	    (!memcmp(uaddr, mapped, sizeof mapped) ||
3650Sstevel@tonic-gate            (!memcmp(uaddr, tunnelled, sizeof tunnelled) &&
3660Sstevel@tonic-gate             memcmp(&uaddr[sizeof tunnelled], v6local, sizeof(v6local))))) {
3670Sstevel@tonic-gate 		/* Unmap. */
3680Sstevel@tonic-gate 		addr = (const char *)addr + sizeof mapped;
3690Sstevel@tonic-gate 		uaddr += sizeof mapped;
3700Sstevel@tonic-gate 		af = AF_INET;
3710Sstevel@tonic-gate 		len = INADDRSZ;
3720Sstevel@tonic-gate 	}
3730Sstevel@tonic-gate 	switch (af) {
3740Sstevel@tonic-gate 	case AF_INET:
3750Sstevel@tonic-gate 		size = INADDRSZ;
3760Sstevel@tonic-gate 		q->qclass = C_IN;
3770Sstevel@tonic-gate 		q->qtype = T_PTR;
3780Sstevel@tonic-gate 		q->answer = q->qbuf.buf;
3790Sstevel@tonic-gate 		q->anslen = sizeof(q->qbuf);
3800Sstevel@tonic-gate 		q->action = RESTGT_DOALWAYS;
3810Sstevel@tonic-gate 		break;
3820Sstevel@tonic-gate 	case AF_INET6:
3830Sstevel@tonic-gate 		size = IN6ADDRSZ;
3840Sstevel@tonic-gate 		q->qclass = C_IN;
3850Sstevel@tonic-gate 		q->qtype = T_PTR;
3860Sstevel@tonic-gate 		q->answer = q->qbuf.buf;
3870Sstevel@tonic-gate 		q->anslen = sizeof(q->qbuf);
3880Sstevel@tonic-gate 		q->next = q2;
3890Sstevel@tonic-gate 		q->action = RESTGT_DOALWAYS;
3900Sstevel@tonic-gate 		q2->qclass = C_IN;
3910Sstevel@tonic-gate 		q2->qtype = T_PTR;
3920Sstevel@tonic-gate 		q2->answer = q2->qbuf.buf;
3930Sstevel@tonic-gate 		q2->anslen = sizeof(q2->qbuf);
3940Sstevel@tonic-gate 		if ((pvt->res->options & RES_NO_NIBBLE2) != 0)
3950Sstevel@tonic-gate 			q2->action = RESTGT_IGNORE;
3960Sstevel@tonic-gate 		else
3970Sstevel@tonic-gate 			q2->action = RESTGT_AFTERFAILURE;
3980Sstevel@tonic-gate 		break;
3990Sstevel@tonic-gate 	default:
4000Sstevel@tonic-gate 		errno = EAFNOSUPPORT;
4010Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
4020Sstevel@tonic-gate 		hp = NULL;
4030Sstevel@tonic-gate 		goto cleanup;
4040Sstevel@tonic-gate 	}
4050Sstevel@tonic-gate 	if (size > len) {
4060Sstevel@tonic-gate 		errno = EINVAL;
4070Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
4080Sstevel@tonic-gate 		hp = NULL;
4090Sstevel@tonic-gate 		goto cleanup;
4100Sstevel@tonic-gate 	}
4110Sstevel@tonic-gate 	switch (af) {
4120Sstevel@tonic-gate 	case AF_INET:
4130Sstevel@tonic-gate 		qp = q->qname;
4140Sstevel@tonic-gate 		(void) sprintf(qp, "%u.%u.%u.%u.in-addr.arpa",
4150Sstevel@tonic-gate 			       (uaddr[3] & 0xff),
4160Sstevel@tonic-gate 			       (uaddr[2] & 0xff),
4170Sstevel@tonic-gate 			       (uaddr[1] & 0xff),
4180Sstevel@tonic-gate 			       (uaddr[0] & 0xff));
4190Sstevel@tonic-gate 		break;
4200Sstevel@tonic-gate 	case AF_INET6:
4210Sstevel@tonic-gate 		if (q->action != RESTGT_IGNORE) {
4220Sstevel@tonic-gate 			qp = q->qname;
4230Sstevel@tonic-gate 			for (n = IN6ADDRSZ - 1; n >= 0; n--) {
4240Sstevel@tonic-gate 				i = SPRINTF((qp, "%x.%x.",
4250Sstevel@tonic-gate 					       uaddr[n] & 0xf,
4260Sstevel@tonic-gate 					       (uaddr[n] >> 4) & 0xf));
4270Sstevel@tonic-gate 				if (i < 0)
4280Sstevel@tonic-gate 					abort();
4290Sstevel@tonic-gate 				qp += i;
4300Sstevel@tonic-gate 			}
4310Sstevel@tonic-gate #ifdef HAVE_STRLCAT
4320Sstevel@tonic-gate 			strlcat(q->qname, res_get_nibblesuffix(pvt->res),
4330Sstevel@tonic-gate 			    sizeof(q->qname));
4340Sstevel@tonic-gate #else
4350Sstevel@tonic-gate 			strcpy(qp, res_get_nibblesuffix(pvt->res));
4360Sstevel@tonic-gate #endif
4370Sstevel@tonic-gate 		}
4380Sstevel@tonic-gate 		if (q2->action != RESTGT_IGNORE) {
4390Sstevel@tonic-gate 			qp = q2->qname;
4400Sstevel@tonic-gate 			for (n = IN6ADDRSZ - 1; n >= 0; n--) {
4410Sstevel@tonic-gate 				i = SPRINTF((qp, "%x.%x.",
4420Sstevel@tonic-gate 					       uaddr[n] & 0xf,
4430Sstevel@tonic-gate 					       (uaddr[n] >> 4) & 0xf));
4440Sstevel@tonic-gate 				if (i < 0)
4450Sstevel@tonic-gate 					abort();
4460Sstevel@tonic-gate 				qp += i;
4470Sstevel@tonic-gate 			}
4480Sstevel@tonic-gate #ifdef HAVE_STRLCAT
4490Sstevel@tonic-gate 			strlcat(q->qname, res_get_nibblesuffix2(pvt->res),
4500Sstevel@tonic-gate 			    sizeof(q->qname));
4510Sstevel@tonic-gate #else
4520Sstevel@tonic-gate 			strcpy(qp, res_get_nibblesuffix2(pvt->res));
4530Sstevel@tonic-gate #endif
4540Sstevel@tonic-gate 		}
4550Sstevel@tonic-gate 		break;
4560Sstevel@tonic-gate 	default:
4570Sstevel@tonic-gate 		abort();
4580Sstevel@tonic-gate 	}
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	for (p = q; p; p = p->next) {
4610Sstevel@tonic-gate 		switch(p->action) {
4620Sstevel@tonic-gate 		case RESTGT_DOALWAYS:
4630Sstevel@tonic-gate 			break;
4640Sstevel@tonic-gate 		case RESTGT_AFTERFAILURE:
4650Sstevel@tonic-gate 			if (querystate == RESQRY_SUCCESS)
4660Sstevel@tonic-gate 				continue;
4670Sstevel@tonic-gate 			break;
4680Sstevel@tonic-gate 		case RESTGT_IGNORE:
4690Sstevel@tonic-gate 			continue;
4700Sstevel@tonic-gate 		}
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 		if ((n = res_nquery(pvt->res, p->qname, p->qclass, p->qtype,
4730Sstevel@tonic-gate 				    p->answer, p->anslen)) < 0) {
4740Sstevel@tonic-gate 			querystate = RESQRY_FAIL;
4750Sstevel@tonic-gate 			continue;
4760Sstevel@tonic-gate 		}
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 		memset(&ai, 0, sizeof(ai));
4790Sstevel@tonic-gate 		ai.ai_family = af;
4800Sstevel@tonic-gate 		hp = gethostans(this, p->answer, n, p->qname, T_PTR, af, size,
4810Sstevel@tonic-gate 				NULL, (const struct addrinfo *)&ai);
4820Sstevel@tonic-gate 		if (!hp) {
4830Sstevel@tonic-gate 			querystate = RESQRY_FAIL;
4840Sstevel@tonic-gate 			continue;
4850Sstevel@tonic-gate 		}
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 		memcpy(pvt->host_addr, addr, len);
4880Sstevel@tonic-gate 		pvt->h_addr_ptrs[0] = (char *)pvt->host_addr;
4890Sstevel@tonic-gate 		pvt->h_addr_ptrs[1] = NULL;
4900Sstevel@tonic-gate 		if (af == AF_INET && (pvt->res->options & RES_USE_INET6)) {
4910Sstevel@tonic-gate 			map_v4v6_address((char*)pvt->host_addr,
4920Sstevel@tonic-gate 					 (char*)pvt->host_addr);
4930Sstevel@tonic-gate 			pvt->host.h_addrtype = AF_INET6;
4940Sstevel@tonic-gate 			pvt->host.h_length = IN6ADDRSZ;
4950Sstevel@tonic-gate 		}
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, NETDB_SUCCESS);
4980Sstevel@tonic-gate 		goto cleanup;	/* no more loop is necessary. */
4990Sstevel@tonic-gate 	}
5000Sstevel@tonic-gate 	hp = NULL; /* H_ERRNO was set by subroutines */
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate  cleanup:
5030Sstevel@tonic-gate 	if (q != NULL)
5040Sstevel@tonic-gate 		memput(q, sizeof(*q));
5050Sstevel@tonic-gate 	if (q2 != NULL)
5060Sstevel@tonic-gate 		memput(q2, sizeof(*q2));
5070Sstevel@tonic-gate 	return(hp);
5080Sstevel@tonic-gate }
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate static struct hostent *
5110Sstevel@tonic-gate ho_next(struct irs_ho *this) {
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	UNUSED(this);
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	return (NULL);
5160Sstevel@tonic-gate }
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate static void
5190Sstevel@tonic-gate ho_rewind(struct irs_ho *this) {
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	UNUSED(this);
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	/* NOOP */
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate static void
5270Sstevel@tonic-gate ho_minimize(struct irs_ho *this) {
5280Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 	if (pvt->res)
5310Sstevel@tonic-gate 		res_nclose(pvt->res);
5320Sstevel@tonic-gate }
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate static struct __res_state *
5350Sstevel@tonic-gate ho_res_get(struct irs_ho *this) {
5360Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 	if (!pvt->res) {
5390Sstevel@tonic-gate 		struct __res_state *res;
5400Sstevel@tonic-gate 		res = (struct __res_state *)malloc(sizeof *res);
5410Sstevel@tonic-gate 		if (!res) {
5420Sstevel@tonic-gate 			errno = ENOMEM;
5430Sstevel@tonic-gate 			return (NULL);
5440Sstevel@tonic-gate 		}
5450Sstevel@tonic-gate 		memset(res, 0, sizeof *res);
5460Sstevel@tonic-gate 		ho_res_set(this, res, free);
5470Sstevel@tonic-gate 	}
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	return (pvt->res);
5500Sstevel@tonic-gate }
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate /* XXX */
5530Sstevel@tonic-gate extern struct addrinfo *addr2addrinfo __P((const struct addrinfo *,
5540Sstevel@tonic-gate 					   const char *));
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate static struct addrinfo *
5570Sstevel@tonic-gate ho_addrinfo(struct irs_ho *this, const char *name, const struct addrinfo *pai)
5580Sstevel@tonic-gate {
5590Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
5600Sstevel@tonic-gate 	int n;
5610Sstevel@tonic-gate 	char tmp[NS_MAXDNAME];
5620Sstevel@tonic-gate 	const char *cp;
5630Sstevel@tonic-gate 	struct dns_res_target *q, *q2, *p;
5640Sstevel@tonic-gate 	struct addrinfo sentinel, *cur;
5650Sstevel@tonic-gate 	int querystate = RESQRY_FAIL;
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	if (init(this) == -1)
5680Sstevel@tonic-gate 		return (NULL);
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 	memset(&sentinel, 0, sizeof(sentinel));
5710Sstevel@tonic-gate 	cur = &sentinel;
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	q = memget(sizeof(*q));
5740Sstevel@tonic-gate 	q2 = memget(sizeof(*q2));
5750Sstevel@tonic-gate 	if (q == NULL || q2 == NULL) {
5760Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
5770Sstevel@tonic-gate 		errno = ENOMEM;
5780Sstevel@tonic-gate 		goto cleanup;
5790Sstevel@tonic-gate 	}
5800Sstevel@tonic-gate 	memset(q, 0, sizeof(q2));
5810Sstevel@tonic-gate 	memset(q2, 0, sizeof(q2));
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	switch (pai->ai_family) {
5840Sstevel@tonic-gate 	case AF_UNSPEC:
5850Sstevel@tonic-gate 		/* prefer IPv6 */
5860Sstevel@tonic-gate 		q->qclass = C_IN;
5870Sstevel@tonic-gate 		q->qtype = T_AAAA;
5880Sstevel@tonic-gate 		q->answer = q->qbuf.buf;
5890Sstevel@tonic-gate 		q->anslen = sizeof(q->qbuf);
5900Sstevel@tonic-gate 		q->next = q2;
5910Sstevel@tonic-gate 		q->action = RESTGT_DOALWAYS;
5920Sstevel@tonic-gate 		q2->qclass = C_IN;
5930Sstevel@tonic-gate 		q2->qtype = T_A;
5940Sstevel@tonic-gate 		q2->answer = q2->qbuf.buf;
5950Sstevel@tonic-gate 		q2->anslen = sizeof(q2->qbuf);
5960Sstevel@tonic-gate 		q2->action = RESTGT_DOALWAYS;
5970Sstevel@tonic-gate 		break;
5980Sstevel@tonic-gate 	case AF_INET:
5990Sstevel@tonic-gate 		q->qclass = C_IN;
6000Sstevel@tonic-gate 		q->qtype = T_A;
6010Sstevel@tonic-gate 		q->answer = q->qbuf.buf;
6020Sstevel@tonic-gate 		q->anslen = sizeof(q->qbuf);
6030Sstevel@tonic-gate 		q->action = RESTGT_DOALWAYS;
6040Sstevel@tonic-gate 		break;
6050Sstevel@tonic-gate 	case AF_INET6:
6060Sstevel@tonic-gate 		q->qclass = C_IN;
6070Sstevel@tonic-gate 		q->qtype = T_AAAA;
6080Sstevel@tonic-gate 		q->answer = q->qbuf.buf;
6090Sstevel@tonic-gate 		q->anslen = sizeof(q->qbuf);
6100Sstevel@tonic-gate 		q->action = RESTGT_DOALWAYS;
6110Sstevel@tonic-gate 		break;
6120Sstevel@tonic-gate 	default:
6130Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); /* better error? */
6140Sstevel@tonic-gate 		goto cleanup;
6150Sstevel@tonic-gate 	}
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 	/*
6180Sstevel@tonic-gate 	 * if there aren't any dots, it could be a user-level alias.
6190Sstevel@tonic-gate 	 * this is also done in res_nquery() since we are not the only
6200Sstevel@tonic-gate 	 * function that looks up host names.
6210Sstevel@tonic-gate 	 */
6220Sstevel@tonic-gate 	if (!strchr(name, '.') && (cp = res_hostalias(pvt->res, name,
6230Sstevel@tonic-gate 						      tmp, sizeof tmp)))
6240Sstevel@tonic-gate 		name = cp;
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 	for (p = q; p; p = p->next) {
6270Sstevel@tonic-gate 		struct addrinfo *ai;
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 		switch(p->action) {
6300Sstevel@tonic-gate 		case RESTGT_DOALWAYS:
6310Sstevel@tonic-gate 			break;
6320Sstevel@tonic-gate 		case RESTGT_AFTERFAILURE:
6330Sstevel@tonic-gate 			if (querystate == RESQRY_SUCCESS)
6340Sstevel@tonic-gate 				continue;
6350Sstevel@tonic-gate 			break;
6360Sstevel@tonic-gate 		case RESTGT_IGNORE:
6370Sstevel@tonic-gate 			continue;
6380Sstevel@tonic-gate 		}
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 		if ((n = res_nsearch(pvt->res, name, p->qclass, p->qtype,
6410Sstevel@tonic-gate 				     p->answer, p->anslen)) < 0) {
6420Sstevel@tonic-gate 			querystate = RESQRY_FAIL;
6430Sstevel@tonic-gate 			continue;
6440Sstevel@tonic-gate 		}
6450Sstevel@tonic-gate 		(void)gethostans(this, p->answer, n, name, p->qtype,
6460Sstevel@tonic-gate 				 pai->ai_family, /* XXX: meaningless */
6470Sstevel@tonic-gate 				 0, &ai, pai);
6480Sstevel@tonic-gate 		if (ai) {
6490Sstevel@tonic-gate 			querystate = RESQRY_SUCCESS;
6500Sstevel@tonic-gate 			cur->ai_next = ai;
6510Sstevel@tonic-gate 			while (cur && cur->ai_next)
6520Sstevel@tonic-gate 				cur = cur->ai_next;
6530Sstevel@tonic-gate 		}
6540Sstevel@tonic-gate 		else
6550Sstevel@tonic-gate 			querystate = RESQRY_FAIL;
6560Sstevel@tonic-gate 	}
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate  cleanup:
6590Sstevel@tonic-gate 	if (q != NULL)
6600Sstevel@tonic-gate 		memput(q, sizeof(*q));
6610Sstevel@tonic-gate 	if (q2 != NULL)
6620Sstevel@tonic-gate 		memput(q2, sizeof(*q2));
6630Sstevel@tonic-gate 	return(sentinel.ai_next);
6640Sstevel@tonic-gate }
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate static void
6670Sstevel@tonic-gate ho_res_set(struct irs_ho *this, struct __res_state *res,
6680Sstevel@tonic-gate 		void (*free_res)(void *)) {
6690Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 	if (pvt->res && pvt->free_res) {
6720Sstevel@tonic-gate 		res_nclose(pvt->res);
6730Sstevel@tonic-gate 		(*pvt->free_res)(pvt->res);
6740Sstevel@tonic-gate 	}
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 	pvt->res = res;
6770Sstevel@tonic-gate 	pvt->free_res = free_res;
6780Sstevel@tonic-gate }
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate /* Private. */
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate static struct hostent *
6830Sstevel@tonic-gate gethostans(struct irs_ho *this,
6840Sstevel@tonic-gate 	   const u_char *ansbuf, int anslen, const char *qname, int qtype,
6850Sstevel@tonic-gate 	   int af, int size,	/* meaningless for addrinfo cases */
6860Sstevel@tonic-gate 	   struct addrinfo **ret_aip, const struct addrinfo *pai)
6870Sstevel@tonic-gate {
6880Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
6890Sstevel@tonic-gate 	int type, class, ancount, qdcount, n, haveanswer, had_error;
6900Sstevel@tonic-gate 	int error = NETDB_SUCCESS, arcount;
6910Sstevel@tonic-gate 	int (*name_ok)(const char *);
6920Sstevel@tonic-gate 	const HEADER *hp;
6930Sstevel@tonic-gate 	const u_char *eom;
6940Sstevel@tonic-gate 	const u_char *eor;
6950Sstevel@tonic-gate 	const u_char *cp;
6960Sstevel@tonic-gate 	const char *tname;
6970Sstevel@tonic-gate 	const char *hname;
6980Sstevel@tonic-gate 	char *bp, *ep, **ap, **hap;
6990Sstevel@tonic-gate 	char tbuf[MAXDNAME+1];
7000Sstevel@tonic-gate 	struct addrinfo sentinel, *cur, ai;
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 	if (pai == NULL) abort();
7030Sstevel@tonic-gate 	if (ret_aip != NULL)
7040Sstevel@tonic-gate 		*ret_aip = NULL;
7050Sstevel@tonic-gate 	memset(&sentinel, 0, sizeof(sentinel));
7060Sstevel@tonic-gate 	cur = &sentinel;
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 	tname = qname;
7090Sstevel@tonic-gate 	eom = ansbuf + anslen;
7100Sstevel@tonic-gate 	switch (qtype) {
7110Sstevel@tonic-gate 	case T_A:
7120Sstevel@tonic-gate 	case T_AAAA:
7130Sstevel@tonic-gate 	case T_ANY:	/* use T_ANY only for T_A/T_AAAA lookup */
7140Sstevel@tonic-gate 		name_ok = res_hnok;
7150Sstevel@tonic-gate 		break;
7160Sstevel@tonic-gate 	case T_PTR:
7170Sstevel@tonic-gate 		name_ok = res_dnok;
7180Sstevel@tonic-gate 		break;
7190Sstevel@tonic-gate 	default:
7200Sstevel@tonic-gate 		abort();
7210Sstevel@tonic-gate 	}
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 	pvt->host.h_addrtype = af;
7240Sstevel@tonic-gate 	pvt->host.h_length = size;
7250Sstevel@tonic-gate 	hname = pvt->host.h_name = NULL;
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 	/*
7280Sstevel@tonic-gate 	 * Find first satisfactory answer.
7290Sstevel@tonic-gate 	 */
7300Sstevel@tonic-gate 	if (ansbuf + HFIXEDSZ > eom) {
7310Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
7320Sstevel@tonic-gate 		return (NULL);
7330Sstevel@tonic-gate 	}
7340Sstevel@tonic-gate 	hp = (const HEADER *)ansbuf;
7350Sstevel@tonic-gate 	ancount = ntohs(hp->ancount);
7360Sstevel@tonic-gate 	qdcount = ntohs(hp->qdcount);
7370Sstevel@tonic-gate 	arcount = ntohs(hp->arcount);
7380Sstevel@tonic-gate 	bp = pvt->hostbuf;
7390Sstevel@tonic-gate 	ep = pvt->hostbuf + sizeof(pvt->hostbuf);
7400Sstevel@tonic-gate 	cp = ansbuf + HFIXEDSZ;
7410Sstevel@tonic-gate 	if (qdcount != 1) {
7420Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
7430Sstevel@tonic-gate 		return (NULL);
7440Sstevel@tonic-gate 	}
7450Sstevel@tonic-gate 	n = dn_expand(ansbuf, eom, cp, bp, ep - bp);
7460Sstevel@tonic-gate 	if (n < 0 || !maybe_ok(pvt->res, bp, name_ok)) {
7470Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
7480Sstevel@tonic-gate 		return (NULL);
7490Sstevel@tonic-gate 	}
7500Sstevel@tonic-gate 	cp += n + QFIXEDSZ;
7510Sstevel@tonic-gate 	if (cp > eom) {
7520Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
7530Sstevel@tonic-gate 		return (NULL);
7540Sstevel@tonic-gate 	}
7550Sstevel@tonic-gate 	if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
7560Sstevel@tonic-gate 		/* res_nsend() has already verified that the query name is the
7570Sstevel@tonic-gate 		 * same as the one we sent; this just gets the expanded name
7580Sstevel@tonic-gate 		 * (i.e., with the succeeding search-domain tacked on).
7590Sstevel@tonic-gate 		 */
7600Sstevel@tonic-gate 		n = strlen(bp) + 1;		/* for the \0 */
7610Sstevel@tonic-gate 		if (n > MAXHOSTNAMELEN) {
7620Sstevel@tonic-gate 			RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
7630Sstevel@tonic-gate 			return (NULL);
7640Sstevel@tonic-gate 		}
7650Sstevel@tonic-gate 		pvt->host.h_name = bp;
7660Sstevel@tonic-gate 		hname = bp;
7670Sstevel@tonic-gate 		bp += n;
7680Sstevel@tonic-gate 		/* The qname can be abbreviated, but hname is now absolute. */
7690Sstevel@tonic-gate 		qname = pvt->host.h_name;
7700Sstevel@tonic-gate 	}
7710Sstevel@tonic-gate 	ap = pvt->host_aliases;
7720Sstevel@tonic-gate 	*ap = NULL;
7730Sstevel@tonic-gate 	pvt->host.h_aliases = pvt->host_aliases;
7740Sstevel@tonic-gate 	hap = pvt->h_addr_ptrs;
7750Sstevel@tonic-gate 	*hap = NULL;
7760Sstevel@tonic-gate 	pvt->host.h_addr_list = pvt->h_addr_ptrs;
7770Sstevel@tonic-gate 	haveanswer = 0;
7780Sstevel@tonic-gate 	had_error = 0;
7790Sstevel@tonic-gate 	while (ancount-- > 0 && cp < eom && !had_error) {
7800Sstevel@tonic-gate 		n = dn_expand(ansbuf, eom, cp, bp, ep - bp);
7810Sstevel@tonic-gate 		if (n < 0 || !maybe_ok(pvt->res, bp, name_ok)) {
7820Sstevel@tonic-gate 			had_error++;
7830Sstevel@tonic-gate 			continue;
7840Sstevel@tonic-gate 		}
7850Sstevel@tonic-gate 		cp += n;			/* name */
7860Sstevel@tonic-gate 		BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
7870Sstevel@tonic-gate 		type = ns_get16(cp);
7880Sstevel@tonic-gate  		cp += INT16SZ;			/* type */
7890Sstevel@tonic-gate 		class = ns_get16(cp);
7900Sstevel@tonic-gate  		cp += INT16SZ + INT32SZ;	/* class, TTL */
7910Sstevel@tonic-gate 		n = ns_get16(cp);
7920Sstevel@tonic-gate 		cp += INT16SZ;			/* len */
7930Sstevel@tonic-gate 		BOUNDS_CHECK(cp, n);
7940Sstevel@tonic-gate 		if (class != C_IN) {
7950Sstevel@tonic-gate 			cp += n;
7960Sstevel@tonic-gate 			continue;
7970Sstevel@tonic-gate 		}
7980Sstevel@tonic-gate 		eor = cp + n;
7990Sstevel@tonic-gate 		if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) &&
8000Sstevel@tonic-gate 		    type == T_CNAME) {
8010Sstevel@tonic-gate 			if (haveanswer) {
8020Sstevel@tonic-gate 				int level = LOG_CRIT;
8030Sstevel@tonic-gate #ifdef LOG_SECURITY
8040Sstevel@tonic-gate 				level |= LOG_SECURITY;
8050Sstevel@tonic-gate #endif
8060Sstevel@tonic-gate 				syslog(level,
8070Sstevel@tonic-gate  "gethostans: possible attempt to exploit buffer overflow while looking up %s",
8080Sstevel@tonic-gate 					*qname ? qname : ".");
8090Sstevel@tonic-gate 			}
8100Sstevel@tonic-gate 			n = dn_expand(ansbuf, eor, cp, tbuf, sizeof tbuf);
8110Sstevel@tonic-gate 			if (n < 0 || !maybe_ok(pvt->res, tbuf, name_ok)) {
8120Sstevel@tonic-gate 				had_error++;
8130Sstevel@tonic-gate 				continue;
8140Sstevel@tonic-gate 			}
8150Sstevel@tonic-gate 			cp += n;
8160Sstevel@tonic-gate 			/* Store alias. */
8170Sstevel@tonic-gate 			if (ap >= &pvt->host_aliases[MAXALIASES-1])
8180Sstevel@tonic-gate 				continue;
8190Sstevel@tonic-gate 			*ap++ = bp;
8200Sstevel@tonic-gate 			n = strlen(bp) + 1;	/* for the \0 */
8210Sstevel@tonic-gate 			bp += n;
8220Sstevel@tonic-gate 			/* Get canonical name. */
8230Sstevel@tonic-gate 			n = strlen(tbuf) + 1;	/* for the \0 */
8240Sstevel@tonic-gate 			if (n > (ep - bp) || n > MAXHOSTNAMELEN) {
8250Sstevel@tonic-gate 				had_error++;
8260Sstevel@tonic-gate 				continue;
8270Sstevel@tonic-gate 			}
8280Sstevel@tonic-gate #ifdef HAVE_STRLCPY
8290Sstevel@tonic-gate 			strlcpy(bp, tbuf, ep - bp);
8300Sstevel@tonic-gate #else
8310Sstevel@tonic-gate 			strcpy(bp, tbuf);
8320Sstevel@tonic-gate #endif
8330Sstevel@tonic-gate 			pvt->host.h_name = bp;
8340Sstevel@tonic-gate 			hname = bp;
8350Sstevel@tonic-gate 			bp += n;
8360Sstevel@tonic-gate 			continue;
8370Sstevel@tonic-gate 		}
8380Sstevel@tonic-gate 		if (qtype == T_PTR && type == T_CNAME) {
8390Sstevel@tonic-gate 			n = dn_expand(ansbuf, eor, cp, tbuf, sizeof tbuf);
8400Sstevel@tonic-gate 			if (n < 0 || !maybe_dnok(pvt->res, tbuf)) {
8410Sstevel@tonic-gate 				had_error++;
8420Sstevel@tonic-gate 				continue;
8430Sstevel@tonic-gate 			}
8440Sstevel@tonic-gate 			cp += n;
8450Sstevel@tonic-gate #ifdef RES_USE_DNAME
8460Sstevel@tonic-gate 			if ((pvt->res->options & RES_USE_DNAME) != 0)
8470Sstevel@tonic-gate #endif
8480Sstevel@tonic-gate 			{
8490Sstevel@tonic-gate 				/*
8500Sstevel@tonic-gate 				 * We may be able to check this regardless
8510Sstevel@tonic-gate 				 * of the USE_DNAME bit, but we add the check
8520Sstevel@tonic-gate 				 * for now since the DNAME support is
8530Sstevel@tonic-gate 				 * experimental.
8540Sstevel@tonic-gate 				 */
8550Sstevel@tonic-gate 				if (ns_samename(tname, bp) != 1)
8560Sstevel@tonic-gate 					continue;
8570Sstevel@tonic-gate 			}
8580Sstevel@tonic-gate 			/* Get canonical name. */
8590Sstevel@tonic-gate 			n = strlen(tbuf) + 1;	/* for the \0 */
8600Sstevel@tonic-gate 			if (n > (ep - bp)) {
8610Sstevel@tonic-gate 				had_error++;
8620Sstevel@tonic-gate 				continue;
8630Sstevel@tonic-gate 			}
8640Sstevel@tonic-gate #ifdef HAVE_STRLCPY
8650Sstevel@tonic-gate 			strlcpy(bp, tbuf, ep - bp);
8660Sstevel@tonic-gate #else
8670Sstevel@tonic-gate 			strcpy(bp, tbuf);
8680Sstevel@tonic-gate #endif
8690Sstevel@tonic-gate 			tname = bp;
8700Sstevel@tonic-gate 			bp += n;
8710Sstevel@tonic-gate 			continue;
8720Sstevel@tonic-gate 		}
8730Sstevel@tonic-gate 		if (qtype == T_ANY) {
8740Sstevel@tonic-gate 			if (!(type == T_A || type == T_AAAA)) {
8750Sstevel@tonic-gate 				cp += n;
8760Sstevel@tonic-gate 				continue;
8770Sstevel@tonic-gate 			}
8780Sstevel@tonic-gate 		} else if (type != qtype) {
8790Sstevel@tonic-gate 			cp += n;
8800Sstevel@tonic-gate 			continue;
8810Sstevel@tonic-gate 		}
8820Sstevel@tonic-gate 		switch (type) {
8830Sstevel@tonic-gate 		case T_PTR:
8840Sstevel@tonic-gate 			if (ret_aip != NULL) {
8850Sstevel@tonic-gate 				/* addrinfo never needs T_PTR */
8860Sstevel@tonic-gate 				cp += n;
8870Sstevel@tonic-gate 				continue;
8880Sstevel@tonic-gate 			}
8890Sstevel@tonic-gate 			if (ns_samename(tname, bp) != 1) {
8900Sstevel@tonic-gate 				cp += n;
8910Sstevel@tonic-gate 				continue;
8920Sstevel@tonic-gate 			}
8930Sstevel@tonic-gate 			n = dn_expand(ansbuf, eor, cp, bp, ep - bp);
8940Sstevel@tonic-gate 			if (n < 0 || !maybe_hnok(pvt->res, bp) ||
8950Sstevel@tonic-gate 			    n >= MAXHOSTNAMELEN) {
8960Sstevel@tonic-gate 				had_error++;
8970Sstevel@tonic-gate 				break;
8980Sstevel@tonic-gate 			}
8990Sstevel@tonic-gate 			cp += n;
9000Sstevel@tonic-gate 			if (!haveanswer) {
9010Sstevel@tonic-gate 				pvt->host.h_name = bp;
9020Sstevel@tonic-gate 				hname = bp;
9030Sstevel@tonic-gate 			}
9040Sstevel@tonic-gate 			else if (ap < &pvt->host_aliases[MAXALIASES-1])
9050Sstevel@tonic-gate 				*ap++ = bp;
9060Sstevel@tonic-gate 			else
9070Sstevel@tonic-gate 				n = -1;
9080Sstevel@tonic-gate 			if (n != -1) {
9090Sstevel@tonic-gate 				n = strlen(bp) + 1;	/* for the \0 */
9100Sstevel@tonic-gate 				bp += n;
9110Sstevel@tonic-gate 			}
9120Sstevel@tonic-gate 			break;
9130Sstevel@tonic-gate 		case T_A:
9140Sstevel@tonic-gate 		case T_AAAA:
9150Sstevel@tonic-gate 			if (ns_samename(hname, bp) != 1) {
9160Sstevel@tonic-gate 				cp += n;
9170Sstevel@tonic-gate 				continue;
9180Sstevel@tonic-gate 			}
9190Sstevel@tonic-gate 			if (type == T_A && n != INADDRSZ) {
9200Sstevel@tonic-gate 				cp += n;
9210Sstevel@tonic-gate 				continue;
9220Sstevel@tonic-gate 			}
9230Sstevel@tonic-gate 			if (type == T_AAAA && n != IN6ADDRSZ) {
9240Sstevel@tonic-gate 				cp += n;
9250Sstevel@tonic-gate 				continue;
9260Sstevel@tonic-gate 			}
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 			/* make addrinfo. don't overwrite constant PAI */
9290Sstevel@tonic-gate 			ai = *pai;
9300Sstevel@tonic-gate 			ai.ai_family = (type == T_AAAA) ? AF_INET6 : AF_INET;
9310Sstevel@tonic-gate 			cur->ai_next = addr2addrinfo(
9320Sstevel@tonic-gate 					(const struct addrinfo *)&ai,
9330Sstevel@tonic-gate 					(const char *)cp);
9340Sstevel@tonic-gate 			if (cur->ai_next == NULL)
9350Sstevel@tonic-gate 				had_error++;
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 			if (!haveanswer) {
9380Sstevel@tonic-gate 				int nn;
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate 				nn = strlen(bp) + 1;	/* for the \0 */
9410Sstevel@tonic-gate 				if (nn >= MAXHOSTNAMELEN) {
9420Sstevel@tonic-gate 					cp += n;
9430Sstevel@tonic-gate 					had_error++;
9440Sstevel@tonic-gate 					continue;
9450Sstevel@tonic-gate 				}
9460Sstevel@tonic-gate 				pvt->host.h_name = bp;
9470Sstevel@tonic-gate 				hname = bp;
9480Sstevel@tonic-gate 				bp += nn;
9490Sstevel@tonic-gate 			}
9500Sstevel@tonic-gate 			/* Ensure alignment. */
9510Sstevel@tonic-gate 			bp = (char *)(((u_long)bp + (sizeof(align) - 1)) &
9520Sstevel@tonic-gate 				      ~(sizeof(align) - 1));
9530Sstevel@tonic-gate 			/* Avoid overflows. */
9540Sstevel@tonic-gate 			if (bp + n >= &pvt->hostbuf[sizeof pvt->hostbuf]) {
9550Sstevel@tonic-gate 				had_error++;
9560Sstevel@tonic-gate 				continue;
9570Sstevel@tonic-gate 			}
9580Sstevel@tonic-gate 			if (ret_aip) { /* need addrinfo. keep it. */
9590Sstevel@tonic-gate 				while (cur && cur->ai_next)
9600Sstevel@tonic-gate 					cur = cur->ai_next;
9610Sstevel@tonic-gate 			} else if (cur->ai_next) { /* need hostent */
9620Sstevel@tonic-gate 				struct addrinfo *aip = cur->ai_next;
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 				for (aip = cur->ai_next; aip;
9650Sstevel@tonic-gate 				     aip = aip->ai_next) {
9660Sstevel@tonic-gate 					int m;
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 					m = add_hostent(pvt, bp, hap, aip);
9690Sstevel@tonic-gate 					if (m < 0) {
9700Sstevel@tonic-gate 						had_error++;
9710Sstevel@tonic-gate 						break;
9720Sstevel@tonic-gate 					}
9730Sstevel@tonic-gate 					if (m == 0)
9740Sstevel@tonic-gate 						continue;
975*9433SStacey.Marshall@Sun.COM 					if (hap < &pvt->h_addr_ptrs[MAXADDRS])
9760Sstevel@tonic-gate 						hap++;
9770Sstevel@tonic-gate 					*hap = NULL;
9780Sstevel@tonic-gate 					bp += m;
9790Sstevel@tonic-gate 				}
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 				freeaddrinfo(cur->ai_next);
9820Sstevel@tonic-gate 				cur->ai_next = NULL;
9830Sstevel@tonic-gate 			}
9840Sstevel@tonic-gate 			cp += n;
9850Sstevel@tonic-gate 			break;
9860Sstevel@tonic-gate 		default:
9870Sstevel@tonic-gate 			abort();
9880Sstevel@tonic-gate 		}
9890Sstevel@tonic-gate 		if (!had_error)
9900Sstevel@tonic-gate 			haveanswer++;
9910Sstevel@tonic-gate 	}
9920Sstevel@tonic-gate 	if (haveanswer) {
9930Sstevel@tonic-gate 		if (ret_aip == NULL) {
9940Sstevel@tonic-gate 			*ap = NULL;
9950Sstevel@tonic-gate 			*hap = NULL;
9960Sstevel@tonic-gate 
997*9433SStacey.Marshall@Sun.COM 			if (pvt->res->nsort && hap != pvt->h_addr_ptrs &&
998*9433SStacey.Marshall@Sun.COM 			    qtype == T_A)
9990Sstevel@tonic-gate 				addrsort(pvt->res, pvt->h_addr_ptrs,
1000*9433SStacey.Marshall@Sun.COM 					 hap - pvt->h_addr_ptrs);
10010Sstevel@tonic-gate 			if (pvt->host.h_name == NULL) {
10020Sstevel@tonic-gate 				n = strlen(qname) + 1;	/* for the \0 */
10030Sstevel@tonic-gate 				if (n > (ep - bp) || n >= MAXHOSTNAMELEN)
10040Sstevel@tonic-gate 					goto no_recovery;
10050Sstevel@tonic-gate #ifdef HAVE_STRLCPY
10060Sstevel@tonic-gate 				strlcpy(bp, qname, ep - bp);
10070Sstevel@tonic-gate #else
10080Sstevel@tonic-gate 				strcpy(bp, qname);
10090Sstevel@tonic-gate #endif
10100Sstevel@tonic-gate 				pvt->host.h_name = bp;
10110Sstevel@tonic-gate 				bp += n;
10120Sstevel@tonic-gate 			}
10130Sstevel@tonic-gate 			if (pvt->res->options & RES_USE_INET6)
10140Sstevel@tonic-gate 				map_v4v6_hostent(&pvt->host, &bp, ep);
10150Sstevel@tonic-gate 			RES_SET_H_ERRNO(pvt->res, NETDB_SUCCESS);
10160Sstevel@tonic-gate 			return (&pvt->host);
10170Sstevel@tonic-gate 		} else {
10180Sstevel@tonic-gate 			if ((pai->ai_flags & AI_CANONNAME) != 0) {
10190Sstevel@tonic-gate 				if (pvt->host.h_name == NULL) {
10200Sstevel@tonic-gate 					sentinel.ai_next->ai_canonname =
10210Sstevel@tonic-gate 						strdup(qname);
10220Sstevel@tonic-gate 				}
10230Sstevel@tonic-gate 				else {
10240Sstevel@tonic-gate 					sentinel.ai_next->ai_canonname =
10250Sstevel@tonic-gate 						strdup(pvt->host.h_name);
10260Sstevel@tonic-gate 				}
10270Sstevel@tonic-gate 			}
10280Sstevel@tonic-gate 			*ret_aip = sentinel.ai_next;
10290Sstevel@tonic-gate 			return(NULL);
10300Sstevel@tonic-gate 		}
10310Sstevel@tonic-gate 	}
10320Sstevel@tonic-gate  no_recovery:
10330Sstevel@tonic-gate 	if (sentinel.ai_next) {
10340Sstevel@tonic-gate 		/* this should be impossible, but check it for safety */
10350Sstevel@tonic-gate 		freeaddrinfo(sentinel.ai_next);
10360Sstevel@tonic-gate 	}
10370Sstevel@tonic-gate 	if (error == NETDB_SUCCESS)
10380Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
10390Sstevel@tonic-gate 	else
10400Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, error);
10410Sstevel@tonic-gate 	return(NULL);
10420Sstevel@tonic-gate }
10430Sstevel@tonic-gate 
10440Sstevel@tonic-gate static int
10450Sstevel@tonic-gate add_hostent(struct pvt *pvt, char *bp, char **hap, struct addrinfo *ai)
10460Sstevel@tonic-gate {
10470Sstevel@tonic-gate 	int addrlen;
10480Sstevel@tonic-gate 	char *addrp;
10490Sstevel@tonic-gate 	const char **tap;
10500Sstevel@tonic-gate 	char *obp = bp;
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 	switch(ai->ai_addr->sa_family) {
10530Sstevel@tonic-gate 	case AF_INET6:
10540Sstevel@tonic-gate 		addrlen = IN6ADDRSZ;
10550Sstevel@tonic-gate 		addrp = (char *)&((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr;
10560Sstevel@tonic-gate 		break;
10570Sstevel@tonic-gate 	case AF_INET:
10580Sstevel@tonic-gate 		addrlen = INADDRSZ;
10590Sstevel@tonic-gate 		addrp = (char *)&((struct sockaddr_in *)ai->ai_addr)->sin_addr;
10600Sstevel@tonic-gate 		break;
10610Sstevel@tonic-gate 	default:
10620Sstevel@tonic-gate 		return(-1);	/* abort? */
10630Sstevel@tonic-gate 	}
10640Sstevel@tonic-gate 
10650Sstevel@tonic-gate 	/* Ensure alignment. */
10660Sstevel@tonic-gate 	bp = (char *)(((u_long)bp + (sizeof(align) - 1)) &
10670Sstevel@tonic-gate 		      ~(sizeof(align) - 1));
10680Sstevel@tonic-gate 	/* Avoid overflows. */
10690Sstevel@tonic-gate 	if (bp + addrlen >= &pvt->hostbuf[sizeof pvt->hostbuf])
10700Sstevel@tonic-gate 		return(-1);
1071*9433SStacey.Marshall@Sun.COM 	if (hap >= &pvt->h_addr_ptrs[MAXADDRS])
10720Sstevel@tonic-gate 		return(0); /* fail, but not treat it as an error. */
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 	/* Suppress duplicates. */
10750Sstevel@tonic-gate 	for (tap = (const char **)pvt->h_addr_ptrs;
10760Sstevel@tonic-gate 	     *tap != NULL;
10770Sstevel@tonic-gate 	     tap++)
10780Sstevel@tonic-gate 		if (memcmp(*tap, addrp, addrlen) == 0)
10790Sstevel@tonic-gate 			break;
10800Sstevel@tonic-gate 	if (*tap != NULL)
10810Sstevel@tonic-gate 		return (0);
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate 	memcpy(*hap = bp, addrp, addrlen);
10840Sstevel@tonic-gate 	return((bp + addrlen) - obp);
10850Sstevel@tonic-gate }
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate static void
10880Sstevel@tonic-gate map_v4v6_hostent(struct hostent *hp, char **bpp, char *ep) {
10890Sstevel@tonic-gate 	char **ap;
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 	if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
10920Sstevel@tonic-gate 		return;
10930Sstevel@tonic-gate 	hp->h_addrtype = AF_INET6;
10940Sstevel@tonic-gate 	hp->h_length = IN6ADDRSZ;
10950Sstevel@tonic-gate 	for (ap = hp->h_addr_list; *ap; ap++) {
10960Sstevel@tonic-gate 		int i = (u_long)*bpp % sizeof(align);
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 		if (i != 0)
10990Sstevel@tonic-gate 			i = sizeof(align) - i;
11000Sstevel@tonic-gate 
11010Sstevel@tonic-gate 		if ((ep - *bpp) < (i + IN6ADDRSZ)) {
11020Sstevel@tonic-gate 			/* Out of memory.  Truncate address list here. */
11030Sstevel@tonic-gate 			*ap = NULL;
11040Sstevel@tonic-gate 			return;
11050Sstevel@tonic-gate 		}
11060Sstevel@tonic-gate 		*bpp += i;
11070Sstevel@tonic-gate 		map_v4v6_address(*ap, *bpp);
11080Sstevel@tonic-gate 		*ap = *bpp;
11090Sstevel@tonic-gate 		*bpp += IN6ADDRSZ;
11100Sstevel@tonic-gate 	}
11110Sstevel@tonic-gate }
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate static void
11140Sstevel@tonic-gate addrsort(res_state statp, char **ap, int num) {
11150Sstevel@tonic-gate 	int i, j, needsort = 0, aval[MAXADDRS];
11160Sstevel@tonic-gate 	char **p;
11170Sstevel@tonic-gate 
11180Sstevel@tonic-gate 	p = ap;
11190Sstevel@tonic-gate 	for (i = 0; i < num; i++, p++) {
11200Sstevel@tonic-gate 		for (j = 0 ; (unsigned)j < statp->nsort; j++)
11210Sstevel@tonic-gate 			if (statp->sort_list[j].addr.s_addr ==
11220Sstevel@tonic-gate 			    (((struct in_addr *)(*p))->s_addr &
11230Sstevel@tonic-gate 			     statp->sort_list[j].mask))
11240Sstevel@tonic-gate 				break;
11250Sstevel@tonic-gate 		aval[i] = j;
11260Sstevel@tonic-gate 		if (needsort == 0 && i > 0 && j < aval[i-1])
11270Sstevel@tonic-gate 			needsort = i;
11280Sstevel@tonic-gate 	}
11290Sstevel@tonic-gate 	if (!needsort)
11300Sstevel@tonic-gate 		return;
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate 	while (needsort < num) {
11330Sstevel@tonic-gate 		for (j = needsort - 1; j >= 0; j--) {
11340Sstevel@tonic-gate 			if (aval[j] > aval[j+1]) {
11350Sstevel@tonic-gate 				char *hp;
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate 				i = aval[j];
11380Sstevel@tonic-gate 				aval[j] = aval[j+1];
11390Sstevel@tonic-gate 				aval[j+1] = i;
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate 				hp = ap[j];
11420Sstevel@tonic-gate 				ap[j] = ap[j+1];
11430Sstevel@tonic-gate 				ap[j+1] = hp;
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 			} else
11460Sstevel@tonic-gate 				break;
11470Sstevel@tonic-gate 		}
11480Sstevel@tonic-gate 		needsort++;
11490Sstevel@tonic-gate 	}
11500Sstevel@tonic-gate }
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate static int
11530Sstevel@tonic-gate init(struct irs_ho *this) {
11540Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
11550Sstevel@tonic-gate 
11560Sstevel@tonic-gate 	if (!pvt->res && !ho_res_get(this))
11570Sstevel@tonic-gate 		return (-1);
11580Sstevel@tonic-gate 	if (((pvt->res->options & RES_INIT) == 0) &&
11590Sstevel@tonic-gate 	    res_ninit(pvt->res) == -1)
11600Sstevel@tonic-gate 		return (-1);
11610Sstevel@tonic-gate 	return (0);
11620Sstevel@tonic-gate }
1163