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