summaryrefslogtreecommitdiff
path: root/core/DHT.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-07-05 17:00:39 -0400
committerirungentoo <irungentoo@gmail.com>2013-07-05 17:00:39 -0400
commita480c0195a78f56116b3bf58fe17d930bf4e64f4 (patch)
treec549b6347e28b8570ed27b5c53b768b8f7b11a61 /core/DHT.c
parent358f46f6483f0c24186272914952e44221c76871 (diff)
Crypto done(still need to test it a bit more thought)
Replaced chars with uint8_t Added a new test program. Added some functions to Lossless UDP. And some other stuff.
Diffstat (limited to 'core/DHT.c')
-rw-r--r--core/DHT.c44
1 files changed, 22 insertions, 22 deletions
diff --git a/core/DHT.c b/core/DHT.c
index 5c63146f..d5af3a08 100644
--- a/core/DHT.c
+++ b/core/DHT.c
@@ -27,13 +27,13 @@
27 27
28#include "DHT.h" 28#include "DHT.h"
29 29
30char self_client_id[CLIENT_ID_SIZE]; 30uint8_t self_client_id[CLIENT_ID_SIZE];
31 31
32//Compares client_id1 and client_id2 with client_id 32//Compares client_id1 and client_id2 with client_id
33//return 0 if both are same distance 33//return 0 if both are same distance
34//return 1 if client_id1 is closer. 34//return 1 if client_id1 is closer.
35//return 2 if client_id2 is closer. 35//return 2 if client_id2 is closer.
36int id_closest(char * client_id, char * client_id1, char * client_id2)//tested 36int id_closest(uint8_t * client_id, uint8_t * client_id1, uint8_t * client_id2)//tested
37{ 37{
38 uint32_t i; 38 uint32_t i;
39 for(i = 0; i < CLIENT_ID_SIZE; i++) 39 for(i = 0; i < CLIENT_ID_SIZE; i++)
@@ -58,7 +58,7 @@ int id_closest(char * client_id, char * client_id1, char * client_id2)//tested
58//if the ip_port is already in the list but associated to a different ip, change it. 58//if the ip_port is already in the list but associated to a different ip, change it.
59//return True(1) or False(0) 59//return True(1) or False(0)
60//TODO: maybe optimize this. 60//TODO: maybe optimize this.
61int client_in_list(Client_data * list, uint32_t length, char * client_id, IP_Port ip_port) 61int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port)
62{ 62{
63 uint32_t i, j; 63 uint32_t i, j;
64 uint32_t temp_time = unix_time(); 64 uint32_t temp_time = unix_time();
@@ -92,7 +92,7 @@ int client_in_list(Client_data * list, uint32_t length, char * client_id, IP_Por
92 92
93//check if client with client_id is already in node format list of length length. 93//check if client with client_id is already in node format list of length length.
94//return True(1) or False(0) 94//return True(1) or False(0)
95int client_in_nodelist(Node_format * list, uint32_t length, char * client_id) 95int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id)
96{ 96{
97 uint32_t i, j; 97 uint32_t i, j;
98 for(i = 0; i < length; i++) 98 for(i = 0; i < length; i++)
@@ -126,7 +126,7 @@ int client_in_nodelist(Node_format * list, uint32_t length, char * client_id)
126//Find MAX_SENT_NODES nodes closest to the client_id for the send nodes request: 126//Find MAX_SENT_NODES nodes closest to the client_id for the send nodes request:
127//put them in the nodes_list and return how many were found. 127//put them in the nodes_list and return how many were found.
128//TODO: Make this function much more efficient. 128//TODO: Make this function much more efficient.
129int get_close_nodes(char * client_id, Node_format * nodes_list) 129int get_close_nodes(uint8_t * client_id, Node_format * nodes_list)
130{ 130{
131 uint32_t i, j, k; 131 uint32_t i, j, k;
132 int num_nodes=0; 132 int num_nodes=0;
@@ -191,7 +191,7 @@ int get_close_nodes(char * client_id, Node_format * nodes_list)
191//replace first bad (or empty) node with this one 191//replace first bad (or empty) node with this one
192//return 0 if successfull 192//return 0 if successfull
193//return 1 if not (list contains no bad nodes) 193//return 1 if not (list contains no bad nodes)
194int replace_bad(Client_data * list, uint32_t length, char * client_id, IP_Port ip_port)//tested 194int replace_bad(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port)//tested
195{ 195{
196 uint32_t i; 196 uint32_t i;
197 uint32_t temp_time = unix_time(); 197 uint32_t temp_time = unix_time();
@@ -210,7 +210,7 @@ int replace_bad(Client_data * list, uint32_t length, char * client_id, IP_Port i
210} 210}
211 211
212//replace the first good node that is further to the comp_client_id than that of the client_id in the list 212//replace the first good node that is further to the comp_client_id than that of the client_id in the list
213int replace_good(Client_data * list, uint32_t length, char * client_id, IP_Port ip_port, char * comp_client_id) 213int replace_good(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port, uint8_t * comp_client_id)
214{ 214{
215 uint32_t i; 215 uint32_t i;
216 uint32_t temp_time = unix_time(); 216 uint32_t temp_time = unix_time();
@@ -230,7 +230,7 @@ int replace_good(Client_data * list, uint32_t length, char * client_id, IP_Port
230} 230}
231 231
232//Attempt to add client with ip_port and client_id to the friends client list and close_clientlist 232//Attempt to add client with ip_port and client_id to the friends client list and close_clientlist
233void addto_lists(IP_Port ip_port, char * client_id) 233void addto_lists(IP_Port ip_port, uint8_t * client_id)
234{ 234{
235 uint32_t i; 235 uint32_t i;
236 236
@@ -414,7 +414,7 @@ int pingreq(IP_Port ip_port)
414 return 1; 414 return 1;
415 } 415 }
416 416
417 char data[5 + CLIENT_ID_SIZE]; 417 uint8_t data[5 + CLIENT_ID_SIZE];
418 data[0] = 0; 418 data[0] = 0;
419 memcpy(data + 1, &ping_id, 4); 419 memcpy(data + 1, &ping_id, 4);
420 memcpy(data + 5, self_client_id, CLIENT_ID_SIZE); 420 memcpy(data + 5, self_client_id, CLIENT_ID_SIZE);
@@ -427,7 +427,7 @@ int pingreq(IP_Port ip_port)
427//send a ping response 427//send a ping response
428int pingres(IP_Port ip_port, uint32_t ping_id) 428int pingres(IP_Port ip_port, uint32_t ping_id)
429{ 429{
430 char data[5 + CLIENT_ID_SIZE]; 430 uint8_t data[5 + CLIENT_ID_SIZE];
431 data[0] = 1; 431 data[0] = 1;
432 432
433 memcpy(data + 1, &ping_id, 4); 433 memcpy(data + 1, &ping_id, 4);
@@ -438,7 +438,7 @@ int pingres(IP_Port ip_port, uint32_t ping_id)
438} 438}
439 439
440//send a getnodes request 440//send a getnodes request
441int getnodes(IP_Port ip_port, char * client_id) 441int getnodes(IP_Port ip_port, uint8_t * client_id)
442{ 442{
443 if(is_gettingnodes(ip_port, 0)) 443 if(is_gettingnodes(ip_port, 0))
444 { 444 {
@@ -452,7 +452,7 @@ int getnodes(IP_Port ip_port, char * client_id)
452 return 1; 452 return 1;
453 } 453 }
454 454
455 char data[5 + CLIENT_ID_SIZE*2]; 455 uint8_t data[5 + CLIENT_ID_SIZE*2];
456 data[0] = 2; 456 data[0] = 2;
457 457
458 memcpy(data + 1, &ping_id, 4); 458 memcpy(data + 1, &ping_id, 4);
@@ -465,9 +465,9 @@ int getnodes(IP_Port ip_port, char * client_id)
465 465
466 466
467//send a send nodes response 467//send a send nodes response
468int sendnodes(IP_Port ip_port, char * client_id, uint32_t ping_id) 468int sendnodes(IP_Port ip_port, uint8_t * client_id, uint32_t ping_id)
469{ 469{
470 char data[5 + CLIENT_ID_SIZE + (CLIENT_ID_SIZE + sizeof(IP_Port))*MAX_SENT_NODES]; 470 uint8_t data[5 + CLIENT_ID_SIZE + (CLIENT_ID_SIZE + sizeof(IP_Port))*MAX_SENT_NODES];
471 Node_format nodes_list[MAX_SENT_NODES]; 471 Node_format nodes_list[MAX_SENT_NODES];
472 472
473 int num_nodes = get_close_nodes(client_id, nodes_list); 473 int num_nodes = get_close_nodes(client_id, nodes_list);
@@ -493,7 +493,7 @@ int sendnodes(IP_Port ip_port, char * client_id, uint32_t ping_id)
493//Packet handling functions 493//Packet handling functions
494//One to handle each types of packets we recieve 494//One to handle each types of packets we recieve
495//return 0 if handled correctly, 1 if packet is bad. 495//return 0 if handled correctly, 1 if packet is bad.
496int handle_pingreq(char * packet, uint32_t length, IP_Port source)//tested 496int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source)//tested
497{ 497{
498 if(length != 5 + CLIENT_ID_SIZE) 498 if(length != 5 + CLIENT_ID_SIZE)
499 { 499 {
@@ -518,7 +518,7 @@ int handle_pingreq(char * packet, uint32_t length, IP_Port source)//tested
518 518
519} 519}
520 520
521int handle_pingres(char * packet, uint32_t length, IP_Port source) 521int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source)
522{ 522{
523 if(length != (5 + CLIENT_ID_SIZE)) 523 if(length != (5 + CLIENT_ID_SIZE))
524 { 524 {
@@ -536,7 +536,7 @@ int handle_pingres(char * packet, uint32_t length, IP_Port source)
536 536
537} 537}
538 538
539int handle_getnodes(char * packet, uint32_t length, IP_Port source) 539int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source)
540{ 540{
541 if(length != (5 + CLIENT_ID_SIZE*2)) 541 if(length != (5 + CLIENT_ID_SIZE*2))
542 { 542 {
@@ -559,7 +559,7 @@ int handle_getnodes(char * packet, uint32_t length, IP_Port source)
559 559
560} 560}
561 561
562int handle_sendnodes(char * packet, uint32_t length, IP_Port source)//tested 562int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source)//tested
563{ 563{
564 if(length > (5 + CLIENT_ID_SIZE + MAX_SENT_NODES * (CLIENT_ID_SIZE + sizeof(IP_Port))) || 564 if(length > (5 + CLIENT_ID_SIZE + MAX_SENT_NODES * (CLIENT_ID_SIZE + sizeof(IP_Port))) ||
565 (length - 5 - CLIENT_ID_SIZE) % (CLIENT_ID_SIZE + sizeof(IP_Port)) != 0) 565 (length - 5 - CLIENT_ID_SIZE) % (CLIENT_ID_SIZE + sizeof(IP_Port)) != 0)
@@ -593,7 +593,7 @@ int handle_sendnodes(char * packet, uint32_t length, IP_Port source)//tested
593 593
594 594
595 595
596int addfriend(char * client_id) 596int addfriend(uint8_t * client_id)
597{ 597{
598 //TODO:Maybe make the array of friends dynamic instead of a static array with MAX_FRIENDS 598 //TODO:Maybe make the array of friends dynamic instead of a static array with MAX_FRIENDS
599 if(MAX_FRIENDS > num_friends) 599 if(MAX_FRIENDS > num_friends)
@@ -610,7 +610,7 @@ int addfriend(char * client_id)
610 610
611 611
612 612
613int delfriend(char * client_id) 613int delfriend(uint8_t * client_id)
614{ 614{
615 uint32_t i; 615 uint32_t i;
616 for(i = 0; i < num_friends; i++) 616 for(i = 0; i < num_friends; i++)
@@ -630,7 +630,7 @@ int delfriend(char * client_id)
630 630
631 631
632//TODO: Optimize this. 632//TODO: Optimize this.
633IP_Port getfriendip(char * client_id) 633IP_Port getfriendip(uint8_t * client_id)
634{ 634{
635 uint32_t i, j; 635 uint32_t i, j;
636 IP_Port empty = {{{0}}, 0}; 636 IP_Port empty = {{{0}}, 0};
@@ -660,7 +660,7 @@ IP_Port getfriendip(char * client_id)
660 660
661 661
662 662
663int DHT_handlepacket(char * packet, uint32_t length, IP_Port source) 663int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source)
664{ 664{
665 switch (packet[0]) { 665 switch (packet[0]) {
666 case 0: 666 case 0: