summaryrefslogtreecommitdiff
path: root/core/DHT.c
diff options
context:
space:
mode:
authorJeffail <ash.jeffs@gmail.com>2013-08-02 10:43:00 +0100
committerJeffail <ash.jeffs@gmail.com>2013-08-02 10:43:00 +0100
commita5a1f917ab77a865b7c34c9ebd674ac5b93b36ad (patch)
treef1367c9a8a1fde9c1b051dec5a5d42036fcfbf4b /core/DHT.c
parente7f74555ea67b4d58f0dd739b489ffbc02a3f053 (diff)
Refactored DHT.c down to line 567
Diffstat (limited to 'core/DHT.c')
-rw-r--r--core/DHT.c137
1 files changed, 83 insertions, 54 deletions
diff --git a/core/DHT.c b/core/DHT.c
index 72ce52bc..680fdab2 100644
--- a/core/DHT.c
+++ b/core/DHT.c
@@ -367,53 +367,66 @@ void addto_lists(IP_Port ip_port, uint8_t * client_id)
367} 367}
368 368
369/* 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
370 nodeclient_id is the id of the node that sent us this info */ 370 * nodeclient_id is the id of the node that sent us this info
371 */
371void returnedip_ports(IP_Port ip_port, uint8_t * client_id, uint8_t * nodeclient_id) 372void returnedip_ports(IP_Port ip_port, uint8_t * client_id, uint8_t * nodeclient_id)
372{ 373{
373 uint32_t i, j; 374 uint32_t i, j, temp_time = unix_time();
374 uint32_t temp_time = unix_time(); 375
375 if(memcmp(client_id, self_public_key, CLIENT_ID_SIZE) == 0) { 376 if (memcmp(client_id, self_public_key, CLIENT_ID_SIZE) == 0) {
376 for(i = 0; i < LCLIENT_LIST; ++i) 377 for (i = 0; i < LCLIENT_LIST; ++i) {
377 if(memcmp(nodeclient_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0) { 378
379 if (memcmp( nodeclient_id,
380 close_clientlist[i].client_id,
381 CLIENT_ID_SIZE ) == 0) {
378 close_clientlist[i].ret_ip_port = ip_port; 382 close_clientlist[i].ret_ip_port = ip_port;
379 close_clientlist[i].ret_timestamp = temp_time; 383 close_clientlist[i].ret_timestamp = temp_time;
380 return; 384 return;
381 } 385 }
382 } else 386 }
383 for(i = 0; i < num_friends; ++i) 387 } else {
384 if(memcmp(client_id, friends_list[i].client_id, CLIENT_ID_SIZE) == 0) 388 for (i = 0; i < num_friends; ++i) {
385 for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) 389 if (memcmp( client_id,
386 if(memcmp(nodeclient_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE) == 0) { 390 friends_list[i].client_id,
391 CLIENT_ID_SIZE ) == 0) {
392 for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
393
394 if (memcmp( nodeclient_id,
395 friends_list[i].client_list[j].client_id,
396 CLIENT_ID_SIZE ) == 0) {
387 friends_list[i].client_list[j].ret_ip_port = ip_port; 397 friends_list[i].client_list[j].ret_ip_port = ip_port;
388 friends_list[i].client_list[j].ret_timestamp = temp_time; 398 friends_list[i].client_list[j].ret_timestamp = temp_time;
389 return; 399 return;
390 } 400 }
401 }
402 }
403 }
404 }
391} 405}
392 406
393/* check if we are currently pinging an ip_port and/or a ping_id 407/* check if we are currently pinging an ip_port and/or a ping_id variables with
394 variables with values of zero will not be checked. 408 * values of zero will not be checked. If we are already, return 1 else return 0
395 if we are already, return 1 409 *
396 else return 0 410 * TODO: optimize this
397TODO: optimize this */ 411 */
398int is_pinging(IP_Port ip_port, uint64_t ping_id) 412int is_pinging(IP_Port ip_port, uint64_t ping_id)
399{ 413{
400 uint32_t i; 414 uint32_t i, temp_time = unix_time();
401 uint8_t pinging; 415 uint8_t pinging;
402 uint32_t temp_time = unix_time();
403 416
404 for(i = 0; i < LPING_ARRAY; ++i ) 417 for (i = 0; i < LPING_ARRAY; ++i ) {
405 if((pings[i].timestamp + PING_TIMEOUT) > temp_time) { 418 if ((pings[i].timestamp + PING_TIMEOUT) > temp_time) {
406 pinging = 0; 419 pinging = 0;
407 if(ip_port.ip.i != 0) 420 if (ip_port.ip.i != 0 &&
408 if(pings[i].ip_port.ip.i == ip_port.ip.i && 421 pings[i].ip_port.ip.i == ip_port.ip.i &&
409 pings[i].ip_port.port == ip_port.port) 422 pings[i].ip_port.port == ip_port.port)
410 ++pinging; 423 ++pinging;
411 if(ping_id != 0) 424 if (ping_id != 0 && pings[i].ping_id == ping_id)
412 if(pings[i].ping_id == ping_id) 425 ++pinging;
413 ++pinging; 426 if (pinging == ((ping_id != 0) + (ip_port.ip.i != 0)))
414 if(pinging == (ping_id != 0) + (ip_port.ip.i != 0))
415 return 1; 427 return 1;
416 } 428 }
429 }
417 430
418 return 0; 431 return 0;
419} 432}
@@ -421,46 +434,47 @@ int is_pinging(IP_Port ip_port, uint64_t ping_id)
421/* Same as last function but for get_node requests. */ 434/* Same as last function but for get_node requests. */
422int is_gettingnodes(IP_Port ip_port, uint64_t ping_id) 435int is_gettingnodes(IP_Port ip_port, uint64_t ping_id)
423{ 436{
424 uint32_t i; 437 uint32_t i, temp_time = unix_time();
425 uint8_t pinging; 438 uint8_t pinging;
426 uint32_t temp_time = unix_time();
427 439
428 for(i = 0; i < LSEND_NODES_ARRAY; ++i ) 440 for(i = 0; i < LSEND_NODES_ARRAY; ++i ) {
429 if((send_nodes[i].timestamp + PING_TIMEOUT) > temp_time) { 441 if((send_nodes[i].timestamp + PING_TIMEOUT) > temp_time) {
430 pinging = 0; 442 pinging = 0;
431 if(ip_port.ip.i != 0) 443 if(ip_port.ip.i != 0 &&
432 if(send_nodes[i].ip_port.ip.i == ip_port.ip.i && 444 send_nodes[i].ip_port.ip.i == ip_port.ip.i &&
433 send_nodes[i].ip_port.port == ip_port.port) 445 send_nodes[i].ip_port.port == ip_port.port)
434 ++pinging; 446 ++pinging;
435 if(ping_id != 0) 447 if(ping_id != 0 && send_nodes[i].ping_id == ping_id)
436 if(send_nodes[i].ping_id == ping_id)
437 ++pinging; 448 ++pinging;
438 if(pinging == (ping_id != 0) + (ip_port.ip.i != 0)) 449 if(pinging == (ping_id != 0) + (ip_port.ip.i != 0))
439 return 1; 450 return 1;
440
441 } 451 }
452 }
442 453
443 return 0; 454 return 0;
444} 455}
445 456
446/* Add a new ping request to the list of ping requests 457/* Add a new ping request to the list of ping requests
447 returns the ping_id to put in the ping request 458 * returns the ping_id to put in the ping request
448 returns 0 if problem. 459 * returns 0 if problem.
449 TODO: optimize this */ 460 *
461 * TODO: optimize this
462 */
450uint64_t add_pinging(IP_Port ip_port) 463uint64_t add_pinging(IP_Port ip_port)
451{ 464{
452 uint32_t i, j; 465 uint32_t i, j, temp_time = unix_time();
453 uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int(); 466 uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int();
454 uint32_t temp_time = unix_time();
455 467
456 for(i = 0; i < PING_TIMEOUT; ++i ) 468 for(i = 0; i < PING_TIMEOUT; ++i ) {
457 for(j = 0; j < LPING_ARRAY; ++j ) 469 for(j = 0; j < LPING_ARRAY; ++j ) {
458 if((pings[j].timestamp + PING_TIMEOUT - i) < temp_time) { 470 if((pings[j].timestamp + PING_TIMEOUT - i) < temp_time) {
459 pings[j].timestamp = temp_time; 471 pings[j].timestamp = temp_time;
460 pings[j].ip_port = ip_port; 472 pings[j].ip_port = ip_port;
461 pings[j].ping_id = ping_id; 473 pings[j].ping_id = ping_id;
462 return ping_id; 474 return ping_id;
463 } 475 }
476 }
477 }
464 478
465 return 0; 479 return 0;
466} 480}
@@ -472,26 +486,28 @@ uint64_t add_gettingnodes(IP_Port ip_port)
472 uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int(); 486 uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int();
473 uint32_t temp_time = unix_time(); 487 uint32_t temp_time = unix_time();
474 488
475 for(i = 0; i < PING_TIMEOUT; ++i ) 489 for(i = 0; i < PING_TIMEOUT; ++i ) {
476 for(j = 0; j < LSEND_NODES_ARRAY; ++j ) 490 for(j = 0; j < LSEND_NODES_ARRAY; ++j ) {
477 if((send_nodes[j].timestamp + PING_TIMEOUT - i) < temp_time) { 491 if((send_nodes[j].timestamp + PING_TIMEOUT - i) < temp_time) {
478 send_nodes[j].timestamp = temp_time; 492 send_nodes[j].timestamp = temp_time;
479 send_nodes[j].ip_port = ip_port; 493 send_nodes[j].ip_port = ip_port;
480 send_nodes[j].ping_id = ping_id; 494 send_nodes[j].ping_id = ping_id;
481 return ping_id; 495 return ping_id;
482 } 496 }
497 }
498 }
483 499
484 return 0; 500 return 0;
485} 501}
486 502
487/* send a ping request 503/* send a ping request, only works if none has been sent to that ip/port
488 Ping request only works if none has been sent to that ip/port in the last 5 seconds. */ 504 * in the last 5 seconds.
505 */
489static int pingreq(IP_Port ip_port, uint8_t * public_key) 506static int pingreq(IP_Port ip_port, uint8_t * public_key)
490{ 507{
491 if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ 508 /* check if packet is gonna be sent to ourself */
492 return 1; 509 if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0
493 510 || is_pinging(ip_port, 0))
494 if(is_pinging(ip_port, 0))
495 return 1; 511 return 1;
496 512
497 uint64_t ping_id = add_pinging(ip_port); 513 uint64_t ping_id = add_pinging(ip_port);
@@ -503,9 +519,16 @@ static int pingreq(IP_Port ip_port, uint8_t * public_key)
503 uint8_t nonce[crypto_box_NONCEBYTES]; 519 uint8_t nonce[crypto_box_NONCEBYTES];
504 random_nonce(nonce); 520 random_nonce(nonce);
505 521
506 int len = encrypt_data(public_key, self_secret_key, nonce, (uint8_t *)&ping_id, sizeof(ping_id), encrypt); 522 int len = encrypt_data( public_key,
523 self_secret_key,
524 nonce,
525 (uint8_t *)&ping_id,
526 sizeof(ping_id),
527 encrypt );
528
507 if(len != sizeof(ping_id) + ENCRYPTION_PADDING) 529 if(len != sizeof(ping_id) + ENCRYPTION_PADDING)
508 return -1; 530 return -1;
531
509 data[0] = 0; 532 data[0] = 0;
510 memcpy(data + 1, self_public_key, CLIENT_ID_SIZE); 533 memcpy(data + 1, self_public_key, CLIENT_ID_SIZE);
511 memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); 534 memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES);
@@ -526,9 +549,15 @@ static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id)
526 uint8_t nonce[crypto_box_NONCEBYTES]; 549 uint8_t nonce[crypto_box_NONCEBYTES];
527 random_nonce(nonce); 550 random_nonce(nonce);
528 551
529 int len = encrypt_data(public_key, self_secret_key, nonce, (uint8_t *)&ping_id, sizeof(ping_id), encrypt); 552 int len = encrypt_data( public_key,
553 self_secret_key, nonce,
554 (uint8_t *)&ping_id,
555 sizeof(ping_id),
556 encrypt );
557
530 if(len != sizeof(ping_id) + ENCRYPTION_PADDING) 558 if(len != sizeof(ping_id) + ENCRYPTION_PADDING)
531 return -1; 559 return -1;
560
532 data[0] = 1; 561 data[0] = 1;
533 memcpy(data + 1, self_public_key, CLIENT_ID_SIZE); 562 memcpy(data + 1, self_public_key, CLIENT_ID_SIZE);
534 memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); 563 memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES);