From f2497b65897f034e736c234242fb08b2fc3b4a24 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Sat, 12 Oct 2013 18:51:59 -0400 Subject: [PATCH] Improved hole punching. --- toxcore/DHT.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 2583b891..470826f0 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -1690,17 +1690,30 @@ static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports, uint32_t i; uint32_t top = dht->friends_list[friend_num].nat.punching_index + MAX_PUNCHING_PORTS; + uint16_t firstport = port_list[0]; - 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; - ip_copy(&pinging.ip, &ip); - pinging.port = htons(port); - send_ping_request(dht->ping, pinging, dht->friends_list[friend_num].client_id); + for (i = 0; i < numports; ++i) { + if (firstport != port_list[0]) + break; } - dht->friends_list[friend_num].nat.punching_index = i; + if (i == numports) { /* If all ports are the same, only try that one port. */ + IP_Port pinging; + ip_copy(&pinging.ip, &ip); + pinging.port = htons(firstport); + send_ping_request(dht->ping, pinging, dht->friends_list[friend_num].client_id); + } else { + 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; + ip_copy(&pinging.ip, &ip); + pinging.port = htons(port); + send_ping_request(dht->ping, pinging, dht->friends_list[friend_num].client_id); + } + + dht->friends_list[friend_num].nat.punching_index = i; + } } static void do_NAT(DHT *dht)