forked from irungentoo/toxcore
-
Notifications
You must be signed in to change notification settings - Fork 279
/
group.c
4104 lines (3086 loc) · 105 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
/* group.c
*
* Slightly better groupchats implementation.
*
* Copyright (C) 2014 Tox project All Rights Reserved.
*
* This file is part of Tox.
*
* 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 "util.h"
typedef ptrdiff_t aint;
#define CLOSEST( g, i ) (((g)->closest_peers_entry & (1ull << (i))) != 0)
#define CLOSEST_SET( g, i ) { (g)->closest_peers_entry |= (1ull << (i)); }
typedef enum {
UNS_NONE,
UNS_TEMP,
UNS_FOREVER,
} UnsubscribeType;
/* Interval in seconds to send ping messages */
#define GROUP_PING_INTERVAL 20
enum {
GROUP_MESSAGE_PING_ID = 0,
GROUP_MESSAGE_UNSUBSCRIBE_ID = 1,
GROUP_MESSAGE_NICKNAME_ID = 2,
GROUP_MESSAGE_NEW_PEER_ID = 16,
GROUP_MESSAGE_KILL_PEER_ID = 17,
GROUP_MESSAGE_NAME_ID = 48,
GROUP_MESSAGE_TITLE_ID = 49
};
#define GROUP_MESSAGE_NEW_PEER_LENGTH (sizeof(uint16_t) + crypto_box_PUBLICKEYBYTES * 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))
enum {
INVITE_ID = 0,
INVITE_RESPONSE_ID = 1,
INVITE_UNSUBSCRIBE_ID = 2,
INVITE_MYGROUP_ID = 3,
};
#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)
enum {
PEER_KILL_ID = 1,
PEER_QUERY_ID = 8,
PEER_RESPONSE_ID = 9,
PEER_TITLE_ID = 10,
PEER_GROUP_NUM_ID = 11,
};
#define MIN_MESSAGE_PACKET_LEN (sizeof(uint16_t) * 2 + sizeof(uint32_t) + 1)
#define MAX_FAILED_JOIN_ATTEMPTS 16
static void group_name_send(const Group_Chats *g_c, aint groupnumber, const uint8_t *nick, aint nick_len);
static aint send_packet_online(Friend_Connections *fr_c, aint friendcon_id, uint16_t group_num, uint8_t *identifier);
static void send_peer_kill(Group_Chats *g_c, aint friendcon_id, uint16_t group_num);
static int 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);
static void send_peers(Group_Chats *g_c, aint groupnumber, int friendcon_id, uint16_t other_group_num);
static int get_self_peer_gid(Group_c *g);
static bool really_connected(Group_Peer *peer)
{
return peer->connected && peer->friendcon_id >= 0 && peer->group_number != 0xffff;
}
static bool is_groupnumber_valid(const Group_Chats *g_c, aint groupnumber)
{
if ((unsigned)groupnumber >= g_c->num_chats) {
return false;
}
if (!g_c->chats) {
return false;
}
return g_c->chats[groupnumber].live;
}
/* Set the size of the groupchat list to num.
*
* return -1 if realloc fails.
* return 0 if it succeeds.
*/
static aint realloc_groupchats(Group_Chats *g_c, aint num)
{
if (num == 0) {
free(g_c->chats);
g_c->chats = NULL;
return 0;
}
Group_c *newgroup_chats = (Group_c *)realloc(g_c->chats, num * sizeof(Group_c));
if (newgroup_chats == NULL) {
return -1;
}
g_c->chats = newgroup_chats;
return 0;
}
static void setup_conference(Group_c *g)
{
memset(g, 0, sizeof(Group_c));
g->keep_join_index = -1;
g->live = true;
}
/* Create a new empty groupchat connection.
*
* return -1 on failure.
* return groupnumber on success.
*/
static aint create_group_chat(Group_Chats *g_c)
{
aint i;
for (i = 0; i < g_c->num_chats; ++i) {
if (!g_c->chats[i].live) {
setup_conference(g_c->chats + i);
return i;
}
}
aint id = -1;
if (realloc_groupchats(g_c, g_c->num_chats + 1) == 0) {
id = g_c->num_chats;
++g_c->num_chats;
setup_conference(g_c->chats + id);
}
return id;
}
static Group_c *get_group_c(const Group_Chats *g_c, aint groupnumber)
{
if (!is_groupnumber_valid(g_c, groupnumber)) {
return NULL;
}
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 aint peer_in_chat(const Group_c *chat, const uint8_t *real_pk)
{
size_t i;
for (i = 0; i < chat->numpeers; ++i) {
if (chat->peers[i].friendcon_id != -2 && id_equal(chat->peers[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 int get_group_num(const Group_Chats *g_c, const uint8_t *identifier)
{
aint i;
for (i = 0; i < g_c->num_chats; ++i) {
if (g_c->chats[i].live && sodium_memcmp(g_c->chats[i].identifier, identifier, GROUP_IDENTIFIER_LENGTH) == 0) {
return (int)i;
}
}
return -1;
}
int conference_by_uid(const Group_Chats *g_c, const uint8_t *uid)
{
aint i;
for (i = 0; i < g_c->num_chats; ++i) {
if (!g_c->chats[i].live) {
continue;
}
if (sodium_memcmp(g_c->chats[i].identifier + 1, uid, GROUP_IDENTIFIER_LENGTH - 1) == 0) {
return (int)i;
}
}
return -1;
}
/*
* check if peer with peer_number is in peer array.
*
* return peer number if peer is in chat.
* return -1 if peer is not in chat.
*
*/
static aint get_peer_index(const Group_c *g, uint16_t peer_gid)
{
aint i;
for (i = 0; i < g->numpeers; ++i) {
if (g->peers[i].gid == (int)peer_gid) {
return i;
}
}
return -1;
}
static uint16_t find_new_peer_gid(const Group_c *g)
{
uint16_t peer_number;
randombytes((uint8_t *)&peer_number, 2);
size_t tries = 0;
while (get_peer_index(g, peer_number) != -1) {
randombytes((uint8_t *)&peer_number, 2);
++tries;
if (tries > 32) {
/* just find first unused peer number */
peer_number = 0;
while (get_peer_index(g, peer_number) != -1) {
++peer_number;
}
break;
}
}
return peer_number;
}
static uint64_t calculate_comp_value(const uint8_t *pk1, const uint8_t *pk2)
{
uint64_t cmp1 = 0, cmp2 = 0;
size_t i;
for (i = 0; i < sizeof(uint64_t); ++i) {
cmp1 = (cmp1 << 8) + (uint64_t)pk1[i];
cmp2 = (cmp2 << 8) + (uint64_t)pk2[i];
}
return (cmp1 - cmp2);
}
static void add_closest(Group_c *g, size_t peerindex)
{
size_t i;
for (i = 0; i < DESIRED_CLOSE_CONNECTIONS; ++i) {
if (!CLOSEST(g, i)) {
g->closest_peers[i] = (uint16_t)peerindex;
CLOSEST_SET(g, i);
return;
}
}
size_t index = DESIRED_CLOSE_CONNECTIONS;
const uint8_t *real_pk = g->peers[peerindex].real_pk;
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->peers[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->peers[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) {
aint rmpeer = g->closest_peers[index];
g->closest_peers[index] = (uint16_t)peerindex;
g->closest_peers_entry |= 1 << index;
add_closest(g, rmpeer);
}
}
static int handle_status(void *object, int friendcon_id, uint8_t status, void *userdata)
{
if (status) {
return 0;
}
size_t ig, i;
Group_Chats *g_c = (Group_Chats *)object;
for (ig = 0; ig < g_c->num_chats; ++ig) {
Group_c *g = get_group_c(g_c, ig);
if (!g) {
continue;
}
for (i = 0; i < g->numpeers; ++i) {
if (g->peers[i].friendcon_id == friendcon_id) {
g->peers[i].friendcon_id = -1;
g->peers[i].group_number = 0xffff;
g->peers[i].connected = false;
}
}
}
return 0;
}
static void apply_changes_in_peers(Group_Chats *g_c, aint groupnumber, void *userdata)
{
Group_c *g = get_group_c(g_c, groupnumber);
if (!g) {
return;
}
bool some_changes = false;
if (g->dirty_list) {
size_t i, j, empty_slots_in_list = 0;
/* detect almost deleted peers and prepare peers_list slots for delete */
for (i = 0; i < g->numpeers_in_list; ++i) {
aint peer_index = g->peers_list[i];
if (g->peers[peer_index].friendcon_id == -2 || g->peers[peer_index].gid < 0) {
some_changes = true;
g->peers_list[i] = 0xffff;
++empty_slots_in_list;
}
}
aint new_size_of_list = 0;
/* Put new peers to just deleted slots */
for (i = 0; i < g->numpeers; ++i) {
const Group_Peer *peer = g->peers + i;
if (peer->friendcon_id != -2 && peer->gid >= 0) {
++new_size_of_list;
if (empty_slots_in_list > 0) {
aint empty_slot = -1;
bool present = false;
for (j = 0; j < g->numpeers_in_list; ++j) {
if (empty_slot < 0 && g->peers_list[j] == 0xffff) {
empty_slot = j;
}
if (g->peers_list[j] == i) {
present = true;
}
if (empty_slot >= 0 && present) {
break;
}
}
if (!present) {
g->peers_list[empty_slot] = (uint16_t)i;
--empty_slots_in_list;
some_changes = true;
}
}
}
}
if (empty_slots_in_list > 0) {
/* all new peers were put into empty slots, so there are no new peers */
/* just remove empty slots both in peers_list and peers*/
/* remove empty slots in peers_list */
for (j = 0; j < g->numpeers_in_list;) {
if (g->peers_list[j] == 0xffff) {
--g->numpeers_in_list;
/* g->peers_list[j] = g->peers_list[g->numpeers_in_list]; */ /* faster */
memcpy(g->peers_list + j, g->peers_list + j + 1,
sizeof(uint16_t) * (g->numpeers_in_list - j)); /* slower, but accurate */
some_changes = true;
continue;
}
++j;
}
/* remove empty slots in peers */
for (i = 0; i < g->numpeers;) {
Group_Peer *peer = g->peers + i;
if (peer->friendcon_id == -2) {
--g->numpeers;
memcpy(peer, g->peers + g->numpeers, sizeof(Group_Peer));
/* fix index in peers_list */
for (j = 0; j < g->numpeers_in_list; ++j) {
if (g->peers_list[j] == g->numpeers) {
g->peers_list[j] = (uint16_t)i;
break;
}
}
continue;
}
++i;
}
} else if (new_size_of_list > g->numpeers_in_list) {
/* Expand peers_list and put indexes of new peers into it */
uint16_t *temp = (uint16_t *)realloc(g->peers_list, sizeof(uint16_t) * new_size_of_list);
if (temp) {
g->peers_list = temp;
for (i = 0; i < g->numpeers; ++i) {
const Group_Peer *peer = g->peers + i;
if (peer->friendcon_id != -2 && peer->gid >= 0) {
bool present = false;
for (j = 0; j < g->numpeers_in_list; ++j) {
if (g->peers_list[j] == i) {
present = true;
break;
}
}
if (!present) {
g->peers_list[g->numpeers_in_list] = (uint16_t)i;
++g->numpeers_in_list;
some_changes = true;
}
}
}
}
}
/* Now notify client by calling callbacks */
if (g_c->group_namelistchange) {
if (some_changes) {
if (!g->invite_called && !g->join_mode) {
if (g_c->invite_callback) {
g->fake_join = true;
g_c->invite_callback(g_c->m, UINT32_MAX, g->identifier[0], g->identifier + 1, GROUP_IDENTIFIER_LENGTH - 1, userdata);
g->fake_join = false;
if (!g->invite_called) {
del_groupchat(g_c, (int)groupnumber);
return;
}
}
}
g_c->group_namelistchange(g_c->m, (int)groupnumber, 0, CHAT_CHANGE_OCCURRED, userdata);
g->nick_changed = false;
}
}
/* and now rebuild closest */
uint16_t old_closest_peers[DESIRED_CLOSE_CONNECTIONS];
size_t inclose = 0;
for (i = 0; i < DESIRED_CLOSE_CONNECTIONS; ++i) {
if (CLOSEST(g,i)) {
old_closest_peers[inclose++] = g->closest_peers[i];
}
}
g->closest_peers_entry = 0;
size_t np = g->numpeers;
for (i = 0; i < np; ++i) {
Group_Peer *peer = g->peers + i;
if (peer->friendcon_id == -2 || peer->auto_join || id_equal(g->real_pk, peer->real_pk)) {
continue;
}
add_closest(g, i);
}
for (i = 0; i < DESIRED_CLOSE_CONNECTIONS; ++i) {
if (CLOSEST(g,i)) {
bool just_added = true;
for (j = 0; j < inclose; ++j) {
if (g->closest_peers[i] == old_closest_peers[j]) {
just_added = false;
break;
}
}
Group_Peer *peer = g->peers + g->closest_peers[i];
if (just_added && peer->connected) {
send_packet_online(g_c->fr_c, peer->friendcon_id, (uint16_t)groupnumber, g->identifier);
}
}
}
g->dirty_list = false;
}
/* notify client about nick change */
if (g->nick_changed && g_c->group_namelistchange) {
size_t i;
for (i = 0; i < g->numpeers_in_list; ++i) {
Group_Peer *peer = g->peers + g->peers_list[i];
if (peer->nick_changed) {
g_c->group_namelistchange(g_c->m, (int)groupnumber, (int)i, CHAT_CHANGE_PEER_NAME, userdata);
peer->nick_changed = false;
}
}
g->nick_changed = false;
}
/* notify client about title change */
if (g->title_changed && g_c->title_callback) {
size_t i;
bool cb = false;
for (i = 0; i < g->numpeers_in_list; ++i) {
Group_Peer *peer = g->peers + g->peers_list[i];
if (peer->title_changed) {
if (!cb) {
g_c->title_callback(g_c->m, (int)groupnumber, (int)i, g->title, g->title_len, userdata);
}
peer->title_changed = false;
cb = true;
}
}
if (!cb) {
g_c->title_callback(g_c->m, (int)groupnumber, -1, g->title, g->title_len, userdata);
}
g->title_changed = false;
}
}
static void send_peer_nums(const Group_Chats *g_c, aint groupnumber, aint friendcon_id, uint16_t other_group_num);
static void connect_to_closest(Group_Chats *g_c, aint groupnumber, void *userdata)
{
Group_c *g = get_group_c(g_c, groupnumber);
if (!g) {
return;
}
if (g->dirty_list) {
return;
}
size_t i;
if (!g->join_mode && is_timeout(g->last_close_check_time, 5)) {
/* kill connections to non closest */
g->last_close_check_time = unix_time();
size_t nconnected = 0;
for (i = 0; i < g->numpeers; ++i) {
if (g->peers[i].connected) {
++nconnected;
}
}
if (nconnected > DESIRED_CLOSE_CONNECTIONS) {
for (i = 0; i < g->numpeers; ++i) {
Group_Peer *peer = g->peers + i;
if (peer->friendcon_id < 0 || peer->keep_connection) {
continue;
}
size_t k;
for (k = 0; k < DESIRED_CLOSE_CONNECTIONS; ++k) {
if (CLOSEST(g,k) && g->closest_peers[k] == i) {
k = DESIRED_CLOSE_CONNECTIONS + 100;
break;
}
}
if (k == DESIRED_CLOSE_CONNECTIONS + 100) {
continue;
}
if (really_connected(peer)) {
send_peer_kill(g_c, peer->friendcon_id, peer->group_number);
}
kill_friend_connection(g_c->fr_c, peer->friendcon_id);
peer->friendcon_id = -1;
peer->group_number = 0xffff;
peer->connected = false;
}
}
}
for (i = 0; i < DESIRED_CLOSE_CONNECTIONS; ++i) {
if (!CLOSEST(g,i)) {
continue;
}
Group_Peer *peer = g->peers + g->closest_peers[i];
if (peer->friendcon_id < 0) {
int friendcon_id = getfriend_conn_id_pk(g_c->fr_c, peer->real_pk);
bool addref = true;
if (friendcon_id < 0) {
friendcon_id = new_friend_connection(g_c->fr_c, peer->real_pk);
addref = false;
if (friendcon_id == -1) {
continue;
}
set_dht_temp_pk(g_c->fr_c, friendcon_id, peer->temp_pk, userdata);
}
if (addref) {
friend_connection_lock(g_c->fr_c, friendcon_id);
}
peer->friendcon_id = friendcon_id;
friend_connection_callbacks(g_c->m->fr_c, friendcon_id, GROUPCHAT_CALLBACK_INDEX, &handle_status, &handle_packet,
&handle_lossy, g_c, friendcon_id);
}
if (friend_con_connected(g_c->fr_c, peer->friendcon_id) == FRIENDCONN_STATUS_CONNECTED) {
if (!peer->connected) {
peer->connected = true;
peer->need_send_peers = true;
g->need_send_name = true;
send_packet_online(g_c->fr_c, peer->friendcon_id, (uint16_t)groupnumber, g->identifier);
}
} else {
peer->connected = false;
}
if (peer->need_send_peers && really_connected(peer)) {
send_peers(g_c, groupnumber, peer->friendcon_id, peer->group_number);
send_peer_nums(g_c, groupnumber, peer->friendcon_id, peer->group_number);
peer->need_send_peers = false;
}
}
if (g->need_send_name) {
group_name_send(g_c, groupnumber, g_c->m->name, g_c->m->name_length);
}
}
static void addjoinpeer(Group_c *g, const uint8_t *real_pk)
{
if (id_equal(g->real_pk, real_pk)) {
return;
}
size_t i;
bool already_here = false;
for (i = 0; i < g->numjoinpeers; ++i) {
if (!already_here && id_equal(g->joinpeers[i].real_pk, real_pk)) {
already_here = true;
g->joinpeers[i].unsubscribed = false;
}
g->joinpeers[i].fails = MAX_FAILED_JOIN_ATTEMPTS;
}
if (!already_here) {
Group_Join_Peer *temp = (Group_Join_Peer *)realloc(g->joinpeers, sizeof(Group_Join_Peer) * (g->numjoinpeers + 1));
if (temp == NULL) {
return;
}
g->joinpeers = temp;
temp = g->joinpeers + g->numjoinpeers;
++g->numjoinpeers;
memset(temp, 0, sizeof(Group_Join_Peer));
id_copy(temp->real_pk, real_pk);
temp->fails = MAX_FAILED_JOIN_ATTEMPTS;
}
}
static void need_send_peers(Group_c *g)
{
size_t i;
for (i = 0; i < g->numpeers; ++i) {
g->peers[i].need_send_peers = true;
}
}
static aint addpeer(Group_c *g, aint groupnumber, const uint8_t *real_pk, const uint8_t *temp_pk, int peer_gid)
{
if (peer_gid >= 0) {
addjoinpeer(g, real_pk);
}
aint peer_index = peer_in_chat(g, real_pk);
if (peer_index != -1) {
id_copy(g->peers[peer_index].temp_pk, temp_pk);
if (peer_gid != g->peers[peer_index].gid) {
g->dirty_list = true;
if (peer_gid >= 0) {
need_send_peers(g);
}
g->peers[peer_index].gid = peer_gid;
if (peer_gid >= 0) {
g->peers[peer_index].auto_join = false;
}
}
return peer_index;
}
peer_index = get_peer_index(g, peer_gid);
if (peer_index != -1) {
return -1;
}
Group_Peer *newpeer = (Group_Peer *)realloc(g->peers, sizeof(Group_Peer) * (g->numpeers + 1));
if (newpeer == NULL) {
return -1;
}
g->peers = newpeer;
newpeer = newpeer + g->numpeers;
++g->numpeers;
/* Group_Peer constructor */
memset(newpeer, 0, sizeof(Group_Peer));
newpeer->friendcon_id = -1; /* set undefined */
newpeer->group_number = id_equal(g->real_pk, real_pk) ? (uint16_t)groupnumber : 0xffff; /* set undefined for other */
id_copy(newpeer->real_pk, real_pk);
id_copy(newpeer->temp_pk, temp_pk);
if (peer_gid >= 0) {
need_send_peers(g);
newpeer->gid = peer_gid;
} else {
newpeer->gid = -1;
}
newpeer->last_recv = unix_time();
g->dirty_list = true;
if (g->peer_on_join) {
g->peer_on_join(g->object, (int)groupnumber, g->numpeers - 1);
}
return (g->numpeers - 1);
}
static void free_peer_stuff(Group_Peer *peer)
{
free(peer->lossy);
peer->lossy = NULL;
free(peer->nick);
peer->nick = NULL;
}
/*
* Delete a peer from the group chat.
*
* return 0 if success
* return -1 if error.
*/
static int delpeer(Group_Chats *g_c, aint groupnumber, aint peer_index)
{
Group_c *g = get_group_c(g_c, groupnumber);
if (!g) {
return -1;
}
Group_Peer *peer = g->peers + peer_index;
if (peer->friendcon_id >= 0) {
kill_friend_connection(g_c->fr_c, peer->friendcon_id);
}
peer->friendcon_id = -2; /* -2 means almost deleted peer.
It just mark "deleted", not really deleted. See apply_changes_in_peers */
peer->connected = false;
peer->group_number = 0xffff;
peer->gid = -1;
free_peer_stuff(peer);
g->dirty_list = true;
if (g->peer_on_leave) {
g->peer_on_leave(g->object, (int)groupnumber, peer->object);
}
peer->object = NULL;
return 0;
}
int find_peer_index_in_list(const Group_c *g, aint peer_index)
{
size_t i;
for (i = 0; i < g->numpeers_in_list; ++i) {
if (g->peers_list[i] == peer_index) {
return (int)i;
}
}
return -1;
}
/* Set the nick for a peer.
*
* return 0 on success.
* return -1 if error.
*/
static aint setnick(Group_c *g, aint peer_index, const uint8_t *nick, aint nick_len)
{
if (nick_len > MAX_NAME_LENGTH || nick_len == 0) {
return -1;
}
Group_Peer *peer = g->peers + peer_index;
/* same name as already stored? */
if (peer->nick_len == nick_len) {
if (nick_len == 0 || !memcmp(peer->nick, nick, nick_len)) {
return 0;
}
}
if (nick_len) {
if (nick_len > peer->nick_len) {
uint8_t *new_nick = realloc(peer->nick, nick_len + 1);
if (!new_nick) {
return 0;
}
peer->nick = new_nick;
peer->nick[nick_len] = 0;
}
memcpy(peer->nick, nick, nick_len);
}
peer->nick_len = (uint8_t)nick_len;
peer->nick_changed = true;
g->nick_changed = true;
return 0;
}
static aint settitle(Group_c *g, aint peer_index, const uint8_t *title, aint title_len)
{
if (title_len > MAX_NAME_LENGTH || title_len == 0) {
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);