From acb6b2d8543c8f2ea0c2e60dc046767cf5cc0de8 Mon Sep 17 00:00:00 2001 From: Diadlo Date: Fri, 25 Aug 2017 09:11:58 +0300 Subject: [PATCH] Improve LAN discovery Issue: If another tox instance started on the not default port, LAN discovery will be failed. Now tox will iterate though all possible ports to find another tox instances. --- toxcore/friend_connection.c | 17 +++++++++++++++++ toxcore/friend_connection.h | 1 + 2 files changed, 18 insertions(+) diff --git a/toxcore/friend_connection.c b/toxcore/friend_connection.c index ccf1621f..2610c13f 100644 --- a/toxcore/friend_connection.c +++ b/toxcore/friend_connection.c @@ -29,6 +29,8 @@ #include "util.h" +#define PORTS_PER_DISCOVERY 10 + /* return 1 if the friendcon_id is not valid. * return 0 if the friendcon_id is valid. */ @@ -827,6 +829,8 @@ Friend_Connections *new_friend_connections(Onion_Client *onion_c, bool local_dis temp->net_crypto = onion_c->c; temp->onion_c = onion_c; temp->local_discovery_enabled = local_discovery_enabled; + // Don't include default port in port range + temp->next_LANport = TOX_PORTRANGE_FROM + 1; new_connection_handler(temp->net_crypto, &handle_new_connections, temp); @@ -841,7 +845,20 @@ Friend_Connections *new_friend_connections(Onion_Client *onion_c, bool local_dis static void LANdiscovery(Friend_Connections *fr_c) { if (fr_c->last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) { + const uint16_t first = fr_c->next_LANport; + uint16_t last = first + PORTS_PER_DISCOVERY; + last = last > TOX_PORTRANGE_TO ? TOX_PORTRANGE_TO : last; + + // Always send to default port send_LANdiscovery(net_htons(TOX_PORT_DEFAULT), fr_c->dht); + + // And check some extra ports + for (uint16_t port = first; port < last; port++) { + send_LANdiscovery(net_htons(port), fr_c->dht); + } + + // Don't include default port in port range + fr_c->next_LANport = last != TOX_PORTRANGE_TO ? last : TOX_PORTRANGE_FROM + 1; fr_c->last_LANdiscovery = unix_time(); } } diff --git a/toxcore/friend_connection.h b/toxcore/friend_connection.h index 3fa2ebce..e993a103 100644 --- a/toxcore/friend_connection.h +++ b/toxcore/friend_connection.h @@ -107,6 +107,7 @@ typedef struct { void *fr_request_object; uint64_t last_LANdiscovery; + uint16_t next_LANport; bool local_discovery_enabled; } Friend_Connections;