2004-05-09 18:33:04 +02:00
|
|
|
/* Copyright 2001 Matej Pfajfar, 2001-2004 Roger Dingledine. */
|
Implemented link padding and receiver token buckets
Each socket reads at most 'bandwidth' bytes per second sustained, but
can handle bursts of up to 10*bandwidth bytes.
Cells are now sent out at evenly-spaced intervals, with padding sent
out otherwise. Set Linkpadding=0 in the rc file to send cells as soon
as they're available (and to never send padding cells).
Added license/copyrights statements at the top of most files.
router->min and router->max have been merged into a single 'bandwidth'
value. We should make the routerinfo_t reflect this (want to do that,
Mat?)
As the bandwidth increases, and we want to stop sleeping more and more
frequently to send a single cell, cpu usage goes up. At 128kB/s we're
pretty much calling poll with a timeout of 1ms or even 0ms. The current
code takes a timeout of 0-9ms and makes it 10ms. prepare_for_poll()
handles everything that should have happened in the past, so as long as
our buffers don't get too full in that 10ms, we're ok.
Speaking of too full, if you run three servers at 100kB/s with -l debug,
it spends too much time printing debugging messages to be able to keep
up with the cells. The outbuf ultimately fills up and it kills that
connection. If you run with -l err, it works fine up through 500kB/s and
probably beyond. Down the road we'll want to teach it to recognize when
an outbuf is getting full, and back off.
svn:r50
2002-07-16 03:12:15 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
/* $Id$ */
|
2002-06-27 00:45:49 +02:00
|
|
|
|
|
|
|
#include "or.h"
|
|
|
|
|
2003-04-16 08:18:31 +02:00
|
|
|
extern or_options_t options; /* command-line and config-file options */
|
|
|
|
|
2004-03-09 23:01:17 +01:00
|
|
|
static int relay_crypt(circuit_t *circ, cell_t *cell, int cell_direction,
|
2003-12-19 06:09:51 +01:00
|
|
|
crypt_path_t **layer_hint, char *recognized);
|
|
|
|
static connection_t *relay_lookup_conn(circuit_t *circ, cell_t *cell, int cell_direction);
|
2004-04-08 11:41:28 +02:00
|
|
|
static int circuit_resume_edge_reading_helper(connection_t *conn,
|
|
|
|
circuit_t *circ,
|
|
|
|
crypt_path_t *layer_hint);
|
2003-09-16 07:41:49 +02:00
|
|
|
static void circuit_free_cpath_node(crypt_path_t *victim);
|
2003-12-19 20:55:02 +01:00
|
|
|
static uint16_t get_unique_circ_id_by_conn(connection_t *conn, int circ_id_type);
|
2004-03-20 05:59:29 +01:00
|
|
|
static void circuit_rep_hist_note_result(circuit_t *circ);
|
2003-09-16 07:41:49 +02:00
|
|
|
|
2004-04-13 07:20:52 +02:00
|
|
|
void circuit_expire_old_circuits(void);
|
2004-04-05 02:47:48 +02:00
|
|
|
static void circuit_is_open(circuit_t *circ);
|
2004-04-07 22:59:38 +02:00
|
|
|
static void circuit_build_failed(circuit_t *circ);
|
2004-04-01 05:44:49 +02:00
|
|
|
static circuit_t *circuit_establish_circuit(uint8_t purpose, const char *exit_nickname);
|
2004-04-01 03:57:22 +02:00
|
|
|
|
2003-10-02 22:00:38 +02:00
|
|
|
unsigned long stats_n_relay_cells_relayed = 0;
|
|
|
|
unsigned long stats_n_relay_cells_delivered = 0;
|
|
|
|
|
2002-06-27 00:45:49 +02:00
|
|
|
/********* START VARIABLES **********/
|
|
|
|
|
2004-03-21 07:33:57 +01:00
|
|
|
static int circuitlist_len=0;
|
2002-07-05 08:27:23 +02:00
|
|
|
static circuit_t *global_circuitlist=NULL;
|
2002-09-22 00:41:48 +02:00
|
|
|
char *circuit_state_to_string[] = {
|
2003-11-17 02:23:15 +01:00
|
|
|
"doing handshakes", /* 0 */
|
|
|
|
"processing the onion", /* 1 */
|
|
|
|
"connecting to firsthop", /* 2 */
|
|
|
|
"open" /* 3 */
|
2002-09-22 00:41:48 +02:00
|
|
|
};
|
|
|
|
|
2002-06-27 00:45:49 +02:00
|
|
|
/********* END VARIABLES ************/
|
|
|
|
|
2004-05-09 18:33:04 +02:00
|
|
|
/* add 'circ' to the global list of circuits. This is called only from
|
|
|
|
* within circuit_new.
|
|
|
|
*/
|
|
|
|
static void circuit_add(circuit_t *circ) {
|
2002-06-27 00:45:49 +02:00
|
|
|
if(!global_circuitlist) { /* first one */
|
|
|
|
global_circuitlist = circ;
|
|
|
|
circ->next = NULL;
|
|
|
|
} else {
|
|
|
|
circ->next = global_circuitlist;
|
|
|
|
global_circuitlist = circ;
|
|
|
|
}
|
2004-03-21 07:33:57 +01:00
|
|
|
++circuitlist_len;
|
2002-06-27 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void circuit_remove(circuit_t *circ) {
|
|
|
|
circuit_t *tmpcirc;
|
|
|
|
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(circ && global_circuitlist);
|
2002-06-27 00:45:49 +02:00
|
|
|
|
|
|
|
if(global_circuitlist == circ) {
|
|
|
|
global_circuitlist = global_circuitlist->next;
|
2004-03-21 07:33:57 +01:00
|
|
|
--circuitlist_len;
|
2002-06-27 00:45:49 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(tmpcirc = global_circuitlist;tmpcirc->next;tmpcirc = tmpcirc->next) {
|
|
|
|
if(tmpcirc->next == circ) {
|
|
|
|
tmpcirc->next = circ->next;
|
2004-03-21 07:33:57 +01:00
|
|
|
--circuitlist_len;
|
2002-06-27 00:45:49 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-02 18:48:17 +01:00
|
|
|
void circuit_close_all_marked()
|
|
|
|
{
|
|
|
|
circuit_t *tmp,*m;
|
2004-03-21 07:33:57 +01:00
|
|
|
|
2004-03-02 18:48:17 +01:00
|
|
|
while (global_circuitlist && global_circuitlist->marked_for_close) {
|
|
|
|
tmp = global_circuitlist->next;
|
|
|
|
circuit_free(global_circuitlist);
|
|
|
|
global_circuitlist = tmp;
|
|
|
|
}
|
|
|
|
|
2004-03-03 02:37:54 +01:00
|
|
|
tmp = global_circuitlist;
|
|
|
|
while (tmp && tmp->next) {
|
|
|
|
if (tmp->next->marked_for_close) {
|
2004-03-02 18:48:17 +01:00
|
|
|
m = tmp->next->next;
|
|
|
|
circuit_free(tmp->next);
|
|
|
|
tmp->next = m;
|
2004-03-03 02:37:54 +01:00
|
|
|
/* Need to check new tmp->next; don't advance tmp. */
|
|
|
|
} else {
|
|
|
|
/* Advance tmp. */
|
|
|
|
tmp = tmp->next;
|
2004-03-02 18:48:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-19 20:55:02 +01:00
|
|
|
circuit_t *circuit_new(uint16_t p_circ_id, connection_t *p_conn) {
|
2003-12-17 22:09:31 +01:00
|
|
|
circuit_t *circ;
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2003-11-18 09:20:19 +01:00
|
|
|
circ = tor_malloc_zero(sizeof(circuit_t));
|
2004-02-25 08:31:46 +01:00
|
|
|
circ->magic = CIRCUIT_MAGIC;
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2003-10-04 05:29:09 +02:00
|
|
|
circ->timestamp_created = time(NULL);
|
2003-04-16 08:18:31 +02:00
|
|
|
|
2003-11-11 04:01:48 +01:00
|
|
|
circ->p_circ_id = p_circ_id;
|
2002-06-27 00:45:49 +02:00
|
|
|
circ->p_conn = p_conn;
|
|
|
|
|
2003-05-06 01:24:46 +02:00
|
|
|
circ->state = CIRCUIT_STATE_ONIONSKIN_PENDING;
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2003-11-11 04:01:48 +01:00
|
|
|
/* CircIDs */
|
|
|
|
circ->p_circ_id = p_circ_id;
|
|
|
|
/* circ->n_circ_id remains 0 because we haven't identified the next hop yet */
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2003-05-20 08:41:23 +02:00
|
|
|
circ->package_window = CIRCWINDOW_START;
|
|
|
|
circ->deliver_window = CIRCWINDOW_START;
|
2002-07-18 08:37:58 +02:00
|
|
|
|
2003-12-28 05:46:09 +01:00
|
|
|
circ->next_stream_id = crypto_pseudo_rand_int(1<<16);
|
|
|
|
|
2002-06-27 00:45:49 +02:00
|
|
|
circuit_add(circ);
|
|
|
|
|
|
|
|
return circ;
|
|
|
|
}
|
|
|
|
|
|
|
|
void circuit_free(circuit_t *circ) {
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(circ);
|
|
|
|
tor_assert(circ->magic == CIRCUIT_MAGIC);
|
2002-08-22 09:30:03 +02:00
|
|
|
if (circ->n_crypto)
|
|
|
|
crypto_free_cipher_env(circ->n_crypto);
|
|
|
|
if (circ->p_crypto)
|
|
|
|
crypto_free_cipher_env(circ->p_crypto);
|
2003-12-17 06:58:30 +01:00
|
|
|
if (circ->n_digest)
|
|
|
|
crypto_free_digest_env(circ->n_digest);
|
|
|
|
if (circ->p_digest)
|
|
|
|
crypto_free_digest_env(circ->p_digest);
|
2004-04-01 22:05:57 +02:00
|
|
|
if(circ->build_state) {
|
2003-11-16 18:00:02 +01:00
|
|
|
tor_free(circ->build_state->chosen_exit);
|
2004-04-02 00:21:01 +02:00
|
|
|
if (circ->build_state->pending_final_cpath)
|
|
|
|
circuit_free_cpath_node(circ->build_state->pending_final_cpath);
|
2004-04-01 22:05:57 +02:00
|
|
|
}
|
2003-11-14 21:45:47 +01:00
|
|
|
tor_free(circ->build_state);
|
2003-05-02 00:55:51 +02:00
|
|
|
circuit_free_cpath(circ->cpath);
|
2004-03-30 21:52:42 +02:00
|
|
|
if (circ->rend_splice) {
|
|
|
|
circ->rend_splice->rend_splice = NULL;
|
|
|
|
}
|
|
|
|
|
2004-02-25 08:31:46 +01:00
|
|
|
memset(circ, 0xAA, sizeof(circuit_t)); /* poison memory */
|
2002-06-27 00:45:49 +02:00
|
|
|
free(circ);
|
Integrated onion proxy into or/
The 'or' process can now be told (by the global_role variable) what
roles this server should play -- connect to all ORs, listen for ORs,
listen for OPs, listen for APs, or any combination.
* everything in /src/op/ is now obsolete.
* connection_ap.c now handles all interactions with application proxies
* "port" is now or_port, op_port, ap_port. But routers are still always
referenced (say, in conn_get_by_addr_port()) by addr / or_port. We
should make routers.c actually read these new ports (currently I've
kludged it so op_port = or_port+10, ap_port=or_port+20)
* circuits currently know if they're at the beginning of the path because
circ->cpath is set. They use this instead for crypts (both ways),
if it's set.
* I still obey the "send a 0 back to the AP when you're ready" protocol,
but I think we should phase it out. I can simply not read from the AP
socket until I'm ready.
I need to do a lot of cleanup work here, but the code appears to work, so
now's a good time for a checkin.
svn:r22
2002-07-02 11:36:58 +02:00
|
|
|
}
|
|
|
|
|
2003-11-12 03:32:20 +01:00
|
|
|
void circuit_free_cpath(crypt_path_t *cpath) {
|
2003-05-02 00:55:51 +02:00
|
|
|
crypt_path_t *victim, *head=cpath;
|
Integrated onion proxy into or/
The 'or' process can now be told (by the global_role variable) what
roles this server should play -- connect to all ORs, listen for ORs,
listen for OPs, listen for APs, or any combination.
* everything in /src/op/ is now obsolete.
* connection_ap.c now handles all interactions with application proxies
* "port" is now or_port, op_port, ap_port. But routers are still always
referenced (say, in conn_get_by_addr_port()) by addr / or_port. We
should make routers.c actually read these new ports (currently I've
kludged it so op_port = or_port+10, ap_port=or_port+20)
* circuits currently know if they're at the beginning of the path because
circ->cpath is set. They use this instead for crypts (both ways),
if it's set.
* I still obey the "send a 0 back to the AP when you're ready" protocol,
but I think we should phase it out. I can simply not read from the AP
socket until I'm ready.
I need to do a lot of cleanup work here, but the code appears to work, so
now's a good time for a checkin.
svn:r22
2002-07-02 11:36:58 +02:00
|
|
|
|
2003-05-02 00:55:51 +02:00
|
|
|
if(!cpath)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* it's a doubly linked list, so we have to notice when we've
|
|
|
|
* gone through it once. */
|
|
|
|
while(cpath->next && cpath->next != head) {
|
|
|
|
victim = cpath;
|
|
|
|
cpath = victim->next;
|
|
|
|
circuit_free_cpath_node(victim);
|
|
|
|
}
|
|
|
|
|
|
|
|
circuit_free_cpath_node(cpath);
|
|
|
|
}
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2003-09-16 07:41:49 +02:00
|
|
|
static void circuit_free_cpath_node(crypt_path_t *victim) {
|
2003-05-02 00:55:51 +02:00
|
|
|
if(victim->f_crypto)
|
|
|
|
crypto_free_cipher_env(victim->f_crypto);
|
|
|
|
if(victim->b_crypto)
|
|
|
|
crypto_free_cipher_env(victim->b_crypto);
|
2003-12-16 09:21:58 +01:00
|
|
|
if(victim->f_digest)
|
|
|
|
crypto_free_digest_env(victim->f_digest);
|
|
|
|
if(victim->b_digest)
|
|
|
|
crypto_free_digest_env(victim->b_digest);
|
2003-05-06 01:24:46 +02:00
|
|
|
if(victim->handshake_state)
|
|
|
|
crypto_dh_free(victim->handshake_state);
|
2003-05-02 00:55:51 +02:00
|
|
|
free(victim);
|
2002-06-27 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
2003-11-11 04:01:48 +01:00
|
|
|
/* return 0 if can't get a unique circ_id. */
|
2003-12-19 20:55:02 +01:00
|
|
|
static uint16_t get_unique_circ_id_by_conn(connection_t *conn, int circ_id_type) {
|
|
|
|
uint16_t test_circ_id;
|
2003-12-16 21:45:10 +01:00
|
|
|
int attempts=0;
|
2003-09-16 22:13:43 +02:00
|
|
|
uint16_t high_bit;
|
2003-09-26 12:03:50 +02:00
|
|
|
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(conn && conn->type == CONN_TYPE_OR);
|
2003-11-11 04:01:48 +01:00
|
|
|
high_bit = (circ_id_type == CIRC_ID_TYPE_HIGHER) ? 1<<15 : 0;
|
2003-09-16 19:17:39 +02:00
|
|
|
do {
|
2003-12-16 21:45:10 +01:00
|
|
|
/* Sequentially iterate over test_circ_id=1...1<<15-1 until we find a
|
2003-11-11 04:01:48 +01:00
|
|
|
* circID such that (high_bit|test_circ_id) is not already used. */
|
|
|
|
test_circ_id = conn->next_circ_id++;
|
|
|
|
if (test_circ_id == 0 || test_circ_id >= 1<<15) {
|
|
|
|
test_circ_id = 1;
|
|
|
|
conn->next_circ_id = 2;
|
2003-09-16 22:13:43 +02:00
|
|
|
}
|
2003-12-16 21:45:10 +01:00
|
|
|
if(++attempts > 1<<15) {
|
|
|
|
/* Make sure we don't loop forever if all circ_id's are used. This
|
|
|
|
* matters because it's an external DoS vulnerability.
|
|
|
|
*/
|
|
|
|
log_fn(LOG_WARN,"No unused circ IDs. Failing.");
|
|
|
|
return 0;
|
|
|
|
}
|
2003-11-11 04:01:48 +01:00
|
|
|
test_circ_id |= high_bit;
|
|
|
|
} while(circuit_get_by_circ_id_conn(test_circ_id, conn));
|
|
|
|
return test_circ_id;
|
2002-06-27 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
2003-12-19 20:55:02 +01:00
|
|
|
circuit_t *circuit_get_by_circ_id_conn(uint16_t circ_id, connection_t *conn) {
|
2002-06-27 00:45:49 +02:00
|
|
|
circuit_t *circ;
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
connection_t *tmpconn;
|
2002-06-27 00:45:49 +02:00
|
|
|
|
|
|
|
for(circ=global_circuitlist;circ;circ = circ->next) {
|
2004-03-02 18:48:17 +01:00
|
|
|
if (circ->marked_for_close)
|
|
|
|
continue;
|
|
|
|
|
2003-11-11 04:01:48 +01:00
|
|
|
if(circ->p_circ_id == circ_id) {
|
2003-05-28 01:39:04 +02:00
|
|
|
if(circ->p_conn == conn)
|
|
|
|
return circ;
|
|
|
|
for(tmpconn = circ->p_streams; tmpconn; tmpconn = tmpconn->next_stream) {
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
if(tmpconn == conn)
|
|
|
|
return circ;
|
|
|
|
}
|
|
|
|
}
|
2003-11-11 04:01:48 +01:00
|
|
|
if(circ->n_circ_id == circ_id) {
|
2003-05-28 01:39:04 +02:00
|
|
|
if(circ->n_conn == conn)
|
|
|
|
return circ;
|
|
|
|
for(tmpconn = circ->n_streams; tmpconn; tmpconn = tmpconn->next_stream) {
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
if(tmpconn == conn)
|
|
|
|
return circ;
|
|
|
|
}
|
2004-05-06 13:08:04 +02:00
|
|
|
for(tmpconn = circ->resolving_streams; tmpconn; tmpconn = tmpconn->next_stream) {
|
|
|
|
if(tmpconn == conn)
|
|
|
|
return circ;
|
|
|
|
}
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
}
|
2002-06-27 00:45:49 +02:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
circuit_t *circuit_get_by_conn(connection_t *conn) {
|
|
|
|
circuit_t *circ;
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
connection_t *tmpconn;
|
2002-06-27 00:45:49 +02:00
|
|
|
|
|
|
|
for(circ=global_circuitlist;circ;circ = circ->next) {
|
2004-03-02 18:48:17 +01:00
|
|
|
if (circ->marked_for_close)
|
|
|
|
continue;
|
|
|
|
|
2003-05-28 01:39:04 +02:00
|
|
|
if(circ->p_conn == conn)
|
|
|
|
return circ;
|
|
|
|
if(circ->n_conn == conn)
|
|
|
|
return circ;
|
|
|
|
for(tmpconn = circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream)
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
if(tmpconn == conn)
|
|
|
|
return circ;
|
2003-05-28 01:39:04 +02:00
|
|
|
for(tmpconn = circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream)
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
if(tmpconn == conn)
|
|
|
|
return circ;
|
2004-05-06 13:08:04 +02:00
|
|
|
for(tmpconn = circ->resolving_streams; tmpconn; tmpconn=tmpconn->next_stream)
|
|
|
|
if(tmpconn == conn)
|
|
|
|
return circ;
|
2002-06-27 00:45:49 +02:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-04-08 03:55:21 +02:00
|
|
|
/* Return 1 iff 'c' could be returned by circuit_get_best.
|
|
|
|
*/
|
|
|
|
static int circuit_is_acceptable(circuit_t *circ,
|
|
|
|
connection_t *conn,
|
|
|
|
int must_be_open,
|
|
|
|
uint8_t purpose,
|
|
|
|
time_t now)
|
|
|
|
{
|
|
|
|
routerinfo_t *exitrouter;
|
|
|
|
|
2004-04-08 04:24:06 +02:00
|
|
|
if (!CIRCUIT_IS_ORIGIN(circ))
|
2004-04-08 03:55:21 +02:00
|
|
|
return 0; /* this circ doesn't start at us */
|
|
|
|
if (must_be_open && (circ->state != CIRCUIT_STATE_OPEN || !circ->n_conn))
|
|
|
|
return 0; /* ignore non-open circs */
|
|
|
|
if (circ->marked_for_close)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* if this circ isn't our purpose, skip. */
|
|
|
|
if(purpose == CIRCUIT_PURPOSE_C_REND_JOINED && !must_be_open) {
|
|
|
|
if(circ->purpose != CIRCUIT_PURPOSE_C_ESTABLISH_REND &&
|
|
|
|
circ->purpose != CIRCUIT_PURPOSE_C_REND_READY &&
|
2004-04-14 00:56:24 +02:00
|
|
|
circ->purpose != CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED &&
|
2004-04-08 03:55:21 +02:00
|
|
|
circ->purpose != CIRCUIT_PURPOSE_C_REND_JOINED)
|
|
|
|
return 0;
|
2004-04-14 00:56:24 +02:00
|
|
|
} else if (purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT && !must_be_open) {
|
2004-04-13 03:41:39 +02:00
|
|
|
if (circ->purpose != CIRCUIT_PURPOSE_C_INTRODUCING &&
|
|
|
|
circ->purpose != CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT)
|
|
|
|
return 0;
|
2004-04-08 03:55:21 +02:00
|
|
|
} else {
|
|
|
|
if(purpose != circ->purpose)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(purpose == CIRCUIT_PURPOSE_C_GENERAL)
|
|
|
|
if(circ->timestamp_dirty &&
|
|
|
|
circ->timestamp_dirty+options.NewCircuitPeriod < now)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if(conn) {
|
|
|
|
/* decide if this circ is suitable for this conn */
|
|
|
|
|
|
|
|
/* for rend circs, circ->cpath->prev is not the last router in the
|
|
|
|
* circuit, it's the magical extra bob hop. so just check the nickname
|
|
|
|
* of the one we meant to finish at.
|
|
|
|
*/
|
|
|
|
exitrouter = router_get_by_nickname(circ->build_state->chosen_exit);
|
|
|
|
|
|
|
|
if(!exitrouter) {
|
|
|
|
log_fn(LOG_INFO,"Skipping broken circ (exit router vanished)");
|
|
|
|
return 0; /* this circuit is screwed and doesn't know it yet */
|
|
|
|
}
|
|
|
|
|
|
|
|
if(purpose == CIRCUIT_PURPOSE_C_GENERAL) {
|
|
|
|
if(connection_ap_can_use_exit(conn, exitrouter) == ADDR_POLICY_REJECTED) {
|
|
|
|
/* can't exit from this router */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else { /* not general */
|
2004-04-14 00:56:24 +02:00
|
|
|
if(rend_cmp_service_ids(conn->rend_query, circ->rend_query) &&
|
|
|
|
(circ->rend_query[0] || purpose != CIRCUIT_PURPOSE_C_REND_JOINED)) {
|
|
|
|
/* this circ is not for this conn, and it's not suitable
|
|
|
|
* for cannibalizing either */
|
2004-04-08 03:55:21 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-04-08 04:50:34 +02:00
|
|
|
return 1;
|
2004-04-08 03:55:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return 1 iff circuit 'a' is better than circuit 'b' for purpose. Used by
|
|
|
|
* circuit_get_best
|
|
|
|
*/
|
|
|
|
static int circuit_is_better(circuit_t *a, circuit_t *b, uint8_t purpose)
|
|
|
|
{
|
|
|
|
switch(purpose) {
|
|
|
|
case CIRCUIT_PURPOSE_C_GENERAL:
|
|
|
|
/* if it's used but less dirty it's best;
|
|
|
|
* else if it's more recently created it's best
|
|
|
|
*/
|
|
|
|
if(b->timestamp_dirty) {
|
|
|
|
if(a->timestamp_dirty &&
|
|
|
|
a->timestamp_dirty > b->timestamp_dirty)
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
if(a->timestamp_dirty ||
|
|
|
|
a->timestamp_created > b->timestamp_created)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
2004-04-14 00:56:24 +02:00
|
|
|
case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
|
|
|
|
/* the closer it is to ack_wait the better it is */
|
|
|
|
if(a->purpose > b->purpose)
|
2004-04-08 03:55:21 +02:00
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
case CIRCUIT_PURPOSE_C_REND_JOINED:
|
|
|
|
/* the closer it is to rend_joined the better it is */
|
|
|
|
if(a->purpose > b->purpose)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-04-05 02:47:48 +02:00
|
|
|
/* Find the best circ that conn can use, preferably one which is
|
2003-12-16 23:56:50 +01:00
|
|
|
* dirty. Circ must not be too old.
|
2004-04-14 00:56:24 +02:00
|
|
|
* conn must be defined.
|
2003-11-17 01:57:56 +01:00
|
|
|
*
|
|
|
|
* If must_be_open, ignore circs not in CIRCUIT_STATE_OPEN.
|
2004-04-03 00:23:15 +02:00
|
|
|
*
|
|
|
|
* circ_purpose specifies what sort of circuit we must have.
|
2004-04-14 00:56:24 +02:00
|
|
|
* It can be C_GENERAL, C_INTRODUCE_ACK_WAIT, or C_REND_JOINED.
|
2004-04-05 02:47:48 +02:00
|
|
|
*
|
|
|
|
* If it's REND_JOINED and must_be_open==0, then return the closest
|
|
|
|
* rendezvous-purposed circuit that you can find.
|
|
|
|
*
|
2004-04-14 00:56:24 +02:00
|
|
|
* If it's INTRODUCE_ACK_WAIT and must_be_open==0, then return the
|
|
|
|
* closest introduce-purposed circuit that you can find.
|
2003-11-16 22:49:52 +01:00
|
|
|
*/
|
2004-04-05 02:47:48 +02:00
|
|
|
circuit_t *circuit_get_best(connection_t *conn,
|
|
|
|
int must_be_open, uint8_t purpose) {
|
|
|
|
circuit_t *circ, *best=NULL;
|
|
|
|
time_t now = time(NULL);
|
|
|
|
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(conn);
|
2004-04-14 00:56:24 +02:00
|
|
|
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(purpose == CIRCUIT_PURPOSE_C_GENERAL ||
|
|
|
|
purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT ||
|
|
|
|
purpose == CIRCUIT_PURPOSE_C_REND_JOINED);
|
2003-02-06 09:00:49 +01:00
|
|
|
|
2004-04-01 01:06:16 +02:00
|
|
|
for (circ=global_circuitlist;circ;circ = circ->next) {
|
2004-04-08 03:55:21 +02:00
|
|
|
if (!circuit_is_acceptable(circ,conn,must_be_open,purpose,now))
|
2004-03-02 18:48:17 +01:00
|
|
|
continue;
|
2004-04-03 00:23:15 +02:00
|
|
|
|
2004-04-05 02:47:48 +02:00
|
|
|
/* now this is an acceptable circ to hand back. but that doesn't
|
|
|
|
* mean it's the *best* circ to hand back. try to decide.
|
|
|
|
*/
|
2004-04-08 03:55:21 +02:00
|
|
|
if(!best || circuit_is_better(circ,best,purpose))
|
2004-04-05 02:47:48 +02:00
|
|
|
best = circ;
|
2003-11-16 22:49:52 +01:00
|
|
|
}
|
|
|
|
|
2004-04-05 02:47:48 +02:00
|
|
|
return best;
|
2003-02-06 09:00:49 +01:00
|
|
|
}
|
|
|
|
|
2004-04-14 00:56:24 +02:00
|
|
|
circuit_t *circuit_get_by_rend_query_and_purpose(const char *rend_query, uint8_t purpose) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2004-04-02 23:56:52 +02:00
|
|
|
/* Return the first circuit in global_circuitlist after 'start' whose
|
2004-04-03 01:44:46 +02:00
|
|
|
* rend_pk_digest field is 'digest' and whose purpose is purpose. Returns
|
2004-04-02 23:56:52 +02:00
|
|
|
* NULL if no circuit is found. If 'start' is null, begin at the start of
|
|
|
|
* the list.
|
2004-03-30 21:52:42 +02:00
|
|
|
*/
|
2004-04-03 01:44:46 +02:00
|
|
|
circuit_t *circuit_get_next_by_pk_and_purpose(circuit_t *start,
|
2004-04-03 02:55:53 +02:00
|
|
|
const char *digest, uint8_t purpose)
|
2004-03-30 21:52:42 +02:00
|
|
|
{
|
|
|
|
circuit_t *circ;
|
2004-04-02 23:56:52 +02:00
|
|
|
if (start == NULL)
|
|
|
|
circ = global_circuitlist;
|
|
|
|
else
|
|
|
|
circ = start->next;
|
|
|
|
|
|
|
|
for( ; circ; circ = circ->next) {
|
2004-03-30 21:52:42 +02:00
|
|
|
if (circ->marked_for_close)
|
|
|
|
continue;
|
|
|
|
if (circ->purpose != purpose)
|
|
|
|
continue;
|
2004-04-03 04:40:30 +02:00
|
|
|
if (!memcmp(circ->rend_pk_digest, digest, DIGEST_LEN))
|
2004-03-30 21:52:42 +02:00
|
|
|
return circ;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-04-02 23:56:52 +02:00
|
|
|
/* Return the circuit waiting for a rendezvous with the provided cookie.
|
|
|
|
* Return NULL if no such circuit is found.
|
|
|
|
*/
|
|
|
|
circuit_t *circuit_get_rendezvous(const char *cookie)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2004-04-18 11:27:05 +02:00
|
|
|
#define MIN_SECONDS_BEFORE_EXPIRING_CIRC 30
|
2003-11-18 08:48:00 +01:00
|
|
|
/* circuits that were born at the end of their second might be expired
|
2004-04-18 11:27:05 +02:00
|
|
|
* after 30.1 seconds; circuits born at the beginning might be expired
|
|
|
|
* after closer to 31 seconds.
|
2003-11-18 08:48:00 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* close all circuits that start at us, aren't open, and were born
|
|
|
|
* at least MIN_SECONDS_BEFORE_EXPIRING_CIRC seconds ago */
|
2004-04-13 07:20:52 +02:00
|
|
|
void circuit_expire_building(time_t now) {
|
2003-11-18 10:53:03 +01:00
|
|
|
circuit_t *victim, *circ = global_circuitlist;
|
2003-11-18 08:48:00 +01:00
|
|
|
|
2003-11-18 10:53:03 +01:00
|
|
|
while(circ) {
|
|
|
|
victim = circ;
|
|
|
|
circ = circ->next;
|
2004-04-08 04:24:06 +02:00
|
|
|
if(!CIRCUIT_IS_ORIGIN(victim))
|
2004-04-07 22:38:23 +02:00
|
|
|
continue; /* didn't originate here */
|
|
|
|
if(victim->marked_for_close)
|
|
|
|
continue; /* don't mess with marked circs */
|
|
|
|
if(victim->timestamp_created + MIN_SECONDS_BEFORE_EXPIRING_CIRC > now)
|
|
|
|
continue; /* it's young still, don't mess with it */
|
|
|
|
|
2004-04-17 03:33:04 +02:00
|
|
|
/* some debug logs, to help track bugs */
|
|
|
|
if(victim->purpose >= CIRCUIT_PURPOSE_C_INTRODUCING &&
|
|
|
|
victim->purpose <= CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED) {
|
2004-04-17 12:04:00 +02:00
|
|
|
if(!victim->timestamp_dirty)
|
2004-04-17 12:25:38 +02:00
|
|
|
log_fn(LOG_DEBUG,"Considering %sopen purp %d to %s (circid %d). (clean).",
|
2004-04-17 03:33:04 +02:00
|
|
|
victim->state == CIRCUIT_STATE_OPEN ? "" : "non",
|
2004-04-17 12:25:38 +02:00
|
|
|
victim->purpose, victim->build_state->chosen_exit,
|
|
|
|
victim->n_circ_id);
|
2004-04-17 03:33:04 +02:00
|
|
|
else
|
2004-04-17 12:25:38 +02:00
|
|
|
log_fn(LOG_DEBUG,"Considering %sopen purp %d to %s (circid %d). %d secs since dirty.",
|
2004-04-17 03:33:04 +02:00
|
|
|
victim->state == CIRCUIT_STATE_OPEN ? "" : "non",
|
|
|
|
victim->purpose, victim->build_state->chosen_exit,
|
2004-04-17 12:25:38 +02:00
|
|
|
victim->n_circ_id,
|
2004-04-17 03:33:04 +02:00
|
|
|
(int)(now - victim->timestamp_dirty));
|
|
|
|
}
|
|
|
|
|
2004-04-09 22:08:13 +02:00
|
|
|
/* if circ is !open, or if it's open but purpose is a non-finished
|
|
|
|
* intro or rend, then mark it for close */
|
2004-04-07 22:38:23 +02:00
|
|
|
if(victim->state != CIRCUIT_STATE_OPEN ||
|
|
|
|
victim->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND ||
|
2004-04-15 01:52:29 +02:00
|
|
|
victim->purpose == CIRCUIT_PURPOSE_C_INTRODUCING ||
|
2004-04-09 22:08:13 +02:00
|
|
|
victim->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO ||
|
2004-04-15 01:52:29 +02:00
|
|
|
|
2004-04-18 11:27:05 +02:00
|
|
|
/* it's a rend_ready circ, but it's already picked a query */
|
|
|
|
(victim->purpose == CIRCUIT_PURPOSE_C_REND_READY &&
|
|
|
|
victim->rend_query[0]) ||
|
|
|
|
|
2004-04-09 22:08:13 +02:00
|
|
|
/* c_rend_ready circs measure age since timestamp_dirty,
|
|
|
|
* because that's set when they switch purposes
|
|
|
|
*/
|
2004-04-16 10:21:35 +02:00
|
|
|
/* rend and intro circs become dirty each time they
|
|
|
|
* make an introduction attempt. so timestamp_dirty
|
|
|
|
* will reflect the time since the last attempt.
|
|
|
|
*/
|
2004-04-15 01:52:29 +02:00
|
|
|
((victim->purpose == CIRCUIT_PURPOSE_C_REND_READY ||
|
|
|
|
victim->purpose == CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED ||
|
|
|
|
victim->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) &&
|
2004-04-09 22:08:13 +02:00
|
|
|
victim->timestamp_dirty + MIN_SECONDS_BEFORE_EXPIRING_CIRC > now)) {
|
2003-11-18 10:53:03 +01:00
|
|
|
if(victim->n_conn)
|
2004-04-15 03:08:59 +02:00
|
|
|
log_fn(LOG_INFO,"Abandoning circ %s:%d:%d (state %d:%s, purpose %d)",
|
2003-11-18 10:53:03 +01:00
|
|
|
victim->n_conn->address, victim->n_port, victim->n_circ_id,
|
2004-04-15 03:08:59 +02:00
|
|
|
victim->state, circuit_state_to_string[victim->state], victim->purpose);
|
2003-11-18 08:48:00 +01:00
|
|
|
else
|
2004-04-15 03:08:59 +02:00
|
|
|
log_fn(LOG_INFO,"Abandoning circ %d (state %d:%s, purpose %d)", victim->n_circ_id,
|
|
|
|
victim->state, circuit_state_to_string[victim->state], victim->purpose);
|
2004-01-30 20:31:39 +01:00
|
|
|
circuit_log_path(LOG_INFO,victim);
|
2004-03-02 18:48:17 +01:00
|
|
|
circuit_mark_for_close(victim);
|
2003-11-18 08:48:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* count the number of circs starting at us that aren't open */
|
2004-04-13 07:20:52 +02:00
|
|
|
int circuit_count_building(uint8_t purpose) {
|
2003-11-18 08:48:00 +01:00
|
|
|
circuit_t *circ;
|
|
|
|
int num=0;
|
|
|
|
|
|
|
|
for(circ=global_circuitlist;circ;circ = circ->next) {
|
2004-04-13 07:20:52 +02:00
|
|
|
if(CIRCUIT_IS_ORIGIN(circ) &&
|
|
|
|
circ->state != CIRCUIT_STATE_OPEN &&
|
|
|
|
circ->purpose == purpose &&
|
|
|
|
!circ->marked_for_close)
|
2003-11-18 08:48:00 +01:00
|
|
|
num++;
|
|
|
|
}
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2003-12-13 00:03:25 +01:00
|
|
|
#define MIN_CIRCUITS_HANDLING_STREAM 2
|
2004-04-05 02:47:48 +02:00
|
|
|
/* return 1 if at least MIN_CIRCUITS_HANDLING_STREAM non-open
|
|
|
|
* general-purpose circuits will have an acceptable exit node for
|
|
|
|
* conn. Else return 0.
|
2003-12-13 00:03:25 +01:00
|
|
|
*/
|
|
|
|
int circuit_stream_is_being_handled(connection_t *conn) {
|
|
|
|
circuit_t *circ;
|
|
|
|
routerinfo_t *exitrouter;
|
|
|
|
int num=0;
|
2004-04-07 21:14:33 +02:00
|
|
|
time_t now = time(NULL);
|
2003-12-13 00:03:25 +01:00
|
|
|
|
|
|
|
for(circ=global_circuitlist;circ;circ = circ->next) {
|
2004-04-08 04:24:06 +02:00
|
|
|
if(CIRCUIT_IS_ORIGIN(circ) && circ->state != CIRCUIT_STATE_OPEN &&
|
2004-04-07 21:14:33 +02:00
|
|
|
!circ->marked_for_close && circ->purpose == CIRCUIT_PURPOSE_C_GENERAL &&
|
|
|
|
(!circ->timestamp_dirty ||
|
|
|
|
circ->timestamp_dirty + options.NewCircuitPeriod < now)) {
|
2003-12-13 00:03:25 +01:00
|
|
|
exitrouter = router_get_by_nickname(circ->build_state->chosen_exit);
|
2004-02-17 09:29:22 +01:00
|
|
|
if(exitrouter && connection_ap_can_use_exit(conn, exitrouter) != ADDR_POLICY_REJECTED)
|
2003-12-13 00:03:25 +01:00
|
|
|
if(++num >= MIN_CIRCUITS_HANDLING_STREAM)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-04-13 07:20:52 +02:00
|
|
|
static circuit_t *
|
|
|
|
circuit_get_youngest_clean_open(uint8_t purpose) {
|
2004-04-13 00:47:12 +02:00
|
|
|
circuit_t *circ;
|
2004-04-13 07:20:52 +02:00
|
|
|
circuit_t *youngest=NULL;
|
2004-04-13 00:47:12 +02:00
|
|
|
|
2004-04-13 07:20:52 +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 &&
|
|
|
|
(!youngest || youngest->timestamp_created < circ->timestamp_created))
|
|
|
|
youngest = circ;
|
|
|
|
}
|
|
|
|
return youngest;
|
|
|
|
}
|
2004-04-13 00:47:12 +02:00
|
|
|
|
|
|
|
/* Build a new test circuit every 5 minutes */
|
|
|
|
#define TESTING_CIRCUIT_INTERVAL 300
|
|
|
|
|
2004-04-13 07:20:52 +02:00
|
|
|
/* this function is called once a second. its job is to make sure
|
|
|
|
* all services we offer have enough circuits available. Some
|
|
|
|
* services just want enough circuits for current tasks, whereas
|
|
|
|
* others want a minimum set of idle circuits hanging around.
|
|
|
|
*/
|
|
|
|
void circuit_build_needed_circs(time_t now) {
|
|
|
|
static long time_to_new_circuit = 0;
|
|
|
|
circuit_t *circ;
|
|
|
|
|
|
|
|
/* launch a new circ for any pending streams that need one */
|
|
|
|
connection_ap_attach_pending();
|
|
|
|
|
|
|
|
/* make sure any hidden services have enough intro points */
|
2004-04-13 19:16:47 +02:00
|
|
|
rend_services_introduce();
|
2004-04-13 07:20:52 +02:00
|
|
|
|
|
|
|
circ = circuit_get_youngest_clean_open(CIRCUIT_PURPOSE_C_GENERAL);
|
|
|
|
|
2004-04-13 00:47:12 +02:00
|
|
|
if(time_to_new_circuit < now) {
|
|
|
|
circuit_reset_failure_count();
|
2004-04-13 07:20:52 +02:00
|
|
|
time_to_new_circuit = now + options.NewCircuitPeriod;
|
|
|
|
if(options.SocksPort)
|
|
|
|
client_dns_clean();
|
|
|
|
circuit_expire_old_circuits();
|
|
|
|
|
|
|
|
if(options.RunTesting && circ &&
|
2004-04-13 00:47:12 +02:00
|
|
|
circ->timestamp_created + TESTING_CIRCUIT_INTERVAL < now) {
|
|
|
|
log_fn(LOG_INFO,"Creating a new testing circuit.");
|
|
|
|
circuit_launch_new(CIRCUIT_PURPOSE_C_GENERAL, NULL);
|
|
|
|
}
|
|
|
|
}
|
2004-04-13 07:20:52 +02:00
|
|
|
|
|
|
|
#define CIRCUIT_MIN_BUILDING_GENERAL 3
|
|
|
|
/* if there's no open circ, and less than 3 are on the way,
|
|
|
|
* go ahead and try another. */
|
|
|
|
if(!circ && circuit_count_building(CIRCUIT_PURPOSE_C_GENERAL)
|
|
|
|
< CIRCUIT_MIN_BUILDING_GENERAL) {
|
2004-04-13 00:47:12 +02:00
|
|
|
circuit_launch_new(CIRCUIT_PURPOSE_C_GENERAL, NULL);
|
|
|
|
}
|
2004-04-13 07:20:52 +02:00
|
|
|
|
|
|
|
/* XXX count idle rendezvous circs and build more */
|
2004-04-13 00:47:12 +02:00
|
|
|
}
|
|
|
|
|
2003-12-16 23:56:50 +01:00
|
|
|
/* update digest from the payload of cell. assign integrity part to cell. */
|
2003-12-23 08:45:31 +01:00
|
|
|
static void relay_set_digest(crypto_digest_env_t *digest, cell_t *cell) {
|
2004-01-02 10:03:38 +01:00
|
|
|
char integrity[4];
|
2003-12-19 06:09:51 +01:00
|
|
|
relay_header_t rh;
|
2003-12-16 23:56:50 +01:00
|
|
|
|
|
|
|
crypto_digest_add_bytes(digest, cell->payload, CELL_PAYLOAD_SIZE);
|
2004-01-02 10:03:38 +01:00
|
|
|
crypto_digest_get_digest(digest, integrity, 4);
|
2004-02-28 08:01:22 +01:00
|
|
|
// log_fn(LOG_DEBUG,"Putting digest of %u %u %u %u into relay cell.",
|
|
|
|
// integrity[0], integrity[1], integrity[2], integrity[3]);
|
2003-12-19 06:09:51 +01:00
|
|
|
relay_header_unpack(&rh, cell->payload);
|
2004-01-02 10:03:38 +01:00
|
|
|
memcpy(rh.integrity, integrity, 4);
|
2003-12-19 06:09:51 +01:00
|
|
|
relay_header_pack(cell->payload, &rh);
|
2003-12-16 23:56:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* update digest from the payload of cell (with the integrity part set
|
2003-12-26 07:29:57 +01:00
|
|
|
* to 0). If the integrity part is valid return 1, else restore digest
|
|
|
|
* and cell to their original state and return 0.
|
2003-12-16 23:56:50 +01:00
|
|
|
*/
|
2003-12-26 07:29:57 +01:00
|
|
|
static int relay_digest_matches(crypto_digest_env_t *digest, cell_t *cell) {
|
2004-01-02 10:03:38 +01:00
|
|
|
char received_integrity[4], calculated_integrity[4];
|
2003-12-19 06:09:51 +01:00
|
|
|
relay_header_t rh;
|
2003-12-26 07:29:57 +01:00
|
|
|
crypto_digest_env_t *backup_digest=NULL;
|
|
|
|
|
|
|
|
backup_digest = crypto_digest_dup(digest);
|
2003-12-19 06:09:51 +01:00
|
|
|
|
|
|
|
relay_header_unpack(&rh, cell->payload);
|
2004-01-02 10:03:38 +01:00
|
|
|
memcpy(received_integrity, rh.integrity, 4);
|
|
|
|
memset(rh.integrity, 0, 4);
|
2003-12-19 06:09:51 +01:00
|
|
|
relay_header_pack(cell->payload, &rh);
|
2003-12-16 23:56:50 +01:00
|
|
|
|
2004-02-28 08:01:22 +01:00
|
|
|
// log_fn(LOG_DEBUG,"Reading digest of %u %u %u %u from relay cell.",
|
|
|
|
// received_integrity[0], received_integrity[1],
|
|
|
|
// received_integrity[2], received_integrity[3]);
|
2003-12-16 23:56:50 +01:00
|
|
|
|
|
|
|
crypto_digest_add_bytes(digest, cell->payload, CELL_PAYLOAD_SIZE);
|
2004-01-02 10:03:38 +01:00
|
|
|
crypto_digest_get_digest(digest, calculated_integrity, 4);
|
2003-12-16 23:56:50 +01:00
|
|
|
|
2004-01-02 10:03:38 +01:00
|
|
|
if(memcmp(received_integrity, calculated_integrity, 4)) {
|
2004-02-28 08:01:22 +01:00
|
|
|
// log_fn(LOG_INFO,"Recognized=0 but bad digest. Not recognizing.");
|
2004-01-02 10:03:38 +01:00
|
|
|
// (%d vs %d).", received_integrity, calculated_integrity);
|
2003-12-26 07:29:57 +01:00
|
|
|
/* restore digest to its old form */
|
|
|
|
crypto_digest_assign(digest, backup_digest);
|
|
|
|
/* restore the relay header */
|
2004-01-02 10:03:38 +01:00
|
|
|
memcpy(rh.integrity, received_integrity, 4);
|
2003-12-26 07:29:57 +01:00
|
|
|
relay_header_pack(cell->payload, &rh);
|
|
|
|
crypto_free_digest_env(backup_digest);
|
|
|
|
return 0;
|
2003-12-16 23:56:50 +01:00
|
|
|
}
|
2003-12-26 07:29:57 +01:00
|
|
|
crypto_free_digest_env(backup_digest);
|
|
|
|
return 1;
|
2003-12-16 23:56:50 +01:00
|
|
|
}
|
|
|
|
|
2003-12-23 08:45:31 +01:00
|
|
|
static int relay_crypt_one_payload(crypto_cipher_env_t *cipher, char *in,
|
|
|
|
int encrypt_mode) {
|
|
|
|
char out[CELL_PAYLOAD_SIZE]; /* 'in' must be this size too */
|
|
|
|
relay_header_t rh;
|
|
|
|
|
|
|
|
relay_header_unpack(&rh, in);
|
2004-02-28 08:01:22 +01:00
|
|
|
// log_fn(LOG_DEBUG,"before crypt: %d",rh.recognized);
|
2003-12-23 08:45:31 +01:00
|
|
|
if(( encrypt_mode && crypto_cipher_encrypt(cipher, in, CELL_PAYLOAD_SIZE, out)) ||
|
|
|
|
(!encrypt_mode && crypto_cipher_decrypt(cipher, in, CELL_PAYLOAD_SIZE, out))) {
|
2004-04-26 20:09:50 +02:00
|
|
|
log_fn(LOG_WARN,"Error during relay encryption");
|
2003-12-23 08:45:31 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
memcpy(in,out,CELL_PAYLOAD_SIZE);
|
|
|
|
relay_header_unpack(&rh, in);
|
2004-02-28 08:01:22 +01:00
|
|
|
// log_fn(LOG_DEBUG,"after crypt: %d",rh.recognized);
|
2003-12-23 08:45:31 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
receive a relay cell:
|
|
|
|
- crypt it (encrypt APward, decrypt at AP, decrypt exitward)
|
|
|
|
- check if recognized (if exitward)
|
|
|
|
- if recognized, check digest, find right conn, deliver to edge.
|
|
|
|
- else connection_or_write_cell_to_buf to the right conn
|
|
|
|
*/
|
|
|
|
int circuit_receive_relay_cell(cell_t *cell, circuit_t *circ,
|
|
|
|
int cell_direction) {
|
2003-05-02 23:29:25 +02:00
|
|
|
connection_t *conn=NULL;
|
2003-12-23 08:45:31 +01:00
|
|
|
crypt_path_t *layer_hint=NULL;
|
2003-05-02 23:29:25 +02:00
|
|
|
char recognized=0;
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(cell && circ);
|
|
|
|
tor_assert(cell_direction == CELL_DIRECTION_OUT || cell_direction == CELL_DIRECTION_IN);
|
2004-03-02 18:48:17 +01:00
|
|
|
if (circ->marked_for_close)
|
|
|
|
return 0;
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
|
2003-12-26 07:29:57 +01:00
|
|
|
if(relay_crypt(circ, cell, cell_direction, &layer_hint, &recognized) < 0) {
|
2003-10-10 03:48:32 +02:00
|
|
|
log_fn(LOG_WARN,"relay crypt failed. Dropping connection.");
|
2002-06-27 00:45:49 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2003-05-02 23:29:25 +02:00
|
|
|
if(recognized) {
|
2003-12-19 06:09:51 +01:00
|
|
|
conn = relay_lookup_conn(circ, cell, cell_direction);
|
2003-05-02 23:29:25 +02:00
|
|
|
if(cell_direction == CELL_DIRECTION_OUT) {
|
2003-10-02 22:00:38 +02:00
|
|
|
++stats_n_relay_cells_delivered;
|
2004-04-08 11:41:28 +02:00
|
|
|
log_fn(LOG_DEBUG,"Sending away from origin.");
|
|
|
|
if (connection_edge_process_relay_cell(cell, circ, conn, NULL) < 0) {
|
|
|
|
log_fn(LOG_WARN,"connection_edge_process_relay_cell (away from origin) failed.");
|
2003-11-14 08:15:52 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2003-05-02 23:29:25 +02:00
|
|
|
}
|
|
|
|
if(cell_direction == CELL_DIRECTION_IN) {
|
2003-10-02 22:00:38 +02:00
|
|
|
++stats_n_relay_cells_delivered;
|
2004-04-08 11:41:28 +02:00
|
|
|
log_fn(LOG_DEBUG,"Sending to origin.");
|
|
|
|
if (connection_edge_process_relay_cell(cell, circ, conn, layer_hint) < 0) {
|
|
|
|
log_fn(LOG_WARN,"connection_edge_process_relay_cell (at origin) failed.");
|
2003-11-14 08:15:52 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2003-05-02 23:29:25 +02:00
|
|
|
}
|
2003-11-16 18:31:19 +01:00
|
|
|
return 0;
|
2002-06-27 00:45:49 +02:00
|
|
|
}
|
2003-05-02 23:29:25 +02:00
|
|
|
|
|
|
|
/* not recognized. pass it on. */
|
2003-12-23 08:45:31 +01:00
|
|
|
if(cell_direction == CELL_DIRECTION_OUT) {
|
|
|
|
cell->circ_id = circ->n_circ_id; /* switch it */
|
2003-05-02 23:29:25 +02:00
|
|
|
conn = circ->n_conn;
|
2003-12-23 08:45:31 +01:00
|
|
|
} else {
|
|
|
|
cell->circ_id = circ->p_circ_id; /* switch it */
|
2003-05-02 23:29:25 +02:00
|
|
|
conn = circ->p_conn;
|
2003-12-23 08:45:31 +01:00
|
|
|
}
|
2003-05-02 23:29:25 +02:00
|
|
|
|
2003-11-14 08:15:52 +01:00
|
|
|
if(!conn) {
|
2004-04-02 23:56:52 +02:00
|
|
|
if (circ->rend_splice && cell_direction == CELL_DIRECTION_OUT) {
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(circ->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED);
|
|
|
|
tor_assert(circ->rend_splice->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED);
|
2004-04-02 23:56:52 +02:00
|
|
|
cell->circ_id = circ->rend_splice->p_circ_id;
|
|
|
|
if (circuit_receive_relay_cell(cell, circ->rend_splice, CELL_DIRECTION_IN)<0) {
|
|
|
|
log_fn(LOG_WARN, "Error relaying cell across rendezvous; closing circuits");
|
|
|
|
circuit_mark_for_close(circ); /* XXXX Do this here, or just return -1? */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2004-02-28 08:01:22 +01:00
|
|
|
log_fn(LOG_WARN,"Didn't recognize cell, but circ stops here! Closing circ.");
|
2004-02-28 05:48:46 +01:00
|
|
|
return -1;
|
Integrated onion proxy into or/
The 'or' process can now be told (by the global_role variable) what
roles this server should play -- connect to all ORs, listen for ORs,
listen for OPs, listen for APs, or any combination.
* everything in /src/op/ is now obsolete.
* connection_ap.c now handles all interactions with application proxies
* "port" is now or_port, op_port, ap_port. But routers are still always
referenced (say, in conn_get_by_addr_port()) by addr / or_port. We
should make routers.c actually read these new ports (currently I've
kludged it so op_port = or_port+10, ap_port=or_port+20)
* circuits currently know if they're at the beginning of the path because
circ->cpath is set. They use this instead for crypts (both ways),
if it's set.
* I still obey the "send a 0 back to the AP when you're ready" protocol,
but I think we should phase it out. I can simply not read from the AP
socket until I'm ready.
I need to do a lot of cleanup work here, but the code appears to work, so
now's a good time for a checkin.
svn:r22
2002-07-02 11:36:58 +02:00
|
|
|
}
|
2003-05-02 23:29:25 +02:00
|
|
|
|
2003-06-18 00:18:26 +02:00
|
|
|
log_fn(LOG_DEBUG,"Passing on unrecognized cell.");
|
2003-11-17 02:23:15 +01:00
|
|
|
++stats_n_relay_cells_relayed;
|
2003-10-09 20:45:14 +02:00
|
|
|
connection_or_write_cell_to_buf(cell, conn);
|
2003-10-04 04:38:18 +02:00
|
|
|
return 0;
|
2002-06-27 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
2003-12-23 08:45:31 +01:00
|
|
|
/* wrap this into receive_relay_cell one day */
|
2004-03-09 23:01:17 +01:00
|
|
|
static int relay_crypt(circuit_t *circ, cell_t *cell, int cell_direction,
|
2003-12-19 06:09:51 +01:00
|
|
|
crypt_path_t **layer_hint, char *recognized) {
|
Integrated onion proxy into or/
The 'or' process can now be told (by the global_role variable) what
roles this server should play -- connect to all ORs, listen for ORs,
listen for OPs, listen for APs, or any combination.
* everything in /src/op/ is now obsolete.
* connection_ap.c now handles all interactions with application proxies
* "port" is now or_port, op_port, ap_port. But routers are still always
referenced (say, in conn_get_by_addr_port()) by addr / or_port. We
should make routers.c actually read these new ports (currently I've
kludged it so op_port = or_port+10, ap_port=or_port+20)
* circuits currently know if they're at the beginning of the path because
circ->cpath is set. They use this instead for crypts (both ways),
if it's set.
* I still obey the "send a 0 back to the AP when you're ready" protocol,
but I think we should phase it out. I can simply not read from the AP
socket until I'm ready.
I need to do a lot of cleanup work here, but the code appears to work, so
now's a good time for a checkin.
svn:r22
2002-07-02 11:36:58 +02:00
|
|
|
crypt_path_t *thishop;
|
2003-12-19 06:09:51 +01:00
|
|
|
relay_header_t rh;
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(circ && cell && recognized);
|
|
|
|
tor_assert(cell_direction == CELL_DIRECTION_IN || cell_direction == CELL_DIRECTION_OUT);
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2003-12-17 22:09:31 +01:00
|
|
|
if(cell_direction == CELL_DIRECTION_IN) {
|
2004-04-08 04:24:06 +02:00
|
|
|
if(CIRCUIT_IS_ORIGIN(circ)) { /* we're at the beginning of the circuit.
|
|
|
|
We'll want to do layered crypts. */
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(circ->cpath);
|
2003-05-02 00:55:51 +02:00
|
|
|
thishop = circ->cpath;
|
2003-05-06 01:24:46 +02:00
|
|
|
if(thishop->state != CPATH_STATE_OPEN) {
|
2004-02-28 08:01:22 +01:00
|
|
|
log_fn(LOG_WARN,"Relay cell before first created cell? Closing.");
|
2003-05-06 01:24:46 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2003-05-02 23:29:25 +02:00
|
|
|
do { /* Remember: cpath is in forward order, that is, first hop first. */
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(thishop);
|
2003-05-02 23:29:25 +02:00
|
|
|
|
2003-12-26 07:29:57 +01:00
|
|
|
if(relay_crypt_one_payload(thishop->b_crypto, cell->payload, 0) < 0)
|
Integrated onion proxy into or/
The 'or' process can now be told (by the global_role variable) what
roles this server should play -- connect to all ORs, listen for ORs,
listen for OPs, listen for APs, or any combination.
* everything in /src/op/ is now obsolete.
* connection_ap.c now handles all interactions with application proxies
* "port" is now or_port, op_port, ap_port. But routers are still always
referenced (say, in conn_get_by_addr_port()) by addr / or_port. We
should make routers.c actually read these new ports (currently I've
kludged it so op_port = or_port+10, ap_port=or_port+20)
* circuits currently know if they're at the beginning of the path because
circ->cpath is set. They use this instead for crypts (both ways),
if it's set.
* I still obey the "send a 0 back to the AP when you're ready" protocol,
but I think we should phase it out. I can simply not read from the AP
socket until I'm ready.
I need to do a lot of cleanup work here, but the code appears to work, so
now's a good time for a checkin.
svn:r22
2002-07-02 11:36:58 +02:00
|
|
|
return -1;
|
2003-05-02 23:29:25 +02:00
|
|
|
|
2003-12-26 07:29:57 +01:00
|
|
|
relay_header_unpack(&rh, cell->payload);
|
2003-12-23 08:45:31 +01:00
|
|
|
if(rh.recognized == 0) {
|
2003-12-26 07:29:57 +01:00
|
|
|
/* it's possibly recognized. have to check digest to be sure. */
|
|
|
|
if(relay_digest_matches(thishop->b_digest, cell)) {
|
|
|
|
*recognized = 1;
|
|
|
|
*layer_hint = thishop;
|
|
|
|
return 0;
|
|
|
|
}
|
2003-05-20 08:41:23 +02:00
|
|
|
}
|
2003-05-02 23:29:25 +02:00
|
|
|
|
2003-05-02 00:55:51 +02:00
|
|
|
thishop = thishop->next;
|
2003-05-06 01:24:46 +02:00
|
|
|
} while(thishop != circ->cpath && thishop->state == CPATH_STATE_OPEN);
|
2004-02-28 08:01:22 +01:00
|
|
|
log_fn(LOG_WARN,"in-cell at OP not recognized. Closing.");
|
|
|
|
return -1;
|
Integrated onion proxy into or/
The 'or' process can now be told (by the global_role variable) what
roles this server should play -- connect to all ORs, listen for ORs,
listen for OPs, listen for APs, or any combination.
* everything in /src/op/ is now obsolete.
* connection_ap.c now handles all interactions with application proxies
* "port" is now or_port, op_port, ap_port. But routers are still always
referenced (say, in conn_get_by_addr_port()) by addr / or_port. We
should make routers.c actually read these new ports (currently I've
kludged it so op_port = or_port+10, ap_port=or_port+20)
* circuits currently know if they're at the beginning of the path because
circ->cpath is set. They use this instead for crypts (both ways),
if it's set.
* I still obey the "send a 0 back to the AP when you're ready" protocol,
but I think we should phase it out. I can simply not read from the AP
socket until I'm ready.
I need to do a lot of cleanup work here, but the code appears to work, so
now's a good time for a checkin.
svn:r22
2002-07-02 11:36:58 +02:00
|
|
|
} else { /* we're in the middle. Just one crypt. */
|
2003-12-26 07:29:57 +01:00
|
|
|
if(relay_crypt_one_payload(circ->p_crypto, cell->payload, 1) < 0)
|
Integrated onion proxy into or/
The 'or' process can now be told (by the global_role variable) what
roles this server should play -- connect to all ORs, listen for ORs,
listen for OPs, listen for APs, or any combination.
* everything in /src/op/ is now obsolete.
* connection_ap.c now handles all interactions with application proxies
* "port" is now or_port, op_port, ap_port. But routers are still always
referenced (say, in conn_get_by_addr_port()) by addr / or_port. We
should make routers.c actually read these new ports (currently I've
kludged it so op_port = or_port+10, ap_port=or_port+20)
* circuits currently know if they're at the beginning of the path because
circ->cpath is set. They use this instead for crypts (both ways),
if it's set.
* I still obey the "send a 0 back to the AP when you're ready" protocol,
but I think we should phase it out. I can simply not read from the AP
socket until I'm ready.
I need to do a lot of cleanup work here, but the code appears to work, so
now's a good time for a checkin.
svn:r22
2002-07-02 11:36:58 +02:00
|
|
|
return -1;
|
2004-02-28 08:01:22 +01:00
|
|
|
// log_fn(LOG_DEBUG,"Skipping recognized check, because we're not the OP.");
|
2002-06-27 00:45:49 +02:00
|
|
|
}
|
2003-12-19 06:09:51 +01:00
|
|
|
} else /* cell_direction == CELL_DIRECTION_OUT */ {
|
2003-12-23 08:45:31 +01:00
|
|
|
/* we're in the middle. Just one crypt. */
|
2003-05-02 23:29:25 +02:00
|
|
|
|
2003-12-26 07:29:57 +01:00
|
|
|
if(relay_crypt_one_payload(circ->n_crypto, cell->payload, 0) < 0)
|
2003-12-23 08:45:31 +01:00
|
|
|
return -1;
|
|
|
|
|
2003-12-26 07:29:57 +01:00
|
|
|
relay_header_unpack(&rh, cell->payload);
|
|
|
|
if (rh.recognized == 0) {
|
|
|
|
/* it's possibly recognized. have to check digest to be sure. */
|
|
|
|
if(relay_digest_matches(circ->n_digest, cell)) {
|
|
|
|
*recognized = 1;
|
|
|
|
return 0;
|
2003-12-23 08:45:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
package a relay cell:
|
2004-02-28 08:01:22 +01:00
|
|
|
1) encrypt it to the right conn
|
|
|
|
2) connection_or_write_cell_to_buf to the right conn
|
2003-12-23 08:45:31 +01:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
circuit_package_relay_cell(cell_t *cell, circuit_t *circ,
|
|
|
|
int cell_direction,
|
|
|
|
crypt_path_t *layer_hint)
|
|
|
|
{
|
|
|
|
connection_t *conn; /* where to send the cell */
|
2003-12-26 07:29:57 +01:00
|
|
|
crypt_path_t *thishop; /* counter for repeated crypts */
|
2003-12-23 08:45:31 +01:00
|
|
|
|
|
|
|
if(cell_direction == CELL_DIRECTION_OUT) {
|
|
|
|
conn = circ->n_conn;
|
|
|
|
if(!conn) {
|
2004-02-28 08:01:22 +01:00
|
|
|
log_fn(LOG_WARN,"outgoing relay cell has n_conn==NULL. Dropping.");
|
2003-12-23 08:45:31 +01:00
|
|
|
return 0; /* just drop it */
|
|
|
|
}
|
2003-12-26 07:29:57 +01:00
|
|
|
relay_set_digest(layer_hint->f_digest, cell);
|
2003-12-23 08:45:31 +01:00
|
|
|
|
2003-12-26 07:29:57 +01:00
|
|
|
thishop = layer_hint;
|
|
|
|
/* moving from farthest to nearest hop */
|
|
|
|
do {
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(thishop);
|
2003-05-02 23:29:25 +02:00
|
|
|
|
2003-12-30 23:49:35 +01:00
|
|
|
log_fn(LOG_DEBUG,"crypting a layer of the relay cell.");
|
2003-12-26 07:29:57 +01:00
|
|
|
if(relay_crypt_one_payload(thishop->f_crypto, cell->payload, 1) < 0) {
|
Integrated onion proxy into or/
The 'or' process can now be told (by the global_role variable) what
roles this server should play -- connect to all ORs, listen for ORs,
listen for OPs, listen for APs, or any combination.
* everything in /src/op/ is now obsolete.
* connection_ap.c now handles all interactions with application proxies
* "port" is now or_port, op_port, ap_port. But routers are still always
referenced (say, in conn_get_by_addr_port()) by addr / or_port. We
should make routers.c actually read these new ports (currently I've
kludged it so op_port = or_port+10, ap_port=or_port+20)
* circuits currently know if they're at the beginning of the path because
circ->cpath is set. They use this instead for crypts (both ways),
if it's set.
* I still obey the "send a 0 back to the AP when you're ready" protocol,
but I think we should phase it out. I can simply not read from the AP
socket until I'm ready.
I need to do a lot of cleanup work here, but the code appears to work, so
now's a good time for a checkin.
svn:r22
2002-07-02 11:36:58 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2003-05-02 23:29:25 +02:00
|
|
|
|
2003-12-26 07:29:57 +01:00
|
|
|
thishop = thishop->prev;
|
|
|
|
} while (thishop != circ->cpath->prev);
|
2003-12-23 08:45:31 +01:00
|
|
|
|
|
|
|
} else { /* incoming cell */
|
|
|
|
conn = circ->p_conn;
|
|
|
|
if(!conn) {
|
2004-02-28 08:01:22 +01:00
|
|
|
log_fn(LOG_WARN,"incoming relay cell has p_conn==NULL. Dropping.");
|
2003-12-23 08:45:31 +01:00
|
|
|
return 0; /* just drop it */
|
2002-06-27 00:45:49 +02:00
|
|
|
}
|
2003-12-23 08:45:31 +01:00
|
|
|
relay_set_digest(circ->p_digest, cell);
|
|
|
|
if(relay_crypt_one_payload(circ->p_crypto, cell->payload, 1) < 0)
|
|
|
|
return -1;
|
2002-06-27 00:45:49 +02:00
|
|
|
}
|
2003-12-23 08:45:31 +01:00
|
|
|
++stats_n_relay_cells_relayed;
|
|
|
|
connection_or_write_cell_to_buf(cell, conn);
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-12-23 08:45:31 +01:00
|
|
|
static connection_t *
|
|
|
|
relay_lookup_conn(circuit_t *circ, cell_t *cell, int cell_direction)
|
|
|
|
{
|
2003-05-02 23:29:25 +02:00
|
|
|
connection_t *tmpconn;
|
2003-12-19 06:09:51 +01:00
|
|
|
relay_header_t rh;
|
2003-05-02 23:29:25 +02:00
|
|
|
|
2003-12-19 06:09:51 +01:00
|
|
|
relay_header_unpack(&rh, cell->payload);
|
|
|
|
|
|
|
|
if(!rh.stream_id)
|
|
|
|
return NULL;
|
2003-05-02 23:29:25 +02:00
|
|
|
|
2004-04-06 23:52:01 +02:00
|
|
|
/* IN or OUT cells could have come from either direction, now
|
|
|
|
* that we allow rendezvous *to* an OP.
|
|
|
|
*/
|
2003-05-02 23:29:25 +02:00
|
|
|
|
2004-04-06 23:52:01 +02:00
|
|
|
for(tmpconn = circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream) {
|
|
|
|
if(rh.stream_id == tmpconn->stream_id) {
|
|
|
|
log_fn(LOG_DEBUG,"found conn for stream %d.", rh.stream_id);
|
|
|
|
if(cell_direction == CELL_DIRECTION_OUT ||
|
|
|
|
connection_edge_is_rendezvous_stream(tmpconn))
|
|
|
|
return tmpconn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(tmpconn = circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream) {
|
2003-12-19 06:09:51 +01:00
|
|
|
if(rh.stream_id == tmpconn->stream_id) {
|
|
|
|
log_fn(LOG_DEBUG,"found conn for stream %d.", rh.stream_id);
|
|
|
|
return tmpconn;
|
2003-05-02 23:29:25 +02:00
|
|
|
}
|
|
|
|
}
|
2004-05-06 13:08:04 +02:00
|
|
|
for(tmpconn = circ->resolving_streams; tmpconn; tmpconn=tmpconn->next_stream) {
|
|
|
|
if(rh.stream_id == tmpconn->stream_id) {
|
|
|
|
log_fn(LOG_DEBUG,"found conn for stream %d.", rh.stream_id);
|
|
|
|
return tmpconn;
|
|
|
|
}
|
|
|
|
}
|
2003-12-19 06:09:51 +01:00
|
|
|
return NULL; /* probably a begin relay cell */
|
2003-05-02 23:29:25 +02:00
|
|
|
}
|
|
|
|
|
2004-04-08 11:41:28 +02:00
|
|
|
void circuit_resume_edge_reading(circuit_t *circ, crypt_path_t *layer_hint) {
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
|
2003-06-18 00:18:26 +02:00
|
|
|
log_fn(LOG_DEBUG,"resuming");
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
|
2004-04-08 11:41:28 +02:00
|
|
|
/* have to check both n_streams and p_streams, to handle rendezvous */
|
|
|
|
if(circuit_resume_edge_reading_helper(circ->n_streams, circ, layer_hint) >= 0)
|
|
|
|
circuit_resume_edge_reading_helper(circ->p_streams, circ, layer_hint);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
circuit_resume_edge_reading_helper(connection_t *conn,
|
|
|
|
circuit_t *circ,
|
|
|
|
crypt_path_t *layer_hint) {
|
2003-02-18 02:35:55 +01:00
|
|
|
|
2003-05-01 08:42:29 +02:00
|
|
|
for( ; conn; conn=conn->next_stream) {
|
2004-04-08 11:41:28 +02:00
|
|
|
if((!layer_hint && conn->package_window > 0) ||
|
|
|
|
(layer_hint && conn->package_window > 0 && conn->cpath_layer == layer_hint)) {
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
connection_start_reading(conn);
|
2004-04-08 11:41:28 +02:00
|
|
|
/* handle whatever might still be on the inbuf */
|
|
|
|
connection_edge_package_raw_inbuf(conn);
|
2003-08-25 22:57:23 +02:00
|
|
|
|
|
|
|
/* If the circuit won't accept any more data, return without looking
|
|
|
|
* at any more of the streams. Any connections that should be stopped
|
2003-10-09 20:45:14 +02:00
|
|
|
* have already been stopped by connection_edge_package_raw_inbuf. */
|
2004-04-08 11:41:28 +02:00
|
|
|
if(circuit_consider_stop_edge_reading(circ, layer_hint))
|
|
|
|
return -1;
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
}
|
|
|
|
}
|
2004-04-08 11:41:28 +02:00
|
|
|
return 0;
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
}
|
|
|
|
|
2004-04-08 11:41:28 +02:00
|
|
|
/* returns -1 if the window is empty, else 0.
|
|
|
|
* If it's empty, tell edge conns to stop reading. */
|
|
|
|
int circuit_consider_stop_edge_reading(circuit_t *circ, crypt_path_t *layer_hint) {
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
connection_t *conn = NULL;
|
|
|
|
|
2003-06-18 00:18:26 +02:00
|
|
|
log_fn(LOG_DEBUG,"considering");
|
2004-04-08 11:41:28 +02:00
|
|
|
if(!layer_hint && circ->package_window <= 0) {
|
|
|
|
log_fn(LOG_DEBUG,"yes, not-at-origin. stopped.");
|
|
|
|
for(conn = circ->n_streams; conn; conn=conn->next_stream)
|
2003-05-20 08:41:23 +02:00
|
|
|
connection_stop_reading(conn);
|
2004-04-08 11:41:28 +02:00
|
|
|
return -1;
|
|
|
|
} else if(layer_hint && layer_hint->package_window <= 0) {
|
|
|
|
log_fn(LOG_DEBUG,"yes, at-origin. stopped.");
|
|
|
|
for(conn = circ->n_streams; conn; conn=conn->next_stream)
|
|
|
|
if(conn->cpath_layer == layer_hint)
|
|
|
|
connection_stop_reading(conn);
|
|
|
|
for(conn = circ->p_streams; conn; conn=conn->next_stream)
|
|
|
|
if(conn->cpath_layer == layer_hint)
|
|
|
|
connection_stop_reading(conn);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
}
|
|
|
|
|
2004-04-08 11:41:28 +02:00
|
|
|
void circuit_consider_sending_sendme(circuit_t *circ, crypt_path_t *layer_hint) {
|
|
|
|
// log_fn(LOG_INFO,"Considering: layer_hint is %s",
|
|
|
|
// layer_hint ? "defined" : "null");
|
|
|
|
while((layer_hint ? layer_hint->deliver_window : circ->deliver_window) <
|
2003-12-16 23:56:50 +01:00
|
|
|
CIRCWINDOW_START - CIRCWINDOW_INCREMENT) {
|
|
|
|
log_fn(LOG_DEBUG,"Queueing circuit sendme.");
|
2004-04-08 11:41:28 +02:00
|
|
|
if(layer_hint)
|
2003-05-20 08:41:23 +02:00
|
|
|
layer_hint->deliver_window += CIRCWINDOW_INCREMENT;
|
2003-12-16 23:56:50 +01:00
|
|
|
else
|
2003-05-20 08:41:23 +02:00
|
|
|
circ->deliver_window += CIRCWINDOW_INCREMENT;
|
2003-12-16 23:56:50 +01:00
|
|
|
if(connection_edge_send_command(NULL, circ, RELAY_COMMAND_SENDME,
|
|
|
|
NULL, 0, layer_hint) < 0) {
|
2004-02-28 08:01:22 +01:00
|
|
|
log_fn(LOG_WARN,"connection_edge_send_command failed. Circuit's closed.");
|
2003-12-16 23:56:50 +01:00
|
|
|
return; /* the circuit's closed, don't continue */
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
}
|
|
|
|
}
|
2002-06-27 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
2004-03-02 18:48:17 +01:00
|
|
|
int _circuit_mark_for_close(circuit_t *circ) {
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
connection_t *conn;
|
|
|
|
|
2004-03-02 18:48:17 +01:00
|
|
|
assert_circuit_ok(circ);
|
2004-05-06 13:08:04 +02:00
|
|
|
if (circ->marked_for_close)
|
2004-03-02 18:48:17 +01:00
|
|
|
return -1;
|
|
|
|
|
2003-11-18 22:12:17 +01:00
|
|
|
if(circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
|
|
|
|
onion_pending_remove(circ);
|
|
|
|
}
|
2004-03-20 05:59:29 +01: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.
|
|
|
|
*/
|
2004-04-01 03:57:22 +02:00
|
|
|
if (circ->state != CIRCUIT_STATE_OPEN) {
|
2004-04-14 06:32:49 +02:00
|
|
|
if(CIRCUIT_IS_ORIGIN(circ))
|
2004-04-08 04:22:26 +02:00
|
|
|
circuit_build_failed(circ); /* take actions if necessary */
|
2004-03-20 05:59:29 +01:00
|
|
|
circuit_rep_hist_note_result(circ);
|
2004-04-01 03:57:22 +02:00
|
|
|
}
|
2004-04-18 09:37:16 +02:00
|
|
|
if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(circ->state == CIRCUIT_STATE_OPEN);
|
2004-04-18 09:37:16 +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.",
|
|
|
|
circ->rend_query, circ->build_state->chosen_exit);
|
|
|
|
rend_client_remove_intro_point(circ->build_state->chosen_exit, circ->rend_query);
|
|
|
|
}
|
2003-11-18 22:12:17 +01:00
|
|
|
|
2003-05-28 01:39:04 +02:00
|
|
|
if(circ->n_conn)
|
2003-11-11 04:01:48 +01:00
|
|
|
connection_send_destroy(circ->n_circ_id, circ->n_conn);
|
2004-05-06 13:08:04 +02:00
|
|
|
for(conn=circ->n_streams; conn; conn=conn->next_stream)
|
2004-02-29 01:11:37 +01:00
|
|
|
connection_edge_destroy(circ->n_circ_id, conn);
|
2004-05-06 13:08:04 +02:00
|
|
|
while(circ->resolving_streams) {
|
|
|
|
conn = circ->resolving_streams;
|
|
|
|
circ->resolving_streams = conn->next_stream;
|
|
|
|
log_fn(LOG_INFO,"Freeing resolving-conn.");
|
|
|
|
connection_free(conn);
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
}
|
2003-05-28 01:39:04 +02:00
|
|
|
if(circ->p_conn)
|
2004-04-26 05:42:41 +02:00
|
|
|
connection_send_destroy(circ->p_circ_id, circ->p_conn);
|
2004-05-06 13:08:04 +02:00
|
|
|
for(conn=circ->p_streams; conn; conn=conn->next_stream)
|
2004-02-29 01:11:37 +01:00
|
|
|
connection_edge_destroy(circ->p_circ_id, conn);
|
2004-04-01 03:57:22 +02:00
|
|
|
|
2004-03-02 18:48:17 +01:00
|
|
|
circ->marked_for_close = 1;
|
2004-03-30 21:52:42 +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;
|
|
|
|
}
|
2004-03-02 18:48:17 +01:00
|
|
|
return 0;
|
2002-06-27 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
2004-02-18 02:21:20 +01:00
|
|
|
void circuit_detach_stream(circuit_t *circ, connection_t *conn) {
|
|
|
|
connection_t *prevconn;
|
|
|
|
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(circ && conn);
|
2004-02-18 02:21:20 +01:00
|
|
|
|
|
|
|
if(conn == circ->p_streams) {
|
|
|
|
circ->p_streams = conn->next_stream;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(conn == circ->n_streams) {
|
|
|
|
circ->n_streams = conn->next_stream;
|
|
|
|
return;
|
|
|
|
}
|
2004-05-06 13:08:04 +02:00
|
|
|
if(conn == circ->resolving_streams) {
|
|
|
|
circ->resolving_streams = conn->next_stream;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(prevconn = circ->p_streams;
|
|
|
|
prevconn && prevconn->next_stream && prevconn->next_stream != conn;
|
|
|
|
prevconn = prevconn->next_stream)
|
|
|
|
;
|
2004-02-18 02:21:20 +01:00
|
|
|
if(prevconn && prevconn->next_stream) {
|
|
|
|
prevconn->next_stream = conn->next_stream;
|
|
|
|
return;
|
|
|
|
}
|
2004-05-06 13:08:04 +02:00
|
|
|
|
|
|
|
for(prevconn = circ->n_streams;
|
|
|
|
prevconn && prevconn->next_stream && prevconn->next_stream != conn;
|
|
|
|
prevconn = prevconn->next_stream)
|
|
|
|
;
|
2004-02-18 02:21:20 +01:00
|
|
|
if(prevconn && prevconn->next_stream) {
|
|
|
|
prevconn->next_stream = conn->next_stream;
|
|
|
|
return;
|
|
|
|
}
|
2004-05-06 13:08:04 +02:00
|
|
|
|
|
|
|
for(prevconn = circ->resolving_streams;
|
|
|
|
prevconn && prevconn->next_stream && prevconn->next_stream != conn;
|
|
|
|
prevconn = prevconn->next_stream)
|
|
|
|
;
|
|
|
|
if(prevconn && prevconn->next_stream) {
|
|
|
|
prevconn->next_stream = conn->next_stream;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-02-18 02:21:20 +01:00
|
|
|
log_fn(LOG_ERR,"edge conn not in circuit's list?");
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(0); /* should never get here */
|
2004-02-18 02:21:20 +01:00
|
|
|
}
|
|
|
|
|
2002-06-27 00:45:49 +02:00
|
|
|
void circuit_about_to_close_connection(connection_t *conn) {
|
|
|
|
/* send destroys for all circuits using conn */
|
|
|
|
/* currently, we assume it's too late to flush conn's buf here.
|
|
|
|
* down the road, maybe we'll consider that eof doesn't mean can't-write
|
|
|
|
*/
|
|
|
|
circuit_t *circ;
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
|
2003-10-21 10:37:07 +02:00
|
|
|
switch(conn->type) {
|
|
|
|
case CONN_TYPE_OR:
|
|
|
|
/* We must close all the circuits on it. */
|
|
|
|
while((circ = circuit_get_by_conn(conn))) {
|
|
|
|
if(circ->n_conn == conn) /* it's closing in front of us */
|
|
|
|
circ->n_conn = NULL;
|
|
|
|
if(circ->p_conn == conn) /* it's closing behind us */
|
|
|
|
circ->p_conn = NULL;
|
2004-03-02 18:48:17 +01:00
|
|
|
circuit_mark_for_close(circ);
|
2003-12-17 22:09:31 +01:00
|
|
|
}
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
return;
|
2003-10-21 10:37:07 +02:00
|
|
|
case CONN_TYPE_AP:
|
|
|
|
case CONN_TYPE_EXIT:
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
|
2003-10-21 10:37:07 +02:00
|
|
|
/* It's an edge conn. Need to remove it from the linked list of
|
|
|
|
* conn's for this circuit. Confirm that 'end' relay command has
|
|
|
|
* been sent. But don't kill the circuit.
|
|
|
|
*/
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2003-10-21 10:37:07 +02:00
|
|
|
circ = circuit_get_by_conn(conn);
|
|
|
|
if(!circ)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(!conn->has_sent_end) {
|
2004-02-27 23:00:26 +01:00
|
|
|
log_fn(LOG_WARN,"Edge connection hasn't sent end yet? Bug.");
|
|
|
|
connection_mark_for_close(conn, END_STREAM_REASON_MISC);
|
2003-10-21 10:37:07 +02:00
|
|
|
}
|
|
|
|
|
2004-02-18 02:21:20 +01:00
|
|
|
circuit_detach_stream(circ, conn);
|
|
|
|
|
2003-10-21 10:37:07 +02:00
|
|
|
} /* end switch */
|
2002-06-27 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
2004-01-20 10:21:46 +01:00
|
|
|
void circuit_log_path(int severity, circuit_t *circ) {
|
2004-03-14 23:47:11 +01:00
|
|
|
char buf[1024];
|
|
|
|
char *s = buf;
|
2004-01-20 10:21:46 +01:00
|
|
|
struct crypt_path_t *hop;
|
2004-01-30 20:31:39 +01:00
|
|
|
char *states[] = {"closed", "waiting for keys", "open"};
|
2004-01-20 10:21:46 +01:00
|
|
|
routerinfo_t *router;
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(CIRCUIT_IS_ORIGIN(circ) && circ->cpath);
|
2004-01-30 20:31:39 +01:00
|
|
|
|
2004-03-14 23:47:11 +01:00
|
|
|
snprintf(s, sizeof(buf)-1, "circ (length %d, exit %s): ",
|
2004-01-30 20:31:39 +01:00
|
|
|
circ->build_state->desired_path_len, circ->build_state->chosen_exit);
|
|
|
|
hop=circ->cpath;
|
|
|
|
do {
|
2004-03-14 23:47:11 +01:00
|
|
|
s = buf + strlen(buf);
|
2004-01-20 10:21:46 +01:00
|
|
|
router = router_get_by_addr_port(hop->addr,hop->port);
|
|
|
|
if(router) {
|
2004-03-14 23:47:11 +01:00
|
|
|
snprintf(s, sizeof(buf) - (s - buf), "%s(%s) ",
|
|
|
|
router->nickname, states[hop->state]);
|
2004-01-20 10:21:46 +01:00
|
|
|
} else {
|
2004-04-06 00:01:35 +02:00
|
|
|
if(circ->purpose == CIRCUIT_PURPOSE_C_REND_JOINED) {
|
2004-04-06 23:25:11 +02:00
|
|
|
snprintf(s, sizeof(buf) - (s - buf), "(rendjoin hop)");
|
2004-04-06 00:01:35 +02:00
|
|
|
} else {
|
|
|
|
snprintf(s, sizeof(buf) - (s - buf), "UNKNOWN ");
|
|
|
|
}
|
2004-01-20 10:21:46 +01:00
|
|
|
}
|
2004-01-30 20:31:39 +01:00
|
|
|
hop=hop->next;
|
|
|
|
} while(hop!=circ->cpath);
|
2004-03-14 23:47:11 +01:00
|
|
|
log_fn(severity,"%s",buf);
|
2004-01-20 10:21:46 +01:00
|
|
|
}
|
|
|
|
|
2004-03-20 05:59:29 +01:00
|
|
|
/* Tell the rep(utation)hist(ory) module about the status of the links
|
|
|
|
* in circ. Hops that have become OPEN are marked as successfully
|
|
|
|
* extended; the _first_ hop that isn't open (if any) is marked as
|
|
|
|
* unable to extend.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
circuit_rep_hist_note_result(circuit_t *circ)
|
|
|
|
{
|
|
|
|
struct crypt_path_t *hop;
|
|
|
|
char *prev_nickname = NULL;
|
|
|
|
routerinfo_t *router;
|
|
|
|
hop = circ->cpath;
|
2004-03-20 21:21:20 +01:00
|
|
|
if(!hop) {
|
|
|
|
/* XXX
|
|
|
|
* if !hop, then we're not the beginning of this circuit.
|
|
|
|
* for now, just forget about it. later, we should remember when
|
|
|
|
* extends-through-us failed, too.
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
}
|
2004-03-20 05:59:29 +01:00
|
|
|
if (options.ORPort) {
|
|
|
|
prev_nickname = options.Nickname;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
router = router_get_by_addr_port(hop->addr,hop->port);
|
|
|
|
if (router) {
|
|
|
|
if (prev_nickname) {
|
|
|
|
if (hop->state == CPATH_STATE_OPEN)
|
|
|
|
rep_hist_note_extend_succeeded(prev_nickname, router->nickname);
|
|
|
|
else {
|
|
|
|
rep_hist_note_extend_failed(prev_nickname, router->nickname);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prev_nickname = router->nickname;
|
|
|
|
} else {
|
|
|
|
prev_nickname = NULL;
|
|
|
|
}
|
|
|
|
hop=hop->next;
|
|
|
|
} while (hop!=circ->cpath);
|
|
|
|
}
|
|
|
|
|
2004-01-20 10:21:46 +01:00
|
|
|
static void
|
|
|
|
circuit_dump_details(int severity, circuit_t *circ, int poll_index,
|
|
|
|
char *type, int this_circid, int other_circid) {
|
2003-11-17 10:30:29 +01:00
|
|
|
struct crypt_path_t *hop;
|
|
|
|
log(severity,"Conn %d has %s circuit: circID %d (other side %d), state %d (%s), born %d",
|
|
|
|
poll_index, type, this_circid, other_circid, circ->state,
|
|
|
|
circuit_state_to_string[circ->state], (int)circ->timestamp_created);
|
2004-04-08 04:24:06 +02:00
|
|
|
if(CIRCUIT_IS_ORIGIN(circ)) { /* circ starts at this node */
|
2003-11-17 10:30:29 +01:00
|
|
|
if(circ->state == CIRCUIT_STATE_BUILDING)
|
|
|
|
log(severity,"Building: desired len %d, planned exit node %s.",
|
|
|
|
circ->build_state->desired_path_len, circ->build_state->chosen_exit);
|
|
|
|
for(hop=circ->cpath;hop->next != circ->cpath; hop=hop->next)
|
2004-03-18 20:22:56 +01:00
|
|
|
log(severity,"hop: state %d, addr 0x%.8x, port %d", hop->state,
|
2003-12-17 22:09:31 +01:00
|
|
|
(unsigned int)hop->addr,
|
2003-12-15 22:35:52 +01:00
|
|
|
(int)hop->port);
|
2003-11-17 10:30:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-15 20:28:32 +02:00
|
|
|
void circuit_dump_by_conn(connection_t *conn, int severity) {
|
2002-09-22 00:41:48 +02:00
|
|
|
circuit_t *circ;
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
connection_t *tmpconn;
|
2002-09-22 00:41:48 +02:00
|
|
|
|
|
|
|
for(circ=global_circuitlist;circ;circ = circ->next) {
|
2003-05-28 01:39:04 +02:00
|
|
|
if(circ->p_conn == conn)
|
2003-11-17 10:30:29 +01:00
|
|
|
circuit_dump_details(severity, circ, conn->poll_index, "App-ward",
|
|
|
|
circ->p_circ_id, circ->n_circ_id);
|
2003-05-28 01:39:04 +02:00
|
|
|
for(tmpconn=circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream) {
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
if(tmpconn == conn) {
|
2003-11-17 10:30:29 +01:00
|
|
|
circuit_dump_details(severity, circ, conn->poll_index, "App-ward",
|
|
|
|
circ->p_circ_id, circ->n_circ_id);
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
}
|
2002-09-22 00:41:48 +02:00
|
|
|
}
|
2003-05-28 01:39:04 +02:00
|
|
|
if(circ->n_conn == conn)
|
2003-11-17 10:30:29 +01:00
|
|
|
circuit_dump_details(severity, circ, conn->poll_index, "Exit-ward",
|
|
|
|
circ->n_circ_id, circ->p_circ_id);
|
2003-05-28 01:39:04 +02:00
|
|
|
for(tmpconn=circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream) {
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
if(tmpconn == conn) {
|
2003-11-17 10:30:29 +01:00
|
|
|
circuit_dump_details(severity, circ, conn->poll_index, "Exit-ward",
|
|
|
|
circ->n_circ_id, circ->p_circ_id);
|
major overhaul: dns slave subsystem, topics
on startup, it forks off a master dns handler, which forks off dns
slaves (like the apache model). slaves as spawned as load increases,
and then reused. excess slaves are not ever killed, currently.
implemented topics. each topic has a receive window in each direction
at each edge of the circuit, and sends sendme's at the data level, as
per before. each circuit also has receive windows in each direction at
each hop; an edge sends a circuit-level sendme as soon as enough data
cells have arrived (regardless of whether the data cells were flushed
to the exit conns). removed the 'connected' cell type, since it's now
a topic command within data cells.
at the edge of the circuit, there can be multiple connections associated
with a single circuit. you find them via the linked list conn->next_topic.
currently each new ap connection starts its own circuit, so we ought
to see comparable performance to what we had before. but that's only
because i haven't written the code to reattach to old circuits. please
try to break it as-is, and then i'll make it reuse the same circuit and
we'll try to break that.
svn:r152
2003-01-26 10:02:24 +01:00
|
|
|
}
|
2002-09-22 00:41:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-21 07:33:57 +01:00
|
|
|
/* Don't keep more than 10 unused open circuits around. */
|
|
|
|
#define MAX_UNUSED_OPEN_CIRCUITS 10
|
2004-03-21 04:03:10 +01:00
|
|
|
|
2004-04-13 07:20:52 +02:00
|
|
|
void circuit_expire_old_circuits(void) {
|
2004-03-21 07:33:57 +01:00
|
|
|
circuit_t *circ;
|
2003-11-16 22:49:52 +01:00
|
|
|
time_t now = time(NULL);
|
2004-03-21 07:33:57 +01:00
|
|
|
smartlist_t *unused_open_circs;
|
|
|
|
int i;
|
2003-04-16 08:18:31 +02:00
|
|
|
|
2004-03-31 00:59:00 +02:00
|
|
|
unused_open_circs = smartlist_create();
|
2004-03-21 07:33:57 +01:00
|
|
|
|
|
|
|
for (circ = global_circuitlist; circ; circ = circ->next) {
|
|
|
|
if (circ->marked_for_close)
|
|
|
|
continue;
|
2004-03-21 04:03:10 +01:00
|
|
|
/* If the circuit has been dirty for too long, and there are no streams
|
|
|
|
* on it, mark it for close.
|
|
|
|
*/
|
2004-03-21 07:33:57 +01:00
|
|
|
if (circ->timestamp_dirty &&
|
|
|
|
circ->timestamp_dirty + options.NewCircuitPeriod < now &&
|
2004-04-27 12:16:31 +02:00
|
|
|
!circ->p_conn && /* we're the origin */
|
2004-04-27 13:23:56 +02:00
|
|
|
!circ->p_streams /* nothing attached */ ) {
|
|
|
|
log_fn(LOG_DEBUG,"Closing n_circ_id %d (dirty %d secs ago, purp %d)",circ->n_circ_id,
|
|
|
|
(int)(now - circ->timestamp_dirty), circ->purpose);
|
2004-04-27 13:28:45 +02:00
|
|
|
/* (only general and purpose_c circs can get dirty) */
|
|
|
|
tor_assert(!circ->n_streams);
|
|
|
|
tor_assert(circ->purpose <= CIRCUIT_PURPOSE_C_REND_JOINED);
|
2004-03-21 07:33:57 +01:00
|
|
|
circuit_mark_for_close(circ);
|
2004-04-08 04:24:06 +02:00
|
|
|
} else if (!circ->timestamp_dirty && CIRCUIT_IS_ORIGIN(circ) &&
|
2004-04-27 12:16:31 +02:00
|
|
|
circ->state == CIRCUIT_STATE_OPEN &&
|
|
|
|
circ->purpose == CIRCUIT_PURPOSE_C_GENERAL) {
|
|
|
|
/* Also, gather a list of open unused general circuits that we created.
|
2004-03-21 07:33:57 +01:00
|
|
|
* Because we add elements to the front of global_circuitlist,
|
|
|
|
* the last elements of unused_open_circs will be the oldest
|
|
|
|
* ones.
|
|
|
|
*/
|
|
|
|
smartlist_add(unused_open_circs, circ);
|
2003-04-16 08:18:31 +02:00
|
|
|
}
|
|
|
|
}
|
2004-04-03 02:58:54 +02:00
|
|
|
for (i = MAX_UNUSED_OPEN_CIRCUITS; i < smartlist_len(unused_open_circs); ++i) {
|
|
|
|
circuit_t *circ = smartlist_get(unused_open_circs, i);
|
2004-04-27 13:23:56 +02:00
|
|
|
log_fn(LOG_DEBUG,"Expiring excess clean circ (n_circ_id %d, purp %d)",
|
|
|
|
circ->n_circ_id, circ->purpose);
|
2004-03-21 07:33:57 +01:00
|
|
|
circuit_mark_for_close(circ);
|
|
|
|
}
|
|
|
|
smartlist_free(unused_open_circs);
|
2003-04-16 08:18:31 +02:00
|
|
|
}
|
|
|
|
|
2004-04-05 02:47:48 +02:00
|
|
|
static void circuit_is_open(circuit_t *circ) {
|
2004-04-01 03:57:22 +02:00
|
|
|
|
|
|
|
switch(circ->purpose) {
|
2004-04-05 02:47:48 +02:00
|
|
|
case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
|
|
|
|
rend_client_rendcirc_is_open(circ);
|
2004-04-03 06:05:30 +02:00
|
|
|
break;
|
2004-04-03 01:38:26 +02:00
|
|
|
case CIRCUIT_PURPOSE_C_INTRODUCING:
|
2004-04-05 02:47:48 +02:00
|
|
|
rend_client_introcirc_is_open(circ);
|
2004-04-03 06:05:30 +02:00
|
|
|
break;
|
2004-04-05 02:47:48 +02:00
|
|
|
case CIRCUIT_PURPOSE_C_GENERAL:
|
|
|
|
/* Tell any AP connections that have been waiting for a new
|
|
|
|
* circuit that one is ready. */
|
2004-04-01 03:57:22 +02:00
|
|
|
connection_ap_attach_pending();
|
|
|
|
break;
|
|
|
|
case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
|
|
|
|
/* at Bob, waiting for introductions */
|
2004-04-02 00:21:01 +02:00
|
|
|
rend_service_intro_is_ready(circ);
|
2004-04-01 03:57:22 +02:00
|
|
|
break;
|
2004-04-02 00:21:01 +02:00
|
|
|
case CIRCUIT_PURPOSE_S_CONNECT_REND:
|
2004-04-01 03:57:22 +02:00
|
|
|
/* at Bob, connecting to rend point */
|
2004-04-02 00:21:01 +02:00
|
|
|
rend_service_rendezvous_is_ready(circ);
|
2004-04-01 03:57:22 +02:00
|
|
|
break;
|
2004-04-03 05:06:06 +02:00
|
|
|
default:
|
|
|
|
log_fn(LOG_ERR,"unhandled purpose %d",circ->purpose);
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(0);
|
2004-04-01 03:57:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-07 22:59:38 +02:00
|
|
|
/* Called whenever a circuit could not be successfully built.
|
|
|
|
*/
|
|
|
|
static void circuit_build_failed(circuit_t *circ) {
|
2004-04-01 03:57:22 +02:00
|
|
|
|
|
|
|
/* we should examine circ and see if it failed because of
|
|
|
|
* the last hop or an earlier hop. then use this info below.
|
|
|
|
*/
|
2004-04-14 23:40:50 +02:00
|
|
|
int failed_at_last_hop = 0;
|
|
|
|
/* If the last hop isn't open, and the second-to-last is, we failed
|
|
|
|
* at the last hop. */
|
|
|
|
if (circ->cpath &&
|
|
|
|
circ->cpath->prev->state != CPATH_STATE_OPEN &&
|
|
|
|
circ->cpath->prev->prev->state == CPATH_STATE_OPEN) {
|
|
|
|
failed_at_last_hop = 1;
|
|
|
|
}
|
2004-04-01 03:57:22 +02:00
|
|
|
|
|
|
|
switch(circ->purpose) {
|
|
|
|
case CIRCUIT_PURPOSE_C_GENERAL:
|
|
|
|
if (circ->state != CIRCUIT_STATE_OPEN) {
|
|
|
|
/* If we never built the circuit, note it as a failure. */
|
|
|
|
/* Note that we can't just check circ->cpath here, because if
|
|
|
|
* circuit-building failed immediately, it won't be set yet. */
|
|
|
|
circuit_increment_failure_count();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
|
|
|
|
/* at Bob, waiting for introductions */
|
2004-04-07 22:59:38 +02:00
|
|
|
if (circ->state != CIRCUIT_STATE_OPEN) {
|
|
|
|
circuit_increment_failure_count();
|
|
|
|
}
|
2004-04-07 22:23:59 +02:00
|
|
|
/* no need to care here, because bob will rebuild intro
|
|
|
|
* points periodically. */
|
2004-04-01 03:57:22 +02:00
|
|
|
break;
|
|
|
|
case CIRCUIT_PURPOSE_C_INTRODUCING:
|
|
|
|
/* at Alice, connecting to intro point */
|
2004-04-07 22:59:38 +02:00
|
|
|
/* Don't increment failure count, since Bob may have picked
|
2004-04-07 23:12:54 +02:00
|
|
|
* the introduction point maliciously */
|
2004-04-07 22:23:59 +02:00
|
|
|
/* Alice will pick a new intro point when this one dies, if
|
|
|
|
* the stream in question still cares. No need to act here. */
|
2004-04-01 03:57:22 +02:00
|
|
|
break;
|
|
|
|
case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
|
|
|
|
/* at Alice, waiting for Bob */
|
2004-04-07 22:59:38 +02:00
|
|
|
if (circ->state != CIRCUIT_STATE_OPEN) {
|
|
|
|
circuit_increment_failure_count();
|
|
|
|
}
|
2004-04-07 22:23:59 +02:00
|
|
|
/* Alice will pick a new rend point when this one dies, if
|
|
|
|
* the stream in question still cares. No need to act here. */
|
2004-04-01 03:57:22 +02:00
|
|
|
break;
|
2004-04-02 00:21:01 +02:00
|
|
|
case CIRCUIT_PURPOSE_S_CONNECT_REND:
|
2004-04-01 03:57:22 +02:00
|
|
|
/* at Bob, connecting to rend point */
|
2004-04-07 22:59:38 +02:00
|
|
|
/* Don't increment failure count, since Alice may have picked
|
2004-04-07 23:12:54 +02:00
|
|
|
* the rendezvous point maliciously */
|
2004-04-14 23:40:50 +02:00
|
|
|
if (failed_at_last_hop) {
|
|
|
|
log_fn(LOG_INFO,"Couldn't connect to Alice's chosen rend point %s. Sucks to be Alice.", circ->build_state->chosen_exit);
|
|
|
|
} else {
|
|
|
|
log_fn(LOG_INFO,"Couldn't connect to Alice's chosen rend point %s, because an earlier node failed.",
|
|
|
|
circ->build_state->chosen_exit);
|
|
|
|
rend_service_relaunch_rendezvous(circ);
|
|
|
|
}
|
2004-04-01 03:57:22 +02:00
|
|
|
break;
|
2004-04-07 22:59:38 +02:00
|
|
|
default:
|
2004-04-07 23:12:54 +02:00
|
|
|
/* Other cases are impossible, since this function is only called with
|
2004-04-07 22:59:38 +02:00
|
|
|
* unbuilt circuits. */
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(0);
|
2004-04-01 03:57:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-11-19 14:12:43 +01:00
|
|
|
/* Number of consecutive failures so far; should only be touched by
|
2003-11-19 03:22:52 +01:00
|
|
|
* circuit_launch_new and circuit_*_failure_count.
|
2003-11-19 14:12:43 +01:00
|
|
|
*/
|
2003-12-17 22:09:31 +01:00
|
|
|
static int n_circuit_failures = 0;
|
2003-11-19 03:22:52 +01:00
|
|
|
|
2004-04-06 22:16:12 +02:00
|
|
|
/* Don't retry launching a new circuit if we try this many times with no
|
|
|
|
* success. */
|
|
|
|
#define MAX_CIRCUIT_FAILURES 5
|
|
|
|
|
2004-04-01 05:23:28 +02:00
|
|
|
/* Launch a new circuit and return a pointer to it. Return NULL if you failed. */
|
2004-04-01 05:44:49 +02:00
|
|
|
circuit_t *circuit_launch_new(uint8_t purpose, const char *exit_nickname) {
|
2003-04-16 08:18:31 +02:00
|
|
|
|
2004-04-06 22:16:12 +02:00
|
|
|
if (n_circuit_failures > MAX_CIRCUIT_FAILURES) {
|
|
|
|
/* too many failed circs in a row. don't try. */
|
2003-12-13 08:42:18 +01:00
|
|
|
// log_fn(LOG_INFO,"%d failures so far, not trying.",n_circuit_failures);
|
2004-04-01 05:23:28 +02:00
|
|
|
return NULL;
|
2003-12-13 08:42:18 +01:00
|
|
|
}
|
2003-04-16 08:18:31 +02:00
|
|
|
|
2004-03-02 18:48:17 +01:00
|
|
|
/* try a circ. if it fails, circuit_mark_for_close will increment n_circuit_failures */
|
2004-04-01 05:44:49 +02:00
|
|
|
return circuit_establish_circuit(purpose, exit_nickname);
|
2003-04-16 08:18:31 +02:00
|
|
|
}
|
|
|
|
|
2003-11-19 03:22:52 +01:00
|
|
|
void circuit_increment_failure_count(void) {
|
|
|
|
++n_circuit_failures;
|
2003-12-13 08:42:18 +01:00
|
|
|
log_fn(LOG_DEBUG,"n_circuit_failures now %d.",n_circuit_failures);
|
2003-11-19 03:22:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void circuit_reset_failure_count(void) {
|
|
|
|
n_circuit_failures = 0;
|
|
|
|
}
|
|
|
|
|
2004-04-01 05:44:49 +02:00
|
|
|
static circuit_t *circuit_establish_circuit(uint8_t purpose,
|
|
|
|
const char *exit_nickname) {
|
2003-04-16 08:18:31 +02:00
|
|
|
routerinfo_t *firsthop;
|
|
|
|
connection_t *n_conn;
|
|
|
|
circuit_t *circ;
|
|
|
|
|
2003-11-11 04:01:48 +01:00
|
|
|
circ = circuit_new(0, NULL); /* sets circ->p_circ_id and circ->p_conn */
|
2003-04-16 08:18:31 +02:00
|
|
|
circ->state = CIRCUIT_STATE_OR_WAIT;
|
2004-04-01 22:33:29 +02:00
|
|
|
circ->build_state = onion_new_cpath_build_state(purpose, exit_nickname);
|
2004-04-01 03:57:22 +02:00
|
|
|
circ->purpose = purpose;
|
2003-11-16 18:00:02 +01:00
|
|
|
|
2003-11-14 21:45:47 +01:00
|
|
|
if (! circ->build_state) {
|
2004-04-01 22:33:29 +02:00
|
|
|
log_fn(LOG_INFO,"Generating cpath failed.");
|
2004-03-02 18:48:17 +01:00
|
|
|
circuit_mark_for_close(circ);
|
2004-04-01 05:23:28 +02:00
|
|
|
return NULL;
|
2003-11-12 03:55:38 +01:00
|
|
|
}
|
|
|
|
|
2003-11-14 21:45:47 +01:00
|
|
|
onion_extend_cpath(&circ->cpath, circ->build_state, &firsthop);
|
2004-04-08 04:24:06 +02:00
|
|
|
if(!CIRCUIT_IS_ORIGIN(circ)) {
|
2003-11-12 03:55:38 +01:00
|
|
|
log_fn(LOG_INFO,"Generating first cpath hop failed.");
|
2004-03-02 18:48:17 +01:00
|
|
|
circuit_mark_for_close(circ);
|
2004-04-01 05:23:28 +02:00
|
|
|
return NULL;
|
2003-05-06 01:24:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* now see if we're already connected to the first OR in 'route' */
|
2003-04-16 08:18:31 +02:00
|
|
|
|
2003-06-18 00:18:26 +02:00
|
|
|
log_fn(LOG_DEBUG,"Looking for firsthop '%s:%u'",
|
2003-04-16 08:18:31 +02:00
|
|
|
firsthop->address,firsthop->or_port);
|
|
|
|
n_conn = connection_twin_get_by_addr_port(firsthop->addr,firsthop->or_port);
|
|
|
|
if(!n_conn || n_conn->state != OR_CONN_STATE_OPEN) { /* not currently connected */
|
|
|
|
circ->n_addr = firsthop->addr;
|
|
|
|
circ->n_port = firsthop->or_port;
|
|
|
|
|
|
|
|
if(!n_conn) { /* launch the connection */
|
2003-05-28 04:03:25 +02:00
|
|
|
n_conn = connection_or_connect(firsthop);
|
2003-04-16 08:18:31 +02:00
|
|
|
if(!n_conn) { /* connect failed, forget the whole thing */
|
2003-09-26 12:03:50 +02:00
|
|
|
log_fn(LOG_INFO,"connect to firsthop failed. Closing.");
|
2004-03-02 18:48:17 +01:00
|
|
|
circuit_mark_for_close(circ);
|
2004-04-01 05:23:28 +02:00
|
|
|
return NULL;
|
2003-04-16 08:18:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-18 00:18:26 +02:00
|
|
|
log_fn(LOG_DEBUG,"connecting in progress (or finished). Good.");
|
2004-04-01 05:23:28 +02:00
|
|
|
/* return success. The onion/circuit/etc will be taken care of automatically
|
|
|
|
* (may already have been) whenever n_conn reaches OR_CONN_STATE_OPEN.
|
|
|
|
*/
|
|
|
|
return circ;
|
2003-04-16 08:18:31 +02:00
|
|
|
} else { /* it (or a twin) is already open. use it. */
|
|
|
|
circ->n_addr = n_conn->addr;
|
|
|
|
circ->n_port = n_conn->port;
|
2003-05-06 01:24:46 +02:00
|
|
|
circ->n_conn = n_conn;
|
2003-06-18 00:18:26 +02:00
|
|
|
log_fn(LOG_DEBUG,"Conn open. Delivering first onion skin.");
|
2003-05-06 01:24:46 +02:00
|
|
|
if(circuit_send_next_onion_skin(circ) < 0) {
|
2003-09-26 12:03:50 +02:00
|
|
|
log_fn(LOG_INFO,"circuit_send_next_onion_skin failed.");
|
2004-03-02 18:48:17 +01:00
|
|
|
circuit_mark_for_close(circ);
|
2004-04-01 05:23:28 +02:00
|
|
|
return NULL;
|
2003-05-06 01:24:46 +02:00
|
|
|
}
|
2003-04-16 08:18:31 +02:00
|
|
|
}
|
2004-04-01 05:23:28 +02:00
|
|
|
return circ;
|
2003-04-16 08:18:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* find circuits that are waiting on me, if any, and get them to send the onion */
|
|
|
|
void circuit_n_conn_open(connection_t *or_conn) {
|
|
|
|
circuit_t *circ;
|
|
|
|
|
2003-11-18 10:53:03 +01:00
|
|
|
for(circ=global_circuitlist;circ;circ = circ->next) {
|
2004-03-02 18:48:17 +01:00
|
|
|
if (circ->marked_for_close)
|
|
|
|
continue;
|
2004-04-08 04:24:06 +02:00
|
|
|
if(CIRCUIT_IS_ORIGIN(circ) && circ->n_addr == or_conn->addr && circ->n_port == or_conn->port) {
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(circ->state == CIRCUIT_STATE_OR_WAIT);
|
2003-11-18 10:53:03 +01:00
|
|
|
log_fn(LOG_DEBUG,"Found circ %d, sending onion skin.", circ->n_circ_id);
|
|
|
|
circ->n_conn = or_conn;
|
|
|
|
if(circuit_send_next_onion_skin(circ) < 0) {
|
|
|
|
log_fn(LOG_INFO,"send_next_onion_skin failed; circuit marked for closing.");
|
2004-03-02 18:48:17 +01:00
|
|
|
circuit_mark_for_close(circ);
|
2003-11-18 10:53:03 +01:00
|
|
|
continue;
|
2004-02-28 08:01:22 +01:00
|
|
|
/* XXX could this be bad, eg if next_onion_skin failed because conn died? */
|
2003-11-18 10:53:03 +01:00
|
|
|
}
|
2003-04-16 08:18:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-01 03:57:22 +02:00
|
|
|
extern int has_completed_circuit;
|
|
|
|
|
2003-05-06 01:24:46 +02:00
|
|
|
int circuit_send_next_onion_skin(circuit_t *circ) {
|
2003-04-16 08:18:31 +02:00
|
|
|
cell_t cell;
|
2003-05-06 01:24:46 +02:00
|
|
|
crypt_path_t *hop;
|
|
|
|
routerinfo_t *router;
|
2003-11-12 03:55:38 +01:00
|
|
|
int r;
|
2003-11-17 19:40:56 +01:00
|
|
|
int circ_id_type;
|
2004-04-06 22:16:12 +02:00
|
|
|
char payload[2+4+ONIONSKIN_CHALLENGE_LEN];
|
2003-04-16 08:18:31 +02:00
|
|
|
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(circ && CIRCUIT_IS_ORIGIN(circ));
|
2003-04-16 08:18:31 +02:00
|
|
|
|
2003-05-06 01:24:46 +02:00
|
|
|
if(circ->cpath->state == CPATH_STATE_CLOSED) {
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(circ->n_conn && circ->n_conn->type == CONN_TYPE_OR);
|
2003-12-14 05:19:12 +01:00
|
|
|
|
2003-06-18 00:18:26 +02:00
|
|
|
log_fn(LOG_DEBUG,"First skin; sending create cell.");
|
2003-11-17 19:40:56 +01:00
|
|
|
circ_id_type = decide_circ_id_type(options.Nickname,
|
|
|
|
circ->n_conn->nickname);
|
|
|
|
circ->n_circ_id = get_unique_circ_id_by_conn(circ->n_conn, circ_id_type);
|
2003-04-16 08:18:31 +02:00
|
|
|
|
2003-05-06 01:24:46 +02:00
|
|
|
memset(&cell, 0, sizeof(cell_t));
|
2003-04-16 08:18:31 +02:00
|
|
|
cell.command = CELL_CREATE;
|
2003-11-11 04:01:48 +01:00
|
|
|
cell.circ_id = circ->n_circ_id;
|
2003-05-06 01:24:46 +02:00
|
|
|
|
2004-04-25 21:04:11 +02:00
|
|
|
router = router_get_by_nickname(circ->n_conn->nickname);
|
|
|
|
if (!router) {
|
|
|
|
log_fn(LOG_WARN,"Couldn't find routerinfo for %s",
|
|
|
|
circ->n_conn->nickname);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(onion_skin_create(router->onion_pkey,
|
2004-04-01 03:57:22 +02:00
|
|
|
&(circ->cpath->handshake_state),
|
|
|
|
cell.payload) < 0) {
|
2003-10-10 03:48:32 +02:00
|
|
|
log_fn(LOG_WARN,"onion_skin_create (first hop) failed.");
|
2003-05-06 01:24:46 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2003-10-09 20:45:14 +02:00
|
|
|
connection_or_write_cell_to_buf(&cell, circ->n_conn);
|
2003-05-06 01:24:46 +02:00
|
|
|
|
|
|
|
circ->cpath->state = CPATH_STATE_AWAITING_KEYS;
|
|
|
|
circ->state = CIRCUIT_STATE_BUILDING;
|
2003-06-18 00:18:26 +02:00
|
|
|
log_fn(LOG_DEBUG,"first skin; finished sending create cell.");
|
2003-05-06 01:24:46 +02:00
|
|
|
} else {
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(circ->cpath->state == CPATH_STATE_OPEN);
|
|
|
|
tor_assert(circ->state == CIRCUIT_STATE_BUILDING);
|
2003-06-18 00:18:26 +02:00
|
|
|
log_fn(LOG_DEBUG,"starting to send subsequent skin.");
|
2003-11-14 21:45:47 +01:00
|
|
|
r = onion_extend_cpath(&circ->cpath, circ->build_state, &router);
|
2003-11-12 04:01:38 +01:00
|
|
|
if (r==1) {
|
2003-11-12 03:55:38 +01:00
|
|
|
/* done building the circuit. whew. */
|
2003-05-06 01:24:46 +02:00
|
|
|
circ->state = CIRCUIT_STATE_OPEN;
|
2003-11-14 21:45:47 +01:00
|
|
|
log_fn(LOG_INFO,"circuit built!");
|
2003-11-19 03:22:52 +01:00
|
|
|
circuit_reset_failure_count();
|
2004-04-01 03:57:22 +02:00
|
|
|
if(!has_completed_circuit) {
|
|
|
|
has_completed_circuit=1;
|
|
|
|
log_fn(LOG_NOTICE,"Tor has successfully opened a circuit. Looks like it's working.");
|
|
|
|
}
|
|
|
|
circuit_rep_hist_note_result(circ);
|
2004-04-05 02:47:48 +02:00
|
|
|
circuit_is_open(circ); /* do other actions as necessary */
|
2003-05-06 01:24:46 +02:00
|
|
|
return 0;
|
2003-11-12 20:34:34 +01:00
|
|
|
} else if (r<0) {
|
2003-11-18 08:48:00 +01:00
|
|
|
log_fn(LOG_INFO,"Unable to extend circuit path.");
|
2003-11-12 03:55:38 +01:00
|
|
|
return -1;
|
2003-05-06 01:24:46 +02:00
|
|
|
}
|
2003-11-12 03:55:38 +01:00
|
|
|
hop = circ->cpath->prev;
|
2003-05-06 01:24:46 +02:00
|
|
|
|
2003-12-17 06:58:30 +01:00
|
|
|
*(uint32_t*)payload = htonl(hop->addr);
|
|
|
|
*(uint16_t*)(payload+4) = htons(hop->port);
|
|
|
|
if(onion_skin_create(router->onion_pkey, &(hop->handshake_state), payload+6) < 0) {
|
2003-10-10 03:48:32 +02:00
|
|
|
log_fn(LOG_WARN,"onion_skin_create failed.");
|
2003-05-06 01:24:46 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2003-06-18 00:18:26 +02:00
|
|
|
log_fn(LOG_DEBUG,"Sending extend relay cell.");
|
2003-12-17 06:58:30 +01:00
|
|
|
/* send it to hop->prev, because it will transfer
|
|
|
|
* it to a create cell and then send to hop */
|
|
|
|
if(connection_edge_send_command(NULL, circ, RELAY_COMMAND_EXTEND,
|
|
|
|
payload, sizeof(payload), hop->prev) < 0)
|
2003-12-23 08:45:31 +01:00
|
|
|
return 0; /* circuit is closed */
|
2003-12-17 06:58:30 +01:00
|
|
|
|
2003-05-06 01:24:46 +02:00
|
|
|
hop->state = CPATH_STATE_AWAITING_KEYS;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-09-18 10:11:31 +02:00
|
|
|
/* take the 'extend' cell, pull out addr/port plus the onion skin. Make
|
|
|
|
* sure we're connected to the next hop, and pass it the onion skin in
|
|
|
|
* a create cell.
|
2003-05-06 01:24:46 +02:00
|
|
|
*/
|
|
|
|
int circuit_extend(cell_t *cell, circuit_t *circ) {
|
|
|
|
connection_t *n_conn;
|
2003-11-11 04:01:48 +01:00
|
|
|
int circ_id_type;
|
2003-05-06 01:24:46 +02:00
|
|
|
cell_t newcell;
|
|
|
|
|
2003-05-28 01:39:04 +02:00
|
|
|
if(circ->n_conn) {
|
2003-10-10 03:48:32 +02:00
|
|
|
log_fn(LOG_WARN,"n_conn already set. Bug/attack. Closing.");
|
2003-05-28 01:39:04 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-04-03 05:07:25 +02:00
|
|
|
circ->n_addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE));
|
|
|
|
circ->n_port = ntohs(get_uint16(cell->payload+RELAY_HEADER_SIZE+4));
|
2003-05-06 01:24:46 +02:00
|
|
|
|
|
|
|
n_conn = connection_twin_get_by_addr_port(circ->n_addr,circ->n_port);
|
|
|
|
if(!n_conn || n_conn->type != CONN_TYPE_OR) {
|
2004-02-28 08:01:22 +01:00
|
|
|
/* I've disabled making connections through OPs, but it's definitely
|
|
|
|
* possible here. I'm not sure if it would be a bug or a feature.
|
|
|
|
*
|
|
|
|
* Note also that this will close circuits where the onion has the same
|
|
|
|
* router twice in a row in the path. I think that's ok.
|
2003-05-06 01:24:46 +02:00
|
|
|
*/
|
2003-08-11 22:22:48 +02:00
|
|
|
struct in_addr in;
|
|
|
|
in.s_addr = htonl(circ->n_addr);
|
2003-11-17 08:43:03 +01:00
|
|
|
log_fn(LOG_INFO,"Next router (%s:%d) not connected. Closing.", inet_ntoa(in), circ->n_port);
|
2004-04-26 06:32:01 +02:00
|
|
|
#if 0 /* if we do truncateds, no need to kill circ */
|
2003-11-17 08:43:03 +01:00
|
|
|
connection_edge_send_command(NULL, circ, RELAY_COMMAND_TRUNCATED,
|
|
|
|
NULL, 0, NULL);
|
|
|
|
return 0;
|
2004-04-26 06:32:01 +02:00
|
|
|
#endif
|
2004-05-02 05:32:00 +02:00
|
|
|
circuit_mark_for_close(circ);
|
|
|
|
return 0;
|
2003-05-06 01:24:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
circ->n_addr = n_conn->addr; /* these are different if we found a twin instead */
|
|
|
|
circ->n_port = n_conn->port;
|
|
|
|
|
|
|
|
circ->n_conn = n_conn;
|
2003-06-18 00:18:26 +02:00
|
|
|
log_fn(LOG_DEBUG,"n_conn is %s:%u",n_conn->address,n_conn->port);
|
2003-05-06 01:24:46 +02:00
|
|
|
|
2003-11-11 04:01:48 +01:00
|
|
|
circ_id_type = decide_circ_id_type(options.Nickname, n_conn->nickname);
|
2003-05-06 01:24:46 +02:00
|
|
|
|
2004-02-28 08:01:22 +01:00
|
|
|
// log_fn(LOG_DEBUG,"circ_id_type = %u.",circ_id_type);
|
2003-11-17 08:24:01 +01:00
|
|
|
circ->n_circ_id = get_unique_circ_id_by_conn(circ->n_conn, circ_id_type);
|
2003-11-11 04:01:48 +01:00
|
|
|
if(!circ->n_circ_id) {
|
|
|
|
log_fn(LOG_WARN,"failed to get unique circID.");
|
2003-05-06 01:24:46 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2003-11-11 04:01:48 +01:00
|
|
|
log_fn(LOG_DEBUG,"Chosen circID %u.",circ->n_circ_id);
|
2003-05-06 01:24:46 +02:00
|
|
|
|
|
|
|
memset(&newcell, 0, sizeof(cell_t));
|
|
|
|
newcell.command = CELL_CREATE;
|
2003-11-11 04:01:48 +01:00
|
|
|
newcell.circ_id = circ->n_circ_id;
|
2003-05-06 01:24:46 +02:00
|
|
|
|
2004-04-06 22:16:12 +02:00
|
|
|
memcpy(newcell.payload, cell->payload+RELAY_HEADER_SIZE+2+4,
|
2003-12-16 09:21:58 +01:00
|
|
|
ONIONSKIN_CHALLENGE_LEN);
|
2003-05-06 01:24:46 +02:00
|
|
|
|
2003-10-09 20:45:14 +02:00
|
|
|
connection_or_write_cell_to_buf(&newcell, circ->n_conn);
|
2003-05-06 01:24:46 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-04-02 00:21:01 +02:00
|
|
|
/* Initialize cpath->{f|b}_{crypto|digest} from the key material in
|
|
|
|
* key_data. key_data must contain CPATH_KEY_MATERIAL bytes, which are
|
|
|
|
* used as follows:
|
|
|
|
* 20 to initialize f_digest
|
|
|
|
* 20 to initialize b_digest
|
|
|
|
* 16 to key f_crypto
|
|
|
|
* 16 to key b_crypto
|
2004-04-05 22:53:04 +02:00
|
|
|
*
|
|
|
|
* (If 'reverse' is true, then f_XX and b_XX are swapped.)
|
2004-04-02 00:21:01 +02:00
|
|
|
*/
|
2004-04-05 22:53:04 +02:00
|
|
|
int circuit_init_cpath_crypto(crypt_path_t *cpath, char *key_data, int reverse)
|
2004-04-02 00:21:01 +02:00
|
|
|
{
|
2004-04-05 22:53:04 +02:00
|
|
|
crypto_digest_env_t *tmp_digest;
|
|
|
|
crypto_cipher_env_t *tmp_crypto;
|
|
|
|
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(cpath && key_data);
|
|
|
|
tor_assert(!(cpath->f_crypto || cpath->b_crypto ||
|
|
|
|
cpath->f_digest || cpath->b_digest));
|
2003-05-06 01:24:46 +02:00
|
|
|
|
2004-04-02 00:21:01 +02:00
|
|
|
log_fn(LOG_DEBUG,"hop init digest forward 0x%.8x, backward 0x%.8x.",
|
|
|
|
(unsigned int)*(uint32_t*)key_data, (unsigned int)*(uint32_t*)(key_data+20));
|
2004-04-03 04:40:30 +02:00
|
|
|
cpath->f_digest = crypto_new_digest_env();
|
|
|
|
crypto_digest_add_bytes(cpath->f_digest, key_data, DIGEST_LEN);
|
|
|
|
cpath->b_digest = crypto_new_digest_env();
|
|
|
|
crypto_digest_add_bytes(cpath->b_digest, key_data+DIGEST_LEN, DIGEST_LEN);
|
2004-04-02 00:21:01 +02:00
|
|
|
|
|
|
|
log_fn(LOG_DEBUG,"hop init cipher forward 0x%.8x, backward 0x%.8x.",
|
2004-04-03 04:40:30 +02:00
|
|
|
(unsigned int)*(uint32_t*)(key_data+40), (unsigned int)*(uint32_t*)(key_data+40+16));
|
2004-04-06 22:16:12 +02:00
|
|
|
if (!(cpath->f_crypto =
|
2004-04-28 22:31:32 +02:00
|
|
|
crypto_create_init_cipher(key_data+(2*DIGEST_LEN),1))) {
|
2004-04-02 00:21:01 +02:00
|
|
|
log(LOG_WARN,"forward cipher initialization failed.");
|
|
|
|
return -1;
|
|
|
|
}
|
2004-04-06 22:16:12 +02:00
|
|
|
if (!(cpath->b_crypto =
|
2004-04-28 22:31:32 +02:00
|
|
|
crypto_create_init_cipher(key_data+(2*DIGEST_LEN)+CIPHER_KEY_LEN,0))) {
|
2004-04-02 00:21:01 +02:00
|
|
|
log(LOG_WARN,"backward cipher initialization failed.");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-04-05 22:53:04 +02:00
|
|
|
if (reverse) {
|
|
|
|
tmp_digest = cpath->f_digest;
|
|
|
|
cpath->f_digest = cpath->b_digest;
|
|
|
|
cpath->b_digest = tmp_digest;
|
|
|
|
tmp_crypto = cpath->f_crypto;
|
|
|
|
cpath->f_crypto = cpath->b_crypto;
|
|
|
|
cpath->b_crypto = tmp_crypto;
|
|
|
|
}
|
|
|
|
|
2004-04-02 00:21:01 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int circuit_finish_handshake(circuit_t *circ, char *reply) {
|
|
|
|
unsigned char keys[CPATH_KEY_MATERIAL_LEN];
|
|
|
|
crypt_path_t *hop;
|
|
|
|
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(CIRCUIT_IS_ORIGIN(circ));
|
2003-05-06 01:24:46 +02:00
|
|
|
if(circ->cpath->state == CPATH_STATE_AWAITING_KEYS)
|
|
|
|
hop = circ->cpath;
|
|
|
|
else {
|
|
|
|
for(hop=circ->cpath->next;
|
|
|
|
hop != circ->cpath && hop->state == CPATH_STATE_OPEN;
|
|
|
|
hop=hop->next) ;
|
|
|
|
if(hop == circ->cpath) { /* got an extended when we're all done? */
|
2003-10-10 03:48:32 +02:00
|
|
|
log_fn(LOG_WARN,"got extended when circ already built? Closing.");
|
2003-09-14 10:17:14 +02:00
|
|
|
return -1;
|
2003-05-06 01:24:46 +02:00
|
|
|
}
|
|
|
|
}
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(hop->state == CPATH_STATE_AWAITING_KEYS);
|
2003-05-06 01:24:46 +02:00
|
|
|
|
2004-04-06 22:16:12 +02:00
|
|
|
if(onion_skin_client_handshake(hop->handshake_state, reply, keys,
|
|
|
|
DIGEST_LEN*2+CIPHER_KEY_LEN*2) < 0) {
|
2003-10-10 03:48:32 +02:00
|
|
|
log_fn(LOG_WARN,"onion_skin_client_handshake failed.");
|
2003-05-06 01:24:46 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
crypto_dh_free(hop->handshake_state); /* don't need it anymore */
|
|
|
|
hop->handshake_state = NULL;
|
2004-04-02 00:21:01 +02:00
|
|
|
/* Remember hash of g^xy */
|
2004-04-03 04:40:30 +02:00
|
|
|
memcpy(hop->handshake_digest, reply+DH_KEY_LEN, DIGEST_LEN);
|
2003-05-06 01:24:46 +02:00
|
|
|
|
2004-04-05 22:53:04 +02:00
|
|
|
if (circuit_init_cpath_crypto(hop, keys, 0)<0) {
|
2003-05-06 01:24:46 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2003-04-16 08:18:31 +02:00
|
|
|
|
2003-05-06 01:24:46 +02:00
|
|
|
hop->state = CPATH_STATE_OPEN;
|
2003-09-26 12:03:50 +02:00
|
|
|
log_fn(LOG_INFO,"finished");
|
2004-02-28 08:01:22 +01:00
|
|
|
circuit_log_path(LOG_INFO,circ);
|
2003-04-16 08:18:31 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-06-12 12:16:33 +02:00
|
|
|
int circuit_truncated(circuit_t *circ, crypt_path_t *layer) {
|
|
|
|
crypt_path_t *victim;
|
|
|
|
connection_t *stream;
|
|
|
|
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(circ && CIRCUIT_IS_ORIGIN(circ));
|
|
|
|
tor_assert(layer);
|
2003-06-12 12:16:33 +02:00
|
|
|
|
2003-12-09 03:06:58 +01:00
|
|
|
/* XXX Since we don't ask for truncates currently, getting a truncated
|
|
|
|
* means that a connection broke or an extend failed. For now,
|
|
|
|
* just give up.
|
|
|
|
*/
|
2004-03-02 18:48:17 +01:00
|
|
|
circuit_mark_for_close(circ);
|
2003-12-09 03:06:58 +01:00
|
|
|
return 0;
|
|
|
|
|
2003-06-12 12:16:33 +02:00
|
|
|
while(layer->next != circ->cpath) {
|
|
|
|
/* we need to clear out layer->next */
|
|
|
|
victim = layer->next;
|
2003-06-18 00:18:26 +02:00
|
|
|
log_fn(LOG_DEBUG, "Killing a layer of the cpath.");
|
2003-06-12 12:16:33 +02:00
|
|
|
|
|
|
|
for(stream = circ->p_streams; stream; stream=stream->next_stream) {
|
|
|
|
if(stream->cpath_layer == victim) {
|
2003-12-19 06:09:51 +01:00
|
|
|
log_fn(LOG_INFO, "Marking stream %d for close.", stream->stream_id);
|
2003-10-21 10:37:07 +02:00
|
|
|
/* no need to send 'end' relay cells,
|
|
|
|
* because the other side's already dead
|
|
|
|
*/
|
2004-02-27 23:00:26 +01:00
|
|
|
connection_mark_for_close(stream,0);
|
2003-06-12 12:16:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
layer->next = victim->next;
|
|
|
|
circuit_free_cpath_node(victim);
|
|
|
|
}
|
|
|
|
|
2003-09-26 12:03:50 +02:00
|
|
|
log_fn(LOG_INFO, "finished");
|
2003-06-12 12:16:33 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
cleanups, bugfixes, more verbose logs
Fixed up the assert_*_ok funcs some (more work remains)
Changed config so it reads either /etc/torrc or the -f arg, never both
Finally tracked down a nasty bug with our use of tls:
It turns out that if you ask SSL_read() for no more than n bytes, it
will read the entire record from the network (and maybe part of the next
record, I'm not sure), give you n bytes of it, and keep the remaining
bytes internally. This is fine, except our poll-for-read looks at the
network, and there are no bytes pending on the network, so we never know
to ask SSL_read() for more bytes. Currently I've hacked it so if we ask
for n bytes and it returns n bytes, then it reads again right then. This
will interact poorly with our rate limiting; we need a cleaner solution.
svn:r481
2003-09-24 23:24:52 +02:00
|
|
|
void assert_cpath_layer_ok(const crypt_path_t *cp)
|
2003-09-16 21:36:19 +02:00
|
|
|
{
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(cp->f_crypto);
|
|
|
|
tor_assert(cp->b_crypto);
|
|
|
|
// tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
|
|
|
|
// tor_assert(cp->port);
|
2003-12-17 22:09:31 +01:00
|
|
|
switch(cp->state)
|
2003-09-16 21:36:19 +02:00
|
|
|
{
|
|
|
|
case CPATH_STATE_CLOSED:
|
|
|
|
case CPATH_STATE_OPEN:
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(!cp->handshake_state);
|
cleanups, bugfixes, more verbose logs
Fixed up the assert_*_ok funcs some (more work remains)
Changed config so it reads either /etc/torrc or the -f arg, never both
Finally tracked down a nasty bug with our use of tls:
It turns out that if you ask SSL_read() for no more than n bytes, it
will read the entire record from the network (and maybe part of the next
record, I'm not sure), give you n bytes of it, and keep the remaining
bytes internally. This is fine, except our poll-for-read looks at the
network, and there are no bytes pending on the network, so we never know
to ask SSL_read() for more bytes. Currently I've hacked it so if we ask
for n bytes and it returns n bytes, then it reads again right then. This
will interact poorly with our rate limiting; we need a cleaner solution.
svn:r481
2003-09-24 23:24:52 +02:00
|
|
|
break;
|
2003-09-16 21:36:19 +02:00
|
|
|
case CPATH_STATE_AWAITING_KEYS:
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(cp->handshake_state);
|
cleanups, bugfixes, more verbose logs
Fixed up the assert_*_ok funcs some (more work remains)
Changed config so it reads either /etc/torrc or the -f arg, never both
Finally tracked down a nasty bug with our use of tls:
It turns out that if you ask SSL_read() for no more than n bytes, it
will read the entire record from the network (and maybe part of the next
record, I'm not sure), give you n bytes of it, and keep the remaining
bytes internally. This is fine, except our poll-for-read looks at the
network, and there are no bytes pending on the network, so we never know
to ask SSL_read() for more bytes. Currently I've hacked it so if we ask
for n bytes and it returns n bytes, then it reads again right then. This
will interact poorly with our rate limiting; we need a cleaner solution.
svn:r481
2003-09-24 23:24:52 +02:00
|
|
|
break;
|
2003-09-16 21:36:19 +02:00
|
|
|
default:
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(0);
|
2003-09-16 21:36:19 +02:00
|
|
|
}
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(cp->package_window >= 0);
|
|
|
|
tor_assert(cp->deliver_window >= 0);
|
2003-09-16 21:36:19 +02:00
|
|
|
}
|
|
|
|
|
cleanups, bugfixes, more verbose logs
Fixed up the assert_*_ok funcs some (more work remains)
Changed config so it reads either /etc/torrc or the -f arg, never both
Finally tracked down a nasty bug with our use of tls:
It turns out that if you ask SSL_read() for no more than n bytes, it
will read the entire record from the network (and maybe part of the next
record, I'm not sure), give you n bytes of it, and keep the remaining
bytes internally. This is fine, except our poll-for-read looks at the
network, and there are no bytes pending on the network, so we never know
to ask SSL_read() for more bytes. Currently I've hacked it so if we ask
for n bytes and it returns n bytes, then it reads again right then. This
will interact poorly with our rate limiting; we need a cleaner solution.
svn:r481
2003-09-24 23:24:52 +02:00
|
|
|
void assert_cpath_ok(const crypt_path_t *cp)
|
2003-09-16 21:36:19 +02:00
|
|
|
{
|
|
|
|
while(cp->prev)
|
|
|
|
cp = cp->prev;
|
|
|
|
|
|
|
|
while(cp->next) {
|
|
|
|
assert_cpath_layer_ok(cp);
|
|
|
|
/* layers must be in sequence of: "open* awaiting? closed*" */
|
|
|
|
if (cp->prev) {
|
|
|
|
if (cp->prev->state == CPATH_STATE_OPEN) {
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(cp->state == CPATH_STATE_CLOSED ||
|
|
|
|
cp->state == CPATH_STATE_AWAITING_KEYS);
|
2003-09-16 21:36:19 +02:00
|
|
|
} else {
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(cp->state == CPATH_STATE_CLOSED);
|
2003-09-16 21:36:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
cp = cp->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-17 22:09:31 +01:00
|
|
|
void assert_circuit_ok(const circuit_t *c)
|
2003-09-16 21:36:19 +02:00
|
|
|
{
|
|
|
|
connection_t *conn;
|
|
|
|
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(c);
|
|
|
|
tor_assert(c->magic == CIRCUIT_MAGIC);
|
|
|
|
tor_assert(c->purpose >= _CIRCUIT_PURPOSE_MIN &&
|
|
|
|
c->purpose <= _CIRCUIT_PURPOSE_MAX);
|
2004-03-02 18:48:17 +01:00
|
|
|
|
2004-03-03 03:24:17 +01:00
|
|
|
if (c->n_conn)
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(c->n_conn->type == CONN_TYPE_OR);
|
2003-09-16 21:36:19 +02:00
|
|
|
if (c->p_conn)
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(c->p_conn->type == CONN_TYPE_OR);
|
2003-09-16 21:36:19 +02:00
|
|
|
for (conn = c->p_streams; conn; conn = conn->next_stream)
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(conn->type == CONN_TYPE_AP);
|
2003-09-16 21:36:19 +02:00
|
|
|
for (conn = c->n_streams; conn; conn = conn->next_stream)
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(conn->type == CONN_TYPE_EXIT);
|
2003-09-16 21:36:19 +02:00
|
|
|
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(c->deliver_window >= 0);
|
|
|
|
tor_assert(c->package_window >= 0);
|
2003-09-16 21:36:19 +02:00
|
|
|
if (c->state == CIRCUIT_STATE_OPEN) {
|
|
|
|
if (c->cpath) {
|
2004-04-25 22:37:37 +02:00
|
|
|
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);
|
2003-09-16 21:36:19 +02:00
|
|
|
} else {
|
2004-04-25 22:37:37 +02:00
|
|
|
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);
|
2003-09-16 21:36:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (c->cpath) {
|
2004-03-03 05:11:18 +01:00
|
|
|
//XXX assert_cpath_ok(c->cpath);
|
2003-09-16 21:36:19 +02:00
|
|
|
}
|
2004-03-30 21:52:42 +02:00
|
|
|
if (c->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED) {
|
|
|
|
if (!c->marked_for_close) {
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(c->rend_splice);
|
|
|
|
tor_assert(c->rend_splice->rend_splice == c);
|
2004-03-30 21:52:42 +02:00
|
|
|
}
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(c->rend_splice != c);
|
2004-03-30 21:52:42 +02:00
|
|
|
} else {
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(!c->rend_splice);
|
2004-03-30 21:52:42 +02:00
|
|
|
}
|
2003-09-16 21:36:19 +02:00
|
|
|
}
|
|
|
|
|
2003-04-07 04:12:02 +02:00
|
|
|
/*
|
|
|
|
Local Variables:
|
|
|
|
mode:c
|
|
|
|
indent-tabs-mode:nil
|
|
|
|
c-basic-offset:2
|
|
|
|
End:
|
|
|
|
*/
|