Compare commits

...

10 Commits

Author SHA1 Message Date
ac08cd0798
tmp patch to enlargen the ngc lossy custom pkg size 2023-11-17 22:46:58 +01:00
05d1648209
Merge commit '4f02c2b55b1eb57f39d69fe7f4319b4cbb50240e' 2023-11-17 16:01:52 +01:00
4f02c2b55b Squashed 'external/toxcore/c-toxcore/' changes from 75f3c33943..d4b06edc2a
d4b06edc2a feat: add ngc events
cd34b60f0f feat: allow for larger incoming NGC packets
94cf9d1f36 fix: Fix memory leak in the error path of loading savedata.
fc623a5281 tox_new() should return null when savedata loading fails
06d949a701 fix: always respond to version packets with toxcore version
REVERT: 75f3c33943 adopt to #2415 changes
REVERT: 38e4c82fe0 feat: add ngc events

git-subtree-dir: external/toxcore/c-toxcore
git-subtree-split: d4b06edc2a35bad51b0f0950d74f61c8c70630ab
2023-11-17 16:01:52 +01:00
fd9d14d00c
tox private impl + dht caps histo 2023-11-13 16:23:49 +01:00
4e4f62dd20
provide ToxPrivateI 2023-11-13 15:14:30 +01:00
cdc4284cb5 Squashed 'external/toxcore/c-toxcore/' changes from 38e4c82fe0..75f3c33943
75f3c33943 adopt to #2415 changes

git-subtree-dir: external/toxcore/c-toxcore
git-subtree-split: 75f3c33943cd3e249cfab3dab122aa4bbb9eec9a
2023-11-13 15:03:22 +01:00
bedbacddde
Merge commit 'cdc4284cb50041b5bf7d476561085d629292e456' 2023-11-13 15:03:22 +01:00
32a8dba185 Squashed 'external/toxcore/c-toxcore/' changes from 82460b2124..38e4c82fe0
38e4c82fe0 feat: add ngc events
8099d82397 diagnostic: get the number of close dht nodes with announce/store support
d01c116764 cleanup: make it more clear that assert and uint32_t increment both only exist if NDEBUG is not defined
58fac53429 refactor: Add a `bin_unpack_bin_max` for max-length arrays.
6be29f01e5 chore: Add more logging to loading conferences from savedata.
1195271b7f Fix inversed return values
82276ef5ac cleanup: Fix GCC compatibility.
REVERT: 82460b2124 feat: add ngc events

git-subtree-dir: external/toxcore/c-toxcore
git-subtree-split: 38e4c82fe0fc373b9d43ee9ad2b8fe5fd1d26810
2023-11-13 14:02:43 +01:00
d6e5051b15
Merge commit '32a8dba185482fe1a89037bce2a5b0c5e76d4127' 2023-11-13 14:02:43 +01:00
780a67a40d
update for toxcore changes 2023-11-13 13:25:41 +01:00
71 changed files with 450 additions and 131 deletions

@ -1 +1 @@
Subproject commit f8851c1033514d3ead25f4df1b643a0e5960ecbc Subproject commit d05875f489577e9a2c26234810058b41c3236cf7

View File

@ -155,6 +155,11 @@ configure_file(
${TOX_DIR}tox/tox_events.h ${TOX_DIR}tox/tox_events.h
@ONLY @ONLY
) )
configure_file(
${TOX_DIR}toxcore/tox_private.h
${TOX_DIR}tox/tox_private.h
@ONLY
)
target_include_directories(toxcore PRIVATE "${TOX_DIR}toxcore") target_include_directories(toxcore PRIVATE "${TOX_DIR}toxcore")
target_include_directories(toxcore PUBLIC "${TOX_DIR}") target_include_directories(toxcore PUBLIC "${TOX_DIR}")

View File

@ -4,11 +4,9 @@ set -eux
git submodule update --init --recursive git submodule update --init --recursive
/src/workspace/tools/inject-repo c-toxcore /src/workspace/tools/inject-repo c-toxcore
# TODO(iphydf): Re-enable fuzz-test when https://github.com/tweag/rules_nixpkgs/issues/442 is fixed.
cd /src/workspace && bazel test -k \ cd /src/workspace && bazel test -k \
--config=remote \ --build_tag_filters=-haskell,-fuzz-test \
--build_tag_filters=-haskell \ --test_tag_filters=-haskell,-fuzz-test \
--test_tag_filters=-haskell \
--remote_download_minimal \
-- \ -- \
//c-toxcore/... \
"$@" "$@"

View File

