1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*0Sstevel@tonic-gate  * The Regents of the University of California
33*0Sstevel@tonic-gate  * All Rights Reserved
34*0Sstevel@tonic-gate  *
35*0Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*0Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*0Sstevel@tonic-gate  * contributors.
38*0Sstevel@tonic-gate  */
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate /*
43*0Sstevel@tonic-gate  * Send query to name server and wait for reply.
44*0Sstevel@tonic-gate  */
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate #include "synonyms.h"
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate #include <sys/param.h>
49*0Sstevel@tonic-gate #include <sys/time.h>
50*0Sstevel@tonic-gate #include <sys/socket.h>
51*0Sstevel@tonic-gate #include <sys/uio.h>
52*0Sstevel@tonic-gate #include <sys/stat.h>
53*0Sstevel@tonic-gate #include <netinet/in.h>
54*0Sstevel@tonic-gate #include <stdio.h>
55*0Sstevel@tonic-gate #include <errno.h>
56*0Sstevel@tonic-gate #include <arpa/nameser.h>
57*0Sstevel@tonic-gate #include <resolv.h>
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate static int s = -1;	/* socket used for communications */
61*0Sstevel@tonic-gate static struct sockaddr no_addr;
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate #ifndef FD_SET
65*0Sstevel@tonic-gate #define	NFDBITS		32
66*0Sstevel@tonic-gate #define	FD_SETSIZE	32
67*0Sstevel@tonic-gate #define	FD_SET(n, p)	((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
68*0Sstevel@tonic-gate #define	FD_CLR(n, p)	((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
69*0Sstevel@tonic-gate #define	FD_ISSET(n, p)	((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
70*0Sstevel@tonic-gate #ifdef SYSV
71*0Sstevel@tonic-gate #define	FD_ZERO(p)	memset((void *)(p), 0, sizeof (*(p)))
72*0Sstevel@tonic-gate #else
73*0Sstevel@tonic-gate #define	FD_ZERO(p)	bzero((char *)(p), sizeof (*(p)))
74*0Sstevel@tonic-gate #endif
75*0Sstevel@tonic-gate #endif
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate /*
78*0Sstevel@tonic-gate  * 1247019: Kludge to time out quickly if there is no /etc/resolv.conf
79*0Sstevel@tonic-gate  * and a TCP connection to the local DNS server fails.
80*0Sstevel@tonic-gate  */
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate static int _confcheck()
83*0Sstevel@tonic-gate {
84*0Sstevel@tonic-gate 	int ns;
85*0Sstevel@tonic-gate 	struct stat rc_stat;
86*0Sstevel@tonic-gate 	struct sockaddr_in ns_sin;
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate 	/* First, we check to see if /etc/resolv.conf exists.
90*0Sstevel@tonic-gate 	 * If it doesn't, then localhost is mostlikely to be
91*0Sstevel@tonic-gate 	 * the nameserver.
92*0Sstevel@tonic-gate 	 */
93*0Sstevel@tonic-gate 	if (stat(_PATH_RESCONF, &rc_stat) == -1 && errno == ENOENT) {
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate 		/* Next, we check to see if _res.nsaddr is set to loopback.
96*0Sstevel@tonic-gate 		 * If it isn't, it has been altered by the application
97*0Sstevel@tonic-gate 		 * explicitly and we then want to bail with success.
98*0Sstevel@tonic-gate 		 */
99*0Sstevel@tonic-gate 		if (_res.nsaddr.sin_addr.S_un.S_addr == htonl(INADDR_LOOPBACK)) {
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 			/* Lastly, we try to connect to the TCP port of the
102*0Sstevel@tonic-gate 			 * nameserver.  If this fails, then we know that
103*0Sstevel@tonic-gate 			 * DNS is misconfigured and we can quickly exit.
104*0Sstevel@tonic-gate 			 */
105*0Sstevel@tonic-gate 			ns = socket(AF_INET, SOCK_STREAM, 0);
106*0Sstevel@tonic-gate 			IN_SET_LOOPBACK_ADDR(&ns_sin);
107*0Sstevel@tonic-gate 			ns_sin.sin_port = htons(NAMESERVER_PORT);
108*0Sstevel@tonic-gate 			if (connect(ns, (struct sockaddr *) &ns_sin,
109*0Sstevel@tonic-gate 				    sizeof ns_sin) == -1) {
110*0Sstevel@tonic-gate 				close(ns);
111*0Sstevel@tonic-gate 				return(-1);
112*0Sstevel@tonic-gate 			}
113*0Sstevel@tonic-gate 			else {
114*0Sstevel@tonic-gate 				close(ns);
115*0Sstevel@tonic-gate 				return(0);
116*0Sstevel@tonic-gate 			}
117*0Sstevel@tonic-gate 		}
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 		return(0);
120*0Sstevel@tonic-gate 	}
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 	return (0);
123*0Sstevel@tonic-gate }
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate res_send(buf, buflen, answer, anslen)
126*0Sstevel@tonic-gate 	char *buf;
127*0Sstevel@tonic-gate 	int buflen;
128*0Sstevel@tonic-gate 	char *answer;
129*0Sstevel@tonic-gate 	int anslen;
130*0Sstevel@tonic-gate {
131*0Sstevel@tonic-gate 	register int n;
132*0Sstevel@tonic-gate 	int try, v_circuit, resplen, ns;
133*0Sstevel@tonic-gate 	int gotsomewhere = 0, connected = 0;
134*0Sstevel@tonic-gate 	int connreset = 0;
135*0Sstevel@tonic-gate 	u_short id, len;
136*0Sstevel@tonic-gate 	char *cp;
137*0Sstevel@tonic-gate 	fd_set dsmask;
138*0Sstevel@tonic-gate 	struct timeval timeout;
139*0Sstevel@tonic-gate 	HEADER *hp = (HEADER *) buf;
140*0Sstevel@tonic-gate 	HEADER *anhp = (HEADER *) answer;
141*0Sstevel@tonic-gate 	struct iovec iov[2];
142*0Sstevel@tonic-gate 	int terrno = ETIMEDOUT;
143*0Sstevel@tonic-gate 	char junk[512];
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate #ifdef DEBUG
146*0Sstevel@tonic-gate 	if (_res.options & RES_DEBUG) {
147*0Sstevel@tonic-gate 		printf("res_send()\n");
148*0Sstevel@tonic-gate 		p_query(buf);
149*0Sstevel@tonic-gate 	}
150*0Sstevel@tonic-gate #endif DEBUG
151*0Sstevel@tonic-gate 	if (!(_res.options & RES_INIT))
152*0Sstevel@tonic-gate 		if (res_init() == -1) {
153*0Sstevel@tonic-gate 			return (-1);
154*0Sstevel@tonic-gate 		}
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 	/* 1247019: Check to see if we can bailout quickly. */
157*0Sstevel@tonic-gate 	if (_confcheck() == -1)
158*0Sstevel@tonic-gate 	    return(-1);
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 	v_circuit = (_res.options & RES_USEVC) || buflen > PACKETSZ;
161*0Sstevel@tonic-gate 	id = hp->id;
162*0Sstevel@tonic-gate 	/*
163*0Sstevel@tonic-gate 	 * Send request, RETRY times, or until successful
164*0Sstevel@tonic-gate 	 */
165*0Sstevel@tonic-gate 	for (try = 0; try < _res.retry; try++) {
166*0Sstevel@tonic-gate 		for (ns = 0; ns < _res.nscount; ns++) {
167*0Sstevel@tonic-gate #ifdef DEBUG
168*0Sstevel@tonic-gate 			if (_res.options & RES_DEBUG)
169*0Sstevel@tonic-gate 				printf("Querying server (# %d) address = %s\n",
170*0Sstevel@tonic-gate 				ns+1, inet_ntoa(_res.nsaddr_list[ns].sin_addr));
171*0Sstevel@tonic-gate #endif DEBUG
172*0Sstevel@tonic-gate 		usevc:
173*0Sstevel@tonic-gate 			if (v_circuit) {
174*0Sstevel@tonic-gate 				int truncated = 0;
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate 				/*
177*0Sstevel@tonic-gate 				 * Use virtual circuit;
178*0Sstevel@tonic-gate 				 * at most one attempt per server.
179*0Sstevel@tonic-gate 				 */
180*0Sstevel@tonic-gate 				try = _res.retry;
181*0Sstevel@tonic-gate 				if (s < 0) {
182*0Sstevel@tonic-gate 					s = _socket(AF_INET, SOCK_STREAM, 0);
183*0Sstevel@tonic-gate 					if (s < 0) {
184*0Sstevel@tonic-gate 						terrno = errno;
185*0Sstevel@tonic-gate #ifdef DEBUG
186*0Sstevel@tonic-gate 						if (_res.options & RES_DEBUG) {
187*0Sstevel@tonic-gate 						perror("socket (vc) failed");
188*0Sstevel@tonic-gate 						}
189*0Sstevel@tonic-gate #endif DEBUG
190*0Sstevel@tonic-gate 						continue;
191*0Sstevel@tonic-gate 					}
192*0Sstevel@tonic-gate 					if (connect(s, (struct sockaddr *) &_res.nsaddr_list[ns],
193*0Sstevel@tonic-gate 						sizeof (struct sockaddr)) < 0) {
194*0Sstevel@tonic-gate 						terrno = errno;
195*0Sstevel@tonic-gate #ifdef DEBUG
196*0Sstevel@tonic-gate 						if (_res.options & RES_DEBUG) {
197*0Sstevel@tonic-gate 						perror("connect failed");
198*0Sstevel@tonic-gate 						}
199*0Sstevel@tonic-gate #endif DEBUG
200*0Sstevel@tonic-gate 						(void) close(s);
201*0Sstevel@tonic-gate 						s = -1;
202*0Sstevel@tonic-gate 						continue;
203*0Sstevel@tonic-gate 					}
204*0Sstevel@tonic-gate 				}
205*0Sstevel@tonic-gate 				/*
206*0Sstevel@tonic-gate 				 * Send length & message
207*0Sstevel@tonic-gate 				 */
208*0Sstevel@tonic-gate 				len = htons((u_short)buflen);
209*0Sstevel@tonic-gate 				iov[0].iov_base = (caddr_t)&len;
210*0Sstevel@tonic-gate 				iov[0].iov_len = sizeof (len);
211*0Sstevel@tonic-gate 				iov[1].iov_base = buf;
212*0Sstevel@tonic-gate 				iov[1].iov_len = buflen;
213*0Sstevel@tonic-gate 				if (writev(s, iov, 2) != sizeof (len) +
214*0Sstevel@tonic-gate 								buflen) {
215*0Sstevel@tonic-gate 					terrno = errno;
216*0Sstevel@tonic-gate #ifdef DEBUG
217*0Sstevel@tonic-gate 					if (_res.options & RES_DEBUG)
218*0Sstevel@tonic-gate 						perror("write failed");
219*0Sstevel@tonic-gate #endif DEBUG
220*0Sstevel@tonic-gate 					(void) close(s);
221*0Sstevel@tonic-gate 					s = -1;
222*0Sstevel@tonic-gate 					continue;
223*0Sstevel@tonic-gate 				}
224*0Sstevel@tonic-gate 				/*
225*0Sstevel@tonic-gate 				 * Receive length & response
226*0Sstevel@tonic-gate 				 */
227*0Sstevel@tonic-gate 				cp = answer;
228*0Sstevel@tonic-gate 				len = sizeof (short);
229*0Sstevel@tonic-gate 				while (len != 0 && (n = read
230*0Sstevel@tonic-gate 					(s, (char *)cp, (int)len)) > 0) {
231*0Sstevel@tonic-gate 					cp += n;
232*0Sstevel@tonic-gate 					len -= n;
233*0Sstevel@tonic-gate 				}
234*0Sstevel@tonic-gate 				if (n <= 0) {
235*0Sstevel@tonic-gate 					terrno = errno;
236*0Sstevel@tonic-gate #ifdef DEBUG
237*0Sstevel@tonic-gate 					if (_res.options & RES_DEBUG)
238*0Sstevel@tonic-gate 						perror("read failed");
239*0Sstevel@tonic-gate #endif DEBUG
240*0Sstevel@tonic-gate 					(void) close(s);
241*0Sstevel@tonic-gate 					s = -1;
242*0Sstevel@tonic-gate 				/*
243*0Sstevel@tonic-gate 				 * A long running process might get its TCP
244*0Sstevel@tonic-gate 				 * connection reset if the remote server was
245*0Sstevel@tonic-gate 				 * restarted.  Requery the server instead of
246*0Sstevel@tonic-gate 				 * trying a new one.  When there is only one
247*0Sstevel@tonic-gate 				 * server, this means that a query might work
248*0Sstevel@tonic-gate 				 * instead of failing.  We only allow one reset
249*0Sstevel@tonic-gate 				 * per query to prevent looping.
250*0Sstevel@tonic-gate 				 */
251*0Sstevel@tonic-gate 					if (terrno == ECONNRESET &&
252*0Sstevel@tonic-gate 							!connreset) {
253*0Sstevel@tonic-gate 						connreset = 1;
254*0Sstevel@tonic-gate 						ns--;
255*0Sstevel@tonic-gate 					}
256*0Sstevel@tonic-gate 					continue;
257*0Sstevel@tonic-gate 				}
258*0Sstevel@tonic-gate 				cp = answer;
259*0Sstevel@tonic-gate 				if ((resplen = ntohs(*(u_short *)cp)) >
260*0Sstevel@tonic-gate 								anslen) {
261*0Sstevel@tonic-gate #ifdef DEBUG
262*0Sstevel@tonic-gate 					if (_res.options & RES_DEBUG)
263*0Sstevel@tonic-gate 						fprintf(stderr,
264*0Sstevel@tonic-gate 							"response truncated\n");
265*0Sstevel@tonic-gate #endif DEBUG
266*0Sstevel@tonic-gate 					len = anslen;
267*0Sstevel@tonic-gate 					truncated = 1;
268*0Sstevel@tonic-gate 				} else
269*0Sstevel@tonic-gate 					len = resplen;
270*0Sstevel@tonic-gate 				while (len != 0 &&
271*0Sstevel@tonic-gate 					(n = read(s, (char *)cp,
272*0Sstevel@tonic-gate 							(int)len)) > 0) {
273*0Sstevel@tonic-gate 					cp += n;
274*0Sstevel@tonic-gate 					len -= n;
275*0Sstevel@tonic-gate 				}
276*0Sstevel@tonic-gate 				if (n <= 0) {
277*0Sstevel@tonic-gate 					terrno = errno;
278*0Sstevel@tonic-gate #ifdef DEBUG
279*0Sstevel@tonic-gate 					if (_res.options & RES_DEBUG)
280*0Sstevel@tonic-gate 						perror("read failed");
281*0Sstevel@tonic-gate #endif DEBUG
282*0Sstevel@tonic-gate 					(void) close(s);
283*0Sstevel@tonic-gate 					s = -1;
284*0Sstevel@tonic-gate 					continue;
285*0Sstevel@tonic-gate 				}
286*0Sstevel@tonic-gate 				if (truncated) {
287*0Sstevel@tonic-gate 					/*
288*0Sstevel@tonic-gate 					 * Flush rest of answer
289*0Sstevel@tonic-gate 					 * so connection stays in synch.
290*0Sstevel@tonic-gate 					 */
291*0Sstevel@tonic-gate 					anhp->tc = 1;
292*0Sstevel@tonic-gate 					len = resplen - anslen;
293*0Sstevel@tonic-gate 					/*
294*0Sstevel@tonic-gate 					 * set the value of resplen to anslen,
295*0Sstevel@tonic-gate 					 * this is done because the caller
296*0Sstevel@tonic-gate 					 * assumes resplen contains the size of
297*0Sstevel@tonic-gate 					 * message read into the "answer" buffer
298*0Sstevel@tonic-gate 					 * passed in.
299*0Sstevel@tonic-gate 					 */
300*0Sstevel@tonic-gate 					resplen = anslen;
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate 					while (len != 0) {
303*0Sstevel@tonic-gate 						n = (len > sizeof (junk) ?
304*0Sstevel@tonic-gate 							sizeof (junk) : len);
305*0Sstevel@tonic-gate 						if ((n = read(s, junk, n)) > 0)
306*0Sstevel@tonic-gate 							len -= n;
307*0Sstevel@tonic-gate 						else
308*0Sstevel@tonic-gate 							break;
309*0Sstevel@tonic-gate 					}
310*0Sstevel@tonic-gate 				}
311*0Sstevel@tonic-gate 			} else {
312*0Sstevel@tonic-gate 				/*
313*0Sstevel@tonic-gate 				 * Use datagrams.
314*0Sstevel@tonic-gate 				 */
315*0Sstevel@tonic-gate 				if (s < 0) {
316*0Sstevel@tonic-gate 					s = _socket(AF_INET, SOCK_DGRAM, 0);
317*0Sstevel@tonic-gate 					if (s < 0) {
318*0Sstevel@tonic-gate 						terrno = errno;
319*0Sstevel@tonic-gate #ifdef DEBUG
320*0Sstevel@tonic-gate 						if (_res.options & RES_DEBUG) {
321*0Sstevel@tonic-gate 						perror("socket (dg) failed");
322*0Sstevel@tonic-gate 						}
323*0Sstevel@tonic-gate #endif DEBUG
324*0Sstevel@tonic-gate 						continue;
325*0Sstevel@tonic-gate 					}
326*0Sstevel@tonic-gate 				}
327*0Sstevel@tonic-gate #if	BSD >= 43
328*0Sstevel@tonic-gate 			/*
329*0Sstevel@tonic-gate 			 * I'm tired of answering this question, so:
330*0Sstevel@tonic-gate 			 * On a 4.3BSD+ machine (client and server,
331*0Sstevel@tonic-gate 			 * actually), sending to a nameserver datagram
332*0Sstevel@tonic-gate 			 * port with no nameserver will cause an
333*0Sstevel@tonic-gate 			 * ICMP port unreachable message to be returned.
334*0Sstevel@tonic-gate 			 * If our datagram socket is "connected" to the
335*0Sstevel@tonic-gate 			 * server, we get an ECONNREFUSED error on the next
336*0Sstevel@tonic-gate 			 * socket operation, and select returns if the
337*0Sstevel@tonic-gate 			 * error message is received.  We can thus detect
338*0Sstevel@tonic-gate 			 * the absence of a nameserver without timing out.
339*0Sstevel@tonic-gate 			 * If we have sent queries to at least two servers,
340*0Sstevel@tonic-gate 			 * however, we don't want to remain connected,
341*0Sstevel@tonic-gate 			 * as we wish to receive answers from the first
342*0Sstevel@tonic-gate 			 * server to respond.
343*0Sstevel@tonic-gate 			 */
344*0Sstevel@tonic-gate 				if (_res.nscount == 1 ||
345*0Sstevel@tonic-gate 						(try == 0 && ns == 0)) {
346*0Sstevel@tonic-gate 					/*
347*0Sstevel@tonic-gate 					 * Don't use connect if we might
348*0Sstevel@tonic-gate 					 * still receive a response
349*0Sstevel@tonic-gate 					 * from another server.
350*0Sstevel@tonic-gate 					 */
351*0Sstevel@tonic-gate 					if (connected == 0) {
352*0Sstevel@tonic-gate 						if (connect(s,
353*0Sstevel@tonic-gate 						(struct sockaddr *) &_res.nsaddr_list[ns],
354*0Sstevel@tonic-gate 						sizeof (struct sockaddr)) < 0) {
355*0Sstevel@tonic-gate #ifdef DEBUG
356*0Sstevel@tonic-gate 							if (_res.options &
357*0Sstevel@tonic-gate 								RES_DEBUG) {
358*0Sstevel@tonic-gate 							perror("connect");
359*0Sstevel@tonic-gate 							}
360*0Sstevel@tonic-gate #endif DEBUG
361*0Sstevel@tonic-gate 							continue;
362*0Sstevel@tonic-gate 						}
363*0Sstevel@tonic-gate 						connected = 1;
364*0Sstevel@tonic-gate 					}
365*0Sstevel@tonic-gate 					if (send(s, buf, buflen, 0) != buflen) {
366*0Sstevel@tonic-gate #ifdef DEBUG
367*0Sstevel@tonic-gate 						if (_res.options & RES_DEBUG)
368*0Sstevel@tonic-gate 							perror("send");
369*0Sstevel@tonic-gate #endif DEBUG
370*0Sstevel@tonic-gate 						continue;
371*0Sstevel@tonic-gate 					}
372*0Sstevel@tonic-gate 				} else {
373*0Sstevel@tonic-gate 					/*
374*0Sstevel@tonic-gate 					 * Disconnect if we want to listen for
375*0Sstevel@tonic-gate 					 * responses from more than one server.
376*0Sstevel@tonic-gate 					 */
377*0Sstevel@tonic-gate 					if (connected) {
378*0Sstevel@tonic-gate 						(void) connect(s, &no_addr,
379*0Sstevel@tonic-gate 							sizeof (no_addr));
380*0Sstevel@tonic-gate 						connected = 0;
381*0Sstevel@tonic-gate 					}
382*0Sstevel@tonic-gate #endif BSD
383*0Sstevel@tonic-gate 					if (sendto(s, buf, buflen, 0,
384*0Sstevel@tonic-gate 						(struct sockaddr *) &_res.nsaddr_list[ns],
385*0Sstevel@tonic-gate 					sizeof (struct sockaddr)) != buflen) {
386*0Sstevel@tonic-gate #ifdef DEBUG
387*0Sstevel@tonic-gate 						if (_res.options & RES_DEBUG)
388*0Sstevel@tonic-gate 							perror("sendto");
389*0Sstevel@tonic-gate #endif DEBUG
390*0Sstevel@tonic-gate 						continue;
391*0Sstevel@tonic-gate 					}
392*0Sstevel@tonic-gate #if	BSD >= 43
393*0Sstevel@tonic-gate 				}
394*0Sstevel@tonic-gate #endif
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate 				/*
397*0Sstevel@tonic-gate 				 * Wait for reply
398*0Sstevel@tonic-gate 				 */
399*0Sstevel@tonic-gate 				timeout.tv_sec = (_res.retrans << try);
400*0Sstevel@tonic-gate 				if (try > 0)
401*0Sstevel@tonic-gate 					timeout.tv_sec /= _res.nscount;
402*0Sstevel@tonic-gate 				if (timeout.tv_sec <= 0)
403*0Sstevel@tonic-gate 					timeout.tv_sec = 1;
404*0Sstevel@tonic-gate 				timeout.tv_usec = 0;
405*0Sstevel@tonic-gate wait:
406*0Sstevel@tonic-gate 				FD_ZERO(&dsmask);
407*0Sstevel@tonic-gate 				FD_SET(s, &dsmask);
408*0Sstevel@tonic-gate 				n = select(s+1, &dsmask, (fd_set *)NULL,
409*0Sstevel@tonic-gate 						(fd_set *)NULL, &timeout);
410*0Sstevel@tonic-gate 				if (n < 0) {
411*0Sstevel@tonic-gate #ifdef DEBUG
412*0Sstevel@tonic-gate 					if (_res.options & RES_DEBUG)
413*0Sstevel@tonic-gate 						perror("select");
414*0Sstevel@tonic-gate #endif DEBUG
415*0Sstevel@tonic-gate 					continue;
416*0Sstevel@tonic-gate 				}
417*0Sstevel@tonic-gate 				if (n == 0) {
418*0Sstevel@tonic-gate 					/*
419*0Sstevel@tonic-gate 					 * timeout
420*0Sstevel@tonic-gate 					 */
421*0Sstevel@tonic-gate #ifdef DEBUG
422*0Sstevel@tonic-gate 					if (_res.options & RES_DEBUG)
423*0Sstevel@tonic-gate 						printf("timeout\n");
424*0Sstevel@tonic-gate #endif DEBUG
425*0Sstevel@tonic-gate #if BSD >= 43
426*0Sstevel@tonic-gate 					gotsomewhere = 1;
427*0Sstevel@tonic-gate #endif
428*0Sstevel@tonic-gate 					continue;
429*0Sstevel@tonic-gate 				}
430*0Sstevel@tonic-gate 				if ((resplen = recv(s, answer, anslen, 0))
431*0Sstevel@tonic-gate 									<= 0) {
432*0Sstevel@tonic-gate #ifdef DEBUG
433*0Sstevel@tonic-gate 					if (_res.options & RES_DEBUG)
434*0Sstevel@tonic-gate 						perror("recvfrom");
435*0Sstevel@tonic-gate #endif DEBUG
436*0Sstevel@tonic-gate 					continue;
437*0Sstevel@tonic-gate 				}
438*0Sstevel@tonic-gate 				gotsomewhere = 1;
439*0Sstevel@tonic-gate 				if (id != anhp->id) {
440*0Sstevel@tonic-gate 					/*
441*0Sstevel@tonic-gate 					 * response from old query, ignore it
442*0Sstevel@tonic-gate 					 */
443*0Sstevel@tonic-gate #ifdef DEBUG
444*0Sstevel@tonic-gate 					if (_res.options & RES_DEBUG) {
445*0Sstevel@tonic-gate 						printf("old answer:\n");
446*0Sstevel@tonic-gate 						p_query(answer);
447*0Sstevel@tonic-gate 					}
448*0Sstevel@tonic-gate #endif DEBUG
449*0Sstevel@tonic-gate 					goto wait;
450*0Sstevel@tonic-gate 				}
451*0Sstevel@tonic-gate 				if (!(_res.options & RES_IGNTC) && anhp->tc) {
452*0Sstevel@tonic-gate 					/*
453*0Sstevel@tonic-gate 					 * get rest of answer;
454*0Sstevel@tonic-gate 					 * use TCP with same server.
455*0Sstevel@tonic-gate 					 */
456*0Sstevel@tonic-gate #ifdef DEBUG
457*0Sstevel@tonic-gate 					if (_res.options & RES_DEBUG)
458*0Sstevel@tonic-gate 						printf("truncated answer\n");
459*0Sstevel@tonic-gate #endif DEBUG
460*0Sstevel@tonic-gate 					(void) close(s);
461*0Sstevel@tonic-gate 					s = -1;
462*0Sstevel@tonic-gate 					v_circuit = 1;
463*0Sstevel@tonic-gate 					goto usevc;
464*0Sstevel@tonic-gate 				}
465*0Sstevel@tonic-gate 			}
466*0Sstevel@tonic-gate #ifdef DEBUG
467*0Sstevel@tonic-gate 			if (_res.options & RES_DEBUG) {
468*0Sstevel@tonic-gate 				printf("got answer:\n");
469*0Sstevel@tonic-gate 				p_query(answer);
470*0Sstevel@tonic-gate 			}
471*0Sstevel@tonic-gate #endif DEBUG
472*0Sstevel@tonic-gate 		/*
473*0Sstevel@tonic-gate 		 * If using virtual circuits, we assume that the first server
474*0Sstevel@tonic-gate 		 * is preferred * over the rest (i.e. it is on the local
475*0Sstevel@tonic-gate 		 * machine) and only keep that one open.
476*0Sstevel@tonic-gate 		 * If we have temporarily opened a virtual circuit,
477*0Sstevel@tonic-gate 		 * or if we haven't been asked to keep a socket open,
478*0Sstevel@tonic-gate 		 * close the socket.
479*0Sstevel@tonic-gate 		 */
480*0Sstevel@tonic-gate 			if ((v_circuit &&
481*0Sstevel@tonic-gate 				((_res.options & RES_USEVC) == 0 || ns != 0)) ||
482*0Sstevel@tonic-gate 				(_res.options & RES_STAYOPEN) == 0) {
483*0Sstevel@tonic-gate 				(void) close(s);
484*0Sstevel@tonic-gate 				s = -1;
485*0Sstevel@tonic-gate 			}
486*0Sstevel@tonic-gate 			return (resplen);
487*0Sstevel@tonic-gate 		}
488*0Sstevel@tonic-gate 	}
489*0Sstevel@tonic-gate 	if (s >= 0) {
490*0Sstevel@tonic-gate 		(void) close(s);
491*0Sstevel@tonic-gate 		s = -1;
492*0Sstevel@tonic-gate 	}
493*0Sstevel@tonic-gate 	if (v_circuit == 0)
494*0Sstevel@tonic-gate 		if (gotsomewhere == 0)
495*0Sstevel@tonic-gate 			errno = ECONNREFUSED;	/* no nameservers found */
496*0Sstevel@tonic-gate 		else
497*0Sstevel@tonic-gate 			errno = ETIMEDOUT;	/* no answer obtained */
498*0Sstevel@tonic-gate 	else
499*0Sstevel@tonic-gate 		errno = terrno;
500*0Sstevel@tonic-gate 	return (-1);
501*0Sstevel@tonic-gate }
502*0Sstevel@tonic-gate 
503*0Sstevel@tonic-gate /*
504*0Sstevel@tonic-gate  * This routine is for closing the socket if a virtual circuit is used and
505*0Sstevel@tonic-gate  * the program wants to close it.  This provides support for endhostent()
506*0Sstevel@tonic-gate  * which expects to close the socket.
507*0Sstevel@tonic-gate  *
508*0Sstevel@tonic-gate  * This routine is not expected to be user visible.
509*0Sstevel@tonic-gate  */
510*0Sstevel@tonic-gate _res_close()
511*0Sstevel@tonic-gate {
512*0Sstevel@tonic-gate 	if (s != -1) {
513*0Sstevel@tonic-gate 		(void) close(s);
514*0Sstevel@tonic-gate 		s = -1;
515*0Sstevel@tonic-gate 	}
516*0Sstevel@tonic-gate }
517