Skip to content
Open
2 changes: 1 addition & 1 deletion .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
windows:
timeout-minutes: 15

runs-on: windows-latest
runs-on: windows-2019

steps:
- uses: actions/checkout@v3
Expand Down
5 changes: 3 additions & 2 deletions external/toxcore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 17 additions & 2 deletions external/toxcore/c-toxcore/toxcore/DHT.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -1542,28 +1549,33 @@ 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;
}

uint64_t ping_id;
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;
}

uint16_t length_nodes = 0;
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;
}

Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion external/toxcore/c-toxcore/toxcore/onion_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
7 changes: 6 additions & 1 deletion external/toxcore/c-toxcore/toxcore/ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}

Expand Down
12 changes: 7 additions & 5 deletions external/toxcore/c-toxcore/toxcore/ping_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "ccompat.h"
#include "crypto_core.h"
#include "logger.h"
#include "mono_time.h"
#include "util.h"

Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion external/toxcore/c-toxcore/toxcore/ping_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <stdint.h>

#include "crypto_core.h"
#include "logger.h"
#include "mem.h"
#include "mono_time.h"

Expand Down Expand Up @@ -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
Expand Down
138 changes: 138 additions & 0 deletions external/toxcore/mono_time_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#include "./c-toxcore/toxcore/mono_time.h"

#ifdef NDEBUG
#undef NDEBUG
#include <cassert>
#else
#include <cassert>
#endif

#include <iostream>
#include <thread>
#include <chrono>

#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<uint64_t *>(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;
}

8 changes: 8 additions & 0 deletions src/tox_client.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "./tox_client.hpp"
#include "toxcore/tox.h"

// meh, change this
#include <exception>
Expand All @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down