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