Merge commit 'aae086cc650e42eec1eea8db28cd01fa868d7f90'
This commit is contained in:
135
external/toxcore/c-toxcore/toxcore/BUILD.bazel
vendored
135
external/toxcore/c-toxcore/toxcore/BUILD.bazel
vendored
@ -21,7 +21,9 @@ cc_test(
|
||||
size = "small",
|
||||
srcs = ["test_util_test.cc"],
|
||||
deps = [
|
||||
":crypto_core",
|
||||
":crypto_core_test_util",
|
||||
":test_util",
|
||||
"@com_google_googletest//:gtest",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
@ -154,6 +156,7 @@ cc_test(
|
||||
deps = [
|
||||
":bin_pack",
|
||||
":bin_unpack",
|
||||
":logger",
|
||||
"@com_google_googletest//:gtest",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
@ -167,10 +170,25 @@ cc_library(
|
||||
deps = [
|
||||
":attributes",
|
||||
":ccompat",
|
||||
":util",
|
||||
"@libsodium",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "crypto_core_pack",
|
||||
srcs = ["crypto_core_pack.c"],
|
||||
hdrs = ["crypto_core_pack.h"],
|
||||
visibility = ["//c-toxcore:__subpackages__"],
|
||||
deps = [
|
||||
":attributes",
|
||||
":bin_pack",
|
||||
":bin_unpack",
|
||||
":ccompat",
|
||||
":crypto_core",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "crypto_core_test_util",
|
||||
testonly = True,
|
||||
@ -222,6 +240,7 @@ cc_library(
|
||||
srcs = ["state.c"],
|
||||
hdrs = ["state.h"],
|
||||
deps = [
|
||||
":attributes",
|
||||
":ccompat",
|
||||
":logger",
|
||||
],
|
||||
@ -271,6 +290,7 @@ cc_library(
|
||||
"//c-toxcore/toxav:__pkg__",
|
||||
],
|
||||
deps = [
|
||||
":attributes",
|
||||
":ccompat",
|
||||
":crypto_core",
|
||||
":logger",
|
||||
@ -291,6 +311,8 @@ cc_library(
|
||||
"//c-toxcore/toxav:__pkg__",
|
||||
],
|
||||
deps = [
|
||||
":attributes",
|
||||
":bin_pack",
|
||||
":ccompat",
|
||||
":crypto_core",
|
||||
":logger",
|
||||
@ -332,6 +354,7 @@ cc_library(
|
||||
srcs = ["timed_auth.c"],
|
||||
hdrs = ["timed_auth.h"],
|
||||
deps = [
|
||||
":attributes",
|
||||
":ccompat",
|
||||
":crypto_core",
|
||||
":mono_time",
|
||||
@ -343,6 +366,7 @@ cc_library(
|
||||
srcs = ["ping_array.c"],
|
||||
hdrs = ["ping_array.h"],
|
||||
deps = [
|
||||
":attributes",
|
||||
":ccompat",
|
||||
":crypto_core",
|
||||
":mem",
|
||||
@ -356,6 +380,7 @@ cc_test(
|
||||
size = "small",
|
||||
srcs = ["ping_array_test.cc"],
|
||||
deps = [
|
||||
":crypto_core_test_util",
|
||||
":mem_test_util",
|
||||
":mono_time",
|
||||
":ping_array",
|
||||
@ -374,10 +399,12 @@ cc_library(
|
||||
"//c-toxcore/testing:__pkg__",
|
||||
],
|
||||
deps = [
|
||||
":attributes",
|
||||
":ccompat",
|
||||
":crypto_core",
|
||||
":network",
|
||||
":util",
|
||||
"@psocket",
|
||||
],
|
||||
)
|
||||
|
||||
@ -422,6 +449,7 @@ cc_library(
|
||||
":DHT",
|
||||
":crypto_core",
|
||||
":crypto_core_test_util",
|
||||
":network",
|
||||
":network_test_util",
|
||||
":test_util",
|
||||
],
|
||||
@ -435,8 +463,13 @@ cc_test(
|
||||
":DHT",
|
||||
":DHT_test_util",
|
||||
":crypto_core",
|
||||
":crypto_core_test_util",
|
||||
":logger",
|
||||
":mem_test_util",
|
||||
":mono_time",
|
||||
":network",
|
||||
":network_test_util",
|
||||
":test_util",
|
||||
"@com_google_googletest//:gtest",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
@ -460,10 +493,13 @@ cc_library(
|
||||
visibility = ["//c-toxcore/auto_tests:__pkg__"],
|
||||
deps = [
|
||||
":DHT",
|
||||
":attributes",
|
||||
":ccompat",
|
||||
":crypto_core",
|
||||
":logger",
|
||||
":mem",
|
||||
":mono_time",
|
||||
":network",
|
||||
":shared_key_cache",
|
||||
":util",
|
||||
],
|
||||
@ -476,7 +512,11 @@ cc_library(
|
||||
visibility = ["//c-toxcore/auto_tests:__pkg__"],
|
||||
deps = [
|
||||
":DHT",
|
||||
":attributes",
|
||||
":ccompat",
|
||||
":crypto_core",
|
||||
":logger",
|
||||
":mono_time",
|
||||
":network",
|
||||
":timed_auth",
|
||||
],
|
||||
@ -503,9 +543,16 @@ cc_library(
|
||||
"//c-toxcore/other/bootstrap_daemon:__pkg__",
|
||||
],
|
||||
deps = [
|
||||
":DHT",
|
||||
":LAN_discovery",
|
||||
":attributes",
|
||||
":ccompat",
|
||||
":crypto_core",
|
||||
":forwarding",
|
||||
":logger",
|
||||
":mem",
|
||||
":mono_time",
|
||||
":network",
|
||||
":shared_key_cache",
|
||||
":timed_auth",
|
||||
":util",
|
||||
@ -521,6 +568,7 @@ cc_library(
|
||||
":attributes",
|
||||
":ccompat",
|
||||
":crypto_core",
|
||||
":logger",
|
||||
":mem",
|
||||
":network",
|
||||
],
|
||||
@ -541,13 +589,18 @@ cc_library(
|
||||
],
|
||||
deps = [
|
||||
":TCP_common",
|
||||
":attributes",
|
||||
":ccompat",
|
||||
":crypto_core",
|
||||
":forwarding",
|
||||
":list",
|
||||
":logger",
|
||||
":mem",
|
||||
":mono_time",
|
||||
":network",
|
||||
":onion",
|
||||
":util",
|
||||
"@psocket",
|
||||
],
|
||||
)
|
||||
|
||||
@ -558,9 +611,12 @@ cc_library(
|
||||
visibility = ["//c-toxcore/auto_tests:__pkg__"],
|
||||
deps = [
|
||||
":TCP_common",
|
||||
":attributes",
|
||||
":ccompat",
|
||||
":crypto_core",
|
||||
":forwarding",
|
||||
":logger",
|
||||
":mem",
|
||||
":mono_time",
|
||||
":network",
|
||||
":util",
|
||||
@ -576,10 +632,15 @@ cc_library(
|
||||
":DHT",
|
||||
":TCP_client",
|
||||
":TCP_common",
|
||||
":attributes",
|
||||
":ccompat",
|
||||
":crypto_core",
|
||||
":forwarding",
|
||||
":list",
|
||||
":logger",
|
||||
":mem",
|
||||
":mono_time",
|
||||
":network",
|
||||
":onion",
|
||||
":util",
|
||||
],
|
||||
@ -604,12 +665,18 @@ cc_library(
|
||||
deps = [
|
||||
":DHT",
|
||||
":LAN_discovery",
|
||||
":TCP_client",
|
||||
":TCP_connection",
|
||||
":attributes",
|
||||
":ccompat",
|
||||
":crypto_core",
|
||||
":list",
|
||||
":logger",
|
||||
":mem",
|
||||
":mono_time",
|
||||
":network",
|
||||
":util",
|
||||
"@pthread",
|
||||
],
|
||||
)
|
||||
|
||||
@ -625,9 +692,13 @@ cc_library(
|
||||
deps = [
|
||||
":DHT",
|
||||
":LAN_discovery",
|
||||
":attributes",
|
||||
":ccompat",
|
||||
":crypto_core",
|
||||
":logger",
|
||||
":mem",
|
||||
":mono_time",
|
||||
":network",
|
||||
":onion",
|
||||
":shared_key_cache",
|
||||
":timed_auth",
|
||||
@ -647,8 +718,12 @@ cc_library(
|
||||
deps = [
|
||||
":DHT",
|
||||
":LAN_discovery",
|
||||
":attributes",
|
||||
":ccompat",
|
||||
":crypto_core",
|
||||
":logger",
|
||||
":mono_time",
|
||||
":network",
|
||||
":util",
|
||||
],
|
||||
)
|
||||
@ -658,9 +733,13 @@ cc_test(
|
||||
size = "small",
|
||||
srcs = ["group_announce_test.cc"],
|
||||
deps = [
|
||||
":DHT",
|
||||
":crypto_core",
|
||||
":group_announce",
|
||||
":logger",
|
||||
":mem_test_util",
|
||||
":mono_time",
|
||||
":network",
|
||||
"@com_google_googletest//:gtest",
|
||||
"@com_google_googletest//:gtest_main",
|
||||
],
|
||||
@ -689,9 +768,16 @@ cc_library(
|
||||
"//c-toxcore/other/bootstrap_daemon:__pkg__",
|
||||
],
|
||||
deps = [
|
||||
":DHT",
|
||||
":attributes",
|
||||
":ccompat",
|
||||
":crypto_core",
|
||||
":group_announce",
|
||||
":logger",
|
||||
":mono_time",
|
||||
":network",
|
||||
":onion_announce",
|
||||
":timed_auth",
|
||||
],
|
||||
)
|
||||
|
||||
@ -703,13 +789,21 @@ cc_library(
|
||||
deps = [
|
||||
":DHT",
|
||||
":LAN_discovery",
|
||||
":TCP_connection",
|
||||
":attributes",
|
||||
":ccompat",
|
||||
":crypto_core",
|
||||
":group_announce",
|
||||
":group_onion_announce",
|
||||
":logger",
|
||||
":mem",
|
||||
":mono_time",
|
||||
":net_crypto",
|
||||
":network",
|
||||
":onion",
|
||||
":onion_announce",
|
||||
":ping_array",
|
||||
":timed_auth",
|
||||
":util",
|
||||
],
|
||||
)
|
||||
@ -722,9 +816,16 @@ cc_library(
|
||||
deps = [
|
||||
":DHT",
|
||||
":LAN_discovery",
|
||||
":TCP_connection",
|
||||
":attributes",
|
||||
":ccompat",
|
||||
":crypto_core",
|
||||
":logger",
|
||||
":mono_time",
|
||||
":net_crypto",
|
||||
":network",
|
||||
":onion",
|
||||
":onion_announce",
|
||||
":onion_client",
|
||||
":util",
|
||||
],
|
||||
@ -740,9 +841,14 @@ cc_library(
|
||||
"//c-toxcore/testing:__pkg__",
|
||||
],
|
||||
deps = [
|
||||
":attributes",
|
||||
":ccompat",
|
||||
":crypto_core",
|
||||
":friend_connection",
|
||||
":network",
|
||||
":onion",
|
||||
":onion_announce",
|
||||
":onion_client",
|
||||
":util",
|
||||
],
|
||||
)
|
||||
@ -753,9 +859,11 @@ cc_library(
|
||||
hdrs = ["group_moderation.h"],
|
||||
deps = [
|
||||
":DHT",
|
||||
":attributes",
|
||||
":ccompat",
|
||||
":crypto_core",
|
||||
":logger",
|
||||
":mem",
|
||||
":mono_time",
|
||||
":network",
|
||||
":util",
|
||||
@ -768,6 +876,7 @@ cc_test(
|
||||
size = "small",
|
||||
srcs = ["group_moderation_test.cc"],
|
||||
deps = [
|
||||
":DHT",
|
||||
":crypto_core",
|
||||
":crypto_core_test_util",
|
||||
":group_moderation",
|
||||
@ -816,13 +925,16 @@ cc_library(
|
||||
deps = [
|
||||
":DHT",
|
||||
":LAN_discovery",
|
||||
":TCP_client",
|
||||
":TCP_connection",
|
||||
":TCP_server",
|
||||
":announce",
|
||||
":attributes",
|
||||
":bin_pack",
|
||||
":bin_unpack",
|
||||
":ccompat",
|
||||
":crypto_core",
|
||||
":crypto_core_pack",
|
||||
":forwarding",
|
||||
":friend_connection",
|
||||
":friend_requests",
|
||||
@ -830,10 +942,13 @@ cc_library(
|
||||
":group_moderation",
|
||||
":group_onion_announce",
|
||||
":logger",
|
||||
":mem",
|
||||
":mono_time",
|
||||
":net_crypto",
|
||||
":network",
|
||||
":onion",
|
||||
":onion_announce",
|
||||
":onion_client",
|
||||
":state",
|
||||
":util",
|
||||
"@libsodium",
|
||||
@ -846,9 +961,16 @@ cc_library(
|
||||
hdrs = ["group.h"],
|
||||
visibility = ["//c-toxcore/toxav:__pkg__"],
|
||||
deps = [
|
||||
":DHT",
|
||||
":Messenger",
|
||||
":attributes",
|
||||
":ccompat",
|
||||
":crypto_core",
|
||||
":friend_connection",
|
||||
":logger",
|
||||
":mono_time",
|
||||
":net_crypto",
|
||||
":network",
|
||||
":state",
|
||||
":util",
|
||||
],
|
||||
@ -868,15 +990,25 @@ cc_library(
|
||||
],
|
||||
visibility = ["//c-toxcore:__subpackages__"],
|
||||
deps = [
|
||||
":DHT",
|
||||
":Messenger",
|
||||
":TCP_client",
|
||||
":attributes",
|
||||
":ccompat",
|
||||
":crypto_core",
|
||||
":friend_requests",
|
||||
":group",
|
||||
":group_moderation",
|
||||
":logger",
|
||||
":mem",
|
||||
":mono_time",
|
||||
":net_crypto",
|
||||
":network",
|
||||
":onion_client",
|
||||
":state",
|
||||
":util",
|
||||
"//c-toxcore/toxencryptsave:defines",
|
||||
"@pthread",
|
||||
],
|
||||
)
|
||||
|
||||
@ -937,6 +1069,7 @@ cc_library(
|
||||
":bin_pack",
|
||||
":bin_unpack",
|
||||
":ccompat",
|
||||
":logger",
|
||||
":mem",
|
||||
":tox",
|
||||
":tox_pack",
|
||||
@ -976,7 +1109,9 @@ cc_library(
|
||||
hdrs = ["tox_dispatch.h"],
|
||||
visibility = ["//c-toxcore:__subpackages__"],
|
||||
deps = [
|
||||
":attributes",
|
||||
":ccompat",
|
||||
":tox",
|
||||
":tox_events",
|
||||
],
|
||||
)
|
||||
|
269
external/toxcore/c-toxcore/toxcore/DHT.c
vendored
269
external/toxcore/c-toxcore/toxcore/DHT.c
vendored
@ -9,11 +9,11 @@
|
||||
#include "DHT.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <limits.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "LAN_discovery.h"
|
||||
#include "attributes.h"
|
||||
#include "bin_pack.h"
|
||||
#include "ccompat.h"
|
||||
#include "crypto_core.h"
|
||||
@ -84,7 +84,7 @@ struct DHT_Friend {
|
||||
static const DHT_Friend empty_dht_friend = {{0}};
|
||||
const Node_format empty_node_format = {{0}};
|
||||
|
||||
static_assert(sizeof (empty_dht_friend.lock_flags) * 8 == DHT_FRIEND_MAX_LOCKS, "Bitfield size and number of locks don't match");
|
||||
static_assert(sizeof(empty_dht_friend.lock_flags) * 8 == DHT_FRIEND_MAX_LOCKS, "Bitfield size and number of locks don't match");
|
||||
|
||||
typedef struct Cryptopacket_Handler {
|
||||
cryptopacket_handler_cb *function;
|
||||
@ -363,85 +363,6 @@ int packed_node_size(Family ip_family)
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
/** @brief Packs an IP structure.
|
||||
*
|
||||
* It's the caller's responsibility to make sure `is_ipv4` tells the truth. This
|
||||
* function is an implementation detail of @ref bin_pack_ip_port.
|
||||
*
|
||||
* @param is_ipv4 whether this IP is an IP4 or IP6.
|
||||
*
|
||||
* @retval true on success.
|
||||
*/
|
||||
non_null()
|
||||
static bool bin_pack_ip(Bin_Pack *bp, const IP *ip, bool is_ipv4)
|
||||
{
|
||||
if (is_ipv4) {
|
||||
return bin_pack_bin_b(bp, ip->ip.v4.uint8, SIZE_IP4);
|
||||
} else {
|
||||
return bin_pack_bin_b(bp, ip->ip.v6.uint8, SIZE_IP6);
|
||||
}
|
||||
}
|
||||
|
||||
/** @brief Packs an IP_Port structure.
|
||||
*
|
||||
* @retval true on success.
|
||||
*/
|
||||
non_null()
|
||||
static bool bin_pack_ip_port(Bin_Pack *bp, const Logger *logger, const IP_Port *ip_port)
|
||||
{
|
||||
bool is_ipv4;
|
||||
uint8_t family;
|
||||
|
||||
if (net_family_is_ipv4(ip_port->ip.family)) {
|
||||
// TODO(irungentoo): use functions to convert endianness
|
||||
is_ipv4 = true;
|
||||
family = TOX_AF_INET;
|
||||
} else if (net_family_is_tcp_ipv4(ip_port->ip.family)) {
|
||||
is_ipv4 = true;
|
||||
family = TOX_TCP_INET;
|
||||
} else if (net_family_is_ipv6(ip_port->ip.family)) {
|
||||
is_ipv4 = false;
|
||||
family = TOX_AF_INET6;
|
||||
} else if (net_family_is_tcp_ipv6(ip_port->ip.family)) {
|
||||
is_ipv4 = false;
|
||||
family = TOX_TCP_INET6;
|
||||
} else {
|
||||
Ip_Ntoa ip_str;
|
||||
// TODO(iphydf): Find out why we're trying to pack invalid IPs, stop
|
||||
// doing that, and turn this into an error.
|
||||
LOGGER_TRACE(logger, "cannot pack invalid IP: %s", net_ip_ntoa(&ip_port->ip, &ip_str));
|
||||
return false;
|
||||
}
|
||||
|
||||
return bin_pack_u08_b(bp, family)
|
||||
&& bin_pack_ip(bp, &ip_port->ip, is_ipv4)
|
||||
&& bin_pack_u16_b(bp, net_ntohs(ip_port->port));
|
||||
}
|
||||
|
||||
non_null()
|
||||
static bool bin_pack_ip_port_handler(Bin_Pack *bp, const Logger *logger, const void *obj)
|
||||
{
|
||||
const IP_Port *ip_port = (const IP_Port *)obj;
|
||||
return bin_pack_ip_port(bp, logger, ip_port);
|
||||
}
|
||||
|
||||
int pack_ip_port(const Logger *logger, uint8_t *data, uint16_t length, const IP_Port *ip_port)
|
||||
{
|
||||
const uint32_t size = bin_pack_obj_size(bin_pack_ip_port_handler, logger, ip_port);
|
||||
|
||||
if (size > length) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!bin_pack_obj(bin_pack_ip_port_handler, logger, ip_port, data, length)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
assert(size < INT_MAX);
|
||||
return (int)size;
|
||||
}
|
||||
|
||||
int dht_create_packet(const Memory *mem, const Random *rng,
|
||||
const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE],
|
||||
const uint8_t *shared_key, const uint8_t type,
|
||||
@ -478,72 +399,12 @@ int dht_create_packet(const Memory *mem, const Random *rng,
|
||||
return 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + encrypted_length;
|
||||
}
|
||||
|
||||
int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length, bool tcp_enabled)
|
||||
{
|
||||
if (data == nullptr) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool is_ipv4;
|
||||
Family host_family;
|
||||
|
||||
if (data[0] == TOX_AF_INET) {
|
||||
is_ipv4 = true;
|
||||
host_family = net_family_ipv4();
|
||||
} else if (data[0] == TOX_TCP_INET) {
|
||||
if (!tcp_enabled) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
is_ipv4 = true;
|
||||
host_family = net_family_tcp_ipv4();
|
||||
} else if (data[0] == TOX_AF_INET6) {
|
||||
is_ipv4 = false;
|
||||
host_family = net_family_ipv6();
|
||||
} else if (data[0] == TOX_TCP_INET6) {
|
||||
if (!tcp_enabled) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
is_ipv4 = false;
|
||||
host_family = net_family_tcp_ipv6();
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
|
||||
ipport_reset(ip_port);
|
||||
|
||||
if (is_ipv4) {
|
||||
const uint32_t size = 1 + SIZE_IP4 + sizeof(uint16_t);
|
||||
|
||||
if (size > length) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
ip_port->ip.family = host_family;
|
||||
memcpy(&ip_port->ip.ip.v4, data + 1, SIZE_IP4);
|
||||
memcpy(&ip_port->port, data + 1 + SIZE_IP4, sizeof(uint16_t));
|
||||
return size;
|
||||
} else {
|
||||
const uint32_t size = 1 + SIZE_IP6 + sizeof(uint16_t);
|
||||
|
||||
if (size > length) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
ip_port->ip.family = host_family;
|
||||
memcpy(&ip_port->ip.ip.v6, data + 1, SIZE_IP6);
|
||||
memcpy(&ip_port->port, data + 1 + SIZE_IP6, sizeof(uint16_t));
|
||||
return size;
|
||||
}
|
||||
}
|
||||
|
||||
/** @brief Pack a single node from a node array.
|
||||
*
|
||||
* @retval true on success.
|
||||
*/
|
||||
non_null()
|
||||
static bool bin_pack_node_handler(Bin_Pack *bp, const Logger *logger, const void *arr, uint32_t index)
|
||||
static bool bin_pack_node_handler(const void *arr, uint32_t index, const Logger *logger, Bin_Pack *bp)
|
||||
{
|
||||
const Node_format *nodes = (const Node_format *)arr;
|
||||
return bin_pack_ip_port(bp, logger, &nodes[index].ip_port)
|
||||
@ -552,8 +413,8 @@ static bool bin_pack_node_handler(Bin_Pack *bp, const Logger *logger, const void
|
||||
|
||||
int pack_nodes(const Logger *logger, uint8_t *data, uint16_t length, const Node_format *nodes, uint16_t number)
|
||||
{
|
||||
const uint32_t size = bin_pack_obj_array_size(bin_pack_node_handler, logger, nodes, number);
|
||||
if (!bin_pack_obj_array(bin_pack_node_handler, logger, nodes, number, data, length)) {
|
||||
const uint32_t size = bin_pack_obj_array_b_size(bin_pack_node_handler, nodes, number, logger);
|
||||
if (!bin_pack_obj_array_b(bin_pack_node_handler, nodes, number, logger, data, length)) {
|
||||
return -1;
|
||||
}
|
||||
return size;
|
||||
@ -585,7 +446,7 @@ int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed
|
||||
#ifndef NDEBUG
|
||||
const uint32_t increment = ipp_size + CRYPTO_PUBLIC_KEY_SIZE;
|
||||
assert(increment == PACKED_NODE_SIZE_IP4 || increment == PACKED_NODE_SIZE_IP6);
|
||||
#endif
|
||||
#endif /* NDEBUG */
|
||||
}
|
||||
|
||||
if (processed_data_len != nullptr) {
|
||||
@ -751,8 +612,8 @@ static bool client_or_ip_port_in_list(const Logger *log, const Mono_Time *mono_t
|
||||
}
|
||||
|
||||
bool add_to_list(
|
||||
Node_format *nodes_list, uint32_t length, const uint8_t pk[CRYPTO_PUBLIC_KEY_SIZE],
|
||||
const IP_Port *ip_port, const uint8_t cmp_pk[CRYPTO_PUBLIC_KEY_SIZE])
|
||||
Node_format *nodes_list, uint32_t length, const uint8_t pk[CRYPTO_PUBLIC_KEY_SIZE],
|
||||
const IP_Port *ip_port, const uint8_t cmp_pk[CRYPTO_PUBLIC_KEY_SIZE])
|
||||
{
|
||||
for (uint32_t i = 0; i < length; ++i) {
|
||||
Node_format *node = &nodes_list[i];
|
||||
@ -782,10 +643,10 @@ bool add_to_list(
|
||||
*/
|
||||
non_null()
|
||||
static void get_close_nodes_inner(
|
||||
uint64_t cur_time, const uint8_t *public_key,
|
||||
Node_format *nodes_list, uint32_t *num_nodes_ptr,
|
||||
Family sa_family, const Client_data *client_list, uint32_t client_list_length,
|
||||
bool is_lan, bool want_announce)
|
||||
uint64_t cur_time, const uint8_t *public_key,
|
||||
Node_format *nodes_list, uint32_t *num_nodes_ptr,
|
||||
Family sa_family, const Client_data *client_list, uint32_t client_list_length,
|
||||
bool is_lan, bool want_announce)
|
||||
{
|
||||
if (!net_family_is_ipv4(sa_family) && !net_family_is_ipv6(sa_family) && !net_family_is_unspec(sa_family)) {
|
||||
return;
|
||||
@ -829,7 +690,7 @@ static void get_close_nodes_inner(
|
||||
continue;
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif /* CHECK_ANNOUNCE_NODE */
|
||||
|
||||
if (num_nodes < MAX_SENT_NODES) {
|
||||
memcpy(nodes_list[num_nodes].public_key, client->public_key, CRYPTO_PUBLIC_KEY_SIZE);
|
||||
@ -853,43 +714,45 @@ static void get_close_nodes_inner(
|
||||
*/
|
||||
non_null()
|
||||
static int get_somewhat_close_nodes(
|
||||
uint64_t cur_time, const uint8_t *public_key, Node_format *nodes_list,
|
||||
Family sa_family, const Client_data *close_clientlist,
|
||||
const DHT_Friend *friends_list, uint16_t friends_list_size,
|
||||
bool is_lan, bool want_announce)
|
||||
uint64_t cur_time, const uint8_t *public_key, Node_format nodes_list[MAX_SENT_NODES],
|
||||
Family sa_family, const Client_data *close_clientlist,
|
||||
const DHT_Friend *friends_list, uint16_t friends_list_size,
|
||||
bool is_lan, bool want_announce)
|
||||
{
|
||||
memset(nodes_list, 0, MAX_SENT_NODES * sizeof(Node_format));
|
||||
for (uint16_t i = 0; i < MAX_SENT_NODES; ++i) {
|
||||
nodes_list[i] = empty_node_format;
|
||||
}
|
||||
|
||||
uint32_t num_nodes = 0;
|
||||
get_close_nodes_inner(
|
||||
cur_time, public_key,
|
||||
nodes_list, &num_nodes,
|
||||
sa_family, close_clientlist, LCLIENT_LIST,
|
||||
is_lan, want_announce);
|
||||
cur_time, public_key,
|
||||
nodes_list, &num_nodes,
|
||||
sa_family, close_clientlist, LCLIENT_LIST,
|
||||
is_lan, want_announce);
|
||||
|
||||
for (uint16_t i = 0; i < friends_list_size; ++i) {
|
||||
const DHT_Friend *dht_friend = &friends_list[i];
|
||||
|
||||
get_close_nodes_inner(
|
||||
cur_time, public_key,
|
||||
nodes_list, &num_nodes,
|
||||
sa_family, dht_friend->client_list, MAX_FRIEND_CLIENTS,
|
||||
is_lan, want_announce);
|
||||
cur_time, public_key,
|
||||
nodes_list, &num_nodes,
|
||||
sa_family, dht_friend->client_list, MAX_FRIEND_CLIENTS,
|
||||
is_lan, want_announce);
|
||||
}
|
||||
|
||||
return num_nodes;
|
||||
}
|
||||
|
||||
int get_close_nodes(
|
||||
const DHT *dht, const uint8_t *public_key,
|
||||
Node_format *nodes_list, Family sa_family,
|
||||
bool is_lan, bool want_announce)
|
||||
const DHT *dht, const uint8_t *public_key,
|
||||
Node_format nodes_list[MAX_SENT_NODES], Family sa_family,
|
||||
bool is_lan, bool want_announce)
|
||||
{
|
||||
return get_somewhat_close_nodes(
|
||||
dht->cur_time, public_key, nodes_list,
|
||||
sa_family, dht->close_clientlist,
|
||||
dht->friends_list, dht->num_friends,
|
||||
is_lan, want_announce);
|
||||
dht->cur_time, public_key, nodes_list,
|
||||
sa_family, dht->close_clientlist,
|
||||
dht->friends_list, dht->num_friends,
|
||||
is_lan, want_announce);
|
||||
}
|
||||
|
||||
typedef struct DHT_Cmp_Data {
|
||||
@ -1035,7 +898,7 @@ static int handle_data_search_response(void *object, const IP_Port *source,
|
||||
return 0;
|
||||
|
||||
}
|
||||
#endif
|
||||
#endif /* CHECK_ANNOUNCE_NODE */
|
||||
|
||||
/** @brief Is it ok to store node with public_key in client.
|
||||
*
|
||||
@ -1101,7 +964,8 @@ static void update_client_with_reset(const Mono_Time *mono_time, Client_data *cl
|
||||
ipptp_write->ret_ip_self = false;
|
||||
|
||||
/* zero out other address */
|
||||
memset(ipptp_clear, 0, sizeof(*ipptp_clear));
|
||||
const IPPTsPng empty_ipptp = {{{{0}}}};
|
||||
*ipptp_clear = empty_ipptp;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1180,7 +1044,7 @@ static bool add_to_close(DHT *dht, const uint8_t *public_key, const IP_Port *ip_
|
||||
#ifdef CHECK_ANNOUNCE_NODE
|
||||
client->announce_node = false;
|
||||
send_announce_ping(dht, public_key, ip_port);
|
||||
#endif
|
||||
#endif /* CHECK_ANNOUNCE_NODE */
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -1298,7 +1162,7 @@ static bool ping_node_from_getnodes_ok(DHT *dht, const uint8_t *public_key, cons
|
||||
*/
|
||||
uint32_t addto_lists(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key)
|
||||
{
|
||||
IP_Port ipp_copy = ip_port_normalize(ip_port);
|
||||
const IP_Port ipp_copy = ip_port_normalize(ip_port);
|
||||
|
||||
uint32_t used = 0;
|
||||
|
||||
@ -1384,7 +1248,7 @@ static bool update_client_data(const Mono_Time *mono_time, Client_data *array, s
|
||||
non_null()
|
||||
static void returnedip_ports(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key, const uint8_t *nodepublic_key)
|
||||
{
|
||||
IP_Port ipp_copy = ip_port_normalize(ip_port);
|
||||
const IP_Port ipp_copy = ip_port_normalize(ip_port);
|
||||
|
||||
if (pk_equal(public_key, dht->self_public_key)) {
|
||||
update_client_data(dht->mono_time, dht->close_clientlist, LCLIENT_LIST, &ipp_copy, nodepublic_key, true);
|
||||
@ -1484,13 +1348,14 @@ static int sendnodes_ipv6(const DHT *dht, const IP_Port *ip_port, const uint8_t
|
||||
memcpy(plain + 1 + nodes_length, sendback_data, length);
|
||||
|
||||
const uint32_t crypto_size = 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + CRYPTO_MAC_SIZE;
|
||||
VLA(uint8_t, data, 1 + nodes_length + length + crypto_size);
|
||||
const uint32_t data_size = 1 + nodes_length + length + crypto_size;
|
||||
VLA(uint8_t, data, data_size);
|
||||
|
||||
const int len = dht_create_packet(dht->mem, dht->rng,
|
||||
dht->self_public_key, shared_encryption_key, NET_PACKET_SEND_NODES_IPV6,
|
||||
plain, 1 + nodes_length + length, data, SIZEOF_VLA(data));
|
||||
plain, 1 + nodes_length + length, data, data_size);
|
||||
|
||||
if (len != SIZEOF_VLA(data)) {
|
||||
if (len != data_size) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -1573,7 +1438,8 @@ static bool handle_sendnodes_core(void *object, const IP_Port *source, const uin
|
||||
return false;
|
||||
}
|
||||
|
||||
VLA(uint8_t, plain, 1 + data_size + sizeof(uint64_t));
|
||||
const uint32_t plain_size = 1 + data_size + sizeof(uint64_t);
|
||||
VLA(uint8_t, plain, plain_size);
|
||||
const uint8_t *shared_key = dht_get_shared_key_sent(dht, packet + 1);
|
||||
const int len = decrypt_data_symmetric(
|
||||
shared_key,
|
||||
@ -1582,7 +1448,7 @@ static bool handle_sendnodes_core(void *object, const IP_Port *source, const uin
|
||||
1 + data_size + sizeof(uint64_t) + CRYPTO_MAC_SIZE,
|
||||
plain);
|
||||
|
||||
if ((unsigned int)len != SIZEOF_VLA(plain)) {
|
||||
if ((uint32_t)len != plain_size) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -1655,7 +1521,7 @@ static int handle_sendnodes_ipv6(void *object, const IP_Port *source, const uint
|
||||
|
||||
non_null(1) nullable(2, 3)
|
||||
static uint32_t dht_friend_lock(DHT_Friend *const dht_friend, dht_ip_cb *ip_callback,
|
||||
void *data, int32_t number)
|
||||
void *data, int32_t number)
|
||||
{
|
||||
// find first free slot
|
||||
uint8_t lock_num;
|
||||
@ -1974,8 +1840,8 @@ bool dht_bootstrap(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key)
|
||||
return dht_getnodes(dht, ip_port, public_key, dht->self_public_key);
|
||||
}
|
||||
|
||||
int dht_bootstrap_from_address(DHT *dht, const char *address, bool ipv6enabled,
|
||||
uint16_t port, const uint8_t *public_key)
|
||||
bool dht_bootstrap_from_address(DHT *dht, const char *address, bool ipv6enabled,
|
||||
uint16_t port, const uint8_t *public_key)
|
||||
{
|
||||
IP_Port ip_port_v64;
|
||||
IP *ip_extra = nullptr;
|
||||
@ -1998,10 +1864,10 @@ int dht_bootstrap_from_address(DHT *dht, const char *address, bool ipv6enabled,
|
||||
dht_bootstrap(dht, &ip_port_v4, public_key);
|
||||
}
|
||||
|
||||
return 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
int route_packet(const DHT *dht, const uint8_t *public_key, const uint8_t *packet, uint16_t length)
|
||||
@ -2103,7 +1969,6 @@ static int friend_iplist(const DHT *dht, IP_Port *ip_portlist, uint16_t friend_n
|
||||
#endif /* !FRIEND_IPLIST_PAD */
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Callback invoked for each IP/port of each client of a friend.
|
||||
*
|
||||
@ -2190,7 +2055,6 @@ uint32_t route_to_friend(const DHT *dht, const uint8_t *friend_id, const Packet
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
IP_Port ip_list[MAX_FRIEND_CLIENTS];
|
||||
const int ip_num = friend_iplist(dht, ip_list, num);
|
||||
|
||||
@ -2426,7 +2290,7 @@ static void punch_holes(DHT *dht, const IP *ip, const uint16_t *port_list, uint1
|
||||
|
||||
uint16_t i;
|
||||
for (i = 0; i < MAX_PUNCHING_PORTS; ++i) {
|
||||
uint32_t it = i + dht->friends_list[friend_num].nat.punching_index2;
|
||||
const uint32_t it = i + dht->friends_list[friend_num].nat.punching_index2;
|
||||
const uint16_t port = 1024;
|
||||
pinging.port = net_htons(port + it);
|
||||
ping_send_request(dht->ping, &pinging, dht->friends_list[friend_num].public_key);
|
||||
@ -2691,7 +2555,7 @@ DHT *new_dht(const Logger *log, const Memory *mem, const Random *rng, const Netw
|
||||
|
||||
#ifdef CHECK_ANNOUNCE_NODE
|
||||
networking_registerhandler(dht->net, NET_PACKET_DATA_SEARCH_RESPONSE, &handle_data_search_response, dht);
|
||||
#endif
|
||||
#endif /* CHECK_ANNOUNCE_NODE */
|
||||
|
||||
crypto_new_keypair(rng, dht->self_public_key, dht->self_secret_key);
|
||||
|
||||
@ -2704,7 +2568,6 @@ DHT *new_dht(const Logger *log, const Memory *mem, const Random *rng, const Netw
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
dht->dht_ping_array = ping_array_new(mem, DHT_PING_ARRAY_SIZE, PING_TIMEOUT);
|
||||
|
||||
if (dht->dht_ping_array == nullptr) {
|
||||
@ -2795,21 +2658,21 @@ uint32_t dht_size(const DHT *dht)
|
||||
uint32_t numv6 = 0;
|
||||
|
||||
for (uint32_t i = 0; i < dht->loaded_num_nodes; ++i) {
|
||||
numv4 += net_family_is_ipv4(dht->loaded_nodes_list[i].ip_port.ip.family);
|
||||
numv6 += net_family_is_ipv6(dht->loaded_nodes_list[i].ip_port.ip.family);
|
||||
numv4 += net_family_is_ipv4(dht->loaded_nodes_list[i].ip_port.ip.family) ? 1 : 0;
|
||||
numv6 += net_family_is_ipv6(dht->loaded_nodes_list[i].ip_port.ip.family) ? 1 : 0;
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < LCLIENT_LIST; ++i) {
|
||||
numv4 += dht->close_clientlist[i].assoc4.timestamp != 0;
|
||||
numv6 += dht->close_clientlist[i].assoc6.timestamp != 0;
|
||||
numv4 += dht->close_clientlist[i].assoc4.timestamp != 0 ? 1 : 0;
|
||||
numv6 += dht->close_clientlist[i].assoc6.timestamp != 0 ? 1 : 0;
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < DHT_FAKE_FRIEND_NUMBER && i < dht->num_friends; ++i) {
|
||||
const DHT_Friend *const fr = &dht->friends_list[i];
|
||||
|
||||
for (uint32_t j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
|
||||
numv4 += fr->client_list[j].assoc4.timestamp != 0;
|
||||
numv6 += fr->client_list[j].assoc6.timestamp != 0;
|
||||
numv4 += fr->client_list[j].assoc4.timestamp != 0 ? 1 : 0;
|
||||
numv6 += fr->client_list[j].assoc6.timestamp != 0 ? 1 : 0;
|
||||
}
|
||||
}
|
||||
|
||||
@ -2877,8 +2740,8 @@ void dht_save(const DHT *dht, uint8_t *data)
|
||||
}
|
||||
|
||||
state_write_section_header(
|
||||
old_data, DHT_STATE_COOKIE_TYPE, pack_nodes(dht->log, data, sizeof(Node_format) * num, clients, num),
|
||||
DHT_STATE_TYPE_NODES);
|
||||
old_data, DHT_STATE_COOKIE_TYPE, pack_nodes(dht->log, data, sizeof(Node_format) * num, clients, num),
|
||||
DHT_STATE_TYPE_NODES);
|
||||
|
||||
mem_delete(dht->mem, clients);
|
||||
}
|
||||
@ -3017,7 +2880,8 @@ bool dht_non_lan_connected(const DHT *dht)
|
||||
return false;
|
||||
}
|
||||
|
||||
uint16_t dht_get_num_closelist(const DHT *dht) {
|
||||
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);
|
||||
@ -3031,7 +2895,8 @@ uint16_t dht_get_num_closelist(const DHT *dht) {
|
||||
return num_valid_close_clients;
|
||||
}
|
||||
|
||||
uint16_t dht_get_num_closelist_announce_capable(const DHT *dht) {
|
||||
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);
|
||||
|
65
external/toxcore/c-toxcore/toxcore/DHT.h
vendored
65
external/toxcore/c-toxcore/toxcore/DHT.h
vendored
@ -23,20 +23,6 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Encryption and signature keys definition */
|
||||
#define ENC_PUBLIC_KEY_SIZE CRYPTO_PUBLIC_KEY_SIZE
|
||||
#define ENC_SECRET_KEY_SIZE CRYPTO_SECRET_KEY_SIZE
|
||||
#define SIG_PUBLIC_KEY_SIZE CRYPTO_SIGN_PUBLIC_KEY_SIZE
|
||||
#define SIG_SECRET_KEY_SIZE CRYPTO_SIGN_SECRET_KEY_SIZE
|
||||
|
||||
/* Size of the group chat_id */
|
||||
#define CHAT_ID_SIZE SIG_PUBLIC_KEY_SIZE
|
||||
|
||||
/* Extended keys for group chats */
|
||||
#define EXT_SECRET_KEY_SIZE (ENC_SECRET_KEY_SIZE + SIG_SECRET_KEY_SIZE)
|
||||
#define EXT_PUBLIC_KEY_SIZE (ENC_PUBLIC_KEY_SIZE + SIG_PUBLIC_KEY_SIZE)
|
||||
|
||||
|
||||
/* Maximum size of a signature (may be smaller) */
|
||||
#define SIGNATURE_SIZE CRYPTO_SIGNATURE_SIZE
|
||||
/** Maximum number of clients stored per friend. */
|
||||
@ -169,7 +155,7 @@ typedef struct Client_data {
|
||||
#ifdef CHECK_ANNOUNCE_NODE
|
||||
/* Responded to data search? */
|
||||
bool announce_node;
|
||||
#endif
|
||||
#endif /* CHECK_ANNOUNCE_NODE */
|
||||
} Client_data;
|
||||
|
||||
/*----------------------------------------------------------------------------------*/
|
||||
@ -204,26 +190,6 @@ non_null() const Client_data *dht_friend_client(const DHT_Friend *dht_friend, si
|
||||
*/
|
||||
int packed_node_size(Family ip_family);
|
||||
|
||||
/** @brief Pack an IP_Port structure into data of max size length.
|
||||
*
|
||||
* Packed_length is the offset of data currently packed.
|
||||
*
|
||||
* @return size of packed IP_Port data on success.
|
||||
* @retval -1 on failure.
|
||||
*/
|
||||
non_null()
|
||||
int pack_ip_port(const Logger *logger, uint8_t *data, uint16_t length, const IP_Port *ip_port);
|
||||
|
||||
/** @brief Unpack IP_Port structure from data of max size length into ip_port.
|
||||
*
|
||||
* len_processed is the offset of data currently unpacked.
|
||||
*
|
||||
* @return size of unpacked ip_port on success.
|
||||
* @retval -1 on failure.
|
||||
*/
|
||||
non_null()
|
||||
int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length, bool tcp_enabled);
|
||||
|
||||
/** @brief Encrypt plain and write resulting DHT packet into packet with max size length.
|
||||
*
|
||||
* @return size of packet on success.
|
||||
@ -257,8 +223,8 @@ int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed
|
||||
|
||||
/*----------------------------------------------------------------------------------*/
|
||||
|
||||
typedef int cryptopacket_handler_cb(void *object, const IP_Port *ip_port, const uint8_t *source_pubkey,
|
||||
const uint8_t *data, uint16_t len, void *userdata);
|
||||
typedef int cryptopacket_handler_cb(void *object, const IP_Port *source, const uint8_t *source_pubkey,
|
||||
const uint8_t *packet, uint16_t length, void *userdata);
|
||||
|
||||
typedef struct DHT DHT;
|
||||
|
||||
@ -370,8 +336,8 @@ unsigned int bit_by_bit_cmp(const uint8_t *pk1, const uint8_t *pk2);
|
||||
*/
|
||||
non_null()
|
||||
bool add_to_list(
|
||||
Node_format *nodes_list, uint32_t length, const uint8_t pk[CRYPTO_PUBLIC_KEY_SIZE],
|
||||
const IP_Port *ip_port, const uint8_t cmp_pk[CRYPTO_PUBLIC_KEY_SIZE]);
|
||||
Node_format *nodes_list, uint32_t length, const uint8_t pk[CRYPTO_PUBLIC_KEY_SIZE],
|
||||
const IP_Port *ip_port, const uint8_t cmp_pk[CRYPTO_PUBLIC_KEY_SIZE]);
|
||||
|
||||
/** Return 1 if node can be added to close list, 0 if it can't. */
|
||||
non_null()
|
||||
@ -381,7 +347,7 @@ bool node_addable_to_close_list(DHT *dht, const uint8_t *public_key, const IP_Po
|
||||
/** Set node as announce node. */
|
||||
non_null()
|
||||
void set_announce_node(DHT *dht, const uint8_t *public_key);
|
||||
#endif
|
||||
#endif /* CHECK_ANNOUNCE_NODE */
|
||||
|
||||
/**
|
||||
* @brief Get the (maximum MAX_SENT_NODES) closest nodes to public_key we know
|
||||
@ -395,10 +361,9 @@ void set_announce_node(DHT *dht, const uint8_t *public_key);
|
||||
*/
|
||||
non_null()
|
||||
int get_close_nodes(
|
||||
const DHT *dht, const uint8_t *public_key,
|
||||
Node_format *nodes_list, Family sa_family,
|
||||
bool is_lan, bool want_announce);
|
||||
|
||||
const DHT *dht, const uint8_t *public_key,
|
||||
Node_format nodes_list[MAX_SENT_NODES], Family sa_family,
|
||||
bool is_lan, bool want_announce);
|
||||
|
||||
/** @brief Put up to max_num nodes in nodes from the random friends.
|
||||
*
|
||||
@ -440,12 +405,12 @@ bool dht_bootstrap(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key);
|
||||
* @param ipv6enabled if false, the resolving sticks STRICTLY to IPv4 addresses.
|
||||
* Otherwise, the resolving looks for IPv6 addresses first, then IPv4 addresses.
|
||||
*
|
||||
* @retval 1 if the address could be converted into an IP address
|
||||
* @retval 0 otherwise
|
||||
* @retval true if the address could be converted into an IP address
|
||||
* @retval false otherwise
|
||||
*/
|
||||
non_null()
|
||||
int dht_bootstrap_from_address(DHT *dht, const char *address, bool ipv6enabled,
|
||||
uint16_t port, const uint8_t *public_key);
|
||||
bool dht_bootstrap_from_address(DHT *dht, const char *address, bool ipv6enabled,
|
||||
uint16_t port, const uint8_t *public_key);
|
||||
|
||||
/** @brief Start sending packets after DHT loaded_friends_list and loaded_clients_list are set.
|
||||
*
|
||||
@ -558,7 +523,7 @@ non_null()
|
||||
unsigned int ipport_self_copy(const DHT *dht, IP_Port *dest);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif
|
||||
#endif /* C_TOXCORE_TOXCORE_DHT_H */
|
||||
|
@ -5,7 +5,7 @@
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
|
||||
#include "../testing/fuzzing/fuzz_support.h"
|
||||
#include "../testing/fuzzing/fuzz_support.hh"
|
||||
|
||||
namespace {
|
||||
|
||||
|
@ -11,8 +11,12 @@
|
||||
#include "DHT_test_util.hh"
|
||||
#include "crypto_core.h"
|
||||
#include "crypto_core_test_util.hh"
|
||||
#include "logger.h"
|
||||
#include "mem_test_util.hh"
|
||||
#include "mono_time.h"
|
||||
#include "network.h"
|
||||
#include "network_test_util.hh"
|
||||
#include "test_util.hh"
|
||||
|
||||
namespace {
|
||||
|
||||
|
@ -3,7 +3,10 @@
|
||||
#include <cstring>
|
||||
#include <iomanip>
|
||||
|
||||
#include "DHT.h"
|
||||
#include "crypto_core.h"
|
||||
#include "crypto_core_test_util.hh"
|
||||
#include "network.h"
|
||||
#include "network_test_util.hh"
|
||||
|
||||
Node_format random_node_format(const Random *rng)
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include <iosfwd>
|
||||
|
||||
#include "DHT.h"
|
||||
#include "crypto_core.h"
|
||||
#include "test_util.hh"
|
||||
|
||||
template <>
|
||||
|
@ -9,7 +9,6 @@
|
||||
#include "LAN_discovery.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
|
||||
// The mingw32/64 Windows library warns about including winsock2.h after
|
||||
@ -21,7 +20,7 @@
|
||||
#include <ws2tcpip.h>
|
||||
|
||||
#include <iphlpapi.h>
|
||||
#endif
|
||||
#endif /* WIN32 */
|
||||
|
||||
#if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__)
|
||||
#include <netinet/in.h>
|
||||
@ -29,23 +28,23 @@
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#endif /* Linux/BSD */
|
||||
|
||||
#ifdef __linux__
|
||||
#include <linux/if.h>
|
||||
#endif
|
||||
#endif /* Linux */
|
||||
|
||||
#if defined(__FreeBSD__) || defined(__DragonFly__)
|
||||
#include <net/if.h>
|
||||
#endif
|
||||
#endif /* BSD */
|
||||
|
||||
#include "attributes.h"
|
||||
#include "ccompat.h"
|
||||
#include "crypto_core.h"
|
||||
#include "network.h"
|
||||
|
||||
#define MAX_INTERFACES 16
|
||||
|
||||
|
||||
struct Broadcast_Info {
|
||||
uint32_t count;
|
||||
IP ips[MAX_INTERFACES];
|
||||
@ -143,14 +142,13 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns)
|
||||
}
|
||||
|
||||
/* Configure ifconf for the ioctl call. */
|
||||
struct ifreq i_faces[MAX_INTERFACES];
|
||||
memset(i_faces, 0, sizeof(struct ifreq) * MAX_INTERFACES);
|
||||
struct ifreq i_faces[MAX_INTERFACES] = {{{0}}};
|
||||
|
||||
struct ifconf ifc;
|
||||
ifc.ifc_buf = (char *)i_faces;
|
||||
ifc.ifc_len = sizeof(i_faces);
|
||||
|
||||
if (ioctl(sock.sock, SIOCGIFCONF, &ifc) < 0) {
|
||||
if (ioctl(net_socket_to_native(sock), SIOCGIFCONF, &ifc) < 0) {
|
||||
kill_sock(ns, sock);
|
||||
free(broadcast);
|
||||
return nullptr;
|
||||
@ -165,7 +163,7 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns)
|
||||
|
||||
for (int i = 0; i < n; ++i) {
|
||||
/* there are interfaces with are incapable of broadcast */
|
||||
if (ioctl(sock.sock, SIOCGIFBRDADDR, &i_faces[i]) < 0) {
|
||||
if (ioctl(net_socket_to_native(sock), SIOCGIFBRDADDR, &i_faces[i]) < 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -204,7 +202,7 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns)
|
||||
return (Broadcast_Info *)calloc(1, sizeof(Broadcast_Info));
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif /* platforms */
|
||||
|
||||
/** @brief Send packet to all IPv4 broadcast addresses
|
||||
*
|
||||
@ -241,8 +239,8 @@ static IP broadcast_ip(Family family_socket, Family family_broadcast)
|
||||
/* `FF02::1` is - according to RFC 4291 - multicast all-nodes link-local */
|
||||
/* `FE80::*:` MUST be exact, for that we would need to look over all
|
||||
* interfaces and check in which status they are */
|
||||
ip.ip.v6.uint8[ 0] = 0xFF;
|
||||
ip.ip.v6.uint8[ 1] = 0x02;
|
||||
ip.ip.v6.uint8[0] = 0xFF;
|
||||
ip.ip.v6.uint8[1] = 0x02;
|
||||
ip.ip.v6.uint8[15] = 0x01;
|
||||
} else if (net_family_is_ipv4(family_broadcast)) {
|
||||
ip.family = net_family_ipv6();
|
||||
@ -341,7 +339,6 @@ bool ip_is_lan(const IP *ip)
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool lan_discovery_send(const Networking_Core *net, const Broadcast_Info *broadcast, const uint8_t *dht_pk,
|
||||
uint16_t port)
|
||||
{
|
||||
@ -378,7 +375,6 @@ bool lan_discovery_send(const Networking_Core *net, const Broadcast_Info *broadc
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
Broadcast_Info *lan_discovery_init(const Network *ns)
|
||||
{
|
||||
return fetch_broadcast_info(ns);
|
||||
|
@ -9,6 +9,7 @@
|
||||
#ifndef C_TOXCORE_TOXCORE_LAN_DISCOVERY_H
|
||||
#define C_TOXCORE_TOXCORE_LAN_DISCOVERY_H
|
||||
|
||||
#include "attributes.h"
|
||||
#include "network.h"
|
||||
|
||||
/**
|
||||
@ -53,4 +54,4 @@ bool ip_is_local(const IP *ip);
|
||||
non_null()
|
||||
bool ip_is_lan(const IP *ip);
|
||||
|
||||
#endif // C_TOXCORE_TOXCORE_LAN_DISCOVERY_H
|
||||
#endif /* C_TOXCORE_TOXCORE_LAN_DISCOVERY_H */
|
||||
|
@ -1,8 +1,7 @@
|
||||
lib_LTLIBRARIES += libtoxcore.la
|
||||
|
||||
libtoxcore_la_include_HEADERS = \
|
||||
../toxcore/tox.h \
|
||||
../toxcore/tox_private.h
|
||||
../toxcore/tox.h
|
||||
|
||||
libtoxcore_la_includedir = $(includedir)/tox
|
||||
|
||||
@ -21,6 +20,7 @@ libtoxcore_la_SOURCES = ../third_party/cmp/cmp.c \
|
||||
../toxcore/events/conference_peer_list_changed.c \
|
||||
../toxcore/events/conference_peer_name.c \
|
||||
../toxcore/events/conference_title.c \
|
||||
../toxcore/events/dht_get_nodes_response.c \
|
||||
../toxcore/events/events_alloc.c \
|
||||
../toxcore/events/events_alloc.h \
|
||||
../toxcore/events/file_chunk_request.c \
|
||||
@ -66,6 +66,8 @@ libtoxcore_la_SOURCES = ../third_party/cmp/cmp.c \
|
||||
../toxcore/network.c \
|
||||
../toxcore/crypto_core.h \
|
||||
../toxcore/crypto_core.c \
|
||||
../toxcore/crypto_core_pack.h \
|
||||
../toxcore/crypto_core_pack.c \
|
||||
../toxcore/timed_auth.h \
|
||||
../toxcore/timed_auth.c \
|
||||
../toxcore/ping_array.h \
|
||||
|
265
external/toxcore/c-toxcore/toxcore/Messenger.c
vendored
265
external/toxcore/c-toxcore/toxcore/Messenger.c
vendored
@ -18,6 +18,7 @@
|
||||
#include "TCP_connection.h"
|
||||
#include "TCP_server.h"
|
||||
#include "announce.h"
|
||||
#include "attributes.h"
|
||||
#include "bin_pack.h"
|
||||
#include "bin_unpack.h"
|
||||
#include "ccompat.h"
|
||||
@ -138,25 +139,25 @@ void getaddress(const Messenger *m, uint8_t *address)
|
||||
non_null()
|
||||
static bool send_online_packet(Messenger *m, int friendcon_id)
|
||||
{
|
||||
uint8_t packet = PACKET_ID_ONLINE;
|
||||
return write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, friendcon_id), &packet,
|
||||
const uint8_t packet[1] = {PACKET_ID_ONLINE};
|
||||
return write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, friendcon_id), packet,
|
||||
sizeof(packet), false) != -1;
|
||||
}
|
||||
|
||||
non_null()
|
||||
static bool send_offline_packet(Messenger *m, int friendcon_id)
|
||||
{
|
||||
uint8_t packet = PACKET_ID_OFFLINE;
|
||||
return write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, friendcon_id), &packet,
|
||||
const uint8_t packet[1] = {PACKET_ID_OFFLINE};
|
||||
return write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, friendcon_id), packet,
|
||||
sizeof(packet), false) != -1;
|
||||
}
|
||||
|
||||
non_null(1) nullable(4)
|
||||
static int m_handle_status(void *object, int i, bool status, void *userdata);
|
||||
static int m_handle_status(void *object, int friendcon_id, bool status, void *userdata);
|
||||
non_null(1, 3) nullable(5)
|
||||
static int m_handle_packet(void *object, int i, const uint8_t *temp, uint16_t len, void *userdata);
|
||||
static int m_handle_packet(void *object, int friendcon_id, const uint8_t *data, uint16_t length, void *userdata);
|
||||
non_null(1, 3) nullable(5)
|
||||
static int m_handle_lossy_packet(void *object, int friend_num, const uint8_t *packet, uint16_t length,
|
||||
static int m_handle_lossy_packet(void *object, int friendcon_id, const uint8_t *data, uint16_t length,
|
||||
void *userdata);
|
||||
|
||||
non_null()
|
||||
@ -403,7 +404,7 @@ bool m_create_group_connection(Messenger *m, GC_Chat *chat)
|
||||
const int onion_friend_number = friend_conn_get_onion_friendnum(connection);
|
||||
Onion_Friend *onion_friend = onion_get_friend(m->onion_c, (uint16_t)onion_friend_number);
|
||||
|
||||
onion_friend_set_gc_public_key(onion_friend, get_chat_id(chat->chat_public_key));
|
||||
onion_friend_set_gc_public_key(onion_friend, get_chat_id(&chat->chat_public_key));
|
||||
onion_friend_set_gc_data(onion_friend, nullptr, 0);
|
||||
|
||||
return true;
|
||||
@ -472,7 +473,7 @@ int m_delfriend(Messenger *m, int32_t friendnumber)
|
||||
}
|
||||
|
||||
if (m->friend_connectionstatuschange_internal != nullptr) {
|
||||
m->friend_connectionstatuschange_internal(m, friendnumber, 0, m->friend_connectionstatuschange_internal_userdata);
|
||||
m->friend_connectionstatuschange_internal(m, friendnumber, false, m->friend_connectionstatuschange_internal_userdata);
|
||||
}
|
||||
|
||||
clear_receipts(m, friendnumber);
|
||||
@ -591,7 +592,7 @@ int m_send_message_generic(Messenger *m, int32_t friendnumber, uint8_t type, con
|
||||
memcpy(packet + 1, message, length);
|
||||
|
||||
const int64_t packet_num = write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
|
||||
m->friendlist[friendnumber].friendcon_id), packet, length + 1, false);
|
||||
m->friendlist[friendnumber].friendcon_id), packet, length + 1, false);
|
||||
|
||||
if (packet_num == -1) {
|
||||
return -4;
|
||||
@ -857,7 +858,7 @@ int m_copy_statusmessage(const Messenger *m, int32_t friendnumber, uint8_t *buf,
|
||||
const uint32_t msglen = min_u32(maxlen, m->friendlist[friendnumber].statusmessage_length);
|
||||
|
||||
memcpy(buf, m->friendlist[friendnumber].statusmessage, msglen);
|
||||
memset(buf + msglen, 0, maxlen - msglen);
|
||||
memzero(buf + msglen, maxlen - msglen);
|
||||
return msglen;
|
||||
}
|
||||
|
||||
@ -1097,7 +1098,6 @@ static void set_friend_status(Messenger *m, int32_t friendnumber, uint8_t status
|
||||
|
||||
/*** CONFERENCES */
|
||||
|
||||
|
||||
/** @brief Set the callback for conference invites. */
|
||||
void m_callback_conference_invite(Messenger *m, m_conference_invite_cb *function)
|
||||
{
|
||||
@ -1120,20 +1120,17 @@ bool send_conference_invite_packet(const Messenger *m, int32_t friendnumber, con
|
||||
return write_cryptpacket_id(m, friendnumber, PACKET_ID_INVITE_CONFERENCE, data, length, false);
|
||||
}
|
||||
|
||||
|
||||
/** @brief Send a group invite packet.
|
||||
*
|
||||
* @retval true if success
|
||||
*/
|
||||
bool send_group_invite_packet(const Messenger *m, uint32_t friendnumber, const uint8_t *data, uint16_t length)
|
||||
bool send_group_invite_packet(const Messenger *m, uint32_t friendnumber, const uint8_t *packet, uint16_t length)
|
||||
{
|
||||
return write_cryptpacket_id(m, friendnumber, PACKET_ID_INVITE_GROUPCHAT, data, length, false);
|
||||
return write_cryptpacket_id(m, friendnumber, PACKET_ID_INVITE_GROUPCHAT, packet, length, false);
|
||||
}
|
||||
|
||||
|
||||
/*** FILE SENDING */
|
||||
|
||||
|
||||
/** @brief Set the callback for file send requests. */
|
||||
void callback_file_sendrequest(Messenger *m, m_file_recv_cb *function)
|
||||
{
|
||||
@ -1195,8 +1192,8 @@ int file_get_id(const Messenger *m, int32_t friendnumber, uint32_t filenumber, u
|
||||
file_number = temp_filenum;
|
||||
|
||||
const struct File_Transfers *const ft = inbound
|
||||
? &m->friendlist[friendnumber].file_receiving[file_number]
|
||||
: &m->friendlist[friendnumber].file_sending[file_number];
|
||||
? &m->friendlist[friendnumber].file_receiving[file_number]
|
||||
: &m->friendlist[friendnumber].file_sending[file_number];
|
||||
|
||||
if (ft->status == FILESTATUS_NONE) {
|
||||
return -2;
|
||||
@ -1223,7 +1220,8 @@ static bool file_sendrequest(const Messenger *m, int32_t friendnumber, uint8_t f
|
||||
return false;
|
||||
}
|
||||
|
||||
VLA(uint8_t, packet, 1 + sizeof(file_type) + sizeof(filesize) + FILE_ID_LENGTH + filename_length);
|
||||
const uint16_t packet_size = 1 + sizeof(file_type) + sizeof(filesize) + FILE_ID_LENGTH + filename_length;
|
||||
VLA(uint8_t, packet, packet_size);
|
||||
packet[0] = filenumber;
|
||||
file_type = net_htonl(file_type);
|
||||
memcpy(packet + 1, &file_type, sizeof(file_type));
|
||||
@ -1234,7 +1232,7 @@ static bool file_sendrequest(const Messenger *m, int32_t friendnumber, uint8_t f
|
||||
memcpy(packet + 1 + sizeof(file_type) + sizeof(filesize) + FILE_ID_LENGTH, filename, filename_length);
|
||||
}
|
||||
|
||||
return write_cryptpacket_id(m, friendnumber, PACKET_ID_FILE_SENDREQUEST, packet, SIZEOF_VLA(packet), false);
|
||||
return write_cryptpacket_id(m, friendnumber, PACKET_ID_FILE_SENDREQUEST, packet, packet_size, false);
|
||||
}
|
||||
|
||||
/** @brief Send a file send request.
|
||||
@ -1301,7 +1299,8 @@ static bool send_file_control_packet(const Messenger *m, int32_t friendnumber, b
|
||||
return false;
|
||||
}
|
||||
|
||||
VLA(uint8_t, packet, 3 + data_length);
|
||||
const uint16_t packet_size = 3 + data_length;
|
||||
VLA(uint8_t, packet, packet_size);
|
||||
|
||||
packet[0] = inbound ? 1 : 0;
|
||||
packet[1] = filenumber;
|
||||
@ -1311,7 +1310,7 @@ static bool send_file_control_packet(const Messenger *m, int32_t friendnumber, b
|
||||
memcpy(packet + 3, data, data_length);
|
||||
}
|
||||
|
||||
return write_cryptpacket_id(m, friendnumber, PACKET_ID_FILE_CONTROL, packet, SIZEOF_VLA(packet), false);
|
||||
return write_cryptpacket_id(m, friendnumber, PACKET_ID_FILE_CONTROL, packet, packet_size, false);
|
||||
}
|
||||
|
||||
/** @brief Send a file control request.
|
||||
@ -1501,7 +1500,8 @@ static int64_t send_file_data_packet(const Messenger *m, int32_t friendnumber, u
|
||||
return -1;
|
||||
}
|
||||
|
||||
VLA(uint8_t, packet, 2 + length);
|
||||
const uint16_t packet_size = 2 + length;
|
||||
VLA(uint8_t, packet, packet_size);
|
||||
packet[0] = PACKET_ID_FILE_DATA;
|
||||
packet[1] = filenumber;
|
||||
|
||||
@ -1510,7 +1510,7 @@ static int64_t send_file_data_packet(const Messenger *m, int32_t friendnumber, u
|
||||
}
|
||||
|
||||
return write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
|
||||
m->friendlist[friendnumber].friendcon_id), packet, SIZEOF_VLA(packet), true);
|
||||
m->friendlist[friendnumber].friendcon_id), packet, packet_size, true);
|
||||
}
|
||||
|
||||
#define MAX_FILE_DATA_SIZE (MAX_CRYPTO_DATA_SIZE - 2)
|
||||
@ -1712,7 +1712,6 @@ static void do_reqchunk_filecb(Messenger *m, int32_t friendnumber, void *userdat
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** @brief Run this when the friend disconnects.
|
||||
* Kill all current file transfers.
|
||||
*/
|
||||
@ -1873,28 +1872,28 @@ bool m_msi_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data,
|
||||
return write_cryptpacket_id(m, friendnumber, PACKET_ID_MSI, data, length, false);
|
||||
}
|
||||
|
||||
static int m_handle_lossy_packet(void *object, int friend_num, const uint8_t *packet, uint16_t length,
|
||||
static int m_handle_lossy_packet(void *object, int friendcon_id, const uint8_t *data, uint16_t length,
|
||||
void *userdata)
|
||||
{
|
||||
Messenger *m = (Messenger *)object;
|
||||
|
||||
if (!m_friend_exists(m, friend_num)) {
|
||||
if (!m_friend_exists(m, friendcon_id)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (packet[0] <= PACKET_ID_RANGE_LOSSY_AV_END) {
|
||||
if (data[0] <= PACKET_ID_RANGE_LOSSY_AV_END) {
|
||||
const RTP_Packet_Handler *const ph =
|
||||
&m->friendlist[friend_num].lossy_rtp_packethandlers[packet[0] % PACKET_ID_RANGE_LOSSY_AV_SIZE];
|
||||
&m->friendlist[friendcon_id].lossy_rtp_packethandlers[data[0] % PACKET_ID_RANGE_LOSSY_AV_SIZE];
|
||||
|
||||
if (ph->function != nullptr) {
|
||||
return ph->function(m, friend_num, packet, length, ph->object);
|
||||
return ph->function(m, friendcon_id, data, length, ph->object);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (m->lossy_packethandler != nullptr) {
|
||||
m->lossy_packethandler(m, friend_num, packet[0], packet, length, userdata);
|
||||
m->lossy_packethandler(m, friendcon_id, data[0], data, length, userdata);
|
||||
}
|
||||
|
||||
return 1;
|
||||
@ -1921,7 +1920,6 @@ int m_callback_rtp_packet(Messenger *m, int32_t friendnumber, uint8_t byte, m_lo
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/** @brief High level function to send custom lossy packets.
|
||||
*
|
||||
* TODO(oxij): this name is confusing, because this function sends both av and custom lossy packets.
|
||||
@ -2020,11 +2018,11 @@ int send_custom_lossless_packet(const Messenger *m, int32_t friendnumber, const
|
||||
|
||||
/** Function to filter out some friend requests*/
|
||||
non_null()
|
||||
static int friend_already_added(const uint8_t *real_pk, void *data)
|
||||
static int friend_already_added(void *object, const uint8_t *public_key)
|
||||
{
|
||||
const Messenger *m = (const Messenger *)data;
|
||||
const Messenger *m = (const Messenger *)object;
|
||||
|
||||
if (getfriend_id(m, real_pk) == -1) {
|
||||
if (getfriend_id(m, public_key) == -1) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -2034,16 +2032,16 @@ static int friend_already_added(const uint8_t *real_pk, void *data)
|
||||
/** @brief Check for and handle a timed-out friend request.
|
||||
*
|
||||
* If the request has timed-out then the friend status is set back to FRIEND_ADDED.
|
||||
* @param i friendlist index of the timed-out friend
|
||||
* @param friendcon_id friendlist index of the timed-out friend
|
||||
* @param t time
|
||||
*/
|
||||
non_null(1) nullable(4)
|
||||
static void check_friend_request_timed_out(Messenger *m, uint32_t i, uint64_t t, void *userdata)
|
||||
static void check_friend_request_timed_out(Messenger *m, uint32_t friendcon_id, uint64_t t, void *userdata)
|
||||
{
|
||||
Friend *f = &m->friendlist[i];
|
||||
Friend *f = &m->friendlist[friendcon_id];
|
||||
|
||||
if (f->friendrequest_lastsent + f->friendrequest_timeout < t) {
|
||||
set_friend_status(m, i, FRIEND_ADDED, userdata);
|
||||
set_friend_status(m, friendcon_id, FRIEND_ADDED, userdata);
|
||||
/* Double the default timeout every time if friendrequest is assumed
|
||||
* to have been sent unsuccessfully.
|
||||
*/
|
||||
@ -2052,15 +2050,15 @@ static void check_friend_request_timed_out(Messenger *m, uint32_t i, uint64_t t,
|
||||
}
|
||||
|
||||
non_null(1) nullable(4)
|
||||
static int m_handle_status(void *object, int i, bool status, void *userdata)
|
||||
static int m_handle_status(void *object, int friendcon_id, bool status, void *userdata)
|
||||
{
|
||||
Messenger *m = (Messenger *)object;
|
||||
|
||||
if (status) { /* Went online. */
|
||||
send_online_packet(m, m->friendlist[i].friendcon_id);
|
||||
send_online_packet(m, m->friendlist[friendcon_id].friendcon_id);
|
||||
} else { /* Went offline. */
|
||||
if (m->friendlist[i].status == FRIEND_ONLINE) {
|
||||
set_friend_status(m, i, FRIEND_CONFIRMED, userdata);
|
||||
if (m->friendlist[friendcon_id].status == FRIEND_ONLINE) {
|
||||
set_friend_status(m, friendcon_id, FRIEND_CONFIRMED, userdata);
|
||||
}
|
||||
}
|
||||
|
||||
@ -2068,17 +2066,17 @@ static int m_handle_status(void *object, int i, bool status, void *userdata)
|
||||
}
|
||||
|
||||
non_null(1, 3) nullable(5)
|
||||
static int m_handle_packet_offline(Messenger *m, const int i, const uint8_t *data, const uint16_t data_length, void *userdata)
|
||||
static int m_handle_packet_offline(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, void *userdata)
|
||||
{
|
||||
if (data_length == 0) {
|
||||
set_friend_status(m, i, FRIEND_CONFIRMED, userdata);
|
||||
set_friend_status(m, friendcon_id, FRIEND_CONFIRMED, userdata);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
non_null(1, 3) nullable(5)
|
||||
static int m_handle_packet_nickname(Messenger *m, const int i, const uint8_t *data, const uint16_t data_length, void *userdata)
|
||||
static int m_handle_packet_nickname(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, void *userdata)
|
||||
{
|
||||
if (data_length > MAX_NAME_LENGTH) {
|
||||
return 0;
|
||||
@ -2091,17 +2089,17 @@ static int m_handle_packet_nickname(Messenger *m, const int i, const uint8_t *da
|
||||
|
||||
/* inform of namechange before we overwrite the old name */
|
||||
if (m->friend_namechange != nullptr) {
|
||||
m->friend_namechange(m, i, data_terminated, data_length, userdata);
|
||||
m->friend_namechange(m, friendcon_id, data_terminated, data_length, userdata);
|
||||
}
|
||||
|
||||
memcpy(m->friendlist[i].name, data_terminated, data_length);
|
||||
m->friendlist[i].name_length = data_length;
|
||||
memcpy(m->friendlist[friendcon_id].name, data_terminated, data_length);
|
||||
m->friendlist[friendcon_id].name_length = data_length;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
non_null(1, 3) nullable(5)
|
||||
static int m_handle_packet_statusmessage(Messenger *m, const int i, const uint8_t *data, const uint16_t data_length, void *userdata)
|
||||
static int m_handle_packet_statusmessage(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, void *userdata)
|
||||
{
|
||||
if (data_length > MAX_STATUSMESSAGE_LENGTH) {
|
||||
return 0;
|
||||
@ -2113,16 +2111,16 @@ static int m_handle_packet_statusmessage(Messenger *m, const int i, const uint8_
|
||||
data_terminated[data_length] = 0;
|
||||
|
||||
if (m->friend_statusmessagechange != nullptr) {
|
||||
m->friend_statusmessagechange(m, i, data_terminated, data_length, userdata);
|
||||
m->friend_statusmessagechange(m, friendcon_id, data_terminated, data_length, userdata);
|
||||
}
|
||||
|
||||
set_friend_statusmessage(m, i, data_terminated, data_length);
|
||||
set_friend_statusmessage(m, friendcon_id, data_terminated, data_length);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
non_null(1, 3) nullable(5)
|
||||
static int m_handle_packet_userstatus(Messenger *m, const int i, const uint8_t *data, const uint16_t data_length, void *userdata)
|
||||
static int m_handle_packet_userstatus(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, void *userdata)
|
||||
{
|
||||
if (data_length != 1) {
|
||||
return 0;
|
||||
@ -2134,16 +2132,16 @@ static int m_handle_packet_userstatus(Messenger *m, const int i, const uint8_t *
|
||||
}
|
||||
|
||||
if (m->friend_userstatuschange != nullptr) {
|
||||
m->friend_userstatuschange(m, i, status, userdata);
|
||||
m->friend_userstatuschange(m, friendcon_id, status, userdata);
|
||||
}
|
||||
|
||||
set_friend_userstatus(m, i, status);
|
||||
set_friend_userstatus(m, friendcon_id, status);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
non_null(1, 3) nullable(5)
|
||||
static int m_handle_packet_typing(Messenger *m, const int i, const uint8_t *data, const uint16_t data_length, void *userdata)
|
||||
static int m_handle_packet_typing(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, void *userdata)
|
||||
{
|
||||
if (data_length != 1) {
|
||||
return 0;
|
||||
@ -2151,17 +2149,17 @@ static int m_handle_packet_typing(Messenger *m, const int i, const uint8_t *data
|
||||
|
||||
const bool typing = data[0] != 0;
|
||||
|
||||
set_friend_typing(m, i, typing);
|
||||
set_friend_typing(m, friendcon_id, typing);
|
||||
|
||||
if (m->friend_typingchange != nullptr) {
|
||||
m->friend_typingchange(m, i, typing, userdata);
|
||||
m->friend_typingchange(m, friendcon_id, typing, userdata);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
non_null(1, 3) nullable(6)
|
||||
static int m_handle_packet_message(Messenger *m, const int i, const uint8_t *data, const uint16_t data_length, const Message_Type message_type, void *userdata)
|
||||
static int m_handle_packet_message(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, const Message_Type message_type, void *userdata)
|
||||
{
|
||||
if (data_length == 0) {
|
||||
return 0;
|
||||
@ -2176,28 +2174,28 @@ static int m_handle_packet_message(Messenger *m, const int i, const uint8_t *dat
|
||||
message_terminated[message_length] = 0;
|
||||
|
||||
if (m->friend_message != nullptr) {
|
||||
m->friend_message(m, i, message_type, message_terminated, message_length, userdata);
|
||||
m->friend_message(m, friendcon_id, message_type, message_terminated, message_length, userdata);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
non_null(1, 3) nullable(5)
|
||||
static int m_handle_packet_invite_conference(Messenger *m, const int i, const uint8_t *data, const uint16_t data_length, void *userdata)
|
||||
static int m_handle_packet_invite_conference(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, void *userdata)
|
||||
{
|
||||
if (data_length == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (m->conference_invite != nullptr) {
|
||||
m->conference_invite(m, i, data, data_length, userdata);
|
||||
m->conference_invite(m, friendcon_id, data, data_length, userdata);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
non_null(1, 3) nullable(5)
|
||||
static int m_handle_packet_file_sendrequest(Messenger *m, const int i, const uint8_t *data, const uint16_t data_length, void *userdata)
|
||||
static int m_handle_packet_file_sendrequest(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, void *userdata)
|
||||
{
|
||||
const unsigned int head_length = 1 + sizeof(uint32_t) + sizeof(uint64_t) + FILE_ID_LENGTH;
|
||||
|
||||
@ -2213,7 +2211,7 @@ static int m_handle_packet_file_sendrequest(Messenger *m, const int i, const uin
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif /* UINT8_MAX >= MAX_CONCURRENT_FILE_PIPES */
|
||||
|
||||
uint64_t filesize;
|
||||
uint32_t file_type;
|
||||
@ -2227,7 +2225,7 @@ static int m_handle_packet_file_sendrequest(Messenger *m, const int i, const uin
|
||||
file_type = net_ntohl(file_type);
|
||||
|
||||
net_unpack_u64(data + 1 + sizeof(uint32_t), &filesize);
|
||||
struct File_Transfers *ft = &m->friendlist[i].file_receiving[filenumber];
|
||||
struct File_Transfers *ft = &m->friendlist[friendcon_id].file_receiving[filenumber];
|
||||
|
||||
if (ft->status != FILESTATUS_NONE) {
|
||||
return 0;
|
||||
@ -2254,7 +2252,7 @@ static int m_handle_packet_file_sendrequest(Messenger *m, const int i, const uin
|
||||
real_filenumber <<= 16;
|
||||
|
||||
if (m->file_sendrequest != nullptr) {
|
||||
m->file_sendrequest(m, i, real_filenumber, file_type, filesize, filename, filename_length,
|
||||
m->file_sendrequest(m, friendcon_id, real_filenumber, file_type, filesize, filename, filename_length,
|
||||
userdata);
|
||||
}
|
||||
|
||||
@ -2262,7 +2260,7 @@ static int m_handle_packet_file_sendrequest(Messenger *m, const int i, const uin
|
||||
}
|
||||
|
||||
non_null(1, 3) nullable(5)
|
||||
static int m_handle_packet_file_control(Messenger *m, const int i, const uint8_t *data, const uint16_t data_length, void *userdata)
|
||||
static int m_handle_packet_file_control(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, void *userdata)
|
||||
{
|
||||
if (data_length < 3) {
|
||||
return 0;
|
||||
@ -2281,9 +2279,9 @@ static int m_handle_packet_file_control(Messenger *m, const int i, const uint8_t
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif /* UINT8_MAX >= MAX_CONCURRENT_FILE_PIPES */
|
||||
|
||||
if (handle_filecontrol(m, i, outbound, filenumber, control_type, data + 3, data_length - 3, userdata) == -1) {
|
||||
if (handle_filecontrol(m, friendcon_id, outbound, filenumber, control_type, data + 3, data_length - 3, userdata) == -1) {
|
||||
// TODO(iphydf): Do something different here? Right now, this
|
||||
// check is pointless.
|
||||
return 0;
|
||||
@ -2293,7 +2291,7 @@ static int m_handle_packet_file_control(Messenger *m, const int i, const uint8_t
|
||||
}
|
||||
|
||||
non_null(1, 3) nullable(5)
|
||||
static int m_handle_packet_file_data(Messenger *m, const int i, const uint8_t *data, const uint16_t data_length, void *userdata)
|
||||
static int m_handle_packet_file_data(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, void *userdata)
|
||||
{
|
||||
if (data_length < 1) {
|
||||
return 0;
|
||||
@ -2307,9 +2305,9 @@ static int m_handle_packet_file_data(Messenger *m, const int i, const uint8_t *d
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif /* UINT8_MAX >= MAX_CONCURRENT_FILE_PIPES */
|
||||
|
||||
struct File_Transfers *ft = &m->friendlist[i].file_receiving[filenumber];
|
||||
struct File_Transfers *ft = &m->friendlist[friendcon_id].file_receiving[filenumber];
|
||||
|
||||
if (ft->status != FILESTATUS_TRANSFERRING) {
|
||||
return 0;
|
||||
@ -2334,7 +2332,7 @@ static int m_handle_packet_file_data(Messenger *m, const int i, const uint8_t *d
|
||||
}
|
||||
|
||||
if (m->file_filedata != nullptr) {
|
||||
m->file_filedata(m, i, real_filenumber, position, file_data, file_data_length, userdata);
|
||||
m->file_filedata(m, friendcon_id, real_filenumber, position, file_data, file_data_length, userdata);
|
||||
}
|
||||
|
||||
ft->transferred += file_data_length;
|
||||
@ -2346,7 +2344,7 @@ static int m_handle_packet_file_data(Messenger *m, const int i, const uint8_t *d
|
||||
|
||||
/* Full file received. */
|
||||
if (m->file_filedata != nullptr) {
|
||||
m->file_filedata(m, i, real_filenumber, position, file_data, file_data_length, userdata);
|
||||
m->file_filedata(m, friendcon_id, real_filenumber, position, file_data, file_data_length, userdata);
|
||||
}
|
||||
}
|
||||
|
||||
@ -2359,21 +2357,21 @@ static int m_handle_packet_file_data(Messenger *m, const int i, const uint8_t *d
|
||||
}
|
||||
|
||||
non_null(1, 3) nullable(5)
|
||||
static int m_handle_packet_msi(Messenger *m, const int i, const uint8_t *data, const uint16_t data_length, void *userdata)
|
||||
static int m_handle_packet_msi(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, void *userdata)
|
||||
{
|
||||
if (data_length == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (m->msi_packet != nullptr) {
|
||||
m->msi_packet(m, i, data, data_length, m->msi_packet_userdata);
|
||||
m->msi_packet(m, friendcon_id, data, data_length, m->msi_packet_userdata);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
non_null(1, 3) nullable(5)
|
||||
static int m_handle_packet_invite_groupchat(Messenger *m, const int i, const uint8_t *data, const uint16_t data_length, void *userdata)
|
||||
static int m_handle_packet_invite_groupchat(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, void *userdata)
|
||||
{
|
||||
// first two bytes are messenger packet type and group invite type
|
||||
if (data_length < 2 + GC_JOIN_DATA_LENGTH) {
|
||||
@ -2386,35 +2384,35 @@ static int m_handle_packet_invite_groupchat(Messenger *m, const int i, const uin
|
||||
|
||||
if (m->group_invite != nullptr && data[1] == GROUP_INVITE && data_length != 2 + GC_JOIN_DATA_LENGTH) {
|
||||
if (group_not_added(m->group_handler, join_data, join_data_len)) {
|
||||
m->group_invite(m, i, join_data, GC_JOIN_DATA_LENGTH,
|
||||
m->group_invite(m, friendcon_id, join_data, GC_JOIN_DATA_LENGTH,
|
||||
join_data + GC_JOIN_DATA_LENGTH, join_data_len - GC_JOIN_DATA_LENGTH, userdata);
|
||||
}
|
||||
} else if (invite_type == GROUP_INVITE_ACCEPTED) {
|
||||
handle_gc_invite_accepted_packet(m->group_handler, i, join_data, join_data_len);
|
||||
handle_gc_invite_accepted_packet(m->group_handler, friendcon_id, join_data, join_data_len);
|
||||
} else if (invite_type == GROUP_INVITE_CONFIRMATION) {
|
||||
handle_gc_invite_confirmed_packet(m->group_handler, i, join_data, join_data_len);
|
||||
handle_gc_invite_confirmed_packet(m->group_handler, friendcon_id, join_data, join_data_len);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
non_null(1, 3) nullable(5)
|
||||
static int m_handle_packet(void *object, int i, const uint8_t *temp, uint16_t len, void *userdata)
|
||||
static int m_handle_packet(void *object, int friendcon_id, const uint8_t *data, uint16_t length, void *userdata)
|
||||
{
|
||||
Messenger *m = (Messenger *)object;
|
||||
|
||||
if (len == 0) {
|
||||
if (length == 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
const uint8_t packet_id = temp[0];
|
||||
const uint8_t *data = temp + 1;
|
||||
const uint16_t data_length = len - 1;
|
||||
const uint8_t packet_id = data[0];
|
||||
const uint8_t *payload = data + 1;
|
||||
const uint16_t payload_length = length - 1;
|
||||
|
||||
if (m->friendlist[i].status != FRIEND_ONLINE) {
|
||||
if (packet_id == PACKET_ID_ONLINE && len == 1) {
|
||||
set_friend_status(m, i, FRIEND_ONLINE, userdata);
|
||||
send_online_packet(m, m->friendlist[i].friendcon_id);
|
||||
if (m->friendlist[friendcon_id].status != FRIEND_ONLINE) {
|
||||
if (packet_id == PACKET_ID_ONLINE && length == 1) {
|
||||
set_friend_status(m, friendcon_id, FRIEND_ONLINE, userdata);
|
||||
send_online_packet(m, m->friendlist[friendcon_id].friendcon_id);
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
@ -2423,34 +2421,34 @@ static int m_handle_packet(void *object, int i, const uint8_t *temp, uint16_t le
|
||||
switch (packet_id) {
|
||||
// TODO(Green-Sky): now all return 0 on error AND success, make errors errors?
|
||||
case PACKET_ID_OFFLINE:
|
||||
return m_handle_packet_offline(m, i, data, data_length, userdata);
|
||||
return m_handle_packet_offline(m, friendcon_id, payload, payload_length, userdata);
|
||||
case PACKET_ID_NICKNAME:
|
||||
return m_handle_packet_nickname(m, i, data, data_length, userdata);
|
||||
return m_handle_packet_nickname(m, friendcon_id, payload, payload_length, userdata);
|
||||
case PACKET_ID_STATUSMESSAGE:
|
||||
return m_handle_packet_statusmessage(m, i, data, data_length, userdata);
|
||||
return m_handle_packet_statusmessage(m, friendcon_id, payload, payload_length, userdata);
|
||||
case PACKET_ID_USERSTATUS:
|
||||
return m_handle_packet_userstatus(m, i, data, data_length, userdata);
|
||||
return m_handle_packet_userstatus(m, friendcon_id, payload, payload_length, userdata);
|
||||
case PACKET_ID_TYPING:
|
||||
return m_handle_packet_typing(m, i, data, data_length, userdata);
|
||||
return m_handle_packet_typing(m, friendcon_id, payload, payload_length, userdata);
|
||||
case PACKET_ID_MESSAGE:
|
||||
return m_handle_packet_message(m, i, data, data_length, MESSAGE_NORMAL, userdata);
|
||||
return m_handle_packet_message(m, friendcon_id, payload, payload_length, MESSAGE_NORMAL, userdata);
|
||||
case PACKET_ID_ACTION:
|
||||
return m_handle_packet_message(m, i, data, data_length, MESSAGE_ACTION, userdata);
|
||||
return m_handle_packet_message(m, friendcon_id, payload, payload_length, MESSAGE_ACTION, userdata);
|
||||
case PACKET_ID_INVITE_CONFERENCE:
|
||||
return m_handle_packet_invite_conference(m, i, data, data_length, userdata);
|
||||
return m_handle_packet_invite_conference(m, friendcon_id, payload, payload_length, userdata);
|
||||
case PACKET_ID_FILE_SENDREQUEST:
|
||||
return m_handle_packet_file_sendrequest(m, i, data, data_length, userdata);
|
||||
return m_handle_packet_file_sendrequest(m, friendcon_id, payload, payload_length, userdata);
|
||||
case PACKET_ID_FILE_CONTROL:
|
||||
return m_handle_packet_file_control(m, i, data, data_length, userdata);
|
||||
return m_handle_packet_file_control(m, friendcon_id, payload, payload_length, userdata);
|
||||
case PACKET_ID_FILE_DATA:
|
||||
return m_handle_packet_file_data(m, i, data, data_length, userdata);
|
||||
return m_handle_packet_file_data(m, friendcon_id, payload, payload_length, userdata);
|
||||
case PACKET_ID_MSI:
|
||||
return m_handle_packet_msi(m, i, data, data_length, userdata);
|
||||
return m_handle_packet_msi(m, friendcon_id, payload, payload_length, userdata);
|
||||
case PACKET_ID_INVITE_GROUPCHAT:
|
||||
return m_handle_packet_invite_groupchat(m, i, data, data_length, userdata);
|
||||
return m_handle_packet_invite_groupchat(m, friendcon_id, payload, payload_length, userdata);
|
||||
}
|
||||
|
||||
return handle_custom_lossless_packet(object, i, temp, len, userdata);
|
||||
return handle_custom_lossless_packet(object, friendcon_id, data, length, userdata);
|
||||
}
|
||||
|
||||
non_null(1) nullable(2)
|
||||
@ -2461,8 +2459,8 @@ static void do_friends(Messenger *m, void *userdata)
|
||||
for (uint32_t i = 0; i < m->numfriends; ++i) {
|
||||
if (m->friendlist[i].status == FRIEND_ADDED) {
|
||||
const int fr = send_friend_request_packet(m->fr_c, m->friendlist[i].friendcon_id, m->friendlist[i].friendrequest_nospam,
|
||||
m->friendlist[i].info,
|
||||
m->friendlist[i].info_size);
|
||||
m->friendlist[i].info,
|
||||
m->friendlist[i].info_size);
|
||||
|
||||
if (fr >= 0) {
|
||||
set_friend_status(m, i, FRIEND_REQUESTED, userdata);
|
||||
@ -2528,7 +2526,6 @@ static void m_connection_status_callback(Messenger *m, void *userdata)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#define DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS 60UL
|
||||
|
||||
#define IDSTRING_LEN (CRYPTO_PUBLIC_KEY_SIZE * 2 + 1)
|
||||
@ -2591,14 +2588,14 @@ static bool self_announce_group(const Messenger *m, GC_Chat *chat, Onion_Friend
|
||||
}
|
||||
|
||||
announce.base_announce.tcp_relays_count = (uint8_t)tcp_num;
|
||||
announce.base_announce.ip_port_is_set = (uint8_t)(ip_port_is_set ? 1 : 0);
|
||||
announce.base_announce.ip_port_is_set = ip_port_is_set;
|
||||
|
||||
if (ip_port_is_set) {
|
||||
memcpy(&announce.base_announce.ip_port, &chat->self_ip_port, sizeof(IP_Port));
|
||||
}
|
||||
|
||||
memcpy(announce.base_announce.peer_public_key, chat->self_public_key, ENC_PUBLIC_KEY_SIZE);
|
||||
memcpy(announce.chat_public_key, get_chat_id(chat->chat_public_key), ENC_PUBLIC_KEY_SIZE);
|
||||
memcpy(announce.base_announce.peer_public_key, chat->self_public_key.enc, ENC_PUBLIC_KEY_SIZE);
|
||||
memcpy(announce.chat_public_key, get_chat_id(&chat->chat_public_key), ENC_PUBLIC_KEY_SIZE);
|
||||
|
||||
uint8_t gc_data[GCA_MAX_DATA_LENGTH];
|
||||
const int length = gca_pack_public_announce(m->log, gc_data, GCA_MAX_DATA_LENGTH, &announce);
|
||||
@ -2620,7 +2617,7 @@ static bool self_announce_group(const Messenger *m, GC_Chat *chat, Onion_Friend
|
||||
if (tcp_num > 0) {
|
||||
pk_copy(chat->announced_tcp_relay_pk, announce.base_announce.tcp_relays[0].public_key);
|
||||
} else {
|
||||
memset(chat->announced_tcp_relay_pk, 0, sizeof(chat->announced_tcp_relay_pk));
|
||||
memzero(chat->announced_tcp_relay_pk, sizeof(chat->announced_tcp_relay_pk));
|
||||
}
|
||||
|
||||
LOGGER_DEBUG(chat->log, "Published group announce. TCP relays: %d, UDP status: %d", tcp_num,
|
||||
@ -2721,7 +2718,6 @@ void do_messenger(Messenger *m, void *userdata)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* dht contains additional "friends" (requests) */
|
||||
const uint32_t num_dhtfriends = dht_get_num_friends(m->dht);
|
||||
VLA(int32_t, m2dht, num_dhtfriends);
|
||||
@ -2889,7 +2885,6 @@ static uint8_t *friend_save(const struct Saved_Friend *temp, uint8_t *data)
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
non_null()
|
||||
static const uint8_t *friend_load(struct Saved_Friend *temp, const uint8_t *data)
|
||||
{
|
||||
@ -2927,7 +2922,6 @@ static const uint8_t *friend_load(struct Saved_Friend *temp, const uint8_t *data
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
non_null()
|
||||
static uint32_t m_state_plugins_size(const Messenger *m)
|
||||
{
|
||||
@ -2956,7 +2950,7 @@ bool m_register_state_plugin(Messenger *m, State_Type type, m_state_size_cb *siz
|
||||
{
|
||||
const uint32_t new_length = m->options.state_plugins_length + 1;
|
||||
Messenger_State_Plugin *temp = (Messenger_State_Plugin *)mem_vrealloc(
|
||||
m->mem, m->options.state_plugins, new_length, sizeof(Messenger_State_Plugin));
|
||||
m->mem, m->options.state_plugins, new_length, sizeof(Messenger_State_Plugin));
|
||||
|
||||
if (temp == nullptr) {
|
||||
return false;
|
||||
@ -3117,7 +3111,7 @@ static uint8_t *friends_list_save(const Messenger *m, uint8_t *data)
|
||||
assert(next_data - cur_data == friend_size());
|
||||
#ifdef __LP64__
|
||||
assert(memcmp(cur_data, &temp, friend_size()) == 0);
|
||||
#endif
|
||||
#endif /* __LP64__ */
|
||||
cur_data = next_data;
|
||||
++num;
|
||||
}
|
||||
@ -3191,7 +3185,7 @@ static void pack_groupchats(const GC_Session *c, Bin_Pack *bp)
|
||||
}
|
||||
|
||||
non_null()
|
||||
static bool pack_groupchats_handler(Bin_Pack *bp, const Logger *log, const void *obj)
|
||||
static bool pack_groupchats_handler(const void *obj, const Logger *logger, Bin_Pack *bp)
|
||||
{
|
||||
const GC_Session *session = (const GC_Session *)obj;
|
||||
pack_groupchats(session, bp);
|
||||
@ -3201,8 +3195,8 @@ static bool pack_groupchats_handler(Bin_Pack *bp, const Logger *log, const void
|
||||
non_null()
|
||||
static uint32_t saved_groups_size(const Messenger *m)
|
||||
{
|
||||
const GC_Session *c = m->group_handler;
|
||||
return bin_pack_obj_size(pack_groupchats_handler, m->log, c);
|
||||
const GC_Session *session = m->group_handler;
|
||||
return bin_pack_obj_size(pack_groupchats_handler, session, m->log);
|
||||
}
|
||||
|
||||
non_null()
|
||||
@ -3224,7 +3218,7 @@ static uint8_t *groups_save(const Messenger *m, uint8_t *data)
|
||||
|
||||
data = state_write_section_header(data, STATE_COOKIE_TYPE, len, STATE_TYPE_GROUPS);
|
||||
|
||||
if (!bin_pack_obj(pack_groupchats_handler, m->log, c, data, len)) {
|
||||
if (!bin_pack_obj(pack_groupchats_handler, c, m->log, data, len)) {
|
||||
LOGGER_FATAL(m->log, "failed to pack group chats into buffer of length %u", len);
|
||||
return data;
|
||||
}
|
||||
@ -3237,22 +3231,17 @@ static uint8_t *groups_save(const Messenger *m, uint8_t *data)
|
||||
}
|
||||
|
||||
non_null()
|
||||
static State_Load_Status groups_load(Messenger *m, const uint8_t *data, uint32_t length)
|
||||
static bool handle_groups_load(void *obj, Bin_Unpack *bu)
|
||||
{
|
||||
Bin_Unpack *bu = bin_unpack_new(data, length);
|
||||
if (bu == nullptr) {
|
||||
LOGGER_ERROR(m->log, "failed to allocate binary unpacker");
|
||||
return STATE_LOAD_STATUS_ERROR;
|
||||
}
|
||||
Messenger *m = (Messenger *)obj;
|
||||
|
||||
uint32_t num_groups;
|
||||
if (!bin_unpack_array(bu, &num_groups)) {
|
||||
LOGGER_ERROR(m->log, "msgpack failed to unpack groupchats array: expected array");
|
||||
bin_unpack_free(bu);
|
||||
return STATE_LOAD_STATUS_ERROR;
|
||||
return false;
|
||||
}
|
||||
|
||||
LOGGER_DEBUG(m->log, "Loading %u groups (length %u)", num_groups, length);
|
||||
LOGGER_DEBUG(m->log, "Loading %u groups", num_groups);
|
||||
|
||||
for (uint32_t i = 0; i < num_groups; ++i) {
|
||||
const int group_number = gc_group_load(m->group_handler, bu);
|
||||
@ -3266,7 +3255,16 @@ static State_Load_Status groups_load(Messenger *m, const uint8_t *data, uint32_t
|
||||
|
||||
LOGGER_DEBUG(m->log, "Successfully loaded %u groups", gc_count_groups(m->group_handler));
|
||||
|
||||
bin_unpack_free(bu);
|
||||
return true;
|
||||
}
|
||||
|
||||
non_null()
|
||||
static State_Load_Status groups_load(Messenger *m, const uint8_t *data, uint32_t length)
|
||||
{
|
||||
if (!bin_unpack_obj(handle_groups_load, m, data, length)) {
|
||||
LOGGER_ERROR(m->log, "msgpack failed to unpack groupchats array");
|
||||
return STATE_LOAD_STATUS_ERROR;
|
||||
}
|
||||
|
||||
return STATE_LOAD_STATUS_CONTINUE;
|
||||
}
|
||||
@ -3412,10 +3410,9 @@ static uint32_t path_node_size(const Messenger *m)
|
||||
non_null()
|
||||
static uint8_t *save_path_nodes(const Messenger *m, uint8_t *data)
|
||||
{
|
||||
Node_format nodes[NUM_SAVED_PATH_NODES];
|
||||
Node_format nodes[NUM_SAVED_PATH_NODES] = {{{0}}};
|
||||
uint8_t *temp_data = data;
|
||||
data = state_write_section_header(data, STATE_COOKIE_TYPE, 0, STATE_TYPE_PATH_NODE);
|
||||
memset(nodes, 0, sizeof(nodes));
|
||||
const unsigned int num = onion_backup_nodes(m->onion_c, nodes, NUM_SAVED_PATH_NODES);
|
||||
const int l = pack_nodes(m->log, data, NUM_SAVED_PATH_NODES * packed_node_size(net_family_tcp_ipv6()), nodes, num);
|
||||
|
||||
@ -3457,7 +3454,9 @@ static void m_register_default_plugins(Messenger *m)
|
||||
m_register_state_plugin(m, STATE_TYPE_STATUSMESSAGE, status_message_size, load_status_message,
|
||||
save_status_message);
|
||||
m_register_state_plugin(m, STATE_TYPE_STATUS, status_size, load_status, save_status);
|
||||
m_register_state_plugin(m, STATE_TYPE_GROUPS, saved_groups_size, groups_load, groups_save);
|
||||
if (m->options.groups_persistence_enabled) {
|
||||
m_register_state_plugin(m, STATE_TYPE_GROUPS, saved_groups_size, groups_load, groups_save);
|
||||
}
|
||||
m_register_state_plugin(m, STATE_TYPE_TCP_RELAY, tcp_relay_size, load_tcp_relays, save_tcp_relays);
|
||||
m_register_state_plugin(m, STATE_TYPE_PATH_NODE, path_node_size, load_path_nodes, save_path_nodes);
|
||||
}
|
||||
|
39
external/toxcore/c-toxcore/toxcore/Messenger.h
vendored
39
external/toxcore/c-toxcore/toxcore/Messenger.h
vendored
@ -10,15 +10,25 @@
|
||||
#ifndef C_TOXCORE_TOXCORE_MESSENGER_H
|
||||
#define C_TOXCORE_TOXCORE_MESSENGER_H
|
||||
|
||||
#include "DHT.h"
|
||||
#include "TCP_client.h"
|
||||
#include "TCP_server.h"
|
||||
#include "announce.h"
|
||||
#include "attributes.h"
|
||||
#include "crypto_core.h"
|
||||
#include "forwarding.h"
|
||||
#include "friend_connection.h"
|
||||
#include "friend_requests.h"
|
||||
#include "group_announce.h"
|
||||
#include "group_common.h"
|
||||
#include "logger.h"
|
||||
#include "mem.h"
|
||||
#include "mono_time.h"
|
||||
#include "net_crypto.h"
|
||||
#include "network.h"
|
||||
#include "onion.h"
|
||||
#include "onion_announce.h"
|
||||
#include "onion_client.h"
|
||||
#include "state.h"
|
||||
|
||||
#define MAX_NAME_LENGTH 128
|
||||
@ -29,7 +39,6 @@
|
||||
/* This cannot be bigger than 256 */
|
||||
#define MAX_CONCURRENT_FILE_PIPES 256
|
||||
|
||||
|
||||
#define FRIEND_ADDRESS_SIZE (CRYPTO_PUBLIC_KEY_SIZE + sizeof(uint32_t) + sizeof(uint16_t))
|
||||
|
||||
typedef enum Message_Type {
|
||||
@ -41,7 +50,7 @@ typedef enum Message_Type {
|
||||
#ifndef MESSENGER_DEFINED
|
||||
#define MESSENGER_DEFINED
|
||||
typedef struct Messenger Messenger;
|
||||
#endif // MESSENGER_DEFINED
|
||||
#endif /* MESSENGER_DEFINED */
|
||||
|
||||
// Returns the size of the data
|
||||
typedef uint32_t m_state_size_cb(const Messenger *m);
|
||||
@ -69,6 +78,7 @@ typedef struct Messenger_Options {
|
||||
bool hole_punching_enabled;
|
||||
bool local_discovery_enabled;
|
||||
bool dht_announcements_enabled;
|
||||
bool groups_persistence_enabled;
|
||||
|
||||
logger_cb *log_callback;
|
||||
void *log_context;
|
||||
@ -78,7 +88,6 @@ typedef struct Messenger_Options {
|
||||
uint8_t state_plugins_length;
|
||||
} Messenger_Options;
|
||||
|
||||
|
||||
struct Receipts {
|
||||
uint32_t packet_num;
|
||||
uint32_t msg_id;
|
||||
@ -108,7 +117,6 @@ typedef enum Friend_Add_Error {
|
||||
FAERR_NOMEM = -8,
|
||||
} Friend_Add_Error;
|
||||
|
||||
|
||||
/** Default start timeout in seconds between friend requests. */
|
||||
#define FRIENDREQUEST_TIMEOUT 5
|
||||
|
||||
@ -166,7 +174,6 @@ typedef enum Filekind {
|
||||
FILEKIND_AVATAR,
|
||||
} Filekind;
|
||||
|
||||
|
||||
typedef void m_self_connection_status_cb(Messenger *m, Onion_Connection_Status connection_status, void *user_data);
|
||||
typedef void m_friend_status_cb(Messenger *m, uint32_t friend_number, unsigned int status, void *user_data);
|
||||
typedef void m_friend_connection_status_cb(Messenger *m, uint32_t friend_number, unsigned int connection_status,
|
||||
@ -194,14 +201,14 @@ typedef void m_friend_lossy_packet_cb(Messenger *m, uint32_t friend_number, uint
|
||||
typedef void m_friend_lossless_packet_cb(Messenger *m, uint32_t friend_number, uint8_t packet_id, const uint8_t *data,
|
||||
size_t length, void *user_data);
|
||||
typedef void m_friend_connectionstatuschange_internal_cb(Messenger *m, uint32_t friend_number,
|
||||
uint8_t connection_status, void *user_data);
|
||||
bool is_online, void *user_data);
|
||||
typedef void m_conference_invite_cb(Messenger *m, uint32_t friend_number, const uint8_t *cookie, uint16_t length,
|
||||
void *user_data);
|
||||
typedef void m_group_invite_cb(const Messenger *m, uint32_t friendnumber, const uint8_t *data, size_t length,
|
||||
const uint8_t *group_name, size_t group_name_length, void *userdata);
|
||||
typedef void m_group_invite_cb(const Messenger *m, uint32_t friend_number, const uint8_t *invite_data, size_t length,
|
||||
const uint8_t *group_name, size_t group_name_length, void *user_data);
|
||||
typedef void m_msi_packet_cb(Messenger *m, uint32_t friend_number, const uint8_t *data, uint16_t length,
|
||||
void *user_data);
|
||||
typedef int m_lossy_rtp_packet_cb(Messenger *m, uint32_t friendnumber, const uint8_t *data, uint16_t len, void *object);
|
||||
typedef int m_lossy_rtp_packet_cb(Messenger *m, uint32_t friend_number, const uint8_t *data, uint16_t length, void *object);
|
||||
|
||||
typedef struct RTP_Packet_Handler {
|
||||
m_lossy_rtp_packet_cb *function;
|
||||
@ -359,7 +366,6 @@ void getaddress(const Messenger *m, uint8_t *address);
|
||||
non_null()
|
||||
int32_t m_addfriend(Messenger *m, const uint8_t *address, const uint8_t *data, uint16_t length);
|
||||
|
||||
|
||||
/** @brief Add a friend without sending a friendrequest.
|
||||
* @return the friend number if success.
|
||||
* @retval -3 if user's own key.
|
||||
@ -449,7 +455,6 @@ non_null(1, 4) nullable(6)
|
||||
int m_send_message_generic(Messenger *m, int32_t friendnumber, uint8_t type, const uint8_t *message, uint32_t length,
|
||||
uint32_t *message_id);
|
||||
|
||||
|
||||
/** @brief Set the name and name_length of a friend.
|
||||
*
|
||||
* name must be a string of maximum MAX_NAME_LENGTH length.
|
||||
@ -540,7 +545,6 @@ non_null() int m_copy_self_statusmessage(const Messenger *m, uint8_t *buf);
|
||||
non_null() uint8_t m_get_userstatus(const Messenger *m, int32_t friendnumber);
|
||||
non_null() uint8_t m_get_self_userstatus(const Messenger *m);
|
||||
|
||||
|
||||
/** @brief returns timestamp of last time friendnumber was seen online or 0 if never seen.
|
||||
* if friendnumber is invalid this function will return UINT64_MAX.
|
||||
*/
|
||||
@ -614,7 +618,6 @@ non_null() void m_callback_connectionstatus(Messenger *m, m_friend_connection_st
|
||||
non_null() void m_callback_connectionstatus_internal_av(
|
||||
Messenger *m, m_friend_connectionstatuschange_internal_cb *function, void *userdata);
|
||||
|
||||
|
||||
/** @brief Set the callback for typing changes. */
|
||||
non_null() void m_callback_core_connection(Messenger *m, m_self_connection_status_cb *function);
|
||||
|
||||
@ -629,7 +632,6 @@ void m_callback_conference_invite(Messenger *m, m_conference_invite_cb *function
|
||||
non_null(1) nullable(2)
|
||||
void m_callback_group_invite(Messenger *m, m_group_invite_cb *function);
|
||||
|
||||
|
||||
/** @brief Send a conference invite packet.
|
||||
*
|
||||
* return true on success
|
||||
@ -646,16 +648,13 @@ bool send_conference_invite_packet(const Messenger *m, int32_t friendnumber, con
|
||||
* return true on success
|
||||
*/
|
||||
non_null()
|
||||
bool send_group_invite_packet(const Messenger *m, uint32_t friendnumber, const uint8_t *data, uint16_t length);
|
||||
|
||||
bool send_group_invite_packet(const Messenger *m, uint32_t friendnumber, const uint8_t *packet, uint16_t length);
|
||||
|
||||
/*** FILE SENDING */
|
||||
|
||||
|
||||
/** @brief Set the callback for file send requests. */
|
||||
non_null() void callback_file_sendrequest(Messenger *m, m_file_recv_cb *function);
|
||||
|
||||
|
||||
/** @brief Set the callback for file control requests. */
|
||||
non_null() void callback_file_control(Messenger *m, m_file_recv_control_cb *function);
|
||||
|
||||
@ -665,7 +664,6 @@ non_null() void callback_file_data(Messenger *m, m_file_recv_chunk_cb *function)
|
||||
/** @brief Set the callback for file request chunk. */
|
||||
non_null() void callback_file_reqchunk(Messenger *m, m_file_chunk_request_cb *function);
|
||||
|
||||
|
||||
/** @brief Copy the file transfer file id to file_id
|
||||
*
|
||||
* @retval 0 on success.
|
||||
@ -780,7 +778,6 @@ non_null() void custom_lossy_packet_registerhandler(Messenger *m, m_friend_lossy
|
||||
non_null()
|
||||
int m_send_custom_lossy_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint32_t length);
|
||||
|
||||
|
||||
/** @brief Set handlers for custom lossless packets. */
|
||||
non_null()
|
||||
void custom_lossless_packet_registerhandler(Messenger *m, m_friend_lossless_packet_cb *lossless_packethandler);
|
||||
@ -890,4 +887,4 @@ uint32_t copy_friendlist(const Messenger *m, uint32_t *out_list, uint32_t list_s
|
||||
non_null()
|
||||
bool m_is_receiving_file(Messenger *m);
|
||||
|
||||
#endif
|
||||
#endif /* C_TOXCORE_TOXCORE_MESSENGER_H */
|
||||
|
37
external/toxcore/c-toxcore/toxcore/TCP_client.c
vendored
37
external/toxcore/c-toxcore/toxcore/TCP_client.c
vendored
@ -12,8 +12,8 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "DHT.h"
|
||||
#include "TCP_common.h"
|
||||
#include "attributes.h"
|
||||
#include "ccompat.h"
|
||||
#include "crypto_core.h"
|
||||
#include "forwarding.h"
|
||||
@ -285,7 +285,7 @@ static int proxy_socks5_read_connection_response(const Logger *logger, const TCP
|
||||
} else {
|
||||
uint8_t data[4 + sizeof(IP6) + sizeof(uint16_t)];
|
||||
const TCP_Connection *con = &tcp_conn->con;
|
||||
int ret = read_tcp_packet(logger, con->mem, con->ns, con->sock, data, sizeof(data), &con->ip_port);
|
||||
const int ret = read_tcp_packet(logger, con->mem, con->ns, con->sock, data, sizeof(data), &con->ip_port);
|
||||
|
||||
if (ret == -1) {
|
||||
return 0;
|
||||
@ -313,7 +313,7 @@ static int generate_handshake(TCP_Client_Connection *tcp_conn)
|
||||
memcpy(tcp_conn->con.last_packet, tcp_conn->self_public_key, CRYPTO_PUBLIC_KEY_SIZE);
|
||||
random_nonce(tcp_conn->con.rng, tcp_conn->con.last_packet + CRYPTO_PUBLIC_KEY_SIZE);
|
||||
const int len = encrypt_data_symmetric(tcp_conn->con.shared_key, tcp_conn->con.last_packet + CRYPTO_PUBLIC_KEY_SIZE, plain,
|
||||
sizeof(plain), tcp_conn->con.last_packet + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE);
|
||||
sizeof(plain), tcp_conn->con.last_packet + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE);
|
||||
|
||||
if (len != sizeof(plain) + CRYPTO_MAC_SIZE) {
|
||||
return -1;
|
||||
@ -335,7 +335,7 @@ static int handle_handshake(TCP_Client_Connection *tcp_conn, const uint8_t *data
|
||||
{
|
||||
uint8_t plain[CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE];
|
||||
const int len = decrypt_data_symmetric(tcp_conn->con.shared_key, data, data + CRYPTO_NONCE_SIZE,
|
||||
TCP_SERVER_HANDSHAKE_SIZE - CRYPTO_NONCE_SIZE, plain);
|
||||
TCP_SERVER_HANDSHAKE_SIZE - CRYPTO_NONCE_SIZE, plain);
|
||||
|
||||
if (len != sizeof(plain)) {
|
||||
return -1;
|
||||
@ -394,10 +394,11 @@ int send_data(const Logger *logger, TCP_Client_Connection *con, uint8_t con_id,
|
||||
return 0;
|
||||
}
|
||||
|
||||
VLA(uint8_t, packet, 1 + length);
|
||||
const uint16_t packet_size = 1 + length;
|
||||
VLA(uint8_t, packet, packet_size);
|
||||
packet[0] = con_id + NUM_RESERVED_PORTS;
|
||||
memcpy(packet + 1, data, length);
|
||||
return write_packet_tcp_secure_connection(logger, &con->con, packet, SIZEOF_VLA(packet), false);
|
||||
return write_packet_tcp_secure_connection(logger, &con->con, packet, packet_size, false);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -412,14 +413,14 @@ int send_oob_packet(const Logger *logger, TCP_Client_Connection *con, const uint
|
||||
return -1;
|
||||
}
|
||||
|
||||
VLA(uint8_t, packet, 1 + CRYPTO_PUBLIC_KEY_SIZE + length);
|
||||
const uint16_t packet_size = 1 + CRYPTO_PUBLIC_KEY_SIZE + length;
|
||||
VLA(uint8_t, packet, packet_size);
|
||||
packet[0] = TCP_PACKET_OOB_SEND;
|
||||
memcpy(packet + 1, public_key, CRYPTO_PUBLIC_KEY_SIZE);
|
||||
memcpy(packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, data, length);
|
||||
return write_packet_tcp_secure_connection(logger, &con->con, packet, SIZEOF_VLA(packet), false);
|
||||
return write_packet_tcp_secure_connection(logger, &con->con, packet, packet_size, false);
|
||||
}
|
||||
|
||||
|
||||
/** @brief Set the number that will be used as an argument in the callbacks related to con_id.
|
||||
*
|
||||
* When not set by this function, the number is -1.
|
||||
@ -536,10 +537,11 @@ int send_disconnect_request(const Logger *logger, TCP_Client_Connection *con, ui
|
||||
*/
|
||||
int send_onion_request(const Logger *logger, TCP_Client_Connection *con, const uint8_t *data, uint16_t length)
|
||||
{
|
||||
VLA(uint8_t, packet, 1 + length);
|
||||
const uint16_t packet_size = 1 + length;
|
||||
VLA(uint8_t, packet, packet_size);
|
||||
packet[0] = TCP_PACKET_ONION_REQUEST;
|
||||
memcpy(packet + 1, data, length);
|
||||
return write_packet_tcp_secure_connection(logger, &con->con, packet, SIZEOF_VLA(packet), false);
|
||||
return write_packet_tcp_secure_connection(logger, &con->con, packet, packet_size, false);
|
||||
}
|
||||
|
||||
void onion_response_handler(TCP_Client_Connection *con, tcp_onion_response_cb *onion_callback, void *object)
|
||||
@ -578,9 +580,9 @@ void forwarding_handler(TCP_Client_Connection *con, forwarded_response_cb *forwa
|
||||
|
||||
/** Create new TCP connection to ip_port/public_key */
|
||||
TCP_Client_Connection *new_tcp_connection(
|
||||
const Logger *logger, const Memory *mem, const Mono_Time *mono_time, const Random *rng, const Network *ns,
|
||||
const IP_Port *ip_port, const uint8_t *public_key, const uint8_t *self_public_key, const uint8_t *self_secret_key,
|
||||
const TCP_Proxy_Info *proxy_info)
|
||||
const Logger *logger, const Memory *mem, const Mono_Time *mono_time, const Random *rng, const Network *ns,
|
||||
const IP_Port *ip_port, const uint8_t *public_key, const uint8_t *self_public_key, const uint8_t *self_secret_key,
|
||||
const TCP_Proxy_Info *proxy_info)
|
||||
{
|
||||
assert(logger != nullptr);
|
||||
assert(mem != nullptr);
|
||||
@ -871,7 +873,8 @@ non_null(1, 2) nullable(3)
|
||||
static bool tcp_process_packet(const Logger *logger, TCP_Client_Connection *conn, void *userdata)
|
||||
{
|
||||
uint8_t packet[MAX_PACKET_SIZE];
|
||||
const int len = read_packet_tcp_secure_connection(logger, conn->con.mem, conn->con.ns, conn->con.sock, &conn->next_packet_length, conn->con.shared_key, conn->recv_nonce, packet, sizeof(packet), &conn->ip_port);
|
||||
const int len = read_packet_tcp_secure_connection(logger, conn->con.mem, conn->con.ns, conn->con.sock, &conn->next_packet_length, conn->con.shared_key, conn->recv_nonce, packet, sizeof(packet),
|
||||
&conn->ip_port);
|
||||
|
||||
if (len == 0) {
|
||||
return false;
|
||||
@ -949,7 +952,7 @@ void do_tcp_connection(const Logger *logger, const Mono_Time *mono_time,
|
||||
|
||||
if (tcp_connection->status == TCP_CLIENT_PROXY_SOCKS5_CONNECTING) {
|
||||
if (send_pending_data(logger, &tcp_connection->con) == 0) {
|
||||
int ret = socks5_read_handshake_response(logger, tcp_connection);
|
||||
const int ret = socks5_read_handshake_response(logger, tcp_connection);
|
||||
|
||||
if (ret == -1) {
|
||||
tcp_connection->kill_at = 0;
|
||||
@ -965,7 +968,7 @@ void do_tcp_connection(const Logger *logger, const Mono_Time *mono_time,
|
||||
|
||||
if (tcp_connection->status == TCP_CLIENT_PROXY_SOCKS5_UNCONFIRMED) {
|
||||
if (send_pending_data(logger, &tcp_connection->con) == 0) {
|
||||
int ret = proxy_socks5_read_connection_response(logger, tcp_connection);
|
||||
const int ret = proxy_socks5_read_connection_response(logger, tcp_connection);
|
||||
|
||||
if (ret == -1) {
|
||||
tcp_connection->kill_at = 0;
|
||||
|
12
external/toxcore/c-toxcore/toxcore/TCP_client.h
vendored
12
external/toxcore/c-toxcore/toxcore/TCP_client.h
vendored
@ -9,8 +9,11 @@
|
||||
#ifndef C_TOXCORE_TOXCORE_TCP_CLIENT_H
|
||||
#define C_TOXCORE_TOXCORE_TCP_CLIENT_H
|
||||
|
||||
#include "attributes.h"
|
||||
#include "crypto_core.h"
|
||||
#include "forwarding.h"
|
||||
#include "logger.h"
|
||||
#include "mem.h"
|
||||
#include "mono_time.h"
|
||||
#include "network.h"
|
||||
|
||||
@ -59,9 +62,9 @@ void tcp_con_set_custom_uint(TCP_Client_Connection *con, uint32_t value);
|
||||
/** Create new TCP connection to ip_port/public_key */
|
||||
non_null(1, 2, 3, 4, 5, 6, 7, 8, 9) nullable(10)
|
||||
TCP_Client_Connection *new_tcp_connection(
|
||||
const Logger *logger, const Memory *mem, const Mono_Time *mono_time, const Random *rng, const Network *ns,
|
||||
const IP_Port *ip_port, const uint8_t *public_key, const uint8_t *self_public_key, const uint8_t *self_secret_key,
|
||||
const TCP_Proxy_Info *proxy_info);
|
||||
const Logger *logger, const Memory *mem, const Mono_Time *mono_time, const Random *rng, const Network *ns,
|
||||
const IP_Port *ip_port, const uint8_t *public_key, const uint8_t *self_public_key, const uint8_t *self_secret_key,
|
||||
const TCP_Proxy_Info *proxy_info);
|
||||
|
||||
/** Run the TCP connection */
|
||||
non_null(1, 2, 3) nullable(4)
|
||||
@ -150,5 +153,4 @@ int send_oob_packet(const Logger *logger, TCP_Client_Connection *con, const uint
|
||||
non_null()
|
||||
void oob_data_handler(TCP_Client_Connection *con, tcp_oob_data_cb *oob_data_callback, void *object);
|
||||
|
||||
|
||||
#endif
|
||||
#endif /* C_TOXCORE_TOXCORE_TCP_CLIENT_H */
|
||||
|
30
external/toxcore/c-toxcore/toxcore/TCP_common.c
vendored
30
external/toxcore/c-toxcore/toxcore/TCP_common.c
vendored
@ -7,6 +7,7 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "attributes.h"
|
||||
#include "ccompat.h"
|
||||
#include "crypto_core.h"
|
||||
#include "logger.h"
|
||||
@ -151,18 +152,19 @@ int write_packet_tcp_secure_connection(const Logger *logger, TCP_Connection *con
|
||||
}
|
||||
}
|
||||
|
||||
VLA(uint8_t, packet, sizeof(uint16_t) + length + CRYPTO_MAC_SIZE);
|
||||
const uint16_t packet_size = sizeof(uint16_t) + length + CRYPTO_MAC_SIZE;
|
||||
VLA(uint8_t, packet, packet_size);
|
||||
|
||||
uint16_t c_length = net_htons(length + CRYPTO_MAC_SIZE);
|
||||
memcpy(packet, &c_length, sizeof(uint16_t));
|
||||
int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t));
|
||||
|
||||
if ((unsigned int)len != (SIZEOF_VLA(packet) - sizeof(uint16_t))) {
|
||||
if ((unsigned int)len != (packet_size - sizeof(uint16_t))) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (priority) {
|
||||
len = sendpriority ? net_send(con->ns, logger, con->sock, packet, SIZEOF_VLA(packet), &con->ip_port) : 0;
|
||||
len = sendpriority ? net_send(con->ns, logger, con->sock, packet, packet_size, &con->ip_port) : 0;
|
||||
|
||||
if (len <= 0) {
|
||||
len = 0;
|
||||
@ -170,14 +172,14 @@ int write_packet_tcp_secure_connection(const Logger *logger, TCP_Connection *con
|
||||
|
||||
increment_nonce(con->sent_nonce);
|
||||
|
||||
if ((unsigned int)len == SIZEOF_VLA(packet)) {
|
||||
if ((unsigned int)len == packet_size) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return add_priority(con, packet, SIZEOF_VLA(packet), len) ? 1 : 0;
|
||||
return add_priority(con, packet, packet_size, len) ? 1 : 0;
|
||||
}
|
||||
|
||||
len = net_send(con->ns, logger, con->sock, packet, SIZEOF_VLA(packet), &con->ip_port);
|
||||
len = net_send(con->ns, logger, con->sock, packet, packet_size, &con->ip_port);
|
||||
|
||||
if (len <= 0) {
|
||||
return 0;
|
||||
@ -185,12 +187,12 @@ int write_packet_tcp_secure_connection(const Logger *logger, TCP_Connection *con
|
||||
|
||||
increment_nonce(con->sent_nonce);
|
||||
|
||||
if ((unsigned int)len == SIZEOF_VLA(packet)) {
|
||||
if ((unsigned int)len == packet_size) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
memcpy(con->last_packet, packet, SIZEOF_VLA(packet));
|
||||
con->last_packet_length = SIZEOF_VLA(packet);
|
||||
memcpy(con->last_packet, packet, packet_size);
|
||||
con->last_packet_length = packet_size;
|
||||
con->last_packet_sent = len;
|
||||
return 1;
|
||||
}
|
||||
@ -201,7 +203,7 @@ int write_packet_tcp_secure_connection(const Logger *logger, TCP_Connection *con
|
||||
* return -1 on failure/no data in buffer.
|
||||
*/
|
||||
int read_tcp_packet(
|
||||
const Logger *logger, const Memory *mem, const Network *ns, Socket sock, uint8_t *data, uint16_t length, const IP_Port *ip_port)
|
||||
const Logger *logger, const Memory *mem, const Network *ns, Socket sock, uint8_t *data, uint16_t length, const IP_Port *ip_port)
|
||||
{
|
||||
const uint16_t count = net_socket_data_recv_buffer(ns, sock);
|
||||
|
||||
@ -265,10 +267,10 @@ static uint16_t read_tcp_length(const Logger *logger, const Memory *mem, const N
|
||||
* @retval -1 on failure (connection must be killed).
|
||||
*/
|
||||
int read_packet_tcp_secure_connection(
|
||||
const Logger *logger, const Memory *mem, const Network *ns,
|
||||
Socket sock, uint16_t *next_packet_length,
|
||||
const uint8_t *shared_key, uint8_t *recv_nonce, uint8_t *data,
|
||||
uint16_t max_len, const IP_Port *ip_port)
|
||||
const Logger *logger, const Memory *mem, const Network *ns,
|
||||
Socket sock, uint16_t *next_packet_length,
|
||||
const uint8_t *shared_key, uint8_t *recv_nonce, uint8_t *data,
|
||||
uint16_t max_len, const IP_Port *ip_port)
|
||||
{
|
||||
if (*next_packet_length == 0) {
|
||||
const uint16_t len = read_tcp_length(logger, mem, ns, sock, ip_port);
|
||||
|
18
external/toxcore/c-toxcore/toxcore/TCP_common.h
vendored
18
external/toxcore/c-toxcore/toxcore/TCP_common.h
vendored
@ -6,7 +6,9 @@
|
||||
#ifndef C_TOXCORE_TOXCORE_TCP_COMMON_H
|
||||
#define C_TOXCORE_TOXCORE_TCP_COMMON_H
|
||||
|
||||
#include "attributes.h"
|
||||
#include "crypto_core.h"
|
||||
#include "logger.h"
|
||||
#include "mem.h"
|
||||
#include "network.h"
|
||||
|
||||
@ -87,8 +89,8 @@ int send_pending_data(const Logger *logger, TCP_Connection *con);
|
||||
*/
|
||||
non_null()
|
||||
int write_packet_tcp_secure_connection(
|
||||
const Logger *logger, TCP_Connection *con, const uint8_t *data, uint16_t length,
|
||||
bool priority);
|
||||
const Logger *logger, TCP_Connection *con, const uint8_t *data, uint16_t length,
|
||||
bool priority);
|
||||
|
||||
/** @brief Read length bytes from socket.
|
||||
*
|
||||
@ -97,7 +99,7 @@ int write_packet_tcp_secure_connection(
|
||||
*/
|
||||
non_null()
|
||||
int read_tcp_packet(
|
||||
const Logger *logger, const Memory *mem, const Network *ns, Socket sock, uint8_t *data, uint16_t length, const IP_Port *ip_port);
|
||||
const Logger *logger, const Memory *mem, const Network *ns, Socket sock, uint8_t *data, uint16_t length, const IP_Port *ip_port);
|
||||
|
||||
/**
|
||||
* @return length of received packet on success.
|
||||
@ -106,9 +108,9 @@ int read_tcp_packet(
|
||||
*/
|
||||
non_null()
|
||||
int read_packet_tcp_secure_connection(
|
||||
const Logger *logger, const Memory *mem, const Network *ns,
|
||||
Socket sock, uint16_t *next_packet_length,
|
||||
const uint8_t *shared_key, uint8_t *recv_nonce, uint8_t *data,
|
||||
uint16_t max_len, const IP_Port *ip_port);
|
||||
const Logger *logger, const Memory *mem, const Network *ns,
|
||||
Socket sock, uint16_t *next_packet_length,
|
||||
const uint8_t *shared_key, uint8_t *recv_nonce, uint8_t *data,
|
||||
uint16_t max_len, const IP_Port *ip_port);
|
||||
|
||||
#endif
|
||||
#endif /* C_TOXCORE_TOXCORE_TCP_COMMON_H */
|
||||
|
@ -13,6 +13,7 @@
|
||||
|
||||
#include "DHT.h"
|
||||
#include "TCP_client.h"
|
||||
#include "attributes.h"
|
||||
#include "ccompat.h"
|
||||
#include "crypto_core.h"
|
||||
#include "forwarding.h"
|
||||
@ -57,17 +58,14 @@ struct TCP_Connections {
|
||||
uint16_t onion_num_conns;
|
||||
};
|
||||
|
||||
|
||||
static const TCP_Connection_to empty_tcp_connection_to = {0};
|
||||
static const TCP_con empty_tcp_con = {0};
|
||||
|
||||
|
||||
const uint8_t *tcp_connections_public_key(const TCP_Connections *tcp_c)
|
||||
{
|
||||
return tcp_c->self_public_key;
|
||||
}
|
||||
|
||||
|
||||
uint32_t tcp_connections_count(const TCP_Connections *tcp_c)
|
||||
{
|
||||
return tcp_c->tcp_connections_length;
|
||||
@ -119,7 +117,6 @@ static int realloc_tcp_con(const Memory *mem, TCP_con **array, size_t num)
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return true if the connections_number is valid.
|
||||
*/
|
||||
@ -752,7 +749,7 @@ int set_tcp_connection_to_status(const TCP_Connections *tcp_c, int connections_n
|
||||
}
|
||||
|
||||
if (tcp_con->status == TCP_CONN_SLEEPING) {
|
||||
tcp_con->unsleep = 1;
|
||||
tcp_con->unsleep = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -768,7 +765,7 @@ int set_tcp_connection_to_status(const TCP_Connections *tcp_c, int connections_n
|
||||
|
||||
for (uint32_t i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
|
||||
if (con_to->connections[i].tcp_connection > 0) {
|
||||
unsigned int tcp_connections_number = con_to->connections[i].tcp_connection - 1;
|
||||
const unsigned int tcp_connections_number = con_to->connections[i].tcp_connection - 1;
|
||||
TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
|
||||
|
||||
if (tcp_con == nullptr) {
|
||||
@ -927,7 +924,7 @@ static int reconnect_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connec
|
||||
return -1;
|
||||
}
|
||||
|
||||
IP_Port ip_port = tcp_con_ip_port(tcp_con->connection);
|
||||
const IP_Port ip_port = tcp_con_ip_port(tcp_con->connection);
|
||||
uint8_t relay_pk[CRYPTO_PUBLIC_KEY_SIZE];
|
||||
memcpy(relay_pk, tcp_con_public_key(tcp_con->connection), CRYPTO_PUBLIC_KEY_SIZE);
|
||||
kill_tcp_connection(tcp_con->connection);
|
||||
@ -948,14 +945,14 @@ static int reconnect_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connec
|
||||
|
||||
if (tcp_con->onion) {
|
||||
--tcp_c->onion_num_conns;
|
||||
tcp_con->onion = 0;
|
||||
tcp_con->onion = false;
|
||||
}
|
||||
|
||||
tcp_con->lock_count = 0;
|
||||
tcp_con->sleep_count = 0;
|
||||
tcp_con->connected_time = 0;
|
||||
tcp_con->status = TCP_CONN_VALID;
|
||||
tcp_con->unsleep = 0;
|
||||
tcp_con->unsleep = false;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -993,14 +990,14 @@ static int sleep_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connection
|
||||
|
||||
if (tcp_con->onion) {
|
||||
--tcp_c->onion_num_conns;
|
||||
tcp_con->onion = 0;
|
||||
tcp_con->onion = false;
|
||||
}
|
||||
|
||||
tcp_con->lock_count = 0;
|
||||
tcp_con->sleep_count = 0;
|
||||
tcp_con->connected_time = 0;
|
||||
tcp_con->status = TCP_CONN_SLEEPING;
|
||||
tcp_con->unsleep = 0;
|
||||
tcp_con->unsleep = false;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -1019,8 +1016,8 @@ static int unsleep_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connecti
|
||||
}
|
||||
|
||||
tcp_con->connection = new_tcp_connection(
|
||||
tcp_c->logger, tcp_c->mem, tcp_c->mono_time, tcp_c->rng, tcp_c->ns, &tcp_con->ip_port,
|
||||
tcp_con->relay_pk, tcp_c->self_public_key, tcp_c->self_secret_key, &tcp_c->proxy_info);
|
||||
tcp_c->logger, tcp_c->mem, tcp_c->mono_time, tcp_c->rng, tcp_c->ns, &tcp_con->ip_port,
|
||||
tcp_con->relay_pk, tcp_c->self_public_key, tcp_c->self_secret_key, &tcp_c->proxy_info);
|
||||
|
||||
if (tcp_con->connection == nullptr) {
|
||||
kill_tcp_relay_connection(tcp_c, tcp_connections_number);
|
||||
@ -1031,7 +1028,7 @@ static int unsleep_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connecti
|
||||
tcp_con->sleep_count = 0;
|
||||
tcp_con->connected_time = 0;
|
||||
tcp_con->status = TCP_CONN_VALID;
|
||||
tcp_con->unsleep = 0;
|
||||
tcp_con->unsleep = false;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -1286,7 +1283,7 @@ static int tcp_relay_on_online(TCP_Connections *tcp_c, int tcp_connections_numbe
|
||||
}
|
||||
|
||||
if (tcp_c->onion_status && tcp_c->onion_num_conns < NUM_ONION_TCP_CONNECTIONS) {
|
||||
tcp_con->onion = 1;
|
||||
tcp_con->onion = true;
|
||||
++tcp_c->onion_num_conns;
|
||||
}
|
||||
|
||||
@ -1317,8 +1314,8 @@ static int add_tcp_relay_instance(TCP_Connections *tcp_c, const IP_Port *ip_port
|
||||
TCP_con *tcp_con = &tcp_c->tcp_connections[tcp_connections_number];
|
||||
|
||||
tcp_con->connection = new_tcp_connection(
|
||||
tcp_c->logger, tcp_c->mem, tcp_c->mono_time, tcp_c->rng, tcp_c->ns, &ipp_copy,
|
||||
relay_pk, tcp_c->self_public_key, tcp_c->self_secret_key, &tcp_c->proxy_info);
|
||||
tcp_c->logger, tcp_c->mem, tcp_c->mono_time, tcp_c->rng, tcp_c->ns, &ipp_copy,
|
||||
relay_pk, tcp_c->self_public_key, tcp_c->self_secret_key, &tcp_c->proxy_info);
|
||||
|
||||
if (tcp_con->connection == nullptr) {
|
||||
return -1;
|
||||
@ -1372,7 +1369,7 @@ int add_tcp_number_relay_connection(const TCP_Connections *tcp_c, int connection
|
||||
}
|
||||
|
||||
if (con_to->status != TCP_CONN_SLEEPING && tcp_con->status == TCP_CONN_SLEEPING) {
|
||||
tcp_con->unsleep = 1;
|
||||
tcp_con->unsleep = true;
|
||||
}
|
||||
|
||||
if (add_tcp_connection_to_conn(con_to, tcp_connections_number) == -1) {
|
||||
@ -1540,7 +1537,7 @@ int set_tcp_onion_status(TCP_Connections *tcp_c, bool status)
|
||||
if (tcp_con != nullptr) {
|
||||
if (tcp_con->status == TCP_CONN_CONNECTED && !tcp_con->onion) {
|
||||
++tcp_c->onion_num_conns;
|
||||
tcp_con->onion = 1;
|
||||
tcp_con->onion = true;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1557,7 +1554,7 @@ int set_tcp_onion_status(TCP_Connections *tcp_c, bool status)
|
||||
|
||||
if (tcp_con != nullptr) {
|
||||
if (tcp_con->status == TCP_CONN_SLEEPING) {
|
||||
tcp_con->unsleep = 1;
|
||||
tcp_con->unsleep = true;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1567,7 +1564,7 @@ int set_tcp_onion_status(TCP_Connections *tcp_c, bool status)
|
||||
}
|
||||
}
|
||||
|
||||
tcp_c->onion_status = 1;
|
||||
tcp_c->onion_status = true;
|
||||
} else {
|
||||
for (uint32_t i = 0; i < tcp_c->tcp_connections_length; ++i) {
|
||||
TCP_con *tcp_con = get_tcp_connection(tcp_c, i);
|
||||
@ -1575,12 +1572,12 @@ int set_tcp_onion_status(TCP_Connections *tcp_c, bool status)
|
||||
if (tcp_con != nullptr) {
|
||||
if (tcp_con->onion) {
|
||||
--tcp_c->onion_num_conns;
|
||||
tcp_con->onion = 0;
|
||||
tcp_con->onion = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tcp_c->onion_status = 0;
|
||||
tcp_c->onion_status = false;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
@ -10,10 +10,18 @@
|
||||
#define C_TOXCORE_TOXCORE_TCP_CONNECTION_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "DHT.h" // for Node_format
|
||||
#include "TCP_client.h"
|
||||
#include "TCP_common.h"
|
||||
#include "attributes.h"
|
||||
#include "crypto_core.h"
|
||||
#include "forwarding.h"
|
||||
#include "logger.h"
|
||||
#include "mem.h"
|
||||
#include "mono_time.h"
|
||||
#include "network.h"
|
||||
|
||||
#define TCP_CONN_NONE 0
|
||||
#define TCP_CONN_VALID 1
|
||||
@ -156,7 +164,7 @@ non_null()
|
||||
int tcp_send_oob_packet(const TCP_Connections *tcp_c, unsigned int tcp_connections_number, const uint8_t *public_key,
|
||||
const uint8_t *packet, uint16_t length);
|
||||
|
||||
typedef int tcp_data_cb(void *object, int id, const uint8_t *data, uint16_t length, void *userdata);
|
||||
typedef int tcp_data_cb(void *object, int crypt_connection_id, const uint8_t *packet, uint16_t length, void *userdata);
|
||||
|
||||
non_null()
|
||||
int tcp_send_oob_packet_using_relay(const TCP_Connections *tcp_c, const uint8_t *relay_pk, const uint8_t *public_key,
|
||||
@ -178,9 +186,8 @@ void set_forwarding_packet_tcp_connection_callback(TCP_Connections *tcp_c,
|
||||
forwarded_response_cb *tcp_forwarded_response_callback,
|
||||
void *object);
|
||||
|
||||
|
||||
typedef int tcp_oob_cb(void *object, const uint8_t *public_key, unsigned int tcp_connections_number,
|
||||
const uint8_t *data, uint16_t length, void *userdata);
|
||||
const uint8_t *packet, uint16_t length, void *userdata);
|
||||
|
||||
/** @brief Set the callback for TCP oob data packets. */
|
||||
non_null()
|
||||
@ -310,4 +317,4 @@ void do_tcp_connections(const Logger *logger, TCP_Connections *tcp_c, void *user
|
||||
nullable(1)
|
||||
void kill_tcp_connections(TCP_Connections *tcp_c);
|
||||
|
||||
#endif
|
||||
#endif /* C_TOXCORE_TOXCORE_TCP_CONNECTION_H */
|
||||
|
86
external/toxcore/c-toxcore/toxcore/TCP_server.c
vendored
86
external/toxcore/c-toxcore/toxcore/TCP_server.c
vendored
@ -11,15 +11,15 @@
|
||||
#include <string.h>
|
||||
#if !defined(_WIN32) && !defined(__WIN32__) && !defined (WIN32)
|
||||
#include <sys/ioctl.h>
|
||||
#endif
|
||||
#endif /* !WIN32 */
|
||||
|
||||
#ifdef TCP_SERVER_USE_EPOLL
|
||||
#include <sys/epoll.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#endif /* TCP_SERVER_USE_EPOLL */
|
||||
|
||||
#include "DHT.h"
|
||||
#include "TCP_common.h"
|
||||
#include "attributes.h"
|
||||
#include "ccompat.h"
|
||||
#include "crypto_core.h"
|
||||
#include "forwarding.h"
|
||||
@ -35,7 +35,7 @@
|
||||
#define TCP_SOCKET_INCOMING 1
|
||||
#define TCP_SOCKET_UNCONFIRMED 2
|
||||
#define TCP_SOCKET_CONFIRMED 3
|
||||
#endif
|
||||
#endif /* TCP_SERVER_USE_EPOLL */
|
||||
|
||||
typedef struct TCP_Secure_Conn {
|
||||
uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE];
|
||||
@ -60,6 +60,7 @@ typedef struct TCP_Secure_Connection {
|
||||
uint64_t ping_id;
|
||||
} TCP_Secure_Connection;
|
||||
|
||||
static const TCP_Secure_Connection empty_tcp_secure_connection = {{nullptr}};
|
||||
|
||||
struct TCP_Server {
|
||||
const Logger *logger;
|
||||
@ -72,7 +73,7 @@ struct TCP_Server {
|
||||
#ifdef TCP_SERVER_USE_EPOLL
|
||||
int efd;
|
||||
uint64_t last_run_pinged;
|
||||
#endif
|
||||
#endif /* TCP_SERVER_USE_EPOLL */
|
||||
Socket *socks_listening;
|
||||
unsigned int num_listening_socks;
|
||||
|
||||
@ -109,8 +110,8 @@ size_t tcp_server_listen_count(const TCP_Server *tcp_server)
|
||||
#ifdef TCP_SERVER_USE_EPOLL
|
||||
#ifndef EPOLLRDHUP
|
||||
#define EPOLLRDHUP 0x2000
|
||||
#endif
|
||||
#endif
|
||||
#endif /* EPOLLRDHUP */
|
||||
#endif /* TCP_SERVER_USE_EPOLL */
|
||||
|
||||
/** @brief Increase the size of the connection list
|
||||
*
|
||||
@ -135,8 +136,9 @@ static int alloc_new_connections(TCP_Server *tcp_server, uint32_t num)
|
||||
}
|
||||
|
||||
const uint32_t old_size = tcp_server->size_accepted_connections;
|
||||
const uint32_t size_new_entries = num * sizeof(TCP_Secure_Connection);
|
||||
memset(new_connections + old_size, 0, size_new_entries);
|
||||
for (uint32_t i = 0; i < num; ++i) {
|
||||
new_connections[old_size + i] = empty_tcp_secure_connection;
|
||||
}
|
||||
|
||||
tcp_server->accepted_connection_array = new_connections;
|
||||
tcp_server->size_accepted_connections = new_size;
|
||||
@ -185,7 +187,6 @@ static int get_tcp_connection_index(const TCP_Server *tcp_server, const uint8_t
|
||||
return bs_list_find(&tcp_server->accepted_key_list, public_key);
|
||||
}
|
||||
|
||||
|
||||
non_null()
|
||||
static int kill_accepted(TCP_Server *tcp_server, int index);
|
||||
|
||||
@ -354,7 +355,7 @@ static int handle_tcp_handshake(const Logger *logger, TCP_Secure_Connection *con
|
||||
return -1;
|
||||
}
|
||||
|
||||
IP_Port ipp = {{{0}}};
|
||||
const IP_Port ipp = {{{0}}};
|
||||
|
||||
if (TCP_SERVER_HANDSHAKE_SIZE != net_send(con->con.ns, logger, con->con.sock, response, TCP_SERVER_HANDSHAKE_SIZE, &ipp)) {
|
||||
crypto_memzero(shared_key, sizeof(shared_key));
|
||||
@ -529,12 +530,13 @@ static int handle_tcp_oob_send(TCP_Server *tcp_server, uint32_t con_id, const ui
|
||||
const int other_index = get_tcp_connection_index(tcp_server, public_key);
|
||||
|
||||
if (other_index != -1) {
|
||||
VLA(uint8_t, resp_packet, 1 + CRYPTO_PUBLIC_KEY_SIZE + length);
|
||||
const uint16_t resp_packet_size = 1 + CRYPTO_PUBLIC_KEY_SIZE + length;
|
||||
VLA(uint8_t, resp_packet, resp_packet_size);
|
||||
resp_packet[0] = TCP_PACKET_OOB_RECV;
|
||||
memcpy(resp_packet + 1, con->public_key, CRYPTO_PUBLIC_KEY_SIZE);
|
||||
memcpy(resp_packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, data, length);
|
||||
write_packet_tcp_secure_connection(tcp_server->logger, &tcp_server->accepted_connection_array[other_index].con,
|
||||
resp_packet, SIZEOF_VLA(resp_packet), false);
|
||||
resp_packet, resp_packet_size, false);
|
||||
}
|
||||
|
||||
return 0;
|
||||
@ -617,11 +619,12 @@ static int handle_onion_recv_1(void *object, const IP_Port *dest, const uint8_t
|
||||
|
||||
TCP_Secure_Connection *con = &tcp_server->accepted_connection_array[index];
|
||||
|
||||
VLA(uint8_t, packet, 1 + length);
|
||||
const uint16_t packet_size = 1 + length;
|
||||
VLA(uint8_t, packet, packet_size);
|
||||
memcpy(packet + 1, data, length);
|
||||
packet[0] = TCP_PACKET_ONION_RESPONSE;
|
||||
|
||||
if (write_packet_tcp_secure_connection(tcp_server->logger, &con->con, packet, SIZEOF_VLA(packet), false) != 1) {
|
||||
if (write_packet_tcp_secure_connection(tcp_server->logger, &con->con, packet, packet_size, false) != 1) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -657,11 +660,12 @@ static bool handle_forward_reply_tcp(void *object, const uint8_t *sendback_data,
|
||||
return false;
|
||||
}
|
||||
|
||||
VLA(uint8_t, packet, 1 + length);
|
||||
const uint16_t packet_size = 1 + length;
|
||||
VLA(uint8_t, packet, packet_size);
|
||||
memcpy(packet + 1, data, length);
|
||||
packet[0] = TCP_PACKET_FORWARDING;
|
||||
|
||||
return write_packet_tcp_secure_connection(tcp_server->logger, &con->con, packet, SIZEOF_VLA(packet), false) == 1;
|
||||
return write_packet_tcp_secure_connection(tcp_server->logger, &con->con, packet, packet_size, false) == 1;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -759,7 +763,7 @@ static int handle_tcp_packet(TCP_Server *tcp_server, uint32_t con_id, const uint
|
||||
return -1;
|
||||
}
|
||||
|
||||
IP_Port source = con_id_to_ip_port(con_id, con->identifier);
|
||||
const IP_Port source = con_id_to_ip_port(con_id, con->identifier);
|
||||
onion_send_1(tcp_server->onion, data + 1 + CRYPTO_NONCE_SIZE, length - (1 + CRYPTO_NONCE_SIZE), &source,
|
||||
data + 1);
|
||||
}
|
||||
@ -844,7 +848,6 @@ static int handle_tcp_packet(TCP_Server *tcp_server, uint32_t con_id, const uint
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
non_null()
|
||||
static int confirm_tcp_connection(TCP_Server *tcp_server, const Mono_Time *mono_time, TCP_Secure_Connection *con,
|
||||
const uint8_t *data, uint16_t length)
|
||||
@ -991,7 +994,7 @@ TCP_Server *new_tcp_server(const Logger *logger, const Memory *mem, const Random
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif /* TCP_SERVER_USE_EPOLL */
|
||||
|
||||
const Family family = ipv6_enabled ? net_family_ipv6() : net_family_ipv4();
|
||||
|
||||
@ -1006,13 +1009,13 @@ TCP_Server *new_tcp_server(const Logger *logger, const Memory *mem, const Random
|
||||
struct epoll_event ev;
|
||||
|
||||
ev.events = EPOLLIN | EPOLLET;
|
||||
ev.data.u64 = sock.sock | ((uint64_t)TCP_SOCKET_LISTENING << 32);
|
||||
ev.data.u64 = net_socket_to_native(sock) | ((uint64_t)TCP_SOCKET_LISTENING << 32);
|
||||
|
||||
if (epoll_ctl(temp->efd, EPOLL_CTL_ADD, sock.sock, &ev) == -1) {
|
||||
if (epoll_ctl(temp->efd, EPOLL_CTL_ADD, net_socket_to_native(sock), &ev) == -1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif /* TCP_SERVER_USE_EPOLL */
|
||||
|
||||
temp->socks_listening[temp->num_listening_socks] = sock;
|
||||
++temp->num_listening_socks;
|
||||
@ -1037,7 +1040,7 @@ TCP_Server *new_tcp_server(const Logger *logger, const Memory *mem, const Random
|
||||
memcpy(temp->secret_key, secret_key, CRYPTO_SECRET_KEY_SIZE);
|
||||
crypto_derive_public_key(temp->public_key, temp->secret_key);
|
||||
|
||||
bs_list_init(&temp->accepted_key_list, CRYPTO_PUBLIC_KEY_SIZE, 8);
|
||||
bs_list_init(&temp->accepted_key_list, CRYPTO_PUBLIC_KEY_SIZE, 8, memcmp);
|
||||
|
||||
return temp;
|
||||
}
|
||||
@ -1057,7 +1060,7 @@ static void do_tcp_accept_new(TCP_Server *tcp_server)
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endif /* TCP_SERVER_USE_EPOLL */
|
||||
|
||||
non_null()
|
||||
static int do_incoming(TCP_Server *tcp_server, uint32_t i)
|
||||
@ -1109,7 +1112,8 @@ static int do_unconfirmed(TCP_Server *tcp_server, const Mono_Time *mono_time, ui
|
||||
LOGGER_TRACE(tcp_server->logger, "handling unconfirmed TCP connection %d", i);
|
||||
|
||||
uint8_t packet[MAX_PACKET_SIZE];
|
||||
const int len = read_packet_tcp_secure_connection(tcp_server->logger, conn->con.mem, conn->con.ns, conn->con.sock, &conn->next_packet_length, conn->con.shared_key, conn->recv_nonce, packet, sizeof(packet), &conn->con.ip_port);
|
||||
const int len = read_packet_tcp_secure_connection(tcp_server->logger, conn->con.mem, conn->con.ns, conn->con.sock, &conn->next_packet_length, conn->con.shared_key, conn->recv_nonce, packet,
|
||||
sizeof(packet), &conn->con.ip_port);
|
||||
|
||||
if (len == 0) {
|
||||
return -1;
|
||||
@ -1129,7 +1133,8 @@ static bool tcp_process_secure_packet(TCP_Server *tcp_server, uint32_t i)
|
||||
TCP_Secure_Connection *const conn = &tcp_server->accepted_connection_array[i];
|
||||
|
||||
uint8_t packet[MAX_PACKET_SIZE];
|
||||
const int len = read_packet_tcp_secure_connection(tcp_server->logger, conn->con.mem, conn->con.ns, conn->con.sock, &conn->next_packet_length, conn->con.shared_key, conn->recv_nonce, packet, sizeof(packet), &conn->con.ip_port);
|
||||
const int len = read_packet_tcp_secure_connection(tcp_server->logger, conn->con.mem, conn->con.ns, conn->con.sock, &conn->next_packet_length, conn->con.shared_key, conn->recv_nonce, packet,
|
||||
sizeof(packet), &conn->con.ip_port);
|
||||
LOGGER_TRACE(tcp_server->logger, "processing packet for %d: %d", i, len);
|
||||
|
||||
if (len == 0) {
|
||||
@ -1174,7 +1179,7 @@ static void do_tcp_unconfirmed(TCP_Server *tcp_server, const Mono_Time *mono_tim
|
||||
do_unconfirmed(tcp_server, mono_time, i);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endif /* TCP_SERVER_USE_EPOLL */
|
||||
|
||||
non_null()
|
||||
static void do_tcp_confirmed(TCP_Server *tcp_server, const Mono_Time *mono_time)
|
||||
@ -1186,7 +1191,7 @@ static void do_tcp_confirmed(TCP_Server *tcp_server, const Mono_Time *mono_time)
|
||||
}
|
||||
|
||||
tcp_server->last_run_pinged = mono_time_get(mono_time);
|
||||
#endif
|
||||
#endif /* TCP_SERVER_USE_EPOLL */
|
||||
|
||||
for (uint32_t i = 0; i < tcp_server->size_accepted_connections; ++i) {
|
||||
TCP_Secure_Connection *conn = &tcp_server->accepted_connection_array[i];
|
||||
@ -1229,7 +1234,7 @@ static void do_tcp_confirmed(TCP_Server *tcp_server, const Mono_Time *mono_time)
|
||||
|
||||
do_confirmed_recv(tcp_server, i);
|
||||
|
||||
#endif
|
||||
#endif /* TCP_SERVER_USE_EPOLL */
|
||||
}
|
||||
}
|
||||
|
||||
@ -1243,7 +1248,7 @@ static bool tcp_epoll_process(TCP_Server *tcp_server, const Mono_Time *mono_time
|
||||
#undef MAX_EVENTS
|
||||
|
||||
for (int n = 0; n < nfds; ++n) {
|
||||
const Socket sock = {(int)(events[n].data.u64 & 0xFFFFFFFF)};
|
||||
const Socket sock = net_socket_from_native((int)(events[n].data.u64 & 0xFFFFFFFF));
|
||||
const int status = (events[n].data.u64 >> 32) & 0xFF;
|
||||
const int index = events[n].data.u64 >> 40;
|
||||
|
||||
@ -1277,7 +1282,6 @@ static bool tcp_epoll_process(TCP_Server *tcp_server, const Mono_Time *mono_time
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
if ((events[n].events & EPOLLIN) == 0) {
|
||||
continue;
|
||||
}
|
||||
@ -1302,9 +1306,9 @@ static bool tcp_epoll_process(TCP_Server *tcp_server, const Mono_Time *mono_time
|
||||
|
||||
ev.events = EPOLLIN | EPOLLET | EPOLLRDHUP;
|
||||
|
||||
ev.data.u64 = sock_new.sock | ((uint64_t)TCP_SOCKET_INCOMING << 32) | ((uint64_t)index_new << 40);
|
||||
ev.data.u64 = net_socket_to_native(sock_new) | ((uint64_t)TCP_SOCKET_INCOMING << 32) | ((uint64_t)index_new << 40);
|
||||
|
||||
if (epoll_ctl(tcp_server->efd, EPOLL_CTL_ADD, sock_new.sock, &ev) == -1) {
|
||||
if (epoll_ctl(tcp_server->efd, EPOLL_CTL_ADD, net_socket_to_native(sock_new), &ev) == -1) {
|
||||
LOGGER_DEBUG(tcp_server->logger, "new connection %d was dropped due to epoll error %d", index, net_error());
|
||||
kill_tcp_secure_connection(&tcp_server->incoming_connection_queue[index_new]);
|
||||
continue;
|
||||
@ -1320,9 +1324,9 @@ static bool tcp_epoll_process(TCP_Server *tcp_server, const Mono_Time *mono_time
|
||||
if (index_new != -1) {
|
||||
LOGGER_TRACE(tcp_server->logger, "incoming connection %d was accepted as %d", index, index_new);
|
||||
events[n].events = EPOLLIN | EPOLLET | EPOLLRDHUP;
|
||||
events[n].data.u64 = sock.sock | ((uint64_t)TCP_SOCKET_UNCONFIRMED << 32) | ((uint64_t)index_new << 40);
|
||||
events[n].data.u64 = net_socket_to_native(sock) | ((uint64_t)TCP_SOCKET_UNCONFIRMED << 32) | ((uint64_t)index_new << 40);
|
||||
|
||||
if (epoll_ctl(tcp_server->efd, EPOLL_CTL_MOD, sock.sock, &events[n]) == -1) {
|
||||
if (epoll_ctl(tcp_server->efd, EPOLL_CTL_MOD, net_socket_to_native(sock), &events[n]) == -1) {
|
||||
LOGGER_DEBUG(tcp_server->logger, "incoming connection %d was dropped due to epoll error %d", index, net_error());
|
||||
kill_tcp_secure_connection(&tcp_server->unconfirmed_connection_queue[index_new]);
|
||||
break;
|
||||
@ -1338,9 +1342,9 @@ static bool tcp_epoll_process(TCP_Server *tcp_server, const Mono_Time *mono_time
|
||||
if (index_new != -1) {
|
||||
LOGGER_TRACE(tcp_server->logger, "unconfirmed connection %d was confirmed as %d", index, index_new);
|
||||
events[n].events = EPOLLIN | EPOLLET | EPOLLRDHUP;
|
||||
events[n].data.u64 = sock.sock | ((uint64_t)TCP_SOCKET_CONFIRMED << 32) | ((uint64_t)index_new << 40);
|
||||
events[n].data.u64 = net_socket_to_native(sock) | ((uint64_t)TCP_SOCKET_CONFIRMED << 32) | ((uint64_t)index_new << 40);
|
||||
|
||||
if (epoll_ctl(tcp_server->efd, EPOLL_CTL_MOD, sock.sock, &events[n]) == -1) {
|
||||
if (epoll_ctl(tcp_server->efd, EPOLL_CTL_MOD, net_socket_to_native(sock), &events[n]) == -1) {
|
||||
// remove from confirmed connections
|
||||
LOGGER_DEBUG(tcp_server->logger, "unconfirmed connection %d was dropped due to epoll error %d", index, net_error());
|
||||
kill_accepted(tcp_server, index_new);
|
||||
@ -1369,7 +1373,7 @@ static void do_tcp_epoll(TCP_Server *tcp_server, const Mono_Time *mono_time)
|
||||
continue;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endif /* TCP_SERVER_USE_EPOLL */
|
||||
|
||||
void do_tcp_server(TCP_Server *tcp_server, const Mono_Time *mono_time)
|
||||
{
|
||||
@ -1380,7 +1384,7 @@ void do_tcp_server(TCP_Server *tcp_server, const Mono_Time *mono_time)
|
||||
do_tcp_accept_new(tcp_server);
|
||||
do_tcp_incoming(tcp_server);
|
||||
do_tcp_unconfirmed(tcp_server, mono_time);
|
||||
#endif
|
||||
#endif /* TCP_SERVER_USE_EPOLL */
|
||||
|
||||
do_tcp_confirmed(tcp_server, mono_time);
|
||||
}
|
||||
@ -1407,7 +1411,7 @@ void kill_tcp_server(TCP_Server *tcp_server)
|
||||
|
||||
#ifdef TCP_SERVER_USE_EPOLL
|
||||
close(tcp_server->efd);
|
||||
#endif
|
||||
#endif /* TCP_SERVER_USE_EPOLL */
|
||||
|
||||
for (uint32_t i = 0; i < MAX_INCOMING_CONNECTIONS; ++i) {
|
||||
wipe_secure_connection(&tcp_server->incoming_connection_queue[i]);
|
||||
|
@ -9,8 +9,13 @@
|
||||
#ifndef C_TOXCORE_TOXCORE_TCP_SERVER_H
|
||||
#define C_TOXCORE_TOXCORE_TCP_SERVER_H
|
||||
|
||||
#include "attributes.h"
|
||||
#include "crypto_core.h"
|
||||
#include "forwarding.h"
|
||||
#include "logger.h"
|
||||
#include "mem.h"
|
||||
#include "mono_time.h"
|
||||
#include "network.h"
|
||||
#include "onion.h"
|
||||
|
||||
#define MAX_INCOMING_CONNECTIONS 256
|
||||
@ -47,5 +52,4 @@ void do_tcp_server(TCP_Server *tcp_server, const Mono_Time *mono_time);
|
||||
nullable(1)
|
||||
void kill_tcp_server(TCP_Server *tcp_server);
|
||||
|
||||
|
||||
#endif
|
||||
#endif /* C_TOXCORE_TOXCORE_TCP_SERVER_H */
|
||||
|
35
external/toxcore/c-toxcore/toxcore/announce.c
vendored
35
external/toxcore/c-toxcore/toxcore/announce.c
vendored
@ -14,6 +14,7 @@
|
||||
|
||||
#include "DHT.h"
|
||||
#include "LAN_discovery.h"
|
||||
#include "attributes.h"
|
||||
#include "ccompat.h"
|
||||
#include "crypto_core.h"
|
||||
#include "forwarding.h"
|
||||
@ -164,7 +165,6 @@ static const Announce_Entry *get_stored_const(const Announcements *announce, con
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
bool announce_on_stored(const Announcements *announce, const uint8_t *data_public_key,
|
||||
announce_on_retrieve_cb *on_retrieve_callback, void *object)
|
||||
{
|
||||
@ -239,9 +239,7 @@ bool announce_store_data(Announcements *announce, const uint8_t *data_public_key
|
||||
if (length > 0) {
|
||||
assert(data != nullptr);
|
||||
|
||||
if (entry->data != nullptr) {
|
||||
free(entry->data);
|
||||
}
|
||||
free(entry->data);
|
||||
|
||||
uint8_t *entry_data = (uint8_t *)malloc(length);
|
||||
|
||||
@ -354,7 +352,7 @@ static int create_reply_plain_data_search_request(Announcements *announce,
|
||||
to_auth, to_auth_length, p);
|
||||
p += TIMED_AUTH_SIZE;
|
||||
|
||||
*p = would_accept_store_request(announce, data_public_key);
|
||||
*p = would_accept_store_request(announce, data_public_key) ? 1 : 0;
|
||||
++p;
|
||||
|
||||
Node_format nodes_list[MAX_SENT_NODES];
|
||||
@ -387,11 +385,11 @@ static int create_reply_plain_data_search_request(Announcements *announce,
|
||||
|
||||
non_null()
|
||||
static int create_reply_plain_data_retrieve_request(
|
||||
const Announcements *announce,
|
||||
const IP_Port *source,
|
||||
const uint8_t *data, uint16_t length,
|
||||
uint8_t *reply, uint16_t reply_max_length,
|
||||
const uint8_t *to_auth, uint16_t to_auth_length)
|
||||
const Announcements *announce,
|
||||
const IP_Port *source,
|
||||
const uint8_t *data, uint16_t length,
|
||||
uint8_t *reply, uint16_t reply_max_length,
|
||||
const uint8_t *to_auth, uint16_t to_auth_length)
|
||||
{
|
||||
if (length != CRYPTO_PUBLIC_KEY_SIZE + 1 + TIMED_AUTH_SIZE) {
|
||||
return -1;
|
||||
@ -446,7 +444,7 @@ static int create_reply_plain_store_announce_request(Announcements *announce,
|
||||
|
||||
VLA(uint8_t, plain, plain_len);
|
||||
|
||||
const uint8_t* shared_key = shared_key_cache_lookup(announce->shared_keys, data_public_key);
|
||||
const uint8_t *shared_key = shared_key_cache_lookup(announce->shared_keys, data_public_key);
|
||||
|
||||
if (shared_key == nullptr) {
|
||||
/* Error looking up/deriving the shared key */
|
||||
@ -629,16 +627,15 @@ static void forwarded_request_callback(void *object, const IP_Port *forwarder,
|
||||
}
|
||||
|
||||
non_null(1, 2, 3) nullable(5)
|
||||
static int handle_dht_announce_request(void *object, const IP_Port *source,
|
||||
const uint8_t *data, uint16_t length, void *userdata)
|
||||
static int handle_dht_announce_request(
|
||||
void *object, const IP_Port *source, const uint8_t *packet, uint16_t length, void *userdata)
|
||||
{
|
||||
Announcements *announce = (Announcements *) object;
|
||||
Announcements *announce = (Announcements *)object;
|
||||
|
||||
uint8_t reply[MAX_FORWARD_DATA_SIZE];
|
||||
|
||||
const int len = create_reply(announce, source,
|
||||
nullptr, 0,
|
||||
data, length, reply, sizeof(reply));
|
||||
const int len
|
||||
= create_reply(announce, source, nullptr, 0, packet, length, reply, sizeof(reply));
|
||||
|
||||
if (len == -1) {
|
||||
return -1;
|
||||
@ -703,9 +700,7 @@ void kill_announcements(Announcements *announce)
|
||||
shared_key_cache_free(announce->shared_keys);
|
||||
|
||||
for (uint32_t i = 0; i < ANNOUNCE_BUCKETS * ANNOUNCE_BUCKET_SIZE; ++i) {
|
||||
if (announce->entries[i].data != nullptr) {
|
||||
free(announce->entries[i].data);
|
||||
}
|
||||
free(announce->entries[i].data);
|
||||
}
|
||||
|
||||
free(announce);
|
||||
|
10
external/toxcore/c-toxcore/toxcore/announce.h
vendored
10
external/toxcore/c-toxcore/toxcore/announce.h
vendored
@ -5,7 +5,14 @@
|
||||
#ifndef C_TOXCORE_TOXCORE_ANNOUNCE_H
|
||||
#define C_TOXCORE_TOXCORE_ANNOUNCE_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "attributes.h"
|
||||
#include "crypto_core.h"
|
||||
#include "forwarding.h"
|
||||
#include "logger.h"
|
||||
#include "mem.h"
|
||||
#include "mono_time.h"
|
||||
|
||||
#define MAX_ANNOUNCEMENT_SIZE 512
|
||||
|
||||
@ -34,7 +41,6 @@ void announce_set_synch_offset(Announcements *announce, int32_t synch_offset);
|
||||
nullable(1)
|
||||
void kill_announcements(Announcements *announce);
|
||||
|
||||
|
||||
/* The declarations below are not public, they are exposed only for tests. */
|
||||
|
||||
/** @private
|
||||
@ -65,4 +71,4 @@ bool announce_store_data(Announcements *announce, const uint8_t *data_public_key
|
||||
#define ANNOUNCE_BUCKET_PREFIX_LENGTH 5
|
||||
#define ANNOUNCE_BUCKETS 32 // ANNOUNCE_BUCKETS = 2 ** ANNOUNCE_BUCKET_PREFIX_LENGTH
|
||||
|
||||
#endif
|
||||
#endif /* C_TOXCORE_TOXCORE_ANNOUNCE_H */
|
||||
|
10
external/toxcore/c-toxcore/toxcore/attributes.h
vendored
10
external/toxcore/c-toxcore/toxcore/attributes.h
vendored
@ -26,6 +26,14 @@
|
||||
|
||||
#define nullable(...)
|
||||
|
||||
#ifdef SPARSE
|
||||
#define bitwise __attribute__((bitwise))
|
||||
#define force __attribute__((force))
|
||||
#else
|
||||
#define bitwise
|
||||
#define force
|
||||
#endif
|
||||
|
||||
//!TOKSTYLE+
|
||||
|
||||
#endif // C_TOXCORE_TOXCORE_ATTRIBUTES_H
|
||||
#endif /* C_TOXCORE_TOXCORE_ATTRIBUTES_H */
|
||||
|
61
external/toxcore/c-toxcore/toxcore/bin_pack.c
vendored
61
external/toxcore/c-toxcore/toxcore/bin_pack.c
vendored
@ -5,10 +5,10 @@
|
||||
#include "bin_pack.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../third_party/cmp/cmp.h"
|
||||
#include "attributes.h"
|
||||
#include "ccompat.h"
|
||||
#include "logger.h"
|
||||
|
||||
@ -27,15 +27,16 @@ static bool null_reader(cmp_ctx_t *ctx, void *data, size_t limit)
|
||||
}
|
||||
|
||||
non_null()
|
||||
static bool null_skipper(cmp_ctx_t *ctx, size_t limit)
|
||||
static bool null_skipper(cmp_ctx_t *ctx, size_t count)
|
||||
{
|
||||
assert(limit == 0);
|
||||
assert(count == 0);
|
||||
return false;
|
||||
}
|
||||
|
||||
non_null()
|
||||
static size_t buf_writer(cmp_ctx_t *ctx, const void *data, size_t count)
|
||||
{
|
||||
const uint8_t *bytes = (const uint8_t *)data;
|
||||
Bin_Pack *bp = (Bin_Pack *)ctx->buf;
|
||||
assert(bp != nullptr);
|
||||
const uint32_t new_pos = bp->bytes_pos + count;
|
||||
@ -48,7 +49,7 @@ static size_t buf_writer(cmp_ctx_t *ctx, const void *data, size_t count)
|
||||
// Buffer too small.
|
||||
return 0;
|
||||
}
|
||||
memcpy(bp->bytes + bp->bytes_pos, data, count);
|
||||
memcpy(&bp->bytes[bp->bytes_pos], bytes, count);
|
||||
}
|
||||
bp->bytes_pos += count;
|
||||
return count;
|
||||
@ -63,60 +64,71 @@ static void bin_pack_init(Bin_Pack *bp, uint8_t *buf, uint32_t buf_size)
|
||||
cmp_init(&bp->ctx, bp, null_reader, null_skipper, buf_writer);
|
||||
}
|
||||
|
||||
uint32_t bin_pack_obj_size(bin_pack_cb *callback, const Logger *logger, const void *obj)
|
||||
uint32_t bin_pack_obj_size(bin_pack_cb *callback, const void *obj, const Logger *logger)
|
||||
{
|
||||
Bin_Pack bp;
|
||||
bin_pack_init(&bp, nullptr, 0);
|
||||
if (!callback(&bp, logger, obj)) {
|
||||
if (!callback(obj, logger, &bp)) {
|
||||
return UINT32_MAX;
|
||||
}
|
||||
return bp.bytes_pos;
|
||||
}
|
||||
|
||||
bool bin_pack_obj(bin_pack_cb *callback, const Logger *logger, const void *obj, uint8_t *buf, uint32_t buf_size)
|
||||
bool bin_pack_obj(bin_pack_cb *callback, const void *obj, const Logger *logger, uint8_t *buf, uint32_t buf_size)
|
||||
{
|
||||
Bin_Pack bp;
|
||||
bin_pack_init(&bp, buf, buf_size);
|
||||
return callback(&bp, logger, obj);
|
||||
return callback(obj, logger, &bp);
|
||||
}
|
||||
|
||||
uint32_t bin_pack_obj_array_size(bin_pack_array_cb *callback, const Logger *logger, const void *arr, uint32_t count)
|
||||
uint32_t bin_pack_obj_array_b_size(bin_pack_array_cb *callback, const void *arr, uint32_t arr_size, const Logger *logger)
|
||||
{
|
||||
Bin_Pack bp;
|
||||
bin_pack_init(&bp, nullptr, 0);
|
||||
for (uint32_t i = 0; i < count; ++i) {
|
||||
if (!callback(&bp, logger, arr, i)) {
|
||||
if (arr == nullptr) {
|
||||
assert(arr_size == 0);
|
||||
}
|
||||
for (uint32_t i = 0; i < arr_size; ++i) {
|
||||
if (!callback(arr, i, logger, &bp)) {
|
||||
return UINT32_MAX;
|
||||
}
|
||||
}
|
||||
return bp.bytes_pos;
|
||||
}
|
||||
|
||||
bool bin_pack_obj_array(bin_pack_array_cb *callback, const Logger *logger, const void *arr, uint32_t count, uint8_t *buf, uint32_t buf_size)
|
||||
bool bin_pack_obj_array_b(bin_pack_array_cb *callback, const void *arr, uint32_t arr_size, const Logger *logger, uint8_t *buf, uint32_t buf_size)
|
||||
{
|
||||
Bin_Pack bp;
|
||||
bin_pack_init(&bp, buf, buf_size);
|
||||
for (uint32_t i = 0; i < count; ++i) {
|
||||
if (!callback(&bp, logger, arr, i)) {
|
||||
if (arr == nullptr) {
|
||||
assert(arr_size == 0);
|
||||
}
|
||||
for (uint32_t i = 0; i < arr_size; ++i) {
|
||||
if (!callback(arr, i, logger, &bp)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Bin_Pack *bin_pack_new(uint8_t *buf, uint32_t buf_size)
|
||||
bool bin_pack_obj_array(Bin_Pack *bp, bin_pack_array_cb *callback, const void *arr, uint32_t arr_size, const Logger *logger)
|
||||
{
|
||||
Bin_Pack *bp = (Bin_Pack *)calloc(1, sizeof(Bin_Pack));
|
||||
if (bp == nullptr) {
|
||||
return nullptr;
|
||||
if (arr == nullptr) {
|
||||
assert(arr_size == 0);
|
||||
return bin_pack_array(bp, 0);
|
||||
}
|
||||
bin_pack_init(bp, buf, buf_size);
|
||||
return bp;
|
||||
}
|
||||
|
||||
void bin_pack_free(Bin_Pack *bp)
|
||||
{
|
||||
free(bp);
|
||||
if (!bin_pack_array(bp, arr_size)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < arr_size; ++i) {
|
||||
if (!callback(arr, i, logger, bp)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool bin_pack_array(Bin_Pack *bp, uint32_t size)
|
||||
@ -191,4 +203,3 @@ bool bin_pack_bin_b(Bin_Pack *bp, const uint8_t *data, uint32_t length)
|
||||
{
|
||||
return bp->ctx.write(&bp->ctx, data, length) == length;
|
||||
}
|
||||
|
||||
|
107
external/toxcore/c-toxcore/toxcore/bin_pack.h
vendored
107
external/toxcore/c-toxcore/toxcore/bin_pack.h
vendored
@ -16,6 +16,26 @@ extern "C" {
|
||||
|
||||
/**
|
||||
* @brief Binary serialisation object.
|
||||
*
|
||||
* Naming convention:
|
||||
* - Functions ending in `_b` (or `_b_size`) are NOT MessagePack, i.e. write
|
||||
* data in plain big endian binary format.
|
||||
* - All other functions encode their input in MessagePack format.
|
||||
*
|
||||
* Some notes on parameter order:
|
||||
*
|
||||
* - We pass the `obj` pointer as `this`-like pointer first to the callbacks.
|
||||
* - Any extra arguments passed to the callback follow the `obj` (and in case of
|
||||
* array packing, the `arr` and `arr_size` parameters).
|
||||
* - The packer is passed last.
|
||||
*
|
||||
* This roughly matches a curried lambda function:
|
||||
*
|
||||
* @code
|
||||
* bin_pack_obj([](const void *obj, const Logger *logger, Bin_Pack *bp) { ... }, obj, logger, buf, buf_size);
|
||||
* // Translates roughly to:
|
||||
* bin_pack_obj([obj, logger](Bin_Pack *bp) { ... }, buf, buf_size);
|
||||
* @endcode
|
||||
*/
|
||||
typedef struct Bin_Pack Bin_Pack;
|
||||
|
||||
@ -24,7 +44,7 @@ typedef struct Bin_Pack Bin_Pack;
|
||||
* This function would typically cast the `void *` to the actual object pointer type and then call
|
||||
* more appropriately typed packing functions.
|
||||
*/
|
||||
typedef bool bin_pack_cb(Bin_Pack *bp, const Logger *logger, const void *obj);
|
||||
typedef bool bin_pack_cb(const void *obj, const Logger *logger, Bin_Pack *bp);
|
||||
|
||||
/** @brief Function used to pack an array of objects.
|
||||
*
|
||||
@ -34,99 +54,104 @@ typedef bool bin_pack_cb(Bin_Pack *bp, const Logger *logger, const void *obj);
|
||||
* @param arr is the object array as void pointer.
|
||||
* @param index is the index in the object array that is currently being packed.
|
||||
*/
|
||||
typedef bool bin_pack_array_cb(Bin_Pack *bp, const Logger *logger, const void *arr, uint32_t index);
|
||||
typedef bool bin_pack_array_cb(const void *arr, uint32_t index, const Logger *logger, Bin_Pack *bp);
|
||||
|
||||
/** @brief Determine the serialised size of an object.
|
||||
*
|
||||
* @param callback The function called on the created packer and packed object.
|
||||
* @param logger Optional logger object to pass to the callback.
|
||||
* @param obj The object to be packed, passed as `obj` to the callback.
|
||||
* @param logger Optional logger object to pass to the callback.
|
||||
*
|
||||
* @return The packed size of the passed object according to the callback.
|
||||
* @retval UINT32_MAX in case of errors such as buffer overflow.
|
||||
*/
|
||||
non_null(1) nullable(2, 3)
|
||||
uint32_t bin_pack_obj_size(bin_pack_cb *callback, const Logger *logger, const void *obj);
|
||||
uint32_t bin_pack_obj_size(bin_pack_cb *callback, const void *obj, const Logger *logger);
|
||||
|
||||
/** @brief Pack an object into a buffer of a given size.
|
||||
*
|
||||
* This function creates and initialises a `Bin_Pack` packer object, calls the callback with the
|
||||
* packer object and the to-be-packed object, and then cleans up the packer object.
|
||||
* packer object and the to-be-packed object, and then cleans up the packer object. Note that
|
||||
* there is nothing MessagePack-specific about this function, so it can be used for both custom
|
||||
* binary and MessagePack formats.
|
||||
*
|
||||
* You can use `bin_pack_obj_size` to determine the minimum required size of `buf`. If packing
|
||||
* overflows `uint32_t`, this function returns `false`.
|
||||
*
|
||||
* Passing NULL for `obj` is supported, but requires that the callback supports nullable inputs.
|
||||
*
|
||||
* @param callback The function called on the created packer and packed object.
|
||||
* @param logger Optional logger object to pass to the callback.
|
||||
* @param obj The object to be packed, passed as `obj` to the callback.
|
||||
* @param logger Optional logger object to pass to the callback.
|
||||
* @param buf A byte array large enough to hold the serialised representation of `obj`.
|
||||
* @param buf_size The size of the byte array. Can be `UINT32_MAX` to disable bounds checking.
|
||||
*
|
||||
* @retval false if an error occurred (e.g. buffer overflow).
|
||||
*/
|
||||
non_null(1, 4) nullable(2, 3)
|
||||
bool bin_pack_obj(bin_pack_cb *callback, const Logger *logger, const void *obj, uint8_t *buf, uint32_t buf_size);
|
||||
bool bin_pack_obj(bin_pack_cb *callback, const void *obj, const Logger *logger, uint8_t *buf, uint32_t buf_size);
|
||||
|
||||
/** @brief Determine the serialised size of an object array.
|
||||
*
|
||||
* Calls the callback `count` times with increasing `index` argument from 0 to
|
||||
* `count`. This function is here just so we don't need to write the same
|
||||
* trivial loop many times and so we don't need an extra struct just to contain
|
||||
* an array with size so it can be passed to `bin_pack_obj_size`.
|
||||
* Behaves exactly like `bin_pack_obj_b_array` but doesn't write.
|
||||
*
|
||||
* @param callback The function called on the created packer and each object to
|
||||
* be packed.
|
||||
* @param logger Optional logger object to pass to the callback.
|
||||
* @param arr The object array to be packed, passed as `arr` to the callback.
|
||||
* @param count The number of elements in the object array.
|
||||
* @param arr_size The number of elements in the object array.
|
||||
* @param logger Optional logger object to pass to the callback.
|
||||
*
|
||||
* @return The packed size of the passed object array according to the callback.
|
||||
* @retval UINT32_MAX in case of errors such as buffer overflow.
|
||||
*/
|
||||
non_null(1, 3) nullable(2)
|
||||
uint32_t bin_pack_obj_array_size(bin_pack_array_cb *callback, const Logger *logger, const void *arr, uint32_t count);
|
||||
non_null(1) nullable(2, 4)
|
||||
uint32_t bin_pack_obj_array_b_size(bin_pack_array_cb *callback, const void *arr, uint32_t arr_size, const Logger *logger);
|
||||
|
||||
/** @brief Pack an object array into a buffer of a given size.
|
||||
*
|
||||
* Calls the callback `count` times with increasing `index` argument from 0 to
|
||||
* `count`. This function is here just so we don't need to write the same
|
||||
* trivial loop many times and so we don't need an extra struct just to contain
|
||||
* an array with size so it can be passed to `bin_pack_obj`.
|
||||
* Similar to `bin_pack_obj_array` but does not write the array length, so
|
||||
* if you need that, encoding it is on you.
|
||||
*
|
||||
* Similar to `bin_pack_obj` but for arrays. Does not write the array length, so
|
||||
* if you need that, write it manually using `bin_pack_array`.
|
||||
* Passing NULL for `arr` has no effect, but requires that `arr_size` is 0.
|
||||
*
|
||||
* @param callback The function called on the created packer and packed object
|
||||
* array.
|
||||
* @param logger Optional logger object to pass to the callback.
|
||||
* @param arr The object array to be packed, passed as `arr` to the callback.
|
||||
* @param count The number of elements in the object array.
|
||||
* @param arr_size The number of elements in the object array.
|
||||
* @param logger Optional logger object to pass to the callback.
|
||||
* @param buf A byte array large enough to hold the serialised representation of `arr`.
|
||||
* @param buf_size The size of the byte array. Can be `UINT32_MAX` to disable bounds checking.
|
||||
*
|
||||
* @retval false if an error occurred (e.g. buffer overflow).
|
||||
*/
|
||||
non_null(1, 3, 5) nullable(2)
|
||||
bool bin_pack_obj_array(bin_pack_array_cb *callback, const Logger *logger, const void *arr, uint32_t count, uint8_t *buf, uint32_t buf_size);
|
||||
non_null(1, 5) nullable(2, 4)
|
||||
bool bin_pack_obj_array_b(bin_pack_array_cb *callback, const void *arr, uint32_t arr_size, const Logger *logger, uint8_t *buf, uint32_t buf_size);
|
||||
|
||||
/** @brief Allocate a new packer object.
|
||||
/** @brief Encode an object array as MessagePack array into a bin packer.
|
||||
*
|
||||
* This is the only function that allocates memory in this module.
|
||||
* Calls the callback `arr_size` times with increasing `index` argument from 0 to
|
||||
* `arr_size`. This function is here just so we don't need to write the same
|
||||
* trivial loop many times and so we don't need an extra struct just to contain
|
||||
* an array with size so it can be passed to `bin_pack_obj`.
|
||||
*
|
||||
* @param buf A byte array large enough to hold the serialised representation of `obj`.
|
||||
* @param buf_size The size of the byte array. Can be `UINT32_MAX` to disable bounds checking.
|
||||
* Similar to `bin_pack_obj` but for arrays. Note that a `Bin_Pack` object is
|
||||
* required here, so it must be called from within a callback to one of the
|
||||
* functions above.
|
||||
*
|
||||
* @retval nullptr on allocation failure.
|
||||
* Passing NULL for `arr` requires that `arr_size` is 0. This will write a 0-size
|
||||
* MessagePack array to the packer.
|
||||
*
|
||||
* @param bp Bin packer object.
|
||||
* @param callback The function called on the created packer and packed object
|
||||
* array.
|
||||
* @param arr The object array to be packed, passed as `arr` to the callback.
|
||||
* @param arr_size The number of elements in the object array.
|
||||
* @param logger Optional logger object to pass to the callback.
|
||||
*
|
||||
* @retval false if an error occurred (e.g. buffer overflow).
|
||||
*/
|
||||
non_null()
|
||||
Bin_Pack *bin_pack_new(uint8_t *buf, uint32_t buf_size);
|
||||
|
||||
/** @brief Deallocates a packer object.
|
||||
*
|
||||
* Does not deallocate the buffer inside.
|
||||
*/
|
||||
nullable(1)
|
||||
void bin_pack_free(Bin_Pack *bp);
|
||||
non_null(1, 2) nullable(3, 5)
|
||||
bool bin_pack_obj_array(Bin_Pack *bp, bin_pack_array_cb *callback, const void *arr, uint32_t arr_size, const Logger *logger);
|
||||
|
||||
/** @brief Start packing a MessagePack array.
|
||||
*
|
||||
@ -172,7 +197,7 @@ non_null() bool bin_pack_u64_b(Bin_Pack *bp, uint64_t val);
|
||||
non_null() bool bin_pack_bin_b(Bin_Pack *bp, const uint8_t *data, uint32_t length);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif // C_TOXCORE_TOXCORE_BIN_PACK_H
|
||||
#endif /* C_TOXCORE_TOXCORE_BIN_PACK_H */
|
||||
|
174
external/toxcore/c-toxcore/toxcore/bin_pack_test.cc
vendored
174
external/toxcore/c-toxcore/toxcore/bin_pack_test.cc
vendored
@ -7,118 +7,142 @@
|
||||
#include <vector>
|
||||
|
||||
#include "bin_unpack.h"
|
||||
#include "logger.h"
|
||||
|
||||
namespace {
|
||||
|
||||
struct Bin_Pack_Deleter {
|
||||
void operator()(Bin_Pack *bp) const { bin_pack_free(bp); }
|
||||
};
|
||||
|
||||
using Bin_Pack_Ptr = std::unique_ptr<Bin_Pack, Bin_Pack_Deleter>;
|
||||
|
||||
struct Bin_Unpack_Deleter {
|
||||
void operator()(Bin_Unpack *bu) const { bin_unpack_free(bu); }
|
||||
};
|
||||
|
||||
using Bin_Unpack_Ptr = std::unique_ptr<Bin_Unpack, Bin_Unpack_Deleter>;
|
||||
|
||||
TEST(BinPack, TooSmallBufferIsNotExceeded)
|
||||
{
|
||||
std::array<uint8_t, 7> buf;
|
||||
Bin_Pack_Ptr bp(bin_pack_new(buf.data(), buf.size()));
|
||||
ASSERT_NE(bp, nullptr);
|
||||
EXPECT_FALSE(bin_pack_u64_b(bp.get(), 1234567812345678LL));
|
||||
const uint64_t orig = 1234567812345678LL;
|
||||
std::array<uint8_t, sizeof(orig) - 1> buf;
|
||||
EXPECT_FALSE(bin_pack_obj(
|
||||
[](const void *obj, const Logger *logger, Bin_Pack *bp) {
|
||||
return bin_pack_u64_b(bp, *static_cast<const uint64_t *>(obj));
|
||||
},
|
||||
&orig, nullptr, buf.data(), buf.size()));
|
||||
}
|
||||
|
||||
TEST(BinPack, PackedUint64CanBeUnpacked)
|
||||
{
|
||||
const uint64_t orig = 1234567812345678LL;
|
||||
std::array<uint8_t, 8> buf;
|
||||
Bin_Pack_Ptr bp(bin_pack_new(buf.data(), buf.size()));
|
||||
ASSERT_NE(bp, nullptr);
|
||||
ASSERT_TRUE(bin_pack_u64_b(bp.get(), 1234567812345678LL));
|
||||
EXPECT_TRUE(bin_pack_obj(
|
||||
[](const void *obj, const Logger *logger, Bin_Pack *bp) {
|
||||
return bin_pack_u64_b(bp, *static_cast<const uint64_t *>(obj));
|
||||
},
|
||||
&orig, nullptr, buf.data(), buf.size()));
|
||||
|
||||
Bin_Unpack_Ptr bu(bin_unpack_new(buf.data(), buf.size()));
|
||||
ASSERT_NE(bu, nullptr);
|
||||
uint64_t val;
|
||||
ASSERT_TRUE(bin_unpack_u64_b(bu.get(), &val));
|
||||
EXPECT_EQ(val, 1234567812345678LL);
|
||||
uint64_t unpacked;
|
||||
EXPECT_TRUE(bin_unpack_obj(
|
||||
[](void *obj, Bin_Unpack *bu) {
|
||||
return bin_unpack_u64_b(bu, static_cast<uint64_t *>(obj));
|
||||
},
|
||||
&unpacked, buf.data(), buf.size()));
|
||||
EXPECT_EQ(unpacked, 1234567812345678LL);
|
||||
}
|
||||
|
||||
TEST(BinPack, MsgPackedUint8CanBeUnpackedAsUint32)
|
||||
{
|
||||
const uint8_t orig = 123;
|
||||
std::array<uint8_t, 2> buf;
|
||||
Bin_Pack_Ptr bp(bin_pack_new(buf.data(), buf.size()));
|
||||
ASSERT_NE(bp, nullptr);
|
||||
ASSERT_TRUE(bin_pack_u08(bp.get(), 123));
|
||||
EXPECT_TRUE(bin_pack_obj(
|
||||
[](const void *obj, const Logger *logger, Bin_Pack *bp) {
|
||||
return bin_pack_u08(bp, *static_cast<const uint8_t *>(obj));
|
||||
},
|
||||
&orig, nullptr, buf.data(), buf.size()));
|
||||
|
||||
Bin_Unpack_Ptr bu(bin_unpack_new(buf.data(), buf.size()));
|
||||
ASSERT_NE(bu, nullptr);
|
||||
uint32_t val;
|
||||
ASSERT_TRUE(bin_unpack_u32(bu.get(), &val));
|
||||
EXPECT_EQ(val, 123);
|
||||
uint32_t unpacked;
|
||||
EXPECT_TRUE(bin_unpack_obj(
|
||||
[](void *obj, Bin_Unpack *bu) { return bin_unpack_u32(bu, static_cast<uint32_t *>(obj)); },
|
||||
&unpacked, buf.data(), buf.size()));
|
||||
EXPECT_EQ(unpacked, 123);
|
||||
}
|
||||
|
||||
TEST(BinPack, MsgPackedUint32CanBeUnpackedAsUint8IfSmallEnough)
|
||||
{
|
||||
const uint32_t orig = 123;
|
||||
std::array<uint8_t, 2> buf;
|
||||
Bin_Pack_Ptr bp(bin_pack_new(buf.data(), buf.size()));
|
||||
ASSERT_NE(bp, nullptr);
|
||||
ASSERT_TRUE(bin_pack_u32(bp.get(), 123));
|
||||
EXPECT_TRUE(bin_pack_obj(
|
||||
[](const void *obj, const Logger *logger, Bin_Pack *bp) {
|
||||
return bin_pack_u32(bp, *static_cast<const uint32_t *>(obj));
|
||||
},
|
||||
&orig, nullptr, buf.data(), buf.size()));
|
||||
|
||||
Bin_Unpack_Ptr bu(bin_unpack_new(buf.data(), buf.size()));
|
||||
ASSERT_NE(bu, nullptr);
|
||||
uint8_t val;
|
||||
ASSERT_TRUE(bin_unpack_u08(bu.get(), &val));
|
||||
EXPECT_EQ(val, 123);
|
||||
uint8_t unpacked;
|
||||
EXPECT_TRUE(bin_unpack_obj(
|
||||
[](void *obj, Bin_Unpack *bu) { return bin_unpack_u08(bu, static_cast<uint8_t *>(obj)); },
|
||||
&unpacked, buf.data(), buf.size()));
|
||||
|
||||
EXPECT_EQ(unpacked, 123);
|
||||
}
|
||||
|
||||
TEST(BinPack, LargeMsgPackedUint32CannotBeUnpackedAsUint8)
|
||||
{
|
||||
const uint32_t orig = 1234567;
|
||||
std::array<uint8_t, 5> buf;
|
||||
Bin_Pack_Ptr bp(bin_pack_new(buf.data(), buf.size()));
|
||||
ASSERT_NE(bp, nullptr);
|
||||
ASSERT_TRUE(bin_pack_u32(bp.get(), 1234567));
|
||||
EXPECT_TRUE(bin_pack_obj(
|
||||
[](const void *obj, const Logger *logger, Bin_Pack *bp) {
|
||||
return bin_pack_u32(bp, *static_cast<const uint32_t *>(obj));
|
||||
},
|
||||
&orig, nullptr, buf.data(), buf.size()));
|
||||
|
||||
Bin_Unpack_Ptr bu(bin_unpack_new(buf.data(), buf.size()));
|
||||
ASSERT_NE(bu, nullptr);
|
||||
uint8_t val;
|
||||
EXPECT_FALSE(bin_unpack_u08(bu.get(), &val));
|
||||
uint8_t unpacked;
|
||||
EXPECT_FALSE(bin_unpack_obj(
|
||||
[](void *obj, Bin_Unpack *bu) { return bin_unpack_u08(bu, static_cast<uint8_t *>(obj)); },
|
||||
&unpacked, buf.data(), buf.size()));
|
||||
}
|
||||
|
||||
TEST(BinPack, BinCanHoldPackedInts)
|
||||
{
|
||||
std::array<uint8_t, 12> buf;
|
||||
Bin_Pack_Ptr bp(bin_pack_new(buf.data(), buf.size()));
|
||||
ASSERT_NE(bp, nullptr);
|
||||
ASSERT_TRUE(bin_pack_bin_marker(bp.get(), 8));
|
||||
ASSERT_TRUE(bin_pack_u64_b(bp.get(), 1234567812345678LL));
|
||||
ASSERT_TRUE(bin_pack_u16_b(bp.get(), 54321));
|
||||
struct Stuff {
|
||||
uint64_t u64;
|
||||
uint16_t u16;
|
||||
};
|
||||
const Stuff orig = {1234567812345678LL, 54321};
|
||||
static const uint32_t packed_size = sizeof(uint64_t) + sizeof(uint16_t);
|
||||
|
||||
Bin_Unpack_Ptr bu(bin_unpack_new(buf.data(), buf.size()));
|
||||
ASSERT_NE(bu, nullptr);
|
||||
uint32_t size;
|
||||
EXPECT_TRUE(bin_unpack_bin_size(bu.get(), &size));
|
||||
EXPECT_EQ(size, 8);
|
||||
uint64_t val1;
|
||||
EXPECT_TRUE(bin_unpack_u64_b(bu.get(), &val1));
|
||||
EXPECT_EQ(val1, 1234567812345678LL);
|
||||
uint16_t val2;
|
||||
EXPECT_TRUE(bin_unpack_u16_b(bu.get(), &val2));
|
||||
EXPECT_EQ(val2, 54321);
|
||||
std::array<uint8_t, 12> buf;
|
||||
EXPECT_TRUE(bin_pack_obj(
|
||||
[](const void *obj, const Logger *logger, Bin_Pack *bp) {
|
||||
const Stuff *self = static_cast<const Stuff *>(obj);
|
||||
return bin_pack_bin_marker(bp, packed_size) //
|
||||
&& bin_pack_u64_b(bp, self->u64) //
|
||||
&& bin_pack_u16_b(bp, self->u16);
|
||||
},
|
||||
&orig, nullptr, buf.data(), buf.size()));
|
||||
|
||||
Stuff unpacked;
|
||||
EXPECT_TRUE(bin_unpack_obj(
|
||||
[](void *obj, Bin_Unpack *bu) {
|
||||
Stuff *stuff = static_cast<Stuff *>(obj);
|
||||
uint32_t size;
|
||||
return bin_unpack_bin_size(bu, &size) //
|
||||
&& size == 10 //
|
||||
&& bin_unpack_u64_b(bu, &stuff->u64) //
|
||||
&& bin_unpack_u16_b(bu, &stuff->u16);
|
||||
},
|
||||
&unpacked, buf.data(), buf.size()));
|
||||
EXPECT_EQ(unpacked.u64, 1234567812345678LL);
|
||||
EXPECT_EQ(unpacked.u16, 54321);
|
||||
}
|
||||
|
||||
TEST(BinPack, BinCanHoldArbitraryData)
|
||||
{
|
||||
std::array<uint8_t, 7> buf;
|
||||
Bin_Pack_Ptr bp(bin_pack_new(buf.data(), buf.size()));
|
||||
ASSERT_NE(bp, nullptr);
|
||||
ASSERT_TRUE(bin_pack_bin_marker(bp.get(), 5));
|
||||
ASSERT_TRUE(bin_pack_bin_b(bp.get(), reinterpret_cast<const uint8_t *>("hello"), 5));
|
||||
EXPECT_TRUE(bin_pack_obj(
|
||||
[](const void *obj, const Logger *logger, Bin_Pack *bp) {
|
||||
return bin_pack_bin_marker(bp, 5) //
|
||||
&& bin_pack_bin_b(bp, reinterpret_cast<const uint8_t *>("hello"), 5);
|
||||
},
|
||||
nullptr, nullptr, buf.data(), buf.size()));
|
||||
|
||||
Bin_Unpack_Ptr bu(bin_unpack_new(buf.data(), buf.size()));
|
||||
ASSERT_NE(bu, nullptr);
|
||||
std::array<uint8_t, 5> str;
|
||||
EXPECT_TRUE(bin_unpack_bin_fixed(bu.get(), str.data(), str.size()));
|
||||
EXPECT_TRUE(bin_unpack_obj(
|
||||
[](void *obj, Bin_Unpack *bu) {
|
||||
uint8_t *data = static_cast<uint8_t *>(obj);
|
||||
return bin_unpack_bin_fixed(bu, data, 5);
|
||||
},
|
||||
str.data(), buf.data(), buf.size()));
|
||||
EXPECT_EQ(str, (std::array<uint8_t, 5>{'h', 'e', 'l', 'l', 'o'}));
|
||||
}
|
||||
|
||||
@ -126,9 +150,13 @@ TEST(BinPack, OversizedArrayFailsUnpack)
|
||||
{
|
||||
std::array<uint8_t, 1> buf = {0x91};
|
||||
|
||||
Bin_Unpack_Ptr bu(bin_unpack_new(buf.data(), buf.size()));
|
||||
uint32_t size;
|
||||
EXPECT_FALSE(bin_unpack_array(bu.get(), &size));
|
||||
EXPECT_FALSE(bin_unpack_obj(
|
||||
[](void *obj, Bin_Unpack *bu) {
|
||||
uint32_t *size_ptr = static_cast<uint32_t *>(obj);
|
||||
return bin_unpack_array(bu, size_ptr);
|
||||
},
|
||||
&size, buf.data(), buf.size()));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
32
external/toxcore/c-toxcore/toxcore/bin_unpack.c
vendored
32
external/toxcore/c-toxcore/toxcore/bin_unpack.c
vendored
@ -9,6 +9,7 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "../third_party/cmp/cmp.h"
|
||||
#include "attributes.h"
|
||||
#include "ccompat.h"
|
||||
|
||||
struct Bin_Unpack {
|
||||
@ -20,27 +21,28 @@ struct Bin_Unpack {
|
||||
non_null()
|
||||
static bool buf_reader(cmp_ctx_t *ctx, void *data, size_t limit)
|
||||
{
|
||||
uint8_t *bytes = (uint8_t *)data;
|
||||
Bin_Unpack *reader = (Bin_Unpack *)ctx->buf;
|
||||
assert(reader != nullptr && reader->bytes != nullptr);
|
||||
if (limit > reader->bytes_size) {
|
||||
return false;
|
||||
}
|
||||
memcpy(data, reader->bytes, limit);
|
||||
memcpy(bytes, reader->bytes, limit);
|
||||
reader->bytes += limit;
|
||||
reader->bytes_size -= limit;
|
||||
return true;
|
||||
}
|
||||
|
||||
non_null()
|
||||
static bool buf_skipper(cmp_ctx_t *ctx, size_t limit)
|
||||
static bool buf_skipper(cmp_ctx_t *ctx, size_t count)
|
||||
{
|
||||
Bin_Unpack *reader = (Bin_Unpack *)ctx->buf;
|
||||
assert(reader != nullptr && reader->bytes != nullptr);
|
||||
if (limit > reader->bytes_size) {
|
||||
if (count > reader->bytes_size) {
|
||||
return false;
|
||||
}
|
||||
reader->bytes += limit;
|
||||
reader->bytes_size -= limit;
|
||||
reader->bytes += count;
|
||||
reader->bytes_size -= count;
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -51,21 +53,19 @@ static size_t null_writer(cmp_ctx_t *ctx, const void *data, size_t count)
|
||||
return 0;
|
||||
}
|
||||
|
||||
Bin_Unpack *bin_unpack_new(const uint8_t *buf, uint32_t buf_size)
|
||||
non_null()
|
||||
static void bin_unpack_init(Bin_Unpack *bu, const uint8_t *buf, uint32_t buf_size)
|
||||
{
|
||||
Bin_Unpack *bu = (Bin_Unpack *)calloc(1, sizeof(Bin_Unpack));
|
||||
if (bu == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
bu->bytes = buf;
|
||||
bu->bytes_size = buf_size;
|
||||
cmp_init(&bu->ctx, bu, buf_reader, buf_skipper, null_writer);
|
||||
return bu;
|
||||
}
|
||||
|
||||
void bin_unpack_free(Bin_Unpack *bu)
|
||||
bool bin_unpack_obj(bin_unpack_cb *callback, void *obj, const uint8_t *buf, uint32_t buf_size)
|
||||
{
|
||||
free(bu);
|
||||
Bin_Unpack bu;
|
||||
bin_unpack_init(&bu, buf, buf_size);
|
||||
return callback(obj, &bu);
|
||||
}
|
||||
|
||||
bool bin_unpack_array(Bin_Unpack *bu, uint32_t *size)
|
||||
@ -169,7 +169,7 @@ bool bin_unpack_u16_b(Bin_Unpack *bu, uint16_t *val)
|
||||
uint8_t hi = 0;
|
||||
uint8_t lo = 0;
|
||||
if (!(bin_unpack_u08_b(bu, &hi)
|
||||
&& bin_unpack_u08_b(bu, &lo))) {
|
||||
&& bin_unpack_u08_b(bu, &lo))) {
|
||||
return false;
|
||||
}
|
||||
*val = ((uint16_t)hi << 8) | lo;
|
||||
@ -181,7 +181,7 @@ bool bin_unpack_u32_b(Bin_Unpack *bu, uint32_t *val)
|
||||
uint16_t hi = 0;
|
||||
uint16_t lo = 0;
|
||||
if (!(bin_unpack_u16_b(bu, &hi)
|
||||
&& bin_unpack_u16_b(bu, &lo))) {
|
||||
&& bin_unpack_u16_b(bu, &lo))) {
|
||||
return false;
|
||||
}
|
||||
*val = ((uint32_t)hi << 16) | lo;
|
||||
@ -193,7 +193,7 @@ bool bin_unpack_u64_b(Bin_Unpack *bu, uint64_t *val)
|
||||
uint32_t hi = 0;
|
||||
uint32_t lo = 0;
|
||||
if (!(bin_unpack_u32_b(bu, &hi)
|
||||
&& bin_unpack_u32_b(bu, &lo))) {
|
||||
&& bin_unpack_u32_b(bu, &lo))) {
|
||||
return false;
|
||||
}
|
||||
*val = ((uint64_t)hi << 32) | lo;
|
||||
|
38
external/toxcore/c-toxcore/toxcore/bin_unpack.h
vendored
38
external/toxcore/c-toxcore/toxcore/bin_unpack.h
vendored
@ -16,25 +16,37 @@ extern "C" {
|
||||
|
||||
/**
|
||||
* @brief Binary deserialisation object.
|
||||
*
|
||||
* User code never creates this object. It is created and destroyed within the below functions,
|
||||
* and passed to the callback. This enforces an alloc/dealloc bracket, so user code can never
|
||||
* forget to clean up an unpacker.
|
||||
*/
|
||||
typedef struct Bin_Unpack Bin_Unpack;
|
||||
|
||||
/** @brief Allocate a new unpacker object.
|
||||
/** @brief Function used to unpack an object.
|
||||
*
|
||||
* @param buf The byte array to unpack values from.
|
||||
* This function would typically cast the `void *` to the actual object pointer type and then call
|
||||
* more appropriately typed unpacking functions.
|
||||
*/
|
||||
typedef bool bin_unpack_cb(void *obj, Bin_Unpack *bu);
|
||||
|
||||
/** @brief Unpack an object from a buffer of a given size.
|
||||
*
|
||||
* This function creates and initialises a `Bin_Unpack` object, calls the callback with the
|
||||
* unpacker object and the to-be-unpacked object, and then cleans up the unpacker object.
|
||||
*
|
||||
* Unlike `bin_pack_obj`, this function does not support NULL anywhere. The input array
|
||||
* must be non-null, even if it is zero-length.
|
||||
*
|
||||
* @param callback The function called on the created unpacker and unpacked object.
|
||||
* @param obj The object to be packed, passed as `obj` to the callback.
|
||||
* @param buf A byte array containing the serialised representation of `obj`.
|
||||
* @param buf_size The size of the byte array.
|
||||
*
|
||||
* @retval nullptr on allocation failure.
|
||||
* @retval false if an error occurred (e.g. buffer overrun).
|
||||
*/
|
||||
non_null()
|
||||
Bin_Unpack *bin_unpack_new(const uint8_t *buf, uint32_t buf_size);
|
||||
|
||||
/** @brief Deallocates an unpacker object.
|
||||
*
|
||||
* Does not deallocate the buffer inside.
|
||||
*/
|
||||
nullable(1)
|
||||
void bin_unpack_free(Bin_Unpack *bu);
|
||||
bool bin_unpack_obj(bin_unpack_cb *callback, void *obj, const uint8_t *buf, uint32_t buf_size);
|
||||
|
||||
/** @brief Start unpacking a MessagePack array.
|
||||
*
|
||||
@ -107,7 +119,7 @@ non_null() bool bin_unpack_u64_b(Bin_Unpack *bu, uint64_t *val);
|
||||
non_null() bool bin_unpack_bin_b(Bin_Unpack *bu, uint8_t *data, uint32_t length);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif // C_TOXCORE_TOXCORE_BIN_UNPACK_H
|
||||
#endif /* C_TOXCORE_TOXCORE_BIN_UNPACK_H */
|
||||
|
19
external/toxcore/c-toxcore/toxcore/ccompat.h
vendored
19
external/toxcore/c-toxcore/toxcore/ccompat.h
vendored
@ -26,7 +26,6 @@
|
||||
#if !defined(DISABLE_VLA) && !defined(_MSC_VER) && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
|
||||
// C99 VLAs.
|
||||
#define ALLOC_VLA(type, name, size) type name[size]
|
||||
#define SIZEOF_VLA sizeof
|
||||
#else
|
||||
|
||||
// Emulation using alloca.
|
||||
@ -47,9 +46,7 @@
|
||||
#endif
|
||||
|
||||
#define ALLOC_VLA(type, name, size) \
|
||||
const size_t name##_vla_size = (size) * sizeof(type); \
|
||||
type *const name = (type *)alloca(name##_vla_size)
|
||||
#define SIZEOF_VLA(name) name##_vla_size
|
||||
type *const name = (type *)alloca((size) * sizeof(type))
|
||||
|
||||
#endif
|
||||
|
||||
@ -72,16 +69,10 @@
|
||||
#define STATIC_ASSERT_(cond, msg, line) typedef int static_assert_##line[(cond) ? 1 : -1]
|
||||
#define STATIC_ASSERT(cond, msg, line) STATIC_ASSERT_(cond, msg, line)
|
||||
#define static_assert(cond, msg) STATIC_ASSERT(cond, msg, __LINE__)
|
||||
#endif // !__GNUC__
|
||||
#endif // !static_assert
|
||||
#endif // !__cplusplus
|
||||
|
||||
#ifdef __GNUC__
|
||||
#define GNU_PRINTF(f, a) __attribute__((__format__(__printf__, f, a)))
|
||||
#else
|
||||
#define GNU_PRINTF(f, a)
|
||||
#endif
|
||||
#endif /* !__GNUC__ */
|
||||
#endif /* !static_assert */
|
||||
#endif /* !__cplusplus */
|
||||
|
||||
//!TOKSTYLE+
|
||||
|
||||
#endif // C_TOXCORE_TOXCORE_CCOMPAT_H
|
||||
#endif /* C_TOXCORE_TOXCORE_CCOMPAT_H */
|
||||
|
174
external/toxcore/c-toxcore/toxcore/crypto_core.c
vendored
174
external/toxcore/c-toxcore/toxcore/crypto_core.c
vendored
@ -1,13 +1,8 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
* Copyright © 2016-2018 The TokTok team.
|
||||
* Copyright © 2016-2024 The TokTok team.
|
||||
* Copyright © 2013 Tox project.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Functions for the core crypto.
|
||||
*
|
||||
* NOTE: This code has to be perfect. We don't mess around with encryption.
|
||||
*/
|
||||
#include "crypto_core.h"
|
||||
|
||||
#include <assert.h>
|
||||
@ -16,15 +11,9 @@
|
||||
|
||||
#include <sodium.h>
|
||||
|
||||
#include "attributes.h"
|
||||
#include "ccompat.h"
|
||||
|
||||
#ifndef crypto_box_MACBYTES
|
||||
#define crypto_box_MACBYTES (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES)
|
||||
#endif
|
||||
|
||||
// Need dht because of ENC_SECRET_KEY_SIZE and ENC_PUBLIC_KEY_SIZE
|
||||
#define ENC_PUBLIC_KEY_SIZE CRYPTO_PUBLIC_KEY_SIZE
|
||||
#define ENC_SECRET_KEY_SIZE CRYPTO_SECRET_KEY_SIZE
|
||||
#include "util.h"
|
||||
|
||||
static_assert(CRYPTO_PUBLIC_KEY_SIZE == crypto_box_PUBLICKEYBYTES,
|
||||
"CRYPTO_PUBLIC_KEY_SIZE should be equal to crypto_box_PUBLICKEYBYTES");
|
||||
@ -56,43 +45,46 @@ static_assert(CRYPTO_SIGN_PUBLIC_KEY_SIZE == crypto_sign_PUBLICKEYBYTES,
|
||||
static_assert(CRYPTO_SIGN_SECRET_KEY_SIZE == crypto_sign_SECRETKEYBYTES,
|
||||
"CRYPTO_SIGN_SECRET_KEY_SIZE should be equal to crypto_sign_SECRETKEYBYTES");
|
||||
|
||||
bool create_extended_keypair(uint8_t *pk, uint8_t *sk)
|
||||
bool create_extended_keypair(Extended_Public_Key *pk, Extended_Secret_Key *sk, const Random *rng)
|
||||
{
|
||||
/* create signature key pair */
|
||||
crypto_sign_keypair(pk + ENC_PUBLIC_KEY_SIZE, sk + ENC_SECRET_KEY_SIZE);
|
||||
uint8_t seed[crypto_sign_SEEDBYTES];
|
||||
random_bytes(rng, seed, crypto_sign_SEEDBYTES);
|
||||
crypto_sign_seed_keypair(pk->sig, sk->sig, seed);
|
||||
crypto_memzero(seed, crypto_sign_SEEDBYTES);
|
||||
|
||||
/* convert public signature key to public encryption key */
|
||||
const int res1 = crypto_sign_ed25519_pk_to_curve25519(pk, pk + ENC_PUBLIC_KEY_SIZE);
|
||||
const int res1 = crypto_sign_ed25519_pk_to_curve25519(pk->enc, pk->sig);
|
||||
|
||||
/* convert secret signature key to secret encryption key */
|
||||
const int res2 = crypto_sign_ed25519_sk_to_curve25519(sk, sk + ENC_SECRET_KEY_SIZE);
|
||||
const int res2 = crypto_sign_ed25519_sk_to_curve25519(sk->enc, sk->sig);
|
||||
|
||||
return res1 == 0 && res2 == 0;
|
||||
}
|
||||
|
||||
const uint8_t *get_enc_key(const uint8_t *key)
|
||||
const uint8_t *get_enc_key(const Extended_Public_Key *key)
|
||||
{
|
||||
return key;
|
||||
return key->enc;
|
||||
}
|
||||
|
||||
const uint8_t *get_sig_pk(const uint8_t *key)
|
||||
const uint8_t *get_sig_pk(const Extended_Public_Key *key)
|
||||
{
|
||||
return key + ENC_PUBLIC_KEY_SIZE;
|
||||
return key->sig;
|
||||
}
|
||||
|
||||
void set_sig_pk(uint8_t *key, const uint8_t *sig_pk)
|
||||
void set_sig_pk(Extended_Public_Key *key, const uint8_t *sig_pk)
|
||||
{
|
||||
memcpy(key + ENC_PUBLIC_KEY_SIZE, sig_pk, SIG_PUBLIC_KEY_SIZE);
|
||||
memcpy(key->sig, sig_pk, SIG_PUBLIC_KEY_SIZE);
|
||||
}
|
||||
|
||||
const uint8_t *get_sig_sk(const uint8_t *key)
|
||||
const uint8_t *get_sig_sk(const Extended_Secret_Key *key)
|
||||
{
|
||||
return key + ENC_SECRET_KEY_SIZE;
|
||||
return key->sig;
|
||||
}
|
||||
|
||||
const uint8_t *get_chat_id(const uint8_t *key)
|
||||
const uint8_t *get_chat_id(const Extended_Public_Key *key)
|
||||
{
|
||||
return key + ENC_PUBLIC_KEY_SIZE;
|
||||
return key->sig;
|
||||
}
|
||||
|
||||
#if !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
|
||||
@ -117,15 +109,15 @@ static void crypto_free(uint8_t *ptr, size_t bytes)
|
||||
|
||||
free(ptr);
|
||||
}
|
||||
#endif // !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
|
||||
#endif /* !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) */
|
||||
|
||||
void crypto_memzero(void *data, size_t length)
|
||||
{
|
||||
#if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
|
||||
memset(data, 0, length);
|
||||
memzero((uint8_t *)data, length);
|
||||
#else
|
||||
sodium_memzero(data, length);
|
||||
#endif
|
||||
#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
|
||||
}
|
||||
|
||||
bool crypto_memlock(void *data, size_t length)
|
||||
@ -134,12 +126,8 @@ bool crypto_memlock(void *data, size_t length)
|
||||
return false;
|
||||
#else
|
||||
|
||||
if (sodium_mlock(data, length) != 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
#endif
|
||||
return sodium_mlock(data, length) == 0;
|
||||
#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
|
||||
}
|
||||
|
||||
bool crypto_memunlock(void *data, size_t length)
|
||||
@ -148,12 +136,8 @@ bool crypto_memunlock(void *data, size_t length)
|
||||
return false;
|
||||
#else
|
||||
|
||||
if (sodium_munlock(data, length) != 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
#endif
|
||||
return sodium_munlock(data, length) == 0;
|
||||
#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
|
||||
}
|
||||
|
||||
bool pk_equal(const uint8_t pk1[CRYPTO_PUBLIC_KEY_SIZE], const uint8_t pk2[CRYPTO_PUBLIC_KEY_SIZE])
|
||||
@ -163,7 +147,7 @@ bool pk_equal(const uint8_t pk1[CRYPTO_PUBLIC_KEY_SIZE], const uint8_t pk2[CRYPT
|
||||
return memcmp(pk1, pk2, CRYPTO_PUBLIC_KEY_SIZE) == 0;
|
||||
#else
|
||||
return crypto_verify_32(pk1, pk2) == 0;
|
||||
#endif
|
||||
#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
|
||||
}
|
||||
|
||||
void pk_copy(uint8_t dest[CRYPTO_PUBLIC_KEY_SIZE], const uint8_t src[CRYPTO_PUBLIC_KEY_SIZE])
|
||||
@ -171,24 +155,24 @@ void pk_copy(uint8_t dest[CRYPTO_PUBLIC_KEY_SIZE], const uint8_t src[CRYPTO_PUBL
|
||||
memcpy(dest, src, CRYPTO_PUBLIC_KEY_SIZE);
|
||||
}
|
||||
|
||||
bool crypto_sha512_eq(const uint8_t *cksum1, const uint8_t *cksum2)
|
||||
bool crypto_sha512_eq(const uint8_t cksum1[CRYPTO_SHA512_SIZE], const uint8_t cksum2[CRYPTO_SHA512_SIZE])
|
||||
{
|
||||
#if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
|
||||
// Hope that this is better for the fuzzer
|
||||
return memcmp(cksum1, cksum2, CRYPTO_SHA512_SIZE) == 0;
|
||||
#else
|
||||
return crypto_verify_64(cksum1, cksum2) == 0;
|
||||
#endif
|
||||
#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
|
||||
}
|
||||
|
||||
bool crypto_sha256_eq(const uint8_t *cksum1, const uint8_t *cksum2)
|
||||
bool crypto_sha256_eq(const uint8_t cksum1[CRYPTO_SHA256_SIZE], const uint8_t cksum2[CRYPTO_SHA256_SIZE])
|
||||
{
|
||||
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
|
||||
// Hope that this is better for the fuzzer
|
||||
return memcmp(cksum1, cksum2, CRYPTO_SHA256_SIZE) == 0;
|
||||
#else
|
||||
return crypto_verify_32(cksum1, cksum2) == 0;
|
||||
#endif
|
||||
#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
|
||||
}
|
||||
|
||||
uint8_t random_u08(const Random *rng)
|
||||
@ -224,36 +208,40 @@ uint32_t random_range_u32(const Random *rng, uint32_t upper_bound)
|
||||
return rng->funcs->random_uniform(rng->obj, upper_bound);
|
||||
}
|
||||
|
||||
bool crypto_signature_create(uint8_t *signature, const uint8_t *message, uint64_t message_length,
|
||||
const uint8_t *secret_key)
|
||||
bool crypto_signature_create(uint8_t signature[CRYPTO_SIGNATURE_SIZE],
|
||||
const uint8_t *message, uint64_t message_length,
|
||||
const uint8_t secret_key[SIG_SECRET_KEY_SIZE])
|
||||
{
|
||||
return crypto_sign_detached(signature, nullptr, message, message_length, secret_key) == 0;
|
||||
}
|
||||
|
||||
bool crypto_signature_verify(const uint8_t *signature, const uint8_t *message, uint64_t message_length,
|
||||
const uint8_t *public_key)
|
||||
bool crypto_signature_verify(const uint8_t signature[CRYPTO_SIGNATURE_SIZE],
|
||||
const uint8_t *message, uint64_t message_length,
|
||||
const uint8_t public_key[SIG_PUBLIC_KEY_SIZE])
|
||||
{
|
||||
return crypto_sign_verify_detached(signature, message, message_length, public_key) == 0;
|
||||
}
|
||||
|
||||
bool public_key_valid(const uint8_t *public_key)
|
||||
bool public_key_valid(const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE])
|
||||
{
|
||||
/* Last bit of key is always zero. */
|
||||
return public_key[31] < 128;
|
||||
}
|
||||
|
||||
int32_t encrypt_precompute(const uint8_t *public_key, const uint8_t *secret_key,
|
||||
uint8_t *shared_key)
|
||||
int32_t encrypt_precompute(const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE],
|
||||
const uint8_t secret_key[CRYPTO_SECRET_KEY_SIZE],
|
||||
uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE])
|
||||
{
|
||||
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
|
||||
memcpy(shared_key, public_key, CRYPTO_SHARED_KEY_SIZE);
|
||||
return 0;
|
||||
#else
|
||||
return crypto_box_beforenm(shared_key, public_key, secret_key);
|
||||
#endif
|
||||
#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
|
||||
}
|
||||
|
||||
int32_t encrypt_data_symmetric(const uint8_t *shared_key, const uint8_t *nonce,
|
||||
int32_t encrypt_data_symmetric(const uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE],
|
||||
const uint8_t nonce[CRYPTO_NONCE_SIZE],
|
||||
const uint8_t *plain, size_t length, uint8_t *encrypted)
|
||||
{
|
||||
if (length == 0 || shared_key == nullptr || nonce == nullptr || plain == nullptr || encrypted == nullptr) {
|
||||
@ -264,7 +252,7 @@ int32_t encrypt_data_symmetric(const uint8_t *shared_key, const uint8_t *nonce,
|
||||
// Don't encrypt anything.
|
||||
memcpy(encrypted, plain, length);
|
||||
// Zero MAC to avoid uninitialized memory reads.
|
||||
memset(encrypted + length, 0, crypto_box_MACBYTES);
|
||||
memzero(encrypted + length, crypto_box_MACBYTES);
|
||||
#else
|
||||
|
||||
const size_t size_temp_plain = length + crypto_box_ZEROBYTES;
|
||||
@ -282,9 +270,9 @@ int32_t encrypt_data_symmetric(const uint8_t *shared_key, const uint8_t *nonce,
|
||||
// crypto_box_afternm requires the entire range of the output array be
|
||||
// initialised with something. It doesn't matter what it's initialised with,
|
||||
// so we'll pick 0x00.
|
||||
memset(temp_encrypted, 0, size_temp_encrypted);
|
||||
memzero(temp_encrypted, size_temp_encrypted);
|
||||
|
||||
memset(temp_plain, 0, crypto_box_ZEROBYTES);
|
||||
memzero(temp_plain, crypto_box_ZEROBYTES);
|
||||
// Pad the message with 32 0 bytes.
|
||||
memcpy(temp_plain + crypto_box_ZEROBYTES, plain, length);
|
||||
|
||||
@ -300,12 +288,13 @@ int32_t encrypt_data_symmetric(const uint8_t *shared_key, const uint8_t *nonce,
|
||||
|
||||
crypto_free(temp_plain, size_temp_plain);
|
||||
crypto_free(temp_encrypted, size_temp_encrypted);
|
||||
#endif
|
||||
#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
|
||||
assert(length < INT32_MAX - crypto_box_MACBYTES);
|
||||
return (int32_t)(length + crypto_box_MACBYTES);
|
||||
}
|
||||
|
||||
int32_t decrypt_data_symmetric(const uint8_t *shared_key, const uint8_t *nonce,
|
||||
int32_t decrypt_data_symmetric(const uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE],
|
||||
const uint8_t nonce[CRYPTO_NONCE_SIZE],
|
||||
const uint8_t *encrypted, size_t length, uint8_t *plain)
|
||||
{
|
||||
if (length <= crypto_box_BOXZEROBYTES || shared_key == nullptr || nonce == nullptr || encrypted == nullptr
|
||||
@ -333,9 +322,9 @@ int32_t decrypt_data_symmetric(const uint8_t *shared_key, const uint8_t *nonce,
|
||||
// crypto_box_open_afternm requires the entire range of the output array be
|
||||
// initialised with something. It doesn't matter what it's initialised with,
|
||||
// so we'll pick 0x00.
|
||||
memset(temp_plain, 0, size_temp_plain);
|
||||
memzero(temp_plain, size_temp_plain);
|
||||
|
||||
memset(temp_encrypted, 0, crypto_box_BOXZEROBYTES);
|
||||
memzero(temp_encrypted, crypto_box_BOXZEROBYTES);
|
||||
// Pad the message with 16 0 bytes.
|
||||
memcpy(temp_encrypted + crypto_box_BOXZEROBYTES, encrypted, length);
|
||||
|
||||
@ -350,13 +339,15 @@ int32_t decrypt_data_symmetric(const uint8_t *shared_key, const uint8_t *nonce,
|
||||
|
||||
crypto_free(temp_plain, size_temp_plain);
|
||||
crypto_free(temp_encrypted, size_temp_encrypted);
|
||||
#endif
|
||||
#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
|
||||
assert(length > crypto_box_MACBYTES);
|
||||
assert(length < INT32_MAX);
|
||||
return (int32_t)(length - crypto_box_MACBYTES);
|
||||
}
|
||||
|
||||
int32_t encrypt_data(const uint8_t *public_key, const uint8_t *secret_key, const uint8_t *nonce,
|
||||
int32_t encrypt_data(const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE],
|
||||
const uint8_t secret_key[CRYPTO_SECRET_KEY_SIZE],
|
||||
const uint8_t nonce[CRYPTO_NONCE_SIZE],
|
||||
const uint8_t *plain, size_t length, uint8_t *encrypted)
|
||||
{
|
||||
if (public_key == nullptr || secret_key == nullptr) {
|
||||
@ -370,7 +361,9 @@ int32_t encrypt_data(const uint8_t *public_key, const uint8_t *secret_key, const
|
||||
return ret;
|
||||
}
|
||||
|
||||
int32_t decrypt_data(const uint8_t *public_key, const uint8_t *secret_key, const uint8_t *nonce,
|
||||
int32_t decrypt_data(const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE],
|
||||
const uint8_t secret_key[CRYPTO_SECRET_KEY_SIZE],
|
||||
const uint8_t nonce[CRYPTO_NONCE_SIZE],
|
||||
const uint8_t *encrypted, size_t length, uint8_t *plain)
|
||||
{
|
||||
if (public_key == nullptr || secret_key == nullptr) {
|
||||
@ -384,7 +377,7 @@ int32_t decrypt_data(const uint8_t *public_key, const uint8_t *secret_key, const
|
||||
return ret;
|
||||
}
|
||||
|
||||
void increment_nonce(uint8_t *nonce)
|
||||
void increment_nonce(uint8_t nonce[CRYPTO_NONCE_SIZE])
|
||||
{
|
||||
/* TODO(irungentoo): use `increment_nonce_number(nonce, 1)` or
|
||||
* sodium_increment (change to little endian).
|
||||
@ -403,7 +396,7 @@ void increment_nonce(uint8_t *nonce)
|
||||
}
|
||||
}
|
||||
|
||||
void increment_nonce_number(uint8_t *nonce, uint32_t increment)
|
||||
void increment_nonce_number(uint8_t nonce[CRYPTO_NONCE_SIZE], uint32_t increment)
|
||||
{
|
||||
/* NOTE don't use breaks inside this loop
|
||||
* In particular, make sure, as far as possible,
|
||||
@ -425,25 +418,28 @@ void increment_nonce_number(uint8_t *nonce, uint32_t increment)
|
||||
}
|
||||
}
|
||||
|
||||
void random_nonce(const Random *rng, uint8_t *nonce)
|
||||
void random_nonce(const Random *rng, uint8_t nonce[CRYPTO_NONCE_SIZE])
|
||||
{
|
||||
random_bytes(rng, nonce, crypto_box_NONCEBYTES);
|
||||
}
|
||||
|
||||
void new_symmetric_key(const Random *rng, uint8_t *key)
|
||||
void new_symmetric_key(const Random *rng, uint8_t key[CRYPTO_SYMMETRIC_KEY_SIZE])
|
||||
{
|
||||
random_bytes(rng, key, CRYPTO_SYMMETRIC_KEY_SIZE);
|
||||
}
|
||||
|
||||
int32_t crypto_new_keypair(const Random *rng, uint8_t *public_key, uint8_t *secret_key)
|
||||
int32_t crypto_new_keypair(const Random *rng,
|
||||
uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE],
|
||||
uint8_t secret_key[CRYPTO_SECRET_KEY_SIZE])
|
||||
{
|
||||
random_bytes(rng, secret_key, CRYPTO_SECRET_KEY_SIZE);
|
||||
memset(public_key, 0, CRYPTO_PUBLIC_KEY_SIZE); // Make MSAN happy
|
||||
memzero(public_key, CRYPTO_PUBLIC_KEY_SIZE); // Make MSAN happy
|
||||
crypto_derive_public_key(public_key, secret_key);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void crypto_derive_public_key(uint8_t *public_key, const uint8_t *secret_key)
|
||||
void crypto_derive_public_key(uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE],
|
||||
const uint8_t secret_key[CRYPTO_SECRET_KEY_SIZE])
|
||||
{
|
||||
crypto_scalarmult_curve25519_base(public_key, secret_key);
|
||||
}
|
||||
@ -453,15 +449,15 @@ void new_hmac_key(const Random *rng, uint8_t key[CRYPTO_HMAC_KEY_SIZE])
|
||||
random_bytes(rng, key, CRYPTO_HMAC_KEY_SIZE);
|
||||
}
|
||||
|
||||
void crypto_hmac(uint8_t auth[CRYPTO_HMAC_SIZE], const uint8_t key[CRYPTO_HMAC_KEY_SIZE], const uint8_t *data,
|
||||
size_t length)
|
||||
void crypto_hmac(uint8_t auth[CRYPTO_HMAC_SIZE], const uint8_t key[CRYPTO_HMAC_KEY_SIZE],
|
||||
const uint8_t *data, size_t length)
|
||||
{
|
||||
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
|
||||
memcpy(auth, key, 16);
|
||||
memcpy(auth + 16, data, length < 16 ? length : 16);
|
||||
#else
|
||||
crypto_auth(auth, data, length, key);
|
||||
#endif
|
||||
#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
|
||||
}
|
||||
|
||||
bool crypto_hmac_verify(const uint8_t auth[CRYPTO_HMAC_SIZE], const uint8_t key[CRYPTO_HMAC_KEY_SIZE],
|
||||
@ -471,27 +467,27 @@ bool crypto_hmac_verify(const uint8_t auth[CRYPTO_HMAC_SIZE], const uint8_t key[
|
||||
return memcmp(auth, key, 16) == 0 && memcmp(auth + 16, data, length < 16 ? length : 16) == 0;
|
||||
#else
|
||||
return crypto_auth_verify(auth, data, length, key) == 0;
|
||||
#endif
|
||||
#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
|
||||
}
|
||||
|
||||
void crypto_sha256(uint8_t *hash, const uint8_t *data, size_t length)
|
||||
void crypto_sha256(uint8_t hash[CRYPTO_SHA256_SIZE], const uint8_t *data, size_t length)
|
||||
{
|
||||
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
|
||||
memset(hash, 0, CRYPTO_SHA256_SIZE);
|
||||
memzero(hash, CRYPTO_SHA256_SIZE);
|
||||
memcpy(hash, data, length < CRYPTO_SHA256_SIZE ? length : CRYPTO_SHA256_SIZE);
|
||||
#else
|
||||
crypto_hash_sha256(hash, data, length);
|
||||
#endif
|
||||
#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
|
||||
}
|
||||
|
||||
void crypto_sha512(uint8_t *hash, const uint8_t *data, size_t length)
|
||||
void crypto_sha512(uint8_t hash[CRYPTO_SHA512_SIZE], const uint8_t *data, size_t length)
|
||||
{
|
||||
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
|
||||
memset(hash, 0, CRYPTO_SHA512_SIZE);
|
||||
memzero(hash, CRYPTO_SHA512_SIZE);
|
||||
memcpy(hash, data, length < CRYPTO_SHA512_SIZE ? length : CRYPTO_SHA512_SIZE);
|
||||
#else
|
||||
crypto_hash_sha512(hash, data, length);
|
||||
#endif
|
||||
#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
|
||||
}
|
||||
|
||||
non_null()
|
||||
@ -506,26 +502,26 @@ static uint32_t sys_random_uniform(void *obj, uint32_t upper_bound)
|
||||
return randombytes_uniform(upper_bound);
|
||||
}
|
||||
|
||||
static const Random_Funcs system_random_funcs = {
|
||||
static const Random_Funcs os_random_funcs = {
|
||||
sys_random_bytes,
|
||||
sys_random_uniform,
|
||||
};
|
||||
|
||||
static const Random system_random_obj = {&system_random_funcs};
|
||||
static const Random os_random_obj = {&os_random_funcs};
|
||||
|
||||
const Random *system_random(void)
|
||||
const Random *os_random(void)
|
||||
{
|
||||
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
|
||||
if ((true)) {
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
|
||||
// It is safe to call this function more than once and from different
|
||||
// threads -- subsequent calls won't have any effects.
|
||||
if (sodium_init() == -1) {
|
||||
return nullptr;
|
||||
}
|
||||
return &system_random_obj;
|
||||
return &os_random_obj;
|
||||
}
|
||||
|
||||
void random_bytes(const Random *rng, uint8_t *bytes, size_t length)
|
||||
|
159
external/toxcore/c-toxcore/toxcore/crypto_core.h
vendored
159
external/toxcore/c-toxcore/toxcore/crypto_core.h
vendored
@ -1,10 +1,12 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
* Copyright © 2016-2018 The TokTok team.
|
||||
* Copyright © 2016-2024 The TokTok team.
|
||||
* Copyright © 2013 Tox project.
|
||||
*/
|
||||
|
||||
/** @file
|
||||
* @brief Functions for the core crypto.
|
||||
*
|
||||
* @note This code has to be perfect. We don't mess around with encryption.
|
||||
*/
|
||||
#ifndef C_TOXCORE_TOXCORE_CRYPTO_CORE_H
|
||||
#define C_TOXCORE_TOXCORE_CRYPTO_CORE_H
|
||||
@ -20,17 +22,17 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* The number of bytes in a signature.
|
||||
* @brief The number of bytes in a signature.
|
||||
*/
|
||||
#define CRYPTO_SIGNATURE_SIZE 64
|
||||
|
||||
/**
|
||||
* The number of bytes in a Tox public key used for signatures.
|
||||
* @brief The number of bytes in a Tox public key used for signatures.
|
||||
*/
|
||||
#define CRYPTO_SIGN_PUBLIC_KEY_SIZE 32
|
||||
|
||||
/**
|
||||
* The number of bytes in a Tox secret key used for signatures.
|
||||
* @brief The number of bytes in a Tox secret key used for signatures.
|
||||
*/
|
||||
#define CRYPTO_SIGN_SECRET_KEY_SIZE 64
|
||||
|
||||
@ -75,20 +77,43 @@ extern "C" {
|
||||
*/
|
||||
#define CRYPTO_SHA512_SIZE 64
|
||||
|
||||
/** @brief Fill a byte array with random bytes.
|
||||
*
|
||||
* This is the key generator callback and as such must be a cryptographically
|
||||
* secure pseudo-random number generator (CSPRNG). The security of Tox heavily
|
||||
* depends on the security of this RNG.
|
||||
*/
|
||||
typedef void crypto_random_bytes_cb(void *obj, uint8_t *bytes, size_t length);
|
||||
|
||||
/** @brief Generate a random integer between 0 and @p upper_bound.
|
||||
*
|
||||
* Should produce a uniform random distribution, but Tox security does not
|
||||
* depend on this being correct. In principle, it could even be a non-CSPRNG.
|
||||
*/
|
||||
typedef uint32_t crypto_random_uniform_cb(void *obj, uint32_t upper_bound);
|
||||
|
||||
/** @brief Virtual function table for Random. */
|
||||
typedef struct Random_Funcs {
|
||||
crypto_random_bytes_cb *random_bytes;
|
||||
crypto_random_uniform_cb *random_uniform;
|
||||
} Random_Funcs;
|
||||
|
||||
/** @brief Random number generator object.
|
||||
*
|
||||
* Can be used by test code and fuzzers to make toxcore behave in specific
|
||||
* well-defined (non-random) ways. Production code ought to use libsodium's
|
||||
* CSPRNG and use `os_random` below.
|
||||
*/
|
||||
typedef struct Random {
|
||||
const Random_Funcs *funcs;
|
||||
void *obj;
|
||||
} Random;
|
||||
|
||||
const Random *system_random(void);
|
||||
/** @brief System random number generator.
|
||||
*
|
||||
* Uses libsodium's CSPRNG (on Linux, `/dev/urandom`).
|
||||
*/
|
||||
const Random *os_random(void);
|
||||
|
||||
/**
|
||||
* @brief The number of bytes in an encryption public key used by DHT group chats.
|
||||
@ -147,25 +172,29 @@ void crypto_memzero(void *data, size_t length);
|
||||
|
||||
/**
|
||||
* @brief Compute a SHA256 hash (32 bytes).
|
||||
*
|
||||
* @param[out] hash The SHA256 hash of @p data will be written to this byte array.
|
||||
*/
|
||||
non_null()
|
||||
void crypto_sha256(uint8_t *hash, const uint8_t *data, size_t length);
|
||||
void crypto_sha256(uint8_t hash[CRYPTO_SHA256_SIZE], const uint8_t *data, size_t length);
|
||||
|
||||
/**
|
||||
* @brief Compute a SHA512 hash (64 bytes).
|
||||
*
|
||||
* @param[out] hash The SHA512 hash of @p data will be written to this byte array.
|
||||
*/
|
||||
non_null()
|
||||
void crypto_sha512(uint8_t *hash, const uint8_t *data, size_t length);
|
||||
void crypto_sha512(uint8_t hash[CRYPTO_SHA512_SIZE], const uint8_t *data, size_t length);
|
||||
|
||||
/**
|
||||
* @brief Compute an HMAC authenticator (32 bytes).
|
||||
*
|
||||
* @param auth Resulting authenticator.
|
||||
* @param[out] auth Resulting authenticator.
|
||||
* @param key Secret key, as generated by `new_hmac_key()`.
|
||||
*/
|
||||
non_null()
|
||||
void crypto_hmac(uint8_t auth[CRYPTO_HMAC_SIZE], const uint8_t key[CRYPTO_HMAC_KEY_SIZE], const uint8_t *data,
|
||||
size_t length);
|
||||
void crypto_hmac(uint8_t auth[CRYPTO_HMAC_SIZE], const uint8_t key[CRYPTO_HMAC_KEY_SIZE],
|
||||
const uint8_t *data, size_t length);
|
||||
|
||||
/**
|
||||
* @brief Verify an HMAC authenticator.
|
||||
@ -176,7 +205,7 @@ bool crypto_hmac_verify(const uint8_t auth[CRYPTO_HMAC_SIZE], const uint8_t key[
|
||||
|
||||
/**
|
||||
* @brief Compare 2 public keys of length @ref CRYPTO_PUBLIC_KEY_SIZE, not vulnerable to
|
||||
* timing attacks.
|
||||
* timing attacks.
|
||||
*
|
||||
* @retval true if both mem locations of length are equal
|
||||
* @retval false if they are not
|
||||
@ -192,21 +221,21 @@ void pk_copy(uint8_t dest[CRYPTO_PUBLIC_KEY_SIZE], const uint8_t src[CRYPTO_PUBL
|
||||
|
||||
/**
|
||||
* @brief Compare 2 SHA512 checksums of length CRYPTO_SHA512_SIZE, not vulnerable to
|
||||
* timing attacks.
|
||||
* timing attacks.
|
||||
*
|
||||
* @return true if both mem locations of length are equal, false if they are not.
|
||||
*/
|
||||
non_null()
|
||||
bool crypto_sha512_eq(const uint8_t *cksum1, const uint8_t *cksum2);
|
||||
bool crypto_sha512_eq(const uint8_t cksum1[CRYPTO_SHA512_SIZE], const uint8_t cksum2[CRYPTO_SHA512_SIZE]);
|
||||
|
||||
/**
|
||||
* @brief Compare 2 SHA256 checksums of length CRYPTO_SHA256_SIZE, not vulnerable to
|
||||
* timing attacks.
|
||||
* timing attacks.
|
||||
*
|
||||
* @return true if both mem locations of length are equal, false if they are not.
|
||||
*/
|
||||
non_null()
|
||||
bool crypto_sha256_eq(const uint8_t *cksum1, const uint8_t *cksum2);
|
||||
bool crypto_sha256_eq(const uint8_t cksum1[CRYPTO_SHA256_SIZE], const uint8_t cksum2[CRYPTO_SHA256_SIZE]);
|
||||
|
||||
/**
|
||||
* @brief Return a random 8 bit integer.
|
||||
@ -240,10 +269,11 @@ uint64_t random_u64(const Random *rng);
|
||||
non_null()
|
||||
uint32_t random_range_u32(const Random *rng, uint32_t upper_bound);
|
||||
|
||||
/** @brief Cryptographically signs a message using the supplied secret key and puts the resulting signature
|
||||
* in the supplied buffer.
|
||||
/**
|
||||
* @brief Cryptographically signs a message using the supplied secret key and puts the resulting signature
|
||||
* in the supplied buffer.
|
||||
*
|
||||
* @param signature The buffer for the resulting signature, which must have room for at
|
||||
* @param[out] signature The buffer for the resulting signature, which must have room for at
|
||||
* least CRYPTO_SIGNATURE_SIZE bytes.
|
||||
* @param message The message being signed.
|
||||
* @param message_length The length in bytes of the message being signed.
|
||||
@ -253,8 +283,9 @@ uint32_t random_range_u32(const Random *rng, uint32_t upper_bound);
|
||||
* @retval true on success.
|
||||
*/
|
||||
non_null()
|
||||
bool crypto_signature_create(uint8_t *signature, const uint8_t *message, uint64_t message_length,
|
||||
const uint8_t *secret_key);
|
||||
bool crypto_signature_create(uint8_t signature[CRYPTO_SIGNATURE_SIZE],
|
||||
const uint8_t *message, uint64_t message_length,
|
||||
const uint8_t secret_key[SIG_SECRET_KEY_SIZE]);
|
||||
|
||||
/** @brief Verifies that the given signature was produced by a given message and public key.
|
||||
*
|
||||
@ -267,14 +298,15 @@ bool crypto_signature_create(uint8_t *signature, const uint8_t *message, uint64_
|
||||
* @retval true on success.
|
||||
*/
|
||||
non_null()
|
||||
bool crypto_signature_verify(const uint8_t *signature, const uint8_t *message, uint64_t message_length,
|
||||
const uint8_t *public_key);
|
||||
bool crypto_signature_verify(const uint8_t signature[CRYPTO_SIGNATURE_SIZE],
|
||||
const uint8_t *message, uint64_t message_length,
|
||||
const uint8_t public_key[SIG_PUBLIC_KEY_SIZE]);
|
||||
|
||||
/**
|
||||
* @brief Fill the given nonce with random bytes.
|
||||
*/
|
||||
non_null()
|
||||
void random_nonce(const Random *rng, uint8_t *nonce);
|
||||
void random_nonce(const Random *rng, uint8_t nonce[CRYPTO_NONCE_SIZE]);
|
||||
|
||||
/**
|
||||
* @brief Fill an array of bytes with random values.
|
||||
@ -290,25 +322,40 @@ void random_bytes(const Random *rng, uint8_t *bytes, size_t length);
|
||||
* @return false if it isn't, true if it is.
|
||||
*/
|
||||
non_null()
|
||||
bool public_key_valid(const uint8_t *public_key);
|
||||
bool public_key_valid(const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]);
|
||||
|
||||
/** @brief Creates an extended keypair: curve25519 and ed25519 for encryption and signing
|
||||
typedef struct Extended_Public_Key {
|
||||
uint8_t enc[CRYPTO_PUBLIC_KEY_SIZE];
|
||||
uint8_t sig[CRYPTO_SIGN_PUBLIC_KEY_SIZE];
|
||||
} Extended_Public_Key;
|
||||
|
||||
typedef struct Extended_Secret_Key {
|
||||
uint8_t enc[CRYPTO_SECRET_KEY_SIZE];
|
||||
uint8_t sig[CRYPTO_SIGN_SECRET_KEY_SIZE];
|
||||
} Extended_Secret_Key;
|
||||
|
||||
/**
|
||||
* @brief Creates an extended keypair: curve25519 and ed25519 for encryption and signing
|
||||
* respectively. The Encryption keys are derived from the signature keys.
|
||||
*
|
||||
* @param pk The buffer where the public key will be stored. Must have room for EXT_PUBLIC_KEY_SIZE bytes.
|
||||
* @param sk The buffer where the secret key will be stored. Must have room for EXT_SECRET_KEY_SIZE bytes.
|
||||
* NOTE: This does *not* use Random, so any code using this will not be fuzzable.
|
||||
* TODO: Make it use Random.
|
||||
*
|
||||
* @param[out] pk The buffer where the public key will be stored. Must have room for EXT_PUBLIC_KEY_SIZE bytes.
|
||||
* @param[out] sk The buffer where the secret key will be stored. Must have room for EXT_SECRET_KEY_SIZE bytes.
|
||||
* @param rng The random number generator to use for the key generator seed.
|
||||
*
|
||||
* @retval true on success.
|
||||
*/
|
||||
non_null()
|
||||
bool create_extended_keypair(uint8_t *pk, uint8_t *sk);
|
||||
bool create_extended_keypair(Extended_Public_Key *pk, Extended_Secret_Key *sk, const Random *rng);
|
||||
|
||||
/** Functions for groupchat extended keys */
|
||||
non_null() const uint8_t *get_enc_key(const uint8_t *key);
|
||||
non_null() const uint8_t *get_sig_pk(const uint8_t *key);
|
||||
non_null() void set_sig_pk(uint8_t *key, const uint8_t *sig_pk);
|
||||
non_null() const uint8_t *get_sig_sk(const uint8_t *key);
|
||||
non_null() const uint8_t *get_chat_id(const uint8_t *key);
|
||||
non_null() const uint8_t *get_enc_key(const Extended_Public_Key *key);
|
||||
non_null() const uint8_t *get_sig_pk(const Extended_Public_Key *key);
|
||||
non_null() void set_sig_pk(Extended_Public_Key *key, const uint8_t *sig_pk);
|
||||
non_null() const uint8_t *get_sig_sk(const Extended_Secret_Key *key);
|
||||
non_null() const uint8_t *get_chat_id(const Extended_Public_Key *key);
|
||||
|
||||
/**
|
||||
* @brief Generate a new random keypair.
|
||||
@ -316,13 +363,16 @@ non_null() const uint8_t *get_chat_id(const uint8_t *key);
|
||||
* Every call to this function is likely to generate a different keypair.
|
||||
*/
|
||||
non_null()
|
||||
int32_t crypto_new_keypair(const Random *rng, uint8_t *public_key, uint8_t *secret_key);
|
||||
int32_t crypto_new_keypair(const Random *rng,
|
||||
uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE],
|
||||
uint8_t secret_key[CRYPTO_SECRET_KEY_SIZE]);
|
||||
|
||||
/**
|
||||
* @brief Derive the public key from a given secret key.
|
||||
*/
|
||||
non_null()
|
||||
void crypto_derive_public_key(uint8_t *public_key, const uint8_t *secret_key);
|
||||
void crypto_derive_public_key(uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE],
|
||||
const uint8_t secret_key[CRYPTO_SECRET_KEY_SIZE]);
|
||||
|
||||
/**
|
||||
* @brief Encrypt message to send from secret key to public key.
|
||||
@ -336,8 +386,10 @@ void crypto_derive_public_key(uint8_t *public_key, const uint8_t *secret_key);
|
||||
* @return length of encrypted data if everything was fine.
|
||||
*/
|
||||
non_null()
|
||||
int32_t encrypt_data(const uint8_t *public_key, const uint8_t *secret_key, const uint8_t *nonce, const uint8_t *plain,
|
||||
size_t length, uint8_t *encrypted);
|
||||
int32_t encrypt_data(const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE],
|
||||
const uint8_t secret_key[CRYPTO_SECRET_KEY_SIZE],
|
||||
const uint8_t nonce[CRYPTO_NONCE_SIZE],
|
||||
const uint8_t *plain, size_t length, uint8_t *encrypted);
|
||||
|
||||
/**
|
||||
* @brief Decrypt message from public key to secret key.
|
||||
@ -351,7 +403,9 @@ int32_t encrypt_data(const uint8_t *public_key, const uint8_t *secret_key, const
|
||||
* @return length of plain text data if everything was fine.
|
||||
*/
|
||||
non_null()
|
||||
int32_t decrypt_data(const uint8_t *public_key, const uint8_t *secret_key, const uint8_t *nonce,
|
||||
int32_t decrypt_data(const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE],
|
||||
const uint8_t secret_key[CRYPTO_SECRET_KEY_SIZE],
|
||||
const uint8_t nonce[CRYPTO_NONCE_SIZE],
|
||||
const uint8_t *encrypted, size_t length, uint8_t *plain);
|
||||
|
||||
/**
|
||||
@ -362,21 +416,24 @@ int32_t decrypt_data(const uint8_t *public_key, const uint8_t *secret_key, const
|
||||
* encrypt/decrypt.
|
||||
*/
|
||||
non_null()
|
||||
int32_t encrypt_precompute(const uint8_t *public_key, const uint8_t *secret_key, uint8_t *shared_key);
|
||||
int32_t encrypt_precompute(const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE],
|
||||
const uint8_t secret_key[CRYPTO_SECRET_KEY_SIZE],
|
||||
uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE]);
|
||||
|
||||
/**
|
||||
* @brief Encrypt message with precomputed shared key.
|
||||
*
|
||||
* Encrypts plain of length length to encrypted of length + @ref CRYPTO_MAC_SIZE
|
||||
* using a shared key @ref CRYPTO_SYMMETRIC_KEY_SIZE big and a @ref CRYPTO_NONCE_SIZE
|
||||
* using a shared key @ref CRYPTO_SHARED_KEY_SIZE big and a @ref CRYPTO_NONCE_SIZE
|
||||
* byte nonce.
|
||||
*
|
||||
* @retval -1 if there was a problem.
|
||||
* @return length of encrypted data if everything was fine.
|
||||
*/
|
||||
non_null()
|
||||
int32_t encrypt_data_symmetric(const uint8_t *shared_key, const uint8_t *nonce, const uint8_t *plain, size_t length,
|
||||
uint8_t *encrypted);
|
||||
int32_t encrypt_data_symmetric(const uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE],
|
||||
const uint8_t nonce[CRYPTO_NONCE_SIZE],
|
||||
const uint8_t *plain, size_t length, uint8_t *encrypted);
|
||||
|
||||
/**
|
||||
* @brief Decrypt message with precomputed shared key.
|
||||
@ -389,15 +446,15 @@ int32_t encrypt_data_symmetric(const uint8_t *shared_key, const uint8_t *nonce,
|
||||
* @return length of plain data if everything was fine.
|
||||
*/
|
||||
non_null()
|
||||
int32_t decrypt_data_symmetric(const uint8_t *shared_key, const uint8_t *nonce, const uint8_t *encrypted, size_t length,
|
||||
uint8_t *plain);
|
||||
int32_t decrypt_data_symmetric(const uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE],
|
||||
const uint8_t nonce[CRYPTO_NONCE_SIZE],
|
||||
const uint8_t *encrypted, size_t length, uint8_t *plain);
|
||||
|
||||
/**
|
||||
* @brief Increment the given nonce by 1 in big endian (rightmost byte incremented
|
||||
* first).
|
||||
* @brief Increment the given nonce by 1 in big endian (rightmost byte incremented first).
|
||||
*/
|
||||
non_null()
|
||||
void increment_nonce(uint8_t *nonce);
|
||||
void increment_nonce(uint8_t nonce[CRYPTO_NONCE_SIZE]);
|
||||
|
||||
/**
|
||||
* @brief Increment the given nonce by a given number.
|
||||
@ -405,13 +462,13 @@ void increment_nonce(uint8_t *nonce);
|
||||
* The number should be in host byte order.
|
||||
*/
|
||||
non_null()
|
||||
void increment_nonce_number(uint8_t *nonce, uint32_t increment);
|
||||
void increment_nonce_number(uint8_t nonce[CRYPTO_NONCE_SIZE], uint32_t increment);
|
||||
|
||||
/**
|
||||
* @brief Fill a key @ref CRYPTO_SYMMETRIC_KEY_SIZE big with random bytes.
|
||||
*/
|
||||
non_null()
|
||||
void new_symmetric_key(const Random *rng, uint8_t *key);
|
||||
void new_symmetric_key(const Random *rng, uint8_t key[CRYPTO_SYMMETRIC_KEY_SIZE]);
|
||||
|
||||
/**
|
||||
* @brief Locks `length` bytes of memory pointed to by `data`.
|
||||
@ -445,7 +502,7 @@ non_null()
|
||||
void new_hmac_key(const Random *rng, uint8_t key[CRYPTO_HMAC_KEY_SIZE]);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif // C_TOXCORE_TOXCORE_CRYPTO_CORE_H
|
||||
#endif /* C_TOXCORE_TOXCORE_CRYPTO_CORE_H */
|
||||
|
66
external/toxcore/c-toxcore/toxcore/crypto_core_pack.c
vendored
Normal file
66
external/toxcore/c-toxcore/toxcore/crypto_core_pack.c
vendored
Normal file
@ -0,0 +1,66 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
* Copyright © 2016-2024 The TokTok team.
|
||||
* Copyright © 2013 Tox project.
|
||||
*/
|
||||
|
||||
#include "crypto_core_pack.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "bin_pack.h"
|
||||
#include "bin_unpack.h"
|
||||
#include "ccompat.h"
|
||||
#include "crypto_core.h"
|
||||
|
||||
bool pack_extended_public_key(const Extended_Public_Key *key, Bin_Pack *bp)
|
||||
{
|
||||
uint8_t ext_key[EXT_PUBLIC_KEY_SIZE];
|
||||
static_assert(sizeof(ext_key) == sizeof(key->enc) + sizeof(key->sig),
|
||||
"extended secret key size is not the sum of the encryption and sign secret key sizes");
|
||||
memcpy(ext_key, key->enc, sizeof(key->enc));
|
||||
memcpy(&ext_key[sizeof(key->enc)], key->sig, sizeof(key->sig));
|
||||
|
||||
return bin_pack_bin(bp, ext_key, sizeof(ext_key));
|
||||
}
|
||||
|
||||
bool pack_extended_secret_key(const Extended_Secret_Key *key, Bin_Pack *bp)
|
||||
{
|
||||
uint8_t ext_key[EXT_SECRET_KEY_SIZE];
|
||||
static_assert(sizeof(ext_key) == sizeof(key->enc) + sizeof(key->sig),
|
||||
"extended secret key size is not the sum of the encryption and sign secret key sizes");
|
||||
memcpy(ext_key, key->enc, sizeof(key->enc));
|
||||
memcpy(&ext_key[sizeof(key->enc)], key->sig, sizeof(key->sig));
|
||||
|
||||
const bool result = bin_pack_bin(bp, ext_key, sizeof(ext_key));
|
||||
crypto_memzero(ext_key, sizeof(ext_key));
|
||||
return result;
|
||||
}
|
||||
|
||||
bool unpack_extended_public_key(Extended_Public_Key *key, Bin_Unpack *bu)
|
||||
{
|
||||
uint8_t ext_key[EXT_PUBLIC_KEY_SIZE];
|
||||
|
||||
if (!bin_unpack_bin_fixed(bu, ext_key, sizeof(ext_key))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
memcpy(key->enc, ext_key, sizeof(key->enc));
|
||||
memcpy(key->sig, &ext_key[sizeof(key->enc)], sizeof(key->sig));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool unpack_extended_secret_key(Extended_Secret_Key *key, Bin_Unpack *bu)
|
||||
{
|
||||
uint8_t ext_key[EXT_SECRET_KEY_SIZE];
|
||||
|
||||
if (!bin_unpack_bin_fixed(bu, ext_key, sizeof(ext_key))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
memcpy(key->enc, ext_key, sizeof(key->enc));
|
||||
memcpy(key->sig, &ext_key[sizeof(key->enc)], sizeof(key->sig));
|
||||
crypto_memzero(ext_key, sizeof(ext_key));
|
||||
|
||||
return true;
|
||||
}
|
31
external/toxcore/c-toxcore/toxcore/crypto_core_pack.h
vendored
Normal file
31
external/toxcore/c-toxcore/toxcore/crypto_core_pack.h
vendored
Normal file
@ -0,0 +1,31 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
* Copyright © 2016-2024 The TokTok team.
|
||||
* Copyright © 2013 Tox project.
|
||||
*/
|
||||
|
||||
#ifndef C_TOXCORE_TOXCORE_CRYPTO_CORE_PACK_H
|
||||
#define C_TOXCORE_TOXCORE_CRYPTO_CORE_PACK_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "attributes.h"
|
||||
#include "bin_pack.h"
|
||||
#include "bin_unpack.h"
|
||||
#include "crypto_core.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
non_null() bool pack_extended_public_key(const Extended_Public_Key *key, Bin_Pack *bp);
|
||||
non_null() bool pack_extended_secret_key(const Extended_Secret_Key *key, Bin_Pack *bp);
|
||||
non_null() bool unpack_extended_public_key(Extended_Public_Key *key, Bin_Unpack *bu);
|
||||
non_null() bool unpack_extended_secret_key(Extended_Secret_Key *key, Bin_Unpack *bu);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif /* C_TOXCORE_TOXCORE_CRYPTO_CORE_PACK_H */
|
@ -14,8 +14,6 @@ namespace {
|
||||
using HmacKey = std::array<uint8_t, CRYPTO_HMAC_KEY_SIZE>;
|
||||
using Hmac = std::array<uint8_t, CRYPTO_HMAC_SIZE>;
|
||||
using SecretKey = std::array<uint8_t, CRYPTO_SECRET_KEY_SIZE>;
|
||||
using ExtPublicKey = std::array<uint8_t, EXT_PUBLIC_KEY_SIZE>;
|
||||
using ExtSecretKey = std::array<uint8_t, EXT_SECRET_KEY_SIZE>;
|
||||
using Signature = std::array<uint8_t, CRYPTO_SIGNATURE_SIZE>;
|
||||
using Nonce = std::array<uint8_t, CRYPTO_NONCE_SIZE>;
|
||||
|
||||
@ -72,10 +70,10 @@ TEST(CryptoCore, Signatures)
|
||||
{
|
||||
Test_Random rng;
|
||||
|
||||
ExtPublicKey pk;
|
||||
ExtSecretKey sk;
|
||||
Extended_Public_Key pk;
|
||||
Extended_Secret_Key sk;
|
||||
|
||||
EXPECT_TRUE(create_extended_keypair(pk.data(), sk.data()));
|
||||
EXPECT_TRUE(create_extended_keypair(&pk, &sk, rng));
|
||||
|
||||
std::vector<uint8_t> message{0};
|
||||
message.clear();
|
||||
@ -84,9 +82,9 @@ TEST(CryptoCore, Signatures)
|
||||
for (uint8_t i = 0; i < 100; ++i) {
|
||||
Signature signature;
|
||||
EXPECT_TRUE(crypto_signature_create(
|
||||
signature.data(), message.data(), message.size(), get_sig_sk(sk.data())));
|
||||
signature.data(), message.data(), message.size(), get_sig_sk(&sk)));
|
||||
EXPECT_TRUE(crypto_signature_verify(
|
||||
signature.data(), message.data(), message.size(), get_sig_pk(pk.data())));
|
||||
signature.data(), message.data(), message.size(), get_sig_pk(&pk)));
|
||||
|
||||
message.push_back(random_u08(rng));
|
||||
}
|
||||
|
@ -3,6 +3,9 @@
|
||||
#include <cstring>
|
||||
#include <iomanip>
|
||||
|
||||
#include "crypto_core.h"
|
||||
#include "test_util.hh"
|
||||
|
||||
Random_Funcs const Random_Class::vtable = {
|
||||
Method<crypto_random_bytes_cb, Random_Class>::invoke<&Random_Class::random_bytes>,
|
||||
Method<crypto_random_uniform_cb, Random_Class>::invoke<&Random_Class::random_uniform>,
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -13,14 +14,12 @@
|
||||
#include "../tox.h"
|
||||
#include "../tox_events.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_Conference_Connected {
|
||||
uint32_t conference_number;
|
||||
};
|
||||
@ -65,7 +64,6 @@ static bool tox_event_conference_connected_unpack_into(
|
||||
return bin_unpack_u32(bu, &event->conference_number);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -119,6 +117,7 @@ bool tox_event_conference_connected_unpack(
|
||||
Tox_Event_Conference_Connected **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_conference_connected_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -148,16 +147,15 @@ static Tox_Event_Conference_Connected *tox_event_conference_connected_alloc(void
|
||||
return conference_connected;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_conference_connected(Tox *tox, uint32_t conference_number,
|
||||
void *user_data)
|
||||
void tox_events_handle_conference_connected(
|
||||
Tox *tox, uint32_t conference_number,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_Conference_Connected *conference_connected = tox_event_conference_connected_alloc(user_data);
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -17,14 +18,12 @@
|
||||
#include "../tox_pack.h"
|
||||
#include "../tox_unpack.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_Conference_Invite {
|
||||
uint32_t friend_number;
|
||||
Tox_Conference_Type type;
|
||||
@ -58,7 +57,7 @@ Tox_Conference_Type tox_event_conference_invite_get_type(const Tox_Event_Confere
|
||||
return conference_invite->type;
|
||||
}
|
||||
|
||||
non_null()
|
||||
non_null(1) nullable(2)
|
||||
static bool tox_event_conference_invite_set_cookie(Tox_Event_Conference_Invite *conference_invite,
|
||||
const uint8_t *cookie, uint32_t cookie_length)
|
||||
{
|
||||
@ -70,6 +69,11 @@ static bool tox_event_conference_invite_set_cookie(Tox_Event_Conference_Invite *
|
||||
conference_invite->cookie_length = 0;
|
||||
}
|
||||
|
||||
if (cookie == nullptr) {
|
||||
assert(cookie_length == 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t *cookie_copy = (uint8_t *)malloc(cookie_length);
|
||||
|
||||
if (cookie_copy == nullptr) {
|
||||
@ -128,7 +132,6 @@ static bool tox_event_conference_invite_unpack_into(
|
||||
&& bin_unpack_bin(bu, &event->cookie, &event->cookie_length);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -182,6 +185,7 @@ bool tox_event_conference_invite_unpack(
|
||||
Tox_Event_Conference_Invite **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_conference_invite_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -211,16 +215,15 @@ static Tox_Event_Conference_Invite *tox_event_conference_invite_alloc(void *user
|
||||
return conference_invite;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_conference_invite(Tox *tox, uint32_t friend_number, Tox_Conference_Type type, const uint8_t *cookie, size_t length,
|
||||
void *user_data)
|
||||
void tox_events_handle_conference_invite(
|
||||
Tox *tox, uint32_t friend_number, Tox_Conference_Type type, const uint8_t *cookie, size_t length,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_Conference_Invite *conference_invite = tox_event_conference_invite_alloc(user_data);
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -17,14 +18,12 @@
|
||||
#include "../tox_pack.h"
|
||||
#include "../tox_unpack.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_Conference_Message {
|
||||
uint32_t conference_number;
|
||||
uint32_t peer_number;
|
||||
@ -72,7 +71,7 @@ Tox_Message_Type tox_event_conference_message_get_type(const Tox_Event_Conferenc
|
||||
return conference_message->type;
|
||||
}
|
||||
|
||||
non_null()
|
||||
non_null(1) nullable(2)
|
||||
static bool tox_event_conference_message_set_message(Tox_Event_Conference_Message *conference_message,
|
||||
const uint8_t *message, uint32_t message_length)
|
||||
{
|
||||
@ -84,6 +83,11 @@ static bool tox_event_conference_message_set_message(Tox_Event_Conference_Messag
|
||||
conference_message->message_length = 0;
|
||||
}
|
||||
|
||||
if (message == nullptr) {
|
||||
assert(message_length == 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t *message_copy = (uint8_t *)malloc(message_length);
|
||||
|
||||
if (message_copy == nullptr) {
|
||||
@ -144,7 +148,6 @@ static bool tox_event_conference_message_unpack_into(
|
||||
&& bin_unpack_bin(bu, &event->message, &event->message_length);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -198,6 +201,7 @@ bool tox_event_conference_message_unpack(
|
||||
Tox_Event_Conference_Message **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_conference_message_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -227,16 +231,15 @@ static Tox_Event_Conference_Message *tox_event_conference_message_alloc(void *us
|
||||
return conference_message;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_conference_message(Tox *tox, uint32_t conference_number, uint32_t peer_number, Tox_Message_Type type, const uint8_t *message, size_t length,
|
||||
void *user_data)
|
||||
void tox_events_handle_conference_message(
|
||||
Tox *tox, uint32_t conference_number, uint32_t peer_number, Tox_Message_Type type, const uint8_t *message, size_t length,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_Conference_Message *conference_message = tox_event_conference_message_alloc(user_data);
|
||||
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -13,14 +14,12 @@
|
||||
#include "../tox.h"
|
||||
#include "../tox_events.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_Conference_Peer_List_Changed {
|
||||
uint32_t conference_number;
|
||||
};
|
||||
@ -65,7 +64,6 @@ static bool tox_event_conference_peer_list_changed_unpack_into(
|
||||
return bin_unpack_u32(bu, &event->conference_number);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -119,6 +117,7 @@ bool tox_event_conference_peer_list_changed_unpack(
|
||||
Tox_Event_Conference_Peer_List_Changed **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_conference_peer_list_changed_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -148,16 +147,15 @@ static Tox_Event_Conference_Peer_List_Changed *tox_event_conference_peer_list_ch
|
||||
return conference_peer_list_changed;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_conference_peer_list_changed(Tox *tox, uint32_t conference_number,
|
||||
void *user_data)
|
||||
void tox_events_handle_conference_peer_list_changed(
|
||||
Tox *tox, uint32_t conference_number,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed = tox_event_conference_peer_list_changed_alloc(user_data);
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -15,14 +16,12 @@
|
||||
#include "../tox.h"
|
||||
#include "../tox_events.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_Conference_Peer_Name {
|
||||
uint32_t conference_number;
|
||||
uint32_t peer_number;
|
||||
@ -56,7 +55,7 @@ uint32_t tox_event_conference_peer_name_get_peer_number(const Tox_Event_Conferen
|
||||
return conference_peer_name->peer_number;
|
||||
}
|
||||
|
||||
non_null()
|
||||
non_null(1) nullable(2)
|
||||
static bool tox_event_conference_peer_name_set_name(Tox_Event_Conference_Peer_Name *conference_peer_name,
|
||||
const uint8_t *name, uint32_t name_length)
|
||||
{
|
||||
@ -68,6 +67,11 @@ static bool tox_event_conference_peer_name_set_name(Tox_Event_Conference_Peer_Na
|
||||
conference_peer_name->name_length = 0;
|
||||
}
|
||||
|
||||
if (name == nullptr) {
|
||||
assert(name_length == 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t *name_copy = (uint8_t *)malloc(name_length);
|
||||
|
||||
if (name_copy == nullptr) {
|
||||
@ -126,7 +130,6 @@ static bool tox_event_conference_peer_name_unpack_into(
|
||||
&& bin_unpack_bin(bu, &event->name, &event->name_length);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -180,6 +183,7 @@ bool tox_event_conference_peer_name_unpack(
|
||||
Tox_Event_Conference_Peer_Name **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_conference_peer_name_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -209,16 +213,15 @@ static Tox_Event_Conference_Peer_Name *tox_event_conference_peer_name_alloc(void
|
||||
return conference_peer_name;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_conference_peer_name(Tox *tox, uint32_t conference_number, uint32_t peer_number, const uint8_t *name, size_t length,
|
||||
void *user_data)
|
||||
void tox_events_handle_conference_peer_name(
|
||||
Tox *tox, uint32_t conference_number, uint32_t peer_number, const uint8_t *name, size_t length,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_Conference_Peer_Name *conference_peer_name = tox_event_conference_peer_name_alloc(user_data);
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -15,14 +16,12 @@
|
||||
#include "../tox.h"
|
||||
#include "../tox_events.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_Conference_Title {
|
||||
uint32_t conference_number;
|
||||
uint32_t peer_number;
|
||||
@ -56,7 +55,7 @@ uint32_t tox_event_conference_title_get_peer_number(const Tox_Event_Conference_T
|
||||
return conference_title->peer_number;
|
||||
}
|
||||
|
||||
non_null()
|
||||
non_null(1) nullable(2)
|
||||
static bool tox_event_conference_title_set_title(Tox_Event_Conference_Title *conference_title,
|
||||
const uint8_t *title, uint32_t title_length)
|
||||
{
|
||||
@ -68,6 +67,11 @@ static bool tox_event_conference_title_set_title(Tox_Event_Conference_Title *con
|
||||
conference_title->title_length = 0;
|
||||
}
|
||||
|
||||
if (title == nullptr) {
|
||||
assert(title_length == 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t *title_copy = (uint8_t *)malloc(title_length);
|
||||
|
||||
if (title_copy == nullptr) {
|
||||
@ -126,7 +130,6 @@ static bool tox_event_conference_title_unpack_into(
|
||||
&& bin_unpack_bin(bu, &event->title, &event->title_length);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -180,6 +183,7 @@ bool tox_event_conference_title_unpack(
|
||||
Tox_Event_Conference_Title **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_conference_title_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -209,16 +213,15 @@ static Tox_Event_Conference_Title *tox_event_conference_title_alloc(void *user_d
|
||||
return conference_title;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_conference_title(Tox *tox, uint32_t conference_number, uint32_t peer_number, const uint8_t *title, size_t length,
|
||||
void *user_data)
|
||||
void tox_events_handle_conference_title(
|
||||
Tox *tox, uint32_t conference_number, uint32_t peer_number, const uint8_t *title, size_t length,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_Conference_Title *conference_title = tox_event_conference_title_alloc(user_data);
|
||||
|
||||
|
223
external/toxcore/c-toxcore/toxcore/events/dht_get_nodes_response.c
vendored
Normal file
223
external/toxcore/c-toxcore/toxcore/events/dht_get_nodes_response.c
vendored
Normal file
@ -0,0 +1,223 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
* Copyright © 2022 The TokTok team.
|
||||
*/
|
||||
|
||||
#include "events_alloc.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
#include "../mem.h"
|
||||
#include "../tox.h"
|
||||
#include "../tox_events.h"
|
||||
#include "../tox_private.h"
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
struct Tox_Event_Dht_Get_Nodes_Response {
|
||||
uint8_t public_key[TOX_PUBLIC_KEY_SIZE];
|
||||
uint8_t *ip;
|
||||
uint32_t ip_length;
|
||||
uint16_t port;
|
||||
};
|
||||
|
||||
non_null()
|
||||
static bool tox_event_dht_get_nodes_response_set_public_key(Tox_Event_Dht_Get_Nodes_Response *dht_get_nodes_response, const uint8_t public_key[TOX_PUBLIC_KEY_SIZE])
|
||||
{
|
||||
memcpy(dht_get_nodes_response->public_key, public_key, TOX_PUBLIC_KEY_SIZE);
|
||||
return true;
|
||||
}
|
||||
const uint8_t *tox_event_dht_get_nodes_response_get_public_key(const Tox_Event_Dht_Get_Nodes_Response *dht_get_nodes_response)
|
||||
{
|
||||
return dht_get_nodes_response->public_key;
|
||||
}
|
||||
|
||||
non_null()
|
||||
static bool tox_event_dht_get_nodes_response_set_ip(Tox_Event_Dht_Get_Nodes_Response *dht_get_nodes_response,
|
||||
const char *ip, uint32_t ip_length, const Memory *mem)
|
||||
{
|
||||
if (dht_get_nodes_response->ip != nullptr) {
|
||||
mem_delete(mem, dht_get_nodes_response->ip);
|
||||
dht_get_nodes_response->ip = nullptr;
|
||||
dht_get_nodes_response->ip_length = 0;
|
||||
}
|
||||
|
||||
uint8_t *ip_tmp = (uint8_t *)mem_balloc(mem, ip_length);
|
||||
|
||||
if (ip_tmp == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
memcpy(ip_tmp, ip, ip_length);
|
||||
dht_get_nodes_response->ip = ip_tmp;
|
||||
dht_get_nodes_response->ip_length = ip_length;
|
||||
return true;
|
||||
}
|
||||
uint32_t tox_event_dht_get_nodes_response_get_ip_length(const Tox_Event_Dht_Get_Nodes_Response *dht_get_nodes_response)
|
||||
{
|
||||
return dht_get_nodes_response->ip_length;
|
||||
}
|
||||
const uint8_t *tox_event_dht_get_nodes_response_get_ip(const Tox_Event_Dht_Get_Nodes_Response *dht_get_nodes_response)
|
||||
{
|
||||
return dht_get_nodes_response->ip;
|
||||
}
|
||||
|
||||
non_null()
|
||||
static bool tox_event_dht_get_nodes_response_set_port(Tox_Event_Dht_Get_Nodes_Response *dht_get_nodes_response, uint16_t port)
|
||||
{
|
||||
dht_get_nodes_response->port = port;
|
||||
return true;
|
||||
}
|
||||
uint16_t tox_event_dht_get_nodes_response_get_port(const Tox_Event_Dht_Get_Nodes_Response *dht_get_nodes_response)
|
||||
{
|
||||
return dht_get_nodes_response->port;
|
||||
}
|
||||
|
||||
non_null()
|
||||
static void tox_event_dht_get_nodes_response_construct(Tox_Event_Dht_Get_Nodes_Response *dht_get_nodes_response)
|
||||
{
|
||||
*dht_get_nodes_response = (Tox_Event_Dht_Get_Nodes_Response) {
|
||||
{
|
||||
0
|
||||
}
|
||||
};
|
||||
}
|
||||
non_null()
|
||||
static void tox_event_dht_get_nodes_response_destruct(Tox_Event_Dht_Get_Nodes_Response *dht_get_nodes_response, const Memory *mem)
|
||||
{
|
||||
mem_delete(mem, dht_get_nodes_response->ip);
|
||||
}
|
||||
|
||||
bool tox_event_dht_get_nodes_response_pack(
|
||||
const Tox_Event_Dht_Get_Nodes_Response *event, Bin_Pack *bp)
|
||||
{
|
||||
return bin_pack_array(bp, 3)
|
||||
&& bin_pack_bin(bp, event->public_key, TOX_PUBLIC_KEY_SIZE)
|
||||
&& bin_pack_bin(bp, event->ip, event->ip_length)
|
||||
&& bin_pack_u16(bp, event->port);
|
||||
}
|
||||
|
||||
non_null()
|
||||
static bool tox_event_dht_get_nodes_response_unpack_into(
|
||||
Tox_Event_Dht_Get_Nodes_Response *event, Bin_Unpack *bu)
|
||||
{
|
||||
if (!bin_unpack_array_fixed(bu, 3, nullptr)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return bin_unpack_bin_fixed(bu, event->public_key, TOX_PUBLIC_KEY_SIZE)
|
||||
&& bin_unpack_bin(bu, &event->ip, &event->ip_length)
|
||||
&& bin_unpack_u16(bu, &event->port);
|
||||
}
|
||||
|
||||
const Tox_Event_Dht_Get_Nodes_Response *tox_event_get_dht_get_nodes_response(
|
||||
const Tox_Event *event)
|
||||
{
|
||||
return event->type == TOX_EVENT_DHT_GET_NODES_RESPONSE ? event->data.dht_get_nodes_response : nullptr;
|
||||
}
|
||||
|
||||
Tox_Event_Dht_Get_Nodes_Response *tox_event_dht_get_nodes_response_new(const Memory *mem)
|
||||
{
|
||||
Tox_Event_Dht_Get_Nodes_Response *const dht_get_nodes_response =
|
||||
(Tox_Event_Dht_Get_Nodes_Response *)mem_alloc(mem, sizeof(Tox_Event_Dht_Get_Nodes_Response));
|
||||
|
||||
if (dht_get_nodes_response == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
tox_event_dht_get_nodes_response_construct(dht_get_nodes_response);
|
||||
return dht_get_nodes_response;
|
||||
}
|
||||
|
||||
void tox_event_dht_get_nodes_response_free(Tox_Event_Dht_Get_Nodes_Response *dht_get_nodes_response, const Memory *mem)
|
||||
{
|
||||
if (dht_get_nodes_response != nullptr) {
|
||||
tox_event_dht_get_nodes_response_destruct(dht_get_nodes_response, mem);
|
||||
}
|
||||
mem_delete(mem, dht_get_nodes_response);
|
||||
}
|
||||
|
||||
non_null()
|
||||
static Tox_Event_Dht_Get_Nodes_Response *tox_events_add_dht_get_nodes_response(Tox_Events *events, const Memory *mem)
|
||||
{
|
||||
Tox_Event_Dht_Get_Nodes_Response *const dht_get_nodes_response = tox_event_dht_get_nodes_response_new(mem);
|
||||
|
||||
if (dht_get_nodes_response == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Tox_Event event;
|
||||
event.type = TOX_EVENT_DHT_GET_NODES_RESPONSE;
|
||||
event.data.dht_get_nodes_response = dht_get_nodes_response;
|
||||
|
||||
tox_events_add(events, &event);
|
||||
return dht_get_nodes_response;
|
||||
}
|
||||
|
||||
bool tox_event_dht_get_nodes_response_unpack(
|
||||
Tox_Event_Dht_Get_Nodes_Response **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
*event = tox_event_dht_get_nodes_response_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return tox_event_dht_get_nodes_response_unpack_into(*event, bu);
|
||||
}
|
||||
|
||||
non_null()
|
||||
static Tox_Event_Dht_Get_Nodes_Response *tox_event_dht_get_nodes_response_alloc(void *user_data)
|
||||
{
|
||||
Tox_Events_State *state = tox_events_alloc(user_data);
|
||||
assert(state != nullptr);
|
||||
|
||||
if (state->events == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Tox_Event_Dht_Get_Nodes_Response *dht_get_nodes_response = tox_events_add_dht_get_nodes_response(state->events, state->mem);
|
||||
|
||||
if (dht_get_nodes_response == nullptr) {
|
||||
state->error = TOX_ERR_EVENTS_ITERATE_MALLOC;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return dht_get_nodes_response;
|
||||
}
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
void tox_events_handle_dht_get_nodes_response(
|
||||
Tox *tox, const uint8_t public_key[TOX_PUBLIC_KEY_SIZE],
|
||||
const char *ip, uint16_t port, void *user_data)
|
||||
{
|
||||
Tox_Event_Dht_Get_Nodes_Response *dht_get_nodes_response = tox_event_dht_get_nodes_response_alloc(user_data);
|
||||
|
||||
if (dht_get_nodes_response == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
const size_t ip_length = strlen(ip);
|
||||
if (ip_length >= UINT32_MAX) {
|
||||
return;
|
||||
}
|
||||
|
||||
const Tox_System *sys = tox_get_system(tox);
|
||||
|
||||
tox_event_dht_get_nodes_response_set_public_key(dht_get_nodes_response, public_key);
|
||||
tox_event_dht_get_nodes_response_set_ip(dht_get_nodes_response, ip, ip_length + 1, sys->mem);
|
||||
tox_event_dht_get_nodes_response_set_port(dht_get_nodes_response, port);
|
||||
}
|
@ -63,7 +63,7 @@ bool tox_events_add(Tox_Events *events, const Tox_Event *event)
|
||||
if (events->events_size == events->events_capacity) {
|
||||
const uint32_t new_events_capacity = events->events_capacity * 2 + 1;
|
||||
Tox_Event *new_events = (Tox_Event *)mem_vrealloc(
|
||||
events->mem, events->events, new_events_capacity, sizeof(Tox_Event));
|
||||
events->mem, events->events, new_events_capacity, sizeof(Tox_Event));
|
||||
|
||||
if (new_events == nullptr) {
|
||||
return false;
|
||||
|
@ -8,7 +8,11 @@
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../mem.h"
|
||||
#include "../tox.h"
|
||||
#include "../tox_event.h"
|
||||
#include "../tox_events.h"
|
||||
#include "../tox_private.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@ -34,6 +38,7 @@ tox_conference_message_cb tox_events_handle_conference_message;
|
||||
tox_conference_peer_list_changed_cb tox_events_handle_conference_peer_list_changed;
|
||||
tox_conference_peer_name_cb tox_events_handle_conference_peer_name;
|
||||
tox_conference_title_cb tox_events_handle_conference_title;
|
||||
tox_dht_get_nodes_response_cb tox_events_handle_dht_get_nodes_response;
|
||||
tox_file_chunk_request_cb tox_events_handle_file_chunk_request;
|
||||
tox_file_recv_cb tox_events_handle_file_recv;
|
||||
tox_file_recv_chunk_cb tox_events_handle_file_recv_chunk;
|
||||
@ -68,12 +73,6 @@ tox_group_self_join_cb tox_events_handle_group_self_join;
|
||||
tox_group_join_fail_cb tox_events_handle_group_join_fail;
|
||||
tox_group_moderation_cb tox_events_handle_group_moderation;
|
||||
|
||||
non_null(2) nullable(1)
|
||||
bool tox_events_pack(const Tox_Events *events, Bin_Pack *bp);
|
||||
|
||||
non_null()
|
||||
bool tox_events_unpack(Tox_Events *events, Bin_Unpack *bu, const Memory *mem);
|
||||
|
||||
non_null()
|
||||
Tox_Events_State *tox_events_alloc(void *user_data);
|
||||
|
||||
@ -81,7 +80,7 @@ non_null()
|
||||
bool tox_events_add(Tox_Events *events, const Tox_Event *event);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif // C_TOXCORE_TOXCORE_EVENTS_EVENTS_ALLOC_H
|
||||
#endif /* C_TOXCORE_TOXCORE_EVENTS_EVENTS_ALLOC_H */
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -13,14 +14,12 @@
|
||||
#include "../tox.h"
|
||||
#include "../tox_events.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_File_Chunk_Request {
|
||||
uint32_t friend_number;
|
||||
uint32_t file_number;
|
||||
@ -118,7 +117,6 @@ static bool tox_event_file_chunk_request_unpack_into(
|
||||
&& bin_unpack_u16(bu, &event->length);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -172,6 +170,7 @@ bool tox_event_file_chunk_request_unpack(
|
||||
Tox_Event_File_Chunk_Request **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_file_chunk_request_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -201,16 +200,15 @@ static Tox_Event_File_Chunk_Request *tox_event_file_chunk_request_alloc(void *us
|
||||
return file_chunk_request;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_file_chunk_request(Tox *tox, uint32_t friend_number, uint32_t file_number, uint64_t position, size_t length,
|
||||
void *user_data)
|
||||
void tox_events_handle_file_chunk_request(
|
||||
Tox *tox, uint32_t friend_number, uint32_t file_number, uint64_t position, size_t length,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_File_Chunk_Request *file_chunk_request = tox_event_file_chunk_request_alloc(user_data);
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -15,14 +16,12 @@
|
||||
#include "../tox.h"
|
||||
#include "../tox_events.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_File_Recv {
|
||||
uint32_t friend_number;
|
||||
uint32_t file_number;
|
||||
@ -84,7 +83,7 @@ uint64_t tox_event_file_recv_get_file_size(const Tox_Event_File_Recv *file_recv)
|
||||
return file_recv->file_size;
|
||||
}
|
||||
|
||||
non_null()
|
||||
non_null(1) nullable(2)
|
||||
static bool tox_event_file_recv_set_filename(Tox_Event_File_Recv *file_recv,
|
||||
const uint8_t *filename, uint32_t filename_length)
|
||||
{
|
||||
@ -96,6 +95,11 @@ static bool tox_event_file_recv_set_filename(Tox_Event_File_Recv *file_recv,
|
||||
file_recv->filename_length = 0;
|
||||
}
|
||||
|
||||
if (filename == nullptr) {
|
||||
assert(filename_length == 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t *filename_copy = (uint8_t *)malloc(filename_length);
|
||||
|
||||
if (filename_copy == nullptr) {
|
||||
@ -158,7 +162,6 @@ static bool tox_event_file_recv_unpack_into(
|
||||
&& bin_unpack_bin(bu, &event->filename, &event->filename_length);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -212,6 +215,7 @@ bool tox_event_file_recv_unpack(
|
||||
Tox_Event_File_Recv **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_file_recv_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -241,16 +245,15 @@ static Tox_Event_File_Recv *tox_event_file_recv_alloc(void *user_data)
|
||||
return file_recv;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_file_recv(Tox *tox, uint32_t friend_number, uint32_t file_number, uint32_t kind, uint64_t file_size, const uint8_t *filename, size_t filename_length,
|
||||
void *user_data)
|
||||
void tox_events_handle_file_recv(
|
||||
Tox *tox, uint32_t friend_number, uint32_t file_number, uint32_t kind, uint64_t file_size, const uint8_t *filename, size_t filename_length,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_File_Recv *file_recv = tox_event_file_recv_alloc(user_data);
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -15,14 +16,12 @@
|
||||
#include "../tox.h"
|
||||
#include "../tox_events.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_File_Recv_Chunk {
|
||||
uint32_t friend_number;
|
||||
uint32_t file_number;
|
||||
@ -70,7 +69,7 @@ uint64_t tox_event_file_recv_chunk_get_position(const Tox_Event_File_Recv_Chunk
|
||||
return file_recv_chunk->position;
|
||||
}
|
||||
|
||||
non_null()
|
||||
non_null(1) nullable(2)
|
||||
static bool tox_event_file_recv_chunk_set_data(Tox_Event_File_Recv_Chunk *file_recv_chunk,
|
||||
const uint8_t *data, uint32_t data_length)
|
||||
{
|
||||
@ -82,6 +81,11 @@ static bool tox_event_file_recv_chunk_set_data(Tox_Event_File_Recv_Chunk *file_r
|
||||
file_recv_chunk->data_length = 0;
|
||||
}
|
||||
|
||||
if (data == nullptr) {
|
||||
assert(data_length == 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t *data_copy = (uint8_t *)malloc(data_length);
|
||||
|
||||
if (data_copy == nullptr) {
|
||||
@ -142,7 +146,6 @@ static bool tox_event_file_recv_chunk_unpack_into(
|
||||
&& bin_unpack_bin(bu, &event->data, &event->data_length);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -196,6 +199,7 @@ bool tox_event_file_recv_chunk_unpack(
|
||||
Tox_Event_File_Recv_Chunk **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_file_recv_chunk_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -225,16 +229,15 @@ static Tox_Event_File_Recv_Chunk *tox_event_file_recv_chunk_alloc(void *user_dat
|
||||
return file_recv_chunk;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_file_recv_chunk(Tox *tox, uint32_t friend_number, uint32_t file_number, uint64_t position, const uint8_t *data, size_t length,
|
||||
void *user_data)
|
||||
void tox_events_handle_file_recv_chunk(
|
||||
Tox *tox, uint32_t friend_number, uint32_t file_number, uint64_t position, const uint8_t *data, size_t length,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_File_Recv_Chunk *file_recv_chunk = tox_event_file_recv_chunk_alloc(user_data);
|
||||
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -15,14 +16,12 @@
|
||||
#include "../tox_pack.h"
|
||||
#include "../tox_unpack.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_File_Recv_Control {
|
||||
uint32_t friend_number;
|
||||
uint32_t file_number;
|
||||
@ -104,7 +103,6 @@ static bool tox_event_file_recv_control_unpack_into(
|
||||
&& tox_file_control_unpack(&event->control, bu);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -158,6 +156,7 @@ bool tox_event_file_recv_control_unpack(
|
||||
Tox_Event_File_Recv_Control **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_file_recv_control_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -187,16 +186,15 @@ static Tox_Event_File_Recv_Control *tox_event_file_recv_control_alloc(void *user
|
||||
return file_recv_control;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_file_recv_control(Tox *tox, uint32_t friend_number, uint32_t file_number, Tox_File_Control control,
|
||||
void *user_data)
|
||||
void tox_events_handle_file_recv_control(
|
||||
Tox *tox, uint32_t friend_number, uint32_t file_number, Tox_File_Control control,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_File_Recv_Control *file_recv_control = tox_event_file_recv_control_alloc(user_data);
|
||||
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -15,14 +16,12 @@
|
||||
#include "../tox_pack.h"
|
||||
#include "../tox_unpack.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_Friend_Connection_Status {
|
||||
uint32_t friend_number;
|
||||
Tox_Connection connection_status;
|
||||
@ -88,7 +87,6 @@ static bool tox_event_friend_connection_status_unpack_into(
|
||||
&& tox_connection_unpack(&event->connection_status, bu);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -142,6 +140,7 @@ bool tox_event_friend_connection_status_unpack(
|
||||
Tox_Event_Friend_Connection_Status **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_friend_connection_status_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -171,16 +170,15 @@ static Tox_Event_Friend_Connection_Status *tox_event_friend_connection_status_al
|
||||
return friend_connection_status;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_friend_connection_status(Tox *tox, uint32_t friend_number, Tox_Connection connection_status,
|
||||
void *user_data)
|
||||
void tox_events_handle_friend_connection_status(
|
||||
Tox *tox, uint32_t friend_number, Tox_Connection connection_status,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_Friend_Connection_Status *friend_connection_status = tox_event_friend_connection_status_alloc(user_data);
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -15,14 +16,12 @@
|
||||
#include "../tox.h"
|
||||
#include "../tox_events.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_Friend_Lossless_Packet {
|
||||
uint32_t friend_number;
|
||||
uint8_t *data;
|
||||
@ -42,7 +41,7 @@ uint32_t tox_event_friend_lossless_packet_get_friend_number(const Tox_Event_Frie
|
||||
return friend_lossless_packet->friend_number;
|
||||
}
|
||||
|
||||
non_null()
|
||||
non_null(1) nullable(2)
|
||||
static bool tox_event_friend_lossless_packet_set_data(Tox_Event_Friend_Lossless_Packet *friend_lossless_packet,
|
||||
const uint8_t *data, uint32_t data_length)
|
||||
{
|
||||
@ -54,6 +53,11 @@ static bool tox_event_friend_lossless_packet_set_data(Tox_Event_Friend_Lossless_
|
||||
friend_lossless_packet->data_length = 0;
|
||||
}
|
||||
|
||||
if (data == nullptr) {
|
||||
assert(data_length == 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t *data_copy = (uint8_t *)malloc(data_length);
|
||||
|
||||
if (data_copy == nullptr) {
|
||||
@ -110,7 +114,6 @@ static bool tox_event_friend_lossless_packet_unpack_into(
|
||||
&& bin_unpack_bin(bu, &event->data, &event->data_length);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -164,6 +167,7 @@ bool tox_event_friend_lossless_packet_unpack(
|
||||
Tox_Event_Friend_Lossless_Packet **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_friend_lossless_packet_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -193,16 +197,15 @@ static Tox_Event_Friend_Lossless_Packet *tox_event_friend_lossless_packet_alloc(
|
||||
return friend_lossless_packet;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_friend_lossless_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length,
|
||||
void *user_data)
|
||||
void tox_events_handle_friend_lossless_packet(
|
||||
Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_Friend_Lossless_Packet *friend_lossless_packet = tox_event_friend_lossless_packet_alloc(user_data);
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -15,14 +16,12 @@
|
||||
#include "../tox.h"
|
||||
#include "../tox_events.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_Friend_Lossy_Packet {
|
||||
uint32_t friend_number;
|
||||
uint8_t *data;
|
||||
@ -42,7 +41,7 @@ uint32_t tox_event_friend_lossy_packet_get_friend_number(const Tox_Event_Friend_
|
||||
return friend_lossy_packet->friend_number;
|
||||
}
|
||||
|
||||
non_null()
|
||||
non_null(1) nullable(2)
|
||||
static bool tox_event_friend_lossy_packet_set_data(Tox_Event_Friend_Lossy_Packet *friend_lossy_packet,
|
||||
const uint8_t *data, uint32_t data_length)
|
||||
{
|
||||
@ -54,6 +53,11 @@ static bool tox_event_friend_lossy_packet_set_data(Tox_Event_Friend_Lossy_Packet
|
||||
friend_lossy_packet->data_length = 0;
|
||||
}
|
||||
|
||||
if (data == nullptr) {
|
||||
assert(data_length == 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t *data_copy = (uint8_t *)malloc(data_length);
|
||||
|
||||
if (data_copy == nullptr) {
|
||||
@ -110,7 +114,6 @@ static bool tox_event_friend_lossy_packet_unpack_into(
|
||||
&& bin_unpack_bin(bu, &event->data, &event->data_length);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -164,6 +167,7 @@ bool tox_event_friend_lossy_packet_unpack(
|
||||
Tox_Event_Friend_Lossy_Packet **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_friend_lossy_packet_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -193,16 +197,15 @@ static Tox_Event_Friend_Lossy_Packet *tox_event_friend_lossy_packet_alloc(void *
|
||||
return friend_lossy_packet;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_friend_lossy_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length,
|
||||
void *user_data)
|
||||
void tox_events_handle_friend_lossy_packet(
|
||||
Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_Friend_Lossy_Packet *friend_lossy_packet = tox_event_friend_lossy_packet_alloc(user_data);
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -17,14 +18,12 @@
|
||||
#include "../tox_pack.h"
|
||||
#include "../tox_unpack.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_Friend_Message {
|
||||
uint32_t friend_number;
|
||||
Tox_Message_Type type;
|
||||
@ -58,7 +57,7 @@ Tox_Message_Type tox_event_friend_message_get_type(const Tox_Event_Friend_Messag
|
||||
return friend_message->type;
|
||||
}
|
||||
|
||||
non_null()
|
||||
non_null(1) nullable(2)
|
||||
static bool tox_event_friend_message_set_message(Tox_Event_Friend_Message *friend_message,
|
||||
const uint8_t *message, uint32_t message_length)
|
||||
{
|
||||
@ -70,6 +69,11 @@ static bool tox_event_friend_message_set_message(Tox_Event_Friend_Message *frien
|
||||
friend_message->message_length = 0;
|
||||
}
|
||||
|
||||
if (message == nullptr) {
|
||||
assert(message_length == 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t *message_copy = (uint8_t *)malloc(message_length);
|
||||
|
||||
if (message_copy == nullptr) {
|
||||
@ -128,7 +132,6 @@ static bool tox_event_friend_message_unpack_into(
|
||||
&& bin_unpack_bin(bu, &event->message, &event->message_length);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -182,6 +185,7 @@ bool tox_event_friend_message_unpack(
|
||||
Tox_Event_Friend_Message **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_friend_message_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -211,16 +215,15 @@ static Tox_Event_Friend_Message *tox_event_friend_message_alloc(void *user_data)
|
||||
return friend_message;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_friend_message(Tox *tox, uint32_t friend_number, Tox_Message_Type type, const uint8_t *message, size_t length,
|
||||
void *user_data)
|
||||
void tox_events_handle_friend_message(
|
||||
Tox *tox, uint32_t friend_number, Tox_Message_Type type, const uint8_t *message, size_t length,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_Friend_Message *friend_message = tox_event_friend_message_alloc(user_data);
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -15,14 +16,12 @@
|
||||
#include "../tox.h"
|
||||
#include "../tox_events.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_Friend_Name {
|
||||
uint32_t friend_number;
|
||||
uint8_t *name;
|
||||
@ -42,7 +41,7 @@ uint32_t tox_event_friend_name_get_friend_number(const Tox_Event_Friend_Name *fr
|
||||
return friend_name->friend_number;
|
||||
}
|
||||
|
||||
non_null()
|
||||
non_null(1) nullable(2)
|
||||
static bool tox_event_friend_name_set_name(Tox_Event_Friend_Name *friend_name,
|
||||
const uint8_t *name, uint32_t name_length)
|
||||
{
|
||||
@ -54,6 +53,11 @@ static bool tox_event_friend_name_set_name(Tox_Event_Friend_Name *friend_name,
|
||||
friend_name->name_length = 0;
|
||||
}
|
||||
|
||||
if (name == nullptr) {
|
||||
assert(name_length == 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t *name_copy = (uint8_t *)malloc(name_length);
|
||||
|
||||
if (name_copy == nullptr) {
|
||||
@ -110,7 +114,6 @@ static bool tox_event_friend_name_unpack_into(
|
||||
&& bin_unpack_bin(bu, &event->name, &event->name_length);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -164,6 +167,7 @@ bool tox_event_friend_name_unpack(
|
||||
Tox_Event_Friend_Name **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_friend_name_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -193,16 +197,15 @@ static Tox_Event_Friend_Name *tox_event_friend_name_alloc(void *user_data)
|
||||
return friend_name;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_friend_name(Tox *tox, uint32_t friend_number, const uint8_t *name, size_t length,
|
||||
void *user_data)
|
||||
void tox_events_handle_friend_name(
|
||||
Tox *tox, uint32_t friend_number, const uint8_t *name, size_t length,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_Friend_Name *friend_name = tox_event_friend_name_alloc(user_data);
|
||||
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -13,14 +14,12 @@
|
||||
#include "../tox.h"
|
||||
#include "../tox_events.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_Friend_Read_Receipt {
|
||||
uint32_t friend_number;
|
||||
uint32_t message_id;
|
||||
@ -86,7 +85,6 @@ static bool tox_event_friend_read_receipt_unpack_into(
|
||||
&& bin_unpack_u32(bu, &event->message_id);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -140,6 +138,7 @@ bool tox_event_friend_read_receipt_unpack(
|
||||
Tox_Event_Friend_Read_Receipt **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_friend_read_receipt_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -169,16 +168,15 @@ static Tox_Event_Friend_Read_Receipt *tox_event_friend_read_receipt_alloc(void *
|
||||
return friend_read_receipt;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_friend_read_receipt(Tox *tox, uint32_t friend_number, uint32_t message_id,
|
||||
void *user_data)
|
||||
void tox_events_handle_friend_read_receipt(
|
||||
Tox *tox, uint32_t friend_number, uint32_t message_id,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_Friend_Read_Receipt *friend_read_receipt = tox_event_friend_read_receipt_alloc(user_data);
|
||||
|
||||
|
@ -7,6 +7,7 @@
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -15,14 +16,12 @@
|
||||
#include "../tox_events.h"
|
||||
#include "../tox_private.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_Friend_Request {
|
||||
uint8_t public_key[TOX_PUBLIC_KEY_SIZE];
|
||||
uint8_t *message;
|
||||
@ -81,7 +80,9 @@ non_null()
|
||||
static void tox_event_friend_request_construct(Tox_Event_Friend_Request *friend_request)
|
||||
{
|
||||
*friend_request = (Tox_Event_Friend_Request) {
|
||||
0
|
||||
{
|
||||
0
|
||||
}
|
||||
};
|
||||
}
|
||||
non_null()
|
||||
@ -159,6 +160,7 @@ bool tox_event_friend_request_unpack(
|
||||
Tox_Event_Friend_Request **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_friend_request_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -188,14 +190,12 @@ static Tox_Event_Friend_Request *tox_event_friend_request_alloc(void *user_data)
|
||||
return friend_request;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_friend_request(Tox *tox, const uint8_t *public_key, const uint8_t *message, size_t length,
|
||||
void *user_data)
|
||||
{
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -15,14 +16,12 @@
|
||||
#include "../tox_pack.h"
|
||||
#include "../tox_unpack.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_Friend_Status {
|
||||
uint32_t friend_number;
|
||||
Tox_User_Status status;
|
||||
@ -88,7 +87,6 @@ static bool tox_event_friend_status_unpack_into(
|
||||
&& tox_user_status_unpack(&event->status, bu);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -142,6 +140,7 @@ bool tox_event_friend_status_unpack(
|
||||
Tox_Event_Friend_Status **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_friend_status_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -171,16 +170,15 @@ static Tox_Event_Friend_Status *tox_event_friend_status_alloc(void *user_data)
|
||||
return friend_status;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_friend_status(Tox *tox, uint32_t friend_number, Tox_User_Status status,
|
||||
void *user_data)
|
||||
void tox_events_handle_friend_status(
|
||||
Tox *tox, uint32_t friend_number, Tox_User_Status status,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_Friend_Status *friend_status = tox_event_friend_status_alloc(user_data);
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -15,14 +16,12 @@
|
||||
#include "../tox.h"
|
||||
#include "../tox_events.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_Friend_Status_Message {
|
||||
uint32_t friend_number;
|
||||
uint8_t *message;
|
||||
@ -42,7 +41,7 @@ uint32_t tox_event_friend_status_message_get_friend_number(const Tox_Event_Frien
|
||||
return friend_status_message->friend_number;
|
||||
}
|
||||
|
||||
non_null()
|
||||
non_null(1) nullable(2)
|
||||
static bool tox_event_friend_status_message_set_message(Tox_Event_Friend_Status_Message *friend_status_message,
|
||||
const uint8_t *message, uint32_t message_length)
|
||||
{
|
||||
@ -54,6 +53,11 @@ static bool tox_event_friend_status_message_set_message(Tox_Event_Friend_Status_
|
||||
friend_status_message->message_length = 0;
|
||||
}
|
||||
|
||||
if (message == nullptr) {
|
||||
assert(message_length == 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t *message_copy = (uint8_t *)malloc(message_length);
|
||||
|
||||
if (message_copy == nullptr) {
|
||||
@ -110,7 +114,6 @@ static bool tox_event_friend_status_message_unpack_into(
|
||||
&& bin_unpack_bin(bu, &event->message, &event->message_length);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -164,6 +167,7 @@ bool tox_event_friend_status_message_unpack(
|
||||
Tox_Event_Friend_Status_Message **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_friend_status_message_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -193,16 +197,15 @@ static Tox_Event_Friend_Status_Message *tox_event_friend_status_message_alloc(vo
|
||||
return friend_status_message;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_friend_status_message(Tox *tox, uint32_t friend_number, const uint8_t *message, size_t length,
|
||||
void *user_data)
|
||||
void tox_events_handle_friend_status_message(
|
||||
Tox *tox, uint32_t friend_number, const uint8_t *message, size_t length,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_Friend_Status_Message *friend_status_message = tox_event_friend_status_message_alloc(user_data);
|
||||
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -13,14 +14,12 @@
|
||||
#include "../tox.h"
|
||||
#include "../tox_events.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_Friend_Typing {
|
||||
uint32_t friend_number;
|
||||
bool typing;
|
||||
@ -86,7 +85,6 @@ static bool tox_event_friend_typing_unpack_into(
|
||||
&& bin_unpack_bool(bu, &event->typing);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -140,6 +138,7 @@ bool tox_event_friend_typing_unpack(
|
||||
Tox_Event_Friend_Typing **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_friend_typing_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -169,16 +168,15 @@ static Tox_Event_Friend_Typing *tox_event_friend_typing_alloc(void *user_data)
|
||||
return friend_typing;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_friend_typing(Tox *tox, uint32_t friend_number, bool typing,
|
||||
void *user_data)
|
||||
void tox_events_handle_friend_typing(
|
||||
Tox *tox, uint32_t friend_number, bool typing,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_Friend_Typing *friend_typing = tox_event_friend_typing_alloc(user_data);
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -15,14 +16,12 @@
|
||||
#include "../tox.h"
|
||||
#include "../tox_events.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_Group_Custom_Packet {
|
||||
uint32_t group_number;
|
||||
uint32_t peer_id;
|
||||
@ -56,7 +55,7 @@ uint32_t tox_event_group_custom_packet_get_peer_id(const Tox_Event_Group_Custom_
|
||||
return group_custom_packet->peer_id;
|
||||
}
|
||||
|
||||
non_null()
|
||||
non_null(1) nullable(2)
|
||||
static bool tox_event_group_custom_packet_set_data(Tox_Event_Group_Custom_Packet *group_custom_packet,
|
||||
const uint8_t *data, uint32_t data_length)
|
||||
{
|
||||
@ -68,6 +67,11 @@ static bool tox_event_group_custom_packet_set_data(Tox_Event_Group_Custom_Packet
|
||||
group_custom_packet->data_length = 0;
|
||||
}
|
||||
|
||||
if (data == nullptr) {
|
||||
assert(data_length == 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t *data_copy = (uint8_t *)malloc(data_length);
|
||||
|
||||
if (data_copy == nullptr) {
|
||||
@ -126,7 +130,6 @@ static bool tox_event_group_custom_packet_unpack_into(
|
||||
&& bin_unpack_bin(bu, &event->data, &event->data_length);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -180,6 +183,7 @@ bool tox_event_group_custom_packet_unpack(
|
||||
Tox_Event_Group_Custom_Packet **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_group_custom_packet_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -209,16 +213,15 @@ static Tox_Event_Group_Custom_Packet *tox_event_group_custom_packet_alloc(void *
|
||||
return group_custom_packet;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_group_custom_packet(Tox *tox, uint32_t group_number, uint32_t peer_id, const uint8_t *data, size_t length,
|
||||
void *user_data)
|
||||
void tox_events_handle_group_custom_packet(
|
||||
Tox *tox, uint32_t group_number, uint32_t peer_id, const uint8_t *data, size_t length,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_Group_Custom_Packet *group_custom_packet = tox_event_group_custom_packet_alloc(user_data);
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -15,14 +16,12 @@
|
||||
#include "../tox.h"
|
||||
#include "../tox_events.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_Group_Custom_Private_Packet {
|
||||
uint32_t group_number;
|
||||
uint32_t peer_id;
|
||||
@ -56,7 +55,7 @@ uint32_t tox_event_group_custom_private_packet_get_peer_id(const Tox_Event_Group
|
||||
return group_custom_private_packet->peer_id;
|
||||
}
|
||||
|
||||
non_null()
|
||||
non_null(1) nullable(2)
|
||||
static bool tox_event_group_custom_private_packet_set_data(Tox_Event_Group_Custom_Private_Packet *group_custom_private_packet,
|
||||
const uint8_t *data, uint32_t data_length)
|
||||
{
|
||||
@ -68,6 +67,11 @@ static bool tox_event_group_custom_private_packet_set_data(Tox_Event_Group_Custo
|
||||
group_custom_private_packet->data_length = 0;
|
||||
}
|
||||
|
||||
if (data == nullptr) {
|
||||
assert(data_length == 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t *data_copy = (uint8_t *)malloc(data_length);
|
||||
|
||||
if (data_copy == nullptr) {
|
||||
@ -126,7 +130,6 @@ static bool tox_event_group_custom_private_packet_unpack_into(
|
||||
&& bin_unpack_bin(bu, &event->data, &event->data_length);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -180,6 +183,7 @@ bool tox_event_group_custom_private_packet_unpack(
|
||||
Tox_Event_Group_Custom_Private_Packet **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_group_custom_private_packet_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -209,16 +213,15 @@ static Tox_Event_Group_Custom_Private_Packet *tox_event_group_custom_private_pac
|
||||
return group_custom_private_packet;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_group_custom_private_packet(Tox *tox, uint32_t group_number, uint32_t peer_id, const uint8_t *data, size_t length,
|
||||
void *user_data)
|
||||
void tox_events_handle_group_custom_private_packet(
|
||||
Tox *tox, uint32_t group_number, uint32_t peer_id, const uint8_t *data, size_t length,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_Group_Custom_Private_Packet *group_custom_private_packet = tox_event_group_custom_private_packet_alloc(user_data);
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -15,14 +16,12 @@
|
||||
#include "../tox.h"
|
||||
#include "../tox_events.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_Group_Invite {
|
||||
uint32_t friend_number;
|
||||
uint8_t *invite_data;
|
||||
@ -44,7 +43,7 @@ uint32_t tox_event_group_invite_get_friend_number(const Tox_Event_Group_Invite *
|
||||
return group_invite->friend_number;
|
||||
}
|
||||
|
||||
non_null()
|
||||
non_null(1) nullable(2)
|
||||
static bool tox_event_group_invite_set_invite_data(Tox_Event_Group_Invite *group_invite,
|
||||
const uint8_t *invite_data, uint32_t invite_data_length)
|
||||
{
|
||||
@ -56,6 +55,11 @@ static bool tox_event_group_invite_set_invite_data(Tox_Event_Group_Invite *group
|
||||
group_invite->invite_data_length = 0;
|
||||
}
|
||||
|
||||
if (invite_data == nullptr) {
|
||||
assert(invite_data_length == 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t *invite_data_copy = (uint8_t *)malloc(invite_data_length);
|
||||
|
||||
if (invite_data_copy == nullptr) {
|
||||
@ -78,7 +82,7 @@ const uint8_t *tox_event_group_invite_get_invite_data(const Tox_Event_Group_Invi
|
||||
return group_invite->invite_data;
|
||||
}
|
||||
|
||||
non_null()
|
||||
non_null(1) nullable(2)
|
||||
static bool tox_event_group_invite_set_group_name(Tox_Event_Group_Invite *group_invite,
|
||||
const uint8_t *group_name, uint32_t group_name_length)
|
||||
{
|
||||
@ -90,6 +94,11 @@ static bool tox_event_group_invite_set_group_name(Tox_Event_Group_Invite *group_
|
||||
group_invite->group_name_length = 0;
|
||||
}
|
||||
|
||||
if (group_name == nullptr) {
|
||||
assert(group_name_length == 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t *group_name_copy = (uint8_t *)malloc(group_name_length);
|
||||
|
||||
if (group_name_copy == nullptr) {
|
||||
@ -149,7 +158,6 @@ static bool tox_event_group_invite_unpack_into(
|
||||
&& bin_unpack_bin(bu, &event->group_name, &event->group_name_length);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -203,6 +211,7 @@ bool tox_event_group_invite_unpack(
|
||||
Tox_Event_Group_Invite **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_group_invite_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -232,16 +241,15 @@ static Tox_Event_Group_Invite *tox_event_group_invite_alloc(void *user_data)
|
||||
return group_invite;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_group_invite(Tox *tox, uint32_t friend_number, const uint8_t *invite_data, size_t length, const uint8_t *group_name, size_t group_name_length,
|
||||
void *user_data)
|
||||
void tox_events_handle_group_invite(
|
||||
Tox *tox, uint32_t friend_number, const uint8_t *invite_data, size_t length, const uint8_t *group_name, size_t group_name_length,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_Group_Invite *group_invite = tox_event_group_invite_alloc(user_data);
|
||||
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -15,14 +16,12 @@
|
||||
#include "../tox_pack.h"
|
||||
#include "../tox_unpack.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_Group_Join_Fail {
|
||||
uint32_t group_number;
|
||||
Tox_Group_Join_Fail fail_type;
|
||||
@ -88,7 +87,6 @@ static bool tox_event_group_join_fail_unpack_into(
|
||||
&& tox_group_join_fail_unpack(&event->fail_type, bu);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -142,6 +140,7 @@ bool tox_event_group_join_fail_unpack(
|
||||
Tox_Event_Group_Join_Fail **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_group_join_fail_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -171,16 +170,15 @@ static Tox_Event_Group_Join_Fail *tox_event_group_join_fail_alloc(void *user_dat
|
||||
return group_join_fail;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_group_join_fail(Tox *tox, uint32_t group_number, Tox_Group_Join_Fail fail_type,
|
||||
void *user_data)
|
||||
void tox_events_handle_group_join_fail(
|
||||
Tox *tox, uint32_t group_number, Tox_Group_Join_Fail fail_type,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_Group_Join_Fail *group_join_fail = tox_event_group_join_fail_alloc(user_data);
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -17,14 +18,12 @@
|
||||
#include "../tox_pack.h"
|
||||
#include "../tox_unpack.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_Group_Message {
|
||||
uint32_t group_number;
|
||||
uint32_t peer_id;
|
||||
@ -73,7 +72,7 @@ Tox_Message_Type tox_event_group_message_get_type(const Tox_Event_Group_Message
|
||||
return group_message->type;
|
||||
}
|
||||
|
||||
non_null()
|
||||
non_null(1) nullable(2)
|
||||
static bool tox_event_group_message_set_message(Tox_Event_Group_Message *group_message,
|
||||
const uint8_t *message, uint32_t message_length)
|
||||
{
|
||||
@ -85,6 +84,11 @@ static bool tox_event_group_message_set_message(Tox_Event_Group_Message *group_m
|
||||
group_message->message_length = 0;
|
||||
}
|
||||
|
||||
if (message == nullptr) {
|
||||
assert(message_length == 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t *message_copy = (uint8_t *)malloc(message_length);
|
||||
|
||||
if (message_copy == nullptr) {
|
||||
@ -160,7 +164,6 @@ static bool tox_event_group_message_unpack_into(
|
||||
&& bin_unpack_u32(bu, &event->message_id);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -214,6 +217,7 @@ bool tox_event_group_message_unpack(
|
||||
Tox_Event_Group_Message **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_group_message_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -243,16 +247,15 @@ static Tox_Event_Group_Message *tox_event_group_message_alloc(void *user_data)
|
||||
return group_message;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_group_message(Tox *tox, uint32_t group_number, uint32_t peer_id, Tox_Message_Type type, const uint8_t *message, size_t length, uint32_t message_id,
|
||||
void *user_data)
|
||||
void tox_events_handle_group_message(
|
||||
Tox *tox, uint32_t group_number, uint32_t peer_id, Tox_Message_Type type, const uint8_t *message, size_t length, uint32_t message_id,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_Group_Message *group_message = tox_event_group_message_alloc(user_data);
|
||||
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -15,14 +16,12 @@
|
||||
#include "../tox_pack.h"
|
||||
#include "../tox_unpack.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_Group_Moderation {
|
||||
uint32_t group_number;
|
||||
uint32_t source_peer_id;
|
||||
@ -120,7 +119,6 @@ static bool tox_event_group_moderation_unpack_into(
|
||||
&& tox_group_mod_event_unpack(&event->mod_type, bu);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -174,6 +172,7 @@ bool tox_event_group_moderation_unpack(
|
||||
Tox_Event_Group_Moderation **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_group_moderation_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -203,16 +202,15 @@ static Tox_Event_Group_Moderation *tox_event_group_moderation_alloc(void *user_d
|
||||
return group_moderation;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_group_moderation(Tox *tox, uint32_t group_number, uint32_t source_peer_id, uint32_t target_peer_id, Tox_Group_Mod_Event mod_type,
|
||||
void *user_data)
|
||||
void tox_events_handle_group_moderation(
|
||||
Tox *tox, uint32_t group_number, uint32_t source_peer_id, uint32_t target_peer_id, Tox_Group_Mod_Event mod_type,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_Group_Moderation *group_moderation = tox_event_group_moderation_alloc(user_data);
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -15,14 +16,12 @@
|
||||
#include "../tox.h"
|
||||
#include "../tox_events.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_Group_Password {
|
||||
uint32_t group_number;
|
||||
uint8_t *password;
|
||||
@ -42,7 +41,7 @@ uint32_t tox_event_group_password_get_group_number(const Tox_Event_Group_Passwor
|
||||
return group_password->group_number;
|
||||
}
|
||||
|
||||
non_null()
|
||||
non_null(1) nullable(2)
|
||||
static bool tox_event_group_password_set_password(Tox_Event_Group_Password *group_password,
|
||||
const uint8_t *password, uint32_t password_length)
|
||||
{
|
||||
@ -54,6 +53,11 @@ static bool tox_event_group_password_set_password(Tox_Event_Group_Password *grou
|
||||
group_password->password_length = 0;
|
||||
}
|
||||
|
||||
if (password == nullptr) {
|
||||
assert(password_length == 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t *password_copy = (uint8_t *)malloc(password_length);
|
||||
|
||||
if (password_copy == nullptr) {
|
||||
@ -110,7 +114,6 @@ static bool tox_event_group_password_unpack_into(
|
||||
&& bin_unpack_bin(bu, &event->password, &event->password_length);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -164,6 +167,7 @@ bool tox_event_group_password_unpack(
|
||||
Tox_Event_Group_Password **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_group_password_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -193,16 +197,15 @@ static Tox_Event_Group_Password *tox_event_group_password_alloc(void *user_data)
|
||||
return group_password;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_group_password(Tox *tox, uint32_t group_number, const uint8_t *password, size_t length,
|
||||
void *user_data)
|
||||
void tox_events_handle_group_password(
|
||||
Tox *tox, uint32_t group_number, const uint8_t *password, size_t length,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_Group_Password *group_password = tox_event_group_password_alloc(user_data);
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -17,14 +18,12 @@
|
||||
#include "../tox_pack.h"
|
||||
#include "../tox_unpack.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_Group_Peer_Exit {
|
||||
uint32_t group_number;
|
||||
uint32_t peer_id;
|
||||
@ -74,7 +73,7 @@ Tox_Group_Exit_Type tox_event_group_peer_exit_get_exit_type(const Tox_Event_Grou
|
||||
return group_peer_exit->exit_type;
|
||||
}
|
||||
|
||||
non_null()
|
||||
non_null(1) nullable(2)
|
||||
static bool tox_event_group_peer_exit_set_name(Tox_Event_Group_Peer_Exit *group_peer_exit,
|
||||
const uint8_t *name, uint32_t name_length)
|
||||
{
|
||||
@ -86,6 +85,11 @@ static bool tox_event_group_peer_exit_set_name(Tox_Event_Group_Peer_Exit *group_
|
||||
group_peer_exit->name_length = 0;
|
||||
}
|
||||
|
||||
if (name == nullptr) {
|
||||
assert(name_length == 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t *name_copy = (uint8_t *)malloc(name_length);
|
||||
|
||||
if (name_copy == nullptr) {
|
||||
@ -108,7 +112,7 @@ const uint8_t *tox_event_group_peer_exit_get_name(const Tox_Event_Group_Peer_Exi
|
||||
return group_peer_exit->name;
|
||||
}
|
||||
|
||||
non_null()
|
||||
non_null(1) nullable(2)
|
||||
static bool tox_event_group_peer_exit_set_part_message(Tox_Event_Group_Peer_Exit *group_peer_exit,
|
||||
const uint8_t *part_message, uint32_t part_message_length)
|
||||
{
|
||||
@ -120,6 +124,11 @@ static bool tox_event_group_peer_exit_set_part_message(Tox_Event_Group_Peer_Exit
|
||||
group_peer_exit->part_message_length = 0;
|
||||
}
|
||||
|
||||
if (part_message == nullptr) {
|
||||
assert(part_message_length == 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t *part_message_copy = (uint8_t *)malloc(part_message_length);
|
||||
|
||||
if (part_message_copy == nullptr) {
|
||||
@ -183,7 +192,6 @@ static bool tox_event_group_peer_exit_unpack_into(
|
||||
&& bin_unpack_bin(bu, &event->part_message, &event->part_message_length);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -237,6 +245,7 @@ bool tox_event_group_peer_exit_unpack(
|
||||
Tox_Event_Group_Peer_Exit **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_group_peer_exit_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -266,16 +275,15 @@ static Tox_Event_Group_Peer_Exit *tox_event_group_peer_exit_alloc(void *user_dat
|
||||
return group_peer_exit;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_group_peer_exit(Tox *tox, uint32_t group_number, uint32_t peer_id, Tox_Group_Exit_Type exit_type, const uint8_t *name, size_t name_length, const uint8_t *part_message, size_t part_message_length,
|
||||
void *user_data)
|
||||
void tox_events_handle_group_peer_exit(
|
||||
Tox *tox, uint32_t group_number, uint32_t peer_id, Tox_Group_Exit_Type exit_type, const uint8_t *name, size_t name_length, const uint8_t *part_message, size_t part_message_length,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_Group_Peer_Exit *group_peer_exit = tox_event_group_peer_exit_alloc(user_data);
|
||||
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -13,14 +14,12 @@
|
||||
#include "../tox.h"
|
||||
#include "../tox_events.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_Group_Peer_Join {
|
||||
uint32_t group_number;
|
||||
uint32_t peer_id;
|
||||
@ -86,7 +85,6 @@ static bool tox_event_group_peer_join_unpack_into(
|
||||
&& bin_unpack_u32(bu, &event->peer_id);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -140,6 +138,7 @@ bool tox_event_group_peer_join_unpack(
|
||||
Tox_Event_Group_Peer_Join **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_group_peer_join_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -169,16 +168,15 @@ static Tox_Event_Group_Peer_Join *tox_event_group_peer_join_alloc(void *user_dat
|
||||
return group_peer_join;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_group_peer_join(Tox *tox, uint32_t group_number, uint32_t peer_id,
|
||||
void *user_data)
|
||||
void tox_events_handle_group_peer_join(
|
||||
Tox *tox, uint32_t group_number, uint32_t peer_id,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_Group_Peer_Join *group_peer_join = tox_event_group_peer_join_alloc(user_data);
|
||||
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -13,14 +14,12 @@
|
||||
#include "../tox.h"
|
||||
#include "../tox_events.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_Group_Peer_Limit {
|
||||
uint32_t group_number;
|
||||
uint32_t peer_limit;
|
||||
@ -86,7 +85,6 @@ static bool tox_event_group_peer_limit_unpack_into(
|
||||
&& bin_unpack_u32(bu, &event->peer_limit);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -140,6 +138,7 @@ bool tox_event_group_peer_limit_unpack(
|
||||
Tox_Event_Group_Peer_Limit **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_group_peer_limit_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -169,16 +168,15 @@ static Tox_Event_Group_Peer_Limit *tox_event_group_peer_limit_alloc(void *user_d
|
||||
return group_peer_limit;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_group_peer_limit(Tox *tox, uint32_t group_number, uint32_t peer_limit,
|
||||
void *user_data)
|
||||
void tox_events_handle_group_peer_limit(
|
||||
Tox *tox, uint32_t group_number, uint32_t peer_limit,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_Group_Peer_Limit *group_peer_limit = tox_event_group_peer_limit_alloc(user_data);
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -15,14 +16,12 @@
|
||||
#include "../tox.h"
|
||||
#include "../tox_events.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_Group_Peer_Name {
|
||||
uint32_t group_number;
|
||||
uint32_t peer_id;
|
||||
@ -56,7 +55,7 @@ uint32_t tox_event_group_peer_name_get_peer_id(const Tox_Event_Group_Peer_Name *
|
||||
return group_peer_name->peer_id;
|
||||
}
|
||||
|
||||
non_null()
|
||||
non_null(1) nullable(2)
|
||||
static bool tox_event_group_peer_name_set_name(Tox_Event_Group_Peer_Name *group_peer_name,
|
||||
const uint8_t *name, uint32_t name_length)
|
||||
{
|
||||
@ -68,6 +67,11 @@ static bool tox_event_group_peer_name_set_name(Tox_Event_Group_Peer_Name *group_
|
||||
group_peer_name->name_length = 0;
|
||||
}
|
||||
|
||||
if (name == nullptr) {
|
||||
assert(name_length == 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t *name_copy = (uint8_t *)malloc(name_length);
|
||||
|
||||
if (name_copy == nullptr) {
|
||||
@ -126,7 +130,6 @@ static bool tox_event_group_peer_name_unpack_into(
|
||||
&& bin_unpack_bin(bu, &event->name, &event->name_length);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -180,6 +183,7 @@ bool tox_event_group_peer_name_unpack(
|
||||
Tox_Event_Group_Peer_Name **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_group_peer_name_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -209,16 +213,15 @@ static Tox_Event_Group_Peer_Name *tox_event_group_peer_name_alloc(void *user_dat
|
||||
return group_peer_name;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_group_peer_name(Tox *tox, uint32_t group_number, uint32_t peer_id, const uint8_t *name, size_t length,
|
||||
void *user_data)
|
||||
void tox_events_handle_group_peer_name(
|
||||
Tox *tox, uint32_t group_number, uint32_t peer_id, const uint8_t *name, size_t length,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_Group_Peer_Name *group_peer_name = tox_event_group_peer_name_alloc(user_data);
|
||||
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -15,14 +16,12 @@
|
||||
#include "../tox_pack.h"
|
||||
#include "../tox_unpack.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_Group_Peer_Status {
|
||||
uint32_t group_number;
|
||||
uint32_t peer_id;
|
||||
@ -104,7 +103,6 @@ static bool tox_event_group_peer_status_unpack_into(
|
||||
&& tox_user_status_unpack(&event->status, bu);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -158,6 +156,7 @@ bool tox_event_group_peer_status_unpack(
|
||||
Tox_Event_Group_Peer_Status **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_group_peer_status_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -187,16 +186,15 @@ static Tox_Event_Group_Peer_Status *tox_event_group_peer_status_alloc(void *user
|
||||
return group_peer_status;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_group_peer_status(Tox *tox, uint32_t group_number, uint32_t peer_id, Tox_User_Status status,
|
||||
void *user_data)
|
||||
void tox_events_handle_group_peer_status(
|
||||
Tox *tox, uint32_t group_number, uint32_t peer_id, Tox_User_Status status,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_Group_Peer_Status *group_peer_status = tox_event_group_peer_status_alloc(user_data);
|
||||
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -15,14 +16,12 @@
|
||||
#include "../tox_pack.h"
|
||||
#include "../tox_unpack.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_Group_Privacy_State {
|
||||
uint32_t group_number;
|
||||
Tox_Group_Privacy_State privacy_state;
|
||||
@ -88,7 +87,6 @@ static bool tox_event_group_privacy_state_unpack_into(
|
||||
&& tox_group_privacy_state_unpack(&event->privacy_state, bu);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -142,6 +140,7 @@ bool tox_event_group_privacy_state_unpack(
|
||||
Tox_Event_Group_Privacy_State **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_group_privacy_state_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -171,16 +170,15 @@ static Tox_Event_Group_Privacy_State *tox_event_group_privacy_state_alloc(void *
|
||||
return group_privacy_state;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_group_privacy_state(Tox *tox, uint32_t group_number, Tox_Group_Privacy_State privacy_state,
|
||||
void *user_data)
|
||||
void tox_events_handle_group_privacy_state(
|
||||
Tox *tox, uint32_t group_number, Tox_Group_Privacy_State privacy_state,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_Group_Privacy_State *group_privacy_state = tox_event_group_privacy_state_alloc(user_data);
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -17,20 +18,19 @@
|
||||
#include "../tox_pack.h"
|
||||
#include "../tox_unpack.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_Group_Private_Message {
|
||||
uint32_t group_number;
|
||||
uint32_t peer_id;
|
||||
Tox_Message_Type type;
|
||||
uint8_t *message;
|
||||
uint32_t message_length;
|
||||
uint32_t message_id;
|
||||
};
|
||||
|
||||
non_null()
|
||||
@ -72,7 +72,7 @@ Tox_Message_Type tox_event_group_private_message_get_type(const Tox_Event_Group_
|
||||
return group_private_message->type;
|
||||
}
|
||||
|
||||
non_null()
|
||||
non_null(1) nullable(2)
|
||||
static bool tox_event_group_private_message_set_message(Tox_Event_Group_Private_Message *group_private_message,
|
||||
const uint8_t *message, uint32_t message_length)
|
||||
{
|
||||
@ -84,6 +84,11 @@ static bool tox_event_group_private_message_set_message(Tox_Event_Group_Private_
|
||||
group_private_message->message_length = 0;
|
||||
}
|
||||
|
||||
if (message == nullptr) {
|
||||
assert(message_length == 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t *message_copy = (uint8_t *)malloc(message_length);
|
||||
|
||||
if (message_copy == nullptr) {
|
||||
@ -106,6 +111,19 @@ const uint8_t *tox_event_group_private_message_get_message(const Tox_Event_Group
|
||||
return group_private_message->message;
|
||||
}
|
||||
|
||||
non_null()
|
||||
static void tox_event_group_private_message_set_message_id(Tox_Event_Group_Private_Message *group_private_message,
|
||||
uint32_t message_id)
|
||||
{
|
||||
assert(group_private_message != nullptr);
|
||||
group_private_message->message_id = message_id;
|
||||
}
|
||||
uint32_t tox_event_group_private_message_get_message_id(const Tox_Event_Group_Private_Message *group_private_message)
|
||||
{
|
||||
assert(group_private_message != nullptr);
|
||||
return group_private_message->message_id;
|
||||
}
|
||||
|
||||
non_null()
|
||||
static void tox_event_group_private_message_construct(Tox_Event_Group_Private_Message *group_private_message)
|
||||
{
|
||||
@ -122,11 +140,12 @@ static void tox_event_group_private_message_destruct(Tox_Event_Group_Private_Mes
|
||||
bool tox_event_group_private_message_pack(
|
||||
const Tox_Event_Group_Private_Message *event, Bin_Pack *bp)
|
||||
{
|
||||
return bin_pack_array(bp, 4)
|
||||
return bin_pack_array(bp, 5)
|
||||
&& bin_pack_u32(bp, event->group_number)
|
||||
&& bin_pack_u32(bp, event->peer_id)
|
||||
&& tox_message_type_pack(event->type, bp)
|
||||
&& bin_pack_bin(bp, event->message, event->message_length);
|
||||
&& bin_pack_bin(bp, event->message, event->message_length)
|
||||
&& bin_pack_u32(bp, event->message_id);
|
||||
}
|
||||
|
||||
non_null()
|
||||
@ -134,17 +153,17 @@ static bool tox_event_group_private_message_unpack_into(
|
||||
Tox_Event_Group_Private_Message *event, Bin_Unpack *bu)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
if (!bin_unpack_array_fixed(bu, 4, nullptr)) {
|
||||
if (!bin_unpack_array_fixed(bu, 5, nullptr)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return bin_unpack_u32(bu, &event->group_number)
|
||||
&& bin_unpack_u32(bu, &event->peer_id)
|
||||
&& tox_message_type_unpack(&event->type, bu)
|
||||
&& bin_unpack_bin(bu, &event->message, &event->message_length);
|
||||
&& bin_unpack_bin(bu, &event->message, &event->message_length)
|
||||
&& bin_unpack_u32(bu, &event->message_id);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -198,6 +217,7 @@ bool tox_event_group_private_message_unpack(
|
||||
Tox_Event_Group_Private_Message **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_group_private_message_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -227,16 +247,15 @@ static Tox_Event_Group_Private_Message *tox_event_group_private_message_alloc(vo
|
||||
return group_private_message;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_group_private_message(Tox *tox, uint32_t group_number, uint32_t peer_id, Tox_Message_Type type, const uint8_t *message, size_t length,
|
||||
void *user_data)
|
||||
void tox_events_handle_group_private_message(
|
||||
Tox *tox, uint32_t group_number, uint32_t peer_id, Tox_Message_Type type, const uint8_t *message, size_t length, uint32_t message_id,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_Group_Private_Message *group_private_message = tox_event_group_private_message_alloc(user_data);
|
||||
|
||||
@ -248,4 +267,5 @@ void tox_events_handle_group_private_message(Tox *tox, uint32_t group_number, ui
|
||||
tox_event_group_private_message_set_peer_id(group_private_message, peer_id);
|
||||
tox_event_group_private_message_set_type(group_private_message, type);
|
||||
tox_event_group_private_message_set_message(group_private_message, message, length);
|
||||
tox_event_group_private_message_set_message_id(group_private_message, message_id);
|
||||
}
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -13,14 +14,12 @@
|
||||
#include "../tox.h"
|
||||
#include "../tox_events.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_Group_Self_Join {
|
||||
uint32_t group_number;
|
||||
};
|
||||
@ -65,7 +64,6 @@ static bool tox_event_group_self_join_unpack_into(
|
||||
return bin_unpack_u32(bu, &event->group_number);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -119,6 +117,7 @@ bool tox_event_group_self_join_unpack(
|
||||
Tox_Event_Group_Self_Join **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_group_self_join_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -148,16 +147,15 @@ static Tox_Event_Group_Self_Join *tox_event_group_self_join_alloc(void *user_dat
|
||||
return group_self_join;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_group_self_join(Tox *tox, uint32_t group_number,
|
||||
void *user_data)
|
||||
void tox_events_handle_group_self_join(
|
||||
Tox *tox, uint32_t group_number,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_Group_Self_Join *group_self_join = tox_event_group_self_join_alloc(user_data);
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -15,14 +16,12 @@
|
||||
#include "../tox.h"
|
||||
#include "../tox_events.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_Group_Topic {
|
||||
uint32_t group_number;
|
||||
uint32_t peer_id;
|
||||
@ -56,7 +55,7 @@ uint32_t tox_event_group_topic_get_peer_id(const Tox_Event_Group_Topic *group_to
|
||||
return group_topic->peer_id;
|
||||
}
|
||||
|
||||
non_null()
|
||||
non_null(1) nullable(2)
|
||||
static bool tox_event_group_topic_set_topic(Tox_Event_Group_Topic *group_topic,
|
||||
const uint8_t *topic, uint32_t topic_length)
|
||||
{
|
||||
@ -68,6 +67,11 @@ static bool tox_event_group_topic_set_topic(Tox_Event_Group_Topic *group_topic,
|
||||
group_topic->topic_length = 0;
|
||||
}
|
||||
|
||||
if (topic == nullptr) {
|
||||
assert(topic_length == 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t *topic_copy = (uint8_t *)malloc(topic_length);
|
||||
|
||||
if (topic_copy == nullptr) {
|
||||
@ -126,7 +130,6 @@ static bool tox_event_group_topic_unpack_into(
|
||||
&& bin_unpack_bin(bu, &event->topic, &event->topic_length);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -180,6 +183,7 @@ bool tox_event_group_topic_unpack(
|
||||
Tox_Event_Group_Topic **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_group_topic_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -209,16 +213,15 @@ static Tox_Event_Group_Topic *tox_event_group_topic_alloc(void *user_data)
|
||||
return group_topic;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_group_topic(Tox *tox, uint32_t group_number, uint32_t peer_id, const uint8_t *topic, size_t length,
|
||||
void *user_data)
|
||||
void tox_events_handle_group_topic(
|
||||
Tox *tox, uint32_t group_number, uint32_t peer_id, const uint8_t *topic, size_t length,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_Group_Topic *group_topic = tox_event_group_topic_alloc(user_data);
|
||||
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -15,14 +16,12 @@
|
||||
#include "../tox_pack.h"
|
||||
#include "../tox_unpack.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_Group_Topic_Lock {
|
||||
uint32_t group_number;
|
||||
Tox_Group_Topic_Lock topic_lock;
|
||||
@ -88,7 +87,6 @@ static bool tox_event_group_topic_lock_unpack_into(
|
||||
&& tox_group_topic_lock_unpack(&event->topic_lock, bu);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -142,6 +140,7 @@ bool tox_event_group_topic_lock_unpack(
|
||||
Tox_Event_Group_Topic_Lock **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_group_topic_lock_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -171,16 +170,15 @@ static Tox_Event_Group_Topic_Lock *tox_event_group_topic_lock_alloc(void *user_d
|
||||
return group_topic_lock;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_group_topic_lock(Tox *tox, uint32_t group_number, Tox_Group_Topic_Lock topic_lock,
|
||||
void *user_data)
|
||||
void tox_events_handle_group_topic_lock(
|
||||
Tox *tox, uint32_t group_number, Tox_Group_Topic_Lock topic_lock,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_Group_Topic_Lock *group_topic_lock = tox_event_group_topic_lock_alloc(user_data);
|
||||
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -15,14 +16,12 @@
|
||||
#include "../tox_pack.h"
|
||||
#include "../tox_unpack.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_Group_Voice_State {
|
||||
uint32_t group_number;
|
||||
Tox_Group_Voice_State voice_state;
|
||||
@ -88,7 +87,6 @@ static bool tox_event_group_voice_state_unpack_into(
|
||||
&& tox_group_voice_state_unpack(&event->voice_state, bu);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -142,6 +140,7 @@ bool tox_event_group_voice_state_unpack(
|
||||
Tox_Event_Group_Voice_State **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_group_voice_state_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -171,16 +170,15 @@ static Tox_Event_Group_Voice_State *tox_event_group_voice_state_alloc(void *user
|
||||
return group_voice_state;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_group_voice_state(Tox *tox, uint32_t group_number, Tox_Group_Voice_State voice_state,
|
||||
void *user_data)
|
||||
void tox_events_handle_group_voice_state(
|
||||
Tox *tox, uint32_t group_number, Tox_Group_Voice_State voice_state,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_Group_Voice_State *group_voice_state = tox_event_group_voice_state_alloc(user_data);
|
||||
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include "../attributes.h"
|
||||
#include "../bin_pack.h"
|
||||
#include "../bin_unpack.h"
|
||||
#include "../ccompat.h"
|
||||
@ -15,14 +16,12 @@
|
||||
#include "../tox_pack.h"
|
||||
#include "../tox_unpack.h"
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: struct and accessors
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
struct Tox_Event_Self_Connection_Status {
|
||||
Tox_Connection connection_status;
|
||||
};
|
||||
@ -67,7 +66,6 @@ static bool tox_event_self_connection_status_unpack_into(
|
||||
return tox_connection_unpack(&event->connection_status, bu);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: new/free/add/get/size/unpack
|
||||
@ -121,6 +119,7 @@ bool tox_event_self_connection_status_unpack(
|
||||
Tox_Event_Self_Connection_Status **event, Bin_Unpack *bu, const Memory *mem)
|
||||
{
|
||||
assert(event != nullptr);
|
||||
assert(*event == nullptr);
|
||||
*event = tox_event_self_connection_status_new(mem);
|
||||
|
||||
if (*event == nullptr) {
|
||||
@ -150,16 +149,15 @@ static Tox_Event_Self_Connection_Status *tox_event_self_connection_status_alloc(
|
||||
return self_connection_status;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************
|
||||
*
|
||||
* :: event handler
|
||||
*
|
||||
*****************************************************/
|
||||
|
||||
|
||||
void tox_events_handle_self_connection_status(Tox *tox, Tox_Connection connection_status,
|
||||
void *user_data)
|
||||
void tox_events_handle_self_connection_status(
|
||||
Tox *tox, Tox_Connection connection_status,
|
||||
void *user_data)
|
||||
{
|
||||
Tox_Event_Self_Connection_Status *self_connection_status = tox_event_self_connection_status_alloc(user_data);
|
||||
|
||||
|
@ -9,6 +9,7 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "DHT.h"
|
||||
#include "attributes.h"
|
||||
#include "ccompat.h"
|
||||
#include "crypto_core.h"
|
||||
#include "logger.h"
|
||||
|
@ -6,6 +6,10 @@
|
||||
#define C_TOXCORE_TOXCORE_FORWARDING_H
|
||||
|
||||
#include "DHT.h"
|
||||
#include "attributes.h"
|
||||
#include "crypto_core.h"
|
||||
#include "logger.h"
|
||||
#include "mono_time.h"
|
||||
#include "network.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
@ -79,7 +83,6 @@ bool forward_reply(const Networking_Core *net, const IP_Port *forwarder,
|
||||
const uint8_t *sendback, uint16_t sendback_length,
|
||||
const uint8_t *data, uint16_t length);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Set callback to handle a forwarded request.
|
||||
* To reply to the packet, callback should use `forward_reply()` to send a reply
|
||||
@ -119,7 +122,7 @@ nullable(1)
|
||||
void kill_forwarding(Forwarding *forwarding);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif
|
||||
#endif /* C_TOXCORE_TOXCORE_FORWARDING_H */
|
||||
|
@ -5,8 +5,8 @@
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
|
||||
#include "../testing/fuzzing/fuzz_support.h"
|
||||
#include "../testing/fuzzing/fuzz_tox.h"
|
||||
#include "../testing/fuzzing/fuzz_support.hh"
|
||||
#include "../testing/fuzzing/fuzz_tox.hh"
|
||||
|
||||
namespace {
|
||||
|
||||
|
@ -14,13 +14,17 @@
|
||||
#include "DHT.h"
|
||||
#include "LAN_discovery.h"
|
||||
#include "TCP_connection.h"
|
||||
#include "attributes.h"
|
||||
#include "ccompat.h"
|
||||
#include "crypto_core.h"
|
||||
#include "logger.h"
|
||||
#include "mono_time.h"
|
||||
#include "net_crypto.h"
|
||||
#include "network.h"
|
||||
#include "onion.h"
|
||||
#include "onion_announce.h"
|
||||
#include "onion_client.h"
|
||||
#include "util.h"
|
||||
|
||||
#define PORTS_PER_DISCOVERY 10
|
||||
|
||||
@ -63,7 +67,6 @@ struct Friend_Conn {
|
||||
|
||||
static const Friend_Conn empty_friend_conn = {0};
|
||||
|
||||
|
||||
struct Friend_Connections {
|
||||
const Mono_Time *mono_time;
|
||||
const Logger *logger;
|
||||
@ -102,7 +105,6 @@ const IP_Port *friend_conn_get_dht_ip_port(const Friend_Conn *fc)
|
||||
return &fc->dht_ip_port;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @retval true if the friendcon_id is valid.
|
||||
* @retval false if the friendcon_id is not valid.
|
||||
@ -115,7 +117,6 @@ static bool friendconn_id_valid(const Friend_Connections *fr_c, int friendcon_id
|
||||
fr_c->conns[friendcon_id].status != FRIENDCONN_STATUS_NONE;
|
||||
}
|
||||
|
||||
|
||||
/** @brief Set the size of the friend connections list to num.
|
||||
*
|
||||
* @retval false if realloc fails.
|
||||
@ -245,7 +246,7 @@ static int friend_add_tcp_relay(Friend_Connections *fr_c, int friendcon_id, cons
|
||||
if (!net_family_is_unspec(friend_con->dht_ip_port.ip.family)) {
|
||||
ipp_copy.ip = friend_con->dht_ip_port.ip;
|
||||
} else {
|
||||
friend_con->hosting_tcp_relay = 0;
|
||||
friend_con->hosting_tcp_relay = false;
|
||||
}
|
||||
}
|
||||
|
||||
@ -369,7 +370,7 @@ static void dht_ip_callback(void *object, int32_t number, const IP_Port *ip_port
|
||||
|
||||
if (friend_con->hosting_tcp_relay) {
|
||||
friend_add_tcp_relay(fr_c, number, ip_port, friend_con->dht_temp_pk);
|
||||
friend_con->hosting_tcp_relay = 0;
|
||||
friend_con->hosting_tcp_relay = false;
|
||||
}
|
||||
}
|
||||
|
||||
@ -397,10 +398,10 @@ static void change_dht_pk(Friend_Connections *fr_c, int friendcon_id, const uint
|
||||
}
|
||||
|
||||
non_null()
|
||||
static int handle_status(void *object, int number, bool status, void *userdata)
|
||||
static int handle_status(void *object, int id, bool status, void *userdata)
|
||||
{
|
||||
Friend_Connections *const fr_c = (Friend_Connections *)object;
|
||||
Friend_Conn *const friend_con = get_conn(fr_c, number);
|
||||
Friend_Conn *const friend_con = get_conn(fr_c, id);
|
||||
|
||||
if (friend_con == nullptr) {
|
||||
return -1;
|
||||
@ -423,12 +424,12 @@ static int handle_status(void *object, int number, bool status, void *userdata)
|
||||
|
||||
friend_con->status = FRIENDCONN_STATUS_CONNECTING;
|
||||
friend_con->crypt_connection_id = -1;
|
||||
friend_con->hosting_tcp_relay = 0;
|
||||
friend_con->hosting_tcp_relay = false;
|
||||
}
|
||||
|
||||
if (status_changed) {
|
||||
if (fr_c->global_status_callback != nullptr) {
|
||||
fr_c->global_status_callback(fr_c->global_status_callback_object, number, status, userdata);
|
||||
fr_c->global_status_callback(fr_c->global_status_callback_object, id, status, userdata);
|
||||
}
|
||||
|
||||
for (unsigned i = 0; i < MAX_FRIEND_CONNECTION_CALLBACKS; ++i) {
|
||||
@ -472,7 +473,7 @@ static void dht_pk_callback(void *object, int32_t number, const uint8_t *dht_pub
|
||||
}
|
||||
|
||||
non_null()
|
||||
static int handle_packet(void *object, int number, const uint8_t *data, uint16_t length, void *userdata)
|
||||
static int handle_packet(void *object, int id, const uint8_t *data, uint16_t length, void *userdata)
|
||||
{
|
||||
Friend_Connections *const fr_c = (Friend_Connections *)object;
|
||||
|
||||
@ -480,7 +481,7 @@ static int handle_packet(void *object, int number, const uint8_t *data, uint16_t
|
||||
return -1;
|
||||
}
|
||||
|
||||
Friend_Conn *friend_con = get_conn(fr_c, number);
|
||||
Friend_Conn *friend_con = get_conn(fr_c, id);
|
||||
|
||||
if (friend_con == nullptr) {
|
||||
return -1;
|
||||
@ -508,7 +509,7 @@ static int handle_packet(void *object, int number, const uint8_t *data, uint16_t
|
||||
}
|
||||
|
||||
for (int j = 0; j < n; ++j) {
|
||||
friend_add_tcp_relay(fr_c, number, &nodes[j].ip_port, nodes[j].public_key);
|
||||
friend_add_tcp_relay(fr_c, id, &nodes[j].ip_port, nodes[j].public_key);
|
||||
}
|
||||
|
||||
return 0;
|
||||
@ -521,7 +522,7 @@ static int handle_packet(void *object, int number, const uint8_t *data, uint16_t
|
||||
friend_con->callbacks[i].callback_id, data, length, userdata);
|
||||
}
|
||||
|
||||
friend_con = get_conn(fr_c, number);
|
||||
friend_con = get_conn(fr_c, id);
|
||||
|
||||
if (friend_con == nullptr) {
|
||||
return -1;
|
||||
@ -532,7 +533,7 @@ static int handle_packet(void *object, int number, const uint8_t *data, uint16_t
|
||||
}
|
||||
|
||||
non_null()
|
||||
static int handle_lossy_packet(void *object, int number, const uint8_t *data, uint16_t length, void *userdata)
|
||||
static int handle_lossy_packet(void *object, int id, const uint8_t *data, uint16_t length, void *userdata)
|
||||
{
|
||||
const Friend_Connections *const fr_c = (const Friend_Connections *)object;
|
||||
|
||||
@ -540,7 +541,7 @@ static int handle_lossy_packet(void *object, int number, const uint8_t *data, ui
|
||||
return -1;
|
||||
}
|
||||
|
||||
const Friend_Conn *friend_con = get_conn(fr_c, number);
|
||||
const Friend_Conn *friend_con = get_conn(fr_c, id);
|
||||
|
||||
if (friend_con == nullptr) {
|
||||
return -1;
|
||||
@ -553,7 +554,7 @@ static int handle_lossy_packet(void *object, int number, const uint8_t *data, ui
|
||||
friend_con->callbacks[i].callback_id, data, length, userdata);
|
||||
}
|
||||
|
||||
friend_con = get_conn(fr_c, number);
|
||||
friend_con = get_conn(fr_c, id);
|
||||
|
||||
if (friend_con == nullptr) {
|
||||
return -1;
|
||||
@ -854,7 +855,6 @@ int kill_friend_connection(Friend_Connections *fr_c, int friendcon_id)
|
||||
return wipe_friend_conn(fr_c, friendcon_id);
|
||||
}
|
||||
|
||||
|
||||
/** @brief Set friend request callback.
|
||||
*
|
||||
* This function will be called every time a friend request packet is received.
|
||||
@ -885,18 +885,19 @@ int send_friend_request_packet(Friend_Connections *fr_c, int friendcon_id, uint3
|
||||
return -1;
|
||||
}
|
||||
|
||||
VLA(uint8_t, packet, 1 + sizeof(nospam_num) + length);
|
||||
const uint16_t packet_size = 1 + sizeof(nospam_num) + length;
|
||||
VLA(uint8_t, packet, packet_size);
|
||||
memcpy(packet + 1, &nospam_num, sizeof(nospam_num));
|
||||
memcpy(packet + 1 + sizeof(nospam_num), data, length);
|
||||
|
||||
if (friend_con->status == FRIENDCONN_STATUS_CONNECTED) {
|
||||
packet[0] = PACKET_ID_FRIEND_REQUESTS;
|
||||
return write_cryptpacket(fr_c->net_crypto, friend_con->crypt_connection_id, packet, SIZEOF_VLA(packet),
|
||||
return write_cryptpacket(fr_c->net_crypto, friend_con->crypt_connection_id, packet, packet_size,
|
||||
false) != -1 ? 1 : 0;
|
||||
}
|
||||
|
||||
packet[0] = CRYPTO_PACKET_FRIEND_REQ;
|
||||
const int num = send_onion_data(fr_c->onion_c, friend_con->onion_friendnum, packet, SIZEOF_VLA(packet));
|
||||
const int num = send_onion_data(fr_c->onion_c, friend_con->onion_friendnum, packet, packet_size);
|
||||
|
||||
if (num <= 0) {
|
||||
return -1;
|
||||
@ -907,8 +908,8 @@ int send_friend_request_packet(Friend_Connections *fr_c, int friendcon_id, uint3
|
||||
|
||||
/** Create new friend_connections instance. */
|
||||
Friend_Connections *new_friend_connections(
|
||||
const Logger *logger, const Mono_Time *mono_time, const Network *ns,
|
||||
Onion_Client *onion_c, bool local_discovery_enabled)
|
||||
const Logger *logger, const Mono_Time *mono_time, const Network *ns,
|
||||
Onion_Client *onion_c, bool local_discovery_enabled)
|
||||
{
|
||||
if (onion_c == nullptr) {
|
||||
return nullptr;
|
||||
@ -982,7 +983,7 @@ void do_friend_connections(Friend_Connections *fr_c, void *userdata)
|
||||
if (friend_con->dht_lock_token > 0) {
|
||||
dht_delfriend(fr_c->dht, friend_con->dht_temp_pk, friend_con->dht_lock_token);
|
||||
friend_con->dht_lock_token = 0;
|
||||
memset(friend_con->dht_temp_pk, 0, CRYPTO_PUBLIC_KEY_SIZE);
|
||||
memzero(friend_con->dht_temp_pk, CRYPTO_PUBLIC_KEY_SIZE);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -9,9 +9,15 @@
|
||||
#ifndef C_TOXCORE_TOXCORE_FRIEND_CONNECTION_H
|
||||
#define C_TOXCORE_TOXCORE_FRIEND_CONNECTION_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "DHT.h"
|
||||
#include "LAN_discovery.h"
|
||||
#include "attributes.h"
|
||||
#include "logger.h"
|
||||
#include "mono_time.h"
|
||||
#include "net_crypto.h"
|
||||
#include "network.h"
|
||||
#include "onion_client.h"
|
||||
|
||||
#define MAX_FRIEND_CONNECTION_CALLBACKS 2
|
||||
@ -39,7 +45,6 @@
|
||||
/** How often we share our TCP relays with each friend connection */
|
||||
#define SHARE_RELAYS_INTERVAL (60 * 2)
|
||||
|
||||
|
||||
typedef enum Friendconn_Status {
|
||||
FRIENDCONN_STATUS_NONE,
|
||||
FRIENDCONN_STATUS_CONNECTING,
|
||||
@ -84,11 +89,11 @@ int get_friendcon_public_keys(uint8_t *real_pk, uint8_t *dht_temp_pk, const Frie
|
||||
non_null()
|
||||
void set_dht_temp_pk(Friend_Connections *fr_c, int friendcon_id, const uint8_t *dht_temp_pk, void *userdata);
|
||||
|
||||
typedef int global_status_cb(void *object, int id, bool status, void *userdata);
|
||||
typedef int global_status_cb(void *object, int friendcon_id, bool status, void *userdata);
|
||||
|
||||
typedef int fc_status_cb(void *object, int id, bool status, void *userdata);
|
||||
typedef int fc_data_cb(void *object, int id, const uint8_t *data, uint16_t length, void *userdata);
|
||||
typedef int fc_lossy_data_cb(void *object, int id, const uint8_t *data, uint16_t length, void *userdata);
|
||||
typedef int fc_status_cb(void *object, int friendcon_id, bool status, void *userdata);
|
||||
typedef int fc_data_cb(void *object, int friendcon_id, const uint8_t *data, uint16_t length, void *userdata);
|
||||
typedef int fc_lossy_data_cb(void *object, int friendcon_id, const uint8_t *data, uint16_t length, void *userdata);
|
||||
|
||||
/** Set global status callback for friend connections. */
|
||||
non_null(1) nullable(2, 3)
|
||||
@ -144,7 +149,7 @@ int send_friend_request_packet(
|
||||
Friend_Connections *fr_c, int friendcon_id, uint32_t nospam_num, const uint8_t *data, uint16_t length);
|
||||
|
||||
typedef int fr_request_cb(
|
||||
void *object, const uint8_t *source_pubkey, const uint8_t *data, uint16_t len, void *userdata);
|
||||
void *object, const uint8_t *source_pubkey, const uint8_t *data, uint16_t length, void *userdata);
|
||||
|
||||
/** @brief Set friend request callback.
|
||||
*
|
||||
@ -156,8 +161,8 @@ void set_friend_request_callback(Friend_Connections *fr_c, fr_request_cb *fr_req
|
||||
/** Create new friend_connections instance. */
|
||||
non_null()
|
||||
Friend_Connections *new_friend_connections(
|
||||
const Logger *logger, const Mono_Time *mono_time, const Network *ns,
|
||||
Onion_Client *onion_c, bool local_discovery_enabled);
|
||||
const Logger *logger, const Mono_Time *mono_time, const Network *ns,
|
||||
Onion_Client *onion_c, bool local_discovery_enabled);
|
||||
|
||||
/** main friend_connections loop. */
|
||||
non_null()
|
||||
@ -173,4 +178,4 @@ non_null() Friend_Conn *get_conn(const Friend_Connections *fr_c, int friendcon_i
|
||||
non_null() int friend_conn_get_onion_friendnum(const Friend_Conn *fc);
|
||||
non_null() const IP_Port *friend_conn_get_dht_ip_port(const Friend_Conn *fc);
|
||||
|
||||
#endif
|
||||
#endif /* C_TOXCORE_TOXCORE_FRIEND_CONNECTION_H */
|
||||
|
@ -11,9 +11,13 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "attributes.h"
|
||||
#include "ccompat.h"
|
||||
#include "crypto_core.h"
|
||||
#include "friend_connection.h"
|
||||
#include "network.h"
|
||||
#include "onion.h"
|
||||
#include "onion_announce.h"
|
||||
#include "onion_client.h"
|
||||
|
||||
/**
|
||||
@ -50,7 +54,6 @@ uint32_t get_nospam(const Friend_Requests *fr)
|
||||
return fr->nospam;
|
||||
}
|
||||
|
||||
|
||||
/** Set the function that will be executed when a friend request for us is received. */
|
||||
void callback_friendrequest(Friend_Requests *fr, fr_friend_request_cb *function, void *object)
|
||||
{
|
||||
@ -114,9 +117,8 @@ int remove_request_received(Friend_Requests *fr, const uint8_t *real_pk)
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
non_null()
|
||||
static int friendreq_handlepacket(void *object, const uint8_t *source_pubkey, const uint8_t *packet, uint16_t length,
|
||||
static int friendreq_handlepacket(void *object, const uint8_t *source_pubkey, const uint8_t *data, uint16_t length,
|
||||
void *userdata)
|
||||
{
|
||||
Friend_Requests *const fr = (Friend_Requests *)object;
|
||||
@ -125,7 +127,7 @@ static int friendreq_handlepacket(void *object, const uint8_t *source_pubkey, co
|
||||
return 1;
|
||||
}
|
||||
|
||||
++packet;
|
||||
++data;
|
||||
--length;
|
||||
|
||||
if (fr->handle_friendrequest_isset == 0) {
|
||||
@ -136,22 +138,22 @@ static int friendreq_handlepacket(void *object, const uint8_t *source_pubkey, co
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (memcmp(packet, &fr->nospam, sizeof(fr->nospam)) != 0) {
|
||||
if (memcmp(data, &fr->nospam, sizeof(fr->nospam)) != 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (fr->filter_function != nullptr) {
|
||||
if (fr->filter_function(source_pubkey, fr->filter_function_userdata) != 0) {
|
||||
if (fr->filter_function(fr->filter_function_userdata, source_pubkey) != 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
addto_receivedlist(fr, source_pubkey);
|
||||
|
||||
const uint32_t message_len = length - sizeof(fr->nospam);
|
||||
const uint16_t message_len = length - sizeof(fr->nospam);
|
||||
VLA(uint8_t, message, message_len + 1);
|
||||
memcpy(message, packet + sizeof(fr->nospam), message_len);
|
||||
message[SIZEOF_VLA(message) - 1] = 0; /* Be sure the message is null terminated. */
|
||||
memcpy(message, data + sizeof(fr->nospam), message_len);
|
||||
message[message_len] = 0; /* Be sure the message is null terminated. TODO(iphydf): But why? */
|
||||
|
||||
fr->handle_friendrequest(fr->handle_friendrequest_object, source_pubkey, message, message_len, userdata);
|
||||
return 0;
|
||||
|
@ -9,6 +9,9 @@
|
||||
#ifndef C_TOXCORE_TOXCORE_FRIEND_REQUESTS_H
|
||||
#define C_TOXCORE_TOXCORE_FRIEND_REQUESTS_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include "attributes.h"
|
||||
#include "friend_connection.h"
|
||||
|
||||
#define MAX_FRIEND_REQUEST_DATA_SIZE (ONION_CLIENT_MAX_DATA_SIZE - (1 + sizeof(uint32_t)))
|
||||
@ -34,7 +37,7 @@ typedef void fr_friend_request_cb(void *object, const uint8_t *public_key, const
|
||||
non_null()
|
||||
void callback_friendrequest(Friend_Requests *fr, fr_friend_request_cb *function, void *object);
|
||||
|
||||
typedef int filter_function_cb(const uint8_t *public_key, void *user_data);
|
||||
typedef int filter_function_cb(void *object, const uint8_t *public_key);
|
||||
|
||||
/** @brief Set the function used to check if a friend request should be displayed to the user or not.
|
||||
* It must return 0 if the request is ok (anything else if it is bad).
|
||||
@ -51,4 +54,4 @@ Friend_Requests *friendreq_new(void);
|
||||
nullable(1)
|
||||
void friendreq_kill(Friend_Requests *fr);
|
||||
|
||||
#endif
|
||||
#endif /* C_TOXCORE_TOXCORE_FRIEND_REQUESTS_H */
|
||||
|
84
external/toxcore/c-toxcore/toxcore/group.c
vendored
84
external/toxcore/c-toxcore/toxcore/group.c
vendored
@ -14,6 +14,7 @@
|
||||
|
||||
#include "DHT.h"
|
||||
#include "Messenger.h"
|
||||
#include "attributes.h"
|
||||
#include "ccompat.h"
|
||||
#include "crypto_core.h"
|
||||
#include "friend_connection.h"
|
||||
@ -243,7 +244,6 @@ static bool is_groupnumber_valid(const Group_Chats *g_c, uint32_t groupnumber)
|
||||
&& g_c->chats[groupnumber].status != GROUPCHAT_STATUS_NONE;
|
||||
}
|
||||
|
||||
|
||||
/** @brief Set the size of the groupchat list to num.
|
||||
*
|
||||
* @retval false if realloc fails.
|
||||
@ -429,7 +429,6 @@ static int get_peer_index(const Group_c *g, uint16_t peer_number)
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
non_null()
|
||||
static uint64_t calculate_comp_value(const uint8_t *pk1, const uint8_t *pk2)
|
||||
{
|
||||
@ -488,7 +487,7 @@ static bool add_to_closest(Group_c *g, const uint8_t *real_pk, const uint8_t *te
|
||||
comp_val = calculate_comp_value(real_pk, g->real_pk);
|
||||
|
||||
for (unsigned int i = DESIRED_CLOSEST / 2; i < DESIRED_CLOSEST; ++i) {
|
||||
uint64_t comp = calculate_comp_value(g->closest_peers[i].real_pk, g->real_pk);
|
||||
const uint64_t comp = calculate_comp_value(g->closest_peers[i].real_pk, g->real_pk);
|
||||
|
||||
if (comp > comp_val && comp > comp_d) {
|
||||
index = i;
|
||||
@ -956,11 +955,6 @@ static bool delpeer(Group_Chats *g_c, uint32_t groupnumber, int peer_index, void
|
||||
return true;
|
||||
}
|
||||
|
||||
static int cmp_u64(uint64_t a, uint64_t b)
|
||||
{
|
||||
return (a > b ? 1 : 0) - (a < b ? 1 : 0);
|
||||
}
|
||||
|
||||
/** Order peers with friends first and with more recently active earlier */
|
||||
non_null()
|
||||
static int cmp_frozen(const void *a, const void *b)
|
||||
@ -972,7 +966,7 @@ static int cmp_frozen(const void *a, const void *b)
|
||||
return pa->is_friend ? -1 : 1;
|
||||
}
|
||||
|
||||
return cmp_u64(pb->last_active, pa->last_active);
|
||||
return cmp_uint(pb->last_active, pa->last_active);
|
||||
}
|
||||
|
||||
/** @brief Delete frozen peers as necessary to ensure at most `g->maxfrozen` remain.
|
||||
@ -1043,7 +1037,6 @@ static bool freeze_peer(Group_Chats *g_c, uint32_t groupnumber, int peer_index,
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/** @brief Set the nick for a peer.
|
||||
*
|
||||
* do_gc_callback indicates whether we want to trigger callbacks set by the client
|
||||
@ -1405,7 +1398,6 @@ static const Group_Peer *peer_in_list(const Group_c *g, uint32_t peernumber, boo
|
||||
return &list[peernumber];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Copy the public key of (frozen, if frozen is true) peernumber who is in
|
||||
* groupnumber to pk.
|
||||
@ -1622,12 +1614,13 @@ static bool send_packet_group_peer(const Friend_Connections *fr_c, int friendcon
|
||||
}
|
||||
|
||||
group_num = net_htons(group_num);
|
||||
VLA(uint8_t, packet, 1 + sizeof(uint16_t) + length);
|
||||
const uint32_t packet_size = 1 + sizeof(uint16_t) + length;
|
||||
VLA(uint8_t, packet, packet_size);
|
||||
packet[0] = packet_id;
|
||||
memcpy(packet + 1, &group_num, sizeof(uint16_t));
|
||||
memcpy(packet + 1 + sizeof(uint16_t), data, length);
|
||||
return write_cryptpacket(friendconn_net_crypto(fr_c), friend_connection_crypt_connection_id(fr_c, friendcon_id), packet,
|
||||
SIZEOF_VLA(packet), false) != -1;
|
||||
return write_cryptpacket(friendconn_net_crypto(fr_c), friend_connection_crypt_connection_id(fr_c, friendcon_id),
|
||||
packet, packet_size, false) != -1;
|
||||
}
|
||||
|
||||
/** @brief Send a group lossy packet to friendcon_id.
|
||||
@ -1644,12 +1637,13 @@ static bool send_lossy_group_peer(const Friend_Connections *fr_c, int friendcon_
|
||||
}
|
||||
|
||||
group_num = net_htons(group_num);
|
||||
VLA(uint8_t, packet, 1 + sizeof(uint16_t) + length);
|
||||
const uint32_t packet_size = 1 + sizeof(uint16_t) + length;
|
||||
VLA(uint8_t, packet, packet_size);
|
||||
packet[0] = packet_id;
|
||||
memcpy(packet + 1, &group_num, sizeof(uint16_t));
|
||||
memcpy(packet + 1 + sizeof(uint16_t), data, length);
|
||||
return send_lossy_cryptpacket(friendconn_net_crypto(fr_c), friend_connection_crypt_connection_id(fr_c, friendcon_id),
|
||||
packet, SIZEOF_VLA(packet)) != -1;
|
||||
packet, packet_size) != -1;
|
||||
}
|
||||
|
||||
/** @brief invite friendnumber to groupnumber.
|
||||
@ -1782,7 +1776,8 @@ static bool send_invite_response(Group_Chats *g_c, int groupnumber, uint32_t fri
|
||||
|
||||
const bool member = g->status == GROUPCHAT_STATUS_CONNECTED;
|
||||
|
||||
VLA(uint8_t, response, member ? INVITE_MEMBER_PACKET_SIZE : INVITE_ACCEPT_PACKET_SIZE);
|
||||
const uint32_t response_size = member ? INVITE_MEMBER_PACKET_SIZE : INVITE_ACCEPT_PACKET_SIZE;
|
||||
VLA(uint8_t, response, response_size);
|
||||
response[0] = member ? INVITE_MEMBER_ID : INVITE_ACCEPT_ID;
|
||||
net_pack_u16(response + 1, groupnumber);
|
||||
memcpy(response + 1 + sizeof(uint16_t), data, length);
|
||||
@ -1791,7 +1786,7 @@ static bool send_invite_response(Group_Chats *g_c, int groupnumber, uint32_t fri
|
||||
net_pack_u16(response + 1 + sizeof(uint16_t) + length, g->peer_number);
|
||||
}
|
||||
|
||||
if (!send_conference_invite_packet(g_c->m, friendnumber, response, SIZEOF_VLA(response))) {
|
||||
if (!send_conference_invite_packet(g_c->m, friendnumber, response, response_size)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -2013,7 +2008,6 @@ static bool group_leave(const Group_Chats *g_c, uint32_t groupnumber, bool perma
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** @brief set the group's title, limited to MAX_NAME_LENGTH.
|
||||
* @retval 0 on success
|
||||
* @retval -1 if groupnumber is invalid.
|
||||
@ -2115,8 +2109,8 @@ static bool get_peer_number(const Group_c *g, const uint8_t *real_pk, uint16_t *
|
||||
}
|
||||
|
||||
non_null(1, 3) nullable(5)
|
||||
static void handle_friend_invite_packet(Messenger *m, uint32_t friendnumber, const uint8_t *data, uint16_t length,
|
||||
void *userdata)
|
||||
static void handle_friend_invite_packet(Messenger *m, uint32_t friend_number, const uint8_t *cookie, uint16_t length,
|
||||
void *user_data)
|
||||
{
|
||||
Group_Chats *g_c = m->conferences_object;
|
||||
|
||||
@ -2124,20 +2118,20 @@ static void handle_friend_invite_packet(Messenger *m, uint32_t friendnumber, con
|
||||
return;
|
||||
}
|
||||
|
||||
switch (data[0]) {
|
||||
switch (cookie[0]) {
|
||||
case INVITE_ID: {
|
||||
if (length != INVITE_PACKET_SIZE) {
|
||||
return;
|
||||
}
|
||||
|
||||
const int groupnumber = get_group_num(g_c, data[1 + sizeof(uint16_t)], data + 1 + sizeof(uint16_t) + 1);
|
||||
const int groupnumber = get_group_num(g_c, cookie[1 + sizeof(uint16_t)], cookie + 1 + sizeof(uint16_t) + 1);
|
||||
|
||||
const uint8_t *invite_data = data + 1;
|
||||
const uint8_t *invite_data = cookie + 1;
|
||||
const uint16_t invite_length = length - 1;
|
||||
|
||||
if (groupnumber == -1) {
|
||||
if (g_c->invite_callback != nullptr) {
|
||||
g_c->invite_callback(m, friendnumber, invite_data[sizeof(uint16_t)], invite_data, invite_length, userdata);
|
||||
g_c->invite_callback(m, friend_number, invite_data[sizeof(uint16_t)], invite_data, invite_length, user_data);
|
||||
}
|
||||
|
||||
return;
|
||||
@ -2145,7 +2139,7 @@ static void handle_friend_invite_packet(Messenger *m, uint32_t friendnumber, con
|
||||
const Group_c *g = get_group_c(g_c, groupnumber);
|
||||
|
||||
if (g != nullptr && g->status == GROUPCHAT_STATUS_CONNECTED) {
|
||||
send_invite_response(g_c, groupnumber, friendnumber, invite_data, invite_length);
|
||||
send_invite_response(g_c, groupnumber, friend_number, invite_data, invite_length);
|
||||
}
|
||||
}
|
||||
|
||||
@ -2154,7 +2148,7 @@ static void handle_friend_invite_packet(Messenger *m, uint32_t friendnumber, con
|
||||
|
||||
case INVITE_ACCEPT_ID:
|
||||
case INVITE_MEMBER_ID: {
|
||||
const bool member = data[0] == INVITE_MEMBER_ID;
|
||||
const bool member = cookie[0] == INVITE_MEMBER_ID;
|
||||
|
||||
if (length != (member ? INVITE_MEMBER_PACKET_SIZE : INVITE_ACCEPT_PACKET_SIZE)) {
|
||||
return;
|
||||
@ -2162,8 +2156,8 @@ static void handle_friend_invite_packet(Messenger *m, uint32_t friendnumber, con
|
||||
|
||||
uint16_t other_groupnum;
|
||||
uint16_t groupnum;
|
||||
net_unpack_u16(data + 1, &other_groupnum);
|
||||
net_unpack_u16(data + 1 + sizeof(uint16_t), &groupnum);
|
||||
net_unpack_u16(cookie + 1, &other_groupnum);
|
||||
net_unpack_u16(cookie + 1 + sizeof(uint16_t), &groupnum);
|
||||
|
||||
Group_c *g = get_group_c(g_c, groupnum);
|
||||
|
||||
@ -2171,18 +2165,18 @@ static void handle_friend_invite_packet(Messenger *m, uint32_t friendnumber, con
|
||||
return;
|
||||
}
|
||||
|
||||
if (data[1 + sizeof(uint16_t) * 2] != g->type) {
|
||||
if (cookie[1 + sizeof(uint16_t) * 2] != g->type) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!group_id_eq(data + 1 + sizeof(uint16_t) * 2 + 1, g->id)) {
|
||||
if (!group_id_eq(cookie + 1 + sizeof(uint16_t) * 2 + 1, g->id)) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint16_t peer_number;
|
||||
|
||||
if (member) {
|
||||
net_unpack_u16(data + 1 + sizeof(uint16_t) * 2 + 1 + GROUP_ID_LENGTH, &peer_number);
|
||||
net_unpack_u16(cookie + 1 + sizeof(uint16_t) * 2 + 1 + GROUP_ID_LENGTH, &peer_number);
|
||||
} else {
|
||||
/* TODO(irungentoo): what if two people enter the group at the
|
||||
* same time and are given the same peer_number by different
|
||||
@ -2201,7 +2195,7 @@ static void handle_friend_invite_packet(Messenger *m, uint32_t friendnumber, con
|
||||
}
|
||||
}
|
||||
|
||||
const int friendcon_id = getfriendcon_id(m, friendnumber);
|
||||
const int friendcon_id = getfriendcon_id(m, friend_number);
|
||||
|
||||
if (friendcon_id == -1) {
|
||||
// TODO(iphydf): Log something?
|
||||
@ -2212,7 +2206,7 @@ static void handle_friend_invite_packet(Messenger *m, uint32_t friendnumber, con
|
||||
uint8_t temp_pk[CRYPTO_PUBLIC_KEY_SIZE];
|
||||
get_friendcon_public_keys(real_pk, temp_pk, g_c->fr_c, friendcon_id);
|
||||
|
||||
addpeer(g_c, groupnum, real_pk, temp_pk, peer_number, userdata, true, true);
|
||||
addpeer(g_c, groupnum, real_pk, temp_pk, peer_number, user_data, true, true);
|
||||
const int connection_index = add_conn_to_groupchat(g_c, friendcon_id, g,
|
||||
GROUPCHAT_CONNECTION_REASON_INTRODUCING, true);
|
||||
|
||||
@ -2386,7 +2380,6 @@ static int handle_packet_rejoin(Group_Chats *g_c, int friendcon_id, const uint8_
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
// we could send title with invite, but then if it changes between sending and accepting inv, joinee won't see it
|
||||
|
||||
/**
|
||||
@ -2400,7 +2393,6 @@ static bool send_peer_introduced(const Group_Chats *g_c, int friendcon_id, uint1
|
||||
return send_packet_group_peer(g_c->fr_c, friendcon_id, PACKET_ID_DIRECT_CONFERENCE, group_num, packet, sizeof(packet));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @retval true on success.
|
||||
* @retval false on failure
|
||||
@ -2457,11 +2449,12 @@ static unsigned int send_peers(const Group_Chats *g_c, const Group_c *g, int fri
|
||||
}
|
||||
|
||||
if (g->title_len > 0) {
|
||||
VLA(uint8_t, title_packet, 1 + g->title_len);
|
||||
const uint32_t title_packet_size = 1 + g->title_len;
|
||||
VLA(uint8_t, title_packet, title_packet_size);
|
||||
title_packet[0] = PEER_TITLE_ID;
|
||||
memcpy(title_packet + 1, g->title, g->title_len);
|
||||
send_packet_group_peer(g_c->fr_c, friendcon_id, PACKET_ID_DIRECT_CONFERENCE, group_num, title_packet,
|
||||
SIZEOF_VLA(title_packet));
|
||||
send_packet_group_peer(g_c->fr_c, friendcon_id, PACKET_ID_DIRECT_CONFERENCE, group_num,
|
||||
title_packet, title_packet_size);
|
||||
}
|
||||
|
||||
return sent;
|
||||
@ -2554,13 +2547,11 @@ static void handle_direct_packet(Group_Chats *g_c, uint32_t groupnumber, const u
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
case PEER_RESPONSE_ID: {
|
||||
handle_send_peers(g_c, groupnumber, data + 1, length - 1, userdata);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
case PEER_TITLE_ID: {
|
||||
if (!g->title_fresh) {
|
||||
settitle(g_c, groupnumber, -1, data + 1, length - 1, userdata);
|
||||
@ -2696,7 +2687,8 @@ static int send_message_group(const Group_Chats *g_c, uint32_t groupnumber, uint
|
||||
return -3;
|
||||
}
|
||||
|
||||
VLA(uint8_t, packet, sizeof(uint16_t) + sizeof(uint32_t) + 1 + len);
|
||||
const uint16_t packet_size = sizeof(uint16_t) + sizeof(uint32_t) + 1 + len;
|
||||
VLA(uint8_t, packet, packet_size);
|
||||
const uint16_t peer_num = net_htons(g->peer_number);
|
||||
memcpy(packet, &peer_num, sizeof(peer_num));
|
||||
|
||||
@ -2715,7 +2707,7 @@ static int send_message_group(const Group_Chats *g_c, uint32_t groupnumber, uint
|
||||
memcpy(packet + sizeof(uint16_t) + sizeof(uint32_t) + 1, data, len);
|
||||
}
|
||||
|
||||
const unsigned int ret = send_message_all_connections(g_c, g, packet, SIZEOF_VLA(packet), -1);
|
||||
const unsigned int ret = send_message_all_connections(g_c, g, packet, packet_size, -1);
|
||||
|
||||
if (ret == 0) {
|
||||
return -4;
|
||||
@ -2768,14 +2760,15 @@ int send_group_lossy_packet(const Group_Chats *g_c, uint32_t groupnumber, const
|
||||
return -1;
|
||||
}
|
||||
|
||||
VLA(uint8_t, packet, sizeof(uint16_t) * 2 + length);
|
||||
const uint16_t packet_size = sizeof(uint16_t) * 2 + length;
|
||||
VLA(uint8_t, packet, packet_size);
|
||||
const uint16_t peer_number = net_htons(g->peer_number);
|
||||
memcpy(packet, &peer_number, sizeof(uint16_t));
|
||||
const uint16_t message_num = net_htons(g->lossy_message_number);
|
||||
memcpy(packet + sizeof(uint16_t), &message_num, sizeof(uint16_t));
|
||||
memcpy(packet + sizeof(uint16_t) * 2, data, length);
|
||||
|
||||
if (send_lossy_all_connections(g_c, g, packet, SIZEOF_VLA(packet), -1) == 0) {
|
||||
if (send_lossy_all_connections(g_c, g, packet, packet_size, -1) == 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -3758,7 +3751,6 @@ bool conferences_load_state_section(Group_Chats *g_c, const uint8_t *data, uint3
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/** Create new groupchat instance. */
|
||||
Group_Chats *new_groupchats(const Mono_Time *mono_time, Messenger *m)
|
||||
{
|
||||
|
11
external/toxcore/c-toxcore/toxcore/group.h
vendored
11
external/toxcore/c-toxcore/toxcore/group.h
vendored
@ -9,7 +9,14 @@
|
||||
#ifndef C_TOXCORE_TOXCORE_GROUP_H
|
||||
#define C_TOXCORE_TOXCORE_GROUP_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "Messenger.h"
|
||||
#include "attributes.h"
|
||||
#include "crypto_core.h"
|
||||
#include "mono_time.h"
|
||||
#include "state.h"
|
||||
|
||||
typedef enum Groupchat_Type {
|
||||
GROUPCHAT_TYPE_TEXT,
|
||||
@ -72,7 +79,6 @@ void g_callback_group_connected(Group_Chats *g_c, g_conference_connected_cb *fun
|
||||
non_null()
|
||||
void g_callback_group_message(Group_Chats *g_c, g_conference_message_cb *function);
|
||||
|
||||
|
||||
/** Set callback function for title changes. */
|
||||
non_null()
|
||||
void g_callback_group_title(Group_Chats *g_c, title_cb *function);
|
||||
@ -217,7 +223,6 @@ int group_action_send(const Group_Chats *g_c, uint32_t groupnumber, const uint8_
|
||||
non_null()
|
||||
int group_title_send(const Group_Chats *g_c, uint32_t groupnumber, const uint8_t *title, uint8_t title_len);
|
||||
|
||||
|
||||
/** @brief return the group's title size.
|
||||
* @retval -1 of groupnumber is invalid.
|
||||
* @retval -2 if title is too long or empty.
|
||||
@ -395,4 +400,4 @@ void do_groupchats(Group_Chats *g_c, void *userdata);
|
||||
nullable(1)
|
||||
void kill_groupchats(Group_Chats *g_c);
|
||||
|
||||
#endif
|
||||
#endif /* C_TOXCORE_TOXCORE_GROUP_H */
|
||||
|
@ -9,7 +9,9 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "DHT.h"
|
||||
#include "attributes.h"
|
||||
#include "ccompat.h"
|
||||
#include "crypto_core.h"
|
||||
#include "logger.h"
|
||||
#include "mono_time.h"
|
||||
#include "network.h"
|
||||
@ -76,7 +78,7 @@ int gca_get_announces(const GC_Announces_List *gc_announces_list, GC_Announce *g
|
||||
for (size_t i = 0; i < announces->index && i < GCA_MAX_SAVED_ANNOUNCES_PER_GC && added_count < max_nodes; ++i) {
|
||||
const size_t index = i % GCA_MAX_SAVED_ANNOUNCES_PER_GC;
|
||||
|
||||
if (memcmp(except_public_key, &announces->peer_announces[index].base_announce.peer_public_key,
|
||||
if (memcmp(except_public_key, announces->peer_announces[index].base_announce.peer_public_key,
|
||||
ENC_PUBLIC_KEY_SIZE) == 0) {
|
||||
continue;
|
||||
}
|
||||
@ -84,7 +86,7 @@ int gca_get_announces(const GC_Announces_List *gc_announces_list, GC_Announce *g
|
||||
bool already_added = false;
|
||||
|
||||
for (size_t j = 0; j < added_count; ++j) {
|
||||
if (memcmp(&gc_announces[j].peer_public_key, &announces->peer_announces[index].base_announce.peer_public_key,
|
||||
if (memcmp(gc_announces[j].peer_public_key, announces->peer_announces[index].base_announce.peer_public_key,
|
||||
ENC_PUBLIC_KEY_SIZE) == 0) {
|
||||
already_added = true;
|
||||
break;
|
||||
@ -341,8 +343,8 @@ int gca_unpack_announces_list(const Logger *log, const uint8_t *data, uint16_t l
|
||||
|
||||
non_null()
|
||||
static GC_Announces *gca_new_announces(
|
||||
GC_Announces_List *gc_announces_list,
|
||||
const GC_Public_Announce *public_announce)
|
||||
GC_Announces_List *gc_announces_list,
|
||||
const GC_Public_Announce *public_announce)
|
||||
{
|
||||
GC_Announces *announces = (GC_Announces *)calloc(1, sizeof(GC_Announces));
|
||||
|
||||
|
@ -10,8 +10,14 @@
|
||||
#define C_TOXCORE_TOXCORE_GROUP_ANNOUNCE_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "DHT.h"
|
||||
#include "attributes.h"
|
||||
#include "crypto_core.h"
|
||||
#include "logger.h"
|
||||
#include "mono_time.h"
|
||||
#include "network.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@ -77,7 +83,6 @@ struct GC_Announces_List {
|
||||
uint64_t last_timeout_check;
|
||||
};
|
||||
|
||||
|
||||
/** @brief Returns a new group announces list.
|
||||
*
|
||||
* The caller is responsible for freeing the memory with `kill_gca`.
|
||||
@ -212,7 +217,7 @@ non_null()
|
||||
bool gca_is_valid_announce(const GC_Announce *announce);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif // C_TOXCORE_TOXCORE_GROUP_ANNOUNCE_H
|
||||
#endif /* C_TOXCORE_TOXCORE_GROUP_ANNOUNCE_H */
|
||||
|
@ -5,7 +5,7 @@
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "../testing/fuzzing/fuzz_support.h"
|
||||
#include "../testing/fuzzing/fuzz_support.hh"
|
||||
#include "mem_test_util.hh"
|
||||
|
||||
namespace {
|
||||
|
@ -2,8 +2,12 @@
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "DHT.h"
|
||||
#include "crypto_core.h"
|
||||
#include "logger.h"
|
||||
#include "mem_test_util.hh"
|
||||
#include "mono_time.h"
|
||||
#include "network.h"
|
||||
|
||||
namespace {
|
||||
|
||||
|
481
external/toxcore/c-toxcore/toxcore/group_chats.c
vendored
481
external/toxcore/c-toxcore/toxcore/group_chats.c
vendored
File diff suppressed because it is too large
Load Diff
44
external/toxcore/c-toxcore/toxcore/group_chats.h
vendored
44
external/toxcore/c-toxcore/toxcore/group_chats.h
vendored
@ -14,12 +14,15 @@
|
||||
#include <stdint.h>
|
||||
|
||||
#include "TCP_connection.h"
|
||||
#include "attributes.h"
|
||||
#include "bin_pack.h"
|
||||
#include "bin_unpack.h"
|
||||
#include "crypto_core.h"
|
||||
#include "group_announce.h"
|
||||
#include "group_common.h"
|
||||
#include "group_connection.h"
|
||||
#include "logger.h"
|
||||
#include "network.h"
|
||||
|
||||
#define GC_PING_TIMEOUT 12
|
||||
#define GC_SEND_IP_PORT_INTERVAL (GC_PING_TIMEOUT * 5)
|
||||
@ -178,9 +181,9 @@ int gc_send_message(const GC_Chat *chat, const uint8_t *message, uint16_t length
|
||||
* Returns -5 if the sender has the observer role.
|
||||
* Returns -6 if the packet fails to send.
|
||||
*/
|
||||
non_null()
|
||||
int gc_send_private_message(const GC_Chat *chat, uint32_t peer_id, uint8_t type, const uint8_t *message,
|
||||
uint16_t length);
|
||||
non_null(1, 4) nullable(6)
|
||||
int gc_send_private_message(const GC_Chat *chat, GC_Peer_Id peer_id, uint8_t type, const uint8_t *message,
|
||||
uint16_t length, uint32_t *message_id);
|
||||
|
||||
/** @brief Sends a custom packet to the group. If lossless is true, the packet will be lossless.
|
||||
*
|
||||
@ -189,8 +192,7 @@ int gc_send_private_message(const GC_Chat *chat, uint32_t peer_id, uint8_t type,
|
||||
* Returns 0 on success.
|
||||
* Returns -1 if the message is too long.
|
||||
* Returns -2 if the message pointer is NULL or length is zero.
|
||||
* Returns -3 if the sender has the observer role.
|
||||
* Returns -4 if the packet did not successfully send to any peer.
|
||||
* Returns -3 if the packet did not successfully send to any peer.
|
||||
*/
|
||||
non_null()
|
||||
int gc_send_custom_packet(const GC_Chat *chat, bool lossless, const uint8_t *data, uint16_t length);
|
||||
@ -203,11 +205,10 @@ int gc_send_custom_packet(const GC_Chat *chat, bool lossless, const uint8_t *dat
|
||||
* @retval -1 if the message is too long.
|
||||
* @retval -2 if the message pointer is NULL or length is zero.
|
||||
* @retval -3 if the supplied peer_id does not designate a valid peer.
|
||||
* @retval -4 if the sender has the observer role.
|
||||
* @retval -5 if the packet fails to send.
|
||||
* @retval -4 if the packet fails to send.
|
||||
*/
|
||||
non_null()
|
||||
int gc_send_custom_private_packet(const GC_Chat *chat, bool lossless, uint32_t peer_id, const uint8_t *message,
|
||||
int gc_send_custom_private_packet(const GC_Chat *chat, bool lossless, GC_Peer_Id peer_id, const uint8_t *message,
|
||||
uint16_t length);
|
||||
|
||||
/** @brief Sets ignore for peer_id.
|
||||
@ -217,7 +218,7 @@ int gc_send_custom_private_packet(const GC_Chat *chat, bool lossless, uint32_t p
|
||||
* Returns -2 if the caller attempted to ignore himself.
|
||||
*/
|
||||
non_null()
|
||||
int gc_set_ignore(const GC_Chat *chat, uint32_t peer_id, bool ignore);
|
||||
int gc_set_ignore(const GC_Chat *chat, GC_Peer_Id peer_id, bool ignore);
|
||||
|
||||
/** @brief Sets the group topic and broadcasts it to the group.
|
||||
*
|
||||
@ -342,7 +343,7 @@ uint8_t gc_get_self_status(const GC_Chat *chat);
|
||||
|
||||
/** @brief Returns your own peer id. */
|
||||
non_null()
|
||||
uint32_t gc_get_self_peer_id(const GC_Chat *chat);
|
||||
GC_Peer_Id gc_get_self_peer_id(const GC_Chat *chat);
|
||||
|
||||
/** @brief Copies self public key to `public_key`.
|
||||
*
|
||||
@ -365,7 +366,7 @@ void gc_get_self_public_key(const GC_Chat *chat, uint8_t *public_key);
|
||||
* Returns false if peer_id is invalid.
|
||||
*/
|
||||
non_null(1) nullable(3)
|
||||
bool gc_get_peer_nick(const GC_Chat *chat, uint32_t peer_id, uint8_t *name);
|
||||
bool gc_get_peer_nick(const GC_Chat *chat, GC_Peer_Id peer_id, uint8_t *name);
|
||||
|
||||
/** @brief Returns the length of the nick for the peer designated by `peer_id`.
|
||||
* Returns -1 if peer_id is invalid.
|
||||
@ -374,7 +375,7 @@ bool gc_get_peer_nick(const GC_Chat *chat, uint32_t peer_id, uint8_t *name);
|
||||
* nick_change callback.
|
||||
*/
|
||||
non_null()
|
||||
int gc_get_peer_nick_size(const GC_Chat *chat, uint32_t peer_id);
|
||||
int gc_get_peer_nick_size(const GC_Chat *chat, GC_Peer_Id peer_id);
|
||||
|
||||
/** @brief Copies peer_id's public key to `public_key`.
|
||||
*
|
||||
@ -389,13 +390,13 @@ int gc_get_peer_nick_size(const GC_Chat *chat, uint32_t peer_id);
|
||||
* Returns -2 if `public_key` is null.
|
||||
*/
|
||||
non_null(1) nullable(3)
|
||||
int gc_get_peer_public_key_by_peer_id(const GC_Chat *chat, uint32_t peer_id, uint8_t *public_key);
|
||||
int gc_get_peer_public_key_by_peer_id(const GC_Chat *chat, GC_Peer_Id peer_id, uint8_t *public_key);
|
||||
|
||||
/** @brief Returns the length of the IP address for the peer designated by `peer_id`.
|
||||
* Returns -1 if peer_id is invalid.
|
||||
*/
|
||||
non_null()
|
||||
int gc_get_peer_ip_address_size(const GC_Chat *chat, uint32_t peer_id);
|
||||
int gc_get_peer_ip_address_size(const GC_Chat *chat, GC_Peer_Id peer_id);
|
||||
|
||||
/** @brief Copies peer_id's IP address to `ip_addr`.
|
||||
*
|
||||
@ -412,7 +413,7 @@ int gc_get_peer_ip_address_size(const GC_Chat *chat, uint32_t peer_id);
|
||||
* Returns -2 if `ip_addr` is null.
|
||||
*/
|
||||
non_null(1) nullable(3)
|
||||
int gc_get_peer_ip_address(const GC_Chat *chat, uint32_t peer_id, uint8_t *ip_addr);
|
||||
int gc_get_peer_ip_address(const GC_Chat *chat, GC_Peer_Id peer_id, uint8_t *ip_addr);
|
||||
|
||||
/** @brief Gets the connection status for peer associated with `peer_id`.
|
||||
*
|
||||
@ -426,7 +427,7 @@ int gc_get_peer_ip_address(const GC_Chat *chat, uint32_t peer_id, uint8_t *ip_ad
|
||||
* Note: Return values must correspond to Tox_Connection enum in API.
|
||||
*/
|
||||
non_null()
|
||||
unsigned int gc_get_peer_connection_status(const GC_Chat *chat, uint32_t peer_id);
|
||||
unsigned int gc_get_peer_connection_status(const GC_Chat *chat, GC_Peer_Id peer_id);
|
||||
|
||||
/** @brief Sets the caller's status to `status`.
|
||||
*
|
||||
@ -444,7 +445,7 @@ int gc_set_self_status(const Messenger *m, int group_number, Group_Peer_Status s
|
||||
* callback.
|
||||
*/
|
||||
non_null()
|
||||
uint8_t gc_get_status(const GC_Chat *chat, uint32_t peer_id);
|
||||
uint8_t gc_get_status(const GC_Chat *chat, GC_Peer_Id peer_id);
|
||||
|
||||
/** @brief Returns the group role of peer designated by `peer_id`.
|
||||
* Returns UINT8_MAX on failure.
|
||||
@ -452,7 +453,7 @@ uint8_t gc_get_status(const GC_Chat *chat, uint32_t peer_id);
|
||||
* The role returned is equal to the last role received through the moderation callback.
|
||||
*/
|
||||
non_null()
|
||||
uint8_t gc_get_role(const GC_Chat *chat, uint32_t peer_id);
|
||||
uint8_t gc_get_role(const GC_Chat *chat, GC_Peer_Id peer_id);
|
||||
|
||||
/** @brief Sets the role of peer_id. role must be one of: GR_MODERATOR, GR_USER, GR_OBSERVER
|
||||
*
|
||||
@ -465,7 +466,7 @@ uint8_t gc_get_role(const GC_Chat *chat, uint32_t peer_id);
|
||||
* Returns -6 if the caller attempted to kick himself.
|
||||
*/
|
||||
non_null()
|
||||
int gc_set_peer_role(const Messenger *m, int group_number, uint32_t peer_id, Group_Role new_role);
|
||||
int gc_set_peer_role(const Messenger *m, int group_number, GC_Peer_Id peer_id, Group_Role new_role);
|
||||
|
||||
/** @brief Sets the group password and distributes the new shared state to the group.
|
||||
*
|
||||
@ -560,7 +561,7 @@ int gc_founder_set_max_peers(GC_Chat *chat, uint16_t max_peers);
|
||||
* Returns -6 if the caller attempted to kick himself.
|
||||
*/
|
||||
non_null()
|
||||
int gc_kick_peer(const Messenger *m, int group_number, uint32_t peer_id);
|
||||
int gc_kick_peer(const Messenger *m, int group_number, GC_Peer_Id peer_id);
|
||||
|
||||
/** @brief Copies the chat_id to dest. If dest is null this function has no effect.
|
||||
*
|
||||
@ -569,7 +570,6 @@ int gc_kick_peer(const Messenger *m, int group_number, uint32_t peer_id);
|
||||
non_null(1) nullable(2)
|
||||
void gc_get_chat_id(const GC_Chat *chat, uint8_t *dest);
|
||||
|
||||
|
||||
/** Group callbacks */
|
||||
non_null(1) nullable(2) void gc_callback_message(const Messenger *m, gc_message_cb *function);
|
||||
non_null(1) nullable(2) void gc_callback_private_message(const Messenger *m, gc_private_message_cb *function);
|
||||
@ -806,4 +806,4 @@ GC_Chat *gc_get_group_by_public_key(const GC_Session *c, const uint8_t *public_k
|
||||
non_null()
|
||||
int gc_add_peers_from_announces(GC_Chat *chat, const GC_Announce *announces, uint8_t gc_announces_count);
|
||||
|
||||
#endif // C_TOXCORE_TOXCORE_GROUP_CHATS_H
|
||||
#endif /* C_TOXCORE_TOXCORE_GROUP_CHATS_H */
|
||||
|
@ -14,7 +14,13 @@
|
||||
|
||||
#include "DHT.h"
|
||||
#include "TCP_connection.h"
|
||||
#include "attributes.h"
|
||||
#include "crypto_core.h"
|
||||
#include "group_moderation.h"
|
||||
#include "logger.h"
|
||||
#include "mem.h"
|
||||
#include "mono_time.h"
|
||||
#include "network.h"
|
||||
|
||||
#define MAX_GC_PART_MESSAGE_SIZE 128
|
||||
#define MAX_GC_NICK_SIZE 128
|
||||
@ -73,7 +79,7 @@ typedef struct GC_Exit_Info {
|
||||
} GC_Exit_Info;
|
||||
|
||||
typedef struct GC_PeerAddress {
|
||||
uint8_t public_key[EXT_PUBLIC_KEY_SIZE];
|
||||
Extended_Public_Key public_key;
|
||||
IP_Port ip_port;
|
||||
} GC_PeerAddress;
|
||||
|
||||
@ -133,6 +139,7 @@ typedef struct GC_Connection {
|
||||
bool pending_key_rotation_request;
|
||||
|
||||
bool pending_delete; /* true if this peer has been marked for deletion */
|
||||
bool delete_this_iteration; /* true if this peer should be deleted this do_gc() iteration*/
|
||||
GC_Exit_Info exit_info;
|
||||
} GC_Connection;
|
||||
|
||||
@ -210,6 +217,15 @@ typedef struct GC_TimedOutPeer {
|
||||
uint64_t last_reconn_try; // the last time we tried to establish a new connection
|
||||
} GC_TimedOutPeer;
|
||||
|
||||
typedef bitwise uint32_t GC_Peer_Id_Value;
|
||||
|
||||
typedef struct GC_Peer_Id {
|
||||
GC_Peer_Id_Value value;
|
||||
} GC_Peer_Id;
|
||||
|
||||
GC_Peer_Id gc_peer_id_from_int(uint32_t value);
|
||||
uint32_t gc_peer_id_to_int(GC_Peer_Id peer_id);
|
||||
|
||||
typedef struct GC_Peer {
|
||||
/* Below state is sent to other peers in peer info exchange */
|
||||
uint8_t nick[MAX_GC_NICK_SIZE];
|
||||
@ -218,7 +234,7 @@ typedef struct GC_Peer {
|
||||
|
||||
/* Below state is local only */
|
||||
Group_Role role;
|
||||
uint32_t peer_id; // permanent ID (used for the public API)
|
||||
GC_Peer_Id peer_id; // permanent ID (used for the public API)
|
||||
bool ignore;
|
||||
|
||||
GC_Connection gconn;
|
||||
@ -226,7 +242,7 @@ typedef struct GC_Peer {
|
||||
|
||||
typedef struct GC_SharedState {
|
||||
uint32_t version;
|
||||
uint8_t founder_public_key[EXT_PUBLIC_KEY_SIZE];
|
||||
Extended_Public_Key founder_public_key;
|
||||
uint16_t maxpeers;
|
||||
uint16_t group_name_len;
|
||||
uint8_t group_name[MAX_GC_GROUP_NAME_SIZE];
|
||||
@ -281,11 +297,11 @@ typedef struct GC_Chat {
|
||||
uint32_t numpeers;
|
||||
int group_number;
|
||||
|
||||
uint8_t chat_public_key[EXT_PUBLIC_KEY_SIZE]; // the chat_id is the sig portion
|
||||
uint8_t chat_secret_key[EXT_SECRET_KEY_SIZE]; // only used by the founder
|
||||
Extended_Public_Key chat_public_key; // the chat_id is the sig portion
|
||||
Extended_Secret_Key chat_secret_key; // only used by the founder
|
||||
|
||||
uint8_t self_public_key[EXT_PUBLIC_KEY_SIZE];
|
||||
uint8_t self_secret_key[EXT_SECRET_KEY_SIZE];
|
||||
Extended_Public_Key self_public_key;
|
||||
Extended_Secret_Key self_secret_key;
|
||||
|
||||
uint64_t time_connected;
|
||||
uint64_t last_ping_interval;
|
||||
@ -320,6 +336,8 @@ typedef struct GC_Chat {
|
||||
|
||||
uint8_t m_group_public_key[CRYPTO_PUBLIC_KEY_SIZE]; // public key for group's messenger friend connection
|
||||
int friend_connection_id; // identifier for group's messenger friend connection
|
||||
|
||||
bool flag_exit; // true if the group will be deleted after the next do_gc() iteration
|
||||
} GC_Chat;
|
||||
|
||||
#ifndef MESSENGER_DEFINED
|
||||
@ -327,34 +345,34 @@ typedef struct GC_Chat {
|
||||
typedef struct Messenger Messenger;
|
||||
#endif /* MESSENGER_DEFINED */
|
||||
|
||||
typedef void gc_message_cb(const Messenger *m, uint32_t group_number, uint32_t peer_id, unsigned int type,
|
||||
const uint8_t *data, size_t length, uint32_t message_id, void *user_data);
|
||||
typedef void gc_private_message_cb(const Messenger *m, uint32_t group_number, uint32_t peer_id, unsigned int type,
|
||||
const uint8_t *data, size_t length, void *user_data);
|
||||
typedef void gc_custom_packet_cb(const Messenger *m, uint32_t group_number, uint32_t peer_id, const uint8_t *data,
|
||||
typedef void gc_message_cb(const Messenger *m, uint32_t group_number, GC_Peer_Id peer_id, unsigned int type,
|
||||
const uint8_t *message, size_t length, uint32_t message_id, void *user_data);
|
||||
typedef void gc_private_message_cb(const Messenger *m, uint32_t group_number, GC_Peer_Id peer_id, unsigned int type,
|
||||
const uint8_t *message, size_t length, uint32_t message_id, void *user_data);
|
||||
typedef void gc_custom_packet_cb(const Messenger *m, uint32_t group_number, GC_Peer_Id peer_id, const uint8_t *data,
|
||||
size_t length, void *user_data);
|
||||
typedef void gc_custom_private_packet_cb(const Messenger *m, uint32_t group_number, uint32_t peer_id,
|
||||
const uint8_t *data,
|
||||
size_t length, void *user_data);
|
||||
typedef void gc_moderation_cb(const Messenger *m, uint32_t group_number, uint32_t peer_id, uint32_t target_peer,
|
||||
unsigned int mod_event, void *user_data);
|
||||
typedef void gc_nick_change_cb(const Messenger *m, uint32_t group_number, uint32_t peer_id, const uint8_t *data,
|
||||
typedef void gc_custom_private_packet_cb(const Messenger *m, uint32_t group_number, GC_Peer_Id peer_id,
|
||||
const uint8_t *data, size_t length, void *user_data);
|
||||
typedef void gc_moderation_cb(const Messenger *m, uint32_t group_number, GC_Peer_Id source_peer_number,
|
||||
GC_Peer_Id target_peer_number, unsigned int mod_type, void *user_data);
|
||||
typedef void gc_nick_change_cb(const Messenger *m, uint32_t group_number, GC_Peer_Id peer_id, const uint8_t *name,
|
||||
size_t length, void *user_data);
|
||||
typedef void gc_status_change_cb(const Messenger *m, uint32_t group_number, uint32_t peer_id, unsigned int status,
|
||||
typedef void gc_status_change_cb(const Messenger *m, uint32_t group_number, GC_Peer_Id peer_id, unsigned int status,
|
||||
void *user_data);
|
||||
typedef void gc_topic_change_cb(const Messenger *m, uint32_t group_number, uint32_t peer_id, const uint8_t *data,
|
||||
typedef void gc_topic_change_cb(const Messenger *m, uint32_t group_number, GC_Peer_Id peer_id, const uint8_t *topic,
|
||||
size_t length, void *user_data);
|
||||
typedef void gc_topic_lock_cb(const Messenger *m, uint32_t group_number, unsigned int topic_lock, void *user_data);
|
||||
typedef void gc_voice_state_cb(const Messenger *m, uint32_t group_number, unsigned int voice_state, void *user_data);
|
||||
typedef void gc_peer_limit_cb(const Messenger *m, uint32_t group_number, uint32_t max_peers, void *user_data);
|
||||
typedef void gc_privacy_state_cb(const Messenger *m, uint32_t group_number, unsigned int state, void *user_data);
|
||||
typedef void gc_password_cb(const Messenger *m, uint32_t group_number, const uint8_t *data, size_t length,
|
||||
typedef void gc_peer_limit_cb(const Messenger *m, uint32_t group_number, uint32_t peer_limit, void *user_data);
|
||||
typedef void gc_privacy_state_cb(const Messenger *m, uint32_t group_number, unsigned int privacy_state, void *user_data);
|
||||
typedef void gc_password_cb(const Messenger *m, uint32_t group_number, const uint8_t *password, size_t length,
|
||||
void *user_data);
|
||||
typedef void gc_peer_join_cb(const Messenger *m, uint32_t group_number, uint32_t peer_id, void *user_data);
|
||||
typedef void gc_peer_exit_cb(const Messenger *m, uint32_t group_number, uint32_t peer_id, unsigned int exit_type,
|
||||
const uint8_t *nick, size_t nick_len, const uint8_t *data, size_t length, void *user_data);
|
||||
typedef void gc_peer_join_cb(const Messenger *m, uint32_t group_number, GC_Peer_Id peer_id, void *user_data);
|
||||
typedef void gc_peer_exit_cb(const Messenger *m, uint32_t group_number, GC_Peer_Id peer_id, unsigned int exit_type,
|
||||
const uint8_t *name, size_t name_length, const uint8_t *part_message, size_t length,
|
||||
void *user_data);
|
||||
typedef void gc_self_join_cb(const Messenger *m, uint32_t group_number, void *user_data);
|
||||
typedef void gc_rejected_cb(const Messenger *m, uint32_t group_number, unsigned int type, void *user_data);
|
||||
typedef void gc_rejected_cb(const Messenger *m, uint32_t group_number, unsigned int fail_type, void *user_data);
|
||||
|
||||
typedef struct GC_Session {
|
||||
Messenger *messenger;
|
||||
@ -410,4 +428,4 @@ int unpack_gc_saved_peers(GC_Chat *chat, const uint8_t *data, uint16_t length);
|
||||
non_null(1, 2) nullable(4)
|
||||
int pack_gc_saved_peers(const GC_Chat *chat, uint8_t *data, uint16_t length, uint16_t *processed);
|
||||
|
||||
#endif // C_TOXCORE_TOXCORE_GROUP_COMMON_H
|
||||
#endif /* C_TOXCORE_TOXCORE_GROUP_COMMON_H */
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
#include "DHT.h"
|
||||
#include "TCP_connection.h"
|
||||
#include "attributes.h"
|
||||
#include "ccompat.h"
|
||||
#include "crypto_core.h"
|
||||
#include "group_chats.h"
|
||||
@ -40,9 +41,7 @@ static bool array_entry_is_empty(const GC_Message_Array_Entry *array_entry)
|
||||
non_null()
|
||||
static void clear_array_entry(GC_Message_Array_Entry *const array_entry)
|
||||
{
|
||||
if (array_entry->data != nullptr) {
|
||||
free(array_entry->data);
|
||||
}
|
||||
free(array_entry->data);
|
||||
|
||||
*array_entry = (GC_Message_Array_Entry) {
|
||||
nullptr
|
||||
@ -93,7 +92,7 @@ void gcc_set_recv_message_id(GC_Connection *gconn, uint64_t id)
|
||||
*/
|
||||
non_null(1, 2, 3) nullable(4)
|
||||
static bool create_array_entry(const Logger *log, const Mono_Time *mono_time, GC_Message_Array_Entry *array_entry,
|
||||
const uint8_t *data, uint16_t length, uint8_t packet_type, uint64_t message_id)
|
||||
const uint8_t *data, uint16_t length, uint8_t packet_type, uint64_t message_id)
|
||||
{
|
||||
if (!array_entry_is_empty(array_entry)) {
|
||||
LOGGER_WARNING(log, "Failed to create array entry; entry is not empty.");
|
||||
@ -182,7 +181,6 @@ int gcc_send_lossless_packet(const GC_Chat *chat, GC_Connection *gconn, const ui
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
bool gcc_send_lossless_packet_fragments(const GC_Chat *chat, GC_Connection *gconn, const uint8_t *data,
|
||||
uint16_t length, uint8_t packet_type)
|
||||
{
|
||||
@ -441,7 +439,7 @@ int gcc_handle_packet_fragment(const GC_Session *c, GC_Chat *chat, uint32_t peer
|
||||
}
|
||||
|
||||
uint8_t sender_pk[ENC_PUBLIC_KEY_SIZE];
|
||||
memcpy(sender_pk, get_enc_key(gconn->addr.public_key), ENC_PUBLIC_KEY_SIZE);
|
||||
memcpy(sender_pk, get_enc_key(&gconn->addr.public_key), ENC_PUBLIC_KEY_SIZE);
|
||||
|
||||
uint8_t *payload = nullptr;
|
||||
const uint16_t processed_len = reassemble_packet(chat->log, gconn, &payload, message_id);
|
||||
@ -515,7 +513,7 @@ static bool process_recv_array_entry(const GC_Session *c, GC_Chat *chat, GC_Conn
|
||||
GC_Message_Array_Entry *const array_entry, void *userdata)
|
||||
{
|
||||
uint8_t sender_pk[ENC_PUBLIC_KEY_SIZE];
|
||||
memcpy(sender_pk, get_enc_key(gconn->addr.public_key), ENC_PUBLIC_KEY_SIZE);
|
||||
memcpy(sender_pk, get_enc_key(&gconn->addr.public_key), ENC_PUBLIC_KEY_SIZE);
|
||||
|
||||
const bool ret = handle_gc_lossless_helper(c, chat, peer_number, array_entry->data, array_entry->data_length,
|
||||
array_entry->packet_type, userdata);
|
||||
@ -631,7 +629,7 @@ int gcc_encrypt_and_send_lossless_packet(const GC_Chat *chat, const GC_Connectio
|
||||
}
|
||||
|
||||
const int enc_len = group_packet_wrap(
|
||||
chat->log, chat->rng, chat->self_public_key, gconn->session_shared_key, packet,
|
||||
chat->log, chat->rng, chat->self_public_key.enc, gconn->session_shared_key, packet,
|
||||
packet_size, data, length, message_id, packet_type, NET_PACKET_GC_LOSSLESS);
|
||||
|
||||
if (enc_len < 0) {
|
||||
|
@ -10,7 +10,14 @@
|
||||
#ifndef C_TOXCORE_TOXCORE_GROUP_CONNECTION_H
|
||||
#define C_TOXCORE_TOXCORE_GROUP_CONNECTION_H
|
||||
|
||||
#include "DHT.h"
|
||||
#include "TCP_connection.h"
|
||||
#include "attributes.h"
|
||||
#include "crypto_core.h"
|
||||
#include "group_common.h"
|
||||
#include "logger.h"
|
||||
#include "mono_time.h"
|
||||
#include "network.h"
|
||||
|
||||
/* Max number of TCP relays we share with a peer on handshake */
|
||||
#define GCC_MAX_TCP_SHARED_RELAYS 3
|
||||
@ -166,7 +173,6 @@ non_null()
|
||||
bool gcc_send_lossless_packet_fragments(const GC_Chat *chat, GC_Connection *gconn, const uint8_t *data,
|
||||
uint16_t length, uint8_t packet_type);
|
||||
|
||||
|
||||
/** @brief Encrypts `data` of `length` bytes, designated by `message_id`, using the shared key
|
||||
* associated with `gconn` and sends lossless packet over the wire.
|
||||
*
|
||||
@ -178,7 +184,7 @@ bool gcc_send_lossless_packet_fragments(const GC_Chat *chat, GC_Connection *gcon
|
||||
*/
|
||||
non_null(1, 2) nullable(3)
|
||||
int gcc_encrypt_and_send_lossless_packet(const GC_Chat *chat, const GC_Connection *gconn, const uint8_t *data,
|
||||
uint16_t length, uint64_t message_id, uint8_t packet_type);
|
||||
uint16_t length, uint64_t message_id, uint8_t packet_type);
|
||||
|
||||
/** @brief Called when a peer leaves the group. */
|
||||
non_null()
|
||||
@ -188,4 +194,4 @@ void gcc_peer_cleanup(GC_Connection *gconn);
|
||||
non_null()
|
||||
void gcc_cleanup(const GC_Chat *chat);
|
||||
|
||||
#endif // C_TOXCORE_TOXCORE_GROUP_CONNECTION_H
|
||||
#endif /* C_TOXCORE_TOXCORE_GROUP_CONNECTION_H */
|
||||
|
@ -16,6 +16,7 @@
|
||||
#include <time.h>
|
||||
|
||||
#include "DHT.h"
|
||||
#include "attributes.h"
|
||||
#include "ccompat.h"
|
||||
#include "crypto_core.h"
|
||||
#include "logger.h"
|
||||
@ -93,7 +94,7 @@ void mod_list_get_data_hash(uint8_t *hash, const uint8_t *packed_mod_list, uint1
|
||||
bool mod_list_make_hash(const Moderation *moderation, uint8_t *hash)
|
||||
{
|
||||
if (moderation->num_mods == 0) {
|
||||
memset(hash, 0, MOD_MODERATION_HASH_SIZE);
|
||||
memzero(hash, MOD_MODERATION_HASH_SIZE);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -235,15 +236,15 @@ uint16_t sanctions_creds_pack(const Mod_Sanction_Creds *creds, uint8_t *data)
|
||||
{
|
||||
uint16_t packed_len = 0;
|
||||
|
||||
net_pack_u32(data + packed_len, creds->version);
|
||||
net_pack_u32(&data[packed_len], creds->version);
|
||||
packed_len += sizeof(uint32_t);
|
||||
memcpy(data + packed_len, creds->hash, MOD_SANCTION_HASH_SIZE);
|
||||
memcpy(&data[packed_len], creds->hash, MOD_SANCTION_HASH_SIZE);
|
||||
packed_len += MOD_SANCTION_HASH_SIZE;
|
||||
net_pack_u16(data + packed_len, creds->checksum);
|
||||
net_pack_u16(&data[packed_len], creds->checksum);
|
||||
packed_len += sizeof(uint16_t);
|
||||
memcpy(data + packed_len, creds->sig_pk, SIG_PUBLIC_KEY_SIZE);
|
||||
memcpy(&data[packed_len], creds->sig_pk, SIG_PUBLIC_KEY_SIZE);
|
||||
packed_len += SIG_PUBLIC_KEY_SIZE;
|
||||
memcpy(data + packed_len, creds->sig, SIGNATURE_SIZE);
|
||||
memcpy(&data[packed_len], creds->sig, SIGNATURE_SIZE);
|
||||
packed_len += SIGNATURE_SIZE;
|
||||
|
||||
return packed_len;
|
||||
@ -267,11 +268,11 @@ int sanctions_list_pack(uint8_t *data, uint16_t length, const Mod_Sanction *sanc
|
||||
return -1;
|
||||
}
|
||||
|
||||
memcpy(data + packed_len, &sanctions[i].type, sizeof(uint8_t));
|
||||
memcpy(&data[packed_len], &sanctions[i].type, sizeof(uint8_t));
|
||||
packed_len += sizeof(uint8_t);
|
||||
memcpy(data + packed_len, sanctions[i].setter_public_sig_key, SIG_PUBLIC_KEY_SIZE);
|
||||
memcpy(&data[packed_len], sanctions[i].setter_public_sig_key, SIG_PUBLIC_KEY_SIZE);
|
||||
packed_len += SIG_PUBLIC_KEY_SIZE;
|
||||
net_pack_u64(data + packed_len, sanctions[i].time_set);
|
||||
net_pack_u64(&data[packed_len], sanctions[i].time_set);
|
||||
packed_len += TIME_STAMP_SIZE;
|
||||
|
||||
const uint8_t sanctions_type = sanctions[i].type;
|
||||
@ -281,7 +282,7 @@ int sanctions_list_pack(uint8_t *data, uint16_t length, const Mod_Sanction *sanc
|
||||
return -1;
|
||||
}
|
||||
|
||||
memcpy(data + packed_len, sanctions[i].target_public_enc_key, ENC_PUBLIC_KEY_SIZE);
|
||||
memcpy(&data[packed_len], sanctions[i].target_public_enc_key, ENC_PUBLIC_KEY_SIZE);
|
||||
packed_len += ENC_PUBLIC_KEY_SIZE;
|
||||
} else {
|
||||
return -1;
|
||||
@ -292,7 +293,7 @@ int sanctions_list_pack(uint8_t *data, uint16_t length, const Mod_Sanction *sanc
|
||||
}
|
||||
|
||||
/* Signature must be packed last */
|
||||
memcpy(data + packed_len, sanctions[i].signature, SIGNATURE_SIZE);
|
||||
memcpy(&data[packed_len], sanctions[i].signature, SIGNATURE_SIZE);
|
||||
packed_len += SIGNATURE_SIZE;
|
||||
}
|
||||
|
||||
@ -304,7 +305,7 @@ int sanctions_list_pack(uint8_t *data, uint16_t length, const Mod_Sanction *sanc
|
||||
return -1;
|
||||
}
|
||||
|
||||
const uint16_t cred_len = sanctions_creds_pack(creds, data + packed_len);
|
||||
const uint16_t cred_len = sanctions_creds_pack(creds, &data[packed_len]);
|
||||
|
||||
if (cred_len != MOD_SANCTIONS_CREDS_SIZE) {
|
||||
return -1;
|
||||
@ -317,15 +318,15 @@ uint16_t sanctions_creds_unpack(Mod_Sanction_Creds *creds, const uint8_t *data)
|
||||
{
|
||||
uint16_t len_processed = 0;
|
||||
|
||||
net_unpack_u32(data + len_processed, &creds->version);
|
||||
net_unpack_u32(&data[len_processed], &creds->version);
|
||||
len_processed += sizeof(uint32_t);
|
||||
memcpy(creds->hash, data + len_processed, MOD_SANCTION_HASH_SIZE);
|
||||
memcpy(creds->hash, &data[len_processed], MOD_SANCTION_HASH_SIZE);
|
||||
len_processed += MOD_SANCTION_HASH_SIZE;
|
||||
net_unpack_u16(data + len_processed, &creds->checksum);
|
||||
net_unpack_u16(&data[len_processed], &creds->checksum);
|
||||
len_processed += sizeof(uint16_t);
|
||||
memcpy(creds->sig_pk, data + len_processed, SIG_PUBLIC_KEY_SIZE);
|
||||
memcpy(creds->sig_pk, &data[len_processed], SIG_PUBLIC_KEY_SIZE);
|
||||
len_processed += SIG_PUBLIC_KEY_SIZE;
|
||||
memcpy(creds->sig, data + len_processed, SIGNATURE_SIZE);
|
||||
memcpy(creds->sig, &data[len_processed], SIGNATURE_SIZE);
|
||||
len_processed += SIGNATURE_SIZE;
|
||||
|
||||
return len_processed;
|
||||
@ -342,11 +343,11 @@ int sanctions_list_unpack(Mod_Sanction *sanctions, Mod_Sanction_Creds *creds, ui
|
||||
return -1;
|
||||
}
|
||||
|
||||
memcpy(&sanctions[num].type, data + len_processed, sizeof(uint8_t));
|
||||
memcpy(&sanctions[num].type, &data[len_processed], sizeof(uint8_t));
|
||||
len_processed += sizeof(uint8_t);
|
||||
memcpy(sanctions[num].setter_public_sig_key, data + len_processed, SIG_PUBLIC_KEY_SIZE);
|
||||
memcpy(sanctions[num].setter_public_sig_key, &data[len_processed], SIG_PUBLIC_KEY_SIZE);
|
||||
len_processed += SIG_PUBLIC_KEY_SIZE;
|
||||
net_unpack_u64(data + len_processed, &sanctions[num].time_set);
|
||||
net_unpack_u64(&data[len_processed], &sanctions[num].time_set);
|
||||
len_processed += TIME_STAMP_SIZE;
|
||||
|
||||
if (sanctions[num].type == SA_OBSERVER) {
|
||||
@ -354,7 +355,7 @@ int sanctions_list_unpack(Mod_Sanction *sanctions, Mod_Sanction_Creds *creds, ui
|
||||
return -1;
|
||||
}
|
||||
|
||||
memcpy(sanctions[num].target_public_enc_key, data + len_processed, ENC_PUBLIC_KEY_SIZE);
|
||||
memcpy(sanctions[num].target_public_enc_key, &data[len_processed], ENC_PUBLIC_KEY_SIZE);
|
||||
len_processed += ENC_PUBLIC_KEY_SIZE;
|
||||
} else {
|
||||
return -1;
|
||||
@ -364,7 +365,7 @@ int sanctions_list_unpack(Mod_Sanction *sanctions, Mod_Sanction_Creds *creds, ui
|
||||
return -1;
|
||||
}
|
||||
|
||||
memcpy(sanctions[num].signature, data + len_processed, SIGNATURE_SIZE);
|
||||
memcpy(sanctions[num].signature, &data[len_processed], SIGNATURE_SIZE);
|
||||
len_processed += SIGNATURE_SIZE;
|
||||
|
||||
++num;
|
||||
@ -382,7 +383,7 @@ int sanctions_list_unpack(Mod_Sanction *sanctions, Mod_Sanction_Creds *creds, ui
|
||||
return num;
|
||||
}
|
||||
|
||||
const uint16_t creds_len = sanctions_creds_unpack(creds, data + len_processed);
|
||||
const uint16_t creds_len = sanctions_creds_unpack(creds, &data[len_processed]);
|
||||
|
||||
if (creds_len != MOD_SANCTIONS_CREDS_SIZE) {
|
||||
return -1;
|
||||
@ -395,7 +396,6 @@ int sanctions_list_unpack(Mod_Sanction *sanctions, Mod_Sanction_Creds *creds, ui
|
||||
return num;
|
||||
}
|
||||
|
||||
|
||||
/** @brief Creates a new sanction list hash and puts it in hash.
|
||||
*
|
||||
* The hash is derived from the signature of all entries plus the version number.
|
||||
@ -410,7 +410,7 @@ static bool sanctions_list_make_hash(const Mod_Sanction *sanctions, uint32_t new
|
||||
uint8_t *hash)
|
||||
{
|
||||
if (num_sanctions == 0 || sanctions == nullptr) {
|
||||
memset(hash, 0, MOD_SANCTION_HASH_SIZE);
|
||||
memzero(hash, MOD_SANCTION_HASH_SIZE);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -864,9 +864,7 @@ uint16_t sanctions_list_replace_sig(Moderation *moderation, const uint8_t *publi
|
||||
|
||||
void sanctions_list_cleanup(Moderation *moderation)
|
||||
{
|
||||
if (moderation->sanctions != nullptr) {
|
||||
free(moderation->sanctions);
|
||||
}
|
||||
free(moderation->sanctions);
|
||||
|
||||
moderation->sanctions = nullptr;
|
||||
moderation->num_sanctions = 0;
|
||||
|
@ -14,7 +14,10 @@
|
||||
#include <stdint.h>
|
||||
|
||||
#include "DHT.h"
|
||||
#include "attributes.h"
|
||||
#include "crypto_core.h"
|
||||
#include "logger.h"
|
||||
#include "mem.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@ -291,7 +294,7 @@ non_null()
|
||||
void sanctions_list_cleanup(Moderation *moderation);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif // C_TOXCORE_TOXCORE_GROUP_MODERATION_H
|
||||
#endif /* C_TOXCORE_TOXCORE_GROUP_MODERATION_H */
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "group_moderation.h"
|
||||
|
||||
#include "../testing/fuzzing/fuzz_support.h"
|
||||
#include "../testing/fuzzing/fuzz_support.hh"
|
||||
#include "mem_test_util.hh"
|
||||
|
||||
namespace {
|
||||
|
@ -6,6 +6,7 @@
|
||||
#include <array>
|
||||
#include <vector>
|
||||
|
||||
#include "DHT.h"
|
||||
#include "crypto_core.h"
|
||||
#include "crypto_core_test_util.hh"
|
||||
#include "logger.h"
|
||||
@ -14,8 +15,6 @@
|
||||
|
||||
namespace {
|
||||
|
||||
using ExtPublicKey = std::array<uint8_t, EXT_PUBLIC_KEY_SIZE>;
|
||||
using ExtSecretKey = std::array<uint8_t, EXT_SECRET_KEY_SIZE>;
|
||||
using ModerationHash = std::array<uint8_t, MOD_MODERATION_HASH_SIZE>;
|
||||
|
||||
TEST(ModList, PackedSizeOfEmptyModListIsZero)
|
||||
@ -190,9 +189,10 @@ TEST(SanctionsList, PackUnpackSanctionsCreds)
|
||||
|
||||
struct SanctionsListMod : ::testing::Test {
|
||||
protected:
|
||||
ExtPublicKey pk;
|
||||
ExtSecretKey sk;
|
||||
Extended_Public_Key pk;
|
||||
Extended_Secret_Key sk;
|
||||
Logger *log = logger_new();
|
||||
Test_Random rng;
|
||||
Test_Memory mem;
|
||||
Moderation mod{mem};
|
||||
|
||||
@ -202,14 +202,14 @@ protected:
|
||||
|
||||
void SetUp() override
|
||||
{
|
||||
ASSERT_TRUE(create_extended_keypair(pk.data(), sk.data()));
|
||||
ASSERT_TRUE(create_extended_keypair(&pk, &sk, rng));
|
||||
|
||||
mod.log = log;
|
||||
|
||||
memcpy(mod.self_public_sig_key, get_sig_pk(pk.data()), SIG_PUBLIC_KEY_SIZE);
|
||||
memcpy(mod.self_secret_sig_key, get_sig_sk(sk.data()), SIG_SECRET_KEY_SIZE);
|
||||
memcpy(mod.self_public_sig_key, get_sig_pk(&pk), SIG_PUBLIC_KEY_SIZE);
|
||||
memcpy(mod.self_secret_sig_key, get_sig_sk(&sk), SIG_SECRET_KEY_SIZE);
|
||||
|
||||
ASSERT_TRUE(mod_list_add_entry(&mod, get_sig_pk(pk.data())));
|
||||
ASSERT_TRUE(mod_list_add_entry(&mod, get_sig_pk(&pk)));
|
||||
|
||||
EXPECT_FALSE(sanctions_list_check_integrity(&mod, &mod.sanctions_creds, &sanctions[0], 0));
|
||||
EXPECT_FALSE(sanctions_list_check_integrity(&mod, &mod.sanctions_creds, &sanctions[0], 1));
|
||||
@ -230,7 +230,7 @@ protected:
|
||||
EXPECT_TRUE(sanctions_list_remove_observer(&mod, sanctioned_pk2, nullptr));
|
||||
EXPECT_FALSE(sanctions_list_entry_exists(&mod, &sanctions[0]));
|
||||
EXPECT_FALSE(sanctions_list_entry_exists(&mod, &sanctions[1]));
|
||||
EXPECT_TRUE(mod_list_remove_entry(&mod, get_sig_pk(pk.data())));
|
||||
EXPECT_TRUE(mod_list_remove_entry(&mod, get_sig_pk(&pk)));
|
||||
|
||||
logger_kill(log);
|
||||
}
|
||||
|
@ -8,6 +8,8 @@
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "DHT.h"
|
||||
#include "attributes.h"
|
||||
#include "ccompat.h"
|
||||
#include "crypto_core.h"
|
||||
#include "group_announce.h"
|
||||
@ -15,6 +17,7 @@
|
||||
#include "mono_time.h"
|
||||
#include "network.h"
|
||||
#include "onion_announce.h"
|
||||
#include "timed_auth.h"
|
||||
|
||||
static_assert(GCA_ANNOUNCE_MAX_SIZE <= ONION_MAX_EXTRA_DATA_SIZE,
|
||||
"GC_Announce does not fit into the onion packet extra data");
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user