Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions auto_tests/announce_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ static void test_store_data(void)
ck_assert(net != nullptr);
DHT *dht = new_dht(log, mem, rng, ns, mono_time, net, true, true);
ck_assert(dht != nullptr);
Forwarding *forwarding = new_forwarding(log, mem, rng, mono_time, dht);
Forwarding *forwarding = new_forwarding(log, mem, rng, mono_time, dht, net);
ck_assert(forwarding != nullptr);
Announcements *announce = new_announcements(log, mem, rng, mono_time, forwarding);
Announcements *announce = new_announcements(log, mem, rng, mono_time, forwarding, dht, net);
ck_assert(announce != nullptr);

/* Just to prevent CI from complaining that set_synch_offset is unused: */
Expand Down
6 changes: 3 additions & 3 deletions auto_tests/forwarding_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@ static Forwarding_Subtox *new_forwarding_subtox(const Memory *mem, bool no_udp,
ck_assert(subtox->tcp_np != nullptr);

const TCP_Proxy_Info inf = {{{{0}}}};
subtox->c = new_net_crypto(subtox->log, mem, rng, ns, subtox->mono_time, subtox->dht, &inf, subtox->tcp_np);
subtox->c = new_net_crypto(subtox->log, mem, rng, ns, subtox->mono_time, subtox->net, subtox->dht, &inf, subtox->tcp_np);

subtox->forwarding = new_forwarding(subtox->log, mem, rng, subtox->mono_time, subtox->dht);
subtox->forwarding = new_forwarding(subtox->log, mem, rng, subtox->mono_time, subtox->dht, subtox->net);
ck_assert(subtox->forwarding != nullptr);

subtox->announce = new_announcements(subtox->log, mem, rng, subtox->mono_time, subtox->forwarding);
subtox->announce = new_announcements(subtox->log, mem, rng, subtox->mono_time, subtox->forwarding, subtox->dht, subtox->net);
ck_assert(subtox->announce != nullptr);

return subtox;
Expand Down
36 changes: 21 additions & 15 deletions auto_tests/onion_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "../testing/misc_tools.h"
#include "../toxcore/mono_time.h"
#include "../toxcore/network.h"
#include "../toxcore/onion.h"
#include "../toxcore/onion_announce.h"
#include "../toxcore/onion_client.h"
Expand Down Expand Up @@ -238,8 +239,10 @@ static void test_basic(void)
Mono_Time *mono_time2 = mono_time_new(mem, nullptr, nullptr);

IP ip = get_loopback();
Onion *onion1 = new_onion(log1, mem, mono_time1, rng, new_dht(log1, mem, rng, ns, mono_time1, new_networking(log1, mem, ns, &ip, 36567), true, false));
Onion *onion2 = new_onion(log2, mem, mono_time2, rng, new_dht(log2, mem, rng, ns, mono_time2, new_networking(log2, mem, ns, &ip, 36568), true, false));
Networking_Core *net1 = new_networking(log1, mem, ns, &ip, 36567);
Onion *onion1 = new_onion(log1, mem, mono_time1, rng, new_dht(log1, mem, rng, ns, mono_time1, net1, true, false), net1);
Networking_Core *net2 = new_networking(log2, mem, ns, &ip, 36568);
Onion *onion2 = new_onion(log2, mem, mono_time2, rng, new_dht(log2, mem, rng, ns, mono_time2, net2, true, false), net2);
ck_assert_msg((onion1 != nullptr) && (onion2 != nullptr), "Onion failed initializing.");
networking_registerhandler(onion2->net, NET_PACKET_ANNOUNCE_REQUEST, &handle_test_1, onion2);

Expand Down Expand Up @@ -282,8 +285,8 @@ static void test_basic(void)
do_onion(mono_time2, onion2);
} while (handled_test_2 == 0);

Onion_Announce *onion1_a = new_onion_announce(log1, mem, rng, mono_time1, onion1->dht);
Onion_Announce *onion2_a = new_onion_announce(log2, mem, rng, mono_time2, onion2->dht);
Onion_Announce *onion1_a = new_onion_announce(log1, mem, rng, mono_time1, onion1->dht, onion1->net);
Onion_Announce *onion2_a = new_onion_announce(log2, mem, rng, mono_time2, onion2->dht, onion2->net);
networking_registerhandler(onion1->net, NET_PACKET_ANNOUNCE_RESPONSE, &handle_test_3, onion1);
networking_registerhandler(onion1->net, NET_PACKET_ANNOUNCE_RESPONSE_OLD, &handle_test_3_old, onion1);
ck_assert_msg((onion1_a != nullptr) && (onion2_a != nullptr), "Onion_Announce failed initializing.");
Expand Down Expand Up @@ -335,7 +338,8 @@ static void test_basic(void)

