summaryrefslogtreecommitdiff
path: root/toxcore/ping.c
diff options
context:
space:
mode:
authorCoren[m] <Break@Ocean>2013-11-06 14:50:46 +0100
committerCoren[m] <Break@Ocean>2013-11-06 14:50:55 +0100
commitaee50435c849058c658f1a33486faea3d0fa0e3e (patch)
tree9fc4ddd35906adc9cda9ce6f115f104175db8724 /toxcore/ping.c
parent71f7a4940247db6fd1b6f74418fd775f0f4c2bd3 (diff)
addto_lists(): store the IP/Port that was used to *send*.
Avoids a DOS of sending a copy of a valid response with an invalid IP.
Diffstat (limited to 'toxcore/ping.c')
-rw-r--r--toxcore/ping.c43
1 files changed, 26 insertions, 17 deletions
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
111static 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 */
117static 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