@ -7,11 +7,11 @@ workflows:
jobs: jobs:
# Dynamic analysis in the Bazel build # Dynamic analysis in the Bazel build
- bazel-asan - bazel-asan
- bazel-msan
- bazel-tsan - bazel-tsan
# Dynamic analysis with CMake # Dynamic analysis with CMake
- asan - asan
- tsan - tsan
- msan
- ubsan - ubsan
# Static analysis # Static analysis
- clang-analyze - clang-analyze
@ -29,6 +29,7 @@ jobs:
steps: steps:
- checkout - checkout
- run: .circleci/bazel-test - run: .circleci/bazel-test
//c-toxcore/...
bazel-tsan: bazel-tsan:
working_directory: /tmp/cirrus-ci-build working_directory: /tmp/cirrus-ci-build
@ -38,11 +39,22 @@ jobs:
steps: steps:
- checkout - checkout
- run: .circleci/bazel-test - run: .circleci/bazel-test
//c-toxcore/...
-//c-toxcore/auto_tests:conference_av_test -//c-toxcore/auto_tests:conference_av_test
-//c-toxcore/auto_tests:conference_test -//c-toxcore/auto_tests:conference_test
-//c-toxcore/auto_tests:onion_test -//c-toxcore/auto_tests:onion_test
-//c-toxcore/auto_tests:tox_many_test -//c-toxcore/auto_tests:tox_many_test
bazel-msan:
working_directory: /tmp/cirrus-ci-build
docker:
- image: toxchat/toktok-stack:latest-msan
steps:
- checkout
- run: .circleci/bazel-test
//c-toxcore/auto_tests:lossless_packet_test
asan: asan:
working_directory: ~/work working_directory: ~/work
docker: docker:
@ -91,21 +103,6 @@ jobs:
- run: git submodule update --init --recursive - run: git submodule update --init --recursive
- run: CC=clang .circleci/cmake-ubsan - run: CC=clang .circleci/cmake-ubsan
msan:
working_directory: ~/work
docker:
- image: toxchat/toktok-stack:latest-msan
steps:
- checkout
- run: git submodule update --init --recursive
- run: rm -rf /src/workspace/c-toxcore/* && mv * /src/workspace/c-toxcore/
- run:
cd /src/workspace && bazel test
//c-toxcore/auto_tests:lossless_packet_test
//c-toxcore/toxav/...
//c-toxcore/toxcore/...
infer: infer:
working_directory: ~/work working_directory: ~/work
docker: docker:

View File

@ -9,10 +9,9 @@ bazel-opt_task:
- /src/workspace/tools/inject-repo c-toxcore - /src/workspace/tools/inject-repo c-toxcore
test_all_script: test_all_script:
- cd /src/workspace && bazel test -k - cd /src/workspace && bazel test -k
--remote_http_cache=http://$CIRRUS_HTTP_CACHE_HOST --config=ci
--build_tag_filters=-haskell --build_tag_filters=-haskell
--test_tag_filters=-haskell --test_tag_filters=-haskell
--remote_download_minimal
-- --
//c-toxcore/... //c-toxcore/...
-//c-toxcore/auto_tests:tcp_relay_test # TODO(robinlinden): Why does this pass locally but not in Cirrus? -//c-toxcore/auto_tests:tcp_relay_test # TODO(robinlinden): Why does this pass locally but not in Cirrus?
@ -27,10 +26,9 @@ bazel-dbg_task:
- /src/workspace/tools/inject-repo c-toxcore - /src/workspace/tools/inject-repo c-toxcore
test_all_script: test_all_script:
- cd /src/workspace && bazel test -k - cd /src/workspace && bazel test -k
--remote_http_cache=http://$CIRRUS_HTTP_CACHE_HOST --config=ci
--build_tag_filters=-haskell --build_tag_filters=-haskell
--test_tag_filters=-haskell --test_tag_filters=-haskell
--remote_download_minimal
-- --
//c-toxcore/... //c-toxcore/...
-//c-toxcore/auto_tests:tcp_relay_test # TODO(robinlinden): Why does this pass locally but not in Cirrus? -//c-toxcore/auto_tests:tcp_relay_test # TODO(robinlinden): Why does this pass locally but not in Cirrus?
@ -45,7 +43,7 @@ cimple_task:
- /src/workspace/tools/inject-repo c-toxcore - /src/workspace/tools/inject-repo c-toxcore
test_all_script: test_all_script:
- cd /src/workspace && bazel test -k - cd /src/workspace && bazel test -k
--remote_http_cache=http://$CIRRUS_HTTP_CACHE_HOST --config=ci
--build_tag_filters=haskell --build_tag_filters=haskell
--test_tag_filters=haskell --test_tag_filters=haskell
-- --

View File

@ -29,12 +29,26 @@ flaky_tests = {
"tox_many_tcp_test": True, "tox_many_tcp_test": True,
} }
extra_args = {
"proxy_test": ["$(location //c-toxcore/other/proxy)"],
}
extra_data = {
"proxy_test": ["//c-toxcore/other/proxy"],
}
[cc_test( [cc_test(
name = src[:-2], name = src[:-2],
size = "small", size = "small",
srcs = [src], srcs = [src],
args = ["$(location %s)" % src] + ["$(location //c-toxcore/other/proxy)"], args = ["$(location %s)" % src] + extra_args.get(
data = glob(["data/*"]) + ["//c-toxcore/other/proxy"], src[:-2],
[],
),
data = glob(["data/*"]) + extra_data.get(
src[:-2],
[],
),
flaky = flaky_tests.get( flaky = flaky_tests.get(
src[:-2], src[:-2],
False, False,

View File

@ -122,6 +122,12 @@ static void test_dht_getnodes(AutoTox *autotoxes)
tox_self_get_dht_id(autotoxes[i].tox, public_key_list[i]); tox_self_get_dht_id(autotoxes[i].tox, public_key_list[i]);
tox_callback_dht_get_nodes_response(autotoxes[i].tox, getnodes_response_cb); tox_callback_dht_get_nodes_response(autotoxes[i].tox, getnodes_response_cb);
printf("Peer %zu dht closenode count total/annouce-capable: %d/%d\n",
i,
tox_dht_get_num_closelist(autotoxes[i].tox),
tox_dht_get_num_closelist_announce_capable(autotoxes[i].tox)
);
} }
while (!all_nodes_crawled(autotoxes, NUM_TOXES, public_key_list)) { while (!all_nodes_crawled(autotoxes, NUM_TOXES, public_key_list)) {

View File

@ -16,6 +16,7 @@ cc_binary(
testonly = 1, testonly = 1,
srcs = ["DHT_bootstrap.c"], srcs = ["DHT_bootstrap.c"],
deps = [ deps = [
":bootstrap_node_packets",
"//c-toxcore/testing:misc_tools", "//c-toxcore/testing:misc_tools",
"//c-toxcore/toxcore:DHT", "//c-toxcore/toxcore:DHT",
"//c-toxcore/toxcore:LAN_discovery", "//c-toxcore/toxcore:LAN_discovery",

View File

@ -31,11 +31,16 @@
#include "../testing/misc_tools.h" #include "../testing/misc_tools.h"
#define DHT_NODE_EXTRA_PACKETS
#ifdef DHT_NODE_EXTRA_PACKETS #ifdef DHT_NODE_EXTRA_PACKETS
#include "./bootstrap_node_packets.h" #include "./bootstrap_node_packets.h"
#define DHT_VERSION_NUMBER 1 #ifndef DAEMON_VERSION_NUMBER
#define DHT_MOTD "This is a test motd" #define DAEMON_VERSION_NUMBER (1000000000UL + TOX_VERSION_MAJOR*1000000UL + TOX_VERSION_MINOR*1000UL + TOX_VERSION_PATCH*1UL)
#endif
static const char *motd_str = ""; //Change this to anything within 256 bytes(but 96 bytes maximum prefered)
#endif #endif
#define PORT 33445 #define PORT 33445
@ -152,7 +157,7 @@ int main(int argc, char *argv[])
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);
#ifdef DHT_NODE_EXTRA_PACKETS #ifdef DHT_NODE_EXTRA_PACKETS
bootstrap_set_callbacks(dht_get_net(dht), DHT_VERSION_NUMBER, DHT_MOTD, sizeof(DHT_MOTD)); bootstrap_set_callbacks(dht_get_net(dht), (uint32_t)DAEMON_VERSION_NUMBER, (const uint8_t *) motd_str, strlen(motd_str)+1);
#endif #endif
if (!(onion && forwarding && onion_a)) { if (!(onion && forwarding && onion_a)) {

View File

@ -1 +1 @@
4f5b47978dc26aed78719526f862a44693f821db12f5ff6d70b338d67fb6f784 /usr/local/bin/tox-bootstrapd b2996d73cab7c7453dc10ccf7ad733622558de3b1ad0db824a379cf96f500379 /usr/local/bin/tox-bootstrapd

View File

@ -306,7 +306,7 @@ void generate_event_impl(const std::string& event_name, std::vector<EventType> e
f << " Tox_Event_" << event_name << " *event, Bin_Unpack *bu)\n{\n"; f << " Tox_Event_" << event_name << " *event, Bin_Unpack *bu)\n{\n";
f << " assert(event != nullptr);\n"; f << " assert(event != nullptr);\n";
if (event_types.size() > 1) { if (event_types.size() > 1) {
f << " if (!bin_unpack_array_fixed(bu, " << event_types.size() << ")) {\n return false;\n }\n\n"; f << " if (!bin_unpack_array_fixed(bu, " << event_types.size() << ", nullptr)) {\n return false;\n }\n\n";
} }
bool first = true; bool first = true;

View File

@ -28,7 +28,7 @@ cc_library(
cc_fuzz_test( cc_fuzz_test(
name = "bootstrap_fuzz_test", name = "bootstrap_fuzz_test",
#size = "small", size = "small",
srcs = ["bootstrap_harness.cc"], srcs = ["bootstrap_harness.cc"],
copts = ["-UNDEBUG"], copts = ["-UNDEBUG"],
corpus = ["//tools/toktok-fuzzer/corpus:bootstrap_fuzzer"], corpus = ["//tools/toktok-fuzzer/corpus:bootstrap_fuzzer"],
@ -43,7 +43,7 @@ cc_fuzz_test(
cc_fuzz_test( cc_fuzz_test(
name = "e2e_fuzz_test", name = "e2e_fuzz_test",
#size = "small", size = "small",
srcs = ["e2e_fuzz_test.cc"], srcs = ["e2e_fuzz_test.cc"],
copts = ["-UNDEBUG"], copts = ["-UNDEBUG"],
corpus = ["//tools/toktok-fuzzer/corpus:e2e_fuzz_test"], corpus = ["//tools/toktok-fuzzer/corpus:e2e_fuzz_test"],
@ -59,7 +59,7 @@ cc_fuzz_test(
cc_fuzz_test( cc_fuzz_test(
name = "toxsave_fuzz_test", name = "toxsave_fuzz_test",
#size = "small", size = "small",
srcs = ["toxsave_harness.cc"], srcs = ["toxsave_harness.cc"],
copts = ["-UNDEBUG"], copts = ["-UNDEBUG"],
corpus = ["//tools/toktok-fuzzer/corpus:toxsave_fuzzer"], corpus = ["//tools/toktok-fuzzer/corpus:toxsave_fuzzer"],
@ -92,7 +92,7 @@ fuzzing_binary(
cc_fuzz_test( cc_fuzz_test(
name = "protodump_reduce", name = "protodump_reduce",
#size = "small", size = "small",
srcs = ["protodump_reduce.cc"], srcs = ["protodump_reduce.cc"],
copts = ["-UNDEBUG"], copts = ["-UNDEBUG"],
deps = [ deps = [

View File

@ -22,6 +22,8 @@
const bool DEBUG = false; const bool DEBUG = false;
static constexpr tox_mono_time_cb *get_self_clock = ![](Fuzz_System *self) { return self->clock; };
// TODO(iphydf): Put this somewhere shared. // TODO(iphydf): Put this somewhere shared.
struct Network_Addr { struct Network_Addr {
struct sockaddr_storage addr; struct sockaddr_storage addr;
@ -182,7 +184,7 @@ Fuzz_System::Fuzz_System(Fuzz_Data &input)
} }
, data(input) , data(input)
{ {
sys->mono_time_callback = ![](Fuzz_System *self) { return self->clock; }; sys->mono_time_callback = get_self_clock;
sys->mono_time_user_data = this; sys->mono_time_user_data = this;
sys->mem = mem.get(); sys->mem = mem.get();
sys->ns = ns.get(); sys->ns = ns.get();
@ -269,7 +271,7 @@ Null_System::Null_System()
std::make_unique<Random>(Random{&null_random_funcs, this}), std::make_unique<Random>(Random{&null_random_funcs, this}),
} }
{ {
sys->mono_time_callback = ![](Fuzz_System *self) { return self->clock; }; sys->mono_time_callback = get_self_clock;
sys->mono_time_user_data = this; sys->mono_time_user_data = this;
sys->mem = mem.get(); sys->mem = mem.get();
sys->ns = ns.get(); sys->ns = ns.get();
@ -400,7 +402,7 @@ Record_System::Record_System(Global &global, uint64_t seed, const char *name)
, seed_(seed) , seed_(seed)
, name_(name) , name_(name)
{ {
sys->mono_time_callback = ![](Fuzz_System *self) { return self->clock; }; sys->mono_time_callback = get_self_clock;
sys->mono_time_user_data = this; sys->mono_time_user_data = this;
sys->mem = mem.get(); sys->mem = mem.get();
sys->ns = ns.get(); sys->ns = ns.get();

View File

@ -533,8 +533,8 @@ int pack_nodes(const Logger *logger, uint8_t *data, uint16_t length, const Node_
#ifndef NDEBUG #ifndef NDEBUG
const uint32_t increment = ipp_size + CRYPTO_PUBLIC_KEY_SIZE; const uint32_t increment = ipp_size + CRYPTO_PUBLIC_KEY_SIZE;
#endif
assert(increment == PACKED_NODE_SIZE_IP4 || increment == PACKED_NODE_SIZE_IP6); assert(increment == PACKED_NODE_SIZE_IP4 || increment == PACKED_NODE_SIZE_IP6);
#endif
} }
return packed_length; return packed_length;
@ -565,8 +565,8 @@ int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed
#ifndef NDEBUG #ifndef NDEBUG
const uint32_t increment = ipp_size + CRYPTO_PUBLIC_KEY_SIZE; const uint32_t increment = ipp_size + CRYPTO_PUBLIC_KEY_SIZE;
#endif
assert(increment == PACKED_NODE_SIZE_IP4 || increment == PACKED_NODE_SIZE_IP6); assert(increment == PACKED_NODE_SIZE_IP4 || increment == PACKED_NODE_SIZE_IP6);
#endif
} }
if (processed_data_len != nullptr) { if (processed_data_len != nullptr) {
@ -2965,6 +2965,34 @@ bool dht_non_lan_connected(const DHT *dht)
return false; return false;
} }
uint16_t dht_get_num_closelist(const DHT *dht) {
uint16_t num_valid_close_clients = 0;
for (uint32_t i = 0; i < LCLIENT_LIST; ++i) {
const Client_data *const client = dht_get_close_client(dht, i);
// check if client is valid
if (!(assoc_timeout(dht->cur_time, &client->assoc4) && assoc_timeout(dht->cur_time, &client->assoc6))) {
++num_valid_close_clients;
}
}
return num_valid_close_clients;
}
uint16_t dht_get_num_closelist_announce_capable(const DHT *dht) {
uint16_t num_valid_close_clients_with_cap = 0;
for (uint32_t i = 0; i < LCLIENT_LIST; ++i) {
const Client_data *const client = dht_get_close_client(dht, i);
// check if client is valid
if (!(assoc_timeout(dht->cur_time, &client->assoc4) && assoc_timeout(dht->cur_time, &client->assoc6)) && client->announce_node) {
++num_valid_close_clients_with_cap;
}
}
return num_valid_close_clients_with_cap;
}
unsigned int ipport_self_copy(const DHT *dht, IP_Port *dest) unsigned int ipport_self_copy(const DHT *dht, IP_Port *dest)
{ {
ipport_reset(dest); ipport_reset(dest);

View File

@ -515,6 +515,24 @@ bool dht_isconnected(const DHT *dht);
non_null() non_null()
bool dht_non_lan_connected(const DHT *dht); bool dht_non_lan_connected(const DHT *dht);
/**
* This function returns the ratio of close dht nodes that are known to support announce/store.
* This function returns the number of DHT nodes in the closelist.
*
* @return number
*/
non_null()
uint16_t dht_get_num_closelist(const DHT *dht);
/**
* This function returns the number of DHT nodes in the closelist,
* that are capable to store annouce data (introduced in version 0.2.18).
*
* @return number
*/
non_null()
uint16_t dht_get_num_closelist_announce_capable(const DHT *dht);
/** @brief Attempt to add client with ip_port and public_key to the friends client list /** @brief Attempt to add client with ip_port and public_key to the friends client list
* and close_clientlist. * and close_clientlist.
* *

View File

@ -2988,6 +2988,7 @@ static State_Load_Status load_nospam_keys(Messenger *m, const uint8_t *data, uin
load_secret_key(m->net_crypto, data + sizeof(uint32_t) + CRYPTO_PUBLIC_KEY_SIZE); load_secret_key(m->net_crypto, data + sizeof(uint32_t) + CRYPTO_PUBLIC_KEY_SIZE);
if (!pk_equal(data + sizeof(uint32_t), nc_get_self_public_key(m->net_crypto))) { if (!pk_equal(data + sizeof(uint32_t), nc_get_self_public_key(m->net_crypto))) {
LOGGER_ERROR(m->log, "public key stored in savedata does not match its secret key");
return STATE_LOAD_STATUS_ERROR; return STATE_LOAD_STATUS_ERROR;
} }
@ -3219,9 +3220,13 @@ static State_Load_Status groups_load(Messenger *m, const uint8_t *data, uint32_t
if (group_number < 0) { if (group_number < 0) {
LOGGER_WARNING(m->log, "Failed to load group %u", i); LOGGER_WARNING(m->log, "Failed to load group %u", i);
// Can't recover trivially. We may need to skip over some data here.
break;
} }
} }
LOGGER_DEBUG(m->log, "Successfully loaded %u groups", gc_count_groups(m->group_handler));
bin_unpack_free(bu); bin_unpack_free(bu);
return STATE_LOAD_STATUS_CONTINUE; return STATE_LOAD_STATUS_CONTINUE;

View File

@ -73,10 +73,14 @@ bool bin_unpack_array(Bin_Unpack *bu, uint32_t *size)
return cmp_read_array(&bu->ctx, size) && *size <= bu->bytes_size; return cmp_read_array(&bu->ctx, size) && *size <= bu->bytes_size;
} }
bool bin_unpack_array_fixed(Bin_Unpack *bu, uint32_t required_size) bool bin_unpack_array_fixed(Bin_Unpack *bu, uint32_t required_size, uint32_t *actual_size)
{ {
uint32_t size; uint32_t size = 0;
return cmp_read_array(&bu->ctx, &size) && size == required_size; const bool success = cmp_read_array(&bu->ctx, &size) && size == required_size;
if (actual_size != nullptr) {
*actual_size = size;
}
return success;
} }
bool bin_unpack_bool(Bin_Unpack *bu, bool *val) bool bin_unpack_bool(Bin_Unpack *bu, bool *val)
@ -128,6 +132,18 @@ bool bin_unpack_bin(Bin_Unpack *bu, uint8_t **data_ptr, uint32_t *data_length_pt
return true; return true;
} }
bool bin_unpack_bin_max(Bin_Unpack *bu, uint8_t *data, uint16_t *data_length_ptr, uint16_t max_data_length)
{
uint32_t bin_size;
if (!bin_unpack_bin_size(bu, &bin_size) || bin_size > max_data_length) {
return false;
}
*data_length_ptr = bin_size;
return bin_unpack_bin_b(bu, data, bin_size);
}
bool bin_unpack_bin_fixed(Bin_Unpack *bu, uint8_t *data, uint32_t data_length) bool bin_unpack_bin_fixed(Bin_Unpack *bu, uint8_t *data, uint32_t data_length)
{ {
uint32_t bin_size; uint32_t bin_size;

View File

@ -45,10 +45,14 @@ void bin_unpack_free(Bin_Unpack *bu);
non_null() bool bin_unpack_array(Bin_Unpack *bu, uint32_t *size); non_null() bool bin_unpack_array(Bin_Unpack *bu, uint32_t *size);
/** @brief Start unpacking a fixed size MessagePack array. /** @brief Start unpacking a fixed size MessagePack array.
*
* Fails if the array size is not the required size. If `actual_size` is passed a non-null
* pointer, the array size is written there.
* *
* @retval false if the packed array size is not exactly the required size. * @retval false if the packed array size is not exactly the required size.
*/ */
non_null() bool bin_unpack_array_fixed(Bin_Unpack *bu, uint32_t required_size); non_null(1) nullable(3)
bool bin_unpack_array_fixed(Bin_Unpack *bu, uint32_t required_size, uint32_t *actual_size);
/** @brief Unpack a MessagePack bool. */ /** @brief Unpack a MessagePack bool. */
non_null() bool bin_unpack_bool(Bin_Unpack *bu, bool *val); non_null() bool bin_unpack_bool(Bin_Unpack *bu, bool *val);
@ -71,10 +75,16 @@ non_null() bool bin_unpack_nil(Bin_Unpack *bu);
* large allocation unless the input array was already that large. * large allocation unless the input array was already that large.
*/ */
non_null() bool bin_unpack_bin(Bin_Unpack *bu, uint8_t **data_ptr, uint32_t *data_length_ptr); non_null() bool bin_unpack_bin(Bin_Unpack *bu, uint8_t **data_ptr, uint32_t *data_length_ptr);
/** @brief Unpack a variable size MessagePack bin into a fixed size byte array.
*
* Stores unpacked data into `data` with its length stored in `data_length_ptr`. This function does
* not allocate memory and requires that `max_data_length` is less than or equal to `sizeof(arr)`
* when `arr` is passed as `data` pointer.
*/
non_null() bool bin_unpack_bin_max(Bin_Unpack *bu, uint8_t *data, uint16_t *data_length_ptr, uint16_t max_data_length);
/** @brief Unpack a MessagePack bin of a fixed length into a pre-allocated byte array. /** @brief Unpack a MessagePack bin of a fixed length into a pre-allocated byte array.
* *
* Unlike the function above, this function does not allocate any memory, but requires the size to * Similar to the function above, but doesn't output the data length.
* be known up front.
*/ */
non_null() bool bin_unpack_bin_fixed(Bin_Unpack *bu, uint8_t *data, uint32_t data_length); non_null() bool bin_unpack_bin_fixed(Bin_Unpack *bu, uint8_t *data, uint32_t data_length);

