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