2003-10-07 23:54:04 +02:00
|
|
|
/* Copyright 1998 by the Massachusetts Institute of Technology.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this
|
|
|
|
* software and its documentation for any purpose and without
|
|
|
|
* fee is hereby granted, provided that the above copyright
|
|
|
|
* notice appear in all copies and that both that copyright
|
|
|
|
* notice and this permission notice appear in supporting
|
|
|
|
* documentation, and that the name of M.I.T. not be used in
|
|
|
|
* advertising or publicity pertaining to distribution of the
|
|
|
|
* software without specific, written prior permission.
|
|
|
|
* M.I.T. makes no representations about the suitability of
|
|
|
|
* this software for any purpose. It is provided "as is"
|
|
|
|
* without express or implied warranty.
|
|
|
|
*/
|
|
|
|
|
2004-07-23 00:18:45 +02:00
|
|
|
#include "setup.h"
|
2003-10-07 23:54:04 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2004-08-20 15:45:26 +02:00
|
|
|
#if defined(WIN32) && !defined(WATT32)
|
2003-10-07 23:54:04 +02:00
|
|
|
#include "nameser.h"
|
2004-08-20 15:45:26 +02:00
|
|
|
|
2003-10-07 23:54:04 +02:00
|
|
|
#else
|
|
|
|
#include <sys/socket.h>
|
2004-08-20 15:45:26 +02:00
|
|
|
#ifdef HAVE_SYS_UIO_H
|
2003-10-07 23:54:04 +02:00
|
|
|
#include <sys/uio.h>
|
2004-08-20 15:45:26 +02:00
|
|
|
#endif
|
2003-10-07 23:54:04 +02:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <arpa/nameser.h>
|
2005-04-07 00:27:42 +02:00
|
|
|
#ifdef HAVE_ARPA_NAMESER_COMPAT_H
|
|
|
|
#include <arpa/nameser_compat.h>
|
|
|
|
#endif
|
2004-08-20 15:45:26 +02:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2003-10-07 23:54:04 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2005-06-03 09:39:18 +02:00
|
|
|
#ifdef HAVE_SYS_IOCTL_H
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#endif
|
2005-08-10 19:03:53 +02:00
|
|
|
#ifdef NETWARE
|
|
|
|
#include <sys/filio.h>
|
|
|
|
#endif
|
2004-08-20 15:45:26 +02:00
|
|
|
#endif
|
2003-10-07 23:54:04 +02:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <errno.h>
|
2004-08-20 15:45:26 +02:00
|
|
|
|
2003-10-07 23:54:04 +02:00
|
|
|
#include "ares.h"
|
|
|
|
#include "ares_dns.h"
|
|
|
|
#include "ares_private.h"
|
|
|
|
|
2005-06-02 23:10:21 +02:00
|
|
|
#ifndef TRUE
|
|
|
|
/* at least Solaris 7 does not have TRUE at this point */
|
|
|
|
#define TRUE 1
|
|
|
|
#endif
|
|
|
|
|
2004-08-20 16:07:11 +02:00
|
|
|
#if (defined(WIN32) || defined(WATT32)) && !defined(MSDOS)
|
2004-07-01 15:54:24 +02:00
|
|
|
#define GET_ERRNO() WSAGetLastError()
|
|
|
|
#else
|
|
|
|
#define GET_ERRNO() errno
|
|
|
|
#endif
|
|
|
|
|
2003-10-07 23:54:04 +02:00
|
|
|
static void write_tcp_data(ares_channel channel, fd_set *write_fds,
|
2004-10-06 09:50:18 +02:00
|
|
|
time_t now);
|
2003-10-07 23:54:04 +02:00
|
|
|
static void read_tcp_data(ares_channel channel, fd_set *read_fds, time_t now);
|
|
|
|
static void read_udp_packets(ares_channel channel, fd_set *read_fds,
|
2004-10-06 09:50:18 +02:00
|
|
|
time_t now);
|
2003-10-07 23:54:04 +02:00
|
|
|
static void process_timeouts(ares_channel channel, time_t now);
|
|
|
|
static void process_answer(ares_channel channel, unsigned char *abuf,
|
2004-10-06 09:50:18 +02:00
|
|
|
int alen, int whichserver, int tcp, int now);
|
2003-10-07 23:54:04 +02:00
|
|
|
static void handle_error(ares_channel channel, int whichserver, time_t now);
|
2004-07-24 23:47:49 +02:00
|
|
|
static struct query *next_server(ares_channel channel, struct query *query, time_t now);
|
2003-10-07 23:54:04 +02:00
|
|
|
static int open_tcp_socket(ares_channel channel, struct server_state *server);
|
|
|
|
static int open_udp_socket(ares_channel channel, struct server_state *server);
|
|
|
|
static int same_questions(const unsigned char *qbuf, int qlen,
|
2004-10-06 09:50:18 +02:00
|
|
|
const unsigned char *abuf, int alen);
|
2004-07-24 23:47:49 +02:00
|
|
|
static struct query *end_query(ares_channel channel, struct query *query, int status,
|
2004-10-06 09:50:18 +02:00
|
|
|
unsigned char *abuf, int alen);
|
2003-10-07 23:54:04 +02:00
|
|
|
|
|
|
|
/* Something interesting happened on the wire, or there was a timeout.
|
|
|
|
* See what's up and respond accordingly.
|
|
|
|
*/
|
|
|
|
void ares_process(ares_channel channel, fd_set *read_fds, fd_set *write_fds)
|
|
|
|
{
|
|
|
|
time_t now;
|
|
|
|
|
|
|
|
time(&now);
|
|
|
|
write_tcp_data(channel, write_fds, now);
|
|
|
|
read_tcp_data(channel, read_fds, now);
|
|
|
|
read_udp_packets(channel, read_fds, now);
|
|
|
|
process_timeouts(channel, now);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If any TCP sockets select true for writing, write out queued data
|
|
|
|
* we have for them.
|
|
|
|
*/
|
|
|
|
static void write_tcp_data(ares_channel channel, fd_set *write_fds, time_t now)
|
|
|
|
{
|
|
|
|
struct server_state *server;
|
|
|
|
struct send_request *sendreq;
|
|
|
|
struct iovec *vec;
|
2004-07-23 00:18:45 +02:00
|
|
|
int i;
|
2004-07-29 09:24:39 +02:00
|
|
|
ssize_t scount;
|
|
|
|
int wcount;
|
2004-07-23 00:18:45 +02:00
|
|
|
size_t n;
|
2003-10-07 23:54:04 +02:00
|
|
|
|
|
|
|
for (i = 0; i < channel->nservers; i++)
|
|
|
|
{
|
|
|
|
/* Make sure server has data to send and is selected in write_fds. */
|
|
|
|
server = &channel->servers[i];
|
2004-07-23 00:18:45 +02:00
|
|
|
if (!server->qhead || server->tcp_socket == ARES_SOCKET_BAD
|
2004-10-06 09:50:18 +02:00
|
|
|
|| !FD_ISSET(server->tcp_socket, write_fds))
|
|
|
|
continue;
|
2003-10-07 23:54:04 +02:00
|
|
|
|
|
|
|
/* Count the number of send queue items. */
|
|
|
|
n = 0;
|
|
|
|
for (sendreq = server->qhead; sendreq; sendreq = sendreq->next)
|
2004-10-06 09:50:18 +02:00
|
|
|
n++;
|
2003-10-07 23:54:04 +02:00
|
|
|
|
|
|
|
/* Allocate iovecs so we can send all our data at once. */
|
|
|
|
vec = malloc(n * sizeof(struct iovec));
|
|
|
|
if (vec)
|
2004-10-06 09:50:18 +02:00
|
|
|
{
|
|
|
|
/* Fill in the iovecs and send. */
|
|
|
|
n = 0;
|
|
|
|
for (sendreq = server->qhead; sendreq; sendreq = sendreq->next)
|
|
|
|
{
|
|
|
|
vec[n].iov_base = (char *) sendreq->data;
|
|
|
|
vec[n].iov_len = sendreq->len;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
wcount = writev(server->tcp_socket, vec, n);
|
|
|
|
free(vec);
|
|
|
|
if (wcount < 0)
|
|
|
|
{
|
|
|
|
handle_error(channel, i, now);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Advance the send queue by as many bytes as we sent. */
|
|
|
|
while (wcount)
|
|
|
|
{
|
|
|
|
sendreq = server->qhead;
|
|
|
|
if ((size_t)wcount >= sendreq->len)
|
|
|
|
{
|
|
|
|
wcount -= sendreq->len;
|
|
|
|
server->qhead = sendreq->next;
|
|
|
|
if (server->qhead == NULL)
|
|
|
|
server->qtail = NULL;
|
|
|
|
free(sendreq);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sendreq->data += wcount;
|
|
|
|
sendreq->len -= wcount;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-10-07 23:54:04 +02:00
|
|
|
else
|
2004-10-06 09:50:18 +02:00
|
|
|
{
|
|
|
|
/* Can't allocate iovecs; just send the first request. */
|
|
|
|
sendreq = server->qhead;
|
2003-10-07 23:54:04 +02:00
|
|
|
|
2005-11-25 23:14:53 +01:00
|
|
|
scount = send(server->tcp_socket, (void *)sendreq->data,
|
|
|
|
sendreq->len, 0);
|
2003-10-07 23:54:04 +02:00
|
|
|
|
2004-10-06 09:50:18 +02:00
|
|
|
if (scount < 0)
|
|
|
|
{
|
|
|
|
handle_error(channel, i, now);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Advance the send queue by as many bytes as we sent. */
|
|
|
|
if ((size_t)scount == sendreq->len)
|
|
|
|
{
|
|
|
|
server->qhead = sendreq->next;
|
|
|
|
if (server->qhead == NULL)
|
|
|
|
server->qtail = NULL;
|
|
|
|
free(sendreq);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sendreq->data += scount;
|
|
|
|
sendreq->len -= scount;
|
|
|
|
}
|
|
|
|
}
|
2003-10-07 23:54:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If any TCP socket selects true for reading, read some data,
|
|
|
|
* allocate a buffer if we finish reading the length word, and process
|
|
|
|
* a packet if we finish reading one.
|
|
|
|
*/
|
|
|
|
static void read_tcp_data(ares_channel channel, fd_set *read_fds, time_t now)
|
|
|
|
{
|
|
|
|
struct server_state *server;
|
|
|
|
int i, count;
|
|
|
|
|
|
|
|
for (i = 0; i < channel->nservers; i++)
|
|
|
|
{
|
|
|
|
/* Make sure the server has a socket and is selected in read_fds. */
|
|
|
|
server = &channel->servers[i];
|
2004-07-23 00:18:45 +02:00
|
|
|
if (server->tcp_socket == ARES_SOCKET_BAD ||
|
|
|
|
!FD_ISSET(server->tcp_socket, read_fds))
|
2004-10-06 09:50:18 +02:00
|
|
|
continue;
|
2003-10-07 23:54:04 +02:00
|
|
|
|
|
|
|
if (server->tcp_lenbuf_pos != 2)
|
2004-10-06 09:50:18 +02:00
|
|
|
{
|
|
|
|
/* We haven't yet read a length word, so read that (or
|
|
|
|
* what's left to read of it).
|
|
|
|
*/
|
2003-10-07 23:54:04 +02:00
|
|
|
count = recv(server->tcp_socket,
|
2005-11-25 23:14:53 +01:00
|
|
|
(void *)(server->tcp_lenbuf + server->tcp_buffer_pos),
|
2003-10-07 23:54:04 +02:00
|
|
|
2 - server->tcp_buffer_pos, 0);
|
2004-10-06 09:50:18 +02:00
|
|
|
if (count <= 0)
|
|
|
|
{
|
|
|
|
handle_error(channel, i, now);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
server->tcp_lenbuf_pos += count;
|
|
|
|
if (server->tcp_lenbuf_pos == 2)
|
|
|
|
{
|
|
|
|
/* We finished reading the length word. Decode the
|
2003-10-07 23:54:04 +02:00
|
|
|
* length and allocate a buffer for the data.
|
2004-10-06 09:50:18 +02:00
|
|
|
*/
|
|
|
|
server->tcp_length = server->tcp_lenbuf[0] << 8
|
|
|
|
| server->tcp_lenbuf[1];
|
|
|
|
server->tcp_buffer = malloc(server->tcp_length);
|
|
|
|
if (!server->tcp_buffer)
|
|
|
|
handle_error(channel, i, now);
|
|
|
|
server->tcp_buffer_pos = 0;
|
|
|
|
}
|
|
|
|
}
|
2003-10-07 23:54:04 +02:00
|
|
|
else
|
2004-10-06 09:50:18 +02:00
|
|
|
{
|
|
|
|
/* Read data into the allocated buffer. */
|
|
|
|
count = recv(server->tcp_socket,
|
2005-11-25 23:14:53 +01:00
|
|
|
(void *)(server->tcp_buffer + server->tcp_buffer_pos),
|
2004-10-06 09:50:18 +02:00
|
|
|
server->tcp_length - server->tcp_buffer_pos, 0);
|
|
|
|
if (count <= 0)
|
|
|
|
{
|
|
|
|
handle_error(channel, i, now);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
server->tcp_buffer_pos += count;
|
|
|
|
if (server->tcp_buffer_pos == server->tcp_length)
|
|
|
|
{
|
|
|
|
/* We finished reading this answer; process it and
|
2003-10-07 23:54:04 +02:00
|
|
|
* prepare to read another length word.
|
2004-10-06 09:50:18 +02:00
|
|
|
*/
|
|
|
|
process_answer(channel, server->tcp_buffer, server->tcp_length,
|
|
|
|
i, 1, now);
|
2004-08-20 15:45:26 +02:00
|
|
|
if (server->tcp_buffer)
|
2004-10-06 09:50:18 +02:00
|
|
|
free(server->tcp_buffer);
|
|
|
|
server->tcp_buffer = NULL;
|
|
|
|
server->tcp_lenbuf_pos = 0;
|
|
|
|
}
|
|
|
|
}
|
2003-10-07 23:54:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If any UDP sockets select true for reading, process them. */
|
|
|
|
static void read_udp_packets(ares_channel channel, fd_set *read_fds,
|
2004-10-06 09:50:18 +02:00
|
|
|
time_t now)
|
2003-10-07 23:54:04 +02:00
|
|
|
{
|
|
|
|
struct server_state *server;
|
|
|
|
int i, count;
|
|
|
|
unsigned char buf[PACKETSZ + 1];
|
|
|
|
|
|
|
|
for (i = 0; i < channel->nservers; i++)
|
|
|
|
{
|
|
|
|
/* Make sure the server has a socket and is selected in read_fds. */
|
|
|
|
server = &channel->servers[i];
|
|
|
|
|
2004-07-23 00:18:45 +02:00
|
|
|
if (server->udp_socket == ARES_SOCKET_BAD ||
|
|
|
|
!FD_ISSET(server->udp_socket, read_fds))
|
2004-10-06 09:50:18 +02:00
|
|
|
continue;
|
2003-10-07 23:54:04 +02:00
|
|
|
|
2005-11-25 23:14:53 +01:00
|
|
|
count = recv(server->udp_socket, (void *)buf, sizeof(buf), 0);
|
2003-10-07 23:54:04 +02:00
|
|
|
if (count <= 0)
|
2004-10-06 09:50:18 +02:00
|
|
|
handle_error(channel, i, now);
|
2003-10-07 23:54:04 +02:00
|
|
|
|
|
|
|
process_answer(channel, buf, count, i, 0, now);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If any queries have timed out, note the timeout and move them on. */
|
|
|
|
static void process_timeouts(ares_channel channel, time_t now)
|
|
|
|
{
|
|
|
|
struct query *query, *next;
|
|
|
|
|
|
|
|
for (query = channel->queries; query; query = next)
|
|
|
|
{
|
|
|
|
next = query->next;
|
|
|
|
if (query->timeout != 0 && now >= query->timeout)
|
2004-10-06 09:50:18 +02:00
|
|
|
{
|
|
|
|
query->error_status = ARES_ETIMEOUT;
|
|
|
|
next = next_server(channel, query, now);
|
|
|
|
}
|
2003-10-07 23:54:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle an answer from a server. */
|
|
|
|
static void process_answer(ares_channel channel, unsigned char *abuf,
|
2004-10-06 09:50:18 +02:00
|
|
|
int alen, int whichserver, int tcp, int now)
|
2003-10-07 23:54:04 +02:00
|
|
|
{
|
|
|
|
int id, tc, rcode;
|
|
|
|
struct query *query;
|
|
|
|
|
|
|
|
/* If there's no room in the answer for a header, we can't do much
|
|
|
|
* with it. */
|
|
|
|
if (alen < HFIXEDSZ)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Grab the query ID, truncate bit, and response code from the packet. */
|
|
|
|
id = DNS_HEADER_QID(abuf);
|
|
|
|
tc = DNS_HEADER_TC(abuf);
|
|
|
|
rcode = DNS_HEADER_RCODE(abuf);
|
|
|
|
|
|
|
|
/* Find the query corresponding to this packet. */
|
|
|
|
for (query = channel->queries; query; query = query->next)
|
|
|
|
{
|
|
|
|
if (query->qid == id)
|
2004-10-06 09:50:18 +02:00
|
|
|
break;
|
2003-10-07 23:54:04 +02:00
|
|
|
}
|
|
|
|
if (!query)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* If we got a truncated UDP packet and are not ignoring truncation,
|
|
|
|
* don't accept the packet, and switch the query to TCP if we hadn't
|
|
|
|
* done so already.
|
|
|
|
*/
|
|
|
|
if ((tc || alen > PACKETSZ) && !tcp && !(channel->flags & ARES_FLAG_IGNTC))
|
|
|
|
{
|
|
|
|
if (!query->using_tcp)
|
2004-10-06 09:50:18 +02:00
|
|
|
{
|
|
|
|
query->using_tcp = 1;
|
|
|
|
ares__send_query(channel, query, now);
|
|
|
|
}
|
2003-10-07 23:54:04 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Limit alen to PACKETSZ if we aren't using TCP (only relevant if we
|
|
|
|
* are ignoring truncation.
|
|
|
|
*/
|
|
|
|
if (alen > PACKETSZ && !tcp)
|
|
|
|
alen = PACKETSZ;
|
|
|
|
|
|
|
|
/* If we aren't passing through all error packets, discard packets
|
|
|
|
* with SERVFAIL, NOTIMP, or REFUSED response codes.
|
|
|
|
*/
|
|
|
|
if (!(channel->flags & ARES_FLAG_NOCHECKRESP))
|
|
|
|
{
|
|
|
|
if (rcode == SERVFAIL || rcode == NOTIMP || rcode == REFUSED)
|
2004-10-06 09:50:18 +02:00
|
|
|
{
|
|
|
|
query->skip_server[whichserver] = 1;
|
|
|
|
if (query->server == whichserver)
|
|
|
|
next_server(channel, query, now);
|
|
|
|
return;
|
|
|
|
}
|
2003-10-07 23:54:04 +02:00
|
|
|
if (!same_questions(query->qbuf, query->qlen, abuf, alen))
|
2004-10-06 09:50:18 +02:00
|
|
|
{
|
|
|
|
if (query->server == whichserver)
|
|
|
|
next_server(channel, query, now);
|
|
|
|
return;
|
|
|
|
}
|
2003-10-07 23:54:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
end_query(channel, query, ARES_SUCCESS, abuf, alen);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_error(ares_channel channel, int whichserver, time_t now)
|
|
|
|
{
|
2004-07-24 23:47:49 +02:00
|
|
|
struct query *query, *next;
|
2003-10-07 23:54:04 +02:00
|
|
|
|
|
|
|
/* Reset communications with this server. */
|
|
|
|
ares__close_sockets(&channel->servers[whichserver]);
|
|
|
|
|
|
|
|
/* Tell all queries talking to this server to move on and not try
|
|
|
|
* this server again.
|
|
|
|
*/
|
2004-07-24 23:47:49 +02:00
|
|
|
|
|
|
|
for (query = channel->queries; query; query = next)
|
2003-10-07 23:54:04 +02:00
|
|
|
{
|
2004-07-24 23:47:49 +02:00
|
|
|
next = query->next;
|
2003-10-07 23:54:04 +02:00
|
|
|
if (query->server == whichserver)
|
2004-10-06 09:50:18 +02:00
|
|
|
{
|
|
|
|
query->skip_server[whichserver] = 1;
|
|
|
|
next = next_server(channel, query, now);
|
|
|
|
}
|
2003-10-07 23:54:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-24 23:47:49 +02:00
|
|
|
static struct query *next_server(ares_channel channel, struct query *query, time_t now)
|
2003-10-07 23:54:04 +02:00
|
|
|
{
|
|
|
|
/* Advance to the next server or try. */
|
|
|
|
query->server++;
|
|
|
|
for (; query->try < channel->tries; query->try++)
|
|
|
|
{
|
|
|
|
for (; query->server < channel->nservers; query->server++)
|
2004-10-06 09:50:18 +02:00
|
|
|
{
|
|
|
|
if (!query->skip_server[query->server])
|
|
|
|
{
|
|
|
|
ares__send_query(channel, query, now);
|
|
|
|
return (query->next);
|
|
|
|
}
|
|
|
|
}
|
2003-10-07 23:54:04 +02:00
|
|
|
query->server = 0;
|
|
|
|
|
|
|
|
/* Only one try if we're using TCP. */
|
|
|
|
if (query->using_tcp)
|
2004-10-06 09:50:18 +02:00
|
|
|
break;
|
2003-10-07 23:54:04 +02:00
|
|
|
}
|
2004-07-24 23:47:49 +02:00
|
|
|
return end_query(channel, query, query->error_status, NULL, 0);
|
2003-10-07 23:54:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ares__send_query(ares_channel channel, struct query *query, time_t now)
|
|
|
|
{
|
|
|
|
struct send_request *sendreq;
|
|
|
|
struct server_state *server;
|
|
|
|
|
|
|
|
server = &channel->servers[query->server];
|
|
|
|
if (query->using_tcp)
|
|
|
|
{
|
|
|
|
/* Make sure the TCP socket for this server is set up and queue
|
|
|
|
* a send request.
|
|
|
|
*/
|
2004-07-23 00:18:45 +02:00
|
|
|
if (server->tcp_socket == ARES_SOCKET_BAD)
|
2004-10-06 09:50:18 +02:00
|
|
|
{
|
|
|
|
if (open_tcp_socket(channel, server) == -1)
|
|
|
|
{
|
|
|
|
query->skip_server[query->server] = 1;
|
|
|
|
next_server(channel, query, now);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2004-07-24 23:47:49 +02:00
|
|
|
sendreq = calloc(sizeof(struct send_request), 1);
|
2003-10-07 23:54:04 +02:00
|
|
|
if (!sendreq)
|
2004-07-24 23:47:49 +02:00
|
|
|
{
|
2004-10-06 09:50:18 +02:00
|
|
|
end_query(channel, query, ARES_ENOMEM, NULL, 0);
|
2004-07-24 23:47:49 +02:00
|
|
|
return;
|
|
|
|
}
|
2003-10-07 23:54:04 +02:00
|
|
|
sendreq->data = query->tcpbuf;
|
|
|
|
sendreq->len = query->tcplen;
|
|
|
|
sendreq->next = NULL;
|
|
|
|
if (server->qtail)
|
2004-10-06 09:50:18 +02:00
|
|
|
server->qtail->next = sendreq;
|
2003-10-07 23:54:04 +02:00
|
|
|
else
|
2004-10-06 09:50:18 +02:00
|
|
|
server->qhead = sendreq;
|
2003-10-07 23:54:04 +02:00
|
|
|
server->qtail = sendreq;
|
|
|
|
query->timeout = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-07-23 00:18:45 +02:00
|
|
|
if (server->udp_socket == ARES_SOCKET_BAD)
|
2004-10-06 09:50:18 +02:00
|
|
|
{
|
|
|
|
if (open_udp_socket(channel, server) == -1)
|
|
|
|
{
|
|
|
|
query->skip_server[query->server] = 1;
|
|
|
|
next_server(channel, query, now);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2005-11-25 23:14:53 +01:00
|
|
|
if (send(server->udp_socket, (void *)query->qbuf,
|
|
|
|
query->qlen, 0) == -1)
|
2004-10-06 09:50:18 +02:00
|
|
|
{
|
|
|
|
query->skip_server[query->server] = 1;
|
|
|
|
next_server(channel, query, now);
|
|
|
|
return;
|
|
|
|
}
|
2003-10-07 23:54:04 +02:00
|
|
|
query->timeout = now
|
2004-10-06 09:50:18 +02:00
|
|
|
+ ((query->try == 0) ? channel->timeout
|
|
|
|
: channel->timeout << query->try / channel->nservers);
|
2003-10-07 23:54:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-02 13:58:04 +02:00
|
|
|
/*
|
|
|
|
* nonblock() set the given socket to either blocking or non-blocking mode
|
|
|
|
* based on the 'nonblock' boolean argument. This function is highly portable.
|
|
|
|
*/
|
|
|
|
static int nonblock(ares_socket_t sockfd, /* operate on this */
|
|
|
|
int nonblock /* TRUE or FALSE */)
|
2003-10-07 23:54:04 +02:00
|
|
|
{
|
2005-06-02 13:58:04 +02:00
|
|
|
#undef SETBLOCK
|
|
|
|
#define SETBLOCK 0
|
|
|
|
#ifdef HAVE_O_NONBLOCK
|
|
|
|
/* most recent unix versions */
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
flags = fcntl(sockfd, F_GETFL, 0);
|
|
|
|
if (TRUE == nonblock)
|
|
|
|
return fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
|
|
|
|
else
|
|
|
|
return fcntl(sockfd, F_SETFL, flags & (~O_NONBLOCK));
|
|
|
|
#undef SETBLOCK
|
|
|
|
#define SETBLOCK 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(HAVE_FIONBIO) && (SETBLOCK == 0)
|
|
|
|
/* older unix versions */
|
2004-07-23 00:18:45 +02:00
|
|
|
int flags;
|
2005-06-02 13:58:04 +02:00
|
|
|
|
|
|
|
flags = nonblock;
|
|
|
|
return ioctl(sockfd, FIONBIO, &flags);
|
|
|
|
#undef SETBLOCK
|
|
|
|
#define SETBLOCK 2
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(HAVE_IOCTLSOCKET) && (SETBLOCK == 0)
|
|
|
|
/* Windows? */
|
|
|
|
unsigned long flags;
|
|
|
|
flags = nonblock;
|
|
|
|
|
|
|
|
return ioctlsocket(sockfd, FIONBIO, &flags);
|
|
|
|
#undef SETBLOCK
|
|
|
|
#define SETBLOCK 3
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(HAVE_IOCTLSOCKET_CASE) && (SETBLOCK == 0)
|
|
|
|
/* presumably for Amiga */
|
|
|
|
return IoctlSocket(sockfd, FIONBIO, (long)nonblock);
|
|
|
|
#undef SETBLOCK
|
|
|
|
#define SETBLOCK 4
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(HAVE_SO_NONBLOCK) && (SETBLOCK == 0)
|
|
|
|
/* BeOS */
|
|
|
|
long b = nonblock ? 1 : 0;
|
|
|
|
return setsockopt(sockfd, SOL_SOCKET, SO_NONBLOCK, &b, sizeof(b));
|
|
|
|
#undef SETBLOCK
|
|
|
|
#define SETBLOCK 5
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_DISABLED_NONBLOCKING
|
|
|
|
return 0; /* returns success */
|
|
|
|
#undef SETBLOCK
|
|
|
|
#define SETBLOCK 6
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (SETBLOCK == 0)
|
|
|
|
#error "no non-blocking method was found/used/set"
|
2005-03-13 10:21:54 +01:00
|
|
|
#endif
|
2005-06-02 13:58:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int open_tcp_socket(ares_channel channel, struct server_state *server)
|
|
|
|
{
|
2005-03-13 10:21:54 +01:00
|
|
|
ares_socket_t s;
|
2004-02-04 11:23:15 +01:00
|
|
|
struct sockaddr_in sockin;
|
2003-10-07 23:54:04 +02:00
|
|
|
|
|
|
|
/* Acquire a socket. */
|
|
|
|
s = socket(AF_INET, SOCK_STREAM, 0);
|
2004-07-23 00:18:45 +02:00
|
|
|
if (s == ARES_SOCKET_BAD)
|
2003-10-07 23:54:04 +02:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Set the socket non-blocking. */
|
2005-06-02 13:58:04 +02:00
|
|
|
nonblock(s, TRUE);
|
2004-07-01 15:54:24 +02:00
|
|
|
|
2003-10-07 23:54:04 +02:00
|
|
|
/* Connect to the server. */
|
2004-02-04 11:23:15 +01:00
|
|
|
memset(&sockin, 0, sizeof(sockin));
|
|
|
|
sockin.sin_family = AF_INET;
|
|
|
|
sockin.sin_addr = server->addr;
|
|
|
|
sockin.sin_port = channel->tcp_port;
|
2004-07-01 15:54:24 +02:00
|
|
|
if (connect(s, (struct sockaddr *) &sockin, sizeof(sockin)) == -1) {
|
|
|
|
int err = GET_ERRNO();
|
|
|
|
|
|
|
|
if (err != EINPROGRESS && err != EWOULDBLOCK) {
|
2003-10-07 23:54:04 +02:00
|
|
|
closesocket(s);
|
|
|
|
return -1;
|
|
|
|
}
|
2004-07-01 15:54:24 +02:00
|
|
|
}
|
2003-10-07 23:54:04 +02:00
|
|
|
|
2004-07-24 23:47:49 +02:00
|
|
|
server->tcp_buffer_pos = 0;
|
2003-10-07 23:54:04 +02:00
|
|
|
server->tcp_socket = s;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int open_udp_socket(ares_channel channel, struct server_state *server)
|
|
|
|
{
|
2004-07-23 00:18:45 +02:00
|
|
|
ares_socket_t s;
|
2004-02-04 11:23:15 +01:00
|
|
|
struct sockaddr_in sockin;
|
2003-10-07 23:54:04 +02:00
|
|
|
|
|
|
|
/* Acquire a socket. */
|
|
|
|
s = socket(AF_INET, SOCK_DGRAM, 0);
|
2004-07-23 00:18:45 +02:00
|
|
|
if (s == ARES_SOCKET_BAD)
|
2003-10-07 23:54:04 +02:00
|
|
|
return -1;
|
|
|
|
|
2005-06-02 13:58:04 +02:00
|
|
|
/* Set the socket non-blocking. */
|
|
|
|
nonblock(s, TRUE);
|
|
|
|
|
2003-10-07 23:54:04 +02:00
|
|
|
/* Connect to the server. */
|
2004-02-04 11:23:15 +01:00
|
|
|
memset(&sockin, 0, sizeof(sockin));
|
|
|
|
sockin.sin_family = AF_INET;
|
|
|
|
sockin.sin_addr = server->addr;
|
|
|
|
sockin.sin_port = channel->udp_port;
|
|
|
|
if (connect(s, (struct sockaddr *) &sockin, sizeof(sockin)) == -1)
|
2003-10-07 23:54:04 +02:00
|
|
|
{
|
|
|
|
closesocket(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
server->udp_socket = s;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int same_questions(const unsigned char *qbuf, int qlen,
|
2004-10-06 09:50:18 +02:00
|
|
|
const unsigned char *abuf, int alen)
|
2003-10-07 23:54:04 +02:00
|
|
|
{
|
|
|
|
struct {
|
|
|
|
const unsigned char *p;
|
|
|
|
int qdcount;
|
|
|
|
char *name;
|
2004-02-23 08:52:20 +01:00
|
|
|
long namelen;
|
2003-10-07 23:54:04 +02:00
|
|
|
int type;
|
|
|
|
int dnsclass;
|
|
|
|
} q, a;
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
if (qlen < HFIXEDSZ || alen < HFIXEDSZ)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Extract qdcount from the request and reply buffers and compare them. */
|
|
|
|
q.qdcount = DNS_HEADER_QDCOUNT(qbuf);
|
|
|
|
a.qdcount = DNS_HEADER_QDCOUNT(abuf);
|
|
|
|
if (q.qdcount != a.qdcount)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* For each question in qbuf, find it in abuf. */
|
|
|
|
q.p = qbuf + HFIXEDSZ;
|
|
|
|
for (i = 0; i < q.qdcount; i++)
|
|
|
|
{
|
|
|
|
/* Decode the question in the query. */
|
|
|
|
if (ares_expand_name(q.p, qbuf, qlen, &q.name, &q.namelen)
|
2004-10-06 09:50:18 +02:00
|
|
|
!= ARES_SUCCESS)
|
|
|
|
return 0;
|
2003-10-07 23:54:04 +02:00
|
|
|
q.p += q.namelen;
|
|
|
|
if (q.p + QFIXEDSZ > qbuf + qlen)
|
2004-10-06 09:50:18 +02:00
|
|
|
{
|
|
|
|
free(q.name);
|
|
|
|
return 0;
|
|
|
|
}
|
2003-10-07 23:54:04 +02:00
|
|
|
q.type = DNS_QUESTION_TYPE(q.p);
|
|
|
|
q.dnsclass = DNS_QUESTION_CLASS(q.p);
|
|
|
|
q.p += QFIXEDSZ;
|
|
|
|
|
|
|
|
/* Search for this question in the answer. */
|
|
|
|
a.p = abuf + HFIXEDSZ;
|
|
|
|
for (j = 0; j < a.qdcount; j++)
|
2004-10-06 09:50:18 +02:00
|
|
|
{
|
|
|
|
/* Decode the question in the answer. */
|
|
|
|
if (ares_expand_name(a.p, abuf, alen, &a.name, &a.namelen)
|
|
|
|
!= ARES_SUCCESS)
|
|
|
|
{
|
|
|
|
free(q.name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
a.p += a.namelen;
|
|
|
|
if (a.p + QFIXEDSZ > abuf + alen)
|
|
|
|
{
|
|
|
|
free(q.name);
|
|
|
|
free(a.name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
a.type = DNS_QUESTION_TYPE(a.p);
|
|
|
|
a.dnsclass = DNS_QUESTION_CLASS(a.p);
|
|
|
|
a.p += QFIXEDSZ;
|
|
|
|
|
|
|
|
/* Compare the decoded questions. */
|
|
|
|
if (strcasecmp(q.name, a.name) == 0 && q.type == a.type
|
|
|
|
&& q.dnsclass == a.dnsclass)
|
|
|
|
{
|
|
|
|
free(a.name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free(a.name);
|
|
|
|
}
|
2003-10-07 23:54:04 +02:00
|
|
|
|
|
|
|
free(q.name);
|
|
|
|
if (j == a.qdcount)
|
2004-10-06 09:50:18 +02:00
|
|
|
return 0;
|
2003-10-07 23:54:04 +02:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-07-24 23:47:49 +02:00
|
|
|
static struct query *end_query (ares_channel channel, struct query *query, int status,
|
2004-10-06 09:50:18 +02:00
|
|
|
unsigned char *abuf, int alen)
|
2003-10-07 23:54:04 +02:00
|
|
|
{
|
2004-07-24 23:47:49 +02:00
|
|
|
struct query **q, *next;
|
2003-10-07 23:54:04 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
query->callback(query->arg, status, abuf, alen);
|
|
|
|
for (q = &channel->queries; *q; q = &(*q)->next)
|
|
|
|
{
|
|
|
|
if (*q == query)
|
2004-10-06 09:50:18 +02:00
|
|
|
break;
|
2003-10-07 23:54:04 +02:00
|
|
|
}
|
|
|
|
*q = query->next;
|
2004-07-24 23:47:49 +02:00
|
|
|
if (*q)
|
|
|
|
next = (*q)->next;
|
|
|
|
else
|
|
|
|
next = NULL;
|
2003-10-07 23:54:04 +02:00
|
|
|
free(query->tcpbuf);
|
|
|
|
free(query->skip_server);
|
|
|
|
free(query);
|
|
|
|
|
|
|
|
/* Simple cleanup policy: if no queries are remaining, close all
|
|
|
|
* network sockets unless STAYOPEN is set.
|
|
|
|
*/
|
|
|
|
if (!channel->queries && !(channel->flags & ARES_FLAG_STAYOPEN))
|
|
|
|
{
|
|
|
|
for (i = 0; i < channel->nservers; i++)
|
2004-10-06 09:50:18 +02:00
|
|
|
ares__close_sockets(&channel->servers[i]);
|
2003-10-07 23:54:04 +02:00
|
|
|
}
|
2004-07-24 23:47:49 +02:00
|
|
|
return (next);
|
2003-10-07 23:54:04 +02:00
|
|
|
}
|