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.
This commit is contained in:
Diadlo 2017-08-25 09:11:58 +03:00
parent 987ad5eac1
commit acb6b2d854
No known key found for this signature in database
GPG Key ID: 5AF9F2E29107C727
2 changed files with 18 additions and 0 deletions

View File

@ -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();
}
}

View File

@ -107,6 +107,7 @@ typedef struct {
void *fr_request_object;
uint64_t last_LANdiscovery;
uint16_t next_LANport;
bool local_discovery_enabled;
} Friend_Connections;