2006-07-22 11:37:10 -04:00
|
|
|
/* $Id$ */
|
|
|
|
|
2003-10-07 17:54:04 -04: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-22 18:18:45 -04:00
|
|
|
#include "setup.h"
|
2003-10-07 17:54:04 -04:00
|
|
|
|
2004-08-20 09:45:26 -04:00
|
|
|
#if defined(WIN32) && !defined(WATT32)
|
2003-10-07 17:54:04 -04:00
|
|
|
#include "nameser.h"
|
2004-08-20 09:45:26 -04:00
|
|
|
|
2003-10-07 17:54:04 -04:00
|
|
|
#else
|
2007-09-29 22:12:11 -04:00
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
2003-10-07 17:54:04 -04:00
|
|
|
#include <sys/socket.h>
|
2007-09-29 22:12:11 -04:00
|
|
|
#endif
|
2004-08-20 09:45:26 -04:00
|
|
|
#ifdef HAVE_SYS_UIO_H
|
2003-10-07 17:54:04 -04:00
|
|
|
#include <sys/uio.h>
|
2004-08-20 09:45:26 -04:00
|
|
|
#endif
|
2007-09-29 22:12:11 -04:00
|
|
|
#ifdef HAVE_NETINET_IN_H
|
|
|
|
#include <netinet/in.h> /* <netinet/tcp.h> may need it */
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETINET_TCP_H
|
|
|
|
#include <netinet/tcp.h> /* for TCP_NODELAY */
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETDB_H
|
2003-10-07 17:54:04 -04:00
|
|
|
#include <netdb.h>
|
2007-09-29 22:12:11 -04:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ARPA_NAMESER_H
|
2003-10-07 17:54:04 -04:00
|
|
|
#include <arpa/nameser.h>
|
2007-09-29 22:12:11 -04:00
|
|
|
#endif
|
2005-04-06 18:27:42 -04:00
|
|
|
#ifdef HAVE_ARPA_NAMESER_COMPAT_H
|
|
|
|
#include <arpa/nameser_compat.h>
|
|
|
|
#endif
|
2005-12-20 15:48:38 -05:00
|
|
|
#endif /* WIN32 && !WATT32 */
|
|
|
|
|
2004-08-20 09:45:26 -04:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2003-10-07 17:54:04 -04:00
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2005-06-03 03:39:18 -04:00
|
|
|
#ifdef HAVE_SYS_IOCTL_H
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#endif
|
2005-08-10 13:03:53 -04:00
|
|
|
#ifdef NETWARE
|
|
|
|
#include <sys/filio.h>
|
|
|
|
#endif
|
2003-10-07 17:54:04 -04:00
|
|
|
|
2007-09-29 17:57:05 -04:00
|
|
|
#include <assert.h>
|
2003-10-07 17:54:04 -04:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <errno.h>
|
2004-08-20 09:45:26 -04:00
|
|
|
|
2003-10-07 17:54:04 -04:00
|
|
|
#include "ares.h"
|
|
|
|
#include "ares_dns.h"
|
|
|
|
#include "ares_private.h"
|
|
|
|
|
2005-06-02 17:10:21 -04:00
|
|
|
|
2006-06-18 21:18:05 -04:00
|
|
|
static int try_again(int errnum);
|
2003-10-07 17:54:04 -04:00
|
|
|
static void write_tcp_data(ares_channel channel, fd_set *write_fds,
|
2007-05-30 08:58:47 -04:00
|
|
|
ares_socket_t write_fd, time_t now);
|
|
|
|
static void read_tcp_data(ares_channel channel, fd_set *read_fds,
|
|
|
|
ares_socket_t read_fd, time_t now);
|
2003-10-07 17:54:04 -04:00
|
|
|
static void read_udp_packets(ares_channel channel, fd_set *read_fds,
|
2007-05-30 08:58:47 -04:00
|
|
|
ares_socket_t read_fd, time_t now);
|
2007-09-29 10:09:14 -04:00
|
|
|
static void advance_tcp_send_queue(ares_channel channel, int whichserver,
|
|
|
|
ssize_t num_bytes);
|
2003-10-07 17:54:04 -04:00
|
|
|
static void process_timeouts(ares_channel channel, time_t now);
|
2007-09-28 10:28:14 -04:00
|
|
|
static void process_broken_connections(ares_channel channel, time_t now);
|
2003-10-07 17:54:04 -04:00
|
|
|
static void process_answer(ares_channel channel, unsigned char *abuf,
|
2007-06-02 15:48:29 -04:00
|
|
|
int alen, int whichserver, int tcp, time_t now);
|
2003-10-07 17:54:04 -04:00
|
|
|
static void handle_error(ares_channel channel, int whichserver, time_t now);
|
2007-09-28 10:26:11 -04:00
|
|
|
static void skip_server(ares_channel channel, struct query *query,
|
|
|
|
int whichserver);
|
2007-09-29 14:18:47 -04:00
|
|
|
static void next_server(ares_channel channel, struct query *query, time_t now);
|
2007-09-28 11:51:00 -04:00
|
|
|
static int configure_socket(int s, ares_channel channel);
|
2003-10-07 17:54:04 -04: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 03:50:18 -04:00
|
|
|
const unsigned char *abuf, int alen);
|
2007-09-29 14:18:47 -04:00
|
|
|
static void end_query(ares_channel channel, struct query *query, int status,
|
2004-10-06 03:50:18 -04:00
|
|
|
unsigned char *abuf, int alen);
|
2003-10-07 17:54:04 -04: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);
|
2007-05-30 08:58:47 -04:00
|
|
|
write_tcp_data(channel, write_fds, ARES_SOCKET_BAD, now);
|
|
|
|
read_tcp_data(channel, read_fds, ARES_SOCKET_BAD, now);
|
|
|
|
read_udp_packets(channel, read_fds, ARES_SOCKET_BAD, now);
|
2003-10-07 17:54:04 -04:00
|
|
|
process_timeouts(channel, now);
|
2007-09-28 10:28:14 -04:00
|
|
|
process_broken_connections(channel, now);
|
2003-10-07 17:54:04 -04:00
|
|
|
}
|
|
|
|
|
2007-05-30 08:58:47 -04:00
|
|
|
/* Something interesting happened on the wire, or there was a timeout.
|
|
|
|
* See what's up and respond accordingly.
|
|
|
|
*/
|
|
|
|
void ares_process_fd(ares_channel channel,
|
|
|
|
ares_socket_t read_fd, /* use ARES_SOCKET_BAD or valid
|
|
|
|
file descriptors */
|
|
|
|
ares_socket_t write_fd)
|
|
|
|
{
|
|
|
|
time_t now;
|
|
|
|
|
|
|
|
time(&now);
|
|
|
|
write_tcp_data(channel, NULL, write_fd, now);
|
|
|
|
read_tcp_data(channel, NULL, read_fd, now);
|
|
|
|
read_udp_packets(channel, NULL, read_fd, now);
|
|
|
|
process_timeouts(channel, now);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-02-16 10:04:44 -05:00
|
|
|
/* Return 1 if the specified error number describes a readiness error, or 0
|
2006-06-18 21:18:05 -04:00
|
|
|
* otherwise. This is mostly for HP-UX, which could return EAGAIN or
|
|
|
|
* EWOULDBLOCK. See this man page
|
|
|
|
*
|
2007-09-27 08:04:56 -04:00
|
|
|
* http://devrsrc1.external.hp.com/STKS/cgi-bin/man2html?manpage=/usr/share/man/man2.Z/send.2
|
2006-06-18 21:18:05 -04:00
|
|
|
*/
|
|
|
|
static int try_again(int errnum)
|
|
|
|
{
|
|
|
|
#if !defined EWOULDBLOCK && !defined EAGAIN
|
|
|
|
#error "Neither EWOULDBLOCK nor EAGAIN defined"
|
|
|
|
#endif
|
|
|
|
switch (errnum)
|
|
|
|
{
|
|
|
|
#ifdef EWOULDBLOCK
|
|
|
|
case EWOULDBLOCK:
|
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
#if defined EAGAIN && EAGAIN != EWOULDBLOCK
|
|
|
|
case EAGAIN:
|
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-10-07 17:54:04 -04:00
|
|
|
/* If any TCP sockets select true for writing, write out queued data
|
|
|
|
* we have for them.
|
|
|
|
*/
|
2007-05-30 08:58:47 -04:00
|
|
|
static void write_tcp_data(ares_channel channel,
|
|
|
|
fd_set *write_fds,
|
|
|
|
ares_socket_t write_fd,
|
|
|
|
time_t now)
|
2003-10-07 17:54:04 -04:00
|
|
|
{
|
|
|
|
struct server_state *server;
|
|
|
|
struct send_request *sendreq;
|
|
|
|
struct iovec *vec;
|
2004-07-22 18:18:45 -04:00
|
|
|
int i;
|
2004-07-29 03:24:39 -04:00
|
|
|
ssize_t scount;
|
2005-12-09 16:09:21 -05:00
|
|
|
ssize_t wcount;
|
2004-07-22 18:18:45 -04:00
|
|
|
size_t n;
|
2003-10-07 17:54:04 -04:00
|
|
|
|
2007-09-27 08:04:56 -04:00
|
|
|
if(!write_fds && (write_fd == ARES_SOCKET_BAD))
|
2007-05-30 08:58:47 -04:00
|
|
|
/* no possible action */
|
|
|
|
return;
|
|
|
|
|
2003-10-07 17:54:04 -04:00
|
|
|
for (i = 0; i < channel->nservers; i++)
|
|
|
|
{
|
2007-05-30 08:58:47 -04:00
|
|
|
/* Make sure server has data to send and is selected in write_fds or
|
|
|
|
write_fd. */
|
2003-10-07 17:54:04 -04:00
|
|
|
server = &channel->servers[i];
|
2007-09-28 10:28:14 -04:00
|
|
|
if (!server->qhead || server->tcp_socket == ARES_SOCKET_BAD || server->is_broken)
|
2004-10-06 03:50:18 -04:00
|
|
|
continue;
|
2003-10-07 17:54:04 -04:00
|
|
|
|
2007-05-30 08:58:47 -04:00
|
|
|
if(write_fds) {
|
|
|
|
if(!FD_ISSET(server->tcp_socket, write_fds))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(server->tcp_socket != write_fd)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-09-27 08:04:56 -04:00
|
|
|
if(write_fds)
|
|
|
|
/* If there's an error and we close this socket, then open
|
|
|
|
* another with the same fd to talk to another server, then we
|
|
|
|
* don't want to think that it was the new socket that was
|
|
|
|
* ready. This is not disastrous, but is likely to result in
|
|
|
|
* extra system calls and confusion. */
|
|
|
|
FD_CLR(server->tcp_socket, write_fds);
|
2007-09-22 17:23:10 -04:00
|
|
|
|
2003-10-07 17:54:04 -04:00
|
|
|
/* Count the number of send queue items. */
|
|
|
|
n = 0;
|
|
|
|
for (sendreq = server->qhead; sendreq; sendreq = sendreq->next)
|
2004-10-06 03:50:18 -04:00
|
|
|
n++;
|
2003-10-07 17:54:04 -04:00
|
|
|
|
|
|
|
/* Allocate iovecs so we can send all our data at once. */
|
|
|
|
vec = malloc(n * sizeof(struct iovec));
|
|
|
|
if (vec)
|
2004-10-06 03:50:18 -04: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++;
|
|
|
|
}
|
2006-10-25 06:25:43 -04:00
|
|
|
wcount = (ssize_t)writev(server->tcp_socket, vec, (int)n);
|
2004-10-06 03:50:18 -04:00
|
|
|
free(vec);
|
|
|
|
if (wcount < 0)
|
|
|
|
{
|
2007-02-16 10:04:44 -05:00
|
|
|
if (!try_again(SOCKERRNO))
|
2006-06-18 21:18:05 -04:00
|
|
|
handle_error(channel, i, now);
|
2004-10-06 03:50:18 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Advance the send queue by as many bytes as we sent. */
|
2007-09-29 10:09:14 -04:00
|
|
|
advance_tcp_send_queue(channel, i, wcount);
|
2004-10-06 03:50:18 -04:00
|
|
|
}
|
2003-10-07 17:54:04 -04:00
|
|
|
else
|
2004-10-06 03:50:18 -04:00
|
|
|
{
|
|
|
|
/* Can't allocate iovecs; just send the first request. */
|
|
|
|
sendreq = server->qhead;
|
2003-10-07 17:54:04 -04:00
|
|
|
|
2006-07-28 14:01:23 -04:00
|
|
|
scount = swrite(server->tcp_socket, sendreq->data, sendreq->len);
|
2004-10-06 03:50:18 -04:00
|
|
|
if (scount < 0)
|
|
|
|
{
|
2007-02-16 10:04:44 -05:00
|
|
|
if (!try_again(SOCKERRNO))
|
2006-06-18 21:18:05 -04:00
|
|
|
handle_error(channel, i, now);
|
2004-10-06 03:50:18 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Advance the send queue by as many bytes as we sent. */
|
2007-09-29 10:09:14 -04:00
|
|
|
advance_tcp_send_queue(channel, i, scount);
|
2004-10-06 03:50:18 -04:00
|
|
|
}
|
2003-10-07 17:54:04 -04:00
|
|
|
}
|
|
|
|
}
|
2007-09-29 10:34:59 -04:00
|
|
|
|
2007-09-29 10:09:14 -04:00
|
|
|
/* Consume the given number of bytes from the head of the TCP send queue. */
|
|
|
|
static void advance_tcp_send_queue(ares_channel channel, int whichserver,
|
|
|
|
ssize_t num_bytes)
|
|
|
|
{
|
|
|
|
struct send_request *sendreq;
|
|
|
|
struct server_state *server = &channel->servers[whichserver];
|
|
|
|
while (num_bytes > 0)
|
|
|
|
{
|
|
|
|
sendreq = server->qhead;
|
|
|
|
if ((size_t)num_bytes >= sendreq->len)
|
|
|
|
{
|
|
|
|
num_bytes -= sendreq->len;
|
|
|
|
server->qhead = sendreq->next;
|
|
|
|
if (server->qhead == NULL)
|
|
|
|
{
|
|
|
|
SOCK_STATE_CALLBACK(channel, server->tcp_socket, 1, 0);
|
|
|
|
server->qtail = NULL;
|
|
|
|
}
|
|
|
|
if (sendreq->data_storage != NULL)
|
|
|
|
free(sendreq->data_storage);
|
|
|
|
free(sendreq);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sendreq->data += num_bytes;
|
|
|
|
sendreq->len -= num_bytes;
|
|
|
|
num_bytes = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-10-07 17:54:04 -04: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.
|
|
|
|
*/
|
2007-05-30 08:58:47 -04:00
|
|
|
static void read_tcp_data(ares_channel channel, fd_set *read_fds,
|
|
|
|
ares_socket_t read_fd, time_t now)
|
2003-10-07 17:54:04 -04:00
|
|
|
{
|
|
|
|
struct server_state *server;
|
2006-07-29 05:15:03 -04:00
|
|
|
int i;
|
|
|
|
ssize_t count;
|
2003-10-07 17:54:04 -04:00
|
|
|
|
2007-09-27 08:04:56 -04:00
|
|
|
if(!read_fds && (read_fd == ARES_SOCKET_BAD))
|
2007-05-30 08:58:47 -04:00
|
|
|
/* no possible action */
|
|
|
|
return;
|
|
|
|
|
2003-10-07 17:54:04 -04:00
|
|
|
for (i = 0; i < channel->nservers; i++)
|
|
|
|
{
|
|
|
|
/* Make sure the server has a socket and is selected in read_fds. */
|
|
|
|
server = &channel->servers[i];
|
2007-09-28 10:28:14 -04:00
|
|
|
if (server->tcp_socket == ARES_SOCKET_BAD || server->is_broken)
|
2004-10-06 03:50:18 -04:00
|
|
|
continue;
|
2003-10-07 17:54:04 -04:00
|
|
|
|
2007-05-30 08:58:47 -04:00
|
|
|
if(read_fds) {
|
|
|
|
if(!FD_ISSET(server->tcp_socket, read_fds))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(server->tcp_socket != read_fd)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-09-27 08:04:56 -04:00
|
|
|
if(read_fds)
|
|
|
|
/* If there's an error and we close this socket, then open
|
|
|
|
* another with the same fd to talk to another server, then we
|
|
|
|
* don't want to think that it was the new socket that was
|
|
|
|
* ready. This is not disastrous, but is likely to result in
|
|
|
|
* extra system calls and confusion. */
|
|
|
|
FD_CLR(server->tcp_socket, read_fds);
|
2007-09-22 17:23:10 -04:00
|
|
|
|
2003-10-07 17:54:04 -04:00
|
|
|
if (server->tcp_lenbuf_pos != 2)
|
2004-10-06 03:50:18 -04:00
|
|
|
{
|
|
|
|
/* We haven't yet read a length word, so read that (or
|
|
|
|
* what's left to read of it).
|
|
|
|
*/
|
2006-07-28 14:01:23 -04:00
|
|
|
count = sread(server->tcp_socket,
|
|
|
|
server->tcp_lenbuf + server->tcp_lenbuf_pos,
|
|
|
|
2 - server->tcp_lenbuf_pos);
|
2004-10-06 03:50:18 -04:00
|
|
|
if (count <= 0)
|
|
|
|
{
|
2007-02-16 10:04:44 -05:00
|
|
|
if (!(count == -1 && try_again(SOCKERRNO)))
|
2006-06-18 21:18:05 -04:00
|
|
|
handle_error(channel, i, now);
|
2004-10-06 03:50:18 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-07-29 05:15:03 -04:00
|
|
|
server->tcp_lenbuf_pos += (int)count;
|
2004-10-06 03:50:18 -04:00
|
|
|
if (server->tcp_lenbuf_pos == 2)
|
|
|
|
{
|
|
|
|
/* We finished reading the length word. Decode the
|
2003-10-07 17:54:04 -04:00
|
|
|
* length and allocate a buffer for the data.
|
2004-10-06 03:50:18 -04: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 17:54:04 -04:00
|
|
|
else
|
2004-10-06 03:50:18 -04:00
|
|
|
{
|
|
|
|
/* Read data into the allocated buffer. */
|
2006-07-28 14:01:23 -04:00
|
|
|
count = sread(server->tcp_socket,
|
|
|
|
server->tcp_buffer + server->tcp_buffer_pos,
|
|
|
|
server->tcp_length - server->tcp_buffer_pos);
|
2004-10-06 03:50:18 -04:00
|
|
|
if (count <= 0)
|
|
|
|
{
|
2007-02-16 10:04:44 -05:00
|
|
|
if (!(count == -1 && try_again(SOCKERRNO)))
|
2006-06-18 21:18:05 -04:00
|
|
|
handle_error(channel, i, now);
|
2004-10-06 03:50:18 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-07-29 05:15:03 -04:00
|
|
|
server->tcp_buffer_pos += (int)count;
|
2004-10-06 03:50:18 -04:00
|
|
|
if (server->tcp_buffer_pos == server->tcp_length)
|
|
|
|
{
|
|
|
|
/* We finished reading this answer; process it and
|
2003-10-07 17:54:04 -04:00
|
|
|
* prepare to read another length word.
|
2004-10-06 03:50:18 -04:00
|
|
|
*/
|
|
|
|
process_answer(channel, server->tcp_buffer, server->tcp_length,
|
|
|
|
i, 1, now);
|
2004-08-20 09:45:26 -04:00
|
|
|
if (server->tcp_buffer)
|
2004-10-06 03:50:18 -04:00
|
|
|
free(server->tcp_buffer);
|
|
|
|
server->tcp_buffer = NULL;
|
|
|
|
server->tcp_lenbuf_pos = 0;
|
2006-08-29 11:17:47 -04:00
|
|
|
server->tcp_buffer_pos = 0;
|
2004-10-06 03:50:18 -04:00
|
|
|
}
|
|
|
|
}
|
2003-10-07 17:54:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If any UDP sockets select true for reading, process them. */
|
|
|
|
static void read_udp_packets(ares_channel channel, fd_set *read_fds,
|
2007-05-30 08:58:47 -04:00
|
|
|
ares_socket_t read_fd, time_t now)
|
2003-10-07 17:54:04 -04:00
|
|
|
{
|
|
|
|
struct server_state *server;
|
2006-07-29 05:15:03 -04:00
|
|
|
int i;
|
|
|
|
ssize_t count;
|
2003-10-07 17:54:04 -04:00
|
|
|
unsigned char buf[PACKETSZ + 1];
|
|
|
|
|
2007-09-27 08:04:56 -04:00
|
|
|
if(!read_fds && (read_fd == ARES_SOCKET_BAD))
|
2007-05-30 08:58:47 -04:00
|
|
|
/* no possible action */
|
|
|
|
return;
|
|
|
|
|
2003-10-07 17:54:04 -04:00
|
|
|
for (i = 0; i < channel->nservers; i++)
|
|
|
|
{
|
|
|
|
/* Make sure the server has a socket and is selected in read_fds. */
|
|
|
|
server = &channel->servers[i];
|
|
|
|
|
2007-09-28 10:28:14 -04:00
|
|
|
if (server->udp_socket == ARES_SOCKET_BAD || server->is_broken)
|
2004-10-06 03:50:18 -04:00
|
|
|
continue;
|
2003-10-07 17:54:04 -04:00
|
|
|
|
2007-05-30 08:58:47 -04:00
|
|
|
if(read_fds) {
|
|
|
|
if(!FD_ISSET(server->udp_socket, read_fds))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(server->udp_socket != read_fd)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-09-27 08:04:56 -04:00
|
|
|
if(read_fds)
|
|
|
|
/* If there's an error and we close this socket, then open
|
|
|
|
* another with the same fd to talk to another server, then we
|
|
|
|
* don't want to think that it was the new socket that was
|
|
|
|
* ready. This is not disastrous, but is likely to result in
|
|
|
|
* extra system calls and confusion. */
|
|
|
|
FD_CLR(server->udp_socket, read_fds);
|
2007-09-22 17:23:10 -04:00
|
|
|
|
2007-09-29 10:21:47 -04:00
|
|
|
/* To reduce event loop overhead, read and process as many
|
|
|
|
* packets as we can. */
|
|
|
|
do {
|
|
|
|
count = sread(server->udp_socket, buf, sizeof(buf));
|
|
|
|
if (count == -1 && try_again(SOCKERRNO))
|
|
|
|
continue;
|
|
|
|
else if (count <= 0)
|
|
|
|
handle_error(channel, i, now);
|
|
|
|
else
|
|
|
|
process_answer(channel, buf, (int)count, i, 0, now);
|
|
|
|
} while (count > 0);
|
2003-10-07 17:54:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If any queries have timed out, note the timeout and move them on. */
|
|
|
|
static void process_timeouts(ares_channel channel, time_t now)
|
|
|
|
{
|
2007-09-29 14:18:47 -04:00
|
|
|
time_t t; /* the time of the timeouts we're processing */
|
|
|
|
struct query *query;
|
|
|
|
struct list_node* list_head;
|
|
|
|
struct list_node* list_node;
|
|
|
|
|
|
|
|
/* Process all the timeouts that have fired since the last time we
|
|
|
|
* processed timeouts. If things are going well, then we'll have
|
|
|
|
* hundreds/thousands of queries that fall into future buckets, and
|
|
|
|
* only a handful of requests that fall into the "now" bucket, so
|
|
|
|
* this should be quite quick.
|
|
|
|
*/
|
|
|
|
for (t = channel->last_timeout_processed; t <= now; t++)
|
2003-10-07 17:54:04 -04:00
|
|
|
{
|
2007-09-29 14:18:47 -04:00
|
|
|
list_head = &(channel->queries_by_timeout[t % ARES_TIMEOUT_TABLE_SIZE]);
|
|
|
|
for (list_node = list_head->next; list_node != list_head; )
|
2004-10-06 03:50:18 -04:00
|
|
|
{
|
2007-09-29 14:18:47 -04:00
|
|
|
query = list_node->data;
|
|
|
|
list_node = list_node->next; /* in case the query gets deleted */
|
|
|
|
if (query->timeout != 0 && now >= query->timeout)
|
|
|
|
{
|
|
|
|
query->error_status = ARES_ETIMEOUT;
|
|
|
|
++query->timeouts;
|
|
|
|
next_server(channel, query, now);
|
|
|
|
}
|
2004-10-06 03:50:18 -04:00
|
|
|
}
|
2007-09-29 14:18:47 -04:00
|
|
|
}
|
|
|
|
channel->last_timeout_processed = now;
|
2003-10-07 17:54:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle an answer from a server. */
|
|
|
|
static void process_answer(ares_channel channel, unsigned char *abuf,
|
2007-06-02 15:48:29 -04:00
|
|
|
int alen, int whichserver, int tcp, time_t now)
|
2003-10-07 17:54:04 -04:00
|
|
|
{
|
2007-07-14 09:08:50 -04:00
|
|
|
int tc, rcode;
|
|
|
|
unsigned short id;
|
2003-10-07 17:54:04 -04:00
|
|
|
struct query *query;
|
2007-09-29 14:18:47 -04:00
|
|
|
struct list_node* list_head;
|
|
|
|
struct list_node* list_node;
|
2003-10-07 17:54:04 -04:00
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
2007-09-29 14:18:47 -04:00
|
|
|
/* Find the query corresponding to this packet. The queries are
|
|
|
|
* hashed/bucketed by query id, so this lookup should be quick.
|
|
|
|
* Note that both the query id and the questions must be the same;
|
|
|
|
* when the query id wraps around we can have multiple outstanding
|
|
|
|
* queries with the same query id, so we need to check both the id and
|
|
|
|
* question.
|
|
|
|
*/
|
|
|
|
query = NULL;
|
|
|
|
list_head = &(channel->queries_by_qid[id % ARES_QID_TABLE_SIZE]);
|
|
|
|
for (list_node = list_head->next; list_node != list_head;
|
|
|
|
list_node = list_node->next)
|
2003-10-07 17:54:04 -04:00
|
|
|
{
|
2007-09-29 14:18:47 -04:00
|
|
|
struct query *q = list_node->data;
|
|
|
|
if ((q->qid == id) && same_questions(q->qbuf, q->qlen, abuf, alen))
|
|
|
|
{
|
|
|
|
query = q;
|
|
|
|
break;
|
|
|
|
}
|
2003-10-07 17:54:04 -04: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 03:50:18 -04:00
|
|
|
{
|
|
|
|
query->using_tcp = 1;
|
|
|
|
ares__send_query(channel, query, now);
|
|
|
|
}
|
2003-10-07 17:54:04 -04: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 03:50:18 -04:00
|
|
|
{
|
2007-09-28 10:26:11 -04:00
|
|
|
skip_server(channel, query, whichserver);
|
2004-10-06 03:50:18 -04:00
|
|
|
if (query->server == whichserver)
|
|
|
|
next_server(channel, query, now);
|
|
|
|
return;
|
|
|
|
}
|
2003-10-07 17:54:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
end_query(channel, query, ARES_SUCCESS, abuf, alen);
|
|
|
|
}
|
|
|
|
|
2007-09-28 10:28:14 -04:00
|
|
|
/* Close all the connections that are no longer usable. */
|
|
|
|
static void process_broken_connections(ares_channel channel, time_t now)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < channel->nservers; i++)
|
|
|
|
{
|
|
|
|
struct server_state *server = &channel->servers[i];
|
|
|
|
if (server->is_broken)
|
|
|
|
{
|
|
|
|
handle_error(channel, i, now);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-07 17:54:04 -04:00
|
|
|
static void handle_error(ares_channel channel, int whichserver, time_t now)
|
|
|
|
{
|
2007-09-29 14:18:47 -04:00
|
|
|
struct server_state *server;
|
|
|
|
struct query *query;
|
|
|
|
struct list_node list_head;
|
|
|
|
struct list_node* list_node;
|
|
|
|
|
|
|
|
server = &channel->servers[whichserver];
|
2003-10-07 17:54:04 -04:00
|
|
|
|
|
|
|
/* Reset communications with this server. */
|
2007-09-29 14:18:47 -04:00
|
|
|
ares__close_sockets(channel, server);
|
2003-10-07 17:54:04 -04:00
|
|
|
|
|
|
|
/* Tell all queries talking to this server to move on and not try
|
2007-09-29 14:18:47 -04:00
|
|
|
* this server again. We steal the current list of queries that were
|
|
|
|
* in-flight to this server, since when we call next_server this can
|
|
|
|
* cause the queries to be re-sent to this server, which will
|
|
|
|
* re-insert these queries in that same server->queries_to_server
|
|
|
|
* list.
|
2003-10-07 17:54:04 -04:00
|
|
|
*/
|
2007-09-29 14:18:47 -04:00
|
|
|
ares__init_list_head(&list_head);
|
|
|
|
ares__swap_lists(&list_head, &(server->queries_to_server));
|
|
|
|
for (list_node = list_head.next; list_node != &list_head; )
|
2003-10-07 17:54:04 -04:00
|
|
|
{
|
2007-09-29 14:18:47 -04:00
|
|
|
query = list_node->data;
|
|
|
|
list_node = list_node->next; /* in case the query gets deleted */
|
|
|
|
assert(query->server == whichserver);
|
|
|
|
skip_server(channel, query, whichserver);
|
|
|
|
next_server(channel, query, now);
|
2003-10-07 17:54:04 -04:00
|
|
|
}
|
2007-09-29 14:18:47 -04:00
|
|
|
/* Each query should have removed itself from our temporary list as
|
|
|
|
* it re-sent itself or finished up...
|
|
|
|
*/
|
|
|
|
assert(ares__is_list_empty(&list_head));
|
2003-10-07 17:54:04 -04:00
|
|
|
}
|
|
|
|
|
2007-09-28 10:26:11 -04:00
|
|
|
static void skip_server(ares_channel channel, struct query *query,
|
|
|
|
int whichserver) {
|
|
|
|
/* The given server gave us problems with this query, so if we have
|
|
|
|
* the luxury of using other servers, then let's skip the
|
|
|
|
* potentially broken server and just use the others. If we only
|
|
|
|
* have one server and we need to retry then we should just go ahead
|
|
|
|
* and re-use that server, since it's our only hope; perhaps we
|
|
|
|
* just got unlucky, and retrying will work (eg, the server timed
|
|
|
|
* out our TCP connection just as we were sending another request).
|
|
|
|
*/
|
|
|
|
if (channel->nservers > 1)
|
|
|
|
{
|
2007-09-28 10:28:14 -04:00
|
|
|
query->server_info[whichserver].skip_server = 1;
|
2007-09-28 10:26:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-29 14:18:47 -04:00
|
|
|
static void next_server(ares_channel channel, struct query *query, time_t now)
|
2003-10-07 17:54:04 -04: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 03:50:18 -04:00
|
|
|
{
|
2007-09-28 10:28:14 -04:00
|
|
|
struct server_state *server = &channel->servers[query->server];
|
|
|
|
/* We don't want to use this server if (1) we decided this
|
|
|
|
* connection is broken, and thus about to be closed, (2)
|
|
|
|
* we've decided to skip this server because of earlier
|
|
|
|
* errors we encountered, or (3) we already sent this query
|
|
|
|
* over this exact connection.
|
|
|
|
*/
|
|
|
|
if (!server->is_broken &&
|
|
|
|
!query->server_info[query->server].skip_server &&
|
|
|
|
!(query->using_tcp &&
|
|
|
|
(query->server_info[query->server].tcp_connection_generation ==
|
|
|
|
server->tcp_connection_generation)))
|
2004-10-06 03:50:18 -04:00
|
|
|
{
|
2007-09-28 10:28:14 -04:00
|
|
|
ares__send_query(channel, query, now);
|
2007-09-29 14:18:47 -04:00
|
|
|
return;
|
2004-10-06 03:50:18 -04:00
|
|
|
}
|
|
|
|
}
|
2003-10-07 17:54:04 -04:00
|
|
|
query->server = 0;
|
|
|
|
|
2007-09-22 17:04:16 -04:00
|
|
|
/* You might think that with TCP we only need one try. However,
|
|
|
|
* even when using TCP, servers can time-out our connection just
|
|
|
|
* as we're sending a request, or close our connection because
|
|
|
|
* they die, or never send us a reply because they get wedged or
|
|
|
|
* tickle a bug that drops our request.
|
|
|
|
*/
|
2003-10-07 17:54:04 -04:00
|
|
|
}
|
2007-09-29 14:18:47 -04:00
|
|
|
end_query(channel, query, query->error_status, NULL, 0);
|
2003-10-07 17:54:04 -04: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-22 18:18:45 -04:00
|
|
|
if (server->tcp_socket == ARES_SOCKET_BAD)
|
2004-10-06 03:50:18 -04:00
|
|
|
{
|
|
|
|
if (open_tcp_socket(channel, server) == -1)
|
|
|
|
{
|
2007-09-28 10:26:11 -04:00
|
|
|
skip_server(channel, query, query->server);
|
2004-10-06 03:50:18 -04:00
|
|
|
next_server(channel, query, now);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2004-07-24 17:47:49 -04:00
|
|
|
sendreq = calloc(sizeof(struct send_request), 1);
|
2003-10-07 17:54:04 -04:00
|
|
|
if (!sendreq)
|
2004-07-24 17:47:49 -04:00
|
|
|
{
|
2004-10-06 03:50:18 -04:00
|
|
|
end_query(channel, query, ARES_ENOMEM, NULL, 0);
|
2004-07-24 17:47:49 -04:00
|
|
|
return;
|
|
|
|
}
|
2007-09-28 10:28:14 -04:00
|
|
|
/* To make the common case fast, we avoid copies by using the
|
|
|
|
* query's tcpbuf for as long as the query is alive. In the rare
|
|
|
|
* case where the query ends while it's queued for transmission,
|
|
|
|
* then we give the sendreq its own copy of the request packet
|
|
|
|
* and put it in sendreq->data_storage.
|
|
|
|
*/
|
|
|
|
sendreq->data_storage = NULL;
|
2003-10-07 17:54:04 -04:00
|
|
|
sendreq->data = query->tcpbuf;
|
|
|
|
sendreq->len = query->tcplen;
|
2007-09-28 10:28:14 -04:00
|
|
|
sendreq->owner_query = query;
|
2003-10-07 17:54:04 -04:00
|
|
|
sendreq->next = NULL;
|
|
|
|
if (server->qtail)
|
2004-10-06 03:50:18 -04:00
|
|
|
server->qtail->next = sendreq;
|
2003-10-07 17:54:04 -04:00
|
|
|
else
|
2006-05-03 02:11:44 -04:00
|
|
|
{
|
|
|
|
SOCK_STATE_CALLBACK(channel, server->tcp_socket, 1, 1);
|
|
|
|
server->qhead = sendreq;
|
|
|
|
}
|
2003-10-07 17:54:04 -04:00
|
|
|
server->qtail = sendreq;
|
2007-09-28 10:28:14 -04:00
|
|
|
query->server_info[query->server].tcp_connection_generation =
|
|
|
|
server->tcp_connection_generation;
|
2003-10-07 17:54:04 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-07-22 18:18:45 -04:00
|
|
|
if (server->udp_socket == ARES_SOCKET_BAD)
|
2004-10-06 03:50:18 -04:00
|
|
|
{
|
|
|
|
if (open_udp_socket(channel, server) == -1)
|
|
|
|
{
|
2007-09-28 10:26:11 -04:00
|
|
|
skip_server(channel, query, query->server);
|
2004-10-06 03:50:18 -04:00
|
|
|
next_server(channel, query, now);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2006-07-28 14:01:23 -04:00
|
|
|
if (swrite(server->udp_socket, query->qbuf, query->qlen) == -1)
|
2004-10-06 03:50:18 -04:00
|
|
|
{
|
2006-06-18 21:18:05 -04:00
|
|
|
/* FIXME: Handle EAGAIN here since it likely can happen. */
|
2007-09-28 10:26:11 -04:00
|
|
|
skip_server(channel, query, query->server);
|
2004-10-06 03:50:18 -04:00
|
|
|
next_server(channel, query, now);
|
|
|
|
return;
|
|
|
|
}
|
2003-10-07 17:54:04 -04:00
|
|
|
}
|
2007-09-29 10:25:14 -04:00
|
|
|
query->timeout = now
|
|
|
|
+ ((query->try == 0) ? channel->timeout
|
|
|
|
: channel->timeout << query->try / channel->nservers);
|
2007-09-29 14:18:47 -04:00
|
|
|
/* Keep track of queries bucketed by timeout, so we can process
|
|
|
|
* timeout events quickly.
|
|
|
|
*/
|
|
|
|
ares__remove_from_list(&(query->queries_by_timeout));
|
|
|
|
ares__insert_in_list(
|
|
|
|
&(query->queries_by_timeout),
|
|
|
|
&(channel->queries_by_timeout[query->timeout %
|
|
|
|
ARES_TIMEOUT_TABLE_SIZE]));
|
2007-10-08 10:38:51 -04:00
|
|
|
|
2007-09-29 14:18:47 -04:00
|
|
|
/* Keep track of queries bucketed by server, so we can process server
|
|
|
|
* errors quickly.
|
|
|
|
*/
|
|
|
|
ares__remove_from_list(&(query->queries_to_server));
|
|
|
|
ares__insert_in_list(&(query->queries_to_server),
|
|
|
|
&(server->queries_to_server));
|
2003-10-07 17:54:04 -04:00
|
|
|
}
|
|
|
|
|
2005-06-02 07:58:04 -04:00
|
|
|
/*
|
2007-10-18 13:17:18 -04:00
|
|
|
* setsocknonblock sets the given socket to either blocking or non-blocking mode
|
2005-06-02 07:58:04 -04:00
|
|
|
* based on the 'nonblock' boolean argument. This function is highly portable.
|
|
|
|
*/
|
2007-10-18 13:17:18 -04:00
|
|
|
static int setsocknonblock(ares_socket_t sockfd, /* operate on this */
|
2005-06-02 07:58:04 -04:00
|
|
|
int nonblock /* TRUE or FALSE */)
|
2003-10-07 17:54:04 -04:00
|
|
|
{
|
2005-06-02 07:58:04 -04:00
|
|
|
#undef SETBLOCK
|
|
|
|
#define SETBLOCK 0
|
|
|
|
#ifdef HAVE_O_NONBLOCK
|
|
|
|
/* most recent unix versions */
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
flags = fcntl(sockfd, F_GETFL, 0);
|
2007-02-21 21:51:54 -05:00
|
|
|
if (FALSE != nonblock)
|
2005-06-02 07:58:04 -04:00
|
|
|
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-22 18:18:45 -04:00
|
|
|
int flags;
|
2005-06-02 07:58:04 -04:00
|
|
|
|
|
|
|
flags = nonblock;
|
|
|
|
return ioctl(sockfd, FIONBIO, &flags);
|
|
|
|
#undef SETBLOCK
|
|
|
|
#define SETBLOCK 2
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(HAVE_IOCTLSOCKET) && (SETBLOCK == 0)
|
2007-02-04 08:34:34 -05:00
|
|
|
#ifdef WATT32
|
|
|
|
char flags;
|
|
|
|
#else
|
2005-06-02 07:58:04 -04:00
|
|
|
/* Windows? */
|
|
|
|
unsigned long flags;
|
2007-02-04 08:34:34 -05:00
|
|
|
#endif
|
2005-06-02 07:58:04 -04:00
|
|
|
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 04:21:54 -05:00
|
|
|
#endif
|
2005-06-02 07:58:04 -04:00
|
|
|
}
|
|
|
|
|
2007-09-28 11:15:39 -04:00
|
|
|
static int configure_socket(int s, ares_channel channel)
|
|
|
|
{
|
2007-10-18 13:17:18 -04:00
|
|
|
setsocknonblock(s, TRUE);
|
2007-09-28 11:15:39 -04:00
|
|
|
|
2007-10-08 10:38:51 -04:00
|
|
|
#if defined(FD_CLOEXEC) && !defined(MSDOS)
|
2007-09-28 11:15:39 -04:00
|
|
|
/* Configure the socket fd as close-on-exec. */
|
|
|
|
if (fcntl(s, F_SETFD, FD_CLOEXEC) == -1)
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Set the socket's send and receive buffer sizes. */
|
|
|
|
if ((channel->socket_send_buffer_size > 0) &&
|
|
|
|
setsockopt(s, SOL_SOCKET, SO_SNDBUF,
|
2007-09-30 15:43:23 -04:00
|
|
|
(void *)&channel->socket_send_buffer_size,
|
2007-09-28 11:15:39 -04:00
|
|
|
sizeof(channel->socket_send_buffer_size)) == -1)
|
|
|
|
return -1;
|
2007-09-29 10:34:59 -04:00
|
|
|
|
2007-09-28 11:15:39 -04:00
|
|
|
if ((channel->socket_receive_buffer_size > 0) &&
|
|
|
|
setsockopt(s, SOL_SOCKET, SO_RCVBUF,
|
2007-09-30 15:43:23 -04:00
|
|
|
(void *)&channel->socket_receive_buffer_size,
|
2007-09-28 11:15:39 -04:00
|
|
|
sizeof(channel->socket_receive_buffer_size)) == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
2007-09-29 10:34:59 -04:00
|
|
|
}
|
2007-09-28 11:15:39 -04:00
|
|
|
|
2005-06-02 07:58:04 -04:00
|
|
|
static int open_tcp_socket(ares_channel channel, struct server_state *server)
|
|
|
|
{
|
2005-03-13 04:21:54 -05:00
|
|
|
ares_socket_t s;
|
2007-09-28 11:15:39 -04:00
|
|
|
int opt;
|
2004-02-04 05:23:15 -05:00
|
|
|
struct sockaddr_in sockin;
|
2003-10-07 17:54:04 -04:00
|
|
|
|
|
|
|
/* Acquire a socket. */
|
|
|
|
s = socket(AF_INET, SOCK_STREAM, 0);
|
2004-07-22 18:18:45 -04:00
|
|
|
if (s == ARES_SOCKET_BAD)
|
2003-10-07 17:54:04 -04:00
|
|
|
return -1;
|
|
|
|
|
2007-09-28 11:15:39 -04:00
|
|
|
/* Configure it. */
|
|
|
|
if (configure_socket(s, channel) < 0)
|
|
|
|
{
|
|
|
|
close(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Disable the Nagle algorithm (only relevant for TCP sockets, and thus not in
|
|
|
|
* configure_socket). In general, in DNS lookups we're pretty much interested
|
|
|
|
* in firing off a single request and then waiting for a reply, so batching
|
|
|
|
* isn't very interesting in general.
|
|
|
|
*/
|
|
|
|
opt = 1;
|
2007-09-29 10:34:59 -04:00
|
|
|
if (setsockopt(s, IPPROTO_TCP, TCP_NODELAY,
|
2007-09-30 15:43:23 -04:00
|
|
|
(void *)&opt, sizeof(opt)) == -1)
|
2007-09-28 11:15:39 -04:00
|
|
|
{
|
|
|
|
close(s);
|
|
|
|
return -1;
|
|
|
|
}
|
2004-07-01 09:54:24 -04:00
|
|
|
|
2003-10-07 17:54:04 -04:00
|
|
|
/* Connect to the server. */
|
2004-02-04 05:23:15 -05:00
|
|
|
memset(&sockin, 0, sizeof(sockin));
|
|
|
|
sockin.sin_family = AF_INET;
|
|
|
|
sockin.sin_addr = server->addr;
|
2007-02-01 10:36:56 -05:00
|
|
|
sockin.sin_port = (unsigned short)(channel->tcp_port & 0xffff);
|
2004-07-01 09:54:24 -04:00
|
|
|
if (connect(s, (struct sockaddr *) &sockin, sizeof(sockin)) == -1) {
|
2007-02-16 10:04:44 -05:00
|
|
|
int err = SOCKERRNO;
|
2004-07-01 09:54:24 -04:00
|
|
|
|
|
|
|
if (err != EINPROGRESS && err != EWOULDBLOCK) {
|
2003-10-07 17:54:04 -04:00
|
|
|
closesocket(s);
|
|
|
|
return -1;
|
|
|
|
}
|
2004-07-01 09:54:24 -04:00
|
|
|
}
|
2003-10-07 17:54:04 -04:00
|
|
|
|
2006-05-03 02:11:44 -04:00
|
|
|
SOCK_STATE_CALLBACK(channel, s, 1, 0);
|
2004-07-24 17:47:49 -04:00
|
|
|
server->tcp_buffer_pos = 0;
|
2003-10-07 17:54:04 -04:00
|
|
|
server->tcp_socket = s;
|
2007-09-28 10:28:14 -04:00
|
|
|
server->tcp_connection_generation = ++channel->tcp_connection_generation;
|
2003-10-07 17:54:04 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int open_udp_socket(ares_channel channel, struct server_state *server)
|
|
|
|
{
|
2004-07-22 18:18:45 -04:00
|
|
|
ares_socket_t s;
|
2004-02-04 05:23:15 -05:00
|
|
|
struct sockaddr_in sockin;
|
2003-10-07 17:54:04 -04:00
|
|
|
|
|
|
|
/* Acquire a socket. */
|
|
|
|
s = socket(AF_INET, SOCK_DGRAM, 0);
|
2004-07-22 18:18:45 -04:00
|
|
|
if (s == ARES_SOCKET_BAD)
|
2003-10-07 17:54:04 -04:00
|
|
|
return -1;
|
|
|
|
|
2005-06-02 07:58:04 -04:00
|
|
|
/* Set the socket non-blocking. */
|
2007-09-28 11:15:39 -04:00
|
|
|
if (configure_socket(s, channel) < 0)
|
|
|
|
{
|
|
|
|
close(s);
|
|
|
|
return -1;
|
|
|
|
}
|
2005-06-02 07:58:04 -04:00
|
|
|
|
2003-10-07 17:54:04 -04:00
|
|
|
/* Connect to the server. */
|
2004-02-04 05:23:15 -05:00
|
|
|
memset(&sockin, 0, sizeof(sockin));
|
|
|
|
sockin.sin_family = AF_INET;
|
|
|
|
sockin.sin_addr = server->addr;
|
2007-02-01 10:36:56 -05:00
|
|
|
sockin.sin_port = (unsigned short)(channel->udp_port & 0xffff);
|
2004-02-04 05:23:15 -05:00
|
|
|
if (connect(s, (struct sockaddr *) &sockin, sizeof(sockin)) == -1)
|
2003-10-07 17:54:04 -04:00
|
|
|
{
|
|
|
|
closesocket(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-05-03 02:11:44 -04:00
|
|
|
SOCK_STATE_CALLBACK(channel, s, 1, 0);
|
|
|
|
|
2003-10-07 17:54:04 -04:00
|
|
|
server->udp_socket = s;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int same_questions(const unsigned char *qbuf, int qlen,
|
2004-10-06 03:50:18 -04:00
|
|
|
const unsigned char *abuf, int alen)
|
2003-10-07 17:54:04 -04:00
|
|
|
{
|
|
|
|
struct {
|
|
|
|
const unsigned char *p;
|
|
|
|
int qdcount;
|
|
|
|
char *name;
|
2004-02-23 02:52:20 -05:00
|
|
|
long namelen;
|
2003-10-07 17:54:04 -04: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 03:50:18 -04:00
|
|
|
!= ARES_SUCCESS)
|
|
|
|
return 0;
|
2003-10-07 17:54:04 -04:00
|
|
|
q.p += q.namelen;
|
|
|
|
if (q.p + QFIXEDSZ > qbuf + qlen)
|
2004-10-06 03:50:18 -04:00
|
|
|
{
|
|
|
|
free(q.name);
|
|
|
|
return 0;
|
|
|
|
}
|
2003-10-07 17:54:04 -04: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 03:50:18 -04: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 17:54:04 -04:00
|
|
|
|
|
|
|
free(q.name);
|
|
|
|
if (j == a.qdcount)
|
2004-10-06 03:50:18 -04:00
|
|
|
return 0;
|
2003-10-07 17:54:04 -04:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-09-29 14:18:47 -04:00
|
|
|
static void end_query (ares_channel channel, struct query *query, int status,
|
|
|
|
unsigned char *abuf, int alen)
|
2003-10-07 17:54:04 -04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2007-09-28 10:28:14 -04:00
|
|
|
/* First we check to see if this query ended while one of our send
|
|
|
|
* queues still has pointers to it.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < channel->nservers; i++)
|
|
|
|
{
|
|
|
|
struct server_state *server = &channel->servers[i];
|
|
|
|
struct send_request *sendreq;
|
|
|
|
for (sendreq = server->qhead; sendreq; sendreq = sendreq->next)
|
|
|
|
if (sendreq->owner_query == query)
|
|
|
|
{
|
|
|
|
sendreq->owner_query = NULL;
|
|
|
|
assert(sendreq->data_storage == NULL);
|
|
|
|
if (status == ARES_SUCCESS)
|
|
|
|
{
|
|
|
|
/* We got a reply for this query, but this queued
|
|
|
|
* sendreq points into this soon-to-be-gone query's
|
|
|
|
* tcpbuf. Probably this means we timed out and queued
|
|
|
|
* the query for retransmission, then received a
|
|
|
|
* response before actually retransmitting. This is
|
|
|
|
* perfectly fine, so we want to keep the connection
|
|
|
|
* running smoothly if we can. But in the worst case
|
|
|
|
* we may have sent only some prefix of the query,
|
|
|
|
* with some suffix of the query left to send. Also,
|
|
|
|
* the buffer may be queued on multiple queues. To
|
|
|
|
* prevent dangling pointers to the query's tcpbuf and
|
|
|
|
* handle these cases, we just give such sendreqs
|
|
|
|
* their own copy of the query packet.
|
|
|
|
*/
|
|
|
|
sendreq->data_storage = malloc(sendreq->len);
|
|
|
|
if (sendreq->data_storage != NULL)
|
|
|
|
{
|
|
|
|
memcpy(sendreq->data_storage, sendreq->data, sendreq->len);
|
|
|
|
sendreq->data = sendreq->data_storage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((status != ARES_SUCCESS) || (sendreq->data_storage == NULL))
|
|
|
|
{
|
|
|
|
/* We encountered an error (probably a timeout,
|
|
|
|
* suggesting the DNS server we're talking to is
|
|
|
|
* probably unreachable, wedged, or severely
|
|
|
|
* overloaded) or we couldn't copy the request, so
|
|
|
|
* mark the connection as broken. When we get to
|
|
|
|
* process_broken_connections() we'll close the
|
|
|
|
* connection and try to re-send requests to another
|
|
|
|
* server.
|
|
|
|
*/
|
|
|
|
server->is_broken = 1;
|
|
|
|
/* Just to be paranoid, zero out this sendreq... */
|
|
|
|
sendreq->data = NULL;
|
|
|
|
sendreq->len = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-09-29 10:34:59 -04:00
|
|
|
|
|
|
|
/* Invoke the callback */
|
2007-09-28 10:46:51 -04:00
|
|
|
query->callback(query->arg, status, query->timeouts, abuf, alen);
|
2007-09-29 14:18:47 -04:00
|
|
|
ares__free_query(query);
|
2003-10-07 17:54:04 -04:00
|
|
|
|
|
|
|
/* Simple cleanup policy: if no queries are remaining, close all
|
|
|
|
* network sockets unless STAYOPEN is set.
|
|
|
|
*/
|
2007-09-29 14:18:47 -04:00
|
|
|
if (!(channel->flags & ARES_FLAG_STAYOPEN) &&
|
|
|
|
ares__is_list_empty(&(channel->all_queries)))
|
2003-10-07 17:54:04 -04:00
|
|
|
{
|
|
|
|
for (i = 0; i < channel->nservers; i++)
|
2006-05-03 02:11:44 -04:00
|
|
|
ares__close_sockets(channel, &channel->servers[i]);
|
2003-10-07 17:54:04 -04:00
|
|
|
}
|
2007-09-29 14:18:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ares__free_query(struct query *query)
|
|
|
|
{
|
|
|
|
/* Remove the query from all the lists in which it is linked */
|
|
|
|
ares__remove_from_list(&(query->queries_by_qid));
|
|
|
|
ares__remove_from_list(&(query->queries_by_timeout));
|
|
|
|
ares__remove_from_list(&(query->queries_to_server));
|
|
|
|
ares__remove_from_list(&(query->all_queries));
|
|
|
|
/* Zero out some important stuff, to help catch bugs */
|
|
|
|
query->callback = NULL;
|
|
|
|
query->arg = NULL;
|
|
|
|
/* Deallocate the memory associated with the query */
|
|
|
|
free(query->tcpbuf);
|
|
|
|
free(query->server_info);
|
|
|
|
free(query);
|
2003-10-07 17:54:04 -04:00
|
|
|
}
|