From ced07d6700021e4376317c25210550d88d2a83d8 Mon Sep 17 00:00:00 2001 From: Diadlo Date: Thu, 13 Apr 2017 23:33:28 +0300 Subject: [PATCH] Change way to iterate through assoc --- toxcore/DHT.c | 72 ++++++++++++++++++++++++++------------------------- 1 file changed, 37 insertions(+), 35 deletions(-) diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 43230853..e12c4d64 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -54,6 +54,8 @@ /* Number of get node requests to send to quickly find close nodes. */ #define MAX_BOOTSTRAP_TIMES 5 +#define ASSOC_COUNT 2 + /* Compares pk1 and pk2 with pk. * * return 0 if both are same distance. @@ -1554,10 +1556,11 @@ int DHT_getfriendip(const DHT *dht, const uint8_t *public_key, IP_Port *ip_port) } Client_data *client = &frnd->client_list[client_index]; - IPPTsPng *assoc; - uint32_t a; + IPPTsPng *assocs[ASSOC_COUNT] = { &client->assoc6, &client->assoc4 }; + + for (size_t i = 0; i < ASSOC_COUNT; i++) { + IPPTsPng *assoc = assocs[i]; - for (a = 0, assoc = &client->assoc6; a < 2; a++, assoc = &client->assoc4) { if (!is_timeout(assoc->timestamp, BAD_NODE_TIMEOUT)) { *ip_port = assoc->ip_port; return 1; @@ -1583,10 +1586,12 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co for (uint32_t i = 0; i < list_count; i++) { /* If node is not dead. */ Client_data *client = &list[i]; - IPPTsPng *assoc; - uint32_t a; - for (a = 0, assoc = &client->assoc6; a < 2; a++, assoc = &client->assoc4) { + IPPTsPng *assocs[ASSOC_COUNT] = { &client->assoc6, &client->assoc4 }; + + for (size_t i = 0; i < ASSOC_COUNT; i++) { + IPPTsPng *assoc = assocs[i]; + if (!is_timeout(assoc->timestamp, KILL_NODE_TIMEOUT)) { sort = 0; not_kill++; @@ -1679,10 +1684,12 @@ static void do_Close(DHT *dht) for (size_t i = 0; i < LCLIENT_LIST; i++) { Client_data *client = &dht->close_clientlist[i]; - IPPTsPng *assoc; - uint32_t a; - for (a = 0, assoc = &client->assoc4; a < 2; a++, assoc = &client->assoc6) { + IPPTsPng *assocs[ASSOC_COUNT] = { &client->assoc6, &client->assoc4 }; + + for (size_t j = 0; j < ASSOC_COUNT; j++) { + IPPTsPng *assoc = assocs[j]; + if (assoc->timestamp) { assoc->timestamp = badonly; } @@ -1739,13 +1746,14 @@ int route_packet(const DHT *dht, const uint8_t *public_key, const uint8_t *packe for (uint32_t i = 0; i < LCLIENT_LIST; ++i) { if (id_equal(public_key, dht->close_clientlist[i].public_key)) { const Client_data *client = &dht->close_clientlist[i]; + const IPPTsPng *assocs[ASSOC_COUNT] = { &client->assoc6, &client->assoc4 }; - if (ip_isset(&client->assoc6.ip_port.ip)) { - return sendpacket(dht->net, client->assoc6.ip_port, packet, length); - } + for (size_t j = 0; j < ASSOC_COUNT; j++) { + const IPPTsPng *assoc = assocs[j]; - if (ip_isset(&client->assoc4.ip_port.ip)) { - return sendpacket(dht->net, client->assoc4.ip_port, packet, length); + if (ip_isset(&assoc->ip_port.ip)) { + return sendpacket(dht->net, assoc->ip_port, packet, length); + } } break; @@ -1859,20 +1867,17 @@ int route_tofriend(const DHT *dht, const uint8_t *friend_id, const uint8_t *pack /* extra legwork, because having the outside allocating the space for us * is *usually* good(tm) (bites us in the behind in this case though) */ - for (uint32_t a = 0; a < 2; a++) { - for (uint32_t i = 0; i < MAX_FRIEND_CLIENTS; ++i) { - if (friend_sent[i]) {/* Send one packet per client.*/ - continue; - } + for (uint32_t i = 0; i < MAX_FRIEND_CLIENTS; ++i) { + if (friend_sent[i]) {/* Send one packet per client.*/ + continue; + } - client = &dht_friend->client_list[i]; - IPPTsPng *assoc = NULL; + client = &dht_friend->client_list[i]; - if (!a) { - assoc = &client->assoc4; - } else { - assoc = &client->assoc6; - } + const IPPTsPng *assocs[ASSOC_COUNT] = { &client->assoc4, &client->assoc6 }; + + for (size_t j = 0; j < ASSOC_COUNT; j++) { + const IPPTsPng *assoc = assocs[j]; /* If ip is not zero and node is good. */ if (ip_isset(&assoc->ret_ip_port.ip) && !is_timeout(assoc->ret_timestamp, BAD_NODE_TIMEOUT)) { @@ -1910,16 +1915,13 @@ static int routeone_tofriend(DHT *dht, const uint8_t *friend_id, const uint8_t * /* extra legwork, because having the outside allocating the space for us * is *usually* good(tm) (bites us in the behind in this case though) */ - for (uint32_t a = 0; a < 2; a++) { - for (uint32_t i = 0; i < MAX_FRIEND_CLIENTS; ++i) { - client = &dht_friend->client_list[i]; - IPPTsPng *assoc = NULL; + for (uint32_t i = 0; i < MAX_FRIEND_CLIENTS; ++i) { + client = &dht_friend->client_list[i]; - if (!a) { - assoc = &client->assoc4; - } else { - assoc = &client->assoc6; - } + const IPPTsPng *assocs[ASSOC_COUNT] = { &client->assoc4, &client->assoc6 }; + + for (size_t j = 0; j < ASSOC_COUNT; j++) { + const IPPTsPng *assoc = assocs[j]; /* If ip is not zero and node is good. */ if (ip_isset(&assoc->ret_ip_port.ip) && !is_timeout(assoc->ret_timestamp, BAD_NODE_TIMEOUT)) {