View File

@ -120,7 +120,7 @@ static bool tox_event_conference_invite_unpack(
Tox_Event_Conference_Invite *event, Bin_Unpack *bu) Tox_Event_Conference_Invite *event, Bin_Unpack *bu)
{ {
assert(event != nullptr); assert(event != nullptr);
if (!bin_unpack_array_fixed(bu, 3)) { if (!bin_unpack_array_fixed(bu, 3, nullptr)) {
return false; return false;
} }

View File

@ -135,7 +135,7 @@ static bool tox_event_conference_message_unpack(
Tox_Event_Conference_Message *event, Bin_Unpack *bu) Tox_Event_Conference_Message *event, Bin_Unpack *bu)
{ {
assert(event != nullptr); assert(event != nullptr);
if (!bin_unpack_array_fixed(bu, 4)) { if (!bin_unpack_array_fixed(bu, 4, nullptr)) {
return false; return false;
} }

View File

@ -120,7 +120,7 @@ static bool tox_event_conference_peer_name_unpack(
Tox_Event_Conference_Peer_Name *event, Bin_Unpack *bu) Tox_Event_Conference_Peer_Name *event, Bin_Unpack *bu)
{ {
assert(event != nullptr); assert(event != nullptr);
if (!bin_unpack_array_fixed(bu, 3)) { if (!bin_unpack_array_fixed(bu, 3, nullptr)) {
return false; return false;
} }

View File

@ -119,7 +119,7 @@ static bool tox_event_conference_title_unpack(
Tox_Event_Conference_Title *event, Bin_Unpack *bu) Tox_Event_Conference_Title *event, Bin_Unpack *bu)
{ {
assert(event != nullptr); assert(event != nullptr);
if (!bin_unpack_array_fixed(bu, 3)) { if (!bin_unpack_array_fixed(bu, 3, nullptr)) {
return false; return false;
} }

View File

@ -112,7 +112,7 @@ static bool tox_event_file_chunk_request_unpack(
Tox_Event_File_Chunk_Request *event, Bin_Unpack *bu) Tox_Event_File_Chunk_Request *event, Bin_Unpack *bu)
{ {
assert(event != nullptr); assert(event != nullptr);
if (!bin_unpack_array_fixed(bu, 4)) { if (!bin_unpack_array_fixed(bu, 4, nullptr)) {
return false; return false;
} }

View File

@ -149,7 +149,7 @@ static bool tox_event_file_recv_unpack(
Tox_Event_File_Recv *event, Bin_Unpack *bu) Tox_Event_File_Recv *event, Bin_Unpack *bu)
{ {
assert(event != nullptr); assert(event != nullptr);
if (!bin_unpack_array_fixed(bu, 5)) { if (!bin_unpack_array_fixed(bu, 5, nullptr)) {
return false; return false;
} }

View File

@ -134,7 +134,7 @@ static bool tox_event_file_recv_chunk_unpack(
Tox_Event_File_Recv_Chunk *event, Bin_Unpack *bu) Tox_Event_File_Recv_Chunk *event, Bin_Unpack *bu)
{ {
assert(event != nullptr); assert(event != nullptr);
if (!bin_unpack_array_fixed(bu, 4)) { if (!bin_unpack_array_fixed(bu, 4, nullptr)) {
return false; return false;
} }

View File

@ -99,7 +99,7 @@ static bool tox_event_file_recv_control_unpack(
Tox_Event_File_Recv_Control *event, Bin_Unpack *bu) Tox_Event_File_Recv_Control *event, Bin_Unpack *bu)
{ {
assert(event != nullptr); assert(event != nullptr);
if (!bin_unpack_array_fixed(bu, 3)) { if (!bin_unpack_array_fixed(bu, 3, nullptr)) {
return false; return false;
} }

View File

@ -86,7 +86,7 @@ static bool tox_event_friend_connection_status_unpack(
Tox_Event_Friend_Connection_Status *event, Bin_Unpack *bu) Tox_Event_Friend_Connection_Status *event, Bin_Unpack *bu)
{ {
assert(event != nullptr); assert(event != nullptr);
if (!bin_unpack_array_fixed(bu, 2)) { if (!bin_unpack_array_fixed(bu, 2, nullptr)) {
return false; return false;
} }

View File

@ -105,7 +105,7 @@ static bool tox_event_friend_lossless_packet_unpack(
Tox_Event_Friend_Lossless_Packet *event, Bin_Unpack *bu) Tox_Event_Friend_Lossless_Packet *event, Bin_Unpack *bu)
{ {
assert(event != nullptr); assert(event != nullptr);
if (!bin_unpack_array_fixed(bu, 2)) { if (!bin_unpack_array_fixed(bu, 2, nullptr)) {
return false; return false;
} }

View File

@ -104,7 +104,7 @@ static bool tox_event_friend_lossy_packet_unpack(
Tox_Event_Friend_Lossy_Packet *event, Bin_Unpack *bu) Tox_Event_Friend_Lossy_Packet *event, Bin_Unpack *bu)
{ {
assert(event != nullptr); assert(event != nullptr);
if (!bin_unpack_array_fixed(bu, 2)) { if (!bin_unpack_array_fixed(bu, 2, nullptr)) {
return false; return false;
} }

View File

@ -119,7 +119,7 @@ static bool tox_event_friend_message_unpack(
Tox_Event_Friend_Message *event, Bin_Unpack *bu) Tox_Event_Friend_Message *event, Bin_Unpack *bu)
{ {
assert(event != nullptr); assert(event != nullptr);
if (!bin_unpack_array_fixed(bu, 3)) { if (!bin_unpack_array_fixed(bu, 3, nullptr)) {
return false; return false;
} }

View File

@ -104,7 +104,7 @@ static bool tox_event_friend_name_unpack(
Tox_Event_Friend_Name *event, Bin_Unpack *bu) Tox_Event_Friend_Name *event, Bin_Unpack *bu)
{ {
assert(event != nullptr); assert(event != nullptr);
if (!bin_unpack_array_fixed(bu, 2)) { if (!bin_unpack_array_fixed(bu, 2, nullptr)) {
return false; return false;
} }

View File

@ -83,7 +83,7 @@ static bool tox_event_friend_read_receipt_unpack(
Tox_Event_Friend_Read_Receipt *event, Bin_Unpack *bu) Tox_Event_Friend_Read_Receipt *event, Bin_Unpack *bu)
{ {
assert(event != nullptr); assert(event != nullptr);
if (!bin_unpack_array_fixed(bu, 2)) { if (!bin_unpack_array_fixed(bu, 2, nullptr)) {
return false; return false;
} }

View File

@ -105,7 +105,7 @@ static bool tox_event_friend_request_unpack(
Tox_Event_Friend_Request *event, Bin_Unpack *bu) Tox_Event_Friend_Request *event, Bin_Unpack *bu)
{ {
assert(event != nullptr); assert(event != nullptr);
if (!bin_unpack_array_fixed(bu, 2)) { if (!bin_unpack_array_fixed(bu, 2, nullptr)) {
return false; return false;
} }

View File

@ -84,7 +84,7 @@ static bool tox_event_friend_status_unpack(
Tox_Event_Friend_Status *event, Bin_Unpack *bu) Tox_Event_Friend_Status *event, Bin_Unpack *bu)
{ {
assert(event != nullptr); assert(event != nullptr);
if (!bin_unpack_array_fixed(bu, 2)) { if (!bin_unpack_array_fixed(bu, 2, nullptr)) {
return false; return false;
} }

View File

@ -106,7 +106,7 @@ static bool tox_event_friend_status_message_unpack(
Tox_Event_Friend_Status_Message *event, Bin_Unpack *bu) Tox_Event_Friend_Status_Message *event, Bin_Unpack *bu)
{ {
assert(event != nullptr); assert(event != nullptr);
if (!bin_unpack_array_fixed(bu, 2)) { if (!bin_unpack_array_fixed(bu, 2, nullptr)) {
return false; return false;
} }

View File

@ -82,7 +82,7 @@ static bool tox_event_friend_typing_unpack(
Tox_Event_Friend_Typing *event, Bin_Unpack *bu) Tox_Event_Friend_Typing *event, Bin_Unpack *bu)
{ {
assert(event != nullptr); assert(event != nullptr);
if (!bin_unpack_array_fixed(bu, 2)) { if (!bin_unpack_array_fixed(bu, 2, nullptr)) {
return false; return false;
} }

View File

@ -120,7 +120,7 @@ static bool tox_event_group_custom_packet_unpack(
Tox_Event_Group_Custom_Packet *event, Bin_Unpack *bu) Tox_Event_Group_Custom_Packet *event, Bin_Unpack *bu)
{ {
assert(event != nullptr); assert(event != nullptr);
if (!bin_unpack_array_fixed(bu, 3)) { if (!bin_unpack_array_fixed(bu, 3, nullptr)) {
return false; return false;
} }

View File

@ -120,7 +120,7 @@ static bool tox_event_group_custom_private_packet_unpack(
Tox_Event_Group_Custom_Private_Packet *event, Bin_Unpack *bu) Tox_Event_Group_Custom_Private_Packet *event, Bin_Unpack *bu)
{ {
assert(event != nullptr); assert(event != nullptr);
if (!bin_unpack_array_fixed(bu, 3)) { if (!bin_unpack_array_fixed(bu, 3, nullptr)) {
return false; return false;
} }

View File

@ -142,7 +142,7 @@ static bool tox_event_group_invite_unpack(
Tox_Event_Group_Invite *event, Bin_Unpack *bu) Tox_Event_Group_Invite *event, Bin_Unpack *bu)
{ {
assert(event != nullptr); assert(event != nullptr);
if (!bin_unpack_array_fixed(bu, 3)) { if (!bin_unpack_array_fixed(bu, 3, nullptr)) {
return false; return false;
} }

View File

@ -84,7 +84,7 @@ static bool tox_event_group_join_fail_unpack(
Tox_Event_Group_Join_Fail *event, Bin_Unpack *bu) Tox_Event_Group_Join_Fail *event, Bin_Unpack *bu)
{ {
assert(event != nullptr); assert(event != nullptr);
if (!bin_unpack_array_fixed(bu, 2)) { if (!bin_unpack_array_fixed(bu, 2, nullptr)) {
return false; return false;
} }

View File

@ -150,7 +150,7 @@ static bool tox_event_group_message_unpack(
Tox_Event_Group_Message *event, Bin_Unpack *bu) Tox_Event_Group_Message *event, Bin_Unpack *bu)
{ {
assert(event != nullptr); assert(event != nullptr);
if (!bin_unpack_array_fixed(bu, 5)) { if (!bin_unpack_array_fixed(bu, 5, nullptr)) {
return false; return false;
} }

View File

@ -114,7 +114,7 @@ static bool tox_event_group_moderation_unpack(
Tox_Event_Group_Moderation *event, Bin_Unpack *bu) Tox_Event_Group_Moderation *event, Bin_Unpack *bu)
{ {
assert(event != nullptr); assert(event != nullptr);
if (!bin_unpack_array_fixed(bu, 4)) { if (!bin_unpack_array_fixed(bu, 4, nullptr)) {
return false; return false;
} }

View File

@ -105,7 +105,7 @@ static bool tox_event_group_password_unpack(
Tox_Event_Group_Password *event, Bin_Unpack *bu) Tox_Event_Group_Password *event, Bin_Unpack *bu)
{ {
assert(event != nullptr); assert(event != nullptr);
if (!bin_unpack_array_fixed(bu, 2)) { if (!bin_unpack_array_fixed(bu, 2, nullptr)) {
return false; return false;
} }

View File

@ -172,7 +172,7 @@ static bool tox_event_group_peer_exit_unpack(
Tox_Event_Group_Peer_Exit *event, Bin_Unpack *bu) Tox_Event_Group_Peer_Exit *event, Bin_Unpack *bu)
{ {
assert(event != nullptr); assert(event != nullptr);
if (!bin_unpack_array_fixed(bu, 5)) { if (!bin_unpack_array_fixed(bu, 5, nullptr)) {
return false; return false;
} }

View File

@ -84,7 +84,7 @@ static bool tox_event_group_peer_join_unpack(
Tox_Event_Group_Peer_Join *event, Bin_Unpack *bu) Tox_Event_Group_Peer_Join *event, Bin_Unpack *bu)
{ {
assert(event != nullptr); assert(event != nullptr);
if (!bin_unpack_array_fixed(bu, 2)) { if (!bin_unpack_array_fixed(bu, 2, nullptr)) {
return false; return false;
} }

View File

@ -84,7 +84,7 @@ static bool tox_event_group_peer_limit_unpack(
Tox_Event_Group_Peer_Limit *event, Bin_Unpack *bu) Tox_Event_Group_Peer_Limit *event, Bin_Unpack *bu)
{ {
assert(event != nullptr); assert(event != nullptr);
if (!bin_unpack_array_fixed(bu, 2)) { if (!bin_unpack_array_fixed(bu, 2, nullptr)) {
return false; return false;
} }

View File

@ -120,7 +120,7 @@ static bool tox_event_group_peer_name_unpack(
Tox_Event_Group_Peer_Name *event, Bin_Unpack *bu) Tox_Event_Group_Peer_Name *event, Bin_Unpack *bu)
{ {
assert(event != nullptr); assert(event != nullptr);
if (!bin_unpack_array_fixed(bu, 3)) { if (!bin_unpack_array_fixed(bu, 3, nullptr)) {
return false; return false;
} }

View File

@ -99,7 +99,7 @@ static bool tox_event_group_peer_status_unpack(
Tox_Event_Group_Peer_Status *event, Bin_Unpack *bu) Tox_Event_Group_Peer_Status *event, Bin_Unpack *bu)
{ {
assert(event != nullptr); assert(event != nullptr);
if (!bin_unpack_array_fixed(bu, 3)) { if (!bin_unpack_array_fixed(bu, 3, nullptr)) {
return false; return false;
} }

View File

@ -84,7 +84,7 @@ static bool tox_event_group_privacy_state_unpack(
Tox_Event_Group_Privacy_State *event, Bin_Unpack *bu) Tox_Event_Group_Privacy_State *event, Bin_Unpack *bu)
{ {
assert(event != nullptr); assert(event != nullptr);
if (!bin_unpack_array_fixed(bu, 2)) { if (!bin_unpack_array_fixed(bu, 2, nullptr)) {
return false; return false;
} }

View File

@ -135,7 +135,7 @@ static bool tox_event_group_private_message_unpack(
Tox_Event_Group_Private_Message *event, Bin_Unpack *bu) Tox_Event_Group_Private_Message *event, Bin_Unpack *bu)
{ {
assert(event != nullptr); assert(event != nullptr);
if (!bin_unpack_array_fixed(bu, 4)) { if (!bin_unpack_array_fixed(bu, 4, nullptr)) {
return false; return false;
} }

View File

@ -120,7 +120,7 @@ static bool tox_event_group_topic_unpack(
Tox_Event_Group_Topic *event, Bin_Unpack *bu) Tox_Event_Group_Topic *event, Bin_Unpack *bu)
{ {
assert(event != nullptr); assert(event != nullptr);
if (!bin_unpack_array_fixed(bu, 3)) { if (!bin_unpack_array_fixed(bu, 3, nullptr)) {
return false; return false;
} }

View File

@ -84,7 +84,7 @@ static bool tox_event_group_topic_lock_unpack(
Tox_Event_Group_Topic_Lock *event, Bin_Unpack *bu) Tox_Event_Group_Topic_Lock *event, Bin_Unpack *bu)
{ {
assert(event != nullptr); assert(event != nullptr);
if (!bin_unpack_array_fixed(bu, 2)) { if (!bin_unpack_array_fixed(bu, 2, nullptr)) {
return false; return false;
} }

View File

@ -84,7 +84,7 @@ static bool tox_event_group_voice_state_unpack(
Tox_Event_Group_Voice_State *event, Bin_Unpack *bu) Tox_Event_Group_Voice_State *event, Bin_Unpack *bu)
{ {
assert(event != nullptr); assert(event != nullptr);
if (!bin_unpack_array_fixed(bu, 2)) { if (!bin_unpack_array_fixed(bu, 2, nullptr)) {
return false; return false;
} }

View File

@ -802,6 +802,7 @@ static int addpeer(Group_Chats *g_c, uint32_t groupnumber, const uint8_t *real_p
if (peer_index != -1) { if (peer_index != -1) {
if (!pk_equal(g->group[peer_index].real_pk, real_pk)) { if (!pk_equal(g->group[peer_index].real_pk, real_pk)) {
LOGGER_ERROR(g_c->m->log, "peer public key is incorrect for peer %d", peer_number);
return -1; return -1;
} }
@ -3679,6 +3680,7 @@ static State_Load_Status load_conferences_helper(Group_Chats *g_c, const uint8_t
if (groupnumber == -1) { if (groupnumber == -1) {
// If this fails there's a serious problem, don't bother with cleanup // If this fails there's a serious problem, don't bother with cleanup
LOGGER_ERROR(g_c->m->log, "conference creation failed");
return STATE_LOAD_STATUS_ERROR; return STATE_LOAD_STATUS_ERROR;
} }
@ -3696,6 +3698,7 @@ static State_Load_Status load_conferences_helper(Group_Chats *g_c, const uint8_t
assert(ret); assert(ret);
} }
LOGGER_ERROR(g_c->m->log, "conference loading failed");
return STATE_LOAD_STATUS_ERROR; return STATE_LOAD_STATUS_ERROR;
} }
@ -3705,6 +3708,7 @@ static State_Load_Status load_conferences_helper(Group_Chats *g_c, const uint8_t
nullptr, true, false); nullptr, true, false);
if (peer_index == -1) { if (peer_index == -1) {
LOGGER_ERROR(g_c->m->log, "adding peer %d failed", g->peer_number);
return STATE_LOAD_STATUS_ERROR; return STATE_LOAD_STATUS_ERROR;
} }

View File

@ -108,10 +108,19 @@ static_assert(GCC_BUFFER_SIZE <= UINT16_MAX,
static_assert(MAX_GC_PACKET_CHUNK_SIZE < MAX_GC_PACKET_SIZE, static_assert(MAX_GC_PACKET_CHUNK_SIZE < MAX_GC_PACKET_SIZE,
"MAX_GC_PACKET_CHUNK_SIZE must be < MAX_GC_PACKET_SIZE"); "MAX_GC_PACKET_CHUNK_SIZE must be < MAX_GC_PACKET_SIZE");
static_assert(MAX_GC_PACKET_INCOMING_CHUNK_SIZE < MAX_GC_PACKET_SIZE,
"MAX_GC_PACKET_INCOMING_CHUNK_SIZE must be < MAX_GC_PACKET_SIZE");
static_assert(MAX_GC_PACKET_INCOMING_CHUNK_SIZE >= MAX_GC_PACKET_CHUNK_SIZE,
"MAX_GC_PACKET_INCOMING_CHUNK_SIZE must be >= MAX_GC_PACKET_CHUNK_SIZE");
// size of a lossless handshake packet - lossless packets can't/shouldn't be split up // size of a lossless handshake packet - lossless packets can't/shouldn't be split up
static_assert(MAX_GC_PACKET_CHUNK_SIZE >= 171, static_assert(MAX_GC_PACKET_CHUNK_SIZE >= 171,
"MAX_GC_PACKET_CHUNK_SIZE must be >= 171"); "MAX_GC_PACKET_CHUNK_SIZE must be >= 171");
static_assert(MAX_GC_PACKET_INCOMING_CHUNK_SIZE >= 171,
"MAX_GC_PACKET_INCOMING_CHUNK_SIZE must be >= 171");
// group_moderation constants assume this is the max packet size. // group_moderation constants assume this is the max packet size.
static_assert(MAX_GC_PACKET_SIZE >= 50000, static_assert(MAX_GC_PACKET_SIZE >= 50000,
"MAX_GC_PACKET_SIZE doesn't match constants in group_moderation.h"); "MAX_GC_PACKET_SIZE doesn't match constants in group_moderation.h");
@ -119,6 +128,9 @@ static_assert(MAX_GC_PACKET_SIZE >= 50000,
static_assert(MAX_GC_PACKET_SIZE <= UINT16_MAX - MAX_GC_PACKET_CHUNK_SIZE, static_assert(MAX_GC_PACKET_SIZE <= UINT16_MAX - MAX_GC_PACKET_CHUNK_SIZE,
"MAX_GC_PACKET_SIZE must be <= UINT16_MAX - MAX_GC_PACKET_CHUNK_SIZE"); "MAX_GC_PACKET_SIZE must be <= UINT16_MAX - MAX_GC_PACKET_CHUNK_SIZE");
static_assert(MAX_GC_PACKET_SIZE <= UINT16_MAX - MAX_GC_PACKET_INCOMING_CHUNK_SIZE,
"MAX_GC_PACKET_SIZE must be <= UINT16_MAX - MAX_GC_PACKET_INCOMING_CHUNK_SIZE");
/** Types of broadcast messages. */ /** Types of broadcast messages. */
typedef enum Group_Message_Type { typedef enum Group_Message_Type {
GC_MESSAGE_TYPE_NORMAL = 0x00, GC_MESSAGE_TYPE_NORMAL = 0x00,
@ -170,7 +182,11 @@ static void kill_group_friend_connection(const GC_Session *c, const GC_Chat *cha
uint16_t gc_get_wrapped_packet_size(uint16_t length, Net_Packet_Type packet_type) uint16_t gc_get_wrapped_packet_size(uint16_t length, Net_Packet_Type packet_type)
{ {
assert(length <= MAX_GC_PACKET_CHUNK_SIZE); if (packet_type == NET_PACKET_GC_LOSSY) {
assert(length <= MAX_GC_CUSTOM_LOSSY_PACKET_SIZE);
} else {
assert(length <= MAX_GC_PACKET_CHUNK_SIZE);
}
const uint16_t min_header_size = packet_type == NET_PACKET_GC_LOSSY const uint16_t min_header_size = packet_type == NET_PACKET_GC_LOSSY
? GC_MIN_LOSSY_PAYLOAD_SIZE ? GC_MIN_LOSSY_PAYLOAD_SIZE
@ -215,9 +231,13 @@ GC_Connection *get_gc_connection(const GC_Chat *chat, int peer_number)
} }
/** Returns the amount of empty padding a packet of designated length should have. */ /** Returns the amount of empty padding a packet of designated length should have. */
static uint16_t group_packet_padding_length(uint16_t length) static uint16_t group_packet_padding_length(uint16_t length, uint8_t net_packet_type)
{ {
return (MAX_GC_PACKET_CHUNK_SIZE - length) % GC_MAX_PACKET_PADDING; if (net_packet_type == NET_PACKET_GC_LOSSY) {
return (MAX_GC_CUSTOM_LOSSY_PACKET_SIZE - length) % GC_MAX_PACKET_PADDING;
} else {
return (MAX_GC_PACKET_CHUNK_SIZE - length) % GC_MAX_PACKET_PADDING;
}
} }
void gc_get_self_nick(const GC_Chat *chat, uint8_t *nick) void gc_get_self_nick(const GC_Chat *chat, uint8_t *nick)
@ -1473,7 +1493,7 @@ int group_packet_wrap(
uint16_t packet_size, const uint8_t *data, uint16_t length, uint64_t message_id, uint16_t packet_size, const uint8_t *data, uint16_t length, uint64_t message_id,
uint8_t gp_packet_type, uint8_t net_packet_type) uint8_t gp_packet_type, uint8_t net_packet_type)
{ {
const uint16_t padding_len = group_packet_padding_length(length); const uint16_t padding_len = group_packet_padding_length(length, net_packet_type);
const uint16_t min_packet_size = net_packet_type == NET_PACKET_GC_LOSSLESS const uint16_t min_packet_size = net_packet_type == NET_PACKET_GC_LOSSLESS
? length + padding_len + CRYPTO_MAC_SIZE + 1 + ENC_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + GC_MESSAGE_ID_BYTES + 1 ? length + padding_len + CRYPTO_MAC_SIZE + 1 + ENC_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + GC_MESSAGE_ID_BYTES + 1
: length + padding_len + CRYPTO_MAC_SIZE + 1 + ENC_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + 1; : length + padding_len + CRYPTO_MAC_SIZE + 1 + ENC_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + 1;
@ -1483,9 +1503,16 @@ int group_packet_wrap(
return -1; return -1;
} }
if (length > MAX_GC_PACKET_CHUNK_SIZE) { if (net_packet_type == NET_PACKET_GC_LOSSY) {
LOGGER_ERROR(log, "Packet payload size (%u) exceeds maximum (%u)", length, MAX_GC_PACKET_CHUNK_SIZE); if (length > MAX_GC_CUSTOM_LOSSY_PACKET_SIZE) {
return -1; LOGGER_ERROR(log, "Packet payload size (%u) exceeds maximum (%u)", length, MAX_GC_CUSTOM_LOSSY_PACKET_SIZE);
return -1;
}
} else {
if (length > MAX_GC_PACKET_CHUNK_SIZE) {
LOGGER_ERROR(log, "Packet payload size (%u) exceeds maximum (%u)", length, MAX_GC_PACKET_CHUNK_SIZE);
return -1;
}
} }
uint8_t *plain = (uint8_t *)malloc(packet_size); uint8_t *plain = (uint8_t *)malloc(packet_size);
@ -1551,7 +1578,7 @@ non_null()
static bool send_lossy_group_packet(const GC_Chat *chat, const GC_Connection *gconn, const uint8_t *data, static bool send_lossy_group_packet(const GC_Chat *chat, const GC_Connection *gconn, const uint8_t *data,
uint16_t length, uint8_t packet_type) uint16_t length, uint8_t packet_type)
{ {
assert(length <= MAX_GC_PACKET_CHUNK_SIZE); assert(length <= MAX_GC_CUSTOM_LOSSY_PACKET_SIZE);
if (!gconn->handshaked || gconn->pending_delete) { if (!gconn->handshaked || gconn->pending_delete) {
return false; return false;
@ -6255,13 +6282,13 @@ static int handle_gc_tcp_packet(void *object, int id, const uint8_t *packet, uin
if (length <= MIN_TCP_PACKET_SIZE) { if (length <= MIN_TCP_PACKET_SIZE) {
LOGGER_WARNING(m->log, "Got tcp packet with invalid length: %u (expected %u to %u)", length, LOGGER_WARNING(m->log, "Got tcp packet with invalid length: %u (expected %u to %u)", length,
MIN_TCP_PACKET_SIZE, MAX_GC_PACKET_CHUNK_SIZE + MIN_TCP_PACKET_SIZE + ENC_PUBLIC_KEY_SIZE); MIN_TCP_PACKET_SIZE, MAX_GC_PACKET_INCOMING_CHUNK_SIZE + MIN_TCP_PACKET_SIZE + ENC_PUBLIC_KEY_SIZE);
return -1; return -1;
} }
if (length > MAX_GC_PACKET_CHUNK_SIZE + MIN_TCP_PACKET_SIZE + ENC_PUBLIC_KEY_SIZE) { if (length > MAX_GC_PACKET_INCOMING_CHUNK_SIZE + MIN_TCP_PACKET_SIZE + ENC_PUBLIC_KEY_SIZE) {
LOGGER_WARNING(m->log, "Got tcp packet with invalid length: %u (expected %u to %u)", length, LOGGER_WARNING(m->log, "Got tcp packet with invalid length: %u (expected %u to %u)", length,
MIN_TCP_PACKET_SIZE, MAX_GC_PACKET_CHUNK_SIZE + MIN_TCP_PACKET_SIZE + ENC_PUBLIC_KEY_SIZE); MIN_TCP_PACKET_SIZE, MAX_GC_PACKET_INCOMING_CHUNK_SIZE + MIN_TCP_PACKET_SIZE + ENC_PUBLIC_KEY_SIZE);
return -1; return -1;
} }
@ -6336,13 +6363,13 @@ static int handle_gc_tcp_oob_packet(void *object, const uint8_t *public_key, uns
if (length <= GC_MIN_HS_PACKET_PAYLOAD_SIZE) { if (length <= GC_MIN_HS_PACKET_PAYLOAD_SIZE) {
LOGGER_WARNING(m->log, "Got tcp oob packet with invalid length: %u (expected %u to %u)", length, LOGGER_WARNING(m->log, "Got tcp oob packet with invalid length: %u (expected %u to %u)", length,
GC_MIN_HS_PACKET_PAYLOAD_SIZE, MAX_GC_PACKET_CHUNK_SIZE + CRYPTO_MAC_SIZE + CRYPTO_NONCE_SIZE); GC_MIN_HS_PACKET_PAYLOAD_SIZE, MAX_GC_PACKET_INCOMING_CHUNK_SIZE + CRYPTO_MAC_SIZE + CRYPTO_NONCE_SIZE);
return -1; return -1;
} }
if (length > MAX_GC_PACKET_CHUNK_SIZE + CRYPTO_MAC_SIZE + CRYPTO_NONCE_SIZE) { if (length > MAX_GC_PACKET_INCOMING_CHUNK_SIZE + CRYPTO_MAC_SIZE + CRYPTO_NONCE_SIZE) {
LOGGER_WARNING(m->log, "Got tcp oob packet with invalid length: %u (expected %u to %u)", length, LOGGER_WARNING(m->log, "Got tcp oob packet with invalid length: %u (expected %u to %u)", length,
GC_MIN_HS_PACKET_PAYLOAD_SIZE, MAX_GC_PACKET_CHUNK_SIZE + CRYPTO_MAC_SIZE + CRYPTO_NONCE_SIZE); GC_MIN_HS_PACKET_PAYLOAD_SIZE, MAX_GC_PACKET_INCOMING_CHUNK_SIZE + CRYPTO_MAC_SIZE + CRYPTO_NONCE_SIZE);
return -1; return -1;
} }
@ -6392,13 +6419,13 @@ static int handle_gc_udp_packet(void *object, const IP_Port *ipp, const uint8_t
if (length <= MIN_UDP_PACKET_SIZE) { if (length <= MIN_UDP_PACKET_SIZE) {
LOGGER_WARNING(m->log, "Got UDP packet with invalid length: %u (expected %u to %u)", length, LOGGER_WARNING(m->log, "Got UDP packet with invalid length: %u (expected %u to %u)", length,
MIN_UDP_PACKET_SIZE, MAX_GC_PACKET_CHUNK_SIZE + MIN_UDP_PACKET_SIZE + ENC_PUBLIC_KEY_SIZE); MIN_UDP_PACKET_SIZE, MAX_GC_PACKET_INCOMING_CHUNK_SIZE + MIN_UDP_PACKET_SIZE + ENC_PUBLIC_KEY_SIZE);
return -1; return -1;
} }
if (length > MAX_GC_PACKET_CHUNK_SIZE + MIN_UDP_PACKET_SIZE + ENC_PUBLIC_KEY_SIZE) { if (length > MAX_GC_PACKET_INCOMING_CHUNK_SIZE + MIN_UDP_PACKET_SIZE + ENC_PUBLIC_KEY_SIZE) {
LOGGER_WARNING(m->log, "Got UDP packet with invalid length: %u (expected %u to %u)", length, LOGGER_WARNING(m->log, "Got UDP packet with invalid length: %u (expected %u to %u)", length,
MIN_UDP_PACKET_SIZE, MAX_GC_PACKET_CHUNK_SIZE + MIN_UDP_PACKET_SIZE + ENC_PUBLIC_KEY_SIZE); MIN_UDP_PACKET_SIZE, MAX_GC_PACKET_INCOMING_CHUNK_SIZE + MIN_UDP_PACKET_SIZE + ENC_PUBLIC_KEY_SIZE);
return -1; return -1;
} }
@ -6961,12 +6988,12 @@ static bool ping_peer(const GC_Chat *chat, const GC_Connection *gconn)
if (!send_lossy_group_packet(chat, gconn, data, packed_len, GP_PING)) { if (!send_lossy_group_packet(chat, gconn, data, packed_len, GP_PING)) {
free(data); free(data);
return true; return false;
} }
free(data); free(data);
return false; return true;
} }
/** /**

View File

@ -28,11 +28,13 @@
* For an explanation on why this value was chosen, see the following link: https://archive.ph/vsCOG * For an explanation on why this value was chosen, see the following link: https://archive.ph/vsCOG
*/ */
#define MAX_GC_PACKET_CHUNK_SIZE 500 #define MAX_GC_PACKET_CHUNK_SIZE 500
/* Max size of an incoming packet chunk that is allowed */
#define MAX_GC_PACKET_INCOMING_CHUNK_SIZE 1372
#define MAX_GC_MESSAGE_SIZE GROUP_MAX_MESSAGE_LENGTH #define MAX_GC_MESSAGE_SIZE GROUP_MAX_MESSAGE_LENGTH
#define MAX_GC_MESSAGE_RAW_SIZE (MAX_GC_MESSAGE_SIZE + GC_MESSAGE_PSEUDO_ID_SIZE) #define MAX_GC_MESSAGE_RAW_SIZE (MAX_GC_MESSAGE_SIZE + GC_MESSAGE_PSEUDO_ID_SIZE)
#define MAX_GC_CUSTOM_LOSSLESS_PACKET_SIZE 1373 #define MAX_GC_CUSTOM_LOSSLESS_PACKET_SIZE 1373
#define MAX_GC_CUSTOM_LOSSY_PACKET_SIZE MAX_GC_PACKET_CHUNK_SIZE #define MAX_GC_CUSTOM_LOSSY_PACKET_SIZE 1000
#define MAX_GC_PASSWORD_SIZE 32 #define MAX_GC_PASSWORD_SIZE 32
#define MAX_GC_SAVED_INVITES 10 #define MAX_GC_SAVED_INVITES 10
#define MAX_GC_PEERS_DEFAULT 100 #define MAX_GC_PEERS_DEFAULT 100

View File

@ -366,7 +366,7 @@ static uint16_t reassemble_packet(const Logger *log, GC_Connection *gconn, uint8
// search backwards in recv array until we find an empty slot or a non-fragment packet type // search backwards in recv array until we find an empty slot or a non-fragment packet type
while (!array_entry_is_empty(entry) && entry->packet_type == GP_FRAGMENT) { while (!array_entry_is_empty(entry) && entry->packet_type == GP_FRAGMENT) {
assert(entry->data != nullptr); assert(entry->data != nullptr);
assert(entry->data_length <= MAX_GC_PACKET_CHUNK_SIZE); assert(entry->data_length <= MAX_GC_PACKET_INCOMING_CHUNK_SIZE);
const uint16_t diff = packet_length + entry->data_length; const uint16_t diff = packet_length + entry->data_length;

View File

@ -22,7 +22,7 @@
non_null() non_null()
static bool load_unpack_state_values(GC_Chat *chat, Bin_Unpack *bu) static bool load_unpack_state_values(GC_Chat *chat, Bin_Unpack *bu)
{ {
if (!bin_unpack_array_fixed(bu, 8)) { if (!bin_unpack_array_fixed(bu, 8, nullptr)) {
LOGGER_ERROR(chat->log, "Group state values array malformed"); LOGGER_ERROR(chat->log, "Group state values array malformed");
return false; return false;
} }
@ -58,15 +58,23 @@ static bool load_unpack_state_values(GC_Chat *chat, Bin_Unpack *bu)
non_null() non_null()
static bool load_unpack_state_bin(GC_Chat *chat, Bin_Unpack *bu) static bool load_unpack_state_bin(GC_Chat *chat, Bin_Unpack *bu)
{ {
if (!bin_unpack_array_fixed(bu, 5)) { if (!bin_unpack_array_fixed(bu, 5, nullptr)) {
LOGGER_ERROR(chat->log, "Group state binary array malformed"); LOGGER_ERROR(chat->log, "Group state binary array malformed");
return false; return false;
} }
if (!(bin_unpack_bin_fixed(bu, chat->shared_state_sig, SIGNATURE_SIZE) if (!bin_unpack_bin_fixed(bu, chat->shared_state_sig, SIGNATURE_SIZE)) {
&& bin_unpack_bin_fixed(bu, chat->shared_state.founder_public_key, EXT_PUBLIC_KEY_SIZE) LOGGER_ERROR(chat->log, "Failed to unpack shared state signature");
&& bin_unpack_bin_fixed(bu, chat->shared_state.group_name, chat->shared_state.group_name_len) return false;
&& bin_unpack_bin_fixed(bu, chat->shared_state.password, chat->shared_state.password_length) }
if (!bin_unpack_bin_fixed(bu, chat->shared_state.founder_public_key, EXT_PUBLIC_KEY_SIZE)) {
LOGGER_ERROR(chat->log, "Failed to unpack founder public key");
return false;
}
if (!(bin_unpack_bin_max(bu, chat->shared_state.group_name, &chat->shared_state.group_name_len, sizeof(chat->shared_state.group_name))
&& bin_unpack_bin_max(bu, chat->shared_state.password, &chat->shared_state.password_length, sizeof(chat->shared_state.password))
&& bin_unpack_bin_fixed(bu, chat->shared_state.mod_list_hash, MOD_MODERATION_HASH_SIZE))) { && bin_unpack_bin_fixed(bu, chat->shared_state.mod_list_hash, MOD_MODERATION_HASH_SIZE))) {
LOGGER_ERROR(chat->log, "Failed to unpack state binary data"); LOGGER_ERROR(chat->log, "Failed to unpack state binary data");
return false; return false;
@ -78,7 +86,7 @@ static bool load_unpack_state_bin(GC_Chat *chat, Bin_Unpack *bu)
non_null() non_null()
static bool load_unpack_topic_info(GC_Chat *chat, Bin_Unpack *bu) static bool load_unpack_topic_info(GC_Chat *chat, Bin_Unpack *bu)
{ {
if (!bin_unpack_array_fixed(bu, 6)) { if (!bin_unpack_array_fixed(bu, 6, nullptr)) {
LOGGER_ERROR(chat->log, "Group topic array malformed"); LOGGER_ERROR(chat->log, "Group topic array malformed");
return false; return false;
} }
@ -86,7 +94,7 @@ static bool load_unpack_topic_info(GC_Chat *chat, Bin_Unpack *bu)
if (!(bin_unpack_u32(bu, &chat->topic_info.version) if (!(bin_unpack_u32(bu, &chat->topic_info.version)
&& bin_unpack_u16(bu, &chat->topic_info.length) && bin_unpack_u16(bu, &chat->topic_info.length)
&& bin_unpack_u16(bu, &chat->topic_info.checksum) && bin_unpack_u16(bu, &chat->topic_info.checksum)
&& bin_unpack_bin_fixed(bu, chat->topic_info.topic, chat->topic_info.length) && bin_unpack_bin_max(bu, chat->topic_info.topic, &chat->topic_info.length, sizeof(chat->topic_info.topic))
&& bin_unpack_bin_fixed(bu, chat->topic_info.public_sig_key, SIG_PUBLIC_KEY_SIZE) && bin_unpack_bin_fixed(bu, chat->topic_info.public_sig_key, SIG_PUBLIC_KEY_SIZE)
&& bin_unpack_bin_fixed(bu, chat->topic_sig, SIGNATURE_SIZE))) { && bin_unpack_bin_fixed(bu, chat->topic_sig, SIGNATURE_SIZE))) {
LOGGER_ERROR(chat->log, "Failed to unpack topic info"); LOGGER_ERROR(chat->log, "Failed to unpack topic info");
@ -99,8 +107,9 @@ static bool load_unpack_topic_info(GC_Chat *chat, Bin_Unpack *bu)
non_null() non_null()
static bool load_unpack_mod_list(GC_Chat *chat, Bin_Unpack *bu) static bool load_unpack_mod_list(GC_Chat *chat, Bin_Unpack *bu)
{ {
if (!bin_unpack_array_fixed(bu, 2)) { uint32_t actual_size = 0;
LOGGER_ERROR(chat->log, "Group mod list array malformed"); if (!bin_unpack_array_fixed(bu, 2, &actual_size)) {
LOGGER_ERROR(chat->log, "Group mod list array malformed: %d != 2", actual_size);
return false; return false;
} }
@ -148,7 +157,7 @@ static bool load_unpack_mod_list(GC_Chat *chat, Bin_Unpack *bu)
non_null() non_null()
static bool load_unpack_keys(GC_Chat *chat, Bin_Unpack *bu) static bool load_unpack_keys(GC_Chat *chat, Bin_Unpack *bu)
{ {
if (!bin_unpack_array_fixed(bu, 4)) { if (!bin_unpack_array_fixed(bu, 4, nullptr)) {
LOGGER_ERROR(chat->log, "Group keys array malformed"); LOGGER_ERROR(chat->log, "Group keys array malformed");
return false; return false;
} }
@ -167,7 +176,7 @@ static bool load_unpack_keys(GC_Chat *chat, Bin_Unpack *bu)
non_null() non_null()
static bool load_unpack_self_info(GC_Chat *chat, Bin_Unpack *bu) static bool load_unpack_self_info(GC_Chat *chat, Bin_Unpack *bu)
{ {
if (!bin_unpack_array_fixed(bu, 4)) { if (!bin_unpack_array_fixed(bu, 4, nullptr)) {
LOGGER_ERROR(chat->log, "Group self info array malformed"); LOGGER_ERROR(chat->log, "Group self info array malformed");
return false; return false;
} }
@ -214,7 +223,7 @@ static bool load_unpack_self_info(GC_Chat *chat, Bin_Unpack *bu)
non_null() non_null()
static bool load_unpack_saved_peers(GC_Chat *chat, Bin_Unpack *bu) static bool load_unpack_saved_peers(GC_Chat *chat, Bin_Unpack *bu)
{ {
if (!bin_unpack_array_fixed(bu, 2)) { if (!bin_unpack_array_fixed(bu, 2, nullptr)) {
LOGGER_ERROR(chat->log, "Group saved peers array malformed"); LOGGER_ERROR(chat->log, "Group saved peers array malformed");
return false; return false;
} }
@ -256,8 +265,9 @@ static bool load_unpack_saved_peers(GC_Chat *chat, Bin_Unpack *bu)
bool gc_load_unpack_group(GC_Chat *chat, Bin_Unpack *bu) bool gc_load_unpack_group(GC_Chat *chat, Bin_Unpack *bu)
{ {
if (!bin_unpack_array_fixed(bu, 7)) { uint32_t actual_size;
LOGGER_ERROR(chat->log, "Group info array malformed"); if (!bin_unpack_array_fixed(bu, 7, &actual_size)) {
LOGGER_ERROR(chat->log, "Group info array malformed: %d != 7", actual_size);
return false; return false;
} }

View File

@ -1086,7 +1086,7 @@ void networking_poll(const Networking_Core *net, void *userdata)
} }
IP_Port ip_port; IP_Port ip_port;
uint8_t data[MAX_UDP_PACKET_SIZE]; uint8_t data[MAX_UDP_PACKET_SIZE] = {0};
uint32_t length; uint32_t length;
while (receivepacket(net->ns, net->mem, net->log, net->sock, &ip_port, data, &length) != -1) { while (receivepacket(net->ns, net->mem, net->log, net->sock, &ip_port, data, &length) != -1) {

View File

@ -52,7 +52,7 @@ int state_load(const Logger *log, state_load_cb *state_load_callback, void *oute
} }
case STATE_LOAD_STATUS_ERROR: { case STATE_LOAD_STATUS_ERROR: {
LOGGER_ERROR(log, "Error occcured in state file (type: %u).", type); LOGGER_ERROR(log, "Error occcured in state file (type: 0x%02x).", type);
return -1; return -1;
} }

View File

@ -858,12 +858,26 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error)
if (load_savedata_tox if (load_savedata_tox
&& tox_load(tox, tox_options_get_savedata_data(opts), tox_options_get_savedata_length(opts)) == -1) { && tox_load(tox, tox_options_get_savedata_data(opts), tox_options_get_savedata_length(opts)) == -1) {
kill_groupchats(tox->m->conferences_object);
kill_messenger(tox->m);
mono_time_free(tox->sys.mem, tox->mono_time);
tox_options_free(default_options);
tox_unlock(tox);
if (tox->mutex != nullptr) {
pthread_mutex_destroy(tox->mutex);
}
mem_delete(sys->mem, tox->mutex);
mem_delete(sys->mem, tox);
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT);
} else if (load_savedata_sk) { return nullptr;
}
if (load_savedata_sk) {
load_secret_key(tox->m->net_crypto, tox_options_get_savedata_data(opts)); load_secret_key(tox->m->net_crypto, tox_options_get_savedata_data(opts));
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_OK);
} else {
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_OK);
} }
m_callback_namechange(tox->m, tox_friend_name_handler); m_callback_namechange(tox->m, tox_friend_name_handler);
@ -913,6 +927,9 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error)
tox_options_free(default_options); tox_options_free(default_options);
tox_unlock(tox); tox_unlock(tox);
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_OK);
return tox; return tox;
} }

View File

@ -890,9 +890,6 @@ typedef enum Tox_Err_New {
* This function will bring the instance into a valid state. Running the event * This function will bring the instance into a valid state. Running the event
* loop with a new instance will operate correctly. * loop with a new instance will operate correctly.
* *
* If loading failed or succeeded only partially, the new or partially loaded
* instance is returned and an error code is set.
*
* @param options An options object as described above. If this parameter is * @param options An options object as described above. If this parameter is
* NULL, the default options are used. * NULL, the default options are used.
* *
@ -3310,7 +3307,7 @@ uint32_t tox_group_max_message_length(void);
/** /**
* Maximum length of a group custom lossy packet. * Maximum length of a group custom lossy packet.
*/ */
#define TOX_GROUP_MAX_CUSTOM_LOSSY_PACKET_LENGTH 500 #define TOX_GROUP_MAX_CUSTOM_LOSSY_PACKET_LENGTH 1000
uint32_t tox_group_max_custom_lossy_packet_length(void); uint32_t tox_group_max_custom_lossy_packet_length(void);

View File

@ -149,3 +149,20 @@ bool tox_dht_get_nodes(const Tox *tox, const uint8_t *public_key, const char *ip
return true; return true;
} }
uint16_t tox_dht_get_num_closelist(const Tox *tox) {
tox_lock(tox);
const uint16_t num_total = dht_get_num_closelist(tox->m->dht);
tox_unlock(tox);
return num_total;
}
uint16_t tox_dht_get_num_closelist_announce_capable(const Tox *tox){
tox_lock(tox);
const uint16_t num_cap = dht_get_num_closelist_announce_capable(tox->m->dht);
tox_unlock(tox);
return num_cap;
}

View File

@ -140,6 +140,22 @@ typedef enum Tox_Err_Dht_Get_Nodes {
bool tox_dht_get_nodes(const Tox *tox, const uint8_t *public_key, const char *ip, uint16_t port, bool tox_dht_get_nodes(const Tox *tox, const uint8_t *public_key, const char *ip, uint16_t port,
const uint8_t *target_public_key, Tox_Err_Dht_Get_Nodes *error); const uint8_t *target_public_key, Tox_Err_Dht_Get_Nodes *error);
/**
* This function returns the ratio of close dht nodes that are known to support announce/store.
* This function returns the number of DHT nodes in the closelist.
*
* @return number
*/
uint16_t tox_dht_get_num_closelist(const Tox *tox);
/**
* This function returns the number of DHT nodes in the closelist,
* that are capable to store annouce data (introduced in version 0.2.18).
*
* @return number
*/
uint16_t tox_dht_get_num_closelist_announce_capable(const Tox *tox);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -58,6 +58,9 @@ add_executable(tomato
./tox_ui_utils.hpp ./tox_ui_utils.hpp
./tox_ui_utils.cpp ./tox_ui_utils.cpp
./tox_dht_cap_histo.hpp
./tox_dht_cap_histo.cpp
./chat_gui4.hpp ./chat_gui4.hpp
./chat_gui4.cpp ./chat_gui4.cpp
) )

View File

@ -11,6 +11,7 @@ MainScreen::MainScreen(SDL_Renderer* renderer_, std::string save_path, std::stri
rmm(cr), rmm(cr),
mts(rmm), mts(rmm),
tc(save_path, save_password), tc(save_path, save_password),
tpi(tc.getTox()),
ad(tc), ad(tc),
tcm(cr, tc, tc), tcm(cr, tc, tc),
tmm(rmm, cr, tcm, tc, tc), tmm(rmm, cr, tcm, tc, tc),
@ -20,7 +21,8 @@ MainScreen::MainScreen(SDL_Renderer* renderer_, std::string save_path, std::stri
sdlrtu(renderer_), sdlrtu(renderer_),
cg(conf, rmm, cr, sdlrtu), cg(conf, rmm, cr, sdlrtu),
sw(conf), sw(conf),
tuiu(tc, conf) tuiu(tc, conf),
tdch(tpi)
{ {
tel.subscribeAll(tc); tel.subscribeAll(tc);
@ -44,6 +46,7 @@ MainScreen::MainScreen(SDL_Renderer* renderer_, std::string save_path, std::stri
g_provideInstance<RegistryMessageModel>("RegistryMessageModel", "host", &rmm); g_provideInstance<RegistryMessageModel>("RegistryMessageModel", "host", &rmm);
g_provideInstance<ToxI>("ToxI", "host", &tc); g_provideInstance<ToxI>("ToxI", "host", &tc);
g_provideInstance<ToxPrivateI>("ToxPrivateI", "host", &tpi);
g_provideInstance<ToxEventProviderI>("ToxEventProviderI", "host", &tc); g_provideInstance<ToxEventProviderI>("ToxEventProviderI", "host", &tc);
g_provideInstance<ToxContactModel2>("ToxContactModel2", "host", &tcm); g_provideInstance<ToxContactModel2>("ToxContactModel2", "host", &tcm);
@ -107,12 +110,14 @@ Screen* MainScreen::poll(bool& quit) {
} }
pm.tick(time_delta); pm.tick(time_delta);
tdch.tick(time_delta);
mts.iterate(); mts.iterate();
cg.render(); cg.render();
sw.render(); sw.render();
tuiu.render(); tuiu.render();
tdch.render();
if constexpr (false) { if constexpr (false) {
ImGui::ShowDemoWindow(); ImGui::ShowDemoWindow();

View File

@ -8,6 +8,7 @@
#include <solanaceae/message3/message_time_sort.hpp> #include <solanaceae/message3/message_time_sort.hpp>
#include <solanaceae/plugin/plugin_manager.hpp> #include <solanaceae/plugin/plugin_manager.hpp>
#include <solanaceae/toxcore/tox_event_logger.hpp> #include <solanaceae/toxcore/tox_event_logger.hpp>
#include "./tox_private_impl.hpp"
#include <solanaceae/tox_contacts/tox_contact_model2.hpp> #include <solanaceae/tox_contacts/tox_contact_model2.hpp>
#include <solanaceae/tox_messages/tox_message_manager.hpp> #include <solanaceae/tox_messages/tox_message_manager.hpp>
@ -23,6 +24,7 @@
#include "./chat_gui4.hpp" #include "./chat_gui4.hpp"
#include "./settings_window.hpp" #include "./settings_window.hpp"
#include "./tox_ui_utils.hpp" #include "./tox_ui_utils.hpp"
#include "./tox_dht_cap_histo.hpp"
#include <string> #include <string>
#include <iostream> #include <iostream>
@ -47,6 +49,7 @@ struct MainScreen final : public Screen {
ToxEventLogger tel{std::cout}; ToxEventLogger tel{std::cout};
ToxClient tc; ToxClient tc;
ToxPrivateImpl tpi;
AutoDirty ad; AutoDirty ad;
ToxContactModel2 tcm; ToxContactModel2 tcm;
ToxMessageManager tmm; ToxMessageManager tmm;
@ -61,6 +64,7 @@ struct MainScreen final : public Screen {
ChatGui4 cg; ChatGui4 cg;
SettingsWindow sw; SettingsWindow sw;
ToxUIUtils tuiu; ToxUIUtils tuiu;
ToxDHTCapHisto tdch;
MainScreen(SDL_Renderer* renderer_, std::string save_path, std::string save_password, std::vector<std::string> plugins); MainScreen(SDL_Renderer* renderer_, std::string save_path, std::string save_password, std::vector<std::string> plugins);
~MainScreen(void); ~MainScreen(void);

69
src/tox_dht_cap_histo.cpp Normal file
View File

@ -0,0 +1,69 @@
#include "./tox_dht_cap_histo.hpp"
#include <imgui/imgui.h>
void ToxDHTCapHisto::tick(float time_delta) {
if (!_enabled) {
return;
}
_time_since_last_add += time_delta;
if (_time_since_last_add >= _value_add_interval) {
_time_since_last_add = 0.f; // very loose
const auto total = _tpi.toxDHTGetNumCloselist();
const auto with_cap = _tpi.toxDHTGetNumCloselistAnnounceCapable();
if (total == 0 || with_cap == 0) {
_ratios.push_back(0.f);
} else {
_ratios.push_back(float(with_cap) / float(total));
}
// TODO: limit
while (_ratios.size() > 5*60) {
_ratios.erase(_ratios.begin());
}
}
}
void ToxDHTCapHisto::render(void) {
{ // main window menubar injection
// assumes the window "tomato" was rendered already by cg
if (ImGui::Begin("tomato")) {
if (ImGui::BeginMenuBar()) {
ImGui::Separator();
if (ImGui::BeginMenu("Tox")) {
ImGui::SeparatorText("DHT diagnostics");
ImGui::Checkbox("enabled", &_enabled);
if (ImGui::MenuItem("show DHT announce capability histogram")) {
_show_window = true;
}
ImGui::EndMenu();
}
ImGui::EndMenuBar();
}
}
ImGui::End();
}
if (_show_window) {
if (ImGui::Begin("Tox DHT announce capability histogram", &_show_window)) {
if (_enabled) {
ImGui::PlotHistogram("##histogram", _ratios.data(), _ratios.size(), 0, nullptr, 0.f, 1.f, {-1, -1});
} else {
ImGui::TextUnformatted("logging disabled!");
if (ImGui::Button("enable")) {
_enabled = true;
}
}
}
ImGui::End();
}
}

22
src/tox_dht_cap_histo.hpp Normal file
View File

@ -0,0 +1,22 @@
#pragma once
#include <solanaceae/toxcore/tox_private_interface.hpp>
#include <vector>
class ToxDHTCapHisto {
ToxPrivateI& _tpi;
bool _enabled {true};
bool _show_window {false};
std::vector<float> _ratios;
const float _value_add_interval {1.f}; // every second
float _time_since_last_add {0.f};
public:
ToxDHTCapHisto(ToxPrivateI& tpi) : _tpi(tpi) {}
void tick(float time_delta);
void render(void);
};

19
src/tox_private_impl.hpp Normal file
View File

@ -0,0 +1,19 @@
#pragma once
#include <tox/tox_private.h>
#include <solanaceae/toxcore/tox_private_interface.hpp>
struct ToxPrivateImpl : public ToxPrivateI {
Tox* _tox = nullptr;
ToxPrivateImpl(Tox* tox) : _tox(tox) {}
virtual ~ToxPrivateImpl(void) {}
uint16_t toxDHTGetNumCloselist(void) override {
return tox_dht_get_num_closelist(_tox);
}
uint16_t toxDHTGetNumCloselistAnnounceCapable(void) override {
return tox_dht_get_num_closelist_announce_capable(_tox);
}
};

View File

@ -1,5 +1,7 @@
#include "./tox_ui_utils.hpp" #include "./tox_ui_utils.hpp"
#include <tox/tox.h>
#include <solanaceae/toxcore/tox_interface.hpp> #include <solanaceae/toxcore/tox_interface.hpp>
#include <solanaceae/toxcore/utils.hpp> #include <solanaceae/toxcore/utils.hpp>
@ -21,6 +23,8 @@ void ToxUIUtils::render(void) {
if (ImGui::BeginMenuBar()) { if (ImGui::BeginMenuBar()) {
ImGui::Separator(); ImGui::Separator();
if (ImGui::BeginMenu("Tox")) { if (ImGui::BeginMenu("Tox")) {
ImGui::SeparatorText("Friends/Groups");
if (ImGui::MenuItem("add Friend by ID")) { if (ImGui::MenuItem("add Friend by ID")) {
_show_add_friend_window = true; _show_add_friend_window = true;
} }