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.
|
2010-02-27 23:13:37 +01:00
|
|
|
* Copyright (c) 2007-2010, 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
|
|
|
|
2005-06-11 07:31:17 +02:00
|
|
|
#include "or.h"
|
2010-07-22 01:21:00 +02:00
|
|
|
#include "circuitbuild.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"
|
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"
|
2005-06-11 07:31:17 +02:00
|
|
|
|
2007-05-19 20:31:04 +02:00
|
|
|
// #define DEBUG_ROUTERLIST
|
|
|
|
|
2002-09-26 14:09:10 +02:00
|
|
|
/****************************************************************************/
|
|
|
|
|
2002-09-24 12:43:57 +02:00
|
|
|
/* static function prototypes */
|
2010-09-29 07:35:08 +02:00
|
|
|
static const routerstatus_t *router_pick_directory_server_impl(
|
2007-10-24 20:42:16 +02:00
|
|
|
authority_type_t auth, int flags);
|
2010-09-29 07:35:08 +02:00
|
|
|
static const routerstatus_t *router_pick_trusteddirserver_impl(
|
2008-12-11 20:12:48 +01:00
|
|
|
authority_type_t auth, int flags, int *n_busy_out);
|
2004-10-12 17:55:20 +02:00
|
|
|
static void mark_all_trusteddirservers_up(void);
|
2010-09-29 06:38:32 +02:00
|
|
|
static int router_nickname_matches(const routerinfo_t *router,
|
|
|
|
const char *nickname);
|
2005-10-05 07:03:52 +02:00
|
|
|
static void trusted_dir_server_free(trusted_dir_server_t *ds);
|
2005-12-27 06:26:03 +01:00
|
|
|
static int signed_desc_digest_is_recognized(signed_descriptor_t *desc);
|
2006-08-15 05:54:09 +02:00
|
|
|
static void update_router_have_minimum_dir_info(void);
|
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);
|
2003-12-09 05:29:52 +01:00
|
|
|
|
2007-06-12 00:19:40 +02:00
|
|
|
DECLARE_TYPED_DIGESTMAP_FNS(sdmap_, digest_sd_map_t, signed_descriptor_t)
|
|
|
|
DECLARE_TYPED_DIGESTMAP_FNS(rimap_, digest_ri_map_t, routerinfo_t)
|
|
|
|
DECLARE_TYPED_DIGESTMAP_FNS(eimap_, digest_ei_map_t, extrainfo_t)
|
2008-02-06 06:31:21 +01:00
|
|
|
#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)
|
2007-06-12 00:19:40 +02:00
|
|
|
|
2003-12-09 05:29:52 +01:00
|
|
|
/****************************************************************************/
|
2003-12-13 02:43:21 +01:00
|
|
|
|
2005-11-01 04:48:51 +01:00
|
|
|
/** Global list of a trusted_dir_server_t object for each trusted directory
|
|
|
|
* server. */
|
|
|
|
static smartlist_t *trusted_dir_servers = NULL;
|
2008-01-27 00:18:30 +01:00
|
|
|
|
2008-02-06 17:58:05 +01:00
|
|
|
/** List of for a given authority, and download status for latest certificate.
|
|
|
|
*/
|
2008-01-27 00:18:30 +01:00
|
|
|
typedef struct cert_list_t {
|
|
|
|
download_status_t dl_status;
|
|
|
|
smartlist_t *certs;
|
|
|
|
} cert_list_t;
|
|
|
|
/** Map from v3 identity key digest to cert_list_t. */
|
|
|
|
static digestmap_t *trusted_dir_certs = NULL;
|
|
|
|
/** True iff any key certificate in at least one member of
|
|
|
|
* <b>trusted_dir_certs</b> has changed since we last flushed the
|
2007-05-29 19:31:13 +02:00
|
|
|
* certificates to disk. */
|
2007-05-22 19:58:25 +02:00
|
|
|
static int trusted_dir_servers_certs_changed = 0;
|
2004-05-04 20:17:45 +02:00
|
|
|
|
2005-09-05 01:12:27 +02:00
|
|
|
/** Global list of all of the routers that we know about. */
|
2004-05-04 20:17:45 +02:00
|
|
|
static routerlist_t *routerlist = NULL;
|
2004-02-29 02:31:33 +01:00
|
|
|
|
2005-10-05 03:53:44 +02:00
|
|
|
/** List of strings for nicknames we've already warned about and that are
|
|
|
|
* still unknown / unavailable. */
|
|
|
|
static smartlist_t *warned_nicknames = NULL;
|
2005-11-01 04:48:51 +01:00
|
|
|
|
2005-10-11 03:57:28 +02:00
|
|
|
/** The last time we tried to download any routerdesc, or 0 for "never". We
|
|
|
|
* use this to rate-limit download attempts when the number of routerdescs to
|
|
|
|
* download is low. */
|
2005-10-05 07:03:52 +02:00
|
|
|
static time_t last_routerdesc_download_attempted = 0;
|
2005-11-01 04:48:51 +01:00
|
|
|
|
2008-12-26 18:35:08 +01:00
|
|
|
/** When we last computed the weights to use for bandwidths on directory
|
|
|
|
* requests, what were the total weighted bandwidth, and our share of that
|
|
|
|
* bandwidth? Used to determine what fraction of directory requests we should
|
|
|
|
* expect to see. */
|
|
|
|
static uint64_t sl_last_total_weighted_bw = 0,
|
|
|
|
sl_last_weighted_bw_of_me = 0;
|
2008-06-10 20:28:10 +02:00
|
|
|
|
2007-05-09 06:15:46 +02:00
|
|
|
/** Return the number of directory authorities whose type matches some bit set
|
|
|
|
* in <b>type</b> */
|
2007-06-14 00:44:30 +02:00
|
|
|
int
|
2007-05-09 06:15:46 +02:00
|
|
|
get_n_authorities(authority_type_t type)
|
2006-09-29 01:57:59 +02:00
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
if (!trusted_dir_servers)
|
|
|
|
return 0;
|
|
|
|
SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ds,
|
2007-05-09 06:15:46 +02:00
|
|
|
if (ds->type & type)
|
2006-09-29 01:57:59 +02:00
|
|
|
++n);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2007-05-09 06:15:46 +02:00
|
|
|
#define get_n_v2_authorities() get_n_authorities(V2_AUTHORITY)
|
|
|
|
|
2008-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));
|
2008-11-07 15:57:14 +01:00
|
|
|
cl->dl_status.schedule = DL_SCHED_CONSENSUS;
|
2008-01-27 00:18:30 +01:00
|
|
|
cl->certs = smartlist_create();
|
|
|
|
digestmap_set(trusted_dir_certs, id_digest, cl);
|
|
|
|
}
|
|
|
|
return cl;
|
|
|
|
}
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/** Reload the cached v3 key certificates from the cached-certs file in
|
|
|
|
* the data directory. Return 0 on success, -1 on failure. */
|
2007-05-22 19:58:25 +02:00
|
|
|
int
|
|
|
|
trusted_dirs_reload_certs(void)
|
|
|
|
{
|
2007-10-17 18:55:44 +02:00
|
|
|
char *filename;
|
2007-05-22 19:58:25 +02:00
|
|
|
char *contents;
|
|
|
|
int r;
|
|
|
|
|
2007-10-17 18:55:44 +02:00
|
|
|
filename = get_datadir_fname("cached-certs");
|
2007-07-29 00:14:42 +02:00
|
|
|
contents = read_file_to_str(filename, RFTS_IGNORE_MISSING, NULL);
|
2007-10-17 18:55:44 +02:00
|
|
|
tor_free(filename);
|
2007-05-22 19:58:25 +02:00
|
|
|
if (!contents)
|
2007-07-29 00:14:42 +02:00
|
|
|
return 0;
|
2008-02-21 00:20:36 +01:00
|
|
|
r = trusted_dirs_load_certs_from_string(contents, 1, 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,
|
|
|
|
{
|
|
|
|
if (!memcmp(c->cache_info.signed_descriptor_digest,
|
|
|
|
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
|
|
|
|
* <b>from_store</b> is true, the certificates are from the cache, and we
|
2009-08-20 22:50:51 +02:00
|
|
|
* don't need to flush them to disk. 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
|
2008-02-21 00:20:36 +01:00
|
|
|
trusted_dirs_load_certs_from_string(const char *contents, int from_store,
|
|
|
|
int flush)
|
2007-05-22 19:58:25 +02:00
|
|
|
{
|
|
|
|
trusted_dir_server_t *ds;
|
|
|
|
const char *s, *eos;
|
2009-08-20 22:50:51 +02:00
|
|
|
int failure_code = 0;
|
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
|
|
|
|
|
|
|
/* a duplicate on a download should be treated as a failure, since it
|
|
|
|
* probably means we wanted a different secret key or we are trying to
|
|
|
|
* replace an expired cert that has not in fact been updated. */
|
|
|
|
if (!from_store) {
|
2010-04-20 00:35:57 +02:00
|
|
|
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");
|
2008-11-07 15:01:44 +01:00
|
|
|
authority_cert_dl_failed(cert->cache_info.identity_digest, 404);
|
|
|
|
}
|
|
|
|
|
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 {
|
2009-02-10 21:28:30 +01:00
|
|
|
int adding = directory_caches_dir_info(get_options());
|
|
|
|
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;
|
|
|
|
|
|
|
|
chunks = smartlist_create();
|
2008-01-27 00:18:30 +01:00
|
|
|
DIGESTMAP_FOREACH(trusted_dir_certs, key, cert_list_t *, cl) {
|
|
|
|
SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert,
|
2007-07-26 22:49:04 +02:00
|
|
|
{
|
|
|
|
sized_chunk_t *c = tor_malloc(sizeof(sized_chunk_t));
|
|
|
|
c->bytes = cert->cache_info.signed_descriptor_body;
|
|
|
|
c->len = cert->cache_info.signed_descriptor_len;
|
|
|
|
smartlist_add(chunks, c);
|
|
|
|
});
|
2008-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");
|
2007-05-22 19:58:25 +02:00
|
|
|
if (write_chunks_to_file(filename, chunks, 0)) {
|
|
|
|
log_warn(LD_FS, "Error writing certificates to disk.");
|
|
|
|
}
|
2007-10-17 18:55:44 +02:00
|
|
|
tor_free(filename);
|
2007-05-22 19:58:25 +02:00
|
|
|
SMARTLIST_FOREACH(chunks, sized_chunk_t *, c, tor_free(c));
|
|
|
|
smartlist_free(chunks);
|
|
|
|
|
|
|
|
trusted_dir_servers_certs_changed = 0;
|
|
|
|
}
|
|
|
|
|
2007-07-30 19:46:14 +02:00
|
|
|
/** Remove all v3 authority certificates that have been superseded for more
|
|
|
|
* than 48 hours. (If the most recent cert was published more than 48 hours
|
|
|
|
* ago, then we aren't going to get any consensuses signed with older
|
|
|
|
* keys.) */
|
2007-07-28 02:11:34 +02:00
|
|
|
static void
|
|
|
|
trusted_dirs_remove_old_certs(void)
|
|
|
|
{
|
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;
|
|
|
|
expired = ftime_definitely_after(now, cert->expires);
|
|
|
|
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()) &&
|
|
|
|
!memcmp(c->signing_key_digest, sk_digest, DIGEST_LEN))
|
|
|
|
return c;
|
|
|
|
if ((c = get_my_v3_legacy_cert()) &&
|
|
|
|
!memcmp(c->signing_key_digest, sk_digest, DIGEST_LEN))
|
|
|
|
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
|
|
|
{
|
|
|
|
if (!memcmp(cert->signing_key_digest, sk_digest, DIGEST_LEN))
|
|
|
|
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,
|
2007-07-26 22:49:04 +02:00
|
|
|
if (!memcmp(cert->signing_key_digest, sk_digest, DIGEST_LEN))
|
|
|
|
return cert; );
|
2007-07-26 22:26:59 +02:00
|
|
|
|
2007-07-26 22:49:04 +02:00
|
|
|
return NULL;
|
2007-07-26 22:26:59 +02:00
|
|
|
}
|
|
|
|
|
2008-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
|
|
|
|
* ID <b>id_digest</b> 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
|
|
|
|
authority_cert_dl_failed(const char *id_digest, int status)
|
|
|
|
{
|
|
|
|
cert_list_t *cl;
|
2008-02-05 20:40:15 +01:00
|
|
|
if (!trusted_dir_certs ||
|
|
|
|
!(cl = digestmap_get(trusted_dir_certs, id_digest)))
|
2008-01-27 00:18:30 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
download_status_failed(&cl->dl_status, status);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
n_failures = download_status_get_n_failures(&cl->dl_status);
|
|
|
|
return n_failures >= N_AUTH_CERT_DL_FAILURES_TO_BUG_USER;
|
|
|
|
}
|
|
|
|
|
2007-10-10 21:33:19 +02:00
|
|
|
/** How many times will we try to fetch a certificate before giving up? */
|
|
|
|
#define MAX_CERT_DL_FAILURES 8
|
|
|
|
|
2007-10-04 18:21:58 +02:00
|
|
|
/** Try to download any v3 authority certificates that we may be missing. If
|
|
|
|
* <b>status</b> is provided, try to get all the ones that were used to sign
|
|
|
|
* <b>status</b>. Additionally, try to have a non-expired certificate for
|
|
|
|
* every V3 authority in trusted_dir_servers. Don't fetch certificates we
|
|
|
|
* already have.
|
|
|
|
**/
|
2007-09-08 21:08:39 +02:00
|
|
|
void
|
2008-02-05 22:39:29 +01:00
|
|
|
authority_certs_fetch_missing(networkstatus_t *status, time_t now)
|
2007-09-08 21:08:39 +02:00
|
|
|
{
|
2007-12-09 18:43:43 +01:00
|
|
|
digestmap_t *pending;
|
2008-11-07 15:57:14 +01:00
|
|
|
authority_cert_t *cert;
|
2007-12-09 18:43:43 +01:00
|
|
|
smartlist_t *missing_digests;
|
2007-10-10 21:33:19 +02:00
|
|
|
char *resource = NULL;
|
2008-01-27 00:18:30 +01:00
|
|
|
cert_list_t *cl;
|
|
|
|
const int cache = directory_caches_dir_info(get_options());
|
2007-10-09 22:44:45 +02:00
|
|
|
|
2007-10-19 18:41:30 +02:00
|
|
|
if (should_delay_dir_fetches(get_options()))
|
|
|
|
return;
|
|
|
|
|
2007-12-09 18:43:43 +01:00
|
|
|
pending = digestmap_new();
|
|
|
|
missing_digests = smartlist_create();
|
|
|
|
|
2007-10-09 22:44:45 +02:00
|
|
|
list_pending_downloads(pending, DIR_PURPOSE_FETCH_CERTIFICATE, "fp/");
|
2007-09-08 21:08:39 +02:00
|
|
|
if (status) {
|
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.*/
|
|
|
|
cl = get_cert_list(voter->identity_digest);
|
|
|
|
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)
|
|
|
|
download_status_reset(&cl->dl_status);
|
2007-10-10 21:33:19 +02:00
|
|
|
continue;
|
|
|
|
}
|
2008-01-27 00:18:30 +01:00
|
|
|
if (download_status_is_ready(&cl->dl_status, now,
|
2008-11-07 15:01:44 +01:00
|
|
|
MAX_CERT_DL_FAILURES) &&
|
|
|
|
!digestmap_get(pending, voter->identity_digest)) {
|
2008-01-27 00:18:30 +01:00
|
|
|
log_notice(LD_DIR, "We're missing a certificate from authority "
|
|
|
|
"with signing key %s: launching request.",
|
2009-09-16 23:01:01 +02:00
|
|
|
hex_str(sig->signing_key_digest, DIGEST_LEN));
|
|
|
|
smartlist_add(missing_digests, sig->identity_digest);
|
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
|
|
|
}
|
2009-09-16 23:01:01 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(trusted_dir_servers, trusted_dir_server_t *, ds) {
|
|
|
|
int found = 0;
|
|
|
|
if (!(ds->type & V3_AUTHORITY))
|
|
|
|
continue;
|
|
|
|
if (smartlist_digest_isin(missing_digests, ds->v3_identity_digest))
|
|
|
|
continue;
|
|
|
|
cl = get_cert_list(ds->v3_identity_digest);
|
|
|
|
SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert, {
|
|
|
|
if (!ftime_definitely_after(now, cert->expires)) {
|
|
|
|
/* It's not expired, and we weren't looking for something to
|
|
|
|
* verify a consensus with. Call it done. */
|
|
|
|
download_status_reset(&cl->dl_status);
|
|
|
|
found = 1;
|
|
|
|
break;
|
2007-11-08 17:19:04 +01:00
|
|
|
}
|
2007-09-08 21:08:39 +02:00
|
|
|
});
|
2009-09-16 23:01:01 +02:00
|
|
|
if (!found &&
|
|
|
|
download_status_is_ready(&cl->dl_status, now,MAX_CERT_DL_FAILURES) &&
|
|
|
|
!digestmap_get(pending, ds->v3_identity_digest)) {
|
|
|
|
log_notice(LD_DIR, "No current certificate known for authority %s; "
|
|
|
|
"launching request.", ds->nickname);
|
|
|
|
smartlist_add(missing_digests, ds->v3_identity_digest);
|
|
|
|
}
|
|
|
|
} SMARTLIST_FOREACH_END(ds);
|
2007-09-08 21:08:39 +02:00
|
|
|
|
2007-10-10 21:33:19 +02:00
|
|
|
if (!smartlist_len(missing_digests)) {
|
|
|
|
goto done;
|
|
|
|
} else {
|
2007-09-08 21:08:39 +02:00
|
|
|
smartlist_t *fps = smartlist_create();
|
2007-10-09 19:40:23 +02:00
|
|
|
smartlist_add(fps, tor_strdup("fp/"));
|
2007-09-08 21:08:39 +02:00
|
|
|
SMARTLIST_FOREACH(missing_digests, const char *, d, {
|
2007-10-09 22:44:45 +02:00
|
|
|
char *fp;
|
|
|
|
if (digestmap_get(pending, d))
|
|
|
|
continue;
|
|
|
|
fp = tor_malloc(HEX_DIGEST_LEN+2);
|
2007-09-08 21:08:39 +02:00
|
|
|
base16_encode(fp, HEX_DIGEST_LEN+1, d, DIGEST_LEN);
|
2007-10-09 19:40:23 +02:00
|
|
|
fp[HEX_DIGEST_LEN] = '+';
|
|
|
|
fp[HEX_DIGEST_LEN+1] = '\0';
|
2007-09-08 21:08:39 +02:00
|
|
|
smartlist_add(fps, fp);
|
|
|
|
});
|
2007-11-04 03:06:09 +01:00
|
|
|
if (smartlist_len(fps) == 1) {
|
|
|
|
/* we didn't add any: they were all pending */
|
|
|
|
SMARTLIST_FOREACH(fps, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(fps);
|
|
|
|
goto done;
|
|
|
|
}
|
2007-10-09 19:40:23 +02:00
|
|
|
resource = smartlist_join_strings(fps, "", 0, NULL);
|
|
|
|
resource[strlen(resource)-1] = '\0';
|
2007-09-08 21:08:39 +02:00
|
|
|
SMARTLIST_FOREACH(fps, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(fps);
|
|
|
|
}
|
|
|
|
directory_get_from_dirserver(DIR_PURPOSE_FETCH_CERTIFICATE, 0,
|
2008-12-11 20:12:45 +01:00
|
|
|
resource, PDS_RETRY_IF_NO_SERVERS);
|
2007-10-10 21:33:19 +02:00
|
|
|
|
|
|
|
done:
|
2007-09-08 21:08:39 +02:00
|
|
|
tor_free(resource);
|
2007-10-10 21:33:19 +02:00
|
|
|
smartlist_free(missing_digests);
|
2007-10-09 22:44:45 +02:00
|
|
|
digestmap_free(pending, NULL);
|
2007-09-08 21:08:39 +02:00
|
|
|
}
|
|
|
|
|
2005-09-13 23:14:55 +02:00
|
|
|
/* Router descriptor storage.
|
|
|
|
*
|
2007-09-27 22:46:24 +02:00
|
|
|
* Routerdescs are stored in a big file, named "cached-descriptors". As new
|
2005-09-13 23:14:55 +02:00
|
|
|
* routerdescs arrive, we append them to a journal file named
|
2007-09-27 22:46:24 +02:00
|
|
|
* "cached-descriptors.new".
|
2005-09-13 23:14:55 +02:00
|
|
|
*
|
2007-09-27 22:46:24 +02:00
|
|
|
* From time to time, we replace "cached-descriptors" with a new file
|
|
|
|
* containing only the live, non-superseded descriptors, and clear
|
|
|
|
* cached-routers.new.
|
2005-09-13 23:14:55 +02:00
|
|
|
*
|
|
|
|
* On startup, we read both files.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** Helper: return 1 iff the router log is so big we want to rebuild the
|
|
|
|
* store. */
|
2005-09-13 08:21:10 +02:00
|
|
|
static int
|
2007-08-31 17:08:37 +02:00
|
|
|
router_should_rebuild_store(desc_store_t *store)
|
|
|
|
{
|
|
|
|
if (store->store_len > (1<<16))
|
|
|
|
return (store->journal_len > store->store_len / 2 ||
|
|
|
|
store->bytes_dropped > store->store_len / 2);
|
|
|
|
else
|
|
|
|
return store->journal_len > (1<<15);
|
|
|
|
}
|
|
|
|
|
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
|
2007-04-30 21:48:39 +02:00
|
|
|
_compare_signed_descriptors_by_age(const void **_a, const void **_b)
|
2006-06-22 09:01:54 +02:00
|
|
|
{
|
|
|
|
const signed_descriptor_t *r1 = *_a, *r2 = *_b;
|
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
|
|
|
|
2005-09-13 08:21:10 +02:00
|
|
|
chunk_list = smartlist_create();
|
|
|
|
|
2007-03-21 16:37:30 +01:00
|
|
|
/* We sort the routers by age to enhance locality on disk. */
|
2007-04-30 21:48:39 +02:00
|
|
|
signed_descriptors = smartlist_create();
|
2007-08-31 17:08:37 +02:00
|
|
|
if (store->type == EXTRAINFO_STORE) {
|
2007-06-12 00:19:40 +02:00
|
|
|
eimap_iter_t *iter;
|
|
|
|
for (iter = eimap_iter_init(routerlist->extra_info_map);
|
|
|
|
!eimap_iter_done(iter);
|
|
|
|
iter = eimap_iter_next(routerlist->extra_info_map, iter)) {
|
2007-04-30 21:48:39 +02:00
|
|
|
const char *key;
|
|
|
|
extrainfo_t *ei;
|
2007-06-12 00:19:40 +02:00
|
|
|
eimap_iter_get(iter, &key, &ei);
|
2007-04-30 21:48:39 +02:00
|
|
|
smartlist_add(signed_descriptors, &ei->cache_info);
|
|
|
|
}
|
|
|
|
} else {
|
2007-08-31 17:08:37 +02:00
|
|
|
SMARTLIST_FOREACH(routerlist->old_routers, signed_descriptor_t *, sd,
|
2007-10-16 01:15:24 +02:00
|
|
|
smartlist_add(signed_descriptors, sd));
|
2007-04-30 21:48:39 +02:00
|
|
|
SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, ri,
|
2007-10-16 01:15:24 +02:00
|
|
|
smartlist_add(signed_descriptors, &ri->cache_info));
|
2007-04-30 21:48:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
smartlist_sort(signed_descriptors, _compare_signed_descriptors_by_age);
|
|
|
|
|
|
|
|
/* Now, add the appropriate members to chunk_list */
|
|
|
|
SMARTLIST_FOREACH(signed_descriptors, signed_descriptor_t *, sd,
|
2005-10-27 02:34:39 +02:00
|
|
|
{
|
|
|
|
sized_chunk_t *c;
|
2007-09-26 18:19:44 +02:00
|
|
|
const char *body = signed_descriptor_get_body_impl(sd, 1);
|
2006-01-12 19:04:17 +01:00
|
|
|
if (!body) {
|
2007-03-04 21:11:46 +01:00
|
|
|
log_warn(LD_BUG, "No descriptor available for router.");
|
2005-10-27 02:34:39 +02:00
|
|
|
goto done;
|
|
|
|
}
|
2007-10-16 01:15:24 +02:00
|
|
|
if (sd->do_not_cache) {
|
|
|
|
++nocache;
|
2007-03-21 16:37:30 +01:00
|
|
|
continue;
|
2007-10-16 01:15:24 +02:00
|
|
|
}
|
2005-10-27 02:34:39 +02:00
|
|
|
c = tor_malloc(sizeof(sized_chunk_t));
|
2006-01-12 19:04:17 +01:00
|
|
|
c->bytes = body;
|
2007-09-26 18:19:44 +02:00
|
|
|
c->len = sd->signed_descriptor_len + sd->annotations_len;
|
2007-10-24 17:45:45 +02:00
|
|
|
total_expected_len += c->len;
|
2005-10-27 02:34:39 +02:00
|
|
|
smartlist_add(chunk_list, c);
|
|
|
|
});
|
2007-04-30 21:48:39 +02:00
|
|
|
|
2006-12-07 16:07:33 +01:00
|
|
|
if (write_chunks_to_file(fname_tmp, chunk_list, 1)<0) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_FS, "Error writing router store to disk.");
|
2005-09-13 08:21:10 +02:00
|
|
|
goto done;
|
|
|
|
}
|
2007-04-30 21:48:39 +02:00
|
|
|
|
2006-06-22 09:01:54 +02:00
|
|
|
/* Our mmap is now invalid. */
|
2007-08-31 17:08:37 +02:00
|
|
|
if (store->mmap) {
|
|
|
|
tor_munmap_file(store->mmap);
|
|
|
|
store->mmap = NULL;
|
2006-06-22 09:01:54 +02:00
|
|
|
}
|
2007-04-30 21:48:39 +02:00
|
|
|
|
2006-12-07 16:07:33 +01:00
|
|
|
if (replace_file(fname_tmp, fname)<0) {
|
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;
|
2007-04-30 21:48:39 +02:00
|
|
|
SMARTLIST_FOREACH(signed_descriptors, signed_descriptor_t *, sd,
|
2006-04-29 20:42:26 +02:00
|
|
|
{
|
2007-04-30 21:48:39 +02:00
|
|
|
if (sd->do_not_cache)
|
2007-03-21 16:37:30 +01:00
|
|
|
continue;
|
2006-04-29 20:42:26 +02:00
|
|
|
sd->saved_location = SAVED_IN_CACHE;
|
2007-08-31 17:08:37 +02:00
|
|
|
if (store->mmap) {
|
2006-06-27 17:52:51 +02:00
|
|
|
tor_free(sd->signed_descriptor_body); // sets it to null
|
2006-06-28 17:39:02 +02:00
|
|
|
sd->saved_offset = offset;
|
2006-06-27 17:52:51 +02:00
|
|
|
}
|
2007-09-27 22:46:30 +02:00
|
|
|
offset += sd->signed_descriptor_len + sd->annotations_len;
|
2007-04-30 21:48:39 +02:00
|
|
|
signed_descriptor_get_body(sd); /* reconstruct and assert */
|
2006-04-29 20:42:26 +02:00
|
|
|
});
|
2005-09-13 08:21:10 +02:00
|
|
|
|
2007-10-17 18:55:44 +02:00
|
|
|
tor_free(fname);
|
|
|
|
fname = get_datadir_fname_suffix(store->fname_base, ".new");
|
2006-07-10 00:28:12 +02:00
|
|
|
write_str_to_file(fname, "", 1);
|
2005-09-13 08:21:10 +02:00
|
|
|
|
|
|
|
r = 0;
|
2007-08-31 17:08:37 +02:00
|
|
|
store->store_len = (size_t) offset;
|
|
|
|
store->journal_len = 0;
|
|
|
|
store->bytes_dropped = 0;
|
2005-09-13 08:21:10 +02:00
|
|
|
done:
|
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
|
|
|
{
|
2007-10-17 18:55:44 +02:00
|
|
|
char *fname = NULL, *altname = NULL, *contents = NULL;
|
2007-05-22 04:20:52 +02:00
|
|
|
struct stat st;
|
2007-09-27 22:46:24 +02:00
|
|
|
int read_from_old_location = 0;
|
2007-08-31 17:08:37 +02:00
|
|
|
int extrainfo = (store->type == EXTRAINFO_STORE);
|
2007-10-14 02:13:06 +02:00
|
|
|
time_t now = time(NULL);
|
2007-08-31 17:08:37 +02:00
|
|
|
store->journal_len = store->store_len = 0;
|
2006-08-27 04:07:54 +02:00
|
|
|
|
2007-10-17 18:55:44 +02:00
|
|
|
fname = get_datadir_fname(store->fname_base);
|
|
|
|
if (store->fname_alt_base)
|
|
|
|
altname = get_datadir_fname(store->fname_alt_base);
|
2006-08-27 04:07:54 +02:00
|
|
|
|
2007-08-31 17:08:37 +02:00
|
|
|
if (store->mmap) /* get rid of it first */
|
|
|
|
tor_munmap_file(store->mmap);
|
|
|
|
store->mmap = NULL;
|
2007-04-30 21:48:39 +02:00
|
|
|
|
2007-08-31 17:08:37 +02:00
|
|
|
store->mmap = tor_mmap_file(fname);
|
2007-09-27 22:46:24 +02:00
|
|
|
if (!store->mmap && altname && file_status(altname) == FN_FILE) {
|
|
|
|
read_from_old_location = 1;
|
|
|
|
log_notice(LD_DIR, "Couldn't read %s; trying to load routers from old "
|
|
|
|
"location %s.", fname, altname);
|
|
|
|
if ((store->mmap = tor_mmap_file(altname)))
|
|
|
|
read_from_old_location = 1;
|
|
|
|
}
|
2007-10-14 02:13:06 +02:00
|
|
|
if (altname && !read_from_old_location) {
|
|
|
|
remove_file_if_very_old(altname, now);
|
|
|
|
}
|
2007-08-31 17:08:37 +02:00
|
|
|
if (store->mmap) {
|
|
|
|
store->store_len = store->mmap->size;
|
2007-04-30 21:48:39 +02:00
|
|
|
if (extrainfo)
|
2007-08-31 17:08:37 +02:00
|
|
|
router_load_extrainfo_from_string(store->mmap->data,
|
|
|
|
store->mmap->data+store->mmap->size,
|
2007-09-21 08:14:36 +02:00
|
|
|
SAVED_IN_CACHE, NULL, 0);
|
2007-04-30 21:48:39 +02:00
|
|
|
else
|
2007-08-31 17:08:37 +02:00
|
|
|
router_load_routers_from_string(store->mmap->data,
|
|
|
|
store->mmap->data+store->mmap->size,
|
2007-09-27 22:46:30 +02:00
|
|
|
SAVED_IN_CACHE, NULL, 0, NULL);
|
2005-09-15 07:19:38 +02:00
|
|
|
}
|
2006-06-22 09:01:54 +02:00
|
|
|
|
2007-10-17 18:55:44 +02:00
|
|
|
tor_free(fname);
|
|
|
|
fname = get_datadir_fname_suffix(store->fname_base, ".new");
|
2007-02-12 22:39:44 +01:00
|
|
|
if (file_status(fname) == FN_FILE)
|
2007-05-22 04:20:52 +02:00
|
|
|
contents = read_file_to_str(fname, RFTS_BIN|RFTS_IGNORE_MISSING, &st);
|
2007-10-14 02:13:06 +02:00
|
|
|
if (read_from_old_location) {
|
2007-10-17 18:55:44 +02:00
|
|
|
tor_free(altname);
|
|
|
|
altname = get_datadir_fname_suffix(store->fname_alt_base, ".new");
|
2007-10-14 02:13:06 +02:00
|
|
|
if (!contents)
|
|
|
|
contents = read_file_to_str(altname, RFTS_BIN|RFTS_IGNORE_MISSING, &st);
|
|
|
|
else
|
|
|
|
remove_file_if_very_old(altname, now);
|
2007-09-27 22:46:24 +02:00
|
|
|
}
|
2006-06-22 09:01:54 +02:00
|
|
|
if (contents) {
|
2007-04-30 21:48:39 +02:00
|
|
|
if (extrainfo)
|
2007-09-21 08:14:36 +02:00
|
|
|
router_load_extrainfo_from_string(contents, NULL,SAVED_IN_JOURNAL,
|
|
|
|
NULL, 0);
|
2007-04-30 21:48:39 +02:00
|
|
|
else
|
2007-09-21 08:14:36 +02:00
|
|
|
router_load_routers_from_string(contents, NULL, SAVED_IN_JOURNAL,
|
2007-09-27 22:46:30 +02:00
|
|
|
NULL, 0, NULL);
|
2007-08-31 17:08:37 +02:00
|
|
|
store->journal_len = (size_t) st.st_size;
|
2006-08-27 04:12:12 +02:00
|
|
|
tor_free(contents);
|
2006-06-22 09:01:54 +02:00
|
|
|
}
|
|
|
|
|
2005-09-30 23:11:22 +02:00
|
|
|
tor_free(fname);
|
2007-09-27 22:46:24 +02:00
|
|
|
tor_free(altname);
|
2005-09-15 07:19:38 +02:00
|
|
|
|
2007-09-27 22:46:24 +02:00
|
|
|
if (store->journal_len || read_from_old_location) {
|
2005-09-15 07:19:38 +02:00
|
|
|
/* Always clear the journal on startup.*/
|
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;
|
|
|
|
}
|
|
|
|
|
2006-04-10 03:35:56 +02:00
|
|
|
/** Return a smartlist containing a list of trusted_dir_server_t * for all
|
|
|
|
* known trusted dirservers. Callers must not modify the list or its
|
|
|
|
* contents.
|
2004-10-15 21:04:38 +02:00
|
|
|
*/
|
2006-04-10 03:35:56 +02:00
|
|
|
smartlist_t *
|
|
|
|
router_get_trusted_dir_servers(void)
|
2004-10-15 21:04:38 +02:00
|
|
|
{
|
|
|
|
if (!trusted_dir_servers)
|
|
|
|
trusted_dir_servers = smartlist_create();
|
|
|
|
|
2006-04-10 03:35:56 +02:00
|
|
|
return trusted_dir_servers;
|
2004-10-15 21:04:38 +02:00
|
|
|
}
|
|
|
|
|
2007-10-24 20:42:16 +02:00
|
|
|
/** Try to find a running dirserver that supports operations of <b>type</b>.
|
2007-10-24 17:45:45 +02:00
|
|
|
*
|
2007-10-24 20:42:16 +02:00
|
|
|
* If there are no running dirservers in our routerlist and the
|
|
|
|
* <b>PDS_RETRY_IF_NO_SERVERS</b> flag is set, set all the authoritative ones
|
|
|
|
* as running again, and pick one.
|
|
|
|
*
|
|
|
|
* If the <b>PDS_IGNORE_FASCISTFIREWALL</b> flag is set, then include
|
|
|
|
* dirservers that we can't reach.
|
|
|
|
*
|
|
|
|
* If the <b>PDS_ALLOW_SELF</b> flag is not set, then don't include ourself
|
2007-10-25 05:03:52 +02:00
|
|
|
* (if we're a dirserver).
|
2007-10-24 20:42:16 +02:00
|
|
|
*
|
|
|
|
* Don't pick an authority if any non-authority is viable; try to avoid using
|
|
|
|
* servers that have returned 503 recently.
|
2004-11-29 22:01:34 +01:00
|
|
|
*/
|
2010-09-29 07:35:08 +02:00
|
|
|
const routerstatus_t *
|
2007-10-24 20:42:16 +02:00
|
|
|
router_pick_directory_server(authority_type_t type, int flags)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2010-09-29 07:35:08 +02:00
|
|
|
const routerstatus_t *choice;
|
2007-10-24 20:42:16 +02:00
|
|
|
if (get_options()->PreferTunneledDirConns)
|
|
|
|
flags |= _PDS_PREFER_TUNNELED_DIR_CONNS;
|
2004-02-26 23:56:36 +01:00
|
|
|
|
2004-10-14 03:44:32 +02:00
|
|
|
if (!routerlist)
|
2004-10-14 04:29:03 +02:00
|
|
|
return NULL;
|
2004-10-14 03:44:32 +02:00
|
|
|
|
2007-10-24 20:42:16 +02:00
|
|
|
choice = router_pick_directory_server_impl(type, flags);
|
|
|
|
if (choice || !(flags & PDS_RETRY_IF_NO_SERVERS))
|
2004-07-20 08:44:16 +02:00
|
|
|
return choice;
|
|
|
|
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_DIR,
|
|
|
|
"No reachable router entries for dirservers. "
|
|
|
|
"Trying them all again.");
|
2004-08-18 23:13:58 +02:00
|
|
|
/* mark all authdirservers as up again */
|
2004-10-12 17:55:20 +02:00
|
|
|
mark_all_trusteddirservers_up();
|
2004-07-20 08:44:16 +02:00
|
|
|
/* try again */
|
2007-10-24 20:42:16 +02:00
|
|
|
choice = router_pick_directory_server_impl(type, flags);
|
2004-02-26 23:56:36 +01:00
|
|
|
return choice;
|
|
|
|
}
|
|
|
|
|
2009-05-27 23:55:51 +02:00
|
|
|
/** Try to determine which fraction of v2 and v3 directory requests aimed at
|
2008-12-26 18:35:08 +01:00
|
|
|
* caches will be sent to us. Set *<b>v2_share_out</b> and
|
|
|
|
* *<b>v3_share_out</b> to the fractions of v2 and v3 protocol shares we
|
2009-05-27 23:55:51 +02:00
|
|
|
* expect to see, respectively. Return 0 on success, negative on failure. */
|
2008-06-10 20:28:10 +02:00
|
|
|
int
|
|
|
|
router_get_my_share_of_directory_requests(double *v2_share_out,
|
|
|
|
double *v3_share_out)
|
|
|
|
{
|
2010-09-29 06:38:32 +02:00
|
|
|
const routerinfo_t *me = router_get_my_routerinfo();
|
2010-09-29 07:35:08 +02:00
|
|
|
const routerstatus_t *rs;
|
2008-06-10 20:28:10 +02:00
|
|
|
const int pds_flags = PDS_ALLOW_SELF|PDS_IGNORE_FASCISTFIREWALL;
|
|
|
|
*v2_share_out = *v3_share_out = 0.0;
|
2008-07-10 20:31:28 +02:00
|
|
|
if (!me)
|
|
|
|
return -1;
|
2008-06-10 20:28:10 +02:00
|
|
|
rs = router_get_consensus_status_by_id(me->cache_info.identity_digest);
|
2008-07-10 20:31:28 +02:00
|
|
|
if (!rs)
|
2008-06-10 20:28:10 +02:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Calling for side effect */
|
2008-12-26 18:35:08 +01:00
|
|
|
/* XXXX This is a bit of a kludge */
|
2008-06-10 20:28:10 +02:00
|
|
|
if (rs->is_v2_dir) {
|
|
|
|
sl_last_total_weighted_bw = 0;
|
|
|
|
router_pick_directory_server(V2_AUTHORITY, pds_flags);
|
|
|
|
if (sl_last_total_weighted_bw != 0) {
|
2008-06-13 21:57:52 +02:00
|
|
|
*v2_share_out = U64_TO_DBL(sl_last_weighted_bw_of_me) /
|
|
|
|
U64_TO_DBL(sl_last_total_weighted_bw);
|
2008-06-10 20:28:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rs->version_supports_v3_dir) {
|
|
|
|
sl_last_total_weighted_bw = 0;
|
|
|
|
router_pick_directory_server(V3_AUTHORITY, pds_flags);
|
|
|
|
if (sl_last_total_weighted_bw != 0) {
|
2008-06-13 21:57:52 +02:00
|
|
|
*v3_share_out = U64_TO_DBL(sl_last_weighted_bw_of_me) /
|
|
|
|
U64_TO_DBL(sl_last_total_weighted_bw);
|
2008-06-10 20:28:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-12-14 23:00:58 +01:00
|
|
|
/** Return the trusted_dir_server_t for the directory authority whose identity
|
|
|
|
* key hashes to <b>digest</b>, or NULL if no such authority is known.
|
|
|
|
*/
|
2005-09-08 08:22:44 +02:00
|
|
|
trusted_dir_server_t *
|
|
|
|
router_get_trusteddirserver_by_digest(const char *digest)
|
|
|
|
{
|
|
|
|
if (!trusted_dir_servers)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ds,
|
|
|
|
{
|
|
|
|
if (!memcmp(ds->digest, digest, DIGEST_LEN))
|
|
|
|
return ds;
|
|
|
|
});
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-04-20 08:48:35 +02:00
|
|
|
/** Return the trusted_dir_server_t for the directory authority whose
|
|
|
|
* v3 identity key hashes to <b>digest</b>, or NULL if no such authority
|
|
|
|
* is known.
|
2007-05-22 19:58:25 +02:00
|
|
|
*/
|
|
|
|
trusted_dir_server_t *
|
|
|
|
trusteddirserver_get_by_v3_auth_digest(const char *digest)
|
|
|
|
{
|
|
|
|
if (!trusted_dir_servers)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ds,
|
|
|
|
{
|
2007-10-26 16:57:26 +02:00
|
|
|
if (!memcmp(ds->v3_identity_digest, digest, DIGEST_LEN) &&
|
|
|
|
(ds->type & V3_AUTHORITY))
|
2007-05-22 19:58:25 +02:00
|
|
|
return ds;
|
|
|
|
});
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-10-24 20:42:16 +02:00
|
|
|
/** Try to find a running trusted dirserver. Flags are as for
|
|
|
|
* router_pick_directory_server.
|
2005-06-21 01:04:13 +02:00
|
|
|
*/
|
2010-09-29 07:35:08 +02:00
|
|
|
const routerstatus_t *
|
2007-10-24 20:42:16 +02:00
|
|
|
router_pick_trusteddirserver(authority_type_t type, int flags)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
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)
|
|
|
|
flags |= _PDS_PREFER_TUNNELED_DIR_CONNS;
|
2004-10-12 17:55:20 +02:00
|
|
|
|
2008-12-11 20:12:48 +01:00
|
|
|
choice = router_pick_trusteddirserver_impl(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,
|
|
|
|
"No trusted dirservers are reachable. Trying them all again.");
|
2004-10-12 17:55:20 +02:00
|
|
|
mark_all_trusteddirservers_up();
|
2008-12-11 20:12:48 +01:00
|
|
|
return router_pick_trusteddirserver_impl(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
|
|
|
*
|
2007-10-24 20:42:16 +02:00
|
|
|
* If the _PDS_PREFER_TUNNELED_DIR_CONNS flag is set, prefer directory servers
|
|
|
|
* that we can use with BEGINDIR.
|
2004-07-20 08:44:16 +02:00
|
|
|
*/
|
2010-09-29 07:35:08 +02:00
|
|
|
static const routerstatus_t *
|
2007-10-24 20:42:16 +02:00
|
|
|
router_pick_directory_server_impl(authority_type_t type, int flags)
|
2004-07-20 08:44:16 +02:00
|
|
|
{
|
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);
|
|
|
|
int prefer_tunnel = (flags & _PDS_PREFER_TUNNELED_DIR_CONNS);
|
2007-10-16 01:15:24 +02:00
|
|
|
|
|
|
|
if (!consensus)
|
|
|
|
return NULL;
|
2002-09-26 14:09:10 +02:00
|
|
|
|
2007-01-03 07:33:03 +01:00
|
|
|
direct = smartlist_create();
|
|
|
|
tunnel = smartlist_create();
|
|
|
|
trusted_direct = smartlist_create();
|
|
|
|
trusted_tunnel = smartlist_create();
|
|
|
|
overloaded_direct = smartlist_create();
|
|
|
|
overloaded_tunnel = smartlist_create();
|
|
|
|
|
2004-05-04 20:17:45 +02:00
|
|
|
/* Find all the running dirservers we know about. */
|
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;
|
|
|
|
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;
|
2007-10-08 19:44:19 +02:00
|
|
|
if (type & V3_AUTHORITY) {
|
|
|
|
if (!(status->version_supports_v3_dir ||
|
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_digest_is_trusted_dir_type(node->identity,
|
2007-10-08 19:44:19 +02:00
|
|
|
V3_AUTHORITY)))
|
|
|
|
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);
|
|
|
|
if ((type & V2_AUTHORITY) && !(node->rs->is_v2_dir || is_trusted))
|
2004-11-29 22:01:34 +01:00
|
|
|
continue;
|
2007-05-18 23:19:19 +02:00
|
|
|
if ((type & EXTRAINFO_CACHE) &&
|
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;
|
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 &&
|
2007-01-03 07:33:03 +01:00
|
|
|
status->version_supports_begindir &&
|
2007-04-27 12:26:09 +02:00
|
|
|
(!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);
|
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 result->rs;
|
2004-07-20 08:44:16 +02:00
|
|
|
}
|
2003-11-10 09:06:55 +01:00
|
|
|
|
2007-10-24 20:42:16 +02:00
|
|
|
/** Choose randomly from among the trusted dirservers that are up. Flags
|
|
|
|
* are as for router_pick_directory_server_impl().
|
2005-06-21 01:04:13 +02:00
|
|
|
*/
|
2010-09-29 07:35:08 +02:00
|
|
|
static const routerstatus_t *
|
2008-12-11 20:12:48 +01:00
|
|
|
router_pick_trusteddirserver_impl(authority_type_t type, int flags,
|
|
|
|
int *n_busy_out)
|
2004-10-12 17:55:20 +02:00
|
|
|
{
|
2007-01-03 07:33:03 +01:00
|
|
|
smartlist_t *direct, *tunnel;
|
|
|
|
smartlist_t *overloaded_direct, *overloaded_tunnel;
|
2010-09-29 06:38:32 +02:00
|
|
|
const routerinfo_t *me = router_get_my_routerinfo();
|
2010-09-29 07:35:08 +02:00
|
|
|
const routerstatus_t *result;
|
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);
|
|
|
|
const int prefer_tunnel = (flags & _PDS_PREFER_TUNNELED_DIR_CONNS);
|
|
|
|
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);
|
2008-12-11 20:12:48 +01:00
|
|
|
int n_busy = 0;
|
2007-01-03 07:33:03 +01:00
|
|
|
|
2008-01-16 06:27:19 +01:00
|
|
|
if (!trusted_dir_servers)
|
|
|
|
return NULL;
|
|
|
|
|
2007-01-03 07:33:03 +01:00
|
|
|
direct = smartlist_create();
|
|
|
|
tunnel = smartlist_create();
|
|
|
|
overloaded_direct = smartlist_create();
|
|
|
|
overloaded_tunnel = smartlist_create();
|
2004-07-20 08:44:16 +02:00
|
|
|
|
2008-08-05 22:08:19 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(trusted_dir_servers, trusted_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;
|
2007-05-18 23:19:19 +02:00
|
|
|
if ((type & EXTRAINFO_CACHE) &&
|
|
|
|
!router_supports_extrainfo(d->digest, 1))
|
|
|
|
continue;
|
2005-11-05 21:15:27 +01:00
|
|
|
if (requireother && me && router_digest_is_me(d->digest))
|
|
|
|
continue;
|
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)))
|
2007-01-03 07:33:03 +01:00
|
|
|
smartlist_add(is_overloaded ? overloaded_tunnel : tunnel,
|
2007-10-09 17:27:15 +02:00
|
|
|
&d->fake_status);
|
2007-04-27 12:26:09 +02:00
|
|
|
else if (!fascistfirewall ||
|
2008-08-05 22:08:19 +02:00
|
|
|
fascist_firewall_allows_address_dir(&addr, d->dir_port))
|
2007-01-03 07:33:03 +01:00
|
|
|
smartlist_add(is_overloaded ? overloaded_direct : direct,
|
2007-10-09 17:27:15 +02:00
|
|
|
&d->fake_status);
|
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)) {
|
|
|
|
result = smartlist_choose(tunnel);
|
|
|
|
} else if (smartlist_len(overloaded_tunnel)) {
|
|
|
|
result = smartlist_choose(overloaded_tunnel);
|
|
|
|
} else if (smartlist_len(direct)) {
|
|
|
|
result = smartlist_choose(direct);
|
|
|
|
} else {
|
|
|
|
result = smartlist_choose(overloaded_direct);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
return result;
|
2004-10-12 17:55:20 +02:00
|
|
|
}
|
|
|
|
|
2005-06-21 01:04:13 +02:00
|
|
|
/** Go through and mark the authoritative dirservers as up. */
|
2005-06-11 20:52:12 +02:00
|
|
|
static void
|
|
|
|
mark_all_trusteddirservers_up(void)
|
|
|
|
{
|
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 (router_digest_is_trusted_dir(node->identity))
|
|
|
|
node->is_running = 1;
|
|
|
|
});
|
2004-10-12 17:55:20 +02:00
|
|
|
if (trusted_dir_servers) {
|
|
|
|
SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, dir,
|
2005-09-14 23:09:25 +02:00
|
|
|
{
|
2007-10-09 17:27:15 +02:00
|
|
|
routerstatus_t *rs;
|
2005-09-14 23:09:25 +02:00
|
|
|
dir->is_running = 1;
|
2007-10-09 17:27:45 +02:00
|
|
|
download_status_reset(&dir->v2_ns_dl_status);
|
2010-09-29 07:35:08 +02:00
|
|
|
rs = router_get_mutable_consensus_status_by_id(dir->digest);
|
2007-10-09 17:27:15 +02:00
|
|
|
if (rs && !rs->is_running) {
|
|
|
|
rs->is_running = 1;
|
2006-12-24 03:45:35 +01:00
|
|
|
rs->last_dir_503_at = 0;
|
2006-10-20 23:04:39 +02:00
|
|
|
control_event_networkstatus_changed_single(rs);
|
|
|
|
}
|
2005-09-14 23:09:25 +02:00
|
|
|
});
|
2003-12-03 11:39:27 +01:00
|
|
|
}
|
2006-08-31 20:46:46 +02:00
|
|
|
router_dir_info_changed();
|
2005-10-05 07:03:52 +02:00
|
|
|
}
|
|
|
|
|
2010-09-15 04:19:00 +02:00
|
|
|
/** Return true iff r1 and r2 have the same address and OR port. */
|
|
|
|
int
|
|
|
|
routers_have_same_or_addr(const routerinfo_t *r1, const routerinfo_t *r2)
|
|
|
|
{
|
|
|
|
return r1->addr == r2->addr && r1->or_port == r2->or_port;
|
|
|
|
}
|
|
|
|
|
2005-10-05 07:03:52 +02:00
|
|
|
/** Reset all internal variables used to count failed downloads of network
|
|
|
|
* status objects. */
|
|
|
|
void
|
|
|
|
router_reset_status_download_failures(void)
|
|
|
|
{
|
|
|
|
mark_all_trusteddirservers_up();
|
2004-07-12 22:39:40 +02:00
|
|
|
}
|
|
|
|
|
2007-10-11 23:40:35 +02:00
|
|
|
/** Return true iff router1 and router2 have the same /16 network. */
|
|
|
|
static INLINE int
|
2010-09-29 06:38:32 +02:00
|
|
|
routers_in_same_network_family(const routerinfo_t *r1, const routerinfo_t *r2)
|
2007-10-11 23:40:35 +02:00
|
|
|
{
|
|
|
|
return (r1->addr & 0xffff0000) == (r2->addr & 0xffff0000);
|
|
|
|
}
|
|
|
|
|
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 0
|
2006-08-16 05:44:13 +02:00
|
|
|
/** Look through the routerlist and identify routers that
|
|
|
|
* advertise the same /16 network address as <b>router</b>.
|
|
|
|
* Add each of them to <b>sl</b>.
|
|
|
|
*/
|
|
|
|
static void
|
2010-09-29 06:38:32 +02:00
|
|
|
routerlist_add_network_family(smartlist_t *sl, const routerinfo_t *router)
|
2006-08-16 05:44:13 +02:00
|
|
|
{
|
|
|
|
SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, r,
|
|
|
|
{
|
2007-10-11 23:40:35 +02:00
|
|
|
if (router != r && routers_in_same_network_family(router, r))
|
2006-08-16 05:44:13 +02:00
|
|
|
smartlist_add(sl, r);
|
|
|
|
});
|
|
|
|
}
|
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
|
|
|
#endif
|
2006-08-16 05:44:13 +02:00
|
|
|
|
2004-10-15 03:58:11 +02:00
|
|
|
/** Add all the family of <b>router</b> to the smartlist <b>sl</b>.
|
2008-12-08 01:04:29 +01:00
|
|
|
* This is used to make sure we don't pick siblings in a single path,
|
|
|
|
* or pick more than one relay from a family for our entry guard list.
|
2004-09-10 23:40:29 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
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
|
|
|
nodelist_add_node_family(smartlist_t *sl, const node_t *router)
|
2005-06-11 20:52: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
|
|
|
/* XXXX MOVE */
|
|
|
|
#if 0
|
2010-09-29 06:38:32 +02:00
|
|
|
const routerinfo_t *r;
|
2005-07-22 23:12:10 +02:00
|
|
|
config_line_t *cl;
|
2006-09-26 00:12:54 +02:00
|
|
|
or_options_t *options = get_options();
|
2004-09-10 23:40:29 +02:00
|
|
|
|
2007-02-22 08:41:10 +01:00
|
|
|
/* First, add any routers with similar network addresses. */
|
2006-09-26 00:12:54 +02:00
|
|
|
if (options->EnforceDistinctSubnets)
|
|
|
|
routerlist_add_network_family(sl, router);
|
2006-08-16 05:44:13 +02:00
|
|
|
|
2007-05-22 02:46:55 +02:00
|
|
|
if (router->declared_family) {
|
|
|
|
/* Add every r such that router declares familyness with r, and r
|
|
|
|
* declares familyhood with router. */
|
2010-09-29 06:38:32 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(router->declared_family, const char *, n) {
|
2007-05-22 02:46:55 +02:00
|
|
|
if (!(r = router_get_by_nickname(n, 0)))
|
|
|
|
continue;
|
|
|
|
if (!r->declared_family)
|
|
|
|
continue;
|
|
|
|
SMARTLIST_FOREACH(r->declared_family, const char *, n2,
|
|
|
|
{
|
|
|
|
if (router_nickname_matches(router, n2))
|
2010-09-29 06:38:32 +02:00
|
|
|
smartlist_add(sl, (void*)r);
|
2007-05-22 02:46:55 +02:00
|
|
|
});
|
2010-09-29 06:38:32 +02:00
|
|
|
} SMARTLIST_FOREACH_END(n);
|
2007-05-22 02:46:55 +02:00
|
|
|
}
|
2004-10-15 22:52:09 +02:00
|
|
|
|
2005-06-21 01:04:13 +02:00
|
|
|
/* If the user declared any families locally, honor those too. */
|
2007-10-11 23:40:35 +02:00
|
|
|
for (cl = options->NodeFamilies; cl; cl = cl->next) {
|
2004-10-15 22:52:09 +02:00
|
|
|
if (router_nickname_is_in_list(router, cl->value)) {
|
2007-02-13 02:27:55 +01:00
|
|
|
add_nickname_list_to_smartlist(sl, cl->value, 0);
|
2004-10-15 22:52:09 +02:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
#endif
|
|
|
|
(void)sl;
|
|
|
|
(void)router;
|
|
|
|
UNIMPLEMENTED_NODELIST();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Given a <b>router</b>, add every node_t in its family to <b>sl</b>.
|
|
|
|
*
|
|
|
|
* Note the type mismatch: This function takes a routerinfo, but adds nodes
|
|
|
|
* to the smartlist!
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
routerlist_add_nodes_in_family(smartlist_t *sl, const routerinfo_t *router)
|
|
|
|
{
|
|
|
|
(void)router;
|
|
|
|
(void)sl;
|
|
|
|
UNIMPLEMENTED_NODELIST();
|
2004-09-10 23:40:29 +02:00
|
|
|
}
|
|
|
|
|
2007-10-11 23:40:35 +02:00
|
|
|
/** Return true iff r is named by some nickname in <b>lst</b>. */
|
|
|
|
static INLINE int
|
2010-09-29 06:38:32 +02:00
|
|
|
router_in_nickname_smartlist(smartlist_t *lst, const routerinfo_t *r)
|
2007-10-11 23:40:35 +02:00
|
|
|
{
|
|
|
|
if (!lst) return 0;
|
|
|
|
SMARTLIST_FOREACH(lst, const char *, name,
|
|
|
|
if (router_nickname_matches(r, name))
|
|
|
|
return 1;);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return true iff r1 and r2 are in the same family, but not the same
|
|
|
|
* router. */
|
|
|
|
int
|
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
|
|
|
nodes_in_same_family(const node_t *node1, const node_t *node2)
|
2007-10-11 23:40:35 +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
|
|
|
#if 0
|
2007-10-11 23:40:35 +02:00
|
|
|
or_options_t *options = get_options();
|
|
|
|
config_line_t *cl;
|
|
|
|
|
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 (options->EnforceDistinctSubnets &&
|
|
|
|
nodes_in_same_network_family(node1,node2))
|
2007-10-11 23:40:35 +02:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (router_in_nickname_smartlist(r1->declared_family, r2) &&
|
|
|
|
router_in_nickname_smartlist(r2->declared_family, r1))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
for (cl = options->NodeFamilies; cl; cl = cl->next) {
|
|
|
|
if (router_nickname_is_in_list(r1, cl->value) &&
|
|
|
|
router_nickname_is_in_list(r2, cl->value))
|
|
|
|
return 1;
|
|
|
|
}
|
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
|
|
|
#endif
|
|
|
|
(void)node1;
|
|
|
|
(void)node2;
|
|
|
|
UNIMPLEMENTED_NODELIST();
|
2007-10-11 23:40:35 +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 0
|
2006-09-29 01:57:44 +02:00
|
|
|
/** Given a (possibly NULL) comma-and-whitespace separated list of nicknames,
|
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
|
|
|
* see which nicknames in <b>list</b> name nodes we know about, and add
|
|
|
|
* the nodes for those routers to <b>sl</b>. If <b>must_be_running</b>,
|
2007-02-13 02:27:55 +01:00
|
|
|
* only include routers that we think are running.
|
|
|
|
* Warn if any non-Named routers are specified by nickname.
|
2004-05-04 20:17:45 +02:00
|
|
|
*/
|
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 void
|
2005-12-14 21:40:40 +01:00
|
|
|
add_nickname_list_to_smartlist(smartlist_t *sl, const char *list,
|
2007-02-13 02:27:55 +01:00
|
|
|
int must_be_running)
|
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 */
|
|
|
|
/*XXXX this is only used in one place. Can we kill it?*/
|
|
|
|
const node_t *node;
|
2010-09-29 06:38:32 +02:00
|
|
|
const routerinfo_t *router;
|
2004-09-02 20:39:59 +02:00
|
|
|
smartlist_t *nickname_list;
|
2005-12-15 21:44:15 +01:00
|
|
|
int have_dir_info = router_have_minimum_dir_info();
|
2004-04-03 00:23:15 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!list)
|
2004-11-03 11:08:44 +01:00
|
|
|
return; /* nothing to do */
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(sl);
|
2004-04-03 01:38:26 +02:00
|
|
|
|
2004-09-02 20:39:59 +02:00
|
|
|
nickname_list = smartlist_create();
|
2005-04-03 07:53:34 +02:00
|
|
|
if (!warned_nicknames)
|
|
|
|
warned_nicknames = smartlist_create();
|
2004-04-03 00:23:15 +02:00
|
|
|
|
2004-09-02 20:39:59 +02:00
|
|
|
smartlist_split_string(nickname_list, list, ",",
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
|
|
|
|
2010-09-29 06:38:32 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(nickname_list, const char *, nick) {
|
2005-04-03 07:53:34 +02:00
|
|
|
int warned;
|
2005-01-03 18:53:20 +01:00
|
|
|
if (!is_legal_nickname_or_hexdigest(nick)) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_CONFIG, "Nickname '%s' is misformed; skipping", nick);
|
2004-07-22 23:36:03 +02:00
|
|
|
continue;
|
|
|
|
}
|
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
|
|
|
node = node_get_by_nickname(nick, 1);
|
|
|
|
router = node->ri;
|
2005-04-03 07:53:34 +02:00
|
|
|
warned = smartlist_string_isin(warned_nicknames, nick);
|
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) {
|
|
|
|
if (!must_be_running || node->is_running) {
|
|
|
|
smartlist_add(sl,(void*)node);
|
2005-04-03 07:53:34 +02:00
|
|
|
}
|
2007-10-16 01:15:24 +02:00
|
|
|
} else if (!router_get_consensus_status_by_nickname(nick,1)) {
|
2005-04-03 07:53:34 +02:00
|
|
|
if (!warned) {
|
2005-12-15 21:44:15 +01:00
|
|
|
log_fn(have_dir_info ? LOG_WARN : LOG_INFO, LD_CONFIG,
|
2005-04-03 07:53:34 +02:00
|
|
|
"Nickname list includes '%s' which isn't a known router.",nick);
|
|
|
|
smartlist_add(warned_nicknames, tor_strdup(nick));
|
|
|
|
}
|
|
|
|
}
|
2010-09-29 06:38:32 +02:00
|
|
|
} SMARTLIST_FOREACH_END(nick);
|
2004-09-02 20:39:59 +02:00
|
|
|
SMARTLIST_FOREACH(nickname_list, char *, nick, tor_free(nick));
|
|
|
|
smartlist_free(nickname_list);
|
2004-04-03 00:23:15 +02:00
|
|
|
}
|
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
|
|
|
#endif
|
2004-04-03 00:23:15 +02:00
|
|
|
|
2006-09-29 01:57:44 +02:00
|
|
|
/** Return 1 iff any member of the (possibly NULL) comma-separated list
|
|
|
|
* <b>list</b> is an acceptable nickname or hexdigest for <b>router</b>. Else
|
|
|
|
* return 0.
|
2004-10-15 22:52:09 +02:00
|
|
|
*/
|
2006-09-29 01:57:44 +02:00
|
|
|
int
|
2010-09-29 06:38:32 +02:00
|
|
|
router_nickname_is_in_list(const routerinfo_t *router, const char *list)
|
2004-10-15 22:52:09 +02:00
|
|
|
{
|
|
|
|
smartlist_t *nickname_list;
|
|
|
|
int v = 0;
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!list)
|
2004-11-03 11:08:44 +01:00
|
|
|
return 0; /* definitely not */
|
2004-10-15 22:52:09 +02:00
|
|
|
tor_assert(router);
|
|
|
|
|
|
|
|
nickname_list = smartlist_create();
|
|
|
|
smartlist_split_string(nickname_list, list, ",",
|
2008-10-01 05:41:33 +02:00
|
|
|
SPLIT_SKIP_SPACE|SPLIT_STRIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
2004-10-16 10:39:56 +02:00
|
|
|
SMARTLIST_FOREACH(nickname_list, const char *, cp,
|
2004-10-15 22:52:09 +02:00
|
|
|
if (router_nickname_matches(router, cp)) {v=1;break;});
|
2004-10-16 10:39:56 +02:00
|
|
|
SMARTLIST_FOREACH(nickname_list, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(nickname_list);
|
2004-10-15 22:52:09 +02:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2005-08-15 05:25:40 +02:00
|
|
|
/** Find a router that's up, that has this IP address, and
|
|
|
|
* that allows exit to this address:port, or return NULL if there
|
|
|
|
* isn't a good one.
|
|
|
|
*/
|
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 *
|
2005-09-13 17:32:03 +02:00
|
|
|
router_find_exact_exit_enclave(const char *address, uint16_t port)
|
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*/
|
2005-08-15 05:25:40 +02:00
|
|
|
uint32_t addr;
|
|
|
|
struct in_addr in;
|
2008-12-29 02:47:28 +01:00
|
|
|
tor_addr_t a;
|
2005-08-15 05:25:40 +02:00
|
|
|
|
|
|
|
if (!tor_inet_aton(address, &in))
|
|
|
|
return NULL; /* it's not an IP already */
|
|
|
|
addr = ntohl(in.s_addr);
|
|
|
|
|
2008-12-29 02:47:28 +01:00
|
|
|
tor_addr_from_ipv4h(&a, 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
|
|
|
SMARTLIST_FOREACH(nodelist_get_list(), const node_t *, node, {
|
|
|
|
if (node_get_addr_ipv4h(node) == addr &&
|
|
|
|
node->is_running &&
|
|
|
|
compare_tor_addr_to_node_policy(&a, port, node) ==
|
2005-08-15 05:25:40 +02:00
|
|
|
ADDR_POLICY_ACCEPTED)
|
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 node;
|
2005-09-13 17:32:03 +02:00
|
|
|
});
|
2005-08-15 05:25:40 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-06-21 01:04:13 +02:00
|
|
|
/** Return 1 if <b>router</b> is not suitable for these parameters, else 0.
|
|
|
|
* If <b>need_uptime</b> is non-zero, we require a minimum uptime.
|
|
|
|
* If <b>need_capacity</b> is non-zero, we require a minimum advertised
|
|
|
|
* bandwidth.
|
2007-02-02 19:58:11 +01:00
|
|
|
* If <b>need_guard</b>, we require that the router is a possible entry guard.
|
2005-06-21 01:04:13 +02:00
|
|
|
*/
|
2004-08-20 23:34:36 +02:00
|
|
|
int
|
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
|
|
|
node_is_unreliable(const node_t *node, int need_uptime,
|
|
|
|
int need_capacity, int need_guard)
|
2004-08-20 23:34:36 +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
|
|
|
if (need_uptime && !node->is_stable)
|
2004-08-20 23:34:36 +02:00
|
|
|
return 1;
|
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 (need_capacity && !node->is_fast)
|
2004-08-20 23:34:36 +02:00
|
|
|
return 1;
|
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 (need_guard && !node->is_possible_guard)
|
2006-02-12 04:43:39 +01:00
|
|
|
return 1;
|
2004-08-20 23:34:36 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2004-08-15 10:15:12 +02:00
|
|
|
|
2006-03-28 14:01:58 +02:00
|
|
|
/** Return the smaller of the router's configured BandwidthRate
|
|
|
|
* and its advertised capacity. */
|
|
|
|
uint32_t
|
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;
|
|
|
|
}
|
|
|
|
|
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 *
|
|
|
|
smartlist_choose_node_by_bandwidth_weights(smartlist_t *sl,
|
|
|
|
bandwidth_weight_rule_t rule)
|
2010-01-28 00:01:51 +01:00
|
|
|
{
|
|
|
|
int64_t weight_scale;
|
|
|
|
int64_t rand_bw;
|
|
|
|
double Wg = -1, Wm = -1, We = -1, Wd = -1;
|
|
|
|
double Wgb = -1, Wmb = -1, Web = -1, Wdb = -1;
|
|
|
|
double weighted_bw = 0;
|
|
|
|
double *bandwidths;
|
|
|
|
double tmp = 0;
|
|
|
|
unsigned int i;
|
2010-09-27 18:23:49 +02:00
|
|
|
int have_unknown = 0; /* true iff sl contains element not in consensus. */
|
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));
|
2010-02-27 01:17:09 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-01-28 00:01:51 +01:00
|
|
|
weight_scale = networkstatus_get_param(NULL, "bwweightscale",
|
|
|
|
BW_WEIGHT_SCALE);
|
|
|
|
|
|
|
|
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.");
|
|
|
|
return NULL; // Use old algorithm.
|
|
|
|
}
|
|
|
|
|
|
|
|
Wg /= weight_scale;
|
|
|
|
Wm /= weight_scale;
|
|
|
|
We /= weight_scale;
|
|
|
|
Wd /= weight_scale;
|
|
|
|
|
|
|
|
Wgb /= weight_scale;
|
|
|
|
Wmb /= weight_scale;
|
|
|
|
Web /= weight_scale;
|
|
|
|
Wdb /= weight_scale;
|
|
|
|
|
|
|
|
bandwidths = tor_malloc_zero(sizeof(double)*smartlist_len(sl));
|
|
|
|
|
|
|
|
// 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) {
|
2010-06-21 10:13:17 +02:00
|
|
|
int is_exit = 0, is_guard = 0, is_dir = 0, this_bw = 0, is_me = 0;
|
2010-01-28 00:01:51 +01:00
|
|
|
double weight = 1;
|
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;
|
|
|
|
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.");
|
|
|
|
return NULL;
|
|
|
|
}
|
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
|
|
|
this_bw = kb_to_bytes(node->rs->bandwidth);
|
|
|
|
} else if (node->ri) {
|
|
|
|
/* bridge or other descriptor not in our consensus */
|
|
|
|
this_bw = bridge_get_advertised_bandwidth_bounded(node->ri);
|
|
|
|
have_unknown = 1;
|
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
|
|
|
is_me = router_digest_is_me(node->identity);
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
bandwidths[node_sl_idx] = weight*this_bw;
|
2010-01-28 00:01:51 +01:00
|
|
|
weighted_bw += weight*this_bw;
|
2010-06-21 10:13:17 +02:00
|
|
|
if (is_me)
|
|
|
|
sl_last_weighted_bw_of_me = weight*this_bw;
|
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
|
|
|
|
2010-06-21 10:13:17 +02:00
|
|
|
/* XXXX022 this is a kludge to expose these values. */
|
|
|
|
sl_last_total_weighted_bw = weighted_bw;
|
|
|
|
|
2010-04-13 00:12:49 +02:00
|
|
|
log_debug(LD_CIRC, "Choosing node for rule %s based on weights "
|
|
|
|
"Wg=%lf Wm=%lf We=%lf Wd=%lf with total bw %lf",
|
|
|
|
bandwidth_weight_rule_to_string(rule),
|
2010-01-28 00:01:51 +01:00
|
|
|
Wg, Wm, We, Wd, weighted_bw);
|
|
|
|
|
2010-02-27 01:17:09 +01:00
|
|
|
/* If there is no bandwidth, choose at random */
|
|
|
|
if (DBL_TO_U64(weighted_bw) == 0) {
|
2010-09-27 18:23:49 +02:00
|
|
|
/* Don't warn when using bridges/relays not in the consensus */
|
2010-09-05 07:43:03 +02:00
|
|
|
if (!have_unknown)
|
|
|
|
log_warn(LD_CIRC,
|
|
|
|
"Weighted bandwidth is %lf in node selection for rule %s",
|
|
|
|
weighted_bw, bandwidth_weight_rule_to_string(rule));
|
2010-02-27 01:17:09 +01:00
|
|
|
tor_free(bandwidths);
|
|
|
|
return smartlist_choose(sl);
|
|
|
|
}
|
|
|
|
|
2010-01-28 00:01:51 +01:00
|
|
|
rand_bw = crypto_rand_uint64(DBL_TO_U64(weighted_bw));
|
|
|
|
rand_bw++; /* crypto_rand_uint64() counts from 0, and we need to count
|
|
|
|
* from 1 below. See bug 1203 for details. */
|
|
|
|
|
|
|
|
/* Last, count through sl until we get to the element we picked */
|
|
|
|
tmp = 0.0;
|
|
|
|
for (i=0; i < (unsigned)smartlist_len(sl); i++) {
|
|
|
|
tmp += bandwidths[i];
|
|
|
|
if (tmp >= rand_bw)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == (unsigned)smartlist_len(sl)) {
|
|
|
|
/* This was once possible due to round-off error, but shouldn't be able
|
|
|
|
* to occur any longer. */
|
|
|
|
tor_fragile_assert();
|
|
|
|
--i;
|
|
|
|
log_warn(LD_BUG, "Round-off error in computing bandwidth had an effect on "
|
|
|
|
" which router we chose. Please tell the developers. "
|
|
|
|
"%lf " U64_FORMAT " %lf", tmp, U64_PRINTF_ARG(rand_bw),
|
|
|
|
weighted_bw);
|
|
|
|
}
|
|
|
|
tor_free(bandwidths);
|
|
|
|
return smartlist_get(sl, i);
|
|
|
|
}
|
|
|
|
|
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 *
|
|
|
|
smartlist_choose_node_by_bandwidth(smartlist_t *sl,
|
|
|
|
bandwidth_weight_rule_t rule)
|
2004-08-15 10:15:12 +02:00
|
|
|
{
|
2007-07-26 00:56:34 +02:00
|
|
|
unsigned int i;
|
2010-09-29 06:38:32 +02:00
|
|
|
const routerstatus_t *status=NULL;
|
2007-02-08 20:44:55 +01:00
|
|
|
int32_t *bandwidths;
|
2007-02-09 00:24:03 +01:00
|
|
|
int is_exit;
|
2007-08-24 08:30:34 +02:00
|
|
|
int is_guard;
|
2006-10-01 23:59:09 +02:00
|
|
|
uint64_t total_nonexit_bw = 0, total_exit_bw = 0, total_bw = 0;
|
2007-08-24 08:30:34 +02:00
|
|
|
uint64_t total_nonguard_bw = 0, total_guard_bw = 0;
|
2006-10-01 23:59:09 +02:00
|
|
|
uint64_t rand_bw, tmp;
|
|
|
|
double exit_weight;
|
2007-08-24 08:30:34 +02:00
|
|
|
double guard_weight;
|
2007-02-08 20:44:55 +01:00
|
|
|
int n_unknown = 0;
|
2007-07-26 23:26:57 +02:00
|
|
|
bitarray_t *exit_bits;
|
2007-08-24 08:30:34 +02:00
|
|
|
bitarray_t *guard_bits;
|
2008-06-13 21:57:52 +02:00
|
|
|
int me_idx = -1;
|
2007-08-24 08:30:34 +02:00
|
|
|
|
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
|
2007-02-08 20:44:55 +01:00
|
|
|
* of each value. <0 means "unknown; no routerinfo." We use the
|
|
|
|
* bits of negative values to remember whether the router was fast (-x)&1
|
2007-08-25 23:31:34 +02:00
|
|
|
* and whether it was an exit (-x)&2 or guard (-x)&4. Yes, it's a hack. */
|
2007-02-08 20:44:55 +01:00
|
|
|
bandwidths = tor_malloc(sizeof(int32_t)*smartlist_len(sl));
|
2007-07-26 23:26:57 +02:00
|
|
|
exit_bits = bitarray_init_zero(smartlist_len(sl));
|
2007-08-24 08:30:34 +02:00
|
|
|
guard_bits = bitarray_init_zero(smartlist_len(sl));
|
2007-02-08 20:44:55 +01:00
|
|
|
|
|
|
|
/* Iterate over all the routerinfo_t or routerstatus_t, and */
|
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;
|
|
|
|
int32_t flags = 0;
|
2007-02-09 00:24:03 +01:00
|
|
|
uint32_t this_bw = 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 (router_digest_is_me(node->identity))
|
|
|
|
me_idx = node_sl_idx;
|
|
|
|
|
|
|
|
is_exit = node->is_exit;
|
|
|
|
is_guard = node->is_possible_guard;
|
|
|
|
if (node->rs) {
|
|
|
|
if (node->rs->has_bandwidth) {
|
2009-06-25 16:55:08 +02:00
|
|
|
this_bw = kb_to_bytes(status->bandwidth);
|
2007-01-27 10:13:19 +01:00
|
|
|
} else { /* guess */
|
2009-06-21 14:54:35 +02:00
|
|
|
/* XXX022 once consensuses always list bandwidths, we can take
|
|
|
|
* this guessing business out. -RD */
|
2007-02-08 20:44:55 +01:00
|
|
|
is_known = 0;
|
|
|
|
flags = status->is_fast ? 1 : 0;
|
|
|
|
flags |= is_exit ? 2 : 0;
|
2007-08-24 08:30:34 +02:00
|
|
|
flags |= is_guard ? 4 : 0;
|
2007-01-27 10:13:19 +01:00
|
|
|
}
|
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);
|
2007-02-08 20:44:55 +01:00
|
|
|
if (is_known) {
|
|
|
|
bandwidths[i] = (int32_t) this_bw; // safe since MAX_BELIEVABLE<INT32_MAX
|
2010-09-30 06:11:51 +02:00
|
|
|
// XXX this is no longer true! We don't always cap the bw anymore. Can
|
|
|
|
// a consensus make us overflow?-sh
|
2009-06-25 16:55:08 +02:00
|
|
|
tor_assert(bandwidths[i] >= 0);
|
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;
|
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
|
|
|
bandwidths[node_sl_idx] = -flags;
|
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
|
|
|
|
|
|
|
/* Now, fill in the unknown values. */
|
|
|
|
if (n_unknown) {
|
|
|
|
int32_t avg_fast, avg_slow;
|
|
|
|
if (total_exit_bw+total_nonexit_bw) {
|
2007-02-08 21:27:58 +01:00
|
|
|
/* if there's some bandwidth, there's at least one known router,
|
|
|
|
* so no worries about div by 0 here */
|
2007-02-16 21:01:21 +01:00
|
|
|
int n_known = smartlist_len(sl)-n_unknown;
|
|
|
|
avg_fast = avg_slow = (int32_t)
|
|
|
|
((total_exit_bw+total_nonexit_bw)/((uint64_t) n_known));
|
2007-02-08 20:44:55 +01:00
|
|
|
} else {
|
|
|
|
avg_fast = 40000;
|
|
|
|
avg_slow = 20000;
|
|
|
|
}
|
2007-07-26 00:56:34 +02:00
|
|
|
for (i=0; i<(unsigned)smartlist_len(sl); ++i) {
|
2007-02-08 20:44:55 +01:00
|
|
|
int32_t bw = bandwidths[i];
|
|
|
|
if (bw>=0)
|
|
|
|
continue;
|
|
|
|
is_exit = ((-bw)&2);
|
2007-08-24 08:30:34 +02:00
|
|
|
is_guard = ((-bw)&4);
|
2007-02-08 20:44:55 +01:00
|
|
|
bandwidths[i] = ((-bw)&1) ? avg_fast : avg_slow;
|
|
|
|
if (is_exit)
|
|
|
|
total_exit_bw += bandwidths[i];
|
|
|
|
else
|
|
|
|
total_nonexit_bw += bandwidths[i];
|
2007-08-24 08:30:34 +02:00
|
|
|
if (is_guard)
|
|
|
|
total_guard_bw += bandwidths[i];
|
|
|
|
else
|
|
|
|
total_nonguard_bw += bandwidths[i];
|
2007-02-08 20:44:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If there's no bandwidth at all, pick at random. */
|
2006-10-01 23:59:09 +02:00
|
|
|
if (!(total_exit_bw+total_nonexit_bw)) {
|
|
|
|
tor_free(bandwidths);
|
2007-08-24 08:30:34 +02:00
|
|
|
tor_free(exit_bits);
|
|
|
|
tor_free(guard_bits);
|
2004-11-20 13:41:05 +01:00
|
|
|
return smartlist_choose(sl);
|
2004-12-07 09:51:10 +01:00
|
|
|
}
|
2007-02-08 20:44:55 +01:00
|
|
|
|
2007-08-24 08:30:34 +02:00
|
|
|
/* Figure out how to weight exits and guards */
|
|
|
|
{
|
2007-07-29 00:14:39 +02:00
|
|
|
double all_bw = U64_TO_DBL(total_exit_bw+total_nonexit_bw);
|
|
|
|
double exit_bw = U64_TO_DBL(total_exit_bw);
|
2007-08-24 08:30:34 +02:00
|
|
|
double guard_bw = U64_TO_DBL(total_guard_bw);
|
2007-07-29 00:14:39 +02:00
|
|
|
/*
|
|
|
|
* For detailed derivation of this formula, see
|
|
|
|
* http://archives.seul.org/or/dev/Jul-2007/msg00056.html
|
|
|
|
*/
|
2010-01-02 19:13:30 +01:00
|
|
|
if (rule == WEIGHT_FOR_EXIT || !total_exit_bw)
|
2007-08-24 08:30:34 +02:00
|
|
|
exit_weight = 1.0;
|
|
|
|
else
|
|
|
|
exit_weight = 1.0 - all_bw/(3.0*exit_bw);
|
|
|
|
|
2010-01-02 19:13:30 +01:00
|
|
|
if (rule == WEIGHT_FOR_GUARD || !total_guard_bw)
|
2007-08-24 08:30:34 +02:00
|
|
|
guard_weight = 1.0;
|
|
|
|
else
|
|
|
|
guard_weight = 1.0 - all_bw/(3.0*guard_bw);
|
|
|
|
|
|
|
|
if (exit_weight <= 0.0)
|
2007-07-29 00:14:39 +02:00
|
|
|
exit_weight = 0.0;
|
2007-08-24 08:30:34 +02:00
|
|
|
|
|
|
|
if (guard_weight <= 0.0)
|
|
|
|
guard_weight = 0.0;
|
|
|
|
|
|
|
|
total_bw = 0;
|
2008-06-13 21:57:52 +02:00
|
|
|
sl_last_weighted_bw_of_me = 0;
|
2007-08-24 08:30:34 +02:00
|
|
|
for (i=0; i < (unsigned)smartlist_len(sl); i++) {
|
2008-06-13 21:57:52 +02:00
|
|
|
uint64_t bw;
|
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)
|
2008-06-13 21:57:52 +02:00
|
|
|
bw = ((uint64_t)(bandwidths[i] * exit_weight * guard_weight));
|
2007-08-24 08:30:34 +02:00
|
|
|
else if (is_guard)
|
2008-06-13 21:57:52 +02:00
|
|
|
bw = ((uint64_t)(bandwidths[i] * guard_weight));
|
2007-08-24 08:30:34 +02:00
|
|
|
else if (is_exit)
|
2008-06-13 21:57:52 +02:00
|
|
|
bw = ((uint64_t)(bandwidths[i] * exit_weight));
|
2007-08-24 08:30:34 +02:00
|
|
|
else
|
2008-06-13 21:57:52 +02:00
|
|
|
bw = bandwidths[i];
|
|
|
|
total_bw += bw;
|
|
|
|
if (i == (unsigned) me_idx)
|
|
|
|
sl_last_weighted_bw_of_me = bw;
|
2007-07-26 23:26:57 +02:00
|
|
|
}
|
2006-10-01 23:59:09 +02:00
|
|
|
}
|
2008-06-10 20:28:10 +02:00
|
|
|
|
2009-01-04 20:47:21 +01:00
|
|
|
/* XXXX022 this is a kludge to expose these values. */
|
2008-06-10 20:28:10 +02:00
|
|
|
sl_last_total_weighted_bw = total_bw;
|
|
|
|
|
2007-10-25 05:03:52 +02:00
|
|
|
log_debug(LD_CIRC, "Total weighted bw = "U64_FORMAT
|
2007-08-24 08:30:34 +02:00
|
|
|
", exit bw = "U64_FORMAT
|
|
|
|
", nonexit bw = "U64_FORMAT", exit weight = %lf "
|
|
|
|
"(for exit == %d)"
|
|
|
|
", guard bw = "U64_FORMAT
|
|
|
|
", nonguard bw = "U64_FORMAT", guard weight = %lf "
|
|
|
|
"(for guard == %d)",
|
2007-08-24 12:50:48 +02:00
|
|
|
U64_PRINTF_ARG(total_bw),
|
|
|
|
U64_PRINTF_ARG(total_exit_bw), U64_PRINTF_ARG(total_nonexit_bw),
|
2007-08-24 14:33:53 +02:00
|
|
|
exit_weight, (int)(rule == WEIGHT_FOR_EXIT),
|
2007-08-24 12:50:48 +02:00
|
|
|
U64_PRINTF_ARG(total_guard_bw), U64_PRINTF_ARG(total_nonguard_bw),
|
2007-08-24 14:33:53 +02:00
|
|
|
guard_weight, (int)(rule == WEIGHT_FOR_GUARD));
|
2006-10-01 23:59:09 +02:00
|
|
|
|
2007-02-08 20:44:55 +01:00
|
|
|
/* Almost done: choose a random value from the bandwidth weights. */
|
2006-10-01 23:59:09 +02:00
|
|
|
rand_bw = crypto_rand_uint64(total_bw);
|
2010-01-13 05:10:45 +01:00
|
|
|
rand_bw++; /* crypto_rand_uint64() counts from 0, and we need to count
|
|
|
|
* from 1 below. See bug 1203 for details. */
|
2007-02-08 20:44:55 +01:00
|
|
|
|
2005-06-21 01:04:13 +02:00
|
|
|
/* Last, count through sl until we get to the element we picked */
|
2004-08-15 10:15:12 +02:00
|
|
|
tmp = 0;
|
2007-07-26 00:56:34 +02:00
|
|
|
for (i=0; i < (unsigned)smartlist_len(sl); i++) {
|
2007-07-26 23:26:57 +02:00
|
|
|
is_exit = bitarray_is_set(exit_bits, i);
|
2007-08-24 08:30:34 +02:00
|
|
|
is_guard = bitarray_is_set(guard_bits, i);
|
|
|
|
|
|
|
|
/* Weights can be 0 if not counting guards/exits */
|
|
|
|
if (is_exit && is_guard)
|
|
|
|
tmp += ((uint64_t)(bandwidths[i] * exit_weight * guard_weight));
|
|
|
|
else if (is_guard)
|
|
|
|
tmp += ((uint64_t)(bandwidths[i] * guard_weight));
|
|
|
|
else if (is_exit)
|
|
|
|
tmp += ((uint64_t)(bandwidths[i] * exit_weight));
|
2007-08-24 12:50:48 +02:00
|
|
|
else
|
2006-10-01 23:59:09 +02:00
|
|
|
tmp += bandwidths[i];
|
2007-08-24 08:30:34 +02:00
|
|
|
|
2007-07-22 06:36:33 +02:00
|
|
|
if (tmp >= rand_bw)
|
|
|
|
break;
|
2004-08-15 10:15:12 +02:00
|
|
|
}
|
2007-07-26 00:56:34 +02:00
|
|
|
if (i == (unsigned)smartlist_len(sl)) {
|
2007-07-26 23:26:57 +02:00
|
|
|
/* This was once possible due to round-off error, but shouldn't be able
|
|
|
|
* to occur any longer. */
|
|
|
|
tor_fragile_assert();
|
2007-07-12 18:34:45 +02:00
|
|
|
--i;
|
|
|
|
log_warn(LD_BUG, "Round-off error in computing bandwidth had an effect on "
|
2007-08-24 08:30:34 +02:00
|
|
|
" which router we chose. Please tell the developers. "
|
|
|
|
U64_FORMAT " " U64_FORMAT " " U64_FORMAT, U64_PRINTF_ARG(tmp),
|
|
|
|
U64_PRINTF_ARG(rand_bw), U64_PRINTF_ARG(total_bw));
|
2007-07-12 18:34:45 +02:00
|
|
|
}
|
2006-10-01 23:59:09 +02:00
|
|
|
tor_free(bandwidths);
|
2007-07-26 23:26:57 +02:00
|
|
|
tor_free(exit_bits);
|
2007-08-24 08:30:34 +02:00
|
|
|
tor_free(guard_bits);
|
2007-01-27 10:13:19 +01:00
|
|
|
return smartlist_get(sl, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Choose a random element of 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 *
|
|
|
|
node_sl_choose_by_bandwidth(smartlist_t *sl,
|
|
|
|
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
|
|
|
|
2009-09-20 03:02:21 +02:00
|
|
|
smartlist_t *sl=smartlist_create(),
|
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
|
|
|
*excludednodes=smartlist_create();
|
|
|
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2008-02-04 18:25:24 +01:00
|
|
|
if ((r = routerlist_find_my_routerinfo())) {
|
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 *me = node_get_by_id(r->cache_info.identity_digest);
|
|
|
|
if (me)
|
|
|
|
smartlist_add(excludednodes, (void *)me);
|
|
|
|
routerlist_add_nodes_in_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. */
|
|
|
|
static int
|
|
|
|
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 == '~') {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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 !memcmp(digest, identity_digest, DIGEST_LEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return true iff <b>router</b> is listed as named in the current consensus. */
|
|
|
|
static int
|
|
|
|
router_is_named(const routerinfo_t *router)
|
|
|
|
{
|
|
|
|
const char *digest =
|
|
|
|
networkstatus_get_router_digest_by_nickname(router->nickname);
|
|
|
|
|
|
|
|
return (digest &&
|
|
|
|
!memcmp(digest, router->cache_info.identity_digest, DIGEST_LEN));
|
2008-07-18 20:36:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Return true iff the digest of <b>router</b>'s identity key,
|
|
|
|
* encoded in hexadecimal, matches <b>hexdigest</b> (which is
|
|
|
|
* optionally prefixed with a single dollar sign). Return false if
|
|
|
|
* <b>hexdigest</b> is malformed, or it doesn't match. */
|
|
|
|
static INLINE int
|
2010-09-29 06:38:32 +02:00
|
|
|
router_hex_digest_matches(const routerinfo_t *router, const char *hexdigest)
|
2008-07-18 20:36:32 +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 hex_digest_nickname_matches(hexdigest,
|
|
|
|
router->cache_info.identity_digest,
|
|
|
|
router->nickname,
|
|
|
|
router_is_named(router));
|
2004-07-03 01:40:03 +02:00
|
|
|
}
|
|
|
|
|
2005-06-21 01:04:13 +02:00
|
|
|
/** Return true if <b>router</b>'s nickname matches <b>nickname</b>
|
2004-07-17 00:23:18 +02:00
|
|
|
* (case-insensitive), or if <b>router's</b> identity key digest
|
|
|
|
* matches a hexadecimal value stored in <b>nickname</b>. Return
|
2005-06-21 01:04:13 +02:00
|
|
|
* false otherwise. */
|
|
|
|
static int
|
2010-09-29 06:38:32 +02:00
|
|
|
router_nickname_matches(const routerinfo_t *router, const char *nickname)
|
2004-07-03 01:40:03 +02:00
|
|
|
{
|
|
|
|
if (nickname[0]!='$' && !strcasecmp(router->nickname, nickname))
|
|
|
|
return 1;
|
2005-06-21 01:04:13 +02:00
|
|
|
return router_hex_digest_matches(router, nickname);
|
2004-07-03 01:40:03 +02:00
|
|
|
}
|
|
|
|
|
2004-07-01 03:16:59 +02:00
|
|
|
/** Return the router in our routerlist whose (case-insensitive)
|
|
|
|
* nickname or (case-sensitive) hexadecimal key digest is
|
|
|
|
* <b>nickname</b>. Return NULL if no such router is known.
|
2004-05-04 20:17:45 +02:00
|
|
|
*/
|
2010-09-29 06:38:32 +02:00
|
|
|
const routerinfo_t *
|
2005-10-05 00:23:31 +02:00
|
|
|
router_get_by_nickname(const char *nickname, int warn_if_unnamed)
|
2003-09-25 07:17:11 +02:00
|
|
|
{
|
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 1
|
|
|
|
const node_t *node = node_get_by_nickname(nickname, warn_if_unnamed);
|
|
|
|
if (node)
|
|
|
|
return node->ri;
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
#else
|
2005-09-13 17:32:03 +02:00
|
|
|
int maybedigest;
|
2004-07-01 03:16:59 +02:00
|
|
|
char digest[DIGEST_LEN];
|
2005-10-05 00:23:31 +02:00
|
|
|
routerinfo_t *best_match=NULL;
|
|
|
|
int n_matches = 0;
|
2007-10-08 23:21:50 +02:00
|
|
|
const char *named_digest = NULL;
|
2003-09-25 07:17:11 +02:00
|
|
|
|
2004-05-12 21:49:48 +02:00
|
|
|
tor_assert(nickname);
|
2004-06-02 22:00:57 +02:00
|
|
|
if (!routerlist)
|
|
|
|
return NULL;
|
2004-07-03 01:40:03 +02:00
|
|
|
if (nickname[0] == '$')
|
|
|
|
return router_get_by_hexdigest(nickname);
|
2006-09-29 06:51:28 +02:00
|
|
|
if (!strcasecmp(nickname, UNNAMED_ROUTER_NICKNAME))
|
|
|
|
return NULL;
|
2005-02-27 08:23:42 +01:00
|
|
|
if (server_mode(get_options()) &&
|
|
|
|
!strcasecmp(nickname, get_options()->Nickname))
|
|
|
|
return router_get_my_routerinfo();
|
2004-07-03 01:40:03 +02:00
|
|
|
|
2006-10-03 20:58:40 +02:00
|
|
|
maybedigest = (strlen(nickname) >= HEX_DIGEST_LEN) &&
|
2004-07-01 03:16:59 +02:00
|
|
|
(base16_decode(digest,DIGEST_LEN,nickname,HEX_DIGEST_LEN) == 0);
|
|
|
|
|
2007-10-08 23:21:50 +02:00
|
|
|
if ((named_digest = networkstatus_get_router_digest_by_nickname(nickname))) {
|
2007-06-12 00:19:40 +02:00
|
|
|
return rimap_get(routerlist->identity_map, named_digest);
|
2006-09-20 01:48:14 +02:00
|
|
|
}
|
2007-10-19 20:56:24 +02:00
|
|
|
if (networkstatus_nickname_is_unnamed(nickname))
|
|
|
|
return NULL;
|
2006-09-20 01:48:14 +02:00
|
|
|
|
2007-10-16 01:15:24 +02:00
|
|
|
/* If we reach this point, there's no canonical value for the nickname. */
|
|
|
|
|
2005-09-13 17:32:03 +02:00
|
|
|
SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, router,
|
|
|
|
{
|
2005-10-05 00:23:31 +02:00
|
|
|
if (!strcasecmp(router->nickname, nickname)) {
|
2006-09-20 01:48:14 +02:00
|
|
|
++n_matches;
|
|
|
|
if (n_matches <= 1 || router->is_running)
|
|
|
|
best_match = router;
|
2005-10-05 00:23:31 +02:00
|
|
|
} else if (maybedigest &&
|
2005-12-14 21:40:40 +01:00
|
|
|
!memcmp(digest, router->cache_info.identity_digest, DIGEST_LEN)
|
|
|
|
) {
|
2006-10-03 20:58:40 +02:00
|
|
|
if (router_hex_digest_matches(router, nickname))
|
|
|
|
return router;
|
2008-02-06 00:20:44 +01:00
|
|
|
/* If we reach this point, we have a ID=name syntax that matches the
|
|
|
|
* identity but not the name. That isn't an acceptable match. */
|
2005-10-05 00:23:31 +02:00
|
|
|
}
|
2005-09-13 17:32:03 +02:00
|
|
|
});
|
2004-07-01 03:16:59 +02:00
|
|
|
|
2005-10-05 00:23:31 +02:00
|
|
|
if (best_match) {
|
2005-10-11 03:57:28 +02:00
|
|
|
if (warn_if_unnamed && n_matches > 1) {
|
2005-10-05 00:23:31 +02:00
|
|
|
smartlist_t *fps = smartlist_create();
|
|
|
|
int any_unwarned = 0;
|
2010-09-29 07:35:08 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(routerlist->routers, routerinfo_t *, router) {
|
2007-10-09 17:27:15 +02:00
|
|
|
routerstatus_t *rs;
|
2005-10-05 00:23:31 +02:00
|
|
|
char *desc;
|
|
|
|
size_t dlen;
|
|
|
|
char fp[HEX_DIGEST_LEN+1];
|
|
|
|
if (strcasecmp(router->nickname, nickname))
|
|
|
|
continue;
|
2010-09-29 07:35:08 +02:00
|
|
|
rs = router_get_mutable_consensus_status_by_id(
|
2005-12-14 21:40:40 +01:00
|
|
|
router->cache_info.identity_digest);
|
2006-04-04 05:25:36 +02:00
|
|
|
if (rs && !rs->name_lookup_warned) {
|
2005-10-05 00:23:31 +02:00
|
|
|
rs->name_lookup_warned = 1;
|
|
|
|
any_unwarned = 1;
|
|
|
|
}
|
2005-12-14 21:40:40 +01:00
|
|
|
base16_encode(fp, sizeof(fp),
|
|
|
|
router->cache_info.identity_digest, DIGEST_LEN);
|
2005-10-05 00:23:31 +02:00
|
|
|
dlen = 32 + HEX_DIGEST_LEN + strlen(router->address);
|
|
|
|
desc = tor_malloc(dlen);
|
|
|
|
tor_snprintf(desc, dlen, "\"$%s\" for the one at %s:%d",
|
|
|
|
fp, router->address, router->or_port);
|
|
|
|
smartlist_add(fps, desc);
|
2010-09-29 07:35:08 +02:00
|
|
|
} SMARTLIST_FOREACH_END(router);
|
2005-10-05 00:23:31 +02:00
|
|
|
if (any_unwarned) {
|
|
|
|
char *alternatives = smartlist_join_strings(fps, "; ",0,NULL);
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_CONFIG,
|
|
|
|
"There are multiple matches for the nickname \"%s\","
|
2006-04-25 08:02:46 +02:00
|
|
|
" but none is listed as named by the directory authorities. "
|
2006-04-04 05:25:36 +02:00
|
|
|
"Choosing one arbitrarily. If you meant one in particular, "
|
2006-02-13 11:33:00 +01:00
|
|
|
"you should say %s.", nickname, alternatives);
|
2005-10-05 00:23:31 +02:00
|
|
|
tor_free(alternatives);
|
|
|
|
}
|
|
|
|
SMARTLIST_FOREACH(fps, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(fps);
|
|
|
|
} else if (warn_if_unnamed) {
|
2010-09-29 07:35:08 +02:00
|
|
|
routerstatus_t *rs = router_get_mutable_consensus_status_by_id(
|
2005-12-14 21:40:40 +01:00
|
|
|
best_match->cache_info.identity_digest);
|
2005-10-05 00:23:31 +02:00
|
|
|
if (rs && !rs->name_lookup_warned) {
|
|
|
|
char fp[HEX_DIGEST_LEN+1];
|
2005-12-14 21:40:40 +01:00
|
|
|
base16_encode(fp, sizeof(fp),
|
|
|
|
best_match->cache_info.identity_digest, DIGEST_LEN);
|
2007-03-04 19:58:38 +01:00
|
|
|
log_warn(LD_CONFIG, "You specified a server \"%s\" by name, but this "
|
|
|
|
"name is not registered, so it could be used by any server, "
|
|
|
|
"not just the one you meant. "
|
2005-12-10 10:36:26 +01:00
|
|
|
"To make sure you get the same server in the future, refer to "
|
|
|
|
"it by key, as \"$%s\".", nickname, fp);
|
2005-10-05 00:23:31 +02:00
|
|
|
rs->name_lookup_warned = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return best_match;
|
|
|
|
}
|
2004-07-01 03:16:59 +02:00
|
|
|
return NULL;
|
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
|
|
|
#endif
|
2004-07-01 03:16:59 +02:00
|
|
|
}
|
|
|
|
|
2005-11-19 07:57:44 +01:00
|
|
|
/** Try to find a routerinfo for <b>digest</b>. If we don't have one,
|
|
|
|
* return 1. If we do, ask tor_version_as_new_as() for the answer.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
router_digest_version_as_new_as(const char *digest, const char *cutoff)
|
|
|
|
{
|
2010-09-29 06:38:32 +02:00
|
|
|
const routerinfo_t *router = router_get_by_digest(digest);
|
2005-11-19 07:57:44 +01:00
|
|
|
if (!router)
|
|
|
|
return 1;
|
|
|
|
return tor_version_as_new_as(router->platform, cutoff);
|
|
|
|
}
|
|
|
|
|
2007-10-08 19:44:19 +02:00
|
|
|
/** Return true iff <b>digest</b> is the digest of the identity key of a
|
|
|
|
* trusted directory matching at least one bit of <b>type</b>. If <b>type</b>
|
|
|
|
* is zero, any authority is okay. */
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
2007-10-08 19:44:19 +02:00
|
|
|
router_digest_is_trusted_dir_type(const char *digest, authority_type_t type)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2004-10-12 17:55:20 +02:00
|
|
|
if (!trusted_dir_servers)
|
2004-07-20 22:09:59 +02:00
|
|
|
return 0;
|
2007-05-02 11:12:04 +02:00
|
|
|
if (authdir_mode(get_options()) && router_digest_is_me(digest))
|
2006-04-08 23:19:40 +02:00
|
|
|
return 1;
|
2004-10-12 17:55:20 +02:00
|
|
|
SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ent,
|
2007-10-08 19:44:19 +02:00
|
|
|
if (!memcmp(digest, ent->digest, DIGEST_LEN)) {
|
|
|
|
return (!type) || ((type & ent->type) != 0);
|
|
|
|
});
|
2004-07-20 22:09:59 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-10 22:13:52 +02:00
|
|
|
/** Return true iff <b>addr</b> is the address of one of our trusted
|
|
|
|
* directory authorities. */
|
|
|
|
int
|
|
|
|
router_addr_is_trusted_dir(uint32_t addr)
|
|
|
|
{
|
|
|
|
if (!trusted_dir_servers)
|
|
|
|
return 0;
|
|
|
|
SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ent,
|
|
|
|
if (ent->addr == addr)
|
|
|
|
return 1;
|
|
|
|
);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-04-27 12:26:09 +02:00
|
|
|
/** If hexdigest is correctly formed, base16_decode it into
|
|
|
|
* digest, which must have DIGEST_LEN space in it.
|
|
|
|
* Return 0 on success, -1 on failure.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
hexdigest_to_digest(const char *hexdigest, char *digest)
|
|
|
|
{
|
|
|
|
if (hexdigest[0]=='$')
|
|
|
|
++hexdigest;
|
|
|
|
if (strlen(hexdigest) < HEX_DIGEST_LEN ||
|
|
|
|
base16_decode(digest,DIGEST_LEN,hexdigest,HEX_DIGEST_LEN) < 0)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-07-01 03:16:59 +02:00
|
|
|
/** Return the router in our routerlist whose hexadecimal key digest
|
|
|
|
* is <b>hexdigest</b>. Return NULL if no such router is known. */
|
2010-09-29 06:38:32 +02:00
|
|
|
const routerinfo_t *
|
2005-06-11 20:52:12 +02:00
|
|
|
router_get_by_hexdigest(const char *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 (is_legal_nickname(hexdigest))
|
2004-07-01 03:16:59 +02:00
|
|
|
return NULL;
|
2006-10-03 20:58:40 +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
|
|
|
/* It's not a legal nickname, so it must be a hexdigest or nothing. */
|
|
|
|
return router_get_by_nickname(hexdigest, 1);
|
2004-07-01 03:16:59 +02:00
|
|
|
}
|
|
|
|
|
2010-09-29 06:38:32 +02:00
|
|
|
/** As router_get_by_digest,but return a pointer that you're allowed to
|
|
|
|
* 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 *
|
|
|
|
router_get_by_digest(const char *digest)
|
|
|
|
{
|
|
|
|
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) {
|
|
|
|
if (memcmp("router ", r, 7) && 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));
|
|
|
|
routerlist->routers = smartlist_create();
|
2005-10-27 02:34:39 +02:00
|
|
|
routerlist->old_routers = smartlist_create();
|
2007-06-12 00:19:40 +02:00
|
|
|
routerlist->identity_map = rimap_new();
|
|
|
|
routerlist->desc_digest_map = sdmap_new();
|
|
|
|
routerlist->desc_by_eid_map = sdmap_new();
|
|
|
|
routerlist->extra_info_map = eimap_new();
|
2007-08-31 17:08:37 +02:00
|
|
|
|
2007-09-27 22:46:24 +02:00
|
|
|
routerlist->desc_store.fname_base = "cached-descriptors";
|
|
|
|
routerlist->desc_store.fname_alt_base = "cached-routers";
|
2007-08-31 17:08:37 +02:00
|
|
|
routerlist->extrainfo_store.fname_base = "cached-extrainfo";
|
|
|
|
|
|
|
|
routerlist->desc_store.type = ROUTER_STORE;
|
|
|
|
routerlist->extrainfo_store.type = EXTRAINFO_STORE;
|
|
|
|
|
|
|
|
routerlist->desc_store.description = "router descriptors";
|
|
|
|
routerlist->extrainfo_store.description = "extra-info documents";
|
2005-10-18 19:43:54 +02:00
|
|
|
}
|
|
|
|
return routerlist;
|
2002-09-26 14:09:10 +02:00
|
|
|
}
|
|
|
|
|
2004-05-10 06:34:48 +02:00
|
|
|
/** Free all storage held by <b>router</b>. */
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
routerinfo_free(routerinfo_t *router)
|
2002-06-27 00:45:49 +02:00
|
|
|
{
|
2003-05-09 04:00:33 +02:00
|
|
|
if (!router)
|
2002-06-27 00:45:49 +02:00
|
|
|
return;
|
|
|
|
|
2006-01-12 19:04:17 +01:00
|
|
|
tor_free(router->cache_info.signed_descriptor_body);
|
2003-10-21 11:48:17 +02:00
|
|
|
tor_free(router->address);
|
|
|
|
tor_free(router->nickname);
|
2004-04-07 23:36:03 +02:00
|
|
|
tor_free(router->platform);
|
2005-05-06 10:53:23 +02:00
|
|
|
tor_free(router->contact_info);
|
2003-09-25 07:17:11 +02:00
|
|
|
if (router->onion_pkey)
|
|
|
|
crypto_free_pk_env(router->onion_pkey);
|
|
|
|
if (router->identity_pkey)
|
|
|
|
crypto_free_pk_env(router->identity_pkey);
|
2004-10-15 03:58:11 +02:00
|
|
|
if (router->declared_family) {
|
|
|
|
SMARTLIST_FOREACH(router->declared_family, char *, s, tor_free(s));
|
|
|
|
smartlist_free(router->declared_family);
|
|
|
|
}
|
2008-01-02 05:43:44 +01:00
|
|
|
addr_policy_list_free(router->exit_policy);
|
2007-05-19 20:12:31 +02:00
|
|
|
|
2008-02-06 00:20:44 +01:00
|
|
|
/* XXXX Remove if this turns out to affect performance. */
|
2007-05-19 20:12:31 +02:00
|
|
|
memset(router, 77, sizeof(routerinfo_t));
|
|
|
|
|
2004-09-29 08:52:36 +02:00
|
|
|
tor_free(router);
|
2002-09-24 12:43:57 +02:00
|
|
|
}
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/** Release all storage held by <b>extrainfo</b> */
|
2007-04-16 06:17:58 +02:00
|
|
|
void
|
|
|
|
extrainfo_free(extrainfo_t *extrainfo)
|
|
|
|
{
|
|
|
|
if (!extrainfo)
|
|
|
|
return;
|
|
|
|
tor_free(extrainfo->cache_info.signed_descriptor_body);
|
|
|
|
tor_free(extrainfo->pending_sig);
|
2007-05-19 20:12:31 +02:00
|
|
|
|
2008-02-06 00:20:44 +01:00
|
|
|
/* XXXX remove this if it turns out to slow us down. */
|
2007-05-29 19:31:13 +02:00
|
|
|
memset(extrainfo, 88, sizeof(extrainfo_t)); /* debug bad memory usage */
|
2007-04-16 06:17:58 +02:00
|
|
|
tor_free(extrainfo);
|
|
|
|
}
|
|
|
|
|
2005-12-27 06:26:03 +01:00
|
|
|
/** Release storage held by <b>sd</b>. */
|
2005-11-05 21:15:27 +01:00
|
|
|
static void
|
|
|
|
signed_descriptor_free(signed_descriptor_t *sd)
|
|
|
|
{
|
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
|
|
|
|
2008-02-06 00:20:44 +01:00
|
|
|
/* XXXX remove this once more bugs go away. */
|
2007-05-29 19:31:13 +02:00
|
|
|
memset(sd, 99, sizeof(signed_descriptor_t)); /* Debug bad mem usage */
|
2005-11-05 21:15:27 +01:00
|
|
|
tor_free(sd);
|
|
|
|
}
|
|
|
|
|
2005-12-27 06:26:03 +01:00
|
|
|
/** Extract a signed_descriptor_t from a routerinfo, and free the routerinfo.
|
|
|
|
*/
|
2005-11-05 21:15:27 +01:00
|
|
|
static signed_descriptor_t *
|
|
|
|
signed_descriptor_from_routerinfo(routerinfo_t *ri)
|
|
|
|
{
|
|
|
|
signed_descriptor_t *sd = tor_malloc_zero(sizeof(signed_descriptor_t));
|
|
|
|
memcpy(sd, &(ri->cache_info), sizeof(signed_descriptor_t));
|
2007-11-07 18:11:23 +01:00
|
|
|
sd->routerlist_index = -1;
|
2006-01-12 19:04:17 +01:00
|
|
|
ri->cache_info.signed_descriptor_body = NULL;
|
2005-11-05 21:15:27 +01:00
|
|
|
routerinfo_free(ri);
|
|
|
|
return sd;
|
|
|
|
}
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/** Helper: free the storage held by the extrainfo_t in <b>e</b>. */
|
2007-04-16 23:37:21 +02:00
|
|
|
static void
|
|
|
|
_extrainfo_free(void *e)
|
|
|
|
{
|
|
|
|
extrainfo_free(e);
|
|
|
|
}
|
|
|
|
|
2007-05-25 21:41:31 +02:00
|
|
|
/** Free all storage held by a routerlist <b>rl</b>. */
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
routerlist_free(routerlist_t *rl)
|
2003-05-06 19:38:16 +02:00
|
|
|
{
|
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);
|
|
|
|
eimap_free(rl->extra_info_map, _extrainfo_free);
|
2004-04-07 21:46:27 +02:00
|
|
|
SMARTLIST_FOREACH(rl->routers, routerinfo_t *, r,
|
|
|
|
routerinfo_free(r));
|
2005-11-05 21:15:27 +01:00
|
|
|
SMARTLIST_FOREACH(rl->old_routers, signed_descriptor_t *, sd,
|
|
|
|
signed_descriptor_free(sd));
|
2004-04-07 21:46:27 +02:00
|
|
|
smartlist_free(rl->routers);
|
2005-10-27 02:34:39 +02:00
|
|
|
smartlist_free(rl->old_routers);
|
2007-08-31 17:08:37 +02:00
|
|
|
if (routerlist->desc_store.mmap)
|
|
|
|
tor_munmap_file(routerlist->desc_store.mmap);
|
|
|
|
if (routerlist->extrainfo_store.mmap)
|
|
|
|
tor_munmap_file(routerlist->extrainfo_store.mmap);
|
2004-04-07 21:46:27 +02:00
|
|
|
tor_free(rl);
|
2006-08-31 20:46:46 +02:00
|
|
|
|
|
|
|
router_dir_info_changed();
|
2003-05-06 19:38:16 +02:00
|
|
|
}
|
|
|
|
|
2007-10-18 16:19:51 +02:00
|
|
|
/** Log information about how much memory is being used for routerlist,
|
|
|
|
* at log level <b>severity</b>. */
|
2005-11-23 07:00:58 +01:00
|
|
|
void
|
|
|
|
dump_routerlist_mem_usage(int severity)
|
|
|
|
{
|
|
|
|
uint64_t livedescs = 0;
|
|
|
|
uint64_t olddescs = 0;
|
|
|
|
if (!routerlist)
|
|
|
|
return;
|
|
|
|
SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, r,
|
|
|
|
livedescs += r->cache_info.signed_descriptor_len);
|
|
|
|
SMARTLIST_FOREACH(routerlist->old_routers, signed_descriptor_t *, sd,
|
2005-11-23 08:06:36 +01:00
|
|
|
olddescs += sd->signed_descriptor_len);
|
2005-11-23 07:00:58 +01:00
|
|
|
|
2008-03-29 05:27:34 +01:00
|
|
|
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
|
2005-11-05 21:15:27 +01:00
|
|
|
_routerlist_find_elt(smartlist_t *sl, void *ri, int idx)
|
2005-10-27 02:34:39 +02:00
|
|
|
{
|
2007-07-02 22:17:12 +02:00
|
|
|
if (idx < 0) {
|
2005-10-27 02:34:39 +02:00
|
|
|
idx = -1;
|
|
|
|
SMARTLIST_FOREACH(sl, routerinfo_t *, r,
|
|
|
|
if (r == ri) {
|
|
|
|
idx = r_sl_idx;
|
|
|
|
break;
|
|
|
|
});
|
2007-07-02 22:17:12 +02:00
|
|
|
} else {
|
|
|
|
tor_assert(idx < smartlist_len(sl));
|
|
|
|
tor_assert(smartlist_get(sl, idx) == ri);
|
|
|
|
};
|
2005-10-28 01:06:09 +02:00
|
|
|
return idx;
|
2005-10-27 02:34:39 +02:00
|
|
|
}
|
|
|
|
|
2005-10-18 19:43:54 +02:00
|
|
|
/** Insert an item <b>ri</b> into the routerlist <b>rl</b>, updating indices
|
2007-03-11 21:34:44 +01:00
|
|
|
* as needed. There must be no previous member of <b>rl</b> with the same
|
2007-03-11 21:52:07 +01:00
|
|
|
* identity digest as <b>ri</b>: If there is, call routerlist_replace
|
2007-03-11 21:34:44 +01:00
|
|
|
* instead.
|
|
|
|
*/
|
2005-10-18 19:43:54 +02:00
|
|
|
static void
|
|
|
|
routerlist_insert(routerlist_t *rl, routerinfo_t *ri)
|
|
|
|
{
|
2007-03-11 21:34:44 +01:00
|
|
|
routerinfo_t *ri_old;
|
2007-05-19 02:32:00 +02:00
|
|
|
{
|
2008-02-06 00:20:44 +01:00
|
|
|
/* XXXX Remove if this slows us down. */
|
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);
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_set(rl->desc_digest_map, ri->cache_info.signed_descriptor_digest,
|
|
|
|
&(ri->cache_info));
|
2007-05-18 23:19:53 +02:00
|
|
|
if (!tor_digest_is_zero(ri->cache_info.extra_info_digest))
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_set(rl->desc_by_eid_map, ri->cache_info.extra_info_digest,
|
|
|
|
&ri->cache_info);
|
2005-10-18 19:43:54 +02:00
|
|
|
smartlist_add(rl->routers, ri);
|
2007-11-07 18:11:23 +01:00
|
|
|
ri->cache_info.routerlist_index = smartlist_len(rl->routers) - 1;
|
2010-09-28 19:29:31 +02:00
|
|
|
nodelist_add_routerinfo(ri);
|
2006-08-15 05:54:09 +02:00
|
|
|
router_dir_info_changed();
|
2007-05-19 20:31:04 +02:00
|
|
|
#ifdef DEBUG_ROUTERLIST
|
|
|
|
routerlist_assert_ok(rl);
|
|
|
|
#endif
|
2005-10-18 19:43:54 +02:00
|
|
|
}
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/** Adds the extrainfo_t <b>ei</b> to the routerlist <b>rl</b>, if there is a
|
|
|
|
* corresponding router in rl-\>routers or rl-\>old_routers. Return true iff
|
|
|
|
* we actually inserted <b>ei</b>. Free <b>ei</b> if it isn't inserted. */
|
2007-04-30 21:48:39 +02:00
|
|
|
static int
|
2007-04-16 23:37:21 +02:00
|
|
|
extrainfo_insert(routerlist_t *rl, extrainfo_t *ei)
|
|
|
|
{
|
2007-04-30 21:48:39 +02:00
|
|
|
int r = 0;
|
2007-06-12 00:19:40 +02:00
|
|
|
routerinfo_t *ri = rimap_get(rl->identity_map,
|
|
|
|
ei->cache_info.identity_digest);
|
2007-10-21 02:08:35 +02:00
|
|
|
signed_descriptor_t *sd =
|
|
|
|
sdmap_get(rl->desc_by_eid_map, ei->cache_info.signed_descriptor_digest);
|
2007-04-16 23:37:21 +02:00
|
|
|
extrainfo_t *ei_tmp;
|
2007-05-19 22:08:44 +02:00
|
|
|
|
2007-05-20 22:16:45 +02:00
|
|
|
{
|
2008-02-06 00:20:44 +01:00
|
|
|
/* XXXX remove this code if it slows us down. */
|
2007-05-20 22:16:45 +02:00
|
|
|
extrainfo_t *ei_generated = router_get_my_extrainfo();
|
|
|
|
tor_assert(ei_generated != ei);
|
|
|
|
}
|
|
|
|
|
2007-05-19 22:08:44 +02:00
|
|
|
if (!ri) {
|
|
|
|
/* This router is unknown; we can't even verify the signature. Give up.*/
|
|
|
|
goto done;
|
|
|
|
}
|
2007-10-21 02:08:35 +02:00
|
|
|
if (routerinfo_incompatible_with_extrainfo(ri, ei, sd, NULL)) {
|
|
|
|
goto done;
|
2007-04-16 23:37:21 +02:00
|
|
|
}
|
|
|
|
|
2007-05-19 22:08:44 +02:00
|
|
|
/* Okay, if we make it here, we definitely have a router corresponding to
|
|
|
|
* this extrainfo. */
|
|
|
|
|
2007-06-12 00:19:40 +02:00
|
|
|
ei_tmp = eimap_set(rl->extra_info_map,
|
|
|
|
ei->cache_info.signed_descriptor_digest,
|
|
|
|
ei);
|
2007-04-30 21:48:39 +02:00
|
|
|
r = 1;
|
2007-05-22 03:51:53 +02:00
|
|
|
if (ei_tmp) {
|
2007-08-31 17:08:37 +02:00
|
|
|
rl->extrainfo_store.bytes_dropped +=
|
2007-05-22 03:51:53 +02:00
|
|
|
ei_tmp->cache_info.signed_descriptor_len;
|
2007-04-16 23:37:21 +02:00
|
|
|
extrainfo_free(ei_tmp);
|
2007-05-22 03:51:53 +02:00
|
|
|
}
|
2007-04-30 16:26:38 +02:00
|
|
|
|
|
|
|
done:
|
2007-05-19 22:08:44 +02:00
|
|
|
if (r == 0)
|
|
|
|
extrainfo_free(ei);
|
|
|
|
|
2007-05-19 20:31:04 +02:00
|
|
|
#ifdef DEBUG_ROUTERLIST
|
|
|
|
routerlist_assert_ok(rl);
|
|
|
|
#endif
|
2007-04-30 21:48:39 +02:00
|
|
|
return r;
|
2007-04-16 23:37:21 +02:00
|
|
|
}
|
|
|
|
|
2007-12-01 05:58:53 +01:00
|
|
|
#define should_cache_old_descriptors() \
|
|
|
|
directory_caches_dir_info(get_options())
|
2007-10-09 22:44:47 +02:00
|
|
|
|
2007-02-26 06:33:17 +01:00
|
|
|
/** If we're a directory cache and routerlist <b>rl</b> doesn't have
|
|
|
|
* a copy of router <b>ri</b> yet, add it to the list of old (not
|
|
|
|
* recommended but still served) descriptors. Else free it. */
|
2005-10-27 02:34:39 +02:00
|
|
|
static void
|
|
|
|
routerlist_insert_old(routerlist_t *rl, routerinfo_t *ri)
|
|
|
|
{
|
2007-05-19 02:32:00 +02:00
|
|
|
{
|
2008-02-06 00:20:44 +01:00
|
|
|
/* XXXX remove this code if it slows us down. */
|
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;
|
|
|
|
|
2007-03-11 21:34:44 +01:00
|
|
|
routerinfo_t *ri_tmp;
|
2007-04-16 23:37:21 +02:00
|
|
|
extrainfo_t *ei_tmp;
|
2007-05-19 02:32:00 +02:00
|
|
|
{
|
2008-02-06 00:20:44 +01:00
|
|
|
/* XXXX Remove this if it turns out to slow us down. */
|
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);
|
|
|
|
|
2010-09-28 19:29:31 +02:00
|
|
|
nodelist_remove_routerinfo(ri_old);
|
|
|
|
nodelist_add_routerinfo(ri_new);
|
|
|
|
|
2006-08-15 05:54:09 +02:00
|
|
|
router_dir_info_changed();
|
2005-10-19 05:02:28 +02:00
|
|
|
if (idx >= 0) {
|
|
|
|
smartlist_set(rl->routers, idx, ri_new);
|
2007-11-07 18:11:23 +01:00
|
|
|
ri_old->cache_info.routerlist_index = -1;
|
|
|
|
ri_new->cache_info.routerlist_index = idx;
|
2007-07-12 19:37:47 +02:00
|
|
|
/* Check that ri_old is not in rl->routers anymore: */
|
2007-07-02 22:04:26 +02:00
|
|
|
tor_assert( _routerlist_find_elt(rl->routers, ri_old, -1) == -1 );
|
2005-10-19 05:02:28 +02:00
|
|
|
} else {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_BUG, "Appending entry from routerlist_replace.");
|
2005-10-28 01:06:09 +02:00
|
|
|
routerlist_insert(rl, ri_new);
|
|
|
|
return;
|
2005-10-19 05:02:28 +02:00
|
|
|
}
|
2005-12-14 21:40:40 +01:00
|
|
|
if (memcmp(ri_old->cache_info.identity_digest,
|
|
|
|
ri_new->cache_info.identity_digest, DIGEST_LEN)) {
|
2005-10-19 05:02:28 +02:00
|
|
|
/* digests don't match; digestmap_set won't replace */
|
2007-06-12 00:19:40 +02:00
|
|
|
rimap_remove(rl->identity_map, ri_old->cache_info.identity_digest);
|
2005-10-19 05:02:28 +02:00
|
|
|
}
|
2007-06-12 00:19:40 +02:00
|
|
|
ri_tmp = rimap_set(rl->identity_map,
|
|
|
|
ri_new->cache_info.identity_digest, ri_new);
|
2007-03-11 22:10:54 +01:00
|
|
|
tor_assert(!ri_tmp || ri_tmp == ri_old);
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_set(rl->desc_digest_map,
|
|
|
|
ri_new->cache_info.signed_descriptor_digest,
|
|
|
|
&(ri_new->cache_info));
|
2005-10-27 02:34:39 +02:00
|
|
|
|
2007-06-02 16:24:23 +02:00
|
|
|
if (!tor_digest_is_zero(ri_new->cache_info.extra_info_digest)) {
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_set(rl->desc_by_eid_map, ri_new->cache_info.extra_info_digest,
|
|
|
|
&ri_new->cache_info);
|
2007-06-02 16:24:23 +02:00
|
|
|
}
|
2007-05-19 20:31:04 +02:00
|
|
|
|
2007-10-09 22:44:47 +02:00
|
|
|
if (should_cache_old_descriptors() &&
|
2007-03-21 16:37:30 +01:00
|
|
|
ri_old->purpose == ROUTER_PURPOSE_GENERAL) {
|
2005-11-05 21:15:27 +01:00
|
|
|
signed_descriptor_t *sd = signed_descriptor_from_routerinfo(ri_old);
|
|
|
|
smartlist_add(rl->old_routers, sd);
|
2007-11-07 18:11:23 +01:00
|
|
|
sd->routerlist_index = smartlist_len(rl->old_routers)-1;
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_set(rl->desc_digest_map, sd->signed_descriptor_digest, sd);
|
2007-05-19 20:31:04 +02:00
|
|
|
if (!tor_digest_is_zero(sd->extra_info_digest))
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_set(rl->desc_by_eid_map, sd->extra_info_digest, sd);
|
2005-10-27 02:34:39 +02:00
|
|
|
} else {
|
2005-11-05 21:15:27 +01:00
|
|
|
if (memcmp(ri_old->cache_info.signed_descriptor_digest,
|
|
|
|
ri_new->cache_info.signed_descriptor_digest,
|
2005-10-27 02:34:39 +02:00
|
|
|
DIGEST_LEN)) {
|
|
|
|
/* digests don't match; digestmap_set didn't replace */
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_remove(rl->desc_digest_map,
|
|
|
|
ri_old->cache_info.signed_descriptor_digest);
|
2007-05-19 20:12:31 +02:00
|
|
|
}
|
2007-04-16 23:37:21 +02:00
|
|
|
|
2007-06-12 00:19:40 +02:00
|
|
|
ei_tmp = eimap_remove(rl->extra_info_map,
|
|
|
|
ri_old->cache_info.extra_info_digest);
|
2007-05-19 20:12:31 +02:00
|
|
|
if (ei_tmp) {
|
2007-08-31 17:08:37 +02:00
|
|
|
rl->extrainfo_store.bytes_dropped +=
|
2007-05-19 20:12:31 +02:00
|
|
|
ei_tmp->cache_info.signed_descriptor_len;
|
|
|
|
extrainfo_free(ei_tmp);
|
2005-10-27 02:34:39 +02:00
|
|
|
}
|
2007-06-02 16:24:23 +02:00
|
|
|
if (!tor_digest_is_zero(ri_old->cache_info.extra_info_digest)) {
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_remove(rl->desc_by_eid_map,
|
|
|
|
ri_old->cache_info.extra_info_digest);
|
2007-06-02 16:24:23 +02:00
|
|
|
}
|
2007-10-22 18:32:01 +02:00
|
|
|
rl->desc_store.bytes_dropped += ri_old->cache_info.signed_descriptor_len;
|
2005-10-27 02:34:39 +02:00
|
|
|
routerinfo_free(ri_old);
|
|
|
|
}
|
2007-05-19 20:31:04 +02:00
|
|
|
#ifdef DEBUG_ROUTERLIST
|
|
|
|
routerlist_assert_ok(rl);
|
|
|
|
#endif
|
2005-10-19 05:02:28 +02:00
|
|
|
}
|
|
|
|
|
2007-12-19 04:55:40 +01:00
|
|
|
/** Extract the descriptor <b>sd</b> from old_routerlist, and re-parse
|
|
|
|
* it as a fresh routerinfo_t. */
|
2007-11-07 16:19:53 +01:00
|
|
|
static routerinfo_t *
|
2007-11-07 18:11:23 +01:00
|
|
|
routerlist_reparse_old(routerlist_t *rl, signed_descriptor_t *sd)
|
2007-11-07 16:19:53 +01:00
|
|
|
{
|
|
|
|
routerinfo_t *ri;
|
|
|
|
const char *body;
|
|
|
|
|
2007-12-19 04:55:40 +01:00
|
|
|
body = signed_descriptor_get_annotations(sd);
|
2007-11-07 16:19:53 +01:00
|
|
|
|
2007-12-19 04:55:40 +01:00
|
|
|
ri = router_parse_entry_from_string(body,
|
|
|
|
body+sd->signed_descriptor_len+sd->annotations_len,
|
|
|
|
0, 1, NULL);
|
2007-11-07 16:19:53 +01:00
|
|
|
if (!ri)
|
|
|
|
return NULL;
|
|
|
|
memcpy(&ri->cache_info, sd, sizeof(signed_descriptor_t));
|
2007-11-07 18:11:23 +01:00
|
|
|
sd->signed_descriptor_body = NULL; /* Steal reference. */
|
2007-11-07 18:41:14 +01:00
|
|
|
ri->cache_info.routerlist_index = -1;
|
2007-11-07 16:19:53 +01:00
|
|
|
|
2007-11-07 18:11:23 +01:00
|
|
|
routerlist_remove_old(rl, sd, -1);
|
2007-11-07 16:19:53 +01:00
|
|
|
|
|
|
|
return ri;
|
|
|
|
}
|
|
|
|
|
2005-11-01 04:48:51 +01:00
|
|
|
/** Free all memory held by the routerlist module. */
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
2005-09-15 01:42:06 +02:00
|
|
|
routerlist_free_all(void)
|
2005-02-11 00:18:39 +01:00
|
|
|
{
|
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;
|
|
|
|
}
|
2005-09-15 01:42:06 +02:00
|
|
|
if (trusted_dir_servers) {
|
|
|
|
SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ds,
|
2005-10-05 07:03:52 +02:00
|
|
|
trusted_dir_server_free(ds));
|
2005-09-15 01:42:06 +02:00
|
|
|
smartlist_free(trusted_dir_servers);
|
|
|
|
trusted_dir_servers = NULL;
|
|
|
|
}
|
2008-03-26 22:16:56 +01:00
|
|
|
if (trusted_dir_certs) {
|
|
|
|
DIGESTMAP_FOREACH(trusted_dir_certs, key, cert_list_t *, cl) {
|
|
|
|
SMARTLIST_FOREACH(cl->certs, authority_cert_t *, cert,
|
|
|
|
authority_cert_free(cert));
|
|
|
|
smartlist_free(cl->certs);
|
|
|
|
tor_free(cl);
|
|
|
|
} DIGESTMAP_FOREACH_END;
|
|
|
|
digestmap_free(trusted_dir_certs, NULL);
|
|
|
|
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)
|
|
|
|
warned_nicknames = smartlist_create();
|
|
|
|
SMARTLIST_FOREACH(warned_nicknames, char *, cp, tor_free(cp));
|
|
|
|
smartlist_clear(warned_nicknames); /* now the list is empty. */
|
|
|
|
|
2007-10-08 23:21:50 +02:00
|
|
|
networkstatus_reset_warnings();
|
2005-10-05 04:20:46 +02:00
|
|
|
}
|
|
|
|
|
2006-03-18 02:24:04 +01:00
|
|
|
/** Mark the router with ID <b>digest</b> as running or non-running
|
|
|
|
* in our routerlist. */
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
2006-03-18 02:24:04 +01:00
|
|
|
router_set_status(const char *digest, int up)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
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
|
|
|
node_t *node;
|
2007-10-09 17:27:15 +02:00
|
|
|
routerstatus_t *status;
|
2004-07-03 01:40:03 +02:00
|
|
|
tor_assert(digest);
|
2004-10-12 17:55:20 +02:00
|
|
|
|
|
|
|
SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, d,
|
|
|
|
if (!memcmp(d->digest, digest, DIGEST_LEN))
|
2006-03-18 02:24:04 +01:00
|
|
|
d->is_running = up);
|
2004-10-12 17:55:20 +02:00
|
|
|
|
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
|
|
|
node = node_get_mutable_by_id(digest);
|
|
|
|
if (node) {
|
|
|
|
#if 0
|
|
|
|
char buf[MAX_VERBOSE_NICKNAME_LEN+1];
|
|
|
|
node_get_verbose_nickname(node,buf);
|
|
|
|
log_debug(LD_DIR,"Marking router %s as %s.",
|
|
|
|
buf, up ? "up" : "down");
|
|
|
|
#endif
|
|
|
|
if (!up && node_is_me(node) && !we_are_hibernating())
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_NET, "We just marked ourself as down. Are your external "
|
|
|
|
"addresses reachable?");
|
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
|
|
|
node->is_running = up;
|
2005-12-14 23:05:10 +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
|
|
|
|
|
|
|
/*XXXX NM don't change routerstatus's is_running. */
|
2010-09-29 07:35:08 +02:00
|
|
|
status = router_get_mutable_consensus_status_by_id(digest);
|
2007-10-09 17:27:15 +02:00
|
|
|
if (status && status->is_running != up) {
|
|
|
|
status->is_running = up;
|
2006-10-20 23:04:39 +02:00
|
|
|
control_event_networkstatus_changed_single(status);
|
2005-12-14 23:05:10 +01:00
|
|
|
}
|
2006-08-15 05:54:09 +02:00
|
|
|
router_dir_info_changed();
|
2002-09-26 15:17:14 +02:00
|
|
|
}
|
|
|
|
|
2004-05-17 22:31:01 +02:00
|
|
|
/** Add <b>router</b> to the routerlist, if we don't already have it. Replace
|
2005-09-15 08:15:31 +02:00
|
|
|
* older entries (if any) with the same key. Note: Callers should not hold
|
2005-05-14 07:01:41 +02:00
|
|
|
* their pointers to <b>router</b> if this function fails; <b>router</b>
|
2007-07-16 02:11:03 +02:00
|
|
|
* will either be inserted into the routerlist or freed. Similarly, even
|
|
|
|
* if this call succeeds, they should not hold their pointers to
|
|
|
|
* <b>router</b> after subsequent calls with other routerinfo's -- they
|
|
|
|
* might cause the original routerinfo to get freed.
|
2005-04-03 00:02:13 +02:00
|
|
|
*
|
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;
|
2007-12-19 00:45:24 +01:00
|
|
|
int authdir = authdir_mode_handles_descs(get_options(), router->purpose);
|
2006-03-19 02:21:59 +01:00
|
|
|
int authdir_believes_valid = 0;
|
2006-09-20 01:18:30 +02:00
|
|
|
routerinfo_t *old_router;
|
2008-02-05 22:39:29 +01:00
|
|
|
networkstatus_t *consensus = networkstatus_get_latest_consensus();
|
2007-10-16 01:15:24 +02:00
|
|
|
const smartlist_t *networkstatus_v2_list = networkstatus_get_v2_list();
|
|
|
|
int in_consensus = 0;
|
2004-08-07 05:38:07 +02:00
|
|
|
|
2005-08-27 01:12:13 +02:00
|
|
|
tor_assert(msg);
|
|
|
|
|
2005-10-18 22:13:09 +02:00
|
|
|
if (!routerlist)
|
|
|
|
router_get_routerlist();
|
2005-08-26 23:12:34 +02:00
|
|
|
|
2006-03-17 06:50:41 +01:00
|
|
|
id_digest = router->cache_info.identity_digest;
|
2005-10-27 02:34:39 +02:00
|
|
|
|
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-08-20 07:39:03 +02:00
|
|
|
tor_assert(old_router);
|
|
|
|
if (! (routerinfo_is_a_configured_bridge(router) &&
|
|
|
|
(router->purpose == ROUTER_PURPOSE_BRIDGE ||
|
|
|
|
old_router->purpose != ROUTER_PURPOSE_BRIDGE))) {
|
2010-07-30 21:39:08 +02:00
|
|
|
log_info(LD_DIR,
|
|
|
|
"Dropping descriptor that we already have for router '%s'",
|
|
|
|
router->nickname);
|
|
|
|
*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,
|
|
|
|
"Received a no-longer-recognized descriptor for router '%s'",
|
2006-02-13 11:33:00 +01:00
|
|
|
router->nickname);
|
2006-01-12 04:43:39 +01:00
|
|
|
*msg = "Router descriptor is not referenced by any network-status.";
|
2007-02-22 08:41:10 +01:00
|
|
|
|
|
|
|
/* Only journal this desc if we'll be serving it. */
|
2007-10-09 22:44:47 +02:00
|
|
|
if (!from_cache && should_cache_old_descriptors())
|
2007-08-31 17:08:37 +02:00
|
|
|
signed_desc_append_to_journal(&router->cache_info,
|
2007-10-22 18:32:01 +02:00
|
|
|
&routerlist->desc_store);
|
2007-02-22 08:41:10 +01:00
|
|
|
routerlist_insert_old(routerlist, router);
|
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
|
|
|
SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
|
2006-03-27 07:07:57 +02:00
|
|
|
{
|
|
|
|
routerstatus_t *rs =
|
2010-09-29 07:35:08 +02:00
|
|
|
networkstatus_v2_find_mutable_entry(ns, id_digest);
|
2006-03-27 07:07:57 +02:00
|
|
|
if (rs && !memcmp(rs->descriptor_digest,
|
|
|
|
router->cache_info.signed_descriptor_digest,
|
|
|
|
DIGEST_LEN))
|
|
|
|
rs->need_to_mirror = 0;
|
|
|
|
});
|
2007-10-16 01:15:24 +02:00
|
|
|
if (consensus) {
|
2010-09-29 07:35:08 +02:00
|
|
|
routerstatus_t *rs = networkstatus_vote_find_mutable_entry(
|
|
|
|
consensus, id_digest);
|
2007-10-16 01:15:24 +02:00
|
|
|
if (rs && !memcmp(rs->descriptor_digest,
|
|
|
|
router->cache_info.signed_descriptor_digest,
|
|
|
|
DIGEST_LEN)) {
|
|
|
|
in_consensus = 1;
|
|
|
|
rs->need_to_mirror = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-16 16:25:14 +02:00
|
|
|
if (router->purpose == ROUTER_PURPOSE_GENERAL &&
|
2007-12-19 00:45:24 +01:00
|
|
|
consensus && !in_consensus && !authdir) {
|
2007-10-16 16:25:14 +02:00
|
|
|
/* If it's a general router not listed in the consensus, then don't
|
|
|
|
* consider replacing the latest router with it. */
|
2007-10-16 01:15:24 +02:00
|
|
|
if (!from_cache && should_cache_old_descriptors())
|
|
|
|
signed_desc_append_to_journal(&router->cache_info,
|
2007-10-22 18:32:01 +02:00
|
|
|
&routerlist->desc_store);
|
2007-10-16 01:15:24 +02:00
|
|
|
routerlist_insert_old(routerlist, router);
|
2008-01-17 06:25:21 +01:00
|
|
|
*msg = "Skipping router descriptor: not in consensus.";
|
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
|
|
|
|
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. */
|
2010-04-21 09:12:14 +02:00
|
|
|
log_debug(LD_DIR, "Not-new descriptor for router '%s'",
|
2006-09-20 01:18:30 +02:00
|
|
|
router->nickname);
|
|
|
|
/* Only journal this desc if we'll be serving it. */
|
2007-10-09 22:44:47 +02:00
|
|
|
if (!from_cache && should_cache_old_descriptors())
|
2007-08-31 17:08:37 +02:00
|
|
|
signed_desc_append_to_journal(&router->cache_info,
|
2007-10-22 18:32:01 +02:00
|
|
|
&routerlist->desc_store);
|
2006-09-20 01:18:30 +02:00
|
|
|
routerlist_insert_old(routerlist, router);
|
|
|
|
*msg = "Router descriptor was not new.";
|
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. */
|
2006-09-20 01:18:30 +02:00
|
|
|
log_debug(LD_DIR, "Replacing entry for router '%s/%s' [%s]",
|
|
|
|
router->nickname, old_router->nickname,
|
|
|
|
hex_str(id_digest,DIGEST_LEN));
|
2010-09-15 04:19:00 +02:00
|
|
|
if (routers_have_same_or_addr(router, old_router)) {
|
2007-10-24 05:34:03 +02:00
|
|
|
/* these carry over when the address and orport are unchanged. */
|
2006-09-20 01:18:30 +02:00
|
|
|
router->last_reachable = old_router->last_reachable;
|
|
|
|
router->testing_since = old_router->testing_since;
|
|
|
|
}
|
2007-07-06 20:40:39 +02:00
|
|
|
routerlist_replace(routerlist, old_router, router);
|
2006-09-20 01:18:30 +02:00
|
|
|
if (!from_cache) {
|
2007-08-31 17:08:37 +02:00
|
|
|
signed_desc_append_to_journal(&router->cache_info,
|
2007-10-22 18:32:01 +02:00
|
|
|
&routerlist->desc_store);
|
2006-09-20 01:18:30 +02:00
|
|
|
}
|
|
|
|
directory_set_dirty();
|
2007-10-24 05:34:03 +02:00
|
|
|
*msg = authdir_believes_valid ? "Valid server updated" :
|
2006-09-20 01:18:30 +02:00
|
|
|
("Invalid server updated. (This dirserver is marking your "
|
|
|
|
"server as unapproved.)");
|
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;
|
2009-01-04 20:47:21 +01:00
|
|
|
/*XXXX022 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
|
|
|
|
_compare_old_routers_by_identity(const void **_a, const void **_b)
|
|
|
|
{
|
|
|
|
int i;
|
2005-11-05 21:15:27 +01:00
|
|
|
const signed_descriptor_t *r1 = *_a, *r2 = *_b;
|
2005-10-27 02:34:39 +02:00
|
|
|
if ((i = memcmp(r1->identity_digest, r2->identity_digest, DIGEST_LEN)))
|
|
|
|
return i;
|
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
|
|
|
|
_compare_duration_idx(const void *_d1, const void *_d2)
|
|
|
|
{
|
|
|
|
const struct duration_idx_t *d1 = _d1;
|
|
|
|
const struct duration_idx_t *d2 = _d2;
|
|
|
|
return d1->duration - d2->duration;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** The range <b>lo</b> through <b>hi</b> inclusive of routerlist->old_routers
|
|
|
|
* must contain routerinfo_t with the same identity and with publication time
|
|
|
|
* in ascending order. Remove members from this range until there are no more
|
2006-03-27 19:29:53 +02:00
|
|
|
* than max_descriptors_per_router() remaining. Start by removing the oldest
|
2005-10-27 02:34:39 +02:00
|
|
|
* members from before <b>cutoff</b>, then remove members which were current
|
|
|
|
* for the lowest amount of time. The order of members of old_routers at
|
|
|
|
* indices <b>lo</b> or higher may be changed.
|
|
|
|
*/
|
|
|
|
static void
|
2007-10-19 04:15:47 +02:00
|
|
|
routerlist_remove_old_cached_routers_with_id(time_t now,
|
|
|
|
time_t cutoff, int lo, int hi,
|
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);
|
2005-10-27 02:34:39 +02:00
|
|
|
tor_assert(!memcmp(ident, r->identity_digest, DIGEST_LEN));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* Check whether we need to do anything at all. */
|
2007-07-18 22:46:14 +02:00
|
|
|
{
|
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 ||
|
2008-04-08 19:06:41 +02:00
|
|
|
(retain && digestset_isin(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.
|
|
|
|
**/
|
|
|
|
qsort(lifespans, n, sizeof(struct duration_idx_t), _compare_duration_idx);
|
|
|
|
for (i = 0; i < n && n_rmv < n_extra; ++i) {
|
2005-12-27 06:26:03 +01:00
|
|
|
if (!must_keep[lifespans[i].idx-lo] && !lifespans[i].old) {
|
2005-10-27 02:34:39 +02:00
|
|
|
rmv[lifespans[i].idx-lo] = 1;
|
|
|
|
++n_rmv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-17 00:16:13 +02:00
|
|
|
i = hi;
|
|
|
|
do {
|
2005-10-27 02:34:39 +02:00
|
|
|
if (rmv[i-lo])
|
|
|
|
routerlist_remove_old(routerlist, smartlist_get(lst, i), i);
|
2007-05-17 00:16:13 +02:00
|
|
|
} while (--i >= lo);
|
2005-12-27 06:26:03 +01:00
|
|
|
tor_free(must_keep);
|
2005-10-27 02:34:39 +02:00
|
|
|
tor_free(rmv);
|
|
|
|
tor_free(lifespans);
|
|
|
|
}
|
|
|
|
|
2005-11-11 18:16:24 +01:00
|
|
|
/** Deactivate any routers from the routerlist that are more than
|
2006-10-20 01:04:56 +02:00
|
|
|
* ROUTER_MAX_AGE seconds old and not recommended by any networkstatuses;
|
|
|
|
* remove old routers from the list of cached routers if we have too many.
|
2005-11-01 18:34:17 +01:00
|
|
|
*/
|
2005-10-27 02:34:39 +02:00
|
|
|
void
|
2005-11-01 18:34:17 +01:00
|
|
|
routerlist_remove_old_routers(void)
|
2005-10-27 02:34:39 +02:00
|
|
|
{
|
|
|
|
int i, hi=-1;
|
|
|
|
const char *cur_id = NULL;
|
2006-02-05 03:07:28 +01:00
|
|
|
time_t now = time(NULL);
|
|
|
|
time_t cutoff;
|
2005-11-01 18:34:17 +01:00
|
|
|
routerinfo_t *router;
|
2006-01-10 05:57:12 +01:00
|
|
|
signed_descriptor_t *sd;
|
2008-04-08 19:06:41 +02:00
|
|
|
digestset_t *retain;
|
2007-12-04 19:35:03 +01:00
|
|
|
int caches = directory_caches_dir_info(get_options());
|
2008-02-05 22:39:29 +01:00
|
|
|
const networkstatus_t *consensus = networkstatus_get_latest_consensus();
|
2007-10-16 01:15:24 +02:00
|
|
|
const smartlist_t *networkstatus_v2_list = networkstatus_get_v2_list();
|
2009-01-04 20:47:12 +01:00
|
|
|
int have_enough_v2;
|
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);
|
|
|
|
if (caches && networkstatus_v2_list) {
|
|
|
|
/* If we care about v2 statuses, we'll retain at most as many as are
|
|
|
|
listed any of the v2 statues. This will be at least the length of
|
2009-05-27 23:55:51 +02:00
|
|
|
the largest v2 networkstatus, and in the worst case, this set will be
|
2008-12-26 18:35:36 +01:00
|
|
|
equal to the sum of the lengths of all v2 consensuses. Take the
|
|
|
|
worst case.
|
|
|
|
*/
|
|
|
|
SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
|
|
|
|
n_max_retain += smartlist_len(ns->entries));
|
|
|
|
}
|
|
|
|
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-12 19:13:09 +02:00
|
|
|
/* Build a list of all the descriptors that _anybody_ lists. */
|
2008-12-29 15:06:47 +01:00
|
|
|
if (caches && networkstatus_v2_list) {
|
2007-10-16 01:15:24 +02:00
|
|
|
SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
|
2006-10-20 01:04:56 +02:00
|
|
|
{
|
2007-02-26 19:01:23 +01:00
|
|
|
/* XXXX The inner loop here gets pretty expensive, and actually shows up
|
|
|
|
* on some profiles. It may be the reason digestmap_set shows up in
|
|
|
|
* profiles too. If instead we kept a per-descriptor digest count of
|
|
|
|
* how many networkstatuses recommended each descriptor, and changed
|
|
|
|
* that only when the networkstatuses changed, that would be a speed
|
|
|
|
* improvement, possibly 1-4% if it also removes digestmap_set from the
|
|
|
|
* profile. Not worth it for 0.1.2.x, though. The new directory
|
|
|
|
* system will obsolete this whole thing in 0.2.0.x. */
|
2006-10-20 01:04:56 +02:00
|
|
|
SMARTLIST_FOREACH(ns->entries, routerstatus_t *, rs,
|
|
|
|
if (rs->published_on >= cutoff)
|
2008-04-08 19:06:41 +02:00
|
|
|
digestset_add(retain, rs->descriptor_digest));
|
2006-10-20 01:04:56 +02:00
|
|
|
});
|
2007-10-16 01:15:24 +02:00
|
|
|
}
|
2005-12-27 06:26:03 +01:00
|
|
|
|
2007-10-16 01:15:24 +02:00
|
|
|
/* Retain anything listed in the consensus. */
|
|
|
|
if (consensus) {
|
|
|
|
SMARTLIST_FOREACH(consensus->routerstatus_list, routerstatus_t *, rs,
|
2007-10-12 19:13:09 +02:00
|
|
|
if (rs->published_on >= cutoff)
|
2008-04-08 19:06:41 +02:00
|
|
|
digestset_add(retain, rs->descriptor_digest));
|
2007-10-12 19:13:09 +02:00
|
|
|
}
|
|
|
|
|
2009-01-04 20:47:12 +01:00
|
|
|
/* If we have a consensus, and nearly as many v2 networkstatuses as we want,
|
|
|
|
* we should consider pruning current routers that are too old and that
|
|
|
|
* nobody recommends. (If we don't have a consensus or enough v2
|
|
|
|
* networkstatuses, then we should get more before we decide to kill
|
|
|
|
* routers.) */
|
|
|
|
/* we set this to true iff we don't care about v2 info, or we have enough. */
|
|
|
|
have_enough_v2 = !caches ||
|
|
|
|
(networkstatus_v2_list &&
|
|
|
|
smartlist_len(networkstatus_v2_list) > get_n_v2_authorities() / 2);
|
|
|
|
|
|
|
|
if (have_enough_v2 && 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 &&
|
2008-04-08 19:06:41 +02:00
|
|
|
!digestset_isin(retain,
|
|
|
|
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,
|
|
|
|
"Forgetting obsolete (too old) routerinfo for router '%s'",
|
|
|
|
router->nickname);
|
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 &&
|
2008-04-08 19:06:41 +02:00
|
|
|
!digestset_isin(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. */
|
2005-10-27 02:34:39 +02:00
|
|
|
smartlist_sort(routerlist->old_routers, _compare_old_routers_by_identity);
|
2007-11-07 18:29:16 +01:00
|
|
|
/* Fix indices. */
|
2007-11-07 18:30:08 +01:00
|
|
|
for (i = 0; i < smartlist_len(routerlist->old_routers); ++i) {
|
2007-11-07 18:29:16 +01:00
|
|
|
signed_descriptor_t *r = smartlist_get(routerlist->old_routers, i);
|
|
|
|
r->routerlist_index = i;
|
|
|
|
}
|
2005-10-27 02:34:39 +02:00
|
|
|
|
|
|
|
/* Iterate through the list from back to front, so when we remove descriptors
|
|
|
|
* we don't mess up groups we haven't gotten to. */
|
|
|
|
for (i = smartlist_len(routerlist->old_routers)-1; i >= 0; --i) {
|
2005-11-05 21:15:27 +01:00
|
|
|
signed_descriptor_t *r = smartlist_get(routerlist->old_routers, i);
|
2005-10-27 02:34:39 +02:00
|
|
|
if (!cur_id) {
|
|
|
|
cur_id = r->identity_digest;
|
|
|
|
hi = i;
|
|
|
|
}
|
|
|
|
if (memcmp(cur_id, r->identity_digest, DIGEST_LEN)) {
|
2007-10-19 04:15:47 +02:00
|
|
|
routerlist_remove_old_cached_routers_with_id(now,
|
|
|
|
cutoff, i+1, hi, retain);
|
2005-11-23 08:28:54 +01:00
|
|
|
cur_id = r->identity_digest;
|
2005-10-27 02:34:39 +02:00
|
|
|
hi = i;
|
2004-05-20 07:10:30 +02:00
|
|
|
}
|
|
|
|
}
|
2005-11-01 18:34:17 +01:00
|
|
|
if (hi>=0)
|
2007-10-19 04:15:47 +02:00
|
|
|
routerlist_remove_old_cached_routers_with_id(now, cutoff, 0, hi, retain);
|
2007-10-16 20:19:02 +02:00
|
|
|
//routerlist_assert_ok(routerlist);
|
2006-01-11 04:58:07 +01:00
|
|
|
|
|
|
|
done:
|
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;
|
|
|
|
|
2005-10-05 03:27:08 +02:00
|
|
|
lst = smartlist_create();
|
|
|
|
smartlist_add(lst, ri);
|
2007-10-16 01:15:24 +02:00
|
|
|
routers_update_status_from_consensus_networkstatus(lst, 0);
|
2005-09-14 23:09:25 +02:00
|
|
|
|
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
|
|
|
{
|
|
|
|
smartlist_t *routers = smartlist_create(), *changed = smartlist_create();
|
|
|
|
char fp[HEX_DIGEST_LEN+1];
|
|
|
|
const char *msg;
|
2006-06-22 09:01:54 +02:00
|
|
|
int from_cache = (saved_location != SAVED_NOWHERE);
|
2007-09-26 18:19:44 +02:00
|
|
|
int allow_annotations = (saved_location != SAVED_NOWHERE);
|
2008-01-09 00:31:25 +01:00
|
|
|
int any_changed = 0;
|
2005-09-15 07:19:38 +02:00
|
|
|
|
2007-09-26 18:19:44 +02:00
|
|
|
router_parse_list_from_string(&s, eos, routers, saved_location, 0,
|
2007-09-27 22:46:30 +02:00
|
|
|
allow_annotations, prepend_annotations);
|
2005-09-15 07:19:38 +02:00
|
|
|
|
2007-10-16 01:15:24 +02:00
|
|
|
routers_update_status_from_consensus_networkstatus(routers, !from_cache);
|
2005-12-27 06:26:03 +01:00
|
|
|
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_DIR, "%d elements to add", smartlist_len(routers));
|
2005-09-15 07:19:38 +02:00
|
|
|
|
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);
|
2005-09-15 07:19:38 +02:00
|
|
|
if (smartlist_string_isin(requested_fingerprints, fp)) {
|
|
|
|
smartlist_string_remove(requested_fingerprints, fp);
|
|
|
|
} else {
|
|
|
|
char *requested =
|
|
|
|
smartlist_join_strings(requested_fingerprints," ",0,NULL);
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR,
|
|
|
|
"We received a router descriptor with a fingerprint (%s) "
|
|
|
|
"that we never requested. (We asked for: %s.) Dropping.",
|
|
|
|
fp, requested);
|
2005-09-15 07:19:38 +02:00
|
|
|
tor_free(requested);
|
|
|
|
routerinfo_free(ri);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
smartlist_t *extrainfo_list = smartlist_create();
|
|
|
|
const char *msg;
|
|
|
|
int from_cache = (saved_location != SAVED_NOWHERE);
|
|
|
|
|
2007-09-27 18:08:10 +02:00
|
|
|
router_parse_list_from_string(&s, eos, extrainfo_list, saved_location, 1, 0,
|
|
|
|
NULL);
|
2007-04-30 21:48:39 +02:00
|
|
|
|
|
|
|
log_info(LD_DIR, "%d elements to add", smartlist_len(extrainfo_list));
|
|
|
|
|
2007-05-18 23:19:19 +02:00
|
|
|
SMARTLIST_FOREACH(extrainfo_list, extrainfo_t *, ei, {
|
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
|
|
|
}
|
|
|
|
});
|
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-12-04 19:35:03 +01:00
|
|
|
int caches = directory_caches_dir_info(get_options());
|
2007-10-16 01:15:24 +02:00
|
|
|
const smartlist_t *networkstatus_v2_list = networkstatus_get_v2_list();
|
2007-10-08 23:21:50 +02:00
|
|
|
|
2007-10-16 01:15:24 +02:00
|
|
|
if (consensus) {
|
|
|
|
rs = networkstatus_vote_find_entry(consensus, desc->identity_digest);
|
|
|
|
if (rs && !memcmp(rs->descriptor_digest,
|
|
|
|
desc->signed_descriptor_digest, DIGEST_LEN))
|
2007-10-08 23:21:50 +02:00
|
|
|
return 1;
|
2007-10-16 01:15:24 +02:00
|
|
|
}
|
2007-12-04 19:35:03 +01:00
|
|
|
if (caches && networkstatus_v2_list) {
|
2007-10-16 01:15:24 +02:00
|
|
|
SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
|
|
|
|
{
|
|
|
|
if (!(rs = networkstatus_v2_find_entry(ns, desc->identity_digest)))
|
|
|
|
continue;
|
|
|
|
if (!memcmp(rs->descriptor_digest,
|
|
|
|
desc->signed_descriptor_digest, DIGEST_LEN))
|
|
|
|
return 1;
|
|
|
|
});
|
|
|
|
}
|
2007-10-08 23:21:50 +02:00
|
|
|
return 0;
|
2005-09-12 08:56:42 +02:00
|
|
|
}
|
2005-09-13 17:32:03 +02:00
|
|
|
|
2007-10-08 23:21:50 +02:00
|
|
|
/** Clear all our timeouts for fetching v2 and v3 directory stuff, and then
|
|
|
|
* give it all a try again. */
|
|
|
|
void
|
|
|
|
routerlist_retry_directory_downloads(time_t now)
|
2005-09-12 08:56:42 +02:00
|
|
|
{
|
2007-10-08 23:21:50 +02:00
|
|
|
router_reset_status_download_failures();
|
|
|
|
router_reset_descriptor_download_failures();
|
|
|
|
update_networkstatus_downloads(now);
|
|
|
|
update_router_descriptor_downloads(now);
|
2010-05-11 23:20:33 +02:00
|
|
|
update_microdesc_downloads(now);
|
2005-09-12 08:56:42 +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 1 if all running sufficiently-stable routers we can use will reject
|
2007-10-08 23:21:50 +02:00
|
|
|
* addr:port, return 0 if any might accept it. */
|
|
|
|
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_exit_policy_all_nodes_reject(uint32_t addr, uint16_t port,
|
|
|
|
int need_uptime)
|
|
|
|
{ /* XXXX MOVE */
|
2007-10-08 23:21:50 +02:00
|
|
|
addr_policy_result_t r;
|
2006-01-08 22:26:33 +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_BEGIN(nodelist_get_list(), const node_t *, node) {
|
|
|
|
if (node->is_running &&
|
|
|
|
!node_is_unreliable(node, need_uptime, 0, 0)) {
|
|
|
|
|
|
|
|
r = compare_addr_to_node_policy(addr, port, node);
|
|
|
|
|
2007-10-08 23:21:50 +02:00
|
|
|
if (r != ADDR_POLICY_REJECTED && r != ADDR_POLICY_PROBABLY_REJECTED)
|
|
|
|
return 0; /* this one could be ok. good enough. */
|
|
|
|
}
|
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-10-08 23:21:50 +02:00
|
|
|
return 1; /* all will reject. */
|
2006-01-08 22:26:33 +01:00
|
|
|
}
|
|
|
|
|
2007-10-08 23:21:50 +02:00
|
|
|
/** Return true iff <b>router</b> does not permit exit streams.
|
2005-09-07 18:42:53 +02:00
|
|
|
*/
|
|
|
|
int
|
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
|
|
|
|
2009-04-16 10:25:36 +02:00
|
|
|
/** Add to the list of authoritative directory servers one at
|
2007-10-08 23:21:50 +02:00
|
|
|
* <b>address</b>:<b>port</b>, with identity key <b>digest</b>. If
|
2009-04-16 10:25:36 +02:00
|
|
|
* <b>address</b> is NULL, add ourself. Return the new trusted directory
|
|
|
|
* server entry on success or NULL if we couldn't add it. */
|
2008-02-04 17:58:50 +01:00
|
|
|
trusted_dir_server_t *
|
2007-10-08 23:21:50 +02:00
|
|
|
add_trusted_dir_server(const char *nickname, const char *address,
|
|
|
|
uint16_t dir_port, uint16_t or_port,
|
|
|
|
const char *digest, const char *v3_auth_digest,
|
|
|
|
authority_type_t type)
|
|
|
|
{
|
|
|
|
trusted_dir_server_t *ent;
|
|
|
|
uint32_t a;
|
|
|
|
char *hostname = NULL;
|
|
|
|
size_t dlen;
|
|
|
|
if (!trusted_dir_servers)
|
|
|
|
trusted_dir_servers = smartlist_create();
|
2005-09-08 22:36:40 +02:00
|
|
|
|
2007-10-08 23:21:50 +02:00
|
|
|
if (!address) { /* The address is us; we should guess. */
|
|
|
|
if (resolve_my_address(LOG_WARN, get_options(), &a, &hostname) < 0) {
|
|
|
|
log_warn(LD_CONFIG,
|
|
|
|
"Couldn't find a suitable address when adding ourself as a "
|
|
|
|
"trusted directory server.");
|
2008-02-04 17:58:50 +01:00
|
|
|
return NULL;
|
2005-09-12 08:56:42 +02:00
|
|
|
}
|
2007-10-08 23:21:50 +02:00
|
|
|
} else {
|
|
|
|
if (tor_lookup_hostname(address, &a)) {
|
|
|
|
log_warn(LD_CONFIG,
|
|
|
|
"Unable to lookup address for directory server at '%s'",
|
|
|
|
address);
|
2008-02-04 17:58:50 +01:00
|
|
|
return NULL;
|
2006-01-08 22:26:33 +01:00
|
|
|
}
|
2007-10-08 23:21:50 +02:00
|
|
|
hostname = tor_strdup(address);
|
2006-01-08 22:26:33 +01:00
|
|
|
}
|
|
|
|
|
2007-10-08 23:21:50 +02:00
|
|
|
ent = tor_malloc_zero(sizeof(trusted_dir_server_t));
|
|
|
|
ent->nickname = nickname ? tor_strdup(nickname) : NULL;
|
|
|
|
ent->address = hostname;
|
|
|
|
ent->addr = a;
|
|
|
|
ent->dir_port = dir_port;
|
|
|
|
ent->or_port = or_port;
|
|
|
|
ent->is_running = 1;
|
|
|
|
ent->type = type;
|
|
|
|
memcpy(ent->digest, digest, DIGEST_LEN);
|
|
|
|
if (v3_auth_digest && (type & V3_AUTHORITY))
|
|
|
|
memcpy(ent->v3_identity_digest, v3_auth_digest, DIGEST_LEN);
|
2005-09-07 18:42:53 +02:00
|
|
|
|
2007-10-08 23:21:50 +02:00
|
|
|
dlen = 64 + strlen(hostname) + (nickname?strlen(nickname):0);
|
|
|
|
ent->description = tor_malloc(dlen);
|
|
|
|
if (nickname)
|
|
|
|
tor_snprintf(ent->description, dlen, "directory server \"%s\" at %s:%d",
|
|
|
|
nickname, hostname, (int)dir_port);
|
|
|
|
else
|
|
|
|
tor_snprintf(ent->description, dlen, "directory server at %s:%d",
|
|
|
|
hostname, (int)dir_port);
|
2007-05-14 10:09:12 +02:00
|
|
|
|
2007-10-09 17:27:15 +02:00
|
|
|
ent->fake_status.addr = ent->addr;
|
|
|
|
memcpy(ent->fake_status.identity_digest, digest, DIGEST_LEN);
|
2007-10-08 23:21:50 +02:00
|
|
|
if (nickname)
|
2007-10-09 17:27:15 +02:00
|
|
|
strlcpy(ent->fake_status.nickname, nickname,
|
|
|
|
sizeof(ent->fake_status.nickname));
|
2007-10-08 23:21:50 +02:00
|
|
|
else
|
2007-10-09 17:27:15 +02:00
|
|
|
ent->fake_status.nickname[0] = '\0';
|
|
|
|
ent->fake_status.dir_port = ent->dir_port;
|
|
|
|
ent->fake_status.or_port = ent->or_port;
|
2005-09-07 18:42:53 +02:00
|
|
|
|
2007-10-08 23:21:50 +02:00
|
|
|
if (ent->or_port)
|
2007-10-09 17:27:15 +02:00
|
|
|
ent->fake_status.version_supports_begindir = 1;
|
2009-12-19 21:56:49 +01:00
|
|
|
|
2008-04-24 17:43:25 +02:00
|
|
|
ent->fake_status.version_supports_conditional_consensus = 1;
|
2005-12-27 06:26:03 +01:00
|
|
|
|
2007-10-08 23:21:50 +02:00
|
|
|
smartlist_add(trusted_dir_servers, ent);
|
2006-08-15 05:54:09 +02:00
|
|
|
router_dir_info_changed();
|
2008-02-04 17:58:50 +01:00
|
|
|
return ent;
|
2007-10-08 23:21:50 +02:00
|
|
|
}
|
2005-09-14 23:09:25 +02:00
|
|
|
|
2007-10-22 19:31:26 +02:00
|
|
|
/** Free storage held in <b>cert</b>. */
|
|
|
|
void
|
|
|
|
authority_cert_free(authority_cert_t *cert)
|
|
|
|
{
|
|
|
|
if (!cert)
|
|
|
|
return;
|
|
|
|
|
|
|
|
tor_free(cert->cache_info.signed_descriptor_body);
|
2009-12-12 08:07:59 +01:00
|
|
|
crypto_free_pk_env(cert->signing_key);
|
|
|
|
crypto_free_pk_env(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
|
|
|
|
trusted_dir_server_free(trusted_dir_server_t *ds)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2007-10-08 23:21:50 +02:00
|
|
|
/** Remove all members from the list of trusted dir servers. */
|
2005-09-12 08:56:42 +02:00
|
|
|
void
|
2007-10-08 23:21:50 +02:00
|
|
|
clear_trusted_dir_servers(void)
|
2004-10-12 17:55:20 +02:00
|
|
|
{
|
|
|
|
if (trusted_dir_servers) {
|
|
|
|
SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ent,
|
2005-10-05 07:03:52 +02:00
|
|
|
trusted_dir_server_free(ent));
|
2004-10-12 17:55:20 +02:00
|
|
|
smartlist_clear(trusted_dir_servers);
|
|
|
|
} else {
|
|
|
|
trusted_dir_servers = smartlist_create();
|
|
|
|
}
|
2006-08-15 05:54:09 +02:00
|
|
|
router_dir_info_changed();
|
2004-10-12 17:55:20 +02:00
|
|
|
}
|
2005-06-09 21:03:31 +02:00
|
|
|
|
2006-03-15 01:10:13 +01:00
|
|
|
/** Return 1 if any trusted dir server supports v1 directories,
|
|
|
|
* else return 0. */
|
|
|
|
int
|
2006-04-10 23:29:29 +02:00
|
|
|
any_trusted_dir_is_v1_authority(void)
|
2006-03-15 01:10:13 +01:00
|
|
|
{
|
|
|
|
if (trusted_dir_servers)
|
2007-05-09 06:15:46 +02:00
|
|
|
return get_n_authorities(V1_AUTHORITY) > 0;
|
|
|
|
|
2006-03-15 01:10:13 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-18 16:19:51 +02:00
|
|
|
/** For every current directory connection whose purpose is <b>purpose</b>,
|
|
|
|
* and where the resource being downloaded begins with <b>prefix</b>, split
|
|
|
|
* rest of the resource into base16 fingerprints, decode them, and set the
|
2010-05-11 23:20:33 +02:00
|
|
|
* corresponding elements of <b>result</b> to a nonzero value.
|
|
|
|
* DOCDOC purpose==microdesc
|
|
|
|
*/
|
2005-12-27 06:26:03 +01:00
|
|
|
static void
|
2007-10-09 22:44:45 +02:00
|
|
|
list_pending_downloads(digestmap_t *result,
|
|
|
|
int purpose, const char *prefix)
|
2005-09-12 08:56:42 +02:00
|
|
|
{
|
2007-10-09 22:44:45 +02:00
|
|
|
const size_t p_len = strlen(prefix);
|
2005-12-27 06:26:03 +01:00
|
|
|
smartlist_t *tmp = smartlist_create();
|
2007-05-22 17:49:14 +02:00
|
|
|
smartlist_t *conns = get_connection_array();
|
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/");
|
|
|
|
}
|
|
|
|
|
2010-05-11 23:20:33 +02:00
|
|
|
/** DOCDOC */
|
|
|
|
/*XXXX NM should use digest256, if one comes into being. */
|
|
|
|
void
|
|
|
|
list_pending_microdesc_downloads(digestmap_t *result)
|
|
|
|
{
|
|
|
|
list_pending_downloads(result, DIR_PURPOSE_FETCH_MICRODESC, "d/");
|
|
|
|
}
|
|
|
|
|
|
|
|
/** 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,
|
2005-12-27 06:26:03 +01:00
|
|
|
0, /* not private */
|
2007-10-28 21:30:21 +01:00
|
|
|
resource, NULL, 0, 0);
|
2005-12-27 06:26:03 +01:00
|
|
|
} else {
|
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
|
|
|
|
2006-04-08 08:56:38 +02:00
|
|
|
/** Return 0 if this routerstatus is obsolete, too new, isn't
|
|
|
|
* running, or otherwise not a descriptor that we would make any
|
|
|
|
* use of even if we had it. Else return 1. */
|
2006-09-17 22:12:10 +02:00
|
|
|
static INLINE int
|
2006-08-15 05:54:09 +02:00
|
|
|
client_would_use_router(routerstatus_t *rs, time_t now, or_options_t *options)
|
2006-04-08 08:56:38 +02:00
|
|
|
{
|
2006-08-15 05:54:09 +02:00
|
|
|
if (!rs->is_running && !options->FetchUselessDescriptors) {
|
2006-04-08 23:35:17 +02:00
|
|
|
/* If we had this router descriptor, we wouldn't even bother using it.
|
|
|
|
* But, if we want to have a complete list, fetch it anyway. */
|
2006-04-08 08:56:38 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2008-06-20 19:03:13 +02:00
|
|
|
if (rs->published_on + options->TestingEstimatedDescriptorPropagationTime
|
|
|
|
> now) {
|
2006-04-08 08:56:38 +02:00
|
|
|
/* Most caches probably don't have this descriptor yet. */
|
|
|
|
return 0;
|
|
|
|
}
|
2007-10-16 20:19:02 +02:00
|
|
|
if (rs->published_on + OLD_ROUTER_DESC_MAX_AGE < now) {
|
|
|
|
/* We'd drop it immediately for being too old. */
|
|
|
|
return 0;
|
|
|
|
}
|
2006-04-08 08:56:38 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-10-16 01:15:24 +02:00
|
|
|
/** Max amount of hashes to download per request.
|
|
|
|
* Since squid does not like URLs >= 4096 bytes we limit it to 96.
|
|
|
|
* 4096 - strlen(http://255.255.255.255/tor/server/d/.z) == 4058
|
|
|
|
* 4058/41 (40 for the hash and 1 for the + that separates them) => 98
|
|
|
|
* So use 96 because it's a nice number.
|
2005-12-27 06:26:03 +01:00
|
|
|
*/
|
2006-02-03 16:17:48 +01:00
|
|
|
#define MAX_DL_PER_REQUEST 96
|
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
|
|
|
|
* want more, or until MAX_CLIENT_INTERVAL_WITHOUT_REQUEST has
|
|
|
|
* passed. */
|
2005-09-30 22:04:55 +02:00
|
|
|
#define MAX_DL_TO_DELAY 16
|
2007-10-16 01:15:24 +02:00
|
|
|
/** When directory clients have only a few servers to request, they batch
|
|
|
|
* them until they have more, or until this amount of time has passed. */
|
2006-03-12 23:48:18 +01:00
|
|
|
#define MAX_CLIENT_INTERVAL_WITHOUT_REQUEST (10*60)
|
2007-10-08 21:56:57 +02:00
|
|
|
|
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;
|
|
|
|
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 {
|
|
|
|
should_delay = (last_routerdesc_download_attempted +
|
|
|
|
MAX_CLIENT_INTERVAL_WITHOUT_REQUEST) > now;
|
2007-11-16 08:23:25 +01:00
|
|
|
if (!should_delay && n_downloadable) {
|
2007-10-08 21:56:57 +02:00
|
|
|
if (last_routerdesc_download_attempted) {
|
|
|
|
log_info(LD_DIR,
|
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,
|
2007-10-08 21:56:57 +02:00
|
|
|
(int)(now-last_routerdesc_download_attempted));
|
|
|
|
} 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
|
|
|
}
|
2008-02-16 00:39:04 +01:00
|
|
|
/* XXX should we consider having even the dir mirrors delay
|
2008-02-15 20:20:53 +01:00
|
|
|
* a little bit, so we don't load the authorities as much? -RD
|
|
|
|
* I don't think so. If we do, clients that want those descriptors may
|
|
|
|
* not actually find them if the caches haven't got them yet. -NM
|
|
|
|
*/
|
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,
|
|
|
|
"Launching %d request%s for %d router%s, %d at a time",
|
2010-09-15 04:32:36 +02:00
|
|
|
CEIL_DIV(n_downloadable, n_per_request),
|
2007-05-17 00:16:13 +02:00
|
|
|
req_plural, n_downloadable, rtr_plural, n_per_request);
|
2006-06-21 06:57:12 +02:00
|
|
|
smartlist_sort_digests(downloadable);
|
2005-10-04 22:18:26 +02:00
|
|
|
for (i=0; i < n_downloadable; i += n_per_request) {
|
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
|
|
|
}
|
2005-10-05 07:03:52 +02:00
|
|
|
last_routerdesc_download_attempted = now;
|
2005-09-18 04:24:42 +02:00
|
|
|
}
|
2005-09-15 07:19:38 +02:00
|
|
|
}
|
|
|
|
|
2006-03-17 06:50:41 +01:00
|
|
|
/** Launch downloads for router status as needed, using the strategy used by
|
2007-12-04 19:35:03 +01:00
|
|
|
* authorities and caches: based on the v2 networkstatuses we have, download
|
|
|
|
* every descriptor we don't have but would serve, from a random authority
|
|
|
|
* that lists it. */
|
2005-12-27 06:26:03 +01:00
|
|
|
static void
|
2007-12-04 19:35:03 +01:00
|
|
|
update_router_descriptor_cache_downloads_v2(time_t now)
|
2005-12-27 06:26:03 +01:00
|
|
|
{
|
|
|
|
smartlist_t **downloadable; /* For each authority, what can we dl from it? */
|
|
|
|
smartlist_t **download_from; /* ... and, what will we dl from it? */
|
|
|
|
digestmap_t *map; /* Which descs are in progress, or assigned? */
|
|
|
|
int i, j, n;
|
|
|
|
int n_download;
|
|
|
|
or_options_t *options = get_options();
|
2007-10-16 01:15:24 +02:00
|
|
|
const smartlist_t *networkstatus_v2_list = networkstatus_get_v2_list();
|
2005-12-27 06:26:03 +01:00
|
|
|
|
2007-12-21 07:08:00 +01:00
|
|
|
if (! directory_fetches_dir_info_early(options)) {
|
2007-12-04 19:35:03 +01:00
|
|
|
log_warn(LD_BUG, "Called update_router_descriptor_cache_downloads_v2() "
|
2006-05-30 08:11:36 +02:00
|
|
|
"on a non-dir-mirror?");
|
2005-12-27 06:26:03 +01:00
|
|
|
}
|
|
|
|
|
2007-10-16 01:15:24 +02:00
|
|
|
if (!networkstatus_v2_list || !smartlist_len(networkstatus_v2_list))
|
2005-12-27 06:26:03 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
map = digestmap_new();
|
2007-10-16 01:15:24 +02:00
|
|
|
n = smartlist_len(networkstatus_v2_list);
|
2005-12-27 06:26:03 +01:00
|
|
|
|
|
|
|
downloadable = tor_malloc_zero(sizeof(smartlist_t*) * n);
|
|
|
|
download_from = tor_malloc_zero(sizeof(smartlist_t*) * n);
|
|
|
|
|
|
|
|
/* Set map[d]=1 for the digest of every descriptor that we are currently
|
|
|
|
* downloading. */
|
2007-05-18 23:19:19 +02:00
|
|
|
list_pending_descriptor_downloads(map, 0);
|
2005-12-27 06:26:03 +01:00
|
|
|
|
|
|
|
/* For the digest of every descriptor that we don't have, and that we aren't
|
|
|
|
* downloading, add d to downloadable[i] if the i'th networkstatus knows
|
|
|
|
* about that descriptor, and we haven't already failed to get that
|
|
|
|
* descriptor from the corresponding authority.
|
|
|
|
*/
|
|
|
|
n_download = 0;
|
2007-10-16 01:15:24 +02:00
|
|
|
SMARTLIST_FOREACH(networkstatus_v2_list, networkstatus_v2_t *, ns,
|
2005-12-27 06:26:03 +01:00
|
|
|
{
|
2007-05-13 01:22:27 +02:00
|
|
|
trusted_dir_server_t *ds;
|
2007-02-22 08:41:10 +01:00
|
|
|
smartlist_t *dl;
|
2007-02-24 00:11:08 +01:00
|
|
|
dl = downloadable[ns_sl_idx] = smartlist_create();
|
|
|
|
download_from[ns_sl_idx] = smartlist_create();
|
2007-02-26 06:33:17 +01:00
|
|
|
if (ns->published_on + MAX_NETWORKSTATUS_AGE+10*60 < now) {
|
2007-02-22 08:41:10 +01:00
|
|
|
/* Don't download if the networkstatus is almost ancient. */
|
2007-02-26 06:33:17 +01:00
|
|
|
/* Actually, I suspect what's happening here is that we ask
|
|
|
|
* for the descriptor when we have a given networkstatus,
|
|
|
|
* and then we get a newer networkstatus, and then we receive
|
|
|
|
* the descriptor. Having a networkstatus actually expire is
|
|
|
|
* probably a rare event, and we'll probably be happiest if
|
|
|
|
* we take this clause out. -RD */
|
2007-02-22 08:41:10 +01:00
|
|
|
continue;
|
|
|
|
}
|
2007-05-13 01:22:27 +02:00
|
|
|
|
|
|
|
/* Don't try dirservers that we think are down -- we might have
|
|
|
|
* just tried them and just marked them as down. */
|
|
|
|
ds = router_get_trusteddirserver_by_digest(ns->identity_digest);
|
|
|
|
if (ds && !ds->is_running)
|
|
|
|
continue;
|
|
|
|
|
2005-12-27 06:26:03 +01:00
|
|
|
SMARTLIST_FOREACH(ns->entries, routerstatus_t * , rs,
|
|
|
|
{
|
|
|
|
if (!rs->need_to_mirror)
|
|
|
|
continue;
|
|
|
|
if (router_get_by_descriptor_digest(rs->descriptor_digest)) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_BUG,
|
2007-05-13 11:25:06 +02:00
|
|
|
"We have a router descriptor, but need_to_mirror=1.");
|
2005-12-27 06:26:03 +01:00
|
|
|
rs->need_to_mirror = 0;
|
|
|
|
continue;
|
|
|
|
}
|
2007-05-02 11:12:04 +02:00
|
|
|
if (authdir_mode(options) && dirserv_would_reject_router(rs)) {
|
2005-12-27 06:26:03 +01:00
|
|
|
rs->need_to_mirror = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (digestmap_get(map, rs->descriptor_digest)) {
|
|
|
|
/* We're downloading it already. */
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
/* We could download it from this guy. */
|
|
|
|
smartlist_add(dl, rs->descriptor_digest);
|
|
|
|
++n_download;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
/* At random, assign descriptors to authorities such that:
|
|
|
|
* - if d is a member of some downloadable[x], d is a member of some
|
|
|
|
* download_from[y]. (Everything we want to download, we try to download
|
|
|
|
* from somebody.)
|
2006-03-22 07:22:12 +01:00
|
|
|
* - If d is a member of download_from[y], d is a member of downloadable[y].
|
2005-12-27 06:26:03 +01:00
|
|
|
* (We only try to download descriptors from authorities who claim to have
|
|
|
|
* them.)
|
|
|
|
* - No d is a member of download_from[x] and download_from[y] s.t. x != y.
|
|
|
|
* (We don't try to download anything from two authorities concurrently.)
|
|
|
|
*/
|
|
|
|
while (n_download) {
|
|
|
|
int which_ns = crypto_rand_int(n);
|
|
|
|
smartlist_t *dl = downloadable[which_ns];
|
|
|
|
int idx;
|
|
|
|
char *d;
|
|
|
|
if (!smartlist_len(dl))
|
|
|
|
continue;
|
|
|
|
idx = crypto_rand_int(smartlist_len(dl));
|
|
|
|
d = smartlist_get(dl, idx);
|
|
|
|
if (! digestmap_get(map, d)) {
|
|
|
|
smartlist_add(download_from[which_ns], d);
|
|
|
|
digestmap_set(map, d, (void*) 1);
|
|
|
|
}
|
|
|
|
smartlist_del(dl, idx);
|
|
|
|
--n_download;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now, we can actually launch our requests. */
|
|
|
|
for (i=0; i<n; ++i) {
|
2007-10-16 01:15:24 +02:00
|
|
|
networkstatus_v2_t *ns = smartlist_get(networkstatus_v2_list, i);
|
2005-12-27 06:26:03 +01:00
|
|
|
trusted_dir_server_t *ds =
|
|
|
|
router_get_trusteddirserver_by_digest(ns->identity_digest);
|
2005-12-29 21:25:53 +01:00
|
|
|
smartlist_t *dl = download_from[i];
|
2008-12-11 20:12:55 +01:00
|
|
|
int pds_flags = PDS_RETRY_IF_NO_SERVERS;
|
|
|
|
if (! authdir_mode_any_nonhidserv(options))
|
2008-12-18 17:11:24 +01:00
|
|
|
pds_flags |= PDS_NO_EXISTING_SERVERDESC_FETCH; /* XXXX ignored*/
|
2008-12-11 20:12:55 +01:00
|
|
|
|
2005-12-27 06:26:03 +01:00
|
|
|
if (!ds) {
|
2010-01-19 20:25:15 +01:00
|
|
|
log_info(LD_DIR, "Networkstatus with no corresponding authority!");
|
2005-12-27 06:26:03 +01:00
|
|
|
continue;
|
|
|
|
}
|
2005-12-27 07:05:54 +01:00
|
|
|
if (! smartlist_len(dl))
|
|
|
|
continue;
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_DIR, "Requesting %d descriptors from authority \"%s\"",
|
|
|
|
smartlist_len(dl), ds->nickname);
|
2005-12-27 06:26:03 +01:00
|
|
|
for (j=0; j < smartlist_len(dl); j += MAX_DL_PER_REQUEST) {
|
2007-10-09 17:27:15 +02:00
|
|
|
initiate_descriptor_downloads(&(ds->fake_status),
|
2007-05-18 23:19:19 +02:00
|
|
|
DIR_PURPOSE_FETCH_SERVERDESC, dl, j,
|
2008-12-11 20:12:55 +01:00
|
|
|
j+MAX_DL_PER_REQUEST, pds_flags);
|
2005-12-27 06:26:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i=0; i<n; ++i) {
|
|
|
|
smartlist_free(download_from[i]);
|
|
|
|
smartlist_free(downloadable[i]);
|
|
|
|
}
|
|
|
|
tor_free(download_from);
|
|
|
|
tor_free(downloadable);
|
|
|
|
digestmap_free(map,NULL);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
or_options_t *options = get_options();
|
|
|
|
digestmap_t *map = NULL;
|
2007-11-07 16:19:53 +01:00
|
|
|
smartlist_t *no_longer_old = smartlist_create();
|
2007-10-08 21:56:57 +02:00
|
|
|
smartlist_t *downloadable = smartlist_create();
|
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 */
|
|
|
|
trusted_dir_server_t *ds;
|
|
|
|
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;
|
2007-11-06 21:51:45 +01:00
|
|
|
if (!(ri = router_get_by_digest(rs->identity_digest)) ||
|
|
|
|
memcmp(ri->cache_info.signed_descriptor_digest,
|
|
|
|
sd->signed_descriptor_digest, DIGEST_LEN)) {
|
2007-11-12 16:46:38 +01:00
|
|
|
/* We have a descriptor with this digest, but either there is no
|
|
|
|
* entry in routerlist with the same ID (!ri), or there is one,
|
|
|
|
* but the identity digest differs (memcmp).
|
|
|
|
*/
|
2007-11-07 16:19:53 +01:00
|
|
|
smartlist_add(no_longer_old, sd);
|
|
|
|
++n_in_oldrouters; /* We have it in old_routers. */
|
2007-11-06 21:51:45 +01:00
|
|
|
}
|
2007-10-08 21:56:57 +02:00
|
|
|
continue; /* We have it already. */
|
2007-10-16 20:47:32 +02:00
|
|
|
}
|
2008-01-18 05:47:03 +01:00
|
|
|
if (digestmap_get(map, rs->descriptor_digest)) {
|
|
|
|
++n_inprogress;
|
|
|
|
continue; /* We have an in-progress download. */
|
|
|
|
}
|
2007-10-16 01:15:24 +02:00
|
|
|
if (!download_status_is_ready(&rs->dl_status, now,
|
2007-10-16 20:47:32 +02:00
|
|
|
MAX_ROUTERDESC_DOWNLOAD_FAILURES)) {
|
2007-10-18 16:19:51 +02:00
|
|
|
++n_delayed; /* Not ready for retry. */
|
2007-10-11 20:01:12 +02:00
|
|
|
continue;
|
2007-10-16 20:47:32 +02:00
|
|
|
}
|
|
|
|
if (authdir && dirserv_would_reject_router(rs)) {
|
|
|
|
++n_would_reject;
|
2007-10-08 21:56:57 +02:00
|
|
|
continue; /* We would throw it out immediately. */
|
2007-10-16 20:47:32 +02:00
|
|
|
}
|
2007-12-04 19:35:03 +01:00
|
|
|
if (!directory_caches_dir_info(options) &&
|
|
|
|
!client_would_use_router(rs, now, options)) {
|
2007-10-16 20:47:32 +02:00
|
|
|
++n_wouldnt_use;
|
2007-10-08 21:56:57 +02:00
|
|
|
continue; /* We would never use it ourself. */
|
2007-10-16 20:47:32 +02:00
|
|
|
}
|
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;
|
|
|
|
oldrouter = router_get_by_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)",
|
|
|
|
rs->nickname, time_bufnew,
|
|
|
|
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));
|
2007-11-07 16:19:53 +01:00
|
|
|
SMARTLIST_FOREACH(no_longer_old, signed_descriptor_t *, sd, {
|
|
|
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
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? */
|
|
|
|
/*XXXX021 this info should come from netinfo cells or something, or we should
|
|
|
|
* do this only when we aren't seeing incoming data. see bug 652. */
|
|
|
|
#define DUMMY_DOWNLOAD_INTERVAL (20*60)
|
|
|
|
|
2006-03-17 06:50:41 +01:00
|
|
|
/** Launch downloads for router status as needed. */
|
2005-12-27 06:26:03 +01:00
|
|
|
void
|
|
|
|
update_router_descriptor_downloads(time_t now)
|
|
|
|
{
|
|
|
|
or_options_t *options = get_options();
|
2008-04-22 23:10:41 +02:00
|
|
|
static time_t last_dummy_download = 0;
|
2007-06-12 11:17:23 +02:00
|
|
|
if (should_delay_dir_fetches(options))
|
|
|
|
return;
|
2007-12-21 07:08:00 +01:00
|
|
|
if (directory_fetches_dir_info_early(options)) {
|
2007-12-04 19:35:03 +01:00
|
|
|
update_router_descriptor_cache_downloads_v2(now);
|
2005-12-27 06:26:03 +01:00
|
|
|
}
|
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));
|
2008-04-22 23:10:41 +02:00
|
|
|
|
|
|
|
/* XXXX021 we could be smarter here; see notes on bug 652. */
|
2010-09-20 20:18:17 +02:00
|
|
|
/* XXXX NM Microdescs: if we're not fetching microdescriptors, we need
|
|
|
|
* to make something else invoke this. */
|
2008-04-22 23:10:41 +02: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_routerdesc_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,
|
2008-12-11 20:12:45 +01:00
|
|
|
ROUTER_PURPOSE_GENERAL, "authority.z",
|
|
|
|
PDS_RETRY_IF_NO_SERVERS);
|
2008-04-22 23:10:41 +02:00
|
|
|
}
|
2005-12-27 06:26:03 +01:00
|
|
|
}
|
|
|
|
|
2007-06-12 11:17:23 +02:00
|
|
|
/** Launch extrainfo downloads as needed. */
|
2007-05-18 23:19:19 +02:00
|
|
|
void
|
|
|
|
update_extrainfo_downloads(time_t now)
|
|
|
|
{
|
|
|
|
or_options_t *options = get_options();
|
|
|
|
routerlist_t *rl;
|
|
|
|
smartlist_t *wanted;
|
|
|
|
digestmap_t *pending;
|
2007-10-18 16:50:59 +02:00
|
|
|
int old_routers, i;
|
|
|
|
int n_no_ei = 0, n_pending = 0, n_have = 0, n_delay = 0;
|
2007-05-18 23:19:19 +02:00
|
|
|
if (! options->DownloadExtraInfo)
|
|
|
|
return;
|
2007-06-12 11:17:23 +02:00
|
|
|
if (should_delay_dir_fetches(options))
|
|
|
|
return;
|
2007-10-21 01:45:07 +02:00
|
|
|
if (!router_have_minimum_dir_info())
|
|
|
|
return;
|
2007-05-18 23:19:19 +02:00
|
|
|
|
|
|
|
pending = digestmap_new();
|
|
|
|
list_pending_descriptor_downloads(pending, 1);
|
|
|
|
rl = router_get_routerlist();
|
|
|
|
wanted = smartlist_create();
|
2007-10-18 16:50:59 +02:00
|
|
|
for (old_routers = 0; old_routers < 2; ++old_routers) {
|
|
|
|
smartlist_t *lst = old_routers ? rl->old_routers : rl->routers;
|
|
|
|
for (i = 0; i < smartlist_len(lst); ++i) {
|
|
|
|
signed_descriptor_t *sd;
|
|
|
|
char *d;
|
|
|
|
if (old_routers)
|
|
|
|
sd = smartlist_get(lst, i);
|
|
|
|
else
|
|
|
|
sd = &((routerinfo_t*)smartlist_get(lst, i))->cache_info;
|
|
|
|
if (sd->is_extrainfo)
|
|
|
|
continue; /* This should never happen. */
|
2007-10-21 06:41:00 +02:00
|
|
|
if (old_routers && !router_get_by_digest(sd->identity_digest))
|
|
|
|
continue; /* Couldn't check the signature if we got it. */
|
|
|
|
if (sd->extrainfo_is_bogus)
|
|
|
|
continue;
|
2007-10-18 16:50:59 +02:00
|
|
|
d = sd->extra_info_digest;
|
|
|
|
if (tor_digest_is_zero(d)) {
|
|
|
|
++n_no_ei;
|
|
|
|
continue;
|
2007-05-18 23:19:19 +02:00
|
|
|
}
|
2007-10-18 16:50:59 +02:00
|
|
|
if (eimap_get(rl->extra_info_map, d)) {
|
|
|
|
++n_have;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!download_status_is_ready(&sd->ei_dl_status, now,
|
|
|
|
MAX_ROUTERDESC_DOWNLOAD_FAILURES)) {
|
|
|
|
++n_delay;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (digestmap_get(pending, d)) {
|
|
|
|
++n_pending;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
smartlist_add(wanted, d);
|
|
|
|
}
|
2007-05-18 23:19:19 +02:00
|
|
|
}
|
|
|
|
digestmap_free(pending, NULL);
|
|
|
|
|
2007-10-18 16:50:59 +02:00
|
|
|
log_info(LD_DIR, "Extrainfo download status: %d router with no ei, %d "
|
|
|
|
"with present ei, %d delaying, %d pending, %d downloadable.",
|
|
|
|
n_no_ei, n_have, n_delay, n_pending, smartlist_len(wanted));
|
|
|
|
|
2007-05-18 23:19:19 +02:00
|
|
|
smartlist_shuffle(wanted);
|
|
|
|
for (i = 0; i < smartlist_len(wanted); i += MAX_DL_PER_REQUEST) {
|
|
|
|
initiate_descriptor_downloads(NULL, DIR_PURPOSE_FETCH_EXTRAINFO,
|
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);
|
|
|
|
}
|
|
|
|
|
2007-02-16 21:01:02 +01:00
|
|
|
/** True iff, the last time we checked whether we had enough directory info
|
|
|
|
* to build circuits, the answer was "yes". */
|
2006-08-15 05:54:09 +02:00
|
|
|
static int have_min_dir_info = 0;
|
2007-02-24 08:50:38 +01:00
|
|
|
/** True iff enough has changed since the last time we checked whether we had
|
2007-02-16 21:01:02 +01:00
|
|
|
* enough directory info to build circuits that our old answer can no longer
|
2007-02-24 08:50:38 +01:00
|
|
|
* be trusted. */
|
2006-08-15 05:54:09 +02:00
|
|
|
static int need_to_update_have_min_dir_info = 1;
|
2007-12-19 05:18:28 +01:00
|
|
|
/** String describing what we're missing before we have enough directory
|
|
|
|
* info. */
|
2007-11-02 05:18:28 +01:00
|
|
|
static char dir_info_status[128] = "";
|
2006-08-15 05:54:09 +02:00
|
|
|
|
2005-09-15 08:15:31 +02:00
|
|
|
/** Return true iff we have enough networkstatus and router information to
|
2006-03-22 07:22:12 +01:00
|
|
|
* start building circuits. Right now, this means "more than half the
|
|
|
|
* networkstatus documents, and at least 1/4 of expected routers." */
|
2005-12-15 22:26:52 +01:00
|
|
|
//XXX should consider whether we have enough exiting nodes here.
|
2005-09-15 07:19:38 +02:00
|
|
|
int
|
|
|
|
router_have_minimum_dir_info(void)
|
2006-08-15 05:54:09 +02:00
|
|
|
{
|
2007-03-06 21:25:44 +01:00
|
|
|
if (PREDICT_UNLIKELY(need_to_update_have_min_dir_info)) {
|
2006-08-15 05:54:09 +02:00
|
|
|
update_router_have_minimum_dir_info();
|
|
|
|
need_to_update_have_min_dir_info = 0;
|
|
|
|
}
|
|
|
|
return have_min_dir_info;
|
|
|
|
}
|
|
|
|
|
2007-01-22 08:51:06 +01:00
|
|
|
/** Called when our internal view of the directory has changed. This can be
|
|
|
|
* when the authorities change, networkstatuses change, the list of routerdescs
|
|
|
|
* changes, or number of running routers changes.
|
2006-08-15 05:54:09 +02:00
|
|
|
*/
|
2007-10-08 23:21:50 +02:00
|
|
|
void
|
2006-08-15 05:54:09 +02:00
|
|
|
router_dir_info_changed(void)
|
|
|
|
{
|
|
|
|
need_to_update_have_min_dir_info = 1;
|
2008-09-09 10:41:58 +02:00
|
|
|
rend_hsdir_routers_changed();
|
2006-08-15 05:54:09 +02:00
|
|
|
}
|
|
|
|
|
2007-12-19 05:18:28 +01:00
|
|
|
/** Return a string describing what we're missing before we have enough
|
|
|
|
* directory info. */
|
2007-11-02 05:18:28 +01:00
|
|
|
const char *
|
|
|
|
get_dir_info_status_string(void)
|
|
|
|
{
|
|
|
|
return dir_info_status;
|
|
|
|
}
|
|
|
|
|
2008-12-26 18:35:08 +01:00
|
|
|
/** Iterate over the servers listed in <b>consensus</b>, and count how many of
|
|
|
|
* them seem like ones we'd use, and how many of <em>those</em> we have
|
|
|
|
* descriptors for. Store the former in *<b>num_usable</b> and the latter in
|
2010-09-21 21:17:40 +02:00
|
|
|
* *<b>num_present</b>. If <b>in_set</b> is non-NULL, only consider those
|
|
|
|
* routers in <b>in_set</b>.
|
|
|
|
*/
|
2008-06-09 08:33:29 +02:00
|
|
|
static void
|
|
|
|
count_usable_descriptors(int *num_present, int *num_usable,
|
|
|
|
const networkstatus_t *consensus,
|
2010-09-21 21:17:40 +02:00
|
|
|
or_options_t *options, time_t now,
|
|
|
|
routerset_t *in_set)
|
2008-06-09 08:33:29 +02:00
|
|
|
{
|
|
|
|
*num_present = 0, *num_usable=0;
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH(consensus->routerstatus_list, routerstatus_t *, rs,
|
|
|
|
{
|
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 (in_set && ! routerset_contains_routerstatus(in_set, rs, -1))
|
2010-09-21 21:17:40 +02:00
|
|
|
continue;
|
2008-06-09 08:33:29 +02:00
|
|
|
if (client_would_use_router(rs, now, options)) {
|
|
|
|
++*num_usable; /* the consensus says we want it. */
|
|
|
|
if (router_get_by_descriptor_digest(rs->descriptor_digest)) {
|
|
|
|
/* we have the descriptor listed in the consensus. */
|
|
|
|
++*num_present;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
log_debug(LD_DIR, "%d usable, %d present.", *num_usable, *num_present);
|
|
|
|
}
|
|
|
|
|
2008-06-11 01:13:14 +02:00
|
|
|
/** We just fetched a new set of descriptors. Compute how far through
|
|
|
|
* the "loading descriptors" bootstrapping phase we are, so we can inform
|
|
|
|
* the controller of our progress. */
|
2008-06-09 08:33:29 +02:00
|
|
|
int
|
|
|
|
count_loading_descriptors_progress(void)
|
|
|
|
{
|
|
|
|
int num_present = 0, num_usable=0;
|
|
|
|
time_t now = time(NULL);
|
|
|
|
const networkstatus_t *consensus =
|
2010-09-20 20:18:17 +02:00
|
|
|
networkstatus_get_reasonably_live_consensus(now, FLAV_NS);
|
2008-06-09 08:33:29 +02:00
|
|
|
double fraction;
|
|
|
|
|
|
|
|
if (!consensus)
|
|
|
|
return 0; /* can't count descriptors if we have no list of them */
|
|
|
|
|
|
|
|
count_usable_descriptors(&num_present, &num_usable,
|
2010-09-21 21:17:40 +02:00
|
|
|
consensus, get_options(), now, NULL);
|
2008-06-09 08:33:29 +02:00
|
|
|
|
|
|
|
if (num_usable == 0)
|
|
|
|
return 0; /* don't div by 0 */
|
|
|
|
fraction = num_present / (num_usable/4.);
|
|
|
|
if (fraction > 1.0)
|
|
|
|
return 0; /* it's not the number of descriptors holding us back */
|
2008-06-09 22:52:59 +02:00
|
|
|
return BOOTSTRAP_STATUS_LOADING_DESCRIPTORS + (int)
|
|
|
|
(fraction*(BOOTSTRAP_STATUS_CONN_OR-1 -
|
|
|
|
BOOTSTRAP_STATUS_LOADING_DESCRIPTORS));
|
2008-06-09 08:33:29 +02:00
|
|
|
}
|
|
|
|
|
2007-01-22 08:51:06 +01:00
|
|
|
/** Change the value of have_min_dir_info, setting it true iff we have enough
|
|
|
|
* network and router information to build circuits. Clear the value of
|
|
|
|
* need_to_update_have_min_dir_info. */
|
2006-08-15 05:54:09 +02:00
|
|
|
static void
|
|
|
|
update_router_have_minimum_dir_info(void)
|
2005-09-15 07:19:38 +02:00
|
|
|
{
|
2008-06-07 07:00:06 +02:00
|
|
|
int num_present = 0, num_usable=0;
|
2007-02-13 03:01:38 +01:00
|
|
|
time_t now = time(NULL);
|
2007-10-16 01:15:24 +02:00
|
|
|
int res;
|
|
|
|
or_options_t *options = get_options();
|
2008-02-05 22:39:29 +01:00
|
|
|
const networkstatus_t *consensus =
|
2010-09-20 20:18:17 +02:00
|
|
|
networkstatus_get_reasonably_live_consensus(now, FLAV_NS);
|
2007-10-08 23:21:50 +02:00
|
|
|
|
2007-10-16 01:15:24 +02:00
|
|
|
if (!consensus) {
|
2007-11-02 05:18:28 +01:00
|
|
|
if (!networkstatus_get_latest_consensus())
|
2007-11-11 18:37:59 +01:00
|
|
|
strlcpy(dir_info_status, "We have no network-status consensus.",
|
2007-11-02 18:16:34 +01:00
|
|
|
sizeof(dir_info_status));
|
2007-11-02 05:18:28 +01:00
|
|
|
else
|
2007-11-11 18:37:59 +01:00
|
|
|
strlcpy(dir_info_status, "We have no recent network-status consensus.",
|
2007-11-02 18:16:34 +01:00
|
|
|
sizeof(dir_info_status));
|
2005-12-15 21:44:15 +01:00
|
|
|
res = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
2007-02-13 03:01:38 +01:00
|
|
|
|
2007-06-12 11:17:23 +02:00
|
|
|
if (should_delay_dir_fetches(get_options())) {
|
2007-07-30 10:43:02 +02:00
|
|
|
log_notice(LD_DIR, "no known bridge descriptors running yet; stalling");
|
2007-11-04 03:36:09 +01:00
|
|
|
strlcpy(dir_info_status, "No live bridge descriptors.",
|
|
|
|
sizeof(dir_info_status));
|
2007-06-12 11:17:23 +02:00
|
|
|
res = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2010-09-21 21:17:40 +02:00
|
|
|
count_usable_descriptors(&num_present, &num_usable, consensus, options, now,
|
|
|
|
NULL);
|
2008-06-07 07:00:06 +02:00
|
|
|
|
2007-11-02 05:18:28 +01:00
|
|
|
if (num_present < num_usable/4) {
|
|
|
|
tor_snprintf(dir_info_status, sizeof(dir_info_status),
|
|
|
|
"We have only %d/%d usable descriptors.", num_present, num_usable);
|
|
|
|
res = 0;
|
2008-06-07 07:27:34 +02:00
|
|
|
control_event_bootstrap(BOOTSTRAP_STATUS_REQUESTING_DESCRIPTORS, 0);
|
2010-09-21 21:17:40 +02:00
|
|
|
goto done;
|
2008-06-07 07:00:06 +02:00
|
|
|
} else if (num_present < 2) {
|
2007-11-02 05:18:28 +01:00
|
|
|
tor_snprintf(dir_info_status, sizeof(dir_info_status),
|
2008-06-07 07:00:06 +02:00
|
|
|
"Only %d descriptor%s here and believed reachable!",
|
|
|
|
num_present, num_present ? "" : "s");
|
2007-11-02 05:18:28 +01:00
|
|
|
res = 0;
|
2010-09-21 21:17:40 +02:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for entry nodes. */
|
|
|
|
if (options->EntryNodes) {
|
2010-09-22 07:30:23 +02:00
|
|
|
count_usable_descriptors(&num_present, &num_usable, consensus, options,
|
|
|
|
now, options->EntryNodes);
|
2010-09-21 21:17:40 +02:00
|
|
|
|
2010-09-29 03:59:31 +02:00
|
|
|
if (!num_usable || !num_present) {
|
2010-09-21 21:17:40 +02:00
|
|
|
tor_snprintf(dir_info_status, sizeof(dir_info_status),
|
|
|
|
"We have only %d/%d usable entry node descriptors.",
|
|
|
|
num_present, num_usable);
|
|
|
|
res = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
2007-11-02 05:18:28 +01:00
|
|
|
}
|
2007-10-16 01:15:24 +02:00
|
|
|
|
2010-09-21 21:17:40 +02:00
|
|
|
res = 1;
|
|
|
|
|
2005-12-15 21:44:15 +01:00
|
|
|
done:
|
2006-08-15 05:54:09 +02:00
|
|
|
if (res && !have_min_dir_info) {
|
2005-12-15 21:44:15 +01:00
|
|
|
log(LOG_NOTICE, LD_DIR,
|
|
|
|
"We now have enough directory information to build circuits.");
|
2006-10-23 12:16:43 +02:00
|
|
|
control_event_client_status(LOG_NOTICE, "ENOUGH_DIR_INFO");
|
2008-06-07 07:27:34 +02:00
|
|
|
control_event_bootstrap(BOOTSTRAP_STATUS_CONN_OR, 0);
|
2005-12-15 21:44:15 +01:00
|
|
|
}
|
2006-08-15 05:54:09 +02:00
|
|
|
if (!res && have_min_dir_info) {
|
2008-06-09 07:17:52 +02:00
|
|
|
int quiet = directory_too_idle_to_fetch_descriptors(options, now);
|
2008-06-09 07:16:47 +02:00
|
|
|
log(quiet ? LOG_INFO : LOG_NOTICE, LD_DIR,
|
|
|
|
"Our directory information is no longer up-to-date "
|
2008-06-07 00:31:36 +02:00
|
|
|
"enough to build circuits: %s", dir_info_status);
|
2010-09-21 08:16:43 +02:00
|
|
|
|
|
|
|
/* a) make us log when we next complete a circuit, so we know when Tor
|
|
|
|
* is back up and usable, and b) disable some activities that Tor
|
|
|
|
* should only do while circuits are working, like reachability tests
|
|
|
|
* and fetching bridge descriptors only over circuits. */
|
2010-09-22 07:52:57 +02:00
|
|
|
can_complete_circuit = 0;
|
2010-09-21 08:16:43 +02:00
|
|
|
|
2006-10-23 12:16:43 +02:00
|
|
|
control_event_client_status(LOG_NOTICE, "NOT_ENOUGH_DIR_INFO");
|
2005-12-15 21:44:15 +01:00
|
|
|
}
|
2006-08-15 05:54:09 +02:00
|
|
|
have_min_dir_info = res;
|
2007-11-02 05:18:28 +01:00
|
|
|
need_to_update_have_min_dir_info = 0;
|
2005-09-15 07:19:38 +02:00
|
|
|
}
|
|
|
|
|
2005-09-30 22:04:55 +02:00
|
|
|
/** Reset the descriptor download failure count on all routers, so that we
|
|
|
|
* can retry any long-failed routers immediately.
|
|
|
|
*/
|
2005-09-22 08:34:29 +02:00
|
|
|
void
|
|
|
|
router_reset_descriptor_download_failures(void)
|
|
|
|
{
|
2007-10-16 01:15:24 +02:00
|
|
|
networkstatus_reset_download_failures();
|
2005-10-05 07:03:52 +02:00
|
|
|
last_routerdesc_download_attempted = 0;
|
2007-05-24 19:13:02 +02:00
|
|
|
if (!routerlist)
|
|
|
|
return;
|
|
|
|
SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, ri,
|
|
|
|
{
|
2007-10-09 17:27:45 +02:00
|
|
|
download_status_reset(&ri->cache_info.ei_dl_status);
|
2007-05-24 19:13:02 +02:00
|
|
|
});
|
|
|
|
SMARTLIST_FOREACH(routerlist->old_routers, signed_descriptor_t *, sd,
|
|
|
|
{
|
2007-10-09 17:27:45 +02:00
|
|
|
download_status_reset(&sd->ei_dl_status);
|
2007-05-24 19:13:02 +02:00
|
|
|
});
|
2005-09-22 08:34:29 +02:00
|
|
|
}
|
2005-09-23 19:11:20 +02:00
|
|
|
|
2006-03-12 23:48:18 +01:00
|
|
|
/** Any changes in a router descriptor's publication time larger than this are
|
|
|
|
* automatically non-cosmetic. */
|
|
|
|
#define ROUTER_MAX_COSMETIC_TIME_DIFFERENCE (12*60*60)
|
|
|
|
|
2006-06-18 18:05:54 +02:00
|
|
|
/** We allow uptime to vary from how much it ought to be by this much. */
|
2006-11-02 00:53:15 +01:00
|
|
|
#define ROUTER_ALLOW_UPTIME_DRIFT (6*60*60)
|
2006-06-18 18:05:54 +02:00
|
|
|
|
2005-10-12 15:49:13 +02:00
|
|
|
/** Return true iff the only differences between r1 and r2 are such that
|
2005-11-01 04:48:51 +01:00
|
|
|
* would not cause a recent (post 0.1.1.6) dirserver to republish.
|
2005-10-12 15:49:13 +02:00
|
|
|
*/
|
|
|
|
int
|
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. */
|
|
|
|
if (strcasecmp(r1->address, r2->address) ||
|
|
|
|
strcasecmp(r1->nickname, r2->nickname) ||
|
|
|
|
r1->or_port != r2->or_port ||
|
|
|
|
r1->dir_port != r2->dir_port ||
|
2007-12-23 02:18:14 +01:00
|
|
|
r1->purpose != r2->purpose ||
|
2005-10-12 19:16:25 +02:00
|
|
|
crypto_pk_cmp_keys(r1->onion_pkey, r2->onion_pkey) ||
|
|
|
|
crypto_pk_cmp_keys(r1->identity_pkey, r2->identity_pkey) ||
|
2005-10-12 15:49:13 +02:00
|
|
|
strcasecmp(r1->platform, r2->platform) ||
|
2005-10-18 23:19:02 +02:00
|
|
|
(r1->contact_info && !r2->contact_info) || /* contact_info is optional */
|
|
|
|
(!r1->contact_info && r2->contact_info) ||
|
2005-12-14 21:40:40 +01:00
|
|
|
(r1->contact_info && r2->contact_info &&
|
|
|
|
strcasecmp(r1->contact_info, r2->contact_info)) ||
|
2005-10-12 15:49:13 +02:00
|
|
|
r1->is_hibernating != r2->is_hibernating ||
|
2006-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
|
|
|
|
2007-10-21 06:41:00 +02:00
|
|
|
digest_matches = !memcmp(ei->cache_info.signed_descriptor_digest,
|
|
|
|
sd->extra_info_digest, DIGEST_LEN);
|
|
|
|
|
|
|
|
/* The identity must match exactly to have been generated at the same time
|
2007-05-25 21:41:31 +02:00
|
|
|
* by the same router. */
|
2007-10-21 06:41:00 +02:00
|
|
|
if (memcmp(ri->cache_info.identity_digest, ei->cache_info.identity_digest,
|
2007-05-20 20:03:26 +02:00
|
|
|
DIGEST_LEN)) {
|
|
|
|
if (msg) *msg = "Extrainfo nickname or identity did not match routerinfo";
|
2007-10-21 06:41:00 +02:00
|
|
|
goto err; /* different servers */
|
2007-05-20 20:03:26 +02:00
|
|
|
}
|
2007-04-16 19:55:08 +02:00
|
|
|
|
|
|
|
if (ei->pending_sig) {
|
|
|
|
char signed_digest[128];
|
|
|
|
if (crypto_pk_public_checksig(ri->identity_pkey, signed_digest,
|
2007-05-15 00:51:05 +02:00
|
|
|
ei->pending_sig, ei->pending_sig_len) != DIGEST_LEN ||
|
2007-04-16 19:55:08 +02:00
|
|
|
memcmp(signed_digest, ei->cache_info.signed_descriptor_digest,
|
2007-04-16 23:37:21 +02:00
|
|
|
DIGEST_LEN)) {
|
|
|
|
ei->bad_sig = 1;
|
|
|
|
tor_free(ei->pending_sig);
|
2007-05-20 20:03:26 +02:00
|
|
|
if (msg) *msg = "Extrainfo signature bad, or signed with wrong key";
|
2007-10-21 06:41:00 +02:00
|
|
|
goto err; /* Bad signature, or no match. */
|
2007-04-16 23:37:21 +02:00
|
|
|
}
|
2007-04-16 19:55:08 +02:00
|
|
|
|
2008-01-10 18:48:40 +01:00
|
|
|
ei->cache_info.send_unencrypted = ri->cache_info.send_unencrypted;
|
2007-04-16 19:55:08 +02:00
|
|
|
tor_free(ei->pending_sig);
|
|
|
|
}
|
|
|
|
|
2007-10-21 02:08:35 +02:00
|
|
|
if (ei->cache_info.published_on < sd->published_on) {
|
2007-05-20 20:03:26 +02:00
|
|
|
if (msg) *msg = "Extrainfo published time did not match routerdesc";
|
2007-10-21 06:41:00 +02:00
|
|
|
goto err;
|
2007-10-21 02:08:35 +02:00
|
|
|
} else if (ei->cache_info.published_on > sd->published_on) {
|
2007-05-20 20:03:26 +02:00
|
|
|
if (msg) *msg = "Extrainfo published time did not match routerdesc";
|
2007-10-21 06:41:00 +02:00
|
|
|
r = -1;
|
|
|
|
goto err;
|
2007-05-20 20:03:26 +02:00
|
|
|
}
|
|
|
|
|
2007-10-21 06:41:00 +02:00
|
|
|
if (!digest_matches) {
|
2007-05-20 20:03:26 +02:00
|
|
|
if (msg) *msg = "Extrainfo digest did not match value from routerdesc";
|
2007-10-21 06:41:00 +02:00
|
|
|
goto err; /* Digest doesn't match declared value. */
|
2007-05-20 20:03:26 +02:00
|
|
|
}
|
2007-04-16 19:55:08 +02:00
|
|
|
|
|
|
|
return 0;
|
2007-10-21 06:41:00 +02:00
|
|
|
err:
|
|
|
|
if (digest_matches) {
|
|
|
|
/* This signature was okay, and the digest was right: This is indeed the
|
|
|
|
* corresponding extrainfo. But insanely, it doesn't match the routerinfo
|
|
|
|
* that lists it. Don't try to fetch this one again. */
|
|
|
|
sd->extrainfo_is_bogus = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
2007-04-16 19:55:08 +02:00
|
|
|
}
|
|
|
|
|
2007-01-22 08:51:06 +01:00
|
|
|
/** Assert that the internal representation of <b>rl</b> is
|
|
|
|
* self-consistent. */
|
2007-04-30 07:36:41 +02:00
|
|
|
void
|
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;
|
|
|
|
SMARTLIST_FOREACH(rl->routers, routerinfo_t *, r,
|
|
|
|
{
|
2007-06-12 00:19:40 +02:00
|
|
|
r2 = rimap_get(rl->identity_map, r->cache_info.identity_digest);
|
2005-10-27 02:34:39 +02:00
|
|
|
tor_assert(r == r2);
|
2007-06-12 00:19:40 +02:00
|
|
|
sd2 = sdmap_get(rl->desc_digest_map,
|
|
|
|
r->cache_info.signed_descriptor_digest);
|
2005-11-05 21:15:27 +01:00
|
|
|
tor_assert(&(r->cache_info) == sd2);
|
2007-11-07 18:11:23 +01:00
|
|
|
tor_assert(r->cache_info.routerlist_index == r_sl_idx);
|
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
|
|
|
*/
|
2005-10-27 02:34:39 +02:00
|
|
|
});
|
2005-11-05 21:15:27 +01:00
|
|
|
SMARTLIST_FOREACH(rl->old_routers, signed_descriptor_t *, sd,
|
2005-10-27 02:34:39 +02:00
|
|
|
{
|
2007-06-12 00:19:40 +02:00
|
|
|
r2 = rimap_get(rl->identity_map, sd->identity_digest);
|
2005-11-05 21:15:27 +01:00
|
|
|
tor_assert(sd != &(r2->cache_info));
|
2007-06-12 00:19:40 +02:00
|
|
|
sd2 = sdmap_get(rl->desc_digest_map, sd->signed_descriptor_digest);
|
2005-11-05 21:15:27 +01:00
|
|
|
tor_assert(sd == sd2);
|
2007-11-07 18:11:23 +01:00
|
|
|
tor_assert(sd->routerlist_index == sd_sl_idx);
|
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
|
|
|
*/
|
2005-10-18 22:13:09 +02:00
|
|
|
});
|
2007-06-12 00:19:40 +02:00
|
|
|
|
2008-02-06 06:31:21 +01:00
|
|
|
RIMAP_FOREACH(rl->identity_map, d, r) {
|
2005-11-05 21:15:27 +01:00
|
|
|
tor_assert(!memcmp(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) {
|
2005-11-05 21:15:27 +01:00
|
|
|
tor_assert(!memcmp(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);
|
2007-05-19 20:12:31 +02:00
|
|
|
tor_assert(!memcmp(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;
|
|
|
|
tor_assert(!memcmp(ei->cache_info.signed_descriptor_digest,
|
|
|
|
d, DIGEST_LEN));
|
2007-06-12 00:19:40 +02:00
|
|
|
sd = sdmap_get(rl->desc_by_eid_map,
|
|
|
|
ei->cache_info.signed_descriptor_digest);
|
2008-12-18 17:11:24 +01:00
|
|
|
// tor_assert(sd); // XXXX see above
|
2007-06-12 02:15:55 +02:00
|
|
|
if (sd) {
|
|
|
|
tor_assert(!memcmp(ei->cache_info.signed_descriptor_digest,
|
|
|
|
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;
|
|
|
|
size_t len;
|
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);
|
|
|
|
|
|
|
|
len = strlen(esc_contact)+strlen(esc_platform)+32;
|
|
|
|
info = tor_malloc(len);
|
|
|
|
tor_snprintf(info, len, "Contact %s, Platform %s", esc_contact,
|
|
|
|
esc_platform);
|
|
|
|
tor_free(esc_contact);
|
|
|
|
tor_free(esc_platform);
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2007-10-12 23:33:37 +02:00
|
|
|
/** Helper for sorting: compare two routerinfos by their identity
|
|
|
|
* digest. */
|
|
|
|
static int
|
|
|
|
_compare_routerinfo_by_id_digest(const void **a, const void **b)
|
|
|
|
{
|
|
|
|
routerinfo_t *first = *(routerinfo_t **)a, *second = *(routerinfo_t **)b;
|
|
|
|
return memcmp(first->cache_info.identity_digest,
|
|
|
|
second->cache_info.identity_digest,
|
|
|
|
DIGEST_LEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Sort a list of routerinfo_t in ascending order of identity digest. */
|
|
|
|
void
|
|
|
|
routers_sort_by_identity(smartlist_t *routers)
|
|
|
|
{
|
|
|
|
smartlist_sort(routers, _compare_routerinfo_by_id_digest);
|
|
|
|
}
|
|
|
|
|
2008-07-18 20:36:32 +02:00
|
|
|
/** A routerset specifies constraints on a set of possible routerinfos, based
|
|
|
|
* on their names, identities, or addresses. It is optimized for determining
|
|
|
|
* whether a router is a member or not, in O(1+P) time, where P is the number
|
|
|
|
* of address policy constraints. */
|
|
|
|
struct routerset_t {
|
|
|
|
/** A list of strings for the elements of the policy. Each string is either
|
|
|
|
* a nickname, a hexadecimal identity fingerprint, or an address policy. A
|
|
|
|
* router belongs to the set if its nickname OR its identity OR its address
|
|
|
|
* matches an entry here. */
|
|
|
|
smartlist_t *list;
|
|
|
|
/** A map from lowercase nicknames of routers in the set to (void*)1 */
|
|
|
|
strmap_t *names;
|
|
|
|
/** A map from identity digests routers in the set to (void*)1 */
|
|
|
|
digestmap_t *digests;
|
|
|
|
/** An address policy for routers in the set. For implementation reasons,
|
|
|
|
* a router belongs to the set if it is _rejected_ by this policy. */
|
|
|
|
smartlist_t *policies;
|
2008-09-25 22:21:35 +02:00
|
|
|
|
2008-12-17 23:58:20 +01:00
|
|
|
/** A human-readable description of what this routerset is for. Used in
|
|
|
|
* log messages. */
|
2008-09-25 22:21:35 +02:00
|
|
|
char *description;
|
|
|
|
|
2008-12-17 23:58:20 +01:00
|
|
|
/** A list of the country codes in this set. */
|
2008-09-25 22:21:35 +02:00
|
|
|
smartlist_t *country_names;
|
2008-12-18 17:11:08 +01:00
|
|
|
/** Total number of countries we knew about when we built <b>countries</b>.*/
|
2008-09-25 22:21:35 +02:00
|
|
|
int n_countries;
|
2008-12-17 23:58:20 +01:00
|
|
|
/** Bit array mapping the return value of geoip_get_country() to 1 iff the
|
|
|
|
* country is a member of this routerset. Note that we MUST call
|
|
|
|
* routerset_refresh_countries() whenever the geoip country list is
|
|
|
|
* reloaded. */
|
2008-09-25 22:21:35 +02:00
|
|
|
bitarray_t *countries;
|
2008-07-18 20:36:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Return a new empty routerset. */
|
|
|
|
routerset_t *
|
|
|
|
routerset_new(void)
|
|
|
|
{
|
|
|
|
routerset_t *result = tor_malloc_zero(sizeof(routerset_t));
|
|
|
|
result->list = smartlist_create();
|
|
|
|
result->names = strmap_new();
|
|
|
|
result->digests = digestmap_new();
|
|
|
|
result->policies = smartlist_create();
|
2008-09-25 22:21:35 +02:00
|
|
|
result->country_names = smartlist_create();
|
2008-07-18 20:36:32 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-12-17 23:58:20 +01:00
|
|
|
/** If <b>c</b> is a country code in the form {cc}, return a newly allocated
|
|
|
|
* string holding the "cc" part. Else, return NULL. */
|
2008-09-25 22:21:35 +02:00
|
|
|
static char *
|
|
|
|
routerset_get_countryname(const char *c)
|
|
|
|
{
|
|
|
|
char *country;
|
|
|
|
|
|
|
|
if (strlen(c) < 4 || c[0] !='{' || c[3] !='}')
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
country = tor_strndup(c+1, 2);
|
|
|
|
tor_strlower(country);
|
|
|
|
return country;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/** Add the GeoIP database's integer index (+1) of a valid two-character
|
|
|
|
* country code to the routerset's <b>countries</b> bitarray. Return the
|
|
|
|
* integer index if the country code is valid, -1 otherwise.*/
|
|
|
|
static int
|
|
|
|
routerset_add_country(const char *c)
|
|
|
|
{
|
|
|
|
char country[3];
|
|
|
|
country_t cc;
|
|
|
|
|
|
|
|
/* XXXX: Country codes must be of the form \{[a-z\?]{2}\} but this accepts
|
|
|
|
\{[.]{2}\}. Do we need to be strict? -RH */
|
|
|
|
/* Nope; if the country code is bad, we'll get 0 when we look it up. */
|
|
|
|
|
|
|
|
if (!geoip_is_loaded()) {
|
|
|
|
log(LOG_WARN, LD_CONFIG, "GeoIP database not loaded: Cannot add country"
|
|
|
|
"entry %s, ignoring.", c);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(country, c+1, 2);
|
|
|
|
country[2] = '\0';
|
|
|
|
tor_strlower(country);
|
|
|
|
|
|
|
|
if ((cc=geoip_get_country(country))==-1) {
|
|
|
|
log(LOG_WARN, LD_CONFIG, "Country code '%s' is not valid, ignoring.",
|
|
|
|
country);
|
|
|
|
}
|
|
|
|
return cc;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/** Update the routerset's <b>countries</b> bitarray_t. Called whenever
|
|
|
|
* the GeoIP database is reloaded.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
routerset_refresh_countries(routerset_t *target)
|
|
|
|
{
|
|
|
|
int cc;
|
2009-12-12 08:07:59 +01:00
|
|
|
bitarray_free(target->countries);
|
|
|
|
|
2008-09-25 22:21:35 +02:00
|
|
|
if (!geoip_is_loaded()) {
|
|
|
|
target->countries = NULL;
|
|
|
|
target->n_countries = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
target->n_countries = geoip_get_n_countries();
|
|
|
|
target->countries = bitarray_init_zero(target->n_countries);
|
|
|
|
SMARTLIST_FOREACH_BEGIN(target->country_names, const char *, country) {
|
|
|
|
cc = geoip_get_country(country);
|
|
|
|
if (cc >= 0) {
|
|
|
|
tor_assert(cc < target->n_countries);
|
|
|
|
bitarray_set(target->countries, cc);
|
|
|
|
} else {
|
|
|
|
log(LOG_WARN, LD_CONFIG, "Country code '%s' is not recognized.",
|
|
|
|
country);
|
|
|
|
}
|
|
|
|
} SMARTLIST_FOREACH_END(country);
|
|
|
|
}
|
|
|
|
|
2008-07-18 20:36:32 +02:00
|
|
|
/** Parse the string <b>s</b> to create a set of routerset entries, and add
|
|
|
|
* them to <b>target</b>. In log messages, refer to the string as
|
|
|
|
* <b>description</b>. Return 0 on success, -1 on failure.
|
|
|
|
*
|
|
|
|
* Three kinds of elements are allowed in routersets: nicknames, IP address
|
|
|
|
* patterns, and fingerprints. They may be surrounded by optional space, and
|
2009-05-27 23:55:51 +02:00
|
|
|
* must be separated by commas.
|
2008-07-18 20:36:32 +02:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
routerset_parse(routerset_t *target, const char *s, const char *description)
|
|
|
|
{
|
|
|
|
int r = 0;
|
2008-09-25 22:21:35 +02:00
|
|
|
int added_countries = 0;
|
|
|
|
char *countryname;
|
2008-07-18 20:36:32 +02:00
|
|
|
smartlist_t *list = smartlist_create();
|
|
|
|
smartlist_split_string(list, s, ",",
|
|
|
|
SPLIT_SKIP_SPACE | SPLIT_IGNORE_BLANK, 0);
|
2008-09-25 22:21:35 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(list, char *, nick) {
|
2008-07-18 20:36:32 +02:00
|
|
|
addr_policy_t *p;
|
|
|
|
if (is_legal_hexdigest(nick)) {
|
|
|
|
char d[DIGEST_LEN];
|
|
|
|
if (*nick == '$')
|
|
|
|
++nick;
|
2008-07-18 21:11:30 +02:00
|
|
|
log_debug(LD_CONFIG, "Adding identity %s to %s", nick, description);
|
2008-07-18 20:36:32 +02:00
|
|
|
base16_decode(d, sizeof(d), nick, HEX_DIGEST_LEN);
|
|
|
|
digestmap_set(target->digests, d, (void*)1);
|
|
|
|
} else if (is_legal_nickname(nick)) {
|
2008-07-18 21:11:30 +02:00
|
|
|
log_debug(LD_CONFIG, "Adding nickname %s to %s", nick, description);
|
2008-07-18 20:36:32 +02:00
|
|
|
strmap_set_lc(target->names, nick, (void*)1);
|
2008-09-25 22:21:35 +02:00
|
|
|
} else if ((countryname = routerset_get_countryname(nick)) != NULL) {
|
|
|
|
log_debug(LD_CONFIG, "Adding country %s to %s", nick,
|
|
|
|
description);
|
|
|
|
smartlist_add(target->country_names, countryname);
|
|
|
|
added_countries = 1;
|
2008-07-18 20:36:32 +02:00
|
|
|
} else if ((strchr(nick,'.') || strchr(nick, '*')) &&
|
|
|
|
(p = router_parse_addr_policy_item_from_string(
|
2008-07-18 21:11:30 +02:00
|
|
|
nick, ADDR_POLICY_REJECT))) {
|
|
|
|
log_debug(LD_CONFIG, "Adding address %s to %s", nick, description);
|
2008-07-18 20:36:32 +02:00
|
|
|
smartlist_add(target->policies, p);
|
|
|
|
} else {
|
2008-09-25 22:21:35 +02:00
|
|
|
log_warn(LD_CONFIG, "Entry '%s' in %s is misformed.", nick,
|
2008-07-18 20:36:32 +02:00
|
|
|
description);
|
|
|
|
r = -1;
|
|
|
|
tor_free(nick);
|
|
|
|
SMARTLIST_DEL_CURRENT(list, nick);
|
|
|
|
}
|
2008-09-25 22:21:35 +02:00
|
|
|
} SMARTLIST_FOREACH_END(nick);
|
2008-07-18 20:36:32 +02:00
|
|
|
smartlist_add_all(target->list, list);
|
|
|
|
smartlist_free(list);
|
2008-09-25 22:21:35 +02:00
|
|
|
if (added_countries)
|
|
|
|
routerset_refresh_countries(target);
|
2008-07-18 20:36:32 +02:00
|
|
|
return r;
|
|
|
|
}
|
2009-02-16 16:15:06 +01:00
|
|
|
|
2009-10-19 06:30:52 +02:00
|
|
|
/** Called when we change a node set, or when we reload the geoip list:
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
or_options_t *options = get_options();
|
|
|
|
|
|
|
|
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);
|
|
|
|
if (options->_ExcludeExitNodesUnion)
|
|
|
|
routerset_refresh_countries(options->_ExcludeExitNodesUnion);
|
|
|
|
|
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
|
|
|
|
|
|
|
/** Add all members of the set <b>source</b> to <b>target</b>. */
|
|
|
|
void
|
|
|
|
routerset_union(routerset_t *target, const routerset_t *source)
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
tor_assert(target);
|
|
|
|
if (!source || !source->list)
|
|
|
|
return;
|
|
|
|
s = routerset_to_string(source);
|
|
|
|
routerset_parse(target, s, "other routerset");
|
|
|
|
tor_free(s);
|
|
|
|
}
|
|
|
|
|
2008-09-25 22:21:35 +02:00
|
|
|
/** Return true iff <b>set</b> lists only nicknames and digests, and includes
|
|
|
|
* no IP ranges or countries. */
|
|
|
|
int
|
|
|
|
routerset_is_list(const routerset_t *set)
|
|
|
|
{
|
|
|
|
return smartlist_len(set->country_names) == 0 &&
|
|
|
|
smartlist_len(set->policies) == 0;
|
|
|
|
}
|
|
|
|
|
2008-12-17 23:58:20 +01:00
|
|
|
/** Return true iff we need a GeoIP IP-to-country database to make sense of
|
|
|
|
* <b>set</b>. */
|
2008-09-25 23:06:32 +02:00
|
|
|
int
|
|
|
|
routerset_needs_geoip(const routerset_t *set)
|
|
|
|
{
|
|
|
|
return set && smartlist_len(set->country_names);
|
|
|
|
}
|
|
|
|
|
2008-12-17 23:58:20 +01:00
|
|
|
/** Return true iff there are no entries in <b>set</b>. */
|
2008-09-25 22:21:35 +02:00
|
|
|
static int
|
|
|
|
routerset_is_empty(const routerset_t *set)
|
|
|
|
{
|
|
|
|
return !set || smartlist_len(set->list) == 0;
|
|
|
|
}
|
|
|
|
|
2008-07-18 20:36:32 +02:00
|
|
|
/** Helper. Return true iff <b>set</b> contains a router based on the other
|
2008-12-17 23:58:20 +01:00
|
|
|
* provided fields. Return higher values for more specific subentries: a
|
|
|
|
* single router is more specific than an address range of routers, which is
|
|
|
|
* more specific in turn than a country code.
|
|
|
|
*
|
|
|
|
* (If country is -1, then we take the country
|
|
|
|
* from addr.) */
|
2008-07-18 20:36:32 +02:00
|
|
|
static int
|
2008-09-16 00:29:47 +02:00
|
|
|
routerset_contains(const routerset_t *set, const tor_addr_t *addr,
|
|
|
|
uint16_t orport,
|
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 char *nickname, const char *id_digest,
|
2008-09-25 22:21:35 +02:00
|
|
|
country_t country)
|
2008-07-18 20:36:32 +02:00
|
|
|
{
|
|
|
|
if (!set || !set->list) return 0;
|
2008-09-16 00:29:47 +02:00
|
|
|
if (nickname && strmap_get_lc(set->names, nickname))
|
2008-09-25 22:21:35 +02:00
|
|
|
return 4;
|
2008-09-16 00:29:47 +02:00
|
|
|
if (id_digest && digestmap_get(set->digests, id_digest))
|
2008-09-25 22:21:35 +02:00
|
|
|
return 4;
|
2008-09-16 00:29:47 +02:00
|
|
|
if (addr && compare_tor_addr_to_addr_policy(addr, orport, set->policies)
|
2008-07-18 21:11:30 +02:00
|
|
|
== ADDR_POLICY_REJECTED)
|
2008-09-25 22:21:35 +02:00
|
|
|
return 3;
|
|
|
|
if (set->countries) {
|
|
|
|
if (country < 0 && addr)
|
|
|
|
country = geoip_get_country_by_ip(tor_addr_to_ipv4h(addr));
|
|
|
|
|
|
|
|
if (country >= 0 && country < set->n_countries &&
|
|
|
|
bitarray_is_set(set->countries, country))
|
|
|
|
return 2;
|
|
|
|
}
|
2008-07-18 20:36:32 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-16 00:29:47 +02:00
|
|
|
/** Return true iff we can tell that <b>ei</b> is a member of <b>set</b>. */
|
|
|
|
int
|
2009-05-27 20:45:44 +02:00
|
|
|
routerset_contains_extendinfo(const routerset_t *set, const extend_info_t *ei)
|
2008-09-16 00:29:47 +02:00
|
|
|
{
|
|
|
|
return routerset_contains(set,
|
|
|
|
&ei->addr,
|
|
|
|
ei->port,
|
|
|
|
ei->nickname,
|
|
|
|
ei->identity_digest,
|
2008-09-25 22:21:35 +02:00
|
|
|
-1 /*country*/);
|
2008-09-16 00:29:47 +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 true iff <b>ri</b> is in <b>set</b>. If country is <b>-1</b>, we
|
|
|
|
* look up the country. */
|
2008-07-18 20:36:32 +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
|
|
|
routerset_contains_router(const routerset_t *set, const routerinfo_t *ri,
|
|
|
|
country_t country)
|
2008-07-18 20:36:32 +02:00
|
|
|
{
|
2008-09-16 00:29:47 +02:00
|
|
|
tor_addr_t addr;
|
|
|
|
tor_addr_from_ipv4h(&addr, ri->addr);
|
2008-07-18 20:36:32 +02:00
|
|
|
return routerset_contains(set,
|
2008-09-16 00:29:47 +02:00
|
|
|
&addr,
|
2008-07-18 20:36:32 +02:00
|
|
|
ri->or_port,
|
|
|
|
ri->nickname,
|
|
|
|
ri->cache_info.identity_digest,
|
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
|
|
|
country);
|
2008-07-18 20:36:32 +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 true iff <b>rs</b> is in <b>set</b>. If country is <b>-1</b>, we
|
|
|
|
* look up the country. */
|
2008-07-18 20:36:32 +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
|
|
|
routerset_contains_routerstatus(const routerset_t *set,
|
|
|
|
const routerstatus_t *rs,
|
|
|
|
country_t country)
|
2008-07-18 20:36:32 +02:00
|
|
|
{
|
2008-09-16 00:29:47 +02:00
|
|
|
tor_addr_t addr;
|
|
|
|
tor_addr_from_ipv4h(&addr, rs->addr);
|
2008-07-18 20:36:32 +02:00
|
|
|
return routerset_contains(set,
|
2008-09-16 00:29:47 +02:00
|
|
|
&addr,
|
2008-07-18 20:36:32 +02:00
|
|
|
rs->or_port,
|
|
|
|
rs->nickname,
|
|
|
|
rs->identity_digest,
|
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
|
|
|
country);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return true iff <b>node</b> is in <b>set</b>. */
|
|
|
|
int
|
|
|
|
routerset_contains_node(const routerset_t *set, const node_t *node)
|
|
|
|
{
|
|
|
|
if (node->rs)
|
|
|
|
return routerset_contains_routerstatus(set, node->rs, node->country);
|
|
|
|
else if (node->ri)
|
|
|
|
return routerset_contains_router(set, node->ri, node->country);
|
|
|
|
else
|
|
|
|
return 0;
|
2008-07-18 20:36:32 +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 known node_t that is a member of <b>routerset</b> to
|
2008-07-18 20:36:32 +02:00
|
|
|
* <b>out</b>. If <b>running_only</b>, only add the running ones. */
|
|
|
|
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
|
|
|
routerset_get_all_nodes(smartlist_t *out, const routerset_t *routerset,
|
|
|
|
int running_only)
|
|
|
|
{ /* XXXX MOVE */
|
2008-07-18 20:36:32 +02:00
|
|
|
tor_assert(out);
|
|
|
|
if (!routerset || !routerset->list)
|
|
|
|
return;
|
2008-09-25 22:21:35 +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
|
|
|
if (routerset_is_list(routerset)) {
|
2008-09-25 22:21:35 +02:00
|
|
|
/* No routers are specified by type; all are given by name or digest.
|
|
|
|
* we can do a lookup in O(len(list)). */
|
|
|
|
SMARTLIST_FOREACH(routerset->list, const char *, name, {
|
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 *node = node_get_by_nickname(name, 1);
|
|
|
|
if (node) {
|
|
|
|
if (!running_only || node->is_running)
|
|
|
|
smartlist_add(out, (void*)node);
|
2008-09-25 22:21:35 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
/* We need to iterate over the routerlist to get all the ones of the
|
|
|
|
* right kind. */
|
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_t *nodes = nodelist_get_list();
|
|
|
|
SMARTLIST_FOREACH(nodes, const node_t *, node, {
|
|
|
|
if (running_only && !node->is_running)
|
2008-09-25 22:21:35 +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 (routerset_contains_node(routerset, node))
|
|
|
|
smartlist_add(out, (void*)node);
|
2008-09-25 22:21:35 +02:00
|
|
|
});
|
2008-07-18 20:36:32 +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 to <b>target</b> every node_t from <b>source</b> except:
|
2009-09-20 03:02:21 +02:00
|
|
|
*
|
|
|
|
* 1) Don't add it if <b>include</b> is non-empty and the relay isn't in
|
|
|
|
* <b>include</b>; and
|
|
|
|
* 2) Don't add it if <b>exclude</b> is non-empty and the relay is
|
|
|
|
* excluded in a more specific fashion by <b>exclude</b>.
|
|
|
|
* 3) If <b>running_only</b>, don't add non-running routers.
|
2008-12-17 23:58:20 +01:00
|
|
|
*/
|
2008-09-25 22:21:35 +02:00
|
|
|
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
|
|
|
routersets_get_node_disjunction(smartlist_t *target,
|
2008-09-25 22:21:35 +02:00
|
|
|
const smartlist_t *source,
|
|
|
|
const routerset_t *include,
|
|
|
|
const routerset_t *exclude, int running_only)
|
|
|
|
{
|
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(source, const node_t *, node, {
|
2008-09-25 22:21:35 +02:00
|
|
|
int include_result;
|
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 (running_only && !node->is_running)
|
2008-09-25 22:21:35 +02:00
|
|
|
continue;
|
|
|
|
if (!routerset_is_empty(include))
|
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
|
|
|
include_result = routerset_contains_node(include, node);
|
2008-09-25 22:21:35 +02:00
|
|
|
else
|
|
|
|
include_result = 1;
|
|
|
|
|
|
|
|
if (include_result) {
|
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 exclude_result = routerset_contains_node(exclude, node);
|
2008-09-25 22:21:35 +02:00
|
|
|
if (include_result >= exclude_result)
|
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(target, (void*)node);
|
2008-09-25 22:21:35 +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
|
|
|
/** Remove every node_t from <b>lst</b> that is in <b>routerset</b>. */
|
2008-07-18 20:36:32 +02:00
|
|
|
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
|
|
|
routerset_subtract_nodes(smartlist_t *lst, const routerset_t *routerset)
|
|
|
|
{ /*XXXX MOVE ? */
|
2008-07-18 20:36:32 +02:00
|
|
|
tor_assert(lst);
|
|
|
|
if (!routerset)
|
|
|
|
return;
|
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(lst, const node_t *, node, {
|
|
|
|
if (routerset_contains_node(routerset, node)) {
|
2008-07-18 21:11:30 +02:00
|
|
|
//log_debug(LD_DIR, "Subtracting %s",r->nickname);
|
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_DEL_CURRENT(lst, node);
|
2008-07-18 20:36:32 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return a new string that when parsed by routerset_parse_string() will
|
|
|
|
* yield <b>set</b>. */
|
|
|
|
char *
|
|
|
|
routerset_to_string(const routerset_t *set)
|
|
|
|
{
|
|
|
|
if (!set || !set->list)
|
|
|
|
return tor_strdup("");
|
|
|
|
return smartlist_join_strings(set->list, ",", 0, NULL);
|
|
|
|
}
|
|
|
|
|
2008-09-25 22:21:35 +02:00
|
|
|
/** Helper: return true iff old and new are both NULL, or both non-NULL
|
|
|
|
* equal routersets. */
|
|
|
|
int
|
|
|
|
routerset_equal(const routerset_t *old, const routerset_t *new)
|
|
|
|
{
|
2008-11-16 19:25:20 +01:00
|
|
|
if (old == NULL && new == NULL)
|
|
|
|
return 1;
|
|
|
|
else if (old == NULL || new == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2008-09-25 22:21:35 +02:00
|
|
|
if (smartlist_len(old->list) != smartlist_len(new->list))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH(old->list, const char *, cp1, {
|
|
|
|
const char *cp2 = smartlist_get(new->list, cp1_sl_idx);
|
|
|
|
if (strcmp(cp1, cp2))
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-07-18 20:36:32 +02:00
|
|
|
/** Free all storage held in <b>routerset</b>. */
|
|
|
|
void
|
|
|
|
routerset_free(routerset_t *routerset)
|
|
|
|
{
|
2009-09-28 16:37:01 +02:00
|
|
|
if (!routerset)
|
|
|
|
return;
|
|
|
|
|
2008-07-18 20:36:32 +02:00
|
|
|
SMARTLIST_FOREACH(routerset->list, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(routerset->list);
|
|
|
|
SMARTLIST_FOREACH(routerset->policies, addr_policy_t *, p,
|
|
|
|
addr_policy_free(p));
|
|
|
|
smartlist_free(routerset->policies);
|
2008-09-25 22:21:35 +02:00
|
|
|
SMARTLIST_FOREACH(routerset->country_names, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(routerset->country_names);
|
2008-07-18 20:36:32 +02:00
|
|
|
|
|
|
|
strmap_free(routerset->names, NULL);
|
|
|
|
digestmap_free(routerset->digests, NULL);
|
2009-12-12 08:07:59 +01:00
|
|
|
bitarray_free(routerset->countries);
|
2008-07-18 20:36:32 +02:00
|
|
|
tor_free(routerset);
|
|
|
|
}
|
|
|
|
|
2008-12-17 23:58:20 +01:00
|
|
|
/** Refresh the country code of <b>ri</b>. This function MUST be called on
|
|
|
|
* each router when the GeoIP database is reloaded, and on all new routers. */
|
2008-09-25 22:21:35 +02:00
|
|
|
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
|
|
|
node_set_country(node_t *node)
|
2008-09-25 22:21:35 +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
|
|
|
if (node->rs)
|
|
|
|
node->country = geoip_get_country_by_ip(node->rs->addr);
|
|
|
|
else if (node->ri)
|
|
|
|
node->country = geoip_get_country_by_ip(node->ri->addr);
|
|
|
|
else
|
|
|
|
node->country = -1;
|
2008-09-25 22:21:35 +02:00
|
|
|
}
|
|
|
|
|
2008-12-17 23:58:20 +01:00
|
|
|
/** Set the country code of all routers in the routerlist. */
|
2008-09-25 22:21:35 +02:00
|
|
|
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
|
|
|
nodelist_refresh_countries(void) /* MOVE */
|
2008-09-25 22:21:35 +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
|
|
|
smartlist_t *nodes = nodelist_get_list();
|
|
|
|
SMARTLIST_FOREACH(nodes, node_t *, node,
|
|
|
|
node_set_country(node));
|
2008-09-25 22:21:35 +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();
|
2008-09-25 00:29:22 +02:00
|
|
|
int use_begindir = get_options()->TunnelDirConns;
|
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) {
|
2008-09-25 00:29:22 +02:00
|
|
|
if (r->dir_port || use_begindir)
|
|
|
|
smartlist_add(responsible_dirs, r);
|
|
|
|
else
|
|
|
|
log_info(LD_REND, "Not adding router '%s' to list of responsible "
|
|
|
|
"hidden service directories, because we have no way of "
|
|
|
|
"reaching it.", r->nickname);
|
2007-11-03 21:12:41 +01:00
|
|
|
if (++n_added == REND_NUMBER_OF_CONSECUTIVE_REPLICAS)
|
2008-09-25 00:29:22 +02:00
|
|
|
break;
|
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();
|
2008-02-05 22:39:29 +01:00
|
|
|
networkstatus_t *c;
|
2010-09-29 07:35:08 +02:00
|
|
|
const routerstatus_t *rs;
|
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;
|
|
|
|
}
|
2007-11-03 21:12:41 +01:00
|
|
|
if (!(c = networkstatus_get_latest_consensus())) {
|
|
|
|
log_info(LD_REND, "There's no consensus, so I can't tell if I'm a hidden "
|
|
|
|
"service directory");
|
2007-10-29 20:10:42 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2007-11-03 21:12:41 +01:00
|
|
|
rs = networkstatus_vote_find_entry(c, me->cache_info.identity_digest);
|
|
|
|
if (!rs) {
|
|
|
|
log_info(LD_REND, "We're not listed in the consensus, so we're not "
|
|
|
|
"being a hidden service directory.");
|
2007-10-29 20:10:42 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2007-11-03 21:12:41 +01:00
|
|
|
if (!rs->is_hs_dir) {
|
|
|
|
log_info(LD_REND, "We're not listed as a hidden service directory in "
|
|
|
|
"the consensus, so we won't be one.");
|
|
|
|
return 0;
|
|
|
|
}
|
2007-10-29 20:10:42 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return true if this node is responsible for storing the descriptor ID
|
|
|
|
* in <b>query</b> and false otherwise. */
|
|
|
|
int
|
2007-11-03 21:12:41 +01:00
|
|
|
hid_serv_responsible_for_desc_id(const char *query)
|
2007-10-29 20:10:42 +01:00
|
|
|
{
|
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;
|
|
|
|
responsible = smartlist_create();
|
2008-01-25 08:22:42 +01:00
|
|
|
if (hid_serv_get_responsible_directories(responsible, query) < 0) {
|
2007-11-03 21:12:41 +01:00
|
|
|
smartlist_free(responsible);
|
2007-10-29 20:10:42 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2007-11-03 21:12:41 +01:00
|
|
|
last_rs = smartlist_get(responsible, smartlist_len(responsible)-1);
|
|
|
|
last_id = last_rs->identity_digest;
|
|
|
|
result = rend_id_is_in_interval(my_id, query, last_id);
|
|
|
|
smartlist_free(responsible);
|
|
|
|
return result;
|
2007-10-29 20:10:42 +01:00
|
|
|
}
|
|
|
|
|