/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2015 Gary Mills
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* University Copyright- Copyright (c) 1982, 1986, 1988
* The Regents of the University of California
* All Rights Reserved
*
* University Acknowledgment- Portions of this document are derived from
* software developed by the University of California, Berkeley, and its
* contributors.
*/
/*
* Send query to name server and wait for reply.
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <resolv.h>
#include "crossl.h"
/*
* Undocumented external function in libsocket
*/
extern int
_socket(int, int, int);
static int s = -1; /* socket used for communications */
#if BSD >= 43
#endif /* BSD */
#ifndef FD_SET
#ifdef SYSV
#else
#endif
#endif
/*
* 1247019: Kludge to time out quickly if there is no /etc/resolv.conf
* and a TCP connection to the local DNS server fails.
*/
static int _confcheck()
{
int ns;
/* First, we check to see if /etc/resolv.conf exists.
* If it doesn't, then localhost is mostlikely to be
* the nameserver.
*/
/* Next, we check to see if _res.nsaddr is set to loopback.
* If it isn't, it has been altered by the application
* explicitly and we then want to bail with success.
*/
/* Lastly, we try to connect to the TCP port of the
* nameserver. If this fails, then we know that
* DNS is misconfigured and we can quickly exit.
*/
sizeof ns_sin) == -1) {
return(-1);
}
else {
return(0);
}
}
return(0);
}
return (0);
}
int
char *buf;
int buflen;
char *answer;
int anslen;
{
register int n;
int gotsomewhere = 0;
#if BSD >= 43
int connected = 0;
#endif /* BSD */
int connreset = 0;
char *cp;
#ifdef DEBUG
printf("res_send()\n");
}
#endif
if (res_init() == -1) {
return (-1);
}
/* 1247019: Check to see if we can bailout quickly. */
if (_confcheck() == -1)
return(-1);
/*
* Send request, RETRY times, or until successful
*/
#ifdef DEBUG
printf("Querying server (# %d) address = %s\n",
#endif
if (v_circuit) {
int truncated = 0;
/*
* Use virtual circuit;
* at most one attempt per server.
*/
if (s < 0) {
if (s < 0) {
#ifdef DEBUG
perror("socket (vc) failed");
}
#endif
continue;
}
sizeof (struct sockaddr)) < 0) {
#ifdef DEBUG
perror("connect failed");
}
#endif
(void) close(s);
s = -1;
continue;
}
}
/*
* Send length & message
*/
buflen) {
#ifdef DEBUG
perror("write failed");
#endif
(void) close(s);
s = -1;
continue;
}
/*
* Receive length & response
*/
len = sizeof (short);
cp += n;
len -= n;
}
if (n <= 0) {
#ifdef DEBUG
perror("read failed");
#endif
(void) close(s);
s = -1;
/*
* A long running process might get its TCP
* connection reset if the remote server was
* restarted. Requery the server instead of
* trying a new one. When there is only one
* server, this means that a query might work
* instead of failing. We only allow one reset
* per query to prevent looping.
*/
if (terrno == ECONNRESET &&
!connreset) {
connreset = 1;
ns--;
}
continue;
}
anslen) {
#ifdef DEBUG
"response truncated\n");
#endif
truncated = 1;
} else
while (len != 0 &&
(int)len)) > 0) {
cp += n;
len -= n;
}
if (n <= 0) {
#ifdef DEBUG
perror("read failed");
#endif
(void) close(s);
s = -1;
continue;
}
if (truncated) {
/*
* Flush rest of answer
* so connection stays in synch.
*/
/*
* set the value of resplen to anslen,
* this is done because the caller
* assumes resplen contains the size of
* message read into the "answer" buffer
* passed in.
*/
while (len != 0) {
len -= n;
else
break;
}
}
} else {
/*
* Use datagrams.
*/
if (s < 0) {
if (s < 0) {
#ifdef DEBUG
perror("socket (dg) failed");
}
#endif
continue;
}
}
#if BSD >= 43
/*
* I'm tired of answering this question, so:
* On a 4.3BSD+ machine (client and server,
* actually), sending to a nameserver datagram
* port with no nameserver will cause an
* ICMP port unreachable message to be returned.
* If our datagram socket is "connected" to the
* server, we get an ECONNREFUSED error on the next
* socket operation, and select returns if the
* error message is received. We can thus detect
* the absence of a nameserver without timing out.
* If we have sent queries to at least two servers,
* however, we don't want to remain connected,
* as we wish to receive answers from the first
* server to respond.
*/
/*
* Don't use connect if we might
* still receive a response
* from another server.
*/
if (connected == 0) {
if (connect(s,
sizeof (struct sockaddr)) < 0) {
#ifdef DEBUG
RES_DEBUG) {
perror("connect");
}
#endif
continue;
}
connected = 1;
}
#ifdef DEBUG
perror("send");
#endif
continue;
}
} else {
/*
* Disconnect if we want to listen for
* responses from more than one server.
*/
if (connected) {
sizeof (no_addr));
connected = 0;
}
#endif /* BSD */
#ifdef DEBUG
perror("sendto");
#endif
continue;
}
#if BSD >= 43
}
#endif
/*
* Wait for reply
*/
if (try > 0)
wait:
if (n < 0) {
#ifdef DEBUG
perror("select");
#endif
continue;
}
if (n == 0) {
/*
* timeout
*/
#ifdef DEBUG
printf("timeout\n");
#endif
#if BSD >= 43
gotsomewhere = 1;
#endif
continue;
}
<= 0) {
#ifdef DEBUG
perror("recvfrom");
#endif
continue;
}
gotsomewhere = 1;
/*
* response from old query, ignore it
*/
#ifdef DEBUG
printf("old answer:\n");
}
#endif
goto wait;
}
/*
* get rest of answer;
* use TCP with same server.
*/
#ifdef DEBUG
printf("truncated answer\n");
#endif
(void) close(s);
s = -1;
v_circuit = 1;
goto usevc;
}
}
#ifdef DEBUG
printf("got answer:\n");
}
#endif
/*
* If using virtual circuits, we assume that the first server
* is preferred * over the rest (i.e. it is on the local
* machine) and only keep that one open.
* If we have temporarily opened a virtual circuit,
* or if we haven't been asked to keep a socket open,
* close the socket.
*/
if ((v_circuit &&
(void) close(s);
s = -1;
}
return (resplen);
}
}
if (s >= 0) {
(void) close(s);
s = -1;
}
if (v_circuit == 0)
if (gotsomewhere == 0)
else
else
return (-1);
}
/*
* This routine is for closing the socket if a virtual circuit is used and
* the program wants to close it. This provides support for endhostent()
* which expects to close the socket.
*
* This routine is not expected to be user visible.
*/
void
{
if (s != -1) {
(void) close(s);
s = -1;
}
}