Improved hole punching a bit.

This commit is contained in:
irungentoo 2014-01-25 21:09:26 -05:00
parent 0d53abebcd
commit 86ba735198
3 changed files with 25 additions and 3 deletions

View File

@ -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)

View File

@ -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;

View File

@ -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