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