summaryrefslogtreecommitdiff
path: root/protocols/Tox/libtox/src/toxcore/group_chats.c
diff options
context:
space:
mode:
Diffstat (limited to 'protocols/Tox/libtox/src/toxcore/group_chats.c')
-rw-r--r--protocols/Tox/libtox/src/toxcore/group_chats.c78
1 files changed, 39 insertions, 39 deletions
diff --git a/protocols/Tox/libtox/src/toxcore/group_chats.c b/protocols/Tox/libtox/src/toxcore/group_chats.c
index 96f647cc2e..2d64ae60ec 100644
--- a/protocols/Tox/libtox/src/toxcore/group_chats.c
+++ b/protocols/Tox/libtox/src/toxcore/group_chats.c
@@ -961,7 +961,7 @@ non_null() static bool broadcast_gc_mod_list(const GC_Chat *chat);
non_null() static bool broadcast_gc_shared_state(const GC_Chat *chat);
non_null() static bool update_gc_sanctions_list(GC_Chat *chat, const uint8_t *public_sig_key);
non_null() static bool update_gc_topic(GC_Chat *chat, const uint8_t *public_sig_key);
-non_null() static bool send_gc_set_observer(const GC_Chat *chat, const uint8_t *target_ext_pk,
+non_null() static bool send_gc_set_observer(const GC_Chat *chat, const Extended_Public_Key *target_ext_pk,
const uint8_t *sanction_data, uint16_t length, bool add_obs);
/** Returns true if peer designated by `peer_number` is in the sanctions list as an observer. */
@@ -1119,7 +1119,7 @@ static bool prune_gc_mod_list(GC_Chat *chat)
non_null()
static bool prune_gc_sanctions_list_inner(
GC_Chat *chat, const Mod_Sanction *sanction,
- const uint8_t target_ext_pk[ENC_PUBLIC_KEY_SIZE + SIG_PUBLIC_KEY_SIZE])
+ const Extended_Public_Key *target_ext_pk)
{
if (!sanctions_list_remove_observer(&chat->moderation, sanction->target_public_enc_key, nullptr)) {
LOGGER_WARNING(chat->log, "Failed to remove entry from observer list");
@@ -1159,10 +1159,10 @@ static bool prune_gc_sanctions_list(GC_Chat *chat)
if (peer_number == -1) {
const Mod_Sanction *sanction = &chat->moderation.sanctions[i];
- uint8_t target_ext_pk[ENC_PUBLIC_KEY_SIZE + SIG_PUBLIC_KEY_SIZE];
- memcpy(target_ext_pk, sanction->target_public_enc_key, ENC_PUBLIC_KEY_SIZE);
- memcpy(target_ext_pk + ENC_PUBLIC_KEY_SIZE, sanction->setter_public_sig_key, SIG_PUBLIC_KEY_SIZE);
- return prune_gc_sanctions_list_inner(chat, sanction, target_ext_pk);
+ Extended_Public_Key target_ext_pk;
+ memcpy(target_ext_pk.enc, sanction->target_public_enc_key, ENC_PUBLIC_KEY_SIZE);
+ memcpy(target_ext_pk.sig, sanction->setter_public_sig_key, SIG_PUBLIC_KEY_SIZE);
+ return prune_gc_sanctions_list_inner(chat, sanction, &target_ext_pk);
}
}
@@ -4457,10 +4457,10 @@ static int handle_gc_set_observer(const GC_Session *c, GC_Chat *chat, uint32_t p
* Returns true on success.
*/
non_null()
-static bool send_gc_set_observer(const GC_Chat *chat, const uint8_t *target_ext_pk, const uint8_t *sanction_data,
- uint16_t length, bool add_obs)
+static bool send_gc_set_observer(const GC_Chat *chat, const Extended_Public_Key *target_ext_pk,
+ const uint8_t *sanction_data, uint16_t length, bool add_obs)
{
- const uint16_t packet_len = 1 + EXT_PUBLIC_KEY_SIZE + length;
+ const uint16_t packet_len = 1 + ENC_PUBLIC_KEY_SIZE + SIG_PUBLIC_KEY_SIZE + length;
uint8_t *packet = (uint8_t *)malloc(packet_len);
if (packet == nullptr) {
@@ -4469,8 +4469,9 @@ static bool send_gc_set_observer(const GC_Chat *chat, const uint8_t *target_ext_
net_pack_bool(&packet[0], add_obs);
- memcpy(packet + 1, target_ext_pk, EXT_PUBLIC_KEY_SIZE);
- memcpy(packet + 1 + EXT_PUBLIC_KEY_SIZE, sanction_data, length);
+ memcpy(packet + 1, target_ext_pk->enc, ENC_PUBLIC_KEY_SIZE);
+ memcpy(packet + 1 + ENC_PUBLIC_KEY_SIZE, target_ext_pk->sig, SIG_PUBLIC_KEY_SIZE);
+ memcpy(packet + 1 + ENC_PUBLIC_KEY_SIZE + SIG_PUBLIC_KEY_SIZE, sanction_data, length);
if (!send_gc_broadcast_message(chat, packet, packet_len, GM_SET_OBSERVER)) {
free(packet);
@@ -4557,7 +4558,7 @@ static bool mod_gc_set_observer(GC_Chat *chat, uint32_t peer_number, bool add_ob
update_gc_peer_roles(chat);
- return send_gc_set_observer(chat, gconn->addr.public_key.enc, sanction_data, length, add_obs);
+ return send_gc_set_observer(chat, &gconn->addr.public_key, sanction_data, length, add_obs);
}
/** @brief Sets the role of `peer_number` to `new_role`. If necessary this function will first
@@ -4910,11 +4911,12 @@ int gc_send_message(const GC_Chat *chat, const uint8_t *message, uint16_t length
return -5;
}
+ free(message_raw);
+
if (message_id != nullptr) {
*message_id = pseudo_msg_id;
}
- free(message_raw);
return 0;
}
@@ -4954,7 +4956,7 @@ static int handle_gc_message(const GC_Session *c, const GC_Chat *chat, const GC_
}
int gc_send_private_message(const GC_Chat *chat, GC_Peer_Id peer_id, uint8_t type, const uint8_t *message,
- uint16_t length)
+ uint16_t length, uint32_t *message_id)
{
if (length > MAX_GC_MESSAGE_SIZE) {
return -1;
@@ -4980,23 +4982,28 @@ int gc_send_private_message(const GC_Chat *chat, GC_Peer_Id peer_id, uint8_t typ
return -5;
}
- uint8_t *message_with_type = (uint8_t *)malloc(length + 1);
+ const uint16_t raw_length = 1 + length + GC_MESSAGE_PSEUDO_ID_SIZE;
+ uint8_t *message_with_type = (uint8_t *)malloc(raw_length);
if (message_with_type == nullptr) {
return -6;
}
message_with_type[0] = type;
- memcpy(message_with_type + 1, message, length);
- uint8_t *packet = (uint8_t *)malloc(length + 1 + GC_BROADCAST_ENC_HEADER_SIZE);
+ const uint32_t pseudo_msg_id = random_u32(chat->rng);
+ net_pack_u32(message_with_type + 1, pseudo_msg_id);
+
+ memcpy(message_with_type + 1 + GC_MESSAGE_PSEUDO_ID_SIZE, message, length);
+
+ uint8_t *packet = (uint8_t *)malloc(raw_length + GC_BROADCAST_ENC_HEADER_SIZE);
if (packet == nullptr) {
free(message_with_type);
return -6;
}
- const uint16_t packet_len = make_gc_broadcast_header(message_with_type, length + 1, packet, GM_PRIVATE_MESSAGE);
+ const uint16_t packet_len = make_gc_broadcast_header(message_with_type, raw_length, packet, GM_PRIVATE_MESSAGE);
free(message_with_type);
@@ -5007,6 +5014,10 @@ int gc_send_private_message(const GC_Chat *chat, GC_Peer_Id peer_id, uint8_t typ
free(packet);
+ if (message_id != nullptr) {
+ *message_id = pseudo_msg_id;
+ }
+
return 0;
}
@@ -5019,7 +5030,7 @@ non_null(1, 2, 3, 4) nullable(6)
static int handle_gc_private_message(const GC_Session *c, const GC_Chat *chat, const GC_Peer *peer, const uint8_t *data,
uint16_t length, void *userdata)
{
- if (data == nullptr || length > MAX_GC_MESSAGE_SIZE || length <= 1) {
+ if (data == nullptr || length > MAX_GC_MESSAGE_SIZE || length <= 1 + GC_MESSAGE_PSEUDO_ID_SIZE) {
return -1;
}
@@ -5034,8 +5045,13 @@ static int handle_gc_private_message(const GC_Session *c, const GC_Chat *chat, c
return 0;
}
+ uint32_t message_id;
+ net_unpack_u32(data + 1, &message_id);
+
if (c->private_message != nullptr) {
- c->private_message(c->messenger, chat->group_number, peer->peer_id, message_type, data + 1, length - 1, userdata);
+ c->private_message(c->messenger, chat->group_number, peer->peer_id, message_type,
+ data + 1 + GC_MESSAGE_PSEUDO_ID_SIZE, length - 1 - GC_MESSAGE_PSEUDO_ID_SIZE,
+ message_id, userdata);
}
return 0;
@@ -5066,10 +5082,6 @@ int gc_send_custom_private_packet(const GC_Chat *chat, bool lossless, GC_Peer_Id
return -3;
}
- if (gc_get_self_role(chat) >= GR_OBSERVER) {
- return -4;
- }
-
bool ret;
if (lossless) {
@@ -5078,7 +5090,7 @@ int gc_send_custom_private_packet(const GC_Chat *chat, bool lossless, GC_Peer_Id
ret = send_lossy_group_packet(chat, gconn, message, length, GP_CUSTOM_PRIVATE_PACKET);
}
- return ret ? 0 : -5;
+ return ret ? 0 : -4;
}
/** @brief Handles a custom private packet.
@@ -5098,10 +5110,6 @@ static int handle_gc_custom_private_packet(const GC_Session *c, const GC_Chat *c
return -1;
}
- if (peer->ignore || peer->role >= GR_OBSERVER) {
- return 0;
- }
-
if (c->custom_private_packet != nullptr) {
c->custom_private_packet(c->messenger, chat->group_number, peer->peer_id, data, length, userdata);
}
@@ -5119,10 +5127,6 @@ int gc_send_custom_packet(const GC_Chat *chat, bool lossless, const uint8_t *dat
return -2;
}
- if (gc_get_self_role(chat) >= GR_OBSERVER) {
- return -3;
- }
-
bool success;
if (lossless) {
@@ -5131,7 +5135,7 @@ int gc_send_custom_packet(const GC_Chat *chat, bool lossless, const uint8_t *dat
success = send_gc_lossy_packet_all_peers(chat, data, length, GP_CUSTOM_PACKET);
}
- return success ? 0 : -4;
+ return success ? 0 : -3;
}
/** @brief Handles a custom packet.
@@ -5151,10 +5155,6 @@ static int handle_gc_custom_packet(const GC_Session *c, const GC_Chat *chat, con
return -1;
}
- if (peer->ignore || peer->role >= GR_OBSERVER) {
- return 0;
- }
-
if (c->custom_packet != nullptr) {
c->custom_packet(c->messenger, chat->group_number, peer->peer_id, data, length, userdata);
}
@@ -8353,7 +8353,7 @@ bool gc_group_is_valid(const GC_Chat *chat)
/** Return true if `group_number` designates an active group in session `c`. */
static bool group_number_valid(const GC_Session *c, int group_number)
{
- if (group_number < 0 || group_number >= c->chats_index) {
+ if (group_number < 0 || (uint32_t)group_number >= c->chats_index) {
return false;
}