res_send.c revision c16fc6609d9aa72229802524dc1d8c4ead6e9d2a
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* 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.
*/
/*
* Portions Copyright (c) 1996-1999 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.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
static const char sccsid[] = "@(#)res_send.c 8.1 (Berkeley) 6/4/93";
static const char rcsid[] = "$Id: res_send.c,v 8.52 2003/04/29 02:13:08 marka Exp $";
#endif /* LIBC_SCCS and not lint */
/*
* 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 SUNW_POLL
#include <stropts.h>
#include <poll.h>
#if defined(pselect)
#endif
#endif /* SUNW_POLL */
/* Options. Leave them on. */
#define DEBUG
#include "res_debug.h"
#include "res_private.h"
#ifdef ORIGINAL_ISC_CODE
#else
static int highestFD = 0;
#endif
/* Forward. */
u_char *, int, int *, int);
u_char *, int, int *, int,
int *, int *);
const struct sockaddr *, int);
#ifdef NEED_PSELECT
static int pselect(int, void *, void *, void *,
struct timespec *,
const sigset_t *);
#endif
#ifdef ORIGINAL_ISC_CODE
#else
#endif /* ORIGINAL_ISC_CODE */
/* Public. */
/* int
* res_isourserver(ina)
* looks up "ina" in _res.ns_addr_list[]
* returns:
* 0 : not found
* >0 : found
* author:
* 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);
}
/* 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
{
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
{
char abuf[NI_MAXHOST];
#ifdef SUNW_POLL
#endif
/* No name servers or previous res_ninit() 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;
struct sockaddr_storage peer;
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.
*/
union res_sockaddr_union inu;
struct sockaddr_in ina;
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 *
get_nsaddr(statp, n)
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;
connreset = 0;
truncating = 0;
/* Are we still talking to whom we want to talk to? */
struct sockaddr_storage peer;
}
}
}
return (-1);
}
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.
*/
/*
* Reset the value of resplen to anssiz,
* this is done because the caller assumes
* resplen contains the size of message read
* into the "ans" buffer passed in.
*/
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 nsaplen;
struct sockaddr_storage from;
#ifdef SUNW_POLL
int polltimeout;
#endif
}
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:
#ifdef SUNW_POLL
timeout = evConsTime(0, 0);
#else
else
timeout = evConsTime(0, 0);
#endif
if (n == 0) {
*gotsomewhere = 1;
return (0);
}
if (n < 0) {
goto wait;
#ifdef SUNW_POLL
#else
#endif
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
{
char hbuf[NI_MAXHOST];
char sbuf[NI_MAXSERV];
}
}
}
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;
}
}
#ifdef NEED_PSELECT
/* XXX needs to move to the porting library. */
static int
{
int n;
if (tsp) {
} else
if (sigmask)
if (sigmask)
if (tsp)
return (n);
}
#endif