summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--toxcore/LAN_discovery.c37
-rw-r--r--toxcore/LAN_discovery.h3
-rw-r--r--toxcore/friend_connection.c20
-rw-r--r--toxcore/friend_connection.h2
4 files changed, 54 insertions, 8 deletions
diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c
index dbce762e..5ab5b0e1 100644
--- a/toxcore/LAN_discovery.c
+++ b/toxcore/LAN_discovery.c
@@ -227,18 +227,43 @@ static IP broadcast_ip(sa_family_t family_socket, sa_family_t family_broadcast)
227 return ip; 227 return ip;
228} 228}
229 229
230/* Is IP a local ip or not. */
231_Bool Local_ip(IP ip)
232{
233 if (ip.family == AF_INET) {
234 IP4 ip4 = ip.ip4;
235
236 /* Loopback. */
237 if (ip4.uint8[0] == 127)
238 return 1;
239 } else {
240 /* embedded IPv4-in-IPv6 */
241 if (IPV6_IPV4_IN_V6(ip.ip6)) {
242 IP ip4;
243 ip4.family = AF_INET;
244 ip4.ip4.uint32 = ip.ip6.uint32[3];
245 return Local_ip(ip4);
246 }
247
248 /* localhost in IPv6 (::1) */
249 if (ip.ip6.uint64[0] == 0 && ip.ip6.uint32[2] == 0 && ip.ip6.uint32[3] == htonl(1))
250 return 1;
251 }
252
253 return 0;
254}
255
230/* return 0 if ip is a LAN ip. 256/* return 0 if ip is a LAN ip.
231 * return -1 if it is not. 257 * return -1 if it is not.
232 */ 258 */
233int LAN_ip(IP ip) 259int LAN_ip(IP ip)
234{ 260{
261 if (Local_ip(ip))
262 return 0;
263
235 if (ip.family == AF_INET) { 264 if (ip.family == AF_INET) {
236 IP4 ip4 = ip.ip4; 265 IP4 ip4 = ip.ip4;
237 266
238 /* Loopback. */
239 if (ip4.uint8[0] == 127)
240 return 0;
241
242 /* 10.0.0.0 to 10.255.255.255 range. */ 267 /* 10.0.0.0 to 10.255.255.255 range. */
243 if (ip4.uint8[0] == 10) 268 if (ip4.uint8[0] == 10)
244 return 0; 269 return 0;
@@ -276,10 +301,6 @@ int LAN_ip(IP ip)
276 ip4.ip4.uint32 = ip.ip6.uint32[3]; 301 ip4.ip4.uint32 = ip.ip6.uint32[3];
277 return LAN_ip(ip4); 302 return LAN_ip(ip4);
278 } 303 }
279
280 /* localhost in IPv6 (::1) */
281 if (ip.ip6.uint64[0] == 0 && ip.ip6.uint32[2] == 0 && ip.ip6.uint32[3] == htonl(1))
282 return 0;
283 } 304 }
284 305
285 return -1; 306 return -1;
diff --git a/toxcore/LAN_discovery.h b/toxcore/LAN_discovery.h
index 5243bd93..358bea2f 100644
--- a/toxcore/LAN_discovery.h
+++ b/toxcore/LAN_discovery.h
@@ -40,6 +40,9 @@ void LANdiscovery_init(DHT *dht);
40/* Clear packet handlers. */ 40/* Clear packet handlers. */
41void LANdiscovery_kill(DHT *dht); 41void LANdiscovery_kill(DHT *dht);
42 42
43/* Is IP a local ip or not. */
44_Bool Local_ip(IP ip);
45
43/* checks if a given IP isn't routable 46/* checks if a given IP isn't routable
44 * 47 *
45 * return 0 if ip is a LAN ip. 48 * return 0 if ip is a LAN ip.
diff --git a/toxcore/friend_connection.c b/toxcore/friend_connection.c
index de34e570..7dd8e3e3 100644
--- a/toxcore/friend_connection.c
+++ b/toxcore/friend_connection.c
@@ -158,6 +158,20 @@ int friend_add_tcp_relay(Friend_Connections *fr_c, int friendcon_id, IP_Port ip_
158 if (!friend_con) 158 if (!friend_con)
159 return -1; 159 return -1;
160 160
161 /* Local ip means that they are hosting a TCP relay. */
162 if (Local_ip(ip_port.ip)) {
163 if (friend_con->dht_ip_port.ip.family != 0) {
164 ip_port.ip = friend_con->dht_ip_port.ip;
165 } else {
166 if (memcmp(friend_con->dht_temp_pk, public_key, crypto_box_PUBLICKEYBYTES) == 0) {
167 friend_con->hosting_tcp_relay = 0;
168 return 0;
169 } else {
170 return -1;
171 }
172 }
173 }
174
161 unsigned int i; 175 unsigned int i;
162 176
163 uint16_t index = friend_con->tcp_relay_counter % FRIEND_MAX_STORED_TCP_RELAYS; 177 uint16_t index = friend_con->tcp_relay_counter % FRIEND_MAX_STORED_TCP_RELAYS;
@@ -268,6 +282,11 @@ static void dht_ip_callback(void *object, int32_t number, IP_Port ip_port)
268 set_direct_ip_port(fr_c->net_crypto, friend_con->crypt_connection_id, ip_port, 1); 282 set_direct_ip_port(fr_c->net_crypto, friend_con->crypt_connection_id, ip_port, 1);
269 friend_con->dht_ip_port = ip_port; 283 friend_con->dht_ip_port = ip_port;
270 friend_con->dht_ip_port_lastrecv = unix_time(); 284 friend_con->dht_ip_port_lastrecv = unix_time();
285
286 if (friend_con->hosting_tcp_relay) {
287 friend_add_tcp_relay(fr_c, number, ip_port, friend_con->dht_temp_pk);
288 friend_con->hosting_tcp_relay = 0;
289 }
271} 290}
272 291
273static void change_dht_pk(Friend_Connections *fr_c, int friendcon_id, const uint8_t *dht_public_key) 292static void change_dht_pk(Friend_Connections *fr_c, int friendcon_id, const uint8_t *dht_public_key)
@@ -317,6 +336,7 @@ static int handle_status(void *object, int number, uint8_t status)
317 336
318 friend_con->status = FRIENDCONN_STATUS_CONNECTING; 337 friend_con->status = FRIENDCONN_STATUS_CONNECTING;
319 friend_con->crypt_connection_id = -1; 338 friend_con->crypt_connection_id = -1;
339 friend_con->hosting_tcp_relay = 0;
320 } 340 }
321 341
322 if (call_cb) { 342 if (call_cb) {
diff --git a/toxcore/friend_connection.h b/toxcore/friend_connection.h
index baca4b76..32e947ac 100644
--- a/toxcore/friend_connection.h
+++ b/toxcore/friend_connection.h
@@ -96,6 +96,8 @@ typedef struct {
96 96
97 Node_format tcp_relays[FRIEND_MAX_STORED_TCP_RELAYS]; 97 Node_format tcp_relays[FRIEND_MAX_STORED_TCP_RELAYS];
98 uint16_t tcp_relay_counter; 98 uint16_t tcp_relay_counter;
99
100 _Bool hosting_tcp_relay;
99} Friend_Conn; 101} Friend_Conn;
100 102
101 103