summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2015-01-16 21:33:11 -0500
committerirungentoo <irungentoo@gmail.com>2015-01-16 21:33:11 -0500
commit0fff7d999404ec6e68dad2b4cbc283e34c198087 (patch)
tree81076a0215fc7fec7c206f2f943d2bbacb3608f2
parent553a8dd7bdab4bfed36352bb3fdf2ce2f514d0fb (diff)
Tox should work better on TCP only when some people have broken TCP
nodes and don't update them. Paths now try to use the same TCP node for their entire lifetime.
-rw-r--r--toxcore/net_crypto.c39
-rw-r--r--toxcore/net_crypto.h11
-rw-r--r--toxcore/onion_client.c10
3 files changed, 48 insertions, 12 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index 8af3cbbc..112931ad 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -2039,29 +2039,50 @@ int add_tcp_relay(Net_Crypto *c, IP_Port ip_port, const uint8_t *public_key)
2039 return -1; 2039 return -1;
2040} 2040}
2041 2041
2042/* Send an onion packet via a random connected TCP relay. 2042/* Return a random TCP connection number for use in send_tcp_onion_request.
2043 * 2043 *
2044 * return 0 on success. 2044 * TODO: This number is just the index of an array that the elements can
2045 * change without warning.
2046 *
2047 * return TCP connection number on success.
2045 * return -1 on failure. 2048 * return -1 on failure.
2046 */ 2049 */
2047int send_tcp_onion_request(Net_Crypto *c, const uint8_t *data, uint16_t length) 2050int get_random_tcp_con_number(Net_Crypto *c)
2048{ 2051{
2049 unsigned int i, r = rand(); 2052 unsigned int i, r = rand();
2050 2053
2051 for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) { 2054 for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) {
2052 if (c->tcp_connections[(i + r) % MAX_TCP_CONNECTIONS]) { 2055 if (c->tcp_connections[(i + r) % MAX_TCP_CONNECTIONS]) {
2053 pthread_mutex_lock(&c->tcp_mutex); 2056 return (i + r) % MAX_TCP_CONNECTIONS;
2054 int ret = send_onion_request(c->tcp_connections[(i + r) % MAX_TCP_CONNECTIONS], data, length);
2055 pthread_mutex_unlock(&c->tcp_mutex);
2056
2057 if (ret == 1)
2058 return 0;
2059 } 2057 }
2060 } 2058 }
2061 2059
2062 return -1; 2060 return -1;
2063} 2061}
2064 2062
2063/* Send an onion packet via the TCP relay corresponding to TCP_conn_number.
2064 *
2065 * return 0 on success.
2066 * return -1 on failure.
2067 */
2068int send_tcp_onion_request(Net_Crypto *c, unsigned int TCP_conn_number, const uint8_t *data, uint16_t length)
2069{
2070 if (TCP_conn_number > MAX_TCP_CONNECTIONS) {
2071 return -1;
2072 }
2073
2074 if (c->tcp_connections[TCP_conn_number]) {
2075 pthread_mutex_lock(&c->tcp_mutex);
2076 int ret = send_onion_request(c->tcp_connections[TCP_conn_number], data, length);
2077 pthread_mutex_unlock(&c->tcp_mutex);
2078
2079 if (ret == 1)
2080 return 0;
2081 }
2082
2083 return -1;
2084}
2085
2065/* Set the function to be called when an onion response packet is received by one of the TCP connections. 2086/* Set the function to be called when an onion response packet is received by one of the TCP connections.
2066 */ 2087 */
2067void tcp_onion_response_handler(Net_Crypto *c, int (*tcp_onion_callback)(void *object, const uint8_t *data, 2088void tcp_onion_response_handler(Net_Crypto *c, int (*tcp_onion_callback)(void *object, const uint8_t *data,
diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h
index b9fb1747..9d699340 100644
--- a/toxcore/net_crypto.h
+++ b/toxcore/net_crypto.h
@@ -364,12 +364,19 @@ int add_tcp_relay(Net_Crypto *c, IP_Port ip_port, const uint8_t *public_key);
364void tcp_onion_response_handler(Net_Crypto *c, int (*tcp_onion_callback)(void *object, const uint8_t *data, 364void tcp_onion_response_handler(Net_Crypto *c, int (*tcp_onion_callback)(void *object, const uint8_t *data,
365 uint16_t length), void *object); 365 uint16_t length), void *object);
366 366
367/* Send an onion packet via a random connected TCP relay. 367/* Return a random TCP connection number for use in send_tcp_onion_request.
368 *
369 * return TCP connection number on success.
370 * return -1 on failure.
371 */
372int get_random_tcp_con_number(Net_Crypto *c);
373
374/* Send an onion packet via the TCP relay corresponding to TCP_conn_number.
368 * 375 *
369 * return 0 on success. 376 * return 0 on success.
370 * return -1 on failure. 377 * return -1 on failure.
371 */ 378 */
372int send_tcp_onion_request(Net_Crypto *c, const uint8_t *data, uint16_t length); 379int send_tcp_onion_request(Net_Crypto *c, unsigned int TCP_conn_number, const uint8_t *data, uint16_t length);
373 380
374/* Copy a maximum of num TCP relays we are connected to to tcp_relays. 381/* Copy a maximum of num TCP relays we are connected to to tcp_relays.
375 * NOTE that the family of the copied ip ports will be set to TCP_INET or TCP_INET6. 382 * NOTE that the family of the copied ip ports will be set to TCP_INET or TCP_INET6.
diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c
index f8d7630b..a9eb16a0 100644
--- a/toxcore/onion_client.c
+++ b/toxcore/onion_client.c
@@ -140,8 +140,15 @@ static uint16_t random_nodes_path_onion(const Onion_Client *onion_c, Node_format
140 nodes[i] = onion_c->path_nodes[rand() % num_nodes]; 140 nodes[i] = onion_c->path_nodes[rand() % num_nodes];
141 } 141 }
142 } else { 142 } else {
143 int random_tcp = get_random_tcp_con_number(onion_c->c);
144
145 if (random_tcp == -1) {
146 return 0;
147 }
148
143 if (num_nodes >= 2) { 149 if (num_nodes >= 2) {
144 nodes[0].ip_port.ip.family = TCP_FAMILY; 150 nodes[0].ip_port.ip.family = TCP_FAMILY;
151 nodes[0].ip_port.ip.ip4.uint32 = random_tcp;
145 152
146 for (i = 1; i < max_num; ++i) { 153 for (i = 1; i < max_num; ++i) {
147 nodes[i] = onion_c->path_nodes[rand() % num_nodes]; 154 nodes[i] = onion_c->path_nodes[rand() % num_nodes];
@@ -154,6 +161,7 @@ static uint16_t random_nodes_path_onion(const Onion_Client *onion_c, Node_format
154 return 0; 161 return 0;
155 162
156 nodes[0].ip_port.ip.family = TCP_FAMILY; 163 nodes[0].ip_port.ip.family = TCP_FAMILY;
164 nodes[0].ip_port.ip.ip4.uint32 = random_tcp;
157 165
158 for (i = 1; i < max_num; ++i) { 166 for (i = 1; i < max_num; ++i) {
159 nodes[i] = onion_c->path_nodes_bs[rand() % num_nodes_bs]; 167 nodes[i] = onion_c->path_nodes_bs[rand() % num_nodes_bs];
@@ -301,7 +309,7 @@ static int send_onion_packet_tcp_udp(const Onion_Client *onion_c, const Onion_Pa
301 if (len == -1) 309 if (len == -1)
302 return -1; 310 return -1;
303 311
304 return send_tcp_onion_request(onion_c->c, packet, len); 312 return send_tcp_onion_request(onion_c->c, path->ip_port1.ip.ip4.uint32, packet, len);
305 } else { 313 } else {
306 return -1; 314 return -1;
307 } 315 }