Skip to content

Commit

Permalink
cleanup: Remove port from Broadcast_Info.
Browse files Browse the repository at this point in the history
This is added later when we actually send the broadcasts.
  • Loading branch information
iphydf committed Mar 3, 2022
1 parent 64254d6 commit 825262a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 18 deletions.
2 changes: 1 addition & 1 deletion auto_tests/auto_test_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ void set_mono_time_callback(AutoTox *autotox)
Mono_Time *mono_time = ((Messenger *)autotox->tox)->mono_time;

autotox->clock = current_time_monotonic(mono_time);
mono_time_set_current_time_callback(mono_time, nullptr, &autotox->clock); // set to default first
mono_time_set_current_time_callback(mono_time, nullptr, nullptr); // set to default first
mono_time_set_current_time_callback(mono_time, get_state_clock_callback, &autotox->clock);
}

Expand Down
22 changes: 21 additions & 1 deletion auto_tests/lan_discovery_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,31 @@
#include "../toxcore/ccompat.h"
#include "auto_test_support.h"

static uint64_t get_state_clock_callback(Mono_Time *mono_time, void *user_data)
{
const uint64_t *clock = (const uint64_t *)user_data;
return *clock;
}

int main(void)
{
setvbuf(stdout, nullptr, _IONBF, 0);

Tox *tox1 = tox_new_log_lan(nullptr, nullptr, nullptr, /* lan_discovery */true);
Tox *tox2 = tox_new_log_lan(nullptr, nullptr, nullptr, /* lan_discovery */true);

// TODO(iphydf): Don't rely on toxcore internals.
uint64_t clock = current_time_monotonic(((Messenger *)tox1)->mono_time);
Mono_Time *mono_time;

// TODO(iphydf): Don't rely on toxcore internals.
mono_time = ((Messenger *)tox1)->mono_time;
mono_time_set_current_time_callback(mono_time, get_state_clock_callback, &clock);

// TODO(iphydf): Don't rely on toxcore internals.
mono_time = ((Messenger *)tox2)->mono_time;
mono_time_set_current_time_callback(mono_time, get_state_clock_callback, &clock);

printf("Waiting for LAN discovery. This loop will attempt to run until successful.");

do {
Expand All @@ -19,7 +38,8 @@ int main(void)

tox_iterate(tox1, nullptr);
tox_iterate(tox2, nullptr);
c_sleep(1000);
c_sleep(5);
clock += 100;
} while (tox_self_get_connection_status(tox1) == TOX_CONNECTION_NONE ||
tox_self_get_connection_status(tox2) == TOX_CONNECTION_NONE);

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 @@
eb9e7efd034de17ba6af095bbfd595308ff994148f394e715087078859d860f6 /usr/local/bin/tox-bootstrapd
ce191b497af89aafb2837d053043469c725ea7af0b1aa2bbedc39f8de2686d56 /usr/local/bin/tox-bootstrapd
31 changes: 16 additions & 15 deletions toxcore/LAN_discovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@

struct Broadcast_Info {
uint32_t count;
IP_Port ip_ports[MAX_INTERFACES];
IP ips[MAX_INTERFACES];
};

#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)

static Broadcast_Info *fetch_broadcast_info(uint16_t port)
static Broadcast_Info *fetch_broadcast_info(void)
{
Broadcast_Info *broadcast = (Broadcast_Info *)calloc(1, sizeof(Broadcast_Info));

Expand Down Expand Up @@ -89,13 +89,12 @@ static Broadcast_Info *fetch_broadcast_info(uint16_t port)
if (addr_parse_ip(pAdapter->IpAddressList.IpMask.String, &subnet_mask)
&& addr_parse_ip(pAdapter->GatewayList.IpAddress.String, &gateway)) {
if (net_family_is_ipv4(gateway.family) && net_family_is_ipv4(subnet_mask.family)) {
IP_Port *ip_port = &broadcast->ip_ports[broadcast->count];
ip_port->ip.family = net_family_ipv4;
IP *ip = &broadcast->ips[broadcast->count];
ip->family = net_family_ipv4;
const uint32_t gateway_ip = net_ntohl(gateway.ip.v4.uint32);
const uint32_t subnet_ip = net_ntohl(subnet_mask.ip.v4.uint32);
const uint32_t broadcast_ip = gateway_ip + ~subnet_ip - 1;
ip_port->ip.ip.v4.uint32 = net_htonl(broadcast_ip);
ip_port->port = port;
ip->ip.v4.uint32 = net_htonl(broadcast_ip);
++broadcast->count;

if (broadcast->count >= MAX_INTERFACES) {
Expand All @@ -117,7 +116,7 @@ static Broadcast_Info *fetch_broadcast_info(uint16_t port)

#elif !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) && (defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__))

static Broadcast_Info *fetch_broadcast_info(uint16_t port)
static Broadcast_Info *fetch_broadcast_info(void)
{
Broadcast_Info *broadcast = (Broadcast_Info *)calloc(1, sizeof(Broadcast_Info));

Expand Down Expand Up @@ -174,15 +173,14 @@ static Broadcast_Info *fetch_broadcast_info(uint16_t port)
break;
}

IP_Port *ip_port = &broadcast->ip_ports[broadcast->count];
ip_port->ip.family = net_family_ipv4;
ip_port->ip.ip.v4.uint32 = sock4->sin_addr.s_addr;
IP *ip = &broadcast->ips[broadcast->count];
ip->family = net_family_ipv4;
ip->ip.v4.uint32 = sock4->sin_addr.s_addr;

if (ip_port->ip.ip.v4.uint32 == 0) {
if (ip->ip.v4.uint32 == 0) {
continue;
}

ip_port->port = port;
++broadcast->count;
}

Expand All @@ -193,7 +191,7 @@ static Broadcast_Info *fetch_broadcast_info(uint16_t port)

#else // TODO(irungentoo): Other platforms?

static Broadcast_Info *fetch_broadcast_info(uint16_t port)
static Broadcast_Info *fetch_broadcast_info(void)
{
return (Broadcast_Info *)calloc(1, sizeof(Broadcast_Info));
}
Expand All @@ -214,7 +212,10 @@ static bool send_broadcasts(const Networking_Core *net, const Broadcast_Info *br
}

for (uint32_t i = 0; i < broadcast->count; ++i) {
sendpacket(net, &broadcast->ip_ports[i], data, length);
IP_Port ip_port;
ip_port.ip = broadcast->ips[i];
ip_port.port = port;
sendpacket(net, &ip_port, data, length);
}

return true;
Expand Down Expand Up @@ -393,7 +394,7 @@ bool lan_discovery_send(Networking_Core *net, const Broadcast_Info *broadcast, c

Broadcast_Info *lan_discovery_init(DHT *dht)
{
Broadcast_Info *broadcast = fetch_broadcast_info(net_htons(TOX_PORT_DEFAULT));
Broadcast_Info *broadcast = fetch_broadcast_info();
networking_registerhandler(dht_get_net(dht), NET_PACKET_LAN_DISCOVERY, &handle_LANdiscovery, dht);
return broadcast;
}
Expand Down

0 comments on commit 825262a

Please sign in to comment.