summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/onion.h2
-rw-r--r--toxcore/onion_client.c57
2 files changed, 34 insertions, 25 deletions
diff --git a/toxcore/onion.h b/toxcore/onion.h
index 527f5c60..5dedd60d 100644
--- a/toxcore/onion.h
+++ b/toxcore/onion.h
@@ -65,6 +65,8 @@ typedef struct {
65 IP_Port ip_port1; 65 IP_Port ip_port1;
66 IP_Port ip_port2; 66 IP_Port ip_port2;
67 IP_Port ip_port3; 67 IP_Port ip_port3;
68
69 uint32_t path_num;
68} Onion_Path; 70} Onion_Path;
69 71
70/* Create a new onion path. 72/* Create a new onion path.
diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c
index 5bffb114..1d6cdb6b 100644
--- a/toxcore/onion_client.c
+++ b/toxcore/onion_client.c
@@ -130,6 +130,12 @@ static int random_path(const Onion_Client *onion_c, Onion_Client_Paths *onion_pa
130 130
131 onion_paths->last_path_success[pathnum] = unix_time() + ONION_PATH_FIRST_TIMEOUT - ONION_PATH_TIMEOUT; 131 onion_paths->last_path_success[pathnum] = unix_time() + ONION_PATH_FIRST_TIMEOUT - ONION_PATH_TIMEOUT;
132 onion_paths->path_creation_time[pathnum] = unix_time(); 132 onion_paths->path_creation_time[pathnum] = unix_time();
133 uint32_t path_num = rand();
134 path_num /= NUMBER_ONION_PATHS;
135 path_num *= NUMBER_ONION_PATHS;
136 path_num += pathnum;
137
138 onion_paths->paths[pathnum].path_num = path_num;
133 } else { 139 } else {
134 pathnum = n; 140 pathnum = n;
135 } 141 }
@@ -142,7 +148,7 @@ static int random_path(const Onion_Client *onion_c, Onion_Client_Paths *onion_pa
142/* Set path timeouts, return the path number. 148/* Set path timeouts, return the path number.
143 * 149 *
144 */ 150 */
145static uint32_t set_path_timeouts(Onion_Client *onion_c, uint32_t num, IP_Port source) 151static uint32_t set_path_timeouts(Onion_Client *onion_c, uint32_t num, uint32_t path_num)
146{ 152{
147 if (num > onion_c->num_friends) 153 if (num > onion_c->num_friends)
148 return -1; 154 return -1;
@@ -155,13 +161,9 @@ static uint32_t set_path_timeouts(Onion_Client *onion_c, uint32_t num, IP_Port s
155 onion_paths = &onion_c->friends_list[num - 1].onion_paths; 161 onion_paths = &onion_c->friends_list[num - 1].onion_paths;
156 } 162 }
157 163
158 uint32_t i; 164 if (onion_paths->paths[path_num % NUMBER_ONION_PATHS].path_num == path_num) {
159 165 onion_paths->last_path_success[path_num % NUMBER_ONION_PATHS] = unix_time();
160 for (i = 0; i < NUMBER_ONION_PATHS; ++i) { 166 return path_num % NUMBER_ONION_PATHS;
161 if (ipport_equal(&onion_paths->paths[i].ip_port1, &source)) {
162 onion_paths->last_path_success[i] = unix_time();
163 return i;
164 }
165 } 167 }
166 168
167 return ~0; 169 return ~0;
@@ -208,12 +210,13 @@ static int send_onion_packet_tcp_udp(const Onion_Client *onion_c, const Onion_Pa
208 * 210 *
209 */ 211 */
210static int new_sendback(Onion_Client *onion_c, uint32_t num, const uint8_t *public_key, IP_Port ip_port, 212static int new_sendback(Onion_Client *onion_c, uint32_t num, const uint8_t *public_key, IP_Port ip_port,
211 uint64_t *sendback) 213 uint32_t path_num, uint64_t *sendback)
212{ 214{
213 uint8_t data[sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES + sizeof(IP_Port)]; 215 uint8_t data[sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES + sizeof(IP_Port) + sizeof(uint32_t)];
214 memcpy(data, &num, sizeof(uint32_t)); 216 memcpy(data, &num, sizeof(uint32_t));
215 memcpy(data + sizeof(uint32_t), public_key, crypto_box_PUBLICKEYBYTES); 217 memcpy(data + sizeof(uint32_t), public_key, crypto_box_PUBLICKEYBYTES);
216 memcpy(data + sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES, &ip_port, sizeof(IP_Port)); 218 memcpy(data + sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES, &ip_port, sizeof(IP_Port));
219 memcpy(data + sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES + sizeof(IP_Port), &path_num, sizeof(uint32_t));
217 *sendback = ping_array_add(&onion_c->announce_ping_array, data, sizeof(data)); 220 *sendback = ping_array_add(&onion_c->announce_ping_array, data, sizeof(data));
218 221
219 if (*sendback == 0) 222 if (*sendback == 0)
@@ -233,17 +236,19 @@ static int new_sendback(Onion_Client *onion_c, uint32_t num, const uint8_t *publ
233 * return num (see new_sendback(...)) on success 236 * return num (see new_sendback(...)) on success
234 */ 237 */
235static uint32_t check_sendback(Onion_Client *onion_c, const uint8_t *sendback, uint8_t *ret_pubkey, 238static uint32_t check_sendback(Onion_Client *onion_c, const uint8_t *sendback, uint8_t *ret_pubkey,
236 IP_Port *ret_ip_port) 239 IP_Port *ret_ip_port, uint32_t *path_num)
237{ 240{
238 uint64_t sback; 241 uint64_t sback;
239 memcpy(&sback, sendback, sizeof(uint64_t)); 242 memcpy(&sback, sendback, sizeof(uint64_t));
240 uint8_t data[sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES + sizeof(IP_Port)]; 243 uint8_t data[sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES + sizeof(IP_Port) + sizeof(uint32_t)];
241 244
242 if (ping_array_check(data, sizeof(data), &onion_c->announce_ping_array, sback) != sizeof(data)) 245 if (ping_array_check(data, sizeof(data), &onion_c->announce_ping_array, sback) != sizeof(data))
243 return ~0; 246 return ~0;
244 247
245 memcpy(ret_pubkey, data + sizeof(uint32_t), crypto_box_PUBLICKEYBYTES); 248 memcpy(ret_pubkey, data + sizeof(uint32_t), crypto_box_PUBLICKEYBYTES);
246 memcpy(ret_ip_port, data + sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES, sizeof(IP_Port)); 249 memcpy(ret_ip_port, data + sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES, sizeof(IP_Port));
250 memcpy(path_num, data + sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES + sizeof(IP_Port), sizeof(uint32_t));
251
247 uint32_t num; 252 uint32_t num;
248 memcpy(&num, data, sizeof(uint32_t)); 253 memcpy(&num, data, sizeof(uint32_t));
249 return num; 254 return num;
@@ -256,8 +261,17 @@ static int client_send_announce_request(Onion_Client *onion_c, uint32_t num, IP_
256 return -1; 261 return -1;
257 262
258 uint64_t sendback; 263 uint64_t sendback;
264 Onion_Path path;
265
266 if (num == 0) {
267 if (random_path(onion_c, &onion_c->onion_paths, pathnum, &path) == -1)
268 return -1;
269 } else {
270 if (random_path(onion_c, &onion_c->friends_list[num - 1].onion_paths, pathnum, &path) == -1)
271 return -1;
272 }
259 273
260 if (new_sendback(onion_c, num, dest_pubkey, dest, &sendback) == -1) 274 if (new_sendback(onion_c, num, dest_pubkey, dest, path.path_num, &sendback) == -1)
261 return -1; 275 return -1;
262 276
263 uint8_t zero_ping_id[ONION_PING_ID_SIZE] = {0}; 277 uint8_t zero_ping_id[ONION_PING_ID_SIZE] = {0};
@@ -265,8 +279,6 @@ static int client_send_announce_request(Onion_Client *onion_c, uint32_t num, IP_
265 if (ping_id == NULL) 279 if (ping_id == NULL)
266 ping_id = zero_ping_id; 280 ping_id = zero_ping_id;
267 281
268 Onion_Path path;
269
270 Node_format dest_node; 282 Node_format dest_node;
271 dest_node.ip_port = dest; 283 dest_node.ip_port = dest;
272 memcpy(dest_node.client_id, dest_pubkey, crypto_box_PUBLICKEYBYTES); 284 memcpy(dest_node.client_id, dest_pubkey, crypto_box_PUBLICKEYBYTES);
@@ -274,16 +286,10 @@ static int client_send_announce_request(Onion_Client *onion_c, uint32_t num, IP_
274 int len; 286 int len;
275 287
276 if (num == 0) { 288 if (num == 0) {
277 if (random_path(onion_c, &onion_c->onion_paths, pathnum, &path) == -1)
278 return -1;
279
280 len = create_announce_request(request, sizeof(request), dest_pubkey, onion_c->c->self_public_key, 289 len = create_announce_request(request, sizeof(request), dest_pubkey, onion_c->c->self_public_key,
281 onion_c->c->self_secret_key, ping_id, onion_c->c->self_public_key, onion_c->temp_public_key, sendback); 290 onion_c->c->self_secret_key, ping_id, onion_c->c->self_public_key, onion_c->temp_public_key, sendback);
282 291
283 } else { 292 } else {
284 if (random_path(onion_c, &onion_c->friends_list[num - 1].onion_paths, pathnum, &path) == -1)
285 return -1;
286
287 len = create_announce_request(request, sizeof(request), dest_pubkey, onion_c->friends_list[num - 1].temp_public_key, 293 len = create_announce_request(request, sizeof(request), dest_pubkey, onion_c->friends_list[num - 1].temp_public_key,
288 onion_c->friends_list[num - 1].temp_secret_key, ping_id, onion_c->friends_list[num - 1].real_client_id, zero_ping_id, 294 onion_c->friends_list[num - 1].temp_secret_key, ping_id, onion_c->friends_list[num - 1].real_client_id, zero_ping_id,
289 sendback); 295 sendback);
@@ -326,7 +332,7 @@ static int cmp_entry(const void *a, const void *b)
326} 332}
327 333
328static int client_add_to_list(Onion_Client *onion_c, uint32_t num, const uint8_t *public_key, IP_Port ip_port, 334static int client_add_to_list(Onion_Client *onion_c, uint32_t num, const uint8_t *public_key, IP_Port ip_port,
329 uint8_t is_stored, const uint8_t *pingid_or_key, IP_Port source) 335 uint8_t is_stored, const uint8_t *pingid_or_key, uint32_t path_num)
330{ 336{
331 if (num > onion_c->num_friends) 337 if (num > onion_c->num_friends)
332 return -1; 338 return -1;
@@ -385,7 +391,7 @@ static int client_add_to_list(Onion_Client *onion_c, uint32_t num, const uint8_t
385 list_nodes[index].is_stored = is_stored; 391 list_nodes[index].is_stored = is_stored;
386 list_nodes[index].timestamp = unix_time(); 392 list_nodes[index].timestamp = unix_time();
387 list_nodes[index].last_pinged = 0; 393 list_nodes[index].last_pinged = 0;
388 list_nodes[index].path_used = set_path_timeouts(onion_c, num, source); 394 list_nodes[index].path_used = set_path_timeouts(onion_c, num, path_num);
389 return 0; 395 return 0;
390} 396}
391 397
@@ -470,7 +476,8 @@ static int handle_announce_response(void *object, IP_Port source, const uint8_t
470 476
471 uint8_t public_key[crypto_box_PUBLICKEYBYTES]; 477 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
472 IP_Port ip_port; 478 IP_Port ip_port;
473 uint32_t num = check_sendback(onion_c, packet + 1, public_key, &ip_port); 479 uint32_t path_num;
480 uint32_t num = check_sendback(onion_c, packet + 1, public_key, &ip_port, &path_num);
474 481
475 if (num > onion_c->num_friends) 482 if (num > onion_c->num_friends)
476 return 1; 483 return 1;
@@ -495,7 +502,7 @@ static int handle_announce_response(void *object, IP_Port source, const uint8_t
495 if ((uint32_t)len != sizeof(plain)) 502 if ((uint32_t)len != sizeof(plain))
496 return 1; 503 return 1;
497 504
498 if (client_add_to_list(onion_c, num, public_key, ip_port, plain[0], plain + 1, source) == -1) 505 if (client_add_to_list(onion_c, num, public_key, ip_port, plain[0], plain + 1, path_num) == -1)
499 return 1; 506 return 1;
500 507
501 if (len_nodes != 0) { 508 if (len_nodes != 0) {