From 5536cb7687f358f0f666869b289feb8192cec10f Mon Sep 17 00:00:00 2001 From: iphydf Date: Thu, 3 Mar 2022 23:05:39 +0000 Subject: [PATCH] cleanup: Move test-only functions into tests. Also inlined `new_networking` in most places. --- auto_tests/onion_test.c | 32 +++++++++++++++++++ other/DHT_bootstrap.c | 2 +- .../docker/tox-bootstrapd.sha256 | 2 +- other/bootstrap_daemon/src/tox-bootstrapd.c | 15 +++++---- toxcore/network.c | 12 ------- toxcore/network.h | 5 --- toxcore/onion.c | 25 --------------- toxcore/onion.h | 14 +------- toxcore/tox_dispatch.c | 3 ++ 9 files changed, 46 insertions(+), 64 deletions(-) diff --git a/auto_tests/onion_test.c b/auto_tests/onion_test.c index 782187aac0a..cbc92a0e0d6 100644 --- a/auto_tests/onion_test.c +++ b/auto_tests/onion_test.c @@ -164,6 +164,38 @@ static int handle_test_4(void *object, const IP_Port *source, const uint8_t *pac return 0; } +/** Create and send a onion packet. + * + * Use Onion_Path path to send data of length to dest. + * Maximum length of data is ONION_MAX_DATA_SIZE. + * + * return -1 on failure. + * return 0 on success. + */ +static int send_onion_packet(const Networking_Core *net, const Onion_Path *path, const IP_Port *dest, const uint8_t *data, uint16_t length) +{ + uint8_t packet[ONION_MAX_PACKET_SIZE]; + const int len = create_onion_packet(packet, sizeof(packet), path, dest, data, length); + + if (len == -1) { + return -1; + } + + if (sendpacket(net, &path->ip_port1, packet, len) != len) { + return -1; + } + + return 0; +} + +/** 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) +{ + return new_networking_ex(log, ip, port, port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM), nullptr); +} + static void test_basic(void) { uint32_t index[] = { 1, 2, 3 }; diff --git a/other/DHT_bootstrap.c b/other/DHT_bootstrap.c index db013d036b2..c735025308c 100644 --- a/other/DHT_bootstrap.c +++ b/other/DHT_bootstrap.c @@ -142,7 +142,7 @@ int main(int argc, char *argv[]) } Mono_Time *mono_time = mono_time_new(); - DHT *dht = new_dht(logger, mono_time, new_networking(logger, &ip, PORT), true); + DHT *dht = new_dht(logger, mono_time, new_networking_ex(logger, &ip, PORT, PORT, nullptr), 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 a7371f21b9a..8d08197c6fb 100644 --- a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 +++ b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 @@ -1 +1 @@ -ac88db3ee0c9ca621846897bb508c469eba97c263637966d74483e266b5564b0 /usr/local/bin/tox-bootstrapd +1f686b142e173522393e1c9722fe939ac751cec3df2a3f7a0513994aea9cf2aa /usr/local/bin/tox-bootstrapd diff --git a/other/bootstrap_daemon/src/tox-bootstrapd.c b/other/bootstrap_daemon/src/tox-bootstrapd.c index 776c01cac1c..297d9e4d568 100644 --- a/other/bootstrap_daemon/src/tox-bootstrapd.c +++ b/other/bootstrap_daemon/src/tox-bootstrapd.c @@ -234,7 +234,7 @@ int main(int argc, char *argv[]) char *pid_file_path = nullptr; char *keys_file_path = nullptr; - int port; + int start_port; int enable_ipv6; int enable_ipv4_fallback; int enable_lan_discovery; @@ -244,7 +244,7 @@ int main(int argc, char *argv[]) int enable_motd; char *motd = nullptr; - if (get_general_config(cfg_file_path, &pid_file_path, &keys_file_path, &port, &enable_ipv6, &enable_ipv4_fallback, + if (get_general_config(cfg_file_path, &pid_file_path, &keys_file_path, &start_port, &enable_ipv6, &enable_ipv4_fallback, &enable_lan_discovery, &enable_tcp_relay, &tcp_relay_ports, &tcp_relay_port_count, &enable_motd, &motd)) { log_write(LOG_LEVEL_INFO, "General config read successfully\n"); } else { @@ -252,8 +252,8 @@ int main(int argc, char *argv[]) return 1; } - if (port < MIN_ALLOWED_PORT || port > MAX_ALLOWED_PORT) { - log_write(LOG_LEVEL_ERROR, "Invalid port: %d, should be in [%d, %d]. Exiting.\n", port, MIN_ALLOWED_PORT, + if (start_port < MIN_ALLOWED_PORT || start_port > MAX_ALLOWED_PORT) { + log_write(LOG_LEVEL_ERROR, "Invalid port: %d, should be in [%d, %d]. Exiting.\n", start_port, MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); free(motd); free(tcp_relay_ports); @@ -277,14 +277,15 @@ int main(int argc, char *argv[]) logger_callback_log(logger, toxcore_logger_callback, nullptr, nullptr); } - Networking_Core *net = new_networking(logger, &ip, port); + const uint16_t end_port = start_port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM); + Networking_Core *net = new_networking_ex(logger, &ip, start_port, end_port, nullptr); if (net == nullptr) { if (enable_ipv6 && enable_ipv4_fallback) { log_write(LOG_LEVEL_WARNING, "Couldn't initialize IPv6 networking. Falling back to using IPv4.\n"); enable_ipv6 = 0; ip_init(&ip, enable_ipv6); - net = new_networking(logger, &ip, port); + net = new_networking_ex(logger, &ip, start_port, end_port, nullptr); if (net == nullptr) { log_write(LOG_LEVEL_ERROR, "Couldn't fallback to IPv4. Exiting.\n"); @@ -470,7 +471,7 @@ int main(int argc, char *argv[]) print_public_key(dht_get_self_public_key(dht)); uint64_t last_LANdiscovery = 0; - const uint16_t net_htons_port = net_htons(port); + const uint16_t net_htons_port = net_htons(start_port); int waiting_for_dht_connection = 1; diff --git a/toxcore/network.c b/toxcore/network.c index 632074f8e4a..87d4802a6bb 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -813,14 +813,6 @@ static void at_shutdown(void) } #endif -/** Initialize networking. - * Added for reverse compatibility with old new_networking calls. - */ -Networking_Core *new_networking(const Logger *log, const IP *ip, uint16_t port) -{ - return new_networking_ex(log, ip, port, port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM), nullptr); -} - /** Initialize networking. * Bind to ip and port. * ip must be in network order EX: 127.0.0.1 = (7F000001). @@ -1280,8 +1272,6 @@ bool ip_parse_addr(const IP *ip, char *address, size_t length) if (net_family_is_ipv4(ip->family)) { struct in_addr addr; - static_assert(sizeof(addr) == sizeof(ip->ip.v4.uint32), - "assumption does not hold: in_addr should be 4 bytes"); assert(make_family(ip->family) == AF_INET); fill_addr4(&ip->ip.v4, &addr); return inet_ntop4(&addr, address, length) != nullptr; @@ -1289,8 +1279,6 @@ bool ip_parse_addr(const IP *ip, char *address, size_t length) if (net_family_is_ipv6(ip->family)) { struct in6_addr addr; - static_assert(sizeof(addr) == sizeof(ip->ip.v6.uint8), - "assumption does not hold: in6_addr should be 16 bytes"); assert(make_family(ip->family) == AF_INET6); fill_addr6(&ip->ip.v6, &addr); return inet_ntop6(&addr, address, length) != nullptr; diff --git a/toxcore/network.h b/toxcore/network.h index c57e8b1249d..1d8939cc68c 100644 --- a/toxcore/network.h +++ b/toxcore/network.h @@ -487,11 +487,6 @@ char *net_new_strerror(int error); non_null() void net_kill_strerror(char *strerror); -/** Initialize networking. - * Added for reverse compatibility with old new_networking calls. - */ -non_null() -Networking_Core *new_networking(const Logger *log, const IP *ip, uint16_t port); /** Initialize networking. * Bind to ip and port. * ip must be in network order EX: 127.0.0.1 = (7F000001). diff --git a/toxcore/onion.c b/toxcore/onion.c index 60de6455cea..4a961242024 100644 --- a/toxcore/onion.c +++ b/toxcore/onion.c @@ -268,31 +268,6 @@ int create_onion_packet_tcp(uint8_t *packet, uint16_t max_packet_length, const O return CRYPTO_NONCE_SIZE + SIZE_IPPORT + CRYPTO_PUBLIC_KEY_SIZE + len; } -/** Create and send a onion packet. - * - * Use Onion_Path path to send data of length to dest. - * Maximum length of data is ONION_MAX_DATA_SIZE. - * - * return -1 on failure. - * return 0 on success. - */ -int send_onion_packet(const Networking_Core *net, const Onion_Path *path, const IP_Port *dest, const uint8_t *data, - uint16_t length) -{ - uint8_t packet[ONION_MAX_PACKET_SIZE]; - const int len = create_onion_packet(packet, sizeof(packet), path, dest, data, length); - - if (len == -1) { - return -1; - } - - if (sendpacket(net, &path->ip_port1, packet, len) != len) { - return -1; - } - - return 0; -} - /** Create and send a onion response sent initially to dest with. * Maximum length of data is ONION_RESPONSE_MAX_DATA_SIZE. * diff --git a/toxcore/onion.h b/toxcore/onion.h index d1741562265..07afc2f4bfc 100644 --- a/toxcore/onion.h +++ b/toxcore/onion.h @@ -115,18 +115,6 @@ non_null() int create_onion_packet_tcp(uint8_t *packet, uint16_t max_packet_length, const Onion_Path *path, const IP_Port *dest, const uint8_t *data, uint16_t length); -/** Create and send a onion packet. - * - * Use Onion_Path path to send data of length to dest. - * Maximum length of data is ONION_MAX_DATA_SIZE. - * - * return -1 on failure. - * return 0 on success. - */ -non_null() -int send_onion_packet(const Networking_Core *net, const Onion_Path *path, const IP_Port *dest, const uint8_t *data, - uint16_t length); - /** Create and send a onion response sent initially to dest with. * Maximum length of data is ONION_RESPONSE_MAX_DATA_SIZE. * @@ -137,7 +125,7 @@ non_null() int send_onion_response(const Networking_Core *net, const IP_Port *dest, const uint8_t *data, uint16_t length, const uint8_t *ret); -/** Function to handle/send received decrypted versions of the packet sent with send_onion_packet. +/** Function to handle/send received decrypted versions of the packet created by create_onion_packet. * * return 0 on success. * return 1 on failure. diff --git a/toxcore/tox_dispatch.c b/toxcore/tox_dispatch.c index 54fac36eb93..4b4546e4a43 100644 --- a/toxcore/tox_dispatch.c +++ b/toxcore/tox_dispatch.c @@ -47,6 +47,9 @@ Tox_Dispatch *tox_dispatch_new(Tox_Err_Dispatch_New *error) *dispatch = (Tox_Dispatch) { nullptr }; + if (error != nullptr) { + *error = TOX_ERR_DISPATCH_NEW_OK; + } return dispatch; }