diff options
-rw-r--r-- | core/DHT.c | 74 |
1 files changed, 37 insertions, 37 deletions
@@ -91,7 +91,7 @@ static Pinged send_nodes[LSEND_NODES_ARRAY]; | |||
91 | int id_closest(uint8_t * client_id, uint8_t * client_id1, uint8_t * client_id2)//tested | 91 | int 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) |
95 | { | 95 | { |
96 | if(abs(client_id[i] ^ client_id1[i]) < abs(client_id[i] ^ client_id2[i])) | 96 | if(abs(client_id[i] ^ client_id1[i]) < abs(client_id[i] ^ client_id2[i])) |
97 | { | 97 | { |
@@ -118,7 +118,7 @@ int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_ | |||
118 | uint32_t i; | 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(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 | { |
@@ -138,7 +138,7 @@ int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_ | |||
138 | 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) |
139 | { | 139 | { |
140 | uint32_t i; | 140 | uint32_t i; |
141 | for(i = 0; i < length; i++) | 141 | for(i = 0; i < length; ++i) |
142 | { | 142 | { |
143 | if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) | 143 | if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) |
144 | { | 144 | { |
@@ -166,7 +166,7 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list) | |||
166 | uint32_t i, j, k; | 166 | uint32_t i, j, k; |
167 | int num_nodes=0; | 167 | int num_nodes=0; |
168 | uint32_t temp_time = unix_time(); | 168 | uint32_t temp_time = unix_time(); |
169 | for(i = 0; i < LCLIENT_LIST; i++) | 169 | for(i = 0; i < LCLIENT_LIST; ++i) |
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)) |
@@ -178,7 +178,7 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list) | |||
178 | nodes_list[num_nodes].ip_port = close_clientlist[i].ip_port; | 178 | nodes_list[num_nodes].ip_port = close_clientlist[i].ip_port; |
179 | num_nodes++; | 179 | num_nodes++; |
180 | } | 180 | } |
181 | else for(j = 0; j < MAX_SENT_NODES; j++) | 181 | else for(j = 0; j < MAX_SENT_NODES; ++j) |
182 | { | 182 | { |
183 | if(id_closest(client_id, nodes_list[j].client_id, close_clientlist[i].client_id) == 2) | 183 | if(id_closest(client_id, nodes_list[j].client_id, close_clientlist[i].client_id) == 2) |
184 | { | 184 | { |
@@ -190,9 +190,9 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list) | |||
190 | } | 190 | } |
191 | 191 | ||
192 | } | 192 | } |
193 | for(i = 0; i < num_friends; i++) | 193 | for(i = 0; i < num_friends; ++i) |
194 | { | 194 | { |
195 | for(j = 0; j < MAX_FRIEND_CLIENTS; j++) | 195 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) |
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)) |
@@ -204,7 +204,7 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list) | |||
204 | nodes_list[num_nodes].ip_port = friends_list[i].client_list[j].ip_port; | 204 | nodes_list[num_nodes].ip_port = friends_list[i].client_list[j].ip_port; |
205 | num_nodes++; | 205 | num_nodes++; |
206 | } | 206 | } |
207 | else for(k = 0; k < MAX_SENT_NODES; k++) | 207 | else for(k = 0; k < MAX_SENT_NODES; ++k) |
208 | { | 208 | { |
209 | if(id_closest(client_id, nodes_list[k].client_id, friends_list[i].client_list[j].client_id) == 2) | 209 | if(id_closest(client_id, nodes_list[k].client_id, friends_list[i].client_list[j].client_id) == 2) |
210 | { | 210 | { |
@@ -230,7 +230,7 @@ int replace_bad(Client_data * list, uint32_t length, uint8_t * client_id, IP_Por | |||
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 | { |
@@ -250,7 +250,7 @@ int replace_good(Client_data * list, uint32_t length, uint8_t * client_id, IP_Po | |||
250 | uint32_t i; | 250 | uint32_t i; |
251 | uint32_t temp_time = unix_time(); | 251 | uint32_t temp_time = unix_time(); |
252 | 252 | ||
253 | for(i = 0; i < length; i++) | 253 | for(i = 0; i < length; ++i) |
254 | { | 254 | { |
255 | if(id_closest(comp_client_id, list[i].client_id, client_id) == 2) | 255 | if(id_closest(comp_client_id, list[i].client_id, client_id) == 2) |
256 | { | 256 | { |
@@ -280,7 +280,7 @@ void addto_lists(IP_Port ip_port, uint8_t * client_id) | |||
280 | } | 280 | } |
281 | 281 | ||
282 | } | 282 | } |
283 | for(i = 0; i < num_friends; i++) | 283 | for(i = 0; i < num_friends; ++i) |
284 | { | 284 | { |
285 | if(!client_in_list(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port)) | 285 | if(!client_in_list(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port)) |
286 | { | 286 | { |
@@ -308,7 +308,7 @@ int is_pinging(IP_Port ip_port, uint64_t ping_id) | |||
308 | uint8_t pinging; | 308 | uint8_t pinging; |
309 | uint32_t temp_time = unix_time(); | 309 | uint32_t temp_time = unix_time(); |
310 | 310 | ||
311 | for(i = 0; i < LPING_ARRAY; i++ ) | 311 | for(i = 0; i < LPING_ARRAY; ++i ) |
312 | { | 312 | { |
313 | if((pings[i].timestamp + PING_TIMEOUT) > temp_time) | 313 | if((pings[i].timestamp + PING_TIMEOUT) > temp_time) |
314 | { | 314 | { |
@@ -318,14 +318,14 @@ int is_pinging(IP_Port ip_port, uint64_t ping_id) | |||
318 | if(pings[i].ip_port.ip.i == ip_port.ip.i && | 318 | if(pings[i].ip_port.ip.i == ip_port.ip.i && |
319 | pings[i].ip_port.port == ip_port.port) | 319 | pings[i].ip_port.port == ip_port.port) |
320 | { | 320 | { |
321 | pinging++; | 321 | ++pinging; |
322 | } | 322 | } |
323 | } | 323 | } |
324 | if(ping_id != 0) | 324 | if(ping_id != 0) |
325 | { | 325 | { |
326 | if(pings[i].ping_id == ping_id) | 326 | if(pings[i].ping_id == ping_id) |
327 | { | 327 | { |
328 | pinging++; | 328 | ++pinging; |
329 | } | 329 | } |
330 | } | 330 | } |
331 | if(pinging == (ping_id != 0) + (ip_port.ip.i != 0)) | 331 | if(pinging == (ping_id != 0) + (ip_port.ip.i != 0)) |
@@ -348,7 +348,7 @@ int is_gettingnodes(IP_Port ip_port, uint64_t ping_id) | |||
348 | uint8_t pinging; | 348 | uint8_t pinging; |
349 | uint32_t temp_time = unix_time(); | 349 | uint32_t temp_time = unix_time(); |
350 | 350 | ||
351 | for(i = 0; i < LSEND_NODES_ARRAY; i++ ) | 351 | for(i = 0; i < LSEND_NODES_ARRAY; ++i ) |
352 | { | 352 | { |
353 | if((send_nodes[i].timestamp + PING_TIMEOUT) > temp_time) | 353 | if((send_nodes[i].timestamp + PING_TIMEOUT) > temp_time) |
354 | { | 354 | { |
@@ -358,14 +358,14 @@ int is_gettingnodes(IP_Port ip_port, uint64_t ping_id) | |||
358 | if(send_nodes[i].ip_port.ip.i == ip_port.ip.i && | 358 | if(send_nodes[i].ip_port.ip.i == ip_port.ip.i && |
359 | send_nodes[i].ip_port.port == ip_port.port) | 359 | send_nodes[i].ip_port.port == ip_port.port) |
360 | { | 360 | { |
361 | pinging++; | 361 | ++pinging; |
362 | } | 362 | } |
363 | } | 363 | } |
364 | if(ping_id != 0) | 364 | if(ping_id != 0) |
365 | { | 365 | { |
366 | if(send_nodes[i].ping_id == ping_id) | 366 | if(send_nodes[i].ping_id == ping_id) |
367 | { | 367 | { |
368 | pinging++; | 368 | ++pinging; |
369 | } | 369 | } |
370 | } | 370 | } |
371 | if(pinging == (ping_id != 0) + (ip_port.ip.i != 0)) | 371 | if(pinging == (ping_id != 0) + (ip_port.ip.i != 0)) |
@@ -391,9 +391,9 @@ uint64_t add_pinging(IP_Port ip_port) | |||
391 | uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int(); | 391 | uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int(); |
392 | uint32_t temp_time = unix_time(); | 392 | uint32_t temp_time = unix_time(); |
393 | 393 | ||
394 | for(i = 0; i < PING_TIMEOUT; i++ ) | 394 | for(i = 0; i < PING_TIMEOUT; ++i ) |
395 | { | 395 | { |
396 | for(j = 0; j < LPING_ARRAY; j++ ) | 396 | for(j = 0; j < LPING_ARRAY; ++j ) |
397 | { | 397 | { |
398 | if((pings[j].timestamp + PING_TIMEOUT - i) < temp_time) | 398 | if((pings[j].timestamp + PING_TIMEOUT - i) < temp_time) |
399 | { | 399 | { |
@@ -415,9 +415,9 @@ uint64_t add_gettingnodes(IP_Port ip_port) | |||
415 | uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int(); | 415 | uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int(); |
416 | uint32_t temp_time = unix_time(); | 416 | uint32_t temp_time = unix_time(); |
417 | 417 | ||
418 | for(i = 0; i < PING_TIMEOUT; i++ ) | 418 | for(i = 0; i < PING_TIMEOUT; ++i ) |
419 | { | 419 | { |
420 | for(j = 0; j < LSEND_NODES_ARRAY; j++ ) | 420 | for(j = 0; j < LSEND_NODES_ARRAY; ++j ) |
421 | { | 421 | { |
422 | if((send_nodes[j].timestamp + PING_TIMEOUT - i) < temp_time) | 422 | if((send_nodes[j].timestamp + PING_TIMEOUT - i) < temp_time) |
423 | { | 423 | { |
@@ -727,7 +727,7 @@ int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source) | |||
727 | memcpy(nodes_list, plain + sizeof(ping_id), num_nodes * sizeof(Node_format)); | 727 | memcpy(nodes_list, plain + sizeof(ping_id), num_nodes * sizeof(Node_format)); |
728 | 728 | ||
729 | uint32_t i; | 729 | uint32_t i; |
730 | for(i = 0; i < num_nodes; i++) | 730 | for(i = 0; i < num_nodes; ++i) |
731 | { | 731 | { |
732 | pingreq(nodes_list[i].ip_port, nodes_list[i].client_id); | 732 | pingreq(nodes_list[i].ip_port, nodes_list[i].client_id); |
733 | } | 733 | } |
@@ -760,7 +760,7 @@ int DHT_addfriend(uint8_t * client_id) | |||
760 | friends_list = temp; | 760 | friends_list = temp; |
761 | memset(&friends_list[num_friends], 0, sizeof(Friend)); | 761 | memset(&friends_list[num_friends], 0, sizeof(Friend)); |
762 | memcpy(friends_list[num_friends].client_id, client_id, CLIENT_ID_SIZE); | 762 | memcpy(friends_list[num_friends].client_id, client_id, CLIENT_ID_SIZE); |
763 | num_friends++; | 763 | ++num_friends; |
764 | return 0; | 764 | return 0; |
765 | } | 765 | } |
766 | 766 | ||
@@ -772,11 +772,11 @@ int DHT_delfriend(uint8_t * client_id) | |||
772 | { | 772 | { |
773 | uint32_t i; | 773 | uint32_t i; |
774 | Friend * temp; | 774 | Friend * temp; |
775 | for(i = 0; i < num_friends; i++) | 775 | for(i = 0; i < num_friends; ++i) |
776 | { | 776 | { |
777 | if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0)//Equal | 777 | if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0)//Equal |
778 | { | 778 | { |
779 | num_friends--; | 779 | --num_friends; |
780 | if(num_friends != i) | 780 | if(num_friends != i) |
781 | { | 781 | { |
782 | memcpy(friends_list[i].client_id, friends_list[num_friends].client_id, CLIENT_ID_SIZE); | 782 | memcpy(friends_list[i].client_id, friends_list[num_friends].client_id, CLIENT_ID_SIZE); |
@@ -801,11 +801,11 @@ IP_Port DHT_getfriendip(uint8_t * client_id) | |||
801 | uint32_t i, j; | 801 | uint32_t i, j; |
802 | IP_Port empty = {{{0}}, 0}; | 802 | IP_Port empty = {{{0}}, 0}; |
803 | uint32_t temp_time = unix_time(); | 803 | uint32_t temp_time = unix_time(); |
804 | for(i = 0; i < num_friends; i++) | 804 | for(i = 0; i < num_friends; ++i) |
805 | { | 805 | { |
806 | if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0)//Equal | 806 | if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0)//Equal |
807 | { | 807 | { |
808 | for(j = 0; j < MAX_FRIEND_CLIENTS; j++) | 808 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) |
809 | { | 809 | { |
810 | if(memcmp(friends_list[i].client_list[j].client_id, client_id, CLIENT_ID_SIZE) == 0 && | 810 | if(memcmp(friends_list[i].client_list[j].client_id, client_id, CLIENT_ID_SIZE) == 0 && |
811 | friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time) | 811 | friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time) |
@@ -872,9 +872,9 @@ void doDHTFriends() | |||
872 | uint32_t rand_node; | 872 | uint32_t rand_node; |
873 | uint32_t index[MAX_FRIEND_CLIENTS]; | 873 | uint32_t index[MAX_FRIEND_CLIENTS]; |
874 | 874 | ||
875 | for(i = 0; i < num_friends; i++) | 875 | for(i = 0; i < num_friends; ++i) |
876 | { | 876 | { |
877 | for(j = 0; j < MAX_FRIEND_CLIENTS; j++) | 877 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) |
878 | { | 878 | { |
879 | if(friends_list[i].client_list[j].timestamp + Kill_NODE_TIMEOUT > temp_time)//if node is not dead. | 879 | if(friends_list[i].client_list[j].timestamp + Kill_NODE_TIMEOUT > temp_time)//if node is not dead. |
880 | { | 880 | { |
@@ -886,7 +886,7 @@ void doDHTFriends() | |||
886 | if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time)//if node is good. | 886 | if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time)//if node is good. |
887 | { | 887 | { |
888 | index[num_nodes] = j; | 888 | index[num_nodes] = j; |
889 | num_nodes++; | 889 | ++num_nodes; |
890 | } | 890 | } |
891 | } | 891 | } |
892 | } | 892 | } |
@@ -913,7 +913,7 @@ void doClose()//tested | |||
913 | uint32_t rand_node; | 913 | uint32_t rand_node; |
914 | uint32_t index[LCLIENT_LIST]; | 914 | uint32_t index[LCLIENT_LIST]; |
915 | 915 | ||
916 | for(i = 0; i < LCLIENT_LIST; i++) | 916 | for(i = 0; i < LCLIENT_LIST; ++i) |
917 | { | 917 | { |
918 | if(close_clientlist[i].timestamp + Kill_NODE_TIMEOUT > temp_time)//if node is not dead. | 918 | if(close_clientlist[i].timestamp + Kill_NODE_TIMEOUT > temp_time)//if node is not dead. |
919 | { | 919 | { |
@@ -925,7 +925,7 @@ void doClose()//tested | |||
925 | if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time)//if node is good. | 925 | if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time)//if node is good. |
926 | { | 926 | { |
927 | index[num_nodes] = i; | 927 | index[num_nodes] = i; |
928 | num_nodes++; | 928 | ++num_nodes; |
929 | } | 929 | } |
930 | } | 930 | } |
931 | } | 931 | } |
@@ -996,10 +996,10 @@ int DHT_load(uint8_t * data, uint32_t size) | |||
996 | { | 996 | { |
997 | Friend * tempfriends_list = (Friend *)(data + sizeof(close_clientlist)); | 997 | Friend * tempfriends_list = (Friend *)(data + sizeof(close_clientlist)); |
998 | 998 | ||
999 | for(i = 0; i < temp; i++) | 999 | for(i = 0; i < temp; ++i) |
1000 | { | 1000 | { |
1001 | DHT_addfriend(tempfriends_list[i].client_id); | 1001 | DHT_addfriend(tempfriends_list[i].client_id); |
1002 | for(j = 0; j < MAX_FRIEND_CLIENTS; j++) | 1002 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) |
1003 | { | 1003 | { |
1004 | if(tempfriends_list[i].client_list[j].timestamp != 0) | 1004 | if(tempfriends_list[i].client_list[j].timestamp != 0) |
1005 | { | 1005 | { |
@@ -1011,7 +1011,7 @@ int DHT_load(uint8_t * data, uint32_t size) | |||
1011 | } | 1011 | } |
1012 | Client_data * tempclose_clientlist = (Client_data *)data; | 1012 | Client_data * tempclose_clientlist = (Client_data *)data; |
1013 | 1013 | ||
1014 | for(i = 0; i < LCLIENT_LIST; i++) | 1014 | for(i = 0; i < LCLIENT_LIST; ++i) |
1015 | { | 1015 | { |
1016 | if(tempclose_clientlist[i].timestamp != 0) | 1016 | if(tempclose_clientlist[i].timestamp != 0) |
1017 | { | 1017 | { |
@@ -1027,7 +1027,7 @@ int DHT_isconnected() | |||
1027 | { | 1027 | { |
1028 | uint32_t i; | 1028 | uint32_t i; |
1029 | uint32_t temp_time = unix_time(); | 1029 | uint32_t temp_time = unix_time(); |
1030 | for(i = 0; i < LCLIENT_LIST; i++) | 1030 | for(i = 0; i < LCLIENT_LIST; ++i) |
1031 | { | 1031 | { |
1032 | if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time) | 1032 | if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time) |
1033 | { | 1033 | { |
@@ -1035,4 +1035,4 @@ int DHT_isconnected() | |||
1035 | } | 1035 | } |
1036 | } | 1036 | } |
1037 | return 0; | 1037 | return 0; |
1038 | } \ No newline at end of file | 1038 | } |