Skip to content

Commit

Permalink
Merge 3d5fd9c into 2377cac
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Jul 13, 2018
2 parents 2377cac + 3d5fd9c commit b310981
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 62 deletions.
105 changes: 44 additions & 61 deletions toxcore/group.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,72 +33,74 @@
#include "mono_time.h"
#include "util.h"

/* return 1 if the groupnumber is not valid.
* return 0 if the groupnumber is valid.
/* return false if the groupnumber is not valid.
* return true if the groupnumber is valid.
*/
static uint8_t groupnumber_not_valid(const Group_Chats *g_c, uint32_t groupnumber)
static bool is_groupnumber_valid(const Group_Chats *g_c, uint32_t groupnumber)
{
if (groupnumber >= g_c->num_chats) {
return 1;
return false;
}

if (g_c->chats == nullptr) {
return 1;
return false;
}

if (g_c->chats[groupnumber].status == GROUPCHAT_STATUS_NONE) {
return 1;
return false;
}

return 0;
return true;
}


/* Set the size of the groupchat list to num.
*
* return -1 if realloc fails.
* return 0 if it succeeds.
* return false if realloc fails.
* return true if it succeeds.
*/
static int realloc_groupchats(Group_Chats *g_c, uint32_t num)
static bool realloc_conferences(Group_Chats *g_c, uint16_t num)
{
if (num == 0) {
free(g_c->chats);
g_c->chats = nullptr;
return 0;
return true;
}

Group_c *newgroup_chats = (Group_c *)realloc(g_c->chats, num * sizeof(Group_c));

if (newgroup_chats == nullptr) {
return -1;
return false;
}

g_c->chats = newgroup_chats;
return 0;
return true;
}

static void setup_conference(Group_c *g)
{
memset(g, 0, sizeof(Group_c));
}

