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