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