summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/DHT.c2
-rw-r--r--toxcore/onion_announce.c37
-rw-r--r--toxcore/onion_client.c37
3 files changed, 63 insertions, 13 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index 227c6175..dfd4d821 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -866,7 +866,7 @@ static unsigned int store_node_ok(const Client_data *client, const uint8_t *publ
866static void sort_client_list(Client_data *list, unsigned int length, const uint8_t *comp_public_key) 866static void sort_client_list(Client_data *list, unsigned int length, const uint8_t *comp_public_key)
867{ 867{
868 // Pass comp_public_key to qsort with each Client_data entry, so the 868 // Pass comp_public_key to qsort with each Client_data entry, so the
869 // comparison function cmp_dht_entry can use it as the base of comparison. 869 // comparison function can use it as the base of comparison.
870 Cmp_data cmp_list[length]; 870 Cmp_data cmp_list[length];
871 871
872 for (uint32_t i = 0; i < length; i++) { 872 for (uint32_t i = 0; i < length; i++) {
diff --git a/toxcore/onion_announce.c b/toxcore/onion_announce.c
index 04450c57..28d3b281 100644
--- a/toxcore/onion_announce.c
+++ b/toxcore/onion_announce.c
@@ -233,12 +233,20 @@ static int in_entries(const Onion_Announce *onion_a, const uint8_t *public_key)
233 return -1; 233 return -1;
234} 234}
235 235
236static uint8_t cmp_public_key[CRYPTO_PUBLIC_KEY_SIZE]; 236typedef struct {
237 const uint8_t *base_public_key;
238 Onion_Announce_Entry entry;
239} Cmp_data;
240
237static int cmp_entry(const void *a, const void *b) 241static int cmp_entry(const void *a, const void *b)
238{ 242{
239 Onion_Announce_Entry entry1, entry2; 243 Cmp_data cmp1, cmp2;
240 memcpy(&entry1, a, sizeof(Onion_Announce_Entry)); 244 memcpy(&cmp1, a, sizeof(Cmp_data));
241 memcpy(&entry2, b, sizeof(Onion_Announce_Entry)); 245 memcpy(&cmp2, b, sizeof(Cmp_data));
246 Onion_Announce_Entry entry1 = cmp1.entry;
247 Onion_Announce_Entry entry2 = cmp2.entry;
248 const uint8_t *cmp_public_key = cmp1.base_public_key;
249
242 int t1 = is_timeout(entry1.time, ONION_ANNOUNCE_TIMEOUT); 250 int t1 = is_timeout(entry1.time, ONION_ANNOUNCE_TIMEOUT);
243 int t2 = is_timeout(entry2.time, ONION_ANNOUNCE_TIMEOUT); 251 int t2 = is_timeout(entry2.time, ONION_ANNOUNCE_TIMEOUT);
244 252
@@ -267,6 +275,24 @@ static int cmp_entry(const void *a, const void *b)
267 return 0; 275 return 0;
268} 276}
269 277
278static void sort_onion_announce_list(Onion_Announce_Entry *list, unsigned int length, const uint8_t *comp_public_key)
279{
280 // Pass comp_public_key to qsort with each Client_data entry, so the
281 // comparison function can use it as the base of comparison.
282 Cmp_data cmp_list[length];
283
284 for (uint32_t i = 0; i < length; i++) {
285 cmp_list[i].base_public_key = comp_public_key;
286 cmp_list[i].entry = list[i];
287 }
288
289 qsort(cmp_list, length, sizeof(Cmp_data), cmp_entry);
290
291 for (uint32_t i = 0; i < length; i++) {
292 list[i] = cmp_list[i].entry;
293 }
294}
295
270/* add entry to entries list 296/* add entry to entries list
271 * 297 *
272 * return -1 if failure 298 * return -1 if failure
@@ -304,8 +330,7 @@ static int add_to_entries(Onion_Announce *onion_a, IP_Port ret_ip_port, const ui
304 memcpy(onion_a->entries[pos].data_public_key, data_public_key, CRYPTO_PUBLIC_KEY_SIZE); 330 memcpy(onion_a->entries[pos].data_public_key, data_public_key, CRYPTO_PUBLIC_KEY_SIZE);
305 onion_a->entries[pos].time = unix_time(); 331 onion_a->entries[pos].time = unix_time();
306 332
307 memcpy(cmp_public_key, onion_a->dht->self_public_key, CRYPTO_PUBLIC_KEY_SIZE); 333 sort_onion_announce_list(onion_a->entries, ONION_ANNOUNCE_MAX_ENTRIES, onion_a->dht->self_public_key);
308 qsort(onion_a->entries, ONION_ANNOUNCE_MAX_ENTRIES, sizeof(Onion_Announce_Entry), cmp_entry);
309 return in_entries(onion_a, public_key); 334 return in_entries(onion_a, public_key);
310} 335}
311 336
diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c
index a20d3d70..58ac8e0d 100644
--- a/toxcore/onion_client.c
+++ b/toxcore/onion_client.c
@@ -471,12 +471,20 @@ static int client_send_announce_request(Onion_Client *onion_c, uint32_t num, IP_
471 return send_onion_packet_tcp_udp(onion_c, &path, dest, request, len); 471 return send_onion_packet_tcp_udp(onion_c, &path, dest, request, len);
472} 472}
473 473
474static uint8_t cmp_public_key[CRYPTO_PUBLIC_KEY_SIZE]; 474typedef struct {
475 const uint8_t *base_public_key;
476 Onion_Node entry;
477} Cmp_data;
478
475static int cmp_entry(const void *a, const void *b) 479static int cmp_entry(const void *a, const void *b)
476{ 480{
477 Onion_Node entry1, entry2; 481 Cmp_data cmp1, cmp2;
478 memcpy(&entry1, a, sizeof(Onion_Node)); 482 memcpy(&cmp1, a, sizeof(Cmp_data));
479 memcpy(&entry2, b, sizeof(Onion_Node)); 483 memcpy(&cmp2, b, sizeof(Cmp_data));
484 Onion_Node entry1 = cmp1.entry;
485 Onion_Node entry2 = cmp2.entry;
486 const uint8_t *cmp_public_key = cmp1.base_public_key;
487
480 int t1 = is_timeout(entry1.timestamp, ONION_NODE_TIMEOUT); 488 int t1 = is_timeout(entry1.timestamp, ONION_NODE_TIMEOUT);
481 int t2 = is_timeout(entry2.timestamp, ONION_NODE_TIMEOUT); 489 int t2 = is_timeout(entry2.timestamp, ONION_NODE_TIMEOUT);
482 490
@@ -505,6 +513,24 @@ static int cmp_entry(const void *a, const void *b)
505 return 0; 513 return 0;
506} 514}
507 515
516static void sort_onion_node_list(Onion_Node *list, unsigned int length, const uint8_t *comp_public_key)
517{
518 // Pass comp_public_key to qsort with each Client_data entry, so the
519 // comparison function can use it as the base of comparison.
520 Cmp_data cmp_list[length];
521
522 for (uint32_t i = 0; i < length; i++) {
523 cmp_list[i].base_public_key = comp_public_key;
524 cmp_list[i].entry = list[i];
525 }
526
527 qsort(cmp_list, length, sizeof(Cmp_data), cmp_entry);
528
529 for (uint32_t i = 0; i < length; i++) {
530 list[i] = cmp_list[i].entry;
531 }
532}
533
508static int client_add_to_list(Onion_Client *onion_c, uint32_t num, const uint8_t *public_key, IP_Port ip_port, 534static int client_add_to_list(Onion_Client *onion_c, uint32_t num, const uint8_t *public_key, IP_Port ip_port,
509 uint8_t is_stored, const uint8_t *pingid_or_key, uint32_t path_num) 535 uint8_t is_stored, const uint8_t *pingid_or_key, uint32_t path_num)
510{ 536{
@@ -534,8 +560,7 @@ static int client_add_to_list(Onion_Client *onion_c, uint32_t num, const uint8_t
534 list_length = MAX_ONION_CLIENTS; 560 list_length = MAX_ONION_CLIENTS;
535 } 561 }
536 562
537 memcpy(cmp_public_key, reference_id, CRYPTO_PUBLIC_KEY_SIZE); 563 sort_onion_node_list(list_nodes, list_length, reference_id);
538 qsort(list_nodes, list_length, sizeof(Onion_Node), cmp_entry);
539 564
540 int index = -1, stored = 0; 565 int index = -1, stored = 0;
541 unsigned int i; 566 unsigned int i;