From 86ba73519848011bacd3cdd690eeaf402af21989 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Sat, 25 Jan 2014 21:09:26 -0500 Subject: Improved hole punching a bit. --- toxcore/DHT.c | 23 +++++++++++++++++++++-- toxcore/DHT.h | 3 +++ toxcore/ping.c | 2 +- 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 @@ /* Ping interval in seconds for each random sending of a get nodes request. */ #define GET_NODE_INTERVAL 5 -#define MAX_PUNCHING_PORTS 32 +#define MAX_PUNCHING_PORTS 48 /* Interval in seconds between punching attempts*/ #define PUNCH_INTERVAL 3 +#define MAX_NORMAL_PUNCHING_TRIES 5 + #define NAT_PING_REQUEST 0 #define NAT_PING_RESPONSE 1 @@ -1792,8 +1794,23 @@ static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports, ip_copy(&pinging.ip, &ip); pinging.port = htons(firstport); send_ping_request(dht->ping, pinging, dht->friends_list[friend_num].client_id); + + if (dht->friends_list[friend_num].nat.tries > MAX_NORMAL_PUNCHING_TRIES) { + top = dht->friends_list[friend_num].nat.punching_index2 + MAX_PUNCHING_PORTS / 2; + uint16_t port1 = 1024; + uint16_t port2 = ~0; + + for (i = dht->friends_list[friend_num].nat.punching_index2; i != top; ++i) { + pinging.port = htons(port1 + i); + send_ping_request(dht->ping, pinging, dht->friends_list[friend_num].client_id); + pinging.port = htons(port2 - i); + send_ping_request(dht->ping, pinging, dht->friends_list[friend_num].client_id); + } + + dht->friends_list[friend_num].nat.punching_index2 = i; + } } else { - for (i = dht->friends_list[friend_num].nat.punching_index; i != top; i++) { + for (i = dht->friends_list[friend_num].nat.punching_index; i != top; ++i) { /* TODO: Improve port guessing algorithm. */ uint16_t port = port_list[(i / 2) % numports] + (i / (2 * numports)) * ((i % 2) ? -1 : 1); IP_Port pinging; @@ -1804,6 +1821,8 @@ static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports, dht->friends_list[friend_num].nat.punching_index = i; } + + ++dht->friends_list[friend_num].nat.tries; } static 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 { /* 1 if currently hole punching, otherwise 0 */ uint8_t hole_punching; uint32_t punching_index; + uint32_t tries; + uint32_t punching_index2; + uint64_t punching_timestamp; uint64_t recvNATping_timestamp; 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 @@ #include "network.h" #include "util.h" -#define PING_NUM_MAX 384 +#define PING_NUM_MAX 512 /* Ping newly announced nodes to ping per TIME_TOPING seconds*/ #define TIME_TOPING 5 -- cgit v1.2.3