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