diff options
Diffstat (limited to 'core/DHT.c')
-rw-r--r-- | core/DHT.c | 44 |
1 files changed, 22 insertions, 22 deletions
@@ -27,13 +27,13 @@ | |||
27 | 27 | ||
28 | #include "DHT.h" | 28 | #include "DHT.h" |
29 | 29 | ||
30 | char self_client_id[CLIENT_ID_SIZE]; | 30 | uint8_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. |
36 | int id_closest(char * client_id, char * client_id1, char * client_id2)//tested | 36 | int 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. |
61 | int client_in_list(Client_data * list, uint32_t length, char * client_id, IP_Port ip_port) | 61 | int 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) |
95 | int client_in_nodelist(Node_format * list, uint32_t length, char * client_id) | 95 | int 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. |
129 | int get_close_nodes(char * client_id, Node_format * nodes_list) | 129 | int 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) |
194 | int replace_bad(Client_data * list, uint32_t length, char * client_id, IP_Port ip_port)//tested | 194 | int 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 |
213 | int replace_good(Client_data * list, uint32_t length, char * client_id, IP_Port ip_port, char * comp_client_id) | 213 | int 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 |
233 | void addto_lists(IP_Port ip_port, char * client_id) | 233 | void 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 |
428 | int pingres(IP_Port ip_port, uint32_t ping_id) | 428 | int 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 |
441 | int getnodes(IP_Port ip_port, char * client_id) | 441 | int 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 |
468 | int sendnodes(IP_Port ip_port, char * client_id, uint32_t ping_id) | 468 | int 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. |
496 | int handle_pingreq(char * packet, uint32_t length, IP_Port source)//tested | 496 | int 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 | ||
521 | int handle_pingres(char * packet, uint32_t length, IP_Port source) | 521 | int 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 | ||
539 | int handle_getnodes(char * packet, uint32_t length, IP_Port source) | 539 | int 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 | ||
562 | int handle_sendnodes(char * packet, uint32_t length, IP_Port source)//tested | 562 | int 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 | ||
596 | int addfriend(char * client_id) | 596 | int 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 | ||
613 | int delfriend(char * client_id) | 613 | int 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. |
633 | IP_Port getfriendip(char * client_id) | 633 | IP_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 | ||
663 | int DHT_handlepacket(char * packet, uint32_t length, IP_Port source) | 663 | int 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: |