17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib  * Portions Copyright (C) 2004-2006, 2008  Internet Systems Consortium, Inc. ("ISC")
3*9525b14bSRao Shoaib  * Portions Copyright (C) 1996-2003  Internet Software Consortium.
4*9525b14bSRao Shoaib  *
5*9525b14bSRao Shoaib  * Permission to use, copy, modify, and/or distribute this software for any
6*9525b14bSRao Shoaib  * purpose with or without fee is hereby granted, provided that the above
7*9525b14bSRao Shoaib  * copyright notice and this permission notice appear in all copies.
8*9525b14bSRao Shoaib  *
9*9525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10*9525b14bSRao Shoaib  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11*9525b14bSRao Shoaib  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12*9525b14bSRao Shoaib  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13*9525b14bSRao Shoaib  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14*9525b14bSRao Shoaib  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15*9525b14bSRao Shoaib  * PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate  */
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate /*
197c478bd9Sstevel@tonic-gate  * Copyright (c) 1985, 1988, 1993
207c478bd9Sstevel@tonic-gate  *    The Regents of the University of California.  All rights reserved.
21*9525b14bSRao Shoaib  *
227c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
237c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
247c478bd9Sstevel@tonic-gate  * are met:
257c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
267c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
277c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
287c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
297c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
307c478bd9Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
317c478bd9Sstevel@tonic-gate  *    must display the following acknowledgement:
327c478bd9Sstevel@tonic-gate  * 	This product includes software developed by the University of
337c478bd9Sstevel@tonic-gate  * 	California, Berkeley and its contributors.
347c478bd9Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
357c478bd9Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
367c478bd9Sstevel@tonic-gate  *    without specific prior written permission.
37*9525b14bSRao Shoaib  *
387c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
397c478bd9Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
407c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
417c478bd9Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
427c478bd9Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
437c478bd9Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
447c478bd9Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
457c478bd9Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
467c478bd9Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
477c478bd9Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
487c478bd9Sstevel@tonic-gate  * SUCH DAMAGE.
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /* from gethostnamadr.c	8.1 (Berkeley) 6/4/93 */
527c478bd9Sstevel@tonic-gate /* BIND Id: gethnamaddr.c,v 8.15 1996/05/22 04:56:30 vixie Exp $ */
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /* Imports. */
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #include "port_before.h"
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #include <sys/types.h>
597c478bd9Sstevel@tonic-gate #include <sys/param.h>
607c478bd9Sstevel@tonic-gate #include <sys/socket.h>
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #include <netinet/in.h>
637c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
647c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #include <ctype.h>
677c478bd9Sstevel@tonic-gate #include <errno.h>
687c478bd9Sstevel@tonic-gate #include <stdlib.h>
697c478bd9Sstevel@tonic-gate #include <netdb.h>
707c478bd9Sstevel@tonic-gate #include <resolv.h>
717c478bd9Sstevel@tonic-gate #include <stdio.h>
727c478bd9Sstevel@tonic-gate #include <string.h>
737c478bd9Sstevel@tonic-gate #include <syslog.h>
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate #include <isc/memcluster.h>
767c478bd9Sstevel@tonic-gate #include <irs.h>
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #include "port_after.h"
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate #include "irs_p.h"
817c478bd9Sstevel@tonic-gate #include "dns_p.h"
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate #ifdef SPRINTF_CHAR
847c478bd9Sstevel@tonic-gate # define SPRINTF(x) strlen(sprintf/**/x)
857c478bd9Sstevel@tonic-gate #else
867c478bd9Sstevel@tonic-gate # define SPRINTF(x) sprintf x
877c478bd9Sstevel@tonic-gate #endif
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate /* Definitions. */
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate #define	MAXALIASES	35
927c478bd9Sstevel@tonic-gate #define	MAXADDRS	35
937c478bd9Sstevel@tonic-gate 
94*9525b14bSRao Shoaib #define MAXPACKET (65535)	/*%< Maximum TCP message size */
957c478bd9Sstevel@tonic-gate #define BOUNDS_CHECK(ptr, count) \
967c478bd9Sstevel@tonic-gate 	if ((ptr) + (count) > eom) { \
977c478bd9Sstevel@tonic-gate 		had_error++; \
987c478bd9Sstevel@tonic-gate 		continue; \
997c478bd9Sstevel@tonic-gate 	} else (void)0
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate typedef union {
1027c478bd9Sstevel@tonic-gate 	HEADER hdr;
1037c478bd9Sstevel@tonic-gate 	u_char buf[MAXPACKET];
1047c478bd9Sstevel@tonic-gate } querybuf;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate struct dns_res_target {
1077c478bd9Sstevel@tonic-gate 	struct dns_res_target *next;
108*9525b14bSRao Shoaib 	querybuf qbuf;		/*%< query buffer */
109*9525b14bSRao Shoaib 	u_char *answer;		/*%< buffer to put answer */
110*9525b14bSRao Shoaib 	int anslen;		/*%< size of answer buffer */
111*9525b14bSRao Shoaib 	int qclass, qtype;	/*%< class and type of query */
112*9525b14bSRao Shoaib 	int action;		/*%< condition whether query is really issued */
113*9525b14bSRao Shoaib 	char qname[MAXDNAME +1]; /*%< domain name */
1147c478bd9Sstevel@tonic-gate #if 0
115*9525b14bSRao Shoaib 	int n;			/*%< result length */
1167c478bd9Sstevel@tonic-gate #endif
1177c478bd9Sstevel@tonic-gate };
1187c478bd9Sstevel@tonic-gate enum {RESTGT_DOALWAYS, RESTGT_AFTERFAILURE, RESTGT_IGNORE};
1197c478bd9Sstevel@tonic-gate enum {RESQRY_SUCCESS, RESQRY_FAIL};
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate struct pvt {
1227c478bd9Sstevel@tonic-gate 	struct hostent	host;
1237c478bd9Sstevel@tonic-gate 	char *		h_addr_ptrs[MAXADDRS + 1];
1247c478bd9Sstevel@tonic-gate 	char *		host_aliases[MAXALIASES];
1257c478bd9Sstevel@tonic-gate 	char		hostbuf[8*1024];
126*9525b14bSRao Shoaib 	u_char		host_addr[16];	/*%< IPv4 or IPv6 */
1277c478bd9Sstevel@tonic-gate 	struct __res_state  *res;
1287c478bd9Sstevel@tonic-gate 	void		(*free_res)(void *);
1297c478bd9Sstevel@tonic-gate };
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate typedef union {
1327c478bd9Sstevel@tonic-gate 	int32_t al;
1337c478bd9Sstevel@tonic-gate 	char ac;
1347c478bd9Sstevel@tonic-gate } align;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
1377c478bd9Sstevel@tonic-gate static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
1387c478bd9Sstevel@tonic-gate /* Note: the IPv6 loopback address is in the "tunnel" space */
139*9525b14bSRao Shoaib static const u_char v6local[] = { 0,0, 0,1 }; /*%< last 4 bytes of IPv6 addr */
1407c478bd9Sstevel@tonic-gate /* Forwards. */
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate static void		ho_close(struct irs_ho *this);
1437c478bd9Sstevel@tonic-gate static struct hostent *	ho_byname(struct irs_ho *this, const char *name);
1447c478bd9Sstevel@tonic-gate static struct hostent *	ho_byname2(struct irs_ho *this, const char *name,
1457c478bd9Sstevel@tonic-gate 				   int af);
1467c478bd9Sstevel@tonic-gate static struct hostent *	ho_byaddr(struct irs_ho *this, const void *addr,
1477c478bd9Sstevel@tonic-gate 				  int len, int af);
1487c478bd9Sstevel@tonic-gate static struct hostent *	ho_next(struct irs_ho *this);
1497c478bd9Sstevel@tonic-gate static void		ho_rewind(struct irs_ho *this);
1507c478bd9Sstevel@tonic-gate static void		ho_minimize(struct irs_ho *this);
1517c478bd9Sstevel@tonic-gate static struct __res_state * ho_res_get(struct irs_ho *this);
1527c478bd9Sstevel@tonic-gate static void		ho_res_set(struct irs_ho *this,
1537c478bd9Sstevel@tonic-gate 				   struct __res_state *res,
1547c478bd9Sstevel@tonic-gate 				   void (*free_res)(void *));
1557c478bd9Sstevel@tonic-gate static struct addrinfo * ho_addrinfo(struct irs_ho *this, const char *name,
1567c478bd9Sstevel@tonic-gate 				     const struct addrinfo *pai);
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate static void		map_v4v6_hostent(struct hostent *hp, char **bp,
1597c478bd9Sstevel@tonic-gate 					 char *ep);
1607c478bd9Sstevel@tonic-gate static void		addrsort(res_state, char **, int);
1617c478bd9Sstevel@tonic-gate static struct hostent *	gethostans(struct irs_ho *this,
1627c478bd9Sstevel@tonic-gate 				   const u_char *ansbuf, int anslen,
1637c478bd9Sstevel@tonic-gate 				   const char *qname, int qtype,
1647c478bd9Sstevel@tonic-gate 				   int af, int size,
1657c478bd9Sstevel@tonic-gate 				   struct addrinfo **ret_aip,
1667c478bd9Sstevel@tonic-gate 				   const struct addrinfo *pai);
1677c478bd9Sstevel@tonic-gate static int add_hostent(struct pvt *pvt, char *bp, char **hap,
1687c478bd9Sstevel@tonic-gate 		       struct addrinfo *ai);
1697c478bd9Sstevel@tonic-gate static int		init(struct irs_ho *this);
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate /* Exports. */
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate struct irs_ho *
irs_dns_ho(struct irs_acc * this)1747c478bd9Sstevel@tonic-gate irs_dns_ho(struct irs_acc *this) {
1757c478bd9Sstevel@tonic-gate 	struct irs_ho *ho;
1767c478bd9Sstevel@tonic-gate 	struct pvt *pvt;
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	UNUSED(this);
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	if (!(pvt = memget(sizeof *pvt))) {
1817c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
1827c478bd9Sstevel@tonic-gate 		return (NULL);
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate 	memset(pvt, 0, sizeof *pvt);
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	if (!(ho = memget(sizeof *ho))) {
1877c478bd9Sstevel@tonic-gate 		memput(pvt, sizeof *pvt);
1887c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
1897c478bd9Sstevel@tonic-gate 		return (NULL);
1907c478bd9Sstevel@tonic-gate 	}
1917c478bd9Sstevel@tonic-gate 	memset(ho, 0x5e, sizeof *ho);
1927c478bd9Sstevel@tonic-gate 	ho->private = pvt;
1937c478bd9Sstevel@tonic-gate 	ho->close = ho_close;
1947c478bd9Sstevel@tonic-gate 	ho->byname = ho_byname;
1957c478bd9Sstevel@tonic-gate 	ho->byname2 = ho_byname2;
1967c478bd9Sstevel@tonic-gate 	ho->byaddr = ho_byaddr;
1977c478bd9Sstevel@tonic-gate 	ho->next = ho_next;
1987c478bd9Sstevel@tonic-gate 	ho->rewind = ho_rewind;
1997c478bd9Sstevel@tonic-gate 	ho->minimize = ho_minimize;
2007c478bd9Sstevel@tonic-gate 	ho->res_get = ho_res_get;
2017c478bd9Sstevel@tonic-gate 	ho->res_set = ho_res_set;
2027c478bd9Sstevel@tonic-gate 	ho->addrinfo = ho_addrinfo;
2037c478bd9Sstevel@tonic-gate 	return (ho);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate /* Methods. */
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate static void
ho_close(struct irs_ho * this)2097c478bd9Sstevel@tonic-gate ho_close(struct irs_ho *this) {
2107c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	ho_minimize(this);
2137c478bd9Sstevel@tonic-gate 	if (pvt->res && pvt->free_res)
2147c478bd9Sstevel@tonic-gate 		(*pvt->free_res)(pvt->res);
215*9525b14bSRao Shoaib 	memput(pvt, sizeof *pvt);
2167c478bd9Sstevel@tonic-gate 	memput(this, sizeof *this);
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate static struct hostent *
ho_byname(struct irs_ho * this,const char * name)2207c478bd9Sstevel@tonic-gate ho_byname(struct irs_ho *this, const char *name) {
2217c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2227c478bd9Sstevel@tonic-gate 	struct hostent *hp;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	if (init(this) == -1)
2257c478bd9Sstevel@tonic-gate 		return (NULL);
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	if (pvt->res->options & RES_USE_INET6) {
2287c478bd9Sstevel@tonic-gate 		hp = ho_byname2(this, name, AF_INET6);
2297c478bd9Sstevel@tonic-gate 		if (hp)
2307c478bd9Sstevel@tonic-gate 			return (hp);
2317c478bd9Sstevel@tonic-gate 	}
2327c478bd9Sstevel@tonic-gate 	return (ho_byname2(this, name, AF_INET));
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate static struct hostent *
ho_byname2(struct irs_ho * this,const char * name,int af)2367c478bd9Sstevel@tonic-gate ho_byname2(struct irs_ho *this, const char *name, int af)
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2397c478bd9Sstevel@tonic-gate 	struct hostent *hp = NULL;
2407c478bd9Sstevel@tonic-gate 	int n, size;
2417c478bd9Sstevel@tonic-gate 	char tmp[NS_MAXDNAME];
2427c478bd9Sstevel@tonic-gate 	const char *cp;
2437c478bd9Sstevel@tonic-gate 	struct addrinfo ai;
2447c478bd9Sstevel@tonic-gate 	struct dns_res_target *q, *p;
2457c478bd9Sstevel@tonic-gate 	int querystate = RESQRY_FAIL;
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	if (init(this) == -1)
2487c478bd9Sstevel@tonic-gate 		return (NULL);
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	q = memget(sizeof(*q));
2517c478bd9Sstevel@tonic-gate 	if (q == NULL) {
2527c478bd9Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
2537c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
2547c478bd9Sstevel@tonic-gate 		goto cleanup;
2557c478bd9Sstevel@tonic-gate 	}
256*9525b14bSRao Shoaib 	memset(q, 0, sizeof(*q));
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	switch (af) {
2597c478bd9Sstevel@tonic-gate 	case AF_INET:
2607c478bd9Sstevel@tonic-gate 		size = INADDRSZ;
2617c478bd9Sstevel@tonic-gate 		q->qclass = C_IN;
2627c478bd9Sstevel@tonic-gate 		q->qtype = T_A;
2637c478bd9Sstevel@tonic-gate 		q->answer = q->qbuf.buf;
2647c478bd9Sstevel@tonic-gate 		q->anslen = sizeof(q->qbuf);
2657c478bd9Sstevel@tonic-gate 		q->action = RESTGT_DOALWAYS;
2667c478bd9Sstevel@tonic-gate 		break;
2677c478bd9Sstevel@tonic-gate 	case AF_INET6:
2687c478bd9Sstevel@tonic-gate 		size = IN6ADDRSZ;
2697c478bd9Sstevel@tonic-gate 		q->qclass = C_IN;
2707c478bd9Sstevel@tonic-gate 		q->qtype = T_AAAA;
2717c478bd9Sstevel@tonic-gate 		q->answer = q->qbuf.buf;
2727c478bd9Sstevel@tonic-gate 		q->anslen = sizeof(q->qbuf);
2737c478bd9Sstevel@tonic-gate 		q->action = RESTGT_DOALWAYS;
2747c478bd9Sstevel@tonic-gate 		break;
2757c478bd9Sstevel@tonic-gate 	default:
2767c478bd9Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
2777c478bd9Sstevel@tonic-gate 		errno = EAFNOSUPPORT;
2787c478bd9Sstevel@tonic-gate 		hp = NULL;
2797c478bd9Sstevel@tonic-gate 		goto cleanup;
2807c478bd9Sstevel@tonic-gate 	}
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	/*
2837c478bd9Sstevel@tonic-gate 	 * if there aren't any dots, it could be a user-level alias.
2847c478bd9Sstevel@tonic-gate 	 * this is also done in res_nquery() since we are not the only
2857c478bd9Sstevel@tonic-gate 	 * function that looks up host names.
2867c478bd9Sstevel@tonic-gate 	 */
2877c478bd9Sstevel@tonic-gate 	if (!strchr(name, '.') && (cp = res_hostalias(pvt->res, name,
2887c478bd9Sstevel@tonic-gate 						      tmp, sizeof tmp)))
2897c478bd9Sstevel@tonic-gate 		name = cp;
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	for (p = q; p; p = p->next) {
2927c478bd9Sstevel@tonic-gate 		switch(p->action) {
2937c478bd9Sstevel@tonic-gate 		case RESTGT_DOALWAYS:
2947c478bd9Sstevel@tonic-gate 			break;
2957c478bd9Sstevel@tonic-gate 		case RESTGT_AFTERFAILURE:
2967c478bd9Sstevel@tonic-gate 			if (querystate == RESQRY_SUCCESS)
2977c478bd9Sstevel@tonic-gate 				continue;
2987c478bd9Sstevel@tonic-gate 			break;
2997c478bd9Sstevel@tonic-gate 		case RESTGT_IGNORE:
3007c478bd9Sstevel@tonic-gate 			continue;
3017c478bd9Sstevel@tonic-gate 		}
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 		if ((n = res_nsearch(pvt->res, name, p->qclass, p->qtype,
3047c478bd9Sstevel@tonic-gate 				     p->answer, p->anslen)) < 0) {
3057c478bd9Sstevel@tonic-gate 			querystate = RESQRY_FAIL;
3067c478bd9Sstevel@tonic-gate 			continue;
3077c478bd9Sstevel@tonic-gate 		}
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 		memset(&ai, 0, sizeof(ai));
3107c478bd9Sstevel@tonic-gate 		ai.ai_family = af;
3117c478bd9Sstevel@tonic-gate 		if ((hp = gethostans(this, p->answer, n, name, p->qtype,
3127c478bd9Sstevel@tonic-gate 				     af, size, NULL,
3137c478bd9Sstevel@tonic-gate 				     (const struct addrinfo *)&ai)) != NULL)
314*9525b14bSRao Shoaib 			goto cleanup;	/*%< no more loop is necessary */
3157c478bd9Sstevel@tonic-gate 		querystate = RESQRY_FAIL;
3167c478bd9Sstevel@tonic-gate 		continue;
3177c478bd9Sstevel@tonic-gate 	}
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate  cleanup:
3207c478bd9Sstevel@tonic-gate 	if (q != NULL)
3217c478bd9Sstevel@tonic-gate 		memput(q, sizeof(*q));
3227c478bd9Sstevel@tonic-gate 	return(hp);
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate static struct hostent *
ho_byaddr(struct irs_ho * this,const void * addr,int len,int af)3267c478bd9Sstevel@tonic-gate ho_byaddr(struct irs_ho *this, const void *addr, int len, int af)
3277c478bd9Sstevel@tonic-gate {
3287c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
3297c478bd9Sstevel@tonic-gate 	const u_char *uaddr = addr;
3307c478bd9Sstevel@tonic-gate 	char *qp;
3317c478bd9Sstevel@tonic-gate 	struct hostent *hp = NULL;
3327c478bd9Sstevel@tonic-gate 	struct addrinfo ai;
3337c478bd9Sstevel@tonic-gate 	struct dns_res_target *q, *q2, *p;
3347c478bd9Sstevel@tonic-gate 	int n, size, i;
3357c478bd9Sstevel@tonic-gate 	int querystate = RESQRY_FAIL;
336*9525b14bSRao Shoaib 
3377c478bd9Sstevel@tonic-gate 	if (init(this) == -1)
3387c478bd9Sstevel@tonic-gate 		return (NULL);
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	q = memget(sizeof(*q));
3417c478bd9Sstevel@tonic-gate 	q2 = memget(sizeof(*q2));
3427c478bd9Sstevel@tonic-gate 	if (q == NULL || q2 == NULL) {
3437c478bd9Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
3447c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
3457c478bd9Sstevel@tonic-gate 		goto cleanup;
3467c478bd9Sstevel@tonic-gate 	}
347*9525b14bSRao Shoaib 	memset(q, 0, sizeof(*q));
348*9525b14bSRao Shoaib 	memset(q2, 0, sizeof(*q2));
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	if (af == AF_INET6 && len == IN6ADDRSZ &&
3517c478bd9Sstevel@tonic-gate 	    (!memcmp(uaddr, mapped, sizeof mapped) ||
352*9525b14bSRao Shoaib 	   (!memcmp(uaddr, tunnelled, sizeof tunnelled) &&
353*9525b14bSRao Shoaib 	    memcmp(&uaddr[sizeof tunnelled], v6local, sizeof(v6local))))) {
3547c478bd9Sstevel@tonic-gate 		/* Unmap. */
3557c478bd9Sstevel@tonic-gate 		addr = (const char *)addr + sizeof mapped;
3567c478bd9Sstevel@tonic-gate 		uaddr += sizeof mapped;
3577c478bd9Sstevel@tonic-gate 		af = AF_INET;
3587c478bd9Sstevel@tonic-gate 		len = INADDRSZ;
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 	switch (af) {
3617c478bd9Sstevel@tonic-gate 	case AF_INET:
3627c478bd9Sstevel@tonic-gate 		size = INADDRSZ;
3637c478bd9Sstevel@tonic-gate 		q->qclass = C_IN;
3647c478bd9Sstevel@tonic-gate 		q->qtype = T_PTR;
3657c478bd9Sstevel@tonic-gate 		q->answer = q->qbuf.buf;
3667c478bd9Sstevel@tonic-gate 		q->anslen = sizeof(q->qbuf);
3677c478bd9Sstevel@tonic-gate 		q->action = RESTGT_DOALWAYS;
3687c478bd9Sstevel@tonic-gate 		break;
3697c478bd9Sstevel@tonic-gate 	case AF_INET6:
3707c478bd9Sstevel@tonic-gate 		size = IN6ADDRSZ;
3717c478bd9Sstevel@tonic-gate 		q->qclass = C_IN;
3727c478bd9Sstevel@tonic-gate 		q->qtype = T_PTR;
3737c478bd9Sstevel@tonic-gate 		q->answer = q->qbuf.buf;
3747c478bd9Sstevel@tonic-gate 		q->anslen = sizeof(q->qbuf);
3757c478bd9Sstevel@tonic-gate 		q->next = q2;
3767c478bd9Sstevel@tonic-gate 		q->action = RESTGT_DOALWAYS;
3777c478bd9Sstevel@tonic-gate 		q2->qclass = C_IN;
3787c478bd9Sstevel@tonic-gate 		q2->qtype = T_PTR;
3797c478bd9Sstevel@tonic-gate 		q2->answer = q2->qbuf.buf;
3807c478bd9Sstevel@tonic-gate 		q2->anslen = sizeof(q2->qbuf);
381*9525b14bSRao Shoaib 		if ((pvt->res->options & RES_NO_NIBBLE2) != 0U)
3827c478bd9Sstevel@tonic-gate 			q2->action = RESTGT_IGNORE;
3837c478bd9Sstevel@tonic-gate 		else
3847c478bd9Sstevel@tonic-gate 			q2->action = RESTGT_AFTERFAILURE;
3857c478bd9Sstevel@tonic-gate 		break;
3867c478bd9Sstevel@tonic-gate 	default:
3877c478bd9Sstevel@tonic-gate 		errno = EAFNOSUPPORT;
3887c478bd9Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
3897c478bd9Sstevel@tonic-gate 		hp = NULL;
3907c478bd9Sstevel@tonic-gate 		goto cleanup;
3917c478bd9Sstevel@tonic-gate 	}
3927c478bd9Sstevel@tonic-gate 	if (size > len) {
3937c478bd9Sstevel@tonic-gate 		errno = EINVAL;
3947c478bd9Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
3957c478bd9Sstevel@tonic-gate 		hp = NULL;
3967c478bd9Sstevel@tonic-gate 		goto cleanup;
3977c478bd9Sstevel@tonic-gate 	}
3987c478bd9Sstevel@tonic-gate 	switch (af) {
3997c478bd9Sstevel@tonic-gate 	case AF_INET:
4007c478bd9Sstevel@tonic-gate 		qp = q->qname;
4017c478bd9Sstevel@tonic-gate 		(void) sprintf(qp, "%u.%u.%u.%u.in-addr.arpa",
4027c478bd9Sstevel@tonic-gate 			       (uaddr[3] & 0xff),
4037c478bd9Sstevel@tonic-gate 			       (uaddr[2] & 0xff),
4047c478bd9Sstevel@tonic-gate 			       (uaddr[1] & 0xff),
4057c478bd9Sstevel@tonic-gate 			       (uaddr[0] & 0xff));
4067c478bd9Sstevel@tonic-gate 		break;
4077c478bd9Sstevel@tonic-gate 	case AF_INET6:
4087c478bd9Sstevel@tonic-gate 		if (q->action != RESTGT_IGNORE) {
409*9525b14bSRao Shoaib 			const char *nibsuff = res_get_nibblesuffix(pvt->res);
4107c478bd9Sstevel@tonic-gate 			qp = q->qname;
4117c478bd9Sstevel@tonic-gate 			for (n = IN6ADDRSZ - 1; n >= 0; n--) {
4127c478bd9Sstevel@tonic-gate 				i = SPRINTF((qp, "%x.%x.",
4137c478bd9Sstevel@tonic-gate 					       uaddr[n] & 0xf,
4147c478bd9Sstevel@tonic-gate 					       (uaddr[n] >> 4) & 0xf));
415*9525b14bSRao Shoaib 				if (i != 4)
4167c478bd9Sstevel@tonic-gate 					abort();
4177c478bd9Sstevel@tonic-gate 				qp += i;
4187c478bd9Sstevel@tonic-gate 			}
419*9525b14bSRao Shoaib 			if (strlen(q->qname) + strlen(nibsuff) + 1 >
420*9525b14bSRao Shoaib 			    sizeof q->qname) {
421*9525b14bSRao Shoaib 				errno = ENAMETOOLONG;
422*9525b14bSRao Shoaib 				RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
423*9525b14bSRao Shoaib 				hp = NULL;
424*9525b14bSRao Shoaib 				goto cleanup;
425*9525b14bSRao Shoaib 			}
426*9525b14bSRao Shoaib 			strcpy(qp, nibsuff);	/* (checked) */
4277c478bd9Sstevel@tonic-gate 		}
4287c478bd9Sstevel@tonic-gate 		if (q2->action != RESTGT_IGNORE) {
429*9525b14bSRao Shoaib 			const char *nibsuff2 = res_get_nibblesuffix2(pvt->res);
4307c478bd9Sstevel@tonic-gate 			qp = q2->qname;
4317c478bd9Sstevel@tonic-gate 			for (n = IN6ADDRSZ - 1; n >= 0; n--) {
4327c478bd9Sstevel@tonic-gate 				i = SPRINTF((qp, "%x.%x.",
4337c478bd9Sstevel@tonic-gate 					       uaddr[n] & 0xf,
4347c478bd9Sstevel@tonic-gate 					       (uaddr[n] >> 4) & 0xf));
435*9525b14bSRao Shoaib 				if (i != 4)
4367c478bd9Sstevel@tonic-gate 					abort();
4377c478bd9Sstevel@tonic-gate 				qp += i;
4387c478bd9Sstevel@tonic-gate 			}
439*9525b14bSRao Shoaib 			if (strlen(q2->qname) + strlen(nibsuff2) + 1 >
440*9525b14bSRao Shoaib 			    sizeof q2->qname) {
441*9525b14bSRao Shoaib 				errno = ENAMETOOLONG;
442*9525b14bSRao Shoaib 				RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
443*9525b14bSRao Shoaib 				hp = NULL;
444*9525b14bSRao Shoaib 				goto cleanup;
445*9525b14bSRao Shoaib 			}
446*9525b14bSRao Shoaib 			strcpy(qp, nibsuff2);	/* (checked) */
4477c478bd9Sstevel@tonic-gate 		}
4487c478bd9Sstevel@tonic-gate 		break;
4497c478bd9Sstevel@tonic-gate 	default:
4507c478bd9Sstevel@tonic-gate 		abort();
4517c478bd9Sstevel@tonic-gate 	}
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	for (p = q; p; p = p->next) {
4547c478bd9Sstevel@tonic-gate 		switch(p->action) {
4557c478bd9Sstevel@tonic-gate 		case RESTGT_DOALWAYS:
4567c478bd9Sstevel@tonic-gate 			break;
4577c478bd9Sstevel@tonic-gate 		case RESTGT_AFTERFAILURE:
4587c478bd9Sstevel@tonic-gate 			if (querystate == RESQRY_SUCCESS)
4597c478bd9Sstevel@tonic-gate 				continue;
4607c478bd9Sstevel@tonic-gate 			break;
4617c478bd9Sstevel@tonic-gate 		case RESTGT_IGNORE:
4627c478bd9Sstevel@tonic-gate 			continue;
4637c478bd9Sstevel@tonic-gate 		}
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 		if ((n = res_nquery(pvt->res, p->qname, p->qclass, p->qtype,
4667c478bd9Sstevel@tonic-gate 				    p->answer, p->anslen)) < 0) {
4677c478bd9Sstevel@tonic-gate 			querystate = RESQRY_FAIL;
4687c478bd9Sstevel@tonic-gate 			continue;
4697c478bd9Sstevel@tonic-gate 		}
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 		memset(&ai, 0, sizeof(ai));
4727c478bd9Sstevel@tonic-gate 		ai.ai_family = af;
4737c478bd9Sstevel@tonic-gate 		hp = gethostans(this, p->answer, n, p->qname, T_PTR, af, size,
4747c478bd9Sstevel@tonic-gate 				NULL, (const struct addrinfo *)&ai);
4757c478bd9Sstevel@tonic-gate 		if (!hp) {
4767c478bd9Sstevel@tonic-gate 			querystate = RESQRY_FAIL;
4777c478bd9Sstevel@tonic-gate 			continue;
4787c478bd9Sstevel@tonic-gate 		}
479*9525b14bSRao Shoaib 
4807c478bd9Sstevel@tonic-gate 		memcpy(pvt->host_addr, addr, len);
4817c478bd9Sstevel@tonic-gate 		pvt->h_addr_ptrs[0] = (char *)pvt->host_addr;
4827c478bd9Sstevel@tonic-gate 		pvt->h_addr_ptrs[1] = NULL;
4837c478bd9Sstevel@tonic-gate 		if (af == AF_INET && (pvt->res->options & RES_USE_INET6)) {
4847c478bd9Sstevel@tonic-gate 			map_v4v6_address((char*)pvt->host_addr,
4857c478bd9Sstevel@tonic-gate 					 (char*)pvt->host_addr);
4867c478bd9Sstevel@tonic-gate 			pvt->host.h_addrtype = AF_INET6;
4877c478bd9Sstevel@tonic-gate 			pvt->host.h_length = IN6ADDRSZ;
4887c478bd9Sstevel@tonic-gate 		}
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, NETDB_SUCCESS);
491*9525b14bSRao Shoaib 		goto cleanup;	/*%< no more loop is necessary. */
4927c478bd9Sstevel@tonic-gate 	}
493*9525b14bSRao Shoaib 	hp = NULL; /*%< H_ERRNO was set by subroutines */
4947c478bd9Sstevel@tonic-gate  cleanup:
4957c478bd9Sstevel@tonic-gate 	if (q != NULL)
4967c478bd9Sstevel@tonic-gate 		memput(q, sizeof(*q));
4977c478bd9Sstevel@tonic-gate 	if (q2 != NULL)
4987c478bd9Sstevel@tonic-gate 		memput(q2, sizeof(*q2));
4997c478bd9Sstevel@tonic-gate 	return(hp);
5007c478bd9Sstevel@tonic-gate }
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate static struct hostent *
ho_next(struct irs_ho * this)5037c478bd9Sstevel@tonic-gate ho_next(struct irs_ho *this) {
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	UNUSED(this);
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	return (NULL);
5087c478bd9Sstevel@tonic-gate }
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate static void
ho_rewind(struct irs_ho * this)5117c478bd9Sstevel@tonic-gate ho_rewind(struct irs_ho *this) {
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	UNUSED(this);
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	/* NOOP */
5167c478bd9Sstevel@tonic-gate }
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate static void
ho_minimize(struct irs_ho * this)5197c478bd9Sstevel@tonic-gate ho_minimize(struct irs_ho *this) {
5207c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	if (pvt->res)
5237c478bd9Sstevel@tonic-gate 		res_nclose(pvt->res);
5247c478bd9Sstevel@tonic-gate }
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate static struct __res_state *
ho_res_get(struct irs_ho * this)5277c478bd9Sstevel@tonic-gate ho_res_get(struct irs_ho *this) {
5287c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	if (!pvt->res) {
5317c478bd9Sstevel@tonic-gate 		struct __res_state *res;
5327c478bd9Sstevel@tonic-gate 		res = (struct __res_state *)malloc(sizeof *res);
5337c478bd9Sstevel@tonic-gate 		if (!res) {
5347c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
5357c478bd9Sstevel@tonic-gate 			return (NULL);
5367c478bd9Sstevel@tonic-gate 		}
5377c478bd9Sstevel@tonic-gate 		memset(res, 0, sizeof *res);
5387c478bd9Sstevel@tonic-gate 		ho_res_set(this, res, free);
5397c478bd9Sstevel@tonic-gate 	}
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	return (pvt->res);
5427c478bd9Sstevel@tonic-gate }
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate /* XXX */
5457c478bd9Sstevel@tonic-gate extern struct addrinfo *addr2addrinfo __P((const struct addrinfo *,
5467c478bd9Sstevel@tonic-gate 					   const char *));
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate static struct addrinfo *
ho_addrinfo(struct irs_ho * this,const char * name,const struct addrinfo * pai)5497c478bd9Sstevel@tonic-gate ho_addrinfo(struct irs_ho *this, const char *name, const struct addrinfo *pai)
5507c478bd9Sstevel@tonic-gate {
5517c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
5527c478bd9Sstevel@tonic-gate 	int n;
5537c478bd9Sstevel@tonic-gate 	char tmp[NS_MAXDNAME];
5547c478bd9Sstevel@tonic-gate 	const char *cp;
5557c478bd9Sstevel@tonic-gate 	struct dns_res_target *q, *q2, *p;
5567c478bd9Sstevel@tonic-gate 	struct addrinfo sentinel, *cur;
5577c478bd9Sstevel@tonic-gate 	int querystate = RESQRY_FAIL;
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	if (init(this) == -1)
5607c478bd9Sstevel@tonic-gate 		return (NULL);
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	memset(&sentinel, 0, sizeof(sentinel));
5637c478bd9Sstevel@tonic-gate 	cur = &sentinel;
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	q = memget(sizeof(*q));
5667c478bd9Sstevel@tonic-gate 	q2 = memget(sizeof(*q2));
5677c478bd9Sstevel@tonic-gate 	if (q == NULL || q2 == NULL) {
5687c478bd9Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
5697c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
5707c478bd9Sstevel@tonic-gate 		goto cleanup;
5717c478bd9Sstevel@tonic-gate 	}
572*9525b14bSRao Shoaib 	memset(q, 0, sizeof(*q2));
573*9525b14bSRao Shoaib 	memset(q2, 0, sizeof(*q2));
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	switch (pai->ai_family) {
5767c478bd9Sstevel@tonic-gate 	case AF_UNSPEC:
5777c478bd9Sstevel@tonic-gate 		/* prefer IPv6 */
5787c478bd9Sstevel@tonic-gate 		q->qclass = C_IN;
5797c478bd9Sstevel@tonic-gate 		q->qtype = T_AAAA;
5807c478bd9Sstevel@tonic-gate 		q->answer = q->qbuf.buf;
5817c478bd9Sstevel@tonic-gate 		q->anslen = sizeof(q->qbuf);
5827c478bd9Sstevel@tonic-gate 		q->next = q2;
5837c478bd9Sstevel@tonic-gate 		q->action = RESTGT_DOALWAYS;
5847c478bd9Sstevel@tonic-gate 		q2->qclass = C_IN;
5857c478bd9Sstevel@tonic-gate 		q2->qtype = T_A;
5867c478bd9Sstevel@tonic-gate 		q2->answer = q2->qbuf.buf;
5877c478bd9Sstevel@tonic-gate 		q2->anslen = sizeof(q2->qbuf);
5887c478bd9Sstevel@tonic-gate 		q2->action = RESTGT_DOALWAYS;
5897c478bd9Sstevel@tonic-gate 		break;
5907c478bd9Sstevel@tonic-gate 	case AF_INET:
5917c478bd9Sstevel@tonic-gate 		q->qclass = C_IN;
5927c478bd9Sstevel@tonic-gate 		q->qtype = T_A;
5937c478bd9Sstevel@tonic-gate 		q->answer = q->qbuf.buf;
5947c478bd9Sstevel@tonic-gate 		q->anslen = sizeof(q->qbuf);
5957c478bd9Sstevel@tonic-gate 		q->action = RESTGT_DOALWAYS;
5967c478bd9Sstevel@tonic-gate 		break;
5977c478bd9Sstevel@tonic-gate 	case AF_INET6:
5987c478bd9Sstevel@tonic-gate 		q->qclass = C_IN;
5997c478bd9Sstevel@tonic-gate 		q->qtype = T_AAAA;
6007c478bd9Sstevel@tonic-gate 		q->answer = q->qbuf.buf;
6017c478bd9Sstevel@tonic-gate 		q->anslen = sizeof(q->qbuf);
6027c478bd9Sstevel@tonic-gate 		q->action = RESTGT_DOALWAYS;
6037c478bd9Sstevel@tonic-gate 		break;
6047c478bd9Sstevel@tonic-gate 	default:
605*9525b14bSRao Shoaib 		RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); /*%< better error? */
6067c478bd9Sstevel@tonic-gate 		goto cleanup;
6077c478bd9Sstevel@tonic-gate 	}
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	/*
6107c478bd9Sstevel@tonic-gate 	 * if there aren't any dots, it could be a user-level alias.
6117c478bd9Sstevel@tonic-gate 	 * this is also done in res_nquery() since we are not the only
6127c478bd9Sstevel@tonic-gate 	 * function that looks up host names.
6137c478bd9Sstevel@tonic-gate 	 */
6147c478bd9Sstevel@tonic-gate 	if (!strchr(name, '.') && (cp = res_hostalias(pvt->res, name,
6157c478bd9Sstevel@tonic-gate 						      tmp, sizeof tmp)))
6167c478bd9Sstevel@tonic-gate 		name = cp;
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	for (p = q; p; p = p->next) {
6197c478bd9Sstevel@tonic-gate 		struct addrinfo *ai;
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 		switch(p->action) {
6227c478bd9Sstevel@tonic-gate 		case RESTGT_DOALWAYS:
6237c478bd9Sstevel@tonic-gate 			break;
6247c478bd9Sstevel@tonic-gate 		case RESTGT_AFTERFAILURE:
6257c478bd9Sstevel@tonic-gate 			if (querystate == RESQRY_SUCCESS)
6267c478bd9Sstevel@tonic-gate 				continue;
6277c478bd9Sstevel@tonic-gate 			break;
6287c478bd9Sstevel@tonic-gate 		case RESTGT_IGNORE:
6297c478bd9Sstevel@tonic-gate 			continue;
6307c478bd9Sstevel@tonic-gate 		}
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 		if ((n = res_nsearch(pvt->res, name, p->qclass, p->qtype,
6337c478bd9Sstevel@tonic-gate 				     p->answer, p->anslen)) < 0) {
6347c478bd9Sstevel@tonic-gate 			querystate = RESQRY_FAIL;
6357c478bd9Sstevel@tonic-gate 			continue;
6367c478bd9Sstevel@tonic-gate 		}
6377c478bd9Sstevel@tonic-gate 		(void)gethostans(this, p->answer, n, name, p->qtype,
638*9525b14bSRao Shoaib 				 pai->ai_family, /*%< XXX: meaningless */
6397c478bd9Sstevel@tonic-gate 				 0, &ai, pai);
6407c478bd9Sstevel@tonic-gate 		if (ai) {
6417c478bd9Sstevel@tonic-gate 			querystate = RESQRY_SUCCESS;
6427c478bd9Sstevel@tonic-gate 			cur->ai_next = ai;
643*9525b14bSRao Shoaib 			while (cur->ai_next)
6447c478bd9Sstevel@tonic-gate 				cur = cur->ai_next;
645*9525b14bSRao Shoaib 		} else
6467c478bd9Sstevel@tonic-gate 			querystate = RESQRY_FAIL;
6477c478bd9Sstevel@tonic-gate 	}
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate  cleanup:
6507c478bd9Sstevel@tonic-gate 	if (q != NULL)
6517c478bd9Sstevel@tonic-gate 		memput(q, sizeof(*q));
6527c478bd9Sstevel@tonic-gate 	if (q2 != NULL)
6537c478bd9Sstevel@tonic-gate 		memput(q2, sizeof(*q2));
6547c478bd9Sstevel@tonic-gate 	return(sentinel.ai_next);
6557c478bd9Sstevel@tonic-gate }
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate static void
ho_res_set(struct irs_ho * this,struct __res_state * res,void (* free_res)(void *))6587c478bd9Sstevel@tonic-gate ho_res_set(struct irs_ho *this, struct __res_state *res,
6597c478bd9Sstevel@tonic-gate 		void (*free_res)(void *)) {
6607c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 	if (pvt->res && pvt->free_res) {
6637c478bd9Sstevel@tonic-gate 		res_nclose(pvt->res);
6647c478bd9Sstevel@tonic-gate 		(*pvt->free_res)(pvt->res);
6657c478bd9Sstevel@tonic-gate 	}
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	pvt->res = res;
6687c478bd9Sstevel@tonic-gate 	pvt->free_res = free_res;
6697c478bd9Sstevel@tonic-gate }
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate /* Private. */
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate static struct hostent *
gethostans(struct irs_ho * this,const u_char * ansbuf,int anslen,const char * qname,int qtype,int af,int size,struct addrinfo ** ret_aip,const struct addrinfo * pai)6747c478bd9Sstevel@tonic-gate gethostans(struct irs_ho *this,
6757c478bd9Sstevel@tonic-gate 	   const u_char *ansbuf, int anslen, const char *qname, int qtype,
676*9525b14bSRao Shoaib 	   int af, int size,	/*!< meaningless for addrinfo cases  */
6777c478bd9Sstevel@tonic-gate 	   struct addrinfo **ret_aip, const struct addrinfo *pai)
6787c478bd9Sstevel@tonic-gate {
6797c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
6807c478bd9Sstevel@tonic-gate 	int type, class, ancount, qdcount, n, haveanswer, had_error;
681*9525b14bSRao Shoaib 	int error = NETDB_SUCCESS;
6827c478bd9Sstevel@tonic-gate 	int (*name_ok)(const char *);
6837c478bd9Sstevel@tonic-gate 	const HEADER *hp;
6847c478bd9Sstevel@tonic-gate 	const u_char *eom;
6857c478bd9Sstevel@tonic-gate 	const u_char *eor;
6867c478bd9Sstevel@tonic-gate 	const u_char *cp;
6877c478bd9Sstevel@tonic-gate 	const char *tname;
6887c478bd9Sstevel@tonic-gate 	const char *hname;
6897c478bd9Sstevel@tonic-gate 	char *bp, *ep, **ap, **hap;
6907c478bd9Sstevel@tonic-gate 	char tbuf[MAXDNAME+1];
6917c478bd9Sstevel@tonic-gate 	struct addrinfo sentinel, *cur, ai;
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	if (pai == NULL) abort();
6947c478bd9Sstevel@tonic-gate 	if (ret_aip != NULL)
6957c478bd9Sstevel@tonic-gate 		*ret_aip = NULL;
6967c478bd9Sstevel@tonic-gate 	memset(&sentinel, 0, sizeof(sentinel));
6977c478bd9Sstevel@tonic-gate 	cur = &sentinel;
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	tname = qname;
7007c478bd9Sstevel@tonic-gate 	eom = ansbuf + anslen;
7017c478bd9Sstevel@tonic-gate 	switch (qtype) {
7027c478bd9Sstevel@tonic-gate 	case T_A:
7037c478bd9Sstevel@tonic-gate 	case T_AAAA:
704*9525b14bSRao Shoaib 	case T_ANY:	/*%< use T_ANY only for T_A/T_AAAA lookup */
7057c478bd9Sstevel@tonic-gate 		name_ok = res_hnok;
7067c478bd9Sstevel@tonic-gate 		break;
7077c478bd9Sstevel@tonic-gate 	case T_PTR:
7087c478bd9Sstevel@tonic-gate 		name_ok = res_dnok;
7097c478bd9Sstevel@tonic-gate 		break;
7107c478bd9Sstevel@tonic-gate 	default:
7117c478bd9Sstevel@tonic-gate 		abort();
7127c478bd9Sstevel@tonic-gate 	}
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	pvt->host.h_addrtype = af;
7157c478bd9Sstevel@tonic-gate 	pvt->host.h_length = size;
7167c478bd9Sstevel@tonic-gate 	hname = pvt->host.h_name = NULL;
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	/*
7197c478bd9Sstevel@tonic-gate 	 * Find first satisfactory answer.
7207c478bd9Sstevel@tonic-gate 	 */
7217c478bd9Sstevel@tonic-gate 	if (ansbuf + HFIXEDSZ > eom) {
7227c478bd9Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
7237c478bd9Sstevel@tonic-gate 		return (NULL);
7247c478bd9Sstevel@tonic-gate 	}
7257c478bd9Sstevel@tonic-gate 	hp = (const HEADER *)ansbuf;
7267c478bd9Sstevel@tonic-gate 	ancount = ntohs(hp->ancount);
7277c478bd9Sstevel@tonic-gate 	qdcount = ntohs(hp->qdcount);
7287c478bd9Sstevel@tonic-gate 	bp = pvt->hostbuf;
7297c478bd9Sstevel@tonic-gate 	ep = pvt->hostbuf + sizeof(pvt->hostbuf);
7307c478bd9Sstevel@tonic-gate 	cp = ansbuf + HFIXEDSZ;
7317c478bd9Sstevel@tonic-gate 	if (qdcount != 1) {
7327c478bd9Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
7337c478bd9Sstevel@tonic-gate 		return (NULL);
7347c478bd9Sstevel@tonic-gate 	}
7357c478bd9Sstevel@tonic-gate 	n = dn_expand(ansbuf, eom, cp, bp, ep - bp);
7367c478bd9Sstevel@tonic-gate 	if (n < 0 || !maybe_ok(pvt->res, bp, name_ok)) {
7377c478bd9Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
7387c478bd9Sstevel@tonic-gate 		return (NULL);
7397c478bd9Sstevel@tonic-gate 	}
7407c478bd9Sstevel@tonic-gate 	cp += n + QFIXEDSZ;
7417c478bd9Sstevel@tonic-gate 	if (cp > eom) {
7427c478bd9Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
7437c478bd9Sstevel@tonic-gate 		return (NULL);
7447c478bd9Sstevel@tonic-gate 	}
7457c478bd9Sstevel@tonic-gate 	if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
7467c478bd9Sstevel@tonic-gate 		/* res_nsend() has already verified that the query name is the
7477c478bd9Sstevel@tonic-gate 		 * same as the one we sent; this just gets the expanded name
7487c478bd9Sstevel@tonic-gate 		 * (i.e., with the succeeding search-domain tacked on).
7497c478bd9Sstevel@tonic-gate 		 */
750*9525b14bSRao Shoaib 		n = strlen(bp) + 1;		/*%< for the \\0 */
7517c478bd9Sstevel@tonic-gate 		if (n > MAXHOSTNAMELEN) {
7527c478bd9Sstevel@tonic-gate 			RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
7537c478bd9Sstevel@tonic-gate 			return (NULL);
7547c478bd9Sstevel@tonic-gate 		}
7557c478bd9Sstevel@tonic-gate 		pvt->host.h_name = bp;
7567c478bd9Sstevel@tonic-gate 		hname = bp;
7577c478bd9Sstevel@tonic-gate 		bp += n;
7587c478bd9Sstevel@tonic-gate 		/* The qname can be abbreviated, but hname is now absolute. */
7597c478bd9Sstevel@tonic-gate 		qname = pvt->host.h_name;
7607c478bd9Sstevel@tonic-gate 	}
7617c478bd9Sstevel@tonic-gate 	ap = pvt->host_aliases;
7627c478bd9Sstevel@tonic-gate 	*ap = NULL;
7637c478bd9Sstevel@tonic-gate 	pvt->host.h_aliases = pvt->host_aliases;
7647c478bd9Sstevel@tonic-gate 	hap = pvt->h_addr_ptrs;
7657c478bd9Sstevel@tonic-gate 	*hap = NULL;
7667c478bd9Sstevel@tonic-gate 	pvt->host.h_addr_list = pvt->h_addr_ptrs;
7677c478bd9Sstevel@tonic-gate 	haveanswer = 0;
7687c478bd9Sstevel@tonic-gate 	had_error = 0;
7697c478bd9Sstevel@tonic-gate 	while (ancount-- > 0 && cp < eom && !had_error) {
7707c478bd9Sstevel@tonic-gate 		n = dn_expand(ansbuf, eom, cp, bp, ep - bp);
7717c478bd9Sstevel@tonic-gate 		if (n < 0 || !maybe_ok(pvt->res, bp, name_ok)) {
7727c478bd9Sstevel@tonic-gate 			had_error++;
7737c478bd9Sstevel@tonic-gate 			continue;
7747c478bd9Sstevel@tonic-gate 		}
775*9525b14bSRao Shoaib 		cp += n;			/*%< name */
7767c478bd9Sstevel@tonic-gate 		BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
7777c478bd9Sstevel@tonic-gate 		type = ns_get16(cp);
778*9525b14bSRao Shoaib 		cp += INT16SZ;			/*%< type */
7797c478bd9Sstevel@tonic-gate 		class = ns_get16(cp);
780*9525b14bSRao Shoaib 		cp += INT16SZ + INT32SZ;	/*%< class, TTL */
7817c478bd9Sstevel@tonic-gate 		n = ns_get16(cp);
782*9525b14bSRao Shoaib 		cp += INT16SZ;			/*%< len */
7837c478bd9Sstevel@tonic-gate 		BOUNDS_CHECK(cp, n);
7847c478bd9Sstevel@tonic-gate 		if (class != C_IN) {
7857c478bd9Sstevel@tonic-gate 			cp += n;
7867c478bd9Sstevel@tonic-gate 			continue;
7877c478bd9Sstevel@tonic-gate 		}
7887c478bd9Sstevel@tonic-gate 		eor = cp + n;
7897c478bd9Sstevel@tonic-gate 		if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) &&
7907c478bd9Sstevel@tonic-gate 		    type == T_CNAME) {
7917c478bd9Sstevel@tonic-gate 			if (haveanswer) {
7927c478bd9Sstevel@tonic-gate 				int level = LOG_CRIT;
7937c478bd9Sstevel@tonic-gate #ifdef LOG_SECURITY
7947c478bd9Sstevel@tonic-gate 				level |= LOG_SECURITY;
7957c478bd9Sstevel@tonic-gate #endif
7967c478bd9Sstevel@tonic-gate 				syslog(level,
7977c478bd9Sstevel@tonic-gate  "gethostans: possible attempt to exploit buffer overflow while looking up %s",
7987c478bd9Sstevel@tonic-gate 					*qname ? qname : ".");
7997c478bd9Sstevel@tonic-gate 			}
8007c478bd9Sstevel@tonic-gate 			n = dn_expand(ansbuf, eor, cp, tbuf, sizeof tbuf);
8017c478bd9Sstevel@tonic-gate 			if (n < 0 || !maybe_ok(pvt->res, tbuf, name_ok)) {
8027c478bd9Sstevel@tonic-gate 				had_error++;
8037c478bd9Sstevel@tonic-gate 				continue;
8047c478bd9Sstevel@tonic-gate 			}
8057c478bd9Sstevel@tonic-gate 			cp += n;
8067c478bd9Sstevel@tonic-gate 			/* Store alias. */
8077c478bd9Sstevel@tonic-gate 			if (ap >= &pvt->host_aliases[MAXALIASES-1])
8087c478bd9Sstevel@tonic-gate 				continue;
8097c478bd9Sstevel@tonic-gate 			*ap++ = bp;
810*9525b14bSRao Shoaib 			n = strlen(bp) + 1;	/*%< for the \\0 */
8117c478bd9Sstevel@tonic-gate 			bp += n;
8127c478bd9Sstevel@tonic-gate 			/* Get canonical name. */
813*9525b14bSRao Shoaib 			n = strlen(tbuf) + 1;	/*%< for the \\0 */
8147c478bd9Sstevel@tonic-gate 			if (n > (ep - bp) || n > MAXHOSTNAMELEN) {
8157c478bd9Sstevel@tonic-gate 				had_error++;
8167c478bd9Sstevel@tonic-gate 				continue;
8177c478bd9Sstevel@tonic-gate 			}
818*9525b14bSRao Shoaib 			strcpy(bp, tbuf);	/* (checked) */
8197c478bd9Sstevel@tonic-gate 			pvt->host.h_name = bp;
8207c478bd9Sstevel@tonic-gate 			hname = bp;
8217c478bd9Sstevel@tonic-gate 			bp += n;
8227c478bd9Sstevel@tonic-gate 			continue;
8237c478bd9Sstevel@tonic-gate 		}
8247c478bd9Sstevel@tonic-gate 		if (qtype == T_PTR && type == T_CNAME) {
8257c478bd9Sstevel@tonic-gate 			n = dn_expand(ansbuf, eor, cp, tbuf, sizeof tbuf);
8267c478bd9Sstevel@tonic-gate 			if (n < 0 || !maybe_dnok(pvt->res, tbuf)) {
8277c478bd9Sstevel@tonic-gate 				had_error++;
8287c478bd9Sstevel@tonic-gate 				continue;
8297c478bd9Sstevel@tonic-gate 			}
8307c478bd9Sstevel@tonic-gate 			cp += n;
8317c478bd9Sstevel@tonic-gate #ifdef RES_USE_DNAME
832*9525b14bSRao Shoaib 			if ((pvt->res->options & RES_USE_DNAME) != 0U)
8337c478bd9Sstevel@tonic-gate #endif
8347c478bd9Sstevel@tonic-gate 			{
8357c478bd9Sstevel@tonic-gate 				/*
8367c478bd9Sstevel@tonic-gate 				 * We may be able to check this regardless
8377c478bd9Sstevel@tonic-gate 				 * of the USE_DNAME bit, but we add the check
8387c478bd9Sstevel@tonic-gate 				 * for now since the DNAME support is
8397c478bd9Sstevel@tonic-gate 				 * experimental.
8407c478bd9Sstevel@tonic-gate 				 */
8417c478bd9Sstevel@tonic-gate 				if (ns_samename(tname, bp) != 1)
8427c478bd9Sstevel@tonic-gate 					continue;
8437c478bd9Sstevel@tonic-gate 			}
8447c478bd9Sstevel@tonic-gate 			/* Get canonical name. */
845*9525b14bSRao Shoaib 			n = strlen(tbuf) + 1;	/*%< for the \\0 */
8467c478bd9Sstevel@tonic-gate 			if (n > (ep - bp)) {
8477c478bd9Sstevel@tonic-gate 				had_error++;
8487c478bd9Sstevel@tonic-gate 				continue;
8497c478bd9Sstevel@tonic-gate 			}
850*9525b14bSRao Shoaib 			strcpy(bp, tbuf);	/* (checked) */
8517c478bd9Sstevel@tonic-gate 			tname = bp;
8527c478bd9Sstevel@tonic-gate 			bp += n;
8537c478bd9Sstevel@tonic-gate 			continue;
8547c478bd9Sstevel@tonic-gate 		}
8557c478bd9Sstevel@tonic-gate 		if (qtype == T_ANY) {
8567c478bd9Sstevel@tonic-gate 			if (!(type == T_A || type == T_AAAA)) {
8577c478bd9Sstevel@tonic-gate 				cp += n;
8587c478bd9Sstevel@tonic-gate 				continue;
8597c478bd9Sstevel@tonic-gate 			}
8607c478bd9Sstevel@tonic-gate 		} else if (type != qtype) {
8617c478bd9Sstevel@tonic-gate 			cp += n;
8627c478bd9Sstevel@tonic-gate 			continue;
8637c478bd9Sstevel@tonic-gate 		}
8647c478bd9Sstevel@tonic-gate 		switch (type) {
8657c478bd9Sstevel@tonic-gate 		case T_PTR:
8667c478bd9Sstevel@tonic-gate 			if (ret_aip != NULL) {
8677c478bd9Sstevel@tonic-gate 				/* addrinfo never needs T_PTR */
8687c478bd9Sstevel@tonic-gate 				cp += n;
8697c478bd9Sstevel@tonic-gate 				continue;
8707c478bd9Sstevel@tonic-gate 			}
8717c478bd9Sstevel@tonic-gate 			if (ns_samename(tname, bp) != 1) {
8727c478bd9Sstevel@tonic-gate 				cp += n;
8737c478bd9Sstevel@tonic-gate 				continue;
8747c478bd9Sstevel@tonic-gate 			}
8757c478bd9Sstevel@tonic-gate 			n = dn_expand(ansbuf, eor, cp, bp, ep - bp);
8767c478bd9Sstevel@tonic-gate 			if (n < 0 || !maybe_hnok(pvt->res, bp) ||
8777c478bd9Sstevel@tonic-gate 			    n >= MAXHOSTNAMELEN) {
8787c478bd9Sstevel@tonic-gate 				had_error++;
8797c478bd9Sstevel@tonic-gate 				break;
8807c478bd9Sstevel@tonic-gate 			}
8817c478bd9Sstevel@tonic-gate 			cp += n;
8827c478bd9Sstevel@tonic-gate 			if (!haveanswer) {
8837c478bd9Sstevel@tonic-gate 				pvt->host.h_name = bp;
8847c478bd9Sstevel@tonic-gate 				hname = bp;
8857c478bd9Sstevel@tonic-gate 			}
8867c478bd9Sstevel@tonic-gate 			else if (ap < &pvt->host_aliases[MAXALIASES-1])
8877c478bd9Sstevel@tonic-gate 				*ap++ = bp;
8887c478bd9Sstevel@tonic-gate 			else
8897c478bd9Sstevel@tonic-gate 				n = -1;
8907c478bd9Sstevel@tonic-gate 			if (n != -1) {
891*9525b14bSRao Shoaib 				n = strlen(bp) + 1;	/*%< for the \\0 */
8927c478bd9Sstevel@tonic-gate 				bp += n;
8937c478bd9Sstevel@tonic-gate 			}
8947c478bd9Sstevel@tonic-gate 			break;
8957c478bd9Sstevel@tonic-gate 		case T_A:
8967c478bd9Sstevel@tonic-gate 		case T_AAAA:
8977c478bd9Sstevel@tonic-gate 			if (ns_samename(hname, bp) != 1) {
8987c478bd9Sstevel@tonic-gate 				cp += n;
8997c478bd9Sstevel@tonic-gate 				continue;
9007c478bd9Sstevel@tonic-gate 			}
9017c478bd9Sstevel@tonic-gate 			if (type == T_A && n != INADDRSZ) {
9027c478bd9Sstevel@tonic-gate 				cp += n;
9037c478bd9Sstevel@tonic-gate 				continue;
9047c478bd9Sstevel@tonic-gate 			}
9057c478bd9Sstevel@tonic-gate 			if (type == T_AAAA && n != IN6ADDRSZ) {
9067c478bd9Sstevel@tonic-gate 				cp += n;
9077c478bd9Sstevel@tonic-gate 				continue;
9087c478bd9Sstevel@tonic-gate 			}
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 			/* make addrinfo. don't overwrite constant PAI */
9117c478bd9Sstevel@tonic-gate 			ai = *pai;
9127c478bd9Sstevel@tonic-gate 			ai.ai_family = (type == T_AAAA) ? AF_INET6 : AF_INET;
9137c478bd9Sstevel@tonic-gate 			cur->ai_next = addr2addrinfo(
9147c478bd9Sstevel@tonic-gate 					(const struct addrinfo *)&ai,
9157c478bd9Sstevel@tonic-gate 					(const char *)cp);
9167c478bd9Sstevel@tonic-gate 			if (cur->ai_next == NULL)
9177c478bd9Sstevel@tonic-gate 				had_error++;
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 			if (!haveanswer) {
9207c478bd9Sstevel@tonic-gate 				int nn;
9217c478bd9Sstevel@tonic-gate 
922*9525b14bSRao Shoaib 				nn = strlen(bp) + 1;	/*%< for the \\0 */
9237c478bd9Sstevel@tonic-gate 				if (nn >= MAXHOSTNAMELEN) {
9247c478bd9Sstevel@tonic-gate 					cp += n;
9257c478bd9Sstevel@tonic-gate 					had_error++;
9267c478bd9Sstevel@tonic-gate 					continue;
9277c478bd9Sstevel@tonic-gate 				}
9287c478bd9Sstevel@tonic-gate 				pvt->host.h_name = bp;
9297c478bd9Sstevel@tonic-gate 				hname = bp;
9307c478bd9Sstevel@tonic-gate 				bp += nn;
9317c478bd9Sstevel@tonic-gate 			}
9327c478bd9Sstevel@tonic-gate 			/* Ensure alignment. */
9337c478bd9Sstevel@tonic-gate 			bp = (char *)(((u_long)bp + (sizeof(align) - 1)) &
9347c478bd9Sstevel@tonic-gate 				      ~(sizeof(align) - 1));
9357c478bd9Sstevel@tonic-gate 			/* Avoid overflows. */
936*9525b14bSRao Shoaib 			if (bp + n > &pvt->hostbuf[sizeof(pvt->hostbuf) - 1]) {
9377c478bd9Sstevel@tonic-gate 				had_error++;
9387c478bd9Sstevel@tonic-gate 				continue;
9397c478bd9Sstevel@tonic-gate 			}
940*9525b14bSRao Shoaib 			if (ret_aip) { /*%< need addrinfo. keep it. */
941*9525b14bSRao Shoaib 				while (cur->ai_next)
9427c478bd9Sstevel@tonic-gate 					cur = cur->ai_next;
943*9525b14bSRao Shoaib 			} else if (cur->ai_next) { /*%< need hostent */
9447c478bd9Sstevel@tonic-gate 				struct addrinfo *aip = cur->ai_next;
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 				for (aip = cur->ai_next; aip;
9477c478bd9Sstevel@tonic-gate 				     aip = aip->ai_next) {
9487c478bd9Sstevel@tonic-gate 					int m;
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 					m = add_hostent(pvt, bp, hap, aip);
9517c478bd9Sstevel@tonic-gate 					if (m < 0) {
9527c478bd9Sstevel@tonic-gate 						had_error++;
9537c478bd9Sstevel@tonic-gate 						break;
9547c478bd9Sstevel@tonic-gate 					}
9557c478bd9Sstevel@tonic-gate 					if (m == 0)
9567c478bd9Sstevel@tonic-gate 						continue;
957b07ce584SStacey Marshall 					if (hap < &pvt->h_addr_ptrs[MAXADDRS])
9587c478bd9Sstevel@tonic-gate 						hap++;
9597c478bd9Sstevel@tonic-gate 					*hap = NULL;
9607c478bd9Sstevel@tonic-gate 					bp += m;
9617c478bd9Sstevel@tonic-gate 				}
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 				freeaddrinfo(cur->ai_next);
9647c478bd9Sstevel@tonic-gate 				cur->ai_next = NULL;
9657c478bd9Sstevel@tonic-gate 			}
9667c478bd9Sstevel@tonic-gate 			cp += n;
9677c478bd9Sstevel@tonic-gate 			break;
9687c478bd9Sstevel@tonic-gate 		default:
9697c478bd9Sstevel@tonic-gate 			abort();
9707c478bd9Sstevel@tonic-gate 		}
9717c478bd9Sstevel@tonic-gate 		if (!had_error)
9727c478bd9Sstevel@tonic-gate 			haveanswer++;
9737c478bd9Sstevel@tonic-gate 	}
9747c478bd9Sstevel@tonic-gate 	if (haveanswer) {
9757c478bd9Sstevel@tonic-gate 		if (ret_aip == NULL) {
9767c478bd9Sstevel@tonic-gate 			*ap = NULL;
9777c478bd9Sstevel@tonic-gate 			*hap = NULL;
9787c478bd9Sstevel@tonic-gate 
979b07ce584SStacey Marshall 			if (pvt->res->nsort && hap != pvt->h_addr_ptrs &&
980b07ce584SStacey Marshall 			    qtype == T_A)
9817c478bd9Sstevel@tonic-gate 				addrsort(pvt->res, pvt->h_addr_ptrs,
982b07ce584SStacey Marshall 					 hap - pvt->h_addr_ptrs);
9837c478bd9Sstevel@tonic-gate 			if (pvt->host.h_name == NULL) {
984*9525b14bSRao Shoaib 				n = strlen(qname) + 1;	/*%< for the \\0 */
9857c478bd9Sstevel@tonic-gate 				if (n > (ep - bp) || n >= MAXHOSTNAMELEN)
9867c478bd9Sstevel@tonic-gate 					goto no_recovery;
987*9525b14bSRao Shoaib 				strcpy(bp, qname);	/* (checked) */
9887c478bd9Sstevel@tonic-gate 				pvt->host.h_name = bp;
9897c478bd9Sstevel@tonic-gate 				bp += n;
9907c478bd9Sstevel@tonic-gate 			}
9917c478bd9Sstevel@tonic-gate 			if (pvt->res->options & RES_USE_INET6)
9927c478bd9Sstevel@tonic-gate 				map_v4v6_hostent(&pvt->host, &bp, ep);
9937c478bd9Sstevel@tonic-gate 			RES_SET_H_ERRNO(pvt->res, NETDB_SUCCESS);
9947c478bd9Sstevel@tonic-gate 			return (&pvt->host);
9957c478bd9Sstevel@tonic-gate 		} else {
9967c478bd9Sstevel@tonic-gate 			if ((pai->ai_flags & AI_CANONNAME) != 0) {
9977c478bd9Sstevel@tonic-gate 				if (pvt->host.h_name == NULL) {
9987c478bd9Sstevel@tonic-gate 					sentinel.ai_next->ai_canonname =
9997c478bd9Sstevel@tonic-gate 						strdup(qname);
10007c478bd9Sstevel@tonic-gate 				}
10017c478bd9Sstevel@tonic-gate 				else {
10027c478bd9Sstevel@tonic-gate 					sentinel.ai_next->ai_canonname =
10037c478bd9Sstevel@tonic-gate 						strdup(pvt->host.h_name);
10047c478bd9Sstevel@tonic-gate 				}
10057c478bd9Sstevel@tonic-gate 			}
10067c478bd9Sstevel@tonic-gate 			*ret_aip = sentinel.ai_next;
10077c478bd9Sstevel@tonic-gate 			return(NULL);
10087c478bd9Sstevel@tonic-gate 		}
10097c478bd9Sstevel@tonic-gate 	}
10107c478bd9Sstevel@tonic-gate  no_recovery:
10117c478bd9Sstevel@tonic-gate 	if (sentinel.ai_next) {
10127c478bd9Sstevel@tonic-gate 		/* this should be impossible, but check it for safety */
10137c478bd9Sstevel@tonic-gate 		freeaddrinfo(sentinel.ai_next);
10147c478bd9Sstevel@tonic-gate 	}
10157c478bd9Sstevel@tonic-gate 	if (error == NETDB_SUCCESS)
10167c478bd9Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
10177c478bd9Sstevel@tonic-gate 	else
10187c478bd9Sstevel@tonic-gate 		RES_SET_H_ERRNO(pvt->res, error);
10197c478bd9Sstevel@tonic-gate 	return(NULL);
10207c478bd9Sstevel@tonic-gate }
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate static int
add_hostent(struct pvt * pvt,char * bp,char ** hap,struct addrinfo * ai)10237c478bd9Sstevel@tonic-gate add_hostent(struct pvt *pvt, char *bp, char **hap, struct addrinfo *ai)
10247c478bd9Sstevel@tonic-gate {
10257c478bd9Sstevel@tonic-gate 	int addrlen;
10267c478bd9Sstevel@tonic-gate 	char *addrp;
10277c478bd9Sstevel@tonic-gate 	const char **tap;
10287c478bd9Sstevel@tonic-gate 	char *obp = bp;
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 	switch(ai->ai_addr->sa_family) {
10317c478bd9Sstevel@tonic-gate 	case AF_INET6:
10327c478bd9Sstevel@tonic-gate 		addrlen = IN6ADDRSZ;
10337c478bd9Sstevel@tonic-gate 		addrp = (char *)&((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr;
10347c478bd9Sstevel@tonic-gate 		break;
10357c478bd9Sstevel@tonic-gate 	case AF_INET:
10367c478bd9Sstevel@tonic-gate 		addrlen = INADDRSZ;
10377c478bd9Sstevel@tonic-gate 		addrp = (char *)&((struct sockaddr_in *)ai->ai_addr)->sin_addr;
10387c478bd9Sstevel@tonic-gate 		break;
10397c478bd9Sstevel@tonic-gate 	default:
1040*9525b14bSRao Shoaib 		return(-1);	/*%< abort? */
10417c478bd9Sstevel@tonic-gate 	}
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 	/* Ensure alignment. */
10447c478bd9Sstevel@tonic-gate 	bp = (char *)(((u_long)bp + (sizeof(align) - 1)) &
10457c478bd9Sstevel@tonic-gate 		      ~(sizeof(align) - 1));
10467c478bd9Sstevel@tonic-gate 	/* Avoid overflows. */
1047*9525b14bSRao Shoaib 	if (bp + addrlen > &pvt->hostbuf[sizeof(pvt->hostbuf) - 1])
10487c478bd9Sstevel@tonic-gate 		return(-1);
1049b07ce584SStacey Marshall 	if (hap >= &pvt->h_addr_ptrs[MAXADDRS])
1050*9525b14bSRao Shoaib 		return(0); /*%< fail, but not treat it as an error. */
10517c478bd9Sstevel@tonic-gate 	/* Suppress duplicates. */
10527c478bd9Sstevel@tonic-gate 	for (tap = (const char **)pvt->h_addr_ptrs;
10537c478bd9Sstevel@tonic-gate 	     *tap != NULL;
10547c478bd9Sstevel@tonic-gate 	     tap++)
10557c478bd9Sstevel@tonic-gate 		if (memcmp(*tap, addrp, addrlen) == 0)
10567c478bd9Sstevel@tonic-gate 			break;
10577c478bd9Sstevel@tonic-gate 	if (*tap != NULL)
10587c478bd9Sstevel@tonic-gate 		return (0);
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 	memcpy(*hap = bp, addrp, addrlen);
10617c478bd9Sstevel@tonic-gate 	return((bp + addrlen) - obp);
10627c478bd9Sstevel@tonic-gate }
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate static void
map_v4v6_hostent(struct hostent * hp,char ** bpp,char * ep)10657c478bd9Sstevel@tonic-gate map_v4v6_hostent(struct hostent *hp, char **bpp, char *ep) {
10667c478bd9Sstevel@tonic-gate 	char **ap;
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 	if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
10697c478bd9Sstevel@tonic-gate 		return;
10707c478bd9Sstevel@tonic-gate 	hp->h_addrtype = AF_INET6;
10717c478bd9Sstevel@tonic-gate 	hp->h_length = IN6ADDRSZ;
10727c478bd9Sstevel@tonic-gate 	for (ap = hp->h_addr_list; *ap; ap++) {
10737c478bd9Sstevel@tonic-gate 		int i = (u_long)*bpp % sizeof(align);
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 		if (i != 0)
10767c478bd9Sstevel@tonic-gate 			i = sizeof(align) - i;
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 		if ((ep - *bpp) < (i + IN6ADDRSZ)) {
10797c478bd9Sstevel@tonic-gate 			/* Out of memory.  Truncate address list here. */
10807c478bd9Sstevel@tonic-gate 			*ap = NULL;
10817c478bd9Sstevel@tonic-gate 			return;
10827c478bd9Sstevel@tonic-gate 		}
10837c478bd9Sstevel@tonic-gate 		*bpp += i;
10847c478bd9Sstevel@tonic-gate 		map_v4v6_address(*ap, *bpp);
10857c478bd9Sstevel@tonic-gate 		*ap = *bpp;
10867c478bd9Sstevel@tonic-gate 		*bpp += IN6ADDRSZ;
10877c478bd9Sstevel@tonic-gate 	}
10887c478bd9Sstevel@tonic-gate }
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate static void
addrsort(res_state statp,char ** ap,int num)10917c478bd9Sstevel@tonic-gate addrsort(res_state statp, char **ap, int num) {
10927c478bd9Sstevel@tonic-gate 	int i, j, needsort = 0, aval[MAXADDRS];
10937c478bd9Sstevel@tonic-gate 	char **p;
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 	p = ap;
10967c478bd9Sstevel@tonic-gate 	for (i = 0; i < num; i++, p++) {
10977c478bd9Sstevel@tonic-gate 		for (j = 0 ; (unsigned)j < statp->nsort; j++)
1098*9525b14bSRao Shoaib 			if (statp->sort_list[j].addr.s_addr ==
10997c478bd9Sstevel@tonic-gate 			    (((struct in_addr *)(*p))->s_addr &
11007c478bd9Sstevel@tonic-gate 			     statp->sort_list[j].mask))
11017c478bd9Sstevel@tonic-gate 				break;
11027c478bd9Sstevel@tonic-gate 		aval[i] = j;
11037c478bd9Sstevel@tonic-gate 		if (needsort == 0 && i > 0 && j < aval[i-1])
11047c478bd9Sstevel@tonic-gate 			needsort = i;
11057c478bd9Sstevel@tonic-gate 	}
11067c478bd9Sstevel@tonic-gate 	if (!needsort)
11077c478bd9Sstevel@tonic-gate 		return;
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 	while (needsort < num) {
11107c478bd9Sstevel@tonic-gate 		for (j = needsort - 1; j >= 0; j--) {
11117c478bd9Sstevel@tonic-gate 			if (aval[j] > aval[j+1]) {
11127c478bd9Sstevel@tonic-gate 				char *hp;
1113