diff options
author | zugz <mbays+tox@sdf.org> | 2017-02-11 15:21:06 +0100 |
---|---|---|
committer | zugz <mbays+tox@sdf.org> | 2017-02-11 15:27:48 +0100 |
commit | bbb979d6ef2299834cb38932a2d395eeaf119ff0 (patch) | |
tree | 7bb53229ae11b90fb01497c9e515b689573e9d31 /toxcore/onion_client.c | |
parent | 6ae33c16cf9e37fda85d70c78b3c2779eb8ca21a (diff) |
remove statics used in onion comparison functions
Diffstat (limited to 'toxcore/onion_client.c')
-rw-r--r-- | toxcore/onion_client.c | 37 |
1 files changed, 31 insertions, 6 deletions
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 | ||
474 | static uint8_t cmp_public_key[CRYPTO_PUBLIC_KEY_SIZE]; | 474 | typedef struct { |
475 | const uint8_t *base_public_key; | ||
476 | Onion_Node entry; | ||
477 | } Cmp_data; | ||
478 | |||
475 | static int cmp_entry(const void *a, const void *b) | 479 | static 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 | ||
516 | static 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 | |||
508 | static int client_add_to_list(Onion_Client *onion_c, uint32_t num, const uint8_t *public_key, IP_Port ip_port, | 534 | static 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; |