/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Copyright 2014 Nexenta Systems, Inc. All rights reserved.
*/
/*
* Portions Copyright (C) 2004-2009 Internet Systems Consortium, Inc. ("ISC")
* Portions Copyright (C) 1996-2003 Internet Software Consortium.
*
* 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 ISC DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL ISC 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.
*/
/*
* Copyright (c) 1985, 1989, 1993
* The Regents of the University of California. 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 and its contributors.
* 4. Neither the name of the University nor the names of its 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 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 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.
*/
#endif /* LIBC_SCCS and not lint */
/*! \file
* \brief
* Send query to name server and wait for reply.
*/
#include "port_before.h"
#include "fd_setsize.h"
#include <errno.h>
#include <netdb.h>
#include <resolv.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <isc/eventlib.h>
#include "port_after.h"
#ifdef USE_POLL
#ifdef HAVE_STROPTS_H
#include <stropts.h>
#endif
#include <poll.h>
#endif /* USE_POLL */
/* Options. Leave them on. */
#define DEBUG
#include "res_debug.h"
#include "res_private.h"
#ifndef USE_POLL
#else
static int highestFD = 0;
#endif
/* Forward. */
u_char *, int, int *, int);
u_char *, int, int *, int, int,
int *, int *);
const struct sockaddr *, int);
#if defined(NEED_PSELECT) && !defined(USE_POLL)
static int pselect(int, void *, void *, void *,
struct timespec *,
const sigset_t *);
#endif
#ifndef ORIGINAL_ISC_CODE
#endif /* ORIGINAL_ISC_CODE */
/* Public. */
/*%
* looks up "ina" in _res.ns_addr_list[]
*
* returns:
*\li 0 : not found
*\li >0 : found
*
* author:
*\li paul vixie, 29may94
*/
int
int ns;
case AF_INET:
return (1);
}
break;
case AF_INET6:
break;
#ifdef HAVE_SIN6_SCOPE_ID
(srv6->sin6_scope_id == 0 ||
#endif
return (1);
}
break;
default:
break;
}
return (0);
}
/*%
* look for (name,type,class) in the query section of packet (buf,eom)
*
* requires:
*\li buf + HFIXEDSZ <= eom
*
* returns:
*\li -1 : format error
*\li 0 : not found
*\li >0 : found
*
* author:
*\li paul vixie, 29may94
*/
int
{
while (qdcount-- > 0) {
if (n < 0)
return (-1);
cp += n;
return (-1);
return (1);
}
return (0);
}
/*%
* is there a 1:1 mapping of (name,type,class)
* in (buf1,eom1) and (buf2,eom2)?
*
* returns:
*\li -1 : format error
*\li 0 : not a 1:1 mapping
*\li >0 : is a 1:1 mapping
*
* author:
*\li 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
{
#ifdef USE_POLL
#endif
/* No name servers or res_init() failure */
return (-1);
}
return (-1);
}
gotsomewhere = 0;
/*
* If the ns_addr_list in the resolver context has changed, then
* invalidate our cached copy and the associated timing data.
*/
int needclose = 0;
needclose++;
else
needclose++;
break;
}
continue;
needclose++;
break;
}
needclose++;
break;
}
}
if (needclose) {
}
}
/*
* Maybe initialize our private copy of the ns_addr_list.
*/
continue;
}
}
/*
* Some resolvers want to even out the load on their nameservers.
* Note that RES_BLAST overrides RES_ROTATE.
*/
int fd;
}
}
/*
* Send request, RETRY times, or until successful.
*/
int nsaplen;
do {
switch (act) {
case res_goahead:
done = 1;
break;
case res_nextns:
goto next_ns;
case res_done:
return (resplen);
case res_modified:
/* give the hook another try */
break;
/*FALLTHROUGH*/
case res_error:
/*FALLTHROUGH*/
default:
goto fail;
}
} while (!done);
}
(stdout, ";; Querying server (# %d) address = %s\n",
if (v_circuit) {
/* Use VC; at most one attempt per server. */
ns);
if (n < 0)
goto fail;
if (n == 0)
goto next_ns;
resplen = n;
} else {
/* Use datagrams. */
if (n < 0)
goto fail;
if (n == 0)
goto next_ns;
if (v_circuit)
goto same_ns;
resplen = n;
}
(stdout, ";; got answer:\n"));
/*
* If we have temporarily opened a virtual circuit,
* or if we haven't been asked to keep a socket open,
* close the socket.
*/
}
do {
switch (act) {
case res_goahead:
case res_done:
done = 1;
break;
case res_nextns:
goto next_ns;
case res_modified:
/* give the hook another try */
break;
/*FALLTHROUGH*/
case res_error:
/*FALLTHROUGH*/
default:
goto fail;
}
} while (!done);
}
return (resplen);
next_ns: ;
} /*foreach ns*/
} /*foreach retry*/
if (!v_circuit) {
if (!gotsomewhere)
else
} else
return (-1);
fail:
return (-1);
}
/* Private */
static int
{
#ifdef HAVE_SA_LEN
/* There are people do not set sa_len. Be forgiving to them. */
#endif
return (sizeof(struct sockaddr_in));
return (sizeof(struct sockaddr_in6));
else
return (0); /*%< unknown, die on connect */
}
/*%
* pick appropriate nsaddr_list for use. see res_init() for initialization.
*/
static struct sockaddr *
size_t n;
{
/*
* - EXT(statp).ext->nsaddrs[n] holds an address that is larger
* than struct sockaddr, and
* - user code did not update statp->nsaddr_list[n].
*/
} else {
/*
* - user code updated statp->nsaddr_list[n], or
* - statp->nsaddr_list[n] has the same content as
* EXT(statp).ext->nsaddrs[n].
*/
}
}
static int
{
int nsaplen;
void *tmp;
#ifdef SO_NOSIGPIPE
#endif
connreset = 0;
truncating = 0;
/* Are we still talking to whom we want to talk to? */
}
}
}
switch (errno) {
case EPROTONOSUPPORT:
#ifdef EPFNOSUPPORT
case EPFNOSUPPORT:
#endif
case EAFNOSUPPORT:
return (0);
default:
return (-1);
}
}
#ifdef SO_NOSIGPIPE
/*
* Disable generation of SIGPIPE when writing to a closed
* socket. Write should return -1 and set errno to EPIPE
* instead.
*
* Push on even if setsockopt(SO_NOSIGPIPE) fails.
*/
sizeof(on));
#endif
#ifdef TCP_CONN_ABORT_THRESHOLD
/*
* The default connection timeout is over two minutes.
* We need something more reasonable here. The default
* retrans value is 5 sec., then 10, 20, 40, on retries.
* TCP connect does its own retries, so we want just one
* reasonable timeout value. Using 2X retrans, which
* gives us a 10 sec. connect timeout. If we're waiting
* that long to connect, we probably want to give up and
* try the next DNS server in our list.
*
* It might be reasonable to do this for all callers,
* but for now do it only when we see MS_INTEROP in the
* environment (set in smbd and idmapd)
*/
int conn_tmo;
sizeof(conn_tmo));
}
#endif
errno = 0;
nsaplen);
return (0);
}
}
/*
* Send length & message
*/
return (0);
}
/*
* Receive length & response
*/
cp += n;
if ((len -= n) == 0)
break;
}
if (n <= 0) {
/*
* 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;
goto same_ns;
}
return (0);
}
(stdout, ";; response truncated\n")
);
truncating = 1;
} else
/*
* Undersized message.
*/
return (0);
}
cp += n;
len -= n;
}
if (n <= 0) {
return (0);
}
if (truncating) {
/*
* Flush rest of answer so connection stays in synch.
*/
while (len != 0) {
if (n > 0)
len -= n;
else
break;
}
}
/*
* If 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, then drop the packet and
* wait for the correct one.
*/
(stdout, ";; old answer (unexpected):\n"),
goto read_len;
}
/*
* All is well, or the error is fatal. Signal that the
* next nameserver ought not be tried.
*/
return (resplen);
}
static int
int *gotsomewhere)
{
int nsaplen;
#ifdef USE_POLL
int polltimeout;
#else
#endif
}
switch (errno) {
case EPROTONOSUPPORT:
#ifdef EPFNOSUPPORT
case EPFNOSUPPORT:
#endif
case EAFNOSUPPORT:
return (0);
default:
return (-1);
}
}
#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.
*/
nsaplen);
return (0);
}
#endif /* !CANNOT_CONNECT_DGRAM */
(stdout, ";; new DG socket\n"))
}
#ifndef CANNOT_CONNECT_DGRAM
return (0);
}
#else /* !CANNOT_CONNECT_DGRAM */
{
return (0);
}
#endif /* !CANNOT_CONNECT_DGRAM */
/*
* Wait for reply.
*/
if (ns > 0)
if (seconds <= 0)
seconds = 1;
goto nonow;
wait:
#ifndef USE_POLL
else
timeout = evConsTime(0, 0);
#else
timeout = evConsTime(0, 0);
#endif /* USE_POLL */
if (n == 0) {
*gotsomewhere = 1;
return (0);
}
if (n < 0) {
goto wait;
#ifndef USE_POLL
#else
#endif /* USE_POLL */
return (0);
}
errno = 0;
if (resplen <= 0) {
return (0);
}
*gotsomewhere = 1;
/*
* Undersized message.
*/
(stdout, ";; undersized: %d\n",
resplen));
return (0);
}
/*
* response from old query, ignore it.
* XXX - potential security hazard could
* be detected here.
*/
(stdout, ";; old answer:\n"),
goto wait;
}
/*
* response from wrong server? ignore it.
* XXX - potential security hazard could
* be detected here.
*/
(stdout, ";; not our server:\n"),
goto wait;
}
#ifdef RES_USE_EDNS0
/*
* Do not retry if the server do not understand EDNS0.
* The case has to be captured here, as FORMERR packet do not
* carry query section, hence res_queriesmatch() returns 0.
*/
(stdout, "server rejected query with EDNS0:\n"),
/* record the error */
return (0);
}
#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"),
/* don't retry if called from dig */
return (0);
}
/*
* To get the rest of answer,
* use TCP with same server.
*/
(stdout, ";; truncated answer\n"));
*v_circuit = 1;
return (1);
}
/*
* All is well, or the error is fatal. Signal that the
* next nameserver ought not be tried.
*/
return (resplen);
}
static void
{
}
}
}
static void
}
static int
return 0;
switch (a->sa_family) {
case AF_INET:
a4 = (struct sockaddr_in *)a;
b4 = (struct sockaddr_in *)b;
case AF_INET6:
a6 = (struct sockaddr_in6 *)a;
b6 = (struct sockaddr_in6 *)b;
#ifdef HAVE_SIN6_SCOPE_ID
#endif
default:
return 0;
}
}
#if defined(NEED_PSELECT) && !defined(USE_POLL)
/* XXX needs to move to the porting library. */
static int
{
int n;
if (tsp) {
} else
if (sigmask)
if (sigmask)
if (tsp)
return (n);
}
#endif