summaryrefslogtreecommitdiff
path: root/toxcore/onion_client.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/onion_client.c')
-rw-r--r--toxcore/onion_client.c64
1 files changed, 40 insertions, 24 deletions
diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c
index f0f8fa70..93697c28 100644
--- a/toxcore/onion_client.c
+++ b/toxcore/onion_client.c
@@ -50,7 +50,7 @@ static int new_sendback(Onion_Client *onion_c, uint32_t num, uint8_t *public_key
50{ 50{
51 uint8_t plain[sizeof(uint32_t) + sizeof(uint64_t) + crypto_box_PUBLICKEYBYTES + sizeof(IP_Port)]; 51 uint8_t plain[sizeof(uint32_t) + sizeof(uint64_t) + crypto_box_PUBLICKEYBYTES + sizeof(IP_Port)];
52 uint64_t time = unix_time(); 52 uint64_t time = unix_time();
53 new_nonce(sendback); 53 random_nonce(sendback);
54 memcpy(plain, &num, sizeof(uint32_t)); 54 memcpy(plain, &num, sizeof(uint32_t));
55 memcpy(plain + sizeof(uint32_t), &time, sizeof(uint64_t)); 55 memcpy(plain + sizeof(uint32_t), &time, sizeof(uint64_t));
56 memcpy(plain + sizeof(uint32_t) + sizeof(uint64_t), public_key, crypto_box_PUBLICKEYBYTES); 56 memcpy(plain + sizeof(uint32_t) + sizeof(uint64_t), public_key, crypto_box_PUBLICKEYBYTES);
@@ -183,13 +183,19 @@ static int client_add_to_list(Onion_Client *onion_c, uint32_t num, uint8_t *publ
183 qsort(list_nodes, MAX_ONION_CLIENTS, sizeof(Onion_Node), cmp_entry); 183 qsort(list_nodes, MAX_ONION_CLIENTS, sizeof(Onion_Node), cmp_entry);
184 184
185 int index = -1; 185 int index = -1;
186 uint32_t i;
186 187
187 if (is_timeout(list_nodes[0].timestamp, ONION_NODE_TIMEOUT) 188 for (i = 0; i < MAX_ONION_CLIENTS; ++i) {
188 || id_closest(reference_id, list_nodes[0].client_id, public_key) == 2) { 189 if (is_timeout(list_nodes[i].timestamp, ONION_NODE_TIMEOUT)
189 index = 0; 190 || id_closest(reference_id, list_nodes[i].client_id, public_key) == 2) {
190 } 191 index = i;
191 192
192 uint32_t i; 193 if (i != 0)
194 break;
195 } else {
196 break;
197 }
198 }
193 199
194 for (i = 0; i < MAX_ONION_CLIENTS; ++i) { 200 for (i = 0; i < MAX_ONION_CLIENTS; ++i) {
195 if (memcmp(list_nodes[i].client_id, public_key, crypto_box_PUBLICKEYBYTES) == 0) { 201 if (memcmp(list_nodes[i].client_id, public_key, crypto_box_PUBLICKEYBYTES) == 0) {
@@ -255,8 +261,9 @@ static int client_ping_nodes(Onion_Client *onion_c, uint32_t num, Node_format *n
255 } 261 }
256 } 262 }
257 263
258 if (j == MAX_ONION_CLIENTS) 264 if (j == MAX_ONION_CLIENTS) {
259 client_send_announce_request(onion_c, num, nodes[i].ip_port, nodes[i].client_id, NULL); 265 client_send_announce_request(onion_c, num, nodes[i].ip_port, nodes[i].client_id, NULL);
266 }
260 } 267 }
261 } 268 }
262 269
@@ -306,7 +313,10 @@ static int handle_announce_response(void *object, IP_Port source, uint8_t *packe
306 if (client_add_to_list(onion_c, num, public_key, ip_port, plain[0], plain + 1) == -1) 313 if (client_add_to_list(onion_c, num, public_key, ip_port, plain[0], plain + 1) == -1)
307 return 1; 314 return 1;
308 315
309 if (client_ping_nodes(onion_c, num, (Node_format *)plain + 1 + ONION_PING_ID_SIZE, num_nodes, source) == -1) 316 Node_format nodes[MAX_SENT_NODES];
317 memcpy(nodes, plain + 1 + ONION_PING_ID_SIZE, num_nodes * sizeof(Node_format));
318
319 if (client_ping_nodes(onion_c, num, nodes, num_nodes, source) == -1)
310 return 1; 320 return 1;
311 321
312 return 0; 322 return 0;
@@ -420,7 +430,7 @@ int send_onion_data(Onion_Client *onion_c, int friend_num, uint8_t *data, uint32
420 return -1; 430 return -1;
421 431
422 uint8_t nonce[crypto_box_NONCEBYTES]; 432 uint8_t nonce[crypto_box_NONCEBYTES];
423 new_nonce(nonce); 433 random_nonce(nonce);
424 434
425 uint8_t packet[DATA_IN_RESPONSE_MIN_SIZE + length]; 435 uint8_t packet[DATA_IN_RESPONSE_MIN_SIZE + length];
426 memcpy(packet, onion_c->dht->c->self_public_key, crypto_box_PUBLICKEYBYTES); 436 memcpy(packet, onion_c->dht->c->self_public_key, crypto_box_PUBLICKEYBYTES);
@@ -703,9 +713,11 @@ int onion_set_friend_online(Onion_Client *onion_c, int friend_num, uint8_t is_on
703 return -1; 713 return -1;
704 714
705 onion_c->friends_list[friend_num].is_online = is_online; 715 onion_c->friends_list[friend_num].is_online = is_online;
706 /* Should we reset the no_replay when the other goes offline? 716
717 /* This should prevent some clock related issues */
707 if (!is_online) 718 if (!is_online)
708 onion_c->friends_list[friend_num].last_noreplay = 0; */ 719 onion_c->friends_list[friend_num].last_noreplay = 0;
720
709 return 0; 721 return 0;
710} 722}
711 723
@@ -754,16 +766,17 @@ static void do_friend(Onion_Client *onion_c, uint16_t friendnum)
754 } 766 }
755 } 767 }
756 768
757 if (count < MAX_ONION_CLIENTS / 2) { 769 if (count != MAX_ONION_CLIENTS) {
758 Node_format nodes_list[MAX_SENT_NODES]; 770 if (count < rand() % MAX_ONION_CLIENTS) {
759 uint32_t num_nodes = get_close_nodes(onion_c->dht, onion_c->friends_list[friendnum].real_client_id, nodes_list, 771 Node_format nodes_list[MAX_SENT_NODES];
760 rand() % 2 ? AF_INET : AF_INET6, 1, 0); 772 uint32_t num_nodes = get_close_nodes(onion_c->dht, onion_c->friends_list[friendnum].real_client_id, nodes_list,
773 rand() % 2 ? AF_INET : AF_INET6, 1, 0);
761 774
762 for (i = 0; i < num_nodes; ++i) 775 for (i = 0; i < num_nodes; ++i)
763 client_send_announce_request(onion_c, friendnum + 1, nodes_list[i].ip_port, nodes_list[i].client_id, 0); 776 client_send_announce_request(onion_c, friendnum + 1, nodes_list[i].ip_port, nodes_list[i].client_id, 0);
777 }
764 } 778 }
765 779
766
767 /* send packets to friend telling them our fake DHT id. */ 780 /* send packets to friend telling them our fake DHT id. */
768 if (is_timeout(onion_c->friends_list[friendnum].last_fakeid_onion_sent, ONION_FAKEID_INTERVAL)) 781 if (is_timeout(onion_c->friends_list[friendnum].last_fakeid_onion_sent, ONION_FAKEID_INTERVAL))
769 if (send_fakeid_announce(onion_c, friendnum, 0) >= 1) 782 if (send_fakeid_announce(onion_c, friendnum, 0) >= 1)
@@ -772,6 +785,7 @@ static void do_friend(Onion_Client *onion_c, uint16_t friendnum)
772 if (is_timeout(onion_c->friends_list[friendnum].last_fakeid_dht_sent, DHT_FAKEID_INTERVAL)) 785 if (is_timeout(onion_c->friends_list[friendnum].last_fakeid_dht_sent, DHT_FAKEID_INTERVAL))
773 if (send_fakeid_announce(onion_c, friendnum, 1) >= 1) 786 if (send_fakeid_announce(onion_c, friendnum, 1) >= 1)
774 onion_c->friends_list[friendnum].last_fakeid_dht_sent = unix_time(); 787 onion_c->friends_list[friendnum].last_fakeid_dht_sent = unix_time();
788
775 } 789 }
776} 790}
777/* Function to call when onion data packet with contents beginning with byte is received. */ 791/* Function to call when onion data packet with contents beginning with byte is received. */
@@ -808,13 +822,15 @@ static void do_announce(Onion_Client *onion_c)
808 } 822 }
809 } 823 }
810 824
811 if (count < MAX_ONION_CLIENTS / 2) { 825 if (count != MAX_ONION_CLIENTS) {
812 Node_format nodes_list[MAX_SENT_NODES]; 826 if (count < rand() % MAX_ONION_CLIENTS) {
813 uint32_t num_nodes = get_close_nodes(onion_c->dht, onion_c->dht->c->self_public_key, nodes_list, 827 Node_format nodes_list[MAX_SENT_NODES];
814 rand() % 2 ? AF_INET : AF_INET6, 1, 0); 828 uint32_t num_nodes = get_close_nodes(onion_c->dht, onion_c->dht->c->self_public_key, nodes_list,
829 rand() % 2 ? AF_INET : AF_INET6, 1, 0);
815 830
816 for (i = 0; i < num_nodes; ++i) 831 for (i = 0; i < num_nodes; ++i)
817 client_send_announce_request(onion_c, 0, nodes_list[i].ip_port, nodes_list[i].client_id, 0); 832 client_send_announce_request(onion_c, 0, nodes_list[i].ip_port, nodes_list[i].client_id, 0);
833 }
818 } 834 }
819} 835}
820 836