diff options
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/group.c | 105 | ||||
-rw-r--r-- | toxcore/group.h | 2 |
2 files changed, 45 insertions, 62 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 | */ |
39 | static uint8_t groupnumber_not_valid(const Group_Chats *g_c, uint32_t groupnumber) | 39 | static 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 | */ |
62 | static int realloc_groupchats(Group_Chats *g_c, uint32_t num) | 62 | static 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 | ||
80 | static 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 | */ |
86 | static int create_group_chat(Group_Chats *g_c) | 90 | static 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 | */ |
113 | static int wipe_group_chat(Group_Chats *g_c, uint32_t groupnumber) | 115 | static 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 | ||
136 | static Group_c *get_group_c(const Group_Chats *g_c, uint32_t groupnumber) | 138 | static 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 | ||
154 | static int peer_in_chat(const Group_c *chat, const uint8_t *real_pk) | 156 | static 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 | */ |
175 | static int get_group_num(const Group_Chats *g_c, const uint8_t *identifier) | 175 | static 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 | */ |
196 | static int get_peer_index(Group_c *g, uint16_t peer_number) | 194 | static 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 | ||
473 | static int remove_close_conn(Group_Chats *g_c, uint32_t groupnumber, int friendcon_id) | 467 | static 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 */ |
658 | static void set_conns_status_groups(Group_Chats *g_c, int friendcon_id, uint8_t type) | 652 | static 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 | */ |
737 | int add_groupchat(Group_Chats *g_c, uint8_t type) | 729 | int 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 | */ |
2445 | void send_name_all_groups(Group_Chats *g_c) | 2435 | void 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. */ |
2484 | void do_groupchats(Group_Chats *g_c, void *userdata) | 2472 | void 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. */ |
2506 | void kill_groupchats(Group_Chats *g_c) | 2492 | void 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) | |||
2523 | uint32_t count_chatlist(Group_Chats *g_c) | 2507 | uint32_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 | } |
diff --git a/toxcore/group.h b/toxcore/group.h index 947c7035..b2a297f9 100644 --- a/toxcore/group.h +++ b/toxcore/group.h | |||
@@ -138,7 +138,7 @@ typedef struct Group_Chats { | |||
138 | Friend_Connections *fr_c; | 138 | Friend_Connections *fr_c; |
139 | 139 | ||
140 | Group_c *chats; | 140 | Group_c *chats; |
141 | uint32_t num_chats; | 141 | uint16_t num_chats; |
142 | 142 | ||
143 | g_conference_invite_cb *invite_callback; | 143 | g_conference_invite_cb *invite_callback; |
144 | g_conference_message_cb *message_callback; | 144 | g_conference_message_cb *message_callback; |