17c478bd9Sstevel@tonic-gate /* 2*e8031f0aSraf * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 37c478bd9Sstevel@tonic-gate * Use is subject to license terms. 47c478bd9Sstevel@tonic-gate */ 57c478bd9Sstevel@tonic-gate 67c478bd9Sstevel@tonic-gate /* 77c478bd9Sstevel@tonic-gate * Copyright (c) 1985, 1989, 1993 87c478bd9Sstevel@tonic-gate * The Regents of the University of California. All rights reserved. 97c478bd9Sstevel@tonic-gate * 107c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 117c478bd9Sstevel@tonic-gate * modification, are permitted provided that the following conditions 127c478bd9Sstevel@tonic-gate * are met: 137c478bd9Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 147c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 157c478bd9Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 167c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 177c478bd9Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 187c478bd9Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software 197c478bd9Sstevel@tonic-gate * must display the following acknowledgement: 207c478bd9Sstevel@tonic-gate * This product includes software developed by the University of 217c478bd9Sstevel@tonic-gate * California, Berkeley and its contributors. 227c478bd9Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors 237c478bd9Sstevel@tonic-gate * may be used to endorse or promote products derived from this software 247c478bd9Sstevel@tonic-gate * without specific prior written permission. 257c478bd9Sstevel@tonic-gate * 267c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 277c478bd9Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 287c478bd9Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 297c478bd9Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 307c478bd9Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 317c478bd9Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 327c478bd9Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 337c478bd9Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 347c478bd9Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 357c478bd9Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 367c478bd9Sstevel@tonic-gate * SUCH DAMAGE. 377c478bd9Sstevel@tonic-gate */ 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate /* 407c478bd9Sstevel@tonic-gate * Portions Copyright (c) 1993 by Digital Equipment Corporation. 417c478bd9Sstevel@tonic-gate * 427c478bd9Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any 437c478bd9Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above 447c478bd9Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies, and that 457c478bd9Sstevel@tonic-gate * the name of Digital Equipment Corporation not be used in advertising or 467c478bd9Sstevel@tonic-gate * publicity pertaining to distribution of the document or software without 477c478bd9Sstevel@tonic-gate * specific, written prior permission. 487c478bd9Sstevel@tonic-gate * 497c478bd9Sstevel@tonic-gate * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 507c478bd9Sstevel@tonic-gate * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 517c478bd9Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 527c478bd9Sstevel@tonic-gate * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 537c478bd9Sstevel@tonic-gate * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 547c478bd9Sstevel@tonic-gate * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 557c478bd9Sstevel@tonic-gate * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 567c478bd9Sstevel@tonic-gate * SOFTWARE. 577c478bd9Sstevel@tonic-gate */ 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate /* 607c478bd9Sstevel@tonic-gate * Portions Copyright (c) 1996-1999 by Internet Software Consortium. 617c478bd9Sstevel@tonic-gate * 627c478bd9Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any 637c478bd9Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above 647c478bd9Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies. 657c478bd9Sstevel@tonic-gate * 667c478bd9Sstevel@tonic-gate * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS 677c478bd9Sstevel@tonic-gate * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES 687c478bd9Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE 697c478bd9Sstevel@tonic-gate * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 707c478bd9Sstevel@tonic-gate * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 717c478bd9Sstevel@tonic-gate * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 727c478bd9Sstevel@tonic-gate * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 737c478bd9Sstevel@tonic-gate * SOFTWARE. 747c478bd9Sstevel@tonic-gate */ 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint) 797c478bd9Sstevel@tonic-gate static const char sccsid[] = "@(#)res_send.c 8.1 (Berkeley) 6/4/93"; 807c478bd9Sstevel@tonic-gate static const char rcsid[] = "$Id: res_send.c,v 8.52 2003/04/29 02:13:08 marka Exp $"; 817c478bd9Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */ 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate /* 847c478bd9Sstevel@tonic-gate * Send query to name server and wait for reply. 857c478bd9Sstevel@tonic-gate */ 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate #include "port_before.h" 887c478bd9Sstevel@tonic-gate #include "fd_setsize.h" 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate #include <sys/types.h> 917c478bd9Sstevel@tonic-gate #include <sys/param.h> 927c478bd9Sstevel@tonic-gate #include <sys/time.h> 937c478bd9Sstevel@tonic-gate #include <sys/socket.h> 947c478bd9Sstevel@tonic-gate #include <sys/uio.h> 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate #include <netinet/in.h> 977c478bd9Sstevel@tonic-gate #include <arpa/nameser.h> 987c478bd9Sstevel@tonic-gate #include <arpa/inet.h> 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate #include <errno.h> 1017c478bd9Sstevel@tonic-gate #include <netdb.h> 1027c478bd9Sstevel@tonic-gate #include <resolv.h> 1037c478bd9Sstevel@tonic-gate #include <signal.h> 1047c478bd9Sstevel@tonic-gate #include <stdio.h> 1057c478bd9Sstevel@tonic-gate #include <stdlib.h> 1067c478bd9Sstevel@tonic-gate #include <string.h> 1077c478bd9Sstevel@tonic-gate #include <unistd.h> 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate #include <isc/eventlib.h> 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate #include "port_after.h" 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate #ifdef SUNW_POLL 1147c478bd9Sstevel@tonic-gate #include <stropts.h> 1157c478bd9Sstevel@tonic-gate #include <poll.h> 116*e8031f0aSraf #if defined(pselect) 117*e8031f0aSraf #undef pselect 118*e8031f0aSraf #endif 1197c478bd9Sstevel@tonic-gate #define pselect Pselect 1207c478bd9Sstevel@tonic-gate #endif /* SUNW_POLL */ 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate /* Options. Leave them on. */ 1237c478bd9Sstevel@tonic-gate #define DEBUG 1247c478bd9Sstevel@tonic-gate #include "res_debug.h" 1257c478bd9Sstevel@tonic-gate #include "res_private.h" 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate #define EXT(res) ((res)->_u._ext) 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate #ifdef ORIGINAL_ISC_CODE 1307c478bd9Sstevel@tonic-gate static const int highestFD = FD_SETSIZE - 1; 1317c478bd9Sstevel@tonic-gate #else 1327c478bd9Sstevel@tonic-gate static int highestFD = 0; 1337c478bd9Sstevel@tonic-gate #endif 1347c478bd9Sstevel@tonic-gate 1357c478bd9Sstevel@tonic-gate /* Forward. */ 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate static int get_salen __P((const struct sockaddr *)); 1387c478bd9Sstevel@tonic-gate static struct sockaddr * get_nsaddr __P((res_state, size_t)); 1397c478bd9Sstevel@tonic-gate static int send_vc(res_state, const u_char *, int, 1407c478bd9Sstevel@tonic-gate u_char *, int, int *, int); 1417c478bd9Sstevel@tonic-gate static int send_dg(res_state, const u_char *, int, 1427c478bd9Sstevel@tonic-gate u_char *, int, int *, int, 1437c478bd9Sstevel@tonic-gate int *, int *); 1447c478bd9Sstevel@tonic-gate static void Aerror(const res_state, FILE *, const char *, int, 1457c478bd9Sstevel@tonic-gate const struct sockaddr *, int); 1467c478bd9Sstevel@tonic-gate static void Perror(const res_state, FILE *, const char *, int); 1477c478bd9Sstevel@tonic-gate static int sock_eq(struct sockaddr *, struct sockaddr *); 1487c478bd9Sstevel@tonic-gate #ifdef NEED_PSELECT 1497c478bd9Sstevel@tonic-gate static int pselect(int, void *, void *, void *, 1507c478bd9Sstevel@tonic-gate struct timespec *, 1517c478bd9Sstevel@tonic-gate const sigset_t *); 1527c478bd9Sstevel@tonic-gate #endif 1537c478bd9Sstevel@tonic-gate void res_pquery(const res_state, const u_char *, int, FILE *); 1547c478bd9Sstevel@tonic-gate 1557c478bd9Sstevel@tonic-gate #ifdef ORIGINAL_ISC_CODE 1567c478bd9Sstevel@tonic-gate #else 1577c478bd9Sstevel@tonic-gate #pragma weak __res_nameinquery = res_nameinquery 1587c478bd9Sstevel@tonic-gate #pragma weak __res_queriesmatch = res_queriesmatch 1597c478bd9Sstevel@tonic-gate #pragma weak res_nisourserver = res_ourserver_p 1607c478bd9Sstevel@tonic-gate #endif /* ORIGINAL_ISC_CODE */ 1617c478bd9Sstevel@tonic-gate 1627c478bd9Sstevel@tonic-gate static const int niflags = NI_NUMERICHOST | NI_NUMERICSERV; 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gate /* Public. */ 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate /* int 1677c478bd9Sstevel@tonic-gate * res_isourserver(ina) 1687c478bd9Sstevel@tonic-gate * looks up "ina" in _res.ns_addr_list[] 1697c478bd9Sstevel@tonic-gate * returns: 1707c478bd9Sstevel@tonic-gate * 0 : not found 1717c478bd9Sstevel@tonic-gate * >0 : found 1727c478bd9Sstevel@tonic-gate * author: 1737c478bd9Sstevel@tonic-gate * paul vixie, 29may94 1747c478bd9Sstevel@tonic-gate */ 1757c478bd9Sstevel@tonic-gate int 1767c478bd9Sstevel@tonic-gate res_ourserver_p(const res_state statp, const struct sockaddr *sa) { 1777c478bd9Sstevel@tonic-gate const struct sockaddr_in *inp, *srv; 1787c478bd9Sstevel@tonic-gate const struct sockaddr_in6 *in6p, *srv6; 1797c478bd9Sstevel@tonic-gate int ns; 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate switch (sa->sa_family) { 1827c478bd9Sstevel@tonic-gate case AF_INET: 1837c478bd9Sstevel@tonic-gate inp = (const struct sockaddr_in *)sa; 1847c478bd9Sstevel@tonic-gate for (ns = 0; ns < statp->nscount; ns++) { 1857c478bd9Sstevel@tonic-gate srv = (struct sockaddr_in *)get_nsaddr(statp, ns); 1867c478bd9Sstevel@tonic-gate if (srv->sin_family == inp->sin_family && 1877c478bd9Sstevel@tonic-gate srv->sin_port == inp->sin_port && 1887c478bd9Sstevel@tonic-gate (srv->sin_addr.s_addr == INADDR_ANY || 1897c478bd9Sstevel@tonic-gate srv->sin_addr.s_addr == inp->sin_addr.s_addr)) 1907c478bd9Sstevel@tonic-gate return (1); 1917c478bd9Sstevel@tonic-gate } 1927c478bd9Sstevel@tonic-gate break; 1937c478bd9Sstevel@tonic-gate case AF_INET6: 1947c478bd9Sstevel@tonic-gate if (EXT(statp).ext == NULL) 1957c478bd9Sstevel@tonic-gate break; 1967c478bd9Sstevel@tonic-gate in6p = (const struct sockaddr_in6 *)sa; 1977c478bd9Sstevel@tonic-gate for (ns = 0; ns < statp->nscount; ns++) { 1987c478bd9Sstevel@tonic-gate srv6 = (struct sockaddr_in6 *)get_nsaddr(statp, ns); 1997c478bd9Sstevel@tonic-gate if (srv6->sin6_family == in6p->sin6_family && 2007c478bd9Sstevel@tonic-gate srv6->sin6_port == in6p->sin6_port && 2017c478bd9Sstevel@tonic-gate #ifdef HAVE_SIN6_SCOPE_ID 2027c478bd9Sstevel@tonic-gate (srv6->sin6_scope_id == 0 || 2037c478bd9Sstevel@tonic-gate srv6->sin6_scope_id == in6p->sin6_scope_id) && 2047c478bd9Sstevel@tonic-gate #endif 2057c478bd9Sstevel@tonic-gate (IN6_IS_ADDR_UNSPECIFIED(&srv6->sin6_addr) || 2067c478bd9Sstevel@tonic-gate IN6_ARE_ADDR_EQUAL(&srv6->sin6_addr, &in6p->sin6_addr))) 2077c478bd9Sstevel@tonic-gate return (1); 2087c478bd9Sstevel@tonic-gate } 2097c478bd9Sstevel@tonic-gate break; 2107c478bd9Sstevel@tonic-gate default: 2117c478bd9Sstevel@tonic-gate break; 2127c478bd9Sstevel@tonic-gate } 2137c478bd9Sstevel@tonic-gate return (0); 2147c478bd9Sstevel@tonic-gate } 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate /* int 2177c478bd9Sstevel@tonic-gate * res_nameinquery(name, type, class, buf, eom) 2187c478bd9Sstevel@tonic-gate * look for (name,type,class) in the query section of packet (buf,eom) 2197c478bd9Sstevel@tonic-gate * requires: 2207c478bd9Sstevel@tonic-gate * buf + HFIXEDSZ <= eom 2217c478bd9Sstevel@tonic-gate * returns: 2227c478bd9Sstevel@tonic-gate * -1 : format error 2237c478bd9Sstevel@tonic-gate * 0 : not found 2247c478bd9Sstevel@tonic-gate * >0 : found 2257c478bd9Sstevel@tonic-gate * author: 2267c478bd9Sstevel@tonic-gate * paul vixie, 29may94 2277c478bd9Sstevel@tonic-gate */ 2287c478bd9Sstevel@tonic-gate int 2297c478bd9Sstevel@tonic-gate res_nameinquery(const char *name, int type, int class, 2307c478bd9Sstevel@tonic-gate const u_char *buf, const u_char *eom) 2317c478bd9Sstevel@tonic-gate { 2327c478bd9Sstevel@tonic-gate const u_char *cp = buf + HFIXEDSZ; 2337c478bd9Sstevel@tonic-gate int qdcount = ntohs(((const HEADER*)buf)->qdcount); 2347c478bd9Sstevel@tonic-gate 2357c478bd9Sstevel@tonic-gate while (qdcount-- > 0) { 2367c478bd9Sstevel@tonic-gate char tname[MAXDNAME+1]; 2377c478bd9Sstevel@tonic-gate int n, ttype, tclass; 2387c478bd9Sstevel@tonic-gate 2397c478bd9Sstevel@tonic-gate n = dn_expand(buf, eom, cp, tname, sizeof tname); 2407c478bd9Sstevel@tonic-gate if (n < 0) 2417c478bd9Sstevel@tonic-gate return (-1); 2427c478bd9Sstevel@tonic-gate cp += n; 2437c478bd9Sstevel@tonic-gate if (cp + 2 * INT16SZ > eom) 2447c478bd9Sstevel@tonic-gate return (-1); 2457c478bd9Sstevel@tonic-gate ttype = ns_get16(cp); cp += INT16SZ; 2467c478bd9Sstevel@tonic-gate tclass = ns_get16(cp); cp += INT16SZ; 2477c478bd9Sstevel@tonic-gate if (ttype == type && tclass == class && 2487c478bd9Sstevel@tonic-gate ns_samename(tname, name) == 1) 2497c478bd9Sstevel@tonic-gate return (1); 2507c478bd9Sstevel@tonic-gate } 2517c478bd9Sstevel@tonic-gate return (0); 2527c478bd9Sstevel@tonic-gate } 2537c478bd9Sstevel@tonic-gate 2547c478bd9Sstevel@tonic-gate /* int 2557c478bd9Sstevel@tonic-gate * res_queriesmatch(buf1, eom1, buf2, eom2) 2567c478bd9Sstevel@tonic-gate * is there a 1:1 mapping of (name,type,class) 2577c478bd9Sstevel@tonic-gate * in (buf1,eom1) and (buf2,eom2)? 2587c478bd9Sstevel@tonic-gate * returns: 2597c478bd9Sstevel@tonic-gate * -1 : format error 2607c478bd9Sstevel@tonic-gate * 0 : not a 1:1 mapping 2617c478bd9Sstevel@tonic-gate * >0 : is a 1:1 mapping 2627c478bd9Sstevel@tonic-gate * author: 2637c478bd9Sstevel@tonic-gate * paul vixie, 29may94 2647c478bd9Sstevel@tonic-gate */ 2657c478bd9Sstevel@tonic-gate int 2667c478bd9Sstevel@tonic-gate res_queriesmatch(const u_char *buf1, const u_char *eom1, 2677c478bd9Sstevel@tonic-gate const u_char *buf2, const u_char *eom2) 2687c478bd9Sstevel@tonic-gate { 2697c478bd9Sstevel@tonic-gate const u_char *cp = buf1 + HFIXEDSZ; 2707c478bd9Sstevel@tonic-gate int qdcount = ntohs(((const HEADER*)buf1)->qdcount); 2717c478bd9Sstevel@tonic-gate 2727c478bd9Sstevel@tonic-gate if (buf1 + HFIXEDSZ > eom1 || buf2 + HFIXEDSZ > eom2) 2737c478bd9Sstevel@tonic-gate return (-1); 2747c478bd9Sstevel@tonic-gate 2757c478bd9Sstevel@tonic-gate /* 2767c478bd9Sstevel@tonic-gate * Only header section present in replies to 2777c478bd9Sstevel@tonic-gate * dynamic update packets. 2787c478bd9Sstevel@tonic-gate */ 2797c478bd9Sstevel@tonic-gate if ((((const HEADER *)buf1)->opcode == ns_o_update) && 2807c478bd9Sstevel@tonic-gate (((const HEADER *)buf2)->opcode == ns_o_update)) 2817c478bd9Sstevel@tonic-gate return (1); 2827c478bd9Sstevel@tonic-gate 2837c478bd9Sstevel@tonic-gate if (qdcount != ntohs(((const HEADER*)buf2)->qdcount)) 2847c478bd9Sstevel@tonic-gate return (0); 2857c478bd9Sstevel@tonic-gate while (qdcount-- > 0) { 2867c478bd9Sstevel@tonic-gate char tname[MAXDNAME+1]; 2877c478bd9Sstevel@tonic-gate int n, ttype, tclass; 2887c478bd9Sstevel@tonic-gate 2897c478bd9Sstevel@tonic-gate n = dn_expand(buf1, eom1, cp, tname, sizeof tname); 2907c478bd9Sstevel@tonic-gate if (n < 0) 2917c478bd9Sstevel@tonic-gate return (-1); 2927c478bd9Sstevel@tonic-gate cp += n; 2937c478bd9Sstevel@tonic-gate if (cp + 2 * INT16SZ > eom1) 2947c478bd9Sstevel@tonic-gate return (-1); 2957c478bd9Sstevel@tonic-gate ttype = ns_get16(cp); cp += INT16SZ; 2967c478bd9Sstevel@tonic-gate tclass = ns_get16(cp); cp += INT16SZ; 2977c478bd9Sstevel@tonic-gate if (!res_nameinquery(tname, ttype, tclass, buf2, eom2)) 2987c478bd9Sstevel@tonic-gate return (0); 2997c478bd9Sstevel@tonic-gate } 3007c478bd9Sstevel@tonic-gate return (1); 3017c478bd9Sstevel@tonic-gate } 3027c478bd9Sstevel@tonic-gate 3037c478bd9Sstevel@tonic-gate int 3047c478bd9Sstevel@tonic-gate res_nsend(res_state statp, 3057c478bd9Sstevel@tonic-gate const u_char *buf, int buflen, u_char *ans, int anssiz) 3067c478bd9Sstevel@tonic-gate { 3077c478bd9Sstevel@tonic-gate int gotsomewhere, terrno, try, v_circuit, resplen, ns, n; 3087c478bd9Sstevel@tonic-gate char abuf[NI_MAXHOST]; 3097c478bd9Sstevel@tonic-gate 3107c478bd9Sstevel@tonic-gate #ifdef SUNW_POLL 3117c478bd9Sstevel@tonic-gate highestFD = sysconf(_SC_OPEN_MAX) - 1; 3127c478bd9Sstevel@tonic-gate #endif 3137c478bd9Sstevel@tonic-gate 3147c478bd9Sstevel@tonic-gate if (statp->nscount == 0) { 3157c478bd9Sstevel@tonic-gate errno = ESRCH; 3167c478bd9Sstevel@tonic-gate return (-1); 3177c478bd9Sstevel@tonic-gate } 3187c478bd9Sstevel@tonic-gate if (anssiz < HFIXEDSZ) { 3197c478bd9Sstevel@tonic-gate errno = EINVAL; 3207c478bd9Sstevel@tonic-gate return (-1); 3217c478bd9Sstevel@tonic-gate } 3227c478bd9Sstevel@tonic-gate DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_QUERY), 3237c478bd9Sstevel@tonic-gate (stdout, ";; res_send()\n"), buf, buflen); 3247c478bd9Sstevel@tonic-gate v_circuit = (statp->options & RES_USEVC) || buflen > PACKETSZ; 3257c478bd9Sstevel@tonic-gate gotsomewhere = 0; 3267c478bd9Sstevel@tonic-gate terrno = ETIMEDOUT; 3277c478bd9Sstevel@tonic-gate 3287c478bd9Sstevel@tonic-gate /* 3297c478bd9Sstevel@tonic-gate * If the ns_addr_list in the resolver context has changed, then 3307c478bd9Sstevel@tonic-gate * invalidate our cached copy and the associated timing data. 3317c478bd9Sstevel@tonic-gate */ 3327c478bd9Sstevel@tonic-gate if (EXT(statp).nscount != 0) { 3337c478bd9Sstevel@tonic-gate int needclose = 0; 3347c478bd9Sstevel@tonic-gate struct sockaddr_storage peer; 3357c478bd9Sstevel@tonic-gate ISC_SOCKLEN_T peerlen; 3367c478bd9Sstevel@tonic-gate 3377c478bd9Sstevel@tonic-gate if (EXT(statp).nscount != statp->nscount) 3387c478bd9Sstevel@tonic-gate needclose++; 3397c478bd9Sstevel@tonic-gate else 3407c478bd9Sstevel@tonic-gate for (ns = 0; ns < statp->nscount; ns++) { 3417c478bd9Sstevel@tonic-gate if (statp->nsaddr_list[ns].sin_family && 3427c478bd9Sstevel@tonic-gate !sock_eq((struct sockaddr *)&statp->nsaddr_list[ns], 3437c478bd9Sstevel@tonic-gate (struct sockaddr *)&EXT(statp).ext->nsaddrs[ns])) { 3447c478bd9Sstevel@tonic-gate needclose++; 3457c478bd9Sstevel@tonic-gate break; 3467c478bd9Sstevel@tonic-gate } 3477c478bd9Sstevel@tonic-gate 3487c478bd9Sstevel@tonic-gate if (EXT(statp).nssocks[ns] == -1) 3497c478bd9Sstevel@tonic-gate continue; 3507c478bd9Sstevel@tonic-gate peerlen = sizeof(peer); 3517c478bd9Sstevel@tonic-gate if (getsockname(EXT(statp).nssocks[ns], 3527c478bd9Sstevel@tonic-gate (struct sockaddr *)&peer, &peerlen) < 0) { 3537c478bd9Sstevel@tonic-gate needclose++; 3547c478bd9Sstevel@tonic-gate break; 3557c478bd9Sstevel@tonic-gate } 3567c478bd9Sstevel@tonic-gate if (!sock_eq((struct sockaddr *)&peer, 3577c478bd9Sstevel@tonic-gate get_nsaddr(statp, ns))) { 3587c478bd9Sstevel@tonic-gate needclose++; 3597c478bd9Sstevel@tonic-gate break; 3607c478bd9Sstevel@tonic-gate } 3617c478bd9Sstevel@tonic-gate } 3627c478bd9Sstevel@tonic-gate if (needclose) { 3637c478bd9Sstevel@tonic-gate res_nclose(statp); 3647c478bd9Sstevel@tonic-gate EXT(statp).nscount = 0; 3657c478bd9Sstevel@tonic-gate } 3667c478bd9Sstevel@tonic-gate } 3677c478bd9Sstevel@tonic-gate 3687c478bd9Sstevel@tonic-gate /* 3697c478bd9Sstevel@tonic-gate * Maybe initialize our private copy of the ns_addr_list. 3707c478bd9Sstevel@tonic-gate */ 3717c478bd9Sstevel@tonic-gate if (EXT(statp).nscount == 0) { 3727c478bd9Sstevel@tonic-gate for (ns = 0; ns < statp->nscount; ns++) { 3737c478bd9Sstevel@tonic-gate EXT(statp).nstimes[ns] = RES_MAXTIME; 3747c478bd9Sstevel@tonic-gate EXT(statp).nssocks[ns] = -1; 3757c478bd9Sstevel@tonic-gate if (!statp->nsaddr_list[ns].sin_family) 3767c478bd9Sstevel@tonic-gate continue; 3777c478bd9Sstevel@tonic-gate EXT(statp).ext->nsaddrs[ns].sin = 3787c478bd9Sstevel@tonic-gate statp->nsaddr_list[ns]; 3797c478bd9Sstevel@tonic-gate } 3807c478bd9Sstevel@tonic-gate EXT(statp).nscount = statp->nscount; 3817c478bd9Sstevel@tonic-gate } 3827c478bd9Sstevel@tonic-gate 3837c478bd9Sstevel@tonic-gate /* 3847c478bd9Sstevel@tonic-gate * Some resolvers want to even out the load on their nameservers. 3857c478bd9Sstevel@tonic-gate * Note that RES_BLAST overrides RES_ROTATE. 3867c478bd9Sstevel@tonic-gate */ 3877c478bd9Sstevel@tonic-gate if ((statp->options & RES_ROTATE) != 0 && 3887c478bd9Sstevel@tonic-gate (statp->options & RES_BLAST) == 0) { 3897c478bd9Sstevel@tonic-gate union res_sockaddr_union inu; 3907c478bd9Sstevel@tonic-gate struct sockaddr_in ina; 3917c478bd9Sstevel@tonic-gate int lastns = statp->nscount - 1; 3927c478bd9Sstevel@tonic-gate int fd; 3937c478bd9Sstevel@tonic-gate u_int16_t nstime; 3947c478bd9Sstevel@tonic-gate 3957c478bd9Sstevel@tonic-gate if (EXT(statp).ext != NULL) 3967c478bd9Sstevel@tonic-gate inu = EXT(statp).ext->nsaddrs[0]; 3977c478bd9Sstevel@tonic-gate ina = statp->nsaddr_list[0]; 3987c478bd9Sstevel@tonic-gate fd = EXT(statp).nssocks[0]; 3997c478bd9Sstevel@tonic-gate nstime = EXT(statp).nstimes[0]; 4007c478bd9Sstevel@tonic-gate for (ns = 0; ns < lastns; ns++) { 4017c478bd9Sstevel@tonic-gate if (EXT(statp).ext != NULL) 4027c478bd9Sstevel@tonic-gate EXT(statp).ext->nsaddrs[ns] = 4037c478bd9Sstevel@tonic-gate EXT(statp).ext->nsaddrs[ns + 1]; 4047c478bd9Sstevel@tonic-gate statp->nsaddr_list[ns] = statp->nsaddr_list[ns + 1]; 4057c478bd9Sstevel@tonic-gate EXT(statp).nssocks[ns] = EXT(statp).nssocks[ns + 1]; 4067c478bd9Sstevel@tonic-gate EXT(statp).nstimes[ns] = EXT(statp).nstimes[ns + 1]; 4077c478bd9Sstevel@tonic-gate } 4087c478bd9Sstevel@tonic-gate if (EXT(statp).ext != NULL) 4097c478bd9Sstevel@tonic-gate EXT(statp).ext->nsaddrs[lastns] = inu; 4107c478bd9Sstevel@tonic-gate statp->nsaddr_list[lastns] = ina; 4117c478bd9Sstevel@tonic-gate EXT(statp).nssocks[lastns] = fd; 4127c478bd9Sstevel@tonic-gate EXT(statp).nstimes[lastns] = nstime; 4137c478bd9Sstevel@tonic-gate } 4147c478bd9Sstevel@tonic-gate 4157c478bd9Sstevel@tonic-gate /* 4167c478bd9Sstevel@tonic-gate * Send request, RETRY times, or until successful. 4177c478bd9Sstevel@tonic-gate */ 4187c478bd9Sstevel@tonic-gate for (try = 0; try < statp->retry; try++) { 4197c478bd9Sstevel@tonic-gate for (ns = 0; ns < statp->nscount; ns++) { 4207c478bd9Sstevel@tonic-gate struct sockaddr *nsap; 4217c478bd9Sstevel@tonic-gate int nsaplen; 4227c478bd9Sstevel@tonic-gate nsap = get_nsaddr(statp, ns); 4237c478bd9Sstevel@tonic-gate nsaplen = get_salen(nsap); 4247c478bd9Sstevel@tonic-gate statp->_flags &= ~RES_F_LASTMASK; 4257c478bd9Sstevel@tonic-gate statp->_flags |= (ns << RES_F_LASTSHIFT); 4267c478bd9Sstevel@tonic-gate same_ns: 4277c478bd9Sstevel@tonic-gate if (statp->qhook) { 4287c478bd9Sstevel@tonic-gate int done = 0, loops = 0; 4297c478bd9Sstevel@tonic-gate 4307c478bd9Sstevel@tonic-gate do { 4317c478bd9Sstevel@tonic-gate res_sendhookact act; 4327c478bd9Sstevel@tonic-gate 4337c478bd9Sstevel@tonic-gate act = (*statp->qhook)(&nsap, &buf, &buflen, 4347c478bd9Sstevel@tonic-gate ans, anssiz, &resplen); 4357c478bd9Sstevel@tonic-gate switch (act) { 4367c478bd9Sstevel@tonic-gate case res_goahead: 4377c478bd9Sstevel@tonic-gate done = 1; 4387c478bd9Sstevel@tonic-gate break; 4397c478bd9Sstevel@tonic-gate case res_nextns: 4407c478bd9Sstevel@tonic-gate res_nclose(statp); 4417c478bd9Sstevel@tonic-gate goto next_ns; 4427c478bd9Sstevel@tonic-gate case res_done: 4437c478bd9Sstevel@tonic-gate return (resplen); 4447c478bd9Sstevel@tonic-gate case res_modified: 4457c478bd9Sstevel@tonic-gate /* give the hook another try */ 4467c478bd9Sstevel@tonic-gate if (++loops < 42) /*doug adams*/ 4477c478bd9Sstevel@tonic-gate break; 4487c478bd9Sstevel@tonic-gate /*FALLTHROUGH*/ 4497c478bd9Sstevel@tonic-gate case res_error: 4507c478bd9Sstevel@tonic-gate /*FALLTHROUGH*/ 4517c478bd9Sstevel@tonic-gate default: 4527c478bd9Sstevel@tonic-gate goto fail; 4537c478bd9Sstevel@tonic-gate } 4547c478bd9Sstevel@tonic-gate } while (!done); 4557c478bd9Sstevel@tonic-gate } 4567c478bd9Sstevel@tonic-gate 4577c478bd9Sstevel@tonic-gate Dprint(((statp->options & RES_DEBUG) && 4587c478bd9Sstevel@tonic-gate getnameinfo(nsap, nsaplen, abuf, sizeof(abuf), 4597c478bd9Sstevel@tonic-gate NULL, 0, niflags) == 0), 4607c478bd9Sstevel@tonic-gate (stdout, ";; Querying server (# %d) address = %s\n", 4617c478bd9Sstevel@tonic-gate ns + 1, abuf)); 4627c478bd9Sstevel@tonic-gate 4637c478bd9Sstevel@tonic-gate 4647c478bd9Sstevel@tonic-gate if (v_circuit) { 4657c478bd9Sstevel@tonic-gate /* Use VC; at most one attempt per server. */ 4667c478bd9Sstevel@tonic-gate try = statp->retry; 4677c478bd9Sstevel@tonic-gate n = send_vc(statp, buf, buflen, ans, anssiz, &terrno, 4687c478bd9Sstevel@tonic-gate ns); 4697c478bd9Sstevel@tonic-gate if (n < 0) 4707c478bd9Sstevel@tonic-gate goto fail; 4717c478bd9Sstevel@tonic-gate if (n == 0) 4727c478bd9Sstevel@tonic-gate goto next_ns; 4737c478bd9Sstevel@tonic-gate resplen = n; 4747c478bd9Sstevel@tonic-gate } else { 4757c478bd9Sstevel@tonic-gate /* Use datagrams. */ 4767c478bd9Sstevel@tonic-gate n = send_dg(statp, buf, buflen, ans, anssiz, &terrno, 4777c478bd9Sstevel@tonic-gate ns, &v_circuit, &gotsomewhere); 4787c478bd9Sstevel@tonic-gate if (n < 0) 4797c478bd9Sstevel@tonic-gate goto fail; 4807c478bd9Sstevel@tonic-gate if (n == 0) 4817c478bd9Sstevel@tonic-gate goto next_ns; 4827c478bd9Sstevel@tonic-gate if (v_circuit) 4837c478bd9Sstevel@tonic-gate goto same_ns; 4847c478bd9Sstevel@tonic-gate resplen = n; 4857c478bd9Sstevel@tonic-gate } 4867c478bd9Sstevel@tonic-gate 4877c478bd9Sstevel@tonic-gate Dprint((statp->options & RES_DEBUG) || 4887c478bd9Sstevel@tonic-gate ((statp->pfcode & RES_PRF_REPLY) && 4897c478bd9Sstevel@tonic-gate (statp->pfcode & RES_PRF_HEAD1)), 4907c478bd9Sstevel@tonic-gate (stdout, ";; got answer:\n")); 4917c478bd9Sstevel@tonic-gate 4927c478bd9Sstevel@tonic-gate DprintQ((statp->options & RES_DEBUG) || 4937c478bd9Sstevel@tonic-gate (statp->pfcode & RES_PRF_REPLY), 4947c478bd9Sstevel@tonic-gate (stdout, "%s", ""), 4957c478bd9Sstevel@tonic-gate ans, (resplen > anssiz) ? anssiz : resplen); 4967c478bd9Sstevel@tonic-gate 4977c478bd9Sstevel@tonic-gate /* 4987c478bd9Sstevel@tonic-gate * If we have temporarily opened a virtual circuit, 4997c478bd9Sstevel@tonic-gate * or if we haven't been asked to keep a socket open, 5007c478bd9Sstevel@tonic-gate * close the socket. 5017c478bd9Sstevel@tonic-gate */ 5027c478bd9Sstevel@tonic-gate if ((v_circuit && (statp->options & RES_USEVC) == 0) || 5037c478bd9Sstevel@tonic-gate (statp->options & RES_STAYOPEN) == 0) { 5047c478bd9Sstevel@tonic-gate res_nclose(statp); 5057c478bd9Sstevel@tonic-gate } 5067c478bd9Sstevel@tonic-gate if (statp->rhook) { 5077c478bd9Sstevel@tonic-gate int done = 0, loops = 0; 5087c478bd9Sstevel@tonic-gate 5097c478bd9Sstevel@tonic-gate do { 5107c478bd9Sstevel@tonic-gate res_sendhookact act; 5117c478bd9Sstevel@tonic-gate 5127c478bd9Sstevel@tonic-gate act = (*statp->rhook)(nsap, buf, buflen, 5137c478bd9Sstevel@tonic-gate ans, anssiz, &resplen); 5147c478bd9Sstevel@tonic-gate switch (act) { 5157c478bd9Sstevel@tonic-gate case res_goahead: 5167c478bd9Sstevel@tonic-gate case res_done: 5177c478bd9Sstevel@tonic-gate done = 1; 5187c478bd9Sstevel@tonic-gate break; 5197c478bd9Sstevel@tonic-gate case res_nextns: 5207c478bd9Sstevel@tonic-gate res_nclose(statp); 5217c478bd9Sstevel@tonic-gate goto next_ns; 5227c478bd9Sstevel@tonic-gate case res_modified: 5237c478bd9Sstevel@tonic-gate /* give the hook another try */ 5247c478bd9Sstevel@tonic-gate if (++loops < 42) /*doug adams*/ 5257c478bd9Sstevel@tonic-gate break; 5267c478bd9Sstevel@tonic-gate /*FALLTHROUGH*/ 5277c478bd9Sstevel@tonic-gate case res_error: 5287c478bd9Sstevel@tonic-gate /*FALLTHROUGH*/ 5297c478bd9Sstevel@tonic-gate default: 5307c478bd9Sstevel@tonic-gate goto fail; 5317c478bd9Sstevel@tonic-gate } 5327c478bd9Sstevel@tonic-gate } while (!done); 5337c478bd9Sstevel@tonic-gate 5347c478bd9Sstevel@tonic-gate } 5357c478bd9Sstevel@tonic-gate return (resplen); 5367c478bd9Sstevel@tonic-gate next_ns: ; 5377c478bd9Sstevel@tonic-gate } /*foreach ns*/ 5387c478bd9Sstevel@tonic-gate } /*foreach retry*/ 5397c478bd9Sstevel@tonic-gate res_nclose(statp); 5407c478bd9Sstevel@tonic-gate if (!v_circuit) { 5417c478bd9Sstevel@tonic-gate if (!gotsomewhere) 5427c478bd9Sstevel@tonic-gate errno = ECONNREFUSED; /* no nameservers found */ 5437c478bd9Sstevel@tonic-gate else 5447c478bd9Sstevel@tonic-gate errno = ETIMEDOUT; /* no answer obtained */ 5457c478bd9Sstevel@tonic-gate } else 5467c478bd9Sstevel@tonic-gate errno = terrno; 5477c478bd9Sstevel@tonic-gate return (-1); 5487c478bd9Sstevel@tonic-gate fail: 5497c478bd9Sstevel@tonic-gate res_nclose(statp); 5507c478bd9Sstevel@tonic-gate return (-1); 5517c478bd9Sstevel@tonic-gate } 5527c478bd9Sstevel@tonic-gate 5537c478bd9Sstevel@tonic-gate /* Private */ 5547c478bd9Sstevel@tonic-gate 5557c478bd9Sstevel@tonic-gate static int 5567c478bd9Sstevel@tonic-gate get_salen(sa) 5577c478bd9Sstevel@tonic-gate const struct sockaddr *sa; 5587c478bd9Sstevel@tonic-gate { 5597c478bd9Sstevel@tonic-gate 5607c478bd9Sstevel@tonic-gate #ifdef HAVE_SA_LEN 5617c478bd9Sstevel@tonic-gate /* There are people do not set sa_len. Be forgiving to them. */ 5627c478bd9Sstevel@tonic-gate if (sa->sa_len) 5637c478bd9Sstevel@tonic-gate return (sa->sa_len); 5647c478bd9Sstevel@tonic-gate #endif 5657c478bd9Sstevel@tonic-gate 5667c478bd9Sstevel@tonic-gate if (sa->sa_family == AF_INET) 5677c478bd9Sstevel@tonic-gate return (sizeof(struct sockaddr_in)); 5687c478bd9Sstevel@tonic-gate else if (sa->sa_family == AF_INET6) 5697c478bd9Sstevel@tonic-gate return (sizeof(struct sockaddr_in6)); 5707c478bd9Sstevel@tonic-gate else 5717c478bd9Sstevel@tonic-gate return (0); /* unknown, die on connect */ 5727c478bd9Sstevel@tonic-gate } 5737c478bd9Sstevel@tonic-gate 5747c478bd9Sstevel@tonic-gate /* 5757c478bd9Sstevel@tonic-gate * pick appropriate nsaddr_list for use. see res_init() for initialization. 5767c478bd9Sstevel@tonic-gate */ 5777c478bd9Sstevel@tonic-gate static struct sockaddr * 5787c478bd9Sstevel@tonic-gate get_nsaddr(statp, n) 5797c478bd9Sstevel@tonic-gate res_state statp; 5807c478bd9Sstevel@tonic-gate size_t n; 5817c478bd9Sstevel@tonic-gate { 5827c478bd9Sstevel@tonic-gate 5837c478bd9Sstevel@tonic-gate if (!statp->nsaddr_list[n].sin_family && EXT(statp).ext) { 5847c478bd9Sstevel@tonic-gate /* 5857c478bd9Sstevel@tonic-gate * - EXT(statp).ext->nsaddrs[n] holds an address that is larger 5867c478bd9Sstevel@tonic-gate * than struct sockaddr, and 5877c478bd9Sstevel@tonic-gate * - user code did not update statp->nsaddr_list[n]. 5887c478bd9Sstevel@tonic-gate */ 5897c478bd9Sstevel@tonic-gate return (struct sockaddr *)(void *)&EXT(statp).ext->nsaddrs[n]; 5907c478bd9Sstevel@tonic-gate } else { 5917c478bd9Sstevel@tonic-gate /* 5927c478bd9Sstevel@tonic-gate * - user code updated statp->nsaddr_list[n], or 5937c478bd9Sstevel@tonic-gate * - statp->nsaddr_list[n] has the same content as 5947c478bd9Sstevel@tonic-gate * EXT(statp).ext->nsaddrs[n]. 5957c478bd9Sstevel@tonic-gate */ 5967c478bd9Sstevel@tonic-gate return (struct sockaddr *)(void *)&statp->nsaddr_list[n]; 5977c478bd9Sstevel@tonic-gate } 5987c478bd9Sstevel@tonic-gate } 5997c478bd9Sstevel@tonic-gate 6007c478bd9Sstevel@tonic-gate static int 6017c478bd9Sstevel@tonic-gate send_vc(res_state statp, 6027c478bd9Sstevel@tonic-gate const u_char *buf, int buflen, u_char *ans, int anssiz, 6037c478bd9Sstevel@tonic-gate int *terrno, int ns) 6047c478bd9Sstevel@tonic-gate { 6057c478bd9Sstevel@tonic-gate const HEADER *hp = (const HEADER *) buf; 6067c478bd9Sstevel@tonic-gate HEADER *anhp = (HEADER *) ans; 6077c478bd9Sstevel@tonic-gate struct sockaddr *nsap; 6087c478bd9Sstevel@tonic-gate int nsaplen; 6097c478bd9Sstevel@tonic-gate int truncating, connreset, resplen, n; 6107c478bd9Sstevel@tonic-gate struct iovec iov[2]; 6117c478bd9Sstevel@tonic-gate u_short len; 6127c478bd9Sstevel@tonic-gate u_char *cp; 6137c478bd9Sstevel@tonic-gate void *tmp; 6147c478bd9Sstevel@tonic-gate 6157c478bd9Sstevel@tonic-gate nsap = get_nsaddr(statp, ns); 6167c478bd9Sstevel@tonic-gate nsaplen = get_salen(nsap); 6177c478bd9Sstevel@tonic-gate 6187c478bd9Sstevel@tonic-gate connreset = 0; 6197c478bd9Sstevel@tonic-gate same_ns: 6207c478bd9Sstevel@tonic-gate truncating = 0; 6217c478bd9Sstevel@tonic-gate 6227c478bd9Sstevel@tonic-gate /* Are we still talking to whom we want to talk to? */ 6237c478bd9Sstevel@tonic-gate if (statp->_vcsock >= 0 && (statp->_flags & RES_F_VC) != 0) { 6247c478bd9Sstevel@tonic-gate struct sockaddr_storage peer; 6257c478bd9Sstevel@tonic-gate ISC_SOCKLEN_T size = sizeof peer; 6267c478bd9Sstevel@tonic-gate 6277c478bd9Sstevel@tonic-gate if (getpeername(statp->_vcsock, 6287c478bd9Sstevel@tonic-gate (struct sockaddr *)&peer, &size) < 0 || 6297c478bd9Sstevel@tonic-gate !sock_eq((struct sockaddr *)&peer, nsap)) { 6307c478bd9Sstevel@tonic-gate res_nclose(statp); 6317c478bd9Sstevel@tonic-gate statp->_flags &= ~RES_F_VC; 6327c478bd9Sstevel@tonic-gate } 6337c478bd9Sstevel@tonic-gate } 6347c478bd9Sstevel@tonic-gate 6357c478bd9Sstevel@tonic-gate if (statp->_vcsock < 0 || (statp->_flags & RES_F_VC) == 0) { 6367c478bd9Sstevel@tonic-gate if (statp->_vcsock >= 0) 6377c478bd9Sstevel@tonic-gate res_nclose(statp); 6387c478bd9Sstevel@tonic-gate 6397c478bd9Sstevel@tonic-gate statp->_vcsock = socket(nsap->sa_family, SOCK_STREAM, 0); 6407c478bd9Sstevel@tonic-gate if (statp->_vcsock > highestFD) { 6417c478bd9Sstevel@tonic-gate res_nclose(statp); 6427c478bd9Sstevel@tonic-gate errno = ENOTSOCK; 6437c478bd9Sstevel@tonic-gate } 6447c478bd9Sstevel@tonic-gate if (statp->_vcsock < 0) { 6457c478bd9Sstevel@tonic-gate *terrno = errno; 6467c478bd9Sstevel@tonic-gate Perror(statp, stderr, "socket(vc)", errno); 6477c478bd9Sstevel@tonic-gate return (-1); 6487c478bd9Sstevel@tonic-gate } 6497c478bd9Sstevel@tonic-gate errno = 0; 6507c478bd9Sstevel@tonic-gate if (connect(statp->_vcsock, nsap, nsaplen) < 0) { 6517c478bd9Sstevel@tonic-gate *terrno = errno; 6527c478bd9Sstevel@tonic-gate Aerror(statp, stderr, "connect/vc", errno, nsap, 6537c478bd9Sstevel@tonic-gate nsaplen); 6547c478bd9Sstevel@tonic-gate res_nclose(statp); 6557c478bd9Sstevel@tonic-gate return (0); 6567c478bd9Sstevel@tonic-gate } 6577c478bd9Sstevel@tonic-gate statp->_flags |= RES_F_VC; 6587c478bd9Sstevel@tonic-gate } 6597c478bd9Sstevel@tonic-gate 6607c478bd9Sstevel@tonic-gate /* 6617c478bd9Sstevel@tonic-gate * Send length & message 6627c478bd9Sstevel@tonic-gate */ 6637c478bd9Sstevel@tonic-gate ns_put16((u_short)buflen, (u_char*)&len); 6647c478bd9Sstevel@tonic-gate iov[0] = evConsIovec(&len, INT16SZ); 6657c478bd9Sstevel@tonic-gate DE_CONST(buf, tmp); 6667c478bd9Sstevel@tonic-gate iov[1] = evConsIovec(tmp, buflen); 6677c478bd9Sstevel@tonic-gate if (writev(statp->_vcsock, iov, 2) != (INT16SZ + buflen)) { 6687c478bd9Sstevel@tonic-gate *terrno = errno; 6697c478bd9Sstevel@tonic-gate Perror(statp, stderr, "write failed", errno); 6707c478bd9Sstevel@tonic-gate res_nclose(statp); 6717c478bd9Sstevel@tonic-gate return (0); 6727c478bd9Sstevel@tonic-gate } 6737c478bd9Sstevel@tonic-gate /* 6747c478bd9Sstevel@tonic-gate * Receive length & response 6757c478bd9Sstevel@tonic-gate */ 6767c478bd9Sstevel@tonic-gate read_len: 6777c478bd9Sstevel@tonic-gate cp = ans; 6787c478bd9Sstevel@tonic-gate len = INT16SZ; 6797c478bd9Sstevel@tonic-gate while ((n = read(statp->_vcsock, (char *)cp, (int)len)) > 0) { 6807c478bd9Sstevel@tonic-gate cp += n; 6817c478bd9Sstevel@tonic-gate if ((len -= n) <= 0) 6827c478bd9Sstevel@tonic-gate break; 6837c478bd9Sstevel@tonic-gate } 6847c478bd9Sstevel@tonic-gate if (n <= 0) { 6857c478bd9Sstevel@tonic-gate *terrno = errno; 6867c478bd9Sstevel@tonic-gate Perror(statp, stderr, "read failed", errno); 6877c478bd9Sstevel@tonic-gate res_nclose(statp); 6887c478bd9Sstevel@tonic-gate /* 6897c478bd9Sstevel@tonic-gate * A long running process might get its TCP 6907c478bd9Sstevel@tonic-gate * connection reset if the remote server was 6917c478bd9Sstevel@tonic-gate * restarted. Requery the server instead of 6927c478bd9Sstevel@tonic-gate * trying a new one. When there is only one 6937c478bd9Sstevel@tonic-gate * server, this means that a query might work 6947c478bd9Sstevel@tonic-gate * instead of failing. We only allow one reset 6957c478bd9Sstevel@tonic-gate * per query to prevent looping. 6967c478bd9Sstevel@tonic-gate */ 6977c478bd9Sstevel@tonic-gate if (*terrno == ECONNRESET && !connreset) { 6987c478bd9Sstevel@tonic-gate connreset = 1; 6997c478bd9Sstevel@tonic-gate res_nclose(statp); 7007c478bd9Sstevel@tonic-gate goto same_ns; 7017c478bd9Sstevel@tonic-gate } 7027c478bd9Sstevel@tonic-gate res_nclose(statp); 7037c478bd9Sstevel@tonic-gate return (0); 7047c478bd9Sstevel@tonic-gate } 7057c478bd9Sstevel@tonic-gate resplen = ns_get16(ans); 7067c478bd9Sstevel@tonic-gate if (resplen > anssiz) { 7077c478bd9Sstevel@tonic-gate Dprint(statp->options & RES_DEBUG, 7087c478bd9Sstevel@tonic-gate (stdout, ";; response truncated\n") 7097c478bd9Sstevel@tonic-gate ); 7107c478bd9Sstevel@tonic-gate truncating = 1; 7117c478bd9Sstevel@tonic-gate len = anssiz; 7127c478bd9Sstevel@tonic-gate } else 7137c478bd9Sstevel@tonic-gate len = resplen; 7147c478bd9Sstevel@tonic-gate if (len < HFIXEDSZ) { 7157c478bd9Sstevel@tonic-gate /* 7167c478bd9Sstevel@tonic-gate * Undersized message. 7177c478bd9Sstevel@tonic-gate */ 7187c478bd9Sstevel@tonic-gate Dprint(statp->options & RES_DEBUG, 7197c478bd9Sstevel@tonic-gate (stdout, ";; undersized: %d\n", len)); 7207c478bd9Sstevel@tonic-gate *terrno = EMSGSIZE; 7217c478bd9Sstevel@tonic-gate res_nclose(statp); 7227c478bd9Sstevel@tonic-gate return (0); 7237c478bd9Sstevel@tonic-gate } 7247c478bd9Sstevel@tonic-gate cp = ans; 7257c478bd9Sstevel@tonic-gate while (len != 0 && (n = read(statp->_vcsock, (char *)cp, (int)len)) > 0){ 7267c478bd9Sstevel@tonic-gate cp += n; 7277c478bd9Sstevel@tonic-gate len -= n; 7287c478bd9Sstevel@tonic-gate } 7297c478bd9Sstevel@tonic-gate if (n <= 0) { 7307c478bd9Sstevel@tonic-gate *terrno = errno; 7317c478bd9Sstevel@tonic-gate Perror(statp, stderr, "read(vc)", errno); 7327c478bd9Sstevel@tonic-gate res_nclose(statp); 7337c478bd9Sstevel@tonic-gate return (0); 7347c478bd9Sstevel@tonic-gate } 7357c478bd9Sstevel@tonic-gate if (truncating) { 7367c478bd9Sstevel@tonic-gate /* 7377c478bd9Sstevel@tonic-gate * Flush rest of answer so connection stays in synch. 7387c478bd9Sstevel@tonic-gate */ 7397c478bd9Sstevel@tonic-gate anhp->tc = 1; 7407c478bd9Sstevel@tonic-gate len = resplen - anssiz; 7417c478bd9Sstevel@tonic-gate /* 7427c478bd9Sstevel@tonic-gate * Reset the value of resplen to anssiz, 7437c478bd9Sstevel@tonic-gate * this is done because the caller assumes 7447c478bd9Sstevel@tonic-gate * resplen contains the size of message read 7457c478bd9Sstevel@tonic-gate * into the "ans" buffer passed in. 7467c478bd9Sstevel@tonic-gate */ 7477c478bd9Sstevel@tonic-gate resplen = anssiz; 7487c478bd9Sstevel@tonic-gate 7497c478bd9Sstevel@tonic-gate while (len != 0) { 7507c478bd9Sstevel@tonic-gate char junk[PACKETSZ]; 7517c478bd9Sstevel@tonic-gate 7527c478bd9Sstevel@tonic-gate n = read(statp->_vcsock, junk, 7537c478bd9Sstevel@tonic-gate (len > sizeof junk) ? sizeof junk : len); 7547c478bd9Sstevel@tonic-gate if (n > 0) 7557c478bd9Sstevel@tonic-gate len -= n; 7567c478bd9Sstevel@tonic-gate else 7577c478bd9Sstevel@tonic-gate break; 7587c478bd9Sstevel@tonic-gate } 7597c478bd9Sstevel@tonic-gate } 7607c478bd9Sstevel@tonic-gate /* 7617c478bd9Sstevel@tonic-gate * If the calling applicating has bailed out of 7627c478bd9Sstevel@tonic-gate * a previous call and failed to arrange to have 7637c478bd9Sstevel@tonic-gate * the circuit closed or the server has got 7647c478bd9Sstevel@tonic-gate * itself confused, then drop the packet and 7657c478bd9Sstevel@tonic-gate * wait for the correct one. 7667c478bd9Sstevel@tonic-gate */ 7677c478bd9Sstevel@tonic-gate if (hp->id != anhp->id) { 7687c478bd9Sstevel@tonic-gate DprintQ((statp->options & RES_DEBUG) || 7697c478bd9Sstevel@tonic-gate (statp->pfcode & RES_PRF_REPLY), 7707c478bd9Sstevel@tonic-gate (stdout, ";; old answer (unexpected):\n"), 7717c478bd9Sstevel@tonic-gate ans, (resplen > anssiz) ? anssiz: resplen); 7727c478bd9Sstevel@tonic-gate goto read_len; 7737c478bd9Sstevel@tonic-gate } 7747c478bd9Sstevel@tonic-gate 7757c478bd9Sstevel@tonic-gate /* 7767c478bd9Sstevel@tonic-gate * All is well, or the error is fatal. Signal that the 7777c478bd9Sstevel@tonic-gate * next nameserver ought not be tried. 7787c478bd9Sstevel@tonic-gate */ 7797c478bd9Sstevel@tonic-gate return (resplen); 7807c478bd9Sstevel@tonic-gate } 7817c478bd9Sstevel@tonic-gate 7827c478bd9Sstevel@tonic-gate static int 7837c478bd9Sstevel@tonic-gate send_dg(res_state statp, 7847c478bd9Sstevel@tonic-gate const u_char *buf, int buflen, u_char *ans, int anssiz, 7857c478bd9Sstevel@tonic-gate int *terrno, int ns, int *v_circuit, int *gotsomewhere) 7867c478bd9Sstevel@tonic-gate { 7877c478bd9Sstevel@tonic-gate const HEADER *hp = (const HEADER *) buf; 7887c478bd9Sstevel@tonic-gate HEADER *anhp = (HEADER *) ans; 7897c478bd9Sstevel@tonic-gate const struct sockaddr *nsap; 7907c478bd9Sstevel@tonic-gate int nsaplen; 7917c478bd9Sstevel@tonic-gate struct timespec now, timeout, finish; 7927c478bd9Sstevel@tonic-gate fd_set dsmask; 7937c478bd9Sstevel@tonic-gate struct sockaddr_storage from; 7947c478bd9Sstevel@tonic-gate ISC_SOCKLEN_T fromlen; 7957c478bd9Sstevel@tonic-gate int resplen, seconds, n, s; 7967c478bd9Sstevel@tonic-gate #ifdef SUNW_POLL 7977c478bd9Sstevel@tonic-gate int polltimeout; 7987c478bd9Sstevel@tonic-gate struct pollfd pollfd; 7997c478bd9Sstevel@tonic-gate #endif 8007c478bd9Sstevel@tonic-gate 8017c478bd9Sstevel@tonic-gate nsap = get_nsaddr(statp, ns); 8027c478bd9Sstevel@tonic-gate nsaplen = get_salen(nsap); 8037c478bd9Sstevel@tonic-gate if (EXT(statp).nssocks[ns] == -1) { 8047c478bd9Sstevel@tonic-gate EXT(statp).nssocks[ns] = socket(nsap->sa_family, SOCK_DGRAM, 0); 8057c478bd9Sstevel@tonic-gate if (EXT(statp).nssocks[ns] > highestFD) { 8067c478bd9Sstevel@tonic-gate res_nclose(statp); 8077c478bd9Sstevel@tonic-gate errno = ENOTSOCK; 8087c478bd9Sstevel@tonic-gate } 8097c478bd9Sstevel@tonic-gate if (EXT(statp).nssocks[ns] < 0) { 8107c478bd9Sstevel@tonic-gate *terrno = errno; 8117c478bd9Sstevel@tonic-gate Perror(statp, stderr, "socket(dg)", errno); 8127c478bd9Sstevel@tonic-gate return (-1); 8137c478bd9Sstevel@tonic-gate } 8147c478bd9Sstevel@tonic-gate #ifndef CANNOT_CONNECT_DGRAM 8157c478bd9Sstevel@tonic-gate /* 8167c478bd9Sstevel@tonic-gate * On a 4.3BSD+ machine (client and server, 8177c478bd9Sstevel@tonic-gate * actually), sending to a nameserver datagram 8187c478bd9Sstevel@tonic-gate * port with no nameserver will cause an 8197c478bd9Sstevel@tonic-gate * ICMP port unreachable message to be returned. 8207c478bd9Sstevel@tonic-gate * If our datagram socket is "connected" to the 8217c478bd9Sstevel@tonic-gate * server, we get an ECONNREFUSED error on the next 8227c478bd9Sstevel@tonic-gate * socket operation, and select returns if the 8237c478bd9Sstevel@tonic-gate * error message is received. We can thus detect 8247c478bd9Sstevel@tonic-gate * the absence of a nameserver without timing out. 8257c478bd9Sstevel@tonic-gate */ 8267c478bd9Sstevel@tonic-gate if (connect(EXT(statp).nssocks[ns], nsap, nsaplen) < 0) { 8277c478bd9Sstevel@tonic-gate Aerror(statp, stderr, "connect(dg)", errno, nsap, 8287c478bd9Sstevel@tonic-gate nsaplen); 8297c478bd9Sstevel@tonic-gate res_nclose(statp); 8307c478bd9Sstevel@tonic-gate return (0); 8317c478bd9Sstevel@tonic-gate } 8327c478bd9Sstevel@tonic-gate #endif /* !CANNOT_CONNECT_DGRAM */ 8337c478bd9Sstevel@tonic-gate Dprint(statp->options & RES_DEBUG, 8347c478bd9Sstevel@tonic-gate (stdout, ";; new DG socket\n")) 8357c478bd9Sstevel@tonic-gate } 8367c478bd9Sstevel@tonic-gate s = EXT(statp).nssocks[ns]; 8377c478bd9Sstevel@tonic-gate #ifndef CANNOT_CONNECT_DGRAM 8387c478bd9Sstevel@tonic-gate if (send(s, (const char*)buf, buflen, 0) != buflen) { 8397c478bd9Sstevel@tonic-gate Perror(statp, stderr, "send", errno); 8407c478bd9Sstevel@tonic-gate res_nclose(statp); 8417c478bd9Sstevel@tonic-gate return (0); 8427c478bd9Sstevel@tonic-gate } 8437c478bd9Sstevel@tonic-gate #else /* !CANNOT_CONNECT_DGRAM */ 8447c478bd9Sstevel@tonic-gate if (sendto(s, (const char*)buf, buflen, 0, nsap, nsaplen) != buflen) 8457c478bd9Sstevel@tonic-gate { 8467c478bd9Sstevel@tonic-gate Aerror(statp, stderr, "sendto", errno, nsap, nsaplen); 8477c478bd9Sstevel@tonic-gate res_nclose(statp); 8487c478bd9Sstevel@tonic-gate return (0); 8497c478bd9Sstevel@tonic-gate } 8507c478bd9Sstevel@tonic-gate #endif /* !CANNOT_CONNECT_DGRAM */ 8517c478bd9Sstevel@tonic-gate 8527c478bd9Sstevel@tonic-gate /* 8537c478bd9Sstevel@tonic-gate * Wait for reply. 8547c478bd9Sstevel@tonic-gate */ 8557c478bd9Sstevel@tonic-gate seconds = (statp->retrans << ns); 8567c478bd9Sstevel@tonic-gate if (ns > 0) 8577c478bd9Sstevel@tonic-gate seconds /= statp->nscount; 8587c478bd9Sstevel@tonic-gate if (seconds <= 0) 8597c478bd9Sstevel@tonic-gate seconds = 1; 8607c478bd9Sstevel@tonic-gate now = evNowTime(); 8617c478bd9Sstevel@tonic-gate timeout = evConsTime(seconds, 0); 8627c478bd9Sstevel@tonic-gate finish = evAddTime(now, timeout); 8637c478bd9Sstevel@tonic-gate goto nonow; 8647c478bd9Sstevel@tonic-gate wait: 8657c478bd9Sstevel@tonic-gate now = evNowTime(); 8667c478bd9Sstevel@tonic-gate nonow: 8677c478bd9Sstevel@tonic-gate #ifdef SUNW_POLL 8687c478bd9Sstevel@tonic-gate timeout = evSubTime(finish, now); 8697c478bd9Sstevel@tonic-gate if (timeout.tv_sec < 0) 8707c478bd9Sstevel@tonic-gate timeout = evConsTime(0, 0); 8717c478bd9Sstevel@tonic-gate polltimeout = 1000*timeout.tv_sec + 8727c478bd9Sstevel@tonic-gate timeout.tv_nsec/1000000; 8737c478bd9Sstevel@tonic-gate pollfd.fd = s; 8747c478bd9Sstevel@tonic-gate pollfd.events = POLLRDNORM; 8757c478bd9Sstevel@tonic-gate n = poll(&pollfd, 1, polltimeout); 8767c478bd9Sstevel@tonic-gate #else 8777c478bd9Sstevel@tonic-gate FD_ZERO(&dsmask); 8787c478bd9Sstevel@tonic-gate FD_SET(s, &dsmask); 8797c478bd9Sstevel@tonic-gate if (evCmpTime(finish, now) > 0) 8807c478bd9Sstevel@tonic-gate timeout = evSubTime(finish, now); 8817c478bd9Sstevel@tonic-gate else 8827c478bd9Sstevel@tonic-gate timeout = evConsTime(0, 0); 8837c478bd9Sstevel@tonic-gate n = pselect(s + 1, &dsmask, NULL, NULL, &timeout, NULL); 8847c478bd9Sstevel@tonic-gate #endif 8857c478bd9Sstevel@tonic-gate if (n == 0) { 8867c478bd9Sstevel@tonic-gate Dprint(statp->options & RES_DEBUG, (stdout, ";; timeout\n")); 8877c478bd9Sstevel@tonic-gate *gotsomewhere = 1; 8887c478bd9Sstevel@tonic-gate return (0); 8897c478bd9Sstevel@tonic-gate } 8907c478bd9Sstevel@tonic-gate if (n < 0) { 8917c478bd9Sstevel@tonic-gate if (errno == EINTR) 8927c478bd9Sstevel@tonic-gate goto wait; 8937c478bd9Sstevel@tonic-gate #ifdef SUNW_POLL 8947c478bd9Sstevel@tonic-gate Perror(statp, stderr, "poll", errno); 8957c478bd9Sstevel@tonic-gate #else 8967c478bd9Sstevel@tonic-gate Perror(statp, stderr, "select", errno); 8977c478bd9Sstevel@tonic-gate #endif 8987c478bd9Sstevel@tonic-gate res_nclose(statp); 8997c478bd9Sstevel@tonic-gate return (0); 9007c478bd9Sstevel@tonic-gate } 9017c478bd9Sstevel@tonic-gate errno = 0; 9027c478bd9Sstevel@tonic-gate fromlen = sizeof(from); 9037c478bd9Sstevel@tonic-gate resplen = recvfrom(s, (char*)ans, anssiz,0, 9047c478bd9Sstevel@tonic-gate (struct sockaddr *)&from, &fromlen); 9057c478bd9Sstevel@tonic-gate if (resplen <= 0) { 9067c478bd9Sstevel@tonic-gate Perror(statp, stderr, "recvfrom", errno); 9077c478bd9Sstevel@tonic-gate res_nclose(statp); 9087c478bd9Sstevel@tonic-gate return (0); 9097c478bd9Sstevel@tonic-gate } 9107c478bd9Sstevel@tonic-gate *gotsomewhere = 1; 9117c478bd9Sstevel@tonic-gate if (resplen < HFIXEDSZ) { 9127c478bd9Sstevel@tonic-gate /* 9137c478bd9Sstevel@tonic-gate * Undersized message. 9147c478bd9Sstevel@tonic-gate */ 9157c478bd9Sstevel@tonic-gate Dprint(statp->options & RES_DEBUG, 9167c478bd9Sstevel@tonic-gate (stdout, ";; undersized: %d\n", 9177c478bd9Sstevel@tonic-gate resplen)); 9187c478bd9Sstevel@tonic-gate *terrno = EMSGSIZE; 9197c478bd9Sstevel@tonic-gate res_nclose(statp); 9207c478bd9Sstevel@tonic-gate return (0); 9217c478bd9Sstevel@tonic-gate } 9227c478bd9Sstevel@tonic-gate if (hp->id != anhp->id) { 9237c478bd9Sstevel@tonic-gate /* 9247c478bd9Sstevel@tonic-gate * response from old query, ignore it. 9257c478bd9Sstevel@tonic-gate * XXX - potential security hazard could 9267c478bd9Sstevel@tonic-gate * be detected here. 9277c478bd9Sstevel@tonic-gate */ 9287c478bd9Sstevel@tonic-gate DprintQ((statp->options & RES_DEBUG) || 9297c478bd9Sstevel@tonic-gate (statp->pfcode & RES_PRF_REPLY), 9307c478bd9Sstevel@tonic-gate (stdout, ";; old answer:\n"), 9317c478bd9Sstevel@tonic-gate ans, (resplen > anssiz) ? anssiz : resplen); 9327c478bd9Sstevel@tonic-gate goto wait; 9337c478bd9Sstevel@tonic-gate } 9347c478bd9Sstevel@tonic-gate if (!(statp->options & RES_INSECURE1) && 9357c478bd9Sstevel@tonic-gate !res_ourserver_p(statp, (struct sockaddr *)&from)) { 9367c478bd9Sstevel@tonic-gate /* 9377c478bd9Sstevel@tonic-gate * response from wrong server? ignore it. 9387c478bd9Sstevel@tonic-gate * XXX - potential security hazard could 9397c478bd9Sstevel@tonic-gate * be detected here. 9407c478bd9Sstevel@tonic-gate */ 9417c478bd9Sstevel@tonic-gate DprintQ((statp->options & RES_DEBUG) || 9427c478bd9Sstevel@tonic-gate (statp->pfcode & RES_PRF_REPLY), 9437c478bd9Sstevel@tonic-gate (stdout, ";; not our server:\n"), 9447c478bd9Sstevel@tonic-gate ans, (resplen > anssiz) ? anssiz : resplen); 9457c478bd9Sstevel@tonic-gate goto wait; 9467c478bd9Sstevel@tonic-gate } 9477c478bd9Sstevel@tonic-gate #ifdef RES_USE_EDNS0 9487c478bd9Sstevel@tonic-gate if (anhp->rcode == FORMERR && (statp->options & RES_USE_EDNS0) != 0) { 9497c478bd9Sstevel@tonic-gate /* 9507c478bd9Sstevel@tonic-gate * Do not retry if the server do not understand EDNS0. 9517c478bd9Sstevel@tonic-gate * The case has to be captured here, as FORMERR packet do not 9527c478bd9Sstevel@tonic-gate * carry query section, hence res_queriesmatch() returns 0. 9537c478bd9Sstevel@tonic-gate */ 9547c478bd9Sstevel@tonic-gate DprintQ(statp->options & RES_DEBUG, 9557c478bd9Sstevel@tonic-gate (stdout, "server rejected query with EDNS0:\n"), 9567c478bd9Sstevel@tonic-gate ans, (resplen > anssiz) ? anssiz : resplen); 9577c478bd9Sstevel@tonic-gate /* record the error */ 9587c478bd9Sstevel@tonic-gate statp->_flags |= RES_F_EDNS0ERR; 9597c478bd9Sstevel@tonic-gate res_nclose(statp); 9607c478bd9Sstevel@tonic-gate return (0); 9617c478bd9Sstevel@tonic-gate } 9627c478bd9Sstevel@tonic-gate #endif 9637c478bd9Sstevel@tonic-gate if (!(statp->options & RES_INSECURE2) && 9647c478bd9Sstevel@tonic-gate !res_queriesmatch(buf, buf + buflen, 9657c478bd9Sstevel@tonic-gate ans, ans + anssiz)) { 9667c478bd9Sstevel@tonic-gate /* 9677c478bd9Sstevel@tonic-gate * response contains wrong query? ignore it. 9687c478bd9Sstevel@tonic-gate * XXX - potential security hazard could 9697c478bd9Sstevel@tonic-gate * be detected here. 9707c478bd9Sstevel@tonic-gate */ 9717c478bd9Sstevel@tonic-gate DprintQ((statp->options & RES_DEBUG) || 9727c478bd9Sstevel@tonic-gate (statp->pfcode & RES_PRF_REPLY), 9737c478bd9Sstevel@tonic-gate (stdout, ";; wrong query name:\n"), 9747c478bd9Sstevel@tonic-gate ans, (resplen > anssiz) ? anssiz : resplen); 9757c478bd9Sstevel@tonic-gate goto wait; 9767c478bd9Sstevel@tonic-gate } 9777c478bd9Sstevel@tonic-gate if (anhp->rcode == SERVFAIL || 9787c478bd9Sstevel@tonic-gate anhp->rcode == NOTIMP || 9797c478bd9Sstevel@tonic-gate anhp->rcode == REFUSED) { 9807c478bd9Sstevel@tonic-gate DprintQ(statp->options & RES_DEBUG, 9817c478bd9Sstevel@tonic-gate (stdout, "server rejected query:\n"), 9827c478bd9Sstevel@tonic-gate ans, (resplen > anssiz) ? anssiz : resplen); 9837c478bd9Sstevel@tonic-gate res_nclose(statp); 9847c478bd9Sstevel@tonic-gate /* don't retry if called from dig */ 9857c478bd9Sstevel@tonic-gate if (!statp->pfcode) 9867c478bd9Sstevel@tonic-gate return (0); 9877c478bd9Sstevel@tonic-gate } 9887c478bd9Sstevel@tonic-gate if (!(statp->options & RES_IGNTC) && anhp->tc) { 9897c478bd9Sstevel@tonic-gate /* 9907c478bd9Sstevel@tonic-gate * To get the rest of answer, 9917c478bd9Sstevel@tonic-gate * use TCP with same server. 9927c478bd9Sstevel@tonic-gate */ 9937c478bd9Sstevel@tonic-gate Dprint(statp->options & RES_DEBUG, 9947c478bd9Sstevel@tonic-gate (stdout, ";; truncated answer\n")); 9957c478bd9Sstevel@tonic-gate *v_circuit = 1; 9967c478bd9Sstevel@tonic-gate res_nclose(statp); 9977c478bd9Sstevel@tonic-gate return (1); 9987c478bd9Sstevel@tonic-gate } 9997c478bd9Sstevel@tonic-gate /* 10007c478bd9Sstevel@tonic-gate * All is well, or the error is fatal. Signal that the 10017c478bd9Sstevel@tonic-gate * next nameserver ought not be tried. 10027c478bd9Sstevel@tonic-gate */ 10037c478bd9Sstevel@tonic-gate return (resplen); 10047c478bd9Sstevel@tonic-gate } 10057c478bd9Sstevel@tonic-gate 10067c478bd9Sstevel@tonic-gate static void 10077c478bd9Sstevel@tonic-gate Aerror(const res_state statp, FILE *file, const char *string, int error, 10087c478bd9Sstevel@tonic-gate const struct sockaddr *address, int alen) 10097c478bd9Sstevel@tonic-gate { 10107c478bd9Sstevel@tonic-gate int save = errno; 10117c478bd9Sstevel@tonic-gate char hbuf[NI_MAXHOST]; 10127c478bd9Sstevel@tonic-gate char sbuf[NI_MAXSERV]; 10137c478bd9Sstevel@tonic-gate 10147c478bd9Sstevel@tonic-gate alen = alen; 10157c478bd9Sstevel@tonic-gate 10167c478bd9Sstevel@tonic-gate if ((statp->options & RES_DEBUG) != 0) { 10177c478bd9Sstevel@tonic-gate if (getnameinfo(address, alen, hbuf, sizeof(hbuf), 10187c478bd9Sstevel@tonic-gate sbuf, sizeof(sbuf), niflags)) { 10197c478bd9Sstevel@tonic-gate strncpy(hbuf, "?", sizeof(hbuf) - 1); 10207c478bd9Sstevel@tonic-gate hbuf[sizeof(hbuf) - 1] = '\0'; 10217c478bd9Sstevel@tonic-gate strncpy(sbuf, "?", sizeof(sbuf) - 1); 10227c478bd9Sstevel@tonic-gate sbuf[sizeof(sbuf) - 1] = '\0'; 10237c478bd9Sstevel@tonic-gate } 10247c478bd9Sstevel@tonic-gate fprintf(file, "res_send: %s ([%s].%s): %s\n", 10257c478bd9Sstevel@tonic-gate string, hbuf, sbuf, strerror(error)); 10267c478bd9Sstevel@tonic-gate } 10277c478bd9Sstevel@tonic-gate errno = save; 10287c478bd9Sstevel@tonic-gate } 10297c478bd9Sstevel@tonic-gate 10307c478bd9Sstevel@tonic-gate static void 10317c478bd9Sstevel@tonic-gate Perror(const res_state statp, FILE *file, const char *string, int error) { 10327c478bd9Sstevel@tonic-gate int save = errno; 10337c478bd9Sstevel@tonic-gate 10347c478bd9Sstevel@tonic-gate if ((statp->options & RES_DEBUG) != 0) 10357c478bd9Sstevel@tonic-gate fprintf(file, "res_send: %s: %s\n", 10367c478bd9Sstevel@tonic-gate string, strerror(error)); 10377c478bd9Sstevel@tonic-gate errno = save; 10387c478bd9Sstevel@tonic-gate } 10397c478bd9Sstevel@tonic-gate 10407c478bd9Sstevel@tonic-gate static int 10417c478bd9Sstevel@tonic-gate sock_eq(struct sockaddr *a, struct sockaddr *b) { 10427c478bd9Sstevel@tonic-gate struct sockaddr_in *a4, *b4; 10437c478bd9Sstevel@tonic-gate struct sockaddr_in6 *a6, *b6; 10447c478bd9Sstevel@tonic-gate 10457c478bd9Sstevel@tonic-gate if (a->sa_family != b->sa_family) 10467c478bd9Sstevel@tonic-gate return 0; 10477c478bd9Sstevel@tonic-gate switch (a->sa_family) { 10487c478bd9Sstevel@tonic-gate case AF_INET: 10497c478bd9Sstevel@tonic-gate a4 = (struct sockaddr_in *)a; 10507c478bd9Sstevel@tonic-gate b4 = (struct sockaddr_in *)b; 10517c478bd9Sstevel@tonic-gate return a4->sin_port == b4->sin_port && 10527c478bd9Sstevel@tonic-gate a4->sin_addr.s_addr == b4->sin_addr.s_addr; 10537c478bd9Sstevel@tonic-gate case AF_INET6: 10547c478bd9Sstevel@tonic-gate a6 = (struct sockaddr_in6 *)a; 10557c478bd9Sstevel@tonic-gate b6 = (struct sockaddr_in6 *)b; 10567c478bd9Sstevel@tonic-gate return a6->sin6_port == b6->sin6_port && 10577c478bd9Sstevel@tonic-gate #ifdef HAVE_SIN6_SCOPE_ID 10587c478bd9Sstevel@tonic-gate a6->sin6_scope_id == b6->sin6_scope_id && 10597c478bd9Sstevel@tonic-gate #endif 10607c478bd9Sstevel@tonic-gate IN6_ARE_ADDR_EQUAL(&a6->sin6_addr, &b6->sin6_addr); 10617c478bd9Sstevel@tonic-gate default: 10627c478bd9Sstevel@tonic-gate return 0; 10637c478bd9Sstevel@tonic-gate } 10647c478bd9Sstevel@tonic-gate } 10657c478bd9Sstevel@tonic-gate 10667c478bd9Sstevel@tonic-gate #ifdef NEED_PSELECT 10677c478bd9Sstevel@tonic-gate /* XXX needs to move to the porting library. */ 10687c478bd9Sstevel@tonic-gate static int 10697c478bd9Sstevel@tonic-gate pselect(int nfds, void *rfds, void *wfds, void *efds, 10707c478bd9Sstevel@tonic-gate struct timespec *tsp, const sigset_t *sigmask) 10717c478bd9Sstevel@tonic-gate { 10727c478bd9Sstevel@tonic-gate struct timeval tv, *tvp; 10737c478bd9Sstevel@tonic-gate sigset_t sigs; 10747c478bd9Sstevel@tonic-gate int n; 10757c478bd9Sstevel@tonic-gate 10767c478bd9Sstevel@tonic-gate if (tsp) { 10777c478bd9Sstevel@tonic-gate tvp = &tv; 10787c478bd9Sstevel@tonic-gate tv = evTimeVal(*tsp); 10797c478bd9Sstevel@tonic-gate } else 10807c478bd9Sstevel@tonic-gate tvp = NULL; 10817c478bd9Sstevel@tonic-gate if (sigmask) 10827c478bd9Sstevel@tonic-gate sigprocmask(SIG_SETMASK, sigmask, &sigs); 10837c478bd9Sstevel@tonic-gate n = select(nfds, rfds, wfds, efds, tvp); 10847c478bd9Sstevel@tonic-gate if (sigmask) 10857c478bd9Sstevel@tonic-gate sigprocmask(SIG_SETMASK, &sigs, NULL); 10867c478bd9Sstevel@tonic-gate if (tsp) 10877c478bd9Sstevel@tonic-gate *tsp = evTimeSpec(tv); 10887c478bd9Sstevel@tonic-gate return (n); 10897c478bd9Sstevel@tonic-gate } 10907c478bd9Sstevel@tonic-gate #endif 1091