forked from irungentoo/toxcore
-
Notifications
You must be signed in to change notification settings - Fork 291
/
Copy pathgroup.c
2953 lines (2332 loc) · 80.1 KB
/
group.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Slightly better groupchats implementation.
*/
/*
* Copyright © 2016-2017 The TokTok team.
* Copyright © 2014 Tox project.
*
* This file is part of Tox, the free peer to peer instant messenger.
*
* Tox is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Tox is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "group.h"
#include <stdlib.h>
#include <string.h>
#include "mono_time.h"
#include "util.h"
/**
* Packet type IDs as per the protocol specification.
*/
typedef enum Group_Message_Id {
GROUP_MESSAGE_PING_ID = 0,
GROUP_MESSAGE_NEW_PEER_ID = 16,
GROUP_MESSAGE_KILL_PEER_ID = 17,
GROUP_MESSAGE_NAME_ID = 48,
GROUP_MESSAGE_TITLE_ID = 49,
} Group_Message_Id;
#define GROUP_MESSAGE_NEW_PEER_LENGTH (sizeof(uint16_t) + CRYPTO_PUBLIC_KEY_SIZE * 2)
#define GROUP_MESSAGE_KILL_PEER_LENGTH (sizeof(uint16_t))
#define MAX_GROUP_MESSAGE_DATA_LEN (MAX_CRYPTO_DATA_SIZE - (1 + MIN_MESSAGE_PACKET_LEN))
typedef enum Invite_Id {
INVITE_ID = 0,
INVITE_RESPONSE_ID = 1,
} Invite_Id;
#define INVITE_PACKET_SIZE (1 + sizeof(uint16_t) + GROUP_IDENTIFIER_LENGTH)
#define INVITE_RESPONSE_PACKET_SIZE (1 + sizeof(uint16_t) * 2 + GROUP_IDENTIFIER_LENGTH)
#define ONLINE_PACKET_DATA_SIZE (sizeof(uint16_t) + GROUP_IDENTIFIER_LENGTH)
typedef enum Peer_Id {
PEER_KILL_ID = 1,
PEER_QUERY_ID = 8,
PEER_RESPONSE_ID = 9,
PEER_TITLE_ID = 10,
} Peer_Id;
#define MIN_MESSAGE_PACKET_LEN (sizeof(uint16_t) * 2 + sizeof(uint32_t) + 1)
/* return false if the groupnumber is not valid.
* return true if the groupnumber is valid.
*/
static bool is_groupnumber_valid(const Group_Chats *g_c, uint32_t groupnumber)
{
if (groupnumber >= g_c->num_chats) {
return false;
}
if (g_c->chats == nullptr) {
return false;
}
if (g_c->chats[groupnumber].status == GROUPCHAT_STATUS_NONE) {
return false;
}
return true;
}
/* Set the size of the groupchat list to num.
*
* return false if realloc fails.
* return true if it succeeds.
*/
static bool realloc_conferences(Group_Chats *g_c, uint16_t num)
{
if (num == 0) {
free(g_c->chats);
g_c->chats = nullptr;
return true;
}
Group_c *newgroup_chats = (Group_c *)realloc(g_c->chats, num * sizeof(Group_c));
if (newgroup_chats == nullptr) {
return false;
}
g_c->chats = newgroup_chats;
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 int32_t create_group_chat(Group_Chats *g_c)
{
for (uint16_t i = 0; i < g_c->num_chats; ++i) {
if (g_c->chats[i].status == GROUPCHAT_STATUS_NONE) {
return i;
}
}
int32_t id = -1;
if (realloc_conferences(g_c, g_c->num_chats + 1)) {
id = g_c->num_chats;
++g_c->num_chats;
setup_conference(&g_c->chats[id]);
}
return id;
}
/* Wipe a groupchat.
*
* return -1 on failure.
* return 0 on success.
*/
static int wipe_group_chat(Group_Chats *g_c, uint32_t groupnumber)
{
if (!is_groupnumber_valid(g_c, groupnumber)) {
return -1;
}
uint16_t i;
crypto_memzero(&g_c->chats[groupnumber], sizeof(Group_c));
for (i = g_c->num_chats; i != 0; --i) {
if (g_c->chats[i - 1].status != GROUPCHAT_STATUS_NONE) {
break;
}
}
if (g_c->num_chats != i) {
g_c->num_chats = i;
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 (!is_groupnumber_valid(g_c, groupnumber)) {
return nullptr;
}
return &g_c->chats[groupnumber];
}
/*
* check if peer with real_pk is in peer array.
*
* return peer index if peer is in chat.
* return -1 if peer is not in chat.
*
* TODO(irungentoo): make this more efficient.
*/
static int peer_in_chat(const Group_c *chat, const uint8_t *real_pk)
{
for (uint32_t i = 0; i < chat->numpeers; ++i) {
if (id_equal(chat->group[i].real_pk, real_pk)) {
return i;
}
}
return -1;
}
/*
* check if group with identifier is in group array.
*
* return group number if peer is in list.
* return -1 if group is not in list.
*
* TODO(irungentoo): make this more efficient and maybe use constant time comparisons?
*/
static int32_t get_group_num(const Group_Chats *g_c, const uint8_t *identifier)
{
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;
}
}
return -1;
}
int32_t conference_by_uid(const Group_Chats *g_c, const uint8_t *uid)
{
for (uint16_t i = 0; i < g_c->num_chats; ++i) {
if (crypto_memcmp(g_c->chats[i].identifier + 1, uid, GROUP_IDENTIFIER_LENGTH - 1) == 0) {
return i;
}
}
return -1;
}
/*
* check if peer with peer_number is in peer array.
*
* return peer index if peer is in chat.
* return -1 if peer is not in chat.
*
* TODO(irungentoo): make this more efficient.
*/
static int get_peer_index(Group_c *g, uint16_t peer_number)
{
for (uint32_t i = 0; i < g->numpeers; ++i) {
if (g->group[i].peer_number == peer_number) {
return i;
}
}
return -1;
}
static uint64_t calculate_comp_value(const uint8_t *pk1, const uint8_t *pk2)
{
uint64_t cmp1 = 0, cmp2 = 0;
for (size_t i = 0; i < sizeof(uint64_t); ++i) {
cmp1 = (cmp1 << 8) + (uint64_t)pk1[i];
cmp2 = (cmp2 << 8) + (uint64_t)pk2[i];
}
return cmp1 - cmp2;
}
typedef enum Groupchat_Closest {
GROUPCHAT_CLOSEST_NONE,
GROUPCHAT_CLOSEST_ADDED,
GROUPCHAT_CLOSEST_REMOVED
} Groupchat_Closest;
static int friend_in_close(Group_c *g, int friendcon_id);
static int add_conn_to_groupchat(Group_Chats *g_c, int friendcon_id, uint32_t groupnumber, uint8_t closest,
uint8_t lock);
static int add_to_closest(Group_Chats *g_c, uint32_t groupnumber, const uint8_t *real_pk, const uint8_t *temp_pk)
{
Group_c *g = get_group_c(g_c, groupnumber);
if (!g) {
return -1;
}
if (public_key_cmp(g->real_pk, real_pk) == 0) {
return -1;
}
unsigned int i;
unsigned int index = DESIRED_CLOSE_CONNECTIONS;
for (i = 0; i < DESIRED_CLOSE_CONNECTIONS; ++i) {
if (g->closest_peers[i].entry && public_key_cmp(real_pk, g->closest_peers[i].real_pk) == 0) {
return 0;
}
}
for (i = 0; i < DESIRED_CLOSE_CONNECTIONS; ++i) {
if (g->closest_peers[i].entry == 0) {
index = i;
break;
}
}
if (index == DESIRED_CLOSE_CONNECTIONS) {
uint64_t comp_val = calculate_comp_value(g->real_pk, real_pk);
uint64_t comp_d = 0;
for (i = 0; i < (DESIRED_CLOSE_CONNECTIONS / 2); ++i) {
uint64_t comp;
comp = calculate_comp_value(g->real_pk, g->closest_peers[i].real_pk);
if (comp > comp_val && comp > comp_d) {
index = i;
comp_d = comp;
}
}
comp_val = calculate_comp_value(real_pk, g->real_pk);
for (i = (DESIRED_CLOSE_CONNECTIONS / 2); i < DESIRED_CLOSE_CONNECTIONS; ++i) {
uint64_t comp = calculate_comp_value(g->closest_peers[i].real_pk, g->real_pk);
if (comp > comp_val && comp > comp_d) {
index = i;
comp_d = comp;
}
}
}
if (index == DESIRED_CLOSE_CONNECTIONS) {
return -1;
}
uint8_t old_real_pk[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t old_temp_pk[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t old = 0;
if (g->closest_peers[index].entry) {
memcpy(old_real_pk, g->closest_peers[index].real_pk, CRYPTO_PUBLIC_KEY_SIZE);
memcpy(old_temp_pk, g->closest_peers[index].temp_pk, CRYPTO_PUBLIC_KEY_SIZE);
old = 1;
}
g->closest_peers[index].entry = 1;
memcpy(g->closest_peers[index].real_pk, real_pk, CRYPTO_PUBLIC_KEY_SIZE);
memcpy(g->closest_peers[index].temp_pk, temp_pk, CRYPTO_PUBLIC_KEY_SIZE);
if (old) {
add_to_closest(g_c, groupnumber, old_real_pk, old_temp_pk);
}
if (!g->changed) {
g->changed = GROUPCHAT_CLOSEST_ADDED;
}
return 0;
}
static unsigned int pk_in_closest_peers(Group_c *g, uint8_t *real_pk)
{
unsigned int i;
for (i = 0; i < DESIRED_CLOSE_CONNECTIONS; ++i) {
if (!g->closest_peers[i].entry) {
continue;
}
if (public_key_cmp(g->closest_peers[i].real_pk, real_pk) == 0) {
return 1;
}
}
return 0;
}
static int send_packet_online(Friend_Connections *fr_c, int friendcon_id, uint16_t group_num, uint8_t *identifier);
static int connect_to_closest(Group_Chats *g_c, uint32_t groupnumber, void *userdata)
{
Group_c *g = get_group_c(g_c, groupnumber);
if (!g) {
return -1;
}
if (!g->changed) {
return 0;
}
if (g->changed == GROUPCHAT_CLOSEST_REMOVED) {
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 (uint32_t i = 0; i < MAX_GROUP_CONNECTIONS; ++i) {
if (g->close[i].type == GROUPCHAT_CLOSE_NONE) {
continue;
}
if (!g->close[i].closest) {
continue;
}
uint8_t real_pk[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t dht_temp_pk[CRYPTO_PUBLIC_KEY_SIZE];
get_friendcon_public_keys(real_pk, dht_temp_pk, g_c->fr_c, g->close[i].number);
if (!pk_in_closest_peers(g, real_pk)) {
g->close[i].closest = false;
if (!g->close[i].introducer && !g->close[i].introduced) {
g->close[i].type = GROUPCHAT_CLOSE_NONE;
kill_friend_connection(g_c->fr_c, g->close[i].number);
}
}
}
for (uint32_t i = 0; i < DESIRED_CLOSE_CONNECTIONS; ++i) {
if (!g->closest_peers[i].entry) {
continue;
}
int friendcon_id = getfriend_conn_id_pk(g_c->fr_c, g->closest_peers[i].real_pk);
uint8_t lock = 1;
if (friendcon_id == -1) {
friendcon_id = new_friend_connection(g_c->fr_c, g->closest_peers[i].real_pk);
lock = 0;
if (friendcon_id == -1) {
continue;
}
set_dht_temp_pk(g_c->fr_c, friendcon_id, g->closest_peers[i].temp_pk, userdata);
}
add_conn_to_groupchat(g_c, friendcon_id, groupnumber, 1, lock);
if (friend_con_connected(g_c->fr_c, friendcon_id) == FRIENDCONN_STATUS_CONNECTED) {
send_packet_online(g_c->fr_c, friendcon_id, groupnumber, g->identifier);
}
}
g->changed = GROUPCHAT_CLOSEST_NONE;
return 0;
}
static int get_frozen_index(Group_c *g, uint16_t peer_number)
{
for (uint32_t i = 0; i < g->numfrozen; ++i) {
if (g->frozen[i].peer_number == peer_number) {
return i;
}
}
return -1;
}
/* Update last_active timestamp on peer, and thaw the peer if it is frozen.
*
* return peer index if peer is in the conference.
* return -1 otherwise, and on error.
*/
static int note_peer_active(Group_Chats *g_c, uint32_t groupnumber, uint16_t peer_number, void *userdata)
{
Group_c *g = get_group_c(g_c, groupnumber);
if (!g) {
return -1;
}
int peer_index = get_peer_index(g, peer_number);
if (peer_index != -1) {
g->group[peer_index].last_active = unix_time();
return peer_index;
}
int frozen_index = get_frozen_index(g, peer_number);
if (frozen_index == -1) {
return -1;
}
/* Now thaw the peer */
Group_Peer *temp = (Group_Peer *)realloc(g->group, sizeof(Group_Peer) * (g->numpeers + 1));
if (temp == nullptr) {
return -1;
}
memcpy(&temp[g->numpeers], &g->frozen[frozen_index], sizeof(Group_Peer));
temp[g->numpeers].nick_updated = false;
temp[g->numpeers].temp_pk_updated = false;
g->group = temp;
g->group[g->numpeers].last_active = unix_time();
add_to_closest(g_c, groupnumber, g->group[g->numpeers].real_pk, g->group[g->numpeers].temp_pk);
++g->numpeers;
if (g_c->peer_list_changed_callback) {
g_c->peer_list_changed_callback(g_c->m, groupnumber, userdata);
}
if (g->peer_on_join) {
g->peer_on_join(g->object, groupnumber, g->numpeers - 1);
}
--g->numfrozen;
if (g->numfrozen == 0) {
free(g->frozen);
g->frozen = nullptr;
} else {
if (g->numfrozen != (uint32_t)frozen_index) {
memcpy(&g->frozen[frozen_index], &g->frozen[g->numfrozen], sizeof(Group_Peer));
}
Group_Peer *temp = (Group_Peer *)realloc(g->frozen, sizeof(Group_Peer) * (g->numfrozen));
if (temp == nullptr) {
return -1;
}
g->frozen = temp;
}
g->need_send_name = true;
return g->numpeers - 1;
}
/* Add a peer to the group chat, or update an existing peer.
*
* fresh indicates whether we should consider this information on the peer to
* be current, and so should update temp_pk and consider the peer active.
*
* do_gc_callback indicates whether we want to trigger callbacks set by the client
* via the public API. This should be set to false if this function is called
* from outside of the tox_iterate() loop.
*
* return peer_index if success or peer already in chat.
* return -1 if error.
*/
static int addpeer(Group_Chats *g_c, uint32_t groupnumber, const uint8_t *real_pk, const uint8_t *temp_pk,
uint16_t peer_number, void *userdata, bool fresh, bool do_gc_callback)
{
Group_c *g = get_group_c(g_c, groupnumber);
if (!g) {
return -1;
}
int peer_index = -1;
if (fresh) {
peer_index = note_peer_active(g_c, groupnumber, peer_number, userdata);
} else {
peer_index = get_peer_index(g, peer_number);
}
if (peer_index != -1) {
if (!id_equal(g->group[peer_index].real_pk, real_pk)) {
return -1;
}
if (fresh || !g->group[peer_index].temp_pk_updated) {
id_copy(g->group[peer_index].temp_pk, temp_pk);
g->group[peer_index].temp_pk_updated = true;
}
return peer_index;
}
Group_Peer *temp = (Group_Peer *)realloc(g->group, sizeof(Group_Peer) * (g->numpeers + 1));
if (temp == nullptr) {
return -1;
}
memset(&temp[g->numpeers], 0, sizeof(Group_Peer));
g->group = temp;
id_copy(g->group[g->numpeers].real_pk, real_pk);
id_copy(g->group[g->numpeers].temp_pk, temp_pk);
g->group[g->numpeers].temp_pk_updated = true;
g->group[g->numpeers].peer_number = peer_number;
g->group[g->numpeers].last_active = unix_time();
++g->numpeers;
add_to_closest(g_c, groupnumber, real_pk, temp_pk);
if (do_gc_callback && g_c->peer_list_changed_callback) {
g_c->peer_list_changed_callback(g_c->m, groupnumber, userdata);
}
if (g->peer_on_join) {
g->peer_on_join(g->object, groupnumber, g->numpeers - 1);
}
return g->numpeers - 1;
}
static int remove_close_conn(Group_Chats *g_c, uint32_t groupnumber, int friendcon_id)
{
Group_c *g = get_group_c(g_c, groupnumber);
if (!g) {
return -1;
}
uint32_t i;
for (i = 0; i < MAX_GROUP_CONNECTIONS; ++i) {
if (g->close[i].type == GROUPCHAT_CLOSE_NONE) {
continue;
}
if (g->close[i].number == (unsigned int)friendcon_id) {
g->close[i].type = GROUPCHAT_CLOSE_NONE;
kill_friend_connection(g_c->fr_c, friendcon_id);
return 0;
}
}
return -1;
}
static void remove_from_closest(Group_c *g, int peer_index)
{
for (uint32_t i = 0; i < DESIRED_CLOSE_CONNECTIONS; ++i) { /* If peer is in closest_peers list, remove it. */
if (g->closest_peers[i].entry && id_equal(g->closest_peers[i].real_pk, g->group[peer_index].real_pk)) {
g->closest_peers[i].entry = 0;
g->changed = GROUPCHAT_CLOSEST_REMOVED;
break;
}
}
}
/*
* Delete a peer from the group chat.
*
* return 0 if success
* return -1 if error.
*/
static int delpeer(Group_Chats *g_c, uint32_t groupnumber, int peer_index, bool freeze, void *userdata)
{
Group_c *g = get_group_c(g_c, groupnumber);
if (!g) {
return -1;
}
if (freeze) {
Group_Peer *temp = (Group_Peer *)realloc(g->frozen, sizeof(Group_Peer) * (g->numfrozen + 1));
if (temp == nullptr) {
return -1;
}
memcpy(&temp[g->numfrozen], &g->group[peer_index], sizeof(Group_Peer));
g->frozen = temp;
++g->numfrozen;
}
remove_from_closest(g, peer_index);
int friendcon_id = getfriend_conn_id_pk(g_c->fr_c, g->group[peer_index].real_pk);
if (friendcon_id != -1 && !freeze) {
remove_close_conn(g_c, groupnumber, friendcon_id);
}
--g->numpeers;
void *peer_object = g->group[peer_index].object;
if (g->numpeers == 0) {
free(g->group);
g->group = nullptr;
} else {
if (g->numpeers != (uint32_t)peer_index) {
memcpy(&g->group[peer_index], &g->group[g->numpeers], sizeof(Group_Peer));
}
Group_Peer *temp = (Group_Peer *)realloc(g->group, sizeof(Group_Peer) * (g->numpeers));
if (temp == nullptr) {
return -1;
}
g->group = temp;
}
if (g_c->peer_list_changed_callback) {
g_c->peer_list_changed_callback(g_c->m, groupnumber, userdata);
}
if (g->peer_on_leave) {
g->peer_on_leave(g->object, groupnumber, peer_object);
}
return 0;
}
/* Set the nick for a peer.
*
* do_gc_callback indicates whether we want to trigger callbacks set by the client
* via the public API. This should be set to false if this function is called
* from outside of the tox_iterate() loop.
*
* return 0 on success.
* return -1 if error.
*/
static int setnick(Group_Chats *g_c, uint32_t groupnumber, int peer_index, const uint8_t *nick, uint16_t nick_len,
void *userdata, bool do_gc_callback)
{
if (nick_len > MAX_NAME_LENGTH) {
return -1;
}
Group_c *g = get_group_c(g_c, groupnumber);
if (!g) {
return -1;
}
g->group[peer_index].nick_updated = true;
/* same name as already stored? */
if (g->group[peer_index].nick_len == nick_len) {
if (nick_len == 0 || !memcmp(g->group[peer_index].nick, nick, nick_len)) {
return 0;
}
}
if (nick_len) {
memcpy(g->group[peer_index].nick, nick, nick_len);
}
g->group[peer_index].nick_len = nick_len;
if (do_gc_callback && g_c->peer_name_callback) {
g_c->peer_name_callback(g_c->m, groupnumber, peer_index, nick, nick_len, userdata);
}
return 0;
}
static int settitle(Group_Chats *g_c, uint32_t groupnumber, int peer_index, const uint8_t *title, uint8_t title_len,
void *userdata)
{
if (title_len > MAX_NAME_LENGTH || title_len == 0) {
return -1;
}
Group_c *g = get_group_c(g_c, groupnumber);
if (!g) {
return -1;
}
/* same as already set? */
if (g->title_len == title_len && !memcmp(g->title, title, title_len)) {
return 0;
}
memcpy(g->title, title, title_len);
g->title_len = title_len;
if (g_c->title_callback) {
g_c->title_callback(g_c->m, groupnumber, peer_index, title, title_len, userdata);
}
return 0;
}
/* Check if the group has no online connection, and freeze all peers if so */
static void check_disconnected(Group_Chats *g_c, uint32_t groupnumber, void *userdata)
{
Group_c *g = get_group_c(g_c, groupnumber);
if (!g) {
return;
}
for (uint32_t i = 0; i < MAX_GROUP_CONNECTIONS; ++i) {
if (g->close[i].type == GROUPCHAT_CLOSE_ONLINE) {
return;
}
}
for (uint32_t i = 0; i < g->numpeers; ++i) {
if (id_equal(g->group[i].real_pk, g->real_pk)) {
continue;
}
delpeer(g_c, groupnumber, i, true, userdata);
}
}
static void set_conns_type_close(Group_Chats *g_c, uint32_t groupnumber, int friendcon_id, uint8_t type, void *userdata)
{
Group_c *g = get_group_c(g_c, groupnumber);
if (!g) {
return;
}
uint32_t i;
for (i = 0; i < MAX_GROUP_CONNECTIONS; ++i) {
if (g->close[i].type == GROUPCHAT_CLOSE_NONE) {
continue;
}
if (g->close[i].number != (unsigned int)friendcon_id) {
continue;
}
if (type == GROUPCHAT_CLOSE_ONLINE) {
send_packet_online(g_c->fr_c, friendcon_id, groupnumber, g->identifier);
} else {
g->close[i].type = type;
check_disconnected(g_c, groupnumber, userdata);
}
}
}
/* 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, void *userdata)
{
for (uint16_t i = 0; i < g_c->num_chats; ++i) {
set_conns_type_close(g_c, i, friendcon_id, type, userdata);
}
}
static int try_send_rejoin(Group_Chats *g_c, uint32_t groupnumber, const uint8_t *real_pk);
static void rejoin_frozen_friend(Group_Chats *g_c, int friendcon_id)
{
uint8_t real_pk[CRYPTO_PUBLIC_KEY_SIZE];
get_friendcon_public_keys(real_pk, nullptr, g_c->fr_c, friendcon_id);
for (uint16_t i = 0; i < g_c->num_chats; ++i) {
Group_c *g = get_group_c(g_c, i);
if (!g) {
continue;
}
for (uint32_t j = 0; j < g->numfrozen; ++j) {
if (id_equal(g->frozen[j].real_pk, real_pk)) {
try_send_rejoin(g_c, i, real_pk);
break;
}
}
}
}
static int g_handle_status(void *object, int friendcon_id, uint8_t status, void *userdata)
{
Group_Chats *g_c = (Group_Chats *)object;
if (status) { /* Went online */
set_conns_status_groups(g_c, friendcon_id, GROUPCHAT_CLOSE_ONLINE, userdata);
rejoin_frozen_friend(g_c, friendcon_id);
} else { /* Went offline */
set_conns_status_groups(g_c, friendcon_id, GROUPCHAT_CLOSE_CONNECTION, userdata);
// TODO(irungentoo): remove timedout connections?
}
return 0;
}
static int g_handle_packet(void *object, int friendcon_id, const uint8_t *data, uint16_t length, void *userdata);
static int handle_lossy(void *object, int friendcon_id, const uint8_t *data, uint16_t length, void *userdata);
/* Add friend to group chat.
*
* return close index on success
* return -1 on failure.
*/
static int add_conn_to_groupchat(Group_Chats *g_c, int friendcon_id, uint32_t groupnumber, uint8_t closest,
uint8_t lock)
{
Group_c *g = get_group_c(g_c, groupnumber);
if (!g) {
return -1;
}
uint16_t i, ind = MAX_GROUP_CONNECTIONS;
for (i = 0; i < MAX_GROUP_CONNECTIONS; ++i) {
if (g->close[i].type == GROUPCHAT_CLOSE_NONE) {
ind = i;
continue;
}
if (g->close[i].number == (uint32_t)friendcon_id) {
g->close[i].closest |= closest;
return i; /* Already in list. */
}
}
if (ind == MAX_GROUP_CONNECTIONS) {
return -1;
}
if (lock) {
friend_connection_lock(g_c->fr_c, friendcon_id);
}
g->close[ind].type = GROUPCHAT_CLOSE_CONNECTION;
g->close[ind].number = friendcon_id;
g->close[ind].closest = closest;
g->close[ind].introducer = false;
g->close[ind].introduced = false;
// TODO(irungentoo):
friend_connection_callbacks(g_c->m->fr_c, friendcon_id, GROUPCHAT_CALLBACK_INDEX, &g_handle_status, &g_handle_packet,
&handle_lossy, g_c, friendcon_id);
return ind;
}
/* Creates a new groupchat and puts it in the chats array.
*
* type is one of GROUPCHAT_TYPE_*
*
* return group number on success.
* return -1 on failure.
*/
int add_groupchat(Group_Chats *g_c, uint8_t type)
{
int32_t groupnumber = create_group_chat(g_c);
if (groupnumber == -1) {
return -1;
}
Group_c *g = &g_c->chats[groupnumber];
g->status = GROUPCHAT_STATUS_CONNECTED;
new_symmetric_key(g->identifier + 1);
g->identifier[0] = type;
g->peer_number = 0; /* Founder is peer 0. */
memcpy(g->real_pk, nc_get_self_public_key(g_c->m->net_crypto), CRYPTO_PUBLIC_KEY_SIZE);
int peer_index = addpeer(g_c, groupnumber, g->real_pk, dht_get_self_public_key(g_c->m->dht), 0, nullptr, true, false);
if (peer_index == -1) {
return -1;
}
setnick(g_c, groupnumber, peer_index, g_c->m->name, g_c->m->name_length, nullptr, false);
return groupnumber;
}
static int group_kill_peer_send(const Group_Chats *g_c, uint32_t groupnumber, uint16_t peer_num);
/* Delete a groupchat from the chats array.
*
* return 0 on success.
* return -1 if groupnumber is invalid.
*/
int del_groupchat(Group_Chats *g_c, uint32_t groupnumber)
{
Group_c *g = get_group_c(g_c, groupnumber);
if (!g) {
return -1;
}
group_kill_peer_send(g_c, groupnumber, g->peer_number);
unsigned int i;
for (i = 0; i < MAX_GROUP_CONNECTIONS; ++i) {
if (g->close[i].type == GROUPCHAT_CLOSE_NONE) {
continue;
}
g->close[i].type = GROUPCHAT_CLOSE_NONE;
kill_friend_connection(g_c->fr_c, g->close[i].number);
}
if (g->peer_on_leave) {
g->peer_on_leave(g->object, groupnumber, g->group[i].object);
}
free(g->group);
if (g->group_on_delete) {
g->group_on_delete(g->object, groupnumber);
}