summaryrefslogtreecommitdiff
path: root/core/DHT.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-07-20 07:55:21 -0400
committerirungentoo <irungentoo@gmail.com>2013-07-20 07:55:21 -0400
commit11e94066f710a602a9a071b028036a2b35de2741 (patch)
tree0a6c037e40c0d8744516715c7b4e126d94e75841 /core/DHT.c
parent41cc5b19caae05fb751dc9b066b1ea98c263f585 (diff)
parenta72777ed70ed6bfcda8a532c52e0d64241a8bb71 (diff)
Merge branch 'master' of https://github.com/sometwo/ProjectTox-Core into sometwo-master
Conflicts: core/Messenger.c
Diffstat (limited to 'core/DHT.c')
-rw-r--r--core/DHT.c194
1 files changed, 92 insertions, 102 deletions
diff --git a/core/DHT.c b/core/DHT.c
index d4a3e806..2726aec6 100644
--- a/core/DHT.c
+++ b/core/DHT.c
@@ -35,13 +35,13 @@ typedef struct
35 uint32_t timestamp; 35 uint32_t timestamp;
36 uint32_t last_pinged; 36 uint32_t last_pinged;
37}Client_data; 37}Client_data;
38//maximum number of clients stored per friend. 38/* maximum number of clients stored per friend. */
39#define MAX_FRIEND_CLIENTS 8 39#define MAX_FRIEND_CLIENTS 8
40typedef struct 40typedef struct
41{ 41{
42 uint8_t client_id[CLIENT_ID_SIZE]; 42 uint8_t client_id[CLIENT_ID_SIZE];
43 Client_data client_list[MAX_FRIEND_CLIENTS]; 43 Client_data client_list[MAX_FRIEND_CLIENTS];
44 uint32_t lastgetnode;//time at which the last get_nodes request was sent. 44 uint32_t lastgetnode; /* time at which the last get_nodes request was sent. */
45 45
46}Friend; 46}Friend;
47 47
@@ -60,12 +60,12 @@ typedef struct
60 60
61}Pinged; 61}Pinged;
62 62
63//Our client id/public key 63/* Our client id/public key */
64uint8_t self_public_key[CLIENT_ID_SIZE]; 64uint8_t self_public_key[CLIENT_ID_SIZE];
65uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; 65uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
66 66
67//TODO: Move these out of here and put them into the .c file. 67/* TODO: Move these out of here and put them into the .c file.
68//A list of the clients mathematically closest to ours. 68 A list of the clients mathematically closest to ours. */
69#define LCLIENT_LIST 32 69#define LCLIENT_LIST 32
70static Client_data close_clientlist[LCLIENT_LIST]; 70static Client_data close_clientlist[LCLIENT_LIST];
71 71
@@ -74,7 +74,7 @@ static Client_data close_clientlist[LCLIENT_LIST];
74static Friend * friends_list; 74static Friend * friends_list;
75static uint16_t num_friends; 75static uint16_t num_friends;
76 76
77//The list of ip ports along with the ping_id of what we sent them and a timestamp 77/* The list of ip ports along with the ping_id of what we sent them and a timestamp */
78#define LPING_ARRAY 128 78#define LPING_ARRAY 128
79 79
80static Pinged pings[LPING_ARRAY]; 80static Pinged pings[LPING_ARRAY];
@@ -84,11 +84,11 @@ static Pinged pings[LPING_ARRAY];
84static Pinged send_nodes[LSEND_NODES_ARRAY]; 84static Pinged send_nodes[LSEND_NODES_ARRAY];
85 85
86 86
87//Compares client_id1 and client_id2 with client_id 87/* Compares client_id1 and client_id2 with client_id
88//return 0 if both are same distance 88 return 0 if both are same distance
89//return 1 if client_id1 is closer. 89 return 1 if client_id1 is closer
90//return 2 if client_id2 is closer. 90 return 2 if client_id2 is closer */
91int id_closest(uint8_t * client_id, uint8_t * client_id1, uint8_t * client_id2)//tested 91int id_closest(uint8_t * client_id, uint8_t * client_id1, uint8_t * client_id2) /* tested */
92{ 92{
93 uint32_t i; 93 uint32_t i;
94 for(i = 0; i < CLIENT_ID_SIZE; ++i) 94 for(i = 0; i < CLIENT_ID_SIZE; ++i)
@@ -108,11 +108,11 @@ int id_closest(uint8_t * client_id, uint8_t * client_id1, uint8_t * client_id2)/
108 108
109} 109}
110 110
111//check if client with client_id is already in list of length length. 111/* check if client with client_id is already in list of length length.
112//if it is set it's corresponding timestamp to current time. 112 if it is set it's corresponding timestamp to current time.
113//if the id is already in the list with a different ip_port, update it. 113 if the id is already in the list with a different ip_port, update it.
114//return True(1) or False(0) 114 return True(1) or False(0)
115//TODO: maybe optimize this. 115 TODO: maybe optimize this. */
116int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port) 116int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port)
117{ 117{
118 uint32_t i; 118 uint32_t i;
@@ -122,7 +122,7 @@ int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_
122 { 122 {
123 if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) 123 if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0)
124 { 124 {
125 //Refresh the client timestamp. 125 /* Refresh the client timestamp. */
126 list[i].timestamp = temp_time; 126 list[i].timestamp = temp_time;
127 list[i].ip_port.ip.i = ip_port.ip.i; 127 list[i].ip_port.ip.i = ip_port.ip.i;
128 list[i].ip_port.port = ip_port.port; 128 list[i].ip_port.port = ip_port.port;
@@ -133,8 +133,8 @@ int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_
133 133
134} 134}
135 135
136//check if client with client_id is already in node format list of length length. 136/* check if client with client_id is already in node format list of length length.
137//return True(1) or False(0) 137 return True(1) or False(0) */
138int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id) 138int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id)
139{ 139{
140 uint32_t i; 140 uint32_t i;
@@ -152,15 +152,15 @@ int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id)
152 152
153 153
154 154
155//the number of seconds for a non responsive node to become bad. 155/* the number of seconds for a non responsive node to become bad. */
156#define BAD_NODE_TIMEOUT 130 156#define BAD_NODE_TIMEOUT 130
157//The max number of nodes to send with send nodes. 157/* the max number of nodes to send with send nodes. */
158#define MAX_SENT_NODES 8 158#define MAX_SENT_NODES 8
159 159
160 160
161//Find MAX_SENT_NODES nodes closest to the client_id for the send nodes request: 161/* Find MAX_SENT_NODES nodes closest to the client_id for the send nodes request:
162//put them in the nodes_list and return how many were found. 162 put them in the nodes_list and return how many were found.
163//TODO: Make this function much more efficient. 163 TODO: Make this function much more efficient. */
164int get_close_nodes(uint8_t * client_id, Node_format * nodes_list) 164int get_close_nodes(uint8_t * client_id, Node_format * nodes_list)
165{ 165{
166 uint32_t i, j, k; 166 uint32_t i, j, k;
@@ -170,7 +170,7 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list)
170 { 170 {
171 if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time && 171 if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time &&
172 !client_in_nodelist(nodes_list, MAX_SENT_NODES,close_clientlist[i].client_id)) 172 !client_in_nodelist(nodes_list, MAX_SENT_NODES,close_clientlist[i].client_id))
173 //if node is good and not already in list. 173 /* if node is good and not already in list. */
174 { 174 {
175 if(num_nodes < MAX_SENT_NODES) 175 if(num_nodes < MAX_SENT_NODES)
176 { 176 {
@@ -196,7 +196,7 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list)
196 { 196 {
197 if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time && 197 if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time &&
198 !client_in_nodelist(nodes_list, MAX_SENT_NODES,friends_list[i].client_list[j].client_id)) 198 !client_in_nodelist(nodes_list, MAX_SENT_NODES,friends_list[i].client_list[j].client_id))
199 //if node is good and not already in list. 199 /* if node is good and not already in list. */
200 { 200 {
201 if(num_nodes < MAX_SENT_NODES) 201 if(num_nodes < MAX_SENT_NODES)
202 { 202 {
@@ -223,16 +223,16 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list)
223 223
224 224
225 225
226//replace first bad (or empty) node with this one 226/* replace first bad (or empty) node with this one
227//return 0 if successful 227 return 0 if successful
228//return 1 if not (list contains no bad nodes) 228 return 1 if not (list contains no bad nodes) */
229int replace_bad(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port)//tested 229int replace_bad(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port) /* tested */
230{ 230{
231 uint32_t i; 231 uint32_t i;
232 uint32_t temp_time = unix_time(); 232 uint32_t temp_time = unix_time();
233 for(i = 0; i < length; ++i) 233 for(i = 0; i < length; ++i)
234 { 234 {
235 if(list[i].timestamp + BAD_NODE_TIMEOUT < temp_time)//if node is bad. 235 if(list[i].timestamp + BAD_NODE_TIMEOUT < temp_time) /* if node is bad. */
236 { 236 {
237 memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); 237 memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE);
238 list[i].ip_port = ip_port; 238 list[i].ip_port = ip_port;
@@ -244,7 +244,7 @@ int replace_bad(Client_data * list, uint32_t length, uint8_t * client_id, IP_Por
244 244
245} 245}
246 246
247//replace the first good node that is further to the comp_client_id than that of the client_id in the list 247/* replace the first good node that is further to the comp_client_id than that of the client_id in the list */
248int replace_good(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port, uint8_t * comp_client_id) 248int replace_good(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port, uint8_t * comp_client_id)
249{ 249{
250 uint32_t i; 250 uint32_t i;
@@ -264,18 +264,18 @@ int replace_good(Client_data * list, uint32_t length, uint8_t * client_id, IP_Po
264 264
265} 265}
266 266
267//Attempt to add client with ip_port and client_id to the friends client list and close_clientlist 267/* Attempt to add client with ip_port and client_id to the friends client list and close_clientlist */
268void addto_lists(IP_Port ip_port, uint8_t * client_id) 268void addto_lists(IP_Port ip_port, uint8_t * client_id)
269{ 269{
270 uint32_t i; 270 uint32_t i;
271 271
272 //NOTE: current behavior if there are two clients with the same id is to replace the first ip by the second. 272 /* NOTE: current behavior if there are two clients with the same id is to replace the first ip by the second. */
273 if(!client_in_list(close_clientlist, LCLIENT_LIST, client_id, ip_port)) 273 if(!client_in_list(close_clientlist, LCLIENT_LIST, client_id, ip_port))
274 { 274 {
275 275
276 if(replace_bad(close_clientlist, LCLIENT_LIST, client_id, ip_port)) 276 if(replace_bad(close_clientlist, LCLIENT_LIST, client_id, ip_port))
277 { 277 {
278 //if we can't replace bad nodes we try replacing good ones 278 /* if we can't replace bad nodes we try replacing good ones */
279 replace_good(close_clientlist, LCLIENT_LIST, client_id, ip_port, self_public_key); 279 replace_good(close_clientlist, LCLIENT_LIST, client_id, ip_port, self_public_key);
280 } 280 }
281 281
@@ -287,7 +287,7 @@ void addto_lists(IP_Port ip_port, uint8_t * client_id)
287 287
288 if(replace_bad(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port)) 288 if(replace_bad(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port))
289 { 289 {
290 //if we can't replace bad nodes we try replacing good ones 290 /* if we can't replace bad nodes we try replacing good ones. */
291 replace_good(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port, friends_list[i].client_id); 291 replace_good(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port, friends_list[i].client_id);
292 } 292 }
293 } 293 }
@@ -295,13 +295,13 @@ void addto_lists(IP_Port ip_port, uint8_t * client_id)
295} 295}
296 296
297 297
298//ping timeout in seconds 298/* ping timeout in seconds */
299#define PING_TIMEOUT 5 299#define PING_TIMEOUT 5
300//check if we are currently pinging an ip_port and/or a ping_id 300/* check if we are currently pinging an ip_port and/or a ping_id
301//Variables with values of zero will not be checked. 301 variables with values of zero will not be checked.
302//if we are already, return 1 302 if we are already, return 1
303//else return 0 303 else return 0
304//TODO: Maybe optimize this 304TODO: optimize this */
305int is_pinging(IP_Port ip_port, uint64_t ping_id) 305int is_pinging(IP_Port ip_port, uint64_t ping_id)
306{ 306{
307 uint32_t i; 307 uint32_t i;
@@ -341,7 +341,7 @@ int is_pinging(IP_Port ip_port, uint64_t ping_id)
341} 341}
342 342
343 343
344//Same as last function but for get_node requests. 344/* Same as last function but for get_node requests. */
345int is_gettingnodes(IP_Port ip_port, uint64_t ping_id) 345int is_gettingnodes(IP_Port ip_port, uint64_t ping_id)
346{ 346{
347 uint32_t i; 347 uint32_t i;
@@ -381,10 +381,10 @@ int is_gettingnodes(IP_Port ip_port, uint64_t ping_id)
381} 381}
382 382
383 383
384//Add a new ping request to the list of ping requests 384/* Add a new ping request to the list of ping requests
385//returns the ping_id to put in the ping request 385 returns the ping_id to put in the ping request
386//returns 0 if problem. 386 returns 0 if problem.
387//TODO: Maybe optimize this 387 TODO: optimize this */
388uint64_t add_pinging(IP_Port ip_port) 388uint64_t add_pinging(IP_Port ip_port)
389{ 389{
390 uint32_t i, j; 390 uint32_t i, j;
@@ -408,7 +408,7 @@ uint64_t add_pinging(IP_Port ip_port)
408 408
409} 409}
410 410
411//Same but for get node requests 411/* Same but for get node requests */
412uint64_t add_gettingnodes(IP_Port ip_port) 412uint64_t add_gettingnodes(IP_Port ip_port)
413{ 413{
414 uint32_t i, j; 414 uint32_t i, j;
@@ -434,11 +434,11 @@ uint64_t add_gettingnodes(IP_Port ip_port)
434 434
435 435
436 436
437//send a ping request 437/* send a ping request
438//Ping request only works if none has been sent to that ip/port in the last 5 seconds. 438 Ping request only works if none has been sent to that ip/port in the last 5 seconds. */
439static int pingreq(IP_Port ip_port, uint8_t * public_key) 439static int pingreq(IP_Port ip_port, uint8_t * public_key)
440{ 440{
441 if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0)//check if packet is gonna be sent to ourself 441 if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */
442 { 442 {
443 return 1; 443 return 1;
444 } 444 }
@@ -474,10 +474,10 @@ static int pingreq(IP_Port ip_port, uint8_t * public_key)
474} 474}
475 475
476 476
477//send a ping response 477/* send a ping response */
478static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id) 478static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id)
479{ 479{
480 if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0)//check if packet is gonna be sent to ourself 480 if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */
481 { 481 {
482 return 1; 482 return 1;
483 } 483 }
@@ -501,10 +501,10 @@ static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id)
501 501
502} 502}
503 503
504//send a getnodes request 504/* send a getnodes request */
505static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id) 505static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id)
506{ 506{
507 if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0)//check if packet is gonna be sent to ourself 507 if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */
508 { 508 {
509 return 1; 509 return 1;
510 } 510 }
@@ -545,10 +545,10 @@ static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id)
545} 545}
546 546
547 547
548//send a send nodes response 548/* send a send nodes response */
549static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, uint64_t ping_id) 549static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, uint64_t ping_id)
550{ 550{
551 if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0)//check if packet is gonna be sent to ourself 551 if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */
552 { 552 {
553 return 1; 553 return 1;
554 } 554 }
@@ -591,10 +591,9 @@ static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id,
591 591
592 592
593 593
594 594/* Packet handling functions
595//Packet handling functions 595 One to handle each types of packets we receive
596//One to handle each types of packets we receive 596 return 0 if handled correctly, 1 if packet is bad. */
597//return 0 if handled correctly, 1 if packet is bad.
598int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source) 597int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source)
599{ 598{
600 uint64_t ping_id; 599 uint64_t ping_id;
@@ -602,7 +601,7 @@ int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source)
602 { 601 {
603 return 1; 602 return 1;
604 } 603 }
605 if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0)//check if packet is from ourself. 604 if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is from ourself. */
606 { 605 {
607 return 1; 606 return 1;
608 } 607 }
@@ -620,7 +619,7 @@ int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source)
620 619
621 pingres(source, packet + 1, ping_id); 620 pingres(source, packet + 1, ping_id);
622 621
623 pingreq(source, packet + 1);//TODO: make this smarter? 622 pingreq(source, packet + 1); /* TODO: make this smarter? */
624 623
625 return 0; 624 return 0;
626 625
@@ -633,7 +632,7 @@ int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source)
633 { 632 {
634 return 1; 633 return 1;
635 } 634 }
636 if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0)//check if packet is from ourself. 635 if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is from ourself. */
637 { 636 {
638 return 1; 637 return 1;
639 } 638 }
@@ -664,7 +663,7 @@ int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source)
664 { 663 {
665 return 1; 664 return 1;
666 } 665 }
667 if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0)//check if packet is from ourself. 666 if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is from ourself. */
668 { 667 {
669 return 1; 668 return 1;
670 } 669 }
@@ -684,7 +683,7 @@ int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source)
684 memcpy(&ping_id, plain, sizeof(ping_id)); 683 memcpy(&ping_id, plain, sizeof(ping_id));
685 sendnodes(source, packet + 1, plain + sizeof(ping_id), ping_id); 684 sendnodes(source, packet + 1, plain + sizeof(ping_id), ping_id);
686 685
687 pingreq(source, packet + 1);//TODO: make this smarter? 686 pingreq(source, packet + 1); /* TODO: make this smarter? */
688 687
689 return 0; 688 return 0;
690 689
@@ -737,9 +736,7 @@ int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source)
737 736
738} 737}
739 738
740//END of packet handling functions 739/* END of packet handling functions */
741
742
743 740
744int DHT_addfriend(uint8_t * client_id) 741int DHT_addfriend(uint8_t * client_id)
745{ 742{
@@ -766,15 +763,13 @@ int DHT_addfriend(uint8_t * client_id)
766 763
767 764
768 765
769
770
771int DHT_delfriend(uint8_t * client_id) 766int DHT_delfriend(uint8_t * client_id)
772{ 767{
773 uint32_t i; 768 uint32_t i;
774 Friend * temp; 769 Friend * temp;
775 for(i = 0; i < num_friends; ++i) 770 for(i = 0; i < num_friends; ++i)
776 { 771 {
777 if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0)//Equal 772 if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) /* Equal */
778 { 773 {
779 --num_friends; 774 --num_friends;
780 if(num_friends != i) 775 if(num_friends != i)
@@ -795,7 +790,7 @@ int DHT_delfriend(uint8_t * client_id)
795 790
796 791
797 792
798//TODO: Optimize this. 793/* TODO: Optimize this. */
799IP_Port DHT_getfriendip(uint8_t * client_id) 794IP_Port DHT_getfriendip(uint8_t * client_id)
800{ 795{
801 uint32_t i, j; 796 uint32_t i, j;
@@ -803,7 +798,7 @@ IP_Port DHT_getfriendip(uint8_t * client_id)
803 uint32_t temp_time = unix_time(); 798 uint32_t temp_time = unix_time();
804 for(i = 0; i < num_friends; ++i) 799 for(i = 0; i < num_friends; ++i)
805 { 800 {
806 if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0)//Equal 801 if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) /* Equal */
807 { 802 {
808 for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) 803 for(j = 0; j < MAX_FRIEND_CLIENTS; ++j)
809 { 804 {
@@ -821,9 +816,7 @@ IP_Port DHT_getfriendip(uint8_t * client_id)
821 empty.ip.i = 1; 816 empty.ip.i = 1;
822 return empty; 817 return empty;
823 818
824} 819}
825
826
827 820
828 821
829 822
@@ -846,22 +839,22 @@ int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source)
846 return 1; 839 return 1;
847 840
848 } 841 }
849 842
850 return 0; 843 return 0;
851 844
852} 845}
853 846
854//The timeout after which a node is discarded completely. 847/* The timeout after which a node is discarded completely. */
855#define Kill_NODE_TIMEOUT 300 848#define Kill_NODE_TIMEOUT 300
856 849
857//ping interval in seconds for each node in our lists. 850/* ping interval in seconds for each node in our lists. */
858#define PING_INTERVAL 60 851#define PING_INTERVAL 60
859 852
860//ping interval in seconds for each random sending of a get nodes request. 853/* ping interval in seconds for each random sending of a get nodes request. */
861#define GET_NODE_INTERVAL 10 854#define GET_NODE_INTERVAL 10
862 855
863//Ping each client in the "friends" list every 60 seconds. 856/* Ping each client in the "friends" list every 60 seconds.
864//Send a get nodes request every 20 seconds to a random good node for each "friend" in our "friends" list. 857 Send a get nodes request every 20 seconds to a random good node for each "friend" in our "friends" list. */
865 858
866 859
867void doDHTFriends() 860void doDHTFriends()
@@ -876,14 +869,14 @@ void doDHTFriends()
876 { 869 {
877 for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) 870 for(j = 0; j < MAX_FRIEND_CLIENTS; ++j)
878 { 871 {
879 if(friends_list[i].client_list[j].timestamp + Kill_NODE_TIMEOUT > temp_time)//if node is not dead. 872 if(friends_list[i].client_list[j].timestamp + Kill_NODE_TIMEOUT > temp_time) /* if node is not dead. */
880 { 873 {
881 if((friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) 874 if((friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time)
882 { 875 {
883 pingreq(friends_list[i].client_list[j].ip_port, friends_list[i].client_list[j].client_id); 876 pingreq(friends_list[i].client_list[j].ip_port, friends_list[i].client_list[j].client_id);
884 friends_list[i].client_list[j].last_pinged = temp_time; 877 friends_list[i].client_list[j].last_pinged = temp_time;
885 } 878 }
886 if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time)//if node is good. 879 if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time) /* if node is good. */
887 { 880 {
888 index[num_nodes] = j; 881 index[num_nodes] = j;
889 ++num_nodes; 882 ++num_nodes;
@@ -903,9 +896,9 @@ void doDHTFriends()
903 896
904static uint32_t close_lastgetnodes; 897static uint32_t close_lastgetnodes;
905 898
906//Ping each client in the close nodes list every 60 seconds. 899/* Ping each client in the close nodes list every 60 seconds.
907//Send a get nodes request every 20 seconds to a random good node in the list. 900 Send a get nodes request every 20 seconds to a random good node in the list. */
908void doClose()//tested 901void doClose() /* tested */
909{ 902{
910 uint32_t i; 903 uint32_t i;
911 uint32_t temp_time = unix_time(); 904 uint32_t temp_time = unix_time();
@@ -915,14 +908,14 @@ void doClose()//tested
915 908
916 for(i = 0; i < LCLIENT_LIST; ++i) 909 for(i = 0; i < LCLIENT_LIST; ++i)
917 { 910 {
918 if(close_clientlist[i].timestamp + Kill_NODE_TIMEOUT > temp_time)//if node is not dead. 911 if(close_clientlist[i].timestamp + Kill_NODE_TIMEOUT > temp_time) /* if node is not dead. */
919 { 912 {
920 if((close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) 913 if((close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time)
921 { 914 {
922 pingreq(close_clientlist[i].ip_port, close_clientlist[i].client_id); 915 pingreq(close_clientlist[i].ip_port, close_clientlist[i].client_id);
923 close_clientlist[i].last_pinged = temp_time; 916 close_clientlist[i].last_pinged = temp_time;
924 } 917 }
925 if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time)//if node is good. 918 if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time) /* if node is good. */
926 { 919 {
927 index[num_nodes] = i; 920 index[num_nodes] = i;
928 ++num_nodes; 921 ++num_nodes;
@@ -951,31 +944,28 @@ void doDHT()
951 944
952 945
953 946
954
955void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key) 947void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key)
956{ 948{
957 949 getnodes(ip_port, public_key, self_public_key);
958 getnodes(ip_port, public_key, self_public_key);
959
960} 950}
961 951
962 952
963//get the size of the DHT (for saving) 953/* get the size of the DHT (for saving) */
964uint32_t DHT_size() 954uint32_t DHT_size()
965{ 955{
966 return sizeof(close_clientlist) + sizeof(Friend) * num_friends; 956 return sizeof(close_clientlist) + sizeof(Friend) * num_friends;
967} 957}
968 958
969//save the DHT in data where data is an array of size DHT_size() 959/* save the DHT in data where data is an array of size DHT_size() */
970void DHT_save(uint8_t * data) 960void DHT_save(uint8_t * data)
971{ 961{
972 memcpy(data, close_clientlist, sizeof(close_clientlist)); 962 memcpy(data, close_clientlist, sizeof(close_clientlist));
973 memcpy(data + sizeof(close_clientlist), friends_list, sizeof(Friend) * num_friends); 963 memcpy(data + sizeof(close_clientlist), friends_list, sizeof(Friend) * num_friends);
974} 964}
975 965
976//load the DHT from data of size size; 966/* load the DHT from data of size size;
977//return -1 if failure 967 return -1 if failure
978//return 0 if success 968 return 0 if success */
979int DHT_load(uint8_t * data, uint32_t size) 969int DHT_load(uint8_t * data, uint32_t size)
980{ 970{
981 if(size < sizeof(close_clientlist)) 971 if(size < sizeof(close_clientlist))
@@ -987,7 +977,7 @@ int DHT_load(uint8_t * data, uint32_t size)
987 return -1; 977 return -1;
988 } 978 }
989 uint32_t i, j; 979 uint32_t i, j;
990 //uint32_t temp_time = unix_time(); 980 /* uint32_t temp_time = unix_time(); */
991 uint16_t temp; 981 uint16_t temp;
992 982
993 temp = (size - sizeof(close_clientlist))/sizeof(Friend); 983 temp = (size - sizeof(close_clientlist))/sizeof(Friend);
@@ -1021,8 +1011,8 @@ int DHT_load(uint8_t * data, uint32_t size)
1021 return 0; 1011 return 0;
1022} 1012}
1023 1013
1024//returns 0 if we are not connected to the DHT 1014/* returns 0 if we are not connected to the DHT
1025//returns 1 if we are 1015 returns 1 if we are */
1026int DHT_isconnected() 1016int DHT_isconnected()
1027{ 1017{
1028 uint32_t i; 1018 uint32_t i;