2007-12-12 22:09:01 +01:00
|
|
|
/* Copyright (c) 2001-2004, Roger Dingledine.
|
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2010-02-27 23:13:37 +01:00
|
|
|
* Copyright (c) 2007-2010, The Tor Project, Inc. */
|
2003-09-27 23:30:10 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
2007-06-13 20:16:05 +02:00
|
|
|
#define DIRSERV_PRIVATE
|
2003-09-27 23:30:10 +02:00
|
|
|
#include "or.h"
|
2010-07-22 00:46:18 +02:00
|
|
|
#include "buffers.h"
|
2010-07-22 10:22:51 +02:00
|
|
|
#include "config.h"
|
2010-07-22 10:32:52 +02:00
|
|
|
#include "connection.h"
|
2010-07-22 10:50:34 +02:00
|
|
|
#include "connection_or.h"
|
2010-07-22 11:35:09 +02:00
|
|
|
#include "control.h"
|
2010-07-22 11:54:50 +02:00
|
|
|
#include "directory.h"
|
2010-07-22 12:09:49 +02:00
|
|
|
#include "dirserv.h"
|
2010-07-22 12:19:28 +02:00
|
|
|
#include "dirvote.h"
|
2010-07-21 16:17:10 +02:00
|
|
|
#include "router.h"
|
2010-07-21 17:08:11 +02:00
|
|
|
#include "routerlist.h"
|
2003-09-27 23:30:10 +02:00
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/**
|
|
|
|
* \file dirserv.c
|
2005-06-11 07:31:17 +02:00
|
|
|
* \brief Directory server core implementation. Manages directory
|
|
|
|
* contents and generates directories.
|
2007-07-26 00:56:40 +02:00
|
|
|
*/
|
2004-05-05 02:30:43 +02:00
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** How far in the future do we allow a router to get? (seconds) */
|
2006-03-12 23:48:18 +01:00
|
|
|
#define ROUTER_ALLOW_SKEW (60*60*12)
|
2004-09-23 21:58:44 +02:00
|
|
|
/** How many seconds do we wait before regenerating the directory? */
|
2005-09-08 22:47:11 +02:00
|
|
|
#define DIR_REGEN_SLACK_TIME 30
|
2006-01-08 22:26:33 +01:00
|
|
|
/** If we're a cache, keep this many networkstatuses around from non-trusted
|
|
|
|
* directory authorities. */
|
|
|
|
#define MAX_UNTRUSTED_NETWORKSTATUSES 16
|
2003-10-22 18:41:35 +02:00
|
|
|
|
2007-02-25 04:43:00 +01:00
|
|
|
/** If a v1 directory is older than this, discard it. */
|
|
|
|
#define MAX_V1_DIRECTORY_AGE (30*24*60*60)
|
|
|
|
/** If a v1 running-routers is older than this, discard it. */
|
|
|
|
#define MAX_V1_RR_AGE (7*24*60*60)
|
|
|
|
|
2007-11-08 19:15:49 +01:00
|
|
|
extern time_t time_of_process_start; /* from main.c */
|
|
|
|
|
2008-12-23 18:56:31 +01:00
|
|
|
/** Do we need to regenerate the v1 directory when someone asks for it? */
|
2008-02-22 20:09:45 +01:00
|
|
|
static time_t the_directory_is_dirty = 1;
|
2008-12-23 18:56:31 +01:00
|
|
|
/** Do we need to regenerate the v1 runningrouters document when somebody
|
|
|
|
* asks for it? */
|
2008-02-22 20:09:45 +01:00
|
|
|
static time_t runningrouters_is_dirty = 1;
|
2008-12-23 18:56:31 +01:00
|
|
|
/** Do we need to regenerate our v2 networkstatus document when somebody asks
|
|
|
|
* for it? */
|
2008-02-22 20:09:45 +01:00
|
|
|
static time_t the_v2_networkstatus_is_dirty = 1;
|
2003-09-27 23:30:10 +02:00
|
|
|
|
2007-07-28 01:18:58 +02:00
|
|
|
/** Most recently generated encoded signed v1 directory. (v1 auth dirservers
|
|
|
|
* only.) */
|
|
|
|
static cached_dir_t *the_directory = NULL;
|
|
|
|
|
|
|
|
/** For authoritative directories: the current (v1) network status. */
|
2009-10-13 23:06:01 +02:00
|
|
|
static cached_dir_t the_runningrouters;
|
2007-07-28 01:18:58 +02:00
|
|
|
|
2005-06-21 03:00:42 +02:00
|
|
|
static void directory_remove_invalid(void);
|
2006-06-18 23:30:03 +02:00
|
|
|
static cached_dir_t *dirserv_regenerate_directory(void);
|
2005-08-25 22:33:17 +02:00
|
|
|
static char *format_versions_list(config_line_t *ln);
|
2006-10-23 05:48:42 +02:00
|
|
|
struct authdir_config_t;
|
2007-05-24 19:12:54 +02:00
|
|
|
static int add_fingerprint_to_dir(const char *nickname, const char *fp,
|
|
|
|
struct authdir_config_t *list);
|
2006-10-23 05:48:42 +02:00
|
|
|
static uint32_t dirserv_router_get_status(const routerinfo_t *router,
|
|
|
|
const char **msg);
|
|
|
|
static uint32_t
|
2005-12-06 07:55:43 +01:00
|
|
|
dirserv_get_status_impl(const char *fp, const char *nickname,
|
|
|
|
const char *address,
|
|
|
|
uint32_t addr, uint16_t or_port,
|
|
|
|
const char *platform, const char *contact,
|
|
|
|
const char **msg, int should_log);
|
2006-06-18 09:38:55 +02:00
|
|
|
static void clear_cached_dir(cached_dir_t *d);
|
2008-01-10 18:48:40 +01:00
|
|
|
static signed_descriptor_t *get_signed_descriptor_by_fp(const char *fp,
|
|
|
|
int extrainfo,
|
|
|
|
time_t publish_cutoff);
|
2007-05-15 09:13:56 +02:00
|
|
|
static int dirserv_add_extrainfo(extrainfo_t *ei, const char **msg);
|
|
|
|
|
2009-07-31 06:33:53 +02:00
|
|
|
/************** Measured Bandwidth parsing code ******/
|
|
|
|
#define MAX_MEASUREMENT_AGE (3*24*60*60) /* 3 days */
|
|
|
|
|
2003-09-29 09:50:08 +02:00
|
|
|
/************** Fingerprint handling code ************/
|
|
|
|
|
2006-10-23 05:48:42 +02:00
|
|
|
#define FP_NAMED 1 /**< Listed in fingerprint file. */
|
|
|
|
#define FP_INVALID 2 /**< Believed invalid. */
|
|
|
|
#define FP_REJECT 4 /**< We will not publish this router. */
|
2007-12-10 17:49:54 +01:00
|
|
|
#define FP_BADDIR 8 /**< We'll tell clients to avoid using this as a dir. */
|
2010-01-12 20:05:12 +01:00
|
|
|
#define FP_BADEXIT 16 /**< We'll tell clients not to use this as an exit. */
|
2007-12-10 17:49:54 +01:00
|
|
|
#define FP_UNNAMED 32 /**< Another router has this name in fingerprint file. */
|
2006-10-23 05:48:42 +02:00
|
|
|
|
2007-02-16 21:39:37 +01:00
|
|
|
/** Encapsulate a nickname and an FP_* status; target of status_by_digest
|
|
|
|
* map. */
|
2006-10-23 05:48:42 +02:00
|
|
|
typedef struct router_status_t {
|
|
|
|
char nickname[MAX_NICKNAME_LEN+1];
|
|
|
|
uint32_t status;
|
|
|
|
} router_status_t;
|
2003-09-27 23:30:10 +02:00
|
|
|
|
2004-05-10 06:34:48 +02:00
|
|
|
/** List of nickname-\>identity fingerprint mappings for all the routers
|
2007-02-16 21:39:37 +01:00
|
|
|
* that we name. Used to prevent router impersonation. */
|
2006-10-23 05:48:42 +02:00
|
|
|
typedef struct authdir_config_t {
|
2007-02-24 08:50:38 +01:00
|
|
|
strmap_t *fp_by_name; /**< Map from lc nickname to fingerprint. */
|
|
|
|
digestmap_t *status_by_digest; /**< Map from digest to router_status_t. */
|
2006-10-23 05:48:42 +02:00
|
|
|
} authdir_config_t;
|
|
|
|
|
2007-02-24 08:50:38 +01:00
|
|
|
/** Should be static; exposed for testing. */
|
2007-05-24 19:12:54 +02:00
|
|
|
static authdir_config_t *fingerprint_list = NULL;
|
2006-10-23 05:48:42 +02:00
|
|
|
|
2007-02-16 21:39:37 +01:00
|
|
|
/** Allocate and return a new, empty, authdir_config_t. */
|
2006-10-23 05:48:42 +02:00
|
|
|
static authdir_config_t *
|
|
|
|
authdir_config_new(void)
|
|
|
|
{
|
|
|
|
authdir_config_t *list = tor_malloc_zero(sizeof(authdir_config_t));
|
|
|
|
list->fp_by_name = strmap_new();
|
|
|
|
list->status_by_digest = digestmap_new();
|
|
|
|
return list;
|
|
|
|
}
|
2003-09-27 23:30:10 +02:00
|
|
|
|
2010-01-12 20:05:12 +01:00
|
|
|
/** Add the fingerprint <b>fp</b> for <b>nickname</b> to
|
2005-06-21 03:00:42 +02:00
|
|
|
* the smartlist of fingerprint_entry_t's <b>list</b>. Return 0 if it's
|
|
|
|
* new, or 1 if we replaced the old value.
|
2004-05-05 02:30:43 +02:00
|
|
|
*/
|
2007-04-11 15:18:25 +02:00
|
|
|
/* static */ int
|
2006-10-23 05:48:42 +02:00
|
|
|
add_fingerprint_to_dir(const char *nickname, const char *fp,
|
|
|
|
authdir_config_t *list)
|
2003-09-30 21:27:54 +02:00
|
|
|
{
|
2005-08-24 00:00:35 +02:00
|
|
|
char *fingerprint;
|
2006-10-23 05:48:42 +02:00
|
|
|
char d[DIGEST_LEN];
|
|
|
|
router_status_t *status;
|
2005-06-21 03:00:42 +02:00
|
|
|
tor_assert(nickname);
|
|
|
|
tor_assert(fp);
|
|
|
|
tor_assert(list);
|
2004-05-18 19:41:40 +02:00
|
|
|
|
2006-11-20 17:26:52 +01:00
|
|
|
fingerprint = tor_strdup(fp);
|
2005-08-24 00:00:35 +02:00
|
|
|
tor_strstrip(fingerprint, " ");
|
2006-10-23 05:48:42 +02:00
|
|
|
if (base16_decode(d, DIGEST_LEN, fingerprint, strlen(fingerprint))) {
|
|
|
|
log_warn(LD_DIRSERV, "Couldn't decode fingerprint \"%s\"",
|
|
|
|
escaped(fp));
|
2006-11-20 17:22:46 +01:00
|
|
|
tor_free(fingerprint);
|
2006-10-23 05:48:42 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2005-08-24 00:00:35 +02:00
|
|
|
|
2006-09-29 06:51:28 +02:00
|
|
|
if (!strcasecmp(nickname, UNNAMED_ROUTER_NICKNAME)) {
|
|
|
|
log_warn(LD_DIRSERV, "Tried to add a mapping for reserved nickname %s",
|
|
|
|
UNNAMED_ROUTER_NICKNAME);
|
2006-11-20 17:22:46 +01:00
|
|
|
tor_free(fingerprint);
|
2006-09-29 06:51:28 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-10-23 05:48:42 +02:00
|
|
|
status = digestmap_get(list->status_by_digest, d);
|
|
|
|
if (!status) {
|
|
|
|
status = tor_malloc_zero(sizeof(router_status_t));
|
|
|
|
digestmap_set(list->status_by_digest, d, status);
|
|
|
|
}
|
|
|
|
|
2005-10-05 00:23:31 +02:00
|
|
|
if (nickname[0] != '!') {
|
2006-10-23 05:48:42 +02:00
|
|
|
char *old_fp = strmap_get_lc(list->fp_by_name, nickname);
|
2006-10-24 23:38:31 +02:00
|
|
|
if (old_fp && !strcasecmp(fingerprint, old_fp)) {
|
|
|
|
tor_free(fingerprint);
|
|
|
|
} else {
|
|
|
|
tor_free(old_fp);
|
|
|
|
strmap_set_lc(list->fp_by_name, nickname, fingerprint);
|
2003-09-30 21:27:54 +02:00
|
|
|
}
|
2006-10-23 05:48:42 +02:00
|
|
|
status->status |= FP_NAMED;
|
|
|
|
strlcpy(status->nickname, nickname, sizeof(status->nickname));
|
|
|
|
} else {
|
2006-11-20 17:36:44 +01:00
|
|
|
tor_free(fingerprint);
|
2006-10-23 05:48:42 +02:00
|
|
|
if (!strcasecmp(nickname, "!reject")) {
|
|
|
|
status->status |= FP_REJECT;
|
|
|
|
} else if (!strcasecmp(nickname, "!invalid")) {
|
|
|
|
status->status |= FP_INVALID;
|
2007-12-10 17:49:54 +01:00
|
|
|
} else if (!strcasecmp(nickname, "!baddir")) {
|
|
|
|
status->status |= FP_BADDIR;
|
2006-10-23 05:48:42 +02:00
|
|
|
} else if (!strcasecmp(nickname, "!badexit")) {
|
|
|
|
status->status |= FP_BADEXIT;
|
|
|
|
}
|
2003-09-30 21:27:54 +02:00
|
|
|
}
|
2005-06-21 03:00:42 +02:00
|
|
|
return 0;
|
2003-09-30 21:27:54 +02:00
|
|
|
}
|
|
|
|
|
2005-06-21 03:00:42 +02:00
|
|
|
/** Add the nickname and fingerprint for this OR to the
|
|
|
|
* global list of recognized identity key fingerprints. */
|
2003-09-30 21:27:54 +02:00
|
|
|
int
|
|
|
|
dirserv_add_own_fingerprint(const char *nickname, crypto_pk_env_t *pk)
|
|
|
|
{
|
|
|
|
char fp[FINGERPRINT_LEN+1];
|
2004-10-06 15:31:48 +02:00
|
|
|
if (crypto_pk_get_fingerprint(pk, fp, 0)<0) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_err(LD_BUG, "Error computing fingerprint");
|
2003-09-30 21:27:54 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2005-06-21 03:00:42 +02:00
|
|
|
if (!fingerprint_list)
|
2006-10-23 05:48:42 +02:00
|
|
|
fingerprint_list = authdir_config_new();
|
2005-06-21 03:00:42 +02:00
|
|
|
add_fingerprint_to_dir(nickname, fp, fingerprint_list);
|
2003-09-30 21:27:54 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-09-14 06:53:23 +02:00
|
|
|
/** Load the nickname-\>fingerprint mappings stored in the approved-routers
|
|
|
|
* file. The file format is line-based, with each non-blank holding one
|
|
|
|
* nickname, some space, and a fingerprint for that nickname. On success,
|
|
|
|
* replace the current fingerprint list with the new list and return 0. On
|
2010-01-12 20:05:12 +01:00
|
|
|
* failure, leave the current fingerprint list untouched, and return -1. */
|
2003-12-14 07:03:46 +01:00
|
|
|
int
|
2006-09-14 07:00:02 +02:00
|
|
|
dirserv_load_fingerprint_file(void)
|
2003-09-27 23:30:10 +02:00
|
|
|
{
|
2007-10-17 18:55:44 +02:00
|
|
|
char *fname;
|
2004-11-06 06:18:11 +01:00
|
|
|
char *cf;
|
2003-09-29 09:50:08 +02:00
|
|
|
char *nickname, *fingerprint;
|
2006-10-23 05:48:42 +02:00
|
|
|
authdir_config_t *fingerprint_list_new;
|
2005-06-21 03:00:42 +02:00
|
|
|
int result;
|
2005-07-22 23:12:10 +02:00
|
|
|
config_line_t *front=NULL, *list;
|
2006-09-14 06:53:23 +02:00
|
|
|
or_options_t *options = get_options();
|
|
|
|
|
2007-10-17 18:55:44 +02:00
|
|
|
fname = get_datadir_fname("approved-routers");
|
2006-09-14 06:53:23 +02:00
|
|
|
log_info(LD_GENERAL,
|
|
|
|
"Reloading approved fingerprints from \"%s\"...", fname);
|
2003-09-29 09:50:08 +02:00
|
|
|
|
2007-06-09 07:28:11 +02:00
|
|
|
cf = read_file_to_str(fname, RFTS_IGNORE_MISSING, NULL);
|
2004-11-06 06:18:11 +01:00
|
|
|
if (!cf) {
|
2006-09-14 06:53:23 +02:00
|
|
|
if (options->NamingAuthoritativeDir) {
|
2006-03-18 01:22:23 +01:00
|
|
|
log_warn(LD_FS, "Cannot open fingerprint file '%s'. Failing.", fname);
|
2007-10-17 18:55:44 +02:00
|
|
|
tor_free(fname);
|
2006-03-18 01:22:23 +01:00
|
|
|
return -1;
|
|
|
|
} else {
|
2008-02-06 13:46:46 +01:00
|
|
|
log_info(LD_FS, "Cannot open fingerprint file '%s'. That's ok.", fname);
|
2007-10-17 18:55:44 +02:00
|
|
|
tor_free(fname);
|
2006-03-18 01:22:23 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2003-09-27 23:30:10 +02:00
|
|
|
}
|
2007-10-17 18:55:44 +02:00
|
|
|
tor_free(fname);
|
|
|
|
|
2004-11-06 06:18:11 +01:00
|
|
|
result = config_get_lines(cf, &front);
|
|
|
|
tor_free(cf);
|
|
|
|
if (result < 0) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_warn(LD_CONFIG, "Error reading from fingerprint file");
|
2004-11-06 06:18:11 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-10-23 05:48:42 +02:00
|
|
|
fingerprint_list_new = authdir_config_new();
|
2004-11-06 06:18:11 +01:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
for (list=front; list; list=list->next) {
|
2008-06-16 04:08:30 +02:00
|
|
|
char digest_tmp[DIGEST_LEN];
|
2004-11-06 06:18:11 +01:00
|
|
|
nickname = list->key; fingerprint = list->value;
|
2003-09-27 23:30:10 +02:00
|
|
|
if (strlen(nickname) > MAX_NICKNAME_LEN) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_notice(LD_CONFIG,
|
|
|
|
"Nickname '%s' too long in fingerprint file. Skipping.",
|
|
|
|
nickname);
|
2003-09-29 09:50:08 +02:00
|
|
|
continue;
|
2003-09-27 23:30:10 +02:00
|
|
|
}
|
2005-09-15 16:39:05 +02:00
|
|
|
if (!is_legal_nickname(nickname) &&
|
2005-09-15 23:10:58 +02:00
|
|
|
strcasecmp(nickname, "!reject") &&
|
2006-10-23 05:48:42 +02:00
|
|
|
strcasecmp(nickname, "!invalid") &&
|
|
|
|
strcasecmp(nickname, "!badexit")) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_notice(LD_CONFIG,
|
|
|
|
"Invalid nickname '%s' in fingerprint file. Skipping.",
|
|
|
|
nickname);
|
2005-09-15 16:39:05 +02:00
|
|
|
continue;
|
|
|
|
}
|
2008-06-16 04:08:30 +02:00
|
|
|
tor_strstrip(fingerprint, " "); /* remove spaces */
|
|
|
|
if (strlen(fingerprint) != HEX_DIGEST_LEN ||
|
|
|
|
base16_decode(digest_tmp, sizeof(digest_tmp),
|
|
|
|
fingerprint, HEX_DIGEST_LEN) < 0) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_notice(LD_CONFIG,
|
|
|
|
"Invalid fingerprint (nickname '%s', "
|
|
|
|
"fingerprint %s). Skipping.",
|
|
|
|
nickname, fingerprint);
|
2003-09-29 09:50:08 +02:00
|
|
|
continue;
|
2003-09-27 23:30:10 +02:00
|
|
|
}
|
2004-11-09 21:04:00 +01:00
|
|
|
if (0==strcasecmp(nickname, DEFAULT_CLIENT_NICKNAME)) {
|
2004-11-09 19:22:17 +01:00
|
|
|
/* If you approved an OR called "client", then clients who use
|
|
|
|
* the default nickname could all be rejected. That's no good. */
|
2006-02-13 10:37:53 +01:00
|
|
|
log_notice(LD_CONFIG,
|
2007-05-02 11:12:04 +02:00
|
|
|
"Authorizing nickname '%s' would break "
|
2006-10-23 05:48:42 +02:00
|
|
|
"many clients; skipping.",
|
|
|
|
DEFAULT_CLIENT_NICKNAME);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (0==strcasecmp(nickname, UNNAMED_ROUTER_NICKNAME)) {
|
|
|
|
/* If you approved an OR called "unnamed", then clients will be
|
2006-10-29 08:41:25 +01:00
|
|
|
* confused. */
|
2006-10-23 05:48:42 +02:00
|
|
|
log_notice(LD_CONFIG,
|
2007-05-02 11:12:04 +02:00
|
|
|
"Authorizing nickname '%s' is not allowed; skipping.",
|
2006-10-23 05:48:42 +02:00
|
|
|
UNNAMED_ROUTER_NICKNAME);
|
|
|
|
continue;
|
|
|
|
}
|
2005-12-14 21:40:40 +01:00
|
|
|
if (add_fingerprint_to_dir(nickname, fingerprint, fingerprint_list_new)
|
|
|
|
!= 0)
|
2006-02-13 10:37:53 +01:00
|
|
|
log_notice(LD_CONFIG, "Duplicate nickname '%s'.", nickname);
|
2003-09-27 23:30:10 +02:00
|
|
|
}
|
2004-11-06 06:18:11 +01:00
|
|
|
|
|
|
|
config_free_lines(front);
|
|
|
|
dirserv_free_fingerprint_list();
|
|
|
|
fingerprint_list = fingerprint_list_new;
|
|
|
|
/* Delete any routers whose fingerprints we no longer recognize */
|
2005-06-21 03:00:42 +02:00
|
|
|
directory_remove_invalid();
|
2004-11-06 06:18:11 +01:00
|
|
|
return 0;
|
2003-12-14 06:25:23 +01:00
|
|
|
}
|
2003-09-27 23:30:10 +02:00
|
|
|
|
2004-05-10 06:34:48 +02:00
|
|
|
/** Check whether <b>router</b> has a nickname/identity key combination that
|
2005-09-15 02:51:42 +02:00
|
|
|
* we recognize from the fingerprint list, or an IP we automatically act on
|
2005-09-15 08:15:31 +02:00
|
|
|
* according to our configuration. Return the appropriate router status.
|
|
|
|
*
|
|
|
|
* If the status is 'FP_REJECT' and <b>msg</b> is provided, set
|
2005-12-06 07:55:43 +01:00
|
|
|
* *<b>msg</b> to an explanation of why. */
|
2006-10-23 05:48:42 +02:00
|
|
|
static uint32_t
|
2005-09-15 02:51:42 +02:00
|
|
|
dirserv_router_get_status(const routerinfo_t *router, const char **msg)
|
2003-09-27 23:30:10 +02:00
|
|
|
{
|
2006-10-23 05:48:42 +02:00
|
|
|
char d[DIGEST_LEN];
|
2004-05-18 19:41:40 +02:00
|
|
|
|
2006-10-23 05:48:42 +02:00
|
|
|
if (crypto_pk_get_digest(router->identity_pkey, d)) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_warn(LD_BUG,"Error computing fingerprint");
|
2006-12-04 19:54:01 +01:00
|
|
|
if (msg)
|
|
|
|
*msg = "Bug: Error computing fingerprint";
|
2006-10-09 04:35:51 +02:00
|
|
|
return FP_REJECT;
|
2005-09-15 16:39:05 +02:00
|
|
|
}
|
|
|
|
|
2006-10-23 05:48:42 +02:00
|
|
|
return dirserv_get_status_impl(d, router->nickname,
|
2005-12-06 07:55:43 +01:00
|
|
|
router->address,
|
|
|
|
router->addr, router->or_port,
|
|
|
|
router->platform, router->contact_info,
|
|
|
|
msg, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return true if there is no point in downloading the router described by
|
|
|
|
* <b>rs</b> because this directory would reject it. */
|
|
|
|
int
|
|
|
|
dirserv_would_reject_router(routerstatus_t *rs)
|
|
|
|
{
|
2006-10-23 05:48:42 +02:00
|
|
|
uint32_t res;
|
2005-12-06 07:55:43 +01:00
|
|
|
|
2006-10-23 05:48:42 +02:00
|
|
|
res = dirserv_get_status_impl(rs->identity_digest, rs->nickname,
|
2005-12-06 07:55:43 +01:00
|
|
|
"", /* address is only used in logs */
|
|
|
|
rs->addr, rs->or_port,
|
|
|
|
NULL, NULL,
|
|
|
|
NULL, 0);
|
|
|
|
|
2006-10-23 05:48:42 +02:00
|
|
|
return (res & FP_REJECT) != 0;
|
2005-12-06 07:55:43 +01:00
|
|
|
}
|
|
|
|
|
2007-10-19 20:56:26 +02:00
|
|
|
/** Helper: Based only on the ID/Nickname combination,
|
|
|
|
* return FP_UNNAMED (unnamed), FP_NAMED (named), or 0 (neither).
|
|
|
|
*/
|
|
|
|
static uint32_t
|
|
|
|
dirserv_get_name_status(const char *id_digest, const char *nickname)
|
|
|
|
{
|
|
|
|
char fp[HEX_DIGEST_LEN+1];
|
|
|
|
char *fp_by_name;
|
|
|
|
|
|
|
|
base16_encode(fp, sizeof(fp), id_digest, DIGEST_LEN);
|
|
|
|
|
|
|
|
if ((fp_by_name =
|
|
|
|
strmap_get_lc(fingerprint_list->fp_by_name, nickname))) {
|
|
|
|
if (!strcasecmp(fp, fp_by_name)) {
|
|
|
|
return FP_NAMED;
|
|
|
|
} else {
|
|
|
|
return FP_UNNAMED; /* Wrong fingerprint. */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-12-06 07:55:43 +01:00
|
|
|
/** Helper: As dirserv_get_router_status, but takes the router fingerprint
|
|
|
|
* (hex, no spaces), nickname, address (used for logging only), IP address, OR
|
|
|
|
* port, platform (logging only) and contact info (logging only) as arguments.
|
|
|
|
*
|
|
|
|
* If should_log is false, do not log messages. (There's not much point in
|
|
|
|
* logging that we're rejecting servers we'll not download.)
|
|
|
|
*/
|
2006-10-23 05:48:42 +02:00
|
|
|
static uint32_t
|
|
|
|
dirserv_get_status_impl(const char *id_digest, const char *nickname,
|
2005-12-06 07:55:43 +01:00
|
|
|
const char *address,
|
|
|
|
uint32_t addr, uint16_t or_port,
|
|
|
|
const char *platform, const char *contact,
|
|
|
|
const char **msg, int should_log)
|
|
|
|
{
|
2005-12-15 22:39:38 +01:00
|
|
|
int reject_unlisted = get_options()->AuthDirRejectUnlisted;
|
2006-10-23 05:48:42 +02:00
|
|
|
uint32_t result = 0;
|
|
|
|
router_status_t *status_by_digest;
|
2007-10-19 20:56:26 +02:00
|
|
|
|
2005-12-06 07:55:43 +01:00
|
|
|
if (!fingerprint_list)
|
2006-10-23 05:48:42 +02:00
|
|
|
fingerprint_list = authdir_config_new();
|
|
|
|
|
2005-12-06 07:55:43 +01:00
|
|
|
if (should_log)
|
2006-10-24 23:38:31 +02:00
|
|
|
log_debug(LD_DIRSERV, "%d fingerprints, %d digests known.",
|
|
|
|
strmap_size(fingerprint_list->fp_by_name),
|
2006-10-23 05:48:42 +02:00
|
|
|
digestmap_size(fingerprint_list->status_by_digest));
|
2005-09-15 02:51:42 +02:00
|
|
|
|
2009-10-01 00:34:21 +02:00
|
|
|
/* Tor 0.1.2.x is pretty old, but there are a lot of them running still,
|
|
|
|
* and there aren't any critical relay-side vulnerabilities. Once more
|
|
|
|
* of them die off, we should raise this minimum to 0.2.0.x. */
|
|
|
|
if (platform && !tor_version_as_new_as(platform,"0.1.2.14")) {
|
2008-07-16 15:15:11 +02:00
|
|
|
if (msg)
|
|
|
|
*msg = "Tor version is far too old to work.";
|
|
|
|
return FP_REJECT;
|
|
|
|
}
|
|
|
|
|
2007-10-19 20:56:26 +02:00
|
|
|
result = dirserv_get_name_status(id_digest, nickname);
|
|
|
|
if (result & FP_NAMED) {
|
|
|
|
if (should_log)
|
|
|
|
log_debug(LD_DIRSERV,"Good fingerprint for '%s'",nickname);
|
|
|
|
}
|
|
|
|
if (result & FP_UNNAMED) {
|
|
|
|
if (should_log) {
|
|
|
|
char *esc_contact = esc_for_log(contact);
|
2007-10-28 21:01:22 +01:00
|
|
|
log_info(LD_DIRSERV,
|
2007-10-19 20:56:26 +02:00
|
|
|
"Mismatched fingerprint for '%s'. "
|
|
|
|
"ContactInfo '%s', platform '%s'.)",
|
|
|
|
nickname,
|
|
|
|
esc_contact,
|
|
|
|
platform ? escaped(platform) : "");
|
|
|
|
tor_free(esc_contact);
|
2005-09-15 16:39:05 +02:00
|
|
|
}
|
2007-10-19 20:56:26 +02:00
|
|
|
if (msg)
|
|
|
|
*msg = "Rejected: There is already a named server with this nickname "
|
|
|
|
"and a different fingerprint.";
|
2005-09-15 16:39:05 +02:00
|
|
|
}
|
2007-10-19 20:56:26 +02:00
|
|
|
|
2006-10-23 05:48:42 +02:00
|
|
|
status_by_digest = digestmap_get(fingerprint_list->status_by_digest,
|
|
|
|
id_digest);
|
2006-10-24 22:51:28 +02:00
|
|
|
if (status_by_digest)
|
|
|
|
result |= (status_by_digest->status & ~FP_NAMED);
|
2006-10-23 05:48:42 +02:00
|
|
|
|
|
|
|
if (result & FP_REJECT) {
|
|
|
|
if (msg)
|
|
|
|
*msg = "Fingerprint is marked rejected";
|
|
|
|
return FP_REJECT;
|
|
|
|
} else if (result & FP_INVALID) {
|
|
|
|
if (msg)
|
|
|
|
*msg = "Fingerprint is marked invalid";
|
|
|
|
}
|
2005-09-15 16:39:05 +02:00
|
|
|
|
2007-12-10 17:49:54 +01:00
|
|
|
if (authdir_policy_baddir_address(addr, or_port)) {
|
|
|
|
if (should_log)
|
|
|
|
log_info(LD_DIRSERV,
|
|
|
|
"Marking '%s' as bad directory because of address '%s'",
|
|
|
|
nickname, address);
|
|
|
|
result |= FP_BADDIR;
|
|
|
|
}
|
|
|
|
|
2006-10-23 05:48:42 +02:00
|
|
|
if (authdir_policy_badexit_address(addr, or_port)) {
|
|
|
|
if (should_log)
|
|
|
|
log_info(LD_DIRSERV, "Marking '%s' as bad exit because of address '%s'",
|
|
|
|
nickname, address);
|
|
|
|
result |= FP_BADEXIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(result & FP_NAMED)) {
|
2006-03-27 04:25:34 +02:00
|
|
|
if (!authdir_policy_permits_address(addr, or_port)) {
|
2005-12-06 07:55:43 +01:00
|
|
|
if (should_log)
|
2006-02-13 10:37:53 +01:00
|
|
|
log_info(LD_DIRSERV, "Rejecting '%s' because of address '%s'",
|
|
|
|
nickname, address);
|
2005-09-15 16:39:05 +02:00
|
|
|
if (msg)
|
|
|
|
*msg = "Authdir is rejecting routers in this range.";
|
|
|
|
return FP_REJECT;
|
|
|
|
}
|
2006-03-27 04:25:34 +02:00
|
|
|
if (!authdir_policy_valid_address(addr, or_port)) {
|
2005-12-06 07:55:43 +01:00
|
|
|
if (should_log)
|
2006-02-13 10:37:53 +01:00
|
|
|
log_info(LD_DIRSERV, "Not marking '%s' valid because of address '%s'",
|
|
|
|
nickname, address);
|
2006-10-23 05:48:42 +02:00
|
|
|
result |= FP_INVALID;
|
2005-09-15 16:39:05 +02:00
|
|
|
}
|
2006-12-04 19:54:01 +01:00
|
|
|
if (reject_unlisted) {
|
|
|
|
if (msg)
|
|
|
|
*msg = "Authdir rejects unknown routers.";
|
2006-10-23 05:48:42 +02:00
|
|
|
return FP_REJECT;
|
2006-12-04 19:54:01 +01:00
|
|
|
}
|
2003-09-27 23:30:10 +02:00
|
|
|
}
|
2006-10-23 05:48:42 +02:00
|
|
|
|
|
|
|
return result;
|
2003-09-27 23:30:10 +02:00
|
|
|
}
|
|
|
|
|
2004-09-29 01:27:41 +02:00
|
|
|
/** If we are an authoritative dirserver, and the list of approved
|
2004-09-29 00:24:56 +02:00
|
|
|
* servers contains one whose identity key digest is <b>digest</b>,
|
|
|
|
* return that router's nickname. Otherwise return NULL. */
|
2005-06-11 20:52:12 +02:00
|
|
|
const char *
|
|
|
|
dirserv_get_nickname_by_digest(const char *digest)
|
2004-05-05 06:55:00 +02:00
|
|
|
{
|
2006-10-23 05:48:42 +02:00
|
|
|
router_status_t *status;
|
2004-05-18 19:41:40 +02:00
|
|
|
if (!fingerprint_list)
|
2004-09-29 00:24:56 +02:00
|
|
|
return NULL;
|
|
|
|
tor_assert(digest);
|
|
|
|
|
2006-10-23 05:48:42 +02:00
|
|
|
status = digestmap_get(fingerprint_list->status_by_digest, digest);
|
|
|
|
return status ? status->nickname : NULL;
|
2004-09-29 00:24:56 +02:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Clear the current fingerprint list. */
|
2003-12-17 22:09:31 +01:00
|
|
|
void
|
2005-09-30 00:59:17 +02:00
|
|
|
dirserv_free_fingerprint_list(void)
|
2003-09-27 23:30:10 +02:00
|
|
|
{
|
2004-05-18 19:41:40 +02:00
|
|
|
if (!fingerprint_list)
|
|
|
|
return;
|
|
|
|
|
2006-10-23 05:48:42 +02:00
|
|
|
strmap_free(fingerprint_list->fp_by_name, _tor_free);
|
|
|
|
digestmap_free(fingerprint_list->status_by_digest, _tor_free);
|
|
|
|
tor_free(fingerprint_list);
|
2003-09-27 23:30:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Descriptor list
|
|
|
|
*/
|
2004-05-05 02:30:43 +02:00
|
|
|
|
2005-01-06 22:03:27 +01:00
|
|
|
/** Return -1 if <b>ri</b> has a private or otherwise bad address,
|
|
|
|
* unless we're configured to not care. Return 0 if all ok. */
|
|
|
|
static int
|
|
|
|
dirserv_router_has_valid_address(routerinfo_t *ri)
|
|
|
|
{
|
|
|
|
struct in_addr iaddr;
|
|
|
|
if (get_options()->DirAllowPrivateAddresses)
|
|
|
|
return 0; /* whatever it is, we're fine with it */
|
|
|
|
if (!tor_inet_aton(ri->address, &iaddr)) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_info(LD_DIRSERV,"Router '%s' published non-IP address '%s'. Refusing.",
|
|
|
|
ri->nickname, ri->address);
|
2005-01-06 22:03:27 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2005-12-09 03:46:46 +01:00
|
|
|
if (is_internal_IP(ntohl(iaddr.s_addr), 0)) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_info(LD_DIRSERV,
|
|
|
|
"Router '%s' published internal IP address '%s'. Refusing.",
|
|
|
|
ri->nickname, ri->address);
|
2005-01-06 22:03:27 +01:00
|
|
|
return -1; /* it's a private IP, we should reject it */
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-09-15 02:51:42 +02:00
|
|
|
/** Check whether we, as a directory server, want to accept <b>ri</b>. If so,
|
2006-01-03 11:42:20 +01:00
|
|
|
* set its is_valid,named,running fields and return 0. Otherwise, return -1.
|
2005-09-15 07:19:38 +02:00
|
|
|
*
|
2005-09-21 23:10:36 +02:00
|
|
|
* If the router is rejected, set *<b>msg</b> to an explanation of why.
|
2006-01-03 11:42:20 +01:00
|
|
|
*
|
|
|
|
* If <b>complain</b> then explain at log-level 'notice' why we refused
|
|
|
|
* a descriptor; else explain at log-level 'info'.
|
2005-09-15 02:51:42 +02:00
|
|
|
*/
|
2003-09-27 23:30:10 +02:00
|
|
|
int
|
2006-01-03 11:42:20 +01:00
|
|
|
authdir_wants_to_reject_router(routerinfo_t *ri, const char **msg,
|
|
|
|
int complain)
|
2003-09-27 23:30:10 +02:00
|
|
|
{
|
|
|
|
/* Okay. Now check whether the fingerprint is recognized. */
|
2006-10-23 05:48:42 +02:00
|
|
|
uint32_t status = dirserv_router_get_status(ri, msg);
|
2005-08-26 22:59:04 +02:00
|
|
|
time_t now;
|
2009-11-17 13:39:15 +01:00
|
|
|
int severity = (complain && ri->contact_info) ? LOG_NOTICE : LOG_INFO;
|
2005-09-23 19:11:20 +02:00
|
|
|
tor_assert(msg);
|
2006-10-23 05:48:42 +02:00
|
|
|
if (status & FP_REJECT)
|
2005-09-15 02:51:42 +02:00
|
|
|
return -1; /* msg is already set. */
|
|
|
|
|
2003-10-22 18:41:35 +02:00
|
|
|
/* Is there too much clock skew? */
|
2004-03-29 21:28:16 +02:00
|
|
|
now = time(NULL);
|
2005-11-05 21:15:27 +01:00
|
|
|
if (ri->cache_info.published_on > now+ROUTER_ALLOW_SKEW) {
|
2006-01-03 11:42:20 +01:00
|
|
|
log_fn(severity, LD_DIRSERV, "Publication time for nickname '%s' is too "
|
|
|
|
"far (%d minutes) in the future; possible clock skew. Not adding "
|
2006-03-11 03:21:30 +01:00
|
|
|
"(%s)",
|
2005-11-05 21:15:27 +01:00
|
|
|
ri->nickname, (int)((ri->cache_info.published_on-now)/60),
|
2006-03-11 03:21:30 +01:00
|
|
|
esc_router_info(ri));
|
2005-12-14 21:40:40 +01:00
|
|
|
*msg = "Rejected: Your clock is set too far in the future, or your "
|
|
|
|
"timezone is not correct.";
|
2005-01-20 21:18:32 +01:00
|
|
|
return -1;
|
2003-09-27 23:30:10 +02:00
|
|
|
}
|
2006-03-08 07:29:52 +01:00
|
|
|
if (ri->cache_info.published_on < now-ROUTER_MAX_AGE_TO_PUBLISH) {
|
2006-01-03 11:42:20 +01:00
|
|
|
log_fn(severity, LD_DIRSERV,
|
2005-12-10 09:27:01 +01:00
|
|
|
"Publication time for router with nickname '%s' is too far "
|
2006-03-11 03:21:30 +01:00
|
|
|
"(%d minutes) in the past. Not adding (%s)",
|
2005-11-05 21:15:27 +01:00
|
|
|
ri->nickname, (int)((now-ri->cache_info.published_on)/60),
|
2006-03-11 03:21:30 +01:00
|
|
|
esc_router_info(ri));
|
2005-12-14 21:40:40 +01:00
|
|
|
*msg = "Rejected: Server is expired, or your clock is too far in the past,"
|
|
|
|
" or your timezone is not correct.";
|
2005-01-20 21:18:32 +01:00
|
|
|
return -1;
|
2004-03-29 21:28:16 +02:00
|
|
|
}
|
2005-01-06 22:03:27 +01:00
|
|
|
if (dirserv_router_has_valid_address(ri) < 0) {
|
2006-01-03 11:42:20 +01:00
|
|
|
log_fn(severity, LD_DIRSERV,
|
|
|
|
"Router with nickname '%s' has invalid address '%s'. "
|
2006-03-11 03:21:30 +01:00
|
|
|
"Not adding (%s).",
|
2005-05-03 00:06:04 +02:00
|
|
|
ri->nickname, ri->address,
|
2006-03-11 03:21:30 +01:00
|
|
|
esc_router_info(ri));
|
2005-01-20 21:18:32 +01:00
|
|
|
*msg = "Rejected: Address is not an IP, or IP is a private address.";
|
|
|
|
return -1;
|
2005-01-06 22:03:27 +01:00
|
|
|
}
|
2005-09-15 02:51:42 +02:00
|
|
|
/* Okay, looks like we're willing to accept this one. */
|
2006-10-23 05:48:42 +02:00
|
|
|
ri->is_named = (status & FP_NAMED) ? 1 : 0;
|
|
|
|
ri->is_valid = (status & FP_INVALID) ? 0 : 1;
|
2007-12-10 17:49:54 +01:00
|
|
|
ri->is_bad_directory = (status & FP_BADDIR) ? 1 : 0;
|
2006-10-23 05:48:42 +02:00
|
|
|
ri->is_bad_exit = (status & FP_BADEXIT) ? 1 : 0;
|
2005-09-15 02:51:42 +02:00
|
|
|
|
2005-08-26 22:59:04 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-17 23:46:00 +01:00
|
|
|
/** True iff <b>a</b> is more severe than <b>b</b>. */
|
2008-12-17 22:50:01 +01:00
|
|
|
static int
|
|
|
|
WRA_MORE_SEVERE(was_router_added_t a, was_router_added_t b)
|
|
|
|
{
|
2008-12-22 07:25:49 +01:00
|
|
|
return a < b;
|
2008-12-17 22:50:01 +01:00
|
|
|
}
|
|
|
|
|
2008-12-17 23:46:00 +01:00
|
|
|
/** As for dirserv_add_descriptor(), but accepts multiple documents, and
|
2007-04-30 21:48:33 +02:00
|
|
|
* returns the most severe error that occurred for any one of them. */
|
2008-12-17 22:50:01 +01:00
|
|
|
was_router_added_t
|
2007-06-09 09:05:19 +02:00
|
|
|
dirserv_add_multiple_descriptors(const char *desc, uint8_t purpose,
|
2007-09-27 22:46:30 +02:00
|
|
|
const char *source,
|
2007-06-09 09:05:19 +02:00
|
|
|
const char **msg)
|
2007-04-30 21:48:33 +02:00
|
|
|
{
|
2008-12-22 05:53:11 +01:00
|
|
|
was_router_added_t r, r_tmp;
|
2007-04-30 21:48:33 +02:00
|
|
|
const char *msg_out;
|
2007-05-15 09:13:56 +02:00
|
|
|
smartlist_t *list;
|
|
|
|
const char *s;
|
|
|
|
int n_parsed = 0;
|
2007-09-27 22:46:30 +02:00
|
|
|
time_t now = time(NULL);
|
2007-12-02 05:39:56 +01:00
|
|
|
char annotation_buf[ROUTER_ANNOTATION_BUF_LEN];
|
2007-09-27 22:46:30 +02:00
|
|
|
char time_buf[ISO_TIME_LEN+1];
|
|
|
|
int general = purpose == ROUTER_PURPOSE_GENERAL;
|
2007-05-29 16:41:20 +02:00
|
|
|
tor_assert(msg);
|
2007-05-15 09:13:56 +02:00
|
|
|
|
2008-12-22 07:25:49 +01:00
|
|
|
r=ROUTER_ADDED_SUCCESSFULLY; /*Least severe return value. */
|
2008-12-22 05:53:11 +01:00
|
|
|
|
2007-09-27 22:46:30 +02:00
|
|
|
format_iso_time(time_buf, now);
|
|
|
|
if (tor_snprintf(annotation_buf, sizeof(annotation_buf),
|
|
|
|
"@uploaded-at %s\n"
|
|
|
|
"@source %s\n"
|
|
|
|
"%s%s%s", time_buf, escaped(source),
|
|
|
|
!general ? "@purpose " : "",
|
|
|
|
!general ? router_purpose_to_string(purpose) : "",
|
|
|
|
!general ? "\n" : "")<0) {
|
|
|
|
*msg = "Couldn't format annotations";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-05-15 09:13:56 +02:00
|
|
|
s = desc;
|
|
|
|
list = smartlist_create();
|
2007-09-27 18:08:10 +02:00
|
|
|
if (!router_parse_list_from_string(&s, NULL, list, SAVED_NOWHERE, 0, 0,
|
2007-09-27 22:46:30 +02:00
|
|
|
annotation_buf)) {
|
2007-05-15 09:13:56 +02:00
|
|
|
SMARTLIST_FOREACH(list, routerinfo_t *, ri, {
|
2007-05-28 23:34:42 +02:00
|
|
|
msg_out = NULL;
|
2007-10-11 02:13:06 +02:00
|
|
|
tor_assert(ri->purpose == purpose);
|
2009-04-01 15:02:04 +02:00
|
|
|
r_tmp = dirserv_add_descriptor(ri, &msg_out, source);
|
2008-12-17 22:50:01 +01:00
|
|
|
if (WRA_MORE_SEVERE(r_tmp, r)) {
|
2007-05-15 09:13:56 +02:00
|
|
|
r = r_tmp;
|
|
|
|
*msg = msg_out;
|
|
|
|
}
|
|
|
|
});
|
2007-04-30 21:48:33 +02:00
|
|
|
}
|
2007-05-15 09:13:56 +02:00
|
|
|
n_parsed += smartlist_len(list);
|
|
|
|
smartlist_clear(list);
|
|
|
|
|
|
|
|
s = desc;
|
2007-09-27 18:08:10 +02:00
|
|
|
if (!router_parse_list_from_string(&s, NULL, list, SAVED_NOWHERE, 1, 0,
|
|
|
|
NULL)) {
|
2007-05-15 09:13:56 +02:00
|
|
|
SMARTLIST_FOREACH(list, extrainfo_t *, ei, {
|
2007-05-28 23:34:42 +02:00
|
|
|
msg_out = NULL;
|
2007-06-09 09:05:19 +02:00
|
|
|
|
2007-05-15 09:13:56 +02:00
|
|
|
r_tmp = dirserv_add_extrainfo(ei, &msg_out);
|
2008-12-17 22:50:01 +01:00
|
|
|
if (WRA_MORE_SEVERE(r_tmp, r)) {
|
2007-05-15 09:13:56 +02:00
|
|
|
r = r_tmp;
|
|
|
|
*msg = msg_out;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
n_parsed += smartlist_len(list);
|
|
|
|
smartlist_free(list);
|
2007-04-30 21:48:33 +02:00
|
|
|
|
2007-05-29 20:39:56 +02:00
|
|
|
if (! *msg) {
|
|
|
|
if (!n_parsed) {
|
|
|
|
*msg = "No descriptors found in your POST.";
|
2008-12-22 05:53:11 +01:00
|
|
|
if (WRA_WAS_ADDED(r))
|
|
|
|
r = ROUTER_WAS_NOT_NEW;
|
2007-05-29 20:39:56 +02:00
|
|
|
} else {
|
|
|
|
*msg = "(no message)";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-17 22:50:01 +01:00
|
|
|
return r;
|
2007-04-30 21:48:33 +02:00
|
|
|
}
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/** Examine the parsed server descriptor in <b>ri</b> and maybe insert it into
|
|
|
|
* the list of server descriptors. Set *<b>msg</b> to a message that should be
|
2009-08-11 17:33:58 +02:00
|
|
|
* passed back to the origin of this descriptor, or NULL if there is no such
|
|
|
|
* message. Use <b>source</b> to produce better log messages.
|
2005-08-26 22:59:04 +02:00
|
|
|
*
|
2008-12-22 17:37:20 +01:00
|
|
|
* Return the status of the operation
|
2007-09-27 22:46:30 +02:00
|
|
|
*
|
|
|
|
* This function is only called when fresh descriptors are posted, not when
|
|
|
|
* we re-load the cache.
|
2005-08-26 22:59:04 +02:00
|
|
|
*/
|
2008-12-17 22:50:01 +01:00
|
|
|
was_router_added_t
|
2009-04-01 15:02:04 +02:00
|
|
|
dirserv_add_descriptor(routerinfo_t *ri, const char **msg, const char *source)
|
2005-08-26 22:59:04 +02:00
|
|
|
{
|
2008-12-17 22:50:01 +01:00
|
|
|
was_router_added_t r;
|
2007-05-15 09:13:56 +02:00
|
|
|
routerinfo_t *ri_old;
|
2009-04-01 15:02:04 +02:00
|
|
|
char *desc, *nickname;
|
2007-05-31 20:48:28 +02:00
|
|
|
size_t desclen = 0;
|
2009-08-11 17:33:58 +02:00
|
|
|
*msg = NULL;
|
2005-08-26 22:59:04 +02:00
|
|
|
|
2007-12-02 07:11:53 +01:00
|
|
|
/* If it's too big, refuse it now. Otherwise we'll cache it all over the
|
|
|
|
* network and it'll clog everything up. */
|
|
|
|
if (ri->cache_info.signed_descriptor_len > MAX_DESCRIPTOR_UPLOAD_SIZE) {
|
2009-04-01 15:02:04 +02:00
|
|
|
log_notice(LD_DIR, "Somebody attempted to publish a router descriptor '%s'"
|
|
|
|
" (source: %s) with size %d. Either this is an attack, or the "
|
2007-12-02 07:11:53 +01:00
|
|
|
"MAX_DESCRIPTOR_UPLOAD_SIZE (%d) constant is too low.",
|
2009-04-01 15:02:04 +02:00
|
|
|
ri->nickname, source, (int)ri->cache_info.signed_descriptor_len,
|
2007-12-02 07:11:53 +01:00
|
|
|
MAX_DESCRIPTOR_UPLOAD_SIZE);
|
|
|
|
*msg = "Router descriptor was too large";
|
|
|
|
control_event_or_authdir_new_descriptor("REJECTED",
|
|
|
|
ri->cache_info.signed_descriptor_body,
|
|
|
|
ri->cache_info.signed_descriptor_len, *msg);
|
|
|
|
routerinfo_free(ri);
|
2008-12-22 17:37:20 +01:00
|
|
|
return ROUTER_AUTHDIR_REJECTS;
|
2007-12-02 07:11:53 +01:00
|
|
|
}
|
|
|
|
|
2005-10-12 20:25:25 +02:00
|
|
|
/* Check whether this descriptor is semantically identical to the last one
|
|
|
|
* from this server. (We do this here and not in router_add_to_routerlist
|
|
|
|
* because we want to be able to accept the newest router descriptor that
|
|
|
|
* another authority has, so we all converge on the same one.) */
|
2005-11-05 21:15:27 +01:00
|
|
|
ri_old = router_get_by_digest(ri->cache_info.identity_digest);
|
|
|
|
if (ri_old && ri_old->cache_info.published_on < ri->cache_info.published_on
|
2006-01-08 22:33:15 +01:00
|
|
|
&& router_differences_are_cosmetic(ri_old, ri)
|
|
|
|
&& !router_is_me(ri)) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_info(LD_DIRSERV,
|
2009-04-01 15:02:04 +02:00
|
|
|
"Not replacing descriptor from '%s' (source: %s); "
|
|
|
|
"differences are cosmetic.",
|
|
|
|
ri->nickname, source);
|
2005-12-14 21:40:40 +01:00
|
|
|
*msg = "Not replacing router descriptor; no information has changed since "
|
|
|
|
"the last one with this identity.";
|
2007-05-31 20:48:28 +02:00
|
|
|
control_event_or_authdir_new_descriptor("DROPPED",
|
|
|
|
ri->cache_info.signed_descriptor_body,
|
|
|
|
ri->cache_info.signed_descriptor_len, *msg);
|
2007-05-19 20:31:02 +02:00
|
|
|
routerinfo_free(ri);
|
2008-12-22 17:37:20 +01:00
|
|
|
return ROUTER_WAS_NOT_NEW;
|
2005-10-12 20:25:25 +02:00
|
|
|
}
|
2009-04-01 15:02:04 +02:00
|
|
|
|
|
|
|
/* Make a copy of desc, since router_add_to_routerlist might free
|
|
|
|
* ri and its associated signed_descriptor_t. */
|
|
|
|
desclen = ri->cache_info.signed_descriptor_len;
|
|
|
|
desc = tor_strndup(ri->cache_info.signed_descriptor_body, desclen);
|
|
|
|
nickname = tor_strdup(ri->nickname);
|
2007-05-31 20:48:28 +02:00
|
|
|
|
2008-12-17 22:50:01 +01:00
|
|
|
r = router_add_to_routerlist(ri, msg, 0, 0);
|
|
|
|
if (!WRA_WAS_ADDED(r)) {
|
|
|
|
/* unless the routerinfo was fine, just out-of-date */
|
2009-04-01 15:02:04 +02:00
|
|
|
if (WRA_WAS_REJECTED(r))
|
2007-05-31 20:48:28 +02:00
|
|
|
control_event_or_authdir_new_descriptor("REJECTED", desc, desclen, *msg);
|
2009-04-01 15:02:04 +02:00
|
|
|
log_info(LD_DIRSERV,
|
|
|
|
"Did not add descriptor from '%s' (source: %s): %s.",
|
2009-08-11 17:33:58 +02:00
|
|
|
nickname, source, *msg ? *msg : "(no message)");
|
2005-08-26 22:59:04 +02:00
|
|
|
} else {
|
2005-11-20 17:53:49 +01:00
|
|
|
smartlist_t *changed;
|
2009-04-01 15:02:04 +02:00
|
|
|
control_event_or_authdir_new_descriptor("ACCEPTED", desc, desclen, *msg);
|
2005-11-19 19:35:43 +01:00
|
|
|
|
2005-11-20 17:53:49 +01:00
|
|
|
changed = smartlist_create();
|
2005-08-26 23:02:18 +02:00
|
|
|
smartlist_add(changed, ri);
|
|
|
|
control_event_descriptors_changed(changed);
|
|
|
|
smartlist_free(changed);
|
2005-08-26 23:46:24 +02:00
|
|
|
if (!*msg) {
|
2006-03-19 02:21:59 +01:00
|
|
|
*msg = ri->is_valid ? "Descriptor for valid server accepted" :
|
|
|
|
"Descriptor for invalid server accepted";
|
2005-08-26 23:46:24 +02:00
|
|
|
}
|
2009-04-01 15:02:04 +02:00
|
|
|
log_info(LD_DIRSERV,
|
|
|
|
"Added descriptor from '%s' (source: %s): %s.",
|
|
|
|
nickname, source, *msg);
|
2005-08-26 22:59:04 +02:00
|
|
|
}
|
2009-04-01 15:02:04 +02:00
|
|
|
tor_free(desc);
|
|
|
|
tor_free(nickname);
|
2008-12-17 22:50:01 +01:00
|
|
|
return r;
|
2003-09-27 23:30:10 +02:00
|
|
|
}
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/** As dirserv_add_descriptor, but for an extrainfo_t <b>ei</b>. */
|
2008-12-22 06:21:49 +01:00
|
|
|
static was_router_added_t
|
2007-05-15 09:13:56 +02:00
|
|
|
dirserv_add_extrainfo(extrainfo_t *ei, const char **msg)
|
|
|
|
{
|
|
|
|
routerinfo_t *ri;
|
2007-05-24 19:12:54 +02:00
|
|
|
int r;
|
2007-05-15 09:13:56 +02:00
|
|
|
tor_assert(msg);
|
|
|
|
*msg = NULL;
|
|
|
|
|
|
|
|
ri = router_get_by_digest(ei->cache_info.identity_digest);
|
|
|
|
if (!ri) {
|
|
|
|
*msg = "No corresponding router descriptor for extra-info descriptor";
|
|
|
|
extrainfo_free(ei);
|
2008-12-22 06:21:49 +01:00
|
|
|
return ROUTER_BAD_EI;
|
2007-05-15 09:13:56 +02:00
|
|
|
}
|
2007-12-02 07:11:53 +01:00
|
|
|
|
|
|
|
/* If it's too big, refuse it now. Otherwise we'll cache it all over the
|
|
|
|
* network and it'll clog everything up. */
|
|
|
|
if (ei->cache_info.signed_descriptor_len > MAX_EXTRAINFO_UPLOAD_SIZE) {
|
|
|
|
log_notice(LD_DIR, "Somebody attempted to publish an extrainfo "
|
|
|
|
"with size %d. Either this is an attack, or the "
|
|
|
|
"MAX_EXTRAINFO_UPLOAD_SIZE (%d) constant is too low.",
|
|
|
|
(int)ei->cache_info.signed_descriptor_len,
|
|
|
|
MAX_EXTRAINFO_UPLOAD_SIZE);
|
|
|
|
*msg = "Extrainfo document was too large";
|
|
|
|
extrainfo_free(ei);
|
2008-12-22 06:21:49 +01:00
|
|
|
return ROUTER_BAD_EI;
|
2007-12-02 07:11:53 +01:00
|
|
|
}
|
|
|
|
|
2007-10-21 02:08:35 +02:00
|
|
|
if ((r = routerinfo_incompatible_with_extrainfo(ri, ei, NULL, msg))) {
|
2007-05-15 09:13:56 +02:00
|
|
|
extrainfo_free(ei);
|
2008-12-22 06:21:49 +01:00
|
|
|
return r < 0 ? ROUTER_WAS_NOT_NEW : ROUTER_BAD_EI;
|
2007-05-15 09:13:56 +02:00
|
|
|
}
|
|
|
|
router_add_extrainfo_to_routerlist(ei, msg, 0, 0);
|
2008-12-22 06:21:49 +01:00
|
|
|
return ROUTER_ADDED_SUCCESSFULLY;
|
2007-05-15 09:13:56 +02:00
|
|
|
}
|
|
|
|
|
2005-06-21 03:00:42 +02:00
|
|
|
/** Remove all descriptors whose nicknames or fingerprints no longer
|
|
|
|
* are allowed by our fingerprint list. (Descriptors that used to be
|
|
|
|
* good can become bad when we reload the fingerprint list.)
|
2004-05-05 02:30:43 +02:00
|
|
|
*/
|
2004-04-13 22:06:08 +02:00
|
|
|
static void
|
2005-06-21 03:00:42 +02:00
|
|
|
directory_remove_invalid(void)
|
2004-04-13 22:06:08 +02:00
|
|
|
{
|
|
|
|
int i;
|
2005-08-26 22:59:04 +02:00
|
|
|
int changed = 0;
|
2005-10-18 19:43:54 +02:00
|
|
|
routerlist_t *rl = router_get_routerlist();
|
2004-05-18 19:41:40 +02:00
|
|
|
|
2007-04-30 07:32:57 +02:00
|
|
|
routerlist_assert_ok(rl);
|
|
|
|
|
2005-10-18 19:43:54 +02:00
|
|
|
for (i = 0; i < smartlist_len(rl->routers); ++i) {
|
2005-09-15 02:51:42 +02:00
|
|
|
const char *msg;
|
2005-10-18 19:43:54 +02:00
|
|
|
routerinfo_t *ent = smartlist_get(rl->routers, i);
|
2006-10-23 05:48:42 +02:00
|
|
|
uint32_t r = dirserv_router_get_status(ent, &msg);
|
|
|
|
if (r & FP_REJECT) {
|
|
|
|
log_info(LD_DIRSERV, "Router '%s' is now rejected: %s",
|
|
|
|
ent->nickname, msg?msg:"");
|
2009-06-20 07:52:59 +02:00
|
|
|
routerlist_remove(rl, ent, 0, time(NULL));
|
2007-07-18 16:02:47 +02:00
|
|
|
i--;
|
2006-10-23 05:48:42 +02:00
|
|
|
changed = 1;
|
2007-04-30 07:32:57 +02:00
|
|
|
continue;
|
2006-10-23 05:48:42 +02:00
|
|
|
}
|
2006-10-24 23:41:48 +02:00
|
|
|
if (bool_neq((r & FP_NAMED), ent->is_named)) {
|
2006-10-23 05:48:42 +02:00
|
|
|
log_info(LD_DIRSERV,
|
|
|
|
"Router '%s' is now %snamed.", ent->nickname,
|
|
|
|
(r&FP_NAMED)?"":"un");
|
|
|
|
ent->is_named = (r&FP_NAMED)?1:0;
|
|
|
|
changed = 1;
|
|
|
|
}
|
2006-10-24 23:41:48 +02:00
|
|
|
if (bool_neq((r & FP_INVALID), !ent->is_valid)) {
|
2006-10-23 05:48:42 +02:00
|
|
|
log_info(LD_DIRSERV, "Router '%s' is now %svalid.", ent->nickname,
|
|
|
|
(r&FP_INVALID) ? "in" : "");
|
|
|
|
ent->is_valid = (r&FP_INVALID)?0:1;
|
|
|
|
changed = 1;
|
|
|
|
}
|
2007-12-10 17:49:54 +01:00
|
|
|
if (bool_neq((r & FP_BADDIR), ent->is_bad_directory)) {
|
|
|
|
log_info(LD_DIRSERV, "Router '%s' is now a %s directory", ent->nickname,
|
|
|
|
(r & FP_BADDIR) ? "bad" : "good");
|
|
|
|
ent->is_bad_directory = (r&FP_BADDIR) ? 1: 0;
|
|
|
|
changed = 1;
|
|
|
|
}
|
2006-10-24 23:41:48 +02:00
|
|
|
if (bool_neq((r & FP_BADEXIT), ent->is_bad_exit)) {
|
2006-10-23 05:48:42 +02:00
|
|
|
log_info(LD_DIRSERV, "Router '%s' is now a %s exit", ent->nickname,
|
|
|
|
(r & FP_BADEXIT) ? "bad" : "good");
|
|
|
|
ent->is_bad_exit = (r&FP_BADEXIT) ? 1: 0;
|
|
|
|
changed = 1;
|
2004-04-13 22:06:08 +02:00
|
|
|
}
|
|
|
|
}
|
2005-08-26 22:59:04 +02:00
|
|
|
if (changed)
|
|
|
|
directory_set_dirty();
|
2007-04-30 07:32:57 +02:00
|
|
|
|
|
|
|
routerlist_assert_ok(rl);
|
2004-04-13 22:06:08 +02:00
|
|
|
}
|
|
|
|
|
2004-05-10 06:34:48 +02:00
|
|
|
/** Mark the directory as <b>dirty</b> -- when we're next asked for a
|
2004-05-05 02:30:43 +02:00
|
|
|
* directory, we will rebuild it instead of reusing the most recently
|
|
|
|
* generated one.
|
|
|
|
*/
|
2003-12-17 22:09:31 +01:00
|
|
|
void
|
2005-09-30 00:59:17 +02:00
|
|
|
directory_set_dirty(void)
|
2003-09-27 23:30:10 +02:00
|
|
|
{
|
2004-09-23 21:58:44 +02:00
|
|
|
time_t now = time(NULL);
|
2007-08-10 05:07:00 +02:00
|
|
|
int set_v1_dirty=0;
|
2007-07-26 00:56:40 +02:00
|
|
|
|
2008-02-16 00:39:08 +01:00
|
|
|
/* Regenerate stubs only every 8 hours.
|
2008-12-18 17:11:24 +01:00
|
|
|
* XXXX It would be nice to generate less often, but these are just
|
|
|
|
* stubs: it doesn't matter. */
|
2007-07-26 00:56:40 +02:00
|
|
|
#define STUB_REGENERATE_INTERVAL (8*60*60)
|
|
|
|
if (!the_directory || !the_runningrouters.dir)
|
|
|
|
set_v1_dirty = 1;
|
|
|
|
else if (the_directory->published < now - STUB_REGENERATE_INTERVAL ||
|
|
|
|
the_runningrouters.published < now - STUB_REGENERATE_INTERVAL)
|
|
|
|
set_v1_dirty = 1;
|
|
|
|
|
|
|
|
if (set_v1_dirty) {
|
|
|
|
if (!the_directory_is_dirty)
|
|
|
|
the_directory_is_dirty = now;
|
|
|
|
if (!runningrouters_is_dirty)
|
|
|
|
runningrouters_is_dirty = now;
|
|
|
|
}
|
2005-09-15 01:14:37 +02:00
|
|
|
if (!the_v2_networkstatus_is_dirty)
|
|
|
|
the_v2_networkstatus_is_dirty = now;
|
2003-09-27 23:30:10 +02:00
|
|
|
}
|
|
|
|
|
2004-10-27 02:48:51 +02:00
|
|
|
/**
|
|
|
|
* Allocate and return a description of the status of the server <b>desc</b>,
|
2007-05-02 11:12:04 +02:00
|
|
|
* for use in a v1-style router-status line. The server is listed
|
2004-10-27 02:48:51 +02:00
|
|
|
* as running iff <b>is_live</b> is true.
|
2004-05-05 02:30:43 +02:00
|
|
|
*/
|
2004-10-27 02:48:51 +02:00
|
|
|
static char *
|
2005-05-02 23:22:31 +02:00
|
|
|
list_single_server_status(routerinfo_t *desc, int is_live)
|
2003-10-01 02:43:34 +02:00
|
|
|
{
|
2004-10-27 02:48:51 +02:00
|
|
|
char buf[MAX_NICKNAME_LEN+HEX_DIGEST_LEN+4]; /* !nickname=$hexdigest\0 */
|
2003-10-01 02:43:34 +02:00
|
|
|
char *cp;
|
2004-07-13 20:23:40 +02:00
|
|
|
|
2004-10-27 02:48:51 +02:00
|
|
|
tor_assert(desc);
|
|
|
|
|
|
|
|
cp = buf;
|
|
|
|
if (!is_live) {
|
|
|
|
*cp++ = '!';
|
|
|
|
}
|
2006-03-19 02:21:59 +01:00
|
|
|
if (desc->is_valid) {
|
2004-10-27 08:48:16 +02:00
|
|
|
strlcpy(cp, desc->nickname, sizeof(buf)-(cp-buf));
|
2004-10-27 02:48:51 +02:00
|
|
|
cp += strlen(cp);
|
2005-05-02 23:22:31 +02:00
|
|
|
*cp++ = '=';
|
2004-10-27 02:48:51 +02:00
|
|
|
}
|
2005-05-02 23:22:31 +02:00
|
|
|
*cp++ = '$';
|
2005-11-05 21:15:27 +01:00
|
|
|
base16_encode(cp, HEX_DIGEST_LEN+1, desc->cache_info.identity_digest,
|
2005-05-02 23:22:31 +02:00
|
|
|
DIGEST_LEN);
|
2004-10-27 02:48:51 +02:00
|
|
|
return tor_strdup(buf);
|
|
|
|
}
|
|
|
|
|
2009-07-02 19:56:52 +02:00
|
|
|
static INLINE int
|
|
|
|
running_long_enough_to_decide_unreachable(void)
|
|
|
|
{
|
|
|
|
return time_of_process_start
|
|
|
|
+ get_options()->TestingAuthDirTimeToLearnReachability < approx_time();
|
|
|
|
}
|
|
|
|
|
2006-03-12 22:24:03 +01:00
|
|
|
/** Each server needs to have passed a reachability test no more
|
|
|
|
* than this number of seconds ago, or he is listed as down in
|
|
|
|
* the directory. */
|
2006-05-28 18:14:26 +02:00
|
|
|
#define REACHABLE_TIMEOUT (45*60)
|
2005-08-24 04:31:02 +02:00
|
|
|
|
2005-08-30 08:43:07 +02:00
|
|
|
/** Treat a router as alive if
|
|
|
|
* - It's me, and I'm not hibernating.
|
2006-03-12 21:57:52 +01:00
|
|
|
* or - We've found it reachable recently. */
|
2007-12-18 23:39:15 +01:00
|
|
|
void
|
2007-08-17 03:29:58 +02:00
|
|
|
dirserv_set_router_is_running(routerinfo_t *router, time_t now)
|
2005-08-30 08:43:07 +02:00
|
|
|
{
|
2009-07-02 19:56:52 +02:00
|
|
|
/*XXXX022 This function is a mess. Separate out the part that calculates
|
|
|
|
whether it's reachable and the part that tells rephist that the router was
|
|
|
|
unreachable.
|
|
|
|
*/
|
2007-08-17 03:29:58 +02:00
|
|
|
int answer;
|
|
|
|
|
2005-08-30 08:43:07 +02:00
|
|
|
if (router_is_me(router) && !we_are_hibernating())
|
2007-08-17 03:29:58 +02:00
|
|
|
answer = 1;
|
|
|
|
else
|
|
|
|
answer = get_options()->AssumeReachable ||
|
|
|
|
now < router->last_reachable + REACHABLE_TIMEOUT;
|
|
|
|
|
2009-07-02 19:56:52 +02:00
|
|
|
if (!answer && running_long_enough_to_decide_unreachable()) {
|
2009-06-20 07:37:09 +02:00
|
|
|
/* not considered reachable. tell rephist. */
|
2007-08-17 03:29:58 +02:00
|
|
|
rep_hist_note_router_unreachable(router->cache_info.identity_digest, now);
|
|
|
|
}
|
|
|
|
|
|
|
|
router->is_running = answer;
|
2005-08-30 08:43:07 +02:00
|
|
|
}
|
|
|
|
|
2005-03-23 20:15:10 +01:00
|
|
|
/** Based on the routerinfo_ts in <b>routers</b>, allocate the
|
2007-05-02 11:12:04 +02:00
|
|
|
* contents of a v1-style router-status line, and store it in
|
2005-05-02 23:22:31 +02:00
|
|
|
* *<b>router_status_out</b>. Return 0 on success, -1 on failure.
|
2006-10-27 04:07:04 +02:00
|
|
|
*
|
|
|
|
* If for_controller is true, include the routers with very old descriptors.
|
2004-10-27 02:48:51 +02:00
|
|
|
*/
|
2005-03-22 04:27:51 +01:00
|
|
|
int
|
2007-12-18 23:39:15 +01:00
|
|
|
list_server_status_v1(smartlist_t *routers, char **router_status_out,
|
|
|
|
int for_controller)
|
2004-10-27 02:48:51 +02:00
|
|
|
{
|
|
|
|
/* List of entries in a router-status style: An optional !, then an optional
|
|
|
|
* equals-suffixed nickname, then a dollar-prefixed hexdigest. */
|
|
|
|
smartlist_t *rs_entries;
|
2005-08-24 04:31:02 +02:00
|
|
|
time_t now = time(NULL);
|
2006-03-08 07:29:52 +01:00
|
|
|
time_t cutoff = now - ROUTER_MAX_AGE_TO_PUBLISH;
|
2007-05-02 11:12:04 +02:00
|
|
|
or_options_t *options = get_options();
|
|
|
|
/* We include v2 dir auths here too, because they need to answer
|
2007-12-18 23:39:15 +01:00
|
|
|
* controllers. Eventually we'll deprecate this whole function;
|
|
|
|
* see also networkstatus_getinfo_by_purpose(). */
|
2007-06-09 09:05:19 +02:00
|
|
|
int authdir = authdir_mode_publishes_statuses(options);
|
2005-05-02 23:22:31 +02:00
|
|
|
tor_assert(router_status_out);
|
2004-10-27 02:48:51 +02:00
|
|
|
|
|
|
|
rs_entries = smartlist_create();
|
|
|
|
|
2009-05-08 18:12:20 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(routers, routerinfo_t *, ri) {
|
2007-05-02 11:12:04 +02:00
|
|
|
if (authdir) {
|
2005-08-29 20:42:36 +02:00
|
|
|
/* Update router status in routerinfo_t. */
|
2007-08-17 03:29:58 +02:00
|
|
|
dirserv_set_router_is_running(ri, now);
|
2005-03-23 20:15:10 +01:00
|
|
|
}
|
2009-05-08 18:12:20 +02:00
|
|
|
if (for_controller) {
|
2006-10-27 04:07:04 +02:00
|
|
|
char name_buf[MAX_VERBOSE_NICKNAME_LEN+2];
|
|
|
|
char *cp = name_buf;
|
|
|
|
if (!ri->is_running)
|
|
|
|
*cp++ = '!';
|
|
|
|
router_get_verbose_nickname(cp, ri);
|
|
|
|
smartlist_add(rs_entries, tor_strdup(name_buf));
|
2009-05-08 18:12:20 +02:00
|
|
|
} else if (ri->cache_info.published_on >= cutoff) {
|
|
|
|
smartlist_add(rs_entries, list_single_server_status(ri, ri->is_running));
|
2006-10-27 04:07:04 +02:00
|
|
|
}
|
2009-05-08 18:12:20 +02:00
|
|
|
} SMARTLIST_FOREACH_END(ri);
|
2004-10-27 02:48:51 +02:00
|
|
|
|
2005-08-30 08:43:07 +02:00
|
|
|
*router_status_out = smartlist_join_strings(rs_entries, " ", 0, NULL);
|
2004-10-27 02:48:51 +02:00
|
|
|
|
|
|
|
SMARTLIST_FOREACH(rs_entries, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(rs_entries);
|
|
|
|
|
2003-10-01 02:43:34 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-02 21:06:43 +01:00
|
|
|
/** Given a (possibly empty) list of config_line_t, each line of which contains
|
2005-08-26 17:34:53 +02:00
|
|
|
* a list of comma-separated version numbers surrounded by optional space,
|
|
|
|
* allocate and return a new string containing the version numbers, in order,
|
2005-09-15 08:15:31 +02:00
|
|
|
* separated by commas. Used to generate Recommended(Client|Server)?Versions
|
|
|
|
*/
|
2005-08-25 22:33:17 +02:00
|
|
|
static char *
|
|
|
|
format_versions_list(config_line_t *ln)
|
|
|
|
{
|
|
|
|
smartlist_t *versions;
|
|
|
|
char *result;
|
|
|
|
versions = smartlist_create();
|
|
|
|
for ( ; ln; ln = ln->next) {
|
|
|
|
smartlist_split_string(versions, ln->value, ",",
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
|
|
|
}
|
2006-04-03 08:23:24 +02:00
|
|
|
sort_version_list(versions, 1);
|
2005-08-25 22:33:17 +02:00
|
|
|
result = smartlist_join_strings(versions,",",0,NULL);
|
|
|
|
SMARTLIST_FOREACH(versions,char *,s,tor_free(s));
|
|
|
|
smartlist_free(versions);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-02-28 22:06:05 +01:00
|
|
|
/** Return 1 if <b>ri</b>'s descriptor is "active" -- running, valid,
|
|
|
|
* not hibernating, and not too old. Else return 0.
|
2006-03-29 10:56:39 +02:00
|
|
|
*/
|
|
|
|
static int
|
2007-02-28 22:06:05 +01:00
|
|
|
router_is_active(routerinfo_t *ri, time_t now)
|
2006-03-29 10:56:39 +02:00
|
|
|
{
|
|
|
|
time_t cutoff = now - ROUTER_MAX_AGE_TO_PUBLISH;
|
|
|
|
if (ri->cache_info.published_on < cutoff)
|
|
|
|
return 0;
|
2007-02-28 22:06:05 +01:00
|
|
|
if (!ri->is_running || !ri->is_valid || ri->is_hibernating)
|
2006-03-29 11:21:23 +02:00
|
|
|
return 0;
|
2006-03-29 10:56:39 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Generate a new v1 directory and write it into a newly allocated string.
|
2004-12-24 03:17:32 +01:00
|
|
|
* Point *<b>dir_out</b> to the allocated string. Sign the
|
2004-05-10 06:34:48 +02:00
|
|
|
* directory with <b>private_key</b>. Return 0 on success, -1 on
|
2006-06-13 13:11:19 +02:00
|
|
|
* failure. If <b>complete</b> is set, give us all the descriptors;
|
|
|
|
* otherwise leave out non-running and non-valid ones.
|
2004-03-29 21:28:16 +02:00
|
|
|
*/
|
2003-09-27 23:30:10 +02:00
|
|
|
int
|
2004-12-24 03:17:32 +01:00
|
|
|
dirserv_dump_directory_to_string(char **dir_out,
|
2007-10-11 15:56:33 +02:00
|
|
|
crypto_pk_env_t *private_key)
|
2003-09-27 23:30:10 +02:00
|
|
|
{
|
2005-10-08 08:02:41 +02:00
|
|
|
char *cp;
|
2004-10-07 23:10:40 +02:00
|
|
|
char *identity_pkey; /* Identity key, DER64-encoded. */
|
2004-10-15 07:09:48 +02:00
|
|
|
char *recommended_versions;
|
2005-08-25 22:33:17 +02:00
|
|
|
char digest[DIGEST_LEN];
|
|
|
|
char published[ISO_TIME_LEN+1];
|
2004-12-24 03:17:32 +01:00
|
|
|
char *buf = NULL;
|
|
|
|
size_t buf_len;
|
2005-05-02 23:22:31 +02:00
|
|
|
size_t identity_pkey_len;
|
2006-03-22 22:32:42 +01:00
|
|
|
time_t now = time(NULL);
|
2004-12-24 03:17:32 +01:00
|
|
|
|
|
|
|
tor_assert(dir_out);
|
|
|
|
*dir_out = NULL;
|
2003-09-27 23:30:10 +02:00
|
|
|
|
2005-05-02 23:22:31 +02:00
|
|
|
if (crypto_pk_write_public_key_to_string(private_key,&identity_pkey,
|
|
|
|
&identity_pkey_len)<0) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_warn(LD_BUG,"write identity_pkey to string failed!");
|
2004-10-06 15:31:48 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2004-10-15 07:09:48 +02:00
|
|
|
|
2005-12-14 21:40:40 +01:00
|
|
|
recommended_versions =
|
|
|
|
format_versions_list(get_options()->RecommendedVersions);
|
2004-11-09 21:04:00 +01:00
|
|
|
|
2006-03-22 22:32:42 +01:00
|
|
|
format_iso_time(published, now);
|
2004-12-24 03:17:32 +01:00
|
|
|
|
2007-10-11 15:56:33 +02:00
|
|
|
buf_len = 2048+strlen(recommended_versions);
|
|
|
|
|
2004-12-24 03:17:32 +01:00
|
|
|
buf = tor_malloc(buf_len);
|
|
|
|
/* We'll be comparing against buf_len throughout the rest of the
|
|
|
|
function, though strictly speaking we shouldn't be able to exceed
|
|
|
|
it. This is C, after all, so we may as well check for buffer
|
|
|
|
overruns.*/
|
|
|
|
|
|
|
|
tor_snprintf(buf, buf_len,
|
2005-12-10 10:36:26 +01:00
|
|
|
"signed-directory\n"
|
|
|
|
"published %s\n"
|
|
|
|
"recommended-software %s\n"
|
|
|
|
"router-status %s\n"
|
|
|
|
"dir-signing-key\n%s\n",
|
2007-10-11 15:56:33 +02:00
|
|
|
published, recommended_versions, "",
|
2005-12-10 10:36:26 +01:00
|
|
|
identity_pkey);
|
2004-06-25 02:29:31 +02:00
|
|
|
|
2005-02-01 00:47:25 +01:00
|
|
|
tor_free(recommended_versions);
|
2004-10-06 15:31:48 +02:00
|
|
|
tor_free(identity_pkey);
|
2003-12-14 06:25:23 +01:00
|
|
|
|
2005-10-08 08:02:41 +02:00
|
|
|
cp = buf + strlen(buf);
|
|
|
|
*cp = '\0';
|
2004-05-18 19:41:40 +02:00
|
|
|
|
2004-03-29 21:28:16 +02:00
|
|
|
/* These multiple strlcat calls are inefficient, but dwarfed by the RSA
|
2006-02-05 03:33:40 +01:00
|
|
|
signature. */
|
2004-12-24 03:17:32 +01:00
|
|
|
if (strlcat(buf, "directory-signature ", buf_len) >= buf_len)
|
2004-03-29 21:28:16 +02:00
|
|
|
goto truncated;
|
2004-12-24 03:17:32 +01:00
|
|
|
if (strlcat(buf, get_options()->Nickname, buf_len) >= buf_len)
|
2004-04-25 00:17:50 +02:00
|
|
|
goto truncated;
|
2004-12-24 03:17:32 +01:00
|
|
|
if (strlcat(buf, "\n", buf_len) >= buf_len)
|
2004-04-25 00:17:50 +02:00
|
|
|
goto truncated;
|
|
|
|
|
2004-12-24 03:17:32 +01:00
|
|
|
if (router_get_dir_hash(buf,digest)) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_warn(LD_BUG,"couldn't compute digest");
|
2004-12-24 03:17:32 +01:00
|
|
|
tor_free(buf);
|
2003-09-27 23:30:10 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2006-10-31 20:17:07 +01:00
|
|
|
note_crypto_pk_op(SIGN_DIR);
|
2009-09-14 17:57:19 +02:00
|
|
|
if (router_append_dirobj_signature(buf,buf_len,digest,DIGEST_LEN,
|
|
|
|
private_key)<0) {
|
2004-12-24 03:17:32 +01:00
|
|
|
tor_free(buf);
|
2003-09-27 23:30:10 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2004-07-23 01:21:12 +02:00
|
|
|
|
2004-12-24 03:17:32 +01:00
|
|
|
*dir_out = buf;
|
2003-09-27 23:30:10 +02:00
|
|
|
return 0;
|
2004-03-29 21:28:16 +02:00
|
|
|
truncated:
|
2006-02-13 10:37:53 +01:00
|
|
|
log_warn(LD_BUG,"tried to exceed string length.");
|
2004-12-24 03:17:32 +01:00
|
|
|
tor_free(buf);
|
2004-03-29 21:28:16 +02:00
|
|
|
return -1;
|
2003-09-27 23:30:10 +02:00
|
|
|
}
|
|
|
|
|
2007-12-01 05:58:53 +01:00
|
|
|
/********************************************************************/
|
|
|
|
|
|
|
|
/* A set of functions to answer questions about how we'd like to behave
|
2007-12-04 19:35:03 +01:00
|
|
|
* as a directory mirror/client. */
|
2007-12-01 05:58:53 +01:00
|
|
|
|
2007-12-04 19:35:03 +01:00
|
|
|
/** Return 1 if we fetch our directory material directly from the
|
|
|
|
* authorities, rather than from a mirror. */
|
|
|
|
int
|
|
|
|
directory_fetches_from_authorities(or_options_t *options)
|
|
|
|
{
|
2008-01-18 05:42:22 +01:00
|
|
|
routerinfo_t *me;
|
2008-02-24 00:39:16 +01:00
|
|
|
uint32_t addr;
|
2007-12-21 07:08:00 +01:00
|
|
|
if (options->FetchDirInfoEarly)
|
|
|
|
return 1;
|
2007-12-06 18:01:16 +01:00
|
|
|
if (options->BridgeRelay == 1)
|
|
|
|
return 0;
|
2008-02-24 00:39:16 +01:00
|
|
|
if (server_mode(options) && router_pick_published_address(options, &addr)<0)
|
|
|
|
return 1; /* we don't know our IP address; ask an authority. */
|
2010-03-16 09:02:18 +01:00
|
|
|
if (options->DirPort == 0 && !options->RefuseUnknownExits)
|
2008-02-24 00:39:16 +01:00
|
|
|
return 0;
|
2008-01-18 05:42:22 +01:00
|
|
|
if (!server_mode(options) || !advertised_server_mode())
|
2007-12-04 19:35:03 +01:00
|
|
|
return 0;
|
2008-01-18 05:42:22 +01:00
|
|
|
me = router_get_my_routerinfo();
|
2010-03-16 09:02:18 +01:00
|
|
|
if (!me || (!me->dir_port && !options->RefuseUnknownExits))
|
2008-01-18 05:42:22 +01:00
|
|
|
return 0; /* if dirport not advertised, return 0 too */
|
2007-12-04 19:35:03 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-12-22 15:56:28 +01:00
|
|
|
/** Return 1 if we should fetch new networkstatuses, descriptors, etc
|
2007-12-04 19:35:03 +01:00
|
|
|
* on the "mirror" schedule rather than the "client" schedule.
|
2007-12-01 05:58:53 +01:00
|
|
|
*/
|
|
|
|
int
|
2007-12-21 07:08:00 +01:00
|
|
|
directory_fetches_dir_info_early(or_options_t *options)
|
2007-12-01 05:58:53 +01:00
|
|
|
{
|
2007-12-04 19:35:03 +01:00
|
|
|
return directory_fetches_from_authorities(options);
|
2007-12-01 05:58:53 +01:00
|
|
|
}
|
|
|
|
|
2008-12-22 15:56:28 +01:00
|
|
|
/** Return 1 if we should fetch new networkstatuses, descriptors, etc
|
2007-12-04 19:35:03 +01:00
|
|
|
* on a very passive schedule -- waiting long enough for ordinary clients
|
|
|
|
* to probably have the info we want. These would include bridge users,
|
|
|
|
* and maybe others in the future e.g. if a Tor client uses another Tor
|
|
|
|
* client as a directory guard.
|
|
|
|
*/
|
2007-12-01 05:58:53 +01:00
|
|
|
int
|
2007-12-21 07:08:00 +01:00
|
|
|
directory_fetches_dir_info_later(or_options_t *options)
|
2007-12-04 19:35:03 +01:00
|
|
|
{
|
|
|
|
return options->UseBridges != 0;
|
|
|
|
}
|
|
|
|
|
2007-12-20 07:47:59 +01:00
|
|
|
/** Return 1 if we want to cache v2 dir info (each status file).
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
directory_caches_v2_dir_info(or_options_t *options)
|
|
|
|
{
|
|
|
|
return options->DirPort != 0;
|
|
|
|
}
|
|
|
|
|
2007-12-04 19:35:03 +01:00
|
|
|
/** Return 1 if we want to keep descriptors, networkstatuses, etc around
|
|
|
|
* and we're willing to serve them to others. Else return 0.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
directory_caches_dir_info(or_options_t *options)
|
2007-12-01 05:58:53 +01:00
|
|
|
{
|
2010-03-16 09:02:18 +01:00
|
|
|
if (options->BridgeRelay || options->DirPort)
|
|
|
|
return 1;
|
|
|
|
if (!server_mode(options) || !advertised_server_mode())
|
|
|
|
return 0;
|
|
|
|
return options->RefuseUnknownExits;
|
2007-12-01 05:58:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Return 1 if we want to allow remote people to ask us directory
|
|
|
|
* requests via the "begin_dir" interface, which doesn't require
|
|
|
|
* having any separate port open. */
|
|
|
|
int
|
|
|
|
directory_permits_begindir_requests(or_options_t *options)
|
|
|
|
{
|
2007-12-04 19:35:03 +01:00
|
|
|
return options->BridgeRelay != 0 || options->DirPort != 0;
|
2007-12-01 05:58:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Return 1 if we want to allow controllers to ask us directory
|
|
|
|
* requests via the controller interface, which doesn't require
|
|
|
|
* having any separate port open. */
|
|
|
|
int
|
|
|
|
directory_permits_controller_requests(or_options_t *options)
|
|
|
|
{
|
|
|
|
return options->DirPort != 0;
|
|
|
|
}
|
|
|
|
|
2007-12-04 19:35:03 +01:00
|
|
|
/** Return 1 if we have no need to fetch new descriptors. This generally
|
|
|
|
* happens when we're not a dir cache and we haven't built any circuits
|
|
|
|
* lately.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
directory_too_idle_to_fetch_descriptors(or_options_t *options, time_t now)
|
|
|
|
{
|
2009-01-05 19:54:11 +01:00
|
|
|
return !directory_caches_dir_info(options) &&
|
|
|
|
!options->FetchUselessDescriptors &&
|
2007-12-04 19:35:03 +01:00
|
|
|
rep_hist_circbuilding_dormant(now);
|
|
|
|
}
|
|
|
|
|
2007-12-01 05:58:53 +01:00
|
|
|
/********************************************************************/
|
|
|
|
|
2007-05-02 11:12:04 +02:00
|
|
|
/* Used only by non-v1-auth dirservers: The v1 directory and
|
|
|
|
* runningrouters we'll serve when requested. */
|
2008-12-23 18:56:31 +01:00
|
|
|
|
|
|
|
/** The v1 directory we'll serve (as a cache or as an authority) if
|
|
|
|
* requested. */
|
2006-06-18 09:38:55 +02:00
|
|
|
static cached_dir_t *cached_directory = NULL;
|
2008-12-23 18:56:31 +01:00
|
|
|
/** The v1 runningrouters document we'll serve (as a cache or as an authority)
|
|
|
|
* if requested. */
|
2009-10-13 23:06:01 +02:00
|
|
|
static cached_dir_t cached_runningrouters;
|
2004-06-21 06:37:27 +02:00
|
|
|
|
2007-02-02 21:06:43 +01:00
|
|
|
/** Used for other dirservers' v2 network statuses. Map from hexdigest to
|
2005-08-25 22:33:17 +02:00
|
|
|
* cached_dir_t. */
|
2005-10-18 22:12:22 +02:00
|
|
|
static digestmap_t *cached_v2_networkstatus = NULL;
|
2005-08-25 22:33:17 +02:00
|
|
|
|
2009-10-13 23:06:01 +02:00
|
|
|
/** Map from flavor name to the v3 consensuses that we're currently serving. */
|
|
|
|
static strmap_t *cached_consensuses = NULL;
|
2007-07-30 01:11:44 +02:00
|
|
|
|
2005-08-26 17:34:53 +02:00
|
|
|
/** Possibly replace the contents of <b>d</b> with the value of
|
2005-08-28 06:20:37 +02:00
|
|
|
* <b>directory</b> published on <b>when</b>, unless <b>when</b> is older than
|
|
|
|
* the last value, or too far in the future.
|
|
|
|
*
|
2006-07-21 09:53:21 +02:00
|
|
|
* Does not copy <b>directory</b>; frees it if it isn't used.
|
2005-08-28 06:20:37 +02:00
|
|
|
*/
|
2005-08-25 22:33:17 +02:00
|
|
|
static void
|
2005-08-28 06:20:37 +02:00
|
|
|
set_cached_dir(cached_dir_t *d, char *directory, time_t when)
|
2004-06-21 06:37:27 +02:00
|
|
|
{
|
2005-08-25 22:33:17 +02:00
|
|
|
time_t now = time(NULL);
|
2004-11-15 05:04:20 +01:00
|
|
|
if (when<=d->published) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_info(LD_DIRSERV, "Ignoring old directory; not caching.");
|
2005-08-28 06:20:37 +02:00
|
|
|
tor_free(directory);
|
2006-03-08 07:29:52 +01:00
|
|
|
} else if (when>=now+ROUTER_MAX_AGE_TO_PUBLISH) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_info(LD_DIRSERV, "Ignoring future directory; not caching.");
|
2005-08-28 06:20:37 +02:00
|
|
|
tor_free(directory);
|
2005-03-23 07:39:53 +01:00
|
|
|
} else {
|
|
|
|
/* if (when>d->published && when<now+ROUTER_MAX_AGE) */
|
2006-02-13 10:37:53 +01:00
|
|
|
log_debug(LD_DIRSERV, "Caching directory.");
|
2004-11-15 05:04:20 +01:00
|
|
|
tor_free(d->dir);
|
2005-08-28 06:20:37 +02:00
|
|
|
d->dir = directory;
|
2004-11-15 05:04:20 +01:00
|
|
|
d->dir_len = strlen(directory);
|
|
|
|
tor_free(d->dir_z);
|
|
|
|
if (tor_gzip_compress(&(d->dir_z), &(d->dir_z_len), d->dir, d->dir_len,
|
2004-09-02 20:57:09 +02:00
|
|
|
ZLIB_METHOD)) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_warn(LD_BUG,"Error compressing cached directory");
|
2004-09-02 20:57:09 +02:00
|
|
|
}
|
2004-11-15 05:04:20 +01:00
|
|
|
d->published = when;
|
2005-08-25 22:33:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-23 20:22:52 +01:00
|
|
|
/** Decrement the reference count on <b>d</b>, and free it if it no longer has
|
|
|
|
* any references. */
|
2006-06-18 09:38:55 +02:00
|
|
|
void
|
|
|
|
cached_dir_decref(cached_dir_t *d)
|
|
|
|
{
|
|
|
|
if (!d || --d->refcnt > 0)
|
|
|
|
return;
|
|
|
|
clear_cached_dir(d);
|
|
|
|
tor_free(d);
|
|
|
|
}
|
|
|
|
|
2007-01-23 20:22:52 +01:00
|
|
|
/** Allocate and return a new cached_dir_t containing the string <b>s</b>,
|
|
|
|
* published at <b>published</b>. */
|
2007-07-26 00:56:44 +02:00
|
|
|
cached_dir_t *
|
2006-06-18 09:38:55 +02:00
|
|
|
new_cached_dir(char *s, time_t published)
|
|
|
|
{
|
|
|
|
cached_dir_t *d = tor_malloc_zero(sizeof(cached_dir_t));
|
|
|
|
d->refcnt = 1;
|
|
|
|
d->dir = s;
|
|
|
|
d->dir_len = strlen(s);
|
|
|
|
d->published = published;
|
|
|
|
if (tor_gzip_compress(&(d->dir_z), &(d->dir_z_len), d->dir, d->dir_len,
|
|
|
|
ZLIB_METHOD)) {
|
|
|
|
log_warn(LD_BUG, "Error compressing directory");
|
|
|
|
}
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2005-08-26 17:34:53 +02:00
|
|
|
/** Remove all storage held in <b>d</b>, but do not free <b>d</b> itself. */
|
2005-08-25 22:33:17 +02:00
|
|
|
static void
|
|
|
|
clear_cached_dir(cached_dir_t *d)
|
|
|
|
{
|
|
|
|
tor_free(d->dir);
|
|
|
|
tor_free(d->dir_z);
|
|
|
|
memset(d, 0, sizeof(cached_dir_t));
|
|
|
|
}
|
|
|
|
|
2005-08-26 17:34:53 +02:00
|
|
|
/** Free all storage held by the cached_dir_t in <b>d</b>. */
|
2005-08-25 22:33:17 +02:00
|
|
|
static void
|
2006-06-20 02:48:23 +02:00
|
|
|
_free_cached_dir(void *_d)
|
2005-08-25 22:33:17 +02:00
|
|
|
{
|
2009-09-28 16:37:01 +02:00
|
|
|
cached_dir_t *d;
|
|
|
|
if (!_d)
|
|
|
|
return;
|
|
|
|
|
|
|
|
d = (cached_dir_t *)_d;
|
2006-06-20 02:48:23 +02:00
|
|
|
cached_dir_decref(d);
|
2005-08-25 22:33:17 +02:00
|
|
|
}
|
|
|
|
|
2008-09-14 06:07:29 +02:00
|
|
|
/** If we have no cached v1 directory, or it is older than <b>published</b>,
|
2007-02-24 02:12:53 +01:00
|
|
|
* then replace it with <b>directory</b>, published at <b>published</b>.
|
|
|
|
*
|
2007-02-25 04:43:00 +01:00
|
|
|
* If <b>published</b> is too old, do nothing.
|
|
|
|
*
|
2007-05-02 11:12:04 +02:00
|
|
|
* If <b>is_running_routers</b>, this is really a v1 running_routers
|
|
|
|
* document rather than a v1 directory.
|
2005-08-25 22:33:17 +02:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
dirserv_set_cached_directory(const char *directory, time_t published,
|
|
|
|
int is_running_routers)
|
|
|
|
{
|
2007-02-25 04:43:00 +01:00
|
|
|
time_t now = time(NULL);
|
|
|
|
|
2006-06-18 09:38:55 +02:00
|
|
|
if (is_running_routers) {
|
2007-02-25 04:43:00 +01:00
|
|
|
if (published >= now - MAX_V1_RR_AGE)
|
|
|
|
set_cached_dir(&cached_runningrouters, tor_strdup(directory), published);
|
2006-06-18 09:38:55 +02:00
|
|
|
} else {
|
2007-02-25 04:43:00 +01:00
|
|
|
if (published >= now - MAX_V1_DIRECTORY_AGE) {
|
|
|
|
cached_dir_decref(cached_directory);
|
|
|
|
cached_directory = new_cached_dir(tor_strdup(directory), published);
|
|
|
|
}
|
2006-06-18 09:38:55 +02:00
|
|
|
}
|
2005-08-25 22:33:17 +02:00
|
|
|
}
|
|
|
|
|
2007-05-02 11:12:04 +02:00
|
|
|
/** If <b>networkstatus</b> is non-NULL, we've just received a v2
|
|
|
|
* network-status for an authoritative directory with identity digest
|
|
|
|
* <b>identity</b> published at <b>published</b> -- store it so we can
|
|
|
|
* serve it to others.
|
|
|
|
*
|
|
|
|
* If <b>networkstatus</b> is NULL, remove the entry with the given
|
|
|
|
* identity fingerprint from the v2 cache.
|
2005-08-26 20:02:49 +02:00
|
|
|
*/
|
2005-08-25 22:33:17 +02:00
|
|
|
void
|
2006-01-08 22:26:33 +01:00
|
|
|
dirserv_set_cached_networkstatus_v2(const char *networkstatus,
|
2005-10-18 22:12:22 +02:00
|
|
|
const char *identity,
|
2005-08-25 22:33:17 +02:00
|
|
|
time_t published)
|
|
|
|
{
|
2006-06-20 02:48:23 +02:00
|
|
|
cached_dir_t *d, *old_d;
|
2006-01-08 22:26:33 +01:00
|
|
|
smartlist_t *trusted_dirs;
|
2005-08-25 22:33:17 +02:00
|
|
|
if (!cached_v2_networkstatus)
|
2005-10-18 22:12:22 +02:00
|
|
|
cached_v2_networkstatus = digestmap_new();
|
2005-08-25 22:33:17 +02:00
|
|
|
|
2006-06-20 02:48:23 +02:00
|
|
|
old_d = digestmap_get(cached_v2_networkstatus, identity);
|
|
|
|
if (!old_d && !networkstatus)
|
|
|
|
return;
|
2005-08-25 22:33:17 +02:00
|
|
|
|
2006-01-08 22:26:33 +01:00
|
|
|
if (networkstatus) {
|
2006-06-20 02:48:23 +02:00
|
|
|
if (!old_d || published > old_d->published) {
|
|
|
|
d = new_cached_dir(tor_strdup(networkstatus), published);
|
|
|
|
digestmap_set(cached_v2_networkstatus, identity, d);
|
|
|
|
if (old_d)
|
|
|
|
cached_dir_decref(old_d);
|
2006-01-08 22:26:33 +01:00
|
|
|
}
|
2005-09-12 08:56:42 +02:00
|
|
|
} else {
|
2006-06-20 02:48:23 +02:00
|
|
|
if (old_d) {
|
|
|
|
digestmap_remove(cached_v2_networkstatus, identity);
|
|
|
|
cached_dir_decref(old_d);
|
|
|
|
}
|
2005-09-12 08:56:42 +02:00
|
|
|
}
|
2006-01-08 22:26:33 +01:00
|
|
|
|
2006-06-20 02:48:23 +02:00
|
|
|
/* Now purge old entries. */
|
2006-04-10 03:35:56 +02:00
|
|
|
trusted_dirs = router_get_trusted_dir_servers();
|
2006-01-08 22:26:33 +01:00
|
|
|
if (digestmap_size(cached_v2_networkstatus) >
|
|
|
|
smartlist_len(trusted_dirs) + MAX_UNTRUSTED_NETWORKSTATUSES) {
|
|
|
|
/* We need to remove the oldest untrusted networkstatus. */
|
|
|
|
const char *oldest = NULL;
|
|
|
|
time_t oldest_published = TIME_MAX;
|
|
|
|
digestmap_iter_t *iter;
|
|
|
|
|
|
|
|
for (iter = digestmap_iter_init(cached_v2_networkstatus);
|
|
|
|
!digestmap_iter_done(iter);
|
|
|
|
iter = digestmap_iter_next(cached_v2_networkstatus, iter)) {
|
|
|
|
const char *ident;
|
|
|
|
void *val;
|
|
|
|
digestmap_iter_get(iter, &ident, &val);
|
|
|
|
d = val;
|
|
|
|
if (d->published < oldest_published &&
|
2006-09-29 01:57:59 +02:00
|
|
|
!router_digest_is_trusted_dir(ident)) {
|
2006-01-08 22:26:33 +01:00
|
|
|
oldest = ident;
|
|
|
|
oldest_published = d->published;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tor_assert(oldest);
|
|
|
|
d = digestmap_remove(cached_v2_networkstatus, oldest);
|
|
|
|
if (d)
|
2006-06-20 02:48:23 +02:00
|
|
|
cached_dir_decref(d);
|
2006-01-08 22:26:33 +01:00
|
|
|
}
|
2005-09-07 18:42:53 +02:00
|
|
|
}
|
2005-08-25 22:33:17 +02:00
|
|
|
|
2009-10-13 23:06:01 +02:00
|
|
|
/** Replace the v3 consensus networkstatus of type <b>flavor_name</b> that
|
|
|
|
* we're serving with <b>networkstatus</b>, published at <b>published</b>. No
|
|
|
|
* validation is performed. */
|
2007-07-30 01:11:44 +02:00
|
|
|
void
|
2009-10-13 23:06:01 +02:00
|
|
|
dirserv_set_cached_consensus_networkstatus(const char *networkstatus,
|
|
|
|
const char *flavor_name,
|
|
|
|
const digests_t *digests,
|
|
|
|
time_t published)
|
2007-07-30 01:11:44 +02:00
|
|
|
{
|
2009-10-13 23:06:01 +02:00
|
|
|
cached_dir_t *new_networkstatus;
|
|
|
|
cached_dir_t *old_networkstatus;
|
|
|
|
if (!cached_consensuses)
|
|
|
|
cached_consensuses = strmap_new();
|
|
|
|
|
|
|
|
new_networkstatus = new_cached_dir(tor_strdup(networkstatus), published);
|
|
|
|
memcpy(&new_networkstatus->digests, digests, sizeof(digests_t));
|
|
|
|
old_networkstatus = strmap_set(cached_consensuses, flavor_name,
|
|
|
|
new_networkstatus);
|
|
|
|
if (old_networkstatus)
|
|
|
|
cached_dir_decref(old_networkstatus);
|
2007-07-30 01:11:44 +02:00
|
|
|
}
|
|
|
|
|
2007-05-02 11:12:04 +02:00
|
|
|
/** Remove any v2 networkstatus from the directory cache that was published
|
2007-01-10 21:43:40 +01:00
|
|
|
* before <b>cutoff</b>. */
|
|
|
|
void
|
|
|
|
dirserv_clear_old_networkstatuses(time_t cutoff)
|
|
|
|
{
|
2007-01-11 15:13:04 +01:00
|
|
|
if (!cached_v2_networkstatus)
|
|
|
|
return;
|
|
|
|
|
2008-02-05 20:40:17 +01:00
|
|
|
DIGESTMAP_FOREACH_MODIFY(cached_v2_networkstatus, id, cached_dir_t *, dir) {
|
2007-01-10 21:43:40 +01:00
|
|
|
if (dir->published < cutoff) {
|
|
|
|
char *fname;
|
2008-02-05 20:40:17 +01:00
|
|
|
fname = networkstatus_get_cache_filename(id);
|
2007-01-10 21:43:40 +01:00
|
|
|
if (file_status(fname) == FN_FILE) {
|
|
|
|
log_info(LD_DIR, "Removing too-old untrusted networkstatus in %s",
|
|
|
|
fname);
|
|
|
|
unlink(fname);
|
|
|
|
}
|
|
|
|
tor_free(fname);
|
|
|
|
cached_dir_decref(dir);
|
2008-02-05 20:40:17 +01:00
|
|
|
MAP_DEL_CURRENT(id);
|
2007-01-10 21:43:40 +01:00
|
|
|
}
|
2008-02-05 20:40:17 +01:00
|
|
|
} DIGESTMAP_FOREACH_END
|
2007-02-13 00:39:24 +01:00
|
|
|
}
|
2007-01-10 21:43:40 +01:00
|
|
|
|
2007-02-24 02:12:53 +01:00
|
|
|
/** Remove any v1 info from the directory cache that was published
|
|
|
|
* too long ago. */
|
2007-02-13 00:39:24 +01:00
|
|
|
void
|
|
|
|
dirserv_clear_old_v1_info(time_t now)
|
|
|
|
{
|
|
|
|
if (cached_directory &&
|
2007-02-24 02:12:53 +01:00
|
|
|
cached_directory->published < (now - MAX_V1_DIRECTORY_AGE)) {
|
2007-02-13 00:39:24 +01:00
|
|
|
cached_dir_decref(cached_directory);
|
2007-02-25 00:55:36 +01:00
|
|
|
cached_directory = NULL;
|
2007-02-13 00:39:24 +01:00
|
|
|
}
|
|
|
|
if (cached_runningrouters.published < (now - MAX_V1_RR_AGE)) {
|
|
|
|
clear_cached_dir(&cached_runningrouters);
|
|
|
|
}
|
2007-01-10 21:43:40 +01:00
|
|
|
}
|
|
|
|
|
2008-02-16 00:39:08 +01:00
|
|
|
/** Helper: If we're an authority for the right directory version (v1 or v2)
|
2007-05-04 10:04:27 +02:00
|
|
|
* (based on <b>auth_type</b>), try to regenerate
|
2005-09-13 23:14:55 +02:00
|
|
|
* auth_src as appropriate and return it, falling back to cache_src on
|
2007-05-04 10:04:27 +02:00
|
|
|
* failure. If we're a cache, simply return cache_src.
|
2005-09-13 23:14:55 +02:00
|
|
|
*/
|
2005-09-07 18:42:53 +02:00
|
|
|
static cached_dir_t *
|
|
|
|
dirserv_pick_cached_dir_obj(cached_dir_t *cache_src,
|
|
|
|
cached_dir_t *auth_src,
|
2006-06-18 23:30:03 +02:00
|
|
|
time_t dirty, cached_dir_t *(*regenerate)(void),
|
2005-09-07 18:42:53 +02:00
|
|
|
const char *name,
|
2007-05-04 10:04:27 +02:00
|
|
|
authority_type_t auth_type)
|
2005-09-07 18:42:53 +02:00
|
|
|
{
|
2007-05-02 11:12:04 +02:00
|
|
|
or_options_t *options = get_options();
|
2007-05-04 10:04:27 +02:00
|
|
|
int authority = (auth_type == V1_AUTHORITY && authdir_mode_v1(options)) ||
|
|
|
|
(auth_type == V2_AUTHORITY && authdir_mode_v2(options));
|
2005-08-25 22:33:17 +02:00
|
|
|
|
2007-06-09 09:05:19 +02:00
|
|
|
if (!authority || authdir_mode_bridge(options)) {
|
2005-09-07 18:42:53 +02:00
|
|
|
return cache_src;
|
|
|
|
} else {
|
|
|
|
/* We're authoritative. */
|
|
|
|
if (regenerate != NULL) {
|
|
|
|
if (dirty && dirty + DIR_REGEN_SLACK_TIME < time(NULL)) {
|
2006-06-18 23:30:03 +02:00
|
|
|
if (!(auth_src = regenerate())) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_err(LD_BUG, "Couldn't generate %s?", name);
|
2005-09-07 18:42:53 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
} else {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_info(LD_DIRSERV, "The %s is still clean; reusing.", name);
|
2005-09-07 18:42:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return auth_src ? auth_src : cache_src;
|
2005-08-25 22:33:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-02 11:12:04 +02:00
|
|
|
/** Return the most recently generated encoded signed v1 directory,
|
|
|
|
* generating a new one as necessary. If not a v1 authoritative directory
|
|
|
|
* may return NULL if no directory is yet cached. */
|
2006-06-18 09:38:55 +02:00
|
|
|
cached_dir_t *
|
|
|
|
dirserv_get_directory(void)
|
2003-09-27 23:30:10 +02:00
|
|
|
{
|
2006-06-18 09:38:55 +02:00
|
|
|
return dirserv_pick_cached_dir_obj(cached_directory, the_directory,
|
|
|
|
the_directory_is_dirty,
|
|
|
|
dirserv_regenerate_directory,
|
2008-08-05 01:59:38 +02:00
|
|
|
"v1 server directory", V1_AUTHORITY);
|
2004-09-02 20:57:09 +02:00
|
|
|
}
|
|
|
|
|
2007-05-02 11:12:04 +02:00
|
|
|
/** Only called by v1 auth dirservers.
|
|
|
|
* Generate a fresh v1 directory; set the_directory and return a pointer
|
|
|
|
* to the new value.
|
2004-09-02 20:57:09 +02:00
|
|
|
*/
|
2006-06-18 23:30:03 +02:00
|
|
|
static cached_dir_t *
|
2005-06-11 20:52:12 +02:00
|
|
|
dirserv_regenerate_directory(void)
|
2004-09-02 20:57:09 +02:00
|
|
|
{
|
2004-12-24 03:17:32 +01:00
|
|
|
char *new_directory=NULL;
|
2004-09-02 20:57:09 +02:00
|
|
|
|
2007-10-11 15:56:33 +02:00
|
|
|
if (dirserv_dump_directory_to_string(&new_directory, get_identity_key())) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_warn(LD_BUG, "Error creating directory.");
|
2004-09-02 20:57:09 +02:00
|
|
|
tor_free(new_directory);
|
2006-06-18 23:30:03 +02:00
|
|
|
return NULL;
|
2004-09-02 20:57:09 +02:00
|
|
|
}
|
2006-06-18 09:38:55 +02:00
|
|
|
cached_dir_decref(the_directory);
|
|
|
|
the_directory = new_cached_dir(new_directory, time(NULL));
|
2006-02-13 10:37:53 +01:00
|
|
|
log_info(LD_DIRSERV,"New directory (size %d) has been built.",
|
2006-06-18 09:38:55 +02:00
|
|
|
(int)the_directory->dir_len);
|
2006-02-13 10:37:53 +01:00
|
|
|
log_debug(LD_DIRSERV,"New directory (size %d):\n%s",
|
2006-06-18 09:38:55 +02:00
|
|
|
(int)the_directory->dir_len, the_directory->dir);
|
2004-09-02 20:57:09 +02:00
|
|
|
|
|
|
|
the_directory_is_dirty = 0;
|
|
|
|
|
2004-11-15 17:17:59 +01:00
|
|
|
/* Save the directory to disk so we re-load it quickly on startup.
|
|
|
|
*/
|
2006-06-18 09:38:55 +02:00
|
|
|
dirserv_set_cached_directory(the_directory->dir, time(NULL), 0);
|
2004-11-15 17:17:59 +01:00
|
|
|
|
2006-06-18 23:30:03 +02:00
|
|
|
return the_directory;
|
2003-09-27 23:30:10 +02:00
|
|
|
}
|
2003-10-18 05:23:26 +02:00
|
|
|
|
2007-05-02 11:12:04 +02:00
|
|
|
/** Only called by v1 auth dirservers.
|
|
|
|
* Replace the current running-routers list with a newly generated one. */
|
2006-06-18 23:30:03 +02:00
|
|
|
static cached_dir_t *
|
2005-08-25 22:33:17 +02:00
|
|
|
generate_runningrouters(void)
|
2004-06-25 02:29:31 +02:00
|
|
|
{
|
2005-08-25 22:33:17 +02:00
|
|
|
char *s=NULL;
|
2004-06-25 02:29:31 +02:00
|
|
|
char digest[DIGEST_LEN];
|
2005-08-25 22:33:17 +02:00
|
|
|
char published[ISO_TIME_LEN+1];
|
2004-08-04 01:57:05 +02:00
|
|
|
size_t len;
|
2005-08-25 22:33:17 +02:00
|
|
|
crypto_pk_env_t *private_key = get_identity_key();
|
2004-10-07 23:10:40 +02:00
|
|
|
char *identity_pkey; /* Identity key, DER64-encoded. */
|
2005-05-02 23:22:31 +02:00
|
|
|
size_t identity_pkey_len;
|
2005-03-23 20:15:10 +01:00
|
|
|
|
2005-05-02 23:22:31 +02:00
|
|
|
if (crypto_pk_write_public_key_to_string(private_key,&identity_pkey,
|
|
|
|
&identity_pkey_len)<0) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_warn(LD_BUG,"write identity_pkey to string failed!");
|
2004-10-27 14:34:02 +02:00
|
|
|
goto err;
|
2004-10-07 23:10:40 +02:00
|
|
|
}
|
2005-08-25 22:33:17 +02:00
|
|
|
format_iso_time(published, time(NULL));
|
2004-12-24 02:41:52 +01:00
|
|
|
|
2007-10-11 15:56:33 +02:00
|
|
|
len = 2048;
|
2004-12-24 02:41:52 +01:00
|
|
|
s = tor_malloc_zero(len);
|
2005-08-25 22:33:17 +02:00
|
|
|
tor_snprintf(s, len,
|
|
|
|
"network-status\n"
|
|
|
|
"published %s\n"
|
|
|
|
"router-status %s\n"
|
|
|
|
"dir-signing-key\n%s"
|
|
|
|
"directory-signature %s\n",
|
2007-10-11 15:56:33 +02:00
|
|
|
published, "", identity_pkey,
|
2005-12-14 21:40:40 +01:00
|
|
|
get_options()->Nickname);
|
2004-10-07 23:10:40 +02:00
|
|
|
tor_free(identity_pkey);
|
2004-06-25 02:29:31 +02:00
|
|
|
if (router_get_runningrouters_hash(s,digest)) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_warn(LD_BUG,"couldn't compute digest");
|
2004-10-27 14:34:02 +02:00
|
|
|
goto err;
|
2004-06-25 02:29:31 +02:00
|
|
|
}
|
2006-10-31 20:17:07 +01:00
|
|
|
note_crypto_pk_op(SIGN_DIR);
|
2009-09-14 17:57:19 +02:00
|
|
|
if (router_append_dirobj_signature(s, len, digest, DIGEST_LEN,
|
|
|
|
private_key)<0)
|
2004-10-27 14:34:02 +02:00
|
|
|
goto err;
|
2004-06-25 02:29:31 +02:00
|
|
|
|
2005-08-25 22:33:17 +02:00
|
|
|
set_cached_dir(&the_runningrouters, s, time(NULL));
|
2004-06-25 02:29:31 +02:00
|
|
|
runningrouters_is_dirty = 0;
|
2004-11-15 17:17:59 +01:00
|
|
|
|
2006-06-18 23:30:03 +02:00
|
|
|
return &the_runningrouters;
|
2004-10-27 14:34:02 +02:00
|
|
|
err:
|
|
|
|
tor_free(s);
|
2006-06-18 23:30:03 +02:00
|
|
|
return NULL;
|
2004-06-25 02:29:31 +02:00
|
|
|
}
|
|
|
|
|
2004-06-16 23:08:29 +02:00
|
|
|
/** Set *<b>rr</b> to the most recently generated encoded signed
|
2004-08-06 22:00:16 +02:00
|
|
|
* running-routers list, generating a new one as necessary. Return the
|
|
|
|
* size of the directory on success, and 0 on failure. */
|
2007-06-02 17:26:57 +02:00
|
|
|
cached_dir_t *
|
|
|
|
dirserv_get_runningrouters(void)
|
2004-06-16 23:08:29 +02:00
|
|
|
{
|
2007-06-02 17:26:57 +02:00
|
|
|
return dirserv_pick_cached_dir_obj(
|
2005-08-25 22:33:17 +02:00
|
|
|
&cached_runningrouters, &the_runningrouters,
|
|
|
|
runningrouters_is_dirty,
|
|
|
|
generate_runningrouters,
|
2007-05-04 10:04:27 +02:00
|
|
|
"v1 network status list", V1_AUTHORITY);
|
2005-08-25 22:33:17 +02:00
|
|
|
}
|
|
|
|
|
2008-12-22 18:53:04 +01:00
|
|
|
/** Return the latest downloaded consensus networkstatus in encoded, signed,
|
|
|
|
* optionally compressed format, suitable for sending to clients. */
|
2007-07-30 01:11:44 +02:00
|
|
|
cached_dir_t *
|
2009-10-13 23:06:01 +02:00
|
|
|
dirserv_get_consensus(const char *flavor_name)
|
2007-07-30 01:11:44 +02:00
|
|
|
{
|
2010-06-10 16:18:26 +02:00
|
|
|
if (!cached_consensuses)
|
|
|
|
return NULL;
|
2009-10-13 23:06:01 +02:00
|
|
|
return strmap_get(cached_consensuses, flavor_name);
|
2007-07-30 01:11:44 +02:00
|
|
|
}
|
|
|
|
|
2007-02-24 02:12:53 +01:00
|
|
|
/** For authoritative directories: the current (v2) network status. */
|
2006-06-20 02:48:23 +02:00
|
|
|
static cached_dir_t *the_v2_networkstatus = NULL;
|
2005-08-25 22:33:17 +02:00
|
|
|
|
2007-02-16 21:39:37 +01:00
|
|
|
/** Return true iff our opinion of the routers has been stale for long
|
2007-05-02 11:12:04 +02:00
|
|
|
* enough that we should generate a new v2 network status doc. */
|
2005-11-29 18:14:10 +01:00
|
|
|
static int
|
|
|
|
should_generate_v2_networkstatus(void)
|
|
|
|
{
|
2007-05-02 11:12:04 +02:00
|
|
|
return authdir_mode_v2(get_options()) &&
|
2005-11-29 18:14:10 +01:00
|
|
|
the_v2_networkstatus_is_dirty &&
|
|
|
|
the_v2_networkstatus_is_dirty + DIR_REGEN_SLACK_TIME < time(NULL);
|
|
|
|
}
|
|
|
|
|
2007-03-10 06:43:35 +01:00
|
|
|
/** If a router's uptime is at least this value, then it is always
|
|
|
|
* considered stable, regardless of the rest of the network. This
|
|
|
|
* way we resist attacks where an attacker doubles the size of the
|
|
|
|
* network using allegedly high-uptime nodes, displacing all the
|
|
|
|
* current guards. */
|
|
|
|
#define UPTIME_TO_GUARANTEE_STABLE (3600*24*30)
|
2007-09-17 23:59:07 +02:00
|
|
|
/** If a router's MTBF is at least this value, then it is always stable.
|
2007-11-08 17:58:59 +01:00
|
|
|
* See above. (Corresponds to about 7 days for current decay rates.) */
|
|
|
|
#define MTBF_TO_GUARANTEE_STABLE (60*60*24*5)
|
2007-07-22 01:40:55 +02:00
|
|
|
/** Similarly, we protect sufficiently fast nodes from being pushed
|
|
|
|
* out of the set of Fast nodes. */
|
2009-04-12 09:56:58 +02:00
|
|
|
#define BANDWIDTH_TO_GUARANTEE_FAST ROUTER_REQUIRED_MIN_BANDWIDTH
|
2007-07-22 01:40:55 +02:00
|
|
|
/** Similarly, every node with sufficient bandwidth can be considered
|
|
|
|
* for Guard status. */
|
|
|
|
#define BANDWIDTH_TO_GUARANTEE_GUARD (250*1024)
|
2007-11-08 17:58:59 +01:00
|
|
|
/** Similarly, every node with at least this much weighted time known can be
|
|
|
|
* considered familiar enough to be a guard. Corresponds to about 20 days for
|
|
|
|
* current decay rates.
|
|
|
|
*/
|
|
|
|
#define TIME_KNOWN_TO_GUARANTEE_FAMILIAR (8*24*60*60)
|
|
|
|
/** Similarly, every node with sufficient WFU is around enough to be a guard.
|
|
|
|
*/
|
2010-07-12 02:51:44 +02:00
|
|
|
#define WFU_TO_GUARANTEE_GUARD (0.98)
|
2007-03-10 06:43:35 +01:00
|
|
|
|
2007-01-23 20:22:52 +01:00
|
|
|
/* Thresholds for server performance: set by
|
|
|
|
* dirserv_compute_performance_thresholds, and used by
|
|
|
|
* generate_v2_networkstatus */
|
2008-12-23 18:56:31 +01:00
|
|
|
|
2008-12-26 18:35:08 +01:00
|
|
|
/** Any router with an uptime of at least this value is stable. */
|
2006-03-20 22:41:12 +01:00
|
|
|
static uint32_t stable_uptime = 0; /* start at a safe value */
|
2008-12-26 18:35:08 +01:00
|
|
|
/** Any router with an mtbf of at least this value is stable. */
|
2007-08-21 07:37:24 +02:00
|
|
|
static double stable_mtbf = 0.0;
|
2008-12-26 18:35:08 +01:00
|
|
|
/** If true, we have measured enough mtbf info to look at stable_mtbf rather
|
|
|
|
* than stable_uptime. */
|
2007-08-21 07:37:24 +02:00
|
|
|
static int enough_mtbf_info = 0;
|
2008-12-26 18:35:08 +01:00
|
|
|
/** Any router with a weighted fractional uptime of at least this much might
|
|
|
|
* be good as a guard. */
|
2007-10-27 00:50:42 +02:00
|
|
|
static double guard_wfu = 0.0;
|
2008-12-26 18:35:08 +01:00
|
|
|
/** Don't call a router a guard unless we've known about it for at least this
|
|
|
|
* many seconds. */
|
2007-11-08 17:58:59 +01:00
|
|
|
static long guard_tk = 0;
|
2008-12-26 18:35:08 +01:00
|
|
|
/** Any router with a bandwidth at least this high is "Fast" */
|
2006-03-20 22:41:12 +01:00
|
|
|
static uint32_t fast_bandwidth = 0;
|
2008-12-26 18:35:08 +01:00
|
|
|
/** If exits can be guards, then all guards must have a bandwidth this
|
|
|
|
* high. */
|
2007-01-26 10:03:20 +01:00
|
|
|
static uint32_t guard_bandwidth_including_exits = 0;
|
2008-12-26 18:35:08 +01:00
|
|
|
/** If exits can't be guards, then all guards must have a bandwidth this
|
|
|
|
* high. */
|
2007-01-26 10:03:20 +01:00
|
|
|
static uint32_t guard_bandwidth_excluding_exits = 0;
|
2008-12-26 18:35:08 +01:00
|
|
|
/** Total bandwidth of all the routers we're considering. */
|
2006-12-19 20:48:48 +01:00
|
|
|
static uint64_t total_bandwidth = 0;
|
2008-12-26 18:35:08 +01:00
|
|
|
/** Total bandwidth of all the exit routers we're considering. */
|
2006-12-19 20:48:48 +01:00
|
|
|
static uint64_t total_exit_bandwidth = 0;
|
2006-01-02 04:32:55 +01:00
|
|
|
|
2007-01-23 20:22:52 +01:00
|
|
|
/** Helper: estimate the uptime of a router given its stated uptime and the
|
|
|
|
* amount of time since it last stated its stated uptime. */
|
2008-02-22 20:09:45 +01:00
|
|
|
static INLINE long
|
2006-04-08 07:43:52 +02:00
|
|
|
real_uptime(routerinfo_t *router, time_t now)
|
|
|
|
{
|
2006-07-14 05:14:02 +02:00
|
|
|
if (now < router->cache_info.published_on)
|
|
|
|
return router->uptime;
|
|
|
|
else
|
|
|
|
return router->uptime + (now - router->cache_info.published_on);
|
2006-04-08 07:43:52 +02:00
|
|
|
}
|
|
|
|
|
2005-12-15 22:30:57 +01:00
|
|
|
/** Return 1 if <b>router</b> is not suitable for these parameters, else 0.
|
|
|
|
* If <b>need_uptime</b> is non-zero, we require a minimum uptime.
|
|
|
|
* If <b>need_capacity</b> is non-zero, we require a minimum advertised
|
|
|
|
* bandwidth.
|
|
|
|
*/
|
|
|
|
static int
|
2006-04-03 01:01:01 +02:00
|
|
|
dirserv_thinks_router_is_unreliable(time_t now,
|
|
|
|
routerinfo_t *router,
|
2005-12-15 22:30:57 +01:00
|
|
|
int need_uptime, int need_capacity)
|
|
|
|
{
|
2007-03-10 06:43:35 +01:00
|
|
|
if (need_uptime) {
|
2007-08-21 07:37:24 +02:00
|
|
|
if (!enough_mtbf_info) {
|
2008-12-29 21:17:18 +01:00
|
|
|
/* XXX022 Once most authorities are on v3, we should change the rule from
|
2007-08-21 07:37:24 +02:00
|
|
|
* "use uptime if we don't have mtbf data" to "don't advertise Stable on
|
|
|
|
* v3 if we don't have enough mtbf data." */
|
2008-02-22 20:09:45 +01:00
|
|
|
long uptime = real_uptime(router, now);
|
2007-08-21 07:37:24 +02:00
|
|
|
if ((unsigned)uptime < stable_uptime &&
|
|
|
|
(unsigned)uptime < UPTIME_TO_GUARANTEE_STABLE)
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
double mtbf =
|
|
|
|
rep_hist_get_stability(router->cache_info.identity_digest, now);
|
2008-12-29 21:17:18 +01:00
|
|
|
if (mtbf < stable_mtbf &&
|
|
|
|
mtbf < MTBF_TO_GUARANTEE_STABLE)
|
2007-08-21 07:37:24 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2007-03-10 06:43:35 +01:00
|
|
|
}
|
2007-07-22 01:40:55 +02:00
|
|
|
if (need_capacity) {
|
|
|
|
uint32_t bw = router_get_advertised_bandwidth(router);
|
2007-11-08 17:58:59 +01:00
|
|
|
if (bw < fast_bandwidth)
|
2007-07-22 01:40:55 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2005-12-15 22:30:57 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-11-17 13:15:32 +01:00
|
|
|
/** Return true iff <b>router</b> should be assigned the "HSDir" flag.
|
|
|
|
* Right now this means it advertises support for it, it has a high
|
2010-07-14 19:20:57 +02:00
|
|
|
* uptime, it has a DirPort open, and it's currently considered Running.
|
2007-11-17 13:15:32 +01:00
|
|
|
*
|
2007-12-18 22:37:58 +01:00
|
|
|
* This function needs to be called after router-\>is_running has
|
2007-11-17 13:15:32 +01:00
|
|
|
* been set.
|
2007-10-29 20:10:42 +01:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
dirserv_thinks_router_is_hs_dir(routerinfo_t *router, time_t now)
|
|
|
|
{
|
2008-02-22 20:09:45 +01:00
|
|
|
long uptime = real_uptime(router, now);
|
2007-10-29 20:10:42 +01:00
|
|
|
|
2010-07-14 19:20:57 +02:00
|
|
|
/* XXX We shouldn't need to check dir_port, but we do because of
|
|
|
|
* bug 1693. In the future, once relays set wants_to_be_hs_dir
|
|
|
|
* correctly, we can revert to only checking dir_port if router's
|
|
|
|
* version is too old. */
|
|
|
|
return (router->wants_to_be_hs_dir && router->dir_port &&
|
2007-11-29 16:25:04 +01:00
|
|
|
uptime > get_options()->MinUptimeHidServDirectoryV2 &&
|
2007-11-17 13:15:32 +01:00
|
|
|
router->is_running);
|
2007-10-29 20:10:42 +01:00
|
|
|
}
|
|
|
|
|
2008-02-21 04:38:46 +01:00
|
|
|
/** Look through the routerlist, the Mean Time Between Failure history, and
|
|
|
|
* the Weighted Fractional Uptime history, and use them to set thresholds for
|
|
|
|
* the Stable, Fast, and Guard flags. Update the fields stable_uptime,
|
|
|
|
* stable_mtbf, enough_mtbf_info, guard_wfu, guard_tk, fast_bandwidth,
|
|
|
|
* guard_bandwidh_including_exits, guard_bandwidth_excluding_exits,
|
|
|
|
* total_bandwidth, and total_exit_bandwidth.
|
|
|
|
*
|
|
|
|
* Also, set the is_exit flag of each router appropriately. */
|
2006-01-11 04:59:55 +01:00
|
|
|
static void
|
2006-03-20 22:41:12 +01:00
|
|
|
dirserv_compute_performance_thresholds(routerlist_t *rl)
|
2006-01-02 04:32:55 +01:00
|
|
|
{
|
2007-11-08 17:58:59 +01:00
|
|
|
int n_active, n_active_nonexit, n_familiar;
|
2007-09-17 20:27:49 +02:00
|
|
|
uint32_t *uptimes, *bandwidths, *bandwidths_excluding_exits;
|
2007-11-08 17:58:59 +01:00
|
|
|
long *tks;
|
2007-10-27 00:50:42 +02:00
|
|
|
double *mtbfs, *wfus;
|
2006-04-03 01:01:01 +02:00
|
|
|
time_t now = time(NULL);
|
2006-03-20 22:41:12 +01:00
|
|
|
|
2007-01-26 10:03:20 +01:00
|
|
|
/* initialize these all here, in case there are no routers */
|
|
|
|
stable_uptime = 0;
|
2007-11-08 17:58:59 +01:00
|
|
|
stable_mtbf = 0;
|
2007-01-26 10:03:20 +01:00
|
|
|
fast_bandwidth = 0;
|
|
|
|
guard_bandwidth_including_exits = 0;
|
|
|
|
guard_bandwidth_excluding_exits = 0;
|
2007-11-08 17:58:59 +01:00
|
|
|
guard_tk = 0;
|
|
|
|
guard_wfu = 0;
|
2006-12-19 20:48:48 +01:00
|
|
|
total_bandwidth = 0;
|
|
|
|
total_exit_bandwidth = 0;
|
|
|
|
|
2008-02-21 04:38:46 +01:00
|
|
|
/* Initialize arrays that will hold values for each router. We'll
|
|
|
|
* sort them and use that to compute thresholds. */
|
2007-09-17 20:27:49 +02:00
|
|
|
n_active = n_active_nonexit = 0;
|
2008-02-21 04:38:46 +01:00
|
|
|
/* Uptime for every active router. */
|
2007-09-17 20:27:49 +02:00
|
|
|
uptimes = tor_malloc(sizeof(uint32_t)*smartlist_len(rl->routers));
|
2008-02-21 04:38:46 +01:00
|
|
|
/* Bandwidth for every active router. */
|
2007-09-17 20:27:49 +02:00
|
|
|
bandwidths = tor_malloc(sizeof(uint32_t)*smartlist_len(rl->routers));
|
2008-02-21 04:38:46 +01:00
|
|
|
/* Bandwidth for every active non-exit router. */
|
2007-09-17 20:27:49 +02:00
|
|
|
bandwidths_excluding_exits =
|
|
|
|
tor_malloc(sizeof(uint32_t)*smartlist_len(rl->routers));
|
2008-02-21 04:38:46 +01:00
|
|
|
/* Weighted mean time between failure for each active router. */
|
2007-09-17 20:27:49 +02:00
|
|
|
mtbfs = tor_malloc(sizeof(double)*smartlist_len(rl->routers));
|
2008-02-21 04:38:46 +01:00
|
|
|
/* Time-known for each active router. */
|
2007-11-08 17:58:59 +01:00
|
|
|
tks = tor_malloc(sizeof(long)*smartlist_len(rl->routers));
|
2008-02-21 04:38:46 +01:00
|
|
|
/* Weighted fractional uptime for each active router. */
|
2007-10-27 00:50:42 +02:00
|
|
|
wfus = tor_malloc(sizeof(double)*smartlist_len(rl->routers));
|
2006-01-02 04:32:55 +01:00
|
|
|
|
2008-02-21 04:38:46 +01:00
|
|
|
/* Now, fill in the arrays. */
|
2006-01-02 04:32:55 +01:00
|
|
|
SMARTLIST_FOREACH(rl->routers, routerinfo_t *, ri, {
|
2007-02-28 22:06:05 +01:00
|
|
|
if (router_is_active(ri, now)) {
|
2007-09-17 20:27:49 +02:00
|
|
|
const char *id = ri->cache_info.identity_digest;
|
|
|
|
uint32_t bw;
|
2010-02-03 23:54:18 +01:00
|
|
|
ri->is_exit = (!router_exit_policy_rejects_all(ri) &&
|
|
|
|
exit_policy_is_general_exit(ri->exit_policy));
|
2008-02-22 20:09:45 +01:00
|
|
|
uptimes[n_active] = (uint32_t)real_uptime(ri, now);
|
2007-09-17 20:27:49 +02:00
|
|
|
mtbfs[n_active] = rep_hist_get_stability(id, now);
|
2007-11-08 17:58:59 +01:00
|
|
|
tks [n_active] = rep_hist_get_weighted_time_known(id, now);
|
2007-09-17 20:27:49 +02:00
|
|
|
bandwidths[n_active] = bw = router_get_advertised_bandwidth(ri);
|
|
|
|
total_bandwidth += bw;
|
2007-01-26 10:03:20 +01:00
|
|
|
if (ri->is_exit && !ri->is_bad_exit) {
|
2007-09-17 20:27:49 +02:00
|
|
|
total_exit_bandwidth += bw;
|
2007-01-26 10:03:20 +01:00
|
|
|
} else {
|
2007-09-17 20:27:49 +02:00
|
|
|
bandwidths_excluding_exits[n_active_nonexit] = bw;
|
|
|
|
++n_active_nonexit;
|
2007-01-26 10:03:20 +01:00
|
|
|
}
|
2007-09-17 20:27:49 +02:00
|
|
|
++n_active;
|
2006-01-02 04:32:55 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2008-02-21 04:38:46 +01:00
|
|
|
/* Now, compute thresholds. */
|
2007-09-17 20:27:49 +02:00
|
|
|
if (n_active) {
|
2008-02-21 04:38:46 +01:00
|
|
|
/* The median uptime is stable. */
|
2007-09-17 20:27:49 +02:00
|
|
|
stable_uptime = median_uint32(uptimes, n_active);
|
2008-02-21 04:38:46 +01:00
|
|
|
/* The median mtbf is stable, if we have enough mtbf info */
|
2007-09-17 20:27:49 +02:00
|
|
|
stable_mtbf = median_double(mtbfs, n_active);
|
2008-02-21 04:38:46 +01:00
|
|
|
/* The 12.5th percentile bandwidth is fast. */
|
2007-09-17 20:27:49 +02:00
|
|
|
fast_bandwidth = find_nth_uint32(bandwidths, n_active, n_active/8);
|
2008-02-21 04:38:46 +01:00
|
|
|
/* (Now bandwidths is sorted.) */
|
2009-04-12 09:56:58 +02:00
|
|
|
if (fast_bandwidth < ROUTER_REQUIRED_MIN_BANDWIDTH/2)
|
2007-09-17 20:27:49 +02:00
|
|
|
fast_bandwidth = bandwidths[n_active/4];
|
|
|
|
guard_bandwidth_including_exits = bandwidths[(n_active-1)/2];
|
2007-11-08 17:58:59 +01:00
|
|
|
guard_tk = find_nth_long(tks, n_active, n_active/8);
|
2007-01-26 10:03:20 +01:00
|
|
|
}
|
|
|
|
|
2007-11-08 17:58:59 +01:00
|
|
|
if (guard_tk > TIME_KNOWN_TO_GUARANTEE_FAMILIAR)
|
|
|
|
guard_tk = TIME_KNOWN_TO_GUARANTEE_FAMILIAR;
|
|
|
|
|
|
|
|
if (fast_bandwidth > BANDWIDTH_TO_GUARANTEE_FAST)
|
|
|
|
fast_bandwidth = BANDWIDTH_TO_GUARANTEE_FAST;
|
|
|
|
|
2008-02-21 04:38:46 +01:00
|
|
|
/* Now that we have a time-known that 7/8 routers are known longer than,
|
|
|
|
* fill wfus with the wfu of every such "familiar" router. */
|
2007-11-08 17:58:59 +01:00
|
|
|
n_familiar = 0;
|
|
|
|
SMARTLIST_FOREACH(rl->routers, routerinfo_t *, ri, {
|
|
|
|
if (router_is_active(ri, now)) {
|
|
|
|
const char *id = ri->cache_info.identity_digest;
|
|
|
|
long tk = rep_hist_get_weighted_time_known(id, now);
|
|
|
|
if (tk < guard_tk)
|
|
|
|
continue;
|
|
|
|
wfus[n_familiar++] = rep_hist_get_weighted_fractional_uptime(id, now);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (n_familiar)
|
|
|
|
guard_wfu = median_double(wfus, n_familiar);
|
|
|
|
if (guard_wfu > WFU_TO_GUARANTEE_GUARD)
|
|
|
|
guard_wfu = WFU_TO_GUARANTEE_GUARD;
|
|
|
|
|
2007-09-17 20:27:49 +02:00
|
|
|
enough_mtbf_info = rep_hist_have_measured_enough_stability();
|
|
|
|
|
|
|
|
if (n_active_nonexit) {
|
2007-01-26 10:03:20 +01:00
|
|
|
guard_bandwidth_excluding_exits =
|
2007-09-17 20:27:49 +02:00
|
|
|
median_uint32(bandwidths_excluding_exits, n_active_nonexit);
|
2006-03-20 22:49:55 +01:00
|
|
|
}
|
2006-01-02 04:32:55 +01:00
|
|
|
|
2006-09-29 02:19:43 +02:00
|
|
|
log(LOG_INFO, LD_DIRSERV,
|
2007-11-08 17:58:59 +01:00
|
|
|
"Cutoffs: For Stable, %lu sec uptime, %lu sec MTBF. "
|
2007-12-03 23:04:19 +01:00
|
|
|
"For Fast: %lu bytes/sec. "
|
2007-11-08 17:58:59 +01:00
|
|
|
"For Guard: WFU %.03lf%%, time-known %lu sec, "
|
2008-11-07 02:48:55 +01:00
|
|
|
"and bandwidth %lu or %lu bytes/sec. We%s have enough stability data.",
|
2006-09-29 02:19:43 +02:00
|
|
|
(unsigned long)stable_uptime,
|
2007-11-08 17:58:59 +01:00
|
|
|
(unsigned long)stable_mtbf,
|
2006-09-29 02:19:43 +02:00
|
|
|
(unsigned long)fast_bandwidth,
|
2007-11-08 17:58:59 +01:00
|
|
|
guard_wfu*100,
|
|
|
|
(unsigned long)guard_tk,
|
2007-01-26 10:03:20 +01:00
|
|
|
(unsigned long)guard_bandwidth_including_exits,
|
2008-09-29 16:40:34 +02:00
|
|
|
(unsigned long)guard_bandwidth_excluding_exits,
|
2008-11-07 04:39:19 +01:00
|
|
|
enough_mtbf_info ? "" : " don't ");
|
2006-01-02 04:32:55 +01:00
|
|
|
|
2007-09-17 20:27:49 +02:00
|
|
|
tor_free(uptimes);
|
|
|
|
tor_free(mtbfs);
|
|
|
|
tor_free(bandwidths);
|
|
|
|
tor_free(bandwidths_excluding_exits);
|
2007-11-08 17:58:59 +01:00
|
|
|
tor_free(tks);
|
|
|
|
tor_free(wfus);
|
2006-01-02 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2007-06-17 17:10:43 +02:00
|
|
|
/** Given a platform string as in a routerinfo_t (possibly null), return a
|
|
|
|
* newly allocated version string for a networkstatus document, or NULL if the
|
|
|
|
* platform doesn't give a Tor version. */
|
2007-06-08 20:41:09 +02:00
|
|
|
static char *
|
|
|
|
version_from_platform(const char *platform)
|
|
|
|
{
|
|
|
|
if (platform && !strcmpstart(platform, "Tor ")) {
|
|
|
|
const char *eos = find_whitespace(platform+4);
|
|
|
|
if (eos && !strcmpstart(eos, " (r")) {
|
2009-01-04 20:47:21 +01:00
|
|
|
/* XXXX Unify this logic with the other version extraction
|
|
|
|
* logic in routerparse.c. */
|
2007-06-08 20:41:09 +02:00
|
|
|
eos = find_whitespace(eos+1);
|
|
|
|
}
|
|
|
|
if (eos) {
|
|
|
|
return tor_strndup(platform, eos-platform);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/** Helper: write the router-status information in <b>rs</b> into <b>buf</b>,
|
|
|
|
* which has at least <b>buf_len</b> free characters. Do NUL-termination.
|
2007-10-04 18:21:58 +02:00
|
|
|
* Use the same format as in network-status documents. If <b>version</b> is
|
2007-05-29 19:31:13 +02:00
|
|
|
* non-NULL, add a "v" line for the platform. Return 0 on success, -1 on
|
2009-07-31 06:33:53 +02:00
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
* The format argument has three possible values:
|
|
|
|
* NS_V2 - Output an entry suitable for a V2 NS opinion document
|
|
|
|
* NS_V3_CONSENSUS - Output the first portion of a V3 NS consensus entry
|
2009-10-15 22:06:00 +02:00
|
|
|
* NS_V3_CONSENSUS_MICRODESC - Output the first portion of a V3 microdesc
|
|
|
|
* consensus entry.
|
2009-07-31 06:33:53 +02:00
|
|
|
* NS_V3_VOTE - Output a complete V3 NS vote
|
2009-09-03 03:01:23 +02:00
|
|
|
* NS_CONTROL_PORT - Output a NS document for the control port
|
2007-10-04 18:21:58 +02:00
|
|
|
*/
|
2007-05-22 19:58:30 +02:00
|
|
|
int
|
|
|
|
routerstatus_format_entry(char *buf, size_t buf_len,
|
2007-06-08 20:41:14 +02:00
|
|
|
routerstatus_t *rs, const char *version,
|
2009-07-31 06:33:53 +02:00
|
|
|
routerstatus_format_type_t format)
|
2007-05-22 19:58:30 +02:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
struct in_addr in;
|
2007-06-04 21:19:01 +02:00
|
|
|
char *cp;
|
2008-08-14 14:37:00 +02:00
|
|
|
char *summary;
|
2007-05-22 19:58:30 +02:00
|
|
|
|
|
|
|
char published[ISO_TIME_LEN+1];
|
|
|
|
char ipaddr[INET_NTOA_BUF_LEN];
|
|
|
|
char identity64[BASE64_DIGEST_LEN+1];
|
|
|
|
char digest64[BASE64_DIGEST_LEN+1];
|
|
|
|
|
|
|
|
format_iso_time(published, rs->published_on);
|
|
|
|
digest_to_base64(identity64, rs->identity_digest);
|
|
|
|
digest_to_base64(digest64, rs->descriptor_digest);
|
|
|
|
in.s_addr = htonl(rs->addr);
|
|
|
|
tor_inet_ntoa(&in, ipaddr, sizeof(ipaddr));
|
|
|
|
|
|
|
|
r = tor_snprintf(buf, buf_len,
|
2009-08-24 18:51:33 +02:00
|
|
|
"r %s %s %s%s%s %s %d %d\n",
|
2007-05-22 19:58:30 +02:00
|
|
|
rs->nickname,
|
|
|
|
identity64,
|
2009-08-24 18:51:33 +02:00
|
|
|
(format==NS_V3_CONSENSUS_MICRODESC)?"":digest64,
|
|
|
|
(format==NS_V3_CONSENSUS_MICRODESC)?"":" ",
|
2007-05-22 19:58:30 +02:00
|
|
|
published,
|
|
|
|
ipaddr,
|
|
|
|
(int)rs->or_port,
|
2007-06-04 21:19:01 +02:00
|
|
|
(int)rs->dir_port);
|
|
|
|
if (r<0) {
|
|
|
|
log_warn(LD_BUG, "Not enough space in buffer.");
|
|
|
|
return -1;
|
|
|
|
}
|
2009-07-31 06:33:53 +02:00
|
|
|
|
|
|
|
/* TODO: Maybe we want to pass in what we need to build the rest of
|
|
|
|
* this here, instead of in the caller. Then we could use the
|
|
|
|
* networkstatus_type_t values, with an additional control port value
|
|
|
|
* added -MP */
|
2009-08-24 18:51:33 +02:00
|
|
|
if (format == NS_V3_CONSENSUS || format == NS_V3_CONSENSUS_MICRODESC)
|
2007-06-04 21:19:01 +02:00
|
|
|
return 0;
|
2008-08-14 14:37:34 +02:00
|
|
|
|
2007-06-04 21:19:01 +02:00
|
|
|
cp = buf + strlen(buf);
|
2008-02-05 22:39:36 +01:00
|
|
|
/* NOTE: Whenever this list expands, be sure to increase MAX_FLAG_LINE_LEN*/
|
2007-06-04 21:19:01 +02:00
|
|
|
r = tor_snprintf(cp, buf_len - (cp-buf),
|
2008-08-14 14:37:07 +02:00
|
|
|
"s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
|
2007-06-04 21:19:01 +02:00
|
|
|
/* These must stay in alphabetical order. */
|
2007-06-13 21:06:23 +02:00
|
|
|
rs->is_authority?" Authority":"",
|
2007-12-10 17:49:54 +01:00
|
|
|
rs->is_bad_directory?" BadDirectory":"",
|
2007-05-22 19:58:30 +02:00
|
|
|
rs->is_bad_exit?" BadExit":"",
|
|
|
|
rs->is_exit?" Exit":"",
|
|
|
|
rs->is_fast?" Fast":"",
|
|
|
|
rs->is_possible_guard?" Guard":"",
|
2007-10-29 20:10:42 +01:00
|
|
|
rs->is_hs_dir?" HSDir":"",
|
2007-05-22 19:58:30 +02:00
|
|
|
rs->is_named?" Named":"",
|
|
|
|
rs->is_running?" Running":"",
|
2007-10-19 20:56:26 +02:00
|
|
|
rs->is_stable?" Stable":"",
|
|
|
|
rs->is_unnamed?" Unnamed":"",
|
2007-06-13 20:29:16 +02:00
|
|
|
rs->is_v2_dir?" V2Dir":"",
|
2008-08-14 14:37:07 +02:00
|
|
|
rs->is_valid?" Valid":"");
|
2007-05-22 19:58:30 +02:00
|
|
|
if (r<0) {
|
|
|
|
log_warn(LD_BUG, "Not enough space in buffer.");
|
|
|
|
return -1;
|
|
|
|
}
|
2007-06-10 21:13:40 +02:00
|
|
|
cp += strlen(cp);
|
2007-06-08 20:41:09 +02:00
|
|
|
|
2008-02-05 22:39:36 +01:00
|
|
|
/* length of "opt v \n" */
|
|
|
|
#define V_LINE_OVERHEAD 7
|
|
|
|
if (version && strlen(version) < MAX_V_LINE_LEN - V_LINE_OVERHEAD) {
|
2007-06-10 21:13:40 +02:00
|
|
|
if (tor_snprintf(cp, buf_len - (cp-buf), "opt v %s\n", version)<0) {
|
2007-06-08 20:41:09 +02:00
|
|
|
log_warn(LD_BUG, "Unable to print router version.");
|
|
|
|
return -1;
|
2007-05-22 19:58:30 +02:00
|
|
|
}
|
2008-08-14 14:37:28 +02:00
|
|
|
cp += strlen(cp);
|
2007-05-22 19:58:30 +02:00
|
|
|
}
|
|
|
|
|
2009-07-31 06:33:53 +02:00
|
|
|
if (format != NS_V2) {
|
2008-08-14 14:37:34 +02:00
|
|
|
routerinfo_t* desc = router_get_by_digest(rs->identity_digest);
|
2009-08-10 03:21:15 +02:00
|
|
|
uint32_t bw;
|
2008-08-14 14:37:34 +02:00
|
|
|
|
2009-07-31 06:33:53 +02:00
|
|
|
if (format != NS_CONTROL_PORT) {
|
|
|
|
/* Blow up more or less nicely if we didn't get anything or not the
|
|
|
|
* thing we expected.
|
|
|
|
*/
|
|
|
|
if (!desc) {
|
|
|
|
char id[HEX_DIGEST_LEN+1];
|
|
|
|
char dd[HEX_DIGEST_LEN+1];
|
|
|
|
|
|
|
|
base16_encode(id, sizeof(id), rs->identity_digest, DIGEST_LEN);
|
|
|
|
base16_encode(dd, sizeof(dd), rs->descriptor_digest, DIGEST_LEN);
|
|
|
|
log_warn(LD_BUG, "Cannot get any descriptor for %s "
|
|
|
|
"(wanted descriptor %s).",
|
|
|
|
id, dd);
|
|
|
|
return -1;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* This assert can fire for the control port, because
|
|
|
|
* it can request NS documents before all descriptors
|
|
|
|
* have been fetched. */
|
|
|
|
if (memcmp(desc->cache_info.signed_descriptor_digest,
|
|
|
|
rs->descriptor_digest,
|
|
|
|
DIGEST_LEN)) {
|
|
|
|
char rl_d[HEX_DIGEST_LEN+1];
|
|
|
|
char rs_d[HEX_DIGEST_LEN+1];
|
|
|
|
char id[HEX_DIGEST_LEN+1];
|
|
|
|
|
|
|
|
base16_encode(rl_d, sizeof(rl_d),
|
|
|
|
desc->cache_info.signed_descriptor_digest, DIGEST_LEN);
|
|
|
|
base16_encode(rs_d, sizeof(rs_d), rs->descriptor_digest, DIGEST_LEN);
|
|
|
|
base16_encode(id, sizeof(id), rs->identity_digest, DIGEST_LEN);
|
|
|
|
log_err(LD_BUG, "descriptor digest in routerlist does not match "
|
|
|
|
"the one in routerstatus: %s vs %s "
|
|
|
|
"(router %s)\n",
|
|
|
|
rl_d, rs_d, id);
|
|
|
|
|
|
|
|
tor_assert(!memcmp(desc->cache_info.signed_descriptor_digest,
|
|
|
|
rs->descriptor_digest,
|
|
|
|
DIGEST_LEN));
|
|
|
|
};
|
|
|
|
}
|
2008-08-14 14:37:34 +02:00
|
|
|
|
2009-06-27 09:08:18 +02:00
|
|
|
if (format == NS_CONTROL_PORT && rs->has_bandwidth) {
|
|
|
|
bw = rs->bandwidth;
|
|
|
|
} else {
|
|
|
|
tor_assert(desc);
|
|
|
|
bw = router_get_advertised_bandwidth_capped(desc) / 1000;
|
|
|
|
}
|
2008-08-14 14:37:07 +02:00
|
|
|
r = tor_snprintf(cp, buf_len - (cp-buf),
|
2009-06-27 09:08:18 +02:00
|
|
|
"w Bandwidth=%d\n", bw);
|
2009-07-31 06:33:53 +02:00
|
|
|
|
2008-08-14 14:37:00 +02:00
|
|
|
if (r<0) {
|
|
|
|
log_warn(LD_BUG, "Not enough space in buffer.");
|
|
|
|
return -1;
|
|
|
|
}
|
2008-08-14 14:37:28 +02:00
|
|
|
cp += strlen(cp);
|
2009-07-31 06:33:53 +02:00
|
|
|
if (format == NS_V3_VOTE && rs->has_measured_bw) {
|
|
|
|
*--cp = '\0'; /* Kill "\n" */
|
|
|
|
r = tor_snprintf(cp, buf_len - (cp-buf),
|
|
|
|
" Measured=%d\n", rs->measured_bw);
|
|
|
|
if (r<0) {
|
|
|
|
log_warn(LD_BUG, "Not enough space in buffer for weight line.");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cp += strlen(cp);
|
|
|
|
}
|
2008-08-14 14:37:07 +02:00
|
|
|
|
2009-07-31 06:33:53 +02:00
|
|
|
if (desc) {
|
|
|
|
summary = policy_summarize(desc->exit_policy);
|
|
|
|
r = tor_snprintf(cp, buf_len - (cp-buf), "p %s\n", summary);
|
|
|
|
if (r<0) {
|
|
|
|
log_warn(LD_BUG, "Not enough space in buffer.");
|
|
|
|
tor_free(summary);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cp += strlen(cp);
|
2008-09-05 22:52:15 +02:00
|
|
|
tor_free(summary);
|
2008-08-14 14:37:07 +02:00
|
|
|
}
|
2008-08-14 14:37:00 +02:00
|
|
|
}
|
|
|
|
|
2007-05-22 19:58:30 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-08-22 17:07:44 +02:00
|
|
|
/** Helper for sorting: compares two routerinfos first by address, and then by
|
|
|
|
* descending order of "usefulness". (An authority is more useful than a
|
|
|
|
* non-authority; a running router is more useful than a non-running router;
|
|
|
|
* and a router with more bandwidth is more useful than one with less.)
|
2007-07-29 07:20:31 +02:00
|
|
|
**/
|
|
|
|
static int
|
|
|
|
_compare_routerinfo_by_ip_and_bw(const void **a, const void **b)
|
|
|
|
{
|
|
|
|
routerinfo_t *first = *(routerinfo_t **)a, *second = *(routerinfo_t **)b;
|
2007-07-29 07:32:03 +02:00
|
|
|
int first_is_auth, second_is_auth;
|
2007-07-29 07:56:30 +02:00
|
|
|
uint32_t bw_first, bw_second;
|
2007-07-29 07:32:03 +02:00
|
|
|
|
2007-07-29 07:20:31 +02:00
|
|
|
/* we return -1 if first should appear before second... that is,
|
|
|
|
* if first is a better router. */
|
|
|
|
if (first->addr < second->addr)
|
|
|
|
return -1;
|
|
|
|
else if (first->addr > second->addr)
|
|
|
|
return 1;
|
2007-07-29 07:32:03 +02:00
|
|
|
|
2007-08-22 17:07:44 +02:00
|
|
|
/* Potentially, this next bit could cause k n lg n memcmp calls. But in
|
|
|
|
* reality, we will almost never get here, since addresses will usually be
|
|
|
|
* different. */
|
2007-07-29 15:18:52 +02:00
|
|
|
|
2007-07-29 07:32:03 +02:00
|
|
|
first_is_auth =
|
|
|
|
router_digest_is_trusted_dir(first->cache_info.identity_digest);
|
|
|
|
second_is_auth =
|
|
|
|
router_digest_is_trusted_dir(second->cache_info.identity_digest);
|
|
|
|
|
|
|
|
if (first_is_auth && !second_is_auth)
|
|
|
|
return -1;
|
|
|
|
else if (!first_is_auth && second_is_auth)
|
|
|
|
return 1;
|
|
|
|
|
2007-07-29 07:20:31 +02:00
|
|
|
else if (first->is_running && !second->is_running)
|
|
|
|
return -1;
|
|
|
|
else if (!first->is_running && second->is_running)
|
|
|
|
return 1;
|
2007-07-29 07:32:03 +02:00
|
|
|
|
2007-07-29 07:56:30 +02:00
|
|
|
bw_first = router_get_advertised_bandwidth(first);
|
|
|
|
bw_second = router_get_advertised_bandwidth(second);
|
|
|
|
|
|
|
|
if (bw_first > bw_second)
|
|
|
|
return -1;
|
|
|
|
else if (bw_first < bw_second)
|
2007-07-29 07:20:31 +02:00
|
|
|
return 1;
|
2007-07-29 07:56:30 +02:00
|
|
|
|
|
|
|
/* They're equal! Compare by identity digest, so there's a
|
|
|
|
* deterministic order and we avoid flapping. */
|
2007-10-12 23:33:37 +02:00
|
|
|
return memcmp(first->cache_info.identity_digest,
|
|
|
|
second->cache_info.identity_digest,
|
|
|
|
DIGEST_LEN);
|
2007-07-29 07:20:31 +02:00
|
|
|
}
|
|
|
|
|
2007-08-22 17:07:44 +02:00
|
|
|
/** Given a list of routerinfo_t in <b>routers</b>, return a new digestmap_t
|
|
|
|
* whose keys are the identity digests of those routers that we're going to
|
|
|
|
* exclude for Sybil-like appearance. */
|
2007-07-29 07:20:31 +02:00
|
|
|
static digestmap_t *
|
|
|
|
get_possible_sybil_list(const smartlist_t *routers)
|
|
|
|
{
|
2008-01-10 17:08:47 +01:00
|
|
|
or_options_t *options = get_options();
|
2007-07-29 07:20:31 +02:00
|
|
|
digestmap_t *omit_as_sybil;
|
|
|
|
smartlist_t *routers_by_ip = smartlist_create();
|
|
|
|
uint32_t last_addr;
|
|
|
|
int addr_count;
|
2008-01-10 17:08:47 +01:00
|
|
|
/* Allow at most this number of Tor servers on a single IP address, ... */
|
|
|
|
int max_with_same_addr = options->AuthDirMaxServersPerAddr;
|
|
|
|
/* ... unless it's a directory authority, in which case allow more. */
|
|
|
|
int max_with_same_addr_on_authority = options->AuthDirMaxServersPerAuthAddr;
|
|
|
|
if (max_with_same_addr <= 0)
|
|
|
|
max_with_same_addr = INT_MAX;
|
|
|
|
if (max_with_same_addr_on_authority <= 0)
|
|
|
|
max_with_same_addr_on_authority = INT_MAX;
|
|
|
|
|
2007-07-29 07:20:31 +02:00
|
|
|
smartlist_add_all(routers_by_ip, routers);
|
|
|
|
smartlist_sort(routers_by_ip, _compare_routerinfo_by_ip_and_bw);
|
|
|
|
omit_as_sybil = digestmap_new();
|
|
|
|
|
|
|
|
last_addr = 0;
|
|
|
|
addr_count = 0;
|
|
|
|
SMARTLIST_FOREACH(routers_by_ip, routerinfo_t *, ri,
|
|
|
|
{
|
|
|
|
if (last_addr != ri->addr) {
|
|
|
|
last_addr = ri->addr;
|
|
|
|
addr_count = 1;
|
2008-01-10 17:08:47 +01:00
|
|
|
} else if (++addr_count > max_with_same_addr) {
|
2007-10-10 22:13:52 +02:00
|
|
|
if (!router_addr_is_trusted_dir(ri->addr) ||
|
2008-01-10 17:08:47 +01:00
|
|
|
addr_count > max_with_same_addr_on_authority)
|
2007-10-10 21:53:08 +02:00
|
|
|
digestmap_set(omit_as_sybil, ri->cache_info.identity_digest, ri);
|
2007-07-29 07:20:31 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
smartlist_free(routers_by_ip);
|
|
|
|
return omit_as_sybil;
|
|
|
|
}
|
|
|
|
|
2007-08-22 17:07:44 +02:00
|
|
|
/** Extract status information from <b>ri</b> and from other authority
|
|
|
|
* functions and store it in <b>rs</b>>. If <b>naming</b>, consider setting
|
2010-07-12 02:51:44 +02:00
|
|
|
* the named flag in <b>rs</b>.
|
2007-12-18 22:37:58 +01:00
|
|
|
*
|
|
|
|
* We assume that ri-\>is_running has already been set, e.g. by
|
|
|
|
* dirserv_set_router_is_running(ri, now);
|
|
|
|
*/
|
2007-12-18 23:39:15 +01:00
|
|
|
void
|
2007-06-08 20:41:09 +02:00
|
|
|
set_routerstatus_from_routerinfo(routerstatus_t *rs,
|
|
|
|
routerinfo_t *ri, time_t now,
|
2010-07-12 02:51:44 +02:00
|
|
|
int naming, int listbadexits,
|
|
|
|
int listbaddirs)
|
2007-06-08 20:41:09 +02:00
|
|
|
{
|
|
|
|
int unstable_version =
|
|
|
|
!tor_version_as_new_as(ri->platform,"0.1.1.16-rc-cvs");
|
|
|
|
memset(rs, 0, sizeof(routerstatus_t));
|
|
|
|
|
2007-09-20 06:54:50 +02:00
|
|
|
rs->is_authority =
|
|
|
|
router_digest_is_trusted_dir(ri->cache_info.identity_digest);
|
2007-06-13 21:06:23 +02:00
|
|
|
|
2007-06-08 20:41:09 +02:00
|
|
|
/* Already set by compute_performance_thresholds. */
|
|
|
|
rs->is_exit = ri->is_exit;
|
|
|
|
rs->is_stable = ri->is_stable =
|
|
|
|
router_is_active(ri, now) &&
|
|
|
|
!dirserv_thinks_router_is_unreliable(now, ri, 1, 0) &&
|
|
|
|
!unstable_version;
|
|
|
|
rs->is_fast = ri->is_fast =
|
|
|
|
router_is_active(ri, now) &&
|
|
|
|
!dirserv_thinks_router_is_unreliable(now, ri, 0, 1);
|
|
|
|
rs->is_running = ri->is_running; /* computed above */
|
2007-10-19 20:56:26 +02:00
|
|
|
|
|
|
|
if (naming) {
|
|
|
|
uint32_t name_status = dirserv_get_name_status(
|
|
|
|
ri->cache_info.identity_digest, ri->nickname);
|
|
|
|
rs->is_named = (naming && (name_status & FP_NAMED)) ? 1 : 0;
|
|
|
|
rs->is_unnamed = (naming && (name_status & FP_UNNAMED)) ? 1 : 0;
|
|
|
|
}
|
2007-06-08 20:41:09 +02:00
|
|
|
rs->is_valid = ri->is_valid;
|
2007-10-28 20:48:19 +01:00
|
|
|
|
2007-10-27 00:50:42 +02:00
|
|
|
if (rs->is_fast &&
|
2010-07-21 15:24:14 +02:00
|
|
|
(router_get_advertised_bandwidth(ri) >= BANDWIDTH_TO_GUARANTEE_GUARD ||
|
2007-10-27 00:50:42 +02:00
|
|
|
router_get_advertised_bandwidth(ri) >=
|
2010-07-12 02:51:44 +02:00
|
|
|
MIN(guard_bandwidth_including_exits,
|
|
|
|
guard_bandwidth_excluding_exits))) {
|
2007-11-08 17:58:59 +01:00
|
|
|
long tk = rep_hist_get_weighted_time_known(
|
|
|
|
ri->cache_info.identity_digest, now);
|
2007-10-27 00:50:42 +02:00
|
|
|
double wfu = rep_hist_get_weighted_fractional_uptime(
|
|
|
|
ri->cache_info.identity_digest, now);
|
2007-11-08 17:58:59 +01:00
|
|
|
rs->is_possible_guard = (wfu >= guard_wfu && tk >= guard_tk) ? 1 : 0;
|
2007-10-27 00:50:42 +02:00
|
|
|
} else {
|
|
|
|
rs->is_possible_guard = 0;
|
|
|
|
}
|
2007-12-10 17:49:54 +01:00
|
|
|
rs->is_bad_directory = listbaddirs && ri->is_bad_directory;
|
2007-06-08 20:41:09 +02:00
|
|
|
rs->is_bad_exit = listbadexits && ri->is_bad_exit;
|
2007-10-29 20:10:42 +01:00
|
|
|
ri->is_hs_dir = dirserv_thinks_router_is_hs_dir(ri, now);
|
|
|
|
rs->is_hs_dir = ri->is_hs_dir;
|
2008-07-16 15:15:11 +02:00
|
|
|
rs->is_v2_dir = ri->dir_port != 0;
|
2007-06-08 20:41:09 +02:00
|
|
|
|
|
|
|
if (!strcasecmp(ri->nickname, UNNAMED_ROUTER_NICKNAME))
|
2007-10-19 20:56:26 +02:00
|
|
|
rs->is_named = rs->is_unnamed = 0;
|
2007-06-08 20:41:09 +02:00
|
|
|
|
|
|
|
rs->published_on = ri->cache_info.published_on;
|
|
|
|
memcpy(rs->identity_digest, ri->cache_info.identity_digest, DIGEST_LEN);
|
|
|
|
memcpy(rs->descriptor_digest, ri->cache_info.signed_descriptor_digest,
|
|
|
|
DIGEST_LEN);
|
|
|
|
rs->addr = ri->addr;
|
|
|
|
strlcpy(rs->nickname, ri->nickname, sizeof(rs->nickname));
|
|
|
|
rs->or_port = ri->or_port;
|
|
|
|
rs->dir_port = ri->dir_port;
|
|
|
|
}
|
|
|
|
|
2007-12-10 17:40:14 +01:00
|
|
|
/** Routerstatus <b>rs</b> is part of a group of routers that are on
|
|
|
|
* too narrow an IP-space. Clear out its flags: we don't want people
|
|
|
|
* using it.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
clear_status_flags_on_sybil(routerstatus_t *rs)
|
|
|
|
{
|
|
|
|
rs->is_authority = rs->is_exit = rs->is_stable = rs->is_fast =
|
|
|
|
rs->is_running = rs->is_named = rs->is_valid = rs->is_v2_dir =
|
2007-12-10 17:49:54 +01:00
|
|
|
rs->is_hs_dir = rs->is_possible_guard = rs->is_bad_exit =
|
|
|
|
rs->is_bad_directory = 0;
|
2007-12-10 17:40:14 +01:00
|
|
|
/* FFFF we might want some mechanism to check later on if we
|
|
|
|
* missed zeroing any flags: it's easy to add a new flag but
|
|
|
|
* forget to add it to this clause. */
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Clear all the status flags in routerinfo <b>router</b>. We put this
|
|
|
|
* function here because it's eerily similar to
|
|
|
|
* clear_status_flags_on_sybil() above. One day we should merge them. */
|
|
|
|
void
|
|
|
|
router_clear_status_flags(routerinfo_t *router)
|
|
|
|
{
|
|
|
|
router->is_valid = router->is_running = router->is_hs_dir =
|
|
|
|
router->is_fast = router->is_stable =
|
|
|
|
router->is_possible_guard = router->is_exit =
|
2007-12-10 17:49:54 +01:00
|
|
|
router->is_bad_exit = router->is_bad_directory = 0;
|
2007-12-10 17:40:14 +01:00
|
|
|
}
|
|
|
|
|
2009-07-31 06:33:53 +02:00
|
|
|
/**
|
|
|
|
* Helper function to parse out a line in the measured bandwidth file
|
|
|
|
* into a measured_bw_line_t output structure. Returns -1 on failure
|
|
|
|
* or 0 on success.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
measured_bw_line_parse(measured_bw_line_t *out, const char *orig_line)
|
|
|
|
{
|
|
|
|
char *line = tor_strdup(orig_line);
|
|
|
|
char *cp = line;
|
|
|
|
int got_bw = 0;
|
|
|
|
int got_node_id = 0;
|
|
|
|
char *strtok_state; /* lame sauce d'jour */
|
2009-08-10 03:42:29 +02:00
|
|
|
cp = tor_strtok_r(cp, " \t", &strtok_state);
|
2009-07-31 06:33:53 +02:00
|
|
|
|
|
|
|
if (!cp) {
|
|
|
|
log_warn(LD_DIRSERV, "Invalid line in bandwidth file: %s",
|
|
|
|
escaped(orig_line));
|
|
|
|
tor_free(line);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (orig_line[strlen(orig_line)-1] != '\n') {
|
|
|
|
log_warn(LD_DIRSERV, "Incomplete line in bandwidth file: %s",
|
|
|
|
escaped(orig_line));
|
|
|
|
tor_free(line);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2009-08-10 03:21:15 +02:00
|
|
|
if (strcmpstart(cp, "bw=") == 0) {
|
|
|
|
int parse_ok = 0;
|
2009-07-31 06:33:53 +02:00
|
|
|
char *endptr;
|
|
|
|
if (got_bw) {
|
|
|
|
log_warn(LD_DIRSERV, "Double bw= in bandwidth file line: %s",
|
|
|
|
escaped(orig_line));
|
|
|
|
tor_free(line);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cp+=strlen("bw=");
|
2009-08-10 03:21:15 +02:00
|
|
|
|
|
|
|
out->bw = tor_parse_long(cp, 0, 0, LONG_MAX, &parse_ok, &endptr);
|
|
|
|
if (!parse_ok || (*endptr && !TOR_ISSPACE(*endptr))) {
|
2009-07-31 06:33:53 +02:00
|
|
|
log_warn(LD_DIRSERV, "Invalid bandwidth in bandwidth file line: %s",
|
|
|
|
escaped(orig_line));
|
|
|
|
tor_free(line);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
got_bw=1;
|
2009-08-10 03:21:15 +02:00
|
|
|
} else if (strcmpstart(cp, "node_id=$") == 0) {
|
2009-07-31 06:33:53 +02:00
|
|
|
if (got_node_id) {
|
|
|
|
log_warn(LD_DIRSERV, "Double node_id= in bandwidth file line: %s",
|
|
|
|
escaped(orig_line));
|
|
|
|
tor_free(line);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cp+=strlen("node_id=$");
|
|
|
|
|
|
|
|
if (strlen(cp) != HEX_DIGEST_LEN ||
|
|
|
|
base16_decode(out->node_id, DIGEST_LEN, cp, HEX_DIGEST_LEN)) {
|
|
|
|
log_warn(LD_DIRSERV, "Invalid node_id in bandwidth file line: %s",
|
|
|
|
escaped(orig_line));
|
|
|
|
tor_free(line);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
strncpy(out->node_hex, cp, sizeof(out->node_hex));
|
|
|
|
got_node_id=1;
|
|
|
|
}
|
2009-08-10 03:42:29 +02:00
|
|
|
} while ((cp = tor_strtok_r(NULL, " \t", &strtok_state)));
|
2009-07-31 06:33:53 +02:00
|
|
|
|
|
|
|
if (got_bw && got_node_id) {
|
|
|
|
tor_free(line);
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
log_warn(LD_DIRSERV, "Incomplete line in bandwidth file: %s",
|
|
|
|
escaped(orig_line));
|
|
|
|
tor_free(line);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to apply a parsed measurement line to a list
|
|
|
|
* of bandwidth statuses. Returns true if a line is found,
|
|
|
|
* false otherwise.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
measured_bw_line_apply(measured_bw_line_t *parsed_line,
|
|
|
|
smartlist_t *routerstatuses)
|
|
|
|
{
|
|
|
|
routerstatus_t *rs = NULL;
|
|
|
|
if (!routerstatuses)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
rs = smartlist_bsearch(routerstatuses, parsed_line->node_id,
|
|
|
|
compare_digest_to_routerstatus_entry);
|
|
|
|
|
|
|
|
if (rs) {
|
|
|
|
rs->has_measured_bw = 1;
|
2009-09-01 05:23:47 +02:00
|
|
|
rs->measured_bw = (uint32_t)parsed_line->bw;
|
2009-07-31 06:33:53 +02:00
|
|
|
} else {
|
|
|
|
log_info(LD_DIRSERV, "Node ID %s not found in routerstatus list",
|
|
|
|
parsed_line->node_hex);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rs != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read the measured bandwidth file and apply it to the list of
|
|
|
|
* routerstatuses. Returns -1 on error, 0 otherwise.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
dirserv_read_measured_bandwidths(const char *from_file,
|
|
|
|
smartlist_t *routerstatuses)
|
|
|
|
{
|
|
|
|
char line[256];
|
|
|
|
FILE *fp = fopen(from_file, "r");
|
|
|
|
int applied_lines = 0;
|
|
|
|
time_t file_time;
|
|
|
|
int ok;
|
|
|
|
if (fp == NULL) {
|
|
|
|
log_warn(LD_CONFIG, "Can't open bandwidth file at configured location: %s",
|
|
|
|
from_file);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-08-10 03:21:15 +02:00
|
|
|
if (!fgets(line, sizeof(line), fp)
|
|
|
|
|| !strlen(line) || line[strlen(line)-1] != '\n') {
|
2009-07-31 06:33:53 +02:00
|
|
|
log_warn(LD_DIRSERV, "Long or truncated time in bandwidth file: %s",
|
|
|
|
escaped(line));
|
|
|
|
fclose(fp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
line[strlen(line)-1] = '\0';
|
|
|
|
file_time = tor_parse_ulong(line, 10, 0, ULONG_MAX, &ok, NULL);
|
|
|
|
if (!ok) {
|
|
|
|
log_warn(LD_DIRSERV, "Non-integer time in bandwidth file: %s",
|
|
|
|
escaped(line));
|
|
|
|
fclose(fp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((time(NULL) - file_time) > MAX_MEASUREMENT_AGE) {
|
|
|
|
log_warn(LD_DIRSERV, "Bandwidth measurement file stale. Age: %u",
|
|
|
|
(unsigned)(time(NULL) - file_time));
|
|
|
|
fclose(fp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (routerstatuses)
|
|
|
|
smartlist_sort(routerstatuses, compare_routerstatus_entries);
|
|
|
|
|
|
|
|
while (!feof(fp)) {
|
|
|
|
measured_bw_line_t parsed_line;
|
2009-08-10 03:21:15 +02:00
|
|
|
if (fgets(line, sizeof(line), fp) && strlen(line)) {
|
|
|
|
if (measured_bw_line_parse(&parsed_line, line) != -1) {
|
|
|
|
if (measured_bw_line_apply(&parsed_line, routerstatuses) > 0)
|
|
|
|
applied_lines++;
|
|
|
|
}
|
2009-07-31 06:33:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
2009-09-23 10:59:05 +02:00
|
|
|
log_info(LD_DIRSERV,
|
|
|
|
"Bandwidth measurement file successfully read. "
|
|
|
|
"Applied %d measurements.", applied_lines);
|
2009-07-31 06:33:53 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-05 22:39:29 +01:00
|
|
|
/** Return a new networkstatus_t* containing our current opinion. (For v3
|
2007-12-10 17:40:14 +01:00
|
|
|
* authorities) */
|
2008-02-05 22:39:29 +01:00
|
|
|
networkstatus_t *
|
2007-10-22 19:31:26 +02:00
|
|
|
dirserv_generate_networkstatus_vote_obj(crypto_pk_env_t *private_key,
|
2007-12-10 17:40:14 +01:00
|
|
|
authority_cert_t *cert)
|
2007-06-08 20:41:09 +02:00
|
|
|
{
|
|
|
|
or_options_t *options = get_options();
|
2008-02-05 22:39:29 +01:00
|
|
|
networkstatus_t *v3_out = NULL;
|
2007-06-08 20:41:09 +02:00
|
|
|
uint32_t addr;
|
|
|
|
char *hostname = NULL, *client_versions = NULL, *server_versions = NULL;
|
|
|
|
const char *contact;
|
|
|
|
smartlist_t *routers, *routerstatuses;
|
|
|
|
char identity_digest[DIGEST_LEN];
|
|
|
|
char signing_key_digest[DIGEST_LEN];
|
|
|
|
int naming = options->NamingAuthoritativeDir;
|
|
|
|
int listbadexits = options->AuthDirListBadExits;
|
2007-12-10 17:49:54 +01:00
|
|
|
int listbaddirs = options->AuthDirListBadDirs;
|
2007-06-08 20:41:09 +02:00
|
|
|
routerlist_t *rl = router_get_routerlist();
|
|
|
|
time_t now = time(NULL);
|
|
|
|
time_t cutoff = now - ROUTER_MAX_AGE_TO_PUBLISH;
|
|
|
|
networkstatus_voter_info_t *voter = NULL;
|
2007-07-18 22:46:12 +02:00
|
|
|
vote_timing_t timing;
|
2007-07-29 07:20:31 +02:00
|
|
|
digestmap_t *omit_as_sybil = NULL;
|
2009-07-02 19:56:52 +02:00
|
|
|
const int vote_on_reachability = running_long_enough_to_decide_unreachable();
|
2009-08-24 18:51:33 +02:00
|
|
|
smartlist_t *microdescriptors = NULL;
|
2007-06-08 20:41:09 +02:00
|
|
|
|
|
|
|
tor_assert(private_key);
|
|
|
|
tor_assert(cert);
|
|
|
|
|
|
|
|
if (resolve_my_address(LOG_WARN, options, &addr, &hostname)<0) {
|
|
|
|
log_warn(LD_NET, "Couldn't resolve my hostname");
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-10-19 18:28:13 +02:00
|
|
|
if (!strchr(hostname, '.')) {
|
|
|
|
tor_free(hostname);
|
2008-07-24 15:44:04 +02:00
|
|
|
hostname = tor_dup_ip(addr);
|
2007-10-19 18:28:13 +02:00
|
|
|
}
|
2007-06-08 20:41:09 +02:00
|
|
|
if (crypto_pk_get_digest(private_key, signing_key_digest)<0) {
|
|
|
|
log_err(LD_BUG, "Error computing signing key digest");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (crypto_pk_get_digest(cert->identity_key, identity_digest)<0) {
|
|
|
|
log_err(LD_BUG, "Error computing identity key digest");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options->VersioningAuthoritativeDir) {
|
|
|
|
client_versions = format_versions_list(options->RecommendedClientVersions);
|
|
|
|
server_versions = format_versions_list(options->RecommendedServerVersions);
|
|
|
|
}
|
|
|
|
|
|
|
|
contact = get_options()->ContactInfo;
|
|
|
|
if (!contact)
|
|
|
|
contact = "(none)";
|
|
|
|
|
|
|
|
/* precompute this part, since we need it to decide what "stable"
|
|
|
|
* means. */
|
|
|
|
SMARTLIST_FOREACH(rl->routers, routerinfo_t *, ri, {
|
2007-08-17 03:29:58 +02:00
|
|
|
dirserv_set_router_is_running(ri, now);
|
2007-06-08 20:41:09 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
dirserv_compute_performance_thresholds(rl);
|
|
|
|
|
|
|
|
routers = smartlist_create();
|
|
|
|
smartlist_add_all(routers, rl->routers);
|
2007-10-12 23:33:37 +02:00
|
|
|
routers_sort_by_identity(routers);
|
2007-07-29 07:20:31 +02:00
|
|
|
omit_as_sybil = get_possible_sybil_list(routers);
|
2007-06-08 20:41:09 +02:00
|
|
|
|
|
|
|
routerstatuses = smartlist_create();
|
2009-08-24 18:51:33 +02:00
|
|
|
microdescriptors = smartlist_create();
|
2007-06-08 20:41:09 +02:00
|
|
|
|
2009-08-24 18:51:33 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(routers, routerinfo_t *, ri) {
|
2007-06-08 20:41:09 +02:00
|
|
|
if (ri->cache_info.published_on >= cutoff) {
|
|
|
|
routerstatus_t *rs;
|
|
|
|
vote_routerstatus_t *vrs;
|
2009-08-24 18:51:33 +02:00
|
|
|
microdesc_t *md;
|
2007-06-08 20:41:09 +02:00
|
|
|
|
|
|
|
vrs = tor_malloc_zero(sizeof(vote_routerstatus_t));
|
|
|
|
rs = &vrs->status;
|
|
|
|
set_routerstatus_from_routerinfo(rs, ri, now,
|
2010-07-12 02:51:44 +02:00
|
|
|
naming, listbadexits, listbaddirs);
|
2007-06-08 20:41:09 +02:00
|
|
|
|
2007-12-10 17:40:14 +01:00
|
|
|
if (digestmap_get(omit_as_sybil, ri->cache_info.identity_digest))
|
|
|
|
clear_status_flags_on_sybil(rs);
|
|
|
|
|
2007-11-08 19:15:49 +01:00
|
|
|
if (!vote_on_reachability)
|
|
|
|
rs->is_running = 0;
|
2007-07-29 07:20:31 +02:00
|
|
|
|
2007-06-08 20:41:09 +02:00
|
|
|
vrs->version = version_from_platform(ri->platform);
|
2009-08-24 18:51:33 +02:00
|
|
|
md = dirvote_create_microdescriptor(ri);
|
|
|
|
if (md) {
|
|
|
|
char buf[128];
|
|
|
|
vote_microdesc_hash_t *h;
|
|
|
|
dirvote_format_microdesc_vote_line(buf, sizeof(buf), md);
|
|
|
|
h = tor_malloc(sizeof(vote_microdesc_hash_t));
|
|
|
|
h->microdesc_hash_line = tor_strdup(buf);
|
|
|
|
h->next = NULL;
|
|
|
|
vrs->microdesc = h;
|
|
|
|
md->last_listed = now;
|
|
|
|
smartlist_add(microdescriptors, md);
|
|
|
|
}
|
|
|
|
|
2007-06-08 20:41:09 +02:00
|
|
|
smartlist_add(routerstatuses, vrs);
|
|
|
|
}
|
2009-08-24 18:51:33 +02:00
|
|
|
} SMARTLIST_FOREACH_END(ri);
|
|
|
|
|
|
|
|
{
|
|
|
|
smartlist_t *added =
|
|
|
|
microdescs_add_list_to_cache(get_microdesc_cache(),
|
|
|
|
microdescriptors, SAVED_NOWHERE, 0);
|
|
|
|
smartlist_free(added);
|
|
|
|
smartlist_free(microdescriptors);
|
|
|
|
}
|
2009-07-31 06:33:53 +02:00
|
|
|
|
2007-06-08 20:41:09 +02:00
|
|
|
smartlist_free(routers);
|
2007-07-29 07:20:31 +02:00
|
|
|
digestmap_free(omit_as_sybil, NULL);
|
2007-06-08 20:41:09 +02:00
|
|
|
|
2009-07-31 06:33:53 +02:00
|
|
|
if (options->V3BandwidthsFile) {
|
|
|
|
dirserv_read_measured_bandwidths(options->V3BandwidthsFile,
|
|
|
|
routerstatuses);
|
|
|
|
}
|
|
|
|
|
2008-02-05 22:39:29 +01:00
|
|
|
v3_out = tor_malloc_zero(sizeof(networkstatus_t));
|
2007-08-13 21:55:08 +02:00
|
|
|
|
2008-07-24 11:22:27 +02:00
|
|
|
v3_out->type = NS_TYPE_VOTE;
|
2007-07-18 22:46:12 +02:00
|
|
|
dirvote_get_preferred_voting_intervals(&timing);
|
|
|
|
v3_out->published = now;
|
2007-10-09 22:44:51 +02:00
|
|
|
{
|
2007-10-15 22:37:59 +02:00
|
|
|
char tbuf[ISO_TIME_LEN+1];
|
2008-02-05 22:39:29 +01:00
|
|
|
networkstatus_t *current_consensus =
|
2007-10-09 22:44:51 +02:00
|
|
|
networkstatus_get_live_consensus(now);
|
2008-02-22 20:09:45 +01:00
|
|
|
long last_consensus_interval; /* only used to pick a valid_after */
|
2007-10-09 22:44:51 +02:00
|
|
|
if (current_consensus)
|
2007-10-23 08:38:16 +02:00
|
|
|
last_consensus_interval = current_consensus->fresh_until -
|
2007-10-09 22:44:51 +02:00
|
|
|
current_consensus->valid_after;
|
|
|
|
else
|
2008-06-20 19:03:13 +02:00
|
|
|
last_consensus_interval = options->TestingV3AuthInitialVotingInterval;
|
2007-10-09 22:44:51 +02:00
|
|
|
v3_out->valid_after =
|
2008-02-22 20:09:45 +01:00
|
|
|
dirvote_get_start_of_next_interval(now, (int)last_consensus_interval);
|
2007-10-15 22:37:59 +02:00
|
|
|
format_iso_time(tbuf, v3_out->valid_after);
|
|
|
|
log_notice(LD_DIR,"Choosing valid-after time in vote as %s: "
|
2007-10-23 08:38:16 +02:00
|
|
|
"consensus_set=%d, last_interval=%d",
|
|
|
|
tbuf, current_consensus?1:0, (int)last_consensus_interval);
|
2007-10-09 22:44:51 +02:00
|
|
|
}
|
2007-07-18 22:46:12 +02:00
|
|
|
v3_out->fresh_until = v3_out->valid_after + timing.vote_interval;
|
|
|
|
v3_out->valid_until = v3_out->valid_after +
|
|
|
|
(timing.vote_interval * timing.n_intervals_valid);
|
|
|
|
v3_out->vote_seconds = timing.vote_delay;
|
|
|
|
v3_out->dist_seconds = timing.dist_delay;
|
2007-08-15 17:38:58 +02:00
|
|
|
tor_assert(v3_out->vote_seconds > 0);
|
|
|
|
tor_assert(v3_out->dist_seconds > 0);
|
|
|
|
tor_assert(timing.n_intervals_valid > 0);
|
2007-06-08 20:41:09 +02:00
|
|
|
|
|
|
|
v3_out->client_versions = client_versions;
|
|
|
|
v3_out->server_versions = server_versions;
|
|
|
|
v3_out->known_flags = smartlist_create();
|
|
|
|
smartlist_split_string(v3_out->known_flags,
|
2007-11-08 19:15:49 +01:00
|
|
|
"Authority Exit Fast Guard HSDir Stable V2Dir Valid",
|
2007-06-08 20:41:09 +02:00
|
|
|
0, SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
2007-11-08 19:15:49 +01:00
|
|
|
if (vote_on_reachability)
|
|
|
|
smartlist_add(v3_out->known_flags, tor_strdup("Running"));
|
2007-12-10 17:49:54 +01:00
|
|
|
if (listbaddirs)
|
|
|
|
smartlist_add(v3_out->known_flags, tor_strdup("BadDirectory"));
|
2007-06-08 20:41:09 +02:00
|
|
|
if (listbadexits)
|
|
|
|
smartlist_add(v3_out->known_flags, tor_strdup("BadExit"));
|
2007-10-19 20:56:26 +02:00
|
|
|
if (naming) {
|
2007-06-08 20:41:09 +02:00
|
|
|
smartlist_add(v3_out->known_flags, tor_strdup("Named"));
|
2007-10-19 20:56:26 +02:00
|
|
|
smartlist_add(v3_out->known_flags, tor_strdup("Unnamed"));
|
|
|
|
}
|
2007-06-08 20:41:09 +02:00
|
|
|
smartlist_sort_strings(v3_out->known_flags);
|
|
|
|
|
2009-09-15 10:40:08 +02:00
|
|
|
if (options->ConsensusParams) {
|
|
|
|
v3_out->net_params = smartlist_create();
|
|
|
|
smartlist_split_string(v3_out->net_params,
|
|
|
|
options->ConsensusParams, NULL, 0, 0);
|
|
|
|
smartlist_sort_strings(v3_out->net_params);
|
|
|
|
}
|
|
|
|
|
2007-06-08 20:41:09 +02:00
|
|
|
voter = tor_malloc_zero(sizeof(networkstatus_voter_info_t));
|
|
|
|
voter->nickname = tor_strdup(options->Nickname);
|
|
|
|
memcpy(voter->identity_digest, identity_digest, DIGEST_LEN);
|
2009-09-16 23:01:01 +02:00
|
|
|
voter->sigs = smartlist_create();
|
2007-06-08 20:41:09 +02:00
|
|
|
voter->address = hostname;
|
|
|
|
voter->addr = addr;
|
|
|
|
voter->dir_port = options->DirPort;
|
|
|
|
voter->or_port = options->ORPort;
|
|
|
|
voter->contact = tor_strdup(contact);
|
2008-05-12 04:14:01 +02:00
|
|
|
if (options->V3AuthUseLegacyKey) {
|
|
|
|
authority_cert_t *c = get_my_v3_legacy_cert();
|
|
|
|
if (c) {
|
|
|
|
crypto_pk_get_digest(c->identity_key, voter->legacy_id_digest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-08 20:41:09 +02:00
|
|
|
v3_out->voters = smartlist_create();
|
|
|
|
smartlist_add(v3_out->voters, voter);
|
|
|
|
v3_out->cert = authority_cert_dup(cert);
|
|
|
|
v3_out->routerstatus_list = routerstatuses;
|
2007-10-11 18:06:51 +02:00
|
|
|
/* Note: networkstatus_digest is unset; it won't get set until we actually
|
|
|
|
* format the vote. */
|
2007-06-08 20:41:09 +02:00
|
|
|
|
|
|
|
return v3_out;
|
|
|
|
}
|
|
|
|
|
2007-10-22 19:31:26 +02:00
|
|
|
/** For v2 authoritative directories only: Replace the contents of
|
|
|
|
* <b>the_v2_networkstatus</b> with a newly generated network status
|
|
|
|
* object. */
|
2006-06-18 23:30:03 +02:00
|
|
|
static cached_dir_t *
|
2007-10-22 19:31:26 +02:00
|
|
|
generate_v2_networkstatus_opinion(void)
|
2005-08-25 22:33:17 +02:00
|
|
|
{
|
2006-06-18 23:30:03 +02:00
|
|
|
cached_dir_t *r = NULL;
|
2005-08-25 22:33:17 +02:00
|
|
|
size_t len, identity_pkey_len;
|
|
|
|
char *status = NULL, *client_versions = NULL, *server_versions = NULL,
|
|
|
|
*identity_pkey = NULL, *hostname = NULL;
|
|
|
|
char *outp, *endp;
|
|
|
|
or_options_t *options = get_options();
|
|
|
|
char fingerprint[FINGERPRINT_LEN+1];
|
2006-03-15 00:40:37 +01:00
|
|
|
char ipaddr[INET_NTOA_BUF_LEN];
|
2006-03-14 23:43:52 +01:00
|
|
|
char published[ISO_TIME_LEN+1];
|
2005-08-25 22:33:17 +02:00
|
|
|
char digest[DIGEST_LEN];
|
|
|
|
struct in_addr in;
|
|
|
|
uint32_t addr;
|
2007-05-22 20:52:32 +02:00
|
|
|
crypto_pk_env_t *private_key;
|
2005-10-18 19:43:54 +02:00
|
|
|
routerlist_t *rl = router_get_routerlist();
|
2005-09-15 07:19:38 +02:00
|
|
|
time_t now = time(NULL);
|
2006-03-08 07:29:52 +01:00
|
|
|
time_t cutoff = now - ROUTER_MAX_AGE_TO_PUBLISH;
|
2005-09-15 16:39:05 +02:00
|
|
|
int naming = options->NamingAuthoritativeDir;
|
2005-09-21 02:41:06 +02:00
|
|
|
int versioning = options->VersioningAuthoritativeDir;
|
2007-12-10 17:49:54 +01:00
|
|
|
int listbaddirs = options->AuthDirListBadDirs;
|
2006-10-23 05:48:42 +02:00
|
|
|
int listbadexits = options->AuthDirListBadExits;
|
2005-09-02 22:30:03 +02:00
|
|
|
const char *contact;
|
2007-05-22 20:52:32 +02:00
|
|
|
char *version_lines = NULL;
|
|
|
|
smartlist_t *routers = NULL;
|
2007-07-29 07:20:31 +02:00
|
|
|
digestmap_t *omit_as_sybil = NULL;
|
2007-05-22 20:52:32 +02:00
|
|
|
|
2007-06-08 20:41:09 +02:00
|
|
|
private_key = get_identity_key();
|
2005-08-26 22:59:04 +02:00
|
|
|
|
2006-07-15 22:26:05 +02:00
|
|
|
if (resolve_my_address(LOG_WARN, options, &addr, &hostname)<0) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_warn(LD_NET, "Couldn't resolve my hostname");
|
2005-08-25 22:33:17 +02:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
in.s_addr = htonl(addr);
|
|
|
|
tor_inet_ntoa(&in, ipaddr, sizeof(ipaddr));
|
|
|
|
|
2007-05-19 02:14:34 +02:00
|
|
|
format_iso_time(published, now);
|
2005-08-25 22:33:17 +02:00
|
|
|
|
|
|
|
client_versions = format_versions_list(options->RecommendedClientVersions);
|
|
|
|
server_versions = format_versions_list(options->RecommendedServerVersions);
|
|
|
|
|
|
|
|
if (crypto_pk_write_public_key_to_string(private_key, &identity_pkey,
|
|
|
|
&identity_pkey_len)<0) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_warn(LD_BUG,"Writing public key to string failed.");
|
2005-08-25 22:33:17 +02:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2007-06-08 20:41:09 +02:00
|
|
|
if (crypto_pk_get_fingerprint(private_key, fingerprint, 0)<0) {
|
|
|
|
log_err(LD_BUG, "Error computing fingerprint");
|
|
|
|
goto done;
|
2005-08-25 22:33:17 +02:00
|
|
|
}
|
|
|
|
|
2005-09-02 22:30:03 +02:00
|
|
|
contact = get_options()->ContactInfo;
|
|
|
|
if (!contact)
|
|
|
|
contact = "(none)";
|
|
|
|
|
2007-05-22 20:52:32 +02:00
|
|
|
if (versioning) {
|
2007-05-23 02:43:20 +02:00
|
|
|
size_t v_len = 64+strlen(client_versions)+strlen(server_versions);
|
2007-05-22 20:52:32 +02:00
|
|
|
version_lines = tor_malloc(v_len);
|
|
|
|
tor_snprintf(version_lines, v_len,
|
|
|
|
"client-versions %s\nserver-versions %s\n",
|
|
|
|
client_versions, server_versions);
|
|
|
|
} else {
|
|
|
|
version_lines = tor_strdup("");
|
|
|
|
}
|
|
|
|
|
|
|
|
len = 4096+strlen(client_versions)+strlen(server_versions);
|
2005-12-14 21:40:40 +01:00
|
|
|
len += identity_pkey_len*2;
|
2005-10-18 19:43:54 +02:00
|
|
|
len += (RS_ENTRY_LEN)*smartlist_len(rl->routers);
|
2005-08-25 22:33:17 +02:00
|
|
|
|
|
|
|
status = tor_malloc(len);
|
2007-06-08 20:41:09 +02:00
|
|
|
tor_snprintf(status, len,
|
2005-08-25 22:33:17 +02:00
|
|
|
"network-status-version 2\n"
|
|
|
|
"dir-source %s %s %d\n"
|
2005-09-02 22:30:03 +02:00
|
|
|
"fingerprint %s\n"
|
|
|
|
"contact %s\n"
|
2005-08-25 22:33:17 +02:00
|
|
|
"published %s\n"
|
2007-12-10 17:49:54 +01:00
|
|
|
"dir-options%s%s%s%s\n"
|
2007-05-22 20:52:32 +02:00
|
|
|
"%s" /* client version line, server version line. */
|
2007-05-23 02:43:20 +02:00
|
|
|
"dir-signing-key\n%s",
|
2005-08-25 22:33:17 +02:00
|
|
|
hostname, ipaddr, (int)options->DirPort,
|
|
|
|
fingerprint,
|
2005-09-02 22:30:03 +02:00
|
|
|
contact,
|
2005-09-03 04:38:39 +02:00
|
|
|
published,
|
2005-09-15 16:39:05 +02:00
|
|
|
naming ? " Names" : "",
|
2007-12-10 17:49:54 +01:00
|
|
|
listbaddirs ? " BadDirectories" : "",
|
2006-10-23 05:48:42 +02:00
|
|
|
listbadexits ? " BadExits" : "",
|
2005-09-21 02:41:06 +02:00
|
|
|
versioning ? " Versions" : "",
|
2007-05-22 20:52:32 +02:00
|
|
|
version_lines,
|
2005-08-25 22:33:17 +02:00
|
|
|
identity_pkey);
|
2007-06-08 20:41:09 +02:00
|
|
|
outp = status + strlen(status);
|
|
|
|
endp = status + len;
|
2005-08-25 22:33:17 +02:00
|
|
|
|
2006-01-02 04:32:55 +01:00
|
|
|
/* precompute this part, since we need it to decide what "stable"
|
|
|
|
* means. */
|
|
|
|
SMARTLIST_FOREACH(rl->routers, routerinfo_t *, ri, {
|
2007-08-17 03:29:58 +02:00
|
|
|
dirserv_set_router_is_running(ri, now);
|
2006-01-02 04:32:55 +01:00
|
|
|
});
|
|
|
|
|
2006-03-20 22:41:12 +01:00
|
|
|
dirserv_compute_performance_thresholds(rl);
|
2006-01-02 04:32:55 +01:00
|
|
|
|
2007-05-22 20:52:32 +02:00
|
|
|
routers = smartlist_create();
|
|
|
|
smartlist_add_all(routers, rl->routers);
|
2007-10-12 23:33:37 +02:00
|
|
|
routers_sort_by_identity(routers);
|
2007-05-22 20:52:32 +02:00
|
|
|
|
2007-07-29 07:20:31 +02:00
|
|
|
omit_as_sybil = get_possible_sybil_list(routers);
|
|
|
|
|
2007-05-22 20:52:32 +02:00
|
|
|
SMARTLIST_FOREACH(routers, routerinfo_t *, ri, {
|
2006-02-05 03:33:40 +01:00
|
|
|
if (ri->cache_info.published_on >= cutoff) {
|
2007-05-22 19:58:30 +02:00
|
|
|
routerstatus_t rs;
|
2007-06-08 20:41:14 +02:00
|
|
|
char *version = version_from_platform(ri->platform);
|
2007-06-08 20:41:09 +02:00
|
|
|
|
|
|
|
set_routerstatus_from_routerinfo(&rs, ri, now,
|
2010-07-12 02:51:44 +02:00
|
|
|
naming, listbadexits, listbaddirs);
|
2007-05-22 19:58:30 +02:00
|
|
|
|
2007-12-10 17:40:14 +01:00
|
|
|
if (digestmap_get(omit_as_sybil, ri->cache_info.identity_digest))
|
|
|
|
clear_status_flags_on_sybil(&rs);
|
2007-07-29 07:20:31 +02:00
|
|
|
|
2009-07-31 06:33:53 +02:00
|
|
|
if (routerstatus_format_entry(outp, endp-outp, &rs, version, NS_V2)) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_warn(LD_BUG, "Unable to print router status.");
|
2007-06-08 20:41:14 +02:00
|
|
|
tor_free(version);
|
2005-08-25 22:33:17 +02:00
|
|
|
goto done;
|
2005-10-25 19:52:14 +02:00
|
|
|
}
|
2007-06-08 20:41:14 +02:00
|
|
|
tor_free(version);
|
2005-08-25 22:33:17 +02:00
|
|
|
outp += strlen(outp);
|
2006-02-05 03:33:40 +01:00
|
|
|
}
|
|
|
|
});
|
2005-08-25 22:33:17 +02:00
|
|
|
|
2007-06-08 20:41:09 +02:00
|
|
|
if (tor_snprintf(outp, endp-outp, "directory-signature %s\n",
|
|
|
|
get_options()->Nickname)<0) {
|
|
|
|
log_warn(LD_BUG, "Unable to write signature line.");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (router_get_networkstatus_v2_hash(status, digest)<0) {
|
|
|
|
log_warn(LD_BUG, "Unable to hash network status");
|
|
|
|
goto done;
|
2005-08-25 22:33:17 +02:00
|
|
|
}
|
2007-06-08 20:41:09 +02:00
|
|
|
outp += strlen(outp);
|
2005-08-25 22:33:17 +02:00
|
|
|
|
2006-10-31 20:17:07 +01:00
|
|
|
note_crypto_pk_op(SIGN_DIR);
|
2009-09-14 17:57:19 +02:00
|
|
|
if (router_append_dirobj_signature(outp,endp-outp,digest,DIGEST_LEN,
|
|
|
|
private_key)<0) {
|
2006-02-13 10:37:53 +01:00
|
|
|
log_warn(LD_BUG, "Unable to sign router status.");
|
2005-08-25 22:33:17 +02:00
|
|
|
goto done;
|
2005-09-09 21:37:12 +02:00
|
|
|
}
|
2005-08-25 22:33:17 +02:00
|
|
|
|
2007-06-08 21:02:39 +02:00
|
|
|
{
|
2007-10-16 01:15:24 +02:00
|
|
|
networkstatus_v2_t *ns;
|
|
|
|
if (!(ns = networkstatus_v2_parse_from_string(status))) {
|
2007-06-08 21:02:39 +02:00
|
|
|
log_err(LD_BUG,"Generated a networkstatus we couldn't parse.");
|
|
|
|
goto done;
|
|
|
|
}
|
2007-10-16 01:15:24 +02:00
|
|
|
networkstatus_v2_free(ns);
|
2007-06-08 21:02:39 +02:00
|
|
|
}
|
|
|
|
|
2007-05-22 20:52:32 +02:00
|
|
|
{
|
2007-06-08 20:41:09 +02:00
|
|
|
cached_dir_t **ns_ptr = &the_v2_networkstatus;
|
2007-05-22 20:52:32 +02:00
|
|
|
if (*ns_ptr)
|
|
|
|
cached_dir_decref(*ns_ptr);
|
|
|
|
*ns_ptr = new_cached_dir(status, now);
|
|
|
|
status = NULL; /* So it doesn't get double-freed. */
|
2007-06-08 20:41:09 +02:00
|
|
|
the_v2_networkstatus_is_dirty = 0;
|
2007-10-16 01:15:24 +02:00
|
|
|
router_set_networkstatus_v2((*ns_ptr)->dir, now, NS_GENERATED, NULL);
|
2007-05-22 20:52:32 +02:00
|
|
|
r = *ns_ptr;
|
|
|
|
}
|
|
|
|
|
2005-08-25 22:33:17 +02:00
|
|
|
done:
|
|
|
|
tor_free(client_versions);
|
|
|
|
tor_free(server_versions);
|
2007-05-22 20:52:32 +02:00
|
|
|
tor_free(version_lines);
|
2005-08-25 22:33:17 +02:00
|
|
|
tor_free(status);
|
|
|
|
tor_free(hostname);
|
|
|
|
tor_free(identity_pkey);
|
2009-12-12 08:07:59 +01:00
|
|
|
smartlist_free(routers);
|
|
|
|
digestmap_free(omit_as_sybil, NULL);
|
2005-08-25 22:33:17 +02:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2006-12-30 03:41:14 +01:00
|
|
|
/** Given the portion of a networkstatus request URL after "tor/status/" in
|
|
|
|
* <b>key</b>, append to <b>result</b> the digests of the identity keys of the
|
|
|
|
* networkstatus objects that the client has requested. */
|
2006-06-20 02:48:23 +02:00
|
|
|
void
|
|
|
|
dirserv_get_networkstatus_v2_fingerprints(smartlist_t *result,
|
|
|
|
const char *key)
|
|
|
|
{
|
|
|
|
tor_assert(result);
|
|
|
|
|
|
|
|
if (!cached_v2_networkstatus)
|
|
|
|
cached_v2_networkstatus = digestmap_new();
|
|
|
|
|
|
|
|
if (should_generate_v2_networkstatus())
|
2007-10-22 19:31:26 +02:00
|
|
|
generate_v2_networkstatus_opinion();
|
2006-06-20 02:48:23 +02:00
|
|
|
|
|
|
|
if (!strcmp(key,"authority")) {
|
2007-05-02 11:12:04 +02:00
|
|
|
if (authdir_mode_v2(get_options())) {
|
2006-06-20 02:48:23 +02:00
|
|
|
routerinfo_t *me = router_get_my_routerinfo();
|
|
|
|
if (me)
|
|
|
|
smartlist_add(result,
|
|
|
|
tor_memdup(me->cache_info.identity_digest, DIGEST_LEN));
|
|
|
|
}
|
|
|
|
} else if (!strcmp(key, "all")) {
|
2006-12-08 02:50:02 +01:00
|
|
|
if (digestmap_size(cached_v2_networkstatus)) {
|
|
|
|
digestmap_iter_t *iter;
|
|
|
|
iter = digestmap_iter_init(cached_v2_networkstatus);
|
|
|
|
while (!digestmap_iter_done(iter)) {
|
|
|
|
const char *ident;
|
|
|
|
void *val;
|
|
|
|
digestmap_iter_get(iter, &ident, &val);
|
|
|
|
smartlist_add(result, tor_memdup(ident, DIGEST_LEN));
|
|
|
|
iter = digestmap_iter_next(cached_v2_networkstatus, iter);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
SMARTLIST_FOREACH(router_get_trusted_dir_servers(),
|
|
|
|
trusted_dir_server_t *, ds,
|
2007-05-09 06:15:46 +02:00
|
|
|
if (ds->type & V2_AUTHORITY)
|
2007-05-02 11:12:04 +02:00
|
|
|
smartlist_add(result, tor_memdup(ds->digest, DIGEST_LEN)));
|
2006-06-20 02:48:23 +02:00
|
|
|
}
|
2006-06-21 06:57:12 +02:00
|
|
|
smartlist_sort_digests(result);
|
2006-06-20 02:48:23 +02:00
|
|
|
if (smartlist_len(result) == 0)
|
2008-02-06 13:46:46 +01:00
|
|
|
log_info(LD_DIRSERV,
|
2006-06-20 02:48:23 +02:00
|
|
|
"Client requested 'all' network status objects; we have none.");
|
|
|
|
} else if (!strcmpstart(key, "fp/")) {
|
2009-10-18 21:45:57 +02:00
|
|
|
dir_split_resource_into_fingerprints(key+3, result, NULL,
|
|
|
|
DSR_HEX|DSR_SORT_UNIQ);
|
2006-06-20 02:48:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-26 20:02:49 +02:00
|
|
|
/** Look for a network status object as specified by <b>key</b>, which should
|
2005-08-30 08:01:13 +02:00
|
|
|
* be either "authority" (to find a network status generated by us), a hex
|
2005-08-26 20:02:49 +02:00
|
|
|
* identity digest (to find a network status generated by given directory), or
|
2006-04-02 00:00:49 +02:00
|
|
|
* "all" (to return all the v2 network status objects we have).
|
2005-08-26 20:02:49 +02:00
|
|
|
*/
|
2006-04-02 00:00:49 +02:00
|
|
|
void
|
2005-09-07 18:42:53 +02:00
|
|
|
dirserv_get_networkstatus_v2(smartlist_t *result,
|
|
|
|
const char *key)
|
2005-08-25 22:33:17 +02:00
|
|
|
{
|
2006-12-30 03:41:14 +01:00
|
|
|
cached_dir_t *cached;
|
|
|
|
smartlist_t *fingerprints = smartlist_create();
|
2005-09-07 18:42:53 +02:00
|
|
|
tor_assert(result);
|
|
|
|
|
2005-09-08 20:14:23 +02:00
|
|
|
if (!cached_v2_networkstatus)
|
2005-10-18 22:12:22 +02:00
|
|
|
cached_v2_networkstatus = digestmap_new();
|
2005-09-08 20:14:23 +02:00
|
|
|
|
2006-12-30 03:41:14 +01:00
|
|
|
dirserv_get_networkstatus_v2_fingerprints(fingerprints, key);
|
|
|
|
SMARTLIST_FOREACH(fingerprints, const char *, fp,
|
|
|
|
{
|
|
|
|
if (router_digest_is_me(fp) && should_generate_v2_networkstatus())
|
2007-10-22 19:31:26 +02:00
|
|
|
generate_v2_networkstatus_opinion();
|
2006-12-30 03:41:14 +01:00
|
|
|
cached = digestmap_get(cached_v2_networkstatus, fp);
|
|
|
|
if (cached) {
|
|
|
|
smartlist_add(result, cached);
|
|
|
|
} else {
|
|
|
|
char hexbuf[HEX_DIGEST_LEN+1];
|
|
|
|
base16_encode(hexbuf, sizeof(hexbuf), fp, DIGEST_LEN);
|
|
|
|
log_info(LD_DIRSERV, "Don't know about any network status with "
|
|
|
|
"fingerprint '%s'", hexbuf);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
SMARTLIST_FOREACH(fingerprints, char *, cp, tor_free(cp));
|
|
|
|
smartlist_free(fingerprints);
|
2005-08-25 22:33:17 +02:00
|
|
|
}
|
|
|
|
|
2006-06-18 09:38:55 +02:00
|
|
|
/** As dirserv_get_routerdescs(), but instead of getting signed_descriptor_t
|
2007-04-16 20:39:39 +02:00
|
|
|
* pointers, adds copies of digests to fps_out, and doesn't use the
|
|
|
|
* /tor/server/ prefix. For a /d/ request, adds descriptor digests; for other
|
|
|
|
* requests, adds identity digests.
|
2006-06-18 09:38:55 +02:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
dirserv_get_routerdesc_fingerprints(smartlist_t *fps_out, const char *key,
|
2009-01-06 16:37:51 +01:00
|
|
|
const char **msg, int for_unencrypted_conn,
|
|
|
|
int is_extrainfo)
|
2006-06-18 09:38:55 +02:00
|
|
|
{
|
2008-01-10 18:48:40 +01:00
|
|
|
int by_id = 1;
|
2006-06-18 09:38:55 +02:00
|
|
|
*msg = NULL;
|
|
|
|
|
2007-04-16 20:39:39 +02:00
|
|
|
if (!strcmp(key, "all")) {
|
2006-06-18 09:38:55 +02:00
|
|
|
routerlist_t *rl = router_get_routerlist();
|
|
|
|
SMARTLIST_FOREACH(rl->routers, routerinfo_t *, r,
|
|
|
|
smartlist_add(fps_out,
|
|
|
|
tor_memdup(r->cache_info.identity_digest, DIGEST_LEN)));
|
2007-04-16 20:39:39 +02:00
|
|
|
} else if (!strcmp(key, "authority")) {
|
2006-06-18 09:38:55 +02:00
|
|
|
routerinfo_t *ri = router_get_my_routerinfo();
|
|
|
|
if (ri)
|
|
|
|
smartlist_add(fps_out,
|
|
|
|
tor_memdup(ri->cache_info.identity_digest, DIGEST_LEN));
|
2007-04-16 20:39:39 +02:00
|
|
|
} else if (!strcmpstart(key, "d/")) {
|
2008-01-10 18:48:40 +01:00
|
|
|
by_id = 0;
|
2007-04-16 20:39:39 +02:00
|
|
|
key += strlen("d/");
|
2009-10-18 21:45:57 +02:00
|
|
|
dir_split_resource_into_fingerprints(key, fps_out, NULL,
|
|
|
|
DSR_HEX|DSR_SORT_UNIQ);
|
2007-04-16 20:39:39 +02:00
|
|
|
} else if (!strcmpstart(key, "fp/")) {
|
|
|
|
key += strlen("fp/");
|
2009-10-18 21:45:57 +02:00
|
|
|
dir_split_resource_into_fingerprints(key, fps_out, NULL,
|
|
|
|
DSR_HEX|DSR_SORT_UNIQ);
|
2006-06-18 09:38:55 +02:00
|
|
|
} else {
|
|
|
|
*msg = "Key not recognized";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-01-10 18:48:40 +01:00
|
|
|
if (for_unencrypted_conn) {
|
2009-01-06 16:37:51 +01:00
|
|
|
/* Remove anything that insists it not be sent unencrypted. */
|
2008-01-10 18:48:40 +01:00
|
|
|
SMARTLIST_FOREACH(fps_out, char *, cp, {
|
2009-01-06 16:37:51 +01:00
|
|
|
signed_descriptor_t *sd;
|
|
|
|
if (by_id)
|
|
|
|
sd = get_signed_descriptor_by_fp(cp,is_extrainfo,0);
|
|
|
|
else if (is_extrainfo)
|
|
|
|
sd = extrainfo_get_by_descriptor_digest(cp);
|
|
|
|
else
|
|
|
|
sd = router_get_by_descriptor_digest(cp);
|
2008-01-10 18:48:40 +01:00
|
|
|
if (sd && !sd->send_unencrypted) {
|
|
|
|
tor_free(cp);
|
|
|
|
SMARTLIST_DEL_CURRENT(fps_out, cp);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2006-06-18 09:38:55 +02:00
|
|
|
if (!smartlist_len(fps_out)) {
|
|
|
|
*msg = "Servers unavailable";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-11-05 21:15:27 +01:00
|
|
|
/** Add a signed_descriptor_t to <b>descs_out</b> for each router matching
|
2005-10-18 16:33:19 +02:00
|
|
|
* <b>key</b>. The key should be either
|
|
|
|
* - "/tor/server/authority" for our own routerinfo;
|
|
|
|
* - "/tor/server/all" for all the routerinfos we have, concatenated;
|
|
|
|
* - "/tor/server/fp/FP" where FP is a plus-separated sequence of
|
|
|
|
* hex identity digests; or
|
2005-10-18 16:34:49 +02:00
|
|
|
* - "/tor/server/d/D" where D is a plus-separated sequence
|
2005-10-18 16:33:19 +02:00
|
|
|
* of server descriptor digests, in hex.
|
2005-10-18 19:09:57 +02:00
|
|
|
*
|
2005-11-01 04:48:51 +01:00
|
|
|
* Return 0 if we found some matching descriptors, or -1 if we do not
|
|
|
|
* have any descriptors, no matching descriptors, or if we did not
|
|
|
|
* recognize the key (URL).
|
|
|
|
* If -1 is returned *<b>msg</b> will be set to an appropriate error
|
|
|
|
* message.
|
2006-03-15 06:06:56 +01:00
|
|
|
*
|
2008-12-18 17:11:24 +01:00
|
|
|
* XXXX rename this function. It's only called from the controller.
|
2009-05-27 23:55:51 +02:00
|
|
|
* XXXX in fact, refactor this function, merging as much as possible.
|
2005-08-26 20:02:49 +02:00
|
|
|
*/
|
2005-10-18 19:09:57 +02:00
|
|
|
int
|
|
|
|
dirserv_get_routerdescs(smartlist_t *descs_out, const char *key,
|
2008-01-10 18:48:40 +01:00
|
|
|
const char **msg)
|
2005-08-25 22:33:17 +02:00
|
|
|
{
|
2005-10-18 19:09:57 +02:00
|
|
|
*msg = NULL;
|
|
|
|
|
2005-08-25 22:33:17 +02:00
|
|
|
if (!strcmp(key, "/tor/server/all")) {
|
2005-10-18 19:43:54 +02:00
|
|
|
routerlist_t *rl = router_get_routerlist();
|
2005-11-05 21:15:27 +01:00
|
|
|
SMARTLIST_FOREACH(rl->routers, routerinfo_t *, r,
|
|
|
|
smartlist_add(descs_out, &(r->cache_info)));
|
2005-08-25 22:33:17 +02:00
|
|
|
} else if (!strcmp(key, "/tor/server/authority")) {
|
|
|
|
routerinfo_t *ri = router_get_my_routerinfo();
|
|
|
|
if (ri)
|
2005-11-05 21:15:27 +01:00
|
|
|
smartlist_add(descs_out, &(ri->cache_info));
|
2005-10-14 06:56:20 +02:00
|
|
|
} else if (!strcmpstart(key, "/tor/server/d/")) {
|
|
|
|
smartlist_t *digests = smartlist_create();
|
|
|
|
key += strlen("/tor/server/d/");
|
2009-10-18 21:45:57 +02:00
|
|
|
dir_split_resource_into_fingerprints(key, digests, NULL,
|
|
|
|
DSR_HEX|DSR_SORT_UNIQ);
|
2005-10-14 06:56:20 +02:00
|
|
|
SMARTLIST_FOREACH(digests, const char *, d,
|
|
|
|
{
|
2005-11-05 21:15:27 +01:00
|
|
|
signed_descriptor_t *sd = router_get_by_descriptor_digest(d);
|
|
|
|
if (sd)
|
|
|
|
smartlist_add(descs_out,sd);
|
2005-10-14 06:56:20 +02:00
|
|
|
});
|
|
|
|
SMARTLIST_FOREACH(digests, char *, d, tor_free(d));
|
|
|
|
smartlist_free(digests);
|
2005-08-25 22:33:17 +02:00
|
|
|
} else if (!strcmpstart(key, "/tor/server/fp/")) {
|
|
|
|
smartlist_t *digests = smartlist_create();
|
2006-03-08 07:29:52 +01:00
|
|
|
time_t cutoff = time(NULL) - ROUTER_MAX_AGE_TO_PUBLISH;
|
2005-08-25 22:33:17 +02:00
|
|
|
key += strlen("/tor/server/fp/");
|
2009-10-18 21:45:57 +02:00
|
|
|
dir_split_resource_into_fingerprints(key, digests, NULL,
|
|
|
|
DSR_HEX|DSR_SORT_UNIQ);
|
2005-09-08 23:01:24 +02:00
|
|
|
SMARTLIST_FOREACH(digests, const char *, d,
|
|
|
|
{
|
|
|
|
if (router_digest_is_me(d)) {
|
2008-02-24 23:11:18 +01:00
|
|
|
/* make sure desc_routerinfo exists */
|
|
|
|
routerinfo_t *ri = router_get_my_routerinfo();
|
|
|
|
if (ri)
|
|
|
|
smartlist_add(descs_out, &(ri->cache_info));
|
2005-09-08 23:01:24 +02:00
|
|
|
} else {
|
|
|
|
routerinfo_t *ri = router_get_by_digest(d);
|
2006-01-10 05:57:12 +01:00
|
|
|
/* Don't actually serve a descriptor that everyone will think is
|
|
|
|
* expired. This is an (ugly) workaround to keep buggy 0.1.1.10
|
|
|
|
* Tors from downloading descriptors that they will throw away.
|
|
|
|
*/
|
|
|
|
if (ri && ri->cache_info.published_on > cutoff)
|
2005-11-05 21:15:27 +01:00
|
|
|
smartlist_add(descs_out, &(ri->cache_info));
|
2005-09-08 23:01:24 +02:00
|
|
|
}
|
|
|
|
});
|
2005-08-25 22:33:17 +02:00
|
|
|
SMARTLIST_FOREACH(digests, char *, d, tor_free(d));
|
|
|
|
smartlist_free(digests);
|
2005-10-18 19:09:57 +02:00
|
|
|
} else {
|
|
|
|
*msg = "Key not recognized";
|
|
|
|
return -1;
|
2004-06-25 02:29:31 +02:00
|
|
|
}
|
2005-10-18 19:09:57 +02:00
|
|
|
|
|
|
|
if (!smartlist_len(descs_out)) {
|
|
|
|
*msg = "Servers unavailable";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
2004-06-16 23:08:29 +02:00
|
|
|
}
|
|
|
|
|
2005-04-06 23:09:47 +02:00
|
|
|
/** Called when a TLS handshake has completed successfully with a
|
|
|
|
* router listening at <b>address</b>:<b>or_port</b>, and has yielded
|
2007-02-24 02:26:09 +01:00
|
|
|
* a certificate with digest <b>digest_rcvd</b>.
|
2005-06-21 01:04:13 +02:00
|
|
|
*
|
|
|
|
* Also, if as_advertised is 1, then inform the reachability checker
|
|
|
|
* that we could get to this guy.
|
2005-04-06 23:09:47 +02:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
dirserv_orconn_tls_done(const char *address,
|
|
|
|
uint16_t or_port,
|
|
|
|
const char *digest_rcvd,
|
2005-08-24 04:31:02 +02:00
|
|
|
int as_advertised)
|
2005-04-06 23:09:47 +02:00
|
|
|
{
|
2005-10-18 19:43:54 +02:00
|
|
|
routerlist_t *rl = router_get_routerlist();
|
2007-08-17 03:29:58 +02:00
|
|
|
time_t now = time(NULL);
|
2007-12-18 22:37:58 +01:00
|
|
|
int bridge_auth = authdir_mode_bridge(get_options());
|
2005-04-06 23:09:47 +02:00
|
|
|
tor_assert(address);
|
|
|
|
tor_assert(digest_rcvd);
|
|
|
|
|
2007-01-02 22:47:21 +01:00
|
|
|
SMARTLIST_FOREACH(rl->routers, routerinfo_t *, ri, {
|
|
|
|
if (!strcasecmp(address, ri->address) && or_port == ri->or_port &&
|
2007-01-05 07:03:10 +01:00
|
|
|
as_advertised &&
|
2007-02-24 02:26:09 +01:00
|
|
|
!memcmp(ri->cache_info.identity_digest, digest_rcvd, DIGEST_LEN)) {
|
|
|
|
/* correct digest. mark this router reachable! */
|
2007-12-18 22:37:58 +01:00
|
|
|
if (!bridge_auth || ri->purpose == ROUTER_PURPOSE_BRIDGE) {
|
|
|
|
log_info(LD_DIRSERV, "Found router %s to be reachable. Yay.",
|
|
|
|
ri->nickname);
|
|
|
|
rep_hist_note_router_reachable(digest_rcvd, now);
|
|
|
|
ri->last_reachable = now;
|
|
|
|
}
|
2005-04-06 23:09:47 +02:00
|
|
|
}
|
2007-01-02 22:47:21 +01:00
|
|
|
});
|
|
|
|
/* FFFF Maybe we should reinstate the code that dumps routers with the same
|
|
|
|
* addr/port but with nonmatching keys, but instead of dumping, we should
|
|
|
|
* skip testing. */
|
2005-04-06 23:09:47 +02:00
|
|
|
}
|
|
|
|
|
2010-04-21 09:12:14 +02:00
|
|
|
/** Helper function for dirserv_test_reachability(). Start a TLS
|
|
|
|
* connection to <b>router</b>, and annotate it with when we started
|
|
|
|
* the test. */
|
|
|
|
void
|
|
|
|
dirserv_single_reachability_test(time_t now, routerinfo_t *router)
|
|
|
|
{
|
|
|
|
tor_addr_t router_addr;
|
|
|
|
log_debug(LD_OR,"Testing reachability of %s at %s:%u.",
|
|
|
|
router->nickname, router->address, router->or_port);
|
|
|
|
/* Remember when we started trying to determine reachability */
|
|
|
|
if (!router->testing_since)
|
|
|
|
router->testing_since = now;
|
|
|
|
tor_addr_from_ipv4h(&router_addr, router->addr);
|
|
|
|
connection_or_connect(&router_addr, router->or_port,
|
|
|
|
router->cache_info.identity_digest);
|
|
|
|
}
|
|
|
|
|
2010-04-21 10:27:56 +02:00
|
|
|
/** Auth dir server only: load balance such that we only
|
2006-07-04 05:31:27 +02:00
|
|
|
* try a few connections per call.
|
|
|
|
*
|
|
|
|
* The load balancing is such that if we get called once every ten
|
|
|
|
* seconds, we will cycle through all the tests in 1280 seconds (a
|
|
|
|
* bit over 20 minutes).
|
|
|
|
*/
|
|
|
|
void
|
2010-04-21 10:27:56 +02:00
|
|
|
dirserv_test_reachability(time_t now)
|
2006-07-04 05:31:27 +02:00
|
|
|
{
|
2007-03-01 05:40:43 +01:00
|
|
|
/* XXX decide what to do here; see or-talk thread "purging old router
|
|
|
|
* information, revocation." -NM
|
|
|
|
* We can't afford to mess with this in 0.1.2.x. The reason is that
|
|
|
|
* if we stop doing reachability tests on some of routerlist, then
|
|
|
|
* we'll for-sure think they're down, which may have unexpected
|
|
|
|
* effects in other parts of the code. It doesn't hurt much to do
|
|
|
|
* the testing, and directory authorities are easy to upgrade. Let's
|
|
|
|
* wait til 0.2.0. -RD */
|
2007-02-28 22:06:05 +01:00
|
|
|
// time_t cutoff = now - ROUTER_MAX_AGE_TO_PUBLISH;
|
2006-07-04 05:31:27 +02:00
|
|
|
routerlist_t *rl = router_get_routerlist();
|
|
|
|
static char ctr = 0;
|
2007-12-18 22:37:58 +01:00
|
|
|
int bridge_auth = authdir_mode_bridge(get_options());
|
2006-07-04 05:31:27 +02:00
|
|
|
|
2008-08-05 22:08:19 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(rl->routers, routerinfo_t *, router) {
|
2006-07-04 05:31:27 +02:00
|
|
|
const char *id_digest = router->cache_info.identity_digest;
|
|
|
|
if (router_is_me(router))
|
|
|
|
continue;
|
2007-12-18 22:37:58 +01:00
|
|
|
if (bridge_auth && router->purpose != ROUTER_PURPOSE_BRIDGE)
|
|
|
|
continue; /* bridge authorities only test reachability on bridges */
|
2007-02-28 22:06:05 +01:00
|
|
|
// if (router->cache_info.published_on > cutoff)
|
|
|
|
// continue;
|
2010-04-21 10:27:56 +02:00
|
|
|
if ((((uint8_t)id_digest[0]) % 128) == ctr) {
|
2010-04-21 09:12:14 +02:00
|
|
|
dirserv_single_reachability_test(now, router);
|
2006-07-04 05:31:27 +02:00
|
|
|
}
|
2008-08-05 22:08:19 +02:00
|
|
|
} SMARTLIST_FOREACH_END(router);
|
2010-04-21 10:27:56 +02:00
|
|
|
ctr = (ctr + 1) % 128; /* increment ctr */
|
2006-07-04 05:31:27 +02:00
|
|
|
}
|
|
|
|
|
2009-10-18 21:45:57 +02:00
|
|
|
/** Given a fingerprint <b>fp</b> which is either set if we're looking for a
|
|
|
|
* v2 status, or zeroes if we're looking for a v3 status, or a NUL-padded
|
|
|
|
* flavor name if we want a flavored v3 status, return a pointer to the
|
|
|
|
* appropriate cached dir object, or NULL if there isn't one available. */
|
2007-12-24 12:25:45 +01:00
|
|
|
static cached_dir_t *
|
|
|
|
lookup_cached_dir_by_fp(const char *fp)
|
|
|
|
{
|
|
|
|
cached_dir_t *d = NULL;
|
2009-10-13 23:06:01 +02:00
|
|
|
if (tor_digest_is_zero(fp) && cached_consensuses)
|
|
|
|
d = strmap_get(cached_consensuses, "ns");
|
2009-10-18 21:45:57 +02:00
|
|
|
else if (memchr(fp, '\0', DIGEST_LEN) && cached_consensuses &&
|
|
|
|
(d = strmap_get(cached_consensuses, fp))) {
|
|
|
|
/* this here interface is a nasty hack XXXX022 */;
|
|
|
|
} else if (router_digest_is_me(fp) && the_v2_networkstatus)
|
2007-12-24 12:25:45 +01:00
|
|
|
d = the_v2_networkstatus;
|
|
|
|
else if (cached_v2_networkstatus)
|
|
|
|
d = digestmap_get(cached_v2_networkstatus, fp);
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2007-06-15 06:20:51 +02:00
|
|
|
/** Remove from <b>fps</b> every networkstatus key where both
|
|
|
|
* a) we have a networkstatus document and
|
|
|
|
* b) it is not newer than <b>cutoff</b>.
|
|
|
|
*
|
2007-08-14 22:19:58 +02:00
|
|
|
* Return 1 if any items were present at all; else return 0.
|
2007-06-15 06:20:51 +02:00
|
|
|
*/
|
2007-06-02 17:26:57 +02:00
|
|
|
int
|
2007-06-15 06:23:36 +02:00
|
|
|
dirserv_remove_old_statuses(smartlist_t *fps, time_t cutoff)
|
2007-06-02 17:26:57 +02:00
|
|
|
{
|
2007-08-14 22:19:58 +02:00
|
|
|
int found_any = 0;
|
2007-06-15 06:20:51 +02:00
|
|
|
SMARTLIST_FOREACH(fps, char *, digest,
|
2007-06-02 17:26:57 +02:00
|
|
|
{
|
2007-12-24 12:25:45 +01:00
|
|
|
cached_dir_t *d = lookup_cached_dir_by_fp(digest);
|
2007-08-14 22:19:58 +02:00
|
|
|
if (!d)
|
|
|
|
continue;
|
|
|
|
found_any = 1;
|
|
|
|
if (d->published <= cutoff) {
|
2007-06-15 06:20:51 +02:00
|
|
|
tor_free(digest);
|
|
|
|
SMARTLIST_DEL_CURRENT(fps, digest);
|
|
|
|
}
|
2007-06-02 17:26:57 +02:00
|
|
|
});
|
|
|
|
|
2007-08-14 22:19:58 +02:00
|
|
|
return found_any;
|
|
|
|
}
|
|
|
|
|
2007-09-21 00:08:40 +02:00
|
|
|
/** Return the cache-info for identity fingerprint <b>fp</b>, or
|
|
|
|
* its extra-info document if <b>extrainfo</b> is true. Return
|
|
|
|
* NULL if not found or if the descriptor is older than
|
|
|
|
* <b>publish_cutoff</b>. */
|
|
|
|
static signed_descriptor_t *
|
|
|
|
get_signed_descriptor_by_fp(const char *fp, int extrainfo,
|
|
|
|
time_t publish_cutoff)
|
|
|
|
{
|
|
|
|
if (router_digest_is_me(fp)) {
|
|
|
|
if (extrainfo)
|
|
|
|
return &(router_get_my_extrainfo()->cache_info);
|
|
|
|
else
|
|
|
|
return &(router_get_my_routerinfo()->cache_info);
|
|
|
|
} else {
|
|
|
|
routerinfo_t *ri = router_get_by_digest(fp);
|
|
|
|
if (ri &&
|
|
|
|
ri->cache_info.published_on > publish_cutoff) {
|
|
|
|
if (extrainfo)
|
|
|
|
return extrainfo_get_by_descriptor_digest(
|
|
|
|
ri->cache_info.extra_info_digest);
|
|
|
|
else
|
|
|
|
return &ri->cache_info;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-05-27 23:55:51 +02:00
|
|
|
/** Return true iff we have any of the documents (extrainfo or routerdesc)
|
2007-08-22 17:07:44 +02:00
|
|
|
* specified by the fingerprints in <b>fps</b> and <b>spool_src</b>. Used to
|
|
|
|
* decide whether to send a 404. */
|
2007-08-14 22:19:58 +02:00
|
|
|
int
|
|
|
|
dirserv_have_any_serverdesc(smartlist_t *fps, int spool_src)
|
|
|
|
{
|
2007-09-21 00:08:40 +02:00
|
|
|
time_t publish_cutoff = time(NULL)-ROUTER_MAX_AGE_TO_PUBLISH;
|
2007-08-14 22:19:58 +02:00
|
|
|
SMARTLIST_FOREACH(fps, const char *, fp, {
|
|
|
|
switch (spool_src)
|
|
|
|
{
|
|
|
|
case DIR_SPOOL_EXTRA_BY_DIGEST:
|
|
|
|
if (extrainfo_get_by_descriptor_digest(fp)) return 1;
|
|
|
|
break;
|
|
|
|
case DIR_SPOOL_SERVER_BY_DIGEST:
|
|
|
|
if (router_get_by_descriptor_digest(fp)) return 1;
|
|
|
|
break;
|
2007-09-21 00:08:40 +02:00
|
|
|
case DIR_SPOOL_EXTRA_BY_FP:
|
2007-08-14 22:19:58 +02:00
|
|
|
case DIR_SPOOL_SERVER_BY_FP:
|
2007-09-21 00:08:40 +02:00
|
|
|
if (get_signed_descriptor_by_fp(fp,
|
|
|
|
spool_src == DIR_SPOOL_EXTRA_BY_FP, publish_cutoff))
|
|
|
|
return 1;
|
2007-08-14 22:19:58 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return 0;
|
2007-06-02 17:26:57 +02:00
|
|
|
}
|
|
|
|
|
2009-10-18 21:45:57 +02:00
|
|
|
/** Return true iff any of the 256-bit elements in <b>fps</b> is the digest of
|
|
|
|
* a microdescriptor we have. */
|
|
|
|
int
|
|
|
|
dirserv_have_any_microdesc(const smartlist_t *fps)
|
|
|
|
{
|
|
|
|
microdesc_cache_t *cache = get_microdesc_cache();
|
|
|
|
SMARTLIST_FOREACH(fps, const char *, fp,
|
|
|
|
if (microdesc_cache_lookup_by_digest256(cache, fp))
|
|
|
|
return 1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-01-05 07:03:10 +01:00
|
|
|
/** Return an approximate estimate of the number of bytes that will
|
|
|
|
* be needed to transmit the server descriptors (if is_serverdescs --
|
|
|
|
* they can be either d/ or fp/ queries) or networkstatus objects (if
|
|
|
|
* !is_serverdescs) listed in <b>fps</b>. If <b>compressed</b> is set,
|
|
|
|
* we guess how large the data will be after compression.
|
2007-01-03 20:58:00 +01:00
|
|
|
*
|
|
|
|
* The return value is an estimate; it might be larger or smaller.
|
|
|
|
**/
|
|
|
|
size_t
|
|
|
|
dirserv_estimate_data_size(smartlist_t *fps, int is_serverdescs,
|
|
|
|
int compressed)
|
|
|
|
{
|
|
|
|
size_t result;
|
2007-01-05 07:30:31 +01:00
|
|
|
tor_assert(fps);
|
2007-01-03 20:58:00 +01:00
|
|
|
if (is_serverdescs) {
|
|
|
|
int n = smartlist_len(fps);
|
|
|
|
routerinfo_t *me = router_get_my_routerinfo();
|
|
|
|
result = (me?me->cache_info.signed_descriptor_len:2048) * n;
|
|
|
|
if (compressed)
|
2009-05-27 23:55:51 +02:00
|
|
|
result /= 2; /* observed compressibility is between 35 and 55%. */
|
2007-01-03 20:58:00 +01:00
|
|
|
} else {
|
|
|
|
result = 0;
|
2007-12-24 12:25:45 +01:00
|
|
|
SMARTLIST_FOREACH(fps, const char *, digest, {
|
|
|
|
cached_dir_t *dir = lookup_cached_dir_by_fp(digest);
|
2007-01-03 20:58:00 +01:00
|
|
|
if (dir)
|
|
|
|
result += compressed ? dir->dir_z_len : dir->dir_len;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-10-18 21:45:57 +02:00
|
|
|
/** Given a list of microdescriptor hashes, guess how many bytes will be
|
|
|
|
* needed to transmit them, and return the guess. */
|
|
|
|
size_t
|
|
|
|
dirserv_estimate_microdesc_size(const smartlist_t *fps, int compressed)
|
|
|
|
{
|
|
|
|
size_t result = smartlist_len(fps) * microdesc_average_size(NULL);
|
|
|
|
if (compressed)
|
|
|
|
result /= 2;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2006-06-18 09:38:55 +02:00
|
|
|
/** When we're spooling data onto our outbuf, add more whenever we dip
|
|
|
|
* below this threshold. */
|
|
|
|
#define DIRSERV_BUFFER_MIN 16384
|
|
|
|
|
2007-01-23 20:22:52 +01:00
|
|
|
/** Spooling helper: called when we have no more data to spool to <b>conn</b>.
|
|
|
|
* Flushes any remaining data to be (un)compressed, and changes the spool
|
|
|
|
* source to NONE. Returns 0 on success, negative on failure. */
|
2006-06-20 02:48:23 +02:00
|
|
|
static int
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_dirserv_finish_spooling(dir_connection_t *conn)
|
2006-06-20 02:48:23 +02:00
|
|
|
{
|
|
|
|
if (conn->zlib_state) {
|
2006-12-29 06:07:04 +01:00
|
|
|
connection_write_to_buf_zlib("", 0, conn, 1);
|
2006-06-20 02:48:23 +02:00
|
|
|
tor_zlib_free(conn->zlib_state);
|
|
|
|
conn->zlib_state = NULL;
|
|
|
|
}
|
|
|
|
conn->dir_spool_src = DIR_SPOOL_NONE;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-01-23 20:22:52 +01:00
|
|
|
/** Spooling helper: called when we're sending a bunch of server descriptors,
|
|
|
|
* and the outbuf has become too empty. Pulls some entries from
|
|
|
|
* fingerprint_stack, and writes the corresponding servers onto outbuf. If we
|
|
|
|
* run out of entries, flushes the zlib state and sets the spool source to
|
|
|
|
* NONE. Returns 0 on success, negative on failure.
|
|
|
|
*/
|
2006-06-18 09:38:55 +02:00
|
|
|
static int
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_dirserv_add_servers_to_outbuf(dir_connection_t *conn)
|
2006-06-18 09:38:55 +02:00
|
|
|
{
|
2008-04-04 23:18:56 +02:00
|
|
|
#ifdef TRACK_SERVED_TIME
|
2008-03-29 05:27:34 +01:00
|
|
|
time_t now = time(NULL);
|
2008-04-04 23:18:56 +02:00
|
|
|
#endif
|
2007-04-16 20:39:39 +02:00
|
|
|
int by_fp = (conn->dir_spool_src == DIR_SPOOL_SERVER_BY_FP ||
|
|
|
|
conn->dir_spool_src == DIR_SPOOL_EXTRA_BY_FP);
|
|
|
|
int extra = (conn->dir_spool_src == DIR_SPOOL_EXTRA_BY_FP ||
|
|
|
|
conn->dir_spool_src == DIR_SPOOL_EXTRA_BY_DIGEST);
|
|
|
|
time_t publish_cutoff = time(NULL)-ROUTER_MAX_AGE_TO_PUBLISH;
|
2006-06-18 09:38:55 +02:00
|
|
|
|
|
|
|
while (smartlist_len(conn->fingerprint_stack) &&
|
2006-07-26 21:07:26 +02:00
|
|
|
buf_datalen(conn->_base.outbuf) < DIRSERV_BUFFER_MIN) {
|
2006-06-22 09:34:04 +02:00
|
|
|
const char *body;
|
2006-06-18 09:38:55 +02:00
|
|
|
char *fp = smartlist_pop_last(conn->fingerprint_stack);
|
|
|
|
signed_descriptor_t *sd = NULL;
|
2006-06-18 10:07:16 +02:00
|
|
|
if (by_fp) {
|
2007-09-21 00:08:40 +02:00
|
|
|
sd = get_signed_descriptor_by_fp(fp, extra, publish_cutoff);
|
2007-04-16 20:39:39 +02:00
|
|
|
} else {
|
|
|
|
sd = extra ? extrainfo_get_by_descriptor_digest(fp)
|
|
|
|
: router_get_by_descriptor_digest(fp);
|
|
|
|
}
|
2006-06-18 09:38:55 +02:00
|
|
|
tor_free(fp);
|
|
|
|
if (!sd)
|
|
|
|
continue;
|
2008-01-10 18:48:40 +01:00
|
|
|
if (!connection_dir_is_encrypted(conn) && !sd->send_unencrypted) {
|
|
|
|
/* we did this check once before (so we could have an accurate size
|
|
|
|
* estimate and maybe send a 404 if somebody asked for only bridges on a
|
|
|
|
* connection), but we need to do it again in case a previously
|
|
|
|
* unknown bridge descriptor has shown up between then and now. */
|
|
|
|
continue;
|
|
|
|
}
|
2008-04-04 23:18:56 +02:00
|
|
|
#ifdef TRACK_SERVED_TIME
|
2008-03-29 05:27:34 +01:00
|
|
|
sd->last_served_at = now;
|
2008-04-04 23:18:56 +02:00
|
|
|
#endif
|
2006-06-22 09:01:54 +02:00
|
|
|
body = signed_descriptor_get_body(sd);
|
2006-06-18 09:38:55 +02:00
|
|
|
if (conn->zlib_state) {
|
2009-10-18 21:45:57 +02:00
|
|
|
/* XXXX022 This 'last' business should actually happen on the last
|
|
|
|
* routerinfo, not on the last fingerprint. */
|
2006-06-29 13:19:52 +02:00
|
|
|
int last = ! smartlist_len(conn->fingerprint_stack);
|
2006-12-29 06:07:04 +01:00
|
|
|
connection_write_to_buf_zlib(body, sd->signed_descriptor_len, conn,
|
|
|
|
last);
|
2006-06-29 13:19:52 +02:00
|
|
|
if (last) {
|
2006-07-06 04:45:46 +02:00
|
|
|
tor_zlib_free(conn->zlib_state);
|
|
|
|
conn->zlib_state = NULL;
|
2006-06-29 13:19:52 +02:00
|
|
|
}
|
2006-06-18 09:38:55 +02:00
|
|
|
} else {
|
2006-06-22 09:01:54 +02:00
|
|
|
connection_write_to_buf(body,
|
2006-06-18 10:46:55 +02:00
|
|
|
sd->signed_descriptor_len,
|
2006-07-26 21:07:26 +02:00
|
|
|
TO_CONN(conn));
|
2006-06-18 09:38:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!smartlist_len(conn->fingerprint_stack)) {
|
|
|
|
/* We just wrote the last one; finish up. */
|
2006-06-29 13:19:52 +02:00
|
|
|
conn->dir_spool_src = DIR_SPOOL_NONE;
|
2006-06-18 09:38:55 +02:00
|
|
|
smartlist_free(conn->fingerprint_stack);
|
|
|
|
conn->fingerprint_stack = NULL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-10-18 21:45:57 +02:00
|
|
|
/** Spooling helper: called when we're sending a bunch of microdescriptors,
|
|
|
|
* and the outbuf has become too empty. Pulls some entries from
|
|
|
|
* fingerprint_stack, and writes the corresponding microdescs onto outbuf. If
|
|
|
|
* we run out of entries, flushes the zlib state and sets the spool source to
|
|
|
|
* NONE. Returns 0 on success, negative on failure.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
connection_dirserv_add_microdescs_to_outbuf(dir_connection_t *conn)
|
|
|
|
{
|
|
|
|
microdesc_cache_t *cache = get_microdesc_cache();
|
|
|
|
while (smartlist_len(conn->fingerprint_stack) &&
|
|
|
|
buf_datalen(conn->_base.outbuf) < DIRSERV_BUFFER_MIN) {
|
|
|
|
char *fp256 = smartlist_pop_last(conn->fingerprint_stack);
|
|
|
|
microdesc_t *md = microdesc_cache_lookup_by_digest256(cache, fp256);
|
|
|
|
tor_free(fp256);
|
|
|
|
if (!md)
|
|
|
|
continue;
|
|
|
|
if (conn->zlib_state) {
|
|
|
|
/* XXXX022 This 'last' business should actually happen on the last
|
|
|
|
* routerinfo, not on the last fingerprint. */
|
|
|
|
int last = !smartlist_len(conn->fingerprint_stack);
|
|
|
|
connection_write_to_buf_zlib(md->body, md->bodylen, conn, last);
|
|
|
|
if (last) {
|
|
|
|
tor_zlib_free(conn->zlib_state);
|
|
|
|
conn->zlib_state = NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
connection_write_to_buf(md->body, md->bodylen, TO_CONN(conn));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!smartlist_len(conn->fingerprint_stack)) {
|
|
|
|
conn->dir_spool_src = DIR_SPOOL_NONE;
|
|
|
|
smartlist_free(conn->fingerprint_stack);
|
|
|
|
conn->fingerprint_stack = NULL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-01-23 20:22:52 +01:00
|
|
|
/** Spooling helper: Called when we're sending a directory or networkstatus,
|
|
|
|
* and the outbuf has become too empty. Pulls some bytes from
|
|
|
|
* <b>conn</b>-\>cached_dir-\>dir_z, uncompresses them if appropriate, and
|
|
|
|
* puts them on the outbuf. If we run out of entries, flushes the zlib state
|
|
|
|
* and sets the spool source to NONE. Returns 0 on success, negative on
|
|
|
|
* failure. */
|
2006-06-18 09:38:55 +02:00
|
|
|
static int
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_dirserv_add_dir_bytes_to_outbuf(dir_connection_t *conn)
|
2006-06-18 09:38:55 +02:00
|
|
|
{
|
2006-11-14 02:07:52 +01:00
|
|
|
ssize_t bytes;
|
|
|
|
int64_t remaining;
|
2006-06-18 09:38:55 +02:00
|
|
|
|
2006-07-26 21:07:26 +02:00
|
|
|
bytes = DIRSERV_BUFFER_MIN - buf_datalen(conn->_base.outbuf);
|
2006-06-18 09:38:55 +02:00
|
|
|
tor_assert(bytes > 0);
|
2006-06-18 22:58:27 +02:00
|
|
|
tor_assert(conn->cached_dir);
|
2006-06-18 09:38:55 +02:00
|
|
|
if (bytes < 8192)
|
|
|
|
bytes = 8192;
|
|
|
|
remaining = conn->cached_dir->dir_z_len - conn->cached_dir_offset;
|
|
|
|
if (bytes > remaining)
|
2006-11-14 02:07:52 +01:00
|
|
|
bytes = (ssize_t) remaining;
|
2006-06-18 09:38:55 +02:00
|
|
|
|
|
|
|
if (conn->zlib_state) {
|
2006-12-29 06:07:04 +01:00
|
|
|
connection_write_to_buf_zlib(
|
2006-06-18 10:46:55 +02:00
|
|
|
conn->cached_dir->dir_z + conn->cached_dir_offset,
|
2006-12-29 06:07:04 +01:00
|
|
|
bytes, conn, bytes == remaining);
|
2006-06-18 09:38:55 +02:00
|
|
|
} else {
|
2006-06-18 10:46:55 +02:00
|
|
|
connection_write_to_buf(conn->cached_dir->dir_z + conn->cached_dir_offset,
|
2006-07-26 21:07:26 +02:00
|
|
|
bytes, TO_CONN(conn));
|
2006-06-18 09:38:55 +02:00
|
|
|
}
|
|
|
|
conn->cached_dir_offset += bytes;
|
2006-06-18 22:58:27 +02:00
|
|
|
if (conn->cached_dir_offset == (int)conn->cached_dir->dir_z_len) {
|
2006-06-18 09:38:55 +02:00
|
|
|
/* We just wrote the last one; finish up. */
|
2006-06-20 08:27:13 +02:00
|
|
|
connection_dirserv_finish_spooling(conn);
|
2006-06-18 09:38:55 +02:00
|
|
|
cached_dir_decref(conn->cached_dir);
|
|
|
|
conn->cached_dir = NULL;
|
2006-06-20 02:48:23 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-02 21:06:43 +01:00
|
|
|
/** Spooling helper: Called when we're spooling networkstatus objects on
|
2007-01-23 20:22:52 +01:00
|
|
|
* <b>conn</b>, and the outbuf has become too empty. If the current
|
|
|
|
* networkstatus object (in <b>conn</b>-\>cached_dir) has more data, pull data
|
|
|
|
* from there. Otherwise, pop the next fingerprint from fingerprint_stack,
|
2007-10-11 18:06:51 +02:00
|
|
|
* and start spooling the next networkstatus. (A digest of all 0 bytes is
|
|
|
|
* treated as a request for the current consensus.) If we run out of entries,
|
2007-01-23 20:22:52 +01:00
|
|
|
* flushes the zlib state and sets the spool source to NONE. Returns 0 on
|
|
|
|
* success, negative on failure. */
|
2006-06-20 02:48:23 +02:00
|
|
|
static int
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_dirserv_add_networkstatus_bytes_to_outbuf(dir_connection_t *conn)
|
2006-06-20 02:48:23 +02:00
|
|
|
{
|
2006-06-20 08:27:13 +02:00
|
|
|
|
2006-07-26 21:07:26 +02:00
|
|
|
while (buf_datalen(conn->_base.outbuf) < DIRSERV_BUFFER_MIN) {
|
2006-06-20 02:48:23 +02:00
|
|
|
if (conn->cached_dir) {
|
2006-06-20 08:27:13 +02:00
|
|
|
int uncompressing = (conn->zlib_state != NULL);
|
|
|
|
int r = connection_dirserv_add_dir_bytes_to_outbuf(conn);
|
2006-06-21 01:06:52 +02:00
|
|
|
if (conn->dir_spool_src == DIR_SPOOL_NONE) {
|
2006-06-22 09:25:15 +02:00
|
|
|
/* add_dir_bytes thinks we're done with the cached_dir. But we
|
|
|
|
* may have more cached_dirs! */
|
|
|
|
conn->dir_spool_src = DIR_SPOOL_NETWORKSTATUS;
|
|
|
|
/* This bit is tricky. If we were uncompressing the last
|
|
|
|
* networkstatus, we may need to make a new zlib object to
|
|
|
|
* uncompress the next one. */
|
|
|
|
if (uncompressing && ! conn->zlib_state &&
|
|
|
|
conn->fingerprint_stack &&
|
|
|
|
smartlist_len(conn->fingerprint_stack)) {
|
|
|
|
conn->zlib_state = tor_zlib_new(0, ZLIB_METHOD);
|
|
|
|
}
|
2006-06-21 01:06:52 +02:00
|
|
|
}
|
2006-06-20 08:27:13 +02:00
|
|
|
if (r) return r;
|
2006-06-20 02:48:23 +02:00
|
|
|
} else if (conn->fingerprint_stack &&
|
|
|
|
smartlist_len(conn->fingerprint_stack)) {
|
|
|
|
/* Add another networkstatus; start serving it. */
|
|
|
|
char *fp = smartlist_pop_last(conn->fingerprint_stack);
|
2007-12-24 12:25:45 +01:00
|
|
|
cached_dir_t *d = lookup_cached_dir_by_fp(fp);
|
2006-06-20 02:48:23 +02:00
|
|
|
tor_free(fp);
|
|
|
|
if (d) {
|
|
|
|
++d->refcnt;
|
|
|
|
conn->cached_dir = d;
|
|
|
|
conn->cached_dir_offset = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
connection_dirserv_finish_spooling(conn);
|
2009-12-12 08:07:59 +01:00
|
|
|
smartlist_free(conn->fingerprint_stack);
|
2006-06-20 02:48:23 +02:00
|
|
|
conn->fingerprint_stack = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
2006-06-18 09:38:55 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Called whenever we have flushed some directory data in state
|
|
|
|
* SERVER_WRITING. */
|
|
|
|
int
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_dirserv_flushed_some(dir_connection_t *conn)
|
2006-06-18 09:38:55 +02:00
|
|
|
{
|
2006-07-26 21:07:26 +02:00
|
|
|
tor_assert(conn->_base.state == DIR_CONN_STATE_SERVER_WRITING);
|
2006-06-18 09:38:55 +02:00
|
|
|
|
2006-08-11 09:09:17 +02:00
|
|
|
if (buf_datalen(conn->_base.outbuf) >= DIRSERV_BUFFER_MIN)
|
2006-06-18 09:38:55 +02:00
|
|
|
return 0;
|
|
|
|
|
2006-06-20 02:48:23 +02:00
|
|
|
switch (conn->dir_spool_src) {
|
2007-04-16 20:39:39 +02:00
|
|
|
case DIR_SPOOL_EXTRA_BY_DIGEST:
|
|
|
|
case DIR_SPOOL_EXTRA_BY_FP:
|
2006-06-20 02:48:23 +02:00
|
|
|
case DIR_SPOOL_SERVER_BY_DIGEST:
|
|
|
|
case DIR_SPOOL_SERVER_BY_FP:
|
2006-06-18 09:55:04 +02:00
|
|
|
return connection_dirserv_add_servers_to_outbuf(conn);
|
2009-10-18 21:45:57 +02:00
|
|
|
case DIR_SPOOL_MICRODESC:
|
|
|
|
return connection_dirserv_add_microdescs_to_outbuf(conn);
|
2006-06-20 02:48:23 +02:00
|
|
|
case DIR_SPOOL_CACHED_DIR:
|
2006-06-18 09:55:04 +02:00
|
|
|
return connection_dirserv_add_dir_bytes_to_outbuf(conn);
|
2006-06-20 02:48:23 +02:00
|
|
|
case DIR_SPOOL_NETWORKSTATUS:
|
|
|
|
return connection_dirserv_add_networkstatus_bytes_to_outbuf(conn);
|
2006-08-11 09:09:17 +02:00
|
|
|
case DIR_SPOOL_NONE:
|
2006-06-18 09:57:47 +02:00
|
|
|
default:
|
|
|
|
return 0;
|
2006-06-18 09:38:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-11 20:52:12 +02:00
|
|
|
/** Release all storage used by the directory server. */
|
2005-02-11 00:18:39 +01:00
|
|
|
void
|
|
|
|
dirserv_free_all(void)
|
|
|
|
{
|
2006-10-23 05:48:42 +02:00
|
|
|
dirserv_free_fingerprint_list();
|
|
|
|
|
2006-06-18 09:38:55 +02:00
|
|
|
cached_dir_decref(the_directory);
|
2005-08-25 22:33:17 +02:00
|
|
|
clear_cached_dir(&the_runningrouters);
|
2006-06-20 02:48:23 +02:00
|
|
|
cached_dir_decref(the_v2_networkstatus);
|
2006-06-18 09:38:55 +02:00
|
|
|
cached_dir_decref(cached_directory);
|
2005-08-25 22:33:17 +02:00
|
|
|
clear_cached_dir(&cached_runningrouters);
|
2009-12-12 08:07:59 +01:00
|
|
|
|
|
|
|
digestmap_free(cached_v2_networkstatus, _free_cached_dir);
|
|
|
|
cached_v2_networkstatus = NULL;
|
|
|
|
strmap_free(cached_consensuses, _free_cached_dir);
|
|
|
|
cached_consensuses = NULL;
|
2005-02-11 00:18:39 +01:00
|
|
|
}
|
2005-06-09 21:03:31 +02:00
|
|
|
|