Skip to content

Commit

Permalink
refactor: Use a struct wrapper for GC_Peer_Id.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Feb 6, 2024
1 parent 911cf87 commit b95b67c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 34 deletions.
45 changes: 36 additions & 9 deletions toxcore/group_chats.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@
/* The value the topic lock is set to when the topic lock is enabled. */
#define GC_TOPIC_LOCK_ENABLED 0

#define GC_INVALID_PEER_ID ((force GC_Peer_Id)-1)

static_assert(GCC_BUFFER_SIZE <= UINT16_MAX,
"GCC_BUFFER_SIZE must be <= UINT16_MAX)");

Expand Down Expand Up @@ -179,6 +177,35 @@ non_null() static bool saved_peer_is_valid(const GC_SavedPeerInfo *saved_peer);

static const GC_Chat empty_gc_chat = {nullptr};

#define GC_INVALID_PEER_ID_VALUE ((force GC_Peer_Id_Value)-1)

static GC_Peer_Id gc_invalid_peer_id(void)
{
const GC_Peer_Id invalid = {GC_INVALID_PEER_ID_VALUE};
return invalid;
}

static bool gc_peer_id_is_valid(GC_Peer_Id peer_id)
{
return peer_id.value == GC_INVALID_PEER_ID_VALUE;
}

GC_Peer_Id gc_peer_id_from_int(uint32_t value)
{
const GC_Peer_Id peer_id = {(force GC_Peer_Id_Value)value};
return peer_id;
}

uint32_t gc_peer_id_to_int(GC_Peer_Id peer_id)
{
return (force uint32_t)peer_id.value;
}

static GC_Peer_Id gc_unknown_peer_id(void)
{
return gc_peer_id_from_int(0);
}

