diff options
Diffstat (limited to 'core/DHT.c')
-rw-r--r-- | core/DHT.c | 64 |
1 files changed, 49 insertions, 15 deletions
@@ -274,14 +274,19 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list) | |||
274 | } | 274 | } |
275 | 275 | ||
276 | /* replace first bad (or empty) node with this one | 276 | /* replace first bad (or empty) node with this one |
277 | return 0 if successful | 277 | * return 0 if successful |
278 | return 1 if not (list contains no bad nodes) */ | 278 | * return 1 if not (list contains no bad nodes) |
279 | int replace_bad(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port) /* tested */ | 279 | */ |
280 | int replace_bad( Client_data * list, | ||
281 | uint32_t length, | ||
282 | uint8_t * client_id, | ||
283 | IP_Port ip_port ) | ||
280 | { | 284 | { |
281 | uint32_t i; | 285 | uint32_t i; |
282 | uint32_t temp_time = unix_time(); | 286 | uint32_t temp_time = unix_time(); |
283 | for(i = 0; i < length; ++i) | 287 | for(i = 0; i < length; ++i) { |
284 | if(list[i].timestamp + BAD_NODE_TIMEOUT < temp_time) { /* if node is bad. */ | 288 | /* if node is bad */ |
289 | if(list[i].timestamp + BAD_NODE_TIMEOUT < temp_time) { | ||
285 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); | 290 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); |
286 | list[i].ip_port = ip_port; | 291 | list[i].ip_port = ip_port; |
287 | list[i].timestamp = temp_time; | 292 | list[i].timestamp = temp_time; |
@@ -290,12 +295,17 @@ int replace_bad(Client_data * list, uint32_t length, uint8_t * client_id, IP_Por | |||
290 | list[i].ret_timestamp = 0; | 295 | list[i].ret_timestamp = 0; |
291 | return 0; | 296 | return 0; |
292 | } | 297 | } |
298 | } | ||
293 | 299 | ||
294 | return 1; | 300 | return 1; |
295 | } | 301 | } |
296 | 302 | ||
297 | /* replace the first good node that is further to the comp_client_id than that of the client_id in the list */ | 303 | /* replace the first good node that is further to the comp_client_id than that of the client_id in the list */ |
298 | int replace_good(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port, uint8_t * comp_client_id) | 304 | int replace_good( Client_data * list, |
305 | uint32_t length, | ||
306 | uint8_t * client_id, | ||
307 | IP_Port ip_port, | ||
308 | uint8_t * comp_client_id ) | ||
299 | { | 309 | { |
300 | uint32_t i; | 310 | uint32_t i; |
301 | uint32_t temp_time = unix_time(); | 311 | uint32_t temp_time = unix_time(); |
@@ -314,22 +324,46 @@ int replace_good(Client_data * list, uint32_t length, uint8_t * client_id, IP_Po | |||
314 | return 1; | 324 | return 1; |
315 | } | 325 | } |
316 | 326 | ||
317 | /* Attempt to add client with ip_port and client_id to the friends client list and close_clientlist */ | 327 | /* Attempt to add client with ip_port and client_id to the friends client list |
328 | * and close_clientlist | ||
329 | */ | ||
318 | void addto_lists(IP_Port ip_port, uint8_t * client_id) | 330 | void addto_lists(IP_Port ip_port, uint8_t * client_id) |
319 | { | 331 | { |
320 | uint32_t i; | 332 | uint32_t i; |
321 | 333 | ||
322 | /* NOTE: current behavior if there are two clients with the same id is to replace the first ip by the second. */ | 334 | /* NOTE: current behavior if there are two clients with the same id is |
323 | if(!client_in_list(close_clientlist, LCLIENT_LIST, client_id, ip_port)) | 335 | * to replace the first ip by the second. |
324 | if(replace_bad(close_clientlist, LCLIENT_LIST, client_id, ip_port)) | 336 | */ |
337 | if (!client_in_list(close_clientlist, LCLIENT_LIST, client_id, ip_port)) { | ||
338 | if (replace_bad(close_clientlist, LCLIENT_LIST, client_id, ip_port)) { | ||
325 | /* if we can't replace bad nodes we try replacing good ones */ | 339 | /* if we can't replace bad nodes we try replacing good ones */ |
326 | replace_good(close_clientlist, LCLIENT_LIST, client_id, ip_port, self_public_key); | 340 | replace_good( close_clientlist, |
341 | LCLIENT_LIST, | ||
342 | client_id, | ||
343 | ip_port, | ||
344 | self_public_key ); | ||
345 | } | ||
346 | } | ||
327 | 347 | ||
328 | for(i = 0; i < num_friends; ++i) | 348 | for (i = 0; i < num_friends; ++i) { |
329 | if(!client_in_list(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port)) | 349 | if (!client_in_list( friends_list[i].client_list, |
330 | if(replace_bad(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port)) | 350 | MAX_FRIEND_CLIENTS, |
351 | client_id, | ||
352 | ip_port )) { | ||
353 | |||
354 | if (replace_bad( friends_list[i].client_list, | ||
355 | MAX_FRIEND_CLIENTS, | ||
356 | client_id, | ||
357 | ip_port )) { | ||
331 | /* if we can't replace bad nodes we try replacing good ones. */ | 358 | /* if we can't replace bad nodes we try replacing good ones. */ |
332 | replace_good(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port, friends_list[i].client_id); | 359 | replace_good( friends_list[i].client_list, |
360 | MAX_FRIEND_CLIENTS, | ||
361 | client_id, | ||
362 | ip_port, | ||
363 | friends_list[i].client_id ); | ||
364 | } | ||
365 | } | ||
366 | } | ||
333 | } | 367 | } |
334 | 368 | ||
335 | /* If client_id is a friend or us, update ret_ip_port | 369 | /* If client_id is a friend or us, update ret_ip_port |