Skip to content

Commit

Permalink
cleanup: Don't pass the whole DHT object to lan discovery.
Browse files Browse the repository at this point in the history
It only needs net and dht public key.
  • Loading branch information
iphydf committed Feb 3, 2022
1 parent 014cc42 commit 28dc8c1
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion other/DHT_bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ int main(int argc, char *argv[])
do_dht(dht);

if (mono_time_is_timeout(mono_time, last_LANdiscovery, is_waiting_for_dht_connection ? 5 : LAN_DISCOVERY_INTERVAL)) {
lan_discovery_send(net_htons(PORT), dht);
lan_discovery_send(dht_get_net(dht), dht_get_self_public_key(dht), net_htons(PORT));
last_LANdiscovery = mono_time_get(mono_time);
}

Expand Down
2 changes: 1 addition & 1 deletion other/bootstrap_daemon/docker/tox-bootstrapd.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
87ab42da03d54cf33815b364a974d1548a2a673415bd82e86ffbd6511483eb81 /usr/local/bin/tox-bootstrapd
de00572e0a22b67defb05759a4d5aac6bf0e107bfd6834a1edc20ffb0379528d /usr/local/bin/tox-bootstrapd
2 changes: 1 addition & 1 deletion other/bootstrap_daemon/src/tox-bootstrapd.c
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ int main(int argc, char *argv[])
do_dht(dht);

if (enable_lan_discovery && mono_time_is_timeout(mono_time, last_LANdiscovery, LAN_DISCOVERY_INTERVAL)) {
lan_discovery_send(net_htons_port, dht);
lan_discovery_send(dht_get_net(dht), dht_get_self_public_key(dht), net_htons_port);
last_LANdiscovery = mono_time_get(mono_time);
}

Expand Down
20 changes: 10 additions & 10 deletions toxcore/LAN_discovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -367,35 +367,35 @@ static int handle_LANdiscovery(void *object, const IP_Port *source, const uint8_
}


int lan_discovery_send(uint16_t port, const DHT *dht)
bool lan_discovery_send(Networking_Core *net, const uint8_t *dht_pk, uint16_t port)
{
uint8_t data[CRYPTO_PUBLIC_KEY_SIZE + 1];
data[0] = NET_PACKET_LAN_DISCOVERY;
id_copy(data + 1, dht_get_self_public_key(dht));
id_copy(data + 1, dht_pk);

send_broadcasts(dht_get_net(dht), port, data, 1 + CRYPTO_PUBLIC_KEY_SIZE);
send_broadcasts(net, port, data, 1 + CRYPTO_PUBLIC_KEY_SIZE);

int res = -1;
bool res = false;
IP_Port ip_port;
ip_port.port = port;

/* IPv6 multicast */
if (net_family_is_ipv6(net_family(dht_get_net(dht)))) {
if (net_family_is_ipv6(net_family(net))) {
ip_port.ip = broadcast_ip(net_family_ipv6, net_family_ipv6);

if (ip_isset(&ip_port.ip)) {
if (sendpacket(dht_get_net(dht), &ip_port, data, 1 + CRYPTO_PUBLIC_KEY_SIZE) > 0) {
res = 1;
if (sendpacket(net, &ip_port, data, 1 + CRYPTO_PUBLIC_KEY_SIZE) > 0) {
res = true;
}
}
}

/* IPv4 broadcast (has to be IPv4-in-IPv6 mapping if socket is IPv6 */
ip_port.ip = broadcast_ip(net_family(dht_get_net(dht)), net_family_ipv4);
ip_port.ip = broadcast_ip(net_family(net), net_family_ipv4);

if (ip_isset(&ip_port.ip)) {
if (sendpacket(dht_get_net(dht), &ip_port, data, 1 + CRYPTO_PUBLIC_KEY_SIZE)) {
res = 1;
if (sendpacket(net, &ip_port, data, 1 + CRYPTO_PUBLIC_KEY_SIZE)) {
res = true;
}
}

Expand Down
4 changes: 3 additions & 1 deletion toxcore/LAN_discovery.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

/**
* Send a LAN discovery pcaket to the broadcast address with port port.
*
* @return true on success, false on failure.
*/
int32_t lan_discovery_send(uint16_t port, const DHT *dht);
bool lan_discovery_send(Networking_Core *net, const uint8_t *dht_pk, uint16_t port);

/**
* Sets up packet handlers.
Expand Down
4 changes: 2 additions & 2 deletions toxcore/friend_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -897,11 +897,11 @@ static void lan_discovery(Friend_Connections *fr_c)
last = last > TOX_PORTRANGE_TO ? TOX_PORTRANGE_TO : last;

// Always send to default port
lan_discovery_send(net_htons(TOX_PORT_DEFAULT), fr_c->dht);
lan_discovery_send(dht_get_net(fr_c->dht), dht_get_self_public_key(fr_c->dht), net_htons(TOX_PORT_DEFAULT));

// And check some extra ports
for (uint16_t port = first; port < last; ++port) {
lan_discovery_send(net_htons(port), fr_c->dht);
lan_discovery_send(dht_get_net(fr_c->dht), dht_get_self_public_key(fr_c->dht), net_htons(port));
}

// Don't include default port in port range
Expand Down

0 comments on commit 28dc8c1

Please sign in to comment.