summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2014-01-25 21:09:26 -0500
committerirungentoo <irungentoo@gmail.com>2014-01-25 21:09:26 -0500
commit86ba73519848011bacd3cdd690eeaf402af21989 (patch)
tree85a4e4400d4995017589425d52ff4ce2367defdf
parent0d53abebcdea36adc509ee46e2bfdacea41ac5e4 (diff)
Improved hole punching a bit.
-rw-r--r--toxcore/DHT.c23
-rw-r--r--toxcore/DHT.h3
-rw-r--r--toxcore/ping.c2
3 files changed, 25 insertions, 3 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index d5808313..65fe6ae0 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -46,11 +46,13 @@
46/* Ping interval in seconds for each random sending of a get nodes request. */ 46/* Ping interval in seconds for each random sending of a get nodes request. */
47#define GET_NODE_INTERVAL 5 47#define GET_NODE_INTERVAL 5
48 48
49#define MAX_PUNCHING_PORTS 32 49#define MAX_PUNCHING_PORTS 48
50 50
51/* Interval in seconds between punching attempts*/ 51/* Interval in seconds between punching attempts*/
52#define PUNCH_INTERVAL 3 52#define PUNCH_INTERVAL 3
53 53
54#define MAX_NORMAL_PUNCHING_TRIES 5
55
54#define NAT_PING_REQUEST 0 56#define NAT_PING_REQUEST 0
55#define NAT_PING_RESPONSE 1 57#define NAT_PING_RESPONSE 1
56 58
@@ -1792,8 +1794,23 @@ static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports,
1792 ip_copy(&pinging.ip, &ip); 1794 ip_copy(&pinging.ip, &ip);
1793 pinging.port = htons(firstport); 1795 pinging.port = htons(firstport);
1794 send_ping_request(dht->ping, pinging, dht->friends_list[friend_num].client_id); 1796 send_ping_request(dht->ping, pinging, dht->friends_list[friend_num].client_id);
1797
1798 if (dht->friends_list[friend_num].nat.tries > MAX_NORMAL_PUNCHING_TRIES) {
1799 top = dht->friends_list[friend_num].nat.punching_index2 + MAX_PUNCHING_PORTS / 2;
1800 uint16_t port1 = 1024;
1801 uint16_t port2 = ~0;
1802
1803 for (i = dht->friends_list[friend_num].nat.punching_index2; i != top; ++i) {
1804 pinging.port = htons(port1 + i);
1805 send_ping_request(dht->ping, pinging, dht->friends_list[friend_num].client_id);
1806 pinging.port = htons(port2 - i);
1807 send_ping_request(dht->ping, pinging, dht->friends_list[friend_num].client_id);
1808 }
1809
1810 dht->friends_list[friend_num].nat.punching_index2 = i;
1811 }
1795 } else { 1812 } else {
1796 for (i = dht->friends_list[friend_num].nat.punching_index; i != top; i++) { 1813 for (i = dht->friends_list[friend_num].nat.punching_index; i != top; ++i) {
1797 /* TODO: Improve port guessing algorithm. */ 1814 /* TODO: Improve port guessing algorithm. */
1798 uint16_t port = port_list[(i / 2) % numports] + (i / (2 * numports)) * ((i % 2) ? -1 : 1); 1815 uint16_t port = port_list[(i / 2) % numports] + (i / (2 * numports)) * ((i % 2) ? -1 : 1);
1799 IP_Port pinging; 1816 IP_Port pinging;
@@ -1804,6 +1821,8 @@ static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports,
1804 1821
1805 dht->friends_list[friend_num].nat.punching_index = i; 1822 dht->friends_list[friend_num].nat.punching_index = i;
1806 } 1823 }
1824
1825 ++dht->friends_list[friend_num].nat.tries;
1807} 1826}
1808 1827
1809static void do_NAT(DHT *dht) 1828static void do_NAT(DHT *dht)
diff --git a/toxcore/DHT.h b/toxcore/DHT.h
index eb889d5d..b649338a 100644
--- a/toxcore/DHT.h
+++ b/toxcore/DHT.h
@@ -109,6 +109,9 @@ typedef struct {
109 /* 1 if currently hole punching, otherwise 0 */ 109 /* 1 if currently hole punching, otherwise 0 */
110 uint8_t hole_punching; 110 uint8_t hole_punching;
111 uint32_t punching_index; 111 uint32_t punching_index;
112 uint32_t tries;
113 uint32_t punching_index2;
114
112 uint64_t punching_timestamp; 115 uint64_t punching_timestamp;
113 uint64_t recvNATping_timestamp; 116 uint64_t recvNATping_timestamp;
114 uint64_t NATping_id; 117 uint64_t NATping_id;
diff --git a/toxcore/ping.c b/toxcore/ping.c
index a37b531d..6b1b906a 100644
--- a/toxcore/ping.c
+++ b/toxcore/ping.c
@@ -35,7 +35,7 @@
35#include "network.h" 35#include "network.h"
36#include "util.h" 36#include "util.h"
37 37
38#define PING_NUM_MAX 384 38#define PING_NUM_MAX 512
39 39
40/* Ping newly announced nodes to ping per TIME_TOPING seconds*/ 40/* Ping newly announced nodes to ping per TIME_TOPING seconds*/
41#define TIME_TOPING 5 41#define TIME_TOPING 5