2004-11-07 02:33:06 +01:00
|
|
|
/* Copyright 2001 Matej Pfajfar.
|
|
|
|
* Copyright 2001-2004 Roger Dingledine.
|
2005-04-01 22:15:56 +02:00
|
|
|
* Copyright 2004-2005 Roger Dingledine, Nick Mathewson. */
|
2004-05-13 09:24:49 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
/* $Id$ */
|
2004-11-29 23:25:31 +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-04-06 07:33:32 +02:00
|
|
|
#include "tree.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
|
|
|
|
|
|
|
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-04-06 07:33:32 +02:00
|
|
|
struct orconn_circid_circuit_map_t {
|
2005-04-07 07:09:19 +02:00
|
|
|
RB_ENTRY(orconn_circid_circuit_map_t) node;
|
2005-04-06 07:33:32 +02:00
|
|
|
connection_t *or_conn;
|
|
|
|
uint16_t circ_id;
|
|
|
|
circuit_t *circuit;
|
|
|
|
};
|
|
|
|
|
2005-06-29 23:46:55 +02:00
|
|
|
/** helper for RB tree: compare the OR connection and circuit ID for a and b,
|
|
|
|
* and return less than, equal to, or greater than zero appropriately.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static INLINE int
|
|
|
|
compare_orconn_circid_entries(struct orconn_circid_circuit_map_t *a,
|
|
|
|
struct orconn_circid_circuit_map_t *b)
|
2005-04-06 07:33:32 +02:00
|
|
|
{
|
|
|
|
if (a->or_conn < b->or_conn)
|
|
|
|
return -1;
|
|
|
|
else if (a->or_conn > b->or_conn)
|
|
|
|
return 1;
|
|
|
|
else
|
2005-04-07 06:50:25 +02:00
|
|
|
return ((int)b->circ_id) - ((int)a->circ_id);
|
2005-04-06 07:33:32 +02:00
|
|
|
};
|
2005-06-11 20:52:12 +02:00
|
|
|
|
2005-04-07 07:09:19 +02:00
|
|
|
static RB_HEAD(orconn_circid_tree, orconn_circid_circuit_map_t) orconn_circid_circuit_map = RB_INITIALIZER(orconn_circid_circuit_map);
|
|
|
|
RB_PROTOTYPE(orconn_circid_tree, orconn_circid_circuit_map_t, node, compare_orconn_circid_entries);
|
|
|
|
RB_GENERATE(orconn_circid_tree, orconn_circid_circuit_map_t, node, compare_orconn_circid_entries);
|
|
|
|
|
2005-06-29 23:46:55 +02:00
|
|
|
/** The most recently returned entyr from circuit_get_by_circid_orconn; used
|
|
|
|
* to improve performance when many cells arrive in a row from the same circuit.
|
|
|
|
*/
|
|
|
|
/* (We tried using splay trees, but round-robin turned out to make them suck.) */
|
2005-04-07 07:09:19 +02:00
|
|
|
struct orconn_circid_circuit_map_t *_last_circid_orconn_ent = NULL;
|
2005-04-06 07:33:32 +02:00
|
|
|
|
2005-06-29 23:46:55 +02:00
|
|
|
/** Set the p_conn or 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. */
|
2005-04-06 07:33:32 +02:00
|
|
|
void
|
|
|
|
circuit_set_circid_orconn(circuit_t *circ, uint16_t id,
|
|
|
|
connection_t *conn,
|
|
|
|
enum which_conn_changed_t which)
|
|
|
|
{
|
|
|
|
uint16_t old_id;
|
|
|
|
connection_t *old_conn;
|
|
|
|
struct orconn_circid_circuit_map_t search;
|
|
|
|
struct orconn_circid_circuit_map_t *found;
|
|
|
|
|
|
|
|
tor_assert(!conn || conn->type == CONN_TYPE_OR);
|
|
|
|
|
|
|
|
if (which == P_CONN_CHANGED) {
|
|
|
|
old_id = circ->p_circ_id;
|
|
|
|
old_conn = circ->p_conn;
|
|
|
|
circ->p_circ_id = id;
|
|
|
|
circ->p_conn = conn;
|
|
|
|
} else {
|
|
|
|
old_id = circ->n_circ_id;
|
|
|
|
old_conn = circ->n_conn;
|
|
|
|
circ->n_circ_id = id;
|
|
|
|
circ->n_conn = conn;
|
|
|
|
}
|
|
|
|
|
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-04-06 07:33:32 +02:00
|
|
|
if (old_conn) {
|
|
|
|
search.circ_id = old_id;
|
|
|
|
search.or_conn = old_conn;
|
2005-04-07 07:09:19 +02:00
|
|
|
found = RB_FIND(orconn_circid_tree, &orconn_circid_circuit_map, &search);
|
2005-04-06 07:33:32 +02:00
|
|
|
if (found) {
|
2005-04-07 07:09:19 +02:00
|
|
|
RB_REMOVE(orconn_circid_tree, &orconn_circid_circuit_map, found);
|
2005-04-06 07:33:32 +02:00
|
|
|
}
|
|
|
|
tor_free(found);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (conn == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
search.circ_id = id;
|
|
|
|
search.or_conn = conn;
|
2005-04-07 07:09:19 +02:00
|
|
|
found = RB_FIND(orconn_circid_tree, &orconn_circid_circuit_map, &search);
|
2005-04-06 07:33:32 +02:00
|
|
|
if (found) {
|
|
|
|
found->circuit = circ;
|
|
|
|
} else {
|
|
|
|
found = tor_malloc_zero(sizeof(struct orconn_circid_circuit_map_t));
|
|
|
|
found->circ_id = id;
|
|
|
|
found->or_conn = conn;
|
|
|
|
found->circuit = circ;
|
2005-04-07 07:09:19 +02:00
|
|
|
RB_INSERT(orconn_circid_tree, &orconn_circid_circuit_map, found);
|
2005-04-06 07:33:32 +02: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-29 23:46:55 +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 *
|
|
|
|
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";
|
|
|
|
case CIRCUIT_STATE_OR_WAIT: return "connecting to firsthop";
|
|
|
|
case CIRCUIT_STATE_OPEN: return "open";
|
|
|
|
default:
|
|
|
|
log_fn(LOG_WARN, "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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-13 09:24:49 +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.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
circuit_t *
|
|
|
|
circuit_new(uint16_t p_circ_id, connection_t *p_conn)
|
|
|
|
{
|
2004-05-13 09:24:49 +02:00
|
|
|
circuit_t *circ;
|
2005-03-22 01:42:38 +01:00
|
|
|
static uint32_t n_circuits_allocated = 1;
|
2005-03-22 02:01:15 +01:00
|
|
|
/* never zero, since a global ID of 0 is treated specially by the controller */
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
circ = tor_malloc_zero(sizeof(circuit_t));
|
|
|
|
circ->magic = CIRCUIT_MAGIC;
|
|
|
|
|
|
|
|
circ->timestamp_created = time(NULL);
|
|
|
|
|
|
|
|
circ->state = CIRCUIT_STATE_ONIONSKIN_PENDING;
|
|
|
|
|
|
|
|
/* CircIDs */
|
2005-04-06 07:33:32 +02:00
|
|
|
if (p_conn) {
|
|
|
|
circuit_set_circid_orconn(circ, p_circ_id, p_conn, P_CONN_CHANGED);
|
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
/* circ->n_circ_id remains 0 because we haven't identified the next hop yet */
|
|
|
|
|
|
|
|
circ->package_window = CIRCWINDOW_START;
|
|
|
|
circ->deliver_window = CIRCWINDOW_START;
|
|
|
|
|
|
|
|
circ->next_stream_id = crypto_pseudo_rand_int(1<<16);
|
2004-11-03 19:33:07 +01:00
|
|
|
circ->global_identifier = n_circuits_allocated++;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
circuit_add(circ);
|
|
|
|
|
|
|
|
return circ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Deallocate space associated with circ.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static void
|
|
|
|
circuit_free(circuit_t *circ)
|
|
|
|
{
|
2004-05-13 09:24:49 +02:00
|
|
|
tor_assert(circ);
|
|
|
|
tor_assert(circ->magic == CIRCUIT_MAGIC);
|
|
|
|
if (circ->n_crypto)
|
|
|
|
crypto_free_cipher_env(circ->n_crypto);
|
|
|
|
if (circ->p_crypto)
|
|
|
|
crypto_free_cipher_env(circ->p_crypto);
|
|
|
|
if (circ->n_digest)
|
|
|
|
crypto_free_digest_env(circ->n_digest);
|
|
|
|
if (circ->p_digest)
|
|
|
|
crypto_free_digest_env(circ->p_digest);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (circ->build_state) {
|
2005-06-29 23:46:55 +02:00
|
|
|
if (circ->build_state->chosen_exit)
|
|
|
|
extend_info_free(circ->build_state->chosen_exit);
|
2004-05-13 09:24:49 +02:00
|
|
|
if (circ->build_state->pending_final_cpath)
|
|
|
|
circuit_free_cpath_node(circ->build_state->pending_final_cpath);
|
|
|
|
}
|
|
|
|
tor_free(circ->build_state);
|
|
|
|
circuit_free_cpath(circ->cpath);
|
|
|
|
if (circ->rend_splice) {
|
|
|
|
circ->rend_splice->rend_splice = NULL;
|
|
|
|
}
|
2005-04-06 07:33:32 +02:00
|
|
|
/* Remove from map. */
|
|
|
|
circuit_set_circid_orconn(circ, 0, NULL, P_CONN_CHANGED);
|
|
|
|
circuit_set_circid_orconn(circ, 0, NULL, N_CONN_CHANGED);
|
2004-05-13 09:24:49 +02:00
|
|
|
|
|
|
|
memset(circ, 0xAA, sizeof(circuit_t)); /* poison memory */
|
2004-09-29 08:52:36 +02:00
|
|
|
tor_free(circ);
|
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;
|
|
|
|
while (global_circuitlist->resolving_streams) {
|
|
|
|
connection_t *next;
|
|
|
|
next = global_circuitlist->resolving_streams->next_stream;
|
|
|
|
connection_free(global_circuitlist->resolving_streams);
|
|
|
|
global_circuitlist->resolving_streams = next;
|
|
|
|
}
|
|
|
|
circuit_free(global_circuitlist);
|
|
|
|
global_circuitlist = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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. */
|
2005-03-12 05:22:01 +01:00
|
|
|
circuit_t *
|
|
|
|
circuit_get_by_global_id(uint32_t id)
|
|
|
|
{
|
|
|
|
circuit_t *circ;
|
|
|
|
for (circ=global_circuitlist;circ;circ = circ->next) {
|
|
|
|
if (circ->global_identifier == id) {
|
|
|
|
if (circ->marked_for_close)
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
return circ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
* - circ is attached to <b>conn</b>, either as p_conn, n-conn, or
|
|
|
|
* in p_streams or n_streams.
|
|
|
|
* Return NULL if no such circuit exists.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
circuit_t *
|
|
|
|
circuit_get_by_circid_orconn(uint16_t circ_id, connection_t *conn)
|
|
|
|
{
|
2005-04-06 07:33:32 +02:00
|
|
|
struct orconn_circid_circuit_map_t search;
|
|
|
|
struct orconn_circid_circuit_map_t *found;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2005-04-06 07:33:32 +02:00
|
|
|
tor_assert(conn->type == CONN_TYPE_OR);
|
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;
|
|
|
|
found = RB_FIND(orconn_circid_tree, &orconn_circid_circuit_map, &search);
|
|
|
|
_last_circid_orconn_ent = found;
|
|
|
|
}
|
2005-04-06 07:33:32 +02:00
|
|
|
if (found && found->circuit && !found->circuit->marked_for_close)
|
|
|
|
return found->circuit;
|
2005-04-06 08:13:49 +02:00
|
|
|
|
|
|
|
/* The rest of this can be replaced with
|
|
|
|
"return NULL;" once we believe the code works. */
|
|
|
|
|
|
|
|
{
|
|
|
|
circuit_t *circ;
|
|
|
|
for (circ=global_circuitlist;circ;circ = circ->next) {
|
|
|
|
if (circ->marked_for_close)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (circ->p_conn == conn && circ->p_circ_id == circ_id) {
|
|
|
|
log_fn(LOG_WARN, "circuit matches p_conn, but not in tree (Bug!)");
|
|
|
|
return circ;
|
|
|
|
}
|
|
|
|
if (circ->n_conn == conn && circ->n_circ_id == circ_id) {
|
|
|
|
log_fn(LOG_WARN, "circuit matches n_conn, but not in tree (Bug!)");
|
|
|
|
return circ;
|
|
|
|
}
|
|
|
|
}
|
2005-04-06 07:33:32 +02:00
|
|
|
return NULL;
|
2005-04-06 08:13:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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 *
|
|
|
|
circuit_get_by_edge_conn(connection_t *conn)
|
2005-04-06 08:13:49 +02:00
|
|
|
{
|
|
|
|
circuit_t *circ;
|
2005-07-01 04:02:24 +02:00
|
|
|
#if 0
|
2005-04-06 08:13:49 +02:00
|
|
|
connection_t *tmpconn;
|
2005-07-01 04:02:24 +02:00
|
|
|
#endif
|
2005-04-06 08:13:49 +02:00
|
|
|
tor_assert(CONN_IS_EDGE(conn));
|
|
|
|
|
|
|
|
if (! conn->on_circuit) {
|
|
|
|
/* return NULL; */
|
|
|
|
circ = circuit_get_by_conn(conn);
|
|
|
|
if (circ) {
|
2005-05-03 12:04:08 +02:00
|
|
|
log_fn(LOG_WARN, "BUG: conn->on_circuit==NULL, but there was in fact a circuit there.");
|
2005-04-06 08:13:49 +02:00
|
|
|
}
|
|
|
|
return circ;
|
|
|
|
}
|
|
|
|
|
|
|
|
circ = conn->on_circuit;
|
|
|
|
tor_assert(circ->magic == CIRCUIT_MAGIC);
|
2005-06-29 23:46:55 +02:00
|
|
|
#if 0
|
|
|
|
/* All this stuff here is sanity-checking. */
|
2005-04-06 08:13:49 +02:00
|
|
|
for (tmpconn = circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream)
|
|
|
|
if (tmpconn == conn)
|
|
|
|
return circ;
|
|
|
|
for (tmpconn = circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream)
|
|
|
|
if (tmpconn == conn)
|
|
|
|
return circ;
|
|
|
|
for (tmpconn = circ->resolving_streams; tmpconn; tmpconn=tmpconn->next_stream)
|
|
|
|
if (tmpconn == conn)
|
|
|
|
return circ;
|
|
|
|
|
|
|
|
tor_assert(0);
|
2005-06-29 23:46:55 +02:00
|
|
|
#endif
|
|
|
|
return circ;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Return a circ such that circ is attached to <b>conn</b>, either as
|
2004-05-21 14:25:15 +02:00
|
|
|
* p_conn, n-conn, or in p_streams or n_streams or resolving_streams.
|
2004-05-13 09:24:49 +02:00
|
|
|
*
|
|
|
|
* Return NULL if no such circuit exists.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
circuit_t *
|
|
|
|
circuit_get_by_conn(connection_t *conn)
|
|
|
|
{
|
2004-05-13 09:24:49 +02:00
|
|
|
circuit_t *circ;
|
|
|
|
connection_t *tmpconn;
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
for (circ=global_circuitlist;circ;circ = circ->next) {
|
2004-05-13 09:24:49 +02:00
|
|
|
if (circ->marked_for_close)
|
|
|
|
continue;
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (circ->p_conn == conn)
|
2004-05-13 09:24:49 +02:00
|
|
|
return circ;
|
2004-11-28 10:05:49 +01:00
|
|
|
if (circ->n_conn == conn)
|
2004-05-13 09:24:49 +02:00
|
|
|
return circ;
|
2004-11-28 10:05:49 +01:00
|
|
|
for (tmpconn = circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream)
|
|
|
|
if (tmpconn == conn)
|
2004-05-13 09:24:49 +02:00
|
|
|
return circ;
|
2004-11-28 10:05:49 +01:00
|
|
|
for (tmpconn = circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream)
|
|
|
|
if (tmpconn == conn)
|
2004-05-13 09:24:49 +02:00
|
|
|
return circ;
|
2004-11-28 10:05:49 +01:00
|
|
|
for (tmpconn = circ->resolving_streams; tmpconn; tmpconn=tmpconn->next_stream)
|
|
|
|
if (tmpconn == conn)
|
2004-05-13 09:24:49 +02:00
|
|
|
return circ;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** 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.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
circuit_t *
|
|
|
|
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;
|
|
|
|
|
|
|
|
for (circ = global_circuitlist; circ; circ = circ->next) {
|
|
|
|
if (!circ->marked_for_close &&
|
|
|
|
circ->purpose == purpose &&
|
|
|
|
!rend_cmp_service_ids(rend_query, circ->rend_query))
|
|
|
|
return circ;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return the first circuit in global_circuitlist after <b>start</b> whose
|
|
|
|
* rend_pk_digest field is <b>digest</b> and whose purpose is <b>purpose</b>. Returns
|
|
|
|
* NULL if no circuit is found. If <b>start</b> is NULL, begin at the start of
|
|
|
|
* the list.
|
|
|
|
*/
|
|
|
|
circuit_t *
|
|
|
|
circuit_get_next_by_pk_and_purpose(circuit_t *start,
|
|
|
|
const char *digest, uint8_t purpose)
|
|
|
|
{
|
|
|
|
circuit_t *circ;
|
|
|
|
if (start == NULL)
|
|
|
|
circ = global_circuitlist;
|
|
|
|
else
|
|
|
|
circ = start->next;
|
|
|
|
|
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;
|
|
|
|
if (!memcmp(circ->rend_pk_digest, digest, DIGEST_LEN))
|
|
|
|
return circ;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return the circuit waiting for a rendezvous with the provided cookie.
|
|
|
|
* Return NULL if no such circuit is found.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
circuit_t *
|
|
|
|
circuit_get_rendezvous(const char *cookie)
|
2004-05-13 09:24:49 +02:00
|
|
|
{
|
|
|
|
circuit_t *circ;
|
|
|
|
for (circ = global_circuitlist; circ; circ = circ->next) {
|
|
|
|
if (! circ->marked_for_close &&
|
|
|
|
circ->purpose == CIRCUIT_PURPOSE_REND_POINT_WAITING &&
|
|
|
|
! memcmp(circ->rend_cookie, cookie, REND_COOKIE_LEN) )
|
|
|
|
return circ;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
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>,
|
|
|
|
* has a timestamp_dirty value of 0, and is uptime/capacity/internal
|
|
|
|
* if required; or NULL if no circuit fits this description.
|
2005-01-20 00:13:20 +01:00
|
|
|
*
|
|
|
|
* Avoid returning need_uptime circuits if not necessary.
|
|
|
|
* FFFF As a more important goal, not yet implemented, avoid returning
|
|
|
|
* internal circuits if not necessary.
|
2004-05-13 09:24:49 +02:00
|
|
|
*/
|
|
|
|
circuit_t *
|
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
|
|
|
circuit_get_clean_open(uint8_t purpose, int need_uptime,
|
2005-06-11 20:52:12 +02:00
|
|
|
int need_capacity, int internal)
|
|
|
|
{
|
2004-05-13 09:24:49 +02:00
|
|
|
circuit_t *circ;
|
2005-01-20 00:13:20 +01:00
|
|
|
circuit_t *best=NULL;
|
2004-05-13 09:24:49 +02:00
|
|
|
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
log_fn(LOG_DEBUG,"Hunting for a circ to cannibalize: purpose %d, uptime %d, capacity %d, internal %d", purpose, need_uptime, need_capacity, internal);
|
|
|
|
|
|
|
|
for (circ=global_circuitlist; circ; circ = circ->next) {
|
2004-11-28 12:39:53 +01:00
|
|
|
if (CIRCUIT_IS_ORIGIN(circ) &&
|
|
|
|
circ->state == CIRCUIT_STATE_OPEN &&
|
|
|
|
!circ->marked_for_close &&
|
|
|
|
circ->purpose == purpose &&
|
|
|
|
!circ->timestamp_dirty &&
|
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
|
|
|
(!need_uptime || circ->build_state->need_uptime) &&
|
|
|
|
(!need_capacity || circ->build_state->need_capacity) &&
|
2005-01-20 00:13:20 +01:00
|
|
|
(!internal || circ->build_state->is_internal)) {
|
|
|
|
if (!best || (best->build_state->need_uptime && !need_uptime))
|
|
|
|
best = circ;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
circuit_mark_for_close(circ);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
_circuit_mark_for_close(circuit_t *circ, int line, const char *file)
|
2005-04-03 07:22:33 +02:00
|
|
|
{
|
2004-05-13 09:24:49 +02:00
|
|
|
connection_t *conn;
|
|
|
|
|
|
|
|
assert_circuit_ok(circ);
|
2005-04-03 07:22:33 +02:00
|
|
|
tor_assert(line);
|
|
|
|
tor_assert(file);
|
|
|
|
|
|
|
|
if (circ->marked_for_close) {
|
|
|
|
log(LOG_WARN,"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;
|
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
|
2004-05-13 09:24:49 +02:00
|
|
|
onion_pending_remove(circ);
|
|
|
|
}
|
|
|
|
/* 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)) {
|
2004-05-13 09:24:49 +02:00
|
|
|
circuit_build_failed(circ); /* take actions if necessary */
|
2004-11-03 19:33:07 +01:00
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
circuit_rep_hist_note_result(circ);
|
|
|
|
}
|
2004-11-03 19:33:07 +01:00
|
|
|
if (CIRCUIT_IS_ORIGIN(circ)) {
|
|
|
|
control_event_circuit_status(circ,
|
|
|
|
(circ->state == CIRCUIT_STATE_OPEN)?CIRC_EVENT_CLOSED:CIRC_EVENT_FAILED);
|
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
|
|
|
|
tor_assert(circ->state == CIRCUIT_STATE_OPEN);
|
2005-06-29 23:46:55 +02:00
|
|
|
tor_assert(circ->build_state->chosen_exit);
|
2004-05-13 09:24:49 +02:00
|
|
|
/* treat this like getting a nack from it */
|
|
|
|
log_fn(LOG_INFO,"Failed intro circ %s to %s (awaiting ack). Removing from descriptor.",
|
2005-06-29 23:46:55 +02:00
|
|
|
safe_str(circ->rend_query),
|
|
|
|
safe_str(build_state_get_exit_nickname(circ->build_state)));
|
|
|
|
// XXXX NM
|
|
|
|
rend_client_remove_intro_point(circ->build_state->chosen_exit,
|
|
|
|
circ->rend_query);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (circ->n_conn)
|
2004-05-13 09:24:49 +02:00
|
|
|
connection_send_destroy(circ->n_circ_id, circ->n_conn);
|
2004-11-28 10:05:49 +01:00
|
|
|
for (conn=circ->n_streams; conn; conn=conn->next_stream)
|
2004-05-13 09:24:49 +02:00
|
|
|
connection_edge_destroy(circ->n_circ_id, conn);
|
2004-11-28 10:05:49 +01:00
|
|
|
while (circ->resolving_streams) {
|
2004-05-13 09:24:49 +02:00
|
|
|
conn = circ->resolving_streams;
|
|
|
|
circ->resolving_streams = conn->next_stream;
|
2005-03-02 04:13:05 +01:00
|
|
|
if (!conn->marked_for_close) {
|
|
|
|
/* The other side will see a DESTROY, and infer that the connections
|
|
|
|
* are closing because the circuit is getting torn down. No need
|
|
|
|
* to send an end cell*/
|
2005-03-29 02:35:35 +02:00
|
|
|
conn->has_sent_end = 1; /* we're closing the circuit, nothing to send to */
|
2005-01-31 04:47:38 +01:00
|
|
|
connection_mark_for_close(conn);
|
2005-03-02 04:13:05 +01:00
|
|
|
}
|
2005-04-06 08:13:49 +02:00
|
|
|
conn->on_circuit = NULL;
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
2004-11-28 10:05:49 +01:00
|
|
|
if (circ->p_conn)
|
2004-05-13 09:24:49 +02:00
|
|
|
connection_send_destroy(circ->p_circ_id, circ->p_conn);
|
2004-11-28 10:05:49 +01:00
|
|
|
for (conn=circ->p_streams; conn; conn=conn->next_stream)
|
2004-05-13 09:24:49 +02:00
|
|
|
connection_edge_destroy(circ->p_circ_id, conn);
|
|
|
|
|
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
|
|
|
|
|
|
|
if (circ->rend_splice && !circ->rend_splice->marked_for_close) {
|
|
|
|
/* do this after marking this circuit, to avoid infinite recursion. */
|
|
|
|
circuit_mark_for_close(circ->rend_splice);
|
|
|
|
circ->rend_splice = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** 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:
|
2004-10-17 00:56:46 +02:00
|
|
|
log_fn(LOG_ERR,"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
|
|
|
{
|
|
|
|
connection_t *conn;
|
|
|
|
|
|
|
|
tor_assert(c);
|
|
|
|
tor_assert(c->magic == CIRCUIT_MAGIC);
|
|
|
|
tor_assert(c->purpose >= _CIRCUIT_PURPOSE_MIN &&
|
|
|
|
c->purpose <= _CIRCUIT_PURPOSE_MAX);
|
|
|
|
|
2004-07-03 03:45:13 +02:00
|
|
|
if (c->n_conn) {
|
2004-05-13 09:24:49 +02:00
|
|
|
tor_assert(c->n_conn->type == CONN_TYPE_OR);
|
2004-07-02 11:29:01 +02: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
|
|
|
}
|
2005-04-06 07:33:32 +02:00
|
|
|
if (c->p_conn) {
|
2004-05-13 09:24:49 +02:00
|
|
|
tor_assert(c->p_conn->type == CONN_TYPE_OR);
|
2005-04-06 07:33:32 +02:00
|
|
|
if (c->p_circ_id)
|
|
|
|
tor_assert(c == circuit_get_by_circid_orconn(c->p_circ_id, c->p_conn));
|
|
|
|
}
|
2004-05-13 09:24:49 +02:00
|
|
|
for (conn = c->p_streams; conn; conn = conn->next_stream)
|
|
|
|
tor_assert(conn->type == CONN_TYPE_AP);
|
|
|
|
for (conn = c->n_streams; conn; conn = conn->next_stream)
|
|
|
|
tor_assert(conn->type == CONN_TYPE_EXIT);
|
|
|
|
|
|
|
|
tor_assert(c->deliver_window >= 0);
|
|
|
|
tor_assert(c->package_window >= 0);
|
|
|
|
if (c->state == CIRCUIT_STATE_OPEN) {
|
|
|
|
if (c->cpath) {
|
|
|
|
tor_assert(CIRCUIT_IS_ORIGIN(c));
|
|
|
|
tor_assert(!c->n_crypto);
|
|
|
|
tor_assert(!c->p_crypto);
|
|
|
|
tor_assert(!c->n_digest);
|
|
|
|
tor_assert(!c->p_digest);
|
|
|
|
} else {
|
|
|
|
tor_assert(!CIRCUIT_IS_ORIGIN(c));
|
|
|
|
tor_assert(c->n_crypto);
|
|
|
|
tor_assert(c->p_crypto);
|
|
|
|
tor_assert(c->n_digest);
|
|
|
|
tor_assert(c->p_digest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (c->cpath) {
|
2004-05-18 17:35:21 +02:00
|
|
|
assert_cpath_ok(c->cpath);
|
2004-05-13 09:24:49 +02:00
|
|
|
}
|
|
|
|
if (c->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED) {
|
|
|
|
if (!c->marked_for_close) {
|
|
|
|
tor_assert(c->rend_splice);
|
|
|
|
tor_assert(c->rend_splice->rend_splice == c);
|
|
|
|
}
|
|
|
|
tor_assert(c->rend_splice != c);
|
|
|
|
} else {
|
|
|
|
tor_assert(!c->rend_splice);
|
|
|
|
}
|
|
|
|
}
|
2005-06-09 21:03:31 +02:00
|
|
|
|