Only allocate some TCP connections for onion use when needed.

Added a function to enable and disable TCP onion connections.
This commit is contained in:
irungentoo 2015-04-21 15:30:41 -04:00
parent 0a0ed45202
commit e1a98987ff
No known key found for this signature in database
GPG Key ID: 10349DC9BED89E98
4 changed files with 89 additions and 3 deletions

View File

@ -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)

View File

@ -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.

View File

@ -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();
}

View File

@ -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];