17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
6*9525b14bSRao Shoaib 
7*9525b14bSRao Shoaib /*
8*9525b14bSRao Shoaib  * Portions Copyright (C) 2004-2009  Internet Systems Consortium, Inc. ("ISC")
9*9525b14bSRao Shoaib  * Portions Copyright (C) 1996-2003  Internet Software Consortium.
10*9525b14bSRao Shoaib  *
11*9525b14bSRao Shoaib  * Permission to use, copy, modify, and/or distribute this software for any
12*9525b14bSRao Shoaib  * purpose with or without fee is hereby granted, provided that the above
13*9525b14bSRao Shoaib  * copyright notice and this permission notice appear in all copies.
14*9525b14bSRao Shoaib  *
15*9525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
16*9525b14bSRao Shoaib  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
17*9525b14bSRao Shoaib  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
18*9525b14bSRao Shoaib  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
19*9525b14bSRao Shoaib  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
20*9525b14bSRao Shoaib  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
21*9525b14bSRao Shoaib  * PERFORMANCE OF THIS SOFTWARE.
22*9525b14bSRao Shoaib  */
23*9525b14bSRao Shoaib 
247c478bd9Sstevel@tonic-gate /*
257c478bd9Sstevel@tonic-gate  * Copyright (c) 1985, 1989, 1993
267c478bd9Sstevel@tonic-gate  *    The Regents of the University of California.  All rights reserved.
27*9525b14bSRao Shoaib  *
287c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
297c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
307c478bd9Sstevel@tonic-gate  * are met:
317c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
327c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
337c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
347c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
357c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
367c478bd9Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
377c478bd9Sstevel@tonic-gate  *    must display the following acknowledgement:
387c478bd9Sstevel@tonic-gate  * 	This product includes software developed by the University of
397c478bd9Sstevel@tonic-gate  * 	California, Berkeley and its contributors.
407c478bd9Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
417c478bd9Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
427c478bd9Sstevel@tonic-gate  *    without specific prior written permission.
43*9525b14bSRao Shoaib  *
447c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
457c478bd9Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
467c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
477c478bd9Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
487c478bd9Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
497c478bd9Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
507c478bd9Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
517c478bd9Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
527c478bd9Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
537c478bd9Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
547c478bd9Sstevel@tonic-gate  * SUCH DAMAGE.
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /*
587c478bd9Sstevel@tonic-gate  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
59*9525b14bSRao Shoaib  *
607c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
617c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
627c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies, and that
637c478bd9Sstevel@tonic-gate  * the name of Digital Equipment Corporation not be used in advertising or
647c478bd9Sstevel@tonic-gate  * publicity pertaining to distribution of the document or software without
657c478bd9Sstevel@tonic-gate  * specific, written prior permission.
66*9525b14bSRao Shoaib  *
677c478bd9Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
687c478bd9Sstevel@tonic-gate  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
697c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
707c478bd9Sstevel@tonic-gate  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
717c478bd9Sstevel@tonic-gate  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
727c478bd9Sstevel@tonic-gate  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
737c478bd9Sstevel@tonic-gate  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
747c478bd9Sstevel@tonic-gate  * SOFTWARE.
757c478bd9Sstevel@tonic-gate  */
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
787c478bd9Sstevel@tonic-gate static const char sccsid[] = "@(#)res_send.c	8.1 (Berkeley) 6/4/93";
79*9525b14bSRao Shoaib static const char rcsid[] = "$Id: res_send.c,v 1.22 2009/01/22 23:49:23 tbox Exp $";
807c478bd9Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
817c478bd9Sstevel@tonic-gate 
82*9525b14bSRao Shoaib /*! \file
83*9525b14bSRao Shoaib  * \brief
847c478bd9Sstevel@tonic-gate  * Send query to name server and wait for reply.
857c478bd9Sstevel@tonic-gate  */
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate #include "port_before.h"
887c478bd9Sstevel@tonic-gate #include "fd_setsize.h"
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate #include <sys/types.h>
917c478bd9Sstevel@tonic-gate #include <sys/param.h>
927c478bd9Sstevel@tonic-gate #include <sys/time.h>
937c478bd9Sstevel@tonic-gate #include <sys/socket.h>
947c478bd9Sstevel@tonic-gate #include <sys/uio.h>
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate #include <netinet/in.h>
977c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
987c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate #include <errno.h>
1017c478bd9Sstevel@tonic-gate #include <netdb.h>
1027c478bd9Sstevel@tonic-gate #include <resolv.h>
1037c478bd9Sstevel@tonic-gate #include <signal.h>
1047c478bd9Sstevel@tonic-gate #include <stdio.h>
1057c478bd9Sstevel@tonic-gate #include <stdlib.h>
1067c478bd9Sstevel@tonic-gate #include <string.h>
1077c478bd9Sstevel@tonic-gate #include <unistd.h>
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate #include <isc/eventlib.h>
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate #include "port_after.h"
1127c478bd9Sstevel@tonic-gate 
113*9525b14bSRao Shoaib #ifdef USE_POLL
114*9525b14bSRao Shoaib #ifdef HAVE_STROPTS_H
1157c478bd9Sstevel@tonic-gate #include <stropts.h>
116e8031f0aSraf #endif
117*9525b14bSRao Shoaib #include <poll.h>
118*9525b14bSRao Shoaib #endif /* USE_POLL */
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate /* Options.  Leave them on. */
1217c478bd9Sstevel@tonic-gate #define DEBUG
1227c478bd9Sstevel@tonic-gate #include "res_debug.h"
1237c478bd9Sstevel@tonic-gate #include "res_private.h"
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate #define EXT(res) ((res)->_u._ext)
1267c478bd9Sstevel@tonic-gate 
127*9525b14bSRao Shoaib #ifndef USE_POLL
1287c478bd9Sstevel@tonic-gate static const int highestFD = FD_SETSIZE - 1;
1297c478bd9Sstevel@tonic-gate #else
1307c478bd9Sstevel@tonic-gate static int highestFD = 0;
1317c478bd9Sstevel@tonic-gate #endif
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate /* Forward. */
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate static int		get_salen __P((const struct sockaddr *));
1367c478bd9Sstevel@tonic-gate static struct sockaddr * get_nsaddr __P((res_state, size_t));
1377c478bd9Sstevel@tonic-gate static int		send_vc(res_state, const u_char *, int,
1387c478bd9Sstevel@tonic-gate 				u_char *, int, int *, int);
1397c478bd9Sstevel@tonic-gate static int		send_dg(res_state, const u_char *, int,
140*9525b14bSRao Shoaib 				u_char *, int, int *, int, int,
1417c478bd9Sstevel@tonic-gate 				int *, int *);
1427c478bd9Sstevel@tonic-gate static void		Aerror(const res_state, FILE *, const char *, int,
1437c478bd9Sstevel@tonic-gate 			       const struct sockaddr *, int);
1447c478bd9Sstevel@tonic-gate static void		Perror(const res_state, FILE *, const char *, int);
1457c478bd9Sstevel@tonic-gate static int		sock_eq(struct sockaddr *, struct sockaddr *);
146*9525b14bSRao Shoaib #if defined(NEED_PSELECT) && !defined(USE_POLL)
1477c478bd9Sstevel@tonic-gate static int		pselect(int, void *, void *, void *,
1487c478bd9Sstevel@tonic-gate 				struct timespec *,
1497c478bd9Sstevel@tonic-gate 				const sigset_t *);
1507c478bd9Sstevel@tonic-gate #endif
1517c478bd9Sstevel@tonic-gate void res_pquery(const res_state, const u_char *, int, FILE *);
1527c478bd9Sstevel@tonic-gate 
153*9525b14bSRao Shoaib #ifndef	ORIGINAL_ISC_CODE
1547c478bd9Sstevel@tonic-gate #pragma weak	__res_nameinquery	=	res_nameinquery
1557c478bd9Sstevel@tonic-gate #pragma weak	__res_queriesmatch	=	res_queriesmatch
1567c478bd9Sstevel@tonic-gate #pragma weak	res_nisourserver	=	res_ourserver_p
1577c478bd9Sstevel@tonic-gate #endif	/* ORIGINAL_ISC_CODE */
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate static const int niflags = NI_NUMERICHOST | NI_NUMERICSERV;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate /* Public. */
1627c478bd9Sstevel@tonic-gate 
163*9525b14bSRao Shoaib /*%
1647c478bd9Sstevel@tonic-gate  *	looks up "ina" in _res.ns_addr_list[]
165*9525b14bSRao Shoaib  *
1667c478bd9Sstevel@tonic-gate  * returns:
167*9525b14bSRao Shoaib  *\li	0  : not found
168*9525b14bSRao Shoaib  *\li	>0 : found
169*9525b14bSRao Shoaib  *
1707c478bd9Sstevel@tonic-gate  * author:
171*9525b14bSRao Shoaib  *\li	paul vixie, 29may94
1727c478bd9Sstevel@tonic-gate  */
1737c478bd9Sstevel@tonic-gate int
1747c478bd9Sstevel@tonic-gate res_ourserver_p(const res_state statp, const struct sockaddr *sa) {
1757c478bd9Sstevel@tonic-gate 	const struct sockaddr_in *inp, *srv;
1767c478bd9Sstevel@tonic-gate 	const struct sockaddr_in6 *in6p, *srv6;
1777c478bd9Sstevel@tonic-gate 	int ns;
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	switch (sa->sa_family) {
1807c478bd9Sstevel@tonic-gate 	case AF_INET:
1817c478bd9Sstevel@tonic-gate 		inp = (const struct sockaddr_in *)sa;
1827c478bd9Sstevel@tonic-gate 		for (ns = 0;  ns < statp->nscount;  ns++) {
1837c478bd9Sstevel@tonic-gate 			srv = (struct sockaddr_in *)get_nsaddr(statp, ns);
1847c478bd9Sstevel@tonic-gate 			if (srv->sin_family == inp->sin_family &&
1857c478bd9Sstevel@tonic-gate 			    srv->sin_port == inp->sin_port &&
1867c478bd9Sstevel@tonic-gate 			    (srv->sin_addr.s_addr == INADDR_ANY ||
1877c478bd9Sstevel@tonic-gate 			     srv->sin_addr.s_addr == inp->sin_addr.s_addr))
1887c478bd9Sstevel@tonic-gate 				return (1);
1897c478bd9Sstevel@tonic-gate 		}
1907c478bd9Sstevel@tonic-gate 		break;
1917c478bd9Sstevel@tonic-gate 	case AF_INET6:
1927c478bd9Sstevel@tonic-gate 		if (EXT(statp).ext == NULL)
1937c478bd9Sstevel@tonic-gate 			break;
1947c478bd9Sstevel@tonic-gate 		in6p = (const struct sockaddr_in6 *)sa;
1957c478bd9Sstevel@tonic-gate 		for (ns = 0;  ns < statp->nscount;  ns++) {
1967c478bd9Sstevel@tonic-gate 			srv6 = (struct sockaddr_in6 *)get_nsaddr(statp, ns);
1977c478bd9Sstevel@tonic-gate 			if (srv6->sin6_family == in6p->sin6_family &&
1987c478bd9Sstevel@tonic-gate 			    srv6->sin6_port == in6p->sin6_port &&
1997c478bd9Sstevel@tonic-gate #ifdef HAVE_SIN6_SCOPE_ID
200*9525b14bSRao Shoaib 			    (srv6->sin6_scope_id == 0 ||
201*9525b14bSRao Shoaib 			     srv6->sin6_scope_id == in6p->sin6_scope_id) &&
2027c478bd9Sstevel@tonic-gate #endif
2037c478bd9Sstevel@tonic-gate 			    (IN6_IS_ADDR_UNSPECIFIED(&srv6->sin6_addr) ||
2047c478bd9Sstevel@tonic-gate 			     IN6_ARE_ADDR_EQUAL(&srv6->sin6_addr, &in6p->sin6_addr)))
2057c478bd9Sstevel@tonic-gate 				return (1);
2067c478bd9Sstevel@tonic-gate 		}
2077c478bd9Sstevel@tonic-gate 		break;
2087c478bd9Sstevel@tonic-gate 	default:
2097c478bd9Sstevel@tonic-gate 		break;
2107c478bd9Sstevel@tonic-gate 	}
2117c478bd9Sstevel@tonic-gate 	return (0);
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate 
214*9525b14bSRao Shoaib /*%
2157c478bd9Sstevel@tonic-gate  *	look for (name,type,class) in the query section of packet (buf,eom)
216*9525b14bSRao Shoaib  *
2177c478bd9Sstevel@tonic-gate  * requires:
218*9525b14bSRao Shoaib  *\li	buf + HFIXEDSZ <= eom
219*9525b14bSRao Shoaib  *
2207c478bd9Sstevel@tonic-gate  * returns:
221*9525b14bSRao Shoaib  *\li	-1 : format error
222*9525b14bSRao Shoaib  *\li	0  : not found
223*9525b14bSRao Shoaib  *\li	>0 : found
224*9525b14bSRao Shoaib  *
2257c478bd9Sstevel@tonic-gate  * author:
226*9525b14bSRao Shoaib  *\li	paul vixie, 29may94
2277c478bd9Sstevel@tonic-gate  */
2287c478bd9Sstevel@tonic-gate int
2297c478bd9Sstevel@tonic-gate res_nameinquery(const char *name, int type, int class,
2307c478bd9Sstevel@tonic-gate 		const u_char *buf, const u_char *eom)
2317c478bd9Sstevel@tonic-gate {
2327c478bd9Sstevel@tonic-gate 	const u_char *cp = buf + HFIXEDSZ;
2337c478bd9Sstevel@tonic-gate 	int qdcount = ntohs(((const HEADER*)buf)->qdcount);
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	while (qdcount-- > 0) {
2367c478bd9Sstevel@tonic-gate 		char tname[MAXDNAME+1];
2377c478bd9Sstevel@tonic-gate 		int n, ttype, tclass;
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 		n = dn_expand(buf, eom, cp, tname, sizeof tname);
2407c478bd9Sstevel@tonic-gate 		if (n < 0)
2417c478bd9Sstevel@tonic-gate 			return (-1);
2427c478bd9Sstevel@tonic-gate 		cp += n;
2437c478bd9Sstevel@tonic-gate 		if (cp + 2 * INT16SZ > eom)
2447c478bd9Sstevel@tonic-gate 			return (-1);
2457c478bd9Sstevel@tonic-gate 		ttype = ns_get16(cp); cp += INT16SZ;
2467c478bd9Sstevel@tonic-gate 		tclass = ns_get16(cp); cp += INT16SZ;
2477c478bd9Sstevel@tonic-gate 		if (ttype == type && tclass == class &&
2487c478bd9Sstevel@tonic-gate 		    ns_samename(tname, name) == 1)
2497c478bd9Sstevel@tonic-gate 			return (1);
2507c478bd9Sstevel@tonic-gate 	}
2517c478bd9Sstevel@tonic-gate 	return (0);
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate 
254*9525b14bSRao Shoaib /*%
2557c478bd9Sstevel@tonic-gate  *	is there a 1:1 mapping of (name,type,class)
2567c478bd9Sstevel@tonic-gate  *	in (buf1,eom1) and (buf2,eom2)?
257*9525b14bSRao Shoaib  *
2587c478bd9Sstevel@tonic-gate  * returns:
259*9525b14bSRao Shoaib  *\li	-1 : format error
260*9525b14bSRao Shoaib  *\li	0  : not a 1:1 mapping
261*9525b14bSRao Shoaib  *\li	>0 : is a 1:1 mapping
262*9525b14bSRao Shoaib  *
2637c478bd9Sstevel@tonic-gate  * author:
264*9525b14bSRao Shoaib  *\li	paul vixie, 29may94
2657c478bd9Sstevel@tonic-gate  */
2667c478bd9Sstevel@tonic-gate int
2677c478bd9Sstevel@tonic-gate res_queriesmatch(const u_char *buf1, const u_char *eom1,
2687c478bd9Sstevel@tonic-gate 		 const u_char *buf2, const u_char *eom2)
2697c478bd9Sstevel@tonic-gate {
2707c478bd9Sstevel@tonic-gate 	const u_char *cp = buf1 + HFIXEDSZ;
2717c478bd9Sstevel@tonic-gate 	int qdcount = ntohs(((const HEADER*)buf1)->qdcount);
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	if (buf1 + HFIXEDSZ > eom1 || buf2 + HFIXEDSZ > eom2)
2747c478bd9Sstevel@tonic-gate 		return (-1);
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	/*
2777c478bd9Sstevel@tonic-gate 	 * Only header section present in replies to
2787c478bd9Sstevel@tonic-gate 	 * dynamic update packets.
2797c478bd9Sstevel@tonic-gate 	 */
2807c478bd9Sstevel@tonic-gate 	if ((((const HEADER *)buf1)->opcode == ns_o_update) &&
2817c478bd9Sstevel@tonic-gate 	    (((const HEADER *)buf2)->opcode == ns_o_update))
2827c478bd9Sstevel@tonic-gate 		return (1);
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	if (qdcount != ntohs(((const HEADER*)buf2)->qdcount))
2857c478bd9Sstevel@tonic-gate 		return (0);
2867c478bd9Sstevel@tonic-gate 	while (qdcount-- > 0) {
2877c478bd9Sstevel@tonic-gate 		char tname[MAXDNAME+1];
2887c478bd9Sstevel@tonic-gate 		int n, ttype, tclass;
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 		n = dn_expand(buf1, eom1, cp, tname, sizeof tname);
2917c478bd9Sstevel@tonic-gate 		if (n < 0)
2927c478bd9Sstevel@tonic-gate 			return (-1);
2937c478bd9Sstevel@tonic-gate 		cp += n;
2947c478bd9Sstevel@tonic-gate 		if (cp + 2 * INT16SZ > eom1)
2957c478bd9Sstevel@tonic-gate 			return (-1);
2967c478bd9Sstevel@tonic-gate 		ttype = ns_get16(cp);	cp += INT16SZ;
2977c478bd9Sstevel@tonic-gate 		tclass = ns_get16(cp); cp += INT16SZ;
2987c478bd9Sstevel@tonic-gate 		if (!res_nameinquery(tname, ttype, tclass, buf2, eom2))
2997c478bd9Sstevel@tonic-gate 			return (0);
3007c478bd9Sstevel@tonic-gate 	}
3017c478bd9Sstevel@tonic-gate 	return (1);
3027c478bd9Sstevel@tonic-gate }
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate int
3057c478bd9Sstevel@tonic-gate res_nsend(res_state statp,
3067c478bd9Sstevel@tonic-gate 	  const u_char *buf, int buflen, u_char *ans, int anssiz)
3077c478bd9Sstevel@tonic-gate {
308*9525b14bSRao Shoaib 	int gotsomewhere, terrno, tries, v_circuit, resplen, ns, n;
3097c478bd9Sstevel@tonic-gate 	char abuf[NI_MAXHOST];
3107c478bd9Sstevel@tonic-gate 
311*9525b14bSRao Shoaib #ifdef USE_POLL
3127c478bd9Sstevel@tonic-gate 	highestFD = sysconf(_SC_OPEN_MAX) - 1;
3137c478bd9Sstevel@tonic-gate #endif
3147c478bd9Sstevel@tonic-gate 
315*9525b14bSRao Shoaib 	/* No name servers or res_init() failure */
316c16fc660Ssm 	if (statp->nscount == 0 || EXT(statp).ext == NULL) {
3177c478bd9Sstevel@tonic-gate 		errno = ESRCH;
3187c478bd9Sstevel@tonic-gate 		return (-1);
3197c478bd9Sstevel@tonic-gate 	}
3207c478bd9Sstevel@tonic-gate 	if (anssiz < HFIXEDSZ) {
3217c478bd9Sstevel@tonic-gate 		errno = EINVAL;
3227c478bd9Sstevel@tonic-gate 		return (-1);
3237c478bd9Sstevel@tonic-gate 	}
3247c478bd9Sstevel@tonic-gate 	DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_QUERY),
3257c478bd9Sstevel@tonic-gate 		(stdout, ";; res_send()\n"), buf, buflen);
3267c478bd9Sstevel@tonic-gate 	v_circuit = (statp->options & RES_USEVC) || buflen > PACKETSZ;
3277c478bd9Sstevel@tonic-gate 	gotsomewhere = 0;
3287c478bd9Sstevel@tonic-gate 	terrno = ETIMEDOUT;
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	/*
3317c478bd9Sstevel@tonic-gate 	 * If the ns_addr_list in the resolver context has changed, then
3327c478bd9Sstevel@tonic-gate 	 * invalidate our cached copy and the associated timing data.
3337c478bd9Sstevel@tonic-gate 	 */
3347c478bd9Sstevel@tonic-gate 	if (EXT(statp).nscount != 0) {
3357c478bd9Sstevel@tonic-gate 		int needclose = 0;
3367c478bd9Sstevel@tonic-gate 		struct sockaddr_storage peer;
3377c478bd9Sstevel@tonic-gate 		ISC_SOCKLEN_T peerlen;
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 		if (EXT(statp).nscount != statp->nscount)
3407c478bd9Sstevel@tonic-gate 			needclose++;
3417c478bd9Sstevel@tonic-gate 		else
3427c478bd9Sstevel@tonic-gate 			for (ns = 0; ns < statp->nscount; ns++) {
3437c478bd9Sstevel@tonic-gate 				if (statp->nsaddr_list[ns].sin_family &&
3447c478bd9Sstevel@tonic-gate 				    !sock_eq((struct sockaddr *)&statp->nsaddr_list[ns],
3457c478bd9Sstevel@tonic-gate 					     (struct sockaddr *)&EXT(statp).ext->nsaddrs[ns])) {
3467c478bd9Sstevel@tonic-gate 					needclose++;
3477c478bd9Sstevel@tonic-gate 					break;
3487c478bd9Sstevel@tonic-gate 				}
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 				if (EXT(statp).nssocks[ns] == -1)
3517c478bd9Sstevel@tonic-gate 					continue;
3527c478bd9Sstevel@tonic-gate 				peerlen = sizeof(peer);
353*9525b14bSRao Shoaib 				if (getpeername(EXT(statp).nssocks[ns],
3547c478bd9Sstevel@tonic-gate 				    (struct sockaddr *)&peer, &peerlen) < 0) {
3557c478bd9Sstevel@tonic-gate 					needclose++;
3567c478bd9Sstevel@tonic-gate 					break;
3577c478bd9Sstevel@tonic-gate 				}
3587c478bd9Sstevel@tonic-gate 				if (!sock_eq((struct sockaddr *)&peer,
3597c478bd9Sstevel@tonic-gate 				    get_nsaddr(statp, ns))) {
3607c478bd9Sstevel@tonic-gate 					needclose++;
3617c478bd9Sstevel@tonic-gate 					break;
3627c478bd9Sstevel@tonic-gate 				}
3637c478bd9Sstevel@tonic-gate 			}
3647c478bd9Sstevel@tonic-gate 		if (needclose) {
3657c478bd9Sstevel@tonic-gate 			res_nclose(statp);
3667c478bd9Sstevel@tonic-gate 			EXT(statp).nscount = 0;
3677c478bd9Sstevel@tonic-gate 		}
3687c478bd9Sstevel@tonic-gate 	}
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	/*
3717c478bd9Sstevel@tonic-gate 	 * Maybe initialize our private copy of the ns_addr_list.
3727c478bd9Sstevel@tonic-gate 	 */
3737c478bd9Sstevel@tonic-gate 	if (EXT(statp).nscount == 0) {
3747c478bd9Sstevel@tonic-gate 		for (ns = 0; ns < statp->nscount; ns++) {
3757c478bd9Sstevel@tonic-gate 			EXT(statp).nstimes[ns] = RES_MAXTIME;
3767c478bd9Sstevel@tonic-gate 			EXT(statp).nssocks[ns] = -1;
3777c478bd9Sstevel@tonic-gate 			if (!statp->nsaddr_list[ns].sin_family)
3787c478bd9Sstevel@tonic-gate 				continue;
3797c478bd9Sstevel@tonic-gate 			EXT(statp).ext->nsaddrs[ns].sin =
3807c478bd9Sstevel@tonic-gate 				 statp->nsaddr_list[ns];
3817c478bd9Sstevel@tonic-gate 		}
3827c478bd9Sstevel@tonic-gate 		EXT(statp).nscount = statp->nscount;
3837c478bd9Sstevel@tonic-gate 	}
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	/*
3867c478bd9Sstevel@tonic-gate 	 * Some resolvers want to even out the load on their nameservers.
3877c478bd9Sstevel@tonic-gate 	 * Note that RES_BLAST overrides RES_ROTATE.
3887c478bd9Sstevel@tonic-gate 	 */
389*9525b14bSRao Shoaib 	if ((statp->options & RES_ROTATE) != 0U &&
390*9525b14bSRao Shoaib 	    (statp->options & RES_BLAST) == 0U) {
3917c478bd9Sstevel@tonic-gate 		union res_sockaddr_union inu;
3927c478bd9Sstevel@tonic-gate 		struct sockaddr_in ina;
3937c478bd9Sstevel@tonic-gate 		int lastns = statp->nscount - 1;
3947c478bd9Sstevel@tonic-gate 		int fd;
3957c478bd9Sstevel@tonic-gate 		u_int16_t nstime;
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 		if (EXT(statp).ext != NULL)
3987c478bd9Sstevel@tonic-gate 			inu = EXT(statp).ext->nsaddrs[0];
3997c478bd9Sstevel@tonic-gate 		ina = statp->nsaddr_list[0];
4007c478bd9Sstevel@tonic-gate 		fd = EXT(statp).nssocks[0];
4017c478bd9Sstevel@tonic-gate 		nstime = EXT(statp).nstimes[0];
4027c478bd9Sstevel@tonic-gate 		for (ns = 0; ns < lastns; ns++) {
4037c478bd9Sstevel@tonic-gate 			if (EXT(statp).ext != NULL)
404*9525b14bSRao Shoaib 				EXT(statp).ext->nsaddrs[ns] =
4057c478bd9Sstevel@tonic-gate 					EXT(statp).ext->nsaddrs[ns + 1];
4067c478bd9Sstevel@tonic-gate 			statp->nsaddr_list[ns] = statp->nsaddr_list[ns + 1];
4077c478bd9Sstevel@tonic-gate 			EXT(statp).nssocks[ns] = EXT(statp).nssocks[ns + 1];
4087c478bd9Sstevel@tonic-gate 			EXT(statp).nstimes[ns] = EXT(statp).nstimes[ns + 1];
4097c478bd9Sstevel@tonic-gate 		}
4107c478bd9Sstevel@tonic-gate 		if (EXT(statp).ext != NULL)
4117c478bd9Sstevel@tonic-gate 			EXT(statp).ext->nsaddrs[lastns] = inu;
4127c478bd9Sstevel@tonic-gate 		statp->nsaddr_list[lastns] = ina;
4137c478bd9Sstevel@tonic-gate 		EXT(statp).nssocks[lastns] = fd;
4147c478bd9Sstevel@tonic-gate 		EXT(statp).nstimes[lastns] = nstime;
4157c478bd9Sstevel@tonic-gate 	}
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	/*
4187c478bd9Sstevel@tonic-gate 	 * Send request, RETRY times, or until successful.
4197c478bd9Sstevel@tonic-gate 	 */
420*9525b14bSRao Shoaib 	for (tries = 0; tries < statp->retry; tries++) {
4217c478bd9Sstevel@tonic-gate 	    for (ns = 0; ns < statp->nscount; ns++) {
4227c478bd9Sstevel@tonic-gate 		struct sockaddr *nsap;
4237c478bd9Sstevel@tonic-gate 		int nsaplen;
4247c478bd9Sstevel@tonic-gate 		nsap = get_nsaddr(statp, ns);
4257c478bd9Sstevel@tonic-gate 		nsaplen = get_salen(nsap);
4267c478bd9Sstevel@tonic-gate 		statp->_flags &= ~RES_F_LASTMASK;
4277c478bd9Sstevel@tonic-gate 		statp->_flags |= (ns << RES_F_LASTSHIFT);
4287c478bd9Sstevel@tonic-gate  same_ns:
4297c478bd9Sstevel@tonic-gate 		if (statp->qhook) {
4307c478bd9Sstevel@tonic-gate 			int done = 0, loops = 0;
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 			do {
4337c478bd9Sstevel@tonic-gate 				res_sendhookact act;
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 				act = (*statp->qhook)(&nsap, &buf, &buflen,
4367c478bd9Sstevel@tonic-gate 						      ans, anssiz, &resplen);
4377c478bd9Sstevel@tonic-gate 				switch (act) {
4387c478bd9Sstevel@tonic-gate 				case res_goahead:
4397c478bd9Sstevel@tonic-gate 					done = 1;
4407c478bd9Sstevel@tonic-gate 					break;
4417c478bd9Sstevel@tonic-gate 				case res_nextns:
4427c478bd9Sstevel@tonic-gate 					res_nclose(statp);
4437c478bd9Sstevel@tonic-gate 					goto next_ns;
4447c478bd9Sstevel@tonic-gate 				case res_done:
4457c478bd9Sstevel@tonic-gate 					return (resplen);
4467c478bd9Sstevel@tonic-gate 				case res_modified:
4477c478bd9Sstevel@tonic-gate 					/* give the hook another try */
4487c478bd9Sstevel@tonic-gate 					if (++loops < 42) /*doug adams*/
4497c478bd9Sstevel@tonic-gate 						break;
4507c478bd9Sstevel@tonic-gate 					/*FALLTHROUGH*/
4517c478bd9Sstevel@tonic-gate 				case res_error:
4527c478bd9Sstevel@tonic-gate 					/*FALLTHROUGH*/
4537c478bd9Sstevel@tonic-gate 				default:
4547c478bd9Sstevel@tonic-gate 					goto fail;
4557c478bd9Sstevel@tonic-gate 				}
4567c478bd9Sstevel@tonic-gate 			} while (!done);
4577c478bd9Sstevel@tonic-gate 		}
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 		Dprint(((statp->options & RES_DEBUG) &&
4607c478bd9Sstevel@tonic-gate 			getnameinfo(nsap, nsaplen, abuf, sizeof(abuf),
4617c478bd9Sstevel@tonic-gate 				    NULL, 0, niflags) == 0),
4627c478bd9Sstevel@tonic-gate 		       (stdout, ";; Querying server (# %d) address = %s\n",
4637c478bd9Sstevel@tonic-gate 			ns + 1, abuf));
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 		if (v_circuit) {
4677c478bd9Sstevel@tonic-gate 			/* Use VC; at most one attempt per server. */
468*9525b14bSRao Shoaib 			tries = statp->retry;
4697c478bd9Sstevel@tonic-gate 			n = send_vc(statp, buf, buflen, ans, anssiz, &terrno,
4707c478bd9Sstevel@tonic-gate 				    ns);
4717c478bd9Sstevel@tonic-gate 			if (n < 0)
4727c478bd9Sstevel@tonic-gate 				goto fail;
4737c478bd9Sstevel@tonic-gate 			if (n == 0)
4747c478bd9Sstevel@tonic-gate 				goto next_ns;
4757c478bd9Sstevel@tonic-gate 			resplen = n;
4767c478bd9Sstevel@tonic-gate 		} else {
4777c478bd9Sstevel@tonic-gate 			/* Use datagrams. */
4787c478bd9Sstevel@tonic-gate 			n = send_dg(statp, buf, buflen, ans, anssiz, &terrno,
479*9525b14bSRao Shoaib 				    ns, tries, &v_circuit, &gotsomewhere);
4807c478bd9Sstevel@tonic-gate 			if (n < 0)
4817c478bd9Sstevel@tonic-gate 				goto fail;
4827c478bd9Sstevel@tonic-gate 			if (n == 0)
4837c478bd9Sstevel@tonic-gate 				goto next_ns;
4847c478bd9Sstevel@tonic-gate 			if (v_circuit)
4857c478bd9Sstevel@tonic-gate 				goto same_ns;
4867c478bd9Sstevel@tonic-gate 			resplen = n;
4877c478bd9Sstevel@tonic-gate 		}
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 		Dprint((statp->options & RES_DEBUG) ||
4907c478bd9Sstevel@tonic-gate 		       ((statp->pfcode & RES_PRF_REPLY) &&
4917c478bd9Sstevel@tonic-gate 			(statp->pfcode & RES_PRF_HEAD1)),
4927c478bd9Sstevel@tonic-gate 		       (stdout, ";; got answer:\n"));
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 		DprintQ((statp->options & RES_DEBUG) ||
4957c478bd9Sstevel@tonic-gate 			(statp->pfcode & RES_PRF_REPLY),
4967c478bd9Sstevel@tonic-gate 			(stdout, "%s", ""),
4977c478bd9Sstevel@tonic-gate 			ans, (resplen > anssiz) ? anssiz : resplen);
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 		/*
5007c478bd9Sstevel@tonic-gate 		 * If we have temporarily opened a virtual circuit,
5017c478bd9Sstevel@tonic-gate 		 * or if we haven't been asked to keep a socket open,
5027c478bd9Sstevel@tonic-gate 		 * close the socket.
5037c478bd9Sstevel@tonic-gate 		 */
504*9525b14bSRao Shoaib 		if ((v_circuit && (statp->options & RES_USEVC) == 0U) ||
505*9525b14bSRao Shoaib 		    (statp->options & RES_STAYOPEN) == 0U) {
5067c478bd9Sstevel@tonic-gate 			res_nclose(statp);
5077c478bd9Sstevel@tonic-gate 		}
5087c478bd9Sstevel@tonic-gate 		if (statp->rhook) {
5097c478bd9Sstevel@tonic-gate 			int done = 0, loops = 0;
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 			do {
5127c478bd9Sstevel@tonic-gate 				res_sendhookact act;
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 				act = (*statp->rhook)(nsap, buf, buflen,
5157c478bd9Sstevel@tonic-gate 						      ans, anssiz, &resplen);
5167c478bd9Sstevel@tonic-gate 				switch (act) {
5177c478bd9Sstevel@tonic-gate 				case res_goahead:
5187c478bd9Sstevel@tonic-gate 				case res_done:
5197c478bd9Sstevel@tonic-gate 					done = 1;
5207c478bd9Sstevel@tonic-gate 					break;
5217c478bd9Sstevel@tonic-gate 				case res_nextns:
5227c478bd9Sstevel@tonic-gate 					res_nclose(statp);
5237c478bd9Sstevel@tonic-gate 					goto next_ns;
5247c478bd9Sstevel@tonic-gate 				case res_modified:
5257c478bd9Sstevel@tonic-gate 					/* give the hook another try */
5267c478bd9Sstevel@tonic-gate 					if (++loops < 42) /*doug adams*/
5277c478bd9Sstevel@tonic-gate 						break;
5287c478bd9Sstevel@tonic-gate 					/*FALLTHROUGH*/
5297c478bd9Sstevel@tonic-gate 				case res_error:
5307c478bd9Sstevel@tonic-gate 					/*FALLTHROUGH*/
5317c478bd9Sstevel@tonic-gate 				default:
5327c478bd9Sstevel@tonic-gate 					goto fail;
5337c478bd9Sstevel@tonic-gate 				}
5347c478bd9Sstevel@tonic-gate 			} while (!done);
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 		}
5377c478bd9Sstevel@tonic-gate 		return (resplen);
5387c478bd9Sstevel@tonic-gate  next_ns: ;
5397c478bd9Sstevel@tonic-gate 	   } /*foreach ns*/
5407c478bd9Sstevel@tonic-gate 	} /*foreach retry*/
5417c478bd9Sstevel@tonic-gate 	res_nclose(statp);
5427c478bd9Sstevel@tonic-gate 	if (!v_circuit) {
5437c478bd9Sstevel@tonic-gate 		if (!gotsomewhere)
544*9525b14bSRao Shoaib 			errno = ECONNREFUSED;	/*%< no nameservers found */
5457c478bd9Sstevel@tonic-gate 		else
546*9525b14bSRao Shoaib 			errno = ETIMEDOUT;	/*%< no answer obtained */
5477c478bd9Sstevel@tonic-gate 	} else
5487c478bd9Sstevel@tonic-gate 		errno = terrno;
5497c478bd9Sstevel@tonic-gate 	return (-1);
5507c478bd9Sstevel@tonic-gate  fail:
5517c478bd9Sstevel@tonic-gate 	res_nclose(statp);
5527c478bd9Sstevel@tonic-gate 	return (-1);
5537c478bd9Sstevel@tonic-gate }
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate /* Private */
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate static int
5587c478bd9Sstevel@tonic-gate get_salen(sa)
5597c478bd9Sstevel@tonic-gate 	const struct sockaddr *sa;
5607c478bd9Sstevel@tonic-gate {
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate #ifdef HAVE_SA_LEN
5637c478bd9Sstevel@tonic-gate 	/* There are people do not set sa_len.  Be forgiving to them. */
5647c478bd9Sstevel@tonic-gate 	if (sa->sa_len)
5657c478bd9Sstevel@tonic-gate 		return (sa->sa_len);
5667c478bd9Sstevel@tonic-gate #endif
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	if (sa->sa_family == AF_INET)
5697c478bd9Sstevel@tonic-gate 		return (sizeof(struct sockaddr_in));
5707c478bd9Sstevel@tonic-gate 	else if (sa->sa_family == AF_INET6)
5717c478bd9Sstevel@tonic-gate 		return (sizeof(struct sockaddr_in6));
5727c478bd9Sstevel@tonic-gate 	else
573*9525b14bSRao Shoaib 		return (0);	/*%< unknown, die on connect */
5747c478bd9Sstevel@tonic-gate }
5757c478bd9Sstevel@tonic-gate 
576*9525b14bSRao Shoaib /*%
5777c478bd9Sstevel@tonic-gate  * pick appropriate nsaddr_list for use.  see res_init() for initialization.
5787c478bd9Sstevel@tonic-gate  */
5797c478bd9Sstevel@tonic-gate static struct sockaddr *
5807c478bd9Sstevel@tonic-gate get_nsaddr(statp, n)
5817c478bd9Sstevel@tonic-gate 	res_state statp;
5827c478bd9Sstevel@tonic-gate 	size_t n;
5837c478bd9Sstevel@tonic-gate {
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	if (!statp->nsaddr_list[n].sin_family && EXT(statp).ext) {
5867c478bd9Sstevel@tonic-gate 		/*
5877c478bd9Sstevel@tonic-gate 		 * - EXT(statp).ext->nsaddrs[n] holds an address that is larger
5887c478bd9Sstevel@tonic-gate 		 *   than struct sockaddr, and
5897c478bd9Sstevel@tonic-gate 		 * - user code did not update statp->nsaddr_list[n].
5907c478bd9Sstevel@tonic-gate 		 */
5917c478bd9Sstevel@tonic-gate 		return (struct sockaddr *)(void *)&EXT(statp).ext->nsaddrs[n];
5927c478bd9Sstevel@tonic-gate 	} else {
5937c478bd9Sstevel@tonic-gate 		/*
5947c478bd9Sstevel@tonic-gate 		 * - user code updated statp->nsaddr_list[n], or
5957c478bd9Sstevel@tonic-gate 		 * - statp->nsaddr_list[n] has the same content as
5967c478bd9Sstevel@tonic-gate 		 *   EXT(statp).ext->nsaddrs[n].
5977c478bd9Sstevel@tonic-gate 		 */
5987c478bd9Sstevel@tonic-gate 		return (struct sockaddr *)(void *)&statp->nsaddr_list[n];
5997c478bd9Sstevel@tonic-gate 	}
6007c478bd9Sstevel@tonic-gate }
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate static int
6037c478bd9Sstevel@tonic-gate send_vc(res_state statp,
6047c478bd9Sstevel@tonic-gate 	const u_char *buf, int buflen, u_char *ans, int anssiz,
6057c478bd9Sstevel@tonic-gate 	int *terrno, int ns)
6067c478bd9Sstevel@tonic-gate {
6077c478bd9Sstevel@tonic-gate 	const HEADER *hp = (const HEADER *) buf;
6087c478bd9Sstevel@tonic-gate 	HEADER *anhp = (HEADER *) ans;
6097c478bd9Sstevel@tonic-gate 	struct sockaddr *nsap;
6107c478bd9Sstevel@tonic-gate 	int nsaplen;
6117c478bd9Sstevel@tonic-gate 	int truncating, connreset, resplen, n;
6127c478bd9Sstevel@tonic-gate 	struct iovec iov[2];
6137c478bd9Sstevel@tonic-gate 	u_short len;
6147c478bd9Sstevel@tonic-gate 	u_char *cp;
6157c478bd9Sstevel@tonic-gate 	void *tmp;
616*9525b14bSRao Shoaib #ifdef SO_NOSIGPIPE
617*9525b14bSRao Shoaib 	int on = 1;
618*9525b14bSRao Shoaib #endif
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	nsap = get_nsaddr(statp, ns);
6217c478bd9Sstevel@tonic-gate 	nsaplen = get_salen(nsap);
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	connreset = 0;
6247c478bd9Sstevel@tonic-gate  same_ns:
6257c478bd9Sstevel@tonic-gate 	truncating = 0;
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	/* Are we still talking to whom we want to talk to? */
6287c478bd9Sstevel@tonic-gate 	if (statp->_vcsock >= 0 && (statp->_flags & RES_F_VC) != 0) {
6297c478bd9Sstevel@tonic-gate 		struct sockaddr_storage peer;
6307c478bd9Sstevel@tonic-gate 		ISC_SOCKLEN_T size = sizeof peer;
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 		if (getpeername(statp->_vcsock,
6337c478bd9Sstevel@tonic-gate 				(struct sockaddr *)&peer, &size) < 0 ||
6347c478bd9Sstevel@tonic-gate 		    !sock_eq((struct sockaddr *)&peer, nsap)) {
6357c478bd9Sstevel@tonic-gate 			res_nclose(statp);
6367c478bd9Sstevel@tonic-gate 			statp->_flags &= ~RES_F_VC;
6377c478bd9Sstevel@tonic-gate 		}
6387c478bd9Sstevel@tonic-gate 	}
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	if (statp->_vcsock < 0 || (statp->_flags & RES_F_VC) == 0) {
6417c478bd9Sstevel@tonic-gate 		if (statp->_vcsock >= 0)
6427c478bd9Sstevel@tonic-gate 			res_nclose(statp);
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 		statp->_vcsock = socket(nsap->sa_family, SOCK_STREAM, 0);
6457c478bd9Sstevel@tonic-gate 		if (statp->_vcsock > highestFD) {
6467c478bd9Sstevel@tonic-gate 			res_nclose(statp);
6477c478bd9Sstevel@tonic-gate 			errno = ENOTSOCK;
6487c478bd9Sstevel@tonic-gate 		}
6497c478bd9Sstevel@tonic-gate 		if (statp->_vcsock < 0) {
650*9525b14bSRao Shoaib 			switch (errno) {
651*9525b14bSRao Shoaib 			case EPROTONOSUPPORT:
652*9525b14bSRao Shoaib #ifdef EPFNOSUPPORT
653*9525b14bSRao Shoaib 			case EPFNOSUPPORT:
654*9525b14bSRao Shoaib #endif
655*9525b14bSRao Shoaib 			case EAFNOSUPPORT:
656*9525b14bSRao Shoaib 				Perror(statp, stderr, "socket(vc)", errno);
657*9525b14bSRao Shoaib 				return (0);
658*9525b14bSRao Shoaib 			default:
659*9525b14bSRao Shoaib 				*terrno = errno;
660*9525b14bSRao Shoaib 				Perror(statp, stderr, "socket(vc)", errno);
661*9525b14bSRao Shoaib 				return (-1);
662*9525b14bSRao Shoaib 			}
6637c478bd9Sstevel@tonic-gate 		}
664*9525b14bSRao Shoaib #ifdef SO_NOSIGPIPE
665*9525b14bSRao Shoaib 		/*
666*9525b14bSRao Shoaib 		 * Disable generation of SIGPIPE when writing to a closed
667*9525b14bSRao Shoaib 		 * socket.  Write should return -1 and set errno to EPIPE
668*9525b14bSRao Shoaib 		 * instead.
669*9525b14bSRao Shoaib 		 *
670*9525b14bSRao Shoaib 		 * Push on even if setsockopt(SO_NOSIGPIPE) fails.
671*9525b14bSRao Shoaib 		 */
672*9525b14bSRao Shoaib 		(void)setsockopt(statp->_vcsock, SOL_SOCKET, SO_NOSIGPIPE, &on,
673*9525b14bSRao Shoaib 				 sizeof(on));
674*9525b14bSRao Shoaib #endif
6757c478bd9Sstevel@tonic-gate 		errno = 0;
6767c478bd9Sstevel@tonic-gate 		if (connect(statp->_vcsock, nsap, nsaplen) < 0) {
6777c478bd9Sstevel@tonic-gate 			*terrno = errno;
6787c478bd9Sstevel@tonic-gate 			Aerror(statp, stderr, "connect/vc", errno, nsap,
6797c478bd9Sstevel@tonic-gate 			    nsaplen);
6807c478bd9Sstevel@tonic-gate 			res_nclose(statp);
6817c478bd9Sstevel@tonic-gate 			return (0);
6827c478bd9Sstevel@tonic-gate 		}
6837c478bd9Sstevel@tonic-gate 		statp->_flags |= RES_F_VC;
6847c478bd9Sstevel@tonic-gate 	}
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	/*
6877c478bd9Sstevel@tonic-gate 	 * Send length & message
6887c478bd9Sstevel@tonic-gate 	 */
6897c478bd9Sstevel@tonic-gate 	ns_put16((u_short)buflen, (u_char*)&len);
6907c478bd9Sstevel@tonic-gate 	iov[0] = evConsIovec(&len, INT16SZ);
6917c478bd9Sstevel@tonic-gate 	DE_CONST(buf, tmp);
6927c478bd9Sstevel@tonic-gate 	iov[1] = evConsIovec(tmp, buflen);
6937c478bd9Sstevel@tonic-gate 	if (writev(statp->_vcsock, iov, 2) != (INT16SZ + buflen)) {
6947c478bd9Sstevel@tonic-gate 		*terrno = errno;
6957c478bd9Sstevel@tonic-gate 		Perror(statp, stderr, "write failed", errno);
6967c478bd9Sstevel@tonic-gate 		res_nclose(statp);
6977c478bd9Sstevel@tonic-gate 		return (0);
6987c478bd9Sstevel@tonic-gate 	}
6997c478bd9Sstevel@tonic-gate 	/*
7007c478bd9Sstevel@tonic-gate 	 * Receive length & response
7017c478bd9Sstevel@tonic-gate 	 */
7027c478bd9Sstevel@tonic-gate  read_len:
7037c478bd9Sstevel@tonic-gate 	cp = ans;
7047c478bd9Sstevel@tonic-gate 	len = INT16SZ;
7057c478bd9Sstevel@tonic-gate 	while ((n = read(statp->_vcsock, (char *)cp, (int)len)) > 0) {
7067c478bd9Sstevel@tonic-gate 		cp += n;
707*9525b14bSRao Shoaib 		if ((len -= n) == 0)
7087c478bd9Sstevel@tonic-gate 			break;
7097c478bd9Sstevel@tonic-gate 	}
7107c478bd9Sstevel@tonic-gate 	if (n <= 0) {
7117c478bd9Sstevel@tonic-gate 		*terrno = errno;
7127c478bd9Sstevel@tonic-gate 		Perror(statp, stderr, "read failed", errno);
7137c478bd9Sstevel@tonic-gate 		res_nclose(statp);
7147c478bd9Sstevel@tonic-gate 		/*
7157c478bd9Sstevel@tonic-gate 		 * A long running process might get its TCP
7167c478bd9Sstevel@tonic-gate 		 * connection reset if the remote server was
7177c478bd9Sstevel@tonic-gate 		 * restarted.  Requery the server instead of
7187c478bd9Sstevel@tonic-gate 		 * trying a new one.  When there is only one
7197c478bd9Sstevel@tonic-gate 		 * server, this means that a query might work
7207c478bd9Sstevel@tonic-gate 		 * instead of failing.  We only allow one reset
7217c478bd9Sstevel@tonic-gate 		 * per query to prevent looping.
7227c478bd9Sstevel@tonic-gate 		 */
7237c478bd9Sstevel@tonic-gate 		if (*terrno == ECONNRESET && !connreset) {
7247c478bd9Sstevel@tonic-gate 			connreset = 1;
7257c478bd9Sstevel@tonic-gate 			res_nclose(statp);
7267c478bd9Sstevel@tonic-gate 			goto same_ns;
7277c478bd9Sstevel@tonic-gate 		}
7287c478bd9Sstevel@tonic-gate 		res_nclose(statp);
7297c478bd9Sstevel@tonic-gate 		return (0);
7307c478bd9Sstevel@tonic-gate 	}
7317c478bd9Sstevel@tonic-gate 	resplen = ns_get16(ans);
7327c478bd9Sstevel@tonic-gate 	if (resplen > anssiz) {
7337c478bd9Sstevel@tonic-gate 		Dprint(statp->options & RES_DEBUG,
7347c478bd9Sstevel@tonic-gate 		       (stdout, ";; response truncated\n")
7357c478bd9Sstevel@tonic-gate 		       );
7367c478bd9Sstevel@tonic-gate 		truncating = 1;
7377c478bd9Sstevel@tonic-gate 		len = anssiz;
7387c478bd9Sstevel@tonic-gate 	} else
7397c478bd9Sstevel@tonic-gate 		len = resplen;
7407c478bd9Sstevel@tonic-gate 	if (len < HFIXEDSZ) {
7417c478bd9Sstevel@tonic-gate 		/*
7427c478bd9Sstevel@tonic-gate 		 * Undersized message.
7437c478bd9Sstevel@tonic-gate 		 */
7447c478bd9Sstevel@tonic-gate 		Dprint(statp->options & RES_DEBUG,
7457c478bd9Sstevel@tonic-gate 		       (stdout, ";; undersized: %d\n", len));
7467c478bd9Sstevel@tonic-gate 		*terrno = EMSGSIZE;
7477c478bd9Sstevel@tonic-gate 		res_nclose(statp);
7487c478bd9Sstevel@tonic-gate 		return (0);
7497c478bd9Sstevel@tonic-gate 	}
7507c478bd9Sstevel@tonic-gate 	cp = ans;
7517c478bd9Sstevel@tonic-gate 	while (len != 0 && (n = read(statp->_vcsock, (char *)cp, (int)len)) > 0){
7527c478bd9Sstevel@tonic-gate 		cp += n;
7537c478bd9Sstevel@tonic-gate 		len -= n;
7547c478bd9Sstevel@tonic-gate 	}
7557c478bd9Sstevel@tonic-gate 	if (n <= 0) {
7567c478bd9Sstevel@tonic-gate 		*terrno = errno;
7577c478bd9Sstevel@tonic-gate 		Perror(statp, stderr, "read(vc)", errno);
7587c478bd9Sstevel@tonic-gate 		res_nclose(statp);
7597c478bd9Sstevel@tonic-gate 		return (0);
7607c478bd9Sstevel@tonic-gate 	}
7617c478bd9Sstevel@tonic-gate 	if (truncating) {
7627c478bd9Sstevel@tonic-gate 		/*
7637c478bd9Sstevel@tonic-gate 		 * Flush rest of answer so connection stays in synch.
7647c478bd9Sstevel@tonic-gate 		 */
7657c478bd9Sstevel@tonic-gate 		anhp->tc = 1;
7667c478bd9Sstevel@tonic-gate 		len = resplen - anssiz;
7677c478bd9Sstevel@tonic-gate 		while (len != 0) {
7687c478bd9Sstevel@tonic-gate 			char junk[PACKETSZ];
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 			n = read(statp->_vcsock, junk,
7717c478bd9Sstevel@tonic-gate 				 (len > sizeof junk) ? sizeof junk : len);
7727c478bd9Sstevel@tonic-gate 			if (n > 0)
7737c478bd9Sstevel@tonic-gate 				len -= n;
7747c478bd9Sstevel@tonic-gate 			else
7757c478bd9Sstevel@tonic-gate 				break;
7767c478bd9Sstevel@tonic-gate 		}
7777c478bd9Sstevel@tonic-gate 	}
7787c478bd9Sstevel@tonic-gate 	/*
7797c478bd9Sstevel@tonic-gate 	 * If the calling applicating has bailed out of
7807c478bd9Sstevel@tonic-gate 	 * a previous call and failed to arrange to have
7817c478bd9Sstevel@tonic-gate 	 * the circuit closed or the server has got
7827c478bd9Sstevel@tonic-gate 	 * itself confused, then drop the packet and
7837c478bd9Sstevel@tonic-gate 	 * wait for the correct one.
7847c478bd9Sstevel@tonic-gate 	 */
7857c478bd9Sstevel@tonic-gate 	if (hp->id != anhp->id) {
7867c478bd9Sstevel@tonic-gate 		DprintQ((statp->options & RES_DEBUG) ||
7877c478bd9Sstevel@tonic-gate 			(statp->pfcode & RES_PRF_REPLY),
7887c478bd9Sstevel@tonic-gate 			(stdout, ";; old answer (unexpected):\n"),
7897c478bd9Sstevel@tonic-gate 			ans, (resplen > anssiz) ? anssiz: resplen);
7907c478bd9Sstevel@tonic-gate 		goto read_len;
7917c478bd9Sstevel@tonic-gate 	}
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	/*
7947c478bd9Sstevel@tonic-gate 	 * All is well, or the error is fatal.  Signal that the
7957c478bd9Sstevel@tonic-gate 	 * next nameserver ought not be tried.
7967c478bd9Sstevel@tonic-gate 	 */
7977c478bd9Sstevel@tonic-gate 	return (resplen);
7987c478bd9Sstevel@tonic-gate }
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate static int
801*9525b14bSRao Shoaib send_dg(res_state statp, const u_char *buf, int buflen, u_char *ans,
802*9525b14bSRao Shoaib 	int anssiz, int *terrno, int ns, int tries, int *v_circuit,
803*9525b14bSRao Shoaib 	int *gotsomewhere)
8047c478bd9Sstevel@tonic-gate {
8057c478bd9Sstevel@tonic-gate 	const HEADER *hp = (const HEADER *) buf;
8067c478bd9Sstevel@tonic-gate 	HEADER *anhp = (HEADER *) ans;
8077c478bd9Sstevel@tonic-gate 	const struct sockaddr *nsap;
8087c478bd9Sstevel@tonic-gate 	int nsaplen;
8097c478bd9Sstevel@tonic-gate 	struct timespec now, timeout, finish;
8107c478bd9Sstevel@tonic-gate 	struct sockaddr_storage from;
8117c478bd9Sstevel@tonic-gate 	ISC_SOCKLEN_T fromlen;
8127c478bd9Sstevel@tonic-gate 	int resplen, seconds, n, s;
813*9525b14bSRao Shoaib #ifdef USE_POLL
814*9525b14bSRao Shoaib 	int     polltimeout;
8157c478bd9Sstevel@tonic-gate 	struct pollfd   pollfd;
816*9525b14bSRao Shoaib #else
817*9525b14bSRao Shoaib 	fd_set dsmask;
8187c478bd9Sstevel@tonic-gate #endif
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	nsap = get_nsaddr(statp, ns);
8217c478bd9Sstevel@tonic-gate 	nsaplen = get_salen(nsap);
8227c478bd9Sstevel@tonic-gate 	if (EXT(statp).nssocks[ns] == -1) {
8237c478bd9Sstevel@tonic-gate 		EXT(statp).nssocks[ns] = socket(nsap->sa_family, SOCK_DGRAM, 0);
8247c478bd9Sstevel@tonic-gate 		if (EXT(statp).nssocks[ns] > highestFD) {
8257c478bd9Sstevel@tonic-gate 			res_nclose(statp);
8267c478bd9Sstevel@tonic-gate 			errno = ENOTSOCK;
8277c478bd9Sstevel@tonic-gate 		}
8287c478bd9Sstevel@tonic-gate 		if (EXT(statp).nssocks[ns] < 0) {
829*9525b14bSRao Shoaib 			switch (errno) {
830*9525b14bSRao Shoaib 			case EPROTONOSUPPORT:
831*9525b14bSRao Shoaib #ifdef EPFNOSUPPORT
832*9525b14bSRao Shoaib 			case EPFNOSUPPORT:
833*9525b14bSRao Shoaib #endif
834*9525b14bSRao Shoaib 			case EAFNOSUPPORT:
835*9525b14bSRao Shoaib 				Perror(statp, stderr, "socket(dg)", errno);
836*9525b14bSRao Shoaib 				return (0);
837*9525b14bSRao Shoaib 			default:
838*9525b14bSRao Shoaib 				*terrno = errno;
839*9525b14bSRao Shoaib 				Perror(statp, stderr, "socket(dg)", errno);
840*9525b14bSRao Shoaib 				return (-1);
841*9525b14bSRao Shoaib 			}
8427c478bd9Sstevel@tonic-gate 		}
8437c478bd9Sstevel@tonic-gate #ifndef CANNOT_CONNECT_DGRAM
8447c478bd9Sstevel@tonic-gate 		/*
8457c478bd9Sstevel@tonic-gate 		 * On a 4.3BSD+ machine (client and server,
8467c478bd9Sstevel@tonic-gate 		 * actually), sending to a nameserver datagram
8477c478bd9Sstevel@tonic-gate 		 * port with no nameserver will cause an
8487c478bd9Sstevel@tonic-gate 		 * ICMP port unreachable message to be returned.
8497c478bd9Sstevel@tonic-gate 		 * If our datagram socket is "connected" to the
8507c478bd9Sstevel@tonic-gate 		 * server, we get an ECONNREFUSED error on the next
8517c478bd9Sstevel@tonic-gate 		 * socket operation, and select returns if the
8527c478bd9Sstevel@tonic-gate 		 * error message is received.  We can thus detect
8537c478bd9Sstevel@tonic-gate 		 * the absence of a nameserver without timing out.
8547c478bd9Sstevel@tonic-gate 		 */
8557c478bd9Sstevel@tonic-gate 		if (connect(EXT(statp).nssocks[ns], nsap, nsaplen) < 0) {
8567c478bd9Sstevel@tonic-gate 			Aerror(statp, stderr, "connect(dg)", errno, nsap,
8577c478bd9Sstevel@tonic-gate 			    nsaplen);
8587c478bd9Sstevel@tonic-gate 			res_nclose(statp);
8597c478bd9Sstevel@tonic-gate 			return (0);
8607c478bd9Sstevel@tonic-gate 		}
8617c478bd9Sstevel@tonic-gate #endif /* !CANNOT_CONNECT_DGRAM */
8627c478bd9Sstevel@tonic-gate 		Dprint(statp->options & RES_DEBUG,
8637c478bd9Sstevel@tonic-gate 		       (stdout, ";; new DG socket\n"))
8647c478bd9Sstevel@tonic-gate 	}
8657c478bd9Sstevel@tonic-gate 	s = EXT(statp).nssocks[ns];
8667c478bd9Sstevel@tonic-gate #ifndef CANNOT_CONNECT_DGRAM
8677c478bd9Sstevel@tonic-gate 	if (send(s, (const char*)buf, buflen, 0) != buflen) {
8687c478bd9Sstevel@tonic-gate 		Perror(statp, stderr, "send", errno);
8697c478bd9Sstevel@tonic-gate 		res_nclose(statp);
8707c478bd9Sstevel@tonic-gate 		return (0);
8717c478bd9Sstevel@tonic-gate 	}
8727c478bd9Sstevel@tonic-gate #else /* !CANNOT_CONNECT_DGRAM */
8737c478bd9Sstevel@tonic-gate 	if (sendto(s, (const char*)buf, buflen, 0, nsap, nsaplen) != buflen)
8747c478bd9Sstevel@tonic-gate 	{
8757c478bd9Sstevel@tonic-gate 		Aerror(statp, stderr, "sendto", errno, nsap, nsaplen);
8767c478bd9Sstevel@tonic-gate 		res_nclose(statp);
8777c478bd9Sstevel@tonic-gate 		return (0);
8787c478bd9Sstevel@tonic-gate 	}
8797c478bd9Sstevel@tonic-gate #endif /* !CANNOT_CONNECT_DGRAM */
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 	/*
8827c478bd9Sstevel@tonic-gate 	 * Wait for reply.
8837c478bd9Sstevel@tonic-gate 	 */
884*9525b14bSRao Shoaib 	seconds = (statp->retrans << tries);
8857c478bd9Sstevel@tonic-gate 	if (ns > 0)
8867c478bd9Sstevel@tonic-gate 		seconds /= statp->nscount;
8877c478bd9Sstevel@tonic-gate 	if (seconds <= 0)
8887c478bd9Sstevel@tonic-gate 		seconds = 1;
8897c478bd9Sstevel@tonic-gate 	now = evNowTime();
8907c478bd9Sstevel@tonic-gate 	timeout = evConsTime(seconds, 0);
8917c478bd9Sstevel@tonic-gate 	finish = evAddTime(now, timeout);
8927c478bd9Sstevel@tonic-gate 	goto nonow;
8937c478bd9Sstevel@tonic-gate  wait:
8947c478bd9Sstevel@tonic-gate 	now = evNowTime();
8957c478bd9Sstevel@tonic-gate  nonow:
896*9525b14bSRao Shoaib #ifndef USE_POLL
897*9525b14bSRao Shoaib 	FD_ZERO(&dsmask);
898*9525b14bSRao Shoaib 	FD_SET(s, &dsmask);
899*9525b14bSRao Shoaib 	if (evCmpTime(finish, now) > 0)
900*9525b14bSRao Shoaib 		timeout = evSubTime(finish, now);
901*9525b14bSRao Shoaib 	else
902*9525b14bSRao Shoaib 		timeout = evConsTime(0, 0);
903*9525b14bSRao Shoaib 	n = pselect(s + 1, &dsmask, NULL, NULL, &timeout, NULL);
904*9525b14bSRao Shoaib #else
9057c478bd9Sstevel@tonic-gate 	timeout = evSubTime(finish, now);
9067c478bd9Sstevel@tonic-gate 	if (timeout.tv_sec < 0)
9077c478bd9Sstevel@tonic-gate 		timeout = evConsTime(0, 0);
9087c478bd9Sstevel@tonic-gate 	polltimeout = 1000*timeout.tv_sec +
9097c478bd9Sstevel@tonic-gate 		timeout.tv_nsec/1000000;
9107c478bd9Sstevel@tonic-gate 	pollfd.fd = s;
9117c478bd9Sstevel@tonic-gate 	pollfd.events = POLLRDNORM;
9127c478bd9Sstevel@tonic-gate 	n = poll(&pollfd, 1, polltimeout);
913*9525b14bSRao Shoaib #endif /* USE_POLL */
914*9525b14bSRao Shoaib 
9157c478bd9Sstevel@tonic-gate 	if (n == 0) {
9167c478bd9Sstevel@tonic-gate 		Dprint(statp->options & RES_DEBUG, (stdout, ";; timeout\n"));
9177c478bd9Sstevel@tonic-gate 		*gotsomewhere = 1;
9187c478bd9Sstevel@tonic-gate 		return (0);
9197c478bd9Sstevel@tonic-gate 	}
9207c478bd9Sstevel@tonic-gate 	if (n < 0) {
9217c478bd9Sstevel@tonic-gate 		if (errno == EINTR)
9227c478bd9Sstevel@tonic-gate 			goto wait;
923*9525b14bSRao Shoaib #ifndef USE_POLL
9247c478bd9Sstevel@tonic-gate 		Perror(statp, stderr, "select", errno);
925*9525b14bSRao Shoaib #else
926*9525b14bSRao Shoaib 		Perror(statp, stderr, "poll", errno);
927*9525b14bSRao Shoaib #endif /* USE_POLL */
9287c478bd9Sstevel@tonic-gate 		res_nclose(statp);
9297c478bd9Sstevel@tonic-gate 		return (0);
9307c478bd9Sstevel@tonic-gate 	}
9317c478bd9Sstevel@tonic-gate 	errno = 0;
9327c478bd9Sstevel@tonic-gate 	fromlen = sizeof(from);
9337c478bd9Sstevel@tonic-gate 	resplen = recvfrom(s, (char*)ans, anssiz,0,
9347c478bd9Sstevel@tonic-gate 			   (struct sockaddr *)&from, &fromlen);
9357c478bd9Sstevel@tonic-gate 	if (resplen <= 0) {
9367c478bd9Sstevel@tonic-gate 		Perror(statp, stderr, "recvfrom", errno);
9377c478bd9Sstevel@tonic-gate 		res_nclose(statp);
9387c478bd9Sstevel@tonic-gate 		return (0);
9397c478bd9Sstevel@tonic-gate 	}
9407c478bd9Sstevel@tonic-gate 	*gotsomewhere = 1;
9417c478bd9Sstevel@tonic-gate 	if (resplen < HFIXEDSZ) {
9427c478bd9Sstevel@tonic-gate 		/*
9437c478bd9Sstevel@tonic-gate 		 * Undersized message.
9447c478bd9Sstevel@tonic-gate 		 */
9457c478bd9Sstevel@tonic-gate 		Dprint(statp->options & RES_DEBUG,
9467c478bd9Sstevel@tonic-gate 		       (stdout, ";; undersized: %d\n",
9477c478bd9Sstevel@tonic-gate 			resplen));
9487c478bd9Sstevel@tonic-gate 		*terrno = EMSGSIZE;
9497c478bd9Sstevel@tonic-gate 		res_nclose(statp);
9507c478bd9Sstevel@tonic-gate 		return (0);
9517c478bd9Sstevel@tonic-gate 	}
9527c478bd9Sstevel@tonic-gate 	if (hp->id != anhp->id) {
9537c478bd9Sstevel@tonic-gate 		/*
9547c478bd9Sstevel@tonic-gate 		 * response from old query, ignore it.
9557c478bd9Sstevel@tonic-gate 		 * XXX - potential security hazard could
9567c478bd9Sstevel@tonic-gate 		 *	 be detected here.
9577c478bd9Sstevel@tonic-gate 		 */
9587c478bd9Sstevel@tonic-gate 		DprintQ((statp->options & RES_DEBUG) ||
9597c478bd9Sstevel@tonic-gate 			(statp->pfcode & RES_PRF_REPLY),
9607c478bd9Sstevel@tonic-gate 			(stdout, ";; old answer:\n"),
9617c478bd9Sstevel@tonic-gate 			ans, (resplen > anssiz) ? anssiz : resplen);
9627c478bd9Sstevel@tonic-gate 		goto wait;
9637c478bd9Sstevel@tonic-gate 	}
9647c478bd9Sstevel@tonic-gate 	if (!(statp->options & RES_INSECURE1) &&
9657c478bd9Sstevel@tonic-gate 	    !res_ourserver_p(statp, (struct sockaddr *)&from)) {
9667c478bd9Sstevel@tonic-gate 		/*
9677c478bd9Sstevel@tonic-gate 		 * response from wrong server? ignore it.
9687c478bd9Sstevel@tonic-gate 		 * XXX - potential security hazard could
9697c478bd9Sstevel@tonic-gate 		 *	 be detected here.
9707c478bd9Sstevel@tonic-gate 		 */
9717c478bd9Sstevel@tonic-gate 		DprintQ((statp->options & RES_DEBUG) ||
9727c478bd9Sstevel@tonic-gate 			(statp->pfcode & RES_PRF_REPLY),
9737c478bd9Sstevel@tonic-gate 			(stdout, ";; not our server:\n"),
9747c478bd9Sstevel@tonic-gate 			ans, (resplen > anssiz) ? anssiz : resplen);
9757c478bd9Sstevel@tonic-gate 		goto wait;
9767c478bd9Sstevel@tonic-gate 	}
9777c478bd9Sstevel@tonic-gate #ifdef RES_USE_EDNS0
978*9525b14bSRao Shoaib 	if (anhp->rcode == FORMERR && (statp->options & RES_USE_EDNS0) != 0U) {
9797c478bd9Sstevel@tonic-gate 		/*
9807c478bd9Sstevel@tonic-gate 		 * Do not retry if the server do not understand EDNS0.
9817c478bd9Sstevel@tonic-gate 		 * The case has to be captured here, as FORMERR packet do not
9827c478bd9Sstevel@tonic-gate 		 * carry query section, hence res_queriesmatch() returns 0.
9837c478bd9Sstevel@tonic-gate 		 */
9847c478bd9Sstevel@tonic-gate 		DprintQ(statp->options & RES_DEBUG,
9857c478bd9Sstevel@tonic-gate 			(stdout, "server rejected query with EDNS0:\n"),
9867c478bd9Sstevel@tonic-gate 			ans, (resplen > anssiz) ? anssiz : resplen);
9877c478bd9Sstevel@tonic-gate 		/* record the error */
9887c478bd9Sstevel@tonic-gate 		statp->_flags |= RES_F_EDNS0ERR;
9897c478bd9Sstevel@tonic-gate 		res_nclose(statp);
9907c478bd9Sstevel@tonic-gate 		return (0);
9917c478bd9Sstevel@tonic-gate 	}
9927c478bd9Sstevel@tonic-gate #endif
9937c478bd9Sstevel@tonic-gate 	if (!(statp->options & RES_INSECURE2) &&
9947c478bd9Sstevel@tonic-gate 	    !res_queriesmatch(buf, buf + buflen,
9957c478bd9Sstevel@tonic-gate 			      ans, ans + anssiz)) {
9967c478bd9Sstevel@tonic-gate 		/*
9977c478bd9Sstevel@tonic-gate 		 * response contains wrong query? ignore it.
9987c478bd9Sstevel@tonic-gate 		 * XXX - potential security hazard could
9997c478bd9Sstevel@tonic-gate 		 *	 be detected here.
10007c478bd9Sstevel@tonic-gate 		 */
10017c478bd9Sstevel@tonic-gate 		DprintQ((statp->options & RES_DEBUG) ||
10027c478bd9Sstevel@tonic-gate 			(statp->pfcode & RES_PRF_REPLY),
10037c478bd9Sstevel@tonic-gate 			(stdout, ";; wrong query name:\n"),
10047c478bd9Sstevel@tonic-gate 			ans, (resplen > anssiz) ? anssiz : resplen);
10057c478bd9Sstevel@tonic-gate 		goto wait;
10067c478bd9Sstevel@tonic-gate 	}
10077c478bd9Sstevel@tonic-gate 	if (anhp->rcode == SERVFAIL ||
10087c478bd9Sstevel@tonic-gate 	    anhp->rcode == NOTIMP ||
10097c478bd9Sstevel@tonic-gate 	    anhp->rcode == REFUSED) {
10107c478bd9Sstevel@tonic-gate 		DprintQ(statp->options & RES_DEBUG,
10117c478bd9Sstevel@tonic-gate 			(stdout, "server rejected query:\n"),
10127c478bd9Sstevel@tonic-gate 			ans, (resplen > anssiz) ? anssiz : resplen);
10137c478bd9Sstevel@tonic-gate 		res_nclose(statp);
10147c478bd9Sstevel@tonic-gate 		/* don't retry if called from dig */
10157c478bd9Sstevel@tonic-gate 		if (!statp->pfcode)
10167c478bd9Sstevel@tonic-gate 			return (0);
10177c478bd9Sstevel@tonic-gate 	}
10187c478bd9Sstevel@tonic-gate 	if (!(statp->options & RES_IGNTC) && anhp->tc) {
10197c478bd9Sstevel@tonic-gate 		/*
10207c478bd9Sstevel@tonic-gate 		 * To get the rest of answer,
10217c478bd9Sstevel@tonic-gate 		 * use TCP with same server.
10227c478bd9Sstevel@tonic-gate 		 */
10237c478bd9Sstevel@tonic-gate 		Dprint(statp->options & RES_DEBUG,
10247c478bd9Sstevel@tonic-gate 		       (stdout, ";; truncated answer\n"));
10257c478bd9Sstevel@tonic-gate 		*v_circuit = 1;
10267c478bd9Sstevel@tonic-gate 		res_nclose(statp);
10277c478bd9Sstevel@tonic-gate 		return (1);
10287c478bd9Sstevel@tonic-gate 	}
10297c478bd9Sstevel@tonic-gate 	/*
10307c478bd9Sstevel@tonic-gate 	 * All is well, or the error is fatal.  Signal that the
10317c478bd9Sstevel@tonic-gate 	 * next nameserver ought not be tried.
10327c478bd9Sstevel@tonic-gate 	 */
10337c478bd9Sstevel@tonic-gate 	return (resplen);
10347c478bd9Sstevel@tonic-gate }
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate static void
10377c478bd9Sstevel@tonic-gate Aerror(const res_state statp, FILE *file, const char *string, int error,
10387c478bd9Sstevel@tonic-gate        const struct sockaddr *address, int alen)
10397c478bd9Sstevel@tonic-gate {
10407c478bd9Sstevel@tonic-gate 	int save = errno;
10417c478bd9Sstevel@tonic-gate 	char hbuf[NI_MAXHOST];
10427c478bd9Sstevel@tonic-gate 	char sbuf[NI_MAXSERV];
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 	alen = alen;
10457c478bd9Sstevel@tonic-gate 
1046*9525b14bSRao Shoaib 	if ((statp->options & RES_DEBUG) != 0U) {
10477c478bd9Sstevel@tonic-gate 		if (getnameinfo(address, alen, hbuf, sizeof(hbuf),
10487c478bd9Sstevel@tonic-gate 		    sbuf, sizeof(sbuf), niflags)) {
10497c478bd9Sstevel@tonic-gate 			strncpy(hbuf, "?", sizeof(hbuf) - 1);
10507c478bd9Sstevel@tonic-gate 			hbuf[sizeof(hbuf) - 1] = '\0';
10517c478bd9Sstevel@tonic-gate 			strncpy(sbuf, "?", sizeof(sbuf) - 1);
10527c478bd9Sstevel@tonic-gate 			sbuf[sizeof(sbuf) - 1] = '\0';
10537c478bd9Sstevel@tonic-gate 		}
10547c478bd9Sstevel@tonic-gate 		fprintf(file, "res_send: %s ([%s].%s): %s\n",
10557c478bd9Sstevel@tonic-gate 			string, hbuf, sbuf, strerror(error));
10567c478bd9Sstevel@tonic-gate 	}
10577c478bd9Sstevel@tonic-gate 	errno = save;
10587c478bd9Sstevel@tonic-gate }
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate static void
10617c478bd9Sstevel@tonic-gate Perror(const res_state statp, FILE *file, const char *string, int error) {
10627c478bd9Sstevel@tonic-gate 	int save = errno;
10637c478bd9Sstevel@tonic-gate 
1064*9525b14bSRao Shoaib 	if ((statp->options & RES_DEBUG) != 0U)
10657c478bd9Sstevel@tonic-gate 		fprintf(file, "res_send: %s: %s\n",
10667c478bd9Sstevel@tonic-gate 			string, strerror(error));
10677c478bd9Sstevel@tonic-gate 	errno = save;
10687c478bd9Sstevel@tonic-gate }
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate static int
10717c478bd9Sstevel@tonic-gate sock_eq(struct sockaddr *a, struct sockaddr *b) {
10727c478bd9Sstevel@tonic-gate 	struct sockaddr_in *a4, *b4;
10737c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *a6, *b6;
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 	if (a->sa_family != b->sa_family)
10767c478bd9Sstevel@tonic-gate 		return 0;
10777c478bd9Sstevel@tonic-gate 	switch (a->sa_family) {
10787c478bd9Sstevel@tonic-gate 	case AF_INET:
10797c478bd9Sstevel@tonic-gate 		a4 = (struct sockaddr_in *)a;
10807c478bd9Sstevel@tonic-gate 		b4 = (struct sockaddr_in *)b;
10817c478bd9Sstevel@tonic-gate 		return a4->sin_port == b4->sin_port &&
10827c478bd9Sstevel@tonic-gate 		    a4->sin_addr.s_addr == b4->sin_addr.s_addr;
10837c478bd9Sstevel@tonic-gate 	case AF_INET6:
10847c478bd9Sstevel@tonic-gate 		a6 = (struct sockaddr_in6 *)a;
10857c478bd9Sstevel@tonic-gate 		b6 = (struct sockaddr_in6 *)b;
10867c478bd9Sstevel@tonic-gate 		return a6->sin6_port == b6->sin6_port &&
10877c478bd9Sstevel@tonic-gate #ifdef HAVE_SIN6_SCOPE_ID
10887c478bd9Sstevel@tonic-gate 		    a6->sin6_scope_id == b6->sin6_scope_id &&
10897c478bd9Sstevel@tonic-gate #endif
10907c478bd9Sstevel@tonic-gate 		    IN6_ARE_ADDR_EQUAL(&a6->sin6_addr, &b6->sin6_addr);
10917c478bd9Sstevel@tonic-gate 	default:
10927c478bd9Sstevel@tonic-gate 		return 0;
10937c478bd9Sstevel@tonic-gate 	}
10947c478bd9Sstevel@tonic-gate }
10957c478bd9Sstevel@tonic-gate 
1096*9525b14bSRao Shoaib #if defined(NEED_PSELECT) && !defined(USE_POLL)
10977c478bd9Sstevel@tonic-gate /* XXX needs to move to the porting library. */
10987c478bd9Sstevel@tonic-gate static int
10997c478bd9Sstevel@tonic-gate pselect(int nfds, void *rfds, void *wfds, void *efds,
11007c478bd9Sstevel@tonic-gate 	struct timespec *tsp, const sigset_t *sigmask)
11017c478bd9Sstevel@tonic-gate {
11027c478bd9Sstevel@tonic-gate 	struct timeval tv, *tvp;
11037c478bd9Sstevel@tonic-gate 	sigset_t sigs;
11047c478bd9Sstevel@tonic-gate 	int n;
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate 	if (tsp) {
11077c478bd9Sstevel@tonic-gate 		tvp = &tv;
11087c478bd9Sstevel@tonic-gate 		tv = evTimeVal(*tsp);
11097c478bd9Sstevel@tonic-gate 	} else
11107c478bd9Sstevel@tonic-gate 		tvp = NULL;
11117c478bd9Sstevel@tonic-gate 	if (sigmask)
11127c478bd9Sstevel@tonic-gate 		sigprocmask(SIG_SETMASK, sigmask, &sigs);
11137c478bd9Sstevel@tonic-gate 	n = select(nfds, rfds, wfds, efds, tvp);
11147c478bd9Sstevel@tonic-gate 	if (sigmask)
11157c478bd9Sstevel@tonic-gate 		sigprocmask(SIG_SETMASK, &sigs, NULL);
11167c478bd9Sstevel@tonic-gate 	if (tsp)
11177c478bd9Sstevel@tonic-gate 		*tsp = evTimeSpec(tv);
11187c478bd9Sstevel@tonic-gate 	return (n);
11197c478bd9Sstevel@tonic-gate }
11207c478bd9Sstevel@tonic-gate #endif
1121