xref: /onnv-gate/usr/src/lib/libresolv2/common/resolv/res_send.c (revision 11038:74b12212b8a2)
10Sstevel@tonic-gate /*
2*11038SRao.Shoaib@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
30Sstevel@tonic-gate  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
6*11038SRao.Shoaib@Sun.COM 
7*11038SRao.Shoaib@Sun.COM /*
8*11038SRao.Shoaib@Sun.COM  * Portions Copyright (C) 2004-2009  Internet Systems Consortium, Inc. ("ISC")
9*11038SRao.Shoaib@Sun.COM  * Portions Copyright (C) 1996-2003  Internet Software Consortium.
10*11038SRao.Shoaib@Sun.COM  *
11*11038SRao.Shoaib@Sun.COM  * Permission to use, copy, modify, and/or distribute this software for any
12*11038SRao.Shoaib@Sun.COM  * purpose with or without fee is hereby granted, provided that the above
13*11038SRao.Shoaib@Sun.COM  * copyright notice and this permission notice appear in all copies.
14*11038SRao.Shoaib@Sun.COM  *
15*11038SRao.Shoaib@Sun.COM  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
16*11038SRao.Shoaib@Sun.COM  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
17*11038SRao.Shoaib@Sun.COM  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
18*11038SRao.Shoaib@Sun.COM  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
19*11038SRao.Shoaib@Sun.COM  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
20*11038SRao.Shoaib@Sun.COM  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
21*11038SRao.Shoaib@Sun.COM  * PERFORMANCE OF THIS SOFTWARE.
22*11038SRao.Shoaib@Sun.COM  */
23*11038SRao.Shoaib@Sun.COM 
240Sstevel@tonic-gate /*
250Sstevel@tonic-gate  * Copyright (c) 1985, 1989, 1993
260Sstevel@tonic-gate  *    The Regents of the University of California.  All rights reserved.
27*11038SRao.Shoaib@Sun.COM  *
280Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
290Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
300Sstevel@tonic-gate  * are met:
310Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
320Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
330Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
340Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
350Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
360Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
370Sstevel@tonic-gate  *    must display the following acknowledgement:
380Sstevel@tonic-gate  * 	This product includes software developed by the University of
390Sstevel@tonic-gate  * 	California, Berkeley and its contributors.
400Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
410Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
420Sstevel@tonic-gate  *    without specific prior written permission.
43*11038SRao.Shoaib@Sun.COM  *
440Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
450Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
460Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
470Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
480Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
490Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
500Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
510Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
520Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
530Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
540Sstevel@tonic-gate  * SUCH DAMAGE.
550Sstevel@tonic-gate  */
560Sstevel@tonic-gate 
570Sstevel@tonic-gate /*
580Sstevel@tonic-gate  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
59*11038SRao.Shoaib@Sun.COM  *
600Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
610Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
620Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies, and that
630Sstevel@tonic-gate  * the name of Digital Equipment Corporation not be used in advertising or
640Sstevel@tonic-gate  * publicity pertaining to distribution of the document or software without
650Sstevel@tonic-gate  * specific, written prior permission.
66*11038SRao.Shoaib@Sun.COM  *
670Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
680Sstevel@tonic-gate  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
690Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
700Sstevel@tonic-gate  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
710Sstevel@tonic-gate  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
720Sstevel@tonic-gate  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
730Sstevel@tonic-gate  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
740Sstevel@tonic-gate  * SOFTWARE.
750Sstevel@tonic-gate  */
760Sstevel@tonic-gate 
770Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
780Sstevel@tonic-gate static const char sccsid[] = "@(#)res_send.c	8.1 (Berkeley) 6/4/93";
79*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: res_send.c,v 1.22 2009/01/22 23:49:23 tbox Exp $";
800Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
810Sstevel@tonic-gate 
82*11038SRao.Shoaib@Sun.COM /*! \file
83*11038SRao.Shoaib@Sun.COM  * \brief
840Sstevel@tonic-gate  * Send query to name server and wait for reply.
850Sstevel@tonic-gate  */
860Sstevel@tonic-gate 
870Sstevel@tonic-gate #include "port_before.h"
880Sstevel@tonic-gate #include "fd_setsize.h"
890Sstevel@tonic-gate 
900Sstevel@tonic-gate #include <sys/types.h>
910Sstevel@tonic-gate #include <sys/param.h>
920Sstevel@tonic-gate #include <sys/time.h>
930Sstevel@tonic-gate #include <sys/socket.h>
940Sstevel@tonic-gate #include <sys/uio.h>
950Sstevel@tonic-gate 
960Sstevel@tonic-gate #include <netinet/in.h>
970Sstevel@tonic-gate #include <arpa/nameser.h>
980Sstevel@tonic-gate #include <arpa/inet.h>
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate #include <errno.h>
1010Sstevel@tonic-gate #include <netdb.h>
1020Sstevel@tonic-gate #include <resolv.h>
1030Sstevel@tonic-gate #include <signal.h>
1040Sstevel@tonic-gate #include <stdio.h>
1050Sstevel@tonic-gate #include <stdlib.h>
1060Sstevel@tonic-gate #include <string.h>
1070Sstevel@tonic-gate #include <unistd.h>
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate #include <isc/eventlib.h>
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate #include "port_after.h"
1120Sstevel@tonic-gate 
113*11038SRao.Shoaib@Sun.COM #ifdef USE_POLL
114*11038SRao.Shoaib@Sun.COM #ifdef HAVE_STROPTS_H
1150Sstevel@tonic-gate #include <stropts.h>
116*11038SRao.Shoaib@Sun.COM #endif
1170Sstevel@tonic-gate #include <poll.h>
118*11038SRao.Shoaib@Sun.COM #endif /* USE_POLL */
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate /* Options.  Leave them on. */
1210Sstevel@tonic-gate #define DEBUG
1220Sstevel@tonic-gate #include "res_debug.h"
1230Sstevel@tonic-gate #include "res_private.h"
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate #define EXT(res) ((res)->_u._ext)
1260Sstevel@tonic-gate 
127*11038SRao.Shoaib@Sun.COM #ifndef USE_POLL
1280Sstevel@tonic-gate static const int highestFD = FD_SETSIZE - 1;
1290Sstevel@tonic-gate #else
1300Sstevel@tonic-gate static int highestFD = 0;
1310Sstevel@tonic-gate #endif
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate /* Forward. */
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate static int		get_salen __P((const struct sockaddr *));
1360Sstevel@tonic-gate static struct sockaddr * get_nsaddr __P((res_state, size_t));
1370Sstevel@tonic-gate static int		send_vc(res_state, const u_char *, int,
1380Sstevel@tonic-gate 				u_char *, int, int *, int);
1390Sstevel@tonic-gate static int		send_dg(res_state, const u_char *, int,
140*11038SRao.Shoaib@Sun.COM 				u_char *, int, int *, int, int,
1410Sstevel@tonic-gate 				int *, int *);
1420Sstevel@tonic-gate static void		Aerror(const res_state, FILE *, const char *, int,
1430Sstevel@tonic-gate 			       const struct sockaddr *, int);
1440Sstevel@tonic-gate static void		Perror(const res_state, FILE *, const char *, int);
1450Sstevel@tonic-gate static int		sock_eq(struct sockaddr *, struct sockaddr *);
146*11038SRao.Shoaib@Sun.COM #if defined(NEED_PSELECT) && !defined(USE_POLL)
1470Sstevel@tonic-gate static int		pselect(int, void *, void *, void *,
1480Sstevel@tonic-gate 				struct timespec *,
1490Sstevel@tonic-gate 				const sigset_t *);
1500Sstevel@tonic-gate #endif
1510Sstevel@tonic-gate void res_pquery(const res_state, const u_char *, int, FILE *);
1520Sstevel@tonic-gate 
153*11038SRao.Shoaib@Sun.COM #ifndef	ORIGINAL_ISC_CODE
1540Sstevel@tonic-gate #pragma weak	__res_nameinquery	=	res_nameinquery
1550Sstevel@tonic-gate #pragma weak	__res_queriesmatch	=	res_queriesmatch
1560Sstevel@tonic-gate #pragma weak	res_nisourserver	=	res_ourserver_p
1570Sstevel@tonic-gate #endif	/* ORIGINAL_ISC_CODE */
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate static const int niflags = NI_NUMERICHOST | NI_NUMERICSERV;
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate /* Public. */
1620Sstevel@tonic-gate 
163*11038SRao.Shoaib@Sun.COM /*%
1640Sstevel@tonic-gate  *	looks up "ina" in _res.ns_addr_list[]
165*11038SRao.Shoaib@Sun.COM  *
1660Sstevel@tonic-gate  * returns:
167*11038SRao.Shoaib@Sun.COM  *\li	0  : not found
168*11038SRao.Shoaib@Sun.COM  *\li	>0 : found
169*11038SRao.Shoaib@Sun.COM  *
1700Sstevel@tonic-gate  * author:
171*11038SRao.Shoaib@Sun.COM  *\li	paul vixie, 29may94
1720Sstevel@tonic-gate  */
1730Sstevel@tonic-gate int
res_ourserver_p(const res_state statp,const struct sockaddr * sa)1740Sstevel@tonic-gate res_ourserver_p(const res_state statp, const struct sockaddr *sa) {
1750Sstevel@tonic-gate 	const struct sockaddr_in *inp, *srv;
1760Sstevel@tonic-gate 	const struct sockaddr_in6 *in6p, *srv6;
1770Sstevel@tonic-gate 	int ns;
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	switch (sa->sa_family) {
1800Sstevel@tonic-gate 	case AF_INET:
1810Sstevel@tonic-gate 		inp = (const struct sockaddr_in *)sa;
1820Sstevel@tonic-gate 		for (ns = 0;  ns < statp->nscount;  ns++) {
1830Sstevel@tonic-gate 			srv = (struct sockaddr_in *)get_nsaddr(statp, ns);
1840Sstevel@tonic-gate 			if (srv->sin_family == inp->sin_family &&
1850Sstevel@tonic-gate 			    srv->sin_port == inp->sin_port &&
1860Sstevel@tonic-gate 			    (srv->sin_addr.s_addr == INADDR_ANY ||
1870Sstevel@tonic-gate 			     srv->sin_addr.s_addr == inp->sin_addr.s_addr))
1880Sstevel@tonic-gate 				return (1);
1890Sstevel@tonic-gate 		}
1900Sstevel@tonic-gate 		break;
1910Sstevel@tonic-gate 	case AF_INET6:
1920Sstevel@tonic-gate 		if (EXT(statp).ext == NULL)
1930Sstevel@tonic-gate 			break;
1940Sstevel@tonic-gate 		in6p = (const struct sockaddr_in6 *)sa;
1950Sstevel@tonic-gate 		for (ns = 0;  ns < statp->nscount;  ns++) {
1960Sstevel@tonic-gate 			srv6 = (struct sockaddr_in6 *)get_nsaddr(statp, ns);
1970Sstevel@tonic-gate 			if (srv6->sin6_family == in6p->sin6_family &&
1980Sstevel@tonic-gate 			    srv6->sin6_port == in6p->sin6_port &&
1990Sstevel@tonic-gate #ifdef HAVE_SIN6_SCOPE_ID
200*11038SRao.Shoaib@Sun.COM 			    (srv6->sin6_scope_id == 0 ||
201*11038SRao.Shoaib@Sun.COM 			     srv6->sin6_scope_id == in6p->sin6_scope_id) &&
2020Sstevel@tonic-gate #endif
2030Sstevel@tonic-gate 			    (IN6_IS_ADDR_UNSPECIFIED(&srv6->sin6_addr) ||
2040Sstevel@tonic-gate 			     IN6_ARE_ADDR_EQUAL(&srv6->sin6_addr, &in6p->sin6_addr)))
2050Sstevel@tonic-gate 				return (1);
2060Sstevel@tonic-gate 		}
2070Sstevel@tonic-gate 		break;
2080Sstevel@tonic-gate 	default:
2090Sstevel@tonic-gate 		break;
2100Sstevel@tonic-gate 	}
2110Sstevel@tonic-gate 	return (0);
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate 
214*11038SRao.Shoaib@Sun.COM /*%
2150Sstevel@tonic-gate  *	look for (name,type,class) in the query section of packet (buf,eom)
216*11038SRao.Shoaib@Sun.COM  *
2170Sstevel@tonic-gate  * requires:
218*11038SRao.Shoaib@Sun.COM  *\li	buf + HFIXEDSZ <= eom
219*11038SRao.Shoaib@Sun.COM  *
2200Sstevel@tonic-gate  * returns:
221*11038SRao.Shoaib@Sun.COM  *\li	-1 : format error
222*11038SRao.Shoaib@Sun.COM  *\li	0  : not found
223*11038SRao.Shoaib@Sun.COM  *\li	>0 : found
224*11038SRao.Shoaib@Sun.COM  *
2250Sstevel@tonic-gate  * author:
226*11038SRao.Shoaib@Sun.COM  *\li	paul vixie, 29may94
2270Sstevel@tonic-gate  */
2280Sstevel@tonic-gate int
res_nameinquery(const char * name,int type,int class,const u_char * buf,const u_char * eom)2290Sstevel@tonic-gate res_nameinquery(const char *name, int type, int class,
2300Sstevel@tonic-gate 		const u_char *buf, const u_char *eom)
2310Sstevel@tonic-gate {
2320Sstevel@tonic-gate 	const u_char *cp = buf + HFIXEDSZ;
2330Sstevel@tonic-gate 	int qdcount = ntohs(((const HEADER*)buf)->qdcount);
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	while (qdcount-- > 0) {
2360Sstevel@tonic-gate 		char tname[MAXDNAME+1];
2370Sstevel@tonic-gate 		int n, ttype, tclass;
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 		n = dn_expand(buf, eom, cp, tname, sizeof tname);
2400Sstevel@tonic-gate 		if (n < 0)
2410Sstevel@tonic-gate 			return (-1);
2420Sstevel@tonic-gate 		cp += n;
2430Sstevel@tonic-gate 		if (cp + 2 * INT16SZ > eom)
2440Sstevel@tonic-gate 			return (-1);
2450Sstevel@tonic-gate 		ttype = ns_get16(cp); cp += INT16SZ;
2460Sstevel@tonic-gate 		tclass = ns_get16(cp); cp += INT16SZ;
2470Sstevel@tonic-gate 		if (ttype == type && tclass == class &&
2480Sstevel@tonic-gate 		    ns_samename(tname, name) == 1)
2490Sstevel@tonic-gate 			return (1);
2500Sstevel@tonic-gate 	}
2510Sstevel@tonic-gate 	return (0);
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate 
254*11038SRao.Shoaib@Sun.COM /*%
2550Sstevel@tonic-gate  *	is there a 1:1 mapping of (name,type,class)
2560Sstevel@tonic-gate  *	in (buf1,eom1) and (buf2,eom2)?
257*11038SRao.Shoaib@Sun.COM  *
2580Sstevel@tonic-gate  * returns:
259*11038SRao.Shoaib@Sun.COM  *\li	-1 : format error
260*11038SRao.Shoaib@Sun.COM  *\li	0  : not a 1:1 mapping
261*11038SRao.Shoaib@Sun.COM  *\li	>0 : is a 1:1 mapping
262*11038SRao.Shoaib@Sun.COM  *
2630Sstevel@tonic-gate  * author:
264*11038SRao.Shoaib@Sun.COM  *\li	paul vixie, 29may94
2650Sstevel@tonic-gate  */
2660Sstevel@tonic-gate int
res_queriesmatch(const u_char * buf1,const u_char * eom1,const u_char * buf2,const u_char * eom2)2670Sstevel@tonic-gate res_queriesmatch(const u_char *buf1, const u_char *eom1,
2680Sstevel@tonic-gate 		 const u_char *buf2, const u_char *eom2)
2690Sstevel@tonic-gate {
2700Sstevel@tonic-gate 	const u_char *cp = buf1 + HFIXEDSZ;
2710Sstevel@tonic-gate 	int qdcount = ntohs(((const HEADER*)buf1)->qdcount);
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	if (buf1 + HFIXEDSZ > eom1 || buf2 + HFIXEDSZ > eom2)
2740Sstevel@tonic-gate 		return (-1);
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	/*
2770Sstevel@tonic-gate 	 * Only header section present in replies to
2780Sstevel@tonic-gate 	 * dynamic update packets.
2790Sstevel@tonic-gate 	 */
2800Sstevel@tonic-gate 	if ((((const HEADER *)buf1)->opcode == ns_o_update) &&
2810Sstevel@tonic-gate 	    (((const HEADER *)buf2)->opcode == ns_o_update))
2820Sstevel@tonic-gate 		return (1);
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	if (qdcount != ntohs(((const HEADER*)buf2)->qdcount))
2850Sstevel@tonic-gate 		return (0);
2860Sstevel@tonic-gate 	while (qdcount-- > 0) {
2870Sstevel@tonic-gate 		char tname[MAXDNAME+1];
2880Sstevel@tonic-gate 		int n, ttype, tclass;
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 		n = dn_expand(buf1, eom1, cp, tname, sizeof tname);
2910Sstevel@tonic-gate 		if (n < 0)
2920Sstevel@tonic-gate 			return (-1);
2930Sstevel@tonic-gate 		cp += n;
2940Sstevel@tonic-gate 		if (cp + 2 * INT16SZ > eom1)
2950Sstevel@tonic-gate 			return (-1);
2960Sstevel@tonic-gate 		ttype = ns_get16(cp);	cp += INT16SZ;
2970Sstevel@tonic-gate 		tclass = ns_get16(cp); cp += INT16SZ;
2980Sstevel@tonic-gate 		if (!res_nameinquery(tname, ttype, tclass, buf2, eom2))
2990Sstevel@tonic-gate 			return (0);
3000Sstevel@tonic-gate 	}
3010Sstevel@tonic-gate 	return (1);
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate int
res_nsend(res_state statp,const u_char * buf,int buflen,u_char * ans,int anssiz)3050Sstevel@tonic-gate res_nsend(res_state statp,
3060Sstevel@tonic-gate 	  const u_char *buf, int buflen, u_char *ans, int anssiz)
3070Sstevel@tonic-gate {
308*11038SRao.Shoaib@Sun.COM 	int gotsomewhere, terrno, tries, v_circuit, resplen, ns, n;
3090Sstevel@tonic-gate 	char abuf[NI_MAXHOST];
3100Sstevel@tonic-gate 
311*11038SRao.Shoaib@Sun.COM #ifdef USE_POLL
3120Sstevel@tonic-gate 	highestFD = sysconf(_SC_OPEN_MAX) - 1;
3130Sstevel@tonic-gate #endif
3140Sstevel@tonic-gate 
315*11038SRao.Shoaib@Sun.COM 	/* No name servers or res_init() failure */
3163360Ssm26363 	if (statp->nscount == 0 || EXT(statp).ext == NULL) {
3170Sstevel@tonic-gate 		errno = ESRCH;
3180Sstevel@tonic-gate 		return (-1);
3190Sstevel@tonic-gate 	}
3200Sstevel@tonic-gate 	if (anssiz < HFIXEDSZ) {
3210Sstevel@tonic-gate 		errno = EINVAL;
3220Sstevel@tonic-gate 		return (-1);
3230Sstevel@tonic-gate 	}
3240Sstevel@tonic-gate 	DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_QUERY),
3250Sstevel@tonic-gate 		(stdout, ";; res_send()\n"), buf, buflen);
3260Sstevel@tonic-gate 	v_circuit = (statp->options & RES_USEVC) || buflen > PACKETSZ;
3270Sstevel@tonic-gate 	gotsomewhere = 0;
3280Sstevel@tonic-gate 	terrno = ETIMEDOUT;
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	/*
3310Sstevel@tonic-gate 	 * If the ns_addr_list in the resolver context has changed, then
3320Sstevel@tonic-gate 	 * invalidate our cached copy and the associated timing data.
3330Sstevel@tonic-gate 	 */
3340Sstevel@tonic-gate 	if (EXT(statp).nscount != 0) {
3350Sstevel@tonic-gate 		int needclose = 0;
3360Sstevel@tonic-gate 		struct sockaddr_storage peer;
3370Sstevel@tonic-gate 		ISC_SOCKLEN_T peerlen;
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 		if (EXT(statp).nscount != statp->nscount)
3400Sstevel@tonic-gate 			needclose++;
3410Sstevel@tonic-gate 		else
3420Sstevel@tonic-gate 			for (ns = 0; ns < statp->nscount; ns++) {
3430Sstevel@tonic-gate 				if (statp->nsaddr_list[ns].sin_family &&
3440Sstevel@tonic-gate 				    !sock_eq((struct sockaddr *)&statp->nsaddr_list[ns],
3450Sstevel@tonic-gate 					     (struct sockaddr *)&EXT(statp).ext->nsaddrs[ns])) {
3460Sstevel@tonic-gate 					needclose++;
3470Sstevel@tonic-gate 					break;
3480Sstevel@tonic-gate 				}
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 				if (EXT(statp).nssocks[ns] == -1)
3510Sstevel@tonic-gate 					continue;
3520Sstevel@tonic-gate 				peerlen = sizeof(peer);
353*11038SRao.Shoaib@Sun.COM 				if (getpeername(EXT(statp).nssocks[ns],
3540Sstevel@tonic-gate 				    (struct sockaddr *)&peer, &peerlen) < 0) {
3550Sstevel@tonic-gate 					needclose++;
3560Sstevel@tonic-gate 					break;
3570Sstevel@tonic-gate 				}
3580Sstevel@tonic-gate 				if (!sock_eq((struct sockaddr *)&peer,
3590Sstevel@tonic-gate 				    get_nsaddr(statp, ns))) {
3600Sstevel@tonic-gate 					needclose++;
3610Sstevel@tonic-gate 					break;
3620Sstevel@tonic-gate 				}
3630Sstevel@tonic-gate 			}
3640Sstevel@tonic-gate 		if (needclose) {
3650Sstevel@tonic-gate 			res_nclose(statp);
3660Sstevel@tonic-gate 			EXT(statp).nscount = 0;
3670Sstevel@tonic-gate 		}
3680Sstevel@tonic-gate 	}
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	/*
3710Sstevel@tonic-gate 	 * Maybe initialize our private copy of the ns_addr_list.
3720Sstevel@tonic-gate 	 */
3730Sstevel@tonic-gate 	if (EXT(statp).nscount == 0) {
3740Sstevel@tonic-gate 		for (ns = 0; ns < statp->nscount; ns++) {
3750Sstevel@tonic-gate 			EXT(statp).nstimes[ns] = RES_MAXTIME;
3760Sstevel@tonic-gate 			EXT(statp).nssocks[ns] = -1;
3770Sstevel@tonic-gate 			if (!statp->nsaddr_list[ns].sin_family)
3780Sstevel@tonic-gate 				continue;
3790Sstevel@tonic-gate 			EXT(statp).ext->nsaddrs[ns].sin =
3800Sstevel@tonic-gate 				 statp->nsaddr_list[ns];
3810Sstevel@tonic-gate 		}
3820Sstevel@tonic-gate 		EXT(statp).nscount = statp->nscount;
3830Sstevel@tonic-gate 	}
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 	/*
3860Sstevel@tonic-gate 	 * Some resolvers want to even out the load on their nameservers.
3870Sstevel@tonic-gate 	 * Note that RES_BLAST overrides RES_ROTATE.
3880Sstevel@tonic-gate 	 */
389*11038SRao.Shoaib@Sun.COM 	if ((statp->options & RES_ROTATE) != 0U &&
390*11038SRao.Shoaib@Sun.COM 	    (statp->options & RES_BLAST) == 0U) {
3910Sstevel@tonic-gate 		union res_sockaddr_union inu;
3920Sstevel@tonic-gate 		struct sockaddr_in ina;
3930Sstevel@tonic-gate 		int lastns = statp->nscount - 1;
3940Sstevel@tonic-gate 		int fd;
3950Sstevel@tonic-gate 		u_int16_t nstime;
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 		if (EXT(statp).ext != NULL)
3980Sstevel@tonic-gate 			inu = EXT(statp).ext->nsaddrs[0];
3990Sstevel@tonic-gate 		ina = statp->nsaddr_list[0];
4000Sstevel@tonic-gate 		fd = EXT(statp).nssocks[0];
4010Sstevel@tonic-gate 		nstime = EXT(statp).nstimes[0];
4020Sstevel@tonic-gate 		for (ns = 0; ns < lastns; ns++) {
4030Sstevel@tonic-gate 			if (EXT(statp).ext != NULL)
404*11038SRao.Shoaib@Sun.COM 				EXT(statp).ext->nsaddrs[ns] =
4050Sstevel@tonic-gate 					EXT(statp).ext->nsaddrs[ns + 1];
4060Sstevel@tonic-gate 			statp->nsaddr_list[ns] = statp->nsaddr_list[ns + 1];
4070Sstevel@tonic-gate 			EXT(statp).nssocks[ns] = EXT(statp).nssocks[ns + 1];
4080Sstevel@tonic-gate 			EXT(statp).nstimes[ns] = EXT(statp).nstimes[ns + 1];
4090Sstevel@tonic-gate 		}
4100Sstevel@tonic-gate 		if (EXT(statp).ext != NULL)
4110Sstevel@tonic-gate 			EXT(statp).ext->nsaddrs[lastns] = inu;
4120Sstevel@tonic-gate 		statp->nsaddr_list[lastns] = ina;
4130Sstevel@tonic-gate 		EXT(statp).nssocks[lastns] = fd;
4140Sstevel@tonic-gate 		EXT(statp).nstimes[lastns] = nstime;
4150Sstevel@tonic-gate 	}
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	/*
4180Sstevel@tonic-gate 	 * Send request, RETRY times, or until successful.
4190Sstevel@tonic-gate 	 */
420*11038SRao.Shoaib@Sun.COM 	for (tries = 0; tries < statp->retry; tries++) {
4210Sstevel@tonic-gate 	    for (ns = 0; ns < statp->nscount; ns++) {
4220Sstevel@tonic-gate 		struct sockaddr *nsap;
4230Sstevel@tonic-gate 		int nsaplen;
4240Sstevel@tonic-gate 		nsap = get_nsaddr(statp, ns);
4250Sstevel@tonic-gate 		nsaplen = get_salen(nsap);
4260Sstevel@tonic-gate 		statp->_flags &= ~RES_F_LASTMASK;
4270Sstevel@tonic-gate 		statp->_flags |= (ns << RES_F_LASTSHIFT);
4280Sstevel@tonic-gate  same_ns:
4290Sstevel@tonic-gate 		if (statp->qhook) {
4300Sstevel@tonic-gate 			int done = 0, loops = 0;
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 			do {
4330Sstevel@tonic-gate 				res_sendhookact act;
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 				act = (*statp->qhook)(&nsap, &buf, &buflen,
4360Sstevel@tonic-gate 						      ans, anssiz, &resplen);
4370Sstevel@tonic-gate 				switch (act) {
4380Sstevel@tonic-gate 				case res_goahead:
4390Sstevel@tonic-gate 					done = 1;
4400Sstevel@tonic-gate 					break;
4410Sstevel@tonic-gate 				case res_nextns:
4420Sstevel@tonic-gate 					res_nclose(statp);
4430Sstevel@tonic-gate 					goto next_ns;
4440Sstevel@tonic-gate 				case res_done:
4450Sstevel@tonic-gate 					return (resplen);
4460Sstevel@tonic-gate 				case res_modified:
4470Sstevel@tonic-gate 					/* give the hook another try */
4480Sstevel@tonic-gate 					if (++loops < 42) /*doug adams*/
4490Sstevel@tonic-gate 						break;
4500Sstevel@tonic-gate 					/*FALLTHROUGH*/
4510Sstevel@tonic-gate 				case res_error:
4520Sstevel@tonic-gate 					/*FALLTHROUGH*/
4530Sstevel@tonic-gate 				default:
4540Sstevel@tonic-gate 					goto fail;
4550Sstevel@tonic-gate 				}
4560Sstevel@tonic-gate 			} while (!done);
4570Sstevel@tonic-gate 		}
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 		Dprint(((statp->options & RES_DEBUG) &&
4600Sstevel@tonic-gate 			getnameinfo(nsap, nsaplen, abuf, sizeof(abuf),
4610Sstevel@tonic-gate 				    NULL, 0, niflags) == 0),
4620Sstevel@tonic-gate 		       (stdout, ";; Querying server (# %d) address = %s\n",
4630Sstevel@tonic-gate 			ns + 1, abuf));
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 		if (v_circuit) {
4670Sstevel@tonic-gate 			/* Use VC; at most one attempt per server. */
468*11038SRao.Shoaib@Sun.COM 			tries = statp->retry;
4690Sstevel@tonic-gate 			n = send_vc(statp, buf, buflen, ans, anssiz, &terrno,
4700Sstevel@tonic-gate 				    ns);
4710Sstevel@tonic-gate 			if (n < 0)
4720Sstevel@tonic-gate 				goto fail;
4730Sstevel@tonic-gate 			if (n == 0)
4740Sstevel@tonic-gate 				goto next_ns;
4750Sstevel@tonic-gate 			resplen = n;
4760Sstevel@tonic-gate 		} else {
4770Sstevel@tonic-gate 			/* Use datagrams. */
4780Sstevel@tonic-gate 			n = send_dg(statp, buf, buflen, ans, anssiz, &terrno,
479*11038SRao.Shoaib@Sun.COM 				    ns, tries, &v_circuit, &gotsomewhere);
4800Sstevel@tonic-gate 			if (n < 0)
4810Sstevel@tonic-gate 				goto fail;
4820Sstevel@tonic-gate 			if (n == 0)
4830Sstevel@tonic-gate 				goto next_ns;
4840Sstevel@tonic-gate 			if (v_circuit)
4850Sstevel@tonic-gate 				goto same_ns;
4860Sstevel@tonic-gate 			resplen = n;
4870Sstevel@tonic-gate 		}
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 		Dprint((statp->options & RES_DEBUG) ||
4900Sstevel@tonic-gate 		       ((statp->pfcode & RES_PRF_REPLY) &&
4910Sstevel@tonic-gate 			(statp->pfcode & RES_PRF_HEAD1)),
4920Sstevel@tonic-gate 		       (stdout, ";; got answer:\n"));
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 		DprintQ((statp->options & RES_DEBUG) ||
4950Sstevel@tonic-gate 			(statp->pfcode & RES_PRF_REPLY),
4960Sstevel@tonic-gate 			(stdout, "%s", ""),
4970Sstevel@tonic-gate 			ans, (resplen > anssiz) ? anssiz : resplen);
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 		/*
5000Sstevel@tonic-gate 		 * If we have temporarily opened a virtual circuit,
5010Sstevel@tonic-gate 		 * or if we haven't been asked to keep a socket open,
5020Sstevel@tonic-gate 		 * close the socket.
5030Sstevel@tonic-gate 		 */
504*11038SRao.Shoaib@Sun.COM 		if ((v_circuit && (statp->options & RES_USEVC) == 0U) ||
505*11038SRao.Shoaib@Sun.COM 		    (statp->options & RES_STAYOPEN) == 0U) {
5060Sstevel@tonic-gate 			res_nclose(statp);
5070Sstevel@tonic-gate 		}
5080Sstevel@tonic-gate 		if (statp->rhook) {
5090Sstevel@tonic-gate 			int done = 0, loops = 0;
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 			do {
5120Sstevel@tonic-gate 				res_sendhookact act;
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 				act = (*statp->rhook)(nsap, buf, buflen,
5150Sstevel@tonic-gate 						      ans, anssiz, &resplen);
5160Sstevel@tonic-gate 				switch (act) {
5170Sstevel@tonic-gate 				case res_goahead:
5180Sstevel@tonic-gate 				case res_done:
5190Sstevel@tonic-gate 					done = 1;
5200Sstevel@tonic-gate 					break;
5210Sstevel@tonic-gate 				case res_nextns:
5220Sstevel@tonic-gate 					res_nclose(statp);
5230Sstevel@tonic-gate 					goto next_ns;
5240Sstevel@tonic-gate 				case res_modified:
5250Sstevel@tonic-gate 					/* give the hook another try */
5260Sstevel@tonic-gate 					if (++loops < 42) /*doug adams*/
5270Sstevel@tonic-gate 						break;
5280Sstevel@tonic-gate 					/*FALLTHROUGH*/
5290Sstevel@tonic-gate 				case res_error:
5300Sstevel@tonic-gate 					/*FALLTHROUGH*/
5310Sstevel@tonic-gate 				default:
5320Sstevel@tonic-gate 					goto fail;
5330Sstevel@tonic-gate 				}
5340Sstevel@tonic-gate 			} while (!done);
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 		}
5370Sstevel@tonic-gate 		return (resplen);
5380Sstevel@tonic-gate  next_ns: ;
5390Sstevel@tonic-gate 	   } /*foreach ns*/
5400Sstevel@tonic-gate 	} /*foreach retry*/
5410Sstevel@tonic-gate 	res_nclose(statp);
5420Sstevel@tonic-gate 	if (!v_circuit) {
5430Sstevel@tonic-gate 		if (!gotsomewhere)
544*11038SRao.Shoaib@Sun.COM 			errno = ECONNREFUSED;	/*%< no nameservers found */
5450Sstevel@tonic-gate 		else
546*11038SRao.Shoaib@Sun.COM 			errno = ETIMEDOUT;	/*%< no answer obtained */
5470Sstevel@tonic-gate 	} else
5480Sstevel@tonic-gate 		errno = terrno;
5490Sstevel@tonic-gate 	return (-1);
5500Sstevel@tonic-gate  fail:
5510Sstevel@tonic-gate 	res_nclose(statp);
5520Sstevel@tonic-gate 	return (-1);
5530Sstevel@tonic-gate }
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate /* Private */
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate static int
get_salen(sa)5580Sstevel@tonic-gate get_salen(sa)
5590Sstevel@tonic-gate 	const struct sockaddr *sa;
5600Sstevel@tonic-gate {
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate #ifdef HAVE_SA_LEN
5630Sstevel@tonic-gate 	/* There are people do not set sa_len.  Be forgiving to them. */
5640Sstevel@tonic-gate 	if (sa->sa_len)
5650Sstevel@tonic-gate 		return (sa->sa_len);
5660Sstevel@tonic-gate #endif
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	if (sa->sa_family == AF_INET)
5690Sstevel@tonic-gate 		return (sizeof(struct sockaddr_in));
5700Sstevel@tonic-gate 	else if (sa->sa_family == AF_INET6)
5710Sstevel@tonic-gate 		return (sizeof(struct sockaddr_in6));
5720Sstevel@tonic-gate 	else
573*11038SRao.Shoaib@Sun.COM 		return (0);	/*%< unknown, die on connect */
5740Sstevel@tonic-gate }
5750Sstevel@tonic-gate 
576*11038SRao.Shoaib@Sun.COM /*%
5770Sstevel@tonic-gate  * pick appropriate nsaddr_list for use.  see res_init() for initialization.
5780Sstevel@tonic-gate  */
5790Sstevel@tonic-gate static struct sockaddr *
get_nsaddr(statp,n)5800Sstevel@tonic-gate get_nsaddr(statp, n)
5810Sstevel@tonic-gate 	res_state statp;
5820Sstevel@tonic-gate 	size_t n;
5830Sstevel@tonic-gate {
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 	if (!statp->nsaddr_list[n].sin_family && EXT(statp).ext) {
5860Sstevel@tonic-gate 		/*
5870Sstevel@tonic-gate 		 * - EXT(statp).ext->nsaddrs[n] holds an address that is larger
5880Sstevel@tonic-gate 		 *   than struct sockaddr, and
5890Sstevel@tonic-gate 		 * - user code did not update statp->nsaddr_list[n].
5900Sstevel@tonic-gate 		 */
5910Sstevel@tonic-gate 		return (struct sockaddr *)(void *)&EXT(statp).ext->nsaddrs[n];
5920Sstevel@tonic-gate 	} else {
5930Sstevel@tonic-gate 		/*
5940Sstevel@tonic-gate 		 * - user code updated statp->nsaddr_list[n], or
5950Sstevel@tonic-gate 		 * - statp->nsaddr_list[n] has the same content as
5960Sstevel@tonic-gate 		 *   EXT(statp).ext->nsaddrs[n].
5970Sstevel@tonic-gate 		 */
5980Sstevel@tonic-gate 		return (struct sockaddr *)(void *)&statp->nsaddr_list[n];
5990Sstevel@tonic-gate 	}
6000Sstevel@tonic-gate }
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate static int
send_vc(res_state statp,const u_char * buf,int buflen,u_char * ans,int anssiz,int * terrno,int ns)6030Sstevel@tonic-gate send_vc(res_state statp,
6040Sstevel@tonic-gate 	const u_char *buf, int buflen, u_char *ans, int anssiz,
6050Sstevel@tonic-gate 	int *terrno, int ns)
6060Sstevel@tonic-gate {
6070Sstevel@tonic-gate 	const HEADER *hp = (const HEADER *) buf;
6080Sstevel@tonic-gate 	HEADER *anhp = (HEADER *) ans;
6090Sstevel@tonic-gate 	struct sockaddr *nsap;
6100Sstevel@tonic-gate 	int nsaplen;
6110Sstevel@tonic-gate 	int truncating, connreset, resplen, n;
6120Sstevel@tonic-gate 	struct iovec iov[2];
6130Sstevel@tonic-gate 	u_short len;
6140Sstevel@tonic-gate 	u_char *cp;
6150Sstevel@tonic-gate 	void *tmp;
616*11038SRao.Shoaib@Sun.COM #ifdef SO_NOSIGPIPE
617*11038SRao.Shoaib@Sun.COM 	int on = 1;
618*11038SRao.Shoaib@Sun.COM #endif
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	nsap = get_nsaddr(statp, ns);
6210Sstevel@tonic-gate 	nsaplen = get_salen(nsap);
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	connreset = 0;
6240Sstevel@tonic-gate  same_ns:
6250Sstevel@tonic-gate 	truncating = 0;
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	/* Are we still talking to whom we want to talk to? */
6280Sstevel@tonic-gate 	if (statp->_vcsock >= 0 && (statp->_flags & RES_F_VC) != 0) {
6290Sstevel@tonic-gate 		struct sockaddr_storage peer;
6300Sstevel@tonic-gate 		ISC_SOCKLEN_T size = sizeof peer;
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 		if (getpeername(statp->_vcsock,
6330Sstevel@tonic-gate 				(struct sockaddr *)&peer, &size) < 0 ||
6340Sstevel@tonic-gate 		    !sock_eq((struct sockaddr *)&peer, nsap)) {
6350Sstevel@tonic-gate 			res_nclose(statp);
6360Sstevel@tonic-gate 			statp->_flags &= ~RES_F_VC;
6370Sstevel@tonic-gate 		}
6380Sstevel@tonic-gate 	}
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 	if (statp->_vcsock < 0 || (statp->_flags & RES_F_VC) == 0) {
6410Sstevel@tonic-gate 		if (statp->_vcsock >= 0)
6420Sstevel@tonic-gate 			res_nclose(statp);
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 		statp->_vcsock = socket(nsap->sa_family, SOCK_STREAM, 0);
6450Sstevel@tonic-gate 		if (statp->_vcsock > highestFD) {
6460Sstevel@tonic-gate 			res_nclose(statp);
6470Sstevel@tonic-gate 			errno = ENOTSOCK;
6480Sstevel@tonic-gate 		}
6490Sstevel@tonic-gate 		if (statp->_vcsock < 0) {
650*11038SRao.Shoaib@Sun.COM 			switch (errno) {
651*11038SRao.Shoaib@Sun.COM 			case EPROTONOSUPPORT:
652*11038SRao.Shoaib@Sun.COM #ifdef EPFNOSUPPORT
653*11038SRao.Shoaib@Sun.COM 			case EPFNOSUPPORT:
654*11038SRao.Shoaib@Sun.COM #endif
655*11038SRao.Shoaib@Sun.COM 			case EAFNOSUPPORT:
656*11038SRao.Shoaib@Sun.COM 				Perror(statp, stderr, "socket(vc)", errno);
657*11038SRao.Shoaib@Sun.COM 				return (0);
658*11038SRao.Shoaib@Sun.COM 			default:
659*11038SRao.Shoaib@Sun.COM 				*terrno = errno;
660*11038SRao.Shoaib@Sun.COM 				Perror(statp, stderr, "socket(vc)", errno);
661*11038SRao.Shoaib@Sun.COM 				return (-1);
662*11038SRao.Shoaib@Sun.COM 			}
6630Sstevel@tonic-gate 		}
664*11038SRao.Shoaib@Sun.COM #ifdef SO_NOSIGPIPE
665*11038SRao.Shoaib@Sun.COM 		/*
666*11038SRao.Shoaib@Sun.COM 		 * Disable generation of SIGPIPE when writing to a closed
667*11038SRao.Shoaib@Sun.COM 		 * socket.  Write should return -1 and set errno to EPIPE
668*11038SRao.Shoaib@Sun.COM 		 * instead.
669*11038SRao.Shoaib@Sun.COM 		 *
670*11038SRao.Shoaib@Sun.COM 		 * Push on even if setsockopt(SO_NOSIGPIPE) fails.
671*11038SRao.Shoaib@Sun.COM 		 */
672*11038SRao.Shoaib@Sun.COM 		(void)setsockopt(statp->_vcsock, SOL_SOCKET, SO_NOSIGPIPE, &on,
673*11038SRao.Shoaib@Sun.COM 				 sizeof(on));
674*11038SRao.Shoaib@Sun.COM #endif
6750Sstevel@tonic-gate 		errno = 0;
6760Sstevel@tonic-gate 		if (connect(statp->_vcsock, nsap, nsaplen) < 0) {
6770Sstevel@tonic-gate 			*terrno = errno;
6780Sstevel@tonic-gate 			Aerror(statp, stderr, "connect/vc", errno, nsap,
6790Sstevel@tonic-gate 			    nsaplen);
6800Sstevel@tonic-gate 			res_nclose(statp);
6810Sstevel@tonic-gate 			return (0);
6820Sstevel@tonic-gate 		}
6830Sstevel@tonic-gate 		statp->_flags |= RES_F_VC;
6840Sstevel@tonic-gate 	}
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 	/*
6870Sstevel@tonic-gate 	 * Send length & message
6880Sstevel@tonic-gate 	 */
6890Sstevel@tonic-gate 	ns_put16((u_short)buflen, (u_char*)&len);
6900Sstevel@tonic-gate 	iov[0] = evConsIovec(&len, INT16SZ);
6910Sstevel@tonic-gate 	DE_CONST(buf, tmp);
6920Sstevel@tonic-gate 	iov[1] = evConsIovec(tmp, buflen);
6930Sstevel@tonic-gate 	if (writev(statp->_vcsock, iov, 2) != (INT16SZ + buflen)) {
6940Sstevel@tonic-gate 		*terrno = errno;
6950Sstevel@tonic-gate 		Perror(statp, stderr, "write failed", errno);
6960Sstevel@tonic-gate 		res_nclose(statp);
6970Sstevel@tonic-gate 		return (0);
6980Sstevel@tonic-gate 	}
6990Sstevel@tonic-gate 	/*
7000Sstevel@tonic-gate 	 * Receive length & response
7010Sstevel@tonic-gate 	 */
7020Sstevel@tonic-gate  read_len:
7030Sstevel@tonic-gate 	cp = ans;
7040Sstevel@tonic-gate 	len = INT16SZ;
7050Sstevel@tonic-gate 	while ((n = read(statp->_vcsock, (char *)cp, (int)len)) > 0) {
7060Sstevel@tonic-gate 		cp += n;
707*11038SRao.Shoaib@Sun.COM 		if ((len -= n) == 0)
7080Sstevel@tonic-gate 			break;
7090Sstevel@tonic-gate 	}
7100Sstevel@tonic-gate 	if (n <= 0) {
7110Sstevel@tonic-gate 		*terrno = errno;
7120Sstevel@tonic-gate 		Perror(statp, stderr, "read failed", errno);
7130Sstevel@tonic-gate 		res_nclose(statp);
7140Sstevel@tonic-gate 		/*
7150Sstevel@tonic-gate 		 * A long running process might get its TCP
7160Sstevel@tonic-gate 		 * connection reset if the remote server was
7170Sstevel@tonic-gate 		 * restarted.  Requery the server instead of
7180Sstevel@tonic-gate 		 * trying a new one.  When there is only one
7190Sstevel@tonic-gate 		 * server, this means that a query might work
7200Sstevel@tonic-gate 		 * instead of failing.  We only allow one reset
7210Sstevel@tonic-gate 		 * per query to prevent looping.
7220Sstevel@tonic-gate 		 */
7230Sstevel@tonic-gate 		if (*terrno == ECONNRESET && !connreset) {
7240Sstevel@tonic-gate 			connreset = 1;
7250Sstevel@tonic-gate 			res_nclose(statp);
7260Sstevel@tonic-gate 			goto same_ns;
7270Sstevel@tonic-gate 		}
7280Sstevel@tonic-gate 		res_nclose(statp);
7290Sstevel@tonic-gate 		return (0);
7300Sstevel@tonic-gate 	}
7310Sstevel@tonic-gate 	resplen = ns_get16(ans);
7320Sstevel@tonic-gate 	if (resplen > anssiz) {
7330Sstevel@tonic-gate 		Dprint(statp->options & RES_DEBUG,
7340Sstevel@tonic-gate 		       (stdout, ";; response truncated\n")
7350Sstevel@tonic-gate 		       );
7360Sstevel@tonic-gate 		truncating = 1;
7370Sstevel@tonic-gate 		len = anssiz;
7380Sstevel@tonic-gate 	} else
7390Sstevel@tonic-gate 		len = resplen;
7400Sstevel@tonic-gate 	if (len < HFIXEDSZ) {
7410Sstevel@tonic-gate 		/*
7420Sstevel@tonic-gate 		 * Undersized message.
7430Sstevel@tonic-gate 		 */
7440Sstevel@tonic-gate 		Dprint(statp->options & RES_DEBUG,
7450Sstevel@tonic-gate 		       (stdout, ";; undersized: %d\n", len));
7460Sstevel@tonic-gate 		*terrno = EMSGSIZE;
7470Sstevel@tonic-gate 		res_nclose(statp);
7480Sstevel@tonic-gate 		return (0);
7490Sstevel@tonic-gate 	}
7500Sstevel@tonic-gate 	cp = ans;
7510Sstevel@tonic-gate 	while (len != 0 && (n = read(statp->_vcsock, (char *)cp, (int)len)) > 0){
7520Sstevel@tonic-gate 		cp += n;
7530Sstevel@tonic-gate 		len -= n;
7540Sstevel@tonic-gate 	}
7550Sstevel@tonic-gate 	if (n <= 0) {
7560Sstevel@tonic-gate 		*terrno = errno;
7570Sstevel@tonic-gate 		Perror(statp, stderr, "read(vc)", errno);
7580Sstevel@tonic-gate 		res_nclose(statp);
7590Sstevel@tonic-gate 		return (0);
7600Sstevel@tonic-gate 	}
7610Sstevel@tonic-gate 	if (truncating) {
7620Sstevel@tonic-gate 		/*
7630Sstevel@tonic-gate 		 * Flush rest of answer so connection stays in synch.
7640Sstevel@tonic-gate 		 */
7650Sstevel@tonic-gate 		anhp->tc = 1;
7660Sstevel@tonic-gate 		len = resplen - anssiz;
7670Sstevel@tonic-gate 		while (len != 0) {
7680Sstevel@tonic-gate 			char junk[PACKETSZ];
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 			n = read(statp->_vcsock, junk,
7710Sstevel@tonic-gate 				 (len > sizeof junk) ? sizeof junk : len);
7720Sstevel@tonic-gate 			if (n > 0)
7730Sstevel@tonic-gate 				len -= n;
7740Sstevel@tonic-gate 			else
7750Sstevel@tonic-gate 				break;
7760Sstevel@tonic-gate 		}
7770Sstevel@tonic-gate 	}
7780Sstevel@tonic-gate 	/*
7790Sstevel@tonic-gate 	 * If the calling applicating has bailed out of
7800Sstevel@tonic-gate 	 * a previous call and failed to arrange to have
7810Sstevel@tonic-gate 	 * the circuit closed or the server has got
7820Sstevel@tonic-gate 	 * itself confused, then drop the packet and
7830Sstevel@tonic-gate 	 * wait for the correct one.
7840Sstevel@tonic-gate 	 */
7850Sstevel@tonic-gate 	if (hp->id != anhp->id) {
7860Sstevel@tonic-gate 		DprintQ((statp->options & RES_DEBUG) ||
7870Sstevel@tonic-gate 			(statp->pfcode & RES_PRF_REPLY),
7880Sstevel@tonic-gate 			(stdout, ";; old answer (unexpected):\n"),
7890Sstevel@tonic-gate 			ans, (resplen > anssiz) ? anssiz: resplen);
7900Sstevel@tonic-gate 		goto read_len;
7910Sstevel@tonic-gate 	}
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 	/*
7940Sstevel@tonic-gate 	 * All is well, or the error is fatal.  Signal that the
7950Sstevel@tonic-gate 	 * next nameserver ought not be tried.
7960Sstevel@tonic-gate 	 */
7970Sstevel@tonic-gate 	return (resplen);
7980Sstevel@tonic-gate }
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate static int
send_dg(res_state statp,const u_char * buf,int buflen,u_char * ans,int anssiz,int * terrno,int ns,int tries,int * v_circuit,int * gotsomewhere)801*11038SRao.Shoaib@Sun.COM send_dg(res_state statp, const u_char *buf, int buflen, u_char *ans,
802*11038SRao.Shoaib@Sun.COM 	int anssiz, int *terrno, int ns, int tries, int *v_circuit,
803*11038SRao.Shoaib@Sun.COM 	int *gotsomewhere)
8040Sstevel@tonic-gate {
8050Sstevel@tonic-gate 	const HEADER *hp = (const HEADER *) buf;
8060Sstevel@tonic-gate 	HEADER *anhp = (HEADER *) ans;
8070Sstevel@tonic-gate 	const struct sockaddr *nsap;
8080Sstevel@tonic-gate 	int nsaplen;
8090Sstevel@tonic-gate 	struct timespec now, timeout, finish;
8100Sstevel@tonic-gate 	struct sockaddr_storage from;
8110Sstevel@tonic-gate 	ISC_SOCKLEN_T fromlen;
8120Sstevel@tonic-gate 	int resplen, seconds, n, s;
813*11038SRao.Shoaib@Sun.COM #ifdef USE_POLL
814*11038SRao.Shoaib@Sun.COM 	int     polltimeout;
8150Sstevel@tonic-gate 	struct pollfd   pollfd;
816*11038SRao.Shoaib@Sun.COM #else
817*11038SRao.Shoaib@Sun.COM 	fd_set dsmask;
8180Sstevel@tonic-gate #endif
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 	nsap = get_nsaddr(statp, ns);
8210Sstevel@tonic-gate 	nsaplen = get_salen(nsap);
8220Sstevel@tonic-gate 	if (EXT(statp).nssocks[ns] == -1) {
8230Sstevel@tonic-gate 		EXT(statp).nssocks[ns] = socket(nsap->sa_family, SOCK_DGRAM, 0);
8240Sstevel@tonic-gate 		if (EXT(statp).nssocks[ns] > highestFD) {
8250Sstevel@tonic-gate 			res_nclose(statp);
8260Sstevel@tonic-gate 			errno = ENOTSOCK;
8270Sstevel@tonic-gate 		}
8280Sstevel@tonic-gate 		if (EXT(statp).nssocks[ns] < 0) {
829*11038SRao.Shoaib@Sun.COM 			switch (errno) {
830*11038SRao.Shoaib@Sun.COM 			case EPROTONOSUPPORT:
831*11038SRao.Shoaib@Sun.COM #ifdef EPFNOSUPPORT
832*11038SRao.Shoaib@Sun.COM 			case EPFNOSUPPORT:
833*11038SRao.Shoaib@Sun.COM #endif
834*11038SRao.Shoaib@Sun.COM 			case EAFNOSUPPORT:
835*11038SRao.Shoaib@Sun.COM 				Perror(statp, stderr, "socket(dg)", errno);
836*11038SRao.Shoaib@Sun.COM 				return (0);
837*11038SRao.Shoaib@Sun.COM 			default:
838*11038SRao.Shoaib@Sun.COM 				*terrno = errno;
839*11038SRao.Shoaib@Sun.COM 				Perror(statp, stderr, "socket(dg)", errno);
840*11038SRao.Shoaib@Sun.COM 				return (-1);
841*11038SRao.Shoaib@Sun.COM 			}
8420Sstevel@tonic-gate 		}
8430Sstevel@tonic-gate #ifndef CANNOT_CONNECT_DGRAM
8440Sstevel@tonic-gate 		/*
8450Sstevel@tonic-gate 		 * On a 4.3BSD+ machine (client and server,
8460Sstevel@tonic-gate 		 * actually), sending to a nameserver datagram
8470Sstevel@tonic-gate 		 * port with no nameserver will cause an
8480Sstevel@tonic-gate 		 * ICMP port unreachable message to be returned.
8490Sstevel@tonic-gate 		 * If our datagram socket is "connected" to the
8500Sstevel@tonic-gate 		 * server, we get an ECONNREFUSED error on the next
8510Sstevel@tonic-gate 		 * socket operation, and select returns if the
8520Sstevel@tonic-gate 		 * error message is received.  We can thus detect
8530Sstevel@tonic-gate 		 * the absence of a nameserver without timing out.
8540Sstevel@tonic-gate 		 */
8550Sstevel@tonic-gate 		if (connect(EXT(statp).nssocks[ns], nsap, nsaplen) < 0) {
8560Sstevel@tonic-gate 			Aerror(statp, stderr, "connect(dg)", errno, nsap,
8570Sstevel@tonic-gate 			    nsaplen);
8580Sstevel@tonic-gate 			res_nclose(statp);
8590Sstevel@tonic-gate 			return (0);
8600Sstevel@tonic-gate 		}
8610Sstevel@tonic-gate #endif /* !CANNOT_CONNECT_DGRAM */
8620Sstevel@tonic-gate 		Dprint(statp->options & RES_DEBUG,
8630Sstevel@tonic-gate 		       (stdout, ";; new DG socket\n"))
8640Sstevel@tonic-gate 	}
8650Sstevel@tonic-gate 	s = EXT(statp).nssocks[ns];
8660Sstevel@tonic-gate #ifndef CANNOT_CONNECT_DGRAM
8670Sstevel@tonic-gate 	if (send(s, (const char*)buf, buflen, 0) != buflen) {
8680Sstevel@tonic-gate 		Perror(statp, stderr, "send", errno);
8690Sstevel@tonic-gate 		res_nclose(statp);
8700Sstevel@tonic-gate 		return (0);
8710Sstevel@tonic-gate 	}
8720Sstevel@tonic-gate #else /* !CANNOT_CONNECT_DGRAM */
8730Sstevel@tonic-gate 	if (sendto(s, (const char*)buf, buflen, 0, nsap, nsaplen) != buflen)
8740Sstevel@tonic-gate 	{
8750Sstevel@tonic-gate 		Aerror(statp, stderr, "sendto", errno, nsap, nsaplen);
8760Sstevel@tonic-gate 		res_nclose(statp);
8770Sstevel@tonic-gate 		return (0);
8780Sstevel@tonic-gate 	}
8790Sstevel@tonic-gate #endif /* !CANNOT_CONNECT_DGRAM */
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 	/*
8820Sstevel@tonic-gate 	 * Wait for reply.
8830Sstevel@tonic-gate 	 */
884*11038SRao.Shoaib@Sun.COM 	seconds = (statp->retrans << tries);
8850Sstevel@tonic-gate 	if (ns > 0)
8860Sstevel@tonic-gate 		seconds /= statp->nscount;
8870Sstevel@tonic-gate 	if (seconds <= 0)
8880Sstevel@tonic-gate 		seconds = 1;
8890Sstevel@tonic-gate 	now = evNowTime();
8900Sstevel@tonic-gate 	timeout = evConsTime(seconds, 0);
8910Sstevel@tonic-gate 	finish = evAddTime(now, timeout);
8920Sstevel@tonic-gate 	goto nonow;
8930Sstevel@tonic-gate  wait:
8940Sstevel@tonic-gate 	now = evNowTime();
8950Sstevel@tonic-gate  nonow:
896*11038SRao.Shoaib@Sun.COM #ifndef USE_POLL
897*11038SRao.Shoaib@Sun.COM 	FD_ZERO(&dsmask);
898*11038SRao.Shoaib@Sun.COM 	FD_SET(s, &dsmask);
899*11038SRao.Shoaib@Sun.COM 	if (evCmpTime(finish, now) > 0)
900*11038SRao.Shoaib@Sun.COM 		timeout = evSubTime(finish, now);
901*11038SRao.Shoaib@Sun.COM 	else
902*11038SRao.Shoaib@Sun.COM 		timeout = evConsTime(0, 0);
903*11038SRao.Shoaib@Sun.COM 	n = pselect(s + 1, &dsmask, NULL, NULL, &timeout, NULL);
904*11038SRao.Shoaib@Sun.COM #else
9050Sstevel@tonic-gate 	timeout = evSubTime(finish, now);
9060Sstevel@tonic-gate 	if (timeout.tv_sec < 0)
9070Sstevel@tonic-gate 		timeout = evConsTime(0, 0);
9080Sstevel@tonic-gate 	polltimeout = 1000*timeout.tv_sec +
9090Sstevel@tonic-gate 		timeout.tv_nsec/1000000;
9100Sstevel@tonic-gate 	pollfd.fd = s;
9110Sstevel@tonic-gate 	pollfd.events = POLLRDNORM;
9120Sstevel@tonic-gate 	n = poll(&pollfd, 1, polltimeout);
913*11038SRao.Shoaib@Sun.COM #endif /* USE_POLL */
914*11038SRao.Shoaib@Sun.COM 
9150Sstevel@tonic-gate 	if (n == 0) {
9160Sstevel@tonic-gate 		Dprint(statp->options & RES_DEBUG, (stdout, ";; timeout\n"));
9170Sstevel@tonic-gate 		*gotsomewhere = 1;
9180Sstevel@tonic-gate 		return (0);
9190Sstevel@tonic-gate 	}
9200Sstevel@tonic-gate 	if (n < 0) {
9210Sstevel@tonic-gate 		if (errno == EINTR)
9220Sstevel@tonic-gate 			goto wait;
923*11038SRao.Shoaib@Sun.COM #ifndef USE_POLL
924*11038SRao.Shoaib@Sun.COM 		Perror(statp, stderr, "select", errno);
9250Sstevel@tonic-gate #else
926*11038SRao.Shoaib@Sun.COM 		Perror(statp, stderr, "poll", errno);
927*11038SRao.Shoaib@Sun.COM #endif /* USE_POLL */
9280Sstevel@tonic-gate 		res_nclose(statp);
9290Sstevel@tonic-gate 		return (0);
9300Sstevel@tonic-gate 	}
9310Sstevel@tonic-gate 	errno = 0;
9320Sstevel@tonic-gate 	fromlen = sizeof(from);
9330Sstevel@tonic-gate 	resplen = recvfrom(s, (char*)ans, anssiz,0,
9340Sstevel@tonic-gate 			   (struct sockaddr *)&from, &fromlen);
9350Sstevel@tonic-gate 	if (resplen <= 0) {
9360Sstevel@tonic-gate 		Perror(statp, stderr, "recvfrom", errno);
9370Sstevel@tonic-gate 		res_nclose(statp);
9380Sstevel@tonic-gate 		return (0);
9390Sstevel@tonic-gate 	}
9400Sstevel@tonic-gate 	*gotsomewhere = 1;
9410Sstevel@tonic-gate 	if (resplen < HFIXEDSZ) {
9420Sstevel@tonic-gate 		/*
9430Sstevel@tonic-gate 		 * Undersized message.
9440Sstevel@tonic-gate 		 */
9450Sstevel@tonic-gate 		Dprint(statp->options & RES_DEBUG,
9460Sstevel@tonic-gate 		       (stdout, ";; undersized: %d\n",
9470Sstevel@tonic-gate 			resplen));
9480Sstevel@tonic-gate 		*terrno = EMSGSIZE;
9490Sstevel@tonic-gate 		res_nclose(statp);
9500Sstevel@tonic-gate 		return (0);
9510Sstevel@tonic-gate 	}
9520Sstevel@tonic-gate 	if (hp->id != anhp->id) {
9530Sstevel@tonic-gate 		/*
9540Sstevel@tonic-gate 		 * response from old query, ignore it.
9550Sstevel@tonic-gate 		 * XXX - potential security hazard could
9560Sstevel@tonic-gate 		 *	 be detected here.
9570Sstevel@tonic-gate 		 */
9580Sstevel@tonic-gate 		DprintQ((statp->options & RES_DEBUG) ||
9590Sstevel@tonic-gate 			(statp->pfcode & RES_PRF_REPLY),
9600Sstevel@tonic-gate 			(stdout, ";; old answer:\n"),
9610Sstevel@tonic-gate 			ans, (resplen > anssiz) ? anssiz : resplen);
9620Sstevel@tonic-gate 		goto wait;
9630Sstevel@tonic-gate 	}
9640Sstevel@tonic-gate 	if (!(statp->options & RES_INSECURE1) &&
9650Sstevel@tonic-gate 	    !res_ourserver_p(statp, (struct sockaddr *)&from)) {
9660Sstevel@tonic-gate 		/*
9670Sstevel@tonic-gate 		 * response from wrong server? ignore it.
9680Sstevel@tonic-gate 		 * XXX - potential security hazard could
9690Sstevel@tonic-gate 		 *	 be detected here.
9700Sstevel@tonic-gate 		 */
9710Sstevel@tonic-gate 		DprintQ((statp->options & RES_DEBUG) ||
9720Sstevel@tonic-gate 			(statp->pfcode & RES_PRF_REPLY),
9730Sstevel@tonic-gate 			(stdout, ";; not our server:\n"),
9740Sstevel@tonic-gate 			ans, (resplen > anssiz) ? anssiz : resplen);
9750Sstevel@tonic-gate 		goto wait;
9760Sstevel@tonic-gate 	}
9770Sstevel@tonic-gate #ifdef RES_USE_EDNS0
978*11038SRao.Shoaib@Sun.COM 	if (anhp->rcode == FORMERR && (statp->options & RES_USE_EDNS0) != 0U) {
9790Sstevel@tonic-gate 		/*
9800Sstevel@tonic-gate 		 * Do not retry if the server do not understand EDNS0.
9810Sstevel@tonic-gate 		 * The case has to be captured here, as FORMERR packet do not
9820Sstevel@tonic-gate 		 * carry query section, hence res_queriesmatch() returns 0.
9830Sstevel@tonic-gate 		 */
9840Sstevel@tonic-gate 		DprintQ(statp->options & RES_DEBUG,
9850Sstevel@tonic-gate 			(stdout, "server rejected query with EDNS0:\n"),
9860Sstevel@tonic-gate 			ans, (resplen > anssiz) ? anssiz : resplen);
9870Sstevel@tonic-gate 		/* record the error */
9880Sstevel@tonic-gate 		statp->_flags |= RES_F_EDNS0ERR;
9890Sstevel@tonic-gate 		res_nclose(statp);
9900Sstevel@tonic-gate 		return (0);
9910Sstevel@tonic-gate 	}
9920Sstevel@tonic-gate #endif
9930Sstevel@tonic-gate 	if (!(statp->options & RES_INSECURE2) &&
9940Sstevel@tonic-gate 	    !res_queriesmatch(buf, buf + buflen,
9950Sstevel@tonic-gate 			      ans, ans + anssiz)) {
9960Sstevel@tonic-gate 		/*
9970Sstevel@tonic-gate 		 * response contains wrong query? ignore it.
9980Sstevel@tonic-gate 		 * XXX - potential security hazard could
9990Sstevel@tonic-gate 		 *	 be detected here.
10000Sstevel@tonic-gate 		 */
10010Sstevel@tonic-gate 		DprintQ((statp->options & RES_DEBUG) ||
10020Sstevel@tonic-gate 			(statp->pfcode & RES_PRF_REPLY),
10030Sstevel@tonic-gate 			(stdout, ";; wrong query name:\n"),
10040Sstevel@tonic-gate 			ans, (resplen > anssiz) ? anssiz : resplen);
10050Sstevel@tonic-gate 		goto wait;
10060Sstevel@tonic-gate 	}
10070Sstevel@tonic-gate 	if (anhp->rcode == SERVFAIL ||
10080Sstevel@tonic-gate 	    anhp->rcode == NOTIMP ||
10090Sstevel@tonic-gate 	    anhp->rcode == REFUSED) {
10100Sstevel@tonic-gate 		DprintQ(statp->options & RES_DEBUG,
10110Sstevel@tonic-gate 			(stdout, "server rejected query:\n"),
10120Sstevel@tonic-gate 			ans, (resplen > anssiz) ? anssiz : resplen);
10130Sstevel@tonic-gate 		res_nclose(statp);
10140Sstevel@tonic-gate 		/* don't retry if called from dig */
10150Sstevel@tonic-gate 		if (!statp->pfcode)
10160Sstevel@tonic-gate 			return (0);
10170Sstevel@tonic-gate 	}
10180Sstevel@tonic-gate 	if (!(statp->options & RES_IGNTC) && anhp->tc) {
10190Sstevel@tonic-gate 		/*
10200Sstevel@tonic-gate 		 * To get the rest of answer,
10210Sstevel@tonic-gate 		 * use TCP with same server.
10220Sstevel@tonic-gate 		 */
10230Sstevel@tonic-gate 		Dprint(statp->options & RES_DEBUG,
10240Sstevel@tonic-gate 		       (stdout, ";; truncated answer\n"));
10250Sstevel@tonic-gate 		*v_circuit = 1;
10260Sstevel@tonic-gate 		res_nclose(statp);
10270Sstevel@tonic-gate 		return (1);
10280Sstevel@tonic-gate 	}
10290Sstevel@tonic-gate 	/*
10300Sstevel@tonic-gate 	 * All is well, or the error is fatal.  Signal that the
10310Sstevel@tonic-gate 	 * next nameserver ought not be tried.
10320Sstevel@tonic-gate 	 */
10330Sstevel@tonic-gate 	return (resplen);
10340Sstevel@tonic-gate }
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate static void
Aerror(const res_state statp,FILE * file,const char * string,int error,const struct sockaddr * address,int alen)10370Sstevel@tonic-gate Aerror(const res_state statp, FILE *file, const char *string, int error,
10380Sstevel@tonic-gate        const struct sockaddr *address, int alen)
10390Sstevel@tonic-gate {
10400Sstevel@tonic-gate 	int save = errno;
10410Sstevel@tonic-gate 	char hbuf[NI_MAXHOST];
10420Sstevel@tonic-gate 	char sbuf[NI_MAXSERV];
10430Sstevel@tonic-gate 
10440Sstevel@tonic-gate 	alen = alen;
10450Sstevel@tonic-gate 
1046*11038SRao.Shoaib@Sun.COM 	if ((statp->options & RES_DEBUG) != 0U) {
10470Sstevel@tonic-gate 		if (getnameinfo(address, alen, hbuf, sizeof(hbuf),
10480Sstevel@tonic-gate 		    sbuf, sizeof(sbuf), niflags)) {
10490Sstevel@tonic-gate 			strncpy(hbuf, "?", sizeof(hbuf) - 1);
10500Sstevel@tonic-gate 			hbuf[sizeof(hbuf) - 1] = '\0';
10510Sstevel@tonic-gate 			strncpy(sbuf, "?", sizeof(sbuf) - 1);
10520Sstevel@tonic-gate 			sbuf[sizeof(sbuf) - 1] = '\0';
10530Sstevel@tonic-gate 		}
10540Sstevel@tonic-gate 		fprintf(file, "res_send: %s ([%s].%s): %s\n",
10550Sstevel@tonic-gate 			string, hbuf, sbuf, strerror(error));
10560Sstevel@tonic-gate 	}
10570Sstevel@tonic-gate 	errno = save;
10580Sstevel@tonic-gate }
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate static void
Perror(const res_state statp,FILE * file,const char * string,int error)10610Sstevel@tonic-gate Perror(const res_state statp, FILE *file, const char *string, int error) {
10620Sstevel@tonic-gate 	int save = errno;
10630Sstevel@tonic-gate 
1064*11038SRao.Shoaib@Sun.COM 	if ((statp->options & RES_DEBUG) != 0U)
10650Sstevel@tonic-gate 		fprintf(file, "res_send: %s: %s\n",
10660Sstevel@tonic-gate 			string, strerror(error));
10670Sstevel@tonic-gate 	errno = save;
10680Sstevel@tonic-gate }
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate static int
sock_eq(struct sockaddr * a,struct sockaddr * b)10710Sstevel@tonic-gate sock_eq(struct sockaddr *a, struct sockaddr *b) {
10720Sstevel@tonic-gate 	struct sockaddr_in *a4, *b4;
10730Sstevel@tonic-gate 	struct sockaddr_in6 *a6, *b6;
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 	if (a->sa_family != b->sa_family)
10760Sstevel@tonic-gate 		return 0;
10770Sstevel@tonic-gate 	switch (a->sa_family) {
10780Sstevel@tonic-gate 	case AF_INET:
10790Sstevel@tonic-gate 		a4 = (struct sockaddr_in *)a;
10800Sstevel@tonic-gate 		b4 = (struct sockaddr_in *)b;
10810Sstevel@tonic-gate 		return a4->sin_port == b4->sin_port &&
10820Sstevel@tonic-gate 		    a4->sin_addr.s_addr == b4->sin_addr.s_addr;
10830Sstevel@tonic-gate 	case AF_INET6:
10840Sstevel@tonic-gate 		a6 = (struct sockaddr_in6 *)a;
10850Sstevel@tonic-gate 		b6 = (struct sockaddr_in6 *)b;
10860Sstevel@tonic-gate 		return a6->sin6_port == b6->sin6_port &&
10870Sstevel@tonic-gate #ifdef HAVE_SIN6_SCOPE_ID
10880Sstevel@tonic-gate 		    a6->sin6_scope_id == b6->sin6_scope_id &&
10890Sstevel@tonic-gate #endif
10900Sstevel@tonic-gate 		    IN6_ARE_ADDR_EQUAL(&a6->sin6_addr, &b6->sin6_addr);
10910Sstevel@tonic-gate 	default:
10920Sstevel@tonic-gate 		return 0;
10930Sstevel@tonic-gate 	}
10940Sstevel@tonic-gate }
10950Sstevel@tonic-gate 
1096*11038SRao.Shoaib@Sun.COM #if defined(NEED_PSELECT) && !defined(USE_POLL)
10970Sstevel@tonic-gate /* XXX needs to move to the porting library. */
10980Sstevel@tonic-gate static int
pselect(int nfds,void * rfds,void * wfds,void * efds,struct timespec * tsp,const sigset_t * sigmask)10990Sstevel@tonic-gate pselect(int nfds, void *rfds, void *wfds, void *efds,
11000Sstevel@tonic-gate 	struct timespec *tsp, const sigset_t *sigmask)
11010Sstevel@tonic-gate {
11020Sstevel@tonic-gate 	struct timeval tv, *tvp;
11030Sstevel@tonic-gate 	sigset_t sigs;
11040Sstevel@tonic-gate 	int n;
11050Sstevel@tonic-gate 
11060Sstevel@tonic-gate 	if (tsp) {
11070Sstevel@tonic-gate 		tvp = &tv;
11080Sstevel@tonic-gate 		tv = evTimeVal(*tsp);
11090Sstevel@tonic-gate 	} else
11100Sstevel@tonic-gate 		tvp = NULL;
11110Sstevel@tonic-gate 	if (sigmask)
11120Sstevel@tonic-gate 		sigprocmask(SIG_SETMASK, sigmask, &sigs);
11130Sstevel@tonic-gate 	n = select(nfds, rfds, wfds, efds, tvp);
11140Sstevel@tonic-gate 	if (sigmask)
11150Sstevel@tonic-gate 		sigprocmask(SIG_SETMASK, &sigs, NULL);
11160Sstevel@tonic-gate 	if (tsp)
11170Sstevel@tonic-gate 		*tsp = evTimeSpec(tv);
11180Sstevel@tonic-gate 	return (n);
11190Sstevel@tonic-gate }
11200Sstevel@tonic-gate #endif
1121