From 28dc8c1ded4a917a2ad704609c578f6d8d8e7f28 Mon Sep 17 00:00:00 2001 From: iphydf Date: Thu, 3 Feb 2022 20:39:33 +0000 Subject: [PATCH] cleanup: Don't pass the whole DHT object to lan discovery. It only needs net and dht public key. --- other/DHT_bootstrap.c | 2 +- .../docker/tox-bootstrapd.sha256 | 2 +- other/bootstrap_daemon/src/tox-bootstrapd.c | 2 +- toxcore/LAN_discovery.c | 20 +++++++++---------- toxcore/LAN_discovery.h | 4 +++- toxcore/friend_connection.c | 4 ++-- 6 files changed, 18 insertions(+), 16 deletions(-) diff --git a/other/DHT_bootstrap.c b/other/DHT_bootstrap.c index d34ad679..21afa60e 100644 --- a/other/DHT_bootstrap.c +++ b/other/DHT_bootstrap.c @@ -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); } diff --git a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 index 28ea6f35..bbb8cd8f 100644 --- a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 +++ b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 @@ -1 +1 @@ -87ab42da03d54cf33815b364a974d1548a2a673415bd82e86ffbd6511483eb81 /usr/local/bin/tox-bootstrapd +de00572e0a22b67defb05759a4d5aac6bf0e107bfd6834a1edc20ffb0379528d /usr/local/bin/tox-bootstrapd diff --git a/other/bootstrap_daemon/src/tox-bootstrapd.c b/other/bootstrap_daemon/src/tox-bootstrapd.c index 072d4628..ce7f7308 100644 --- a/other/bootstrap_daemon/src/tox-bootstrapd.c +++ b/other/bootstrap_daemon/src/tox-bootstrapd.c @@ -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); } diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c index 11282b9a..cb5fc3f1 100644 --- a/toxcore/LAN_discovery.c +++ b/toxcore/LAN_discovery.c @@ -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; } } diff --git a/toxcore/LAN_discovery.h b/toxcore/LAN_discovery.h index e6701807..d4cc85fd 100644 --- a/toxcore/LAN_discovery.h +++ b/toxcore/LAN_discovery.h @@ -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. diff --git a/toxcore/friend_connection.c b/toxcore/friend_connection.c index 3507f8f4..9e28d588 100644 --- a/toxcore/friend_connection.c +++ b/toxcore/friend_connection.c @@ -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