summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-07-28 17:10:55 -0400
committerirungentoo <irungentoo@gmail.com>2013-07-28 17:10:55 -0400
commit0eb429e45821974b9006d089baacad10cddec606 (patch)
treeaa3e51e1e4f2dc5001edf9980de7f98b9813efdf
parentcaaa1ff8889ee8f517539dcc0f32c7b795d3c8fc (diff)
Slightly improved hole punching in the DHT.
-rw-r--r--core/DHT.c21
1 files changed, 11 insertions, 10 deletions
diff --git a/core/DHT.c b/core/DHT.c
index 014ec588..5039f710 100644
--- a/core/DHT.c
+++ b/core/DHT.c
@@ -45,6 +45,7 @@ typedef struct {
45 uint8_t hole_punching; /*0 if not hole punching, 1 if currently hole punching */ 45 uint8_t hole_punching; /*0 if not hole punching, 1 if currently hole punching */
46 uint32_t punching_index; 46 uint32_t punching_index;
47 uint32_t punching_timestamp; 47 uint32_t punching_timestamp;
48 uint32_t recvNATping_timestamp;
48 uint64_t NATping_id; 49 uint64_t NATping_id;
49 uint32_t NATping_timestamp; 50 uint32_t NATping_timestamp;
50} Friend; 51} Friend;
@@ -623,10 +624,7 @@ int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source)
623int DHT_addfriend(uint8_t * client_id) 624int DHT_addfriend(uint8_t * client_id)
624{ 625{
625 Friend * temp; 626 Friend * temp;
626 if(num_friends == 0) 627 temp = realloc(friends_list, sizeof(Friend) * (num_friends + 1));
627 temp = malloc(sizeof(Friend));
628 else
629 temp = realloc(friends_list, sizeof(Friend) * (num_friends + 1));
630 if(temp == NULL) 628 if(temp == NULL)
631 return 1; 629 return 1;
632 630
@@ -903,6 +901,7 @@ int handle_NATping(uint8_t * packet, uint32_t length, IP_Port source)
903 901
904 if(data[0] == 0) { 902 if(data[0] == 0) {
905 send_NATping(public_key, ping_id, 1); /*1 is reply*/ 903 send_NATping(public_key, ping_id, 1); /*1 is reply*/
904 friends_list[friendnumber].recvNATping_timestamp = unix_time();
906 return 0; 905 return 0;
907 } else if (data[0] == 1) 906 } else if (data[0] == 1)
908 if(friends_list[friendnumber].NATping_id == ping_id) { 907 if(friends_list[friendnumber].NATping_id == ping_id) {
@@ -987,12 +986,14 @@ static void doNAT()
987 if(num < MAX_FRIEND_CLIENTS/2) 986 if(num < MAX_FRIEND_CLIENTS/2)
988 continue; 987 continue;
989 988
990 if(friends_list[i].hole_punching != 1) { 989
991 if(friends_list[i].NATping_timestamp + PUNCH_INTERVAL < temp_time) { 990 if(friends_list[i].NATping_timestamp + PUNCH_INTERVAL < temp_time) {
992 send_NATping(friends_list[i].client_id, friends_list[i].NATping_id, 0); /*0 is request*/ 991 send_NATping(friends_list[i].client_id, friends_list[i].NATping_id, 0); /*0 is request*/
993 friends_list[i].NATping_timestamp = temp_time; 992 friends_list[i].NATping_timestamp = temp_time;
994 } 993 }
995 } else if(friends_list[i].punching_timestamp + PUNCH_INTERVAL < temp_time) { 994 if(friends_list[i].hole_punching == 1 &&
995 friends_list[i].punching_timestamp + PUNCH_INTERVAL < temp_time &&
996 friends_list[i].recvNATping_timestamp + PUNCH_INTERVAL*2 >= temp_time) {
996 IP ip = NAT_commonip(ip_list, num, MAX_FRIEND_CLIENTS/2); 997 IP ip = NAT_commonip(ip_list, num, MAX_FRIEND_CLIENTS/2);
997 if(ip.i == 0) 998 if(ip.i == 0)
998 continue; 999 continue;