17c478bd9Sstevel@tonic-gate /*
29525b14bSRao Shoaib * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
37c478bd9Sstevel@tonic-gate * Use is subject to license terms.
4*b3700b07SGordon Ross *
5*b3700b07SGordon Ross * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
67c478bd9Sstevel@tonic-gate */
77c478bd9Sstevel@tonic-gate
89525b14bSRao Shoaib
99525b14bSRao Shoaib /*
109525b14bSRao Shoaib * Portions Copyright (C) 2004-2009 Internet Systems Consortium, Inc. ("ISC")
119525b14bSRao Shoaib * Portions Copyright (C) 1996-2003 Internet Software Consortium.
129525b14bSRao Shoaib *
139525b14bSRao Shoaib * Permission to use, copy, modify, and/or distribute this software for any
149525b14bSRao Shoaib * purpose with or without fee is hereby granted, provided that the above
159525b14bSRao Shoaib * copyright notice and this permission notice appear in all copies.
169525b14bSRao Shoaib *
179525b14bSRao Shoaib * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
189525b14bSRao Shoaib * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
199525b14bSRao Shoaib * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
209525b14bSRao Shoaib * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
219525b14bSRao Shoaib * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
229525b14bSRao Shoaib * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
239525b14bSRao Shoaib * PERFORMANCE OF THIS SOFTWARE.
249525b14bSRao Shoaib */
259525b14bSRao Shoaib
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate * Copyright (c) 1985, 1989, 1993
287c478bd9Sstevel@tonic-gate * The Regents of the University of California. All rights reserved.
299525b14bSRao Shoaib *
307c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
317c478bd9Sstevel@tonic-gate * modification, are permitted provided that the following conditions
327c478bd9Sstevel@tonic-gate * are met:
337c478bd9Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
347c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
357c478bd9Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
367c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
377c478bd9Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
387c478bd9Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
397c478bd9Sstevel@tonic-gate * must display the following acknowledgement:
407c478bd9Sstevel@tonic-gate * This product includes software developed by the University of
417c478bd9Sstevel@tonic-gate * California, Berkeley and its contributors.
427c478bd9Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors
437c478bd9Sstevel@tonic-gate * may be used to endorse or promote products derived from this software
447c478bd9Sstevel@tonic-gate * without specific prior written permission.
459525b14bSRao Shoaib *
467c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
477c478bd9Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
487c478bd9Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
497c478bd9Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
507c478bd9Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
517c478bd9Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
527c478bd9Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
537c478bd9Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
547c478bd9Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
557c478bd9Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
567c478bd9Sstevel@tonic-gate * SUCH DAMAGE.
577c478bd9Sstevel@tonic-gate */
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate * Portions Copyright (c) 1993 by Digital Equipment Corporation.
619525b14bSRao Shoaib *
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, and that
657c478bd9Sstevel@tonic-gate * the name of Digital Equipment Corporation not be used in advertising or
667c478bd9Sstevel@tonic-gate * publicity pertaining to distribution of the document or software without
677c478bd9Sstevel@tonic-gate * specific, written prior permission.
689525b14bSRao Shoaib *
697c478bd9Sstevel@tonic-gate * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
707c478bd9Sstevel@tonic-gate * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
717c478bd9Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
727c478bd9Sstevel@tonic-gate * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
737c478bd9Sstevel@tonic-gate * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
747c478bd9Sstevel@tonic-gate * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
757c478bd9Sstevel@tonic-gate * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
767c478bd9Sstevel@tonic-gate * SOFTWARE.
777c478bd9Sstevel@tonic-gate */
787c478bd9Sstevel@tonic-gate
799525b14bSRao Shoaib /*! \file
809525b14bSRao Shoaib * \brief
817c478bd9Sstevel@tonic-gate * Send query to name server and wait for reply.
827c478bd9Sstevel@tonic-gate */
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate #include "port_before.h"
857c478bd9Sstevel@tonic-gate #include "fd_setsize.h"
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate #include <sys/types.h>
887c478bd9Sstevel@tonic-gate #include <sys/param.h>
897c478bd9Sstevel@tonic-gate #include <sys/time.h>
907c478bd9Sstevel@tonic-gate #include <sys/socket.h>
917c478bd9Sstevel@tonic-gate #include <sys/uio.h>
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gate #include <netinet/in.h>
94*b3700b07SGordon Ross #include <netinet/tcp.h>
957c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
967c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate #include <errno.h>
997c478bd9Sstevel@tonic-gate #include <netdb.h>
1007c478bd9Sstevel@tonic-gate #include <resolv.h>
1017c478bd9Sstevel@tonic-gate #include <signal.h>
1027c478bd9Sstevel@tonic-gate #include <stdio.h>
1037c478bd9Sstevel@tonic-gate #include <stdlib.h>
1047c478bd9Sstevel@tonic-gate #include <string.h>
1057c478bd9Sstevel@tonic-gate #include <unistd.h>
1067c478bd9Sstevel@tonic-gate
1077c478bd9Sstevel@tonic-gate #include <isc/eventlib.h>
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate #include "port_after.h"
1107c478bd9Sstevel@tonic-gate
1119525b14bSRao Shoaib #ifdef USE_POLL
1129525b14bSRao Shoaib #ifdef HAVE_STROPTS_H
1137c478bd9Sstevel@tonic-gate #include <stropts.h>
114e8031f0aSraf #endif
1159525b14bSRao Shoaib #include <poll.h>
1169525b14bSRao Shoaib #endif /* USE_POLL */
1177c478bd9Sstevel@tonic-gate
1187c478bd9Sstevel@tonic-gate /* Options. Leave them on. */
1197c478bd9Sstevel@tonic-gate #define DEBUG
1207c478bd9Sstevel@tonic-gate #include "res_debug.h"
1217c478bd9Sstevel@tonic-gate #include "res_private.h"
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate #define EXT(res) ((res)->_u._ext)
1247c478bd9Sstevel@tonic-gate
1259525b14bSRao Shoaib #ifndef USE_POLL
1267c478bd9Sstevel@tonic-gate static const int highestFD = FD_SETSIZE - 1;
1277c478bd9Sstevel@tonic-gate #else
1287c478bd9Sstevel@tonic-gate static int highestFD = 0;
1297c478bd9Sstevel@tonic-gate #endif
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate /* Forward. */
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate static int get_salen __P((const struct sockaddr *));
1347c478bd9Sstevel@tonic-gate static struct sockaddr * get_nsaddr __P((res_state, size_t));
1357c478bd9Sstevel@tonic-gate static int send_vc(res_state, const u_char *, int,
1367c478bd9Sstevel@tonic-gate u_char *, int, int *, int);
1377c478bd9Sstevel@tonic-gate static int send_dg(res_state, const u_char *, int,
1389525b14bSRao Shoaib u_char *, int, int *, int, int,
1397c478bd9Sstevel@tonic-gate int *, int *);
1407c478bd9Sstevel@tonic-gate static void Aerror(const res_state, FILE *, const char *, int,
1417c478bd9Sstevel@tonic-gate const struct sockaddr *, int);
1427c478bd9Sstevel@tonic-gate static void Perror(const res_state, FILE *, const char *, int);
1437c478bd9Sstevel@tonic-gate static int sock_eq(struct sockaddr *, struct sockaddr *);
1449525b14bSRao Shoaib #if defined(NEED_PSELECT) && !defined(USE_POLL)
1457c478bd9Sstevel@tonic-gate static int pselect(int, void *, void *, void *,
1467c478bd9Sstevel@tonic-gate struct timespec *,
1477c478bd9Sstevel@tonic-gate const sigset_t *);
1487c478bd9Sstevel@tonic-gate #endif
1497c478bd9Sstevel@tonic-gate void res_pquery(const res_state, const u_char *, int, FILE *);
1507c478bd9Sstevel@tonic-gate
1519525b14bSRao Shoaib #ifndef ORIGINAL_ISC_CODE
1527c478bd9Sstevel@tonic-gate #pragma weak __res_nameinquery = res_nameinquery
1537c478bd9Sstevel@tonic-gate #pragma weak __res_queriesmatch = res_queriesmatch
1547c478bd9Sstevel@tonic-gate #pragma weak res_nisourserver = res_ourserver_p
1557c478bd9Sstevel@tonic-gate #endif /* ORIGINAL_ISC_CODE */
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gate static const int niflags = NI_NUMERICHOST | NI_NUMERICSERV;
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate /* Public. */
1607c478bd9Sstevel@tonic-gate
1619525b14bSRao Shoaib /*%
1627c478bd9Sstevel@tonic-gate * looks up "ina" in _res.ns_addr_list[]
1639525b14bSRao Shoaib *
1647c478bd9Sstevel@tonic-gate * returns:
1659525b14bSRao Shoaib *\li 0 : not found
1669525b14bSRao Shoaib *\li >0 : found
1679525b14bSRao Shoaib *
1687c478bd9Sstevel@tonic-gate * author:
1699525b14bSRao Shoaib *\li paul vixie, 29may94
1707c478bd9Sstevel@tonic-gate */
1717c478bd9Sstevel@tonic-gate int
res_ourserver_p(const res_state statp,const struct sockaddr * sa)1727c478bd9Sstevel@tonic-gate res_ourserver_p(const res_state statp, const struct sockaddr *sa) {
1737c478bd9Sstevel@tonic-gate const struct sockaddr_in *inp, *srv;
1747c478bd9Sstevel@tonic-gate const struct sockaddr_in6 *in6p, *srv6;
1757c478bd9Sstevel@tonic-gate int ns;
1767c478bd9Sstevel@tonic-gate
1777c478bd9Sstevel@tonic-gate switch (sa->sa_family) {
1787c478bd9Sstevel@tonic-gate case AF_INET:
1797c478bd9Sstevel@tonic-gate inp = (const struct sockaddr_in *)sa;
1807c478bd9Sstevel@tonic-gate for (ns = 0; ns < statp->nscount; ns++) {
1817c478bd9Sstevel@tonic-gate srv = (struct sockaddr_in *)get_nsaddr(statp, ns);
1827c478bd9Sstevel@tonic-gate if (srv->sin_family == inp->sin_family &&
1837c478bd9Sstevel@tonic-gate srv->sin_port == inp->sin_port &&
1847c478bd9Sstevel@tonic-gate (srv->sin_addr.s_addr == INADDR_ANY ||
1857c478bd9Sstevel@tonic-gate srv->sin_addr.s_addr == inp->sin_addr.s_addr))
1867c478bd9Sstevel@tonic-gate return (1);
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate break;
1897c478bd9Sstevel@tonic-gate case AF_INET6:
1907c478bd9Sstevel@tonic-gate if (EXT(statp).ext == NULL)
1917c478bd9Sstevel@tonic-gate break;
1927c478bd9Sstevel@tonic-gate in6p = (const struct sockaddr_in6 *)sa;
1937c478bd9Sstevel@tonic-gate for (ns = 0; ns < statp->nscount; ns++) {
1947c478bd9Sstevel@tonic-gate srv6 = (struct sockaddr_in6 *)get_nsaddr(statp, ns);
1957c478bd9Sstevel@tonic-gate if (srv6->sin6_family == in6p->sin6_family &&
1967c478bd9Sstevel@tonic-gate srv6->sin6_port == in6p->sin6_port &&
1977c478bd9Sstevel@tonic-gate #ifdef HAVE_SIN6_SCOPE_ID
1989525b14bSRao Shoaib (srv6->sin6_scope_id == 0 ||
1999525b14bSRao Shoaib srv6->sin6_scope_id == in6p->sin6_scope_id) &&
2007c478bd9Sstevel@tonic-gate #endif
2017c478bd9Sstevel@tonic-gate (IN6_IS_ADDR_UNSPECIFIED(&srv6->sin6_addr) ||
2027c478bd9Sstevel@tonic-gate IN6_ARE_ADDR_EQUAL(&srv6->sin6_addr, &in6p->sin6_addr)))
2037c478bd9Sstevel@tonic-gate return (1);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate break;
2067c478bd9Sstevel@tonic-gate default:
2077c478bd9Sstevel@tonic-gate break;
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate return (0);
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate
2129525b14bSRao Shoaib /*%
2137c478bd9Sstevel@tonic-gate * look for (name,type,class) in the query section of packet (buf,eom)
2149525b14bSRao Shoaib *
2157c478bd9Sstevel@tonic-gate * requires:
2169525b14bSRao Shoaib *\li buf + HFIXEDSZ <= eom
2179525b14bSRao Shoaib *
2187c478bd9Sstevel@tonic-gate * returns:
2199525b14bSRao Shoaib *\li -1 : format error
2209525b14bSRao Shoaib *\li 0 : not found
2219525b14bSRao Shoaib *\li >0 : found
2229525b14bSRao Shoaib *
2237c478bd9Sstevel@tonic-gate * author:
2249525b14bSRao Shoaib *\li paul vixie, 29may94
2257c478bd9Sstevel@tonic-gate */
2267c478bd9Sstevel@tonic-gate int
res_nameinquery(const char * name,int type,int class,const u_char * buf,const u_char * eom)2277c478bd9Sstevel@tonic-gate res_nameinquery(const char *name, int type, int class,
2287c478bd9Sstevel@tonic-gate const u_char *buf, const u_char *eom)
2297c478bd9Sstevel@tonic-gate {
2307c478bd9Sstevel@tonic-gate const u_char *cp = buf + HFIXEDSZ;
2317c478bd9Sstevel@tonic-gate int qdcount = ntohs(((const HEADER*)buf)->qdcount);
2327c478bd9Sstevel@tonic-gate
2337c478bd9Sstevel@tonic-gate while (qdcount-- > 0) {
2347c478bd9Sstevel@tonic-gate char tname[MAXDNAME+1];
2357c478bd9Sstevel@tonic-gate int n, ttype, tclass;
2367c478bd9Sstevel@tonic-gate
2377c478bd9Sstevel@tonic-gate n = dn_expand(buf, eom, cp, tname, sizeof tname);
2387c478bd9Sstevel@tonic-gate if (n < 0)
2397c478bd9Sstevel@tonic-gate return (-1);
2407c478bd9Sstevel@tonic-gate cp += n;
2417c478bd9Sstevel@tonic-gate if (cp + 2 * INT16SZ > eom)
2427c478bd9Sstevel@tonic-gate return (-1);
2437c478bd9Sstevel@tonic-gate ttype = ns_get16(cp); cp += INT16SZ;
2447c478bd9Sstevel@tonic-gate tclass = ns_get16(cp); cp += INT16SZ;
2457c478bd9Sstevel@tonic-gate if (ttype == type && tclass == class &&
2467c478bd9Sstevel@tonic-gate ns_samename(tname, name) == 1)
2477c478bd9Sstevel@tonic-gate return (1);
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate return (0);
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate
2529525b14bSRao Shoaib /*%
2537c478bd9Sstevel@tonic-gate * is there a 1:1 mapping of (name,type,class)
2547c478bd9Sstevel@tonic-gate * in (buf1,eom1) and (buf2,eom2)?
2559525b14bSRao Shoaib *
2567c478bd9Sstevel@tonic-gate * returns:
2579525b14bSRao Shoaib *\li -1 : format error
2589525b14bSRao Shoaib *\li 0 : not a 1:1 mapping
2599525b14bSRao Shoaib *\li >0 : is a 1:1 mapping
2609525b14bSRao Shoaib *
2617c478bd9Sstevel@tonic-gate * author:
2629525b14bSRao Shoaib *\li paul vixie, 29may94
2637c478bd9Sstevel@tonic-gate */
2647c478bd9Sstevel@tonic-gate int
res_queriesmatch(const u_char * buf1,const u_char * eom1,const u_char * buf2,const u_char * eom2)2657c478bd9Sstevel@tonic-gate res_queriesmatch(const u_char *buf1, const u_char *eom1,
2667c478bd9Sstevel@tonic-gate const u_char *buf2, const u_char *eom2)
2677c478bd9Sstevel@tonic-gate {
2687c478bd9Sstevel@tonic-gate const u_char *cp = buf1 + HFIXEDSZ;
2697c478bd9Sstevel@tonic-gate int qdcount = ntohs(((const HEADER*)buf1)->qdcount);
2707c478bd9Sstevel@tonic-gate
2717c478bd9Sstevel@tonic-gate if (buf1 + HFIXEDSZ > eom1 || buf2 + HFIXEDSZ > eom2)
2727c478bd9Sstevel@tonic-gate return (-1);
2737c478bd9Sstevel@tonic-gate
2747c478bd9Sstevel@tonic-gate /*
2757c478bd9Sstevel@tonic-gate * Only header section present in replies to
2767c478bd9Sstevel@tonic-gate * dynamic update packets.
2777c478bd9Sstevel@tonic-gate */
2787c478bd9Sstevel@tonic-gate if ((((const HEADER *)buf1)->opcode == ns_o_update) &&
2797c478bd9Sstevel@tonic-gate (((const HEADER *)buf2)->opcode == ns_o_update))
2807c478bd9Sstevel@tonic-gate return (1);
2817c478bd9Sstevel@tonic-gate
2827c478bd9Sstevel@tonic-gate if (qdcount != ntohs(((const HEADER*)buf2)->qdcount))
2837c478bd9Sstevel@tonic-gate return (0);
2847c478bd9Sstevel@tonic-gate while (qdcount-- > 0) {
2857c478bd9Sstevel@tonic-gate char tname[MAXDNAME+1];
2867c478bd9Sstevel@tonic-gate int n, ttype, tclass;
2877c478bd9Sstevel@tonic-gate
2887c478bd9Sstevel@tonic-gate n = dn_expand(buf1, eom1, cp, tname, sizeof tname);
2897c478bd9Sstevel@tonic-gate if (n < 0)
2907c478bd9Sstevel@tonic-gate return (-1);
2917c478bd9Sstevel@tonic-gate cp += n;
2927c478bd9Sstevel@tonic-gate if (cp + 2 * INT16SZ > eom1)
2937c478bd9Sstevel@tonic-gate return (-1);
2947c478bd9Sstevel@tonic-gate ttype = ns_get16(cp); cp += INT16SZ;
2957c478bd9Sstevel@tonic-gate tclass = ns_get16(cp); cp += INT16SZ;
2967c478bd9Sstevel@tonic-gate if (!res_nameinquery(tname, ttype, tclass, buf2, eom2))
2977c478bd9Sstevel@tonic-gate return (0);
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate return (1);
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate
3027c478bd9Sstevel@tonic-gate int
res_nsend(res_state statp,const u_char * buf,int buflen,u_char * ans,int anssiz)3037c478bd9Sstevel@tonic-gate res_nsend(res_state statp,
3047c478bd9Sstevel@tonic-gate const u_char *buf, int buflen, u_char *ans, int anssiz)
3057c478bd9Sstevel@tonic-gate {
3069525b14bSRao Shoaib int gotsomewhere, terrno, tries, v_circuit, resplen, ns, n;
3077c478bd9Sstevel@tonic-gate char abuf[NI_MAXHOST];
3087c478bd9Sstevel@tonic-gate
3099525b14bSRao Shoaib #ifdef USE_POLL
3107c478bd9Sstevel@tonic-gate highestFD = sysconf(_SC_OPEN_MAX) - 1;
3117c478bd9Sstevel@tonic-gate #endif
3127c478bd9Sstevel@tonic-gate
3139525b14bSRao Shoaib /* No name servers or res_init() failure */
314c16fc660Ssm if (statp->nscount == 0 || EXT(statp).ext == NULL) {
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);
3519525b14bSRao Shoaib if (getpeername(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 */
3879525b14bSRao Shoaib if ((statp->options & RES_ROTATE) != 0U &&
3889525b14bSRao Shoaib (statp->options & RES_BLAST) == 0U) {
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)
4029525b14bSRao Shoaib 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 */
4189525b14bSRao Shoaib for (tries = 0; tries < statp->retry; tries++) {
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. */
4669525b14bSRao Shoaib tries = 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,
4779525b14bSRao Shoaib ns, tries, &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 */
5029525b14bSRao Shoaib if ((v_circuit && (statp->options & RES_USEVC) == 0U) ||
5039525b14bSRao Shoaib (statp->options & RES_STAYOPEN) == 0U) {
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)
5429525b14bSRao Shoaib errno = ECONNREFUSED; /*%< no nameservers found */
5437c478bd9Sstevel@tonic-gate else
5449525b14bSRao Shoaib 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
get_salen(sa)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
5719525b14bSRao Shoaib return (0); /*%< unknown, die on connect */
5727c478bd9Sstevel@tonic-gate }
5737c478bd9Sstevel@tonic-gate
5749525b14bSRao Shoaib /*%
5757c478bd9Sstevel@tonic-gate * pick appropriate nsaddr_list for use. see res_init() for initialization.
5767c478bd9Sstevel@tonic-gate */
5777c478bd9Sstevel@tonic-gate static struct sockaddr *
get_nsaddr(statp,n)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
send_vc(res_state statp,const u_char * buf,int buflen,u_char * ans,int anssiz,int * terrno,int ns)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;
6149525b14bSRao Shoaib #ifdef SO_NOSIGPIPE
6159525b14bSRao Shoaib int on = 1;
6169525b14bSRao Shoaib #endif
6177c478bd9Sstevel@tonic-gate
6187c478bd9Sstevel@tonic-gate nsap = get_nsaddr(statp, ns);
6197c478bd9Sstevel@tonic-gate nsaplen = get_salen(nsap);
6207c478bd9Sstevel@tonic-gate
6217c478bd9Sstevel@tonic-gate connreset = 0;
6227c478bd9Sstevel@tonic-gate same_ns:
6237c478bd9Sstevel@tonic-gate truncating = 0;
6247c478bd9Sstevel@tonic-gate
6257c478bd9Sstevel@tonic-gate /* Are we still talking to whom we want to talk to? */
6267c478bd9Sstevel@tonic-gate if (statp->_vcsock >= 0 && (statp->_flags & RES_F_VC) != 0) {
6277c478bd9Sstevel@tonic-gate struct sockaddr_storage peer;
6287c478bd9Sstevel@tonic-gate ISC_SOCKLEN_T size = sizeof peer;
6297c478bd9Sstevel@tonic-gate
6307c478bd9Sstevel@tonic-gate if (getpeername(statp->_vcsock,
6317c478bd9Sstevel@tonic-gate (struct sockaddr *)&peer, &size) < 0 ||
6327c478bd9Sstevel@tonic-gate !sock_eq((struct sockaddr *)&peer, nsap)) {
6337c478bd9Sstevel@tonic-gate res_nclose(statp);
6347c478bd9Sstevel@tonic-gate statp->_flags &= ~RES_F_VC;
6357c478bd9Sstevel@tonic-gate }
6367c478bd9Sstevel@tonic-gate }
6377c478bd9Sstevel@tonic-gate
6387c478bd9Sstevel@tonic-gate if (statp->_vcsock < 0 || (statp->_flags & RES_F_VC) == 0) {
6397c478bd9Sstevel@tonic-gate if (statp->_vcsock >= 0)
6407c478bd9Sstevel@tonic-gate res_nclose(statp);
6417c478bd9Sstevel@tonic-gate
6427c478bd9Sstevel@tonic-gate statp->_vcsock = socket(nsap->sa_family, SOCK_STREAM, 0);
6437c478bd9Sstevel@tonic-gate if (statp->_vcsock > highestFD) {
6447c478bd9Sstevel@tonic-gate res_nclose(statp);
6457c478bd9Sstevel@tonic-gate errno = ENOTSOCK;
6467c478bd9Sstevel@tonic-gate }
6477c478bd9Sstevel@tonic-gate if (statp->_vcsock < 0) {
6489525b14bSRao Shoaib switch (errno) {
6499525b14bSRao Shoaib case EPROTONOSUPPORT:
6509525b14bSRao Shoaib #ifdef EPFNOSUPPORT
6519525b14bSRao Shoaib case EPFNOSUPPORT:
6529525b14bSRao Shoaib #endif
6539525b14bSRao Shoaib case EAFNOSUPPORT:
6549525b14bSRao Shoaib Perror(statp, stderr, "socket(vc)", errno);
6559525b14bSRao Shoaib return (0);
6569525b14bSRao Shoaib default:
6579525b14bSRao Shoaib *terrno = errno;
6589525b14bSRao Shoaib Perror(statp, stderr, "socket(vc)", errno);
6599525b14bSRao Shoaib return (-1);
6609525b14bSRao Shoaib }
6617c478bd9Sstevel@tonic-gate }
6629525b14bSRao Shoaib #ifdef SO_NOSIGPIPE
6639525b14bSRao Shoaib /*
6649525b14bSRao Shoaib * Disable generation of SIGPIPE when writing to a closed
6659525b14bSRao Shoaib * socket. Write should return -1 and set errno to EPIPE
6669525b14bSRao Shoaib * instead.
6679525b14bSRao Shoaib *
6689525b14bSRao Shoaib * Push on even if setsockopt(SO_NOSIGPIPE) fails.
6699525b14bSRao Shoaib */
6709525b14bSRao Shoaib (void)setsockopt(statp->_vcsock, SOL_SOCKET, SO_NOSIGPIPE, &on,
6719525b14bSRao Shoaib sizeof(on));
672*b3700b07SGordon Ross #endif
673*b3700b07SGordon Ross #ifdef TCP_CONN_ABORT_THRESHOLD
674*b3700b07SGordon Ross /*
675*b3700b07SGordon Ross * The default connection timeout is over two minutes.
676*b3700b07SGordon Ross * We need something more reasonable here. The default
677*b3700b07SGordon Ross * retrans value is 5 sec., then 10, 20, 40, on retries.
678*b3700b07SGordon Ross * TCP connect does its own retries, so we want just one
679*b3700b07SGordon Ross * reasonable timeout value. Using 2X retrans, which
680*b3700b07SGordon Ross * gives us a 10 sec. connect timeout. If we're waiting
681*b3700b07SGordon Ross * that long to connect, we probably want to give up and
682*b3700b07SGordon Ross * try the next DNS server in our list.
683*b3700b07SGordon Ross *
684*b3700b07SGordon Ross * It might be reasonable to do this for all callers,
685*b3700b07SGordon Ross * but for now do it only when we see MS_INTEROP in the
686*b3700b07SGordon Ross * environment (set in smbd and idmapd)
687*b3700b07SGordon Ross */
688*b3700b07SGordon Ross if (getenv("MS_INTEROP") != NULL) {
689*b3700b07SGordon Ross int conn_tmo;
690*b3700b07SGordon Ross conn_tmo = statp->retrans * 2000; /* mSec */
691*b3700b07SGordon Ross (void)setsockopt(statp->_vcsock, IPPROTO_TCP,
692*b3700b07SGordon Ross TCP_CONN_ABORT_THRESHOLD, &conn_tmo,
693*b3700b07SGordon Ross sizeof(conn_tmo));
694*b3700b07SGordon Ross }
6959525b14bSRao Shoaib #endif
6967c478bd9Sstevel@tonic-gate errno = 0;
6977c478bd9Sstevel@tonic-gate if (connect(statp->_vcsock, nsap, nsaplen) < 0) {
6987c478bd9Sstevel@tonic-gate *terrno = errno;
6997c478bd9Sstevel@tonic-gate Aerror(statp, stderr, "connect/vc", errno, nsap,
7007c478bd9Sstevel@tonic-gate nsaplen);
7017c478bd9Sstevel@tonic-gate res_nclose(statp);
7027c478bd9Sstevel@tonic-gate return (0);
7037c478bd9Sstevel@tonic-gate }
7047c478bd9Sstevel@tonic-gate statp->_flags |= RES_F_VC;
7057c478bd9Sstevel@tonic-gate }
7067c478bd9Sstevel@tonic-gate
7077c478bd9Sstevel@tonic-gate /*
7087c478bd9Sstevel@tonic-gate * Send length & message
7097c478bd9Sstevel@tonic-gate */
7107c478bd9Sstevel@tonic-gate ns_put16((u_short)buflen, (u_char*)&len);
7117c478bd9Sstevel@tonic-gate iov[0] = evConsIovec(&len, INT16SZ);
7127c478bd9Sstevel@tonic-gate DE_CONST(buf, tmp);
7137c478bd9Sstevel@tonic-gate iov[1] = evConsIovec(tmp, buflen);
7147c478bd9Sstevel@tonic-gate if (writev(statp->_vcsock, iov, 2) != (INT16SZ + buflen)) {
7157c478bd9Sstevel@tonic-gate *terrno = errno;
7167c478bd9Sstevel@tonic-gate Perror(statp, stderr, "write failed", errno);
7177c478bd9Sstevel@tonic-gate res_nclose(statp);
7187c478bd9Sstevel@tonic-gate return (0);
7197c478bd9Sstevel@tonic-gate }
7207c478bd9Sstevel@tonic-gate /*
7217c478bd9Sstevel@tonic-gate * Receive length & response
7227c478bd9Sstevel@tonic-gate */
7237c478bd9Sstevel@tonic-gate read_len:
7247c478bd9Sstevel@tonic-gate cp = ans;
7257c478bd9Sstevel@tonic-gate len = INT16SZ;
7267c478bd9Sstevel@tonic-gate while ((n = read(statp->_vcsock, (char *)cp, (int)len)) > 0) {
7277c478bd9Sstevel@tonic-gate cp += n;
7289525b14bSRao Shoaib if ((len -= n) == 0)
7297c478bd9Sstevel@tonic-gate break;
7307c478bd9Sstevel@tonic-gate }
7317c478bd9Sstevel@tonic-gate if (n <= 0) {
7327c478bd9Sstevel@tonic-gate *terrno = errno;
7337c478bd9Sstevel@tonic-gate Perror(statp, stderr, "read failed", errno);
7347c478bd9Sstevel@tonic-gate res_nclose(statp);
7357c478bd9Sstevel@tonic-gate /*
7367c478bd9Sstevel@tonic-gate * A long running process might get its TCP
7377c478bd9Sstevel@tonic-gate * connection reset if the remote server was
7387c478bd9Sstevel@tonic-gate * restarted. Requery the server instead of
7397c478bd9Sstevel@tonic-gate * trying a new one. When there is only one
7407c478bd9Sstevel@tonic-gate * server, this means that a query might work
7417c478bd9Sstevel@tonic-gate * instead of failing. We only allow one reset
7427c478bd9Sstevel@tonic-gate * per query to prevent looping.
7437c478bd9Sstevel@tonic-gate */
7447c478bd9Sstevel@tonic-gate if (*terrno == ECONNRESET && !connreset) {
7457c478bd9Sstevel@tonic-gate connreset = 1;
7467c478bd9Sstevel@tonic-gate res_nclose(statp);
7477c478bd9Sstevel@tonic-gate goto same_ns;
7487c478bd9Sstevel@tonic-gate }
7497c478bd9Sstevel@tonic-gate res_nclose(statp);
7507c478bd9Sstevel@tonic-gate return (0);
7517c478bd9Sstevel@tonic-gate }
7527c478bd9Sstevel@tonic-gate resplen = ns_get16(ans);
7537c478bd9Sstevel@tonic-gate if (resplen > anssiz) {
7547c478bd9Sstevel@tonic-gate Dprint(statp->options & RES_DEBUG,
7557c478bd9Sstevel@tonic-gate (stdout, ";; response truncated\n")
7567c478bd9Sstevel@tonic-gate );
7577c478bd9Sstevel@tonic-gate truncating = 1;
7587c478bd9Sstevel@tonic-gate len = anssiz;
7597c478bd9Sstevel@tonic-gate } else
7607c478bd9Sstevel@tonic-gate len = resplen;
7617c478bd9Sstevel@tonic-gate if (len < HFIXEDSZ) {
7627c478bd9Sstevel@tonic-gate /*
7637c478bd9Sstevel@tonic-gate * Undersized message.
7647c478bd9Sstevel@tonic-gate */
7657c478bd9Sstevel@tonic-gate Dprint(statp->options & RES_DEBUG,
7667c478bd9Sstevel@tonic-gate (stdout, ";; undersized: %d\n", len));
7677c478bd9Sstevel@tonic-gate *terrno = EMSGSIZE;
7687c478bd9Sstevel@tonic-gate res_nclose(statp);
7697c478bd9Sstevel@tonic-gate return (0);
7707c478bd9Sstevel@tonic-gate }
7717c478bd9Sstevel@tonic-gate cp = ans;
7727c478bd9Sstevel@tonic-gate while (len != 0 && (n = read(statp->_vcsock, (char *)cp, (int)len)) > 0){
7737c478bd9Sstevel@tonic-gate cp += n;
7747c478bd9Sstevel@tonic-gate len -= n;
7757c478bd9Sstevel@tonic-gate }
7767c478bd9Sstevel@tonic-gate if (n <= 0) {
7777c478bd9Sstevel@tonic-gate *terrno = errno;
7787c478bd9Sstevel@tonic-gate Perror(statp, stderr, "read(vc)", errno);
7797c478bd9Sstevel@tonic-gate res_nclose(statp);
7807c478bd9Sstevel@tonic-gate return (0);
7817c478bd9Sstevel@tonic-gate }
7827c478bd9Sstevel@tonic-gate if (truncating) {
7837c478bd9Sstevel@tonic-gate /*
7847c478bd9Sstevel@tonic-gate * Flush rest of answer so connection stays in synch.
7857c478bd9Sstevel@tonic-gate */
7867c478bd9Sstevel@tonic-gate anhp->tc = 1;
7877c478bd9Sstevel@tonic-gate len = resplen - anssiz;
7887c478bd9Sstevel@tonic-gate while (len != 0) {
7897c478bd9Sstevel@tonic-gate char junk[PACKETSZ];
7907c478bd9Sstevel@tonic-gate
7917c478bd9Sstevel@tonic-gate n = read(statp->_vcsock, junk,
7927c478bd9Sstevel@tonic-gate (len > sizeof junk) ? sizeof junk : len);
7937c478bd9Sstevel@tonic-gate if (n > 0)
7947c478bd9Sstevel@tonic-gate len -= n;
7957c478bd9Sstevel@tonic-gate else
7967c478bd9Sstevel@tonic-gate break;
7977c478bd9Sstevel@tonic-gate }
7987c478bd9Sstevel@tonic-gate }
7997c478bd9Sstevel@tonic-gate /*
8007c478bd9Sstevel@tonic-gate * If the calling applicating has bailed out of
8017c478bd9Sstevel@tonic-gate * a previous call and failed to arrange to have
8027c478bd9Sstevel@tonic-gate * the circuit closed or the server has got
8037c478bd9Sstevel@tonic-gate * itself confused, then drop the packet and
8047c478bd9Sstevel@tonic-gate * wait for the correct one.
8057c478bd9Sstevel@tonic-gate */
8067c478bd9Sstevel@tonic-gate if (hp->id != anhp->id) {
8077c478bd9Sstevel@tonic-gate DprintQ((statp->options & RES_DEBUG) ||
8087c478bd9Sstevel@tonic-gate (statp->pfcode & RES_PRF_REPLY),
8097c478bd9Sstevel@tonic-gate (stdout, ";; old answer (unexpected):\n"),
8107c478bd9Sstevel@tonic-gate ans, (resplen > anssiz) ? anssiz: resplen);
8117c478bd9Sstevel@tonic-gate goto read_len;
8127c478bd9Sstevel@tonic-gate }
8137c478bd9Sstevel@tonic-gate
8147c478bd9Sstevel@tonic-gate /*
8157c478bd9Sstevel@tonic-gate * All is well, or the error is fatal. Signal that the
8167c478bd9Sstevel@tonic-gate * next nameserver ought not be tried.
8177c478bd9Sstevel@tonic-gate */
8187c478bd9Sstevel@tonic-gate return (resplen);
8197c478bd9Sstevel@tonic-gate }
8207c478bd9Sstevel@tonic-gate
8217c478bd9Sstevel@tonic-gate static int
send_dg(res_state statp,const u_char * buf,int buflen,u_char * ans,int anssiz,int * terrno,int ns,int tries,int * v_circuit,int * gotsomewhere)8229525b14bSRao Shoaib send_dg(res_state statp, const u_char *buf, int buflen, u_char *ans,
8239525b14bSRao Shoaib int anssiz, int *terrno, int ns, int tries, int *v_circuit,
8249525b14bSRao Shoaib int *gotsomewhere)
8257c478bd9Sstevel@tonic-gate {
8267c478bd9Sstevel@tonic-gate const HEADER *hp = (const HEADER *) buf;
8277c478bd9Sstevel@tonic-gate HEADER *anhp = (HEADER *) ans;
8287c478bd9Sstevel@tonic-gate const struct sockaddr *nsap;
8297c478bd9Sstevel@tonic-gate int nsaplen;
8307c478bd9Sstevel@tonic-gate struct timespec now, timeout, finish;
8317c478bd9Sstevel@tonic-gate struct sockaddr_storage from;
8327c478bd9Sstevel@tonic-gate ISC_SOCKLEN_T fromlen;
8337c478bd9Sstevel@tonic-gate int resplen, seconds, n, s;
8349525b14bSRao Shoaib #ifdef USE_POLL
8359525b14bSRao Shoaib int polltimeout;
8367c478bd9Sstevel@tonic-gate struct pollfd pollfd;
8379525b14bSRao Shoaib #else
8389525b14bSRao Shoaib fd_set dsmask;
8397c478bd9Sstevel@tonic-gate #endif
8407c478bd9Sstevel@tonic-gate
8417c478bd9Sstevel@tonic-gate nsap = get_nsaddr(statp, ns);
8427c478bd9Sstevel@tonic-gate nsaplen = get_salen(nsap);
8437c478bd9Sstevel@tonic-gate if (EXT(statp).nssocks[ns] == -1) {
8447c478bd9Sstevel@tonic-gate EXT(statp).nssocks[ns] = socket(nsap->sa_family, SOCK_DGRAM, 0);
8457c478bd9Sstevel@tonic-gate if (EXT(statp).nssocks[ns] > highestFD) {
8467c478bd9Sstevel@tonic-gate res_nclose(statp);
8477c478bd9Sstevel@tonic-gate errno = ENOTSOCK;
8487c478bd9Sstevel@tonic-gate }
8497c478bd9Sstevel@tonic-gate if (EXT(statp).nssocks[ns] < 0) {
8509525b14bSRao Shoaib switch (errno) {
8519525b14bSRao Shoaib case EPROTONOSUPPORT:
8529525b14bSRao Shoaib #ifdef EPFNOSUPPORT
8539525b14bSRao Shoaib case EPFNOSUPPORT:
8549525b14bSRao Shoaib #endif
8559525b14bSRao Shoaib case EAFNOSUPPORT:
8569525b14bSRao Shoaib Perror(statp, stderr, "socket(dg)", errno);
8579525b14bSRao Shoaib return (0);
8589525b14bSRao Shoaib default:
8599525b14bSRao Shoaib *terrno = errno;
8609525b14bSRao Shoaib Perror(statp, stderr, "socket(dg)", errno);
8619525b14bSRao Shoaib return (-1);
8629525b14bSRao Shoaib }
8637c478bd9Sstevel@tonic-gate }
8647c478bd9Sstevel@tonic-gate #ifndef CANNOT_CONNECT_DGRAM
8657c478bd9Sstevel@tonic-gate /*
8667c478bd9Sstevel@tonic-gate * On a 4.3BSD+ machine (client and server,
8677c478bd9Sstevel@tonic-gate * actually), sending to a nameserver datagram
8687c478bd9Sstevel@tonic-gate * port with no nameserver will cause an
8697c478bd9Sstevel@tonic-gate * ICMP port unreachable message to be returned.
8707c478bd9Sstevel@tonic-gate * If our datagram socket is "connected" to the
8717c478bd9Sstevel@tonic-gate * server, we get an ECONNREFUSED error on the next
8727c478bd9Sstevel@tonic-gate * socket operation, and select returns if the
8737c478bd9Sstevel@tonic-gate * error message is received. We can thus detect
8747c478bd9Sstevel@tonic-gate * the absence of a nameserver without timing out.
8757c478bd9Sstevel@tonic-gate */
8767c478bd9Sstevel@tonic-gate if (connect(EXT(statp).nssocks[ns], nsap, nsaplen) < 0) {
8777c478bd9Sstevel@tonic-gate Aerror(statp, stderr, "connect(dg)", errno, nsap,
8787c478bd9Sstevel@tonic-gate nsaplen);
8797c478bd9Sstevel@tonic-gate res_nclose(statp);
8807c478bd9Sstevel@tonic-gate return (0);
8817c478bd9Sstevel@tonic-gate }
8827c478bd9Sstevel@tonic-gate #endif /* !CANNOT_CONNECT_DGRAM */
8837c478bd9Sstevel@tonic-gate Dprint(statp->options & RES_DEBUG,
8847c478bd9Sstevel@tonic-gate (stdout, ";; new DG socket\n"))
8857c478bd9Sstevel@tonic-gate }
8867c478bd9Sstevel@tonic-gate s = EXT(statp).nssocks[ns];
8877c478bd9Sstevel@tonic-gate #ifndef CANNOT_CONNECT_DGRAM
8887c478bd9Sstevel@tonic-gate if (send(s, (const char*)buf, buflen, 0) != buflen) {
8897c478bd9Sstevel@tonic-gate Perror(statp, stderr, "send", errno);
8907c478bd9Sstevel@tonic-gate res_nclose(statp);
8917c478bd9Sstevel@tonic-gate return (0);
8927c478bd9Sstevel@tonic-gate }
8937c478bd9Sstevel@tonic-gate #else /* !CANNOT_CONNECT_DGRAM */
8947c478bd9Sstevel@tonic-gate if (sendto(s, (const char*)buf, buflen, 0, nsap, nsaplen) != buflen)
8957c478bd9Sstevel@tonic-gate {
8967c478bd9Sstevel@tonic-gate Aerror(statp, stderr, "sendto", errno, nsap, nsaplen);
8977c478bd9Sstevel@tonic-gate res_nclose(statp);
8987c478bd9Sstevel@tonic-gate return (0);
8997c478bd9Sstevel@tonic-gate }
9007c478bd9Sstevel@tonic-gate #endif /* !CANNOT_CONNECT_DGRAM */
9017c478bd9Sstevel@tonic-gate
9027c478bd9Sstevel@tonic-gate /*
9037c478bd9Sstevel@tonic-gate * Wait for reply.
9047c478bd9Sstevel@tonic-gate */
9059525b14bSRao Shoaib seconds = (statp->retrans << tries);
9067c478bd9Sstevel@tonic-gate if (ns > 0)
9077c478bd9Sstevel@tonic-gate seconds /= statp->nscount;
9087c478bd9Sstevel@tonic-gate if (seconds <= 0)
9097c478bd9Sstevel@tonic-gate seconds = 1;
9107c478bd9Sstevel@tonic-gate now = evNowTime();
9117c478bd9Sstevel@tonic-gate timeout = evConsTime(seconds, 0);
9127c478bd9Sstevel@tonic-gate finish = evAddTime(now, timeout);
9137c478bd9Sstevel@tonic-gate goto nonow;
9147c478bd9Sstevel@tonic-gate wait:
9157c478bd9Sstevel@tonic-gate now = evNowTime();
9167c478bd9Sstevel@tonic-gate nonow:
9179525b14bSRao Shoaib #ifndef USE_POLL
9189525b14bSRao Shoaib FD_ZERO(&dsmask);
9199525b14bSRao Shoaib FD_SET(s, &dsmask);
9209525b14bSRao Shoaib if (evCmpTime(finish, now) > 0)
9219525b14bSRao Shoaib timeout = evSubTime(finish, now);
9229525b14bSRao Shoaib else
9239525b14bSRao Shoaib timeout = evConsTime(0, 0);
9249525b14bSRao Shoaib n = pselect(s + 1, &dsmask, NULL, NULL, &timeout, NULL);
9259525b14bSRao Shoaib #else
9267c478bd9Sstevel@tonic-gate timeout = evSubTime(finish, now);
9277c478bd9Sstevel@tonic-gate if (timeout.tv_sec < 0)
9287c478bd9Sstevel@tonic-gate timeout = evConsTime(0, 0);
9297c478bd9Sstevel@tonic-gate polltimeout = 1000*timeout.tv_sec +
9307c478bd9Sstevel@tonic-gate timeout.tv_nsec/1000000;
9317c478bd9Sstevel@tonic-gate pollfd.fd = s;
9327c478bd9Sstevel@tonic-gate pollfd.events = POLLRDNORM;
9337c478bd9Sstevel@tonic-gate n = poll(&pollfd, 1, polltimeout);
9349525b14bSRao Shoaib #endif /* USE_POLL */
9359525b14bSRao Shoaib
9367c478bd9Sstevel@tonic-gate if (n == 0) {
9377c478bd9Sstevel@tonic-gate Dprint(statp->options & RES_DEBUG, (stdout, ";; timeout\n"));
9387c478bd9Sstevel@tonic-gate *gotsomewhere = 1;
9397c478bd9Sstevel@tonic-gate return (0);
9407c478bd9Sstevel@tonic-gate }
9417c478bd9Sstevel@tonic-gate if (n < 0) {
9427c478bd9Sstevel@tonic-gate if (errno == EINTR)
9437c478bd9Sstevel@tonic-gate goto wait;
9449525b14bSRao Shoaib #ifndef USE_POLL
9457c478bd9Sstevel@tonic-gate Perror(statp, stderr, "select", errno);
9469525b14bSRao Shoaib #else
9479525b14bSRao Shoaib Perror(statp, stderr, "poll", errno);
9489525b14bSRao Shoaib #endif /* USE_POLL */
9497c478bd9Sstevel@tonic-gate res_nclose(statp);
9507c478bd9Sstevel@tonic-gate return (0);
9517c478bd9Sstevel@tonic-gate }
9527c478bd9Sstevel@tonic-gate errno = 0;
9537c478bd9Sstevel@tonic-gate fromlen = sizeof(from);
9547c478bd9Sstevel@tonic-gate resplen = recvfrom(s, (char*)ans, anssiz,0,
9557c478bd9Sstevel@tonic-gate (struct sockaddr *)&from, &fromlen);
9567c478bd9Sstevel@tonic-gate if (resplen <= 0) {
9577c478bd9Sstevel@tonic-gate Perror(statp, stderr, "recvfrom", errno);
9587c478bd9Sstevel@tonic-gate res_nclose(statp);
9597c478bd9Sstevel@tonic-gate return (0);
9607c478bd9Sstevel@tonic-gate }
9617c478bd9Sstevel@tonic-gate *gotsomewhere = 1;
9627c478bd9Sstevel@tonic-gate if (resplen < HFIXEDSZ) {
9637c478bd9Sstevel@tonic-gate /*
9647c478bd9Sstevel@tonic-gate * Undersized message.
9657c478bd9Sstevel@tonic-gate */
9667c478bd9Sstevel@tonic-gate Dprint(statp->options & RES_DEBUG,
9677c478bd9Sstevel@tonic-gate (stdout, ";; undersized: %d\n",
9687c478bd9Sstevel@tonic-gate resplen));
9697c478bd9Sstevel@tonic-gate *terrno = EMSGSIZE;
9707c478bd9Sstevel@tonic-gate res_nclose(statp);
9717c478bd9Sstevel@tonic-gate return (0);
9727c478bd9Sstevel@tonic-gate }
9737c478bd9Sstevel@tonic-gate if (hp->id != anhp->id) {
9747c478bd9Sstevel@tonic-gate /*
9757c478bd9Sstevel@tonic-gate * response from old query, ignore it.
9767c478bd9Sstevel@tonic-gate * XXX - potential security hazard could
9777c478bd9Sstevel@tonic-gate * be detected here.
9787c478bd9Sstevel@tonic-gate */
9797c478bd9Sstevel@tonic-gate DprintQ((statp->options & RES_DEBUG) ||
9807c478bd9Sstevel@tonic-gate (statp->pfcode & RES_PRF_REPLY),
9817c478bd9Sstevel@tonic-gate (stdout, ";; old answer:\n"),
9827c478bd9Sstevel@tonic-gate ans, (resplen > anssiz) ? anssiz : resplen);
9837c478bd9Sstevel@tonic-gate goto wait;
9847c478bd9Sstevel@tonic-gate }
9857c478bd9Sstevel@tonic-gate if (!(statp->options & RES_INSECURE1) &&
9867c478bd9Sstevel@tonic-gate !res_ourserver_p(statp, (struct sockaddr *)&from)) {
9877c478bd9Sstevel@tonic-gate /*
9887c478bd9Sstevel@tonic-gate * response from wrong server? ignore it.
9897c478bd9Sstevel@tonic-gate * XXX - potential security hazard could
9907c478bd9Sstevel@tonic-gate * be detected here.
9917c478bd9Sstevel@tonic-gate */
9927c478bd9Sstevel@tonic-gate DprintQ((statp->options & RES_DEBUG) ||
9937c478bd9Sstevel@tonic-gate (statp->pfcode & RES_PRF_REPLY),
9947c478bd9Sstevel@tonic-gate (stdout, ";; not our server:\n"),
9957c478bd9Sstevel@tonic-gate ans, (resplen > anssiz) ? anssiz : resplen);
9967c478bd9Sstevel@tonic-gate goto wait;
9977c478bd9Sstevel@tonic-gate }
9987c478bd9Sstevel@tonic-gate #ifdef RES_USE_EDNS0
9999525b14bSRao Shoaib if (anhp->rcode == FORMERR && (statp->options & RES_USE_EDNS0) != 0U) {
10007c478bd9Sstevel@tonic-gate /*
10017c478bd9Sstevel@tonic-gate * Do not retry if the server do not understand EDNS0.
10027c478bd9Sstevel@tonic-gate * The case has to be captured here, as FORMERR packet do not
10037c478bd9Sstevel@tonic-gate * carry query section, hence res_queriesmatch() returns 0.
10047c478bd9Sstevel@tonic-gate */
10057c478bd9Sstevel@tonic-gate DprintQ(statp->options & RES_DEBUG,
10067c478bd9Sstevel@tonic-gate (stdout, "server rejected query with EDNS0:\n"),
10077c478bd9Sstevel@tonic-gate ans, (resplen > anssiz) ? anssiz : resplen);
10087c478bd9Sstevel@tonic-gate /* record the error */
10097c478bd9Sstevel@tonic-gate statp->_flags |= RES_F_EDNS0ERR;
10107c478bd9Sstevel@tonic-gate res_nclose(statp);
10117c478bd9Sstevel@tonic-gate return (0);
10127c478bd9Sstevel@tonic-gate }
10137c478bd9Sstevel@tonic-gate #endif
10147c478bd9Sstevel@tonic-gate if (!(statp->options & RES_INSECURE2) &&
10157c478bd9Sstevel@tonic-gate !res_queriesmatch(buf, buf + buflen,
10167c478bd9Sstevel@tonic-gate ans, ans + anssiz)) {
10177c478bd9Sstevel@tonic-gate /*
10187c478bd9Sstevel@tonic-gate * response contains wrong query? ignore it.
10197c478bd9Sstevel@tonic-gate * XXX - potential security hazard could
10207c478bd9Sstevel@tonic-gate * be detected here.
10217c478bd9Sstevel@tonic-gate */
10227c478bd9Sstevel@tonic-gate DprintQ((statp->options & RES_DEBUG) ||
10237c478bd9Sstevel@tonic-gate (statp->pfcode & RES_PRF_REPLY),
10247c478bd9Sstevel@tonic-gate (stdout, ";; wrong query name:\n"),
10257c478bd9Sstevel@tonic-gate ans, (resplen > anssiz) ? anssiz : resplen);
10267c478bd9Sstevel@tonic-gate goto wait;
10277c478bd9Sstevel@tonic-gate }
10287c478bd9Sstevel@tonic-gate if (anhp->rcode == SERVFAIL ||
10297c478bd9Sstevel@tonic-gate anhp->rcode == NOTIMP ||
10307c478bd9Sstevel@tonic-gate anhp->rcode == REFUSED) {
10317c478bd9Sstevel@tonic-gate DprintQ(statp->options & RES_DEBUG,
10327c478bd9Sstevel@tonic-gate (stdout, "server rejected query:\n"),
10337c478bd9Sstevel@tonic-gate ans, (resplen > anssiz) ? anssiz : resplen);
10347c478bd9Sstevel@tonic-gate res_nclose(statp);
10357c478bd9Sstevel@tonic-gate /* don't retry if called from dig */
10367c478bd9Sstevel@tonic-gate if (!statp->pfcode)
10377c478bd9Sstevel@tonic-gate return (0);
10387c478bd9Sstevel@tonic-gate }
10397c478bd9Sstevel@tonic-gate if (!(statp->options & RES_IGNTC) && anhp->tc) {
10407c478bd9Sstevel@tonic-gate /*
10417c478bd9Sstevel@tonic-gate * To get the rest of answer,
10427c478bd9Sstevel@tonic-gate * use TCP with same server.
10437c478bd9Sstevel@tonic-gate */
10447c478bd9Sstevel@tonic-gate Dprint(statp->options & RES_DEBUG,
10457c478bd9Sstevel@tonic-gate (stdout, ";; truncated answer\n"));
10467c478bd9Sstevel@tonic-gate *v_circuit = 1;
10477c478bd9Sstevel@tonic-gate res_nclose(statp);
10487c478bd9Sstevel@tonic-gate return (1);
10497c478bd9Sstevel@tonic-gate }
10507c478bd9Sstevel@tonic-gate /*
10517c478bd9Sstevel@tonic-gate * All is well, or the error is fatal. Signal that the
10527c478bd9Sstevel@tonic-gate * next nameserver ought not be tried.
10537c478bd9Sstevel@tonic-gate */
10547c478bd9Sstevel@tonic-gate return (resplen);
10557c478bd9Sstevel@tonic-gate }
10567c478bd9Sstevel@tonic-gate
10577c478bd9Sstevel@tonic-gate static void
Aerror(const res_state statp,FILE * file,const char * string,int error,const struct sockaddr * address,int alen)10587c478bd9Sstevel@tonic-gate Aerror(const res_state statp, FILE *file, const char *string, int error,
10597c478bd9Sstevel@tonic-gate const struct sockaddr *address, int alen)
10607c478bd9Sstevel@tonic-gate {
10617c478bd9Sstevel@tonic-gate int save = errno;
10627c478bd9Sstevel@tonic-gate char hbuf[NI_MAXHOST];
10637c478bd9Sstevel@tonic-gate char sbuf[NI_MAXSERV];
10647c478bd9Sstevel@tonic-gate
10657c478bd9Sstevel@tonic-gate alen = alen;
10667c478bd9Sstevel@tonic-gate
10679525b14bSRao Shoaib if ((statp->options & RES_DEBUG) != 0U) {
10687c478bd9Sstevel@tonic-gate if (getnameinfo(address, alen, hbuf, sizeof(hbuf),
10697c478bd9Sstevel@tonic-gate sbuf, sizeof(sbuf), niflags)) {
10707c478bd9Sstevel@tonic-gate strncpy(hbuf, "?", sizeof(hbuf) - 1);
10717c478bd9Sstevel@tonic-gate hbuf[sizeof(hbuf) - 1] = '\0';
10727c478bd9Sstevel@tonic-gate strncpy(sbuf, "?", sizeof(sbuf) - 1);
10737c478bd9Sstevel@tonic-gate sbuf[sizeof(sbuf) - 1] = '\0';
10747c478bd9Sstevel@tonic-gate }
10757c478bd9Sstevel@tonic-gate fprintf(file, "res_send: %s ([%s].%s): %s\n",
10767c478bd9Sstevel@tonic-gate string, hbuf, sbuf, strerror(error));
10777c478bd9Sstevel@tonic-gate }
10787c478bd9Sstevel@tonic-gate errno = save;
10797c478bd9Sstevel@tonic-gate }
10807c478bd9Sstevel@tonic-gate
10817c478bd9Sstevel@tonic-gate static void
Perror(const res_state statp,FILE * file,const char * string,int error)10827c478bd9Sstevel@tonic-gate Perror(const res_state statp, FILE *file, const char *string, int error) {
10837c478bd9Sstevel@tonic-gate int save = errno;
10847c478bd9Sstevel@tonic-gate
10859525b14bSRao Shoaib if ((statp->options & RES_DEBUG) != 0U)
10867c478bd9Sstevel@tonic-gate fprintf(file, "res_send: %s: %s\n",
10877c478bd9Sstevel@tonic-gate string, strerror(error));
10887c478bd9Sstevel@tonic-gate errno = save;
10897c478bd9Sstevel@tonic-gate }
10907c478bd9Sstevel@tonic-gate
10917c478bd9Sstevel@tonic-gate static int
sock_eq(struct sockaddr * a,struct sockaddr * b)10927c478bd9Sstevel@tonic-gate sock_eq(struct sockaddr *a, struct sockaddr *b) {
10937c478bd9Sstevel@tonic-gate struct sockaddr_in *a4, *b4;
10947c478bd9Sstevel@tonic-gate struct sockaddr_in6 *a6, *b6;
10957c478bd9Sstevel@tonic-gate
10967c478bd9Sstevel@tonic-gate if (a->sa_family != b->sa_family)
10977c478bd9Sstevel@tonic-gate return 0;
10987c478bd9Sstevel@tonic-gate switch (a->sa_family) {
10997c478bd9Sstevel@tonic-gate case AF_INET:
11007c478bd9Sstevel@tonic-gate a4 = (struct sockaddr_in *)a;
11017c478bd9Sstevel@tonic-gate b4 = (struct sockaddr_in *)b;
11027c478bd9Sstevel@tonic-gate return a4->sin_port == b4->sin_port &&
11037c478bd9Sstevel@tonic-gate a4->sin_addr.s_addr == b4->sin_addr.s_addr;
11047c478bd9Sstevel@tonic-gate case AF_INET6:
11057c478bd9Sstevel@tonic-gate a6 = (struct sockaddr_in6 *)a;
11067c478bd9Sstevel@tonic-gate b6 = (struct sockaddr_in6 *)b;
11077c478bd9Sstevel@tonic-gate return a6->sin6_port == b6->sin6_port &&
11087c478bd9Sstevel@tonic-gate #ifdef HAVE_SIN6_SCOPE_ID
11097c478bd9Sstevel@tonic-gate a6->sin6_scope_id == b6->sin6_scope_id &&
11107c478bd9Sstevel@tonic-gate #endif
11117c478bd9Sstevel@tonic-gate IN6_ARE_ADDR_EQUAL(&a6->sin6_addr, &b6->sin6_addr);
11127c478bd9Sstevel@tonic-gate default:
11137c478bd9Sstevel@tonic-gate return 0;
11147c478bd9Sstevel@tonic-gate }
11157c478bd9Sstevel@tonic-gate }
11167c478bd9Sstevel@tonic-gate
11179525b14bSRao Shoaib #if defined(NEED_PSELECT) && !defined(USE_POLL)
11187c478bd9Sstevel@tonic-gate /* XXX needs to move to the porting library. */
11197c478bd9Sstevel@tonic-gate static int
pselect(int nfds,void * rfds,void * wfds,void * efds,struct timespec * tsp,const sigset_t * sigmask)11207c478bd9Sstevel@tonic-gate pselect(int nfds, void *rfds, void *wfds, void *efds,
11217c478bd9Sstevel@tonic-gate struct timespec *tsp, const sigset_t *sigmask)
11227c478bd9Sstevel@tonic-gate {
11237c478bd9Sstevel@tonic-gate struct timeval tv, *tvp;
11247c478bd9Sstevel@tonic-gate sigset_t sigs;
11257c478bd9Sstevel@tonic-gate int n;
11267c478bd9Sstevel@tonic-gate
11277c478bd9Sstevel@tonic-gate if (tsp) {
11287c478bd9Sstevel@tonic-gate tvp = &tv;
11297c478bd9Sstevel@tonic-gate tv = evTimeVal(*tsp);
11307c478bd9Sstevel@tonic-gate } else
11317c478bd9Sstevel@tonic-gate tvp = NULL;
11327c478bd9Sstevel@tonic-gate if (sigmask)
11337c478bd9Sstevel@tonic-gate sigprocmask(SIG_SETMASK, sigmask, &sigs);
11347c478bd9Sstevel@tonic-gate n = select(nfds, rfds, wfds, efds, tvp);
11357c478bd9Sstevel@tonic-gate if (sigmask)
11367c478bd9Sstevel@tonic-gate sigprocmask(SIG_SETMASK, &sigs, NULL);
11377c478bd9Sstevel@tonic-gate if (tsp)
11387c478bd9Sstevel@tonic-gate *tsp = evTimeSpec(tv);
11397c478bd9Sstevel@tonic-gate return (n);
11407c478bd9Sstevel@tonic-gate }
11417c478bd9Sstevel@tonic-gate #endif
1142