diff --git a/auto_tests/announce_test.c b/auto_tests/announce_test.c index 18f61c3f58..008af93d6b 100644 --- a/auto_tests/announce_test.c +++ b/auto_tests/announce_test.c @@ -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: */ diff --git a/auto_tests/forwarding_test.c b/auto_tests/forwarding_test.c index 754eebd697..3e5c307107 100644 --- a/auto_tests/forwarding_test.c +++ b/auto_tests/forwarding_test.c @@ -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; diff --git a/auto_tests/onion_test.c b/auto_tests/onion_test.c index b8ff1cab31..f4cbeff7b9 100644 --- a/auto_tests/onion_test.c +++ b/auto_tests/onion_test.c @@ -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" @@ -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); @@ -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."); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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; @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); diff --git a/other/DHT_bootstrap.c b/other/DHT_bootstrap.c index feb4dbda25..547e324112 100644 --- a/other/DHT_bootstrap.c +++ b/other/DHT_bootstrap.c @@ -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) { @@ -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"); @@ -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); } @@ -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); } diff --git a/other/bootstrap_daemon/src/tox-bootstrapd.c b/other/bootstrap_daemon/src/tox-bootstrapd.c index 125551e3cb..e5e24a22c7 100644 --- a/other/bootstrap_daemon/src/tox-bootstrapd.c +++ b/other/bootstrap_daemon/src/tox-bootstrapd.c @@ -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"); @@ -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"); @@ -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"); @@ -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"); @@ -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 { @@ -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); } @@ -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"); diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 025de96782..58341bdf8a 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -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) @@ -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; @@ -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"); diff --git a/toxcore/DHT.h b/toxcore/DHT.h index 43ddbadb07..6279053f28 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h @@ -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); diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index a04e1f639a..8902311fce 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -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"); @@ -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; } @@ -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)) || diff --git a/toxcore/announce.c b/toxcore/announce.c index b8dfb56625..382e01880d 100644 --- a/toxcore/announce.c +++ b/toxcore/announce.c @@ -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; @@ -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); diff --git a/toxcore/announce.h b/toxcore/announce.h index 27b2f78558..2c1183c4ad 100644 --- a/toxcore/announce.h +++ b/toxcore/announce.h @@ -7,12 +7,14 @@ #include +#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 @@ -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. diff --git a/toxcore/forwarding.c b/toxcore/forwarding.c index 4c7cf91172..5b29c8e760 100644 --- a/toxcore/forwarding.c +++ b/toxcore/forwarding.c @@ -37,11 +37,6 @@ struct Forwarding { void *forwarded_response_callback_object; }; -DHT *forwarding_get_dht(const Forwarding *forwarding) -{ - return forwarding->dht; -} - #define SENDBACK_TIMEOUT 3600 bool send_forward_request(const Networking_Core *net, const IP_Port *forwarder, @@ -348,7 +343,8 @@ void set_callback_forward_reply(Forwarding *forwarding, forward_reply_cb *functi forwarding->forward_reply_callback_object = object; } -Forwarding *_Nullable new_forwarding(const Logger *log, const Memory *mem, const Random *rng, const Mono_Time *mono_time, DHT *dht) +Forwarding *_Nullable new_forwarding(const Logger *log, const Memory *mem, const Random *rng, const Mono_Time *mono_time, DHT *dht, + Networking_Core *net) { if (log == nullptr || mono_time == nullptr || dht == nullptr) { return nullptr; @@ -365,7 +361,7 @@ Forwarding *_Nullable new_forwarding(const Logger *log, const Memory *mem, const forwarding->rng = rng; forwarding->mono_time = mono_time; forwarding->dht = dht; - forwarding->net = dht_get_net(dht); + forwarding->net = net; networking_registerhandler(forwarding->net, NET_PACKET_FORWARD_REQUEST, &handle_forward_request, forwarding); networking_registerhandler(forwarding->net, NET_PACKET_FORWARD_REPLY, &handle_forward_reply, forwarding); diff --git a/toxcore/forwarding.h b/toxcore/forwarding.h index ac533b3ab0..8cef884a49 100644 --- a/toxcore/forwarding.h +++ b/toxcore/forwarding.h @@ -30,8 +30,6 @@ extern "C" { typedef struct Forwarding Forwarding; -DHT *_Nonnull forwarding_get_dht(const Forwarding *_Nonnull forwarding); - /** * @brief Send data to forwarder for forwarding via chain of dht nodes. * Destination is last key in the chain. @@ -100,7 +98,8 @@ typedef bool forward_reply_cb(void *_Nullable object, const uint8_t *_Nullable s * sendback. */ void set_callback_forward_reply(Forwarding *_Nonnull forwarding, forward_reply_cb *_Nullable function, void *_Nullable object); -Forwarding *_Nullable new_forwarding(const Logger *_Nonnull log, const Memory *_Nonnull mem, const Random *_Nonnull rng, const Mono_Time *_Nonnull mono_time, DHT *_Nonnull dht); +Forwarding *_Nullable new_forwarding(const Logger *_Nonnull log, const Memory *_Nonnull mem, const Random *_Nonnull rng, const Mono_Time *_Nonnull mono_time, DHT *_Nonnull dht, + Networking_Core *_Nonnull net); void kill_forwarding(Forwarding *_Nullable forwarding); #ifdef __cplusplus diff --git a/toxcore/friend_connection.c b/toxcore/friend_connection.c index e3464e5e6d..bab78f8742 100644 --- a/toxcore/friend_connection.c +++ b/toxcore/friend_connection.c @@ -71,6 +71,7 @@ struct Friend_Connections { const Mono_Time *mono_time; const Memory *mem; const Logger *logger; + Networking_Core *net; Net_Crypto *net_crypto; DHT *dht; Broadcast_Info *broadcast; @@ -896,7 +897,8 @@ int send_friend_request_packet(Friend_Connections *fr_c, int friendcon_id, uint3 /** Create new friend_connections instance. */ Friend_Connections *new_friend_connections( const Logger *logger, const Memory *mem, const Mono_Time *mono_time, const Network *ns, - Onion_Client *onion_c, bool local_discovery_enabled) + Onion_Client *onion_c, DHT *dht, Net_Crypto *net_crypto, Networking_Core *net, + bool local_discovery_enabled) { if (onion_c == nullptr) { return nullptr; @@ -922,8 +924,9 @@ Friend_Connections *new_friend_connections( temp->mono_time = mono_time; temp->mem = mem; temp->logger = logger; - temp->dht = onion_get_dht(onion_c); - temp->net_crypto = onion_get_net_crypto(onion_c); + temp->dht = dht; + temp->net = net; + temp->net_crypto = net_crypto; temp->onion_c = onion_c; // Don't include default port in port range temp->next_lan_port = TOX_PORTRANGE_FROM + 1; @@ -942,12 +945,12 @@ static void lan_discovery(Friend_Connections *_Nonnull fr_c) last = last > TOX_PORTRANGE_TO ? TOX_PORTRANGE_TO : last; // Always send to default port - lan_discovery_send(dht_get_net(fr_c->dht), fr_c->broadcast, dht_get_self_public_key(fr_c->dht), + lan_discovery_send(fr_c->net, fr_c->broadcast, 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(dht_get_net(fr_c->dht), fr_c->broadcast, dht_get_self_public_key(fr_c->dht), net_htons(port)); + lan_discovery_send(fr_c->net, fr_c->broadcast, dht_get_self_public_key(fr_c->dht), net_htons(port)); } // Don't include default port in port range diff --git a/toxcore/friend_connection.h b/toxcore/friend_connection.h index 8ab02ee2aa..a6d09f0e2c 100644 --- a/toxcore/friend_connection.h +++ b/toxcore/friend_connection.h @@ -145,7 +145,8 @@ void set_friend_request_callback(Friend_Connections *_Nonnull fr_c, fr_request_c /** Create new friend_connections instance. */ Friend_Connections *_Nullable new_friend_connections(const Logger *_Nonnull logger, const Memory *_Nonnull mem, const Mono_Time *_Nonnull mono_time, const Network *_Nonnull ns, - Onion_Client *_Nonnull onion_c, bool local_discovery_enabled); + Onion_Client *_Nonnull onion_c, DHT *_Nonnull dht, Net_Crypto *_Nonnull net_crypto, Networking_Core *_Nonnull net, + bool local_discovery_enabled); /** main friend_connections loop. */ void do_friend_connections(Friend_Connections *_Nonnull fr_c, void *_Nonnull userdata); diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 6815a13ab6..59ca4601b2 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -137,6 +137,7 @@ struct Net_Crypto { const Random *rng; Mono_Time *mono_time; const Network *ns; + Networking_Core *net; DHT *dht; TCP_Connections *tcp_c; @@ -176,11 +177,6 @@ TCP_Connections *nc_get_tcp_c(const Net_Crypto *c) return c->tcp_c; } -DHT *nc_get_dht(const Net_Crypto *c) -{ - return c->dht; -} - static bool crypt_connection_id_is_valid(const Net_Crypto *_Nonnull c, int crypt_connection_id) { if ((uint32_t)crypt_connection_id >= c->crypto_connections_length) { @@ -366,7 +362,7 @@ static int udp_handle_cookie_request(void *_Nonnull object, const IP_Port *_Nonn return 1; } - if ((uint32_t)sendpacket(dht_get_net(c->dht), source, data, sizeof(data)) != sizeof(data)) { + if ((uint32_t)sendpacket(c->net, source, data, sizeof(data)) != sizeof(data)) { return 1; } @@ -676,7 +672,7 @@ static int send_packet_to(const Net_Crypto *_Nonnull c, int crypt_connection_id, crypto_connection_status(c, crypt_connection_id, &direct_connected, nullptr); if (direct_connected) { - if ((uint32_t)sendpacket(dht_get_net(c->dht), &ip_port, data, length) == length) { + if ((uint32_t)sendpacket(c->net, &ip_port, data, length) == length) { return 0; } @@ -689,7 +685,7 @@ static int send_packet_to(const Net_Crypto *_Nonnull c, int crypt_connection_id, if ((((UDP_DIRECT_TIMEOUT / 2) + conn->direct_send_attempt_time) < current_time && length < 96) || data[0] == NET_PACKET_COOKIE_REQUEST || data[0] == NET_PACKET_CRYPTO_HS) { - if ((uint32_t)sendpacket(dht_get_net(c->dht), &ip_port, data, length) == length) { + if ((uint32_t)sendpacket(c->net, &ip_port, data, length) == length) { direct_send_attempt = true; conn->direct_send_attempt_time = mono_time_get(c->mono_time); } @@ -2915,7 +2911,7 @@ void load_secret_key(Net_Crypto *c, const uint8_t *sk) * Sets all the global connection variables to their default values. */ Net_Crypto *new_net_crypto(const Logger *log, const Memory *mem, const Random *rng, const Network *ns, - Mono_Time *mono_time, DHT *dht, const TCP_Proxy_Info *proxy_info, Net_Profile *tcp_np) + Mono_Time *mono_time, Networking_Core *net, DHT *dht, const TCP_Proxy_Info *proxy_info, Net_Profile *tcp_np) { if (dht == nullptr) { return nullptr; @@ -2932,6 +2928,7 @@ Net_Crypto *new_net_crypto(const Logger *log, const Memory *mem, const Random *r temp->rng = rng; temp->mono_time = mono_time; temp->ns = ns; + temp->net = net; temp->tcp_c = new_tcp_connections(log, mem, rng, ns, mono_time, dht_get_self_secret_key(dht), proxy_info, tcp_np); @@ -2950,10 +2947,10 @@ Net_Crypto *new_net_crypto(const Logger *log, const Memory *mem, const Random *r temp->current_sleep_time = CRYPTO_SEND_PACKET_INTERVAL; - networking_registerhandler(dht_get_net(dht), NET_PACKET_COOKIE_REQUEST, &udp_handle_cookie_request, temp); - networking_registerhandler(dht_get_net(dht), NET_PACKET_COOKIE_RESPONSE, &udp_handle_packet, temp); - networking_registerhandler(dht_get_net(dht), NET_PACKET_CRYPTO_HS, &udp_handle_packet, temp); - networking_registerhandler(dht_get_net(dht), NET_PACKET_CRYPTO_DATA, &udp_handle_packet, temp); + networking_registerhandler(net, NET_PACKET_COOKIE_REQUEST, &udp_handle_cookie_request, temp); + networking_registerhandler(net, NET_PACKET_COOKIE_RESPONSE, &udp_handle_packet, temp); + networking_registerhandler(net, NET_PACKET_CRYPTO_HS, &udp_handle_packet, temp); + networking_registerhandler(net, NET_PACKET_CRYPTO_DATA, &udp_handle_packet, temp); bs_list_init(&temp->ip_port_list, mem, sizeof(IP_Port), 8, ipport_cmp_handler); @@ -3016,10 +3013,10 @@ void kill_net_crypto(Net_Crypto *c) kill_tcp_connections(c->tcp_c); bs_list_free(&c->ip_port_list); - networking_registerhandler(dht_get_net(c->dht), NET_PACKET_COOKIE_REQUEST, nullptr, nullptr); - networking_registerhandler(dht_get_net(c->dht), NET_PACKET_COOKIE_RESPONSE, nullptr, nullptr); - networking_registerhandler(dht_get_net(c->dht), NET_PACKET_CRYPTO_HS, nullptr, nullptr); - networking_registerhandler(dht_get_net(c->dht), NET_PACKET_CRYPTO_DATA, nullptr, nullptr); + networking_registerhandler(c->net, NET_PACKET_COOKIE_REQUEST, nullptr, nullptr); + networking_registerhandler(c->net, NET_PACKET_COOKIE_RESPONSE, nullptr, nullptr); + networking_registerhandler(c->net, NET_PACKET_CRYPTO_HS, nullptr, nullptr); + networking_registerhandler(c->net, NET_PACKET_CRYPTO_DATA, nullptr, nullptr); crypto_memzero(c, sizeof(Net_Crypto)); mem_delete(mem, c); } diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h index dc93d6585a..f3270ad8ac 100644 --- a/toxcore/net_crypto.h +++ b/toxcore/net_crypto.h @@ -126,7 +126,6 @@ typedef struct Net_Crypto Net_Crypto; const uint8_t *_Nonnull nc_get_self_public_key(const Net_Crypto *_Nonnull c); const uint8_t *_Nonnull nc_get_self_secret_key(const Net_Crypto *_Nonnull c); TCP_Connections *_Nonnull nc_get_tcp_c(const Net_Crypto *_Nonnull c); -DHT *_Nonnull nc_get_dht(const Net_Crypto *_Nonnull c); typedef struct New_Connection { IP_Port source; @@ -369,7 +368,8 @@ void load_secret_key(Net_Crypto *_Nonnull c, const uint8_t *_Nonnull sk); /** @brief Create new instance of Net_Crypto. * Sets all the global connection variables to their default values. */ -Net_Crypto *_Nullable new_net_crypto(const Logger *_Nonnull log, const Memory *_Nonnull mem, const Random *_Nonnull rng, const Network *_Nonnull ns, Mono_Time *_Nonnull mono_time, DHT *_Nonnull dht, +Net_Crypto *_Nullable new_net_crypto(const Logger *_Nonnull log, const Memory *_Nonnull mem, const Random *_Nonnull rng, const Network *_Nonnull ns, Mono_Time *_Nonnull mono_time, + Networking_Core *_Nonnull net, DHT *_Nonnull dht, const TCP_Proxy_Info *_Nonnull proxy_info, Net_Profile *_Nonnull tcp_np); /** return the optimal interval in ms for running do_net_crypto. */ diff --git a/toxcore/net_crypto_fuzz_test.cc b/toxcore/net_crypto_fuzz_test.cc index 96ee86fca0..01ff12c242 100644 --- a/toxcore/net_crypto_fuzz_test.cc +++ b/toxcore/net_crypto_fuzz_test.cc @@ -76,7 +76,7 @@ void TestNetCrypto(Fuzz_Data &input) const Ptr net_crypto( new_net_crypto(logger.get(), sys.mem.get(), sys.rng.get(), sys.ns.get(), mono_time.get(), - dht.get(), &proxy_info, tcp_np), + net.get(), dht.get(), &proxy_info, tcp_np), kill_net_crypto); if (net_crypto == nullptr) { netprof_kill(sys.mem.get(), tcp_np); diff --git a/toxcore/onion.c b/toxcore/onion.c index f5738dfb89..9452bd2c49 100644 --- a/toxcore/onion.c +++ b/toxcore/onion.c @@ -702,7 +702,7 @@ void set_callback_handle_recv_1(Onion *onion, onion_recv_1_cb *function, void *o onion->callback_object = object; } -Onion *new_onion(const Logger *log, const Memory *mem, const Mono_Time *mono_time, const Random *rng, DHT *dht) +Onion *new_onion(const Logger *log, const Memory *mem, const Mono_Time *mono_time, const Random *rng, DHT *dht, Networking_Core *net) { if (dht == nullptr) { return nullptr; @@ -716,7 +716,7 @@ Onion *new_onion(const Logger *log, const Memory *mem, const Mono_Time *mono_tim onion->log = log; onion->dht = dht; - onion->net = dht_get_net(dht); + onion->net = net; onion->mono_time = mono_time; onion->rng = rng; onion->mem = mem; diff --git a/toxcore/onion.h b/toxcore/onion.h index 355a41f6b7..6c967ba8ce 100644 --- a/toxcore/onion.h +++ b/toxcore/onion.h @@ -140,7 +140,7 @@ int onion_send_1(const Onion *_Nonnull onion, const uint8_t *_Nonnull plain, uin /** Set the callback to be called when the dest ip_port doesn't have TOX_AF_INET6 or TOX_AF_INET as the family. */ void set_callback_handle_recv_1(Onion *_Nonnull onion, onion_recv_1_cb *_Nullable function, void *_Nullable object); -Onion *_Nullable new_onion(const Logger *_Nonnull log, const Memory *_Nonnull mem, const Mono_Time *_Nonnull mono_time, const Random *_Nonnull rng, DHT *_Nonnull dht); +Onion *_Nullable new_onion(const Logger *_Nonnull log, const Memory *_Nonnull mem, const Mono_Time *_Nonnull mono_time, const Random *_Nonnull rng, DHT *_Nonnull dht, Networking_Core *_Nonnull net); void kill_onion(Onion *_Nullable onion); #endif /* C_TOXCORE_TOXCORE_ONION_H */ diff --git a/toxcore/onion_announce.c b/toxcore/onion_announce.c index 6c80aa96de..f752506cd7 100644 --- a/toxcore/onion_announce.c +++ b/toxcore/onion_announce.c @@ -680,7 +680,8 @@ static int handle_data_request(void *_Nonnull object, const IP_Port *_Nonnull so return 0; } -Onion_Announce *new_onion_announce(const Logger *log, const Memory *mem, const Random *rng, const Mono_Time *mono_time, DHT *dht) +Onion_Announce *new_onion_announce(const Logger *log, const Memory *mem, const Random *rng, const Mono_Time *mono_time, DHT *dht, + Networking_Core *net) { if (dht == nullptr) { return nullptr; @@ -697,7 +698,7 @@ Onion_Announce *new_onion_announce(const Logger *log, const Memory *mem, const R onion_a->mem = mem; onion_a->mono_time = mono_time; onion_a->dht = dht; - onion_a->net = dht_get_net(dht); + onion_a->net = net; onion_a->extra_data_max_size = 0; onion_a->extra_data_callback = nullptr; onion_a->extra_data_object = nullptr; diff --git a/toxcore/onion_announce.h b/toxcore/onion_announce.h index a7e0f0658d..fca6798c34 100644 --- a/toxcore/onion_announce.h +++ b/toxcore/onion_announce.h @@ -124,7 +124,8 @@ typedef int pack_extra_data_cb(void *_Nonnull object, const Logger *_Nonnull log void onion_announce_extra_data_callback(Onion_Announce *_Nonnull onion_a, uint16_t extra_data_max_size, pack_extra_data_cb *_Nonnull extra_data_callback, void *_Nonnull extra_data_object); -Onion_Announce *_Nullable new_onion_announce(const Logger *_Nonnull log, const Memory *_Nonnull mem, const Random *_Nonnull rng, const Mono_Time *_Nonnull mono_time, DHT *_Nonnull dht); +Onion_Announce *_Nullable new_onion_announce(const Logger *_Nonnull log, const Memory *_Nonnull mem, const Random *_Nonnull rng, const Mono_Time *_Nonnull mono_time, DHT *_Nonnull dht, + Networking_Core *_Nonnull net); void kill_onion_announce(Onion_Announce *_Nullable onion_a); #endif /* C_TOXCORE_TOXCORE_ONION_ANNOUNCE_H */ diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index 4c50925b99..b47a6522a1 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c @@ -203,16 +203,6 @@ bool onion_friend_is_groupchat(const Onion_Friend *const onion_friend) return onion_friend->is_groupchat; } -DHT *onion_get_dht(const Onion_Client *onion_c) -{ - return onion_c->dht; -} - -Net_Crypto *onion_get_net_crypto(const Onion_Client *onion_c) -{ - return onion_c->c; -} - /** @brief Add a node to the path_nodes bootstrap array. * * If a node with the given public key was already in the bootstrap array, this function has no @@ -2169,7 +2159,8 @@ void do_onion_client(Onion_Client *onion_c) onion_c->last_run = mono_time_get(onion_c->mono_time); } -Onion_Client *new_onion_client(const Logger *logger, const Memory *mem, const Random *rng, const Mono_Time *mono_time, Net_Crypto *c) +Onion_Client *new_onion_client(const Logger *logger, const Memory *mem, const Random *rng, const Mono_Time *mono_time, Net_Crypto *c, + DHT *dht, Networking_Core *net) { if (c == nullptr) { return nullptr; @@ -2192,8 +2183,8 @@ Onion_Client *new_onion_client(const Logger *logger, const Memory *mem, const Ra onion_c->logger = logger; onion_c->rng = rng; onion_c->mem = mem; - onion_c->dht = nc_get_dht(c); - onion_c->net = dht_get_net(onion_c->dht); + onion_c->dht = dht; + onion_c->net = net; onion_c->c = c; new_symmetric_key(rng, onion_c->secret_symmetric_key); crypto_new_keypair(rng, onion_c->temp_public_key, onion_c->temp_secret_key); diff --git a/toxcore/onion_client.h b/toxcore/onion_client.h index bdcaf3553c..a61e9ac774 100644 --- a/toxcore/onion_client.h +++ b/toxcore/onion_client.h @@ -21,7 +21,6 @@ #include "net_crypto.h" #include "network.h" #include "onion_announce.h" -#include "ping_array.h" #define MAX_ONION_CLIENTS 8 #define MAX_ONION_CLIENTS_ANNOUNCE 12 // Number of nodes to announce ourselves to. @@ -66,9 +65,6 @@ typedef struct Onion_Client Onion_Client; -DHT *_Nonnull onion_get_dht(const Onion_Client *_Nonnull onion_c); -Net_Crypto *_Nonnull onion_get_net_crypto(const Onion_Client *_Nonnull onion_c); - /** @brief Add a node to the path_nodes bootstrap array. * * If a node with the given public key was already in the bootstrap array, this function has no @@ -195,7 +191,8 @@ typedef bool onion_group_announce_cb(Onion_Client *_Nonnull onion_c, uint32_t se void onion_group_announce_register(Onion_Client *_Nonnull onion_c, onion_group_announce_cb *_Nullable func, void *_Nullable user_data); void do_onion_client(Onion_Client *_Nonnull onion_c); -Onion_Client *_Nullable new_onion_client(const Logger *_Nonnull logger, const Memory *_Nonnull mem, const Random *_Nonnull rng, const Mono_Time *_Nonnull mono_time, Net_Crypto *_Nonnull c); +Onion_Client *_Nullable new_onion_client(const Logger *_Nonnull logger, const Memory *_Nonnull mem, const Random *_Nonnull rng, const Mono_Time *_Nonnull mono_time, Net_Crypto *_Nonnull c, + DHT *_Nonnull dht, Networking_Core *_Nonnull net); void kill_onion_client(Onion_Client *_Nullable onion_c); typedef enum Onion_Connection_Status { diff --git a/toxcore/ping.c b/toxcore/ping.c index 13e4433534..910a0a6f37 100644 --- a/toxcore/ping.c +++ b/toxcore/ping.c @@ -33,6 +33,7 @@ struct Ping { const Random *rng; const Memory *mem; DHT *dht; + Networking_Core *net; Ping_Array *ping_array; Node_format to_ping[MAX_TO_PING]; @@ -83,7 +84,7 @@ void ping_send_request(Ping *ping, const IP_Port *ipp, const uint8_t *public_key } // We never check this return value and failures in sendpacket are already logged - sendpacket(dht_get_net(ping->dht), ipp, pk, sizeof(pk)); + sendpacket(ping->net, ipp, pk, sizeof(pk)); } static int ping_send_response(const Ping *_Nonnull ping, const IP_Port *_Nonnull ipp, const uint8_t *_Nonnull public_key, uint64_t ping_id, const uint8_t *_Nonnull shared_encryption_key) @@ -112,7 +113,7 @@ static int ping_send_response(const Ping *_Nonnull ping, const IP_Port *_Nonnull return 1; } - return sendpacket(dht_get_net(ping->dht), ipp, pk, sizeof(pk)); + return sendpacket(ping->net, ipp, pk, sizeof(pk)); } static int handle_ping_request(void *_Nonnull object, const IP_Port *_Nonnull source, const uint8_t *_Nonnull packet, uint16_t length, void *_Nonnull userdata) @@ -324,7 +325,7 @@ void ping_iterate(Ping *ping) } } -Ping *ping_new(const Memory *mem, const Mono_Time *mono_time, const Random *rng, DHT *dht) +Ping *ping_new(const Memory *mem, const Mono_Time *mono_time, const Random *rng, DHT *dht, Networking_Core *net) { Ping *ping = (Ping *)mem_alloc(mem, sizeof(Ping)); @@ -343,8 +344,9 @@ Ping *ping_new(const Memory *mem, const Mono_Time *mono_time, const Random *rng, ping->rng = rng; ping->mem = mem; ping->dht = dht; - networking_registerhandler(dht_get_net(ping->dht), NET_PACKET_PING_REQUEST, &handle_ping_request, dht); - networking_registerhandler(dht_get_net(ping->dht), NET_PACKET_PING_RESPONSE, &handle_ping_response, dht); + ping->net = net; + networking_registerhandler(ping->net, NET_PACKET_PING_REQUEST, &handle_ping_request, dht); + networking_registerhandler(ping->net, NET_PACKET_PING_RESPONSE, &handle_ping_response, dht); return ping; } @@ -355,8 +357,8 @@ void ping_kill(const Memory *mem, Ping *ping) return; } - networking_registerhandler(dht_get_net(ping->dht), NET_PACKET_PING_REQUEST, nullptr, nullptr); - networking_registerhandler(dht_get_net(ping->dht), NET_PACKET_PING_RESPONSE, nullptr, nullptr); + networking_registerhandler(ping->net, NET_PACKET_PING_REQUEST, nullptr, nullptr); + networking_registerhandler(ping->net, NET_PACKET_PING_RESPONSE, nullptr, nullptr); ping_array_kill(ping->ping_array); mem_delete(mem, ping); diff --git a/toxcore/ping.h b/toxcore/ping.h index 90e9cfa8e9..0ed705b75d 100644 --- a/toxcore/ping.h +++ b/toxcore/ping.h @@ -21,7 +21,7 @@ typedef struct Ping Ping; -Ping *_Nullable ping_new(const Memory *_Nonnull mem, const Mono_Time *_Nonnull mono_time, const Random *_Nonnull rng, DHT *_Nonnull dht); +Ping *_Nullable ping_new(const Memory *_Nonnull mem, const Mono_Time *_Nonnull mono_time, const Random *_Nonnull rng, DHT *_Nonnull dht, Networking_Core *_Nonnull net); void ping_kill(const Memory *_Nonnull mem, Ping *_Nullable ping); /** @brief Add nodes to the to_ping list.