2006-02-09 06:46:49 +01:00
|
|
|
/* Copyright (c) 2001 Matej Pfajfar.
|
|
|
|
* Copyright (c) 2001-2004, Roger Dingledine.
|
2007-12-12 22:09:01 +01:00
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2008-02-07 06:31:47 +01:00
|
|
|
* Copyright (c) 2007-2008, The Tor Project, Inc. */
|
2004-05-13 09:24:49 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
/* $Id$ */
|
2005-12-14 21:40:40 +01:00
|
|
|
const char circuitbuild_c_id[] =
|
|
|
|
"$Id$";
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \file circuitbuild.c
|
|
|
|
* \brief The actual details of building circuits.
|
|
|
|
**/
|
|
|
|
|
|
|
|
#include "or.h"
|
|
|
|
|
|
|
|
/********* START VARIABLES **********/
|
|
|
|
|
|
|
|
/** A global list of all circuits at this hop. */
|
|
|
|
extern circuit_t *global_circuitlist;
|
|
|
|
|
2006-01-10 23:42:44 +01:00
|
|
|
/** An entry_guard_t represents our information about a chosen long-term
|
2005-12-28 10:07:31 +01:00
|
|
|
* first hop, known as a "helper" node in the literature. We can't just
|
|
|
|
* use a routerinfo_t, since we want to remember these even when we
|
|
|
|
* don't have a directory. */
|
2005-07-22 19:32:25 +02:00
|
|
|
typedef struct {
|
|
|
|
char nickname[MAX_NICKNAME_LEN+1];
|
|
|
|
char identity[DIGEST_LEN];
|
2007-08-29 19:22:00 +02:00
|
|
|
time_t chosen_on_date; /**< Approximately when was this guard added?
|
|
|
|
* "0" if we don't know. */
|
|
|
|
char *chosen_by_version; /**< What tor version added this guard? NULL
|
|
|
|
* if we don't know. */
|
2007-02-23 19:34:35 +01:00
|
|
|
unsigned int made_contact : 1; /**< 0 if we have never connected to this
|
|
|
|
* router, 1 if we have. */
|
|
|
|
unsigned int can_retry : 1; /**< Should we retry connecting to this entry,
|
|
|
|
* in spite of having it marked as unreachable?*/
|
2006-09-29 01:57:44 +02:00
|
|
|
time_t bad_since; /**< 0 if this guard is currently usable, or the time at
|
|
|
|
* which it was observed to become (according to the
|
|
|
|
* directory or the user configuration) unusable. */
|
|
|
|
time_t unreachable_since; /**< 0 if we can connect to this guard, or the
|
|
|
|
* time at which we first noticed we couldn't
|
|
|
|
* connect to it. */
|
2006-09-29 01:57:52 +02:00
|
|
|
time_t last_attempted; /**< 0 if we can connect to this guard, or the time
|
|
|
|
* at which we last failed to connect to it. */
|
2006-01-10 23:42:44 +01:00
|
|
|
} entry_guard_t;
|
2005-07-22 19:32:25 +02:00
|
|
|
|
2006-01-10 23:42:44 +01:00
|
|
|
/** A list of our chosen entry guards. */
|
|
|
|
static smartlist_t *entry_guards = NULL;
|
|
|
|
/** A value of 1 means that the entry_guards list has changed
|
2005-12-25 00:32:35 +01:00
|
|
|
* and those changes need to be flushed to disk. */
|
2006-01-10 23:42:44 +01:00
|
|
|
static int entry_guards_dirty = 0;
|
2005-07-22 19:32:25 +02:00
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
/********* END VARIABLES ************/
|
|
|
|
|
2005-05-03 00:35:18 +02:00
|
|
|
static int circuit_deliver_create_cell(circuit_t *circ,
|
2007-01-30 23:19:41 +01:00
|
|
|
uint8_t cell_type, const char *payload);
|
2006-07-23 09:37:35 +02:00
|
|
|
static int onion_pick_cpath_exit(origin_circuit_t *circ, extend_info_t *exit);
|
2005-03-22 01:42:38 +01:00
|
|
|
static crypt_path_t *onion_next_hop_in_cpath(crypt_path_t *cpath);
|
2006-10-07 08:28:50 +02:00
|
|
|
static int onion_extend_cpath(origin_circuit_t *circ);
|
2004-05-13 09:24:49 +02:00
|
|
|
static int count_acceptable_routers(smartlist_t *routers);
|
2005-06-29 23:46:55 +02:00
|
|
|
static int onion_append_hop(crypt_path_t **head_ptr, extend_info_t *choice);
|
2005-12-28 10:07:31 +01:00
|
|
|
|
2006-01-10 23:42:44 +01:00
|
|
|
static void entry_guards_changed(void);
|
2007-08-29 19:22:00 +02:00
|
|
|
static time_t start_of_month(time_t when);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
/** Iterate over values of circ_id, starting from conn-\>next_circ_id,
|
2006-07-04 05:19:59 +02:00
|
|
|
* and with the high bit specified by conn-\>circ_id_type, until we get
|
|
|
|
* a circ_id that is not in use by any other circuit on that conn.
|
2004-05-13 09:24:49 +02:00
|
|
|
*
|
|
|
|
* Return it, or 0 if can't get a unique circ_id.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static uint16_t
|
2006-07-26 21:07:26 +02:00
|
|
|
get_unique_circ_id_by_conn(or_connection_t *conn)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2004-05-13 09:24:49 +02:00
|
|
|
uint16_t test_circ_id;
|
2005-12-25 05:37:33 +01:00
|
|
|
uint16_t attempts=0;
|
2004-05-13 09:24:49 +02:00
|
|
|
uint16_t high_bit;
|
|
|
|
|
2004-10-17 00:14:52 +02:00
|
|
|
tor_assert(conn);
|
2007-02-12 22:39:33 +01:00
|
|
|
if (conn->circ_id_type == CIRC_ID_TYPE_NEITHER) {
|
2007-03-04 21:11:46 +01:00
|
|
|
log_warn(LD_BUG, "Trying to pick a circuit ID for a connection from "
|
2007-02-12 22:39:33 +01:00
|
|
|
"a client with no identity.");
|
|
|
|
return 0;
|
|
|
|
}
|
2004-11-10 21:14:37 +01:00
|
|
|
high_bit = (conn->circ_id_type == CIRC_ID_TYPE_HIGHER) ? 1<<15 : 0;
|
2004-05-13 09:24:49 +02:00
|
|
|
do {
|
|
|
|
/* Sequentially iterate over test_circ_id=1...1<<15-1 until we find a
|
|
|
|
* circID such that (high_bit|test_circ_id) is not already used. */
|
|
|
|
test_circ_id = conn->next_circ_id++;
|
|
|
|
if (test_circ_id == 0 || test_circ_id >= 1<<15) {
|
|
|
|
test_circ_id = 1;
|
|
|
|
conn->next_circ_id = 2;
|
|
|
|
}
|
2004-11-28 10:05:49 +01:00
|
|
|
if (++attempts > 1<<15) {
|
2004-05-13 09:24:49 +02:00
|
|
|
/* Make sure we don't loop forever if all circ_id's are used. This
|
2005-10-17 10:41:58 +02:00
|
|
|
* matters because it's an external DoS opportunity.
|
2004-05-13 09:24:49 +02:00
|
|
|
*/
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_CIRC,"No unused circ IDs. Failing.");
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
test_circ_id |= high_bit;
|
2006-10-09 01:20:43 +02:00
|
|
|
} while (circuit_get_by_circid_orconn(test_circ_id, conn));
|
2004-05-13 09:24:49 +02:00
|
|
|
return test_circ_id;
|
|
|
|
}
|
|
|
|
|
2006-10-07 21:56:49 +02:00
|
|
|
/** If <b>verbose</b> is false, allocate and return a comma-separated list of
|
|
|
|
* the currently built elements of circuit_t. If <b>verbose</b> is true, also
|
|
|
|
* list information about link status in a more verbose format using spaces.
|
|
|
|
* If <b>verbose_names</b> is false, give nicknames for Named routers and hex
|
2006-10-08 02:33:43 +02:00
|
|
|
* digests for others; if <b>verbose_names</b> is true, use $DIGEST=Name style
|
|
|
|
* names.
|
2004-11-03 19:33:07 +01:00
|
|
|
*/
|
2006-10-07 21:56:49 +02:00
|
|
|
static char *
|
|
|
|
circuit_list_path_impl(origin_circuit_t *circ, int verbose, int verbose_names)
|
2004-11-03 19:33:07 +01:00
|
|
|
{
|
2005-07-22 23:12:10 +02:00
|
|
|
crypt_path_t *hop;
|
2004-11-03 19:33:07 +01:00
|
|
|
smartlist_t *elements;
|
2004-11-23 01:11:36 +01:00
|
|
|
const char *states[] = {"closed", "waiting for keys", "open"};
|
|
|
|
char buf[128];
|
2004-11-03 19:33:07 +01:00
|
|
|
char *s;
|
|
|
|
|
|
|
|
elements = smartlist_create();
|
|
|
|
|
2004-11-23 01:11:36 +01:00
|
|
|
if (verbose) {
|
2005-06-29 23:46:55 +02:00
|
|
|
const char *nickname = build_state_get_exit_nickname(circ->build_state);
|
2005-12-31 07:32:57 +01:00
|
|
|
tor_snprintf(buf, sizeof(buf), "%s%s circ (length %d%s%s):",
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
circ->build_state->is_internal ? "internal" : "exit",
|
|
|
|
circ->build_state->need_uptime ? " (high-uptime)" : "",
|
2004-11-23 01:11:36 +01:00
|
|
|
circ->build_state->desired_path_len,
|
2006-07-23 09:37:35 +02:00
|
|
|
circ->_base.state == CIRCUIT_STATE_OPEN ? "" : ", exit ",
|
|
|
|
circ->_base.state == CIRCUIT_STATE_OPEN ? "" :
|
2006-09-30 05:11:13 +02:00
|
|
|
(nickname?nickname:"*unnamed*"));
|
2004-11-23 01:11:36 +01:00
|
|
|
smartlist_add(elements, tor_strdup(buf));
|
|
|
|
}
|
|
|
|
|
2004-11-23 07:08:08 +01:00
|
|
|
hop = circ->cpath;
|
|
|
|
do {
|
2006-09-30 05:11:13 +02:00
|
|
|
routerinfo_t *ri;
|
|
|
|
char *elt;
|
2004-11-23 07:08:08 +01:00
|
|
|
if (!hop)
|
|
|
|
break;
|
2004-11-23 01:11:36 +01:00
|
|
|
if (!verbose && hop->state != CPATH_STATE_OPEN)
|
2004-11-03 19:33:07 +01:00
|
|
|
break;
|
2005-06-29 23:46:55 +02:00
|
|
|
if (!hop->extend_info)
|
|
|
|
break;
|
2006-10-07 21:56:49 +02:00
|
|
|
if (verbose_names) {
|
|
|
|
elt = tor_malloc(MAX_VERBOSE_NICKNAME_LEN+1);
|
|
|
|
if ((ri = router_get_by_digest(hop->extend_info->identity_digest))) {
|
|
|
|
router_get_verbose_nickname(elt, ri);
|
|
|
|
} else if (hop->extend_info->nickname &&
|
|
|
|
is_legal_nickname(hop->extend_info->nickname)) {
|
|
|
|
elt[0] = '$';
|
|
|
|
base16_encode(elt+1, HEX_DIGEST_LEN+1,
|
|
|
|
hop->extend_info->identity_digest, DIGEST_LEN);
|
|
|
|
elt[HEX_DIGEST_LEN+1]= '~';
|
|
|
|
strlcpy(elt+HEX_DIGEST_LEN+2,
|
|
|
|
hop->extend_info->nickname, MAX_NICKNAME_LEN+1);
|
|
|
|
} else {
|
|
|
|
elt[0] = '$';
|
|
|
|
base16_encode(elt+1, HEX_DIGEST_LEN+1,
|
|
|
|
hop->extend_info->identity_digest, DIGEST_LEN);
|
|
|
|
}
|
|
|
|
} else { /* ! verbose_names */
|
|
|
|
if ((ri = router_get_by_digest(hop->extend_info->identity_digest)) &&
|
|
|
|
ri->is_named) {
|
|
|
|
elt = tor_strdup(hop->extend_info->nickname);
|
|
|
|
} else {
|
|
|
|
elt = tor_malloc(HEX_DIGEST_LEN+2);
|
|
|
|
elt[0] = '$';
|
|
|
|
base16_encode(elt+1, HEX_DIGEST_LEN+1,
|
|
|
|
hop->extend_info->identity_digest, DIGEST_LEN);
|
|
|
|
}
|
2006-09-30 05:11:13 +02:00
|
|
|
}
|
2005-07-14 10:43:19 +02:00
|
|
|
tor_assert(elt);
|
2004-11-23 01:11:36 +01:00
|
|
|
if (verbose) {
|
|
|
|
size_t len = strlen(elt)+2+strlen(states[hop->state])+1;
|
|
|
|
char *v = tor_malloc(len);
|
|
|
|
tor_assert(hop->state <= 2);
|
|
|
|
tor_snprintf(v,len,"%s(%s)",elt,states[hop->state]);
|
|
|
|
smartlist_add(elements, v);
|
2006-09-30 05:11:13 +02:00
|
|
|
tor_free(elt);
|
2004-11-03 19:33:07 +01:00
|
|
|
} else {
|
2006-09-30 05:11:13 +02:00
|
|
|
smartlist_add(elements, elt);
|
2004-11-03 19:33:07 +01:00
|
|
|
}
|
2004-11-23 07:08:08 +01:00
|
|
|
hop = hop->next;
|
|
|
|
} while (hop != circ->cpath);
|
2004-11-03 19:33:07 +01:00
|
|
|
|
2004-11-23 01:11:36 +01:00
|
|
|
s = smartlist_join_strings(elements, verbose?" ":",", 0, NULL);
|
2004-11-03 19:33:07 +01:00
|
|
|
SMARTLIST_FOREACH(elements, char*, cp, tor_free(cp));
|
2004-12-07 08:48:16 +01:00
|
|
|
smartlist_free(elements);
|
2004-11-03 19:33:07 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2006-10-07 21:56:49 +02:00
|
|
|
/** If <b>verbose</b> is false, allocate and return a comma-separated
|
|
|
|
* list of the currently built elements of circuit_t. If
|
|
|
|
* <b>verbose</b> is true, also list information about link status in
|
|
|
|
* a more verbose format using spaces.
|
|
|
|
*/
|
2006-10-03 20:58:40 +02:00
|
|
|
char *
|
2006-10-07 21:56:49 +02:00
|
|
|
circuit_list_path(origin_circuit_t *circ, int verbose)
|
2006-10-03 20:58:40 +02:00
|
|
|
{
|
2006-10-07 21:56:49 +02:00
|
|
|
return circuit_list_path_impl(circ, verbose, 0);
|
|
|
|
}
|
2006-10-03 20:58:40 +02:00
|
|
|
|
2006-10-07 21:56:49 +02:00
|
|
|
/** Allocate and return a comma-separated list of the currently built elements
|
|
|
|
* of circuit_t, giving each as a verbose nickname.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
circuit_list_path_for_controller(origin_circuit_t *circ)
|
|
|
|
{
|
|
|
|
return circuit_list_path_impl(circ, 0, 1);
|
2006-10-03 20:58:40 +02:00
|
|
|
}
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
/** Log, at severity <b>severity</b>, the nicknames of each router in
|
|
|
|
* circ's cpath. Also log the length of the cpath, and the intended
|
|
|
|
* exit point.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
2006-07-23 09:37:35 +02:00
|
|
|
circuit_log_path(int severity, unsigned int domain, origin_circuit_t *circ)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2004-11-23 01:11:36 +01:00
|
|
|
char *s = circuit_list_path(circ,1);
|
2005-10-24 21:37:45 +02:00
|
|
|
log(severity,domain,"%s",s);
|
2004-11-23 01:11:36 +01:00
|
|
|
tor_free(s);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Tell the rep(utation)hist(ory) module about the status of the links
|
|
|
|
* in circ. Hops that have become OPEN are marked as successfully
|
|
|
|
* extended; the _first_ hop that isn't open (if any) is marked as
|
|
|
|
* unable to extend.
|
|
|
|
*/
|
2006-10-07 11:13:30 +02:00
|
|
|
/* XXXX Someday we should learn from OR circuits too. */
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
2006-07-23 09:37:35 +02:00
|
|
|
circuit_rep_hist_note_result(origin_circuit_t *circ)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2005-07-22 23:12:10 +02:00
|
|
|
crypt_path_t *hop;
|
2004-07-13 20:23:40 +02:00
|
|
|
char *prev_digest = NULL;
|
2004-05-13 09:24:49 +02:00
|
|
|
routerinfo_t *router;
|
|
|
|
hop = circ->cpath;
|
2006-10-03 00:13:42 +02:00
|
|
|
if (!hop) /* circuit hasn't started building yet. */
|
2004-05-13 09:24:49 +02:00
|
|
|
return;
|
2004-11-06 06:18:11 +01:00
|
|
|
if (server_mode(get_options())) {
|
2004-07-21 02:44:04 +02:00
|
|
|
routerinfo_t *me = router_get_my_routerinfo();
|
2006-07-17 08:54:28 +02:00
|
|
|
if (!me)
|
|
|
|
return;
|
2005-11-05 21:15:27 +01:00
|
|
|
prev_digest = me->cache_info.identity_digest;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
do {
|
2005-06-29 23:46:55 +02:00
|
|
|
router = router_get_by_digest(hop->extend_info->identity_digest);
|
2004-05-13 09:24:49 +02:00
|
|
|
if (router) {
|
2004-07-13 20:23:40 +02:00
|
|
|
if (prev_digest) {
|
2004-05-13 09:24:49 +02:00
|
|
|
if (hop->state == CPATH_STATE_OPEN)
|
2005-12-14 21:40:40 +01:00
|
|
|
rep_hist_note_extend_succeeded(prev_digest,
|
|
|
|
router->cache_info.identity_digest);
|
2004-05-13 09:24:49 +02:00
|
|
|
else {
|
2005-12-14 21:40:40 +01:00
|
|
|
rep_hist_note_extend_failed(prev_digest,
|
|
|
|
router->cache_info.identity_digest);
|
2004-05-13 09:24:49 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2005-11-05 21:15:27 +01:00
|
|
|
prev_digest = router->cache_info.identity_digest;
|
2004-05-13 09:24:49 +02:00
|
|
|
} else {
|
2004-07-13 20:23:40 +02:00
|
|
|
prev_digest = NULL;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
hop=hop->next;
|
|
|
|
} while (hop!=circ->cpath);
|
|
|
|
}
|
|
|
|
|
2005-03-22 01:42:38 +01:00
|
|
|
/** Pick all the entries in our cpath. Stop and return 0 when we're
|
|
|
|
* happy, or return -1 if an error occurs. */
|
|
|
|
static int
|
2006-07-23 09:37:35 +02:00
|
|
|
onion_populate_cpath(origin_circuit_t *circ)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2005-03-22 01:42:38 +01:00
|
|
|
int r;
|
|
|
|
again:
|
2006-10-07 08:28:50 +02:00
|
|
|
r = onion_extend_cpath(circ);
|
2005-03-22 01:42:38 +01:00
|
|
|
if (r < 0) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_CIRC,"Generating cpath hop failed.");
|
2005-03-22 01:42:38 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (r == 0)
|
|
|
|
goto again;
|
|
|
|
return 0; /* if r == 1 */
|
|
|
|
}
|
|
|
|
|
2006-07-23 09:37:35 +02:00
|
|
|
/** Create and return a new origin circuit. Initialize its purpose and
|
2007-12-23 20:15:22 +01:00
|
|
|
* build-state based on our arguments. The <b>flags</b> argument is a
|
|
|
|
* bitfield of CIRCLAUNCH_* flags. */
|
2006-07-23 09:37:35 +02:00
|
|
|
origin_circuit_t *
|
2007-12-23 20:15:22 +01:00
|
|
|
origin_circuit_init(uint8_t purpose, int flags)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2005-12-14 21:40:40 +01:00
|
|
|
/* sets circ->p_circ_id and circ->p_conn */
|
2006-07-23 09:37:35 +02:00
|
|
|
origin_circuit_t *circ = origin_circuit_new();
|
|
|
|
circuit_set_state(TO_CIRCUIT(circ), CIRCUIT_STATE_OR_WAIT);
|
2005-03-22 01:42:38 +01:00
|
|
|
circ->build_state = tor_malloc_zero(sizeof(cpath_build_state_t));
|
2007-12-23 20:15:22 +01:00
|
|
|
circ->build_state->onehop_tunnel =
|
|
|
|
((flags & CIRCLAUNCH_ONEHOP_TUNNEL) ? 1 : 0);
|
|
|
|
circ->build_state->need_uptime =
|
|
|
|
((flags & CIRCLAUNCH_NEED_UPTIME) ? 1 : 0);
|
|
|
|
circ->build_state->need_capacity =
|
|
|
|
((flags & CIRCLAUNCH_NEED_CAPACITY) ? 1 : 0);
|
|
|
|
circ->build_state->is_internal =
|
|
|
|
((flags & CIRCLAUNCH_IS_INTERNAL) ? 1 : 0);
|
2006-07-23 09:37:35 +02:00
|
|
|
circ->_base.purpose = purpose;
|
2005-03-22 01:42:38 +01:00
|
|
|
return circ;
|
|
|
|
}
|
|
|
|
|
2007-07-29 06:38:21 +02:00
|
|
|
/** Build a new circuit for <b>purpose</b>. If <b>exit</b>
|
2004-05-13 09:24:49 +02:00
|
|
|
* is defined, then use that as your exit router, else choose a suitable
|
|
|
|
* exit node.
|
|
|
|
*
|
|
|
|
* Also launch a connection to the first OR in the chosen path, if
|
|
|
|
* it's not open already.
|
|
|
|
*/
|
2006-07-23 09:37:35 +02:00
|
|
|
origin_circuit_t *
|
2007-12-23 20:15:22 +01:00
|
|
|
circuit_establish_circuit(uint8_t purpose, extend_info_t *exit, int flags)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2006-07-23 09:37:35 +02:00
|
|
|
origin_circuit_t *circ;
|
2006-10-09 17:47:27 +02:00
|
|
|
int err_reason = 0;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2007-12-23 20:15:22 +01:00
|
|
|
circ = origin_circuit_init(purpose, flags);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2007-05-08 12:01:33 +02:00
|
|
|
if (onion_pick_cpath_exit(circ, exit) < 0 ||
|
2005-03-22 01:42:38 +01:00
|
|
|
onion_populate_cpath(circ) < 0) {
|
2006-10-17 17:20:00 +02:00
|
|
|
circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_NOPATH);
|
2004-05-13 09:24:49 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-10-09 17:47:27 +02:00
|
|
|
control_event_circuit_status(circ, CIRC_EVENT_LAUNCHED, 0);
|
2005-03-22 01:42:38 +01:00
|
|
|
|
2006-10-09 17:47:27 +02:00
|
|
|
if ((err_reason = circuit_handle_first_hop(circ)) < 0) {
|
|
|
|
circuit_mark_for_close(TO_CIRCUIT(circ), -err_reason);
|
2004-05-13 09:24:49 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2005-03-22 01:42:38 +01:00
|
|
|
return circ;
|
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2005-03-22 01:42:38 +01:00
|
|
|
/** Start establishing the first hop of our circuit. Figure out what
|
|
|
|
* OR we should connect to, and if necessary start the connection to
|
|
|
|
* it. If we're already connected, then send the 'create' cell.
|
2006-10-09 17:47:27 +02:00
|
|
|
* Return 0 for ok, -reason if circ should be marked-for-close. */
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
2006-07-23 09:37:35 +02:00
|
|
|
circuit_handle_first_hop(origin_circuit_t *circ)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2005-06-29 23:46:55 +02:00
|
|
|
crypt_path_t *firsthop;
|
2006-07-26 21:07:26 +02:00
|
|
|
or_connection_t *n_conn;
|
2006-03-15 00:40:37 +01:00
|
|
|
char tmpbuf[INET_NTOA_BUF_LEN];
|
2005-06-29 23:46:55 +02:00
|
|
|
struct in_addr in;
|
2006-10-09 17:47:27 +02:00
|
|
|
int err_reason = 0;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2005-06-29 23:46:55 +02:00
|
|
|
firsthop = onion_next_hop_in_cpath(circ->cpath);
|
2004-08-17 14:09:37 +02:00
|
|
|
tor_assert(firsthop);
|
2005-07-14 10:43:19 +02:00
|
|
|
tor_assert(firsthop->extend_info);
|
2005-03-22 01:42:38 +01:00
|
|
|
|
|
|
|
/* now see if we're already connected to the first OR in 'route' */
|
2005-06-29 23:46:55 +02:00
|
|
|
in.s_addr = htonl(firsthop->extend_info->addr);
|
|
|
|
tor_inet_ntoa(&in, tmpbuf, sizeof(tmpbuf));
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_CIRC,"Looking for firsthop '%s:%u'",tmpbuf,
|
|
|
|
firsthop->extend_info->port);
|
2004-07-13 01:53:16 +02:00
|
|
|
/* imprint the circuit with its future n_conn->id */
|
2006-07-23 09:37:35 +02:00
|
|
|
memcpy(circ->_base.n_conn_id_digest, firsthop->extend_info->identity_digest,
|
2005-06-29 23:46:55 +02:00
|
|
|
DIGEST_LEN);
|
2005-11-30 04:01:16 +01:00
|
|
|
n_conn = connection_or_get_by_identity_digest(
|
2005-11-19 07:57:44 +01:00
|
|
|
firsthop->extend_info->identity_digest);
|
2005-12-10 22:07:21 +01:00
|
|
|
/* If we don't have an open conn, or the conn we have is obsolete
|
|
|
|
* (i.e. old or broken) and the other side will let us make a second
|
|
|
|
* connection without dropping it immediately... */
|
2006-07-26 21:07:26 +02:00
|
|
|
if (!n_conn || n_conn->_base.state != OR_CONN_STATE_OPEN ||
|
|
|
|
(n_conn->_base.or_is_obsolete &&
|
2005-11-19 07:57:44 +01:00
|
|
|
router_digest_version_as_new_as(firsthop->extend_info->identity_digest,
|
|
|
|
"0.1.1.9-alpha-cvs"))) {
|
|
|
|
/* not currently connected */
|
2006-07-23 09:37:35 +02:00
|
|
|
circ->_base.n_addr = firsthop->extend_info->addr;
|
|
|
|
circ->_base.n_port = firsthop->extend_info->port;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2006-07-26 21:07:26 +02:00
|
|
|
if (!n_conn || n_conn->_base.or_is_obsolete) { /* launch the connection */
|
2005-06-29 23:46:55 +02:00
|
|
|
n_conn = connection_or_connect(firsthop->extend_info->addr,
|
|
|
|
firsthop->extend_info->port,
|
|
|
|
firsthop->extend_info->identity_digest);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!n_conn) { /* connect failed, forget the whole thing */
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_CIRC,"connect to firsthop failed. Closing.");
|
2006-10-09 17:47:27 +02:00
|
|
|
return -END_CIRC_REASON_CONNECTFAILED;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_CIRC,"connecting in progress (or finished). Good.");
|
2005-12-14 21:40:40 +01:00
|
|
|
/* return success. The onion/circuit/etc will be taken care of
|
|
|
|
* automatically (may already have been) whenever n_conn reaches
|
|
|
|
* OR_CONN_STATE_OPEN.
|
2004-05-13 09:24:49 +02:00
|
|
|
*/
|
2005-03-22 01:42:38 +01:00
|
|
|
return 0;
|
2004-08-18 22:35:11 +02:00
|
|
|
} else { /* it's already open. use it. */
|
2006-07-26 21:07:26 +02:00
|
|
|
circ->_base.n_addr = n_conn->_base.addr;
|
|
|
|
circ->_base.n_port = n_conn->_base.port;
|
2006-07-23 09:37:35 +02:00
|
|
|
circ->_base.n_conn = n_conn;
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_CIRC,"Conn open. Delivering first onion skin.");
|
2006-10-09 17:47:27 +02:00
|
|
|
if ((err_reason = circuit_send_next_onion_skin(circ)) < 0) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_CIRC,"circuit_send_next_onion_skin failed.");
|
2006-10-09 17:47:27 +02:00
|
|
|
return err_reason;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
}
|
2005-03-22 01:42:38 +01:00
|
|
|
return 0;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
2005-12-28 08:19:55 +01:00
|
|
|
/** Find any circuits that are waiting on <b>or_conn</b> to become
|
|
|
|
* open and get them to send their create cells forward.
|
2004-11-21 08:43:12 +01:00
|
|
|
*
|
|
|
|
* Status is 1 if connect succeeded, or 0 if connect failed.
|
2004-05-13 09:24:49 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
2006-07-26 21:07:26 +02:00
|
|
|
circuit_n_conn_done(or_connection_t *or_conn, int status)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2007-02-05 18:42:40 +01:00
|
|
|
smartlist_t *pending_circs;
|
2006-10-09 17:47:27 +02:00
|
|
|
int err_reason = 0;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_CIRC,"or_conn to %s, status=%d",
|
|
|
|
or_conn->nickname ? or_conn->nickname : "NULL", status);
|
2004-07-13 01:53:16 +02:00
|
|
|
|
2007-02-05 18:42:40 +01:00
|
|
|
pending_circs = smartlist_create();
|
|
|
|
circuit_get_all_pending_on_or_conn(pending_circs, or_conn);
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH(pending_circs, circuit_t *, circ,
|
|
|
|
{
|
2007-06-15 08:01:04 +02:00
|
|
|
/* These checks are redundant wrt get_all_pending_on_or_conn, but I'm
|
2007-02-05 18:42:40 +01:00
|
|
|
* leaving them in in case it's possible for the status of a circuit to
|
|
|
|
* change as we're going down the list. */
|
|
|
|
if (circ->marked_for_close || circ->n_conn ||
|
2007-06-15 08:01:04 +02:00
|
|
|
circ->state != CIRCUIT_STATE_OR_WAIT)
|
2007-02-05 18:42:40 +01:00
|
|
|
continue;
|
2007-06-15 08:01:04 +02:00
|
|
|
if (tor_digest_is_zero(circ->n_conn_id_digest)) {
|
|
|
|
/* Look at addr/port. This is an unkeyed connection. */
|
|
|
|
if (circ->n_addr != or_conn->_base.addr ||
|
|
|
|
circ->n_port != or_conn->_base.port)
|
|
|
|
continue;
|
2007-06-15 18:21:40 +02:00
|
|
|
/* now teach circ the right identity_digest */
|
|
|
|
memcpy(circ->n_conn_id_digest, or_conn->identity_digest, DIGEST_LEN);
|
2007-06-15 08:01:04 +02:00
|
|
|
} else {
|
|
|
|
/* We expected a key. See if it's the right one. */
|
|
|
|
if (memcmp(or_conn->identity_digest,
|
|
|
|
circ->n_conn_id_digest, DIGEST_LEN))
|
|
|
|
continue;
|
|
|
|
}
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!status) { /* or_conn failed; close circ */
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_CIRC,"or_conn failed. Closing circ.");
|
2006-05-30 08:17:28 +02:00
|
|
|
circuit_mark_for_close(circ, END_CIRC_REASON_OR_CONN_CLOSED);
|
2004-05-13 09:24:49 +02:00
|
|
|
continue;
|
2004-07-02 11:29:01 +02:00
|
|
|
}
|
2006-05-30 07:29:03 +02:00
|
|
|
log_debug(LD_CIRC, "Found circ, sending create cell.");
|
2005-12-03 17:32:29 +01:00
|
|
|
/* circuit_deliver_create_cell will set n_circ_id and add us to
|
|
|
|
* orconn_circuid_circuit_map, so we don't need to call
|
|
|
|
* set_circid_orconn here. */
|
2004-07-02 11:29:01 +02:00
|
|
|
circ->n_conn = or_conn;
|
2004-11-28 10:05:49 +01:00
|
|
|
if (CIRCUIT_IS_ORIGIN(circ)) {
|
2006-10-09 17:47:50 +02:00
|
|
|
if ((err_reason =
|
|
|
|
circuit_send_next_onion_skin(TO_ORIGIN_CIRCUIT(circ))) < 0) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_CIRC,
|
|
|
|
"send_next_onion_skin failed; circuit marked for closing.");
|
2006-10-09 17:47:27 +02:00
|
|
|
circuit_mark_for_close(circ, -err_reason);
|
2004-07-02 11:29:01 +02:00
|
|
|
continue;
|
2005-12-14 21:40:40 +01:00
|
|
|
/* XXX could this be bad, eg if next_onion_skin failed because conn
|
|
|
|
* died? */
|
2004-07-02 11:29:01 +02:00
|
|
|
}
|
|
|
|
} else {
|
2004-07-03 01:08:59 +02:00
|
|
|
/* pull the create cell out of circ->onionskin, and send it */
|
2008-02-06 00:20:49 +01:00
|
|
|
tor_assert(circ->n_conn_onionskin);
|
|
|
|
if (circuit_deliver_create_cell(circ,CELL_CREATE,
|
|
|
|
circ->n_conn_onionskin)<0) {
|
2006-01-05 22:23:03 +01:00
|
|
|
circuit_mark_for_close(circ, END_CIRC_REASON_RESOURCELIMIT);
|
2004-07-03 01:08:59 +02:00
|
|
|
continue;
|
|
|
|
}
|
2008-02-06 00:20:49 +01:00
|
|
|
tor_free(circ->n_conn_onionskin);
|
2007-02-05 18:42:40 +01:00
|
|
|
circuit_set_state(circ, CIRCUIT_STATE_OPEN);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
2007-02-05 18:42:40 +01:00
|
|
|
});
|
2006-06-06 05:33:24 +02:00
|
|
|
|
2007-02-05 18:42:40 +01:00
|
|
|
smartlist_free(pending_circs);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
2005-12-07 23:09:02 +01:00
|
|
|
/** Find a new circid that isn't currently in use on the circ->n_conn
|
|
|
|
* for the outgoing
|
2005-06-11 23:17:38 +02:00
|
|
|
* circuit <b>circ</b>, and deliver a cell of type <b>cell_type</b>
|
|
|
|
* (either CELL_CREATE or CELL_CREATE_FAST) with payload <b>payload</b>
|
|
|
|
* to this circuit.
|
|
|
|
* Return -1 if we failed to find a suitable circid, else return 0.
|
|
|
|
*/
|
2004-07-03 01:08:59 +02:00
|
|
|
static int
|
2006-07-23 09:37:35 +02:00
|
|
|
circuit_deliver_create_cell(circuit_t *circ, uint8_t cell_type,
|
2007-01-30 23:19:41 +01:00
|
|
|
const char *payload)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2004-07-03 01:08:59 +02:00
|
|
|
cell_t cell;
|
2005-04-06 07:45:07 +02:00
|
|
|
uint16_t id;
|
2004-07-03 01:08:59 +02:00
|
|
|
|
2004-10-17 00:14:52 +02:00
|
|
|
tor_assert(circ);
|
|
|
|
tor_assert(circ->n_conn);
|
2004-07-03 01:08:59 +02:00
|
|
|
tor_assert(payload);
|
2005-05-03 00:35:18 +02:00
|
|
|
tor_assert(cell_type == CELL_CREATE || cell_type == CELL_CREATE_FAST);
|
2004-07-03 01:08:59 +02:00
|
|
|
|
2005-04-06 07:45:07 +02:00
|
|
|
id = get_unique_circ_id_by_conn(circ->n_conn);
|
|
|
|
if (!id) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_CIRC,"failed to get unique circID.");
|
2004-07-03 01:08:59 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_CIRC,"Chosen circID %u.", id);
|
2006-07-23 09:37:35 +02:00
|
|
|
circuit_set_n_circid_orconn(circ, id, circ->n_conn);
|
2004-07-03 01:08:59 +02:00
|
|
|
|
|
|
|
memset(&cell, 0, sizeof(cell_t));
|
2005-05-03 00:35:18 +02:00
|
|
|
cell.command = cell_type;
|
2004-07-03 01:08:59 +02:00
|
|
|
cell.circ_id = circ->n_circ_id;
|
|
|
|
|
|
|
|
memcpy(cell.payload, payload, ONIONSKIN_CHALLENGE_LEN);
|
2007-03-26 16:07:59 +02:00
|
|
|
append_cell_to_circuit_queue(circ, circ->n_conn, &cell, CELL_DIRECTION_OUT);
|
2007-03-20 04:26:51 +01:00
|
|
|
|
2007-12-02 12:24:06 +01:00
|
|
|
if (CIRCUIT_IS_ORIGIN(circ)) {
|
|
|
|
/* mark it so it gets better rate limiting treatment. */
|
|
|
|
circ->n_conn->client_used = time(NULL);
|
|
|
|
}
|
2007-03-20 04:26:51 +01:00
|
|
|
|
2004-07-03 01:08:59 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-09-13 23:24:51 +02:00
|
|
|
/** We've decided to start our reachability testing. If all
|
|
|
|
* is set, log this to the user. Return 1 if we did, or 0 if
|
|
|
|
* we chose not to log anything. */
|
2006-01-17 05:03:23 +01:00
|
|
|
int
|
2005-09-13 23:24:51 +02:00
|
|
|
inform_testing_reachability(void)
|
|
|
|
{
|
|
|
|
char dirbuf[128];
|
|
|
|
routerinfo_t *me = router_get_my_routerinfo();
|
|
|
|
if (!me)
|
|
|
|
return 0;
|
|
|
|
if (me->dir_port)
|
|
|
|
tor_snprintf(dirbuf, sizeof(dirbuf), " and DirPort %s:%d",
|
|
|
|
me->address, me->dir_port);
|
2005-12-10 09:27:01 +01:00
|
|
|
log(LOG_NOTICE, LD_OR, "Now checking whether ORPort %s:%d%s %s reachable... "
|
|
|
|
"(this may take up to %d minutes -- look for log "
|
|
|
|
"messages indicating success)",
|
2005-09-13 23:24:51 +02:00
|
|
|
me->address, me->or_port,
|
|
|
|
me->dir_port ? dirbuf : "",
|
2005-12-08 22:12:47 +01:00
|
|
|
me->dir_port ? "are" : "is",
|
|
|
|
TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT/60);
|
2005-09-13 23:24:51 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-12-07 23:09:02 +01:00
|
|
|
/** Return true iff we should send a create_fast cell to build a circuit
|
2007-01-27 20:29:16 +01:00
|
|
|
* starting at <b>router</b>. (If <b>router</b> is NULL, we don't have
|
|
|
|
* information on the router, so assume true.) */
|
2005-12-07 23:09:02 +01:00
|
|
|
static INLINE int
|
2007-01-27 20:29:16 +01:00
|
|
|
should_use_create_fast_for_router(routerinfo_t *router,
|
|
|
|
origin_circuit_t *circ)
|
2005-12-07 23:09:02 +01:00
|
|
|
{
|
|
|
|
or_options_t *options = get_options();
|
|
|
|
|
2007-01-27 20:29:16 +01:00
|
|
|
if (!options->FastFirstHopPK) /* create_fast is disabled */
|
2005-12-07 23:09:02 +01:00
|
|
|
return 0;
|
2007-01-27 20:29:16 +01:00
|
|
|
if (router && router->platform &&
|
|
|
|
!tor_version_as_new_as(router->platform, "0.1.0.6-rc")) {
|
|
|
|
/* known not to work */
|
2005-12-07 23:09:02 +01:00
|
|
|
return 0;
|
2007-01-27 20:29:16 +01:00
|
|
|
}
|
|
|
|
if (server_mode(options) && circ->cpath->extend_info->onion_key) {
|
|
|
|
/* We're a server, and we know an onion key. We can choose.
|
|
|
|
* Prefer to blend in. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
2005-12-07 23:09:02 +01:00
|
|
|
}
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
/** This is the backbone function for building circuits.
|
|
|
|
*
|
|
|
|
* If circ's first hop is closed, then we need to build a create
|
|
|
|
* cell and send it forward.
|
|
|
|
*
|
|
|
|
* Otherwise, we need to build a relay extend cell and send it
|
|
|
|
* forward.
|
|
|
|
*
|
2006-01-05 22:23:03 +01:00
|
|
|
* Return -reason if we want to tear down circ, else return 0.
|
2004-05-13 09:24:49 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
2006-07-23 09:37:35 +02:00
|
|
|
circuit_send_next_onion_skin(origin_circuit_t *circ)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2004-05-13 09:24:49 +02:00
|
|
|
crypt_path_t *hop;
|
|
|
|
routerinfo_t *router;
|
2004-07-01 03:16:59 +02:00
|
|
|
char payload[2+4+DIGEST_LEN+ONIONSKIN_CHALLENGE_LEN];
|
|
|
|
char *onionskin;
|
2004-10-14 04:47:09 +02:00
|
|
|
size_t payload_len;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2004-10-17 00:14:52 +02:00
|
|
|
tor_assert(circ);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (circ->cpath->state == CPATH_STATE_CLOSED) {
|
2005-12-07 23:09:02 +01:00
|
|
|
int fast;
|
2005-05-03 00:35:18 +02:00
|
|
|
uint8_t cell_type;
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_CIRC,"First skin; sending create cell.");
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2006-07-23 09:37:35 +02:00
|
|
|
router = router_get_by_digest(circ->_base.n_conn->identity_digest);
|
2007-01-27 20:29:16 +01:00
|
|
|
fast = should_use_create_fast_for_router(router, circ);
|
|
|
|
if (!fast && !circ->cpath->extend_info->onion_key) {
|
|
|
|
log_warn(LD_CIRC,
|
|
|
|
"Can't send create_fast, but have no onion key. Failing.");
|
|
|
|
return - END_CIRC_REASON_INTERNAL;
|
|
|
|
}
|
|
|
|
if (!fast) {
|
2005-05-03 00:35:18 +02:00
|
|
|
/* We are an OR, or we are connecting to an old Tor: we should
|
|
|
|
* send an old slow create cell.
|
|
|
|
*/
|
|
|
|
cell_type = CELL_CREATE;
|
2005-06-29 23:46:55 +02:00
|
|
|
if (onion_skin_create(circ->cpath->extend_info->onion_key,
|
2005-05-03 00:35:18 +02:00
|
|
|
&(circ->cpath->dh_handshake_state),
|
|
|
|
payload) < 0) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_CIRC,"onion_skin_create (first hop) failed.");
|
2006-01-05 22:23:03 +01:00
|
|
|
return - END_CIRC_REASON_INTERNAL;
|
2005-05-03 00:35:18 +02:00
|
|
|
}
|
|
|
|
} else {
|
2005-12-08 18:38:32 +01:00
|
|
|
/* We are not an OR, and we're building the first hop of a circuit to a
|
|
|
|
* new OR: we can be speedy and use CREATE_FAST to save an RSA operation
|
|
|
|
* and a DH operation. */
|
2005-05-03 00:35:18 +02:00
|
|
|
cell_type = CELL_CREATE_FAST;
|
|
|
|
memset(payload, 0, sizeof(payload));
|
|
|
|
crypto_rand(circ->cpath->fast_handshake_state,
|
|
|
|
sizeof(circ->cpath->fast_handshake_state));
|
|
|
|
memcpy(payload, circ->cpath->fast_handshake_state,
|
|
|
|
sizeof(circ->cpath->fast_handshake_state));
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
2006-07-23 09:37:35 +02:00
|
|
|
if (circuit_deliver_create_cell(TO_CIRCUIT(circ), cell_type, payload) < 0)
|
2006-01-05 22:23:03 +01:00
|
|
|
return - END_CIRC_REASON_RESOURCELIMIT;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
circ->cpath->state = CPATH_STATE_AWAITING_KEYS;
|
2006-07-23 09:37:35 +02:00
|
|
|
circuit_set_state(TO_CIRCUIT(circ), CIRCUIT_STATE_BUILDING);
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_CIRC,"First hop: finished sending %s cell to '%s'",
|
|
|
|
fast ? "CREATE_FAST" : "CREATE",
|
2006-07-04 05:19:59 +02:00
|
|
|
router ? router->nickname : "<unnamed>");
|
2004-05-13 09:24:49 +02:00
|
|
|
} else {
|
|
|
|
tor_assert(circ->cpath->state == CPATH_STATE_OPEN);
|
2006-07-23 09:37:35 +02:00
|
|
|
tor_assert(circ->_base.state == CIRCUIT_STATE_BUILDING);
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_CIRC,"starting to send subsequent skin.");
|
2005-06-29 23:46:55 +02:00
|
|
|
hop = onion_next_hop_in_cpath(circ->cpath);
|
|
|
|
if (!hop) {
|
2004-05-13 09:24:49 +02:00
|
|
|
/* done building the circuit. whew. */
|
2006-07-23 09:37:35 +02:00
|
|
|
circuit_set_state(TO_CIRCUIT(circ), CIRCUIT_STATE_OPEN);
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_CIRC,"circuit built!");
|
2004-08-18 10:51:04 +02:00
|
|
|
circuit_reset_failure_count(0);
|
2006-12-13 03:49:45 +01:00
|
|
|
if (!has_completed_circuit && !circ->build_state->onehop_tunnel) {
|
2005-03-26 02:43:39 +01:00
|
|
|
or_options_t *options = get_options();
|
2004-05-13 09:24:49 +02:00
|
|
|
has_completed_circuit=1;
|
2005-09-13 23:24:51 +02:00
|
|
|
/* FFFF Log a count of known routers here */
|
2005-10-24 21:37:45 +02:00
|
|
|
log(LOG_NOTICE, LD_GENERAL,
|
2006-03-17 20:25:36 +01:00
|
|
|
"Tor has successfully opened a circuit. "
|
|
|
|
"Looks like client functionality is working.");
|
2006-10-23 07:51:46 +02:00
|
|
|
control_event_client_status(LOG_NOTICE, "CIRCUIT_ESTABLISHED");
|
2005-08-22 07:46:02 +02:00
|
|
|
if (server_mode(options) && !check_whether_orport_reachable()) {
|
2005-09-13 23:24:51 +02:00
|
|
|
inform_testing_reachability();
|
2006-09-15 07:53:00 +02:00
|
|
|
consider_testing_reachability(1, 1);
|
2005-08-22 07:46:02 +02:00
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
circuit_rep_hist_note_result(circ);
|
|
|
|
circuit_has_opened(circ); /* do other actions as necessary */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-01-25 00:42:00 +01:00
|
|
|
set_uint32(payload, htonl(hop->extend_info->addr));
|
|
|
|
set_uint16(payload+4, htons(hop->extend_info->port));
|
2004-07-22 05:47:20 +02:00
|
|
|
|
|
|
|
onionskin = payload+2+4;
|
2005-12-14 21:40:40 +01:00
|
|
|
memcpy(payload+2+4+ONIONSKIN_CHALLENGE_LEN,
|
|
|
|
hop->extend_info->identity_digest, DIGEST_LEN);
|
2004-07-22 05:47:20 +02:00
|
|
|
payload_len = 2+4+ONIONSKIN_CHALLENGE_LEN+DIGEST_LEN;
|
2004-07-01 03:16:59 +02:00
|
|
|
|
2005-06-29 23:46:55 +02:00
|
|
|
if (onion_skin_create(hop->extend_info->onion_key,
|
|
|
|
&(hop->dh_handshake_state), onionskin) < 0) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_CIRC,"onion_skin_create failed.");
|
2006-01-05 22:23:03 +01:00
|
|
|
return - END_CIRC_REASON_INTERNAL;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
2007-03-26 16:07:59 +02:00
|
|
|
log_info(LD_CIRC,"Sending extend relay cell.");
|
2004-05-13 09:24:49 +02:00
|
|
|
/* send it to hop->prev, because it will transfer
|
|
|
|
* it to a create cell and then send to hop */
|
2007-03-24 16:57:51 +01:00
|
|
|
if (relay_send_command_from_edge(0, TO_CIRCUIT(circ),
|
2006-07-23 09:37:35 +02:00
|
|
|
RELAY_COMMAND_EXTEND,
|
2004-11-28 12:39:53 +01:00
|
|
|
payload, payload_len, hop->prev) < 0)
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0; /* circuit is closed */
|
|
|
|
|
|
|
|
hop->state = CPATH_STATE_AWAITING_KEYS;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-01-22 20:20:33 +01:00
|
|
|
/** Our clock just jumped by <b>seconds_elapsed</b>. Assume
|
2005-03-22 01:42:38 +01:00
|
|
|
* something has also gone wrong with our network: notify the user,
|
|
|
|
* and abandon all not-yet-used circuits. */
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
circuit_note_clock_jumped(int seconds_elapsed)
|
|
|
|
{
|
2006-10-23 12:16:43 +02:00
|
|
|
int severity = server_mode(get_options()) ? LOG_WARN : LOG_NOTICE;
|
2007-10-09 23:11:16 +02:00
|
|
|
log(severity, LD_GENERAL, "Your system clock just jumped %d seconds %s; "
|
2007-01-22 20:20:33 +01:00
|
|
|
"assuming established circuits no longer work.",
|
|
|
|
seconds_elapsed >=0 ? seconds_elapsed : -seconds_elapsed,
|
|
|
|
seconds_elapsed >=0 ? "forward" : "backward");
|
2006-10-23 12:16:43 +02:00
|
|
|
control_event_general_status(LOG_WARN, "CLOCK_JUMPED TIME=%d",
|
|
|
|
seconds_elapsed);
|
2005-03-20 00:58:42 +01:00
|
|
|
has_completed_circuit=0; /* so it'll log when it works again */
|
2006-10-23 12:16:43 +02:00
|
|
|
control_event_client_status(severity, "CIRCUIT_NOT_ESTABLISHED REASON=%s",
|
|
|
|
"CLOCK_JUMPED");
|
2005-03-20 00:58:42 +01:00
|
|
|
circuit_mark_all_unused_circs();
|
2005-12-25 05:37:33 +01:00
|
|
|
circuit_expire_all_dirty_circs();
|
2005-03-20 00:58:42 +01:00
|
|
|
}
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
/** Take the 'extend' cell, pull out addr/port plus the onion skin. Make
|
2005-10-17 03:29:28 +02:00
|
|
|
* sure we're connected to the next hop, and pass it the onion skin using
|
|
|
|
* a create cell. Return -1 if we want to warn and tear down the circuit,
|
|
|
|
* else return 0.
|
2004-05-13 09:24:49 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
|
|
|
circuit_extend(cell_t *cell, circuit_t *circ)
|
|
|
|
{
|
2006-07-26 21:07:26 +02:00
|
|
|
or_connection_t *n_conn;
|
2004-07-01 03:16:59 +02:00
|
|
|
relay_header_t rh;
|
|
|
|
char *onionskin;
|
2004-07-03 01:08:59 +02:00
|
|
|
char *id_digest=NULL;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (circ->n_conn) {
|
2006-07-30 06:32:58 +02:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
|
2005-10-24 21:37:45 +02:00
|
|
|
"n_conn already set. Bug/attack. Closing.");
|
2004-05-13 09:24:49 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-07-30 06:32:58 +02:00
|
|
|
if (!server_mode(get_options())) {
|
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
|
|
|
|
"Got an extend cell, but running as a client. Closing.");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-07-01 03:16:59 +02:00
|
|
|
relay_header_unpack(&rh, cell->payload);
|
|
|
|
|
2004-10-17 23:51:20 +02:00
|
|
|
if (rh.length < 4+2+ONIONSKIN_CHALLENGE_LEN+DIGEST_LEN) {
|
2005-12-10 10:36:26 +01:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
|
|
|
|
"Wrong length %d on extend cell. Closing circuit.",
|
|
|
|
rh.length);
|
2004-07-03 01:08:59 +02:00
|
|
|
return -1;
|
2004-07-01 03:16:59 +02:00
|
|
|
}
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
circ->n_addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE));
|
|
|
|
circ->n_port = ntohs(get_uint16(cell->payload+RELAY_HEADER_SIZE+4));
|
|
|
|
|
2004-10-17 23:51:20 +02:00
|
|
|
onionskin = cell->payload+RELAY_HEADER_SIZE+4+2;
|
|
|
|
id_digest = cell->payload+RELAY_HEADER_SIZE+4+2+ONIONSKIN_CHALLENGE_LEN;
|
2005-11-30 04:01:16 +01:00
|
|
|
n_conn = connection_or_get_by_identity_digest(id_digest);
|
2004-07-01 03:16:59 +02:00
|
|
|
|
2005-12-10 22:07:21 +01:00
|
|
|
/* If we don't have an open conn, or the conn we have is obsolete
|
|
|
|
* (i.e. old or broken) and the other side will let us make a second
|
|
|
|
* connection without dropping it immediately... */
|
2006-07-26 21:07:26 +02:00
|
|
|
if (!n_conn || n_conn->_base.state != OR_CONN_STATE_OPEN ||
|
|
|
|
(n_conn->_base.or_is_obsolete &&
|
2005-11-19 07:57:44 +01:00
|
|
|
router_digest_version_as_new_as(id_digest,"0.1.1.9-alpha-cvs"))) {
|
2004-05-13 09:24:49 +02:00
|
|
|
struct in_addr in;
|
2005-02-22 09:18:36 +01:00
|
|
|
char tmpbuf[INET_NTOA_BUF_LEN];
|
2004-05-13 09:24:49 +02:00
|
|
|
in.s_addr = htonl(circ->n_addr);
|
2005-02-22 09:18:36 +01:00
|
|
|
tor_inet_ntoa(&in,tmpbuf,sizeof(tmpbuf));
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_CIRC|LD_OR,"Next router (%s:%d) not connected. Connecting.",
|
|
|
|
tmpbuf, circ->n_port);
|
2004-07-02 11:29:01 +02:00
|
|
|
|
2008-02-06 00:20:49 +01:00
|
|
|
circ->n_conn_onionskin = tor_malloc(ONIONSKIN_CHALLENGE_LEN);
|
|
|
|
memcpy(circ->n_conn_onionskin, onionskin, ONIONSKIN_CHALLENGE_LEN);
|
2005-12-03 03:12:37 +01:00
|
|
|
circuit_set_state(circ, CIRCUIT_STATE_OR_WAIT);
|
2004-07-13 01:53:16 +02:00
|
|
|
|
|
|
|
/* imprint the circuit with its future n_conn->id */
|
|
|
|
memcpy(circ->n_conn_id_digest, id_digest, DIGEST_LEN);
|
|
|
|
|
2006-07-26 21:07:26 +02:00
|
|
|
if (n_conn && !n_conn->_base.or_is_obsolete) {
|
|
|
|
circ->n_addr = n_conn->_base.addr;
|
|
|
|
circ->n_port = n_conn->_base.port;
|
2004-08-08 12:32:36 +02:00
|
|
|
} else {
|
|
|
|
/* we should try to open a connection */
|
|
|
|
n_conn = connection_or_connect(circ->n_addr, circ->n_port, id_digest);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!n_conn) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_CIRC,"Launching n_conn failed. Closing circuit.");
|
2006-01-05 22:23:03 +01:00
|
|
|
circuit_mark_for_close(circ, END_CIRC_REASON_CONNECTFAILED);
|
2005-10-17 03:29:28 +02:00
|
|
|
return 0;
|
2004-08-08 12:32:36 +02:00
|
|
|
}
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_CIRC,"connecting in progress (or finished). Good.");
|
2004-07-03 01:08:59 +02:00
|
|
|
}
|
2005-12-14 21:40:40 +01:00
|
|
|
/* return success. The onion/circuit/etc will be taken care of
|
|
|
|
* automatically (may already have been) whenever n_conn reaches
|
|
|
|
* OR_CONN_STATE_OPEN.
|
2004-07-02 11:29:01 +02:00
|
|
|
*/
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-08-08 12:32:36 +02:00
|
|
|
/* these may be different if the router connected to us from elsewhere */
|
2006-07-26 21:07:26 +02:00
|
|
|
circ->n_addr = n_conn->_base.addr;
|
|
|
|
circ->n_port = n_conn->_base.port;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
circ->n_conn = n_conn;
|
2004-07-03 03:37:59 +02:00
|
|
|
memcpy(circ->n_conn_id_digest, n_conn->identity_digest, DIGEST_LEN);
|
2006-07-26 21:07:37 +02:00
|
|
|
log_debug(LD_CIRC,"n_conn is %s:%u",
|
|
|
|
n_conn->_base.address,n_conn->_base.port);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2005-05-03 00:35:18 +02:00
|
|
|
if (circuit_deliver_create_cell(circ, CELL_CREATE, onionskin) < 0)
|
2004-05-13 09:24:49 +02:00
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Initialize cpath-\>{f|b}_{crypto|digest} from the key material in
|
|
|
|
* key_data. key_data must contain CPATH_KEY_MATERIAL bytes, which are
|
|
|
|
* used as follows:
|
|
|
|
* - 20 to initialize f_digest
|
|
|
|
* - 20 to initialize b_digest
|
|
|
|
* - 16 to key f_crypto
|
|
|
|
* - 16 to key b_crypto
|
|
|
|
*
|
|
|
|
* (If 'reverse' is true, then f_XX and b_XX are swapped.)
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
2007-01-30 23:19:41 +01:00
|
|
|
circuit_init_cpath_crypto(crypt_path_t *cpath, const char *key_data,
|
|
|
|
int reverse)
|
2004-05-13 09:24:49 +02:00
|
|
|
{
|
|
|
|
crypto_digest_env_t *tmp_digest;
|
|
|
|
crypto_cipher_env_t *tmp_crypto;
|
|
|
|
|
2004-10-17 00:14:52 +02:00
|
|
|
tor_assert(cpath);
|
|
|
|
tor_assert(key_data);
|
2004-05-13 09:24:49 +02:00
|
|
|
tor_assert(!(cpath->f_crypto || cpath->b_crypto ||
|
|
|
|
cpath->f_digest || cpath->b_digest));
|
|
|
|
|
|
|
|
cpath->f_digest = crypto_new_digest_env();
|
|
|
|
crypto_digest_add_bytes(cpath->f_digest, key_data, DIGEST_LEN);
|
|
|
|
cpath->b_digest = crypto_new_digest_env();
|
|
|
|
crypto_digest_add_bytes(cpath->b_digest, key_data+DIGEST_LEN, DIGEST_LEN);
|
|
|
|
|
|
|
|
if (!(cpath->f_crypto =
|
|
|
|
crypto_create_init_cipher(key_data+(2*DIGEST_LEN),1))) {
|
2007-03-04 21:11:46 +01:00
|
|
|
log_warn(LD_BUG,"Forward cipher initialization failed.");
|
2004-05-13 09:24:49 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!(cpath->b_crypto =
|
2004-11-28 12:39:53 +01:00
|
|
|
crypto_create_init_cipher(key_data+(2*DIGEST_LEN)+CIPHER_KEY_LEN,0))) {
|
2007-03-04 21:11:46 +01:00
|
|
|
log_warn(LD_BUG,"Backward cipher initialization failed.");
|
2004-05-13 09:24:49 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reverse) {
|
|
|
|
tmp_digest = cpath->f_digest;
|
|
|
|
cpath->f_digest = cpath->b_digest;
|
|
|
|
cpath->b_digest = tmp_digest;
|
|
|
|
tmp_crypto = cpath->f_crypto;
|
|
|
|
cpath->f_crypto = cpath->b_crypto;
|
|
|
|
cpath->b_crypto = tmp_crypto;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-12-08 18:38:32 +01:00
|
|
|
/** A created or extended cell came back to us on the circuit, and it included
|
|
|
|
* <b>reply</b> as its body. (If <b>reply_type</b> is CELL_CREATED, the body
|
|
|
|
* contains (the second DH key, plus KH). If <b>reply_type</b> is
|
|
|
|
* CELL_CREATED_FAST, the body contains a secret y and a hash H(x|y).)
|
2004-05-13 09:24:49 +02:00
|
|
|
*
|
|
|
|
* Calculate the appropriate keys and digests, make sure KH is
|
|
|
|
* correct, and initialize this hop of the cpath.
|
|
|
|
*
|
2006-01-05 22:23:03 +01:00
|
|
|
* Return - reason if we want to mark circ for close, else return 0.
|
2004-05-13 09:24:49 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
2006-07-23 09:37:35 +02:00
|
|
|
circuit_finish_handshake(origin_circuit_t *circ, uint8_t reply_type,
|
2007-01-30 23:19:41 +01:00
|
|
|
const char *reply)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2005-05-07 07:55:06 +02:00
|
|
|
char keys[CPATH_KEY_MATERIAL_LEN];
|
2004-05-13 09:24:49 +02:00
|
|
|
crypt_path_t *hop;
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (circ->cpath->state == CPATH_STATE_AWAITING_KEYS)
|
2004-05-13 09:24:49 +02:00
|
|
|
hop = circ->cpath;
|
|
|
|
else {
|
2005-03-22 01:42:38 +01:00
|
|
|
hop = onion_next_hop_in_cpath(circ->cpath);
|
|
|
|
if (!hop) { /* got an extended when we're all done? */
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_PROTOCOL,"got extended when circ already built? Closing.");
|
2006-01-05 22:23:03 +01:00
|
|
|
return - END_CIRC_REASON_TORPROTOCOL;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
tor_assert(hop->state == CPATH_STATE_AWAITING_KEYS);
|
|
|
|
|
2005-05-03 00:35:18 +02:00
|
|
|
if (reply_type == CELL_CREATED && hop->dh_handshake_state) {
|
|
|
|
if (onion_skin_client_handshake(hop->dh_handshake_state, reply, keys,
|
|
|
|
DIGEST_LEN*2+CIPHER_KEY_LEN*2) < 0) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_CIRC,"onion_skin_client_handshake failed.");
|
2006-01-05 22:23:03 +01:00
|
|
|
return -END_CIRC_REASON_TORPROTOCOL;
|
2005-05-03 00:35:18 +02:00
|
|
|
}
|
|
|
|
/* Remember hash of g^xy */
|
|
|
|
memcpy(hop->handshake_digest, reply+DH_KEY_LEN, DIGEST_LEN);
|
|
|
|
} else if (reply_type == CELL_CREATED_FAST && !hop->dh_handshake_state) {
|
|
|
|
if (fast_client_handshake(hop->fast_handshake_state, reply, keys,
|
|
|
|
DIGEST_LEN*2+CIPHER_KEY_LEN*2) < 0) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_CIRC,"fast_client_handshake failed.");
|
2006-01-05 22:23:03 +01:00
|
|
|
return -END_CIRC_REASON_TORPROTOCOL;
|
2005-05-03 00:35:18 +02:00
|
|
|
}
|
|
|
|
memcpy(hop->handshake_digest, reply+DIGEST_LEN, DIGEST_LEN);
|
|
|
|
} else {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_PROTOCOL,"CREATED cell type did not match CREATE cell type.");
|
2006-01-05 22:23:03 +01:00
|
|
|
return -END_CIRC_REASON_TORPROTOCOL;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
2005-05-03 00:35:18 +02:00
|
|
|
if (hop->dh_handshake_state) {
|
|
|
|
crypto_dh_free(hop->dh_handshake_state); /* don't need it anymore */
|
|
|
|
hop->dh_handshake_state = NULL;
|
|
|
|
}
|
|
|
|
memset(hop->fast_handshake_state, 0, sizeof(hop->fast_handshake_state));
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
if (circuit_init_cpath_crypto(hop, keys, 0)<0) {
|
2006-01-05 22:23:03 +01:00
|
|
|
return -END_CIRC_REASON_TORPROTOCOL;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
hop->state = CPATH_STATE_OPEN;
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_CIRC,"Finished building %scircuit hop:",
|
|
|
|
(reply_type == CELL_CREATED_FAST) ? "fast " : "");
|
2005-10-24 21:37:45 +02:00
|
|
|
circuit_log_path(LOG_INFO,LD_CIRC,circ);
|
2006-10-09 17:47:27 +02:00
|
|
|
control_event_circuit_status(circ, CIRC_EVENT_EXTENDED, 0);
|
2004-11-03 19:33:07 +01:00
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** We received a relay truncated cell on circ.
|
|
|
|
*
|
|
|
|
* Since we don't ask for truncates currently, getting a truncated
|
|
|
|
* means that a connection broke or an extend failed. For now,
|
|
|
|
* just give up: for circ to close, and return 0.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
2006-07-23 09:37:35 +02:00
|
|
|
circuit_truncated(origin_circuit_t *circ, crypt_path_t *layer)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2004-10-11 03:17:42 +02:00
|
|
|
// crypt_path_t *victim;
|
|
|
|
// connection_t *stream;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2004-10-17 00:14:52 +02:00
|
|
|
tor_assert(circ);
|
2004-05-13 09:24:49 +02:00
|
|
|
tor_assert(layer);
|
|
|
|
|
|
|
|
/* XXX Since we don't ask for truncates currently, getting a truncated
|
|
|
|
* means that a connection broke or an extend failed. For now,
|
|
|
|
* just give up.
|
|
|
|
*/
|
2007-01-15 22:21:05 +01:00
|
|
|
circuit_mark_for_close(TO_CIRCUIT(circ),
|
2007-01-15 22:13:37 +01:00
|
|
|
END_CIRC_REASON_FLAG_REMOTE|END_CIRC_REASON_OR_CONN_CLOSED);
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0;
|
|
|
|
|
2004-10-11 03:17:42 +02:00
|
|
|
#if 0
|
2004-11-28 10:05:49 +01:00
|
|
|
while (layer->next != circ->cpath) {
|
2004-05-13 09:24:49 +02:00
|
|
|
/* we need to clear out layer->next */
|
|
|
|
victim = layer->next;
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_CIRC, "Killing a layer of the cpath.");
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
for (stream = circ->p_streams; stream; stream=stream->next_stream) {
|
|
|
|
if (stream->cpath_layer == victim) {
|
2006-10-03 00:13:42 +02:00
|
|
|
log_info(LD_APP, "Marking stream %d for close because of truncate.",
|
|
|
|
stream->stream_id);
|
2004-05-13 09:24:49 +02:00
|
|
|
/* no need to send 'end' relay cells,
|
|
|
|
* because the other side's already dead
|
|
|
|
*/
|
2005-04-03 00:11:24 +02:00
|
|
|
connection_mark_unattached_ap(stream, END_STREAM_REASON_DESTROY);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
layer->next = victim->next;
|
|
|
|
circuit_free_cpath_node(victim);
|
|
|
|
}
|
|
|
|
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_CIRC, "finished");
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0;
|
2004-10-11 03:17:42 +02:00
|
|
|
#endif
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Given a response payload and keys, initialize, then send a created
|
|
|
|
* cell back.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
2007-01-30 23:19:41 +01:00
|
|
|
onionskin_answer(or_circuit_t *circ, uint8_t cell_type, const char *payload,
|
|
|
|
const char *keys)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2004-05-13 09:24:49 +02:00
|
|
|
cell_t cell;
|
|
|
|
crypt_path_t *tmp_cpath;
|
|
|
|
|
|
|
|
tmp_cpath = tor_malloc_zero(sizeof(crypt_path_t));
|
2005-03-23 07:21:48 +01:00
|
|
|
tmp_cpath->magic = CRYPT_PATH_MAGIC;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
memset(&cell, 0, sizeof(cell_t));
|
2005-05-03 00:35:18 +02:00
|
|
|
cell.command = cell_type;
|
2004-05-13 09:24:49 +02:00
|
|
|
cell.circ_id = circ->p_circ_id;
|
|
|
|
|
2006-07-23 09:37:35 +02:00
|
|
|
circuit_set_state(TO_CIRCUIT(circ), CIRCUIT_STATE_OPEN);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2005-05-03 00:35:18 +02:00
|
|
|
memcpy(cell.payload, payload,
|
|
|
|
cell_type == CELL_CREATED ? ONIONSKIN_REPLY_LEN : DIGEST_LEN*2);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_CIRC,"init digest forward 0x%.8x, backward 0x%.8x.",
|
|
|
|
(unsigned int)*(uint32_t*)(keys),
|
|
|
|
(unsigned int)*(uint32_t*)(keys+20));
|
2004-05-13 09:24:49 +02:00
|
|
|
if (circuit_init_cpath_crypto(tmp_cpath, keys, 0)<0) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_BUG,"Circuit initialization failed");
|
2004-05-13 09:24:49 +02:00
|
|
|
tor_free(tmp_cpath);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
circ->n_digest = tmp_cpath->f_digest;
|
|
|
|
circ->n_crypto = tmp_cpath->f_crypto;
|
|
|
|
circ->p_digest = tmp_cpath->b_digest;
|
|
|
|
circ->p_crypto = tmp_cpath->b_crypto;
|
2005-03-23 07:21:48 +01:00
|
|
|
tmp_cpath->magic = 0;
|
2004-05-13 09:24:49 +02:00
|
|
|
tor_free(tmp_cpath);
|
|
|
|
|
2005-05-03 00:35:18 +02:00
|
|
|
if (cell_type == CELL_CREATED)
|
|
|
|
memcpy(circ->handshake_digest, cell.payload+DH_KEY_LEN, DIGEST_LEN);
|
|
|
|
else
|
|
|
|
memcpy(circ->handshake_digest, cell.payload+DIGEST_LEN, DIGEST_LEN);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2006-10-01 22:50:11 +02:00
|
|
|
circ->is_first_hop = (cell_type == CELL_CREATED_FAST);
|
|
|
|
|
2007-03-26 16:07:59 +02:00
|
|
|
append_cell_to_circuit_queue(TO_CIRCUIT(circ),
|
|
|
|
circ->p_conn, &cell, CELL_DIRECTION_IN);
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_CIRC,"Finished sending 'created' cell.");
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2006-07-26 21:07:26 +02:00
|
|
|
if (!is_local_IP(circ->p_conn->_base.addr) &&
|
2006-06-07 09:11:42 +02:00
|
|
|
!connection_or_nonopen_was_started_here(circ->p_conn)) {
|
2005-03-31 09:46:59 +02:00
|
|
|
/* record that we could process create cells from a non-local conn
|
|
|
|
* that we didn't initiate; presumably this means that create cells
|
|
|
|
* can reach us too. */
|
2005-03-15 02:44:46 +01:00
|
|
|
router_orport_found_reachable();
|
|
|
|
}
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Choose a length for a circuit of purpose <b>purpose</b>.
|
|
|
|
* Default length is 3 + the number of endpoints that would give something
|
|
|
|
* away. If the routerlist <b>routers</b> doesn't have enough routers
|
|
|
|
* to handle the desired path length, return as large a path length as
|
|
|
|
* is feasible, except if it's less than 2, in which case return -1.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static int
|
2007-02-08 23:07:56 +01:00
|
|
|
new_route_len(uint8_t purpose, extend_info_t *exit,
|
2005-09-10 03:01:40 +02:00
|
|
|
smartlist_t *routers)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2004-05-13 09:24:49 +02:00
|
|
|
int num_acceptable_routers;
|
|
|
|
int routelen;
|
|
|
|
|
2004-10-17 00:14:52 +02:00
|
|
|
tor_assert(routers);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
#ifdef TOR_PERF
|
|
|
|
routelen = 2;
|
|
|
|
#else
|
2005-09-10 03:01:40 +02:00
|
|
|
routelen = 3;
|
|
|
|
if (exit &&
|
|
|
|
purpose != CIRCUIT_PURPOSE_TESTING &&
|
|
|
|
purpose != CIRCUIT_PURPOSE_S_ESTABLISH_INTRO)
|
|
|
|
routelen++;
|
2004-05-13 09:24:49 +02:00
|
|
|
#endif
|
2006-03-18 01:22:23 +01:00
|
|
|
log_debug(LD_CIRC,"Chosen route length %d (%d routers available).",
|
|
|
|
routelen, smartlist_len(routers));
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
num_acceptable_routers = count_acceptable_routers(routers);
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (num_acceptable_routers < 2) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_CIRC,
|
|
|
|
"Not enough acceptable routers (%d). Discarding this circuit.",
|
|
|
|
num_acceptable_routers);
|
2004-05-13 09:24:49 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (num_acceptable_routers < routelen) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_CIRC,"Not enough routers: cutting routelen from %d to %d.",
|
|
|
|
routelen, num_acceptable_routers);
|
2004-05-13 09:24:49 +02:00
|
|
|
routelen = num_acceptable_routers;
|
|
|
|
}
|
|
|
|
|
|
|
|
return routelen;
|
|
|
|
}
|
|
|
|
|
2004-12-07 16:29:54 +01:00
|
|
|
/** Fetch the list of predicted ports, dup it into a smartlist of
|
|
|
|
* uint16_t's, remove the ones that are already handled by an
|
2004-12-05 08:10:08 +01:00
|
|
|
* existing circuit, and return it.
|
|
|
|
*/
|
|
|
|
static smartlist_t *
|
2005-06-11 20:52:12 +02:00
|
|
|
circuit_get_unhandled_ports(time_t now)
|
|
|
|
{
|
2004-12-07 16:29:54 +01:00
|
|
|
smartlist_t *source = rep_hist_get_predicted_ports(now);
|
|
|
|
smartlist_t *dest = smartlist_create();
|
|
|
|
uint16_t *tmp;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < smartlist_len(source); ++i) {
|
2007-07-26 22:26:53 +02:00
|
|
|
tmp = tor_malloc(sizeof(uint16_t));
|
2004-12-07 16:29:54 +01:00
|
|
|
memcpy(tmp, smartlist_get(source, i), sizeof(uint16_t));
|
|
|
|
smartlist_add(dest, tmp);
|
|
|
|
}
|
2004-12-05 08:10:08 +01:00
|
|
|
|
2004-12-07 16:29:54 +01:00
|
|
|
circuit_remove_handled_ports(dest);
|
|
|
|
return dest;
|
2004-12-05 08:10:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Return 1 if we already have circuits present or on the way for
|
|
|
|
* all anticipated ports. Return 0 if we should make more.
|
2005-01-12 05:58:23 +01:00
|
|
|
*
|
|
|
|
* If we're returning 0, set need_uptime and need_capacity to
|
|
|
|
* indicate any requirements that the unhandled ports have.
|
2004-12-05 08:10:08 +01:00
|
|
|
*/
|
|
|
|
int
|
2005-01-12 05:58:23 +01:00
|
|
|
circuit_all_predicted_ports_handled(time_t now, int *need_uptime,
|
2005-06-11 20:52:12 +02:00
|
|
|
int *need_capacity)
|
|
|
|
{
|
2005-01-12 05:58:23 +01:00
|
|
|
int i, enough;
|
|
|
|
uint16_t *port;
|
2004-12-05 08:10:08 +01:00
|
|
|
smartlist_t *sl = circuit_get_unhandled_ports(now);
|
2005-01-12 05:58:23 +01:00
|
|
|
smartlist_t *LongLivedServices = get_options()->LongLivedPorts;
|
2005-09-30 00:59:17 +02:00
|
|
|
tor_assert(need_uptime);
|
|
|
|
tor_assert(need_capacity);
|
2004-12-05 08:10:08 +01:00
|
|
|
enough = (smartlist_len(sl) == 0);
|
2005-01-12 05:58:23 +01:00
|
|
|
for (i = 0; i < smartlist_len(sl); ++i) {
|
|
|
|
port = smartlist_get(sl, i);
|
|
|
|
if (smartlist_string_num_isin(LongLivedServices, *port))
|
|
|
|
*need_uptime = 1;
|
|
|
|
tor_free(port);
|
|
|
|
}
|
2004-12-05 08:10:08 +01:00
|
|
|
smartlist_free(sl);
|
|
|
|
return enough;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return 1 if <b>router</b> can handle one or more of the ports in
|
|
|
|
* <b>needed_ports</b>, else return 0.
|
|
|
|
*/
|
|
|
|
static int
|
2005-06-11 20:52:12 +02:00
|
|
|
router_handles_some_port(routerinfo_t *router, smartlist_t *needed_ports)
|
|
|
|
{
|
2004-12-05 08:10:08 +01:00
|
|
|
int i;
|
|
|
|
uint16_t port;
|
|
|
|
|
|
|
|
for (i = 0; i < smartlist_len(needed_ports); ++i) {
|
2005-03-19 07:57:16 +01:00
|
|
|
addr_policy_result_t r;
|
2004-12-05 08:10:08 +01:00
|
|
|
port = *(uint16_t *)smartlist_get(needed_ports, i);
|
|
|
|
tor_assert(port);
|
2006-03-27 04:25:34 +02:00
|
|
|
r = compare_addr_to_addr_policy(0, port, router->exit_policy);
|
2005-03-19 07:57:16 +01:00
|
|
|
if (r != ADDR_POLICY_REJECTED && r != ADDR_POLICY_PROBABLY_REJECTED)
|
2004-12-05 08:10:08 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-24 08:50:38 +01:00
|
|
|
/** Return true iff <b>conn</b> needs another general circuit to be
|
2007-02-16 21:39:37 +01:00
|
|
|
* built. */
|
2005-01-19 18:59:37 +01:00
|
|
|
static int
|
2005-06-11 20:52:12 +02:00
|
|
|
ap_stream_wants_exit_attention(connection_t *conn)
|
|
|
|
{
|
2005-01-19 18:59:37 +01:00
|
|
|
if (conn->type == CONN_TYPE_AP &&
|
|
|
|
conn->state == AP_CONN_STATE_CIRCUIT_WAIT &&
|
|
|
|
!conn->marked_for_close &&
|
2006-07-26 21:07:26 +02:00
|
|
|
!connection_edge_is_rendezvous_stream(TO_EDGE_CONN(conn)) &&
|
|
|
|
!circuit_stream_is_being_handled(TO_EDGE_CONN(conn), 0,
|
|
|
|
MIN_CIRCUITS_HANDLING_STREAM))
|
2005-01-19 18:59:37 +01:00
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
/** Return a pointer to a suitable router to be the exit node for the
|
|
|
|
* general-purpose circuit we're about to build.
|
|
|
|
*
|
|
|
|
* Look through the connection array, and choose a router that maximizes
|
|
|
|
* the number of pending streams that can exit from this router.
|
|
|
|
*
|
|
|
|
* Return NULL if we can't find any suitable routers.
|
|
|
|
*/
|
2005-01-12 05:58:23 +01:00
|
|
|
static routerinfo_t *
|
|
|
|
choose_good_exit_server_general(routerlist_t *dir, int need_uptime,
|
|
|
|
int need_capacity)
|
2004-05-13 09:24:49 +02:00
|
|
|
{
|
|
|
|
int *n_supported;
|
2007-05-22 17:49:14 +02:00
|
|
|
int i;
|
2004-05-13 09:24:49 +02:00
|
|
|
int n_pending_connections = 0;
|
2007-05-22 17:49:14 +02:00
|
|
|
smartlist_t *connections;
|
2004-05-13 09:24:49 +02:00
|
|
|
int best_support = -1;
|
|
|
|
int n_best_support=0;
|
2005-12-31 07:32:57 +01:00
|
|
|
smartlist_t *sl, *preferredexits, *excludedexits;
|
2004-05-13 09:24:49 +02:00
|
|
|
routerinfo_t *router;
|
2004-11-06 06:18:11 +01:00
|
|
|
or_options_t *options = get_options();
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2007-05-22 17:49:14 +02:00
|
|
|
connections = get_connection_array();
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
/* Count how many connections are waiting for a circuit to be built.
|
|
|
|
* We use this for log messages now, but in the future we may depend on it.
|
|
|
|
*/
|
2007-05-22 17:49:14 +02:00
|
|
|
SMARTLIST_FOREACH(connections, connection_t *, conn,
|
|
|
|
{
|
|
|
|
if (ap_stream_wants_exit_attention(conn))
|
2004-05-13 09:24:49 +02:00
|
|
|
++n_pending_connections;
|
2007-05-22 17:49:14 +02:00
|
|
|
});
|
2004-12-04 01:25:54 +01:00
|
|
|
// log_fn(LOG_DEBUG, "Choosing exit node; %d connections are pending",
|
|
|
|
// n_pending_connections);
|
2004-05-13 09:24:49 +02:00
|
|
|
/* Now we count, for each of the routers in the directory, how many
|
|
|
|
* of the pending connections could possibly exit from that
|
|
|
|
* router (n_supported[i]). (We can't be sure about cases where we
|
|
|
|
* don't know the IP address of the pending connection.)
|
|
|
|
*/
|
|
|
|
n_supported = tor_malloc(sizeof(int)*smartlist_len(dir->routers));
|
2005-12-14 21:40:40 +01:00
|
|
|
for (i = 0; i < smartlist_len(dir->routers); ++i) {/* iterate over routers */
|
2004-05-13 09:24:49 +02:00
|
|
|
router = smartlist_get(dir->routers, i);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (router_is_me(router)) {
|
2004-05-13 09:24:49 +02:00
|
|
|
n_supported[i] = -1;
|
2004-12-04 01:25:54 +01:00
|
|
|
// log_fn(LOG_DEBUG,"Skipping node %s -- it's me.", router->nickname);
|
2004-05-13 09:24:49 +02:00
|
|
|
/* XXX there's probably a reverse predecessor attack here, but
|
|
|
|
* it's slow. should we take this out? -RD
|
|
|
|
*/
|
|
|
|
continue;
|
|
|
|
}
|
2006-10-12 00:06:01 +02:00
|
|
|
if (!router->is_running || router->is_bad_exit) {
|
2004-05-13 09:24:49 +02:00
|
|
|
n_supported[i] = -1;
|
2006-10-12 00:06:01 +02:00
|
|
|
continue; /* skip routers that are known to be down or bad exits */
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
2006-02-12 04:43:39 +01:00
|
|
|
if (router_is_unreliable(router, need_uptime, need_capacity, 0)) {
|
2005-01-12 05:58:23 +01:00
|
|
|
n_supported[i] = -1;
|
|
|
|
continue; /* skip routers that are not suitable */
|
|
|
|
}
|
2007-01-27 20:40:49 +01:00
|
|
|
if (!(router->is_valid || options->_AllowInvalid & ALLOW_INVALID_EXIT)) {
|
|
|
|
/* if it's invalid and we don't want it */
|
2004-07-21 11:35:24 +02:00
|
|
|
n_supported[i] = -1;
|
2006-03-19 02:21:59 +01:00
|
|
|
// log_fn(LOG_DEBUG,"Skipping node %s (index %d) -- invalid router.",
|
2004-12-04 01:25:54 +01:00
|
|
|
// router->nickname, i);
|
2006-03-19 02:21:59 +01:00
|
|
|
continue; /* skip invalid routers */
|
2004-07-21 11:35:24 +02:00
|
|
|
}
|
2004-11-28 10:05:49 +01:00
|
|
|
if (router_exit_policy_rejects_all(router)) {
|
2004-05-13 09:24:49 +02:00
|
|
|
n_supported[i] = -1;
|
2004-12-04 01:25:54 +01:00
|
|
|
// log_fn(LOG_DEBUG,"Skipping node %s (index %d) -- it rejects all.",
|
|
|
|
// router->nickname, i);
|
2004-05-13 09:24:49 +02:00
|
|
|
continue; /* skip routers that reject all */
|
|
|
|
}
|
|
|
|
n_supported[i] = 0;
|
2007-05-22 17:49:14 +02:00
|
|
|
/* iterate over connections */
|
|
|
|
SMARTLIST_FOREACH(connections, connection_t *, conn,
|
|
|
|
{
|
|
|
|
if (!ap_stream_wants_exit_attention(conn))
|
2004-05-13 09:24:49 +02:00
|
|
|
continue; /* Skip everything but APs in CIRCUIT_WAIT */
|
2007-05-22 17:49:14 +02:00
|
|
|
if (connection_ap_can_use_exit(TO_EDGE_CONN(conn), router)) {
|
2004-07-21 05:16:24 +02:00
|
|
|
++n_supported[i];
|
2004-12-04 01:25:54 +01:00
|
|
|
// log_fn(LOG_DEBUG,"%s is supported. n_supported[%d] now %d.",
|
|
|
|
// router->nickname, i, n_supported[i]);
|
2004-07-21 05:16:24 +02:00
|
|
|
} else {
|
2004-12-04 01:25:54 +01:00
|
|
|
// log_fn(LOG_DEBUG,"%s (index %d) would reject this stream.",
|
|
|
|
// router->nickname, i);
|
2004-07-21 05:16:24 +02:00
|
|
|
}
|
2007-05-22 17:49:14 +02:00
|
|
|
}); /* End looping over connections. */
|
2004-05-13 09:24:49 +02:00
|
|
|
if (n_supported[i] > best_support) {
|
|
|
|
/* If this router is better than previous ones, remember its index
|
|
|
|
* and goodness, and start counting how many routers are this good. */
|
|
|
|
best_support = n_supported[i]; n_best_support=1;
|
2004-12-04 01:25:54 +01:00
|
|
|
// log_fn(LOG_DEBUG,"%s is new best supported option so far.",
|
|
|
|
// router->nickname);
|
2004-05-13 09:24:49 +02:00
|
|
|
} else if (n_supported[i] == best_support) {
|
|
|
|
/* If this router is _as good_ as the best one, just increment the
|
|
|
|
* count of equally good routers.*/
|
|
|
|
++n_best_support;
|
|
|
|
}
|
|
|
|
}
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_CIRC,
|
|
|
|
"Found %d servers that might support %d/%d pending connections.",
|
|
|
|
n_best_support, best_support >= 0 ? best_support : 0,
|
|
|
|
n_pending_connections);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
preferredexits = smartlist_create();
|
2007-02-13 02:27:55 +01:00
|
|
|
add_nickname_list_to_smartlist(preferredexits,options->ExitNodes,1);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
excludedexits = smartlist_create();
|
2007-02-13 02:27:55 +01:00
|
|
|
add_nickname_list_to_smartlist(excludedexits,options->ExcludeNodes,0);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
sl = smartlist_create();
|
|
|
|
|
|
|
|
/* If any routers definitely support any pending connections, choose one
|
|
|
|
* at random. */
|
|
|
|
if (best_support > 0) {
|
|
|
|
for (i = 0; i < smartlist_len(dir->routers); i++)
|
|
|
|
if (n_supported[i] == best_support)
|
|
|
|
smartlist_add(sl, smartlist_get(dir->routers, i));
|
|
|
|
|
|
|
|
smartlist_subtract(sl,excludedexits);
|
2004-11-06 06:18:11 +01:00
|
|
|
if (options->StrictExitNodes || smartlist_overlap(sl,preferredexits))
|
2004-05-13 09:24:49 +02:00
|
|
|
smartlist_intersect(sl,preferredexits);
|
2007-08-24 14:33:53 +02:00
|
|
|
router = routerlist_sl_choose_by_bandwidth(sl, WEIGHT_FOR_EXIT);
|
2004-05-13 09:24:49 +02:00
|
|
|
} else {
|
|
|
|
/* Either there are no pending connections, or no routers even seem to
|
2004-12-05 08:10:08 +01:00
|
|
|
* possibly support any of them. Choose a router at random that satisfies
|
|
|
|
* at least one predicted exit port. */
|
|
|
|
|
|
|
|
int try;
|
2007-08-22 05:03:24 +02:00
|
|
|
smartlist_t *needed_ports;
|
2004-12-05 08:10:08 +01:00
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
if (best_support == -1) {
|
2005-08-27 00:16:32 +02:00
|
|
|
if (need_uptime || need_capacity) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_CIRC,
|
|
|
|
"We couldn't find any live%s%s routers; falling back "
|
|
|
|
"to list of all routers.",
|
|
|
|
need_capacity?", fast":"",
|
|
|
|
need_uptime?", stable":"");
|
2006-11-20 17:07:33 +01:00
|
|
|
smartlist_free(preferredexits);
|
|
|
|
smartlist_free(excludedexits);
|
|
|
|
smartlist_free(sl);
|
|
|
|
tor_free(n_supported);
|
|
|
|
return choose_good_exit_server_general(dir, 0, 0);
|
2005-08-27 00:16:32 +02:00
|
|
|
}
|
2006-06-29 15:11:23 +02:00
|
|
|
log_notice(LD_CIRC, "All routers are down or won't exit -- choosing a "
|
2006-02-13 09:28:42 +01:00
|
|
|
"doomed exit at random.");
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
2007-08-22 05:03:24 +02:00
|
|
|
needed_ports = circuit_get_unhandled_ports(time(NULL));
|
2004-12-05 08:10:08 +01:00
|
|
|
for (try = 0; try < 2; try++) {
|
|
|
|
/* try once to pick only from routers that satisfy a needed port,
|
|
|
|
* then if there are none, pick from any that support exiting. */
|
|
|
|
for (i = 0; i < smartlist_len(dir->routers); i++) {
|
|
|
|
router = smartlist_get(dir->routers, i);
|
|
|
|
if (n_supported[i] != -1 &&
|
|
|
|
(try || router_handles_some_port(router, needed_ports))) {
|
2005-12-14 21:40:40 +01:00
|
|
|
// log_fn(LOG_DEBUG,"Try %d: '%s' is a possibility.",
|
|
|
|
// try, router->nickname);
|
2004-12-05 08:10:08 +01:00
|
|
|
smartlist_add(sl, router);
|
|
|
|
}
|
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2004-12-05 08:10:08 +01:00
|
|
|
smartlist_subtract(sl,excludedexits);
|
|
|
|
if (options->StrictExitNodes || smartlist_overlap(sl,preferredexits))
|
|
|
|
smartlist_intersect(sl,preferredexits);
|
2005-10-29 21:13:48 +02:00
|
|
|
/* XXX sometimes the above results in null, when the requested
|
|
|
|
* exit node is down. we should pick it anyway. */
|
2007-08-24 14:33:53 +02:00
|
|
|
router = routerlist_sl_choose_by_bandwidth(sl, WEIGHT_FOR_EXIT);
|
2004-12-05 08:10:08 +01:00
|
|
|
if (router)
|
|
|
|
break;
|
|
|
|
}
|
2004-12-07 16:29:54 +01:00
|
|
|
SMARTLIST_FOREACH(needed_ports, uint16_t *, cp, tor_free(cp));
|
2004-12-05 08:10:08 +01:00
|
|
|
smartlist_free(needed_ports);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
smartlist_free(preferredexits);
|
|
|
|
smartlist_free(excludedexits);
|
|
|
|
smartlist_free(sl);
|
|
|
|
tor_free(n_supported);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (router) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_CIRC, "Chose exit server '%s'", router->nickname);
|
2004-05-13 09:24:49 +02:00
|
|
|
return router;
|
|
|
|
}
|
2005-10-24 21:37:45 +02:00
|
|
|
if (options->StrictExitNodes) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_CIRC,
|
2006-04-03 00:59:59 +02:00
|
|
|
"No specified exit routers seem to be running, and "
|
|
|
|
"StrictExitNodes is set: can't choose an exit.");
|
2005-10-24 21:37:45 +02:00
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return a pointer to a suitable router to be the exit node for the
|
|
|
|
* circuit of purpose <b>purpose</b> that we're about to build (or NULL
|
|
|
|
* if no router is suitable).
|
|
|
|
*
|
|
|
|
* For general-purpose circuits, pass it off to
|
|
|
|
* choose_good_exit_server_general()
|
|
|
|
*
|
|
|
|
* For client-side rendezvous circuits, choose a random node, weighted
|
|
|
|
* toward the preferences in 'options'.
|
|
|
|
*/
|
2005-01-12 05:58:23 +01:00
|
|
|
static routerinfo_t *
|
|
|
|
choose_good_exit_server(uint8_t purpose, routerlist_t *dir,
|
2005-11-24 07:43:39 +01:00
|
|
|
int need_uptime, int need_capacity, int is_internal)
|
2004-05-13 09:24:49 +02:00
|
|
|
{
|
2004-11-06 06:18:11 +01:00
|
|
|
or_options_t *options = get_options();
|
2004-11-28 10:05:49 +01:00
|
|
|
switch (purpose) {
|
2004-05-13 09:24:49 +02:00
|
|
|
case CIRCUIT_PURPOSE_C_GENERAL:
|
2005-11-24 07:43:39 +01:00
|
|
|
if (is_internal) /* pick it like a middle hop */
|
|
|
|
return router_choose_random_node(NULL, get_options()->ExcludeNodes,
|
2006-02-12 04:43:39 +01:00
|
|
|
NULL, need_uptime, need_capacity, 0,
|
2006-10-01 23:59:09 +02:00
|
|
|
get_options()->_AllowInvalid & ALLOW_INVALID_MIDDLE, 0, 0);
|
2005-11-24 07:43:39 +01:00
|
|
|
else
|
2005-12-14 21:40:40 +01:00
|
|
|
return choose_good_exit_server_general(dir,need_uptime,need_capacity);
|
2004-05-13 09:24:49 +02:00
|
|
|
case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
|
2005-12-14 21:40:40 +01:00
|
|
|
return router_choose_random_node(
|
|
|
|
options->RendNodes, options->RendExcludeNodes,
|
2006-02-12 04:43:39 +01:00
|
|
|
NULL, need_uptime, need_capacity, 0,
|
2006-10-01 23:59:09 +02:00
|
|
|
options->_AllowInvalid & ALLOW_INVALID_RENDEZVOUS, 0, 0);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
2007-03-04 21:11:46 +01:00
|
|
|
log_warn(LD_BUG,"Unhandled purpose %d", purpose);
|
2005-04-26 20:52:16 +02:00
|
|
|
tor_fragile_assert();
|
2004-10-17 00:56:46 +02:00
|
|
|
return NULL;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
2005-03-22 01:42:38 +01:00
|
|
|
/** Decide a suitable length for circ's cpath, and pick an exit
|
|
|
|
* router (or use <b>exit</b> if provided). Store these in the
|
|
|
|
* cpath. Return 0 if ok, -1 if circuit should be closed. */
|
|
|
|
static int
|
2006-07-23 09:37:35 +02:00
|
|
|
onion_pick_cpath_exit(origin_circuit_t *circ, extend_info_t *exit)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2005-03-22 01:42:38 +01:00
|
|
|
cpath_build_state_t *state = circ->build_state;
|
2005-10-18 19:43:54 +02:00
|
|
|
routerlist_t *rl = router_get_routerlist();
|
2004-11-06 06:18:11 +01:00
|
|
|
|
2006-12-13 03:49:45 +01:00
|
|
|
if (state->onehop_tunnel) {
|
|
|
|
log_debug(LD_CIRC, "Launching a one-hop circuit for dir tunnel.");
|
|
|
|
state->desired_path_len = 1;
|
|
|
|
} else {
|
2007-02-08 23:07:56 +01:00
|
|
|
int r = new_route_len(circ->_base.purpose, exit, rl->routers);
|
2006-12-13 03:49:45 +01:00
|
|
|
if (r < 1) /* must be at least 1 */
|
|
|
|
return -1;
|
|
|
|
state->desired_path_len = r;
|
|
|
|
}
|
2005-03-22 01:42:38 +01:00
|
|
|
|
|
|
|
if (exit) { /* the circuit-builder pre-requested one */
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_CIRC,"Using requested exit node '%s'", exit->nickname);
|
2005-06-29 23:46:55 +02:00
|
|
|
exit = extend_info_dup(exit);
|
2004-05-13 09:24:49 +02:00
|
|
|
} else { /* we have to decide one */
|
2005-07-14 10:43:19 +02:00
|
|
|
routerinfo_t *router =
|
2006-07-23 09:37:35 +02:00
|
|
|
choose_good_exit_server(circ->_base.purpose, rl, state->need_uptime,
|
|
|
|
state->need_capacity, state->is_internal);
|
2005-06-29 23:46:55 +02:00
|
|
|
if (!router) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_CIRC,"failed to choose an exit server");
|
2005-03-22 01:42:38 +01:00
|
|
|
return -1;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
2005-06-29 23:46:55 +02:00
|
|
|
exit = extend_info_from_router(router);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
2005-06-29 23:46:55 +02:00
|
|
|
state->chosen_exit = exit;
|
2005-03-22 01:42:38 +01:00
|
|
|
return 0;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
2005-03-22 02:01:15 +01:00
|
|
|
/** Give <b>circ</b> a new exit destination to <b>exit</b>, and add a
|
|
|
|
* hop to the cpath reflecting this. Don't send the next extend cell --
|
|
|
|
* the caller will do this if it wants to.
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
*/
|
|
|
|
int
|
2007-05-08 12:01:33 +02:00
|
|
|
circuit_append_new_exit(origin_circuit_t *circ, extend_info_t *exit)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2005-06-29 23:46:55 +02:00
|
|
|
cpath_build_state_t *state;
|
2007-05-08 12:01:33 +02:00
|
|
|
tor_assert(exit);
|
2006-07-23 09:37:35 +02:00
|
|
|
tor_assert(circ);
|
2005-06-29 23:46:55 +02:00
|
|
|
|
|
|
|
state = circ->build_state;
|
|
|
|
tor_assert(state);
|
|
|
|
if (state->chosen_exit)
|
|
|
|
extend_info_free(state->chosen_exit);
|
2007-05-08 12:01:33 +02:00
|
|
|
state->chosen_exit = extend_info_dup(exit);
|
2005-06-29 23:46:55 +02:00
|
|
|
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
++circ->build_state->desired_path_len;
|
2007-05-08 12:01:33 +02:00
|
|
|
onion_append_hop(&circ->cpath, exit);
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-12-26 06:52:44 +01:00
|
|
|
/** Take an open <b>circ</b>, and add a new hop at the end, based on
|
|
|
|
* <b>info</b>. Set its state back to CIRCUIT_STATE_BUILDING, and then
|
|
|
|
* send the next extend cell to begin connecting to that hop.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
2007-05-08 12:01:33 +02:00
|
|
|
circuit_extend_to_new_exit(origin_circuit_t *circ, extend_info_t *exit)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2006-10-09 17:47:27 +02:00
|
|
|
int err_reason = 0;
|
2007-05-08 12:01:33 +02:00
|
|
|
circuit_append_new_exit(circ, exit);
|
2006-07-23 09:37:35 +02:00
|
|
|
circuit_set_state(TO_CIRCUIT(circ), CIRCUIT_STATE_BUILDING);
|
2006-10-09 17:47:27 +02:00
|
|
|
if ((err_reason = circuit_send_next_onion_skin(circ))<0) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_CIRC, "Couldn't extend circuit to new point '%s'.",
|
2007-05-08 12:01:33 +02:00
|
|
|
exit->nickname);
|
2006-10-09 17:47:27 +02:00
|
|
|
circuit_mark_for_close(TO_CIRCUIT(circ), -err_reason);
|
2005-03-22 02:01:15 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
/** Return the number of routers in <b>routers</b> that are currently up
|
2004-08-18 22:35:11 +02:00
|
|
|
* and available for building circuits through.
|
2004-05-13 09:24:49 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static int
|
|
|
|
count_acceptable_routers(smartlist_t *routers)
|
|
|
|
{
|
2004-08-18 22:35:11 +02:00
|
|
|
int i, n;
|
2004-05-13 09:24:49 +02:00
|
|
|
int num=0;
|
2004-08-18 22:35:11 +02:00
|
|
|
routerinfo_t *r;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
n = smartlist_len(routers);
|
2004-11-28 10:05:49 +01:00
|
|
|
for (i=0;i<n;i++) {
|
2004-05-13 09:24:49 +02:00
|
|
|
r = smartlist_get(routers, i);
|
2006-03-18 02:24:04 +01:00
|
|
|
// log_debug(LD_CIRC,
|
|
|
|
// "Contemplating whether router %d (%s) is a new option.",
|
|
|
|
// i, r->nickname);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (r->is_running == 0) {
|
2006-03-18 02:24:04 +01:00
|
|
|
// log_debug(LD_CIRC,"Nope, the directory says %d is not running.",i);
|
2004-05-13 09:24:49 +02:00
|
|
|
goto next_i_loop;
|
|
|
|
}
|
2006-03-19 02:21:59 +01:00
|
|
|
if (r->is_valid == 0) {
|
|
|
|
// log_debug(LD_CIRC,"Nope, the directory says %d is not valid.",i);
|
2004-11-10 15:27:26 +01:00
|
|
|
goto next_i_loop;
|
2006-04-25 09:06:48 +02:00
|
|
|
/* XXX This clause makes us count incorrectly: if AllowInvalidRouters
|
|
|
|
* allows this node in some places, then we're getting an inaccurate
|
|
|
|
* count. For now, be conservative and don't count it. But later we
|
|
|
|
* should try to be smarter. */
|
2004-07-21 11:35:24 +02:00
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
num++;
|
2006-03-18 02:24:04 +01:00
|
|
|
// log_debug(LD_CIRC,"I like %d. num_acceptable_routers now %d.",i, num);
|
2004-05-13 09:24:49 +02:00
|
|
|
next_i_loop:
|
|
|
|
; /* C requires an explicit statement after the label */
|
|
|
|
}
|
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Add <b>new_hop</b> to the end of the doubly-linked-list <b>head_ptr</b>.
|
|
|
|
* This function is used to extend cpath by another hop.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
onion_append_to_cpath(crypt_path_t **head_ptr, crypt_path_t *new_hop)
|
2004-05-13 09:24:49 +02:00
|
|
|
{
|
|
|
|
if (*head_ptr) {
|
|
|
|
new_hop->next = (*head_ptr);
|
|
|
|
new_hop->prev = (*head_ptr)->prev;
|
|
|
|
(*head_ptr)->prev->next = new_hop;
|
|
|
|
(*head_ptr)->prev = new_hop;
|
|
|
|
} else {
|
|
|
|
*head_ptr = new_hop;
|
|
|
|
new_hop->prev = new_hop->next = new_hop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-13 12:25:22 +02:00
|
|
|
/** Pick a random server digest that's running a Tor version that
|
2006-06-18 23:07:45 +02:00
|
|
|
* doesn't have the reachability bug. These are versions 0.1.1.21-cvs+
|
2006-06-14 13:06:43 +02:00
|
|
|
* and 0.1.2.1-alpha+. Avoid picking authorities, since we're
|
|
|
|
* probably already connected to them.
|
2006-06-13 12:25:22 +02:00
|
|
|
*
|
|
|
|
* We only return one, so this doesn't become stupid when the
|
|
|
|
* whole network has upgraded. */
|
|
|
|
static char *
|
|
|
|
compute_preferred_testing_list(const char *answer)
|
|
|
|
{
|
|
|
|
smartlist_t *choices;
|
|
|
|
routerlist_t *rl = router_get_routerlist();
|
|
|
|
routerinfo_t *router;
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
if (answer) /* they have one in mind -- easy */
|
|
|
|
return tor_strdup(answer);
|
|
|
|
|
|
|
|
choices = smartlist_create();
|
|
|
|
/* now count up our choices */
|
|
|
|
SMARTLIST_FOREACH(rl->routers, routerinfo_t *, r,
|
|
|
|
if (r->is_running && r->is_valid &&
|
|
|
|
((tor_version_as_new_as(r->platform,"0.1.1.21-cvs") &&
|
|
|
|
!tor_version_as_new_as(r->platform,"0.1.2.0-alpha-cvs")) ||
|
2006-06-14 13:06:43 +02:00
|
|
|
tor_version_as_new_as(r->platform,"0.1.2.1-alpha")) &&
|
2006-10-03 07:45:58 +02:00
|
|
|
!is_local_IP(r->addr) &&
|
2006-06-14 13:06:43 +02:00
|
|
|
!router_get_trusteddirserver_by_digest(r->cache_info.identity_digest))
|
2006-06-13 12:25:22 +02:00
|
|
|
smartlist_add(choices, r));
|
|
|
|
router = smartlist_choose(choices);
|
|
|
|
smartlist_free(choices);
|
|
|
|
if (!router) {
|
|
|
|
log_info(LD_CIRC, "Looking for middle server that doesn't have the "
|
|
|
|
"reachability bug, but didn't find one. Oh well.");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
log_info(LD_CIRC, "Looking for middle server that doesn't have the "
|
|
|
|
"reachability bug, and chose '%s'. Great.", router->nickname);
|
2006-06-13 14:57:19 +02:00
|
|
|
s = tor_malloc(HEX_DIGEST_LEN+2);
|
|
|
|
s[0] = '$';
|
|
|
|
base16_encode(s+1, HEX_DIGEST_LEN+1,
|
2006-06-13 12:25:22 +02:00
|
|
|
router->cache_info.identity_digest, DIGEST_LEN);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2005-12-26 06:52:44 +01:00
|
|
|
/** A helper function used by onion_extend_cpath(). Use <b>purpose</b>
|
|
|
|
* and <b>state</b> and the cpath <b>head</b> (currently populated only
|
|
|
|
* to length <b>cur_len</b> to decide a suitable middle hop for a
|
|
|
|
* circuit. In particular, make sure we don't pick the exit node or its
|
|
|
|
* family, and make sure we don't duplicate any previous nodes or their
|
|
|
|
* families. */
|
2005-06-11 20:52:12 +02:00
|
|
|
static routerinfo_t *
|
|
|
|
choose_good_middle_server(uint8_t purpose,
|
|
|
|
cpath_build_state_t *state,
|
|
|
|
crypt_path_t *head,
|
|
|
|
int cur_len)
|
2004-08-15 10:15:12 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
routerinfo_t *r, *choice;
|
|
|
|
crypt_path_t *cpath;
|
2004-12-07 10:18:25 +01:00
|
|
|
smartlist_t *excluded;
|
2006-06-10 02:26:39 +02:00
|
|
|
or_options_t *options = get_options();
|
2006-06-13 12:25:22 +02:00
|
|
|
char *preferred = NULL;
|
2005-09-30 00:59:17 +02:00
|
|
|
tor_assert(_CIRCUIT_PURPOSE_MIN <= purpose &&
|
|
|
|
purpose <= _CIRCUIT_PURPOSE_MAX);
|
2004-08-15 10:15:12 +02:00
|
|
|
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_CIRC, "Contemplating intermediate hop: random choice.");
|
2004-08-15 10:15:12 +02:00
|
|
|
excluded = smartlist_create();
|
2005-06-29 23:46:55 +02:00
|
|
|
if ((r = build_state_get_exit_router(state))) {
|
2004-08-15 10:15:12 +02:00
|
|
|
smartlist_add(excluded, r);
|
2004-10-15 03:58:11 +02:00
|
|
|
routerlist_add_family(excluded, r);
|
2004-09-10 23:40:29 +02:00
|
|
|
}
|
2004-08-15 10:15:12 +02:00
|
|
|
for (i = 0, cpath = head; i < cur_len; ++i, cpath=cpath->next) {
|
2005-06-29 23:46:55 +02:00
|
|
|
if ((r = router_get_by_digest(cpath->extend_info->identity_digest))) {
|
2004-09-30 17:40:05 +02:00
|
|
|
smartlist_add(excluded, r);
|
2004-10-15 03:58:11 +02:00
|
|
|
routerlist_add_family(excluded, r);
|
2004-09-30 17:40:05 +02:00
|
|
|
}
|
2004-08-15 10:15:12 +02:00
|
|
|
}
|
2006-06-13 12:25:22 +02:00
|
|
|
if (purpose == CIRCUIT_PURPOSE_TESTING)
|
|
|
|
preferred = compute_preferred_testing_list(options->TestVia);
|
|
|
|
choice = router_choose_random_node(preferred,
|
2006-06-10 02:26:39 +02:00
|
|
|
options->ExcludeNodes, excluded,
|
2006-02-12 04:43:39 +01:00
|
|
|
state->need_uptime, state->need_capacity, 0,
|
2006-10-01 23:59:09 +02:00
|
|
|
options->_AllowInvalid & ALLOW_INVALID_MIDDLE, 0, 0);
|
2007-07-22 02:16:48 +02:00
|
|
|
tor_free(preferred);
|
2004-08-15 10:15:12 +02:00
|
|
|
smartlist_free(excluded);
|
|
|
|
return choice;
|
|
|
|
}
|
|
|
|
|
2005-07-23 06:12:35 +02:00
|
|
|
/** Pick a good entry server for the circuit to be built according to
|
2005-12-07 05:43:39 +01:00
|
|
|
* <b>state</b>. Don't reuse a chosen exit (if any), don't use this
|
|
|
|
* router (if we're an OR), and respect firewall settings; if we're
|
2007-06-12 11:17:23 +02:00
|
|
|
* configured to use entry guards, return one.
|
2005-07-22 19:32:25 +02:00
|
|
|
*
|
2007-06-12 11:17:23 +02:00
|
|
|
* If <b>state</b> is NULL, we're choosing a router to serve as an entry
|
|
|
|
* guard, not for any particular circuit.
|
2005-07-22 19:32:25 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static routerinfo_t *
|
2005-12-11 13:14:26 +01:00
|
|
|
choose_good_entry_server(uint8_t purpose, cpath_build_state_t *state)
|
2004-08-15 10:15:12 +02:00
|
|
|
{
|
|
|
|
routerinfo_t *r, *choice;
|
2006-01-11 05:14:10 +01:00
|
|
|
smartlist_t *excluded;
|
2004-11-06 06:18:11 +01:00
|
|
|
or_options_t *options = get_options();
|
2006-06-05 00:42:13 +02:00
|
|
|
(void)purpose; /* not used yet. */
|
2004-08-15 10:15:12 +02:00
|
|
|
|
2006-01-10 23:42:44 +01:00
|
|
|
if (state && options->UseEntryGuards) {
|
2005-12-28 10:07:31 +01:00
|
|
|
return choose_random_entry(state);
|
2005-07-22 19:32:25 +02:00
|
|
|
}
|
|
|
|
|
2006-01-11 05:14:10 +01:00
|
|
|
excluded = smartlist_create();
|
|
|
|
|
2005-07-22 19:32:25 +02:00
|
|
|
if (state && (r = build_state_get_exit_router(state))) {
|
2004-08-15 10:15:12 +02:00
|
|
|
smartlist_add(excluded, r);
|
2004-10-15 03:58:11 +02:00
|
|
|
routerlist_add_family(excluded, r);
|
2004-09-10 23:40:29 +02:00
|
|
|
}
|
2006-02-13 22:17:20 +01:00
|
|
|
if (firewall_is_fascist_or()) {
|
2004-08-15 10:15:12 +02:00
|
|
|
/* exclude all ORs that listen on the wrong port */
|
2005-10-18 19:43:54 +02:00
|
|
|
routerlist_t *rl = router_get_routerlist();
|
2004-08-15 10:15:12 +02:00
|
|
|
int i;
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
for (i=0; i < smartlist_len(rl->routers); i++) {
|
2004-08-15 10:15:12 +02:00
|
|
|
r = smartlist_get(rl->routers, i);
|
2006-02-13 22:17:20 +01:00
|
|
|
if (!fascist_firewall_allows_address_or(r->addr,r->or_port))
|
2005-08-07 23:24:00 +02:00
|
|
|
smartlist_add(excluded, r);
|
2004-08-15 10:15:12 +02:00
|
|
|
}
|
|
|
|
}
|
2006-06-09 11:02:32 +02:00
|
|
|
/* and exclude current entry guards, if applicable */
|
|
|
|
if (options->UseEntryGuards && entry_guards) {
|
|
|
|
SMARTLIST_FOREACH(entry_guards, entry_guard_t *, entry,
|
|
|
|
{
|
|
|
|
if ((r = router_get_by_digest(entry->identity)))
|
|
|
|
smartlist_add(excluded, r);
|
|
|
|
});
|
|
|
|
}
|
2006-10-03 00:13:42 +02:00
|
|
|
|
2005-12-14 21:40:40 +01:00
|
|
|
choice = router_choose_random_node(
|
2005-12-28 10:07:31 +01:00
|
|
|
NULL, options->ExcludeNodes,
|
2006-02-12 04:43:39 +01:00
|
|
|
excluded, state ? state->need_uptime : 0,
|
|
|
|
state ? state->need_capacity : 0,
|
|
|
|
state ? 0 : 1,
|
2006-10-01 23:59:09 +02:00
|
|
|
options->_AllowInvalid & ALLOW_INVALID_ENTRY, 0, 0);
|
2004-08-15 10:15:12 +02:00
|
|
|
smartlist_free(excluded);
|
|
|
|
return choice;
|
|
|
|
}
|
|
|
|
|
2005-03-22 01:42:38 +01:00
|
|
|
/** Return the first non-open hop in cpath, or return NULL if all
|
|
|
|
* hops are open. */
|
|
|
|
static crypt_path_t *
|
2005-06-11 20:52:12 +02:00
|
|
|
onion_next_hop_in_cpath(crypt_path_t *cpath)
|
|
|
|
{
|
2005-03-22 01:42:38 +01:00
|
|
|
crypt_path_t *hop = cpath;
|
|
|
|
do {
|
|
|
|
if (hop->state != CPATH_STATE_OPEN)
|
|
|
|
return hop;
|
|
|
|
hop = hop->next;
|
|
|
|
} while (hop != cpath);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
/** Choose a suitable next hop in the cpath <b>head_ptr</b>,
|
2005-03-22 01:42:38 +01:00
|
|
|
* based on <b>state</b>. Append the hop info to head_ptr.
|
2004-05-13 09:24:49 +02:00
|
|
|
*/
|
|
|
|
static int
|
2006-10-07 08:28:50 +02:00
|
|
|
onion_extend_cpath(origin_circuit_t *circ)
|
2004-05-13 09:24:49 +02:00
|
|
|
{
|
2006-10-07 08:28:50 +02:00
|
|
|
uint8_t purpose = circ->_base.purpose;
|
|
|
|
cpath_build_state_t *state = circ->build_state;
|
|
|
|
int cur_len = circuit_get_cpath_len(circ);
|
2005-06-29 23:46:55 +02:00
|
|
|
extend_info_t *info = NULL;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
if (cur_len >= state->desired_path_len) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_CIRC, "Path is complete: %d steps long",
|
|
|
|
state->desired_path_len);
|
2004-05-13 09:24:49 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2005-03-22 01:42:38 +01:00
|
|
|
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_CIRC, "Path is %d long; we want %d", cur_len,
|
|
|
|
state->desired_path_len);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (cur_len == state->desired_path_len - 1) { /* Picking last node */
|
2005-06-29 23:46:55 +02:00
|
|
|
info = extend_info_dup(state->chosen_exit);
|
2004-11-28 10:05:49 +01:00
|
|
|
} else if (cur_len == 0) { /* picking first node */
|
2005-12-11 13:14:26 +01:00
|
|
|
routerinfo_t *r = choose_good_entry_server(purpose, state);
|
2005-06-29 23:46:55 +02:00
|
|
|
if (r)
|
|
|
|
info = extend_info_from_router(r);
|
2004-05-13 09:24:49 +02:00
|
|
|
} else {
|
2005-06-29 23:46:55 +02:00
|
|
|
routerinfo_t *r =
|
2006-10-07 08:28:50 +02:00
|
|
|
choose_good_middle_server(purpose, state, circ->cpath, cur_len);
|
2005-06-29 23:46:55 +02:00
|
|
|
if (r)
|
|
|
|
info = extend_info_from_router(r);
|
2004-08-15 10:15:12 +02:00
|
|
|
}
|
|
|
|
|
2005-06-29 23:46:55 +02:00
|
|
|
if (!info) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_CIRC,"Failed to find node for hop %d of our path. Discarding "
|
|
|
|
"this circuit.", cur_len);
|
2004-08-15 10:15:12 +02:00
|
|
|
return -1;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_CIRC,"Chose router %s for hop %d (exit is %s)",
|
|
|
|
info->nickname, cur_len+1, build_state_get_exit_nickname(state));
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2006-10-07 08:28:50 +02:00
|
|
|
onion_append_hop(&circ->cpath, info);
|
2005-06-29 23:46:55 +02:00
|
|
|
extend_info_free(info);
|
2005-03-22 01:42:38 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Create a new hop, annotate it with information about its
|
|
|
|
* corresponding router <b>choice</b>, and append it to the
|
|
|
|
* end of the cpath <b>head_ptr</b>. */
|
|
|
|
static int
|
2005-06-29 23:46:55 +02:00
|
|
|
onion_append_hop(crypt_path_t **head_ptr, extend_info_t *choice)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2005-03-22 01:42:38 +01:00
|
|
|
crypt_path_t *hop = tor_malloc_zero(sizeof(crypt_path_t));
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
/* link hop into the cpath, at the end. */
|
|
|
|
onion_append_to_cpath(head_ptr, hop);
|
|
|
|
|
2005-03-23 07:21:48 +01:00
|
|
|
hop->magic = CRYPT_PATH_MAGIC;
|
2004-05-13 09:24:49 +02:00
|
|
|
hop->state = CPATH_STATE_CLOSED;
|
|
|
|
|
2005-06-29 23:46:55 +02:00
|
|
|
hop->extend_info = extend_info_dup(choice);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
hop->package_window = CIRCWINDOW_START;
|
|
|
|
hop->deliver_window = CIRCWINDOW_START;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2005-06-09 21:03:31 +02:00
|
|
|
|
2007-06-12 11:17:23 +02:00
|
|
|
/** Allocate a new extend_info object based on the various arguments. */
|
|
|
|
extend_info_t *
|
|
|
|
extend_info_alloc(const char *nickname, const char *digest,
|
|
|
|
crypto_pk_env_t *onion_key,
|
|
|
|
uint32_t addr, uint16_t port)
|
|
|
|
{
|
|
|
|
extend_info_t *info = tor_malloc_zero(sizeof(extend_info_t));
|
|
|
|
memcpy(info->identity_digest, digest, DIGEST_LEN);
|
2007-08-28 02:00:32 +02:00
|
|
|
if (nickname)
|
|
|
|
strlcpy(info->nickname, nickname, sizeof(info->nickname));
|
2007-06-12 11:17:23 +02:00
|
|
|
if (onion_key)
|
|
|
|
info->onion_key = crypto_pk_dup_key(onion_key);
|
|
|
|
info->addr = addr;
|
|
|
|
info->port = port;
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2005-06-29 23:46:55 +02:00
|
|
|
/** Allocate and return a new extend_info_t that can be used to build a
|
|
|
|
* circuit to or through the router <b>r</b>. */
|
|
|
|
extend_info_t *
|
|
|
|
extend_info_from_router(routerinfo_t *r)
|
|
|
|
{
|
|
|
|
tor_assert(r);
|
2007-06-12 11:17:23 +02:00
|
|
|
return extend_info_alloc(r->nickname, r->cache_info.identity_digest,
|
|
|
|
r->onion_pkey, r->addr, r->or_port);
|
2005-06-29 23:46:55 +02:00
|
|
|
}
|
|
|
|
|
2007-06-12 11:17:23 +02:00
|
|
|
#if 0
|
2007-05-08 12:11:53 +02:00
|
|
|
/** What router purpose is <b>digest</b>?
|
|
|
|
* It's a general purpose router unless it's on our bridges list.
|
|
|
|
*/
|
|
|
|
static uint8_t
|
2007-05-09 06:15:46 +02:00
|
|
|
get_router_purpose_from_digest(char *digest)
|
|
|
|
{
|
2007-06-12 11:17:23 +02:00
|
|
|
if (digest_is_a_bridge(digest))
|
|
|
|
return ROUTER_PURPOSE_BRIDGE;
|
|
|
|
return ROUTER_PURPOSE_GENERAL;
|
2007-05-08 12:11:53 +02:00
|
|
|
}
|
2007-06-12 11:17:23 +02:00
|
|
|
#endif
|
2007-05-08 12:11:53 +02:00
|
|
|
|
2007-06-12 11:17:23 +02:00
|
|
|
#if 0
|
2007-01-27 20:29:16 +01:00
|
|
|
/** Allocate and return a new extend_info_t that can be used to build a
|
|
|
|
* circuit to or through the router <b>r</b>. */
|
|
|
|
extend_info_t *
|
|
|
|
extend_info_from_routerstatus(routerstatus_t *s)
|
|
|
|
{
|
|
|
|
tor_assert(s);
|
2007-06-12 11:17:23 +02:00
|
|
|
/* routerstatus doesn't know onion_key; leave it NULL */
|
|
|
|
return extend_info_alloc(s->nickname, s->identity_digest,
|
|
|
|
NULL, s->addr, s->or_port);
|
|
|
|
// get_router_purpose_from_digest(s->identity_digest));
|
2007-01-27 20:29:16 +01:00
|
|
|
}
|
2007-06-12 11:17:23 +02:00
|
|
|
#endif
|
2007-01-27 20:29:16 +01:00
|
|
|
|
2005-06-29 23:46:55 +02:00
|
|
|
/** Release storage held by an extend_info_t struct. */
|
|
|
|
void
|
|
|
|
extend_info_free(extend_info_t *info)
|
|
|
|
{
|
|
|
|
tor_assert(info);
|
2007-01-27 20:29:16 +01:00
|
|
|
if (info->onion_key)
|
|
|
|
crypto_free_pk_env(info->onion_key);
|
2005-06-29 23:46:55 +02:00
|
|
|
tor_free(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Allocate and return a new extend_info_t with the same contents as
|
|
|
|
* <b>info</b>. */
|
|
|
|
extend_info_t *
|
|
|
|
extend_info_dup(extend_info_t *info)
|
|
|
|
{
|
|
|
|
extend_info_t *newinfo;
|
|
|
|
tor_assert(info);
|
|
|
|
newinfo = tor_malloc(sizeof(extend_info_t));
|
|
|
|
memcpy(newinfo, info, sizeof(extend_info_t));
|
2007-01-27 20:29:16 +01:00
|
|
|
if (info->onion_key)
|
|
|
|
newinfo->onion_key = crypto_pk_dup_key(info->onion_key);
|
|
|
|
else
|
|
|
|
newinfo->onion_key = NULL;
|
2005-06-29 23:46:55 +02:00
|
|
|
return newinfo;
|
|
|
|
}
|
|
|
|
|
2005-12-26 06:52:44 +01:00
|
|
|
/** Return the routerinfo_t for the chosen exit router in <b>state</b>.
|
|
|
|
* If there is no chosen exit, or if we don't know the routerinfo_t for
|
|
|
|
* the chosen exit, return NULL.
|
2005-06-29 23:46:55 +02:00
|
|
|
*/
|
|
|
|
routerinfo_t *
|
|
|
|
build_state_get_exit_router(cpath_build_state_t *state)
|
|
|
|
{
|
|
|
|
if (!state || !state->chosen_exit)
|
|
|
|
return NULL;
|
|
|
|
return router_get_by_digest(state->chosen_exit->identity_digest);
|
|
|
|
}
|
|
|
|
|
2005-12-26 06:52:44 +01:00
|
|
|
/** Return the nickname for the chosen exit router in <b>state</b>. If
|
2005-06-29 23:46:55 +02:00
|
|
|
* there is no chosen exit, or if we don't know the routerinfo_t for the
|
|
|
|
* chosen exit, return NULL.
|
|
|
|
*/
|
|
|
|
const char *
|
|
|
|
build_state_get_exit_nickname(cpath_build_state_t *state)
|
|
|
|
{
|
|
|
|
if (!state || !state->chosen_exit)
|
|
|
|
return NULL;
|
|
|
|
return state->chosen_exit->nickname;
|
|
|
|
}
|
|
|
|
|
2006-09-29 01:57:52 +02:00
|
|
|
/** Check whether the entry guard <b>e</b> is usable, given the directory
|
2007-06-10 02:30:14 +02:00
|
|
|
* authorities' opinion about the router (stored in <b>ri</b>) and the user's
|
2006-10-07 01:37:07 +02:00
|
|
|
* configuration (in <b>options</b>). Set <b>e</b>->bad_since
|
2007-11-26 03:18:57 +01:00
|
|
|
* accordingly. Return true iff the entry guard's status changes.
|
|
|
|
*
|
|
|
|
* If it's not usable, set *<b>reason</b> to a static string explaining why.
|
|
|
|
*/
|
2006-09-29 01:57:44 +02:00
|
|
|
static int
|
|
|
|
entry_guard_set_status(entry_guard_t *e, routerinfo_t *ri,
|
2007-11-26 03:18:57 +01:00
|
|
|
time_t now, or_options_t *options, const char **reason)
|
2006-09-29 01:57:44 +02:00
|
|
|
{
|
|
|
|
char buf[HEX_DIGEST_LEN+1];
|
|
|
|
int changed = 0;
|
|
|
|
|
2006-09-29 01:57:56 +02:00
|
|
|
tor_assert(options);
|
|
|
|
|
2007-11-26 03:18:57 +01:00
|
|
|
*reason = NULL;
|
|
|
|
|
2006-09-29 01:57:44 +02:00
|
|
|
/* Do we want to mark this guard as bad? */
|
|
|
|
if (!ri)
|
2007-11-26 03:18:57 +01:00
|
|
|
*reason = "unlisted";
|
2006-09-29 01:57:44 +02:00
|
|
|
else if (!ri->is_running)
|
2007-11-26 03:18:57 +01:00
|
|
|
*reason = "down";
|
2007-06-12 11:17:23 +02:00
|
|
|
else if (options->UseBridges && ri->purpose != ROUTER_PURPOSE_BRIDGE)
|
2007-11-26 03:18:57 +01:00
|
|
|
*reason = "not a bridge";
|
2007-06-12 11:17:23 +02:00
|
|
|
else if (!options->UseBridges && !ri->is_possible_guard &&
|
2006-09-29 01:57:56 +02:00
|
|
|
!router_nickname_is_in_list(ri, options->EntryNodes))
|
2007-11-26 03:18:57 +01:00
|
|
|
*reason = "not recommended as a guard";
|
2006-09-29 01:57:56 +02:00
|
|
|
else if (router_nickname_is_in_list(ri, options->ExcludeNodes))
|
2007-11-26 03:18:57 +01:00
|
|
|
*reason = "excluded";
|
2006-09-29 01:57:44 +02:00
|
|
|
|
2007-11-26 03:18:57 +01:00
|
|
|
if (*reason && ! e->bad_since) {
|
2006-09-29 01:57:44 +02:00
|
|
|
/* Router is newly bad. */
|
|
|
|
base16_encode(buf, sizeof(buf), e->identity, DIGEST_LEN);
|
2006-10-07 01:37:07 +02:00
|
|
|
log_info(LD_CIRC, "Entry guard %s (%s) is %s: marking as unusable.",
|
2007-11-26 03:18:57 +01:00
|
|
|
e->nickname, buf, *reason);
|
2006-09-29 01:57:44 +02:00
|
|
|
|
2006-10-07 01:37:07 +02:00
|
|
|
e->bad_since = now;
|
2006-12-15 23:40:20 +01:00
|
|
|
control_event_guard(e->nickname, e->identity, "BAD");
|
2006-09-29 01:57:44 +02:00
|
|
|
changed = 1;
|
2007-11-26 03:18:57 +01:00
|
|
|
} else if (!*reason && e->bad_since) {
|
2006-09-29 01:57:44 +02:00
|
|
|
/* There's nothing wrong with the router any more. */
|
|
|
|
base16_encode(buf, sizeof(buf), e->identity, DIGEST_LEN);
|
|
|
|
log_info(LD_CIRC, "Entry guard %s (%s) is no longer unusable: "
|
2006-10-07 01:37:07 +02:00
|
|
|
"marking as ok.", e->nickname, buf);
|
2006-09-29 01:57:44 +02:00
|
|
|
|
|
|
|
e->bad_since = 0;
|
2006-12-15 23:40:20 +01:00
|
|
|
control_event_guard(e->nickname, e->identity, "GOOD");
|
2006-09-29 01:57:44 +02:00
|
|
|
changed = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
2007-07-22 02:16:48 +02:00
|
|
|
/** Return true iff enough time has passed since we last tried to connect
|
|
|
|
* to the unreachable guard <b>e</b> that we're willing to try again. */
|
2006-09-29 01:57:44 +02:00
|
|
|
static int
|
|
|
|
entry_is_time_to_retry(entry_guard_t *e, time_t now)
|
|
|
|
{
|
|
|
|
long diff;
|
|
|
|
if (e->last_attempted < e->unreachable_since)
|
|
|
|
return 1;
|
|
|
|
diff = now - e->unreachable_since;
|
|
|
|
if (diff < 6*60*60)
|
|
|
|
return now > (e->last_attempted + 60*60);
|
|
|
|
else if (diff < 3*24*60*60)
|
|
|
|
return now > (e->last_attempted + 4*60*60);
|
|
|
|
else if (diff < 7*24*60*60)
|
|
|
|
return now > (e->last_attempted + 18*60*60);
|
|
|
|
else
|
|
|
|
return now > (e->last_attempted + 36*60*60);
|
|
|
|
}
|
|
|
|
|
2005-12-28 10:07:31 +01:00
|
|
|
/** Return the router corresponding to <b>e</b>, if <b>e</b> is
|
|
|
|
* working well enough that we are willing to use it as an entry
|
2005-12-27 08:23:52 +01:00
|
|
|
* right now. (Else return NULL.) In particular, it must be
|
|
|
|
* - Listed as either up or never yet contacted;
|
|
|
|
* - Present in the routerlist;
|
2005-12-27 08:37:21 +01:00
|
|
|
* - Listed as 'stable' or 'fast' by the current dirserver concensus,
|
2006-10-07 01:37:07 +02:00
|
|
|
* if demanded by <b>need_uptime</b> or <b>need_capacity</b>;
|
2006-10-07 04:55:44 +02:00
|
|
|
* (This check is currently redundant with the Guard flag, but in
|
|
|
|
* the future that might change. Best to leave it in for now.)
|
2007-07-22 02:16:48 +02:00
|
|
|
* - Allowed by our current ReachableORAddresses config option; and
|
2006-09-29 01:57:52 +02:00
|
|
|
* - Currently thought to be reachable by us (unless assume_reachable
|
|
|
|
* is true).
|
2005-12-27 08:23:52 +01:00
|
|
|
*/
|
|
|
|
static INLINE routerinfo_t *
|
2006-09-29 01:57:44 +02:00
|
|
|
entry_is_live(entry_guard_t *e, int need_uptime, int need_capacity,
|
|
|
|
int assume_reachable)
|
2005-12-27 08:23:52 +01:00
|
|
|
{
|
|
|
|
routerinfo_t *r;
|
2006-09-29 01:57:44 +02:00
|
|
|
if (e->bad_since)
|
|
|
|
return NULL;
|
2007-02-23 19:34:35 +01:00
|
|
|
/* no good if it's unreachable, unless assume_unreachable or can_retry. */
|
|
|
|
if ((!assume_reachable && !e->can_retry) &&
|
2006-09-29 01:57:44 +02:00
|
|
|
e->unreachable_since && !entry_is_time_to_retry(e, time(NULL)))
|
2005-12-27 08:23:52 +01:00
|
|
|
return NULL;
|
2005-12-28 10:07:31 +01:00
|
|
|
r = router_get_by_digest(e->identity);
|
2005-12-27 08:23:52 +01:00
|
|
|
if (!r)
|
|
|
|
return NULL;
|
2007-06-12 11:17:23 +02:00
|
|
|
if (get_options()->UseBridges && r->purpose != ROUTER_PURPOSE_BRIDGE)
|
|
|
|
return NULL;
|
|
|
|
if (!get_options()->UseBridges && r->purpose != ROUTER_PURPOSE_GENERAL)
|
|
|
|
return NULL;
|
2006-02-12 04:43:39 +01:00
|
|
|
if (router_is_unreliable(r, need_uptime, need_capacity, 0))
|
2005-12-27 08:23:52 +01:00
|
|
|
return NULL;
|
2007-07-22 02:16:48 +02:00
|
|
|
if (!fascist_firewall_allows_address_or(r->addr,r->or_port))
|
2005-12-27 08:23:52 +01:00
|
|
|
return NULL;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2006-01-10 23:42:44 +01:00
|
|
|
/** Return the number of entry guards that we think are usable. */
|
2005-07-22 19:32:25 +02:00
|
|
|
static int
|
2006-01-10 23:42:44 +01:00
|
|
|
num_live_entry_guards(void)
|
2005-07-22 19:32:25 +02:00
|
|
|
{
|
|
|
|
int n = 0;
|
2006-01-10 23:42:44 +01:00
|
|
|
if (! entry_guards)
|
2005-07-23 06:12:35 +02:00
|
|
|
return 0;
|
2006-01-10 23:42:44 +01:00
|
|
|
SMARTLIST_FOREACH(entry_guards, entry_guard_t *, entry,
|
2005-12-27 08:23:52 +01:00
|
|
|
{
|
2006-09-29 01:57:44 +02:00
|
|
|
if (entry_is_live(entry, 0, 1, 0))
|
2005-12-27 08:23:52 +01:00
|
|
|
++n;
|
|
|
|
});
|
2005-07-22 19:32:25 +02:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2007-06-15 20:32:27 +02:00
|
|
|
/** If <b>digest</b> matches the identity of any node in the
|
|
|
|
* entry_guards list, return that node. Else return NULL. */
|
|
|
|
static INLINE entry_guard_t *
|
2007-01-30 23:19:31 +01:00
|
|
|
is_an_entry_guard(const char *digest)
|
2005-12-27 08:23:52 +01:00
|
|
|
{
|
2006-01-10 23:42:44 +01:00
|
|
|
SMARTLIST_FOREACH(entry_guards, entry_guard_t *, entry,
|
2006-01-05 22:23:03 +01:00
|
|
|
if (!memcmp(digest, entry->identity, DIGEST_LEN))
|
2007-06-15 20:32:27 +02:00
|
|
|
return entry;
|
2005-12-27 08:23:52 +01:00
|
|
|
);
|
2007-06-15 20:32:27 +02:00
|
|
|
return NULL;
|
2005-12-27 08:23:52 +01:00
|
|
|
}
|
|
|
|
|
2006-09-29 01:57:52 +02:00
|
|
|
/** Dump a description of our list of entry guards to the log at level
|
2006-10-07 02:50:39 +02:00
|
|
|
* <b>severity</b>. */
|
2005-12-31 07:32:57 +01:00
|
|
|
static void
|
2006-01-10 23:42:44 +01:00
|
|
|
log_entry_guards(int severity)
|
2005-12-31 07:32:57 +01:00
|
|
|
{
|
|
|
|
smartlist_t *elements = smartlist_create();
|
|
|
|
char buf[1024];
|
|
|
|
char *s;
|
|
|
|
|
2006-01-10 23:42:44 +01:00
|
|
|
SMARTLIST_FOREACH(entry_guards, entry_guard_t *, e,
|
2005-12-31 07:32:57 +01:00
|
|
|
{
|
2006-09-29 01:57:44 +02:00
|
|
|
tor_snprintf(buf, sizeof(buf), "%s (%s%s)",
|
2005-12-31 07:32:57 +01:00
|
|
|
e->nickname,
|
2006-09-29 01:57:44 +02:00
|
|
|
e->bad_since ? "down " : "up ",
|
2005-12-31 07:32:57 +01:00
|
|
|
e->made_contact ? "made-contact" : "never-contacted");
|
|
|
|
smartlist_add(elements, tor_strdup(buf));
|
|
|
|
});
|
|
|
|
|
|
|
|
s = smartlist_join_strings(elements, ",", 0, NULL);
|
|
|
|
SMARTLIST_FOREACH(elements, char*, cp, tor_free(cp));
|
|
|
|
smartlist_free(elements);
|
|
|
|
log_fn(severity,LD_CIRC,"%s",s);
|
|
|
|
tor_free(s);
|
|
|
|
}
|
|
|
|
|
2007-01-24 01:43:52 +01:00
|
|
|
/** Called when one or more guards that we would previously have used for some
|
|
|
|
* purpose are no longer in use because a higher-priority guard has become
|
|
|
|
* useable again. */
|
2006-12-15 23:40:20 +01:00
|
|
|
static void
|
|
|
|
control_event_guard_deferred(void)
|
|
|
|
{
|
2007-01-24 01:43:52 +01:00
|
|
|
/* XXXX We don't actually have a good way to figure out _how many_ entries
|
|
|
|
* are live for some purpose. We need an entry_is_even_slightly_live()
|
|
|
|
* function for this to work right. NumEntryGuards isn't reliable: if we
|
|
|
|
* need guards with weird properties, we can have more than that number
|
|
|
|
* live.
|
|
|
|
**/
|
2006-12-15 23:40:20 +01:00
|
|
|
#if 0
|
|
|
|
int n = 0;
|
|
|
|
or_options_t *options = get_options();
|
|
|
|
if (!entry_guards)
|
|
|
|
return;
|
|
|
|
SMARTLIST_FOREACH(entry_guards, entry_guard_t *, entry,
|
|
|
|
{
|
|
|
|
if (entry_is_live(entry, 0, 1, 0)) {
|
|
|
|
if (n++ == options->NumEntryGuards) {
|
|
|
|
control_event_guard(entry->nickname, entry->identity, "DEFERRED");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2006-06-09 11:02:32 +02:00
|
|
|
/** Add a new (preferably stable and fast) router to our
|
2006-01-10 23:42:44 +01:00
|
|
|
* entry_guards list. Return a pointer to the router if we succeed,
|
2005-12-31 07:32:57 +01:00
|
|
|
* or NULL if we can't find any more suitable entries.
|
2005-12-28 10:07:31 +01:00
|
|
|
*
|
2005-12-31 07:32:57 +01:00
|
|
|
* If <b>chosen</b> is defined, use that one, and if it's not
|
2006-01-10 23:42:44 +01:00
|
|
|
* already in our entry_guards list, put it at the *beginning*.
|
2005-12-31 07:32:57 +01:00
|
|
|
* Else, put the one we pick at the end of the list. */
|
2005-12-27 08:23:52 +01:00
|
|
|
static routerinfo_t *
|
2007-06-15 20:32:27 +02:00
|
|
|
add_an_entry_guard(routerinfo_t *chosen, int reset_status)
|
2005-12-27 08:23:52 +01:00
|
|
|
{
|
2005-12-28 10:07:31 +01:00
|
|
|
routerinfo_t *router;
|
2006-01-10 23:42:44 +01:00
|
|
|
entry_guard_t *entry;
|
2005-12-31 07:32:57 +01:00
|
|
|
|
2006-06-09 11:07:59 +02:00
|
|
|
if (chosen) {
|
2005-12-28 10:07:31 +01:00
|
|
|
router = chosen;
|
2007-06-15 20:32:27 +02:00
|
|
|
entry = is_an_entry_guard(router->cache_info.identity_digest);
|
|
|
|
if (entry) {
|
2007-12-24 11:31:39 +01:00
|
|
|
if (reset_status) {
|
2007-06-15 20:32:27 +02:00
|
|
|
entry->bad_since = 0;
|
2007-12-24 11:31:39 +01:00
|
|
|
entry->can_retry = 1;
|
|
|
|
}
|
2006-06-09 11:07:59 +02:00
|
|
|
return NULL;
|
2007-06-15 20:32:27 +02:00
|
|
|
}
|
2006-06-09 11:07:59 +02:00
|
|
|
} else {
|
2005-12-28 10:07:31 +01:00
|
|
|
router = choose_good_entry_server(CIRCUIT_PURPOSE_C_GENERAL, NULL);
|
2006-06-09 11:07:59 +02:00
|
|
|
if (!router)
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-01-10 23:42:44 +01:00
|
|
|
entry = tor_malloc_zero(sizeof(entry_guard_t));
|
2006-03-09 01:55:55 +01:00
|
|
|
log_info(LD_CIRC, "Chose '%s' as new entry guard.", router->nickname);
|
2005-12-28 10:07:31 +01:00
|
|
|
strlcpy(entry->nickname, router->nickname, sizeof(entry->nickname));
|
|
|
|
memcpy(entry->identity, router->cache_info.identity_digest, DIGEST_LEN);
|
2007-08-29 19:22:00 +02:00
|
|
|
entry->chosen_on_date = start_of_month(time(NULL));
|
|
|
|
entry->chosen_by_version = tor_strdup(VERSION);
|
2006-06-09 11:02:32 +02:00
|
|
|
if (chosen) /* prepend */
|
2006-01-10 23:42:44 +01:00
|
|
|
smartlist_insert(entry_guards, 0, entry);
|
2006-06-09 11:02:32 +02:00
|
|
|
else /* append */
|
2006-01-10 23:42:44 +01:00
|
|
|
smartlist_add(entry_guards, entry);
|
2006-12-15 23:40:20 +01:00
|
|
|
control_event_guard(entry->nickname, entry->identity, "NEW");
|
|
|
|
control_event_guard_deferred();
|
2006-01-10 23:42:44 +01:00
|
|
|
log_entry_guards(LOG_INFO);
|
2005-12-28 10:07:31 +01:00
|
|
|
return router;
|
2005-12-27 08:23:52 +01:00
|
|
|
}
|
|
|
|
|
2006-01-10 23:42:44 +01:00
|
|
|
/** If the use of entry guards is configured, choose more entry guards
|
2005-12-26 06:52:44 +01:00
|
|
|
* until we have enough in the list. */
|
2005-07-22 19:32:25 +02:00
|
|
|
static void
|
2006-01-10 23:42:44 +01:00
|
|
|
pick_entry_guards(void)
|
2005-07-22 19:32:25 +02:00
|
|
|
{
|
|
|
|
or_options_t *options = get_options();
|
2005-08-04 21:56:41 +02:00
|
|
|
int changed = 0;
|
2005-07-22 19:32:25 +02:00
|
|
|
|
2006-01-10 23:42:44 +01:00
|
|
|
tor_assert(entry_guards);
|
2005-07-22 19:32:25 +02:00
|
|
|
|
2006-01-10 23:42:44 +01:00
|
|
|
while (num_live_entry_guards() < options->NumEntryGuards) {
|
2007-06-15 20:32:27 +02:00
|
|
|
if (!add_an_entry_guard(NULL, 0))
|
2005-12-27 08:23:52 +01:00
|
|
|
break;
|
2005-08-04 21:56:41 +02:00
|
|
|
changed = 1;
|
2005-07-22 19:32:25 +02:00
|
|
|
}
|
2005-08-04 21:56:41 +02:00
|
|
|
if (changed)
|
2006-01-10 23:42:44 +01:00
|
|
|
entry_guards_changed();
|
2005-07-22 19:32:25 +02:00
|
|
|
}
|
|
|
|
|
2006-09-29 01:57:44 +02:00
|
|
|
/** How long (in seconds) do we allow an entry guard to be nonfunctional,
|
2006-10-07 01:37:07 +02:00
|
|
|
* unlisted, excluded, or otherwise nonusable before we give up on it? */
|
2006-09-29 01:57:44 +02:00
|
|
|
#define ENTRY_GUARD_REMOVE_AFTER (30*24*60*60)
|
2005-07-22 19:32:25 +02:00
|
|
|
|
2007-08-29 19:22:00 +02:00
|
|
|
/** Release all storage held by <b>e</b>. */
|
|
|
|
static void
|
|
|
|
entry_guard_free(entry_guard_t *e)
|
|
|
|
{
|
|
|
|
tor_assert(e);
|
|
|
|
tor_free(e->chosen_by_version);
|
|
|
|
tor_free(e);
|
|
|
|
}
|
|
|
|
|
2007-10-04 18:21:58 +02:00
|
|
|
/** Remove any entry guard which was selected by an unknown version of Tor,
|
|
|
|
* or which was selected by a version of Tor that's known to select
|
|
|
|
* entry guards badly. */
|
2007-08-29 19:22:00 +02:00
|
|
|
static int
|
|
|
|
remove_obsolete_entry_guards(void)
|
|
|
|
{
|
2007-09-01 19:50:29 +02:00
|
|
|
int changed = 0, i;
|
2007-08-29 19:22:00 +02:00
|
|
|
for (i = 0; i < smartlist_len(entry_guards); ++i) {
|
|
|
|
entry_guard_t *entry = smartlist_get(entry_guards, i);
|
|
|
|
const char *ver = entry->chosen_by_version;
|
|
|
|
const char *msg = NULL;
|
|
|
|
tor_version_t v;
|
|
|
|
int version_is_bad = 0;
|
|
|
|
if (!ver) {
|
|
|
|
msg = "does not say what version of Tor it was selected by";
|
|
|
|
version_is_bad = 1;
|
|
|
|
} else if (tor_version_parse(ver, &v)) {
|
|
|
|
msg = "does not seem to be from any recognized version of Tor";
|
|
|
|
version_is_bad = 1;
|
|
|
|
} else if ((tor_version_as_new_as(ver, "0.1.0.10-alpha") &&
|
|
|
|
!tor_version_as_new_as(ver, "0.1.2.16-dev")) ||
|
|
|
|
(tor_version_as_new_as(ver, "0.2.0.0-alpha") &&
|
|
|
|
!tor_version_as_new_as(ver, "0.2.0.6-alpha"))) {
|
|
|
|
msg = "was selected without regard for guard bandwidth";
|
|
|
|
version_is_bad = 1;
|
|
|
|
}
|
|
|
|
if (version_is_bad) {
|
|
|
|
char dbuf[HEX_DIGEST_LEN+1];
|
|
|
|
tor_assert(msg);
|
|
|
|
base16_encode(dbuf, sizeof(dbuf), entry->identity, DIGEST_LEN);
|
|
|
|
log_notice(LD_CIRC, "Entry guard '%s' (%s) %s. (Version=%s.) "
|
|
|
|
"Replacing it.",
|
|
|
|
entry->nickname, dbuf, msg, ver?escaped(ver):"none");
|
|
|
|
control_event_guard(entry->nickname, entry->identity, "DROPPED");
|
|
|
|
entry_guard_free(entry);
|
|
|
|
smartlist_del_keeporder(entry_guards, i--);
|
|
|
|
log_entry_guards(LOG_INFO);
|
|
|
|
changed = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return changed ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2006-01-10 23:42:44 +01:00
|
|
|
/** Remove all entry guards that have been down or unlisted for so
|
2005-12-27 08:23:52 +01:00
|
|
|
* long that we don't think they'll come up again. Return 1 if we
|
|
|
|
* removed any, or 0 if we did nothing. */
|
|
|
|
static int
|
2007-08-29 19:22:00 +02:00
|
|
|
remove_dead_entry_guards(void)
|
2005-07-22 19:32:25 +02:00
|
|
|
{
|
|
|
|
char dbuf[HEX_DIGEST_LEN+1];
|
|
|
|
char tbuf[ISO_TIME_LEN+1];
|
|
|
|
time_t now = time(NULL);
|
|
|
|
int i;
|
2005-12-11 12:56:17 +01:00
|
|
|
int changed = 0;
|
2005-07-22 19:32:25 +02:00
|
|
|
|
2006-01-10 23:42:44 +01:00
|
|
|
for (i = 0; i < smartlist_len(entry_guards); ) {
|
|
|
|
entry_guard_t *entry = smartlist_get(entry_guards, i);
|
2006-09-29 01:57:44 +02:00
|
|
|
if (entry->bad_since &&
|
|
|
|
entry->bad_since + ENTRY_GUARD_REMOVE_AFTER < now) {
|
|
|
|
|
2005-12-28 10:07:31 +01:00
|
|
|
base16_encode(dbuf, sizeof(dbuf), entry->identity, DIGEST_LEN);
|
2006-09-29 01:57:44 +02:00
|
|
|
format_local_iso_time(tbuf, entry->bad_since);
|
|
|
|
log_info(LD_CIRC, "Entry guard '%s' (%s) has been down or unlisted "
|
2006-12-15 00:39:14 +01:00
|
|
|
"since %s local time; removing.",
|
2006-09-29 01:57:44 +02:00
|
|
|
entry->nickname, dbuf, tbuf);
|
2006-12-15 23:40:20 +01:00
|
|
|
control_event_guard(entry->nickname, entry->identity, "DROPPED");
|
2007-08-29 19:22:00 +02:00
|
|
|
entry_guard_free(entry);
|
2006-01-10 23:42:44 +01:00
|
|
|
smartlist_del_keeporder(entry_guards, i);
|
|
|
|
log_entry_guards(LOG_INFO);
|
2005-12-13 03:52:38 +01:00
|
|
|
changed = 1;
|
2005-07-22 19:32:25 +02:00
|
|
|
} else
|
|
|
|
++i;
|
|
|
|
}
|
2005-12-27 08:23:52 +01:00
|
|
|
return changed ? 1 : 0;
|
2005-07-22 19:32:25 +02:00
|
|
|
}
|
|
|
|
|
2005-12-25 00:32:35 +01:00
|
|
|
/** A new directory or router-status has arrived; update the down/listed
|
2006-01-10 23:42:44 +01:00
|
|
|
* status of the entry guards.
|
2005-07-23 06:12:35 +02:00
|
|
|
*
|
2005-12-28 10:07:31 +01:00
|
|
|
* An entry is 'down' if the directory lists it as nonrunning.
|
|
|
|
* An entry is 'unlisted' if the directory doesn't include it.
|
2007-11-26 03:18:57 +01:00
|
|
|
*
|
|
|
|
* Don't call this on startup; only on a fresh download. Otherwise we'll
|
|
|
|
* think that things are unlisted.
|
2005-07-23 06:12:35 +02:00
|
|
|
*/
|
2005-07-22 19:32:25 +02:00
|
|
|
void
|
2006-09-29 01:57:44 +02:00
|
|
|
entry_guards_compute_status(void)
|
2005-07-22 19:32:25 +02:00
|
|
|
{
|
|
|
|
time_t now;
|
|
|
|
int changed = 0;
|
2006-03-15 23:48:48 +01:00
|
|
|
int severity = LOG_INFO;
|
2006-09-29 01:57:44 +02:00
|
|
|
or_options_t *options;
|
2006-01-10 23:42:44 +01:00
|
|
|
if (! entry_guards)
|
2005-07-22 19:32:25 +02:00
|
|
|
return;
|
|
|
|
|
2006-09-29 01:57:44 +02:00
|
|
|
options = get_options();
|
2005-07-22 19:32:25 +02:00
|
|
|
|
|
|
|
now = time(NULL);
|
|
|
|
|
2006-01-10 23:42:44 +01:00
|
|
|
SMARTLIST_FOREACH(entry_guards, entry_guard_t *, entry,
|
2005-07-22 19:32:25 +02:00
|
|
|
{
|
2005-12-28 10:07:31 +01:00
|
|
|
routerinfo_t *r = router_get_by_digest(entry->identity);
|
2007-11-26 03:18:57 +01:00
|
|
|
const char *reason = NULL;
|
|
|
|
if (entry_guard_set_status(entry, r, now, options, &reason))
|
2006-09-29 01:57:44 +02:00
|
|
|
changed = 1;
|
|
|
|
|
2007-11-26 03:18:57 +01:00
|
|
|
if (entry->bad_since)
|
|
|
|
tor_assert(reason);
|
|
|
|
|
|
|
|
log_info(LD_CIRC, "Summary: Entry '%s' is %s, %s%s, and %s.",
|
2006-02-13 09:28:42 +01:00
|
|
|
entry->nickname,
|
2006-09-29 01:57:44 +02:00
|
|
|
entry->unreachable_since ? "unreachable" : "reachable",
|
2007-11-26 03:18:57 +01:00
|
|
|
entry->bad_since ? "unusable: " : "usable",
|
|
|
|
entry->bad_since ? reason : "",
|
2006-09-29 01:57:44 +02:00
|
|
|
entry_is_live(entry, 0, 1, 0) ? "live" : "not live");
|
2005-07-22 19:32:25 +02:00
|
|
|
});
|
|
|
|
|
2007-08-29 19:22:00 +02:00
|
|
|
if (remove_dead_entry_guards())
|
2005-12-27 08:23:52 +01:00
|
|
|
changed = 1;
|
|
|
|
|
2005-07-28 21:01:48 +02:00
|
|
|
if (changed) {
|
2006-01-10 23:42:44 +01:00
|
|
|
log_fn(severity, LD_CIRC, " (%d/%d entry guards are usable/new)",
|
|
|
|
num_live_entry_guards(), smartlist_len(entry_guards));
|
|
|
|
log_entry_guards(LOG_INFO);
|
|
|
|
entry_guards_changed();
|
2005-07-28 21:01:48 +02:00
|
|
|
}
|
2005-07-22 19:32:25 +02:00
|
|
|
}
|
|
|
|
|
2005-07-23 06:12:35 +02:00
|
|
|
/** Called when a connection to an OR with the identity digest <b>digest</b>
|
|
|
|
* is established (<b>succeeded</b>==1) or has failed (<b>succeeded</b>==0).
|
2005-12-28 10:07:31 +01:00
|
|
|
* If the OR is an entry, change that entry's up/down status.
|
2005-12-28 08:19:55 +01:00
|
|
|
* Return 0 normally, or -1 if we want to tear down the new connection.
|
2005-07-23 06:12:35 +02:00
|
|
|
*/
|
2005-12-28 08:19:55 +01:00
|
|
|
int
|
2006-10-07 01:37:07 +02:00
|
|
|
entry_guard_register_connect_status(const char *digest, int succeeded,
|
|
|
|
time_t now)
|
2005-07-22 19:32:25 +02:00
|
|
|
{
|
2005-12-25 00:32:35 +01:00
|
|
|
int changed = 0;
|
2005-12-28 08:19:55 +01:00
|
|
|
int refuse_conn = 0;
|
2006-09-29 01:57:44 +02:00
|
|
|
int first_contact = 0;
|
|
|
|
entry_guard_t *entry = NULL;
|
|
|
|
int idx = -1;
|
|
|
|
char buf[HEX_DIGEST_LEN+1];
|
2005-12-25 00:32:35 +01:00
|
|
|
|
2006-01-10 23:42:44 +01:00
|
|
|
if (! entry_guards)
|
2005-12-28 08:19:55 +01:00
|
|
|
return 0;
|
2005-07-22 19:32:25 +02:00
|
|
|
|
2006-09-29 01:57:44 +02:00
|
|
|
SMARTLIST_FOREACH(entry_guards, entry_guard_t *, e,
|
2005-07-22 19:32:25 +02:00
|
|
|
{
|
2006-09-29 01:57:44 +02:00
|
|
|
if (!memcmp(e->identity, digest, DIGEST_LEN)) {
|
|
|
|
entry = e;
|
|
|
|
idx = e_sl_idx;
|
|
|
|
break;
|
2005-07-22 19:32:25 +02:00
|
|
|
}
|
|
|
|
});
|
2005-12-25 00:32:35 +01:00
|
|
|
|
2006-09-29 01:57:44 +02:00
|
|
|
if (!entry)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
base16_encode(buf, sizeof(buf), entry->identity, DIGEST_LEN);
|
|
|
|
|
|
|
|
if (succeeded) {
|
|
|
|
if (entry->unreachable_since) {
|
|
|
|
log_info(LD_CIRC, "Entry guard '%s' (%s) is now reachable again. Good.",
|
|
|
|
entry->nickname, buf);
|
2007-02-23 19:34:35 +01:00
|
|
|
entry->can_retry = 0;
|
2006-09-29 01:57:44 +02:00
|
|
|
entry->unreachable_since = 0;
|
2006-10-07 01:37:07 +02:00
|
|
|
entry->last_attempted = now;
|
2006-12-15 23:40:20 +01:00
|
|
|
control_event_guard(entry->nickname, entry->identity, "UP");
|
2006-09-29 01:57:44 +02:00
|
|
|
changed = 1;
|
|
|
|
}
|
|
|
|
if (!entry->made_contact) {
|
|
|
|
entry->made_contact = 1;
|
|
|
|
first_contact = changed = 1;
|
|
|
|
}
|
|
|
|
} else { /* ! succeeded */
|
|
|
|
if (!entry->made_contact) {
|
|
|
|
/* We've never connected to this one. */
|
|
|
|
log_info(LD_CIRC,
|
|
|
|
"Connection to never-contacted entry guard '%s' (%s) failed. "
|
|
|
|
"Removing from the list. %d/%d entry guards usable/new.",
|
|
|
|
entry->nickname, buf,
|
|
|
|
num_live_entry_guards()-1, smartlist_len(entry_guards)-1);
|
2007-11-15 12:14:08 +01:00
|
|
|
entry_guard_free(entry);
|
2006-09-29 01:57:44 +02:00
|
|
|
smartlist_del_keeporder(entry_guards, idx);
|
|
|
|
log_entry_guards(LOG_INFO);
|
|
|
|
changed = 1;
|
|
|
|
} else if (!entry->unreachable_since) {
|
|
|
|
log_info(LD_CIRC, "Unable to connect to entry guard '%s' (%s). "
|
|
|
|
"Marking as unreachable.", entry->nickname, buf);
|
2006-10-07 01:37:07 +02:00
|
|
|
entry->unreachable_since = entry->last_attempted = now;
|
2006-12-15 23:40:20 +01:00
|
|
|
control_event_guard(entry->nickname, entry->identity, "DOWN");
|
2006-09-29 01:57:44 +02:00
|
|
|
changed = 1;
|
2007-11-15 12:14:08 +01:00
|
|
|
entry->can_retry = 0; /* We gave it an early chance; no good. */
|
2006-09-29 01:57:44 +02:00
|
|
|
} else {
|
|
|
|
char tbuf[ISO_TIME_LEN+1];
|
|
|
|
format_iso_time(tbuf, entry->unreachable_since);
|
|
|
|
log_debug(LD_CIRC, "Failed to connect to unreachable entry guard "
|
|
|
|
"'%s' (%s). It has been unreachable since %s.",
|
|
|
|
entry->nickname, buf, tbuf);
|
2006-10-07 01:37:07 +02:00
|
|
|
entry->last_attempted = now;
|
2007-02-24 07:44:40 +01:00
|
|
|
entry->can_retry = 0; /* We gave it an early chance; no good. */
|
2007-11-15 12:14:08 +01:00
|
|
|
}
|
2006-09-29 01:57:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (first_contact) {
|
|
|
|
/* We've just added a new long-term entry guard. Perhaps the network just
|
|
|
|
* came back? We should give our earlier entries another try too,
|
|
|
|
* and close this connection so we don't use it before we've given
|
|
|
|
* the others a shot. */
|
|
|
|
SMARTLIST_FOREACH(entry_guards, entry_guard_t *, e, {
|
|
|
|
if (e == entry)
|
|
|
|
break;
|
|
|
|
if (e->made_contact) {
|
2007-02-23 19:34:35 +01:00
|
|
|
routerinfo_t *r = entry_is_live(e, 0, 1, 1);
|
|
|
|
if (r && e->unreachable_since) {
|
2006-09-29 01:57:44 +02:00
|
|
|
refuse_conn = 1;
|
2007-02-23 19:34:35 +01:00
|
|
|
e->can_retry = 1;
|
2006-09-29 01:57:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (refuse_conn) {
|
|
|
|
log_info(LD_CIRC,
|
|
|
|
"Connected to new entry guard '%s' (%s). Marking earlier "
|
|
|
|
"entry guards up. %d/%d entry guards usable/new.",
|
|
|
|
entry->nickname, buf,
|
|
|
|
num_live_entry_guards(), smartlist_len(entry_guards));
|
|
|
|
log_entry_guards(LOG_INFO);
|
|
|
|
changed = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-12-25 00:32:35 +01:00
|
|
|
if (changed)
|
2006-01-10 23:42:44 +01:00
|
|
|
entry_guards_changed();
|
2005-12-28 08:19:55 +01:00
|
|
|
return refuse_conn ? -1 : 0;
|
2005-07-22 19:32:25 +02:00
|
|
|
}
|
|
|
|
|
2006-01-10 23:42:44 +01:00
|
|
|
/** When we try to choose an entry guard, should we parse and add
|
2005-12-31 07:32:57 +01:00
|
|
|
* config's EntryNodes first? */
|
|
|
|
static int should_add_entry_nodes = 0;
|
|
|
|
|
2006-09-29 01:57:52 +02:00
|
|
|
/** Called when the value of EntryNodes changes in our configuration. */
|
2005-12-31 07:32:57 +01:00
|
|
|
void
|
|
|
|
entry_nodes_should_be_added(void)
|
|
|
|
{
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_CIRC, "New EntryNodes config option detected. Will use.");
|
2005-12-31 07:32:57 +01:00
|
|
|
should_add_entry_nodes = 1;
|
|
|
|
}
|
|
|
|
|
2006-09-29 01:57:52 +02:00
|
|
|
/** Add all nodes in EntryNodes that aren't currently guard nodes to the list
|
|
|
|
* of guard nodes, at the front. */
|
2007-02-28 01:23:05 +01:00
|
|
|
static void
|
2006-01-10 23:42:44 +01:00
|
|
|
entry_guards_prepend_from_config(void)
|
2005-12-31 07:32:57 +01:00
|
|
|
{
|
|
|
|
or_options_t *options = get_options();
|
2007-06-07 17:07:33 +02:00
|
|
|
smartlist_t *entry_routers, *entry_fps;
|
|
|
|
smartlist_t *old_entry_guards_on_list, *old_entry_guards_not_on_list;
|
2006-01-10 23:42:44 +01:00
|
|
|
tor_assert(entry_guards);
|
2007-02-28 01:23:05 +01:00
|
|
|
|
|
|
|
should_add_entry_nodes = 0;
|
|
|
|
|
|
|
|
if (!options->EntryNodes) {
|
|
|
|
/* It's possible that a controller set EntryNodes, thus making
|
|
|
|
* should_add_entry_nodes set, then cleared it again, all before the
|
|
|
|
* call to choose_random_entry() that triggered us. If so, just return.
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
}
|
2006-01-02 12:33:14 +01:00
|
|
|
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_CIRC,"Adding configured EntryNodes '%s'.",
|
|
|
|
options->EntryNodes);
|
2005-12-31 07:32:57 +01:00
|
|
|
|
2007-06-07 17:07:33 +02:00
|
|
|
entry_routers = smartlist_create();
|
|
|
|
entry_fps = smartlist_create();
|
|
|
|
old_entry_guards_on_list = smartlist_create();
|
|
|
|
old_entry_guards_not_on_list = smartlist_create();
|
|
|
|
|
2007-02-13 02:27:55 +01:00
|
|
|
/* Split entry guards into those on the list and those not. */
|
|
|
|
add_nickname_list_to_smartlist(entry_routers, options->EntryNodes, 0);
|
|
|
|
SMARTLIST_FOREACH(entry_routers, routerinfo_t *, ri,
|
|
|
|
smartlist_add(entry_fps,ri->cache_info.identity_digest));
|
|
|
|
SMARTLIST_FOREACH(entry_guards, entry_guard_t *, e, {
|
|
|
|
if (smartlist_digest_isin(entry_fps, e->identity))
|
|
|
|
smartlist_add(old_entry_guards_on_list, e);
|
|
|
|
else
|
|
|
|
smartlist_add(old_entry_guards_not_on_list, e);
|
|
|
|
});
|
|
|
|
|
|
|
|
/* Remove all currently configured entry guards from entry_routers. */
|
|
|
|
SMARTLIST_FOREACH(entry_routers, routerinfo_t *, ri, {
|
|
|
|
if (is_an_entry_guard(ri->cache_info.identity_digest)) {
|
2007-02-24 08:37:45 +01:00
|
|
|
SMARTLIST_DEL_CURRENT(entry_routers, ri);
|
2007-02-13 02:27:55 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/* Now build the new entry_guards list. */
|
|
|
|
smartlist_clear(entry_guards);
|
|
|
|
/* First, the previously configured guards that are in EntryNodes. */
|
|
|
|
smartlist_add_all(entry_guards, old_entry_guards_on_list);
|
|
|
|
/* Next, the rest of EntryNodes */
|
|
|
|
SMARTLIST_FOREACH(entry_routers, routerinfo_t *, ri, {
|
2007-06-15 20:32:27 +02:00
|
|
|
add_an_entry_guard(ri, 0);
|
2007-02-13 02:27:55 +01:00
|
|
|
});
|
|
|
|
/* Finally, the remaining EntryNodes, unless we're strict */
|
|
|
|
if (options->StrictEntryNodes) {
|
|
|
|
SMARTLIST_FOREACH(old_entry_guards_not_on_list, entry_guard_t *, e,
|
2007-10-11 04:03:53 +02:00
|
|
|
entry_guard_free(e));
|
2007-02-13 02:27:55 +01:00
|
|
|
} else {
|
|
|
|
smartlist_add_all(entry_guards, old_entry_guards_not_on_list);
|
|
|
|
}
|
2005-12-31 07:32:57 +01:00
|
|
|
|
2007-02-13 02:27:55 +01:00
|
|
|
smartlist_free(entry_routers);
|
|
|
|
smartlist_free(entry_fps);
|
|
|
|
smartlist_free(old_entry_guards_on_list);
|
|
|
|
smartlist_free(old_entry_guards_not_on_list);
|
|
|
|
entry_guards_changed();
|
2005-12-31 07:32:57 +01:00
|
|
|
}
|
|
|
|
|
2007-06-12 11:17:23 +02:00
|
|
|
/** Return 1 if we're fine adding arbitrary routers out of the
|
|
|
|
* directory to our entry guard list. Else return 0. */
|
2007-12-10 18:16:41 +01:00
|
|
|
int
|
|
|
|
entry_list_can_grow(or_options_t *options)
|
2007-06-12 11:17:23 +02:00
|
|
|
{
|
|
|
|
if (options->StrictEntryNodes)
|
|
|
|
return 0;
|
|
|
|
if (options->UseBridges)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Pick a live (up and listed) entry guard from entry_guards. If
|
|
|
|
* <b>state</b> is non-NULL, this is for a specific circuit --
|
|
|
|
* make sure not to pick this circuit's exit or any node in the
|
|
|
|
* exit's family. If <b>state</b> is NULL, we're looking for a random
|
|
|
|
* guard (likely a bridge). */
|
|
|
|
routerinfo_t *
|
2005-12-28 10:07:31 +01:00
|
|
|
choose_random_entry(cpath_build_state_t *state)
|
2005-07-22 19:32:25 +02:00
|
|
|
{
|
2005-12-31 07:32:57 +01:00
|
|
|
or_options_t *options = get_options();
|
2006-01-10 23:42:44 +01:00
|
|
|
smartlist_t *live_entry_guards = smartlist_create();
|
2007-05-22 03:01:24 +02:00
|
|
|
smartlist_t *exit_family = smartlist_create();
|
2007-06-12 11:17:23 +02:00
|
|
|
routerinfo_t *chosen_exit = state?build_state_get_exit_router(state) : NULL;
|
2005-12-31 07:32:57 +01:00
|
|
|
routerinfo_t *r = NULL;
|
2007-06-12 11:17:23 +02:00
|
|
|
int need_uptime = state ? state->need_uptime : 0;
|
|
|
|
int need_capacity = state ? state->need_capacity : 0;
|
2007-11-16 08:37:49 +01:00
|
|
|
int consider_exit_family = 0;
|
2005-07-22 19:32:25 +02:00
|
|
|
|
2007-05-25 01:39:04 +02:00
|
|
|
if (chosen_exit) {
|
|
|
|
smartlist_add(exit_family, chosen_exit);
|
|
|
|
routerlist_add_family(exit_family, chosen_exit);
|
2007-11-16 08:37:49 +01:00
|
|
|
consider_exit_family = 1;
|
2007-05-25 01:39:04 +02:00
|
|
|
}
|
2007-05-22 03:01:24 +02:00
|
|
|
|
2006-01-10 23:42:44 +01:00
|
|
|
if (!entry_guards)
|
|
|
|
entry_guards = smartlist_create();
|
2005-12-31 07:32:57 +01:00
|
|
|
|
|
|
|
if (should_add_entry_nodes)
|
2006-01-10 23:42:44 +01:00
|
|
|
entry_guards_prepend_from_config();
|
2005-12-31 07:32:57 +01:00
|
|
|
|
2007-12-10 18:16:41 +01:00
|
|
|
if (entry_list_can_grow(options) &&
|
2006-01-10 23:42:44 +01:00
|
|
|
(! entry_guards ||
|
|
|
|
smartlist_len(entry_guards) < options->NumEntryGuards))
|
|
|
|
pick_entry_guards();
|
2005-07-22 19:32:25 +02:00
|
|
|
|
|
|
|
retry:
|
2006-01-10 23:42:44 +01:00
|
|
|
smartlist_clear(live_entry_guards);
|
|
|
|
SMARTLIST_FOREACH(entry_guards, entry_guard_t *, entry,
|
2005-12-27 08:23:52 +01:00
|
|
|
{
|
2006-09-29 01:57:44 +02:00
|
|
|
r = entry_is_live(entry, need_uptime, need_capacity, 0);
|
2007-11-16 08:37:49 +01:00
|
|
|
if (r && (!consider_exit_family || !smartlist_isin(exit_family, r))) {
|
2006-01-10 23:42:44 +01:00
|
|
|
smartlist_add(live_entry_guards, r);
|
2007-07-03 00:15:26 +02:00
|
|
|
if (!entry->made_contact) {
|
|
|
|
/* Always start with the first not-yet-contacted entry
|
|
|
|
* guard. Otherwise we might add several new ones, pick
|
|
|
|
* the second new one, and now we've expanded our entry
|
|
|
|
* guard list without needing to. */
|
|
|
|
goto choose_and_finish;
|
|
|
|
}
|
2006-01-10 23:42:44 +01:00
|
|
|
if (smartlist_len(live_entry_guards) >= options->NumEntryGuards)
|
2005-12-27 08:23:52 +01:00
|
|
|
break; /* we have enough */
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/* Try to have at least 2 choices available. This way we don't
|
2005-12-28 10:07:31 +01:00
|
|
|
* get stuck with a single live-but-crummy entry and just keep
|
2005-12-27 08:23:52 +01:00
|
|
|
* using him.
|
2006-01-10 23:42:44 +01:00
|
|
|
* (We might get 2 live-but-crummy entry guards, but so be it.) */
|
|
|
|
if (smartlist_len(live_entry_guards) < 2) {
|
2007-12-10 18:16:41 +01:00
|
|
|
if (entry_list_can_grow(options)) {
|
2005-12-31 07:32:57 +01:00
|
|
|
/* still no? try adding a new entry then */
|
2006-10-07 04:55:44 +02:00
|
|
|
/* XXX if guard doesn't imply fast and stable, then we need
|
|
|
|
* to tell add_an_entry_guard below what we want, or it might
|
|
|
|
* be a long time til we get it. -RD */
|
2007-06-15 20:32:27 +02:00
|
|
|
r = add_an_entry_guard(NULL, 0);
|
2005-12-31 07:32:57 +01:00
|
|
|
if (r) {
|
2006-01-10 23:42:44 +01:00
|
|
|
smartlist_add(live_entry_guards, r);
|
|
|
|
entry_guards_changed();
|
2005-12-31 07:32:57 +01:00
|
|
|
}
|
|
|
|
}
|
2006-10-07 04:55:44 +02:00
|
|
|
if (!r && need_uptime) {
|
|
|
|
need_uptime = 0; /* try without that requirement */
|
|
|
|
goto retry;
|
|
|
|
}
|
2005-12-31 07:32:57 +01:00
|
|
|
if (!r && need_capacity) {
|
2006-10-07 04:55:44 +02:00
|
|
|
/* still no? last attempt, try without requiring capacity */
|
|
|
|
need_capacity = 0;
|
|
|
|
goto retry;
|
2005-12-27 08:23:52 +01:00
|
|
|
}
|
2007-12-10 18:16:41 +01:00
|
|
|
if (!r && !entry_list_can_grow(options) && consider_exit_family) {
|
2007-11-16 08:37:49 +01:00
|
|
|
/* still no? if we're using bridges or have strictentrynodes
|
|
|
|
* set, and our chosen exit is in the same family as all our
|
|
|
|
* bridges/entry guards, then be flexible about families. */
|
|
|
|
consider_exit_family = 0;
|
|
|
|
goto retry;
|
|
|
|
}
|
2007-06-12 11:17:23 +02:00
|
|
|
/* live_entry_guards may be empty below. Oh well, we tried. */
|
2005-07-22 19:32:25 +02:00
|
|
|
}
|
|
|
|
|
2007-07-03 00:15:26 +02:00
|
|
|
choose_and_finish:
|
2006-01-10 23:42:44 +01:00
|
|
|
r = smartlist_choose(live_entry_guards);
|
|
|
|
smartlist_free(live_entry_guards);
|
2007-05-22 03:01:24 +02:00
|
|
|
smartlist_free(exit_family);
|
2005-07-22 19:32:25 +02:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2007-10-04 18:21:58 +02:00
|
|
|
/** Helper: Return the start of the month containing <b>time</b>. */
|
2007-08-29 19:22:00 +02:00
|
|
|
static time_t
|
|
|
|
start_of_month(time_t now)
|
|
|
|
{
|
|
|
|
struct tm tm;
|
|
|
|
tor_gmtime_r(&now, &tm);
|
|
|
|
tm.tm_sec = 0;
|
|
|
|
tm.tm_min = 0;
|
|
|
|
tm.tm_hour = 0;
|
|
|
|
tm.tm_mday = 1;
|
|
|
|
return tor_timegm(&tm);
|
|
|
|
}
|
|
|
|
|
2006-01-10 23:42:44 +01:00
|
|
|
/** Parse <b>state</b> and learn about the entry guards it describes.
|
2005-12-24 21:07:19 +01:00
|
|
|
* If <b>set</b> is true, and there are no errors, replace the global
|
2005-12-28 10:07:31 +01:00
|
|
|
* entry_list with what we find.
|
2006-03-26 08:51:26 +02:00
|
|
|
* On success, return 0. On failure, alloc into *<b>msg</b> a string
|
2005-12-24 21:07:19 +01:00
|
|
|
* describing the error, and return -1.
|
|
|
|
*/
|
2005-07-28 21:01:48 +02:00
|
|
|
int
|
2006-03-26 08:51:26 +02:00
|
|
|
entry_guards_parse_state(or_state_t *state, int set, char **msg)
|
2005-07-28 21:01:48 +02:00
|
|
|
{
|
2006-01-10 23:42:44 +01:00
|
|
|
entry_guard_t *node = NULL;
|
|
|
|
smartlist_t *new_entry_guards = smartlist_create();
|
2005-07-28 21:01:48 +02:00
|
|
|
config_line_t *line;
|
2007-08-18 20:20:42 +02:00
|
|
|
time_t now = time(NULL);
|
2007-08-29 19:22:00 +02:00
|
|
|
const char *state_version = state->TorVersion;
|
|
|
|
digestmap_t *added_by = digestmap_new();
|
2005-07-28 21:01:48 +02:00
|
|
|
|
2006-03-26 08:51:26 +02:00
|
|
|
*msg = NULL;
|
2006-01-10 23:42:44 +01:00
|
|
|
for (line = state->EntryGuards; line; line = line->next) {
|
|
|
|
if (!strcasecmp(line->key, "EntryGuard")) {
|
2005-07-28 21:01:48 +02:00
|
|
|
smartlist_t *args = smartlist_create();
|
2006-01-10 23:42:44 +01:00
|
|
|
node = tor_malloc_zero(sizeof(entry_guard_t));
|
2006-01-11 05:04:42 +01:00
|
|
|
/* all entry guards on disk have been contacted */
|
|
|
|
node->made_contact = 1;
|
2006-01-10 23:42:44 +01:00
|
|
|
smartlist_add(new_entry_guards, node);
|
2005-07-28 21:01:48 +02:00
|
|
|
smartlist_split_string(args, line->value, " ",
|
|
|
|
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
|
|
|
|
if (smartlist_len(args)<2) {
|
2006-03-26 08:51:26 +02:00
|
|
|
*msg = tor_strdup("Unable to parse entry nodes: "
|
|
|
|
"Too few arguments to EntryGuard");
|
2005-10-25 09:00:03 +02:00
|
|
|
} else if (!is_legal_nickname(smartlist_get(args,0))) {
|
2006-03-26 08:51:26 +02:00
|
|
|
*msg = tor_strdup("Unable to parse entry nodes: "
|
|
|
|
"Bad nickname for EntryGuard");
|
2005-10-25 09:00:03 +02:00
|
|
|
} else {
|
|
|
|
strlcpy(node->nickname, smartlist_get(args,0), MAX_NICKNAME_LEN+1);
|
|
|
|
if (base16_decode(node->identity, DIGEST_LEN, smartlist_get(args,1),
|
|
|
|
strlen(smartlist_get(args,1)))<0) {
|
2006-03-26 08:51:26 +02:00
|
|
|
*msg = tor_strdup("Unable to parse entry nodes: "
|
|
|
|
"Bad hex digest for EntryGuard");
|
2005-10-25 09:00:03 +02:00
|
|
|
}
|
2005-07-28 21:01:48 +02:00
|
|
|
}
|
2005-10-25 09:00:03 +02:00
|
|
|
SMARTLIST_FOREACH(args, char*, cp, tor_free(cp));
|
|
|
|
smartlist_free(args);
|
2006-03-26 08:51:26 +02:00
|
|
|
if (*msg)
|
2005-07-28 21:01:48 +02:00
|
|
|
break;
|
2007-08-29 19:22:00 +02:00
|
|
|
} else if (!strcasecmp(line->key, "EntryGuardDownSince") ||
|
|
|
|
!strcasecmp(line->key, "EntryGuardUnlistedSince")) {
|
2005-07-28 21:01:48 +02:00
|
|
|
time_t when;
|
2006-09-29 01:57:49 +02:00
|
|
|
time_t last_try = 0;
|
2005-07-28 21:01:48 +02:00
|
|
|
if (!node) {
|
2006-03-26 08:51:26 +02:00
|
|
|
*msg = tor_strdup("Unable to parse entry nodes: "
|
|
|
|
"EntryGuardDownSince/UnlistedSince without EntryGuard");
|
2005-07-28 21:01:48 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (parse_iso_time(line->value, &when)<0) {
|
2006-03-26 08:51:26 +02:00
|
|
|
*msg = tor_strdup("Unable to parse entry nodes: "
|
|
|
|
"Bad time in EntryGuardDownSince/UnlistedSince");
|
2005-07-28 21:01:48 +02:00
|
|
|
break;
|
|
|
|
}
|
2007-08-18 20:20:42 +02:00
|
|
|
if (when > now) {
|
|
|
|
/* It's a bad idea to believe info in the future: you can wind
|
|
|
|
* up with timeouts that aren't allowed to happen for years. */
|
|
|
|
continue;
|
|
|
|
}
|
2006-09-29 01:57:49 +02:00
|
|
|
if (strlen(line->value) >= ISO_TIME_LEN+ISO_TIME_LEN+1) {
|
|
|
|
/* ignore failure */
|
2008-01-14 20:00:23 +01:00
|
|
|
(void) parse_iso_time(line->value+ISO_TIME_LEN+1, &last_try);
|
2006-09-29 01:57:49 +02:00
|
|
|
}
|
|
|
|
if (!strcasecmp(line->key, "EntryGuardDownSince")) {
|
2006-09-29 01:57:44 +02:00
|
|
|
node->unreachable_since = when;
|
2006-09-29 01:57:49 +02:00
|
|
|
node->last_attempted = last_try;
|
|
|
|
} else {
|
2006-09-29 01:57:44 +02:00
|
|
|
node->bad_since = when;
|
2006-09-29 01:57:49 +02:00
|
|
|
}
|
2007-08-29 19:22:00 +02:00
|
|
|
} else if (!strcasecmp(line->key, "EntryGuardAddedBy")) {
|
|
|
|
char d[DIGEST_LEN];
|
|
|
|
/* format is digest version date */
|
|
|
|
if (strlen(line->value) < HEX_DIGEST_LEN+1+1+1+ISO_TIME_LEN) {
|
|
|
|
log_warn(LD_BUG, "EntryGuardAddedBy line is not long enough.");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (base16_decode(d, sizeof(d), line->value, HEX_DIGEST_LEN)<0 ||
|
|
|
|
line->value[HEX_DIGEST_LEN] != ' ') {
|
|
|
|
log_warn(LD_BUG, "EntryGuardAddedBy line %s does not begin with "
|
|
|
|
"hex digest", escaped(line->value));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
digestmap_set(added_by, d, tor_strdup(line->value+HEX_DIGEST_LEN+1));
|
|
|
|
} else {
|
|
|
|
log_warn(LD_BUG, "Unexpected key %s", line->key);
|
2005-07-28 21:01:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-29 19:22:00 +02:00
|
|
|
SMARTLIST_FOREACH(new_entry_guards, entry_guard_t *, e,
|
|
|
|
{
|
|
|
|
char *sp;
|
|
|
|
char *val = digestmap_get(added_by, e->identity);
|
|
|
|
if (val && (sp = strchr(val, ' '))) {
|
|
|
|
time_t when;
|
|
|
|
*sp++ = '\0';
|
|
|
|
if (parse_iso_time(sp, &when)<0) {
|
|
|
|
log_warn(LD_BUG, "Can't read time %s in EntryGuardAddedBy", sp);
|
|
|
|
} else {
|
|
|
|
e->chosen_by_version = tor_strdup(val);
|
|
|
|
e->chosen_on_date = when;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (state_version) {
|
|
|
|
e->chosen_by_version = tor_strdup(state_version);
|
|
|
|
e->chosen_on_date = start_of_month(time(NULL));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2006-03-26 08:51:26 +02:00
|
|
|
if (*msg || !set) {
|
2007-10-11 04:03:53 +02:00
|
|
|
SMARTLIST_FOREACH(new_entry_guards, entry_guard_t *, e,
|
|
|
|
entry_guard_free(e));
|
2006-01-10 23:42:44 +01:00
|
|
|
smartlist_free(new_entry_guards);
|
2005-12-27 08:23:52 +01:00
|
|
|
} else { /* !*err && set */
|
2006-01-10 23:42:44 +01:00
|
|
|
if (entry_guards) {
|
2007-10-11 04:03:53 +02:00
|
|
|
SMARTLIST_FOREACH(entry_guards, entry_guard_t *, e,
|
|
|
|
entry_guard_free(e));
|
2006-01-10 23:42:44 +01:00
|
|
|
smartlist_free(entry_guards);
|
2005-07-28 21:01:48 +02:00
|
|
|
}
|
2006-01-10 23:42:44 +01:00
|
|
|
entry_guards = new_entry_guards;
|
|
|
|
entry_guards_dirty = 0;
|
2007-08-29 19:22:00 +02:00
|
|
|
if (remove_obsolete_entry_guards())
|
|
|
|
entry_guards_dirty = 1;
|
2005-07-28 21:01:48 +02:00
|
|
|
}
|
2007-08-29 19:22:00 +02:00
|
|
|
digestmap_free(added_by, _tor_free);
|
2006-03-26 08:51:26 +02:00
|
|
|
return *msg ? -1 : 0;
|
2005-07-28 21:01:48 +02:00
|
|
|
}
|
|
|
|
|
2006-01-10 23:42:44 +01:00
|
|
|
/** Our list of entry guards has changed, or some element of one
|
2006-12-07 21:11:30 +01:00
|
|
|
* of our entry guards has changed. Write the changes to disk within
|
2006-12-09 20:40:16 +01:00
|
|
|
* the next few minutes.
|
2006-12-07 21:11:30 +01:00
|
|
|
*/
|
2005-07-28 21:01:48 +02:00
|
|
|
static void
|
2006-01-10 23:42:44 +01:00
|
|
|
entry_guards_changed(void)
|
2005-07-28 21:01:48 +02:00
|
|
|
{
|
2006-12-24 03:45:46 +01:00
|
|
|
time_t when;
|
2006-01-10 23:42:44 +01:00
|
|
|
entry_guards_dirty = 1;
|
2005-07-28 21:01:48 +02:00
|
|
|
|
2006-12-07 21:11:30 +01:00
|
|
|
/* or_state_save() will call entry_guards_update_state(). */
|
2006-12-24 03:45:46 +01:00
|
|
|
when = get_options()->AvoidDiskWrites ? time(NULL) + 3600 : time(NULL)+600;
|
|
|
|
or_state_mark_dirty(get_or_state(), when);
|
2005-07-28 21:01:48 +02:00
|
|
|
}
|
|
|
|
|
2006-01-10 23:42:44 +01:00
|
|
|
/** If the entry guard info has not changed, do nothing and return.
|
|
|
|
* Otherwise, free the EntryGuards piece of <b>state</b> and create
|
|
|
|
* a new one out of the global entry_guards list, and then mark
|
2005-12-28 10:07:31 +01:00
|
|
|
* <b>state</b> dirty so it will get saved to disk.
|
2005-12-24 21:07:19 +01:00
|
|
|
*/
|
2005-12-24 23:48:12 +01:00
|
|
|
void
|
2006-01-10 23:42:44 +01:00
|
|
|
entry_guards_update_state(or_state_t *state)
|
2005-07-28 21:01:48 +02:00
|
|
|
{
|
|
|
|
config_line_t **next, *line;
|
2006-01-10 23:42:44 +01:00
|
|
|
if (! entry_guards_dirty)
|
2005-12-24 23:48:12 +01:00
|
|
|
return;
|
2005-07-28 21:01:48 +02:00
|
|
|
|
2006-01-10 23:42:44 +01:00
|
|
|
config_free_lines(state->EntryGuards);
|
|
|
|
next = &state->EntryGuards;
|
2005-07-28 21:01:48 +02:00
|
|
|
*next = NULL;
|
2006-01-10 23:42:44 +01:00
|
|
|
if (!entry_guards)
|
|
|
|
entry_guards = smartlist_create();
|
|
|
|
SMARTLIST_FOREACH(entry_guards, entry_guard_t *, e,
|
2005-07-28 21:01:48 +02:00
|
|
|
{
|
|
|
|
char dbuf[HEX_DIGEST_LEN+1];
|
2005-12-28 10:07:31 +01:00
|
|
|
if (!e->made_contact)
|
2005-12-25 00:32:35 +01:00
|
|
|
continue; /* don't write this one to disk */
|
2005-07-28 21:01:48 +02:00
|
|
|
*next = line = tor_malloc_zero(sizeof(config_line_t));
|
2006-01-10 23:42:44 +01:00
|
|
|
line->key = tor_strdup("EntryGuard");
|
2005-07-28 21:01:48 +02:00
|
|
|
line->value = tor_malloc(HEX_DIGEST_LEN+MAX_NICKNAME_LEN+2);
|
2005-12-28 10:07:31 +01:00
|
|
|
base16_encode(dbuf, sizeof(dbuf), e->identity, DIGEST_LEN);
|
2005-07-28 21:01:48 +02:00
|
|
|
tor_snprintf(line->value,HEX_DIGEST_LEN+MAX_NICKNAME_LEN+2,
|
2005-12-28 10:07:31 +01:00
|
|
|
"%s %s", e->nickname, dbuf);
|
2005-07-28 21:01:48 +02:00
|
|
|
next = &(line->next);
|
2006-09-29 01:57:44 +02:00
|
|
|
if (e->unreachable_since) {
|
2005-07-28 21:01:48 +02:00
|
|
|
*next = line = tor_malloc_zero(sizeof(config_line_t));
|
2006-01-10 23:42:44 +01:00
|
|
|
line->key = tor_strdup("EntryGuardDownSince");
|
2006-09-29 01:57:49 +02:00
|
|
|
line->value = tor_malloc(ISO_TIME_LEN+1+ISO_TIME_LEN+1);
|
2006-09-29 01:57:44 +02:00
|
|
|
format_iso_time(line->value, e->unreachable_since);
|
2006-09-29 01:57:49 +02:00
|
|
|
if (e->last_attempted) {
|
|
|
|
line->value[ISO_TIME_LEN] = ' ';
|
|
|
|
format_iso_time(line->value+ISO_TIME_LEN+1, e->last_attempted);
|
|
|
|
}
|
2005-07-28 21:01:48 +02:00
|
|
|
next = &(line->next);
|
|
|
|
}
|
2006-09-29 01:57:44 +02:00
|
|
|
if (e->bad_since) {
|
2005-07-28 21:01:48 +02:00
|
|
|
*next = line = tor_malloc_zero(sizeof(config_line_t));
|
2006-01-10 23:42:44 +01:00
|
|
|
line->key = tor_strdup("EntryGuardUnlistedSince");
|
2005-07-28 21:01:48 +02:00
|
|
|
line->value = tor_malloc(ISO_TIME_LEN+1);
|
2006-09-29 01:57:44 +02:00
|
|
|
format_iso_time(line->value, e->bad_since);
|
2005-07-28 21:01:48 +02:00
|
|
|
next = &(line->next);
|
|
|
|
}
|
2007-08-29 19:22:00 +02:00
|
|
|
if (e->chosen_on_date && e->chosen_by_version &&
|
|
|
|
!strchr(e->chosen_by_version, ' ')) {
|
|
|
|
char d[HEX_DIGEST_LEN+1];
|
|
|
|
char t[ISO_TIME_LEN+1];
|
|
|
|
size_t val_len;
|
|
|
|
*next = line = tor_malloc_zero(sizeof(config_line_t));
|
|
|
|
line->key = tor_strdup("EntryGuardAddedBy");
|
|
|
|
val_len = (HEX_DIGEST_LEN+1+strlen(e->chosen_by_version)
|
|
|
|
+1+ISO_TIME_LEN+1);
|
|
|
|
line->value = tor_malloc(val_len);
|
|
|
|
base16_encode(d, sizeof(d), e->identity, DIGEST_LEN);
|
|
|
|
format_iso_time(t, e->chosen_on_date);
|
|
|
|
tor_snprintf(line->value, val_len, "%s %s %s",
|
|
|
|
d, e->chosen_by_version, t);
|
|
|
|
next = &(line->next);
|
|
|
|
}
|
2005-07-28 21:01:48 +02:00
|
|
|
});
|
2006-12-24 03:45:46 +01:00
|
|
|
if (!get_options()->AvoidDiskWrites)
|
|
|
|
or_state_mark_dirty(get_or_state(), 0);
|
2006-01-10 23:42:44 +01:00
|
|
|
entry_guards_dirty = 0;
|
2005-07-28 21:01:48 +02:00
|
|
|
}
|
2005-08-04 21:56:41 +02:00
|
|
|
|
2006-01-10 23:42:44 +01:00
|
|
|
/** If <b>question</b> is the string "entry-guards", then dump
|
2005-12-25 00:32:35 +01:00
|
|
|
* to *<b>answer</b> a newly allocated string describing all of
|
2006-01-10 23:42:44 +01:00
|
|
|
* the nodes in the global entry_guards list. See control-spec.txt
|
2005-12-28 10:07:31 +01:00
|
|
|
* for details.
|
|
|
|
* For backward compatibility, we also handle the string "helper-nodes".
|
|
|
|
* */
|
2005-08-04 21:56:41 +02:00
|
|
|
int
|
2006-12-08 05:39:13 +01:00
|
|
|
getinfo_helper_entry_guards(control_connection_t *conn,
|
|
|
|
const char *question, char **answer)
|
2005-08-04 21:56:41 +02:00
|
|
|
{
|
2006-12-08 05:39:13 +01:00
|
|
|
int use_long_names = conn->use_long_names;
|
|
|
|
|
2006-01-10 23:42:44 +01:00
|
|
|
if (!strcmp(question,"entry-guards") ||
|
2005-12-28 10:07:31 +01:00
|
|
|
!strcmp(question,"helper-nodes")) {
|
2005-08-04 21:56:41 +02:00
|
|
|
smartlist_t *sl = smartlist_create();
|
|
|
|
char tbuf[ISO_TIME_LEN+1];
|
2006-10-03 21:00:18 +02:00
|
|
|
char nbuf[MAX_VERBOSE_NICKNAME_LEN+1];
|
2006-01-10 23:42:44 +01:00
|
|
|
if (!entry_guards)
|
|
|
|
entry_guards = smartlist_create();
|
|
|
|
SMARTLIST_FOREACH(entry_guards, entry_guard_t *, e,
|
2005-08-04 21:56:41 +02:00
|
|
|
{
|
2006-10-03 21:00:18 +02:00
|
|
|
size_t len = MAX_VERBOSE_NICKNAME_LEN+ISO_TIME_LEN+32;
|
2005-08-04 21:56:41 +02:00
|
|
|
char *c = tor_malloc(len);
|
|
|
|
const char *status = NULL;
|
|
|
|
time_t when = 0;
|
2005-12-28 10:07:31 +01:00
|
|
|
if (!e->made_contact) {
|
2005-12-25 00:32:35 +01:00
|
|
|
status = "never-connected";
|
2006-09-29 01:57:44 +02:00
|
|
|
} else if (e->bad_since) {
|
|
|
|
when = e->bad_since;
|
|
|
|
status = "unusable";
|
2005-08-04 21:56:41 +02:00
|
|
|
} else {
|
|
|
|
status = "up";
|
|
|
|
}
|
2006-10-03 21:00:18 +02:00
|
|
|
if (use_long_names) {
|
|
|
|
routerinfo_t *ri = router_get_by_digest(e->identity);
|
|
|
|
if (ri) {
|
|
|
|
router_get_verbose_nickname(nbuf, ri);
|
|
|
|
} else {
|
|
|
|
nbuf[0] = '$';
|
|
|
|
base16_encode(nbuf+1, sizeof(nbuf)-1, e->identity, DIGEST_LEN);
|
|
|
|
/* e->nickname field is not very reliable if we don't know about
|
|
|
|
* this router any longer; don't include it. */
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
base16_encode(nbuf, sizeof(nbuf), e->identity, DIGEST_LEN);
|
|
|
|
}
|
2005-08-04 21:56:41 +02:00
|
|
|
if (when) {
|
|
|
|
format_iso_time(tbuf, when);
|
2006-10-09 23:26:34 +02:00
|
|
|
tor_snprintf(c, len, "%s %s %s\n", nbuf, status, tbuf);
|
2005-08-04 21:56:41 +02:00
|
|
|
} else {
|
2006-10-09 23:26:34 +02:00
|
|
|
tor_snprintf(c, len, "%s %s\n", nbuf, status);
|
2005-08-04 21:56:41 +02:00
|
|
|
}
|
|
|
|
smartlist_add(sl, c);
|
|
|
|
});
|
|
|
|
*answer = smartlist_join_strings(sl, "", 0, NULL);
|
|
|
|
SMARTLIST_FOREACH(sl, char *, c, tor_free(c));
|
|
|
|
smartlist_free(sl);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2005-08-08 23:58:48 +02:00
|
|
|
|
2007-08-29 19:22:00 +02:00
|
|
|
/** DOCDOC */
|
2007-06-10 09:34:21 +02:00
|
|
|
typedef struct {
|
|
|
|
uint32_t addr;
|
|
|
|
uint16_t port;
|
|
|
|
char identity[DIGEST_LEN];
|
2007-07-18 12:06:03 +02:00
|
|
|
download_status_t fetch_status;
|
2007-06-10 09:34:21 +02:00
|
|
|
} bridge_info_t;
|
|
|
|
|
|
|
|
/** A list of known bridges. */
|
|
|
|
static smartlist_t *bridge_list = NULL;
|
|
|
|
|
|
|
|
/** Initialize the bridge list to empty, creating it if needed. */
|
|
|
|
void
|
|
|
|
clear_bridge_list(void)
|
|
|
|
{
|
|
|
|
if (!bridge_list)
|
|
|
|
bridge_list = smartlist_create();
|
|
|
|
SMARTLIST_FOREACH(bridge_list, bridge_info_t *, b, tor_free(b));
|
|
|
|
smartlist_clear(bridge_list);
|
|
|
|
}
|
|
|
|
|
2007-07-18 12:06:03 +02:00
|
|
|
/** Return a bridge pointer if <b>ri</b> is one of our known bridges
|
|
|
|
* (either by comparing keys if possible, else by comparing addr/port).
|
|
|
|
* Else return NULL. */
|
|
|
|
static bridge_info_t *
|
|
|
|
routerinfo_get_configured_bridge(routerinfo_t *ri)
|
2007-06-15 08:01:04 +02:00
|
|
|
{
|
2007-12-21 23:58:22 +01:00
|
|
|
if (!bridge_list)
|
|
|
|
return NULL;
|
2007-06-15 08:01:04 +02:00
|
|
|
SMARTLIST_FOREACH(bridge_list, bridge_info_t *, bridge,
|
|
|
|
{
|
|
|
|
if (tor_digest_is_zero(bridge->identity) &&
|
|
|
|
bridge->addr == ri->addr && bridge->port == ri->or_port)
|
2007-07-18 12:06:03 +02:00
|
|
|
return bridge;
|
2007-06-15 08:01:04 +02:00
|
|
|
if (!memcmp(bridge->identity, ri->cache_info.identity_digest,
|
|
|
|
DIGEST_LEN))
|
2007-07-18 12:06:03 +02:00
|
|
|
return bridge;
|
2007-06-15 08:01:04 +02:00
|
|
|
});
|
2007-07-18 12:06:03 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-07-22 00:04:18 +02:00
|
|
|
/** Return 1 if <b>ri</b> is one of our known bridges, else 0. */
|
2007-07-18 12:06:03 +02:00
|
|
|
int
|
|
|
|
routerinfo_is_a_configured_bridge(routerinfo_t *ri)
|
|
|
|
{
|
|
|
|
return routerinfo_get_configured_bridge(ri) ? 1 : 0;
|
2007-06-15 08:01:04 +02:00
|
|
|
}
|
2007-06-12 11:17:23 +02:00
|
|
|
|
2007-06-10 09:34:21 +02:00
|
|
|
/** Remember a new bridge at <b>addr</b>:<b>port</b>. If <b>digest</b>
|
|
|
|
* is set, it tells us the identity key too. */
|
|
|
|
void
|
|
|
|
bridge_add_from_config(uint32_t addr, uint16_t port, char *digest)
|
|
|
|
{
|
|
|
|
bridge_info_t *b = tor_malloc_zero(sizeof(bridge_info_t));
|
|
|
|
b->addr = addr;
|
|
|
|
b->port = port;
|
|
|
|
if (digest)
|
|
|
|
memcpy(b->identity, digest, DIGEST_LEN);
|
|
|
|
if (!bridge_list)
|
|
|
|
bridge_list = smartlist_create();
|
|
|
|
smartlist_add(bridge_list, b);
|
|
|
|
}
|
|
|
|
|
2007-07-18 12:06:03 +02:00
|
|
|
/** Schedule the next fetch for <b>bridge</b>, based on
|
|
|
|
* some retry schedule. */
|
|
|
|
static void
|
|
|
|
bridge_fetch_status_increment(bridge_info_t *bridge, time_t now)
|
|
|
|
{
|
|
|
|
switch (bridge->fetch_status.n_download_failures) {
|
|
|
|
case 0: bridge->fetch_status.next_attempt_at = now+60*15; break;
|
|
|
|
case 1: bridge->fetch_status.next_attempt_at = now+60*15; break;
|
|
|
|
default: bridge->fetch_status.next_attempt_at = now+60*60; break;
|
|
|
|
}
|
|
|
|
if (bridge->fetch_status.n_download_failures < 10)
|
|
|
|
bridge->fetch_status.n_download_failures++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** We just got a new descriptor for <b>bridge</b>. Reschedule the
|
|
|
|
* next fetch for a long time from <b>now</b>. */
|
|
|
|
static void
|
|
|
|
bridge_fetch_status_arrived(bridge_info_t *bridge, time_t now)
|
|
|
|
{
|
2007-12-22 07:11:49 +01:00
|
|
|
tor_assert(bridge);
|
2007-07-18 12:06:03 +02:00
|
|
|
bridge->fetch_status.next_attempt_at = now+60*60;
|
|
|
|
bridge->fetch_status.n_download_failures = 0;
|
|
|
|
}
|
|
|
|
|
2007-11-04 01:15:42 +01:00
|
|
|
/** If <b>digest</b> is one of our known bridges, return it. */
|
|
|
|
static bridge_info_t *
|
|
|
|
find_bridge_by_digest(char *digest)
|
|
|
|
{
|
|
|
|
SMARTLIST_FOREACH(bridge_list, bridge_info_t *, bridge,
|
|
|
|
{
|
|
|
|
if (!memcmp(bridge->identity, digest, DIGEST_LEN))
|
|
|
|
return bridge;
|
|
|
|
});
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** We need to ask <b>bridge</b> for its server descriptor. <b>address</b>
|
|
|
|
* is a helpful string describing this bridge. */
|
|
|
|
static void
|
|
|
|
launch_direct_bridge_descriptor_fetch(char *address, bridge_info_t *bridge)
|
|
|
|
{
|
|
|
|
if (connection_get_by_type_addr_port_purpose(
|
|
|
|
CONN_TYPE_DIR, bridge->addr, bridge->port,
|
|
|
|
DIR_PURPOSE_FETCH_SERVERDESC))
|
|
|
|
return; /* it's already on the way */
|
|
|
|
directory_initiate_command(address, bridge->addr,
|
|
|
|
bridge->port, 0,
|
|
|
|
1, bridge->identity,
|
|
|
|
DIR_PURPOSE_FETCH_SERVERDESC,
|
|
|
|
ROUTER_PURPOSE_BRIDGE,
|
|
|
|
0, "authority.z", NULL, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Fetching the bridge descriptor from the bridge authority returned a
|
|
|
|
* "not found". Fall back to trying a direct fetch. */
|
|
|
|
void
|
|
|
|
retry_bridge_descriptor_fetch_directly(char *digest)
|
|
|
|
{
|
|
|
|
bridge_info_t *bridge = find_bridge_by_digest(digest);
|
|
|
|
char address_buf[INET_NTOA_BUF_LEN+1];
|
|
|
|
struct in_addr in;
|
|
|
|
|
|
|
|
if (!bridge)
|
|
|
|
return; /* not found? oh well. */
|
|
|
|
|
|
|
|
in.s_addr = htonl(bridge->addr);
|
|
|
|
tor_inet_ntoa(&in, address_buf, sizeof(address_buf));
|
|
|
|
launch_direct_bridge_descriptor_fetch(address_buf, bridge);
|
|
|
|
}
|
|
|
|
|
2007-06-10 09:34:21 +02:00
|
|
|
/** For each bridge in our list for which we don't currently have a
|
|
|
|
* descriptor, fetch a new copy of its descriptor -- either directly
|
|
|
|
* from the bridge or via a bridge authority. */
|
|
|
|
void
|
2007-07-18 12:06:03 +02:00
|
|
|
fetch_bridge_descriptors(time_t now)
|
2007-06-10 09:34:21 +02:00
|
|
|
{
|
|
|
|
char address_buf[INET_NTOA_BUF_LEN+1];
|
|
|
|
struct in_addr in;
|
|
|
|
or_options_t *options = get_options();
|
|
|
|
int num_bridge_auths = get_n_authorities(BRIDGE_AUTHORITY);
|
2007-07-18 09:13:15 +02:00
|
|
|
int ask_bridge_directly;
|
2007-11-16 08:31:51 +01:00
|
|
|
int can_use_bridge_authority;
|
2007-06-10 09:34:21 +02:00
|
|
|
|
|
|
|
if (!bridge_list)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH(bridge_list, bridge_info_t *, bridge,
|
|
|
|
{
|
2007-07-22 02:16:48 +02:00
|
|
|
if (bridge->fetch_status.next_attempt_at > now)
|
2007-07-18 12:06:03 +02:00
|
|
|
continue; /* don't bother, no need to retry yet */
|
|
|
|
|
2007-07-22 02:16:48 +02:00
|
|
|
/* schedule another fetch as if this one will fail, in case it does */
|
2007-07-18 12:06:03 +02:00
|
|
|
bridge_fetch_status_increment(bridge, now);
|
|
|
|
|
2007-06-10 09:34:21 +02:00
|
|
|
in.s_addr = htonl(bridge->addr);
|
|
|
|
tor_inet_ntoa(&in, address_buf, sizeof(address_buf));
|
|
|
|
|
2007-11-16 08:31:51 +01:00
|
|
|
can_use_bridge_authority = !tor_digest_is_zero(bridge->identity) &&
|
|
|
|
num_bridge_auths;
|
|
|
|
ask_bridge_directly = !can_use_bridge_authority ||
|
|
|
|
!options->UpdateBridgesFromAuthority;
|
2007-09-21 08:14:36 +02:00
|
|
|
log_debug(LD_DIR, "ask_bridge_directly=%d (%d, %d, %d)",
|
|
|
|
ask_bridge_directly, tor_digest_is_zero(bridge->identity),
|
|
|
|
!options->UpdateBridgesFromAuthority, !num_bridge_auths);
|
2007-07-18 09:13:15 +02:00
|
|
|
|
|
|
|
if (ask_bridge_directly &&
|
|
|
|
!fascist_firewall_allows_address_or(bridge->addr, bridge->port)) {
|
|
|
|
log_notice(LD_DIR, "Bridge at '%s:%d' isn't reachable by our "
|
|
|
|
"firewall policy. %s.", address_buf, bridge->port,
|
2007-11-16 08:31:51 +01:00
|
|
|
can_use_bridge_authority ?
|
|
|
|
"Asking bridge authority instead" : "Skipping");
|
2007-11-16 08:33:57 +01:00
|
|
|
if (can_use_bridge_authority)
|
2007-11-04 01:15:42 +01:00
|
|
|
ask_bridge_directly = 0;
|
|
|
|
else
|
|
|
|
continue;
|
2007-07-18 09:13:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ask_bridge_directly) {
|
2007-11-04 01:15:42 +01:00
|
|
|
/* we need to ask the bridge itself for its descriptor. */
|
|
|
|
launch_direct_bridge_descriptor_fetch(address_buf, bridge);
|
2007-06-10 09:34:21 +02:00
|
|
|
} else {
|
2007-07-22 02:16:48 +02:00
|
|
|
/* We have a digest and we want to ask an authority. We could
|
|
|
|
* combine all the requests into one, but that may give more
|
|
|
|
* hints to the bridge authority than we want to give. */
|
|
|
|
char resource[10 + HEX_DIGEST_LEN];
|
|
|
|
memcpy(resource, "fp/", 3);
|
|
|
|
base16_encode(resource+3, HEX_DIGEST_LEN+1,
|
|
|
|
bridge->identity, DIGEST_LEN);
|
|
|
|
memcpy(resource+3+HEX_DIGEST_LEN, ".z", 3);
|
|
|
|
log_info(LD_DIR, "Fetching bridge info '%s' from bridge authority.",
|
|
|
|
resource);
|
|
|
|
directory_get_from_dirserver(DIR_PURPOSE_FETCH_SERVERDESC,
|
2007-09-21 08:14:36 +02:00
|
|
|
ROUTER_PURPOSE_BRIDGE, resource, 0);
|
2007-06-10 09:34:21 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2007-06-12 11:17:23 +02:00
|
|
|
/** We just learned a descriptor for a bridge. See if that
|
|
|
|
* digest is in our entry guard list, and add it if not. */
|
|
|
|
void
|
2007-12-24 11:31:39 +01:00
|
|
|
learned_bridge_descriptor(routerinfo_t *ri, int from_cache)
|
2007-06-12 11:17:23 +02:00
|
|
|
{
|
|
|
|
tor_assert(ri);
|
|
|
|
tor_assert(ri->purpose == ROUTER_PURPOSE_BRIDGE);
|
|
|
|
if (get_options()->UseBridges) {
|
2007-06-15 04:12:15 +02:00
|
|
|
int first = !any_bridge_descriptors_known();
|
2007-07-18 12:06:03 +02:00
|
|
|
bridge_info_t *bridge = routerinfo_get_configured_bridge(ri);
|
|
|
|
time_t now = time(NULL);
|
2007-06-12 11:17:23 +02:00
|
|
|
ri->is_running = 1;
|
2007-07-18 12:06:03 +02:00
|
|
|
|
2007-12-22 07:11:49 +01:00
|
|
|
if (bridge) { /* if we actually want to use this one */
|
|
|
|
/* it's here; schedule its re-fetch for a long time from now. */
|
2007-12-24 11:31:39 +01:00
|
|
|
if (!from_cache)
|
|
|
|
bridge_fetch_status_arrived(bridge, now);
|
2007-07-18 12:06:03 +02:00
|
|
|
|
2007-12-22 07:11:49 +01:00
|
|
|
add_an_entry_guard(ri, 1);
|
2007-12-24 11:31:39 +01:00
|
|
|
log_notice(LD_DIR, "new bridge descriptor '%s' (%s)", ri->nickname,
|
|
|
|
from_cache ? "cached" : "fresh");
|
2007-12-22 07:11:49 +01:00
|
|
|
if (first)
|
|
|
|
routerlist_retry_directory_downloads(now);
|
|
|
|
}
|
2007-06-12 11:17:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return 1 if any of our entry guards have descriptors that
|
2007-07-18 12:06:03 +02:00
|
|
|
* are marked with purpose 'bridge' and are running. Else return 0.
|
2007-06-12 11:17:23 +02:00
|
|
|
*
|
|
|
|
* We use this function to decide if we're ready to start building
|
|
|
|
* circuits through our bridges, or if we need to wait until the
|
|
|
|
* directory "server/authority" requests finish. */
|
|
|
|
int
|
|
|
|
any_bridge_descriptors_known(void)
|
|
|
|
{
|
2007-07-29 06:38:21 +02:00
|
|
|
tor_assert(get_options()->UseBridges);
|
2007-06-12 11:17:23 +02:00
|
|
|
return choose_random_entry(NULL)!=NULL ? 1 : 0;
|
2007-07-18 12:06:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Return 1 if we have at least one descriptor for a bridge and
|
2007-07-29 06:38:21 +02:00
|
|
|
* all descriptors we know are down. Else return 0. If <b>act</b> is
|
|
|
|
* 1, then mark the down bridges up; else just observe and report. */
|
|
|
|
static int
|
|
|
|
bridges_retry_helper(int act)
|
2007-07-18 12:06:03 +02:00
|
|
|
{
|
2007-06-12 11:17:23 +02:00
|
|
|
routerinfo_t *ri;
|
2007-07-18 12:06:03 +02:00
|
|
|
int any_known = 0;
|
2007-07-29 06:38:21 +02:00
|
|
|
int any_running = 0;
|
2007-06-12 11:17:23 +02:00
|
|
|
if (!entry_guards)
|
|
|
|
entry_guards = smartlist_create();
|
|
|
|
SMARTLIST_FOREACH(entry_guards, entry_guard_t *, e,
|
|
|
|
{
|
|
|
|
ri = router_get_by_digest(e->identity);
|
2007-07-18 12:06:03 +02:00
|
|
|
if (ri && ri->purpose == ROUTER_PURPOSE_BRIDGE) {
|
|
|
|
any_known = 1;
|
|
|
|
if (ri->is_running)
|
2007-07-29 06:38:21 +02:00
|
|
|
any_running = 1; /* some bridge is both known and running */
|
|
|
|
else if (act) { /* mark it for retry */
|
|
|
|
ri->is_running = 1;
|
|
|
|
e->can_retry = 1;
|
|
|
|
e->bad_since = 0;
|
|
|
|
}
|
2007-07-18 12:06:03 +02:00
|
|
|
}
|
2007-06-12 11:17:23 +02:00
|
|
|
});
|
2007-07-29 06:38:21 +02:00
|
|
|
return any_known && !any_running;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Do we know any descriptors for our bridges, and are they all
|
|
|
|
* down? */
|
|
|
|
int
|
2007-11-26 07:26:17 +01:00
|
|
|
bridges_known_but_down(void)
|
2007-07-29 06:38:21 +02:00
|
|
|
{
|
|
|
|
return bridges_retry_helper(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Mark all down known bridges up. */
|
|
|
|
void
|
|
|
|
bridges_retry_all(void)
|
|
|
|
{
|
|
|
|
bridges_retry_helper(1);
|
2007-06-12 11:17:23 +02:00
|
|
|
}
|
|
|
|
|
2007-07-17 13:33:38 +02:00
|
|
|
/** Release all storage held by the list of entry guards and related
|
|
|
|
* memory structs. */
|
|
|
|
void
|
|
|
|
entry_guards_free_all(void)
|
|
|
|
{
|
|
|
|
if (entry_guards) {
|
2007-08-29 19:22:00 +02:00
|
|
|
SMARTLIST_FOREACH(entry_guards, entry_guard_t *, e,
|
|
|
|
entry_guard_free(e));
|
2007-07-17 13:33:38 +02:00
|
|
|
smartlist_free(entry_guards);
|
|
|
|
entry_guards = NULL;
|
|
|
|
}
|
|
|
|
clear_bridge_list();
|
|
|
|
smartlist_free(bridge_list);
|
|
|
|
bridge_list = NULL;
|
|
|
|
}
|
|
|
|
|