2004-11-07 02:33:06 +01:00
|
|
|
/* Copyright 2001 Matej Pfajfar.
|
2006-02-09 06:46:49 +01:00
|
|
|
* Copyright (c) 2001-2004, Roger Dingledine.
|
2007-12-12 22:09:01 +01:00
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2011-01-03 17:50:39 +01:00
|
|
|
* Copyright (c) 2007-2011, The Tor Project, Inc. */
|
2004-05-13 09:24:49 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file circuitlist.c
|
|
|
|
* \brief Manage the global circuit list.
|
|
|
|
**/
|
|
|
|
|
|
|
|
#include "or.h"
|
2010-07-22 01:21:00 +02:00
|
|
|
#include "circuitbuild.h"
|
2010-07-22 09:46:23 +02:00
|
|
|
#include "circuitlist.h"
|
2010-07-22 10:03:40 +02:00
|
|
|
#include "circuituse.h"
|
2010-07-22 10:32:52 +02:00
|
|
|
#include "connection.h"
|
2010-07-22 10:22:51 +02:00
|
|
|
#include "config.h"
|
2010-07-22 10:43:02 +02:00
|
|
|
#include "connection_edge.h"
|
2010-07-22 10:50:34 +02:00
|
|
|
#include "connection_or.h"
|
2010-07-22 11:35:09 +02:00
|
|
|
#include "control.h"
|
2010-07-23 20:18:55 +02:00
|
|
|
#include "networkstatus.h"
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
#include "nodelist.h"
|
2010-07-23 20:38:25 +02:00
|
|
|
#include "onion.h"
|
2010-07-23 21:53:11 +02:00
|
|
|
#include "relay.h"
|
2010-07-22 00:13:51 +02:00
|
|
|
#include "rendclient.h"
|
2010-07-21 17:52:54 +02:00
|
|
|
#include "rendcommon.h"
|
2010-07-23 22:57:20 +02:00
|
|
|
#include "rephist.h"
|
2010-07-21 17:08:11 +02:00
|
|
|
#include "routerlist.h"
|
2007-08-08 07:50:31 +02:00
|
|
|
#include "ht.h"
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
/********* START VARIABLES **********/
|
|
|
|
|
2005-04-06 07:33:32 +02:00
|
|
|
/** A global list of all circuits at this hop. */
|
|
|
|
circuit_t *global_circuitlist=NULL;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2005-12-03 03:12:37 +01:00
|
|
|
/** A list of all the circuits in CIRCUIT_STATE_OR_WAIT. */
|
2007-02-05 18:42:40 +01:00
|
|
|
static smartlist_t *circuits_pending_or_conns=NULL;
|
2005-12-03 03:12:37 +01:00
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
static void circuit_free(circuit_t *circ);
|
|
|
|
static void circuit_free_cpath(crypt_path_t *cpath);
|
2004-11-10 15:28:04 +01:00
|
|
|
static void circuit_free_cpath_node(crypt_path_t *victim);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2005-04-06 07:33:32 +02:00
|
|
|
/********* END VARIABLES ************/
|
|
|
|
|
2005-06-29 23:46:55 +02:00
|
|
|
/** A map from OR connection and circuit ID to circuit. (Lookup performance is
|
|
|
|
* very important here, since we need to do it every time a cell arrives.) */
|
2005-07-22 23:12:10 +02:00
|
|
|
typedef struct orconn_circid_circuit_map_t {
|
2005-11-23 05:18:45 +01:00
|
|
|
HT_ENTRY(orconn_circid_circuit_map_t) node;
|
2006-07-26 21:07:26 +02:00
|
|
|
or_connection_t *or_conn;
|
2008-07-23 17:58:30 +02:00
|
|
|
circid_t circ_id;
|
2005-04-06 07:33:32 +02:00
|
|
|
circuit_t *circuit;
|
2005-07-22 23:12:10 +02:00
|
|
|
} orconn_circid_circuit_map_t;
|
2005-04-06 07:33:32 +02:00
|
|
|
|
2005-12-14 21:40:40 +01:00
|
|
|
/** Helper for hash tables: compare the OR connection and circuit ID for a and
|
|
|
|
* b, and return less than, equal to, or greater than zero appropriately.
|
2005-06-29 23:46:55 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static INLINE int
|
2005-11-23 05:18:45 +01:00
|
|
|
_orconn_circid_entries_eq(orconn_circid_circuit_map_t *a,
|
|
|
|
orconn_circid_circuit_map_t *b)
|
|
|
|
{
|
|
|
|
return a->or_conn == b->or_conn && a->circ_id == b->circ_id;
|
|
|
|
}
|
|
|
|
|
2007-02-16 21:39:37 +01:00
|
|
|
/** Helper: return a hash based on circuit ID and the pointer value of
|
|
|
|
* or_conn in <b>a</b>. */
|
2005-11-23 05:18:45 +01:00
|
|
|
static INLINE unsigned int
|
|
|
|
_orconn_circid_entry_hash(orconn_circid_circuit_map_t *a)
|
2005-04-06 07:33:32 +02:00
|
|
|
{
|
2008-07-23 17:58:30 +02:00
|
|
|
return (((unsigned)a->circ_id)<<8) ^ (unsigned)(uintptr_t)(a->or_conn);
|
2005-11-23 05:18:45 +01:00
|
|
|
}
|
|
|
|
|
2007-02-16 21:39:37 +01:00
|
|
|
/** Map from [orconn,circid] to circuit. */
|
2005-12-14 21:40:40 +01:00
|
|
|
static HT_HEAD(orconn_circid_map, orconn_circid_circuit_map_t)
|
|
|
|
orconn_circid_circuit_map = HT_INITIALIZER();
|
2005-12-03 03:21:31 +01:00
|
|
|
HT_PROTOTYPE(orconn_circid_map, orconn_circid_circuit_map_t, node,
|
2007-06-17 20:41:03 +02:00
|
|
|
_orconn_circid_entry_hash, _orconn_circid_entries_eq)
|
2005-12-03 03:21:31 +01:00
|
|
|
HT_GENERATE(orconn_circid_map, orconn_circid_circuit_map_t, node,
|
2005-11-23 05:18:45 +01:00
|
|
|
_orconn_circid_entry_hash, _orconn_circid_entries_eq, 0.6,
|
2007-06-17 20:41:03 +02:00
|
|
|
malloc, realloc, free)
|
2005-04-07 07:09:19 +02:00
|
|
|
|
2005-07-14 10:43:19 +02:00
|
|
|
/** The most recently returned entry from circuit_get_by_circid_orconn;
|
|
|
|
* used to improve performance when many cells arrive in a row from the
|
|
|
|
* same circuit.
|
2005-06-29 23:46:55 +02:00
|
|
|
*/
|
2005-07-22 23:12:10 +02:00
|
|
|
orconn_circid_circuit_map_t *_last_circid_orconn_ent = NULL;
|
2005-04-06 07:33:32 +02:00
|
|
|
|
2007-03-26 16:08:35 +02:00
|
|
|
/** Implementation helper for circuit_set_{p,n}_circid_orconn: A circuit ID
|
|
|
|
* and/or or_connection for circ has just changed from <b>old_conn, old_id</b>
|
|
|
|
* to <b>conn, id</b>. Adjust the conn,circid map as appropriate, removing
|
2010-10-15 19:18:24 +02:00
|
|
|
* the old entry (if any) and adding a new one. */
|
2006-07-23 09:37:35 +02:00
|
|
|
static void
|
2007-04-09 22:09:28 +02:00
|
|
|
circuit_set_circid_orconn_helper(circuit_t *circ, int direction,
|
2008-07-23 17:58:30 +02:00
|
|
|
circid_t id,
|
2007-04-10 18:24:50 +02:00
|
|
|
or_connection_t *conn)
|
2005-04-06 07:33:32 +02:00
|
|
|
{
|
2005-07-22 23:12:10 +02:00
|
|
|
orconn_circid_circuit_map_t search;
|
|
|
|
orconn_circid_circuit_map_t *found;
|
2007-04-09 22:09:28 +02:00
|
|
|
or_connection_t *old_conn, **conn_ptr;
|
2008-07-23 17:58:30 +02:00
|
|
|
circid_t old_id, *circid_ptr;
|
2007-04-10 18:24:50 +02:00
|
|
|
int was_active, make_active;
|
2007-04-09 22:09:28 +02:00
|
|
|
|
|
|
|
if (direction == CELL_DIRECTION_OUT) {
|
|
|
|
conn_ptr = &circ->n_conn;
|
|
|
|
circid_ptr = &circ->n_circ_id;
|
2007-04-10 18:24:50 +02:00
|
|
|
was_active = circ->next_active_on_n_conn != NULL;
|
|
|
|
make_active = circ->n_conn_cells.n > 0;
|
2007-04-09 22:09:28 +02:00
|
|
|
} else {
|
|
|
|
or_circuit_t *c = TO_OR_CIRCUIT(circ);
|
|
|
|
conn_ptr = &c->p_conn;
|
|
|
|
circid_ptr = &c->p_circ_id;
|
2007-04-10 18:24:50 +02:00
|
|
|
was_active = c->next_active_on_p_conn != NULL;
|
|
|
|
make_active = c->p_conn_cells.n > 0;
|
2007-04-09 22:09:28 +02:00
|
|
|
}
|
|
|
|
old_conn = *conn_ptr;
|
|
|
|
old_id = *circid_ptr;
|
|
|
|
|
|
|
|
if (id == old_id && conn == old_conn)
|
|
|
|
return;
|
2005-04-06 07:33:32 +02:00
|
|
|
|
2005-04-07 07:09:19 +02:00
|
|
|
if (_last_circid_orconn_ent &&
|
|
|
|
((old_id == _last_circid_orconn_ent->circ_id &&
|
|
|
|
old_conn == _last_circid_orconn_ent->or_conn) ||
|
|
|
|
(id == _last_circid_orconn_ent->circ_id &&
|
|
|
|
conn == _last_circid_orconn_ent->or_conn))) {
|
|
|
|
_last_circid_orconn_ent = NULL;
|
|
|
|
}
|
|
|
|
|
2005-11-11 20:25:30 +01:00
|
|
|
if (old_conn) { /* we may need to remove it from the conn-circid map */
|
2006-07-26 21:07:26 +02:00
|
|
|
tor_assert(old_conn->_base.magic == OR_CONNECTION_MAGIC);
|
2005-04-06 07:33:32 +02:00
|
|
|
search.circ_id = old_id;
|
|
|
|
search.or_conn = old_conn;
|
2005-12-03 03:21:31 +01:00
|
|
|
found = HT_REMOVE(orconn_circid_map, &orconn_circid_circuit_map, &search);
|
2005-04-06 07:33:32 +02:00
|
|
|
if (found) {
|
2005-11-23 05:18:45 +01:00
|
|
|
tor_free(found);
|
2006-02-09 03:42:15 +01:00
|
|
|
--old_conn->n_circuits;
|
2005-04-06 07:33:32 +02:00
|
|
|
}
|
2007-04-10 18:24:50 +02:00
|
|
|
if (was_active && old_conn != conn)
|
2007-03-26 16:07:59 +02:00
|
|
|
make_circuit_inactive_on_conn(circ,old_conn);
|
2005-04-06 07:33:32 +02:00
|
|
|
}
|
|
|
|
|
2007-04-09 22:09:28 +02:00
|
|
|
/* Change the values only after we have possibly made the circuit inactive
|
|
|
|
* on the previous conn. */
|
|
|
|
*conn_ptr = conn;
|
|
|
|
*circid_ptr = id;
|
|
|
|
|
2005-04-06 07:33:32 +02:00
|
|
|
if (conn == NULL)
|
|
|
|
return;
|
|
|
|
|
2005-11-11 20:25:30 +01:00
|
|
|
/* now add the new one to the conn-circid map */
|
2005-04-06 07:33:32 +02:00
|
|
|
search.circ_id = id;
|
|
|
|
search.or_conn = conn;
|
2005-12-03 03:21:31 +01:00
|
|
|
found = HT_FIND(orconn_circid_map, &orconn_circid_circuit_map, &search);
|
2005-04-06 07:33:32 +02:00
|
|
|
if (found) {
|
|
|
|
found->circuit = circ;
|
|
|
|
} else {
|
2005-07-22 23:12:10 +02:00
|
|
|
found = tor_malloc_zero(sizeof(orconn_circid_circuit_map_t));
|
2005-04-06 07:33:32 +02:00
|
|
|
found->circ_id = id;
|
|
|
|
found->or_conn = conn;
|
|
|
|
found->circuit = circ;
|
2005-12-03 03:21:31 +01:00
|
|
|
HT_INSERT(orconn_circid_map, &orconn_circid_circuit_map, found);
|
2005-04-06 07:33:32 +02:00
|
|
|
}
|
2007-04-10 18:24:50 +02:00
|
|
|
if (make_active && old_conn != conn)
|
2007-03-26 16:07:59 +02:00
|
|
|
make_circuit_active_on_conn(circ,conn);
|
|
|
|
|
2005-11-26 02:43:57 +01:00
|
|
|
++conn->n_circuits;
|
2005-04-06 07:33:32 +02:00
|
|
|
}
|
|
|
|
|
2006-07-23 09:37:35 +02:00
|
|
|
/** Set the p_conn field of a circuit <b>circ</b>, along
|
|
|
|
* with the corresponding circuit ID, and add the circuit as appropriate
|
|
|
|
* to the (orconn,id)-\>circuit map. */
|
|
|
|
void
|
2008-07-23 17:58:30 +02:00
|
|
|
circuit_set_p_circid_orconn(or_circuit_t *circ, circid_t id,
|
2006-07-26 21:07:26 +02:00
|
|
|
or_connection_t *conn)
|
2006-07-23 09:37:35 +02:00
|
|
|
{
|
2007-04-09 22:09:28 +02:00
|
|
|
circuit_set_circid_orconn_helper(TO_CIRCUIT(circ), CELL_DIRECTION_IN,
|
2007-04-10 18:24:50 +02:00
|
|
|
id, conn);
|
2007-04-09 22:09:28 +02:00
|
|
|
|
|
|
|
if (conn)
|
2007-04-10 18:24:50 +02:00
|
|
|
tor_assert(bool_eq(circ->p_conn_cells.n, circ->next_active_on_p_conn));
|
2006-07-23 09:37:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Set the n_conn field of a circuit <b>circ</b>, along
|
|
|
|
* with the corresponding circuit ID, and add the circuit as appropriate
|
|
|
|
* to the (orconn,id)-\>circuit map. */
|
|
|
|
void
|
2008-07-23 17:58:30 +02:00
|
|
|
circuit_set_n_circid_orconn(circuit_t *circ, circid_t id,
|
2006-07-26 21:07:26 +02:00
|
|
|
or_connection_t *conn)
|
2006-07-23 09:37:35 +02:00
|
|
|
{
|
2007-04-10 18:24:50 +02:00
|
|
|
circuit_set_circid_orconn_helper(circ, CELL_DIRECTION_OUT, id, conn);
|
2007-04-09 22:09:28 +02:00
|
|
|
|
|
|
|
if (conn)
|
2007-04-10 18:24:50 +02:00
|
|
|
tor_assert(bool_eq(circ->n_conn_cells.n, circ->next_active_on_n_conn));
|
2006-07-23 09:37:35 +02:00
|
|
|
}
|
|
|
|
|
2005-12-03 17:32:29 +01:00
|
|
|
/** Change the state of <b>circ</b> to <b>state</b>, adding it to or removing
|
|
|
|
* it from lists as appropriate. */
|
2005-12-03 03:12:37 +01:00
|
|
|
void
|
2008-02-20 02:01:09 +01:00
|
|
|
circuit_set_state(circuit_t *circ, uint8_t state)
|
2005-12-03 03:12:37 +01:00
|
|
|
{
|
|
|
|
tor_assert(circ);
|
|
|
|
if (state == circ->state)
|
|
|
|
return;
|
2007-02-05 18:42:40 +01:00
|
|
|
if (!circuits_pending_or_conns)
|
|
|
|
circuits_pending_or_conns = smartlist_create();
|
2005-12-03 03:12:37 +01:00
|
|
|
if (circ->state == CIRCUIT_STATE_OR_WAIT) {
|
|
|
|
/* remove from waiting-circuit list. */
|
2007-02-05 18:42:40 +01:00
|
|
|
smartlist_remove(circuits_pending_or_conns, circ);
|
2005-12-03 03:12:37 +01:00
|
|
|
}
|
|
|
|
if (state == CIRCUIT_STATE_OR_WAIT) {
|
|
|
|
/* add to waiting-circuit list. */
|
|
|
|
smartlist_add(circuits_pending_or_conns, circ);
|
|
|
|
}
|
2011-07-19 19:51:43 +02:00
|
|
|
if (state == CIRCUIT_STATE_OPEN)
|
Launch sufficient circuits to satisfy pending isolated streams
Our old "do we need to launch a circuit for stream S" logic was,
more or less, that if we had a pending circuit that could handle S,
we didn't need to launch a new one.
But now that we have streams isolated from one another, we need
something stronger here: It's possible that some pending C can
handle either S1 or S2, but not both.
This patch reuses the existing isolation logic for a simple
solution: when we decide during circuit launching that some pending
C would satisfy stream S1, we "hypothetically" mark C as though S1
had been connected to it. Now if S2 is incompatible with S1, it
won't be something that can attach to C, and so we'll launch a new
stream.
When the circuit becomes OPEN for the first time (with no streams
attached to it), we reset the circuit's isolation status. I'm not
too sure about this part: I wanted some way to be sure that, if all
streams that would have used a circuit die before the circuit is
done, the circuit can still get used. But I worry that this
approach could also lead to us launching too many circuits. Careful
thought needed here.
2011-07-07 16:40:23 +02:00
|
|
|
tor_assert(!circ->n_conn_onionskin);
|
2011-07-19 19:51:43 +02:00
|
|
|
circ->state = state;
|
2005-12-03 03:12:37 +01:00
|
|
|
}
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
/** Add <b>circ</b> to the global list of circuits. This is called only from
|
|
|
|
* within circuit_new.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static void
|
|
|
|
circuit_add(circuit_t *circ)
|
|
|
|
{
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!global_circuitlist) { /* first one */
|
2004-05-13 09:24:49 +02:00
|
|
|
global_circuitlist = circ;
|
|
|
|
circ->next = NULL;
|
|
|
|
} else {
|
|
|
|
circ->next = global_circuitlist;
|
|
|
|
global_circuitlist = circ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-15 08:01:04 +02:00
|
|
|
/** Append to <b>out</b> all circuits in state OR_WAIT waiting for
|
2007-02-05 18:42:40 +01:00
|
|
|
* the given connection. */
|
|
|
|
void
|
|
|
|
circuit_get_all_pending_on_or_conn(smartlist_t *out, or_connection_t *or_conn)
|
|
|
|
{
|
|
|
|
tor_assert(out);
|
|
|
|
tor_assert(or_conn);
|
|
|
|
|
|
|
|
if (!circuits_pending_or_conns)
|
|
|
|
return;
|
|
|
|
|
2008-08-05 22:08:19 +02:00
|
|
|
SMARTLIST_FOREACH_BEGIN(circuits_pending_or_conns, circuit_t *, circ) {
|
2007-02-05 18:42:40 +01:00
|
|
|
if (circ->marked_for_close)
|
|
|
|
continue;
|
2008-07-30 15:04:32 +02:00
|
|
|
if (!circ->n_hop)
|
|
|
|
continue;
|
2007-02-05 18:42:40 +01:00
|
|
|
tor_assert(circ->state == CIRCUIT_STATE_OR_WAIT);
|
2008-07-30 15:04:32 +02:00
|
|
|
if (tor_digest_is_zero(circ->n_hop->identity_digest)) {
|
2007-06-15 08:01:04 +02:00
|
|
|
/* Look at addr/port. This is an unkeyed connection. */
|
2008-08-05 22:08:19 +02:00
|
|
|
if (!tor_addr_eq(&circ->n_hop->addr, &or_conn->_base.addr) ||
|
2008-07-30 15:04:32 +02:00
|
|
|
circ->n_hop->port != or_conn->_base.port)
|
2007-06-15 08:01:04 +02:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
/* We expected a key. See if it's the right one. */
|
2011-05-10 22:58:38 +02:00
|
|
|
if (tor_memneq(or_conn->identity_digest,
|
2008-07-30 15:04:32 +02:00
|
|
|
circ->n_hop->identity_digest, DIGEST_LEN))
|
2007-06-15 08:01:04 +02:00
|
|
|
continue;
|
2007-02-05 18:42:40 +01:00
|
|
|
}
|
2007-06-15 08:01:04 +02:00
|
|
|
smartlist_add(out, circ);
|
2008-08-05 22:08:19 +02:00
|
|
|
} SMARTLIST_FOREACH_END(circ);
|
2007-02-05 18:42:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Return the number of circuits in state OR_WAIT, waiting for the given
|
2007-02-06 03:49:07 +01:00
|
|
|
* connection. */
|
2007-02-05 18:42:40 +01:00
|
|
|
int
|
|
|
|
circuit_count_pending_on_or_conn(or_connection_t *or_conn)
|
|
|
|
{
|
|
|
|
int cnt;
|
|
|
|
smartlist_t *sl = smartlist_create();
|
|
|
|
circuit_get_all_pending_on_or_conn(sl, or_conn);
|
|
|
|
cnt = smartlist_len(sl);
|
|
|
|
smartlist_free(sl);
|
2011-05-16 03:58:46 +02:00
|
|
|
log_debug(LD_CIRC,"or_conn to %s at %s, %d pending circs",
|
|
|
|
or_conn->nickname ? or_conn->nickname : "NULL",
|
|
|
|
or_conn->_base.address,
|
|
|
|
cnt);
|
2007-02-05 18:42:40 +01:00
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
/** Detach from the global circuit list, and deallocate, all
|
|
|
|
* circuits that have been marked for close.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
circuit_close_all_marked(void)
|
2004-05-13 09:24:49 +02:00
|
|
|
{
|
|
|
|
circuit_t *tmp,*m;
|
|
|
|
|
|
|
|
while (global_circuitlist && global_circuitlist->marked_for_close) {
|
|
|
|
tmp = global_circuitlist->next;
|
|
|
|
circuit_free(global_circuitlist);
|
|
|
|
global_circuitlist = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = global_circuitlist;
|
|
|
|
while (tmp && tmp->next) {
|
|
|
|
if (tmp->next->marked_for_close) {
|
|
|
|
m = tmp->next->next;
|
|
|
|
circuit_free(tmp->next);
|
|
|
|
tmp->next = m;
|
|
|
|
/* Need to check new tmp->next; don't advance tmp. */
|
|
|
|
} else {
|
|
|
|
/* Advance tmp. */
|
|
|
|
tmp = tmp->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-15 07:30:25 +02:00
|
|
|
/** Return the head of the global linked list of circuits. */
|
2005-06-19 22:40:41 +02:00
|
|
|
circuit_t *
|
|
|
|
_circuit_get_global_list(void)
|
|
|
|
{
|
|
|
|
return global_circuitlist;
|
|
|
|
}
|
|
|
|
|
2005-04-07 23:07:19 +02:00
|
|
|
/** Function to make circ-\>state human-readable */
|
|
|
|
const char *
|
2005-09-30 03:09:52 +02:00
|
|
|
circuit_state_to_string(int state)
|
|
|
|
{
|
2005-04-08 05:07:07 +02:00
|
|
|
static char buf[64];
|
2005-04-07 23:07:19 +02:00
|
|
|
switch (state) {
|
|
|
|
case CIRCUIT_STATE_BUILDING: return "doing handshakes";
|
|
|
|
case CIRCUIT_STATE_ONIONSKIN_PENDING: return "processing the onion";
|
2005-11-19 02:56:58 +01:00
|
|
|
case CIRCUIT_STATE_OR_WAIT: return "connecting to server";
|
2005-04-07 23:07:19 +02:00
|
|
|
case CIRCUIT_STATE_OPEN: return "open";
|
|
|
|
default:
|
2007-03-04 21:11:46 +01:00
|
|
|
log_warn(LD_BUG, "Unknown circuit state %d", state);
|
2005-04-08 05:26:23 +02:00
|
|
|
tor_snprintf(buf, sizeof(buf), "unknown state [%d]", state);
|
2005-04-07 23:07:19 +02:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-30 00:34:22 +02:00
|
|
|
/** Map a circuit purpose to a string suitable to be displayed to a
|
|
|
|
* controller. */
|
|
|
|
const char *
|
|
|
|
circuit_purpose_to_controller_string(uint8_t purpose)
|
|
|
|
{
|
|
|
|
static char buf[32];
|
|
|
|
switch (purpose) {
|
|
|
|
case CIRCUIT_PURPOSE_OR:
|
|
|
|
case CIRCUIT_PURPOSE_INTRO_POINT:
|
|
|
|
case CIRCUIT_PURPOSE_REND_POINT_WAITING:
|
|
|
|
case CIRCUIT_PURPOSE_REND_ESTABLISHED:
|
|
|
|
return "SERVER"; /* A controller should never see these, actually. */
|
|
|
|
|
|
|
|
case CIRCUIT_PURPOSE_C_GENERAL:
|
|
|
|
return "GENERAL";
|
2008-11-10 00:56:29 +01:00
|
|
|
case CIRCUIT_PURPOSE_C_INTRODUCING:
|
2008-09-30 00:34:22 +02:00
|
|
|
case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
|
|
|
|
case CIRCUIT_PURPOSE_C_INTRODUCE_ACKED:
|
|
|
|
return "HS_CLIENT_INTRO";
|
|
|
|
|
|
|
|
case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
|
|
|
|
case CIRCUIT_PURPOSE_C_REND_READY:
|
|
|
|
case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED:
|
|
|
|
case CIRCUIT_PURPOSE_C_REND_JOINED:
|
|
|
|
return "HS_CLIENT_REND";
|
|
|
|
|
|
|
|
case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
|
|
|
|
case CIRCUIT_PURPOSE_S_INTRO:
|
|
|
|
return "HS_SERVICE_INTRO";
|
|
|
|
|
|
|
|
case CIRCUIT_PURPOSE_S_CONNECT_REND:
|
|
|
|
case CIRCUIT_PURPOSE_S_REND_JOINED:
|
|
|
|
return "HS_SERVICE_REND";
|
|
|
|
|
|
|
|
case CIRCUIT_PURPOSE_TESTING:
|
|
|
|
return "TESTING";
|
2010-07-15 03:06:49 +02:00
|
|
|
case CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT:
|
2010-07-24 18:19:20 +02:00
|
|
|
return "MEASURE_TIMEOUT";
|
2008-09-30 00:34:22 +02:00
|
|
|
case CIRCUIT_PURPOSE_CONTROLLER:
|
|
|
|
return "CONTROLLER";
|
|
|
|
|
|
|
|
default:
|
|
|
|
tor_snprintf(buf, sizeof(buf), "UNKNOWN_%d", (int)purpose);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-08 19:27:25 +02:00
|
|
|
/** Return a human-readable string for the circuit purpose <b>purpose</b>. */
|
|
|
|
const char *
|
|
|
|
circuit_purpose_to_string(uint8_t purpose)
|
|
|
|
{
|
|
|
|
static char buf[32];
|
|
|
|
|
|
|
|
switch (purpose)
|
|
|
|
{
|
|
|
|
case CIRCUIT_PURPOSE_OR:
|
|
|
|
return "Circuit at relay";
|
|
|
|
case CIRCUIT_PURPOSE_INTRO_POINT:
|
|
|
|
return "Acting as intro point";
|
|
|
|
case CIRCUIT_PURPOSE_REND_POINT_WAITING:
|
|
|
|
return "Acting as rendevous (pending)";
|
|
|
|
case CIRCUIT_PURPOSE_REND_ESTABLISHED:
|
|
|
|
return "Acting as rendevous (established)";
|
|
|
|
case CIRCUIT_PURPOSE_C_GENERAL:
|
|
|
|
return "General-purpose client";
|
|
|
|
case CIRCUIT_PURPOSE_C_INTRODUCING:
|
|
|
|
return "Hidden service client: Connecting to intro point";
|
|
|
|
case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
|
|
|
|
return "Hidden service client: Waiting for ack from intro point";
|
|
|
|
case CIRCUIT_PURPOSE_C_INTRODUCE_ACKED:
|
|
|
|
return "Hidden service client: Received ack from intro point";
|
|
|
|
case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
|
|
|
|
return "Hidden service client: Establishing rendezvous point";
|
|
|
|
case CIRCUIT_PURPOSE_C_REND_READY:
|
|
|
|
return "Hidden service client: Pending rendezvous point";
|
|
|
|
case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED:
|
|
|
|
return "Hidden service client: Pending rendezvous point (ack received)";
|
|
|
|
case CIRCUIT_PURPOSE_C_REND_JOINED:
|
|
|
|
return "Hidden service client: Active rendezvous point";
|
|
|
|
case CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT:
|
|
|
|
return "Measuring circuit timeout";
|
|
|
|
|
|
|
|
case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
|
|
|
|
return "Hidden service: Establishing introduction point";
|
|
|
|
case CIRCUIT_PURPOSE_S_INTRO:
|
|
|
|
return "Hidden service: Introduction point";
|
|
|
|
case CIRCUIT_PURPOSE_S_CONNECT_REND:
|
|
|
|
return "Hidden service: Connecting to rendezvous point";
|
|
|
|
case CIRCUIT_PURPOSE_S_REND_JOINED:
|
|
|
|
return "Hidden service: Active rendezvous point";
|
|
|
|
|
|
|
|
case CIRCUIT_PURPOSE_TESTING:
|
|
|
|
return "Testing circuit";
|
|
|
|
|
|
|
|
case CIRCUIT_PURPOSE_CONTROLLER:
|
|
|
|
return "Circuit made by controller";
|
|
|
|
|
|
|
|
default:
|
|
|
|
tor_snprintf(buf, sizeof(buf), "UNKNOWN_%d", (int)purpose);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-15 12:33:33 +02:00
|
|
|
/** Pick a reasonable package_window to start out for our circuits.
|
|
|
|
* Originally this was hard-coded at 1000, but now the consensus votes
|
|
|
|
* on the answer. See proposal 168. */
|
|
|
|
int32_t
|
|
|
|
circuit_initial_package_window(void)
|
|
|
|
{
|
2010-12-30 19:54:13 +01:00
|
|
|
int32_t num = networkstatus_get_param(NULL, "circwindow", CIRCWINDOW_START,
|
|
|
|
CIRCWINDOW_START_MIN,
|
|
|
|
CIRCWINDOW_START_MAX);
|
2009-09-23 04:09:33 +02:00
|
|
|
/* If the consensus tells us a negative number, we'd assert. */
|
|
|
|
if (num < 0)
|
|
|
|
num = CIRCWINDOW_START;
|
|
|
|
return num;
|
2009-09-15 12:33:33 +02:00
|
|
|
}
|
|
|
|
|
2007-01-24 02:30:07 +01:00
|
|
|
/** Initialize the common elements in a circuit_t, and add it to the global
|
|
|
|
* list. */
|
2006-07-23 09:37:35 +02:00
|
|
|
static void
|
|
|
|
init_circuit_base(circuit_t *circ)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2010-10-15 18:35:55 +02:00
|
|
|
tor_gettimeofday(&circ->timestamp_created);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2009-09-15 12:33:33 +02:00
|
|
|
circ->package_window = circuit_initial_package_window();
|
2004-05-13 09:24:49 +02:00
|
|
|
circ->deliver_window = CIRCWINDOW_START;
|
|
|
|
|
2009-12-10 17:12:42 +01:00
|
|
|
/* Initialize the cell_ewma_t structure */
|
2009-12-12 06:49:48 +01:00
|
|
|
circ->n_cell_ewma.last_adjusted_tick = cell_ewma_get_tick();
|
2009-12-10 17:12:42 +01:00
|
|
|
circ->n_cell_ewma.cell_count = 0.0;
|
2009-12-12 06:49:48 +01:00
|
|
|
circ->n_cell_ewma.heap_index = -1;
|
|
|
|
circ->n_cell_ewma.is_for_p_conn = 0;
|
2009-12-10 17:12:42 +01:00
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
circuit_add(circ);
|
2006-07-23 09:37:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Allocate space for a new circuit, initializing with <b>p_circ_id</b>
|
|
|
|
* and <b>p_conn</b>. Add it to the global circuit list.
|
|
|
|
*/
|
|
|
|
origin_circuit_t *
|
|
|
|
origin_circuit_new(void)
|
|
|
|
{
|
|
|
|
origin_circuit_t *circ;
|
2006-07-28 17:11:11 +02:00
|
|
|
/* never zero, since a global ID of 0 is treated specially by the
|
|
|
|
* controller */
|
|
|
|
static uint32_t n_circuits_allocated = 1;
|
2006-07-23 09:37:35 +02:00
|
|
|
|
|
|
|
circ = tor_malloc_zero(sizeof(origin_circuit_t));
|
|
|
|
circ->_base.magic = ORIGIN_CIRCUIT_MAGIC;
|
|
|
|
|
|
|
|
circ->next_stream_id = crypto_rand_int(1<<16);
|
2006-07-28 17:11:11 +02:00
|
|
|
circ->global_identifier = n_circuits_allocated++;
|
2008-07-23 17:58:38 +02:00
|
|
|
circ->remaining_relay_early_cells = MAX_RELAY_EARLY_CELLS_PER_CIRCUIT;
|
|
|
|
circ->remaining_relay_early_cells -= crypto_rand_int(2);
|
2006-07-23 09:37:35 +02:00
|
|
|
|
|
|
|
init_circuit_base(TO_CIRCUIT(circ));
|
|
|
|
|
2009-09-01 08:09:54 +02:00
|
|
|
circ_times.last_circ_at = approx_time();
|
|
|
|
|
2006-07-23 09:37:35 +02:00
|
|
|
return circ;
|
|
|
|
}
|
|
|
|
|
2007-02-16 21:39:37 +01:00
|
|
|
/** Allocate a new or_circuit_t, connected to <b>p_conn</b> as
|
|
|
|
* <b>p_circ_id</b>. If <b>p_conn</b> is NULL, the circuit is unattached. */
|
2006-07-23 09:37:35 +02:00
|
|
|
or_circuit_t *
|
2008-07-23 17:58:30 +02:00
|
|
|
or_circuit_new(circid_t p_circ_id, or_connection_t *p_conn)
|
2006-07-23 09:37:35 +02:00
|
|
|
{
|
|
|
|
/* CircIDs */
|
|
|
|
or_circuit_t *circ;
|
|
|
|
|
|
|
|
circ = tor_malloc_zero(sizeof(or_circuit_t));
|
|
|
|
circ->_base.magic = OR_CIRCUIT_MAGIC;
|
|
|
|
|
|
|
|
if (p_conn)
|
|
|
|
circuit_set_p_circid_orconn(circ, p_circ_id, p_conn);
|
|
|
|
|
2008-07-23 17:58:38 +02:00
|
|
|
circ->remaining_relay_early_cells = MAX_RELAY_EARLY_CELLS_PER_CIRCUIT;
|
|
|
|
|
2006-07-23 09:37:35 +02:00
|
|
|
init_circuit_base(TO_CIRCUIT(circ));
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2009-12-10 17:12:42 +01:00
|
|
|
/* Initialize the cell_ewma_t structure */
|
|
|
|
|
|
|
|
/* Initialize the cell counts to 0 */
|
|
|
|
circ->p_cell_ewma.cell_count = 0.0;
|
2009-12-12 06:49:48 +01:00
|
|
|
circ->p_cell_ewma.last_adjusted_tick = cell_ewma_get_tick();
|
|
|
|
circ->p_cell_ewma.is_for_p_conn = 1;
|
|
|
|
|
|
|
|
/* It's not in any heap yet. */
|
|
|
|
circ->p_cell_ewma.heap_index = -1;
|
2009-12-10 17:12:42 +01:00
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
return circ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Deallocate space associated with circ.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static void
|
|
|
|
circuit_free(circuit_t *circ)
|
|
|
|
{
|
2006-07-23 09:37:35 +02:00
|
|
|
void *mem;
|
2008-02-07 17:10:33 +01:00
|
|
|
size_t memlen;
|
2009-09-28 16:37:01 +02:00
|
|
|
if (!circ)
|
|
|
|
return;
|
|
|
|
|
2006-07-23 09:37:35 +02:00
|
|
|
if (CIRCUIT_IS_ORIGIN(circ)) {
|
|
|
|
origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
|
|
|
|
mem = ocirc;
|
2008-02-07 17:10:33 +01:00
|
|
|
memlen = sizeof(origin_circuit_t);
|
2006-07-23 09:37:35 +02:00
|
|
|
tor_assert(circ->magic == ORIGIN_CIRCUIT_MAGIC);
|
|
|
|
if (ocirc->build_state) {
|
|
|
|
extend_info_free(ocirc->build_state->chosen_exit);
|
|
|
|
circuit_free_cpath_node(ocirc->build_state->pending_final_cpath);
|
|
|
|
}
|
|
|
|
tor_free(ocirc->build_state);
|
|
|
|
|
|
|
|
circuit_free_cpath(ocirc->cpath);
|
2009-12-12 08:07:59 +01:00
|
|
|
|
|
|
|
crypto_free_pk_env(ocirc->intro_key);
|
|
|
|
rend_data_free(ocirc->rend_data);
|
2011-07-06 22:03:47 +02:00
|
|
|
|
|
|
|
tor_free(ocirc->dest_address);
|
2011-08-06 01:07:33 +02:00
|
|
|
if (ocirc->socks_username) {
|
|
|
|
memset(ocirc->socks_username, 0x12, ocirc->socks_username_len);
|
|
|
|
tor_free(ocirc->socks_username);
|
|
|
|
}
|
|
|
|
if (ocirc->socks_password) {
|
|
|
|
memset(ocirc->socks_password, 0x06, ocirc->socks_password_len);
|
|
|
|
tor_free(ocirc->socks_password);
|
|
|
|
}
|
2006-07-23 09:37:35 +02:00
|
|
|
} else {
|
|
|
|
or_circuit_t *ocirc = TO_OR_CIRCUIT(circ);
|
2009-07-05 19:53:25 +02:00
|
|
|
/* Remember cell statistics for this circuit before deallocating. */
|
|
|
|
if (get_options()->CellStatistics)
|
2009-08-19 15:41:12 +02:00
|
|
|
rep_hist_buffer_stats_add_circ(circ, time(NULL));
|
2006-07-23 09:37:35 +02:00
|
|
|
mem = ocirc;
|
2008-02-07 17:10:33 +01:00
|
|
|
memlen = sizeof(or_circuit_t);
|
2006-07-23 09:37:35 +02:00
|
|
|
tor_assert(circ->magic == OR_CIRCUIT_MAGIC);
|
|
|
|
|
2009-12-12 08:07:59 +01:00
|
|
|
crypto_free_cipher_env(ocirc->p_crypto);
|
|
|
|
crypto_free_digest_env(ocirc->p_digest);
|
|
|
|
crypto_free_cipher_env(ocirc->n_crypto);
|
|
|
|
crypto_free_digest_env(ocirc->n_digest);
|
2006-07-23 09:37:35 +02:00
|
|
|
|
|
|
|
if (ocirc->rend_splice) {
|
|
|
|
or_circuit_t *other = ocirc->rend_splice;
|
|
|
|
tor_assert(other->_base.magic == OR_CIRCUIT_MAGIC);
|
|
|
|
other->rend_splice = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove from map. */
|
|
|
|
circuit_set_p_circid_orconn(ocirc, 0, NULL);
|
|
|
|
|
2007-04-09 22:09:26 +02:00
|
|
|
/* Clear cell queue _after_ removing it from the map. Otherwise our
|
|
|
|
* "active" checks will be violated. */
|
|
|
|
cell_queue_clear(ô->p_conn_cells);
|
|
|
|
}
|
2007-03-26 16:07:59 +02:00
|
|
|
|
2009-12-12 08:07:59 +01:00
|
|
|
extend_info_free(circ->n_hop);
|
2008-02-06 00:20:49 +01:00
|
|
|
tor_free(circ->n_conn_onionskin);
|
|
|
|
|
2005-04-06 07:33:32 +02:00
|
|
|
/* Remove from map. */
|
2006-07-23 09:37:35 +02:00
|
|
|
circuit_set_n_circid_orconn(circ, 0, NULL);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2007-04-09 22:09:26 +02:00
|
|
|
/* Clear cell queue _after_ removing it from the map. Otherwise our
|
|
|
|
* "active" checks will be violated. */
|
|
|
|
cell_queue_clear(&circ->n_conn_cells);
|
|
|
|
|
2009-11-22 04:59:18 +01:00
|
|
|
memset(mem, 0xAA, memlen); /* poison memory */
|
2006-07-23 09:37:35 +02:00
|
|
|
tor_free(mem);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Deallocate space associated with the linked list <b>cpath</b>. */
|
2005-06-11 20:52:12 +02:00
|
|
|
static void
|
|
|
|
circuit_free_cpath(crypt_path_t *cpath)
|
|
|
|
{
|
2004-05-13 09:24:49 +02:00
|
|
|
crypt_path_t *victim, *head=cpath;
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!cpath)
|
2004-05-13 09:24:49 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* it's a doubly linked list, so we have to notice when we've
|
|
|
|
* gone through it once. */
|
2004-11-28 10:05:49 +01:00
|
|
|
while (cpath->next && cpath->next != head) {
|
2004-05-13 09:24:49 +02:00
|
|
|
victim = cpath;
|
|
|
|
cpath = victim->next;
|
|
|
|
circuit_free_cpath_node(victim);
|
|
|
|
}
|
|
|
|
|
|
|
|
circuit_free_cpath_node(cpath);
|
|
|
|
}
|
|
|
|
|
2005-02-11 02:26:47 +01:00
|
|
|
/** Release all storage held by circuits. */
|
|
|
|
void
|
|
|
|
circuit_free_all(void)
|
|
|
|
{
|
|
|
|
circuit_t *next;
|
|
|
|
while (global_circuitlist) {
|
|
|
|
next = global_circuitlist->next;
|
2006-07-23 09:37:35 +02:00
|
|
|
if (! CIRCUIT_IS_ORIGIN(global_circuitlist)) {
|
|
|
|
or_circuit_t *or_circ = TO_OR_CIRCUIT(global_circuitlist);
|
|
|
|
while (or_circ->resolving_streams) {
|
2007-05-17 00:15:14 +02:00
|
|
|
edge_connection_t *next_conn;
|
|
|
|
next_conn = or_circ->resolving_streams->next_stream;
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_free(TO_CONN(or_circ->resolving_streams));
|
2007-05-17 00:15:14 +02:00
|
|
|
or_circ->resolving_streams = next_conn;
|
2006-07-23 09:37:35 +02:00
|
|
|
}
|
2005-02-11 02:26:47 +01:00
|
|
|
}
|
|
|
|
circuit_free(global_circuitlist);
|
|
|
|
global_circuitlist = next;
|
|
|
|
}
|
2009-12-12 08:07:59 +01:00
|
|
|
|
|
|
|
smartlist_free(circuits_pending_or_conns);
|
|
|
|
circuits_pending_or_conns = NULL;
|
|
|
|
|
2005-12-08 20:46:22 +01:00
|
|
|
HT_CLEAR(orconn_circid_map, &orconn_circid_circuit_map);
|
2005-02-11 02:26:47 +01:00
|
|
|
}
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
/** Deallocate space associated with the cpath node <b>victim</b>. */
|
2004-11-10 15:28:04 +01:00
|
|
|
static void
|
2005-06-11 20:52:12 +02:00
|
|
|
circuit_free_cpath_node(crypt_path_t *victim)
|
|
|
|
{
|
2009-09-28 16:37:01 +02:00
|
|
|
if (!victim)
|
|
|
|
return;
|
|
|
|
|
2009-12-12 08:07:59 +01:00
|
|
|
crypto_free_cipher_env(victim->f_crypto);
|
|
|
|
crypto_free_cipher_env(victim->b_crypto);
|
|
|
|
crypto_free_digest_env(victim->f_digest);
|
|
|
|
crypto_free_digest_env(victim->b_digest);
|
|
|
|
crypto_dh_free(victim->dh_handshake_state);
|
|
|
|
extend_info_free(victim->extend_info);
|
2005-06-29 23:46:55 +02:00
|
|
|
|
2008-02-07 17:10:33 +01:00
|
|
|
memset(victim, 0xBB, sizeof(crypt_path_t)); /* poison memory */
|
2004-09-29 08:52:36 +02:00
|
|
|
tor_free(victim);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
2006-07-23 09:37:35 +02:00
|
|
|
/** A helper function for circuit_dump_by_conn() below. Log a bunch
|
|
|
|
* of information about circuit <b>circ</b>.
|
|
|
|
*/
|
|
|
|
static void
|
2006-07-27 07:03:57 +02:00
|
|
|
circuit_dump_details(int severity, circuit_t *circ, int conn_array_index,
|
2006-07-23 09:37:35 +02:00
|
|
|
const char *type, int this_circid, int other_circid)
|
|
|
|
{
|
|
|
|
log(severity, LD_CIRC, "Conn %d has %s circuit: circID %d (other side %d), "
|
2011-03-26 06:34:42 +01:00
|
|
|
"state %d (%s), born %ld:",
|
2006-07-27 07:03:57 +02:00
|
|
|
conn_array_index, type, this_circid, other_circid, circ->state,
|
2010-10-15 18:35:55 +02:00
|
|
|
circuit_state_to_string(circ->state),
|
2011-03-26 06:34:42 +01:00
|
|
|
(long)circ->timestamp_created.tv_sec);
|
2006-07-23 09:37:35 +02:00
|
|
|
if (CIRCUIT_IS_ORIGIN(circ)) { /* circ starts at this node */
|
|
|
|
circuit_log_path(severity, LD_CIRC, TO_ORIGIN_CIRCUIT(circ));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Log, at severity <b>severity</b>, information about each circuit
|
|
|
|
* that is connected to <b>conn</b>.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
circuit_dump_by_conn(connection_t *conn, int severity)
|
|
|
|
{
|
|
|
|
circuit_t *circ;
|
2006-07-26 21:07:26 +02:00
|
|
|
edge_connection_t *tmpconn;
|
2006-07-23 09:37:35 +02:00
|
|
|
|
|
|
|
for (circ=global_circuitlist;circ;circ = circ->next) {
|
|
|
|
circid_t n_circ_id = circ->n_circ_id, p_circ_id = 0;
|
|
|
|
if (circ->marked_for_close)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (! CIRCUIT_IS_ORIGIN(circ))
|
|
|
|
p_circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
|
|
|
|
|
2006-07-26 21:07:26 +02:00
|
|
|
if (! CIRCUIT_IS_ORIGIN(circ) && TO_OR_CIRCUIT(circ)->p_conn &&
|
|
|
|
TO_CONN(TO_OR_CIRCUIT(circ)->p_conn) == conn)
|
2006-07-27 07:03:57 +02:00
|
|
|
circuit_dump_details(severity, circ, conn->conn_array_index, "App-ward",
|
2006-07-23 09:37:35 +02:00
|
|
|
p_circ_id, n_circ_id);
|
|
|
|
if (CIRCUIT_IS_ORIGIN(circ)) {
|
|
|
|
for (tmpconn=TO_ORIGIN_CIRCUIT(circ)->p_streams; tmpconn;
|
|
|
|
tmpconn=tmpconn->next_stream) {
|
2006-07-26 21:07:26 +02:00
|
|
|
if (TO_CONN(tmpconn) == conn) {
|
2006-07-27 07:03:57 +02:00
|
|
|
circuit_dump_details(severity, circ, conn->conn_array_index,
|
|
|
|
"App-ward", p_circ_id, n_circ_id);
|
2006-07-23 09:37:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-07-26 21:07:26 +02:00
|
|
|
if (circ->n_conn && TO_CONN(circ->n_conn) == conn)
|
2006-07-27 07:03:57 +02:00
|
|
|
circuit_dump_details(severity, circ, conn->conn_array_index, "Exit-ward",
|
2006-07-23 09:37:35 +02:00
|
|
|
n_circ_id, p_circ_id);
|
|
|
|
if (! CIRCUIT_IS_ORIGIN(circ)) {
|
|
|
|
for (tmpconn=TO_OR_CIRCUIT(circ)->n_streams; tmpconn;
|
|
|
|
tmpconn=tmpconn->next_stream) {
|
2006-07-26 21:07:26 +02:00
|
|
|
if (TO_CONN(tmpconn) == conn) {
|
2006-07-27 07:03:57 +02:00
|
|
|
circuit_dump_details(severity, circ, conn->conn_array_index,
|
|
|
|
"Exit-ward", n_circ_id, p_circ_id);
|
2006-07-23 09:37:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-07-30 15:04:32 +02:00
|
|
|
if (!circ->n_conn && circ->n_hop &&
|
2008-08-05 22:08:19 +02:00
|
|
|
tor_addr_eq(&circ->n_hop->addr, &conn->addr) &&
|
2008-07-30 15:04:32 +02:00
|
|
|
circ->n_hop->port == conn->port &&
|
2006-07-26 21:07:26 +02:00
|
|
|
conn->type == CONN_TYPE_OR &&
|
2011-05-10 22:23:43 +02:00
|
|
|
tor_memeq(TO_OR_CONN(conn)->identity_digest,
|
2008-08-05 01:39:07 +02:00
|
|
|
circ->n_hop->identity_digest, DIGEST_LEN)) {
|
2006-07-27 07:03:57 +02:00
|
|
|
circuit_dump_details(severity, circ, conn->conn_array_index,
|
2006-07-23 09:37:35 +02:00
|
|
|
(circ->state == CIRCUIT_STATE_OPEN &&
|
|
|
|
!CIRCUIT_IS_ORIGIN(circ)) ?
|
|
|
|
"Endpoint" : "Pending",
|
|
|
|
n_circ_id, p_circ_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-03-17 13:38:37 +01:00
|
|
|
/** Return the circuit whose global ID is <b>id</b>, or NULL if no
|
|
|
|
* such circuit exists. */
|
2006-07-28 17:11:11 +02:00
|
|
|
origin_circuit_t *
|
2005-03-12 05:22:01 +01:00
|
|
|
circuit_get_by_global_id(uint32_t id)
|
|
|
|
{
|
|
|
|
circuit_t *circ;
|
|
|
|
for (circ=global_circuitlist;circ;circ = circ->next) {
|
2006-07-28 17:11:11 +02:00
|
|
|
if (CIRCUIT_IS_ORIGIN(circ) &&
|
|
|
|
TO_ORIGIN_CIRCUIT(circ)->global_identifier == id) {
|
2005-03-12 05:22:01 +01:00
|
|
|
if (circ->marked_for_close)
|
|
|
|
return NULL;
|
|
|
|
else
|
2006-07-28 17:11:11 +02:00
|
|
|
return TO_ORIGIN_CIRCUIT(circ);
|
2005-03-12 05:22:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
/** Return a circ such that:
|
|
|
|
* - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
|
2005-10-17 10:41:58 +02:00
|
|
|
* - circ is attached to <b>conn</b>, either as p_conn or n_conn.
|
2004-05-13 09:24:49 +02:00
|
|
|
* Return NULL if no such circuit exists.
|
|
|
|
*/
|
2005-10-24 21:37:45 +02:00
|
|
|
static INLINE circuit_t *
|
2008-07-23 17:58:30 +02:00
|
|
|
circuit_get_by_circid_orconn_impl(circid_t circ_id, or_connection_t *conn)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2005-07-22 23:12:10 +02:00
|
|
|
orconn_circid_circuit_map_t search;
|
|
|
|
orconn_circid_circuit_map_t *found;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2005-04-07 07:09:19 +02:00
|
|
|
if (_last_circid_orconn_ent &&
|
|
|
|
circ_id == _last_circid_orconn_ent->circ_id &&
|
|
|
|
conn == _last_circid_orconn_ent->or_conn) {
|
|
|
|
found = _last_circid_orconn_ent;
|
|
|
|
} else {
|
|
|
|
search.circ_id = circ_id;
|
|
|
|
search.or_conn = conn;
|
2005-12-03 03:21:31 +01:00
|
|
|
found = HT_FIND(orconn_circid_map, &orconn_circid_circuit_map, &search);
|
2005-04-07 07:09:19 +02:00
|
|
|
_last_circid_orconn_ent = found;
|
|
|
|
}
|
2005-10-24 21:37:45 +02:00
|
|
|
if (found && found->circuit)
|
2005-04-06 07:33:32 +02:00
|
|
|
return found->circuit;
|
2005-04-06 08:13:49 +02:00
|
|
|
|
2005-11-11 20:28:32 +01:00
|
|
|
return NULL;
|
|
|
|
/* The rest of this checks for bugs. Disabled by default. */
|
2011-06-08 21:23:05 +02:00
|
|
|
/* We comment it out because coverity complains otherwise.
|
2005-04-06 08:13:49 +02:00
|
|
|
{
|
|
|
|
circuit_t *circ;
|
|
|
|
for (circ=global_circuitlist;circ;circ = circ->next) {
|
2006-07-23 09:37:35 +02:00
|
|
|
if (! CIRCUIT_IS_ORIGIN(circ)) {
|
|
|
|
or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
|
|
|
|
if (or_circ->p_conn == conn && or_circ->p_circ_id == circ_id) {
|
|
|
|
log_warn(LD_BUG,
|
|
|
|
"circuit matches p_conn, but not in hash table (Bug!)");
|
|
|
|
return circ;
|
|
|
|
}
|
2005-04-06 08:13:49 +02:00
|
|
|
}
|
|
|
|
if (circ->n_conn == conn && circ->n_circ_id == circ_id) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_BUG,
|
|
|
|
"circuit matches n_conn, but not in hash table (Bug!)");
|
2005-04-06 08:13:49 +02:00
|
|
|
return circ;
|
|
|
|
}
|
|
|
|
}
|
2005-04-06 07:33:32 +02:00
|
|
|
return NULL;
|
2011-06-08 21:23:05 +02:00
|
|
|
} */
|
2005-10-24 21:37:45 +02:00
|
|
|
}
|
2005-04-06 08:13:49 +02:00
|
|
|
|
2005-10-24 21:37:45 +02:00
|
|
|
/** Return a circ such that:
|
|
|
|
* - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
|
|
|
|
* - circ is attached to <b>conn</b>, either as p_conn or n_conn.
|
|
|
|
* - circ is not marked for close.
|
|
|
|
* Return NULL if no such circuit exists.
|
|
|
|
*/
|
|
|
|
circuit_t *
|
2008-07-23 17:58:30 +02:00
|
|
|
circuit_get_by_circid_orconn(circid_t circ_id, or_connection_t *conn)
|
2005-10-24 21:37:45 +02:00
|
|
|
{
|
|
|
|
circuit_t *circ = circuit_get_by_circid_orconn_impl(circ_id, conn);
|
2005-10-25 17:31:25 +02:00
|
|
|
if (!circ || circ->marked_for_close)
|
2005-10-24 21:37:45 +02:00
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
return circ;
|
|
|
|
}
|
|
|
|
|
2008-07-23 14:55:55 +02:00
|
|
|
/** Return true iff the circuit ID <b>circ_id</b> is currently used by a
|
|
|
|
* circuit, marked or not, on <b>conn</b>. */
|
|
|
|
int
|
2008-07-23 17:58:30 +02:00
|
|
|
circuit_id_in_use_on_orconn(circid_t circ_id, or_connection_t *conn)
|
2008-07-23 14:55:55 +02:00
|
|
|
{
|
|
|
|
return circuit_get_by_circid_orconn_impl(circ_id, conn) != NULL;
|
|
|
|
}
|
|
|
|
|
2005-06-29 23:46:55 +02:00
|
|
|
/** Return the circuit that a given edge connection is using. */
|
2005-06-11 20:52:12 +02:00
|
|
|
circuit_t *
|
2006-07-26 21:07:26 +02:00
|
|
|
circuit_get_by_edge_conn(edge_connection_t *conn)
|
2005-04-06 08:13:49 +02:00
|
|
|
{
|
|
|
|
circuit_t *circ;
|
|
|
|
|
|
|
|
circ = conn->on_circuit;
|
2006-07-23 09:37:35 +02:00
|
|
|
tor_assert(!circ ||
|
|
|
|
(CIRCUIT_IS_ORIGIN(circ) ? circ->magic == ORIGIN_CIRCUIT_MAGIC
|
|
|
|
: circ->magic == OR_CIRCUIT_MAGIC));
|
2005-11-26 02:43:57 +01:00
|
|
|
|
2005-06-29 23:46:55 +02:00
|
|
|
return circ;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
2007-01-11 00:48:24 +01:00
|
|
|
/** For each circuit that has <b>conn</b> as n_conn or p_conn, unlink the
|
2005-12-05 20:45:54 +01:00
|
|
|
* circuit from the orconn,circid map, and mark it for close if it hasn't
|
|
|
|
* been marked already.
|
2004-05-13 09:24:49 +02:00
|
|
|
*/
|
2005-12-05 20:45:54 +01:00
|
|
|
void
|
2006-07-26 21:07:26 +02:00
|
|
|
circuit_unlink_all_from_or_conn(or_connection_t *conn, int reason)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2004-05-13 09:24:49 +02:00
|
|
|
circuit_t *circ;
|
2007-03-26 16:07:59 +02:00
|
|
|
|
|
|
|
connection_or_unlink_all_active_circs(conn);
|
|
|
|
|
2005-12-05 20:45:54 +01:00
|
|
|
for (circ = global_circuitlist; circ; circ = circ->next) {
|
2006-07-23 09:37:35 +02:00
|
|
|
int mark = 0;
|
|
|
|
if (circ->n_conn == conn) {
|
|
|
|
circuit_set_n_circid_orconn(circ, 0, NULL);
|
|
|
|
mark = 1;
|
|
|
|
}
|
|
|
|
if (! CIRCUIT_IS_ORIGIN(circ)) {
|
|
|
|
or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
|
|
|
|
if (or_circ->p_conn == conn) {
|
|
|
|
circuit_set_p_circid_orconn(or_circ, 0, NULL);
|
|
|
|
mark = 1;
|
|
|
|
}
|
2005-12-05 20:45:54 +01:00
|
|
|
}
|
2006-07-23 09:37:35 +02:00
|
|
|
if (mark && !circ->marked_for_close)
|
|
|
|
circuit_mark_for_close(circ, reason);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return a circ such that:
|
2011-06-25 00:12:45 +02:00
|
|
|
* - circ-\>rend_data-\>onion_address is equal to <b>rend_query</b>, and
|
2004-05-13 09:24:49 +02:00
|
|
|
* - circ-\>purpose is equal to <b>purpose</b>.
|
|
|
|
*
|
|
|
|
* Return NULL if no such circuit exists.
|
|
|
|
*/
|
2006-07-26 21:05:34 +02:00
|
|
|
origin_circuit_t *
|
2005-06-11 20:52:12 +02:00
|
|
|
circuit_get_by_rend_query_and_purpose(const char *rend_query, uint8_t purpose)
|
|
|
|
{
|
2004-05-13 09:24:49 +02:00
|
|
|
circuit_t *circ;
|
|
|
|
|
2006-07-26 21:05:34 +02:00
|
|
|
tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose));
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
for (circ = global_circuitlist; circ; circ = circ->next) {
|
|
|
|
if (!circ->marked_for_close &&
|
2008-09-24 16:44:29 +02:00
|
|
|
circ->purpose == purpose) {
|
|
|
|
origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
|
|
|
|
if (ocirc->rend_data &&
|
|
|
|
!rend_cmp_service_ids(rend_query,
|
|
|
|
ocirc->rend_data->onion_address))
|
|
|
|
return ocirc;
|
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-02-02 19:58:11 +01:00
|
|
|
/** Return the first circuit originating here in global_circuitlist after
|
2007-02-06 03:49:07 +01:00
|
|
|
* <b>start</b> whose purpose is <b>purpose</b>, and where
|
2007-02-02 19:58:11 +01:00
|
|
|
* <b>digest</b> (if set) matches the rend_pk_digest field. Return NULL if no
|
|
|
|
* circuit is found. If <b>start</b> is NULL, begin at the start of the list.
|
2004-05-13 09:24:49 +02:00
|
|
|
*/
|
2006-07-26 21:05:41 +02:00
|
|
|
origin_circuit_t *
|
|
|
|
circuit_get_next_by_pk_and_purpose(origin_circuit_t *start,
|
2004-05-13 09:24:49 +02:00
|
|
|
const char *digest, uint8_t purpose)
|
|
|
|
{
|
|
|
|
circuit_t *circ;
|
2006-07-26 21:05:41 +02:00
|
|
|
tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose));
|
2004-05-13 09:24:49 +02:00
|
|
|
if (start == NULL)
|
|
|
|
circ = global_circuitlist;
|
|
|
|
else
|
2006-07-26 21:05:41 +02:00
|
|
|
circ = TO_CIRCUIT(start)->next;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
for ( ; circ; circ = circ->next) {
|
2004-05-13 09:24:49 +02:00
|
|
|
if (circ->marked_for_close)
|
|
|
|
continue;
|
|
|
|
if (circ->purpose != purpose)
|
|
|
|
continue;
|
2006-09-15 07:53:00 +02:00
|
|
|
if (!digest)
|
|
|
|
return TO_ORIGIN_CIRCUIT(circ);
|
2008-09-24 16:44:29 +02:00
|
|
|
else if (TO_ORIGIN_CIRCUIT(circ)->rend_data &&
|
2011-05-10 22:23:43 +02:00
|
|
|
tor_memeq(TO_ORIGIN_CIRCUIT(circ)->rend_data->rend_pk_digest,
|
2006-09-15 07:53:00 +02:00
|
|
|
digest, DIGEST_LEN))
|
2006-07-26 21:05:41 +02:00
|
|
|
return TO_ORIGIN_CIRCUIT(circ);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-02-02 19:58:11 +01:00
|
|
|
/** Return the first OR circuit in the global list whose purpose is
|
|
|
|
* <b>purpose</b>, and whose rend_token is the <b>len</b>-byte
|
2007-02-06 03:49:07 +01:00
|
|
|
* <b>token</b>. */
|
2006-07-26 21:05:41 +02:00
|
|
|
static or_circuit_t *
|
|
|
|
circuit_get_by_rend_token_and_purpose(uint8_t purpose, const char *token,
|
|
|
|
size_t len)
|
2004-05-13 09:24:49 +02:00
|
|
|
{
|
|
|
|
circuit_t *circ;
|
|
|
|
for (circ = global_circuitlist; circ; circ = circ->next) {
|
|
|
|
if (! circ->marked_for_close &&
|
2006-07-26 21:05:41 +02:00
|
|
|
circ->purpose == purpose &&
|
2011-05-10 22:23:43 +02:00
|
|
|
tor_memeq(TO_OR_CIRCUIT(circ)->rend_token, token, len))
|
2006-07-23 09:37:35 +02:00
|
|
|
return TO_OR_CIRCUIT(circ);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-07-26 21:05:41 +02:00
|
|
|
/** Return the circuit waiting for a rendezvous with the provided cookie.
|
|
|
|
* Return NULL if no such circuit is found.
|
|
|
|
*/
|
|
|
|
or_circuit_t *
|
|
|
|
circuit_get_rendezvous(const char *cookie)
|
|
|
|
{
|
|
|
|
return circuit_get_by_rend_token_and_purpose(
|
|
|
|
CIRCUIT_PURPOSE_REND_POINT_WAITING,
|
|
|
|
cookie, REND_COOKIE_LEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return the circuit waiting for intro cells of the given digest.
|
|
|
|
* Return NULL if no such circuit is found.
|
|
|
|
*/
|
|
|
|
or_circuit_t *
|
|
|
|
circuit_get_intro_point(const char *digest)
|
|
|
|
{
|
|
|
|
return circuit_get_by_rend_token_and_purpose(
|
|
|
|
CIRCUIT_PURPOSE_INTRO_POINT, digest,
|
|
|
|
DIGEST_LEN);
|
|
|
|
}
|
|
|
|
|
2007-08-19 09:04:24 +02:00
|
|
|
/** Return a circuit that is open, is CIRCUIT_PURPOSE_C_GENERAL,
|
2007-12-23 20:15:22 +01:00
|
|
|
* has a timestamp_dirty value of 0, has flags matching the CIRCLAUNCH_*
|
|
|
|
* flags in <b>flags</b>, and if info is defined, does not already use info
|
2005-11-11 20:25:30 +01:00
|
|
|
* as any of its hops; or NULL if no circuit fits this description.
|
2005-01-20 00:13:20 +01:00
|
|
|
*
|
2008-12-18 18:18:14 +01:00
|
|
|
* The <b>purpose</b> argument (currently ignored) refers to the purpose of
|
|
|
|
* the circuit we want to create, not the purpose of the circuit we want to
|
|
|
|
* cannibalize.
|
|
|
|
*
|
2007-12-23 20:15:22 +01:00
|
|
|
* If !CIRCLAUNCH_NEED_UPTIME, prefer returning non-uptime circuits.
|
2004-05-13 09:24:49 +02:00
|
|
|
*/
|
2006-07-23 09:37:35 +02:00
|
|
|
origin_circuit_t *
|
2005-11-11 20:25:30 +01:00
|
|
|
circuit_find_to_cannibalize(uint8_t purpose, extend_info_t *info,
|
2007-12-23 20:15:22 +01:00
|
|
|
int flags)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2006-07-23 09:37:35 +02:00
|
|
|
circuit_t *_circ;
|
|
|
|
origin_circuit_t *best=NULL;
|
2008-03-24 19:43:01 +01:00
|
|
|
int need_uptime = (flags & CIRCLAUNCH_NEED_UPTIME) != 0;
|
|
|
|
int need_capacity = (flags & CIRCLAUNCH_NEED_CAPACITY) != 0;
|
|
|
|
int internal = (flags & CIRCLAUNCH_IS_INTERNAL) != 0;
|
2011-06-14 19:01:38 +02:00
|
|
|
const or_options_t *options = get_options();
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2010-03-01 22:26:44 +01:00
|
|
|
/* Make sure we're not trying to create a onehop circ by
|
|
|
|
* cannibalization. */
|
|
|
|
tor_assert(!(flags & CIRCLAUNCH_ONEHOP_TUNNEL));
|
|
|
|
|
2006-02-13 09:28:42 +01:00
|
|
|
log_debug(LD_CIRC,
|
|
|
|
"Hunting for a circ to cannibalize: purpose %d, uptime %d, "
|
|
|
|
"capacity %d, internal %d",
|
|
|
|
purpose, need_uptime, need_capacity, internal);
|
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
|
|
|
|
2006-07-23 09:37:35 +02:00
|
|
|
for (_circ=global_circuitlist; _circ; _circ = _circ->next) {
|
|
|
|
if (CIRCUIT_IS_ORIGIN(_circ) &&
|
|
|
|
_circ->state == CIRCUIT_STATE_OPEN &&
|
|
|
|
!_circ->marked_for_close &&
|
2007-08-19 09:04:24 +02:00
|
|
|
_circ->purpose == CIRCUIT_PURPOSE_C_GENERAL &&
|
2006-07-23 09:37:35 +02:00
|
|
|
!_circ->timestamp_dirty) {
|
|
|
|
origin_circuit_t *circ = TO_ORIGIN_CIRCUIT(_circ);
|
|
|
|
if ((!need_uptime || circ->build_state->need_uptime) &&
|
|
|
|
(!need_capacity || circ->build_state->need_capacity) &&
|
2008-12-29 20:55:13 +01:00
|
|
|
(internal == circ->build_state->is_internal) &&
|
2010-03-01 22:26:44 +01:00
|
|
|
circ->remaining_relay_early_cells &&
|
2011-07-20 20:40:47 +02:00
|
|
|
!circ->build_state->onehop_tunnel &&
|
|
|
|
!circ->isolation_values_set) {
|
2006-07-23 09:37:35 +02:00
|
|
|
if (info) {
|
|
|
|
/* need to make sure we don't duplicate hops */
|
|
|
|
crypt_path_t *hop = circ->cpath;
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
const node_t *ri1 = node_get_by_id(info->identity_digest);
|
2006-07-23 09:37:35 +02:00
|
|
|
do {
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
const node_t *ri2;
|
2011-05-10 22:23:43 +02:00
|
|
|
if (tor_memeq(hop->extend_info->identity_digest,
|
2006-07-23 09:37:35 +02:00
|
|
|
info->identity_digest, DIGEST_LEN))
|
|
|
|
goto next;
|
2007-10-11 23:40:35 +02:00
|
|
|
if (ri1 &&
|
Initial conversion to use node_t throughout our codebase.
A node_t is an abstraction over routerstatus_t, routerinfo_t, and
microdesc_t. It should try to present a consistent interface to all
of them. There should be a node_t for a server whenever there is
* A routerinfo_t for it in the routerlist
* A routerstatus_t in the current_consensus.
(note that a microdesc_t alone isn't enough to make a node_t exist,
since microdescriptors aren't usable on their own.)
There are three ways to get a node_t right now: looking it up by ID,
looking it up by nickname, and iterating over the whole list of
microdescriptors.
All (or nearly all) functions that are supposed to return "a router"
-- especially those used in building connections and circuits --
should return a node_t, not a routerinfo_t or a routerstatus_t.
A node_t should hold all the *mutable* flags about a node. This
patch moves the is_foo flags from routerinfo_t into node_t. The
flags in routerstatus_t remain, but they get set from the consensus
and should not change.
Some other highlights of this patch are:
* Looking up routerinfo and routerstatus by nickname is now
unified and based on the "look up a node by nickname" function.
This tries to look only at the values from current consensus,
and not get confused by the routerinfo_t->is_named flag, which
could get set for other weird reasons. This changes the
behavior of how authorities (when acting as clients) deal with
nodes that have been listed by nickname.
* I tried not to artificially increase the size of the diff here
by moving functions around. As a result, some functions that
now operate on nodes are now in the wrong file -- they should
get moved to nodelist.c once this refactoring settles down.
This moving should happen as part of a patch that moves
functions AND NOTHING ELSE.
* Some old code is now left around inside #if 0/1 blocks, and
should get removed once I've verified that I don't want it
sitting around to see how we used to do things.
There are still some unimplemented functions: these are flagged
with "UNIMPLEMENTED_NODELIST()." I'll work on filling in the
implementation here, piece by piece.
I wish this patch could have been smaller, but there did not seem to
be any piece of it that was independent from the rest. Moving flags
forces many functions that once returned routerinfo_t * to return
node_t *, which forces their friends to change, and so on.
2010-09-29 21:00:41 +02:00
|
|
|
(ri2 = node_get_by_id(hop->extend_info->identity_digest))
|
|
|
|
&& nodes_in_same_family(ri1, ri2))
|
2007-10-11 23:40:35 +02:00
|
|
|
goto next;
|
2006-07-23 09:37:35 +02:00
|
|
|
hop=hop->next;
|
|
|
|
} while (hop!=circ->cpath);
|
|
|
|
}
|
2011-04-04 01:58:28 +02:00
|
|
|
if (options->ExcludeNodes) {
|
|
|
|
/* Make sure no existing nodes in the circuit are excluded for
|
|
|
|
* general use. (This may be possible if StrictNodes is 0, and we
|
|
|
|
* thought we needed to use an otherwise excluded node for, say, a
|
|
|
|
* directory operation.) */
|
|
|
|
crypt_path_t *hop = circ->cpath;
|
|
|
|
do {
|
|
|
|
if (routerset_contains_extendinfo(options->ExcludeNodes,
|
|
|
|
hop->extend_info))
|
|
|
|
goto next;
|
|
|
|
hop = hop->next;
|
|
|
|
} while (hop != circ->cpath);
|
|
|
|
}
|
2006-07-23 09:37:35 +02:00
|
|
|
if (!best || (best->build_state->need_uptime && !need_uptime))
|
|
|
|
best = circ;
|
2005-11-11 20:28:32 +01:00
|
|
|
next: ;
|
2006-07-23 09:37:35 +02:00
|
|
|
}
|
2005-01-20 00:13:20 +01:00
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
2005-01-20 00:13:20 +01:00
|
|
|
return best;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
2006-10-01 22:50:11 +02:00
|
|
|
/** Return the number of hops in circuit's path. */
|
|
|
|
int
|
|
|
|
circuit_get_cpath_len(origin_circuit_t *circ)
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
if (circ && circ->cpath) {
|
|
|
|
crypt_path_t *cpath, *cpath_next = NULL;
|
|
|
|
for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
|
|
|
|
cpath_next = cpath->next;
|
|
|
|
++n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2007-04-30 13:10:45 +02:00
|
|
|
/** Return the <b>hopnum</b>th hop in <b>circ</b>->cpath, or NULL if there
|
|
|
|
* aren't that many hops in the list. */
|
|
|
|
crypt_path_t *
|
|
|
|
circuit_get_cpath_hop(origin_circuit_t *circ, int hopnum)
|
|
|
|
{
|
2007-04-30 16:09:11 +02:00
|
|
|
if (circ && circ->cpath && hopnum > 0) {
|
2007-04-30 13:10:45 +02:00
|
|
|
crypt_path_t *cpath, *cpath_next = NULL;
|
|
|
|
for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
|
|
|
|
cpath_next = cpath->next;
|
|
|
|
if (--hopnum <= 0)
|
|
|
|
return cpath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-03-20 00:58:42 +01:00
|
|
|
/** Go through the circuitlist; mark-for-close each circuit that starts
|
|
|
|
* at us but has not yet been used. */
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
circuit_mark_all_unused_circs(void)
|
|
|
|
{
|
2005-03-20 00:58:42 +01:00
|
|
|
circuit_t *circ;
|
|
|
|
|
|
|
|
for (circ=global_circuitlist; circ; circ = circ->next) {
|
|
|
|
if (CIRCUIT_IS_ORIGIN(circ) &&
|
|
|
|
!circ->marked_for_close &&
|
|
|
|
!circ->timestamp_dirty)
|
2006-10-17 17:20:00 +02:00
|
|
|
circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
|
2005-03-20 00:58:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-06 00:31:21 +02:00
|
|
|
/** Go through the circuitlist; for each circuit that starts at us
|
|
|
|
* and is dirty, frob its timestamp_dirty so we won't use it for any
|
|
|
|
* new streams.
|
|
|
|
*
|
|
|
|
* This is useful for letting the user change pseudonyms, so new
|
|
|
|
* streams will not be linkable to old streams.
|
|
|
|
*/
|
2011-03-25 22:49:44 +01:00
|
|
|
/* XXX023 this is a bad name for what this function does */
|
2005-10-06 00:31:21 +02:00
|
|
|
void
|
|
|
|
circuit_expire_all_dirty_circs(void)
|
|
|
|
{
|
|
|
|
circuit_t *circ;
|
2011-06-14 19:01:38 +02:00
|
|
|
const or_options_t *options = get_options();
|
2005-10-06 00:31:21 +02:00
|
|
|
|
|
|
|
for (circ=global_circuitlist; circ; circ = circ->next) {
|
|
|
|
if (CIRCUIT_IS_ORIGIN(circ) &&
|
|
|
|
!circ->marked_for_close &&
|
|
|
|
circ->timestamp_dirty)
|
2011-03-25 22:49:44 +01:00
|
|
|
/* XXXX023 This is a screwed-up way to say "This is too dirty
|
|
|
|
* for new circuits. */
|
2005-10-06 00:31:21 +02:00
|
|
|
circ->timestamp_dirty -= options->MaxCircuitDirtiness;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
/** Mark <b>circ</b> to be closed next time we call
|
|
|
|
* circuit_close_all_marked(). Do any cleanup needed:
|
|
|
|
* - If state is onionskin_pending, remove circ from the onion_pending
|
|
|
|
* list.
|
2004-10-24 19:11:44 +02:00
|
|
|
* - If circ isn't open yet: call circuit_build_failed() if we're
|
|
|
|
* the origin, and in either case call circuit_rep_hist_note_result()
|
2004-05-13 09:24:49 +02:00
|
|
|
* to note stats.
|
2011-09-17 12:19:29 +02:00
|
|
|
* - If purpose is C_INTRODUCE_ACK_WAIT, report the intro point
|
|
|
|
* failure we just had to the hidden service client module.
|
2011-09-17 12:53:07 +02:00
|
|
|
* - If purpose is C_INTRODUCING and <b>reason</b> isn't TIMEOUT,
|
|
|
|
* report to the hidden service client module that the intro point
|
|
|
|
* we just tried may be unreachable.
|
2004-05-13 09:24:49 +02:00
|
|
|
* - Send appropriate destroys and edge_destroys for conns and
|
|
|
|
* streams attached to circ.
|
|
|
|
* - If circ->rend_splice is set (we are the midpoint of a joined
|
|
|
|
* rendezvous stream), then mark the other circuit to close as well.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
2006-01-05 22:23:03 +01:00
|
|
|
_circuit_mark_for_close(circuit_t *circ, int reason, int line,
|
|
|
|
const char *file)
|
2005-04-03 07:22:33 +02:00
|
|
|
{
|
2006-10-09 17:47:50 +02:00
|
|
|
int orig_reason = reason; /* Passed to the controller */
|
2004-05-13 09:24:49 +02:00
|
|
|
assert_circuit_ok(circ);
|
2005-04-03 07:22:33 +02:00
|
|
|
tor_assert(line);
|
|
|
|
tor_assert(file);
|
|
|
|
|
|
|
|
if (circ->marked_for_close) {
|
2005-10-24 21:37:45 +02:00
|
|
|
log(LOG_WARN,LD_BUG,
|
2005-10-19 00:56:40 +02:00
|
|
|
"Duplicate call to circuit_mark_for_close at %s:%d"
|
2005-04-03 07:31:41 +02:00
|
|
|
" (first at %s:%d)", file, line,
|
2005-04-03 07:22:33 +02:00
|
|
|
circ->marked_for_close_file, circ->marked_for_close);
|
|
|
|
return;
|
|
|
|
}
|
2006-01-05 22:23:03 +01:00
|
|
|
if (reason == END_CIRC_AT_ORIGIN) {
|
|
|
|
if (!CIRCUIT_IS_ORIGIN(circ)) {
|
2006-02-13 09:28:42 +01:00
|
|
|
log_warn(LD_BUG, "Specified 'at-origin' non-reason for ending circuit, "
|
|
|
|
"but circuit was not at origin. (called %s:%d, purpose=%d)",
|
|
|
|
file, line, circ->purpose);
|
2006-01-05 22:23:03 +01:00
|
|
|
}
|
|
|
|
reason = END_CIRC_REASON_NONE;
|
2007-07-11 05:37:45 +02:00
|
|
|
}
|
|
|
|
if (CIRCUIT_IS_ORIGIN(circ)) {
|
|
|
|
/* We don't send reasons when closing circuits at the origin. */
|
2006-01-05 22:23:03 +01:00
|
|
|
reason = END_CIRC_REASON_NONE;
|
|
|
|
}
|
2006-10-20 01:04:49 +02:00
|
|
|
|
2006-11-01 02:30:25 +01:00
|
|
|
if (reason & END_CIRC_REASON_FLAG_REMOTE)
|
2006-10-20 01:04:49 +02:00
|
|
|
reason &= ~END_CIRC_REASON_FLAG_REMOTE;
|
|
|
|
|
2006-01-05 22:23:03 +01:00
|
|
|
if (reason < _END_CIRC_REASON_MIN || reason > _END_CIRC_REASON_MAX) {
|
2006-11-01 00:35:50 +01:00
|
|
|
if (!(orig_reason & END_CIRC_REASON_FLAG_REMOTE))
|
|
|
|
log_warn(LD_BUG, "Reason %d out of range at %s:%d", reason, file, line);
|
|
|
|
reason = END_CIRC_REASON_NONE;
|
2006-01-05 22:23:03 +01:00
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
|
2006-07-23 09:37:35 +02:00
|
|
|
onion_pending_remove(TO_OR_CIRCUIT(circ));
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
/* If the circuit ever became OPEN, we sent it to the reputation history
|
|
|
|
* module then. If it isn't OPEN, we send it there now to remember which
|
|
|
|
* links worked and which didn't.
|
|
|
|
*/
|
|
|
|
if (circ->state != CIRCUIT_STATE_OPEN) {
|
2004-11-28 10:05:49 +01:00
|
|
|
if (CIRCUIT_IS_ORIGIN(circ)) {
|
2006-07-23 09:37:35 +02:00
|
|
|
origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
|
|
|
|
circuit_build_failed(ocirc); /* take actions if necessary */
|
|
|
|
circuit_rep_hist_note_result(ocirc);
|
2004-11-03 19:33:07 +01:00
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
2005-12-03 03:12:37 +01:00
|
|
|
if (circ->state == CIRCUIT_STATE_OR_WAIT) {
|
|
|
|
if (circuits_pending_or_conns)
|
|
|
|
smartlist_remove(circuits_pending_or_conns, circ);
|
|
|
|
}
|
2004-11-03 19:33:07 +01:00
|
|
|
if (CIRCUIT_IS_ORIGIN(circ)) {
|
2006-07-23 09:37:35 +02:00
|
|
|
control_event_circuit_status(TO_ORIGIN_CIRCUIT(circ),
|
2006-10-09 17:47:27 +02:00
|
|
|
(circ->state == CIRCUIT_STATE_OPEN)?CIRC_EVENT_CLOSED:CIRC_EVENT_FAILED,
|
|
|
|
orig_reason);
|
2004-11-03 19:33:07 +01:00
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
|
2006-07-23 09:37:35 +02:00
|
|
|
origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
|
2011-10-12 15:41:33 +02:00
|
|
|
int timed_out = (reason == END_CIRC_REASON_TIMEOUT);
|
2004-05-13 09:24:49 +02:00
|
|
|
tor_assert(circ->state == CIRCUIT_STATE_OPEN);
|
2006-07-23 09:37:35 +02:00
|
|
|
tor_assert(ocirc->build_state->chosen_exit);
|
2008-09-24 16:44:29 +02:00
|
|
|
tor_assert(ocirc->rend_data);
|
2004-05-13 09:24:49 +02:00
|
|
|
/* treat this like getting a nack from it */
|
2011-09-17 12:19:29 +02:00
|
|
|
log_info(LD_REND, "Failed intro circ %s to %s (awaiting ack). %s",
|
2009-12-15 23:23:36 +01:00
|
|
|
safe_str_client(ocirc->rend_data->onion_address),
|
2011-09-17 12:19:29 +02:00
|
|
|
safe_str_client(build_state_get_exit_nickname(ocirc->build_state)),
|
|
|
|
timed_out ? "Recording timeout." : "Removing from descriptor.");
|
|
|
|
rend_client_report_intro_point_failure(ocirc->build_state->chosen_exit,
|
|
|
|
ocirc->rend_data,
|
|
|
|
timed_out ?
|
|
|
|
INTRO_POINT_FAILURE_TIMEOUT :
|
|
|
|
INTRO_POINT_FAILURE_GENERIC);
|
2011-09-17 12:53:07 +02:00
|
|
|
} else if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCING &&
|
2011-10-12 15:41:33 +02:00
|
|
|
reason != END_CIRC_REASON_TIMEOUT) {
|
2011-09-17 12:53:07 +02:00
|
|
|
origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
|
|
|
|
tor_assert(ocirc->build_state->chosen_exit);
|
|
|
|
tor_assert(ocirc->rend_data);
|
|
|
|
log_info(LD_REND, "Failed intro circ %s to %s "
|
|
|
|
"(building circuit to intro point). "
|
|
|
|
"Marking intro point as possibly unreachable.",
|
|
|
|
safe_str_client(ocirc->rend_data->onion_address),
|
2009-12-15 23:23:36 +01:00
|
|
|
safe_str_client(build_state_get_exit_nickname(ocirc->build_state)));
|
2011-09-17 12:53:07 +02:00
|
|
|
rend_client_report_intro_point_failure(ocirc->build_state->chosen_exit,
|
|
|
|
ocirc->rend_data,
|
|
|
|
INTRO_POINT_FAILURE_UNREACHABLE);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
2010-07-31 00:55:24 +02:00
|
|
|
if (circ->n_conn) {
|
|
|
|
circuit_clear_cell_queue(circ, circ->n_conn);
|
2006-01-05 22:23:03 +01:00
|
|
|
connection_or_send_destroy(circ->n_circ_id, circ->n_conn, reason);
|
2010-07-31 00:55:24 +02:00
|
|
|
}
|
2006-07-23 09:37:35 +02:00
|
|
|
|
|
|
|
if (! CIRCUIT_IS_ORIGIN(circ)) {
|
|
|
|
or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
|
2006-07-26 21:07:26 +02:00
|
|
|
edge_connection_t *conn;
|
2006-07-23 09:37:35 +02:00
|
|
|
for (conn=or_circ->n_streams; conn; conn=conn->next_stream)
|
|
|
|
connection_edge_destroy(or_circ->p_circ_id, conn);
|
2009-11-23 16:13:50 +01:00
|
|
|
or_circ->n_streams = NULL;
|
2006-07-23 09:37:35 +02:00
|
|
|
|
|
|
|
while (or_circ->resolving_streams) {
|
|
|
|
conn = or_circ->resolving_streams;
|
|
|
|
or_circ->resolving_streams = conn->next_stream;
|
2006-07-26 21:07:26 +02:00
|
|
|
if (!conn->_base.marked_for_close) {
|
2007-02-07 19:25:04 +01:00
|
|
|
/* The client will see a DESTROY, and infer that the connections
|
2006-07-23 09:37:35 +02:00
|
|
|
* are closing because the circuit is getting torn down. No need
|
|
|
|
* to send an end cell. */
|
2008-12-17 15:59:28 +01:00
|
|
|
conn->edge_has_sent_end = 1;
|
2006-10-20 19:54:48 +02:00
|
|
|
conn->end_reason = END_STREAM_REASON_DESTROY;
|
2007-02-07 07:54:22 +01:00
|
|
|
conn->end_reason |= END_STREAM_REASON_FLAG_ALREADY_SENT_CLOSED;
|
2006-07-26 21:07:26 +02:00
|
|
|
connection_mark_for_close(TO_CONN(conn));
|
2006-07-23 09:37:35 +02:00
|
|
|
}
|
|
|
|
conn->on_circuit = NULL;
|
2005-03-02 04:13:05 +01:00
|
|
|
}
|
2006-07-23 09:37:35 +02:00
|
|
|
|
2010-07-31 00:55:24 +02:00
|
|
|
if (or_circ->p_conn) {
|
|
|
|
circuit_clear_cell_queue(circ, or_circ->p_conn);
|
2006-07-23 09:37:35 +02:00
|
|
|
connection_or_send_destroy(or_circ->p_circ_id, or_circ->p_conn, reason);
|
2010-07-31 00:55:24 +02:00
|
|
|
}
|
2006-07-23 09:37:35 +02:00
|
|
|
} else {
|
|
|
|
origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
|
2006-07-26 21:07:26 +02:00
|
|
|
edge_connection_t *conn;
|
2006-07-23 09:37:35 +02:00
|
|
|
for (conn=ocirc->p_streams; conn; conn=conn->next_stream)
|
|
|
|
connection_edge_destroy(circ->n_circ_id, conn);
|
2009-11-23 16:13:50 +01:00
|
|
|
ocirc->p_streams = NULL;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
2005-04-03 07:22:33 +02:00
|
|
|
circ->marked_for_close = line;
|
|
|
|
circ->marked_for_close_file = file;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2007-07-30 00:13:44 +02:00
|
|
|
if (!CIRCUIT_IS_ORIGIN(circ)) {
|
2006-07-23 09:37:35 +02:00
|
|
|
or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
|
|
|
|
if (or_circ->rend_splice) {
|
|
|
|
if (!or_circ->rend_splice->_base.marked_for_close) {
|
|
|
|
/* do this after marking this circuit, to avoid infinite recursion. */
|
|
|
|
circuit_mark_for_close(TO_CIRCUIT(or_circ->rend_splice), reason);
|
|
|
|
}
|
|
|
|
or_circ->rend_splice = NULL;
|
2005-12-14 19:55:17 +01:00
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Verify that cpath layer <b>cp</b> has all of its invariants
|
|
|
|
* correct. Trigger an assert if anything is invalid.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
assert_cpath_layer_ok(const crypt_path_t *cp)
|
2004-05-13 09:24:49 +02:00
|
|
|
{
|
|
|
|
// tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
|
|
|
|
// tor_assert(cp->port);
|
2005-03-23 07:21:48 +01:00
|
|
|
tor_assert(cp);
|
|
|
|
tor_assert(cp->magic == CRYPT_PATH_MAGIC);
|
2004-11-28 10:05:49 +01:00
|
|
|
switch (cp->state)
|
2004-05-13 09:24:49 +02:00
|
|
|
{
|
|
|
|
case CPATH_STATE_OPEN:
|
2004-05-20 01:51:39 +02:00
|
|
|
tor_assert(cp->f_crypto);
|
|
|
|
tor_assert(cp->b_crypto);
|
|
|
|
/* fall through */
|
|
|
|
case CPATH_STATE_CLOSED:
|
2005-05-03 00:35:18 +02:00
|
|
|
tor_assert(!cp->dh_handshake_state);
|
2004-05-13 09:24:49 +02:00
|
|
|
break;
|
|
|
|
case CPATH_STATE_AWAITING_KEYS:
|
2005-05-03 00:35:18 +02:00
|
|
|
/* tor_assert(cp->dh_handshake_state); */
|
2004-05-13 09:24:49 +02:00
|
|
|
break;
|
|
|
|
default:
|
2005-10-25 09:05:03 +02:00
|
|
|
log_fn(LOG_ERR, LD_BUG, "Unexpected state %d", cp->state);
|
2004-05-13 09:24:49 +02:00
|
|
|
tor_assert(0);
|
|
|
|
}
|
|
|
|
tor_assert(cp->package_window >= 0);
|
|
|
|
tor_assert(cp->deliver_window >= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Verify that cpath <b>cp</b> has all of its invariants
|
|
|
|
* correct. Trigger an assert if anything is invalid.
|
|
|
|
*/
|
|
|
|
static void
|
2004-05-18 17:35:21 +02:00
|
|
|
assert_cpath_ok(const crypt_path_t *cp)
|
2004-05-13 09:24:49 +02:00
|
|
|
{
|
2004-05-18 17:35:21 +02:00
|
|
|
const crypt_path_t *start = cp;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2004-05-18 17:35:21 +02:00
|
|
|
do {
|
2004-05-13 09:24:49 +02:00
|
|
|
assert_cpath_layer_ok(cp);
|
|
|
|
/* layers must be in sequence of: "open* awaiting? closed*" */
|
2004-05-18 17:35:21 +02:00
|
|
|
if (cp != start) {
|
|
|
|
if (cp->state == CPATH_STATE_AWAITING_KEYS) {
|
|
|
|
tor_assert(cp->prev->state == CPATH_STATE_OPEN);
|
|
|
|
} else if (cp->state == CPATH_STATE_OPEN) {
|
|
|
|
tor_assert(cp->prev->state == CPATH_STATE_OPEN);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
cp = cp->next;
|
2004-05-18 17:35:21 +02:00
|
|
|
tor_assert(cp);
|
|
|
|
} while (cp != start);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Verify that circuit <b>c</b> has all of its invariants
|
|
|
|
* correct. Trigger an assert if anything is invalid.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
assert_circuit_ok(const circuit_t *c)
|
2004-05-13 09:24:49 +02:00
|
|
|
{
|
2006-07-26 21:07:26 +02:00
|
|
|
edge_connection_t *conn;
|
2006-07-23 09:37:35 +02:00
|
|
|
const or_circuit_t *or_circ = NULL;
|
|
|
|
const origin_circuit_t *origin_circ = NULL;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
tor_assert(c);
|
2006-07-23 09:37:35 +02:00
|
|
|
tor_assert(c->magic == ORIGIN_CIRCUIT_MAGIC || c->magic == OR_CIRCUIT_MAGIC);
|
2004-05-13 09:24:49 +02:00
|
|
|
tor_assert(c->purpose >= _CIRCUIT_PURPOSE_MIN &&
|
|
|
|
c->purpose <= _CIRCUIT_PURPOSE_MAX);
|
|
|
|
|
2007-10-16 15:59:37 +02:00
|
|
|
{
|
|
|
|
/* Having a separate variable for this pleases GCC 4.2 in ways I hope I
|
|
|
|
* never understand. -NM. */
|
|
|
|
circuit_t *nonconst_circ = (circuit_t*) c;
|
|
|
|
if (CIRCUIT_IS_ORIGIN(c))
|
|
|
|
origin_circ = TO_ORIGIN_CIRCUIT(nonconst_circ);
|
|
|
|
else
|
|
|
|
or_circ = TO_OR_CIRCUIT(nonconst_circ);
|
|
|
|
}
|
2006-07-23 09:37:35 +02:00
|
|
|
|
2004-07-03 03:45:13 +02:00
|
|
|
if (c->n_conn) {
|
2008-07-30 15:04:32 +02:00
|
|
|
tor_assert(!c->n_hop);
|
|
|
|
|
2008-11-01 21:27:41 +01:00
|
|
|
if (c->n_circ_id) {
|
|
|
|
/* We use the _impl variant here to make sure we don't fail on marked
|
|
|
|
* circuits, which would not be returned by the regular function. */
|
|
|
|
circuit_t *c2 = circuit_get_by_circid_orconn_impl(c->n_circ_id,
|
|
|
|
c->n_conn);
|
|
|
|
tor_assert(c == c2);
|
|
|
|
}
|
2004-07-03 03:45:13 +02:00
|
|
|
}
|
2006-07-23 09:37:35 +02:00
|
|
|
if (or_circ && or_circ->p_conn) {
|
2008-11-01 21:27:41 +01:00
|
|
|
if (or_circ->p_circ_id) {
|
|
|
|
/* ibid */
|
|
|
|
circuit_t *c2 = circuit_get_by_circid_orconn_impl(or_circ->p_circ_id,
|
|
|
|
or_circ->p_conn);
|
|
|
|
tor_assert(c == c2);
|
|
|
|
}
|
2005-04-06 07:33:32 +02:00
|
|
|
}
|
2006-07-23 09:37:35 +02:00
|
|
|
if (or_circ)
|
|
|
|
for (conn = or_circ->n_streams; conn; conn = conn->next_stream)
|
2006-07-26 21:07:26 +02:00
|
|
|
tor_assert(conn->_base.type == CONN_TYPE_EXIT);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
tor_assert(c->deliver_window >= 0);
|
|
|
|
tor_assert(c->package_window >= 0);
|
|
|
|
if (c->state == CIRCUIT_STATE_OPEN) {
|
2008-02-06 00:20:49 +01:00
|
|
|
tor_assert(!c->n_conn_onionskin);
|
2006-07-23 09:37:35 +02:00
|
|
|
if (or_circ) {
|
|
|
|
tor_assert(or_circ->n_crypto);
|
|
|
|
tor_assert(or_circ->p_crypto);
|
|
|
|
tor_assert(or_circ->n_digest);
|
|
|
|
tor_assert(or_circ->p_digest);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
}
|
2005-12-03 03:12:37 +01:00
|
|
|
if (c->state == CIRCUIT_STATE_OR_WAIT && !c->marked_for_close) {
|
|
|
|
tor_assert(circuits_pending_or_conns &&
|
|
|
|
smartlist_isin(circuits_pending_or_conns, c));
|
|
|
|
} else {
|
2005-12-03 04:36:32 +01:00
|
|
|
tor_assert(!circuits_pending_or_conns ||
|
2005-12-03 03:12:37 +01:00
|
|
|
!smartlist_isin(circuits_pending_or_conns, c));
|
|
|
|
}
|
2006-07-23 10:13:45 +02:00
|
|
|
if (origin_circ && origin_circ->cpath) {
|
2006-07-23 09:37:35 +02:00
|
|
|
assert_cpath_ok(origin_circ->cpath);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
if (c->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED) {
|
2006-07-23 09:37:35 +02:00
|
|
|
tor_assert(or_circ);
|
2004-05-13 09:24:49 +02:00
|
|
|
if (!c->marked_for_close) {
|
2006-07-23 09:37:35 +02:00
|
|
|
tor_assert(or_circ->rend_splice);
|
|
|
|
tor_assert(or_circ->rend_splice->rend_splice == or_circ);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
2006-07-23 09:37:35 +02:00
|
|
|
tor_assert(or_circ->rend_splice != or_circ);
|
2004-05-13 09:24:49 +02:00
|
|
|
} else {
|
2006-07-23 09:37:35 +02:00
|
|
|
tor_assert(!or_circ || !or_circ->rend_splice);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
}
|
2005-06-09 21:03:31 +02:00
|
|
|
|