Home | History | Annotate | Line # | Download | only in resolv
res_send.c revision 1.24
      1 /*	$NetBSD: res_send.c,v 1.24 2012/03/20 17:44:18 matt Exp $	*/
      2 
      3 /*
      4  * Portions Copyright (C) 2004-2009  Internet Systems Consortium, Inc. ("ISC")
      5  * Portions Copyright (C) 1996-2003  Internet Software Consortium.
      6  *
      7  * Permission to use, copy, modify, and/or distribute this software for any
      8  * purpose with or without fee is hereby granted, provided that the above
      9  * copyright notice and this permission notice appear in all copies.
     10  *
     11  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
     12  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
     13  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
     14  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
     15  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
     16  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
     17  * PERFORMANCE OF THIS SOFTWARE.
     18  */
     19 
     20 /*
     21  * Copyright (c) 1985, 1989, 1993
     22  *    The Regents of the University of California.  All rights reserved.
     23  *
     24  * Redistribution and use in source and binary forms, with or without
     25  * modification, are permitted provided that the following conditions
     26  * are met:
     27  * 1. Redistributions of source code must retain the above copyright
     28  *    notice, this list of conditions and the following disclaimer.
     29  * 2. Redistributions in binary form must reproduce the above copyright
     30  *    notice, this list of conditions and the following disclaimer in the
     31  *    documentation and/or other materials provided with the distribution.
     32  * 3. All advertising materials mentioning features or use of this software
     33  *    must display the following acknowledgement:
     34  * 	This product includes software developed by the University of
     35  * 	California, Berkeley and its contributors.
     36  * 4. Neither the name of the University nor the names of its contributors
     37  *    may be used to endorse or promote products derived from this software
     38  *    without specific prior written permission.
     39  *
     40  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     50  * SUCH DAMAGE.
     51  */
     52 
     53 /*
     54  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
     55  *
     56  * Permission to use, copy, modify, and distribute this software for any
     57  * purpose with or without fee is hereby granted, provided that the above
     58  * copyright notice and this permission notice appear in all copies, and that
     59  * the name of Digital Equipment Corporation not be used in advertising or
     60  * publicity pertaining to distribution of the document or software without
     61  * specific, written prior permission.
     62  *
     63  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
     64  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
     65  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
     66  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
     67  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
     68  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
     69  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
     70  * SOFTWARE.
     71  */
     72 
     73 /*
     74  * Copyright (c) 2005 by Internet Systems Consortium, Inc. ("ISC")
     75  * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
     76  *
     77  * Permission to use, copy, modify, and distribute this software for any
     78  * purpose with or without fee is hereby granted, provided that the above
     79  * copyright notice and this permission notice appear in all copies.
     80  *
     81  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
     82  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     83  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
     84  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     85  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     86  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
     87  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     88  */
     89 
     90 #include <sys/cdefs.h>
     91 #if defined(LIBC_SCCS) && !defined(lint)
     92 #ifdef notdef
     93 static const char sccsid[] = "@(#)res_send.c	8.1 (Berkeley) 6/4/93";
     94 static const char rcsid[] = "Id: res_send.c,v 1.22 2009/01/22 23:49:23 tbox Exp";
     95 #else
     96 __RCSID("$NetBSD: res_send.c,v 1.24 2012/03/20 17:44:18 matt Exp $");
     97 #endif
     98 #endif /* LIBC_SCCS and not lint */
     99 
    100 /*! \file
    101  * \brief
    102  * Send query to name server and wait for reply.
    103  */
    104 
    105 #include "namespace.h"
    106 #include "port_before.h"
    107 #include "fd_setsize.h"
    108 
    109 #include <sys/types.h>
    110 #include <sys/param.h>
    111 #include <sys/time.h>
    112 #include <sys/socket.h>
    113 #include <sys/uio.h>
    114 
    115 #include <netinet/in.h>
    116 #include <arpa/nameser.h>
    117 #include <arpa/inet.h>
    118 
    119 #include <assert.h>
    120 #include <errno.h>
    121 #include <netdb.h>
    122 #include <resolv.h>
    123 #include <signal.h>
    124 #include <stdio.h>
    125 #include <stdlib.h>
    126 #include <string.h>
    127 #include <unistd.h>
    128 
    129 #include <isc/eventlib.h>
    130 
    131 #include "port_after.h"
    132 
    133 #if 0
    134 #ifdef __weak_alias
    135 __weak_alias(res_ourserver_p,__res_ourserver_p)
    136 __weak_alias(res_nameinquery,__res_nameinquery)
    137 __weak_alias(res_queriesmatch,__res_queriesmatch)
    138 __weak_alias(res_nsend,__res_nsend)
    139 #endif
    140 #endif
    141 
    142 
    143 #ifdef USE_POLL
    144 #ifdef HAVE_STROPTS_H
    145 #include <stropts.h>
    146 #endif
    147 #include <poll.h>
    148 #endif /* USE_POLL */
    149 
    150 /* Options.  Leave them on. */
    151 #ifndef DEBUG
    152 #define DEBUG
    153 #endif
    154 #include "res_debug.h"
    155 #include "res_private.h"
    156 
    157 #define EXT(res) ((res)->_u._ext)
    158 
    159 #ifndef USE_POLL
    160 static const int highestFD = FD_SETSIZE - 1;
    161 #endif
    162 
    163 /* Forward. */
    164 
    165 static int		get_salen(const struct sockaddr *);
    166 static struct sockaddr * get_nsaddr(res_state, size_t);
    167 static int		send_vc(res_state, const u_char *, int,
    168 				u_char *, int, int *, int);
    169 static int		send_dg(res_state, const u_char *, int,
    170 				u_char *, int, int *, int, int,
    171 				int *, int *);
    172 static void		Aerror(const res_state, FILE *, const char *, int,
    173 			       const struct sockaddr *, int);
    174 static void		Perror(const res_state, FILE *, const char *, int);
    175 static int		sock_eq(struct sockaddr *, struct sockaddr *);
    176 #if defined(NEED_PSELECT) && !defined(USE_POLL)
    177 static int		pselect(int, void *, void *, void *,
    178 				struct timespec *,
    179 				const sigset_t *);
    180 #endif
    181 void res_pquery(const res_state, const u_char *, int, FILE *);
    182 
    183 static const int niflags = NI_NUMERICHOST | NI_NUMERICSERV;
    184 
    185 /* Public. */
    186 
    187 /*%
    188  *	looks up "ina" in _res.ns_addr_list[]
    189  *
    190  * returns:
    191  *\li	0  : not found
    192  *\li	>0 : found
    193  *
    194  * author:
    195  *\li	paul vixie, 29may94
    196  */
    197 int
    198 res_ourserver_p(const res_state statp, const struct sockaddr *sa) {
    199 	const struct sockaddr_in *inp, *srv;
    200 	const struct sockaddr_in6 *in6p, *srv6;
    201 	int ns;
    202 
    203 	switch (sa->sa_family) {
    204 	case AF_INET:
    205 		inp = (const struct sockaddr_in *)(const void *)sa;
    206 		for (ns = 0;  ns < statp->nscount;  ns++) {
    207 			srv = (struct sockaddr_in *)(void *)get_nsaddr(statp, (size_t)ns);
    208 			if (srv->sin_family == inp->sin_family &&
    209 			    srv->sin_port == inp->sin_port &&
    210 			    (srv->sin_addr.s_addr == INADDR_ANY ||
    211 			     srv->sin_addr.s_addr == inp->sin_addr.s_addr))
    212 				return (1);
    213 		}
    214 		break;
    215 	case AF_INET6:
    216 		if (EXT(statp).ext == NULL)
    217 			break;
    218 		in6p = (const struct sockaddr_in6 *)(const void *)sa;
    219 		for (ns = 0;  ns < statp->nscount;  ns++) {
    220 			srv6 = (struct sockaddr_in6 *)(void *)get_nsaddr(statp, (size_t)ns);
    221 			if (srv6->sin6_family == in6p->sin6_family &&
    222 			    srv6->sin6_port == in6p->sin6_port &&
    223 #ifdef HAVE_SIN6_SCOPE_ID
    224 			    (srv6->sin6_scope_id == 0 ||
    225 			     srv6->sin6_scope_id == in6p->sin6_scope_id) &&
    226 #endif
    227 			    (IN6_IS_ADDR_UNSPECIFIED(&srv6->sin6_addr) ||
    228 			     IN6_ARE_ADDR_EQUAL(&srv6->sin6_addr, &in6p->sin6_addr)))
    229 				return (1);
    230 		}
    231 		break;
    232 	default:
    233 		break;
    234 	}
    235 	return (0);
    236 }
    237 
    238 /*%
    239  *	look for (name,type,class) in the query section of packet (buf,eom)
    240  *
    241  * requires:
    242  *\li	buf + HFIXEDSZ <= eom
    243  *
    244  * returns:
    245  *\li	-1 : format error
    246  *\li	0  : not found
    247  *\li	>0 : found
    248  *
    249  * author:
    250  *\li	paul vixie, 29may94
    251  */
    252 int
    253 res_nameinquery(const char *name, int type, int class,
    254 		const u_char *buf, const u_char *eom)
    255 {
    256 	const u_char *cp = buf + HFIXEDSZ;
    257 	int qdcount = ntohs(((const HEADER*)(const void *)buf)->qdcount);
    258 
    259 	while (qdcount-- > 0) {
    260 		char tname[MAXDNAME+1];
    261 		int n, ttype, tclass;
    262 
    263 		n = dn_expand(buf, eom, cp, tname, (int)sizeof tname);
    264 		if (n < 0)
    265 			return (-1);
    266 		cp += n;
    267 		if (cp + 2 * INT16SZ > eom)
    268 			return (-1);
    269 		ttype = ns_get16(cp); cp += INT16SZ;
    270 		tclass = ns_get16(cp); cp += INT16SZ;
    271 		if (ttype == type && tclass == class &&
    272 		    ns_samename(tname, name) == 1)
    273 			return (1);
    274 	}
    275 	return (0);
    276 }
    277 
    278 /*%
    279  *	is there a 1:1 mapping of (name,type,class)
    280  *	in (buf1,eom1) and (buf2,eom2)?
    281  *
    282  * returns:
    283  *\li	-1 : format error
    284  *\li	0  : not a 1:1 mapping
    285  *\li	>0 : is a 1:1 mapping
    286  *
    287  * author:
    288  *\li	paul vixie, 29may94
    289  */
    290 int
    291 res_queriesmatch(const u_char *buf1, const u_char *eom1,
    292 		 const u_char *buf2, const u_char *eom2)
    293 {
    294 	const u_char *cp = buf1 + HFIXEDSZ;
    295 	int qdcount = ntohs(((const HEADER*)(const void *)buf1)->qdcount);
    296 
    297 	if (buf1 + HFIXEDSZ > eom1 || buf2 + HFIXEDSZ > eom2)
    298 		return (-1);
    299 
    300 	/*
    301 	 * Only header section present in replies to
    302 	 * dynamic update packets.
    303 	 */
    304 	if ((((const HEADER *)(const void *)buf1)->opcode == ns_o_update) &&
    305 	    (((const HEADER *)(const void *)buf2)->opcode == ns_o_update))
    306 		return (1);
    307 
    308 	if (qdcount != ntohs(((const HEADER*)(const void *)buf2)->qdcount))
    309 		return (0);
    310 	while (qdcount-- > 0) {
    311 		char tname[MAXDNAME+1];
    312 		int n, ttype, tclass;
    313 
    314 		n = dn_expand(buf1, eom1, cp, tname, (int)sizeof tname);
    315 		if (n < 0)
    316 			return (-1);
    317 		cp += n;
    318 		if (cp + 2 * INT16SZ > eom1)
    319 			return (-1);
    320 		ttype = ns_get16(cp);	cp += INT16SZ;
    321 		tclass = ns_get16(cp); cp += INT16SZ;
    322 		if (!res_nameinquery(tname, ttype, tclass, buf2, eom2))
    323 			return (0);
    324 	}
    325 	return (1);
    326 }
    327 
    328 int
    329 res_nsend(res_state statp,
    330 	  const u_char *buf, int buflen, u_char *ans, int anssiz)
    331 {
    332 	int gotsomewhere, terrno, tries, v_circuit, resplen, ns, n;
    333 	char abuf[NI_MAXHOST];
    334 
    335 	(void)res_check(statp, NULL);
    336 
    337 	/* No name servers or res_init() failure */
    338 	if (statp->nscount == 0 || EXT(statp).ext == NULL) {
    339 		errno = ESRCH;
    340 		return (-1);
    341 	}
    342 	if (anssiz < HFIXEDSZ) {
    343 		errno = EINVAL;
    344 		return (-1);
    345 	}
    346 	DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_QUERY),
    347 		(stdout, ";; res_send()\n"), buf, buflen);
    348 	v_circuit = (statp->options & RES_USEVC) || buflen > PACKETSZ;
    349 	gotsomewhere = 0;
    350 	terrno = ETIMEDOUT;
    351 
    352 	/*
    353 	 * If the ns_addr_list in the resolver context has changed, then
    354 	 * invalidate our cached copy and the associated timing data.
    355 	 */
    356 	if (EXT(statp).nscount != 0) {
    357 		int needclose = 0;
    358 		struct sockaddr_storage peer;
    359 		ISC_SOCKLEN_T peerlen;
    360 
    361 		if (EXT(statp).nscount != statp->nscount)
    362 			needclose++;
    363 		else
    364 			for (ns = 0; ns < statp->nscount; ns++) {
    365 				if (statp->nsaddr_list[ns].sin_family &&
    366 				    !sock_eq((struct sockaddr *)(void *)&statp->nsaddr_list[ns],
    367 					     (struct sockaddr *)(void *)&EXT(statp).ext->nsaddrs[ns])) {
    368 					needclose++;
    369 					break;
    370 				}
    371 
    372 				if (EXT(statp).nssocks[ns] == -1)
    373 					continue;
    374 				peerlen = sizeof(peer);
    375 				if (getpeername(EXT(statp).nssocks[ns],
    376 				    (struct sockaddr *)(void *)&peer, &peerlen) < 0) {
    377 					needclose++;
    378 					break;
    379 				}
    380 				if (!sock_eq((struct sockaddr *)(void *)&peer,
    381 				    get_nsaddr(statp, (size_t)ns))) {
    382 					needclose++;
    383 					break;
    384 				}
    385 			}
    386 		if (needclose) {
    387 			res_nclose(statp);
    388 			EXT(statp).nscount = 0;
    389 		}
    390 	}
    391 
    392 	/*
    393 	 * Maybe initialize our private copy of the ns_addr_list.
    394 	 */
    395 	if (EXT(statp).nscount == 0) {
    396 		for (ns = 0; ns < statp->nscount; ns++) {
    397 			EXT(statp).nstimes[ns] = RES_MAXTIME;
    398 			EXT(statp).nssocks[ns] = -1;
    399 			if (!statp->nsaddr_list[ns].sin_family)
    400 				continue;
    401 			EXT(statp).ext->nsaddrs[ns].sin =
    402 				 statp->nsaddr_list[ns];
    403 		}
    404 		EXT(statp).nscount = statp->nscount;
    405 	}
    406 
    407 	/*
    408 	 * Some resolvers want to even out the load on their nameservers.
    409 	 * Note that RES_BLAST overrides RES_ROTATE.
    410 	 */
    411 	if ((statp->options & RES_ROTATE) != 0U &&
    412 	    (statp->options & RES_BLAST) == 0U) {
    413 		union res_sockaddr_union inu;
    414 		struct sockaddr_in ina;
    415 		int lastns = statp->nscount - 1;
    416 		int fd;
    417 		u_int16_t nstime;
    418 
    419 		if (EXT(statp).ext != NULL)
    420 			inu = EXT(statp).ext->nsaddrs[0];
    421 		ina = statp->nsaddr_list[0];
    422 		fd = EXT(statp).nssocks[0];
    423 		nstime = EXT(statp).nstimes[0];
    424 		for (ns = 0; ns < lastns; ns++) {
    425 			if (EXT(statp).ext != NULL)
    426 				EXT(statp).ext->nsaddrs[ns] =
    427 					EXT(statp).ext->nsaddrs[ns + 1];
    428 			statp->nsaddr_list[ns] = statp->nsaddr_list[ns + 1];
    429 			EXT(statp).nssocks[ns] = EXT(statp).nssocks[ns + 1];
    430 			EXT(statp).nstimes[ns] = EXT(statp).nstimes[ns + 1];
    431 		}
    432 		if (EXT(statp).ext != NULL)
    433 			EXT(statp).ext->nsaddrs[lastns] = inu;
    434 		statp->nsaddr_list[lastns] = ina;
    435 		EXT(statp).nssocks[lastns] = fd;
    436 		EXT(statp).nstimes[lastns] = nstime;
    437 	}
    438 
    439 	/*
    440 	 * Send request, RETRY times, or until successful.
    441 	 */
    442 	for (tries = 0; tries < statp->retry; tries++) {
    443 	    for (ns = 0; ns < statp->nscount; ns++) {
    444 		struct sockaddr *nsap;
    445 		int nsaplen;
    446 		nsap = get_nsaddr(statp, (size_t)ns);
    447 		nsaplen = get_salen(nsap);
    448 		statp->_flags &= ~RES_F_LASTMASK;
    449 		statp->_flags |= (ns << RES_F_LASTSHIFT);
    450  same_ns:
    451 		if (statp->qhook) {
    452 			int done = 0, loops = 0;
    453 
    454 			do {
    455 				res_sendhookact act;
    456 
    457 				act = (*statp->qhook)(&nsap, &buf, &buflen,
    458 						      ans, anssiz, &resplen);
    459 				switch (act) {
    460 				case res_goahead:
    461 					done = 1;
    462 					break;
    463 				case res_nextns:
    464 					res_nclose(statp);
    465 					goto next_ns;
    466 				case res_done:
    467 					return (resplen);
    468 				case res_modified:
    469 					/* give the hook another try */
    470 					if (++loops < 42) /*doug adams*/
    471 						break;
    472 					/*FALLTHROUGH*/
    473 				case res_error:
    474 					/*FALLTHROUGH*/
    475 				default:
    476 					goto fail;
    477 				}
    478 			} while (!done);
    479 		}
    480 
    481 		Dprint(((statp->options & RES_DEBUG) &&
    482 			getnameinfo(nsap, (socklen_t)nsaplen, abuf,
    483 			    (socklen_t)sizeof(abuf), NULL, 0, niflags) == 0),
    484 		       (stdout, ";; Querying server (# %d) address = %s\n",
    485 			ns + 1, abuf));
    486 
    487 
    488 		if (v_circuit) {
    489 			/* Use VC; at most one attempt per server. */
    490 			tries = statp->retry;
    491 			n = send_vc(statp, buf, buflen, ans, anssiz, &terrno,
    492 				    ns);
    493 			if (n < 0)
    494 				goto fail;
    495 			if (n == 0)
    496 				goto next_ns;
    497 			resplen = n;
    498 		} else {
    499 			/* Use datagrams. */
    500 			n = send_dg(statp, buf, buflen, ans, anssiz, &terrno,
    501 				    ns, tries, &v_circuit, &gotsomewhere);
    502 			if (n < 0)
    503 				goto fail;
    504 			if (n == 0)
    505 				goto next_ns;
    506 			if (v_circuit)
    507 				goto same_ns;
    508 			resplen = n;
    509 		}
    510 
    511 		Dprint((statp->options & RES_DEBUG) ||
    512 		       ((statp->pfcode & RES_PRF_REPLY) &&
    513 			(statp->pfcode & RES_PRF_HEAD1)),
    514 		       (stdout, ";; got answer:\n"));
    515 
    516 		DprintQ((statp->options & RES_DEBUG) ||
    517 			(statp->pfcode & RES_PRF_REPLY),
    518 			(stdout, "%s", ""),
    519 			ans, (resplen > anssiz) ? anssiz : resplen);
    520 
    521 		/*
    522 		 * If we have temporarily opened a virtual circuit,
    523 		 * or if we haven't been asked to keep a socket open,
    524 		 * close the socket.
    525 		 */
    526 		if ((v_circuit && (statp->options & RES_USEVC) == 0U) ||
    527 		    (statp->options & RES_STAYOPEN) == 0U) {
    528 			res_nclose(statp);
    529 		}
    530 		if (statp->rhook) {
    531 			int done = 0, loops = 0;
    532 
    533 			do {
    534 				res_sendhookact act;
    535 
    536 				act = (*statp->rhook)(nsap, buf, buflen,
    537 						      ans, anssiz, &resplen);
    538 				switch (act) {
    539 				case res_goahead:
    540 				case res_done:
    541 					done = 1;
    542 					break;
    543 				case res_nextns:
    544 					res_nclose(statp);
    545 					goto next_ns;
    546 				case res_modified:
    547 					/* give the hook another try */
    548 					if (++loops < 42) /*doug adams*/
    549 						break;
    550 					/*FALLTHROUGH*/
    551 				case res_error:
    552 					/*FALLTHROUGH*/
    553 				default:
    554 					goto fail;
    555 				}
    556 			} while (!done);
    557 
    558 		}
    559 		return (resplen);
    560  next_ns: ;
    561 	   } /*foreach ns*/
    562 	} /*foreach retry*/
    563 	res_nclose(statp);
    564 	if (!v_circuit) {
    565 		if (!gotsomewhere)
    566 			errno = ECONNREFUSED;	/*%< no nameservers found */
    567 		else
    568 			errno = ETIMEDOUT;	/*%< no answer obtained */
    569 	} else
    570 		errno = terrno;
    571 	return (-1);
    572  fail:
    573 	res_nclose(statp);
    574 	return (-1);
    575 }
    576 
    577 /* Private */
    578 
    579 static int
    580 get_salen(const struct sockaddr *sa)
    581 {
    582 
    583 #ifdef HAVE_SA_LEN
    584 	/* There are people do not set sa_len.  Be forgiving to them. */
    585 	if (sa->sa_len)
    586 		return (sa->sa_len);
    587 #endif
    588 
    589 	if (sa->sa_family == AF_INET)
    590 		return (sizeof(struct sockaddr_in));
    591 	else if (sa->sa_family == AF_INET6)
    592 		return (sizeof(struct sockaddr_in6));
    593 	else
    594 		return (0);	/*%< unknown, die on connect */
    595 }
    596 
    597 /*%
    598  * pick appropriate nsaddr_list for use.  see res_init() for initialization.
    599  */
    600 static struct sockaddr *
    601 get_nsaddr(res_state statp, size_t n)
    602 {
    603 
    604 	if (!statp->nsaddr_list[n].sin_family && EXT(statp).ext) {
    605 		/*
    606 		 * - EXT(statp).ext->nsaddrs[n] holds an address that is larger
    607 		 *   than struct sockaddr, and
    608 		 * - user code did not update statp->nsaddr_list[n].
    609 		 */
    610 		return (struct sockaddr *)(void *)&EXT(statp).ext->nsaddrs[n];
    611 	} else {
    612 		/*
    613 		 * - user code updated statp->nsaddr_list[n], or
    614 		 * - statp->nsaddr_list[n] has the same content as
    615 		 *   EXT(statp).ext->nsaddrs[n].
    616 		 */
    617 		return (struct sockaddr *)(void *)&statp->nsaddr_list[n];
    618 	}
    619 }
    620 
    621 static int
    622 send_vc(res_state statp,
    623 	const u_char *buf, int buflen, u_char *ans, int anssiz,
    624 	int *terrno, int ns)
    625 {
    626 	const HEADER *hp = (const HEADER *)(const void *)buf;
    627 	HEADER *anhp = (HEADER *)(void *)ans;
    628 	struct sockaddr *nsap;
    629 	int nsaplen;
    630 	int truncating, connreset, resplen;
    631 	ssize_t n;
    632 	struct iovec iov[2];
    633 	u_short len;
    634 	u_char *cp;
    635 	void *tmp;
    636 #ifdef SO_NOSIGPIPE
    637 	int on = 1;
    638 #endif
    639 
    640 	nsap = get_nsaddr(statp, (size_t)ns);
    641 	nsaplen = get_salen(nsap);
    642 
    643 	connreset = 0;
    644  same_ns:
    645 	truncating = 0;
    646 
    647 	/* Are we still talking to whom we want to talk to? */
    648 	if (statp->_vcsock >= 0 && (statp->_flags & RES_F_VC) != 0) {
    649 		struct sockaddr_storage peer;
    650 		ISC_SOCKLEN_T size = sizeof peer;
    651 
    652 		if (getpeername(statp->_vcsock,
    653 				(struct sockaddr *)(void *)&peer, &size) < 0 ||
    654 		    !sock_eq((struct sockaddr *)(void *)&peer, nsap)) {
    655 			res_nclose(statp);
    656 			statp->_flags &= ~RES_F_VC;
    657 		}
    658 	}
    659 
    660 	if (statp->_vcsock < 0 || (statp->_flags & RES_F_VC) == 0) {
    661 		if (statp->_vcsock >= 0)
    662 			res_nclose(statp);
    663 
    664 		statp->_vcsock = socket(nsap->sa_family, SOCK_STREAM, 0);
    665 #ifndef USE_POLL
    666 		if (statp->_vcsock > highestFD) {
    667 			res_nclose(statp);
    668 			errno = ENOTSOCK;
    669 		}
    670 #endif
    671 		if (statp->_vcsock < 0) {
    672 			switch (errno) {
    673 			case EPROTONOSUPPORT:
    674 #ifdef EPFNOSUPPORT
    675 			case EPFNOSUPPORT:
    676 #endif
    677 			case EAFNOSUPPORT:
    678 				Perror(statp, stderr, "socket(vc)", errno);
    679 				return (0);
    680 			default:
    681 				*terrno = errno;
    682 				Perror(statp, stderr, "socket(vc)", errno);
    683 				return (-1);
    684 			}
    685 		}
    686 #ifdef SO_NOSIGPIPE
    687 		/*
    688 		 * Disable generation of SIGPIPE when writing to a closed
    689 		 * socket.  Write should return -1 and set errno to EPIPE
    690 		 * instead.
    691 		 *
    692 		 * Push on even if setsockopt(SO_NOSIGPIPE) fails.
    693 		 */
    694 		(void)setsockopt(statp->_vcsock, SOL_SOCKET, SO_NOSIGPIPE, &on,
    695 				 (unsigned int)sizeof(on));
    696 #endif
    697 		errno = 0;
    698 		if (connect(statp->_vcsock, nsap, (socklen_t)nsaplen) < 0) {
    699 			*terrno = errno;
    700 			Aerror(statp, stderr, "connect/vc", errno, nsap,
    701 			    nsaplen);
    702 			res_nclose(statp);
    703 			return (0);
    704 		}
    705 		statp->_flags |= RES_F_VC;
    706 	}
    707 
    708 	/*
    709 	 * Send length & message
    710 	 */
    711 	ns_put16((u_short)buflen, (u_char*)(void *)&len);
    712 	iov[0] = evConsIovec(&len, INT16SZ);
    713 	DE_CONST(buf, tmp);
    714 	iov[1] = evConsIovec(tmp, (size_t)buflen);
    715 	if (writev(statp->_vcsock, iov, 2) != (INT16SZ + buflen)) {
    716 		*terrno = errno;
    717 		Perror(statp, stderr, "write failed", errno);
    718 		res_nclose(statp);
    719 		return (0);
    720 	}
    721 	/*
    722 	 * Receive length & response
    723 	 */
    724  read_len:
    725 	cp = ans;
    726 	len = INT16SZ;
    727 	while ((n = read(statp->_vcsock, (char *)cp, (size_t)len)) > 0) {
    728 		cp += n;
    729 		if ((len -= (u_short)n) == 0)
    730 			break;
    731 	}
    732 	if (n <= 0) {
    733 		*terrno = errno;
    734 		Perror(statp, stderr, "read failed", errno);
    735 		res_nclose(statp);
    736 		/*
    737 		 * A long running process might get its TCP
    738 		 * connection reset if the remote server was
    739 		 * restarted.  Requery the server instead of
    740 		 * trying a new one.  When there is only one
    741 		 * server, this means that a query might work
    742 		 * instead of failing.  We only allow one reset
    743 		 * per query to prevent looping.
    744 		 */
    745 		if (*terrno == ECONNRESET && !connreset) {
    746 			connreset = 1;
    747 			res_nclose(statp);
    748 			goto same_ns;
    749 		}
    750 		res_nclose(statp);
    751 		return (0);
    752 	}
    753 	resplen = ns_get16(ans);
    754 	if (resplen > anssiz) {
    755 		Dprint(statp->options & RES_DEBUG,
    756 		       (stdout, ";; response truncated\n")
    757 		       );
    758 		truncating = 1;
    759 		len = anssiz;
    760 	} else
    761 		len = resplen;
    762 	if (len < HFIXEDSZ) {
    763 		/*
    764 		 * Undersized message.
    765 		 */
    766 		Dprint(statp->options & RES_DEBUG,
    767 		       (stdout, ";; undersized: %d\n", len));
    768 		*terrno = EMSGSIZE;
    769 		res_nclose(statp);
    770 		return (0);
    771 	}
    772 	cp = ans;
    773 	while (len != 0 && (n = read(statp->_vcsock, (char *)cp, (size_t)len)) > 0){
    774 		cp += n;
    775 		len -= (u_short)n;
    776 	}
    777 	if (n <= 0) {
    778 		*terrno = errno;
    779 		Perror(statp, stderr, "read(vc)", errno);
    780 		res_nclose(statp);
    781 		return (0);
    782 	}
    783 	if (truncating) {
    784 		/*
    785 		 * Flush rest of answer so connection stays in synch.
    786 		 */
    787 		anhp->tc = 1;
    788 		len = resplen - anssiz;
    789 		while (len != 0) {
    790 			char junk[PACKETSZ];
    791 
    792 			n = read(statp->_vcsock, junk,
    793 				 (len > sizeof junk) ? sizeof junk : len);
    794 			if (n > 0)
    795 				len -= (u_short)n;
    796 			else
    797 				break;
    798 		}
    799 	}
    800 	/*
    801 	 * If the calling applicating has bailed out of
    802 	 * a previous call and failed to arrange to have
    803 	 * the circuit closed or the server has got
    804 	 * itself confused, then drop the packet and
    805 	 * wait for the correct one.
    806 	 */
    807 	if (hp->id != anhp->id) {
    808 		DprintQ((statp->options & RES_DEBUG) ||
    809 			(statp->pfcode & RES_PRF_REPLY),
    810 			(stdout, ";; old answer (unexpected):\n"),
    811 			ans, (resplen > anssiz) ? anssiz: resplen);
    812 		goto read_len;
    813 	}
    814 
    815 	/*
    816 	 * All is well, or the error is fatal.  Signal that the
    817 	 * next nameserver ought not be tried.
    818 	 */
    819 	return (resplen);
    820 }
    821 
    822 static int
    823 send_dg(res_state statp, const u_char *buf, int buflen, u_char *ans,
    824 	int anssiz, int *terrno, int ns, int tries, int *v_circuit,
    825 	int *gotsomewhere)
    826 {
    827 	const HEADER *hp = (const HEADER *)(const void *)buf;
    828 	HEADER *anhp = (HEADER *)(void *)ans;
    829 	const struct sockaddr *nsap;
    830 	int nsaplen;
    831 	struct timespec now, timeout, finish;
    832 	struct sockaddr_storage from;
    833 	ISC_SOCKLEN_T fromlen;
    834 	ssize_t resplen;
    835 	int seconds, n, s;
    836 #ifdef USE_POLL
    837 	int     polltimeout;
    838 	struct pollfd   pollfd;
    839 #else
    840 	fd_set dsmask;
    841 #endif
    842 
    843 	nsap = get_nsaddr(statp, (size_t)ns);
    844 	nsaplen = get_salen(nsap);
    845 	if (EXT(statp).nssocks[ns] == -1) {
    846 		EXT(statp).nssocks[ns] = socket(nsap->sa_family, SOCK_DGRAM, 0);
    847 #ifndef USE_POLL
    848 		if (EXT(statp).nssocks[ns] > highestFD) {
    849 			res_nclose(statp);
    850 			errno = ENOTSOCK;
    851 		}
    852 #endif
    853 		if (EXT(statp).nssocks[ns] < 0) {
    854 			switch (errno) {
    855 			case EPROTONOSUPPORT:
    856 #ifdef EPFNOSUPPORT
    857 			case EPFNOSUPPORT:
    858 #endif
    859 			case EAFNOSUPPORT:
    860 				Perror(statp, stderr, "socket(dg)", errno);
    861 				return (0);
    862 			default:
    863 				*terrno = errno;
    864 				Perror(statp, stderr, "socket(dg)", errno);
    865 				return (-1);
    866 			}
    867 		}
    868 #ifndef CANNOT_CONNECT_DGRAM
    869 		/*
    870 		 * On a 4.3BSD+ machine (client and server,
    871 		 * actually), sending to a nameserver datagram
    872 		 * port with no nameserver will cause an
    873 		 * ICMP port unreachable message to be returned.
    874 		 * If our datagram socket is "connected" to the
    875 		 * server, we get an ECONNREFUSED error on the next
    876 		 * socket operation, and select returns if the
    877 		 * error message is received.  We can thus detect
    878 		 * the absence of a nameserver without timing out.
    879 		 */
    880 		if (connect(EXT(statp).nssocks[ns], nsap, (socklen_t)nsaplen) < 0) {
    881 			Aerror(statp, stderr, "connect(dg)", errno, nsap,
    882 			    nsaplen);
    883 			res_nclose(statp);
    884 			return (0);
    885 		}
    886 #endif /* !CANNOT_CONNECT_DGRAM */
    887 		Dprint(statp->options & RES_DEBUG,
    888 		       (stdout, ";; new DG socket\n"))
    889 	}
    890 	s = EXT(statp).nssocks[ns];
    891 #ifndef CANNOT_CONNECT_DGRAM
    892 	if (send(s, (const char*)buf, (size_t)buflen, 0) != buflen) {
    893 		Perror(statp, stderr, "send", errno);
    894 		res_nclose(statp);
    895 		return (0);
    896 	}
    897 #else /* !CANNOT_CONNECT_DGRAM */
    898 	if (sendto(s, (const char*)buf, buflen, 0, nsap, nsaplen) != buflen)
    899 	{
    900 		Aerror(statp, stderr, "sendto", errno, nsap, nsaplen);
    901 		res_nclose(statp);
    902 		return (0);
    903 	}
    904 #endif /* !CANNOT_CONNECT_DGRAM */
    905 
    906 	/*
    907 	 * Wait for reply.
    908 	 */
    909 	seconds = (statp->retrans << tries);
    910 	if (ns > 0)
    911 		seconds /= statp->nscount;
    912 	if (seconds <= 0)
    913 		seconds = 1;
    914 	now = evNowTime();
    915 	timeout = evConsTime((long)seconds, 0L);
    916 	finish = evAddTime(now, timeout);
    917 	goto nonow;
    918  wait:
    919 	now = evNowTime();
    920  nonow:
    921 #ifndef USE_POLL
    922 	FD_ZERO(&dsmask);
    923 	FD_SET(s, &dsmask);
    924 	if (evCmpTime(finish, now) > 0)
    925 		timeout = evSubTime(finish, now);
    926 	else
    927 		timeout = evConsTime(0L, 0L);
    928 	n = pselect(s + 1, &dsmask, NULL, NULL, &timeout, NULL);
    929 #else
    930 	timeout = evSubTime(finish, now);
    931 	if (timeout.tv_sec < 0)
    932 		timeout = evConsTime(0L, 0L);
    933 	polltimeout = 1000*(int)timeout.tv_sec +
    934 		(int)timeout.tv_nsec/1000000;
    935 	pollfd.fd = s;
    936 	pollfd.events = POLLRDNORM;
    937 	n = poll(&pollfd, 1, polltimeout);
    938 #endif /* USE_POLL */
    939 
    940 	if (n == 0) {
    941 		Dprint(statp->options & RES_DEBUG, (stdout, ";; timeout\n"));
    942 		*gotsomewhere = 1;
    943 		return (0);
    944 	}
    945 	if (n < 0) {
    946 		if (errno == EINTR)
    947 			goto wait;
    948 #ifndef USE_POLL
    949 		Perror(statp, stderr, "select", errno);
    950 #else
    951 		Perror(statp, stderr, "poll", errno);
    952 #endif /* USE_POLL */
    953 		res_nclose(statp);
    954 		return (0);
    955 	}
    956 	errno = 0;
    957 	fromlen = sizeof(from);
    958 	resplen = recvfrom(s, (char*)ans, (size_t)anssiz,0,
    959 			   (struct sockaddr *)(void *)&from, &fromlen);
    960 	if (resplen <= 0) {
    961 		Perror(statp, stderr, "recvfrom", errno);
    962 		res_nclose(statp);
    963 		return (0);
    964 	}
    965 	*gotsomewhere = 1;
    966 	if (resplen < HFIXEDSZ) {
    967 		/*
    968 		 * Undersized message.
    969 		 */
    970 		Dprint(statp->options & RES_DEBUG,
    971 		       (stdout, ";; undersized: %zd\n",
    972 			resplen));
    973 		*terrno = EMSGSIZE;
    974 		res_nclose(statp);
    975 		return (0);
    976 	}
    977 	if (hp->id != anhp->id) {
    978 		/*
    979 		 * response from old query, ignore it.
    980 		 * XXX - potential security hazard could
    981 		 *	 be detected here.
    982 		 */
    983 		DprintQ((statp->options & RES_DEBUG) ||
    984 			(statp->pfcode & RES_PRF_REPLY),
    985 			(stdout, ";; old answer:\n"),
    986 			ans, (resplen > anssiz) ? anssiz : resplen);
    987 		goto wait;
    988 	}
    989 	if (!(statp->options & RES_INSECURE1) &&
    990 	    !res_ourserver_p(statp, (struct sockaddr *)(void *)&from)) {
    991 		/*
    992 		 * response from wrong server? ignore it.
    993 		 * XXX - potential security hazard could
    994 		 *	 be detected here.
    995 		 */
    996 		DprintQ((statp->options & RES_DEBUG) ||
    997 			(statp->pfcode & RES_PRF_REPLY),
    998 			(stdout, ";; not our server:\n"),
    999 			ans, (resplen > anssiz) ? anssiz : resplen);
   1000 		goto wait;
   1001 	}
   1002 #ifdef RES_USE_EDNS0
   1003 	if (anhp->rcode == FORMERR && (statp->options & RES_USE_EDNS0) != 0U) {
   1004 		/*
   1005 		 * Do not retry if the server do not understand EDNS0.
   1006 		 * The case has to be captured here, as FORMERR packet do not
   1007 		 * carry query section, hence res_queriesmatch() returns 0.
   1008 		 */
   1009 		DprintQ(statp->options & RES_DEBUG,
   1010 			(stdout, "server rejected query with EDNS0:\n"),
   1011 			ans, (resplen > anssiz) ? anssiz : resplen);
   1012 		/* record the error */
   1013 		statp->_flags |= RES_F_EDNS0ERR;
   1014 		res_nclose(statp);
   1015 		return (0);
   1016 	}
   1017 #endif
   1018 	if (!(statp->options & RES_INSECURE2) &&
   1019 	    !res_queriesmatch(buf, buf + buflen,
   1020 			      ans, ans + anssiz)) {
   1021 		/*
   1022 		 * response contains wrong query? ignore it.
   1023 		 * XXX - potential security hazard could
   1024 		 *	 be detected here.
   1025 		 */
   1026 		DprintQ((statp->options & RES_DEBUG) ||
   1027 			(statp->pfcode & RES_PRF_REPLY),
   1028 			(stdout, ";; wrong query name:\n"),
   1029 			ans, (int)(resplen > anssiz) ? anssiz : resplen);
   1030 		goto wait;
   1031 	}
   1032 	if (anhp->rcode == SERVFAIL ||
   1033 	    anhp->rcode == NOTIMP ||
   1034 	    anhp->rcode == REFUSED) {
   1035 		DprintQ(statp->options & RES_DEBUG,
   1036 			(stdout, "server rejected query:\n"),
   1037 			ans, (int)(resplen > anssiz) ? anssiz : resplen);
   1038 		res_nclose(statp);
   1039 		/* don't retry if called from dig */
   1040 		if (!statp->pfcode)
   1041 			return (0);
   1042 	}
   1043 	if (!(statp->options & RES_IGNTC) && anhp->tc) {
   1044 		/*
   1045 		 * To get the rest of answer,
   1046 		 * use TCP with same server.
   1047 		 */
   1048 		Dprint(statp->options & RES_DEBUG,
   1049 		       (stdout, ";; truncated answer\n"));
   1050 		*v_circuit = 1;
   1051 		res_nclose(statp);
   1052 		return (1);
   1053 	}
   1054 	/*
   1055 	 * All is well, or the error is fatal.  Signal that the
   1056 	 * next nameserver ought not be tried.
   1057 	 */
   1058 	_DIAGASSERT(__type_fit(int, resplen));
   1059 	return (int)resplen;
   1060 }
   1061 
   1062 static void
   1063 Aerror(const res_state statp, FILE *file, const char *string, int error,
   1064        const struct sockaddr *address, int alen)
   1065 {
   1066 	int save = errno;
   1067 	char hbuf[NI_MAXHOST];
   1068 	char sbuf[NI_MAXSERV];
   1069 
   1070 	if ((statp->options & RES_DEBUG) != 0U) {
   1071 		if (getnameinfo(address, (socklen_t)alen, hbuf,
   1072 		    (socklen_t)sizeof(hbuf), sbuf, (socklen_t)sizeof(sbuf),
   1073 		    niflags)) {
   1074 			strncpy(hbuf, "?", sizeof(hbuf) - 1);
   1075 			hbuf[sizeof(hbuf) - 1] = '\0';
   1076 			strncpy(sbuf, "?", sizeof(sbuf) - 1);
   1077 			sbuf[sizeof(sbuf) - 1] = '\0';
   1078 		}
   1079 		fprintf(file, "res_send: %s ([%s].%s): %s\n",
   1080 			string, hbuf, sbuf, strerror(error));
   1081 	}
   1082 	errno = save;
   1083 }
   1084 
   1085 static void
   1086 Perror(const res_state statp, FILE *file, const char *string, int error) {
   1087 	int save = errno;
   1088 
   1089 	if ((statp->options & RES_DEBUG) != 0U)
   1090 		fprintf(file, "res_send: %s: %s\n",
   1091 			string, strerror(error));
   1092 	errno = save;
   1093 }
   1094 
   1095 static int
   1096 sock_eq(struct sockaddr *a, struct sockaddr *b) {
   1097 	struct sockaddr_in *a4, *b4;
   1098 	struct sockaddr_in6 *a6, *b6;
   1099 
   1100 	if (a->sa_family != b->sa_family)
   1101 		return 0;
   1102 	switch (a->sa_family) {
   1103 	case AF_INET:
   1104 		a4 = (struct sockaddr_in *)(void *)a;
   1105 		b4 = (struct sockaddr_in *)(void *)b;
   1106 		return a4->sin_port == b4->sin_port &&
   1107 		    a4->sin_addr.s_addr == b4->sin_addr.s_addr;
   1108 	case AF_INET6:
   1109 		a6 = (struct sockaddr_in6 *)(void *)a;
   1110 		b6 = (struct sockaddr_in6 *)(void *)b;
   1111 		return a6->sin6_port == b6->sin6_port &&
   1112 #ifdef HAVE_SIN6_SCOPE_ID
   1113 		    a6->sin6_scope_id == b6->sin6_scope_id &&
   1114 #endif
   1115 		    IN6_ARE_ADDR_EQUAL(&a6->sin6_addr, &b6->sin6_addr);
   1116 	default:
   1117 		return 0;
   1118 	}
   1119 }
   1120 
   1121 #if defined(NEED_PSELECT) && !defined(USE_POLL)
   1122 /* XXX needs to move to the porting library. */
   1123 static int
   1124 pselect(int nfds, void *rfds, void *wfds, void *efds,
   1125 	struct timespec *tsp, const sigset_t *sigmask)
   1126 {
   1127 	struct timeval tv, *tvp;
   1128 	sigset_t sigs;
   1129 	int n;
   1130 
   1131 	if (tsp) {
   1132 		tvp = &tv;
   1133 		tv = evTimeVal(*tsp);
   1134 	} else
   1135 		tvp = NULL;
   1136 	if (sigmask)
   1137 		sigprocmask(SIG_SETMASK, sigmask, &sigs);
   1138 	n = select(nfds, rfds, wfds, efds, tvp);
   1139 	if (sigmask)
   1140 		sigprocmask(SIG_SETMASK, &sigs, NULL);
   1141 	if (tsp)
   1142 		*tsp = evTimeSpec(tv);
   1143 	return (n);
   1144 }
   1145 #endif
   1146