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
|
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2016-2020 The TokTok team.
* Copyright © 2015 Tox project.
*/
#include "group_announce.h"
#include <stdlib.h>
#include <string.h>
#include "LAN_discovery.h"
#include "ccompat.h"
#include "mono_time.h"
#include "util.h"
/**
* Removes `announces` from `gc_announces_list`.
*/
non_null()
static void remove_announces(GC_Announces_List *gc_announces_list, GC_Announces *announces)
{
if (announces == nullptr || gc_announces_list == nullptr) {
return;
}
if (announces->prev_announce != nullptr) {
announces->prev_announce->next_announce = announces->next_announce;
} else {
gc_announces_list->root_announces = announces->next_announce;
}
if (announces->next_announce != nullptr) {
announces->next_announce->prev_announce = announces->prev_announce;
}
free(announces);
}
/**
* Returns the announce designated by `chat_id`.
* Returns null if no announce is found.
*/
non_null()
static GC_Announces *get_announces_by_chat_id(const GC_Announces_List *gc_announces_list, const uint8_t *chat_id)
{
GC_Announces *announces = gc_announces_list->root_announces;
while (announces != nullptr) {
if (memcmp(announces->chat_id, chat_id, CHAT_ID_SIZE) == 0) {
return announces;
}
announces = announces->next_announce;
}
return nullptr;
}
int gca_get_announces(const GC_Announces_List *gc_announces_list, GC_Announce *gc_announces, uint8_t max_nodes,
const uint8_t *chat_id, const uint8_t *except_public_key)
{
if (gc_announces == nullptr || gc_announces_list == nullptr || chat_id == nullptr || max_nodes == 0
|| except_public_key == nullptr) {
return -1;
}
const GC_Announces *announces = get_announces_by_chat_id(gc_announces_list, chat_id);
if (announces == nullptr) {
return 0;
}
uint16_t added_count = 0;
for (size_t i = 0; i < announces->index && i < GCA_MAX_SAVED_ANNOUNCES_PER_GC && added_count < max_nodes; ++i) {
const size_t index = i % GCA_MAX_SAVED_ANNOUNCES_PER_GC;
if (memcmp(except_public_key, &announces->peer_announces[index].base_announce.peer_public_key,
ENC_PUBLIC_KEY_SIZE) == 0) {
continue;
}
bool already_added = false;
for (size_t j = 0; j < added_count; ++j) {
if (memcmp(&gc_announces[j].peer_public_key, &announces->peer_announces[index].base_announce.peer_public_key,
ENC_PUBLIC_KEY_SIZE) == 0) {
already_added = true;
break;
}
}
if (!already_added) {
gc_announces[added_count] = announces->peer_announces[index].base_announce;
++added_count;
}
}
return added_count;
}
uint16_t gca_pack_announces_list_size(uint16_t count)
{
return count * GCA_ANNOUNCE_MAX_SIZE;
}
int gca_pack_announce(const Logger *log, uint8_t *data, uint16_t length, const GC_Announce *announce)
{
if (length < GCA_ANNOUNCE_MAX_SIZE) {
LOGGER_ERROR(log, "Invalid announce length: %u", length);
return -1;
}
if (data == nullptr) {
LOGGER_ERROR(log, "data is null");
return -1;
}
if (announce == nullptr) {
LOGGER_ERROR(log, "announce is null");
return -1;
}
uint16_t offset = 0;
memcpy(data + offset, announce->peer_public_key, ENC_PUBLIC_KEY_SIZE);
offset += ENC_PUBLIC_KEY_SIZE;
data[offset] = announce->ip_port_is_set ? 1 : 0;
++offset;
data[offset] = announce->tcp_relays_count;
++offset;
if (!announce->ip_port_is_set && announce->tcp_relays_count == 0) {
LOGGER_ERROR(log, "Failed to pack announce: no valid ip_port or tcp relay");
return -1;
}
if (announce->ip_port_is_set) {
const int ip_port_length = pack_ip_port(log, data + offset, length - offset, &announce->ip_port);
if (ip_port_length == -1) {
LOGGER_ERROR(log, "Failed to pack ip_port");
return -1;
}
offset += ip_port_length;
}
const int nodes_length = pack_nodes(log, data + offset, length - offset, announce->tcp_relays,
announce->tcp_relays_count);
if (nodes_length == -1) {
LOGGER_ERROR(log, "Failed to pack TCP nodes");
return -1;
}
return nodes_length + offset;
}
/**
* Unpacks `announce` into `data` buffer of size `length`.
*
* Returns the size of the unpacked data on success.
* Returns -1 on failure.
*/
non_null()
static int gca_unpack_announce(const Logger *log, const uint8_t *data, uint16_t length, GC_Announce *announce)
{
if (length < ENC_PUBLIC_KEY_SIZE + 2) {
LOGGER_ERROR(log, "Invalid announce length: %u", length);
return -1;
}
if (data == nullptr) {
LOGGER_ERROR(log, "data is null");
return -1;
}
if (announce == nullptr) {
LOGGER_ERROR(log, "announce is null");
return -1;
}
uint16_t offset = 0;
memcpy(announce->peer_public_key, data + offset, ENC_PUBLIC_KEY_SIZE);
offset += ENC_PUBLIC_KEY_SIZE;
announce->ip_port_is_set = data[offset] == 1;
++offset;
announce->tcp_relays_count = data[offset];
++offset;
if (announce->tcp_relays_count > GCA_MAX_ANNOUNCED_TCP_RELAYS) {
return -1;
}
if (announce->ip_port_is_set) {
if (length - offset == 0) {
return -1;
}
const int ip_port_length = unpack_ip_port(&announce->ip_port, data + offset, length - offset, false);
if (ip_port_length == -1) {
LOGGER_ERROR(log, "Failed to unpack ip_port");
return -1;
}
offset += ip_port_length;
}
uint16_t nodes_length;
const int nodes_count = unpack_nodes(announce->tcp_relays, announce->tcp_relays_count, &nodes_length,
data + offset, length - offset, true);
if (nodes_count != announce->tcp_relays_count) {
LOGGER_ERROR(log, "Failed to unpack TCP nodes");
return -1;
}
return offset + nodes_length;
}
int gca_pack_public_announce(const Logger *log, uint8_t *data, uint16_t length,
const GC_Public_Announce *public_announce)
{
if (public_announce == nullptr || data == nullptr || length < CHAT_ID_SIZE) {
return -1;
}
memcpy(data, public_announce->chat_public_key, CHAT_ID_SIZE);
const int packed_size = gca_pack_announce(log, data + CHAT_ID_SIZE, length - CHAT_ID_SIZE,
&public_announce->base_announce);
if (packed_size < 0) {
LOGGER_ERROR(log, "Failed to pack public group announce");
return -1;
}
return packed_size + CHAT_ID_SIZE;
}
int gca_unpack_public_announce(const Logger *log, const uint8_t *data, uint16_t length,
GC_Public_Announce *public_announce)
{
if (length < CHAT_ID_SIZE) {
LOGGER_ERROR(log, "invalid public announce length: %u", length);
return -1;
}
if (data == nullptr) {
LOGGER_ERROR(log, "data is null");
return -1;
}
if (public_announce == nullptr) {
LOGGER_ERROR(log, "public_announce is null");
return -1;
}
memcpy(public_announce->chat_public_key, data, CHAT_ID_SIZE);
const int base_announce_size = gca_unpack_announce(log, data + ENC_PUBLIC_KEY_SIZE, length - ENC_PUBLIC_KEY_SIZE,
&public_announce->base_announce);
if (base_announce_size == -1) {
LOGGER_ERROR(log, "Failed to unpack group announce");
return -1;
}
return base_announce_size + CHAT_ID_SIZE;
}
int gca_pack_announces_list(const Logger *log, uint8_t *data, uint16_t length, const GC_Announce *announces,
uint8_t announces_count, size_t *processed)
{
if (data == nullptr) {
LOGGER_ERROR(log, "data is null");
return -1;
}
if (announces == nullptr) {
LOGGER_ERROR(log, "announces is null");
return -1;
}
uint16_t offset = 0;
for (size_t i = 0; i < announces_count; ++i) {
const int packed_length = gca_pack_announce(log, data + offset, length - offset, &announces[i]);
if (packed_length < 0) {
LOGGER_ERROR(log, "Failed to pack group announce");
return -1;
}
offset += packed_length;
}
if (processed != nullptr) {
*processed = offset;
}
return announces_count;
}
int gca_unpack_announces_list(const Logger *log, const uint8_t *data, uint16_t length, GC_Announce *announces,
uint8_t max_count)
{
if (data == nullptr) {
LOGGER_ERROR(log, "data is null");
return -1;
}
if (announces == nullptr) {
LOGGER_ERROR(log, "announces is null");
return -1;
}
uint16_t offset = 0;
int announces_count = 0;
for (size_t i = 0; i < max_count && length > offset; ++i) {
const int unpacked_length = gca_unpack_announce(log, data + offset, length - offset, &announces[i]);
if (unpacked_length == -1) {
LOGGER_WARNING(log, "Failed to unpack group announce: %d %d", length, offset);
return -1;
}
offset += unpacked_length;
++announces_count;
}
return announces_count;
}
GC_Peer_Announce *gca_add_announce(const Mono_Time *mono_time, GC_Announces_List *gc_announces_list,
const GC_Public_Announce *public_announce)
{
if (gc_announces_list == nullptr || public_announce == nullptr) {
return nullptr;
}
GC_Announces *announces = get_announces_by_chat_id(gc_announces_list, public_announce->chat_public_key);
// No entry for this chat_id exists so we create one
if (announces == nullptr) {
announces = (GC_Announces *)calloc(1, sizeof(GC_Announces));
if (announces == nullptr) {
return nullptr;
}
announces->index = 0;
announces->prev_announce = nullptr;
if (gc_announces_list->root_announces != nullptr) {
gc_announces_list->root_announces->prev_announce = announces;
}
announces->next_announce = gc_announces_list->root_announces;
gc_announces_list->root_announces = announces;
memcpy(announces->chat_id, public_announce->chat_public_key, CHAT_ID_SIZE);
}
const uint64_t cur_time = mono_time_get(mono_time);
announces->last_announce_received_timestamp = cur_time;
const uint64_t index = announces->index % GCA_MAX_SAVED_ANNOUNCES_PER_GC;
GC_Peer_Announce *gc_peer_announce = &announces->peer_announces[index];
gc_peer_announce->base_announce = public_announce->base_announce;
gc_peer_announce->timestamp = cur_time;
++announces->index;
return gc_peer_announce;
}
bool gca_is_valid_announce(const GC_Announce *announce)
{
if (announce == nullptr) {
return false;
}
return announce->tcp_relays_count > 0 || announce->ip_port_is_set;
}
GC_Announces_List *new_gca_list(void)
{
GC_Announces_List *announces_list = (GC_Announces_List *)calloc(1, sizeof(GC_Announces_List));
return announces_list;
}
void kill_gca(GC_Announces_List *announces_list)
{
if (announces_list == nullptr) {
return;
}
GC_Announces *root = announces_list->root_announces;
while (root != nullptr) {
GC_Announces *next = root->next_announce;
free(root);
root = next;
}
free(announces_list);
}
/* How long we save a peer's announce before we consider it stale and remove it. */
#define GCA_ANNOUNCE_SAVE_TIMEOUT 30
/* How often we run do_gca() */
#define GCA_DO_GCA_TIMEOUT 1
void do_gca(const Mono_Time *mono_time, GC_Announces_List *gc_announces_list)
{
if (gc_announces_list == nullptr) {
return;
}
if (!mono_time_is_timeout(mono_time, gc_announces_list->last_timeout_check, GCA_DO_GCA_TIMEOUT)) {
return;
}
gc_announces_list->last_timeout_check = mono_time_get(mono_time);
GC_Announces *announces = gc_announces_list->root_announces;
while (announces != nullptr) {
if (mono_time_is_timeout(mono_time, announces->last_announce_received_timestamp, GCA_ANNOUNCE_SAVE_TIMEOUT)) {
GC_Announces *to_delete = announces;
announces = announces->next_announce;
remove_announces(gc_announces_list, to_delete);
continue;
}
announces = announces->next_announce;
}
}
void cleanup_gca(GC_Announces_List *gc_announces_list, const uint8_t *chat_id)
{
if (gc_announces_list == nullptr || chat_id == nullptr) {
return;
}
GC_Announces *announces = get_announces_by_chat_id(gc_announces_list, chat_id);
if (announces != nullptr) {
remove_announces(gc_announces_list, announces);
}
}
|