14bd3cce6SAxel Dörfler/*
24bd3cce6SAxel Dörfler * Portions Copyright (C) 2004-2009  Internet Systems Consortium, Inc. ("ISC")
34bd3cce6SAxel Dörfler * Portions Copyright (C) 1996-2003  Internet Software Consortium.
44bd3cce6SAxel Dörfler *
54bd3cce6SAxel Dörfler * Permission to use, copy, modify, and/or distribute this software for any
64bd3cce6SAxel Dörfler * purpose with or without fee is hereby granted, provided that the above
74bd3cce6SAxel Dörfler * copyright notice and this permission notice appear in all copies.
84bd3cce6SAxel Dörfler *
94bd3cce6SAxel Dörfler * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
104bd3cce6SAxel Dörfler * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
114bd3cce6SAxel Dörfler * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
124bd3cce6SAxel Dörfler * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
134bd3cce6SAxel Dörfler * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
144bd3cce6SAxel Dörfler * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
154bd3cce6SAxel Dörfler * PERFORMANCE OF THIS SOFTWARE.
164bd3cce6SAxel Dörfler */
174bd3cce6SAxel Dörfler
185d7cf21bSWaldemar Kornewald/*
195d7cf21bSWaldemar Kornewald * Copyright (c) 1985, 1989, 1993
205d7cf21bSWaldemar Kornewald *    The Regents of the University of California.  All rights reserved.
214bd3cce6SAxel Dörfler *
225d7cf21bSWaldemar Kornewald * Redistribution and use in source and binary forms, with or without
235d7cf21bSWaldemar Kornewald * modification, are permitted provided that the following conditions
245d7cf21bSWaldemar Kornewald * are met:
255d7cf21bSWaldemar Kornewald * 1. Redistributions of source code must retain the above copyright
265d7cf21bSWaldemar Kornewald *    notice, this list of conditions and the following disclaimer.
275d7cf21bSWaldemar Kornewald * 2. Redistributions in binary form must reproduce the above copyright
285d7cf21bSWaldemar Kornewald *    notice, this list of conditions and the following disclaimer in the
295d7cf21bSWaldemar Kornewald *    documentation and/or other materials provided with the distribution.
3024a60d9bSAugustin Cavalier * 3. Neither the name of the University nor the names of its contributors
315d7cf21bSWaldemar Kornewald *    may be used to endorse or promote products derived from this software
325d7cf21bSWaldemar Kornewald *    without specific prior written permission.
334bd3cce6SAxel Dörfler *
345d7cf21bSWaldemar Kornewald * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
355d7cf21bSWaldemar Kornewald * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
365d7cf21bSWaldemar Kornewald * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
375d7cf21bSWaldemar Kornewald * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
385d7cf21bSWaldemar Kornewald * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
395d7cf21bSWaldemar Kornewald * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
405d7cf21bSWaldemar Kornewald * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
415d7cf21bSWaldemar Kornewald * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
425d7cf21bSWaldemar Kornewald * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
435d7cf21bSWaldemar Kornewald * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
445d7cf21bSWaldemar Kornewald * SUCH DAMAGE.
455d7cf21bSWaldemar Kornewald */
465d7cf21bSWaldemar Kornewald
475d7cf21bSWaldemar Kornewald/*
485d7cf21bSWaldemar Kornewald * Portions Copyright (c) 1993 by Digital Equipment Corporation.
494bd3cce6SAxel Dörfler *
505d7cf21bSWaldemar Kornewald * Permission to use, copy, modify, and distribute this software for any
515d7cf21bSWaldemar Kornewald * purpose with or without fee is hereby granted, provided that the above
525d7cf21bSWaldemar Kornewald * copyright notice and this permission notice appear in all copies, and that
535d7cf21bSWaldemar Kornewald * the name of Digital Equipment Corporation not be used in advertising or
545d7cf21bSWaldemar Kornewald * publicity pertaining to distribution of the document or software without
555d7cf21bSWaldemar Kornewald * specific, written prior permission.
564bd3cce6SAxel Dörfler *
575d7cf21bSWaldemar Kornewald * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
585d7cf21bSWaldemar Kornewald * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
595d7cf21bSWaldemar Kornewald * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
605d7cf21bSWaldemar Kornewald * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
615d7cf21bSWaldemar Kornewald * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
625d7cf21bSWaldemar Kornewald * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
635d7cf21bSWaldemar Kornewald * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
645d7cf21bSWaldemar Kornewald * SOFTWARE.
655d7cf21bSWaldemar Kornewald */
665d7cf21bSWaldemar Kornewald
678d2aee6bSAdrien Destugues#include <sys/cdefs.h>
685d7cf21bSWaldemar Kornewald#if defined(LIBC_SCCS) && !defined(lint)
698d2aee6bSAdrien Destugues#ifdef notdef
705d7cf21bSWaldemar Kornewaldstatic const char sccsid[] = "@(#)res_send.c	8.1 (Berkeley) 6/4/93";
714bd3cce6SAxel Dörflerstatic const char rcsid[] = "$Id: res_send.c,v 1.22 2009/01/22 23:49:23 tbox Exp $";
728d2aee6bSAdrien Destugues#else
738d2aee6bSAdrien Destugues__RCSID("$NetBSD: res_send.c,v 1.29 2013/02/16 13:45:45 para Exp $");
748d2aee6bSAdrien Destugues#endif
755d7cf21bSWaldemar Kornewald#endif /* LIBC_SCCS and not lint */
765d7cf21bSWaldemar Kornewald
774bd3cce6SAxel Dörfler/*! \file
784bd3cce6SAxel Dörfler * \brief
795d7cf21bSWaldemar Kornewald * Send query to name server and wait for reply.
805d7cf21bSWaldemar Kornewald */
815d7cf21bSWaldemar Kornewald
825d7cf21bSWaldemar Kornewald#include "port_before.h"
838d2aee6bSAdrien Destugues#ifndef USE_KQUEUE
845d7cf21bSWaldemar Kornewald#include "fd_setsize.h"
858d2aee6bSAdrien Destugues#endif /* USE_KQUEUE */
865d7cf21bSWaldemar Kornewald
875d7cf21bSWaldemar Kornewald#include <sys/types.h>
885d7cf21bSWaldemar Kornewald#include <sys/param.h>
895d7cf21bSWaldemar Kornewald#include <sys/time.h>
905d7cf21bSWaldemar Kornewald#include <sys/socket.h>
915d7cf21bSWaldemar Kornewald#include <sys/uio.h>
928d2aee6bSAdrien Destugues#ifdef USE_KQUEUE
938d2aee6bSAdrien Destugues#include <sys/event.h>
948d2aee6bSAdrien Destugues#endif /* USE_KQUEUE */
955d7cf21bSWaldemar Kornewald
965d7cf21bSWaldemar Kornewald#include <netinet/in.h>
975d7cf21bSWaldemar Kornewald#include <arpa/nameser.h>
985d7cf21bSWaldemar Kornewald#include <arpa/inet.h>
995d7cf21bSWaldemar Kornewald
1008d2aee6bSAdrien Destugues#include <assert.h>
1015d7cf21bSWaldemar Kornewald#include <errno.h>
1025d7cf21bSWaldemar Kornewald#include <netdb.h>
1035d7cf21bSWaldemar Kornewald#include <resolv.h>
1045d7cf21bSWaldemar Kornewald#include <signal.h>
1055d7cf21bSWaldemar Kornewald#include <stdio.h>
1068d2aee6bSAdrien Destugues#include <fcntl.h>
1075d7cf21bSWaldemar Kornewald#include <stdlib.h>
1085d7cf21bSWaldemar Kornewald#include <string.h>
1095d7cf21bSWaldemar Kornewald#include <unistd.h>
1105d7cf21bSWaldemar Kornewald
1115d7cf21bSWaldemar Kornewald#include <isc/eventlib.h>
1125d7cf21bSWaldemar Kornewald
1135d7cf21bSWaldemar Kornewald#include "port_after.h"
1145d7cf21bSWaldemar Kornewald
1158d2aee6bSAdrien Destugues#if 0
1168d2aee6bSAdrien Destugues#ifdef __weak_alias
1178d2aee6bSAdrien Destugues__weak_alias(res_ourserver_p,__res_ourserver_p)
1188d2aee6bSAdrien Destugues__weak_alias(res_nameinquery,__res_nameinquery)
1198d2aee6bSAdrien Destugues__weak_alias(res_queriesmatch,__res_queriesmatch)
1208d2aee6bSAdrien Destugues__weak_alias(res_nsend,__res_nsend)
1218d2aee6bSAdrien Destugues#endif
1228d2aee6bSAdrien Destugues#endif
1238d2aee6bSAdrien Destugues
1248d2aee6bSAdrien Destugues#ifndef SOCK_NOSIGPIPE
1258d2aee6bSAdrien Destugues#define SOCK_NOSIGPIPE 0
1268d2aee6bSAdrien Destugues#endif
1278d2aee6bSAdrien Destugues#ifndef SOCK_NOCLOEXEC
1288d2aee6bSAdrien Destugues#define SOCK_NOCLOEXEC 0
1298d2aee6bSAdrien Destugues#endif
1308d2aee6bSAdrien Destugues
1314bd3cce6SAxel Dörfler#ifdef USE_POLL
1324bd3cce6SAxel Dörfler#ifdef HAVE_STROPTS_H
1334bd3cce6SAxel Dörfler#include <stropts.h>
1344bd3cce6SAxel Dörfler#endif
1354bd3cce6SAxel Dörfler#include <poll.h>
1364bd3cce6SAxel Dörfler#endif /* USE_POLL */
1374bd3cce6SAxel Dörfler
1385d7cf21bSWaldemar Kornewald/* Options.  Leave them on. */
1398d2aee6bSAdrien Destugues#ifndef DEBUG
14051974c5dSOliver Tappe//#define DEBUG
1418d2aee6bSAdrien Destugues#endif
1425d7cf21bSWaldemar Kornewald#include "res_debug.h"
1435d7cf21bSWaldemar Kornewald#include "res_private.h"
1445d7cf21bSWaldemar Kornewald
1455d7cf21bSWaldemar Kornewald#define EXT(res) ((res)->_u._ext)
1465d7cf21bSWaldemar Kornewald
1478d2aee6bSAdrien Destugues#if !defined(USE_POLL) && !defined(USE_KQUEUE)
1485d7cf21bSWaldemar Kornewaldstatic const int highestFD = FD_SETSIZE - 1;
1494bd3cce6SAxel Dörfler#endif
1505d7cf21bSWaldemar Kornewald
1515d7cf21bSWaldemar Kornewald/* Forward. */
1525d7cf21bSWaldemar Kornewald
1538d2aee6bSAdrien Destuguesstatic socklen_t	get_salen(const struct sockaddr *);
1548d2aee6bSAdrien Destuguesstatic struct sockaddr * get_nsaddr(res_state, size_t);
1558d2aee6bSAdrien Destuguesstatic int		send_vc(res_state, const void *, size_t,
1565d7cf21bSWaldemar Kornewald				u_char *, int, int *, int);
1578d2aee6bSAdrien Destuguesstatic int		send_dg(res_state,
1588d2aee6bSAdrien Destugues#ifdef USE_KQUEUE
1598d2aee6bSAdrien Destugues				int kq,
1608d2aee6bSAdrien Destugues#endif
1618d2aee6bSAdrien Destugues				const void *, size_t,
1624bd3cce6SAxel Dörfler				u_char *, int, int *, int, int,
1635d7cf21bSWaldemar Kornewald				int *, int *);
16451974c5dSOliver Tappe#ifdef DEBUG
1655d7cf21bSWaldemar Kornewaldstatic void		Aerror(const res_state, FILE *, const char *, int,
1668d2aee6bSAdrien Destugues			       const struct sockaddr *, socklen_t);
1675d7cf21bSWaldemar Kornewaldstatic void		Perror(const res_state, FILE *, const char *, int);
16851974c5dSOliver Tappe#endif // DEBUG
1695d7cf21bSWaldemar Kornewaldstatic int		sock_eq(struct sockaddr *, struct sockaddr *);
1708d2aee6bSAdrien Destugues#if defined(NEED_PSELECT) && !defined(USE_POLL) && !defined(USE_KQUEUE)
1715d7cf21bSWaldemar Kornewaldstatic int		pselect(int, void *, void *, void *,
1725d7cf21bSWaldemar Kornewald				struct timespec *,
1735d7cf21bSWaldemar Kornewald				const sigset_t *);
1745d7cf21bSWaldemar Kornewald#endif
1755d7cf21bSWaldemar Kornewaldvoid res_pquery(const res_state, const u_char *, int, FILE *);
1765d7cf21bSWaldemar Kornewald
177e43cfe78SAdrien Destugues#ifdef DEBUG
1785d7cf21bSWaldemar Kornewaldstatic const int niflags = NI_NUMERICHOST | NI_NUMERICSERV;
179e43cfe78SAdrien Destugues#endif
1805d7cf21bSWaldemar Kornewald
1815d7cf21bSWaldemar Kornewald/* Public. */
1825d7cf21bSWaldemar Kornewald
1834bd3cce6SAxel Dörfler/*%
1845d7cf21bSWaldemar Kornewald *	looks up "ina" in _res.ns_addr_list[]
1854bd3cce6SAxel Dörfler *
1865d7cf21bSWaldemar Kornewald * returns:
1874bd3cce6SAxel Dörfler *\li	0  : not found
1884bd3cce6SAxel Dörfler *\li	>0 : found
1894bd3cce6SAxel Dörfler *
1905d7cf21bSWaldemar Kornewald * author:
1914bd3cce6SAxel Dörfler *\li	paul vixie, 29may94
1925d7cf21bSWaldemar Kornewald */
1935d7cf21bSWaldemar Kornewaldint
1945d7cf21bSWaldemar Kornewaldres_ourserver_p(const res_state statp, const struct sockaddr *sa) {
1955d7cf21bSWaldemar Kornewald	const struct sockaddr_in *inp, *srv;
1965d7cf21bSWaldemar Kornewald	const struct sockaddr_in6 *in6p, *srv6;
1975d7cf21bSWaldemar Kornewald	int ns;
1985d7cf21bSWaldemar Kornewald
1995d7cf21bSWaldemar Kornewald	switch (sa->sa_family) {
2005d7cf21bSWaldemar Kornewald	case AF_INET:
2018d2aee6bSAdrien Destugues		inp = (const struct sockaddr_in *)(const void *)sa;
2025d7cf21bSWaldemar Kornewald		for (ns = 0;  ns < statp->nscount;  ns++) {
2038d2aee6bSAdrien Destugues			srv = (struct sockaddr_in *)(void *)get_nsaddr(statp, (size_t)ns);
2045d7cf21bSWaldemar Kornewald			if (srv->sin_family == inp->sin_family &&
2055d7cf21bSWaldemar Kornewald			    srv->sin_port == inp->sin_port &&
2065d7cf21bSWaldemar Kornewald			    (srv->sin_addr.s_addr == INADDR_ANY ||
2075d7cf21bSWaldemar Kornewald			     srv->sin_addr.s_addr == inp->sin_addr.s_addr))
2085d7cf21bSWaldemar Kornewald				return (1);
2095d7cf21bSWaldemar Kornewald		}
2105d7cf21bSWaldemar Kornewald		break;
2115d7cf21bSWaldemar Kornewald	case AF_INET6:
2125d7cf21bSWaldemar Kornewald		if (EXT(statp).ext == NULL)
2135d7cf21bSWaldemar Kornewald			break;
2148d2aee6bSAdrien Destugues		in6p = (const struct sockaddr_in6 *)(const void *)sa;
2155d7cf21bSWaldemar Kornewald		for (ns = 0;  ns < statp->nscount;  ns++) {
2168d2aee6bSAdrien Destugues			srv6 = (struct sockaddr_in6 *)(void *)get_nsaddr(statp, (size_t)ns);
2175d7cf21bSWaldemar Kornewald			if (srv6->sin6_family == in6p->sin6_family &&
2185d7cf21bSWaldemar Kornewald			    srv6->sin6_port == in6p->sin6_port &&
2195d7cf21bSWaldemar Kornewald#ifdef HAVE_SIN6_SCOPE_ID
2205d7cf21bSWaldemar Kornewald			    (srv6->sin6_scope_id == 0 ||
2215d7cf21bSWaldemar Kornewald			     srv6->sin6_scope_id == in6p->sin6_scope_id) &&
2225d7cf21bSWaldemar Kornewald#endif
2235d7cf21bSWaldemar Kornewald			    (IN6_IS_ADDR_UNSPECIFIED(&srv6->sin6_addr) ||
2245d7cf21bSWaldemar Kornewald			     IN6_ARE_ADDR_EQUAL(&srv6->sin6_addr, &in6p->sin6_addr)))
2255d7cf21bSWaldemar Kornewald				return (1);
2265d7cf21bSWaldemar Kornewald		}
2275d7cf21bSWaldemar Kornewald		break;
2285d7cf21bSWaldemar Kornewald	default:
2295d7cf21bSWaldemar Kornewald		break;
2305d7cf21bSWaldemar Kornewald	}
2315d7cf21bSWaldemar Kornewald	return (0);
2325d7cf21bSWaldemar Kornewald}
2335d7cf21bSWaldemar Kornewald
2344bd3cce6SAxel Dörfler/*%
2355d7cf21bSWaldemar Kornewald *	look for (name,type,class) in the query section of packet (buf,eom)
2364bd3cce6SAxel Dörfler *
2375d7cf21bSWaldemar Kornewald * requires:
2384bd3cce6SAxel Dörfler *\li	buf + HFIXEDSZ <= eom
2394bd3cce6SAxel Dörfler *
2405d7cf21bSWaldemar Kornewald * returns:
2414bd3cce6SAxel Dörfler *\li	-1 : format error
2424bd3cce6SAxel Dörfler *\li	0  : not found
2434bd3cce6SAxel Dörfler *\li	>0 : found
2444bd3cce6SAxel Dörfler *
2455d7cf21bSWaldemar Kornewald * author:
2464bd3cce6SAxel Dörfler *\li	paul vixie, 29may94
2475d7cf21bSWaldemar Kornewald */
2485d7cf21bSWaldemar Kornewaldint
2495d7cf21bSWaldemar Kornewaldres_nameinquery(const char *name, int type, int class,
2505d7cf21bSWaldemar Kornewald		const u_char *buf, const u_char *eom)
2515d7cf21bSWaldemar Kornewald{
2525d7cf21bSWaldemar Kornewald	const u_char *cp = buf + HFIXEDSZ;
2538d2aee6bSAdrien Destugues	int qdcount = ntohs(((const HEADER*)(const void *)buf)->qdcount);
2545d7cf21bSWaldemar Kornewald
2555d7cf21bSWaldemar Kornewald	while (qdcount-- > 0) {
2565d7cf21bSWaldemar Kornewald		char tname[MAXDNAME+1];
2575d7cf21bSWaldemar Kornewald		int n, ttype, tclass;
2585d7cf21bSWaldemar Kornewald
2598d2aee6bSAdrien Destugues		n = dn_expand(buf, eom, cp, tname, (int)sizeof tname);
2605d7cf21bSWaldemar Kornewald		if (n < 0)
2615d7cf21bSWaldemar Kornewald			return (-1);
2625d7cf21bSWaldemar Kornewald		cp += n;
2635d7cf21bSWaldemar Kornewald		if (cp + 2 * INT16SZ > eom)
2645d7cf21bSWaldemar Kornewald			return (-1);
2655d7cf21bSWaldemar Kornewald		ttype = ns_get16(cp); cp += INT16SZ;
2665d7cf21bSWaldemar Kornewald		tclass = ns_get16(cp); cp += INT16SZ;
2675d7cf21bSWaldemar Kornewald		if (ttype == type && tclass == class &&
2685d7cf21bSWaldemar Kornewald		    ns_samename(tname, name) == 1)
2695d7cf21bSWaldemar Kornewald			return (1);
2705d7cf21bSWaldemar Kornewald	}
2715d7cf21bSWaldemar Kornewald	return (0);
2725d7cf21bSWaldemar Kornewald}
2735d7cf21bSWaldemar Kornewald
2744bd3cce6SAxel Dörfler/*%
2755d7cf21bSWaldemar Kornewald *	is there a 1:1 mapping of (name,type,class)
2765d7cf21bSWaldemar Kornewald *	in (buf1,eom1) and (buf2,eom2)?
2774bd3cce6SAxel Dörfler *
2785d7cf21bSWaldemar Kornewald * returns:
2794bd3cce6SAxel Dörfler *\li	-1 : format error
2804bd3cce6SAxel Dörfler *\li	0  : not a 1:1 mapping
2814bd3cce6SAxel Dörfler *\li	>0 : is a 1:1 mapping
2824bd3cce6SAxel Dörfler *
2835d7cf21bSWaldemar Kornewald * author:
2844bd3cce6SAxel Dörfler *\li	paul vixie, 29may94
2855d7cf21bSWaldemar Kornewald */
2865d7cf21bSWaldemar Kornewaldint
2875d7cf21bSWaldemar Kornewaldres_queriesmatch(const u_char *buf1, const u_char *eom1,
2885d7cf21bSWaldemar Kornewald		 const u_char *buf2, const u_char *eom2)
2895d7cf21bSWaldemar Kornewald{
2905d7cf21bSWaldemar Kornewald	const u_char *cp = buf1 + HFIXEDSZ;
2918d2aee6bSAdrien Destugues	int qdcount = ntohs(((const HEADER*)(const void *)buf1)->qdcount);
2925d7cf21bSWaldemar Kornewald
2935d7cf21bSWaldemar Kornewald	if (buf1 + HFIXEDSZ > eom1 || buf2 + HFIXEDSZ > eom2)
2945d7cf21bSWaldemar Kornewald		return (-1);
2955d7cf21bSWaldemar Kornewald
2965d7cf21bSWaldemar Kornewald	/*
2975d7cf21bSWaldemar Kornewald	 * Only header section present in replies to
2985d7cf21bSWaldemar Kornewald	 * dynamic update packets.
2995d7cf21bSWaldemar Kornewald	 */
3008d2aee6bSAdrien Destugues	if ((((const HEADER *)(const void *)buf1)->opcode == ns_o_update) &&
3018d2aee6bSAdrien Destugues	    (((const HEADER *)(const void *)buf2)->opcode == ns_o_update))
3025d7cf21bSWaldemar Kornewald		return (1);
3035d7cf21bSWaldemar Kornewald
3048d2aee6bSAdrien Destugues	if (qdcount != ntohs(((const HEADER*)(const void *)buf2)->qdcount))
3055d7cf21bSWaldemar Kornewald		return (0);
3065d7cf21bSWaldemar Kornewald	while (qdcount-- > 0) {
3075d7cf21bSWaldemar Kornewald		char tname[MAXDNAME+1];
3085d7cf21bSWaldemar Kornewald		int n, ttype, tclass;
3095d7cf21bSWaldemar Kornewald
3108d2aee6bSAdrien Destugues		n = dn_expand(buf1, eom1, cp, tname, (int)sizeof tname);
3115d7cf21bSWaldemar Kornewald		if (n < 0)
3125d7cf21bSWaldemar Kornewald			return (-1);
3135d7cf21bSWaldemar Kornewald		cp += n;
3145d7cf21bSWaldemar Kornewald		if (cp + 2 * INT16SZ > eom1)
3155d7cf21bSWaldemar Kornewald			return (-1);
3165d7cf21bSWaldemar Kornewald		ttype = ns_get16(cp);	cp += INT16SZ;
3175d7cf21bSWaldemar Kornewald		tclass = ns_get16(cp); cp += INT16SZ;
3185d7cf21bSWaldemar Kornewald		if (!res_nameinquery(tname, ttype, tclass, buf2, eom2))
3195d7cf21bSWaldemar Kornewald			return (0);
3205d7cf21bSWaldemar Kornewald	}
3215d7cf21bSWaldemar Kornewald	return (1);
3225d7cf21bSWaldemar Kornewald}
3235d7cf21bSWaldemar Kornewald
3245d7cf21bSWaldemar Kornewaldint
3255d7cf21bSWaldemar Kornewaldres_nsend(res_state statp,
3265d7cf21bSWaldemar Kornewald	  const u_char *buf, int buflen, u_char *ans, int anssiz)
3275d7cf21bSWaldemar Kornewald{
3284bd3cce6SAxel Dörfler	int gotsomewhere, terrno, tries, v_circuit, resplen, ns, n;
3298d2aee6bSAdrien Destugues#ifdef USE_KQUEUE
3308d2aee6bSAdrien Destugues	int kq;
3318d2aee6bSAdrien Destugues#endif
33251974c5dSOliver Tappe#ifdef DEBUG
3335d7cf21bSWaldemar Kornewald	char abuf[NI_MAXHOST];
33451974c5dSOliver Tappe#endif
3355d7cf21bSWaldemar Kornewald
3364bd3cce6SAxel Dörfler	/* No name servers or res_init() failure */
3374bd3cce6SAxel Dörfler	if (statp->nscount == 0 || EXT(statp).ext == NULL) {
3385d7cf21bSWaldemar Kornewald		errno = ESRCH;
3395d7cf21bSWaldemar Kornewald		return (-1);
3405d7cf21bSWaldemar Kornewald	}
3415d7cf21bSWaldemar Kornewald	if (anssiz < HFIXEDSZ) {
3425d7cf21bSWaldemar Kornewald		errno = EINVAL;
3435d7cf21bSWaldemar Kornewald		return (-1);
3445d7cf21bSWaldemar Kornewald	}
3455d7cf21bSWaldemar Kornewald	DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_QUERY),
3465d7cf21bSWaldemar Kornewald		(stdout, ";; res_send()\n"), buf, buflen);
3475d7cf21bSWaldemar Kornewald	v_circuit = (statp->options & RES_USEVC) || buflen > PACKETSZ;
3485d7cf21bSWaldemar Kornewald	gotsomewhere = 0;
3495d7cf21bSWaldemar Kornewald	terrno = ETIMEDOUT;
3505d7cf21bSWaldemar Kornewald
3518d2aee6bSAdrien Destugues#ifdef USE_KQUEUE
3528d2aee6bSAdrien Destugues	if ((kq = kqueue1(O_CLOEXEC)) == -1) {
3538d2aee6bSAdrien Destugues		return (-1);
3548d2aee6bSAdrien Destugues	}
3558d2aee6bSAdrien Destugues#endif
3568d2aee6bSAdrien Destugues
3575d7cf21bSWaldemar Kornewald	/*
3585d7cf21bSWaldemar Kornewald	 * If the ns_addr_list in the resolver context has changed, then
3595d7cf21bSWaldemar Kornewald	 * invalidate our cached copy and the associated timing data.
3605d7cf21bSWaldemar Kornewald	 */
3615d7cf21bSWaldemar Kornewald	if (EXT(statp).nscount != 0) {
3625d7cf21bSWaldemar Kornewald		int needclose = 0;
3635d7cf21bSWaldemar Kornewald		struct sockaddr_storage peer;
3645d7cf21bSWaldemar Kornewald		ISC_SOCKLEN_T peerlen;
3655d7cf21bSWaldemar Kornewald
3665d7cf21bSWaldemar Kornewald		if (EXT(statp).nscount != statp->nscount)
3675d7cf21bSWaldemar Kornewald			needclose++;
3685d7cf21bSWaldemar Kornewald		else
3695d7cf21bSWaldemar Kornewald			for (ns = 0; ns < statp->nscount; ns++) {
3705d7cf21bSWaldemar Kornewald				if (statp->nsaddr_list[ns].sin_family &&
3718d2aee6bSAdrien Destugues				    !sock_eq((struct sockaddr *)(void *)&statp->nsaddr_list[ns],
3728d2aee6bSAdrien Destugues					     (struct sockaddr *)(void *)&EXT(statp).ext->nsaddrs[ns])) {
3735d7cf21bSWaldemar Kornewald					needclose++;
3745d7cf21bSWaldemar Kornewald					break;
3755d7cf21bSWaldemar Kornewald				}
3765d7cf21bSWaldemar Kornewald
3775d7cf21bSWaldemar Kornewald				if (EXT(statp).nssocks[ns] == -1)
3785d7cf21bSWaldemar Kornewald					continue;
3795d7cf21bSWaldemar Kornewald				peerlen = sizeof(peer);
3804bd3cce6SAxel Dörfler				if (getpeername(EXT(statp).nssocks[ns],
3818d2aee6bSAdrien Destugues				    (struct sockaddr *)(void *)&peer, &peerlen) < 0) {
3825d7cf21bSWaldemar Kornewald					needclose++;
3835d7cf21bSWaldemar Kornewald					break;
3845d7cf21bSWaldemar Kornewald				}
3858d2aee6bSAdrien Destugues				if (!sock_eq((struct sockaddr *)(void *)&peer,
3868d2aee6bSAdrien Destugues				    get_nsaddr(statp, (size_t)ns))) {
3875d7cf21bSWaldemar Kornewald					needclose++;
3885d7cf21bSWaldemar Kornewald					break;
3895d7cf21bSWaldemar Kornewald				}
3905d7cf21bSWaldemar Kornewald			}
3915d7cf21bSWaldemar Kornewald		if (needclose) {
3925d7cf21bSWaldemar Kornewald			res_nclose(statp);
3935d7cf21bSWaldemar Kornewald			EXT(statp).nscount = 0;
3945d7cf21bSWaldemar Kornewald		}
3955d7cf21bSWaldemar Kornewald	}
3965d7cf21bSWaldemar Kornewald
3975d7cf21bSWaldemar Kornewald	/*
3985d7cf21bSWaldemar Kornewald	 * Maybe initialize our private copy of the ns_addr_list.
3995d7cf21bSWaldemar Kornewald	 */
4005d7cf21bSWaldemar Kornewald	if (EXT(statp).nscount == 0) {
4015d7cf21bSWaldemar Kornewald		for (ns = 0; ns < statp->nscount; ns++) {
4025d7cf21bSWaldemar Kornewald			EXT(statp).nstimes[ns] = RES_MAXTIME;
4035d7cf21bSWaldemar Kornewald			EXT(statp).nssocks[ns] = -1;
4045d7cf21bSWaldemar Kornewald			if (!statp->nsaddr_list[ns].sin_family)
4055d7cf21bSWaldemar Kornewald				continue;
4065d7cf21bSWaldemar Kornewald			EXT(statp).ext->nsaddrs[ns].sin =
4075d7cf21bSWaldemar Kornewald				 statp->nsaddr_list[ns];
4085d7cf21bSWaldemar Kornewald		}
4095d7cf21bSWaldemar Kornewald		EXT(statp).nscount = statp->nscount;
4105d7cf21bSWaldemar Kornewald	}
4115d7cf21bSWaldemar Kornewald
4125d7cf21bSWaldemar Kornewald	/*
4135d7cf21bSWaldemar Kornewald	 * Some resolvers want to even out the load on their nameservers.
4145d7cf21bSWaldemar Kornewald	 * Note that RES_BLAST overrides RES_ROTATE.
4155d7cf21bSWaldemar Kornewald	 */
4165d7cf21bSWaldemar Kornewald	if ((statp->options & RES_ROTATE) != 0U &&
4175d7cf21bSWaldemar Kornewald	    (statp->options & RES_BLAST) == 0U) {
4185d7cf21bSWaldemar Kornewald		union res_sockaddr_union inu;
4195d7cf21bSWaldemar Kornewald		struct sockaddr_in ina;
4205d7cf21bSWaldemar Kornewald		int lastns = statp->nscount - 1;
4215d7cf21bSWaldemar Kornewald		int fd;
4225d7cf21bSWaldemar Kornewald		u_int16_t nstime;
4235d7cf21bSWaldemar Kornewald
4245d7cf21bSWaldemar Kornewald		if (EXT(statp).ext != NULL)
4255d7cf21bSWaldemar Kornewald			inu = EXT(statp).ext->nsaddrs[0];
4265d7cf21bSWaldemar Kornewald		ina = statp->nsaddr_list[0];
4275d7cf21bSWaldemar Kornewald		fd = EXT(statp).nssocks[0];
4285d7cf21bSWaldemar Kornewald		nstime = EXT(statp).nstimes[0];
4295d7cf21bSWaldemar Kornewald		for (ns = 0; ns < lastns; ns++) {
4305d7cf21bSWaldemar Kornewald			if (EXT(statp).ext != NULL)
4314bd3cce6SAxel Dörfler				EXT(statp).ext->nsaddrs[ns] =
4325d7cf21bSWaldemar Kornewald					EXT(statp).ext->nsaddrs[ns + 1];
4335d7cf21bSWaldemar Kornewald			statp->nsaddr_list[ns] = statp->nsaddr_list[ns + 1];
4345d7cf21bSWaldemar Kornewald			EXT(statp).nssocks[ns] = EXT(statp).nssocks[ns + 1];
4355d7cf21bSWaldemar Kornewald			EXT(statp).nstimes[ns] = EXT(statp).nstimes[ns + 1];
4365d7cf21bSWaldemar Kornewald		}
4375d7cf21bSWaldemar Kornewald		if (EXT(statp).ext != NULL)
4385d7cf21bSWaldemar Kornewald			EXT(statp).ext->nsaddrs[lastns] = inu;
4395d7cf21bSWaldemar Kornewald		statp->nsaddr_list[lastns] = ina;
4405d7cf21bSWaldemar Kornewald		EXT(statp).nssocks[lastns] = fd;
4415d7cf21bSWaldemar Kornewald		EXT(statp).nstimes[lastns] = nstime;
4425d7cf21bSWaldemar Kornewald	}
4435d7cf21bSWaldemar Kornewald
4445d7cf21bSWaldemar Kornewald	/*
4455d7cf21bSWaldemar Kornewald	 * Send request, RETRY times, or until successful.
4465d7cf21bSWaldemar Kornewald	 */
4474bd3cce6SAxel Dörfler	for (tries = 0; tries < statp->retry; tries++) {
4485d7cf21bSWaldemar Kornewald	    for (ns = 0; ns < statp->nscount; ns++) {
4495d7cf21bSWaldemar Kornewald		struct sockaddr *nsap;
4508d2aee6bSAdrien Destugues		socklen_t nsaplen;
4518d2aee6bSAdrien Destugues		nsap = get_nsaddr(statp, (size_t)ns);
4525d7cf21bSWaldemar Kornewald		nsaplen = get_salen(nsap);
4535d7cf21bSWaldemar Kornewald		statp->_flags &= ~RES_F_LASTMASK;
4545d7cf21bSWaldemar Kornewald		statp->_flags |= (ns << RES_F_LASTSHIFT);
4555d7cf21bSWaldemar Kornewald same_ns:
4565d7cf21bSWaldemar Kornewald		if (statp->qhook) {
4575d7cf21bSWaldemar Kornewald			int done = 0, loops = 0;
4585d7cf21bSWaldemar Kornewald
4595d7cf21bSWaldemar Kornewald			do {
4605d7cf21bSWaldemar Kornewald				res_sendhookact act;
4615d7cf21bSWaldemar Kornewald
4625d7cf21bSWaldemar Kornewald				act = (*statp->qhook)(&nsap, &buf, &buflen,
4635d7cf21bSWaldemar Kornewald						      ans, anssiz, &resplen);
4645d7cf21bSWaldemar Kornewald				switch (act) {
4655d7cf21bSWaldemar Kornewald				case res_goahead:
4665d7cf21bSWaldemar Kornewald					done = 1;
4675d7cf21bSWaldemar Kornewald					break;
4685d7cf21bSWaldemar Kornewald				case res_nextns:
4695d7cf21bSWaldemar Kornewald					res_nclose(statp);
4705d7cf21bSWaldemar Kornewald					goto next_ns;
4715d7cf21bSWaldemar Kornewald				case res_done:
4728d2aee6bSAdrien Destugues#ifdef USE_KQUEUE
4738d2aee6bSAdrien Destugues					close(kq);
4748d2aee6bSAdrien Destugues#endif
4755d7cf21bSWaldemar Kornewald					return (resplen);
4765d7cf21bSWaldemar Kornewald				case res_modified:
4775d7cf21bSWaldemar Kornewald					/* give the hook another try */
4785d7cf21bSWaldemar Kornewald					if (++loops < 42) /*doug adams*/
4795d7cf21bSWaldemar Kornewald						break;
4805d7cf21bSWaldemar Kornewald					/*FALLTHROUGH*/
4815d7cf21bSWaldemar Kornewald				case res_error:
4825d7cf21bSWaldemar Kornewald					/*FALLTHROUGH*/
4835d7cf21bSWaldemar Kornewald				default:
4845d7cf21bSWaldemar Kornewald					goto fail;
4855d7cf21bSWaldemar Kornewald				}
4865d7cf21bSWaldemar Kornewald			} while (!done);
4875d7cf21bSWaldemar Kornewald		}
4885d7cf21bSWaldemar Kornewald
4895d7cf21bSWaldemar Kornewald		Dprint(((statp->options & RES_DEBUG) &&
4908d2aee6bSAdrien Destugues			getnameinfo(nsap, nsaplen, abuf,
4918d2aee6bSAdrien Destugues			    (socklen_t)sizeof(abuf), NULL, 0, niflags) == 0),
4925d7cf21bSWaldemar Kornewald		       (stdout, ";; Querying server (# %d) address = %s\n",
4935d7cf21bSWaldemar Kornewald			ns + 1, abuf));
4945d7cf21bSWaldemar Kornewald
4955d7cf21bSWaldemar Kornewald
4965d7cf21bSWaldemar Kornewald		if (v_circuit) {
4975d7cf21bSWaldemar Kornewald			/* Use VC; at most one attempt per server. */
4984bd3cce6SAxel Dörfler			tries = statp->retry;
4998d2aee6bSAdrien Destugues			n = send_vc(statp, buf, (size_t)buflen, ans, anssiz, &terrno,
5005d7cf21bSWaldemar Kornewald				    ns);
5015d7cf21bSWaldemar Kornewald			if (n < 0)
5025d7cf21bSWaldemar Kornewald				goto fail;
5035d7cf21bSWaldemar Kornewald			if (n == 0)
5045d7cf21bSWaldemar Kornewald				goto next_ns;
5055d7cf21bSWaldemar Kornewald			resplen = n;
5065d7cf21bSWaldemar Kornewald		} else {
5075d7cf21bSWaldemar Kornewald			/* Use datagrams. */
5088d2aee6bSAdrien Destugues			n = send_dg(statp,
5098d2aee6bSAdrien Destugues#ifdef USE_KQUEUE
5108d2aee6bSAdrien Destugues			    kq,
5118d2aee6bSAdrien Destugues#endif
5128d2aee6bSAdrien Destugues			    buf, (size_t)buflen, ans, anssiz, &terrno,
5138d2aee6bSAdrien Destugues			    ns, tries, &v_circuit, &gotsomewhere);
5145d7cf21bSWaldemar Kornewald			if (n < 0)
5155d7cf21bSWaldemar Kornewald				goto fail;
5165d7cf21bSWaldemar Kornewald			if (n == 0)
5175d7cf21bSWaldemar Kornewald				goto next_ns;
5185d7cf21bSWaldemar Kornewald			if (v_circuit)
5195d7cf21bSWaldemar Kornewald				goto same_ns;
5205d7cf21bSWaldemar Kornewald			resplen = n;
5215d7cf21bSWaldemar Kornewald		}
5225d7cf21bSWaldemar Kornewald
5235d7cf21bSWaldemar Kornewald		Dprint((statp->options & RES_DEBUG) ||
5245d7cf21bSWaldemar Kornewald		       ((statp->pfcode & RES_PRF_REPLY) &&
5255d7cf21bSWaldemar Kornewald			(statp->pfcode & RES_PRF_HEAD1)),
5265d7cf21bSWaldemar Kornewald		       (stdout, ";; got answer:\n"));
5275d7cf21bSWaldemar Kornewald
5285d7cf21bSWaldemar Kornewald		DprintQ((statp->options & RES_DEBUG) ||
5295d7cf21bSWaldemar Kornewald			(statp->pfcode & RES_PRF_REPLY),
5305d7cf21bSWaldemar Kornewald			(stdout, "%s", ""),
5315d7cf21bSWaldemar Kornewald			ans, (resplen > anssiz) ? anssiz : resplen);
5325d7cf21bSWaldemar Kornewald
5335d7cf21bSWaldemar Kornewald		/*
5345d7cf21bSWaldemar Kornewald		 * If we have temporarily opened a virtual circuit,
5355d7cf21bSWaldemar Kornewald		 * or if we haven't been asked to keep a socket open,
5365d7cf21bSWaldemar Kornewald		 * close the socket.
5375d7cf21bSWaldemar Kornewald		 */
5385d7cf21bSWaldemar Kornewald		if ((v_circuit && (statp->options & RES_USEVC) == 0U) ||
5395d7cf21bSWaldemar Kornewald		    (statp->options & RES_STAYOPEN) == 0U) {
5405d7cf21bSWaldemar Kornewald			res_nclose(statp);
5415d7cf21bSWaldemar Kornewald		}
5425d7cf21bSWaldemar Kornewald		if (statp->rhook) {
5435d7cf21bSWaldemar Kornewald			int done = 0, loops = 0;
5445d7cf21bSWaldemar Kornewald
5455d7cf21bSWaldemar Kornewald			do {
5465d7cf21bSWaldemar Kornewald				res_sendhookact act;
5475d7cf21bSWaldemar Kornewald
5485d7cf21bSWaldemar Kornewald				act = (*statp->rhook)(nsap, buf, buflen,
5495d7cf21bSWaldemar Kornewald						      ans, anssiz, &resplen);
5505d7cf21bSWaldemar Kornewald				switch (act) {
5515d7cf21bSWaldemar Kornewald				case res_goahead:
5525d7cf21bSWaldemar Kornewald				case res_done:
5535d7cf21bSWaldemar Kornewald					done = 1;
5545d7cf21bSWaldemar Kornewald					break;
5555d7cf21bSWaldemar Kornewald				case res_nextns:
5565d7cf21bSWaldemar Kornewald					res_nclose(statp);
5575d7cf21bSWaldemar Kornewald					goto next_ns;
5585d7cf21bSWaldemar Kornewald				case res_modified:
5595d7cf21bSWaldemar Kornewald					/* give the hook another try */
5605d7cf21bSWaldemar Kornewald					if (++loops < 42) /*doug adams*/
5615d7cf21bSWaldemar Kornewald						break;
5625d7cf21bSWaldemar Kornewald					/*FALLTHROUGH*/
5635d7cf21bSWaldemar Kornewald				case res_error:
5645d7cf21bSWaldemar Kornewald					/*FALLTHROUGH*/
5655d7cf21bSWaldemar Kornewald				default:
5665d7cf21bSWaldemar Kornewald					goto fail;
5675d7cf21bSWaldemar Kornewald				}
5685d7cf21bSWaldemar Kornewald			} while (!done);
5695d7cf21bSWaldemar Kornewald
5705d7cf21bSWaldemar Kornewald		}
5718d2aee6bSAdrien Destugues#ifdef USE_KQUEUE
5728d2aee6bSAdrien Destugues		close(kq);
5738d2aee6bSAdrien Destugues#endif
5745d7cf21bSWaldemar Kornewald		return (resplen);
5755d7cf21bSWaldemar Kornewald next_ns: ;
5765d7cf21bSWaldemar Kornewald	   } /*foreach ns*/
5775d7cf21bSWaldemar Kornewald	} /*foreach retry*/
5785d7cf21bSWaldemar Kornewald	res_nclose(statp);
5798d2aee6bSAdrien Destugues#ifdef USE_KQUEUE
5808d2aee6bSAdrien Destugues	close(kq);
5818d2aee6bSAdrien Destugues#endif
5825d7cf21bSWaldemar Kornewald	if (!v_circuit) {
5835d7cf21bSWaldemar Kornewald		if (!gotsomewhere)
5844bd3cce6SAxel Dörfler			errno = ECONNREFUSED;	/*%< no nameservers found */
5855d7cf21bSWaldemar Kornewald		else
5864bd3cce6SAxel Dörfler			errno = ETIMEDOUT;	/*%< no answer obtained */
5875d7cf21bSWaldemar Kornewald	} else
5885d7cf21bSWaldemar Kornewald		errno = terrno;
5895d7cf21bSWaldemar Kornewald	return (-1);
5905d7cf21bSWaldemar Kornewald fail:
5915d7cf21bSWaldemar Kornewald	res_nclose(statp);
5928d2aee6bSAdrien Destugues#ifdef USE_KQUEUE
5938d2aee6bSAdrien Destugues	close(kq);
5948d2aee6bSAdrien Destugues#endif
5955d7cf21bSWaldemar Kornewald	return (-1);
5965d7cf21bSWaldemar Kornewald}
5975d7cf21bSWaldemar Kornewald
5985d7cf21bSWaldemar Kornewald/* Private */
5995d7cf21bSWaldemar Kornewald
6008d2aee6bSAdrien Destuguesstatic socklen_t
6018d2aee6bSAdrien Destuguesget_salen(const struct sockaddr *sa)
6025d7cf21bSWaldemar Kornewald{
6035d7cf21bSWaldemar Kornewald
6045d7cf21bSWaldemar Kornewald#ifdef HAVE_SA_LEN
6055d7cf21bSWaldemar Kornewald	/* There are people do not set sa_len.  Be forgiving to them. */
6065d7cf21bSWaldemar Kornewald	if (sa->sa_len)
6078d2aee6bSAdrien Destugues		return (socklen_t)sa->sa_len;
6085d7cf21bSWaldemar Kornewald#endif
6095d7cf21bSWaldemar Kornewald
6105d7cf21bSWaldemar Kornewald	if (sa->sa_family == AF_INET)
6118d2aee6bSAdrien Destugues		return (socklen_t)sizeof(struct sockaddr_in);
6125d7cf21bSWaldemar Kornewald	else if (sa->sa_family == AF_INET6)
6138d2aee6bSAdrien Destugues		return (socklen_t)sizeof(struct sockaddr_in6);
6145d7cf21bSWaldemar Kornewald	else
6158d2aee6bSAdrien Destugues		return 0;	/*%< unknown, die on connect */
6165d7cf21bSWaldemar Kornewald}
6175d7cf21bSWaldemar Kornewald
6184bd3cce6SAxel Dörfler/*%
6195d7cf21bSWaldemar Kornewald * pick appropriate nsaddr_list for use.  see res_init() for initialization.
6205d7cf21bSWaldemar Kornewald */
6215d7cf21bSWaldemar Kornewaldstatic struct sockaddr *
6228d2aee6bSAdrien Destuguesget_nsaddr(res_state statp, size_t n)
6235d7cf21bSWaldemar Kornewald{
6245d7cf21bSWaldemar Kornewald
6255d7cf21bSWaldemar Kornewald	if (!statp->nsaddr_list[n].sin_family && EXT(statp).ext) {
6265d7cf21bSWaldemar Kornewald		/*
6275d7cf21bSWaldemar Kornewald		 * - EXT(statp).ext->nsaddrs[n] holds an address that is larger
6285d7cf21bSWaldemar Kornewald		 *   than struct sockaddr, and
6295d7cf21bSWaldemar Kornewald		 * - user code did not update statp->nsaddr_list[n].
6305d7cf21bSWaldemar Kornewald		 */
6315d7cf21bSWaldemar Kornewald		return (struct sockaddr *)(void *)&EXT(statp).ext->nsaddrs[n];
6325d7cf21bSWaldemar Kornewald	} else {
6335d7cf21bSWaldemar Kornewald		/*
6345d7cf21bSWaldemar Kornewald		 * - user code updated statp->nsaddr_list[n], or
6355d7cf21bSWaldemar Kornewald		 * - statp->nsaddr_list[n] has the same content as
6365d7cf21bSWaldemar Kornewald		 *   EXT(statp).ext->nsaddrs[n].
6375d7cf21bSWaldemar Kornewald		 */
6385d7cf21bSWaldemar Kornewald		return (struct sockaddr *)(void *)&statp->nsaddr_list[n];
6395d7cf21bSWaldemar Kornewald	}
6405d7cf21bSWaldemar Kornewald}
6415d7cf21bSWaldemar Kornewald
6425d7cf21bSWaldemar Kornewaldstatic int
6435d7cf21bSWaldemar Kornewaldsend_vc(res_state statp,
6448d2aee6bSAdrien Destugues	const void *buf, size_t buflen, u_char *ans, int anssiz,
6455d7cf21bSWaldemar Kornewald	int *terrno, int ns)
6465d7cf21bSWaldemar Kornewald{
6478d2aee6bSAdrien Destugues	const HEADER *hp = (const HEADER *)(const void *)buf;
6488d2aee6bSAdrien Destugues	HEADER *anhp = (HEADER *)(void *)ans;
6495d7cf21bSWaldemar Kornewald	struct sockaddr *nsap;
6508d2aee6bSAdrien Destugues	socklen_t nsaplen;
6518d2aee6bSAdrien Destugues	int truncating, connreset, resplen;
6528d2aee6bSAdrien Destugues	ssize_t n;
6535d7cf21bSWaldemar Kornewald	struct iovec iov[2];
6545d7cf21bSWaldemar Kornewald	u_short len;
6555d7cf21bSWaldemar Kornewald	u_char *cp;
6565d7cf21bSWaldemar Kornewald	void *tmp;
6578d2aee6bSAdrien Destugues#if defined(SO_NOSIGPIPE) && SOCK_NOSIGPIPE == 0
6584bd3cce6SAxel Dörfler	int on = 1;
6594bd3cce6SAxel Dörfler#endif
6605d7cf21bSWaldemar Kornewald
6618d2aee6bSAdrien Destugues	nsap = get_nsaddr(statp, (size_t)ns);
6625d7cf21bSWaldemar Kornewald	nsaplen = get_salen(nsap);
6635d7cf21bSWaldemar Kornewald
6645d7cf21bSWaldemar Kornewald	connreset = 0;
6655d7cf21bSWaldemar Kornewald same_ns:
6665d7cf21bSWaldemar Kornewald	truncating = 0;
6675d7cf21bSWaldemar Kornewald
6685d7cf21bSWaldemar Kornewald	/* Are we still talking to whom we want to talk to? */
6695d7cf21bSWaldemar Kornewald	if (statp->_vcsock >= 0 && (statp->_flags & RES_F_VC) != 0) {
6705d7cf21bSWaldemar Kornewald		struct sockaddr_storage peer;
6715d7cf21bSWaldemar Kornewald		ISC_SOCKLEN_T size = sizeof peer;
6725d7cf21bSWaldemar Kornewald
6735d7cf21bSWaldemar Kornewald		if (getpeername(statp->_vcsock,
6748d2aee6bSAdrien Destugues				(struct sockaddr *)(void *)&peer, &size) < 0 ||
6758d2aee6bSAdrien Destugues		    !sock_eq((struct sockaddr *)(void *)&peer, nsap)) {
6765d7cf21bSWaldemar Kornewald			res_nclose(statp);
6775d7cf21bSWaldemar Kornewald			statp->_flags &= ~RES_F_VC;
6785d7cf21bSWaldemar Kornewald		}
6795d7cf21bSWaldemar Kornewald	}
6805d7cf21bSWaldemar Kornewald
6815d7cf21bSWaldemar Kornewald	if (statp->_vcsock < 0 || (statp->_flags & RES_F_VC) == 0) {
6825d7cf21bSWaldemar Kornewald		if (statp->_vcsock >= 0)
6835d7cf21bSWaldemar Kornewald			res_nclose(statp);
6845d7cf21bSWaldemar Kornewald
6858d2aee6bSAdrien Destugues#ifndef SOCK_CLOEXEC
6868d2aee6bSAdrien Destugues#define SOCK_CLOEXEC 0
6878d2aee6bSAdrien Destugues#endif
6888d2aee6bSAdrien Destugues
6898d2aee6bSAdrien Destugues		statp->_vcsock = socket(nsap->sa_family, SOCK_STREAM
6908d2aee6bSAdrien Destugues			| SOCK_NOSIGPIPE | SOCK_CLOEXEC, 0);
6918d2aee6bSAdrien Destugues#if SOCK_CLOEXEC == 0
6928d2aee6bSAdrien Destugues		fcntl(statp->_vcsock, F_SETFD, FD_CLOEXEC);
6938d2aee6bSAdrien Destugues#endif
6948d2aee6bSAdrien Destugues#if !defined(USE_POLL) && !defined(USE_KQUEUE)
6955d7cf21bSWaldemar Kornewald		if (statp->_vcsock > highestFD) {
6965d7cf21bSWaldemar Kornewald			res_nclose(statp);
6975d7cf21bSWaldemar Kornewald			errno = ENOTSOCK;
6985d7cf21bSWaldemar Kornewald		}
6998d2aee6bSAdrien Destugues#endif
7005d7cf21bSWaldemar Kornewald		if (statp->_vcsock < 0) {
7015d7cf21bSWaldemar Kornewald			switch (errno) {
7025d7cf21bSWaldemar Kornewald			case EPROTONOSUPPORT:
7035d7cf21bSWaldemar Kornewald#ifdef EPFNOSUPPORT
7045d7cf21bSWaldemar Kornewald			case EPFNOSUPPORT:
7055d7cf21bSWaldemar Kornewald#endif
7065d7cf21bSWaldemar Kornewald			case EAFNOSUPPORT:
7075d7cf21bSWaldemar Kornewald				Perror(statp, stderr, "socket(vc)", errno);
7085d7cf21bSWaldemar Kornewald				return (0);
7095d7cf21bSWaldemar Kornewald			default:
7105d7cf21bSWaldemar Kornewald				*terrno = errno;
7115d7cf21bSWaldemar Kornewald				Perror(statp, stderr, "socket(vc)", errno);
7125d7cf21bSWaldemar Kornewald				return (-1);
7135d7cf21bSWaldemar Kornewald			}
7145d7cf21bSWaldemar Kornewald		}
7158d2aee6bSAdrien Destugues#if defined(SO_NOSIGPIPE) && SOCK_NOSIGPIPE == 0
7164bd3cce6SAxel Dörfler		/*
7174bd3cce6SAxel Dörfler		 * Disable generation of SIGPIPE when writing to a closed
7184bd3cce6SAxel Dörfler		 * socket.  Write should return -1 and set errno to EPIPE
7194bd3cce6SAxel Dörfler		 * instead.
7204bd3cce6SAxel Dörfler		 *
7214bd3cce6SAxel Dörfler		 * Push on even if setsockopt(SO_NOSIGPIPE) fails.
7224bd3cce6SAxel Dörfler		 */
7234bd3cce6SAxel Dörfler		(void)setsockopt(statp->_vcsock, SOL_SOCKET, SO_NOSIGPIPE, &on,
7248d2aee6bSAdrien Destugues				 (socklen_t)sizeof(on));
7254bd3cce6SAxel Dörfler#endif
7265d7cf21bSWaldemar Kornewald		errno = 0;
7275d7cf21bSWaldemar Kornewald		if (connect(statp->_vcsock, nsap, nsaplen) < 0) {
7285d7cf21bSWaldemar Kornewald			*terrno = errno;
7295d7cf21bSWaldemar Kornewald			Aerror(statp, stderr, "connect/vc", errno, nsap,
7305d7cf21bSWaldemar Kornewald			    nsaplen);
7315d7cf21bSWaldemar Kornewald			res_nclose(statp);
7325d7cf21bSWaldemar Kornewald			return (0);
7335d7cf21bSWaldemar Kornewald		}
7345d7cf21bSWaldemar Kornewald		statp->_flags |= RES_F_VC;
7355d7cf21bSWaldemar Kornewald	}
7365d7cf21bSWaldemar Kornewald
7375d7cf21bSWaldemar Kornewald	/*
7385d7cf21bSWaldemar Kornewald	 * Send length & message
7395d7cf21bSWaldemar Kornewald	 */
7408d2aee6bSAdrien Destugues	ns_put16((u_short)buflen, (u_char*)(void *)&len);
7415d7cf21bSWaldemar Kornewald	iov[0] = evConsIovec(&len, INT16SZ);
7425d7cf21bSWaldemar Kornewald	DE_CONST(buf, tmp);
7438d2aee6bSAdrien Destugues	iov[1] = evConsIovec(tmp, (size_t)buflen);
7448d2aee6bSAdrien Destugues	if (writev(statp->_vcsock, iov, 2) != (ssize_t)(INT16SZ + buflen)) {
7455d7cf21bSWaldemar Kornewald		*terrno = errno;
7465d7cf21bSWaldemar Kornewald		Perror(statp, stderr, "write failed", errno);
7475d7cf21bSWaldemar Kornewald		res_nclose(statp);
7485d7cf21bSWaldemar Kornewald		return (0);
7495d7cf21bSWaldemar Kornewald	}
7505d7cf21bSWaldemar Kornewald	/*
7515d7cf21bSWaldemar Kornewald	 * Receive length & response
7525d7cf21bSWaldemar Kornewald	 */
7535d7cf21bSWaldemar Kornewald read_len:
7545d7cf21bSWaldemar Kornewald	cp = ans;
7555d7cf21bSWaldemar Kornewald	len = INT16SZ;
7568d2aee6bSAdrien Destugues	while ((n = read(statp->_vcsock, (char *)cp, (size_t)len)) > 0) {
7575d7cf21bSWaldemar Kornewald		cp += n;
7588d2aee6bSAdrien Destugues		if ((len -= (u_short)n) == 0)
7595d7cf21bSWaldemar Kornewald			break;
7605d7cf21bSWaldemar Kornewald	}
7615d7cf21bSWaldemar Kornewald	if (n <= 0) {
7625d7cf21bSWaldemar Kornewald		*terrno = errno;
7635d7cf21bSWaldemar Kornewald		Perror(statp, stderr, "read failed", errno);
7645d7cf21bSWaldemar Kornewald		res_nclose(statp);
7655d7cf21bSWaldemar Kornewald		/*
7665d7cf21bSWaldemar Kornewald		 * A long running process might get its TCP
7675d7cf21bSWaldemar Kornewald		 * connection reset if the remote server was
7685d7cf21bSWaldemar Kornewald		 * restarted.  Requery the server instead of
7695d7cf21bSWaldemar Kornewald		 * trying a new one.  When there is only one
7705d7cf21bSWaldemar Kornewald		 * server, this means that a query might work
7715d7cf21bSWaldemar Kornewald		 * instead of failing.  We only allow one reset
7725d7cf21bSWaldemar Kornewald		 * per query to prevent looping.
7735d7cf21bSWaldemar Kornewald		 */
7745d7cf21bSWaldemar Kornewald		if (*terrno == ECONNRESET && !connreset) {
7755d7cf21bSWaldemar Kornewald			connreset = 1;
7765d7cf21bSWaldemar Kornewald			res_nclose(statp);
7775d7cf21bSWaldemar Kornewald			goto same_ns;
7785d7cf21bSWaldemar Kornewald		}
7795d7cf21bSWaldemar Kornewald		res_nclose(statp);
7805d7cf21bSWaldemar Kornewald		return (0);
7815d7cf21bSWaldemar Kornewald	}
7825d7cf21bSWaldemar Kornewald	resplen = ns_get16(ans);
7835d7cf21bSWaldemar Kornewald	if (resplen > anssiz) {
7845d7cf21bSWaldemar Kornewald		Dprint(statp->options & RES_DEBUG,
7855d7cf21bSWaldemar Kornewald		       (stdout, ";; response truncated\n")
7865d7cf21bSWaldemar Kornewald		       );
7875d7cf21bSWaldemar Kornewald		truncating = 1;
7885d7cf21bSWaldemar Kornewald		len = anssiz;
7895d7cf21bSWaldemar Kornewald	} else
7905d7cf21bSWaldemar Kornewald		len = resplen;
7915d7cf21bSWaldemar Kornewald	if (len < HFIXEDSZ) {
7925d7cf21bSWaldemar Kornewald		/*
7935d7cf21bSWaldemar Kornewald		 * Undersized message.
7945d7cf21bSWaldemar Kornewald		 */
7955d7cf21bSWaldemar Kornewald		Dprint(statp->options & RES_DEBUG,
7965d7cf21bSWaldemar Kornewald		       (stdout, ";; undersized: %d\n", len));
7975d7cf21bSWaldemar Kornewald		*terrno = EMSGSIZE;
7985d7cf21bSWaldemar Kornewald		res_nclose(statp);
7995d7cf21bSWaldemar Kornewald		return (0);
8005d7cf21bSWaldemar Kornewald	}
8015d7cf21bSWaldemar Kornewald	cp = ans;
8028d2aee6bSAdrien Destugues	while (len != 0 && (n = read(statp->_vcsock, (char *)cp, (size_t)len)) > 0){
8035d7cf21bSWaldemar Kornewald		cp += n;
8048d2aee6bSAdrien Destugues		len -= (u_short)n;
8055d7cf21bSWaldemar Kornewald	}
8065d7cf21bSWaldemar Kornewald	if (n <= 0) {
8075d7cf21bSWaldemar Kornewald		*terrno = errno;
8085d7cf21bSWaldemar Kornewald		Perror(statp, stderr, "read(vc)", errno);
8095d7cf21bSWaldemar Kornewald		res_nclose(statp);
8105d7cf21bSWaldemar Kornewald		return (0);
8115d7cf21bSWaldemar Kornewald	}
8125d7cf21bSWaldemar Kornewald	if (truncating) {
8135d7cf21bSWaldemar Kornewald		/*
8145d7cf21bSWaldemar Kornewald		 * Flush rest of answer so connection stays in synch.
8155d7cf21bSWaldemar Kornewald		 */
8165d7cf21bSWaldemar Kornewald		anhp->tc = 1;
8175d7cf21bSWaldemar Kornewald		len = resplen - anssiz;
8185d7cf21bSWaldemar Kornewald		while (len != 0) {
8195d7cf21bSWaldemar Kornewald			char junk[PACKETSZ];
8205d7cf21bSWaldemar Kornewald
8215d7cf21bSWaldemar Kornewald			n = read(statp->_vcsock, junk,
8225d7cf21bSWaldemar Kornewald				 (len > sizeof junk) ? sizeof junk : len);
8235d7cf21bSWaldemar Kornewald			if (n > 0)
8248d2aee6bSAdrien Destugues				len -= (u_short)n;
8255d7cf21bSWaldemar Kornewald			else
8265d7cf21bSWaldemar Kornewald				break;
8275d7cf21bSWaldemar Kornewald		}
8285d7cf21bSWaldemar Kornewald	}
8295d7cf21bSWaldemar Kornewald	/*
8305d7cf21bSWaldemar Kornewald	 * If the calling applicating has bailed out of
8315d7cf21bSWaldemar Kornewald	 * a previous call and failed to arrange to have
8325d7cf21bSWaldemar Kornewald	 * the circuit closed or the server has got
8335d7cf21bSWaldemar Kornewald	 * itself confused, then drop the packet and
8345d7cf21bSWaldemar Kornewald	 * wait for the correct one.
8355d7cf21bSWaldemar Kornewald	 */
8365d7cf21bSWaldemar Kornewald	if (hp->id != anhp->id) {
8375d7cf21bSWaldemar Kornewald		DprintQ((statp->options & RES_DEBUG) ||
8385d7cf21bSWaldemar Kornewald			(statp->pfcode & RES_PRF_REPLY),
8395d7cf21bSWaldemar Kornewald			(stdout, ";; old answer (unexpected):\n"),
8405d7cf21bSWaldemar Kornewald			ans, (resplen > anssiz) ? anssiz: resplen);
8415d7cf21bSWaldemar Kornewald		goto read_len;
8425d7cf21bSWaldemar Kornewald	}
8435d7cf21bSWaldemar Kornewald
8445d7cf21bSWaldemar Kornewald	/*
8455d7cf21bSWaldemar Kornewald	 * All is well, or the error is fatal.  Signal that the
8465d7cf21bSWaldemar Kornewald	 * next nameserver ought not be tried.
8475d7cf21bSWaldemar Kornewald	 */
8485d7cf21bSWaldemar Kornewald	return (resplen);
8495d7cf21bSWaldemar Kornewald}
8505d7cf21bSWaldemar Kornewald
8515d7cf21bSWaldemar Kornewaldstatic int
8528d2aee6bSAdrien Destuguessend_dg(res_state statp,
8538d2aee6bSAdrien Destugues#ifdef USE_KQUEUE
8548d2aee6bSAdrien Destugues	int kq,
8558d2aee6bSAdrien Destugues#endif
8568d2aee6bSAdrien Destugues	const void *buf, size_t buflen, u_char *ans,
8574bd3cce6SAxel Dörfler	int anssiz, int *terrno, int ns, int tries, int *v_circuit,
8584bd3cce6SAxel Dörfler	int *gotsomewhere)
8595d7cf21bSWaldemar Kornewald{
8608d2aee6bSAdrien Destugues	const HEADER *hp = (const HEADER *)(const void *)buf;
8618d2aee6bSAdrien Destugues	HEADER *anhp = (HEADER *)(void *)ans;
8625d7cf21bSWaldemar Kornewald	const struct sockaddr *nsap;
8638d2aee6bSAdrien Destugues	socklen_t nsaplen;
8645d7cf21bSWaldemar Kornewald	struct timespec now, timeout, finish;
8655d7cf21bSWaldemar Kornewald	struct sockaddr_storage from;
8665d7cf21bSWaldemar Kornewald	ISC_SOCKLEN_T fromlen;
8678d2aee6bSAdrien Destugues	ssize_t resplen;
8688d2aee6bSAdrien Destugues	int seconds, n, s;
8698d2aee6bSAdrien Destugues#ifdef USE_KQUEUE
8708d2aee6bSAdrien Destugues	struct kevent kv;
8718d2aee6bSAdrien Destugues#else
8724bd3cce6SAxel Dörfler#ifdef USE_POLL
8734bd3cce6SAxel Dörfler	int     polltimeout;
8744bd3cce6SAxel Dörfler	struct pollfd   pollfd;
8754bd3cce6SAxel Dörfler#else
8764bd3cce6SAxel Dörfler	fd_set dsmask;
8778d2aee6bSAdrien Destugues#endif
8784bd3cce6SAxel Dörfler#endif
8795d7cf21bSWaldemar Kornewald
8808d2aee6bSAdrien Destugues	nsap = get_nsaddr(statp, (size_t)ns);
8815d7cf21bSWaldemar Kornewald	nsaplen = get_salen(nsap);
8825d7cf21bSWaldemar Kornewald	if (EXT(statp).nssocks[ns] == -1) {
8838d2aee6bSAdrien Destugues		EXT(statp).nssocks[ns] = socket(nsap->sa_family, SOCK_DGRAM
8848d2aee6bSAdrien Destugues			| SOCK_CLOEXEC, 0);
8858d2aee6bSAdrien Destugues#if SOCK_CLOEXEC == 0
8868d2aee6bSAdrien Destugues		fcntl(EXT(statp).nssocks[ns], F_SETFD, FD_CLOEXEC);
8878d2aee6bSAdrien Destugues#endif
8888d2aee6bSAdrien Destugues#if !defined(USE_POLL) && !defined(USE_KQUEUE)
8895d7cf21bSWaldemar Kornewald		if (EXT(statp).nssocks[ns] > highestFD) {
8905d7cf21bSWaldemar Kornewald			res_nclose(statp);
8915d7cf21bSWaldemar Kornewald			errno = ENOTSOCK;
8925d7cf21bSWaldemar Kornewald		}
8938d2aee6bSAdrien Destugues#endif
8945d7cf21bSWaldemar Kornewald		if (EXT(statp).nssocks[ns] < 0) {
8955d7cf21bSWaldemar Kornewald			switch (errno) {
8965d7cf21bSWaldemar Kornewald			case EPROTONOSUPPORT:
8975d7cf21bSWaldemar Kornewald#ifdef EPFNOSUPPORT
8985d7cf21bSWaldemar Kornewald			case EPFNOSUPPORT:
8995d7cf21bSWaldemar Kornewald#endif
9005d7cf21bSWaldemar Kornewald			case EAFNOSUPPORT:
9015d7cf21bSWaldemar Kornewald				Perror(statp, stderr, "socket(dg)", errno);
9025d7cf21bSWaldemar Kornewald				return (0);
9035d7cf21bSWaldemar Kornewald			default:
9045d7cf21bSWaldemar Kornewald				*terrno = errno;
9055d7cf21bSWaldemar Kornewald				Perror(statp, stderr, "socket(dg)", errno);
9065d7cf21bSWaldemar Kornewald				return (-1);
9075d7cf21bSWaldemar Kornewald			}
9085d7cf21bSWaldemar Kornewald		}
9095d7cf21bSWaldemar Kornewald#ifndef CANNOT_CONNECT_DGRAM
9105d7cf21bSWaldemar Kornewald		/*
9115d7cf21bSWaldemar Kornewald		 * On a 4.3BSD+ machine (client and server,
9125d7cf21bSWaldemar Kornewald		 * actually), sending to a nameserver datagram
9135d7cf21bSWaldemar Kornewald		 * port with no nameserver will cause an
9145d7cf21bSWaldemar Kornewald		 * ICMP port unreachable message to be returned.
9155d7cf21bSWaldemar Kornewald		 * If our datagram socket is "connected" to the
9165d7cf21bSWaldemar Kornewald		 * server, we get an ECONNREFUSED error on the next
9175d7cf21bSWaldemar Kornewald		 * socket operation, and select returns if the
9185d7cf21bSWaldemar Kornewald		 * error message is received.  We can thus detect
9195d7cf21bSWaldemar Kornewald		 * the absence of a nameserver without timing out.
9208d2aee6bSAdrien Destugues		 *
9218d2aee6bSAdrien Destugues		 * When the option "insecure1" is specified, we'd
9228d2aee6bSAdrien Destugues		 * rather expect to see responses from an "unknown"
9238d2aee6bSAdrien Destugues		 * address.  In order to let the kernel accept such
9248d2aee6bSAdrien Destugues		 * responses, do not connect the socket here.
9258d2aee6bSAdrien Destugues		 * XXX: or do we need an explicit option to disable
9268d2aee6bSAdrien Destugues		 * connecting?
9278d2aee6bSAdrien Destugues 		 */
9288d2aee6bSAdrien Destugues		if (!(statp->options & RES_INSECURE1) &&
9298d2aee6bSAdrien Destugues		    connect(EXT(statp).nssocks[ns], nsap, nsaplen) < 0) {
9305d7cf21bSWaldemar Kornewald			Aerror(statp, stderr, "connect(dg)", errno, nsap,
9315d7cf21bSWaldemar Kornewald			    nsaplen);
9325d7cf21bSWaldemar Kornewald			res_nclose(statp);
9335d7cf21bSWaldemar Kornewald			return (0);
9345d7cf21bSWaldemar Kornewald		}
9355d7cf21bSWaldemar Kornewald#endif /* !CANNOT_CONNECT_DGRAM */
9365d7cf21bSWaldemar Kornewald		Dprint(statp->options & RES_DEBUG,
9375d7cf21bSWaldemar Kornewald		       (stdout, ";; new DG socket\n"))
9385d7cf21bSWaldemar Kornewald	}
9395d7cf21bSWaldemar Kornewald	s = EXT(statp).nssocks[ns];
9405d7cf21bSWaldemar Kornewald#ifndef CANNOT_CONNECT_DGRAM
9418d2aee6bSAdrien Destugues	if (statp->options & RES_INSECURE1) {
9428d2aee6bSAdrien Destugues		if (sendto(s, buf, buflen, 0, nsap, nsaplen) !=
9438d2aee6bSAdrien Destugues		    (ssize_t)buflen) {
9448d2aee6bSAdrien Destugues			Aerror(statp, stderr, "sendto", errno, nsap, nsaplen);
9458d2aee6bSAdrien Destugues			res_nclose(statp);
9468d2aee6bSAdrien Destugues			return (0);
9478d2aee6bSAdrien Destugues		}
9488d2aee6bSAdrien Destugues	} else if (send(s, buf, buflen, 0) != (ssize_t)buflen) {
9495d7cf21bSWaldemar Kornewald		Perror(statp, stderr, "send", errno);
9505d7cf21bSWaldemar Kornewald		res_nclose(statp);
9515d7cf21bSWaldemar Kornewald		return (0);
9525d7cf21bSWaldemar Kornewald	}
9535d7cf21bSWaldemar Kornewald#else /* !CANNOT_CONNECT_DGRAM */
9548d2aee6bSAdrien Destugues	if (sendto(s, buf, buflen, 0, nsap, nsaplen) != (ssize_t)buflen)
9555d7cf21bSWaldemar Kornewald	{
9565d7cf21bSWaldemar Kornewald		Aerror(statp, stderr, "sendto", errno, nsap, nsaplen);
9575d7cf21bSWaldemar Kornewald		res_nclose(statp);
9585d7cf21bSWaldemar Kornewald		return (0);
9595d7cf21bSWaldemar Kornewald	}
9605d7cf21bSWaldemar Kornewald#endif /* !CANNOT_CONNECT_DGRAM */
9615d7cf21bSWaldemar Kornewald
9625d7cf21bSWaldemar Kornewald	/*
9635d7cf21bSWaldemar Kornewald	 * Wait for reply.
9645d7cf21bSWaldemar Kornewald	 */
9654bd3cce6SAxel Dörfler	seconds = (statp->retrans << tries);
9665d7cf21bSWaldemar Kornewald	if (ns > 0)
9675d7cf21bSWaldemar Kornewald		seconds /= statp->nscount;
9685d7cf21bSWaldemar Kornewald	if (seconds <= 0)
9695d7cf21bSWaldemar Kornewald		seconds = 1;
9705d7cf21bSWaldemar Kornewald	now = evNowTime();
9718d2aee6bSAdrien Destugues	timeout = evConsTime((time_t)seconds, 0L);
9725d7cf21bSWaldemar Kornewald	finish = evAddTime(now, timeout);
9735d7cf21bSWaldemar Kornewald	goto nonow;
9745d7cf21bSWaldemar Kornewald wait:
9755d7cf21bSWaldemar Kornewald	now = evNowTime();
9765d7cf21bSWaldemar Kornewald nonow:
9774bd3cce6SAxel Dörfler#ifndef USE_POLL
9785d7cf21bSWaldemar Kornewald	if (evCmpTime(finish, now) > 0)
9795d7cf21bSWaldemar Kornewald		timeout = evSubTime(finish, now);
9805d7cf21bSWaldemar Kornewald	else
9818d2aee6bSAdrien Destugues		timeout = evConsTime(0L, 0L);
9828d2aee6bSAdrien Destugues#ifdef USE_KQUEUE
9838d2aee6bSAdrien Destugues	EV_SET(&kv, s, EVFILT_READ, EV_ADD | EV_ONESHOT, 0, 0, 0);
9848d2aee6bSAdrien Destugues	n = kevent(kq, &kv, 1, &kv, 1, &timeout);
9858d2aee6bSAdrien Destugues#else
9868d2aee6bSAdrien Destugues	FD_ZERO(&dsmask);
9878d2aee6bSAdrien Destugues	FD_SET(s, &dsmask);
9885d7cf21bSWaldemar Kornewald	n = pselect(s + 1, &dsmask, NULL, NULL, &timeout, NULL);
9898d2aee6bSAdrien Destugues#endif
9904bd3cce6SAxel Dörfler#else
9914bd3cce6SAxel Dörfler	timeout = evSubTime(finish, now);
9924bd3cce6SAxel Dörfler	if (timeout.tv_sec < 0)
9938d2aee6bSAdrien Destugues		timeout = evConsTime((time_t)0, 0L);
9948d2aee6bSAdrien Destugues	polltimeout = 1000*(int)timeout.tv_sec +
9958d2aee6bSAdrien Destugues		(int)timeout.tv_nsec/1000000;
9964bd3cce6SAxel Dörfler	pollfd.fd = s;
9974bd3cce6SAxel Dörfler	pollfd.events = POLLRDNORM;
9984bd3cce6SAxel Dörfler	n = poll(&pollfd, 1, polltimeout);
9994bd3cce6SAxel Dörfler#endif /* USE_POLL */
10004bd3cce6SAxel Dörfler
10015d7cf21bSWaldemar Kornewald	if (n == 0) {
10025d7cf21bSWaldemar Kornewald		Dprint(statp->options & RES_DEBUG, (stdout, ";; timeout\n"));
10035d7cf21bSWaldemar Kornewald		*gotsomewhere = 1;
10045d7cf21bSWaldemar Kornewald		return (0);
10055d7cf21bSWaldemar Kornewald	}
10065d7cf21bSWaldemar Kornewald	if (n < 0) {
10078d2aee6bSAdrien Destugues#if DEBUG
10088d2aee6bSAdrien Destugues#if defined(USE_POLL)
10098d2aee6bSAdrien Destugues		static const char *fun = "poll";
10108d2aee6bSAdrien Destugues#elif defined(USE_KQUEUE)
10118d2aee6bSAdrien Destugues		static const char *fun = "kevent";
10128d2aee6bSAdrien Destugues#else
10138d2aee6bSAdrien Destugues		static const char *fun = "select";
10148d2aee6bSAdrien Destugues#endif
10158d2aee6bSAdrien Destugues#endif
10165d7cf21bSWaldemar Kornewald		if (errno == EINTR)
10175d7cf21bSWaldemar Kornewald			goto wait;
10188d2aee6bSAdrien Destugues		Perror(statp, stderr, fun, errno);
10195d7cf21bSWaldemar Kornewald		res_nclose(statp);
10205d7cf21bSWaldemar Kornewald		return (0);
10215d7cf21bSWaldemar Kornewald	}
10228d2aee6bSAdrien Destugues#ifdef USE_KQUEUE
10238d2aee6bSAdrien Destugues	if ((int)kv.ident != s)
10248d2aee6bSAdrien Destugues		goto wait;
10258d2aee6bSAdrien Destugues#endif
10265d7cf21bSWaldemar Kornewald	errno = 0;
10275d7cf21bSWaldemar Kornewald	fromlen = sizeof(from);
10288d2aee6bSAdrien Destugues	resplen = recvfrom(s, (char*)ans, (size_t)anssiz,0,
10298d2aee6bSAdrien Destugues			   (struct sockaddr *)(void *)&from, &fromlen);
10305d7cf21bSWaldemar Kornewald	if (resplen <= 0) {
10315d7cf21bSWaldemar Kornewald		Perror(statp, stderr, "recvfrom", errno);
10325d7cf21bSWaldemar Kornewald		res_nclose(statp);
10335d7cf21bSWaldemar Kornewald		return (0);
10345d7cf21bSWaldemar Kornewald	}
10355d7cf21bSWaldemar Kornewald	*gotsomewhere = 1;
10365d7cf21bSWaldemar Kornewald	if (resplen < HFIXEDSZ) {
10375d7cf21bSWaldemar Kornewald		/*
10385d7cf21bSWaldemar Kornewald		 * Undersized message.
10395d7cf21bSWaldemar Kornewald		 */
10405d7cf21bSWaldemar Kornewald		Dprint(statp->options & RES_DEBUG,
10418d2aee6bSAdrien Destugues		       (stdout, ";; undersized: %zd\n",
10425d7cf21bSWaldemar Kornewald			resplen));
10435d7cf21bSWaldemar Kornewald		*terrno = EMSGSIZE;
10445d7cf21bSWaldemar Kornewald		res_nclose(statp);
10455d7cf21bSWaldemar Kornewald		return (0);
10465d7cf21bSWaldemar Kornewald	}
10475d7cf21bSWaldemar Kornewald	if (hp->id != anhp->id) {
10485d7cf21bSWaldemar Kornewald		/*
10495d7cf21bSWaldemar Kornewald		 * response from old query, ignore it.
10505d7cf21bSWaldemar Kornewald		 * XXX - potential security hazard could
10515d7cf21bSWaldemar Kornewald		 *	 be detected here.
10525d7cf21bSWaldemar Kornewald		 */
10535d7cf21bSWaldemar Kornewald		DprintQ((statp->options & RES_DEBUG) ||
10545d7cf21bSWaldemar Kornewald			(statp->pfcode & RES_PRF_REPLY),
10555d7cf21bSWaldemar Kornewald			(stdout, ";; old answer:\n"),
10565d7cf21bSWaldemar Kornewald			ans, (resplen > anssiz) ? anssiz : resplen);
10575d7cf21bSWaldemar Kornewald		goto wait;
10585d7cf21bSWaldemar Kornewald	}
10595d7cf21bSWaldemar Kornewald	if (!(statp->options & RES_INSECURE1) &&
10608d2aee6bSAdrien Destugues	    !res_ourserver_p(statp, (struct sockaddr *)(void *)&from)) {
10615d7cf21bSWaldemar Kornewald		/*
10625d7cf21bSWaldemar Kornewald		 * response from wrong server? ignore it.
10635d7cf21bSWaldemar Kornewald		 * XXX - potential security hazard could
10645d7cf21bSWaldemar Kornewald		 *	 be detected here.
10655d7cf21bSWaldemar Kornewald		 */
10665d7cf21bSWaldemar Kornewald		DprintQ((statp->options & RES_DEBUG) ||
10675d7cf21bSWaldemar Kornewald			(statp->pfcode & RES_PRF_REPLY),
10685d7cf21bSWaldemar Kornewald			(stdout, ";; not our server:\n"),
10695d7cf21bSWaldemar Kornewald			ans, (resplen > anssiz) ? anssiz : resplen);
10705d7cf21bSWaldemar Kornewald		goto wait;
10715d7cf21bSWaldemar Kornewald	}
10725d7cf21bSWaldemar Kornewald#ifdef RES_USE_EDNS0
10735d7cf21bSWaldemar Kornewald	if (anhp->rcode == FORMERR && (statp->options & RES_USE_EDNS0) != 0U) {
10745d7cf21bSWaldemar Kornewald		/*
10755d7cf21bSWaldemar Kornewald		 * Do not retry if the server do not understand EDNS0.
10765d7cf21bSWaldemar Kornewald		 * The case has to be captured here, as FORMERR packet do not
10775d7cf21bSWaldemar Kornewald		 * carry query section, hence res_queriesmatch() returns 0.
10785d7cf21bSWaldemar Kornewald		 */
10795d7cf21bSWaldemar Kornewald		DprintQ(statp->options & RES_DEBUG,
10805d7cf21bSWaldemar Kornewald			(stdout, "server rejected query with EDNS0:\n"),
10815d7cf21bSWaldemar Kornewald			ans, (resplen > anssiz) ? anssiz : resplen);
10825d7cf21bSWaldemar Kornewald		/* record the error */
10835d7cf21bSWaldemar Kornewald		statp->_flags |= RES_F_EDNS0ERR;
10845d7cf21bSWaldemar Kornewald		res_nclose(statp);
10855d7cf21bSWaldemar Kornewald		return (0);
10865d7cf21bSWaldemar Kornewald	}
10875d7cf21bSWaldemar Kornewald#endif
10885d7cf21bSWaldemar Kornewald	if (!(statp->options & RES_INSECURE2) &&
10898d2aee6bSAdrien Destugues	    !res_queriesmatch(buf, (const u_char *)buf + buflen,
10905d7cf21bSWaldemar Kornewald			      ans, ans + anssiz)) {
10915d7cf21bSWaldemar Kornewald		/*
10925d7cf21bSWaldemar Kornewald		 * response contains wrong query? ignore it.
10935d7cf21bSWaldemar Kornewald		 * XXX - potential security hazard could
10945d7cf21bSWaldemar Kornewald		 *	 be detected here.
10955d7cf21bSWaldemar Kornewald		 */
10965d7cf21bSWaldemar Kornewald		DprintQ((statp->options & RES_DEBUG) ||
10975d7cf21bSWaldemar Kornewald			(statp->pfcode & RES_PRF_REPLY),
10985d7cf21bSWaldemar Kornewald			(stdout, ";; wrong query name:\n"),
10998d2aee6bSAdrien Destugues			ans, (int)(resplen > anssiz) ? anssiz : resplen);
11005d7cf21bSWaldemar Kornewald		goto wait;
11015d7cf21bSWaldemar Kornewald	}
11025d7cf21bSWaldemar Kornewald	if (anhp->rcode == SERVFAIL ||
11035d7cf21bSWaldemar Kornewald	    anhp->rcode == NOTIMP ||
11045d7cf21bSWaldemar Kornewald	    anhp->rcode == REFUSED) {
11055d7cf21bSWaldemar Kornewald		DprintQ(statp->options & RES_DEBUG,
11065d7cf21bSWaldemar Kornewald			(stdout, "server rejected query:\n"),
11078d2aee6bSAdrien Destugues			ans, (int)(resplen > anssiz) ? anssiz : resplen);
11085d7cf21bSWaldemar Kornewald		res_nclose(statp);
11095d7cf21bSWaldemar Kornewald		/* don't retry if called from dig */
11105d7cf21bSWaldemar Kornewald		if (!statp->pfcode)
11115d7cf21bSWaldemar Kornewald			return (0);
11125d7cf21bSWaldemar Kornewald	}
11135d7cf21bSWaldemar Kornewald	if (!(statp->options & RES_IGNTC) && anhp->tc) {
11145d7cf21bSWaldemar Kornewald		/*
11155d7cf21bSWaldemar Kornewald		 * To get the rest of answer,
11165d7cf21bSWaldemar Kornewald		 * use TCP with same server.
11175d7cf21bSWaldemar Kornewald		 */
11185d7cf21bSWaldemar Kornewald		Dprint(statp->options & RES_DEBUG,
11195d7cf21bSWaldemar Kornewald		       (stdout, ";; truncated answer\n"));
11205d7cf21bSWaldemar Kornewald		*v_circuit = 1;
11215d7cf21bSWaldemar Kornewald		res_nclose(statp);
11225d7cf21bSWaldemar Kornewald		return (1);
11235d7cf21bSWaldemar Kornewald	}
11245d7cf21bSWaldemar Kornewald	/*
11255d7cf21bSWaldemar Kornewald	 * All is well, or the error is fatal.  Signal that the
11265d7cf21bSWaldemar Kornewald	 * next nameserver ought not be tried.
11275d7cf21bSWaldemar Kornewald	 */
11288d2aee6bSAdrien Destugues	assert(INT_MIN <= resplen && resplen <= INT_MAX);
11298d2aee6bSAdrien Destugues	return (int)resplen;
11305d7cf21bSWaldemar Kornewald}
11315d7cf21bSWaldemar Kornewald
113251974c5dSOliver Tappe#ifdef DEBUG
113351974c5dSOliver Tappe
11345d7cf21bSWaldemar Kornewaldstatic void
11355d7cf21bSWaldemar KornewaldAerror(const res_state statp, FILE *file, const char *string, int error,
11368d2aee6bSAdrien Destugues       const struct sockaddr *address, socklen_t alen)
11375d7cf21bSWaldemar Kornewald{
11385d7cf21bSWaldemar Kornewald	int save = errno;
11395d7cf21bSWaldemar Kornewald	char hbuf[NI_MAXHOST];
11405d7cf21bSWaldemar Kornewald	char sbuf[NI_MAXSERV];
11415d7cf21bSWaldemar Kornewald
11425d7cf21bSWaldemar Kornewald	alen = alen;
11435d7cf21bSWaldemar Kornewald
11445d7cf21bSWaldemar Kornewald	if ((statp->options & RES_DEBUG) != 0U) {
11458d2aee6bSAdrien Destugues		if (getnameinfo(address, alen, hbuf,
11468d2aee6bSAdrien Destugues				(socklen_t)sizeof(hbuf), sbuf, (socklen_t)sizeof(sbuf),
11478d2aee6bSAdrien Destugues				niflags)) {
11485d7cf21bSWaldemar Kornewald			strncpy(hbuf, "?", sizeof(hbuf) - 1);
11495d7cf21bSWaldemar Kornewald			hbuf[sizeof(hbuf) - 1] = '\0';
11505d7cf21bSWaldemar Kornewald			strncpy(sbuf, "?", sizeof(sbuf) - 1);
11515d7cf21bSWaldemar Kornewald			sbuf[sizeof(sbuf) - 1] = '\0';
11525d7cf21bSWaldemar Kornewald		}
11535d7cf21bSWaldemar Kornewald		fprintf(file, "res_send: %s ([%s].%s): %s\n",
11545d7cf21bSWaldemar Kornewald			string, hbuf, sbuf, strerror(error));
11555d7cf21bSWaldemar Kornewald	}
11565d7cf21bSWaldemar Kornewald	errno = save;
11575d7cf21bSWaldemar Kornewald}
11585d7cf21bSWaldemar Kornewald
11595d7cf21bSWaldemar Kornewaldstatic void
11605d7cf21bSWaldemar KornewaldPerror(const res_state statp, FILE *file, const char *string, int error) {
11615d7cf21bSWaldemar Kornewald	int save = errno;
11625d7cf21bSWaldemar Kornewald
11635d7cf21bSWaldemar Kornewald	if ((statp->options & RES_DEBUG) != 0U)
11645d7cf21bSWaldemar Kornewald		fprintf(file, "res_send: %s: %s\n",
11655d7cf21bSWaldemar Kornewald			string, strerror(error));
11665d7cf21bSWaldemar Kornewald	errno = save;
11675d7cf21bSWaldemar Kornewald}
11685d7cf21bSWaldemar Kornewald
116951974c5dSOliver Tappe#endif	// DEBUG
117051974c5dSOliver Tappe
11715d7cf21bSWaldemar Kornewaldstatic int
11725d7cf21bSWaldemar Kornewaldsock_eq(struct sockaddr *a, struct sockaddr *b) {
11735d7cf21bSWaldemar Kornewald	struct sockaddr_in *a4, *b4;
11745d7cf21bSWaldemar Kornewald	struct sockaddr_in6 *a6, *b6;
11755d7cf21bSWaldemar Kornewald
11765d7cf21bSWaldemar Kornewald	if (a->sa_family != b->sa_family)
11775d7cf21bSWaldemar Kornewald		return 0;
11785d7cf21bSWaldemar Kornewald	switch (a->sa_family) {
11795d7cf21bSWaldemar Kornewald	case AF_INET:
11808d2aee6bSAdrien Destugues		a4 = (struct sockaddr_in *)(void *)a;
11818d2aee6bSAdrien Destugues		b4 = (struct sockaddr_in *)(void *)b;
11825d7cf21bSWaldemar Kornewald		return a4->sin_port == b4->sin_port &&
11835d7cf21bSWaldemar Kornewald		    a4->sin_addr.s_addr == b4->sin_addr.s_addr;
11845d7cf21bSWaldemar Kornewald	case AF_INET6:
11858d2aee6bSAdrien Destugues		a6 = (struct sockaddr_in6 *)(void *)a;
11868d2aee6bSAdrien Destugues		b6 = (struct sockaddr_in6 *)(void *)b;
11875d7cf21bSWaldemar Kornewald		return a6->sin6_port == b6->sin6_port &&
11885d7cf21bSWaldemar Kornewald#ifdef HAVE_SIN6_SCOPE_ID
11895d7cf21bSWaldemar Kornewald		    a6->sin6_scope_id == b6->sin6_scope_id &&
11905d7cf21bSWaldemar Kornewald#endif
11915d7cf21bSWaldemar Kornewald		    IN6_ARE_ADDR_EQUAL(&a6->sin6_addr, &b6->sin6_addr);
11925d7cf21bSWaldemar Kornewald	default:
11935d7cf21bSWaldemar Kornewald		return 0;
11945d7cf21bSWaldemar Kornewald	}
11955d7cf21bSWaldemar Kornewald}
11965d7cf21bSWaldemar Kornewald
11978d2aee6bSAdrien Destugues#if defined(NEED_PSELECT) && !defined(USE_POLL) && !defined(USE_KQUEUE)
11985d7cf21bSWaldemar Kornewald/* XXX needs to move to the porting library. */
11995d7cf21bSWaldemar Kornewaldstatic int
12005d7cf21bSWaldemar Kornewaldpselect(int nfds, void *rfds, void *wfds, void *efds,
12015d7cf21bSWaldemar Kornewald	struct timespec *tsp, const sigset_t *sigmask)
12025d7cf21bSWaldemar Kornewald{
12035d7cf21bSWaldemar Kornewald	struct timeval tv, *tvp;
12045d7cf21bSWaldemar Kornewald	sigset_t sigs;
12055d7cf21bSWaldemar Kornewald	int n;
12065d7cf21bSWaldemar Kornewald
12075d7cf21bSWaldemar Kornewald	if (tsp) {
12085d7cf21bSWaldemar Kornewald		tvp = &tv;
12095d7cf21bSWaldemar Kornewald		tv = evTimeVal(*tsp);
12105d7cf21bSWaldemar Kornewald	} else
12115d7cf21bSWaldemar Kornewald		tvp = NULL;
12125d7cf21bSWaldemar Kornewald	if (sigmask)
12135d7cf21bSWaldemar Kornewald		sigprocmask(SIG_SETMASK, sigmask, &sigs);
12145d7cf21bSWaldemar Kornewald	n = select(nfds, rfds, wfds, efds, tvp);
12155d7cf21bSWaldemar Kornewald	if (sigmask)
12165d7cf21bSWaldemar Kornewald		sigprocmask(SIG_SETMASK, &sigs, NULL);
12175d7cf21bSWaldemar Kornewald	if (tsp)
12185d7cf21bSWaldemar Kornewald		*tsp = evTimeSpec(tv);
12195d7cf21bSWaldemar Kornewald	return (n);
12205d7cf21bSWaldemar Kornewald}
12215d7cf21bSWaldemar Kornewald#endif
1222