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-08-28 05:16:02 +02:00
|
|
|
#else
|
|
|
|
/** Have we currently configured nameservers with eventdns? */
|
|
|
|
static int nameservers_configured = 0;
|
|
|
|
/** What was the resolv_conf fname we last used when configuring the
|
|
|
|
* nameservers? Used to check whether we need to reconfigure. */
|
|
|
|
static char *resolv_conf_fname = NULL;
|
|
|
|
/** What was the mtime on the resolv.conf file we last used when configuring
|
|
|
|
* the nameservers? Used to check whether we need to reconfigure. */
|
|
|
|
static time_t resolv_conf_mtime = 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. */
|
2006-09-21 23:48:06 +02:00
|
|
|
union {
|
|
|
|
uint32_t addr; /**< IPv4 addr for <b>address</b>. */
|
|
|
|
char *hostname; /**< Hostname for <b>address</b> (if a reverse lookup) */
|
|
|
|
} result;
|
2006-08-03 06:22:25 +02:00
|
|
|
uint8_t state; /**< Is this cached entry pending/done/valid/failed? */
|
2006-09-21 23:48:06 +02:00
|
|
|
uint8_t is_reverse; /**< Is this a reverse (addr-to-hostname) lookup? */
|
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? */
|
2006-08-28 05:16:02 +02:00
|
|
|
/** Connections that want to know when we get an answer for this resolve. */
|
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-09-21 23:48:06 +02:00
|
|
|
static void dns_found_answer(const char *address, int is_reverse,
|
|
|
|
uint32_t addr, const char *hostname, 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
|
2006-08-28 05:15:55 +02:00
|
|
|
static void dnsworkers_rotate(void);
|
2006-09-06 10:42:20 +02:00
|
|
|
static void 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
|
2006-08-28 05:15:55 +02:00
|
|
|
static int configure_nameservers(int force);
|
2006-06-03 22:52:24 +02:00
|
|
|
#endif
|
2006-08-14 22:16:21 +02:00
|
|
|
#ifdef DEBUG_DNS_CACHE
|
2006-08-15 06:50:17 +02:00
|
|
|
static void _assert_cache_ok(void);
|
2006-08-14 22:16:21 +02:00
|
|
|
#define assert_cache_ok() _assert_cache_ok()
|
|
|
|
#else
|
2006-08-28 05:15:55 +02:00
|
|
|
#define assert_cache_ok() do {} while (0)
|
2006-08-14 22:16:21 +02:00
|
|
|
#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);
|
|
|
|
}
|
|
|
|
|
2006-08-28 05:16:02 +02:00
|
|
|
/** Hash function for cached_resolve objects */
|
2005-11-23 05:18:45 +01:00
|
|
|
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
|
2006-08-28 05:16:02 +02:00
|
|
|
/** Helper: called by eventdns when eventdns wants to log something. */
|
2006-07-05 23:28:37 +02:00
|
|
|
static void
|
2006-08-28 05:15:38 +02:00
|
|
|
eventdns_log_cb(int warn, const char *msg)
|
2006-07-05 23:28:37 +02:00
|
|
|
{
|
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-08-28 05:16:02 +02:00
|
|
|
} else if (!strcmpstart(msg, "Search: ")) {
|
|
|
|
return;
|
2006-07-07 19:33:30 +02:00
|
|
|
}
|
2006-08-28 05:15:38 +02:00
|
|
|
log(warn?LOG_WARN: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. */
|
2006-08-14 23:44:34 +02:00
|
|
|
int
|
2005-06-11 20:52:12 +02:00
|
|
|
dns_init(void)
|
|
|
|
{
|
2005-12-03 03:01:18 +01:00
|
|
|
init_cache_map();
|
2006-08-04 22:30:45 +02:00
|
|
|
#ifdef USE_EVENTDNS
|
2006-08-04 20:24:41 +02:00
|
|
|
if (server_mode(get_options()))
|
2006-08-28 05:15:55 +02:00
|
|
|
return configure_nameservers(1);
|
|
|
|
#else
|
|
|
|
dnsworkers_rotate();
|
2006-08-14 23:44:34 +02:00
|
|
|
#endif
|
2006-08-16 04:18:55 +02:00
|
|
|
return 0;
|
2003-06-17 16:31:05 +02:00
|
|
|
}
|
2003-02-14 08:53:55 +01:00
|
|
|
|
2006-08-28 05:16:02 +02:00
|
|
|
/** Called when DNS-related options change (or may have changed) */
|
2006-08-28 05:15:55 +02:00
|
|
|
void
|
|
|
|
dns_reset(void)
|
|
|
|
{
|
|
|
|
#ifdef USE_EVENTDNS
|
|
|
|
or_options_t *options = get_options();
|
|
|
|
if (! server_mode(options)) {
|
|
|
|
eventdns_clear_nameservers_and_suspend();
|
|
|
|
eventdns_search_clear();
|
|
|
|
nameservers_configured = 0;
|
|
|
|
tor_free(resolv_conf_fname);
|
|
|
|
resolv_conf_mtime = 0;
|
|
|
|
} else {
|
|
|
|
if (configure_nameservers(0) < 0)
|
|
|
|
/* XXXX */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
dnsworkers_rotate();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2006-08-28 05:16:02 +02:00
|
|
|
/** Helper: Given a TTL from a DNS response, determine what TTL to give the
|
|
|
|
* OP that asked us to resolve it. */
|
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;
|
|
|
|
}
|
|
|
|
|
2006-08-28 05:16:02 +02:00
|
|
|
/** Helper: Given a TTL from a DNS response, determine how long to hold it in
|
|
|
|
* our cache. */
|
2006-06-03 23:41:14 +02:00
|
|
|
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-09-21 23:48:06 +02:00
|
|
|
if (r->is_reverse)
|
|
|
|
tor_free(r->result.hostname);
|
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);
|
|
|
|
}
|
|
|
|
|
2006-08-28 05:16:02 +02:00
|
|
|
/** Free all storage held in the DNS cache and related structures. */
|
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;
|
2006-08-28 05:16:02 +02:00
|
|
|
#ifdef USE_EVENTDNS
|
|
|
|
tor_free(resolv_conf_fname);
|
|
|
|
#endif
|
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);
|
2006-09-21 23:48:06 +02:00
|
|
|
if (resolve->is_reverse)
|
|
|
|
tor_free(resolve->result.hostname);
|
2006-07-31 20:01:18 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2006-09-21 23:48:06 +02:00
|
|
|
/** Send a response to the RESOLVE request of a connection for an in-addr.arpa
|
|
|
|
* address on connection <b>conn</b> which yielded the result <b>hostname</b>.
|
|
|
|
* The answer type will be RESOLVED_HOSTNAME.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
send_resolved_hostname_cell(edge_connection_t *conn, const char *hostname)
|
|
|
|
{
|
|
|
|
char buf[RELAY_PAYLOAD_SIZE];
|
|
|
|
size_t buflen;
|
|
|
|
uint32_t ttl;
|
|
|
|
size_t namelen = strlen(hostname);
|
|
|
|
|
|
|
|
tor_assert(namelen < 256);
|
|
|
|
ttl = dns_clip_ttl(conn->address_ttl);
|
|
|
|
|
|
|
|
buf[0] = RESOLVED_TYPE_HOSTNAME;
|
|
|
|
buf[1] = (uint8_t)namelen;
|
|
|
|
memcpy(buf+2, hostname, namelen);
|
|
|
|
set_uint32(buf+2+namelen, htonl(ttl));
|
|
|
|
buflen = 2+namelen+4;
|
|
|
|
|
|
|
|
connection_edge_send_command(conn, circuit_get_by_edge_conn(conn),
|
|
|
|
RELAY_COMMAND_RESOLVED, buf, buflen,
|
|
|
|
conn->cpath_layer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Given a lower-case <b>address</b>, check to see whether it's a
|
|
|
|
* 1.2.3.4.in-addr.arpa address used for reverse lookups. If so,
|
|
|
|
* parse it and place the address in <b>in</b> if present. Return 1 on success;
|
|
|
|
* 0 if the address is not in in-addr.arpa format, and -1 if the address is
|
|
|
|
* malformed. */
|
|
|
|
static int
|
|
|
|
parse_inaddr_arpa_address(const char *address, struct in_addr *in)
|
|
|
|
{
|
|
|
|
char buf[INET_NTOA_BUF_LEN];
|
|
|
|
char *cp;
|
|
|
|
size_t len;
|
|
|
|
struct in_addr inaddr;
|
|
|
|
|
|
|
|
cp = strstr(address, ".in-addr.arpa");
|
|
|
|
if (!cp || *(cp+strlen(".in-addr.arpa")))
|
|
|
|
return 0; /* not an .in-addr.arpa address */
|
|
|
|
|
|
|
|
len = cp - address;
|
|
|
|
|
|
|
|
if (len >= INET_NTOA_BUF_LEN)
|
|
|
|
return -1; /* Too long. */
|
|
|
|
|
|
|
|
memcpy(buf, cp, len);
|
|
|
|
buf[len] = '\0';
|
|
|
|
if (tor_inet_aton(buf, &inaddr) == 0)
|
|
|
|
return -1; /* malformed. */
|
|
|
|
|
|
|
|
if (in) {
|
|
|
|
uint32_t a;
|
|
|
|
/* reverse the bytes */
|
|
|
|
a = ( ((inaddr.s_addr & 0x000000fful) << 24)
|
|
|
|
|((inaddr.s_addr & 0x0000ff00ul) << 8)
|
|
|
|
|((inaddr.s_addr & 0x00ff0000ul) >> 8)
|
|
|
|
|((inaddr.s_addr & 0xff000000ul) >> 24));
|
|
|
|
inaddr.s_addr = a;
|
|
|
|
|
|
|
|
memcpy(in, &inaddr, sizeof(inaddr));
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
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;
|
2005-02-04 02:49:58 +01:00
|
|
|
circuit_t *circ;
|
2006-09-21 23:48:06 +02:00
|
|
|
struct in_addr in;
|
2006-08-10 11:02:26 +02:00
|
|
|
time_t now = time(NULL);
|
2006-09-21 23:48:06 +02:00
|
|
|
int is_reverse = 0, is_resolve, r;
|
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-09-21 23:48:06 +02:00
|
|
|
is_resolve = exitconn->_base.purpose = EXIT_PURPOSE_RESOLVE;
|
|
|
|
|
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-09-21 23:48:06 +02:00
|
|
|
if (is_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
|
|
|
|
2006-09-21 23:48:06 +02:00
|
|
|
/* Check whether this is a reverse lookup. If it's malformed, or it's a
|
|
|
|
* .in-addr.arpa address but this isn't a resolve request, kill the
|
|
|
|
* connecction.
|
|
|
|
*/
|
|
|
|
if ((r = parse_inaddr_arpa_address(exitconn->_base.address, NULL)) != 0) {
|
|
|
|
if (r == 1)
|
|
|
|
is_reverse = 1;
|
|
|
|
|
|
|
|
#ifdef USE_EVENTDNS
|
|
|
|
if (!is_reverse || !is_resolve) {
|
|
|
|
if (!is_reverse)
|
|
|
|
log_info(LD_EXIT, "Bad .in-addr.arpa address \"%s\"; sending error.",
|
|
|
|
escaped_safe_str(exitconn->_base.address));
|
|
|
|
else if (!is_resolve)
|
|
|
|
log_info(LD_EXIT,
|
|
|
|
"Attempt to connect to a .in-addr.arpa address \"%s\"; "
|
|
|
|
"sending error.",
|
|
|
|
escaped_safe_str(exitconn->_base.address));
|
|
|
|
#else
|
|
|
|
if (1) {
|
|
|
|
log_info(LD_PROTOCOL, "Dnsworker code does not support in-addr.arpa "
|
|
|
|
"domain, but received a request for \"%s\"; sending error.",
|
|
|
|
escaped_safe_str(exitconn->_base.address));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (exitconn->_base.purpose == EXIT_PURPOSE_RESOLVE)
|
|
|
|
send_resolved_cell(exitconn, RESOLVED_TYPE_ERROR);
|
|
|
|
circ = circuit_get_by_edge_conn(exitconn);
|
|
|
|
if (circ)
|
|
|
|
circuit_detach_stream(circ, exitconn);
|
|
|
|
if (!exitconn->_base.marked_for_close)
|
|
|
|
connection_free(TO_CONN(exitconn));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-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,
|
2006-09-21 23:48:06 +02:00
|
|
|
escaped_safe_str(resolve->address));
|
|
|
|
exitconn->address_ttl = resolve->ttl;
|
|
|
|
if (resolve->is_reverse) {
|
|
|
|
tor_assert(is_resolve);
|
|
|
|
send_resolved_hostname_cell(exitconn, resolve->result.hostname);
|
|
|
|
} else {
|
|
|
|
exitconn->_base.addr = resolve->result.addr;
|
|
|
|
if (is_resolve)
|
|
|
|
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-09-21 23:48:06 +02:00
|
|
|
if (is_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-09-21 23:48:06 +02:00
|
|
|
resolve->is_reverse = is_reverse;
|
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-09-21 23:48:06 +02:00
|
|
|
dns_cancel_pending_resolve(const 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);
|
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-09-06 22:22:05 +02:00
|
|
|
|
|
|
|
resolve->state = CACHE_STATE_DONE;
|
2003-06-25 09:19:30 +02:00
|
|
|
}
|
|
|
|
|
2006-08-28 05:16:02 +02:00
|
|
|
/** Helper: adds an entry to the DNS cache mapping <b>address</b> to the ipv4
|
2006-09-07 03:23:15 +02:00
|
|
|
* address <b>addr</b>. <b>ttl</b> is a cache ttl; <b>outcome</b> is one of
|
2006-09-21 23:48:06 +02:00
|
|
|
* DNS_RESOLVE_{FAILED_TRANSIENT|FAILED_PERMANENT|SUCCEEDED}.
|
|
|
|
*
|
|
|
|
* DOCDOC args
|
|
|
|
**/
|
2006-07-31 20:01:18 +02:00
|
|
|
static void
|
2006-09-21 23:48:06 +02:00
|
|
|
add_answer_to_cache(const char *address, int is_reverse, uint32_t addr,
|
|
|
|
const char *hostname, char outcome, uint32_t ttl)
|
2006-07-31 20:01:18 +02:00
|
|
|
{
|
|
|
|
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));
|
2006-09-21 23:48:06 +02:00
|
|
|
if (is_reverse) {
|
|
|
|
tor_assert(hostname);
|
|
|
|
resolve->result.hostname = tor_strdup(hostname);
|
|
|
|
} else {
|
|
|
|
tor_assert(!hostname);
|
|
|
|
resolve->result.addr = addr;
|
|
|
|
}
|
2006-07-31 20:01:18 +02:00
|
|
|
resolve->ttl = ttl;
|
|
|
|
assert_resolve_ok(resolve);
|
|
|
|
HT_INSERT(cache_map, &cache_root, resolve);
|
|
|
|
set_expiry(resolve, time(NULL) + dns_get_expiry_ttl(ttl));
|
|
|
|
}
|
|
|
|
|
2006-08-28 05:16:02 +02:00
|
|
|
/** Called on the OR side when a DNS worker or the eventdns library tells us
|
|
|
|
* the outcome of a DNS resolve: tell all pending connections about the result
|
|
|
|
* of the lookup, and 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
|
2004-05-10 06:34:48 +02:00
|
|
|
* 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-09-21 23:48:06 +02:00
|
|
|
dns_found_answer(const char *address, int is_reverse, uint32_t addr,
|
|
|
|
const char *hostname, 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-09-21 23:48:06 +02:00
|
|
|
add_answer_to_cache(address, is_reverse, addr, hostname, 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) {
|
2006-09-21 23:48:06 +02:00
|
|
|
tor_assert(!is_reverse);
|
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-09-21 23:48:06 +02:00
|
|
|
if (is_reverse)
|
|
|
|
send_resolved_hostname_cell(pendconn, hostname);
|
|
|
|
else
|
|
|
|
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-09-21 23:48:06 +02:00
|
|
|
add_answer_to_cache(address, is_reverse, addr, hostname, 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;
|
|
|
|
}
|
|
|
|
|
2006-08-28 05:16:02 +02:00
|
|
|
/** Called when a connection to a dnsworker hits an EOF; this only happens
|
|
|
|
* when a dnsworker dies unexpectedly. */
|
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;
|
2006-09-21 23:48:06 +02:00
|
|
|
dns_found_answer(conn->address, 0, ntohl(addr), NULL, 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.
|
|
|
|
**/
|
2006-08-28 05:15:55 +02:00
|
|
|
static void
|
2005-06-11 20:52:12 +02:00
|
|
|
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.
|
|
|
|
*/
|
2006-09-06 10:42:20 +02:00
|
|
|
static void
|
2005-06-11 20:52:12 +02:00
|
|
|
dnsworker_main(void *data)
|
|
|
|
{
|
2006-08-25 23:02:01 +02:00
|
|
|
char address[MAX_ADDRESSLEN+1]; /* Plus a byte for a final '.' */
|
2003-12-14 08:40:47 +01:00
|
|
|
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;
|
2006-08-28 05:15:50 +02:00
|
|
|
int search = get_options()->SearchDomains;
|
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-08-25 23:02:01 +02:00
|
|
|
/* Add a period to prevent local domain search, and NUL-terminate. */
|
2006-08-28 05:15:50 +02:00
|
|
|
if (address[address_len-1] != '.' && !search) {
|
2006-08-25 23:02:01 +02:00
|
|
|
address[address_len] = '.';
|
|
|
|
address[address_len+1] = '\0';
|
|
|
|
} else {
|
|
|
|
address[address_len] = '\0';
|
|
|
|
}
|
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-08-25 23:02:01 +02: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-08-25 23:02:01 +02: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-08-25 23:02:01 +02: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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 */
|
2006-09-06 03:49:55 +02:00
|
|
|
spawn_func((void*) 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 */
|
2006-08-28 05:16:02 +02:00
|
|
|
|
|
|
|
/** Eventdns helper: return true iff the eventdns result <b>err</b> is
|
|
|
|
* a transient failure. */
|
2006-06-03 22:52:24 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2006-08-28 05:16:02 +02:00
|
|
|
/* Dummy function; never called with eventdns enabled. */
|
2006-06-03 22:52:24 +02:00
|
|
|
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;
|
|
|
|
}
|
2006-08-28 05:16:02 +02:00
|
|
|
/* Dummy function; never called with eventdns enabled. */
|
2006-06-03 22:52:24 +02:00
|
|
|
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;
|
|
|
|
}
|
2006-08-28 05:16:02 +02:00
|
|
|
/* Dummy function; never called with eventdns enabled. */
|
2006-06-03 22:52:24 +02:00
|
|
|
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-27 03:41:08 +02:00
|
|
|
|
2006-08-28 05:16:02 +02:00
|
|
|
/** Configure eventdns nameservers if force is true, or if the configuration
|
|
|
|
* has changed since the last time we called this function. On Unix, this
|
|
|
|
* reads from options->ResolvConf or /etc/resolv.conf; on Windows, this reads
|
|
|
|
* from options->ResolvConf or the registry. Return 0 on success or -1 on
|
|
|
|
* failure. */
|
2006-08-14 23:44:34 +02:00
|
|
|
static int
|
2006-08-28 05:15:55 +02:00
|
|
|
configure_nameservers(int force)
|
2006-08-04 20:24:41 +02:00
|
|
|
{
|
|
|
|
or_options_t *options;
|
2006-08-28 05:15:50 +02:00
|
|
|
const char *conf_fname;
|
|
|
|
struct stat st;
|
2006-08-04 20:24:41 +02:00
|
|
|
options = get_options();
|
2006-08-28 05:15:50 +02:00
|
|
|
conf_fname = options->ResolvConf;
|
|
|
|
#ifndef MS_WINDOWS
|
2006-08-28 05:15:55 +02:00
|
|
|
if (!conf_fname)
|
|
|
|
conf_fname = "/etc/resolv.conf";
|
2006-08-28 05:15:50 +02:00
|
|
|
#endif
|
|
|
|
|
2006-08-28 05:15:55 +02:00
|
|
|
eventdns_set_log_fn(eventdns_log_cb);
|
2006-08-28 05:15:50 +02:00
|
|
|
if (conf_fname) {
|
2006-08-28 05:15:55 +02:00
|
|
|
if (stat(conf_fname, &st)) {
|
|
|
|
log_warn(LD_EXIT, "Unable to stat resolver configuration in '%s'",
|
|
|
|
conf_fname);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!force && resolv_conf_fname && !strcmp(conf_fname,resolv_conf_fname)
|
|
|
|
&& st.st_mtime == resolv_conf_mtime) {
|
|
|
|
log_info(LD_EXIT, "No change to '%s'", conf_fname);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (nameservers_configured) {
|
|
|
|
eventdns_search_clear();
|
|
|
|
eventdns_clear_nameservers_and_suspend();
|
|
|
|
}
|
2006-08-28 05:15:50 +02:00
|
|
|
log_info(LD_EXIT, "Parsing resolver configuration in '%s'", conf_fname);
|
|
|
|
if (eventdns_resolv_conf_parse(DNS_OPTIONS_ALL, conf_fname))
|
|
|
|
return -1;
|
2006-08-14 23:44:34 +02:00
|
|
|
if (eventdns_count_nameservers() == 0) {
|
2006-08-28 05:15:50 +02:00
|
|
|
log_warn(LD_EXIT, "Unable to find any nameservers in '%s'.", conf_fname);
|
2006-08-14 23:44:34 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2006-08-28 05:15:55 +02:00
|
|
|
tor_free(resolv_conf_fname);
|
2006-08-28 22:50:47 +02:00
|
|
|
resolv_conf_fname = tor_strdup(conf_fname);
|
2006-08-28 05:15:55 +02:00
|
|
|
resolv_conf_mtime = st.st_mtime;
|
|
|
|
if (nameservers_configured)
|
|
|
|
eventdns_resume();
|
2006-08-28 05:15:50 +02:00
|
|
|
}
|
2006-08-04 20:24:41 +02:00
|
|
|
#ifdef MS_WINDOWS
|
2006-08-28 05:15:50 +02:00
|
|
|
else {
|
2006-08-28 05:15:55 +02:00
|
|
|
if (nameservers_configured) {
|
|
|
|
eventdns_search_clear();
|
|
|
|
eventdns_clear_nameservers_and_suspend();
|
|
|
|
}
|
2006-09-07 05:53:14 +02:00
|
|
|
if (eventdns_config_windows_nameservers()) {
|
2006-09-07 06:02:52 +02:00
|
|
|
log_warn(LD_EXIT,"Could not config nameservers.");
|
2006-08-14 23:44:34 +02:00
|
|
|
return -1;
|
2006-09-07 06:02:52 +02:00
|
|
|
}
|
2006-08-14 23:44:34 +02:00
|
|
|
if (eventdns_count_nameservers() == 0) {
|
2006-08-27 03:41:08 +02:00
|
|
|
log_warn(LD_EXIT, "Unable to find any platform nameservers in "
|
2006-08-28 05:15:50 +02:00
|
|
|
"your Windows configuration. Perhaps you should list a "
|
|
|
|
"ResolvConf file in your torrc?");
|
2006-08-14 23:44:34 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2006-08-28 05:15:55 +02:00
|
|
|
if (nameservers_configured)
|
|
|
|
eventdns_resume();
|
|
|
|
tor_free(resolv_conf_fname);
|
|
|
|
resolv_conf_mtime = 0;
|
2006-08-04 20:24:41 +02:00
|
|
|
}
|
2006-08-28 05:15:50 +02:00
|
|
|
#endif
|
|
|
|
|
2006-08-04 20:24:41 +02:00
|
|
|
nameservers_configured = 1;
|
2006-08-15 06:50:17 +02:00
|
|
|
return 0;
|
2006-08-04 20:24:41 +02:00
|
|
|
}
|
2006-08-27 03:41:08 +02:00
|
|
|
|
2006-08-28 05:16:02 +02:00
|
|
|
/** For eventdns: Called when we get an answer for a request we launched.
|
|
|
|
* See eventdns.h for arguments; 'arg' holds the address we tried to resolve.
|
|
|
|
*/
|
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-09-21 23:48:06 +02:00
|
|
|
int is_reverse = 0;
|
2006-06-03 23:41:14 +02:00
|
|
|
int status = DNS_RESOLVE_FAILED_PERMANENT;
|
|
|
|
uint32_t addr = 0;
|
2006-09-21 23:48:06 +02:00
|
|
|
const char *hostname = NULL;
|
|
|
|
|
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-09-21 23:48:11 +02:00
|
|
|
char *escaped_address;
|
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));
|
2006-09-21 23:48:11 +02:00
|
|
|
escaped_address = esc_for_log(string_address);
|
2006-06-06 01:01:22 +02:00
|
|
|
log_debug(LD_EXIT, "eventdns said that %s resolves to %s",
|
2006-09-21 23:48:11 +02:00
|
|
|
safe_str(escaped_address),
|
|
|
|
escaped_safe_str(answer_buf));
|
|
|
|
tor_free(escaped_address);
|
2006-09-21 23:48:06 +02:00
|
|
|
} else if (type == DNS_PTR && count) {
|
2006-09-21 23:48:11 +02:00
|
|
|
char *escaped_address;
|
2006-09-21 23:48:06 +02:00
|
|
|
is_reverse = 1;
|
|
|
|
hostname = ((char**)addresses)[0];
|
2006-09-21 23:48:11 +02:00
|
|
|
escaped_address = esc_for_log(string_address);
|
2006-09-21 23:48:06 +02:00
|
|
|
log_debug(LD_EXIT, "eventdns said that %s resolves to %s",
|
2006-09-21 23:48:11 +02:00
|
|
|
safe_str(escaped_address),
|
|
|
|
escaped_safe_str(hostname));
|
|
|
|
tor_free(escaped_address);
|
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-09-21 23:48:06 +02:00
|
|
|
dns_found_answer(string_address, is_reverse, addr, hostname, status, ttl);
|
2006-06-03 22:52:24 +02:00
|
|
|
tor_free(string_address);
|
|
|
|
}
|
2005-06-09 21:03:31 +02:00
|
|
|
|
2006-08-28 05:16:02 +02:00
|
|
|
/** For eventdns: start resolving as necessary to find the target for
|
|
|
|
* <b>exitconn</b> */
|
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-09-21 23:48:06 +02:00
|
|
|
struct in_addr in;
|
2006-06-06 01:01:22 +02:00
|
|
|
int r;
|
2006-08-28 05:15:50 +02:00
|
|
|
int options = get_options()->SearchDomains ? 0 : DNS_QUERY_NO_SEARCH;
|
2006-08-28 05:16:02 +02:00
|
|
|
/* What? Nameservers not configured? Sounds like a bug. */
|
|
|
|
if (!nameservers_configured) {
|
|
|
|
log_warn(LD_EXIT, "Harmless bug: nameservers not configured, but resolve "
|
|
|
|
"launched. Configuring.");
|
2006-08-28 05:15:55 +02:00
|
|
|
if (configure_nameservers(1) < 0)
|
2006-08-27 03:41:08 +02:00
|
|
|
return -1;
|
2006-08-28 05:16:02 +02:00
|
|
|
}
|
2006-09-21 23:48:06 +02:00
|
|
|
|
|
|
|
r = parse_inaddr_arpa_address(exitconn->_base.address, &in);
|
|
|
|
if (r == 0) {
|
|
|
|
log_info(LD_EXIT, "Launching eventdns request for %s",
|
|
|
|
escaped_safe_str(exitconn->_base.address));
|
|
|
|
r = eventdns_resolve_ipv4(exitconn->_base.address, options,
|
|
|
|
eventdns_callback, addr);
|
|
|
|
} else if (r == 1) {
|
|
|
|
log_info(LD_EXIT, "Launching eventdns reverse request for %s",
|
|
|
|
escaped_safe_str(exitconn->_base.address));
|
|
|
|
r = eventdns_resolve_reverse(&in, DNS_QUERY_NO_SEARCH,
|
|
|
|
eventdns_callback, addr);
|
|
|
|
} else if (r == -1) {
|
|
|
|
log_warn(LD_BUG, "Somehow a malformed in-addr.arpa address reached here.");
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2006-08-27 03:41:08 +02:00
|
|
|
dns_cancel_pending_resolve(addr); /* also sends end and frees */
|
2006-06-03 22:52:24 +02:00
|
|
|
tor_free(addr);
|
|
|
|
}
|
|
|
|
return r ? -1 : 0;
|
|
|
|
}
|
|
|
|
#endif /* USE_EVENTDNS */
|
2006-06-03 23:41:14 +02:00
|
|
|
|
2006-08-28 05:16:02 +02:00
|
|
|
/** Exit with an assertion if <b>resolve</b> is corrupt. */
|
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);
|
2006-09-21 23:48:06 +02:00
|
|
|
if (resolve->is_reverse)
|
|
|
|
tor_assert(!resolve->result.hostname);
|
|
|
|
else
|
|
|
|
tor_assert(!resolve->result.addr);
|
2006-07-31 20:01:45 +02:00
|
|
|
}
|
2006-07-11 22:51:58 +02:00
|
|
|
}
|
|
|
|
|
2006-08-15 06:50:17 +02:00
|
|
|
#ifdef DEBUG_DNS_CACHE
|
2006-08-28 05:16:02 +02:00
|
|
|
/** Exit with an assertion if the DNS cache is corrupt. */
|
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
|
|
|
}
|
2006-08-15 06:50:17 +02:00
|
|
|
#endif
|
2006-08-28 05:15:55 +02:00
|
|
|
|