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-02-12 22:39:53 +01:00
|
|
|
* Copyright (c) 2004-2007, Roger Dingledine, Nick Mathewson. */
|
2004-05-13 09:24:49 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
/* $Id$ */
|
2005-12-14 21:40:40 +01:00
|
|
|
const char circuitlist_c_id[] =
|
|
|
|
"$Id$";
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \file circuitlist.c
|
|
|
|
* \brief Manage the global circuit list.
|
|
|
|
**/
|
|
|
|
|
|
|
|
#include "or.h"
|
2005-09-29 22:49:41 +02:00
|
|
|
|
2005-11-23 05:18:45 +01:00
|
|
|
#include "../common/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;
|
2005-04-06 07:33:32 +02:00
|
|
|
uint16_t circ_id;
|
|
|
|
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
|
|
|
{
|
2005-11-23 05:18:45 +01:00
|
|
|
return (((unsigned)a->circ_id)<<16) ^ (unsigned)(uintptr_t)(a->or_conn);
|
|
|
|
}
|
|
|
|
|
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
|
2007-05-04 12:34:47 +02:00
|
|
|
* the old entry (if any) and adding a new one. If <b>active</b> is true,
|
2007-03-26 16:08:35 +02:00
|
|
|
* remove the circuit from the list of active circuits on old_conn and add it
|
2007-05-04 12:34:47 +02:00
|
|
|
* to the list of active circuits on conn.
|
|
|
|
* XXX "active" isn't an arg anymore */
|
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,
|
|
|
|
uint16_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;
|
|
|
|
uint16_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
|
|
|
|
circuit_set_p_circid_orconn(or_circuit_t *circ, uint16_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
|
|
|
|
circuit_set_n_circid_orconn(circuit_t *circ, uint16_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
|
|
|
|
circuit_set_state(circuit_t *circ, int state)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
circ->state = state;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
SMARTLIST_FOREACH(circuits_pending_or_conns, circuit_t *, circ,
|
|
|
|
{
|
|
|
|
if (circ->marked_for_close)
|
|
|
|
continue;
|
|
|
|
tor_assert(circ->state == CIRCUIT_STATE_OR_WAIT);
|
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;
|
|
|
|
} 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;
|
2007-02-05 18:42:40 +01:00
|
|
|
}
|
2007-06-15 08:01:04 +02:00
|
|
|
smartlist_add(out, 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);
|
|
|
|
log_debug(LD_CIRC,"or_conn to %s, %d pending circs",
|
|
|
|
or_conn->nickname ? or_conn->nickname : "NULL", cnt);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2004-05-13 09:24:49 +02:00
|
|
|
circ->timestamp_created = time(NULL);
|
|
|
|
|
|
|
|
circ->package_window = CIRCWINDOW_START;
|
|
|
|
circ->deliver_window = CIRCWINDOW_START;
|
|
|
|
|
|
|
|
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++;
|
2006-07-23 09:37:35 +02:00
|
|
|
|
|
|
|
init_circuit_base(TO_CIRCUIT(circ));
|
|
|
|
|
|
|
|
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 *
|
2006-07-26 21:07:26 +02:00
|
|
|
or_circuit_new(uint16_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);
|
|
|
|
|
|
|
|
init_circuit_base(TO_CIRCUIT(circ));
|
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;
|
2004-05-13 09:24:49 +02:00
|
|
|
tor_assert(circ);
|
2006-07-23 09:37:35 +02:00
|
|
|
if (CIRCUIT_IS_ORIGIN(circ)) {
|
|
|
|
origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
|
|
|
|
mem = ocirc;
|
|
|
|
tor_assert(circ->magic == ORIGIN_CIRCUIT_MAGIC);
|
|
|
|
if (ocirc->build_state) {
|
|
|
|
if (ocirc->build_state->chosen_exit)
|
|
|
|
extend_info_free(ocirc->build_state->chosen_exit);
|
|
|
|
if (ocirc->build_state->pending_final_cpath)
|
|
|
|
circuit_free_cpath_node(ocirc->build_state->pending_final_cpath);
|
|
|
|
}
|
|
|
|
tor_free(ocirc->build_state);
|
|
|
|
|
|
|
|
circuit_free_cpath(ocirc->cpath);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
or_circuit_t *ocirc = TO_OR_CIRCUIT(circ);
|
|
|
|
mem = ocirc;
|
|
|
|
tor_assert(circ->magic == OR_CIRCUIT_MAGIC);
|
|
|
|
|
|
|
|
if (ocirc->p_crypto)
|
|
|
|
crypto_free_cipher_env(ocirc->p_crypto);
|
|
|
|
if (ocirc->p_digest)
|
|
|
|
crypto_free_digest_env(ocirc->p_digest);
|
|
|
|
if (ocirc->n_crypto)
|
|
|
|
crypto_free_cipher_env(ocirc->n_crypto);
|
|
|
|
if (ocirc->n_digest)
|
|
|
|
crypto_free_digest_env(ocirc->n_digest);
|
|
|
|
|
|
|
|
if (ocirc->rend_splice) {
|
|
|
|
or_circuit_t *other = ocirc->rend_splice;
|
|
|
|
tor_assert(other->_base.magic == OR_CIRCUIT_MAGIC);
|
|
|
|
other->rend_splice = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
tor_free(circ->onionskin);
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
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);
|
|
|
|
|
2004-05-13 09:24:49 +02:00
|
|
|
memset(circ, 0xAA, sizeof(circuit_t)); /* 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;
|
|
|
|
}
|
2005-12-14 01:52:15 +01:00
|
|
|
if (circuits_pending_or_conns) {
|
|
|
|
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)
|
|
|
|
{
|
2004-11-28 10:05:49 +01:00
|
|
|
if (victim->f_crypto)
|
2004-05-13 09:24:49 +02:00
|
|
|
crypto_free_cipher_env(victim->f_crypto);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (victim->b_crypto)
|
2004-05-13 09:24:49 +02:00
|
|
|
crypto_free_cipher_env(victim->b_crypto);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (victim->f_digest)
|
2004-05-13 09:24:49 +02:00
|
|
|
crypto_free_digest_env(victim->f_digest);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (victim->b_digest)
|
2004-05-13 09:24:49 +02:00
|
|
|
crypto_free_digest_env(victim->b_digest);
|
2005-05-03 00:35:18 +02:00
|
|
|
if (victim->dh_handshake_state)
|
|
|
|
crypto_dh_free(victim->dh_handshake_state);
|
2005-06-29 23:46:55 +02:00
|
|
|
if (victim->extend_info)
|
|
|
|
extend_info_free(victim->extend_info);
|
|
|
|
|
2005-03-23 07:21:48 +01:00
|
|
|
victim->magic = 0xDEADBEEFu;
|
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), "
|
|
|
|
"state %d (%s), born %d:",
|
2006-07-27 07:03:57 +02:00
|
|
|
conn_array_index, type, this_circid, other_circid, circ->state,
|
2006-07-23 09:37:35 +02:00
|
|
|
circuit_state_to_string(circ->state), (int)circ->timestamp_created);
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!circ->n_conn && circ->n_addr && circ->n_port &&
|
|
|
|
circ->n_addr == conn->addr &&
|
|
|
|
circ->n_port == conn->port &&
|
2006-07-26 21:07:26 +02:00
|
|
|
conn->type == CONN_TYPE_OR &&
|
|
|
|
!memcmp(TO_OR_CONN(conn)->identity_digest, circ->n_conn_id_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 *
|
2006-07-26 21:07:26 +02:00
|
|
|
circuit_get_by_circid_orconn_impl(uint16_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;
|
2005-04-06 08:13:49 +02:00
|
|
|
|
2005-11-11 20:28:32 +01:00
|
|
|
/* The rest of this checks for bugs. Disabled by default. */
|
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;
|
2005-04-06 08:13:49 +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 *
|
2006-07-26 21:07:26 +02:00
|
|
|
circuit_get_by_circid_orconn(uint16_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;
|
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
* - circ-\>rend_query is equal to <b>rend_query</b>, and
|
|
|
|
* - 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 &&
|
|
|
|
circ->purpose == purpose &&
|
2006-07-26 21:05:34 +02:00
|
|
|
!rend_cmp_service_ids(rend_query, TO_ORIGIN_CIRCUIT(circ)->rend_query))
|
|
|
|
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 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);
|
|
|
|
else if (!memcmp(TO_ORIGIN_CIRCUIT(circ)->rend_pk_digest,
|
|
|
|
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 &&
|
|
|
|
! memcmp(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);
|
|
|
|
}
|
|
|
|
|
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 a circuit that is open, has specified <b>purpose</b>,
|
2005-11-11 20:25:30 +01:00
|
|
|
* has a timestamp_dirty value of 0, is uptime/capacity/internal
|
|
|
|
* if required, and if info is defined, does not already use info
|
|
|
|
* as any of its hops; or NULL if no circuit fits this description.
|
2005-01-20 00:13:20 +01:00
|
|
|
*
|
2007-02-24 02:12:53 +01:00
|
|
|
* If ! 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,
|
|
|
|
int need_uptime,
|
|
|
|
int need_capacity, int internal)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2006-07-23 09:37:35 +02:00
|
|
|
circuit_t *_circ;
|
|
|
|
origin_circuit_t *best=NULL;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
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 &&
|
|
|
|
_circ->purpose == purpose &&
|
|
|
|
!_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) &&
|
|
|
|
(internal == circ->build_state->is_internal)) {
|
|
|
|
if (info) {
|
|
|
|
/* need to make sure we don't duplicate hops */
|
|
|
|
crypt_path_t *hop = circ->cpath;
|
|
|
|
do {
|
|
|
|
if (!memcmp(hop->extend_info->identity_digest,
|
|
|
|
info->identity_digest, DIGEST_LEN))
|
|
|
|
goto next;
|
|
|
|
hop=hop->next;
|
|
|
|
} while (hop!=circ->cpath);
|
|
|
|
}
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
circuit_expire_all_dirty_circs(void)
|
|
|
|
{
|
|
|
|
circuit_t *circ;
|
|
|
|
or_options_t *options = get_options();
|
|
|
|
|
|
|
|
for (circ=global_circuitlist; circ; circ = circ->next) {
|
|
|
|
if (CIRCUIT_IS_ORIGIN(circ) &&
|
|
|
|
!circ->marked_for_close &&
|
|
|
|
circ->timestamp_dirty)
|
|
|
|
circ->timestamp_dirty -= options->MaxCircuitDirtiness;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-04 12:34:47 +02:00
|
|
|
/** Return 1 if there are any origin circuits that use
|
|
|
|
* <b>conn</b> as there first hop. Else return 0. */
|
|
|
|
static int
|
|
|
|
circuit_any_origin_circs_on_conn(or_connection_t *conn)
|
|
|
|
{
|
|
|
|
circuit_t *circ;
|
|
|
|
|
|
|
|
for (circ=global_circuitlist; circ; circ = circ->next) {
|
|
|
|
if (CIRCUIT_IS_ORIGIN(circ) &&
|
|
|
|
!circ->marked_for_close &&
|
|
|
|
circ->n_conn == conn)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
* - If purpose is C_INTRODUCE_ACK_WAIT, remove the intro point we
|
|
|
|
* just tried from our list of intro points for that service
|
|
|
|
* descriptor.
|
|
|
|
* - 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);
|
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);
|
2004-05-13 09:24:49 +02:00
|
|
|
/* treat this like getting a nack from it */
|
2006-02-13 09:28:42 +01:00
|
|
|
log_info(LD_REND, "Failed intro circ %s to %s (awaiting ack). "
|
|
|
|
"Removing from descriptor.",
|
2006-07-26 21:05:34 +02:00
|
|
|
safe_str(ocirc->rend_query),
|
2006-07-23 09:37:35 +02:00
|
|
|
safe_str(build_state_get_exit_nickname(ocirc->build_state)));
|
|
|
|
rend_client_remove_intro_point(ocirc->build_state->chosen_exit,
|
2006-07-26 21:05:34 +02:00
|
|
|
ocirc->rend_query);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
2004-11-28 10:05:49 +01:00
|
|
|
if (circ->n_conn)
|
2006-01-05 22:23:03 +01:00
|
|
|
connection_or_send_destroy(circ->n_circ_id, circ->n_conn, reason);
|
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);
|
|
|
|
|
|
|
|
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. */
|
2006-07-26 21:07:26 +02:00
|
|
|
conn->_base.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
|
|
|
|
|
|
|
if (or_circ->p_conn)
|
|
|
|
connection_or_send_destroy(or_circ->p_circ_id, or_circ->p_conn, reason);
|
|
|
|
} 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);
|
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-05-04 12:34:47 +02:00
|
|
|
if (CIRCUIT_IS_ORIGIN(circ)) {
|
|
|
|
if (circ->n_conn && circ->n_conn->client_used) {
|
|
|
|
circ->n_conn->client_used =
|
|
|
|
circuit_any_origin_circs_on_conn(circ->n_conn);
|
|
|
|
}
|
|
|
|
} else {
|
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;
|
2007-05-15 23:17:42 +02:00
|
|
|
circuit_t *nonconst_circ;
|
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-05-15 23:17:42 +02:00
|
|
|
/* Having a separate variable for this pleases GCC 4.2 in ways I hop I never
|
|
|
|
* understand. -NM. */
|
|
|
|
nonconst_circ = (circuit_t*) c;
|
2006-07-23 09:37:35 +02:00
|
|
|
if (CIRCUIT_IS_ORIGIN(c))
|
2007-05-15 23:17:42 +02:00
|
|
|
origin_circ = TO_ORIGIN_CIRCUIT(nonconst_circ);
|
2006-07-23 09:37:35 +02:00
|
|
|
else
|
2007-05-15 23:17:42 +02:00
|
|
|
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) {
|
2005-12-14 21:40:40 +01:00
|
|
|
tor_assert(!memcmp(c->n_conn->identity_digest, c->n_conn_id_digest,
|
|
|
|
DIGEST_LEN));
|
2005-04-06 07:33:32 +02:00
|
|
|
if (c->n_circ_id)
|
|
|
|
tor_assert(c == circuit_get_by_circid_orconn(c->n_circ_id, c->n_conn));
|
2004-07-03 03:45:13 +02:00
|
|
|
}
|
2006-07-23 09:37:35 +02:00
|
|
|
if (or_circ && or_circ->p_conn) {
|
|
|
|
if (or_circ->p_circ_id)
|
|
|
|
tor_assert(c == circuit_get_by_circid_orconn(or_circ->p_circ_id,
|
|
|
|
or_circ->p_conn));
|
2005-04-06 07:33:32 +02:00
|
|
|
}
|
2006-07-30 06:54:13 +02:00
|
|
|
#if 0 /* false now that rendezvous exits are attached to p_streams */
|
2006-07-23 09:37:35 +02:00
|
|
|
if (origin_circ)
|
|
|
|
for (conn = origin_circ->p_streams; conn; conn = conn->next_stream)
|
2006-07-26 21:07:26 +02:00
|
|
|
tor_assert(conn->_base.type == CONN_TYPE_AP);
|
2006-07-30 06:54:13 +02:00
|
|
|
#endif
|
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) {
|
2005-10-29 21:13:48 +02:00
|
|
|
tor_assert(!c->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
|
|
|
|