diff options
author | plutooo <tfy12vbr@student.lu.se> | 2013-08-05 15:04:38 -0700 |
---|---|---|
committer | plutooo <tfy12vbr@student.lu.se> | 2013-08-06 08:51:37 -0700 |
commit | 99d9ecf74cab43ca6ae24ab5dd62a3b758b3e666 (patch) | |
tree | 6b62de268dba0f1240d3c98ae9625366eb3385d7 /core/DHT.c | |
parent | 071ac463082646189ade6e31bb3f6051516f81b2 (diff) |
core: Move send ping packets functions to ping.c
Diffstat (limited to 'core/DHT.c')
-rw-r--r-- | core/DHT.c | 84 |
1 files changed, 10 insertions, 74 deletions
@@ -24,6 +24,7 @@ | |||
24 | /*----------------------------------------------------------------------------------*/ | 24 | /*----------------------------------------------------------------------------------*/ |
25 | 25 | ||
26 | #include "DHT.h" | 26 | #include "DHT.h" |
27 | #include "packets.h" | ||
27 | #include "ping.h" | 28 | #include "ping.h" |
28 | 29 | ||
29 | /* maximum number of clients stored per friend. */ | 30 | /* maximum number of clients stored per friend. */ |
@@ -472,71 +473,6 @@ static uint64_t add_gettingnodes(IP_Port ip_port) | |||
472 | return 0; | 473 | return 0; |
473 | } | 474 | } |
474 | 475 | ||
475 | /* send a ping request, only works if none has been sent to that ip/port | ||
476 | * in the last 5 seconds. | ||
477 | */ | ||
478 | static int pingreq(IP_Port ip_port, uint8_t * public_key) | ||
479 | { | ||
480 | /* check if packet is gonna be sent to ourself */ | ||
481 | if(id_equal(public_key, self_public_key) || is_pinging(ip_port, 0)) | ||
482 | return 1; | ||
483 | |||
484 | uint64_t ping_id = add_ping(ip_port); | ||
485 | if(ping_id == 0) | ||
486 | return 1; | ||
487 | |||
488 | uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING]; | ||
489 | uint8_t encrypt[sizeof(ping_id) + ENCRYPTION_PADDING]; | ||
490 | uint8_t nonce[crypto_box_NONCEBYTES]; | ||
491 | random_nonce(nonce); | ||
492 | |||
493 | int len = encrypt_data( public_key, | ||
494 | self_secret_key, | ||
495 | nonce, | ||
496 | (uint8_t *)&ping_id, | ||
497 | sizeof(ping_id), | ||
498 | encrypt ); | ||
499 | |||
500 | if(len != sizeof(ping_id) + ENCRYPTION_PADDING) | ||
501 | return -1; | ||
502 | |||
503 | data[0] = 0; | ||
504 | memcpy(data + 1, self_public_key, CLIENT_ID_SIZE); | ||
505 | memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); | ||
506 | memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len); | ||
507 | |||
508 | return sendpacket(ip_port, data, sizeof(data)); | ||
509 | } | ||
510 | |||
511 | /* send a ping response */ | ||
512 | static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id) | ||
513 | { | ||
514 | /* check if packet is gonna be sent to ourself */ | ||
515 | if(id_equal(public_key, self_public_key)) | ||
516 | return 1; | ||
517 | |||
518 | uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING]; | ||
519 | uint8_t encrypt[sizeof(ping_id) + ENCRYPTION_PADDING]; | ||
520 | uint8_t nonce[crypto_box_NONCEBYTES]; | ||
521 | random_nonce(nonce); | ||
522 | |||
523 | int len = encrypt_data( public_key, | ||
524 | self_secret_key, nonce, | ||
525 | (uint8_t *)&ping_id, | ||
526 | sizeof(ping_id), | ||
527 | encrypt ); | ||
528 | |||
529 | if(len != sizeof(ping_id) + ENCRYPTION_PADDING) | ||
530 | return -1; | ||
531 | |||
532 | data[0] = 1; | ||
533 | memcpy(data + 1, self_public_key, CLIENT_ID_SIZE); | ||
534 | memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); | ||
535 | memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len); | ||
536 | |||
537 | return sendpacket(ip_port, data, sizeof(data)); | ||
538 | } | ||
539 | |||
540 | /* send a getnodes request */ | 476 | /* send a getnodes request */ |
541 | static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id) | 477 | static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id) |
542 | { | 478 | { |
@@ -641,8 +577,8 @@ static int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source) | |||
641 | if(len != sizeof(ping_id)) | 577 | if(len != sizeof(ping_id)) |
642 | return 1; | 578 | return 1; |
643 | 579 | ||
644 | pingres(source, packet + 1, ping_id); | 580 | send_ping_response(source, (clientid_t*) (packet + 1), ping_id); |
645 | pingreq(source, packet + 1); /* TODO: make this smarter? */ | 581 | send_ping_request(source, (clientid_t*) (packet + 1)); /* TODO: make this smarter? */ |
646 | 582 | ||
647 | return 0; | 583 | return 0; |
648 | } | 584 | } |
@@ -701,7 +637,7 @@ static int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source) | |||
701 | memcpy(&ping_id, plain, sizeof(ping_id)); | 637 | memcpy(&ping_id, plain, sizeof(ping_id)); |
702 | sendnodes(source, packet + 1, plain + sizeof(ping_id), ping_id); | 638 | sendnodes(source, packet + 1, plain + sizeof(ping_id), ping_id); |
703 | 639 | ||
704 | pingreq(source, packet + 1); /* TODO: make this smarter? */ | 640 | send_ping_request(source, (clientid_t*) (packet + 1)); /* TODO: make this smarter? */ |
705 | 641 | ||
706 | return 0; | 642 | return 0; |
707 | } | 643 | } |
@@ -741,7 +677,7 @@ static int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source) | |||
741 | 677 | ||
742 | uint32_t i; | 678 | uint32_t i; |
743 | for(i = 0; i < num_nodes; ++i) { | 679 | for(i = 0; i < num_nodes; ++i) { |
744 | pingreq(nodes_list[i].ip_port, nodes_list[i].client_id); | 680 | send_ping_request(nodes_list[i].ip_port, (clientid_t*) &nodes_list[i].client_id); |
745 | returnedip_ports(nodes_list[i].ip_port, nodes_list[i].client_id, packet + 1); | 681 | returnedip_ports(nodes_list[i].ip_port, nodes_list[i].client_id, packet + 1); |
746 | } | 682 | } |
747 | 683 | ||
@@ -831,8 +767,8 @@ static void doDHTFriends(void) | |||
831 | /* if node is not dead. */ | 767 | /* if node is not dead. */ |
832 | if (!is_timeout(temp_time, friends_list[i].client_list[j].timestamp, Kill_NODE_TIMEOUT)) { | 768 | if (!is_timeout(temp_time, friends_list[i].client_list[j].timestamp, Kill_NODE_TIMEOUT)) { |
833 | if ((friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) { | 769 | if ((friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) { |
834 | pingreq( friends_list[i].client_list[j].ip_port, | 770 | send_ping_request( friends_list[i].client_list[j].ip_port, |
835 | friends_list[i].client_list[j].client_id ); | 771 | (clientid_t*) &friends_list[i].client_list[j].client_id ); |
836 | friends_list[i].client_list[j].last_pinged = temp_time; | 772 | friends_list[i].client_list[j].last_pinged = temp_time; |
837 | } | 773 | } |
838 | /* if node is good. */ | 774 | /* if node is good. */ |
@@ -869,8 +805,8 @@ static void doClose(void) | |||
869 | /* if node is not dead. */ | 805 | /* if node is not dead. */ |
870 | if (!is_timeout(temp_time, close_clientlist[i].timestamp, Kill_NODE_TIMEOUT)) { | 806 | if (!is_timeout(temp_time, close_clientlist[i].timestamp, Kill_NODE_TIMEOUT)) { |
871 | if ((close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) { | 807 | if ((close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) { |
872 | pingreq( close_clientlist[i].ip_port, | 808 | send_ping_request( close_clientlist[i].ip_port, |
873 | close_clientlist[i].client_id ); | 809 | (clientid_t*) &close_clientlist[i].client_id ); |
874 | close_clientlist[i].last_pinged = temp_time; | 810 | close_clientlist[i].last_pinged = temp_time; |
875 | } | 811 | } |
876 | /* if node is good. */ | 812 | /* if node is good. */ |
@@ -1151,7 +1087,7 @@ static void punch_holes(IP ip, uint16_t * port_list, uint16_t numports, uint16_t | |||
1151 | /*TODO: improve port guessing algorithm*/ | 1087 | /*TODO: improve port guessing algorithm*/ |
1152 | uint16_t port = port_list[(i/2) % numports] + (i/(2*numports))*((i % 2) ? -1 : 1); | 1088 | uint16_t port = port_list[(i/2) % numports] + (i/(2*numports))*((i % 2) ? -1 : 1); |
1153 | IP_Port pinging = {ip, htons(port)}; | 1089 | IP_Port pinging = {ip, htons(port)}; |
1154 | pingreq(pinging, friends_list[friend_num].client_id); | 1090 | send_ping_request(pinging, (clientid_t*) &friends_list[friend_num].client_id); |
1155 | } | 1091 | } |
1156 | friends_list[friend_num].punching_index = i; | 1092 | friends_list[friend_num].punching_index = i; |
1157 | } | 1093 | } |