Mono_Time *mono_time3 = mono_time_new(mem, nullptr, nullptr);

Onion *onion3 = new_onion(log3, mem, mono_time3, rng, new_dht(log3, mem, rng, ns, mono_time3, new_networking(log3, mem, ns, &ip, 36569), true, false));
Networking_Core *net3 = new_networking(log3, mem, ns, &ip, 36569);
Onion *onion3 = new_onion(log3, mem, mono_time3, rng, new_dht(log3, mem, rng, ns, mono_time3, net3, true, false), net3);
ck_assert_msg((onion3 != nullptr), "Onion failed initializing.");

random_nonce(rng, nonce);
Expand All @@ -360,7 +364,7 @@ static void test_basic(void)
{
Onion *onion = onion3;

Networking_Core *net = dht_get_net(onion->dht);
Networking_Core *net = onion->net;
DHT *dht = onion->dht;
kill_onion(onion);
kill_dht(dht);
Expand All @@ -372,7 +376,7 @@ static void test_basic(void)
{
Onion *onion = onion2;

Networking_Core *net = dht_get_net(onion->dht);
Networking_Core *net = onion->net;
DHT *dht = onion->dht;
kill_onion(onion);
kill_dht(dht);
Expand All @@ -384,7 +388,7 @@ static void test_basic(void)
{
Onion *onion = onion1;

Networking_Core *net = dht_get_net(onion->dht);
Networking_Core *net = onion->net;
DHT *dht = onion->dht;
kill_onion(onion);
kill_dht(dht);
Expand All @@ -397,6 +401,7 @@ static void test_basic(void)
typedef struct {
Logger *log;
Mono_Time *mono_time;
Net_Crypto *nc;
Net_Profile *tcp_np;
Onion *onion;
Onion_Announce *onion_a;
Expand Down Expand Up @@ -450,7 +455,7 @@ static Onions *new_onions(const Memory *mem, const Random *rng, uint16_t port, u
return nullptr;
}

on->onion = new_onion(on->log, mem, on->mono_time, rng, dht);
on->onion = new_onion(on->log, mem, on->mono_time, rng, dht, net);

if (!on->onion) {
kill_dht(dht);
Expand All @@ -461,7 +466,7 @@ static Onions *new_onions(const Memory *mem, const Random *rng, uint16_t port, u
return nullptr;
}

on->onion_a = new_onion_announce(on->log, mem, rng, on->mono_time, dht);
on->onion_a = new_onion_announce(on->log, mem, rng, on->mono_time, dht, net);

if (!on->onion_a) {
kill_onion(on->onion);
Expand All @@ -487,7 +492,8 @@ static Onions *new_onions(const Memory *mem, const Random *rng, uint16_t port, u
}

TCP_Proxy_Info inf = {{{{0}}}};
on->onion_c = new_onion_client(on->log, mem, rng, on->mono_time, new_net_crypto(on->log, mem, rng, ns, on->mono_time, dht, &inf, on->tcp_np));
on->nc = new_net_crypto(on->log, mem, rng, ns, on->mono_time, net, dht, &inf, on->tcp_np);
on->onion_c = new_onion_client(on->log, mem, rng, on->mono_time, on->nc, dht, net);

if (!on->onion_c) {
netprof_kill(mem, on->tcp_np);
Expand Down Expand Up @@ -515,9 +521,9 @@ static void do_onions(Onions *on)

static void kill_onions(const Memory *mem, Onions *on)
{
Networking_Core *net = dht_get_net(on->onion->dht);
Networking_Core *net = on->onion->net;
DHT *dht = on->onion->dht;
Net_Crypto *c = onion_get_net_crypto(on->onion_c);
Net_Crypto *c = on->nc;
kill_onion_client(on->onion_c);
kill_onion_announce(on->onion_a);
kill_onion(on->onion);
Expand Down Expand Up @@ -641,9 +647,9 @@ static void test_announce(void)

printf("adding friend\n");
int frnum_f = onion_addfriend(onions[NUM_FIRST]->onion_c,
nc_get_self_public_key(onion_get_net_crypto(onions[NUM_LAST]->onion_c)));
nc_get_self_public_key(onions[NUM_LAST]->nc));
int frnum = onion_addfriend(onions[NUM_LAST]->onion_c,
nc_get_self_public_key(onion_get_net_crypto(onions[NUM_FIRST]->onion_c)));
nc_get_self_public_key(onions[NUM_FIRST]->nc));

onion_dht_pk_callback(onions[NUM_FIRST]->onion_c, frnum_f, &dht_pk_callback, onions[NUM_FIRST], NUM_FIRST);
onion_dht_pk_callback(onions[NUM_LAST]->onion_c, frnum, &dht_pk_callback, onions[NUM_LAST], NUM_LAST);
Expand Down
17 changes: 9 additions & 8 deletions other/DHT_bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,15 @@ int main(int argc, char *argv[])
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
const uint16_t start_port = PORT;
const uint16_t end_port = start_port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM);
DHT *dht = new_dht(logger, mem, rng, ns, mono_time, new_networking_ex(logger, mem, ns, &ip, start_port, end_port, nullptr), true, true);
Onion *onion = new_onion(logger, mem, mono_time, rng, dht);
Forwarding *forwarding = new_forwarding(logger, mem, rng, mono_time, dht);
Networking_Core *net = new_networking_ex(logger, mem, ns, &ip, start_port, end_port, nullptr);
DHT *dht = new_dht(logger, mem, rng, ns, mono_time, net, true, true);
Onion *onion = new_onion(logger, mem, mono_time, rng, dht, net);
Forwarding *forwarding = new_forwarding(logger, mem, rng, mono_time, dht, net);
GC_Announces_List *gc_announces_list = new_gca_list(mem);
Onion_Announce *onion_a = new_onion_announce(logger, mem, rng, mono_time, dht);
Onion_Announce *onion_a = new_onion_announce(logger, mem, rng, mono_time, dht, net);

#ifdef DHT_NODE_EXTRA_PACKETS
bootstrap_set_callbacks(dht_get_net(dht), (uint32_t)DAEMON_VERSION_NUMBER, (const uint8_t *) motd_str, strlen(motd_str) + 1);
bootstrap_set_callbacks(net, (uint32_t)DAEMON_VERSION_NUMBER, (const uint8_t *) motd_str, strlen(motd_str) + 1);
#endif

if (onion == nullptr || forwarding == nullptr || onion_a == nullptr) {
Expand Down Expand Up @@ -216,7 +217,7 @@ int main(int argc, char *argv[])
fclose(file);

printf("\n");
printf("Port: %u\n", net_ntohs(net_port(dht_get_net(dht))));
printf("Port: %u\n", net_ntohs(net_port(net)));

if (argc > argvoffset + 3) {
printf("Trying to bootstrap into the network...\n");
Expand Down Expand Up @@ -260,7 +261,7 @@ int main(int argc, char *argv[])
do_dht(dht);

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

Expand All @@ -269,7 +270,7 @@ int main(int argc, char *argv[])
#ifdef TCP_RELAY_ENABLED
do_tcp_server(tcp_s, mono_time);
#endif
networking_poll(dht_get_net(dht), nullptr);
networking_poll(net, nullptr);

c_sleep(1);
}
Expand Down
14 changes: 7 additions & 7 deletions other/bootstrap_daemon/src/tox-bootstrapd.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ int main(int argc, char *argv[])
return 1;
}

Forwarding *forwarding = new_forwarding(logger, mem, rng, mono_time, dht);
Forwarding *forwarding = new_forwarding(logger, mem, rng, mono_time, dht, net);

if (forwarding == nullptr) {
LOG_WRITE(LOG_LEVEL_ERROR, "Couldn't initialize forwarding. Exiting.\n");
Expand All @@ -367,7 +367,7 @@ int main(int argc, char *argv[])
return 1;
}

Announcements *announce = new_announcements(logger, mem, rng, mono_time, forwarding);
Announcements *announce = new_announcements(logger, mem, rng, mono_time, forwarding, dht, net);

if (announce == nullptr) {
LOG_WRITE(LOG_LEVEL_ERROR, "Couldn't initialize DHT announcements. Exiting.\n");
Expand Down Expand Up @@ -398,7 +398,7 @@ int main(int argc, char *argv[])
return 1;
}

Onion *onion = new_onion(logger, mem, mono_time, rng, dht);
Onion *onion = new_onion(logger, mem, mono_time, rng, dht, net);

if (onion == nullptr) {
LOG_WRITE(LOG_LEVEL_ERROR, "Couldn't initialize Tox Onion. Exiting.\n");
Expand All @@ -415,7 +415,7 @@ int main(int argc, char *argv[])
return 1;
}

Onion_Announce *onion_a = new_onion_announce(logger, mem, rng, mono_time, dht);
Onion_Announce *onion_a = new_onion_announce(logger, mem, rng, mono_time, dht, net);

if (onion_a == nullptr) {
LOG_WRITE(LOG_LEVEL_ERROR, "Couldn't initialize Tox Onion Announce. Exiting.\n");
Expand All @@ -436,7 +436,7 @@ int main(int argc, char *argv[])
gca_onion_init(group_announce, onion_a);

if (enable_motd) {
if (bootstrap_set_callbacks(dht_get_net(dht), DAEMON_VERSION_NUMBER, (uint8_t *)motd, strlen(motd) + 1) == 0) {
if (bootstrap_set_callbacks(net, DAEMON_VERSION_NUMBER, (uint8_t *)motd, strlen(motd) + 1) == 0) {
LOG_WRITE(LOG_LEVEL_INFO, "Set MOTD successfully.\n");
free(motd);
} else {
Expand Down Expand Up @@ -596,7 +596,7 @@ int main(int argc, char *argv[])
do_dht(dht);

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

Expand All @@ -606,7 +606,7 @@ int main(int argc, char *argv[])
do_tcp_server(tcp_server, mono_time);
}

networking_poll(dht_get_net(dht), nullptr);
networking_poll(net, nullptr);

if (waiting_for_dht_connection && dht_isconnected(dht)) {
LOG_WRITE(LOG_LEVEL_INFO, "Connected to another bootstrap node successfully.\n");
Expand Down
7 changes: 1 addition & 6 deletions toxcore/DHT.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include "shared_key_cache.h"
#include "sort.h"
#include "state.h"
#include "util.h"

/** The timeout after which a node is discarded completely. */
#define KILL_NODE_TIMEOUT (BAD_NODE_TIMEOUT + PING_INTERVAL)
Expand Down Expand Up @@ -161,10 +160,6 @@ void dht_set_self_secret_key(DHT *dht, const uint8_t *key)
memcpy(dht->self_secret_key, key, CRYPTO_SECRET_KEY_SIZE);
}

Networking_Core *dht_get_net(const DHT *dht)
{
return dht->net;
}
struct Ping *dht_get_ping(const DHT *dht)
{
return dht->ping;
Expand Down Expand Up @@ -2509,7 +2504,7 @@ DHT *new_dht(const Logger *log, const Memory *mem, const Random *rng, const Netw
dht->hole_punching_enabled = hole_punching_enabled;
dht->lan_discovery_enabled = lan_discovery_enabled;

dht->ping = ping_new(mem, mono_time, rng, dht);
dht->ping = ping_new(mem, mono_time, rng, dht, net);

if (dht->ping == nullptr) {
LOGGER_ERROR(log, "failed to initialise ping");
Expand Down
1 change: 0 additions & 1 deletion toxcore/DHT.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ const uint8_t *_Nonnull dht_get_self_secret_key(const DHT *_Nonnull dht);
void dht_set_self_public_key(DHT *_Nonnull dht, const uint8_t *_Nonnull key);
void dht_set_self_secret_key(DHT *_Nonnull dht, const uint8_t *_Nonnull key);

Networking_Core *_Nonnull dht_get_net(const DHT *_Nonnull dht);
struct Ping *_Nonnull dht_get_ping(const DHT *_Nonnull dht);
const Client_data *_Nonnull dht_get_close_clientlist(const DHT *_Nonnull dht);
const Client_data *_Nonnull dht_get_close_client(const DHT *_Nonnull dht, uint32_t client_num);
Expand Down
14 changes: 7 additions & 7 deletions toxcore/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -3443,7 +3443,7 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random *
return nullptr;
}

m->net_crypto = new_net_crypto(m->log, m->mem, m->rng, m->ns, m->mono_time, m->dht, &options->proxy_info, m->tcp_np);
m->net_crypto = new_net_crypto(m->log, m->mem, m->rng, m->ns, m->mono_time, m->net, m->dht, &options->proxy_info, m->tcp_np);

if (m->net_crypto == nullptr) {
LOGGER_WARNING(m->log, "net_crypto initialisation failed");
Expand Down Expand Up @@ -3473,9 +3473,9 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random *
}

if (options->dht_announcements_enabled) {
m->forwarding = new_forwarding(m->log, m->mem, m->rng, m->mono_time, m->dht);
m->forwarding = new_forwarding(m->log, m->mem, m->rng, m->mono_time, m->dht, m->net);
if (m->forwarding != nullptr) {
m->announce = new_announcements(m->log, m->mem, m->rng, m->mono_time, m->forwarding);
m->announce = new_announcements(m->log, m->mem, m->rng, m->mono_time, m->forwarding, m->dht, m->net);
} else {
m->announce = nullptr;
}
Expand All @@ -3484,11 +3484,11 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random *
m->announce = nullptr;
}

m->onion = new_onion(m->log, m->mem, m->mono_time, m->rng, m->dht);
m->onion_a = new_onion_announce(m->log, m->mem, m->rng, m->mono_time, m->dht);
m->onion_c = new_onion_client(m->log, m->mem, m->rng, m->mono_time, m->net_crypto);
m->onion = new_onion(m->log, m->mem, m->mono_time, m->rng, m->dht, m->net);
m->onion_a = new_onion_announce(m->log, m->mem, m->rng, m->mono_time, m->dht, m->net);
m->onion_c = new_onion_client(m->log, m->mem, m->rng, m->mono_time, m->net_crypto, m->dht, m->net);
if (m->onion_c != nullptr) {
m->fr_c = new_friend_connections(m->log, m->mem, m->mono_time, m->ns, m->onion_c, options->local_discovery_enabled);
m->fr_c = new_friend_connections(m->log, m->mem, m->mono_time, m->ns, m->onion_c, m->dht, m->net_crypto, m->net, options->local_discovery_enabled);
}

if ((options->dht_announcements_enabled && (m->forwarding == nullptr || m->announce == nullptr)) ||
Expand Down
6 changes: 3 additions & 3 deletions toxcore/announce.c
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ static int handle_dht_announce_request(
}

Announcements *new_announcements(const Logger *log, const Memory *mem, const Random *rng, const Mono_Time *mono_time,
Forwarding *forwarding)
Forwarding *forwarding, DHT *dht, Networking_Core *net)
{
if (log == nullptr || mono_time == nullptr || forwarding == nullptr) {
return nullptr;
Expand All @@ -630,8 +630,8 @@ Announcements *new_announcements(const Logger *log, const Memory *mem, const Ran
announce->rng = rng;
announce->forwarding = forwarding;
announce->mono_time = mono_time;
announce->dht = forwarding_get_dht(forwarding);
announce->net = dht_get_net(announce->dht);
announce->dht = dht;
announce->net = net;
announce->public_key = dht_get_self_public_key(announce->dht);
announce->secret_key = dht_get_self_secret_key(announce->dht);
new_hmac_key(announce->rng, announce->hmac_key);
Expand Down
5 changes: 4 additions & 1 deletion toxcore/announce.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@

#include <stdint.h>

#include "DHT.h"
#include "attributes.h"
#include "crypto_core.h"
#include "forwarding.h"
#include "logger.h"
#include "mem.h"
#include "mono_time.h"
#include "network.h"

#define MAX_ANNOUNCEMENT_SIZE 512

Expand All @@ -22,7 +24,8 @@ uint8_t announce_response_of_request_type(uint8_t request_type);

typedef struct Announcements Announcements;

Announcements *_Nullable new_announcements(const Logger *_Nonnull log, const Memory *_Nonnull mem, const Random *_Nonnull rng, const Mono_Time *_Nonnull mono_time, Forwarding *_Nonnull forwarding);
Announcements *_Nullable new_announcements(const Logger *_Nonnull log, const Memory *_Nonnull mem, const Random *_Nonnull rng, const Mono_Time *_Nonnull mono_time, Forwarding *_Nonnull forwarding,
DHT *_Nonnull dht, Networking_Core *_Nonnull net);

/**
* @brief If data is stored, run `on_retrieve_callback` on it.
Expand Down
Loading
Loading