17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57257d1b4Sraf * Common Development and Distribution License (the "License"). 67257d1b4Sraf * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 21e8031f0aSraf 227c478bd9Sstevel@tonic-gate /* 23*bd0e95e6SGary Mills * Copyright 2015 Gary Mills 247257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 257c478bd9Sstevel@tonic-gate * Use is subject to license terms. 267c478bd9Sstevel@tonic-gate */ 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 297c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate /* 327c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 337c478bd9Sstevel@tonic-gate * The Regents of the University of California 347c478bd9Sstevel@tonic-gate * All Rights Reserved 357c478bd9Sstevel@tonic-gate * 367c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 377c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 387c478bd9Sstevel@tonic-gate * contributors. 397c478bd9Sstevel@tonic-gate */ 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate /* 427c478bd9Sstevel@tonic-gate * Send query to name server and wait for reply. 437c478bd9Sstevel@tonic-gate */ 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate #include <sys/param.h> 467c478bd9Sstevel@tonic-gate #include <sys/time.h> 477c478bd9Sstevel@tonic-gate #include <sys/socket.h> 487c478bd9Sstevel@tonic-gate #include <sys/uio.h> 497c478bd9Sstevel@tonic-gate #include <sys/stat.h> 507c478bd9Sstevel@tonic-gate #include <netinet/in.h> 517c478bd9Sstevel@tonic-gate #include <stdio.h> 52*bd0e95e6SGary Mills #include <string.h> 53*bd0e95e6SGary Mills #include <unistd.h> 547c478bd9Sstevel@tonic-gate #include <errno.h> 557c478bd9Sstevel@tonic-gate #include <arpa/nameser.h> 56*bd0e95e6SGary Mills #include <arpa/inet.h> 577c478bd9Sstevel@tonic-gate #include <resolv.h> 58*bd0e95e6SGary Mills #include "crossl.h" 597c478bd9Sstevel@tonic-gate 60*bd0e95e6SGary Mills /* 61*bd0e95e6SGary Mills * Undocumented external function in libsocket 62*bd0e95e6SGary Mills */ 63*bd0e95e6SGary Mills extern int 64*bd0e95e6SGary Mills _socket(int, int, int); 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate static int s = -1; /* socket used for communications */ 67*bd0e95e6SGary Mills #if BSD >= 43 687c478bd9Sstevel@tonic-gate static struct sockaddr no_addr; 69*bd0e95e6SGary Mills #endif /* BSD */ 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate #ifndef FD_SET 737c478bd9Sstevel@tonic-gate #define NFDBITS 32 747c478bd9Sstevel@tonic-gate #define FD_SETSIZE 32 757c478bd9Sstevel@tonic-gate #define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS))) 767c478bd9Sstevel@tonic-gate #define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS))) 777c478bd9Sstevel@tonic-gate #define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS))) 787c478bd9Sstevel@tonic-gate #ifdef SYSV 79*bd0e95e6SGary Mills #define FD_ZERO(p) (void) memset((void *)(p), 0, sizeof (*(p))) 807c478bd9Sstevel@tonic-gate #else 817c478bd9Sstevel@tonic-gate #define FD_ZERO(p) bzero((char *)(p), sizeof (*(p))) 827c478bd9Sstevel@tonic-gate #endif 837c478bd9Sstevel@tonic-gate #endif 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate /* 867c478bd9Sstevel@tonic-gate * 1247019: Kludge to time out quickly if there is no /etc/resolv.conf 877c478bd9Sstevel@tonic-gate * and a TCP connection to the local DNS server fails. 887c478bd9Sstevel@tonic-gate */ 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate static int _confcheck() 917c478bd9Sstevel@tonic-gate { 927c478bd9Sstevel@tonic-gate int ns; 937c478bd9Sstevel@tonic-gate struct stat rc_stat; 947c478bd9Sstevel@tonic-gate struct sockaddr_in ns_sin; 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate /* First, we check to see if /etc/resolv.conf exists. 987c478bd9Sstevel@tonic-gate * If it doesn't, then localhost is mostlikely to be 997c478bd9Sstevel@tonic-gate * the nameserver. 1007c478bd9Sstevel@tonic-gate */ 1017c478bd9Sstevel@tonic-gate if (stat(_PATH_RESCONF, &rc_stat) == -1 && errno == ENOENT) { 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate /* Next, we check to see if _res.nsaddr is set to loopback. 1047c478bd9Sstevel@tonic-gate * If it isn't, it has been altered by the application 1057c478bd9Sstevel@tonic-gate * explicitly and we then want to bail with success. 1067c478bd9Sstevel@tonic-gate */ 1077c478bd9Sstevel@tonic-gate if (_res.nsaddr.sin_addr.S_un.S_addr == htonl(INADDR_LOOPBACK)) { 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate /* Lastly, we try to connect to the TCP port of the 1107c478bd9Sstevel@tonic-gate * nameserver. If this fails, then we know that 1117c478bd9Sstevel@tonic-gate * DNS is misconfigured and we can quickly exit. 1127c478bd9Sstevel@tonic-gate */ 1137c478bd9Sstevel@tonic-gate ns = socket(AF_INET, SOCK_STREAM, 0); 1147c478bd9Sstevel@tonic-gate IN_SET_LOOPBACK_ADDR(&ns_sin); 1157c478bd9Sstevel@tonic-gate ns_sin.sin_port = htons(NAMESERVER_PORT); 1167c478bd9Sstevel@tonic-gate if (connect(ns, (struct sockaddr *) &ns_sin, 1177c478bd9Sstevel@tonic-gate sizeof ns_sin) == -1) { 1187c478bd9Sstevel@tonic-gate close(ns); 1197c478bd9Sstevel@tonic-gate return(-1); 1207c478bd9Sstevel@tonic-gate } 1217c478bd9Sstevel@tonic-gate else { 1227c478bd9Sstevel@tonic-gate close(ns); 1237c478bd9Sstevel@tonic-gate return(0); 1247c478bd9Sstevel@tonic-gate } 1257c478bd9Sstevel@tonic-gate } 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate return(0); 1287c478bd9Sstevel@tonic-gate } 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate return (0); 1317c478bd9Sstevel@tonic-gate } 1327c478bd9Sstevel@tonic-gate 1336a1c6faaSanay int 1347c478bd9Sstevel@tonic-gate res_send(buf, buflen, answer, anslen) 1357c478bd9Sstevel@tonic-gate char *buf; 1367c478bd9Sstevel@tonic-gate int buflen; 1377c478bd9Sstevel@tonic-gate char *answer; 1387c478bd9Sstevel@tonic-gate int anslen; 1397c478bd9Sstevel@tonic-gate { 1407c478bd9Sstevel@tonic-gate register int n; 1417c478bd9Sstevel@tonic-gate int try, v_circuit, resplen, ns; 142*bd0e95e6SGary Mills int gotsomewhere = 0; 143*bd0e95e6SGary Mills #if BSD >= 43 144*bd0e95e6SGary Mills int connected = 0; 145*bd0e95e6SGary Mills #endif /* BSD */ 1467c478bd9Sstevel@tonic-gate int connreset = 0; 1477c478bd9Sstevel@tonic-gate u_short id, len; 1487c478bd9Sstevel@tonic-gate char *cp; 1497c478bd9Sstevel@tonic-gate fd_set dsmask; 1507c478bd9Sstevel@tonic-gate struct timeval timeout; 1517c478bd9Sstevel@tonic-gate HEADER *hp = (HEADER *) buf; 1527c478bd9Sstevel@tonic-gate HEADER *anhp = (HEADER *) answer; 1537c478bd9Sstevel@tonic-gate struct iovec iov[2]; 1547c478bd9Sstevel@tonic-gate int terrno = ETIMEDOUT; 1557c478bd9Sstevel@tonic-gate char junk[512]; 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate #ifdef DEBUG 1587c478bd9Sstevel@tonic-gate if (_res.options & RES_DEBUG) { 1597c478bd9Sstevel@tonic-gate printf("res_send()\n"); 1607c478bd9Sstevel@tonic-gate p_query(buf); 1617c478bd9Sstevel@tonic-gate } 1626a1c6faaSanay #endif 1637c478bd9Sstevel@tonic-gate if (!(_res.options & RES_INIT)) 1647c478bd9Sstevel@tonic-gate if (res_init() == -1) { 1657c478bd9Sstevel@tonic-gate return (-1); 1667c478bd9Sstevel@tonic-gate } 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate /* 1247019: Check to see if we can bailout quickly. */ 1697c478bd9Sstevel@tonic-gate if (_confcheck() == -1) 1707c478bd9Sstevel@tonic-gate return(-1); 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate v_circuit = (_res.options & RES_USEVC) || buflen > PACKETSZ; 1737c478bd9Sstevel@tonic-gate id = hp->id; 1747c478bd9Sstevel@tonic-gate /* 1757c478bd9Sstevel@tonic-gate * Send request, RETRY times, or until successful 1767c478bd9Sstevel@tonic-gate */ 1777c478bd9Sstevel@tonic-gate for (try = 0; try < _res.retry; try++) { 1787c478bd9Sstevel@tonic-gate for (ns = 0; ns < _res.nscount; ns++) { 1797c478bd9Sstevel@tonic-gate #ifdef DEBUG 1807c478bd9Sstevel@tonic-gate if (_res.options & RES_DEBUG) 1817c478bd9Sstevel@tonic-gate printf("Querying server (# %d) address = %s\n", 1827c478bd9Sstevel@tonic-gate ns+1, inet_ntoa(_res.nsaddr_list[ns].sin_addr)); 1836a1c6faaSanay #endif 1847c478bd9Sstevel@tonic-gate usevc: 1857c478bd9Sstevel@tonic-gate if (v_circuit) { 1867c478bd9Sstevel@tonic-gate int truncated = 0; 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate /* 1897c478bd9Sstevel@tonic-gate * Use virtual circuit; 1907c478bd9Sstevel@tonic-gate * at most one attempt per server. 1917c478bd9Sstevel@tonic-gate */ 1927c478bd9Sstevel@tonic-gate try = _res.retry; 1937c478bd9Sstevel@tonic-gate if (s < 0) { 1947c478bd9Sstevel@tonic-gate s = _socket(AF_INET, SOCK_STREAM, 0); 1957c478bd9Sstevel@tonic-gate if (s < 0) { 1967c478bd9Sstevel@tonic-gate terrno = errno; 1977c478bd9Sstevel@tonic-gate #ifdef DEBUG 1987c478bd9Sstevel@tonic-gate if (_res.options & RES_DEBUG) { 1997c478bd9Sstevel@tonic-gate perror("socket (vc) failed"); 2007c478bd9Sstevel@tonic-gate } 2016a1c6faaSanay #endif 2027c478bd9Sstevel@tonic-gate continue; 2037c478bd9Sstevel@tonic-gate } 2047c478bd9Sstevel@tonic-gate if (connect(s, (struct sockaddr *) &_res.nsaddr_list[ns], 2057c478bd9Sstevel@tonic-gate sizeof (struct sockaddr)) < 0) { 2067c478bd9Sstevel@tonic-gate terrno = errno; 2077c478bd9Sstevel@tonic-gate #ifdef DEBUG 2087c478bd9Sstevel@tonic-gate if (_res.options & RES_DEBUG) { 2097c478bd9Sstevel@tonic-gate perror("connect failed"); 2107c478bd9Sstevel@tonic-gate } 2116a1c6faaSanay #endif 2127c478bd9Sstevel@tonic-gate (void) close(s); 2137c478bd9Sstevel@tonic-gate s = -1; 2147c478bd9Sstevel@tonic-gate continue; 2157c478bd9Sstevel@tonic-gate } 2167c478bd9Sstevel@tonic-gate } 2177c478bd9Sstevel@tonic-gate /* 2187c478bd9Sstevel@tonic-gate * Send length & message 2197c478bd9Sstevel@tonic-gate */ 2207c478bd9Sstevel@tonic-gate len = htons((u_short)buflen); 2217c478bd9Sstevel@tonic-gate iov[0].iov_base = (caddr_t)&len; 2227c478bd9Sstevel@tonic-gate iov[0].iov_len = sizeof (len); 2237c478bd9Sstevel@tonic-gate iov[1].iov_base = buf; 2247c478bd9Sstevel@tonic-gate iov[1].iov_len = buflen; 2257c478bd9Sstevel@tonic-gate if (writev(s, iov, 2) != sizeof (len) + 2267c478bd9Sstevel@tonic-gate buflen) { 2277c478bd9Sstevel@tonic-gate terrno = errno; 2287c478bd9Sstevel@tonic-gate #ifdef DEBUG 2297c478bd9Sstevel@tonic-gate if (_res.options & RES_DEBUG) 2307c478bd9Sstevel@tonic-gate perror("write failed"); 2316a1c6faaSanay #endif 2327c478bd9Sstevel@tonic-gate (void) close(s); 2337c478bd9Sstevel@tonic-gate s = -1; 2347c478bd9Sstevel@tonic-gate continue; 2357c478bd9Sstevel@tonic-gate } 2367c478bd9Sstevel@tonic-gate /* 2377c478bd9Sstevel@tonic-gate * Receive length & response 2387c478bd9Sstevel@tonic-gate */ 2397c478bd9Sstevel@tonic-gate cp = answer; 2407c478bd9Sstevel@tonic-gate len = sizeof (short); 2417c478bd9Sstevel@tonic-gate while (len != 0 && (n = read 2427c478bd9Sstevel@tonic-gate (s, (char *)cp, (int)len)) > 0) { 2437c478bd9Sstevel@tonic-gate cp += n; 2447c478bd9Sstevel@tonic-gate len -= n; 2457c478bd9Sstevel@tonic-gate } 2467c478bd9Sstevel@tonic-gate if (n <= 0) { 2477c478bd9Sstevel@tonic-gate terrno = errno; 2487c478bd9Sstevel@tonic-gate #ifdef DEBUG 2497c478bd9Sstevel@tonic-gate if (_res.options & RES_DEBUG) 2507c478bd9Sstevel@tonic-gate perror("read failed"); 2516a1c6faaSanay #endif 2527c478bd9Sstevel@tonic-gate (void) close(s); 2537c478bd9Sstevel@tonic-gate s = -1; 2547c478bd9Sstevel@tonic-gate /* 2557c478bd9Sstevel@tonic-gate * A long running process might get its TCP 2567c478bd9Sstevel@tonic-gate * connection reset if the remote server was 2577c478bd9Sstevel@tonic-gate * restarted. Requery the server instead of 2587c478bd9Sstevel@tonic-gate * trying a new one. When there is only one 2597c478bd9Sstevel@tonic-gate * server, this means that a query might work 2607c478bd9Sstevel@tonic-gate * instead of failing. We only allow one reset 2617c478bd9Sstevel@tonic-gate * per query to prevent looping. 2627c478bd9Sstevel@tonic-gate */ 2637c478bd9Sstevel@tonic-gate if (terrno == ECONNRESET && 2647c478bd9Sstevel@tonic-gate !connreset) { 2657c478bd9Sstevel@tonic-gate connreset = 1; 2667c478bd9Sstevel@tonic-gate ns--; 2677c478bd9Sstevel@tonic-gate } 2687c478bd9Sstevel@tonic-gate continue; 2697c478bd9Sstevel@tonic-gate } 2707c478bd9Sstevel@tonic-gate cp = answer; 2717c478bd9Sstevel@tonic-gate if ((resplen = ntohs(*(u_short *)cp)) > 2727c478bd9Sstevel@tonic-gate anslen) { 2737c478bd9Sstevel@tonic-gate #ifdef DEBUG 2747c478bd9Sstevel@tonic-gate if (_res.options & RES_DEBUG) 2757c478bd9Sstevel@tonic-gate fprintf(stderr, 2767c478bd9Sstevel@tonic-gate "response truncated\n"); 2776a1c6faaSanay #endif 2787c478bd9Sstevel@tonic-gate len = anslen; 2797c478bd9Sstevel@tonic-gate truncated = 1; 2807c478bd9Sstevel@tonic-gate } else 2817c478bd9Sstevel@tonic-gate len = resplen; 2827c478bd9Sstevel@tonic-gate while (len != 0 && 2837c478bd9Sstevel@tonic-gate (n = read(s, (char *)cp, 2847c478bd9Sstevel@tonic-gate (int)len)) > 0) { 2857c478bd9Sstevel@tonic-gate cp += n; 2867c478bd9Sstevel@tonic-gate len -= n; 2877c478bd9Sstevel@tonic-gate } 2887c478bd9Sstevel@tonic-gate if (n <= 0) { 2897c478bd9Sstevel@tonic-gate terrno = errno; 2907c478bd9Sstevel@tonic-gate #ifdef DEBUG 2917c478bd9Sstevel@tonic-gate if (_res.options & RES_DEBUG) 2927c478bd9Sstevel@tonic-gate perror("read failed"); 2936a1c6faaSanay #endif 2947c478bd9Sstevel@tonic-gate (void) close(s); 2957c478bd9Sstevel@tonic-gate s = -1; 2967c478bd9Sstevel@tonic-gate continue; 2977c478bd9Sstevel@tonic-gate } 2987c478bd9Sstevel@tonic-gate if (truncated) { 2997c478bd9Sstevel@tonic-gate /* 3007c478bd9Sstevel@tonic-gate * Flush rest of answer 3017c478bd9Sstevel@tonic-gate * so connection stays in synch. 3027c478bd9Sstevel@tonic-gate */ 3037c478bd9Sstevel@tonic-gate anhp->tc = 1; 3047c478bd9Sstevel@tonic-gate len = resplen - anslen; 3057c478bd9Sstevel@tonic-gate /* 3067c478bd9Sstevel@tonic-gate * set the value of resplen to anslen, 3077c478bd9Sstevel@tonic-gate * this is done because the caller 3087c478bd9Sstevel@tonic-gate * assumes resplen contains the size of 3097c478bd9Sstevel@tonic-gate * message read into the "answer" buffer 3107c478bd9Sstevel@tonic-gate * passed in. 3117c478bd9Sstevel@tonic-gate */ 3127c478bd9Sstevel@tonic-gate resplen = anslen; 3137c478bd9Sstevel@tonic-gate 3147c478bd9Sstevel@tonic-gate while (len != 0) { 3157c478bd9Sstevel@tonic-gate n = (len > sizeof (junk) ? 3167c478bd9Sstevel@tonic-gate sizeof (junk) : len); 3177c478bd9Sstevel@tonic-gate if ((n = read(s, junk, n)) > 0) 3187c478bd9Sstevel@tonic-gate len -= n; 3197c478bd9Sstevel@tonic-gate else 3207c478bd9Sstevel@tonic-gate break; 3217c478bd9Sstevel@tonic-gate } 3227c478bd9Sstevel@tonic-gate } 3237c478bd9Sstevel@tonic-gate } else { 3247c478bd9Sstevel@tonic-gate /* 3257c478bd9Sstevel@tonic-gate * Use datagrams. 3267c478bd9Sstevel@tonic-gate */ 3277c478bd9Sstevel@tonic-gate if (s < 0) { 3287c478bd9Sstevel@tonic-gate s = _socket(AF_INET, SOCK_DGRAM, 0); 3297c478bd9Sstevel@tonic-gate if (s < 0) { 3307c478bd9Sstevel@tonic-gate terrno = errno; 3317c478bd9Sstevel@tonic-gate #ifdef DEBUG 3327c478bd9Sstevel@tonic-gate if (_res.options & RES_DEBUG) { 3337c478bd9Sstevel@tonic-gate perror("socket (dg) failed"); 3347c478bd9Sstevel@tonic-gate } 3356a1c6faaSanay #endif 3367c478bd9Sstevel@tonic-gate continue; 3377c478bd9Sstevel@tonic-gate } 3387c478bd9Sstevel@tonic-gate } 3397c478bd9Sstevel@tonic-gate #if BSD >= 43 3407c478bd9Sstevel@tonic-gate /* 3417c478bd9Sstevel@tonic-gate * I'm tired of answering this question, so: 3427c478bd9Sstevel@tonic-gate * On a 4.3BSD+ machine (client and server, 3437c478bd9Sstevel@tonic-gate * actually), sending to a nameserver datagram 3447c478bd9Sstevel@tonic-gate * port with no nameserver will cause an 3457c478bd9Sstevel@tonic-gate * ICMP port unreachable message to be returned. 3467c478bd9Sstevel@tonic-gate * If our datagram socket is "connected" to the 3477c478bd9Sstevel@tonic-gate * server, we get an ECONNREFUSED error on the next 3487c478bd9Sstevel@tonic-gate * socket operation, and select returns if the 3497c478bd9Sstevel@tonic-gate * error message is received. We can thus detect 3507c478bd9Sstevel@tonic-gate * the absence of a nameserver without timing out. 3517c478bd9Sstevel@tonic-gate * If we have sent queries to at least two servers, 3527c478bd9Sstevel@tonic-gate * however, we don't want to remain connected, 3537c478bd9Sstevel@tonic-gate * as we wish to receive answers from the first 3547c478bd9Sstevel@tonic-gate * server to respond. 3557c478bd9Sstevel@tonic-gate */ 3567c478bd9Sstevel@tonic-gate if (_res.nscount == 1 || 3577c478bd9Sstevel@tonic-gate (try == 0 && ns == 0)) { 3587c478bd9Sstevel@tonic-gate /* 3597c478bd9Sstevel@tonic-gate * Don't use connect if we might 3607c478bd9Sstevel@tonic-gate * still receive a response 3617c478bd9Sstevel@tonic-gate * from another server. 3627c478bd9Sstevel@tonic-gate */ 3637c478bd9Sstevel@tonic-gate if (connected == 0) { 3647c478bd9Sstevel@tonic-gate if (connect(s, 3657c478bd9Sstevel@tonic-gate (struct sockaddr *) &_res.nsaddr_list[ns], 3667c478bd9Sstevel@tonic-gate sizeof (struct sockaddr)) < 0) { 3677c478bd9Sstevel@tonic-gate #ifdef DEBUG 3687c478bd9Sstevel@tonic-gate if (_res.options & 3697c478bd9Sstevel@tonic-gate RES_DEBUG) { 3707c478bd9Sstevel@tonic-gate perror("connect"); 3717c478bd9Sstevel@tonic-gate } 3726a1c6faaSanay #endif 3737c478bd9Sstevel@tonic-gate continue; 3747c478bd9Sstevel@tonic-gate } 3757c478bd9Sstevel@tonic-gate connected = 1; 3767c478bd9Sstevel@tonic-gate } 3777c478bd9Sstevel@tonic-gate if (send(s, buf, buflen, 0) != buflen) { 3787c478bd9Sstevel@tonic-gate #ifdef DEBUG 3797c478bd9Sstevel@tonic-gate if (_res.options & RES_DEBUG) 3807c478bd9Sstevel@tonic-gate perror("send"); 3816a1c6faaSanay #endif 3827c478bd9Sstevel@tonic-gate continue; 3837c478bd9Sstevel@tonic-gate } 3847c478bd9Sstevel@tonic-gate } else { 3857c478bd9Sstevel@tonic-gate /* 3867c478bd9Sstevel@tonic-gate * Disconnect if we want to listen for 3877c478bd9Sstevel@tonic-gate * responses from more than one server. 3887c478bd9Sstevel@tonic-gate */ 3897c478bd9Sstevel@tonic-gate if (connected) { 3907c478bd9Sstevel@tonic-gate (void) connect(s, &no_addr, 3917c478bd9Sstevel@tonic-gate sizeof (no_addr)); 3927c478bd9Sstevel@tonic-gate connected = 0; 3937c478bd9Sstevel@tonic-gate } 3946a1c6faaSanay #endif /* BSD */ 3957c478bd9Sstevel@tonic-gate if (sendto(s, buf, buflen, 0, 3967c478bd9Sstevel@tonic-gate (struct sockaddr *) &_res.nsaddr_list[ns], 3977c478bd9Sstevel@tonic-gate sizeof (struct sockaddr)) != buflen) { 3987c478bd9Sstevel@tonic-gate #ifdef DEBUG 3997c478bd9Sstevel@tonic-gate if (_res.options & RES_DEBUG) 4007c478bd9Sstevel@tonic-gate perror("sendto"); 4016a1c6faaSanay #endif 4027c478bd9Sstevel@tonic-gate continue; 4037c478bd9Sstevel@tonic-gate } 4047c478bd9Sstevel@tonic-gate #if BSD >= 43 4057c478bd9Sstevel@tonic-gate } 4067c478bd9Sstevel@tonic-gate #endif 4077c478bd9Sstevel@tonic-gate 4087c478bd9Sstevel@tonic-gate /* 4097c478bd9Sstevel@tonic-gate * Wait for reply 4107c478bd9Sstevel@tonic-gate */ 4117c478bd9Sstevel@tonic-gate timeout.tv_sec = (_res.retrans << try); 4127c478bd9Sstevel@tonic-gate if (try > 0) 4137c478bd9Sstevel@tonic-gate timeout.tv_sec /= _res.nscount; 4147c478bd9Sstevel@tonic-gate if (timeout.tv_sec <= 0) 4157c478bd9Sstevel@tonic-gate timeout.tv_sec = 1; 4167c478bd9Sstevel@tonic-gate timeout.tv_usec = 0; 4177c478bd9Sstevel@tonic-gate wait: 4187c478bd9Sstevel@tonic-gate FD_ZERO(&dsmask); 4197c478bd9Sstevel@tonic-gate FD_SET(s, &dsmask); 4207c478bd9Sstevel@tonic-gate n = select(s+1, &dsmask, (fd_set *)NULL, 4217c478bd9Sstevel@tonic-gate (fd_set *)NULL, &timeout); 4227c478bd9Sstevel@tonic-gate if (n < 0) { 4237c478bd9Sstevel@tonic-gate #ifdef DEBUG 4247c478bd9Sstevel@tonic-gate if (_res.options & RES_DEBUG) 4257c478bd9Sstevel@tonic-gate perror("select"); 4266a1c6faaSanay #endif 4277c478bd9Sstevel@tonic-gate continue; 4287c478bd9Sstevel@tonic-gate } 4297c478bd9Sstevel@tonic-gate if (n == 0) { 4307c478bd9Sstevel@tonic-gate /* 4317c478bd9Sstevel@tonic-gate * timeout 4327c478bd9Sstevel@tonic-gate */ 4337c478bd9Sstevel@tonic-gate #ifdef DEBUG 4347c478bd9Sstevel@tonic-gate if (_res.options & RES_DEBUG) 4357c478bd9Sstevel@tonic-gate printf("timeout\n"); 4366a1c6faaSanay #endif 4377c478bd9Sstevel@tonic-gate #if BSD >= 43 4387c478bd9Sstevel@tonic-gate gotsomewhere = 1; 4397c478bd9Sstevel@tonic-gate #endif 4407c478bd9Sstevel@tonic-gate continue; 4417c478bd9Sstevel@tonic-gate } 4427c478bd9Sstevel@tonic-gate if ((resplen = recv(s, answer, anslen, 0)) 4437c478bd9Sstevel@tonic-gate <= 0) { 4447c478bd9Sstevel@tonic-gate #ifdef DEBUG 4457c478bd9Sstevel@tonic-gate if (_res.options & RES_DEBUG) 4467c478bd9Sstevel@tonic-gate perror("recvfrom"); 4476a1c6faaSanay #endif 4487c478bd9Sstevel@tonic-gate continue; 4497c478bd9Sstevel@tonic-gate } 4507c478bd9Sstevel@tonic-gate gotsomewhere = 1; 4517c478bd9Sstevel@tonic-gate if (id != anhp->id) { 4527c478bd9Sstevel@tonic-gate /* 4537c478bd9Sstevel@tonic-gate * response from old query, ignore it 4547c478bd9Sstevel@tonic-gate */ 4557c478bd9Sstevel@tonic-gate #ifdef DEBUG 4567c478bd9Sstevel@tonic-gate if (_res.options & RES_DEBUG) { 4577c478bd9Sstevel@tonic-gate printf("old answer:\n"); 4587c478bd9Sstevel@tonic-gate p_query(answer); 4597c478bd9Sstevel@tonic-gate } 4606a1c6faaSanay #endif 4617c478bd9Sstevel@tonic-gate goto wait; 4627c478bd9Sstevel@tonic-gate } 4637c478bd9Sstevel@tonic-gate if (!(_res.options & RES_IGNTC) && anhp->tc) { 4647c478bd9Sstevel@tonic-gate /* 4657c478bd9Sstevel@tonic-gate * get rest of answer; 4667c478bd9Sstevel@tonic-gate * use TCP with same server. 4677c478bd9Sstevel@tonic-gate */ 4687c478bd9Sstevel@tonic-gate #ifdef DEBUG 4697c478bd9Sstevel@tonic-gate if (_res.options & RES_DEBUG) 4707c478bd9Sstevel@tonic-gate printf("truncated answer\n"); 4716a1c6faaSanay #endif 4727c478bd9Sstevel@tonic-gate (void) close(s); 4737c478bd9Sstevel@tonic-gate s = -1; 4747c478bd9Sstevel@tonic-gate v_circuit = 1; 4757c478bd9Sstevel@tonic-gate goto usevc; 4767c478bd9Sstevel@tonic-gate } 4777c478bd9Sstevel@tonic-gate } 4787c478bd9Sstevel@tonic-gate #ifdef DEBUG 4797c478bd9Sstevel@tonic-gate if (_res.options & RES_DEBUG) { 4807c478bd9Sstevel@tonic-gate printf("got answer:\n"); 4817c478bd9Sstevel@tonic-gate p_query(answer); 4827c478bd9Sstevel@tonic-gate } 4836a1c6faaSanay #endif 4847c478bd9Sstevel@tonic-gate /* 4857c478bd9Sstevel@tonic-gate * If using virtual circuits, we assume that the first server 4867c478bd9Sstevel@tonic-gate * is preferred * over the rest (i.e. it is on the local 4877c478bd9Sstevel@tonic-gate * machine) and only keep that one open. 4887c478bd9Sstevel@tonic-gate * If we have temporarily opened a virtual circuit, 4897c478bd9Sstevel@tonic-gate * or if we haven't been asked to keep a socket open, 4907c478bd9Sstevel@tonic-gate * close the socket. 4917c478bd9Sstevel@tonic-gate */ 4927c478bd9Sstevel@tonic-gate if ((v_circuit && 4937c478bd9Sstevel@tonic-gate ((_res.options & RES_USEVC) == 0 || ns != 0)) || 4947c478bd9Sstevel@tonic-gate (_res.options & RES_STAYOPEN) == 0) { 4957c478bd9Sstevel@tonic-gate (void) close(s); 4967c478bd9Sstevel@tonic-gate s = -1; 4977c478bd9Sstevel@tonic-gate } 4987c478bd9Sstevel@tonic-gate return (resplen); 4997c478bd9Sstevel@tonic-gate } 5007c478bd9Sstevel@tonic-gate } 5017c478bd9Sstevel@tonic-gate if (s >= 0) { 5027c478bd9Sstevel@tonic-gate (void) close(s); 5037c478bd9Sstevel@tonic-gate s = -1; 5047c478bd9Sstevel@tonic-gate } 5057c478bd9Sstevel@tonic-gate if (v_circuit == 0) 5067c478bd9Sstevel@tonic-gate if (gotsomewhere == 0) 5077c478bd9Sstevel@tonic-gate errno = ECONNREFUSED; /* no nameservers found */ 5087c478bd9Sstevel@tonic-gate else 5097c478bd9Sstevel@tonic-gate errno = ETIMEDOUT; /* no answer obtained */ 5107c478bd9Sstevel@tonic-gate else 5117c478bd9Sstevel@tonic-gate errno = terrno; 5127c478bd9Sstevel@tonic-gate return (-1); 5137c478bd9Sstevel@tonic-gate } 5147c478bd9Sstevel@tonic-gate 5157c478bd9Sstevel@tonic-gate /* 5167c478bd9Sstevel@tonic-gate * This routine is for closing the socket if a virtual circuit is used and 5177c478bd9Sstevel@tonic-gate * the program wants to close it. This provides support for endhostent() 5187c478bd9Sstevel@tonic-gate * which expects to close the socket. 5197c478bd9Sstevel@tonic-gate * 5207c478bd9Sstevel@tonic-gate * This routine is not expected to be user visible. 5217c478bd9Sstevel@tonic-gate */ 5226a1c6faaSanay void 5237c478bd9Sstevel@tonic-gate _res_close() 5247c478bd9Sstevel@tonic-gate { 5257c478bd9Sstevel@tonic-gate if (s != -1) { 5267c478bd9Sstevel@tonic-gate (void) close(s); 5277c478bd9Sstevel@tonic-gate s = -1; 5287c478bd9Sstevel@tonic-gate } 5297c478bd9Sstevel@tonic-gate } 530