10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 23*237Sanay * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 280Sstevel@tonic-gate /* All Rights Reserved */ 290Sstevel@tonic-gate 300Sstevel@tonic-gate /* 310Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 320Sstevel@tonic-gate * The Regents of the University of California 330Sstevel@tonic-gate * All Rights Reserved 340Sstevel@tonic-gate * 350Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 360Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 370Sstevel@tonic-gate * contributors. 380Sstevel@tonic-gate */ 390Sstevel@tonic-gate 400Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 410Sstevel@tonic-gate 420Sstevel@tonic-gate /* 430Sstevel@tonic-gate * Send query to name server and wait for reply. 440Sstevel@tonic-gate */ 450Sstevel@tonic-gate 460Sstevel@tonic-gate #include "synonyms.h" 470Sstevel@tonic-gate 480Sstevel@tonic-gate #include <sys/param.h> 490Sstevel@tonic-gate #include <sys/time.h> 500Sstevel@tonic-gate #include <sys/socket.h> 510Sstevel@tonic-gate #include <sys/uio.h> 520Sstevel@tonic-gate #include <sys/stat.h> 530Sstevel@tonic-gate #include <netinet/in.h> 540Sstevel@tonic-gate #include <stdio.h> 550Sstevel@tonic-gate #include <errno.h> 560Sstevel@tonic-gate #include <arpa/nameser.h> 570Sstevel@tonic-gate #include <resolv.h> 580Sstevel@tonic-gate 590Sstevel@tonic-gate 600Sstevel@tonic-gate static int s = -1; /* socket used for communications */ 610Sstevel@tonic-gate static struct sockaddr no_addr; 620Sstevel@tonic-gate 630Sstevel@tonic-gate 640Sstevel@tonic-gate #ifndef FD_SET 650Sstevel@tonic-gate #define NFDBITS 32 660Sstevel@tonic-gate #define FD_SETSIZE 32 670Sstevel@tonic-gate #define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS))) 680Sstevel@tonic-gate #define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS))) 690Sstevel@tonic-gate #define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS))) 700Sstevel@tonic-gate #ifdef SYSV 710Sstevel@tonic-gate #define FD_ZERO(p) memset((void *)(p), 0, sizeof (*(p))) 720Sstevel@tonic-gate #else 730Sstevel@tonic-gate #define FD_ZERO(p) bzero((char *)(p), sizeof (*(p))) 740Sstevel@tonic-gate #endif 750Sstevel@tonic-gate #endif 760Sstevel@tonic-gate 770Sstevel@tonic-gate /* 780Sstevel@tonic-gate * 1247019: Kludge to time out quickly if there is no /etc/resolv.conf 790Sstevel@tonic-gate * and a TCP connection to the local DNS server fails. 800Sstevel@tonic-gate */ 810Sstevel@tonic-gate 820Sstevel@tonic-gate static int _confcheck() 830Sstevel@tonic-gate { 840Sstevel@tonic-gate int ns; 850Sstevel@tonic-gate struct stat rc_stat; 860Sstevel@tonic-gate struct sockaddr_in ns_sin; 870Sstevel@tonic-gate 880Sstevel@tonic-gate 890Sstevel@tonic-gate /* First, we check to see if /etc/resolv.conf exists. 900Sstevel@tonic-gate * If it doesn't, then localhost is mostlikely to be 910Sstevel@tonic-gate * the nameserver. 920Sstevel@tonic-gate */ 930Sstevel@tonic-gate if (stat(_PATH_RESCONF, &rc_stat) == -1 && errno == ENOENT) { 940Sstevel@tonic-gate 950Sstevel@tonic-gate /* Next, we check to see if _res.nsaddr is set to loopback. 960Sstevel@tonic-gate * If it isn't, it has been altered by the application 970Sstevel@tonic-gate * explicitly and we then want to bail with success. 980Sstevel@tonic-gate */ 990Sstevel@tonic-gate if (_res.nsaddr.sin_addr.S_un.S_addr == htonl(INADDR_LOOPBACK)) { 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate /* Lastly, we try to connect to the TCP port of the 1020Sstevel@tonic-gate * nameserver. If this fails, then we know that 1030Sstevel@tonic-gate * DNS is misconfigured and we can quickly exit. 1040Sstevel@tonic-gate */ 1050Sstevel@tonic-gate ns = socket(AF_INET, SOCK_STREAM, 0); 1060Sstevel@tonic-gate IN_SET_LOOPBACK_ADDR(&ns_sin); 1070Sstevel@tonic-gate ns_sin.sin_port = htons(NAMESERVER_PORT); 1080Sstevel@tonic-gate if (connect(ns, (struct sockaddr *) &ns_sin, 1090Sstevel@tonic-gate sizeof ns_sin) == -1) { 1100Sstevel@tonic-gate close(ns); 1110Sstevel@tonic-gate return(-1); 1120Sstevel@tonic-gate } 1130Sstevel@tonic-gate else { 1140Sstevel@tonic-gate close(ns); 1150Sstevel@tonic-gate return(0); 1160Sstevel@tonic-gate } 1170Sstevel@tonic-gate } 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate return(0); 1200Sstevel@tonic-gate } 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate return (0); 1230Sstevel@tonic-gate } 1240Sstevel@tonic-gate 125*237Sanay int 1260Sstevel@tonic-gate res_send(buf, buflen, answer, anslen) 1270Sstevel@tonic-gate char *buf; 1280Sstevel@tonic-gate int buflen; 1290Sstevel@tonic-gate char *answer; 1300Sstevel@tonic-gate int anslen; 1310Sstevel@tonic-gate { 1320Sstevel@tonic-gate register int n; 1330Sstevel@tonic-gate int try, v_circuit, resplen, ns; 1340Sstevel@tonic-gate int gotsomewhere = 0, connected = 0; 1350Sstevel@tonic-gate int connreset = 0; 1360Sstevel@tonic-gate u_short id, len; 1370Sstevel@tonic-gate char *cp; 1380Sstevel@tonic-gate fd_set dsmask; 1390Sstevel@tonic-gate struct timeval timeout; 1400Sstevel@tonic-gate HEADER *hp = (HEADER *) buf; 1410Sstevel@tonic-gate HEADER *anhp = (HEADER *) answer; 1420Sstevel@tonic-gate struct iovec iov[2]; 1430Sstevel@tonic-gate int terrno = ETIMEDOUT; 1440Sstevel@tonic-gate char junk[512]; 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate #ifdef DEBUG 1470Sstevel@tonic-gate if (_res.options & RES_DEBUG) { 1480Sstevel@tonic-gate printf("res_send()\n"); 1490Sstevel@tonic-gate p_query(buf); 1500Sstevel@tonic-gate } 151*237Sanay #endif 1520Sstevel@tonic-gate if (!(_res.options & RES_INIT)) 1530Sstevel@tonic-gate if (res_init() == -1) { 1540Sstevel@tonic-gate return (-1); 1550Sstevel@tonic-gate } 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate /* 1247019: Check to see if we can bailout quickly. */ 1580Sstevel@tonic-gate if (_confcheck() == -1) 1590Sstevel@tonic-gate return(-1); 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate v_circuit = (_res.options & RES_USEVC) || buflen > PACKETSZ; 1620Sstevel@tonic-gate id = hp->id; 1630Sstevel@tonic-gate /* 1640Sstevel@tonic-gate * Send request, RETRY times, or until successful 1650Sstevel@tonic-gate */ 1660Sstevel@tonic-gate for (try = 0; try < _res.retry; try++) { 1670Sstevel@tonic-gate for (ns = 0; ns < _res.nscount; ns++) { 1680Sstevel@tonic-gate #ifdef DEBUG 1690Sstevel@tonic-gate if (_res.options & RES_DEBUG) 1700Sstevel@tonic-gate printf("Querying server (# %d) address = %s\n", 1710Sstevel@tonic-gate ns+1, inet_ntoa(_res.nsaddr_list[ns].sin_addr)); 172*237Sanay #endif 1730Sstevel@tonic-gate usevc: 1740Sstevel@tonic-gate if (v_circuit) { 1750Sstevel@tonic-gate int truncated = 0; 1760Sstevel@tonic-gate 1770Sstevel@tonic-gate /* 1780Sstevel@tonic-gate * Use virtual circuit; 1790Sstevel@tonic-gate * at most one attempt per server. 1800Sstevel@tonic-gate */ 1810Sstevel@tonic-gate try = _res.retry; 1820Sstevel@tonic-gate if (s < 0) { 1830Sstevel@tonic-gate s = _socket(AF_INET, SOCK_STREAM, 0); 1840Sstevel@tonic-gate if (s < 0) { 1850Sstevel@tonic-gate terrno = errno; 1860Sstevel@tonic-gate #ifdef DEBUG 1870Sstevel@tonic-gate if (_res.options & RES_DEBUG) { 1880Sstevel@tonic-gate perror("socket (vc) failed"); 1890Sstevel@tonic-gate } 190*237Sanay #endif 1910Sstevel@tonic-gate continue; 1920Sstevel@tonic-gate } 1930Sstevel@tonic-gate if (connect(s, (struct sockaddr *) &_res.nsaddr_list[ns], 1940Sstevel@tonic-gate sizeof (struct sockaddr)) < 0) { 1950Sstevel@tonic-gate terrno = errno; 1960Sstevel@tonic-gate #ifdef DEBUG 1970Sstevel@tonic-gate if (_res.options & RES_DEBUG) { 1980Sstevel@tonic-gate perror("connect failed"); 1990Sstevel@tonic-gate } 200*237Sanay #endif 2010Sstevel@tonic-gate (void) close(s); 2020Sstevel@tonic-gate s = -1; 2030Sstevel@tonic-gate continue; 2040Sstevel@tonic-gate } 2050Sstevel@tonic-gate } 2060Sstevel@tonic-gate /* 2070Sstevel@tonic-gate * Send length & message 2080Sstevel@tonic-gate */ 2090Sstevel@tonic-gate len = htons((u_short)buflen); 2100Sstevel@tonic-gate iov[0].iov_base = (caddr_t)&len; 2110Sstevel@tonic-gate iov[0].iov_len = sizeof (len); 2120Sstevel@tonic-gate iov[1].iov_base = buf; 2130Sstevel@tonic-gate iov[1].iov_len = buflen; 2140Sstevel@tonic-gate if (writev(s, iov, 2) != sizeof (len) + 2150Sstevel@tonic-gate buflen) { 2160Sstevel@tonic-gate terrno = errno; 2170Sstevel@tonic-gate #ifdef DEBUG 2180Sstevel@tonic-gate if (_res.options & RES_DEBUG) 2190Sstevel@tonic-gate perror("write failed"); 220*237Sanay #endif 2210Sstevel@tonic-gate (void) close(s); 2220Sstevel@tonic-gate s = -1; 2230Sstevel@tonic-gate continue; 2240Sstevel@tonic-gate } 2250Sstevel@tonic-gate /* 2260Sstevel@tonic-gate * Receive length & response 2270Sstevel@tonic-gate */ 2280Sstevel@tonic-gate cp = answer; 2290Sstevel@tonic-gate len = sizeof (short); 2300Sstevel@tonic-gate while (len != 0 && (n = read 2310Sstevel@tonic-gate (s, (char *)cp, (int)len)) > 0) { 2320Sstevel@tonic-gate cp += n; 2330Sstevel@tonic-gate len -= n; 2340Sstevel@tonic-gate } 2350Sstevel@tonic-gate if (n <= 0) { 2360Sstevel@tonic-gate terrno = errno; 2370Sstevel@tonic-gate #ifdef DEBUG 2380Sstevel@tonic-gate if (_res.options & RES_DEBUG) 2390Sstevel@tonic-gate perror("read failed"); 240*237Sanay #endif 2410Sstevel@tonic-gate (void) close(s); 2420Sstevel@tonic-gate s = -1; 2430Sstevel@tonic-gate /* 2440Sstevel@tonic-gate * A long running process might get its TCP 2450Sstevel@tonic-gate * connection reset if the remote server was 2460Sstevel@tonic-gate * restarted. Requery the server instead of 2470Sstevel@tonic-gate * trying a new one. When there is only one 2480Sstevel@tonic-gate * server, this means that a query might work 2490Sstevel@tonic-gate * instead of failing. We only allow one reset 2500Sstevel@tonic-gate * per query to prevent looping. 2510Sstevel@tonic-gate */ 2520Sstevel@tonic-gate if (terrno == ECONNRESET && 2530Sstevel@tonic-gate !connreset) { 2540Sstevel@tonic-gate connreset = 1; 2550Sstevel@tonic-gate ns--; 2560Sstevel@tonic-gate } 2570Sstevel@tonic-gate continue; 2580Sstevel@tonic-gate } 2590Sstevel@tonic-gate cp = answer; 2600Sstevel@tonic-gate if ((resplen = ntohs(*(u_short *)cp)) > 2610Sstevel@tonic-gate anslen) { 2620Sstevel@tonic-gate #ifdef DEBUG 2630Sstevel@tonic-gate if (_res.options & RES_DEBUG) 2640Sstevel@tonic-gate fprintf(stderr, 2650Sstevel@tonic-gate "response truncated\n"); 266*237Sanay #endif 2670Sstevel@tonic-gate len = anslen; 2680Sstevel@tonic-gate truncated = 1; 2690Sstevel@tonic-gate } else 2700Sstevel@tonic-gate len = resplen; 2710Sstevel@tonic-gate while (len != 0 && 2720Sstevel@tonic-gate (n = read(s, (char *)cp, 2730Sstevel@tonic-gate (int)len)) > 0) { 2740Sstevel@tonic-gate cp += n; 2750Sstevel@tonic-gate len -= n; 2760Sstevel@tonic-gate } 2770Sstevel@tonic-gate if (n <= 0) { 2780Sstevel@tonic-gate terrno = errno; 2790Sstevel@tonic-gate #ifdef DEBUG 2800Sstevel@tonic-gate if (_res.options & RES_DEBUG) 2810Sstevel@tonic-gate perror("read failed"); 282*237Sanay #endif 2830Sstevel@tonic-gate (void) close(s); 2840Sstevel@tonic-gate s = -1; 2850Sstevel@tonic-gate continue; 2860Sstevel@tonic-gate } 2870Sstevel@tonic-gate if (truncated) { 2880Sstevel@tonic-gate /* 2890Sstevel@tonic-gate * Flush rest of answer 2900Sstevel@tonic-gate * so connection stays in synch. 2910Sstevel@tonic-gate */ 2920Sstevel@tonic-gate anhp->tc = 1; 2930Sstevel@tonic-gate len = resplen - anslen; 2940Sstevel@tonic-gate /* 2950Sstevel@tonic-gate * set the value of resplen to anslen, 2960Sstevel@tonic-gate * this is done because the caller 2970Sstevel@tonic-gate * assumes resplen contains the size of 2980Sstevel@tonic-gate * message read into the "answer" buffer 2990Sstevel@tonic-gate * passed in. 3000Sstevel@tonic-gate */ 3010Sstevel@tonic-gate resplen = anslen; 3020Sstevel@tonic-gate 3030Sstevel@tonic-gate while (len != 0) { 3040Sstevel@tonic-gate n = (len > sizeof (junk) ? 3050Sstevel@tonic-gate sizeof (junk) : len); 3060Sstevel@tonic-gate if ((n = read(s, junk, n)) > 0) 3070Sstevel@tonic-gate len -= n; 3080Sstevel@tonic-gate else 3090Sstevel@tonic-gate break; 3100Sstevel@tonic-gate } 3110Sstevel@tonic-gate } 3120Sstevel@tonic-gate } else { 3130Sstevel@tonic-gate /* 3140Sstevel@tonic-gate * Use datagrams. 3150Sstevel@tonic-gate */ 3160Sstevel@tonic-gate if (s < 0) { 3170Sstevel@tonic-gate s = _socket(AF_INET, SOCK_DGRAM, 0); 3180Sstevel@tonic-gate if (s < 0) { 3190Sstevel@tonic-gate terrno = errno; 3200Sstevel@tonic-gate #ifdef DEBUG 3210Sstevel@tonic-gate if (_res.options & RES_DEBUG) { 3220Sstevel@tonic-gate perror("socket (dg) failed"); 3230Sstevel@tonic-gate } 324*237Sanay #endif 3250Sstevel@tonic-gate continue; 3260Sstevel@tonic-gate } 3270Sstevel@tonic-gate } 3280Sstevel@tonic-gate #if BSD >= 43 3290Sstevel@tonic-gate /* 3300Sstevel@tonic-gate * I'm tired of answering this question, so: 3310Sstevel@tonic-gate * On a 4.3BSD+ machine (client and server, 3320Sstevel@tonic-gate * actually), sending to a nameserver datagram 3330Sstevel@tonic-gate * port with no nameserver will cause an 3340Sstevel@tonic-gate * ICMP port unreachable message to be returned. 3350Sstevel@tonic-gate * If our datagram socket is "connected" to the 3360Sstevel@tonic-gate * server, we get an ECONNREFUSED error on the next 3370Sstevel@tonic-gate * socket operation, and select returns if the 3380Sstevel@tonic-gate * error message is received. We can thus detect 3390Sstevel@tonic-gate * the absence of a nameserver without timing out. 3400Sstevel@tonic-gate * If we have sent queries to at least two servers, 3410Sstevel@tonic-gate * however, we don't want to remain connected, 3420Sstevel@tonic-gate * as we wish to receive answers from the first 3430Sstevel@tonic-gate * server to respond. 3440Sstevel@tonic-gate */ 3450Sstevel@tonic-gate if (_res.nscount == 1 || 3460Sstevel@tonic-gate (try == 0 && ns == 0)) { 3470Sstevel@tonic-gate /* 3480Sstevel@tonic-gate * Don't use connect if we might 3490Sstevel@tonic-gate * still receive a response 3500Sstevel@tonic-gate * from another server. 3510Sstevel@tonic-gate */ 3520Sstevel@tonic-gate if (connected == 0) { 3530Sstevel@tonic-gate if (connect(s, 3540Sstevel@tonic-gate (struct sockaddr *) &_res.nsaddr_list[ns], 3550Sstevel@tonic-gate sizeof (struct sockaddr)) < 0) { 3560Sstevel@tonic-gate #ifdef DEBUG 3570Sstevel@tonic-gate if (_res.options & 3580Sstevel@tonic-gate RES_DEBUG) { 3590Sstevel@tonic-gate perror("connect"); 3600Sstevel@tonic-gate } 361*237Sanay #endif 3620Sstevel@tonic-gate continue; 3630Sstevel@tonic-gate } 3640Sstevel@tonic-gate connected = 1; 3650Sstevel@tonic-gate } 3660Sstevel@tonic-gate if (send(s, buf, buflen, 0) != buflen) { 3670Sstevel@tonic-gate #ifdef DEBUG 3680Sstevel@tonic-gate if (_res.options & RES_DEBUG) 3690Sstevel@tonic-gate perror("send"); 370*237Sanay #endif 3710Sstevel@tonic-gate continue; 3720Sstevel@tonic-gate } 3730Sstevel@tonic-gate } else { 3740Sstevel@tonic-gate /* 3750Sstevel@tonic-gate * Disconnect if we want to listen for 3760Sstevel@tonic-gate * responses from more than one server. 3770Sstevel@tonic-gate */ 3780Sstevel@tonic-gate if (connected) { 3790Sstevel@tonic-gate (void) connect(s, &no_addr, 3800Sstevel@tonic-gate sizeof (no_addr)); 3810Sstevel@tonic-gate connected = 0; 3820Sstevel@tonic-gate } 383*237Sanay #endif /* BSD */ 3840Sstevel@tonic-gate if (sendto(s, buf, buflen, 0, 3850Sstevel@tonic-gate (struct sockaddr *) &_res.nsaddr_list[ns], 3860Sstevel@tonic-gate sizeof (struct sockaddr)) != buflen) { 3870Sstevel@tonic-gate #ifdef DEBUG 3880Sstevel@tonic-gate if (_res.options & RES_DEBUG) 3890Sstevel@tonic-gate perror("sendto"); 390*237Sanay #endif 3910Sstevel@tonic-gate continue; 3920Sstevel@tonic-gate } 3930Sstevel@tonic-gate #if BSD >= 43 3940Sstevel@tonic-gate } 3950Sstevel@tonic-gate #endif 3960Sstevel@tonic-gate 3970Sstevel@tonic-gate /* 3980Sstevel@tonic-gate * Wait for reply 3990Sstevel@tonic-gate */ 4000Sstevel@tonic-gate timeout.tv_sec = (_res.retrans << try); 4010Sstevel@tonic-gate if (try > 0) 4020Sstevel@tonic-gate timeout.tv_sec /= _res.nscount; 4030Sstevel@tonic-gate if (timeout.tv_sec <= 0) 4040Sstevel@tonic-gate timeout.tv_sec = 1; 4050Sstevel@tonic-gate timeout.tv_usec = 0; 4060Sstevel@tonic-gate wait: 4070Sstevel@tonic-gate FD_ZERO(&dsmask); 4080Sstevel@tonic-gate FD_SET(s, &dsmask); 4090Sstevel@tonic-gate n = select(s+1, &dsmask, (fd_set *)NULL, 4100Sstevel@tonic-gate (fd_set *)NULL, &timeout); 4110Sstevel@tonic-gate if (n < 0) { 4120Sstevel@tonic-gate #ifdef DEBUG 4130Sstevel@tonic-gate if (_res.options & RES_DEBUG) 4140Sstevel@tonic-gate perror("select"); 415*237Sanay #endif 4160Sstevel@tonic-gate continue; 4170Sstevel@tonic-gate } 4180Sstevel@tonic-gate if (n == 0) { 4190Sstevel@tonic-gate /* 4200Sstevel@tonic-gate * timeout 4210Sstevel@tonic-gate */ 4220Sstevel@tonic-gate #ifdef DEBUG 4230Sstevel@tonic-gate if (_res.options & RES_DEBUG) 4240Sstevel@tonic-gate printf("timeout\n"); 425*237Sanay #endif 4260Sstevel@tonic-gate #if BSD >= 43 4270Sstevel@tonic-gate gotsomewhere = 1; 4280Sstevel@tonic-gate #endif 4290Sstevel@tonic-gate continue; 4300Sstevel@tonic-gate } 4310Sstevel@tonic-gate if ((resplen = recv(s, answer, anslen, 0)) 4320Sstevel@tonic-gate <= 0) { 4330Sstevel@tonic-gate #ifdef DEBUG 4340Sstevel@tonic-gate if (_res.options & RES_DEBUG) 4350Sstevel@tonic-gate perror("recvfrom"); 436*237Sanay #endif 4370Sstevel@tonic-gate continue; 4380Sstevel@tonic-gate } 4390Sstevel@tonic-gate gotsomewhere = 1; 4400Sstevel@tonic-gate if (id != anhp->id) { 4410Sstevel@tonic-gate /* 4420Sstevel@tonic-gate * response from old query, ignore it 4430Sstevel@tonic-gate */ 4440Sstevel@tonic-gate #ifdef DEBUG 4450Sstevel@tonic-gate if (_res.options & RES_DEBUG) { 4460Sstevel@tonic-gate printf("old answer:\n"); 4470Sstevel@tonic-gate p_query(answer); 4480Sstevel@tonic-gate } 449*237Sanay #endif 4500Sstevel@tonic-gate goto wait; 4510Sstevel@tonic-gate } 4520Sstevel@tonic-gate if (!(_res.options & RES_IGNTC) && anhp->tc) { 4530Sstevel@tonic-gate /* 4540Sstevel@tonic-gate * get rest of answer; 4550Sstevel@tonic-gate * use TCP with same server. 4560Sstevel@tonic-gate */ 4570Sstevel@tonic-gate #ifdef DEBUG 4580Sstevel@tonic-gate if (_res.options & RES_DEBUG) 4590Sstevel@tonic-gate printf("truncated answer\n"); 460*237Sanay #endif 4610Sstevel@tonic-gate (void) close(s); 4620Sstevel@tonic-gate s = -1; 4630Sstevel@tonic-gate v_circuit = 1; 4640Sstevel@tonic-gate goto usevc; 4650Sstevel@tonic-gate } 4660Sstevel@tonic-gate } 4670Sstevel@tonic-gate #ifdef DEBUG 4680Sstevel@tonic-gate if (_res.options & RES_DEBUG) { 4690Sstevel@tonic-gate printf("got answer:\n"); 4700Sstevel@tonic-gate p_query(answer); 4710Sstevel@tonic-gate } 472*237Sanay #endif 4730Sstevel@tonic-gate /* 4740Sstevel@tonic-gate * If using virtual circuits, we assume that the first server 4750Sstevel@tonic-gate * is preferred * over the rest (i.e. it is on the local 4760Sstevel@tonic-gate * machine) and only keep that one open. 4770Sstevel@tonic-gate * If we have temporarily opened a virtual circuit, 4780Sstevel@tonic-gate * or if we haven't been asked to keep a socket open, 4790Sstevel@tonic-gate * close the socket. 4800Sstevel@tonic-gate */ 4810Sstevel@tonic-gate if ((v_circuit && 4820Sstevel@tonic-gate ((_res.options & RES_USEVC) == 0 || ns != 0)) || 4830Sstevel@tonic-gate (_res.options & RES_STAYOPEN) == 0) { 4840Sstevel@tonic-gate (void) close(s); 4850Sstevel@tonic-gate s = -1; 4860Sstevel@tonic-gate } 4870Sstevel@tonic-gate return (resplen); 4880Sstevel@tonic-gate } 4890Sstevel@tonic-gate } 4900Sstevel@tonic-gate if (s >= 0) { 4910Sstevel@tonic-gate (void) close(s); 4920Sstevel@tonic-gate s = -1; 4930Sstevel@tonic-gate } 4940Sstevel@tonic-gate if (v_circuit == 0) 4950Sstevel@tonic-gate if (gotsomewhere == 0) 4960Sstevel@tonic-gate errno = ECONNREFUSED; /* no nameservers found */ 4970Sstevel@tonic-gate else 4980Sstevel@tonic-gate errno = ETIMEDOUT; /* no answer obtained */ 4990Sstevel@tonic-gate else 5000Sstevel@tonic-gate errno = terrno; 5010Sstevel@tonic-gate return (-1); 5020Sstevel@tonic-gate } 5030Sstevel@tonic-gate 5040Sstevel@tonic-gate /* 5050Sstevel@tonic-gate * This routine is for closing the socket if a virtual circuit is used and 5060Sstevel@tonic-gate * the program wants to close it. This provides support for endhostent() 5070Sstevel@tonic-gate * which expects to close the socket. 5080Sstevel@tonic-gate * 5090Sstevel@tonic-gate * This routine is not expected to be user visible. 5100Sstevel@tonic-gate */ 511*237Sanay void 5120Sstevel@tonic-gate _res_close() 5130Sstevel@tonic-gate { 5140Sstevel@tonic-gate if (s != -1) { 5150Sstevel@tonic-gate (void) close(s); 5160Sstevel@tonic-gate s = -1; 5170Sstevel@tonic-gate } 5180Sstevel@tonic-gate } 519