diff options
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/DHT.c | 36 | ||||
-rw-r--r-- | toxcore/ping.c | 43 |
2 files changed, 46 insertions, 33 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 3ce2fe84..eb50cc43 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c | |||
@@ -572,25 +572,26 @@ static void returnedip_ports(DHT *dht, IP_Port ip_port, uint8_t *client_id, uint | |||
572 | } | 572 | } |
573 | } | 573 | } |
574 | 574 | ||
575 | /* checks if ip/port or ping_id are already in the list to get nodes | ||
576 | * if both are set, both must match, otherwise the set must match | ||
577 | * | ||
578 | * returns 0 if neither is set or no match was found | ||
579 | * returns the (index + 1) of the match if one was found | ||
580 | */ | ||
575 | static int is_gettingnodes(DHT *dht, IP_Port ip_port, uint64_t ping_id) | 581 | static int is_gettingnodes(DHT *dht, IP_Port ip_port, uint64_t ping_id) |
576 | { | 582 | { |
577 | uint32_t i; | 583 | uint8_t ip_valid = ip_isset(&ip_port.ip); |
578 | uint8_t pinging; | ||
579 | 584 | ||
580 | for (i = 0; i < LSEND_NODES_ARRAY; ++i ) { | 585 | if (!ip_valid && !ping_id) |
581 | if (!is_timeout(dht->send_nodes[i].timestamp, PING_TIMEOUT)) { | 586 | return 0; |
582 | pinging = 0; | ||
583 | |||
584 | if (ping_id != 0 && dht->send_nodes[i].id == ping_id) | ||
585 | ++pinging; | ||
586 | 587 | ||
587 | if (ip_isset(&ip_port.ip) && ipport_equal(&dht->send_nodes[i].ip_port, &ip_port)) | 588 | uint32_t i; |
588 | ++pinging; | ||
589 | 589 | ||
590 | if (pinging == (ping_id != 0) + ip_isset(&ip_port.ip)) | 590 | for (i = 0; i < LSEND_NODES_ARRAY; i++) |
591 | return 1; | 591 | if (!is_timeout(dht->send_nodes[i].timestamp, PING_TIMEOUT)) |
592 | } | 592 | if (!ping_id || (dht->send_nodes[i].id == ping_id)) |
593 | } | 593 | if (!ip_valid || ipport_equal(&dht->send_nodes[i].ip_port, &ip_port)) |
594 | return i + 1; | ||
594 | 595 | ||
595 | return 0; | 596 | return 0; |
596 | } | 597 | } |
@@ -841,10 +842,13 @@ static int handle_sendnodes_core(void *object, IP_Port source, uint8_t *packet, | |||
841 | 842 | ||
842 | memcpy(&ping_id, plain, sizeof(ping_id)); | 843 | memcpy(&ping_id, plain, sizeof(ping_id)); |
843 | 844 | ||
844 | if (!is_gettingnodes(dht, source, ping_id)) | 845 | int send_nodes_index = is_gettingnodes(dht, source, ping_id); |
846 | |||
847 | if (!send_nodes_index) | ||
845 | return 1; | 848 | return 1; |
846 | 849 | ||
847 | addto_lists(dht, source, packet + 1); | 850 | /* store the address the *request* was sent to */ |
851 | addto_lists(dht, dht->send_nodes[send_nodes_index - 1].ip_port, packet + 1); | ||
848 | 852 | ||
849 | *num_nodes_out = num_nodes; | 853 | *num_nodes_out = num_nodes; |
850 | 854 | ||
diff --git a/toxcore/ping.c b/toxcore/ping.c index c7b829b4..bd754c53 100644 --- a/toxcore/ping.c +++ b/toxcore/ping.c | |||
@@ -3,7 +3,7 @@ | |||
3 | * | 3 | * |
4 | * This file is donated to the Tox Project. | 4 | * This file is donated to the Tox Project. |
5 | * Copyright 2013 plutooo | 5 | * Copyright 2013 plutooo |
6 | * | 6 | * |
7 | * Copyright (C) 2013 Tox project All Rights Reserved. | 7 | * Copyright (C) 2013 Tox project All Rights Reserved. |
8 | * | 8 | * |
9 | * This file is part of Tox. | 9 | * This file is part of Tox. |
@@ -20,7 +20,7 @@ | |||
20 | * | 20 | * |
21 | * You should have received a copy of the GNU General Public License | 21 | * You should have received a copy of the GNU General Public License |
22 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | 22 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. |
23 | * | 23 | * |
24 | */ | 24 | */ |
25 | 25 | ||
26 | #ifdef HAVE_CONFIG_H | 26 | #ifdef HAVE_CONFIG_H |
@@ -108,28 +108,35 @@ static uint64_t add_ping(PING *ping, IP_Port ipp) // O(n) | |||
108 | return ping->pings[p].id; | 108 | return ping->pings[p].id; |
109 | } | 109 | } |
110 | 110 | ||
111 | static bool is_pinging(PING *ping, IP_Port ipp, uint64_t ping_id) // O(n) TODO: Replace this with something else. | 111 | /* checks if ip/port or ping_id are already in the list to ping |
112 | * if both are set, both must match, otherwise the set must match | ||
113 | * | ||
114 | * returns 0 if neither is set or no match was found | ||
115 | * returns the (index + 1) of the match if one was found | ||
116 | */ | ||
117 | static int is_pinging(PING *ping, IP_Port ipp, uint64_t ping_id) | ||
112 | { | 118 | { |
119 | // O(n) TODO: Replace this with something else. | ||
113 | 120 | ||
114 | /* shouldn't that be an OR ? */ | 121 | /* at least one MUST be set */ |
115 | if (!ip_isset(&ipp.ip) && ping_id == 0) | 122 | uint8_t ip_valid = ip_isset(&ipp.ip); |
116 | return false; | ||
117 | 123 | ||
118 | size_t i, id; | 124 | if (!ip_valid && !ping_id) |
125 | return 0; | ||
126 | |||
127 | size_t i; | ||
119 | 128 | ||
120 | remove_timeouts(ping); | 129 | remove_timeouts(ping); |
121 | 130 | ||
122 | for (i = 0; i < ping->num_pings; i++) { | 131 | for (i = 0; i < ping->num_pings; i++) { |
123 | id = (ping->pos_pings + i) % PING_NUM_MAX; | 132 | size_t id = (ping->pos_pings + i) % PING_NUM_MAX; |
124 | 133 | ||
125 | /* ping_id = 0 means match any id. */ | 134 | if (!ping_id || (ping->pings[id].id == ping_id)) |
126 | if ((!ip_isset(&ipp.ip) || ipport_equal(&ping->pings[id].ip_port, &ipp)) && | 135 | if (!ip_valid || ipport_equal(&ping->pings[id].ip_port, &ipp)) |
127 | (ping->pings[id].id == ping_id || ping_id == 0)) { | 136 | return id + 1; |
128 | return true; | ||
129 | } | ||
130 | } | 137 | } |
131 | 138 | ||
132 | return false; | 139 | return 0; |
133 | } | 140 | } |
134 | 141 | ||
135 | #define DHT_PING_SIZE (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(uint64_t) + ENCRYPTION_PADDING) | 142 | #define DHT_PING_SIZE (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(uint64_t) + ENCRYPTION_PADDING) |
@@ -246,11 +253,13 @@ static int handle_ping_response(void *_dht, IP_Port source, uint8_t *packet, uin | |||
246 | return 1; | 253 | return 1; |
247 | 254 | ||
248 | /* Make sure ping_id is correct. */ | 255 | /* Make sure ping_id is correct. */ |
249 | if (!is_pinging(ping, source, ping_id)) | 256 | int ping_index = is_pinging(ping, source, ping_id); |
257 | |||
258 | if (!ping_index) | ||
250 | return 1; | 259 | return 1; |
251 | 260 | ||
252 | // Associate source ip with client_id | 261 | /* Associate client_id with the ip the request was sent to */ |
253 | addto_lists(dht, source, packet + 1); | 262 | addto_lists(dht, ping->pings[ping_index - 1].ip_port, packet + 1); |
254 | return 0; | 263 | return 0; |
255 | } | 264 | } |
256 | 265 | ||