summaryrefslogtreecommitdiff
path: root/toxcore/ping.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-11-10 14:32:46 -0500
committerirungentoo <irungentoo@gmail.com>2013-11-10 14:32:46 -0500
commit57763f2737b6920d09e4f3fec5792a2c59ad2b21 (patch)
tree39950688279346bf80940af61bb1dc91f140755b /toxcore/ping.c
parent91c9dc2464b76217b2ac907861c4347c08cc7d80 (diff)
parent572fb60392f2bdbea0b738ab9cc4eca2aefe6585 (diff)
Merge branch 'master' into harden
Conflicts: toxcore/DHT.c
Diffstat (limited to 'toxcore/ping.c')
-rw-r--r--toxcore/ping.c39
1 files changed, 24 insertions, 15 deletions
diff --git a/toxcore/ping.c b/toxcore/ping.c
index 3f237836..80e85a45 100644
--- a/toxcore/ping.c
+++ b/toxcore/ping.c
@@ -114,28 +114,35 @@ static uint64_t add_ping(PING *ping, IP_Port ipp) // O(n)
114 return ping->pings[p].id; 114 return ping->pings[p].id;
115} 115}
116 116
117static bool is_pinging(PING *ping, IP_Port ipp, uint64_t ping_id) // O(n) TODO: Replace this with something else. 117/* checks if ip/port or ping_id are already in the list to ping
118 * if both are set, both must match, otherwise the set must match
119 *
120 * returns 0 if neither is set or no match was found
121 * returns the (index + 1) of the match if one was found
122 */
123static int is_pinging(PING *ping, IP_Port ipp, uint64_t ping_id)
118{ 124{
125 // O(n) TODO: Replace this with something else.
119 126
120 /* shouldn't that be an OR ? */ 127 /* at least one MUST be set */
121 if (!ip_isset(&ipp.ip) && ping_id == 0) 128 uint8_t ip_valid = ip_isset(&ipp.ip);
122 return false;
123 129
124 size_t i, id; 130 if (!ip_valid && !ping_id)
131 return 0;
132
133 size_t i;
125 134
126 remove_timeouts(ping); 135 remove_timeouts(ping);
127 136
128 for (i = 0; i < ping->num_pings; i++) { 137 for (i = 0; i < ping->num_pings; i++) {
129 id = (ping->pos_pings + i) % PING_NUM_MAX; 138 size_t id = (ping->pos_pings + i) % PING_NUM_MAX;
130 139
131 /* ping_id = 0 means match any id. */ 140 if (!ping_id || (ping->pings[id].id == ping_id))
132 if ((!ip_isset(&ipp.ip) || ipport_equal(&ping->pings[id].ip_port, &ipp)) && 141 if (!ip_valid || ipport_equal(&ping->pings[id].ip_port, &ipp))
133 (ping->pings[id].id == ping_id || ping_id == 0)) { 142 return id + 1;
134 return true;
135 }
136 } 143 }
137 144
138 return false; 145 return 0;
139} 146}
140 147
141#define DHT_PING_SIZE (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(uint64_t) + crypto_box_MACBYTES) 148#define DHT_PING_SIZE (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(uint64_t) + crypto_box_MACBYTES)
@@ -252,11 +259,13 @@ static int handle_ping_response(void *_dht, IP_Port source, uint8_t *packet, uin
252 return 1; 259 return 1;
253 260
254 /* Make sure ping_id is correct. */ 261 /* Make sure ping_id is correct. */
255 if (!is_pinging(ping, source, ping_id)) 262 int ping_index = is_pinging(ping, source, ping_id);
263
264 if (!ping_index)
256 return 1; 265 return 1;
257 266
258 // Associate source ip with client_id 267 /* Associate client_id with the ip the request was sent to */
259 addto_lists(dht, source, packet + 1); 268 addto_lists(dht, ping->pings[ping_index - 1].ip_port, packet + 1);
260 return 0; 269 return 0;
261} 270}
262 271