summaryrefslogtreecommitdiff
path: root/toxcore/group.c
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2018-07-12 23:58:24 +0000
committeriphydf <iphydf@users.noreply.github.com>2018-07-12 23:58:24 +0000
commit3d5fd9c2d0114e7d4bbb389e11216c425adaa216 (patch)
tree8cf9551a6f68361d01a9b813f11558c83ecf1090 /toxcore/group.c
parent2377cac94ba801f4b6363f23490dfdbcc332a8ad (diff)
Limit number of group chats to 65536.
By changing numchats from uint32_t to uint16_t. This is done in PGC. This PR is making that change in master to reduce the diff in the PGC branch. Also: * Inverted groupnumber_not_valid and renamed to is_groupnumber_valid. * Renamed realloc_groupchats to realloc_conferences and made it return bool. * Added setup_conference function that currently just zeroes the conference structure but later will initialise more values. * Made some `i` iterator variables local to the for-loop using for-init-decl. This is also done in PGC.
Diffstat (limited to 'toxcore/group.c')
-rw-r--r--toxcore/group.c105
1 files changed, 44 insertions, 61 deletions
diff --git a/toxcore/group.c b/toxcore/group.c
index a2ebca16..3e0c1fd3 100644
--- a/toxcore/group.c
+++ b/toxcore/group.c
@@ -33,72 +33,74 @@
33#include "mono_time.h" 33#include "mono_time.h"
34#include "util.h" 34#include "util.h"
35 35
36/* return 1 if the groupnumber is not valid. 36/* return false if the groupnumber is not valid.
37 * return 0 if the groupnumber is valid. 37 * return true if the groupnumber is valid.
38 */ 38 */
39static uint8_t groupnumber_not_valid(const Group_Chats *g_c, uint32_t groupnumber) 39static bool is_groupnumber_valid(const Group_Chats *g_c, uint32_t groupnumber)
40{ 40{
41 if (groupnumber >= g_c->num_chats) { 41 if (groupnumber >= g_c->num_chats) {
42 return 1; 42 return false;
43 } 43 }
44 44
45 if (g_c->chats == nullptr) { 45 if (g_c->chats == nullptr) {
46 return 1; 46 return false;
47 } 47 }
48 48
49 if (g_c->chats[groupnumber].status == GROUPCHAT_STATUS_NONE) { 49 if (g_c->chats[groupnumber].status == GROUPCHAT_STATUS_NONE) {
50 return 1; 50 return false;
51 } 51 }
52 52
53 return 0; 53 return true;
54} 54}
55 55
56 56
57/* Set the size of the groupchat list to num. 57/* Set the size of the groupchat list to num.
58 * 58 *
59 * return -1 if realloc fails. 59 * return false if realloc fails.
60 * return 0 if it succeeds. 60 * return true if it succeeds.
61 */ 61 */
62static int realloc_groupchats(Group_Chats *g_c, uint32_t num) 62static bool realloc_conferences(Group_Chats *g_c, uint16_t num)
63{ 63{
64 if (num == 0) { 64 if (num == 0) {
65 free(g_c->chats); 65 free(g_c->chats);
66 g_c->chats = nullptr; 66 g_c->chats = nullptr;
67 return 0; 67 return true;
68 } 68 }
69 69
70 Group_c *newgroup_chats = (Group_c *)realloc(g_c->chats, num * sizeof(Group_c)); 70 Group_c *newgroup_chats = (Group_c *)realloc(g_c->chats, num * sizeof(Group_c));
71 71
72 if (newgroup_chats == nullptr) { 72 if (newgroup_chats == nullptr) {
73 return -1; 73 return false;
74 } 74 }
75 75
76 g_c->chats = newgroup_chats; 76 g_c->chats = newgroup_chats;
77 return 0; 77 return true;
78} 78}
79 79
80static void setup_conference(Group_c *g)
81{
82 memset(g, 0, sizeof(Group_c));
83}
80 84
81/* Create a new empty groupchat connection. 85/* Create a new empty groupchat connection.
82 * 86 *
83 * return -1 on failure. 87 * return -1 on failure.
84 * return groupnumber on success. 88 * return groupnumber on success.
85 */ 89 */
86static int create_group_chat(Group_Chats *g_c) 90static int32_t create_group_chat(Group_Chats *g_c)
87{ 91{
88 uint32_t i; 92 for (uint16_t i = 0; i < g_c->num_chats; ++i) {
89
90 for (i = 0; i < g_c->num_chats; ++i) {
91 if (g_c->chats[i].status == GROUPCHAT_STATUS_NONE) { 93 if (g_c->chats[i].status == GROUPCHAT_STATUS_NONE) {
92 return i; 94 return i;
93 } 95 }
94 } 96 }
95 97
96 int id = -1; 98 int32_t id = -1;
97 99
98 if (realloc_groupchats(g_c, g_c->num_chats + 1) == 0) { 100 if (realloc_conferences(g_c, g_c->num_chats + 1)) {
99 id = g_c->num_chats; 101 id = g_c->num_chats;
100 ++g_c->num_chats; 102 ++g_c->num_chats;
101 memset(&g_c->chats[id], 0, sizeof(Group_c)); 103 setup_conference(&g_c->chats[id]);
102 } 104 }
103 105
104 return id; 106 return id;
@@ -112,11 +114,11 @@ static int create_group_chat(Group_Chats *g_c)
112 */ 114 */
113static int wipe_group_chat(Group_Chats *g_c, uint32_t groupnumber) 115static int wipe_group_chat(Group_Chats *g_c, uint32_t groupnumber)
114{ 116{
115 if (groupnumber_not_valid(g_c, groupnumber)) { 117 if (!is_groupnumber_valid(g_c, groupnumber)) {
116 return -1; 118 return -1;
117 } 119 }
118 120
119 uint32_t i; 121 uint16_t i;
120 crypto_memzero(&g_c->chats[groupnumber], sizeof(Group_c)); 122 crypto_memzero(&g_c->chats[groupnumber], sizeof(Group_c));
121 123
122 for (i = g_c->num_chats; i != 0; --i) { 124 for (i = g_c->num_chats; i != 0; --i) {
@@ -127,7 +129,7 @@ static int wipe_group_chat(Group_Chats *g_c, uint32_t groupnumber)
127 129
128 if (g_c->num_chats != i) { 130 if (g_c->num_chats != i) {
129 g_c->num_chats = i; 131 g_c->num_chats = i;
130 realloc_groupchats(g_c, g_c->num_chats); 132 realloc_conferences(g_c, g_c->num_chats);
131 } 133 }
132 134
133 return 0; 135 return 0;
@@ -135,7 +137,7 @@ static int wipe_group_chat(Group_Chats *g_c, uint32_t groupnumber)
135 137
136static Group_c *get_group_c(const Group_Chats *g_c, uint32_t groupnumber) 138static Group_c *get_group_c(const Group_Chats *g_c, uint32_t groupnumber)
137{ 139{
138 if (groupnumber_not_valid(g_c, groupnumber)) { 140 if (!is_groupnumber_valid(g_c, groupnumber)) {
139 return nullptr; 141 return nullptr;
140 } 142 }
141 143
@@ -153,9 +155,7 @@ static Group_c *get_group_c(const Group_Chats *g_c, uint32_t groupnumber)
153 155
154static int peer_in_chat(const Group_c *chat, const uint8_t *real_pk) 156static int peer_in_chat(const Group_c *chat, const uint8_t *real_pk)
155{ 157{
156 uint32_t i; 158 for (uint32_t i = 0; i < chat->numpeers; ++i) {
157
158 for (i = 0; i < chat->numpeers; ++i) {
159 if (id_equal(chat->group[i].real_pk, real_pk)) { 159 if (id_equal(chat->group[i].real_pk, real_pk)) {
160 return i; 160 return i;
161 } 161 }
@@ -172,11 +172,9 @@ static int peer_in_chat(const Group_c *chat, const uint8_t *real_pk)
172 * 172 *
173 * TODO(irungentoo): make this more efficient and maybe use constant time comparisons? 173 * TODO(irungentoo): make this more efficient and maybe use constant time comparisons?
174 */ 174 */
175static int get_group_num(const Group_Chats *g_c, const uint8_t *identifier) 175static int32_t get_group_num(const Group_Chats *g_c, const uint8_t *identifier)
176{ 176{
177 uint32_t i; 177 for (uint16_t i = 0; i < g_c->num_chats; ++i) {
178
179 for (i = 0; i < g_c->num_chats; ++i) {
180 if (crypto_memcmp(g_c->chats[i].identifier, identifier, GROUP_IDENTIFIER_LENGTH) == 0) { 178 if (crypto_memcmp(g_c->chats[i].identifier, identifier, GROUP_IDENTIFIER_LENGTH) == 0) {
181 return i; 179 return i;
182 } 180 }
@@ -195,9 +193,7 @@ static int get_group_num(const Group_Chats *g_c, const uint8_t *identifier)
195 */ 193 */
196static int get_peer_index(Group_c *g, uint16_t peer_number) 194static int get_peer_index(Group_c *g, uint16_t peer_number)
197{ 195{
198 uint32_t i; 196 for (uint32_t i = 0; i < g->numpeers; ++i) {
199
200 for (i = 0; i < g->numpeers; ++i) {
201 if (g->group[i].peer_number == peer_number) { 197 if (g->group[i].peer_number == peer_number) {
202 return i; 198 return i;
203 } 199 }
@@ -345,15 +341,13 @@ static int connect_to_closest(Group_Chats *g_c, uint32_t groupnumber, void *user
345 return 0; 341 return 0;
346 } 342 }
347 343
348 unsigned int i;
349
350 if (g->changed == GROUPCHAT_CLOSEST_REMOVED) { 344 if (g->changed == GROUPCHAT_CLOSEST_REMOVED) {
351 for (i = 0; i < g->numpeers; ++i) { 345 for (uint32_t i = 0; i < g->numpeers; ++i) {
352 add_to_closest(g_c, groupnumber, g->group[i].real_pk, g->group[i].temp_pk); 346 add_to_closest(g_c, groupnumber, g->group[i].real_pk, g->group[i].temp_pk);
353 } 347 }
354 } 348 }
355 349
356 for (i = 0; i < MAX_GROUP_CONNECTIONS; ++i) { 350 for (uint32_t i = 0; i < MAX_GROUP_CONNECTIONS; ++i) {
357 if (g->close[i].type == GROUPCHAT_CLOSE_NONE) { 351 if (g->close[i].type == GROUPCHAT_CLOSE_NONE) {
358 continue; 352 continue;
359 } 353 }
@@ -372,7 +366,7 @@ static int connect_to_closest(Group_Chats *g_c, uint32_t groupnumber, void *user
372 } 366 }
373 } 367 }
374 368
375 for (i = 0; i < DESIRED_CLOSE_CONNECTIONS; ++i) { 369 for (uint32_t i = 0; i < DESIRED_CLOSE_CONNECTIONS; ++i) {
376 if (!g->closest_peers[i].entry) { 370 if (!g->closest_peers[i].entry) {
377 continue; 371 continue;
378 } 372 }
@@ -467,7 +461,7 @@ static int addpeer(Group_Chats *g_c, uint32_t groupnumber, const uint8_t *real_p
467 g->peer_on_join(g->object, groupnumber, g->numpeers - 1); 461 g->peer_on_join(g->object, groupnumber, g->numpeers - 1);
468 } 462 }
469 463
470 return (g->numpeers - 1); 464 return g->numpeers - 1;
471} 465}
472 466
473static int remove_close_conn(Group_Chats *g_c, uint32_t groupnumber, int friendcon_id) 467static int remove_close_conn(Group_Chats *g_c, uint32_t groupnumber, int friendcon_id)
@@ -657,9 +651,7 @@ static void set_conns_type_close(Group_Chats *g_c, uint32_t groupnumber, int fri
657/* Set the type for all close connections with friendcon_id */ 651/* Set the type for all close connections with friendcon_id */
658static void set_conns_status_groups(Group_Chats *g_c, int friendcon_id, uint8_t type) 652static void set_conns_status_groups(Group_Chats *g_c, int friendcon_id, uint8_t type)
659{ 653{
660 uint32_t i; 654 for (uint16_t i = 0; i < g_c->num_chats; ++i) {
661
662 for (i = 0; i < g_c->num_chats; ++i) {
663 set_conns_type_close(g_c, i, friendcon_id, type); 655 set_conns_type_close(g_c, i, friendcon_id, type);
664 } 656 }
665} 657}
@@ -736,7 +728,7 @@ static int add_conn_to_groupchat(Group_Chats *g_c, int friendcon_id, uint32_t gr
736 */ 728 */
737int add_groupchat(Group_Chats *g_c, uint8_t type) 729int add_groupchat(Group_Chats *g_c, uint8_t type)
738{ 730{
739 int groupnumber = create_group_chat(g_c); 731 int32_t groupnumber = create_group_chat(g_c);
740 732
741 if (groupnumber == -1) { 733 if (groupnumber == -1) {
742 return -1; 734 return -1;
@@ -1629,7 +1621,7 @@ static unsigned int send_peers(Group_Chats *g_c, uint32_t groupnumber, int frien
1629 uint8_t *p = response_packet + 1; 1621 uint8_t *p = response_packet + 1;
1630 1622
1631 uint16_t sent = 0; 1623 uint16_t sent = 0;
1632 unsigned int i; 1624 uint32_t i;
1633 1625
1634 for (i = 0; i < g->numpeers; ++i) { 1626 for (i = 0; i < g->numpeers; ++i) {
1635 if ((p - response_packet) + sizeof(uint16_t) + CRYPTO_PUBLIC_KEY_SIZE * 2 + 1 + g->group[i].nick_len > sizeof( 1627 if ((p - response_packet) + sizeof(uint16_t) + CRYPTO_PUBLIC_KEY_SIZE * 2 + 1 + g->group[i].nick_len > sizeof(
@@ -2425,9 +2417,7 @@ static int groupchat_clear_timedout(Group_Chats *g_c, uint32_t groupnumber, void
2425 return -1; 2417 return -1;
2426 } 2418 }
2427 2419
2428 uint32_t i; 2420 for (uint32_t i = 0; i < g->numpeers; ++i) {
2429
2430 for (i = 0; i < g->numpeers; ++i) {
2431 if (g->peer_number != g->group[i].peer_number && is_timeout(g->group[i].last_recv, GROUP_PING_INTERVAL * 3)) { 2421 if (g->peer_number != g->group[i].peer_number && is_timeout(g->group[i].last_recv, GROUP_PING_INTERVAL * 3)) {
2432 delpeer(g_c, groupnumber, i, userdata); 2422 delpeer(g_c, groupnumber, i, userdata);
2433 } 2423 }
@@ -2444,9 +2434,7 @@ static int groupchat_clear_timedout(Group_Chats *g_c, uint32_t groupnumber, void
2444 */ 2434 */
2445void send_name_all_groups(Group_Chats *g_c) 2435void send_name_all_groups(Group_Chats *g_c)
2446{ 2436{
2447 unsigned int i; 2437 for (uint16_t i = 0; i < g_c->num_chats; ++i) {
2448
2449 for (i = 0; i < g_c->num_chats; ++i) {
2450 Group_c *g = get_group_c(g_c, i); 2438 Group_c *g = get_group_c(g_c, i);
2451 2439
2452 if (!g) { 2440 if (!g) {
@@ -2483,9 +2471,7 @@ Group_Chats *new_groupchats(Messenger *m)
2483/* main groupchats loop. */ 2471/* main groupchats loop. */
2484void do_groupchats(Group_Chats *g_c, void *userdata) 2472void do_groupchats(Group_Chats *g_c, void *userdata)
2485{ 2473{
2486 unsigned int i; 2474 for (uint16_t i = 0; i < g_c->num_chats; ++i) {
2487
2488 for (i = 0; i < g_c->num_chats; ++i) {
2489 Group_c *g = get_group_c(g_c, i); 2475 Group_c *g = get_group_c(g_c, i);
2490 2476
2491 if (!g) { 2477 if (!g) {
@@ -2505,9 +2491,7 @@ void do_groupchats(Group_Chats *g_c, void *userdata)
2505/* Free everything related with group chats. */ 2491/* Free everything related with group chats. */
2506void kill_groupchats(Group_Chats *g_c) 2492void kill_groupchats(Group_Chats *g_c)
2507{ 2493{
2508 unsigned int i; 2494 for (uint16_t i = 0; i < g_c->num_chats; ++i) {
2509
2510 for (i = 0; i < g_c->num_chats; ++i) {
2511 del_groupchat(g_c, i); 2495 del_groupchat(g_c, i);
2512 } 2496 }
2513 2497
@@ -2523,9 +2507,8 @@ void kill_groupchats(Group_Chats *g_c)
2523uint32_t count_chatlist(Group_Chats *g_c) 2507uint32_t count_chatlist(Group_Chats *g_c)
2524{ 2508{
2525 uint32_t ret = 0; 2509 uint32_t ret = 0;
2526 uint32_t i;
2527 2510
2528 for (i = 0; i < g_c->num_chats; ++i) { 2511 for (uint16_t i = 0; i < g_c->num_chats; ++i) {
2529 if (g_c->chats[i].status != GROUPCHAT_STATUS_NONE) { 2512 if (g_c->chats[i].status != GROUPCHAT_STATUS_NONE) {
2530 ++ret; 2513 ++ret;
2531 } 2514 }
@@ -2549,9 +2532,9 @@ uint32_t copy_chatlist(Group_Chats *g_c, uint32_t *out_list, uint32_t list_size)
2549 return 0; 2532 return 0;
2550 } 2533 }
2551 2534
2552 uint32_t i, ret = 0; 2535 uint32_t ret = 0;
2553 2536
2554 for (i = 0; i < g_c->num_chats; ++i) { 2537 for (uint16_t i = 0; i < g_c->num_chats; ++i) {
2555 if (ret >= list_size) { 2538 if (ret >= list_size) {
2556 break; /* Abandon ship */ 2539 break; /* Abandon ship */
2557 } 2540 }