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