2004-11-07 02:33:06 +01:00
|
|
|
/* Copyright 2003-2004 Roger Dingledine.
|
2005-04-01 22:15:56 +02:00
|
|
|
* Copyright 2004-2005 Roger Dingledine, Nick Mathewson. */
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
/* $Id$ */
|
2005-12-14 21:40:40 +01:00
|
|
|
const char dns_c_id[] =
|
|
|
|
"$Id$";
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/**
|
|
|
|
* \file dns.c
|
2005-06-11 07:31:17 +02:00
|
|
|
* \brief Implements a farm of 'DNS worker' threads or processes to
|
|
|
|
* perform DNS lookups for onion routers and cache the results.
|
|
|
|
* [This needs to be done in the background because of the lack of a
|
|
|
|
* good, ubiquitous asynchronous DNS implementation.]
|
2004-05-09 18:47:25 +02:00
|
|
|
**/
|
2004-05-05 23:32:43 +02:00
|
|
|
|
2005-12-14 21:40:40 +01:00
|
|
|
/* See
|
|
|
|
* http://elvin.dstc.com/ListArchive/elvin-dev/archive/2001/09/msg00027.html
|
2003-06-24 07:17:09 +02:00
|
|
|
* for some approaches to asynchronous dns. We will want to switch once one of
|
|
|
|
* them becomes more commonly available.
|
|
|
|
*/
|
|
|
|
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
#include "or.h"
|
2005-11-23 05:18:45 +01:00
|
|
|
#include "../common/ht.h"
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Longest hostname we're willing to resolve. */
|
2003-02-14 08:53:55 +01:00
|
|
|
#define MAX_ADDRESSLEN 256
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Maximum DNS processes to spawn. */
|
2004-09-22 05:56:41 +02:00
|
|
|
#define MAX_DNSWORKERS 100
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Minimum DNS processes to spawn. */
|
2003-06-17 16:31:05 +02:00
|
|
|
#define MIN_DNSWORKERS 3
|
2004-05-05 23:32:43 +02:00
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** If more than this many processes are idle, shut down the extras. */
|
2003-06-25 02:31:41 +02:00
|
|
|
#define MAX_IDLE_DNSWORKERS 10
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Possible outcomes from hostname lookup: permanent failure,
|
2004-05-10 12:27:54 +02:00
|
|
|
* transient (retryable) failure, and success. */
|
2004-03-12 19:45:42 +01:00
|
|
|
#define DNS_RESOLVE_FAILED_TRANSIENT 1
|
|
|
|
#define DNS_RESOLVE_FAILED_PERMANENT 2
|
|
|
|
#define DNS_RESOLVE_SUCCEEDED 3
|
|
|
|
|
2004-05-10 12:27:54 +02:00
|
|
|
/** How many dnsworkers we have running right now. */
|
2004-06-06 05:38:31 +02:00
|
|
|
static int num_dnsworkers=0;
|
2004-05-10 12:27:54 +02:00
|
|
|
/** How many of the running dnsworkers have an assigned task right now. */
|
2004-06-06 05:38:31 +02:00
|
|
|
static int num_dnsworkers_busy=0;
|
|
|
|
/** When did we last rotate the dnsworkers? */
|
|
|
|
static time_t last_rotation_time=0;
|
2003-02-14 08:53:55 +01:00
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Linked list of connections waiting for a DNS answer. */
|
2005-07-22 23:12:10 +02:00
|
|
|
typedef struct pending_connection_t {
|
|
|
|
connection_t *conn;
|
2003-02-14 08:53:55 +01:00
|
|
|
struct pending_connection_t *next;
|
2005-07-22 23:12:10 +02:00
|
|
|
} pending_connection_t;
|
2003-02-14 08:53:55 +01:00
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** A DNS request: possibly completed, possibly pending; cached_resolve
|
2005-12-03 03:01:18 +01:00
|
|
|
* structs are stored at the OR side in a hash table, and as a linked
|
2004-05-05 23:32:43 +02:00
|
|
|
* list from oldest to newest.
|
|
|
|
*/
|
2005-07-22 23:12:10 +02:00
|
|
|
typedef struct cached_resolve_t {
|
2005-11-23 05:18:45 +01:00
|
|
|
HT_ENTRY(cached_resolve_t) node;
|
2004-05-10 12:27:54 +02:00
|
|
|
char address[MAX_ADDRESSLEN]; /**< The hostname to be resolved. */
|
2004-05-10 09:37:10 +02:00
|
|
|
uint32_t addr; /**< IPv4 addr for <b>address</b>. */
|
2005-12-14 21:40:40 +01:00
|
|
|
char state; /**< 0 is pending; 1 means answer is valid; 2 means resolve
|
|
|
|
* failed. */
|
2003-02-14 08:53:55 +01:00
|
|
|
#define CACHE_STATE_PENDING 0
|
|
|
|
#define CACHE_STATE_VALID 1
|
|
|
|
#define CACHE_STATE_FAILED 2
|
2004-05-10 12:27:54 +02:00
|
|
|
uint32_t expire; /**< Remove items from cache after this time. */
|
2005-07-22 23:12:10 +02:00
|
|
|
pending_connection_t *pending_connections;
|
|
|
|
struct cached_resolve_t *next;
|
|
|
|
} cached_resolve_t;
|
2003-02-14 08:53:55 +01:00
|
|
|
|
2004-03-12 19:45:42 +01:00
|
|
|
static void purge_expired_resolves(uint32_t now);
|
|
|
|
static int assign_to_dnsworker(connection_t *exitconn);
|
2005-07-22 23:12:10 +02:00
|
|
|
static void dns_purge_resolve(cached_resolve_t *resolve);
|
2004-03-12 19:45:42 +01:00
|
|
|
static void dns_found_answer(char *address, uint32_t addr, char outcome);
|
2004-05-12 22:58:27 +02:00
|
|
|
static int dnsworker_main(void *data);
|
2004-03-12 19:45:42 +01:00
|
|
|
static int spawn_dnsworker(void);
|
2005-09-23 01:43:41 +02:00
|
|
|
static int spawn_enough_dnsworkers(void);
|
2004-06-17 20:13:09 +02:00
|
|
|
static void send_resolved_cell(connection_t *conn, uint8_t answer_type);
|
2004-03-12 19:45:42 +01:00
|
|
|
|
2005-12-03 03:01:18 +01:00
|
|
|
/** Hash table of cached_resolve objects. */
|
|
|
|
static HT_HEAD(cache_map, cached_resolve_t) cache_root;
|
2003-02-14 08:53:55 +01:00
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Function to compare hashed resolves on their addresses; used to
|
2005-12-03 03:21:31 +01:00
|
|
|
* implement hash tables. */
|
2005-11-23 05:18:45 +01:00
|
|
|
static INLINE int
|
|
|
|
cached_resolves_eq(cached_resolve_t *a, cached_resolve_t *b)
|
2005-09-30 03:09:52 +02:00
|
|
|
{
|
2003-02-14 08:53:55 +01:00
|
|
|
/* make this smarter one day? */
|
2005-11-23 05:18:45 +01:00
|
|
|
return !strncmp(a->address, b->address, MAX_ADDRESSLEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
static INLINE unsigned int
|
|
|
|
cached_resolve_hash(cached_resolve_t *a)
|
|
|
|
{
|
|
|
|
return ht_string_hash(a->address);
|
2003-02-14 08:53:55 +01:00
|
|
|
}
|
|
|
|
|
2005-12-03 03:01:18 +01:00
|
|
|
HT_PROTOTYPE(cache_map, cached_resolve_t, node, cached_resolve_hash,
|
2005-11-23 05:18:45 +01:00
|
|
|
cached_resolves_eq);
|
2005-12-03 03:01:18 +01:00
|
|
|
HT_GENERATE(cache_map, cached_resolve_t, node, cached_resolve_hash,
|
2005-11-23 05:18:45 +01:00
|
|
|
cached_resolves_eq, 0.6, malloc, realloc, free);
|
2003-02-14 08:53:55 +01:00
|
|
|
|
2004-05-10 12:27:54 +02:00
|
|
|
/** Initialize the DNS cache. */
|
2005-06-11 20:52:12 +02:00
|
|
|
static void
|
2005-12-03 03:01:18 +01:00
|
|
|
init_cache_map(void)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2005-11-23 05:18:45 +01:00
|
|
|
HT_INIT(&cache_root);
|
2003-02-14 08:53:55 +01:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Initialize the DNS subsystem; called by the OR process. */
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
dns_init(void)
|
|
|
|
{
|
2005-12-03 03:01:18 +01:00
|
|
|
init_cache_map();
|
2005-08-16 01:46:18 +02:00
|
|
|
dnsworkers_rotate();
|
2003-06-17 16:31:05 +02:00
|
|
|
}
|
2003-02-14 08:53:55 +01:00
|
|
|
|
2005-06-11 20:52:12 +02:00
|
|
|
/** Helper: free storage held by an entry in the DNS cache. */
|
2005-02-11 02:26:47 +01:00
|
|
|
static void
|
2005-07-22 23:12:10 +02:00
|
|
|
_free_cached_resolve(cached_resolve_t *r)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2005-03-14 04:18:35 +01:00
|
|
|
while (r->pending_connections) {
|
2005-07-22 23:12:10 +02:00
|
|
|
pending_connection_t *victim = r->pending_connections;
|
2005-02-11 02:26:47 +01:00
|
|
|
r->pending_connections = victim->next;
|
|
|
|
tor_free(victim);
|
|
|
|
}
|
|
|
|
tor_free(r);
|
|
|
|
}
|
|
|
|
|
2005-06-11 20:52:12 +02:00
|
|
|
/** Free all storage held in the DNS cache */
|
2005-02-11 02:26:47 +01:00
|
|
|
void
|
|
|
|
dns_free_all(void)
|
|
|
|
{
|
2005-11-23 05:18:45 +01:00
|
|
|
cached_resolve_t **ptr, **next, *item;
|
2005-12-03 03:01:18 +01:00
|
|
|
for (ptr = HT_START(cache_map, &cache_root); ptr != NULL; ptr = next) {
|
2005-11-23 05:18:45 +01:00
|
|
|
item = *ptr;
|
2005-12-03 03:01:18 +01:00
|
|
|
next = HT_NEXT_RMV(cache_map, &cache_root, ptr);
|
2005-11-23 05:18:45 +01:00
|
|
|
_free_cached_resolve(item);
|
2005-02-11 02:26:47 +01:00
|
|
|
}
|
2005-12-03 03:01:18 +01:00
|
|
|
HT_CLEAR(cache_map, &cache_root);
|
2005-02-11 02:26:47 +01:00
|
|
|
}
|
|
|
|
|
2004-05-10 12:27:54 +02:00
|
|
|
/** Linked list of resolved addresses, oldest to newest. */
|
2005-07-22 23:12:10 +02:00
|
|
|
static cached_resolve_t *oldest_cached_resolve = NULL;
|
|
|
|
static cached_resolve_t *newest_cached_resolve = NULL;
|
2003-06-25 09:19:30 +02:00
|
|
|
|
2004-05-10 06:34:48 +02:00
|
|
|
/** Remove every cached_resolve whose <b>expire</b> time is before <b>now</b>
|
2004-05-05 23:32:43 +02:00
|
|
|
* from the cache. */
|
2005-06-11 20:52:12 +02:00
|
|
|
static void
|
|
|
|
purge_expired_resolves(uint32_t now)
|
|
|
|
{
|
2005-07-22 23:12:10 +02:00
|
|
|
cached_resolve_t *resolve;
|
|
|
|
pending_connection_t *pend;
|
2004-06-02 00:09:58 +02:00
|
|
|
connection_t *pendconn;
|
2003-08-14 05:52:51 +02:00
|
|
|
|
|
|
|
/* this is fast because the linked list
|
|
|
|
* oldest_cached_resolve is ordered by when they came in.
|
|
|
|
*/
|
2004-11-28 10:05:49 +01:00
|
|
|
while (oldest_cached_resolve && (oldest_cached_resolve->expire < now)) {
|
2003-08-14 05:52:51 +02:00
|
|
|
resolve = oldest_cached_resolve;
|
2005-10-25 19:52:14 +02:00
|
|
|
debug(LD_EXIT,"Forgetting old cached resolve (address %s, expires %lu)",
|
2005-12-10 10:36:26 +01:00
|
|
|
safe_str(resolve->address), (unsigned long)resolve->expire);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (resolve->state == CACHE_STATE_PENDING) {
|
2005-12-14 21:40:40 +01:00
|
|
|
debug(LD_EXIT,"Bug: Expiring a dns resolve ('%s') that's still pending."
|
|
|
|
" Forgot to cull it?", safe_str(resolve->address));
|
2005-04-26 20:52:16 +02:00
|
|
|
tor_fragile_assert();
|
2004-06-02 00:09:58 +02:00
|
|
|
}
|
|
|
|
if (resolve->pending_connections) {
|
2005-10-25 19:52:14 +02:00
|
|
|
debug(LD_EXIT, "Closing pending connections on expiring DNS resolve!");
|
2005-04-26 20:52:16 +02:00
|
|
|
tor_fragile_assert();
|
2004-06-02 00:09:58 +02:00
|
|
|
while (resolve->pending_connections) {
|
|
|
|
pend = resolve->pending_connections;
|
|
|
|
resolve->pending_connections = pend->next;
|
|
|
|
/* Connections should only be pending if they have no socket. */
|
|
|
|
tor_assert(pend->conn->s == -1);
|
|
|
|
pendconn = pend->conn;
|
2005-03-01 23:42:31 +01:00
|
|
|
connection_edge_end(pendconn, END_STREAM_REASON_TIMEOUT,
|
2004-06-02 00:09:58 +02:00
|
|
|
pendconn->cpath_layer);
|
2005-04-06 08:43:21 +02:00
|
|
|
circuit_detach_stream(circuit_get_by_edge_conn(pendconn), pendconn);
|
2004-06-02 00:09:58 +02:00
|
|
|
connection_free(pendconn);
|
|
|
|
tor_free(pend);
|
|
|
|
}
|
2004-02-28 23:13:58 +01:00
|
|
|
}
|
2003-08-14 05:52:51 +02:00
|
|
|
oldest_cached_resolve = resolve->next;
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!oldest_cached_resolve) /* if there are no more, */
|
2005-12-14 21:40:40 +01:00
|
|
|
newest_cached_resolve = NULL; /* then make sure the list's tail knows
|
|
|
|
* that too */
|
2005-12-03 03:01:18 +01:00
|
|
|
HT_REMOVE(cache_map, &cache_root, resolve);
|
2004-03-04 19:43:44 +01:00
|
|
|
tor_free(resolve);
|
2003-08-14 05:52:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-11 20:52:12 +02:00
|
|
|
/** Send a response to the RESOVLE request of a connection. answer_type must
|
|
|
|
* be one of RESOLVED_TYPE_(IPV4|ERROR|ERROR_TRANSIENT) */
|
|
|
|
static void
|
|
|
|
send_resolved_cell(connection_t *conn, uint8_t answer_type)
|
2004-06-17 20:13:09 +02:00
|
|
|
{
|
|
|
|
char buf[RELAY_PAYLOAD_SIZE];
|
2004-10-14 04:47:09 +02:00
|
|
|
size_t buflen;
|
2004-06-17 20:13:09 +02:00
|
|
|
|
|
|
|
buf[0] = answer_type;
|
|
|
|
|
|
|
|
switch (answer_type)
|
|
|
|
{
|
|
|
|
case RESOLVED_TYPE_IPV4:
|
|
|
|
buf[1] = 4;
|
|
|
|
set_uint32(buf+2, htonl(conn->addr));
|
2005-09-02 20:53:31 +02:00
|
|
|
set_uint32(buf+6, htonl(MAX_DNS_ENTRY_AGE)); /*XXXX send a real TTL*/
|
|
|
|
buflen = 10;
|
2004-06-17 20:13:09 +02:00
|
|
|
break;
|
|
|
|
case RESOLVED_TYPE_ERROR_TRANSIENT:
|
|
|
|
case RESOLVED_TYPE_ERROR:
|
2005-09-02 20:53:31 +02:00
|
|
|
{
|
|
|
|
const char *errmsg = "Error resolving hostname";
|
|
|
|
int msglen = strlen(errmsg);
|
|
|
|
int ttl = (answer_type == RESOLVED_TYPE_ERROR ? MAX_DNS_ENTRY_AGE : 0);
|
|
|
|
buf[1] = msglen;
|
|
|
|
strlcpy(buf+2, errmsg, sizeof(buf)-2);
|
|
|
|
set_uint32(buf+2+msglen, htonl((uint32_t)ttl));
|
|
|
|
buflen = 6+msglen;
|
|
|
|
break;
|
|
|
|
}
|
2004-06-17 20:13:09 +02:00
|
|
|
default:
|
|
|
|
tor_assert(0);
|
|
|
|
}
|
2005-04-06 08:43:21 +02:00
|
|
|
connection_edge_send_command(conn, circuit_get_by_edge_conn(conn),
|
2004-06-17 20:13:09 +02:00
|
|
|
RELAY_COMMAND_RESOLVED, buf, buflen,
|
|
|
|
conn->cpath_layer);
|
|
|
|
}
|
|
|
|
|
2005-12-03 03:01:18 +01:00
|
|
|
/** Link <b>r</b> into the hash table of address-to-result mappings, and add it
|
|
|
|
* to the linked list of resolves-by-age. */
|
2004-11-12 17:39:03 +01:00
|
|
|
static void
|
2005-07-22 23:12:10 +02:00
|
|
|
insert_resolve(cached_resolve_t *r)
|
2004-11-12 17:39:03 +01:00
|
|
|
{
|
|
|
|
/* add us to the linked list of resolves */
|
|
|
|
if (!oldest_cached_resolve) {
|
|
|
|
oldest_cached_resolve = r;
|
|
|
|
} else {
|
|
|
|
newest_cached_resolve->next = r;
|
|
|
|
}
|
|
|
|
newest_cached_resolve = r;
|
|
|
|
|
2005-12-03 03:01:18 +01:00
|
|
|
HT_INSERT(cache_map, &cache_root, r);
|
2004-11-12 17:39:03 +01:00
|
|
|
}
|
|
|
|
|
2004-05-10 06:34:48 +02:00
|
|
|
/** See if we have a cache entry for <b>exitconn</b>-\>address. if so,
|
|
|
|
* if resolve valid, put it into <b>exitconn</b>-\>addr and return 1.
|
2005-02-04 02:49:58 +01:00
|
|
|
* If resolve failed, unlink exitconn if needed, free it, and return -1.
|
2003-02-14 08:53:55 +01:00
|
|
|
*
|
|
|
|
* Else, if seen before and pending, add conn to the pending list,
|
|
|
|
* and return 0.
|
|
|
|
*
|
|
|
|
* Else, if not seen before, add conn to pending list, hand to
|
|
|
|
* dns farm, and return 0.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
|
|
|
dns_resolve(connection_t *exitconn)
|
|
|
|
{
|
2005-07-22 23:12:10 +02:00
|
|
|
cached_resolve_t *resolve;
|
|
|
|
cached_resolve_t search;
|
|
|
|
pending_connection_t *pending_connection;
|
2004-03-28 23:16:52 +02:00
|
|
|
struct in_addr in;
|
2005-02-04 02:49:58 +01:00
|
|
|
circuit_t *circ;
|
2003-06-25 09:19:30 +02:00
|
|
|
uint32_t now = time(NULL);
|
2004-02-25 08:31:46 +01:00
|
|
|
assert_connection_ok(exitconn, 0);
|
2004-06-02 00:09:58 +02:00
|
|
|
tor_assert(exitconn->s == -1);
|
2003-02-14 08:53:55 +01:00
|
|
|
|
2004-04-09 23:31:09 +02:00
|
|
|
/* first check if exitconn->address is an IP. If so, we already
|
|
|
|
* know the answer. */
|
|
|
|
if (tor_inet_aton(exitconn->address, &in) != 0) {
|
|
|
|
exitconn->addr = ntohl(in.s_addr);
|
2005-02-05 22:03:24 +01:00
|
|
|
if (exitconn->purpose == EXIT_PURPOSE_RESOLVE)
|
|
|
|
send_resolved_cell(exitconn, RESOLVED_TYPE_IPV4);
|
2004-04-09 23:31:09 +02:00
|
|
|
return 1;
|
2004-03-28 23:16:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* then take this opportunity to see if there are any expired
|
2005-12-03 03:01:18 +01:00
|
|
|
* resolves in the hash table. */
|
2003-08-14 05:52:51 +02:00
|
|
|
purge_expired_resolves(now);
|
2003-02-14 08:53:55 +01:00
|
|
|
|
2005-04-06 22:25:21 +02:00
|
|
|
/* lower-case exitconn->address, so it's in canonical form */
|
|
|
|
tor_strlower(exitconn->address);
|
|
|
|
|
2005-12-03 03:21:31 +01:00
|
|
|
/* now check the hash table to see if 'address' is already there. */
|
2004-10-27 08:03:28 +02:00
|
|
|
strlcpy(search.address, exitconn->address, sizeof(search.address));
|
2005-12-03 03:01:18 +01:00
|
|
|
resolve = HT_FIND(cache_map, &cache_root, &search);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (resolve) { /* already there */
|
|
|
|
switch (resolve->state) {
|
2003-02-14 08:53:55 +01:00
|
|
|
case CACHE_STATE_PENDING:
|
|
|
|
/* add us to the pending list */
|
2004-06-17 20:13:09 +02:00
|
|
|
pending_connection = tor_malloc_zero(
|
2005-07-22 23:12:10 +02:00
|
|
|
sizeof(pending_connection_t));
|
2003-02-14 08:53:55 +01:00
|
|
|
pending_connection->conn = exitconn;
|
2003-02-18 02:35:55 +01:00
|
|
|
pending_connection->next = resolve->pending_connections;
|
|
|
|
resolve->pending_connections = pending_connection;
|
2005-12-14 21:40:40 +01:00
|
|
|
debug(LD_EXIT,"Connection (fd %d) waiting for pending DNS "
|
|
|
|
"resolve of '%s'",
|
2005-12-10 10:36:26 +01:00
|
|
|
exitconn->s, safe_str(exitconn->address));
|
2004-04-09 23:31:09 +02:00
|
|
|
exitconn->state = EXIT_CONN_STATE_RESOLVING;
|
2003-02-18 02:35:55 +01:00
|
|
|
return 0;
|
2003-02-14 08:53:55 +01:00
|
|
|
case CACHE_STATE_VALID:
|
2003-12-14 08:40:47 +01:00
|
|
|
exitconn->addr = resolve->addr;
|
2005-10-25 19:52:14 +02:00
|
|
|
debug(LD_EXIT,"Connection (fd %d) found cached answer for '%s'",
|
2005-12-10 10:36:26 +01:00
|
|
|
exitconn->s, safe_str(exitconn->address));
|
2004-06-17 20:13:09 +02:00
|
|
|
if (exitconn->purpose == EXIT_PURPOSE_RESOLVE)
|
|
|
|
send_resolved_cell(exitconn, RESOLVED_TYPE_IPV4);
|
2003-08-14 05:52:51 +02:00
|
|
|
return 1;
|
2003-02-14 08:53:55 +01:00
|
|
|
case CACHE_STATE_FAILED:
|
2005-10-25 19:52:14 +02:00
|
|
|
debug(LD_EXIT,"Connection (fd %d) found cached error for '%s'",
|
2005-12-10 10:36:26 +01:00
|
|
|
exitconn->s, safe_str(exitconn->address));
|
2004-06-17 20:13:09 +02:00
|
|
|
if (exitconn->purpose == EXIT_PURPOSE_RESOLVE)
|
|
|
|
send_resolved_cell(exitconn, RESOLVED_TYPE_ERROR);
|
2005-04-06 08:43:21 +02:00
|
|
|
circ = circuit_get_by_edge_conn(exitconn);
|
2005-02-04 02:49:58 +01:00
|
|
|
if (circ)
|
|
|
|
circuit_detach_stream(circ, exitconn);
|
2005-03-14 05:42:52 +01:00
|
|
|
if (!exitconn->marked_for_close)
|
|
|
|
connection_free(exitconn);
|
2003-02-14 08:53:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(0);
|
2003-12-14 01:12:02 +01:00
|
|
|
}
|
|
|
|
/* not there, need to add it */
|
2005-07-22 23:12:10 +02:00
|
|
|
resolve = tor_malloc_zero(sizeof(cached_resolve_t));
|
2003-12-14 01:12:02 +01:00
|
|
|
resolve->state = CACHE_STATE_PENDING;
|
2003-12-14 08:40:47 +01:00
|
|
|
resolve->expire = now + MAX_DNS_ENTRY_AGE;
|
2004-10-27 08:03:28 +02:00
|
|
|
strlcpy(resolve->address, exitconn->address, sizeof(resolve->address));
|
2003-12-14 01:12:02 +01:00
|
|
|
|
|
|
|
/* add us to the pending list */
|
2005-07-22 23:12:10 +02:00
|
|
|
pending_connection = tor_malloc_zero(sizeof(pending_connection_t));
|
2003-12-14 01:12:02 +01:00
|
|
|
pending_connection->conn = exitconn;
|
|
|
|
resolve->pending_connections = pending_connection;
|
2004-04-09 23:31:09 +02:00
|
|
|
exitconn->state = EXIT_CONN_STATE_RESOLVING;
|
2003-12-14 01:12:02 +01:00
|
|
|
|
2004-11-12 17:39:03 +01:00
|
|
|
insert_resolve(resolve);
|
2003-12-14 01:12:02 +01:00
|
|
|
return assign_to_dnsworker(exitconn);
|
2003-02-14 08:53:55 +01:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Find or spawn a dns worker process to handle resolving
|
2004-05-10 06:34:48 +02:00
|
|
|
* <b>exitconn</b>-\>address; tell that dns worker to begin resolving.
|
2004-05-05 23:32:43 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static int
|
|
|
|
assign_to_dnsworker(connection_t *exitconn)
|
|
|
|
{
|
2003-02-14 08:53:55 +01:00
|
|
|
connection_t *dnsconn;
|
|
|
|
unsigned char len;
|
2003-06-17 16:31:05 +02:00
|
|
|
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(exitconn->state == EXIT_CONN_STATE_RESOLVING);
|
2004-06-02 00:09:58 +02:00
|
|
|
tor_assert(exitconn->s == -1);
|
2004-04-09 23:31:09 +02:00
|
|
|
|
2005-09-23 01:43:41 +02:00
|
|
|
/* respawn here, to be sure there are enough */
|
|
|
|
if (spawn_enough_dnsworkers() < 0) {
|
|
|
|
goto err;
|
|
|
|
}
|
2003-02-14 08:53:55 +01:00
|
|
|
|
2005-12-14 21:40:40 +01:00
|
|
|
dnsconn = connection_get_by_type_state(CONN_TYPE_DNSWORKER,
|
|
|
|
DNSWORKER_STATE_IDLE);
|
2003-02-14 08:53:55 +01:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!dnsconn) {
|
2005-10-25 19:52:14 +02:00
|
|
|
warn(LD_EXIT,"no idle dns workers. Failing.");
|
2005-01-28 09:53:47 +01:00
|
|
|
if (exitconn->purpose == EXIT_PURPOSE_RESOLVE)
|
|
|
|
send_resolved_cell(exitconn, RESOLVED_TYPE_ERROR_TRANSIENT);
|
2005-09-23 01:43:41 +02:00
|
|
|
goto err;
|
2003-02-14 08:53:55 +01:00
|
|
|
}
|
|
|
|
|
2005-10-25 19:52:14 +02:00
|
|
|
debug(LD_EXIT,
|
2005-12-10 10:36:26 +01:00
|
|
|
"Connection (fd %d) needs to resolve '%s'; assigning "
|
|
|
|
"to DNSWorker (fd %d)",
|
2005-10-25 19:52:14 +02:00
|
|
|
exitconn->s, safe_str(exitconn->address), dnsconn->s);
|
2003-11-08 05:02:05 +01:00
|
|
|
|
2004-03-04 19:43:44 +01:00
|
|
|
tor_free(dnsconn->address);
|
2003-10-04 05:29:09 +02:00
|
|
|
dnsconn->address = tor_strdup(exitconn->address);
|
2003-06-17 16:31:05 +02:00
|
|
|
dnsconn->state = DNSWORKER_STATE_BUSY;
|
2003-08-21 01:05:22 +02:00
|
|
|
num_dnsworkers_busy++;
|
2003-02-14 08:53:55 +01:00
|
|
|
|
2003-06-17 16:31:05 +02:00
|
|
|
len = strlen(dnsconn->address);
|
2005-05-07 07:55:06 +02:00
|
|
|
connection_write_to_buf((char*)&len, 1, dnsconn);
|
2003-10-04 04:38:18 +02:00
|
|
|
connection_write_to_buf(dnsconn->address, len, dnsconn);
|
2003-02-14 08:53:55 +01:00
|
|
|
|
|
|
|
return 0;
|
2005-09-23 01:43:41 +02:00
|
|
|
err:
|
2005-12-14 21:40:40 +01:00
|
|
|
dns_cancel_pending_resolve(exitconn->address); /* also sends end and frees */
|
2005-09-23 01:43:41 +02:00
|
|
|
return -1;
|
2003-02-14 08:53:55 +01:00
|
|
|
}
|
|
|
|
|
2004-05-10 06:34:48 +02:00
|
|
|
/** Remove <b>conn</b> from the list of connections waiting for conn-\>address.
|
2004-05-05 23:32:43 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
connection_dns_remove(connection_t *conn)
|
2004-02-28 23:23:44 +01:00
|
|
|
{
|
2005-07-22 23:12:10 +02:00
|
|
|
pending_connection_t *pend, *victim;
|
|
|
|
cached_resolve_t search;
|
|
|
|
cached_resolve_t *resolve;
|
2004-02-28 23:23:44 +01:00
|
|
|
|
2004-06-02 20:12:49 +02:00
|
|
|
tor_assert(conn->type == CONN_TYPE_EXIT);
|
|
|
|
tor_assert(conn->state == EXIT_CONN_STATE_RESOLVING);
|
|
|
|
|
2004-10-27 08:03:28 +02:00
|
|
|
strlcpy(search.address, conn->address, sizeof(search.address));
|
2004-02-28 23:23:44 +01:00
|
|
|
|
2005-12-03 03:01:18 +01:00
|
|
|
resolve = HT_FIND(cache_map, &cache_root, &search);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!resolve) {
|
2005-10-25 19:52:14 +02:00
|
|
|
/* XXXX RD This *is* a bug, right? -NM */
|
2005-12-14 21:40:40 +01:00
|
|
|
notice(LD_BUG, "Address '%s' is not pending. Dropping.",
|
|
|
|
safe_str(conn->address));
|
2004-02-28 23:23:44 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(resolve->pending_connections);
|
2004-02-28 23:23:44 +01:00
|
|
|
assert_connection_ok(conn,0);
|
|
|
|
|
|
|
|
pend = resolve->pending_connections;
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (pend->conn == conn) {
|
2004-02-28 23:23:44 +01:00
|
|
|
resolve->pending_connections = pend->next;
|
2004-03-04 19:43:44 +01:00
|
|
|
tor_free(pend);
|
2005-12-14 21:40:40 +01:00
|
|
|
debug(LD_EXIT, "First connection (fd %d) no longer waiting for resolve "
|
|
|
|
"of '%s'",
|
2005-12-10 10:36:26 +01:00
|
|
|
conn->s, safe_str(conn->address));
|
2004-02-28 23:23:44 +01:00
|
|
|
return;
|
|
|
|
} else {
|
2004-11-28 10:05:49 +01:00
|
|
|
for ( ; pend->next; pend = pend->next) {
|
|
|
|
if (pend->next->conn == conn) {
|
2004-02-28 23:23:44 +01:00
|
|
|
victim = pend->next;
|
|
|
|
pend->next = victim->next;
|
2004-03-04 19:43:44 +01:00
|
|
|
tor_free(victim);
|
2005-12-14 21:40:40 +01:00
|
|
|
debug(LD_EXIT, "Connection (fd %d) no longer waiting for resolve "
|
|
|
|
"of '%s'",
|
2005-12-10 10:36:26 +01:00
|
|
|
conn->s, safe_str(conn->address));
|
2004-02-28 23:23:44 +01:00
|
|
|
return; /* more are pending */
|
|
|
|
}
|
|
|
|
}
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(0); /* not reachable unless onlyconn not in pending list */
|
2004-02-28 23:23:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Log an error and abort if conn is waiting for a DNS resolve.
|
2004-05-05 23:32:43 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
assert_connection_edge_not_dns_pending(connection_t *conn)
|
|
|
|
{
|
2005-07-22 23:12:10 +02:00
|
|
|
pending_connection_t *pend;
|
2005-11-23 05:18:45 +01:00
|
|
|
cached_resolve_t **resolve;
|
2004-03-28 06:54:36 +02:00
|
|
|
|
2005-12-03 03:01:18 +01:00
|
|
|
HT_FOREACH(resolve, cache_map, &cache_root) {
|
2005-11-23 05:18:45 +01:00
|
|
|
for (pend = (*resolve)->pending_connections;
|
2004-11-28 12:39:53 +01:00
|
|
|
pend;
|
|
|
|
pend = pend->next) {
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(pend->conn != conn);
|
2004-03-28 06:54:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Log an error and abort if any connection waiting for a DNS resolve is
|
2004-05-05 23:32:43 +02:00
|
|
|
* corrupted. */
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
assert_all_pending_dns_resolves_ok(void)
|
|
|
|
{
|
2005-07-22 23:12:10 +02:00
|
|
|
pending_connection_t *pend;
|
2005-11-23 05:18:45 +01:00
|
|
|
cached_resolve_t **resolve;
|
2004-04-09 11:39:42 +02:00
|
|
|
|
2005-12-03 03:01:18 +01:00
|
|
|
HT_FOREACH(resolve, cache_map, &cache_root) {
|
2005-11-23 05:18:45 +01:00
|
|
|
for (pend = (*resolve)->pending_connections;
|
2004-11-28 12:39:53 +01:00
|
|
|
pend;
|
|
|
|
pend = pend->next) {
|
2004-04-09 11:39:42 +02:00
|
|
|
assert_connection_ok(pend->conn, 0);
|
2004-06-02 00:09:58 +02:00
|
|
|
tor_assert(pend->conn->s == -1);
|
|
|
|
tor_assert(!connection_in_array(pend->conn));
|
2004-04-09 11:39:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-10 06:34:48 +02:00
|
|
|
/** Mark all connections waiting for <b>address</b> for close. Then cancel
|
|
|
|
* the resolve for <b>address</b> itself, and remove any cached results for
|
|
|
|
* <b>address</b> from the cache.
|
2003-06-27 02:57:04 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
dns_cancel_pending_resolve(char *address)
|
|
|
|
{
|
2005-07-22 23:12:10 +02:00
|
|
|
pending_connection_t *pend;
|
|
|
|
cached_resolve_t search;
|
|
|
|
cached_resolve_t *resolve;
|
2004-03-04 21:49:38 +01:00
|
|
|
connection_t *pendconn;
|
2005-01-28 09:53:47 +01:00
|
|
|
circuit_t *circ;
|
2003-06-25 09:19:30 +02:00
|
|
|
|
2004-10-27 08:03:28 +02:00
|
|
|
strlcpy(search.address, address, sizeof(search.address));
|
2003-06-25 09:19:30 +02:00
|
|
|
|
2005-12-03 03:01:18 +01:00
|
|
|
resolve = HT_FIND(cache_map, &cache_root, &search);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!resolve) {
|
2005-10-25 19:52:14 +02:00
|
|
|
/* XXXX RD This *is* a bug, right? -NM */
|
|
|
|
notice(LD_BUG,"Address '%s' is not pending. Dropping.", safe_str(address));
|
2003-06-25 09:19:30 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-01-04 07:21:06 +01:00
|
|
|
if (!resolve->pending_connections) {
|
|
|
|
/* XXX this should never trigger, but sometimes it does */
|
2005-10-25 19:52:14 +02:00
|
|
|
warn(LD_BUG,"Bug: Address '%s' is pending but has no pending connections!",
|
2005-12-10 10:36:26 +01:00
|
|
|
safe_str(address));
|
2005-04-26 20:52:16 +02:00
|
|
|
tor_fragile_assert();
|
2005-01-04 07:21:06 +01:00
|
|
|
return;
|
|
|
|
}
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(resolve->pending_connections);
|
2003-06-25 09:19:30 +02:00
|
|
|
|
2004-02-28 23:23:44 +01:00
|
|
|
/* mark all pending connections to fail */
|
2005-10-25 19:52:14 +02:00
|
|
|
debug(LD_EXIT, "Failing all connections waiting on DNS resolve of '%s'",
|
|
|
|
safe_str(address));
|
2004-11-28 10:05:49 +01:00
|
|
|
while (resolve->pending_connections) {
|
2003-06-25 09:19:30 +02:00
|
|
|
pend = resolve->pending_connections;
|
2004-03-04 19:43:44 +01:00
|
|
|
pend->conn->state = EXIT_CONN_STATE_RESOLVEFAILED;
|
2004-06-02 20:32:24 +02:00
|
|
|
pendconn = pend->conn;
|
2004-06-02 00:09:58 +02:00
|
|
|
tor_assert(pendconn->s == -1);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!pendconn->marked_for_close) {
|
2005-01-28 09:53:47 +01:00
|
|
|
connection_edge_end(pendconn, END_STREAM_REASON_RESOURCELIMIT,
|
|
|
|
pendconn->cpath_layer);
|
2004-05-20 01:32:20 +02:00
|
|
|
}
|
2005-04-06 08:43:21 +02:00
|
|
|
circ = circuit_get_by_edge_conn(pendconn);
|
2005-01-28 09:53:47 +01:00
|
|
|
if (circ)
|
|
|
|
circuit_detach_stream(circ, pendconn);
|
2004-06-02 00:09:58 +02:00
|
|
|
connection_free(pendconn);
|
2004-03-04 19:43:44 +01:00
|
|
|
resolve->pending_connections = pend->next;
|
|
|
|
tor_free(pend);
|
2004-02-28 23:23:44 +01:00
|
|
|
}
|
|
|
|
|
2004-03-12 19:45:42 +01:00
|
|
|
dns_purge_resolve(resolve);
|
|
|
|
}
|
|
|
|
|
2004-05-10 06:34:48 +02:00
|
|
|
/** Remove <b>resolve</b> from the cache.
|
2004-05-05 23:32:43 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static void
|
2005-07-22 23:12:10 +02:00
|
|
|
dns_purge_resolve(cached_resolve_t *resolve)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2005-07-22 23:12:10 +02:00
|
|
|
cached_resolve_t *tmp;
|
2004-03-12 19:45:42 +01:00
|
|
|
|
2004-02-28 23:23:44 +01:00
|
|
|
/* remove resolve from the linked list */
|
2004-11-28 10:05:49 +01:00
|
|
|
if (resolve == oldest_cached_resolve) {
|
2004-02-28 23:23:44 +01:00
|
|
|
oldest_cached_resolve = resolve->next;
|
2004-11-28 10:05:49 +01:00
|
|
|
if (oldest_cached_resolve == NULL)
|
2004-02-28 23:23:44 +01:00
|
|
|
newest_cached_resolve = NULL;
|
2003-06-27 02:57:04 +02:00
|
|
|
} else {
|
2004-02-28 23:23:44 +01:00
|
|
|
/* FFFF make it a doubly linked list if this becomes too slow */
|
2005-12-14 21:40:40 +01:00
|
|
|
for (tmp=oldest_cached_resolve; tmp && tmp->next != resolve; tmp=tmp->next)
|
|
|
|
;
|
|
|
|
tor_assert(tmp); /* it's got to be in the list, or we screwed up somewhere
|
|
|
|
* else */
|
2004-02-28 23:23:44 +01:00
|
|
|
tmp->next = resolve->next; /* unlink it */
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (newest_cached_resolve == resolve)
|
2004-02-28 23:23:44 +01:00
|
|
|
newest_cached_resolve = tmp;
|
2003-06-25 09:19:30 +02:00
|
|
|
}
|
2004-02-28 23:23:44 +01:00
|
|
|
|
2005-12-03 03:21:31 +01:00
|
|
|
/* remove resolve from the map */
|
2005-12-03 03:01:18 +01:00
|
|
|
HT_REMOVE(cache_map, &cache_root, resolve);
|
2004-02-28 23:23:44 +01:00
|
|
|
|
2004-03-04 19:43:44 +01:00
|
|
|
tor_free(resolve);
|
2003-06-25 09:19:30 +02:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Called on the OR side when a DNS worker tells us the outcome of a DNS
|
2004-05-05 23:32:43 +02:00
|
|
|
* resolve: tell all pending connections about the result of the lookup, and
|
2004-05-10 06:34:48 +02:00
|
|
|
* cache the value. (<b>address</b> is a NUL-terminated string containing the
|
|
|
|
* address to look up; <b>addr</b> is an IPv4 address in host order;
|
|
|
|
* <b>outcome</b> is one of
|
|
|
|
* DNS_RESOLVE_{FAILED_TRANSIENT|FAILED_PERMANENT|SUCCEEDED}.
|
2004-05-05 23:32:43 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static void
|
|
|
|
dns_found_answer(char *address, uint32_t addr, char outcome)
|
|
|
|
{
|
2005-07-22 23:12:10 +02:00
|
|
|
pending_connection_t *pend;
|
|
|
|
cached_resolve_t search;
|
|
|
|
cached_resolve_t *resolve;
|
2004-03-04 21:49:38 +01:00
|
|
|
connection_t *pendconn;
|
2004-05-06 13:08:04 +02:00
|
|
|
circuit_t *circ;
|
2003-02-14 08:53:55 +01:00
|
|
|
|
2004-10-27 08:03:28 +02:00
|
|
|
strlcpy(search.address, address, sizeof(search.address));
|
2003-02-14 08:53:55 +01:00
|
|
|
|
2005-12-03 03:01:18 +01:00
|
|
|
resolve = HT_FIND(cache_map, &cache_root, &search);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!resolve) {
|
2005-10-25 19:52:14 +02:00
|
|
|
info(LD_EXIT,"Resolved unasked address '%s'; caching anyway.",
|
|
|
|
safe_str(address));
|
2005-07-22 23:12:10 +02:00
|
|
|
resolve = tor_malloc_zero(sizeof(cached_resolve_t));
|
2004-11-12 17:39:03 +01:00
|
|
|
resolve->state = (outcome == DNS_RESOLVE_SUCCEEDED) ?
|
|
|
|
CACHE_STATE_VALID : CACHE_STATE_FAILED;
|
|
|
|
resolve->addr = addr;
|
|
|
|
resolve->expire = time(NULL) + MAX_DNS_ENTRY_AGE;
|
|
|
|
insert_resolve(resolve);
|
2003-06-25 09:19:30 +02:00
|
|
|
return;
|
2003-02-14 08:53:55 +01:00
|
|
|
}
|
|
|
|
|
2003-11-08 05:02:05 +01:00
|
|
|
if (resolve->state != CACHE_STATE_PENDING) {
|
2004-05-05 23:32:43 +02:00
|
|
|
/* XXXX Maybe update addr? or check addr for consistency? Or let
|
|
|
|
* VALID replace FAILED? */
|
2005-10-25 19:52:14 +02:00
|
|
|
notice(LD_EXIT, "Resolved '%s' which was already resolved; ignoring",
|
2005-05-03 12:04:08 +02:00
|
|
|
safe_str(address));
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(resolve->pending_connections == NULL);
|
2003-11-08 05:02:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* Removed this assertion: in fact, we'll sometimes get a double answer
|
|
|
|
* to the same question. This can happen when we ask one worker to resolve
|
|
|
|
* X.Y.Z., then we cancel the request, and then we ask another worker to
|
|
|
|
* resolve X.Y.Z. */
|
2004-04-25 22:37:37 +02:00
|
|
|
/* tor_assert(resolve->state == CACHE_STATE_PENDING); */
|
2003-02-14 08:53:55 +01:00
|
|
|
|
2004-11-12 17:39:03 +01:00
|
|
|
resolve->addr = addr;
|
2004-11-28 10:05:49 +01:00
|
|
|
if (outcome == DNS_RESOLVE_SUCCEEDED)
|
2003-02-14 08:53:55 +01:00
|
|
|
resolve->state = CACHE_STATE_VALID;
|
|
|
|
else
|
|
|
|
resolve->state = CACHE_STATE_FAILED;
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
while (resolve->pending_connections) {
|
2003-02-14 08:53:55 +01:00
|
|
|
pend = resolve->pending_connections;
|
2004-02-28 22:52:58 +01:00
|
|
|
assert_connection_ok(pend->conn,time(NULL));
|
2003-12-14 08:40:47 +01:00
|
|
|
pend->conn->addr = resolve->addr;
|
2004-06-17 20:13:09 +02:00
|
|
|
pendconn = pend->conn; /* don't pass complex things to the
|
|
|
|
connection_mark_for_close macro */
|
2004-04-09 23:31:09 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (resolve->state == CACHE_STATE_FAILED) {
|
2004-04-11 19:07:45 +02:00
|
|
|
/* prevent double-remove. */
|
2004-05-06 13:08:04 +02:00
|
|
|
pendconn->state = EXIT_CONN_STATE_RESOLVEFAILED;
|
2004-09-21 18:42:07 +02:00
|
|
|
if (pendconn->purpose == EXIT_PURPOSE_CONNECT) {
|
2005-12-14 21:40:40 +01:00
|
|
|
connection_edge_end(pendconn, END_STREAM_REASON_RESOLVEFAILED,
|
|
|
|
pendconn->cpath_layer);
|
2004-09-21 19:33:05 +02:00
|
|
|
/* This detach must happen after we send the end cell. */
|
2005-04-06 08:43:21 +02:00
|
|
|
circuit_detach_stream(circuit_get_by_edge_conn(pendconn), pendconn);
|
2004-09-21 18:42:07 +02:00
|
|
|
} else {
|
2004-06-17 20:13:09 +02:00
|
|
|
send_resolved_cell(pendconn, RESOLVED_TYPE_ERROR);
|
2004-09-21 18:42:07 +02:00
|
|
|
/* This detach must happen after we send the resolved cell. */
|
2005-04-06 08:43:21 +02:00
|
|
|
circuit_detach_stream(circuit_get_by_edge_conn(pendconn), pendconn);
|
2004-09-21 18:42:07 +02:00
|
|
|
}
|
2004-05-06 13:08:04 +02:00
|
|
|
connection_free(pendconn);
|
2004-02-22 21:50:20 +01:00
|
|
|
} else {
|
2004-06-17 20:13:09 +02:00
|
|
|
if (pendconn->purpose == EXIT_PURPOSE_CONNECT) {
|
|
|
|
/* prevent double-remove. */
|
|
|
|
pend->conn->state = EXIT_CONN_STATE_CONNECTING;
|
|
|
|
|
2005-04-06 08:43:21 +02:00
|
|
|
circ = circuit_get_by_edge_conn(pend->conn);
|
2004-06-17 20:13:09 +02:00
|
|
|
tor_assert(circ);
|
|
|
|
/* unlink pend->conn from resolving_streams, */
|
|
|
|
circuit_detach_stream(circ, pend->conn);
|
|
|
|
/* and link it to n_streams */
|
|
|
|
pend->conn->next_stream = circ->n_streams;
|
2005-04-06 08:13:49 +02:00
|
|
|
pend->conn->on_circuit = circ;
|
2004-06-17 20:13:09 +02:00
|
|
|
circ->n_streams = pend->conn;
|
|
|
|
|
|
|
|
connection_exit_connect(pend->conn);
|
|
|
|
} else {
|
|
|
|
/* prevent double-remove. This isn't really an accurate state,
|
|
|
|
* but it does the right thing. */
|
|
|
|
pendconn->state = EXIT_CONN_STATE_RESOLVEFAILED;
|
|
|
|
send_resolved_cell(pendconn, RESOLVED_TYPE_IPV4);
|
2005-04-06 08:43:21 +02:00
|
|
|
circ = circuit_get_by_edge_conn(pendconn);
|
2004-06-17 20:13:09 +02:00
|
|
|
tor_assert(circ);
|
|
|
|
circuit_detach_stream(circ, pendconn);
|
|
|
|
connection_free(pendconn);
|
|
|
|
}
|
2004-02-22 21:50:20 +01:00
|
|
|
}
|
2004-03-04 19:43:44 +01:00
|
|
|
resolve->pending_connections = pend->next;
|
|
|
|
tor_free(pend);
|
2003-02-14 08:53:55 +01:00
|
|
|
}
|
2004-03-12 19:45:42 +01:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (outcome == DNS_RESOLVE_FAILED_TRANSIENT) { /* remove from cache */
|
2004-03-12 19:45:42 +01:00
|
|
|
dns_purge_resolve(resolve);
|
|
|
|
}
|
2003-02-14 08:53:55 +01:00
|
|
|
}
|
|
|
|
|
2003-06-17 16:31:05 +02:00
|
|
|
/******************************************************************/
|
|
|
|
|
2004-05-10 06:34:48 +02:00
|
|
|
/*
|
2004-05-05 23:32:43 +02:00
|
|
|
* Connection between OR and dnsworker
|
2004-05-10 06:34:48 +02:00
|
|
|
*/
|
2004-05-05 23:32:43 +02:00
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Write handler: called when we've pushed a request to a dnsworker. */
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
|
|
|
connection_dns_finished_flushing(connection_t *conn)
|
|
|
|
{
|
2004-10-17 00:14:52 +02:00
|
|
|
tor_assert(conn);
|
|
|
|
tor_assert(conn->type == CONN_TYPE_DNSWORKER);
|
2003-06-17 16:31:05 +02:00
|
|
|
connection_stop_writing(conn);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
|
|
|
connection_dns_reached_eof(connection_t *conn)
|
|
|
|
{
|
2005-10-25 19:52:14 +02:00
|
|
|
warn(LD_EXIT,"Read eof. Worker died unexpectedly.");
|
2004-11-28 10:05:49 +01:00
|
|
|
if (conn->state == DNSWORKER_STATE_BUSY) {
|
2005-01-04 12:26:08 +01:00
|
|
|
/* don't cancel the resolve here -- it would be cancelled in
|
|
|
|
* connection_about_to_close_connection(), since conn is still
|
|
|
|
* in state BUSY
|
|
|
|
*/
|
2004-11-21 11:14:57 +01:00
|
|
|
num_dnsworkers_busy--;
|
|
|
|
}
|
|
|
|
num_dnsworkers--;
|
|
|
|
connection_mark_for_close(conn);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Read handler: called when we get data from a dnsworker. See
|
2004-05-05 23:32:43 +02:00
|
|
|
* if we have a complete answer. If so, call dns_found_answer on the
|
|
|
|
* result. If not, wait. Returns 0. */
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
|
|
|
connection_dns_process_inbuf(connection_t *conn)
|
|
|
|
{
|
2004-03-20 10:30:30 +01:00
|
|
|
char success;
|
2003-12-14 08:40:47 +01:00
|
|
|
uint32_t addr;
|
2003-06-17 16:31:05 +02:00
|
|
|
|
2004-10-17 00:14:52 +02:00
|
|
|
tor_assert(conn);
|
|
|
|
tor_assert(conn->type == CONN_TYPE_DNSWORKER);
|
2003-06-17 16:31:05 +02:00
|
|
|
|
2004-12-07 00:19:55 +01:00
|
|
|
if (conn->state != DNSWORKER_STATE_BUSY && buf_datalen(conn->inbuf)) {
|
2005-12-14 21:40:40 +01:00
|
|
|
warn(LD_BUG,"Bug: read data (%d bytes) from an idle dns worker (fd %d, "
|
|
|
|
"address '%s'). Please report.",
|
2005-10-25 19:52:14 +02:00
|
|
|
(int)buf_datalen(conn->inbuf), conn->s, safe_str(conn->address));
|
2005-04-26 20:52:16 +02:00
|
|
|
tor_fragile_assert();
|
2005-04-26 21:08:06 +02:00
|
|
|
|
|
|
|
/* Pull it off the buffer anyway, or it will just stay there.
|
|
|
|
* Keep pulling things off because sometimes we get several
|
|
|
|
* answers at once (!). */
|
|
|
|
while (buf_datalen(conn->inbuf)) {
|
|
|
|
connection_fetch_from_buf(&success,1,conn);
|
|
|
|
connection_fetch_from_buf((char *)&addr,sizeof(uint32_t),conn);
|
2005-10-25 19:52:14 +02:00
|
|
|
warn(LD_EXIT,"Discarding idle dns answer (success %d, addr %d.)",
|
2005-12-10 10:36:26 +01:00
|
|
|
success, addr); // XXX safe_str
|
2005-04-26 21:08:06 +02:00
|
|
|
}
|
2004-08-25 07:26:09 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2004-11-28 10:05:49 +01:00
|
|
|
if (buf_datalen(conn->inbuf) < 5) /* entire answer available? */
|
2003-06-17 16:31:05 +02:00
|
|
|
return 0; /* not yet */
|
2004-08-25 07:26:09 +02:00
|
|
|
tor_assert(conn->state == DNSWORKER_STATE_BUSY);
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(buf_datalen(conn->inbuf) == 5);
|
2003-06-17 16:31:05 +02:00
|
|
|
|
2004-03-20 10:30:30 +01:00
|
|
|
connection_fetch_from_buf(&success,1,conn);
|
|
|
|
connection_fetch_from_buf((char *)&addr,sizeof(uint32_t),conn);
|
2003-06-17 16:31:05 +02:00
|
|
|
|
2005-10-25 19:52:14 +02:00
|
|
|
debug(LD_EXIT, "DNSWorker (fd %d) returned answer for '%s'",
|
|
|
|
conn->s, safe_str(conn->address));
|
2003-11-08 05:02:05 +01:00
|
|
|
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(success >= DNS_RESOLVE_FAILED_TRANSIENT);
|
|
|
|
tor_assert(success <= DNS_RESOLVE_SUCCEEDED);
|
2004-11-12 17:39:03 +01:00
|
|
|
dns_found_answer(conn->address, ntohl(addr), success);
|
2003-06-17 16:31:05 +02:00
|
|
|
|
2004-03-04 19:43:44 +01:00
|
|
|
tor_free(conn->address);
|
2003-11-13 07:49:25 +01:00
|
|
|
conn->address = tor_strdup("<idle>");
|
2003-06-17 16:31:05 +02:00
|
|
|
conn->state = DNSWORKER_STATE_IDLE;
|
2003-08-21 01:05:22 +02:00
|
|
|
num_dnsworkers_busy--;
|
2004-06-06 05:38:31 +02:00
|
|
|
if (conn->timestamp_created < last_rotation_time) {
|
|
|
|
connection_mark_for_close(conn);
|
|
|
|
num_dnsworkers--;
|
|
|
|
spawn_enough_dnsworkers();
|
|
|
|
}
|
2003-06-17 16:31:05 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-06-06 05:38:31 +02:00
|
|
|
/** Close and re-open all idle dnsworkers; schedule busy ones to be closed
|
|
|
|
* and re-opened once they're no longer busy.
|
|
|
|
**/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
dnsworkers_rotate(void)
|
2004-06-06 05:38:31 +02:00
|
|
|
{
|
|
|
|
connection_t *dnsconn;
|
|
|
|
while ((dnsconn = connection_get_by_type_state(CONN_TYPE_DNSWORKER,
|
|
|
|
DNSWORKER_STATE_IDLE))) {
|
|
|
|
connection_mark_for_close(dnsconn);
|
|
|
|
num_dnsworkers--;
|
|
|
|
}
|
|
|
|
last_rotation_time = time(NULL);
|
2005-08-16 01:46:18 +02:00
|
|
|
if (server_mode(get_options()))
|
|
|
|
spawn_enough_dnsworkers();
|
2004-06-06 05:38:31 +02:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Implementation for DNS workers; this code runs in a separate
|
2004-05-05 23:32:43 +02:00
|
|
|
* execution context. It takes as its argument an fdarray as returned
|
|
|
|
* by socketpair(), and communicates via fdarray[1]. The protocol is
|
|
|
|
* as follows:
|
2004-05-10 06:34:48 +02:00
|
|
|
* - The OR says:
|
|
|
|
* - ADDRESSLEN [1 byte]
|
|
|
|
* - ADDRESS [ADDRESSLEN bytes]
|
|
|
|
* - The DNS worker does the lookup, and replies:
|
|
|
|
* - OUTCOME [1 byte]
|
|
|
|
* - IP [4 bytes]
|
2004-05-05 23:32:43 +02:00
|
|
|
*
|
|
|
|
* OUTCOME is one of DNS_RESOLVE_{FAILED_TRANSIENT|FAILED_PERMANENT|SUCCEEDED}.
|
|
|
|
* IP is in host order.
|
|
|
|
*
|
|
|
|
* The dnsworker runs indefinitely, until its connection is closed or an error
|
|
|
|
* occurs.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static int
|
|
|
|
dnsworker_main(void *data)
|
|
|
|
{
|
2003-12-14 08:40:47 +01:00
|
|
|
char address[MAX_ADDRESSLEN];
|
|
|
|
unsigned char address_len;
|
2004-03-12 19:45:42 +01:00
|
|
|
char answer[5];
|
2004-04-28 22:11:37 +02:00
|
|
|
uint32_t ip;
|
2003-08-12 09:35:17 +02:00
|
|
|
int *fdarray = data;
|
2003-08-21 01:05:22 +02:00
|
|
|
int fd;
|
2004-07-04 06:52:43 +02:00
|
|
|
int result;
|
2003-08-12 09:35:17 +02:00
|
|
|
|
2005-12-14 21:40:40 +01:00
|
|
|
/* log_fn(LOG_NOTICE,"After spawn: fdarray @%d has %d:%d", (int)fdarray,
|
|
|
|
* fdarray[0],fdarray[1]); */
|
2005-01-03 20:07:25 +01:00
|
|
|
|
2003-08-12 09:35:17 +02:00
|
|
|
fd = fdarray[1]; /* this side is ours */
|
2005-01-03 19:06:51 +01:00
|
|
|
#ifndef TOR_IS_MULTITHREADED
|
2005-12-14 21:40:40 +01:00
|
|
|
tor_close_socket(fdarray[0]); /* this is the side of the socketpair the
|
|
|
|
* parent uses */
|
2005-05-03 20:44:20 +02:00
|
|
|
tor_free_all(1); /* so the child doesn't hold the parent's fd's open */
|
2004-08-08 09:25:45 +02:00
|
|
|
handle_signals(0); /* ignore interrupts from the keyboard, etc */
|
2005-01-03 20:07:25 +01:00
|
|
|
#endif
|
|
|
|
tor_free(data);
|
2003-06-17 16:31:05 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
for (;;) {
|
2005-01-19 22:34:42 +01:00
|
|
|
int r;
|
2003-06-17 16:31:05 +02:00
|
|
|
|
2005-01-19 22:34:42 +01:00
|
|
|
if ((r = recv(fd, &address_len, 1, 0)) != 1) {
|
|
|
|
if (r == 0) {
|
2005-12-14 21:40:40 +01:00
|
|
|
info(LD_EXIT,"DNS worker exiting because Tor process closed "
|
|
|
|
"connection (either pruned idle dnsworker or died).");
|
2005-01-19 22:34:42 +01:00
|
|
|
} else {
|
2005-12-14 21:40:40 +01:00
|
|
|
info(LD_EXIT,"DNS worker exiting because of error on connection "
|
|
|
|
"to Tor process.");
|
|
|
|
info(LD_EXIT,"(Error on %d was %s)", fd,
|
|
|
|
tor_socket_strerror(tor_socket_errno(fd)));
|
2005-01-19 22:34:42 +01:00
|
|
|
}
|
2005-04-04 23:53:26 +02:00
|
|
|
tor_close_socket(fd);
|
2005-10-25 21:01:48 +02:00
|
|
|
crypto_thread_cleanup();
|
2003-08-12 09:35:17 +02:00
|
|
|
spawn_exit();
|
2003-06-17 16:31:05 +02:00
|
|
|
}
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (address_len && read_all(fd, address, address_len, 1) != address_len) {
|
2005-10-25 19:52:14 +02:00
|
|
|
err(LD_BUG,"read hostname failed. Child exiting.");
|
2005-04-04 23:53:26 +02:00
|
|
|
tor_close_socket(fd);
|
2005-10-25 21:01:48 +02:00
|
|
|
crypto_thread_cleanup();
|
2003-08-12 09:35:17 +02:00
|
|
|
spawn_exit();
|
2003-06-17 16:31:05 +02:00
|
|
|
}
|
2003-12-14 08:40:47 +01:00
|
|
|
address[address_len] = 0; /* null terminate it */
|
2003-06-17 16:31:05 +02:00
|
|
|
|
2004-07-04 06:52:43 +02:00
|
|
|
result = tor_lookup_hostname(address, &ip);
|
|
|
|
/* Make 0.0.0.0 an error, so that we can use "0" to mean "no addr") */
|
|
|
|
if (!ip)
|
|
|
|
result = -1;
|
|
|
|
switch (result) {
|
2004-04-28 21:35:12 +02:00
|
|
|
case 1:
|
2004-11-12 17:39:03 +01:00
|
|
|
/* XXX result can never be 1, because we set it to -1 above on error */
|
2005-12-14 21:40:40 +01:00
|
|
|
info(LD_NET,"Could not resolve dest addr %s (transient).",
|
|
|
|
safe_str(address));
|
2004-03-12 19:45:42 +01:00
|
|
|
answer[0] = DNS_RESOLVE_FAILED_TRANSIENT;
|
2004-04-28 21:35:12 +02:00
|
|
|
break;
|
|
|
|
case -1:
|
2005-12-14 21:40:40 +01:00
|
|
|
info(LD_NET,"Could not resolve dest addr %s (permanent).",
|
|
|
|
safe_str(address));
|
2004-03-12 19:45:42 +01:00
|
|
|
answer[0] = DNS_RESOLVE_FAILED_PERMANENT;
|
2004-04-28 21:55:20 +02:00
|
|
|
break;
|
2004-04-28 21:35:12 +02:00
|
|
|
case 0:
|
2005-10-25 19:52:14 +02:00
|
|
|
info(LD_NET,"Resolved address '%s'.",safe_str(address));
|
2004-04-28 21:35:12 +02:00
|
|
|
answer[0] = DNS_RESOLVE_SUCCEEDED;
|
2004-04-28 21:55:20 +02:00
|
|
|
break;
|
2003-06-17 16:31:05 +02:00
|
|
|
}
|
2004-04-28 22:11:37 +02:00
|
|
|
set_uint32(answer+1, ip);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (write_all(fd, answer, 5, 1) != 5) {
|
2005-10-25 19:52:14 +02:00
|
|
|
err(LD_NET,"writing answer failed. Child exiting.");
|
2005-04-04 23:53:26 +02:00
|
|
|
tor_close_socket(fd);
|
2005-10-25 21:01:48 +02:00
|
|
|
crypto_thread_cleanup();
|
2004-03-12 19:45:42 +01:00
|
|
|
spawn_exit();
|
|
|
|
}
|
2003-06-17 16:31:05 +02:00
|
|
|
}
|
2003-08-12 09:35:17 +02:00
|
|
|
return 0; /* windows wants this function to return an int */
|
2003-06-17 16:31:05 +02:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Launch a new DNS worker; return 0 on success, -1 on failure.
|
2004-05-05 23:32:43 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static int
|
|
|
|
spawn_dnsworker(void)
|
|
|
|
{
|
2005-01-03 20:07:25 +01:00
|
|
|
int *fdarray;
|
|
|
|
int fd;
|
2003-06-17 16:31:05 +02:00
|
|
|
connection_t *conn;
|
2005-06-30 09:17:38 +02:00
|
|
|
int err;
|
2003-06-17 16:31:05 +02:00
|
|
|
|
2005-01-03 20:07:25 +01:00
|
|
|
fdarray = tor_malloc(sizeof(int)*2);
|
2005-06-30 09:17:38 +02:00
|
|
|
if ((err = tor_socketpair(AF_UNIX, SOCK_STREAM, 0, fdarray)) < 0) {
|
2005-12-14 21:40:40 +01:00
|
|
|
warn(LD_NET, "Couldn't construct socketpair: %s",
|
|
|
|
tor_socket_strerror(-err));
|
2005-01-03 20:07:25 +01:00
|
|
|
tor_free(fdarray);
|
2005-09-23 01:43:41 +02:00
|
|
|
return -1;
|
2003-06-17 16:31:05 +02:00
|
|
|
}
|
|
|
|
|
2005-12-14 21:40:40 +01:00
|
|
|
/* log_fn(LOG_NOTICE,"Before spawn: fdarray @%d has %d:%d",
|
|
|
|
(int)fdarray, fdarray[0],fdarray[1]); */
|
2005-01-03 20:07:25 +01:00
|
|
|
|
2005-12-14 21:40:40 +01:00
|
|
|
fd = fdarray[0]; /* We copy this out here, since dnsworker_main may free
|
|
|
|
* fdarray */
|
2005-01-03 20:07:25 +01:00
|
|
|
spawn_func(dnsworker_main, (void*)fdarray);
|
2005-12-26 23:42:22 +01:00
|
|
|
debug(LD_EXIT,"just spawned a dns worker.");
|
2005-01-03 19:06:51 +01:00
|
|
|
#ifndef TOR_IS_MULTITHREADED
|
2005-12-14 21:40:40 +01:00
|
|
|
tor_close_socket(fdarray[1]); /* don't need the worker's side of the pipe */
|
2005-01-03 20:07:25 +01:00
|
|
|
tor_free(fdarray);
|
2005-01-03 19:06:51 +01:00
|
|
|
#endif
|
2003-06-17 16:31:05 +02:00
|
|
|
|
|
|
|
conn = connection_new(CONN_TYPE_DNSWORKER);
|
|
|
|
|
2005-01-03 20:07:25 +01:00
|
|
|
set_socket_nonblocking(fd);
|
2003-06-17 16:31:05 +02:00
|
|
|
|
|
|
|
/* set up conn so it's got all the data we need to remember */
|
2005-01-03 20:07:25 +01:00
|
|
|
conn->s = fd;
|
2003-10-18 09:09:09 +02:00
|
|
|
conn->address = tor_strdup("<unused>");
|
2003-06-17 16:31:05 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (connection_add(conn) < 0) { /* no space, forget it */
|
2005-10-25 19:52:14 +02:00
|
|
|
warn(LD_NET,"connection_add failed. Giving up.");
|
2005-01-03 20:07:25 +01:00
|
|
|
connection_free(conn); /* this closes fd */
|
2003-06-17 16:31:05 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
conn->state = DNSWORKER_STATE_IDLE;
|
|
|
|
connection_start_reading(conn);
|
|
|
|
|
|
|
|
return 0; /* success */
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** If we have too many or too few DNS workers, spawn or kill some.
|
2005-09-23 01:43:41 +02:00
|
|
|
* Return 0 if we are happy, return -1 if we tried to spawn more but
|
|
|
|
* we couldn't.
|
2004-05-05 23:32:43 +02:00
|
|
|
*/
|
2005-09-23 01:43:41 +02:00
|
|
|
static int
|
2005-06-11 20:52:12 +02:00
|
|
|
spawn_enough_dnsworkers(void)
|
|
|
|
{
|
2003-08-21 01:05:22 +02:00
|
|
|
int num_dnsworkers_needed; /* aim to have 1 more than needed,
|
2003-06-17 16:31:05 +02:00
|
|
|
* but no less than min and no more than max */
|
2003-06-25 02:31:41 +02:00
|
|
|
connection_t *dnsconn;
|
|
|
|
|
2003-11-08 05:02:05 +01:00
|
|
|
/* XXX This may not be the best strategy. Maybe we should queue pending
|
2005-12-14 21:40:40 +01:00
|
|
|
* requests until the old ones finish or time out: otherwise, if the
|
|
|
|
* connection requests come fast enough, we never get any DNS done. -NM
|
|
|
|
*
|
2003-12-14 08:40:47 +01:00
|
|
|
* XXX But if we queue them, then the adversary can pile even more
|
2005-12-14 21:40:40 +01:00
|
|
|
* queries onto us, blocking legitimate requests for even longer. Maybe
|
|
|
|
* we should compromise and only kill if it's been at it for more than,
|
|
|
|
* e.g., 2 seconds. -RD
|
2003-11-08 05:02:05 +01:00
|
|
|
*/
|
2004-11-28 10:05:49 +01:00
|
|
|
if (num_dnsworkers_busy == MAX_DNSWORKERS) {
|
2003-06-25 02:31:41 +02:00
|
|
|
/* We always want at least one worker idle.
|
|
|
|
* So find the oldest busy worker and kill it.
|
|
|
|
*/
|
2003-12-14 08:40:47 +01:00
|
|
|
dnsconn = connection_get_by_type_state_lastwritten(CONN_TYPE_DNSWORKER,
|
|
|
|
DNSWORKER_STATE_BUSY);
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(dnsconn);
|
2003-06-25 09:19:30 +02:00
|
|
|
|
2005-10-25 19:52:14 +02:00
|
|
|
warn(LD_EXIT, "%d DNS workers are spawned; all are busy. Killing one.",
|
|
|
|
MAX_DNSWORKERS);
|
2003-06-25 02:31:41 +02:00
|
|
|
|
2004-05-12 23:12:33 +02:00
|
|
|
connection_mark_for_close(dnsconn);
|
2003-08-21 01:05:22 +02:00
|
|
|
num_dnsworkers_busy--;
|
2003-09-27 23:09:56 +02:00
|
|
|
num_dnsworkers--;
|
2003-06-25 02:31:41 +02:00
|
|
|
}
|
2003-06-17 16:31:05 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (num_dnsworkers_busy >= MIN_DNSWORKERS)
|
2003-08-21 01:05:22 +02:00
|
|
|
num_dnsworkers_needed = num_dnsworkers_busy+1;
|
2003-06-17 16:31:05 +02:00
|
|
|
else
|
2003-08-21 01:05:22 +02:00
|
|
|
num_dnsworkers_needed = MIN_DNSWORKERS;
|
2003-06-17 16:31:05 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
while (num_dnsworkers < num_dnsworkers_needed) {
|
|
|
|
if (spawn_dnsworker() < 0) {
|
2005-10-25 19:52:14 +02:00
|
|
|
warn(LD_EXIT,"Spawn failed. Will try again later.");
|
2005-09-23 01:43:41 +02:00
|
|
|
return -1;
|
2003-06-17 16:31:05 +02:00
|
|
|
}
|
2003-08-21 01:05:22 +02:00
|
|
|
num_dnsworkers++;
|
2003-06-17 16:31:05 +02:00
|
|
|
}
|
|
|
|
|
2005-06-28 03:53:15 +02:00
|
|
|
while (num_dnsworkers > num_dnsworkers_busy+MAX_IDLE_DNSWORKERS) {
|
|
|
|
/* too many idle? */
|
2003-06-25 02:31:41 +02:00
|
|
|
/* cull excess workers */
|
2005-10-25 19:52:14 +02:00
|
|
|
info(LD_EXIT,"%d of %d dnsworkers are idle. Killing one.",
|
2005-12-10 10:36:26 +01:00
|
|
|
num_dnsworkers-num_dnsworkers_busy, num_dnsworkers);
|
2005-12-14 21:40:40 +01:00
|
|
|
dnsconn = connection_get_by_type_state(CONN_TYPE_DNSWORKER,
|
|
|
|
DNSWORKER_STATE_IDLE);
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(dnsconn);
|
2004-05-12 23:12:33 +02:00
|
|
|
connection_mark_for_close(dnsconn);
|
2003-08-21 01:05:22 +02:00
|
|
|
num_dnsworkers--;
|
2003-06-25 02:31:41 +02:00
|
|
|
}
|
2005-09-23 01:43:41 +02:00
|
|
|
|
|
|
|
return 0;
|
2003-06-17 16:31:05 +02:00
|
|
|
}
|
2005-06-09 21:03:31 +02:00
|
|
|
|