summaryrefslogtreecommitdiff
path: root/core/DHT.c
diff options
context:
space:
mode:
Diffstat (limited to 'core/DHT.c')
-rw-r--r--core/DHT.c63
1 files changed, 5 insertions, 58 deletions
diff --git a/core/DHT.c b/core/DHT.c
index 6375b86b..f0b32df5 100644
--- a/core/DHT.c
+++ b/core/DHT.c
@@ -24,6 +24,7 @@
24/*----------------------------------------------------------------------------------*/ 24/*----------------------------------------------------------------------------------*/
25 25
26#include "DHT.h" 26#include "DHT.h"
27#include "ping.h"
27 28
28/* maximum number of clients stored per friend. */ 29/* maximum number of clients stored per friend. */
29#define MAX_FRIEND_CLIENTS 8 30#define MAX_FRIEND_CLIENTS 8
@@ -109,7 +110,6 @@ uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
109static Client_data close_clientlist[LCLIENT_LIST]; 110static Client_data close_clientlist[LCLIENT_LIST];
110static Friend * friends_list; 111static Friend * friends_list;
111static uint16_t num_friends; 112static uint16_t num_friends;
112static Pinged pings[LPING_ARRAY];
113static Pinged send_nodes[LSEND_NODES_ARRAY]; 113static Pinged send_nodes[LSEND_NODES_ARRAY];
114 114
115/*----------------------------------------------------------------------------------*/ 115/*----------------------------------------------------------------------------------*/
@@ -426,35 +426,6 @@ static void returnedip_ports(IP_Port ip_port, uint8_t * client_id, uint8_t * nod
426 } 426 }
427} 427}
428 428
429/* check if we are currently pinging an ip_port and/or a ping_id variables with
430 * values of zero will not be checked. If we are already, return 1 else return 0
431 *
432 * TODO: optimize this
433 */
434static int is_pinging(IP_Port ip_port, uint64_t ping_id)
435{
436 uint32_t i;
437 uint8_t pinging;
438 uint64_t temp_time = unix_time();
439
440 for (i = 0; i < LPING_ARRAY; ++i ) {
441 if (!is_timeout(temp_time, pings[i].timestamp, PING_TIMEOUT)) {
442 pinging = 0;
443
444 if (ip_port.ip.i != 0 && ipport_equal(pings[i].ip_port, ip_port))
445 ++pinging;
446
447 if (ping_id != 0 && pings[i].ping_id == ping_id)
448 ++pinging;
449
450 if (pinging == ((ping_id != 0) + (ip_port.ip.i != 0)))
451 return 1;
452 }
453 }
454
455 return 0;
456}
457
458/* Same as last function but for get_node requests. */ 429/* Same as last function but for get_node requests. */
459static int is_gettingnodes(IP_Port ip_port, uint64_t ping_id) 430static int is_gettingnodes(IP_Port ip_port, uint64_t ping_id)
460{ 431{
@@ -480,32 +451,6 @@ static int is_gettingnodes(IP_Port ip_port, uint64_t ping_id)
480 return 0; 451 return 0;
481} 452}
482 453
483/* Add a new ping request to the list of ping requests
484 * returns the ping_id to put in the ping request
485 * returns 0 if problem.
486 *
487 * TODO: optimize this
488 */
489static uint64_t add_pinging(IP_Port ip_port)
490{
491 uint32_t i, j;
492 uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int();
493 uint64_t temp_time = unix_time();
494
495 for(i = 0; i < PING_TIMEOUT; ++i ) {
496 for(j = 0; j < LPING_ARRAY; ++j ) {
497 if(is_timeout(temp_time, pings[j].timestamp, PING_TIMEOUT - i)) {
498 pings[j].timestamp = temp_time;
499 pings[j].ip_port = ip_port;
500 pings[j].ping_id = ping_id;
501 return ping_id;
502 }
503 }
504 }
505
506 return 0;
507}
508
509/* Same but for get node requests */ 454/* Same but for get node requests */
510static uint64_t add_gettingnodes(IP_Port ip_port) 455static uint64_t add_gettingnodes(IP_Port ip_port)
511{ 456{
@@ -536,7 +481,7 @@ static int pingreq(IP_Port ip_port, uint8_t * public_key)
536 if(id_equal(public_key, self_public_key) || is_pinging(ip_port, 0)) 481 if(id_equal(public_key, self_public_key) || is_pinging(ip_port, 0))
537 return 1; 482 return 1;
538 483
539 uint64_t ping_id = add_pinging(ip_port); 484 uint64_t ping_id = add_ping(ip_port);
540 if(ping_id == 0) 485 if(ping_id == 0)
541 return 1; 486 return 1;
542 487
@@ -1108,7 +1053,7 @@ static int send_NATping(uint8_t * public_key, uint64_t ping_id, uint8_t type)
1108static int handle_NATping(uint8_t * packet, uint32_t length, IP_Port source) 1053static int handle_NATping(uint8_t * packet, uint32_t length, IP_Port source)
1109{ 1054{
1110 if (length < crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + ENCRYPTION_PADDING 1055 if (length < crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + ENCRYPTION_PADDING
1111 && length > MAX_DATA_SIZE + ENCRYPTION_PADDING) 1056 || length > MAX_DATA_SIZE + ENCRYPTION_PADDING)
1112 return 1; 1057 return 1;
1113 1058
1114 /* check if request is for us. */ 1059 /* check if request is for us. */
@@ -1301,6 +1246,8 @@ void DHT_save(uint8_t * data)
1301 */ 1246 */
1302int DHT_load(uint8_t * data, uint32_t size) 1247int DHT_load(uint8_t * data, uint32_t size)
1303{ 1248{
1249 init_ping();
1250
1304 if(size < sizeof(close_clientlist)) 1251 if(size < sizeof(close_clientlist))
1305 return -1; 1252 return -1;
1306 1253