/*
* Copyright (c) 1985, 1989, 1993
* The Regents of the University of California. All rights reserved.
*
* Portions copyright (c) 1999, 2000
* Intel Corporation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
*
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
*
* This product includes software developed by the University of
* California, Berkeley, Intel Corporation, and its contributors.
*
* 4. Neither the name of University, Intel Corporation, or their respective
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS, INTEL CORPORATION AND
* CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS,
* INTEL CORPORATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/*
* Portions Copyright (c) 1993 by Digital Equipment Corporation.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies, and that
* the name of Digital Equipment Corporation not be used in advertising or
* publicity pertaining to distribution of the document or software without
* specific, written prior permission.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
* WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
* CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
/*
* Portions Copyright (c) 1996 by Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
#endif /* LIBC_SCCS and not lint */
/*
* Send query to name server and wait for reply.
*/
#include <errno.h>
#include <netdb.h>
#include <resolv.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "res_config.h"
#ifndef _ORG_FREEBSD_
#define NOPOLL
#endif
#ifdef NOPOLL /* libc_r doesn't wrap poll yet() */
static int use_poll = 0;
#else
#include <poll.h>
/* 0 = not present, 1 = try it, 2 = exists */
#endif
static int s = -1; /* socket used for communications */
#ifndef DEBUG
#else
} else {}
static void
char *string,
int error,
struct sockaddr_in address
)
{
}
}
static void
char *string,
int error
)
{
}
}
#endif
void
)
{
}
void
)
{
}
/* int
* res_isourserver(ina)
* looks up "ina" in _res.ns_addr_list[]
* returns:
* 0 : not found
* >0 : found
* author:
* paul vixie, 29may94
*/
int
const struct sockaddr_in *inp
)
{
ret = 0;
ret++;
break;
}
}
return (ret);
}
/* int
* res_nameinquery(name, type, class, buf, eom)
* look for (name,type,class) in the query section of packet (buf,eom)
* requires:
* buf + HFIXEDSZ <= eom
* returns:
* -1 : format error
* 0 : not found
* >0 : found
* author:
* paul vixie, 29may94
*/
int
const char *name,
int type,
int class,
)
{
while (qdcount-- > 0) {
if (n < 0)
return (-1);
cp += n;
return (-1);
return (1);
}
return (0);
}
/* int
* res_queriesmatch(buf1, eom1, buf2, eom2)
* is there a 1:1 mapping of (name,type,class)
* in (buf1,eom1) and (buf2,eom2)?
* returns:
* -1 : format error
* 0 : not a 1:1 mapping
* >0 : is a 1:1 mapping
* author:
* paul vixie, 29may94
*/
int
)
{
return (-1);
/*
* Only header section present in replies to
* dynamic update packets.
*/
return (1);
return (0);
while (qdcount-- > 0) {
if (n < 0)
return (-1);
cp += n;
return (-1);
return (0);
}
return (1);
}
int
int buflen,
int anssiz
)
{
ssize_t n;
/* errno should have been set by res_init() in this case. */
return (-1);
}
return (-1);
}
gotsomewhere = 0;
connreset = 0;
badns = 0;
/*
* Send request, RETRY times, or until successful
*/
res_close();
goto next_ns;
}
if (Qhook) {
do {
switch (act) {
case res_goahead:
done = 1;
break;
case res_nextns:
res_close();
goto next_ns;
case res_done:
return (resplen);
case res_modified:
/* give the hook another try */
break;
/*FALLTHROUGH*/
case res_error:
/*FALLTHROUGH*/
default:
return (-1);
}
} while (!done);
}
(stdout, ";; Querying server (# %d) address = %s\n",
if (v_circuit) {
int truncated;
/*
* Use virtual circuit;
* at most one attempt per server.
*/
truncated = 0;
if (s >= 0)
res_close();
if (s < 0) {
return (-1);
}
errno = 0;
sizeof *nsap) < 0) {
res_close();
goto next_ns;
}
vc = 1;
}
/*
* Send length & message
*/
res_close();
goto next_ns;
}
/*
* Receive length & response
*/
cp += n;
if (len <= 0)
break;
}
if (n <= 0) {
res_close();
/*
* 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.
*/
connreset = 1;
res_close();
goto same_ns;
}
res_close();
goto next_ns;
}
(stdout, ";; response truncated\n")
);
truncated = 1;
} else
/*
* Undersized message.
*/
res_close();
goto next_ns;
}
while (len != 0 &&
cp += n;
}
if (n <= 0) {
res_close();
goto next_ns;
}
if (truncated) {
/*
* Flush rest of answer
* so connection stays in synch.
*/
while (len != 0) {
? sizeof(junk)
: len);
else
break;
}
}
/*
* The calling applicating has bailed out of
* a previous call and failed to arrange to have
* the circuit closed or the server has got
* itself confused. Anyway drop the packet and
* wait for the correct one.
*/
(stdout, ";; old answer (unexpected):\n"),
goto read_len;
}
} else {
/*
* Use datagrams.
*/
#ifndef NOPOLL
int msec;
#endif
int dsmasklen;
int fromlen;
if ((s < 0) || vc) {
if (vc)
res_close();
if (s < 0) {
#ifndef CAN_RECONNECT
#endif
return (-1);
}
connected = 0;
}
#ifndef CANNOT_CONNECT_DGRAM
/*
* 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.
*/
/*
* Connect only if we are sure we won't
* receive a response from another server.
*/
if (!connected) {
sizeof *nsap
) < 0) {
"connect(dg)",
res_close();
goto next_ns;
}
connected = 1;
}
res_close();
goto next_ns;
}
} else {
/*
* Disconnect if we want to listen
* for responses from more than one server.
*/
if (connected) {
#ifdef CAN_RECONNECT
(void) connect(s,
(struct sockaddr *)
&no_addr,
sizeof no_addr);
#else
if (s1 < 0)
goto bad_dg_sock;
(stdout, ";; new DG socket\n"))
#endif /* CAN_RECONNECT */
connected = 0;
errno = 0;
}
#endif /* !CANNOT_CONNECT_DGRAM */
sizeof *nsap)
!= buflen) {
res_close();
goto next_ns;
}
#ifndef CANNOT_CONNECT_DGRAM
}
#endif /* !CANNOT_CONNECT_DGRAM */
/*
* Wait for reply
*/
#ifndef NOPOLL
if (use_poll) {
if (try > 0)
if (msec <= 0)
msec = 1000;
} else {
#endif
if (try > 0)
#ifndef NOPOLL
}
#endif
wait:
if (s < 0) {
res_close();
goto next_ns;
}
#ifndef NOPOLL
if (use_poll) {
int sigsys_installed = 0;
if (use_poll == 1) {
sigsys_installed = 1;
}
if (sigsys_installed == 1) {
}
/* XXX why does nosys() return EINVAL? */
use_poll = 0;
goto othersyscall;
} else if (use_poll == 1)
use_poll = 2;
if (n < 0) {
goto wait;
res_close();
goto next_ns;
}
} else {
#endif
sizeof(fd_mask);
res_close();
goto next_ns;
}
} else
/* only zero what we need */
if (n < 0) {
goto wait;
res_close();
goto next_ns;
}
#ifndef NOPOLL
}
#endif
if (n == 0) {
/*
* timeout
*/
(stdout, ";; timeout\n"));
gotsomewhere = 1;
res_close();
goto next_ns;
}
errno = 0;
fromlen = sizeof(struct sockaddr_in);
if (resplen <= 0) {
res_close();
goto next_ns;
}
gotsomewhere = 1;
/*
* Undersized message.
*/
(stdout, ";; undersized: %d\n",
resplen));
res_close();
goto next_ns;
}
/*
* response from old query, ignore it.
* XXX - potential security hazard could
* be detected here.
*/
(stdout, ";; old answer:\n"),
goto wait;
}
#ifdef CHECK_SRVR_ADDR
!res_isourserver(&from)) {
/*
* response from wrong server? ignore it.
* XXX - potential security hazard could
* be detected here.
*/
(stdout, ";; not our server:\n"),
goto wait;
}
#endif
/*
* response contains wrong query? ignore it.
* XXX - potential security hazard could
* be detected here.
*/
(stdout, ";; wrong query name:\n"),
goto wait;
}
(stdout, "server rejected query:\n"),
res_close();
/* don't retry if called from dig */
goto next_ns;
}
/*
* get rest of answer;
* use TCP with same server.
*/
(stdout, ";; truncated answer\n"));
v_circuit = 1;
res_close();
goto same_ns;
}
(stdout, ";; got answer:\n"));
(stdout, ""),
/*
* 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.
*/
res_close();
}
if (Rhook) {
do {
switch (act) {
case res_goahead:
case res_done:
done = 1;
break;
case res_nextns:
res_close();
goto next_ns;
case res_modified:
/* give the hook another try */
break;
/*FALLTHROUGH*/
case res_error:
/*FALLTHROUGH*/
default:
return (-1);
}
} while (!done);
}
return (resplen);
next_ns: ;
} /*foreach ns*/
} /*foreach retry*/
res_close();
if (!v_circuit) {
if (!gotsomewhere)
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 >= 0) {
(void) close(s);
s = -1;
connected = 0;
vc = 0;
}
}