diff options
-rw-r--r-- | core/DHT.c | 103 | ||||
-rw-r--r-- | core/DHT.h | 12 |
2 files changed, 82 insertions, 33 deletions
@@ -110,35 +110,22 @@ int id_closest(uint8_t * client_id, uint8_t * client_id1, uint8_t * client_id2)/ | |||
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 ip_port is already in the list but associated to a different ip, change 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. |
116 | int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port) | 116 | int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port) |
117 | { | 117 | { |
118 | uint32_t i, j; | 118 | uint32_t i; |
119 | uint32_t temp_time = unix_time(); | 119 | uint32_t temp_time = unix_time(); |
120 | 120 | ||
121 | for(i = 0; i < length; i++) | 121 | for(i = 0; i < length; i++) |
122 | { | 122 | { |
123 | //If the id for an ip/port changes, replace it. | 123 | if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) |
124 | if(list[i].ip_port.ip.i == ip_port.ip.i && | ||
125 | list[i].ip_port.port == ip_port.port) | ||
126 | { | ||
127 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); | ||
128 | } | ||
129 | |||
130 | for(j = 0; j < CLIENT_ID_SIZE; j++) | ||
131 | { | ||
132 | |||
133 | if(list[i].client_id[j] != client_id[j]) | ||
134 | { | ||
135 | break; | ||
136 | } | ||
137 | } | ||
138 | if(j == CLIENT_ID_SIZE) | ||
139 | { | 124 | { |
140 | //Refresh the client timestamp. | 125 | //Refresh the client timestamp. |
141 | list[i].timestamp = temp_time; | 126 | list[i].timestamp = temp_time; |
127 | list[i].ip_port.ip.i = ip_port.ip.i; | ||
128 | list[i].ip_port.port = ip_port.port; | ||
142 | return 1; | 129 | return 1; |
143 | } | 130 | } |
144 | } | 131 | } |
@@ -150,18 +137,10 @@ int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_ | |||
150 | //return True(1) or False(0) | 137 | //return True(1) or False(0) |
151 | int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id) | 138 | int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id) |
152 | { | 139 | { |
153 | uint32_t i, j; | 140 | uint32_t i; |
154 | for(i = 0; i < length; i++) | 141 | for(i = 0; i < length; i++) |
155 | { | 142 | { |
156 | for(j = 0; j < CLIENT_ID_SIZE; j++) | 143 | if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) |
157 | { | ||
158 | |||
159 | if(list[i].client_id[j] != client_id[j]) | ||
160 | { | ||
161 | break; | ||
162 | } | ||
163 | } | ||
164 | if(j == CLIENT_ID_SIZE) | ||
165 | { | 144 | { |
166 | 145 | ||
167 | return 1; | 146 | return 1; |
@@ -290,7 +269,7 @@ void addto_lists(IP_Port ip_port, uint8_t * client_id) | |||
290 | { | 269 | { |
291 | uint32_t i; | 270 | uint32_t i; |
292 | 271 | ||
293 | //NOTE: current behavior if there are two clients with the same id is to only keep one (the first one) | 272 | //NOTE: current behavior if there are two clients with the same id is to replace the first ip by the second. |
294 | 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)) |
295 | { | 274 | { |
296 | 275 | ||
@@ -818,14 +797,15 @@ IP_Port DHT_getfriendip(uint8_t * client_id) | |||
818 | { | 797 | { |
819 | uint32_t i, j; | 798 | uint32_t i, j; |
820 | IP_Port empty = {{{0}}, 0}; | 799 | IP_Port empty = {{{0}}, 0}; |
821 | 800 | uint32_t temp_time = unix_time(); | |
822 | for(i = 0; i < num_friends; i++) | 801 | for(i = 0; i < num_friends; i++) |
823 | { | 802 | { |
824 | if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0)//Equal | 803 | if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0)//Equal |
825 | { | 804 | { |
826 | for(j = 0; j < MAX_FRIEND_CLIENTS; j++) | 805 | for(j = 0; j < MAX_FRIEND_CLIENTS; j++) |
827 | { | 806 | { |
828 | if(memcmp(friends_list[i].client_list[j].client_id, client_id, CLIENT_ID_SIZE) == 0) | 807 | if(memcmp(friends_list[i].client_list[j].client_id, client_id, CLIENT_ID_SIZE) == 0 && |
808 | friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time) | ||
829 | { | 809 | { |
830 | return friends_list[i].client_list[j].ip_port; | 810 | return friends_list[i].client_list[j].ip_port; |
831 | } | 811 | } |
@@ -976,3 +956,64 @@ void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key) | |||
976 | 956 | ||
977 | } | 957 | } |
978 | 958 | ||
959 | |||
960 | //get the size of the DHT (for saving) | ||
961 | uint32_t DHT_size() | ||
962 | { | ||
963 | return sizeof(close_clientlist) + sizeof(Friend) * num_friends; | ||
964 | } | ||
965 | |||
966 | //save the DHT in data where data is an array of size DHT_size() | ||
967 | void DHT_save(uint8_t * data) | ||
968 | { | ||
969 | memcpy(data, close_clientlist, sizeof(close_clientlist)); | ||
970 | memcpy(data + sizeof(close_clientlist), friends_list, sizeof(Friend) * num_friends); | ||
971 | } | ||
972 | |||
973 | //load the DHT from data of size size; | ||
974 | //return -1 if failure | ||
975 | //return 0 if success | ||
976 | int DHT_load(uint8_t * data, uint32_t size) | ||
977 | { | ||
978 | if(size < sizeof(close_clientlist)) | ||
979 | { | ||
980 | return -1; | ||
981 | } | ||
982 | if((size - sizeof(close_clientlist)) % sizeof(Friend) != 0) | ||
983 | { | ||
984 | return -1; | ||
985 | } | ||
986 | uint32_t i, j; | ||
987 | //uint32_t temp_time = unix_time(); | ||
988 | uint16_t temp; | ||
989 | |||
990 | temp = (size - sizeof(close_clientlist))/sizeof(Friend); | ||
991 | |||
992 | if(temp != 0) | ||
993 | { | ||
994 | Friend * tempfriends_list = (Friend *)(data + sizeof(close_clientlist)); | ||
995 | |||
996 | for(i = 0; i < temp; i++) | ||
997 | { | ||
998 | DHT_addfriend(tempfriends_list[i].client_id); | ||
999 | for(j = 0; j < MAX_FRIEND_CLIENTS; j++) | ||
1000 | { | ||
1001 | if(tempfriends_list[i].client_list[j].timestamp != 0) | ||
1002 | { | ||
1003 | getnodes(tempfriends_list[i].client_list[j].ip_port, | ||
1004 | tempfriends_list[i].client_list[j].client_id, tempfriends_list[i].client_id); | ||
1005 | } | ||
1006 | } | ||
1007 | } | ||
1008 | } | ||
1009 | Client_data * tempclose_clientlist = (Client_data *)data; | ||
1010 | |||
1011 | for(i = 0; i < LCLIENT_LIST; i++) | ||
1012 | { | ||
1013 | if(tempclose_clientlist[i].timestamp != 0) | ||
1014 | { | ||
1015 | DHT_bootstrap(tempclose_clientlist[i].ip_port, tempclose_clientlist[i].client_id); | ||
1016 | } | ||
1017 | } | ||
1018 | return 0; | ||
1019 | } | ||
@@ -72,11 +72,19 @@ int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source); | |||
72 | void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key); | 72 | void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key); |
73 | 73 | ||
74 | 74 | ||
75 | //TODO: | 75 | //SAVE/LOAD functions |
76 | //Add functions to save and load the state(client list, friends list) | ||
77 | 76 | ||
77 | //get the size of the DHT (for saving) | ||
78 | uint32_t DHT_size(); | ||
78 | 79 | ||
79 | 80 | ||
81 | //save the DHT in data where data is an array of size DHT_size() | ||
82 | void DHT_save(uint8_t * data); | ||
83 | |||
84 | //load the DHT from data of size size; | ||
85 | //return -1 if failure | ||
86 | //return 0 if success | ||
87 | int DHT_load(uint8_t * data, uint32_t size); | ||
80 | 88 | ||
81 | 89 | ||
82 | #endif | 90 | #endif |