diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c index ab18daac..574ae3aa 100644 --- a/toxcore/TCP_connection.c +++ b/toxcore/TCP_connection.c @@ -274,13 +274,14 @@ int send_packet_tcp_connection(TCP_Connections *tcp_c, int connections_number, c * return TCP connection number on success. * return -1 on failure. */ -int get_random_tcp_conn_number(TCP_Connections *tcp_c) +int get_random_tcp_onion_conn_number(TCP_Connections *tcp_c) { unsigned int i, r = rand(); for (i = 0; i < tcp_c->tcp_connections_length; ++i) { - if (tcp_c->tcp_connections[(i + r) % tcp_c->tcp_connections_length].status == TCP_CONN_CONNECTED) { - return ((i + r) % tcp_c->tcp_connections_length); + unsigned int index = ((i + r) % tcp_c->tcp_connections_length); + if (tcp_c->tcp_connections[index].onion && tcp_c->tcp_connections[index].status == TCP_CONN_CONNECTED) { + return index; } } @@ -579,6 +580,10 @@ static int kill_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connections } } + if (tcp_con->onion) { + --tcp_c->onion_num_conns; + } + kill_TCP_connection(tcp_con->connection); return wipe_tcp_connection(tcp_c, tcp_connections_number); @@ -777,6 +782,11 @@ 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) { + tcp_con->onion = 1; + ++tcp_c->onion_num_conns; + } + return 0; } @@ -978,7 +988,7 @@ static void kill_nonused_tcp(TCP_Connections *tcp_c) if (tcp_con) { if (tcp_con->status == TCP_CONN_CONNECTED) { - if (!tcp_con->lock_count && is_timeout(tcp_con->connected_time, TCP_CONNECTION_ANNOUNCE_TIMEOUT)) { + if (!tcp_con->onion && !tcp_con->lock_count && is_timeout(tcp_con->connected_time, TCP_CONNECTION_ANNOUNCE_TIMEOUT)) { to_kill[num_kill] = i; ++num_kill; } diff --git a/toxcore/TCP_connection.h b/toxcore/TCP_connection.h index 07d7820c..9b82e575 100644 --- a/toxcore/TCP_connection.h +++ b/toxcore/TCP_connection.h @@ -43,6 +43,9 @@ NOTE: Must be at most (MAX_FRIEND_TCP_CONNECTIONS / 2) */ #define RECOMMENDED_FRIEND_TCP_CONNECTIONS (MAX_FRIEND_TCP_CONNECTIONS / 2) +/* Number of TCP connections used for onion purposes. */ +#define NUM_ONION_TCP_CONNECTIONS RECOMMENDED_FRIEND_TCP_CONNECTIONS + typedef struct { uint8_t status; uint8_t public_key[crypto_box_PUBLICKEYBYTES]; /* The dht public key of the peer */ @@ -61,6 +64,7 @@ typedef struct { TCP_Client_Connection *connection; uint64_t connected_time; uint32_t lock_count; + _Bool onion; } TCP_con; typedef struct { @@ -83,6 +87,8 @@ typedef struct { void *tcp_onion_callback_object; TCP_Proxy_Info proxy_info; + + uint16_t onion_num_conns; } TCP_Connections; /* Send a packet to the TCP connection. @@ -100,7 +106,7 @@ int send_packet_tcp_connection(TCP_Connections *tcp_c, int connections_number, c * return TCP connection number on success. * return -1 on failure. */ -int get_random_tcp_conn_number(TCP_Connections *tcp_c); +int get_random_tcp_onion_conn_number(TCP_Connections *tcp_c); /* Send an onion packet via the TCP relay corresponding to tcp_connections_number. * diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index c93f4f41..784e8102 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -1785,7 +1785,7 @@ int add_tcp_relay(Net_Crypto *c, IP_Port ip_port, const uint8_t *public_key) int get_random_tcp_con_number(Net_Crypto *c) { pthread_mutex_lock(&c->tcp_mutex); - int ret = get_random_tcp_conn_number(c->tcp_c); + int ret = get_random_tcp_onion_conn_number(c->tcp_c); pthread_mutex_unlock(&c->tcp_mutex); return ret;