diff options
author | Diadlo <polsha3@gmail.com> | 2017-04-13 23:33:28 +0300 |
---|---|---|
committer | Diadlo <polsha3@gmail.com> | 2017-06-05 02:05:53 +0300 |
commit | ced07d6700021e4376317c25210550d88d2a83d8 (patch) | |
tree | f3131ab0e26467c17e98e2b012293b488d9cdc6b /toxcore/DHT.c | |
parent | f9607bced5cc7c9ccf6788c588c650e1f078b38f (diff) |
Change way to iterate through assoc
Diffstat (limited to 'toxcore/DHT.c')
-rw-r--r-- | toxcore/DHT.c | 72 |
1 files 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 @@ | |||
54 | /* Number of get node requests to send to quickly find close nodes. */ | 54 | /* Number of get node requests to send to quickly find close nodes. */ |
55 | #define MAX_BOOTSTRAP_TIMES 5 | 55 | #define MAX_BOOTSTRAP_TIMES 5 |
56 | 56 | ||
57 | #define ASSOC_COUNT 2 | ||
58 | |||
57 | /* Compares pk1 and pk2 with pk. | 59 | /* Compares pk1 and pk2 with pk. |
58 | * | 60 | * |
59 | * return 0 if both are same distance. | 61 | * 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) | |||
1554 | } | 1556 | } |
1555 | 1557 | ||
1556 | Client_data *client = &frnd->client_list[client_index]; | 1558 | Client_data *client = &frnd->client_list[client_index]; |
1557 | IPPTsPng *assoc; | 1559 | IPPTsPng *assocs[ASSOC_COUNT] = { &client->assoc6, &client->assoc4 }; |
1558 | uint32_t a; | 1560 | |
1561 | for (size_t i = 0; i < ASSOC_COUNT; i++) { | ||
1562 | IPPTsPng *assoc = assocs[i]; | ||
1559 | 1563 | ||
1560 | for (a = 0, assoc = &client->assoc6; a < 2; a++, assoc = &client->assoc4) { | ||
1561 | if (!is_timeout(assoc->timestamp, BAD_NODE_TIMEOUT)) { | 1564 | if (!is_timeout(assoc->timestamp, BAD_NODE_TIMEOUT)) { |
1562 | *ip_port = assoc->ip_port; | 1565 | *ip_port = assoc->ip_port; |
1563 | return 1; | 1566 | return 1; |
@@ -1583,10 +1586,12 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co | |||
1583 | for (uint32_t i = 0; i < list_count; i++) { | 1586 | for (uint32_t i = 0; i < list_count; i++) { |
1584 | /* If node is not dead. */ | 1587 | /* If node is not dead. */ |
1585 | Client_data *client = &list[i]; | 1588 | Client_data *client = &list[i]; |
1586 | IPPTsPng *assoc; | ||
1587 | uint32_t a; | ||
1588 | 1589 | ||
1589 | for (a = 0, assoc = &client->assoc6; a < 2; a++, assoc = &client->assoc4) { | 1590 | IPPTsPng *assocs[ASSOC_COUNT] = { &client->assoc6, &client->assoc4 }; |
1591 | |||
1592 | for (size_t i = 0; i < ASSOC_COUNT; i++) { | ||
1593 | IPPTsPng *assoc = assocs[i]; | ||
1594 | |||
1590 | if (!is_timeout(assoc->timestamp, KILL_NODE_TIMEOUT)) { | 1595 | if (!is_timeout(assoc->timestamp, KILL_NODE_TIMEOUT)) { |
1591 | sort = 0; | 1596 | sort = 0; |
1592 | not_kill++; | 1597 | not_kill++; |
@@ -1679,10 +1684,12 @@ static void do_Close(DHT *dht) | |||
1679 | 1684 | ||
1680 | for (size_t i = 0; i < LCLIENT_LIST; i++) { | 1685 | for (size_t i = 0; i < LCLIENT_LIST; i++) { |
1681 | Client_data *client = &dht->close_clientlist[i]; | 1686 | Client_data *client = &dht->close_clientlist[i]; |
1682 | IPPTsPng *assoc; | ||
1683 | uint32_t a; | ||
1684 | 1687 | ||
1685 | for (a = 0, assoc = &client->assoc4; a < 2; a++, assoc = &client->assoc6) { | 1688 | IPPTsPng *assocs[ASSOC_COUNT] = { &client->assoc6, &client->assoc4 }; |
1689 | |||
1690 | for (size_t j = 0; j < ASSOC_COUNT; j++) { | ||
1691 | IPPTsPng *assoc = assocs[j]; | ||
1692 | |||
1686 | if (assoc->timestamp) { | 1693 | if (assoc->timestamp) { |
1687 | assoc->timestamp = badonly; | 1694 | assoc->timestamp = badonly; |
1688 | } | 1695 | } |
@@ -1739,13 +1746,14 @@ int route_packet(const DHT *dht, const uint8_t *public_key, const uint8_t *packe | |||
1739 | for (uint32_t i = 0; i < LCLIENT_LIST; ++i) { | 1746 | for (uint32_t i = 0; i < LCLIENT_LIST; ++i) { |
1740 | if (id_equal(public_key, dht->close_clientlist[i].public_key)) { | 1747 | if (id_equal(public_key, dht->close_clientlist[i].public_key)) { |
1741 | const Client_data *client = &dht->close_clientlist[i]; | 1748 | const Client_data *client = &dht->close_clientlist[i]; |
1749 | const IPPTsPng *assocs[ASSOC_COUNT] = { &client->assoc6, &client->assoc4 }; | ||
1742 | 1750 | ||
1743 | if (ip_isset(&client->assoc6.ip_port.ip)) { | 1751 | for (size_t j = 0; j < ASSOC_COUNT; j++) { |
1744 | return sendpacket(dht->net, client->assoc6.ip_port, packet, length); | 1752 | const IPPTsPng *assoc = assocs[j]; |
1745 | } | ||
1746 | 1753 | ||
1747 | if (ip_isset(&client->assoc4.ip_port.ip)) { | 1754 | if (ip_isset(&assoc->ip_port.ip)) { |
1748 | return sendpacket(dht->net, client->assoc4.ip_port, packet, length); | 1755 | return sendpacket(dht->net, assoc->ip_port, packet, length); |
1756 | } | ||
1749 | } | 1757 | } |
1750 | 1758 | ||
1751 | break; | 1759 | break; |
@@ -1859,20 +1867,17 @@ int route_tofriend(const DHT *dht, const uint8_t *friend_id, const uint8_t *pack | |||
1859 | /* extra legwork, because having the outside allocating the space for us | 1867 | /* extra legwork, because having the outside allocating the space for us |
1860 | * is *usually* good(tm) (bites us in the behind in this case though) */ | 1868 | * is *usually* good(tm) (bites us in the behind in this case though) */ |
1861 | 1869 | ||
1862 | for (uint32_t a = 0; a < 2; a++) { | 1870 | for (uint32_t i = 0; i < MAX_FRIEND_CLIENTS; ++i) { |
1863 | for (uint32_t i = 0; i < MAX_FRIEND_CLIENTS; ++i) { | 1871 | if (friend_sent[i]) {/* Send one packet per client.*/ |
1864 | if (friend_sent[i]) {/* Send one packet per client.*/ | 1872 | continue; |
1865 | continue; | 1873 | } |
1866 | } | ||
1867 | 1874 | ||
1868 | client = &dht_friend->client_list[i]; | 1875 | client = &dht_friend->client_list[i]; |
1869 | IPPTsPng *assoc = NULL; | ||
1870 | 1876 | ||
1871 | if (!a) { | 1877 | const IPPTsPng *assocs[ASSOC_COUNT] = { &client->assoc4, &client->assoc6 }; |
1872 | assoc = &client->assoc4; | 1878 | |
1873 | } else { | 1879 | for (size_t j = 0; j < ASSOC_COUNT; j++) { |
1874 | assoc = &client->assoc6; | 1880 | const IPPTsPng *assoc = assocs[j]; |
1875 | } | ||
1876 | 1881 | ||
1877 | /* If ip is not zero and node is good. */ | 1882 | /* If ip is not zero and node is good. */ |
1878 | if (ip_isset(&assoc->ret_ip_port.ip) && !is_timeout(assoc->ret_timestamp, BAD_NODE_TIMEOUT)) { | 1883 | 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 * | |||
1910 | /* extra legwork, because having the outside allocating the space for us | 1915 | /* extra legwork, because having the outside allocating the space for us |
1911 | * is *usually* good(tm) (bites us in the behind in this case though) */ | 1916 | * is *usually* good(tm) (bites us in the behind in this case though) */ |
1912 | 1917 | ||
1913 | for (uint32_t a = 0; a < 2; a++) { | 1918 | for (uint32_t i = 0; i < MAX_FRIEND_CLIENTS; ++i) { |
1914 | for (uint32_t i = 0; i < MAX_FRIEND_CLIENTS; ++i) { | 1919 | client = &dht_friend->client_list[i]; |
1915 | client = &dht_friend->client_list[i]; | ||
1916 | IPPTsPng *assoc = NULL; | ||
1917 | 1920 | ||
1918 | if (!a) { | 1921 | const IPPTsPng *assocs[ASSOC_COUNT] = { &client->assoc4, &client->assoc6 }; |
1919 | assoc = &client->assoc4; | 1922 | |
1920 | } else { | 1923 | for (size_t j = 0; j < ASSOC_COUNT; j++) { |
1921 | assoc = &client->assoc6; | 1924 | const IPPTsPng *assoc = assocs[j]; |
1922 | } | ||
1923 | 1925 | ||
1924 | /* If ip is not zero and node is good. */ | 1926 | /* If ip is not zero and node is good. */ |
1925 | if (ip_isset(&assoc->ret_ip_port.ip) && !is_timeout(assoc->ret_timestamp, BAD_NODE_TIMEOUT)) { | 1927 | if (ip_isset(&assoc->ret_ip_port.ip) && !is_timeout(assoc->ret_timestamp, BAD_NODE_TIMEOUT)) { |