2004-11-07 02:33:06 +01:00
|
|
|
/* Copyright 2003-2004 Roger Dingledine.
|
2006-02-09 06:46:49 +01:00
|
|
|
* Copyright 2004-2006 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
|
2006-08-04 20:24:25 +02:00
|
|
|
* \brief Implements a local cache for DNS results for Tor servers.
|
2006-08-11 09:31:16 +02:00
|
|
|
* We provide two asynchronous backend implementations:
|
2006-08-04 20:24:25 +02:00
|
|
|
* 1) A farm of 'DNS worker' threads or processes to perform DNS lookups for
|
|
|
|
* onion routers and cache the results.
|
|
|
|
* 2) A wrapper around Adam Langley's eventdns.c code, to send requests
|
|
|
|
* to the nameservers asynchronously.
|
|
|
|
* (We can't just use gethostbyname() and friends because we really need to
|
|
|
|
* be nonblocking.)
|
2004-05-09 18:47:25 +02:00
|
|
|
**/
|
2004-05-05 23:32:43 +02:00
|
|
|
|
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"
|
2006-06-03 22:52:24 +02:00
|
|
|
#ifdef USE_EVENTDNS
|
|
|
|
#include "eventdns.h"
|
|
|
|
#endif
|
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
|
|
|
|
2006-07-31 20:01:49 +02:00
|
|
|
/** How long will we wait for an answer from the resolver before we decide
|
|
|
|
* that the resolver is wedged? */
|
2006-07-31 20:01:18 +02:00
|
|
|
#define RESOLVE_MAX_TIMEOUT 300
|
|
|
|
|
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
|
|
|
|
|
2006-06-03 22:52:24 +02:00
|
|
|
#ifndef USE_EVENTDNS
|
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;
|
2006-06-03 22:52:24 +02:00
|
|
|
#endif
|
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 {
|
2006-07-26 21:07:26 +02:00
|
|
|
edge_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
|
|
|
|
2006-07-11 22:51:58 +02:00
|
|
|
#define CACHED_RESOLVE_MAGIC 0x1234F00D
|
|
|
|
|
2006-07-31 20:01:49 +02:00
|
|
|
/* Possible states for a cached resolve_t */
|
|
|
|
/** We are waiting for the resolver system to tell us an answer here.
|
|
|
|
* When we get one, or when we time out, the state of this cached_resolve_t
|
|
|
|
* will become "DONE" and we'll possibly add a CACHED_VALID or a CACHED_FAILED
|
|
|
|
* entry. This cached_resolve_t will be in the hash table so that we will
|
|
|
|
* know not to launch more requests for this addr, but rather to add more
|
|
|
|
* connections to the pending list for the addr. */
|
2006-07-31 20:01:18 +02:00
|
|
|
#define CACHE_STATE_PENDING 0
|
2006-07-31 20:01:49 +02:00
|
|
|
/** This used to be a pending cached_resolve_t, and we got an answer for it.
|
|
|
|
* Now we're waiting for this cached_resolve_t to expire. This should
|
|
|
|
* have no pending connections, and should not appear in the hash table. */
|
2006-07-31 20:01:18 +02:00
|
|
|
#define CACHE_STATE_DONE 1
|
2006-07-31 20:01:49 +02:00
|
|
|
/** We are caching an answer for this address. This should have no pending
|
|
|
|
* connections, and should appear in the hash table. */
|
2006-07-31 20:01:18 +02:00
|
|
|
#define CACHE_STATE_CACHED_VALID 2
|
2006-07-31 20:01:49 +02:00
|
|
|
/** We are caching a failure for this address. This should have no pending
|
|
|
|
* connections, and should appear in the hash table */
|
2006-07-31 20:01:18 +02:00
|
|
|
#define CACHE_STATE_CACHED_FAILED 3
|
|
|
|
|
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;
|
2006-07-11 22:51:58 +02:00
|
|
|
uint32_t magic;
|
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>. */
|
2006-08-03 06:22:25 +02:00
|
|
|
uint8_t state; /**< Is this cached entry pending/done/valid/failed? */
|
2006-07-31 20:00:18 +02:00
|
|
|
time_t expire; /**< Remove items from cache after this time. */
|
2006-06-03 23:41:14 +02:00
|
|
|
uint32_t ttl; /**< What TTL did the nameserver tell us? */
|
2005-07-22 23:12:10 +02:00
|
|
|
pending_connection_t *pending_connections;
|
|
|
|
} cached_resolve_t;
|
2003-02-14 08:53:55 +01:00
|
|
|
|
2006-08-10 11:02:26 +02:00
|
|
|
static void purge_expired_resolves(time_t now);
|
2006-07-10 20:38:57 +02:00
|
|
|
static void dns_found_answer(const char *address, uint32_t addr, char outcome,
|
2006-06-03 22:52:24 +02:00
|
|
|
uint32_t ttl);
|
2006-07-26 21:07:26 +02:00
|
|
|
static void send_resolved_cell(edge_connection_t *conn, uint8_t answer_type);
|
2006-07-31 20:01:49 +02:00
|
|
|
static int launch_resolve(edge_connection_t *exitconn);
|
2006-06-03 22:52:24 +02:00
|
|
|
#ifndef USE_EVENTDNS
|
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);
|
2006-08-04 20:24:41 +02:00
|
|
|
#else
|
|
|
|
static void configure_nameservers(void);
|
2006-06-03 22:52:24 +02:00
|
|
|
#endif
|
2006-08-14 22:16:21 +02:00
|
|
|
static void _assert_cache_ok(void);
|
|
|
|
#ifdef DEBUG_DNS_CACHE
|
|
|
|
#define assert_cache_ok() _assert_cache_ok()
|
|
|
|
#else
|
|
|
|
#define assert_cache_ok() do {} while(0)
|
|
|
|
#endif
|
2006-07-11 22:51:58 +02:00
|
|
|
static void assert_resolve_ok(cached_resolve_t *resolve);
|
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? */
|
2006-07-11 22:51:58 +02:00
|
|
|
assert_resolve_ok(a); // Not b; b may be just a search.
|
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
|
|
|
}
|
|
|
|
|
2006-07-05 23:28:37 +02:00
|
|
|
#ifdef USE_EVENTDNS
|
|
|
|
static void
|
|
|
|
eventdns_log_cb(const char *msg)
|
|
|
|
{
|
2006-07-07 19:33:30 +02:00
|
|
|
if (!strcmpstart(msg, "Resolve requested for") &&
|
|
|
|
get_options()->SafeLogging) {
|
|
|
|
log(LOG_INFO, LD_EXIT, "eventdns: Resolve requested.");
|
|
|
|
return;
|
|
|
|
}
|
2006-07-05 23:42:18 +02:00
|
|
|
log(LOG_INFO, LD_EXIT, "eventdns: %s", msg);
|
2006-07-05 23:28:37 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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();
|
2006-08-04 22:30:45 +02:00
|
|
|
#ifdef USE_EVENTDNS
|
2006-08-04 20:24:41 +02:00
|
|
|
if (server_mode(get_options()))
|
|
|
|
configure_nameservers();
|
2006-08-04 22:30:45 +02:00
|
|
|
#endif
|
2003-06-17 16:31:05 +02:00
|
|
|
}
|
2003-02-14 08:53:55 +01:00
|
|
|
|
2006-06-03 23:41:14 +02:00
|
|
|
uint32_t
|
|
|
|
dns_clip_ttl(uint32_t ttl)
|
|
|
|
{
|
|
|
|
if (ttl < MIN_DNS_TTL)
|
|
|
|
return MIN_DNS_TTL;
|
|
|
|
else if (ttl > MAX_DNS_TTL)
|
|
|
|
return MAX_DNS_TTL;
|
|
|
|
else
|
|
|
|
return ttl;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t
|
|
|
|
dns_get_expiry_ttl(uint32_t ttl)
|
|
|
|
{
|
|
|
|
if (ttl < MIN_DNS_TTL)
|
|
|
|
return MIN_DNS_TTL;
|
|
|
|
else if (ttl > MAX_DNS_ENTRY_AGE)
|
|
|
|
return MAX_DNS_ENTRY_AGE;
|
|
|
|
else
|
|
|
|
return ttl;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
2006-07-11 22:51:58 +02:00
|
|
|
r->magic = 0xFF00FF00;
|
2005-02-11 02:26:47 +01:00
|
|
|
tor_free(r);
|
|
|
|
}
|
|
|
|
|
2006-07-31 20:01:49 +02:00
|
|
|
/** Compare two cached_resolve_t pointers by expiry time, and return
|
|
|
|
* less-than-zero, zero, or greater-than-zero as appropriate. Used for
|
|
|
|
* the priority queue implementation. */
|
2006-07-31 20:00:18 +02:00
|
|
|
static int
|
2006-07-31 20:01:18 +02:00
|
|
|
_compare_cached_resolves_by_expiry(const void *_a, const void *_b)
|
2006-07-31 20:00:18 +02:00
|
|
|
{
|
2006-07-31 20:01:18 +02:00
|
|
|
const cached_resolve_t *a = _a, *b = _b;
|
2006-08-14 09:08:29 +02:00
|
|
|
return a->expire - b->expire;
|
2006-07-31 20:00:18 +02:00
|
|
|
}
|
|
|
|
|
2006-07-31 20:01:49 +02:00
|
|
|
/** Priority queue of cached_resolve_t objects to let us know when they
|
|
|
|
* will expire. */
|
2006-07-31 20:00:18 +02:00
|
|
|
static smartlist_t *cached_resolve_pqueue = NULL;
|
|
|
|
|
2006-07-31 20:01:49 +02:00
|
|
|
/** Set an expiry time for a cached_resolve_t, and add it to the expiry
|
|
|
|
* priority queue */
|
2006-07-31 20:00:18 +02:00
|
|
|
static void
|
|
|
|
set_expiry(cached_resolve_t *resolve, time_t expires)
|
|
|
|
{
|
|
|
|
tor_assert(resolve && resolve->expire == 0);
|
2006-07-31 20:01:45 +02:00
|
|
|
if (!cached_resolve_pqueue)
|
|
|
|
cached_resolve_pqueue = smartlist_create();
|
2006-07-31 20:00:18 +02:00
|
|
|
resolve->expire = expires;
|
|
|
|
smartlist_pqueue_add(cached_resolve_pqueue,
|
|
|
|
_compare_cached_resolves_by_expiry,
|
|
|
|
resolve);
|
|
|
|
}
|
|
|
|
|
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);
|
2006-08-03 06:22:25 +02:00
|
|
|
if (cached_resolve_pqueue)
|
|
|
|
smartlist_free(cached_resolve_pqueue);
|
2006-07-31 20:00:18 +02:00
|
|
|
cached_resolve_pqueue = NULL;
|
2005-02-11 02:26:47 +01: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
|
2006-08-10 11:02:26 +02:00
|
|
|
purge_expired_resolves(time_t now)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2006-07-27 20:35:56 +02:00
|
|
|
cached_resolve_t *resolve, *removed;
|
2005-07-22 23:12:10 +02:00
|
|
|
pending_connection_t *pend;
|
2006-07-26 21:07:26 +02:00
|
|
|
edge_connection_t *pendconn;
|
2003-08-14 05:52:51 +02:00
|
|
|
|
2006-07-27 20:35:56 +02:00
|
|
|
assert_cache_ok();
|
2006-07-31 20:00:18 +02:00
|
|
|
if (!cached_resolve_pqueue)
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (smartlist_len(cached_resolve_pqueue)) {
|
|
|
|
resolve = smartlist_get(cached_resolve_pqueue, 0);
|
|
|
|
if (resolve->expire > now)
|
|
|
|
break;
|
|
|
|
smartlist_pqueue_pop(cached_resolve_pqueue,
|
|
|
|
_compare_cached_resolves_by_expiry);
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (resolve->state == CACHE_STATE_PENDING) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_debug(LD_EXIT,
|
2006-08-10 11:02:12 +02:00
|
|
|
"Expiring a dns resolve %s that's still pending. Forgot to "
|
|
|
|
"cull it? DNS resolve didn't tell us about the timeout?",
|
2006-07-31 20:01:18 +02:00
|
|
|
escaped_safe_str(resolve->address));
|
|
|
|
} else if (resolve->state == CACHE_STATE_CACHED_VALID ||
|
|
|
|
resolve->state == CACHE_STATE_CACHED_FAILED) {
|
|
|
|
log_debug(LD_EXIT,
|
|
|
|
"Forgetting old cached resolve (address %s, expires %lu)",
|
|
|
|
escaped_safe_str(resolve->address),
|
|
|
|
(unsigned long)resolve->expire);
|
|
|
|
tor_assert(!resolve->pending_connections);
|
|
|
|
} else {
|
|
|
|
tor_assert(resolve->state == CACHE_STATE_DONE);
|
|
|
|
tor_assert(!resolve->pending_connections);
|
2004-06-02 00:09:58 +02:00
|
|
|
}
|
2006-07-31 20:00:18 +02:00
|
|
|
|
2004-06-02 00:09:58 +02:00
|
|
|
if (resolve->pending_connections) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_debug(LD_EXIT,
|
2006-07-31 20:01:18 +02:00
|
|
|
"Closing pending connections on timed-out 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. */
|
2006-07-26 21:07:26 +02:00
|
|
|
tor_assert(pend->conn->_base.s == -1);
|
2004-06-02 00:09:58 +02:00
|
|
|
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);
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_free(TO_CONN(pendconn));
|
2004-06-02 00:09:58 +02:00
|
|
|
tor_free(pend);
|
|
|
|
}
|
2004-02-28 23:13:58 +01:00
|
|
|
}
|
2006-07-31 20:00:18 +02:00
|
|
|
|
2006-07-31 20:01:18 +02:00
|
|
|
if (resolve->state == CACHE_STATE_CACHED_VALID ||
|
|
|
|
resolve->state == CACHE_STATE_CACHED_FAILED ||
|
|
|
|
resolve->state == CACHE_STATE_PENDING) {
|
|
|
|
removed = HT_REMOVE(cache_map, &cache_root, resolve);
|
|
|
|
if (removed != resolve) {
|
|
|
|
log_err(LD_BUG, "The expired resolve we purged didn't match any in"
|
|
|
|
" the cache. Tried to purge %s (%p); instead got %s (%p).",
|
2006-08-11 09:09:45 +02:00
|
|
|
resolve->address, (void*)resolve,
|
|
|
|
removed ? removed->address : "NULL", (void*)remove);
|
2006-07-31 20:01:18 +02:00
|
|
|
}
|
|
|
|
tor_assert(removed == resolve);
|
|
|
|
resolve->magic = 0xF0BBF0BB;
|
|
|
|
tor_free(resolve);
|
|
|
|
} else {
|
2006-07-31 20:01:49 +02:00
|
|
|
/* This should be in state DONE. Make sure it's not in the cache. */
|
2006-07-31 20:01:18 +02:00
|
|
|
cached_resolve_t *tmp = HT_FIND(cache_map, &cache_root, resolve);
|
|
|
|
tor_assert(tmp != resolve);
|
2006-07-30 05:34:44 +02:00
|
|
|
}
|
2003-08-14 05:52:51 +02:00
|
|
|
}
|
2006-07-31 20:00:18 +02:00
|
|
|
|
2006-07-27 20:35:56 +02:00
|
|
|
assert_cache_ok();
|
2003-08-14 05:52:51 +02:00
|
|
|
}
|
|
|
|
|
2006-07-27 21:35:11 +02:00
|
|
|
/** Send a response to the RESOLVE request of a connection. answer_type must
|
2005-06-11 20:52:12 +02:00
|
|
|
* be one of RESOLVED_TYPE_(IPV4|ERROR|ERROR_TRANSIENT) */
|
|
|
|
static void
|
2006-07-26 21:07:26 +02:00
|
|
|
send_resolved_cell(edge_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;
|
2006-06-03 23:41:14 +02:00
|
|
|
uint32_t ttl;
|
2004-06-17 20:13:09 +02:00
|
|
|
|
|
|
|
buf[0] = answer_type;
|
2006-06-03 23:41:14 +02:00
|
|
|
ttl = dns_clip_ttl(conn->address_ttl);
|
2004-06-17 20:13:09 +02:00
|
|
|
|
|
|
|
switch (answer_type)
|
|
|
|
{
|
|
|
|
case RESOLVED_TYPE_IPV4:
|
|
|
|
buf[1] = 4;
|
2006-07-26 21:07:26 +02:00
|
|
|
set_uint32(buf+2, htonl(conn->_base.addr));
|
2006-06-03 22:52:24 +02:00
|
|
|
set_uint32(buf+6, htonl(ttl));
|
2005-09-02 20:53:31 +02:00
|
|
|
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);
|
2006-06-03 22:52:24 +02:00
|
|
|
|
2005-09-02 20:53:31 +02:00
|
|
|
buf[1] = msglen;
|
|
|
|
strlcpy(buf+2, errmsg, sizeof(buf)-2);
|
2006-06-03 22:52:24 +02:00
|
|
|
set_uint32(buf+2+msglen, htonl(ttl));
|
2005-09-02 20:53:31 +02:00
|
|
|
buflen = 6+msglen;
|
|
|
|
break;
|
|
|
|
}
|
2004-06-17 20:13:09 +02:00
|
|
|
default:
|
|
|
|
tor_assert(0);
|
2006-08-11 09:09:45 +02:00
|
|
|
return;
|
2004-06-17 20:13:09 +02:00
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
|
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
|
2006-07-26 21:07:26 +02:00
|
|
|
dns_resolve(edge_connection_t *exitconn)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
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;
|
2006-08-10 11:02:26 +02:00
|
|
|
time_t now = time(NULL);
|
2006-07-26 21:07:26 +02:00
|
|
|
assert_connection_ok(TO_CONN(exitconn), 0);
|
|
|
|
tor_assert(exitconn->_base.s == -1);
|
2003-02-14 08:53:55 +01:00
|
|
|
|
2006-07-11 22:51:58 +02:00
|
|
|
assert_cache_ok();
|
|
|
|
|
2006-07-26 21:07:26 +02:00
|
|
|
/* first check if exitconn->_base.address is an IP. If so, we already
|
2004-04-09 23:31:09 +02:00
|
|
|
* know the answer. */
|
2006-07-26 21:07:26 +02:00
|
|
|
if (tor_inet_aton(exitconn->_base.address, &in) != 0) {
|
|
|
|
exitconn->_base.addr = ntohl(in.s_addr);
|
2006-06-03 23:41:14 +02:00
|
|
|
exitconn->address_ttl = DEFAULT_DNS_TTL;
|
2006-07-26 21:07:26 +02:00
|
|
|
if (exitconn->_base.purpose == EXIT_PURPOSE_RESOLVE)
|
2006-06-03 23:41:14 +02:00
|
|
|
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
|
|
|
|
2006-07-26 21:07:26 +02:00
|
|
|
/* lower-case exitconn->_base.address, so it's in canonical form */
|
|
|
|
tor_strlower(exitconn->_base.address);
|
2005-04-06 22:25:21 +02:00
|
|
|
|
2005-12-03 03:21:31 +01:00
|
|
|
/* now check the hash table to see if 'address' is already there. */
|
2006-07-26 21:07:26 +02:00
|
|
|
strlcpy(search.address, exitconn->_base.address, sizeof(search.address));
|
2005-12-03 03:01:18 +01:00
|
|
|
resolve = HT_FIND(cache_map, &cache_root, &search);
|
2006-06-03 22:52:24 +02:00
|
|
|
if (resolve && resolve->expire > now) { /* already there */
|
2004-11-28 10:05:49 +01:00
|
|
|
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;
|
2006-02-13 10:37:53 +01:00
|
|
|
log_debug(LD_EXIT,"Connection (fd %d) waiting for pending DNS "
|
2006-07-26 21:07:37 +02:00
|
|
|
"resolve of %s", exitconn->_base.s,
|
|
|
|
escaped_safe_str(exitconn->_base.address));
|
2006-07-26 21:07:26 +02:00
|
|
|
exitconn->_base.state = EXIT_CONN_STATE_RESOLVING;
|
2003-02-18 02:35:55 +01:00
|
|
|
return 0;
|
2006-07-31 20:01:18 +02:00
|
|
|
case CACHE_STATE_CACHED_VALID:
|
2006-07-26 21:07:26 +02:00
|
|
|
exitconn->_base.addr = resolve->addr;
|
2006-06-03 23:41:14 +02:00
|
|
|
exitconn->address_ttl = resolve->ttl;
|
2006-03-19 04:55:48 +01:00
|
|
|
log_debug(LD_EXIT,"Connection (fd %d) found cached answer for %s",
|
2006-07-26 21:07:37 +02:00
|
|
|
exitconn->_base.s,
|
|
|
|
escaped_safe_str(exitconn->_base.address));
|
2006-07-26 21:07:26 +02:00
|
|
|
if (exitconn->_base.purpose == EXIT_PURPOSE_RESOLVE)
|
2006-06-03 23:41:14 +02:00
|
|
|
send_resolved_cell(exitconn, RESOLVED_TYPE_IPV4);
|
2003-08-14 05:52:51 +02:00
|
|
|
return 1;
|
2006-07-31 20:01:18 +02:00
|
|
|
case CACHE_STATE_CACHED_FAILED:
|
2006-03-19 04:55:48 +01:00
|
|
|
log_debug(LD_EXIT,"Connection (fd %d) found cached error for %s",
|
2006-07-26 21:07:37 +02:00
|
|
|
exitconn->_base.s,
|
|
|
|
escaped_safe_str(exitconn->_base.address));
|
2006-07-26 21:07:26 +02:00
|
|
|
if (exitconn->_base.purpose == EXIT_PURPOSE_RESOLVE)
|
2006-06-03 23:41:14 +02:00
|
|
|
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);
|
2006-07-26 21:07:26 +02:00
|
|
|
if (!exitconn->_base.marked_for_close)
|
|
|
|
connection_free(TO_CONN(exitconn));
|
2003-02-14 08:53:55 +01:00
|
|
|
return -1;
|
2006-07-31 20:01:18 +02:00
|
|
|
case CACHE_STATE_DONE:
|
|
|
|
log_err(LD_BUG, "Found a 'DONE' dns resolve still in the cache.");
|
|
|
|
tor_fragile_assert();
|
2003-02-14 08:53:55 +01:00
|
|
|
}
|
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));
|
2006-07-11 22:51:58 +02:00
|
|
|
resolve->magic = CACHED_RESOLVE_MAGIC;
|
2003-12-14 01:12:02 +01:00
|
|
|
resolve->state = CACHE_STATE_PENDING;
|
2006-07-26 21:07:26 +02:00
|
|
|
strlcpy(resolve->address, exitconn->_base.address, sizeof(resolve->address));
|
2003-12-14 01:12:02 +01:00
|
|
|
|
2006-07-31 20:01:18 +02:00
|
|
|
/* add this connection 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;
|
2006-07-26 21:07:26 +02:00
|
|
|
exitconn->_base.state = EXIT_CONN_STATE_RESOLVING;
|
2003-12-14 01:12:02 +01:00
|
|
|
|
2006-07-31 20:01:18 +02:00
|
|
|
/* Add this resolve to the cache and priority queue. */
|
2006-07-31 20:00:18 +02:00
|
|
|
HT_INSERT(cache_map, &cache_root, resolve);
|
2006-07-31 20:01:18 +02:00
|
|
|
set_expiry(resolve, now + RESOLVE_MAX_TIMEOUT);
|
2006-07-31 20:00:18 +02:00
|
|
|
|
2006-07-31 20:01:49 +02:00
|
|
|
log_debug(LD_EXIT,"Launching %s.",
|
2006-07-26 21:07:26 +02:00
|
|
|
escaped_safe_str(exitconn->_base.address));
|
2006-07-11 22:51:58 +02:00
|
|
|
assert_cache_ok();
|
2006-07-31 20:01:49 +02:00
|
|
|
return launch_resolve(exitconn);
|
2003-02-14 08:53:55 +01:00
|
|
|
}
|
|
|
|
|
2006-06-03 22:52:24 +02:00
|
|
|
/** Log an error and abort if conn is waiting for a DNS resolve.
|
2004-05-05 23:32:43 +02:00
|
|
|
*/
|
2006-06-03 22:52:24 +02:00
|
|
|
void
|
2006-07-26 21:07:26 +02:00
|
|
|
assert_connection_edge_not_dns_pending(edge_connection_t *conn)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2006-06-03 22:52:24 +02:00
|
|
|
pending_connection_t *pend;
|
|
|
|
cached_resolve_t **resolve;
|
2004-04-09 23:31:09 +02:00
|
|
|
|
2006-06-03 22:52:24 +02:00
|
|
|
HT_FOREACH(resolve, cache_map, &cache_root) {
|
|
|
|
for (pend = (*resolve)->pending_connections;
|
|
|
|
pend;
|
|
|
|
pend = pend->next) {
|
|
|
|
tor_assert(pend->conn != conn);
|
|
|
|
}
|
2005-09-23 01:43:41 +02:00
|
|
|
}
|
2006-06-03 22:52:24 +02:00
|
|
|
}
|
2003-02-14 08:53:55 +01:00
|
|
|
|
2006-06-03 22:52:24 +02:00
|
|
|
/** Log an error and abort if any connection waiting for a DNS resolve is
|
|
|
|
* corrupted. */
|
|
|
|
void
|
|
|
|
assert_all_pending_dns_resolves_ok(void)
|
|
|
|
{
|
|
|
|
pending_connection_t *pend;
|
|
|
|
cached_resolve_t **resolve;
|
2003-02-14 08:53:55 +01:00
|
|
|
|
2006-06-03 22:52:24 +02:00
|
|
|
HT_FOREACH(resolve, cache_map, &cache_root) {
|
|
|
|
for (pend = (*resolve)->pending_connections;
|
|
|
|
pend;
|
|
|
|
pend = pend->next) {
|
2006-07-26 21:07:26 +02:00
|
|
|
assert_connection_ok(TO_CONN(pend->conn), 0);
|
|
|
|
tor_assert(pend->conn->_base.s == -1);
|
|
|
|
tor_assert(!connection_in_array(TO_CONN(pend->conn)));
|
2006-06-03 22:52:24 +02:00
|
|
|
}
|
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
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_dns_remove(edge_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
|
|
|
|
2006-07-26 21:07:26 +02:00
|
|
|
tor_assert(conn->_base.type == CONN_TYPE_EXIT);
|
|
|
|
tor_assert(conn->_base.state == EXIT_CONN_STATE_RESOLVING);
|
2004-06-02 20:12:49 +02:00
|
|
|
|
2006-07-26 21:07:26 +02:00
|
|
|
strlcpy(search.address, conn->_base.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) {
|
2006-03-19 04:55:48 +01:00
|
|
|
log_notice(LD_BUG, "Address %s is not pending. Dropping.",
|
2006-07-26 21:07:26 +02:00
|
|
|
escaped_safe_str(conn->_base.address));
|
2004-02-28 23:23:44 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(resolve->pending_connections);
|
2006-07-26 21:07:26 +02:00
|
|
|
assert_connection_ok(TO_CONN(conn),0);
|
2004-02-28 23:23:44 +01:00
|
|
|
|
|
|
|
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);
|
2006-02-13 10:37:53 +01:00
|
|
|
log_debug(LD_EXIT, "First connection (fd %d) no longer waiting "
|
2006-03-19 04:55:48 +01:00
|
|
|
"for resolve of %s",
|
2006-07-26 21:07:26 +02:00
|
|
|
conn->_base.s, escaped_safe_str(conn->_base.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);
|
2006-02-13 10:37:53 +01:00
|
|
|
log_debug(LD_EXIT,
|
2006-03-19 04:55:48 +01:00
|
|
|
"Connection (fd %d) no longer waiting for resolve of %s",
|
2006-07-26 21:07:26 +02:00
|
|
|
conn->_base.s, escaped_safe_str(conn->_base.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-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
|
2006-08-04 20:26:13 +02:00
|
|
|
dns_cancel_pending_resolve(char *address)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2005-07-22 23:12:10 +02:00
|
|
|
pending_connection_t *pend;
|
|
|
|
cached_resolve_t search;
|
2006-07-31 20:00:18 +02:00
|
|
|
cached_resolve_t *resolve, *tmp;
|
2006-07-26 21:07:26 +02:00
|
|
|
edge_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);
|
2006-07-31 20:01:18 +02:00
|
|
|
if (!resolve || resolve->state != CACHE_STATE_PENDING) {
|
2006-03-19 04:55:48 +01:00
|
|
|
log_notice(LD_BUG,"Address %s is not pending. Dropping.",
|
|
|
|
escaped_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 */
|
2006-02-13 10:37:53 +01:00
|
|
|
log_warn(LD_BUG,
|
2006-03-19 04:55:48 +01:00
|
|
|
"Bug: Address %s is pending but has no pending connections!",
|
|
|
|
escaped_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);
|
2006-07-31 20:00:18 +02:00
|
|
|
tor_assert(! resolve->expire);
|
2003-06-25 09:19:30 +02:00
|
|
|
|
2004-02-28 23:23:44 +01:00
|
|
|
/* mark all pending connections to fail */
|
2006-02-13 10:37:53 +01:00
|
|
|
log_debug(LD_EXIT,
|
2006-03-19 04:55:48 +01:00
|
|
|
"Failing all connections waiting on DNS resolve of %s",
|
|
|
|
escaped_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;
|
2006-07-26 21:07:26 +02:00
|
|
|
pend->conn->_base.state = EXIT_CONN_STATE_RESOLVEFAILED;
|
2004-06-02 20:32:24 +02:00
|
|
|
pendconn = pend->conn;
|
2006-07-26 21:07:26 +02:00
|
|
|
assert_connection_ok(TO_CONN(pendconn), 0);
|
|
|
|
tor_assert(pendconn->_base.s == -1);
|
|
|
|
if (!pendconn->_base.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);
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_free(TO_CONN(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
|
|
|
}
|
|
|
|
|
2006-07-27 20:35:56 +02:00
|
|
|
tmp = HT_REMOVE(cache_map, &cache_root, resolve);
|
2006-07-31 20:01:18 +02:00
|
|
|
if (tmp != resolve) {
|
|
|
|
log_err(LD_BUG, "The cancelled resolve we purged didn't match any in"
|
|
|
|
" the cache. Tried to purge %s (%p); instead got %s (%p).",
|
2006-08-11 09:09:45 +02:00
|
|
|
resolve->address, (void*)resolve,
|
|
|
|
tmp ? tmp->address : "NULL", (void*)tmp);
|
2006-07-31 20:01:18 +02:00
|
|
|
}
|
2006-07-27 20:35:56 +02:00
|
|
|
tor_assert(tmp == resolve);
|
2006-07-31 20:00:18 +02:00
|
|
|
resolve->magic = 0xABABABAB;
|
2004-03-04 19:43:44 +01:00
|
|
|
tor_free(resolve);
|
2003-06-25 09:19:30 +02:00
|
|
|
}
|
|
|
|
|
2006-07-31 20:01:18 +02:00
|
|
|
static void
|
|
|
|
add_answer_to_cache(const char *address, uint32_t addr, char outcome,
|
|
|
|
uint32_t ttl)
|
|
|
|
{
|
|
|
|
cached_resolve_t *resolve;
|
|
|
|
if (outcome == DNS_RESOLVE_FAILED_TRANSIENT)
|
|
|
|
return;
|
|
|
|
|
|
|
|
resolve = tor_malloc_zero(sizeof(cached_resolve_t));
|
|
|
|
resolve->magic = CACHED_RESOLVE_MAGIC;
|
|
|
|
resolve->state = (outcome == DNS_RESOLVE_SUCCEEDED) ?
|
|
|
|
CACHE_STATE_CACHED_VALID : CACHE_STATE_CACHED_FAILED;
|
|
|
|
strlcpy(resolve->address, address, sizeof(resolve->address));
|
|
|
|
resolve->addr = addr;
|
|
|
|
resolve->ttl = ttl;
|
|
|
|
assert_resolve_ok(resolve);
|
|
|
|
HT_INSERT(cache_map, &cache_root, resolve);
|
|
|
|
set_expiry(resolve, time(NULL) + dns_get_expiry_ttl(ttl));
|
|
|
|
}
|
|
|
|
|
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
|
2006-07-11 22:51:58 +02:00
|
|
|
dns_found_answer(const char *address, uint32_t addr, char outcome,
|
|
|
|
uint32_t ttl)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2005-07-22 23:12:10 +02:00
|
|
|
pending_connection_t *pend;
|
|
|
|
cached_resolve_t search;
|
2006-07-31 20:01:18 +02:00
|
|
|
cached_resolve_t *resolve, *removed;
|
2006-07-26 21:07:26 +02:00
|
|
|
edge_connection_t *pendconn;
|
2004-05-06 13:08:04 +02:00
|
|
|
circuit_t *circ;
|
2003-02-14 08:53:55 +01:00
|
|
|
|
2006-07-11 22:51:58 +02:00
|
|
|
assert_cache_ok();
|
|
|
|
|
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) {
|
2006-03-19 04:55:48 +01:00
|
|
|
log_info(LD_EXIT,"Resolved unasked address %s; caching anyway.",
|
|
|
|
escaped_safe_str(address));
|
2006-07-31 20:01:18 +02:00
|
|
|
add_answer_to_cache(address, addr, outcome, ttl);
|
2003-06-25 09:19:30 +02:00
|
|
|
return;
|
2003-02-14 08:53:55 +01:00
|
|
|
}
|
2006-07-27 19:16:10 +02:00
|
|
|
assert_resolve_ok(resolve);
|
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? */
|
2006-03-19 04:55:48 +01:00
|
|
|
log_notice(LD_EXIT, "Resolved %s which was already resolved; ignoring",
|
|
|
|
escaped_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-28 10:05:49 +01:00
|
|
|
while (resolve->pending_connections) {
|
2003-02-14 08:53:55 +01:00
|
|
|
pend = resolve->pending_connections;
|
2004-06-17 20:13:09 +02:00
|
|
|
pendconn = pend->conn; /* don't pass complex things to the
|
|
|
|
connection_mark_for_close macro */
|
2006-07-27 19:16:10 +02:00
|
|
|
assert_connection_ok(TO_CONN(pendconn),time(NULL));
|
2006-07-31 20:01:18 +02:00
|
|
|
pendconn->_base.addr = addr;
|
|
|
|
pendconn->address_ttl = ttl;
|
2004-04-09 23:31:09 +02:00
|
|
|
|
2006-07-31 20:01:18 +02:00
|
|
|
if (outcome != DNS_RESOLVE_SUCCEEDED) {
|
2004-04-11 19:07:45 +02:00
|
|
|
/* prevent double-remove. */
|
2006-07-26 21:07:26 +02:00
|
|
|
pendconn->_base.state = EXIT_CONN_STATE_RESOLVEFAILED;
|
|
|
|
if (pendconn->_base.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 {
|
2006-06-03 23:41:14 +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
|
|
|
}
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_free(TO_CONN(pendconn));
|
2004-02-22 21:50:20 +01:00
|
|
|
} else {
|
2006-07-26 21:07:26 +02:00
|
|
|
if (pendconn->_base.purpose == EXIT_PURPOSE_CONNECT) {
|
2004-06-17 20:13:09 +02:00
|
|
|
/* prevent double-remove. */
|
2006-07-26 21:07:26 +02:00
|
|
|
pend->conn->_base.state = EXIT_CONN_STATE_CONNECTING;
|
2004-06-17 20:13:09 +02:00
|
|
|
|
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);
|
2006-07-23 09:37:35 +02:00
|
|
|
tor_assert(!CIRCUIT_IS_ORIGIN(circ));
|
2004-06-17 20:13:09 +02:00
|
|
|
/* unlink pend->conn from resolving_streams, */
|
|
|
|
circuit_detach_stream(circ, pend->conn);
|
|
|
|
/* and link it to n_streams */
|
2006-07-23 09:37:35 +02:00
|
|
|
pend->conn->next_stream = TO_OR_CIRCUIT(circ)->n_streams;
|
2005-04-06 08:13:49 +02:00
|
|
|
pend->conn->on_circuit = circ;
|
2006-07-23 09:37:35 +02:00
|
|
|
TO_OR_CIRCUIT(circ)->n_streams = pend->conn;
|
2004-06-17 20:13:09 +02:00
|
|
|
|
|
|
|
connection_exit_connect(pend->conn);
|
|
|
|
} else {
|
|
|
|
/* prevent double-remove. This isn't really an accurate state,
|
|
|
|
* but it does the right thing. */
|
2006-07-26 21:07:26 +02:00
|
|
|
pendconn->_base.state = EXIT_CONN_STATE_RESOLVEFAILED;
|
2006-06-03 23:41:14 +02:00
|
|
|
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);
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_free(TO_CONN(pendconn));
|
2004-06-17 20:13:09 +02:00
|
|
|
}
|
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
|
|
|
|
2006-07-31 20:01:18 +02:00
|
|
|
resolve->state = CACHE_STATE_DONE;
|
|
|
|
removed = HT_REMOVE(cache_map, &cache_root, &search);
|
|
|
|
if (removed != resolve) {
|
|
|
|
log_err(LD_BUG, "The pending resolve we found wasn't removable from"
|
|
|
|
" the cache. Tried to purge %s (%p); instead got %s (%p).",
|
2006-08-11 09:09:45 +02:00
|
|
|
resolve->address, (void*)resolve,
|
|
|
|
removed ? removed->address : "NULL", (void*)removed);
|
2004-03-12 19:45:42 +01:00
|
|
|
}
|
2006-07-31 20:01:18 +02:00
|
|
|
assert_resolve_ok(resolve);
|
|
|
|
assert_cache_ok();
|
2006-07-11 22:51:58 +02:00
|
|
|
|
2006-07-31 20:01:18 +02:00
|
|
|
add_answer_to_cache(address, addr, outcome, ttl);
|
2006-07-11 22:51:58 +02:00
|
|
|
assert_cache_ok();
|
2003-02-14 08:53:55 +01:00
|
|
|
}
|
|
|
|
|
2006-06-03 22:52:24 +02:00
|
|
|
#ifndef USE_EVENTDNS
|
|
|
|
/** Find or spawn a dns worker process to handle resolving
|
|
|
|
* <b>exitconn</b>-\>address; tell that dns worker to begin resolving.
|
|
|
|
*/
|
|
|
|
static int
|
2006-07-31 22:25:57 +02:00
|
|
|
launch_resolve(edge_connection_t *exitconn)
|
2006-06-03 22:52:24 +02:00
|
|
|
{
|
|
|
|
connection_t *dnsconn;
|
|
|
|
unsigned char len;
|
|
|
|
|
2006-07-26 21:07:26 +02:00
|
|
|
tor_assert(exitconn->_base.state == EXIT_CONN_STATE_RESOLVING);
|
|
|
|
assert_connection_ok(TO_CONN(exitconn), 0);
|
|
|
|
tor_assert(exitconn->_base.s == -1);
|
2006-06-03 22:52:24 +02:00
|
|
|
|
|
|
|
/* respawn here, to be sure there are enough */
|
|
|
|
if (spawn_enough_dnsworkers() < 0) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
dnsconn = connection_get_by_type_state(CONN_TYPE_DNSWORKER,
|
|
|
|
DNSWORKER_STATE_IDLE);
|
|
|
|
|
|
|
|
if (!dnsconn) {
|
|
|
|
log_warn(LD_EXIT,"no idle dns workers. Failing.");
|
2006-07-26 21:07:26 +02:00
|
|
|
if (exitconn->_base.purpose == EXIT_PURPOSE_RESOLVE)
|
2006-06-03 23:41:14 +02:00
|
|
|
send_resolved_cell(exitconn, RESOLVED_TYPE_ERROR_TRANSIENT);
|
2006-06-03 22:52:24 +02:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
log_debug(LD_EXIT,
|
|
|
|
"Connection (fd %d) needs to resolve %s; assigning "
|
2006-07-26 21:07:26 +02:00
|
|
|
"to DNSWorker (fd %d)", exitconn->_base.s,
|
|
|
|
escaped_safe_str(exitconn->_base.address), dnsconn->s);
|
2006-06-03 22:52:24 +02:00
|
|
|
|
|
|
|
tor_free(dnsconn->address);
|
2006-07-26 21:07:26 +02:00
|
|
|
dnsconn->address = tor_strdup(exitconn->_base.address);
|
2006-06-03 22:52:24 +02:00
|
|
|
dnsconn->state = DNSWORKER_STATE_BUSY;
|
2006-06-12 13:59:19 +02:00
|
|
|
/* touch the lastwritten timestamp, since that's how we check to
|
|
|
|
* see how long it's been since we asked the question, and sometimes
|
|
|
|
* we check before the first call to connection_handle_write(). */
|
|
|
|
dnsconn->timestamp_lastwritten = time(NULL);
|
2006-06-03 22:52:24 +02:00
|
|
|
num_dnsworkers_busy++;
|
|
|
|
|
|
|
|
len = strlen(dnsconn->address);
|
|
|
|
connection_write_to_buf((char*)&len, 1, dnsconn);
|
|
|
|
connection_write_to_buf(dnsconn->address, len, dnsconn);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
err:
|
2006-07-26 21:07:37 +02:00
|
|
|
/* also sends end and frees */
|
|
|
|
dns_cancel_pending_resolve(exitconn->_base.address);
|
2006-06-03 22:52:24 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2006-02-13 10:37:53 +01:00
|
|
|
log_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;
|
2006-06-03 22:52:24 +02:00
|
|
|
int ttl;
|
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)) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_warn(LD_BUG,
|
|
|
|
"Bug: read data (%d bytes) from an idle dns worker (fd %d, "
|
2006-03-19 04:55:48 +01:00
|
|
|
"address %s). Please report.", (int)buf_datalen(conn->inbuf),
|
|
|
|
conn->s, escaped_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);
|
2006-02-13 10:37:53 +01:00
|
|
|
log_warn(LD_EXIT,"Discarding idle dns answer (success %d, addr %d.)",
|
2006-04-18 05:07:24 +02:00
|
|
|
success, addr);
|
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
|
|
|
|
2006-03-19 04:55:48 +01:00
|
|
|
log_debug(LD_EXIT, "DNSWorker (fd %d) returned answer for %s",
|
|
|
|
conn->s, escaped_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);
|
2006-06-03 22:52:24 +02:00
|
|
|
|
|
|
|
ttl = (success == DNS_RESOLVE_FAILED_TRANSIENT) ? 0 : MAX_DNS_ENTRY_AGE;
|
|
|
|
dns_found_answer(conn->address, ntohl(addr), success, ttl);
|
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;
|
2006-03-19 04:55:48 +01:00
|
|
|
char *log_address;
|
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) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_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 {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_info(LD_EXIT,"DNS worker exiting because of error on connection "
|
|
|
|
"to Tor process.");
|
|
|
|
log_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) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_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
|
|
|
}
|
2006-07-15 21:21:30 +02:00
|
|
|
address[address_len] = 0; /* nul terminate it */
|
2003-06-17 16:31:05 +02:00
|
|
|
|
2006-03-19 04:55:48 +01:00
|
|
|
log_address = esc_for_log(safe_str(address));
|
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 */
|
2006-02-13 10:37:53 +01:00
|
|
|
log_info(LD_NET,"Could not resolve dest addr %s (transient).",
|
2006-03-19 04:55:48 +01:00
|
|
|
log_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:
|
2006-02-13 10:37:53 +01:00
|
|
|
log_info(LD_NET,"Could not resolve dest addr %s (permanent).",
|
2006-03-19 04:55:48 +01:00
|
|
|
log_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:
|
2006-03-19 04:55:48 +01:00
|
|
|
log_info(LD_NET,"Resolved address %s.", log_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
|
|
|
}
|
2006-03-19 04:55:48 +01:00
|
|
|
tor_free(log_address);
|
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) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_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) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_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
|
|
|
}
|
|
|
|
|
2006-06-06 02:06:52 +02:00
|
|
|
tor_assert(fdarray[0] >= 0);
|
|
|
|
tor_assert(fdarray[1] >= 0);
|
|
|
|
|
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);
|
2006-02-13 10:37:53 +01:00
|
|
|
log_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 */
|
2006-02-13 10:37:53 +01:00
|
|
|
log_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
|
|
|
|
2006-02-13 10:37:53 +01:00
|
|
|
log_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) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_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 */
|
2006-02-13 10:37:53 +01:00
|
|
|
log_info(LD_EXIT,"%d of %d dnsworkers are idle. Killing one.",
|
|
|
|
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
|
|
|
}
|
2006-06-03 22:52:24 +02:00
|
|
|
#else /* !USE_EVENTDNS */
|
|
|
|
static int
|
|
|
|
eventdns_err_is_transient(int err)
|
|
|
|
{
|
|
|
|
switch (err)
|
|
|
|
{
|
|
|
|
case DNS_ERR_SERVERFAILED:
|
|
|
|
case DNS_ERR_TRUNCATED:
|
|
|
|
case DNS_ERR_TIMEOUT:
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void
|
|
|
|
dnsworkers_rotate(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
int
|
|
|
|
connection_dns_finished_flushing(connection_t *conn)
|
|
|
|
{
|
2006-06-05 00:42:13 +02:00
|
|
|
(void)conn;
|
2006-06-03 22:52:24 +02:00
|
|
|
tor_assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int
|
|
|
|
connection_dns_process_inbuf(connection_t *conn)
|
|
|
|
{
|
2006-06-05 00:42:13 +02:00
|
|
|
(void)conn;
|
2006-06-03 22:52:24 +02:00
|
|
|
tor_assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int
|
|
|
|
connection_dns_reached_eof(connection_t *conn)
|
|
|
|
{
|
2006-06-05 00:42:13 +02:00
|
|
|
(void)conn;
|
2006-06-03 22:52:24 +02:00
|
|
|
tor_assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
2006-08-04 20:24:41 +02:00
|
|
|
static int nameservers_configured = 0;
|
|
|
|
static void
|
|
|
|
configure_nameservers(void)
|
|
|
|
{
|
|
|
|
or_options_t *options;
|
|
|
|
if (nameservers_configured)
|
|
|
|
return;
|
|
|
|
options = get_options();
|
|
|
|
eventdns_set_log_fn(eventdns_log_cb);
|
|
|
|
if (options->Nameservers && smartlist_len(options->Nameservers)) {
|
|
|
|
log_info(LD_EXIT, "Configuring nameservers from Tor configuration");
|
|
|
|
SMARTLIST_FOREACH(options->Nameservers, const char *, ip,
|
|
|
|
{
|
|
|
|
struct in_addr in;
|
|
|
|
if (tor_inet_aton(ip, &in)) {
|
|
|
|
log_info(LD_EXIT, "Adding nameserver '%s'", ip);
|
|
|
|
eventdns_nameserver_add(in.s_addr);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
eventdns_config_windows_nameservers();
|
|
|
|
#else
|
|
|
|
log_info(LD_EXIT, "Parsing /etc/resolv.conf");
|
|
|
|
eventdns_resolv_conf_parse(DNS_OPTION_NAMESERVERS|DNS_OPTION_MISC,
|
|
|
|
"/etc/resolv.conf");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
nameservers_configured = 1;
|
|
|
|
}
|
2006-06-03 22:52:24 +02:00
|
|
|
static void
|
|
|
|
eventdns_callback(int result, char type, int count, int ttl, void *addresses,
|
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
char *string_address = arg;
|
2006-06-03 23:41:14 +02:00
|
|
|
int status = DNS_RESOLVE_FAILED_PERMANENT;
|
|
|
|
uint32_t addr = 0;
|
2006-06-03 22:52:24 +02:00
|
|
|
if (result == DNS_ERR_NONE) {
|
|
|
|
if (type == DNS_IPv4_A && count) {
|
2006-06-06 01:01:22 +02:00
|
|
|
char answer_buf[INET_NTOA_BUF_LEN+1];
|
|
|
|
struct in_addr in;
|
2006-06-03 22:52:24 +02:00
|
|
|
uint32_t *addrs = addresses;
|
2006-06-06 01:01:22 +02:00
|
|
|
in.s_addr = addrs[0];
|
2006-06-05 02:32:31 +02:00
|
|
|
addr = ntohl(addrs[0]);
|
2006-06-03 23:41:14 +02:00
|
|
|
status = DNS_RESOLVE_SUCCEEDED;
|
2006-06-06 01:01:22 +02:00
|
|
|
tor_inet_ntoa(&in, answer_buf, sizeof(answer_buf));
|
|
|
|
log_debug(LD_EXIT, "eventdns said that %s resolves to %s",
|
2006-06-16 00:59:07 +02:00
|
|
|
escaped_safe_str(string_address),
|
|
|
|
escaped_safe_str(answer_buf));
|
2006-06-03 22:52:24 +02:00
|
|
|
} else if (count) {
|
|
|
|
log_warn(LD_EXIT, "eventdns returned only non-IPv4 answers for %s.",
|
|
|
|
escaped_safe_str(string_address));
|
|
|
|
} else {
|
|
|
|
log_warn(LD_BUG, "eventdns returned no addresses or error for %s!",
|
|
|
|
escaped_safe_str(string_address));
|
|
|
|
}
|
|
|
|
} else {
|
2006-06-03 23:41:14 +02:00
|
|
|
if (eventdns_err_is_transient(result))
|
|
|
|
status = DNS_RESOLVE_FAILED_TRANSIENT;
|
2006-06-03 22:52:24 +02:00
|
|
|
}
|
2006-06-03 23:41:14 +02:00
|
|
|
dns_found_answer(string_address, addr, status, ttl);
|
2006-06-03 22:52:24 +02:00
|
|
|
tor_free(string_address);
|
|
|
|
}
|
2005-06-09 21:03:31 +02:00
|
|
|
|
2006-06-03 23:41:14 +02:00
|
|
|
static int
|
2006-07-31 20:01:49 +02:00
|
|
|
launch_resolve(edge_connection_t *exitconn)
|
2006-06-03 22:52:24 +02:00
|
|
|
{
|
2006-07-26 21:07:26 +02:00
|
|
|
char *addr = tor_strdup(exitconn->_base.address);
|
2006-06-06 01:01:22 +02:00
|
|
|
int r;
|
2006-08-04 20:24:41 +02:00
|
|
|
if (!nameservers_configured)
|
|
|
|
configure_nameservers();
|
2006-06-06 01:01:22 +02:00
|
|
|
log_info(LD_EXIT, "Launching eventdns request for %s",
|
2006-07-26 21:07:26 +02:00
|
|
|
escaped_safe_str(exitconn->_base.address));
|
|
|
|
r = eventdns_resolve(exitconn->_base.address, DNS_QUERY_NO_SEARCH,
|
2006-06-06 01:01:22 +02:00
|
|
|
eventdns_callback, addr);
|
2006-06-03 22:52:24 +02:00
|
|
|
if (r) {
|
|
|
|
log_warn(LD_EXIT, "eventdns rejected address %s: error %d.",
|
|
|
|
escaped_safe_str(addr), r);
|
2006-07-26 21:29:30 +02:00
|
|
|
if (exitconn->_base.purpose == EXIT_PURPOSE_RESOLVE) {
|
2006-06-03 22:52:24 +02:00
|
|
|
if (eventdns_err_is_transient(r))
|
2006-06-03 23:41:14 +02:00
|
|
|
send_resolved_cell(exitconn, RESOLVED_TYPE_ERROR_TRANSIENT);
|
|
|
|
else {
|
|
|
|
exitconn->address_ttl = DEFAULT_DNS_TTL;
|
|
|
|
send_resolved_cell(exitconn, RESOLVED_TYPE_ERROR);
|
|
|
|
}
|
2006-06-03 22:52:24 +02:00
|
|
|
}
|
|
|
|
dns_cancel_pending_resolve(addr);/* also sends end and frees */
|
|
|
|
tor_free(addr);
|
|
|
|
}
|
|
|
|
return r ? -1 : 0;
|
|
|
|
}
|
|
|
|
#endif /* USE_EVENTDNS */
|
2006-06-03 23:41:14 +02:00
|
|
|
|
2006-07-11 22:51:58 +02:00
|
|
|
static void
|
|
|
|
assert_resolve_ok(cached_resolve_t *resolve)
|
|
|
|
{
|
|
|
|
tor_assert(resolve);
|
|
|
|
tor_assert(resolve->magic == CACHED_RESOLVE_MAGIC);
|
|
|
|
tor_assert(strlen(resolve->address) < MAX_ADDRESSLEN);
|
2006-07-31 20:01:45 +02:00
|
|
|
tor_assert(tor_strisnonupper(resolve->address));
|
2006-07-31 20:01:18 +02:00
|
|
|
if (resolve->state != CACHE_STATE_PENDING) {
|
|
|
|
tor_assert(!resolve->pending_connections);
|
|
|
|
}
|
2006-07-31 20:01:45 +02:00
|
|
|
if (resolve->state == CACHE_STATE_PENDING ||
|
|
|
|
resolve->state == CACHE_STATE_DONE) {
|
|
|
|
tor_assert(!resolve->ttl);
|
|
|
|
tor_assert(!resolve->addr);
|
|
|
|
}
|
2006-07-11 22:51:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-08-14 22:16:21 +02:00
|
|
|
_assert_cache_ok(void)
|
2006-07-11 22:51:58 +02:00
|
|
|
{
|
|
|
|
cached_resolve_t **resolve;
|
2006-07-27 19:37:37 +02:00
|
|
|
int bad_rep = _cache_map_HT_REP_IS_BAD(&cache_root);
|
|
|
|
if (bad_rep) {
|
|
|
|
log_err(LD_BUG, "Bad rep type %d on dns cache hash table", bad_rep);
|
|
|
|
tor_assert(!bad_rep);
|
|
|
|
}
|
2006-07-11 22:51:58 +02:00
|
|
|
|
|
|
|
HT_FOREACH(resolve, cache_map, &cache_root) {
|
|
|
|
assert_resolve_ok(*resolve);
|
2006-07-31 20:01:18 +02:00
|
|
|
tor_assert((*resolve)->state != CACHE_STATE_DONE);
|
2006-07-11 22:51:58 +02:00
|
|
|
}
|
2006-07-31 20:01:45 +02:00
|
|
|
if (!cached_resolve_pqueue)
|
|
|
|
return;
|
|
|
|
|
|
|
|
smartlist_pqueue_assert_ok(cached_resolve_pqueue,
|
|
|
|
_compare_cached_resolves_by_expiry);
|
2006-07-11 22:51:58 +02:00
|
|
|
}
|
|
|
|
|