diff options
-rw-r--r-- | toxcore/TCP_connection.c | 18 | ||||
-rw-r--r-- | toxcore/TCP_connection.h | 8 | ||||
-rw-r--r-- | toxcore/net_crypto.c | 2 |
3 files changed, 22 insertions, 6 deletions
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 | |||
274 | * return TCP connection number on success. | 274 | * return TCP connection number on success. |
275 | * return -1 on failure. | 275 | * return -1 on failure. |
276 | */ | 276 | */ |
277 | int get_random_tcp_conn_number(TCP_Connections *tcp_c) | 277 | int get_random_tcp_onion_conn_number(TCP_Connections *tcp_c) |
278 | { | 278 | { |
279 | unsigned int i, r = rand(); | 279 | unsigned int i, r = rand(); |
280 | 280 | ||
281 | for (i = 0; i < tcp_c->tcp_connections_length; ++i) { | 281 | for (i = 0; i < tcp_c->tcp_connections_length; ++i) { |
282 | if (tcp_c->tcp_connections[(i + r) % tcp_c->tcp_connections_length].status == TCP_CONN_CONNECTED) { | 282 | unsigned int index = ((i + r) % tcp_c->tcp_connections_length); |
283 | return ((i + r) % tcp_c->tcp_connections_length); | 283 | if (tcp_c->tcp_connections[index].onion && tcp_c->tcp_connections[index].status == TCP_CONN_CONNECTED) { |
284 | return index; | ||
284 | } | 285 | } |
285 | } | 286 | } |
286 | 287 | ||
@@ -579,6 +580,10 @@ static int kill_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connections | |||
579 | } | 580 | } |
580 | } | 581 | } |
581 | 582 | ||
583 | if (tcp_con->onion) { | ||
584 | --tcp_c->onion_num_conns; | ||
585 | } | ||
586 | |||
582 | kill_TCP_connection(tcp_con->connection); | 587 | kill_TCP_connection(tcp_con->connection); |
583 | 588 | ||
584 | return wipe_tcp_connection(tcp_c, tcp_connections_number); | 589 | 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 | |||
777 | tcp_con->connected_time = 0; | 782 | tcp_con->connected_time = 0; |
778 | } | 783 | } |
779 | 784 | ||
785 | if (tcp_c->onion_num_conns < NUM_ONION_TCP_CONNECTIONS) { | ||
786 | tcp_con->onion = 1; | ||
787 | ++tcp_c->onion_num_conns; | ||
788 | } | ||
789 | |||
780 | return 0; | 790 | return 0; |
781 | } | 791 | } |
782 | 792 | ||
@@ -978,7 +988,7 @@ static void kill_nonused_tcp(TCP_Connections *tcp_c) | |||
978 | 988 | ||
979 | if (tcp_con) { | 989 | if (tcp_con) { |
980 | if (tcp_con->status == TCP_CONN_CONNECTED) { | 990 | if (tcp_con->status == TCP_CONN_CONNECTED) { |
981 | if (!tcp_con->lock_count && is_timeout(tcp_con->connected_time, TCP_CONNECTION_ANNOUNCE_TIMEOUT)) { | 991 | if (!tcp_con->onion && !tcp_con->lock_count && is_timeout(tcp_con->connected_time, TCP_CONNECTION_ANNOUNCE_TIMEOUT)) { |
982 | to_kill[num_kill] = i; | 992 | to_kill[num_kill] = i; |
983 | ++num_kill; | 993 | ++num_kill; |
984 | } | 994 | } |
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 @@ | |||
43 | NOTE: Must be at most (MAX_FRIEND_TCP_CONNECTIONS / 2) */ | 43 | NOTE: Must be at most (MAX_FRIEND_TCP_CONNECTIONS / 2) */ |
44 | #define RECOMMENDED_FRIEND_TCP_CONNECTIONS (MAX_FRIEND_TCP_CONNECTIONS / 2) | 44 | #define RECOMMENDED_FRIEND_TCP_CONNECTIONS (MAX_FRIEND_TCP_CONNECTIONS / 2) |
45 | 45 | ||
46 | /* Number of TCP connections used for onion purposes. */ | ||
47 | #define NUM_ONION_TCP_CONNECTIONS RECOMMENDED_FRIEND_TCP_CONNECTIONS | ||
48 | |||
46 | typedef struct { | 49 | typedef struct { |
47 | uint8_t status; | 50 | uint8_t status; |
48 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; /* The dht public key of the peer */ | 51 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; /* The dht public key of the peer */ |
@@ -61,6 +64,7 @@ typedef struct { | |||
61 | TCP_Client_Connection *connection; | 64 | TCP_Client_Connection *connection; |
62 | uint64_t connected_time; | 65 | uint64_t connected_time; |
63 | uint32_t lock_count; | 66 | uint32_t lock_count; |
67 | _Bool onion; | ||
64 | } TCP_con; | 68 | } TCP_con; |
65 | 69 | ||
66 | typedef struct { | 70 | typedef struct { |
@@ -83,6 +87,8 @@ typedef struct { | |||
83 | void *tcp_onion_callback_object; | 87 | void *tcp_onion_callback_object; |
84 | 88 | ||
85 | TCP_Proxy_Info proxy_info; | 89 | TCP_Proxy_Info proxy_info; |
90 | |||
91 | uint16_t onion_num_conns; | ||
86 | } TCP_Connections; | 92 | } TCP_Connections; |
87 | 93 | ||
88 | /* Send a packet to the TCP connection. | 94 | /* Send a packet to the TCP connection. |
@@ -100,7 +106,7 @@ int send_packet_tcp_connection(TCP_Connections *tcp_c, int connections_number, c | |||
100 | * return TCP connection number on success. | 106 | * return TCP connection number on success. |
101 | * return -1 on failure. | 107 | * return -1 on failure. |
102 | */ | 108 | */ |
103 | int get_random_tcp_conn_number(TCP_Connections *tcp_c); | 109 | int get_random_tcp_onion_conn_number(TCP_Connections *tcp_c); |
104 | 110 | ||
105 | /* Send an onion packet via the TCP relay corresponding to tcp_connections_number. | 111 | /* Send an onion packet via the TCP relay corresponding to tcp_connections_number. |
106 | * | 112 | * |
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) | |||
1785 | int get_random_tcp_con_number(Net_Crypto *c) | 1785 | int get_random_tcp_con_number(Net_Crypto *c) |
1786 | { | 1786 | { |
1787 | pthread_mutex_lock(&c->tcp_mutex); | 1787 | pthread_mutex_lock(&c->tcp_mutex); |
1788 | int ret = get_random_tcp_conn_number(c->tcp_c); | 1788 | int ret = get_random_tcp_onion_conn_number(c->tcp_c); |
1789 | pthread_mutex_unlock(&c->tcp_mutex); | 1789 | pthread_mutex_unlock(&c->tcp_mutex); |
1790 | 1790 | ||
1791 | return ret; | 1791 | return ret; |