From e1a98987ffeea04dd075be760af7621824fdc30a Mon Sep 17 00:00:00 2001 From: irungentoo Date: Tue, 21 Apr 2015 15:30:41 -0400 Subject: [PATCH] Only allocate some TCP connections for onion use when needed. Added a function to enable and disable TCP onion connections. --- toxcore/TCP_connection.c | 69 +++++++++++++++++++++++++++++++++++++++- toxcore/TCP_connection.h | 10 ++++++ toxcore/onion_client.c | 11 ++++++- toxcore/onion_client.h | 2 +- 4 files changed, 89 insertions(+), 3 deletions(-) diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c index d2aa3d96..08cb728c 100644 --- a/toxcore/TCP_connection.c +++ b/toxcore/TCP_connection.c @@ -997,7 +997,7 @@ static int tcp_relay_on_online(TCP_Connections *tcp_c, int tcp_connections_numbe tcp_con->connected_time = 0; } - if (tcp_c->onion_num_conns < NUM_ONION_TCP_CONNECTIONS) { + if (tcp_c->onion_status && tcp_c->onion_num_conns < NUM_ONION_TCP_CONNECTIONS) { tcp_con->onion = 1; ++tcp_c->onion_num_conns; } @@ -1156,6 +1156,73 @@ unsigned int tcp_copy_connected_relays(TCP_Connections *tcp_c, Node_format *tcp_ return copied; } +/* Set if we want TCP_connection to allocate some connection for onion use. + * + * If status is 1, allocate some connections. if status is 0, don't. + * + * return 0 on success. + * return -1 on failure. + */ +int set_tcp_onion_status(TCP_Connections *tcp_c, _Bool status) +{ + if (tcp_c->onion_status == status) + return -1; + + if (status) { + unsigned int i; + + for (i = 0; i < tcp_c->tcp_connections_length; ++i) { + TCP_con *tcp_con = get_tcp_connection(tcp_c, i); + + if (tcp_con) { + if (tcp_con->status == TCP_CONN_CONNECTED && !tcp_con->onion) { + ++tcp_c->onion_num_conns; + tcp_con->onion = 1; + } + } + + if (tcp_c->onion_num_conns >= NUM_ONION_TCP_CONNECTIONS) + break; + } + + if (tcp_c->onion_num_conns < NUM_ONION_TCP_CONNECTIONS) { + unsigned int wakeup = NUM_ONION_TCP_CONNECTIONS - tcp_c->onion_num_conns; + + for (i = 0; i < tcp_c->tcp_connections_length; ++i) { + TCP_con *tcp_con = get_tcp_connection(tcp_c, i); + + if (tcp_con) { + if (tcp_con->status == TCP_CONN_SLEEPING) { + tcp_con->unsleep = 1; + } + } + + if (!wakeup) + break; + } + } + + tcp_c->onion_status = 1; + } else { + unsigned int i; + + for (i = 0; i < tcp_c->tcp_connections_length; ++i) { + TCP_con *tcp_con = get_tcp_connection(tcp_c, i); + + if (tcp_con) { + if (tcp_con->onion) { + --tcp_c->onion_num_conns; + tcp_con->onion = 0; + } + } + } + + tcp_c->onion_status = 0; + } + + return 0; +} + TCP_Connections *new_tcp_connections(DHT *dht, TCP_Proxy_Info *proxy_info) { if (dht == NULL) diff --git a/toxcore/TCP_connection.h b/toxcore/TCP_connection.h index c3cf4889..3f7b616d 100644 --- a/toxcore/TCP_connection.h +++ b/toxcore/TCP_connection.h @@ -99,6 +99,7 @@ typedef struct { TCP_Proxy_Info proxy_info; + _Bool onion_status; uint16_t onion_num_conns; } TCP_Connections; @@ -127,6 +128,15 @@ int get_random_tcp_onion_conn_number(TCP_Connections *tcp_c); int tcp_send_onion_request(TCP_Connections *tcp_c, unsigned int tcp_connections_number, const uint8_t *data, uint16_t length); +/* Set if we want TCP_connection to allocate some connection for onion use. + * + * If status is 1, allocate some connections. if status is 0, don't. + * + * return 0 on success. + * return -1 on failure. + */ +int set_tcp_onion_status(TCP_Connections *tcp_c, _Bool status); + /* Send an oob packet via the TCP relay corresponding to tcp_connections_number. * * return 0 on success. diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index 8ab62da2..ed328fb3 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c @@ -1425,7 +1425,6 @@ void do_onion_client(Onion_Client *onion_c) ++onion_c->onion_connected; } - onion_c->UDP_connected = DHT_non_lan_connected(onion_c->dht); } else { populate_path_nodes_tcp(onion_c); @@ -1434,12 +1433,22 @@ void do_onion_client(Onion_Client *onion_c) } } + onion_c->UDP_connected = DHT_non_lan_connected(onion_c->dht); + + if (is_timeout(onion_c->first_run, ONION_CONNECTION_SECONDS)) { + set_tcp_onion_status(onion_c->c->tcp_c, !onion_c->UDP_connected); + } + if (onion_connection_status(onion_c)) { for (i = 0; i < onion_c->num_friends; ++i) { do_friend(onion_c, i); } } + if (onion_c->last_run == 0) { + onion_c->first_run = unix_time(); + } + onion_c->last_run = unix_time(); } diff --git a/toxcore/onion_client.h b/toxcore/onion_client.h index e10a86c5..ad28ac51 100644 --- a/toxcore/onion_client.h +++ b/toxcore/onion_client.h @@ -136,7 +136,7 @@ typedef struct { Onion_Client_Paths onion_paths_friends; uint8_t secret_symmetric_key[crypto_box_KEYBYTES]; - uint64_t last_run; + uint64_t last_run, first_run; uint8_t temp_public_key[crypto_box_PUBLICKEYBYTES]; uint8_t temp_secret_key[crypto_box_SECRETKEYBYTES];