diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index 71e2351d..76c92f9e 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -13,7 +13,7 @@ jobs: windows: timeout-minutes: 15 - runs-on: windows-latest + runs-on: windows-2019 steps: - uses: actions/checkout@v3 diff --git a/external/toxcore/CMakeLists.txt b/external/toxcore/CMakeLists.txt index fc61af11..93e5246c 100644 --- a/external/toxcore/CMakeLists.txt +++ b/external/toxcore/CMakeLists.txt @@ -164,9 +164,10 @@ configure_file( target_include_directories(toxcore PRIVATE "${TOX_DIR}toxcore") target_include_directories(toxcore PUBLIC "${TOX_DIR}") -target_compile_definitions(toxcore PUBLIC USE_IPV6=1) +#target_compile_definitions(toxcore PUBLIC USE_IPV6=1) +target_compile_definitions(toxcore PUBLIC MIN_LOGGER_LEVEL=LOGGER_LEVEL_TRACE) #target_compile_definitions(toxcore PUBLIC MIN_LOGGER_LEVEL=LOGGER_LEVEL_DEBUG) -target_compile_definitions(toxcore PUBLIC MIN_LOGGER_LEVEL=LOGGER_LEVEL_INFO) +#target_compile_definitions(toxcore PUBLIC MIN_LOGGER_LEVEL=LOGGER_LEVEL_INFO) find_package(unofficial-sodium CONFIG QUIET) find_package(sodium QUIET) diff --git a/external/toxcore/c-toxcore/toxcore/DHT.c b/external/toxcore/c-toxcore/toxcore/DHT.c index 80f477ca..b1c5f019 100644 --- a/external/toxcore/c-toxcore/toxcore/DHT.c +++ b/external/toxcore/c-toxcore/toxcore/DHT.c @@ -979,7 +979,7 @@ static int handle_data_search_response(void *object, const IP_Port *source, uint8_t ping_data[CRYPTO_PUBLIC_KEY_SIZE]; - if (ping_array_check(dht->dht_ping_array, + if (ping_array_check(dht->log, dht->dht_ping_array, dht->mono_time, ping_data, sizeof(ping_data), ping_id) != sizeof(ping_data)) { return 1; @@ -1498,13 +1498,17 @@ static bool sent_getnode_to_node(DHT *dht, const uint8_t *public_key, const IP_P { uint8_t data[sizeof(Node_format) * 2]; - if (ping_array_check(dht->dht_ping_array, dht->mono_time, data, sizeof(data), ping_id) != sizeof(Node_format)) { + int32_t res = ping_array_check(dht->log, dht->dht_ping_array, dht->mono_time, data, sizeof(data), ping_id); + if (res != sizeof(Node_format)) { + LOGGER_ERROR(dht->log, "ping array check failed: %d", res); return false; } Node_format test; + // why? if (unpack_nodes(&test, 1, nullptr, data, sizeof(data), false) != 1) { + LOGGER_ERROR(dht->log, "test unpack failed failed"); return false; } @@ -1519,16 +1523,19 @@ static bool handle_sendnodes_core(void *object, const IP_Port *source, const uin const uint32_t cid_size = 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + 1 + sizeof(uint64_t) + CRYPTO_MAC_SIZE; if (length < cid_size) { /* too short */ + LOGGER_ERROR(dht->log, "too short"); return false; } const uint32_t data_size = length - cid_size; if (data_size == 0) { + LOGGER_ERROR(dht->log, "data_size == 0"); return false; } if (data_size > sizeof(Node_format) * MAX_SENT_NODES) { /* invalid length */ + LOGGER_ERROR(dht->log, "data_size too large"); return false; } @@ -1542,10 +1549,12 @@ static bool handle_sendnodes_core(void *object, const IP_Port *source, const uin plain); if ((unsigned int)len != SIZEOF_VLA(plain)) { + LOGGER_ERROR(dht->log, "decrypt_data size mismatch"); return false; } if (plain[0] > size_plain_nodes) { + LOGGER_ERROR(dht->log, "number nodes in packet too large??"); return false; } @@ -1553,6 +1562,7 @@ static bool handle_sendnodes_core(void *object, const IP_Port *source, const uin memcpy(&ping_id, plain + 1 + data_size, sizeof(ping_id)); if (!sent_getnode_to_node(dht, packet + 1, source, ping_id)) { + LOGGER_ERROR(dht->log, "we did not request this nodes packet"); return false; } @@ -1560,10 +1570,12 @@ static bool handle_sendnodes_core(void *object, const IP_Port *source, const uin const int num_nodes = unpack_nodes(plain_nodes, plain[0], &length_nodes, plain + 1, data_size, false); if (length_nodes != data_size) { + LOGGER_ERROR(dht->log, "unpacking nodes size mismatch"); return false; } if (num_nodes != plain[0]) { + LOGGER_ERROR(dht->log, "unpacking nodes not specified number"); return false; } @@ -1588,9 +1600,12 @@ static int handle_sendnodes_ipv6(void *object, const IP_Port *source, const uint uint32_t num_nodes; if (!handle_sendnodes_core(object, source, packet, length, plain_nodes, MAX_SENT_NODES, &num_nodes)) { + LOGGER_WARNING(dht->log, "error parsing dht nodes packet"); return 1; } + LOGGER_INFO(dht->log, "got %u nodes", num_nodes); + if (num_nodes == 0) { return 0; } diff --git a/external/toxcore/c-toxcore/toxcore/onion_client.c b/external/toxcore/c-toxcore/toxcore/onion_client.c index c15eb555..1e93d28a 100644 --- a/external/toxcore/c-toxcore/toxcore/onion_client.c +++ b/external/toxcore/c-toxcore/toxcore/onion_client.c @@ -606,7 +606,7 @@ static uint32_t check_sendback(Onion_Client *onion_c, const uint8_t *sendback, u memcpy(&sback, sendback, sizeof(uint64_t)); uint8_t data[sizeof(uint32_t) + CRYPTO_PUBLIC_KEY_SIZE + sizeof(IP_Port) + sizeof(uint32_t)]; - if (ping_array_check(onion_c->announce_ping_array, onion_c->mono_time, data, sizeof(data), sback) != sizeof(data)) { + if (ping_array_check(onion_c->logger, onion_c->announce_ping_array, onion_c->mono_time, data, sizeof(data), sback) != sizeof(data)) { return -1; } diff --git a/external/toxcore/c-toxcore/toxcore/ping.c b/external/toxcore/c-toxcore/toxcore/ping.c index f8a96edf..856143f8 100644 --- a/external/toxcore/c-toxcore/toxcore/ping.c +++ b/external/toxcore/c-toxcore/toxcore/ping.c @@ -163,6 +163,10 @@ static int handle_ping_request(void *object, const IP_Port *source, const uint8_ return 0; } +struct DHTLOGGEREXTRACTor { + const Logger* log; +}; + non_null() static int handle_ping_response(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length, void *userdata) @@ -203,7 +207,8 @@ static int handle_ping_response(void *object, const IP_Port *source, const uint8 memcpy(&ping_id, ping_plain + 1, sizeof(ping_id)); uint8_t data[PING_DATA_SIZE]; - if (ping_array_check(ping->ping_array, ping->mono_time, data, sizeof(data), ping_id) != sizeof(data)) { + // HACK: logger is the first pointer in the struct, so we fake it + if (ping_array_check(((struct DHTLOGGEREXTRACTor*)dht)->log, ping->ping_array, ping->mono_time, data, sizeof(data), ping_id) != sizeof(data)) { return 1; } diff --git a/external/toxcore/c-toxcore/toxcore/ping_array.c b/external/toxcore/c-toxcore/toxcore/ping_array.c index f303d802..2230a304 100644 --- a/external/toxcore/c-toxcore/toxcore/ping_array.c +++ b/external/toxcore/c-toxcore/toxcore/ping_array.c @@ -13,6 +13,7 @@ #include "ccompat.h" #include "crypto_core.h" +#include "logger.h" #include "mono_time.h" #include "util.h" @@ -139,7 +140,7 @@ uint64_t ping_array_add(Ping_Array *array, const Mono_Time *mono_time, const Ran return ping_id; } -int32_t ping_array_check(Ping_Array *array, const Mono_Time *mono_time, uint8_t *data, +int32_t ping_array_check(const Logger *log, Ping_Array *array, const Mono_Time *mono_time, uint8_t *data, size_t length, uint64_t ping_id) { if (ping_id == 0) { @@ -149,20 +150,21 @@ int32_t ping_array_check(Ping_Array *array, const Mono_Time *mono_time, uint8_t const uint32_t index = ping_id % array->total_size; if (array->entries[index].ping_id != ping_id) { - return -1; + return -2; } + LOGGER_DEBUG(log, "now:%lu ts:%lu to: %u", mono_time_get(mono_time), array->entries[index].ping_time, array->timeout); if (mono_time_is_timeout(mono_time, array->entries[index].ping_time, array->timeout)) { - return -1; + return -3; } if (array->entries[index].length > length) { - return -1; + return -4; } // TODO(iphydf): This can't happen? If it indeed can't, turn it into an assert. if (array->entries[index].data == nullptr) { - return -1; + return -5; } memcpy(data, array->entries[index].data, array->entries[index].length); diff --git a/external/toxcore/c-toxcore/toxcore/ping_array.h b/external/toxcore/c-toxcore/toxcore/ping_array.h index 5fc04017..76044aa1 100644 --- a/external/toxcore/c-toxcore/toxcore/ping_array.h +++ b/external/toxcore/c-toxcore/toxcore/ping_array.h @@ -13,6 +13,7 @@ #include #include "crypto_core.h" +#include "logger.h" #include "mem.h" #include "mono_time.h" @@ -56,7 +57,7 @@ uint64_t ping_array_add(Ping_Array *array, const Mono_Time *mono_time, const Ran * @return length of data copied on success, -1 on failure. */ non_null() -int32_t ping_array_check(Ping_Array *array, const Mono_Time *mono_time, uint8_t *data, size_t length, +int32_t ping_array_check(const Logger *log, Ping_Array *array, const Mono_Time *mono_time, uint8_t *data, size_t length, uint64_t ping_id); #ifdef __cplusplus diff --git a/external/toxcore/mono_time_test.cc b/external/toxcore/mono_time_test.cc new file mode 100644 index 00000000..754f6db5 --- /dev/null +++ b/external/toxcore/mono_time_test.cc @@ -0,0 +1,138 @@ +#include "./c-toxcore/toxcore/mono_time.h" + +#ifdef NDEBUG + #undef NDEBUG + #include +#else + #include +#endif + +#include +#include +#include + +#define TEST(x, y) bool run_test_##x##y(void) + +#define ASSERT_NE(x, y) assert((x) != (y)) +#define EXPECT_GT(x, y) assert((x) > (y)) +#define EXPECT_FALSE(x) assert(!(x)) +#define EXPECT_TRUE(x) assert((x)) +#define EXPECT_EQ(x, y) assert((x) == (y)) + +TEST(MonoTime, UnixTimeIncreasesOverTime) +{ + const Memory *mem = system_memory(); + Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr); + ASSERT_NE(mono_time, nullptr); + + mono_time_update(mono_time); + uint64_t const start = mono_time_get(mono_time); + + while (start == mono_time_get(mono_time)) { + mono_time_update(mono_time); + } + + uint64_t const end = mono_time_get(mono_time); + EXPECT_GT(end, start); + + mono_time_free(mem, mono_time); + + return true; +} + +TEST(MonoTime, IsTimeout) +{ + const Memory *mem = system_memory(); + Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr); + ASSERT_NE(mono_time, nullptr); + + uint64_t const start = mono_time_get(mono_time); + EXPECT_FALSE(mono_time_is_timeout(mono_time, start, 1)); + + while (start == mono_time_get(mono_time)) { + mono_time_update(mono_time); + } + + EXPECT_TRUE(mono_time_is_timeout(mono_time, start, 1)); + + mono_time_free(mem, mono_time); + + return true; +} + +TEST(MonoTime, IsTimeoutReal) +{ + const Memory *mem = system_memory(); + Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr); + ASSERT_NE(mono_time, nullptr); + + uint64_t const start = mono_time_get(mono_time); + EXPECT_FALSE(mono_time_is_timeout(mono_time, start, 5)); + + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + mono_time_update(mono_time); + + // should still not have timed out (5sec) after sleeping ~20ms + EXPECT_FALSE(mono_time_is_timeout(mono_time, start, 5)); + + mono_time_free(mem, mono_time); + + return true; +} + +TEST(MonoTime, CustomTime) +{ + const Memory *mem = system_memory(); + Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr); + ASSERT_NE(mono_time, nullptr); + + uint64_t test_time = current_time_monotonic(mono_time) + 42137; + + mono_time_set_current_time_callback( + mono_time, [](void *user_data) { return *static_cast(user_data); }, &test_time); + mono_time_update(mono_time); + + EXPECT_EQ(current_time_monotonic(mono_time), test_time); + + uint64_t const start = mono_time_get(mono_time); + + test_time += 7000; + + mono_time_update(mono_time); + EXPECT_EQ(mono_time_get(mono_time) - start, 7); + + EXPECT_EQ(current_time_monotonic(mono_time), test_time); + + mono_time_free(mem, mono_time); + + return true; +} + +int main(int argc, char **argv) { + if (!run_test_MonoTimeUnixTimeIncreasesOverTime()) { + std::cerr << "MonoTimeUnixTimeIncreasesOverTime() failed\n"; + return -1; + } + std::cout << "MonoTimeUnixTimeIncreasesOverTime() succeeded\n"; + + if (!run_test_MonoTimeIsTimeout()) { + std::cerr << "MonoTimeIsTimeout() failed\n"; + return -1; + } + std::cout << "MonoTimeIsTimeout() succeeded\n"; + + if (!run_test_MonoTimeIsTimeoutReal()) { + std::cerr << "MonoTimeIsTimeoutReal() failed\n"; + return -1; + } + std::cout << "MonoTimeIsTimeoutReal() succeeded\n"; + + if (!run_test_MonoTimeCustomTime()) { + std::cerr << "MonoTimeCustomTime() failed\n"; + return -1; + } + std::cout << "MonoTimeCustomTime() succeeded\n"; + + return 0; +} + diff --git a/src/tox_client.cpp b/src/tox_client.cpp index e015a1de..156c2e64 100644 --- a/src/tox_client.cpp +++ b/src/tox_client.cpp @@ -1,4 +1,5 @@ #include "./tox_client.hpp" +#include "toxcore/tox.h" // meh, change this #include @@ -18,6 +19,10 @@ static void eee(std::string& mod) { } } +static void tmp_tox_log_cb(Tox *tox, Tox_Log_Level level, const char *file, uint32_t line, const char *func, const char *message, void *user_data) { + std::cerr << "l:" << level << " " << file << ":" << line << "@" << func << "(): '" << message << "'\n"; +} + ToxClient::ToxClient(std::string_view save_path, std::string_view save_password) : _tox_profile_path(save_path), _tox_profile_password(save_password) { @@ -67,6 +72,9 @@ ToxClient::ToxClient(std::string_view save_path, std::string_view save_password) } } + tox_options_set_local_discovery_enabled(options, false); + tox_options_set_log_callback(options, tmp_tox_log_cb); + TOX_ERR_NEW err_new; _tox = tox_new(options, &err_new); tox_options_free(options);