remove statics used in onion comparison functions

This commit is contained in:
zugz 2017-02-11 15:21:06 +01:00
parent 6ae33c16cf
commit bbb979d6ef
3 changed files with 63 additions and 13 deletions

View File

@ -866,7 +866,7 @@ static unsigned int store_node_ok(const Client_data *client, const uint8_t *publ
static void sort_client_list(Client_data *list, unsigned int length, const uint8_t *comp_public_key)
{
// Pass comp_public_key to qsort with each Client_data entry, so the
// comparison function cmp_dht_entry can use it as the base of comparison.
// comparison function can use it as the base of comparison.
Cmp_data cmp_list[length];
for (uint32_t i = 0; i < length; i++) {

View File

@ -233,12 +233,20 @@ static int in_entries(const Onion_Announce *onion_a, const uint8_t *public_key)
return -1;
}
static uint8_t cmp_public_key[CRYPTO_PUBLIC_KEY_SIZE];
typedef struct {
const uint8_t *base_public_key;
Onion_Announce_Entry entry;
} Cmp_data;
static int cmp_entry(const void *a, const void *b)
{
Onion_Announce_Entry entry1, entry2;
memcpy(&entry1, a, sizeof(Onion_Announce_Entry));
memcpy(&entry2, b, sizeof(Onion_Announce_Entry));
Cmp_data cmp1, cmp2;
memcpy(&cmp1, a, sizeof(Cmp_data));
memcpy(&cmp2, b, sizeof(Cmp_data));
Onion_Announce_Entry entry1 = cmp1.entry;
Onion_Announce_Entry entry2 = cmp2.entry;
const uint8_t *cmp_public_key = cmp1.base_public_key;
int t1 = is_timeout(entry1.time, ONION_ANNOUNCE_TIMEOUT);
int t2 = is_timeout(entry2.time, ONION_ANNOUNCE_TIMEOUT);
@ -267,6 +275,24 @@ static int cmp_entry(const void *a, const void *b)
return 0;
}
static void sort_onion_announce_list(Onion_Announce_Entry *list, unsigned int length, const uint8_t *comp_public_key)
{
// Pass comp_public_key to qsort with each Client_data entry, so the
// comparison function can use it as the base of comparison.
Cmp_data cmp_list[length];
for (uint32_t i = 0; i < length; i++) {
cmp_list[i].base_public_key = comp_public_key;
cmp_list[i].entry = list[i];
}
qsort(cmp_list, length, sizeof(Cmp_data), cmp_entry);
for (uint32_t i = 0; i < length; i++) {
list[i] = cmp_list[i].entry;
}
}
/* add entry to entries list
*
* return -1 if failure
@ -304,8 +330,7 @@ static int add_to_entries(Onion_Announce *onion_a, IP_Port ret_ip_port, const ui
memcpy(onion_a->entries[pos].data_public_key, data_public_key, CRYPTO_PUBLIC_KEY_SIZE);
onion_a->entries[pos].time = unix_time();
memcpy(cmp_public_key, onion_a->dht->self_public_key, CRYPTO_PUBLIC_KEY_SIZE);
qsort(onion_a->entries, ONION_ANNOUNCE_MAX_ENTRIES, sizeof(Onion_Announce_Entry), cmp_entry);
sort_onion_announce_list(onion_a->entries, ONION_ANNOUNCE_MAX_ENTRIES, onion_a->dht->self_public_key);
return in_entries(onion_a, public_key);
}

View File

@ -471,12 +471,20 @@ static int client_send_announce_request(Onion_Client *onion_c, uint32_t num, IP_
return send_onion_packet_tcp_udp(onion_c, &path, dest, request, len);
}
static uint8_t cmp_public_key[CRYPTO_PUBLIC_KEY_SIZE];
typedef struct {
const uint8_t *base_public_key;
Onion_Node entry;
} Cmp_data;
static int cmp_entry(const void *a, const void *b)
{
Onion_Node entry1, entry2;
memcpy(&entry1, a, sizeof(Onion_Node));
memcpy(&entry2, b, sizeof(Onion_Node));
Cmp_data cmp1, cmp2;
memcpy(&cmp1, a, sizeof(Cmp_data));
memcpy(&cmp2, b, sizeof(Cmp_data));
Onion_Node entry1 = cmp1.entry;
Onion_Node entry2 = cmp2.entry;
const uint8_t *cmp_public_key = cmp1.base_public_key;
int t1 = is_timeout(entry1.timestamp, ONION_NODE_TIMEOUT);
int t2 = is_timeout(entry2.timestamp, ONION_NODE_TIMEOUT);
@ -505,6 +513,24 @@ static int cmp_entry(const void *a, const void *b)
return 0;
}
static void sort_onion_node_list(Onion_Node *list, unsigned int length, const uint8_t *comp_public_key)
{
// Pass comp_public_key to qsort with each Client_data entry, so the
// comparison function can use it as the base of comparison.
Cmp_data cmp_list[length];
for (uint32_t i = 0; i < length; i++) {
cmp_list[i].base_public_key = comp_public_key;
cmp_list[i].entry = list[i];
}
qsort(cmp_list, length, sizeof(Cmp_data), cmp_entry);
for (uint32_t i = 0; i < length; i++) {
list[i] = cmp_list[i].entry;
}
}
static int client_add_to_list(Onion_Client *onion_c, uint32_t num, const uint8_t *public_key, IP_Port ip_port,
uint8_t is_stored, const uint8_t *pingid_or_key, uint32_t path_num)
{
@ -534,8 +560,7 @@ static int client_add_to_list(Onion_Client *onion_c, uint32_t num, const uint8_t
list_length = MAX_ONION_CLIENTS;
}
memcpy(cmp_public_key, reference_id, CRYPTO_PUBLIC_KEY_SIZE);
qsort(list_nodes, list_length, sizeof(Onion_Node), cmp_entry);
sort_onion_node_list(list_nodes, list_length, reference_id);
int index = -1, stored = 0;
unsigned int i;