summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-11-14 13:02:08 -0800
committerirungentoo <irungentoo@gmail.com>2013-11-14 13:02:08 -0800
commit706b6ec11c5e3944a87579f9af5119f631afe85d (patch)
treeed743b468dc9939fb365d73d4ff6e90a9555ce01 /toxcore
parent3af83a6aecdbc719ef38986cba2f238eab3ba3ec (diff)
parent28dcfae6e42b028db22495185b6b99c48cd70c48 (diff)
Merge pull request #649 from FullName/DHT-client_id-change-wipe-other-address
On replacing a client with another, clear the stale association.
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/DHT.c75
1 files changed, 39 insertions, 36 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index eb50cc43..5e356b0a 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -115,21 +115,6 @@ static int client_id_cmp(ClientPair p1, ClientPair p2)
115 return c; 115 return c;
116} 116}
117 117
118static int client_in_list(Client_data *list, uint32_t length, uint8_t *client_id)
119{
120 uint32_t i;
121
122 for (i = 0; i < length; i++)
123
124 /* Dead nodes are considered dead (not in the list)*/
125 if (!is_timeout(list[i].assoc4.timestamp, KILL_NODE_TIMEOUT) ||
126 !is_timeout(list[i].assoc6.timestamp, KILL_NODE_TIMEOUT))
127 if (id_equal(list[i].client_id, client_id))
128 return 1;
129
130 return 0;
131}
132
133/* Check if client with client_id is already in list of length length. 118/* Check if client with client_id is already in list of length length.
134 * If it is then set its corresponding timestamp to current time. 119 * If it is then set its corresponding timestamp to current time.
135 * If the id is already in the list with a different ip_port, update it. 120 * If the id is already in the list with a different ip_port, update it.
@@ -387,21 +372,31 @@ static int replace_bad( Client_data *list,
387 for (i = 0; i < length; ++i) { 372 for (i = 0; i < length; ++i) {
388 /* If node is bad */ 373 /* If node is bad */
389 Client_data *client = &list[i]; 374 Client_data *client = &list[i];
390 IPPTsPng *ipptp = NULL;
391 375
392 if (ip_port.ip.family == AF_INET) 376 if (is_timeout(client->assoc4.timestamp, BAD_NODE_TIMEOUT) &&
393 ipptp = &client->assoc4; 377 is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT)) {
394 else 378
395 ipptp = &client->assoc6; 379 IPPTsPng *ipptp_write = NULL;
380 IPPTsPng *ipptp_clear = NULL;
381
382 if (ip_port.ip.family == AF_INET) {
383 ipptp_write = &client->assoc4;
384 ipptp_clear = &client->assoc6;
385 } else {
386 ipptp_write = &client->assoc6;
387 ipptp_clear = &client->assoc4;
388 }
396 389
397 if (is_timeout(ipptp->timestamp, BAD_NODE_TIMEOUT)) {
398 memcpy(client->client_id, client_id, CLIENT_ID_SIZE); 390 memcpy(client->client_id, client_id, CLIENT_ID_SIZE);
399 ipptp->ip_port = ip_port; 391 ipptp_write->ip_port = ip_port;
400 ipptp->timestamp = unix_time(); 392 ipptp_write->timestamp = unix_time();
401 393
402 ip_reset(&ipptp->ret_ip_port.ip); 394 ip_reset(&ipptp_write->ret_ip_port.ip);
403 ipptp->ret_ip_port.port = 0; 395 ipptp_write->ret_ip_port.port = 0;
404 ipptp->ret_timestamp = 0; 396 ipptp_write->ret_timestamp = 0;
397
398 /* zero out other address */
399 memset(ipptp_clear, 0, sizeof(*ipptp_clear));
405 400
406 return 0; 401 return 0;
407 } 402 }
@@ -463,20 +458,28 @@ static int replace_good( Client_data *list,
463 assert(replace >= 0 && replace < length); 458 assert(replace >= 0 && replace < length);
464#endif 459#endif
465 Client_data *client = &list[replace]; 460 Client_data *client = &list[replace];
466 IPPTsPng *ipptp = NULL; 461 IPPTsPng *ipptp_write = NULL;
462 IPPTsPng *ipptp_clear = NULL;
467 463
468 if (ip_port.ip.family == AF_INET) 464 if (ip_port.ip.family == AF_INET) {
469 ipptp = &client->assoc4; 465 ipptp_write = &client->assoc4;
470 else 466 ipptp_clear = &client->assoc6;
471 ipptp = &client->assoc6; 467 } else {
468 ipptp_write = &client->assoc6;
469 ipptp_clear = &client->assoc4;
470 }
472 471
473 memcpy(client->client_id, client_id, CLIENT_ID_SIZE); 472 memcpy(client->client_id, client_id, CLIENT_ID_SIZE);
474 ipptp->ip_port = ip_port; 473 ipptp_write->ip_port = ip_port;
475 ipptp->timestamp = unix_time(); 474 ipptp_write->timestamp = unix_time();
475
476 ip_reset(&ipptp_write->ret_ip_port.ip);
477 ipptp_write->ret_ip_port.port = 0;
478 ipptp_write->ret_timestamp = 0;
479
480 /* zero out other address */
481 memset(ipptp_clear, 0, sizeof(*ipptp_clear));
476 482
477 ip_reset(&ipptp->ret_ip_port.ip);
478 ipptp->ret_ip_port.port = 0;
479 ipptp->ret_timestamp = 0;
480 return 0; 483 return 0;
481 } 484 }
482 485