diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/DHT.c | 45 |
1 files changed, 29 insertions, 16 deletions
@@ -41,6 +41,7 @@ typedef 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 | 45 | ||
45 | }Friend; | 46 | }Friend; |
46 | 47 | ||
@@ -69,11 +70,8 @@ uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; | |||
69 | static Client_data close_clientlist[LCLIENT_LIST]; | 70 | static Client_data close_clientlist[LCLIENT_LIST]; |
70 | 71 | ||
71 | 72 | ||
72 | //Hard maximum number of friends | ||
73 | #define MAX_FRIENDS 256 | ||
74 | 73 | ||
75 | //Let's start with a static array for testing. | 74 | static Friend * friends_list; |
76 | static Friend friends_list[MAX_FRIENDS]; | ||
77 | static uint16_t num_friends; | 75 | static uint16_t num_friends; |
78 | 76 | ||
79 | //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 |
@@ -370,7 +368,7 @@ int is_gettingnodes(IP_Port ip_port, uint64_t ping_id) | |||
370 | uint8_t pinging; | 368 | uint8_t pinging; |
371 | uint32_t temp_time = unix_time(); | 369 | uint32_t temp_time = unix_time(); |
372 | 370 | ||
373 | for(i = 0; i < LPING_ARRAY; i++ ) | 371 | for(i = 0; i < LSEND_NODES_ARRAY; i++ ) |
374 | { | 372 | { |
375 | if((send_nodes[i].timestamp + PING_TIMEOUT) > temp_time) | 373 | if((send_nodes[i].timestamp + PING_TIMEOUT) > temp_time) |
376 | { | 374 | { |
@@ -765,14 +763,26 @@ int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source) | |||
765 | 763 | ||
766 | int DHT_addfriend(uint8_t * client_id) | 764 | int DHT_addfriend(uint8_t * client_id) |
767 | { | 765 | { |
768 | //TODO:Maybe make the array of friends dynamic instead of a static array with MAX_FRIENDS | 766 | Friend * temp; |
769 | if(MAX_FRIENDS > num_friends) | 767 | if(num_friends == 0) |
770 | { | 768 | { |
771 | memcpy(friends_list[num_friends].client_id, client_id, CLIENT_ID_SIZE); | 769 | temp = malloc(sizeof(Friend)); |
772 | num_friends++; | 770 | } |
773 | return 0; | 771 | if(num_friends > 0) |
772 | { | ||
773 | temp = realloc(friends_list, sizeof(Friend) * (num_friends + 1)); | ||
774 | } | ||
775 | if(temp == NULL) | ||
776 | { | ||
777 | return 1; | ||
774 | } | 778 | } |
775 | return 1; | 779 | |
780 | friends_list = temp; | ||
781 | memset(&friends_list[num_friends], 0, sizeof(Friend)); | ||
782 | memcpy(friends_list[num_friends].client_id, client_id, CLIENT_ID_SIZE); | ||
783 | num_friends++; | ||
784 | return 0; | ||
785 | |||
776 | 786 | ||
777 | } | 787 | } |
778 | 788 | ||
@@ -783,17 +793,22 @@ int DHT_addfriend(uint8_t * client_id) | |||
783 | int DHT_delfriend(uint8_t * client_id) | 793 | int DHT_delfriend(uint8_t * client_id) |
784 | { | 794 | { |
785 | uint32_t i; | 795 | uint32_t i; |
796 | Friend * temp; | ||
786 | for(i = 0; i < num_friends; i++) | 797 | for(i = 0; i < num_friends; i++) |
787 | { | 798 | { |
788 | if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0)//Equal | 799 | if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0)//Equal |
789 | { | 800 | { |
790 | memcpy(friends_list[num_friends].client_id, friends_list[i].client_id, CLIENT_ID_SIZE); | 801 | memcpy(friends_list[num_friends].client_id, friends_list[i].client_id, CLIENT_ID_SIZE); |
791 | num_friends--; | 802 | num_friends--; |
803 | temp = realloc(friends_list, sizeof(friends_list) * (num_friends)); | ||
804 | if(temp != NULL) | ||
805 | { | ||
806 | friends_list = temp; | ||
807 | } | ||
792 | return 0; | 808 | return 0; |
793 | } | 809 | } |
794 | } | 810 | } |
795 | return 1; | 811 | return 1; |
796 | |||
797 | } | 812 | } |
798 | 813 | ||
799 | 814 | ||
@@ -866,8 +881,6 @@ int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) | |||
866 | //Ping each client in the "friends" list every 60 seconds. | 881 | //Ping each client in the "friends" list every 60 seconds. |
867 | //Send a get nodes request every 20 seconds to a random good node for each "friend" in our "friends" list. | 882 | //Send a get nodes request every 20 seconds to a random good node for each "friend" in our "friends" list. |
868 | 883 | ||
869 | static uint32_t friend_lastgetnode[MAX_FRIENDS]; | ||
870 | |||
871 | 884 | ||
872 | void doDHTFriends() | 885 | void doDHTFriends() |
873 | { | 886 | { |
@@ -895,13 +908,13 @@ void doDHTFriends() | |||
895 | } | 908 | } |
896 | } | 909 | } |
897 | } | 910 | } |
898 | if(friend_lastgetnode[i] + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) | 911 | if(friends_list[i].lastgetnode + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) |
899 | { | 912 | { |
900 | rand_node = rand() % num_nodes; | 913 | rand_node = rand() % num_nodes; |
901 | getnodes(friends_list[i].client_list[index[rand_node]].ip_port, | 914 | getnodes(friends_list[i].client_list[index[rand_node]].ip_port, |
902 | friends_list[i].client_list[index[rand_node]].client_id, | 915 | friends_list[i].client_list[index[rand_node]].client_id, |
903 | friends_list[i].client_id); | 916 | friends_list[i].client_id); |
904 | friend_lastgetnode[i] = temp_time; | 917 | friends_list[i].lastgetnode = temp_time; |
905 | } | 918 | } |
906 | } | 919 | } |
907 | } | 920 | } |