diff options
Diffstat (limited to 'toxcore/ping.c')
-rw-r--r-- | toxcore/ping.c | 27 |
1 files changed, 10 insertions, 17 deletions
diff --git a/toxcore/ping.c b/toxcore/ping.c index 240bd713..3f237836 100644 --- a/toxcore/ping.c +++ b/toxcore/ping.c | |||
@@ -64,7 +64,7 @@ typedef struct { | |||
64 | 64 | ||
65 | static bool is_ping_timeout(uint64_t time) | 65 | static bool is_ping_timeout(uint64_t time) |
66 | { | 66 | { |
67 | return (time + PING_TIMEOUT) < now(); | 67 | return is_timeout(time, PING_TIMEOUT); |
68 | } | 68 | } |
69 | 69 | ||
70 | static void remove_timeouts(PING *ping) // O(n) | 70 | static void remove_timeouts(PING *ping) // O(n) |
@@ -107,7 +107,7 @@ static uint64_t add_ping(PING *ping, IP_Port ipp) // O(n) | |||
107 | p = (ping->pos_pings + ping->num_pings) % PING_NUM_MAX; | 107 | p = (ping->pos_pings + ping->num_pings) % PING_NUM_MAX; |
108 | 108 | ||
109 | ping->pings[p].ip_port = ipp; | 109 | ping->pings[p].ip_port = ipp; |
110 | ping->pings[p].timestamp = now(); | 110 | ping->pings[p].timestamp = unix_time(); |
111 | ping->pings[p].id = random_64b(); | 111 | ping->pings[p].id = random_64b(); |
112 | 112 | ||
113 | ping->num_pings++; | 113 | ping->num_pings++; |
@@ -146,14 +146,14 @@ int send_ping_request(PING *ping, IP_Port ipp, uint8_t *client_id) | |||
146 | int rc; | 146 | int rc; |
147 | uint64_t ping_id; | 147 | uint64_t ping_id; |
148 | 148 | ||
149 | if (is_pinging(ping, ipp, 0) || id_eq(client_id, ping->c->self_public_key)) | 149 | if (is_pinging(ping, ipp, 0) || id_equal(client_id, ping->c->self_public_key)) |
150 | return 1; | 150 | return 1; |
151 | 151 | ||
152 | // Generate random ping_id. | 152 | // Generate random ping_id. |
153 | ping_id = add_ping(ping, ipp); | 153 | ping_id = add_ping(ping, ipp); |
154 | 154 | ||
155 | pk[0] = NET_PACKET_PING_REQUEST; | 155 | pk[0] = NET_PACKET_PING_REQUEST; |
156 | id_cpy(pk + 1, ping->c->self_public_key); // Our pubkey | 156 | id_copy(pk + 1, ping->c->self_public_key); // Our pubkey |
157 | new_nonce(pk + 1 + CLIENT_ID_SIZE); // Generate new nonce | 157 | new_nonce(pk + 1 + CLIENT_ID_SIZE); // Generate new nonce |
158 | 158 | ||
159 | // Encrypt ping_id using recipient privkey | 159 | // Encrypt ping_id using recipient privkey |
@@ -174,11 +174,11 @@ static int send_ping_response(PING *ping, IP_Port ipp, uint8_t *client_id, uint6 | |||
174 | uint8_t pk[DHT_PING_SIZE]; | 174 | uint8_t pk[DHT_PING_SIZE]; |
175 | int rc; | 175 | int rc; |
176 | 176 | ||
177 | if (id_eq(client_id, ping->c->self_public_key)) | 177 | if (id_equal(client_id, ping->c->self_public_key)) |
178 | return 1; | 178 | return 1; |
179 | 179 | ||
180 | pk[0] = NET_PACKET_PING_RESPONSE; | 180 | pk[0] = NET_PACKET_PING_RESPONSE; |
181 | id_cpy(pk + 1, ping->c->self_public_key); // Our pubkey | 181 | id_copy(pk + 1, ping->c->self_public_key); // Our pubkey |
182 | new_nonce(pk + 1 + CLIENT_ID_SIZE); // Generate new nonce | 182 | new_nonce(pk + 1 + CLIENT_ID_SIZE); // Generate new nonce |
183 | 183 | ||
184 | // Encrypt ping_id using recipient privkey | 184 | // Encrypt ping_id using recipient privkey |
@@ -205,7 +205,7 @@ static int handle_ping_request(void *_dht, IP_Port source, uint8_t *packet, uint | |||
205 | 205 | ||
206 | PING *ping = dht->ping; | 206 | PING *ping = dht->ping; |
207 | 207 | ||
208 | if (id_eq(packet + 1, ping->c->self_public_key)) | 208 | if (id_equal(packet + 1, ping->c->self_public_key)) |
209 | return 1; | 209 | return 1; |
210 | 210 | ||
211 | // Decrypt ping_id | 211 | // Decrypt ping_id |
@@ -237,7 +237,7 @@ static int handle_ping_response(void *_dht, IP_Port source, uint8_t *packet, uin | |||
237 | 237 | ||
238 | PING *ping = dht->ping; | 238 | PING *ping = dht->ping; |
239 | 239 | ||
240 | if (id_eq(packet + 1, ping->c->self_public_key)) | 240 | if (id_equal(packet + 1, ping->c->self_public_key)) |
241 | return 1; | 241 | return 1; |
242 | 242 | ||
243 | // Decrypt ping_id | 243 | // Decrypt ping_id |
@@ -301,19 +301,12 @@ int add_toping(PING *ping, uint8_t *client_id, IP_Port ip_port) | |||
301 | /* Ping all the valid nodes in the toping list every TIME_TOPING seconds. | 301 | /* Ping all the valid nodes in the toping list every TIME_TOPING seconds. |
302 | * This function must be run at least once every TIME_TOPING seconds. | 302 | * This function must be run at least once every TIME_TOPING seconds. |
303 | */ | 303 | */ |
304 | static int is_timeout(uint64_t time_now, uint64_t timestamp, uint64_t timeout) | ||
305 | { | ||
306 | return timestamp + timeout <= time_now; | ||
307 | } | ||
308 | |||
309 | void do_toping(PING *ping) | 304 | void do_toping(PING *ping) |
310 | { | 305 | { |
311 | uint64_t temp_time = unix_time(); | 306 | if (!is_timeout(ping->last_toping, TIME_TOPING)) |
312 | |||
313 | if (!is_timeout(temp_time, ping->last_toping, TIME_TOPING)) | ||
314 | return; | 307 | return; |
315 | 308 | ||
316 | ping->last_toping = temp_time; | 309 | ping->last_toping = unix_time(); |
317 | uint32_t i; | 310 | uint32_t i; |
318 | 311 | ||
319 | for (i = 0; i < MAX_TOPING; ++i) { | 312 | for (i = 0; i < MAX_TOPING; ++i) { |