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