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.
|
2013-01-16 07:54:56 +01:00
|
|
|
* Copyright (c) 2007-2013, 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 */
|
|
|
|
|
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
|
|
|
|
2012-08-09 19:47:42 +02:00
|
|
|
#define ROUTERLIST_PRIVATE
|
2005-06-11 07:31:17 +02:00
|
|
|
#include "or.h"
|
2012-10-15 20:48:34 +02:00
|
|
|
#include "circuitstats.h"
|
2010-07-22 10:22:51 +02:00
|
|
|
#include "config.h"
|
2010-07-22 10:32:52 +02:00
|
|
|
#include "connection.h"
|
2010-07-22 11:35:09 +02:00
|
|
|
#include "control.h"
|
2010-07-22 11:54:50 +02:00
|
|
|
#include "directory.h"
|
2010-07-22 12:09:49 +02:00
|
|
|
#include "dirserv.h"
|
2010-07-22 12:19:28 +02:00
|
|
|
#include "dirvote.h"
|
2012-10-15 20:48:34 +02:00
|
|
|
#include "entrynodes.h"
|
2013-05-09 13:56:54 +02:00
|
|
|
#include "fp_pair.h"
|
2010-07-21 14:38:52 +02:00
|
|
|
#include "geoip.h"
|
2010-07-22 12:30:46 +02:00
|
|
|
#include "hibernate.h"
|
2010-07-23 19:58:06 +02:00
|
|
|
#include "main.h"
|
2010-05-11 23:20:33 +02:00
|
|
|
#include "microdesc.h"
|
2010-07-23 20:18:55 +02:00
|
|
|
#include "networkstatus.h"
|
2010-09-28 19:29:31 +02:00
|
|
|
#include "nodelist.h"
|
2010-07-23 20:51:25 +02:00
|
|
|
#include "policies.h"
|
2010-07-26 11:37:13 +02:00
|
|
|
#include "reasons.h"
|
2010-07-21 17:52:54 +02:00
|
|
|
#include "rendcommon.h"
|
2010-07-22 00:30:17 +02:00
|
|
|
#include "rendservice.h"
|
2010-07-23 22:57:20 +02:00
|
|
|
#include "rephist.h"
|
2010-07-21 16:17:10 +02:00
|
|
|
#include "router.h"
|
2010-07-21 17:08:11 +02:00
|
|
|
#include "routerlist.h"
|
2010-07-23 23:23:43 +02:00
|
|
|
#include "routerparse.h"
|
2012-09-13 18:46:39 +02:00
|
|
|
#include "routerset.h"
|
2013-07-23 13:01:53 +02:00
|
|
|
#include "../common/sandbox.h"
|
2007-05-19 20:31:04 +02:00
|
|
|
// #define DEBUG_ROUTERLIST
|
|
|
|
|
2002-09-26 14:09:10 +02:00
|
|
|
/****************************************************************************/
|
|
|
|
|
2013-05-09 13:56:54 +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)
|
|
|
|
DECLARE_TYPED_DIGESTMAP_FNS(dsmap_, digest_ds_map_t, download_status_t)
|
|
|
|
#define SDMAP_FOREACH(map, keyvar, valvar) \
|
|
|
|
DIGESTMAP_FOREACH(sdmap_to_digestmap(map), keyvar, signed_descriptor_t *, \
|
|
|
|
valvar)
|
|
|
|
#define RIMAP_FOREACH(map, keyvar, valvar) \
|
|
|
|
DIGESTMAP_FOREACH(rimap_to_digestmap(map), keyvar, routerinfo_t *, valvar)
|
|
|
|
#define EIMAP_FOREACH(map, keyvar, valvar) \
|
|
|
|
DIGESTMAP_FOREACH(eimap_to_digestmap(map), keyvar, extrainfo_t *, valvar)
|
|
|
|
#define DSMAP_FOREACH(map, keyvar, valvar) \
|
|
|
|
DIGESTMAP_FOREACH(dsmap_to_digestmap(map), keyvar, download_status_t *, \
|
|
|
|
valvar)
|
|
|
|
|
|
|
|
/* Forward declaration for cert_list_t */
|
|
|
|
typedef struct cert_list_t cert_list_t;
|
|
|
|
|
2002-09-24 12:43:57 +02:00
|
|
|
/* static function prototypes */
|
2013-01-18 18:24:54 +01:00
|
|
|
static int compute_weighted_bandwidths(const smartlist_t *sl,
|
|
|
|
bandwidth_weight_rule_t rule,
|
|
|
|
u64_dbl_t **bandwidths_out);
|
2010-09-29 07:35:08 +02:00
|
|
|
static const routerstatus_t *router_pick_directory_server_impl(
|
2010-11-08 20:27:36 +01:00
|
|
|
dirinfo_type_t auth, int flags);
|
2010-09-29 07:35:08 +02:00
|
|
|
static const routerstatus_t *router_pick_trusteddirserver_impl(
|
2012-09-10 21:23:39 +02:00
|
|
|
const smartlist_t *sourcelist, dirinfo_type_t auth,
|
|
|
|
int flags, int *n_busy_out);
|
|
|
|
static const routerstatus_t *router_pick_dirserver_generic(
|
|
|
|
smartlist_t *sourcelist,
|
|
|
|
dirinfo_type_t type, int flags);
|
|
|
|
static void mark_all_dirservers_up(smartlist_t *server_list);
|
2012-09-10 22:33:19 +02:00
|
|
|
static void dir_server_free(dir_server_t *ds);
|
2005-12-27 06:26:03 +01:00
|
|
|
static int signed_desc_digest_is_recognized(signed_descriptor_t *desc);
|
2010-09-29 06:38:32 +02:00
|
|
|
static const char *signed_descriptor_get_body_impl(
|
|
|
|
const 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);
|
2013-05-09 13:56:54 +02:00
|
|
|
static void list_pending_fpsk_downloads(fp_pair_map_t *result);
|
2010-11-12 18:05:27 +01:00
|
|
|
static void launch_dummy_descriptor_download_as_needed(time_t now,
|
2011-06-14 19:01:38 +02:00
|
|
|
const or_options_t *options);
|
2013-05-09 13:56:54 +02:00
|
|
|
static void download_status_reset_by_sk_in_cl(cert_list_t *cl,
|
|
|
|
const char *digest);
|
|
|
|
static int download_status_is_ready_by_sk_in_cl(cert_list_t *cl,
|
|
|
|
const char *digest,
|
|
|
|
time_t now, int max_failures);
|
2007-06-12 00:19:40 +02:00
|
|
|
|
2003-12-09 05:29:52 +01:00
|
|
|
/****************************************************************************/
|
2003-12-13 02:43:21 +01:00
|
|
|
|
2012-09-10 21:55:27 +02:00
|
|
|
/** Global list of a dir_server_t object for each directory
|
2012-09-10 21:23:39 +02:00
|
|
|
* authority. */
|
2005-11-01 04:48:51 +01:00
|
|
|
static smartlist_t *trusted_dir_servers = NULL;
|
2012-09-10 21:55:27 +02:00
|
|
|
/** Global list of dir_server_t objects for all directory authorities
|
2012-09-10 21:23:39 +02:00
|
|
|
* and all fallback directory servers. */
|
|
|
|
static smartlist_t *fallback_dir_servers = NULL;
|
2008-01-27 00:18:30 +01:00
|
|
|
|
2008-02-06 17:58:05 +01:00
|
|
|
/** List of for a given authority, and download status for latest certificate.
|
|
|
|
*/
|
2013-05-09 13:56:54 +02:00
|
|
|
struct cert_list_t {
|
|
|
|
/*
|
|
|
|
* The keys of download status map are cert->signing_key_digest for pending
|
|
|
|
* downloads by (identity digest/signing key digest) pair; functions such
|
|
|
|
* as authority_cert_get_by_digest() already assume these are unique.
|
|
|
|
*/
|
|
|
|
struct digest_ds_map_t *dl_status_map;
|
|
|
|
/* There is also a dlstatus for the download by identity key only */
|
|
|
|
download_status_t dl_status_by_id;
|
2008-01-27 00:18:30 +01:00
|
|
|
smartlist_t *certs;
|
2013-05-09 13:56:54 +02:00
|
|
|
};
|
2008-01-27 00:18:30 +01:00
|
|
|
/** 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. */
|
2010-11-12 18:05:27 +01:00
|
|
|
static time_t last_descriptor_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
|
2010-11-08 20:27:36 +01:00
|
|
|
get_n_authorities(dirinfo_type_t type)
|
2006-09-29 01:57:59 +02:00
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
if (!trusted_dir_servers)
|
|
|
|
return 0;
|
2012-09-10 21:55:27 +02:00
|
|
|
SMARTLIST_FOREACH(trusted_dir_servers, 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;
|
|
|
|
}
|
|
|
|
|
2013-05-09 13:56:54 +02:00
|
|
|
/** Reset the download status of a specified element in a dsmap */
|
|
|
|
static void
|
|
|
|
download_status_reset_by_sk_in_cl(cert_list_t *cl, const char *digest)
|
|
|
|
{
|
|
|
|
download_status_t *dlstatus = NULL;
|
|
|
|
|
|
|
|
tor_assert(cl);
|
|
|
|
tor_assert(digest);
|
|
|
|
|
|
|
|
/* Make sure we have a dsmap */
|
|
|
|
if (!(cl->dl_status_map)) {
|
|
|
|
cl->dl_status_map = dsmap_new();
|
|
|
|
}
|
|
|
|
/* Look for a download_status_t in the map with this digest */
|
|
|
|
dlstatus = dsmap_get(cl->dl_status_map, digest);
|
|
|
|
/* Got one? */
|
|
|
|
if (!dlstatus) {
|
|
|
|
/* Insert before we reset */
|
|
|
|
dlstatus = tor_malloc_zero(sizeof(*dlstatus));
|
|
|
|
dsmap_set(cl->dl_status_map, digest, dlstatus);
|
|
|
|
}
|
|
|
|
tor_assert(dlstatus);
|
|
|
|
/* Go ahead and reset it */
|
|
|
|
download_status_reset(dlstatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if the download for this signing key digest in cl is ready
|
|
|
|
* to be re-attempted.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
download_status_is_ready_by_sk_in_cl(cert_list_t *cl,
|
|
|
|
const char *digest,
|
|
|
|
time_t now, int max_failures)
|
|
|
|
{
|
|
|
|
int rv = 0;
|
|
|
|
download_status_t *dlstatus = NULL;
|
|
|
|
|
|
|
|
tor_assert(cl);
|
|
|
|
tor_assert(digest);
|
|
|
|
|
|
|
|
/* Make sure we have a dsmap */
|
|
|
|
if (!(cl->dl_status_map)) {
|
|
|
|
cl->dl_status_map = dsmap_new();
|
|
|
|
}
|
|
|
|
/* Look for a download_status_t in the map with this digest */
|
|
|
|
dlstatus = dsmap_get(cl->dl_status_map, digest);
|
|
|
|
/* Got one? */
|
|
|
|
if (dlstatus) {
|
|
|
|
/* Use download_status_is_ready() */
|
|
|
|
rv = download_status_is_ready(dlstatus, now, max_failures);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* If we don't know anything about it, return 1, since we haven't
|
|
|
|
* tried this one before. We need to create a new entry here,
|
|
|
|
* too.
|
|
|
|
*/
|
|
|
|
dlstatus = tor_malloc_zero(sizeof(*dlstatus));
|
|
|
|
download_status_reset(dlstatus);
|
|
|
|
dsmap_set(cl->dl_status_map, digest, dlstatus);
|
|
|
|
rv = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2008-02-06 17:58:05 +01:00
|
|
|
/** Helper: Return the cert_list_t for an authority whose authority ID is
|
|
|
|
* <b>id_digest</b>, allocating a new list if necessary. */
|
2008-01-27 00:18:30 +01:00
|
|
|
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));
|
2013-05-09 13:56:54 +02:00
|
|
|
cl->dl_status_by_id.schedule = DL_SCHED_CONSENSUS;
|
2012-01-18 21:53:30 +01:00
|
|
|
cl->certs = smartlist_new();
|
2013-05-09 13:56:54 +02:00
|
|
|
cl->dl_status_map = dsmap_new();
|
2008-01-27 00:18:30 +01:00
|
|
|
digestmap_set(trusted_dir_certs, id_digest, cl);
|
|
|
|
}
|
|
|
|
return cl;
|
|
|
|
}
|
|
|
|
|
2013-09-19 18:22:49 +02:00
|
|
|
/** Release all space held by a cert_list_t */
|
|
|
|
static void
|
|
|
|
cert_list_free(cert_list_t *cl)
|
|
|
|
{
|
|
|
|
if (!cl)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert,
|
|
|
|
authority_cert_free(cert));
|
|
|
|
smartlist_free(cl->certs);
|
|
|
|
dsmap_free(cl->dl_status_map, tor_free_);
|
|
|
|
tor_free(cl);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Wrapper for cert_list_free so we can pass it to digestmap_free */
|
|
|
|
static void
|
|
|
|
cert_list_free_(void *cl)
|
|
|
|
{
|
|
|
|
cert_list_free(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;
|
2013-05-09 13:56:54 +02:00
|
|
|
r = trusted_dirs_load_certs_from_string(
|
|
|
|
contents,
|
|
|
|
TRUSTED_DIRS_CERTS_SRC_FROM_STORE, 1);
|
2007-05-22 19:58:25 +02:00
|
|
|
tor_free(contents);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2008-02-18 19:14:34 +01:00
|
|
|
/** Helper: return true iff we already have loaded the exact cert
|
|
|
|
* <b>cert</b>. */
|
|
|
|
static INLINE int
|
|
|
|
already_have_cert(authority_cert_t *cert)
|
|
|
|
{
|
|
|
|
cert_list_t *cl = get_cert_list(cert->cache_info.identity_digest);
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH(cl->certs, authority_cert_t *, c,
|
|
|
|
{
|
2011-05-10 22:23:43 +02:00
|
|
|
if (tor_memeq(c->cache_info.signed_descriptor_digest,
|
2008-02-18 19:14:34 +01:00
|
|
|
cert->cache_info.signed_descriptor_digest,
|
|
|
|
DIGEST_LEN))
|
|
|
|
return 1;
|
|
|
|
});
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/** Load a bunch of new key certificates from the string <b>contents</b>. If
|
2013-05-09 13:56:54 +02:00
|
|
|
* <b>source</b> is TRUSTED_DIRS_CERTS_SRC_FROM_STORE, the certificates are
|
|
|
|
* from the cache, and we don't need to flush them to disk. If we are a
|
|
|
|
* dirauth loading our own cert, source is TRUSTED_DIRS_CERTS_SRC_SELF.
|
|
|
|
* Otherwise, source is download type: TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST
|
|
|
|
* or TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_SK_DIGEST. If <b>flush</b> is true, we
|
|
|
|
* need to flush any changed certificates to disk now. Return 0 on success,
|
|
|
|
* -1 if any certs fail to parse.
|
|
|
|
*/
|
|
|
|
|
2007-05-22 19:58:25 +02:00
|
|
|
int
|
2013-05-09 13:56:54 +02:00
|
|
|
trusted_dirs_load_certs_from_string(const char *contents, int source,
|
2008-02-21 00:20:36 +01:00
|
|
|
int flush)
|
2007-05-22 19:58:25 +02:00
|
|
|
{
|
2012-09-10 21:55:27 +02:00
|
|
|
dir_server_t *ds;
|
2007-05-22 19:58:25 +02:00
|
|
|
const char *s, *eos;
|
2009-08-20 22:50:51 +02:00
|
|
|
int failure_code = 0;
|
2013-05-09 13:56:54 +02:00
|
|
|
int from_store = (source == TRUSTED_DIRS_CERTS_SRC_FROM_STORE);
|
2007-05-22 19:58:25 +02:00
|
|
|
|
|
|
|
for (s = contents; *s; s = eos) {
|
|
|
|
authority_cert_t *cert = authority_cert_parse_from_string(s, &eos);
|
2008-02-18 19:14:34 +01:00
|
|
|
cert_list_t *cl;
|
2009-08-20 22:50:51 +02:00
|
|
|
if (!cert) {
|
|
|
|
failure_code = -1;
|
2007-05-22 19:58:25 +02:00
|
|
|
break;
|
2009-08-20 22:50:51 +02:00
|
|
|
}
|
2007-05-22 19:58:25 +02:00
|
|
|
ds = trusteddirserver_get_by_v3_auth_digest(
|
|
|
|
cert->cache_info.identity_digest);
|
2008-02-17 20:51:33 +01:00
|
|
|
log_debug(LD_DIR, "Parsed certificate for %s",
|
|
|
|
ds ? ds->nickname : "unknown authority");
|
2008-01-27 00:18:30 +01:00
|
|
|
|
2008-02-18 19:14:34 +01:00
|
|
|
if (already_have_cert(cert)) {
|
|
|
|
/* we already have this one. continue. */
|
|
|
|
log_info(LD_DIR, "Skipping %s certificate for %s that we "
|
|
|
|
"already have.",
|
|
|
|
from_store ? "cached" : "downloaded",
|
2010-04-20 00:35:57 +02:00
|
|
|
ds ? ds->nickname : "an old or new authority");
|
2008-11-07 15:01:44 +01:00
|
|
|
|
2013-05-09 18:41:50 +02:00
|
|
|
/*
|
|
|
|
* A duplicate on download should be treated as a failure, so we call
|
|
|
|
* authority_cert_dl_failed() to reset the download status to make sure
|
|
|
|
* we can't try again. Since we've implemented the fp-sk mechanism
|
|
|
|
* to download certs by signing key, this should be much rarer than it
|
|
|
|
* was and is perhaps cause for concern.
|
|
|
|
*/
|
2008-11-07 15:01:44 +01:00
|
|
|
if (!from_store) {
|
2012-06-28 09:21:06 +02:00
|
|
|
if (authdir_mode(get_options())) {
|
|
|
|
log_warn(LD_DIR,
|
|
|
|
"Got a certificate for %s, but we already have it. "
|
|
|
|
"Maybe they haven't updated it. Waiting for a while.",
|
|
|
|
ds ? ds->nickname : "an old or new authority");
|
|
|
|
} else {
|
|
|
|
log_info(LD_DIR,
|
|
|
|
"Got a certificate for %s, but we already have it. "
|
|
|
|
"Maybe they haven't updated it. Waiting for a while.",
|
|
|
|
ds ? ds->nickname : "an old or new authority");
|
|
|
|
}
|
|
|
|
|
2013-05-09 13:56:54 +02:00
|
|
|
/*
|
|
|
|
* This is where we care about the source; authority_cert_dl_failed()
|
|
|
|
* needs to know whether the download was by fp or (fp,sk) pair to
|
|
|
|
* twiddle the right bit in the download map.
|
|
|
|
*/
|
|
|
|
if (source == TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST) {
|
|
|
|
authority_cert_dl_failed(cert->cache_info.identity_digest,
|
|
|
|
NULL, 404);
|
|
|
|
} else if (source == TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_SK_DIGEST) {
|
|
|
|
authority_cert_dl_failed(cert->cache_info.identity_digest,
|
|
|
|
cert->signing_key_digest, 404);
|
|
|
|
}
|
2008-11-07 15:01:44 +01:00
|
|
|
}
|
|
|
|
|
2008-02-18 19:14:34 +01:00
|
|
|
authority_cert_free(cert);
|
2007-07-26 22:49:04 +02:00
|
|
|
continue;
|
2008-02-18 19:14:34 +01:00
|
|
|
}
|
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 {
|
2012-03-30 21:20:06 +02:00
|
|
|
int adding = directory_caches_unknown_auth_certs(get_options());
|
2009-02-10 21:28:30 +01:00
|
|
|
log_info(LD_DIR, "%s %s certificate for unrecognized directory "
|
2008-01-27 00:18:30 +01:00
|
|
|
"authority with signing key %s",
|
2009-02-10 21:28:30 +01:00
|
|
|
adding ? "Adding" : "Not adding",
|
2008-01-27 00:18:30 +01:00
|
|
|
from_store ? "cached" : "downloaded",
|
|
|
|
hex_str(cert->signing_key_digest,DIGEST_LEN));
|
2009-02-10 21:28:30 +01:00
|
|
|
if (!adding) {
|
|
|
|
authority_cert_free(cert);
|
|
|
|
continue;
|
|
|
|
}
|
2008-01-27 00:18:30 +01:00
|
|
|
}
|
2007-11-08 17:19:04 +01:00
|
|
|
|
2008-02-18 19:14:34 +01:00
|
|
|
cl = get_cert_list(cert->cache_info.identity_digest);
|
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)) {
|
2008-07-24 15:44:04 +02:00
|
|
|
char *a = tor_dup_ip(cert->addr);
|
2007-11-01 05:14:23 +01:00
|
|
|
log_notice(LD_DIR, "Updating address for directory authority %s "
|
2009-08-20 22:50:51 +02:00
|
|
|
"from %s:%d to %s:%d based on certificate.",
|
2007-11-01 05:14:23 +01:00
|
|
|
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
|
|
|
|
2008-02-21 00:20:36 +01:00
|
|
|
if (flush)
|
|
|
|
trusted_dirs_flush_certs_to_disk();
|
2007-07-28 02:11:34 +02:00
|
|
|
|
2009-08-20 22:50:51 +02:00
|
|
|
/* call this even if failure_code is <0, since some certs might have
|
|
|
|
* succeeded. */
|
2007-10-08 23:21:50 +02:00
|
|
|
networkstatus_note_certs_arrived();
|
2009-08-20 22:50:51 +02:00
|
|
|
|
|
|
|
return failure_code;
|
2007-05-22 19:58:25 +02:00
|
|
|
}
|
|
|
|
|
2008-02-06 13:37:25 +01:00
|
|
|
/** Save all v3 key certificates 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;
|
|
|
|
|
2012-01-18 21:53:30 +01:00
|
|
|
chunks = smartlist_new();
|
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-02-06 06:31:21 +01:00
|
|
|
} DIGESTMAP_FOREACH_END;
|
2008-01-27 00:18:30 +01:00
|
|
|
|
2007-10-17 18:55:44 +02:00
|
|
|
filename = get_datadir_fname("cached-certs");
|
2013-09-01 01:24:07 +02:00
|
|
|
if (write_chunks_to_file(filename, chunks, 0, 0)) {
|
2007-05-22 19:58:25 +02:00
|
|
|
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)
|
|
|
|
{
|
2008-11-11 17:01:57 +01:00
|
|
|
time_t now = time(NULL);
|
|
|
|
#define DEAD_CERT_LIFETIME (2*24*60*60)
|
|
|
|
#define OLD_CERT_LIFETIME (7*24*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-11-11 17:01:57 +01:00
|
|
|
if (newest) {
|
|
|
|
const time_t newest_published = newest->cache_info.published_on;
|
|
|
|
SMARTLIST_FOREACH_BEGIN(cl->certs, authority_cert_t *, cert) {
|
|
|
|
int expired;
|
|
|
|
time_t cert_published;
|
|
|
|
if (newest == cert)
|
|
|
|
continue;
|
2011-03-25 20:27:06 +01:00
|
|
|
expired = now > cert->expires;
|
2008-11-11 17:01:57 +01:00
|
|
|
cert_published = cert->cache_info.published_on;
|
|
|
|
/* Store expired certs for 48 hours after a newer arrives;
|
|
|
|
*/
|
|
|
|
if (expired ?
|
|
|
|
(newest_published + DEAD_CERT_LIFETIME < now) :
|
|
|
|
(cert_published + OLD_CERT_LIFETIME < newest_published)) {
|
|
|
|
SMARTLIST_DEL_CURRENT(cl->certs, cert);
|
|
|
|
authority_cert_free(cert);
|
|
|
|
trusted_dir_servers_certs_changed = 1;
|
|
|
|
}
|
|
|
|
} SMARTLIST_FOREACH_END(cert);
|
|
|
|
}
|
2008-02-06 06:31:21 +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-05-12 06:22:01 +02:00
|
|
|
|
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
|
2008-12-22 15:56:16 +01:00
|
|
|
* digest <b>sk_digest</b>. Return NULL if no such certificate is known.
|
2007-10-04 18:21:58 +02:00
|
|
|
*/
|
2007-08-14 23:09:30 +02:00
|
|
|
authority_cert_t *
|
|
|
|
authority_cert_get_by_sk_digest(const char *sk_digest)
|
|
|
|
{
|
2008-05-12 06:22:01 +02:00
|
|
|
authority_cert_t *c;
|
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
|
|
|
|
2008-05-12 06:22:01 +02:00
|
|
|
if ((c = get_my_v3_authority_cert()) &&
|
2011-05-10 22:23:43 +02:00
|
|
|
tor_memeq(c->signing_key_digest, sk_digest, DIGEST_LEN))
|
2008-05-12 06:22:01 +02:00
|
|
|
return c;
|
|
|
|
if ((c = get_my_v3_legacy_cert()) &&
|
2011-05-10 22:23:43 +02:00
|
|
|
tor_memeq(c->signing_key_digest, sk_digest, DIGEST_LEN))
|
2008-05-12 06:22:01 +02:00
|
|
|
return c;
|
|
|
|
|
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
|
|
|
{
|
2011-05-10 22:23:43 +02:00
|
|
|
if (tor_memeq(cert->signing_key_digest, sk_digest, DIGEST_LEN))
|
2007-08-14 23:09:30 +02:00
|
|
|
return cert;
|
|
|
|
});
|
2008-02-06 06:31:21 +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,
|
2011-05-10 22:23:43 +02:00
|
|
|
if (tor_memeq(cert->signing_key_digest, sk_digest, DIGEST_LEN))
|
2007-07-26 22:49:04 +02:00
|
|
|
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-02-06 17:58:05 +01:00
|
|
|
/** Add every known authority_cert_t to <b>certs_out</b>. */
|
2008-01-27 00:18:30 +01:00
|
|
|
void
|
|
|
|
authority_cert_get_all(smartlist_t *certs_out)
|
|
|
|
{
|
2008-02-06 17:58:05 +01:00
|
|
|
tor_assert(certs_out);
|
2008-01-27 00:18:30 +01:00
|
|
|
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));
|
2008-02-06 06:31:21 +01:00
|
|
|
} DIGESTMAP_FOREACH_END;
|
2008-01-27 00:18:30 +01:00
|
|
|
}
|
|
|
|
|
2008-02-06 17:58:05 +01:00
|
|
|
/** Called when an attempt to download a certificate with the authority with
|
2013-05-09 13:56:54 +02:00
|
|
|
* ID <b>id_digest</b> and, if not NULL, signed with key signing_key_digest
|
|
|
|
* fails with HTTP response code <b>status</b>: remember the failure, so we
|
|
|
|
* don't try again immediately. */
|
2008-01-27 00:18:30 +01:00
|
|
|
void
|
2013-05-09 13:56:54 +02:00
|
|
|
authority_cert_dl_failed(const char *id_digest,
|
|
|
|
const char *signing_key_digest, int status)
|
2008-01-27 00:18:30 +01:00
|
|
|
{
|
|
|
|
cert_list_t *cl;
|
2013-05-09 13:56:54 +02:00
|
|
|
download_status_t *dlstatus = NULL;
|
|
|
|
char id_digest_str[2*DIGEST_LEN+1];
|
|
|
|
char sk_digest_str[2*DIGEST_LEN+1];
|
|
|
|
|
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;
|
|
|
|
|
2013-05-09 13:56:54 +02:00
|
|
|
/*
|
|
|
|
* Are we noting a failed download of the latest cert for the id digest,
|
|
|
|
* or of a download by (id, signing key) digest pair?
|
|
|
|
*/
|
|
|
|
if (!signing_key_digest) {
|
|
|
|
/* Just by id digest */
|
|
|
|
download_status_failed(&cl->dl_status_by_id, status);
|
|
|
|
} else {
|
|
|
|
/* Reset by (id, signing key) digest pair
|
|
|
|
*
|
|
|
|
* Look for a download_status_t in the map with this digest
|
|
|
|
*/
|
|
|
|
dlstatus = dsmap_get(cl->dl_status_map, signing_key_digest);
|
|
|
|
/* Got one? */
|
|
|
|
if (dlstatus) {
|
|
|
|
download_status_failed(dlstatus, status);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Do this rather than hex_str(), since hex_str clobbers
|
|
|
|
* old results and we call twice in the param list.
|
|
|
|
*/
|
|
|
|
base16_encode(id_digest_str, sizeof(id_digest_str),
|
|
|
|
id_digest, DIGEST_LEN);
|
|
|
|
base16_encode(sk_digest_str, sizeof(sk_digest_str),
|
|
|
|
signing_key_digest, DIGEST_LEN);
|
2013-05-09 18:33:32 +02:00
|
|
|
log_warn(LD_BUG,
|
2013-05-09 13:56:54 +02:00
|
|
|
"Got failure for cert fetch with (fp,sk) = (%s,%s), with "
|
|
|
|
"status %d, but knew nothing about the download.",
|
|
|
|
id_digest_str, sk_digest_str, status);
|
|
|
|
}
|
|
|
|
}
|
2008-01-27 00:18:30 +01:00
|
|
|
}
|
|
|
|
|
2010-09-02 22:42:18 +02:00
|
|
|
/** Return true iff when we've been getting enough failures when trying to
|
|
|
|
* download the certificate with ID digest <b>id_digest</b> that we're willing
|
|
|
|
* to start bugging the user about it. */
|
|
|
|
int
|
|
|
|
authority_cert_dl_looks_uncertain(const char *id_digest)
|
|
|
|
{
|
|
|
|
#define N_AUTH_CERT_DL_FAILURES_TO_BUG_USER 2
|
|
|
|
cert_list_t *cl;
|
|
|
|
int n_failures;
|
|
|
|
if (!trusted_dir_certs ||
|
|
|
|
!(cl = digestmap_get(trusted_dir_certs, id_digest)))
|
|
|
|
return 0;
|
|
|
|
|
2013-05-09 13:56:54 +02:00
|
|
|
n_failures = download_status_get_n_failures(&cl->dl_status_by_id);
|
2010-09-02 22:42:18 +02:00
|
|
|
return n_failures >= N_AUTH_CERT_DL_FAILURES_TO_BUG_USER;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2013-05-09 13:56:54 +02:00
|
|
|
/*
|
|
|
|
* The pending_id digestmap tracks pending certificate downloads by
|
|
|
|
* identity digest; the pending_cert digestmap tracks pending downloads
|
|
|
|
* by (identity digest, signing key digest) pairs.
|
|
|
|
*/
|
|
|
|
digestmap_t *pending_id;
|
|
|
|
fp_pair_map_t *pending_cert;
|
2008-11-07 15:57:14 +01:00
|
|
|
authority_cert_t *cert;
|
2013-05-09 13:56:54 +02:00
|
|
|
/*
|
|
|
|
* The missing_id_digests smartlist will hold a list of id digests
|
|
|
|
* we want to fetch the newest cert for; the missing_cert_digests
|
|
|
|
* smartlist will hold a list of fp_pair_t with an identity and
|
|
|
|
* signing key digest.
|
|
|
|
*/
|
|
|
|
smartlist_t *missing_cert_digests, *missing_id_digests;
|
2007-10-10 21:33:19 +02:00
|
|
|
char *resource = NULL;
|
2008-01-27 00:18:30 +01:00
|
|
|
cert_list_t *cl;
|
2012-03-30 21:20:06 +02:00
|
|
|
const int cache = directory_caches_unknown_auth_certs(get_options());
|
2013-05-09 13:56:54 +02:00
|
|
|
fp_pair_t *fp_tmp = NULL;
|
|
|
|
char id_digest_str[2*DIGEST_LEN+1];
|
|
|
|
char sk_digest_str[2*DIGEST_LEN+1];
|
2007-10-09 22:44:45 +02:00
|
|
|
|
2014-03-08 14:13:12 +01:00
|
|
|
if (should_delay_dir_fetches(get_options(), NULL))
|
2007-10-19 18:41:30 +02:00
|
|
|
return;
|
|
|
|
|
2013-05-09 13:56:54 +02:00
|
|
|
pending_cert = fp_pair_map_new();
|
|
|
|
pending_id = digestmap_new();
|
|
|
|
missing_cert_digests = smartlist_new();
|
|
|
|
missing_id_digests = smartlist_new();
|
2007-12-09 18:43:43 +01:00
|
|
|
|
2013-05-09 17:19:48 +02:00
|
|
|
/*
|
|
|
|
* First, we get the lists of already pending downloads so we don't
|
|
|
|
* duplicate effort.
|
|
|
|
*/
|
2013-05-09 13:56:54 +02:00
|
|
|
list_pending_downloads(pending_id, DIR_PURPOSE_FETCH_CERTIFICATE, "fp/");
|
|
|
|
list_pending_fpsk_downloads(pending_cert);
|
2007-12-09 18:43:43 +01:00
|
|
|
|
2013-05-09 17:19:48 +02:00
|
|
|
/*
|
|
|
|
* Now, we download any trusted authority certs we don't have by
|
|
|
|
* identity digest only. This gets the latest cert for that authority.
|
|
|
|
*/
|
2013-05-11 04:39:48 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(trusted_dir_servers, dir_server_t *, ds) {
|
2013-05-09 17:19:48 +02:00
|
|
|
int found = 0;
|
|
|
|
if (!(ds->type & V3_DIRINFO))
|
|
|
|
continue;
|
2013-05-11 04:39:48 +02:00
|
|
|
if (smartlist_contains_digest(missing_id_digests,
|
|
|
|
ds->v3_identity_digest))
|
2013-05-09 17:19:48 +02:00
|
|
|
continue;
|
|
|
|
cl = get_cert_list(ds->v3_identity_digest);
|
|
|
|
SMARTLIST_FOREACH_BEGIN(cl->certs, authority_cert_t *, cert) {
|
|
|
|
if (now < cert->expires) {
|
|
|
|
/* It's not expired, and we weren't looking for something to
|
|
|
|
* verify a consensus with. Call it done. */
|
|
|
|
download_status_reset(&(cl->dl_status_by_id));
|
|
|
|
/* No sense trying to download it specifically by signing key hash */
|
|
|
|
download_status_reset_by_sk_in_cl(cl, cert->signing_key_digest);
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} SMARTLIST_FOREACH_END(cert);
|
|
|
|
if (!found &&
|
|
|
|
download_status_is_ready(&(cl->dl_status_by_id), now,
|
2013-05-16 12:08:48 +02:00
|
|
|
get_options()->TestingCertMaxDownloadTries) &&
|
2013-05-09 17:19:48 +02:00
|
|
|
!digestmap_get(pending_id, ds->v3_identity_digest)) {
|
|
|
|
log_info(LD_DIR,
|
|
|
|
"No current certificate known for authority %s "
|
|
|
|
"(ID digest %s); launching request.",
|
|
|
|
ds->nickname, hex_str(ds->v3_identity_digest, DIGEST_LEN));
|
|
|
|
smartlist_add(missing_id_digests, ds->v3_identity_digest);
|
|
|
|
}
|
|
|
|
} SMARTLIST_FOREACH_END(ds);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Next, if we have a consensus, scan through it and look for anything
|
|
|
|
* signed with a key from a cert we don't have. Those get downloaded
|
|
|
|
* by (fp,sk) pair, but if we don't know any certs at all for the fp
|
|
|
|
* (identity digest), and it's one of the trusted dir server certs
|
|
|
|
* we started off above or a pending download in pending_id, don't
|
|
|
|
* try to get it yet. Most likely, the one we'll get for that will
|
|
|
|
* have the right signing key too, and we'd just be downloading
|
|
|
|
* redundantly.
|
|
|
|
*/
|
2007-09-08 21:08:39 +02:00
|
|
|
if (status) {
|
2009-09-16 23:01:01 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(status->voters, networkstatus_voter_info_t *,
|
|
|
|
voter) {
|
|
|
|
if (!smartlist_len(voter->sigs))
|
|
|
|
continue; /* This authority never signed this consensus, so don't
|
|
|
|
* go looking for a cert with key digest 0000000000. */
|
|
|
|
if (!cache &&
|
|
|
|
!trusteddirserver_get_by_v3_auth_digest(voter->identity_digest))
|
|
|
|
continue; /* We are not a cache, and we don't know this authority.*/
|
2013-05-09 17:19:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If we don't know *any* cert for this authority, and a download by ID
|
|
|
|
* is pending or we added it to missing_id_digests above, skip this
|
|
|
|
* one for now to avoid duplicate downloads.
|
|
|
|
*/
|
2009-09-16 23:01:01 +02:00
|
|
|
cl = get_cert_list(voter->identity_digest);
|
2013-05-09 17:19:48 +02:00
|
|
|
if (smartlist_len(cl->certs) == 0) {
|
|
|
|
/* We have no certs at all for this one */
|
|
|
|
|
|
|
|
/* Do we have a download of one pending? */
|
|
|
|
if (digestmap_get(pending_id, voter->identity_digest))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Are we about to launch a download of one due to the trusted
|
|
|
|
* dir server check above?
|
|
|
|
*/
|
2013-05-11 04:39:48 +02:00
|
|
|
if (smartlist_contains_digest(missing_id_digests,
|
|
|
|
voter->identity_digest))
|
2013-05-09 17:19:48 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-09-16 23:01:01 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(voter->sigs, document_signature_t *, sig) {
|
2008-11-07 15:57:14 +01:00
|
|
|
cert = authority_cert_get_by_digests(voter->identity_digest,
|
2009-09-16 23:01:01 +02:00
|
|
|
sig->signing_key_digest);
|
2008-11-07 16:01:49 +01:00
|
|
|
if (cert) {
|
|
|
|
if (now < cert->expires)
|
2013-05-09 13:56:54 +02:00
|
|
|
download_status_reset_by_sk_in_cl(cl, sig->signing_key_digest);
|
2007-10-10 21:33:19 +02:00
|
|
|
continue;
|
|
|
|
}
|
2013-05-09 13:56:54 +02:00
|
|
|
if (download_status_is_ready_by_sk_in_cl(
|
|
|
|
cl, sig->signing_key_digest,
|
2013-05-16 12:08:48 +02:00
|
|
|
now, get_options()->TestingCertMaxDownloadTries) &&
|
2013-05-09 13:56:54 +02:00
|
|
|
!fp_pair_map_get_by_digests(pending_cert,
|
|
|
|
voter->identity_digest,
|
|
|
|
sig->signing_key_digest)) {
|
|
|
|
/*
|
|
|
|
* Do this rather than hex_str(), since hex_str clobbers
|
|
|
|
* old results and we call twice in the param list.
|
|
|
|
*/
|
|
|
|
base16_encode(id_digest_str, sizeof(id_digest_str),
|
|
|
|
voter->identity_digest, DIGEST_LEN);
|
|
|
|
base16_encode(sk_digest_str, sizeof(sk_digest_str),
|
|
|
|
sig->signing_key_digest, DIGEST_LEN);
|
|
|
|
|
|
|
|
if (voter->nickname) {
|
|
|
|
log_info(LD_DIR,
|
|
|
|
"We're missing a certificate from authority %s "
|
|
|
|
"(ID digest %s) with signing key %s: "
|
|
|
|
"launching request.",
|
|
|
|
voter->nickname, id_digest_str, sk_digest_str);
|
|
|
|
} else {
|
|
|
|
log_info(LD_DIR,
|
|
|
|
"We're missing a certificate from authority ID digest "
|
|
|
|
"%s with signing key %s: launching request.",
|
|
|
|
id_digest_str, sk_digest_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate a new fp_pair_t to append */
|
|
|
|
fp_tmp = tor_malloc(sizeof(*fp_tmp));
|
|
|
|
memcpy(fp_tmp->first, voter->identity_digest, sizeof(fp_tmp->first));
|
|
|
|
memcpy(fp_tmp->second, sig->signing_key_digest,
|
|
|
|
sizeof(fp_tmp->second));
|
|
|
|
smartlist_add(missing_cert_digests, fp_tmp);
|
2007-11-08 17:19:04 +01:00
|
|
|
}
|
2009-09-16 23:01:01 +02:00
|
|
|
} SMARTLIST_FOREACH_END(sig);
|
|
|
|
} SMARTLIST_FOREACH_END(voter);
|
2007-09-08 21:08:39 +02:00
|
|
|
}
|
|
|
|
|
2013-05-09 13:56:54 +02:00
|
|
|
/* Do downloads by identity digest */
|
|
|
|
if (smartlist_len(missing_id_digests) > 0) {
|
2013-05-09 18:31:39 +02:00
|
|
|
int need_plus = 0;
|
2012-01-18 21:53:30 +01:00
|
|
|
smartlist_t *fps = smartlist_new();
|
2013-05-09 18:31:39 +02:00
|
|
|
|
2007-10-09 19:40:23 +02:00
|
|
|
smartlist_add(fps, tor_strdup("fp/"));
|
2013-05-09 13:56:54 +02:00
|
|
|
|
2013-05-09 18:31:39 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(missing_id_digests, const char *, d) {
|
|
|
|
char *fp = NULL;
|
|
|
|
|
2013-05-09 13:56:54 +02:00
|
|
|
if (digestmap_get(pending_id, d))
|
|
|
|
continue;
|
2013-05-09 18:31:39 +02:00
|
|
|
|
|
|
|
base16_encode(id_digest_str, sizeof(id_digest_str),
|
|
|
|
d, DIGEST_LEN);
|
|
|
|
|
|
|
|
if (need_plus) {
|
|
|
|
tor_asprintf(&fp, "+%s", id_digest_str);
|
|
|
|
} else {
|
|
|
|
/* No need for tor_asprintf() in this case; first one gets no '+' */
|
|
|
|
fp = tor_strdup(id_digest_str);
|
|
|
|
need_plus = 1;
|
|
|
|
}
|
|
|
|
|
2013-05-09 13:56:54 +02:00
|
|
|
smartlist_add(fps, fp);
|
2013-05-09 18:31:39 +02:00
|
|
|
} SMARTLIST_FOREACH_END(d);
|
2013-05-09 13:56:54 +02:00
|
|
|
|
|
|
|
if (smartlist_len(fps) > 1) {
|
|
|
|
resource = smartlist_join_strings(fps, "", 0, NULL);
|
|
|
|
directory_get_from_dirserver(DIR_PURPOSE_FETCH_CERTIFICATE, 0,
|
|
|
|
resource, PDS_RETRY_IF_NO_SERVERS);
|
|
|
|
tor_free(resource);
|
2007-11-04 03:06:09 +01:00
|
|
|
}
|
2013-05-09 13:56:54 +02:00
|
|
|
/* else we didn't add any: they were all pending */
|
|
|
|
|
2007-09-08 21:08:39 +02:00
|
|
|
SMARTLIST_FOREACH(fps, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(fps);
|
|
|
|
}
|
2007-10-10 21:33:19 +02:00
|
|
|
|
2013-05-09 13:56:54 +02:00
|
|
|
/* Do downloads by identity digest/signing key pair */
|
|
|
|
if (smartlist_len(missing_cert_digests) > 0) {
|
2013-05-09 18:31:39 +02:00
|
|
|
int need_plus = 0;
|
2013-05-09 13:56:54 +02:00
|
|
|
smartlist_t *fp_pairs = smartlist_new();
|
|
|
|
|
|
|
|
smartlist_add(fp_pairs, tor_strdup("fp-sk/"));
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH_BEGIN(missing_cert_digests, const fp_pair_t *, d) {
|
2013-05-09 18:31:39 +02:00
|
|
|
char *fp_pair = NULL;
|
2013-05-09 13:56:54 +02:00
|
|
|
|
|
|
|
if (fp_pair_map_get(pending_cert, d))
|
|
|
|
continue;
|
|
|
|
|
2013-05-09 18:31:39 +02:00
|
|
|
/* Construct string encodings of the digests */
|
|
|
|
base16_encode(id_digest_str, sizeof(id_digest_str),
|
|
|
|
d->first, DIGEST_LEN);
|
|
|
|
base16_encode(sk_digest_str, sizeof(sk_digest_str),
|
|
|
|
d->second, DIGEST_LEN);
|
|
|
|
|
|
|
|
/* Now tor_asprintf() */
|
2013-05-09 13:56:54 +02:00
|
|
|
if (need_plus) {
|
2013-05-09 18:31:39 +02:00
|
|
|
tor_asprintf(&fp_pair, "+%s-%s", id_digest_str, sk_digest_str);
|
2013-05-09 13:56:54 +02:00
|
|
|
} else {
|
2013-05-09 18:31:39 +02:00
|
|
|
/* First one in the list doesn't get a '+' */
|
|
|
|
tor_asprintf(&fp_pair, "%s-%s", id_digest_str, sk_digest_str);
|
2013-05-09 13:56:54 +02:00
|
|
|
need_plus = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add it to the list of pairs to request */
|
|
|
|
smartlist_add(fp_pairs, fp_pair);
|
|
|
|
} SMARTLIST_FOREACH_END(d);
|
|
|
|
|
|
|
|
if (smartlist_len(fp_pairs) > 1) {
|
|
|
|
resource = smartlist_join_strings(fp_pairs, "", 0, NULL);
|
|
|
|
directory_get_from_dirserver(DIR_PURPOSE_FETCH_CERTIFICATE, 0,
|
|
|
|
resource, PDS_RETRY_IF_NO_SERVERS);
|
|
|
|
tor_free(resource);
|
|
|
|
}
|
|
|
|
/* else they were all pending */
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH(fp_pairs, char *, p, tor_free(p));
|
|
|
|
smartlist_free(fp_pairs);
|
|
|
|
}
|
|
|
|
|
|
|
|
smartlist_free(missing_id_digests);
|
|
|
|
SMARTLIST_FOREACH(missing_cert_digests, fp_pair_t *, p, tor_free(p));
|
|
|
|
smartlist_free(missing_cert_digests);
|
|
|
|
digestmap_free(pending_id, NULL);
|
|
|
|
fp_pair_map_free(pending_cert, 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);
|
|
|
|
}
|
|
|
|
|
2008-12-22 18:53:04 +01:00
|
|
|
/** Return the desc_store_t in <b>rl</b> that should be used to store
|
|
|
|
* <b>sd</b>. */
|
2007-08-31 17:08:37 +02:00
|
|
|
static INLINE desc_store_t *
|
2010-09-29 06:38:32 +02:00
|
|
|
desc_get_store(routerlist_t *rl, const 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-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
|
2012-10-12 18:22:13 +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;
|
2008-02-22 20:09:45 +01:00
|
|
|
return (int)(r1->published_on - r2->published_on);
|
2006-06-22 09:01:54 +02:00
|
|
|
}
|
|
|
|
|
2008-05-29 02:44:57 +02:00
|
|
|
#define RRS_FORCE 1
|
|
|
|
#define RRS_DONT_REMOVE_OLD 2
|
|
|
|
|
2008-12-19 19:51:44 +01:00
|
|
|
/** If the journal of <b>store</b> is too long, or if RRS_FORCE is set in
|
|
|
|
* <b>flags</b>, then atomically replace the saved router store with the
|
|
|
|
* routers currently in our routerlist, and clear the journal. Unless
|
|
|
|
* RRS_DONT_REMOVE_OLD is set in <b>flags</b>, delete expired routers before
|
|
|
|
* rebuilding the store. Return 0 on success, -1 on failure.
|
2005-09-13 23:14:55 +02:00
|
|
|
*/
|
2005-09-15 07:41:30 +02:00
|
|
|
static int
|
2008-05-29 02:44:57 +02:00
|
|
|
router_rebuild_store(int flags, 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;
|
2008-05-29 02:44:57 +02:00
|
|
|
int force = flags & RRS_FORCE;
|
2007-04-30 21:48:39 +02:00
|
|
|
|
2009-01-13 15:43:51 +01:00
|
|
|
if (!force && !router_should_rebuild_store(store)) {
|
|
|
|
r = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (!routerlist) {
|
|
|
|
r = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
2005-09-13 08:21:10 +02:00
|
|
|
|
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. */
|
2008-05-29 02:44:57 +02:00
|
|
|
if (!(flags & RRS_DONT_REMOVE_OLD))
|
|
|
|
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
|
|
|
|
2012-01-18 21:53:30 +01:00
|
|
|
chunk_list = smartlist_new();
|
2005-09-13 08:21:10 +02:00
|
|
|
|
2007-03-21 16:37:30 +01:00
|
|
|
/* We sort the routers by age to enhance locality on disk. */
|
2012-01-18 21:53:30 +01:00
|
|
|
signed_descriptors = smartlist_new();
|
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
|
|
|
}
|
|
|
|
|
2012-10-12 18:22:13 +02:00
|
|
|
smartlist_sort(signed_descriptors, compare_signed_descriptors_by_age_);
|
2007-04-30 21:48:39 +02:00
|
|
|
|
|
|
|
/* Now, add the appropriate members to chunk_list */
|
2012-07-17 15:33:38 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(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);
|
2012-07-17 15:33:38 +02:00
|
|
|
} SMARTLIST_FOREACH_END(sd);
|
2007-04-30 21:48:39 +02:00
|
|
|
|
2013-09-01 01:24:07 +02:00
|
|
|
if (write_chunks_to_file(fname_tmp, chunk_list, 1, 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) {
|
2014-03-18 20:39:02 +01:00
|
|
|
int res = tor_munmap_file(store->mmap);
|
|
|
|
store->mmap = NULL;
|
|
|
|
if (res != 0) {
|
2014-03-18 19:04:20 +01:00
|
|
|
log_warn(LD_FS, "Unable to munmap route store in %s", fname);
|
|
|
|
}
|
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) {
|
2008-03-17 18:21:12 +01:00
|
|
|
log_warn(LD_FS, "Error replacing old router store: %s", strerror(errno));
|
2006-12-07 16:07:33 +01:00
|
|
|
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;
|
2012-07-17 15:33:38 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(signed_descriptors, signed_descriptor_t *, sd) {
|
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 */
|
2012-07-17 15:33:38 +02:00
|
|
|
} SMARTLIST_FOREACH_END(sd);
|
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:
|
2009-12-12 08:07:59 +01:00
|
|
|
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);
|
2009-01-14 07:36:30 +01:00
|
|
|
if (chunk_list) {
|
|
|
|
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
|
|
|
{
|
2013-04-01 17:13:00 +02:00
|
|
|
char *fname = NULL, *contents = NULL;
|
2007-05-22 04:20:52 +02:00
|
|
|
struct stat st;
|
2007-08-31 17:08:37 +02:00
|
|
|
int extrainfo = (store->type == EXTRAINFO_STORE);
|
|
|
|
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);
|
2006-08-27 04:07:54 +02:00
|
|
|
|
2014-03-18 19:04:20 +01:00
|
|
|
if (store->mmap) {
|
|
|
|
/* get rid of it first */
|
2014-03-18 20:39:02 +01:00
|
|
|
int res = tor_munmap_file(store->mmap);
|
|
|
|
store->mmap = NULL;
|
|
|
|
if (res != 0) {
|
2014-03-18 19:04:20 +01:00
|
|
|
log_warn(LD_FS, "Failed to munmap %s", fname);
|
|
|
|
tor_free(fname);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2007-04-30 21:48:39 +02:00
|
|
|
|
2007-08-31 17:08:37 +02:00
|
|
|
store->mmap = tor_mmap_file(fname);
|
|
|
|
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);
|
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);
|
2005-09-15 07:19:38 +02:00
|
|
|
|
2013-04-01 17:13:00 +02:00
|
|
|
if (store->journal_len) {
|
2005-09-15 07:19:38 +02:00
|
|
|
/* Always clear the journal on startup.*/
|
2008-05-29 02:44:57 +02:00
|
|
|
router_rebuild_store(RRS_FORCE, 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;
|
|
|
|
}
|
|
|
|
|
2012-09-10 21:55:27 +02:00
|
|
|
/** Return a smartlist containing a list of dir_server_t * for all
|
2006-04-10 03:35:56 +02:00
|
|
|
* known trusted dirservers. Callers must not modify the list or its
|
|
|
|
* contents.
|
2004-10-15 21:04:38 +02:00
|
|
|
*/
|
2012-09-08 04:55:53 +02:00
|
|
|
const smartlist_t *
|
2006-04-10 03:35:56 +02:00
|
|
|
router_get_trusted_dir_servers(void)
|
2004-10-15 21:04:38 +02:00
|
|
|
{
|
|
|
|
if (!trusted_dir_servers)
|
2012-01-18 21:53:30 +01:00
|
|
|
trusted_dir_servers = smartlist_new();
|
2004-10-15 21:04:38 +02:00
|
|
|
|
2006-04-10 03:35:56 +02:00
|
|
|
return trusted_dir_servers;
|
2004-10-15 21:04:38 +02:00
|
|
|
}
|
|
|
|
|
2012-09-10 21:23:39 +02:00
|
|
|
const smartlist_t *
|
|
|
|
router_get_fallback_dir_servers(void)
|
|
|
|
{
|
|
|
|
if (!fallback_dir_servers)
|
|
|
|
fallback_dir_servers = smartlist_new();
|
|
|
|
|
|
|
|
return fallback_dir_servers;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2010-09-29 07:35:08 +02:00
|
|
|
const routerstatus_t *
|
2010-11-08 20:27:36 +01:00
|
|
|
router_pick_directory_server(dirinfo_type_t type, int flags)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2010-09-29 07:35:08 +02:00
|
|
|
const routerstatus_t *choice;
|
2007-10-24 20:42:16 +02:00
|
|
|
if (get_options()->PreferTunneledDirConns)
|
2012-10-12 18:22:13 +02:00
|
|
|
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 */
|
2012-09-10 21:23:39 +02:00
|
|
|
mark_all_dirservers_up(fallback_dir_servers);
|
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-02-26 23:56:36 +01:00
|
|
|
return choice;
|
|
|
|
}
|
|
|
|
|
2012-09-10 21:55:27 +02:00
|
|
|
/** Return the dir_server_t for the directory authority whose identity
|
2005-12-14 23:00:58 +01:00
|
|
|
* key hashes to <b>digest</b>, or NULL if no such authority is known.
|
|
|
|
*/
|
2012-09-10 21:55:27 +02:00
|
|
|
dir_server_t *
|
2005-09-08 08:22:44 +02:00
|
|
|
router_get_trusteddirserver_by_digest(const char *digest)
|
|
|
|
{
|
|
|
|
if (!trusted_dir_servers)
|
|
|
|
return NULL;
|
|
|
|
|
2012-09-10 21:55:27 +02:00
|
|
|
SMARTLIST_FOREACH(trusted_dir_servers, dir_server_t *, ds,
|
2005-09-08 08:22:44 +02:00
|
|
|
{
|
2011-05-10 22:23:43 +02:00
|
|
|
if (tor_memeq(ds->digest, digest, DIGEST_LEN))
|
2005-09-08 08:22:44 +02:00
|
|
|
return ds;
|
|
|
|
});
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-09-10 21:55:27 +02:00
|
|
|
/** Return the dir_server_t for the fallback dirserver whose identity
|
2012-09-10 21:23:39 +02:00
|
|
|
* key hashes to <b>digest</b>, or NULL if no such authority is known.
|
|
|
|
*/
|
2012-09-10 21:55:27 +02:00
|
|
|
dir_server_t *
|
2012-09-10 21:23:39 +02:00
|
|
|
router_get_fallback_dirserver_by_digest(const char *digest)
|
|
|
|
{
|
|
|
|
if (!trusted_dir_servers)
|
|
|
|
return NULL;
|
|
|
|
|
2012-09-10 21:55:27 +02:00
|
|
|
SMARTLIST_FOREACH(trusted_dir_servers, dir_server_t *, ds,
|
2012-09-10 21:23:39 +02:00
|
|
|
{
|
|
|
|
if (tor_memeq(ds->digest, digest, DIGEST_LEN))
|
|
|
|
return ds;
|
|
|
|
});
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-09-10 21:55:27 +02:00
|
|
|
/** Return the dir_server_t for the directory authority whose
|
2010-04-20 08:48:35 +02:00
|
|
|
* v3 identity key hashes to <b>digest</b>, or NULL if no such authority
|
|
|
|
* is known.
|
2007-05-22 19:58:25 +02:00
|
|
|
*/
|
2012-09-10 21:55:27 +02:00
|
|
|
dir_server_t *
|
2007-05-22 19:58:25 +02:00
|
|
|
trusteddirserver_get_by_v3_auth_digest(const char *digest)
|
|
|
|
{
|
|
|
|
if (!trusted_dir_servers)
|
|
|
|
return NULL;
|
|
|
|
|
2012-09-10 21:55:27 +02:00
|
|
|
SMARTLIST_FOREACH(trusted_dir_servers, dir_server_t *, ds,
|
2007-05-22 19:58:25 +02:00
|
|
|
{
|
2011-05-10 22:23:43 +02:00
|
|
|
if (tor_memeq(ds->v3_identity_digest, digest, DIGEST_LEN) &&
|
2010-11-08 20:35:02 +01:00
|
|
|
(ds->type & V3_DIRINFO))
|
2007-05-22 19:58:25 +02:00
|
|
|
return ds;
|
|
|
|
});
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-09-10 21:23:39 +02:00
|
|
|
/** Try to find a running directory authority. Flags are as for
|
2007-10-24 20:42:16 +02:00
|
|
|
* router_pick_directory_server.
|
2005-06-21 01:04:13 +02:00
|
|
|
*/
|
2010-09-29 07:35:08 +02:00
|
|
|
const routerstatus_t *
|
2010-11-08 20:27:36 +01:00
|
|
|
router_pick_trusteddirserver(dirinfo_type_t type, int flags)
|
2012-09-10 21:23:39 +02:00
|
|
|
{
|
|
|
|
return router_pick_dirserver_generic(trusted_dir_servers, type, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Try to find a running fallback directory Flags are as for
|
|
|
|
* router_pick_directory_server.
|
|
|
|
*/
|
|
|
|
const routerstatus_t *
|
|
|
|
router_pick_fallback_dirserver(dirinfo_type_t type, int flags)
|
|
|
|
{
|
|
|
|
return router_pick_dirserver_generic(fallback_dir_servers, type, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Try to find a running fallback directory Flags are as for
|
|
|
|
* router_pick_directory_server.
|
|
|
|
*/
|
|
|
|
static const routerstatus_t *
|
|
|
|
router_pick_dirserver_generic(smartlist_t *sourcelist,
|
|
|
|
dirinfo_type_t type, int flags)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2010-09-29 07:35:08 +02:00
|
|
|
const routerstatus_t *choice;
|
2008-12-11 20:12:48 +01:00
|
|
|
int busy = 0;
|
2007-10-24 20:42:16 +02:00
|
|
|
if (get_options()->PreferTunneledDirConns)
|
2012-10-12 18:22:13 +02:00
|
|
|
flags |= PDS_PREFER_TUNNELED_DIR_CONNS_;
|
2004-10-12 17:55:20 +02:00
|
|
|
|
2012-09-10 21:23:39 +02:00
|
|
|
choice = router_pick_trusteddirserver_impl(sourcelist, type, flags, &busy);
|
2007-10-24 20:42:16 +02:00
|
|
|
if (choice || !(flags & PDS_RETRY_IF_NO_SERVERS))
|
2004-10-12 17:55:20 +02:00
|
|
|
return choice;
|
2008-12-11 20:12:48 +01:00
|
|
|
if (busy) {
|
|
|
|
/* If the reason that we got no server is that servers are "busy",
|
|
|
|
* we must be excluding good servers because we already have serverdesc
|
|
|
|
* fetches with them. Do not mark down servers up because of this. */
|
2010-05-11 23:20:33 +02:00
|
|
|
tor_assert((flags & (PDS_NO_EXISTING_SERVERDESC_FETCH|
|
|
|
|
PDS_NO_EXISTING_MICRODESC_FETCH)));
|
2008-12-11 20:12:48 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2004-10-12 17:55:20 +02:00
|
|
|
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_DIR,
|
2012-09-10 21:23:39 +02:00
|
|
|
"No dirservers are reachable. Trying them all again.");
|
|
|
|
mark_all_dirservers_up(sourcelist);
|
|
|
|
return router_pick_trusteddirserver_impl(sourcelist, type, flags, NULL);
|
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
|
|
|
*
|
2012-10-12 18:22:13 +02:00
|
|
|
* If the PDS_PREFER_TUNNELED_DIR_CONNS_ flag is set, prefer directory servers
|
2007-10-24 20:42:16 +02:00
|
|
|
* that we can use with BEGINDIR.
|
2004-07-20 08:44:16 +02:00
|
|
|
*/
|
2010-09-29 07:35:08 +02:00
|
|
|
static const routerstatus_t *
|
2010-11-08 20:27:36 +01:00
|
|
|
router_pick_directory_server_impl(dirinfo_type_t type, int flags)
|
2004-07-20 08:44:16 +02:00
|
|
|
{
|
2011-06-14 19:01:38 +02:00
|
|
|
const or_options_t *options = get_options();
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
const node_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);
|
2012-10-12 18:22:13 +02:00
|
|
|
int prefer_tunnel = (flags & PDS_PREFER_TUNNELED_DIR_CONNS_);
|
2013-02-14 18:06:59 +01:00
|
|
|
int for_guard = (flags & PDS_FOR_GUARD);
|
2011-03-11 09:09:24 +01:00
|
|
|
int try_excluding = 1, n_excluded = 0;
|
2007-10-16 01:15:24 +02:00
|
|
|
|
|
|
|
if (!consensus)
|
|
|
|
return NULL;
|
2002-09-26 14:09:10 +02:00
|
|
|
|
2011-03-11 09:09:24 +01:00
|
|
|
retry_without_exclude:
|
|
|
|
|
2012-01-18 21:53:30 +01:00
|
|
|
direct = smartlist_new();
|
|
|
|
tunnel = smartlist_new();
|
|
|
|
trusted_direct = smartlist_new();
|
|
|
|
trusted_tunnel = smartlist_new();
|
|
|
|
overloaded_direct = smartlist_new();
|
|
|
|
overloaded_tunnel = smartlist_new();
|
2007-01-03 07:33:03 +01:00
|
|
|
|
2004-05-04 20:17:45 +02:00
|
|
|
/* Find all the running dirservers we know about. */
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(nodelist_get_list(), const node_t *, node) {
|
2006-01-10 00:39:07 +01:00
|
|
|
int is_trusted;
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
int is_overloaded;
|
2008-08-05 22:08:19 +02:00
|
|
|
tor_addr_t addr;
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
const routerstatus_t *status = node->rs;
|
2011-04-27 20:36:30 +02:00
|
|
|
const country_t country = node->country;
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
if (!status)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!node->is_running || !status->dir_port || !node->is_valid)
|
2004-07-20 08:44:16 +02:00
|
|
|
continue;
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
if (node->is_bad_directory)
|
2007-01-03 20:58:00 +01:00
|
|
|
continue;
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
if (requireother && router_digest_is_me(node->identity))
|
2004-07-20 08:44:16 +02:00
|
|
|
continue;
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
is_trusted = router_digest_is_trusted_dir(node->identity);
|
2010-11-08 20:35:02 +01:00
|
|
|
if ((type & EXTRAINFO_DIRINFO) &&
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
!router_supports_extrainfo(node->identity, 0))
|
2007-05-18 23:19:19 +02:00
|
|
|
continue;
|
2010-11-08 20:21:32 +01:00
|
|
|
if ((type & MICRODESC_DIRINFO) && !is_trusted &&
|
|
|
|
!node->rs->version_supports_microdesc_cache)
|
|
|
|
continue;
|
2013-02-14 18:06:59 +01:00
|
|
|
if (for_guard && node->using_as_guard)
|
|
|
|
continue; /* Don't make the same node a guard twice. */
|
2011-07-07 17:52:13 +02:00
|
|
|
if (try_excluding &&
|
2011-04-27 20:36:30 +02:00
|
|
|
routerset_contains_routerstatus(options->ExcludeNodes, status,
|
|
|
|
country)) {
|
2011-03-11 09:09:24 +01:00
|
|
|
++n_excluded;
|
|
|
|
continue;
|
|
|
|
}
|
2008-08-05 22:08:19 +02:00
|
|
|
|
2008-12-18 17:11:24 +01:00
|
|
|
/* XXXX IP6 proposal 118 */
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
tor_addr_from_ipv4h(&addr, node->rs->addr);
|
|
|
|
|
|
|
|
is_overloaded = status->last_dir_503_at + DIR_503_TIMEOUT > now;
|
2008-08-05 22:08:19 +02:00
|
|
|
|
2007-04-27 12:26:09 +02:00
|
|
|
if (prefer_tunnel &&
|
|
|
|
(!fascistfirewall ||
|
2008-08-05 22:08:19 +02:00
|
|
|
fascist_firewall_allows_address_or(&addr, status->or_port)))
|
2007-01-03 07:33:03 +01:00
|
|
|
smartlist_add(is_trusted ? trusted_tunnel :
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
is_overloaded ? overloaded_tunnel : tunnel, (void*)node);
|
2007-04-27 12:26:09 +02:00
|
|
|
else if (!fascistfirewall ||
|
2008-08-05 22:08:19 +02:00
|
|
|
fascist_firewall_allows_address_dir(&addr, status->dir_port))
|
2007-01-03 07:33:03 +01:00
|
|
|
smartlist_add(is_trusted ? trusted_direct :
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
is_overloaded ? overloaded_direct : direct, (void*)node);
|
|
|
|
} SMARTLIST_FOREACH_END(node);
|
2002-09-26 14:09:10 +02:00
|
|
|
|
2007-01-03 07:33:03 +01:00
|
|
|
if (smartlist_len(tunnel)) {
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
result = node_sl_choose_by_bandwidth(tunnel, WEIGHT_FOR_DIR);
|
2007-01-03 07:33:03 +01:00
|
|
|
} else if (smartlist_len(overloaded_tunnel)) {
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
result = node_sl_choose_by_bandwidth(overloaded_tunnel,
|
2010-01-28 00:01:51 +01:00
|
|
|
WEIGHT_FOR_DIR);
|
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)) {
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
result = node_sl_choose_by_bandwidth(direct, WEIGHT_FOR_DIR);
|
2007-01-03 07:33:03 +01:00
|
|
|
} else if (smartlist_len(overloaded_direct)) {
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
result = node_sl_choose_by_bandwidth(overloaded_direct,
|
|
|
|
WEIGHT_FOR_DIR);
|
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);
|
2011-03-11 09:09:24 +01:00
|
|
|
|
|
|
|
if (result == NULL && try_excluding && !options->StrictNodes && n_excluded) {
|
|
|
|
/* If we got no result, and we are excluding nodes, and StrictNodes is
|
|
|
|
* not set, try again without excluding nodes. */
|
|
|
|
try_excluding = 0;
|
|
|
|
n_excluded = 0;
|
|
|
|
goto retry_without_exclude;
|
|
|
|
}
|
|
|
|
|
2010-10-14 03:54:09 +02:00
|
|
|
return result ? result->rs : NULL;
|
2004-07-20 08:44:16 +02:00
|
|
|
}
|
2003-11-10 09:06:55 +01:00
|
|
|
|
2012-09-12 18:44:18 +02:00
|
|
|
/** Pick a random element from a list of dir_server_t, weighting by their
|
|
|
|
* <b>weight</b> field. */
|
|
|
|
static const dir_server_t *
|
2012-09-12 19:56:36 +02:00
|
|
|
dirserver_choose_by_weight(const smartlist_t *servers, double authority_weight)
|
2012-09-12 18:44:18 +02:00
|
|
|
{
|
|
|
|
int n = smartlist_len(servers);
|
|
|
|
int i;
|
|
|
|
u64_dbl_t *weights;
|
|
|
|
const dir_server_t *ds;
|
|
|
|
|
|
|
|
weights = tor_malloc(sizeof(u64_dbl_t) * n);
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
ds = smartlist_get(servers, i);
|
|
|
|
weights[i].dbl = ds->weight;
|
2012-09-12 19:56:36 +02:00
|
|
|
if (ds->is_authority)
|
|
|
|
weights[i].dbl *= authority_weight;
|
2012-09-12 18:44:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
scale_array_elements_to_u64(weights, n, NULL);
|
|
|
|
i = choose_array_element_by_weight(weights, n);
|
|
|
|
tor_free(weights);
|
2012-12-19 03:32:53 +01:00
|
|
|
return (i < 0) ? NULL : smartlist_get(servers, i);
|
2012-09-12 18:44:18 +02:00
|
|
|
}
|
|
|
|
|
2012-09-10 21:55:27 +02:00
|
|
|
/** Choose randomly from among the dir_server_ts in sourcelist that
|
2012-09-10 21:23:39 +02:00
|
|
|
* are up. Flags are as for router_pick_directory_server_impl().
|
2005-06-21 01:04:13 +02:00
|
|
|
*/
|
2010-09-29 07:35:08 +02:00
|
|
|
static const routerstatus_t *
|
2012-09-10 21:23:39 +02:00
|
|
|
router_pick_trusteddirserver_impl(const smartlist_t *sourcelist,
|
|
|
|
dirinfo_type_t type, int flags,
|
2008-12-11 20:12:48 +01:00
|
|
|
int *n_busy_out)
|
2004-10-12 17:55:20 +02:00
|
|
|
{
|
2011-06-14 19:01:38 +02:00
|
|
|
const or_options_t *options = get_options();
|
2007-01-03 07:33:03 +01:00
|
|
|
smartlist_t *direct, *tunnel;
|
|
|
|
smartlist_t *overloaded_direct, *overloaded_tunnel;
|
2010-09-29 06:38:32 +02:00
|
|
|
const routerinfo_t *me = router_get_my_routerinfo();
|
2012-12-17 17:14:12 +01:00
|
|
|
const routerstatus_t *result = NULL;
|
2006-12-24 03:45:35 +01:00
|
|
|
time_t now = time(NULL);
|
2008-12-11 20:12:48 +01:00
|
|
|
const int requireother = ! (flags & PDS_ALLOW_SELF);
|
|
|
|
const int fascistfirewall = ! (flags & PDS_IGNORE_FASCISTFIREWALL);
|
2012-10-12 18:22:13 +02:00
|
|
|
const int prefer_tunnel = (flags & PDS_PREFER_TUNNELED_DIR_CONNS_);
|
2008-12-11 20:12:48 +01:00
|
|
|
const int no_serverdesc_fetching =(flags & PDS_NO_EXISTING_SERVERDESC_FETCH);
|
2010-05-11 23:20:33 +02:00
|
|
|
const int no_microdesc_fetching =(flags & PDS_NO_EXISTING_MICRODESC_FETCH);
|
2012-09-12 19:56:36 +02:00
|
|
|
const double auth_weight = (sourcelist == fallback_dir_servers) ?
|
|
|
|
options->DirAuthorityFallbackRate : 1.0;
|
2012-09-12 18:44:18 +02:00
|
|
|
smartlist_t *pick_from;
|
2008-12-11 20:12:48 +01:00
|
|
|
int n_busy = 0;
|
2011-03-11 09:09:24 +01:00
|
|
|
int try_excluding = 1, n_excluded = 0;
|
2007-01-03 07:33:03 +01:00
|
|
|
|
2012-09-10 21:23:39 +02:00
|
|
|
if (!sourcelist)
|
2008-01-16 06:27:19 +01:00
|
|
|
return NULL;
|
|
|
|
|
2011-03-11 09:09:24 +01:00
|
|
|
retry_without_exclude:
|
|
|
|
|
2012-01-18 21:53:30 +01:00
|
|
|
direct = smartlist_new();
|
|
|
|
tunnel = smartlist_new();
|
|
|
|
overloaded_direct = smartlist_new();
|
|
|
|
overloaded_tunnel = smartlist_new();
|
2004-07-20 08:44:16 +02:00
|
|
|
|
2012-09-10 21:55:27 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(sourcelist, const dir_server_t *, d)
|
2004-10-12 17:55:20 +02:00
|
|
|
{
|
2007-01-03 07:33:03 +01:00
|
|
|
int is_overloaded =
|
|
|
|
d->fake_status.last_dir_503_at + DIR_503_TIMEOUT > now;
|
2008-08-05 22:08:19 +02:00
|
|
|
tor_addr_t addr;
|
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;
|
2010-11-08 20:35:02 +01:00
|
|
|
if ((type & EXTRAINFO_DIRINFO) &&
|
2007-05-18 23:19:19 +02:00
|
|
|
!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;
|
2011-07-07 17:52:13 +02:00
|
|
|
if (try_excluding &&
|
2011-03-11 09:09:24 +01:00
|
|
|
routerset_contains_routerstatus(options->ExcludeNodes,
|
2011-04-27 20:36:30 +02:00
|
|
|
&d->fake_status, -1)) {
|
2011-03-11 09:09:24 +01:00
|
|
|
++n_excluded;
|
|
|
|
continue;
|
|
|
|
}
|
2008-08-05 22:08:19 +02:00
|
|
|
|
2008-12-18 17:11:24 +01:00
|
|
|
/* XXXX IP6 proposal 118 */
|
2008-08-05 22:08:19 +02:00
|
|
|
tor_addr_from_ipv4h(&addr, d->addr);
|
|
|
|
|
2008-12-11 20:12:48 +01:00
|
|
|
if (no_serverdesc_fetching) {
|
|
|
|
if (connection_get_by_type_addr_port_purpose(
|
|
|
|
CONN_TYPE_DIR, &addr, d->dir_port, DIR_PURPOSE_FETCH_SERVERDESC)
|
|
|
|
|| connection_get_by_type_addr_port_purpose(
|
|
|
|
CONN_TYPE_DIR, &addr, d->dir_port, DIR_PURPOSE_FETCH_EXTRAINFO)) {
|
|
|
|
//log_debug(LD_DIR, "We have an existing connection to fetch "
|
|
|
|
// "descriptor from %s; delaying",d->description);
|
|
|
|
++n_busy;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2010-05-11 23:20:33 +02:00
|
|
|
if (no_microdesc_fetching) {
|
|
|
|
if (connection_get_by_type_addr_port_purpose(
|
|
|
|
CONN_TYPE_DIR, &addr, d->dir_port, DIR_PURPOSE_FETCH_MICRODESC)) {
|
|
|
|
++n_busy;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2008-12-11 20:12:48 +01:00
|
|
|
|
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 ||
|
2008-08-05 22:08:19 +02:00
|
|
|
fascist_firewall_allows_address_or(&addr, d->or_port)))
|
2012-09-12 18:44:18 +02:00
|
|
|
smartlist_add(is_overloaded ? overloaded_tunnel : tunnel, (void*)d);
|
2007-04-27 12:26:09 +02:00
|
|
|
else if (!fascistfirewall ||
|
2008-08-05 22:08:19 +02:00
|
|
|
fascist_firewall_allows_address_dir(&addr, d->dir_port))
|
2012-09-12 18:44:18 +02:00
|
|
|
smartlist_add(is_overloaded ? overloaded_direct : direct, (void*)d);
|
2008-08-05 22:08:19 +02:00
|
|
|
}
|
|
|
|
SMARTLIST_FOREACH_END(d);
|
2004-05-04 20:17:45 +02:00
|
|
|
|
2007-01-03 07:33:03 +01:00
|
|
|
if (smartlist_len(tunnel)) {
|
2012-09-12 18:44:18 +02:00
|
|
|
pick_from = tunnel;
|
2007-01-03 07:33:03 +01:00
|
|
|
} else if (smartlist_len(overloaded_tunnel)) {
|
2012-09-12 18:44:18 +02:00
|
|
|
pick_from = overloaded_tunnel;
|
2007-01-03 07:33:03 +01:00
|
|
|
} else if (smartlist_len(direct)) {
|
2012-09-12 18:44:18 +02:00
|
|
|
pick_from = direct;
|
2007-01-03 07:33:03 +01:00
|
|
|
} else {
|
2012-09-12 18:44:18 +02:00
|
|
|
pick_from = overloaded_direct;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2012-09-12 19:56:36 +02:00
|
|
|
const dir_server_t *selection =
|
|
|
|
dirserver_choose_by_weight(pick_from, auth_weight);
|
2012-09-12 18:44:18 +02:00
|
|
|
|
|
|
|
if (selection)
|
|
|
|
result = &selection->fake_status;
|
2007-01-03 07:33:03 +01:00
|
|
|
}
|
|
|
|
|
2008-12-11 20:12:48 +01:00
|
|
|
if (n_busy_out)
|
|
|
|
*n_busy_out = n_busy;
|
|
|
|
|
2007-01-03 07:33:03 +01:00
|
|
|
smartlist_free(direct);
|
|
|
|
smartlist_free(tunnel);
|
|
|
|
smartlist_free(overloaded_direct);
|
|
|
|
smartlist_free(overloaded_tunnel);
|
2011-03-11 09:09:24 +01:00
|
|
|
|
|
|
|
if (result == NULL && try_excluding && !options->StrictNodes && n_excluded) {
|
|
|
|
/* If we got no result, and we are excluding nodes, and StrictNodes is
|
|
|
|
* not set, try again without excluding nodes. */
|
|
|
|
try_excluding = 0;
|
|
|
|
n_excluded = 0;
|
|
|
|
goto retry_without_exclude;
|
|
|
|
}
|
|
|
|
|
2007-01-03 07:33:03 +01:00
|
|
|
return result;
|
2004-10-12 17:55:20 +02:00
|
|
|
}
|
|
|
|
|
2012-09-10 21:55:27 +02:00
|
|
|
/** Mark as running every dir_server_t in <b>server_list</b>. */
|
2005-06-11 20:52:12 +02:00
|
|
|
static void
|
2012-09-10 21:23:39 +02:00
|
|
|
mark_all_dirservers_up(smartlist_t *server_list)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2012-09-10 21:23:39 +02:00
|
|
|
if (server_list) {
|
2012-09-10 21:55:27 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(server_list, dir_server_t *, dir) {
|
2007-10-09 17:27:15 +02:00
|
|
|
routerstatus_t *rs;
|
2012-09-10 21:23:39 +02:00
|
|
|
node_t *node;
|
2005-09-14 23:09:25 +02:00
|
|
|
dir->is_running = 1;
|
2012-09-10 21:23:39 +02:00
|
|
|
node = node_get_mutable_by_id(dir->digest);
|
|
|
|
if (node)
|
|
|
|
node->is_running = 1;
|
2010-09-29 07:35:08 +02:00
|
|
|
rs = router_get_mutable_consensus_status_by_id(dir->digest);
|
2010-09-30 20:58:27 +02:00
|
|
|
if (rs) {
|
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);
|
|
|
|
}
|
2012-07-17 15:33:38 +02:00
|
|
|
} SMARTLIST_FOREACH_END(dir);
|
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
|
|
|
}
|
|
|
|
|
2010-09-15 04:19:00 +02:00
|
|
|
/** Return true iff r1 and r2 have the same address and OR port. */
|
|
|
|
int
|
2012-05-11 10:22:45 +02:00
|
|
|
routers_have_same_or_addrs(const routerinfo_t *r1, const routerinfo_t *r2)
|
2010-09-15 04:19:00 +02:00
|
|
|
{
|
2012-05-09 17:58:01 +02:00
|
|
|
return r1->addr == r2->addr && r1->or_port == r2->or_port &&
|
|
|
|
tor_addr_eq(&r1->ipv6_addr, &r2->ipv6_addr) &&
|
|
|
|
r1->ipv6_orport == r2->ipv6_orport;
|
2010-09-15 04:19:00 +02:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2012-09-10 21:23:39 +02:00
|
|
|
mark_all_dirservers_up(fallback_dir_servers);
|
2004-07-12 22:39:40 +02:00
|
|
|
}
|
|
|
|
|
2011-07-11 17:21:47 +02:00
|
|
|
/** Given a <b>router</b>, add every node_t in its family (including the
|
2012-06-05 01:56:16 +02:00
|
|
|
* node itself!) to <b>sl</b>.
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
*
|
|
|
|
* Note the type mismatch: This function takes a routerinfo, but adds nodes
|
|
|
|
* to the smartlist!
|
|
|
|
*/
|
|
|
|
static void
|
2011-07-11 17:21:47 +02:00
|
|
|
routerlist_add_node_and_family(smartlist_t *sl, const routerinfo_t *router)
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
{
|
2010-10-01 00:25:25 +02:00
|
|
|
/* XXXX MOVE ? */
|
|
|
|
node_t fake_node;
|
|
|
|
const node_t *node = node_get_by_id(router->cache_info.identity_digest);;
|
|
|
|
if (node == NULL) {
|
|
|
|
memset(&fake_node, 0, sizeof(fake_node));
|
|
|
|
fake_node.ri = (routerinfo_t *)router;
|
|
|
|
memcpy(fake_node.identity, router->cache_info.identity_digest, DIGEST_LEN);
|
|
|
|
node = &fake_node;
|
|
|
|
}
|
2011-07-11 17:21:47 +02:00
|
|
|
nodelist_add_node_and_family(sl, node);
|
2004-09-10 23:40:29 +02:00
|
|
|
}
|
|
|
|
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
/** Add every suitable node from our nodelist to <b>sl</b>, so that
|
2007-01-22 06:59:59 +01:00
|
|
|
* 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
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
router_add_running_nodes_to_smartlist(smartlist_t *sl, int allow_invalid,
|
|
|
|
int need_uptime, int need_capacity,
|
|
|
|
int need_guard, int need_desc)
|
|
|
|
{ /* XXXX MOVE */
|
|
|
|
SMARTLIST_FOREACH_BEGIN(nodelist_get_list(), const node_t *, node) {
|
|
|
|
if (!node->is_running ||
|
|
|
|
(!node->is_valid && !allow_invalid))
|
|
|
|
continue;
|
|
|
|
if (need_desc && !(node->ri || (node->rs && node->md)))
|
|
|
|
continue;
|
|
|
|
if (node->ri && node->ri->purpose != ROUTER_PURPOSE_GENERAL)
|
|
|
|
continue;
|
|
|
|
if (node_is_unreliable(node, need_uptime, need_capacity, need_guard))
|
|
|
|
continue;
|
2003-12-03 11:28:51 +01:00
|
|
|
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
smartlist_add(sl, (void *)node);
|
|
|
|
} SMARTLIST_FOREACH_END(node);
|
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. */
|
2010-09-29 06:38:32 +02:00
|
|
|
const 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;
|
|
|
|
}
|
|
|
|
|
2006-03-28 14:01:58 +02:00
|
|
|
/** Return the smaller of the router's configured BandwidthRate
|
|
|
|
* and its advertised capacity. */
|
|
|
|
uint32_t
|
2010-09-29 06:38:32 +02:00
|
|
|
router_get_advertised_bandwidth(const routerinfo_t *router)
|
2006-03-28 14:01:58 +02:00
|
|
|
{
|
|
|
|
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 */
|
|
|
|
|
2008-08-14 14:37:00 +02:00
|
|
|
/** Return the smaller of the router's configured BandwidthRate
|
|
|
|
* and its advertised capacity, capped by max-believe-bw. */
|
|
|
|
uint32_t
|
2010-09-29 06:38:32 +02:00
|
|
|
router_get_advertised_bandwidth_capped(const routerinfo_t *router)
|
2008-08-14 14:37:00 +02:00
|
|
|
{
|
|
|
|
uint32_t result = router->bandwidthcapacity;
|
|
|
|
if (result > router->bandwidthrate)
|
|
|
|
result = router->bandwidthrate;
|
|
|
|
if (result > DEFAULT_MAX_BELIEVABLE_BANDWIDTH)
|
|
|
|
result = DEFAULT_MAX_BELIEVABLE_BANDWIDTH;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-08-27 23:37:56 +02:00
|
|
|
/** Given an array of double/uint64_t unions that are currently being used as
|
|
|
|
* doubles, convert them to uint64_t, and try to scale them linearly so as to
|
|
|
|
* much of the range of uint64_t. If <b>total_out</b> is provided, set it to
|
|
|
|
* the sum of all elements in the array _before_ scaling. */
|
2013-06-06 23:58:28 +02:00
|
|
|
STATIC void
|
2012-08-27 23:37:56 +02:00
|
|
|
scale_array_elements_to_u64(u64_dbl_t *entries, int n_entries,
|
|
|
|
uint64_t *total_out)
|
|
|
|
{
|
|
|
|
double total = 0.0;
|
|
|
|
double scale_factor;
|
|
|
|
int i;
|
|
|
|
/* big, but far away from overflowing an int64_t */
|
|
|
|
#define SCALE_TO_U64_MAX (INT64_MAX / 4)
|
|
|
|
|
|
|
|
for (i = 0; i < n_entries; ++i)
|
|
|
|
total += entries[i].dbl;
|
|
|
|
|
|
|
|
scale_factor = SCALE_TO_U64_MAX / total;
|
|
|
|
|
|
|
|
for (i = 0; i < n_entries; ++i)
|
|
|
|
entries[i].u64 = tor_llround(entries[i].dbl * scale_factor);
|
|
|
|
|
|
|
|
if (total_out)
|
|
|
|
*total_out = (uint64_t) total;
|
|
|
|
|
|
|
|
#undef SCALE_TO_U64_MAX
|
|
|
|
}
|
|
|
|
|
2012-08-28 18:42:21 +02:00
|
|
|
/** Time-invariant 64-bit greater-than; works on two integers in the range
|
|
|
|
* (0,INT64_MAX). */
|
|
|
|
#if SIZEOF_VOID_P == 8
|
|
|
|
#define gt_i64_timei(a,b) ((a) > (b))
|
|
|
|
#else
|
|
|
|
static INLINE int
|
|
|
|
gt_i64_timei(uint64_t a, uint64_t b)
|
|
|
|
{
|
|
|
|
int64_t diff = (int64_t) (b - a);
|
|
|
|
int res = diff >> 63;
|
|
|
|
return res & 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-08-09 19:47:42 +02:00
|
|
|
/** Pick a random element of <b>n_entries</b>-element array <b>entries</b>,
|
2012-08-27 23:37:56 +02:00
|
|
|
* choosing each element with a probability proportional to its (uint64_t)
|
|
|
|
* value, and return the index of that element. If all elements are 0, choose
|
|
|
|
* an index at random. Return -1 on error.
|
2012-08-09 19:47:42 +02:00
|
|
|
*/
|
2013-06-06 23:58:28 +02:00
|
|
|
STATIC int
|
2012-08-27 23:37:56 +02:00
|
|
|
choose_array_element_by_weight(const u64_dbl_t *entries, int n_entries)
|
2012-08-09 19:47:42 +02:00
|
|
|
{
|
|
|
|
int i, i_chosen=-1, n_chosen=0;
|
|
|
|
uint64_t total_so_far = 0;
|
|
|
|
uint64_t rand_val;
|
|
|
|
uint64_t total = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < n_entries; ++i)
|
2012-08-27 23:37:56 +02:00
|
|
|
total += entries[i].u64;
|
2012-08-09 19:47:42 +02:00
|
|
|
|
|
|
|
if (n_entries < 1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (total == 0)
|
|
|
|
return crypto_rand_int(n_entries);
|
|
|
|
|
2012-08-28 18:42:21 +02:00
|
|
|
tor_assert(total < INT64_MAX);
|
|
|
|
|
2012-08-09 19:47:42 +02:00
|
|
|
rand_val = crypto_rand_uint64(total);
|
|
|
|
|
|
|
|
for (i = 0; i < n_entries; ++i) {
|
2012-08-27 23:37:56 +02:00
|
|
|
total_so_far += entries[i].u64;
|
2012-08-28 18:42:21 +02:00
|
|
|
if (gt_i64_timei(total_so_far, rand_val)) {
|
2012-08-09 19:47:42 +02:00
|
|
|
i_chosen = i;
|
|
|
|
n_chosen++;
|
2012-08-28 18:42:21 +02:00
|
|
|
/* Set rand_val to INT64_MAX rather than stopping the loop. This way,
|
2012-08-09 19:47:42 +02:00
|
|
|
* the time we spend in the loop does not leak which element we chose. */
|
2012-08-28 18:42:21 +02:00
|
|
|
rand_val = INT64_MAX;
|
2012-08-09 19:47:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
tor_assert(total_so_far == total);
|
|
|
|
tor_assert(n_chosen == 1);
|
|
|
|
tor_assert(i_chosen >= 0);
|
|
|
|
tor_assert(i_chosen < n_entries);
|
|
|
|
|
|
|
|
return i_chosen;
|
|
|
|
}
|
|
|
|
|
2010-09-30 06:10:56 +02:00
|
|
|
/** When weighting bridges, enforce these values as lower and upper
|
|
|
|
* bound for believable bandwidth, because there is no way for us
|
|
|
|
* to verify a bridge's bandwidth currently. */
|
|
|
|
#define BRIDGE_MIN_BELIEVABLE_BANDWIDTH 20000 /* 20 kB/sec */
|
|
|
|
#define BRIDGE_MAX_BELIEVABLE_BANDWIDTH 100000 /* 100 kB/sec */
|
|
|
|
|
|
|
|
/** Return the smaller of the router's configured BandwidthRate
|
|
|
|
* and its advertised capacity, making sure to stay within the
|
|
|
|
* interval between bridge-min-believe-bw and
|
|
|
|
* bridge-max-believe-bw. */
|
|
|
|
static uint32_t
|
|
|
|
bridge_get_advertised_bandwidth_bounded(routerinfo_t *router)
|
|
|
|
{
|
|
|
|
uint32_t result = router->bandwidthcapacity;
|
|
|
|
if (result > router->bandwidthrate)
|
|
|
|
result = router->bandwidthrate;
|
|
|
|
if (result > BRIDGE_MAX_BELIEVABLE_BANDWIDTH)
|
|
|
|
result = BRIDGE_MAX_BELIEVABLE_BANDWIDTH;
|
|
|
|
else if (result < BRIDGE_MIN_BELIEVABLE_BANDWIDTH)
|
|
|
|
result = BRIDGE_MIN_BELIEVABLE_BANDWIDTH;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-06-25 16:55:08 +02:00
|
|
|
/** Return bw*1000, unless bw*1000 would overflow, in which case return
|
|
|
|
* INT32_MAX. */
|
|
|
|
static INLINE int32_t
|
|
|
|
kb_to_bytes(uint32_t bw)
|
2007-08-24 10:01:47 +02:00
|
|
|
{
|
2009-06-25 16:55:08 +02:00
|
|
|
return (bw > (INT32_MAX/1000)) ? INT32_MAX : bw*1000;
|
2007-08-24 10:01:47 +02:00
|
|
|
}
|
2005-06-21 01:04:13 +02:00
|
|
|
|
2010-01-28 00:01:51 +01:00
|
|
|
/** Helper function:
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
* choose a random element of smartlist <b>sl</b> of nodes, weighted by
|
2010-01-28 00:01:51 +01:00
|
|
|
* the advertised bandwidth of each element using the consensus
|
|
|
|
* bandwidth weights.
|
|
|
|
*
|
|
|
|
* If <b>rule</b>==WEIGHT_FOR_EXIT. we're picking an exit node: consider all
|
|
|
|
* nodes' bandwidth equally regardless of their Exit status, since there may
|
|
|
|
* be some in the list because they exit to obscure ports. If
|
|
|
|
* <b>rule</b>==NO_WEIGHTING, we're picking a non-exit node: weight
|
|
|
|
* exit-node's bandwidth less depending on the smallness of the fraction of
|
|
|
|
* Exit-to-total bandwidth. If <b>rule</b>==WEIGHT_FOR_GUARD, we're picking a
|
|
|
|
* guard node: consider all guard's bandwidth equally. Otherwise, weight
|
|
|
|
* guards proportionally less.
|
|
|
|
*/
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
static const node_t *
|
2013-01-18 18:24:54 +01:00
|
|
|
smartlist_choose_node_by_bandwidth_weights(const smartlist_t *sl,
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
bandwidth_weight_rule_t rule)
|
2013-01-18 18:24:54 +01:00
|
|
|
{
|
|
|
|
u64_dbl_t *bandwidths=NULL;
|
|
|
|
|
|
|
|
if (compute_weighted_bandwidths(sl, rule, &bandwidths) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
2014-02-15 21:33:34 +01:00
|
|
|
scale_array_elements_to_u64(bandwidths, smartlist_len(sl), NULL);
|
2013-01-18 18:24:54 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
int idx = choose_array_element_by_weight(bandwidths,
|
|
|
|
smartlist_len(sl));
|
|
|
|
tor_free(bandwidths);
|
|
|
|
return idx < 0 ? NULL : smartlist_get(sl, idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Given a list of routers and a weighting rule as in
|
|
|
|
* smartlist_choose_node_by_bandwidth_weights, compute weighted bandwidth
|
|
|
|
* values for each node and store them in a freshly allocated
|
2013-01-18 18:56:47 +01:00
|
|
|
* *<b>bandwidths_out</b> of the same length as <b>sl</b>, and holding results
|
|
|
|
* as doubles. Return 0 on success, -1 on failure. */
|
2013-01-18 18:24:54 +01:00
|
|
|
static int
|
|
|
|
compute_weighted_bandwidths(const smartlist_t *sl,
|
|
|
|
bandwidth_weight_rule_t rule,
|
|
|
|
u64_dbl_t **bandwidths_out)
|
2010-01-28 00:01:51 +01:00
|
|
|
{
|
|
|
|
int64_t weight_scale;
|
|
|
|
double Wg = -1, Wm = -1, We = -1, Wd = -1;
|
|
|
|
double Wgb = -1, Wmb = -1, Web = -1, Wdb = -1;
|
2012-08-09 19:47:42 +02:00
|
|
|
uint64_t weighted_bw = 0;
|
2012-08-27 23:37:56 +02:00
|
|
|
u64_dbl_t *bandwidths;
|
2010-01-28 00:01:51 +01:00
|
|
|
|
|
|
|
/* Can't choose exit and guard at same time */
|
|
|
|
tor_assert(rule == NO_WEIGHTING ||
|
|
|
|
rule == WEIGHT_FOR_EXIT ||
|
|
|
|
rule == WEIGHT_FOR_GUARD ||
|
|
|
|
rule == WEIGHT_FOR_MID ||
|
|
|
|
rule == WEIGHT_FOR_DIR);
|
|
|
|
|
2010-03-04 06:02:01 +01:00
|
|
|
if (smartlist_len(sl) == 0) {
|
2010-03-04 02:35:06 +01:00
|
|
|
log_info(LD_CIRC,
|
2010-03-04 06:02:01 +01:00
|
|
|
"Empty routerlist passed in to consensus weight node "
|
2010-04-13 00:12:49 +02:00
|
|
|
"selection for rule %s",
|
|
|
|
bandwidth_weight_rule_to_string(rule));
|
2013-01-18 18:24:54 +01:00
|
|
|
return -1;
|
2010-02-27 01:17:09 +01:00
|
|
|
}
|
|
|
|
|
2013-01-31 03:41:10 +01:00
|
|
|
weight_scale = networkstatus_get_weight_scale_param(NULL);
|
2010-01-28 00:01:51 +01:00
|
|
|
|
|
|
|
if (rule == WEIGHT_FOR_GUARD) {
|
|
|
|
Wg = networkstatus_get_bw_weight(NULL, "Wgg", -1);
|
|
|
|
Wm = networkstatus_get_bw_weight(NULL, "Wgm", -1); /* Bridges */
|
|
|
|
We = 0;
|
|
|
|
Wd = networkstatus_get_bw_weight(NULL, "Wgd", -1);
|
|
|
|
|
|
|
|
Wgb = networkstatus_get_bw_weight(NULL, "Wgb", -1);
|
|
|
|
Wmb = networkstatus_get_bw_weight(NULL, "Wmb", -1);
|
|
|
|
Web = networkstatus_get_bw_weight(NULL, "Web", -1);
|
|
|
|
Wdb = networkstatus_get_bw_weight(NULL, "Wdb", -1);
|
|
|
|
} else if (rule == WEIGHT_FOR_MID) {
|
|
|
|
Wg = networkstatus_get_bw_weight(NULL, "Wmg", -1);
|
|
|
|
Wm = networkstatus_get_bw_weight(NULL, "Wmm", -1);
|
|
|
|
We = networkstatus_get_bw_weight(NULL, "Wme", -1);
|
|
|
|
Wd = networkstatus_get_bw_weight(NULL, "Wmd", -1);
|
|
|
|
|
|
|
|
Wgb = networkstatus_get_bw_weight(NULL, "Wgb", -1);
|
|
|
|
Wmb = networkstatus_get_bw_weight(NULL, "Wmb", -1);
|
|
|
|
Web = networkstatus_get_bw_weight(NULL, "Web", -1);
|
|
|
|
Wdb = networkstatus_get_bw_weight(NULL, "Wdb", -1);
|
|
|
|
} else if (rule == WEIGHT_FOR_EXIT) {
|
|
|
|
// Guards CAN be exits if they have weird exit policies
|
|
|
|
// They are d then I guess...
|
|
|
|
We = networkstatus_get_bw_weight(NULL, "Wee", -1);
|
|
|
|
Wm = networkstatus_get_bw_weight(NULL, "Wem", -1); /* Odd exit policies */
|
|
|
|
Wd = networkstatus_get_bw_weight(NULL, "Wed", -1);
|
|
|
|
Wg = networkstatus_get_bw_weight(NULL, "Weg", -1); /* Odd exit policies */
|
|
|
|
|
|
|
|
Wgb = networkstatus_get_bw_weight(NULL, "Wgb", -1);
|
|
|
|
Wmb = networkstatus_get_bw_weight(NULL, "Wmb", -1);
|
|
|
|
Web = networkstatus_get_bw_weight(NULL, "Web", -1);
|
|
|
|
Wdb = networkstatus_get_bw_weight(NULL, "Wdb", -1);
|
|
|
|
} else if (rule == WEIGHT_FOR_DIR) {
|
|
|
|
We = networkstatus_get_bw_weight(NULL, "Wbe", -1);
|
|
|
|
Wm = networkstatus_get_bw_weight(NULL, "Wbm", -1);
|
|
|
|
Wd = networkstatus_get_bw_weight(NULL, "Wbd", -1);
|
|
|
|
Wg = networkstatus_get_bw_weight(NULL, "Wbg", -1);
|
|
|
|
|
|
|
|
Wgb = Wmb = Web = Wdb = weight_scale;
|
|
|
|
} else if (rule == NO_WEIGHTING) {
|
|
|
|
Wg = Wm = We = Wd = weight_scale;
|
|
|
|
Wgb = Wmb = Web = Wdb = weight_scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Wg < 0 || Wm < 0 || We < 0 || Wd < 0 || Wgb < 0 || Wmb < 0 || Wdb < 0
|
|
|
|
|| Web < 0) {
|
|
|
|
log_debug(LD_CIRC,
|
|
|
|
"Got negative bandwidth weights. Defaulting to old selection"
|
|
|
|
" algorithm.");
|
2013-01-18 18:24:54 +01:00
|
|
|
return -1; // Use old algorithm.
|
2010-01-28 00:01:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Wg /= weight_scale;
|
|
|
|
Wm /= weight_scale;
|
|
|
|
We /= weight_scale;
|
|
|
|
Wd /= weight_scale;
|
|
|
|
|
|
|
|
Wgb /= weight_scale;
|
|
|
|
Wmb /= weight_scale;
|
|
|
|
Web /= weight_scale;
|
|
|
|
Wdb /= weight_scale;
|
|
|
|
|
2012-08-27 23:37:56 +02:00
|
|
|
bandwidths = tor_malloc_zero(sizeof(u64_dbl_t)*smartlist_len(sl));
|
2010-01-28 00:01:51 +01:00
|
|
|
|
|
|
|
// Cycle through smartlist and total the bandwidth.
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(sl, const node_t *, node) {
|
2014-02-15 21:33:34 +01:00
|
|
|
int is_exit = 0, is_guard = 0, is_dir = 0, this_bw = 0;
|
2010-01-28 00:01:51 +01:00
|
|
|
double weight = 1;
|
2011-02-04 18:26:59 +01:00
|
|
|
is_exit = node->is_exit && ! node->is_bad_exit;
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
is_guard = node->is_possible_guard;
|
|
|
|
is_dir = node_is_dir(node);
|
|
|
|
if (node->rs) {
|
|
|
|
if (!node->rs->has_bandwidth) {
|
2010-01-28 00:01:51 +01:00
|
|
|
tor_free(bandwidths);
|
|
|
|
/* This should never happen, unless all the authorites downgrade
|
|
|
|
* to 0.2.0 or rogue routerstatuses get inserted into our consensus. */
|
|
|
|
log_warn(LD_BUG,
|
|
|
|
"Consensus is not listing bandwidths. Defaulting back to "
|
|
|
|
"old router selection algorithm.");
|
2013-01-18 18:24:54 +01:00
|
|
|
return -1;
|
2010-01-28 00:01:51 +01:00
|
|
|
}
|
2013-04-11 17:43:40 +02:00
|
|
|
this_bw = kb_to_bytes(node->rs->bandwidth_kb);
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
} else if (node->ri) {
|
|
|
|
/* bridge or other descriptor not in our consensus */
|
|
|
|
this_bw = bridge_get_advertised_bandwidth_bounded(node->ri);
|
2010-01-28 00:01:51 +01:00
|
|
|
} else {
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
/* We can't use this one. */
|
|
|
|
continue;
|
2010-01-28 00:01:51 +01:00
|
|
|
}
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
|
2010-01-28 00:01:51 +01:00
|
|
|
if (is_guard && is_exit) {
|
|
|
|
weight = (is_dir ? Wdb*Wd : Wd);
|
|
|
|
} else if (is_guard) {
|
|
|
|
weight = (is_dir ? Wgb*Wg : Wg);
|
|
|
|
} else if (is_exit) {
|
|
|
|
weight = (is_dir ? Web*We : We);
|
|
|
|
} else { // middle
|
|
|
|
weight = (is_dir ? Wmb*Wm : Wm);
|
|
|
|
}
|
2012-08-09 18:21:37 +02:00
|
|
|
/* These should be impossible; but overflows here would be bad, so let's
|
|
|
|
* make sure. */
|
|
|
|
if (this_bw < 0)
|
|
|
|
this_bw = 0;
|
|
|
|
if (weight < 0.0)
|
|
|
|
weight = 0.0;
|
|
|
|
|
2012-08-27 23:37:56 +02:00
|
|
|
bandwidths[node_sl_idx].dbl = weight*this_bw + 0.5;
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
} SMARTLIST_FOREACH_END(node);
|
2010-01-28 00:01:51 +01:00
|
|
|
|
2013-01-18 18:24:54 +01:00
|
|
|
log_debug(LD_CIRC, "Generated weighted bandwidths for rule %s based "
|
|
|
|
"on weights "
|
2012-08-09 18:21:37 +02:00
|
|
|
"Wg=%f Wm=%f We=%f Wd=%f with total bw "U64_FORMAT,
|
2010-04-13 00:12:49 +02:00
|
|
|
bandwidth_weight_rule_to_string(rule),
|
2012-08-09 18:21:37 +02:00
|
|
|
Wg, Wm, We, Wd, U64_PRINTF_ARG(weighted_bw));
|
2010-02-27 01:17:09 +01:00
|
|
|
|
2013-01-18 18:24:54 +01:00
|
|
|
*bandwidths_out = bandwidths;
|
2012-08-27 23:37:56 +02:00
|
|
|
|
2013-01-18 18:24:54 +01:00
|
|
|
return 0;
|
2010-01-28 00:01:51 +01:00
|
|
|
}
|
|
|
|
|
2013-01-18 18:56:47 +01:00
|
|
|
/** For all nodes in <b>sl</b>, return the fraction of those nodes, weighted
|
|
|
|
* by their weighted bandwidths with rule <b>rule</b>, for which we have
|
|
|
|
* descriptors. */
|
|
|
|
double
|
|
|
|
frac_nodes_with_descriptors(const smartlist_t *sl,
|
|
|
|
bandwidth_weight_rule_t rule)
|
|
|
|
{
|
|
|
|
u64_dbl_t *bandwidths = NULL;
|
|
|
|
double total, present;
|
|
|
|
|
|
|
|
if (smartlist_len(sl) == 0)
|
|
|
|
return 0.0;
|
|
|
|
|
|
|
|
if (compute_weighted_bandwidths(sl, rule, &bandwidths) < 0) {
|
|
|
|
int n_with_descs = 0;
|
|
|
|
SMARTLIST_FOREACH(sl, const node_t *, node, {
|
|
|
|
if (node_has_descriptor(node))
|
|
|
|
n_with_descs++;
|
|
|
|
});
|
|
|
|
return ((double)n_with_descs) / (double)smartlist_len(sl);
|
|
|
|
}
|
|
|
|
|
|
|
|
total = present = 0.0;
|
|
|
|
SMARTLIST_FOREACH_BEGIN(sl, const node_t *, node) {
|
|
|
|
const double bw = bandwidths[node_sl_idx].dbl;
|
|
|
|
total += bw;
|
|
|
|
if (node_has_descriptor(node))
|
|
|
|
present += bw;
|
|
|
|
} SMARTLIST_FOREACH_END(node);
|
|
|
|
|
|
|
|
tor_free(bandwidths);
|
|
|
|
|
|
|
|
if (total < 1.0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return present / total;
|
|
|
|
}
|
|
|
|
|
2007-01-27 10:13:19 +01:00
|
|
|
/** Helper function:
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
* choose a random node_t element of smartlist <b>sl</b>, weighted by
|
2007-01-27 10:13:19 +01:00
|
|
|
* the advertised bandwidth of each element.
|
|
|
|
*
|
2008-02-16 00:39:04 +01:00
|
|
|
* If <b>rule</b>==WEIGHT_FOR_EXIT. we're picking an exit node: consider all
|
|
|
|
* nodes' bandwidth equally regardless of their Exit status, since there may
|
|
|
|
* be some in the list because they exit to obscure ports. If
|
|
|
|
* <b>rule</b>==NO_WEIGHTING, we're picking a non-exit node: weight
|
|
|
|
* exit-node's bandwidth less depending on the smallness of the fraction of
|
|
|
|
* Exit-to-total bandwidth. If <b>rule</b>==WEIGHT_FOR_GUARD, 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
|
|
|
*/
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
static const node_t *
|
2013-01-18 18:56:47 +01:00
|
|
|
smartlist_choose_node_by_bandwidth(const smartlist_t *sl,
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
bandwidth_weight_rule_t rule)
|
2004-08-15 10:15:12 +02:00
|
|
|
{
|
2007-07-26 00:56:34 +02:00
|
|
|
unsigned int i;
|
2012-08-27 23:37:56 +02:00
|
|
|
u64_dbl_t *bandwidths;
|
2007-02-09 00:24:03 +01:00
|
|
|
int is_exit;
|
2007-08-24 08:30:34 +02:00
|
|
|
int is_guard;
|
2012-08-09 18:59:04 +02:00
|
|
|
int is_fast;
|
2012-08-27 23:37:56 +02:00
|
|
|
double total_nonexit_bw = 0, total_exit_bw = 0;
|
|
|
|
double total_nonguard_bw = 0, total_guard_bw = 0;
|
2006-10-01 23:59:09 +02:00
|
|
|
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;
|
2012-08-09 18:59:04 +02:00
|
|
|
bitarray_t *fast_bits;
|
2007-07-26 23:26:57 +02:00
|
|
|
bitarray_t *exit_bits;
|
2007-08-24 08:30:34 +02:00
|
|
|
bitarray_t *guard_bits;
|
|
|
|
|
2010-01-28 00:01:51 +01:00
|
|
|
// This function does not support WEIGHT_FOR_DIR
|
|
|
|
// or WEIGHT_FOR_MID
|
|
|
|
if (rule == WEIGHT_FOR_DIR || rule == WEIGHT_FOR_MID) {
|
|
|
|
rule = NO_WEIGHTING;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2010-03-04 06:02:01 +01:00
|
|
|
if (smartlist_len(sl) == 0) {
|
|
|
|
log_info(LD_CIRC,
|
2010-04-13 00:12:49 +02:00
|
|
|
"Empty routerlist passed in to old node selection for rule %s",
|
|
|
|
bandwidth_weight_rule_to_string(rule));
|
2010-02-27 01:17:09 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-01-22 06:59:59 +01:00
|
|
|
/* First count the total bandwidth weight, and make a list
|
2012-08-09 18:59:04 +02:00
|
|
|
* of each value. We use UINT64_MAX to indicate "unknown". */
|
2012-08-27 23:37:56 +02:00
|
|
|
bandwidths = tor_malloc_zero(sizeof(u64_dbl_t)*smartlist_len(sl));
|
2012-08-09 18:59:04 +02:00
|
|
|
fast_bits = bitarray_init_zero(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 */
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(sl, const node_t *, node) {
|
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;
|
2007-02-09 00:24:03 +01:00
|
|
|
uint32_t this_bw = 0;
|
2010-10-01 23:22:04 +02:00
|
|
|
i = node_sl_idx;
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
|
|
|
|
is_exit = node->is_exit;
|
|
|
|
is_guard = node->is_possible_guard;
|
|
|
|
if (node->rs) {
|
|
|
|
if (node->rs->has_bandwidth) {
|
2013-04-11 17:43:40 +02:00
|
|
|
this_bw = kb_to_bytes(node->rs->bandwidth_kb);
|
2007-01-27 10:13:19 +01:00
|
|
|
} else { /* guess */
|
2007-02-08 20:44:55 +01:00
|
|
|
is_known = 0;
|
2007-01-27 10:13:19 +01:00
|
|
|
}
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
} else if (node->ri) {
|
|
|
|
/* Must be a bridge if we're willing to use it */
|
|
|
|
this_bw = bridge_get_advertised_bandwidth_bounded(node->ri);
|
2007-01-27 10:13:19 +01:00
|
|
|
}
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
|
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);
|
2012-08-09 18:59:04 +02:00
|
|
|
if (node->is_fast)
|
|
|
|
bitarray_set(fast_bits, i);
|
|
|
|
|
2007-02-08 20:44:55 +01:00
|
|
|
if (is_known) {
|
2012-08-27 23:37:56 +02:00
|
|
|
bandwidths[i].dbl = this_bw;
|
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;
|
2012-08-27 23:37:56 +02:00
|
|
|
bandwidths[i].dbl = -1.0;
|
2007-02-08 20:44:55 +01:00
|
|
|
}
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
} SMARTLIST_FOREACH_END(node);
|
2007-02-08 20:44:55 +01:00
|
|
|
|
2012-08-27 23:37:56 +02:00
|
|
|
#define EPSILON .1
|
|
|
|
|
2007-02-08 20:44:55 +01:00
|
|
|
/* Now, fill in the unknown values. */
|
|
|
|
if (n_unknown) {
|
|
|
|
int32_t avg_fast, avg_slow;
|
2012-08-27 23:37:56 +02:00
|
|
|
if (total_exit_bw+total_nonexit_bw < EPSILON) {
|
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) {
|
2012-08-27 23:37:56 +02:00
|
|
|
if (bandwidths[i].dbl >= 0.0)
|
2007-02-08 20:44:55 +01:00
|
|
|
continue;
|
2012-08-09 18:59:04 +02:00
|
|
|
is_fast = bitarray_is_set(fast_bits, i);
|
|
|
|
is_exit = bitarray_is_set(exit_bits, i);
|
|
|
|
is_guard = bitarray_is_set(guard_bits, i);
|
2012-08-27 23:37:56 +02:00
|
|
|
bandwidths[i].dbl = is_fast ? avg_fast : avg_slow;
|
2007-02-08 20:44:55 +01:00
|
|
|
if (is_exit)
|
2012-08-27 23:37:56 +02:00
|
|
|
total_exit_bw += bandwidths[i].dbl;
|
2007-02-08 20:44:55 +01:00
|
|
|
else
|
2012-08-27 23:37:56 +02:00
|
|
|
total_nonexit_bw += bandwidths[i].dbl;
|
2007-08-24 08:30:34 +02:00
|
|
|
if (is_guard)
|
2012-08-27 23:37:56 +02:00
|
|
|
total_guard_bw += bandwidths[i].dbl;
|
2007-08-24 08:30:34 +02:00
|
|
|
else
|
2012-08-27 23:37:56 +02:00
|
|
|
total_nonguard_bw += bandwidths[i].dbl;
|
2007-02-08 20:44:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If there's no bandwidth at all, pick at random. */
|
2012-08-27 23:37:56 +02:00
|
|
|
if (total_exit_bw+total_nonexit_bw < EPSILON) {
|
2006-10-01 23:59:09 +02:00
|
|
|
tor_free(bandwidths);
|
2012-08-09 18:59:04 +02:00
|
|
|
tor_free(fast_bits);
|
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
|
|
|
|
*/
|
2012-08-27 23:37:56 +02:00
|
|
|
if (rule == WEIGHT_FOR_EXIT || total_exit_bw<EPSILON)
|
2007-08-24 08:30:34 +02:00
|
|
|
exit_weight = 1.0;
|
|
|
|
else
|
|
|
|
exit_weight = 1.0 - all_bw/(3.0*exit_bw);
|
|
|
|
|
2012-08-27 23:37:56 +02:00
|
|
|
if (rule == WEIGHT_FOR_GUARD || total_guard_bw<EPSILON)
|
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;
|
|
|
|
|
|
|
|
for (i=0; i < (unsigned)smartlist_len(sl); i++) {
|
2012-08-27 23:37:56 +02:00
|
|
|
tor_assert(bandwidths[i].dbl >= 0.0);
|
2012-08-09 18:59:04 +02:00
|
|
|
|
2007-08-24 08:30:34 +02:00
|
|
|
is_exit = bitarray_is_set(exit_bits, i);
|
|
|
|
is_guard = bitarray_is_set(guard_bits, i);
|
|
|
|
if (is_exit && is_guard)
|
2012-08-27 23:37:56 +02:00
|
|
|
bandwidths[i].dbl *= exit_weight * guard_weight;
|
2007-08-24 08:30:34 +02:00
|
|
|
else if (is_guard)
|
2012-08-27 23:37:56 +02:00
|
|
|
bandwidths[i].dbl *= guard_weight;
|
2007-08-24 08:30:34 +02:00
|
|
|
else if (is_exit)
|
2012-08-27 23:37:56 +02:00
|
|
|
bandwidths[i].dbl *= exit_weight;
|
2007-07-26 23:26:57 +02:00
|
|
|
}
|
2006-10-01 23:59:09 +02:00
|
|
|
}
|
2008-06-10 20:28:10 +02:00
|
|
|
|
2012-08-09 19:47:42 +02:00
|
|
|
#if 0
|
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
|
2011-08-31 02:42:51 +02:00
|
|
|
", nonexit bw = "U64_FORMAT", exit weight = %f "
|
2007-08-24 08:30:34 +02:00
|
|
|
"(for exit == %d)"
|
|
|
|
", guard bw = "U64_FORMAT
|
2011-08-31 02:42:51 +02:00
|
|
|
", nonguard bw = "U64_FORMAT", guard weight = %f "
|
2007-08-24 08:30:34 +02:00
|
|
|
"(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));
|
2012-08-09 19:47:42 +02:00
|
|
|
#endif
|
2006-10-01 23:59:09 +02:00
|
|
|
|
2014-02-15 21:33:34 +01:00
|
|
|
scale_array_elements_to_u64(bandwidths, smartlist_len(sl), NULL);
|
2007-08-24 08:30:34 +02:00
|
|
|
|
2012-08-09 19:47:42 +02:00
|
|
|
{
|
|
|
|
int idx = choose_array_element_by_weight(bandwidths,
|
2012-08-27 23:37:56 +02:00
|
|
|
smartlist_len(sl));
|
2012-08-09 19:47:42 +02:00
|
|
|
tor_free(bandwidths);
|
|
|
|
tor_free(fast_bits);
|
|
|
|
tor_free(exit_bits);
|
|
|
|
tor_free(guard_bits);
|
|
|
|
return idx < 0 ? NULL : smartlist_get(sl, idx);
|
2007-07-12 18:34:45 +02:00
|
|
|
}
|
2007-01-27 10:13:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Choose a random element of status list <b>sl</b>, weighted by
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
* the advertised bandwidth of each node */
|
|
|
|
const node_t *
|
2013-01-18 18:56:47 +01:00
|
|
|
node_sl_choose_by_bandwidth(const smartlist_t *sl,
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
bandwidth_weight_rule_t rule)
|
|
|
|
{ /*XXXX MOVE */
|
|
|
|
const node_t *ret;
|
|
|
|
if ((ret = smartlist_choose_node_by_bandwidth_weights(sl, rule))) {
|
2010-01-28 00:01:51 +01:00
|
|
|
return ret;
|
|
|
|
} else {
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
return smartlist_choose_node_by_bandwidth(sl, rule);
|
2010-01-28 00:01:51 +01:00
|
|
|
}
|
2004-08-15 10:15:12 +02:00
|
|
|
}
|
|
|
|
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
/** Return a random running node from the nodelist. Never
|
|
|
|
* pick a node that is in
|
|
|
|
* <b>excludedsmartlist</b>, or which matches <b>excludedset</b>,
|
2009-09-20 03:02:21 +02:00
|
|
|
* even if they are the only nodes available.
|
2008-07-24 11:22:34 +02:00
|
|
|
* If <b>CRN_NEED_UPTIME</b> is set in flags and any router has more than
|
2005-12-15 10:53:00 +01:00
|
|
|
* a minimum uptime, return one of those.
|
2008-07-24 11:22:34 +02:00
|
|
|
* If <b>CRN_NEED_CAPACITY</b> is set in flags, weight your choice by the
|
2005-06-21 01:04:13 +02:00
|
|
|
* advertised capacity of each router.
|
2008-07-25 16:47:19 +02:00
|
|
|
* If <b>CRN_ALLOW_INVALID</b> is not set in flags, consider only Valid
|
|
|
|
* routers.
|
2008-07-24 11:22:34 +02:00
|
|
|
* If <b>CRN_NEED_GUARD</b> is set in flags, consider only Guard routers.
|
|
|
|
* If <b>CRN_WEIGHT_AS_EXIT</b> is set in flags, we weight bandwidths as if
|
|
|
|
* picking an exit node, otherwise we weight bandwidths for picking a relay
|
|
|
|
* node (that is, possibly discounting exit nodes).
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
* If <b>CRN_NEED_DESC</b> is set in flags, we only consider nodes that
|
|
|
|
* have a routerinfo or microdescriptor -- that is, enough info to be
|
|
|
|
* used to build a circuit.
|
2004-04-03 00:30:39 +02:00
|
|
|
*/
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
const node_t *
|
2009-09-18 04:45:54 +02:00
|
|
|
router_choose_random_node(smartlist_t *excludedsmartlist,
|
2008-07-18 20:36:32 +02:00
|
|
|
routerset_t *excludedset,
|
2008-07-24 11:22:34 +02:00
|
|
|
router_crn_flags_t flags)
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
{ /* XXXX MOVE */
|
2008-07-24 11:22:34 +02:00
|
|
|
const int need_uptime = (flags & CRN_NEED_UPTIME) != 0;
|
|
|
|
const int need_capacity = (flags & CRN_NEED_CAPACITY) != 0;
|
|
|
|
const int need_guard = (flags & CRN_NEED_GUARD) != 0;
|
|
|
|
const int allow_invalid = (flags & CRN_ALLOW_INVALID) != 0;
|
|
|
|
const int weight_for_exit = (flags & CRN_WEIGHT_AS_EXIT) != 0;
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
const int need_desc = (flags & CRN_NEED_DESC) != 0;
|
2008-07-24 11:22:34 +02:00
|
|
|
|
2012-01-18 21:53:30 +01:00
|
|
|
smartlist_t *sl=smartlist_new(),
|
|
|
|
*excludednodes=smartlist_new();
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
const node_t *choice = NULL;
|
|
|
|
const routerinfo_t *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 :
|
2010-01-28 00:01:51 +01:00
|
|
|
(need_guard ? WEIGHT_FOR_GUARD : WEIGHT_FOR_MID);
|
2004-04-03 00:23:15 +02:00
|
|
|
|
2008-09-26 20:58:45 +02:00
|
|
|
/* Exclude relays that allow single hop exit circuits, if the user
|
|
|
|
* wants to (such relays might be risky) */
|
|
|
|
if (get_options()->ExcludeSingleHopRelays) {
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
SMARTLIST_FOREACH(nodelist_get_list(), node_t *, node,
|
|
|
|
if (node_allows_single_hop_exits(node)) {
|
|
|
|
smartlist_add(excludednodes, node);
|
2008-09-26 20:58:45 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-07-11 17:21:47 +02:00
|
|
|
if ((r = routerlist_find_my_routerinfo()))
|
|
|
|
routerlist_add_node_and_family(excludednodes, r);
|
2008-02-04 18:25:24 +01:00
|
|
|
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
router_add_running_nodes_to_smartlist(sl, allow_invalid,
|
|
|
|
need_uptime, need_capacity,
|
|
|
|
need_guard, need_desc);
|
2009-09-20 03:02:21 +02:00
|
|
|
smartlist_subtract(sl,excludednodes);
|
|
|
|
if (excludedsmartlist)
|
|
|
|
smartlist_subtract(sl,excludedsmartlist);
|
|
|
|
if (excludedset)
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
routerset_subtract_nodes(sl,excludedset);
|
2007-01-22 06:59:59 +01:00
|
|
|
|
2010-02-14 23:45:45 +01:00
|
|
|
// Always weight by bandwidth
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
choice = node_sl_choose_by_bandwidth(sl, rule);
|
2009-09-20 03:02:21 +02:00
|
|
|
|
|
|
|
smartlist_free(sl);
|
|
|
|
if (!choice && (need_uptime || need_capacity || need_guard)) {
|
|
|
|
/* try once more -- recurse but with fewer restrictions. */
|
|
|
|
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":"");
|
|
|
|
flags &= ~ (CRN_NEED_UPTIME|CRN_NEED_CAPACITY|CRN_NEED_GUARD);
|
|
|
|
choice = router_choose_random_node(
|
|
|
|
excludedsmartlist, excludedset, flags);
|
2004-04-03 00:23:15 +02:00
|
|
|
}
|
|
|
|
smartlist_free(excludednodes);
|
2007-02-13 02:27:55 +01:00
|
|
|
if (!choice) {
|
2009-09-18 04:45:54 +02:00
|
|
|
log_warn(LD_CIRC,
|
|
|
|
"No available nodes when trying to choose node. Failing.");
|
2007-02-13 02:27:55 +01:00
|
|
|
}
|
2004-04-03 00:23:15 +02:00
|
|
|
return choice;
|
|
|
|
}
|
|
|
|
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
/** Helper: given an extended nickname in <b>hexdigest</b> try to decode it.
|
|
|
|
* Return 0 on success, -1 on failure. Store the result into the
|
|
|
|
* DIGEST_LEN-byte buffer at <b>digest_out</b>, the single character at
|
|
|
|
* <b>nickname_qualifier_char_out</b>, and the MAXNICKNAME_LEN+1-byte buffer
|
|
|
|
* at <b>nickname_out</b>.
|
|
|
|
*
|
|
|
|
* The recognized format is:
|
|
|
|
* HexName = Dollar? HexDigest NamePart?
|
|
|
|
* Dollar = '?'
|
|
|
|
* HexDigest = HexChar*20
|
|
|
|
* HexChar = 'a'..'f' | 'A'..'F' | '0'..'9'
|
|
|
|
* NamePart = QualChar Name
|
|
|
|
* QualChar = '=' | '~'
|
|
|
|
* Name = NameChar*(1..MAX_NICKNAME_LEN)
|
|
|
|
* NameChar = Any ASCII alphanumeric character
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
hex_digest_nickname_decode(const char *hexdigest,
|
|
|
|
char *digest_out,
|
|
|
|
char *nickname_qualifier_char_out,
|
|
|
|
char *nickname_out)
|
2004-07-03 01:40:03 +02:00
|
|
|
{
|
2006-10-03 20:58:40 +02:00
|
|
|
size_t len;
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
|
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);
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
if (len < HEX_DIGEST_LEN) {
|
|
|
|
return -1;
|
|
|
|
} else if (len > HEX_DIGEST_LEN && (hexdigest[HEX_DIGEST_LEN] == '=' ||
|
|
|
|
hexdigest[HEX_DIGEST_LEN] == '~') &&
|
|
|
|
len <= HEX_DIGEST_LEN+1+MAX_NICKNAME_LEN) {
|
|
|
|
*nickname_qualifier_char_out = hexdigest[HEX_DIGEST_LEN];
|
|
|
|
strlcpy(nickname_out, hexdigest+HEX_DIGEST_LEN+1 , MAX_NICKNAME_LEN+1);
|
|
|
|
} else if (len == HEX_DIGEST_LEN) {
|
|
|
|
;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (base16_decode(digest_out, DIGEST_LEN, hexdigest, HEX_DIGEST_LEN)<0)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Helper: Return true iff the <b>identity_digest</b> and <b>nickname</b>
|
|
|
|
* combination of a router, 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. */
|
2012-09-13 18:57:46 +02:00
|
|
|
int
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
hex_digest_nickname_matches(const char *hexdigest, const char *identity_digest,
|
|
|
|
const char *nickname, int is_named)
|
|
|
|
{
|
|
|
|
char digest[DIGEST_LEN];
|
|
|
|
char nn_char='\0';
|
|
|
|
char nn_buf[MAX_NICKNAME_LEN+1];
|
|
|
|
|
|
|
|
if (hex_digest_nickname_decode(hexdigest, digest, &nn_char, nn_buf) == -1)
|
2006-10-03 20:58:40 +02:00
|
|
|
return 0;
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
|
|
|
|
if (nn_char == '=' || nn_char == '~') {
|
2011-07-01 17:43:34 +02:00
|
|
|
if (!nickname)
|
|
|
|
return 0;
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
if (strcasecmp(nn_buf, nickname))
|
2006-10-03 20:58:40 +02:00
|
|
|
return 0;
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
if (nn_char == '=' && !is_named)
|
2006-10-03 20:58:40 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-05-11 22:39:45 +02:00
|
|
|
return tor_memeq(digest, identity_digest, DIGEST_LEN);
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
}
|
|
|
|
|
2012-06-05 01:51:00 +02:00
|
|
|
/** Return true iff <b>router</b> is listed as named in the current
|
2010-10-14 17:54:45 +02:00
|
|
|
* consensus. */
|
2011-05-30 21:41:46 +02:00
|
|
|
int
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
router_is_named(const routerinfo_t *router)
|
|
|
|
{
|
|
|
|
const char *digest =
|
|
|
|
networkstatus_get_router_digest_by_nickname(router->nickname);
|
|
|
|
|
|
|
|
return (digest &&
|
2011-05-11 22:39:45 +02:00
|
|
|
tor_memeq(digest, router->cache_info.identity_digest, DIGEST_LEN));
|
2008-07-18 20:36:32 +02:00
|
|
|
}
|
|
|
|
|
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
|
2010-11-08 20:27:36 +01:00
|
|
|
router_digest_is_trusted_dir_type(const char *digest, dirinfo_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;
|
2012-09-10 21:55:27 +02:00
|
|
|
SMARTLIST_FOREACH(trusted_dir_servers, dir_server_t *, ent,
|
2011-05-10 22:23:43 +02:00
|
|
|
if (tor_memeq(digest, ent->digest, DIGEST_LEN)) {
|
2007-10-08 19:44:19 +02:00
|
|
|
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;
|
2012-09-10 21:55:27 +02:00
|
|
|
SMARTLIST_FOREACH(trusted_dir_servers, dir_server_t *, ent,
|
2007-10-10 22:13:52 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-10-14 17:49:51 +02:00
|
|
|
/** As router_get_by_id_digest,but return a pointer that you're allowed to
|
2010-09-29 06:38:32 +02:00
|
|
|
* modify */
|
2005-06-11 20:52:12 +02:00
|
|
|
routerinfo_t *
|
2010-09-29 06:38:32 +02:00
|
|
|
router_get_mutable_by_digest(const char *digest)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
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
|
|
|
|
2010-09-29 06:38:32 +02:00
|
|
|
/** Return the router in our routerlist whose 20-byte key digest
|
|
|
|
* is <b>digest</b>. Return NULL if no such router is known. */
|
|
|
|
const routerinfo_t *
|
2010-10-14 17:49:51 +02:00
|
|
|
router_get_by_id_digest(const char *digest)
|
2010-09-29 06:38:32 +02:00
|
|
|
{
|
|
|
|
return router_get_mutable_by_digest(digest);
|
|
|
|
}
|
|
|
|
|
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 *
|
2010-09-29 06:38:32 +02:00
|
|
|
signed_descriptor_get_body_impl(const signed_descriptor_t *desc,
|
2007-09-26 18:19:44 +02:00
|
|
|
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;
|
2008-12-10 04:01:26 +01:00
|
|
|
} else if (store) {
|
|
|
|
log_err(LD_DIR, "We couldn't read a descriptor that is supposedly "
|
|
|
|
"mmaped in our cache. Is another process running in our data "
|
|
|
|
"directory? Exiting.");
|
|
|
|
exit(1);
|
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) {
|
2011-05-10 22:58:38 +02:00
|
|
|
if (fast_memcmp("router ", r, 7) && fast_memcmp("extra-info ", r, 11)) {
|
2008-01-16 06:27:19 +01:00
|
|
|
char *cp = tor_strndup(r, 64);
|
2008-12-10 04:01:26 +01:00
|
|
|
log_err(LD_DIR, "descriptor at %p begins with unexpected string %s. "
|
|
|
|
"Is another process running in our data directory? Exiting.",
|
2008-01-16 06:27:19 +01:00
|
|
|
desc, escaped(cp));
|
2008-12-10 04:01:26 +01:00
|
|
|
exit(1);
|
2007-09-27 22:46:30 +02:00
|
|
|
}
|
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 *
|
2010-09-29 06:38:32 +02:00
|
|
|
signed_descriptor_get_body(const signed_descriptor_t *desc)
|
2007-09-26 18:19:44 +02:00
|
|
|
{
|
|
|
|
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 *
|
2010-09-29 06:38:32 +02:00
|
|
|
signed_descriptor_get_annotations(const signed_descriptor_t *desc)
|
2007-12-06 08:15:06 +01:00
|
|
|
{
|
|
|
|
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));
|
2012-01-18 21:53:30 +01:00
|
|
|
routerlist->routers = smartlist_new();
|
|
|
|
routerlist->old_routers = smartlist_new();
|
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";
|
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->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)
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_free(router->onion_pkey);
|
2012-12-04 21:58:18 +01:00
|
|
|
tor_free(router->onion_curve25519_pkey);
|
2003-09-25 07:17:11 +02:00
|
|
|
if (router->identity_pkey)
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_free(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);
|
2012-10-25 03:59:55 +02:00
|
|
|
short_policy_free(router->ipv6_exit_policy);
|
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
|
|
|
|
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)
|
|
|
|
{
|
2009-09-28 16:37:01 +02:00
|
|
|
if (!sd)
|
|
|
|
return;
|
|
|
|
|
2006-01-12 19:04:17 +01:00
|
|
|
tor_free(sd->signed_descriptor_body);
|
2007-05-19 20:12:31 +02:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2010-11-02 16:20:09 +01:00
|
|
|
/** Extract a signed_descriptor_t from a general routerinfo, and free the
|
|
|
|
* routerinfo.
|
2005-12-27 06:26:03 +01:00
|
|
|
*/
|
2005-11-05 21:15:27 +01:00
|
|
|
static signed_descriptor_t *
|
|
|
|
signed_descriptor_from_routerinfo(routerinfo_t *ri)
|
|
|
|
{
|
2010-11-02 16:20:09 +01:00
|
|
|
signed_descriptor_t *sd;
|
|
|
|
tor_assert(ri->purpose == ROUTER_PURPOSE_GENERAL);
|
|
|
|
sd = tor_malloc_zero(sizeof(signed_descriptor_t));
|
2005-11-05 21:15:27 +01:00
|
|
|
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
|
2012-10-12 18:22:13 +02:00
|
|
|
extrainfo_free_(void *e)
|
2007-04-16 23:37:21 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2009-09-28 16:37:01 +02:00
|
|
|
if (!rl)
|
|
|
|
return;
|
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);
|
2012-10-12 18:22:13 +02:00
|
|
|
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);
|
2014-03-18 19:04:20 +01:00
|
|
|
if (routerlist->desc_store.mmap) {
|
2014-03-18 20:39:02 +01:00
|
|
|
int res = tor_munmap_file(routerlist->desc_store.mmap);
|
2014-03-18 19:04:20 +01:00
|
|
|
if (res != 0) {
|
|
|
|
log_warn(LD_FS, "Failed to munmap routerlist->desc_store.mmap");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (routerlist->extrainfo_store.mmap) {
|
2014-03-18 20:39:02 +01:00
|
|
|
int res = tor_munmap_file(routerlist->extrainfo_store.mmap);
|
2014-03-18 19:04:20 +01:00
|
|
|
if (res != 0) {
|
|
|
|
log_warn(LD_FS, "Failed to munmap 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
|
|
|
|
2013-02-01 21:43:37 +01:00
|
|
|
tor_log(severity, LD_DIR,
|
2005-11-23 07:00:58 +01:00
|
|
|
"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));
|
|
|
|
}
|
|
|
|
|
2008-12-22 18:53:04 +01:00
|
|
|
/** Debugging helper: If <b>idx</b> is nonnegative, assert that <b>ri</b> is
|
|
|
|
* in <b>sl</b> at position <b>idx</b>. Otherwise, search <b>sl</b> for
|
|
|
|
* <b>ri</b>. Return the index of <b>ri</b> in <b>sl</b>, or -1 if <b>ri</b>
|
|
|
|
* is not in <b>sl</b>. */
|
2005-10-27 02:34:39 +02:00
|
|
|
static INLINE int
|
2012-10-12 18:22:13 +02: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;
|
2010-11-10 20:55:00 +01:00
|
|
|
signed_descriptor_t *sd_old;
|
2007-05-19 02:32:00 +02:00
|
|
|
{
|
2010-09-29 06:38:32 +02:00
|
|
|
const routerinfo_t *ri_generated = router_get_my_routerinfo();
|
2007-05-19 02:32:00 +02:00
|
|
|
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);
|
2010-11-10 20:55:00 +01:00
|
|
|
|
|
|
|
sd_old = sdmap_set(rl->desc_digest_map,
|
|
|
|
ri->cache_info.signed_descriptor_digest,
|
|
|
|
&(ri->cache_info));
|
|
|
|
if (sd_old) {
|
2012-05-16 16:51:02 +02:00
|
|
|
int idx = sd_old->routerlist_index;
|
|
|
|
sd_old->routerlist_index = -1;
|
|
|
|
smartlist_del(rl->old_routers, idx);
|
|
|
|
if (idx < smartlist_len(rl->old_routers)) {
|
|
|
|
signed_descriptor_t *d = smartlist_get(rl->old_routers, idx);
|
|
|
|
d->routerlist_index = idx;
|
|
|
|
}
|
2010-11-10 20:55:00 +01:00
|
|
|
rl->desc_store.bytes_dropped += sd_old->signed_descriptor_len;
|
|
|
|
sdmap_remove(rl->desc_by_eid_map, sd_old->extra_info_digest);
|
|
|
|
signed_descriptor_free(sd_old);
|
|
|
|
}
|
|
|
|
|
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;
|
2012-07-18 02:00:19 +02:00
|
|
|
nodelist_set_routerinfo(ri, NULL);
|
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
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2010-09-29 06:38:32 +02:00
|
|
|
const routerinfo_t *ri_generated = router_get_my_routerinfo();
|
2007-05-19 02:32:00 +02:00
|
|
|
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
|
2009-06-20 07:52:59 +02:00
|
|
|
routerlist_remove(routerlist_t *rl, routerinfo_t *ri, int make_old, time_t now)
|
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);
|
|
|
|
|
2010-09-28 19:29:31 +02:00
|
|
|
nodelist_remove_routerinfo(ri);
|
|
|
|
|
2009-06-20 07:52:59 +02:00
|
|
|
/* make sure the rephist module knows that it's not running */
|
|
|
|
rep_hist_note_router_unreachable(ri->cache_info.identity_digest, now);
|
|
|
|
|
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
|
2008-12-22 15:56:16 +01:00
|
|
|
* adjust <b>rl</b> as appropriate. <b>idx</b> is -1, or the index of
|
2007-05-29 19:31:13 +02:00
|
|
|
* <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));
|
2008-02-15 20:20:53 +01:00
|
|
|
/* XXXX edmanm's bridge relay triggered the following assert while
|
|
|
|
* running 0.2.0.12-alpha. If anybody triggers this again, see if we
|
2008-06-18 22:14:25 +02:00
|
|
|
* can get a backtrace. */
|
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;
|
2010-11-10 20:55:00 +01:00
|
|
|
int same_descriptors;
|
2007-07-06 20:40:39 +02:00
|
|
|
|
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
|
|
|
{
|
2010-09-29 06:38:32 +02:00
|
|
|
const routerinfo_t *ri_generated = router_get_my_routerinfo();
|
2007-05-19 02:32:00 +02:00
|
|
|
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);
|
|
|
|
|
2012-07-18 02:00:19 +02:00
|
|
|
{
|
|
|
|
routerinfo_t *ri_old_tmp=NULL;
|
|
|
|
nodelist_set_routerinfo(ri_new, &ri_old_tmp);
|
|
|
|
tor_assert(ri_old == ri_old_tmp);
|
|
|
|
}
|
2010-09-28 19:29:31 +02:00
|
|
|
|
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: */
|
2012-10-12 18:22:13 +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
|
|
|
}
|
2011-05-10 22:58:38 +02:00
|
|
|
if (tor_memneq(ri_old->cache_info.identity_digest,
|
2005-12-14 21:40:40 +01:00
|
|
|
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
|
|
|
|
2011-05-11 22:27:27 +02:00
|
|
|
same_descriptors = tor_memeq(ri_old->cache_info.signed_descriptor_digest,
|
2010-11-10 20:55:00 +01:00
|
|
|
ri_new->cache_info.signed_descriptor_digest,
|
|
|
|
DIGEST_LEN);
|
|
|
|
|
2007-10-09 22:44:47 +02:00
|
|
|
if (should_cache_old_descriptors() &&
|
2010-11-10 20:55:00 +01:00
|
|
|
ri_old->purpose == ROUTER_PURPOSE_GENERAL &&
|
|
|
|
!same_descriptors) {
|
|
|
|
/* ri_old is going to become a signed_descriptor_t and go into
|
|
|
|
* old_routers */
|
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 {
|
2010-11-10 20:55:00 +01:00
|
|
|
/* We're dropping ri_old. */
|
|
|
|
if (!same_descriptors) {
|
|
|
|
/* digests don't match; The sdmap_set above 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-04-16 23:37:21 +02:00
|
|
|
|
2011-05-11 22:32:30 +02:00
|
|
|
if (tor_memneq(ri_old->cache_info.extra_info_digest,
|
2010-11-10 20:55:00 +01:00
|
|
|
ri_new->cache_info.extra_info_digest, DIGEST_LEN)) {
|
|
|
|
ei_tmp = eimap_remove(rl->extra_info_map,
|
|
|
|
ri_old->cache_info.extra_info_digest);
|
|
|
|
if (ei_tmp) {
|
|
|
|
rl->extrainfo_store.bytes_dropped +=
|
|
|
|
ei_tmp->cache_info.signed_descriptor_len;
|
|
|
|
extrainfo_free(ei_tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!tor_digest_is_zero(ri_old->cache_info.extra_info_digest)) {
|
|
|
|
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
|
|
|
{
|
2009-12-12 08:07:59 +01:00
|
|
|
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;
|
|
|
|
}
|
2012-09-10 21:23:39 +02:00
|
|
|
clear_dir_servers();
|
|
|
|
smartlist_free(trusted_dir_servers);
|
|
|
|
smartlist_free(fallback_dir_servers);
|
|
|
|
trusted_dir_servers = fallback_dir_servers = NULL;
|
2008-03-26 22:16:56 +01:00
|
|
|
if (trusted_dir_certs) {
|
2013-09-19 18:22:49 +02:00
|
|
|
digestmap_free(trusted_dir_certs, cert_list_free_);
|
2008-03-26 22:16:56 +01:00
|
|
|
trusted_dir_certs = 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)
|
2012-01-18 21:53:30 +01:00
|
|
|
warned_nicknames = smartlist_new();
|
2005-10-05 04:20:46 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
2008-12-25 16:37:47 +01:00
|
|
|
* Returns the status for the operation. Might set *<b>msg</b> if it wants
|
|
|
|
* the poster of the router to know something.
|
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
|
|
|
*/
|
2008-12-17 22:50:01 +01:00
|
|
|
was_router_added_t
|
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;
|
2011-06-14 19:01:38 +02:00
|
|
|
const or_options_t *options = get_options();
|
2011-03-13 21:56:41 +01:00
|
|
|
int authdir = authdir_mode_handles_descs(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;
|
2011-07-11 16:02:24 +02:00
|
|
|
networkstatus_t *consensus =
|
|
|
|
networkstatus_get_latest_consensus_by_flavor(FLAV_NS);
|
2007-10-16 01:15:24 +02:00
|
|
|
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
|
|
|
|
2010-09-29 06:38:32 +02:00
|
|
|
old_router = router_get_mutable_by_digest(id_digest);
|
2010-07-30 21:39:08 +02:00
|
|
|
|
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)) {
|
2010-07-30 21:39:08 +02:00
|
|
|
/* If we have this descriptor already and the new descriptor is a bridge
|
|
|
|
* descriptor, replace it. If we had a bridge descriptor before and the
|
|
|
|
* new one is not a bridge descriptor, don't replace it. */
|
2010-11-02 16:20:09 +01:00
|
|
|
|
|
|
|
/* Only members of routerlist->identity_map can be bridges; we don't
|
|
|
|
* put bridges in old_routers. */
|
|
|
|
const int was_bridge = old_router &&
|
|
|
|
old_router->purpose == ROUTER_PURPOSE_BRIDGE;
|
|
|
|
|
2010-11-16 01:38:19 +01:00
|
|
|
if (routerinfo_is_a_configured_bridge(router) &&
|
|
|
|
router->purpose == ROUTER_PURPOSE_BRIDGE &&
|
|
|
|
!was_bridge) {
|
|
|
|
log_info(LD_DIR, "Replacing non-bridge descriptor with bridge "
|
2011-05-16 03:58:46 +02:00
|
|
|
"descriptor for router %s",
|
|
|
|
router_describe(router));
|
2010-11-16 01:38:19 +01:00
|
|
|
} else {
|
2010-07-30 21:39:08 +02:00
|
|
|
log_info(LD_DIR,
|
2011-05-16 03:58:46 +02:00
|
|
|
"Dropping descriptor that we already have for router %s",
|
|
|
|
router_describe(router));
|
2010-07-30 21:39:08 +02:00
|
|
|
*msg = "Router descriptor was not new.";
|
|
|
|
routerinfo_free(router);
|
|
|
|
return ROUTER_WAS_NOT_NEW;
|
|
|
|
}
|
2005-10-27 02:34:39 +02:00
|
|
|
}
|
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,
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
!from_cache && !from_fetch,
|
|
|
|
&authdir_believes_valid)) {
|
2006-01-12 04:43:39 +01:00
|
|
|
tor_assert(*msg);
|
2005-10-28 20:44:51 +02:00
|
|
|
routerinfo_free(router);
|
2008-12-17 22:50:01 +01:00
|
|
|
return ROUTER_AUTHDIR_REJECTS;
|
2005-10-28 20:44:51 +02:00
|
|
|
}
|
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,
|
2011-05-16 03:58:46 +02:00
|
|
|
"Received a no-longer-recognized descriptor for router %s",
|
|
|
|
router_describe(router));
|
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);
|
2008-12-17 22:50:01 +01:00
|
|
|
return ROUTER_NOT_IN_CONSENSUS_OR_NETWORKSTATUS;
|
2005-10-12 15:49:13 +02:00
|
|
|
}
|
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
|
|
|
if (consensus) {
|
2010-09-29 07:35:08 +02:00
|
|
|
routerstatus_t *rs = networkstatus_vote_find_mutable_entry(
|
|
|
|
consensus, id_digest);
|
2011-05-10 22:23:43 +02:00
|
|
|
if (rs && tor_memeq(rs->descriptor_digest,
|
2007-10-16 01:15:24 +02:00
|
|
|
router->cache_info.signed_descriptor_digest,
|
|
|
|
DIGEST_LEN)) {
|
|
|
|
in_consensus = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.";
|
2008-12-17 22:50:01 +01:00
|
|
|
return ROUTER_NOT_IN_CONSENSUS;
|
2007-10-16 01:15:24 +02:00
|
|
|
}
|
2006-03-27 07:07:57 +02:00
|
|
|
|
2011-02-08 14:08:47 +01:00
|
|
|
/* If we're reading a bridge descriptor from our cache, and we don't
|
|
|
|
* recognize it as one of our currently configured bridges, drop the
|
|
|
|
* descriptor. Otherwise we could end up using it as one of our entry
|
|
|
|
* guards even if it isn't in our Bridge config lines. */
|
|
|
|
if (router->purpose == ROUTER_PURPOSE_BRIDGE && from_cache &&
|
2011-03-13 21:56:41 +01:00
|
|
|
!authdir_mode_bridge(options) &&
|
2011-02-08 14:08:47 +01:00
|
|
|
!routerinfo_is_a_configured_bridge(router)) {
|
2011-05-16 03:58:46 +02:00
|
|
|
log_info(LD_DIR, "Dropping bridge descriptor for %s because we have "
|
|
|
|
"no bridge configured at that address.",
|
|
|
|
safe_str_client(router_describe(router)));
|
2011-02-08 14:08:47 +01:00
|
|
|
*msg = "Router descriptor was not a configured bridge.";
|
|
|
|
routerinfo_free(router);
|
2011-03-13 21:56:41 +01:00
|
|
|
return ROUTER_WAS_NOT_WANTED;
|
2011-02-08 14:08:47 +01:00
|
|
|
}
|
|
|
|
|
2006-09-20 01:18:30 +02:00
|
|
|
/* If we have a router with the same identity key, choose the newer one. */
|
|
|
|
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. */
|
2011-05-16 03:58:46 +02:00
|
|
|
log_debug(LD_DIR, "Not-new descriptor for router %s",
|
|
|
|
router_describe(router));
|
2006-09-20 01:18:30 +02: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);
|
2006-09-20 01:18:30 +02:00
|
|
|
routerlist_insert_old(routerlist, router);
|
|
|
|
*msg = "Router descriptor was not new.";
|
2008-12-17 22:50:01 +01:00
|
|
|
return ROUTER_WAS_NOT_NEW;
|
2006-09-20 01:18:30 +02:00
|
|
|
} else {
|
2007-11-07 16:19:53 +01:00
|
|
|
/* Same key, and either new, or listed in the consensus. */
|
2011-05-16 03:58:46 +02:00
|
|
|
log_debug(LD_DIR, "Replacing entry for router %s",
|
|
|
|
router_describe(router));
|
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.)");
|
2008-12-17 22:50:01 +01:00
|
|
|
return ROUTER_ADDED_SUCCESSFULLY;
|
2006-09-20 01:18:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-07 03:13:42 +01:00
|
|
|
if (!in_consensus && from_cache &&
|
|
|
|
router->cache_info.published_on < time(NULL) - OLD_ROUTER_DESC_MAX_AGE) {
|
|
|
|
*msg = "Router descriptor was really old.";
|
2009-03-16 19:47:45 +01:00
|
|
|
routerinfo_free(router);
|
2009-01-07 03:13:42 +01:00
|
|
|
return ROUTER_WAS_NOT_NEW;
|
|
|
|
}
|
|
|
|
|
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);
|
2010-04-21 09:12:14 +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);
|
2010-04-21 09:12:14 +02:00
|
|
|
}
|
2005-08-26 22:59:04 +02:00
|
|
|
directory_set_dirty();
|
2008-12-17 22:50:01 +01:00
|
|
|
return ROUTER_ADDED_SUCCESSFULLY;
|
2004-05-17 22:31:01 +02:00
|
|
|
}
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/** Insert <b>ei</b> into the routerlist, or free it. Other arguments are
|
2008-12-22 17:37:20 +01:00
|
|
|
* as for router_add_to_routerlist(). Return ROUTER_ADDED_SUCCESSFULLY iff
|
|
|
|
* we actually inserted it, ROUTER_BAD_EI otherwise.
|
2007-10-21 02:08:35 +02:00
|
|
|
*/
|
2008-12-22 05:53:11 +01:00
|
|
|
was_router_added_t
|
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;
|
2011-03-25 21:01:16 +01:00
|
|
|
/*XXXX023 Do something with msg */
|
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
|
|
|
|
2008-12-22 05:53:11 +01:00
|
|
|
if (inserted)
|
|
|
|
return ROUTER_ADDED_SUCCESSFULLY;
|
|
|
|
else
|
|
|
|
return ROUTER_BAD_EI;
|
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
|
2012-10-12 18:22:13 +02:00
|
|
|
compare_old_routers_by_identity_(const void **_a, const void **_b)
|
2005-10-27 02:34:39 +02:00
|
|
|
{
|
|
|
|
int i;
|
2005-11-05 21:15:27 +01:00
|
|
|
const signed_descriptor_t *r1 = *_a, *r2 = *_b;
|
2011-05-10 22:58:38 +02:00
|
|
|
if ((i = fast_memcmp(r1->identity_digest, r2->identity_digest, DIGEST_LEN)))
|
2005-10-27 02:34:39 +02:00
|
|
|
return i;
|
2008-02-22 20:09:45 +01:00
|
|
|
return (int)(r1->published_on - r2->published_on);
|
2005-10-27 02:34:39 +02:00
|
|
|
}
|
|
|
|
|
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
|
2012-10-12 18:22:13 +02:00
|
|
|
compare_duration_idx_(const void *_d1, const void *_d2)
|
2005-10-27 02:34:39 +02:00
|
|
|
{
|
|
|
|
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,
|
2008-04-08 19:06:41 +02:00
|
|
|
digestset_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);
|
2011-05-10 22:23:43 +02:00
|
|
|
tor_assert(tor_memeq(ident, r->identity_digest, DIGEST_LEN));
|
2005-10-27 02:34:39 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* Check whether we need to do anything at all. */
|
2007-07-18 22:46:14 +02:00
|
|
|
{
|
2008-04-08 19:06:41 +02:00
|
|
|
int mdpr = directory_caches_dir_info(get_options()) ? 2 : 1;
|
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 ||
|
2012-04-11 18:50:50 +02:00
|
|
|
(retain && digestset_contains(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);
|
2008-02-22 20:09:45 +01:00
|
|
|
lifespans[i-lo].duration = (int)(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.
|
|
|
|
**/
|
2012-10-12 18:22:13 +02:00
|
|
|
qsort(lifespans, n, sizeof(struct duration_idx_t), compare_duration_idx_);
|
2005-10-27 02:34:39 +02:00
|
|
|
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;
|
2008-04-08 19:06:41 +02:00
|
|
|
digestset_t *retain;
|
2008-02-05 22:39:29 +01:00
|
|
|
const networkstatus_t *consensus = networkstatus_get_latest_consensus();
|
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
|
|
|
|
2008-12-26 18:35:36 +01:00
|
|
|
/* We need to guess how many router descriptors we will wind up wanting to
|
|
|
|
retain, so that we can be sure to allocate a large enough Bloom filter
|
|
|
|
to hold the digest set. Overestimating is fine; underestimating is bad.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
/* We'll probably retain everything in the consensus. */
|
|
|
|
int n_max_retain = smartlist_len(consensus->routerstatus_list);
|
|
|
|
retain = digestset_new(n_max_retain);
|
2008-04-08 19:06:41 +02:00
|
|
|
}
|
|
|
|
|
2006-02-05 03:07:28 +01:00
|
|
|
cutoff = now - OLD_ROUTER_DESC_MAX_AGE;
|
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)
|
2008-04-08 19:06:41 +02:00
|
|
|
digestset_add(retain, rs->descriptor_digest));
|
2007-10-12 19:13:09 +02:00
|
|
|
}
|
|
|
|
|
2014-01-30 12:48:49 +01:00
|
|
|
/* If we have a consensus, we should consider pruning current routers that
|
|
|
|
* are too old and that nobody recommends. (If we don't have a consensus,
|
|
|
|
* then we should get one before we decide to kill routers.) */
|
2009-01-04 20:47:12 +01:00
|
|
|
|
2014-01-29 21:17:05 +01:00
|
|
|
if (consensus) {
|
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 &&
|
2012-04-11 18:50:50 +02:00
|
|
|
!digestset_contains(retain,
|
2008-04-08 19:06:41 +02:00
|
|
|
router->cache_info.signed_descriptor_digest)) {
|
2006-10-20 01:04:56 +02:00
|
|
|
/* Too old: remove it. (If we're a cache, just move it into
|
|
|
|
* old_routers.) */
|
|
|
|
log_info(LD_DIR,
|
2011-05-16 03:58:46 +02:00
|
|
|
"Forgetting obsolete (too old) routerinfo for router %s",
|
|
|
|
router_describe(router));
|
2009-06-20 07:52:59 +02:00
|
|
|
routerlist_remove(routerlist, router, 1, now);
|
2007-07-18 16:02:47 +02:00
|
|
|
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 &&
|
2012-04-11 18:50:50 +02:00
|
|
|
!digestset_contains(retain, sd->signed_descriptor_digest)) {
|
2006-01-10 05:57:12 +01:00
|
|
|
/* 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
|
|
|
|
2008-04-08 19:06:41 +02:00
|
|
|
log_info(LD_DIR, "We have %d live routers and %d old router descriptors.",
|
2007-10-18 16:58:45 +02:00
|
|
|
smartlist_len(routerlist->routers),
|
2008-04-08 19:06:41 +02:00
|
|
|
smartlist_len(routerlist->old_routers));
|
2007-10-18 16:58:45 +02:00
|
|
|
|
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) <
|
2008-04-08 19:06:41 +02:00
|
|
|
smartlist_len(routerlist->routers))
|
2006-01-11 04:58:07 +01:00
|
|
|
goto done;
|
2005-10-27 02:34:39 +02:00
|
|
|
|
2008-04-08 19:06:41 +02:00
|
|
|
/* Sort by identity, then fix indices. */
|
2012-10-12 18:22:13 +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;
|
|
|
|
}
|
2011-05-10 22:58:38 +02:00
|
|
|
if (tor_memneq(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:
|
2008-04-08 19:06:41 +02:00
|
|
|
digestset_free(retain);
|
2008-05-29 02:44:57 +02:00
|
|
|
router_rebuild_store(RRS_DONT_REMOVE_OLD, &routerlist->desc_store);
|
|
|
|
router_rebuild_store(RRS_DONT_REMOVE_OLD,&routerlist->extrainfo_store);
|
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. */
|
2010-08-18 19:36:09 +02:00
|
|
|
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);
|
2010-08-18 19:36:09 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(sl, routerinfo_t *, ri) {
|
2007-06-12 11:17:23 +02:00
|
|
|
if (ri->purpose == ROUTER_PURPOSE_BRIDGE)
|
2007-12-24 11:31:39 +01:00
|
|
|
learned_bridge_descriptor(ri, from_cache);
|
2010-08-18 19:36:09 +02:00
|
|
|
if (ri->needs_retest_if_added) {
|
|
|
|
ri->needs_retest_if_added = 0;
|
|
|
|
dirserv_single_reachability_test(approx_time(), ri);
|
|
|
|
}
|
|
|
|
} SMARTLIST_FOREACH_END(ri);
|
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;
|
2008-12-17 22:50:01 +01:00
|
|
|
was_router_added_t 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;
|
|
|
|
|
2012-01-18 21:53:30 +01:00
|
|
|
lst = smartlist_new();
|
2005-10-05 03:27:08 +02:00
|
|
|
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
|
|
|
|
2008-12-17 22:50:01 +01:00
|
|
|
r = router_add_to_routerlist(ri, msg, 0, 0);
|
|
|
|
if (!WRA_WAS_ADDED(r)) {
|
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);
|
2008-12-17 22:50:01 +01:00
|
|
|
if (r == ROUTER_AUTHDIR_REJECTS)
|
2006-02-15 04:01:53 +01:00
|
|
|
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
|
|
|
*
|
2008-12-23 22:17:52 +01:00
|
|
|
* Return the number of routers actually added.
|
|
|
|
*
|
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
|
|
|
*/
|
2008-12-23 22:17:52 +01:00
|
|
|
int
|
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
|
|
|
{
|
2012-01-18 21:53:30 +01:00
|
|
|
smartlist_t *routers = smartlist_new(), *changed = smartlist_new();
|
2005-09-15 07:19:38 +02:00
|
|
|
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
|
|
|
|
2008-12-17 22:50:01 +01:00
|
|
|
SMARTLIST_FOREACH_BEGIN(routers, routerinfo_t *, ri) {
|
|
|
|
was_router_added_t r;
|
2009-05-17 08:01:09 +02:00
|
|
|
char d[DIGEST_LEN];
|
2005-09-15 07:19:38 +02:00
|
|
|
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);
|
2012-04-11 18:50:50 +02:00
|
|
|
if (smartlist_contains_string(requested_fingerprints, fp)) {
|
2005-09-15 07:19:38 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-17 08:01:09 +02:00
|
|
|
memcpy(d, ri->cache_info.signed_descriptor_digest, DIGEST_LEN);
|
2008-12-17 22:50:01 +01:00
|
|
|
r = router_add_to_routerlist(ri, &msg, from_cache, !from_cache);
|
|
|
|
if (WRA_WAS_ADDED(r)) {
|
2008-12-23 22:17:52 +01:00
|
|
|
any_changed++;
|
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);
|
2008-12-23 22:17:52 +01:00
|
|
|
} else if (WRA_WAS_REJECTED(r)) {
|
|
|
|
download_status_t *dl_status;
|
2009-05-17 08:01:09 +02:00
|
|
|
dl_status = router_get_dl_status_by_descriptor_digest(d);
|
2008-12-23 22:17:52 +01:00
|
|
|
if (dl_status) {
|
|
|
|
log_info(LD_GENERAL, "Marking router %s as never downloadable",
|
2009-05-17 08:01:09 +02:00
|
|
|
hex_str(d, DIGEST_LEN));
|
2008-12-23 22:17:52 +01:00
|
|
|
download_status_mark_impossible(dl_status);
|
|
|
|
}
|
2007-07-16 02:11:03 +02:00
|
|
|
}
|
2008-12-17 22:50:01 +01:00
|
|
|
} SMARTLIST_FOREACH_END(ri);
|
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);
|
2008-12-23 22:17:52 +01:00
|
|
|
|
|
|
|
return any_changed;
|
2005-09-15 07:19:38 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2012-01-18 21:53:30 +01:00
|
|
|
smartlist_t *extrainfo_list = smartlist_new();
|
2007-04-30 21:48:39 +02:00
|
|
|
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));
|
|
|
|
|
2012-07-17 15:33:38 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(extrainfo_list, extrainfo_t *, ei) {
|
2008-12-22 05:53:11 +01:00
|
|
|
was_router_added_t added =
|
2007-10-21 02:08:35 +02:00
|
|
|
router_add_extrainfo_to_routerlist(ei, &msg, from_cache, !from_cache);
|
2008-12-22 05:53:11 +01:00
|
|
|
if (WRA_WAS_ADDED(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);
|
2008-02-15 20:20:53 +01:00
|
|
|
/* We silently let people stuff us with extrainfos we didn't ask for,
|
|
|
|
* so long as we would have wanted them anyway. Since we always fetch
|
|
|
|
* all the extrainfos we want, and we never actually act on them
|
|
|
|
* inside Tor, this should be harmless. */
|
2007-05-18 23:19:19 +02:00
|
|
|
}
|
2012-07-17 15:33:38 +02:00
|
|
|
} SMARTLIST_FOREACH_END(ei);
|
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
|
|
|
{
|
2010-09-29 07:35:08 +02:00
|
|
|
const routerstatus_t *rs;
|
2008-02-05 22:39:29 +01:00
|
|
|
networkstatus_t *consensus = networkstatus_get_latest_consensus();
|
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);
|
2011-05-10 22:23:43 +02:00
|
|
|
if (rs && tor_memeq(rs->descriptor_digest,
|
2007-10-16 01:15:24 +02:00
|
|
|
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-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
|
|
|
|
2010-11-12 18:05:27 +01:00
|
|
|
/** Update downloads for router descriptors and/or microdescriptors as
|
|
|
|
* appropriate. */
|
|
|
|
void
|
|
|
|
update_all_descriptor_downloads(time_t now)
|
|
|
|
{
|
2011-11-28 21:44:10 +01:00
|
|
|
if (get_options()->DisableNetwork)
|
|
|
|
return;
|
2010-11-12 18:05:27 +01:00
|
|
|
update_router_descriptor_downloads(now);
|
|
|
|
update_microdesc_downloads(now);
|
|
|
|
launch_dummy_descriptor_download_as_needed(now, get_options());
|
|
|
|
}
|
|
|
|
|
2014-01-29 21:17:05 +01:00
|
|
|
/** Clear all our timeouts for fetching v3 directory stuff, and then
|
2007-10-08 23:21:50 +02:00
|
|
|
* 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();
|
2011-11-28 21:44:10 +01:00
|
|
|
if (get_options()->DisableNetwork)
|
|
|
|
return;
|
2007-10-08 23:21:50 +02:00
|
|
|
update_networkstatus_downloads(now);
|
2010-11-12 18:05:27 +01:00
|
|
|
update_all_descriptor_downloads(now);
|
2005-09-12 08:56:42 +02: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
|
2010-09-29 06:38:32 +02:00
|
|
|
router_exit_policy_rejects_all(const routerinfo_t *router)
|
2005-09-07 18:42:53 +02:00
|
|
|
{
|
2008-12-29 02:47:28 +01:00
|
|
|
return router->policy_is_reject_star;
|
2007-10-08 23:21:50 +02:00
|
|
|
}
|
2005-09-07 18:42:53 +02:00
|
|
|
|
2012-09-10 22:33:19 +02:00
|
|
|
/** Create an directory server at <b>address</b>:<b>port</b>, with OR identity
|
|
|
|
* key <b>digest</b>. If <b>address</b> is NULL, add ourself. If
|
|
|
|
* <b>is_authority</b>, this is a directory authority. Return the new
|
|
|
|
* directory server entry on success or NULL on failure. */
|
|
|
|
static dir_server_t *
|
|
|
|
dir_server_new(int is_authority,
|
|
|
|
const char *nickname,
|
|
|
|
const tor_addr_t *addr,
|
|
|
|
const char *hostname,
|
|
|
|
uint16_t dir_port, uint16_t or_port,
|
|
|
|
const char *digest, const char *v3_auth_digest,
|
2012-09-11 00:13:28 +02:00
|
|
|
dirinfo_type_t type,
|
|
|
|
double weight)
|
2007-10-08 23:21:50 +02:00
|
|
|
{
|
2012-09-10 21:55:27 +02:00
|
|
|
dir_server_t *ent;
|
2007-10-08 23:21:50 +02:00
|
|
|
uint32_t a;
|
2012-09-10 22:33:19 +02:00
|
|
|
char *hostname_ = NULL;
|
2005-09-08 22:36:40 +02:00
|
|
|
|
2012-09-11 00:13:28 +02:00
|
|
|
if (weight < 0)
|
|
|
|
return NULL;
|
|
|
|
|
2012-09-10 22:33:19 +02:00
|
|
|
if (tor_addr_family(addr) == AF_INET)
|
|
|
|
a = tor_addr_to_ipv4h(addr);
|
|
|
|
else
|
|
|
|
return NULL; /*XXXX Support IPv6 */
|
|
|
|
|
|
|
|
if (!hostname)
|
|
|
|
hostname_ = tor_dup_addr(addr);
|
|
|
|
else
|
|
|
|
hostname_ = tor_strdup(hostname);
|
2006-01-08 22:26:33 +01:00
|
|
|
|
2012-09-10 21:55:27 +02:00
|
|
|
ent = tor_malloc_zero(sizeof(dir_server_t));
|
2007-10-08 23:21:50 +02:00
|
|
|
ent->nickname = nickname ? tor_strdup(nickname) : NULL;
|
2012-09-10 22:33:19 +02:00
|
|
|
ent->address = hostname_;
|
2007-10-08 23:21:50 +02:00
|
|
|
ent->addr = a;
|
|
|
|
ent->dir_port = dir_port;
|
|
|
|
ent->or_port = or_port;
|
|
|
|
ent->is_running = 1;
|
2012-09-10 22:33:19 +02:00
|
|
|
ent->is_authority = is_authority;
|
2007-10-08 23:21:50 +02:00
|
|
|
ent->type = type;
|
2012-09-11 00:13:28 +02:00
|
|
|
ent->weight = weight;
|
2007-10-08 23:21:50 +02:00
|
|
|
memcpy(ent->digest, digest, DIGEST_LEN);
|
2010-11-08 20:35:02 +01:00
|
|
|
if (v3_auth_digest && (type & V3_DIRINFO))
|
2007-10-08 23:21:50 +02:00
|
|
|
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
|
|
|
if (nickname)
|
2012-01-11 20:02:59 +01:00
|
|
|
tor_asprintf(&ent->description, "directory server \"%s\" at %s:%d",
|
2007-10-08 23:21:50 +02:00
|
|
|
nickname, hostname, (int)dir_port);
|
|
|
|
else
|
2012-01-11 20:02:59 +01:00
|
|
|
tor_asprintf(&ent->description, "directory server at %s:%d",
|
2007-10-08 23:21:50 +02:00
|
|
|
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
|
|
|
|
2012-09-10 22:33:19 +02:00
|
|
|
return ent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Create an authoritative directory server at
|
|
|
|
* <b>address</b>:<b>port</b>, with identity key <b>digest</b>. If
|
|
|
|
* <b>address</b> is NULL, add ourself. Return the new trusted directory
|
|
|
|
* server entry on success or NULL if we couldn't add it. */
|
|
|
|
dir_server_t *
|
|
|
|
trusted_dir_server_new(const char *nickname, const char *address,
|
|
|
|
uint16_t dir_port, uint16_t or_port,
|
|
|
|
const char *digest, const char *v3_auth_digest,
|
2012-09-11 00:13:28 +02:00
|
|
|
dirinfo_type_t type, double weight)
|
2012-09-10 22:33:19 +02:00
|
|
|
{
|
|
|
|
uint32_t a;
|
|
|
|
tor_addr_t addr;
|
|
|
|
char *hostname=NULL;
|
|
|
|
dir_server_t *result;
|
|
|
|
|
|
|
|
if (!address) { /* The address is us; we should guess. */
|
2013-02-11 23:09:10 +01:00
|
|
|
if (resolve_my_address(LOG_WARN, get_options(),
|
2013-02-12 10:25:42 +01:00
|
|
|
&a, NULL, &hostname) < 0) {
|
2012-09-10 22:33:19 +02:00
|
|
|
log_warn(LD_CONFIG,
|
|
|
|
"Couldn't find a suitable address when adding ourself as a "
|
|
|
|
"trusted directory server.");
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-02-10 22:45:48 +01:00
|
|
|
if (!hostname)
|
|
|
|
hostname = tor_dup_ip(a);
|
2012-09-10 22:33:19 +02:00
|
|
|
} else {
|
|
|
|
if (tor_lookup_hostname(address, &a)) {
|
|
|
|
log_warn(LD_CONFIG,
|
|
|
|
"Unable to lookup address for directory server at '%s'",
|
|
|
|
address);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
hostname = tor_strdup(address);
|
|
|
|
}
|
|
|
|
tor_addr_from_ipv4h(&addr, a);
|
|
|
|
|
|
|
|
result = dir_server_new(1, nickname, &addr, hostname,
|
|
|
|
dir_port, or_port, digest,
|
2012-09-11 00:13:28 +02:00
|
|
|
v3_auth_digest, type, weight);
|
2012-09-10 22:33:19 +02:00
|
|
|
tor_free(hostname);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return a new dir_server_t for a fallback directory server at
|
|
|
|
* <b>addr</b>:<b>or_port</b>/<b>dir_port</b>, with identity key digest
|
|
|
|
* <b>id_digest</b> */
|
|
|
|
dir_server_t *
|
|
|
|
fallback_dir_server_new(const tor_addr_t *addr,
|
|
|
|
uint16_t dir_port, uint16_t or_port,
|
2012-09-11 00:13:28 +02:00
|
|
|
const char *id_digest, double weight)
|
2012-09-10 22:33:19 +02:00
|
|
|
{
|
|
|
|
return dir_server_new(0, NULL, addr, NULL, dir_port, or_port, id_digest,
|
2012-09-11 00:13:28 +02:00
|
|
|
NULL, ALL_DIRINFO, weight);
|
2012-09-10 22:33:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Add a directory server to the global list(s). */
|
|
|
|
void
|
|
|
|
dir_server_add(dir_server_t *ent)
|
|
|
|
{
|
|
|
|
if (!trusted_dir_servers)
|
|
|
|
trusted_dir_servers = smartlist_new();
|
|
|
|
if (!fallback_dir_servers)
|
|
|
|
fallback_dir_servers = smartlist_new();
|
|
|
|
|
|
|
|
if (ent->is_authority)
|
|
|
|
smartlist_add(trusted_dir_servers, ent);
|
|
|
|
|
2012-09-10 21:23:39 +02:00
|
|
|
smartlist_add(fallback_dir_servers, ent);
|
2006-08-15 05:54:09 +02:00
|
|
|
router_dir_info_changed();
|
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);
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_free(cert->signing_key);
|
|
|
|
crypto_pk_free(cert->identity_key);
|
2007-10-22 19:31:26 +02:00
|
|
|
|
|
|
|
tor_free(cert);
|
|
|
|
}
|
|
|
|
|
2007-10-08 23:21:50 +02:00
|
|
|
/** Free storage held in <b>ds</b>. */
|
|
|
|
static void
|
2012-09-10 22:33:19 +02:00
|
|
|
dir_server_free(dir_server_t *ds)
|
2007-10-08 23:21:50 +02:00
|
|
|
{
|
2009-09-28 16:37:01 +02:00
|
|
|
if (!ds)
|
|
|
|
return;
|
|
|
|
|
2007-10-08 23:21:50 +02:00
|
|
|
tor_free(ds->nickname);
|
|
|
|
tor_free(ds->description);
|
|
|
|
tor_free(ds->address);
|
|
|
|
tor_free(ds);
|
2005-09-07 18:42:53 +02:00
|
|
|
}
|
|
|
|
|
2012-09-10 21:23:39 +02:00
|
|
|
/** Remove all members from the list of dir servers. */
|
2005-09-12 08:56:42 +02:00
|
|
|
void
|
2012-09-10 21:23:39 +02:00
|
|
|
clear_dir_servers(void)
|
2004-10-12 17:55:20 +02:00
|
|
|
{
|
2012-09-10 21:23:39 +02:00
|
|
|
if (fallback_dir_servers) {
|
2012-09-10 21:55:27 +02:00
|
|
|
SMARTLIST_FOREACH(fallback_dir_servers, dir_server_t *, ent,
|
2012-09-10 22:33:19 +02:00
|
|
|
dir_server_free(ent));
|
2012-09-10 21:23:39 +02:00
|
|
|
smartlist_clear(fallback_dir_servers);
|
|
|
|
} else {
|
|
|
|
fallback_dir_servers = smartlist_new();
|
|
|
|
}
|
|
|
|
if (trusted_dir_servers) {
|
2004-10-12 17:55:20 +02:00
|
|
|
smartlist_clear(trusted_dir_servers);
|
|
|
|
} else {
|
2012-01-18 21:53:30 +01:00
|
|
|
trusted_dir_servers = smartlist_new();
|
2004-10-12 17:55:20 +02:00
|
|
|
}
|
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
|
|
|
|
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
|
2012-06-05 00:50:13 +02:00
|
|
|
* rest of the resource into base16 fingerprints (or base64 fingerprints if
|
|
|
|
* purpose==DIR_PURPPOSE_FETCH_MICRODESC), decode them, and set the
|
2010-05-11 23:20:33 +02:00
|
|
|
* 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);
|
2012-01-18 21:53:30 +01:00
|
|
|
smartlist_t *tmp = smartlist_new();
|
2007-05-22 17:49:14 +02:00
|
|
|
smartlist_t *conns = get_connection_array();
|
2010-05-11 23:20:33 +02:00
|
|
|
int flags = DSR_HEX;
|
|
|
|
if (purpose == DIR_PURPOSE_FETCH_MICRODESC)
|
|
|
|
flags = DSR_DIGEST256|DSR_BASE64;
|
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
|
|
|
|
2010-05-11 23:20:33 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(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,
|
2010-05-11 23:20:33 +02:00
|
|
|
tmp, NULL, flags);
|
2005-09-22 08:34:29 +02:00
|
|
|
}
|
2010-05-11 23:20:33 +02:00
|
|
|
} SMARTLIST_FOREACH_END(conn);
|
|
|
|
|
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/");
|
|
|
|
}
|
|
|
|
|
2012-06-05 00:50:13 +02:00
|
|
|
/** For every microdescriptor we are currently downloading by descriptor
|
|
|
|
* digest, set result[d] to (void*)1. (Note that microdescriptor digests
|
|
|
|
* are 256-bit, and digestmap_t only holds 160-bit digests, so we're only
|
|
|
|
* getting the first 20 bytes of each digest here.)
|
|
|
|
*
|
|
|
|
* XXXX Let there be a digestmap256_t, and use that instead.
|
|
|
|
*/
|
2010-05-11 23:20:33 +02:00
|
|
|
void
|
|
|
|
list_pending_microdesc_downloads(digestmap_t *result)
|
|
|
|
{
|
|
|
|
list_pending_downloads(result, DIR_PURPOSE_FETCH_MICRODESC, "d/");
|
|
|
|
}
|
|
|
|
|
2013-05-09 13:56:54 +02:00
|
|
|
/** For every certificate we are currently downloading by (identity digest,
|
|
|
|
* signing key digest) pair, set result[fp_pair] to (void *1).
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
list_pending_fpsk_downloads(fp_pair_map_t *result)
|
|
|
|
{
|
|
|
|
const char *pfx = "fp-sk/";
|
|
|
|
smartlist_t *tmp;
|
|
|
|
smartlist_t *conns;
|
|
|
|
const char *resource;
|
|
|
|
|
|
|
|
tor_assert(result);
|
|
|
|
|
|
|
|
tmp = smartlist_new();
|
|
|
|
conns = get_connection_array();
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
|
|
|
|
if (conn->type == CONN_TYPE_DIR &&
|
|
|
|
conn->purpose == DIR_PURPOSE_FETCH_CERTIFICATE &&
|
|
|
|
!conn->marked_for_close) {
|
|
|
|
resource = TO_DIR_CONN(conn)->requested_resource;
|
|
|
|
if (!strcmpstart(resource, pfx))
|
|
|
|
dir_split_resource_into_fingerprint_pairs(resource + strlen(pfx),
|
|
|
|
tmp);
|
|
|
|
}
|
|
|
|
} SMARTLIST_FOREACH_END(conn);
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH_BEGIN(tmp, fp_pair_t *, fp) {
|
|
|
|
fp_pair_map_set(result, fp, (void*)1);
|
|
|
|
tor_free(fp);
|
|
|
|
} SMARTLIST_FOREACH_END(fp);
|
|
|
|
|
|
|
|
smartlist_free(tmp);
|
|
|
|
}
|
|
|
|
|
2010-05-11 23:20:33 +02:00
|
|
|
/** Launch downloads for all the descriptors whose digests or digests256
|
|
|
|
* are listed 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.
|
2005-12-27 06:26:03 +01:00
|
|
|
*/
|
|
|
|
static void
|
2010-09-29 07:35:08 +02:00
|
|
|
initiate_descriptor_downloads(const routerstatus_t *source,
|
2007-05-18 23:19:19 +02:00
|
|
|
int purpose,
|
2005-12-27 06:26:03 +01:00
|
|
|
smartlist_t *digests,
|
2008-12-11 20:12:55 +01:00
|
|
|
int lo, int hi, int pds_flags)
|
2005-12-27 06:26:03 +01:00
|
|
|
{
|
|
|
|
int i, n = hi-lo;
|
|
|
|
char *resource, *cp;
|
|
|
|
size_t r_len;
|
2010-05-11 23:20:33 +02:00
|
|
|
|
|
|
|
int digest_len = DIGEST_LEN, enc_digest_len = HEX_DIGEST_LEN;
|
|
|
|
char sep = '+';
|
|
|
|
int b64_256 = 0;
|
|
|
|
|
|
|
|
if (purpose == DIR_PURPOSE_FETCH_MICRODESC) {
|
|
|
|
/* Microdescriptors are downloaded by "-"-separated base64-encoded
|
|
|
|
* 256-bit digests. */
|
|
|
|
digest_len = DIGEST256_LEN;
|
|
|
|
enc_digest_len = BASE64_DIGEST256_LEN;
|
|
|
|
sep = '-';
|
|
|
|
b64_256 = 1;
|
|
|
|
}
|
|
|
|
|
2005-12-27 06:26:03 +01:00
|
|
|
if (n <= 0)
|
|
|
|
return;
|
|
|
|
if (lo < 0)
|
|
|
|
lo = 0;
|
|
|
|
if (hi > smartlist_len(digests))
|
|
|
|
hi = smartlist_len(digests);
|
|
|
|
|
2010-05-11 23:20:33 +02:00
|
|
|
r_len = 8 + (enc_digest_len+1)*n;
|
2005-12-27 06:26:03 +01:00
|
|
|
cp = resource = tor_malloc(r_len);
|
|
|
|
memcpy(cp, "d/", 2);
|
|
|
|
cp += 2;
|
|
|
|
for (i = lo; i < hi; ++i) {
|
2010-05-11 23:20:33 +02:00
|
|
|
if (b64_256) {
|
|
|
|
digest256_to_base64(cp, smartlist_get(digests, i));
|
|
|
|
} else {
|
|
|
|
base16_encode(cp, r_len-(cp-resource),
|
|
|
|
smartlist_get(digests,i), digest_len);
|
|
|
|
}
|
|
|
|
cp += enc_digest_len;
|
|
|
|
*cp++ = sep;
|
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,
|
2012-09-12 16:15:58 +02:00
|
|
|
DIRIND_ONEHOP,
|
2007-10-28 21:30:21 +01:00
|
|
|
resource, NULL, 0, 0);
|
2005-12-27 06:26:03 +01:00
|
|
|
} else {
|
2008-12-11 20:12:45 +01:00
|
|
|
directory_get_from_dirserver(purpose, ROUTER_PURPOSE_GENERAL, resource,
|
2008-12-11 20:12:55 +01:00
|
|
|
pds_flags);
|
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
|
|
|
|
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
|
2010-05-11 23:20:33 +02:00
|
|
|
#define MAX_MICRODESC_DL_PER_REQUEST 92
|
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,
|
2009-12-18 12:55:05 +01:00
|
|
|
* try to split our requests into at least 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
|
2013-05-16 12:08:48 +02:00
|
|
|
* want more, or until TestingClientMaxIntervalWithoutRequest has passed. */
|
2005-09-30 22:04:55 +02:00
|
|
|
#define MAX_DL_TO_DELAY 16
|
2007-10-08 21:56:57 +02:00
|
|
|
|
2010-05-11 23:20:33 +02:00
|
|
|
/** Given a <b>purpose</b> (FETCH_MICRODESC or FETCH_SERVERDESC) and a list of
|
|
|
|
* router descriptor digests or microdescriptor digest256s 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.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
launch_descriptor_downloads(int purpose,
|
|
|
|
smartlist_t *downloadable,
|
2010-09-29 07:35:08 +02:00
|
|
|
const routerstatus_t *source, time_t now)
|
2007-10-08 21:56:57 +02:00
|
|
|
{
|
|
|
|
int should_delay = 0, n_downloadable;
|
2011-06-14 19:01:38 +02:00
|
|
|
const or_options_t *options = get_options();
|
2010-05-11 23:20:33 +02:00
|
|
|
const char *descname;
|
|
|
|
|
|
|
|
tor_assert(purpose == DIR_PURPOSE_FETCH_SERVERDESC ||
|
|
|
|
purpose == DIR_PURPOSE_FETCH_MICRODESC);
|
|
|
|
|
|
|
|
descname = (purpose == DIR_PURPOSE_FETCH_SERVERDESC) ?
|
|
|
|
"routerdesc" : "microdesc";
|
2007-10-08 21:56:57 +02:00
|
|
|
|
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,
|
2010-05-11 23:20:33 +02:00
|
|
|
"There are enough downloadable %ss to launch requests.",
|
|
|
|
descname);
|
2007-10-08 21:56:57 +02:00
|
|
|
should_delay = 0;
|
|
|
|
} else {
|
2010-11-12 18:05:27 +01:00
|
|
|
should_delay = (last_descriptor_download_attempted +
|
2013-05-16 12:08:48 +02:00
|
|
|
options->TestingClientMaxIntervalWithoutRequest) > now;
|
2007-11-16 08:23:25 +01:00
|
|
|
if (!should_delay && n_downloadable) {
|
2010-11-12 18:05:27 +01:00
|
|
|
if (last_descriptor_download_attempted) {
|
2007-10-08 21:56:57 +02:00
|
|
|
log_info(LD_DIR,
|
2010-05-11 23:20:33 +02:00
|
|
|
"There are not many downloadable %ss, but we've "
|
2007-10-08 21:56:57 +02:00
|
|
|
"been waiting long enough (%d seconds). Downloading.",
|
2010-05-11 23:20:33 +02:00
|
|
|
descname,
|
2010-11-12 18:05:27 +01:00
|
|
|
(int)(now-last_descriptor_download_attempted));
|
2007-10-08 21:56:57 +02:00
|
|
|
} else {
|
|
|
|
log_info(LD_DIR,
|
2010-05-11 23:20:33 +02:00
|
|
|
"There are not many downloadable %ss, but we haven't "
|
|
|
|
"tried downloading descriptors recently. Downloading.",
|
|
|
|
descname);
|
2007-10-08 21:56:57 +02:00
|
|
|
}
|
2006-02-19 20:44:09 +01:00
|
|
|
}
|
|
|
|
}
|
2005-10-05 22:45:18 +02:00
|
|
|
}
|
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 = "";
|
2008-12-11 20:12:55 +01:00
|
|
|
int pds_flags = PDS_RETRY_IF_NO_SERVERS;
|
|
|
|
if (! authdir_mode_any_nonhidserv(options)) {
|
|
|
|
/* If we wind up going to the authorities, we want to only open one
|
|
|
|
* connection to each authority at a time, so that we don't overload
|
|
|
|
* them. We do this by setting PDS_NO_EXISTING_SERVERDESC_FETCH
|
|
|
|
* regardless of whether we're a cache or not; it gets ignored if we're
|
|
|
|
* not calling router_pick_trusteddirserver.
|
|
|
|
*
|
|
|
|
* Setting this flag can make initiate_descriptor_downloads() ignore
|
|
|
|
* requests. We need to make sure that we do in fact call
|
|
|
|
* update_router_descriptor_downloads() later on, once the connections
|
|
|
|
* have succeeded or failed.
|
|
|
|
*/
|
2010-05-11 23:20:33 +02:00
|
|
|
pds_flags |= (purpose == DIR_PURPOSE_FETCH_MICRODESC) ?
|
|
|
|
PDS_NO_EXISTING_MICRODESC_FETCH :
|
|
|
|
PDS_NO_EXISTING_SERVERDESC_FETCH;
|
2008-12-11 20:12:55 +01:00
|
|
|
}
|
|
|
|
|
2010-09-15 04:32:36 +02:00
|
|
|
n_per_request = CEIL_DIV(n_downloadable, MIN_REQUESTS);
|
2010-05-11 23:20:33 +02:00
|
|
|
if (purpose == DIR_PURPOSE_FETCH_MICRODESC) {
|
|
|
|
if (n_per_request > MAX_MICRODESC_DL_PER_REQUEST)
|
|
|
|
n_per_request = MAX_MICRODESC_DL_PER_REQUEST;
|
|
|
|
} else {
|
|
|
|
if (n_per_request > MAX_DL_PER_REQUEST)
|
|
|
|
n_per_request = MAX_DL_PER_REQUEST;
|
|
|
|
}
|
2005-12-27 06:26:03 +01:00
|
|
|
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,
|
2012-09-04 04:10:49 +02:00
|
|
|
"Launching %d request%s for %d %s%s, %d at a time",
|
|
|
|
CEIL_DIV(n_downloadable, n_per_request), req_plural,
|
|
|
|
n_downloadable, descname, 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) {
|
2010-05-11 23:20:33 +02:00
|
|
|
initiate_descriptor_downloads(source, purpose,
|
2008-12-11 20:12:55 +01:00
|
|
|
downloadable, i, i+n_per_request,
|
|
|
|
pds_flags);
|
2005-09-18 04:24:42 +02:00
|
|
|
}
|
2010-11-12 18:05:27 +01:00
|
|
|
last_descriptor_download_attempted = now;
|
2005-09-18 04:24:42 +02:00
|
|
|
}
|
2005-09-15 07:19:38 +02:00
|
|
|
}
|
|
|
|
|
2010-03-16 09:36:14 +01:00
|
|
|
/** For any descriptor that we want that's currently listed in
|
|
|
|
* <b>consensus</b>, download it as appropriate. */
|
2010-04-20 09:03:33 +02:00
|
|
|
void
|
|
|
|
update_consensus_router_descriptor_downloads(time_t now, int is_vote,
|
2010-03-16 09:36:14 +01:00
|
|
|
networkstatus_t *consensus)
|
2007-10-08 21:56:57 +02:00
|
|
|
{
|
2011-06-14 19:01:38 +02:00
|
|
|
const or_options_t *options = get_options();
|
2007-10-08 21:56:57 +02:00
|
|
|
digestmap_t *map = NULL;
|
2012-01-18 21:53:30 +01:00
|
|
|
smartlist_t *no_longer_old = smartlist_new();
|
|
|
|
smartlist_t *downloadable = smartlist_new();
|
2010-04-20 09:20:31 +02:00
|
|
|
routerstatus_t *source = NULL;
|
2007-10-08 21:56:57 +02:00
|
|
|
int authdir = authdir_mode(options);
|
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
|
|
|
|
2010-04-20 09:20:31 +02:00
|
|
|
if (is_vote) {
|
|
|
|
/* where's it from, so we know whom to ask for descriptors */
|
2012-09-10 21:55:27 +02:00
|
|
|
dir_server_t *ds;
|
2010-04-20 09:20:31 +02:00
|
|
|
networkstatus_voter_info_t *voter = smartlist_get(consensus->voters, 0);
|
|
|
|
tor_assert(voter);
|
|
|
|
ds = trusteddirserver_get_by_v3_auth_digest(voter->identity_digest);
|
|
|
|
if (ds)
|
|
|
|
source = &(ds->fake_status);
|
|
|
|
else
|
|
|
|
log_warn(LD_DIR, "couldn't lookup source from vote?");
|
|
|
|
}
|
|
|
|
|
2007-10-08 21:56:57 +02:00
|
|
|
map = digestmap_new();
|
|
|
|
list_pending_descriptor_downloads(map, 0);
|
2010-09-29 06:38:32 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(consensus->routerstatus_list, void *, rsp) {
|
2010-04-20 09:03:33 +02:00
|
|
|
routerstatus_t *rs =
|
|
|
|
is_vote ? &(((vote_routerstatus_t *)rsp)->status) : rsp;
|
2007-11-06 21:51:45 +01:00
|
|
|
signed_descriptor_t *sd;
|
|
|
|
if ((sd = router_get_by_descriptor_digest(rs->descriptor_digest))) {
|
2010-09-29 06:38:32 +02:00
|
|
|
const routerinfo_t *ri;
|
2007-10-16 20:47:32 +02:00
|
|
|
++n_have;
|
2010-10-14 17:49:51 +02:00
|
|
|
if (!(ri = router_get_by_id_digest(rs->identity_digest)) ||
|
2011-05-10 22:58:38 +02:00
|
|
|
tor_memneq(ri->cache_info.signed_descriptor_digest,
|
2007-11-06 21:51:45 +01:00
|
|
|
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,
|
2012-12-13 23:34:05 +01:00
|
|
|
* but the identity digest differs (memneq).
|
2007-11-12 16:46:38 +01:00
|
|
|
*/
|
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,
|
2013-05-16 12:08:48 +02:00
|
|
|
options->TestingDescriptorMaxDownloadTries)) {
|
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
|
|
|
}
|
2010-04-20 09:20:31 +02:00
|
|
|
if (is_vote && source) {
|
2010-04-21 00:02:23 +02:00
|
|
|
char time_bufnew[ISO_TIME_LEN+1];
|
|
|
|
char time_bufold[ISO_TIME_LEN+1];
|
2010-09-29 06:38:32 +02:00
|
|
|
const routerinfo_t *oldrouter;
|
2010-10-14 17:49:51 +02:00
|
|
|
oldrouter = router_get_by_id_digest(rs->identity_digest);
|
2010-04-21 00:02:23 +02:00
|
|
|
format_iso_time(time_bufnew, rs->published_on);
|
|
|
|
if (oldrouter)
|
|
|
|
format_iso_time(time_bufold, oldrouter->cache_info.published_on);
|
|
|
|
log_info(LD_DIR, "Learned about %s (%s vs %s) from %s's vote (%s)",
|
2011-05-16 03:58:46 +02:00
|
|
|
routerstatus_describe(rs),
|
|
|
|
time_bufnew,
|
2010-04-21 00:02:23 +02:00
|
|
|
oldrouter ? time_bufold : "none",
|
|
|
|
source->nickname, oldrouter ? "known" : "unknown");
|
2010-04-20 09:20:31 +02:00
|
|
|
}
|
2007-10-08 21:56:57 +02:00
|
|
|
smartlist_add(downloadable, rs->descriptor_digest);
|
2010-09-29 06:38:32 +02:00
|
|
|
} SMARTLIST_FOREACH_END(rsp);
|
2007-10-08 21:56:57 +02:00
|
|
|
|
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));
|
2012-07-17 15:33:38 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(no_longer_old, signed_descriptor_t *, sd) {
|
2007-11-07 16:19:53 +01:00
|
|
|
const char *msg;
|
2008-12-17 22:50:01 +01:00
|
|
|
was_router_added_t r;
|
2007-11-07 16:19:53 +01:00
|
|
|
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);
|
2008-12-17 22:50:01 +01:00
|
|
|
if (WRA_WAS_OUTDATED(r)) {
|
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
|
|
|
}
|
2012-07-17 15:33:38 +02:00
|
|
|
} SMARTLIST_FOREACH_END(sd);
|
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; "
|
2008-02-21 10:01:32 +01: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
|
|
|
|
2010-05-11 23:20:33 +02:00
|
|
|
launch_descriptor_downloads(DIR_PURPOSE_FETCH_SERVERDESC,
|
|
|
|
downloadable, source, now);
|
2007-10-08 21:56:57 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-04-22 23:10:41 +02:00
|
|
|
/** How often should we launch a server/authority request to be sure of getting
|
|
|
|
* a guess for our IP? */
|
2012-06-15 15:37:40 +02:00
|
|
|
/*XXXX024 this info should come from netinfo cells or something, or we should
|
2008-04-22 23:10:41 +02:00
|
|
|
* do this only when we aren't seeing incoming data. see bug 652. */
|
|
|
|
#define DUMMY_DOWNLOAD_INTERVAL (20*60)
|
|
|
|
|
2010-11-12 18:05:27 +01:00
|
|
|
/** As needed, launch a dummy router descriptor fetch to see if our
|
|
|
|
* address has changed. */
|
|
|
|
static void
|
2011-06-14 19:01:38 +02:00
|
|
|
launch_dummy_descriptor_download_as_needed(time_t now,
|
|
|
|
const or_options_t *options)
|
2010-11-12 18:05:27 +01:00
|
|
|
{
|
|
|
|
static time_t last_dummy_download = 0;
|
2012-06-15 15:37:40 +02:00
|
|
|
/* XXXX024 we could be smarter here; see notes on bug 652. */
|
2010-11-12 18:05:27 +01:00
|
|
|
/* If we're a server that doesn't have a configured address, we rely on
|
|
|
|
* directory fetches to learn when our address changes. So if we haven't
|
|
|
|
* tried to get any routerdescs in a long time, try a dummy fetch now. */
|
|
|
|
if (!options->Address &&
|
|
|
|
server_mode(options) &&
|
|
|
|
last_descriptor_download_attempted + DUMMY_DOWNLOAD_INTERVAL < now &&
|
|
|
|
last_dummy_download + DUMMY_DOWNLOAD_INTERVAL < now) {
|
|
|
|
last_dummy_download = now;
|
|
|
|
directory_get_from_dirserver(DIR_PURPOSE_FETCH_SERVERDESC,
|
|
|
|
ROUTER_PURPOSE_GENERAL, "authority.z",
|
|
|
|
PDS_RETRY_IF_NO_SERVERS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2011-06-14 19:01:38 +02:00
|
|
|
const or_options_t *options = get_options();
|
2014-03-08 14:13:12 +01:00
|
|
|
if (should_delay_dir_fetches(options, NULL))
|
2007-06-12 11:17:23 +02:00
|
|
|
return;
|
2010-11-08 20:21:32 +01:00
|
|
|
if (!we_fetch_router_descriptors(options))
|
|
|
|
return;
|
2010-09-20 20:18:17 +02:00
|
|
|
|
2010-04-20 09:03:33 +02:00
|
|
|
update_consensus_router_descriptor_downloads(now, 0,
|
2010-09-20 20:18:17 +02:00
|
|
|
networkstatus_get_reasonably_live_consensus(now, FLAV_NS));
|
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)
|
|
|
|
{
|
2011-06-14 19:01:38 +02:00
|
|
|
const or_options_t *options = get_options();
|
2007-05-18 23:19:19 +02:00
|
|
|
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;
|
2014-03-08 14:13:12 +01:00
|
|
|
if (should_delay_dir_fetches(options, NULL))
|
2007-06-12 11:17:23 +02:00
|
|
|
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();
|
2012-01-18 21:53:30 +01:00
|
|
|
wanted = smartlist_new();
|
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. */
|
2010-10-14 17:49:51 +02:00
|
|
|
if (old_routers && !router_get_by_id_digest(sd->identity_digest))
|
2007-10-21 06:41:00 +02:00
|
|
|
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,
|
2013-05-16 12:08:48 +02:00
|
|
|
options->TestingDescriptorMaxDownloadTries)) {
|
2007-10-18 16:50:59 +02:00
|
|
|
++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,
|
2008-12-11 20:12:55 +01:00
|
|
|
wanted, i, i + MAX_DL_PER_REQUEST,
|
|
|
|
PDS_RETRY_IF_NO_SERVERS|PDS_NO_EXISTING_SERVERDESC_FETCH);
|
2007-05-18 23:19:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
smartlist_free(wanted);
|
|
|
|
}
|
|
|
|
|
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();
|
2010-11-12 18:05:27 +01:00
|
|
|
last_descriptor_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. */
|
2012-03-30 21:38:16 +02:00
|
|
|
#define ROUTER_MAX_COSMETIC_TIME_DIFFERENCE (2*60*60)
|
2006-03-12 23:48:18 +01:00
|
|
|
|
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
|
2010-09-29 06:38:32 +02:00
|
|
|
router_differences_are_cosmetic(const routerinfo_t *r1, const routerinfo_t *r2)
|
2005-10-12 15:49:13 +02:00
|
|
|
{
|
2005-12-14 21:40:40 +01:00
|
|
|
time_t r1pub, r2pub;
|
2008-02-22 20:09:45 +01:00
|
|
|
long 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) {
|
2010-09-29 06:38:32 +02:00
|
|
|
const routerinfo_t *ri_tmp = r2;
|
2005-10-12 20:25:25 +02:00
|
|
|
r2 = r1;
|
|
|
|
r1 = ri_tmp;
|
|
|
|
}
|
|
|
|
|
2005-10-12 15:49:13 +02:00
|
|
|
/* If any key fields differ, they're different. */
|
2013-02-10 04:07:22 +01:00
|
|
|
if (r1->addr != r2->addr ||
|
2005-10-12 15:49:13 +02:00
|
|
|
strcasecmp(r1->nickname, r2->nickname) ||
|
|
|
|
r1->or_port != r2->or_port ||
|
2012-07-19 20:41:23 +02:00
|
|
|
!tor_addr_eq(&r1->ipv6_addr, &r2->ipv6_addr) ||
|
|
|
|
r1->ipv6_orport != r2->ipv6_orport ||
|
2005-10-12 15:49:13 +02:00
|
|
|
r1->dir_port != r2->dir_port ||
|
2007-12-23 02:18:14 +01:00
|
|
|
r1->purpose != r2->purpose ||
|
2012-09-15 12:52:13 +02:00
|
|
|
!crypto_pk_eq_keys(r1->onion_pkey, r2->onion_pkey) ||
|
|
|
|
!crypto_pk_eq_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-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;
|
|
|
|
|
2009-05-27 23:55:51 +02:00
|
|
|
/* Did the bandwidthrate or bandwidthburst change? */
|
2009-04-11 02:12:52 +02:00
|
|
|
if ((r1->bandwidthrate != r2->bandwidthrate) ||
|
|
|
|
(r1->bandwidthburst != r2->bandwidthburst))
|
|
|
|
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;
|
2008-02-22 20:09:45 +01:00
|
|
|
time_difference = labs(r2->uptime - (r1->uptime + (r2pub - r1pub)));
|
2006-11-02 00:53:15 +01:00
|
|
|
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
|
2010-09-29 06:38:32 +02:00
|
|
|
routerinfo_incompatible_with_extrainfo(const 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)
|
2010-09-29 06:38:32 +02:00
|
|
|
sd = (signed_descriptor_t*)&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
|
|
|
|
2011-05-10 22:23:43 +02:00
|
|
|
digest_matches = tor_memeq(ei->cache_info.signed_descriptor_digest,
|
2007-10-21 06:41:00 +02:00
|
|
|
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. */
|
2011-05-16 02:06:36 +02:00
|
|
|
if (tor_memneq(ri->cache_info.identity_digest,
|
|
|
|
ei->cache_info.identity_digest,
|
|
|
|
DIGEST_LEN)) {
|
2007-05-20 20:03:26 +02:00
|
|
|
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];
|
2011-01-13 20:36:41 +01:00
|
|
|
if (crypto_pk_public_checksig(ri->identity_pkey,
|
|
|
|
signed_digest, sizeof(signed_digest),
|
2007-05-15 00:51:05 +02:00
|
|
|
ei->pending_sig, ei->pending_sig_len) != DIGEST_LEN ||
|
2011-05-10 22:58:38 +02:00
|
|
|
tor_memneq(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
|
2010-09-29 06:38:32 +02:00
|
|
|
routerlist_assert_ok(const routerlist_t *rl)
|
2005-10-18 22:13:09 +02:00
|
|
|
{
|
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;
|
2012-07-17 15:33:38 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(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);
|
2008-12-18 17:11:24 +01:00
|
|
|
/* XXXX
|
2007-06-12 02:15:55 +02:00
|
|
|
*
|
|
|
|
* 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
|
2008-02-15 20:20:53 +01:00
|
|
|
* of the world. Changing the representation in 0.2.0.x would just
|
|
|
|
* destabilize the codebase.
|
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));
|
|
|
|
}
|
2008-06-16 20:35:21 +02:00
|
|
|
*/
|
2012-07-17 15:33:38 +02:00
|
|
|
} SMARTLIST_FOREACH_END(r);
|
|
|
|
SMARTLIST_FOREACH_BEGIN(rl->old_routers, signed_descriptor_t *, sd) {
|
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);
|
2008-12-18 17:11:24 +01:00
|
|
|
/* XXXX 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);
|
|
|
|
}
|
2008-06-16 20:35:21 +02:00
|
|
|
*/
|
2012-07-17 15:33:38 +02:00
|
|
|
} SMARTLIST_FOREACH_END(sd);
|
2007-06-12 00:19:40 +02:00
|
|
|
|
2008-02-06 06:31:21 +01:00
|
|
|
RIMAP_FOREACH(rl->identity_map, d, r) {
|
2011-05-10 22:23:43 +02:00
|
|
|
tor_assert(tor_memeq(r->cache_info.identity_digest, d, DIGEST_LEN));
|
2008-02-06 06:31:21 +01:00
|
|
|
} DIGESTMAP_FOREACH_END;
|
|
|
|
SDMAP_FOREACH(rl->desc_digest_map, d, sd) {
|
2011-05-10 22:23:43 +02:00
|
|
|
tor_assert(tor_memeq(sd->signed_descriptor_digest, d, DIGEST_LEN));
|
2008-02-06 06:31:21 +01:00
|
|
|
} DIGESTMAP_FOREACH_END;
|
|
|
|
SDMAP_FOREACH(rl->desc_by_eid_map, d, sd) {
|
2007-06-02 16:24:23 +02:00
|
|
|
tor_assert(!tor_digest_is_zero(d));
|
|
|
|
tor_assert(sd);
|
2011-05-10 22:23:43 +02:00
|
|
|
tor_assert(tor_memeq(sd->extra_info_digest, d, DIGEST_LEN));
|
2008-02-06 06:31:21 +01:00
|
|
|
} DIGESTMAP_FOREACH_END;
|
|
|
|
EIMAP_FOREACH(rl->extra_info_map, d, ei) {
|
2007-05-19 20:12:31 +02:00
|
|
|
signed_descriptor_t *sd;
|
2011-05-10 22:23:43 +02:00
|
|
|
tor_assert(tor_memeq(ei->cache_info.signed_descriptor_digest,
|
2007-05-19 20:12:31 +02:00
|
|
|
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);
|
2008-12-18 17:11:24 +01:00
|
|
|
// tor_assert(sd); // XXXX see above
|
2007-06-12 02:15:55 +02:00
|
|
|
if (sd) {
|
2011-05-10 22:23:43 +02:00
|
|
|
tor_assert(tor_memeq(ei->cache_info.signed_descriptor_digest,
|
2007-06-12 02:15:55 +02:00
|
|
|
sd->extra_info_digest, DIGEST_LEN));
|
|
|
|
}
|
2008-02-06 06:31:21 +01:00
|
|
|
} DIGESTMAP_FOREACH_END;
|
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 *
|
2010-09-29 06:38:32 +02:00
|
|
|
esc_router_info(const routerinfo_t *router)
|
2006-03-11 03:21:30 +01:00
|
|
|
{
|
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;
|
2009-12-12 08:07:59 +01:00
|
|
|
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);
|
|
|
|
|
2012-01-11 20:02:59 +01:00
|
|
|
tor_asprintf(&info, "Contact %s, Platform %s", esc_contact, esc_platform);
|
2006-03-11 03:21:30 +01:00
|
|
|
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
|
2012-10-12 18:22:13 +02:00
|
|
|
compare_routerinfo_by_id_digest_(const void **a, const void **b)
|
2007-10-12 23:33:37 +02:00
|
|
|
{
|
|
|
|
routerinfo_t *first = *(routerinfo_t **)a, *second = *(routerinfo_t **)b;
|
2011-05-10 22:58:38 +02:00
|
|
|
return fast_memcmp(first->cache_info.identity_digest,
|
2007-10-12 23:33:37 +02:00
|
|
|
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)
|
|
|
|
{
|
2012-10-12 18:22:13 +02:00
|
|
|
smartlist_sort(routers, compare_routerinfo_by_id_digest_);
|
2007-10-12 23:33:37 +02:00
|
|
|
}
|
|
|
|
|
2012-10-31 16:38:07 +01:00
|
|
|
/** Called when we change a node set, or when we reload the geoip IPv4 list:
|
2009-10-19 06:30:52 +02:00
|
|
|
* recompute all country info in all configuration node sets and in the
|
|
|
|
* routerlist. */
|
2009-02-16 16:15:06 +01:00
|
|
|
void
|
|
|
|
refresh_all_country_info(void)
|
|
|
|
{
|
2011-06-14 19:01:38 +02:00
|
|
|
const or_options_t *options = get_options();
|
2009-02-16 16:15:06 +01:00
|
|
|
|
|
|
|
if (options->EntryNodes)
|
|
|
|
routerset_refresh_countries(options->EntryNodes);
|
|
|
|
if (options->ExitNodes)
|
|
|
|
routerset_refresh_countries(options->ExitNodes);
|
|
|
|
if (options->ExcludeNodes)
|
|
|
|
routerset_refresh_countries(options->ExcludeNodes);
|
|
|
|
if (options->ExcludeExitNodes)
|
|
|
|
routerset_refresh_countries(options->ExcludeExitNodes);
|
2012-10-12 18:22:13 +02:00
|
|
|
if (options->ExcludeExitNodesUnion_)
|
|
|
|
routerset_refresh_countries(options->ExcludeExitNodesUnion_);
|
2009-02-16 16:15:06 +01:00
|
|
|
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
nodelist_refresh_countries();
|
2009-02-16 16:15:06 +01:00
|
|
|
}
|
2008-07-18 20:36:32 +02:00
|
|
|
|
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) {
|
2011-03-12 16:17:26 +01:00
|
|
|
smartlist_add(responsible_dirs, r);
|
2007-11-03 21:12:41 +01:00
|
|
|
if (++n_added == REND_NUMBER_OF_CONSECUTIVE_REPLICAS)
|
2011-03-12 16:17:26 +01:00
|
|
|
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
|
|
|
{
|
2010-09-29 06:38:32 +02:00
|
|
|
const routerinfo_t *me = router_get_my_routerinfo();
|
2007-11-03 21:12:41 +01:00
|
|
|
if (!me)
|
2007-10-29 20:10:42 +01:00
|
|
|
return 0;
|
2008-09-25 00:33:47 +02:00
|
|
|
if (!get_options()->HidServDirectoryV2) {
|
2007-10-29 20:10:42 +01:00
|
|
|
log_info(LD_REND, "We are not acting as hidden service directory, "
|
|
|
|
"because we have not been configured as such.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
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
|
|
|
{
|
2010-09-29 06:38:32 +02:00
|
|
|
const routerinfo_t *me;
|
2007-11-03 21:12:41 +01:00
|
|
|
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;
|
2012-01-18 21:53:30 +01:00
|
|
|
responsible = smartlist_new();
|
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
|
|
|
}
|
|
|
|
|