non_null()
static void kill_group_friend_connection(const GC_Session *c, const GC_Chat *chat)
{
Expand Down Expand Up @@ -694,7 +721,7 @@ non_null()
static int get_peer_number_of_peer_id(const GC_Chat *chat, GC_Peer_Id peer_id)
{
for (uint32_t i = 0; i < chat->numpeers; ++i) {
if (chat->group[i].peer_id == peer_id) {
if (chat->group[i].peer_id.value == peer_id.value) {
return i;
}
}
Expand All @@ -712,13 +739,13 @@ non_null()
static GC_Peer_Id get_new_peer_id(const GC_Chat *chat)
{
for (uint32_t i = 0; i < UINT32_MAX - 1; ++i) {
const GC_Peer_Id peer_id = (force GC_Peer_Id)i;
const GC_Peer_Id peer_id = gc_peer_id_from_int(i);
if (get_peer_number_of_peer_id(chat, peer_id) == -1) {
return peer_id;
}
}

return GC_INVALID_PEER_ID;
return gc_invalid_peer_id();
}

/** @brief Sets the password for the group (locally only).
Expand Down Expand Up @@ -3072,7 +3099,7 @@ static int handle_gc_mod_list(const GC_Session *c, GC_Chat *chat, const uint8_t
update_gc_peer_roles(chat);

if (chat->connection_state == CS_CONNECTED && c->moderation != nullptr) {
c->moderation(c->messenger, chat->group_number, GC_INVALID_PEER_ID, GC_INVALID_PEER_ID, MV_MOD, userdata);
c->moderation(c->messenger, chat->group_number, gc_invalid_peer_id(), gc_invalid_peer_id(), MV_MOD, userdata);
}

return 0;
Expand Down Expand Up @@ -3193,7 +3220,7 @@ static int handle_gc_sanctions_list(const GC_Session *c, GC_Chat *chat, const ui

if (chat->connection_state == CS_CONNECTED) {
if (c->moderation != nullptr) {
c->moderation(c->messenger, chat->group_number, GC_INVALID_PEER_ID, GC_INVALID_PEER_ID, MV_OBSERVER, userdata);
c->moderation(c->messenger, chat->group_number, gc_invalid_peer_id(), gc_invalid_peer_id(), MV_OBSERVER, userdata);
}
}

Expand Down Expand Up @@ -3973,7 +4000,7 @@ static int handle_gc_topic(const GC_Session *c, GC_Chat *chat, const GC_Peer *pe

if (!skip_callback && chat->connection_state == CS_CONNECTED && c->topic_change != nullptr) {
const int setter_peer_number = get_peer_number_of_sig_pk(chat, topic_info.public_sig_key);
const GC_Peer_Id peer_id = setter_peer_number >= 0 ? chat->group[setter_peer_number].peer_id : 0;
const GC_Peer_Id peer_id = setter_peer_number >= 0 ? chat->group[setter_peer_number].peer_id : gc_unknown_peer_id();

c->topic_change(c->messenger, chat->group_number, peer_id, topic_info.topic, topic_info.length, userdata);
}
Expand Down Expand Up @@ -6765,7 +6792,7 @@ int peer_add(GC_Chat *chat, const IP_Port *ipp, const uint8_t *public_key)

const GC_Peer_Id peer_id = get_new_peer_id(chat);

if (peer_id == GC_INVALID_PEER_ID) {
if (gc_peer_id_is_valid(peer_id)) {
LOGGER_WARNING(chat->log, "Failed to add peer: all peer ID's are taken?");
return -1;
}
Expand Down
9 changes: 8 additions & 1 deletion toxcore/group_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,14 @@ 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;
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 */
Expand Down
44 changes: 22 additions & 22 deletions toxcore/tox.c
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ static void tox_group_peer_name_handler(const Messenger *m, uint32_t group_numbe
struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;

if (tox_data->tox->group_peer_name_callback != nullptr) {
tox_data->tox->group_peer_name_callback(tox_data->tox, group_number, (force uint32_t)peer_id, name, length, tox_data->user_data);
tox_data->tox->group_peer_name_callback(tox_data->tox, group_number, gc_peer_id_to_int(peer_id), name, length, tox_data->user_data);
}
}

Expand All @@ -388,7 +388,7 @@ static void tox_group_peer_status_handler(const Messenger *m, uint32_t group_num
struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;

if (tox_data->tox->group_peer_status_callback != nullptr) {
tox_data->tox->group_peer_status_callback(tox_data->tox, group_number, (force uint32_t)peer_id, (Tox_User_Status)status,
tox_data->tox->group_peer_status_callback(tox_data->tox, group_number, gc_peer_id_to_int(peer_id), (Tox_User_Status)status,
tox_data->user_data);
}
}
Expand All @@ -400,7 +400,7 @@ static void tox_group_topic_handler(const Messenger *m, uint32_t group_number, G
struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;

if (tox_data->tox->group_topic_callback != nullptr) {
tox_data->tox->group_topic_callback(tox_data->tox, group_number, (force uint32_t)peer_id, topic, length, tox_data->user_data);
tox_data->tox->group_topic_callback(tox_data->tox, group_number, gc_peer_id_to_int(peer_id), topic, length, tox_data->user_data);
}
}

Expand Down Expand Up @@ -469,7 +469,7 @@ static void tox_group_message_handler(const Messenger *m, uint32_t group_number,
struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;

if (tox_data->tox->group_message_callback != nullptr) {
tox_data->tox->group_message_callback(tox_data->tox, group_number, (force uint32_t)peer_id, (Tox_Message_Type)type, message, length,
tox_data->tox->group_message_callback(tox_data->tox, group_number, gc_peer_id_to_int(peer_id), (Tox_Message_Type)type, message, length,
message_id, tox_data->user_data);
}
}
Expand All @@ -481,7 +481,7 @@ static void tox_group_private_message_handler(const Messenger *m, uint32_t group
struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;

if (tox_data->tox->group_private_message_callback != nullptr) {
tox_data->tox->group_private_message_callback(tox_data->tox, group_number, (force uint32_t)peer_id, (Tox_Message_Type)type, message,
tox_data->tox->group_private_message_callback(tox_data->tox, group_number, gc_peer_id_to_int(peer_id), (Tox_Message_Type)type, message,
length,
tox_data->user_data);
}
Expand All @@ -494,7 +494,7 @@ static void tox_group_custom_packet_handler(const Messenger *m, uint32_t group_n
struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;

if (tox_data->tox->group_custom_packet_callback != nullptr) {
tox_data->tox->group_custom_packet_callback(tox_data->tox, group_number, (force uint32_t)peer_id, data, length, tox_data->user_data);
tox_data->tox->group_custom_packet_callback(tox_data->tox, group_number, gc_peer_id_to_int(peer_id), data, length, tox_data->user_data);
}
}

Expand All @@ -505,7 +505,7 @@ static void tox_group_custom_private_packet_handler(const Messenger *m, uint32_t
struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;

if (tox_data->tox->group_custom_private_packet_callback != nullptr) {
tox_data->tox->group_custom_private_packet_callback(tox_data->tox, group_number, (force uint32_t)peer_id, data, length,
tox_data->tox->group_custom_private_packet_callback(tox_data->tox, group_number, gc_peer_id_to_int(peer_id), data, length,
tox_data->user_data);
}
}
Expand All @@ -528,7 +528,7 @@ static void tox_group_peer_join_handler(const Messenger *m, uint32_t group_numbe
struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;

if (tox_data->tox->group_peer_join_callback != nullptr) {
tox_data->tox->group_peer_join_callback(tox_data->tox, group_number, (force uint32_t)peer_id, tox_data->user_data);
tox_data->tox->group_peer_join_callback(tox_data->tox, group_number, gc_peer_id_to_int(peer_id), tox_data->user_data);
}
}

Expand All @@ -540,7 +540,7 @@ static void tox_group_peer_exit_handler(const Messenger *m, uint32_t group_numbe
struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;

if (tox_data->tox->group_peer_exit_callback != nullptr) {
tox_data->tox->group_peer_exit_callback(tox_data->tox, group_number, (force uint32_t)peer_id, (Tox_Group_Exit_Type)exit_type, name,
tox_data->tox->group_peer_exit_callback(tox_data->tox, group_number, gc_peer_id_to_int(peer_id), (Tox_Group_Exit_Type)exit_type, name,
name_length, part_message, length, tox_data->user_data);
}
}
Expand Down Expand Up @@ -575,7 +575,7 @@ static void tox_group_moderation_handler(const Messenger *m, uint32_t group_numb

if (tox_data->tox->group_moderation_callback != nullptr) {
tox_data->tox->group_moderation_callback(tox_data->tox, group_number,
(force uint32_t)source_peer_number, (force uint32_t)target_peer_number,
gc_peer_id_to_int(source_peer_number), gc_peer_id_to_int(target_peer_number),
(Tox_Group_Mod_Event)mod_type, tox_data->user_data);
}
}
Expand Down Expand Up @@ -3373,7 +3373,7 @@ uint32_t tox_group_self_get_peer_id(const Tox *tox, uint32_t group_number, Tox_E
const GC_Peer_Id ret = gc_get_self_peer_id(chat);
tox_unlock(tox);

return (force uint32_t)ret;
return gc_peer_id_to_int(ret);
}

bool tox_group_self_get_public_key(const Tox *tox, uint32_t group_number, uint8_t public_key[TOX_PUBLIC_KEY_SIZE],
Expand Down Expand Up @@ -3412,7 +3412,7 @@ size_t tox_group_peer_get_name_size(const Tox *tox, uint32_t group_number, uint3
return -1;
}

const int ret = gc_get_peer_nick_size(chat, (force GC_Peer_Id)peer_id);
const int ret = gc_get_peer_nick_size(chat, gc_peer_id_from_int(peer_id));
tox_unlock(tox);

if (ret == -1) {
Expand All @@ -3438,7 +3438,7 @@ bool tox_group_peer_get_name(const Tox *tox, uint32_t group_number, uint32_t pee
return false;
}

const bool ret = gc_get_peer_nick(chat, (force GC_Peer_Id)peer_id, name);
const bool ret = gc_get_peer_nick(chat, gc_peer_id_from_int(peer_id), name);
tox_unlock(tox);

if (!ret) {
Expand All @@ -3464,7 +3464,7 @@ Tox_User_Status tox_group_peer_get_status(const Tox *tox, uint32_t group_number,
return (Tox_User_Status) - 1;
}

const uint8_t ret = gc_get_status(chat, (force GC_Peer_Id)peer_id);
const uint8_t ret = gc_get_status(chat, gc_peer_id_from_int(peer_id));
tox_unlock(tox);

if (ret == UINT8_MAX) {
Expand All @@ -3490,7 +3490,7 @@ Tox_Group_Role tox_group_peer_get_role(const Tox *tox, uint32_t group_number, ui
return (Tox_Group_Role) - 1;
}

const uint8_t ret = gc_get_role(chat, (force GC_Peer_Id)peer_id);
const uint8_t ret = gc_get_role(chat, gc_peer_id_from_int(peer_id));
tox_unlock(tox);

if (ret == (uint8_t) -1) {
Expand All @@ -3516,7 +3516,7 @@ bool tox_group_peer_get_public_key(const Tox *tox, uint32_t group_number, uint32
return false;
}

const int ret = gc_get_peer_public_key_by_peer_id(chat, (force GC_Peer_Id)peer_id, public_key);
const int ret = gc_get_peer_public_key_by_peer_id(chat, gc_peer_id_from_int(peer_id), public_key);
tox_unlock(tox);

if (ret == -1) {
Expand All @@ -3542,7 +3542,7 @@ Tox_Connection tox_group_peer_get_connection_status(const Tox *tox, uint32_t gro
return TOX_CONNECTION_NONE;
}

const unsigned int ret = gc_get_peer_connection_status(chat, (force GC_Peer_Id)peer_id);
const unsigned int ret = gc_get_peer_connection_status(chat, gc_peer_id_from_int(peer_id));
tox_unlock(tox);

if (ret == 0) {
Expand Down Expand Up @@ -3936,7 +3936,7 @@ bool tox_group_send_private_message(const Tox *tox, uint32_t group_number, uint3
return false;
}

const int ret = gc_send_private_message(chat, (force GC_Peer_Id)peer_id, type, message, length);
const int ret = gc_send_private_message(chat, gc_peer_id_from_int(peer_id), type, message, length);
tox_unlock(tox);

switch (ret) {
Expand Down Expand Up @@ -4059,7 +4059,7 @@ bool tox_group_send_custom_private_packet(const Tox *tox, uint32_t group_number,
return false;
}

const int ret = gc_send_custom_private_packet(chat, lossless, (force GC_Peer_Id)peer_id, data, length);
const int ret = gc_send_custom_private_packet(chat, lossless, gc_peer_id_from_int(peer_id), data, length);
tox_unlock(tox);

switch (ret) {
Expand Down Expand Up @@ -4483,7 +4483,7 @@ bool tox_group_set_ignore(Tox *tox, uint32_t group_number, uint32_t peer_id, boo
return false;
}

const int ret = gc_set_ignore(chat, (force GC_Peer_Id)peer_id, ignore);
const int ret = gc_set_ignore(chat, gc_peer_id_from_int(peer_id), ignore);
tox_unlock(tox);

switch (ret) {
Expand Down Expand Up @@ -4515,7 +4515,7 @@ bool tox_group_mod_set_role(Tox *tox, uint32_t group_number, uint32_t peer_id, T
assert(tox != nullptr);

tox_lock(tox);
const int ret = gc_set_peer_role(tox->m, group_number, (force GC_Peer_Id)peer_id, (Group_Role) role);
const int ret = gc_set_peer_role(tox->m, group_number, gc_peer_id_from_int(peer_id), (Group_Role) role);
tox_unlock(tox);

switch (ret) {
Expand Down Expand Up @@ -4567,7 +4567,7 @@ bool tox_group_mod_kick_peer(const Tox *tox, uint32_t group_number, uint32_t pee
assert(tox != nullptr);

tox_lock(tox);
const int ret = gc_kick_peer(tox->m, group_number, (force GC_Peer_Id)peer_id);
const int ret = gc_kick_peer(tox->m, group_number, gc_peer_id_from_int(peer_id));
tox_unlock(tox);

switch (ret) {
Expand Down
4 changes: 2 additions & 2 deletions toxcore/tox_private.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ size_t tox_group_peer_get_ip_address_size(const Tox *tox, uint32_t group_number,
return -1;
}

const int ret = gc_get_peer_ip_address_size(chat, (force GC_Peer_Id)peer_id);
const int ret = gc_get_peer_ip_address_size(chat, gc_peer_id_from_int(peer_id));
tox_unlock(tox);

if (ret == -1) {
Expand All @@ -215,7 +215,7 @@ bool tox_group_peer_get_ip_address(const Tox *tox, uint32_t group_number, uint32
return false;
}

const int ret = gc_get_peer_ip_address(chat, (force GC_Peer_Id)peer_id, ip_addr);
const int ret = gc_get_peer_ip_address(chat, gc_peer_id_from_int(peer_id), ip_addr);
tox_unlock(tox);

if (ret == -1) {
Expand Down

0 comments on commit b95b67c

Please sign in to comment.