Merge commit '9ace11a0e2843cbde4af2b6ff7b49bcc6d429f78'

This commit is contained in:
2024-01-09 16:39:05 +01:00
152 changed files with 1542 additions and 1140 deletions

View File

@ -16,13 +16,15 @@
#include "LAN_discovery.h"
#include "bin_pack.h"
#include "ccompat.h"
#include "crypto_core.h"
#include "logger.h"
#include "mem.h"
#include "mono_time.h"
#include "network.h"
#include "ping.h"
#include "ping_array.h"
#include "shared_key_cache.h"
#include "state.h"
#include "util.h"
/** The timeout after which a node is discarded completely. */
#define KILL_NODE_TIMEOUT (BAD_NODE_TIMEOUT + PING_INTERVAL)
@ -420,7 +422,8 @@ static bool bin_pack_ip_port(Bin_Pack *bp, const Logger *logger, const IP_Port *
non_null()
static bool bin_pack_ip_port_handler(Bin_Pack *bp, const Logger *logger, const void *obj)
{
return bin_pack_ip_port(bp, logger, (const IP_Port *)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)
@ -508,7 +511,7 @@ int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length, bool
return -1;
}
*ip_port = empty_ip_port;
ipport_reset(ip_port);
if (is_ipv4) {
const uint32_t size = 1 + SIZE_IP4 + sizeof(uint16_t);
@ -1477,12 +1480,12 @@ static int sendnodes_ipv6(const DHT *dht, const IP_Port *ip_port, const uint8_t
non_null()
static int handle_getnodes(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length, void *userdata)
{
DHT *const dht = (DHT *)object;
if (length != (CRYPTO_SIZE + CRYPTO_MAC_SIZE + sizeof(uint64_t))) {
return 1;
}
DHT *const dht = (DHT *)object;
/* Check if packet is from ourself. */
if (pk_equal(packet + 1, dht->self_public_key)) {
return 1;
@ -2263,11 +2266,12 @@ non_null()
static int handle_nat_ping(void *object, const IP_Port *source, const uint8_t *source_pubkey, const uint8_t *packet,
uint16_t length, void *userdata)
{
DHT *const dht = (DHT *)object;
if (length != sizeof(uint64_t) + 1) {
return 1;
}
DHT *const dht = (DHT *)object;
uint64_t ping_id;
memcpy(&ping_id, packet + 1, sizeof(uint64_t));

View File

@ -232,7 +232,7 @@ int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length, bool
non_null()
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,
const uint8_t *shared_key, uint8_t type,
const uint8_t *plain, size_t plain_length,
uint8_t *packet, size_t length);

View File

@ -32,7 +32,7 @@
#endif
#ifdef __linux__
#include <linux/netdevice.h>
#include <linux/if.h>
#endif
#if defined(__FreeBSD__) || defined(__DragonFly__)
@ -41,7 +41,7 @@
#include "ccompat.h"
#include "crypto_core.h"
#include "util.h"
#include "network.h"
#define MAX_INTERFACES 16
@ -243,11 +243,11 @@ static IP broadcast_ip(Family family_socket, Family family_broadcast)
ip.ip.v6.uint8[15] = 0x01;
} else if (net_family_is_ipv4(family_broadcast)) {
ip.family = net_family_ipv6();
ip.ip.v6 = ip6_broadcast;
ip.ip.v6 = get_ip6_broadcast();
}
} else if (net_family_is_ipv4(family_socket) && net_family_is_ipv4(family_broadcast)) {
ip.family = net_family_ipv4();
ip.ip.v4 = ip4_broadcast;
ip.ip.v4 = get_ip4_broadcast();
}
return ip;

View File

@ -10,17 +10,33 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "DHT.h"
#include "TCP_client.h"
#include "TCP_connection.h"
#include "TCP_server.h"
#include "announce.h"
#include "bin_pack.h"
#include "bin_unpack.h"
#include "ccompat.h"
#include "crypto_core.h"
#include "forwarding.h"
#include "friend_connection.h"
#include "friend_requests.h"
#include "group_announce.h"
#include "group_chats.h"
#include "group_common.h"
#include "group_onion_announce.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"
#include "util.h"
@ -2385,11 +2401,12 @@ static int m_handle_packet_invite_groupchat(Messenger *m, const int i, const uin
non_null(1, 3) nullable(5)
static int m_handle_packet(void *object, int i, const uint8_t *temp, uint16_t len, void *userdata)
{
Messenger *m = (Messenger *)object;
if (len == 0) {
return -1;
}
Messenger *m = (Messenger *)object;
const uint8_t packet_id = temp[0];
const uint8_t *data = temp + 1;
const uint16_t data_length = len - 1;
@ -3176,7 +3193,8 @@ 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)
{
pack_groupchats((const GC_Session *)obj, bp);
const GC_Session *session = (const GC_Session *)obj;
pack_groupchats(session, bp);
return true; // TODO(iphydf): Return bool from pack functions.
}

View File

@ -10,12 +10,17 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "DHT.h"
#include "TCP_common.h"
#include "ccompat.h"
#include "crypto_core.h"
#include "forwarding.h"
#include "logger.h"
#include "mem.h"
#include "mono_time.h"
#include "network.h"
#include "util.h"
typedef struct TCP_Client_Conn {

View File

@ -5,10 +5,13 @@
#include "TCP_common.h"
#include <stdlib.h>
#include <string.h>
#include "ccompat.h"
#include "crypto_core.h"
#include "logger.h"
#include "mem.h"
#include "network.h"
void wipe_priority_list(const Memory *mem, TCP_Priority_List *p)
{

View File

@ -9,12 +9,17 @@
#include "TCP_connection.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "DHT.h"
#include "TCP_client.h"
#include "ccompat.h"
#include "crypto_core.h"
#include "forwarding.h"
#include "logger.h"
#include "mem.h"
#include "mono_time.h"
#include "network.h"
#include "util.h"
struct TCP_Connections {
@ -1133,11 +1138,12 @@ non_null(1, 4) nullable(6)
static int tcp_conn_data_callback(void *object, uint32_t number, uint8_t connection_id, const uint8_t *data,
uint16_t length, void *userdata)
{
const TCP_Client_Connection *tcp_client_con = (TCP_Client_Connection *)object;
if (length == 0) {
return -1;
}
const TCP_Client_Connection *tcp_client_con = (TCP_Client_Connection *)object;
TCP_Connections *tcp_c = (TCP_Connections *)tcp_con_custom_object(tcp_client_con);
const unsigned int tcp_connections_number = tcp_con_custom_uint(tcp_client_con);
@ -1164,11 +1170,12 @@ non_null()
static int tcp_conn_oob_callback(void *object, const uint8_t *public_key, const uint8_t *data, uint16_t length,
void *userdata)
{
const TCP_Client_Connection *tcp_client_con = (const TCP_Client_Connection *)object;
if (length == 0) {
return -1;
}
const TCP_Client_Connection *tcp_client_con = (const TCP_Client_Connection *)object;
TCP_Connections *tcp_c = (TCP_Connections *)tcp_con_custom_object(tcp_client_con);
const unsigned int tcp_connections_number = tcp_con_custom_uint(tcp_client_con);

View File

@ -8,7 +8,6 @@
*/
#include "TCP_server.h"
#include <stdlib.h>
#include <string.h>
#if !defined(_WIN32) && !defined(__WIN32__) && !defined (WIN32)
#include <sys/ioctl.h>
@ -19,11 +18,17 @@
#include <unistd.h>
#endif
#include "DHT.h"
#include "TCP_common.h"
#include "ccompat.h"
#include "crypto_core.h"
#include "forwarding.h"
#include "list.h"
#include "logger.h"
#include "mem.h"
#include "mono_time.h"
#include "util.h"
#include "network.h"
#include "onion.h"
#ifdef TCP_SERVER_USE_EPOLL
#define TCP_SOCKET_LISTENING 0
@ -909,7 +914,7 @@ static Socket new_listening_tcp_socket(const Logger *logger, const Network *ns,
if (!sock_valid(sock)) {
LOGGER_ERROR(logger, "TCP socket creation failed (family = %d)", family.value);
return net_invalid_socket;
return net_invalid_socket();
}
bool ok = set_socket_nonblock(ns, sock);
@ -930,7 +935,7 @@ static Socket new_listening_tcp_socket(const Logger *logger, const Network *ns,
port, family.value, error != nullptr ? error : "(null)");
net_kill_strerror(error);
kill_sock(ns, sock);
return net_invalid_socket;
return net_invalid_socket();
}
LOGGER_DEBUG(logger, "successfully bound to TCP port %d", port);

View File

@ -12,8 +12,15 @@
#include <stdlib.h>
#include <string.h>
#include "DHT.h"
#include "LAN_discovery.h"
#include "ccompat.h"
#include "crypto_core.h"
#include "forwarding.h"
#include "logger.h"
#include "mem.h"
#include "mono_time.h"
#include "network.h"
#include "shared_key_cache.h"
#include "timed_auth.h"
#include "util.h"
@ -427,7 +434,7 @@ static int create_reply_plain_store_announce_request(Announcements *announce,
const uint8_t *to_auth, uint16_t to_auth_length)
{
const int plain_len = (int)length - (CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + CRYPTO_MAC_SIZE);
const int announcement_len = (int)plain_len - (TIMED_AUTH_SIZE + sizeof(uint32_t) + 1);
const int announcement_len = plain_len - (TIMED_AUTH_SIZE + sizeof(uint32_t) + 1);
const uint8_t *const data_public_key = data;

View File

@ -10,6 +10,7 @@
#include "../third_party/cmp/cmp.h"
#include "ccompat.h"
#include "logger.h"
struct Bin_Pack {
uint8_t *bytes;

View File

@ -2,3 +2,5 @@
* Copyright © 2022 The TokTok team.
*/
#include "ccompat.h"
static_assert(sizeof(int) >= 4, "toxcore does not support 16-bit platforms");

View File

@ -1,19 +1,17 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"
/*****************************************************

View File

@ -1,5 +1,5 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
@ -11,6 +11,7 @@
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"

View File

@ -1,5 +1,5 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
@ -11,6 +11,7 @@
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"

View File

@ -1,19 +1,17 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"
/*****************************************************

View File

@ -1,5 +1,5 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
@ -11,9 +11,9 @@
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"
/*****************************************************

View File

@ -1,5 +1,5 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
@ -11,9 +11,9 @@
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"
/*****************************************************

View File

@ -5,9 +5,11 @@
#include "events_alloc.h"
#include <assert.h>
#include <stdlib.h>
#include "../ccompat.h"
#include "../mem.h"
#include "../tox_event.h"
#include "../tox_events.h"
Tox_Events_State *tox_events_alloc(void *user_data)
{

View File

@ -2,8 +2,8 @@
* Copyright © 2022 The TokTok team.
*/
#ifndef C_TOXCORE_TOXCORE_TOX_EVENTS_INTERNAL_H
#define C_TOXCORE_TOXCORE_TOX_EVENTS_INTERNAL_H
#ifndef C_TOXCORE_TOXCORE_EVENTS_EVENTS_ALLOC_H
#define C_TOXCORE_TOXCORE_EVENTS_EVENTS_ALLOC_H
#include "../attributes.h"
#include "../bin_pack.h"
@ -84,4 +84,4 @@ bool tox_events_add(Tox_Events *events, const Tox_Event *event);
}
#endif
#endif // C_TOXCORE_TOXCORE_TOX_EVENTS_INTERNAL_H
#endif // C_TOXCORE_TOXCORE_EVENTS_EVENTS_ALLOC_H

View File

@ -1,19 +1,17 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"
/*****************************************************

View File

@ -1,5 +1,5 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
@ -11,9 +11,9 @@
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"
/*****************************************************

View File

@ -1,5 +1,5 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
@ -11,9 +11,9 @@
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"
/*****************************************************

View File

@ -1,16 +1,15 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"

View File

@ -1,16 +1,15 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"

View File

@ -1,5 +1,5 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
@ -11,9 +11,9 @@
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"
/*****************************************************

View File

@ -1,5 +1,5 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
@ -11,9 +11,9 @@
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"
/*****************************************************

View File

@ -1,5 +1,5 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
@ -11,6 +11,7 @@
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"

View File

@ -1,5 +1,5 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
@ -11,9 +11,9 @@
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"
/*****************************************************

View File

@ -1,19 +1,17 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"
/*****************************************************

View File

@ -5,12 +5,12 @@
#include "events_alloc.h"
#include <assert.h>
#include <stdlib.h>
#include <string.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"

View File

@ -1,16 +1,15 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"

View File

@ -1,5 +1,5 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
@ -11,9 +11,9 @@
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"
/*****************************************************

View File

@ -1,19 +1,17 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"
/*****************************************************

View File

@ -1,5 +1,5 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
@ -11,9 +11,9 @@
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"
/*****************************************************

View File

@ -1,5 +1,5 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
@ -11,9 +11,9 @@
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"
/*****************************************************

View File

@ -1,5 +1,5 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
@ -11,9 +11,9 @@
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"
/*****************************************************

View File

@ -1,16 +1,15 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"

View File

@ -1,5 +1,5 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
@ -11,6 +11,7 @@
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"

View File

@ -1,16 +1,15 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"

View File

@ -1,5 +1,5 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
@ -11,9 +11,9 @@
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"
/*****************************************************

View File

@ -1,5 +1,5 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
@ -11,6 +11,7 @@
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"

View File

@ -1,19 +1,17 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"
/*****************************************************

View File

@ -1,19 +1,17 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"
/*****************************************************

View File

@ -1,5 +1,5 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
@ -11,9 +11,9 @@
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"
/*****************************************************

View File

@ -1,16 +1,15 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"

View File

@ -1,16 +1,15 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"

View File

@ -1,5 +1,5 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
@ -11,6 +11,7 @@
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"

View File

@ -1,19 +1,17 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"
/*****************************************************

View File

@ -1,5 +1,5 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
@ -11,9 +11,9 @@
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"
/*****************************************************

View File

@ -1,16 +1,15 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"

View File

@ -1,16 +1,15 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"

View File

@ -1,16 +1,15 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/
#include "events_alloc.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "../bin_pack.h"
#include "../bin_unpack.h"
#include "../ccompat.h"
#include "../mem.h"
#include "../tox.h"
#include "../tox_events.h"
#include "../tox_unpack.h"

View File

@ -10,6 +10,10 @@
#include "DHT.h"
#include "ccompat.h"
#include "crypto_core.h"
#include "logger.h"
#include "mono_time.h"
#include "network.h"
#include "timed_auth.h"
struct Forwarding {

View File

@ -11,9 +11,16 @@
#include <stdlib.h>
#include <string.h>
#include "DHT.h"
#include "LAN_discovery.h"
#include "TCP_connection.h"
#include "ccompat.h"
#include "crypto_core.h"
#include "logger.h"
#include "mono_time.h"
#include "util.h"
#include "net_crypto.h"
#include "network.h"
#include "onion_client.h"
#define PORTS_PER_DISCOVERY 10
@ -467,11 +474,12 @@ 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)
{
Friend_Connections *const fr_c = (Friend_Connections *)object;
if (length == 0) {
return -1;
}
Friend_Connections *const fr_c = (Friend_Connections *)object;
Friend_Conn *friend_con = get_conn(fr_c, number);
if (friend_con == nullptr) {
@ -526,11 +534,12 @@ 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)
{
const Friend_Connections *const fr_c = (const Friend_Connections *)object;
if (length == 0) {
return -1;
}
const Friend_Connections *const fr_c = (const Friend_Connections *)object;
const Friend_Conn *friend_con = get_conn(fr_c, number);
if (friend_con == nullptr) {

View File

@ -12,7 +12,9 @@
#include <string.h>
#include "ccompat.h"
#include "util.h"
#include "crypto_core.h"
#include "friend_connection.h"
#include "onion_client.h"
/**
* NOTE: The following is just a temporary fix for the multiple friend requests received at the same time problem.

View File

@ -12,8 +12,16 @@
#include <stdlib.h>
#include <string.h>
#include "DHT.h"
#include "Messenger.h"
#include "ccompat.h"
#include "crypto_core.h"
#include "friend_connection.h"
#include "group_common.h"
#include "logger.h"
#include "mono_time.h"
#include "net_crypto.h"
#include "network.h"
#include "state.h"
#include "util.h"

View File

@ -8,10 +8,11 @@
#include <stdlib.h>
#include <string.h>
#include "LAN_discovery.h"
#include "DHT.h"
#include "ccompat.h"
#include "logger.h"
#include "mono_time.h"
#include "util.h"
#include "network.h"
/**
* Removes `announces` from `gc_announces_list`.

View File

@ -6,8 +6,8 @@
/**
* Similar to ping.h, but designed for group chat purposes
*/
#ifndef GROUP_ANNOUNCE_H
#define GROUP_ANNOUNCE_H
#ifndef C_TOXCORE_TOXCORE_GROUP_ANNOUNCE_H
#define C_TOXCORE_TOXCORE_GROUP_ANNOUNCE_H
#include <stdbool.h>
@ -215,4 +215,4 @@ bool gca_is_valid_announce(const GC_Announce *announce);
} // extern "C"
#endif
#endif // GROUP_ANNOUNCE_H
#endif // C_TOXCORE_TOXCORE_GROUP_ANNOUNCE_H

View File

@ -9,7 +9,7 @@ namespace {
struct Announces : ::testing::Test {
protected:
const Memory *mem_ = system_memory();
uint64_t clock_ = 0;
uint64_t clock_ = 1000;
Mono_Time *mono_time_ = nullptr;
GC_Announces_List *gca_ = nullptr;
GC_Announce _ann1;
@ -141,7 +141,7 @@ protected:
ann2.ip_port_is_set = 1;
ann2.tcp_relays_count = 1;
ann2.tcp_relays[0].ip_port.ip.family = net_family_ipv4();
ann2.tcp_relays[0].ip_port.ip.ip.v4 = ip4_broadcast;
ann2.tcp_relays[0].ip_port.ip.ip.v4 = get_ip4_broadcast();
ann2.tcp_relays[0].public_key[0] = 0xea;
}

View File

@ -9,23 +9,31 @@
#include "group_chats.h"
#include <assert.h>
#include <sodium.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "DHT.h"
#include "LAN_discovery.h"
#include "Messenger.h"
#include "TCP_connection.h"
#include "bin_pack.h"
#include "bin_unpack.h"
#include "ccompat.h"
#include "crypto_core.h"
#include "friend_connection.h"
#include "group_announce.h"
#include "group_common.h"
#include "group_connection.h"
#include "group_moderation.h"
#include "group_pack.h"
#include "logger.h"
#include "mono_time.h"
#include "net_crypto.h"
#include "network.h"
#include "onion_announce.h"
#include "onion_client.h"
#include "util.h"
/* The minimum size of a plaintext group handshake packet */
@ -1374,7 +1382,7 @@ static int make_gc_shared_state_packet(const GC_Chat *chat, uint8_t *data, uint1
return -1;
}
return (int)(header_len + packed_len);
return header_len + packed_len;
}
/** @brief Creates a signature for the group's shared state in packed form.
@ -3241,7 +3249,7 @@ static int make_gc_sanctions_list_packet(const GC_Chat *chat, uint8_t *data, uin
return -1;
}
return (int)(length + packed_len);
return length + packed_len;
}
/** @brief Sends the sanctions list to peer.

View File

@ -7,8 +7,8 @@
* An implementation of massive text only group chats.
*/
#ifndef GROUP_CHATS_H
#define GROUP_CHATS_H
#ifndef C_TOXCORE_TOXCORE_GROUP_CHATS_H
#define C_TOXCORE_TOXCORE_GROUP_CHATS_H
#include <stdbool.h>
#include <stdint.h>
@ -805,4 +805,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 // GROUP_CHATS_H
#endif // C_TOXCORE_TOXCORE_GROUP_CHATS_H

View File

@ -6,8 +6,8 @@
* Common groupchat data structures.
*/
#ifndef GROUP_COMMON_H
#define GROUP_COMMON_H
#ifndef C_TOXCORE_TOXCORE_GROUP_COMMON_H
#define C_TOXCORE_TOXCORE_GROUP_COMMON_H
#include <stdbool.h>
#include <stdint.h>
@ -410,4 +410,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 // GROUP_COMMON_H
#endif // C_TOXCORE_TOXCORE_GROUP_COMMON_H

View File

@ -15,11 +15,14 @@
#include <string.h>
#include "DHT.h"
#include "TCP_connection.h"
#include "ccompat.h"
#include "crypto_core.h"
#include "group_chats.h"
#include "group_common.h"
#include "logger.h"
#include "mono_time.h"
#include "network.h"
#include "util.h"
/** Seconds since last direct UDP packet was received before the connection is considered dead */

View File

@ -7,8 +7,8 @@
* An implementation of massive text only group chats.
*/
#ifndef GROUP_CONNECTION_H
#define GROUP_CONNECTION_H
#ifndef C_TOXCORE_TOXCORE_GROUP_CONNECTION_H
#define C_TOXCORE_TOXCORE_GROUP_CONNECTION_H
#include "group_common.h"
@ -186,4 +186,4 @@ void gcc_peer_cleanup(GC_Connection *gconn);
non_null()
void gcc_cleanup(const GC_Chat *chat);
#endif // GROUP_CONNECTION_H
#endif // C_TOXCORE_TOXCORE_GROUP_CONNECTION_H

View File

@ -15,9 +15,10 @@
#include <string.h>
#include <time.h>
#include "DHT.h"
#include "ccompat.h"
#include "crypto_core.h"
#include "mono_time.h"
#include "logger.h"
#include "network.h"
#include "util.h"
@ -305,7 +306,7 @@ int sanctions_list_pack(uint8_t *data, uint16_t length, const Mod_Sanction *sanc
return -1;
}
return (int)(packed_len + cred_len);
return packed_len + cred_len;
}
uint16_t sanctions_creds_unpack(Mod_Sanction_Creds *creds, const uint8_t *data)
@ -457,7 +458,7 @@ static bool sanctions_list_validate_entry(const Moderation *moderation, const Mo
uint8_t packed_data[MOD_SANCTION_PACKED_SIZE];
const int packed_len = sanctions_list_pack(packed_data, sizeof(packed_data), sanction, 1, nullptr);
if (packed_len <= (int) SIGNATURE_SIZE) {
if (packed_len <= SIGNATURE_SIZE) {
return false;
}
@ -785,7 +786,7 @@ static bool sanctions_list_sign_entry(const Moderation *moderation, Mod_Sanction
uint8_t packed_data[MOD_SANCTION_PACKED_SIZE];
const int packed_len = sanctions_list_pack(packed_data, sizeof(packed_data), sanction, 1, nullptr);
if (packed_len <= (int) SIGNATURE_SIZE) {
if (packed_len <= SIGNATURE_SIZE) {
LOGGER_ERROR(moderation->log, "Failed to pack sanctions list: %d", packed_len);
return false;
}

View File

@ -79,7 +79,7 @@ TEST(ModList, UnpackingFromEmptyBufferFails)
std::vector<uint8_t> packed(1);
Moderation mods{system_memory()};
EXPECT_EQ(mod_list_unpack(&mods, packed.end().base(), 0, 1), -1);
EXPECT_EQ(mod_list_unpack(&mods, packed.data(), 0, 1), -1);
}
TEST(ModList, HashOfEmptyModListZeroesOutBuffer)

View File

@ -9,6 +9,12 @@
#include <string.h>
#include "ccompat.h"
#include "crypto_core.h"
#include "group_announce.h"
#include "logger.h"
#include "mono_time.h"
#include "network.h"
#include "onion_announce.h"
static_assert(GCA_ANNOUNCE_MAX_SIZE <= ONION_MAX_EXTRA_DATA_SIZE,
"GC_Announce does not fit into the onion packet extra data");

View File

@ -13,9 +13,13 @@
#include <stdlib.h>
#include <string.h>
#include "DHT.h"
#include "bin_pack.h"
#include "bin_unpack.h"
#include "ccompat.h"
#include "group_common.h"
#include "group_moderation.h"
#include "logger.h"
#include "util.h"
bool group_privacy_state_from_int(uint8_t value, Group_Privacy_State *out)
@ -428,7 +432,7 @@ static void save_pack_self_info(const GC_Chat *chat, Bin_Pack *bp)
bin_pack_u16(bp, self->nick_length); // 1
bin_pack_u08(bp, (uint8_t)self->role); // 2
bin_pack_u08(bp, (uint8_t)self->status); // 3
bin_pack_u08(bp, self->status); // 3
bin_pack_bin(bp, self->nick, self->nick_length); // 4
}

View File

@ -7,8 +7,8 @@
* Packer and unpacker functions for saving and loading groups.
*/
#ifndef GROUP_PACK_H
#define GROUP_PACK_H
#ifndef C_TOXCORE_TOXCORE_GROUP_PACK_H
#define C_TOXCORE_TOXCORE_GROUP_PACK_H
#include <stdbool.h>
@ -37,4 +37,4 @@ bool group_privacy_state_from_int(uint8_t value, Group_Privacy_State *out);
non_null()
bool group_voice_state_from_int(uint8_t value, Group_Voice_State *out);
#endif // GROUP_PACK_H
#endif // C_TOXCORE_TOXCORE_GROUP_PACK_H

View File

@ -10,6 +10,7 @@
*/
#include "list.h"
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
@ -206,6 +207,7 @@ bool bs_list_add(BS_List *list, const uint8_t *data, int id)
}
// insert data to element array
assert(list->data != nullptr);
memmove(list->data + (i + 1) * list->element_size, list->data + i * list->element_size,
(list->n - i) * list->element_size);
memcpy(list->data + i * list->element_size, data, list->element_size);

View File

@ -8,7 +8,6 @@
*/
#include "logger.h"
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>

View File

@ -1,5 +1,5 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2016-2020 The TokTok team.
* Copyright © 2016-2023 The TokTok team.
* Copyright © 2014 Tox project.
*/
#ifndef _XOPEN_SOURCE
@ -26,24 +26,20 @@
#include <sys/time.h>
#endif
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
#include <assert.h>
#endif
#include <pthread.h>
#include <stdlib.h>
#include <time.h>
#include "ccompat.h"
#include "mem.h"
#include "util.h"
/** don't call into system billions of times for no reason */
struct Mono_Time {
uint64_t cur_time;
uint64_t base_time;
#ifdef OS_WIN32
/* protect `last_clock_update` and `last_clock_mono` from concurrent access */
pthread_mutex_t last_clock_lock;
uint32_t last_clock_mono;
bool last_clock_update;
#endif
#ifndef ESP_PLATFORM
/* protect `time` from concurrent access */
@ -54,42 +50,33 @@ struct Mono_Time {
void *user_data;
};
static uint64_t timespec_to_u64(struct timespec clock_mono)
{
return UINT64_C(1000) * clock_mono.tv_sec + (clock_mono.tv_nsec / UINT64_C(1000000));
}
#ifdef OS_WIN32
non_null()
static uint64_t current_time_monotonic_default(void *user_data)
{
Mono_Time *const mono_time = (Mono_Time *)user_data;
/* Must hold mono_time->last_clock_lock here */
/* GetTickCount provides only a 32 bit counter, but we can't use
* GetTickCount64 for backwards compatibility, so we handle wraparound
* ourselves.
*/
const uint32_t ticks = GetTickCount();
/* the higher 32 bits count the number of wrap arounds */
uint64_t old_ovf = mono_time->cur_time & ~((uint64_t)UINT32_MAX);
/* Check if time has decreased because of 32 bit wrap from GetTickCount() */
if (ticks < mono_time->last_clock_mono) {
/* account for overflow */
old_ovf += UINT32_MAX + UINT64_C(1);
LARGE_INTEGER freq;
LARGE_INTEGER count;
if (!QueryPerformanceFrequency(&freq)) {
return 0;
}
if (mono_time->last_clock_update) {
mono_time->last_clock_mono = ticks;
mono_time->last_clock_update = false;
if (!QueryPerformanceCounter(&count)) {
return 0;
}
/* splice the low and high bits back together */
return old_ovf + ticks;
}
#else // !OS_WIN32
static uint64_t timespec_to_u64(struct timespec clock_mono)
{
return 1000ULL * clock_mono.tv_sec + (clock_mono.tv_nsec / 1000000ULL);
struct timespec sp = {0};
sp.tv_sec = count.QuadPart / freq.QuadPart;
if (freq.QuadPart < 1000000000) {
sp.tv_nsec = (count.QuadPart % freq.QuadPart) * 1000000000 / freq.QuadPart;
} else {
sp.tv_nsec = (long)((count.QuadPart % freq.QuadPart) * (1000000000.0 / freq.QuadPart));
}
return timespec_to_u64(sp);
}
#else
#ifdef __APPLE__
non_null()
static uint64_t current_time_monotonic_default(void *user_data)
@ -148,19 +135,6 @@ Mono_Time *mono_time_new(const Memory *mem, mono_time_current_time_cb *current_t
mono_time_set_current_time_callback(mono_time, current_time_callback, user_data);
#ifdef OS_WIN32
mono_time->last_clock_mono = 0;
mono_time->last_clock_update = false;
if (pthread_mutex_init(&mono_time->last_clock_lock, nullptr) < 0) {
mem_delete(mem, mono_time->time_update_lock);
mem_delete(mem, mono_time);
return nullptr;
}
#endif
mono_time->cur_time = 0;
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
// Maximum reproducibility. Never return time = 0.
@ -168,7 +142,7 @@ Mono_Time *mono_time_new(const Memory *mem, mono_time_current_time_cb *current_t
#else
// Never return time = 0 in case time() returns 0 (e.g. on microcontrollers
// without battery-powered RTC or ones where NTP didn't initialise it yet).
mono_time->base_time = max_u64(1, (uint64_t)time(nullptr)) * 1000ULL - current_time_monotonic(mono_time);
mono_time->base_time = max_u64(1, (uint64_t)time(nullptr)) * UINT64_C(1000) - current_time_monotonic(mono_time);
#endif
mono_time_update(mono_time);
@ -181,9 +155,6 @@ void mono_time_free(const Memory *mem, Mono_Time *mono_time)
if (mono_time == nullptr) {
return;
}
#ifdef OS_WIN32
pthread_mutex_destroy(&mono_time->last_clock_lock);
#endif
#ifndef ESP_PLATFORM
pthread_rwlock_destroy(mono_time->time_update_lock);
mem_delete(mem, mono_time->time_update_lock);
@ -193,16 +164,8 @@ void mono_time_free(const Memory *mem, Mono_Time *mono_time)
void mono_time_update(Mono_Time *mono_time)
{
#ifdef OS_WIN32
/* we actually want to update the overflow state of mono_time here */
pthread_mutex_lock(&mono_time->last_clock_lock);
mono_time->last_clock_update = true;
#endif
const uint64_t cur_time =
mono_time->base_time + mono_time->current_time_callback(mono_time->user_data);
#ifdef OS_WIN32
pthread_mutex_unlock(&mono_time->last_clock_lock);
#endif
#ifndef ESP_PLATFORM
pthread_rwlock_wrlock(mono_time->time_update_lock);
@ -228,7 +191,7 @@ uint64_t mono_time_get_ms(const Mono_Time *mono_time)
uint64_t mono_time_get(const Mono_Time *mono_time)
{
return mono_time_get_ms(mono_time) / 1000ULL;
return mono_time_get_ms(mono_time) / UINT64_C(1000);
}
bool mono_time_is_timeout(const Mono_Time *mono_time, uint64_t timestamp, uint64_t timeout)
@ -255,15 +218,5 @@ void mono_time_set_current_time_callback(Mono_Time *mono_time,
*/
uint64_t current_time_monotonic(Mono_Time *mono_time)
{
/* For WIN32 we don't want to change overflow state of mono_time here */
#ifdef OS_WIN32
/* We don't want to update the overflow state of mono_time here,
* but must protect against other threads */
pthread_mutex_lock(&mono_time->last_clock_lock);
#endif
const uint64_t cur_time = mono_time->current_time_callback(mono_time->user_data);
#ifdef OS_WIN32
pthread_mutex_unlock(&mono_time->last_clock_lock);
#endif
return cur_time;
return mono_time->current_time_callback(mono_time->user_data);
}

View File

@ -53,11 +53,14 @@ TEST(MonoTime, IsTimeoutReal)
uint64_t const start = mono_time_get(mono_time);
EXPECT_FALSE(mono_time_is_timeout(mono_time, start, 5));
const uint64_t before_sleep = mono_time_get(mono_time);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
mono_time_update(mono_time);
const uint64_t after_sleep = mono_time_get(mono_time);
// should still not have timed out (5sec) after sleeping ~100ms
EXPECT_FALSE(mono_time_is_timeout(mono_time, start, 5));
EXPECT_FALSE(mono_time_is_timeout(mono_time, start, 5))
<< "before sleep: " << before_sleep << ", after sleep: " << after_sleep;
mono_time_free(mem, mono_time);
}

View File

@ -10,13 +10,19 @@
*/
#include "net_crypto.h"
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "DHT.h"
#include "LAN_discovery.h"
#include "TCP_client.h"
#include "TCP_connection.h"
#include "ccompat.h"
#include "crypto_core.h"
#include "list.h"
#include "logger.h"
#include "mem.h"
#include "mono_time.h"
#include "network.h"
#include "util.h"
typedef struct Packet_Data {

View File

@ -89,7 +89,7 @@
#include "ccompat.h"
#include "logger.h"
#include "mono_time.h"
#include "mem.h"
#include "util.h"
// Disable MSG_NOSIGNAL on systems not supporting it, e.g. Windows, FreeBSD
@ -340,12 +340,20 @@ static void fill_addr6(const IP6 *ip, struct in6_addr *addr)
#endif
static const IP empty_ip = {{0}};
const IP_Port empty_ip_port = {{{0}}};
const IP4 ip4_broadcast = { INADDR_BROADCAST };
const IP6 ip6_broadcast = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }
};
IP4 get_ip4_broadcast(void)
{
const IP4 ip4_broadcast = { INADDR_BROADCAST };
return ip4_broadcast;
}
IP6 get_ip6_broadcast(void)
{
const IP6 ip6_broadcast = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }
};
return ip6_broadcast;
}
IP4 get_ip4_loopback(void)
{
@ -357,7 +365,7 @@ IP4 get_ip4_loopback(void)
IP6 get_ip6_loopback(void)
{
/* in6addr_loopback isn't available everywhere, so we do it ourselves. */
IP6 loopback = empty_ip_port.ip.ip.v6;
IP6 loopback = empty_ip.ip.v6;
loopback.uint8[15] = 1;
return loopback;
}
@ -366,7 +374,11 @@ IP6 get_ip6_loopback(void)
#define INVALID_SOCKET (-1)
#endif
const Socket net_invalid_socket = { (int)INVALID_SOCKET };
Socket net_invalid_socket(void)
{
const Socket invalid_socket = { (int)INVALID_SOCKET };
return invalid_socket;
}
Family net_family_unspec(void)
{
@ -460,7 +472,8 @@ bool net_family_is_tox_tcp_ipv6(Family family)
bool sock_valid(Socket sock)
{
return sock.sock != net_invalid_socket.sock;
const Socket invalid_socket = net_invalid_socket();
return sock.sock != invalid_socket.sock;
}
struct Network_Addr {
@ -1447,6 +1460,7 @@ void ipport_reset(IP_Port *ipport)
return;
}
const IP_Port empty_ip_port = {{{0}}};
*ipport = empty_ip_port;
}
@ -1748,8 +1762,8 @@ bool net_connect(const Memory *mem, const Logger *log, Socket sock, const IP_Por
// Non-blocking socket: "Operation in progress" means it's connecting.
if (!should_ignore_connect_error(error)) {
char *net_strerror = net_new_strerror(error);
LOGGER_ERROR(log, "failed to connect to %s:%d: %d (%s)",
net_ip_ntoa(&ip_port->ip, &ip_str), net_ntohs(ip_port->port), error, net_strerror);
LOGGER_WARNING(log, "failed to connect to %s:%d: %d (%s)",
net_ip_ntoa(&ip_port->ip, &ip_str), net_ntohs(ip_port->port), error, net_strerror);
net_kill_strerror(net_strerror);
return false;
}

View File

@ -183,7 +183,7 @@ typedef union IP4 {
} IP4;
IP4 get_ip4_loopback(void);
extern const IP4 ip4_broadcast;
IP4 get_ip4_broadcast(void);
typedef union IP6 {
uint8_t uint8[16];
@ -193,7 +193,7 @@ typedef union IP6 {
} IP6;
IP6 get_ip6_loopback(void);
extern const IP6 ip6_broadcast;
IP6 get_ip6_broadcast(void);
typedef union IP_Union {
IP4 v4;
@ -210,8 +210,6 @@ typedef struct IP_Port {
uint16_t port;
} IP_Port;
extern const IP_Port empty_ip_port;
typedef struct Socket {
int sock;
} Socket;
@ -226,7 +224,7 @@ Socket net_socket(const Network *ns, Family domain, int type, int protocol);
*/
bool sock_valid(Socket sock);
extern const Socket net_invalid_socket;
Socket net_invalid_socket(void);
/**
* Calls send(sockfd, buf, len, MSG_NOSIGNAL).

View File

@ -9,12 +9,16 @@
#include "onion.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "DHT.h"
#include "ccompat.h"
#include "crypto_core.h"
#include "logger.h"
#include "mem.h"
#include "mono_time.h"
#include "util.h"
#include "network.h"
#include "shared_key_cache.h"
#define RETURN_1 ONION_RETURN_1
#define RETURN_2 ONION_RETURN_2

View File

@ -15,9 +15,14 @@
#include "DHT.h"
#include "LAN_discovery.h"
#include "ccompat.h"
#include "crypto_core.h"
#include "logger.h"
#include "mem.h"
#include "mono_time.h"
#include "network.h"
#include "onion.h"
#include "shared_key_cache.h"
#include "util.h"
#include "timed_auth.h"
#define PING_ID_TIMEOUT ONION_ANNOUNCE_TIMEOUT

View File

@ -13,10 +13,20 @@
#include <stdlib.h>
#include <string.h>
#include "DHT.h"
#include "LAN_discovery.h"
#include "TCP_connection.h"
#include "ccompat.h"
#include "crypto_core.h"
#include "group_onion_announce.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 "ping_array.h"
#include "util.h"
/** @brief defines for the array size and timeout for onion announce packets. */

View File

@ -228,13 +228,13 @@ Onion_Connection_Status onion_connection_status(const Onion_Client *onion_c);
typedef struct Onion_Friend Onion_Friend;
non_null() uint16_t onion_get_friend_count(const Onion_Client *const onion_c);
non_null() Onion_Friend *onion_get_friend(const Onion_Client *const onion_c, uint16_t friend_num);
non_null() const uint8_t *onion_friend_get_gc_public_key(const Onion_Friend *const onion_friend);
non_null() const uint8_t *onion_friend_get_gc_public_key_num(const Onion_Client *const onion_c, uint32_t num);
non_null() void onion_friend_set_gc_public_key(Onion_Friend *const onion_friend, const uint8_t *public_key);
non_null() uint16_t onion_get_friend_count(const Onion_Client *onion_c);
non_null() Onion_Friend *onion_get_friend(const Onion_Client *onion_c, uint16_t friend_num);
non_null() const uint8_t *onion_friend_get_gc_public_key(const Onion_Friend *onion_friend);
non_null() const uint8_t *onion_friend_get_gc_public_key_num(const Onion_Client *onion_c, uint32_t num);
non_null() void onion_friend_set_gc_public_key(Onion_Friend *onion_friend, const uint8_t *public_key);
non_null(1) nullable(2)
void onion_friend_set_gc_data(Onion_Friend *const onion_friend, const uint8_t *gc_data, uint16_t gc_data_length);
non_null() bool onion_friend_is_groupchat(const Onion_Friend *const onion_friend);
void onion_friend_set_gc_data(Onion_Friend *onion_friend, const uint8_t *gc_data, uint16_t gc_data_length);
non_null() bool onion_friend_is_groupchat(const Onion_Friend *onion_friend);
#endif

View File

@ -9,15 +9,15 @@
*/
#include "ping.h"
#include <stdlib.h>
#include <string.h>
#include "DHT.h"
#include "ccompat.h"
#include "crypto_core.h"
#include "mem.h"
#include "mono_time.h"
#include "network.h"
#include "ping_array.h"
#include "util.h"
#define PING_NUM_MAX 512

View File

@ -8,13 +8,12 @@
*/
#include "ping_array.h"
#include <stdlib.h>
#include <string.h>
#include "ccompat.h"
#include "crypto_core.h"
#include "mem.h"
#include "mono_time.h"
#include "util.h"
typedef struct Ping_Array_Entry {
uint8_t *data;

View File

@ -4,12 +4,13 @@
#include "shared_key_cache.h"
#include <assert.h>
#include <stdint.h>
#include <string.h> // memcpy(...)
#include "ccompat.h"
#include "crypto_core.h"
#include "logger.h"
#include "mem.h"
#include "mono_time.h"
typedef struct Shared_Key {

View File

@ -7,6 +7,7 @@
#include <string.h>
#include "ccompat.h"
#include "logger.h"
/** state load/save */
int state_load(const Logger *log, state_load_cb *state_load_callback, void *outer,

View File

@ -6,6 +6,8 @@
#include <string.h>
#include "ccompat.h"
#include "crypto_core.h"
#include "mono_time.h"
non_null(1,6) nullable(4)
static void create_timed_auth_to_hash(const Mono_Time *mono_time, uint16_t timeout, bool previous, const uint8_t *data,

View File

@ -13,18 +13,24 @@
#include "tox.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "DHT.h"
#include "Messenger.h"
#include "TCP_client.h"
#include "ccompat.h"
#include "crypto_core.h"
#include "friend_requests.h"
#include "group.h"
#include "group_chats.h"
#include "group_moderation.h"
#include "group_common.h"
#include "logger.h"
#include "mem.h"
#include "mono_time.h"
#include "net_crypto.h"
#include "network.h"
#include "onion_client.h"
#include "state.h"
#include "tox_private.h"
#include "tox_struct.h"
@ -165,7 +171,7 @@ static void tox_friend_read_receipt_handler(Messenger *m, uint32_t friend_number
static m_friend_request_cb tox_friend_request_handler;
non_null(1, 2, 3) nullable(5)
static void tox_friend_request_handler(Messenger *m, const uint8_t *public_key, const uint8_t *message, size_t length,
static void tox_friend_request_handler(Messenger *m, const uint8_t public_key[TOX_PUBLIC_KEY_SIZE], const uint8_t *message, size_t length,
void *user_data)
{
struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;
@ -335,11 +341,11 @@ non_null(1, 4) nullable(6)
static void tox_friend_lossy_packet_handler(Messenger *m, uint32_t friend_number, uint8_t packet_id,
const uint8_t *data, size_t length, void *user_data)
{
struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;
assert(data != nullptr);
assert(length > 0);
struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;
if (tox_data->tox->friend_lossy_packet_callback_per_pktid[packet_id] != nullptr) {
tox_data->tox->friend_lossy_packet_callback_per_pktid[packet_id](tox_data->tox, friend_number, data, length,
tox_data->user_data);
@ -351,11 +357,11 @@ non_null(1, 4) nullable(6)
static void tox_friend_lossless_packet_handler(Messenger *m, uint32_t friend_number, uint8_t packet_id,
const uint8_t *data, size_t length, void *user_data)
{
struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;
assert(data != nullptr);
assert(length > 0);
struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;
if (tox_data->tox->friend_lossless_packet_callback_per_pktid[packet_id] != nullptr) {
tox_data->tox->friend_lossless_packet_callback_per_pktid[packet_id](tox_data->tox, friend_number, data, length,
tox_data->user_data);
@ -1007,7 +1013,7 @@ void tox_get_savedata(const Tox *tox, uint8_t *savedata)
}
non_null(5) nullable(1, 2, 4, 6)
static int32_t resolve_bootstrap_node(Tox *tox, const char *host, uint16_t port, const uint8_t *public_key,
static int32_t resolve_bootstrap_node(Tox *tox, const char *host, uint16_t port, const uint8_t public_key[TOX_PUBLIC_KEY_SIZE],
IP_Port **root, Tox_Err_Bootstrap *error)
{
assert(tox != nullptr);
@ -1036,7 +1042,7 @@ static int32_t resolve_bootstrap_node(Tox *tox, const char *host, uint16_t port,
return count;
}
bool tox_bootstrap(Tox *tox, const char *host, uint16_t port, const uint8_t *public_key, Tox_Err_Bootstrap *error)
bool tox_bootstrap(Tox *tox, const char *host, uint16_t port, const uint8_t public_key[TOX_PUBLIC_KEY_SIZE], Tox_Err_Bootstrap *error)
{
IP_Port *root;
const int32_t count = resolve_bootstrap_node(tox, host, port, public_key, &root, error);
@ -1087,7 +1093,7 @@ bool tox_bootstrap(Tox *tox, const char *host, uint16_t port, const uint8_t *pub
return true;
}
bool tox_add_tcp_relay(Tox *tox, const char *host, uint16_t port, const uint8_t *public_key,
bool tox_add_tcp_relay(Tox *tox, const char *host, uint16_t port, const uint8_t public_key[TOX_PUBLIC_KEY_SIZE],
Tox_Err_Bootstrap *error)
{
IP_Port *root;
@ -1176,7 +1182,7 @@ void tox_iterate(Tox *tox, void *user_data)
tox_unlock(tox);
}
void tox_self_get_address(const Tox *tox, uint8_t *address)
void tox_self_get_address(const Tox *tox, uint8_t address[TOX_ADDRESS_SIZE])
{
assert(tox != nullptr);
@ -1204,7 +1210,7 @@ uint32_t tox_self_get_nospam(const Tox *tox)
return ret;
}
void tox_self_get_public_key(const Tox *tox, uint8_t *public_key)
void tox_self_get_public_key(const Tox *tox, uint8_t public_key[TOX_PUBLIC_KEY_SIZE])
{
assert(tox != nullptr);
@ -1215,7 +1221,7 @@ void tox_self_get_public_key(const Tox *tox, uint8_t *public_key)
}
}
void tox_self_get_secret_key(const Tox *tox, uint8_t *secret_key)
void tox_self_get_secret_key(const Tox *tox, uint8_t secret_key[TOX_SECRET_KEY_SIZE])
{
assert(tox != nullptr);
@ -1376,7 +1382,7 @@ static void set_friend_error(const Logger *log, int32_t ret, Tox_Err_Friend_Add
}
}
uint32_t tox_friend_add(Tox *tox, const uint8_t *address, const uint8_t *message, size_t length,
uint32_t tox_friend_add(Tox *tox, const uint8_t address[TOX_ADDRESS_SIZE], const uint8_t *message, size_t length,
Tox_Err_Friend_Add *error)
{
assert(tox != nullptr);
@ -1400,7 +1406,7 @@ uint32_t tox_friend_add(Tox *tox, const uint8_t *address, const uint8_t *message
return UINT32_MAX;
}
uint32_t tox_friend_add_norequest(Tox *tox, const uint8_t *public_key, Tox_Err_Friend_Add *error)
uint32_t tox_friend_add_norequest(Tox *tox, const uint8_t public_key[TOX_PUBLIC_KEY_SIZE], Tox_Err_Friend_Add *error)
{
assert(tox != nullptr);
@ -1440,7 +1446,7 @@ bool tox_friend_delete(Tox *tox, uint32_t friend_number, Tox_Err_Friend_Delete *
return true;
}
uint32_t tox_friend_by_public_key(const Tox *tox, const uint8_t *public_key, Tox_Err_Friend_By_Public_Key *error)
uint32_t tox_friend_by_public_key(const Tox *tox, const uint8_t public_key[TOX_PUBLIC_KEY_SIZE], Tox_Err_Friend_By_Public_Key *error)
{
assert(tox != nullptr);
@ -1463,7 +1469,7 @@ uint32_t tox_friend_by_public_key(const Tox *tox, const uint8_t *public_key, Tox
return (uint32_t)ret;
}
bool tox_friend_get_public_key(const Tox *tox, uint32_t friend_number, uint8_t *public_key,
bool tox_friend_get_public_key(const Tox *tox, uint32_t friend_number, uint8_t public_key[TOX_PUBLIC_KEY_SIZE],
Tox_Err_Friend_Get_Public_Key *error)
{
assert(tox != nullptr);
@ -1789,7 +1795,7 @@ void tox_callback_friend_message(Tox *tox, tox_friend_message_cb *callback)
tox->friend_message_callback = callback;
}
bool tox_hash(uint8_t *hash, const uint8_t *data, size_t length)
bool tox_hash(uint8_t hash[TOX_HASH_LENGTH], const uint8_t *data, size_t length)
{
if (hash == nullptr || (data == nullptr && length != 0)) {
return false;
@ -1919,7 +1925,7 @@ void tox_callback_file_recv_control(Tox *tox, tox_file_recv_control_cb *callback
tox->file_recv_control_callback = callback;
}
bool tox_file_get_file_id(const Tox *tox, uint32_t friend_number, uint32_t file_number, uint8_t *file_id,
bool tox_file_get_file_id(const Tox *tox, uint32_t friend_number, uint32_t file_number, uint8_t file_id[TOX_FILE_ID_LENGTH],
Tox_Err_File_Get *error)
{
assert(tox != nullptr);
@ -1947,7 +1953,7 @@ bool tox_file_get_file_id(const Tox *tox, uint32_t friend_number, uint32_t file_
return false;
}
uint32_t tox_file_send(Tox *tox, uint32_t friend_number, uint32_t kind, uint64_t file_size, const uint8_t *file_id,
uint32_t tox_file_send(Tox *tox, uint32_t friend_number, uint32_t kind, uint64_t file_size, const uint8_t file_id[TOX_FILE_ID_LENGTH],
const uint8_t *filename, size_t filename_length, Tox_Err_File_Send *error)
{
assert(tox != nullptr);
@ -2209,7 +2215,7 @@ bool tox_conference_peer_get_name(const Tox *tox, uint32_t conference_number, ui
}
bool tox_conference_peer_get_public_key(const Tox *tox, uint32_t conference_number, uint32_t peer_number,
uint8_t *public_key, Tox_Err_Conference_Peer_Query *error)
uint8_t public_key[TOX_PUBLIC_KEY_SIZE], Tox_Err_Conference_Peer_Query *error)
{
assert(tox != nullptr);
tox_lock(tox);
@ -2330,7 +2336,7 @@ bool tox_conference_offline_peer_get_name(const Tox *tox, uint32_t conference_nu
bool tox_conference_offline_peer_get_public_key(const Tox *tox, uint32_t conference_number,
uint32_t offline_peer_number,
uint8_t *public_key, Tox_Err_Conference_Peer_Query *error)
uint8_t public_key[TOX_PUBLIC_KEY_SIZE], Tox_Err_Conference_Peer_Query *error)
{
assert(tox != nullptr);
tox_lock(tox);
@ -2381,12 +2387,12 @@ uint64_t tox_conference_offline_peer_get_last_active(const Tox *tox, uint32_t co
}
bool tox_conference_set_max_offline(Tox *tox, uint32_t conference_number,
uint32_t max_offline_peers,
uint32_t max_offline,
Tox_Err_Conference_Set_Max_Offline *error)
{
assert(tox != nullptr);
tox_lock(tox);
const int ret = group_set_max_frozen(tox->m->conferences_object, conference_number, max_offline_peers);
const int ret = group_set_max_frozen(tox->m->conferences_object, conference_number, max_offline);
tox_unlock(tox);
if (ret == -1) {
@ -2623,7 +2629,7 @@ Tox_Conference_Type tox_conference_get_type(const Tox *tox, uint32_t conference_
return (Tox_Conference_Type)ret;
}
bool tox_conference_get_id(const Tox *tox, uint32_t conference_number, uint8_t *id)
bool tox_conference_get_id(const Tox *tox, uint32_t conference_number, uint8_t id[TOX_CONFERENCE_ID_SIZE])
{
assert(tox != nullptr);
tox_lock(tox);
@ -2633,13 +2639,13 @@ bool tox_conference_get_id(const Tox *tox, uint32_t conference_number, uint8_t *
}
// TODO(iphydf): Delete in 0.3.0.
bool tox_conference_get_uid(const Tox *tox, uint32_t conference_number, uint8_t *uid)
bool tox_conference_get_uid(const Tox *tox, uint32_t conference_number, uint8_t uid[TOX_CONFERENCE_UID_SIZE])
{
assert(tox != nullptr);
return tox_conference_get_id(tox, conference_number, uid);
}
uint32_t tox_conference_by_id(const Tox *tox, const uint8_t *id, Tox_Err_Conference_By_Id *error)
uint32_t tox_conference_by_id(const Tox *tox, const uint8_t id[TOX_CONFERENCE_ID_SIZE], Tox_Err_Conference_By_Id *error)
{
assert(tox != nullptr);
@ -2663,7 +2669,7 @@ uint32_t tox_conference_by_id(const Tox *tox, const uint8_t *id, Tox_Err_Confere
}
// TODO(iphydf): Delete in 0.3.0.
uint32_t tox_conference_by_uid(const Tox *tox, const uint8_t *uid, Tox_Err_Conference_By_Uid *error)
uint32_t tox_conference_by_uid(const Tox *tox, const uint8_t uid[TOX_CONFERENCE_UID_SIZE], Tox_Err_Conference_By_Uid *error)
{
assert(tox != nullptr);
Tox_Err_Conference_By_Id id_error;
@ -2799,7 +2805,7 @@ void tox_callback_friend_lossless_packet(Tox *tox, tox_friend_lossless_packet_cb
}
}
void tox_self_get_dht_id(const Tox *tox, uint8_t *dht_id)
void tox_self_get_dht_id(const Tox *tox, uint8_t dht_id[TOX_PUBLIC_KEY_SIZE])
{
assert(tox != nullptr);
@ -3001,7 +3007,7 @@ uint32_t tox_group_new(Tox *tox, Tox_Group_Privacy_State privacy_state, const ui
return UINT32_MAX;
}
uint32_t tox_group_join(Tox *tox, const uint8_t *chat_id, const uint8_t *name, size_t name_length,
uint32_t tox_group_join(Tox *tox, const uint8_t chat_id[TOX_GROUP_CHAT_ID_SIZE], const uint8_t *name, size_t name_length,
const uint8_t *password, size_t password_length, Tox_Err_Group_Join *error)
{
assert(tox != nullptr);
@ -3187,7 +3193,7 @@ bool tox_group_leave(Tox *tox, uint32_t group_number, const uint8_t *part_messag
return false;
}
bool tox_group_self_set_name(const Tox *tox, uint32_t group_number, const uint8_t *name, size_t length,
bool tox_group_self_set_name(Tox *tox, uint32_t group_number, const uint8_t *name, size_t length,
Tox_Err_Group_Self_Name_Set *error)
{
assert(tox != nullptr);
@ -3271,7 +3277,7 @@ bool tox_group_self_get_name(const Tox *tox, uint32_t group_number, uint8_t *nam
return true;
}
bool tox_group_self_set_status(const Tox *tox, uint32_t group_number, Tox_User_Status status,
bool tox_group_self_set_status(Tox *tox, uint32_t group_number, Tox_User_Status status,
Tox_Err_Group_Self_Status_Set *error)
{
assert(tox != nullptr);
@ -3366,7 +3372,7 @@ uint32_t tox_group_self_get_peer_id(const Tox *tox, uint32_t group_number, Tox_E
return ret;
}
bool tox_group_self_get_public_key(const Tox *tox, uint32_t group_number, uint8_t *public_key,
bool tox_group_self_get_public_key(const Tox *tox, uint32_t group_number, uint8_t public_key[TOX_PUBLIC_KEY_SIZE],
Tox_Err_Group_Self_Query *error)
{
assert(tox != nullptr);
@ -3492,7 +3498,7 @@ Tox_Group_Role tox_group_peer_get_role(const Tox *tox, uint32_t group_number, ui
return (Tox_Group_Role)ret;
}
bool tox_group_peer_get_public_key(const Tox *tox, uint32_t group_number, uint32_t peer_id, uint8_t *public_key,
bool tox_group_peer_get_public_key(const Tox *tox, uint32_t group_number, uint32_t peer_id, uint8_t public_key[TOX_PUBLIC_KEY_SIZE],
Tox_Err_Group_Peer_Query *error)
{
assert(tox != nullptr);
@ -3544,7 +3550,7 @@ Tox_Connection tox_group_peer_get_connection_status(const Tox *tox, uint32_t gro
return (Tox_Connection)ret;
}
bool tox_group_set_topic(const Tox *tox, uint32_t group_number, const uint8_t *topic, size_t length,
bool tox_group_set_topic(Tox *tox, uint32_t group_number, const uint8_t *topic, size_t length,
Tox_Err_Group_Topic_Set *error)
{
assert(tox != nullptr);
@ -3662,7 +3668,7 @@ size_t tox_group_get_name_size(const Tox *tox, uint32_t group_number, Tox_Err_Gr
return ret;
}
bool tox_group_get_name(const Tox *tox, uint32_t group_number, uint8_t *group_name, Tox_Err_Group_State_Queries *error)
bool tox_group_get_name(const Tox *tox, uint32_t group_number, uint8_t *name, Tox_Err_Group_State_Queries *error)
{
assert(tox != nullptr);
@ -3675,7 +3681,7 @@ bool tox_group_get_name(const Tox *tox, uint32_t group_number, uint8_t *group_na
return false;
}
gc_get_group_name(chat, group_name);
gc_get_group_name(chat, name);
tox_unlock(tox);
SET_ERROR_PARAMETER(error, TOX_ERR_GROUP_STATE_QUERIES_OK);
@ -3683,7 +3689,7 @@ bool tox_group_get_name(const Tox *tox, uint32_t group_number, uint8_t *group_na
return true;
}
bool tox_group_get_chat_id(const Tox *tox, uint32_t group_number, uint8_t *chat_id, Tox_Err_Group_State_Queries *error)
bool tox_group_get_chat_id(const Tox *tox, uint32_t group_number, uint8_t chat_id[TOX_GROUP_CHAT_ID_SIZE], Tox_Err_Group_State_Queries *error)
{
assert(tox != nullptr);
@ -3843,8 +3849,9 @@ bool tox_group_get_password(const Tox *tox, uint32_t group_number, uint8_t *pass
return true;
}
bool tox_group_send_message(const Tox *tox, uint32_t group_number, Tox_Message_Type type, const uint8_t *message,
size_t length, uint32_t *message_id, Tox_Err_Group_Send_Message *error)
Tox_Group_Message_Id tox_group_send_message(
const Tox *tox, uint32_t group_number, Tox_Message_Type type, const uint8_t *message,
size_t length, Tox_Err_Group_Send_Message *error)
{
assert(tox != nullptr);
@ -3854,54 +3861,55 @@ bool tox_group_send_message(const Tox *tox, uint32_t group_number, Tox_Message_T
if (chat == nullptr) {
SET_ERROR_PARAMETER(error, TOX_ERR_GROUP_SEND_MESSAGE_GROUP_NOT_FOUND);
tox_unlock(tox);
return false;
return -1;
}
if (chat->connection_state == CS_DISCONNECTED) {
SET_ERROR_PARAMETER(error, TOX_ERR_GROUP_SEND_MESSAGE_DISCONNECTED);
tox_unlock(tox);
return false;
return -1;
}
const int ret = gc_send_message(chat, message, length, type, message_id);
uint32_t message_id = 0;
const int ret = gc_send_message(chat, message, length, type, &message_id);
tox_unlock(tox);
switch (ret) {
case 0: {
SET_ERROR_PARAMETER(error, TOX_ERR_GROUP_SEND_MESSAGE_OK);
return true;
return message_id;
}
case -1: {
SET_ERROR_PARAMETER(error, TOX_ERR_GROUP_SEND_MESSAGE_TOO_LONG);
return false;
return -1;
}
case -2: {
SET_ERROR_PARAMETER(error, TOX_ERR_GROUP_SEND_MESSAGE_EMPTY);
return false;
return -1;
}
case -3: {
SET_ERROR_PARAMETER(error, TOX_ERR_GROUP_SEND_MESSAGE_BAD_TYPE);
return false;
return -1;
}
case -4: {
SET_ERROR_PARAMETER(error, TOX_ERR_GROUP_SEND_MESSAGE_PERMISSIONS);
return false;
return -1;
}
case -5: {
SET_ERROR_PARAMETER(error, TOX_ERR_GROUP_SEND_MESSAGE_FAIL_SEND);
return false;
return -1;
}
}
/* can't happen */
LOGGER_FATAL(tox->m->log, "impossible return value: %d", ret);
return false;
return -1;
}
bool tox_group_send_private_message(const Tox *tox, uint32_t group_number, uint32_t peer_id, Tox_Message_Type type,
@ -4199,7 +4207,7 @@ uint32_t tox_group_invite_accept(Tox *tox, uint32_t friend_number, const uint8_t
return UINT32_MAX;
}
bool tox_group_founder_set_password(const Tox *tox, uint32_t group_number, const uint8_t *password, size_t length,
bool tox_group_founder_set_password(Tox *tox, uint32_t group_number, const uint8_t *password, size_t length,
Tox_Err_Group_Founder_Set_Password *error)
{
assert(tox != nullptr);
@ -4255,7 +4263,7 @@ bool tox_group_founder_set_password(const Tox *tox, uint32_t group_number, const
return false;
}
bool tox_group_founder_set_privacy_state(const Tox *tox, uint32_t group_number, Tox_Group_Privacy_State privacy_state,
bool tox_group_founder_set_privacy_state(Tox *tox, uint32_t group_number, Tox_Group_Privacy_State privacy_state,
Tox_Err_Group_Founder_Set_Privacy_State *error)
{
assert(tox != nullptr);
@ -4302,7 +4310,7 @@ bool tox_group_founder_set_privacy_state(const Tox *tox, uint32_t group_number,
return false;
}
bool tox_group_founder_set_topic_lock(const Tox *tox, uint32_t group_number, Tox_Group_Topic_Lock topic_lock,
bool tox_group_founder_set_topic_lock(Tox *tox, uint32_t group_number, Tox_Group_Topic_Lock topic_lock,
Tox_Err_Group_Founder_Set_Topic_Lock *error)
{
assert(tox != nullptr);
@ -4354,7 +4362,7 @@ bool tox_group_founder_set_topic_lock(const Tox *tox, uint32_t group_number, Tox
return false;
}
bool tox_group_founder_set_voice_state(const Tox *tox, uint32_t group_number, Tox_Group_Voice_State voice_state,
bool tox_group_founder_set_voice_state(Tox *tox, uint32_t group_number, Tox_Group_Voice_State voice_state,
Tox_Err_Group_Founder_Set_Voice_State *error)
{
assert(tox != nullptr);
@ -4401,7 +4409,7 @@ bool tox_group_founder_set_voice_state(const Tox *tox, uint32_t group_number, To
return false;
}
bool tox_group_founder_set_peer_limit(const Tox *tox, uint32_t group_number, uint16_t max_peers,
bool tox_group_founder_set_peer_limit(Tox *tox, uint32_t group_number, uint16_t peer_limit,
Tox_Err_Group_Founder_Set_Peer_Limit *error)
{
assert(tox != nullptr);
@ -4421,7 +4429,7 @@ bool tox_group_founder_set_peer_limit(const Tox *tox, uint32_t group_number, uin
return false;
}
const int ret = gc_founder_set_max_peers(chat, max_peers);
const int ret = gc_founder_set_max_peers(chat, peer_limit);
tox_unlock(tox);
switch (ret) {
@ -4452,7 +4460,7 @@ bool tox_group_founder_set_peer_limit(const Tox *tox, uint32_t group_number, uin
return false;
}
bool tox_group_set_ignore(const Tox *tox, uint32_t group_number, uint32_t peer_id, bool ignore,
bool tox_group_set_ignore(Tox *tox, uint32_t group_number, uint32_t peer_id, bool ignore,
Tox_Err_Group_Set_Ignore *error)
{
assert(tox != nullptr);
@ -4492,7 +4500,7 @@ bool tox_group_set_ignore(const Tox *tox, uint32_t group_number, uint32_t peer_i
return false;
}
bool tox_group_mod_set_role(const Tox *tox, uint32_t group_number, uint32_t peer_id, Tox_Group_Role role,
bool tox_group_mod_set_role(Tox *tox, uint32_t group_number, uint32_t peer_id, Tox_Group_Role role,
Tox_Err_Group_Mod_Set_Role *error)
{
assert(tox != nullptr);

File diff suppressed because it is too large Load Diff

View File

@ -4,7 +4,6 @@
#include "tox.h"
#include <stdlib.h>
#include <string.h>
#include "ccompat.h"
#include "tox_private.h"
@ -139,33 +138,33 @@ uint32_t tox_dht_node_public_key_size(void)
//!TOKSTYLE-
#define ACCESSORS(type, ns, name) \
type tox_options_get_##ns##name(const struct Tox_Options *options) \
#define ACCESSORS(type, name) \
type tox_options_get_##name(const struct Tox_Options *options) \
{ \
return options->ns##name; \
return options->name; \
} \
void tox_options_set_##ns##name(struct Tox_Options *options, type name) \
void tox_options_set_##name(struct Tox_Options *options, type name) \
{ \
options->ns##name = name; \
options->name = name; \
}
ACCESSORS(bool,, ipv6_enabled)
ACCESSORS(bool,, udp_enabled)
ACCESSORS(Tox_Proxy_Type, proxy_, type)
ACCESSORS(const char *, proxy_, host)
ACCESSORS(uint16_t, proxy_, port)
ACCESSORS(uint16_t,, start_port)
ACCESSORS(uint16_t,, end_port)
ACCESSORS(uint16_t,, tcp_port)
ACCESSORS(bool,, hole_punching_enabled)
ACCESSORS(Tox_Savedata_Type, savedata_, type)
ACCESSORS(size_t, savedata_, length)
ACCESSORS(tox_log_cb *, log_, callback)
ACCESSORS(void *, log_, user_data)
ACCESSORS(bool,, local_discovery_enabled)
ACCESSORS(bool,, dht_announcements_enabled)
ACCESSORS(bool,, experimental_thread_safety)
ACCESSORS(const Tox_System *,, operating_system)
ACCESSORS(bool, ipv6_enabled)
ACCESSORS(bool, udp_enabled)
ACCESSORS(Tox_Proxy_Type, proxy_type)
ACCESSORS(const char *, proxy_host)
ACCESSORS(uint16_t, proxy_port)
ACCESSORS(uint16_t, start_port)
ACCESSORS(uint16_t, end_port)
ACCESSORS(uint16_t, tcp_port)
ACCESSORS(bool, hole_punching_enabled)
ACCESSORS(Tox_Savedata_Type, savedata_type)
ACCESSORS(size_t, savedata_length)
ACCESSORS(tox_log_cb *, log_callback)
ACCESSORS(void *, log_user_data)
ACCESSORS(bool, local_discovery_enabled)
ACCESSORS(bool, dht_announcements_enabled)
ACCESSORS(bool, experimental_thread_safety)
ACCESSORS(const Tox_System *, operating_system)
//!TOKSTYLE+
@ -174,9 +173,9 @@ const uint8_t *tox_options_get_savedata_data(const struct Tox_Options *options)
return options->savedata_data;
}
void tox_options_set_savedata_data(struct Tox_Options *options, const uint8_t *data, size_t length)
void tox_options_set_savedata_data(struct Tox_Options *options, const uint8_t *savedata_data, size_t length)
{
options->savedata_data = data;
options->savedata_data = savedata_data;
options->savedata_length = length;
}

View File

@ -7,8 +7,10 @@
#include <stdlib.h>
#include "ccompat.h"
#include "events/events_alloc.h"
#include "events/events_alloc.h" // IWYU pragma: keep
#include "tox.h"
#include "tox_event.h"
#include "tox_events.h"
struct Tox_Dispatch {
tox_events_conference_connected_cb *conference_connected_callback;

View File

@ -5,14 +5,12 @@
#include "tox_event.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bin_pack.h"
#include "bin_unpack.h"
#include "ccompat.h"
#include "events/events_alloc.h"
#include "tox.h"
#include "mem.h"
#include "tox_events.h"
const char *tox_event_type_to_string(Tox_Event_Type type)
{

View File

@ -5,16 +5,18 @@
#include "tox_events.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bin_pack.h"
#include "bin_unpack.h"
#include "ccompat.h"
#include "events/events_alloc.h"
#include "logger.h"
#include "mem.h"
#include "tox.h"
#include "tox_event.h"
#include "tox_private.h"
#include "tox_struct.h"
/*****************************************************
@ -143,7 +145,8 @@ bool tox_events_unpack(Tox_Events *events, Bin_Unpack *bu, const Memory *mem)
non_null(1) nullable(2, 3)
static bool tox_events_bin_pack_handler(Bin_Pack *bp, const Logger *logger, const void *obj)
{
return tox_events_pack((const Tox_Events *)obj, bp);
const Tox_Events *events = (const Tox_Events *)obj;
return tox_events_pack(events, bp);
}
uint32_t tox_events_bytes_size(const Tox_Events *events)

View File

@ -48,14 +48,14 @@ TEST(ToxEvents, NullEventsPacksToEmptyArray)
TEST(ToxEvents, PackedEventsCanBeUnpacked)
{
const Tox_System sys = tox_default_system();
// [[1, 1]] == Tox_Self_Connection_Status { .connection_status = TOX_CONNECTION_TCP }
std::array<uint8_t, 6> packed{0x91, 0x92, 0xcc, 0x01, 0xcc, 0x01};
// [[0, 1]] == Tox_Self_Connection_Status { .connection_status = TOX_CONNECTION_TCP }
std::array<uint8_t, 6> packed{0x91, 0x92, 0xcc, 0x00, 0xcc, 0x01};
Tox_Events *events = tox_events_load(&sys, packed.data(), packed.size());
ASSERT_NE(events, nullptr);
std::array<uint8_t, 4> bytes;
ASSERT_EQ(tox_events_bytes_size(events), bytes.size());
tox_events_get_bytes(events, bytes.data());
EXPECT_EQ(bytes, (std::array<uint8_t, 4>{0x91, 0x92, 0x01, 0x01}));
EXPECT_EQ(bytes, (std::array<uint8_t, 4>{0x91, 0x92, 0x00, 0x01}));
tox_events_free(events);
}

View File

@ -10,10 +10,15 @@
#include <assert.h>
#include "DHT.h"
#include "ccompat.h"
#include "crypto_core.h"
#include "group_chats.h"
#include "group_common.h"
#include "mem.h"
#include "net_crypto.h"
#include "network.h"
#include "tox.h"
#include "tox_struct.h"
#define SET_ERROR_PARAMETER(param, x) \

View File

@ -31,6 +31,8 @@ Tox_System tox_default_system(void);
void tox_lock(const Tox *tox);
void tox_unlock(const Tox *tox);
const Tox_System *tox_get_system(Tox *tox);
/**
* Set the callback for the `friend_lossy_packet` event for a specific packet ID.
* Pass NULL to unset.

View File

@ -7,7 +7,7 @@
#include <stdint.h>
#include "bin_unpack.h"
#include "ccompat.h"
#include "tox.h"
non_null()
static bool tox_conference_type_from_int(uint32_t value, Tox_Conference_Type *out)

View File

@ -13,11 +13,10 @@
#include "util.h"
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "ccompat.h"
#include "mem.h"
bool is_power_of_2(uint64_t x)
{