xref: /freebsd/lib/libc/resolv/res_send.c (revision d056fa046c6a91b90cd98165face0e42a33a5173)
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.7 2005/08/15 02:04:41 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,
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 	if (statp->nscount == 0) {
317 		errno = ESRCH;
318 		return (-1);
319 	}
320 	if (anssiz < HFIXEDSZ) {
321 		errno = EINVAL;
322 		return (-1);
323 	}
324 	DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_QUERY),
325 		(stdout, ";; res_send()\n"), buf, buflen);
326 	v_circuit = (statp->options & RES_USEVC) || buflen > PACKETSZ;
327 	gotsomewhere = 0;
328 	terrno = ETIMEDOUT;
329 
330 #ifdef USE_KQUEUE
331 	if ((kq = kqueue()) < 0) {
332 		Perror(statp, stderr, "kqueue", errno);
333 		return (-1);
334 	}
335 #endif
336 
337 	/*
338 	 * If the ns_addr_list in the resolver context has changed, then
339 	 * invalidate our cached copy and the associated timing data.
340 	 */
341 	if (EXT(statp).nscount != 0) {
342 		int needclose = 0;
343 		struct sockaddr_storage peer;
344 		ISC_SOCKLEN_T peerlen;
345 
346 		if (EXT(statp).nscount != statp->nscount)
347 			needclose++;
348 		else
349 			for (ns = 0; ns < statp->nscount; ns++) {
350 				if (statp->nsaddr_list[ns].sin_family &&
351 				    !sock_eq((struct sockaddr *)&statp->nsaddr_list[ns],
352 					     (struct sockaddr *)&EXT(statp).ext->nsaddrs[ns])) {
353 					needclose++;
354 					break;
355 				}
356 
357 				if (EXT(statp).nssocks[ns] == -1)
358 					continue;
359 				peerlen = sizeof(peer);
360 				if (_getsockname(EXT(statp).nssocks[ns],
361 				    (struct sockaddr *)&peer, &peerlen) < 0) {
362 					needclose++;
363 					break;
364 				}
365 				if (!sock_eq((struct sockaddr *)&peer,
366 				    get_nsaddr(statp, ns))) {
367 					needclose++;
368 					break;
369 				}
370 			}
371 		if (needclose) {
372 			res_nclose(statp);
373 			EXT(statp).nscount = 0;
374 		}
375 	}
376 
377 	/*
378 	 * Maybe initialize our private copy of the ns_addr_list.
379 	 */
380 	if (EXT(statp).nscount == 0) {
381 		for (ns = 0; ns < statp->nscount; ns++) {
382 			EXT(statp).nstimes[ns] = RES_MAXTIME;
383 			EXT(statp).nssocks[ns] = -1;
384 			if (!statp->nsaddr_list[ns].sin_family)
385 				continue;
386 			EXT(statp).ext->nsaddrs[ns].sin =
387 				 statp->nsaddr_list[ns];
388 		}
389 		EXT(statp).nscount = statp->nscount;
390 	}
391 
392 	/*
393 	 * Some resolvers want to even out the load on their nameservers.
394 	 * Note that RES_BLAST overrides RES_ROTATE.
395 	 */
396 	if ((statp->options & RES_ROTATE) != 0U &&
397 	    (statp->options & RES_BLAST) == 0U) {
398 		union res_sockaddr_union inu;
399 		struct sockaddr_in ina;
400 		int lastns = statp->nscount - 1;
401 		int fd;
402 		u_int16_t nstime;
403 
404 		if (EXT(statp).ext != NULL)
405 			inu = EXT(statp).ext->nsaddrs[0];
406 		ina = statp->nsaddr_list[0];
407 		fd = EXT(statp).nssocks[0];
408 		nstime = EXT(statp).nstimes[0];
409 		for (ns = 0; ns < lastns; ns++) {
410 			if (EXT(statp).ext != NULL)
411                                 EXT(statp).ext->nsaddrs[ns] =
412 					EXT(statp).ext->nsaddrs[ns + 1];
413 			statp->nsaddr_list[ns] = statp->nsaddr_list[ns + 1];
414 			EXT(statp).nssocks[ns] = EXT(statp).nssocks[ns + 1];
415 			EXT(statp).nstimes[ns] = EXT(statp).nstimes[ns + 1];
416 		}
417 		if (EXT(statp).ext != NULL)
418 			EXT(statp).ext->nsaddrs[lastns] = inu;
419 		statp->nsaddr_list[lastns] = ina;
420 		EXT(statp).nssocks[lastns] = fd;
421 		EXT(statp).nstimes[lastns] = nstime;
422 	}
423 
424 	/*
425 	 * Send request, RETRY times, or until successful.
426 	 */
427 	for (try = 0; try < statp->retry; try++) {
428 	    for (ns = 0; ns < statp->nscount; ns++) {
429 		struct sockaddr *nsap;
430 		int nsaplen;
431 		nsap = get_nsaddr(statp, ns);
432 		nsaplen = get_salen(nsap);
433 		statp->_flags &= ~RES_F_LASTMASK;
434 		statp->_flags |= (ns << RES_F_LASTSHIFT);
435  same_ns:
436 		if (statp->qhook) {
437 			int done = 0, loops = 0;
438 
439 			do {
440 				res_sendhookact act;
441 
442 				act = (*statp->qhook)(&nsap, &buf, &buflen,
443 						      ans, anssiz, &resplen);
444 				switch (act) {
445 				case res_goahead:
446 					done = 1;
447 					break;
448 				case res_nextns:
449 					res_nclose(statp);
450 					goto next_ns;
451 				case res_done:
452 #ifdef USE_KQUEUE
453 					_close(kq);
454 #endif
455 					return (resplen);
456 				case res_modified:
457 					/* give the hook another try */
458 					if (++loops < 42) /*doug adams*/
459 						break;
460 					/*FALLTHROUGH*/
461 				case res_error:
462 					/*FALLTHROUGH*/
463 				default:
464 					goto fail;
465 				}
466 			} while (!done);
467 		}
468 
469 		Dprint(((statp->options & RES_DEBUG) &&
470 			getnameinfo(nsap, nsaplen, abuf, sizeof(abuf),
471 				    NULL, 0, niflags) == 0),
472 		       (stdout, ";; Querying server (# %d) address = %s\n",
473 			ns + 1, abuf));
474 
475 
476 		if (v_circuit) {
477 			/* Use VC; at most one attempt per server. */
478 			try = statp->retry;
479 			n = send_vc(statp, buf, buflen, ans, anssiz, &terrno,
480 				    ns);
481 			if (n < 0)
482 				goto fail;
483 			if (n == 0)
484 				goto next_ns;
485 			resplen = n;
486 		} else {
487 			/* Use datagrams. */
488 			n = send_dg(statp,
489 #ifdef USE_KQUEUE
490 				    kq,
491 #endif
492 				    buf, buflen, ans, anssiz, &terrno,
493 				    ns, &v_circuit, &gotsomewhere);
494 			if (n < 0)
495 				goto fail;
496 			if (n == 0)
497 				goto next_ns;
498 			if (v_circuit)
499 				goto same_ns;
500 			resplen = n;
501 		}
502 
503 		Dprint((statp->options & RES_DEBUG) ||
504 		       ((statp->pfcode & RES_PRF_REPLY) &&
505 			(statp->pfcode & RES_PRF_HEAD1)),
506 		       (stdout, ";; got answer:\n"));
507 
508 		DprintQ((statp->options & RES_DEBUG) ||
509 			(statp->pfcode & RES_PRF_REPLY),
510 			(stdout, "%s", ""),
511 			ans, (resplen > anssiz) ? anssiz : resplen);
512 
513 		/*
514 		 * If we have temporarily opened a virtual circuit,
515 		 * or if we haven't been asked to keep a socket open,
516 		 * close the socket.
517 		 */
518 		if ((v_circuit && (statp->options & RES_USEVC) == 0U) ||
519 		    (statp->options & RES_STAYOPEN) == 0U) {
520 			res_nclose(statp);
521 		}
522 		if (statp->rhook) {
523 			int done = 0, loops = 0;
524 
525 			do {
526 				res_sendhookact act;
527 
528 				act = (*statp->rhook)(nsap, buf, buflen,
529 						      ans, anssiz, &resplen);
530 				switch (act) {
531 				case res_goahead:
532 				case res_done:
533 					done = 1;
534 					break;
535 				case res_nextns:
536 					res_nclose(statp);
537 					goto next_ns;
538 				case res_modified:
539 					/* give the hook another try */
540 					if (++loops < 42) /*doug adams*/
541 						break;
542 					/*FALLTHROUGH*/
543 				case res_error:
544 					/*FALLTHROUGH*/
545 				default:
546 					goto fail;
547 				}
548 			} while (!done);
549 
550 		}
551 #ifdef USE_KQUEUE
552 		_close(kq);
553 #endif
554 		return (resplen);
555  next_ns: ;
556 	   } /*foreach ns*/
557 	} /*foreach retry*/
558 	res_nclose(statp);
559 #ifdef USE_KQUEUE
560 	_close(kq);
561 #endif
562 	if (!v_circuit) {
563 		if (!gotsomewhere)
564 			errno = ECONNREFUSED;	/* no nameservers found */
565 		else
566 			errno = ETIMEDOUT;	/* no answer obtained */
567 	} else
568 		errno = terrno;
569 	return (-1);
570  fail:
571 	res_nclose(statp);
572 #ifdef USE_KQUEUE
573 	_close(kq);
574 #endif
575 	return (-1);
576 }
577 
578 /* Private */
579 
580 static int
581 get_salen(sa)
582 	const struct sockaddr *sa;
583 {
584 
585 #ifdef HAVE_SA_LEN
586 	/* There are people do not set sa_len.  Be forgiving to them. */
587 	if (sa->sa_len)
588 		return (sa->sa_len);
589 #endif
590 
591 	if (sa->sa_family == AF_INET)
592 		return (sizeof(struct sockaddr_in));
593 	else if (sa->sa_family == AF_INET6)
594 		return (sizeof(struct sockaddr_in6));
595 	else
596 		return (0);	/* unknown, die on connect */
597 }
598 
599 /*
600  * pick appropriate nsaddr_list for use.  see res_init() for initialization.
601  */
602 static struct sockaddr *
603 get_nsaddr(statp, n)
604 	res_state statp;
605 	size_t n;
606 {
607 
608 	if (!statp->nsaddr_list[n].sin_family && EXT(statp).ext) {
609 		/*
610 		 * - EXT(statp).ext->nsaddrs[n] holds an address that is larger
611 		 *   than struct sockaddr, and
612 		 * - user code did not update statp->nsaddr_list[n].
613 		 */
614 		return (struct sockaddr *)(void *)&EXT(statp).ext->nsaddrs[n];
615 	} else {
616 		/*
617 		 * - user code updated statp->nsaddr_list[n], or
618 		 * - statp->nsaddr_list[n] has the same content as
619 		 *   EXT(statp).ext->nsaddrs[n].
620 		 */
621 		return (struct sockaddr *)(void *)&statp->nsaddr_list[n];
622 	}
623 }
624 
625 static int
626 send_vc(res_state statp,
627 	const u_char *buf, int buflen, u_char *ans, int anssiz,
628 	int *terrno, int ns)
629 {
630 	const HEADER *hp = (const HEADER *) buf;
631 	HEADER *anhp = (HEADER *) ans;
632 	struct sockaddr *nsap;
633 	int nsaplen;
634 	int truncating, connreset, resplen, n;
635 	struct iovec iov[2];
636 	u_short len;
637 	u_char *cp;
638 	void *tmp;
639 
640 	nsap = get_nsaddr(statp, ns);
641 	nsaplen = get_salen(nsap);
642 
643 	connreset = 0;
644  same_ns:
645 	truncating = 0;
646 
647 	/* Are we still talking to whom we want to talk to? */
648 	if (statp->_vcsock >= 0 && (statp->_flags & RES_F_VC) != 0) {
649 		struct sockaddr_storage peer;
650 		ISC_SOCKLEN_T size = sizeof peer;
651 
652 		if (_getpeername(statp->_vcsock,
653 				(struct sockaddr *)&peer, &size) < 0 ||
654 		    !sock_eq((struct sockaddr *)&peer, nsap)) {
655 			res_nclose(statp);
656 			statp->_flags &= ~RES_F_VC;
657 		}
658 	}
659 
660 	if (statp->_vcsock < 0 || (statp->_flags & RES_F_VC) == 0) {
661 		if (statp->_vcsock >= 0)
662 			res_nclose(statp);
663 
664 		statp->_vcsock = _socket(nsap->sa_family, SOCK_STREAM, 0);
665 		if (statp->_vcsock > highestFD) {
666 			res_nclose(statp);
667 			errno = ENOTSOCK;
668 		}
669 		if (statp->_vcsock < 0) {
670 			switch (errno) {
671 			case EPROTONOSUPPORT:
672 #ifdef EPFNOSUPPORT
673 			case EPFNOSUPPORT:
674 #endif
675 			case EAFNOSUPPORT:
676 				Perror(statp, stderr, "socket(vc)", errno);
677 				return (0);
678 			default:
679 				*terrno = errno;
680 				Perror(statp, stderr, "socket(vc)", errno);
681 				return (-1);
682 			}
683 		}
684 		errno = 0;
685 		if (_connect(statp->_vcsock, nsap, nsaplen) < 0) {
686 			*terrno = errno;
687 			Aerror(statp, stderr, "connect/vc", errno, nsap,
688 			    nsaplen);
689 			res_nclose(statp);
690 			return (0);
691 		}
692 		statp->_flags |= RES_F_VC;
693 	}
694 
695 	/*
696 	 * Send length & message
697 	 */
698 	ns_put16((u_short)buflen, (u_char*)&len);
699 	iov[0] = evConsIovec(&len, INT16SZ);
700 	DE_CONST(buf, tmp);
701 	iov[1] = evConsIovec(tmp, buflen);
702 	if (_writev(statp->_vcsock, iov, 2) != (INT16SZ + buflen)) {
703 		*terrno = errno;
704 		Perror(statp, stderr, "write failed", errno);
705 		res_nclose(statp);
706 		return (0);
707 	}
708 	/*
709 	 * Receive length & response
710 	 */
711  read_len:
712 	cp = ans;
713 	len = INT16SZ;
714 	while ((n = _read(statp->_vcsock, (char *)cp, (int)len)) > 0) {
715 		cp += n;
716 		if ((len -= n) == 0)
717 			break;
718 	}
719 	if (n <= 0) {
720 		*terrno = errno;
721 		Perror(statp, stderr, "read failed", errno);
722 		res_nclose(statp);
723 		/*
724 		 * A long running process might get its TCP
725 		 * connection reset if the remote server was
726 		 * restarted.  Requery the server instead of
727 		 * trying a new one.  When there is only one
728 		 * server, this means that a query might work
729 		 * instead of failing.  We only allow one reset
730 		 * per query to prevent looping.
731 		 */
732 		if (*terrno == ECONNRESET && !connreset) {
733 			connreset = 1;
734 			res_nclose(statp);
735 			goto same_ns;
736 		}
737 		res_nclose(statp);
738 		return (0);
739 	}
740 	resplen = ns_get16(ans);
741 	if (resplen > anssiz) {
742 		Dprint(statp->options & RES_DEBUG,
743 		       (stdout, ";; response truncated\n")
744 		       );
745 		truncating = 1;
746 		len = anssiz;
747 	} else
748 		len = resplen;
749 	if (len < HFIXEDSZ) {
750 		/*
751 		 * Undersized message.
752 		 */
753 		Dprint(statp->options & RES_DEBUG,
754 		       (stdout, ";; undersized: %d\n", len));
755 		*terrno = EMSGSIZE;
756 		res_nclose(statp);
757 		return (0);
758 	}
759 	cp = ans;
760 	while (len != 0 &&
761 	    (n = _read(statp->_vcsock, (char *)cp, (int)len)) > 0) {
762 		cp += n;
763 		len -= n;
764 	}
765 	if (n <= 0) {
766 		*terrno = errno;
767 		Perror(statp, stderr, "read(vc)", errno);
768 		res_nclose(statp);
769 		return (0);
770 	}
771 	if (truncating) {
772 		/*
773 		 * Flush rest of answer so connection stays in synch.
774 		 */
775 		anhp->tc = 1;
776 		len = resplen - anssiz;
777 		while (len != 0) {
778 			char junk[PACKETSZ];
779 
780 			n = _read(statp->_vcsock, junk,
781 			    (len > sizeof junk) ? sizeof junk : len);
782 			if (n > 0)
783 				len -= n;
784 			else
785 				break;
786 		}
787 	}
788 	/*
789 	 * If the calling applicating has bailed out of
790 	 * a previous call and failed to arrange to have
791 	 * the circuit closed or the server has got
792 	 * itself confused, then drop the packet and
793 	 * wait for the correct one.
794 	 */
795 	if (hp->id != anhp->id) {
796 		DprintQ((statp->options & RES_DEBUG) ||
797 			(statp->pfcode & RES_PRF_REPLY),
798 			(stdout, ";; old answer (unexpected):\n"),
799 			ans, (resplen > anssiz) ? anssiz: resplen);
800 		goto read_len;
801 	}
802 
803 	/*
804 	 * All is well, or the error is fatal.  Signal that the
805 	 * next nameserver ought not be tried.
806 	 */
807 	return (resplen);
808 }
809 
810 static int
811 send_dg(res_state statp,
812 #ifdef USE_KQUEUE
813 	int kq,
814 #endif
815 	const u_char *buf, int buflen, u_char *ans, int anssiz,
816 	int *terrno, int ns, int *v_circuit, int *gotsomewhere)
817 {
818 	const HEADER *hp = (const HEADER *) buf;
819 	HEADER *anhp = (HEADER *) ans;
820 	const struct sockaddr *nsap;
821 	int nsaplen;
822 	struct timespec now, timeout, finish;
823 	struct sockaddr_storage from;
824 	ISC_SOCKLEN_T fromlen;
825 	int resplen, seconds, n, s;
826 #ifdef USE_KQUEUE
827 	struct kevent kv;
828 #else
829 #ifdef USE_POLL
830 	int     polltimeout;
831 	struct pollfd   pollfd;
832 #else
833 	fd_set dsmask;
834 #endif
835 #endif
836 
837 	nsap = get_nsaddr(statp, ns);
838 	nsaplen = get_salen(nsap);
839 	if (EXT(statp).nssocks[ns] == -1) {
840 		EXT(statp).nssocks[ns] = _socket(nsap->sa_family,
841 		    SOCK_DGRAM, 0);
842 		if (EXT(statp).nssocks[ns] > highestFD) {
843 			res_nclose(statp);
844 			errno = ENOTSOCK;
845 		}
846 		if (EXT(statp).nssocks[ns] < 0) {
847 			switch (errno) {
848 			case EPROTONOSUPPORT:
849 #ifdef EPFNOSUPPORT
850 			case EPFNOSUPPORT:
851 #endif
852 			case EAFNOSUPPORT:
853 				Perror(statp, stderr, "socket(dg)", errno);
854 				return (0);
855 			default:
856 				*terrno = errno;
857 				Perror(statp, stderr, "socket(dg)", errno);
858 				return (-1);
859 			}
860 		}
861 #ifndef CANNOT_CONNECT_DGRAM
862 		/*
863 		 * On a 4.3BSD+ machine (client and server,
864 		 * actually), sending to a nameserver datagram
865 		 * port with no nameserver will cause an
866 		 * ICMP port unreachable message to be returned.
867 		 * If our datagram socket is "connected" to the
868 		 * server, we get an ECONNREFUSED error on the next
869 		 * socket operation, and select returns if the
870 		 * error message is received.  We can thus detect
871 		 * the absence of a nameserver without timing out.
872 		 *
873 		 * When the option "insecure1" is specified, we'd
874 		 * rather expect to see responses from an "unknown"
875 		 * address.  In order to let the kernel accept such
876 		 * responses, do not connect the socket here.
877 		 * XXX: or do we need an explicit option to disable
878 		 * connecting?
879 		 */
880 		if (!(statp->options & RES_INSECURE1) &&
881 		    _connect(EXT(statp).nssocks[ns], nsap, nsaplen) < 0) {
882 			Aerror(statp, stderr, "connect(dg)", errno, nsap,
883 			    nsaplen);
884 			res_nclose(statp);
885 			return (0);
886 		}
887 #endif /* !CANNOT_CONNECT_DGRAM */
888 		Dprint(statp->options & RES_DEBUG,
889 		       (stdout, ";; new DG socket\n"))
890 	}
891 	s = EXT(statp).nssocks[ns];
892 #ifndef CANNOT_CONNECT_DGRAM
893 	if (statp->options & RES_INSECURE1) {
894 		if (_sendto(s,
895 		    (const char*)buf, buflen, 0, nsap, nsaplen) != buflen) {
896 			Aerror(statp, stderr, "sendto", errno, nsap, nsaplen);
897 			res_nclose(statp);
898 			return (0);
899 		}
900 	} else if (send(s, (const char*)buf, buflen, 0) != buflen) {
901 		Perror(statp, stderr, "send", errno);
902 		res_nclose(statp);
903 		return (0);
904 	}
905 #else /* !CANNOT_CONNECT_DGRAM */
906 	if (_sendto(s, (const char*)buf, buflen, 0, nsap, nsaplen) != buflen)
907 	{
908 		Aerror(statp, stderr, "sendto", errno, nsap, nsaplen);
909 		res_nclose(statp);
910 		return (0);
911 	}
912 #endif /* !CANNOT_CONNECT_DGRAM */
913 
914 	/*
915 	 * Wait for reply.
916 	 */
917 	seconds = (statp->retrans << ns);
918 	if (ns > 0)
919 		seconds /= statp->nscount;
920 	if (seconds <= 0)
921 		seconds = 1;
922 	now = evNowTime();
923 	timeout = evConsTime(seconds, 0);
924 	finish = evAddTime(now, timeout);
925 	goto nonow;
926  wait:
927 	now = evNowTime();
928  nonow:
929 #ifndef USE_POLL
930 	if (evCmpTime(finish, now) > 0)
931 		timeout = evSubTime(finish, now);
932 	else
933 		timeout = evConsTime(0, 0);
934 #ifdef USE_KQUEUE
935 	EV_SET(&kv, s, EVFILT_READ, EV_ADD | EV_ONESHOT, 0, 0, 0);
936 	n = _kevent(kq, &kv, 1, &kv, 1, &timeout);
937 #else
938 	FD_ZERO(&dsmask);
939 	FD_SET(s, &dsmask);
940 	n = pselect(s + 1, &dsmask, NULL, NULL, &timeout, NULL);
941 #endif
942 #else
943 	timeout = evSubTime(finish, now);
944 	if (timeout.tv_sec < 0)
945 		timeout = evConsTime(0, 0);
946 	polltimeout = 1000*timeout.tv_sec +
947 		timeout.tv_nsec/1000000;
948 	pollfd.fd = s;
949 	pollfd.events = POLLRDNORM;
950 	n = poll(&pollfd, 1, polltimeout);
951 #endif /* USE_POLL */
952 
953 	if (n == 0) {
954 		Dprint(statp->options & RES_DEBUG, (stdout, ";; timeout\n"));
955 		*gotsomewhere = 1;
956 		return (0);
957 	}
958 	if (n < 0) {
959 		if (errno == EINTR)
960 			goto wait;
961 #ifdef USE_KQUEUE
962 		Perror(statp, stderr, "kevent", errno);
963 #else
964 #ifndef USE_POLL
965 		Perror(statp, stderr, "select", errno);
966 #else
967 		Perror(statp, stderr, "poll", errno);
968 #endif /* USE_POLL */
969 #endif
970 		res_nclose(statp);
971 		return (0);
972 	}
973 	errno = 0;
974 	fromlen = sizeof(from);
975 	resplen = _recvfrom(s, (char*)ans, anssiz,0,
976 			   (struct sockaddr *)&from, &fromlen);
977 	if (resplen <= 0) {
978 		Perror(statp, stderr, "recvfrom", errno);
979 		res_nclose(statp);
980 		return (0);
981 	}
982 	*gotsomewhere = 1;
983 	if (resplen < HFIXEDSZ) {
984 		/*
985 		 * Undersized message.
986 		 */
987 		Dprint(statp->options & RES_DEBUG,
988 		       (stdout, ";; undersized: %d\n",
989 			resplen));
990 		*terrno = EMSGSIZE;
991 		res_nclose(statp);
992 		return (0);
993 	}
994 	if (hp->id != anhp->id) {
995 		/*
996 		 * response from old query, ignore it.
997 		 * XXX - potential security hazard could
998 		 *	 be detected here.
999 		 */
1000 		DprintQ((statp->options & RES_DEBUG) ||
1001 			(statp->pfcode & RES_PRF_REPLY),
1002 			(stdout, ";; old answer:\n"),
1003 			ans, (resplen > anssiz) ? anssiz : resplen);
1004 		goto wait;
1005 	}
1006 	if (!(statp->options & RES_INSECURE1) &&
1007 	    !res_ourserver_p(statp, (struct sockaddr *)&from)) {
1008 		/*
1009 		 * response from wrong server? ignore it.
1010 		 * XXX - potential security hazard could
1011 		 *	 be detected here.
1012 		 */
1013 		DprintQ((statp->options & RES_DEBUG) ||
1014 			(statp->pfcode & RES_PRF_REPLY),
1015 			(stdout, ";; not our server:\n"),
1016 			ans, (resplen > anssiz) ? anssiz : resplen);
1017 		goto wait;
1018 	}
1019 #ifdef RES_USE_EDNS0
1020 	if (anhp->rcode == FORMERR && (statp->options & RES_USE_EDNS0) != 0U) {
1021 		/*
1022 		 * Do not retry if the server do not understand EDNS0.
1023 		 * The case has to be captured here, as FORMERR packet do not
1024 		 * carry query section, hence res_queriesmatch() returns 0.
1025 		 */
1026 		DprintQ(statp->options & RES_DEBUG,
1027 			(stdout, "server rejected query with EDNS0:\n"),
1028 			ans, (resplen > anssiz) ? anssiz : resplen);
1029 		/* record the error */
1030 		statp->_flags |= RES_F_EDNS0ERR;
1031 		res_nclose(statp);
1032 		return (0);
1033 	}
1034 #endif
1035 	if (!(statp->options & RES_INSECURE2) &&
1036 	    !res_queriesmatch(buf, buf + buflen,
1037 			      ans, ans + anssiz)) {
1038 		/*
1039 		 * response contains wrong query? ignore it.
1040 		 * XXX - potential security hazard could
1041 		 *	 be detected here.
1042 		 */
1043 		DprintQ((statp->options & RES_DEBUG) ||
1044 			(statp->pfcode & RES_PRF_REPLY),
1045 			(stdout, ";; wrong query name:\n"),
1046 			ans, (resplen > anssiz) ? anssiz : resplen);
1047 		goto wait;
1048 	}
1049 	if (anhp->rcode == SERVFAIL ||
1050 	    anhp->rcode == NOTIMP ||
1051 	    anhp->rcode == REFUSED) {
1052 		DprintQ(statp->options & RES_DEBUG,
1053 			(stdout, "server rejected query:\n"),
1054 			ans, (resplen > anssiz) ? anssiz : resplen);
1055 		res_nclose(statp);
1056 		/* don't retry if called from dig */
1057 		if (!statp->pfcode)
1058 			return (0);
1059 	}
1060 	if (!(statp->options & RES_IGNTC) && anhp->tc) {
1061 		/*
1062 		 * To get the rest of answer,
1063 		 * use TCP with same server.
1064 		 */
1065 		Dprint(statp->options & RES_DEBUG,
1066 		       (stdout, ";; truncated answer\n"));
1067 		*v_circuit = 1;
1068 		res_nclose(statp);
1069 		return (1);
1070 	}
1071 	/*
1072 	 * All is well, or the error is fatal.  Signal that the
1073 	 * next nameserver ought not be tried.
1074 	 */
1075 	return (resplen);
1076 }
1077 
1078 static void
1079 Aerror(const res_state statp, FILE *file, const char *string, int error,
1080        const struct sockaddr *address, int alen)
1081 {
1082 	int save = errno;
1083 	char hbuf[NI_MAXHOST];
1084 	char sbuf[NI_MAXSERV];
1085 
1086 	alen = alen;
1087 
1088 	if ((statp->options & RES_DEBUG) != 0U) {
1089 		if (getnameinfo(address, alen, hbuf, sizeof(hbuf),
1090 		    sbuf, sizeof(sbuf), niflags)) {
1091 			strncpy(hbuf, "?", sizeof(hbuf) - 1);
1092 			hbuf[sizeof(hbuf) - 1] = '\0';
1093 			strncpy(sbuf, "?", sizeof(sbuf) - 1);
1094 			sbuf[sizeof(sbuf) - 1] = '\0';
1095 		}
1096 		fprintf(file, "res_send: %s ([%s].%s): %s\n",
1097 			string, hbuf, sbuf, strerror(error));
1098 	}
1099 	errno = save;
1100 }
1101 
1102 static void
1103 Perror(const res_state statp, FILE *file, const char *string, int error) {
1104 	int save = errno;
1105 
1106 	if ((statp->options & RES_DEBUG) != 0U)
1107 		fprintf(file, "res_send: %s: %s\n",
1108 			string, strerror(error));
1109 	errno = save;
1110 }
1111 
1112 static int
1113 sock_eq(struct sockaddr *a, struct sockaddr *b) {
1114 	struct sockaddr_in *a4, *b4;
1115 	struct sockaddr_in6 *a6, *b6;
1116 
1117 	if (a->sa_family != b->sa_family)
1118 		return 0;
1119 	switch (a->sa_family) {
1120 	case AF_INET:
1121 		a4 = (struct sockaddr_in *)a;
1122 		b4 = (struct sockaddr_in *)b;
1123 		return a4->sin_port == b4->sin_port &&
1124 		    a4->sin_addr.s_addr == b4->sin_addr.s_addr;
1125 	case AF_INET6:
1126 		a6 = (struct sockaddr_in6 *)a;
1127 		b6 = (struct sockaddr_in6 *)b;
1128 		return a6->sin6_port == b6->sin6_port &&
1129 #ifdef HAVE_SIN6_SCOPE_ID
1130 		    a6->sin6_scope_id == b6->sin6_scope_id &&
1131 #endif
1132 		    IN6_ARE_ADDR_EQUAL(&a6->sin6_addr, &b6->sin6_addr);
1133 	default:
1134 		return 0;
1135 	}
1136 }
1137 
1138 #if defined(NEED_PSELECT) && !defined(USE_POLL) && !defined(USE_KQUEUE)
1139 /* XXX needs to move to the porting library. */
1140 static int
1141 pselect(int nfds, void *rfds, void *wfds, void *efds,
1142 	struct timespec *tsp, const sigset_t *sigmask)
1143 {
1144 	struct timeval tv, *tvp;
1145 	sigset_t sigs;
1146 	int n;
1147 
1148 	if (tsp) {
1149 		tvp = &tv;
1150 		tv = evTimeVal(*tsp);
1151 	} else
1152 		tvp = NULL;
1153 	if (sigmask)
1154 		sigprocmask(SIG_SETMASK, sigmask, &sigs);
1155 	n = select(nfds, rfds, wfds, efds, tvp);
1156 	if (sigmask)
1157 		sigprocmask(SIG_SETMASK, &sigs, NULL);
1158 	if (tsp)
1159 		*tsp = evTimeSpec(tv);
1160 	return (n);
1161 }
1162 #endif
1163