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