2006-02-09 06:46:49 +01:00
|
|
|
/* Copyright (c) 2001 Matej Pfajfar.
|
|
|
|
* Copyright (c) 2001-2004, Roger Dingledine.
|
2007-12-12 22:09:01 +01:00
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
|
|
|
* Copyright (c) 2007, The Tor Project, Inc. */
|
Implemented link padding and receiver token buckets
Each socket reads at most 'bandwidth' bytes per second sustained, but
can handle bursts of up to 10*bandwidth bytes.
Cells are now sent out at evenly-spaced intervals, with padding sent
out otherwise. Set Linkpadding=0 in the rc file to send cells as soon
as they're available (and to never send padding cells).
Added license/copyrights statements at the top of most files.
router->min and router->max have been merged into a single 'bandwidth'
value. We should make the routerinfo_t reflect this (want to do that,
Mat?)
As the bandwidth increases, and we want to stop sleeping more and more
frequently to send a single cell, cpu usage goes up. At 128kB/s we're
pretty much calling poll with a timeout of 1ms or even 0ms. The current
code takes a timeout of 0-9ms and makes it 10ms. prepare_for_poll()
handles everything that should have happened in the past, so as long as
our buffers don't get too full in that 10ms, we're ok.
Speaking of too full, if you run three servers at 100kB/s with -l debug,
it spends too much time printing debugging messages to be able to keep
up with the cells. The outbuf ultimately fills up and it kills that
connection. If you run with -l err, it works fine up through 500kB/s and
probably beyond. Down the road we'll want to teach it to recognize when
an outbuf is getting full, and back off.
svn:r50
2002-07-16 03:12:15 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
/* $Id$ */
|
2005-12-14 21:40:40 +01:00
|
|
|
const char routerlist_c_id[] =
|
|
|
|
"$Id$";
|
Implemented link padding and receiver token buckets
Each socket reads at most 'bandwidth' bytes per second sustained, but
can handle bursts of up to 10*bandwidth bytes.
Cells are now sent out at evenly-spaced intervals, with padding sent
out otherwise. Set Linkpadding=0 in the rc file to send cells as soon
as they're available (and to never send padding cells).
Added license/copyrights statements at the top of most files.
router->min and router->max have been merged into a single 'bandwidth'
value. We should make the routerinfo_t reflect this (want to do that,
Mat?)
As the bandwidth increases, and we want to stop sleeping more and more
frequently to send a single cell, cpu usage goes up. At 128kB/s we're
pretty much calling poll with a timeout of 1ms or even 0ms. The current
code takes a timeout of 0-9ms and makes it 10ms. prepare_for_poll()
handles everything that should have happened in the past, so as long as
our buffers don't get too full in that 10ms, we're ok.
Speaking of too full, if you run three servers at 100kB/s with -l debug,
it spends too much time printing debugging messages to be able to keep
up with the cells. The outbuf ultimately fills up and it kills that
connection. If you run with -l err, it works fine up through 500kB/s and
probably beyond. Down the road we'll want to teach it to recognize when
an outbuf is getting full, and back off.
svn:r50
2002-07-16 03:12:15 +02:00
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/**
|
|
|
|
* \file routerlist.c
|
2004-05-10 19:30:51 +02:00
|
|
|
* \brief Code to
|
2004-05-05 02:30:43 +02:00
|
|
|
* maintain and access the global list of routerinfos for known
|
|
|
|
* servers.
|
2004-05-09 18:47:25 +02:00
|
|
|
**/
|
2004-05-05 02:30:43 +02:00
|
|
|
|
2005-06-11 07:31:17 +02:00
|
|
|
#include "or.h"
|
|
|
|
|
2007-05-19 20:31:04 +02:00
|
|
|
// #define DEBUG_ROUTERLIST
|
|
|
|
|
2002-09-26 14:09:10 +02:00
|
|
|
/****************************************************************************/
|
|
|
|
|
2002-09-24 12:43:57 +02:00
|
|
|
/* static function prototypes */
|
2007-10-24 20:42:16 +02:00
|
|
|
static routerstatus_t *router_pick_directory_server_impl(
|
|
|
|
authority_type_t auth, int flags);
|
2005-12-14 23:00:58 +01:00
|
|
|
static routerstatus_t *router_pick_trusteddirserver_impl(
|
2007-10-24 20:42:16 +02:00
|
|
|
authority_type_t auth, int flags);
|
2004-10-12 17:55:20 +02:00
|
|
|
static void mark_all_trusteddirservers_up(void);
|
2005-06-21 01:04:13 +02:00
|
|
|
static int router_nickname_matches(routerinfo_t *router, const char *nickname);
|
2005-10-05 07:03:52 +02:00
|
|
|
static void trusted_dir_server_free(trusted_dir_server_t *ds);
|
2007-10-08 21:56:57 +02:00
|
|
|
static void launch_router_descriptor_downloads(smartlist_t *downloadable,
|
|
|
|
time_t now);
|
|
|
|
static void update_consensus_router_descriptor_downloads(time_t now);
|
2005-12-27 06:26:03 +01:00
|
|
|
static int signed_desc_digest_is_recognized(signed_descriptor_t *desc);
|
2006-08-15 05:54:09 +02:00
|
|
|
static void update_router_have_minimum_dir_info(void);
|
2007-09-26 18:19:44 +02:00
|
|
|
static const char *signed_descriptor_get_body_impl(signed_descriptor_t *desc,
|
|
|
|
int with_annotations);
|
2007-10-09 22:44:45 +02:00
|
|
|
static void list_pending_downloads(digestmap_t *result,
|
|
|
|
int purpose, const char *prefix);
|
2003-12-09 05:29:52 +01:00
|
|
|
|
2007-06-12 00:19:40 +02:00
|
|
|
DECLARE_TYPED_DIGESTMAP_FNS(sdmap_, digest_sd_map_t, signed_descriptor_t)
|
|
|
|
DECLARE_TYPED_DIGESTMAP_FNS(rimap_, digest_ri_map_t, routerinfo_t)
|
|
|
|
DECLARE_TYPED_DIGESTMAP_FNS(eimap_, digest_ei_map_t, extrainfo_t)
|
|
|
|
|
2003-12-09 05:29:52 +01:00
|
|
|
/****************************************************************************/
|
2003-12-13 02:43:21 +01:00
|
|
|
|
2005-11-01 04:48:51 +01:00
|
|
|
/** Global list of a trusted_dir_server_t object for each trusted directory
|
|
|
|
* server. */
|
|
|
|
static smartlist_t *trusted_dir_servers = NULL;
|
2008-01-27 00:18:30 +01:00
|
|
|
|
|
|
|
/** DOCDOC */
|
|
|
|
typedef struct cert_list_t {
|
|
|
|
download_status_t dl_status;
|
|
|
|
smartlist_t *certs;
|
|
|
|
} cert_list_t;
|
|
|
|
/** Map from v3 identity key digest to cert_list_t. */
|
|
|
|
static digestmap_t *trusted_dir_certs = NULL;
|
|
|
|
/** True iff any key certificate in at least one member of
|
|
|
|
* <b>trusted_dir_certs</b> has changed since we last flushed the
|
2007-05-29 19:31:13 +02:00
|
|
|
* certificates to disk. */
|
2007-05-22 19:58:25 +02:00
|
|
|
static int trusted_dir_servers_certs_changed = 0;
|
2004-05-04 20:17:45 +02:00
|
|
|
|
2005-09-05 01:12:27 +02:00
|
|
|
/** Global list of all of the routers that we know about. */
|
2004-05-04 20:17:45 +02:00
|
|
|
static routerlist_t *routerlist = NULL;
|
2004-02-29 02:31:33 +01:00
|
|
|
|
2005-10-05 03:53:44 +02:00
|
|
|
/** List of strings for nicknames we've already warned about and that are
|
|
|
|
* still unknown / unavailable. */
|
|
|
|
static smartlist_t *warned_nicknames = NULL;
|
2005-11-01 04:48:51 +01:00
|
|
|
|
2005-10-11 03:57:28 +02:00
|
|
|
/** The last time we tried to download any routerdesc, or 0 for "never". We
|
|
|
|
* use this to rate-limit download attempts when the number of routerdescs to
|
|
|
|
* download is low. */
|
2005-10-05 07:03:52 +02:00
|
|
|
static time_t last_routerdesc_download_attempted = 0;
|
2005-11-01 04:48:51 +01:00
|
|
|
|
2007-05-09 06:15:46 +02:00
|
|
|
/** Return the number of directory authorities whose type matches some bit set
|
|
|
|
* in <b>type</b> */
|
2007-06-14 00:44:30 +02:00
|
|
|
int
|
2007-05-09 06:15:46 +02:00
|
|
|
get_n_authorities(authority_type_t type)
|
2006-09-29 01:57:59 +02:00
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
if (!trusted_dir_servers)
|
|
|
|
return 0;
|
|
|
|
SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ds,
|
2007-05-09 06:15:46 +02:00
|
|
|
if (ds->type & type)
|
2006-09-29 01:57:59 +02:00
|
|
|
++n);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2007-05-09 06:15:46 +02:00
|
|
|
#define get_n_v2_authorities() get_n_authorities(V2_AUTHORITY)
|
|
|
|
|
2008-01-27 00:18:30 +01:00
|
|
|
/** DOCDOC */
|
|
|
|
static cert_list_t *
|
|
|
|
get_cert_list(const char *id_digest)
|
|
|
|
{
|
|
|
|
cert_list_t *cl;
|
|
|
|
if (!trusted_dir_certs)
|
|
|
|
trusted_dir_certs = digestmap_new();
|
|
|
|
cl = digestmap_get(trusted_dir_certs, id_digest);
|
|
|
|
if (!cl) {
|
|
|
|
cl = tor_malloc_zero(sizeof(cert_list_t));
|
|
|
|
cl->certs = smartlist_create();
|
|
|
|
digestmap_set(trusted_dir_certs, id_digest, cl);
|
|
|
|
}
|
|
|
|
return cl;
|
|
|
|
}
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/** Reload the cached v3 key certificates from the cached-certs file in
|
|
|
|
* the data directory. Return 0 on success, -1 on failure. */
|
2007-05-22 19:58:25 +02:00
|
|
|
int
|
|
|
|
trusted_dirs_reload_certs(void)
|
|
|
|
{
|
2007-10-17 18:55:44 +02:00
|
|
|
char *filename;
|
2007-05-22 19:58:25 +02:00
|
|
|
char *contents;
|
|
|
|
int r;
|
|
|
|
|
2007-10-17 18:55:44 +02:00
|
|
|
filename = get_datadir_fname("cached-certs");
|
2007-07-29 00:14:42 +02:00
|
|
|
contents = read_file_to_str(filename, RFTS_IGNORE_MISSING, NULL);
|
2007-10-17 18:55:44 +02:00
|
|
|
tor_free(filename);
|
2007-05-22 19:58:25 +02:00
|
|
|
if (!contents)
|
2007-07-29 00:14:42 +02:00
|
|
|
return 0;
|
2007-05-22 19:58:25 +02:00
|
|
|
r = trusted_dirs_load_certs_from_string(contents, 1);
|
|
|
|
tor_free(contents);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/** Load a bunch of new key certificates from the string <b>contents</b>. If
|
|
|
|
* <b>from_store</b> is true, the certificates are from the cache, and we
|
|
|
|
* don't need to flush them to disk. If <b>from_store</b> is false, we need
|
|
|
|
* to flush any changed certificates to disk. Return 0 on success, -1 on
|
|
|
|
* failure. */
|
2007-05-22 19:58:25 +02:00
|
|
|
int
|
|
|
|
trusted_dirs_load_certs_from_string(const char *contents, int from_store)
|
|
|
|
{
|
|
|
|
trusted_dir_server_t *ds;
|
2008-01-27 00:18:30 +01:00
|
|
|
cert_list_t *cl;
|
2007-05-22 19:58:25 +02:00
|
|
|
const char *s, *eos;
|
|
|
|
|
|
|
|
for (s = contents; *s; s = eos) {
|
|
|
|
authority_cert_t *cert = authority_cert_parse_from_string(s, &eos);
|
2007-07-27 03:10:23 +02:00
|
|
|
int found = 0;
|
2007-05-22 19:58:25 +02:00
|
|
|
if (!cert)
|
|
|
|
break;
|
|
|
|
ds = trusteddirserver_get_by_v3_auth_digest(
|
|
|
|
cert->cache_info.identity_digest);
|
2008-01-27 00:18:30 +01:00
|
|
|
|
|
|
|
cl = get_cert_list(cert->cache_info.identity_digest);
|
2007-05-22 19:58:25 +02:00
|
|
|
|
2008-01-27 00:18:30 +01:00
|
|
|
SMARTLIST_FOREACH(cl->certs, authority_cert_t *, c,
|
2007-07-26 22:49:04 +02:00
|
|
|
{
|
2007-08-14 16:30:25 +02:00
|
|
|
if (!memcmp(c->cache_info.signed_descriptor_digest,
|
|
|
|
cert->cache_info.signed_descriptor_digest,
|
|
|
|
DIGEST_LEN)) {
|
2007-07-26 22:49:04 +02:00
|
|
|
/* we already have this one. continue. */
|
2007-11-08 17:19:04 +01:00
|
|
|
log_info(LD_DIR, "Skipping %s certificate for %s that we "
|
|
|
|
"already have.",
|
|
|
|
from_store ? "cached" : "downloaded", ds->nickname);
|
2007-07-26 22:49:04 +02:00
|
|
|
authority_cert_free(cert);
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (found)
|
|
|
|
continue;
|
2007-05-22 19:58:25 +02:00
|
|
|
|
2008-01-27 00:18:30 +01:00
|
|
|
if (ds) {
|
|
|
|
log_info(LD_DIR, "Adding %s certificate for directory authority %s with "
|
|
|
|
"signing key %s", from_store ? "cached" : "downloaded",
|
|
|
|
ds->nickname, hex_str(cert->signing_key_digest,DIGEST_LEN));
|
|
|
|
} else {
|
|
|
|
log_info(LD_DIR, "Adding %s certificate for unrecognized directory "
|
|
|
|
"authority with signing key %s",
|
|
|
|
from_store ? "cached" : "downloaded",
|
|
|
|
hex_str(cert->signing_key_digest,DIGEST_LEN));
|
|
|
|
}
|
2007-11-08 17:19:04 +01:00
|
|
|
|
2008-01-27 00:18:30 +01:00
|
|
|
smartlist_add(cl->certs, cert);
|
|
|
|
if (ds && cert->cache_info.published_on > ds->addr_current_at) {
|
2007-12-19 05:24:05 +01:00
|
|
|
/* Check to see whether we should update our view of the authority's
|
|
|
|
* address. */
|
2007-11-01 05:14:23 +01:00
|
|
|
if (cert->addr && cert->dir_port &&
|
|
|
|
(ds->addr != cert->addr ||
|
|
|
|
ds->dir_port != cert->dir_port)) {
|
|
|
|
char *a = tor_dup_addr(cert->addr);
|
|
|
|
log_notice(LD_DIR, "Updating address for directory authority %s "
|
|
|
|
"from %s:%d to %s:%d based on in certificate.",
|
|
|
|
ds->nickname, ds->address, (int)ds->dir_port,
|
|
|
|
a, cert->dir_port);
|
|
|
|
tor_free(a);
|
|
|
|
ds->addr = cert->addr;
|
|
|
|
ds->dir_port = cert->dir_port;
|
|
|
|
}
|
|
|
|
ds->addr_current_at = cert->cache_info.published_on;
|
|
|
|
}
|
2007-07-26 22:49:04 +02:00
|
|
|
|
2007-05-22 19:58:25 +02:00
|
|
|
if (!from_store)
|
|
|
|
trusted_dir_servers_certs_changed = 1;
|
|
|
|
}
|
2007-07-28 02:11:34 +02:00
|
|
|
|
|
|
|
trusted_dirs_flush_certs_to_disk();
|
|
|
|
|
2007-10-08 23:21:50 +02:00
|
|
|
networkstatus_note_certs_arrived();
|
2007-05-22 19:58:25 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/** Save all v3 key certifiacates to the cached-certs file. */
|
2007-05-22 19:58:25 +02:00
|
|
|
void
|
|
|
|
trusted_dirs_flush_certs_to_disk(void)
|
|
|
|
{
|
2007-10-17 18:55:44 +02:00
|
|
|
char *filename;
|
2007-07-28 02:11:34 +02:00
|
|
|
smartlist_t *chunks;
|
|
|
|
|
2008-01-27 00:18:30 +01:00
|
|
|
if (!trusted_dir_servers_certs_changed || !trusted_dir_certs)
|
2007-07-28 02:11:34 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
chunks = smartlist_create();
|
2008-01-27 00:18:30 +01:00
|
|
|
DIGESTMAP_FOREACH(trusted_dir_certs, key, cert_list_t *, cl) {
|
|
|
|
SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert,
|
2007-07-26 22:49:04 +02:00
|
|
|
{
|
|
|
|
sized_chunk_t *c = tor_malloc(sizeof(sized_chunk_t));
|
|
|
|
c->bytes = cert->cache_info.signed_descriptor_body;
|
|
|
|
c->len = cert->cache_info.signed_descriptor_len;
|
|
|
|
smartlist_add(chunks, c);
|
|
|
|
});
|
2008-01-27 00:18:30 +01:00
|
|
|
} DIGESTMAP_FOREACH_END
|
|
|
|
|
2007-10-17 18:55:44 +02:00
|
|
|
filename = get_datadir_fname("cached-certs");
|
2007-05-22 19:58:25 +02:00
|
|
|
if (write_chunks_to_file(filename, chunks, 0)) {
|
|
|
|
log_warn(LD_FS, "Error writing certificates to disk.");
|
|
|
|
}
|
2007-10-17 18:55:44 +02:00
|
|
|
tor_free(filename);
|
2007-05-22 19:58:25 +02:00
|
|
|
SMARTLIST_FOREACH(chunks, sized_chunk_t *, c, tor_free(c));
|
|
|
|
smartlist_free(chunks);
|
|
|
|
|
|
|
|
trusted_dir_servers_certs_changed = 0;
|
|
|
|
}
|
|
|
|
|
2007-07-30 19:46:14 +02:00
|
|
|
/** Remove all v3 authority certificates that have been superseded for more
|
|
|
|
* than 48 hours. (If the most recent cert was published more than 48 hours
|
|
|
|
* ago, then we aren't going to get any consensuses signed with older
|
|
|
|
* keys.) */
|
2007-07-28 02:11:34 +02:00
|
|
|
static void
|
|
|
|
trusted_dirs_remove_old_certs(void)
|
|
|
|
{
|
|
|
|
#define OLD_CERT_LIFETIME (48*60*60)
|
2008-01-27 00:18:30 +01:00
|
|
|
if (!trusted_dir_certs)
|
|
|
|
return;
|
|
|
|
|
|
|
|
DIGESTMAP_FOREACH(trusted_dir_certs, key, cert_list_t *, cl) {
|
|
|
|
authority_cert_t *newest = NULL;
|
|
|
|
SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert,
|
2007-07-28 02:11:34 +02:00
|
|
|
if (!newest || (cert->cache_info.published_on >
|
|
|
|
newest->cache_info.published_on))
|
|
|
|
newest = cert);
|
2008-01-27 00:18:30 +01:00
|
|
|
SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert,
|
2007-07-28 02:11:34 +02:00
|
|
|
if (newest && (newest->cache_info.published_on >
|
|
|
|
cert->cache_info.published_on + OLD_CERT_LIFETIME)) {
|
2008-01-27 00:18:30 +01:00
|
|
|
SMARTLIST_DEL_CURRENT(cl->certs, cert);
|
2007-07-28 02:11:34 +02:00
|
|
|
authority_cert_free(cert);
|
|
|
|
trusted_dir_servers_certs_changed = 1;
|
|
|
|
});
|
2008-01-27 00:18:30 +01:00
|
|
|
} DIGESTMAP_FOREACH_END
|
2007-07-28 02:11:34 +02:00
|
|
|
#undef OLD_CERT_LIFETIME
|
|
|
|
|
|
|
|
trusted_dirs_flush_certs_to_disk();
|
|
|
|
}
|
|
|
|
|
2007-10-04 18:21:58 +02:00
|
|
|
/** Return the newest v3 authority certificate whose v3 authority identity key
|
|
|
|
* has digest <b>id_digest</b>. Return NULL if no such authority is known,
|
|
|
|
* or it has no certificate. */
|
2007-08-14 23:09:30 +02:00
|
|
|
authority_cert_t *
|
|
|
|
authority_cert_get_newest_by_id(const char *id_digest)
|
|
|
|
{
|
2008-01-27 00:18:30 +01:00
|
|
|
cert_list_t *cl;
|
2007-08-14 23:09:30 +02:00
|
|
|
authority_cert_t *best = NULL;
|
2008-02-05 20:40:15 +01:00
|
|
|
if (!trusted_dir_certs ||
|
|
|
|
!(cl = digestmap_get(trusted_dir_certs, id_digest)))
|
2007-08-14 23:09:30 +02:00
|
|
|
return NULL;
|
2008-01-27 00:18:30 +01:00
|
|
|
SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert,
|
2007-08-14 23:09:30 +02:00
|
|
|
{
|
|
|
|
if (!best || cert->cache_info.published_on > best->cache_info.published_on)
|
|
|
|
best = cert;
|
|
|
|
});
|
|
|
|
return best;
|
|
|
|
}
|
|
|
|
|
2007-10-04 18:21:58 +02:00
|
|
|
/** Return the newest v3 authority certificate whose directory signing key has
|
|
|
|
* giest <sk_digest</b>. Return NULL if no such certificate is known.
|
|
|
|
*/
|
2007-08-14 23:09:30 +02:00
|
|
|
authority_cert_t *
|
|
|
|
authority_cert_get_by_sk_digest(const char *sk_digest)
|
|
|
|
{
|
2008-01-27 00:18:30 +01:00
|
|
|
if (!trusted_dir_certs)
|
2007-08-14 23:09:30 +02:00
|
|
|
return NULL;
|
2008-01-27 00:18:30 +01:00
|
|
|
|
|
|
|
DIGESTMAP_FOREACH(trusted_dir_certs, key, cert_list_t *, cl) {
|
|
|
|
SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert,
|
2007-08-14 23:09:30 +02:00
|
|
|
{
|
|
|
|
if (!memcmp(cert->signing_key_digest, sk_digest, DIGEST_LEN))
|
|
|
|
return cert;
|
|
|
|
});
|
2008-01-27 00:18:30 +01:00
|
|
|
} DIGESTMAP_FOREACH_END
|
2007-08-14 23:09:30 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-07-30 19:46:14 +02:00
|
|
|
/** Return the v3 authority certificate with signing key matching
|
|
|
|
* <b>sk_digest</b>, for the authority with identity digest <b>id_digest</b>.
|
|
|
|
* Return NULL if no such authority is known. */
|
2007-07-26 22:26:59 +02:00
|
|
|
authority_cert_t *
|
|
|
|
authority_cert_get_by_digests(const char *id_digest,
|
|
|
|
const char *sk_digest)
|
|
|
|
{
|
2008-01-27 00:18:30 +01:00
|
|
|
cert_list_t *cl;
|
2008-02-05 20:40:21 +01:00
|
|
|
if (!trusted_dir_certs ||
|
|
|
|
!(cl = digestmap_get(trusted_dir_certs, id_digest)))
|
2007-07-26 22:26:59 +02:00
|
|
|
return NULL;
|
2008-01-27 00:18:30 +01:00
|
|
|
SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert,
|
2007-07-26 22:49:04 +02:00
|
|
|
if (!memcmp(cert->signing_key_digest, sk_digest, DIGEST_LEN))
|
|
|
|
return cert; );
|
2007-07-26 22:26:59 +02:00
|
|
|
|
2007-07-26 22:49:04 +02:00
|
|
|
return NULL;
|
2007-07-26 22:26:59 +02:00
|
|
|
}
|
|
|
|
|
2008-01-27 00:18:30 +01:00
|
|
|
/** DOCDOC */
|
|
|
|
void
|
|
|
|
authority_cert_get_all(smartlist_t *certs_out)
|
|
|
|
{
|
|
|
|
if (!trusted_dir_certs)
|
|
|
|
return;
|
|
|
|
|
|
|
|
DIGESTMAP_FOREACH(trusted_dir_certs, key, cert_list_t *, cl) {
|
|
|
|
SMARTLIST_FOREACH(cl->certs, authority_cert_t *, c,
|
|
|
|
smartlist_add(certs_out, c));
|
|
|
|
} DIGESTMAP_FOREACH_END
|
|
|
|
}
|
|
|
|
|
|
|
|
/** DOCDOC */
|
|
|
|
void
|
|
|
|
authority_cert_dl_failed(const char *id_digest, int status)
|
|
|
|
{
|
|
|
|
cert_list_t *cl;
|
2008-02-05 20:40:15 +01:00
|
|
|
if (!trusted_dir_certs ||
|
|
|
|
!(cl = digestmap_get(trusted_dir_certs, id_digest)))
|
2008-01-27 00:18:30 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
download_status_failed(&cl->dl_status, status);
|
|
|
|
}
|
|
|
|
|
2007-10-10 21:33:19 +02:00
|
|
|
/** How many times will we try to fetch a certificate before giving up? */
|
|
|
|
#define MAX_CERT_DL_FAILURES 8
|
|
|
|
|
2007-10-04 18:21:58 +02:00
|
|
|
/** Try to download any v3 authority certificates that we may be missing. If
|
|
|
|
* <b>status</b> is provided, try to get all the ones that were used to sign
|
|
|
|
* <b>status</b>. Additionally, try to have a non-expired certificate for
|
|
|
|
* every V3 authority in trusted_dir_servers. Don't fetch certificates we
|
|
|
|
* already have.
|
|
|
|
**/
|
2007-09-08 21:08:39 +02:00
|
|
|
void
|
2008-02-05 22:39:29 +01:00
|
|
|
authority_certs_fetch_missing(networkstatus_t *status, time_t now)
|
2007-09-08 21:08:39 +02:00
|
|
|
{
|
2007-12-09 18:43:43 +01:00
|
|
|
digestmap_t *pending;
|
|
|
|
smartlist_t *missing_digests;
|
2007-10-10 21:33:19 +02:00
|
|
|
char *resource = NULL;
|
2008-01-27 00:18:30 +01:00
|
|
|
cert_list_t *cl;
|
|
|
|
const int cache = directory_caches_dir_info(get_options());
|
2007-10-09 22:44:45 +02:00
|
|
|
|
2007-10-19 18:41:30 +02:00
|
|
|
if (should_delay_dir_fetches(get_options()))
|
|
|
|
return;
|
|
|
|
|
2007-12-09 18:43:43 +01:00
|
|
|
pending = digestmap_new();
|
|
|
|
missing_digests = smartlist_create();
|
|
|
|
|
2007-10-09 22:44:45 +02:00
|
|
|
list_pending_downloads(pending, DIR_PURPOSE_FETCH_CERTIFICATE, "fp/");
|
2007-09-08 21:08:39 +02:00
|
|
|
if (status) {
|
|
|
|
SMARTLIST_FOREACH(status->voters, networkstatus_voter_info_t *, voter,
|
|
|
|
{
|
2008-01-02 05:57:48 +01:00
|
|
|
if (tor_digest_is_zero(voter->signing_key_digest))
|
|
|
|
continue; /* This authority never signed this consensus, so don't
|
|
|
|
* go looking for a cert with key digest 0000000000. */
|
2008-01-27 00:18:30 +01:00
|
|
|
if (!cache &&
|
|
|
|
!trusteddirserver_get_by_v3_auth_digest(voter->identity_digest))
|
|
|
|
continue; /* We are not a cache, and we don't know this authority.*/
|
|
|
|
cl = get_cert_list(voter->identity_digest);
|
2007-10-10 21:33:19 +02:00
|
|
|
if (authority_cert_get_by_digests(voter->identity_digest,
|
|
|
|
voter->signing_key_digest)) {
|
2008-01-27 00:18:30 +01:00
|
|
|
download_status_reset(&cl->dl_status);
|
2007-10-10 21:33:19 +02:00
|
|
|
continue;
|
|
|
|
}
|
2008-01-27 00:18:30 +01:00
|
|
|
if (download_status_is_ready(&cl->dl_status, now,
|
2007-11-08 17:19:04 +01:00
|
|
|
MAX_CERT_DL_FAILURES)) {
|
2008-01-27 00:18:30 +01:00
|
|
|
log_notice(LD_DIR, "We're missing a certificate from authority "
|
|
|
|
"with signing key %s: launching request.",
|
2007-11-08 17:19:04 +01:00
|
|
|
hex_str(voter->signing_key_digest, DIGEST_LEN));
|
2007-09-08 21:08:39 +02:00
|
|
|
smartlist_add(missing_digests, voter->identity_digest);
|
2007-11-08 17:19:04 +01:00
|
|
|
}
|
2007-09-08 21:08:39 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ds,
|
|
|
|
{
|
|
|
|
int found = 0;
|
|
|
|
if (!(ds->type & V3_AUTHORITY))
|
|
|
|
continue;
|
|
|
|
if (smartlist_digest_isin(missing_digests, ds->v3_identity_digest))
|
|
|
|
continue;
|
2008-01-27 00:18:30 +01:00
|
|
|
cl = get_cert_list(ds->v3_identity_digest);
|
|
|
|
SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert,
|
2007-09-08 21:08:39 +02:00
|
|
|
{
|
2007-10-10 21:33:19 +02:00
|
|
|
if (!ftime_definitely_after(now, cert->expires)) {
|
|
|
|
/* It's not expired, and we weren't looking for something to
|
|
|
|
* verify a consensus with. Call it done. */
|
2008-01-27 00:18:30 +01:00
|
|
|
download_status_reset(&cl->dl_status);
|
2007-09-08 21:08:39 +02:00
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
2008-01-27 00:18:30 +01:00
|
|
|
if (!found && download_status_is_ready(&cl->dl_status, now,
|
2007-11-08 17:19:04 +01:00
|
|
|
MAX_CERT_DL_FAILURES)) {
|
|
|
|
log_notice(LD_DIR, "No current certificate known for authority %s; "
|
|
|
|
"launching request.", ds->nickname);
|
2007-10-10 21:33:19 +02:00
|
|
|
smartlist_add(missing_digests, ds->v3_identity_digest);
|
2007-11-08 17:19:04 +01:00
|
|
|
}
|
2007-09-08 21:08:39 +02:00
|
|
|
});
|
|
|
|
|
2007-10-10 21:33:19 +02:00
|
|
|
if (!smartlist_len(missing_digests)) {
|
|
|
|
goto done;
|
|
|
|
} else {
|
2007-09-08 21:08:39 +02:00
|
|
|
smartlist_t *fps = smartlist_create();
|
2007-10-09 19:40:23 +02:00
|
|
|
smartlist_add(fps, tor_strdup("fp/"));
|
2007-09-08 21:08:39 +02:00
|
|
|
SMARTLIST_FOREACH(missing_digests, const char *, d, {
|
2007-10-09 22:44:45 +02:00
|
|
|
char *fp;
|
|
|
|
if (digestmap_get(pending, d))
|
|
|
|
continue;
|
|
|
|
fp = tor_malloc(HEX_DIGEST_LEN+2);
|
2007-09-08 21:08:39 +02:00
|
|
|
base16_encode(fp, HEX_DIGEST_LEN+1, d, DIGEST_LEN);
|
2007-10-09 19:40:23 +02:00
|
|
|
fp[HEX_DIGEST_LEN] = '+';
|
|
|
|
fp[HEX_DIGEST_LEN+1] = '\0';
|
2007-09-08 21:08:39 +02:00
|
|
|
smartlist_add(fps, fp);
|
|
|
|
});
|
2007-11-04 03:06:09 +01:00
|
|
|
if (smartlist_len(fps) == 1) {
|
|
|
|
/* we didn't add any: they were all pending */
|
|
|
|
SMARTLIST_FOREACH(fps, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(fps);
|
|
|
|
goto done;
|
|
|
|
}
|
2007-10-09 19:40:23 +02:00
|
|
|
resource = smartlist_join_strings(fps, "", 0, NULL);
|
|
|
|
resource[strlen(resource)-1] = '\0';
|
2007-09-08 21:08:39 +02:00
|
|
|
SMARTLIST_FOREACH(fps, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(fps);
|
|
|
|
}
|
|
|
|
directory_get_from_dirserver(DIR_PURPOSE_FETCH_CERTIFICATE, 0,
|
|
|
|
resource, 1);
|
2007-10-10 21:33:19 +02:00
|
|
|
|
|
|
|
done:
|
2007-09-08 21:08:39 +02:00
|
|
|
tor_free(resource);
|
2007-10-10 21:33:19 +02:00
|
|
|
smartlist_free(missing_digests);
|
2007-10-09 22:44:45 +02:00
|
|
|
digestmap_free(pending, NULL);
|
2007-09-08 21:08:39 +02:00
|
|
|
}
|
|
|
|
|
2005-09-13 23:14:55 +02:00
|
|
|
/* Router descriptor storage.
|
|
|
|
*
|
2007-09-27 22:46:24 +02:00
|
|
|
* Routerdescs are stored in a big file, named "cached-descriptors". As new
|
2005-09-13 23:14:55 +02:00
|
|
|
* routerdescs arrive, we append them to a journal file named
|
2007-09-27 22:46:24 +02:00
|
|
|
* "cached-descriptors.new".
|
2005-09-13 23:14:55 +02:00
|
|
|
*
|
2007-09-27 22:46:24 +02:00
|
|
|
* From time to time, we replace "cached-descriptors" with a new file
|
|
|
|
* containing only the live, non-superseded descriptors, and clear
|
|
|
|
* cached-routers.new.
|
2005-09-13 23:14:55 +02:00
|
|
|
*
|
|
|
|
* On startup, we read both files.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** Helper: return 1 iff the router log is so big we want to rebuild the
|
|
|
|
* store. */
|
2005-09-13 08:21:10 +02:00
|
|
|
static int
|
2007-08-31 17:08:37 +02:00
|
|
|
router_should_rebuild_store(desc_store_t *store)
|
|
|
|
{
|
|
|
|
if (store->store_len > (1<<16))
|
|
|
|
return (store->journal_len > store->store_len / 2 ||
|
|
|
|
store->bytes_dropped > store->store_len / 2);
|
|
|
|
else
|
|
|
|
return store->journal_len > (1<<15);
|
|
|
|
}
|
|
|
|
|
|
|
|
static INLINE desc_store_t *
|
|
|
|
desc_get_store(routerlist_t *rl, signed_descriptor_t *sd)
|
2005-09-13 08:21:10 +02:00
|
|
|
{
|
2007-08-31 17:08:37 +02:00
|
|
|
if (sd->is_extrainfo)
|
|
|
|
return &rl->extrainfo_store;
|
2005-09-13 08:21:10 +02:00
|
|
|
else
|
2007-08-31 17:08:37 +02:00
|
|
|
return &rl->desc_store;
|
|
|
|
}
|
|
|
|
|
2007-04-30 21:48:39 +02:00
|
|
|
/** Add the signed_descriptor_t in <b>desc</b> to the router
|
2007-01-22 20:20:33 +01:00
|
|
|
* journal; change its saved_location to SAVED_IN_JOURNAL and set its
|
2007-10-11 18:06:37 +02:00
|
|
|
* offset appropriately. */
|
2005-09-15 07:41:30 +02:00
|
|
|
static int
|
2007-08-31 17:08:37 +02:00
|
|
|
signed_desc_append_to_journal(signed_descriptor_t *desc,
|
2007-10-11 18:06:37 +02:00
|
|
|
desc_store_t *store)
|
2005-09-13 08:21:10 +02:00
|
|
|
{
|
2007-10-17 18:55:44 +02:00
|
|
|
char *fname = get_datadir_fname_suffix(store->fname_base, ".new");
|
2007-09-26 18:19:44 +02:00
|
|
|
const char *body = signed_descriptor_get_body_impl(desc,1);
|
|
|
|
size_t len = desc->signed_descriptor_len + desc->annotations_len;
|
2007-08-31 17:08:37 +02:00
|
|
|
|
2006-01-12 19:04:17 +01:00
|
|
|
tor_assert(len == strlen(body));
|
2005-09-13 08:21:10 +02:00
|
|
|
|
2006-07-10 00:28:12 +02:00
|
|
|
if (append_bytes_to_file(fname, body, len, 1)) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_FS, "Unable to store router descriptor");
|
2005-09-13 08:21:10 +02:00
|
|
|
tor_free(fname);
|
|
|
|
return -1;
|
|
|
|
}
|
2006-04-29 20:42:26 +02:00
|
|
|
desc->saved_location = SAVED_IN_JOURNAL;
|
2005-09-13 08:21:10 +02:00
|
|
|
tor_free(fname);
|
2007-04-30 21:48:39 +02:00
|
|
|
|
2007-08-31 17:08:37 +02:00
|
|
|
desc->saved_offset = store->journal_len;
|
|
|
|
store->journal_len += len;
|
2007-04-30 16:26:38 +02:00
|
|
|
|
2005-09-13 08:21:10 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-16 21:01:02 +01:00
|
|
|
/** Sorting helper: return <0, 0, or >0 depending on whether the
|
|
|
|
* signed_descriptor_t* in *<b>a</b> is older, the same age as, or newer than
|
2007-02-24 08:50:38 +01:00
|
|
|
* the signed_descriptor_t* in *<b>b</b>. */
|
2006-06-22 09:01:54 +02:00
|
|
|
static int
|
2007-04-30 21:48:39 +02:00
|
|
|
_compare_signed_descriptors_by_age(const void **_a, const void **_b)
|
2006-06-22 09:01:54 +02:00
|
|
|
{
|
|
|
|
const signed_descriptor_t *r1 = *_a, *r2 = *_b;
|
|
|
|
return r1->published_on - r2->published_on;
|
|
|
|
}
|
|
|
|
|
2005-09-13 23:14:55 +02:00
|
|
|
/** If the journal is too long, or if <b>force</b> is true, then atomically
|
|
|
|
* replace the router store with the routers currently in our routerlist, and
|
|
|
|
* clear the journal. Return 0 on success, -1 on failure.
|
2007-04-30 21:48:39 +02:00
|
|
|
*
|
2007-05-29 19:31:13 +02:00
|
|
|
* If <b>extrainfo</b> is true, rebuild the extrainfo store; else rebuild the
|
|
|
|
* router descriptor store.
|
2005-09-13 23:14:55 +02:00
|
|
|
*/
|
2005-09-15 07:41:30 +02:00
|
|
|
static int
|
2007-08-31 17:08:37 +02:00
|
|
|
router_rebuild_store(int force, desc_store_t *store)
|
2005-09-13 08:21:10 +02:00
|
|
|
{
|
|
|
|
smartlist_t *chunk_list = NULL;
|
2006-12-07 16:07:33 +01:00
|
|
|
char *fname = NULL, *fname_tmp = NULL;
|
2007-04-30 21:48:39 +02:00
|
|
|
int r = -1;
|
2006-06-27 17:52:51 +02:00
|
|
|
off_t offset = 0;
|
2007-04-30 21:48:39 +02:00
|
|
|
smartlist_t *signed_descriptors = NULL;
|
2007-10-16 01:15:24 +02:00
|
|
|
int nocache=0;
|
2007-10-24 17:45:45 +02:00
|
|
|
size_t total_expected_len = 0;
|
|
|
|
int had_any;
|
2007-04-30 21:48:39 +02:00
|
|
|
|
2007-08-31 17:08:37 +02:00
|
|
|
if (!force && !router_should_rebuild_store(store))
|
2005-09-13 08:21:10 +02:00
|
|
|
return 0;
|
|
|
|
if (!routerlist)
|
|
|
|
return 0;
|
|
|
|
|
2007-10-24 17:45:45 +02:00
|
|
|
if (store->type == EXTRAINFO_STORE)
|
|
|
|
had_any = !eimap_isempty(routerlist->extra_info_map);
|
|
|
|
else
|
|
|
|
had_any = (smartlist_len(routerlist->routers)+
|
|
|
|
smartlist_len(routerlist->old_routers))>0;
|
2007-04-30 16:26:38 +02:00
|
|
|
|
2005-09-15 07:19:38 +02:00
|
|
|
/* Don't save deadweight. */
|
2005-11-01 18:34:17 +01:00
|
|
|
routerlist_remove_old_routers();
|
2005-09-15 07:19:38 +02:00
|
|
|
|
2007-08-31 17:08:37 +02:00
|
|
|
log_info(LD_DIR, "Rebuilding %s cache", store->description);
|
2006-06-27 17:52:51 +02:00
|
|
|
|
2007-10-17 18:55:44 +02:00
|
|
|
fname = get_datadir_fname(store->fname_base);
|
|
|
|
fname_tmp = get_datadir_fname_suffix(store->fname_base, ".tmp");
|
2006-12-07 16:07:33 +01:00
|
|
|
|
2005-09-13 08:21:10 +02:00
|
|
|
chunk_list = smartlist_create();
|
|
|
|
|
2007-03-21 16:37:30 +01:00
|
|
|
/* We sort the routers by age to enhance locality on disk. */
|
2007-04-30 21:48:39 +02:00
|
|
|
signed_descriptors = smartlist_create();
|
2007-08-31 17:08:37 +02:00
|
|
|
if (store->type == EXTRAINFO_STORE) {
|
2007-06-12 00:19:40 +02:00
|
|
|
eimap_iter_t *iter;
|
|
|
|
for (iter = eimap_iter_init(routerlist->extra_info_map);
|
|
|
|
!eimap_iter_done(iter);
|
|
|
|
iter = eimap_iter_next(routerlist->extra_info_map, iter)) {
|
2007-04-30 21:48:39 +02:00
|
|
|
const char *key;
|
|
|
|
extrainfo_t *ei;
|
2007-06-12 00:19:40 +02:00
|
|
|
eimap_iter_get(iter, &key, &ei);
|
2007-04-30 21:48:39 +02:00
|
|
|
smartlist_add(signed_descriptors, &ei->cache_info);
|
|
|
|
}
|
|
|
|
} else {
|
2007-08-31 17:08:37 +02:00
|
|
|
SMARTLIST_FOREACH(routerlist->old_routers, signed_descriptor_t *, sd,
|
2007-10-16 01:15:24 +02:00
|
|
|
smartlist_add(signed_descriptors, sd));
|
2007-04-30 21:48:39 +02:00
|
|
|
SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, ri,
|
2007-10-16 01:15:24 +02:00
|
|
|
smartlist_add(signed_descriptors, &ri->cache_info));
|
2007-04-30 21:48:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
smartlist_sort(signed_descriptors, _compare_signed_descriptors_by_age);
|
|
|
|
|
|
|
|
/* Now, add the appropriate members to chunk_list */
|
|
|
|
SMARTLIST_FOREACH(signed_descriptors, signed_descriptor_t *, sd,
|
2005-10-27 02:34:39 +02:00
|
|
|
{
|
|
|
|
sized_chunk_t *c;
|
2007-09-26 18:19:44 +02:00
|
|
|
const char *body = signed_descriptor_get_body_impl(sd, 1);
|
2006-01-12 19:04:17 +01:00
|
|
|
if (!body) {
|
2007-03-04 21:11:46 +01:00
|
|
|
log_warn(LD_BUG, "No descriptor available for router.");
|
2005-10-27 02:34:39 +02:00
|
|
|
goto done;
|
|
|
|
}
|
2007-10-16 01:15:24 +02:00
|
|
|
if (sd->do_not_cache) {
|
|
|
|
++nocache;
|
2007-03-21 16:37:30 +01:00
|
|
|
continue;
|
2007-10-16 01:15:24 +02:00
|
|
|
}
|
2005-10-27 02:34:39 +02:00
|
|
|
c = tor_malloc(sizeof(sized_chunk_t));
|
2006-01-12 19:04:17 +01:00
|
|
|
c->bytes = body;
|
2007-09-26 18:19:44 +02:00
|
|
|
c->len = sd->signed_descriptor_len + sd->annotations_len;
|
2007-10-24 17:45:45 +02:00
|
|
|
total_expected_len += c->len;
|
2005-10-27 02:34:39 +02:00
|
|
|
smartlist_add(chunk_list, c);
|
|
|
|
});
|
2007-04-30 21:48:39 +02:00
|
|
|
|
2006-12-07 16:07:33 +01:00
|
|
|
if (write_chunks_to_file(fname_tmp, chunk_list, 1)<0) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_FS, "Error writing router store to disk.");
|
2005-09-13 08:21:10 +02:00
|
|
|
goto done;
|
|
|
|
}
|
2007-04-30 21:48:39 +02:00
|
|
|
|
2006-06-22 09:01:54 +02:00
|
|
|
/* Our mmap is now invalid. */
|
2007-08-31 17:08:37 +02:00
|
|
|
if (store->mmap) {
|
|
|
|
tor_munmap_file(store->mmap);
|
|
|
|
store->mmap = NULL;
|
2006-06-22 09:01:54 +02:00
|
|
|
}
|
2007-04-30 21:48:39 +02:00
|
|
|
|
2006-12-07 16:07:33 +01:00
|
|
|
if (replace_file(fname_tmp, fname)<0) {
|
|
|
|
log_warn(LD_FS, "Error replacing old router store.");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2007-10-24 17:45:45 +02:00
|
|
|
errno = 0;
|
2007-08-31 17:08:37 +02:00
|
|
|
store->mmap = tor_mmap_file(fname);
|
2007-10-16 01:15:24 +02:00
|
|
|
if (! store->mmap) {
|
2007-10-24 17:45:45 +02:00
|
|
|
if (errno == ERANGE) {
|
|
|
|
/* empty store.*/
|
|
|
|
if (total_expected_len) {
|
|
|
|
log_warn(LD_FS, "We wrote some bytes to a new descriptor file at '%s',"
|
|
|
|
" but when we went to mmap it, it was empty!", fname);
|
|
|
|
} else if (had_any) {
|
2008-02-06 00:20:44 +01:00
|
|
|
log_info(LD_FS, "We just removed every descriptor in '%s'. This is "
|
|
|
|
"okay if we're just starting up after a long time. "
|
|
|
|
"Otherwise, it's a bug.", fname);
|
2007-10-24 17:45:45 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log_warn(LD_FS, "Unable to mmap new descriptor file at '%s'.",fname);
|
|
|
|
}
|
2007-10-16 01:15:24 +02:00
|
|
|
}
|
2006-06-22 09:01:54 +02:00
|
|
|
|
2007-05-20 07:15:53 +02:00
|
|
|
log_info(LD_DIR, "Reconstructing pointers into cache");
|
|
|
|
|
2006-06-27 17:52:51 +02:00
|
|
|
offset = 0;
|
2007-04-30 21:48:39 +02:00
|
|
|
SMARTLIST_FOREACH(signed_descriptors, signed_descriptor_t *, sd,
|
2006-04-29 20:42:26 +02:00
|
|
|
{
|
2007-04-30 21:48:39 +02:00
|
|
|
if (sd->do_not_cache)
|
2007-03-21 16:37:30 +01:00
|
|
|
continue;
|
2006-04-29 20:42:26 +02:00
|
|
|
sd->saved_location = SAVED_IN_CACHE;
|
2007-08-31 17:08:37 +02:00
|
|
|
if (store->mmap) {
|
2006-06-27 17:52:51 +02:00
|
|
|
tor_free(sd->signed_descriptor_body); // sets it to null
|
2006-06-28 17:39:02 +02:00
|
|
|
sd->saved_offset = offset;
|
2006-06-27 17:52:51 +02:00
|
|
|
}
|
2007-09-27 22:46:30 +02:00
|
|
|
offset += sd->signed_descriptor_len + sd->annotations_len;
|
2007-04-30 21:48:39 +02:00
|
|
|
signed_descriptor_get_body(sd); /* reconstruct and assert */
|
2006-04-29 20:42:26 +02:00
|
|
|
});
|
2005-09-13 08:21:10 +02:00
|
|
|
|
2007-10-17 18:55:44 +02:00
|
|
|
tor_free(fname);
|
|
|
|
fname = get_datadir_fname_suffix(store->fname_base, ".new");
|
2006-07-10 00:28:12 +02:00
|
|
|
write_str_to_file(fname, "", 1);
|
2005-09-13 08:21:10 +02:00
|
|
|
|
|
|
|
r = 0;
|
2007-08-31 17:08:37 +02:00
|
|
|
store->store_len = (size_t) offset;
|
|
|
|
store->journal_len = 0;
|
|
|
|
store->bytes_dropped = 0;
|
2005-09-13 08:21:10 +02:00
|
|
|
done:
|
2007-04-30 21:48:39 +02:00
|
|
|
if (signed_descriptors)
|
|
|
|
smartlist_free(signed_descriptors);
|
2005-09-13 08:21:10 +02:00
|
|
|
tor_free(fname);
|
2007-07-15 10:34:32 +02:00
|
|
|
tor_free(fname_tmp);
|
2006-10-22 10:08:10 +02:00
|
|
|
SMARTLIST_FOREACH(chunk_list, sized_chunk_t *, c, tor_free(c));
|
|
|
|
smartlist_free(chunk_list);
|
2007-04-30 16:26:38 +02:00
|
|
|
|
2005-09-13 08:21:10 +02:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/** Helper: Reload a cache file and its associated journal, setting metadata
|
|
|
|
* appropriately. If <b>extrainfo</b> is true, reload the extrainfo store;
|
|
|
|
* else reload the router descriptor store. */
|
2007-04-30 21:48:39 +02:00
|
|
|
static int
|
2007-08-31 17:08:37 +02:00
|
|
|
router_reload_router_list_impl(desc_store_t *store)
|
2005-09-15 07:19:38 +02:00
|
|
|
{
|
2007-10-17 18:55:44 +02:00
|
|
|
char *fname = NULL, *altname = NULL, *contents = NULL;
|
2007-05-22 04:20:52 +02:00
|
|
|
struct stat st;
|
2007-09-27 22:46:24 +02:00
|
|
|
int read_from_old_location = 0;
|
2007-08-31 17:08:37 +02:00
|
|
|
int extrainfo = (store->type == EXTRAINFO_STORE);
|
2007-10-14 02:13:06 +02:00
|
|
|
time_t now = time(NULL);
|
2007-08-31 17:08:37 +02:00
|
|
|
store->journal_len = store->store_len = 0;
|
2006-08-27 04:07:54 +02:00
|
|
|
|
2007-10-17 18:55:44 +02:00
|
|
|
fname = get_datadir_fname(store->fname_base);
|
|
|
|
if (store->fname_alt_base)
|
|
|
|
altname = get_datadir_fname(store->fname_alt_base);
|
2006-08-27 04:07:54 +02:00
|
|
|
|
2007-08-31 17:08:37 +02:00
|
|
|
if (store->mmap) /* get rid of it first */
|
|
|
|
tor_munmap_file(store->mmap);
|
|
|
|
store->mmap = NULL;
|
2007-04-30 21:48:39 +02:00
|
|
|
|
2007-08-31 17:08:37 +02:00
|
|
|
store->mmap = tor_mmap_file(fname);
|
2007-09-27 22:46:24 +02:00
|
|
|
if (!store->mmap && altname && file_status(altname) == FN_FILE) {
|
|
|
|
read_from_old_location = 1;
|
|
|
|
log_notice(LD_DIR, "Couldn't read %s; trying to load routers from old "
|
|
|
|
"location %s.", fname, altname);
|
|
|
|
if ((store->mmap = tor_mmap_file(altname)))
|
|
|
|
read_from_old_location = 1;
|
|
|
|
}
|
2007-10-14 02:13:06 +02:00
|
|
|
if (altname && !read_from_old_location) {
|
|
|
|
remove_file_if_very_old(altname, now);
|
|
|
|
}
|
2007-08-31 17:08:37 +02:00
|
|
|
if (store->mmap) {
|
|
|
|
store->store_len = store->mmap->size;
|
2007-04-30 21:48:39 +02:00
|
|
|
if (extrainfo)
|
2007-08-31 17:08:37 +02:00
|
|
|
router_load_extrainfo_from_string(store->mmap->data,
|
|
|
|
store->mmap->data+store->mmap->size,
|
2007-09-21 08:14:36 +02:00
|
|
|
SAVED_IN_CACHE, NULL, 0);
|
2007-04-30 21:48:39 +02:00
|
|
|
else
|
2007-08-31 17:08:37 +02:00
|
|
|
router_load_routers_from_string(store->mmap->data,
|
|
|
|
store->mmap->data+store->mmap->size,
|
2007-09-27 22:46:30 +02:00
|
|
|
SAVED_IN_CACHE, NULL, 0, NULL);
|
2005-09-15 07:19:38 +02:00
|
|
|
}
|
2006-06-22 09:01:54 +02:00
|
|
|
|
2007-10-17 18:55:44 +02:00
|
|
|
tor_free(fname);
|
|
|
|
fname = get_datadir_fname_suffix(store->fname_base, ".new");
|
2007-02-12 22:39:44 +01:00
|
|
|
if (file_status(fname) == FN_FILE)
|
2007-05-22 04:20:52 +02:00
|
|
|
contents = read_file_to_str(fname, RFTS_BIN|RFTS_IGNORE_MISSING, &st);
|
2007-10-14 02:13:06 +02:00
|
|
|
if (read_from_old_location) {
|
2007-10-17 18:55:44 +02:00
|
|
|
tor_free(altname);
|
|
|
|
altname = get_datadir_fname_suffix(store->fname_alt_base, ".new");
|
2007-10-14 02:13:06 +02:00
|
|
|
if (!contents)
|
|
|
|
contents = read_file_to_str(altname, RFTS_BIN|RFTS_IGNORE_MISSING, &st);
|
|
|
|
else
|
|
|
|
remove_file_if_very_old(altname, now);
|
2007-09-27 22:46:24 +02:00
|
|
|
}
|
2006-06-22 09:01:54 +02:00
|
|
|
if (contents) {
|
2007-04-30 21:48:39 +02:00
|
|
|
if (extrainfo)
|
2007-09-21 08:14:36 +02:00
|
|
|
router_load_extrainfo_from_string(contents, NULL,SAVED_IN_JOURNAL,
|
|
|
|
NULL, 0);
|
2007-04-30 21:48:39 +02:00
|
|
|
else
|
2007-09-21 08:14:36 +02:00
|
|
|
router_load_routers_from_string(contents, NULL, SAVED_IN_JOURNAL,
|
2007-09-27 22:46:30 +02:00
|
|
|
NULL, 0, NULL);
|
2007-08-31 17:08:37 +02:00
|
|
|
store->journal_len = (size_t) st.st_size;
|
2006-08-27 04:12:12 +02:00
|
|
|
tor_free(contents);
|
2006-06-22 09:01:54 +02:00
|
|
|
}
|
|
|
|
|
2005-09-30 23:11:22 +02:00
|
|
|
tor_free(fname);
|
2007-09-27 22:46:24 +02:00
|
|
|
tor_free(altname);
|
2005-09-15 07:19:38 +02:00
|
|
|
|
2007-09-27 22:46:24 +02:00
|
|
|
if (store->journal_len || read_from_old_location) {
|
2005-09-15 07:19:38 +02:00
|
|
|
/* Always clear the journal on startup.*/
|
2007-08-31 17:08:37 +02:00
|
|
|
router_rebuild_store(1, store);
|
2007-04-30 21:48:39 +02:00
|
|
|
} else if (!extrainfo) {
|
2006-01-10 05:57:12 +01:00
|
|
|
/* Don't cache expired routers. (This is in an else because
|
|
|
|
* router_rebuild_store() also calls remove_old_routers().) */
|
|
|
|
routerlist_remove_old_routers();
|
2005-09-15 07:19:38 +02:00
|
|
|
}
|
2006-01-10 05:57:12 +01:00
|
|
|
|
2005-09-15 07:19:38 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-04-30 21:48:39 +02:00
|
|
|
/** Load all cached router descriptors and extra-info documents from the
|
|
|
|
* store. Return 0 on success and -1 on failure.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
router_reload_router_list(void)
|
|
|
|
{
|
2007-08-31 17:08:37 +02:00
|
|
|
routerlist_t *rl = router_get_routerlist();
|
|
|
|
if (router_reload_router_list_impl(&rl->desc_store))
|
2007-07-28 02:11:34 +02:00
|
|
|
return -1;
|
2007-08-31 17:08:37 +02:00
|
|
|
if (router_reload_router_list_impl(&rl->extrainfo_store))
|
2007-07-28 02:11:34 +02:00
|
|
|
return -1;
|
2007-04-30 21:48:39 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-04-10 03:35:56 +02:00
|
|
|
/** Return a smartlist containing a list of trusted_dir_server_t * for all
|
|
|
|
* known trusted dirservers. Callers must not modify the list or its
|
|
|
|
* contents.
|
2004-10-15 21:04:38 +02:00
|
|
|
*/
|
2006-04-10 03:35:56 +02:00
|
|
|
smartlist_t *
|
|
|
|
router_get_trusted_dir_servers(void)
|
2004-10-15 21:04:38 +02:00
|
|
|
{
|
|
|
|
if (!trusted_dir_servers)
|
|
|
|
trusted_dir_servers = smartlist_create();
|
|
|
|
|
2006-04-10 03:35:56 +02:00
|
|
|
return trusted_dir_servers;
|
2004-10-15 21:04:38 +02:00
|
|
|
}
|
|
|
|
|
2007-10-24 20:42:16 +02:00
|
|
|
/** Try to find a running dirserver that supports operations of <b>type</b>.
|
2007-10-24 17:45:45 +02:00
|
|
|
*
|
2007-10-24 20:42:16 +02:00
|
|
|
* If there are no running dirservers in our routerlist and the
|
|
|
|
* <b>PDS_RETRY_IF_NO_SERVERS</b> flag is set, set all the authoritative ones
|
|
|
|
* as running again, and pick one.
|
|
|
|
*
|
|
|
|
* If the <b>PDS_IGNORE_FASCISTFIREWALL</b> flag is set, then include
|
|
|
|
* dirservers that we can't reach.
|
|
|
|
*
|
|
|
|
* If the <b>PDS_ALLOW_SELF</b> flag is not set, then don't include ourself
|
2007-10-25 05:03:52 +02:00
|
|
|
* (if we're a dirserver).
|
2007-10-24 20:42:16 +02:00
|
|
|
*
|
|
|
|
* Don't pick an authority if any non-authority is viable; try to avoid using
|
|
|
|
* servers that have returned 503 recently.
|
2004-11-29 22:01:34 +01:00
|
|
|
*/
|
2005-12-14 23:00:58 +01:00
|
|
|
routerstatus_t *
|
2007-10-24 20:42:16 +02:00
|
|
|
router_pick_directory_server(authority_type_t type, int flags)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2005-12-14 23:00:58 +01:00
|
|
|
routerstatus_t *choice;
|
2007-10-24 20:42:16 +02:00
|
|
|
if (get_options()->PreferTunneledDirConns)
|
|
|
|
flags |= _PDS_PREFER_TUNNELED_DIR_CONNS;
|
2004-02-26 23:56:36 +01:00
|
|
|
|
2004-10-14 03:44:32 +02:00
|
|
|
if (!routerlist)
|
2004-10-14 04:29:03 +02:00
|
|
|
return NULL;
|
2004-10-14 03:44:32 +02:00
|
|
|
|
2007-10-24 20:42:16 +02:00
|
|
|
choice = router_pick_directory_server_impl(type, flags);
|
|
|
|
if (choice || !(flags & PDS_RETRY_IF_NO_SERVERS))
|
2004-07-20 08:44:16 +02:00
|
|
|
return choice;
|
|
|
|
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_DIR,
|
|
|
|
"No reachable router entries for dirservers. "
|
|
|
|
"Trying them all again.");
|
2004-08-18 23:13:58 +02:00
|
|
|
/* mark all authdirservers as up again */
|
2004-10-12 17:55:20 +02:00
|
|
|
mark_all_trusteddirservers_up();
|
2004-07-20 08:44:16 +02:00
|
|
|
/* try again */
|
2007-10-24 20:42:16 +02:00
|
|
|
choice = router_pick_directory_server_impl(type, flags);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (choice)
|
2004-07-20 08:44:16 +02:00
|
|
|
return choice;
|
|
|
|
|
2007-10-24 20:42:16 +02:00
|
|
|
/* XXXX020 what's the point of *reloading* and trying again?? -NM */
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_DIR,"Still no %s router entries. Reloading and trying again.",
|
2007-10-24 20:42:16 +02:00
|
|
|
(flags & PDS_IGNORE_FASCISTFIREWALL) ? "known" : "reachable");
|
2004-11-28 10:05:49 +01:00
|
|
|
if (router_reload_router_list()) {
|
2004-09-08 08:52:33 +02:00
|
|
|
return NULL;
|
2004-02-26 23:56:36 +01:00
|
|
|
}
|
2004-07-20 08:44:16 +02:00
|
|
|
/* give it one last try */
|
2007-10-24 20:42:16 +02:00
|
|
|
choice = router_pick_directory_server_impl(type, flags);
|
2004-02-26 23:56:36 +01:00
|
|
|
return choice;
|
|
|
|
}
|
|
|
|
|
2005-12-14 23:00:58 +01:00
|
|
|
/** Return the trusted_dir_server_t for the directory authority whose identity
|
|
|
|
* key hashes to <b>digest</b>, or NULL if no such authority is known.
|
|
|
|
*/
|
2005-09-08 08:22:44 +02:00
|
|
|
trusted_dir_server_t *
|
|
|
|
router_get_trusteddirserver_by_digest(const char *digest)
|
|
|
|
{
|
|
|
|
if (!trusted_dir_servers)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ds,
|
|
|
|
{
|
|
|
|
if (!memcmp(ds->digest, digest, DIGEST_LEN))
|
|
|
|
return ds;
|
|
|
|
});
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-05-22 19:58:25 +02:00
|
|
|
/** Return the trusted_dir_server_t for the directory authority whose identity
|
|
|
|
* key hashes to <b>digest</b>, or NULL if no such authority is known.
|
|
|
|
*/
|
|
|
|
trusted_dir_server_t *
|
|
|
|
trusteddirserver_get_by_v3_auth_digest(const char *digest)
|
|
|
|
{
|
|
|
|
if (!trusted_dir_servers)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ds,
|
|
|
|
{
|
2007-10-26 16:57:26 +02:00
|
|
|
if (!memcmp(ds->v3_identity_digest, digest, DIGEST_LEN) &&
|
|
|
|
(ds->type & V3_AUTHORITY))
|
2007-05-22 19:58:25 +02:00
|
|
|
return ds;
|
|
|
|
});
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-10-24 20:42:16 +02:00
|
|
|
/** Try to find a running trusted dirserver. Flags are as for
|
|
|
|
* router_pick_directory_server.
|
2005-06-21 01:04:13 +02:00
|
|
|
*/
|
2005-12-14 23:00:58 +01:00
|
|
|
routerstatus_t *
|
2007-10-24 20:42:16 +02:00
|
|
|
router_pick_trusteddirserver(authority_type_t type, int flags)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2005-12-14 23:00:58 +01:00
|
|
|
routerstatus_t *choice;
|
2007-10-24 20:42:16 +02:00
|
|
|
if (get_options()->PreferTunneledDirConns)
|
|
|
|
flags |= _PDS_PREFER_TUNNELED_DIR_CONNS;
|
2004-10-12 17:55:20 +02:00
|
|
|
|
2007-10-24 20:42:16 +02:00
|
|
|
choice = router_pick_trusteddirserver_impl(type, flags);
|
|
|
|
if (choice || !(flags & PDS_RETRY_IF_NO_SERVERS))
|
2004-10-12 17:55:20 +02:00
|
|
|
return choice;
|
|
|
|
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_DIR,
|
|
|
|
"No trusted dirservers are reachable. Trying them all again.");
|
2004-10-12 17:55:20 +02:00
|
|
|
mark_all_trusteddirservers_up();
|
2007-10-24 20:42:16 +02:00
|
|
|
return router_pick_trusteddirserver_impl(type, flags);
|
2004-10-12 17:55:20 +02:00
|
|
|
}
|
|
|
|
|
2006-12-24 03:45:35 +01:00
|
|
|
/** How long do we avoid using a directory server after it's given us a 503? */
|
|
|
|
#define DIR_503_TIMEOUT (60*60)
|
|
|
|
|
2006-03-19 02:21:59 +01:00
|
|
|
/** Pick a random running valid directory server/mirror from our
|
2007-10-24 20:42:16 +02:00
|
|
|
* routerlist. Arguments are as for router_pick_directory_server(), except
|
|
|
|
* that RETRY_IF_NO_SERVERS is ignored, and:
|
2007-05-02 11:12:04 +02:00
|
|
|
*
|
2007-10-24 20:42:16 +02:00
|
|
|
* If the _PDS_PREFER_TUNNELED_DIR_CONNS flag is set, prefer directory servers
|
|
|
|
* that we can use with BEGINDIR.
|
2004-07-20 08:44:16 +02:00
|
|
|
*/
|
2005-12-14 23:00:58 +01:00
|
|
|
static routerstatus_t *
|
2007-10-24 20:42:16 +02:00
|
|
|
router_pick_directory_server_impl(authority_type_t type, int flags)
|
2004-07-20 08:44:16 +02:00
|
|
|
{
|
2005-12-14 23:00:58 +01:00
|
|
|
routerstatus_t *result;
|
2007-01-03 07:33:03 +01:00
|
|
|
smartlist_t *direct, *tunnel;
|
|
|
|
smartlist_t *trusted_direct, *trusted_tunnel;
|
|
|
|
smartlist_t *overloaded_direct, *overloaded_tunnel;
|
2006-12-24 03:45:35 +01:00
|
|
|
time_t now = time(NULL);
|
2008-02-05 22:39:29 +01:00
|
|
|
const networkstatus_t *consensus = networkstatus_get_latest_consensus();
|
2007-10-24 20:42:16 +02:00
|
|
|
int requireother = ! (flags & PDS_ALLOW_SELF);
|
|
|
|
int fascistfirewall = ! (flags & PDS_IGNORE_FASCISTFIREWALL);
|
|
|
|
int prefer_tunnel = (flags & _PDS_PREFER_TUNNELED_DIR_CONNS);
|
2007-10-16 01:15:24 +02:00
|
|
|
|
|
|
|
if (!consensus)
|
|
|
|
return NULL;
|
2002-09-26 14:09:10 +02:00
|
|
|
|
2007-01-03 07:33:03 +01:00
|
|
|
direct = smartlist_create();
|
|
|
|
tunnel = smartlist_create();
|
|
|
|
trusted_direct = smartlist_create();
|
|
|
|
trusted_tunnel = smartlist_create();
|
|
|
|
overloaded_direct = smartlist_create();
|
|
|
|
overloaded_tunnel = smartlist_create();
|
|
|
|
|
2004-05-04 20:17:45 +02:00
|
|
|
/* Find all the running dirservers we know about. */
|
2007-10-16 01:15:24 +02:00
|
|
|
SMARTLIST_FOREACH(consensus->routerstatus_list, routerstatus_t *, status,
|
2005-09-13 17:32:03 +02:00
|
|
|
{
|
2006-01-10 00:39:07 +01:00
|
|
|
int is_trusted;
|
2007-10-09 17:27:15 +02:00
|
|
|
int is_overloaded = status->last_dir_503_at + DIR_503_TIMEOUT > now;
|
2005-12-14 23:00:58 +01:00
|
|
|
if (!status->is_running || !status->dir_port || !status->is_valid)
|
2004-07-20 08:44:16 +02:00
|
|
|
continue;
|
2007-01-03 20:58:00 +01:00
|
|
|
if (status->is_bad_directory)
|
|
|
|
continue;
|
2005-12-14 23:00:58 +01:00
|
|
|
if (requireother && router_digest_is_me(status->identity_digest))
|
2004-07-20 08:44:16 +02:00
|
|
|
continue;
|
2007-10-08 19:44:19 +02:00
|
|
|
if (type & V3_AUTHORITY) {
|
|
|
|
if (!(status->version_supports_v3_dir ||
|
|
|
|
router_digest_is_trusted_dir_type(status->identity_digest,
|
|
|
|
V3_AUTHORITY)))
|
|
|
|
continue;
|
|
|
|
}
|
2006-01-10 00:39:07 +01:00
|
|
|
is_trusted = router_digest_is_trusted_dir(status->identity_digest);
|
2007-05-10 06:01:28 +02:00
|
|
|
if ((type & V2_AUTHORITY) && !(status->is_v2_dir || is_trusted))
|
2004-11-29 22:01:34 +01:00
|
|
|
continue;
|
2007-05-18 23:19:19 +02:00
|
|
|
if ((type & EXTRAINFO_CACHE) &&
|
|
|
|
!router_supports_extrainfo(status->identity_digest, 0))
|
|
|
|
continue;
|
2007-04-27 12:26:09 +02:00
|
|
|
if (prefer_tunnel &&
|
2007-01-03 07:33:03 +01:00
|
|
|
status->version_supports_begindir &&
|
2007-04-27 12:26:09 +02:00
|
|
|
(!fascistfirewall ||
|
|
|
|
fascist_firewall_allows_address_or(status->addr, status->or_port)))
|
2007-01-03 07:33:03 +01:00
|
|
|
smartlist_add(is_trusted ? trusted_tunnel :
|
|
|
|
is_overloaded ? overloaded_tunnel : tunnel, status);
|
2007-04-27 12:26:09 +02:00
|
|
|
else if (!fascistfirewall ||
|
2007-01-03 07:33:03 +01:00
|
|
|
fascist_firewall_allows_address_dir(status->addr,
|
2007-04-27 12:26:09 +02:00
|
|
|
status->dir_port))
|
2007-01-03 07:33:03 +01:00
|
|
|
smartlist_add(is_trusted ? trusted_direct :
|
|
|
|
is_overloaded ? overloaded_direct : direct, status);
|
2005-09-13 17:32:03 +02:00
|
|
|
});
|
2002-09-26 14:09:10 +02:00
|
|
|
|
2007-01-03 07:33:03 +01:00
|
|
|
if (smartlist_len(tunnel)) {
|
2007-01-27 10:13:19 +01:00
|
|
|
result = routerstatus_sl_choose_by_bandwidth(tunnel);
|
2007-01-03 07:33:03 +01:00
|
|
|
} else if (smartlist_len(overloaded_tunnel)) {
|
2007-01-27 10:13:19 +01:00
|
|
|
result = routerstatus_sl_choose_by_bandwidth(overloaded_tunnel);
|
2007-01-04 10:12:23 +01:00
|
|
|
} else if (smartlist_len(trusted_tunnel)) {
|
2007-01-03 07:33:03 +01:00
|
|
|
/* FFFF We don't distinguish between trusteds and overloaded trusteds
|
|
|
|
* yet. Maybe one day we should. */
|
2007-01-27 10:13:19 +01:00
|
|
|
/* FFFF We also don't load balance over authorities yet. I think this
|
|
|
|
* is a feature, but it could easily be a bug. -RD */
|
2007-01-03 07:33:03 +01:00
|
|
|
result = smartlist_choose(trusted_tunnel);
|
|
|
|
} else if (smartlist_len(direct)) {
|
2007-01-27 10:13:19 +01:00
|
|
|
result = routerstatus_sl_choose_by_bandwidth(direct);
|
2007-01-03 07:33:03 +01:00
|
|
|
} else if (smartlist_len(overloaded_direct)) {
|
2007-01-27 10:13:19 +01:00
|
|
|
result = routerstatus_sl_choose_by_bandwidth(overloaded_direct);
|
2007-01-03 07:33:03 +01:00
|
|
|
} else {
|
|
|
|
result = smartlist_choose(trusted_direct);
|
|
|
|
}
|
|
|
|
smartlist_free(direct);
|
|
|
|
smartlist_free(tunnel);
|
|
|
|
smartlist_free(trusted_direct);
|
|
|
|
smartlist_free(trusted_tunnel);
|
|
|
|
smartlist_free(overloaded_direct);
|
|
|
|
smartlist_free(overloaded_tunnel);
|
2005-09-13 17:32:03 +02:00
|
|
|
return result;
|
2004-07-20 08:44:16 +02:00
|
|
|
}
|
2003-11-10 09:06:55 +01:00
|
|
|
|
2007-10-24 20:42:16 +02:00
|
|
|
/** Choose randomly from among the trusted dirservers that are up. Flags
|
|
|
|
* are as for router_pick_directory_server_impl().
|
2005-06-21 01:04:13 +02:00
|
|
|
*/
|
2005-12-14 23:00:58 +01:00
|
|
|
static routerstatus_t *
|
2007-10-24 20:42:16 +02:00
|
|
|
router_pick_trusteddirserver_impl(authority_type_t type, int flags)
|
2004-10-12 17:55:20 +02:00
|
|
|
{
|
2007-01-03 07:33:03 +01:00
|
|
|
smartlist_t *direct, *tunnel;
|
|
|
|
smartlist_t *overloaded_direct, *overloaded_tunnel;
|
|
|
|
routerinfo_t *me = router_get_my_routerinfo();
|
|
|
|
routerstatus_t *result;
|
2006-12-24 03:45:35 +01:00
|
|
|
time_t now = time(NULL);
|
2007-10-24 20:42:16 +02:00
|
|
|
int requireother = ! (flags & PDS_ALLOW_SELF);
|
|
|
|
int fascistfirewall = ! (flags & PDS_IGNORE_FASCISTFIREWALL);
|
|
|
|
int prefer_tunnel = (flags & _PDS_PREFER_TUNNELED_DIR_CONNS);
|
2007-01-03 07:33:03 +01:00
|
|
|
|
2008-01-16 06:27:19 +01:00
|
|
|
if (!trusted_dir_servers)
|
|
|
|
return NULL;
|
|
|
|
|
2007-01-03 07:33:03 +01:00
|
|
|
direct = smartlist_create();
|
|
|
|
tunnel = smartlist_create();
|
|
|
|
overloaded_direct = smartlist_create();
|
|
|
|
overloaded_tunnel = smartlist_create();
|
2004-07-20 08:44:16 +02:00
|
|
|
|
2004-10-12 17:55:20 +02:00
|
|
|
SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, d,
|
|
|
|
{
|
2007-01-03 07:33:03 +01:00
|
|
|
int is_overloaded =
|
|
|
|
d->fake_status.last_dir_503_at + DIR_503_TIMEOUT > now;
|
2004-10-12 17:55:20 +02:00
|
|
|
if (!d->is_running) continue;
|
2007-05-09 06:15:46 +02:00
|
|
|
if ((type & d->type) == 0)
|
2005-09-07 18:42:53 +02:00
|
|
|
continue;
|
2007-05-18 23:19:19 +02:00
|
|
|
if ((type & EXTRAINFO_CACHE) &&
|
|
|
|
!router_supports_extrainfo(d->digest, 1))
|
|
|
|
continue;
|
2005-11-05 21:15:27 +01:00
|
|
|
if (requireother && me && router_digest_is_me(d->digest))
|
|
|
|
continue;
|
2007-04-27 12:26:09 +02:00
|
|
|
if (prefer_tunnel &&
|
2007-01-03 07:33:03 +01:00
|
|
|
d->or_port &&
|
2007-04-27 12:26:09 +02:00
|
|
|
(!fascistfirewall ||
|
|
|
|
fascist_firewall_allows_address_or(d->addr, d->or_port)))
|
2007-01-03 07:33:03 +01:00
|
|
|
smartlist_add(is_overloaded ? overloaded_tunnel : tunnel,
|
2007-10-09 17:27:15 +02:00
|
|
|
&d->fake_status);
|
2007-04-27 12:26:09 +02:00
|
|
|
else if (!fascistfirewall ||
|
|
|
|
fascist_firewall_allows_address_dir(d->addr, d->dir_port))
|
2007-01-03 07:33:03 +01:00
|
|
|
smartlist_add(is_overloaded ? overloaded_direct : direct,
|
2007-10-09 17:27:15 +02:00
|
|
|
&d->fake_status);
|
2004-10-12 17:55:20 +02:00
|
|
|
});
|
2004-05-04 20:17:45 +02:00
|
|
|
|
2007-01-03 07:33:03 +01:00
|
|
|
if (smartlist_len(tunnel)) {
|
|
|
|
result = smartlist_choose(tunnel);
|
|
|
|
} else if (smartlist_len(overloaded_tunnel)) {
|
|
|
|
result = smartlist_choose(overloaded_tunnel);
|
|
|
|
} else if (smartlist_len(direct)) {
|
|
|
|
result = smartlist_choose(direct);
|
|
|
|
} else {
|
|
|
|
result = smartlist_choose(overloaded_direct);
|
|
|
|
}
|
|
|
|
|
|
|
|
smartlist_free(direct);
|
|
|
|
smartlist_free(tunnel);
|
|
|
|
smartlist_free(overloaded_direct);
|
|
|
|
smartlist_free(overloaded_tunnel);
|
|
|
|
return result;
|
2004-10-12 17:55:20 +02:00
|
|
|
}
|
|
|
|
|
2005-06-21 01:04:13 +02:00
|
|
|
/** Go through and mark the authoritative dirservers as up. */
|
2005-06-11 20:52:12 +02:00
|
|
|
static void
|
|
|
|
mark_all_trusteddirservers_up(void)
|
|
|
|
{
|
2004-11-28 10:05:49 +01:00
|
|
|
if (routerlist) {
|
2004-10-12 17:55:20 +02:00
|
|
|
SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, router,
|
2005-11-05 21:15:27 +01:00
|
|
|
if (router_digest_is_trusted_dir(router->cache_info.identity_digest) &&
|
|
|
|
router->dir_port > 0) {
|
|
|
|
router->is_running = 1;
|
|
|
|
});
|
2004-10-12 17:55:20 +02:00
|
|
|
}
|
|
|
|
if (trusted_dir_servers) {
|
|
|
|
SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, dir,
|
2005-09-14 23:09:25 +02:00
|
|
|
{
|
2007-10-09 17:27:15 +02:00
|
|
|
routerstatus_t *rs;
|
2005-09-14 23:09:25 +02:00
|
|
|
dir->is_running = 1;
|
2007-10-09 17:27:45 +02:00
|
|
|
download_status_reset(&dir->v2_ns_dl_status);
|
2007-10-16 01:15:24 +02:00
|
|
|
rs = router_get_consensus_status_by_id(dir->digest);
|
2007-10-09 17:27:15 +02:00
|
|
|
if (rs && !rs->is_running) {
|
|
|
|
rs->is_running = 1;
|
2006-12-24 03:45:35 +01:00
|
|
|
rs->last_dir_503_at = 0;
|
2006-10-20 23:04:39 +02:00
|
|
|
control_event_networkstatus_changed_single(rs);
|
|
|
|
}
|
2005-09-14 23:09:25 +02:00
|
|
|
});
|
2003-12-03 11:39:27 +01:00
|
|
|
}
|
2006-08-31 20:46:46 +02:00
|
|
|
router_dir_info_changed();
|
2005-10-05 07:03:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Reset all internal variables used to count failed downloads of network
|
|
|
|
* status objects. */
|
|
|
|
void
|
|
|
|
router_reset_status_download_failures(void)
|
|
|
|
{
|
|
|
|
mark_all_trusteddirservers_up();
|
2004-07-12 22:39:40 +02:00
|
|
|
}
|
|
|
|
|
2007-10-11 23:40:35 +02:00
|
|
|
/** Return true iff router1 and router2 have the same /16 network. */
|
|
|
|
static INLINE int
|
|
|
|
routers_in_same_network_family(routerinfo_t *r1, routerinfo_t *r2)
|
|
|
|
{
|
|
|
|
return (r1->addr & 0xffff0000) == (r2->addr & 0xffff0000);
|
|
|
|
}
|
|
|
|
|
2006-08-16 05:44:13 +02:00
|
|
|
/** Look through the routerlist and identify routers that
|
|
|
|
* advertise the same /16 network address as <b>router</b>.
|
|
|
|
* Add each of them to <b>sl</b>.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
routerlist_add_network_family(smartlist_t *sl, routerinfo_t *router)
|
|
|
|
{
|
|
|
|
SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, r,
|
|
|
|
{
|
2007-10-11 23:40:35 +02:00
|
|
|
if (router != r && routers_in_same_network_family(router, r))
|
2006-08-16 05:44:13 +02:00
|
|
|
smartlist_add(sl, r);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2004-10-15 03:58:11 +02:00
|
|
|
/** Add all the family of <b>router</b> to the smartlist <b>sl</b>.
|
2005-06-21 01:04:13 +02:00
|
|
|
* This is used to make sure we don't pick siblings in a single path.
|
2004-09-10 23:40:29 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
routerlist_add_family(smartlist_t *sl, routerinfo_t *router)
|
|
|
|
{
|
2004-10-15 03:58:11 +02:00
|
|
|
routerinfo_t *r;
|
2005-07-22 23:12:10 +02:00
|
|
|
config_line_t *cl;
|
2006-09-26 00:12:54 +02:00
|
|
|
or_options_t *options = get_options();
|
2004-09-10 23:40:29 +02:00
|
|
|
|
2007-02-22 08:41:10 +01:00
|
|
|
/* First, add any routers with similar network addresses. */
|
2006-09-26 00:12:54 +02:00
|
|
|
if (options->EnforceDistinctSubnets)
|
|
|
|
routerlist_add_network_family(sl, router);
|
2006-08-16 05:44:13 +02:00
|
|
|
|
2007-05-22 02:46:55 +02:00
|
|
|
if (router->declared_family) {
|
|
|
|
/* Add every r such that router declares familyness with r, and r
|
|
|
|
* declares familyhood with router. */
|
|
|
|
SMARTLIST_FOREACH(router->declared_family, const char *, n,
|
|
|
|
{
|
|
|
|
if (!(r = router_get_by_nickname(n, 0)))
|
|
|
|
continue;
|
|
|
|
if (!r->declared_family)
|
|
|
|
continue;
|
|
|
|
SMARTLIST_FOREACH(r->declared_family, const char *, n2,
|
|
|
|
{
|
|
|
|
if (router_nickname_matches(router, n2))
|
|
|
|
smartlist_add(sl, r);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2004-10-15 22:52:09 +02:00
|
|
|
|
2005-06-21 01:04:13 +02:00
|
|
|
/* If the user declared any families locally, honor those too. */
|
2007-10-11 23:40:35 +02:00
|
|
|
for (cl = options->NodeFamilies; cl; cl = cl->next) {
|
2004-10-15 22:52:09 +02:00
|
|
|
if (router_nickname_is_in_list(router, cl->value)) {
|
2007-02-13 02:27:55 +01:00
|
|
|
add_nickname_list_to_smartlist(sl, cl->value, 0);
|
2004-10-15 22:52:09 +02:00
|
|
|
}
|
|
|
|
}
|
2004-09-10 23:40:29 +02:00
|
|
|
}
|
|
|
|
|
2007-10-11 23:40:35 +02:00
|
|
|
/** Return true iff r is named by some nickname in <b>lst</b>. */
|
|
|
|
static INLINE int
|
|
|
|
router_in_nickname_smartlist(smartlist_t *lst, routerinfo_t *r)
|
|
|
|
{
|
|
|
|
if (!lst) return 0;
|
|
|
|
SMARTLIST_FOREACH(lst, const char *, name,
|
|
|
|
if (router_nickname_matches(r, name))
|
|
|
|
return 1;);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return true iff r1 and r2 are in the same family, but not the same
|
|
|
|
* router. */
|
|
|
|
int
|
|
|
|
routers_in_same_family(routerinfo_t *r1, routerinfo_t *r2)
|
|
|
|
{
|
|
|
|
or_options_t *options = get_options();
|
|
|
|
config_line_t *cl;
|
|
|
|
|
|
|
|
if (options->EnforceDistinctSubnets && routers_in_same_network_family(r1,r2))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (router_in_nickname_smartlist(r1->declared_family, r2) &&
|
|
|
|
router_in_nickname_smartlist(r2->declared_family, r1))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
for (cl = options->NodeFamilies; cl; cl = cl->next) {
|
|
|
|
if (router_nickname_is_in_list(r1, cl->value) &&
|
|
|
|
router_nickname_is_in_list(r2, cl->value))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-09-29 01:57:44 +02:00
|
|
|
/** Given a (possibly NULL) comma-and-whitespace separated list of nicknames,
|
|
|
|
* see which nicknames in <b>list</b> name routers in our routerlist, and add
|
|
|
|
* the routerinfos for those routers to <b>sl</b>. If <b>must_be_running</b>,
|
2007-02-13 02:27:55 +01:00
|
|
|
* only include routers that we think are running.
|
|
|
|
* Warn if any non-Named routers are specified by nickname.
|
2004-05-04 20:17:45 +02:00
|
|
|
*/
|
2004-08-18 13:20:15 +02:00
|
|
|
void
|
2005-12-14 21:40:40 +01:00
|
|
|
add_nickname_list_to_smartlist(smartlist_t *sl, const char *list,
|
2007-02-13 02:27:55 +01:00
|
|
|
int must_be_running)
|
2004-08-18 13:20:15 +02:00
|
|
|
{
|
2004-04-03 00:23:15 +02:00
|
|
|
routerinfo_t *router;
|
2004-09-02 20:39:59 +02:00
|
|
|
smartlist_t *nickname_list;
|
2005-12-15 21:44:15 +01:00
|
|
|
int have_dir_info = router_have_minimum_dir_info();
|
2004-04-03 00:23:15 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!list)
|
2004-11-03 11:08:44 +01:00
|
|
|
return; /* nothing to do */
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(sl);
|
2004-04-03 01:38:26 +02:00
|
|
|
|
2004-09-02 20:39:59 +02:00
|
|
|
nickname_list = smartlist_create();
|
2005-04-03 07:53:34 +02:00
|
|
|
if (!warned_nicknames)
|
|
|
|
warned_nicknames = smartlist_create();
|
2004-04-03 00:23:15 +02:00
|
|
|
|
2004-09-02 20:39:59 +02:00
|
|
|
smartlist_split_string(nickname_list, list, ",",
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH(nickname_list, const char *, nick, {
|
2005-04-03 07:53:34 +02:00
|
|
|
int warned;
|
2005-01-03 18:53:20 +01:00
|
|
|
if (!is_legal_nickname_or_hexdigest(nick)) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_CONFIG, "Nickname '%s' is misformed; skipping", nick);
|
2004-07-22 23:36:03 +02:00
|
|
|
continue;
|
|
|
|
}
|
2007-02-13 02:27:55 +01:00
|
|
|
router = router_get_by_nickname(nick, 1);
|
2005-04-03 07:53:34 +02:00
|
|
|
warned = smartlist_string_isin(warned_nicknames, nick);
|
2004-04-03 00:23:15 +02:00
|
|
|
if (router) {
|
2005-12-31 07:32:57 +01:00
|
|
|
if (!must_be_running || router->is_running) {
|
2004-04-03 00:23:15 +02:00
|
|
|
smartlist_add(sl,router);
|
2005-04-03 07:53:34 +02:00
|
|
|
}
|
2007-10-16 01:15:24 +02:00
|
|
|
} else if (!router_get_consensus_status_by_nickname(nick,1)) {
|
2005-04-03 07:53:34 +02:00
|
|
|
if (!warned) {
|
2005-12-15 21:44:15 +01:00
|
|
|
log_fn(have_dir_info ? LOG_WARN : LOG_INFO, LD_CONFIG,
|
2005-04-03 07:53:34 +02:00
|
|
|
"Nickname list includes '%s' which isn't a known router.",nick);
|
|
|
|
smartlist_add(warned_nicknames, tor_strdup(nick));
|
|
|
|
}
|
|
|
|
}
|
2004-09-02 20:39:59 +02:00
|
|
|
});
|
|
|
|
SMARTLIST_FOREACH(nickname_list, char *, nick, tor_free(nick));
|
|
|
|
smartlist_free(nickname_list);
|
2004-04-03 00:23:15 +02:00
|
|
|
}
|
|
|
|
|
2006-09-29 01:57:44 +02:00
|
|
|
/** Return 1 iff any member of the (possibly NULL) comma-separated list
|
|
|
|
* <b>list</b> is an acceptable nickname or hexdigest for <b>router</b>. Else
|
|
|
|
* return 0.
|
2004-10-15 22:52:09 +02:00
|
|
|
*/
|
2006-09-29 01:57:44 +02:00
|
|
|
int
|
2004-10-15 22:52:09 +02:00
|
|
|
router_nickname_is_in_list(routerinfo_t *router, const char *list)
|
|
|
|
{
|
|
|
|
smartlist_t *nickname_list;
|
|
|
|
int v = 0;
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!list)
|
2004-11-03 11:08:44 +01:00
|
|
|
return 0; /* definitely not */
|
2004-10-15 22:52:09 +02:00
|
|
|
tor_assert(router);
|
|
|
|
|
|
|
|
nickname_list = smartlist_create();
|
|
|
|
smartlist_split_string(nickname_list, list, ",",
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
2004-10-16 10:39:56 +02:00
|
|
|
SMARTLIST_FOREACH(nickname_list, const char *, cp,
|
2004-10-15 22:52:09 +02:00
|
|
|
if (router_nickname_matches(router, cp)) {v=1;break;});
|
2004-10-16 10:39:56 +02:00
|
|
|
SMARTLIST_FOREACH(nickname_list, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(nickname_list);
|
2004-10-15 22:52:09 +02:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2007-01-22 06:59:59 +01:00
|
|
|
/** Add every suitable router from our routerlist to <b>sl</b>, so that
|
|
|
|
* we can pick a node for a circuit.
|
2004-05-04 20:17:45 +02:00
|
|
|
*/
|
2004-08-17 08:27:32 +02:00
|
|
|
static void
|
2006-03-19 02:21:59 +01:00
|
|
|
router_add_running_routers_to_smartlist(smartlist_t *sl, int allow_invalid,
|
2006-02-12 04:43:39 +01:00
|
|
|
int need_uptime, int need_capacity,
|
|
|
|
int need_guard)
|
2004-08-17 08:27:32 +02:00
|
|
|
{
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!routerlist)
|
2003-12-13 08:01:46 +01:00
|
|
|
return;
|
2003-12-03 11:28:51 +01:00
|
|
|
|
2005-09-13 17:32:03 +02:00
|
|
|
SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, router,
|
|
|
|
{
|
2004-11-28 10:05:49 +01:00
|
|
|
if (router->is_running &&
|
2006-03-17 23:08:59 +01:00
|
|
|
router->purpose == ROUTER_PURPOSE_GENERAL &&
|
2007-01-22 06:59:59 +01:00
|
|
|
(router->is_valid || allow_invalid) &&
|
|
|
|
!router_is_unreliable(router, need_uptime,
|
|
|
|
need_capacity, need_guard)) {
|
|
|
|
/* If it's running, and it's suitable according to the
|
|
|
|
* other flags we had in mind */
|
2004-08-18 11:07:11 +02:00
|
|
|
smartlist_add(sl, router);
|
2004-08-08 12:32:36 +02:00
|
|
|
}
|
2005-09-13 17:32:03 +02:00
|
|
|
});
|
2003-12-03 11:28:51 +01:00
|
|
|
}
|
|
|
|
|
2005-06-21 01:04:13 +02:00
|
|
|
/** Look through the routerlist until we find a router that has my key.
|
|
|
|
Return it. */
|
2004-08-18 12:32:50 +02:00
|
|
|
routerinfo_t *
|
2005-06-11 20:52:12 +02:00
|
|
|
routerlist_find_my_routerinfo(void)
|
|
|
|
{
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!routerlist)
|
2004-08-18 12:32:50 +02:00
|
|
|
return NULL;
|
|
|
|
|
2005-09-13 17:32:03 +02:00
|
|
|
SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, router,
|
|
|
|
{
|
2004-11-28 10:05:49 +01:00
|
|
|
if (router_is_me(router))
|
2004-08-18 12:32:50 +02:00
|
|
|
return router;
|
2005-09-13 17:32:03 +02:00
|
|
|
});
|
2004-08-18 12:32:50 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-08-15 05:25:40 +02:00
|
|
|
/** Find a router that's up, that has this IP address, and
|
|
|
|
* that allows exit to this address:port, or return NULL if there
|
|
|
|
* isn't a good one.
|
|
|
|
*/
|
|
|
|
routerinfo_t *
|
2005-09-13 17:32:03 +02:00
|
|
|
router_find_exact_exit_enclave(const char *address, uint16_t port)
|
|
|
|
{
|
2005-08-15 05:25:40 +02:00
|
|
|
uint32_t addr;
|
|
|
|
struct in_addr in;
|
|
|
|
|
|
|
|
if (!tor_inet_aton(address, &in))
|
|
|
|
return NULL; /* it's not an IP already */
|
|
|
|
addr = ntohl(in.s_addr);
|
|
|
|
|
2005-09-13 17:32:03 +02:00
|
|
|
SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, router,
|
|
|
|
{
|
2005-08-15 05:25:40 +02:00
|
|
|
if (router->is_running &&
|
|
|
|
router->addr == addr &&
|
2006-03-27 04:25:34 +02:00
|
|
|
compare_addr_to_addr_policy(addr, port, router->exit_policy) ==
|
2005-08-15 05:25:40 +02:00
|
|
|
ADDR_POLICY_ACCEPTED)
|
|
|
|
return router;
|
2005-09-13 17:32:03 +02:00
|
|
|
});
|
2005-08-15 05:25:40 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-06-21 01:04:13 +02:00
|
|
|
/** Return 1 if <b>router</b> is not suitable for these parameters, else 0.
|
|
|
|
* If <b>need_uptime</b> is non-zero, we require a minimum uptime.
|
|
|
|
* If <b>need_capacity</b> is non-zero, we require a minimum advertised
|
|
|
|
* bandwidth.
|
2007-02-02 19:58:11 +01:00
|
|
|
* If <b>need_guard</b>, we require that the router is a possible entry guard.
|
2005-06-21 01:04:13 +02:00
|
|
|
*/
|
2004-08-20 23:34:36 +02:00
|
|
|
int
|
2006-02-12 04:43:39 +01:00
|
|
|
router_is_unreliable(routerinfo_t *router, int need_uptime,
|
|
|
|
int need_capacity, int need_guard)
|
2004-08-20 23:34:36 +02:00
|
|
|
{
|
2005-12-15 22:30:57 +01:00
|
|
|
if (need_uptime && !router->is_stable)
|
2004-08-20 23:34:36 +02:00
|
|
|
return 1;
|
2005-12-15 22:30:57 +01:00
|
|
|
if (need_capacity && !router->is_fast)
|
2004-08-20 23:34:36 +02:00
|
|
|
return 1;
|
2006-02-12 04:43:39 +01:00
|
|
|
if (need_guard && !router->is_possible_guard)
|
|
|
|
return 1;
|
2004-08-20 23:34:36 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2004-08-15 10:15:12 +02:00
|
|
|
|
2006-03-28 14:01:58 +02:00
|
|
|
/** Return the smaller of the router's configured BandwidthRate
|
|
|
|
* and its advertised capacity. */
|
|
|
|
uint32_t
|
|
|
|
router_get_advertised_bandwidth(routerinfo_t *router)
|
|
|
|
{
|
|
|
|
if (router->bandwidthcapacity < router->bandwidthrate)
|
|
|
|
return router->bandwidthcapacity;
|
|
|
|
return router->bandwidthrate;
|
|
|
|
}
|
|
|
|
|
2007-02-16 21:01:02 +01:00
|
|
|
/** Do not weight any declared bandwidth more than this much when picking
|
|
|
|
* routers by bandwidth. */
|
2007-08-24 10:01:47 +02:00
|
|
|
#define DEFAULT_MAX_BELIEVABLE_BANDWIDTH 10000000 /* 10 MB/sec */
|
|
|
|
|
|
|
|
/** Eventually, the number we return will come from the directory
|
|
|
|
* consensus, so clients can dynamically update to better numbers.
|
|
|
|
*
|
|
|
|
* But for now, or in case there is no consensus available, just return
|
|
|
|
* a sufficient default. */
|
|
|
|
static uint32_t
|
|
|
|
get_max_believable_bandwidth(void)
|
|
|
|
{
|
|
|
|
return DEFAULT_MAX_BELIEVABLE_BANDWIDTH;
|
|
|
|
}
|
2005-06-21 01:04:13 +02:00
|
|
|
|
2007-01-27 10:13:19 +01:00
|
|
|
/** Helper function:
|
|
|
|
* choose a random element of smartlist <b>sl</b>, weighted by
|
|
|
|
* the advertised bandwidth of each element.
|
|
|
|
*
|
|
|
|
* If <b>statuses</b> is zero, then <b>sl</b> is a list of
|
|
|
|
* routerinfo_t's. Otherwise it's a list of routerstatus_t's.
|
|
|
|
*
|
2007-02-02 19:58:11 +01:00
|
|
|
* If <b>for_exit</b>, we're picking an exit node: consider all nodes'
|
2007-08-24 08:30:34 +02:00
|
|
|
* bandwidth equally regardless of their Exit status, since there may be
|
|
|
|
* some in the list because they exit to obscure ports. If not <b>for_exit</b>,
|
2007-07-12 19:09:19 +02:00
|
|
|
* we're picking a non-exit node: weight exit-node's bandwidth less
|
2007-02-02 19:58:11 +01:00
|
|
|
* depending on the smallness of the fraction of Exit-to-total bandwidth.
|
2007-08-24 08:30:34 +02:00
|
|
|
*
|
|
|
|
* If <b>for_guard</b>, we're picking a guard node: consider all guard's
|
|
|
|
* bandwidth equally. Otherwise, weight guards proportionally less.
|
|
|
|
*
|
2005-06-21 01:04:13 +02:00
|
|
|
*/
|
2007-01-27 10:13:19 +01:00
|
|
|
static void *
|
2007-08-24 14:33:53 +02:00
|
|
|
smartlist_choose_by_bandwidth(smartlist_t *sl, bandwidth_weight_rule_t rule,
|
2007-08-24 08:30:34 +02:00
|
|
|
int statuses)
|
2004-08-15 10:15:12 +02:00
|
|
|
{
|
2007-07-26 00:56:34 +02:00
|
|
|
unsigned int i;
|
2004-08-15 10:15:12 +02:00
|
|
|
routerinfo_t *router;
|
2007-08-24 08:30:34 +02:00
|
|
|
routerstatus_t *status=NULL;
|
2007-02-08 20:44:55 +01:00
|
|
|
int32_t *bandwidths;
|
2007-02-09 00:24:03 +01:00
|
|
|
int is_exit;
|
2007-08-24 08:30:34 +02:00
|
|
|
int is_guard;
|
2006-10-01 23:59:09 +02:00
|
|
|
uint64_t total_nonexit_bw = 0, total_exit_bw = 0, total_bw = 0;
|
2007-08-24 08:30:34 +02:00
|
|
|
uint64_t total_nonguard_bw = 0, total_guard_bw = 0;
|
2006-10-01 23:59:09 +02:00
|
|
|
uint64_t rand_bw, tmp;
|
|
|
|
double exit_weight;
|
2007-08-24 08:30:34 +02:00
|
|
|
double guard_weight;
|
2007-02-08 20:44:55 +01:00
|
|
|
int n_unknown = 0;
|
2007-07-26 23:26:57 +02:00
|
|
|
bitarray_t *exit_bits;
|
2007-08-24 08:30:34 +02:00
|
|
|
bitarray_t *guard_bits;
|
2007-08-24 10:01:47 +02:00
|
|
|
uint32_t max_believable_bw = get_max_believable_bandwidth();
|
2007-08-24 08:30:34 +02:00
|
|
|
|
|
|
|
/* Can't choose exit and guard at same time */
|
2007-08-24 14:33:53 +02:00
|
|
|
tor_assert(rule == NO_WEIGHTING ||
|
|
|
|
rule == WEIGHT_FOR_EXIT ||
|
|
|
|
rule == WEIGHT_FOR_GUARD);
|
2004-08-15 10:15:12 +02:00
|
|
|
|
2007-01-22 06:59:59 +01:00
|
|
|
/* First count the total bandwidth weight, and make a list
|
2007-02-08 20:44:55 +01:00
|
|
|
* of each value. <0 means "unknown; no routerinfo." We use the
|
|
|
|
* bits of negative values to remember whether the router was fast (-x)&1
|
2007-08-25 23:31:34 +02:00
|
|
|
* and whether it was an exit (-x)&2 or guard (-x)&4. Yes, it's a hack. */
|
2007-02-08 20:44:55 +01:00
|
|
|
bandwidths = tor_malloc(sizeof(int32_t)*smartlist_len(sl));
|
2007-07-26 23:26:57 +02:00
|
|
|
exit_bits = bitarray_init_zero(smartlist_len(sl));
|
2007-08-24 08:30:34 +02:00
|
|
|
guard_bits = bitarray_init_zero(smartlist_len(sl));
|
2007-02-08 20:44:55 +01:00
|
|
|
|
|
|
|
/* Iterate over all the routerinfo_t or routerstatus_t, and */
|
2007-07-26 00:56:34 +02:00
|
|
|
for (i = 0; i < (unsigned)smartlist_len(sl); ++i) {
|
2007-01-27 10:13:19 +01:00
|
|
|
/* first, learn what bandwidth we think i has */
|
2007-02-08 20:44:55 +01:00
|
|
|
int is_known = 1;
|
|
|
|
int32_t flags = 0;
|
2007-02-09 00:24:03 +01:00
|
|
|
uint32_t this_bw = 0;
|
2007-01-27 10:13:19 +01:00
|
|
|
if (statuses) {
|
|
|
|
/* need to extract router info */
|
|
|
|
status = smartlist_get(sl, i);
|
|
|
|
router = router_get_by_digest(status->identity_digest);
|
|
|
|
is_exit = status->is_exit;
|
2007-08-24 08:30:34 +02:00
|
|
|
is_guard = status->is_possible_guard;
|
2007-01-27 10:13:19 +01:00
|
|
|
if (router) {
|
|
|
|
this_bw = router_get_advertised_bandwidth(router);
|
|
|
|
} else { /* guess */
|
2007-02-08 20:44:55 +01:00
|
|
|
is_known = 0;
|
|
|
|
flags = status->is_fast ? 1 : 0;
|
|
|
|
flags |= is_exit ? 2 : 0;
|
2007-08-24 08:30:34 +02:00
|
|
|
flags |= is_guard ? 4 : 0;
|
2007-01-27 10:13:19 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
router = smartlist_get(sl, i);
|
|
|
|
is_exit = router->is_exit;
|
2007-08-24 08:30:34 +02:00
|
|
|
is_guard = router->is_possible_guard;
|
2007-01-27 10:13:19 +01:00
|
|
|
this_bw = router_get_advertised_bandwidth(router);
|
|
|
|
}
|
2007-07-26 23:26:57 +02:00
|
|
|
if (is_exit)
|
|
|
|
bitarray_set(exit_bits, i);
|
2007-08-24 08:30:34 +02:00
|
|
|
if (is_guard)
|
|
|
|
bitarray_set(guard_bits, i);
|
2005-06-21 01:04:13 +02:00
|
|
|
/* if they claim something huge, don't believe it */
|
2007-08-24 10:01:47 +02:00
|
|
|
if (this_bw > max_believable_bw) {
|
2007-08-24 08:30:34 +02:00
|
|
|
char fp[HEX_DIGEST_LEN+1];
|
2007-08-24 12:50:48 +02:00
|
|
|
base16_encode(fp, sizeof(fp), statuses ?
|
|
|
|
status->identity_digest :
|
|
|
|
router->cache_info.identity_digest,
|
|
|
|
DIGEST_LEN);
|
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_DIR,
|
|
|
|
"Bandwidth %d for router %s (%s) exceeds allowed max %d, capping",
|
|
|
|
this_bw, router ? router->nickname : "(null)",
|
|
|
|
fp, max_believable_bw);
|
2007-08-24 10:01:47 +02:00
|
|
|
this_bw = max_believable_bw;
|
2007-08-24 08:30:34 +02:00
|
|
|
}
|
2007-02-08 20:44:55 +01:00
|
|
|
if (is_known) {
|
|
|
|
bandwidths[i] = (int32_t) this_bw; // safe since MAX_BELIEVABLE<INT32_MAX
|
2007-08-24 08:30:34 +02:00
|
|
|
if (is_guard)
|
|
|
|
total_guard_bw += this_bw;
|
|
|
|
else
|
|
|
|
total_nonguard_bw += this_bw;
|
2007-02-08 20:44:55 +01:00
|
|
|
if (is_exit)
|
|
|
|
total_exit_bw += this_bw;
|
|
|
|
else
|
|
|
|
total_nonexit_bw += this_bw;
|
|
|
|
} else {
|
|
|
|
++n_unknown;
|
|
|
|
bandwidths[i] = -flags;
|
|
|
|
}
|
2006-10-01 23:59:09 +02:00
|
|
|
}
|
2007-02-08 20:44:55 +01:00
|
|
|
|
|
|
|
/* Now, fill in the unknown values. */
|
|
|
|
if (n_unknown) {
|
|
|
|
int32_t avg_fast, avg_slow;
|
|
|
|
if (total_exit_bw+total_nonexit_bw) {
|
2007-02-08 21:27:58 +01:00
|
|
|
/* if there's some bandwidth, there's at least one known router,
|
|
|
|
* so no worries about div by 0 here */
|
2007-02-16 21:01:21 +01:00
|
|
|
int n_known = smartlist_len(sl)-n_unknown;
|
|
|
|
avg_fast = avg_slow = (int32_t)
|
|
|
|
((total_exit_bw+total_nonexit_bw)/((uint64_t) n_known));
|
2007-02-08 20:44:55 +01:00
|
|
|
} else {
|
|
|
|
avg_fast = 40000;
|
|
|
|
avg_slow = 20000;
|
|
|
|
}
|
2007-07-26 00:56:34 +02:00
|
|
|
for (i=0; i<(unsigned)smartlist_len(sl); ++i) {
|
2007-02-08 20:44:55 +01:00
|
|
|
int32_t bw = bandwidths[i];
|
|
|
|
if (bw>=0)
|
|
|
|
continue;
|
|
|
|
is_exit = ((-bw)&2);
|
2007-08-24 08:30:34 +02:00
|
|
|
is_guard = ((-bw)&4);
|
2007-02-08 20:44:55 +01:00
|
|
|
bandwidths[i] = ((-bw)&1) ? avg_fast : avg_slow;
|
|
|
|
if (is_exit)
|
|
|
|
total_exit_bw += bandwidths[i];
|
|
|
|
else
|
|
|
|
total_nonexit_bw += bandwidths[i];
|
2007-08-24 08:30:34 +02:00
|
|
|
if (is_guard)
|
|
|
|
total_guard_bw += bandwidths[i];
|
|
|
|
else
|
|
|
|
total_nonguard_bw += bandwidths[i];
|
2007-02-08 20:44:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If there's no bandwidth at all, pick at random. */
|
2006-10-01 23:59:09 +02:00
|
|
|
if (!(total_exit_bw+total_nonexit_bw)) {
|
|
|
|
tor_free(bandwidths);
|
2007-08-24 08:30:34 +02:00
|
|
|
tor_free(exit_bits);
|
|
|
|
tor_free(guard_bits);
|
2004-11-20 13:41:05 +01:00
|
|
|
return smartlist_choose(sl);
|
2004-12-07 09:51:10 +01:00
|
|
|
}
|
2007-02-08 20:44:55 +01:00
|
|
|
|
2007-08-24 08:30:34 +02:00
|
|
|
/* Figure out how to weight exits and guards */
|
|
|
|
{
|
2007-07-29 00:14:39 +02:00
|
|
|
double all_bw = U64_TO_DBL(total_exit_bw+total_nonexit_bw);
|
|
|
|
double exit_bw = U64_TO_DBL(total_exit_bw);
|
2007-08-24 08:30:34 +02:00
|
|
|
double guard_bw = U64_TO_DBL(total_guard_bw);
|
2007-07-29 00:14:39 +02:00
|
|
|
/*
|
|
|
|
* For detailed derivation of this formula, see
|
|
|
|
* http://archives.seul.org/or/dev/Jul-2007/msg00056.html
|
|
|
|
*/
|
2007-08-24 14:33:53 +02:00
|
|
|
if (rule == WEIGHT_FOR_EXIT)
|
2007-08-24 08:30:34 +02:00
|
|
|
exit_weight = 1.0;
|
|
|
|
else
|
|
|
|
exit_weight = 1.0 - all_bw/(3.0*exit_bw);
|
|
|
|
|
2007-08-24 14:33:53 +02:00
|
|
|
if (rule == WEIGHT_FOR_GUARD)
|
2007-08-24 08:30:34 +02:00
|
|
|
guard_weight = 1.0;
|
|
|
|
else
|
|
|
|
guard_weight = 1.0 - all_bw/(3.0*guard_bw);
|
|
|
|
|
|
|
|
if (exit_weight <= 0.0)
|
2007-07-29 00:14:39 +02:00
|
|
|
exit_weight = 0.0;
|
2007-08-24 08:30:34 +02:00
|
|
|
|
|
|
|
if (guard_weight <= 0.0)
|
|
|
|
guard_weight = 0.0;
|
|
|
|
|
|
|
|
total_bw = 0;
|
|
|
|
for (i=0; i < (unsigned)smartlist_len(sl); i++) {
|
|
|
|
is_exit = bitarray_is_set(exit_bits, i);
|
|
|
|
is_guard = bitarray_is_set(guard_bits, i);
|
|
|
|
if (is_exit && is_guard)
|
|
|
|
total_bw += ((uint64_t)(bandwidths[i] * exit_weight * guard_weight));
|
|
|
|
else if (is_guard)
|
|
|
|
total_bw += ((uint64_t)(bandwidths[i] * guard_weight));
|
|
|
|
else if (is_exit)
|
|
|
|
total_bw += ((uint64_t)(bandwidths[i] * exit_weight));
|
|
|
|
else
|
|
|
|
total_bw += bandwidths[i];
|
2007-07-26 23:26:57 +02:00
|
|
|
}
|
2006-10-01 23:59:09 +02:00
|
|
|
}
|
2007-10-25 05:03:52 +02:00
|
|
|
log_debug(LD_CIRC, "Total weighted bw = "U64_FORMAT
|
2007-08-24 08:30:34 +02:00
|
|
|
", exit bw = "U64_FORMAT
|
|
|
|
", nonexit bw = "U64_FORMAT", exit weight = %lf "
|
|
|
|
"(for exit == %d)"
|
|
|
|
", guard bw = "U64_FORMAT
|
|
|
|
", nonguard bw = "U64_FORMAT", guard weight = %lf "
|
|
|
|
"(for guard == %d)",
|
2007-08-24 12:50:48 +02:00
|
|
|
U64_PRINTF_ARG(total_bw),
|
|
|
|
U64_PRINTF_ARG(total_exit_bw), U64_PRINTF_ARG(total_nonexit_bw),
|
2007-08-24 14:33:53 +02:00
|
|
|
exit_weight, (int)(rule == WEIGHT_FOR_EXIT),
|
2007-08-24 12:50:48 +02:00
|
|
|
U64_PRINTF_ARG(total_guard_bw), U64_PRINTF_ARG(total_nonguard_bw),
|
2007-08-24 14:33:53 +02:00
|
|
|
guard_weight, (int)(rule == WEIGHT_FOR_GUARD));
|
2006-10-01 23:59:09 +02:00
|
|
|
|
2007-02-08 20:44:55 +01:00
|
|
|
/* Almost done: choose a random value from the bandwidth weights. */
|
2006-10-01 23:59:09 +02:00
|
|
|
rand_bw = crypto_rand_uint64(total_bw);
|
2007-02-08 20:44:55 +01:00
|
|
|
|
2005-06-21 01:04:13 +02:00
|
|
|
/* Last, count through sl until we get to the element we picked */
|
2004-08-15 10:15:12 +02:00
|
|
|
tmp = 0;
|
2007-07-26 00:56:34 +02:00
|
|
|
for (i=0; i < (unsigned)smartlist_len(sl); i++) {
|
2007-07-26 23:26:57 +02:00
|
|
|
is_exit = bitarray_is_set(exit_bits, i);
|
2007-08-24 08:30:34 +02:00
|
|
|
is_guard = bitarray_is_set(guard_bits, i);
|
|
|
|
|
|
|
|
/* Weights can be 0 if not counting guards/exits */
|
|
|
|
if (is_exit && is_guard)
|
|
|
|
tmp += ((uint64_t)(bandwidths[i] * exit_weight * guard_weight));
|
|
|
|
else if (is_guard)
|
|
|
|
tmp += ((uint64_t)(bandwidths[i] * guard_weight));
|
|
|
|
else if (is_exit)
|
|
|
|
tmp += ((uint64_t)(bandwidths[i] * exit_weight));
|
2007-08-24 12:50:48 +02:00
|
|
|
else
|
2006-10-01 23:59:09 +02:00
|
|
|
tmp += bandwidths[i];
|
2007-08-24 08:30:34 +02:00
|
|
|
|
2007-07-22 06:36:33 +02:00
|
|
|
if (tmp >= rand_bw)
|
|
|
|
break;
|
2004-08-15 10:15:12 +02:00
|
|
|
}
|
2007-07-26 00:56:34 +02:00
|
|
|
if (i == (unsigned)smartlist_len(sl)) {
|
2007-07-26 23:26:57 +02:00
|
|
|
/* This was once possible due to round-off error, but shouldn't be able
|
|
|
|
* to occur any longer. */
|
|
|
|
tor_fragile_assert();
|
2007-07-12 18:34:45 +02:00
|
|
|
--i;
|
|
|
|
log_warn(LD_BUG, "Round-off error in computing bandwidth had an effect on "
|
2007-08-24 08:30:34 +02:00
|
|
|
" which router we chose. Please tell the developers. "
|
|
|
|
U64_FORMAT " " U64_FORMAT " " U64_FORMAT, U64_PRINTF_ARG(tmp),
|
|
|
|
U64_PRINTF_ARG(rand_bw), U64_PRINTF_ARG(total_bw));
|
2007-07-12 18:34:45 +02:00
|
|
|
}
|
2006-10-01 23:59:09 +02:00
|
|
|
tor_free(bandwidths);
|
2007-07-26 23:26:57 +02:00
|
|
|
tor_free(exit_bits);
|
2007-08-24 08:30:34 +02:00
|
|
|
tor_free(guard_bits);
|
2007-01-27 10:13:19 +01:00
|
|
|
return smartlist_get(sl, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Choose a random element of router list <b>sl</b>, weighted by
|
|
|
|
* the advertised bandwidth of each router.
|
|
|
|
*/
|
|
|
|
routerinfo_t *
|
2007-08-24 14:33:53 +02:00
|
|
|
routerlist_sl_choose_by_bandwidth(smartlist_t *sl,
|
|
|
|
bandwidth_weight_rule_t rule)
|
2007-01-27 10:13:19 +01:00
|
|
|
{
|
2007-08-24 14:33:53 +02:00
|
|
|
return smartlist_choose_by_bandwidth(sl, rule, 0);
|
2007-01-27 10:13:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Choose a random element of status list <b>sl</b>, weighted by
|
|
|
|
* the advertised bandwidth of each status.
|
|
|
|
*/
|
|
|
|
routerstatus_t *
|
|
|
|
routerstatus_sl_choose_by_bandwidth(smartlist_t *sl)
|
|
|
|
{
|
2007-08-24 08:30:34 +02:00
|
|
|
/* We are choosing neither exit nor guard here. Weight accordingly. */
|
2007-08-24 14:33:53 +02:00
|
|
|
return smartlist_choose_by_bandwidth(sl, NO_WEIGHTING, 1);
|
2004-08-15 10:15:12 +02:00
|
|
|
}
|
|
|
|
|
2004-05-20 21:12:28 +02:00
|
|
|
/** Return a random running router from the routerlist. If any node
|
2004-08-15 22:14:44 +02:00
|
|
|
* named in <b>preferred</b> is available, pick one of those. Never
|
|
|
|
* pick a node named in <b>excluded</b>, or whose routerinfo is in
|
|
|
|
* <b>excludedsmartlist</b>, even if they are the only nodes
|
|
|
|
* available. If <b>strict</b> is true, never pick any node besides
|
|
|
|
* those in <b>preferred</b>.
|
2005-12-15 10:53:00 +01:00
|
|
|
* If <b>need_uptime</b> is non-zero and any router has more than
|
|
|
|
* a minimum uptime, return one of those.
|
2005-06-21 01:04:13 +02:00
|
|
|
* If <b>need_capacity</b> is non-zero, weight your choice by the
|
|
|
|
* advertised capacity of each router.
|
2007-02-02 19:58:11 +01:00
|
|
|
* If ! <b>allow_invalid</b>, consider only Valid routers.
|
|
|
|
* If <b>need_guard</b>, consider only Guard routers.
|
|
|
|
* If <b>weight_for_exit</b>, we weight bandwidths as if picking an exit node,
|
|
|
|
* otherwise we weight bandwidths for picking a relay node (that is, possibly
|
|
|
|
* discounting exit nodes).
|
2004-04-03 00:30:39 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
routerinfo_t *
|
|
|
|
router_choose_random_node(const char *preferred,
|
|
|
|
const char *excluded,
|
|
|
|
smartlist_t *excludedsmartlist,
|
|
|
|
int need_uptime, int need_capacity,
|
2006-02-12 04:43:39 +01:00
|
|
|
int need_guard,
|
2006-10-01 23:59:09 +02:00
|
|
|
int allow_invalid, int strict,
|
|
|
|
int weight_for_exit)
|
2004-04-03 00:23:15 +02:00
|
|
|
{
|
|
|
|
smartlist_t *sl, *excludednodes;
|
2008-02-04 18:25:24 +01:00
|
|
|
routerinfo_t *choice = NULL, *r;
|
2007-08-24 14:33:53 +02:00
|
|
|
bandwidth_weight_rule_t rule;
|
|
|
|
|
|
|
|
tor_assert(!(weight_for_exit && need_guard));
|
|
|
|
rule = weight_for_exit ? WEIGHT_FOR_EXIT :
|
|
|
|
(need_guard ? WEIGHT_FOR_GUARD : NO_WEIGHTING);
|
2004-04-03 00:23:15 +02:00
|
|
|
|
|
|
|
excludednodes = smartlist_create();
|
2007-02-13 02:27:55 +01:00
|
|
|
add_nickname_list_to_smartlist(excludednodes,excluded,0);
|
2004-04-03 00:23:15 +02:00
|
|
|
|
2008-02-04 18:25:24 +01:00
|
|
|
if ((r = routerlist_find_my_routerinfo())) {
|
|
|
|
smartlist_add(excludednodes, r);
|
|
|
|
routerlist_add_family(excludednodes, r);
|
|
|
|
}
|
|
|
|
|
2006-02-12 04:43:39 +01:00
|
|
|
/* Try the preferred nodes first. Ignore need_uptime and need_capacity
|
|
|
|
* and need_guard, since the user explicitly asked for these nodes. */
|
2006-01-05 11:59:46 +01:00
|
|
|
if (preferred) {
|
|
|
|
sl = smartlist_create();
|
2007-02-13 02:27:55 +01:00
|
|
|
add_nickname_list_to_smartlist(sl,preferred,1);
|
2006-01-05 11:59:46 +01:00
|
|
|
smartlist_subtract(sl,excludednodes);
|
|
|
|
if (excludedsmartlist)
|
|
|
|
smartlist_subtract(sl,excludedsmartlist);
|
|
|
|
choice = smartlist_choose(sl);
|
|
|
|
smartlist_free(sl);
|
|
|
|
}
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!choice && !strict) {
|
2005-04-06 08:17:35 +02:00
|
|
|
/* Then give up on our preferred choices: any node
|
|
|
|
* will do that has the required attributes. */
|
2004-04-03 00:23:15 +02:00
|
|
|
sl = smartlist_create();
|
2006-03-19 02:21:59 +01:00
|
|
|
router_add_running_routers_to_smartlist(sl, allow_invalid,
|
2006-02-12 04:43:39 +01:00
|
|
|
need_uptime, need_capacity,
|
|
|
|
need_guard);
|
2004-04-03 00:23:15 +02:00
|
|
|
smartlist_subtract(sl,excludednodes);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (excludedsmartlist)
|
2004-04-03 00:30:39 +02:00
|
|
|
smartlist_subtract(sl,excludedsmartlist);
|
2007-01-22 06:59:59 +01:00
|
|
|
|
2007-06-05 02:15:00 +02:00
|
|
|
if (need_capacity || need_guard)
|
2007-08-24 14:33:53 +02:00
|
|
|
choice = routerlist_sl_choose_by_bandwidth(sl, rule);
|
2004-08-18 09:53:43 +02:00
|
|
|
else
|
|
|
|
choice = smartlist_choose(sl);
|
2007-01-22 06:59:59 +01:00
|
|
|
|
2004-04-03 00:23:15 +02:00
|
|
|
smartlist_free(sl);
|
2006-02-12 04:43:39 +01:00
|
|
|
if (!choice && (need_uptime || need_capacity || need_guard)) {
|
2005-12-15 10:53:00 +01:00
|
|
|
/* try once more -- recurse but with fewer restrictions. */
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_CIRC,
|
|
|
|
"We couldn't find any live%s%s%s routers; falling back "
|
|
|
|
"to list of all routers.",
|
|
|
|
need_capacity?", fast":"",
|
|
|
|
need_uptime?", stable":"",
|
|
|
|
need_guard?", guard":"");
|
2005-12-15 10:53:00 +01:00
|
|
|
choice = router_choose_random_node(
|
2006-10-01 23:59:09 +02:00
|
|
|
NULL, excluded, excludedsmartlist,
|
|
|
|
0, 0, 0, allow_invalid, 0, weight_for_exit);
|
2005-12-15 10:53:00 +01:00
|
|
|
}
|
2004-04-03 00:23:15 +02:00
|
|
|
}
|
|
|
|
smartlist_free(excludednodes);
|
2007-02-13 02:27:55 +01:00
|
|
|
if (!choice) {
|
|
|
|
if (strict) {
|
|
|
|
log_warn(LD_CIRC, "All preferred nodes were down when trying to choose "
|
2007-02-24 06:36:45 +01:00
|
|
|
"node, and the Strict[...]Nodes option is set. Failing.");
|
2007-02-13 02:27:55 +01:00
|
|
|
} else {
|
|
|
|
log_warn(LD_CIRC,
|
|
|
|
"No available nodes when trying to choose node. Failing.");
|
|
|
|
}
|
|
|
|
}
|
2004-04-03 00:23:15 +02:00
|
|
|
return choice;
|
|
|
|
}
|
|
|
|
|
2004-07-17 00:23:18 +02:00
|
|
|
/** Return true iff the digest of <b>router</b>'s identity key,
|
|
|
|
* encoded in hexadecimal, matches <b>hexdigest</b> (which is
|
|
|
|
* optionally prefixed with a single dollar sign). Return false if
|
|
|
|
* <b>hexdigest</b> is malformed, or it doesn't match. */
|
2005-06-11 20:52:12 +02:00
|
|
|
static INLINE int
|
|
|
|
router_hex_digest_matches(routerinfo_t *router, const char *hexdigest)
|
2004-07-03 01:40:03 +02:00
|
|
|
{
|
|
|
|
char digest[DIGEST_LEN];
|
2006-10-03 20:58:40 +02:00
|
|
|
size_t len;
|
2004-07-03 01:40:03 +02:00
|
|
|
tor_assert(hexdigest);
|
|
|
|
if (hexdigest[0] == '$')
|
|
|
|
++hexdigest;
|
|
|
|
|
2006-10-03 20:58:40 +02:00
|
|
|
len = strlen(hexdigest);
|
|
|
|
if (len < HEX_DIGEST_LEN)
|
|
|
|
return 0;
|
|
|
|
else if (len > HEX_DIGEST_LEN &&
|
|
|
|
(hexdigest[HEX_DIGEST_LEN] == '=' ||
|
|
|
|
hexdigest[HEX_DIGEST_LEN] == '~')) {
|
|
|
|
if (strcasecmp(hexdigest+HEX_DIGEST_LEN+1, router->nickname))
|
|
|
|
return 0;
|
|
|
|
if (hexdigest[HEX_DIGEST_LEN] == '=' && !router->is_named)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (base16_decode(digest, DIGEST_LEN, hexdigest, HEX_DIGEST_LEN)<0)
|
2004-07-03 01:40:03 +02:00
|
|
|
return 0;
|
2005-11-05 21:15:27 +01:00
|
|
|
return (!memcmp(digest, router->cache_info.identity_digest, DIGEST_LEN));
|
2004-07-03 01:40:03 +02:00
|
|
|
}
|
|
|
|
|
2005-06-21 01:04:13 +02:00
|
|
|
/** Return true if <b>router</b>'s nickname matches <b>nickname</b>
|
2004-07-17 00:23:18 +02:00
|
|
|
* (case-insensitive), or if <b>router's</b> identity key digest
|
|
|
|
* matches a hexadecimal value stored in <b>nickname</b>. Return
|
2005-06-21 01:04:13 +02:00
|
|
|
* false otherwise. */
|
|
|
|
static int
|
2005-06-11 20:52:12 +02:00
|
|
|
router_nickname_matches(routerinfo_t *router, const char *nickname)
|
2004-07-03 01:40:03 +02:00
|
|
|
{
|
|
|
|
if (nickname[0]!='$' && !strcasecmp(router->nickname, nickname))
|
|
|
|
return 1;
|
2005-06-21 01:04:13 +02:00
|
|
|
return router_hex_digest_matches(router, nickname);
|
2004-07-03 01:40:03 +02:00
|
|
|
}
|
|
|
|
|
2004-07-01 03:16:59 +02:00
|
|
|
/** Return the router in our routerlist whose (case-insensitive)
|
|
|
|
* nickname or (case-sensitive) hexadecimal key digest is
|
|
|
|
* <b>nickname</b>. Return NULL if no such router is known.
|
2004-05-04 20:17:45 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
routerinfo_t *
|
2005-10-05 00:23:31 +02:00
|
|
|
router_get_by_nickname(const char *nickname, int warn_if_unnamed)
|
2003-09-25 07:17:11 +02:00
|
|
|
{
|
2005-09-13 17:32:03 +02:00
|
|
|
int maybedigest;
|
2004-07-01 03:16:59 +02:00
|
|
|
char digest[DIGEST_LEN];
|
2005-10-05 00:23:31 +02:00
|
|
|
routerinfo_t *best_match=NULL;
|
|
|
|
int n_matches = 0;
|
2007-10-08 23:21:50 +02:00
|
|
|
const char *named_digest = NULL;
|
2003-09-25 07:17:11 +02:00
|
|
|
|
2004-05-12 21:49:48 +02:00
|
|
|
tor_assert(nickname);
|
2004-06-02 22:00:57 +02:00
|
|
|
if (!routerlist)
|
|
|
|
return NULL;
|
2004-07-03 01:40:03 +02:00
|
|
|
if (nickname[0] == '$')
|
|
|
|
return router_get_by_hexdigest(nickname);
|
2006-09-29 06:51:28 +02:00
|
|
|
if (!strcasecmp(nickname, UNNAMED_ROUTER_NICKNAME))
|
|
|
|
return NULL;
|
2005-02-27 08:23:42 +01:00
|
|
|
if (server_mode(get_options()) &&
|
|
|
|
!strcasecmp(nickname, get_options()->Nickname))
|
|
|
|
return router_get_my_routerinfo();
|
2004-07-03 01:40:03 +02:00
|
|
|
|
2006-10-03 20:58:40 +02:00
|
|
|
maybedigest = (strlen(nickname) >= HEX_DIGEST_LEN) &&
|
2004-07-01 03:16:59 +02:00
|
|
|
(base16_decode(digest,DIGEST_LEN,nickname,HEX_DIGEST_LEN) == 0);
|
|
|
|
|
2007-10-08 23:21:50 +02:00
|
|
|
if ((named_digest = networkstatus_get_router_digest_by_nickname(nickname))) {
|
2007-06-12 00:19:40 +02:00
|
|
|
return rimap_get(routerlist->identity_map, named_digest);
|
2006-09-20 01:48:14 +02:00
|
|
|
}
|
2007-10-19 20:56:24 +02:00
|
|
|
if (networkstatus_nickname_is_unnamed(nickname))
|
|
|
|
return NULL;
|
2006-09-20 01:48:14 +02:00
|
|
|
|
2007-10-16 01:15:24 +02:00
|
|
|
/* If we reach this point, there's no canonical value for the nickname. */
|
|
|
|
|
2005-09-13 17:32:03 +02:00
|
|
|
SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, router,
|
|
|
|
{
|
2005-10-05 00:23:31 +02:00
|
|
|
if (!strcasecmp(router->nickname, nickname)) {
|
2006-09-20 01:48:14 +02:00
|
|
|
++n_matches;
|
|
|
|
if (n_matches <= 1 || router->is_running)
|
|
|
|
best_match = router;
|
2005-10-05 00:23:31 +02:00
|
|
|
} else if (maybedigest &&
|
2005-12-14 21:40:40 +01:00
|
|
|
!memcmp(digest, router->cache_info.identity_digest, DIGEST_LEN)
|
|
|
|
) {
|
2006-10-03 20:58:40 +02:00
|
|
|
if (router_hex_digest_matches(router, nickname))
|
|
|
|
return router;
|
2008-02-06 00:20:44 +01:00
|
|
|
/* If we reach this point, we have a ID=name syntax that matches the
|
|
|
|
* identity but not the name. That isn't an acceptable match. */
|
2005-10-05 00:23:31 +02:00
|
|
|
}
|
2005-09-13 17:32:03 +02:00
|
|
|
});
|
2004-07-01 03:16:59 +02:00
|
|
|
|
2005-10-05 00:23:31 +02:00
|
|
|
if (best_match) {
|
2005-10-11 03:57:28 +02:00
|
|
|
if (warn_if_unnamed && n_matches > 1) {
|
2005-10-05 00:23:31 +02:00
|
|
|
smartlist_t *fps = smartlist_create();
|
|
|
|
int any_unwarned = 0;
|
|
|
|
SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, router,
|
|
|
|
{
|
2007-10-09 17:27:15 +02:00
|
|
|
routerstatus_t *rs;
|
2005-10-05 00:23:31 +02:00
|
|
|
char *desc;
|
|
|
|
size_t dlen;
|
|
|
|
char fp[HEX_DIGEST_LEN+1];
|
|
|
|
if (strcasecmp(router->nickname, nickname))
|
|
|
|
continue;
|
2007-10-16 01:15:24 +02:00
|
|
|
rs = router_get_consensus_status_by_id(
|
2005-12-14 21:40:40 +01:00
|
|
|
router->cache_info.identity_digest);
|
2006-04-04 05:25:36 +02:00
|
|
|
if (rs && !rs->name_lookup_warned) {
|
2005-10-05 00:23:31 +02:00
|
|
|
rs->name_lookup_warned = 1;
|
|
|
|
any_unwarned = 1;
|
|
|
|
}
|
2005-12-14 21:40:40 +01:00
|
|
|
base16_encode(fp, sizeof(fp),
|
|
|
|
router->cache_info.identity_digest, DIGEST_LEN);
|
2005-10-05 00:23:31 +02:00
|
|
|
dlen = 32 + HEX_DIGEST_LEN + strlen(router->address);
|
|
|
|
desc = tor_malloc(dlen);
|
|
|
|
tor_snprintf(desc, dlen, "\"$%s\" for the one at %s:%d",
|
|
|
|
fp, router->address, router->or_port);
|
|
|
|
smartlist_add(fps, desc);
|
|
|
|
});
|
|
|
|
if (any_unwarned) {
|
|
|
|
char *alternatives = smartlist_join_strings(fps, "; ",0,NULL);
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_CONFIG,
|
|
|
|
"There are multiple matches for the nickname \"%s\","
|
2006-04-25 08:02:46 +02:00
|
|
|
" but none is listed as named by the directory authorities. "
|
2006-04-04 05:25:36 +02:00
|
|
|
"Choosing one arbitrarily. If you meant one in particular, "
|
2006-02-13 11:33:00 +01:00
|
|
|
"you should say %s.", nickname, alternatives);
|
2005-10-05 00:23:31 +02:00
|
|
|
tor_free(alternatives);
|
|
|
|
}
|
|
|
|
SMARTLIST_FOREACH(fps, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(fps);
|
|
|
|
} else if (warn_if_unnamed) {
|
2007-10-16 01:15:24 +02:00
|
|
|
routerstatus_t *rs = router_get_consensus_status_by_id(
|
2005-12-14 21:40:40 +01:00
|
|
|
best_match->cache_info.identity_digest);
|
2005-10-05 00:23:31 +02:00
|
|
|
if (rs && !rs->name_lookup_warned) {
|
|
|
|
char fp[HEX_DIGEST_LEN+1];
|
2005-12-14 21:40:40 +01:00
|
|
|
base16_encode(fp, sizeof(fp),
|
|
|
|
best_match->cache_info.identity_digest, DIGEST_LEN);
|
2007-03-04 19:58:38 +01:00
|
|
|
log_warn(LD_CONFIG, "You specified a server \"%s\" by name, but this "
|
|
|
|
"name is not registered, so it could be used by any server, "
|
|
|
|
"not just the one you meant. "
|
2005-12-10 10:36:26 +01:00
|
|
|
"To make sure you get the same server in the future, refer to "
|
|
|
|
"it by key, as \"$%s\".", nickname, fp);
|
2005-10-05 00:23:31 +02:00
|
|
|
rs->name_lookup_warned = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return best_match;
|
|
|
|
}
|
|
|
|
|
2004-07-01 03:16:59 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-11-19 07:57:44 +01:00
|
|
|
/** Try to find a routerinfo for <b>digest</b>. If we don't have one,
|
|
|
|
* return 1. If we do, ask tor_version_as_new_as() for the answer.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
router_digest_version_as_new_as(const char *digest, const char *cutoff)
|
|
|
|
{
|
|
|
|
routerinfo_t *router = router_get_by_digest(digest);
|
|
|
|
if (!router)
|
|
|
|
return 1;
|
|
|
|
return tor_version_as_new_as(router->platform, cutoff);
|
|
|
|
}
|
|
|
|
|
2007-10-08 19:44:19 +02:00
|
|
|
/** Return true iff <b>digest</b> is the digest of the identity key of a
|
|
|
|
* trusted directory matching at least one bit of <b>type</b>. If <b>type</b>
|
|
|
|
* is zero, any authority is okay. */
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
2007-10-08 19:44:19 +02:00
|
|
|
router_digest_is_trusted_dir_type(const char *digest, authority_type_t type)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2004-10-12 17:55:20 +02:00
|
|
|
if (!trusted_dir_servers)
|
2004-07-20 22:09:59 +02:00
|
|
|
return 0;
|
2007-05-02 11:12:04 +02:00
|
|
|
if (authdir_mode(get_options()) && router_digest_is_me(digest))
|
2006-04-08 23:19:40 +02:00
|
|
|
return 1;
|
2004-10-12 17:55:20 +02:00
|
|
|
SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ent,
|
2007-10-08 19:44:19 +02:00
|
|
|
if (!memcmp(digest, ent->digest, DIGEST_LEN)) {
|
|
|
|
return (!type) || ((type & ent->type) != 0);
|
|
|
|
});
|
2004-07-20 22:09:59 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-10 22:13:52 +02:00
|
|
|
/** Return true iff <b>addr</b> is the address of one of our trusted
|
|
|
|
* directory authorities. */
|
|
|
|
int
|
|
|
|
router_addr_is_trusted_dir(uint32_t addr)
|
|
|
|
{
|
|
|
|
if (!trusted_dir_servers)
|
|
|
|
return 0;
|
|
|
|
SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ent,
|
|
|
|
if (ent->addr == addr)
|
|
|
|
return 1;
|
|
|
|
);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-04-27 12:26:09 +02:00
|
|
|
/** If hexdigest is correctly formed, base16_decode it into
|
|
|
|
* digest, which must have DIGEST_LEN space in it.
|
|
|
|
* Return 0 on success, -1 on failure.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
hexdigest_to_digest(const char *hexdigest, char *digest)
|
|
|
|
{
|
|
|
|
if (hexdigest[0]=='$')
|
|
|
|
++hexdigest;
|
|
|
|
if (strlen(hexdigest) < HEX_DIGEST_LEN ||
|
|
|
|
base16_decode(digest,DIGEST_LEN,hexdigest,HEX_DIGEST_LEN) < 0)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-07-01 03:16:59 +02:00
|
|
|
/** Return the router in our routerlist whose hexadecimal key digest
|
|
|
|
* is <b>hexdigest</b>. Return NULL if no such router is known. */
|
2005-06-11 20:52:12 +02:00
|
|
|
routerinfo_t *
|
|
|
|
router_get_by_hexdigest(const char *hexdigest)
|
|
|
|
{
|
2004-07-01 03:16:59 +02:00
|
|
|
char digest[DIGEST_LEN];
|
2006-10-03 20:58:40 +02:00
|
|
|
size_t len;
|
|
|
|
routerinfo_t *ri;
|
2004-07-01 03:16:59 +02:00
|
|
|
|
|
|
|
tor_assert(hexdigest);
|
|
|
|
if (!routerlist)
|
|
|
|
return NULL;
|
2004-07-03 01:40:03 +02:00
|
|
|
if (hexdigest[0]=='$')
|
|
|
|
++hexdigest;
|
2006-10-03 20:58:40 +02:00
|
|
|
len = strlen(hexdigest);
|
2007-04-27 12:26:09 +02:00
|
|
|
if (hexdigest_to_digest(hexdigest, digest) < 0)
|
2004-07-01 03:16:59 +02:00
|
|
|
return NULL;
|
|
|
|
|
2006-10-03 20:58:40 +02:00
|
|
|
ri = router_get_by_digest(digest);
|
|
|
|
|
2008-01-15 06:57:19 +01:00
|
|
|
if (ri && len > HEX_DIGEST_LEN) {
|
2006-10-03 20:58:40 +02:00
|
|
|
if (hexdigest[HEX_DIGEST_LEN] == '=') {
|
|
|
|
if (strcasecmp(ri->nickname, hexdigest+HEX_DIGEST_LEN+1) ||
|
|
|
|
!ri->is_named)
|
|
|
|
return NULL;
|
|
|
|
} else if (hexdigest[HEX_DIGEST_LEN] == '~') {
|
|
|
|
if (strcasecmp(ri->nickname, hexdigest+HEX_DIGEST_LEN+1))
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ri;
|
2004-07-01 03:16:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Return the router in our routerlist whose 20-byte key digest
|
2005-03-22 01:42:38 +01:00
|
|
|
* is <b>digest</b>. Return NULL if no such router is known. */
|
2005-06-11 20:52:12 +02:00
|
|
|
routerinfo_t *
|
|
|
|
router_get_by_digest(const char *digest)
|
|
|
|
{
|
2004-07-01 03:16:59 +02:00
|
|
|
tor_assert(digest);
|
2005-06-21 01:04:13 +02:00
|
|
|
|
2005-02-27 10:47:01 +01:00
|
|
|
if (!routerlist) return NULL;
|
2003-09-25 07:17:11 +02:00
|
|
|
|
2005-10-18 22:13:09 +02:00
|
|
|
// routerlist_assert_ok(routerlist);
|
2004-04-06 00:22:42 +02:00
|
|
|
|
2007-06-12 00:19:40 +02:00
|
|
|
return rimap_get(routerlist->identity_map, digest);
|
2003-09-25 07:17:11 +02:00
|
|
|
}
|
2003-09-11 22:32:15 +02:00
|
|
|
|
2005-10-14 06:56:20 +02:00
|
|
|
/** Return the router in our routerlist whose 20-byte descriptor
|
|
|
|
* is <b>digest</b>. Return NULL if no such router is known. */
|
2005-11-05 21:15:27 +01:00
|
|
|
signed_descriptor_t *
|
2005-10-14 06:56:20 +02:00
|
|
|
router_get_by_descriptor_digest(const char *digest)
|
|
|
|
{
|
|
|
|
tor_assert(digest);
|
|
|
|
|
|
|
|
if (!routerlist) return NULL;
|
|
|
|
|
2007-06-12 00:19:40 +02:00
|
|
|
return sdmap_get(routerlist->desc_digest_map, digest);
|
2005-10-14 06:56:20 +02:00
|
|
|
}
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/** Return the signed descriptor for the router in our routerlist whose
|
|
|
|
* 20-byte extra-info digest is <b>digest</b>. Return NULL if no such router
|
|
|
|
* is known. */
|
2007-05-18 23:19:53 +02:00
|
|
|
signed_descriptor_t *
|
|
|
|
router_get_by_extrainfo_digest(const char *digest)
|
|
|
|
{
|
|
|
|
tor_assert(digest);
|
|
|
|
|
|
|
|
if (!routerlist) return NULL;
|
|
|
|
|
2007-06-12 00:19:40 +02:00
|
|
|
return sdmap_get(routerlist->desc_by_eid_map, digest);
|
2007-05-18 23:19:53 +02:00
|
|
|
}
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/** Return the signed descriptor for the extrainfo_t in our routerlist whose
|
|
|
|
* extra-info-digest is <b>digest</b>. Return NULL if no such extra-info
|
|
|
|
* document is known. */
|
2007-04-16 20:39:39 +02:00
|
|
|
signed_descriptor_t *
|
|
|
|
extrainfo_get_by_descriptor_digest(const char *digest)
|
|
|
|
{
|
2007-04-16 23:37:21 +02:00
|
|
|
extrainfo_t *ei;
|
|
|
|
tor_assert(digest);
|
|
|
|
if (!routerlist) return NULL;
|
2007-06-12 00:19:40 +02:00
|
|
|
ei = eimap_get(routerlist->extra_info_map, digest);
|
2007-04-16 23:37:21 +02:00
|
|
|
return ei ? &ei->cache_info : NULL;
|
2007-04-16 20:39:39 +02:00
|
|
|
}
|
|
|
|
|
2007-01-22 08:51:06 +01:00
|
|
|
/** Return a pointer to the signed textual representation of a descriptor.
|
|
|
|
* The returned string is not guaranteed to be NUL-terminated: the string's
|
2007-10-04 18:21:58 +02:00
|
|
|
* length will be in desc-\>signed_descriptor_len.
|
|
|
|
*
|
2007-12-06 08:15:06 +01:00
|
|
|
* If <b>with_annotations</b> is set, the returned string will include
|
|
|
|
* the annotations
|
2007-10-04 18:21:58 +02:00
|
|
|
* (if any) preceding the descriptor. This will increase the length of the
|
|
|
|
* string by desc-\>annotations_len.
|
|
|
|
*
|
|
|
|
* The caller must not free the string returned.
|
|
|
|
*/
|
2007-09-26 18:19:44 +02:00
|
|
|
static const char *
|
|
|
|
signed_descriptor_get_body_impl(signed_descriptor_t *desc,
|
|
|
|
int with_annotations)
|
2006-01-12 19:04:17 +01:00
|
|
|
{
|
2007-05-20 07:15:53 +02:00
|
|
|
const char *r = NULL;
|
2006-06-22 09:01:54 +02:00
|
|
|
size_t len = desc->signed_descriptor_len;
|
2007-09-26 18:19:44 +02:00
|
|
|
off_t offset = desc->saved_offset;
|
|
|
|
if (with_annotations)
|
|
|
|
len += desc->annotations_len;
|
|
|
|
else
|
|
|
|
offset += desc->annotations_len;
|
|
|
|
|
2006-06-22 09:01:54 +02:00
|
|
|
tor_assert(len > 32);
|
2007-05-20 07:15:53 +02:00
|
|
|
if (desc->saved_location == SAVED_IN_CACHE && routerlist) {
|
2007-08-31 17:08:37 +02:00
|
|
|
desc_store_t *store = desc_get_store(router_get_routerlist(), desc);
|
|
|
|
if (store && store->mmap) {
|
|
|
|
tor_assert(desc->saved_offset + len <= store->mmap->size);
|
2007-09-26 18:19:44 +02:00
|
|
|
r = store->mmap->data + offset;
|
2007-05-20 07:15:53 +02:00
|
|
|
}
|
2006-06-22 09:01:54 +02:00
|
|
|
}
|
2007-05-20 07:15:53 +02:00
|
|
|
if (!r) /* no mmap, or not in cache. */
|
2007-09-26 18:19:44 +02:00
|
|
|
r = desc->signed_descriptor_body +
|
|
|
|
(with_annotations ? 0 : desc->annotations_len);
|
2007-05-20 07:15:53 +02:00
|
|
|
|
2006-06-22 09:01:54 +02:00
|
|
|
tor_assert(r);
|
2007-09-27 22:46:30 +02:00
|
|
|
if (!with_annotations) {
|
|
|
|
if (memcmp("router ", r, 7) && memcmp("extra-info ", r, 11)) {
|
2008-01-16 06:27:19 +01:00
|
|
|
char *cp = tor_strndup(r, 64);
|
2007-09-27 22:46:30 +02:00
|
|
|
log_err(LD_DIR, "descriptor at %p begins with unexpected string %s",
|
2008-01-16 06:27:19 +01:00
|
|
|
desc, escaped(cp));
|
|
|
|
tor_free(cp);
|
2007-09-27 22:46:30 +02:00
|
|
|
}
|
|
|
|
tor_assert(!memcmp("router ", r, 7) || !memcmp("extra-info ", r, 11));
|
2007-05-20 07:15:53 +02:00
|
|
|
}
|
2006-06-22 09:01:54 +02:00
|
|
|
|
|
|
|
return r;
|
2006-01-12 19:04:17 +01:00
|
|
|
}
|
|
|
|
|
2007-10-04 18:21:58 +02:00
|
|
|
/** Return a pointer to the signed textual representation of a descriptor.
|
|
|
|
* The returned string is not guaranteed to be NUL-terminated: the string's
|
|
|
|
* length will be in desc-\>signed_descriptor_len.
|
|
|
|
*
|
|
|
|
* The caller must not free the string returned.
|
|
|
|
*/
|
2007-09-26 18:19:44 +02:00
|
|
|
const char *
|
|
|
|
signed_descriptor_get_body(signed_descriptor_t *desc)
|
|
|
|
{
|
|
|
|
return signed_descriptor_get_body_impl(desc, 0);
|
|
|
|
}
|
|
|
|
|
2007-12-06 08:15:06 +01:00
|
|
|
/** As signed_descriptor_get_body(), but points to the beginning of the
|
|
|
|
* annotations section rather than the beginning of the descriptor. */
|
|
|
|
const char *
|
|
|
|
signed_descriptor_get_annotations(signed_descriptor_t *desc)
|
|
|
|
{
|
|
|
|
return signed_descriptor_get_body_impl(desc, 1);
|
|
|
|
}
|
|
|
|
|
2005-10-18 19:43:54 +02:00
|
|
|
/** Return the current list of all known routers. */
|
|
|
|
routerlist_t *
|
|
|
|
router_get_routerlist(void)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2007-08-31 17:08:37 +02:00
|
|
|
if (PREDICT_UNLIKELY(!routerlist)) {
|
2005-10-18 19:43:54 +02:00
|
|
|
routerlist = tor_malloc_zero(sizeof(routerlist_t));
|
|
|
|
routerlist->routers = smartlist_create();
|
2005-10-27 02:34:39 +02:00
|
|
|
routerlist->old_routers = smartlist_create();
|
2007-06-12 00:19:40 +02:00
|
|
|
routerlist->identity_map = rimap_new();
|
|
|
|
routerlist->desc_digest_map = sdmap_new();
|
|
|
|
routerlist->desc_by_eid_map = sdmap_new();
|
|
|
|
routerlist->extra_info_map = eimap_new();
|
2007-08-31 17:08:37 +02:00
|
|
|
|
2007-09-27 22:46:24 +02:00
|
|
|
routerlist->desc_store.fname_base = "cached-descriptors";
|
|
|
|
routerlist->desc_store.fname_alt_base = "cached-routers";
|
2007-08-31 17:08:37 +02:00
|
|
|
routerlist->extrainfo_store.fname_base = "cached-extrainfo";
|
|
|
|
|
|
|
|
routerlist->desc_store.type = ROUTER_STORE;
|
|
|
|
routerlist->extrainfo_store.type = EXTRAINFO_STORE;
|
|
|
|
|
|
|
|
routerlist->desc_store.description = "router descriptors";
|
|
|
|
routerlist->extrainfo_store.description = "extra-info documents";
|
2005-10-18 19:43:54 +02:00
|
|
|
}
|
|
|
|
return routerlist;
|
2002-09-26 14:09:10 +02:00
|
|
|
}
|
|
|
|
|
2004-05-10 06:34:48 +02:00
|
|
|
/** Free all storage held by <b>router</b>. */
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
routerinfo_free(routerinfo_t *router)
|
2002-06-27 00:45:49 +02:00
|
|
|
{
|
2003-05-09 04:00:33 +02:00
|
|
|
if (!router)
|
2002-06-27 00:45:49 +02:00
|
|
|
return;
|
|
|
|
|
2006-01-12 19:04:17 +01:00
|
|
|
tor_free(router->cache_info.signed_descriptor_body);
|
2003-10-21 11:48:17 +02:00
|
|
|
tor_free(router->address);
|
|
|
|
tor_free(router->nickname);
|
2004-04-07 23:36:03 +02:00
|
|
|
tor_free(router->platform);
|
2005-05-06 10:53:23 +02:00
|
|
|
tor_free(router->contact_info);
|
2003-09-25 07:17:11 +02:00
|
|
|
if (router->onion_pkey)
|
|
|
|
crypto_free_pk_env(router->onion_pkey);
|
|
|
|
if (router->identity_pkey)
|
|
|
|
crypto_free_pk_env(router->identity_pkey);
|
2004-10-15 03:58:11 +02:00
|
|
|
if (router->declared_family) {
|
|
|
|
SMARTLIST_FOREACH(router->declared_family, char *, s, tor_free(s));
|
|
|
|
smartlist_free(router->declared_family);
|
|
|
|
}
|
2008-01-02 05:43:44 +01:00
|
|
|
addr_policy_list_free(router->exit_policy);
|
2007-05-19 20:12:31 +02:00
|
|
|
|
2008-02-06 00:20:44 +01:00
|
|
|
/* XXXX Remove if this turns out to affect performance. */
|
2007-05-19 20:12:31 +02:00
|
|
|
memset(router, 77, sizeof(routerinfo_t));
|
|
|
|
|
2004-09-29 08:52:36 +02:00
|
|
|
tor_free(router);
|
2002-09-24 12:43:57 +02:00
|
|
|
}
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/** Release all storage held by <b>extrainfo</b> */
|
2007-04-16 06:17:58 +02:00
|
|
|
void
|
|
|
|
extrainfo_free(extrainfo_t *extrainfo)
|
|
|
|
{
|
|
|
|
if (!extrainfo)
|
|
|
|
return;
|
|
|
|
tor_free(extrainfo->cache_info.signed_descriptor_body);
|
|
|
|
tor_free(extrainfo->pending_sig);
|
2007-05-19 20:12:31 +02:00
|
|
|
|
2008-02-06 00:20:44 +01:00
|
|
|
/* XXXX remove this if it turns out to slow us down. */
|
2007-05-29 19:31:13 +02:00
|
|
|
memset(extrainfo, 88, sizeof(extrainfo_t)); /* debug bad memory usage */
|
2007-04-16 06:17:58 +02:00
|
|
|
tor_free(extrainfo);
|
|
|
|
}
|
|
|
|
|
2005-12-27 06:26:03 +01:00
|
|
|
/** Release storage held by <b>sd</b>. */
|
2005-11-05 21:15:27 +01:00
|
|
|
static void
|
|
|
|
signed_descriptor_free(signed_descriptor_t *sd)
|
|
|
|
{
|
2006-01-12 19:04:17 +01:00
|
|
|
tor_free(sd->signed_descriptor_body);
|
2007-05-19 20:12:31 +02:00
|
|
|
|
2008-02-06 00:20:44 +01:00
|
|
|
/* XXXX remove this once more bugs go away. */
|
2007-05-29 19:31:13 +02:00
|
|
|
memset(sd, 99, sizeof(signed_descriptor_t)); /* Debug bad mem usage */
|
2005-11-05 21:15:27 +01:00
|
|
|
tor_free(sd);
|
|
|
|
}
|
|
|
|
|
2005-12-27 06:26:03 +01:00
|
|
|
/** Extract a signed_descriptor_t from a routerinfo, and free the routerinfo.
|
|
|
|
*/
|
2005-11-05 21:15:27 +01:00
|
|
|
static signed_descriptor_t *
|
|
|
|
signed_descriptor_from_routerinfo(routerinfo_t *ri)
|
|
|
|
{
|
|
|
|
signed_descriptor_t *sd = tor_malloc_zero(sizeof(signed_descriptor_t));
|
|
|
|
memcpy(sd, &(ri->cache_info), sizeof(signed_descriptor_t));
|
2007-11-07 18:11:23 +01:00
|
|
|
sd->routerlist_index = -1;
|
2006-01-12 19:04:17 +01:00
|
|
|
ri->cache_info.signed_descriptor_body = NULL;
|
2005-11-05 21:15:27 +01:00
|
|
|
routerinfo_free(ri);
|
|
|
|
return sd;
|
|
|
|
}
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/** Helper: free the storage held by the extrainfo_t in <b>e</b>. */
|
2007-04-16 23:37:21 +02:00
|
|
|
static void
|
|
|
|
_extrainfo_free(void *e)
|
|
|
|
{
|
|
|
|
extrainfo_free(e);
|
|
|
|
}
|
|
|
|
|
2007-05-25 21:41:31 +02:00
|
|
|
/** Free all storage held by a routerlist <b>rl</b>. */
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
routerlist_free(routerlist_t *rl)
|
2003-05-06 19:38:16 +02:00
|
|
|
{
|
2005-02-25 00:01:26 +01:00
|
|
|
tor_assert(rl);
|
2007-06-12 00:19:40 +02:00
|
|
|
rimap_free(rl->identity_map, NULL);
|
|
|
|
sdmap_free(rl->desc_digest_map, NULL);
|
|
|
|
sdmap_free(rl->desc_by_eid_map, NULL);
|
|
|
|
eimap_free(rl->extra_info_map, _extrainfo_free);
|
2004-04-07 21:46:27 +02:00
|
|
|
SMARTLIST_FOREACH(rl->routers, routerinfo_t *, r,
|
|
|
|
routerinfo_free(r));
|
2005-11-05 21:15:27 +01:00
|
|
|
SMARTLIST_FOREACH(rl->old_routers, signed_descriptor_t *, sd,
|
|
|
|
signed_descriptor_free(sd));
|
2004-04-07 21:46:27 +02:00
|
|
|
smartlist_free(rl->routers);
|
2005-10-27 02:34:39 +02:00
|
|
|
smartlist_free(rl->old_routers);
|
2007-08-31 17:08:37 +02:00
|
|
|
if (routerlist->desc_store.mmap)
|
|
|
|
tor_munmap_file(routerlist->desc_store.mmap);
|
|
|
|
if (routerlist->extrainfo_store.mmap)
|
|
|
|
tor_munmap_file(routerlist->extrainfo_store.mmap);
|
2004-04-07 21:46:27 +02:00
|
|
|
tor_free(rl);
|
2006-08-31 20:46:46 +02:00
|
|
|
|
|
|
|
router_dir_info_changed();
|
2003-05-06 19:38:16 +02:00
|
|
|
}
|
|
|
|
|
2007-10-18 16:19:51 +02:00
|
|
|
/** Log information about how much memory is being used for routerlist,
|
|
|
|
* at log level <b>severity</b>. */
|
2005-11-23 07:00:58 +01:00
|
|
|
void
|
|
|
|
dump_routerlist_mem_usage(int severity)
|
|
|
|
{
|
|
|
|
uint64_t livedescs = 0;
|
|
|
|
uint64_t olddescs = 0;
|
|
|
|
if (!routerlist)
|
|
|
|
return;
|
|
|
|
SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, r,
|
|
|
|
livedescs += r->cache_info.signed_descriptor_len);
|
|
|
|
SMARTLIST_FOREACH(routerlist->old_routers, signed_descriptor_t *, sd,
|
2005-11-23 08:06:36 +01:00
|
|
|
olddescs += sd->signed_descriptor_len);
|
2005-11-23 07:00:58 +01:00
|
|
|
|
|
|
|
log(severity, LD_GENERAL,
|
|
|
|
"In %d live descriptors: "U64_FORMAT" bytes. "
|
|
|
|
"In %d old descriptors: "U64_FORMAT" bytes.",
|
|
|
|
smartlist_len(routerlist->routers), U64_PRINTF_ARG(livedescs),
|
|
|
|
smartlist_len(routerlist->old_routers), U64_PRINTF_ARG(olddescs));
|
|
|
|
}
|
|
|
|
|
2005-10-27 02:34:39 +02:00
|
|
|
static INLINE int
|
2005-11-05 21:15:27 +01:00
|
|
|
_routerlist_find_elt(smartlist_t *sl, void *ri, int idx)
|
2005-10-27 02:34:39 +02:00
|
|
|
{
|
2007-07-02 22:17:12 +02:00
|
|
|
if (idx < 0) {
|
2005-10-27 02:34:39 +02:00
|
|
|
idx = -1;
|
|
|
|
SMARTLIST_FOREACH(sl, routerinfo_t *, r,
|
|
|
|
if (r == ri) {
|
|
|
|
idx = r_sl_idx;
|
|
|
|
break;
|
|
|
|
});
|
2007-07-02 22:17:12 +02:00
|
|
|
} else {
|
|
|
|
tor_assert(idx < smartlist_len(sl));
|
|
|
|
tor_assert(smartlist_get(sl, idx) == ri);
|
|
|
|
};
|
2005-10-28 01:06:09 +02:00
|
|
|
return idx;
|
2005-10-27 02:34:39 +02:00
|
|
|
}
|
|
|
|
|
2005-10-18 19:43:54 +02:00
|
|
|
/** Insert an item <b>ri</b> into the routerlist <b>rl</b>, updating indices
|
2007-03-11 21:34:44 +01:00
|
|
|
* as needed. There must be no previous member of <b>rl</b> with the same
|
2007-03-11 21:52:07 +01:00
|
|
|
* identity digest as <b>ri</b>: If there is, call routerlist_replace
|
2007-03-11 21:34:44 +01:00
|
|
|
* instead.
|
|
|
|
*/
|
2005-10-18 19:43:54 +02:00
|
|
|
static void
|
|
|
|
routerlist_insert(routerlist_t *rl, routerinfo_t *ri)
|
|
|
|
{
|
2007-03-11 21:34:44 +01:00
|
|
|
routerinfo_t *ri_old;
|
2007-05-19 02:32:00 +02:00
|
|
|
{
|
2008-02-06 00:20:44 +01:00
|
|
|
/* XXXX Remove if this slows us down. */
|
2007-05-19 02:32:00 +02:00
|
|
|
routerinfo_t *ri_generated = router_get_my_routerinfo();
|
|
|
|
tor_assert(ri_generated != ri);
|
|
|
|
}
|
2007-11-07 18:11:23 +01:00
|
|
|
tor_assert(ri->cache_info.routerlist_index == -1);
|
2007-05-19 02:32:00 +02:00
|
|
|
|
2007-06-12 00:19:40 +02:00
|
|
|
ri_old = rimap_set(rl->identity_map, ri->cache_info.identity_digest, ri);
|
2007-03-11 21:34:44 +01:00
|
|
|
tor_assert(!ri_old);
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_set(rl->desc_digest_map, ri->cache_info.signed_descriptor_digest,
|
|
|
|
&(ri->cache_info));
|
2007-05-18 23:19:53 +02:00
|
|
|
if (!tor_digest_is_zero(ri->cache_info.extra_info_digest))
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_set(rl->desc_by_eid_map, ri->cache_info.extra_info_digest,
|
|
|
|
&ri->cache_info);
|
2005-10-18 19:43:54 +02:00
|
|
|
smartlist_add(rl->routers, ri);
|
2007-11-07 18:11:23 +01:00
|
|
|
ri->cache_info.routerlist_index = smartlist_len(rl->routers) - 1;
|
2006-08-15 05:54:09 +02:00
|
|
|
router_dir_info_changed();
|
2007-05-19 20:31:04 +02:00
|
|
|
#ifdef DEBUG_ROUTERLIST
|
|
|
|
routerlist_assert_ok(rl);
|
|
|
|
#endif
|
2005-10-18 19:43:54 +02:00
|
|
|
}
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/** Adds the extrainfo_t <b>ei</b> to the routerlist <b>rl</b>, if there is a
|
|
|
|
* corresponding router in rl-\>routers or rl-\>old_routers. Return true iff
|
|
|
|
* we actually inserted <b>ei</b>. Free <b>ei</b> if it isn't inserted. */
|
2007-04-30 21:48:39 +02:00
|
|
|
static int
|
2007-04-16 23:37:21 +02:00
|
|
|
extrainfo_insert(routerlist_t *rl, extrainfo_t *ei)
|
|
|
|
{
|
2007-04-30 21:48:39 +02:00
|
|
|
int r = 0;
|
2007-06-12 00:19:40 +02:00
|
|
|
routerinfo_t *ri = rimap_get(rl->identity_map,
|
|
|
|
ei->cache_info.identity_digest);
|
2007-10-21 02:08:35 +02:00
|
|
|
signed_descriptor_t *sd =
|
|
|
|
sdmap_get(rl->desc_by_eid_map, ei->cache_info.signed_descriptor_digest);
|
2007-04-16 23:37:21 +02:00
|
|
|
extrainfo_t *ei_tmp;
|
2007-05-19 22:08:44 +02:00
|
|
|
|
2007-05-20 22:16:45 +02:00
|
|
|
{
|
2008-02-06 00:20:44 +01:00
|
|
|
/* XXXX remove this code if it slows us down. */
|
2007-05-20 22:16:45 +02:00
|
|
|
extrainfo_t *ei_generated = router_get_my_extrainfo();
|
|
|
|
tor_assert(ei_generated != ei);
|
|
|
|
}
|
|
|
|
|
2007-05-19 22:08:44 +02:00
|
|
|
if (!ri) {
|
|
|
|
/* This router is unknown; we can't even verify the signature. Give up.*/
|
|
|
|
goto done;
|
|
|
|
}
|
2007-10-21 02:08:35 +02:00
|
|
|
if (routerinfo_incompatible_with_extrainfo(ri, ei, sd, NULL)) {
|
|
|
|
goto done;
|
2007-04-16 23:37:21 +02:00
|
|
|
}
|
|
|
|
|
2007-05-19 22:08:44 +02:00
|
|
|
/* Okay, if we make it here, we definitely have a router corresponding to
|
|
|
|
* this extrainfo. */
|
|
|
|
|
2007-06-12 00:19:40 +02:00
|
|
|
ei_tmp = eimap_set(rl->extra_info_map,
|
|
|
|
ei->cache_info.signed_descriptor_digest,
|
|
|
|
ei);
|
2007-04-30 21:48:39 +02:00
|
|
|
r = 1;
|
2007-05-22 03:51:53 +02:00
|
|
|
if (ei_tmp) {
|
2007-08-31 17:08:37 +02:00
|
|
|
rl->extrainfo_store.bytes_dropped +=
|
2007-05-22 03:51:53 +02:00
|
|
|
ei_tmp->cache_info.signed_descriptor_len;
|
2007-04-16 23:37:21 +02:00
|
|
|
extrainfo_free(ei_tmp);
|
2007-05-22 03:51:53 +02:00
|
|
|
}
|
2007-04-30 16:26:38 +02:00
|
|
|
|
|
|
|
done:
|
2007-05-19 22:08:44 +02:00
|
|
|
if (r == 0)
|
|
|
|
extrainfo_free(ei);
|
|
|
|
|
2007-05-19 20:31:04 +02:00
|
|
|
#ifdef DEBUG_ROUTERLIST
|
|
|
|
routerlist_assert_ok(rl);
|
|
|
|
#endif
|
2007-04-30 21:48:39 +02:00
|
|
|
return r;
|
2007-04-16 23:37:21 +02:00
|
|
|
}
|
|
|
|
|
2007-12-01 05:58:53 +01:00
|
|
|
#define should_cache_old_descriptors() \
|
|
|
|
directory_caches_dir_info(get_options())
|
2007-10-09 22:44:47 +02:00
|
|
|
|
2007-02-26 06:33:17 +01:00
|
|
|
/** If we're a directory cache and routerlist <b>rl</b> doesn't have
|
|
|
|
* a copy of router <b>ri</b> yet, add it to the list of old (not
|
|
|
|
* recommended but still served) descriptors. Else free it. */
|
2005-10-27 02:34:39 +02:00
|
|
|
static void
|
|
|
|
routerlist_insert_old(routerlist_t *rl, routerinfo_t *ri)
|
|
|
|
{
|
2007-05-19 02:32:00 +02:00
|
|
|
{
|
2008-02-06 00:20:44 +01:00
|
|
|
/* XXXX remove this code if it slows us down. */
|
2007-05-19 02:32:00 +02:00
|
|
|
routerinfo_t *ri_generated = router_get_my_routerinfo();
|
|
|
|
tor_assert(ri_generated != ri);
|
|
|
|
}
|
2007-11-07 18:11:23 +01:00
|
|
|
tor_assert(ri->cache_info.routerlist_index == -1);
|
2007-06-02 14:44:57 +02:00
|
|
|
|
2007-10-09 22:44:47 +02:00
|
|
|
if (should_cache_old_descriptors() &&
|
2007-03-21 16:37:30 +01:00
|
|
|
ri->purpose == ROUTER_PURPOSE_GENERAL &&
|
2007-06-12 00:19:40 +02:00
|
|
|
!sdmap_get(rl->desc_digest_map,
|
|
|
|
ri->cache_info.signed_descriptor_digest)) {
|
2005-11-05 21:15:27 +01:00
|
|
|
signed_descriptor_t *sd = signed_descriptor_from_routerinfo(ri);
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_set(rl->desc_digest_map, sd->signed_descriptor_digest, sd);
|
2005-11-05 21:15:27 +01:00
|
|
|
smartlist_add(rl->old_routers, sd);
|
2007-11-07 18:11:23 +01:00
|
|
|
sd->routerlist_index = smartlist_len(rl->old_routers)-1;
|
2007-05-18 23:19:53 +02:00
|
|
|
if (!tor_digest_is_zero(sd->extra_info_digest))
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_set(rl->desc_by_eid_map, sd->extra_info_digest, sd);
|
2005-10-27 02:34:39 +02:00
|
|
|
} else {
|
|
|
|
routerinfo_free(ri);
|
|
|
|
}
|
2007-05-19 20:31:04 +02:00
|
|
|
#ifdef DEBUG_ROUTERLIST
|
|
|
|
routerlist_assert_ok(rl);
|
|
|
|
#endif
|
2005-10-27 02:34:39 +02:00
|
|
|
}
|
|
|
|
|
2006-03-24 22:11:14 +01:00
|
|
|
/** Remove an item <b>ri</b> from the routerlist <b>rl</b>, updating indices
|
2005-10-18 19:43:54 +02:00
|
|
|
* as needed. If <b>idx</b> is nonnegative and smartlist_get(rl->routers,
|
|
|
|
* idx) == ri, we don't need to do a linear search over the list to decide
|
2005-11-01 04:48:51 +01:00
|
|
|
* which to remove. We fill the gap in rl->routers with a later element in
|
2007-03-21 16:37:30 +01:00
|
|
|
* the list, if any exists. <b>ri</b> is freed.
|
|
|
|
*
|
2007-03-26 16:08:29 +02:00
|
|
|
* If <b>make_old</b> is true, instead of deleting the router, we try adding
|
|
|
|
* it to rl->old_routers. */
|
2005-10-18 19:43:54 +02:00
|
|
|
void
|
2007-07-18 16:02:47 +02:00
|
|
|
routerlist_remove(routerlist_t *rl, routerinfo_t *ri, int make_old)
|
2005-10-18 19:43:54 +02:00
|
|
|
{
|
2005-10-18 22:13:09 +02:00
|
|
|
routerinfo_t *ri_tmp;
|
2007-04-16 23:37:21 +02:00
|
|
|
extrainfo_t *ei_tmp;
|
2007-11-07 18:11:23 +01:00
|
|
|
int idx = ri->cache_info.routerlist_index;
|
2007-07-18 16:02:47 +02:00
|
|
|
tor_assert(0 <= idx && idx < smartlist_len(rl->routers));
|
|
|
|
tor_assert(smartlist_get(rl->routers, idx) == ri);
|
|
|
|
|
2007-11-07 18:11:23 +01:00
|
|
|
ri->cache_info.routerlist_index = -1;
|
2005-10-18 19:43:54 +02:00
|
|
|
smartlist_del(rl->routers, idx);
|
2006-09-20 01:18:30 +02:00
|
|
|
if (idx < smartlist_len(rl->routers)) {
|
|
|
|
routerinfo_t *r = smartlist_get(rl->routers, idx);
|
2007-11-07 18:11:23 +01:00
|
|
|
r->cache_info.routerlist_index = idx;
|
2006-09-20 01:18:30 +02:00
|
|
|
}
|
|
|
|
|
2007-06-12 00:19:40 +02:00
|
|
|
ri_tmp = rimap_remove(rl->identity_map, ri->cache_info.identity_digest);
|
2006-08-15 05:54:09 +02:00
|
|
|
router_dir_info_changed();
|
2005-10-18 22:13:09 +02:00
|
|
|
tor_assert(ri_tmp == ri);
|
2007-04-16 23:37:21 +02:00
|
|
|
|
2007-10-09 22:44:47 +02:00
|
|
|
if (make_old && should_cache_old_descriptors() &&
|
2007-03-21 16:37:30 +01:00
|
|
|
ri->purpose == ROUTER_PURPOSE_GENERAL) {
|
2005-11-23 08:24:59 +01:00
|
|
|
signed_descriptor_t *sd;
|
|
|
|
sd = signed_descriptor_from_routerinfo(ri);
|
2005-11-05 21:15:27 +01:00
|
|
|
smartlist_add(rl->old_routers, sd);
|
2007-11-07 18:11:23 +01:00
|
|
|
sd->routerlist_index = smartlist_len(rl->old_routers)-1;
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_set(rl->desc_digest_map, sd->signed_descriptor_digest, sd);
|
2007-05-19 20:48:53 +02:00
|
|
|
if (!tor_digest_is_zero(sd->extra_info_digest))
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_set(rl->desc_by_eid_map, sd->extra_info_digest, sd);
|
2005-10-27 02:34:39 +02:00
|
|
|
} else {
|
2007-06-12 00:19:40 +02:00
|
|
|
signed_descriptor_t *sd_tmp;
|
|
|
|
sd_tmp = sdmap_remove(rl->desc_digest_map,
|
|
|
|
ri->cache_info.signed_descriptor_digest);
|
|
|
|
tor_assert(sd_tmp == &(ri->cache_info));
|
2007-10-22 18:32:01 +02:00
|
|
|
rl->desc_store.bytes_dropped += ri->cache_info.signed_descriptor_len;
|
2007-06-12 00:19:40 +02:00
|
|
|
ei_tmp = eimap_remove(rl->extra_info_map,
|
|
|
|
ri->cache_info.extra_info_digest);
|
2007-04-30 21:48:39 +02:00
|
|
|
if (ei_tmp) {
|
2007-08-31 17:08:37 +02:00
|
|
|
rl->extrainfo_store.bytes_dropped +=
|
2007-04-30 21:48:39 +02:00
|
|
|
ei_tmp->cache_info.signed_descriptor_len;
|
2007-04-16 23:37:21 +02:00
|
|
|
extrainfo_free(ei_tmp);
|
2007-04-30 21:48:39 +02:00
|
|
|
}
|
2007-05-18 23:19:53 +02:00
|
|
|
if (!tor_digest_is_zero(ri->cache_info.extra_info_digest))
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_remove(rl->desc_by_eid_map, ri->cache_info.extra_info_digest);
|
2007-05-18 23:19:53 +02:00
|
|
|
routerinfo_free(ri);
|
2005-10-27 02:34:39 +02:00
|
|
|
}
|
2007-05-19 20:31:04 +02:00
|
|
|
#ifdef DEBUG_ROUTERLIST
|
|
|
|
routerlist_assert_ok(rl);
|
|
|
|
#endif
|
2005-10-27 02:34:39 +02:00
|
|
|
}
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/** Remove a signed_descriptor_t <b>sd</b> from <b>rl</b>-\>old_routers, and
|
|
|
|
* adjust <b>rl</b> as appropriate. <b>idx</i> is -1, or the index of
|
|
|
|
* <b>sd</b>. */
|
2005-11-01 18:34:17 +01:00
|
|
|
static void
|
2005-11-05 21:15:27 +01:00
|
|
|
routerlist_remove_old(routerlist_t *rl, signed_descriptor_t *sd, int idx)
|
2005-10-27 02:34:39 +02:00
|
|
|
{
|
2005-11-05 21:15:27 +01:00
|
|
|
signed_descriptor_t *sd_tmp;
|
2007-04-16 23:37:21 +02:00
|
|
|
extrainfo_t *ei_tmp;
|
2007-08-31 17:08:37 +02:00
|
|
|
desc_store_t *store;
|
2007-11-07 18:11:23 +01:00
|
|
|
if (idx == -1) {
|
|
|
|
idx = sd->routerlist_index;
|
|
|
|
}
|
2007-07-18 16:02:47 +02:00
|
|
|
tor_assert(0 <= idx && idx < smartlist_len(rl->old_routers));
|
2007-12-22 06:26:36 +01:00
|
|
|
/* XXX020 edmanm's bridge relay triggered the following assert while
|
2008-02-06 00:20:44 +01:00
|
|
|
* running 0.2.0.12-alpha. */
|
2007-07-18 16:02:47 +02:00
|
|
|
tor_assert(smartlist_get(rl->old_routers, idx) == sd);
|
2007-11-07 18:11:23 +01:00
|
|
|
tor_assert(idx == sd->routerlist_index);
|
2007-07-18 16:02:47 +02:00
|
|
|
|
2007-11-07 18:11:23 +01:00
|
|
|
sd->routerlist_index = -1;
|
2005-10-27 02:34:39 +02:00
|
|
|
smartlist_del(rl->old_routers, idx);
|
2007-11-07 18:11:23 +01:00
|
|
|
if (idx < smartlist_len(rl->old_routers)) {
|
|
|
|
signed_descriptor_t *d = smartlist_get(rl->old_routers, idx);
|
|
|
|
d->routerlist_index = idx;
|
|
|
|
}
|
2007-06-12 00:19:40 +02:00
|
|
|
sd_tmp = sdmap_remove(rl->desc_digest_map,
|
|
|
|
sd->signed_descriptor_digest);
|
2005-11-05 21:15:27 +01:00
|
|
|
tor_assert(sd_tmp == sd);
|
2007-08-31 17:08:37 +02:00
|
|
|
store = desc_get_store(rl, sd);
|
|
|
|
if (store)
|
|
|
|
store->bytes_dropped += sd->signed_descriptor_len;
|
2007-04-16 23:37:21 +02:00
|
|
|
|
2007-06-12 00:19:40 +02:00
|
|
|
ei_tmp = eimap_remove(rl->extra_info_map,
|
|
|
|
sd->extra_info_digest);
|
2007-04-30 21:48:39 +02:00
|
|
|
if (ei_tmp) {
|
2007-08-31 17:08:37 +02:00
|
|
|
rl->extrainfo_store.bytes_dropped +=
|
2007-04-30 21:48:39 +02:00
|
|
|
ei_tmp->cache_info.signed_descriptor_len;
|
2007-04-16 23:37:21 +02:00
|
|
|
extrainfo_free(ei_tmp);
|
2007-04-30 21:48:39 +02:00
|
|
|
}
|
2007-05-18 23:19:53 +02:00
|
|
|
if (!tor_digest_is_zero(sd->extra_info_digest))
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_remove(rl->desc_by_eid_map, sd->extra_info_digest);
|
2007-04-16 23:37:21 +02:00
|
|
|
|
2007-05-18 23:19:53 +02:00
|
|
|
signed_descriptor_free(sd);
|
2007-05-19 20:31:04 +02:00
|
|
|
#ifdef DEBUG_ROUTERLIST
|
|
|
|
routerlist_assert_ok(rl);
|
|
|
|
#endif
|
2005-10-18 19:43:54 +02:00
|
|
|
}
|
|
|
|
|
2005-10-19 05:08:50 +02:00
|
|
|
/** Remove <b>ri_old</b> from the routerlist <b>rl</b>, and replace it with
|
|
|
|
* <b>ri_new</b>, updating all index info. If <b>idx</b> is nonnegative and
|
|
|
|
* smartlist_get(rl->routers, idx) == ri, we don't need to do a linear
|
|
|
|
* search over the list to decide which to remove. We put ri_new in the same
|
2007-03-21 16:37:30 +01:00
|
|
|
* index as ri_old, if possible. ri is freed as appropriate.
|
|
|
|
*
|
2007-12-18 22:37:58 +01:00
|
|
|
* If should_cache_descriptors() is true, instead of deleting the router,
|
|
|
|
* we add it to rl->old_routers. */
|
2005-10-19 05:02:28 +02:00
|
|
|
static void
|
|
|
|
routerlist_replace(routerlist_t *rl, routerinfo_t *ri_old,
|
2007-07-06 20:40:39 +02:00
|
|
|
routerinfo_t *ri_new)
|
2005-10-19 05:02:28 +02:00
|
|
|
{
|
2007-07-06 20:40:39 +02:00
|
|
|
int idx;
|
|
|
|
|
2007-03-11 21:34:44 +01:00
|
|
|
routerinfo_t *ri_tmp;
|
2007-04-16 23:37:21 +02:00
|
|
|
extrainfo_t *ei_tmp;
|
2007-05-19 02:32:00 +02:00
|
|
|
{
|
2008-02-06 00:20:44 +01:00
|
|
|
/* XXXX Remove this if it turns out to slow us down. */
|
2007-05-19 02:32:00 +02:00
|
|
|
routerinfo_t *ri_generated = router_get_my_routerinfo();
|
|
|
|
tor_assert(ri_generated != ri_new);
|
|
|
|
}
|
2005-10-28 01:06:09 +02:00
|
|
|
tor_assert(ri_old != ri_new);
|
2007-11-07 18:11:23 +01:00
|
|
|
tor_assert(ri_new->cache_info.routerlist_index == -1);
|
2007-06-02 14:44:57 +02:00
|
|
|
|
2007-11-07 18:11:23 +01:00
|
|
|
idx = ri_old->cache_info.routerlist_index;
|
2007-07-06 20:40:39 +02:00
|
|
|
tor_assert(0 <= idx && idx < smartlist_len(rl->routers));
|
|
|
|
tor_assert(smartlist_get(rl->routers, idx) == ri_old);
|
|
|
|
|
2006-08-15 05:54:09 +02:00
|
|
|
router_dir_info_changed();
|
2005-10-19 05:02:28 +02:00
|
|
|
if (idx >= 0) {
|
|
|
|
smartlist_set(rl->routers, idx, ri_new);
|
2007-11-07 18:11:23 +01:00
|
|
|
ri_old->cache_info.routerlist_index = -1;
|
|
|
|
ri_new->cache_info.routerlist_index = idx;
|
2007-07-12 19:37:47 +02:00
|
|
|
/* Check that ri_old is not in rl->routers anymore: */
|
2007-07-02 22:04:26 +02:00
|
|
|
tor_assert( _routerlist_find_elt(rl->routers, ri_old, -1) == -1 );
|
2005-10-19 05:02:28 +02:00
|
|
|
} else {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_BUG, "Appending entry from routerlist_replace.");
|
2005-10-28 01:06:09 +02:00
|
|
|
routerlist_insert(rl, ri_new);
|
|
|
|
return;
|
2005-10-19 05:02:28 +02:00
|
|
|
}
|
2005-12-14 21:40:40 +01:00
|
|
|
if (memcmp(ri_old->cache_info.identity_digest,
|
|
|
|
ri_new->cache_info.identity_digest, DIGEST_LEN)) {
|
2005-10-19 05:02:28 +02:00
|
|
|
/* digests don't match; digestmap_set won't replace */
|
2007-06-12 00:19:40 +02:00
|
|
|
rimap_remove(rl->identity_map, ri_old->cache_info.identity_digest);
|
2005-10-19 05:02:28 +02:00
|
|
|
}
|
2007-06-12 00:19:40 +02:00
|
|
|
ri_tmp = rimap_set(rl->identity_map,
|
|
|
|
ri_new->cache_info.identity_digest, ri_new);
|
2007-03-11 22:10:54 +01:00
|
|
|
tor_assert(!ri_tmp || ri_tmp == ri_old);
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_set(rl->desc_digest_map,
|
|
|
|
ri_new->cache_info.signed_descriptor_digest,
|
|
|
|
&(ri_new->cache_info));
|
2005-10-27 02:34:39 +02:00
|
|
|
|
2007-06-02 16:24:23 +02:00
|
|
|
if (!tor_digest_is_zero(ri_new->cache_info.extra_info_digest)) {
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_set(rl->desc_by_eid_map, ri_new->cache_info.extra_info_digest,
|
|
|
|
&ri_new->cache_info);
|
2007-06-02 16:24:23 +02:00
|
|
|
}
|
2007-05-19 20:31:04 +02:00
|
|
|
|
2007-10-09 22:44:47 +02:00
|
|
|
if (should_cache_old_descriptors() &&
|
2007-03-21 16:37:30 +01:00
|
|
|
ri_old->purpose == ROUTER_PURPOSE_GENERAL) {
|
2005-11-05 21:15:27 +01:00
|
|
|
signed_descriptor_t *sd = signed_descriptor_from_routerinfo(ri_old);
|
|
|
|
smartlist_add(rl->old_routers, sd);
|
2007-11-07 18:11:23 +01:00
|
|
|
sd->routerlist_index = smartlist_len(rl->old_routers)-1;
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_set(rl->desc_digest_map, sd->signed_descriptor_digest, sd);
|
2007-05-19 20:31:04 +02:00
|
|
|
if (!tor_digest_is_zero(sd->extra_info_digest))
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_set(rl->desc_by_eid_map, sd->extra_info_digest, sd);
|
2005-10-27 02:34:39 +02:00
|
|
|
} else {
|
2005-11-05 21:15:27 +01:00
|
|
|
if (memcmp(ri_old->cache_info.signed_descriptor_digest,
|
|
|
|
ri_new->cache_info.signed_descriptor_digest,
|
2005-10-27 02:34:39 +02:00
|
|
|
DIGEST_LEN)) {
|
|
|
|
/* digests don't match; digestmap_set didn't replace */
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_remove(rl->desc_digest_map,
|
|
|
|
ri_old->cache_info.signed_descriptor_digest);
|
2007-05-19 20:12:31 +02:00
|
|
|
}
|
2007-04-16 23:37:21 +02:00
|
|
|
|
2007-06-12 00:19:40 +02:00
|
|
|
ei_tmp = eimap_remove(rl->extra_info_map,
|
|
|
|
ri_old->cache_info.extra_info_digest);
|
2007-05-19 20:12:31 +02:00
|
|
|
if (ei_tmp) {
|
2007-08-31 17:08:37 +02:00
|
|
|
rl->extrainfo_store.bytes_dropped +=
|
2007-05-19 20:12:31 +02:00
|
|
|
ei_tmp->cache_info.signed_descriptor_len;
|
|
|
|
extrainfo_free(ei_tmp);
|
2005-10-27 02:34:39 +02:00
|
|
|
}
|
2007-06-02 16:24:23 +02:00
|
|
|
if (!tor_digest_is_zero(ri_old->cache_info.extra_info_digest)) {
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_remove(rl->desc_by_eid_map,
|
|
|
|
ri_old->cache_info.extra_info_digest);
|
2007-06-02 16:24:23 +02:00
|
|
|
}
|
2007-10-22 18:32:01 +02:00
|
|
|
rl->desc_store.bytes_dropped += ri_old->cache_info.signed_descriptor_len;
|
2005-10-27 02:34:39 +02:00
|
|
|
routerinfo_free(ri_old);
|
|
|
|
}
|
2007-05-19 20:31:04 +02:00
|
|
|
#ifdef DEBUG_ROUTERLIST
|
|
|
|
routerlist_assert_ok(rl);
|
|
|
|
#endif
|
2005-10-19 05:02:28 +02:00
|
|
|
}
|
|
|
|
|
2007-12-19 04:55:40 +01:00
|
|
|
/** Extract the descriptor <b>sd</b> from old_routerlist, and re-parse
|
|
|
|
* it as a fresh routerinfo_t. */
|
2007-11-07 16:19:53 +01:00
|
|
|
static routerinfo_t *
|
2007-11-07 18:11:23 +01:00
|
|
|
routerlist_reparse_old(routerlist_t *rl, signed_descriptor_t *sd)
|
2007-11-07 16:19:53 +01:00
|
|
|
{
|
|
|
|
routerinfo_t *ri;
|
|
|
|
const char *body;
|
|
|
|
|
2007-12-19 04:55:40 +01:00
|
|
|
body = signed_descriptor_get_annotations(sd);
|
2007-11-07 16:19:53 +01:00
|
|
|
|
2007-12-19 04:55:40 +01:00
|
|
|
ri = router_parse_entry_from_string(body,
|
|
|
|
body+sd->signed_descriptor_len+sd->annotations_len,
|
|
|
|
0, 1, NULL);
|
2007-11-07 16:19:53 +01:00
|
|
|
if (!ri)
|
|
|
|
return NULL;
|
|
|
|
memcpy(&ri->cache_info, sd, sizeof(signed_descriptor_t));
|
2007-11-07 18:11:23 +01:00
|
|
|
sd->signed_descriptor_body = NULL; /* Steal reference. */
|
2007-11-07 18:41:14 +01:00
|
|
|
ri->cache_info.routerlist_index = -1;
|
2007-11-07 16:19:53 +01:00
|
|
|
|
2007-11-07 18:11:23 +01:00
|
|
|
routerlist_remove_old(rl, sd, -1);
|
2007-11-07 16:19:53 +01:00
|
|
|
|
|
|
|
return ri;
|
|
|
|
}
|
|
|
|
|
2005-11-01 04:48:51 +01:00
|
|
|
/** Free all memory held by the routerlist module. */
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
2005-09-15 01:42:06 +02:00
|
|
|
routerlist_free_all(void)
|
2005-02-11 00:18:39 +01:00
|
|
|
{
|
2005-02-25 00:01:26 +01:00
|
|
|
if (routerlist)
|
|
|
|
routerlist_free(routerlist);
|
2005-02-11 00:18:39 +01:00
|
|
|
routerlist = NULL;
|
2005-04-03 07:53:34 +02:00
|
|
|
if (warned_nicknames) {
|
|
|
|
SMARTLIST_FOREACH(warned_nicknames, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(warned_nicknames);
|
|
|
|
warned_nicknames = NULL;
|
|
|
|
}
|
2005-09-15 01:42:06 +02:00
|
|
|
if (trusted_dir_servers) {
|
|
|
|
SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ds,
|
2005-10-05 07:03:52 +02:00
|
|
|
trusted_dir_server_free(ds));
|
2005-09-15 01:42:06 +02:00
|
|
|
smartlist_free(trusted_dir_servers);
|
|
|
|
trusted_dir_servers = NULL;
|
|
|
|
}
|
2005-09-02 22:37:31 +02:00
|
|
|
}
|
|
|
|
|
2005-10-05 04:20:46 +02:00
|
|
|
/** Forget that we have issued any router-related warnings, so that we'll
|
|
|
|
* warn again if we see the same errors. */
|
|
|
|
void
|
|
|
|
routerlist_reset_warnings(void)
|
|
|
|
{
|
|
|
|
if (!warned_nicknames)
|
|
|
|
warned_nicknames = smartlist_create();
|
|
|
|
SMARTLIST_FOREACH(warned_nicknames, char *, cp, tor_free(cp));
|
|
|
|
smartlist_clear(warned_nicknames); /* now the list is empty. */
|
|
|
|
|
2007-10-08 23:21:50 +02:00
|
|
|
networkstatus_reset_warnings();
|
2005-10-05 04:20:46 +02:00
|
|
|
}
|
|
|
|
|
2006-03-18 02:24:04 +01:00
|
|
|
/** Mark the router with ID <b>digest</b> as running or non-running
|
|
|
|
* in our routerlist. */
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
2006-03-18 02:24:04 +01:00
|
|
|
router_set_status(const char *digest, int up)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2004-05-12 21:49:48 +02:00
|
|
|
routerinfo_t *router;
|
2007-10-09 17:27:15 +02:00
|
|
|
routerstatus_t *status;
|
2004-07-03 01:40:03 +02:00
|
|
|
tor_assert(digest);
|
2004-10-12 17:55:20 +02:00
|
|
|
|
|
|
|
SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, d,
|
|
|
|
if (!memcmp(d->digest, digest, DIGEST_LEN))
|
2006-03-18 02:24:04 +01:00
|
|
|
d->is_running = up);
|
2004-10-12 17:55:20 +02:00
|
|
|
|
2004-07-03 01:40:03 +02:00
|
|
|
router = router_get_by_digest(digest);
|
2005-12-14 23:05:10 +01:00
|
|
|
if (router) {
|
2006-03-18 02:24:04 +01:00
|
|
|
log_debug(LD_DIR,"Marking router '%s' as %s.",
|
|
|
|
router->nickname, up ? "up" : "down");
|
|
|
|
if (!up && router_is_me(router) && !we_are_hibernating())
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_NET, "We just marked ourself as down. Are your external "
|
|
|
|
"addresses reachable?");
|
2006-03-18 02:24:04 +01:00
|
|
|
router->is_running = up;
|
2005-12-14 23:05:10 +01:00
|
|
|
}
|
2007-10-16 01:15:24 +02:00
|
|
|
status = router_get_consensus_status_by_id(digest);
|
2007-10-09 17:27:15 +02:00
|
|
|
if (status && status->is_running != up) {
|
|
|
|
status->is_running = up;
|
2006-10-20 23:04:39 +02:00
|
|
|
control_event_networkstatus_changed_single(status);
|
2005-12-14 23:05:10 +01:00
|
|
|
}
|
2006-08-15 05:54:09 +02:00
|
|
|
router_dir_info_changed();
|
2002-09-26 15:17:14 +02:00
|
|
|
}
|
|
|
|
|
2004-05-17 22:31:01 +02:00
|
|
|
/** Add <b>router</b> to the routerlist, if we don't already have it. Replace
|
2005-09-15 08:15:31 +02:00
|
|
|
* older entries (if any) with the same key. Note: Callers should not hold
|
2005-05-14 07:01:41 +02:00
|
|
|
* their pointers to <b>router</b> if this function fails; <b>router</b>
|
2007-07-16 02:11:03 +02:00
|
|
|
* will either be inserted into the routerlist or freed. Similarly, even
|
|
|
|
* if this call succeeds, they should not hold their pointers to
|
|
|
|
* <b>router</b> after subsequent calls with other routerinfo's -- they
|
|
|
|
* might cause the original routerinfo to get freed.
|
2005-04-03 00:02:13 +02:00
|
|
|
*
|
2005-08-31 08:14:37 +02:00
|
|
|
* Returns >= 0 if the router was added; less than 0 if it was not.
|
|
|
|
*
|
|
|
|
* If we're returning non-zero, then assign to *<b>msg</b> a static string
|
|
|
|
* describing the reason for not liking the routerinfo.
|
2005-08-26 23:46:24 +02:00
|
|
|
*
|
|
|
|
* If the return value is less than -1, there was a problem with the
|
2005-08-31 08:14:37 +02:00
|
|
|
* routerinfo. If the return value is equal to -1, then the routerinfo was
|
|
|
|
* fine, but out-of-date. If the return value is equal to 1, the
|
|
|
|
* routerinfo was accepted, but we should notify the generator of the
|
|
|
|
* descriptor using the message *<b>msg</b>.
|
2005-09-15 02:51:42 +02:00
|
|
|
*
|
2006-01-04 05:42:10 +01:00
|
|
|
* If <b>from_cache</b>, this descriptor came from our disk cache. If
|
|
|
|
* <b>from_fetch</b>, we received it in response to a request we made.
|
|
|
|
* (If both are false, that means it was uploaded to us as an auth dir
|
|
|
|
* server or via the controller.)
|
|
|
|
*
|
2005-09-15 08:15:31 +02:00
|
|
|
* This function should be called *after*
|
2007-10-16 01:15:24 +02:00
|
|
|
* routers_update_status_from_consensus_networkstatus; subsequently, you
|
|
|
|
* should call router_rebuild_store and routerlist_descriptors_added.
|
2004-05-17 22:31:01 +02:00
|
|
|
*/
|
2005-08-26 22:59:04 +02:00
|
|
|
int
|
2005-09-15 07:19:38 +02:00
|
|
|
router_add_to_routerlist(routerinfo_t *router, const char **msg,
|
2006-01-04 05:42:10 +01:00
|
|
|
int from_cache, int from_fetch)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2006-03-17 06:50:41 +01:00
|
|
|
const char *id_digest;
|
2007-12-19 00:45:24 +01:00
|
|
|
int authdir = authdir_mode_handles_descs(get_options(), router->purpose);
|
2006-03-19 02:21:59 +01:00
|
|
|
int authdir_believes_valid = 0;
|
2006-09-20 01:18:30 +02:00
|
|
|
routerinfo_t *old_router;
|
2008-02-05 22:39:29 +01:00
|
|
|
networkstatus_t *consensus = networkstatus_get_latest_consensus();
|
2007-10-16 01:15:24 +02:00
|
|
|
const smartlist_t *networkstatus_v2_list = networkstatus_get_v2_list();
|
|
|
|
int in_consensus = 0;
|
2004-08-07 05:38:07 +02:00
|
|
|
|
2005-08-27 01:12:13 +02:00
|
|
|
tor_assert(msg);
|
|
|
|
|
2005-10-18 22:13:09 +02:00
|
|
|
if (!routerlist)
|
|
|
|
router_get_routerlist();
|
2005-08-26 23:12:34 +02:00
|
|
|
|
2006-03-17 06:50:41 +01:00
|
|
|
id_digest = router->cache_info.identity_digest;
|
2005-10-27 02:34:39 +02:00
|
|
|
|
|
|
|
/* Make sure that we haven't already got this exact descriptor. */
|
2007-06-12 00:19:40 +02:00
|
|
|
if (sdmap_get(routerlist->desc_digest_map,
|
|
|
|
router->cache_info.signed_descriptor_digest)) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_DIR,
|
|
|
|
"Dropping descriptor that we already have for router '%s'",
|
|
|
|
router->nickname);
|
2005-10-27 02:34:39 +02:00
|
|
|
*msg = "Router descriptor was not new.";
|
2005-10-28 20:44:51 +02:00
|
|
|
routerinfo_free(router);
|
2005-10-27 02:34:39 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2004-08-07 05:38:07 +02:00
|
|
|
|
2005-08-26 22:59:04 +02:00
|
|
|
if (authdir) {
|
2006-01-04 05:42:10 +01:00
|
|
|
if (authdir_wants_to_reject_router(router, msg,
|
|
|
|
!from_cache && !from_fetch)) {
|
2006-01-12 04:43:39 +01:00
|
|
|
tor_assert(*msg);
|
2005-10-28 20:44:51 +02:00
|
|
|
routerinfo_free(router);
|
2005-08-26 23:46:24 +02:00
|
|
|
return -2;
|
2005-10-28 20:44:51 +02:00
|
|
|
}
|
2006-03-19 02:21:59 +01:00
|
|
|
authdir_believes_valid = router->is_valid;
|
2006-01-12 04:43:39 +01:00
|
|
|
} else if (from_fetch) {
|
2005-12-27 06:26:03 +01:00
|
|
|
/* Only check the descriptor digest against the network statuses when
|
2006-01-12 04:43:39 +01:00
|
|
|
* we are receiving in response to a fetch. */
|
2007-02-22 08:41:10 +01:00
|
|
|
|
2007-06-12 11:17:23 +02:00
|
|
|
if (!signed_desc_digest_is_recognized(&router->cache_info) &&
|
2007-07-18 12:06:03 +02:00
|
|
|
!routerinfo_is_a_configured_bridge(router)) {
|
2007-02-22 08:41:10 +01:00
|
|
|
/* We asked for it, so some networkstatus must have listed it when we
|
2007-02-24 22:21:38 +01:00
|
|
|
* did. Save it if we're a cache in case somebody else asks for it. */
|
2007-02-22 08:41:10 +01:00
|
|
|
log_info(LD_DIR,
|
|
|
|
"Received a no-longer-recognized descriptor for router '%s'",
|
2006-02-13 11:33:00 +01:00
|
|
|
router->nickname);
|
2006-01-12 04:43:39 +01:00
|
|
|
*msg = "Router descriptor is not referenced by any network-status.";
|
2007-02-22 08:41:10 +01:00
|
|
|
|
|
|
|
/* Only journal this desc if we'll be serving it. */
|
2007-10-09 22:44:47 +02:00
|
|
|
if (!from_cache && should_cache_old_descriptors())
|
2007-08-31 17:08:37 +02:00
|
|
|
signed_desc_append_to_journal(&router->cache_info,
|
2007-10-22 18:32:01 +02:00
|
|
|
&routerlist->desc_store);
|
2007-02-22 08:41:10 +01:00
|
|
|
routerlist_insert_old(routerlist, router);
|
2005-10-12 15:49:13 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2005-08-26 22:59:04 +02:00
|
|
|
}
|
|
|
|
|
2006-03-27 07:07:57 +02:00
|
|
|
/* We no longer need a router with this descriptor digest. */
|
2007-10-16 01:15:24 +02:00
|
|
|
SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
|
2006-03-27 07:07:57 +02:00
|
|
|
{
|
|
|
|
routerstatus_t *rs =
|
2007-10-16 01:15:24 +02:00
|
|
|
networkstatus_v2_find_entry(ns, router->cache_info.identity_digest);
|
2006-03-27 07:07:57 +02:00
|
|
|
if (rs && !memcmp(rs->descriptor_digest,
|
|
|
|
router->cache_info.signed_descriptor_digest,
|
|
|
|
DIGEST_LEN))
|
|
|
|
rs->need_to_mirror = 0;
|
|
|
|
});
|
2007-10-16 01:15:24 +02:00
|
|
|
if (consensus) {
|
|
|
|
routerstatus_t *rs = networkstatus_vote_find_entry(consensus,
|
|
|
|
router->cache_info.identity_digest);
|
|
|
|
if (rs && !memcmp(rs->descriptor_digest,
|
|
|
|
router->cache_info.signed_descriptor_digest,
|
|
|
|
DIGEST_LEN)) {
|
|
|
|
in_consensus = 1;
|
|
|
|
rs->need_to_mirror = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-16 16:25:14 +02:00
|
|
|
if (router->purpose == ROUTER_PURPOSE_GENERAL &&
|
2007-12-19 00:45:24 +01:00
|
|
|
consensus && !in_consensus && !authdir) {
|
2007-10-16 16:25:14 +02:00
|
|
|
/* If it's a general router not listed in the consensus, then don't
|
|
|
|
* consider replacing the latest router with it. */
|
2007-10-16 01:15:24 +02:00
|
|
|
if (!from_cache && should_cache_old_descriptors())
|
|
|
|
signed_desc_append_to_journal(&router->cache_info,
|
2007-10-22 18:32:01 +02:00
|
|
|
&routerlist->desc_store);
|
2007-10-16 01:15:24 +02:00
|
|
|
routerlist_insert_old(routerlist, router);
|
2008-01-17 06:25:21 +01:00
|
|
|
*msg = "Skipping router descriptor: not in consensus.";
|
2007-10-16 01:15:24 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2006-03-27 07:07:57 +02:00
|
|
|
|
2006-09-20 01:18:30 +02:00
|
|
|
/* If we have a router with the same identity key, choose the newer one. */
|
2007-06-12 00:19:40 +02:00
|
|
|
old_router = rimap_get(routerlist->identity_map,
|
|
|
|
router->cache_info.identity_digest);
|
2006-09-20 01:18:30 +02:00
|
|
|
if (old_router) {
|
2007-11-07 16:19:53 +01:00
|
|
|
if (!in_consensus && (router->cache_info.published_on <=
|
|
|
|
old_router->cache_info.published_on)) {
|
|
|
|
/* Same key, but old. This one is not listed in the consensus. */
|
2006-09-20 01:18:30 +02:00
|
|
|
log_debug(LD_DIR, "Skipping not-new descriptor for router '%s'",
|
|
|
|
router->nickname);
|
|
|
|
/* Only journal this desc if we'll be serving it. */
|
2007-10-09 22:44:47 +02:00
|
|
|
if (!from_cache && should_cache_old_descriptors())
|
2007-08-31 17:08:37 +02:00
|
|
|
signed_desc_append_to_journal(&router->cache_info,
|
2007-10-22 18:32:01 +02:00
|
|
|
&routerlist->desc_store);
|
2006-09-20 01:18:30 +02:00
|
|
|
routerlist_insert_old(routerlist, router);
|
|
|
|
*msg = "Router descriptor was not new.";
|
|
|
|
return -1;
|
|
|
|
} else {
|
2007-11-07 16:19:53 +01:00
|
|
|
/* Same key, and either new, or listed in the consensus. */
|
2006-09-20 01:18:30 +02:00
|
|
|
log_debug(LD_DIR, "Replacing entry for router '%s/%s' [%s]",
|
|
|
|
router->nickname, old_router->nickname,
|
|
|
|
hex_str(id_digest,DIGEST_LEN));
|
|
|
|
if (router->addr == old_router->addr &&
|
|
|
|
router->or_port == old_router->or_port) {
|
2007-10-24 05:34:03 +02:00
|
|
|
/* these carry over when the address and orport are unchanged. */
|
2006-09-20 01:18:30 +02:00
|
|
|
router->last_reachable = old_router->last_reachable;
|
|
|
|
router->testing_since = old_router->testing_since;
|
|
|
|
}
|
2007-07-06 20:40:39 +02:00
|
|
|
routerlist_replace(routerlist, old_router, router);
|
2006-09-20 01:18:30 +02:00
|
|
|
if (!from_cache) {
|
2007-08-31 17:08:37 +02:00
|
|
|
signed_desc_append_to_journal(&router->cache_info,
|
2007-10-22 18:32:01 +02:00
|
|
|
&routerlist->desc_store);
|
2006-09-20 01:18:30 +02:00
|
|
|
}
|
|
|
|
directory_set_dirty();
|
2007-10-24 05:34:03 +02:00
|
|
|
*msg = authdir_believes_valid ? "Valid server updated" :
|
2006-09-20 01:18:30 +02:00
|
|
|
("Invalid server updated. (This dirserver is marking your "
|
|
|
|
"server as unapproved.)");
|
2007-10-24 05:34:03 +02:00
|
|
|
return 0;
|
2006-09-20 01:18:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-24 08:50:38 +01:00
|
|
|
/* We haven't seen a router with this identity before. Add it to the end of
|
2004-05-17 22:31:01 +02:00
|
|
|
* the list. */
|
2005-10-18 19:43:54 +02:00
|
|
|
routerlist_insert(routerlist, router);
|
2005-09-15 07:19:38 +02:00
|
|
|
if (!from_cache)
|
2007-08-31 17:08:37 +02:00
|
|
|
signed_desc_append_to_journal(&router->cache_info,
|
2007-10-22 18:32:01 +02:00
|
|
|
&routerlist->desc_store);
|
2005-08-26 22:59:04 +02:00
|
|
|
directory_set_dirty();
|
2004-05-17 22:31:01 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/** Insert <b>ei</b> into the routerlist, or free it. Other arguments are
|
2007-12-19 05:18:28 +01:00
|
|
|
* as for router_add_to_routerlist(). Return true iff we actually inserted
|
|
|
|
* it.
|
2007-10-21 02:08:35 +02:00
|
|
|
*/
|
|
|
|
int
|
2007-04-16 23:37:21 +02:00
|
|
|
router_add_extrainfo_to_routerlist(extrainfo_t *ei, const char **msg,
|
|
|
|
int from_cache, int from_fetch)
|
|
|
|
{
|
2007-04-30 21:48:39 +02:00
|
|
|
int inserted;
|
2007-04-16 23:37:21 +02:00
|
|
|
(void)from_fetch;
|
2007-05-29 16:41:20 +02:00
|
|
|
if (msg) *msg = NULL;
|
2007-04-30 21:48:39 +02:00
|
|
|
|
|
|
|
inserted = extrainfo_insert(router_get_routerlist(), ei);
|
|
|
|
|
|
|
|
if (inserted && !from_cache)
|
2007-08-31 17:08:37 +02:00
|
|
|
signed_desc_append_to_journal(&ei->cache_info,
|
2007-10-11 18:06:37 +02:00
|
|
|
&routerlist->extrainfo_store);
|
2007-10-21 02:08:35 +02:00
|
|
|
|
|
|
|
return inserted;
|
2007-04-16 23:37:21 +02:00
|
|
|
}
|
|
|
|
|
2007-02-16 21:01:02 +01:00
|
|
|
/** Sorting helper: return <0, 0, or >0 depending on whether the
|
2007-02-24 08:50:38 +01:00
|
|
|
* signed_descriptor_t* in *<b>a</b> has an identity digest preceding, equal
|
|
|
|
* to, or later than that of *<b>b</b>. */
|
2005-10-27 02:34:39 +02:00
|
|
|
static int
|
|
|
|
_compare_old_routers_by_identity(const void **_a, const void **_b)
|
|
|
|
{
|
|
|
|
int i;
|
2005-11-05 21:15:27 +01:00
|
|
|
const signed_descriptor_t *r1 = *_a, *r2 = *_b;
|
2005-10-27 02:34:39 +02:00
|
|
|
if ((i = memcmp(r1->identity_digest, r2->identity_digest, DIGEST_LEN)))
|
|
|
|
return i;
|
|
|
|
return r1->published_on - r2->published_on;
|
|
|
|
}
|
|
|
|
|
2007-02-16 21:01:02 +01:00
|
|
|
/** Internal type used to represent how long an old descriptor was valid,
|
|
|
|
* where it appeared in the list of old descriptors, and whether it's extra
|
|
|
|
* old. Used only by routerlist_remove_old_cached_routers_with_id(). */
|
2005-10-27 02:34:39 +02:00
|
|
|
struct duration_idx_t {
|
|
|
|
int duration;
|
|
|
|
int idx;
|
|
|
|
int old;
|
|
|
|
};
|
|
|
|
|
2007-02-16 21:01:02 +01:00
|
|
|
/** Sorting helper: compare two duration_idx_t by their duration. */
|
2005-10-27 02:34:39 +02:00
|
|
|
static int
|
|
|
|
_compare_duration_idx(const void *_d1, const void *_d2)
|
|
|
|
{
|
|
|
|
const struct duration_idx_t *d1 = _d1;
|
|
|
|
const struct duration_idx_t *d2 = _d2;
|
|
|
|
return d1->duration - d2->duration;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** The range <b>lo</b> through <b>hi</b> inclusive of routerlist->old_routers
|
|
|
|
* must contain routerinfo_t with the same identity and with publication time
|
|
|
|
* in ascending order. Remove members from this range until there are no more
|
2006-03-27 19:29:53 +02:00
|
|
|
* than max_descriptors_per_router() remaining. Start by removing the oldest
|
2005-10-27 02:34:39 +02:00
|
|
|
* members from before <b>cutoff</b>, then remove members which were current
|
|
|
|
* for the lowest amount of time. The order of members of old_routers at
|
|
|
|
* indices <b>lo</b> or higher may be changed.
|
|
|
|
*/
|
|
|
|
static void
|
2007-10-19 04:15:47 +02:00
|
|
|
routerlist_remove_old_cached_routers_with_id(time_t now,
|
|
|
|
time_t cutoff, int lo, int hi,
|
2005-12-27 06:26:03 +01:00
|
|
|
digestmap_t *retain)
|
2005-10-27 02:34:39 +02:00
|
|
|
{
|
2007-07-18 22:46:14 +02:00
|
|
|
int i, n = hi-lo+1;
|
|
|
|
unsigned n_extra, n_rmv = 0;
|
2005-10-27 02:34:39 +02:00
|
|
|
struct duration_idx_t *lifespans;
|
2005-12-27 06:26:03 +01:00
|
|
|
uint8_t *rmv, *must_keep;
|
2005-10-27 02:34:39 +02:00
|
|
|
smartlist_t *lst = routerlist->old_routers;
|
|
|
|
#if 1
|
|
|
|
const char *ident;
|
|
|
|
tor_assert(hi < smartlist_len(lst));
|
|
|
|
tor_assert(lo <= hi);
|
2005-11-05 21:15:27 +01:00
|
|
|
ident = ((signed_descriptor_t*)smartlist_get(lst, lo))->identity_digest;
|
2005-10-27 02:34:39 +02:00
|
|
|
for (i = lo+1; i <= hi; ++i) {
|
2005-11-05 21:15:27 +01:00
|
|
|
signed_descriptor_t *r = smartlist_get(lst, i);
|
2005-10-27 02:34:39 +02:00
|
|
|
tor_assert(!memcmp(ident, r->identity_digest, DIGEST_LEN));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Check whether we need to do anything at all. */
|
2007-07-18 22:46:14 +02:00
|
|
|
{
|
2007-12-01 05:58:53 +01:00
|
|
|
int mdpr = directory_caches_dir_info(get_options()) ? 5 : 2;
|
2007-07-18 22:46:14 +02:00
|
|
|
if (n <= mdpr)
|
|
|
|
return;
|
|
|
|
n_extra = n - mdpr;
|
|
|
|
}
|
2005-10-27 02:34:39 +02:00
|
|
|
|
|
|
|
lifespans = tor_malloc_zero(sizeof(struct duration_idx_t)*n);
|
|
|
|
rmv = tor_malloc_zero(sizeof(uint8_t)*n);
|
2005-12-27 06:26:03 +01:00
|
|
|
must_keep = tor_malloc_zero(sizeof(uint8_t)*n);
|
2005-10-27 02:34:39 +02:00
|
|
|
/* Set lifespans to contain the lifespan and index of each server. */
|
|
|
|
/* Set rmv[i-lo]=1 if we're going to remove a server for being too old. */
|
|
|
|
for (i = lo; i <= hi; ++i) {
|
2005-11-05 21:15:27 +01:00
|
|
|
signed_descriptor_t *r = smartlist_get(lst, i);
|
|
|
|
signed_descriptor_t *r_next;
|
2005-11-04 17:47:26 +01:00
|
|
|
lifespans[i-lo].idx = i;
|
2007-10-19 04:15:47 +02:00
|
|
|
if (r->last_listed_as_valid_until >= now ||
|
|
|
|
(retain && digestmap_get(retain, r->signed_descriptor_digest))) {
|
2005-12-27 06:26:03 +01:00
|
|
|
must_keep[i-lo] = 1;
|
|
|
|
}
|
2005-10-27 02:34:39 +02:00
|
|
|
if (i < hi) {
|
|
|
|
r_next = smartlist_get(lst, i+1);
|
|
|
|
tor_assert(r->published_on <= r_next->published_on);
|
2005-11-05 21:15:27 +01:00
|
|
|
lifespans[i-lo].duration = (r_next->published_on - r->published_on);
|
2005-10-27 02:34:39 +02:00
|
|
|
} else {
|
|
|
|
r_next = NULL;
|
2005-11-04 17:47:26 +01:00
|
|
|
lifespans[i-lo].duration = INT_MAX;
|
2005-10-27 02:34:39 +02:00
|
|
|
}
|
2005-12-27 06:26:03 +01:00
|
|
|
if (!must_keep[i-lo] && r->published_on < cutoff && n_rmv < n_extra) {
|
2005-10-27 02:34:39 +02:00
|
|
|
++n_rmv;
|
2005-11-04 17:47:26 +01:00
|
|
|
lifespans[i-lo].old = 1;
|
2005-10-27 02:34:39 +02:00
|
|
|
rmv[i-lo] = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n_rmv < n_extra) {
|
|
|
|
/**
|
|
|
|
* We aren't removing enough servers for being old. Sort lifespans by
|
|
|
|
* the duration of liveness, and remove the ones we're not already going to
|
|
|
|
* remove based on how long they were alive.
|
|
|
|
**/
|
|
|
|
qsort(lifespans, n, sizeof(struct duration_idx_t), _compare_duration_idx);
|
|
|
|
for (i = 0; i < n && n_rmv < n_extra; ++i) {
|
2005-12-27 06:26:03 +01:00
|
|
|
if (!must_keep[lifespans[i].idx-lo] && !lifespans[i].old) {
|
2005-10-27 02:34:39 +02:00
|
|
|
rmv[lifespans[i].idx-lo] = 1;
|
|
|
|
++n_rmv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-17 00:16:13 +02:00
|
|
|
i = hi;
|
|
|
|
do {
|
2005-10-27 02:34:39 +02:00
|
|
|
if (rmv[i-lo])
|
|
|
|
routerlist_remove_old(routerlist, smartlist_get(lst, i), i);
|
2007-05-17 00:16:13 +02:00
|
|
|
} while (--i >= lo);
|
2005-12-27 06:26:03 +01:00
|
|
|
tor_free(must_keep);
|
2005-10-27 02:34:39 +02:00
|
|
|
tor_free(rmv);
|
|
|
|
tor_free(lifespans);
|
|
|
|
}
|
|
|
|
|
2005-11-11 18:16:24 +01:00
|
|
|
/** Deactivate any routers from the routerlist that are more than
|
2006-10-20 01:04:56 +02:00
|
|
|
* ROUTER_MAX_AGE seconds old and not recommended by any networkstatuses;
|
|
|
|
* remove old routers from the list of cached routers if we have too many.
|
2005-11-01 18:34:17 +01:00
|
|
|
*/
|
2005-10-27 02:34:39 +02:00
|
|
|
void
|
2005-11-01 18:34:17 +01:00
|
|
|
routerlist_remove_old_routers(void)
|
2005-10-27 02:34:39 +02:00
|
|
|
{
|
|
|
|
int i, hi=-1;
|
|
|
|
const char *cur_id = NULL;
|
2006-02-05 03:07:28 +01:00
|
|
|
time_t now = time(NULL);
|
|
|
|
time_t cutoff;
|
2005-11-01 18:34:17 +01:00
|
|
|
routerinfo_t *router;
|
2006-01-10 05:57:12 +01:00
|
|
|
signed_descriptor_t *sd;
|
2005-12-27 06:26:03 +01:00
|
|
|
digestmap_t *retain;
|
2007-12-04 19:35:03 +01:00
|
|
|
int caches = directory_caches_dir_info(get_options());
|
2008-02-05 22:39:29 +01:00
|
|
|
const networkstatus_t *consensus = networkstatus_get_latest_consensus();
|
2007-10-16 01:15:24 +02:00
|
|
|
const smartlist_t *networkstatus_v2_list = networkstatus_get_v2_list();
|
2007-07-28 02:11:34 +02:00
|
|
|
|
|
|
|
trusted_dirs_remove_old_certs();
|
|
|
|
|
2007-10-16 01:15:24 +02:00
|
|
|
if (!routerlist || !consensus)
|
2005-10-27 02:34:39 +02:00
|
|
|
return;
|
|
|
|
|
2007-10-16 20:19:02 +02:00
|
|
|
// routerlist_assert_ok(routerlist);
|
2007-04-23 02:24:06 +02:00
|
|
|
|
2005-12-27 06:26:03 +01:00
|
|
|
retain = digestmap_new();
|
2006-02-05 03:07:28 +01:00
|
|
|
cutoff = now - OLD_ROUTER_DESC_MAX_AGE;
|
2007-10-12 19:13:09 +02:00
|
|
|
/* Build a list of all the descriptors that _anybody_ lists. */
|
2007-12-04 19:35:03 +01:00
|
|
|
if (caches) {
|
2007-10-16 01:15:24 +02:00
|
|
|
SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
|
2006-10-20 01:04:56 +02:00
|
|
|
{
|
2007-02-26 19:01:23 +01:00
|
|
|
/* XXXX The inner loop here gets pretty expensive, and actually shows up
|
|
|
|
* on some profiles. It may be the reason digestmap_set shows up in
|
|
|
|
* profiles too. If instead we kept a per-descriptor digest count of
|
|
|
|
* how many networkstatuses recommended each descriptor, and changed
|
|
|
|
* that only when the networkstatuses changed, that would be a speed
|
|
|
|
* improvement, possibly 1-4% if it also removes digestmap_set from the
|
|
|
|
* profile. Not worth it for 0.1.2.x, though. The new directory
|
|
|
|
* system will obsolete this whole thing in 0.2.0.x. */
|
2006-10-20 01:04:56 +02:00
|
|
|
SMARTLIST_FOREACH(ns->entries, routerstatus_t *, rs,
|
|
|
|
if (rs->published_on >= cutoff)
|
|
|
|
digestmap_set(retain, rs->descriptor_digest, (void*)1));
|
|
|
|
});
|
2007-10-16 01:15:24 +02:00
|
|
|
}
|
2005-12-27 06:26:03 +01:00
|
|
|
|
2007-10-16 01:15:24 +02:00
|
|
|
/* Retain anything listed in the consensus. */
|
|
|
|
if (consensus) {
|
|
|
|
SMARTLIST_FOREACH(consensus->routerstatus_list, routerstatus_t *, rs,
|
2007-10-12 19:13:09 +02:00
|
|
|
if (rs->published_on >= cutoff)
|
|
|
|
digestmap_set(retain, rs->descriptor_digest, (void*)1));
|
|
|
|
}
|
|
|
|
|
2006-10-20 01:04:56 +02:00
|
|
|
/* If we have a bunch of networkstatuses, we should consider pruning current
|
|
|
|
* routers that are too old and that nobody recommends. (If we don't have
|
|
|
|
* enough networkstatuses, then we should get more before we decide to kill
|
|
|
|
* routers.) */
|
2007-12-04 19:35:03 +01:00
|
|
|
if (!caches ||
|
2007-10-16 01:15:24 +02:00
|
|
|
smartlist_len(networkstatus_v2_list) > get_n_v2_authorities() / 2) {
|
2006-10-20 01:04:56 +02:00
|
|
|
cutoff = now - ROUTER_MAX_AGE;
|
|
|
|
/* Remove too-old unrecommended members of routerlist->routers. */
|
|
|
|
for (i = 0; i < smartlist_len(routerlist->routers); ++i) {
|
|
|
|
router = smartlist_get(routerlist->routers, i);
|
|
|
|
if (router->cache_info.published_on <= cutoff &&
|
2007-10-19 04:15:47 +02:00
|
|
|
router->cache_info.last_listed_as_valid_until < now &&
|
2006-10-20 01:04:56 +02:00
|
|
|
!digestmap_get(retain,router->cache_info.signed_descriptor_digest)) {
|
|
|
|
/* Too old: remove it. (If we're a cache, just move it into
|
|
|
|
* old_routers.) */
|
|
|
|
log_info(LD_DIR,
|
|
|
|
"Forgetting obsolete (too old) routerinfo for router '%s'",
|
|
|
|
router->nickname);
|
2007-07-18 16:02:47 +02:00
|
|
|
routerlist_remove(routerlist, router, 1);
|
|
|
|
i--;
|
2006-10-20 01:04:56 +02:00
|
|
|
}
|
2005-11-01 18:34:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-16 20:19:02 +02:00
|
|
|
//routerlist_assert_ok(routerlist);
|
2007-04-23 02:24:06 +02:00
|
|
|
|
2006-01-10 05:57:12 +01:00
|
|
|
/* Remove far-too-old members of routerlist->old_routers. */
|
|
|
|
cutoff = now - OLD_ROUTER_DESC_MAX_AGE;
|
|
|
|
for (i = 0; i < smartlist_len(routerlist->old_routers); ++i) {
|
|
|
|
sd = smartlist_get(routerlist->old_routers, i);
|
|
|
|
if (sd->published_on <= cutoff &&
|
2007-10-19 04:15:47 +02:00
|
|
|
sd->last_listed_as_valid_until < now &&
|
2006-01-10 05:57:12 +01:00
|
|
|
!digestmap_get(retain, sd->signed_descriptor_digest)) {
|
|
|
|
/* Too old. Remove it. */
|
|
|
|
routerlist_remove_old(routerlist, sd, i--);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-16 20:19:02 +02:00
|
|
|
//routerlist_assert_ok(routerlist);
|
2007-04-23 02:24:06 +02:00
|
|
|
|
2007-10-18 16:58:45 +02:00
|
|
|
log_info(LD_DIR, "We have %d live routers and %d old router descriptors. "
|
|
|
|
"At most %d must be retained because of networkstatuses.",
|
|
|
|
smartlist_len(routerlist->routers),
|
|
|
|
smartlist_len(routerlist->old_routers),
|
|
|
|
digestmap_size(retain));
|
|
|
|
|
2006-10-20 01:04:56 +02:00
|
|
|
/* Now we might have to look at routerlist->old_routers for extraneous
|
|
|
|
* members. (We'd keep all the members if we could, but we need to save
|
2006-01-10 05:57:12 +01:00
|
|
|
* space.) First, check whether we have too many router descriptors, total.
|
|
|
|
* We're okay with having too many for some given router, so long as the
|
2006-03-27 19:29:53 +02:00
|
|
|
* total number doesn't approach max_descriptors_per_router()*len(router).
|
2005-10-27 02:34:39 +02:00
|
|
|
*/
|
|
|
|
if (smartlist_len(routerlist->old_routers) <
|
2007-12-04 19:35:03 +01:00
|
|
|
smartlist_len(routerlist->routers) * (caches?4:2))
|
2006-01-11 04:58:07 +01:00
|
|
|
goto done;
|
2005-10-27 02:34:39 +02:00
|
|
|
|
|
|
|
smartlist_sort(routerlist->old_routers, _compare_old_routers_by_identity);
|
2007-11-07 18:29:16 +01:00
|
|
|
/* Fix indices. */
|
2007-11-07 18:30:08 +01:00
|
|
|
for (i = 0; i < smartlist_len(routerlist->old_routers); ++i) {
|
2007-11-07 18:29:16 +01:00
|
|
|
signed_descriptor_t *r = smartlist_get(routerlist->old_routers, i);
|
|
|
|
r->routerlist_index = i;
|
|
|
|
}
|
2005-10-27 02:34:39 +02:00
|
|
|
|
|
|
|
/* Iterate through the list from back to front, so when we remove descriptors
|
|
|
|
* we don't mess up groups we haven't gotten to. */
|
|
|
|
for (i = smartlist_len(routerlist->old_routers)-1; i >= 0; --i) {
|
2005-11-05 21:15:27 +01:00
|
|
|
signed_descriptor_t *r = smartlist_get(routerlist->old_routers, i);
|
2005-10-27 02:34:39 +02:00
|
|
|
if (!cur_id) {
|
|
|
|
cur_id = r->identity_digest;
|
|
|
|
hi = i;
|
|
|
|
}
|
|
|
|
if (memcmp(cur_id, r->identity_digest, DIGEST_LEN)) {
|
2007-10-19 04:15:47 +02:00
|
|
|
routerlist_remove_old_cached_routers_with_id(now,
|
|
|
|
cutoff, i+1, hi, retain);
|
2005-11-23 08:28:54 +01:00
|
|
|
cur_id = r->identity_digest;
|
2005-10-27 02:34:39 +02:00
|
|
|
hi = i;
|
2004-05-20 07:10:30 +02:00
|
|
|
}
|
|
|
|
}
|
2005-11-01 18:34:17 +01:00
|
|
|
if (hi>=0)
|
2007-10-19 04:15:47 +02:00
|
|
|
routerlist_remove_old_cached_routers_with_id(now, cutoff, 0, hi, retain);
|
2007-10-16 20:19:02 +02:00
|
|
|
//routerlist_assert_ok(routerlist);
|
2006-01-11 04:58:07 +01:00
|
|
|
|
|
|
|
done:
|
2005-12-27 06:26:03 +01:00
|
|
|
digestmap_free(retain, NULL);
|
2004-05-20 07:10:30 +02:00
|
|
|
}
|
|
|
|
|
2007-12-22 07:11:49 +01:00
|
|
|
/** We just added a new set of descriptors. Take whatever extra steps
|
|
|
|
* we need. */
|
2007-06-12 11:17:23 +02:00
|
|
|
static void
|
2007-12-24 11:31:39 +01:00
|
|
|
routerlist_descriptors_added(smartlist_t *sl, int from_cache)
|
2007-06-12 11:17:23 +02:00
|
|
|
{
|
|
|
|
tor_assert(sl);
|
|
|
|
control_event_descriptors_changed(sl);
|
|
|
|
SMARTLIST_FOREACH(sl, routerinfo_t *, ri,
|
|
|
|
if (ri->purpose == ROUTER_PURPOSE_BRIDGE)
|
2007-12-24 11:31:39 +01:00
|
|
|
learned_bridge_descriptor(ri, from_cache);
|
2007-06-12 11:17:23 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2005-08-26 22:59:04 +02:00
|
|
|
/**
|
2005-06-21 01:04:13 +02:00
|
|
|
* Code to parse a single router descriptor and insert it into the
|
|
|
|
* routerlist. Return -1 if the descriptor was ill-formed; 0 if the
|
2005-04-03 00:02:13 +02:00
|
|
|
* descriptor was well-formed but could not be added; and 1 if the
|
|
|
|
* descriptor was added.
|
2005-06-21 01:04:13 +02:00
|
|
|
*
|
|
|
|
* If we don't add it and <b>msg</b> is not NULL, then assign to
|
|
|
|
* *<b>msg</b> a static string describing the reason for refusing the
|
|
|
|
* descriptor.
|
2005-08-26 22:59:04 +02:00
|
|
|
*
|
|
|
|
* This is used only by the controller.
|
2004-05-10 06:34:48 +02:00
|
|
|
*/
|
2005-02-25 21:46:13 +01:00
|
|
|
int
|
2007-10-12 09:57:29 +02:00
|
|
|
router_load_single_router(const char *s, uint8_t purpose, int cache,
|
|
|
|
const char **msg)
|
2005-02-25 21:46:13 +01:00
|
|
|
{
|
|
|
|
routerinfo_t *ri;
|
2006-02-15 04:01:53 +01:00
|
|
|
int r;
|
2005-10-05 03:27:08 +02:00
|
|
|
smartlist_t *lst;
|
2007-12-02 05:39:56 +01:00
|
|
|
char annotation_buf[ROUTER_ANNOTATION_BUF_LEN];
|
2005-10-05 04:06:36 +02:00
|
|
|
tor_assert(msg);
|
2005-07-15 21:40:38 +02:00
|
|
|
*msg = NULL;
|
2005-02-25 21:46:13 +01:00
|
|
|
|
2007-09-27 22:46:30 +02:00
|
|
|
tor_snprintf(annotation_buf, sizeof(annotation_buf),
|
|
|
|
"@source controller\n"
|
|
|
|
"@purpose %s\n", router_purpose_to_string(purpose));
|
|
|
|
|
|
|
|
if (!(ri = router_parse_entry_from_string(s, NULL, 1, 0, annotation_buf))) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR, "Error parsing router descriptor; dropping.");
|
2005-07-15 21:40:38 +02:00
|
|
|
*msg = "Couldn't parse router descriptor.";
|
2005-02-25 21:46:13 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2007-09-27 22:46:30 +02:00
|
|
|
tor_assert(ri->purpose == purpose);
|
2005-05-14 07:01:41 +02:00
|
|
|
if (router_is_me(ri)) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR, "Router's identity key matches mine; dropping.");
|
2005-05-18 05:52:07 +02:00
|
|
|
*msg = "Router's identity key matches mine.";
|
2005-05-14 07:01:41 +02:00
|
|
|
routerinfo_free(ri);
|
|
|
|
return 0;
|
|
|
|
}
|
2005-10-05 03:27:08 +02:00
|
|
|
|
2007-10-12 09:57:29 +02:00
|
|
|
if (!cache) /* obey the preference of the controller */
|
|
|
|
ri->cache_info.do_not_cache = 1;
|
|
|
|
|
2005-10-05 03:27:08 +02:00
|
|
|
lst = smartlist_create();
|
|
|
|
smartlist_add(lst, ri);
|
2007-10-16 01:15:24 +02:00
|
|
|
routers_update_status_from_consensus_networkstatus(lst, 0);
|
2005-09-14 23:09:25 +02:00
|
|
|
|
2006-02-15 04:01:53 +01:00
|
|
|
if ((r=router_add_to_routerlist(ri, msg, 0, 0))<0) {
|
2005-06-21 01:04:13 +02:00
|
|
|
/* we've already assigned to *msg now, and ri is already freed */
|
2006-02-15 04:01:53 +01:00
|
|
|
tor_assert(*msg);
|
|
|
|
if (r < -1)
|
|
|
|
log_warn(LD_DIR, "Couldn't add router to list: %s Dropping.", *msg);
|
2005-10-05 03:27:08 +02:00
|
|
|
smartlist_free(lst);
|
2005-04-03 00:02:13 +02:00
|
|
|
return 0;
|
2005-03-02 23:29:58 +01:00
|
|
|
} else {
|
2007-12-24 11:31:39 +01:00
|
|
|
routerlist_descriptors_added(lst, 0);
|
2005-10-05 03:27:08 +02:00
|
|
|
smartlist_free(lst);
|
2006-02-13 11:33:00 +01:00
|
|
|
log_debug(LD_DIR, "Added router to list");
|
2005-10-05 03:27:08 +02:00
|
|
|
return 1;
|
2005-02-25 21:46:13 +01:00
|
|
|
}
|
|
|
|
}
|
2004-04-25 21:04:11 +02:00
|
|
|
|
2005-09-15 08:15:31 +02:00
|
|
|
/** Given a string <b>s</b> containing some routerdescs, parse it and put the
|
2007-01-22 08:51:06 +01:00
|
|
|
* routers into our directory. If saved_location is SAVED_NOWHERE, the routers
|
|
|
|
* are in response to a query to the network: cache them by adding them to
|
|
|
|
* the journal.
|
2005-09-15 08:15:31 +02:00
|
|
|
*
|
|
|
|
* If <b>requested_fingerprints</b> is provided, it must contain a list of
|
2007-09-21 08:14:36 +02:00
|
|
|
* uppercased fingerprints. Do not update any router whose
|
2005-09-15 08:15:31 +02:00
|
|
|
* fingerprint is not on the list; after updating a router, remove its
|
|
|
|
* fingerprint from the list.
|
2007-09-21 08:14:36 +02:00
|
|
|
*
|
|
|
|
* If <b>descriptor_digests</b> is non-zero, then the requested_fingerprints
|
|
|
|
* are descriptor digests. Otherwise they are identity digests.
|
2005-09-15 08:15:31 +02:00
|
|
|
*/
|
2005-09-15 07:19:38 +02:00
|
|
|
void
|
2007-05-22 04:20:52 +02:00
|
|
|
router_load_routers_from_string(const char *s, const char *eos,
|
|
|
|
saved_location_t saved_location,
|
2007-06-10 09:34:21 +02:00
|
|
|
smartlist_t *requested_fingerprints,
|
2007-09-21 08:14:36 +02:00
|
|
|
int descriptor_digests,
|
2007-09-27 22:46:30 +02:00
|
|
|
const char *prepend_annotations)
|
2005-09-15 07:19:38 +02:00
|
|
|
{
|
|
|
|
smartlist_t *routers = smartlist_create(), *changed = smartlist_create();
|
|
|
|
char fp[HEX_DIGEST_LEN+1];
|
|
|
|
const char *msg;
|
2006-06-22 09:01:54 +02:00
|
|
|
int from_cache = (saved_location != SAVED_NOWHERE);
|
2007-09-26 18:19:44 +02:00
|
|
|
int allow_annotations = (saved_location != SAVED_NOWHERE);
|
2008-01-09 00:31:25 +01:00
|
|
|
int any_changed = 0;
|
2005-09-15 07:19:38 +02:00
|
|
|
|
2007-09-26 18:19:44 +02:00
|
|
|
router_parse_list_from_string(&s, eos, routers, saved_location, 0,
|
2007-09-27 22:46:30 +02:00
|
|
|
allow_annotations, prepend_annotations);
|
2005-09-15 07:19:38 +02:00
|
|
|
|
2007-10-16 01:15:24 +02:00
|
|
|
routers_update_status_from_consensus_networkstatus(routers, !from_cache);
|
2005-12-27 06:26:03 +01:00
|
|
|
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_DIR, "%d elements to add", smartlist_len(routers));
|
2005-09-15 07:19:38 +02:00
|
|
|
|
|
|
|
SMARTLIST_FOREACH(routers, routerinfo_t *, ri,
|
|
|
|
{
|
|
|
|
if (requested_fingerprints) {
|
2007-09-21 08:14:36 +02:00
|
|
|
base16_encode(fp, sizeof(fp), descriptor_digests ?
|
|
|
|
ri->cache_info.signed_descriptor_digest :
|
|
|
|
ri->cache_info.identity_digest,
|
|
|
|
DIGEST_LEN);
|
2005-09-15 07:19:38 +02:00
|
|
|
if (smartlist_string_isin(requested_fingerprints, fp)) {
|
|
|
|
smartlist_string_remove(requested_fingerprints, fp);
|
|
|
|
} else {
|
|
|
|
char *requested =
|
|
|
|
smartlist_join_strings(requested_fingerprints," ",0,NULL);
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR,
|
|
|
|
"We received a router descriptor with a fingerprint (%s) "
|
|
|
|
"that we never requested. (We asked for: %s.) Dropping.",
|
|
|
|
fp, requested);
|
2005-09-15 07:19:38 +02:00
|
|
|
tor_free(requested);
|
|
|
|
routerinfo_free(ri);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-16 02:11:03 +02:00
|
|
|
if (router_add_to_routerlist(ri, &msg, from_cache, !from_cache) >= 0) {
|
2008-01-09 00:31:25 +01:00
|
|
|
any_changed = 1;
|
2005-09-15 07:19:38 +02:00
|
|
|
smartlist_add(changed, ri);
|
2007-12-24 11:31:39 +01:00
|
|
|
routerlist_descriptors_added(changed, from_cache);
|
2007-07-16 02:11:03 +02:00
|
|
|
smartlist_clear(changed);
|
|
|
|
}
|
2005-09-15 07:19:38 +02:00
|
|
|
});
|
|
|
|
|
2005-10-18 23:58:19 +02:00
|
|
|
routerlist_assert_ok(routerlist);
|
2007-08-31 17:08:37 +02:00
|
|
|
|
2008-01-09 00:31:25 +01:00
|
|
|
if (any_changed)
|
|
|
|
router_rebuild_store(0, &routerlist->desc_store);
|
2005-09-15 07:19:38 +02:00
|
|
|
|
|
|
|
smartlist_free(routers);
|
|
|
|
smartlist_free(changed);
|
|
|
|
}
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/** Parse one or more extrainfos from <b>s</b> (ending immediately before
|
|
|
|
* <b>eos</b> if <b>eos</b> is present). Other arguments are as for
|
|
|
|
* router_load_routers_from_string(). */
|
2007-05-18 23:19:19 +02:00
|
|
|
void
|
2007-05-22 04:20:52 +02:00
|
|
|
router_load_extrainfo_from_string(const char *s, const char *eos,
|
2007-05-18 23:19:19 +02:00
|
|
|
saved_location_t saved_location,
|
2007-09-21 08:14:36 +02:00
|
|
|
smartlist_t *requested_fingerprints,
|
|
|
|
int descriptor_digests)
|
2007-04-30 21:48:39 +02:00
|
|
|
{
|
|
|
|
smartlist_t *extrainfo_list = smartlist_create();
|
|
|
|
const char *msg;
|
|
|
|
int from_cache = (saved_location != SAVED_NOWHERE);
|
|
|
|
|
2007-09-27 18:08:10 +02:00
|
|
|
router_parse_list_from_string(&s, eos, extrainfo_list, saved_location, 1, 0,
|
|
|
|
NULL);
|
2007-04-30 21:48:39 +02:00
|
|
|
|
|
|
|
log_info(LD_DIR, "%d elements to add", smartlist_len(extrainfo_list));
|
|
|
|
|
2007-05-18 23:19:19 +02:00
|
|
|
SMARTLIST_FOREACH(extrainfo_list, extrainfo_t *, ei, {
|
2007-10-21 02:08:35 +02:00
|
|
|
int added =
|
|
|
|
router_add_extrainfo_to_routerlist(ei, &msg, from_cache, !from_cache);
|
|
|
|
if (added && requested_fingerprints) {
|
2007-05-18 23:19:19 +02:00
|
|
|
char fp[HEX_DIGEST_LEN+1];
|
2007-09-21 08:14:36 +02:00
|
|
|
base16_encode(fp, sizeof(fp), descriptor_digests ?
|
|
|
|
ei->cache_info.signed_descriptor_digest :
|
|
|
|
ei->cache_info.identity_digest,
|
2007-05-18 23:19:19 +02:00
|
|
|
DIGEST_LEN);
|
|
|
|
smartlist_string_remove(requested_fingerprints, fp);
|
2007-09-21 08:14:36 +02:00
|
|
|
/* XXX020 We silently let people stuff us with extrainfos we
|
|
|
|
* didn't ask for. Is this a problem? -RD */
|
2007-05-18 23:19:19 +02:00
|
|
|
}
|
|
|
|
});
|
2007-04-30 21:48:39 +02:00
|
|
|
|
|
|
|
routerlist_assert_ok(routerlist);
|
2007-08-31 17:08:37 +02:00
|
|
|
router_rebuild_store(0, &router_get_routerlist()->extrainfo_store);
|
2007-04-30 21:48:39 +02:00
|
|
|
|
|
|
|
smartlist_free(extrainfo_list);
|
|
|
|
}
|
|
|
|
|
2007-10-08 23:21:50 +02:00
|
|
|
/** Return true iff any networkstatus includes a descriptor whose digest
|
|
|
|
* is that of <b>desc</b>. */
|
|
|
|
static int
|
|
|
|
signed_desc_digest_is_recognized(signed_descriptor_t *desc)
|
2005-09-12 08:56:42 +02:00
|
|
|
{
|
2007-10-08 23:21:50 +02:00
|
|
|
routerstatus_t *rs;
|
2008-02-05 22:39:29 +01:00
|
|
|
networkstatus_t *consensus = networkstatus_get_latest_consensus();
|
2007-12-04 19:35:03 +01:00
|
|
|
int caches = directory_caches_dir_info(get_options());
|
2007-10-16 01:15:24 +02:00
|
|
|
const smartlist_t *networkstatus_v2_list = networkstatus_get_v2_list();
|
2007-10-08 23:21:50 +02:00
|
|
|
|
2007-10-16 01:15:24 +02:00
|
|
|
if (consensus) {
|
|
|
|
rs = networkstatus_vote_find_entry(consensus, desc->identity_digest);
|
|
|
|
if (rs && !memcmp(rs->descriptor_digest,
|
|
|
|
desc->signed_descriptor_digest, DIGEST_LEN))
|
2007-10-08 23:21:50 +02:00
|
|
|
return 1;
|
2007-10-16 01:15:24 +02:00
|
|
|
}
|
2007-12-04 19:35:03 +01:00
|
|
|
if (caches && networkstatus_v2_list) {
|
2007-10-16 01:15:24 +02:00
|
|
|
SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
|
|
|
|
{
|
|
|
|
if (!(rs = networkstatus_v2_find_entry(ns, desc->identity_digest)))
|
|
|
|
continue;
|
|
|
|
if (!memcmp(rs->descriptor_digest,
|
|
|
|
desc->signed_descriptor_digest, DIGEST_LEN))
|
|
|
|
return 1;
|
|
|
|
});
|
|
|
|
}
|
2007-10-08 23:21:50 +02:00
|
|
|
return 0;
|
2005-09-12 08:56:42 +02:00
|
|
|
}
|
2005-09-13 17:32:03 +02:00
|
|
|
|
2007-10-08 23:21:50 +02:00
|
|
|
/** Clear all our timeouts for fetching v2 and v3 directory stuff, and then
|
|
|
|
* give it all a try again. */
|
|
|
|
void
|
|
|
|
routerlist_retry_directory_downloads(time_t now)
|
2005-09-12 08:56:42 +02:00
|
|
|
{
|
2007-10-08 23:21:50 +02:00
|
|
|
router_reset_status_download_failures();
|
|
|
|
router_reset_descriptor_download_failures();
|
|
|
|
update_networkstatus_downloads(now);
|
|
|
|
update_router_descriptor_downloads(now);
|
2005-09-12 08:56:42 +02:00
|
|
|
}
|
|
|
|
|
2007-10-08 23:21:50 +02:00
|
|
|
/** Return 1 if all running sufficiently-stable routers will reject
|
|
|
|
* addr:port, return 0 if any might accept it. */
|
|
|
|
int
|
|
|
|
router_exit_policy_all_routers_reject(uint32_t addr, uint16_t port,
|
|
|
|
int need_uptime)
|
2006-01-08 22:26:33 +01:00
|
|
|
{
|
2007-10-08 23:21:50 +02:00
|
|
|
addr_policy_result_t r;
|
|
|
|
if (!routerlist) return 1;
|
2006-01-08 22:26:33 +01:00
|
|
|
|
2007-10-08 23:21:50 +02:00
|
|
|
SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, router,
|
|
|
|
{
|
|
|
|
if (router->is_running &&
|
|
|
|
!router_is_unreliable(router, need_uptime, 0, 0)) {
|
|
|
|
r = compare_addr_to_addr_policy(addr, port, router->exit_policy);
|
|
|
|
if (r != ADDR_POLICY_REJECTED && r != ADDR_POLICY_PROBABLY_REJECTED)
|
|
|
|
return 0; /* this one could be ok. good enough. */
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return 1; /* all will reject. */
|
2006-01-08 22:26:33 +01:00
|
|
|
}
|
|
|
|
|
2007-10-08 23:21:50 +02:00
|
|
|
/** Return true iff <b>router</b> does not permit exit streams.
|
2005-09-07 18:42:53 +02:00
|
|
|
*/
|
|
|
|
int
|
2007-10-08 23:21:50 +02:00
|
|
|
router_exit_policy_rejects_all(routerinfo_t *router)
|
2005-09-07 18:42:53 +02:00
|
|
|
{
|
2007-10-08 23:21:50 +02:00
|
|
|
return compare_addr_to_addr_policy(0, 0, router->exit_policy)
|
|
|
|
== ADDR_POLICY_REJECTED;
|
|
|
|
}
|
2005-09-07 18:42:53 +02:00
|
|
|
|
2007-10-08 23:21:50 +02:00
|
|
|
/** Add to the list of authorized directory servers one at
|
|
|
|
* <b>address</b>:<b>port</b>, with identity key <b>digest</b>. If
|
2008-02-04 17:58:50 +01:00
|
|
|
* <b>address</b> is NULL, add ourself. Return 0 if success, -1 if
|
|
|
|
* we couldn't add it. */
|
|
|
|
trusted_dir_server_t *
|
2007-10-08 23:21:50 +02:00
|
|
|
add_trusted_dir_server(const char *nickname, const char *address,
|
|
|
|
uint16_t dir_port, uint16_t or_port,
|
|
|
|
const char *digest, const char *v3_auth_digest,
|
|
|
|
authority_type_t type)
|
|
|
|
{
|
|
|
|
trusted_dir_server_t *ent;
|
|
|
|
uint32_t a;
|
|
|
|
char *hostname = NULL;
|
|
|
|
size_t dlen;
|
|
|
|
if (!trusted_dir_servers)
|
|
|
|
trusted_dir_servers = smartlist_create();
|
2005-09-08 22:36:40 +02:00
|
|
|
|
2007-10-08 23:21:50 +02:00
|
|
|
if (!address) { /* The address is us; we should guess. */
|
|
|
|
if (resolve_my_address(LOG_WARN, get_options(), &a, &hostname) < 0) {
|
|
|
|
log_warn(LD_CONFIG,
|
|
|
|
"Couldn't find a suitable address when adding ourself as a "
|
|
|
|
"trusted directory server.");
|
2008-02-04 17:58:50 +01:00
|
|
|
return NULL;
|
2005-09-12 08:56:42 +02:00
|
|
|
}
|
2007-10-08 23:21:50 +02:00
|
|
|
} else {
|
|
|
|
if (tor_lookup_hostname(address, &a)) {
|
|
|
|
log_warn(LD_CONFIG,
|
|
|
|
"Unable to lookup address for directory server at '%s'",
|
|
|
|
address);
|
2008-02-04 17:58:50 +01:00
|
|
|
return NULL;
|
2006-01-08 22:26:33 +01:00
|
|
|
}
|
2007-10-08 23:21:50 +02:00
|
|
|
hostname = tor_strdup(address);
|
2006-01-08 22:26:33 +01:00
|
|
|
}
|
|
|
|
|
2007-10-08 23:21:50 +02:00
|
|
|
ent = tor_malloc_zero(sizeof(trusted_dir_server_t));
|
|
|
|
ent->nickname = nickname ? tor_strdup(nickname) : NULL;
|
|
|
|
ent->address = hostname;
|
|
|
|
ent->addr = a;
|
|
|
|
ent->dir_port = dir_port;
|
|
|
|
ent->or_port = or_port;
|
|
|
|
ent->is_running = 1;
|
|
|
|
ent->type = type;
|
|
|
|
memcpy(ent->digest, digest, DIGEST_LEN);
|
|
|
|
if (v3_auth_digest && (type & V3_AUTHORITY))
|
|
|
|
memcpy(ent->v3_identity_digest, v3_auth_digest, DIGEST_LEN);
|
2005-09-07 18:42:53 +02:00
|
|
|
|
2007-10-08 23:21:50 +02:00
|
|
|
dlen = 64 + strlen(hostname) + (nickname?strlen(nickname):0);
|
|
|
|
ent->description = tor_malloc(dlen);
|
|
|
|
if (nickname)
|
|
|
|
tor_snprintf(ent->description, dlen, "directory server \"%s\" at %s:%d",
|
|
|
|
nickname, hostname, (int)dir_port);
|
|
|
|
else
|
|
|
|
tor_snprintf(ent->description, dlen, "directory server at %s:%d",
|
|
|
|
hostname, (int)dir_port);
|
2007-05-14 10:09:12 +02:00
|
|
|
|
2007-10-09 17:27:15 +02:00
|
|
|
ent->fake_status.addr = ent->addr;
|
|
|
|
memcpy(ent->fake_status.identity_digest, digest, DIGEST_LEN);
|
2007-10-08 23:21:50 +02:00
|
|
|
if (nickname)
|
2007-10-09 17:27:15 +02:00
|
|
|
strlcpy(ent->fake_status.nickname, nickname,
|
|
|
|
sizeof(ent->fake_status.nickname));
|
2007-10-08 23:21:50 +02:00
|
|
|
else
|
2007-10-09 17:27:15 +02:00
|
|
|
ent->fake_status.nickname[0] = '\0';
|
|
|
|
ent->fake_status.dir_port = ent->dir_port;
|
|
|
|
ent->fake_status.or_port = ent->or_port;
|
2005-09-07 18:42:53 +02:00
|
|
|
|
2007-10-08 23:21:50 +02:00
|
|
|
if (ent->or_port)
|
2007-10-09 17:27:15 +02:00
|
|
|
ent->fake_status.version_supports_begindir = 1;
|
2005-12-27 06:26:03 +01:00
|
|
|
|
2007-10-08 23:21:50 +02:00
|
|
|
smartlist_add(trusted_dir_servers, ent);
|
2006-08-15 05:54:09 +02:00
|
|
|
router_dir_info_changed();
|
2008-02-04 17:58:50 +01:00
|
|
|
return ent;
|
2007-10-08 23:21:50 +02:00
|
|
|
}
|
2005-09-14 23:09:25 +02:00
|
|
|
|
2007-10-22 19:31:26 +02:00
|
|
|
/** Free storage held in <b>cert</b>. */
|
|
|
|
void
|
|
|
|
authority_cert_free(authority_cert_t *cert)
|
|
|
|
{
|
|
|
|
if (!cert)
|
|
|
|
return;
|
|
|
|
|
|
|
|
tor_free(cert->cache_info.signed_descriptor_body);
|
|
|
|
if (cert->signing_key)
|
|
|
|
crypto_free_pk_env(cert->signing_key);
|
|
|
|
if (cert->identity_key)
|
|
|
|
crypto_free_pk_env(cert->identity_key);
|
|
|
|
|
|
|
|
tor_free(cert);
|
|
|
|
}
|
|
|
|
|
2007-10-08 23:21:50 +02:00
|
|
|
/** Free storage held in <b>ds</b>. */
|
|
|
|
static void
|
|
|
|
trusted_dir_server_free(trusted_dir_server_t *ds)
|
|
|
|
{
|
|
|
|
tor_free(ds->nickname);
|
|
|
|
tor_free(ds->description);
|
|
|
|
tor_free(ds->address);
|
|
|
|
tor_free(ds);
|
2005-09-07 18:42:53 +02:00
|
|
|
}
|
|
|
|
|
2007-10-08 23:21:50 +02:00
|
|
|
/** Remove all members from the list of trusted dir servers. */
|
2005-09-12 08:56:42 +02:00
|
|
|
void
|
2007-10-08 23:21:50 +02:00
|
|
|
clear_trusted_dir_servers(void)
|
2004-10-12 17:55:20 +02:00
|
|
|
{
|
|
|
|
if (trusted_dir_servers) {
|
|
|
|
SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ent,
|
2005-10-05 07:03:52 +02:00
|
|
|
trusted_dir_server_free(ent));
|
2004-10-12 17:55:20 +02:00
|
|
|
smartlist_clear(trusted_dir_servers);
|
|
|
|
} else {
|
|
|
|
trusted_dir_servers = smartlist_create();
|
|
|
|
}
|
2006-08-15 05:54:09 +02:00
|
|
|
router_dir_info_changed();
|
2004-10-12 17:55:20 +02:00
|
|
|
}
|
2005-06-09 21:03:31 +02:00
|
|
|
|
2006-03-15 01:10:13 +01:00
|
|
|
/** Return 1 if any trusted dir server supports v1 directories,
|
|
|
|
* else return 0. */
|
|
|
|
int
|
2006-04-10 23:29:29 +02:00
|
|
|
any_trusted_dir_is_v1_authority(void)
|
2006-03-15 01:10:13 +01:00
|
|
|
{
|
|
|
|
if (trusted_dir_servers)
|
2007-05-09 06:15:46 +02:00
|
|
|
return get_n_authorities(V1_AUTHORITY) > 0;
|
|
|
|
|
2006-03-15 01:10:13 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-18 16:19:51 +02:00
|
|
|
/** For every current directory connection whose purpose is <b>purpose</b>,
|
|
|
|
* and where the resource being downloaded begins with <b>prefix</b>, split
|
|
|
|
* rest of the resource into base16 fingerprints, decode them, and set the
|
|
|
|
* corresponding elements of <b>result</b> to a nonzero value. */
|
2005-12-27 06:26:03 +01:00
|
|
|
static void
|
2007-10-09 22:44:45 +02:00
|
|
|
list_pending_downloads(digestmap_t *result,
|
|
|
|
int purpose, const char *prefix)
|
2005-09-12 08:56:42 +02:00
|
|
|
{
|
2007-10-09 22:44:45 +02:00
|
|
|
const size_t p_len = strlen(prefix);
|
2005-12-27 06:26:03 +01:00
|
|
|
smartlist_t *tmp = smartlist_create();
|
2007-05-22 17:49:14 +02:00
|
|
|
smartlist_t *conns = get_connection_array();
|
2005-09-12 08:56:42 +02:00
|
|
|
|
2005-12-27 06:26:03 +01:00
|
|
|
tor_assert(result);
|
2005-09-22 08:34:29 +02:00
|
|
|
|
2007-05-22 17:49:14 +02:00
|
|
|
SMARTLIST_FOREACH(conns, connection_t *, conn,
|
|
|
|
{
|
2005-09-22 08:34:29 +02:00
|
|
|
if (conn->type == CONN_TYPE_DIR &&
|
2007-05-18 23:19:19 +02:00
|
|
|
conn->purpose == purpose &&
|
2005-09-22 08:34:29 +02:00
|
|
|
!conn->marked_for_close) {
|
2006-07-26 21:07:37 +02:00
|
|
|
const char *resource = TO_DIR_CONN(conn)->requested_resource;
|
|
|
|
if (!strcmpstart(resource, prefix))
|
|
|
|
dir_split_resource_into_fingerprints(resource + p_len,
|
2006-06-21 06:57:12 +02:00
|
|
|
tmp, NULL, 1, 0);
|
2005-09-22 08:34:29 +02:00
|
|
|
}
|
2007-05-22 17:49:14 +02:00
|
|
|
});
|
2005-12-27 06:26:03 +01:00
|
|
|
SMARTLIST_FOREACH(tmp, char *, d,
|
|
|
|
{
|
|
|
|
digestmap_set(result, d, (void*)1);
|
|
|
|
tor_free(d);
|
|
|
|
});
|
|
|
|
smartlist_free(tmp);
|
|
|
|
}
|
2005-09-23 20:05:14 +02:00
|
|
|
|
2007-10-09 22:44:45 +02:00
|
|
|
/** For every router descriptor (or extra-info document if <b>extrainfo</b> is
|
|
|
|
* true) we are currently downloading by descriptor digest, set result[d] to
|
|
|
|
* (void*)1. */
|
|
|
|
static void
|
|
|
|
list_pending_descriptor_downloads(digestmap_t *result, int extrainfo)
|
|
|
|
{
|
|
|
|
int purpose =
|
|
|
|
extrainfo ? DIR_PURPOSE_FETCH_EXTRAINFO : DIR_PURPOSE_FETCH_SERVERDESC;
|
|
|
|
list_pending_downloads(result, purpose, "d/");
|
|
|
|
}
|
|
|
|
|
2006-02-05 04:42:04 +01:00
|
|
|
/** Launch downloads for all the descriptors whose digests are listed
|
2005-12-27 06:26:03 +01:00
|
|
|
* as digests[i] for lo <= i < hi. (Lo and hi may be out of range.)
|
|
|
|
* If <b>source</b> is given, download from <b>source</b>; otherwise,
|
|
|
|
* download from an appropriate random directory server.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
initiate_descriptor_downloads(routerstatus_t *source,
|
2007-05-18 23:19:19 +02:00
|
|
|
int purpose,
|
2005-12-27 06:26:03 +01:00
|
|
|
smartlist_t *digests,
|
|
|
|
int lo, int hi)
|
|
|
|
{
|
|
|
|
int i, n = hi-lo;
|
|
|
|
char *resource, *cp;
|
|
|
|
size_t r_len;
|
|
|
|
if (n <= 0)
|
|
|
|
return;
|
|
|
|
if (lo < 0)
|
|
|
|
lo = 0;
|
|
|
|
if (hi > smartlist_len(digests))
|
|
|
|
hi = smartlist_len(digests);
|
|
|
|
|
|
|
|
r_len = 8 + (HEX_DIGEST_LEN+1)*n;
|
|
|
|
cp = resource = tor_malloc(r_len);
|
|
|
|
memcpy(cp, "d/", 2);
|
|
|
|
cp += 2;
|
|
|
|
for (i = lo; i < hi; ++i) {
|
|
|
|
base16_encode(cp, r_len-(cp-resource),
|
|
|
|
smartlist_get(digests,i), DIGEST_LEN);
|
|
|
|
cp += HEX_DIGEST_LEN;
|
|
|
|
*cp++ = '+';
|
2005-09-22 08:34:29 +02:00
|
|
|
}
|
2005-12-27 06:26:03 +01:00
|
|
|
memcpy(cp-1, ".z", 3);
|
2005-09-22 08:34:29 +02:00
|
|
|
|
2005-12-27 06:26:03 +01:00
|
|
|
if (source) {
|
|
|
|
/* We know which authority we want. */
|
2007-05-18 23:19:19 +02:00
|
|
|
directory_initiate_command_routerstatus(source, purpose,
|
2007-06-10 09:34:21 +02:00
|
|
|
ROUTER_PURPOSE_GENERAL,
|
2005-12-27 06:26:03 +01:00
|
|
|
0, /* not private */
|
2007-10-28 21:30:21 +01:00
|
|
|
resource, NULL, 0, 0);
|
2005-12-27 06:26:03 +01:00
|
|
|
} else {
|
2007-07-22 02:16:48 +02:00
|
|
|
directory_get_from_dirserver(purpose, ROUTER_PURPOSE_GENERAL, resource, 1);
|
2005-09-15 07:19:38 +02:00
|
|
|
}
|
2005-12-27 06:26:03 +01:00
|
|
|
tor_free(resource);
|
|
|
|
}
|
2005-09-15 07:19:38 +02:00
|
|
|
|
2006-03-12 23:48:18 +01:00
|
|
|
/** Clients don't download any descriptor this recent, since it will probably
|
2007-07-22 02:16:48 +02:00
|
|
|
* not have propagated to enough caches. */
|
2006-03-12 23:48:18 +01:00
|
|
|
#define ESTIMATED_PROPAGATION_TIME (10*60)
|
|
|
|
|
2006-04-08 08:56:38 +02:00
|
|
|
/** Return 0 if this routerstatus is obsolete, too new, isn't
|
|
|
|
* running, or otherwise not a descriptor that we would make any
|
|
|
|
* use of even if we had it. Else return 1. */
|
2006-09-17 22:12:10 +02:00
|
|
|
static INLINE int
|
2006-08-15 05:54:09 +02:00
|
|
|
client_would_use_router(routerstatus_t *rs, time_t now, or_options_t *options)
|
2006-04-08 08:56:38 +02:00
|
|
|
{
|
2006-08-15 05:54:09 +02:00
|
|
|
if (!rs->is_running && !options->FetchUselessDescriptors) {
|
2006-04-08 23:35:17 +02:00
|
|
|
/* If we had this router descriptor, we wouldn't even bother using it.
|
|
|
|
* But, if we want to have a complete list, fetch it anyway. */
|
2006-04-08 08:56:38 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (rs->published_on + ESTIMATED_PROPAGATION_TIME > now) {
|
|
|
|
/* Most caches probably don't have this descriptor yet. */
|
|
|
|
return 0;
|
|
|
|
}
|
2007-10-16 20:19:02 +02:00
|
|
|
if (rs->published_on + OLD_ROUTER_DESC_MAX_AGE < now) {
|
|
|
|
/* We'd drop it immediately for being too old. */
|
|
|
|
return 0;
|
|
|
|
}
|
2006-04-08 08:56:38 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-10-16 01:15:24 +02:00
|
|
|
/** Max amount of hashes to download per request.
|
|
|
|
* Since squid does not like URLs >= 4096 bytes we limit it to 96.
|
|
|
|
* 4096 - strlen(http://255.255.255.255/tor/server/d/.z) == 4058
|
|
|
|
* 4058/41 (40 for the hash and 1 for the + that separates them) => 98
|
|
|
|
* So use 96 because it's a nice number.
|
2005-12-27 06:26:03 +01:00
|
|
|
*/
|
2006-02-03 16:17:48 +01:00
|
|
|
#define MAX_DL_PER_REQUEST 96
|
2007-10-16 01:15:24 +02:00
|
|
|
/** Don't split our requests so finely that we are requesting fewer than
|
|
|
|
* this number per server. */
|
2005-09-23 20:05:14 +02:00
|
|
|
#define MIN_DL_PER_REQUEST 4
|
2007-10-16 01:15:24 +02:00
|
|
|
/** To prevent a single screwy cache from confusing us by selective reply,
|
|
|
|
* try to split our requests into at least this this many requests. */
|
2005-09-22 08:34:29 +02:00
|
|
|
#define MIN_REQUESTS 3
|
2007-10-16 01:15:24 +02:00
|
|
|
/** If we want fewer than this many descriptors, wait until we
|
|
|
|
* want more, or until MAX_CLIENT_INTERVAL_WITHOUT_REQUEST has
|
|
|
|
* passed. */
|
2005-09-30 22:04:55 +02:00
|
|
|
#define MAX_DL_TO_DELAY 16
|
2007-10-16 01:15:24 +02:00
|
|
|
/** When directory clients have only a few servers to request, they batch
|
|
|
|
* them until they have more, or until this amount of time has passed. */
|
2006-03-12 23:48:18 +01:00
|
|
|
#define MAX_CLIENT_INTERVAL_WITHOUT_REQUEST (10*60)
|
2007-10-08 21:56:57 +02:00
|
|
|
|
2007-10-18 16:19:51 +02:00
|
|
|
/** Given a list of router descriptor digests in <b>downloadable</b>, decide
|
|
|
|
* whether to delay fetching until we have more. If we don't want to delay,
|
|
|
|
* launch one or more requests to the appropriate directory authorities. */
|
2007-10-08 21:56:57 +02:00
|
|
|
static void
|
|
|
|
launch_router_descriptor_downloads(smartlist_t *downloadable, time_t now)
|
|
|
|
{
|
|
|
|
int should_delay = 0, n_downloadable;
|
|
|
|
or_options_t *options = get_options();
|
|
|
|
|
2005-10-04 22:18:26 +02:00
|
|
|
n_downloadable = smartlist_len(downloadable);
|
2007-12-21 07:08:00 +01:00
|
|
|
if (!directory_fetches_dir_info_early(options)) {
|
2007-10-08 21:56:57 +02:00
|
|
|
if (n_downloadable >= MAX_DL_TO_DELAY) {
|
|
|
|
log_debug(LD_DIR,
|
|
|
|
"There are enough downloadable routerdescs to launch requests.");
|
|
|
|
should_delay = 0;
|
|
|
|
} else {
|
|
|
|
should_delay = (last_routerdesc_download_attempted +
|
|
|
|
MAX_CLIENT_INTERVAL_WITHOUT_REQUEST) > now;
|
2007-11-16 08:23:25 +01:00
|
|
|
if (!should_delay && n_downloadable) {
|
2007-10-08 21:56:57 +02:00
|
|
|
if (last_routerdesc_download_attempted) {
|
|
|
|
log_info(LD_DIR,
|
|
|
|
"There are not many downloadable routerdescs, but we've "
|
|
|
|
"been waiting long enough (%d seconds). Downloading.",
|
|
|
|
(int)(now-last_routerdesc_download_attempted));
|
|
|
|
} else {
|
|
|
|
log_info(LD_DIR,
|
|
|
|
"There are not many downloadable routerdescs, but we haven't "
|
|
|
|
"tried downloading descriptors recently. Downloading.");
|
|
|
|
}
|
2006-02-19 20:44:09 +01:00
|
|
|
}
|
|
|
|
}
|
2005-10-05 22:45:18 +02:00
|
|
|
}
|
2007-12-04 19:35:03 +01:00
|
|
|
/* XXX020 should we consider having even the dir mirrors delay
|
|
|
|
* a little bit, so we don't load the authorities as much? -RD */
|
2005-10-04 22:18:26 +02:00
|
|
|
|
2007-11-06 19:27:14 +01:00
|
|
|
if (! should_delay && n_downloadable) {
|
2005-12-27 06:26:03 +01:00
|
|
|
int i, n_per_request;
|
2007-05-17 00:16:13 +02:00
|
|
|
const char *req_plural = "", *rtr_plural = "";
|
2005-12-27 06:26:03 +01:00
|
|
|
n_per_request = (n_downloadable+MIN_REQUESTS-1) / MIN_REQUESTS;
|
|
|
|
if (n_per_request > MAX_DL_PER_REQUEST)
|
|
|
|
n_per_request = MAX_DL_PER_REQUEST;
|
|
|
|
if (n_per_request < MIN_DL_PER_REQUEST)
|
|
|
|
n_per_request = MIN_DL_PER_REQUEST;
|
|
|
|
|
2007-05-17 00:16:13 +02:00
|
|
|
if (n_downloadable > n_per_request)
|
|
|
|
req_plural = rtr_plural = "s";
|
|
|
|
else if (n_downloadable > 1)
|
|
|
|
rtr_plural = "s";
|
|
|
|
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_DIR,
|
|
|
|
"Launching %d request%s for %d router%s, %d at a time",
|
|
|
|
(n_downloadable+n_per_request-1)/n_per_request,
|
2007-05-17 00:16:13 +02:00
|
|
|
req_plural, n_downloadable, rtr_plural, n_per_request);
|
2006-06-21 06:57:12 +02:00
|
|
|
smartlist_sort_digests(downloadable);
|
2005-10-04 22:18:26 +02:00
|
|
|
for (i=0; i < n_downloadable; i += n_per_request) {
|
2007-05-18 23:19:19 +02:00
|
|
|
initiate_descriptor_downloads(NULL, DIR_PURPOSE_FETCH_SERVERDESC,
|
|
|
|
downloadable, i, i+n_per_request);
|
2005-09-18 04:24:42 +02:00
|
|
|
}
|
2005-10-05 07:03:52 +02:00
|
|
|
last_routerdesc_download_attempted = now;
|
2005-09-18 04:24:42 +02:00
|
|
|
}
|
2005-09-15 07:19:38 +02:00
|
|
|
}
|
|
|
|
|
2006-03-17 06:50:41 +01:00
|
|
|
/** Launch downloads for router status as needed, using the strategy used by
|
2007-12-04 19:35:03 +01:00
|
|
|
* authorities and caches: based on the v2 networkstatuses we have, download
|
|
|
|
* every descriptor we don't have but would serve, from a random authority
|
|
|
|
* that lists it. */
|
2005-12-27 06:26:03 +01:00
|
|
|
static void
|
2007-12-04 19:35:03 +01:00
|
|
|
update_router_descriptor_cache_downloads_v2(time_t now)
|
2005-12-27 06:26:03 +01:00
|
|
|
{
|
|
|
|
smartlist_t **downloadable; /* For each authority, what can we dl from it? */
|
|
|
|
smartlist_t **download_from; /* ... and, what will we dl from it? */
|
|
|
|
digestmap_t *map; /* Which descs are in progress, or assigned? */
|
|
|
|
int i, j, n;
|
|
|
|
int n_download;
|
|
|
|
or_options_t *options = get_options();
|
2007-10-16 01:15:24 +02:00
|
|
|
const smartlist_t *networkstatus_v2_list = networkstatus_get_v2_list();
|
2005-12-27 06:26:03 +01:00
|
|
|
|
2007-12-21 07:08:00 +01:00
|
|
|
if (! directory_fetches_dir_info_early(options)) {
|
2007-12-04 19:35:03 +01:00
|
|
|
log_warn(LD_BUG, "Called update_router_descriptor_cache_downloads_v2() "
|
2006-05-30 08:11:36 +02:00
|
|
|
"on a non-dir-mirror?");
|
2005-12-27 06:26:03 +01:00
|
|
|
}
|
|
|
|
|
2007-10-16 01:15:24 +02:00
|
|
|
if (!networkstatus_v2_list || !smartlist_len(networkstatus_v2_list))
|
2005-12-27 06:26:03 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
map = digestmap_new();
|
2007-10-16 01:15:24 +02:00
|
|
|
n = smartlist_len(networkstatus_v2_list);
|
2005-12-27 06:26:03 +01:00
|
|
|
|
|
|
|
downloadable = tor_malloc_zero(sizeof(smartlist_t*) * n);
|
|
|
|
download_from = tor_malloc_zero(sizeof(smartlist_t*) * n);
|
|
|
|
|
|
|
|
/* Set map[d]=1 for the digest of every descriptor that we are currently
|
|
|
|
* downloading. */
|
2007-05-18 23:19:19 +02:00
|
|
|
list_pending_descriptor_downloads(map, 0);
|
2005-12-27 06:26:03 +01:00
|
|
|
|
|
|
|
/* For the digest of every descriptor that we don't have, and that we aren't
|
|
|
|
* downloading, add d to downloadable[i] if the i'th networkstatus knows
|
|
|
|
* about that descriptor, and we haven't already failed to get that
|
|
|
|
* descriptor from the corresponding authority.
|
|
|
|
*/
|
|
|
|
n_download = 0;
|
2007-10-16 01:15:24 +02:00
|
|
|
SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
|
2005-12-27 06:26:03 +01:00
|
|
|
{
|
2007-05-13 01:22:27 +02:00
|
|
|
trusted_dir_server_t *ds;
|
2007-02-22 08:41:10 +01:00
|
|
|
smartlist_t *dl;
|
2007-02-24 00:11:08 +01:00
|
|
|
dl = downloadable[ns_sl_idx] = smartlist_create();
|
|
|
|
download_from[ns_sl_idx] = smartlist_create();
|
2007-02-26 06:33:17 +01:00
|
|
|
if (ns->published_on + MAX_NETWORKSTATUS_AGE+10*60 < now) {
|
2007-02-22 08:41:10 +01:00
|
|
|
/* Don't download if the networkstatus is almost ancient. */
|
2007-02-26 06:33:17 +01:00
|
|
|
/* Actually, I suspect what's happening here is that we ask
|
|
|
|
* for the descriptor when we have a given networkstatus,
|
|
|
|
* and then we get a newer networkstatus, and then we receive
|
|
|
|
* the descriptor. Having a networkstatus actually expire is
|
|
|
|
* probably a rare event, and we'll probably be happiest if
|
|
|
|
* we take this clause out. -RD */
|
2007-02-22 08:41:10 +01:00
|
|
|
continue;
|
|
|
|
}
|
2007-05-13 01:22:27 +02:00
|
|
|
|
|
|
|
/* Don't try dirservers that we think are down -- we might have
|
|
|
|
* just tried them and just marked them as down. */
|
|
|
|
ds = router_get_trusteddirserver_by_digest(ns->identity_digest);
|
|
|
|
if (ds && !ds->is_running)
|
|
|
|
continue;
|
|
|
|
|
2005-12-27 06:26:03 +01:00
|
|
|
SMARTLIST_FOREACH(ns->entries, routerstatus_t * , rs,
|
|
|
|
{
|
|
|
|
if (!rs->need_to_mirror)
|
|
|
|
continue;
|
|
|
|
if (router_get_by_descriptor_digest(rs->descriptor_digest)) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_BUG,
|
2007-05-13 11:25:06 +02:00
|
|
|
"We have a router descriptor, but need_to_mirror=1.");
|
2005-12-27 06:26:03 +01:00
|
|
|
rs->need_to_mirror = 0;
|
|
|
|
continue;
|
|
|
|
}
|
2007-05-02 11:12:04 +02:00
|
|
|
if (authdir_mode(options) && dirserv_would_reject_router(rs)) {
|
2005-12-27 06:26:03 +01:00
|
|
|
rs->need_to_mirror = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (digestmap_get(map, rs->descriptor_digest)) {
|
|
|
|
/* We're downloading it already. */
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
/* We could download it from this guy. */
|
|
|
|
smartlist_add(dl, rs->descriptor_digest);
|
|
|
|
++n_download;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
/* At random, assign descriptors to authorities such that:
|
|
|
|
* - if d is a member of some downloadable[x], d is a member of some
|
|
|
|
* download_from[y]. (Everything we want to download, we try to download
|
|
|
|
* from somebody.)
|
2006-03-22 07:22:12 +01:00
|
|
|
* - If d is a member of download_from[y], d is a member of downloadable[y].
|
2005-12-27 06:26:03 +01:00
|
|
|
* (We only try to download descriptors from authorities who claim to have
|
|
|
|
* them.)
|
|
|
|
* - No d is a member of download_from[x] and download_from[y] s.t. x != y.
|
|
|
|
* (We don't try to download anything from two authorities concurrently.)
|
|
|
|
*/
|
|
|
|
while (n_download) {
|
|
|
|
int which_ns = crypto_rand_int(n);
|
|
|
|
smartlist_t *dl = downloadable[which_ns];
|
|
|
|
int idx;
|
|
|
|
char *d;
|
|
|
|
if (!smartlist_len(dl))
|
|
|
|
continue;
|
|
|
|
idx = crypto_rand_int(smartlist_len(dl));
|
|
|
|
d = smartlist_get(dl, idx);
|
|
|
|
if (! digestmap_get(map, d)) {
|
|
|
|
smartlist_add(download_from[which_ns], d);
|
|
|
|
digestmap_set(map, d, (void*) 1);
|
|
|
|
}
|
|
|
|
smartlist_del(dl, idx);
|
|
|
|
--n_download;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now, we can actually launch our requests. */
|
|
|
|
for (i=0; i<n; ++i) {
|
2007-10-16 01:15:24 +02:00
|
|
|
networkstatus_v2_t *ns = smartlist_get(networkstatus_v2_list, i);
|
2005-12-27 06:26:03 +01:00
|
|
|
trusted_dir_server_t *ds =
|
|
|
|
router_get_trusteddirserver_by_digest(ns->identity_digest);
|
2005-12-29 21:25:53 +01:00
|
|
|
smartlist_t *dl = download_from[i];
|
2005-12-27 06:26:03 +01:00
|
|
|
if (!ds) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_BUG, "Networkstatus with no corresponding authority!");
|
2005-12-27 06:26:03 +01:00
|
|
|
continue;
|
|
|
|
}
|
2005-12-27 07:05:54 +01:00
|
|
|
if (! smartlist_len(dl))
|
|
|
|
continue;
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_DIR, "Requesting %d descriptors from authority \"%s\"",
|
|
|
|
smartlist_len(dl), ds->nickname);
|
2005-12-27 06:26:03 +01:00
|
|
|
for (j=0; j < smartlist_len(dl); j += MAX_DL_PER_REQUEST) {
|
2007-10-09 17:27:15 +02:00
|
|
|
initiate_descriptor_downloads(&(ds->fake_status),
|
2007-05-18 23:19:19 +02:00
|
|
|
DIR_PURPOSE_FETCH_SERVERDESC, dl, j,
|
2005-12-27 06:26:03 +01:00
|
|
|
j+MAX_DL_PER_REQUEST);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i=0; i<n; ++i) {
|
|
|
|
smartlist_free(download_from[i]);
|
|
|
|
smartlist_free(downloadable[i]);
|
|
|
|
}
|
|
|
|
tor_free(download_from);
|
|
|
|
tor_free(downloadable);
|
|
|
|
digestmap_free(map,NULL);
|
|
|
|
}
|
|
|
|
|
2007-10-18 16:19:51 +02:00
|
|
|
/** For any descriptor that we want that's currently listed in the live
|
|
|
|
* consensus, download it as appropriate. */
|
2007-10-08 21:56:57 +02:00
|
|
|
static void
|
|
|
|
update_consensus_router_descriptor_downloads(time_t now)
|
|
|
|
{
|
|
|
|
or_options_t *options = get_options();
|
|
|
|
digestmap_t *map = NULL;
|
2007-11-07 16:19:53 +01:00
|
|
|
smartlist_t *no_longer_old = smartlist_create();
|
2007-10-08 21:56:57 +02:00
|
|
|
smartlist_t *downloadable = smartlist_create();
|
|
|
|
int authdir = authdir_mode(options);
|
2008-02-05 22:39:29 +01:00
|
|
|
networkstatus_t *consensus =
|
2007-10-25 03:53:49 +02:00
|
|
|
networkstatus_get_reasonably_live_consensus(now);
|
2007-10-16 20:47:32 +02:00
|
|
|
int n_delayed=0, n_have=0, n_would_reject=0, n_wouldnt_use=0,
|
2007-11-06 21:51:45 +01:00
|
|
|
n_inprogress=0, n_in_oldrouters=0;
|
2007-10-08 23:21:50 +02:00
|
|
|
|
2007-12-04 19:35:03 +01:00
|
|
|
if (directory_too_idle_to_fetch_descriptors(options, now))
|
2007-11-08 05:19:51 +01:00
|
|
|
goto done;
|
2007-10-08 23:21:50 +02:00
|
|
|
if (!consensus)
|
2007-11-07 16:19:53 +01:00
|
|
|
goto done;
|
2007-10-08 21:56:57 +02:00
|
|
|
|
|
|
|
map = digestmap_new();
|
|
|
|
list_pending_descriptor_downloads(map, 0);
|
2007-10-08 23:21:50 +02:00
|
|
|
SMARTLIST_FOREACH(consensus->routerstatus_list, routerstatus_t *, rs,
|
2007-10-08 21:56:57 +02:00
|
|
|
{
|
2007-11-06 21:51:45 +01:00
|
|
|
signed_descriptor_t *sd;
|
|
|
|
if ((sd = router_get_by_descriptor_digest(rs->descriptor_digest))) {
|
|
|
|
routerinfo_t *ri;
|
2007-10-16 20:47:32 +02:00
|
|
|
++n_have;
|
2007-11-06 21:51:45 +01:00
|
|
|
if (!(ri = router_get_by_digest(rs->identity_digest)) ||
|
|
|
|
memcmp(ri->cache_info.signed_descriptor_digest,
|
|
|
|
sd->signed_descriptor_digest, DIGEST_LEN)) {
|
2007-11-12 16:46:38 +01:00
|
|
|
/* We have a descriptor with this digest, but either there is no
|
|
|
|
* entry in routerlist with the same ID (!ri), or there is one,
|
|
|
|
* but the identity digest differs (memcmp).
|
|
|
|
*/
|
2007-11-07 16:19:53 +01:00
|
|
|
smartlist_add(no_longer_old, sd);
|
|
|
|
++n_in_oldrouters; /* We have it in old_routers. */
|
2007-11-06 21:51:45 +01:00
|
|
|
}
|
2007-10-08 21:56:57 +02:00
|
|
|
continue; /* We have it already. */
|
2007-10-16 20:47:32 +02:00
|
|
|
}
|
2008-01-18 05:47:03 +01:00
|
|
|
if (digestmap_get(map, rs->descriptor_digest)) {
|
|
|
|
++n_inprogress;
|
|
|
|
continue; /* We have an in-progress download. */
|
|
|
|
}
|
2007-10-16 01:15:24 +02:00
|
|
|
if (!download_status_is_ready(&rs->dl_status, now,
|
2007-10-16 20:47:32 +02:00
|
|
|
MAX_ROUTERDESC_DOWNLOAD_FAILURES)) {
|
2007-10-18 16:19:51 +02:00
|
|
|
++n_delayed; /* Not ready for retry. */
|
2007-10-11 20:01:12 +02:00
|
|
|
continue;
|
2007-10-16 20:47:32 +02:00
|
|
|
}
|
|
|
|
if (authdir && dirserv_would_reject_router(rs)) {
|
|
|
|
++n_would_reject;
|
2007-10-08 21:56:57 +02:00
|
|
|
continue; /* We would throw it out immediately. */
|
2007-10-16 20:47:32 +02:00
|
|
|
}
|
2007-12-04 19:35:03 +01:00
|
|
|
if (!directory_caches_dir_info(options) &&
|
|
|
|
!client_would_use_router(rs, now, options)) {
|
2007-10-16 20:47:32 +02:00
|
|
|
++n_wouldnt_use;
|
2007-10-08 21:56:57 +02:00
|
|
|
continue; /* We would never use it ourself. */
|
2007-10-16 20:47:32 +02:00
|
|
|
}
|
2007-10-08 21:56:57 +02:00
|
|
|
smartlist_add(downloadable, rs->descriptor_digest);
|
|
|
|
});
|
|
|
|
|
2007-12-19 00:45:24 +01:00
|
|
|
if (!authdir_mode_handles_descs(options, ROUTER_PURPOSE_GENERAL)
|
|
|
|
&& smartlist_len(no_longer_old)) {
|
2007-11-07 16:19:53 +01:00
|
|
|
routerlist_t *rl = router_get_routerlist();
|
2007-11-07 18:11:23 +01:00
|
|
|
log_info(LD_DIR, "%d router descriptors listed in consensus are "
|
2007-11-11 18:20:30 +01:00
|
|
|
"currently in old_routers; making them current.",
|
2007-11-07 18:11:23 +01:00
|
|
|
smartlist_len(no_longer_old));
|
2007-11-07 16:19:53 +01:00
|
|
|
SMARTLIST_FOREACH(no_longer_old, signed_descriptor_t *, sd, {
|
|
|
|
const char *msg;
|
|
|
|
int r;
|
|
|
|
routerinfo_t *ri = routerlist_reparse_old(rl, sd);
|
|
|
|
if (!ri) {
|
2007-11-07 18:11:23 +01:00
|
|
|
log_warn(LD_BUG, "Failed to re-parse a router.");
|
2007-11-07 16:19:53 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
r = router_add_to_routerlist(ri, &msg, 1, 0);
|
|
|
|
if (r == -1) {
|
2007-11-07 18:11:23 +01:00
|
|
|
log_warn(LD_DIR, "Couldn't add re-parsed router: %s",
|
|
|
|
msg?msg:"???");
|
2007-11-07 16:19:53 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
routerlist_assert_ok(rl);
|
|
|
|
}
|
|
|
|
|
2007-10-16 20:47:32 +02:00
|
|
|
log_info(LD_DIR,
|
2007-11-07 16:19:53 +01:00
|
|
|
"%d router descriptors downloadable. %d delayed; %d present "
|
2007-11-07 18:11:23 +01:00
|
|
|
"(%d of those were in old_routers); %d would_reject; "
|
2007-10-16 20:47:32 +02:00
|
|
|
"%d wouldnt_use, %d in progress.",
|
2007-11-06 21:51:45 +01:00
|
|
|
smartlist_len(downloadable), n_delayed, n_have, n_in_oldrouters,
|
|
|
|
n_would_reject, n_wouldnt_use, n_inprogress);
|
2007-10-16 20:47:32 +02:00
|
|
|
|
2007-10-08 21:56:57 +02:00
|
|
|
launch_router_descriptor_downloads(downloadable, now);
|
|
|
|
|
|
|
|
digestmap_free(map, NULL);
|
2007-11-07 16:19:53 +01:00
|
|
|
done:
|
|
|
|
smartlist_free(downloadable);
|
|
|
|
smartlist_free(no_longer_old);
|
2007-10-08 21:56:57 +02:00
|
|
|
}
|
|
|
|
|
2006-03-17 06:50:41 +01:00
|
|
|
/** Launch downloads for router status as needed. */
|
2005-12-27 06:26:03 +01:00
|
|
|
void
|
|
|
|
update_router_descriptor_downloads(time_t now)
|
|
|
|
{
|
|
|
|
or_options_t *options = get_options();
|
2007-06-12 11:17:23 +02:00
|
|
|
if (should_delay_dir_fetches(options))
|
|
|
|
return;
|
2007-12-21 07:08:00 +01:00
|
|
|
if (directory_fetches_dir_info_early(options)) {
|
2007-12-04 19:35:03 +01:00
|
|
|
update_router_descriptor_cache_downloads_v2(now);
|
2005-12-27 06:26:03 +01:00
|
|
|
}
|
2007-10-16 01:15:24 +02:00
|
|
|
update_consensus_router_descriptor_downloads(now);
|
2005-12-27 06:26:03 +01:00
|
|
|
}
|
|
|
|
|
2007-06-12 11:17:23 +02:00
|
|
|
/** Launch extrainfo downloads as needed. */
|
2007-05-18 23:19:19 +02:00
|
|
|
void
|
|
|
|
update_extrainfo_downloads(time_t now)
|
|
|
|
{
|
|
|
|
or_options_t *options = get_options();
|
|
|
|
routerlist_t *rl;
|
|
|
|
smartlist_t *wanted;
|
|
|
|
digestmap_t *pending;
|
2007-10-18 16:50:59 +02:00
|
|
|
int old_routers, i;
|
|
|
|
int n_no_ei = 0, n_pending = 0, n_have = 0, n_delay = 0;
|
2007-05-18 23:19:19 +02:00
|
|
|
if (! options->DownloadExtraInfo)
|
|
|
|
return;
|
2007-06-12 11:17:23 +02:00
|
|
|
if (should_delay_dir_fetches(options))
|
|
|
|
return;
|
2007-10-21 01:45:07 +02:00
|
|
|
if (!router_have_minimum_dir_info())
|
|
|
|
return;
|
2007-05-18 23:19:19 +02:00
|
|
|
|
|
|
|
pending = digestmap_new();
|
|
|
|
list_pending_descriptor_downloads(pending, 1);
|
|
|
|
rl = router_get_routerlist();
|
|
|
|
wanted = smartlist_create();
|
2007-10-18 16:50:59 +02:00
|
|
|
for (old_routers = 0; old_routers < 2; ++old_routers) {
|
|
|
|
smartlist_t *lst = old_routers ? rl->old_routers : rl->routers;
|
|
|
|
for (i = 0; i < smartlist_len(lst); ++i) {
|
|
|
|
signed_descriptor_t *sd;
|
|
|
|
char *d;
|
|
|
|
if (old_routers)
|
|
|
|
sd = smartlist_get(lst, i);
|
|
|
|
else
|
|
|
|
sd = &((routerinfo_t*)smartlist_get(lst, i))->cache_info;
|
|
|
|
if (sd->is_extrainfo)
|
|
|
|
continue; /* This should never happen. */
|
2007-10-21 06:41:00 +02:00
|
|
|
if (old_routers && !router_get_by_digest(sd->identity_digest))
|
|
|
|
continue; /* Couldn't check the signature if we got it. */
|
|
|
|
if (sd->extrainfo_is_bogus)
|
|
|
|
continue;
|
2007-10-18 16:50:59 +02:00
|
|
|
d = sd->extra_info_digest;
|
|
|
|
if (tor_digest_is_zero(d)) {
|
|
|
|
++n_no_ei;
|
|
|
|
continue;
|
2007-05-18 23:19:19 +02:00
|
|
|
}
|
2007-10-18 16:50:59 +02:00
|
|
|
if (eimap_get(rl->extra_info_map, d)) {
|
|
|
|
++n_have;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!download_status_is_ready(&sd->ei_dl_status, now,
|
|
|
|
MAX_ROUTERDESC_DOWNLOAD_FAILURES)) {
|
|
|
|
++n_delay;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (digestmap_get(pending, d)) {
|
|
|
|
++n_pending;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
smartlist_add(wanted, d);
|
|
|
|
}
|
2007-05-18 23:19:19 +02:00
|
|
|
}
|
|
|
|
digestmap_free(pending, NULL);
|
|
|
|
|
2007-10-18 16:50:59 +02:00
|
|
|
log_info(LD_DIR, "Extrainfo download status: %d router with no ei, %d "
|
|
|
|
"with present ei, %d delaying, %d pending, %d downloadable.",
|
|
|
|
n_no_ei, n_have, n_delay, n_pending, smartlist_len(wanted));
|
|
|
|
|
2007-05-18 23:19:19 +02:00
|
|
|
smartlist_shuffle(wanted);
|
|
|
|
for (i = 0; i < smartlist_len(wanted); i += MAX_DL_PER_REQUEST) {
|
|
|
|
initiate_descriptor_downloads(NULL, DIR_PURPOSE_FETCH_EXTRAINFO,
|
|
|
|
wanted, i, i + MAX_DL_PER_REQUEST);
|
|
|
|
}
|
|
|
|
|
|
|
|
smartlist_free(wanted);
|
|
|
|
}
|
|
|
|
|
2007-02-16 21:01:02 +01:00
|
|
|
/** True iff, the last time we checked whether we had enough directory info
|
|
|
|
* to build circuits, the answer was "yes". */
|
2006-08-15 05:54:09 +02:00
|
|
|
static int have_min_dir_info = 0;
|
2007-02-24 08:50:38 +01:00
|
|
|
/** True iff enough has changed since the last time we checked whether we had
|
2007-02-16 21:01:02 +01:00
|
|
|
* enough directory info to build circuits that our old answer can no longer
|
2007-02-24 08:50:38 +01:00
|
|
|
* be trusted. */
|
2006-08-15 05:54:09 +02:00
|
|
|
static int need_to_update_have_min_dir_info = 1;
|
2007-12-19 05:18:28 +01:00
|
|
|
/** String describing what we're missing before we have enough directory
|
|
|
|
* info. */
|
2007-11-02 05:18:28 +01:00
|
|
|
static char dir_info_status[128] = "";
|
2006-08-15 05:54:09 +02:00
|
|
|
|
2005-09-15 08:15:31 +02:00
|
|
|
/** Return true iff we have enough networkstatus and router information to
|
2006-03-22 07:22:12 +01:00
|
|
|
* start building circuits. Right now, this means "more than half the
|
|
|
|
* networkstatus documents, and at least 1/4 of expected routers." */
|
2005-12-15 22:26:52 +01:00
|
|
|
//XXX should consider whether we have enough exiting nodes here.
|
2005-09-15 07:19:38 +02:00
|
|
|
int
|
|
|
|
router_have_minimum_dir_info(void)
|
2006-08-15 05:54:09 +02:00
|
|
|
{
|
2007-03-06 21:25:44 +01:00
|
|
|
if (PREDICT_UNLIKELY(need_to_update_have_min_dir_info)) {
|
2006-08-15 05:54:09 +02:00
|
|
|
update_router_have_minimum_dir_info();
|
|
|
|
need_to_update_have_min_dir_info = 0;
|
|
|
|
}
|
|
|
|
return have_min_dir_info;
|
|
|
|
}
|
|
|
|
|
2007-01-22 08:51:06 +01:00
|
|
|
/** Called when our internal view of the directory has changed. This can be
|
|
|
|
* when the authorities change, networkstatuses change, the list of routerdescs
|
|
|
|
* changes, or number of running routers changes.
|
2006-08-15 05:54:09 +02:00
|
|
|
*/
|
2007-10-08 23:21:50 +02:00
|
|
|
void
|
2006-08-15 05:54:09 +02:00
|
|
|
router_dir_info_changed(void)
|
|
|
|
{
|
|
|
|
need_to_update_have_min_dir_info = 1;
|
|
|
|
}
|
|
|
|
|
2007-12-19 05:18:28 +01:00
|
|
|
/** Return a string describing what we're missing before we have enough
|
|
|
|
* directory info. */
|
2007-11-02 05:18:28 +01:00
|
|
|
const char *
|
|
|
|
get_dir_info_status_string(void)
|
|
|
|
{
|
|
|
|
return dir_info_status;
|
|
|
|
}
|
|
|
|
|
2007-01-22 08:51:06 +01:00
|
|
|
/** Change the value of have_min_dir_info, setting it true iff we have enough
|
|
|
|
* network and router information to build circuits. Clear the value of
|
|
|
|
* need_to_update_have_min_dir_info. */
|
2006-08-15 05:54:09 +02:00
|
|
|
static void
|
|
|
|
update_router_have_minimum_dir_info(void)
|
2005-09-15 07:19:38 +02:00
|
|
|
{
|
2007-10-16 01:15:24 +02:00
|
|
|
int num_present = 0, num_usable=0;
|
2007-02-13 03:01:38 +01:00
|
|
|
time_t now = time(NULL);
|
2007-10-16 01:15:24 +02:00
|
|
|
int res;
|
|
|
|
or_options_t *options = get_options();
|
2008-02-05 22:39:29 +01:00
|
|
|
const networkstatus_t *consensus =
|
2007-10-25 03:53:49 +02:00
|
|
|
networkstatus_get_reasonably_live_consensus(now);
|
2007-10-08 23:21:50 +02:00
|
|
|
|
2007-10-16 01:15:24 +02:00
|
|
|
if (!consensus) {
|
2007-11-02 05:18:28 +01:00
|
|
|
if (!networkstatus_get_latest_consensus())
|
2007-11-11 18:37:59 +01:00
|
|
|
strlcpy(dir_info_status, "We have no network-status consensus.",
|
2007-11-02 18:16:34 +01:00
|
|
|
sizeof(dir_info_status));
|
2007-11-02 05:18:28 +01:00
|
|
|
else
|
2007-11-11 18:37:59 +01:00
|
|
|
strlcpy(dir_info_status, "We have no recent network-status consensus.",
|
2007-11-02 18:16:34 +01:00
|
|
|
sizeof(dir_info_status));
|
2005-12-15 21:44:15 +01:00
|
|
|
res = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
2007-02-13 03:01:38 +01:00
|
|
|
|
2007-06-12 11:17:23 +02:00
|
|
|
if (should_delay_dir_fetches(get_options())) {
|
2007-07-30 10:43:02 +02:00
|
|
|
log_notice(LD_DIR, "no known bridge descriptors running yet; stalling");
|
2007-11-04 03:36:09 +01:00
|
|
|
strlcpy(dir_info_status, "No live bridge descriptors.",
|
|
|
|
sizeof(dir_info_status));
|
2007-06-12 11:17:23 +02:00
|
|
|
res = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2007-10-16 01:15:24 +02:00
|
|
|
SMARTLIST_FOREACH(consensus->routerstatus_list, routerstatus_t *, rs,
|
2005-12-15 21:44:15 +01:00
|
|
|
{
|
2007-10-16 01:15:24 +02:00
|
|
|
if (client_would_use_router(rs, now, options)) {
|
|
|
|
++num_usable;
|
|
|
|
if (router_get_by_digest(rs->identity_digest)) {
|
|
|
|
++num_present;
|
|
|
|
}
|
|
|
|
}
|
2005-12-15 21:44:15 +01:00
|
|
|
});
|
2007-11-02 05:18:28 +01:00
|
|
|
|
|
|
|
if (num_present < num_usable/4) {
|
|
|
|
tor_snprintf(dir_info_status, sizeof(dir_info_status),
|
|
|
|
"We have only %d/%d usable descriptors.", num_present, num_usable);
|
|
|
|
res = 0;
|
|
|
|
} else if (num_usable < 2) {
|
|
|
|
tor_snprintf(dir_info_status, sizeof(dir_info_status),
|
|
|
|
"Only %d usable descriptor%s known!", num_usable,
|
|
|
|
num_usable ? "" : "s");
|
|
|
|
res = 0;
|
|
|
|
} else {
|
|
|
|
res = 1;
|
|
|
|
}
|
2007-10-16 01:15:24 +02:00
|
|
|
|
2005-12-15 21:44:15 +01:00
|
|
|
done:
|
2006-08-15 05:54:09 +02:00
|
|
|
if (res && !have_min_dir_info) {
|
2005-12-15 21:44:15 +01:00
|
|
|
log(LOG_NOTICE, LD_DIR,
|
|
|
|
"We now have enough directory information to build circuits.");
|
2006-10-23 12:16:43 +02:00
|
|
|
control_event_client_status(LOG_NOTICE, "ENOUGH_DIR_INFO");
|
2005-12-15 21:44:15 +01:00
|
|
|
}
|
2006-08-15 05:54:09 +02:00
|
|
|
if (!res && have_min_dir_info) {
|
2005-12-15 22:17:40 +01:00
|
|
|
log(LOG_NOTICE, LD_DIR,"Our directory information is no longer up-to-date "
|
2005-12-15 22:41:27 +01:00
|
|
|
"enough to build circuits.%s",
|
2007-10-16 01:15:24 +02:00
|
|
|
num_usable > 2 ? "" : " (Not enough servers seem reachable -- "
|
2006-01-03 14:12:17 +01:00
|
|
|
"is your network connection down?)");
|
2006-10-23 12:16:43 +02:00
|
|
|
control_event_client_status(LOG_NOTICE, "NOT_ENOUGH_DIR_INFO");
|
2005-12-15 21:44:15 +01:00
|
|
|
}
|
2006-08-15 05:54:09 +02:00
|
|
|
have_min_dir_info = res;
|
2007-11-02 05:18:28 +01:00
|
|
|
need_to_update_have_min_dir_info = 0;
|
2005-09-15 07:19:38 +02:00
|
|
|
}
|
|
|
|
|
2005-09-30 22:04:55 +02:00
|
|
|
/** Reset the descriptor download failure count on all routers, so that we
|
|
|
|
* can retry any long-failed routers immediately.
|
|
|
|
*/
|
2005-09-22 08:34:29 +02:00
|
|
|
void
|
|
|
|
router_reset_descriptor_download_failures(void)
|
|
|
|
{
|
2007-10-16 01:15:24 +02:00
|
|
|
networkstatus_reset_download_failures();
|
2005-10-05 07:03:52 +02:00
|
|
|
last_routerdesc_download_attempted = 0;
|
2007-05-24 19:13:02 +02:00
|
|
|
if (!routerlist)
|
|
|
|
return;
|
|
|
|
SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, ri,
|
|
|
|
{
|
2007-10-09 17:27:45 +02:00
|
|
|
download_status_reset(&ri->cache_info.ei_dl_status);
|
2007-05-24 19:13:02 +02:00
|
|
|
});
|
|
|
|
SMARTLIST_FOREACH(routerlist->old_routers, signed_descriptor_t *, sd,
|
|
|
|
{
|
2007-10-09 17:27:45 +02:00
|
|
|
download_status_reset(&sd->ei_dl_status);
|
2007-05-24 19:13:02 +02:00
|
|
|
});
|
2005-09-22 08:34:29 +02:00
|
|
|
}
|
2005-09-23 19:11:20 +02:00
|
|
|
|
2006-03-12 23:48:18 +01:00
|
|
|
/** Any changes in a router descriptor's publication time larger than this are
|
|
|
|
* automatically non-cosmetic. */
|
|
|
|
#define ROUTER_MAX_COSMETIC_TIME_DIFFERENCE (12*60*60)
|
|
|
|
|
2006-06-18 18:05:54 +02:00
|
|
|
/** We allow uptime to vary from how much it ought to be by this much. */
|
2006-11-02 00:53:15 +01:00
|
|
|
#define ROUTER_ALLOW_UPTIME_DRIFT (6*60*60)
|
2006-06-18 18:05:54 +02:00
|
|
|
|
2005-10-12 15:49:13 +02:00
|
|
|
/** Return true iff the only differences between r1 and r2 are such that
|
2005-11-01 04:48:51 +01:00
|
|
|
* would not cause a recent (post 0.1.1.6) dirserver to republish.
|
2005-10-12 15:49:13 +02:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
router_differences_are_cosmetic(routerinfo_t *r1, routerinfo_t *r2)
|
|
|
|
{
|
2005-12-14 21:40:40 +01:00
|
|
|
time_t r1pub, r2pub;
|
2006-11-02 00:53:15 +01:00
|
|
|
int time_difference;
|
2005-10-12 20:25:25 +02:00
|
|
|
tor_assert(r1 && r2);
|
|
|
|
|
2005-10-19 18:21:19 +02:00
|
|
|
/* r1 should be the one that was published first. */
|
2005-11-05 21:15:27 +01:00
|
|
|
if (r1->cache_info.published_on > r2->cache_info.published_on) {
|
2005-10-12 20:25:25 +02:00
|
|
|
routerinfo_t *ri_tmp = r2;
|
|
|
|
r2 = r1;
|
|
|
|
r1 = ri_tmp;
|
|
|
|
}
|
|
|
|
|
2005-10-12 15:49:13 +02:00
|
|
|
/* If any key fields differ, they're different. */
|
|
|
|
if (strcasecmp(r1->address, r2->address) ||
|
|
|
|
strcasecmp(r1->nickname, r2->nickname) ||
|
|
|
|
r1->or_port != r2->or_port ||
|
|
|
|
r1->dir_port != r2->dir_port ||
|
2007-12-23 02:18:14 +01:00
|
|
|
r1->purpose != r2->purpose ||
|
2005-10-12 19:16:25 +02:00
|
|
|
crypto_pk_cmp_keys(r1->onion_pkey, r2->onion_pkey) ||
|
|
|
|
crypto_pk_cmp_keys(r1->identity_pkey, r2->identity_pkey) ||
|
2005-10-12 15:49:13 +02:00
|
|
|
strcasecmp(r1->platform, r2->platform) ||
|
2005-10-18 23:19:02 +02:00
|
|
|
(r1->contact_info && !r2->contact_info) || /* contact_info is optional */
|
|
|
|
(!r1->contact_info && r2->contact_info) ||
|
2005-12-14 21:40:40 +01:00
|
|
|
(r1->contact_info && r2->contact_info &&
|
|
|
|
strcasecmp(r1->contact_info, r2->contact_info)) ||
|
2005-10-12 15:49:13 +02:00
|
|
|
r1->is_hibernating != r2->is_hibernating ||
|
2006-09-22 22:20:21 +02:00
|
|
|
r1->has_old_dnsworkers != r2->has_old_dnsworkers ||
|
2006-03-27 04:25:34 +02:00
|
|
|
cmp_addr_policies(r1->exit_policy, r2->exit_policy))
|
2005-10-12 15:49:13 +02:00
|
|
|
return 0;
|
|
|
|
if ((r1->declared_family == NULL) != (r2->declared_family == NULL))
|
|
|
|
return 0;
|
|
|
|
if (r1->declared_family && r2->declared_family) {
|
|
|
|
int i, n;
|
|
|
|
if (smartlist_len(r1->declared_family)!=smartlist_len(r2->declared_family))
|
|
|
|
return 0;
|
|
|
|
n = smartlist_len(r1->declared_family);
|
|
|
|
for (i=0; i < n; ++i) {
|
|
|
|
if (strcasecmp(smartlist_get(r1->declared_family, i),
|
|
|
|
smartlist_get(r2->declared_family, i)))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Did bandwidth change a lot? */
|
|
|
|
if ((r1->bandwidthcapacity < r2->bandwidthcapacity/2) ||
|
|
|
|
(r2->bandwidthcapacity < r1->bandwidthcapacity/2))
|
|
|
|
return 0;
|
|
|
|
|
2005-10-19 18:21:19 +02:00
|
|
|
/* Did more than 12 hours pass? */
|
2006-03-12 23:48:18 +01:00
|
|
|
if (r1->cache_info.published_on + ROUTER_MAX_COSMETIC_TIME_DIFFERENCE
|
|
|
|
< r2->cache_info.published_on)
|
2005-10-12 20:25:25 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Did uptime fail to increase by approximately the amount we would think,
|
2006-12-02 23:47:46 +01:00
|
|
|
* give or take some slop? */
|
2005-12-14 21:40:40 +01:00
|
|
|
r1pub = r1->cache_info.published_on;
|
|
|
|
r2pub = r2->cache_info.published_on;
|
2006-11-02 00:53:15 +01:00
|
|
|
time_difference = abs(r2->uptime - (r1->uptime + (r2pub - r1pub)));
|
|
|
|
if (time_difference > ROUTER_ALLOW_UPTIME_DRIFT &&
|
|
|
|
time_difference > r1->uptime * .05 &&
|
|
|
|
time_difference > r2->uptime * .05)
|
2005-10-12 15:49:13 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Otherwise, the difference is cosmetic. */
|
|
|
|
return 1;
|
|
|
|
}
|
2005-10-14 06:56:20 +02:00
|
|
|
|
2007-12-19 05:18:28 +01:00
|
|
|
/** Check whether <b>ri</b> (a.k.a. sd) is a router compatible with the
|
|
|
|
* extrainfo document
|
2007-05-29 19:31:13 +02:00
|
|
|
* <b>ei</b>. If no router is compatible with <b>ei</b>, <b>ei</b> should be
|
|
|
|
* dropped. Return 0 for "compatible", return 1 for "reject, and inform
|
|
|
|
* whoever uploaded <b>ei</b>, and return -1 for "reject silently.". If
|
|
|
|
* <b>msg</b> is present, set *<b>msg</b> to a description of the
|
2007-12-19 05:18:28 +01:00
|
|
|
* incompatibility (if any).
|
2007-10-21 02:08:35 +02:00
|
|
|
**/
|
2007-04-16 19:55:08 +02:00
|
|
|
int
|
2007-05-20 20:03:26 +02:00
|
|
|
routerinfo_incompatible_with_extrainfo(routerinfo_t *ri, extrainfo_t *ei,
|
2007-10-21 02:08:35 +02:00
|
|
|
signed_descriptor_t *sd,
|
2007-05-20 20:03:26 +02:00
|
|
|
const char **msg)
|
2007-04-16 19:55:08 +02:00
|
|
|
{
|
2007-10-21 06:41:00 +02:00
|
|
|
int digest_matches, r=1;
|
2007-04-16 19:55:08 +02:00
|
|
|
tor_assert(ri);
|
|
|
|
tor_assert(ei);
|
2007-10-21 02:08:35 +02:00
|
|
|
if (!sd)
|
|
|
|
sd = &ri->cache_info;
|
2007-04-16 19:55:08 +02:00
|
|
|
|
2007-05-24 19:12:54 +02:00
|
|
|
if (ei->bad_sig) {
|
|
|
|
if (msg) *msg = "Extrainfo signature was bad, or signed with wrong key.";
|
2007-04-16 23:37:21 +02:00
|
|
|
return 1;
|
2007-05-24 19:12:54 +02:00
|
|
|
}
|
2007-04-16 23:37:21 +02:00
|
|
|
|
2007-10-21 06:41:00 +02:00
|
|
|
digest_matches = !memcmp(ei->cache_info.signed_descriptor_digest,
|
|
|
|
sd->extra_info_digest, DIGEST_LEN);
|
|
|
|
|
|
|
|
/* The identity must match exactly to have been generated at the same time
|
2007-05-25 21:41:31 +02:00
|
|
|
* by the same router. */
|
2007-10-21 06:41:00 +02:00
|
|
|
if (memcmp(ri->cache_info.identity_digest, ei->cache_info.identity_digest,
|
2007-05-20 20:03:26 +02:00
|
|
|
DIGEST_LEN)) {
|
|
|
|
if (msg) *msg = "Extrainfo nickname or identity did not match routerinfo";
|
2007-10-21 06:41:00 +02:00
|
|
|
goto err; /* different servers */
|
2007-05-20 20:03:26 +02:00
|
|
|
}
|
2007-04-16 19:55:08 +02:00
|
|
|
|
|
|
|
if (ei->pending_sig) {
|
|
|
|
char signed_digest[128];
|
|
|
|
if (crypto_pk_public_checksig(ri->identity_pkey, signed_digest,
|
2007-05-15 00:51:05 +02:00
|
|
|
ei->pending_sig, ei->pending_sig_len) != DIGEST_LEN ||
|
2007-04-16 19:55:08 +02:00
|
|
|
memcmp(signed_digest, ei->cache_info.signed_descriptor_digest,
|
2007-04-16 23:37:21 +02:00
|
|
|
DIGEST_LEN)) {
|
|
|
|
ei->bad_sig = 1;
|
|
|
|
tor_free(ei->pending_sig);
|
2007-05-20 20:03:26 +02:00
|
|
|
if (msg) *msg = "Extrainfo signature bad, or signed with wrong key";
|
2007-10-21 06:41:00 +02:00
|
|
|
goto err; /* Bad signature, or no match. */
|
2007-04-16 23:37:21 +02:00
|
|
|
}
|
2007-04-16 19:55:08 +02:00
|
|
|
|
2008-01-10 18:48:40 +01:00
|
|
|
ei->cache_info.send_unencrypted = ri->cache_info.send_unencrypted;
|
2007-04-16 19:55:08 +02:00
|
|
|
tor_free(ei->pending_sig);
|
|
|
|
}
|
|
|
|
|
2007-10-21 02:08:35 +02:00
|
|
|
if (ei->cache_info.published_on < sd->published_on) {
|
2007-05-20 20:03:26 +02:00
|
|
|
if (msg) *msg = "Extrainfo published time did not match routerdesc";
|
2007-10-21 06:41:00 +02:00
|
|
|
goto err;
|
2007-10-21 02:08:35 +02:00
|
|
|
} else if (ei->cache_info.published_on > sd->published_on) {
|
2007-05-20 20:03:26 +02:00
|
|
|
if (msg) *msg = "Extrainfo published time did not match routerdesc";
|
2007-10-21 06:41:00 +02:00
|
|
|
r = -1;
|
|
|
|
goto err;
|
2007-05-20 20:03:26 +02:00
|
|
|
}
|
|
|
|
|
2007-10-21 06:41:00 +02:00
|
|
|
if (!digest_matches) {
|
2007-05-20 20:03:26 +02:00
|
|
|
if (msg) *msg = "Extrainfo digest did not match value from routerdesc";
|
2007-10-21 06:41:00 +02:00
|
|
|
goto err; /* Digest doesn't match declared value. */
|
2007-05-20 20:03:26 +02:00
|
|
|
}
|
2007-04-16 19:55:08 +02:00
|
|
|
|
|
|
|
return 0;
|
2007-10-21 06:41:00 +02:00
|
|
|
err:
|
|
|
|
if (digest_matches) {
|
|
|
|
/* This signature was okay, and the digest was right: This is indeed the
|
|
|
|
* corresponding extrainfo. But insanely, it doesn't match the routerinfo
|
|
|
|
* that lists it. Don't try to fetch this one again. */
|
|
|
|
sd->extrainfo_is_bogus = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
2007-04-16 19:55:08 +02:00
|
|
|
}
|
|
|
|
|
2007-01-22 08:51:06 +01:00
|
|
|
/** Assert that the internal representation of <b>rl</b> is
|
|
|
|
* self-consistent. */
|
2007-04-30 07:36:41 +02:00
|
|
|
void
|
2005-10-18 22:13:09 +02:00
|
|
|
routerlist_assert_ok(routerlist_t *rl)
|
|
|
|
{
|
2007-06-12 00:19:40 +02:00
|
|
|
digestmap_iter_t *iter; /* XXXX020 use the appropriate iter type. */
|
2005-10-27 02:34:39 +02:00
|
|
|
routerinfo_t *r2;
|
2005-11-05 21:15:27 +01:00
|
|
|
signed_descriptor_t *sd2;
|
2007-04-30 07:36:41 +02:00
|
|
|
if (!rl)
|
2005-10-18 22:13:09 +02:00
|
|
|
return;
|
|
|
|
SMARTLIST_FOREACH(rl->routers, routerinfo_t *, r,
|
|
|
|
{
|
2007-06-12 00:19:40 +02:00
|
|
|
r2 = rimap_get(rl->identity_map, r->cache_info.identity_digest);
|
2005-10-27 02:34:39 +02:00
|
|
|
tor_assert(r == r2);
|
2007-06-12 00:19:40 +02:00
|
|
|
sd2 = sdmap_get(rl->desc_digest_map,
|
|
|
|
r->cache_info.signed_descriptor_digest);
|
2005-11-05 21:15:27 +01:00
|
|
|
tor_assert(&(r->cache_info) == sd2);
|
2007-11-07 18:11:23 +01:00
|
|
|
tor_assert(r->cache_info.routerlist_index == r_sl_idx);
|
2007-06-12 02:15:55 +02:00
|
|
|
#if 0
|
|
|
|
/* XXXX020.
|
|
|
|
*
|
|
|
|
* Hoo boy. We need to fix this one, and the fix is a bit tricky, so
|
|
|
|
* commenting this out is just a band-aid.
|
|
|
|
*
|
|
|
|
* The problem is that, although well-behaved router descriptors
|
|
|
|
* should never have the same value for their extra_info_digest, it's
|
|
|
|
* possible for ill-behaved routers to claim whatever they like there.
|
|
|
|
*
|
|
|
|
* The real answer is to trash desc_by_eid_map and instead have
|
|
|
|
* something that indicates for a given extra-info digest we want,
|
|
|
|
* what its download status is. We'll do that as a part of routerlist
|
|
|
|
* refactoring once consensus directories are in. For now,
|
|
|
|
* this rep violation is probably harmless: an adversary can make us
|
|
|
|
* reset our retry count for an extrainfo, but that's not the end
|
|
|
|
* of the world.
|
|
|
|
*/
|
2007-05-19 20:12:31 +02:00
|
|
|
if (!tor_digest_is_zero(r->cache_info.extra_info_digest)) {
|
|
|
|
signed_descriptor_t *sd3 =
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_get(rl->desc_by_eid_map, r->cache_info.extra_info_digest);
|
2007-05-19 20:12:31 +02:00
|
|
|
tor_assert(sd3 == &(r->cache_info));
|
|
|
|
}
|
2007-06-12 02:15:55 +02:00
|
|
|
#endif
|
2005-10-27 02:34:39 +02:00
|
|
|
});
|
2005-11-05 21:15:27 +01:00
|
|
|
SMARTLIST_FOREACH(rl->old_routers, signed_descriptor_t *, sd,
|
2005-10-27 02:34:39 +02:00
|
|
|
{
|
2007-06-12 00:19:40 +02:00
|
|
|
r2 = rimap_get(rl->identity_map, sd->identity_digest);
|
2005-11-05 21:15:27 +01:00
|
|
|
tor_assert(sd != &(r2->cache_info));
|
2007-06-12 00:19:40 +02:00
|
|
|
sd2 = sdmap_get(rl->desc_digest_map, sd->signed_descriptor_digest);
|
2005-11-05 21:15:27 +01:00
|
|
|
tor_assert(sd == sd2);
|
2007-11-07 18:11:23 +01:00
|
|
|
tor_assert(sd->routerlist_index == sd_sl_idx);
|
2007-06-12 02:15:55 +02:00
|
|
|
#if 0
|
|
|
|
/* XXXX020 see above. */
|
2007-05-19 20:12:31 +02:00
|
|
|
if (!tor_digest_is_zero(sd->extra_info_digest)) {
|
|
|
|
signed_descriptor_t *sd3 =
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_get(rl->desc_by_eid_map, sd->extra_info_digest);
|
2007-05-19 20:12:31 +02:00
|
|
|
tor_assert(sd3 == sd);
|
|
|
|
}
|
2007-06-12 02:15:55 +02:00
|
|
|
#endif
|
2005-10-18 22:13:09 +02:00
|
|
|
});
|
2007-06-12 00:19:40 +02:00
|
|
|
|
|
|
|
iter = digestmap_iter_init((digestmap_t*)rl->identity_map);
|
2005-10-18 22:13:09 +02:00
|
|
|
while (!digestmap_iter_done(iter)) {
|
|
|
|
const char *d;
|
|
|
|
void *_r;
|
|
|
|
routerinfo_t *r;
|
|
|
|
digestmap_iter_get(iter, &d, &_r);
|
|
|
|
r = _r;
|
2005-11-05 21:15:27 +01:00
|
|
|
tor_assert(!memcmp(r->cache_info.identity_digest, d, DIGEST_LEN));
|
2007-06-12 00:19:40 +02:00
|
|
|
iter = digestmap_iter_next((digestmap_t*)rl->identity_map, iter);
|
2005-10-18 22:13:09 +02:00
|
|
|
}
|
2007-06-12 00:19:40 +02:00
|
|
|
iter = digestmap_iter_init((digestmap_t*)rl->desc_digest_map);
|
2005-10-27 02:34:39 +02:00
|
|
|
while (!digestmap_iter_done(iter)) {
|
|
|
|
const char *d;
|
2005-11-05 21:15:27 +01:00
|
|
|
void *_sd;
|
|
|
|
signed_descriptor_t *sd;
|
|
|
|
digestmap_iter_get(iter, &d, &_sd);
|
|
|
|
sd = _sd;
|
|
|
|
tor_assert(!memcmp(sd->signed_descriptor_digest, d, DIGEST_LEN));
|
2007-06-12 00:19:40 +02:00
|
|
|
iter = digestmap_iter_next((digestmap_t*)rl->desc_digest_map, iter);
|
2005-10-27 02:34:39 +02:00
|
|
|
}
|
2007-06-12 00:19:40 +02:00
|
|
|
iter = digestmap_iter_init((digestmap_t*)rl->desc_by_eid_map);
|
2007-05-19 20:12:31 +02:00
|
|
|
while (!digestmap_iter_done(iter)) {
|
|
|
|
const char *d;
|
|
|
|
void *_sd;
|
|
|
|
signed_descriptor_t *sd;
|
|
|
|
digestmap_iter_get(iter, &d, &_sd);
|
|
|
|
sd = _sd;
|
2007-06-02 16:24:23 +02:00
|
|
|
tor_assert(!tor_digest_is_zero(d));
|
|
|
|
tor_assert(sd);
|
2007-05-19 20:12:31 +02:00
|
|
|
tor_assert(!memcmp(sd->extra_info_digest, d, DIGEST_LEN));
|
2007-06-12 00:19:40 +02:00
|
|
|
iter = digestmap_iter_next((digestmap_t*)rl->desc_by_eid_map, iter);
|
2007-05-19 20:12:31 +02:00
|
|
|
}
|
2007-06-12 00:19:40 +02:00
|
|
|
iter = digestmap_iter_init((digestmap_t*)rl->extra_info_map);
|
2007-05-19 20:12:31 +02:00
|
|
|
while (!digestmap_iter_done(iter)) {
|
|
|
|
const char *d;
|
|
|
|
void *_ei;
|
|
|
|
extrainfo_t *ei;
|
|
|
|
signed_descriptor_t *sd;
|
|
|
|
digestmap_iter_get(iter, &d, &_ei);
|
|
|
|
ei = _ei;
|
|
|
|
tor_assert(!memcmp(ei->cache_info.signed_descriptor_digest,
|
|
|
|
d, DIGEST_LEN));
|
2007-06-12 00:19:40 +02:00
|
|
|
sd = sdmap_get(rl->desc_by_eid_map,
|
|
|
|
ei->cache_info.signed_descriptor_digest);
|
2007-06-12 02:15:55 +02:00
|
|
|
// tor_assert(sd); // XXXX020 see above
|
|
|
|
if (sd) {
|
|
|
|
tor_assert(!memcmp(ei->cache_info.signed_descriptor_digest,
|
|
|
|
sd->extra_info_digest, DIGEST_LEN));
|
|
|
|
}
|
2007-06-12 00:19:40 +02:00
|
|
|
iter = digestmap_iter_next((digestmap_t*)rl->extra_info_map, iter);
|
2007-05-19 20:12:31 +02:00
|
|
|
}
|
2005-10-18 22:13:09 +02:00
|
|
|
}
|
|
|
|
|
2006-03-11 18:52:55 +01:00
|
|
|
/** Allocate and return a new string representing the contact info
|
|
|
|
* and platform string for <b>router</b>,
|
|
|
|
* surrounded by quotes and using standard C escapes.
|
|
|
|
*
|
|
|
|
* THIS FUNCTION IS NOT REENTRANT. Don't call it from outside the main
|
|
|
|
* thread. Also, each call invalidates the last-returned value, so don't
|
|
|
|
* try log_warn(LD_GENERAL, "%s %s", esc_router_info(a), esc_router_info(b));
|
2007-07-22 08:32:28 +02:00
|
|
|
*
|
|
|
|
* If <b>router</b> is NULL, it just frees its internal memory and returns.
|
2006-03-11 18:52:55 +01:00
|
|
|
*/
|
2006-03-11 03:21:30 +01:00
|
|
|
const char *
|
|
|
|
esc_router_info(routerinfo_t *router)
|
|
|
|
{
|
2007-07-22 08:32:28 +02:00
|
|
|
static char *info=NULL;
|
2006-03-11 03:21:30 +01:00
|
|
|
char *esc_contact, *esc_platform;
|
|
|
|
size_t len;
|
|
|
|
if (info)
|
|
|
|
tor_free(info);
|
2007-07-22 08:32:28 +02:00
|
|
|
if (!router)
|
|
|
|
return NULL; /* we're exiting; just free the memory we use */
|
2006-03-11 03:21:30 +01:00
|
|
|
|
|
|
|
esc_contact = esc_for_log(router->contact_info);
|
|
|
|
esc_platform = esc_for_log(router->platform);
|
|
|
|
|
|
|
|
len = strlen(esc_contact)+strlen(esc_platform)+32;
|
|
|
|
info = tor_malloc(len);
|
|
|
|
tor_snprintf(info, len, "Contact %s, Platform %s", esc_contact,
|
|
|
|
esc_platform);
|
|
|
|
tor_free(esc_contact);
|
|
|
|
tor_free(esc_platform);
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2007-10-12 23:33:37 +02:00
|
|
|
/** Helper for sorting: compare two routerinfos by their identity
|
|
|
|
* digest. */
|
|
|
|
static int
|
|
|
|
_compare_routerinfo_by_id_digest(const void **a, const void **b)
|
|
|
|
{
|
|
|
|
routerinfo_t *first = *(routerinfo_t **)a, *second = *(routerinfo_t **)b;
|
|
|
|
return memcmp(first->cache_info.identity_digest,
|
|
|
|
second->cache_info.identity_digest,
|
|
|
|
DIGEST_LEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Sort a list of routerinfo_t in ascending order of identity digest. */
|
|
|
|
void
|
|
|
|
routers_sort_by_identity(smartlist_t *routers)
|
|
|
|
{
|
|
|
|
smartlist_sort(routers, _compare_routerinfo_by_id_digest);
|
|
|
|
}
|
|
|
|
|
2008-01-24 23:46:29 +01:00
|
|
|
/** Determine the routers that are responsible for <b>id</b> (binary) and
|
|
|
|
* add pointers to those routers' routerstatus_t to <b>responsible_dirs</b>.
|
2008-01-25 08:22:42 +01:00
|
|
|
* Return -1 if we're returning an empty smartlist, else return 0.
|
|
|
|
*/
|
2007-10-29 20:10:42 +01:00
|
|
|
int
|
|
|
|
hid_serv_get_responsible_directories(smartlist_t *responsible_dirs,
|
2007-11-03 21:12:41 +01:00
|
|
|
const char *id)
|
2007-10-29 20:10:42 +01:00
|
|
|
{
|
2007-11-03 21:12:41 +01:00
|
|
|
int start, found, n_added = 0, i;
|
2008-02-05 22:39:29 +01:00
|
|
|
networkstatus_t *c = networkstatus_get_latest_consensus();
|
2007-11-03 21:12:41 +01:00
|
|
|
if (!c || !smartlist_len(c->routerstatus_list)) {
|
|
|
|
log_warn(LD_REND, "We don't have a consensus, so we can't perform v2 "
|
|
|
|
"rendezvous operations.");
|
2007-11-14 19:39:15 +01:00
|
|
|
return -1;
|
2007-10-29 20:10:42 +01:00
|
|
|
}
|
2007-11-03 21:12:41 +01:00
|
|
|
tor_assert(id);
|
|
|
|
start = networkstatus_vote_find_entry_idx(c, id, &found);
|
|
|
|
if (start == smartlist_len(c->routerstatus_list)) start = 0;
|
|
|
|
i = start;
|
|
|
|
do {
|
|
|
|
routerstatus_t *r = smartlist_get(c->routerstatus_list, i);
|
2007-11-17 02:53:33 +01:00
|
|
|
if (r->is_hs_dir) {
|
2007-11-03 21:12:41 +01:00
|
|
|
smartlist_add(responsible_dirs, r);
|
|
|
|
if (++n_added == REND_NUMBER_OF_CONSECUTIVE_REPLICAS)
|
|
|
|
return 0;
|
2007-10-29 20:10:42 +01:00
|
|
|
}
|
2007-11-03 21:12:41 +01:00
|
|
|
if (++i == smartlist_len(c->routerstatus_list))
|
|
|
|
i = 0;
|
|
|
|
} while (i != start);
|
|
|
|
|
2008-01-24 23:46:29 +01:00
|
|
|
/* Even though we don't have the desired number of hidden service
|
2008-01-25 08:22:42 +01:00
|
|
|
* directories, be happy if we got any. */
|
|
|
|
return smartlist_len(responsible_dirs) ? 0 : -1;
|
2007-10-29 20:10:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Return true if this node is currently acting as hidden service
|
|
|
|
* directory, false otherwise. */
|
|
|
|
int
|
2007-11-03 21:12:41 +01:00
|
|
|
hid_serv_acting_as_directory(void)
|
2007-10-29 20:10:42 +01:00
|
|
|
{
|
2007-11-03 21:12:41 +01:00
|
|
|
routerinfo_t *me = router_get_my_routerinfo();
|
2008-02-05 22:39:29 +01:00
|
|
|
networkstatus_t *c;
|
2007-11-03 21:12:41 +01:00
|
|
|
routerstatus_t *rs;
|
|
|
|
if (!me)
|
2007-10-29 20:10:42 +01:00
|
|
|
return 0;
|
|
|
|
if (!get_options()->HidServDirectoryV2) {
|
|
|
|
log_info(LD_REND, "We are not acting as hidden service directory, "
|
|
|
|
"because we have not been configured as such.");
|
|
|
|
return 0;
|
|
|
|
}
|
2007-11-03 21:12:41 +01:00
|
|
|
if (!(c = networkstatus_get_latest_consensus())) {
|
|
|
|
log_info(LD_REND, "There's no consensus, so I can't tell if I'm a hidden "
|
|
|
|
"service directory");
|
2007-10-29 20:10:42 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2007-11-03 21:12:41 +01:00
|
|
|
rs = networkstatus_vote_find_entry(c, me->cache_info.identity_digest);
|
|
|
|
if (!rs) {
|
|
|
|
log_info(LD_REND, "We're not listed in the consensus, so we're not "
|
|
|
|
"being a hidden service directory.");
|
2007-10-29 20:10:42 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2007-11-03 21:12:41 +01:00
|
|
|
if (!rs->is_hs_dir) {
|
|
|
|
log_info(LD_REND, "We're not listed as a hidden service directory in "
|
|
|
|
"the consensus, so we won't be one.");
|
|
|
|
return 0;
|
|
|
|
}
|
2007-10-29 20:10:42 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return true if this node is responsible for storing the descriptor ID
|
|
|
|
* in <b>query</b> and false otherwise. */
|
|
|
|
int
|
2007-11-03 21:12:41 +01:00
|
|
|
hid_serv_responsible_for_desc_id(const char *query)
|
2007-10-29 20:10:42 +01:00
|
|
|
{
|
2007-11-03 21:12:41 +01:00
|
|
|
routerinfo_t *me;
|
|
|
|
routerstatus_t *last_rs;
|
|
|
|
const char *my_id, *last_id;
|
|
|
|
int result;
|
|
|
|
smartlist_t *responsible;
|
|
|
|
if (!hid_serv_acting_as_directory())
|
|
|
|
return 0;
|
|
|
|
if (!(me = router_get_my_routerinfo()))
|
|
|
|
return 0; /* This is redundant, but let's be paranoid. */
|
|
|
|
my_id = me->cache_info.identity_digest;
|
|
|
|
responsible = smartlist_create();
|
2008-01-25 08:22:42 +01:00
|
|
|
if (hid_serv_get_responsible_directories(responsible, query) < 0) {
|
2007-11-03 21:12:41 +01:00
|
|
|
smartlist_free(responsible);
|
2007-10-29 20:10:42 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2007-11-03 21:12:41 +01:00
|
|
|
last_rs = smartlist_get(responsible, smartlist_len(responsible)-1);
|
|
|
|
last_id = last_rs->identity_digest;
|
|
|
|
result = rend_id_is_in_interval(my_id, query, last_id);
|
|
|
|
smartlist_free(responsible);
|
|
|
|
return result;
|
2007-10-29 20:10:42 +01:00
|
|
|
}
|
|
|
|
|