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