summaryrefslogtreecommitdiff
path: root/toxcore/DHT.c
diff options
context:
space:
mode:
authorDiadlo <polsha3@gmail.com>2017-03-08 00:14:50 +0300
committerDiadlo <polsha3@gmail.com>2017-06-05 02:04:38 +0300
commit6a69e071096a6dc5ac6914e983adc5dfc6fb9797 (patch)
tree596e3e91f3ea2905ad2d7339e6b2ae982af3b0d3 /toxcore/DHT.c
parent3902fd3ceea11a4d87fcee98dd9931720b13a518 (diff)
Cleanup punch_holes
Diffstat (limited to 'toxcore/DHT.c')
-rw-r--r--toxcore/DHT.c25
1 files changed, 14 insertions, 11 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index ebc40e71..c01b932e 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -2114,12 +2114,11 @@ static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports,
2114 return; 2114 return;
2115 } 2115 }
2116 2116
2117 uint16_t first_port = port_list[0];
2117 uint32_t i; 2118 uint32_t i;
2118 uint32_t top = dht->friends_list[friend_num].nat.punching_index + MAX_PUNCHING_PORTS;
2119 uint16_t firstport = port_list[0];
2120 2119
2121 for (i = 0; i < numports; ++i) { 2120 for (i = 0; i < numports; ++i) {
2122 if (firstport != port_list[i]) { 2121 if (first_port != port_list[i]) {
2123 break; 2122 break;
2124 } 2123 }
2125 } 2124 }
@@ -2127,33 +2126,37 @@ static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports,
2127 if (i == numports) { /* If all ports are the same, only try that one port. */ 2126 if (i == numports) { /* If all ports are the same, only try that one port. */
2128 IP_Port pinging; 2127 IP_Port pinging;
2129 ip_copy(&pinging.ip, &ip); 2128 ip_copy(&pinging.ip, &ip);
2130 pinging.port = net_htons(firstport); 2129 pinging.port = net_htons(first_port);
2131 send_ping_request(dht->ping, pinging, dht->friends_list[friend_num].public_key); 2130 send_ping_request(dht->ping, pinging, dht->friends_list[friend_num].public_key);
2132 } else { 2131 } else {
2133 for (uint32_t i = dht->friends_list[friend_num].nat.punching_index; i != top; ++i) { 2132 for (i = 0; i < MAX_PUNCHING_PORTS; ++i) {
2134 /* TODO(irungentoo): Improve port guessing algorithm. */ 2133 /* TODO(irungentoo): Improve port guessing algorithm. */
2135 uint16_t port = port_list[(i / 2) % numports] + (i / (2 * numports)) * ((i % 2) ? -1 : 1); 2134 uint32_t it = i + dht->friends_list[friend_num].nat.punching_index;
2135 int8_t sign = (it % 2) ? -1 : 1;
2136 uint32_t delta = sign * (it / (2 * numports));
2137 uint32_t index = (it / 2) % numports;
2138 uint16_t port = port_list[index] + delta;
2136 IP_Port pinging; 2139 IP_Port pinging;
2137 ip_copy(&pinging.ip, &ip); 2140 ip_copy(&pinging.ip, &ip);
2138 pinging.port = net_htons(port); 2141 pinging.port = net_htons(port);
2139 send_ping_request(dht->ping, pinging, dht->friends_list[friend_num].public_key); 2142 send_ping_request(dht->ping, pinging, dht->friends_list[friend_num].public_key);
2140 } 2143 }
2141 2144
2142 dht->friends_list[friend_num].nat.punching_index = i; 2145 dht->friends_list[friend_num].nat.punching_index += i;
2143 } 2146 }
2144 2147
2145 if (dht->friends_list[friend_num].nat.tries > MAX_NORMAL_PUNCHING_TRIES) { 2148 if (dht->friends_list[friend_num].nat.tries > MAX_NORMAL_PUNCHING_TRIES) {
2146 top = dht->friends_list[friend_num].nat.punching_index2 + MAX_PUNCHING_PORTS;
2147 uint16_t port = 1024; 2149 uint16_t port = 1024;
2148 IP_Port pinging; 2150 IP_Port pinging;
2149 ip_copy(&pinging.ip, &ip); 2151 ip_copy(&pinging.ip, &ip);
2150 2152
2151 for (uint32_t i = dht->friends_list[friend_num].nat.punching_index2; i != top; ++i) { 2153 for (i = 0; i < MAX_PUNCHING_PORTS; ++i) {
2152 pinging.port = net_htons(port + i); 2154 uint32_t it = i + dht->friends_list[friend_num].nat.punching_index2;
2155 pinging.port = net_htons(port + it);
2153 send_ping_request(dht->ping, pinging, dht->friends_list[friend_num].public_key); 2156 send_ping_request(dht->ping, pinging, dht->friends_list[friend_num].public_key);
2154 } 2157 }
2155 2158
2156 dht->friends_list[friend_num].nat.punching_index2 = i - (MAX_PUNCHING_PORTS / 2); 2159 dht->friends_list[friend_num].nat.punching_index2 += i - (MAX_PUNCHING_PORTS / 2);
2157 } 2160 }
2158 2161
2159 ++dht->friends_list[friend_num].nat.tries; 2162 ++dht->friends_list[friend_num].nat.tries;