diff --git a/auto_tests/network_test.c b/auto_tests/network_test.c index 4f60ecfd62..f79d86e365 100644 --- a/auto_tests/network_test.c +++ b/auto_tests/network_test.c @@ -20,12 +20,15 @@ static void test_addr_resolv_localhost(void) errno = 0; #endif + const Network *ns = system_network(); + ck_assert(ns != nullptr); + const char localhost[] = "localhost"; IP ip; ip_init(&ip, 0); // ipv6enabled = 0 - bool res = addr_resolve_or_parse_ip(localhost, &ip, nullptr); + bool res = addr_resolve_or_parse_ip(ns, localhost, &ip, nullptr); int error = net_error(); char *strerror = net_new_strerror(error); @@ -39,14 +42,14 @@ static void test_addr_resolv_localhost(void) ip_ntoa(&ip, ip_str, sizeof(ip_str))); ip_init(&ip, 1); // ipv6enabled = 1 - res = addr_resolve_or_parse_ip(localhost, &ip, nullptr); + res = addr_resolve_or_parse_ip(ns, localhost, &ip, nullptr); #if USE_IPV6 int localhost_split = 0; if (!net_family_is_ipv6(ip.family)) { - res = addr_resolve_or_parse_ip("ip6-localhost", &ip, nullptr); + res = addr_resolve_or_parse_ip(ns, "ip6-localhost", &ip, nullptr); localhost_split = 1; } @@ -72,7 +75,7 @@ static void test_addr_resolv_localhost(void) ip.family = net_family_unspec; IP extra; ip_reset(&extra); - res = addr_resolve_or_parse_ip(localhost, &ip, &extra); + res = addr_resolve_or_parse_ip(ns, localhost, &ip, &extra); error = net_error(); strerror = net_new_strerror(error); ck_assert_msg(res, "Resolver failed: %d, %s", error, strerror); diff --git a/auto_tests/onion_test.c b/auto_tests/onion_test.c index 586c1accfb..dbb8d344b0 100644 --- a/auto_tests/onion_test.c +++ b/auto_tests/onion_test.c @@ -180,15 +180,16 @@ static void send_onion_packet(const Networking_Core *net, const Onion_Path *path /** Initialize networking. * Added for reverse compatibility with old new_networking calls. */ -static Networking_Core *new_networking(const Logger *log, const IP *ip, uint16_t port) +static Networking_Core *new_networking(const Logger *log, const Network *ns, const IP *ip, uint16_t port) { - const Network *ns = system_network(); return new_networking_ex(log, ns, ip, port, port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM), nullptr); } static void test_basic(void) { uint32_t index[] = { 1, 2, 3 }; + const Network *ns = system_network(); + Logger *log1 = logger_new(); logger_callback_log(log1, (logger_cb *)print_debug_log, nullptr, &index[0]); Logger *log2 = logger_new(); @@ -198,8 +199,8 @@ static void test_basic(void) Mono_Time *mono_time2 = mono_time_new(); IP ip = get_loopback(); - Onion *onion1 = new_onion(log1, mono_time1, new_dht(log1, mono_time1, new_networking(log1, &ip, 36567), true, false)); - Onion *onion2 = new_onion(log2, mono_time2, new_dht(log2, mono_time2, new_networking(log2, &ip, 36568), true, false)); + Onion *onion1 = new_onion(log1, mono_time1, new_dht(log1, ns, mono_time1, new_networking(log1, ns, &ip, 36567), true, false)); + Onion *onion2 = new_onion(log2, mono_time2, new_dht(log2, ns, mono_time2, new_networking(log2, ns, &ip, 36568), true, false)); ck_assert_msg((onion1 != nullptr) && (onion2 != nullptr), "Onion failed initializing."); networking_registerhandler(onion2->net, NET_PACKET_ANNOUNCE_REQUEST_OLD, &handle_test_1, onion2); @@ -292,7 +293,7 @@ static void test_basic(void) Mono_Time *mono_time3 = mono_time_new(); - Onion *onion3 = new_onion(log3, mono_time3, new_dht(log3, mono_time3, new_networking(log3, &ip, 36569), true, false)); + Onion *onion3 = new_onion(log3, mono_time3, new_dht(log3, ns, mono_time3, new_networking(log3, ns, &ip, 36569), true, false)); ck_assert_msg((onion3 != nullptr), "Onion failed initializing."); random_nonce(nonce); @@ -361,6 +362,7 @@ static Onions *new_onions(uint16_t port, uint32_t *index) { IP ip = get_loopback(); ip.ip.v6.uint8[15] = 1; + const Network *ns = system_network(); Onions *on = (Onions *)malloc(sizeof(Onions)); if (!on) { @@ -384,7 +386,7 @@ static Onions *new_onions(uint16_t port, uint32_t *index) return nullptr; } - Networking_Core *net = new_networking(on->log, &ip, port); + Networking_Core *net = new_networking(on->log, ns, &ip, port); if (!net) { mono_time_free(on->mono_time); @@ -393,7 +395,7 @@ static Onions *new_onions(uint16_t port, uint32_t *index) return nullptr; } - DHT *dht = new_dht(on->log, on->mono_time, net, true, false); + DHT *dht = new_dht(on->log, ns, on->mono_time, net, true, false); if (!dht) { kill_networking(net); @@ -427,7 +429,6 @@ static Onions *new_onions(uint16_t port, uint32_t *index) } TCP_Proxy_Info inf = {{{{0}}}}; - const Network *ns = system_network(); on->onion_c = new_onion_client(on->log, on->mono_time, new_net_crypto(on->log, on->mono_time, ns, dht, &inf)); if (!on->onion_c) { diff --git a/other/DHT_bootstrap.c b/other/DHT_bootstrap.c index b9f8c679c1..669ea327d1 100644 --- a/other/DHT_bootstrap.c +++ b/other/DHT_bootstrap.c @@ -146,7 +146,7 @@ int main(int argc, char *argv[]) const uint16_t start_port = PORT; const uint16_t end_port = start_port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM); const Network *ns = system_network(); - DHT *dht = new_dht(logger, mono_time, new_networking_ex(logger, ns, &ip, start_port, end_port, nullptr), true, true); + DHT *dht = new_dht(logger, ns, mono_time, new_networking_ex(logger, ns, &ip, start_port, end_port, nullptr), true, true); Onion *onion = new_onion(logger, mono_time, dht); const Onion_Announce *onion_a = new_onion_announce(logger, mono_time, dht); diff --git a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 index a5e6ced4c6..154877deca 100644 --- a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 +++ b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 @@ -1 +1 @@ -073710c9592554bbcb8bd094c9d64a987d52a2d1dedf965557b56fe848ddc14b /usr/local/bin/tox-bootstrapd +c40a0017ec22042f0be897893a774b89a6b6fbb621ac4d7f68416134fe4a78a2 /usr/local/bin/tox-bootstrapd diff --git a/other/bootstrap_daemon/src/tox-bootstrapd.c b/other/bootstrap_daemon/src/tox-bootstrapd.c index dbee571ab8..28afa4a5ee 100644 --- a/other/bootstrap_daemon/src/tox-bootstrapd.c +++ b/other/bootstrap_daemon/src/tox-bootstrapd.c @@ -320,7 +320,7 @@ int main(int argc, char *argv[]) mono_time_update(mono_time); - DHT *const dht = new_dht(logger, mono_time, net, true, enable_lan_discovery); + DHT *const dht = new_dht(logger, ns, mono_time, net, true, enable_lan_discovery); if (dht == nullptr) { log_write(LOG_LEVEL_ERROR, "Couldn't initialize Tox DHT instance. Exiting.\n"); diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 5567d3866f..55c65f1253 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -78,6 +78,7 @@ typedef struct Cryptopacket_Handler { struct DHT { const Logger *log; + const Network *ns; Mono_Time *mono_time; Networking_Core *net; @@ -1910,7 +1911,7 @@ int dht_bootstrap_from_address(DHT *dht, const char *address, bool ipv6enabled, ip_extra = &ip_port_v4.ip; } - if (addr_resolve_or_parse_ip(address, &ip_port_v64.ip, ip_extra)) { + if (addr_resolve_or_parse_ip(dht->ns, address, &ip_port_v64.ip, ip_extra)) { ip_port_v64.port = port; dht_bootstrap(dht, &ip_port_v64, public_key); @@ -2568,8 +2569,8 @@ static int handle_LANdiscovery(void *object, const IP_Port *source, const uint8_ /*----------------------------------------------------------------------------------*/ -DHT *new_dht(const Logger *log, Mono_Time *mono_time, Networking_Core *net, bool hole_punching_enabled, - bool lan_discovery_enabled) +DHT *new_dht(const Logger *log, const Network *ns, Mono_Time *mono_time, Networking_Core *net, + bool hole_punching_enabled, bool lan_discovery_enabled) { if (net == nullptr) { return nullptr; @@ -2581,6 +2582,7 @@ DHT *new_dht(const Logger *log, Mono_Time *mono_time, Networking_Core *net, bool return nullptr; } + dht->ns = ns; dht->mono_time = mono_time; dht->cur_time = mono_time_get(mono_time); dht->log = log; diff --git a/toxcore/DHT.h b/toxcore/DHT.h index 9c732f48db..3d426fb6a7 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h @@ -470,8 +470,8 @@ int dht_load(DHT *dht, const uint8_t *data, uint32_t length); /** Initialize DHT. */ non_null() -DHT *new_dht(const Logger *log, Mono_Time *mono_time, Networking_Core *net, bool hole_punching_enabled, - bool lan_discovery_enabled); +DHT *new_dht(const Logger *log, const Network *ns, Mono_Time *mono_time, Networking_Core *net, + bool hole_punching_enabled, bool lan_discovery_enabled); non_null() void kill_dht(DHT *dht); diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 3a67bbcb67..d9ae875f27 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -3196,7 +3196,7 @@ Messenger *new_messenger(Mono_Time *mono_time, const Network *ns, Messenger_Opti } if (options->udp_disabled) { - m->net = new_networking_no_udp(m->log); + m->net = new_networking_no_udp(m->log, m->ns); } else { IP ip; ip_init(&ip, options->ipv6enabled); @@ -3215,7 +3215,7 @@ Messenger *new_messenger(Mono_Time *mono_time, const Network *ns, Messenger_Opti return nullptr; } - m->dht = new_dht(m->log, m->mono_time, m->net, options->hole_punching_enabled, options->local_discovery_enabled); + m->dht = new_dht(m->log, m->ns, m->mono_time, m->net, options->hole_punching_enabled, options->local_discovery_enabled); if (m->dht == nullptr) { kill_networking(m->net); diff --git a/toxcore/network.c b/toxcore/network.c index 64ab80fb74..bab07d56cf 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -1271,7 +1271,7 @@ Networking_Core *new_networking_ex( return nullptr; } -Networking_Core *new_networking_no_udp(const Logger *log) +Networking_Core *new_networking_no_udp(const Logger *log, const Network *ns) { if (networking_at_startup() != 0) { return nullptr; @@ -1285,6 +1285,7 @@ Networking_Core *new_networking_no_udp(const Logger *log) } net->log = log; + net->ns = ns; return net; } @@ -1536,8 +1537,8 @@ bool addr_parse_ip(const char *address, IP *to) * * @return 0 on failure, `TOX_ADDR_RESOLVE_*` on success. */ -non_null(1, 2) nullable(3) -static int addr_resolve(const char *address, IP *to, IP *extra) +non_null(1, 2, 3) nullable(4) +static int addr_resolve(const Network *ns, const char *address, IP *to, IP *extra) { #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION return 0; @@ -1633,9 +1634,9 @@ static int addr_resolve(const char *address, IP *to, IP *extra) #endif } -bool addr_resolve_or_parse_ip(const char *address, IP *to, IP *extra) +bool addr_resolve_or_parse_ip(const Network *ns, const char *address, IP *to, IP *extra) { - if (addr_resolve(address, to, extra) == 0) { + if (addr_resolve(ns, address, to, extra) == 0) { if (!addr_parse_ip(address, to)) { return false; } diff --git a/toxcore/network.h b/toxcore/network.h index 19526c613d..90bdddfbc1 100644 --- a/toxcore/network.h +++ b/toxcore/network.h @@ -412,8 +412,8 @@ void ipport_copy(IP_Port *target, const IP_Port *source); * * @return true on success, false on failure */ -non_null(1, 2) nullable(3) -bool addr_resolve_or_parse_ip(const char *address, IP *to, IP *extra); +non_null(1, 2, 3) nullable(4) +bool addr_resolve_or_parse_ip(const Network *ns, const char *address, IP *to, IP *extra); /** @brief Function to receive data, ip and port of sender is put into ip_port. * Packet data is put into data. @@ -582,7 +582,7 @@ Networking_Core *new_networking_ex( const Logger *log, const Network *ns, const IP *ip, uint16_t port_from, uint16_t port_to, unsigned int *error); non_null() -Networking_Core *new_networking_no_udp(const Logger *log); +Networking_Core *new_networking_no_udp(const Logger *log, const Network *ns); /** Function to cleanup networking stuff (doesn't do much right now). */ non_null() diff --git a/toxcore/tox.c b/toxcore/tox.c index d5a4ae995b..31f31987e3 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -520,6 +520,8 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) } } + tox_set_network(tox, nullptr); + if (m_options.proxy_info.proxy_type != TCP_PROXY_NONE) { if (tox_options_get_proxy_port(opts) == 0) { SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PROXY_BAD_PORT); @@ -536,7 +538,7 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) const char *const proxy_host = tox_options_get_proxy_host(opts); - if (proxy_host == nullptr || !addr_resolve_or_parse_ip(proxy_host, &m_options.proxy_info.ip_port.ip, nullptr)) { + if (proxy_host == nullptr || !addr_resolve_or_parse_ip(&tox->ns, proxy_host, &m_options.proxy_info.ip_port.ip, nullptr)) { SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PROXY_BAD_HOST); // TODO(irungentoo): TOX_ERR_NEW_PROXY_NOT_FOUND if domain. tox_options_free(default_options); @@ -548,7 +550,6 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) } tox->mono_time = mono_time_new(); - tox_set_network(tox, nullptr); if (tox->mono_time == nullptr) { SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC);