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.
|
2018-06-20 14:13:28 +02:00
|
|
|
* Copyright (c) 2007-2018, 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.
|
2016-10-15 02:08:51 +02:00
|
|
|
*
|
|
|
|
* A "routerinfo_t" object represents a single self-signed router
|
|
|
|
* descriptor, as generated by a Tor relay in order to tell the rest of
|
|
|
|
* the world about its keys, address, and capabilities. An
|
|
|
|
* "extrainfo_t" object represents an adjunct "extra-info" object,
|
|
|
|
* certified by a corresponding router descriptor, reporting more
|
|
|
|
* information about the relay that nearly all users will not need.
|
|
|
|
*
|
|
|
|
* Most users will not use router descriptors for most relays. Instead,
|
|
|
|
* they use the information in microdescriptors and in the consensus
|
|
|
|
* networkstatus.
|
|
|
|
*
|
|
|
|
* Right now, routerinfo_t objects are used in these ways:
|
|
|
|
* <ul>
|
|
|
|
* <li>By clients, in order to learn about bridge keys and capabilities.
|
|
|
|
* (Bridges aren't listed in the consensus networkstatus, so they
|
|
|
|
* can't have microdescriptors.)
|
|
|
|
* <li>By relays, since relays want more information about other relays
|
|
|
|
* than they can learn from microdescriptors. (TODO: Is this still true?)
|
|
|
|
* <li>By authorities, which receive them and use them to generate the
|
|
|
|
* consensus and the microdescriptors.
|
|
|
|
* <li>By all directory caches, which download them in case somebody
|
|
|
|
* else wants them.
|
|
|
|
* </ul>
|
|
|
|
*
|
|
|
|
* Routerinfos are mostly created by parsing them from a string, in
|
|
|
|
* routerparse.c. We store them to disk on receiving them, and
|
|
|
|
* periodically discard the ones we don't need. On restarting, we
|
|
|
|
* re-read them from disk. (This also applies to extrainfo documents, if
|
|
|
|
* we are configured to fetch them.)
|
|
|
|
*
|
|
|
|
* In order to keep our list of routerinfos up-to-date, we periodically
|
|
|
|
* check whether there are any listed in the latest consensus (or in the
|
|
|
|
* votes from other authorities, if we are an authority) that we don't
|
|
|
|
* have. (This also applies to extrainfo documents, if we are
|
|
|
|
* configured to fetch them.)
|
|
|
|
*
|
|
|
|
* Almost nothing in Tor should use a routerinfo_t to refer directly to
|
|
|
|
* a relay; instead, almost everything should use node_t (implemented in
|
|
|
|
* nodelist.c), which provides a common interface to routerinfo_t,
|
|
|
|
* routerstatus_t, and microdescriptor_t.
|
|
|
|
*
|
|
|
|
* <br>
|
|
|
|
*
|
|
|
|
* This module also has some of the functions used for choosing random
|
|
|
|
* nodes according to different rules and weights. Historically, they
|
|
|
|
* were all in this module. Now, they are spread across this module,
|
|
|
|
* nodelist.c, and networkstatus.c. (TODO: Fix that.)
|
2004-05-09 18:47:25 +02:00
|
|
|
**/
|
2004-05-05 02:30:43 +02:00
|
|
|
|
2012-08-09 19:47:42 +02:00
|
|
|
#define ROUTERLIST_PRIVATE
|
2018-07-05 22:34:59 +02:00
|
|
|
#include "core/or/or.h"
|
2018-09-19 20:10:15 +02:00
|
|
|
|
2018-07-05 22:34:59 +02:00
|
|
|
#include "app/config/config.h"
|
|
|
|
#include "core/mainloop/connection.h"
|
2018-09-20 21:19:43 +02:00
|
|
|
#include "core/mainloop/mainloop.h"
|
2018-09-19 20:10:15 +02:00
|
|
|
#include "core/or/policies.h"
|
|
|
|
#include "feature/client/bridges.h"
|
2018-07-05 22:34:59 +02:00
|
|
|
#include "feature/control/control.h"
|
2018-09-19 20:10:15 +02:00
|
|
|
#include "feature/dirauth/mode.h"
|
2018-07-05 22:34:59 +02:00
|
|
|
#include "feature/dircache/directory.h"
|
|
|
|
#include "feature/dircache/dirserv.h"
|
2018-09-20 14:09:35 +02:00
|
|
|
#include "feature/dirauth/reachability.h"
|
|
|
|
#include "feature/dirauth/process_descs.h"
|
2018-09-19 20:10:15 +02:00
|
|
|
#include "feature/nodelist/authcert.h"
|
|
|
|
#include "feature/nodelist/dirlist.h"
|
2018-07-05 22:34:59 +02:00
|
|
|
#include "feature/nodelist/microdesc.h"
|
|
|
|
#include "feature/nodelist/networkstatus.h"
|
|
|
|
#include "feature/nodelist/nodelist.h"
|
2018-09-19 20:10:15 +02:00
|
|
|
#include "feature/nodelist/node_select.h"
|
2018-07-05 22:34:59 +02:00
|
|
|
#include "feature/nodelist/routerlist.h"
|
|
|
|
#include "feature/nodelist/routerparse.h"
|
|
|
|
#include "feature/nodelist/routerset.h"
|
|
|
|
#include "feature/nodelist/torcert.h"
|
2018-09-19 20:10:15 +02:00
|
|
|
#include "feature/relay/router.h"
|
|
|
|
#include "feature/stats/rephist.h"
|
|
|
|
#include "lib/crypt_ops/crypto_format.h"
|
|
|
|
#include "lib/crypt_ops/crypto_rand.h"
|
2018-04-05 20:27:30 +02:00
|
|
|
|
2018-07-05 22:34:59 +02:00
|
|
|
#include "feature/dircommon/dir_connection_st.h"
|
|
|
|
#include "feature/dirclient/dir_server_st.h"
|
|
|
|
#include "feature/nodelist/document_signature_st.h"
|
|
|
|
#include "feature/nodelist/extrainfo_st.h"
|
|
|
|
#include "feature/nodelist/networkstatus_st.h"
|
|
|
|
#include "feature/nodelist/networkstatus_voter_info_st.h"
|
|
|
|
#include "feature/nodelist/node_st.h"
|
|
|
|
#include "feature/nodelist/routerinfo_st.h"
|
|
|
|
#include "feature/nodelist/routerlist_st.h"
|
|
|
|
#include "feature/nodelist/vote_routerstatus_st.h"
|
2018-06-14 22:58:01 +02:00
|
|
|
|
2018-06-26 19:18:23 +02:00
|
|
|
#include "lib/crypt_ops/digestset.h"
|
2018-06-21 22:48:54 +02:00
|
|
|
|
2018-07-01 21:02:01 +02:00
|
|
|
#ifdef HAVE_SYS_STAT_H
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
|
2007-05-19 20:31:04 +02:00
|
|
|
// #define DEBUG_ROUTERLIST
|
|
|
|
|
2002-09-26 14:09:10 +02:00
|
|
|
/****************************************************************************/
|
|
|
|
|
2016-10-15 02:08:51 +02:00
|
|
|
/* Typed wrappers for different digestmap types; used to avoid type
|
|
|
|
* confusion. */
|
|
|
|
|
2013-05-09 13:56:54 +02:00
|
|
|
DECLARE_TYPED_DIGESTMAP_FNS(sdmap_, digest_sd_map_t, signed_descriptor_t)
|
|
|
|
DECLARE_TYPED_DIGESTMAP_FNS(rimap_, digest_ri_map_t, routerinfo_t)
|
|
|
|
DECLARE_TYPED_DIGESTMAP_FNS(eimap_, digest_ei_map_t, extrainfo_t)
|
|
|
|
#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)
|
2017-11-17 17:46:25 +01:00
|
|
|
#define eimap_free(map, fn) MAP_FREE_AND_NULL(eimap, (map), (fn))
|
|
|
|
#define rimap_free(map, fn) MAP_FREE_AND_NULL(rimap, (map), (fn))
|
2018-09-19 20:10:15 +02:00
|
|
|
#define sdmap_free(map, fn) MAP_FREE_AND_NULL(sdmap, (map), (fn))
|
2013-05-09 18:31:39 +02:00
|
|
|
|
2018-09-19 20:10:15 +02:00
|
|
|
/* static function prototypes */
|
|
|
|
static int signed_desc_digest_is_recognized(signed_descriptor_t *desc);
|
|
|
|
static const char *signed_descriptor_get_body_impl(
|
|
|
|
const signed_descriptor_t *desc,
|
|
|
|
int with_annotations);
|
|
|
|
static void launch_dummy_descriptor_download_as_needed(time_t now,
|
|
|
|
const or_options_t *options);
|
2013-05-09 13:56:54 +02:00
|
|
|
|
2018-09-19 20:10:15 +02:00
|
|
|
/****************************************************************************/
|
2013-05-09 13:56:54 +02:00
|
|
|
|
2018-09-19 20:10:15 +02:00
|
|
|
/** Global list of all of the routers that we know about. */
|
|
|
|
static routerlist_t *routerlist = NULL;
|
2013-05-09 13:56:54 +02:00
|
|
|
|
2018-09-19 20:10:15 +02:00
|
|
|
/** List of strings for nicknames we've already warned about and that are
|
|
|
|
* still unknown / unavailable. */
|
|
|
|
static smartlist_t *warned_nicknames = NULL;
|
2013-05-09 13:56:54 +02:00
|
|
|
|
2018-09-19 20:10:15 +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. */
|
|
|
|
static time_t last_descriptor_download_attempted = 0;
|
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>. */
|
2015-12-10 16:19:43 +01: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
|
|
|
{
|
2017-11-14 22:49:41 +01:00
|
|
|
char *fname = get_cachedir_fname_suffix(store->fname_base, ".new");
|
2007-09-26 18:19:44 +02:00
|
|
|
const char *body = signed_descriptor_get_body_impl(desc,1);
|
|
|
|
size_t len = desc->signed_descriptor_len + desc->annotations_len;
|
2007-08-31 17:08:37 +02:00
|
|
|
|
2006-07-10 00:28:12 +02:00
|
|
|
if (append_bytes_to_file(fname, body, len, 1)) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_FS, "Unable to store router descriptor");
|
2005-09-13 08:21:10 +02:00
|
|
|
tor_free(fname);
|
|
|
|
return -1;
|
|
|
|
}
|
2006-04-29 20:42:26 +02:00
|
|
|
desc->saved_location = SAVED_IN_JOURNAL;
|
2005-09-13 08:21:10 +02:00
|
|
|
tor_free(fname);
|
2007-04-30 21:48:39 +02:00
|
|
|
|
2007-08-31 17:08:37 +02:00
|
|
|
desc->saved_offset = store->journal_len;
|
|
|
|
store->journal_len += len;
|
2007-04-30 16:26:38 +02:00
|
|
|
|
2005-09-13 08:21:10 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-16 21:01:02 +01:00
|
|
|
/** Sorting helper: return <0, 0, or >0 depending on whether the
|
|
|
|
* signed_descriptor_t* in *<b>a</b> is older, the same age as, or newer than
|
2007-02-24 08:50:38 +01:00
|
|
|
* the signed_descriptor_t* in *<b>b</b>. */
|
2006-06-22 09:01:54 +02:00
|
|
|
static int
|
2012-10-12 18:22:13 +02:00
|
|
|
compare_signed_descriptors_by_age_(const void **_a, const void **_b)
|
2006-06-22 09:01:54 +02:00
|
|
|
{
|
|
|
|
const signed_descriptor_t *r1 = *_a, *r2 = *_b;
|
2008-02-22 20:09:45 +01:00
|
|
|
return (int)(r1->published_on - r2->published_on);
|
2006-06-22 09:01:54 +02:00
|
|
|
}
|
|
|
|
|
2008-05-29 02:44:57 +02:00
|
|
|
#define RRS_FORCE 1
|
|
|
|
#define RRS_DONT_REMOVE_OLD 2
|
|
|
|
|
2008-12-19 19:51:44 +01:00
|
|
|
/** If the journal of <b>store</b> is too long, or if RRS_FORCE is set in
|
|
|
|
* <b>flags</b>, then atomically replace the saved router store with the
|
|
|
|
* routers currently in our routerlist, and clear the journal. Unless
|
|
|
|
* RRS_DONT_REMOVE_OLD is set in <b>flags</b>, delete expired routers before
|
|
|
|
* rebuilding the store. Return 0 on success, -1 on failure.
|
2005-09-13 23:14:55 +02:00
|
|
|
*/
|
2005-09-15 07:41:30 +02:00
|
|
|
static int
|
2008-05-29 02:44:57 +02:00
|
|
|
router_rebuild_store(int flags, desc_store_t *store)
|
2005-09-13 08:21:10 +02:00
|
|
|
{
|
|
|
|
smartlist_t *chunk_list = NULL;
|
2006-12-07 16:07:33 +01:00
|
|
|
char *fname = NULL, *fname_tmp = NULL;
|
2007-04-30 21:48:39 +02:00
|
|
|
int r = -1;
|
2006-06-27 17:52:51 +02:00
|
|
|
off_t offset = 0;
|
2007-04-30 21:48:39 +02:00
|
|
|
smartlist_t *signed_descriptors = NULL;
|
2007-10-16 01:15:24 +02:00
|
|
|
int nocache=0;
|
2007-10-24 17:45:45 +02:00
|
|
|
size_t total_expected_len = 0;
|
|
|
|
int had_any;
|
2008-05-29 02:44:57 +02:00
|
|
|
int force = flags & RRS_FORCE;
|
2007-04-30 21:48:39 +02:00
|
|
|
|
2009-01-13 15:43:51 +01:00
|
|
|
if (!force && !router_should_rebuild_store(store)) {
|
|
|
|
r = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (!routerlist) {
|
|
|
|
r = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
2005-09-13 08:21:10 +02:00
|
|
|
|
2007-10-24 17:45:45 +02:00
|
|
|
if (store->type == EXTRAINFO_STORE)
|
|
|
|
had_any = !eimap_isempty(routerlist->extra_info_map);
|
|
|
|
else
|
|
|
|
had_any = (smartlist_len(routerlist->routers)+
|
|
|
|
smartlist_len(routerlist->old_routers))>0;
|
2007-04-30 16:26:38 +02:00
|
|
|
|
2005-09-15 07:19:38 +02:00
|
|
|
/* Don't save deadweight. */
|
2008-05-29 02:44:57 +02:00
|
|
|
if (!(flags & RRS_DONT_REMOVE_OLD))
|
|
|
|
routerlist_remove_old_routers();
|
2005-09-15 07:19:38 +02:00
|
|
|
|
2007-08-31 17:08:37 +02:00
|
|
|
log_info(LD_DIR, "Rebuilding %s cache", store->description);
|
2006-06-27 17:52:51 +02:00
|
|
|
|
2017-11-14 22:49:41 +01:00
|
|
|
fname = get_cachedir_fname(store->fname_base);
|
|
|
|
fname_tmp = get_cachedir_fname_suffix(store->fname_base, ".tmp");
|
2006-12-07 16:07:33 +01:00
|
|
|
|
2012-01-18 21:53:30 +01:00
|
|
|
chunk_list = smartlist_new();
|
2005-09-13 08:21:10 +02:00
|
|
|
|
2007-03-21 16:37:30 +01:00
|
|
|
/* We sort the routers by age to enhance locality on disk. */
|
2012-01-18 21:53:30 +01:00
|
|
|
signed_descriptors = smartlist_new();
|
2007-08-31 17:08:37 +02:00
|
|
|
if (store->type == EXTRAINFO_STORE) {
|
2007-06-12 00:19:40 +02:00
|
|
|
eimap_iter_t *iter;
|
|
|
|
for (iter = eimap_iter_init(routerlist->extra_info_map);
|
|
|
|
!eimap_iter_done(iter);
|
|
|
|
iter = eimap_iter_next(routerlist->extra_info_map, iter)) {
|
2007-04-30 21:48:39 +02:00
|
|
|
const char *key;
|
|
|
|
extrainfo_t *ei;
|
2007-06-12 00:19:40 +02:00
|
|
|
eimap_iter_get(iter, &key, &ei);
|
2007-04-30 21:48:39 +02:00
|
|
|
smartlist_add(signed_descriptors, &ei->cache_info);
|
|
|
|
}
|
|
|
|
} else {
|
2007-08-31 17:08:37 +02:00
|
|
|
SMARTLIST_FOREACH(routerlist->old_routers, signed_descriptor_t *, sd,
|
2007-10-16 01:15:24 +02:00
|
|
|
smartlist_add(signed_descriptors, sd));
|
2007-04-30 21:48:39 +02:00
|
|
|
SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, ri,
|
2007-10-16 01:15:24 +02:00
|
|
|
smartlist_add(signed_descriptors, &ri->cache_info));
|
2007-04-30 21:48:39 +02:00
|
|
|
}
|
|
|
|
|
2012-10-12 18:22:13 +02:00
|
|
|
smartlist_sort(signed_descriptors, compare_signed_descriptors_by_age_);
|
2007-04-30 21:48:39 +02:00
|
|
|
|
|
|
|
/* Now, add the appropriate members to chunk_list */
|
2012-07-17 15:33:38 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(signed_descriptors, signed_descriptor_t *, sd) {
|
2005-10-27 02:34:39 +02:00
|
|
|
sized_chunk_t *c;
|
2007-09-26 18:19:44 +02:00
|
|
|
const char *body = signed_descriptor_get_body_impl(sd, 1);
|
2006-01-12 19:04:17 +01:00
|
|
|
if (!body) {
|
2007-03-04 21:11:46 +01:00
|
|
|
log_warn(LD_BUG, "No descriptor available for router.");
|
2005-10-27 02:34:39 +02:00
|
|
|
goto done;
|
|
|
|
}
|
2007-10-16 01:15:24 +02:00
|
|
|
if (sd->do_not_cache) {
|
|
|
|
++nocache;
|
2007-03-21 16:37:30 +01:00
|
|
|
continue;
|
2007-10-16 01:15:24 +02:00
|
|
|
}
|
2005-10-27 02:34:39 +02:00
|
|
|
c = tor_malloc(sizeof(sized_chunk_t));
|
2006-01-12 19:04:17 +01:00
|
|
|
c->bytes = body;
|
2007-09-26 18:19:44 +02:00
|
|
|
c->len = sd->signed_descriptor_len + sd->annotations_len;
|
2007-10-24 17:45:45 +02:00
|
|
|
total_expected_len += c->len;
|
2005-10-27 02:34:39 +02:00
|
|
|
smartlist_add(chunk_list, c);
|
2012-07-17 15:33:38 +02:00
|
|
|
} SMARTLIST_FOREACH_END(sd);
|
2007-04-30 21:48:39 +02:00
|
|
|
|
2013-09-01 01:24:07 +02:00
|
|
|
if (write_chunks_to_file(fname_tmp, chunk_list, 1, 1)<0) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_FS, "Error writing router store to disk.");
|
2005-09-13 08:21:10 +02:00
|
|
|
goto done;
|
|
|
|
}
|
2007-04-30 21:48:39 +02:00
|
|
|
|
2006-06-22 09:01:54 +02:00
|
|
|
/* Our mmap is now invalid. */
|
2007-08-31 17:08:37 +02:00
|
|
|
if (store->mmap) {
|
2014-03-18 20:39:02 +01:00
|
|
|
int res = tor_munmap_file(store->mmap);
|
2007-08-31 17:08:37 +02:00
|
|
|
store->mmap = NULL;
|
2014-03-18 20:39:02 +01:00
|
|
|
if (res != 0) {
|
2014-03-18 19:04:20 +01:00
|
|
|
log_warn(LD_FS, "Unable to munmap route store in %s", fname);
|
|
|
|
}
|
2006-06-22 09:01:54 +02:00
|
|
|
}
|
2007-04-30 21:48:39 +02:00
|
|
|
|
2006-12-07 16:07:33 +01:00
|
|
|
if (replace_file(fname_tmp, fname)<0) {
|
2008-03-17 18:21:12 +01:00
|
|
|
log_warn(LD_FS, "Error replacing old router store: %s", strerror(errno));
|
2006-12-07 16:07:33 +01:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2007-10-24 17:45:45 +02:00
|
|
|
errno = 0;
|
2007-08-31 17:08:37 +02:00
|
|
|
store->mmap = tor_mmap_file(fname);
|
2007-10-16 01:15:24 +02:00
|
|
|
if (! store->mmap) {
|
2007-10-24 17:45:45 +02:00
|
|
|
if (errno == ERANGE) {
|
|
|
|
/* empty store.*/
|
|
|
|
if (total_expected_len) {
|
|
|
|
log_warn(LD_FS, "We wrote some bytes to a new descriptor file at '%s',"
|
|
|
|
" but when we went to mmap it, it was empty!", fname);
|
|
|
|
} else if (had_any) {
|
2008-02-06 00:20:44 +01:00
|
|
|
log_info(LD_FS, "We just removed every descriptor in '%s'. This is "
|
|
|
|
"okay if we're just starting up after a long time. "
|
|
|
|
"Otherwise, it's a bug.", fname);
|
2007-10-24 17:45:45 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log_warn(LD_FS, "Unable to mmap new descriptor file at '%s'.",fname);
|
|
|
|
}
|
2007-10-16 01:15:24 +02:00
|
|
|
}
|
2006-06-22 09:01:54 +02:00
|
|
|
|
2007-05-20 07:15:53 +02:00
|
|
|
log_info(LD_DIR, "Reconstructing pointers into cache");
|
|
|
|
|
2006-06-27 17:52:51 +02:00
|
|
|
offset = 0;
|
2012-07-17 15:33:38 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(signed_descriptors, signed_descriptor_t *, sd) {
|
2007-04-30 21:48:39 +02:00
|
|
|
if (sd->do_not_cache)
|
2007-03-21 16:37:30 +01:00
|
|
|
continue;
|
2006-04-29 20:42:26 +02:00
|
|
|
sd->saved_location = SAVED_IN_CACHE;
|
2007-08-31 17:08:37 +02:00
|
|
|
if (store->mmap) {
|
2006-06-27 17:52:51 +02:00
|
|
|
tor_free(sd->signed_descriptor_body); // sets it to null
|
2006-06-28 17:39:02 +02:00
|
|
|
sd->saved_offset = offset;
|
2006-06-27 17:52:51 +02:00
|
|
|
}
|
2007-09-27 22:46:30 +02:00
|
|
|
offset += sd->signed_descriptor_len + sd->annotations_len;
|
2007-04-30 21:48:39 +02:00
|
|
|
signed_descriptor_get_body(sd); /* reconstruct and assert */
|
2012-07-17 15:33:38 +02:00
|
|
|
} SMARTLIST_FOREACH_END(sd);
|
2005-09-13 08:21:10 +02:00
|
|
|
|
2007-10-17 18:55:44 +02:00
|
|
|
tor_free(fname);
|
2017-11-14 22:49:41 +01:00
|
|
|
fname = get_cachedir_fname_suffix(store->fname_base, ".new");
|
2006-07-10 00:28:12 +02:00
|
|
|
write_str_to_file(fname, "", 1);
|
2005-09-13 08:21:10 +02:00
|
|
|
|
|
|
|
r = 0;
|
2007-08-31 17:08:37 +02:00
|
|
|
store->store_len = (size_t) offset;
|
|
|
|
store->journal_len = 0;
|
|
|
|
store->bytes_dropped = 0;
|
2005-09-13 08:21:10 +02:00
|
|
|
done:
|
2009-12-12 08:07:59 +01:00
|
|
|
smartlist_free(signed_descriptors);
|
2005-09-13 08:21:10 +02:00
|
|
|
tor_free(fname);
|
2007-07-15 10:34:32 +02:00
|
|
|
tor_free(fname_tmp);
|
2009-01-14 07:36:30 +01:00
|
|
|
if (chunk_list) {
|
|
|
|
SMARTLIST_FOREACH(chunk_list, sized_chunk_t *, c, tor_free(c));
|
|
|
|
smartlist_free(chunk_list);
|
|
|
|
}
|
2007-04-30 16:26:38 +02:00
|
|
|
|
2005-09-13 08:21:10 +02:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/** Helper: Reload a cache file and its associated journal, setting metadata
|
|
|
|
* appropriately. If <b>extrainfo</b> is true, reload the extrainfo store;
|
|
|
|
* else reload the router descriptor store. */
|
2007-04-30 21:48:39 +02:00
|
|
|
static int
|
2007-08-31 17:08:37 +02:00
|
|
|
router_reload_router_list_impl(desc_store_t *store)
|
2005-09-15 07:19:38 +02:00
|
|
|
{
|
2013-04-01 17:13:00 +02:00
|
|
|
char *fname = NULL, *contents = NULL;
|
2007-05-22 04:20:52 +02:00
|
|
|
struct stat st;
|
2007-08-31 17:08:37 +02:00
|
|
|
int extrainfo = (store->type == EXTRAINFO_STORE);
|
|
|
|
store->journal_len = store->store_len = 0;
|
2006-08-27 04:07:54 +02:00
|
|
|
|
2017-11-14 22:49:41 +01:00
|
|
|
fname = get_cachedir_fname(store->fname_base);
|
2006-08-27 04:07:54 +02:00
|
|
|
|
2014-03-18 19:04:20 +01:00
|
|
|
if (store->mmap) {
|
|
|
|
/* get rid of it first */
|
2014-03-18 20:39:02 +01:00
|
|
|
int res = tor_munmap_file(store->mmap);
|
|
|
|
store->mmap = NULL;
|
|
|
|
if (res != 0) {
|
2014-03-18 19:04:20 +01:00
|
|
|
log_warn(LD_FS, "Failed to munmap %s", fname);
|
|
|
|
tor_free(fname);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2007-04-30 21:48:39 +02:00
|
|
|
|
2007-08-31 17:08:37 +02:00
|
|
|
store->mmap = tor_mmap_file(fname);
|
|
|
|
if (store->mmap) {
|
|
|
|
store->store_len = store->mmap->size;
|
2007-04-30 21:48:39 +02:00
|
|
|
if (extrainfo)
|
2007-08-31 17:08:37 +02:00
|
|
|
router_load_extrainfo_from_string(store->mmap->data,
|
|
|
|
store->mmap->data+store->mmap->size,
|
2007-09-21 08:14:36 +02:00
|
|
|
SAVED_IN_CACHE, NULL, 0);
|
2007-04-30 21:48:39 +02:00
|
|
|
else
|
2007-08-31 17:08:37 +02:00
|
|
|
router_load_routers_from_string(store->mmap->data,
|
|
|
|
store->mmap->data+store->mmap->size,
|
2007-09-27 22:46:30 +02:00
|
|
|
SAVED_IN_CACHE, NULL, 0, NULL);
|
2005-09-15 07:19:38 +02:00
|
|
|
}
|
2006-06-22 09:01:54 +02:00
|
|
|
|
2007-10-17 18:55:44 +02:00
|
|
|
tor_free(fname);
|
2017-11-14 22:49:41 +01:00
|
|
|
fname = get_cachedir_fname_suffix(store->fname_base, ".new");
|
2014-10-19 08:48:07 +02:00
|
|
|
/* don't load empty files - we wouldn't get any data, even if we tried */
|
2007-02-12 22:39:44 +01:00
|
|
|
if (file_status(fname) == FN_FILE)
|
2007-05-22 04:20:52 +02:00
|
|
|
contents = read_file_to_str(fname, RFTS_BIN|RFTS_IGNORE_MISSING, &st);
|
2006-06-22 09:01:54 +02:00
|
|
|
if (contents) {
|
2007-04-30 21:48:39 +02:00
|
|
|
if (extrainfo)
|
2007-09-21 08:14:36 +02:00
|
|
|
router_load_extrainfo_from_string(contents, NULL,SAVED_IN_JOURNAL,
|
|
|
|
NULL, 0);
|
2018-09-19 20:10:15 +02:00
|
|
|
else
|
|
|
|
router_load_routers_from_string(contents, NULL, SAVED_IN_JOURNAL,
|
|
|
|
NULL, 0, NULL);
|
|
|
|
store->journal_len = (size_t) st.st_size;
|
|
|
|
tor_free(contents);
|
2012-09-12 18:44:18 +02:00
|
|
|
}
|
|
|
|
|
2018-09-19 20:10:15 +02:00
|
|
|
tor_free(fname);
|
2012-09-12 18:44:18 +02:00
|
|
|
|
2018-09-19 20:10:15 +02:00
|
|
|
if (store->journal_len) {
|
|
|
|
/* Always clear the journal on startup.*/
|
|
|
|
router_rebuild_store(RRS_FORCE, store);
|
|
|
|
} else if (!extrainfo) {
|
|
|
|
/* Don't cache expired routers. (This is in an else because
|
|
|
|
* router_rebuild_store() also calls remove_old_routers().) */
|
|
|
|
routerlist_remove_old_routers();
|
2007-01-03 07:33:03 +01:00
|
|
|
}
|
|
|
|
|
2018-09-19 20:10:15 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2011-03-11 09:09:24 +01:00
|
|
|
|
2018-09-19 20:10:15 +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)
|
|
|
|
{
|
|
|
|
routerlist_t *rl = router_get_routerlist();
|
|
|
|
if (router_reload_router_list_impl(&rl->desc_store))
|
|
|
|
return -1;
|
|
|
|
if (router_reload_router_list_impl(&rl->extrainfo_store))
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
2011-03-11 09:09:24 +01:00
|
|
|
|
2018-09-19 20:10:15 +02:00
|
|
|
/* When iterating through the routerlist, can OR address/port preference
|
|
|
|
* and reachability checks be skipped?
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
router_skip_or_reachability(const or_options_t *options, int try_ip_pref)
|
|
|
|
{
|
|
|
|
/* Servers always have and prefer IPv4.
|
|
|
|
* And if clients are checking against the firewall for reachability only,
|
|
|
|
* but there's no firewall, don't bother checking */
|
|
|
|
return server_mode(options) || (!try_ip_pref && !firewall_is_fascist_or());
|
2004-10-12 17:55:20 +02:00
|
|
|
}
|
|
|
|
|
2018-09-19 20:10:15 +02:00
|
|
|
/* When iterating through the routerlist, can Dir address/port preference
|
|
|
|
* and reachability checks be skipped?
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
router_skip_dir_reachability(const or_options_t *options, int try_ip_pref)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2018-09-19 20:10:15 +02:00
|
|
|
/* Servers always have and prefer IPv4.
|
|
|
|
* And if clients are checking against the firewall for reachability only,
|
|
|
|
* but there's no firewall, don't bother checking */
|
|
|
|
return server_mode(options) || (!try_ip_pref && !firewall_is_fascist_dir());
|
2005-10-05 07:03:52 +02:00
|
|
|
}
|
|
|
|
|
2010-09-15 04:19:00 +02:00
|
|
|
/** Return true iff r1 and r2 have the same address and OR port. */
|
|
|
|
int
|
2012-05-11 10:22:45 +02:00
|
|
|
routers_have_same_or_addrs(const routerinfo_t *r1, const routerinfo_t *r2)
|
2010-09-15 04:19:00 +02:00
|
|
|
{
|
2012-05-09 17:58:01 +02:00
|
|
|
return r1->addr == r2->addr && r1->or_port == r2->or_port &&
|
|
|
|
tor_addr_eq(&r1->ipv6_addr, &r2->ipv6_addr) &&
|
|
|
|
r1->ipv6_orport == r2->ipv6_orport;
|
2010-09-15 04:19:00 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2014-09-15 14:28:11 +02:00
|
|
|
void
|
2017-04-25 19:22:35 +02:00
|
|
|
router_add_running_nodes_to_smartlist(smartlist_t *sl, int need_uptime,
|
|
|
|
int need_capacity, int need_guard,
|
|
|
|
int need_desc, int pref_addr,
|
|
|
|
int direct_conn)
|
2016-01-21 03:30:57 +01:00
|
|
|
{
|
|
|
|
const int check_reach = !router_skip_or_reachability(get_options(),
|
|
|
|
pref_addr);
|
|
|
|
/* XXXX MOVE */
|
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) {
|
2017-04-25 19:22:35 +02:00
|
|
|
if (!node->is_running || !node->is_valid)
|
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
|
|
|
continue;
|
2018-04-16 17:40:14 +02:00
|
|
|
if (need_desc && !node_has_preferred_descriptor(node, direct_conn))
|
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
|
|
|
continue;
|
|
|
|
if (node->ri && node->ri->purpose != ROUTER_PURPOSE_GENERAL)
|
|
|
|
continue;
|
|
|
|
if (node_is_unreliable(node, need_uptime, need_capacity, need_guard))
|
|
|
|
continue;
|
2016-10-27 06:53:49 +02:00
|
|
|
/* Don't choose nodes if we are certain they can't do EXTEND2 cells */
|
|
|
|
if (node->rs && !routerstatus_version_supports_extend2_cells(node->rs, 1))
|
2016-07-07 04:58:47 +02:00
|
|
|
continue;
|
2016-10-27 06:53:49 +02:00
|
|
|
/* Don't choose nodes if we are certain they can't do ntor. */
|
2016-07-07 04:58:47 +02:00
|
|
|
if ((node->ri || node->md) && !node_has_curve25519_onion_key(node))
|
|
|
|
continue;
|
2016-01-03 14:35:22 +01:00
|
|
|
/* Choose a node with an OR address that matches the firewall rules */
|
2016-08-18 04:02:33 +02:00
|
|
|
if (direct_conn && check_reach &&
|
2016-08-29 21:02:11 +02:00
|
|
|
!fascist_firewall_allows_node(node,
|
|
|
|
FIREWALL_OR_CONNECTION,
|
|
|
|
pref_addr))
|
2015-12-18 01:28:54 +01:00
|
|
|
continue;
|
2003-12-03 11:28:51 +01:00
|
|
|
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
smartlist_add(sl, (void *)node);
|
|
|
|
} SMARTLIST_FOREACH_END(node);
|
2003-12-03 11:28:51 +01:00
|
|
|
}
|
|
|
|
|
2005-06-21 01:04:13 +02:00
|
|
|
/** Look through the routerlist until we find a router that has my key.
|
|
|
|
Return it. */
|
2010-09-29 06:38:32 +02:00
|
|
|
const routerinfo_t *
|
2005-06-11 20:52:12 +02:00
|
|
|
routerlist_find_my_routerinfo(void)
|
|
|
|
{
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!routerlist)
|
2004-08-18 12:32:50 +02:00
|
|
|
return NULL;
|
|
|
|
|
2005-09-13 17:32:03 +02:00
|
|
|
SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, router,
|
|
|
|
{
|
2004-11-28 10:05:49 +01:00
|
|
|
if (router_is_me(router))
|
2004-08-18 12:32:50 +02:00
|
|
|
return router;
|
2005-09-13 17:32:03 +02:00
|
|
|
});
|
2004-08-18 12:32:50 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-03-28 14:01:58 +02:00
|
|
|
/** Return the smaller of the router's configured BandwidthRate
|
|
|
|
* and its advertised capacity. */
|
|
|
|
uint32_t
|
2010-09-29 06:38:32 +02:00
|
|
|
router_get_advertised_bandwidth(const routerinfo_t *router)
|
2006-03-28 14:01:58 +02:00
|
|
|
{
|
|
|
|
if (router->bandwidthcapacity < router->bandwidthrate)
|
|
|
|
return router->bandwidthcapacity;
|
|
|
|
return router->bandwidthrate;
|
|
|
|
}
|
|
|
|
|
2007-02-16 21:01:02 +01:00
|
|
|
/** Do not weight any declared bandwidth more than this much when picking
|
|
|
|
* routers by bandwidth. */
|
2007-08-24 10:01:47 +02:00
|
|
|
#define DEFAULT_MAX_BELIEVABLE_BANDWIDTH 10000000 /* 10 MB/sec */
|
|
|
|
|
2008-08-14 14:37:00 +02:00
|
|
|
/** Return the smaller of the router's configured BandwidthRate
|
|
|
|
* and its advertised capacity, capped by max-believe-bw. */
|
|
|
|
uint32_t
|
2010-09-29 06:38:32 +02:00
|
|
|
router_get_advertised_bandwidth_capped(const routerinfo_t *router)
|
2008-08-14 14:37:00 +02:00
|
|
|
{
|
|
|
|
uint32_t result = router->bandwidthcapacity;
|
|
|
|
if (result > router->bandwidthrate)
|
|
|
|
result = router->bandwidthrate;
|
|
|
|
if (result > DEFAULT_MAX_BELIEVABLE_BANDWIDTH)
|
|
|
|
result = DEFAULT_MAX_BELIEVABLE_BANDWIDTH;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-06-17 16:41:45 +02:00
|
|
|
if (base16_decode(digest_out, DIGEST_LEN,
|
|
|
|
hexdigest, HEX_DIGEST_LEN) != DIGEST_LEN)
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Helper: Return true iff the <b>identity_digest</b> and <b>nickname</b>
|
|
|
|
* combination of a router, encoded in hexadecimal, matches <b>hexdigest</b>
|
|
|
|
* (which is optionally prefixed with a single dollar sign). Return false if
|
|
|
|
* <b>hexdigest</b> is malformed, or it doesn't match. */
|
2012-09-13 18:57:46 +02:00
|
|
|
int
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
hex_digest_nickname_matches(const char *hexdigest, const char *identity_digest,
|
2017-08-23 00:47:57 +02:00
|
|
|
const char *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
|
|
|
{
|
|
|
|
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
|
|
|
|
2017-08-23 00:47:57 +02:00
|
|
|
if (nn_char == '=') {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nn_char == '~') {
|
|
|
|
if (!nickname) // XXX This seems wrong. -NM
|
2011-07-01 17:43:34 +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 (strcasecmp(nn_buf, nickname))
|
2006-10-03 20:58:40 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-05-11 22:39:45 +02:00
|
|
|
return tor_memeq(digest, identity_digest, DIGEST_LEN);
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
}
|
|
|
|
|
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 ||
|
2016-06-17 16:41:45 +02:00
|
|
|
base16_decode(digest,DIGEST_LEN,hexdigest,HEX_DIGEST_LEN) != DIGEST_LEN)
|
2007-04-27 12:26:09 +02:00
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-10-14 17:49:51 +02:00
|
|
|
/** As router_get_by_id_digest,but return a pointer that you're allowed to
|
2010-09-29 06:38:32 +02:00
|
|
|
* modify */
|
2005-06-11 20:52:12 +02:00
|
|
|
routerinfo_t *
|
2010-09-29 06:38:32 +02:00
|
|
|
router_get_mutable_by_digest(const char *digest)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2004-07-01 03:16:59 +02:00
|
|
|
tor_assert(digest);
|
2005-06-21 01:04:13 +02:00
|
|
|
|
2005-02-27 10:47:01 +01:00
|
|
|
if (!routerlist) return NULL;
|
2003-09-25 07:17:11 +02:00
|
|
|
|
2005-10-18 22:13:09 +02:00
|
|
|
// routerlist_assert_ok(routerlist);
|
2004-04-06 00:22:42 +02:00
|
|
|
|
2007-06-12 00:19:40 +02:00
|
|
|
return rimap_get(routerlist->identity_map, digest);
|
2003-09-25 07:17:11 +02:00
|
|
|
}
|
2003-09-11 22:32:15 +02:00
|
|
|
|
2010-09-29 06:38:32 +02:00
|
|
|
/** Return the router in our routerlist whose 20-byte key digest
|
|
|
|
* is <b>digest</b>. Return NULL if no such router is known. */
|
|
|
|
const routerinfo_t *
|
2010-10-14 17:49:51 +02:00
|
|
|
router_get_by_id_digest(const char *digest)
|
2010-09-29 06:38:32 +02:00
|
|
|
{
|
|
|
|
return router_get_mutable_by_digest(digest);
|
|
|
|
}
|
|
|
|
|
2005-10-14 06:56:20 +02:00
|
|
|
/** Return the router in our routerlist whose 20-byte descriptor
|
|
|
|
* is <b>digest</b>. Return NULL if no such router is known. */
|
2005-11-05 21:15:27 +01:00
|
|
|
signed_descriptor_t *
|
2005-10-14 06:56:20 +02:00
|
|
|
router_get_by_descriptor_digest(const char *digest)
|
|
|
|
{
|
|
|
|
tor_assert(digest);
|
|
|
|
|
|
|
|
if (!routerlist) return NULL;
|
|
|
|
|
2007-06-12 00:19:40 +02:00
|
|
|
return sdmap_get(routerlist->desc_digest_map, digest);
|
2005-10-14 06:56:20 +02:00
|
|
|
}
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/** Return the signed descriptor for the router in our routerlist whose
|
|
|
|
* 20-byte extra-info digest is <b>digest</b>. Return NULL if no such router
|
|
|
|
* is known. */
|
2014-10-07 15:20:44 +02:00
|
|
|
MOCK_IMPL(signed_descriptor_t *,
|
|
|
|
router_get_by_extrainfo_digest,(const char *digest))
|
2007-05-18 23:19:53 +02:00
|
|
|
{
|
|
|
|
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. */
|
2017-04-22 02:17:27 +02:00
|
|
|
MOCK_IMPL(signed_descriptor_t *,
|
|
|
|
extrainfo_get_by_descriptor_digest,(const char *digest))
|
2007-04-16 20:39:39 +02:00
|
|
|
{
|
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.");
|
2017-10-19 19:42:28 +02:00
|
|
|
exit(1); // XXXX bad exit: should recover.
|
2007-05-20 07:15:53 +02:00
|
|
|
}
|
2006-06-22 09:01:54 +02:00
|
|
|
}
|
2007-05-20 07:15:53 +02:00
|
|
|
if (!r) /* no mmap, or not in cache. */
|
2007-09-26 18:19:44 +02:00
|
|
|
r = desc->signed_descriptor_body +
|
|
|
|
(with_annotations ? 0 : desc->annotations_len);
|
2007-05-20 07:15:53 +02:00
|
|
|
|
2006-06-22 09:01:54 +02:00
|
|
|
tor_assert(r);
|
2007-09-27 22:46:30 +02:00
|
|
|
if (!with_annotations) {
|
2011-05-10 22:58:38 +02:00
|
|
|
if (fast_memcmp("router ", r, 7) && fast_memcmp("extra-info ", r, 11)) {
|
2008-01-16 06:27:19 +01:00
|
|
|
char *cp = tor_strndup(r, 64);
|
2008-12-10 04:01:26 +01:00
|
|
|
log_err(LD_DIR, "descriptor at %p begins with unexpected string %s. "
|
|
|
|
"Is another process running in our data directory? Exiting.",
|
2008-01-16 06:27:19 +01:00
|
|
|
desc, escaped(cp));
|
2017-10-19 19:42:28 +02:00
|
|
|
exit(1); // XXXX bad exit: should recover.
|
2007-09-27 22:46:30 +02:00
|
|
|
}
|
2007-05-20 07:15:53 +02:00
|
|
|
}
|
2006-06-22 09:01:54 +02:00
|
|
|
|
|
|
|
return r;
|
2006-01-12 19:04:17 +01:00
|
|
|
}
|
|
|
|
|
2007-10-04 18:21:58 +02:00
|
|
|
/** Return a pointer to the signed textual representation of a descriptor.
|
|
|
|
* The returned string is not guaranteed to be NUL-terminated: the string's
|
|
|
|
* length will be in desc-\>signed_descriptor_len.
|
|
|
|
*
|
|
|
|
* The caller must not free the string returned.
|
|
|
|
*/
|
2007-09-26 18:19:44 +02:00
|
|
|
const char *
|
2010-09-29 06:38:32 +02:00
|
|
|
signed_descriptor_get_body(const signed_descriptor_t *desc)
|
2007-09-26 18:19:44 +02:00
|
|
|
{
|
|
|
|
return signed_descriptor_get_body_impl(desc, 0);
|
|
|
|
}
|
|
|
|
|
2007-12-06 08:15:06 +01:00
|
|
|
/** As signed_descriptor_get_body(), but points to the beginning of the
|
|
|
|
* annotations section rather than the beginning of the descriptor. */
|
|
|
|
const char *
|
2010-09-29 06:38:32 +02:00
|
|
|
signed_descriptor_get_annotations(const signed_descriptor_t *desc)
|
2007-12-06 08:15:06 +01:00
|
|
|
{
|
|
|
|
return signed_descriptor_get_body_impl(desc, 1);
|
|
|
|
}
|
|
|
|
|
2005-10-18 19:43:54 +02:00
|
|
|
/** Return the current list of all known routers. */
|
|
|
|
routerlist_t *
|
|
|
|
router_get_routerlist(void)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2007-08-31 17:08:37 +02:00
|
|
|
if (PREDICT_UNLIKELY(!routerlist)) {
|
2005-10-18 19:43:54 +02:00
|
|
|
routerlist = tor_malloc_zero(sizeof(routerlist_t));
|
2012-01-18 21:53:30 +01:00
|
|
|
routerlist->routers = smartlist_new();
|
|
|
|
routerlist->old_routers = smartlist_new();
|
2007-06-12 00:19:40 +02:00
|
|
|
routerlist->identity_map = rimap_new();
|
|
|
|
routerlist->desc_digest_map = sdmap_new();
|
|
|
|
routerlist->desc_by_eid_map = sdmap_new();
|
|
|
|
routerlist->extra_info_map = eimap_new();
|
2007-08-31 17:08:37 +02:00
|
|
|
|
2007-09-27 22:46:24 +02:00
|
|
|
routerlist->desc_store.fname_base = "cached-descriptors";
|
2007-08-31 17:08:37 +02:00
|
|
|
routerlist->extrainfo_store.fname_base = "cached-extrainfo";
|
|
|
|
|
|
|
|
routerlist->desc_store.type = ROUTER_STORE;
|
|
|
|
routerlist->extrainfo_store.type = EXTRAINFO_STORE;
|
|
|
|
|
|
|
|
routerlist->desc_store.description = "router descriptors";
|
|
|
|
routerlist->extrainfo_store.description = "extra-info documents";
|
2005-10-18 19:43:54 +02:00
|
|
|
}
|
|
|
|
return routerlist;
|
2002-09-26 14:09:10 +02:00
|
|
|
}
|
|
|
|
|
2004-05-10 06:34:48 +02:00
|
|
|
/** Free all storage held by <b>router</b>. */
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
2017-11-21 15:37:47 +01:00
|
|
|
routerinfo_free_(routerinfo_t *router)
|
2002-06-27 00:45:49 +02:00
|
|
|
{
|
2003-05-09 04:00:33 +02:00
|
|
|
if (!router)
|
2002-06-27 00:45:49 +02:00
|
|
|
return;
|
|
|
|
|
2006-01-12 19:04:17 +01:00
|
|
|
tor_free(router->cache_info.signed_descriptor_body);
|
2003-10-21 11:48:17 +02:00
|
|
|
tor_free(router->nickname);
|
2004-04-07 23:36:03 +02:00
|
|
|
tor_free(router->platform);
|
2016-08-19 20:10:20 +02:00
|
|
|
tor_free(router->protocol_list);
|
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)
|
2018-08-23 20:05:42 +02:00
|
|
|
tor_free(router->onion_pkey);
|
2012-12-04 21:58:18 +01:00
|
|
|
tor_free(router->onion_curve25519_pkey);
|
2003-09-25 07:17:11 +02:00
|
|
|
if (router->identity_pkey)
|
2012-01-18 21:53:30 +01:00
|
|
|
crypto_pk_free(router->identity_pkey);
|
2016-05-17 19:14:04 +02:00
|
|
|
tor_cert_free(router->cache_info.signing_key_cert);
|
2004-10-15 03:58:11 +02:00
|
|
|
if (router->declared_family) {
|
|
|
|
SMARTLIST_FOREACH(router->declared_family, char *, s, tor_free(s));
|
|
|
|
smartlist_free(router->declared_family);
|
|
|
|
}
|
2008-01-02 05:43:44 +01:00
|
|
|
addr_policy_list_free(router->exit_policy);
|
2012-10-25 03:59:55 +02:00
|
|
|
short_policy_free(router->ipv6_exit_policy);
|
2007-05-19 20:12:31 +02:00
|
|
|
|
|
|
|
memset(router, 77, sizeof(routerinfo_t));
|
|
|
|
|
2004-09-29 08:52:36 +02:00
|
|
|
tor_free(router);
|
2002-09-24 12:43:57 +02:00
|
|
|
}
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/** Release all storage held by <b>extrainfo</b> */
|
2007-04-16 06:17:58 +02:00
|
|
|
void
|
2017-11-21 15:37:47 +01:00
|
|
|
extrainfo_free_(extrainfo_t *extrainfo)
|
2007-04-16 06:17:58 +02:00
|
|
|
{
|
|
|
|
if (!extrainfo)
|
|
|
|
return;
|
2016-05-17 19:14:04 +02:00
|
|
|
tor_cert_free(extrainfo->cache_info.signing_key_cert);
|
2007-04-16 06:17:58 +02:00
|
|
|
tor_free(extrainfo->cache_info.signed_descriptor_body);
|
|
|
|
tor_free(extrainfo->pending_sig);
|
2007-05-19 20:12:31 +02:00
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
memset(extrainfo, 88, sizeof(extrainfo_t)); /* debug bad memory usage */
|
2007-04-16 06:17:58 +02:00
|
|
|
tor_free(extrainfo);
|
|
|
|
}
|
|
|
|
|
2017-12-08 16:21:12 +01:00
|
|
|
#define signed_descriptor_free(val) \
|
|
|
|
FREE_AND_NULL(signed_descriptor_t, signed_descriptor_free_, (val))
|
|
|
|
|
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
|
2017-12-08 16:21:12 +01:00
|
|
|
signed_descriptor_free_(signed_descriptor_t *sd)
|
2005-11-05 21:15:27 +01:00
|
|
|
{
|
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);
|
2016-05-20 13:59:09 +02:00
|
|
|
tor_cert_free(sd->signing_key_cert);
|
2007-05-19 20:12:31 +02:00
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
memset(sd, 99, sizeof(signed_descriptor_t)); /* Debug bad mem usage */
|
2005-11-05 21:15:27 +01:00
|
|
|
tor_free(sd);
|
|
|
|
}
|
|
|
|
|
2016-12-08 17:46:52 +01:00
|
|
|
/** Reset the given signed descriptor <b>sd</b> by freeing the allocated
|
|
|
|
* memory inside the object and by zeroing its content. */
|
|
|
|
static void
|
|
|
|
signed_descriptor_reset(signed_descriptor_t *sd)
|
|
|
|
{
|
|
|
|
tor_assert(sd);
|
|
|
|
tor_free(sd->signed_descriptor_body);
|
|
|
|
tor_cert_free(sd->signing_key_cert);
|
|
|
|
memset(sd, 0, sizeof(*sd));
|
|
|
|
}
|
|
|
|
|
2016-05-25 17:52:52 +02:00
|
|
|
/** Copy src into dest, and steal all references inside src so that when
|
|
|
|
* we free src, we don't mess up dest. */
|
|
|
|
static void
|
|
|
|
signed_descriptor_move(signed_descriptor_t *dest,
|
|
|
|
signed_descriptor_t *src)
|
|
|
|
{
|
|
|
|
tor_assert(dest != src);
|
2016-12-08 17:46:52 +01:00
|
|
|
/* Cleanup destination object before overwriting it.*/
|
|
|
|
signed_descriptor_reset(dest);
|
2016-05-25 17:52:52 +02:00
|
|
|
memcpy(dest, src, sizeof(signed_descriptor_t));
|
|
|
|
src->signed_descriptor_body = NULL;
|
|
|
|
src->signing_key_cert = NULL;
|
|
|
|
dest->routerlist_index = -1;
|
|
|
|
}
|
|
|
|
|
2010-11-02 16:20:09 +01:00
|
|
|
/** Extract a signed_descriptor_t from a general routerinfo, and free the
|
|
|
|
* routerinfo.
|
2005-12-27 06:26:03 +01:00
|
|
|
*/
|
2005-11-05 21:15:27 +01:00
|
|
|
static signed_descriptor_t *
|
|
|
|
signed_descriptor_from_routerinfo(routerinfo_t *ri)
|
|
|
|
{
|
2010-11-02 16:20:09 +01:00
|
|
|
signed_descriptor_t *sd;
|
|
|
|
tor_assert(ri->purpose == ROUTER_PURPOSE_GENERAL);
|
|
|
|
sd = tor_malloc_zero(sizeof(signed_descriptor_t));
|
2016-05-25 17:52:52 +02:00
|
|
|
signed_descriptor_move(sd, &ri->cache_info);
|
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
|
2017-11-21 15:37:47 +01:00
|
|
|
extrainfo_free_void(void *e)
|
2007-04-16 23:37:21 +02:00
|
|
|
{
|
2017-12-04 21:09:18 +01:00
|
|
|
extrainfo_free_(e);
|
2007-04-16 23:37:21 +02:00
|
|
|
}
|
|
|
|
|
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
|
2017-11-21 15:37:47 +01:00
|
|
|
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);
|
2017-11-21 15:37:47 +01:00
|
|
|
eimap_free(rl->extra_info_map, extrainfo_free_void);
|
2004-04-07 21:46:27 +02:00
|
|
|
SMARTLIST_FOREACH(rl->routers, routerinfo_t *, r,
|
|
|
|
routerinfo_free(r));
|
2005-11-05 21:15:27 +01:00
|
|
|
SMARTLIST_FOREACH(rl->old_routers, signed_descriptor_t *, sd,
|
|
|
|
signed_descriptor_free(sd));
|
2004-04-07 21:46:27 +02:00
|
|
|
smartlist_free(rl->routers);
|
2005-10-27 02:34:39 +02:00
|
|
|
smartlist_free(rl->old_routers);
|
2014-03-31 17:43:11 +02:00
|
|
|
if (rl->desc_store.mmap) {
|
2014-03-18 20:39:02 +01:00
|
|
|
int res = tor_munmap_file(routerlist->desc_store.mmap);
|
2014-03-18 19:04:20 +01:00
|
|
|
if (res != 0) {
|
|
|
|
log_warn(LD_FS, "Failed to munmap routerlist->desc_store.mmap");
|
|
|
|
}
|
|
|
|
}
|
2014-03-31 17:43:11 +02:00
|
|
|
if (rl->extrainfo_store.mmap) {
|
2014-03-18 20:39:02 +01:00
|
|
|
int res = tor_munmap_file(routerlist->extrainfo_store.mmap);
|
2014-03-18 19:04:20 +01:00
|
|
|
if (res != 0) {
|
|
|
|
log_warn(LD_FS, "Failed to munmap routerlist->extrainfo_store.mmap");
|
|
|
|
}
|
|
|
|
}
|
2004-04-07 21:46:27 +02:00
|
|
|
tor_free(rl);
|
2006-08-31 20:46:46 +02:00
|
|
|
|
|
|
|
router_dir_info_changed();
|
2003-05-06 19:38:16 +02:00
|
|
|
}
|
|
|
|
|
2007-10-18 16:19:51 +02:00
|
|
|
/** Log information about how much memory is being used for routerlist,
|
|
|
|
* at log level <b>severity</b>. */
|
2005-11-23 07:00:58 +01:00
|
|
|
void
|
|
|
|
dump_routerlist_mem_usage(int severity)
|
|
|
|
{
|
|
|
|
uint64_t livedescs = 0;
|
|
|
|
uint64_t olddescs = 0;
|
|
|
|
if (!routerlist)
|
|
|
|
return;
|
|
|
|
SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, r,
|
|
|
|
livedescs += r->cache_info.signed_descriptor_len);
|
|
|
|
SMARTLIST_FOREACH(routerlist->old_routers, signed_descriptor_t *, sd,
|
2005-11-23 08:06:36 +01:00
|
|
|
olddescs += sd->signed_descriptor_len);
|
2005-11-23 07:00:58 +01:00
|
|
|
|
2013-02-01 21:43:37 +01:00
|
|
|
tor_log(severity, LD_DIR,
|
2018-07-03 16:40:59 +02:00
|
|
|
"In %d live descriptors: %"PRIu64" bytes. "
|
|
|
|
"In %d old descriptors: %"PRIu64" bytes.",
|
|
|
|
smartlist_len(routerlist->routers), (livedescs),
|
|
|
|
smartlist_len(routerlist->old_routers), (olddescs));
|
2005-11-23 07:00:58 +01:00
|
|
|
}
|
|
|
|
|
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>. */
|
2015-12-10 16:19:43 +01:00
|
|
|
static inline int
|
2012-10-12 18:22:13 +02:00
|
|
|
routerlist_find_elt_(smartlist_t *sl, void *ri, int idx)
|
2005-10-27 02:34:39 +02:00
|
|
|
{
|
2007-07-02 22:17:12 +02:00
|
|
|
if (idx < 0) {
|
2005-10-27 02:34:39 +02:00
|
|
|
idx = -1;
|
|
|
|
SMARTLIST_FOREACH(sl, routerinfo_t *, r,
|
|
|
|
if (r == ri) {
|
|
|
|
idx = r_sl_idx;
|
|
|
|
break;
|
|
|
|
});
|
2007-07-02 22:17:12 +02:00
|
|
|
} else {
|
|
|
|
tor_assert(idx < smartlist_len(sl));
|
|
|
|
tor_assert(smartlist_get(sl, idx) == ri);
|
|
|
|
};
|
2005-10-28 01:06:09 +02:00
|
|
|
return idx;
|
2005-10-27 02:34:39 +02:00
|
|
|
}
|
|
|
|
|
2005-10-18 19:43:54 +02:00
|
|
|
/** Insert an item <b>ri</b> into the routerlist <b>rl</b>, updating indices
|
2007-03-11 21:34:44 +01:00
|
|
|
* as needed. There must be no previous member of <b>rl</b> with the same
|
2007-03-11 21:52:07 +01:00
|
|
|
* identity digest as <b>ri</b>: If there is, call routerlist_replace
|
2007-03-11 21:34:44 +01:00
|
|
|
* instead.
|
|
|
|
*/
|
2005-10-18 19:43:54 +02:00
|
|
|
static void
|
|
|
|
routerlist_insert(routerlist_t *rl, routerinfo_t *ri)
|
|
|
|
{
|
2007-03-11 21:34:44 +01:00
|
|
|
routerinfo_t *ri_old;
|
2010-11-10 20:55:00 +01:00
|
|
|
signed_descriptor_t *sd_old;
|
2007-05-19 02:32:00 +02:00
|
|
|
{
|
2010-09-29 06:38:32 +02:00
|
|
|
const routerinfo_t *ri_generated = router_get_my_routerinfo();
|
2007-05-19 02:32:00 +02:00
|
|
|
tor_assert(ri_generated != ri);
|
|
|
|
}
|
2007-11-07 18:11:23 +01:00
|
|
|
tor_assert(ri->cache_info.routerlist_index == -1);
|
2007-05-19 02:32:00 +02:00
|
|
|
|
2007-06-12 00:19:40 +02:00
|
|
|
ri_old = rimap_set(rl->identity_map, ri->cache_info.identity_digest, ri);
|
2007-03-11 21:34:44 +01:00
|
|
|
tor_assert(!ri_old);
|
2010-11-10 20:55:00 +01:00
|
|
|
|
|
|
|
sd_old = sdmap_set(rl->desc_digest_map,
|
|
|
|
ri->cache_info.signed_descriptor_digest,
|
|
|
|
&(ri->cache_info));
|
|
|
|
if (sd_old) {
|
2012-05-16 16:51:02 +02:00
|
|
|
int idx = sd_old->routerlist_index;
|
|
|
|
sd_old->routerlist_index = -1;
|
|
|
|
smartlist_del(rl->old_routers, idx);
|
|
|
|
if (idx < smartlist_len(rl->old_routers)) {
|
|
|
|
signed_descriptor_t *d = smartlist_get(rl->old_routers, idx);
|
|
|
|
d->routerlist_index = idx;
|
|
|
|
}
|
2010-11-10 20:55:00 +01:00
|
|
|
rl->desc_store.bytes_dropped += sd_old->signed_descriptor_len;
|
|
|
|
sdmap_remove(rl->desc_by_eid_map, sd_old->extra_info_digest);
|
|
|
|
signed_descriptor_free(sd_old);
|
|
|
|
}
|
|
|
|
|
2007-05-18 23:19:53 +02:00
|
|
|
if (!tor_digest_is_zero(ri->cache_info.extra_info_digest))
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_set(rl->desc_by_eid_map, ri->cache_info.extra_info_digest,
|
|
|
|
&ri->cache_info);
|
2005-10-18 19:43:54 +02:00
|
|
|
smartlist_add(rl->routers, ri);
|
2007-11-07 18:11:23 +01:00
|
|
|
ri->cache_info.routerlist_index = smartlist_len(rl->routers) - 1;
|
2012-07-18 02:00:19 +02:00
|
|
|
nodelist_set_routerinfo(ri, NULL);
|
2006-08-15 05:54:09 +02:00
|
|
|
router_dir_info_changed();
|
2007-05-19 20:31:04 +02:00
|
|
|
#ifdef DEBUG_ROUTERLIST
|
|
|
|
routerlist_assert_ok(rl);
|
|
|
|
#endif
|
2005-10-18 19:43:54 +02:00
|
|
|
}
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/** Adds the extrainfo_t <b>ei</b> to the routerlist <b>rl</b>, if there is a
|
2014-10-13 20:11:27 +02:00
|
|
|
* corresponding router in rl-\>routers or rl-\>old_routers. Return the status
|
|
|
|
* of inserting <b>ei</b>. Free <b>ei</b> if it isn't inserted. */
|
|
|
|
MOCK_IMPL(STATIC was_router_added_t,
|
2015-01-07 19:11:06 +01:00
|
|
|
extrainfo_insert,(routerlist_t *rl, extrainfo_t *ei, int warn_if_incompatible))
|
2007-04-16 23:37:21 +02:00
|
|
|
{
|
2014-10-13 20:11:27 +02:00
|
|
|
was_router_added_t r;
|
2014-11-09 16:41:18 +01:00
|
|
|
const char *compatibility_error_msg;
|
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;
|
2015-02-18 19:41:00 +01:00
|
|
|
const int severity = warn_if_incompatible ? LOG_WARN : LOG_INFO;
|
2007-05-19 22:08:44 +02:00
|
|
|
|
2007-05-20 22:16:45 +02:00
|
|
|
{
|
|
|
|
extrainfo_t *ei_generated = router_get_my_extrainfo();
|
|
|
|
tor_assert(ei_generated != ei);
|
|
|
|
}
|
|
|
|
|
2007-05-19 22:08:44 +02:00
|
|
|
if (!ri) {
|
|
|
|
/* This router is unknown; we can't even verify the signature. Give up.*/
|
2014-10-13 20:11:27 +02:00
|
|
|
r = ROUTER_NOT_IN_CONSENSUS;
|
2007-05-19 22:08:44 +02:00
|
|
|
goto done;
|
|
|
|
}
|
2015-02-18 19:05:15 +01:00
|
|
|
if (! sd) {
|
|
|
|
/* The extrainfo router doesn't have a known routerdesc to attach it to.
|
|
|
|
* This just won't work. */;
|
|
|
|
static ratelim_t no_sd_ratelim = RATELIM_INIT(1800);
|
|
|
|
r = ROUTER_BAD_EI;
|
2015-02-18 19:41:00 +01:00
|
|
|
log_fn_ratelim(&no_sd_ratelim, severity, LD_BUG,
|
2015-02-18 19:05:15 +01:00
|
|
|
"No entry found in extrainfo map.");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (tor_memneq(ei->cache_info.signed_descriptor_digest,
|
|
|
|
sd->extra_info_digest, DIGEST_LEN)) {
|
|
|
|
static ratelim_t digest_mismatch_ratelim = RATELIM_INIT(1800);
|
|
|
|
/* The sd we got from the map doesn't match the digest we used to look
|
|
|
|
* it up. This makes no sense. */
|
|
|
|
r = ROUTER_BAD_EI;
|
2015-02-18 19:41:00 +01:00
|
|
|
log_fn_ratelim(&digest_mismatch_ratelim, severity, LD_BUG,
|
|
|
|
"Mismatch in digest in extrainfo map.");
|
2015-02-18 19:05:15 +01:00
|
|
|
goto done;
|
|
|
|
}
|
2016-05-17 19:24:01 +02:00
|
|
|
if (routerinfo_incompatible_with_extrainfo(ri->identity_pkey, ei, sd,
|
2014-11-09 16:41:18 +01:00
|
|
|
&compatibility_error_msg)) {
|
2015-02-18 19:05:15 +01:00
|
|
|
char d1[HEX_DIGEST_LEN+1], d2[HEX_DIGEST_LEN+1];
|
2014-10-26 19:09:03 +01:00
|
|
|
r = (ri->cache_info.extrainfo_is_bogus) ?
|
|
|
|
ROUTER_BAD_EI : ROUTER_NOT_IN_CONSENSUS;
|
2014-11-09 16:41:18 +01:00
|
|
|
|
2015-02-18 19:05:15 +01:00
|
|
|
base16_encode(d1, sizeof(d1), ri->cache_info.identity_digest, DIGEST_LEN);
|
|
|
|
base16_encode(d2, sizeof(d2), ei->cache_info.identity_digest, DIGEST_LEN);
|
|
|
|
|
2015-01-07 19:11:06 +01:00
|
|
|
log_fn(severity,LD_DIR,
|
2015-02-18 19:05:15 +01:00
|
|
|
"router info incompatible with extra info (ri id: %s, ei id %s, "
|
|
|
|
"reason: %s)", d1, d2, compatibility_error_msg);
|
2014-11-09 16:41:18 +01:00
|
|
|
|
2007-10-21 02:08:35 +02:00
|
|
|
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);
|
2014-10-13 20:11:27 +02:00
|
|
|
r = ROUTER_ADDED_SUCCESSFULLY;
|
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:
|
2014-10-13 20:11:27 +02:00
|
|
|
if (r != ROUTER_ADDED_SUCCESSFULLY)
|
2007-05-19 22:08:44 +02:00
|
|
|
extrainfo_free(ei);
|
|
|
|
|
2007-05-19 20:31:04 +02:00
|
|
|
#ifdef DEBUG_ROUTERLIST
|
|
|
|
routerlist_assert_ok(rl);
|
|
|
|
#endif
|
2007-04-30 21:48:39 +02:00
|
|
|
return r;
|
2007-04-16 23:37:21 +02:00
|
|
|
}
|
|
|
|
|
2007-12-01 05:58:53 +01:00
|
|
|
#define should_cache_old_descriptors() \
|
|
|
|
directory_caches_dir_info(get_options())
|
2007-10-09 22:44:47 +02:00
|
|
|
|
2007-02-26 06:33:17 +01:00
|
|
|
/** If we're a directory cache and routerlist <b>rl</b> doesn't have
|
|
|
|
* a copy of router <b>ri</b> yet, add it to the list of old (not
|
|
|
|
* recommended but still served) descriptors. Else free it. */
|
2005-10-27 02:34:39 +02:00
|
|
|
static void
|
|
|
|
routerlist_insert_old(routerlist_t *rl, routerinfo_t *ri)
|
|
|
|
{
|
2007-05-19 02:32:00 +02:00
|
|
|
{
|
2010-09-29 06:38:32 +02:00
|
|
|
const routerinfo_t *ri_generated = router_get_my_routerinfo();
|
2007-05-19 02:32:00 +02:00
|
|
|
tor_assert(ri_generated != ri);
|
|
|
|
}
|
2007-11-07 18:11:23 +01:00
|
|
|
tor_assert(ri->cache_info.routerlist_index == -1);
|
2007-06-02 14:44:57 +02:00
|
|
|
|
2007-10-09 22:44:47 +02:00
|
|
|
if (should_cache_old_descriptors() &&
|
2007-03-21 16:37:30 +01:00
|
|
|
ri->purpose == ROUTER_PURPOSE_GENERAL &&
|
2007-06-12 00:19:40 +02:00
|
|
|
!sdmap_get(rl->desc_digest_map,
|
|
|
|
ri->cache_info.signed_descriptor_digest)) {
|
2005-11-05 21:15:27 +01:00
|
|
|
signed_descriptor_t *sd = signed_descriptor_from_routerinfo(ri);
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_set(rl->desc_digest_map, sd->signed_descriptor_digest, sd);
|
2005-11-05 21:15:27 +01:00
|
|
|
smartlist_add(rl->old_routers, sd);
|
2007-11-07 18:11:23 +01:00
|
|
|
sd->routerlist_index = smartlist_len(rl->old_routers)-1;
|
2007-05-18 23:19:53 +02:00
|
|
|
if (!tor_digest_is_zero(sd->extra_info_digest))
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_set(rl->desc_by_eid_map, sd->extra_info_digest, sd);
|
2005-10-27 02:34:39 +02:00
|
|
|
} else {
|
|
|
|
routerinfo_free(ri);
|
|
|
|
}
|
2007-05-19 20:31:04 +02:00
|
|
|
#ifdef DEBUG_ROUTERLIST
|
|
|
|
routerlist_assert_ok(rl);
|
|
|
|
#endif
|
2005-10-27 02:34:39 +02:00
|
|
|
}
|
|
|
|
|
2006-03-24 22:11:14 +01:00
|
|
|
/** Remove an item <b>ri</b> from the routerlist <b>rl</b>, updating indices
|
2005-10-18 19:43:54 +02:00
|
|
|
* as needed. If <b>idx</b> is nonnegative and smartlist_get(rl->routers,
|
|
|
|
* idx) == ri, we don't need to do a linear search over the list to decide
|
2005-11-01 04:48:51 +01:00
|
|
|
* which to remove. We fill the gap in rl->routers with a later element in
|
2007-03-21 16:37:30 +01:00
|
|
|
* the list, if any exists. <b>ri</b> is freed.
|
|
|
|
*
|
2007-03-26 16:08:29 +02:00
|
|
|
* If <b>make_old</b> is true, instead of deleting the router, we try adding
|
|
|
|
* it to rl->old_routers. */
|
2005-10-18 19:43:54 +02:00
|
|
|
void
|
2009-06-20 07:52:59 +02:00
|
|
|
routerlist_remove(routerlist_t *rl, routerinfo_t *ri, int make_old, time_t now)
|
2005-10-18 19:43:54 +02:00
|
|
|
{
|
2005-10-18 22:13:09 +02:00
|
|
|
routerinfo_t *ri_tmp;
|
2007-04-16 23:37:21 +02:00
|
|
|
extrainfo_t *ei_tmp;
|
2007-11-07 18:11:23 +01:00
|
|
|
int idx = ri->cache_info.routerlist_index;
|
2007-07-18 16:02:47 +02:00
|
|
|
tor_assert(0 <= idx && idx < smartlist_len(rl->routers));
|
|
|
|
tor_assert(smartlist_get(rl->routers, idx) == ri);
|
|
|
|
|
2010-09-28 19:29:31 +02:00
|
|
|
nodelist_remove_routerinfo(ri);
|
|
|
|
|
2009-06-20 07:52:59 +02:00
|
|
|
/* make sure the rephist module knows that it's not running */
|
|
|
|
rep_hist_note_router_unreachable(ri->cache_info.identity_digest, now);
|
|
|
|
|
2007-11-07 18:11:23 +01:00
|
|
|
ri->cache_info.routerlist_index = -1;
|
2005-10-18 19:43:54 +02:00
|
|
|
smartlist_del(rl->routers, idx);
|
2006-09-20 01:18:30 +02:00
|
|
|
if (idx < smartlist_len(rl->routers)) {
|
|
|
|
routerinfo_t *r = smartlist_get(rl->routers, idx);
|
2007-11-07 18:11:23 +01:00
|
|
|
r->cache_info.routerlist_index = idx;
|
2006-09-20 01:18:30 +02:00
|
|
|
}
|
|
|
|
|
2007-06-12 00:19:40 +02:00
|
|
|
ri_tmp = rimap_remove(rl->identity_map, ri->cache_info.identity_digest);
|
2006-08-15 05:54:09 +02:00
|
|
|
router_dir_info_changed();
|
2005-10-18 22:13:09 +02:00
|
|
|
tor_assert(ri_tmp == ri);
|
2007-04-16 23:37:21 +02:00
|
|
|
|
2007-10-09 22:44:47 +02:00
|
|
|
if (make_old && should_cache_old_descriptors() &&
|
2007-03-21 16:37:30 +01:00
|
|
|
ri->purpose == ROUTER_PURPOSE_GENERAL) {
|
2005-11-23 08:24:59 +01:00
|
|
|
signed_descriptor_t *sd;
|
|
|
|
sd = signed_descriptor_from_routerinfo(ri);
|
2005-11-05 21:15:27 +01:00
|
|
|
smartlist_add(rl->old_routers, sd);
|
2007-11-07 18:11:23 +01:00
|
|
|
sd->routerlist_index = smartlist_len(rl->old_routers)-1;
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_set(rl->desc_digest_map, sd->signed_descriptor_digest, sd);
|
2007-05-19 20:48:53 +02:00
|
|
|
if (!tor_digest_is_zero(sd->extra_info_digest))
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_set(rl->desc_by_eid_map, sd->extra_info_digest, sd);
|
2005-10-27 02:34:39 +02:00
|
|
|
} else {
|
2007-06-12 00:19:40 +02:00
|
|
|
signed_descriptor_t *sd_tmp;
|
|
|
|
sd_tmp = sdmap_remove(rl->desc_digest_map,
|
|
|
|
ri->cache_info.signed_descriptor_digest);
|
|
|
|
tor_assert(sd_tmp == &(ri->cache_info));
|
2007-10-22 18:32:01 +02:00
|
|
|
rl->desc_store.bytes_dropped += ri->cache_info.signed_descriptor_len;
|
2007-06-12 00:19:40 +02:00
|
|
|
ei_tmp = eimap_remove(rl->extra_info_map,
|
|
|
|
ri->cache_info.extra_info_digest);
|
2007-04-30 21:48:39 +02:00
|
|
|
if (ei_tmp) {
|
2007-08-31 17:08:37 +02:00
|
|
|
rl->extrainfo_store.bytes_dropped +=
|
2007-04-30 21:48:39 +02:00
|
|
|
ei_tmp->cache_info.signed_descriptor_len;
|
2007-04-16 23:37:21 +02:00
|
|
|
extrainfo_free(ei_tmp);
|
2007-04-30 21:48:39 +02:00
|
|
|
}
|
2007-05-18 23:19:53 +02:00
|
|
|
if (!tor_digest_is_zero(ri->cache_info.extra_info_digest))
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_remove(rl->desc_by_eid_map, ri->cache_info.extra_info_digest);
|
2007-05-18 23:19:53 +02:00
|
|
|
routerinfo_free(ri);
|
2005-10-27 02:34:39 +02:00
|
|
|
}
|
2007-05-19 20:31:04 +02:00
|
|
|
#ifdef DEBUG_ROUTERLIST
|
|
|
|
routerlist_assert_ok(rl);
|
|
|
|
#endif
|
2005-10-27 02:34:39 +02:00
|
|
|
}
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/** Remove a signed_descriptor_t <b>sd</b> from <b>rl</b>-\>old_routers, and
|
2008-12-22 15:56:16 +01:00
|
|
|
* adjust <b>rl</b> as appropriate. <b>idx</b> is -1, or the index of
|
2007-05-29 19:31:13 +02:00
|
|
|
* <b>sd</b>. */
|
2005-11-01 18:34:17 +01:00
|
|
|
static void
|
2005-11-05 21:15:27 +01:00
|
|
|
routerlist_remove_old(routerlist_t *rl, signed_descriptor_t *sd, int idx)
|
2005-10-27 02:34:39 +02:00
|
|
|
{
|
2005-11-05 21:15:27 +01:00
|
|
|
signed_descriptor_t *sd_tmp;
|
2007-04-16 23:37:21 +02:00
|
|
|
extrainfo_t *ei_tmp;
|
2007-08-31 17:08:37 +02:00
|
|
|
desc_store_t *store;
|
2007-11-07 18:11:23 +01:00
|
|
|
if (idx == -1) {
|
|
|
|
idx = sd->routerlist_index;
|
|
|
|
}
|
2007-07-18 16:02:47 +02:00
|
|
|
tor_assert(0 <= idx && idx < smartlist_len(rl->old_routers));
|
2008-02-15 20:20:53 +01:00
|
|
|
/* XXXX edmanm's bridge relay triggered the following assert while
|
|
|
|
* running 0.2.0.12-alpha. If anybody triggers this again, see if we
|
2008-06-18 22:14:25 +02:00
|
|
|
* can get a backtrace. */
|
2007-07-18 16:02:47 +02:00
|
|
|
tor_assert(smartlist_get(rl->old_routers, idx) == sd);
|
2007-11-07 18:11:23 +01:00
|
|
|
tor_assert(idx == sd->routerlist_index);
|
2007-07-18 16:02:47 +02:00
|
|
|
|
2007-11-07 18:11:23 +01:00
|
|
|
sd->routerlist_index = -1;
|
2005-10-27 02:34:39 +02:00
|
|
|
smartlist_del(rl->old_routers, idx);
|
2007-11-07 18:11:23 +01:00
|
|
|
if (idx < smartlist_len(rl->old_routers)) {
|
|
|
|
signed_descriptor_t *d = smartlist_get(rl->old_routers, idx);
|
|
|
|
d->routerlist_index = idx;
|
|
|
|
}
|
2007-06-12 00:19:40 +02:00
|
|
|
sd_tmp = sdmap_remove(rl->desc_digest_map,
|
|
|
|
sd->signed_descriptor_digest);
|
2005-11-05 21:15:27 +01:00
|
|
|
tor_assert(sd_tmp == sd);
|
2007-08-31 17:08:37 +02:00
|
|
|
store = desc_get_store(rl, sd);
|
|
|
|
if (store)
|
|
|
|
store->bytes_dropped += sd->signed_descriptor_len;
|
2007-04-16 23:37:21 +02:00
|
|
|
|
2007-06-12 00:19:40 +02:00
|
|
|
ei_tmp = eimap_remove(rl->extra_info_map,
|
|
|
|
sd->extra_info_digest);
|
2007-04-30 21:48:39 +02:00
|
|
|
if (ei_tmp) {
|
2007-08-31 17:08:37 +02:00
|
|
|
rl->extrainfo_store.bytes_dropped +=
|
2007-04-30 21:48:39 +02:00
|
|
|
ei_tmp->cache_info.signed_descriptor_len;
|
2007-04-16 23:37:21 +02:00
|
|
|
extrainfo_free(ei_tmp);
|
2007-04-30 21:48:39 +02:00
|
|
|
}
|
2007-05-18 23:19:53 +02:00
|
|
|
if (!tor_digest_is_zero(sd->extra_info_digest))
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_remove(rl->desc_by_eid_map, sd->extra_info_digest);
|
2007-04-16 23:37:21 +02:00
|
|
|
|
2007-05-18 23:19:53 +02:00
|
|
|
signed_descriptor_free(sd);
|
2007-05-19 20:31:04 +02:00
|
|
|
#ifdef DEBUG_ROUTERLIST
|
|
|
|
routerlist_assert_ok(rl);
|
|
|
|
#endif
|
2005-10-18 19:43:54 +02:00
|
|
|
}
|
|
|
|
|
2005-10-19 05:08:50 +02:00
|
|
|
/** Remove <b>ri_old</b> from the routerlist <b>rl</b>, and replace it with
|
|
|
|
* <b>ri_new</b>, updating all index info. If <b>idx</b> is nonnegative and
|
|
|
|
* smartlist_get(rl->routers, idx) == ri, we don't need to do a linear
|
|
|
|
* search over the list to decide which to remove. We put ri_new in the same
|
2007-03-21 16:37:30 +01:00
|
|
|
* index as ri_old, if possible. ri is freed as appropriate.
|
|
|
|
*
|
2007-12-18 22:37:58 +01:00
|
|
|
* If should_cache_descriptors() is true, instead of deleting the router,
|
|
|
|
* we add it to rl->old_routers. */
|
2005-10-19 05:02:28 +02:00
|
|
|
static void
|
|
|
|
routerlist_replace(routerlist_t *rl, routerinfo_t *ri_old,
|
2007-07-06 20:40:39 +02:00
|
|
|
routerinfo_t *ri_new)
|
2005-10-19 05:02:28 +02:00
|
|
|
{
|
2007-07-06 20:40:39 +02:00
|
|
|
int idx;
|
2010-11-10 20:55:00 +01:00
|
|
|
int same_descriptors;
|
2007-07-06 20:40:39 +02:00
|
|
|
|
2007-03-11 21:34:44 +01:00
|
|
|
routerinfo_t *ri_tmp;
|
2007-04-16 23:37:21 +02:00
|
|
|
extrainfo_t *ei_tmp;
|
2007-05-19 02:32:00 +02:00
|
|
|
{
|
2010-09-29 06:38:32 +02:00
|
|
|
const routerinfo_t *ri_generated = router_get_my_routerinfo();
|
2007-05-19 02:32:00 +02:00
|
|
|
tor_assert(ri_generated != ri_new);
|
|
|
|
}
|
2005-10-28 01:06:09 +02:00
|
|
|
tor_assert(ri_old != ri_new);
|
2007-11-07 18:11:23 +01:00
|
|
|
tor_assert(ri_new->cache_info.routerlist_index == -1);
|
2007-06-02 14:44:57 +02:00
|
|
|
|
2007-11-07 18:11:23 +01:00
|
|
|
idx = ri_old->cache_info.routerlist_index;
|
2007-07-06 20:40:39 +02:00
|
|
|
tor_assert(0 <= idx && idx < smartlist_len(rl->routers));
|
|
|
|
tor_assert(smartlist_get(rl->routers, idx) == ri_old);
|
|
|
|
|
2012-07-18 02:00:19 +02:00
|
|
|
{
|
|
|
|
routerinfo_t *ri_old_tmp=NULL;
|
|
|
|
nodelist_set_routerinfo(ri_new, &ri_old_tmp);
|
|
|
|
tor_assert(ri_old == ri_old_tmp);
|
|
|
|
}
|
2010-09-28 19:29:31 +02:00
|
|
|
|
2006-08-15 05:54:09 +02:00
|
|
|
router_dir_info_changed();
|
2005-10-19 05:02:28 +02:00
|
|
|
if (idx >= 0) {
|
|
|
|
smartlist_set(rl->routers, idx, ri_new);
|
2007-11-07 18:11:23 +01:00
|
|
|
ri_old->cache_info.routerlist_index = -1;
|
|
|
|
ri_new->cache_info.routerlist_index = idx;
|
2007-07-12 19:37:47 +02:00
|
|
|
/* Check that ri_old is not in rl->routers anymore: */
|
2012-10-12 18:22:13 +02:00
|
|
|
tor_assert( routerlist_find_elt_(rl->routers, ri_old, -1) == -1 );
|
2005-10-19 05:02:28 +02:00
|
|
|
} else {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_BUG, "Appending entry from routerlist_replace.");
|
2005-10-28 01:06:09 +02:00
|
|
|
routerlist_insert(rl, ri_new);
|
|
|
|
return;
|
2005-10-19 05:02:28 +02:00
|
|
|
}
|
2011-05-10 22:58:38 +02:00
|
|
|
if (tor_memneq(ri_old->cache_info.identity_digest,
|
2005-12-14 21:40:40 +01:00
|
|
|
ri_new->cache_info.identity_digest, DIGEST_LEN)) {
|
2005-10-19 05:02:28 +02:00
|
|
|
/* digests don't match; digestmap_set won't replace */
|
2007-06-12 00:19:40 +02:00
|
|
|
rimap_remove(rl->identity_map, ri_old->cache_info.identity_digest);
|
2005-10-19 05:02:28 +02:00
|
|
|
}
|
2007-06-12 00:19:40 +02:00
|
|
|
ri_tmp = rimap_set(rl->identity_map,
|
|
|
|
ri_new->cache_info.identity_digest, ri_new);
|
2007-03-11 22:10:54 +01:00
|
|
|
tor_assert(!ri_tmp || ri_tmp == ri_old);
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_set(rl->desc_digest_map,
|
|
|
|
ri_new->cache_info.signed_descriptor_digest,
|
|
|
|
&(ri_new->cache_info));
|
2005-10-27 02:34:39 +02:00
|
|
|
|
2007-06-02 16:24:23 +02:00
|
|
|
if (!tor_digest_is_zero(ri_new->cache_info.extra_info_digest)) {
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_set(rl->desc_by_eid_map, ri_new->cache_info.extra_info_digest,
|
|
|
|
&ri_new->cache_info);
|
2007-06-02 16:24:23 +02:00
|
|
|
}
|
2007-05-19 20:31:04 +02:00
|
|
|
|
2011-05-11 22:27:27 +02:00
|
|
|
same_descriptors = tor_memeq(ri_old->cache_info.signed_descriptor_digest,
|
2010-11-10 20:55:00 +01:00
|
|
|
ri_new->cache_info.signed_descriptor_digest,
|
|
|
|
DIGEST_LEN);
|
|
|
|
|
2007-10-09 22:44:47 +02:00
|
|
|
if (should_cache_old_descriptors() &&
|
2010-11-10 20:55:00 +01:00
|
|
|
ri_old->purpose == ROUTER_PURPOSE_GENERAL &&
|
|
|
|
!same_descriptors) {
|
|
|
|
/* ri_old is going to become a signed_descriptor_t and go into
|
|
|
|
* old_routers */
|
2005-11-05 21:15:27 +01:00
|
|
|
signed_descriptor_t *sd = signed_descriptor_from_routerinfo(ri_old);
|
|
|
|
smartlist_add(rl->old_routers, sd);
|
2007-11-07 18:11:23 +01:00
|
|
|
sd->routerlist_index = smartlist_len(rl->old_routers)-1;
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_set(rl->desc_digest_map, sd->signed_descriptor_digest, sd);
|
2007-05-19 20:31:04 +02:00
|
|
|
if (!tor_digest_is_zero(sd->extra_info_digest))
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_set(rl->desc_by_eid_map, sd->extra_info_digest, sd);
|
2005-10-27 02:34:39 +02:00
|
|
|
} else {
|
2010-11-10 20:55:00 +01:00
|
|
|
/* We're dropping ri_old. */
|
|
|
|
if (!same_descriptors) {
|
|
|
|
/* digests don't match; The sdmap_set above didn't replace */
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_remove(rl->desc_digest_map,
|
|
|
|
ri_old->cache_info.signed_descriptor_digest);
|
2007-04-16 23:37:21 +02:00
|
|
|
|
2011-05-11 22:32:30 +02:00
|
|
|
if (tor_memneq(ri_old->cache_info.extra_info_digest,
|
2010-11-10 20:55:00 +01:00
|
|
|
ri_new->cache_info.extra_info_digest, DIGEST_LEN)) {
|
|
|
|
ei_tmp = eimap_remove(rl->extra_info_map,
|
|
|
|
ri_old->cache_info.extra_info_digest);
|
|
|
|
if (ei_tmp) {
|
|
|
|
rl->extrainfo_store.bytes_dropped +=
|
|
|
|
ei_tmp->cache_info.signed_descriptor_len;
|
|
|
|
extrainfo_free(ei_tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!tor_digest_is_zero(ri_old->cache_info.extra_info_digest)) {
|
|
|
|
sdmap_remove(rl->desc_by_eid_map,
|
|
|
|
ri_old->cache_info.extra_info_digest);
|
|
|
|
}
|
2007-06-02 16:24:23 +02:00
|
|
|
}
|
2007-10-22 18:32:01 +02:00
|
|
|
rl->desc_store.bytes_dropped += ri_old->cache_info.signed_descriptor_len;
|
2005-10-27 02:34:39 +02:00
|
|
|
routerinfo_free(ri_old);
|
|
|
|
}
|
2007-05-19 20:31:04 +02:00
|
|
|
#ifdef DEBUG_ROUTERLIST
|
|
|
|
routerlist_assert_ok(rl);
|
|
|
|
#endif
|
2005-10-19 05:02:28 +02:00
|
|
|
}
|
|
|
|
|
2007-12-19 04:55:40 +01:00
|
|
|
/** Extract the descriptor <b>sd</b> from old_routerlist, and re-parse
|
|
|
|
* it as a fresh routerinfo_t. */
|
2007-11-07 16:19:53 +01:00
|
|
|
static routerinfo_t *
|
2007-11-07 18:11:23 +01:00
|
|
|
routerlist_reparse_old(routerlist_t *rl, signed_descriptor_t *sd)
|
2007-11-07 16:19:53 +01:00
|
|
|
{
|
|
|
|
routerinfo_t *ri;
|
|
|
|
const char *body;
|
|
|
|
|
2007-12-19 04:55:40 +01:00
|
|
|
body = signed_descriptor_get_annotations(sd);
|
2007-11-07 16:19:53 +01:00
|
|
|
|
2007-12-19 04:55:40 +01:00
|
|
|
ri = router_parse_entry_from_string(body,
|
|
|
|
body+sd->signed_descriptor_len+sd->annotations_len,
|
Treat unparseable (micro)descriptors and extrainfos as undownloadable
One pain point in evolving the Tor design and implementing has been
adding code that makes clients reject directory documents that they
previously would have accepted, if those descriptors actually exist.
When this happened, the clients would get the document, reject it,
and then decide to try downloading it again, ad infinitum. This
problem becomes particularly obnoxious with authorities, since if
some authorities accept a descriptor that others don't, the ones
that don't accept it would go crazy trying to re-fetch it over and
over. (See for example ticket #9286.)
This patch tries to solve this problem by tracking, if a descriptor
isn't parseable, what its digest was, and whether it is invalid
because of some flaw that applies to the portion containing the
digest. (This excludes RSA signature problems: RSA signatures
aren't included in the digest. This means that a directory
authority can still put another directory authority into a loop by
mentioning a descriptor, and then serving that descriptor with an
invalid RSA signatures. But that would also make the misbehaving
directory authority get DoSed by the server it's attacking, so it's
not much of an issue.)
We already have a mechanism to mark something undownloadable with
downloadstatus_mark_impossible(); we use that here for
microdescriptors, extrainfos, and router descriptors.
Unit tests to follow in another patch.
Closes ticket #11243.
2014-10-03 16:55:50 +02:00
|
|
|
0, 1, NULL, NULL);
|
2007-11-07 16:19:53 +01:00
|
|
|
if (!ri)
|
|
|
|
return NULL;
|
2016-05-25 17:52:52 +02:00
|
|
|
signed_descriptor_move(&ri->cache_info, sd);
|
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;
|
|
|
|
}
|
|
|
|
|
2015-01-26 20:49:48 +01:00
|
|
|
/** Free all memory held by the routerlist module.
|
|
|
|
* Note: Calling routerlist_free_all() should always be paired with
|
|
|
|
* a call to nodelist_free_all(). These should only be called during
|
|
|
|
* cleanup.
|
|
|
|
*/
|
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;
|
2018-09-21 14:10:39 +02:00
|
|
|
dirlist_free_all();
|
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;
|
|
|
|
}
|
2018-09-19 20:10:15 +02:00
|
|
|
authcert_free_all();
|
2005-09-02 22:37:31 +02:00
|
|
|
}
|
|
|
|
|
2005-10-05 04:20:46 +02:00
|
|
|
/** Forget that we have issued any router-related warnings, so that we'll
|
|
|
|
* warn again if we see the same errors. */
|
|
|
|
void
|
|
|
|
routerlist_reset_warnings(void)
|
|
|
|
{
|
|
|
|
if (!warned_nicknames)
|
2012-01-18 21:53:30 +01:00
|
|
|
warned_nicknames = smartlist_new();
|
2005-10-05 04:20:46 +02:00
|
|
|
SMARTLIST_FOREACH(warned_nicknames, char *, cp, tor_free(cp));
|
|
|
|
smartlist_clear(warned_nicknames); /* now the list is empty. */
|
|
|
|
|
2007-10-08 23:21:50 +02:00
|
|
|
networkstatus_reset_warnings();
|
2005-10-05 04:20:46 +02:00
|
|
|
}
|
|
|
|
|
2014-07-09 18:20:41 +02:00
|
|
|
/** Return 1 if the signed descriptor of this router is older than
|
|
|
|
* <b>seconds</b> seconds. Otherwise return 0. */
|
2014-06-24 20:12:51 +02:00
|
|
|
MOCK_IMPL(int,
|
2014-07-09 18:20:41 +02:00
|
|
|
router_descriptor_is_older_than,(const routerinfo_t *router, int seconds))
|
2014-06-24 20:12:51 +02:00
|
|
|
{
|
2014-10-13 20:11:27 +02:00
|
|
|
return router->cache_info.published_on < approx_time() - seconds;
|
2014-06-24 20:12:51 +02:00
|
|
|
}
|
|
|
|
|
2004-05-17 22:31:01 +02:00
|
|
|
/** Add <b>router</b> to the routerlist, if we don't already have it. Replace
|
2005-09-15 08:15:31 +02:00
|
|
|
* older entries (if any) with the same key. Note: Callers should not hold
|
2005-05-14 07:01:41 +02:00
|
|
|
* their pointers to <b>router</b> if this function fails; <b>router</b>
|
2007-07-16 02:11:03 +02:00
|
|
|
* will either be inserted into the routerlist or freed. Similarly, even
|
|
|
|
* if this call succeeds, they should not hold their pointers to
|
|
|
|
* <b>router</b> after subsequent calls with other routerinfo's -- they
|
|
|
|
* might cause the original routerinfo to get freed.
|
2005-04-03 00:02:13 +02:00
|
|
|
*
|
2008-12-25 16:37:47 +01:00
|
|
|
* Returns the status for the operation. Might set *<b>msg</b> if it wants
|
|
|
|
* the poster of the router to know something.
|
2005-09-15 02:51:42 +02:00
|
|
|
*
|
2006-01-04 05:42:10 +01:00
|
|
|
* If <b>from_cache</b>, this descriptor came from our disk cache. If
|
|
|
|
* <b>from_fetch</b>, we received it in response to a request we made.
|
|
|
|
* (If both are false, that means it was uploaded to us as an auth dir
|
|
|
|
* server or via the controller.)
|
|
|
|
*
|
2005-09-15 08:15:31 +02:00
|
|
|
* This function should be called *after*
|
2007-10-16 01:15:24 +02:00
|
|
|
* routers_update_status_from_consensus_networkstatus; subsequently, you
|
|
|
|
* should call router_rebuild_store and routerlist_descriptors_added.
|
2004-05-17 22:31:01 +02:00
|
|
|
*/
|
2008-12-17 22:50:01 +01:00
|
|
|
was_router_added_t
|
2005-09-15 07:19:38 +02:00
|
|
|
router_add_to_routerlist(routerinfo_t *router, const char **msg,
|
2006-01-04 05:42:10 +01:00
|
|
|
int from_cache, int from_fetch)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2006-03-17 06:50:41 +01:00
|
|
|
const char *id_digest;
|
2011-06-14 19:01:38 +02:00
|
|
|
const or_options_t *options = get_options();
|
2011-03-13 21:56:41 +01:00
|
|
|
int authdir = authdir_mode_handles_descs(options, router->purpose);
|
2006-03-19 02:21:59 +01:00
|
|
|
int authdir_believes_valid = 0;
|
2006-09-20 01:18:30 +02:00
|
|
|
routerinfo_t *old_router;
|
2011-07-11 16:02:24 +02:00
|
|
|
networkstatus_t *consensus =
|
|
|
|
networkstatus_get_latest_consensus_by_flavor(FLAV_NS);
|
2007-10-16 01:15:24 +02:00
|
|
|
int in_consensus = 0;
|
2004-08-07 05:38:07 +02:00
|
|
|
|
2005-08-27 01:12:13 +02:00
|
|
|
tor_assert(msg);
|
|
|
|
|
2005-10-18 22:13:09 +02:00
|
|
|
if (!routerlist)
|
|
|
|
router_get_routerlist();
|
2005-08-26 23:12:34 +02:00
|
|
|
|
2006-03-17 06:50:41 +01:00
|
|
|
id_digest = router->cache_info.identity_digest;
|
2005-10-27 02:34:39 +02:00
|
|
|
|
2010-09-29 06:38:32 +02:00
|
|
|
old_router = router_get_mutable_by_digest(id_digest);
|
2010-07-30 21:39:08 +02:00
|
|
|
|
2014-10-01 17:54:07 +02:00
|
|
|
/* Make sure that it isn't expired. */
|
|
|
|
if (router->cert_expiration_time < approx_time()) {
|
2015-08-12 18:27:45 +02:00
|
|
|
routerinfo_free(router);
|
|
|
|
*msg = "Some certs on this router are expired.";
|
2014-10-01 17:54:07 +02:00
|
|
|
return ROUTER_CERTS_EXPIRED;
|
|
|
|
}
|
|
|
|
|
2005-10-27 02:34:39 +02:00
|
|
|
/* Make sure that we haven't already got this exact descriptor. */
|
2007-06-12 00:19:40 +02:00
|
|
|
if (sdmap_get(routerlist->desc_digest_map,
|
|
|
|
router->cache_info.signed_descriptor_digest)) {
|
2010-07-30 21:39:08 +02:00
|
|
|
/* If we have this descriptor already and the new descriptor is a bridge
|
|
|
|
* descriptor, replace it. If we had a bridge descriptor before and the
|
|
|
|
* new one is not a bridge descriptor, don't replace it. */
|
2010-11-02 16:20:09 +01:00
|
|
|
|
|
|
|
/* Only members of routerlist->identity_map can be bridges; we don't
|
|
|
|
* put bridges in old_routers. */
|
|
|
|
const int was_bridge = old_router &&
|
|
|
|
old_router->purpose == ROUTER_PURPOSE_BRIDGE;
|
|
|
|
|
2010-11-16 01:38:19 +01:00
|
|
|
if (routerinfo_is_a_configured_bridge(router) &&
|
|
|
|
router->purpose == ROUTER_PURPOSE_BRIDGE &&
|
|
|
|
!was_bridge) {
|
|
|
|
log_info(LD_DIR, "Replacing non-bridge descriptor with bridge "
|
2011-05-16 03:58:46 +02:00
|
|
|
"descriptor for router %s",
|
|
|
|
router_describe(router));
|
2010-11-16 01:38:19 +01:00
|
|
|
} else {
|
2010-07-30 21:39:08 +02:00
|
|
|
log_info(LD_DIR,
|
2011-05-16 03:58:46 +02:00
|
|
|
"Dropping descriptor that we already have for router %s",
|
|
|
|
router_describe(router));
|
2010-07-30 21:39:08 +02:00
|
|
|
*msg = "Router descriptor was not new.";
|
|
|
|
routerinfo_free(router);
|
2014-11-11 19:56:40 +01:00
|
|
|
return ROUTER_IS_ALREADY_KNOWN;
|
2010-07-30 21:39:08 +02:00
|
|
|
}
|
2005-10-27 02:34:39 +02:00
|
|
|
}
|
2004-08-07 05:38:07 +02:00
|
|
|
|
2005-08-26 22:59:04 +02:00
|
|
|
if (authdir) {
|
2006-01-04 05:42:10 +01:00
|
|
|
if (authdir_wants_to_reject_router(router, msg,
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
!from_cache && !from_fetch,
|
|
|
|
&authdir_believes_valid)) {
|
2006-01-12 04:43:39 +01:00
|
|
|
tor_assert(*msg);
|
2005-10-28 20:44:51 +02:00
|
|
|
routerinfo_free(router);
|
2008-12-17 22:50:01 +01:00
|
|
|
return ROUTER_AUTHDIR_REJECTS;
|
2005-10-28 20:44:51 +02:00
|
|
|
}
|
2006-01-12 04:43:39 +01:00
|
|
|
} else if (from_fetch) {
|
2005-12-27 06:26:03 +01:00
|
|
|
/* Only check the descriptor digest against the network statuses when
|
2006-01-12 04:43:39 +01:00
|
|
|
* we are receiving in response to a fetch. */
|
2007-02-22 08:41:10 +01:00
|
|
|
|
2007-06-12 11:17:23 +02:00
|
|
|
if (!signed_desc_digest_is_recognized(&router->cache_info) &&
|
2007-07-18 12:06:03 +02:00
|
|
|
!routerinfo_is_a_configured_bridge(router)) {
|
2007-02-22 08:41:10 +01:00
|
|
|
/* We asked for it, so some networkstatus must have listed it when we
|
2007-02-24 22:21:38 +01:00
|
|
|
* did. Save it if we're a cache in case somebody else asks for it. */
|
2007-02-22 08:41:10 +01:00
|
|
|
log_info(LD_DIR,
|
2011-05-16 03:58:46 +02:00
|
|
|
"Received a no-longer-recognized descriptor for router %s",
|
|
|
|
router_describe(router));
|
2006-01-12 04:43:39 +01:00
|
|
|
*msg = "Router descriptor is not referenced by any network-status.";
|
2007-02-22 08:41:10 +01:00
|
|
|
|
2016-11-30 02:20:28 +01:00
|
|
|
/* Only journal this desc if we want to keep old descriptors */
|
2007-10-09 22:44:47 +02:00
|
|
|
if (!from_cache && should_cache_old_descriptors())
|
2007-08-31 17:08:37 +02:00
|
|
|
signed_desc_append_to_journal(&router->cache_info,
|
2007-10-22 18:32:01 +02:00
|
|
|
&routerlist->desc_store);
|
2007-02-22 08:41:10 +01:00
|
|
|
routerlist_insert_old(routerlist, router);
|
2008-12-17 22:50:01 +01:00
|
|
|
return ROUTER_NOT_IN_CONSENSUS_OR_NETWORKSTATUS;
|
2005-10-12 15:49:13 +02:00
|
|
|
}
|
2005-08-26 22:59:04 +02:00
|
|
|
}
|
|
|
|
|
2006-03-27 07:07:57 +02:00
|
|
|
/* We no longer need a router with this descriptor digest. */
|
2007-10-16 01:15:24 +02:00
|
|
|
if (consensus) {
|
2010-09-29 07:35:08 +02:00
|
|
|
routerstatus_t *rs = networkstatus_vote_find_mutable_entry(
|
|
|
|
consensus, id_digest);
|
2011-05-10 22:23:43 +02:00
|
|
|
if (rs && tor_memeq(rs->descriptor_digest,
|
2007-10-16 01:15:24 +02:00
|
|
|
router->cache_info.signed_descriptor_digest,
|
|
|
|
DIGEST_LEN)) {
|
|
|
|
in_consensus = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-16 16:25:14 +02:00
|
|
|
if (router->purpose == ROUTER_PURPOSE_GENERAL &&
|
2007-12-19 00:45:24 +01:00
|
|
|
consensus && !in_consensus && !authdir) {
|
2007-10-16 16:25:14 +02:00
|
|
|
/* If it's a general router not listed in the consensus, then don't
|
|
|
|
* consider replacing the latest router with it. */
|
2007-10-16 01:15:24 +02:00
|
|
|
if (!from_cache && should_cache_old_descriptors())
|
|
|
|
signed_desc_append_to_journal(&router->cache_info,
|
2007-10-22 18:32:01 +02:00
|
|
|
&routerlist->desc_store);
|
2007-10-16 01:15:24 +02:00
|
|
|
routerlist_insert_old(routerlist, router);
|
2008-01-17 06:25:21 +01:00
|
|
|
*msg = "Skipping router descriptor: not in consensus.";
|
2008-12-17 22:50:01 +01:00
|
|
|
return ROUTER_NOT_IN_CONSENSUS;
|
2007-10-16 01:15:24 +02:00
|
|
|
}
|
2006-03-27 07:07:57 +02:00
|
|
|
|
2011-02-08 14:08:47 +01:00
|
|
|
/* If we're reading a bridge descriptor from our cache, and we don't
|
|
|
|
* recognize it as one of our currently configured bridges, drop the
|
|
|
|
* descriptor. Otherwise we could end up using it as one of our entry
|
|
|
|
* guards even if it isn't in our Bridge config lines. */
|
|
|
|
if (router->purpose == ROUTER_PURPOSE_BRIDGE && from_cache &&
|
2011-03-13 21:56:41 +01:00
|
|
|
!authdir_mode_bridge(options) &&
|
2011-02-08 14:08:47 +01:00
|
|
|
!routerinfo_is_a_configured_bridge(router)) {
|
2011-05-16 03:58:46 +02:00
|
|
|
log_info(LD_DIR, "Dropping bridge descriptor for %s because we have "
|
|
|
|
"no bridge configured at that address.",
|
|
|
|
safe_str_client(router_describe(router)));
|
2011-02-08 14:08:47 +01:00
|
|
|
*msg = "Router descriptor was not a configured bridge.";
|
|
|
|
routerinfo_free(router);
|
2011-03-13 21:56:41 +01:00
|
|
|
return ROUTER_WAS_NOT_WANTED;
|
2011-02-08 14:08:47 +01:00
|
|
|
}
|
|
|
|
|
2006-09-20 01:18:30 +02:00
|
|
|
/* If we have a router with the same identity key, choose the newer one. */
|
|
|
|
if (old_router) {
|
2007-11-07 16:19:53 +01:00
|
|
|
if (!in_consensus && (router->cache_info.published_on <=
|
|
|
|
old_router->cache_info.published_on)) {
|
|
|
|
/* Same key, but old. This one is not listed in the consensus. */
|
2011-05-16 03:58:46 +02:00
|
|
|
log_debug(LD_DIR, "Not-new descriptor for router %s",
|
|
|
|
router_describe(router));
|
2006-09-20 01:18:30 +02:00
|
|
|
/* Only journal this desc if we'll be serving it. */
|
2007-10-09 22:44:47 +02:00
|
|
|
if (!from_cache && should_cache_old_descriptors())
|
2007-08-31 17:08:37 +02:00
|
|
|
signed_desc_append_to_journal(&router->cache_info,
|
2007-10-22 18:32:01 +02:00
|
|
|
&routerlist->desc_store);
|
2006-09-20 01:18:30 +02:00
|
|
|
routerlist_insert_old(routerlist, router);
|
|
|
|
*msg = "Router descriptor was not new.";
|
2014-11-11 19:56:40 +01:00
|
|
|
return ROUTER_IS_ALREADY_KNOWN;
|
2006-09-20 01:18:30 +02:00
|
|
|
} else {
|
2007-11-07 16:19:53 +01:00
|
|
|
/* Same key, and either new, or listed in the consensus. */
|
2011-05-16 03:58:46 +02:00
|
|
|
log_debug(LD_DIR, "Replacing entry for router %s",
|
|
|
|
router_describe(router));
|
2007-07-06 20:40:39 +02:00
|
|
|
routerlist_replace(routerlist, old_router, router);
|
2006-09-20 01:18:30 +02:00
|
|
|
if (!from_cache) {
|
2007-08-31 17:08:37 +02:00
|
|
|
signed_desc_append_to_journal(&router->cache_info,
|
2007-10-22 18:32:01 +02:00
|
|
|
&routerlist->desc_store);
|
2006-09-20 01:18:30 +02:00
|
|
|
}
|
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 &&
|
2014-07-09 18:20:41 +02:00
|
|
|
router_descriptor_is_older_than(router, OLD_ROUTER_DESC_MAX_AGE)) {
|
2009-01-07 03:13:42 +01:00
|
|
|
*msg = "Router descriptor was really old.";
|
2009-03-16 19:47:45 +01:00
|
|
|
routerinfo_free(router);
|
2014-10-13 20:11:27 +02:00
|
|
|
return ROUTER_WAS_TOO_OLD;
|
2009-01-07 03:13:42 +01:00
|
|
|
}
|
|
|
|
|
2007-02-24 08:50:38 +01:00
|
|
|
/* We haven't seen a router with this identity before. Add it to the end of
|
2004-05-17 22:31:01 +02:00
|
|
|
* the list. */
|
2005-10-18 19:43:54 +02:00
|
|
|
routerlist_insert(routerlist, router);
|
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
|
|
|
}
|
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)
|
|
|
|
{
|
2014-10-13 20:11:27 +02:00
|
|
|
was_router_added_t inserted;
|
2007-04-16 23:37:21 +02:00
|
|
|
(void)from_fetch;
|
2007-05-29 16:41:20 +02:00
|
|
|
if (msg) *msg = NULL;
|
2016-05-30 22:18:16 +02:00
|
|
|
/*XXXX Do something with msg */
|
2007-04-30 21:48:39 +02:00
|
|
|
|
2015-01-07 19:11:06 +01:00
|
|
|
inserted = extrainfo_insert(router_get_routerlist(), ei, !from_cache);
|
2007-04-30 21:48:39 +02:00
|
|
|
|
2014-10-13 20:11:27 +02:00
|
|
|
if (WRA_WAS_ADDED(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
|
|
|
|
2014-10-13 20:11:27 +02:00
|
|
|
return inserted;
|
2007-04-16 23:37:21 +02:00
|
|
|
}
|
|
|
|
|
2007-02-16 21:01:02 +01:00
|
|
|
/** Sorting helper: return <0, 0, or >0 depending on whether the
|
2007-02-24 08:50:38 +01:00
|
|
|
* signed_descriptor_t* in *<b>a</b> has an identity digest preceding, equal
|
|
|
|
* to, or later than that of *<b>b</b>. */
|
2005-10-27 02:34:39 +02:00
|
|
|
static int
|
2012-10-12 18:22:13 +02:00
|
|
|
compare_old_routers_by_identity_(const void **_a, const void **_b)
|
2005-10-27 02:34:39 +02:00
|
|
|
{
|
|
|
|
int i;
|
2005-11-05 21:15:27 +01:00
|
|
|
const signed_descriptor_t *r1 = *_a, *r2 = *_b;
|
2011-05-10 22:58:38 +02:00
|
|
|
if ((i = fast_memcmp(r1->identity_digest, r2->identity_digest, DIGEST_LEN)))
|
2005-10-27 02:34:39 +02:00
|
|
|
return i;
|
2008-02-22 20:09:45 +01:00
|
|
|
return (int)(r1->published_on - r2->published_on);
|
2005-10-27 02:34:39 +02:00
|
|
|
}
|
|
|
|
|
2007-02-16 21:01:02 +01:00
|
|
|
/** Internal type used to represent how long an old descriptor was valid,
|
|
|
|
* where it appeared in the list of old descriptors, and whether it's extra
|
|
|
|
* old. Used only by routerlist_remove_old_cached_routers_with_id(). */
|
2005-10-27 02:34:39 +02:00
|
|
|
struct duration_idx_t {
|
|
|
|
int duration;
|
|
|
|
int idx;
|
|
|
|
int old;
|
|
|
|
};
|
|
|
|
|
2007-02-16 21:01:02 +01:00
|
|
|
/** Sorting helper: compare two duration_idx_t by their duration. */
|
2005-10-27 02:34:39 +02:00
|
|
|
static int
|
2012-10-12 18:22:13 +02:00
|
|
|
compare_duration_idx_(const void *_d1, const void *_d2)
|
2005-10-27 02:34:39 +02:00
|
|
|
{
|
|
|
|
const struct duration_idx_t *d1 = _d1;
|
|
|
|
const struct duration_idx_t *d2 = _d2;
|
|
|
|
return d1->duration - d2->duration;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** The range <b>lo</b> through <b>hi</b> inclusive of routerlist->old_routers
|
|
|
|
* must contain routerinfo_t with the same identity and with publication time
|
|
|
|
* in ascending order. Remove members from this range until there are no more
|
2006-03-27 19:29:53 +02:00
|
|
|
* than max_descriptors_per_router() remaining. Start by removing the oldest
|
2005-10-27 02:34:39 +02:00
|
|
|
* members from before <b>cutoff</b>, then remove members which were current
|
|
|
|
* for the lowest amount of time. The order of members of old_routers at
|
|
|
|
* indices <b>lo</b> or higher may be changed.
|
|
|
|
*/
|
|
|
|
static void
|
2007-10-19 04:15:47 +02:00
|
|
|
routerlist_remove_old_cached_routers_with_id(time_t now,
|
|
|
|
time_t cutoff, int lo, int hi,
|
2008-04-08 19:06:41 +02:00
|
|
|
digestset_t *retain)
|
2005-10-27 02:34:39 +02:00
|
|
|
{
|
2007-07-18 22:46:14 +02:00
|
|
|
int i, n = hi-lo+1;
|
|
|
|
unsigned n_extra, n_rmv = 0;
|
2005-10-27 02:34:39 +02:00
|
|
|
struct duration_idx_t *lifespans;
|
2005-12-27 06:26:03 +01:00
|
|
|
uint8_t *rmv, *must_keep;
|
2005-10-27 02:34:39 +02:00
|
|
|
smartlist_t *lst = routerlist->old_routers;
|
|
|
|
#if 1
|
|
|
|
const char *ident;
|
|
|
|
tor_assert(hi < smartlist_len(lst));
|
|
|
|
tor_assert(lo <= hi);
|
2005-11-05 21:15:27 +01:00
|
|
|
ident = ((signed_descriptor_t*)smartlist_get(lst, lo))->identity_digest;
|
2005-10-27 02:34:39 +02:00
|
|
|
for (i = lo+1; i <= hi; ++i) {
|
2005-11-05 21:15:27 +01:00
|
|
|
signed_descriptor_t *r = smartlist_get(lst, i);
|
2011-05-10 22:23:43 +02:00
|
|
|
tor_assert(tor_memeq(ident, r->identity_digest, DIGEST_LEN));
|
2005-10-27 02:34:39 +02:00
|
|
|
}
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* 1 */
|
2005-10-27 02:34:39 +02:00
|
|
|
/* 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
|
|
|
|
2014-11-02 17:56:02 +01:00
|
|
|
lifespans = tor_calloc(n, sizeof(struct duration_idx_t));
|
|
|
|
rmv = tor_calloc(n, sizeof(uint8_t));
|
|
|
|
must_keep = tor_calloc(n, sizeof(uint8_t));
|
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 ||
|
2018-06-26 19:20:54 +02:00
|
|
|
(retain && digestset_probably_contains(retain,
|
|
|
|
r->signed_descriptor_digest))) {
|
2005-12-27 06:26:03 +01:00
|
|
|
must_keep[i-lo] = 1;
|
|
|
|
}
|
2005-10-27 02:34:39 +02:00
|
|
|
if (i < hi) {
|
|
|
|
r_next = smartlist_get(lst, i+1);
|
|
|
|
tor_assert(r->published_on <= r_next->published_on);
|
2008-02-22 20:09:45 +01:00
|
|
|
lifespans[i-lo].duration = (int)(r_next->published_on - r->published_on);
|
2005-10-27 02:34:39 +02:00
|
|
|
} else {
|
|
|
|
r_next = NULL;
|
2005-11-04 17:47:26 +01:00
|
|
|
lifespans[i-lo].duration = INT_MAX;
|
2005-10-27 02:34:39 +02:00
|
|
|
}
|
2005-12-27 06:26:03 +01:00
|
|
|
if (!must_keep[i-lo] && r->published_on < cutoff && n_rmv < n_extra) {
|
2005-10-27 02:34:39 +02:00
|
|
|
++n_rmv;
|
2005-11-04 17:47:26 +01:00
|
|
|
lifespans[i-lo].old = 1;
|
2005-10-27 02:34:39 +02:00
|
|
|
rmv[i-lo] = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n_rmv < n_extra) {
|
|
|
|
/**
|
|
|
|
* We aren't removing enough servers for being old. Sort lifespans by
|
|
|
|
* the duration of liveness, and remove the ones we're not already going to
|
|
|
|
* remove based on how long they were alive.
|
|
|
|
**/
|
2012-10-12 18:22:13 +02:00
|
|
|
qsort(lifespans, n, sizeof(struct duration_idx_t), compare_duration_idx_);
|
2005-10-27 02:34:39 +02:00
|
|
|
for (i = 0; i < n && n_rmv < n_extra; ++i) {
|
2005-12-27 06:26:03 +01:00
|
|
|
if (!must_keep[lifespans[i].idx-lo] && !lifespans[i].old) {
|
2005-10-27 02:34:39 +02:00
|
|
|
rmv[lifespans[i].idx-lo] = 1;
|
|
|
|
++n_rmv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-17 00:16:13 +02:00
|
|
|
i = hi;
|
|
|
|
do {
|
2005-10-27 02:34:39 +02:00
|
|
|
if (rmv[i-lo])
|
|
|
|
routerlist_remove_old(routerlist, smartlist_get(lst, i), i);
|
2007-05-17 00:16:13 +02:00
|
|
|
} while (--i >= lo);
|
2005-12-27 06:26:03 +01:00
|
|
|
tor_free(must_keep);
|
2005-10-27 02:34:39 +02:00
|
|
|
tor_free(rmv);
|
|
|
|
tor_free(lifespans);
|
|
|
|
}
|
|
|
|
|
2005-11-11 18:16:24 +01:00
|
|
|
/** Deactivate any routers from the routerlist that are more than
|
2006-10-20 01:04:56 +02:00
|
|
|
* ROUTER_MAX_AGE seconds old and not recommended by any networkstatuses;
|
|
|
|
* remove old routers from the list of cached routers if we have too many.
|
2005-11-01 18:34:17 +01:00
|
|
|
*/
|
2005-10-27 02:34:39 +02:00
|
|
|
void
|
2005-11-01 18:34:17 +01:00
|
|
|
routerlist_remove_old_routers(void)
|
2005-10-27 02:34:39 +02:00
|
|
|
{
|
|
|
|
int i, hi=-1;
|
|
|
|
const char *cur_id = NULL;
|
2006-02-05 03:07:28 +01:00
|
|
|
time_t now = time(NULL);
|
|
|
|
time_t cutoff;
|
2005-11-01 18:34:17 +01:00
|
|
|
routerinfo_t *router;
|
2006-01-10 05:57:12 +01:00
|
|
|
signed_descriptor_t *sd;
|
2008-04-08 19:06:41 +02:00
|
|
|
digestset_t *retain;
|
2008-02-05 22:39:29 +01:00
|
|
|
const networkstatus_t *consensus = networkstatus_get_latest_consensus();
|
2007-07-28 02:11:34 +02:00
|
|
|
|
|
|
|
trusted_dirs_remove_old_certs();
|
|
|
|
|
2007-10-16 01:15:24 +02:00
|
|
|
if (!routerlist || !consensus)
|
2005-10-27 02:34:39 +02:00
|
|
|
return;
|
|
|
|
|
2007-10-16 20:19:02 +02:00
|
|
|
// routerlist_assert_ok(routerlist);
|
2007-04-23 02:24:06 +02:00
|
|
|
|
2008-12-26 18:35:36 +01:00
|
|
|
/* We need to guess how many router descriptors we will wind up wanting to
|
|
|
|
retain, so that we can be sure to allocate a large enough Bloom filter
|
|
|
|
to hold the digest set. Overestimating is fine; underestimating is bad.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
/* We'll probably retain everything in the consensus. */
|
|
|
|
int n_max_retain = smartlist_len(consensus->routerstatus_list);
|
|
|
|
retain = digestset_new(n_max_retain);
|
2008-04-08 19:06:41 +02:00
|
|
|
}
|
|
|
|
|
2006-02-05 03:07:28 +01:00
|
|
|
cutoff = now - OLD_ROUTER_DESC_MAX_AGE;
|
2007-10-16 01:15:24 +02:00
|
|
|
/* Retain anything listed in the consensus. */
|
|
|
|
if (consensus) {
|
|
|
|
SMARTLIST_FOREACH(consensus->routerstatus_list, routerstatus_t *, rs,
|
2007-10-12 19:13:09 +02:00
|
|
|
if (rs->published_on >= cutoff)
|
2008-04-08 19:06:41 +02:00
|
|
|
digestset_add(retain, rs->descriptor_digest));
|
2007-10-12 19:13:09 +02:00
|
|
|
}
|
|
|
|
|
2014-01-30 12:48:49 +01:00
|
|
|
/* If we have a consensus, we should consider pruning current routers that
|
|
|
|
* are too old and that nobody recommends. (If we don't have a consensus,
|
|
|
|
* then we should get one before we decide to kill routers.) */
|
2009-01-04 20:47:12 +01:00
|
|
|
|
2014-01-29 21:17:05 +01:00
|
|
|
if (consensus) {
|
2006-10-20 01:04:56 +02:00
|
|
|
cutoff = now - ROUTER_MAX_AGE;
|
|
|
|
/* Remove too-old unrecommended members of routerlist->routers. */
|
|
|
|
for (i = 0; i < smartlist_len(routerlist->routers); ++i) {
|
|
|
|
router = smartlist_get(routerlist->routers, i);
|
|
|
|
if (router->cache_info.published_on <= cutoff &&
|
2007-10-19 04:15:47 +02:00
|
|
|
router->cache_info.last_listed_as_valid_until < now &&
|
2018-06-26 19:20:54 +02:00
|
|
|
!digestset_probably_contains(retain,
|
2008-04-08 19:06:41 +02:00
|
|
|
router->cache_info.signed_descriptor_digest)) {
|
2006-10-20 01:04:56 +02:00
|
|
|
/* Too old: remove it. (If we're a cache, just move it into
|
|
|
|
* old_routers.) */
|
|
|
|
log_info(LD_DIR,
|
2011-05-16 03:58:46 +02:00
|
|
|
"Forgetting obsolete (too old) routerinfo for router %s",
|
|
|
|
router_describe(router));
|
2009-06-20 07:52:59 +02:00
|
|
|
routerlist_remove(routerlist, router, 1, now);
|
2007-07-18 16:02:47 +02:00
|
|
|
i--;
|
2006-10-20 01:04:56 +02:00
|
|
|
}
|
2005-11-01 18:34:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-16 20:19:02 +02:00
|
|
|
//routerlist_assert_ok(routerlist);
|
2007-04-23 02:24:06 +02:00
|
|
|
|
2006-01-10 05:57:12 +01:00
|
|
|
/* Remove far-too-old members of routerlist->old_routers. */
|
|
|
|
cutoff = now - OLD_ROUTER_DESC_MAX_AGE;
|
|
|
|
for (i = 0; i < smartlist_len(routerlist->old_routers); ++i) {
|
|
|
|
sd = smartlist_get(routerlist->old_routers, i);
|
|
|
|
if (sd->published_on <= cutoff &&
|
2007-10-19 04:15:47 +02:00
|
|
|
sd->last_listed_as_valid_until < now &&
|
2018-06-26 19:20:54 +02:00
|
|
|
!digestset_probably_contains(retain, sd->signed_descriptor_digest)) {
|
2006-01-10 05:57:12 +01:00
|
|
|
/* Too old. Remove it. */
|
|
|
|
routerlist_remove_old(routerlist, sd, i--);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-16 20:19:02 +02:00
|
|
|
//routerlist_assert_ok(routerlist);
|
2007-04-23 02:24:06 +02:00
|
|
|
|
2008-04-08 19:06:41 +02:00
|
|
|
log_info(LD_DIR, "We have %d live routers and %d old router descriptors.",
|
2007-10-18 16:58:45 +02:00
|
|
|
smartlist_len(routerlist->routers),
|
2008-04-08 19:06:41 +02:00
|
|
|
smartlist_len(routerlist->old_routers));
|
2007-10-18 16:58:45 +02:00
|
|
|
|
2006-10-20 01:04:56 +02:00
|
|
|
/* Now we might have to look at routerlist->old_routers for extraneous
|
|
|
|
* members. (We'd keep all the members if we could, but we need to save
|
2006-01-10 05:57:12 +01:00
|
|
|
* space.) First, check whether we have too many router descriptors, total.
|
|
|
|
* We're okay with having too many for some given router, so long as the
|
2006-03-27 19:29:53 +02:00
|
|
|
* total number doesn't approach max_descriptors_per_router()*len(router).
|
2005-10-27 02:34:39 +02:00
|
|
|
*/
|
|
|
|
if (smartlist_len(routerlist->old_routers) <
|
2008-04-08 19:06:41 +02:00
|
|
|
smartlist_len(routerlist->routers))
|
2006-01-11 04:58:07 +01:00
|
|
|
goto done;
|
2005-10-27 02:34:39 +02:00
|
|
|
|
2008-04-08 19:06:41 +02:00
|
|
|
/* Sort by identity, then fix indices. */
|
2012-10-12 18:22:13 +02:00
|
|
|
smartlist_sort(routerlist->old_routers, compare_old_routers_by_identity_);
|
2007-11-07 18:29:16 +01:00
|
|
|
/* Fix indices. */
|
2007-11-07 18:30:08 +01:00
|
|
|
for (i = 0; i < smartlist_len(routerlist->old_routers); ++i) {
|
2007-11-07 18:29:16 +01:00
|
|
|
signed_descriptor_t *r = smartlist_get(routerlist->old_routers, i);
|
|
|
|
r->routerlist_index = i;
|
|
|
|
}
|
2005-10-27 02:34:39 +02:00
|
|
|
|
|
|
|
/* Iterate through the list from back to front, so when we remove descriptors
|
|
|
|
* we don't mess up groups we haven't gotten to. */
|
|
|
|
for (i = smartlist_len(routerlist->old_routers)-1; i >= 0; --i) {
|
2005-11-05 21:15:27 +01:00
|
|
|
signed_descriptor_t *r = smartlist_get(routerlist->old_routers, i);
|
2005-10-27 02:34:39 +02:00
|
|
|
if (!cur_id) {
|
|
|
|
cur_id = r->identity_digest;
|
|
|
|
hi = i;
|
|
|
|
}
|
2011-05-10 22:58:38 +02:00
|
|
|
if (tor_memneq(cur_id, r->identity_digest, DIGEST_LEN)) {
|
2007-10-19 04:15:47 +02:00
|
|
|
routerlist_remove_old_cached_routers_with_id(now,
|
|
|
|
cutoff, i+1, hi, retain);
|
2005-11-23 08:28:54 +01:00
|
|
|
cur_id = r->identity_digest;
|
2005-10-27 02:34:39 +02:00
|
|
|
hi = i;
|
2004-05-20 07:10:30 +02:00
|
|
|
}
|
|
|
|
}
|
2005-11-01 18:34:17 +01:00
|
|
|
if (hi>=0)
|
2007-10-19 04:15:47 +02:00
|
|
|
routerlist_remove_old_cached_routers_with_id(now, cutoff, 0, hi, retain);
|
2007-10-16 20:19:02 +02:00
|
|
|
//routerlist_assert_ok(routerlist);
|
2006-01-11 04:58:07 +01:00
|
|
|
|
|
|
|
done:
|
2008-04-08 19:06:41 +02:00
|
|
|
digestset_free(retain);
|
2008-05-29 02:44:57 +02:00
|
|
|
router_rebuild_store(RRS_DONT_REMOVE_OLD, &routerlist->desc_store);
|
|
|
|
router_rebuild_store(RRS_DONT_REMOVE_OLD,&routerlist->extrainfo_store);
|
2004-05-20 07:10:30 +02:00
|
|
|
}
|
|
|
|
|
2007-12-22 07:11:49 +01:00
|
|
|
/** We just added a new set of descriptors. Take whatever extra steps
|
|
|
|
* we need. */
|
2010-08-18 19:36:09 +02:00
|
|
|
void
|
2007-12-24 11:31:39 +01:00
|
|
|
routerlist_descriptors_added(smartlist_t *sl, int from_cache)
|
2007-06-12 11:17:23 +02:00
|
|
|
{
|
|
|
|
tor_assert(sl);
|
|
|
|
control_event_descriptors_changed(sl);
|
2010-08-18 19:36:09 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(sl, routerinfo_t *, ri) {
|
2007-06-12 11:17:23 +02:00
|
|
|
if (ri->purpose == ROUTER_PURPOSE_BRIDGE)
|
2007-12-24 11:31:39 +01:00
|
|
|
learned_bridge_descriptor(ri, from_cache);
|
2010-08-18 19:36:09 +02:00
|
|
|
if (ri->needs_retest_if_added) {
|
|
|
|
ri->needs_retest_if_added = 0;
|
|
|
|
dirserv_single_reachability_test(approx_time(), ri);
|
|
|
|
}
|
|
|
|
} SMARTLIST_FOREACH_END(ri);
|
2007-06-12 11:17:23 +02:00
|
|
|
}
|
|
|
|
|
2005-08-26 22:59:04 +02:00
|
|
|
/**
|
2005-06-21 01:04:13 +02:00
|
|
|
* Code to parse a single router descriptor and insert it into the
|
|
|
|
* routerlist. Return -1 if the descriptor was ill-formed; 0 if the
|
2005-04-03 00:02:13 +02:00
|
|
|
* descriptor was well-formed but could not be added; and 1 if the
|
|
|
|
* descriptor was added.
|
2005-06-21 01:04:13 +02:00
|
|
|
*
|
|
|
|
* If we don't add it and <b>msg</b> is not NULL, then assign to
|
|
|
|
* *<b>msg</b> a static string describing the reason for refusing the
|
|
|
|
* descriptor.
|
2005-08-26 22:59:04 +02:00
|
|
|
*
|
|
|
|
* This is used only by the controller.
|
2004-05-10 06:34:48 +02:00
|
|
|
*/
|
2005-02-25 21:46:13 +01:00
|
|
|
int
|
2007-10-12 09:57:29 +02:00
|
|
|
router_load_single_router(const char *s, uint8_t purpose, int cache,
|
|
|
|
const char **msg)
|
2005-02-25 21:46:13 +01:00
|
|
|
{
|
|
|
|
routerinfo_t *ri;
|
2008-12-17 22:50:01 +01:00
|
|
|
was_router_added_t r;
|
2005-10-05 03:27:08 +02:00
|
|
|
smartlist_t *lst;
|
2007-12-02 05:39:56 +01:00
|
|
|
char annotation_buf[ROUTER_ANNOTATION_BUF_LEN];
|
2005-10-05 04:06:36 +02:00
|
|
|
tor_assert(msg);
|
2005-07-15 21:40:38 +02:00
|
|
|
*msg = NULL;
|
2005-02-25 21:46:13 +01:00
|
|
|
|
2007-09-27 22:46:30 +02:00
|
|
|
tor_snprintf(annotation_buf, sizeof(annotation_buf),
|
|
|
|
"@source controller\n"
|
|
|
|
"@purpose %s\n", router_purpose_to_string(purpose));
|
|
|
|
|
Treat unparseable (micro)descriptors and extrainfos as undownloadable
One pain point in evolving the Tor design and implementing has been
adding code that makes clients reject directory documents that they
previously would have accepted, if those descriptors actually exist.
When this happened, the clients would get the document, reject it,
and then decide to try downloading it again, ad infinitum. This
problem becomes particularly obnoxious with authorities, since if
some authorities accept a descriptor that others don't, the ones
that don't accept it would go crazy trying to re-fetch it over and
over. (See for example ticket #9286.)
This patch tries to solve this problem by tracking, if a descriptor
isn't parseable, what its digest was, and whether it is invalid
because of some flaw that applies to the portion containing the
digest. (This excludes RSA signature problems: RSA signatures
aren't included in the digest. This means that a directory
authority can still put another directory authority into a loop by
mentioning a descriptor, and then serving that descriptor with an
invalid RSA signatures. But that would also make the misbehaving
directory authority get DoSed by the server it's attacking, so it's
not much of an issue.)
We already have a mechanism to mark something undownloadable with
downloadstatus_mark_impossible(); we use that here for
microdescriptors, extrainfos, and router descriptors.
Unit tests to follow in another patch.
Closes ticket #11243.
2014-10-03 16:55:50 +02:00
|
|
|
if (!(ri = router_parse_entry_from_string(s, NULL, 1, 0,
|
|
|
|
annotation_buf, NULL))) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR, "Error parsing router descriptor; dropping.");
|
2005-07-15 21:40:38 +02:00
|
|
|
*msg = "Couldn't parse router descriptor.";
|
2005-02-25 21:46:13 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2007-09-27 22:46:30 +02:00
|
|
|
tor_assert(ri->purpose == purpose);
|
2005-05-14 07:01:41 +02:00
|
|
|
if (router_is_me(ri)) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR, "Router's identity key matches mine; dropping.");
|
2005-05-18 05:52:07 +02:00
|
|
|
*msg = "Router's identity key matches mine.";
|
2005-05-14 07:01:41 +02:00
|
|
|
routerinfo_free(ri);
|
|
|
|
return 0;
|
|
|
|
}
|
2005-10-05 03:27:08 +02:00
|
|
|
|
2007-10-12 09:57:29 +02:00
|
|
|
if (!cache) /* obey the preference of the controller */
|
|
|
|
ri->cache_info.do_not_cache = 1;
|
|
|
|
|
2012-01-18 21:53:30 +01:00
|
|
|
lst = smartlist_new();
|
2005-10-05 03:27:08 +02:00
|
|
|
smartlist_add(lst, ri);
|
2007-10-16 01:15:24 +02:00
|
|
|
routers_update_status_from_consensus_networkstatus(lst, 0);
|
2005-09-14 23:09:25 +02:00
|
|
|
|
2008-12-17 22:50:01 +01:00
|
|
|
r = router_add_to_routerlist(ri, msg, 0, 0);
|
|
|
|
if (!WRA_WAS_ADDED(r)) {
|
2005-06-21 01:04:13 +02:00
|
|
|
/* we've already assigned to *msg now, and ri is already freed */
|
2006-02-15 04:01:53 +01:00
|
|
|
tor_assert(*msg);
|
2008-12-17 22:50:01 +01:00
|
|
|
if (r == ROUTER_AUTHDIR_REJECTS)
|
2006-02-15 04:01:53 +01:00
|
|
|
log_warn(LD_DIR, "Couldn't add router to list: %s Dropping.", *msg);
|
2005-10-05 03:27:08 +02:00
|
|
|
smartlist_free(lst);
|
2005-04-03 00:02:13 +02:00
|
|
|
return 0;
|
2005-03-02 23:29:58 +01:00
|
|
|
} else {
|
2007-12-24 11:31:39 +01:00
|
|
|
routerlist_descriptors_added(lst, 0);
|
2005-10-05 03:27:08 +02:00
|
|
|
smartlist_free(lst);
|
2006-02-13 11:33:00 +01:00
|
|
|
log_debug(LD_DIR, "Added router to list");
|
2005-10-05 03:27:08 +02:00
|
|
|
return 1;
|
2005-02-25 21:46:13 +01:00
|
|
|
}
|
|
|
|
}
|
2004-04-25 21:04:11 +02:00
|
|
|
|
2005-09-15 08:15:31 +02:00
|
|
|
/** Given a string <b>s</b> containing some routerdescs, parse it and put the
|
2007-01-22 08:51:06 +01:00
|
|
|
* routers into our directory. If saved_location is SAVED_NOWHERE, the routers
|
|
|
|
* are in response to a query to the network: cache them by adding them to
|
|
|
|
* the journal.
|
2005-09-15 08:15:31 +02:00
|
|
|
*
|
2008-12-23 22:17:52 +01:00
|
|
|
* Return the number of routers actually added.
|
|
|
|
*
|
2005-09-15 08:15:31 +02:00
|
|
|
* If <b>requested_fingerprints</b> is provided, it must contain a list of
|
2007-09-21 08:14:36 +02:00
|
|
|
* uppercased fingerprints. Do not update any router whose
|
2005-09-15 08:15:31 +02:00
|
|
|
* fingerprint is not on the list; after updating a router, remove its
|
|
|
|
* fingerprint from the list.
|
2007-09-21 08:14:36 +02:00
|
|
|
*
|
|
|
|
* If <b>descriptor_digests</b> is non-zero, then the requested_fingerprints
|
|
|
|
* are descriptor digests. Otherwise they are identity digests.
|
2005-09-15 08:15:31 +02:00
|
|
|
*/
|
2008-12-23 22:17:52 +01:00
|
|
|
int
|
2007-05-22 04:20:52 +02:00
|
|
|
router_load_routers_from_string(const char *s, const char *eos,
|
|
|
|
saved_location_t saved_location,
|
2007-06-10 09:34:21 +02:00
|
|
|
smartlist_t *requested_fingerprints,
|
2007-09-21 08:14:36 +02:00
|
|
|
int descriptor_digests,
|
2007-09-27 22:46:30 +02:00
|
|
|
const char *prepend_annotations)
|
2005-09-15 07:19:38 +02:00
|
|
|
{
|
2012-01-18 21:53:30 +01:00
|
|
|
smartlist_t *routers = smartlist_new(), *changed = smartlist_new();
|
2005-09-15 07:19:38 +02:00
|
|
|
char fp[HEX_DIGEST_LEN+1];
|
|
|
|
const char *msg;
|
2006-06-22 09:01:54 +02:00
|
|
|
int from_cache = (saved_location != SAVED_NOWHERE);
|
2007-09-26 18:19:44 +02:00
|
|
|
int allow_annotations = (saved_location != SAVED_NOWHERE);
|
2008-01-09 00:31:25 +01:00
|
|
|
int any_changed = 0;
|
Treat unparseable (micro)descriptors and extrainfos as undownloadable
One pain point in evolving the Tor design and implementing has been
adding code that makes clients reject directory documents that they
previously would have accepted, if those descriptors actually exist.
When this happened, the clients would get the document, reject it,
and then decide to try downloading it again, ad infinitum. This
problem becomes particularly obnoxious with authorities, since if
some authorities accept a descriptor that others don't, the ones
that don't accept it would go crazy trying to re-fetch it over and
over. (See for example ticket #9286.)
This patch tries to solve this problem by tracking, if a descriptor
isn't parseable, what its digest was, and whether it is invalid
because of some flaw that applies to the portion containing the
digest. (This excludes RSA signature problems: RSA signatures
aren't included in the digest. This means that a directory
authority can still put another directory authority into a loop by
mentioning a descriptor, and then serving that descriptor with an
invalid RSA signatures. But that would also make the misbehaving
directory authority get DoSed by the server it's attacking, so it's
not much of an issue.)
We already have a mechanism to mark something undownloadable with
downloadstatus_mark_impossible(); we use that here for
microdescriptors, extrainfos, and router descriptors.
Unit tests to follow in another patch.
Closes ticket #11243.
2014-10-03 16:55:50 +02:00
|
|
|
smartlist_t *invalid_digests = smartlist_new();
|
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,
|
Treat unparseable (micro)descriptors and extrainfos as undownloadable
One pain point in evolving the Tor design and implementing has been
adding code that makes clients reject directory documents that they
previously would have accepted, if those descriptors actually exist.
When this happened, the clients would get the document, reject it,
and then decide to try downloading it again, ad infinitum. This
problem becomes particularly obnoxious with authorities, since if
some authorities accept a descriptor that others don't, the ones
that don't accept it would go crazy trying to re-fetch it over and
over. (See for example ticket #9286.)
This patch tries to solve this problem by tracking, if a descriptor
isn't parseable, what its digest was, and whether it is invalid
because of some flaw that applies to the portion containing the
digest. (This excludes RSA signature problems: RSA signatures
aren't included in the digest. This means that a directory
authority can still put another directory authority into a loop by
mentioning a descriptor, and then serving that descriptor with an
invalid RSA signatures. But that would also make the misbehaving
directory authority get DoSed by the server it's attacking, so it's
not much of an issue.)
We already have a mechanism to mark something undownloadable with
downloadstatus_mark_impossible(); we use that here for
microdescriptors, extrainfos, and router descriptors.
Unit tests to follow in another patch.
Closes ticket #11243.
2014-10-03 16:55:50 +02:00
|
|
|
allow_annotations, prepend_annotations,
|
|
|
|
invalid_digests);
|
2005-09-15 07:19:38 +02:00
|
|
|
|
2007-10-16 01:15:24 +02:00
|
|
|
routers_update_status_from_consensus_networkstatus(routers, !from_cache);
|
2005-12-27 06:26:03 +01:00
|
|
|
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_DIR, "%d elements to add", smartlist_len(routers));
|
2005-09-15 07:19:38 +02:00
|
|
|
|
2008-12-17 22:50:01 +01:00
|
|
|
SMARTLIST_FOREACH_BEGIN(routers, routerinfo_t *, ri) {
|
|
|
|
was_router_added_t r;
|
2009-05-17 08:01:09 +02:00
|
|
|
char d[DIGEST_LEN];
|
2005-09-15 07:19:38 +02:00
|
|
|
if (requested_fingerprints) {
|
2007-09-21 08:14:36 +02:00
|
|
|
base16_encode(fp, sizeof(fp), descriptor_digests ?
|
|
|
|
ri->cache_info.signed_descriptor_digest :
|
|
|
|
ri->cache_info.identity_digest,
|
|
|
|
DIGEST_LEN);
|
2012-04-11 18:50:50 +02:00
|
|
|
if (smartlist_contains_string(requested_fingerprints, fp)) {
|
2005-09-15 07:19:38 +02:00
|
|
|
smartlist_string_remove(requested_fingerprints, fp);
|
|
|
|
} else {
|
|
|
|
char *requested =
|
|
|
|
smartlist_join_strings(requested_fingerprints," ",0,NULL);
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_DIR,
|
|
|
|
"We received a router descriptor with a fingerprint (%s) "
|
|
|
|
"that we never requested. (We asked for: %s.) Dropping.",
|
|
|
|
fp, requested);
|
2005-09-15 07:19:38 +02:00
|
|
|
tor_free(requested);
|
|
|
|
routerinfo_free(ri);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-17 08:01:09 +02:00
|
|
|
memcpy(d, ri->cache_info.signed_descriptor_digest, DIGEST_LEN);
|
2008-12-17 22:50:01 +01:00
|
|
|
r = router_add_to_routerlist(ri, &msg, from_cache, !from_cache);
|
|
|
|
if (WRA_WAS_ADDED(r)) {
|
2008-12-23 22:17:52 +01:00
|
|
|
any_changed++;
|
2005-09-15 07:19:38 +02:00
|
|
|
smartlist_add(changed, ri);
|
2007-12-24 11:31:39 +01:00
|
|
|
routerlist_descriptors_added(changed, from_cache);
|
2007-07-16 02:11:03 +02:00
|
|
|
smartlist_clear(changed);
|
2014-10-13 20:11:27 +02:00
|
|
|
} else if (WRA_NEVER_DOWNLOADABLE(r)) {
|
2008-12-23 22:17:52 +01:00
|
|
|
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
|
|
|
|
Treat unparseable (micro)descriptors and extrainfos as undownloadable
One pain point in evolving the Tor design and implementing has been
adding code that makes clients reject directory documents that they
previously would have accepted, if those descriptors actually exist.
When this happened, the clients would get the document, reject it,
and then decide to try downloading it again, ad infinitum. This
problem becomes particularly obnoxious with authorities, since if
some authorities accept a descriptor that others don't, the ones
that don't accept it would go crazy trying to re-fetch it over and
over. (See for example ticket #9286.)
This patch tries to solve this problem by tracking, if a descriptor
isn't parseable, what its digest was, and whether it is invalid
because of some flaw that applies to the portion containing the
digest. (This excludes RSA signature problems: RSA signatures
aren't included in the digest. This means that a directory
authority can still put another directory authority into a loop by
mentioning a descriptor, and then serving that descriptor with an
invalid RSA signatures. But that would also make the misbehaving
directory authority get DoSed by the server it's attacking, so it's
not much of an issue.)
We already have a mechanism to mark something undownloadable with
downloadstatus_mark_impossible(); we use that here for
microdescriptors, extrainfos, and router descriptors.
Unit tests to follow in another patch.
Closes ticket #11243.
2014-10-03 16:55:50 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(invalid_digests, const uint8_t *, bad_digest) {
|
|
|
|
/* This digest is never going to be parseable. */
|
|
|
|
base16_encode(fp, sizeof(fp), (char*)bad_digest, DIGEST_LEN);
|
|
|
|
if (requested_fingerprints && descriptor_digests) {
|
|
|
|
if (! smartlist_contains_string(requested_fingerprints, fp)) {
|
|
|
|
/* But we didn't ask for it, so we should assume shennanegans. */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
smartlist_string_remove(requested_fingerprints, fp);
|
|
|
|
}
|
|
|
|
download_status_t *dls;
|
|
|
|
dls = router_get_dl_status_by_descriptor_digest((char*)bad_digest);
|
|
|
|
if (dls) {
|
|
|
|
log_info(LD_GENERAL, "Marking router with descriptor %s as unparseable, "
|
|
|
|
"and therefore undownloadable", fp);
|
|
|
|
download_status_mark_impossible(dls);
|
|
|
|
}
|
|
|
|
} SMARTLIST_FOREACH_END(bad_digest);
|
|
|
|
SMARTLIST_FOREACH(invalid_digests, uint8_t *, d, tor_free(d));
|
|
|
|
smartlist_free(invalid_digests);
|
|
|
|
|
2005-10-18 23:58:19 +02:00
|
|
|
routerlist_assert_ok(routerlist);
|
2007-08-31 17:08:37 +02:00
|
|
|
|
2008-01-09 00:31:25 +01:00
|
|
|
if (any_changed)
|
|
|
|
router_rebuild_store(0, &routerlist->desc_store);
|
2005-09-15 07:19:38 +02:00
|
|
|
|
|
|
|
smartlist_free(routers);
|
|
|
|
smartlist_free(changed);
|
2008-12-23 22:17:52 +01:00
|
|
|
|
|
|
|
return any_changed;
|
2005-09-15 07:19:38 +02:00
|
|
|
}
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/** Parse one or more extrainfos from <b>s</b> (ending immediately before
|
|
|
|
* <b>eos</b> if <b>eos</b> is present). Other arguments are as for
|
|
|
|
* router_load_routers_from_string(). */
|
2007-05-18 23:19:19 +02:00
|
|
|
void
|
2007-05-22 04:20:52 +02:00
|
|
|
router_load_extrainfo_from_string(const char *s, const char *eos,
|
2007-05-18 23:19:19 +02:00
|
|
|
saved_location_t saved_location,
|
2007-09-21 08:14:36 +02:00
|
|
|
smartlist_t *requested_fingerprints,
|
|
|
|
int descriptor_digests)
|
2007-04-30 21:48:39 +02:00
|
|
|
{
|
2012-01-18 21:53:30 +01:00
|
|
|
smartlist_t *extrainfo_list = smartlist_new();
|
2007-04-30 21:48:39 +02:00
|
|
|
const char *msg;
|
|
|
|
int from_cache = (saved_location != SAVED_NOWHERE);
|
Treat unparseable (micro)descriptors and extrainfos as undownloadable
One pain point in evolving the Tor design and implementing has been
adding code that makes clients reject directory documents that they
previously would have accepted, if those descriptors actually exist.
When this happened, the clients would get the document, reject it,
and then decide to try downloading it again, ad infinitum. This
problem becomes particularly obnoxious with authorities, since if
some authorities accept a descriptor that others don't, the ones
that don't accept it would go crazy trying to re-fetch it over and
over. (See for example ticket #9286.)
This patch tries to solve this problem by tracking, if a descriptor
isn't parseable, what its digest was, and whether it is invalid
because of some flaw that applies to the portion containing the
digest. (This excludes RSA signature problems: RSA signatures
aren't included in the digest. This means that a directory
authority can still put another directory authority into a loop by
mentioning a descriptor, and then serving that descriptor with an
invalid RSA signatures. But that would also make the misbehaving
directory authority get DoSed by the server it's attacking, so it's
not much of an issue.)
We already have a mechanism to mark something undownloadable with
downloadstatus_mark_impossible(); we use that here for
microdescriptors, extrainfos, and router descriptors.
Unit tests to follow in another patch.
Closes ticket #11243.
2014-10-03 16:55:50 +02:00
|
|
|
smartlist_t *invalid_digests = smartlist_new();
|
2007-04-30 21:48:39 +02:00
|
|
|
|
2007-09-27 18:08:10 +02:00
|
|
|
router_parse_list_from_string(&s, eos, extrainfo_list, saved_location, 1, 0,
|
Treat unparseable (micro)descriptors and extrainfos as undownloadable
One pain point in evolving the Tor design and implementing has been
adding code that makes clients reject directory documents that they
previously would have accepted, if those descriptors actually exist.
When this happened, the clients would get the document, reject it,
and then decide to try downloading it again, ad infinitum. This
problem becomes particularly obnoxious with authorities, since if
some authorities accept a descriptor that others don't, the ones
that don't accept it would go crazy trying to re-fetch it over and
over. (See for example ticket #9286.)
This patch tries to solve this problem by tracking, if a descriptor
isn't parseable, what its digest was, and whether it is invalid
because of some flaw that applies to the portion containing the
digest. (This excludes RSA signature problems: RSA signatures
aren't included in the digest. This means that a directory
authority can still put another directory authority into a loop by
mentioning a descriptor, and then serving that descriptor with an
invalid RSA signatures. But that would also make the misbehaving
directory authority get DoSed by the server it's attacking, so it's
not much of an issue.)
We already have a mechanism to mark something undownloadable with
downloadstatus_mark_impossible(); we use that here for
microdescriptors, extrainfos, and router descriptors.
Unit tests to follow in another patch.
Closes ticket #11243.
2014-10-03 16:55:50 +02:00
|
|
|
NULL, invalid_digests);
|
2007-04-30 21:48:39 +02:00
|
|
|
|
|
|
|
log_info(LD_DIR, "%d elements to add", smartlist_len(extrainfo_list));
|
|
|
|
|
2012-07-17 15:33:38 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(extrainfo_list, extrainfo_t *, ei) {
|
2014-10-13 20:11:27 +02:00
|
|
|
uint8_t d[DIGEST_LEN];
|
|
|
|
memcpy(d, ei->cache_info.signed_descriptor_digest, DIGEST_LEN);
|
2014-10-20 15:04:53 +02:00
|
|
|
was_router_added_t added =
|
|
|
|
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);
|
2016-12-10 01:13:11 +01:00
|
|
|
/* We silently let relays stuff us with extrainfos we didn't ask for,
|
2008-02-15 20:20:53 +01:00
|
|
|
* 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. */
|
2014-10-13 20:11:27 +02:00
|
|
|
} else if (WRA_NEVER_DOWNLOADABLE(added)) {
|
|
|
|
signed_descriptor_t *sd = router_get_by_extrainfo_digest((char*)d);
|
|
|
|
if (sd) {
|
|
|
|
log_info(LD_GENERAL, "Marking extrainfo with descriptor %s as "
|
|
|
|
"unparseable, and therefore undownloadable",
|
|
|
|
hex_str((char*)d,DIGEST_LEN));
|
|
|
|
download_status_mark_impossible(&sd->ei_dl_status);
|
|
|
|
}
|
2007-05-18 23:19:19 +02:00
|
|
|
}
|
2012-07-17 15:33:38 +02:00
|
|
|
} SMARTLIST_FOREACH_END(ei);
|
2007-04-30 21:48:39 +02:00
|
|
|
|
Treat unparseable (micro)descriptors and extrainfos as undownloadable
One pain point in evolving the Tor design and implementing has been
adding code that makes clients reject directory documents that they
previously would have accepted, if those descriptors actually exist.
When this happened, the clients would get the document, reject it,
and then decide to try downloading it again, ad infinitum. This
problem becomes particularly obnoxious with authorities, since if
some authorities accept a descriptor that others don't, the ones
that don't accept it would go crazy trying to re-fetch it over and
over. (See for example ticket #9286.)
This patch tries to solve this problem by tracking, if a descriptor
isn't parseable, what its digest was, and whether it is invalid
because of some flaw that applies to the portion containing the
digest. (This excludes RSA signature problems: RSA signatures
aren't included in the digest. This means that a directory
authority can still put another directory authority into a loop by
mentioning a descriptor, and then serving that descriptor with an
invalid RSA signatures. But that would also make the misbehaving
directory authority get DoSed by the server it's attacking, so it's
not much of an issue.)
We already have a mechanism to mark something undownloadable with
downloadstatus_mark_impossible(); we use that here for
microdescriptors, extrainfos, and router descriptors.
Unit tests to follow in another patch.
Closes ticket #11243.
2014-10-03 16:55:50 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(invalid_digests, const uint8_t *, bad_digest) {
|
|
|
|
/* This digest is never going to be parseable. */
|
|
|
|
char fp[HEX_DIGEST_LEN+1];
|
|
|
|
base16_encode(fp, sizeof(fp), (char*)bad_digest, DIGEST_LEN);
|
|
|
|
if (requested_fingerprints) {
|
|
|
|
if (! smartlist_contains_string(requested_fingerprints, fp)) {
|
|
|
|
/* But we didn't ask for it, so we should assume shennanegans. */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
smartlist_string_remove(requested_fingerprints, fp);
|
|
|
|
}
|
|
|
|
signed_descriptor_t *sd =
|
|
|
|
router_get_by_extrainfo_digest((char*)bad_digest);
|
|
|
|
if (sd) {
|
|
|
|
log_info(LD_GENERAL, "Marking extrainfo with descriptor %s as "
|
|
|
|
"unparseable, and therefore undownloadable", fp);
|
|
|
|
download_status_mark_impossible(&sd->ei_dl_status);
|
|
|
|
}
|
|
|
|
} SMARTLIST_FOREACH_END(bad_digest);
|
|
|
|
SMARTLIST_FOREACH(invalid_digests, uint8_t *, d, tor_free(d));
|
|
|
|
smartlist_free(invalid_digests);
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-11-30 02:20:28 +01:00
|
|
|
/** Return true iff the latest ns-flavored consensus includes a descriptor
|
|
|
|
* whose digest is that of <b>desc</b>. */
|
2007-10-08 23:21:50 +02:00
|
|
|
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;
|
2016-11-30 02:20:28 +01:00
|
|
|
networkstatus_t *consensus = networkstatus_get_latest_consensus_by_flavor(
|
|
|
|
FLAV_NS);
|
2007-10-08 23:21:50 +02:00
|
|
|
|
2007-10-16 01:15:24 +02:00
|
|
|
if (consensus) {
|
|
|
|
rs = networkstatus_vote_find_entry(consensus, desc->identity_digest);
|
2011-05-10 22:23:43 +02:00
|
|
|
if (rs && tor_memeq(rs->descriptor_digest,
|
2007-10-16 01:15:24 +02:00
|
|
|
desc->signed_descriptor_digest, DIGEST_LEN))
|
2007-10-08 23:21:50 +02:00
|
|
|
return 1;
|
2007-10-16 01:15:24 +02:00
|
|
|
}
|
2007-10-08 23:21:50 +02:00
|
|
|
return 0;
|
2005-09-12 08:56:42 +02:00
|
|
|
}
|
2005-09-13 17:32:03 +02:00
|
|
|
|
2010-11-12 18:05:27 +01:00
|
|
|
/** Update downloads for router descriptors and/or microdescriptors as
|
|
|
|
* appropriate. */
|
|
|
|
void
|
|
|
|
update_all_descriptor_downloads(time_t now)
|
|
|
|
{
|
2017-09-21 20:34:36 +02:00
|
|
|
if (should_delay_dir_fetches(get_options(), NULL))
|
2011-11-28 21:44:10 +01:00
|
|
|
return;
|
2010-11-12 18:05:27 +01:00
|
|
|
update_router_descriptor_downloads(now);
|
|
|
|
update_microdesc_downloads(now);
|
|
|
|
launch_dummy_descriptor_download_as_needed(now, get_options());
|
|
|
|
}
|
|
|
|
|
2014-01-29 21:17:05 +01:00
|
|
|
/** Clear all our timeouts for fetching v3 directory stuff, and then
|
2007-10-08 23:21:50 +02:00
|
|
|
* give it all a try again. */
|
|
|
|
void
|
|
|
|
routerlist_retry_directory_downloads(time_t now)
|
2005-09-12 08:56:42 +02:00
|
|
|
{
|
2015-08-13 15:41:43 +02:00
|
|
|
(void)now;
|
2016-07-18 17:31:55 +02:00
|
|
|
|
|
|
|
log_debug(LD_GENERAL,
|
|
|
|
"In routerlist_retry_directory_downloads()");
|
|
|
|
|
2007-10-08 23:21:50 +02:00
|
|
|
router_reset_status_download_failures();
|
|
|
|
router_reset_descriptor_download_failures();
|
2015-08-13 15:41:43 +02:00
|
|
|
reschedule_directory_downloads();
|
2005-09-12 08:56:42 +02:00
|
|
|
}
|
|
|
|
|
2007-10-08 23:21:50 +02:00
|
|
|
/** Return true iff <b>router</b> does not permit exit streams.
|
2005-09-07 18:42:53 +02:00
|
|
|
*/
|
|
|
|
int
|
2010-09-29 06:38:32 +02:00
|
|
|
router_exit_policy_rejects_all(const routerinfo_t *router)
|
2005-09-07 18:42:53 +02:00
|
|
|
{
|
2008-12-29 02:47:28 +01:00
|
|
|
return router->policy_is_reject_star;
|
2007-10-08 23:21:50 +02:00
|
|
|
}
|
2005-09-07 18:42:53 +02:00
|
|
|
|
2007-10-18 16:19:51 +02:00
|
|
|
/** For every current directory connection whose purpose is <b>purpose</b>,
|
|
|
|
* and where the resource being downloaded begins with <b>prefix</b>, split
|
2012-06-05 00:50:13 +02:00
|
|
|
* rest of the resource into base16 fingerprints (or base64 fingerprints if
|
2016-02-22 08:34:50 +01:00
|
|
|
* purpose==DIR_PURPOSE_FETCH_MICRODESC), decode them, and set the
|
2010-05-11 23:20:33 +02:00
|
|
|
* corresponding elements of <b>result</b> to a nonzero value.
|
|
|
|
*/
|
2018-09-19 20:10:15 +02:00
|
|
|
void
|
2014-10-31 16:32:32 +01:00
|
|
|
list_pending_downloads(digestmap_t *result, digest256map_t *result256,
|
2007-10-09 22:44:45 +02:00
|
|
|
int purpose, const char *prefix)
|
2005-09-12 08:56:42 +02:00
|
|
|
{
|
2007-10-09 22:44:45 +02:00
|
|
|
const size_t p_len = strlen(prefix);
|
2012-01-18 21:53:30 +01:00
|
|
|
smartlist_t *tmp = smartlist_new();
|
2007-05-22 17:49:14 +02:00
|
|
|
smartlist_t *conns = get_connection_array();
|
2010-05-11 23:20:33 +02:00
|
|
|
int flags = DSR_HEX;
|
|
|
|
if (purpose == DIR_PURPOSE_FETCH_MICRODESC)
|
|
|
|
flags = DSR_DIGEST256|DSR_BASE64;
|
2005-09-12 08:56:42 +02:00
|
|
|
|
2014-10-31 16:32:32 +01:00
|
|
|
tor_assert(result || result256);
|
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);
|
|
|
|
|
2014-10-31 16:32:32 +01:00
|
|
|
if (result) {
|
|
|
|
SMARTLIST_FOREACH(tmp, char *, d,
|
2005-12-27 06:26:03 +01:00
|
|
|
{
|
|
|
|
digestmap_set(result, d, (void*)1);
|
|
|
|
tor_free(d);
|
|
|
|
});
|
2014-10-31 16:32:32 +01:00
|
|
|
} else if (result256) {
|
|
|
|
SMARTLIST_FOREACH(tmp, uint8_t *, d,
|
|
|
|
{
|
|
|
|
digest256map_set(result256, d, (void*)1);
|
|
|
|
tor_free(d);
|
|
|
|
});
|
|
|
|
}
|
2005-12-27 06:26:03 +01:00
|
|
|
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;
|
2014-10-31 16:32:32 +01:00
|
|
|
list_pending_downloads(result, NULL, purpose, "d/");
|
2007-10-09 22:44:45 +02:00
|
|
|
}
|
|
|
|
|
2012-06-05 00:50:13 +02:00
|
|
|
/** For every microdescriptor we are currently downloading by descriptor
|
2014-10-31 16:32:32 +01:00
|
|
|
* digest, set result[d] to (void*)1.
|
2012-06-05 00:50:13 +02:00
|
|
|
*/
|
2010-05-11 23:20:33 +02:00
|
|
|
void
|
2014-10-31 16:32:32 +01:00
|
|
|
list_pending_microdesc_downloads(digest256map_t *result)
|
2010-05-11 23:20:33 +02:00
|
|
|
{
|
2014-10-31 16:32:32 +01:00
|
|
|
list_pending_downloads(NULL, result, DIR_PURPOSE_FETCH_MICRODESC, "d/");
|
2010-05-11 23:20:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Launch downloads for all the descriptors whose digests or digests256
|
|
|
|
* are listed as digests[i] for lo <= i < hi. (Lo and hi may be out of
|
|
|
|
* range.) If <b>source</b> is given, download from <b>source</b>;
|
|
|
|
* otherwise, download from an appropriate random directory server.
|
2005-12-27 06:26:03 +01:00
|
|
|
*/
|
2017-05-26 20:05:50 +02:00
|
|
|
MOCK_IMPL(STATIC void,
|
|
|
|
initiate_descriptor_downloads,(const routerstatus_t *source,
|
|
|
|
int purpose, smartlist_t *digests,
|
|
|
|
int lo, int hi, int pds_flags))
|
2005-12-27 06:26:03 +01:00
|
|
|
{
|
|
|
|
char *resource, *cp;
|
2014-09-23 18:56:16 +02:00
|
|
|
int digest_len, enc_digest_len;
|
|
|
|
const char *sep;
|
|
|
|
int b64_256;
|
2014-09-23 18:32:02 +02:00
|
|
|
smartlist_t *tmp;
|
2010-05-11 23:20:33 +02:00
|
|
|
|
|
|
|
if (purpose == DIR_PURPOSE_FETCH_MICRODESC) {
|
|
|
|
/* Microdescriptors are downloaded by "-"-separated base64-encoded
|
|
|
|
* 256-bit digests. */
|
|
|
|
digest_len = DIGEST256_LEN;
|
2014-09-23 18:56:16 +02:00
|
|
|
enc_digest_len = BASE64_DIGEST256_LEN + 1;
|
2014-09-23 18:21:08 +02:00
|
|
|
sep = "-";
|
2010-05-11 23:20:33 +02:00
|
|
|
b64_256 = 1;
|
2014-09-23 18:56:16 +02:00
|
|
|
} else {
|
|
|
|
digest_len = DIGEST_LEN;
|
|
|
|
enc_digest_len = HEX_DIGEST_LEN + 1;
|
|
|
|
sep = "+";
|
|
|
|
b64_256 = 0;
|
2010-05-11 23:20:33 +02:00
|
|
|
}
|
|
|
|
|
2005-12-27 06:26:03 +01:00
|
|
|
if (lo < 0)
|
|
|
|
lo = 0;
|
|
|
|
if (hi > smartlist_len(digests))
|
|
|
|
hi = smartlist_len(digests);
|
|
|
|
|
2014-09-23 18:21:08 +02:00
|
|
|
if (hi-lo <= 0)
|
|
|
|
return;
|
|
|
|
|
2014-09-23 18:32:02 +02:00
|
|
|
tmp = smartlist_new();
|
2014-09-23 18:21:08 +02:00
|
|
|
|
|
|
|
for (; lo < hi; ++lo) {
|
|
|
|
cp = tor_malloc(enc_digest_len);
|
2010-05-11 23:20:33 +02:00
|
|
|
if (b64_256) {
|
2014-09-23 18:21:08 +02:00
|
|
|
digest256_to_base64(cp, smartlist_get(digests, lo));
|
2010-05-11 23:20:33 +02:00
|
|
|
} else {
|
2015-01-23 17:18:28 +01:00
|
|
|
base16_encode(cp, enc_digest_len, smartlist_get(digests, lo),
|
|
|
|
digest_len);
|
2010-05-11 23:20:33 +02:00
|
|
|
}
|
2014-09-23 18:21:08 +02:00
|
|
|
smartlist_add(tmp, cp);
|
2005-09-22 08:34:29 +02:00
|
|
|
}
|
2014-09-23 18:21:08 +02:00
|
|
|
|
|
|
|
cp = smartlist_join_strings(tmp, sep, 0, NULL);
|
|
|
|
tor_asprintf(&resource, "d/%s.z", cp);
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH(tmp, char *, cp1, tor_free(cp1));
|
|
|
|
smartlist_free(tmp);
|
|
|
|
tor_free(cp);
|
2005-09-22 08:34:29 +02:00
|
|
|
|
2005-12-27 06:26:03 +01:00
|
|
|
if (source) {
|
2015-12-07 07:55:38 +01:00
|
|
|
/* We know which authority or directory mirror we want. */
|
2017-04-21 21:08:28 +02:00
|
|
|
directory_request_t *req = directory_request_new(purpose);
|
|
|
|
directory_request_set_routerstatus(req, source);
|
|
|
|
directory_request_set_resource(req, resource);
|
|
|
|
directory_initiate_request(req);
|
|
|
|
directory_request_free(req);
|
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,
|
2015-12-07 07:55:38 +01:00
|
|
|
pds_flags, DL_WANT_ANY_DIRSERVER);
|
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
|
|
|
|
2014-09-29 16:56:38 +02:00
|
|
|
/** Return the max number of hashes to put in a URL for a given request.
|
2005-12-27 06:26:03 +01:00
|
|
|
*/
|
2014-09-23 18:34:17 +02:00
|
|
|
static int
|
2014-09-23 18:22:28 +02:00
|
|
|
max_dl_per_request(const or_options_t *options, int purpose)
|
|
|
|
{
|
2014-09-29 16:56:38 +02:00
|
|
|
/* Since squid does not like URLs >= 4096 bytes we limit it to 96.
|
2016-01-13 08:35:35 +01:00
|
|
|
* 4096 - strlen(http://[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535
|
|
|
|
* /tor/server/d/.z) == 4026
|
|
|
|
* 4026/41 (40 for the hash and 1 for the + that separates them) => 98
|
2014-09-29 16:56:38 +02:00
|
|
|
* So use 96 because it's a nice number.
|
2016-01-12 21:19:56 +01:00
|
|
|
*
|
|
|
|
* For microdescriptors, the calculation is
|
2016-01-13 08:35:35 +01:00
|
|
|
* 4096 - strlen(http://[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535
|
|
|
|
* /tor/micro/d/.z) == 4027
|
|
|
|
* 4027/44 (43 for the hash and 1 for the - that separates them) => 91
|
2016-01-12 21:19:56 +01:00
|
|
|
* So use 90 because it's a nice number.
|
2014-09-29 16:56:38 +02:00
|
|
|
*/
|
2014-09-23 18:22:28 +02:00
|
|
|
int max = 96;
|
|
|
|
if (purpose == DIR_PURPOSE_FETCH_MICRODESC) {
|
2016-01-12 21:19:56 +01:00
|
|
|
max = 90;
|
2014-09-23 19:04:22 +02:00
|
|
|
}
|
2014-09-29 16:56:38 +02:00
|
|
|
/* If we're going to tunnel our connections, we can ask for a lot more
|
|
|
|
* in a request. */
|
2016-06-02 16:40:39 +02:00
|
|
|
if (directory_must_use_begindir(options)) {
|
2014-09-29 16:56:38 +02:00
|
|
|
max = 500;
|
2014-09-23 18:22:28 +02:00
|
|
|
}
|
|
|
|
return max;
|
|
|
|
}
|
|
|
|
|
2007-10-16 01:15:24 +02:00
|
|
|
/** Don't split our requests so finely that we are requesting fewer than
|
2017-08-11 19:54:01 +02:00
|
|
|
* this number per server. (Grouping more than this at once leads to
|
|
|
|
* diminishing returns.) */
|
|
|
|
#define MIN_DL_PER_REQUEST 32
|
2007-10-16 01:15:24 +02:00
|
|
|
/** To prevent a single screwy cache from confusing us by selective reply,
|
2009-12-18 12:55:05 +01:00
|
|
|
* try to split our requests into at least this many requests. */
|
2005-09-22 08:34:29 +02:00
|
|
|
#define MIN_REQUESTS 3
|
2007-10-16 01:15:24 +02:00
|
|
|
/** If we want fewer than this many descriptors, wait until we
|
2013-05-16 12:08:48 +02:00
|
|
|
* want more, or until TestingClientMaxIntervalWithoutRequest has passed. */
|
2005-09-30 22:04:55 +02:00
|
|
|
#define MAX_DL_TO_DELAY 16
|
2007-10-08 21:56:57 +02:00
|
|
|
|
2010-05-11 23:20:33 +02:00
|
|
|
/** Given a <b>purpose</b> (FETCH_MICRODESC or FETCH_SERVERDESC) and a list of
|
|
|
|
* router descriptor digests or microdescriptor digest256s in
|
|
|
|
* <b>downloadable</b>, decide whether to delay fetching until we have more.
|
|
|
|
* If we don't want to delay, launch one or more requests to the appropriate
|
|
|
|
* directory authorities.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
launch_descriptor_downloads(int purpose,
|
|
|
|
smartlist_t *downloadable,
|
2010-09-29 07:35:08 +02:00
|
|
|
const routerstatus_t *source, time_t now)
|
2007-10-08 21:56:57 +02:00
|
|
|
{
|
2011-06-14 19:01:38 +02:00
|
|
|
const or_options_t *options = get_options();
|
2010-05-11 23:20:33 +02:00
|
|
|
const char *descname;
|
2014-09-23 18:22:28 +02:00
|
|
|
const int fetch_microdesc = (purpose == DIR_PURPOSE_FETCH_MICRODESC);
|
2014-09-23 18:34:17 +02:00
|
|
|
int n_downloadable = smartlist_len(downloadable);
|
2010-05-11 23:20:33 +02:00
|
|
|
|
2014-09-23 18:34:17 +02:00
|
|
|
int i, n_per_request, max_dl_per_req;
|
|
|
|
const char *req_plural = "", *rtr_plural = "";
|
|
|
|
int pds_flags = PDS_RETRY_IF_NO_SERVERS;
|
2010-05-11 23:20:33 +02:00
|
|
|
|
2014-09-23 18:22:28 +02:00
|
|
|
tor_assert(fetch_microdesc || purpose == DIR_PURPOSE_FETCH_SERVERDESC);
|
|
|
|
descname = fetch_microdesc ? "microdesc" : "routerdesc";
|
2010-05-11 23:20:33 +02:00
|
|
|
|
2014-09-23 18:22:28 +02:00
|
|
|
if (!n_downloadable)
|
|
|
|
return;
|
2007-10-08 21:56:57 +02:00
|
|
|
|
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);
|
2017-11-08 19:22:16 +01:00
|
|
|
} else if (! router_have_minimum_dir_info()) {
|
|
|
|
log_debug(LD_DIR,
|
|
|
|
"We are only missing %d %ss, but we'll fetch anyway, since "
|
|
|
|
"we don't yet have enough directory info.",
|
|
|
|
n_downloadable, descname);
|
2007-10-08 21:56:57 +02:00
|
|
|
} else {
|
2014-09-23 18:22:28 +02:00
|
|
|
|
|
|
|
/* should delay */
|
|
|
|
if ((last_descriptor_download_attempted +
|
|
|
|
options->TestingClientMaxIntervalWithoutRequest) > now)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (last_descriptor_download_attempted) {
|
|
|
|
log_info(LD_DIR,
|
|
|
|
"There are not many downloadable %ss, but we've "
|
|
|
|
"been waiting long enough (%d seconds). Downloading.",
|
|
|
|
descname,
|
|
|
|
(int)(now-last_descriptor_download_attempted));
|
|
|
|
} else {
|
|
|
|
log_info(LD_DIR,
|
|
|
|
"There are not many downloadable %ss, but we haven't "
|
|
|
|
"tried downloading descriptors recently. Downloading.",
|
|
|
|
descname);
|
2006-02-19 20:44:09 +01:00
|
|
|
}
|
|
|
|
}
|
2005-10-05 22:45:18 +02:00
|
|
|
}
|
2005-10-04 22:18:26 +02:00
|
|
|
|
2017-06-02 18:33:40 +02:00
|
|
|
if (!authdir_mode(options)) {
|
2014-09-23 18:22:28 +02:00
|
|
|
/* 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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
pds_flags |= fetch_microdesc ?
|
|
|
|
PDS_NO_EXISTING_MICRODESC_FETCH :
|
|
|
|
PDS_NO_EXISTING_SERVERDESC_FETCH;
|
|
|
|
}
|
2008-12-11 20:12:55 +01:00
|
|
|
|
2014-09-23 18:22:28 +02:00
|
|
|
n_per_request = CEIL_DIV(n_downloadable, MIN_REQUESTS);
|
|
|
|
max_dl_per_req = max_dl_per_request(options, purpose);
|
|
|
|
|
|
|
|
if (n_per_request > max_dl_per_req)
|
|
|
|
n_per_request = max_dl_per_req;
|
|
|
|
|
2017-05-23 15:47:11 +02:00
|
|
|
if (n_per_request < MIN_DL_PER_REQUEST) {
|
|
|
|
n_per_request = MIN(MIN_DL_PER_REQUEST, n_downloadable);
|
|
|
|
}
|
2014-09-23 18:22:28 +02:00
|
|
|
|
|
|
|
if (n_downloadable > n_per_request)
|
|
|
|
req_plural = rtr_plural = "s";
|
|
|
|
else if (n_downloadable > 1)
|
|
|
|
rtr_plural = "s";
|
|
|
|
|
|
|
|
log_info(LD_DIR,
|
|
|
|
"Launching %d request%s for %d %s%s, %d at a time",
|
|
|
|
CEIL_DIV(n_downloadable, n_per_request), req_plural,
|
|
|
|
n_downloadable, descname, rtr_plural, n_per_request);
|
|
|
|
smartlist_sort_digests(downloadable);
|
|
|
|
for (i=0; i < n_downloadable; i += n_per_request) {
|
|
|
|
initiate_descriptor_downloads(source, purpose,
|
|
|
|
downloadable, i, i+n_per_request,
|
|
|
|
pds_flags);
|
2005-09-18 04:24:42 +02:00
|
|
|
}
|
2014-09-23 18:22:28 +02:00
|
|
|
last_descriptor_download_attempted = now;
|
2005-09-15 07:19:38 +02:00
|
|
|
}
|
|
|
|
|
2010-03-16 09:36:14 +01:00
|
|
|
/** For any descriptor that we want that's currently listed in
|
|
|
|
* <b>consensus</b>, download it as appropriate. */
|
2010-04-20 09:03:33 +02:00
|
|
|
void
|
|
|
|
update_consensus_router_descriptor_downloads(time_t now, int is_vote,
|
2010-03-16 09:36:14 +01:00
|
|
|
networkstatus_t *consensus)
|
2007-10-08 21:56:57 +02:00
|
|
|
{
|
2011-06-14 19:01:38 +02:00
|
|
|
const or_options_t *options = get_options();
|
2007-10-08 21:56:57 +02:00
|
|
|
digestmap_t *map = NULL;
|
2012-01-18 21:53:30 +01:00
|
|
|
smartlist_t *no_longer_old = smartlist_new();
|
|
|
|
smartlist_t *downloadable = smartlist_new();
|
2010-04-20 09:20:31 +02:00
|
|
|
routerstatus_t *source = NULL;
|
2007-10-08 21:56:57 +02:00
|
|
|
int authdir = authdir_mode(options);
|
2007-10-16 20:47:32 +02:00
|
|
|
int n_delayed=0, n_have=0, n_would_reject=0, n_wouldnt_use=0,
|
2007-11-06 21:51:45 +01:00
|
|
|
n_inprogress=0, n_in_oldrouters=0;
|
2007-10-08 23:21:50 +02:00
|
|
|
|
2007-12-04 19:35:03 +01:00
|
|
|
if (directory_too_idle_to_fetch_descriptors(options, now))
|
2007-11-08 05:19:51 +01:00
|
|
|
goto done;
|
2007-10-08 23:21:50 +02:00
|
|
|
if (!consensus)
|
2007-11-07 16:19:53 +01:00
|
|
|
goto done;
|
2007-10-08 21:56:57 +02:00
|
|
|
|
2010-04-20 09:20:31 +02:00
|
|
|
if (is_vote) {
|
|
|
|
/* where's it from, so we know whom to ask for descriptors */
|
2012-09-10 21:55:27 +02:00
|
|
|
dir_server_t *ds;
|
2010-04-20 09:20:31 +02:00
|
|
|
networkstatus_voter_info_t *voter = smartlist_get(consensus->voters, 0);
|
|
|
|
tor_assert(voter);
|
|
|
|
ds = trusteddirserver_get_by_v3_auth_digest(voter->identity_digest);
|
|
|
|
if (ds)
|
|
|
|
source = &(ds->fake_status);
|
|
|
|
else
|
|
|
|
log_warn(LD_DIR, "couldn't lookup source from vote?");
|
|
|
|
}
|
|
|
|
|
2007-10-08 21:56:57 +02:00
|
|
|
map = digestmap_new();
|
|
|
|
list_pending_descriptor_downloads(map, 0);
|
2010-09-29 06:38:32 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(consensus->routerstatus_list, void *, rsp) {
|
2010-04-20 09:03:33 +02:00
|
|
|
routerstatus_t *rs =
|
|
|
|
is_vote ? &(((vote_routerstatus_t *)rsp)->status) : rsp;
|
2007-11-06 21:51:45 +01:00
|
|
|
signed_descriptor_t *sd;
|
|
|
|
if ((sd = router_get_by_descriptor_digest(rs->descriptor_digest))) {
|
2010-09-29 06:38:32 +02:00
|
|
|
const routerinfo_t *ri;
|
2007-10-16 20:47:32 +02:00
|
|
|
++n_have;
|
2010-10-14 17:49:51 +02:00
|
|
|
if (!(ri = router_get_by_id_digest(rs->identity_digest)) ||
|
2011-05-10 22:58:38 +02:00
|
|
|
tor_memneq(ri->cache_info.signed_descriptor_digest,
|
2007-11-06 21:51:45 +01:00
|
|
|
sd->signed_descriptor_digest, DIGEST_LEN)) {
|
2007-11-12 16:46:38 +01:00
|
|
|
/* We have a descriptor with this digest, but either there is no
|
|
|
|
* entry in routerlist with the same ID (!ri), or there is one,
|
2012-12-13 23:34:05 +01:00
|
|
|
* but the identity digest differs (memneq).
|
2007-11-12 16:46:38 +01:00
|
|
|
*/
|
2007-11-07 16:19:53 +01:00
|
|
|
smartlist_add(no_longer_old, sd);
|
|
|
|
++n_in_oldrouters; /* We have it in old_routers. */
|
2007-11-06 21:51:45 +01:00
|
|
|
}
|
2007-10-08 21:56:57 +02:00
|
|
|
continue; /* We have it already. */
|
2007-10-16 20:47:32 +02:00
|
|
|
}
|
2008-01-18 05:47:03 +01:00
|
|
|
if (digestmap_get(map, rs->descriptor_digest)) {
|
|
|
|
++n_inprogress;
|
|
|
|
continue; /* We have an in-progress download. */
|
|
|
|
}
|
2018-01-31 21:03:47 +01:00
|
|
|
if (!download_status_is_ready(&rs->dl_status, now)) {
|
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
|
|
|
}
|
2016-11-30 01:06:36 +01:00
|
|
|
if (!we_want_to_fetch_flavor(options, consensus->flavor) &&
|
2017-05-10 23:57:35 +02:00
|
|
|
!client_would_use_router(rs, now)) {
|
2007-10-16 20:47:32 +02:00
|
|
|
++n_wouldnt_use;
|
2007-10-08 21:56:57 +02:00
|
|
|
continue; /* We would never use it ourself. */
|
2007-10-16 20:47:32 +02:00
|
|
|
}
|
2010-04-20 09:20:31 +02:00
|
|
|
if (is_vote && source) {
|
2010-04-21 00:02:23 +02:00
|
|
|
char time_bufnew[ISO_TIME_LEN+1];
|
|
|
|
char time_bufold[ISO_TIME_LEN+1];
|
2010-09-29 06:38:32 +02:00
|
|
|
const routerinfo_t *oldrouter;
|
2010-10-14 17:49:51 +02:00
|
|
|
oldrouter = router_get_by_id_digest(rs->identity_digest);
|
2010-04-21 00:02:23 +02:00
|
|
|
format_iso_time(time_bufnew, rs->published_on);
|
|
|
|
if (oldrouter)
|
|
|
|
format_iso_time(time_bufold, oldrouter->cache_info.published_on);
|
|
|
|
log_info(LD_DIR, "Learned about %s (%s vs %s) from %s's vote (%s)",
|
2011-05-16 03:58:46 +02:00
|
|
|
routerstatus_describe(rs),
|
|
|
|
time_bufnew,
|
2010-04-21 00:02:23 +02:00
|
|
|
oldrouter ? time_bufold : "none",
|
|
|
|
source->nickname, oldrouter ? "known" : "unknown");
|
2010-04-20 09:20:31 +02:00
|
|
|
}
|
2007-10-08 21:56:57 +02:00
|
|
|
smartlist_add(downloadable, rs->descriptor_digest);
|
2010-09-29 06:38:32 +02:00
|
|
|
} SMARTLIST_FOREACH_END(rsp);
|
2007-10-08 21:56:57 +02:00
|
|
|
|
2017-06-02 18:32:41 +02:00
|
|
|
if (!authdir_mode_v3(options)
|
2007-12-19 00:45:24 +01:00
|
|
|
&& smartlist_len(no_longer_old)) {
|
2007-11-07 16:19:53 +01:00
|
|
|
routerlist_t *rl = router_get_routerlist();
|
2007-11-07 18:11:23 +01:00
|
|
|
log_info(LD_DIR, "%d router descriptors listed in consensus are "
|
2007-11-11 18:20:30 +01:00
|
|
|
"currently in old_routers; making them current.",
|
2007-11-07 18:11:23 +01:00
|
|
|
smartlist_len(no_longer_old));
|
2012-07-17 15:33:38 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(no_longer_old, signed_descriptor_t *, sd) {
|
2007-11-07 16:19:53 +01:00
|
|
|
const char *msg;
|
2008-12-17 22:50:01 +01:00
|
|
|
was_router_added_t r;
|
2018-01-07 08:15:18 +01:00
|
|
|
time_t tmp_cert_expiration_time;
|
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;
|
|
|
|
}
|
2018-01-07 08:15:18 +01:00
|
|
|
/* need to remember for below, since add_to_routerlist may free. */
|
|
|
|
tmp_cert_expiration_time = ri->cert_expiration_time;
|
2017-11-30 17:44:15 +01:00
|
|
|
|
2007-11-07 16:19:53 +01:00
|
|
|
r = router_add_to_routerlist(ri, &msg, 1, 0);
|
2008-12-17 22:50:01 +01:00
|
|
|
if (WRA_WAS_OUTDATED(r)) {
|
2017-11-16 14:35:06 +01:00
|
|
|
log_warn(LD_DIR, "Couldn't add re-parsed router: %s. This isn't "
|
|
|
|
"usually a big deal, but you should make sure that your "
|
|
|
|
"clock and timezone are set correctly.",
|
2007-11-07 18:11:23 +01:00
|
|
|
msg?msg:"???");
|
2017-11-16 14:35:06 +01:00
|
|
|
if (r == ROUTER_CERTS_EXPIRED) {
|
|
|
|
char time_cons[ISO_TIME_LEN+1];
|
2018-01-07 08:15:18 +01:00
|
|
|
char time_cert_expires[ISO_TIME_LEN+1];
|
2017-11-16 14:35:06 +01:00
|
|
|
format_iso_time(time_cons, consensus->valid_after);
|
2018-01-07 08:15:18 +01:00
|
|
|
format_iso_time(time_cert_expires, tmp_cert_expiration_time);
|
2017-11-16 14:35:06 +01:00
|
|
|
log_warn(LD_DIR, " (I'm looking at a consensus from %s; This "
|
|
|
|
"router's certificates began expiring at %s.)",
|
2017-11-30 17:44:15 +01:00
|
|
|
time_cons, time_cert_expires);
|
2017-11-16 14:35:06 +01:00
|
|
|
}
|
2007-11-07 16:19:53 +01:00
|
|
|
}
|
2012-07-17 15:33:38 +02:00
|
|
|
} SMARTLIST_FOREACH_END(sd);
|
2007-11-07 16:19:53 +01:00
|
|
|
routerlist_assert_ok(rl);
|
|
|
|
}
|
|
|
|
|
2007-10-16 20:47:32 +02:00
|
|
|
log_info(LD_DIR,
|
2007-11-07 16:19:53 +01:00
|
|
|
"%d router descriptors downloadable. %d delayed; %d present "
|
2007-11-07 18:11:23 +01:00
|
|
|
"(%d of those were in old_routers); %d would_reject; "
|
2008-02-21 10:01:32 +01:00
|
|
|
"%d wouldnt_use; %d in progress.",
|
2007-11-06 21:51:45 +01:00
|
|
|
smartlist_len(downloadable), n_delayed, n_have, n_in_oldrouters,
|
|
|
|
n_would_reject, n_wouldnt_use, n_inprogress);
|
2007-10-16 20:47:32 +02:00
|
|
|
|
2010-05-11 23:20:33 +02:00
|
|
|
launch_descriptor_downloads(DIR_PURPOSE_FETCH_SERVERDESC,
|
|
|
|
downloadable, source, now);
|
2007-10-08 21:56:57 +02:00
|
|
|
|
|
|
|
digestmap_free(map, NULL);
|
2007-11-07 16:19:53 +01:00
|
|
|
done:
|
|
|
|
smartlist_free(downloadable);
|
|
|
|
smartlist_free(no_longer_old);
|
2007-10-08 21:56:57 +02:00
|
|
|
}
|
|
|
|
|
2008-04-22 23:10:41 +02:00
|
|
|
/** How often should we launch a server/authority request to be sure of getting
|
|
|
|
* a guess for our IP? */
|
2016-05-30 22:18:16 +02:00
|
|
|
/*XXXX+ this info should come from netinfo cells or something, or we should
|
2008-04-22 23:10:41 +02:00
|
|
|
* do this only when we aren't seeing incoming data. see bug 652. */
|
|
|
|
#define DUMMY_DOWNLOAD_INTERVAL (20*60)
|
|
|
|
|
2010-11-12 18:05:27 +01:00
|
|
|
/** As needed, launch a dummy router descriptor fetch to see if our
|
|
|
|
* address has changed. */
|
|
|
|
static void
|
2011-06-14 19:01:38 +02:00
|
|
|
launch_dummy_descriptor_download_as_needed(time_t now,
|
|
|
|
const or_options_t *options)
|
2010-11-12 18:05:27 +01:00
|
|
|
{
|
|
|
|
static time_t last_dummy_download = 0;
|
2016-05-30 22:18:16 +02:00
|
|
|
/* XXXX+ we could be smarter here; see notes on bug 652. */
|
2010-11-12 18:05:27 +01:00
|
|
|
/* If we're a server that doesn't have a configured address, we rely on
|
|
|
|
* directory fetches to learn when our address changes. So if we haven't
|
|
|
|
* tried to get any routerdescs in a long time, try a dummy fetch now. */
|
|
|
|
if (!options->Address &&
|
|
|
|
server_mode(options) &&
|
|
|
|
last_descriptor_download_attempted + DUMMY_DOWNLOAD_INTERVAL < now &&
|
|
|
|
last_dummy_download + DUMMY_DOWNLOAD_INTERVAL < now) {
|
|
|
|
last_dummy_download = now;
|
2015-12-07 07:55:38 +01:00
|
|
|
/* XX/teor - do we want an authority here, because they are less likely
|
|
|
|
* to give us the wrong address? (See #17782)
|
|
|
|
* I'm leaving the previous behaviour intact, because I don't like
|
|
|
|
* the idea of some relays contacting an authority every 20 minutes. */
|
2010-11-12 18:05:27 +01:00
|
|
|
directory_get_from_dirserver(DIR_PURPOSE_FETCH_SERVERDESC,
|
|
|
|
ROUTER_PURPOSE_GENERAL, "authority.z",
|
2015-12-07 07:55:38 +01:00
|
|
|
PDS_RETRY_IF_NO_SERVERS,
|
|
|
|
DL_WANT_ANY_DIRSERVER);
|
2010-11-12 18:05:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-17 06:50:41 +01:00
|
|
|
/** Launch downloads for router status as needed. */
|
2005-12-27 06:26:03 +01:00
|
|
|
void
|
|
|
|
update_router_descriptor_downloads(time_t now)
|
|
|
|
{
|
2011-06-14 19:01:38 +02:00
|
|
|
const or_options_t *options = get_options();
|
2014-03-08 14:13:12 +01:00
|
|
|
if (should_delay_dir_fetches(options, NULL))
|
2007-06-12 11:17:23 +02:00
|
|
|
return;
|
2010-11-08 20:21:32 +01:00
|
|
|
if (!we_fetch_router_descriptors(options))
|
|
|
|
return;
|
2010-09-20 20:18:17 +02:00
|
|
|
|
2010-04-20 09:03:33 +02:00
|
|
|
update_consensus_router_descriptor_downloads(now, 0,
|
2010-09-20 20:18:17 +02:00
|
|
|
networkstatus_get_reasonably_live_consensus(now, FLAV_NS));
|
2005-12-27 06:26:03 +01:00
|
|
|
}
|
|
|
|
|
2007-06-12 11:17:23 +02:00
|
|
|
/** Launch extrainfo downloads as needed. */
|
2007-05-18 23:19:19 +02:00
|
|
|
void
|
|
|
|
update_extrainfo_downloads(time_t now)
|
|
|
|
{
|
2011-06-14 19:01:38 +02:00
|
|
|
const or_options_t *options = get_options();
|
2007-05-18 23:19:19 +02:00
|
|
|
routerlist_t *rl;
|
|
|
|
smartlist_t *wanted;
|
|
|
|
digestmap_t *pending;
|
2014-09-23 18:34:17 +02:00
|
|
|
int old_routers, i, max_dl_per_req;
|
2015-02-18 19:05:15 +01:00
|
|
|
int n_no_ei = 0, n_pending = 0, n_have = 0, n_delay = 0, n_bogus[2] = {0,0};
|
2007-05-18 23:19:19 +02:00
|
|
|
if (! options->DownloadExtraInfo)
|
|
|
|
return;
|
2014-03-08 14:13:12 +01:00
|
|
|
if (should_delay_dir_fetches(options, NULL))
|
2007-06-12 11:17:23 +02:00
|
|
|
return;
|
2007-10-21 01:45:07 +02:00
|
|
|
if (!router_have_minimum_dir_info())
|
|
|
|
return;
|
2007-05-18 23:19:19 +02:00
|
|
|
|
|
|
|
pending = digestmap_new();
|
|
|
|
list_pending_descriptor_downloads(pending, 1);
|
|
|
|
rl = router_get_routerlist();
|
2012-01-18 21:53:30 +01:00
|
|
|
wanted = smartlist_new();
|
2007-10-18 16:50:59 +02:00
|
|
|
for (old_routers = 0; old_routers < 2; ++old_routers) {
|
|
|
|
smartlist_t *lst = old_routers ? rl->old_routers : rl->routers;
|
|
|
|
for (i = 0; i < smartlist_len(lst); ++i) {
|
|
|
|
signed_descriptor_t *sd;
|
|
|
|
char *d;
|
|
|
|
if (old_routers)
|
|
|
|
sd = smartlist_get(lst, i);
|
|
|
|
else
|
|
|
|
sd = &((routerinfo_t*)smartlist_get(lst, i))->cache_info;
|
|
|
|
if (sd->is_extrainfo)
|
|
|
|
continue; /* This should never happen. */
|
2010-10-14 17:49:51 +02:00
|
|
|
if (old_routers && !router_get_by_id_digest(sd->identity_digest))
|
2007-10-21 06:41:00 +02:00
|
|
|
continue; /* Couldn't check the signature if we got it. */
|
|
|
|
if (sd->extrainfo_is_bogus)
|
|
|
|
continue;
|
2007-10-18 16:50:59 +02:00
|
|
|
d = sd->extra_info_digest;
|
|
|
|
if (tor_digest_is_zero(d)) {
|
|
|
|
++n_no_ei;
|
|
|
|
continue;
|
2007-05-18 23:19:19 +02:00
|
|
|
}
|
2007-10-18 16:50:59 +02:00
|
|
|
if (eimap_get(rl->extra_info_map, d)) {
|
|
|
|
++n_have;
|
|
|
|
continue;
|
|
|
|
}
|
2018-01-31 21:03:47 +01:00
|
|
|
if (!download_status_is_ready(&sd->ei_dl_status, now)) {
|
2007-10-18 16:50:59 +02:00
|
|
|
++n_delay;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (digestmap_get(pending, d)) {
|
|
|
|
++n_pending;
|
|
|
|
continue;
|
|
|
|
}
|
2015-02-19 15:27:42 +01:00
|
|
|
|
|
|
|
const signed_descriptor_t *sd2 = router_get_by_extrainfo_digest(d);
|
|
|
|
if (sd2 != sd) {
|
|
|
|
if (sd2 != NULL) {
|
|
|
|
char d1[HEX_DIGEST_LEN+1], d2[HEX_DIGEST_LEN+1];
|
|
|
|
char d3[HEX_DIGEST_LEN+1], d4[HEX_DIGEST_LEN+1];
|
|
|
|
base16_encode(d1, sizeof(d1), sd->identity_digest, DIGEST_LEN);
|
|
|
|
base16_encode(d2, sizeof(d2), sd2->identity_digest, DIGEST_LEN);
|
|
|
|
base16_encode(d3, sizeof(d3), d, DIGEST_LEN);
|
|
|
|
base16_encode(d4, sizeof(d3), sd2->extra_info_digest, DIGEST_LEN);
|
|
|
|
|
|
|
|
log_info(LD_DIR, "Found an entry in %s with mismatched "
|
|
|
|
"router_get_by_extrainfo_digest() value. This has ID %s "
|
|
|
|
"but the entry in the map has ID %s. This has EI digest "
|
|
|
|
"%s and the entry in the map has EI digest %s.",
|
|
|
|
old_routers?"old_routers":"routers",
|
|
|
|
d1, d2, d3, d4);
|
|
|
|
} else {
|
|
|
|
char d1[HEX_DIGEST_LEN+1], d2[HEX_DIGEST_LEN+1];
|
|
|
|
base16_encode(d1, sizeof(d1), sd->identity_digest, DIGEST_LEN);
|
|
|
|
base16_encode(d2, sizeof(d2), d, DIGEST_LEN);
|
|
|
|
|
|
|
|
log_info(LD_DIR, "Found an entry in %s with NULL "
|
|
|
|
"router_get_by_extrainfo_digest() value. This has ID %s "
|
|
|
|
"and EI digest %s.",
|
|
|
|
old_routers?"old_routers":"routers",
|
|
|
|
d1, d2);
|
|
|
|
}
|
2015-02-18 19:05:15 +01:00
|
|
|
++n_bogus[old_routers];
|
|
|
|
continue;
|
|
|
|
}
|
2007-10-18 16:50:59 +02:00
|
|
|
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 "
|
2015-02-19 15:27:42 +01:00
|
|
|
"with present ei, %d delaying, %d pending, %d downloadable, %d "
|
|
|
|
"bogus in routers, %d bogus in old_routers",
|
|
|
|
n_no_ei, n_have, n_delay, n_pending, smartlist_len(wanted),
|
|
|
|
n_bogus[0], n_bogus[1]);
|
2015-02-18 19:05:15 +01:00
|
|
|
|
2007-05-18 23:19:19 +02:00
|
|
|
smartlist_shuffle(wanted);
|
2014-09-23 18:34:17 +02:00
|
|
|
|
|
|
|
max_dl_per_req = max_dl_per_request(options, DIR_PURPOSE_FETCH_EXTRAINFO);
|
2014-09-23 18:22:28 +02:00
|
|
|
for (i = 0; i < smartlist_len(wanted); i += max_dl_per_req) {
|
2007-05-18 23:19:19 +02:00
|
|
|
initiate_descriptor_downloads(NULL, DIR_PURPOSE_FETCH_EXTRAINFO,
|
2014-09-23 18:22:28 +02:00
|
|
|
wanted, i, i+max_dl_per_req,
|
2008-12-11 20:12:55 +01:00
|
|
|
PDS_RETRY_IF_NO_SERVERS|PDS_NO_EXISTING_SERVERDESC_FETCH);
|
2007-05-18 23:19:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
smartlist_free(wanted);
|
|
|
|
}
|
|
|
|
|
2017-10-10 20:36:08 +02:00
|
|
|
/** Reset the consensus and extra-info download failure count on all routers.
|
|
|
|
* When we get a new consensus,
|
|
|
|
* routers_update_status_from_consensus_networkstatus() will reset the
|
|
|
|
* download statuses on the descriptors in that consensus.
|
2005-09-30 22:04:55 +02:00
|
|
|
*/
|
2005-09-22 08:34:29 +02:00
|
|
|
void
|
|
|
|
router_reset_descriptor_download_failures(void)
|
|
|
|
{
|
2016-07-18 17:31:55 +02:00
|
|
|
log_debug(LD_GENERAL,
|
|
|
|
"In router_reset_descriptor_download_failures()");
|
|
|
|
|
2007-10-16 01:15:24 +02:00
|
|
|
networkstatus_reset_download_failures();
|
2010-11-12 18:05:27 +01:00
|
|
|
last_descriptor_download_attempted = 0;
|
2007-05-24 19:13:02 +02:00
|
|
|
if (!routerlist)
|
|
|
|
return;
|
2017-10-10 20:36:08 +02:00
|
|
|
/* We want to download *all* extra-info descriptors, not just those in
|
|
|
|
* the consensus we currently have (or are about to have) */
|
2007-05-24 19:13:02 +02:00
|
|
|
SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, ri,
|
|
|
|
{
|
2007-10-09 17:27:45 +02:00
|
|
|
download_status_reset(&ri->cache_info.ei_dl_status);
|
2007-05-24 19:13:02 +02:00
|
|
|
});
|
|
|
|
SMARTLIST_FOREACH(routerlist->old_routers, signed_descriptor_t *, sd,
|
|
|
|
{
|
2007-10-09 17:27:45 +02:00
|
|
|
download_status_reset(&sd->ei_dl_status);
|
2007-05-24 19:13:02 +02:00
|
|
|
});
|
2005-09-22 08:34:29 +02:00
|
|
|
}
|
2005-09-23 19:11:20 +02:00
|
|
|
|
2006-03-12 23:48:18 +01:00
|
|
|
/** Any changes in a router descriptor's publication time larger than this are
|
|
|
|
* automatically non-cosmetic. */
|
2012-03-30 21:38:16 +02:00
|
|
|
#define ROUTER_MAX_COSMETIC_TIME_DIFFERENCE (2*60*60)
|
2006-03-12 23:48:18 +01:00
|
|
|
|
2006-06-18 18:05:54 +02:00
|
|
|
/** We allow uptime to vary from how much it ought to be by this much. */
|
2006-11-02 00:53:15 +01:00
|
|
|
#define ROUTER_ALLOW_UPTIME_DRIFT (6*60*60)
|
2006-06-18 18:05:54 +02:00
|
|
|
|
2005-10-12 15:49:13 +02:00
|
|
|
/** Return true iff the only differences between r1 and r2 are such that
|
2005-11-01 04:48:51 +01:00
|
|
|
* would not cause a recent (post 0.1.1.6) dirserver to republish.
|
2005-10-12 15:49:13 +02:00
|
|
|
*/
|
|
|
|
int
|
2010-09-29 06:38:32 +02:00
|
|
|
router_differences_are_cosmetic(const routerinfo_t *r1, const routerinfo_t *r2)
|
2005-10-12 15:49:13 +02:00
|
|
|
{
|
2005-12-14 21:40:40 +01:00
|
|
|
time_t r1pub, r2pub;
|
2008-02-22 20:09:45 +01:00
|
|
|
long time_difference;
|
2005-10-12 20:25:25 +02:00
|
|
|
tor_assert(r1 && r2);
|
|
|
|
|
2005-10-19 18:21:19 +02:00
|
|
|
/* r1 should be the one that was published first. */
|
2005-11-05 21:15:27 +01:00
|
|
|
if (r1->cache_info.published_on > r2->cache_info.published_on) {
|
2010-09-29 06:38:32 +02:00
|
|
|
const routerinfo_t *ri_tmp = r2;
|
2005-10-12 20:25:25 +02:00
|
|
|
r2 = r1;
|
|
|
|
r1 = ri_tmp;
|
|
|
|
}
|
|
|
|
|
2005-10-12 15:49:13 +02:00
|
|
|
/* If any key fields differ, they're different. */
|
2013-02-10 04:07:22 +01:00
|
|
|
if (r1->addr != r2->addr ||
|
2005-10-12 15:49:13 +02:00
|
|
|
strcasecmp(r1->nickname, r2->nickname) ||
|
|
|
|
r1->or_port != r2->or_port ||
|
2012-07-19 20:41:23 +02:00
|
|
|
!tor_addr_eq(&r1->ipv6_addr, &r2->ipv6_addr) ||
|
|
|
|
r1->ipv6_orport != r2->ipv6_orport ||
|
2005-10-12 15:49:13 +02:00
|
|
|
r1->dir_port != r2->dir_port ||
|
2007-12-23 02:18:14 +01:00
|
|
|
r1->purpose != r2->purpose ||
|
2018-08-23 20:05:42 +02:00
|
|
|
r1->onion_pkey_len != r2->onion_pkey_len ||
|
|
|
|
!tor_memeq(r1->onion_pkey, r2->onion_pkey, r1->onion_pkey_len) ||
|
2012-09-15 12:52:13 +02:00
|
|
|
!crypto_pk_eq_keys(r1->identity_pkey, r2->identity_pkey) ||
|
2005-10-12 15:49:13 +02:00
|
|
|
strcasecmp(r1->platform, r2->platform) ||
|
2005-10-18 23:19:02 +02:00
|
|
|
(r1->contact_info && !r2->contact_info) || /* contact_info is optional */
|
|
|
|
(!r1->contact_info && r2->contact_info) ||
|
2005-12-14 21:40:40 +01:00
|
|
|
(r1->contact_info && r2->contact_info &&
|
|
|
|
strcasecmp(r1->contact_info, r2->contact_info)) ||
|
2005-10-12 15:49:13 +02:00
|
|
|
r1->is_hibernating != r2->is_hibernating ||
|
2017-02-13 19:53:30 +01:00
|
|
|
! addr_policies_eq(r1->exit_policy, r2->exit_policy) ||
|
2015-05-26 01:09:33 +02:00
|
|
|
(r1->supports_tunnelled_dir_requests !=
|
|
|
|
r2->supports_tunnelled_dir_requests))
|
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
|
|
|
|
2016-05-17 19:08:34 +02:00
|
|
|
/** Check whether <b>sd</b> describes a router descriptor compatible with the
|
|
|
|
* extrainfo document <b>ei</b>.
|
|
|
|
*
|
2016-05-17 19:24:01 +02:00
|
|
|
* <b>identity_pkey</b> (which must also be provided) is RSA1024 identity key
|
|
|
|
* for the router. We use it to check the signature of the extrainfo document,
|
|
|
|
* if it has not already been checked.
|
2016-05-17 19:08:34 +02:00
|
|
|
*
|
|
|
|
* If no router is compatible with <b>ei</b>, <b>ei</b> should be
|
2007-05-29 19:31:13 +02:00
|
|
|
* 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).
|
2016-05-17 19:08:34 +02:00
|
|
|
*
|
|
|
|
* Set the extrainfo_is_bogus field in <b>sd</b> if the digests matched
|
|
|
|
* but the extrainfo was nonetheless incompatible.
|
2007-10-21 02:08:35 +02:00
|
|
|
**/
|
2007-04-16 19:55:08 +02:00
|
|
|
int
|
2016-05-17 19:24:01 +02:00
|
|
|
routerinfo_incompatible_with_extrainfo(const crypto_pk_t *identity_pkey,
|
2010-09-29 06:38:32 +02:00
|
|
|
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
|
|
|
{
|
2014-10-24 15:19:49 +02:00
|
|
|
int digest_matches, digest256_matches, r=1;
|
2016-05-17 19:24:01 +02:00
|
|
|
tor_assert(identity_pkey);
|
|
|
|
tor_assert(sd);
|
2007-04-16 19:55:08 +02:00
|
|
|
tor_assert(ei);
|
|
|
|
|
2007-05-24 19:12:54 +02:00
|
|
|
if (ei->bad_sig) {
|
|
|
|
if (msg) *msg = "Extrainfo signature was bad, or signed with wrong key.";
|
2007-04-16 23:37:21 +02:00
|
|
|
return 1;
|
2007-05-24 19:12:54 +02:00
|
|
|
}
|
2007-04-16 23:37:21 +02:00
|
|
|
|
2011-05-10 22:23:43 +02:00
|
|
|
digest_matches = tor_memeq(ei->cache_info.signed_descriptor_digest,
|
2007-10-21 06:41:00 +02:00
|
|
|
sd->extra_info_digest, DIGEST_LEN);
|
2014-10-24 15:19:49 +02:00
|
|
|
/* Set digest256_matches to 1 if the digest is correct, or if no
|
|
|
|
* digest256 was in the ri. */
|
|
|
|
digest256_matches = tor_memeq(ei->digest256,
|
2016-05-17 18:57:03 +02:00
|
|
|
sd->extra_info_digest256, DIGEST256_LEN);
|
2015-06-01 14:59:14 +02:00
|
|
|
digest256_matches |=
|
2016-05-17 18:57:03 +02:00
|
|
|
tor_mem_is_zero(sd->extra_info_digest256, DIGEST256_LEN);
|
2007-10-21 06:41:00 +02:00
|
|
|
|
|
|
|
/* 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. */
|
2016-05-17 19:24:01 +02:00
|
|
|
if (tor_memneq(sd->identity_digest,
|
2011-05-16 02:06:36 +02:00
|
|
|
ei->cache_info.identity_digest,
|
|
|
|
DIGEST_LEN)) {
|
2007-05-20 20:03:26 +02:00
|
|
|
if (msg) *msg = "Extrainfo nickname or identity did not match routerinfo";
|
2007-10-21 06:41:00 +02:00
|
|
|
goto err; /* different servers */
|
2007-05-20 20:03:26 +02:00
|
|
|
}
|
2007-04-16 19:55:08 +02:00
|
|
|
|
2016-05-18 02:08:03 +02:00
|
|
|
if (! tor_cert_opt_eq(sd->signing_key_cert,
|
|
|
|
ei->cache_info.signing_key_cert)) {
|
2014-10-24 15:19:49 +02:00
|
|
|
if (msg) *msg = "Extrainfo signing key cert didn't match routerinfo";
|
|
|
|
goto err; /* different servers */
|
|
|
|
}
|
|
|
|
|
2007-04-16 19:55:08 +02:00
|
|
|
if (ei->pending_sig) {
|
|
|
|
char signed_digest[128];
|
2016-05-17 19:24:01 +02:00
|
|
|
if (crypto_pk_public_checksig(identity_pkey,
|
2011-01-13 20:36:41 +01:00
|
|
|
signed_digest, sizeof(signed_digest),
|
2007-05-15 00:51:05 +02:00
|
|
|
ei->pending_sig, ei->pending_sig_len) != DIGEST_LEN ||
|
2011-05-10 22:58:38 +02:00
|
|
|
tor_memneq(signed_digest, ei->cache_info.signed_descriptor_digest,
|
2007-04-16 23:37:21 +02:00
|
|
|
DIGEST_LEN)) {
|
|
|
|
ei->bad_sig = 1;
|
|
|
|
tor_free(ei->pending_sig);
|
2007-05-20 20:03:26 +02:00
|
|
|
if (msg) *msg = "Extrainfo signature bad, or signed with wrong key";
|
2007-10-21 06:41:00 +02:00
|
|
|
goto err; /* Bad signature, or no match. */
|
2007-04-16 23:37:21 +02:00
|
|
|
}
|
2007-04-16 19:55:08 +02:00
|
|
|
|
2016-05-17 19:24:01 +02:00
|
|
|
ei->cache_info.send_unencrypted = sd->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
|
|
|
}
|
|
|
|
|
2015-09-24 23:45:29 +02:00
|
|
|
if (!digest256_matches && !digest_matches) {
|
|
|
|
if (msg) *msg = "Neither digest256 or digest matched "
|
|
|
|
"digest from routerdesc";
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2014-10-24 15:19:49 +02:00
|
|
|
if (!digest256_matches) {
|
|
|
|
if (msg) *msg = "Extrainfo digest did not match digest256 from routerdesc";
|
|
|
|
goto err; /* Digest doesn't match declared value. */
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-07-07 04:58:47 +02:00
|
|
|
/* Does ri have a valid ntor onion key?
|
|
|
|
* Valid ntor onion keys exist and have at least one non-zero byte. */
|
|
|
|
int
|
|
|
|
routerinfo_has_curve25519_onion_key(const routerinfo_t *ri)
|
|
|
|
{
|
|
|
|
if (!ri) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ri->onion_curve25519_pkey) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tor_mem_is_zero((const char*)ri->onion_curve25519_pkey->public_key,
|
|
|
|
CURVE25519_PUBKEY_LEN)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-10-27 06:53:49 +02:00
|
|
|
/* Is rs running a tor version known to support EXTEND2 cells?
|
2016-09-22 16:21:13 +02:00
|
|
|
* If allow_unknown_versions is true, return true if we can't tell
|
2016-10-27 06:53:49 +02:00
|
|
|
* (from a versions line or a protocols line) whether it supports extend2
|
|
|
|
* cells.
|
2016-07-07 04:58:47 +02:00
|
|
|
* Otherwise, return false if the version is unknown. */
|
|
|
|
int
|
2016-10-27 06:53:49 +02:00
|
|
|
routerstatus_version_supports_extend2_cells(const routerstatus_t *rs,
|
|
|
|
int allow_unknown_versions)
|
2016-07-07 04:58:47 +02:00
|
|
|
{
|
|
|
|
if (!rs) {
|
|
|
|
return allow_unknown_versions;
|
|
|
|
}
|
|
|
|
|
2018-01-24 19:26:50 +01:00
|
|
|
if (!rs->pv.protocols_known) {
|
2016-07-07 04:58:47 +02:00
|
|
|
return allow_unknown_versions;
|
|
|
|
}
|
|
|
|
|
2018-01-24 19:26:50 +01:00
|
|
|
return rs->pv.supports_extend2_cells;
|
2016-07-07 04:58:47 +02:00
|
|
|
}
|
|
|
|
|
2007-01-22 08:51:06 +01:00
|
|
|
/** Assert that the internal representation of <b>rl</b> is
|
|
|
|
* self-consistent. */
|
2007-04-30 07:36:41 +02:00
|
|
|
void
|
2010-09-29 06:38:32 +02:00
|
|
|
routerlist_assert_ok(const routerlist_t *rl)
|
2005-10-18 22:13:09 +02:00
|
|
|
{
|
2005-10-27 02:34:39 +02:00
|
|
|
routerinfo_t *r2;
|
2005-11-05 21:15:27 +01:00
|
|
|
signed_descriptor_t *sd2;
|
2007-04-30 07:36:41 +02:00
|
|
|
if (!rl)
|
2005-10-18 22:13:09 +02:00
|
|
|
return;
|
2012-07-17 15:33:38 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(rl->routers, routerinfo_t *, r) {
|
2007-06-12 00:19:40 +02:00
|
|
|
r2 = rimap_get(rl->identity_map, r->cache_info.identity_digest);
|
2005-10-27 02:34:39 +02:00
|
|
|
tor_assert(r == r2);
|
2007-06-12 00:19:40 +02:00
|
|
|
sd2 = sdmap_get(rl->desc_digest_map,
|
|
|
|
r->cache_info.signed_descriptor_digest);
|
2005-11-05 21:15:27 +01:00
|
|
|
tor_assert(&(r->cache_info) == sd2);
|
2007-11-07 18:11:23 +01:00
|
|
|
tor_assert(r->cache_info.routerlist_index == r_sl_idx);
|
2008-12-18 17:11:24 +01:00
|
|
|
/* XXXX
|
2007-06-12 02:15:55 +02:00
|
|
|
*
|
|
|
|
* Hoo boy. We need to fix this one, and the fix is a bit tricky, so
|
|
|
|
* commenting this out is just a band-aid.
|
|
|
|
*
|
|
|
|
* The problem is that, although well-behaved router descriptors
|
|
|
|
* should never have the same value for their extra_info_digest, it's
|
|
|
|
* possible for ill-behaved routers to claim whatever they like there.
|
|
|
|
*
|
|
|
|
* The real answer is to trash desc_by_eid_map and instead have
|
|
|
|
* something that indicates for a given extra-info digest we want,
|
|
|
|
* what its download status is. We'll do that as a part of routerlist
|
|
|
|
* refactoring once consensus directories are in. For now,
|
|
|
|
* this rep violation is probably harmless: an adversary can make us
|
|
|
|
* reset our retry count for an extrainfo, but that's not the end
|
2008-02-15 20:20:53 +01:00
|
|
|
* of the world. Changing the representation in 0.2.0.x would just
|
|
|
|
* destabilize the codebase.
|
2007-05-19 20:12:31 +02:00
|
|
|
if (!tor_digest_is_zero(r->cache_info.extra_info_digest)) {
|
|
|
|
signed_descriptor_t *sd3 =
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_get(rl->desc_by_eid_map, r->cache_info.extra_info_digest);
|
2007-05-19 20:12:31 +02:00
|
|
|
tor_assert(sd3 == &(r->cache_info));
|
|
|
|
}
|
2008-06-16 20:35:21 +02:00
|
|
|
*/
|
2012-07-17 15:33:38 +02:00
|
|
|
} SMARTLIST_FOREACH_END(r);
|
|
|
|
SMARTLIST_FOREACH_BEGIN(rl->old_routers, signed_descriptor_t *, sd) {
|
2007-06-12 00:19:40 +02:00
|
|
|
r2 = rimap_get(rl->identity_map, sd->identity_digest);
|
2014-09-11 05:46:20 +02:00
|
|
|
tor_assert(!r2 || sd != &(r2->cache_info));
|
2007-06-12 00:19:40 +02:00
|
|
|
sd2 = sdmap_get(rl->desc_digest_map, sd->signed_descriptor_digest);
|
2005-11-05 21:15:27 +01:00
|
|
|
tor_assert(sd == sd2);
|
2007-11-07 18:11:23 +01:00
|
|
|
tor_assert(sd->routerlist_index == sd_sl_idx);
|
2008-12-18 17:11:24 +01:00
|
|
|
/* XXXX see above.
|
2007-05-19 20:12:31 +02:00
|
|
|
if (!tor_digest_is_zero(sd->extra_info_digest)) {
|
|
|
|
signed_descriptor_t *sd3 =
|
2007-06-12 00:19:40 +02:00
|
|
|
sdmap_get(rl->desc_by_eid_map, sd->extra_info_digest);
|
2007-05-19 20:12:31 +02:00
|
|
|
tor_assert(sd3 == sd);
|
|
|
|
}
|
2008-06-16 20:35:21 +02:00
|
|
|
*/
|
2012-07-17 15:33:38 +02:00
|
|
|
} SMARTLIST_FOREACH_END(sd);
|
2007-06-12 00:19:40 +02:00
|
|
|
|
2008-02-06 06:31:21 +01:00
|
|
|
RIMAP_FOREACH(rl->identity_map, d, r) {
|
2011-05-10 22:23:43 +02:00
|
|
|
tor_assert(tor_memeq(r->cache_info.identity_digest, d, DIGEST_LEN));
|
2008-02-06 06:31:21 +01:00
|
|
|
} DIGESTMAP_FOREACH_END;
|
|
|
|
SDMAP_FOREACH(rl->desc_digest_map, d, sd) {
|
2011-05-10 22:23:43 +02:00
|
|
|
tor_assert(tor_memeq(sd->signed_descriptor_digest, d, DIGEST_LEN));
|
2008-02-06 06:31:21 +01:00
|
|
|
} DIGESTMAP_FOREACH_END;
|
|
|
|
SDMAP_FOREACH(rl->desc_by_eid_map, d, sd) {
|
2007-06-02 16:24:23 +02:00
|
|
|
tor_assert(!tor_digest_is_zero(d));
|
|
|
|
tor_assert(sd);
|
2011-05-10 22:23:43 +02:00
|
|
|
tor_assert(tor_memeq(sd->extra_info_digest, d, DIGEST_LEN));
|
2008-02-06 06:31:21 +01:00
|
|
|
} DIGESTMAP_FOREACH_END;
|
|
|
|
EIMAP_FOREACH(rl->extra_info_map, d, ei) {
|
2007-05-19 20:12:31 +02:00
|
|
|
signed_descriptor_t *sd;
|
2011-05-10 22:23:43 +02:00
|
|
|
tor_assert(tor_memeq(ei->cache_info.signed_descriptor_digest,
|
2007-05-19 20:12:31 +02:00
|
|
|
d, DIGEST_LEN));
|
2007-06-12 00:19:40 +02:00
|
|
|
sd = sdmap_get(rl->desc_by_eid_map,
|
|
|
|
ei->cache_info.signed_descriptor_digest);
|
2008-12-18 17:11:24 +01:00
|
|
|
// tor_assert(sd); // XXXX see above
|
2007-06-12 02:15:55 +02:00
|
|
|
if (sd) {
|
2011-05-10 22:23:43 +02:00
|
|
|
tor_assert(tor_memeq(ei->cache_info.signed_descriptor_digest,
|
2007-06-12 02:15:55 +02:00
|
|
|
sd->extra_info_digest, DIGEST_LEN));
|
|
|
|
}
|
2008-02-06 06:31:21 +01:00
|
|
|
} DIGESTMAP_FOREACH_END;
|
2005-10-18 22:13:09 +02:00
|
|
|
}
|
|
|
|
|
2006-03-11 18:52:55 +01:00
|
|
|
/** Allocate and return a new string representing the contact info
|
|
|
|
* and platform string for <b>router</b>,
|
|
|
|
* surrounded by quotes and using standard C escapes.
|
|
|
|
*
|
|
|
|
* THIS FUNCTION IS NOT REENTRANT. Don't call it from outside the main
|
|
|
|
* thread. Also, each call invalidates the last-returned value, so don't
|
|
|
|
* try log_warn(LD_GENERAL, "%s %s", esc_router_info(a), esc_router_info(b));
|
2007-07-22 08:32:28 +02:00
|
|
|
*
|
|
|
|
* If <b>router</b> is NULL, it just frees its internal memory and returns.
|
2006-03-11 18:52:55 +01:00
|
|
|
*/
|
2006-03-11 03:21:30 +01:00
|
|
|
const char *
|
2010-09-29 06:38:32 +02:00
|
|
|
esc_router_info(const routerinfo_t *router)
|
2006-03-11 03:21:30 +01:00
|
|
|
{
|
2007-07-22 08:32:28 +02:00
|
|
|
static char *info=NULL;
|
2006-03-11 03:21:30 +01:00
|
|
|
char *esc_contact, *esc_platform;
|
2009-12-12 08:07:59 +01:00
|
|
|
tor_free(info);
|
|
|
|
|
2007-07-22 08:32:28 +02:00
|
|
|
if (!router)
|
|
|
|
return NULL; /* we're exiting; just free the memory we use */
|
2006-03-11 03:21:30 +01:00
|
|
|
|
|
|
|
esc_contact = esc_for_log(router->contact_info);
|
|
|
|
esc_platform = esc_for_log(router->platform);
|
|
|
|
|
2012-01-11 20:02:59 +01:00
|
|
|
tor_asprintf(&info, "Contact %s, Platform %s", esc_contact, esc_platform);
|
2006-03-11 03:21:30 +01:00
|
|
|
tor_free(esc_contact);
|
|
|
|
tor_free(esc_platform);
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2007-10-12 23:33:37 +02:00
|
|
|
/** Helper for sorting: compare two routerinfos by their identity
|
|
|
|
* digest. */
|
|
|
|
static int
|
2012-10-12 18:22:13 +02:00
|
|
|
compare_routerinfo_by_id_digest_(const void **a, const void **b)
|
2007-10-12 23:33:37 +02:00
|
|
|
{
|
|
|
|
routerinfo_t *first = *(routerinfo_t **)a, *second = *(routerinfo_t **)b;
|
2011-05-10 22:58:38 +02:00
|
|
|
return fast_memcmp(first->cache_info.identity_digest,
|
2007-10-12 23:33:37 +02:00
|
|
|
second->cache_info.identity_digest,
|
|
|
|
DIGEST_LEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Sort a list of routerinfo_t in ascending order of identity digest. */
|
|
|
|
void
|
|
|
|
routers_sort_by_identity(smartlist_t *routers)
|
|
|
|
{
|
2012-10-12 18:22:13 +02:00
|
|
|
smartlist_sort(routers, compare_routerinfo_by_id_digest_);
|
2007-10-12 23:33:37 +02:00
|
|
|
}
|
|
|
|
|
2012-10-31 16:38:07 +01:00
|
|
|
/** Called when we change a node set, or when we reload the geoip IPv4 list:
|
2009-10-19 06:30:52 +02:00
|
|
|
* recompute all country info in all configuration node sets and in the
|
|
|
|
* routerlist. */
|
2009-02-16 16:15:06 +01:00
|
|
|
void
|
|
|
|
refresh_all_country_info(void)
|
|
|
|
{
|
2011-06-14 19:01:38 +02:00
|
|
|
const or_options_t *options = get_options();
|
2009-02-16 16:15:06 +01:00
|
|
|
|
|
|
|
if (options->EntryNodes)
|
|
|
|
routerset_refresh_countries(options->EntryNodes);
|
|
|
|
if (options->ExitNodes)
|
|
|
|
routerset_refresh_countries(options->ExitNodes);
|
|
|
|
if (options->ExcludeNodes)
|
|
|
|
routerset_refresh_countries(options->ExcludeNodes);
|
|
|
|
if (options->ExcludeExitNodes)
|
|
|
|
routerset_refresh_countries(options->ExcludeExitNodes);
|
2012-10-12 18:22:13 +02:00
|
|
|
if (options->ExcludeExitNodesUnion_)
|
|
|
|
routerset_refresh_countries(options->ExcludeExitNodesUnion_);
|
2009-02-16 16:15:06 +01:00
|
|
|
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
nodelist_refresh_countries();
|
2009-02-16 16:15:06 +01:00
|
|
|
}
|