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