summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDiadlo <polsha3@gmail.com>2017-08-25 09:11:58 +0300
committerDiadlo <polsha3@gmail.com>2017-10-04 00:16:35 +0300
commitacb6b2d8543c8f2ea0c2e60dc046767cf5cc0de8 (patch)
treecd75794a175dabebdba3d649e4575f055b765cb5
parent987ad5eac173442d6ad2d5cd80c2da763a815a9a (diff)
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.
-rw-r--r--toxcore/friend_connection.c17
-rw-r--r--toxcore/friend_connection.h1
2 files changed, 18 insertions, 0 deletions
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 @@
29 29
30#include "util.h" 30#include "util.h"
31 31
32#define PORTS_PER_DISCOVERY 10
33
32/* return 1 if the friendcon_id is not valid. 34/* return 1 if the friendcon_id is not valid.
33 * return 0 if the friendcon_id is valid. 35 * return 0 if the friendcon_id is valid.
34 */ 36 */
@@ -827,6 +829,8 @@ Friend_Connections *new_friend_connections(Onion_Client *onion_c, bool local_dis
827 temp->net_crypto = onion_c->c; 829 temp->net_crypto = onion_c->c;
828 temp->onion_c = onion_c; 830 temp->onion_c = onion_c;
829 temp->local_discovery_enabled = local_discovery_enabled; 831 temp->local_discovery_enabled = local_discovery_enabled;
832 // Don't include default port in port range
833 temp->next_LANport = TOX_PORTRANGE_FROM + 1;
830 834
831 new_connection_handler(temp->net_crypto, &handle_new_connections, temp); 835 new_connection_handler(temp->net_crypto, &handle_new_connections, temp);
832 836
@@ -841,7 +845,20 @@ Friend_Connections *new_friend_connections(Onion_Client *onion_c, bool local_dis
841static void LANdiscovery(Friend_Connections *fr_c) 845static void LANdiscovery(Friend_Connections *fr_c)
842{ 846{
843 if (fr_c->last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) { 847 if (fr_c->last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) {
848 const uint16_t first = fr_c->next_LANport;
849 uint16_t last = first + PORTS_PER_DISCOVERY;
850 last = last > TOX_PORTRANGE_TO ? TOX_PORTRANGE_TO : last;
851
852 // Always send to default port
844 send_LANdiscovery(net_htons(TOX_PORT_DEFAULT), fr_c->dht); 853 send_LANdiscovery(net_htons(TOX_PORT_DEFAULT), fr_c->dht);
854
855 // And check some extra ports
856 for (uint16_t port = first; port < last; port++) {
857 send_LANdiscovery(net_htons(port), fr_c->dht);
858 }
859
860 // Don't include default port in port range
861 fr_c->next_LANport = last != TOX_PORTRANGE_TO ? last : TOX_PORTRANGE_FROM + 1;
845 fr_c->last_LANdiscovery = unix_time(); 862 fr_c->last_LANdiscovery = unix_time();
846 } 863 }
847} 864}
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 {
107 void *fr_request_object; 107 void *fr_request_object;
108 108
109 uint64_t last_LANdiscovery; 109 uint64_t last_LANdiscovery;
110 uint16_t next_LANport;
110 111
111 bool local_discovery_enabled; 112 bool local_discovery_enabled;
112} Friend_Connections; 113} Friend_Connections;