summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--toxcore/onion_announce.c1
-rw-r--r--toxcore/onion_announce.h2
-rw-r--r--toxcore/onion_client.c66
-rw-r--r--toxcore/onion_client.h3
4 files changed, 68 insertions, 4 deletions
diff --git a/toxcore/onion_announce.c b/toxcore/onion_announce.c
index c6fbd483..480475be 100644
--- a/toxcore/onion_announce.c
+++ b/toxcore/onion_announce.c
@@ -191,7 +191,6 @@ static int add_to_entries(Onion_Announce *onion_a, IP_Port ret_ip_port, uint8_t
191 if (pos == -1) 191 if (pos == -1)
192 return 0; 192 return 0;
193 193
194
195 memcpy(onion_a->entries[pos].public_key, public_key, crypto_box_PUBLICKEYBYTES); 194 memcpy(onion_a->entries[pos].public_key, public_key, crypto_box_PUBLICKEYBYTES);
196 onion_a->entries[pos].ret_ip_port = ret_ip_port; 195 onion_a->entries[pos].ret_ip_port = ret_ip_port;
197 memcpy(onion_a->entries[pos].ret, ret, ONION_RETURN_3); 196 memcpy(onion_a->entries[pos].ret, ret, ONION_RETURN_3);
diff --git a/toxcore/onion_announce.h b/toxcore/onion_announce.h
index 5afc9baa..f7d565e5 100644
--- a/toxcore/onion_announce.h
+++ b/toxcore/onion_announce.h
@@ -34,6 +34,8 @@
34#define ONION_ANNOUNCE_RESPONSE_MIN_SIZE (1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + crypto_box_NONCEBYTES + ONION_PING_ID_SIZE + crypto_box_MACBYTES) 34#define ONION_ANNOUNCE_RESPONSE_MIN_SIZE (1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + crypto_box_NONCEBYTES + ONION_PING_ID_SIZE + crypto_box_MACBYTES)
35#define ONION_ANNOUNCE_RESPONSE_MAX_SIZE (ONION_ANNOUNCE_RESPONSE_MIN_SIZE + sizeof(Node_format)*MAX_SENT_NODES) 35#define ONION_ANNOUNCE_RESPONSE_MAX_SIZE (ONION_ANNOUNCE_RESPONSE_MIN_SIZE + sizeof(Node_format)*MAX_SENT_NODES)
36 36
37#define ONION_DATA_RESPONSE_MIN_SIZE (1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + crypto_box_MACBYTES)
38
37typedef struct { 39typedef struct {
38 uint8_t public_key[crypto_box_PUBLICKEYBYTES]; 40 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
39 IP_Port ret_ip_port; 41 IP_Port ret_ip_port;
diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c
index 89bd1c5e..f4ac9e01 100644
--- a/toxcore/onion_client.c
+++ b/toxcore/onion_client.c
@@ -129,7 +129,7 @@ static int client_send_announce_request(Onion_Client *onion_c, uint32_t num, IP_
129 } else { 129 } else {
130 return send_announce_request(onion_c->dht, nodes, onion_c->friends_list[num - 1].temp_public_key, 130 return send_announce_request(onion_c->dht, nodes, onion_c->friends_list[num - 1].temp_public_key,
131 onion_c->friends_list[num - 1].temp_secret_key, ping_id, 131 onion_c->friends_list[num - 1].temp_secret_key, ping_id,
132 onion_c->friends_list[num - 1].fake_client_id, sendback); 132 onion_c->friends_list[num - 1].real_client_id, sendback);
133 } 133 }
134} 134}
135 135
@@ -301,6 +301,21 @@ static int handle_data_response(void *object, IP_Port source, uint8_t *packet, u
301{ 301{
302 Onion_Client *onion_c = object; 302 Onion_Client *onion_c = object;
303 303
304 if (length <= ONION_DATA_RESPONSE_MIN_SIZE)
305 return 1;
306
307 if (length > MAX_DATA_SIZE)
308 return 1;
309
310 uint8_t plain[length - ONION_DATA_RESPONSE_MIN_SIZE];
311 int len = decrypt_data(packet + 1 + crypto_box_NONCEBYTES, onion_c->dht->c->self_secret_key, packet + 1,
312 packet + 1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES,
313 length - (1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES), plain);
314
315 if ((uint32_t)len != sizeof(plain))
316 return 1;
317
318 //TODO do something with the plain
304 return 0; 319 return 0;
305} 320}
306 321
@@ -318,11 +333,42 @@ int random_path(Onion_Client *onion_c, Node_format *nodes)
318 return -1; 333 return -1;
319} 334}
320 335
336#define ANNOUNCE_FRIEND 30
321uint8_t zero_ping[ONION_PING_ID_SIZE]; 337uint8_t zero_ping[ONION_PING_ID_SIZE];
322static void do_friend(Onion_Client *onion_c, uint16_t friendnum) 338static void do_friend(Onion_Client *onion_c, uint16_t friendnum)
323{ 339{
340 if (friendnum >= onion_c->num_friends)
341 return;
324 342
343 if (onion_c->friends_list[friendnum].status == 0)
344 return;
325 345
346 uint32_t i, count = 0;
347 Onion_Node *list_nodes = onion_c->friends_list[friendnum].clients_list;
348
349 for (i = 0; i < MAX_ONION_CLIENTS; ++i) {
350 if (is_timeout(list_nodes[i].timestamp, ONION_NODE_TIMEOUT))
351 continue;
352
353 ++count;
354
355 if (is_timeout(list_nodes[i].last_pinged, ANNOUNCE_FRIEND)) {
356 if (client_send_announce_request(onion_c, friendnum + 1, list_nodes[i].ip_port, list_nodes[i].client_id, 0) == 0) {
357 list_nodes[i].last_pinged = unix_time();
358 }
359 }
360 }
361
362 if (count < MAX_ONION_CLIENTS / 2) {
363 Node_format nodes_list[MAX_SENT_NODES];
364 uint32_t num_nodes = get_close_nodes(onion_c->dht, onion_c->friends_list[i].real_client_id, nodes_list,
365 rand() % 2 ? AF_INET : AF_INET6, rand() % 2, 1);
366
367 for (i = 0; i < num_nodes; ++i)
368 client_send_announce_request(onion_c, friendnum + 1, nodes_list[i].ip_port, nodes_list[i].client_id, 0);
369 }
370
371 //TODO send packets to friend telling them our fake DHT id.
326} 372}
327 373
328#define ANNOUNCE_INTERVAL_NOT_ANNOUNCED 10 374#define ANNOUNCE_INTERVAL_NOT_ANNOUNCED 10
@@ -330,13 +376,14 @@ static void do_friend(Onion_Client *onion_c, uint16_t friendnum)
330 376
331static void do_announce(Onion_Client *onion_c) 377static void do_announce(Onion_Client *onion_c)
332{ 378{
333 uint32_t i; 379 uint32_t i, count = 0;
334 Onion_Node *list_nodes = onion_c->clients_announce_list; 380 Onion_Node *list_nodes = onion_c->clients_announce_list;
335 381
336 for (i = 0; i < MAX_ONION_CLIENTS; ++i) { 382 for (i = 0; i < MAX_ONION_CLIENTS; ++i) {
337 if (is_timeout(list_nodes[i].timestamp, ONION_NODE_TIMEOUT)) 383 if (is_timeout(list_nodes[i].timestamp, ONION_NODE_TIMEOUT))
338 continue; 384 continue;
339 385
386 ++count;
340 uint32_t interval = ANNOUNCE_INTERVAL_NOT_ANNOUNCED; 387 uint32_t interval = ANNOUNCE_INTERVAL_NOT_ANNOUNCED;
341 388
342 if (memcmp(list_nodes[i].ping_id, zero_ping, ONION_PING_ID_SIZE) == 0) { 389 if (memcmp(list_nodes[i].ping_id, zero_ping, ONION_PING_ID_SIZE) == 0) {
@@ -350,16 +397,31 @@ static void do_announce(Onion_Client *onion_c)
350 } 397 }
351 } 398 }
352 } 399 }
400
401 if (count < MAX_ONION_CLIENTS / 2) {
402 Node_format nodes_list[MAX_SENT_NODES];
403 uint32_t num_nodes = get_close_nodes(onion_c->dht, onion_c->dht->c->self_public_key, nodes_list,
404 rand() % 2 ? AF_INET : AF_INET6, rand() % 2, 1);
405
406 for (i = 0; i < num_nodes; ++i)
407 client_send_announce_request(onion_c, 0, nodes_list[i].ip_port, nodes_list[i].client_id, 0);
408 }
353} 409}
354 410
355void do_onion_client(Onion_Client *onion_c) 411void do_onion_client(Onion_Client *onion_c)
356{ 412{
357 uint32_t i; 413 uint32_t i;
414
415 if (onion_c->last_run == unix_time())
416 return;
417
358 do_announce(onion_c); 418 do_announce(onion_c);
359 419
360 for (i = 0; i < onion_c->num_friends; ++i) { 420 for (i = 0; i < onion_c->num_friends; ++i) {
361 do_friend(onion_c, i); 421 do_friend(onion_c, i);
362 } 422 }
423
424 onion_c->last_run = unix_time();
363} 425}
364 426
365Onion_Client *new_onion_client(DHT *dht) 427Onion_Client *new_onion_client(DHT *dht)
diff --git a/toxcore/onion_client.h b/toxcore/onion_client.h
index 5f205bad..78a12b0d 100644
--- a/toxcore/onion_client.h
+++ b/toxcore/onion_client.h
@@ -33,7 +33,7 @@ typedef struct {
33 IP_Port ip_port; 33 IP_Port ip_port;
34 uint8_t ping_id[ONION_PING_ID_SIZE]; 34 uint8_t ping_id[ONION_PING_ID_SIZE];
35 uint64_t timestamp; 35 uint64_t timestamp;
36 36
37 uint64_t last_pinged; 37 uint64_t last_pinged;
38} Onion_Node; 38} Onion_Node;
39 39
@@ -57,6 +57,7 @@ typedef struct {
57 Onion_Node clients_announce_list[MAX_ONION_CLIENTS]; 57 Onion_Node clients_announce_list[MAX_ONION_CLIENTS];
58 58
59 uint8_t secret_symmetric_key[crypto_secretbox_KEYBYTES]; 59 uint8_t secret_symmetric_key[crypto_secretbox_KEYBYTES];
60 uint64_t last_run;
60} Onion_Client; 61} Onion_Client;
61 62
62int onion_addfriend(Onion_Client *onion_c, uint8_t *client_id); 63int onion_addfriend(Onion_Client *onion_c, uint8_t *client_id);