/* Create a new empty groupchat connection.
*
* return -1 on failure.
* return groupnumber on success.
*/
static int create_group_chat(Group_Chats *g_c)
static int32_t create_group_chat(Group_Chats *g_c)
{
uint32_t i;

for (i = 0; i < g_c->num_chats; ++i) {
for (uint16_t i = 0; i < g_c->num_chats; ++i) {
if (g_c->chats[i].status == GROUPCHAT_STATUS_NONE) {
return i;
}
}

int id = -1;
int32_t id = -1;

if (realloc_groupchats(g_c, g_c->num_chats + 1) == 0) {
if (realloc_conferences(g_c, g_c->num_chats + 1)) {
id = g_c->num_chats;
++g_c->num_chats;
memset(&g_c->chats[id], 0, sizeof(Group_c));
setup_conference(&g_c->chats[id]);
}

return id;
Expand All @@ -112,11 +114,11 @@ static int create_group_chat(Group_Chats *g_c)
*/
static int wipe_group_chat(Group_Chats *g_c, uint32_t groupnumber)
{
if (groupnumber_not_valid(g_c, groupnumber)) {
if (!is_groupnumber_valid(g_c, groupnumber)) {
return -1;
}

uint32_t i;
uint16_t i;
crypto_memzero(&g_c->chats[groupnumber], sizeof(Group_c));

for (i = g_c->num_chats; i != 0; --i) {
Expand All @@ -127,15 +129,15 @@ static int wipe_group_chat(Group_Chats *g_c, uint32_t groupnumber)

if (g_c->num_chats != i) {
g_c->num_chats = i;
realloc_groupchats(g_c, g_c->num_chats);
realloc_conferences(g_c, g_c->num_chats);
}

return 0;
}

static Group_c *get_group_c(const Group_Chats *g_c, uint32_t groupnumber)
{
if (groupnumber_not_valid(g_c, groupnumber)) {
if (!is_groupnumber_valid(g_c, groupnumber)) {
return nullptr;
}

Expand All @@ -153,9 +155,7 @@ static Group_c *get_group_c(const Group_Chats *g_c, uint32_t groupnumber)

static int peer_in_chat(const Group_c *chat, const uint8_t *real_pk)
{
uint32_t i;

for (i = 0; i < chat->numpeers; ++i) {
for (uint32_t i = 0; i < chat->numpeers; ++i) {
if (id_equal(chat->group[i].real_pk, real_pk)) {
return i;
}
Expand All @@ -172,11 +172,9 @@ static int peer_in_chat(const Group_c *chat, const uint8_t *real_pk)
*
* TODO(irungentoo): make this more efficient and maybe use constant time comparisons?
*/
static int get_group_num(const Group_Chats *g_c, const uint8_t *identifier)
static int32_t get_group_num(const Group_Chats *g_c, const uint8_t *identifier)
{
uint32_t i;

for (i = 0; i < g_c->num_chats; ++i) {
for (uint16_t i = 0; i < g_c->num_chats; ++i) {
if (crypto_memcmp(g_c->chats[i].identifier, identifier, GROUP_IDENTIFIER_LENGTH) == 0) {
return i;
}
Expand All @@ -195,9 +193,7 @@ static int get_group_num(const Group_Chats *g_c, const uint8_t *identifier)
*/
static int get_peer_index(Group_c *g, uint16_t peer_number)
{
uint32_t i;

for (i = 0; i < g->numpeers; ++i) {
for (uint32_t i = 0; i < g->numpeers; ++i) {
if (g->group[i].peer_number == peer_number) {
return i;
}
Expand Down Expand Up @@ -345,15 +341,13 @@ static int connect_to_closest(Group_Chats *g_c, uint32_t groupnumber, void *user
return 0;
}

unsigned int i;

if (g->changed == GROUPCHAT_CLOSEST_REMOVED) {
for (i = 0; i < g->numpeers; ++i) {
for (uint32_t i = 0; i < g->numpeers; ++i) {
add_to_closest(g_c, groupnumber, g->group[i].real_pk, g->group[i].temp_pk);
}
}

for (i = 0; i < MAX_GROUP_CONNECTIONS; ++i) {
for (uint32_t i = 0; i < MAX_GROUP_CONNECTIONS; ++i) {
if (g->close[i].type == GROUPCHAT_CLOSE_NONE) {
continue;
}
Expand All @@ -372,7 +366,7 @@ static int connect_to_closest(Group_Chats *g_c, uint32_t groupnumber, void *user
}
}

for (i = 0; i < DESIRED_CLOSE_CONNECTIONS; ++i) {
for (uint32_t i = 0; i < DESIRED_CLOSE_CONNECTIONS; ++i) {
if (!g->closest_peers[i].entry) {
continue;
}
Expand Down Expand Up @@ -467,7 +461,7 @@ static int addpeer(Group_Chats *g_c, uint32_t groupnumber, const uint8_t *real_p
g->peer_on_join(g->object, groupnumber, g->numpeers - 1);
}

return (g->numpeers - 1);
return g->numpeers - 1;
}

static int remove_close_conn(Group_Chats *g_c, uint32_t groupnumber, int friendcon_id)
Expand Down Expand Up @@ -657,9 +651,7 @@ static void set_conns_type_close(Group_Chats *g_c, uint32_t groupnumber, int fri
/* Set the type for all close connections with friendcon_id */
static void set_conns_status_groups(Group_Chats *g_c, int friendcon_id, uint8_t type)
{
uint32_t i;

for (i = 0; i < g_c->num_chats; ++i) {
for (uint16_t i = 0; i < g_c->num_chats; ++i) {
set_conns_type_close(g_c, i, friendcon_id, type);
}
}
Expand Down Expand Up @@ -736,7 +728,7 @@ static int add_conn_to_groupchat(Group_Chats *g_c, int friendcon_id, uint32_t gr
*/
int add_groupchat(Group_Chats *g_c, uint8_t type)
{
int groupnumber = create_group_chat(g_c);
int32_t groupnumber = create_group_chat(g_c);

if (groupnumber == -1) {
return -1;
Expand Down Expand Up @@ -1629,7 +1621,7 @@ static unsigned int send_peers(Group_Chats *g_c, uint32_t groupnumber, int frien
uint8_t *p = response_packet + 1;

uint16_t sent = 0;
unsigned int i;
uint32_t i;

for (i = 0; i < g->numpeers; ++i) {
if ((p - response_packet) + sizeof(uint16_t) + CRYPTO_PUBLIC_KEY_SIZE * 2 + 1 + g->group[i].nick_len > sizeof(
Expand Down Expand Up @@ -2425,9 +2417,7 @@ static int groupchat_clear_timedout(Group_Chats *g_c, uint32_t groupnumber, void
return -1;
}

uint32_t i;

for (i = 0; i < g->numpeers; ++i) {
for (uint32_t i = 0; i < g->numpeers; ++i) {
if (g->peer_number != g->group[i].peer_number && is_timeout(g->group[i].last_recv, GROUP_PING_INTERVAL * 3)) {
delpeer(g_c, groupnumber, i, userdata);
}
Expand All @@ -2444,9 +2434,7 @@ static int groupchat_clear_timedout(Group_Chats *g_c, uint32_t groupnumber, void
*/
void send_name_all_groups(Group_Chats *g_c)
{
unsigned int i;

for (i = 0; i < g_c->num_chats; ++i) {
for (uint16_t i = 0; i < g_c->num_chats; ++i) {
Group_c *g = get_group_c(g_c, i);

if (!g) {
Expand Down Expand Up @@ -2483,9 +2471,7 @@ Group_Chats *new_groupchats(Messenger *m)
/* main groupchats loop. */
void do_groupchats(Group_Chats *g_c, void *userdata)
{
unsigned int i;

for (i = 0; i < g_c->num_chats; ++i) {
for (uint16_t i = 0; i < g_c->num_chats; ++i) {
Group_c *g = get_group_c(g_c, i);

if (!g) {
Expand All @@ -2505,9 +2491,7 @@ void do_groupchats(Group_Chats *g_c, void *userdata)
/* Free everything related with group chats. */
void kill_groupchats(Group_Chats *g_c)
{
unsigned int i;

for (i = 0; i < g_c->num_chats; ++i) {
for (uint16_t i = 0; i < g_c->num_chats; ++i) {
del_groupchat(g_c, i);
}

Expand All @@ -2523,9 +2507,8 @@ void kill_groupchats(Group_Chats *g_c)
uint32_t count_chatlist(Group_Chats *g_c)
{
uint32_t ret = 0;
uint32_t i;

for (i = 0; i < g_c->num_chats; ++i) {
for (uint16_t i = 0; i < g_c->num_chats; ++i) {
if (g_c->chats[i].status != GROUPCHAT_STATUS_NONE) {
++ret;
}
Expand All @@ -2549,9 +2532,9 @@ uint32_t copy_chatlist(Group_Chats *g_c, uint32_t *out_list, uint32_t list_size)
return 0;
}

uint32_t i, ret = 0;
uint32_t ret = 0;

for (i = 0; i < g_c->num_chats; ++i) {
for (uint16_t i = 0; i < g_c->num_chats; ++i) {
if (ret >= list_size) {
break; /* Abandon ship */
}
Expand Down
2 changes: 1 addition & 1 deletion toxcore/group.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ typedef struct Group_Chats {
Friend_Connections *fr_c;

Group_c *chats;
uint32_t num_chats;
uint16_t num_chats;

g_conference_invite_cb *invite_callback;
g_conference_message_cb *message_callback;
Expand Down

0 comments on commit b310981

Please sign in to comment.