Coverage Report

Created: 2024-01-26 01:52

/work/toxcore/Messenger.c
Line
Count
Source (jump to first uncovered line)
1
/* SPDX-License-Identifier: GPL-3.0-or-later
2
 * Copyright © 2016-2018 The TokTok team.
3
 * Copyright © 2013 Tox project.
4
 */
5
6
/**
7
 * An implementation of a simple text chat only messenger on the tox network core.
8
 */
9
#include "Messenger.h"
10
11
#include <assert.h>
12
#include <stdio.h>
13
#include <string.h>
14
#include <time.h>
15
16
#include "DHT.h"
17
#include "TCP_client.h"
18
#include "TCP_connection.h"
19
#include "TCP_server.h"
20
#include "announce.h"
21
#include "bin_pack.h"
22
#include "bin_unpack.h"
23
#include "ccompat.h"
24
#include "crypto_core.h"
25
#include "forwarding.h"
26
#include "friend_connection.h"
27
#include "friend_requests.h"
28
#include "group_announce.h"
29
#include "group_chats.h"
30
#include "group_common.h"
31
#include "group_onion_announce.h"
32
#include "logger.h"
33
#include "mem.h"
34
#include "mono_time.h"
35
#include "net_crypto.h"
36
#include "network.h"
37
#include "onion.h"
38
#include "onion_announce.h"
39
#include "onion_client.h"
40
#include "state.h"
41
#include "util.h"
42
43
static_assert(MAX_CONCURRENT_FILE_PIPES <= UINT8_MAX + 1,
44
              "uint8_t cannot represent all file transfer numbers");
45
46
static const Friend empty_friend = {{0}};
47
48
/**
49
 * Determines if the friendnumber passed is valid in the Messenger object.
50
 *
51
 * @param friendnumber The index in the friend list.
52
 */
53
bool friend_is_valid(const Messenger *m, int32_t friendnumber)
54
821
{
55
821
    return (uint32_t)friendnumber < m->numfriends && m->friendlist[friendnumber].status != 0;
56
821
}
57
58
/** @brief Set the size of the friend list to numfriends.
59
 *
60
 * @retval -1 if mem_vrealloc fails.
61
 */
62
non_null()
63
static int realloc_friendlist(Messenger *m, uint32_t num)
64
1.88k
{
65
1.88k
    if (num == 0) {
66
0
        mem_delete(m->mem, m->friendlist);
67
0
        m->friendlist = nullptr;
68
0
        return 0;
69
0
    }
70
71
1.88k
    Friend *newfriendlist = (Friend *)mem_vrealloc(m->mem, m->friendlist, num, sizeof(Friend));
72
73
1.88k
    if (newfriendlist == nullptr) {
74
16
        return -1;
75
16
    }
76
77
1.87k
    m->friendlist = newfriendlist;
78
1.87k
    return 0;
79
1.88k
}
80
81
/** @return the friend number associated to that public key.
82
 * @retval -1 if no such friend.
83
 */
84
int32_t getfriend_id(const Messenger *m, const uint8_t *real_pk)
85
4.91k
{
86
8.55k
    for (uint32_t i = 0; i < m->numfriends; ++i) {
87
3.88k
        if (m->friendlist[i].status > 0 && pk_equal(real_pk, m->friendlist[i].real_pk)) {
88
243
            return i;
89
243
        }
90
3.88k
    }
91
92
4.66k
    return -1;
93
4.91k
}
94
95
/** @brief Copies the public key associated to that friend id into real_pk buffer.
96
 *
97
 * Make sure that real_pk is of size CRYPTO_PUBLIC_KEY_SIZE.
98
 *
99
 * @retval 0 if success.
100
 * @retval -1 if failure.
101
 */
102
int get_real_pk(const Messenger *m, int32_t friendnumber, uint8_t *real_pk)
103
2
{
104
2
    if (!m_friend_exists(m, friendnumber)) {
105
0
        return -1;
106
0
    }
107
108
2
    memcpy(real_pk, m->friendlist[friendnumber].real_pk, CRYPTO_PUBLIC_KEY_SIZE);
109
2
    return 0;
110
2
}
111
112
/** @return friend connection id on success.
113
 * @retval -1 if failure.
114
 */
115
int getfriendcon_id(const Messenger *m, int32_t friendnumber)
116
173
{
117
173
    if (!m_friend_exists(m, friendnumber)) {
118
0
        return -1;
119
0
    }
120
121
173
    return m->friendlist[friendnumber].friendcon_id;
122
173
}
123
124
/**
125
 * Format: `[real_pk (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)]`
126
 *
127
 * @param[out] address FRIEND_ADDRESS_SIZE byte address to give to others.
128
 */
129
void getaddress(const Messenger *m, uint8_t *address)
130
117
{
131
117
    pk_copy(address, nc_get_self_public_key(m->net_crypto));
132
117
    uint32_t nospam = get_nospam(m->fr);
133
117
    memcpy(address + CRYPTO_PUBLIC_KEY_SIZE, &nospam, sizeof(nospam));
134
117
    uint16_t checksum = data_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum));
135
117
    memcpy(address + CRYPTO_PUBLIC_KEY_SIZE + sizeof(nospam), &checksum, sizeof(checksum));
136
117
}
137
138
non_null()
139
static bool send_online_packet(Messenger *m, int friendcon_id)
140
2.70k
{
141
2.70k
    uint8_t packet = PACKET_ID_ONLINE;
142
2.70k
    return write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, friendcon_id), &packet,
143
2.70k
                             sizeof(packet), false) != -1;
144
2.70k
}
145
146
non_null()
147
static bool send_offline_packet(Messenger *m, int friendcon_id)
148
0
{
149
0
    uint8_t packet = PACKET_ID_OFFLINE;
150
0
    return write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, friendcon_id), &packet,
151
0
                             sizeof(packet), false) != -1;
152
0
}
153
154
non_null(1) nullable(4)
155
static int m_handle_status(void *object, int friendcon_id, bool status, void *userdata);
156
non_null(1, 3) nullable(5)
157
static int m_handle_packet(void *object, int friendcon_id, const uint8_t *data, uint16_t length, void *userdata);
158
non_null(1, 3) nullable(5)
159
static int m_handle_lossy_packet(void *object, int friendcon_id, const uint8_t *data, uint16_t length,
160
                                 void *userdata);
161
162
non_null()
163
static int32_t init_new_friend(Messenger *m, const uint8_t *real_pk, uint8_t status)
164
1.88k
{
165
1.88k
    if (m->numfriends == UINT32_MAX) {
166
0
        LOGGER_ERROR(m->log, "Friend list full: we have more than 4 billion friends");
167
        /* This is technically incorrect, but close enough. */
168
0
        return FAERR_NOMEM;
169
0
    }
170
171
    /* Resize the friend list if necessary. */
172
1.88k
    if (realloc_friendlist(m, m->numfriends + 1) != 0) {
173
16
        return FAERR_NOMEM;
174
16
    }
175
176
1.87k
    m->friendlist[m->numfriends] = empty_friend;
177
178
1.87k
    const int friendcon_id = new_friend_connection(m->fr_c, real_pk);
179
180
1.87k
    if (friendcon_id == -1) {
181
30
        return FAERR_NOMEM;
182
30
    }
183
184
3.19k
    for (uint32_t i = 0; i <= m->numfriends; ++i) {
185
3.19k
        if (m->friendlist[i].status == NOFRIEND) {
186
1.84k
            m->friendlist[i].status = status;
187
1.84k
            m->friendlist[i].friendcon_id = friendcon_id;
188
1.84k
            m->friendlist[i].friendrequest_lastsent = 0;
189
1.84k
            pk_copy(m->friendlist[i].real_pk, real_pk);
190
1.84k
            m->friendlist[i].statusmessage_length = 0;
191
1.84k
            m->friendlist[i].userstatus = USERSTATUS_NONE;
192
1.84k
            m->friendlist[i].is_typing = false;
193
1.84k
            m->friendlist[i].message_id = 0;
194
1.84k
            friend_connection_callbacks(m->fr_c, friendcon_id, MESSENGER_CALLBACK_INDEX, &m_handle_status, &m_handle_packet,
195
1.84k
                                        &m_handle_lossy_packet, m, i);
196
197
1.84k
            if (m->numfriends == i) {
198
1.84k
                ++m->numfriends;
199
1.84k
            }
200
201
1.84k
            if (friend_con_connected(m->fr_c, friendcon_id) == FRIENDCONN_STATUS_CONNECTED) {
202
0
                send_online_packet(m, friendcon_id);
203
0
            }
204
205
1.84k
            return i;
206
1.84k
        }
207
3.19k
    }
208
209
0
    return FAERR_NOMEM;
210
1.84k
}
211
212
non_null()
213
static int32_t m_add_friend_contact_norequest(Messenger *m, const uint8_t *real_pk)
214
1.72k
{
215
1.72k
    if (getfriend_id(m, real_pk) != -1) {
216
13
        return FAERR_ALREADYSENT;
217
13
    }
218
219
1.71k
    if (pk_equal(real_pk, nc_get_self_public_key(m->net_crypto))) {
220
0
        return FAERR_OWNKEY;
221
0
    }
222
223
1.71k
    return init_new_friend(m, real_pk, FRIEND_CONFIRMED);
224
1.71k
}
225
226
/**
227
 * Add a friend.
228
 *
229
 * Set the data that will be sent along with friend request.
230
 *
231
 * @param address is the address of the friend (returned by getaddress of the friend
232
 *   you wish to add) it must be FRIEND_ADDRESS_SIZE bytes.
233
 *   TODO(irungentoo): add checksum.
234
 * @param data is the data.
235
 * @param length is the length.
236
 *
237
 * @return the friend number if success.
238
 * @retval FA_TOOLONG if message length is too long.
239
 * @retval FAERR_NOMESSAGE if no message (message length must be >= 1 byte).
240
 * @retval FAERR_OWNKEY if user's own key.
241
 * @retval FAERR_ALREADYSENT if friend request already sent or already a friend.
242
 * @retval FAERR_BADCHECKSUM if bad checksum in address.
243
 * @retval FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different.
244
 *   (the nospam for that friend was set to the new one).
245
 * @retval FAERR_NOMEM if increasing the friend list size fails.
246
 */
247
int32_t m_addfriend(Messenger *m, const uint8_t *address, const uint8_t *data, uint16_t length)
248
268
{
249
268
    if (length > MAX_FRIEND_REQUEST_DATA_SIZE) {
250
12
        return FAERR_TOOLONG;
251
12
    }
252
253
256
    uint8_t real_pk[CRYPTO_PUBLIC_KEY_SIZE];
254
256
    pk_copy(real_pk, address);
255
256
256
    if (!public_key_valid(real_pk)) {
257
10
        return FAERR_BADCHECKSUM;
258
10
    }
259
260
246
    uint16_t check;
261
246
    const uint16_t checksum = data_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum));
262
246
    memcpy(&check, address + CRYPTO_PUBLIC_KEY_SIZE + sizeof(uint32_t), sizeof(check));
263
264
246
    if (check != checksum) {
265
1
        return FAERR_BADCHECKSUM;
266
1
    }
267
268
245
    if (length < 1) {
269
11
        return FAERR_NOMESSAGE;
270
11
    }
271
272
234
    if (pk_equal(real_pk, nc_get_self_public_key(m->net_crypto))) {
273
11
        return FAERR_OWNKEY;
274
11
    }
275
276
223
    const int32_t friend_id = getfriend_id(m, real_pk);
277
278
223
    if (friend_id != -1) {
279
47
        if (m->friendlist[friend_id].status >= FRIEND_CONFIRMED) {
280
10
            return FAERR_ALREADYSENT;
281
10
        }
282
283
37
        uint32_t nospam;
284
37
        memcpy(&nospam, address + CRYPTO_PUBLIC_KEY_SIZE, sizeof(nospam));
285
286
37
        if (m->friendlist[friend_id].friendrequest_nospam == nospam) {
287
21
            return FAERR_ALREADYSENT;
288
21
        }
289
290
16
        m->friendlist[friend_id].friendrequest_nospam = nospam;
291
16
        return FAERR_SETNEWNOSPAM;
292
37
    }
293
294
176
    const int32_t ret = init_new_friend(m, real_pk, FRIEND_ADDED);
295
296
176
    if (ret < 0) {
297
0
        return ret;
298
0
    }
299
300
176
    m->friendlist[ret].friendrequest_timeout = FRIENDREQUEST_TIMEOUT;
301
176
    memcpy(m->friendlist[ret].info, data, length);
302
176
    m->friendlist[ret].info_size = length;
303
176
    memcpy(&m->friendlist[ret].friendrequest_nospam, address + CRYPTO_PUBLIC_KEY_SIZE, sizeof(uint32_t));
304
305
176
    return ret;
306
176
}
307
308
int32_t m_addfriend_norequest(Messenger *m, const uint8_t *real_pk)
309
1.75k
{
310
1.75k
    if (!public_key_valid(real_pk)) {
311
21
        return FAERR_BADCHECKSUM;
312
21
    }
313
314
1.73k
    if (pk_equal(real_pk, nc_get_self_public_key(m->net_crypto))) {
315
11
        return FAERR_OWNKEY;
316
11
    }
317
318
1.72k
    return m_add_friend_contact_norequest(m, real_pk);
319
1.73k
}
320
321
non_null()
322
static int clear_receipts(Messenger *m, int32_t friendnumber)
323
1.16k
{
324
1.16k
    if (!m_friend_exists(m, friendnumber)) {
325
0
        return -1;
326
0
    }
327
328
1.16k
    struct Receipts *receipts = m->friendlist[friendnumber].receipts_start;
329
330
58.4k
    while (receipts != nullptr) {
331
57.3k
        struct Receipts *temp_r = receipts->next;
332
57.3k
        mem_delete(m->mem, receipts);
333
57.3k
        receipts = temp_r;
334
57.3k
    }
335
336
1.16k
    m->friendlist[friendnumber].receipts_start = nullptr;
337
1.16k
    m->friendlist[friendnumber].receipts_end = nullptr;
338
1.16k
    return 0;
339
1.16k
}
340
341
non_null()
342
static int add_receipt(Messenger *m, int32_t friendnumber, uint32_t packet_num, uint32_t msg_id)
343
98.4k
{
344
98.4k
    if (!m_friend_exists(m, friendnumber)) {
345
0
        return -1;
346
0
    }
347
348
98.4k
    struct Receipts *new_receipts = (struct Receipts *)mem_alloc(m->mem, sizeof(struct Receipts));
349
350
98.4k
    if (new_receipts == nullptr) {
351
2
        return -1;
352
2
    }
353
354
98.4k
    new_receipts->packet_num = packet_num;
355
98.4k
    new_receipts->msg_id = msg_id;
356
357
98.4k
    if (m->friendlist[friendnumber].receipts_start == nullptr) {
358
127
        m->friendlist[friendnumber].receipts_start = new_receipts;
359
98.2k
    } else {
360
98.2k
        m->friendlist[friendnumber].receipts_end->next = new_receipts;
361
98.2k
    }
362
363
98.4k
    m->friendlist[friendnumber].receipts_end = new_receipts;
364
98.4k
    new_receipts->next = nullptr;
365
98.4k
    return 0;
366
98.4k
}
367
/**
368
 * return -1 on failure.
369
 * return 0 if packet was received.
370
 */
371
non_null()
372
static int friend_received_packet(const Messenger *m, int32_t friendnumber, uint32_t number)
373
135k
{
374
135k
    if (!m_friend_exists(m, friendnumber)) {
375
0
        return -1;
376
0
    }
377
378
135k
    return cryptpacket_received(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
379
135k
                                m->friendlist[friendnumber].friendcon_id), number);
380
135k
}
381
382
bool m_create_group_connection(Messenger *m, GC_Chat *chat)
383
154
{
384
154
    random_bytes(m->rng, chat->m_group_public_key, CRYPTO_PUBLIC_KEY_SIZE);
385
154
    const int friendcon_id = new_friend_connection(m->fr_c, chat->m_group_public_key);
386
387
154
    if (friendcon_id == -1) {
388
0
        return false;
389
0
    }
390
391
154
    const Friend_Conn *connection = get_conn(m->fr_c, friendcon_id);
392
393
154
    if (connection == nullptr) {
394
0
        return false;
395
0
    }
396
397
154
    chat->friend_connection_id = friendcon_id;
398
399
154
    if (friend_con_connected(m->fr_c, friendcon_id) == FRIENDCONN_STATUS_CONNECTED) {
400
0
        send_online_packet(m, friendcon_id);
401
0
    }
402
403
154
    const int onion_friend_number = friend_conn_get_onion_friendnum(connection);
404
154
    Onion_Friend *onion_friend = onion_get_friend(m->onion_c, (uint16_t)onion_friend_number);
405
406
154
    onion_friend_set_gc_public_key(onion_friend, get_chat_id(chat->chat_public_key));
407
154
    onion_friend_set_gc_data(onion_friend, nullptr, 0);
408
409
154
    return true;
410
154
}
411
412
/**
413
 * Kills the friend connection for a groupchat.
414
 */
415
void m_kill_group_connection(Messenger *m, const GC_Chat *chat)
416
159
{
417
159
    remove_request_received(m->fr, chat->m_group_public_key);
418
419
159
    friend_connection_callbacks(m->fr_c, chat->friend_connection_id, MESSENGER_CALLBACK_INDEX, nullptr,
420
159
                                nullptr, nullptr, nullptr, 0);
421
422
159
    if (friend_con_connected(m->fr_c, chat->friend_connection_id) == FRIENDCONN_STATUS_CONNECTED) {
423
0
        send_offline_packet(m, chat->friend_connection_id);
424
0
    }
425
426
159
    kill_friend_connection(m->fr_c, chat->friend_connection_id);
427
159
}
428
429
non_null(1) nullable(3)
430
static int do_receipts(Messenger *m, int32_t friendnumber, void *userdata)
431
91.1k
{
432
91.1k
    if (!m_friend_exists(m, friendnumber)) {
433
0
        return -1;
434
0
    }
435
436
91.1k
    struct Receipts *receipts = m->friendlist[friendnumber].receipts_start;
437
438
132k
    while (receipts != nullptr) {
439
41.5k
        if (friend_received_packet(m, friendnumber, receipts->packet_num) == -1) {
440
425
            break;
441
425
        }
442
443
41.1k
        if (m->read_receipt != nullptr) {
444
41.1k
            m->read_receipt(m, friendnumber, receipts->msg_id, userdata);
445
41.1k
        }
446
447
41.1k
        struct Receipts *r_next = receipts->next;
448
449
41.1k
        mem_delete(m->mem, receipts);
450
451
41.1k
        m->friendlist[friendnumber].receipts_start = r_next;
452
453
41.1k
        receipts = r_next;
454
41.1k
    }
455
456
91.1k
    if (m->friendlist[friendnumber].receipts_start == nullptr) {
457
90.7k
        m->friendlist[friendnumber].receipts_end = nullptr;
458
90.7k
    }
459
460
91.1k
    return 0;
461
91.1k
}
462
463
/** @brief Remove a friend.
464
 *
465
 * @retval 0 if success.
466
 * @retval -1 if failure.
467
 */
468
int m_delfriend(Messenger *m, int32_t friendnumber)
469
0
{
470
0
    if (!m_friend_exists(m, friendnumber)) {
471
0
        return -1;
472
0
    }
473
474
0
    if (m->friend_connectionstatuschange_internal != nullptr) {
475
0
        m->friend_connectionstatuschange_internal(m, friendnumber, 0, m->friend_connectionstatuschange_internal_userdata);
476
0
    }
477
478
0
    clear_receipts(m, friendnumber);
479
0
    remove_request_received(m->fr, m->friendlist[friendnumber].real_pk);
480
0
    friend_connection_callbacks(m->fr_c, m->friendlist[friendnumber].friendcon_id, MESSENGER_CALLBACK_INDEX, nullptr,
481
0
                                nullptr, nullptr, nullptr, 0);
482
483
0
    if (friend_con_connected(m->fr_c, m->friendlist[friendnumber].friendcon_id) == FRIENDCONN_STATUS_CONNECTED) {
484
0
        send_offline_packet(m, m->friendlist[friendnumber].friendcon_id);
485
0
    }
486
487
0
    kill_friend_connection(m->fr_c, m->friendlist[friendnumber].friendcon_id);
488
0
    m->friendlist[friendnumber] = empty_friend;
489
490
0
    uint32_t i;
491
492
0
    for (i = m->numfriends; i != 0; --i) {
493
0
        if (m->friendlist[i - 1].status != NOFRIEND) {
494
0
            break;
495
0
        }
496
0
    }
497
498
0
    m->numfriends = i;
499
500
0
    if (realloc_friendlist(m, m->numfriends) != 0) {
501
0
        return FAERR_NOMEM;
502
0
    }
503
504
0
    return 0;
505
0
}
506
507
int m_get_friend_connectionstatus(const Messenger *m, int32_t friendnumber)
508
124k
{
509
124k
    if (!m_friend_exists(m, friendnumber)) {
510
221
        return -1;
511
221
    }
512
513
123k
    if (m->friendlist[friendnumber].status != FRIEND_ONLINE) {
514
26.0k
        return CONNECTION_NONE;
515
26.0k
    }
516
517
97.7k
    bool direct_connected = false;
518
97.7k
    uint32_t num_online_relays = 0;
519
97.7k
    const int crypt_conn_id = friend_connection_crypt_connection_id(m->fr_c, m->friendlist[friendnumber].friendcon_id);
520
521
97.7k
    if (!crypto_connection_status(m->net_crypto, crypt_conn_id, &direct_connected, &num_online_relays)) {
522
0
        return CONNECTION_NONE;
523
0
    }
524
525
97.7k
    if (direct_connected) {
526
96.3k
        return CONNECTION_UDP;
527
96.3k
    }
528
529
1.44k
    if (num_online_relays != 0) {
530
798
        return CONNECTION_TCP;
531
798
    }
532
533
    /* if we have a valid friend connection but do not have an established connection
534
     * we leave the connection status unchanged until the friend connection is either
535
     * established or dropped.
536
     */
537
645
    return m->friendlist[friendnumber].last_connection_udp_tcp;
538
1.44k
}
539
540
/**
541
 * Checks if there exists a friend with given friendnumber.
542
 *
543
 * @param friendnumber The index in the friend list.
544
 *
545
 * @retval true if friend exists.
546
 * @retval false if friend doesn't exist.
547
 */
548
bool m_friend_exists(const Messenger *m, int32_t friendnumber)
549
739k
{
550
739k
    return (unsigned int)friendnumber < m->numfriends && m->friendlist[friendnumber].status != 0;
551
739k
}
552
553
/** @brief Send a message of type to an online friend.
554
 *
555
 * @retval -1 if friend not valid.
556
 * @retval -2 if too large.
557
 * @retval -3 if friend not online.
558
 * @retval -4 if send failed (because queue is full).
559
 * @retval -5 if bad type.
560
 * @retval 0 if success.
561
 *
562
 * The value in message_id will be passed to your read_receipt callback when the other receives the message.
563
 */
564
int m_send_message_generic(Messenger *m, int32_t friendnumber, uint8_t type, const uint8_t *message, uint32_t length,
565
                           uint32_t *message_id)
566
105k
{
567
105k
    if (type > MESSAGE_ACTION) {
568
0
        LOGGER_WARNING(m->log, "message type %d is invalid", type);
569
0
        return -5;
570
0
    }
571
572
105k
    if (!m_friend_exists(m, friendnumber)) {
573
0
        LOGGER_WARNING(m->log, "friend number %d is invalid", friendnumber);
574
0
        return -1;
575
0
    }
576
577
105k
    if (length >= MAX_CRYPTO_DATA_SIZE) {
578
121
        LOGGER_WARNING(m->log, "message length %u is too large", length);
579
121
        return -2;
580
121
    }
581
582
105k
    if (m->friendlist[friendnumber].status != FRIEND_ONLINE) {
583
0
        LOGGER_WARNING(m->log, "friend %d is not online", friendnumber);
584
0
        return -3;
585
0
    }
586
587
105k
    VLA(uint8_t, packet, length + 1);
588
105k
    packet[0] = PACKET_ID_MESSAGE + type;
589
590
105k
    assert(message != nullptr);
591
105k
    memcpy(packet + 1, message, length);
592
593
105k
    const int64_t packet_num = write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
594
105k
                                           m->friendlist[friendnumber].friendcon_id), packet, length + 1, false);
595
596
105k
    if (packet_num == -1) {
597
7.24k
        return -4;
598
7.24k
    }
599
600
98.4k
    const uint32_t msg_id = ++m->friendlist[friendnumber].message_id;
601
602
98.4k
    add_receipt(m, friendnumber, packet_num, msg_id);
603
604
98.4k
    if (message_id != nullptr) {
605
98.4k
        *message_id = msg_id;
606
98.4k
    }
607
608
98.4k
    return 0;
609
105k
}
610
611
non_null()
612
static bool write_cryptpacket_id(const Messenger *m, int32_t friendnumber, uint8_t packet_id, const uint8_t *data,
613
                                 uint32_t length, bool congestion_control)
614
5.89k
{
615
5.89k
    if (!m_friend_exists(m, friendnumber)) {
616
0
        return false;
617
0
    }
618
619
5.89k
    if (length >= MAX_CRYPTO_DATA_SIZE || m->friendlist[friendnumber].status != FRIEND_ONLINE) {
620
0
        return false;
621
0
    }
622
623
5.89k
    VLA(uint8_t, packet, length + 1);
624
5.89k
    packet[0] = packet_id;
625
626
5.89k
    assert(data != nullptr);
627
5.89k
    memcpy(packet + 1, data, length);
628
629
5.89k
    return write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
630
5.89k
                             m->friendlist[friendnumber].friendcon_id), packet, length + 1, congestion_control) != -1;
631
5.89k
}
632
633
/** @brief Send a name packet to friendnumber.
634
 * length is the length with the NULL terminator.
635
 */
636
non_null()
637
static bool m_sendname(const Messenger *m, int32_t friendnumber, const uint8_t *name, uint16_t length)
638
1.37k
{
639
1.37k
    if (length > MAX_NAME_LENGTH) {
640
0
        return false;
641
0
    }
642
643
1.37k
    return write_cryptpacket_id(m, friendnumber, PACKET_ID_NICKNAME, name, length, false);
644
1.37k
}
645
646
/** @brief Set the name and name_length of a friend.
647
 *
648
 * name must be a string of maximum MAX_NAME_LENGTH length.
649
 * length must be at least 1 byte.
650
 * length is the length of name with the NULL terminator.
651
 *
652
 * @retval 0 if success.
653
 * @retval -1 if failure.
654
 */
655
int setfriendname(Messenger *m, int32_t friendnumber, const uint8_t *name, uint16_t length)
656
185
{
657
185
    if (!m_friend_exists(m, friendnumber)) {
658
0
        return -1;
659
0
    }
660
661
185
    if (length > MAX_NAME_LENGTH || length == 0) {
662
134
        return -1;
663
134
    }
664
665
51
    m->friendlist[friendnumber].name_length = length;
666
51
    memcpy(m->friendlist[friendnumber].name, name, length);
667
51
    return 0;
668
185
}
669
670
/** @brief Set our nickname.
671
 *
672
 * name must be a string of maximum MAX_NAME_LENGTH length.
673
 * length must be at least 1 byte.
674
 * length is the length of name with the NULL terminator.
675
 *
676
 * @retval 0 if success.
677
 * @retval -1 if failure.
678
 */
679
int setname(Messenger *m, const uint8_t *name, uint16_t length)
680
349
{
681
349
    if (length > MAX_NAME_LENGTH) {
682
0
        return -1;
683
0
    }
684
685
349
    if (m->name_length == length && (length == 0 || memcmp(name, m->name, length) == 0)) {
686
69
        return 0;
687
69
    }
688
689
280
    if (length > 0) {
690
280
        memcpy(m->name, name, length);
691
280
    }
692
693
280
    m->name_length = length;
694
695
565
    for (uint32_t i = 0; i < m->numfriends; ++i) {
696
285
        m->friendlist[i].name_sent = false;
697
285
    }
698
699
280
    return 0;
700
349
}
701
702
/**
703
 * @brief Get your nickname.
704
 *
705
 * m - The messenger context to use.
706
 * name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes.
707
 *
708
 * @return length of the name.
709
 * @retval 0 on error.
710
 */
711
uint16_t getself_name(const Messenger *m, uint8_t *name)
712
6
{
713
6
    if (name == nullptr) {
714
0
        return 0;
715
0
    }
716
717
6
    memcpy(name, m->name, m->name_length);
718
719
6
    return m->name_length;
720
6
}
721
722
/** @brief Get name of friendnumber and put it in name.
723
 *
724
 * name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes.
725
 *
726
 * @return length of name if success.
727
 * @retval -1 if failure.
728
 */
729
int getname(const Messenger *m, int32_t friendnumber, uint8_t *name)
730
2
{
731
2
    if (!m_friend_exists(m, friendnumber)) {
732
0
        return -1;
733
0
    }
734
735
2
    memcpy(name, m->friendlist[friendnumber].name, m->friendlist[friendnumber].name_length);
736
2
    return m->friendlist[friendnumber].name_length;
737
2
}
738
739
int m_get_name_size(const Messenger *m, int32_t friendnumber)
740
2
{
741
2
    if (!m_friend_exists(m, friendnumber)) {
742
0
        return -1;
743
0
    }
744
745
2
    return m->friendlist[friendnumber].name_length;
746
2
}
747
748
int m_get_self_name_size(const Messenger *m)
749
9
{
750
9
    return m->name_length;
751
9
}
752
753
int m_set_statusmessage(Messenger *m, const uint8_t *status, uint16_t length)
754
596
{
755
596
    if (length > MAX_STATUSMESSAGE_LENGTH) {
756
0
        return -1;
757
0
    }
758
759
596
    if (m->statusmessage_length == length && (length == 0 || memcmp(m->statusmessage, status, length) == 0)) {
760
123
        return 0;
761
123
    }
762
763
473
    if (length > 0) {
764
473
        memcpy(m->statusmessage, status, length);
765
473
    }
766
767
473
    m->statusmessage_length = length;
768
769
718
    for (uint32_t i = 0; i < m->numfriends; ++i) {
770
245
        m->friendlist[i].statusmessage_sent = false;
771
245
    }
772
773
473
    return 0;
774
596
}
775
776
non_null()
777
static bool userstatus_from_int(uint8_t status, Userstatus *out)
778
2.88k
{
779
2.88k
    switch (status) {
780
2.73k
        case USERSTATUS_NONE: {
781
2.73k
            *out = USERSTATUS_NONE;
782
2.73k
            return true;
783
0
        }
784
785
7
        case USERSTATUS_AWAY: {
786
7
            *out = USERSTATUS_AWAY;
787
7
            return true;
788
0
        }
789
790
68
        case USERSTATUS_BUSY: {
791
68
            *out = USERSTATUS_BUSY;
792
68
            return true;
793
0
        }
794
795
1
        case USERSTATUS_INVALID: {
796
1
            *out = USERSTATUS_INVALID;
797
1
            return true;
798
0
        }
799
800
79
        default: {
801
79
            *out = USERSTATUS_INVALID;
802
79
            return false;
803
0
        }
804
2.88k
    }
805
2.88k
}
806
807
int m_set_userstatus(Messenger *m, uint8_t status)
808
525
{
809
525
    if (status >= USERSTATUS_INVALID) {
810
266
        return -1;
811
266
    }
812
813
259
    if (m->userstatus == status) {
814
121
        return 0;
815
121
    }
816
817
138
    userstatus_from_int(status, &m->userstatus);
818
819
204
    for (uint32_t i = 0; i < m->numfriends; ++i) {
820
66
        m->friendlist[i].userstatus_sent = false;
821
66
    }
822
823
138
    return 0;
824
259
}
825
826
/**
827
 * Guaranteed to be at most MAX_STATUSMESSAGE_LENGTH.
828
 *
829
 * @return the length of friendnumber's status message, including null on success.
830
 * @retval -1 on failure.
831
 */
832
int m_get_statusmessage_size(const Messenger *m, int32_t friendnumber)
833
4
{
834
4
    if (!m_friend_exists(m, friendnumber)) {
835
0
        return -1;
836
0
    }
837
838
4
    return m->friendlist[friendnumber].statusmessage_length;
839
4
}
840
841
/** @brief Copy friendnumber's status message into buf, truncating if size is over maxlen.
842
 *
843
 * Get the size you need to allocate from m_get_statusmessage_size.
844
 * The self variant will copy our own status message.
845
 *
846
 * @return the length of the copied data on success
847
 * @retval -1 on failure.
848
 */
849
int m_copy_statusmessage(const Messenger *m, int32_t friendnumber, uint8_t *buf, uint32_t maxlen)
850
2
{
851
2
    if (!m_friend_exists(m, friendnumber)) {
852
0
        return -1;
853
0
    }
854
855
    // TODO(iphydf): This should be uint16_t and min_u16. If maxlen exceeds
856
    // uint16_t's range, it won't affect the result.
857
2
    const uint32_t msglen = min_u32(maxlen, m->friendlist[friendnumber].statusmessage_length);
858
859
2
    memcpy(buf, m->friendlist[friendnumber].statusmessage, msglen);
860
2
    memzero(buf + msglen, maxlen - msglen);
861
2
    return msglen;
862
2
}
863
864
/** @return the size of friendnumber's user status.
865
 * Guaranteed to be at most MAX_STATUSMESSAGE_LENGTH.
866
 */
867
int m_get_self_statusmessage_size(const Messenger *m)
868
4
{
869
4
    return m->statusmessage_length;
870
4
}
871
872
int m_copy_self_statusmessage(const Messenger *m, uint8_t *buf)
873
3
{
874
3
    memcpy(buf, m->statusmessage, m->statusmessage_length);
875
3
    return m->statusmessage_length;
876
3
}
877
878
uint8_t m_get_userstatus(const Messenger *m, int32_t friendnumber)
879
0
{
880
0
    if (!m_friend_exists(m, friendnumber)) {
881
0
        return USERSTATUS_INVALID;
882
0
    }
883
884
0
    uint8_t status = m->friendlist[friendnumber].userstatus;
885
886
0
    if (status >= USERSTATUS_INVALID) {
887
0
        status = USERSTATUS_NONE;
888
0
    }
889
890
0
    return status;
891
0
}
892
893
uint8_t m_get_self_userstatus(const Messenger *m)
894
0
{
895
0
    return m->userstatus;
896
0
}
897
898
uint64_t m_get_last_online(const Messenger *m, int32_t friendnumber)
899
0
{
900
0
    if (!m_friend_exists(m, friendnumber)) {
901
0
        return UINT64_MAX;
902
0
    }
903
904
0
    return m->friendlist[friendnumber].last_seen_time;
905
0
}
906
907
int m_set_usertyping(Messenger *m, int32_t friendnumber, bool is_typing)
908
20
{
909
20
    if (!m_friend_exists(m, friendnumber)) {
910
0
        return -1;
911
0
    }
912
913
20
    if (m->friendlist[friendnumber].user_istyping == is_typing) {
914
0
        return 0;
915
0
    }
916
917
20
    m->friendlist[friendnumber].user_istyping = is_typing;
918
20
    m->friendlist[friendnumber].user_istyping_sent = false;
919
920
20
    return 0;
921
20
}
922
923
int m_get_istyping(const Messenger *m, int32_t friendnumber)
924
10
{
925
10
    if (!m_friend_exists(m, friendnumber)) {
926
0
        return -1;
927
0
    }
928
929
10
    return m->friendlist[friendnumber].is_typing ? 1 : 0;
930
10
}
931
932
non_null()
933
static bool send_statusmessage(const Messenger *m, int32_t friendnumber, const uint8_t *status, uint16_t length)
934
1.34k
{
935
1.34k
    return write_cryptpacket_id(m, friendnumber, PACKET_ID_STATUSMESSAGE, status, length, false);
936
1.34k
}
937
938
non_null()
939
static bool send_userstatus(const Messenger *m, int32_t friendnumber, uint8_t status)
940
1.34k
{
941
1.34k
    return write_cryptpacket_id(m, friendnumber, PACKET_ID_USERSTATUS, &status, sizeof(status), false);
942
1.34k
}
943
944
non_null()
945
static bool send_user_istyping(const Messenger *m, int32_t friendnumber, bool is_typing)
946
1.36k
{
947
1.36k
    const uint8_t typing = is_typing ? 1 : 0;
948
1.36k
    return write_cryptpacket_id(m, friendnumber, PACKET_ID_TYPING, &typing, sizeof(typing), false);
949
1.36k
}
950
951
non_null()
952
static int set_friend_statusmessage(const Messenger *m, int32_t friendnumber, const uint8_t *status, uint16_t length)
953
1.46k
{
954
1.46k
    if (!m_friend_exists(m, friendnumber)) {
955
0
        return -1;
956
0
    }
957
958
1.46k
    if (length > MAX_STATUSMESSAGE_LENGTH) {
959
74
        return -1;
960
74
    }
961
962
1.39k
    if (length > 0) {
963
47
        memcpy(m->friendlist[friendnumber].statusmessage, status, length);
964
47
    }
965
966
1.39k
    m->friendlist[friendnumber].statusmessage_length = length;
967
1.39k
    return 0;
968
1.46k
}
969
970
non_null()
971
static void set_friend_userstatus(const Messenger *m, int32_t friendnumber, uint8_t status)
972
1.46k
{
973
1.46k
    userstatus_from_int(status, &m->friendlist[friendnumber].userstatus);
974
1.46k
}
975
976
non_null()
977
static void set_friend_typing(const Messenger *m, int32_t friendnumber, bool is_typing)
978
1.28k
{
979
1.28k
    m->friendlist[friendnumber].is_typing = is_typing;
980
1.28k
}
981
982
/** Set the function that will be executed when a friend request is received. */
983
void m_callback_friendrequest(Messenger *m, m_friend_request_cb *function)
984
3.34k
{
985
3.34k
    m->friend_request = function;
986
3.34k
}
987
988
/** Set the function that will be executed when a message from a friend is received. */
989
void m_callback_friendmessage(Messenger *m, m_friend_message_cb *function)
990
3.34k
{
991
3.34k
    m->friend_message = function;
992
3.34k
}
993
994
void m_callback_namechange(Messenger *m, m_friend_name_cb *function)
995
3.34k
{
996
3.34k
    m->friend_namechange = function;
997
3.34k
}
998
999
void m_callback_statusmessage(Messenger *m, m_friend_status_message_cb *function)
1000
3.34k
{
1001
3.34k
    m->friend_statusmessagechange = function;
1002
3.34k
}
1003
1004
void m_callback_userstatus(Messenger *m, m_friend_status_cb *function)
1005
3.34k
{
1006
3.34k
    m->friend_userstatuschange = function;
1007
3.34k
}
1008
1009
void m_callback_typingchange(Messenger *m, m_friend_typing_cb *function)
1010
3.34k
{
1011
3.34k
    m->friend_typingchange = function;
1012
3.34k
}
1013
1014
void m_callback_read_receipt(Messenger *m, m_friend_read_receipt_cb *function)
1015
3.34k
{
1016
3.34k
    m->read_receipt = function;
1017
3.34k
}
1018
1019
void m_callback_connectionstatus(Messenger *m, m_friend_connection_status_cb *function)
1020
3.34k
{
1021
3.34k
    m->friend_connectionstatuschange = function;
1022
3.34k
}
1023
1024
void m_callback_core_connection(Messenger *m, m_self_connection_status_cb *function)
1025
3.34k
{
1026
3.34k
    m->core_connection_change = function;
1027
3.34k
}
1028
1029
void m_callback_connectionstatus_internal_av(Messenger *m, m_friend_connectionstatuschange_internal_cb *function,
1030
        void *userdata)
1031
6
{
1032
6
    m->friend_connectionstatuschange_internal = function;
1033
6
    m->friend_connectionstatuschange_internal_userdata = userdata;
1034
6
}
1035
1036
non_null(1) nullable(3)
1037
static void check_friend_tcp_udp(Messenger *m, int32_t friendnumber, void *userdata)
1038
92.5k
{
1039
92.5k
    const int last_connection_udp_tcp = m->friendlist[friendnumber].last_connection_udp_tcp;
1040
1041
92.5k
    const int ret = m_get_friend_connectionstatus(m, friendnumber);
1042
1043
92.5k
    if (ret == -1) {
1044
0
        return;
1045
0
    }
1046
1047
92.5k
    if (last_connection_udp_tcp != ret) {
1048
1.38k
        if (m->friend_connectionstatuschange != nullptr) {
1049
1.38k
            m->friend_connectionstatuschange(m, friendnumber, ret, userdata);
1050
1.38k
        }
1051
1.38k
    }
1052
1053
92.5k
    m->friendlist[friendnumber].last_connection_udp_tcp = (Connection_Status)ret;
1054
92.5k
}
1055
1056
non_null()
1057
static void break_files(const Messenger *m, int32_t friendnumber);
1058
1059
non_null(1) nullable(4)
1060
static void check_friend_connectionstatus(Messenger *m, int32_t friendnumber, uint8_t status, void *userdata)
1061
1.52k
{
1062
1.52k
    if (status == NOFRIEND) {
1063
0
        return;
1064
0
    }
1065
1066
1.52k
    const bool was_online = m->friendlist[friendnumber].status == FRIEND_ONLINE;
1067
1.52k
    const bool is_online = status == FRIEND_ONLINE;
1068
1069
1.52k
    if (is_online != was_online) {
1070
1.38k
        if (was_online) {
1071
38
            break_files(m, friendnumber);
1072
38
            clear_receipts(m, friendnumber);
1073
1.34k
        } else {
1074
1.34k
            m->friendlist[friendnumber].name_sent = false;
1075
1.34k
            m->friendlist[friendnumber].userstatus_sent = false;
1076
1.34k
            m->friendlist[friendnumber].statusmessage_sent = false;
1077
1.34k
            m->friendlist[friendnumber].user_istyping_sent = false;
1078
1.34k
        }
1079
1080
1.38k
        m->friendlist[friendnumber].status = status;
1081
1082
1.38k
        check_friend_tcp_udp(m, friendnumber, userdata);
1083
1084
1.38k
        if (m->friend_connectionstatuschange_internal != nullptr) {
1085
0
            m->friend_connectionstatuschange_internal(m, friendnumber, is_online,
1086
0
                    m->friend_connectionstatuschange_internal_userdata);
1087
0
        }
1088
1.38k
    }
1089
1.52k
}
1090
1091
non_null(1) nullable(4)
1092
static void set_friend_status(Messenger *m, int32_t friendnumber, uint8_t status, void *userdata)
1093
1.52k
{
1094
1.52k
    check_friend_connectionstatus(m, friendnumber, status, userdata);
1095
1.52k
    m->friendlist[friendnumber].status = status;
1096
1.52k
}
1097
1098
/*** CONFERENCES */
1099
1100
1101
/** @brief Set the callback for conference invites. */
1102
void m_callback_conference_invite(Messenger *m, m_conference_invite_cb *function)
1103
6.24k
{
1104
6.24k
    m->conference_invite = function;
1105
6.24k
}
1106
1107
/** @brief the callback for group invites. */
1108
void m_callback_group_invite(Messenger *m, m_group_invite_cb *function)
1109
3.34k
{
1110
3.34k
    m->group_invite = function;
1111
3.34k
}
1112
1113
/** @brief Send a conference invite packet.
1114
 *
1115
 * return true on success
1116
 * return false on failure
1117
 */
1118
bool send_conference_invite_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint16_t length)
1119
132
{
1120
132
    return write_cryptpacket_id(m, friendnumber, PACKET_ID_INVITE_CONFERENCE, data, length, false);
1121
132
}
1122
1123
1124
/** @brief Send a group invite packet.
1125
 *
1126
 * @retval true if success
1127
 */
1128
bool send_group_invite_packet(const Messenger *m, uint32_t friendnumber, const uint8_t *packet, uint16_t length)
1129
276
{
1130
276
    return write_cryptpacket_id(m, friendnumber, PACKET_ID_INVITE_GROUPCHAT, packet, length, false);
1131
276
}
1132
1133
1134
/*** FILE SENDING */
1135
1136
1137
/** @brief Set the callback for file send requests. */
1138
void callback_file_sendrequest(Messenger *m, m_file_recv_cb *function)
1139
3.34k
{
1140
3.34k
    m->file_sendrequest = function;
1141
3.34k
}
1142
1143
/** @brief Set the callback for file control requests. */
1144
void callback_file_control(Messenger *m, m_file_recv_control_cb *function)
1145
3.34k
{
1146
3.34k
    m->file_filecontrol = function;
1147
3.34k
}
1148
1149
/** @brief Set the callback for file data. */
1150
void callback_file_data(Messenger *m, m_file_recv_chunk_cb *function)
1151
3.34k
{
1152
3.34k
    m->file_filedata = function;
1153
3.34k
}
1154
1155
/** @brief Set the callback for file request chunk. */
1156
void callback_file_reqchunk(Messenger *m, m_file_chunk_request_cb *function)
1157
3.34k
{
1158
3.34k
    m->file_reqchunk = function;
1159
3.34k
}
1160
1161
9
#define MAX_FILENAME_LENGTH 255
1162
1163
/** @brief Copy the file transfer file id to file_id
1164
 *
1165
 * @retval 0 on success.
1166
 * @retval -1 if friend not valid.
1167
 * @retval -2 if filenumber not valid
1168
 */
1169
int file_get_id(const Messenger *m, int32_t friendnumber, uint32_t filenumber, uint8_t *file_id)
1170
12
{
1171
12
    if (!m_friend_exists(m, friendnumber)) {
1172
3
        return -1;
1173
3
    }
1174
1175
9
    if (m->friendlist[friendnumber].status != FRIEND_ONLINE) {
1176
0
        return -2;
1177
0
    }
1178
1179
9
    uint32_t temp_filenum;
1180
9
    bool inbound;
1181
9
    uint8_t file_number;
1182
1183
9
    if (filenumber >= (1 << 16)) {
1184
3
        inbound = true;
1185
3
        temp_filenum = (filenumber >> 16) - 1;
1186
6
    } else {
1187
6
        inbound = false;
1188
6
        temp_filenum = filenumber;
1189
6
    }
1190
1191
9
    if (temp_filenum >= MAX_CONCURRENT_FILE_PIPES) {
1192
0
        return -2;
1193
0
    }
1194
1195
9
    file_number = temp_filenum;
1196
1197
9
    const struct File_Transfers *const ft = inbound
1198
9
        ? &m->friendlist[friendnumber].file_receiving[file_number]
1199
9
        : &m->friendlist[friendnumber].file_sending[file_number];
1200
1201
9
    if (ft->status == FILESTATUS_NONE) {
1202
3
        return -2;
1203
3
    }
1204
1205
6
    memcpy(file_id, ft->id, FILE_ID_LENGTH);
1206
6
    return 0;
1207
9
}
1208
1209
/** @brief Send a file send request.
1210
 * Maximum filename length is 255 bytes.
1211
 * @retval 1 on success
1212
 * @retval 0 on failure
1213
 */
1214
non_null()
1215
static bool file_sendrequest(const Messenger *m, int32_t friendnumber, uint8_t filenumber, uint32_t file_type,
1216
                             uint64_t filesize, const uint8_t *file_id, const uint8_t *filename, uint16_t filename_length)
1217
3
{
1218
3
    if (!m_friend_exists(m, friendnumber)) {
1219
0
        return false;
1220
0
    }
1221
1222
3
    if (filename_length > MAX_FILENAME_LENGTH) {
1223
0
        return false;
1224
0
    }
1225
1226
3
    const uint16_t packet_size = 1 + sizeof(file_type) + sizeof(filesize) + FILE_ID_LENGTH + filename_length;
1227
3
    VLA(uint8_t, packet, packet_size);
1228
3
    packet[0] = filenumber;
1229
3
    file_type = net_htonl(file_type);
1230
3
    memcpy(packet + 1, &file_type, sizeof(file_type));
1231
3
    net_pack_u64(packet + 1 + sizeof(file_type), filesize);
1232
3
    memcpy(packet + 1 + sizeof(file_type) + sizeof(filesize), file_id, FILE_ID_LENGTH);
1233
1234
3
    if (filename_length > 0) {
1235
3
        memcpy(packet + 1 + sizeof(file_type) + sizeof(filesize) + FILE_ID_LENGTH, filename, filename_length);
1236
3
    }
1237
1238
3
    return write_cryptpacket_id(m, friendnumber, PACKET_ID_FILE_SENDREQUEST, packet, packet_size, false);
1239
3
}
1240
1241
/** @brief Send a file send request.
1242
 *
1243
 * Maximum filename length is 255 bytes.
1244
 *
1245
 * @return file number on success
1246
 * @retval -1 if friend not found.
1247
 * @retval -2 if filename length invalid.
1248
 * @retval -3 if no more file sending slots left.
1249
 * @retval -4 if could not send packet (friend offline).
1250
 */
1251
long int new_filesender(const Messenger *m, int32_t friendnumber, uint32_t file_type, uint64_t filesize,
1252
                        const uint8_t *file_id, const uint8_t *filename, uint16_t filename_length)
1253
3
{
1254
3
    if (!m_friend_exists(m, friendnumber)) {
1255
0
        return -1;
1256
0
    }
1257
1258
3
    if (filename_length > MAX_FILENAME_LENGTH) {
1259
0
        return -2;
1260
0
    }
1261
1262
3
    uint32_t i;
1263
1264
3
    for (i = 0; i < MAX_CONCURRENT_FILE_PIPES; ++i) {
1265
3
        if (m->friendlist[friendnumber].file_sending[i].status == FILESTATUS_NONE) {
1266
3
            break;
1267
3
        }
1268
3
    }
1269
1270
3
    if (i == MAX_CONCURRENT_FILE_PIPES) {
1271
0
        return -3;
1272
0
    }
1273
1274
3
    if (!file_sendrequest(m, friendnumber, i, file_type, filesize, file_id, filename, filename_length)) {
1275
0
        return -4;
1276
0
    }
1277
1278
3
    struct File_Transfers *ft = &m->friendlist[friendnumber].file_sending[i];
1279
1280
3
    ft->status = FILESTATUS_NOT_ACCEPTED;
1281
1282
3
    ft->size = filesize;
1283
1284
3
    ft->transferred = 0;
1285
1286
3
    ft->requested = 0;
1287
1288
3
    ft->paused = FILE_PAUSE_NOT;
1289
1290
3
    memcpy(ft->id, file_id, FILE_ID_LENGTH);
1291
1292
3
    return i;
1293
3
}
1294
1295
non_null(1) nullable(6)
1296
static bool send_file_control_packet(const Messenger *m, int32_t friendnumber, bool inbound, uint8_t filenumber,
1297
                                     uint8_t control_type, const uint8_t *data, uint16_t data_length)
1298
5
{
1299
5
    assert(data_length == 0 || data != nullptr);
1300
1301
5
    if ((unsigned int)(1 + 3 + data_length) > MAX_CRYPTO_DATA_SIZE) {
1302
0
        return false;
1303
0
    }
1304
1305
5
    const uint16_t packet_size = 3 + data_length;
1306
5
    VLA(uint8_t, packet, packet_size);
1307
1308
5
    packet[0] = inbound ? 1 : 0;
1309
5
    packet[1] = filenumber;
1310
5
    packet[2] = control_type;
1311
1312
5
    if (data_length > 0) {
1313
2
        memcpy(packet + 3, data, data_length);
1314
2
    }
1315
1316
5
    return write_cryptpacket_id(m, friendnumber, PACKET_ID_FILE_CONTROL, packet, packet_size, false);
1317
5
}
1318
1319
/** @brief Send a file control request.
1320
 *
1321
 * @retval 0 on success
1322
 * @retval -1 if friend not valid.
1323
 * @retval -2 if friend not online.
1324
 * @retval -3 if file number invalid.
1325
 * @retval -4 if file control is bad.
1326
 * @retval -5 if file already paused.
1327
 * @retval -6 if resume file failed because it was only paused by the other.
1328
 * @retval -7 if resume file failed because it wasn't paused.
1329
 * @retval -8 if packet failed to send.
1330
 */
1331
int file_control(const Messenger *m, int32_t friendnumber, uint32_t filenumber, unsigned int control)
1332
3
{
1333
3
    if (!m_friend_exists(m, friendnumber)) {
1334
0
        return -1;
1335
0
    }
1336
1337
3
    if (m->friendlist[friendnumber].status != FRIEND_ONLINE) {
1338
0
        return -2;
1339
0
    }
1340
1341
3
    uint32_t temp_filenum;
1342
3
    bool inbound;
1343
3
    uint8_t file_number;
1344
1345
3
    if (filenumber >= (1 << 16)) {
1346
3
        inbound = true;
1347
3
        temp_filenum = (filenumber >> 16) - 1;
1348
3
    } else {
1349
0
        inbound = false;
1350
0
        temp_filenum = filenumber;
1351
0
    }
1352
1353
3
    if (temp_filenum >= MAX_CONCURRENT_FILE_PIPES) {
1354
0
        return -3;
1355
0
    }
1356
1357
3
    file_number = temp_filenum;
1358
1359
3
    struct File_Transfers *ft;
1360
1361
3
    if (inbound) {
1362
3
        ft = &m->friendlist[friendnumber].file_receiving[file_number];
1363
3
    } else {
1364
0
        ft = &m->friendlist[friendnumber].file_sending[file_number];
1365
0
    }
1366
1367
3
    if (ft->status == FILESTATUS_NONE) {
1368
0
        return -3;
1369
0
    }
1370
1371
3
    if (control > FILECONTROL_KILL) {
1372
0
        return -4;
1373
0
    }
1374
1375
3
    if (control == FILECONTROL_PAUSE && ((ft->paused & FILE_PAUSE_US) != 0 || ft->status != FILESTATUS_TRANSFERRING)) {
1376
0
        return -5;
1377
0
    }
1378
1379
3
    if (control == FILECONTROL_ACCEPT) {
1380
3
        if (ft->status == FILESTATUS_TRANSFERRING) {
1381
0
            if ((ft->paused & FILE_PAUSE_US) == 0) {
1382
0
                if ((ft->paused & FILE_PAUSE_OTHER) != 0) {
1383
0
                    return -6;
1384
0
                }
1385
1386
0
                return -7;
1387
0
            }
1388
3
        } else {
1389
3
            if (ft->status != FILESTATUS_NOT_ACCEPTED) {
1390
0
                return -7;
1391
0
            }
1392
1393
3
            if (!inbound) {
1394
0
                return -6;
1395
0
            }
1396
3
        }
1397
3
    }
1398
1399
3
    if (send_file_control_packet(m, friendnumber, inbound, file_number, control, nullptr, 0)) {
1400
3
        switch (control) {
1401
0
            case FILECONTROL_KILL: {
1402
0
                if (!inbound && (ft->status == FILESTATUS_TRANSFERRING || ft->status == FILESTATUS_FINISHED)) {
1403
                    // We are actively sending that file, remove from list
1404
0
                    --m->friendlist[friendnumber].num_sending_files;
1405
0
                }
1406
1407
0
                ft->status = FILESTATUS_NONE;
1408
0
                break;
1409
0
            }
1410
0
            case FILECONTROL_PAUSE: {
1411
0
                ft->paused |= FILE_PAUSE_US;
1412
0
                break;
1413
0
            }
1414
3
            case FILECONTROL_ACCEPT: {
1415
3
                ft->status = FILESTATUS_TRANSFERRING;
1416
1417
3
                if ((ft->paused & FILE_PAUSE_US) != 0) {
1418
0
                    ft->paused ^= FILE_PAUSE_US;
1419
0
                }
1420
3
                break;
1421
0
            }
1422
3
        }
1423
3
    } else {
1424
0
        return -8;
1425
0
    }
1426
1427
3
    return 0;
1428
3
}
1429
1430
/** @brief Send a seek file control request.
1431
 *
1432
 * @retval 0 on success
1433
 * @retval -1 if friend not valid.
1434
 * @retval -2 if friend not online.
1435
 * @retval -3 if file number invalid.
1436
 * @retval -4 if not receiving file.
1437
 * @retval -5 if file status wrong.
1438
 * @retval -6 if position bad.
1439
 * @retval -8 if packet failed to send.
1440
 */
1441
int file_seek(const Messenger *m, int32_t friendnumber, uint32_t filenumber, uint64_t position)
1442
5
{
1443
5
    if (!m_friend_exists(m, friendnumber)) {
1444
0
        return -1;
1445
0
    }
1446
1447
5
    if (m->friendlist[friendnumber].status != FRIEND_ONLINE) {
1448
0
        return -2;
1449
0
    }
1450
1451
5
    if (filenumber < (1 << 16)) {
1452
        // Not receiving.
1453
0
        return -4;
1454
0
    }
1455
1456
5
    const uint32_t temp_filenum = (filenumber >> 16) - 1;
1457
1458
5
    if (temp_filenum >= MAX_CONCURRENT_FILE_PIPES) {
1459
0
        return -3;
1460
0
    }
1461
1462
5
    assert(temp_filenum <= UINT8_MAX);
1463
5
    const uint8_t file_number = temp_filenum;
1464
1465
    // We're always receiving at this point.
1466
5
    struct File_Transfers *ft = &m->friendlist[friendnumber].file_receiving[file_number];
1467
1468
5
    if (ft->status == FILESTATUS_NONE) {
1469
0
        return -3;
1470
0
    }
1471
1472
5
    if (ft->status != FILESTATUS_NOT_ACCEPTED) {
1473
3
        return -5;
1474
3
    }
1475
1476
2
    if (position >= ft->size) {
1477
0
        return -6;
1478
0
    }
1479
1480
2
    uint8_t sending_pos[sizeof(uint64_t)];
1481
2
    net_pack_u64(sending_pos, position);
1482
1483
2
    if (send_file_control_packet(m, friendnumber, true, file_number, FILECONTROL_SEEK, sending_pos,
1484
2
                                 sizeof(sending_pos))) {
1485
2
        ft->transferred = position;
1486
2
    } else {
1487
0
        return -8;
1488
0
    }
1489
1490
2
    return 0;
1491
2
}
1492
1493
/** @return packet number on success.
1494
 * @retval -1 on failure.
1495
 */
1496
non_null(1) nullable(4)
1497
static int64_t send_file_data_packet(const Messenger *m, int32_t friendnumber, uint8_t filenumber, const uint8_t *data,
1498
                                     uint16_t length)
1499
76.5k
{
1500
76.5k
    assert(length == 0 || data != nullptr);
1501
1502
76.5k
    if (!m_friend_exists(m, friendnumber)) {
1503
0
        return -1;
1504
0
    }
1505
1506
76.5k
    const uint16_t packet_size = 2 + length;
1507
76.5k
    VLA(uint8_t, packet, packet_size);
1508
76.5k
    packet[0] = PACKET_ID_FILE_DATA;
1509
76.5k
    packet[1] = filenumber;
1510
1511
76.5k
    if (length > 0) {
1512
76.5k
        memcpy(packet + 2, data, length);
1513
76.5k
    }
1514
1515
76.5k
    return write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
1516
76.5k
                             m->friendlist[friendnumber].friendcon_id), packet, packet_size, true);
1517
76.5k
}
1518
1519
535k
#define MAX_FILE_DATA_SIZE (MAX_CRYPTO_DATA_SIZE - 2)
1520
84.8k
#define MIN_SLOTS_FREE (CRYPTO_MIN_QUEUE_LENGTH / 4)
1521
/** @brief Send file data.
1522
 *
1523
 * @retval 0 on success
1524
 * @retval -1 if friend not valid.
1525
 * @retval -2 if friend not online.
1526
 * @retval -3 if filenumber invalid.
1527
 * @retval -4 if file transfer not transferring.
1528
 * @retval -5 if bad data size.
1529
 * @retval -6 if packet queue full.
1530
 * @retval -7 if wrong position.
1531
 */
1532
int send_file_data(const Messenger *m, int32_t friendnumber, uint32_t filenumber, uint64_t position,
1533
                   const uint8_t *data, uint16_t length)
1534
76.5k
{
1535
76.5k
    assert(length == 0 || data != nullptr);
1536
1537
76.5k
    if (!m_friend_exists(m, friendnumber)) {
1538
0
        return -1;
1539
0
    }
1540
1541
76.5k
    if (m->friendlist[friendnumber].status != FRIEND_ONLINE) {
1542
0
        return -2;
1543
0
    }
1544
1545
76.5k
    if (filenumber >= MAX_CONCURRENT_FILE_PIPES) {
1546
0
        return -3;
1547
0
    }
1548
1549
76.5k
    struct File_Transfers *ft = &m->friendlist[friendnumber].file_sending[filenumber];
1550
1551
76.5k
    if (ft->status != FILESTATUS_TRANSFERRING) {
1552
0
        return -4;
1553
0
    }
1554
1555
76.5k
    if (length > MAX_FILE_DATA_SIZE) {
1556
0
        return -5;
1557
0
    }
1558
1559
76.5k
    if (ft->size - ft->transferred < length) {
1560
0
        return -5;
1561
0
    }
1562
1563
76.5k
    if (ft->size != UINT64_MAX && length != MAX_FILE_DATA_SIZE && (ft->transferred + length) != ft->size) {
1564
0
        return -5;
1565
0
    }
1566
1567
76.5k
    if (position != ft->transferred || (ft->requested <= position && ft->size != 0)) {
1568
0
        return -7;
1569
0
    }
1570
1571
    /* Prevent file sending from filling up the entire buffer preventing messages from being sent.
1572
     * TODO(irungentoo): remove */
1573
76.5k
    if (crypto_num_free_sendqueue_slots(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
1574
76.5k
                                        m->friendlist[friendnumber].friendcon_id)) < MIN_SLOTS_FREE) {
1575
0
        return -6;
1576
0
    }
1577
1578
76.5k
    const int64_t ret = send_file_data_packet(m, friendnumber, filenumber, data, length);
1579
1580
76.5k
    if (ret != -1) {
1581
        // TODO(irungentoo): record packet ids to check if other received complete file.
1582
76.5k
        ft->transferred += length;
1583
1584
76.5k
        if (length != MAX_FILE_DATA_SIZE || ft->size == ft->transferred) {
1585
3
            ft->status = FILESTATUS_FINISHED;
1586
3
            ft->last_packet_number = ret;
1587
3
        }
1588
1589
76.5k
        return 0;
1590
76.5k
    }
1591
1592
0
    return -6;
1593
76.5k
}
1594
1595
/**
1596
 * Iterate over all file transfers and request chunks (from the client) for each
1597
 * of them.
1598
 *
1599
 * The free_slots parameter is updated by this function.
1600
 *
1601
 * @param m Our messenger object.
1602
 * @param friendnumber The friend we're sending files to.
1603
 * @param userdata The client userdata to pass along to chunk request callbacks.
1604
 * @param free_slots A pointer to the number of free send queue slots in the
1605
 *   crypto connection.
1606
 * @return true if there's still work to do, false otherwise.
1607
 *
1608
 */
1609
non_null()
1610
static bool do_all_filetransfers(Messenger *m, int32_t friendnumber, void *userdata, uint32_t *free_slots)
1611
172k
{
1612
172k
    Friend *const friendcon = &m->friendlist[friendnumber];
1613
1614
    // Iterate over file transfers as long as we're sending files
1615
42.5M
    for (uint32_t i = 0; i < MAX_CONCURRENT_FILE_PIPES; ++i) {
1616
42.4M
        if (friendcon->num_sending_files == 0) {
1617
            // no active file transfers anymore
1618
3
            return false;
1619
3
        }
1620
1621
42.4M
        if (*free_slots == 0) {
1622
            // send buffer full enough
1623
7.23k
            return false;
1624
7.23k
        }
1625
1626
42.3M
        struct File_Transfers *const ft = &friendcon->file_sending[i];
1627
1628
42.3M
        if (ft->status == FILESTATUS_NONE || ft->status == FILESTATUS_NOT_ACCEPTED) {
1629
            // Filetransfers not actively sending, nothing to do
1630
42.2M
            continue;
1631
42.2M
        }
1632
1633
170k
        if (max_speed_reached(m->net_crypto, friend_connection_crypt_connection_id(
1634
170k
                                  m->fr_c, friendcon->friendcon_id))) {
1635
0
            LOGGER_DEBUG(m->log, "maximum connection speed reached");
1636
            // connection doesn't support any more data
1637
0
            return false;
1638
0
        }
1639
1640
        // If the file transfer is complete, we request a chunk of size 0.
1641
170k
        if (ft->status == FILESTATUS_FINISHED && friend_received_packet(m, friendnumber, ft->last_packet_number) == 0) {
1642
3
            if (m->file_reqchunk != nullptr) {
1643
3
                m->file_reqchunk(m, friendnumber, i, ft->transferred, 0, userdata);
1644
3
            }
1645
1646
            // Now it's inactive, we're no longer sending this.
1647
3
            ft->status = FILESTATUS_NONE;
1648
3
            --friendcon->num_sending_files;
1649
170k
        } else if (ft->status == FILESTATUS_TRANSFERRING && ft->paused == FILE_PAUSE_NOT) {
1650
76.6k
            if (ft->size == 0) {
1651
                /* Send 0 data to friend if file is 0 length. */
1652
1
                send_file_data(m, friendnumber, i, 0, nullptr, 0);
1653
1
                continue;
1654
1
            }
1655
1656
76.6k
            if (ft->size == ft->requested) {
1657
                // This file transfer is done.
1658
46
                continue;
1659
46
            }
1660
1661
76.5k
            const uint16_t length = min_u64(ft->size - ft->requested, MAX_FILE_DATA_SIZE);
1662
76.5k
            const uint64_t position = ft->requested;
1663
76.5k
            ft->requested += length;
1664
1665
76.5k
            if (m->file_reqchunk != nullptr) {
1666
76.5k
                m->file_reqchunk(m, friendnumber, i, position, length, userdata);
1667
76.5k
            }
1668
1669
            // The allocated slot is no longer free.
1670
76.5k
            --*free_slots;
1671
76.5k
        }
1672
170k
    }
1673
1674
165k
    return true;
1675
172k
}
1676
1677
non_null(1) nullable(3)
1678
static void do_reqchunk_filecb(Messenger *m, int32_t friendnumber, void *userdata)
1679
91.1k
{
1680
    // We're not currently doing any file transfers.
1681
91.1k
    if (m->friendlist[friendnumber].num_sending_files == 0) {
1682
82.8k
        return;
1683
82.8k
    }
1684
1685
    // The number of packet slots left in the sendbuffer.
1686
    // This is a per friend count (CRYPTO_PACKET_BUFFER_SIZE).
1687
8.32k
    uint32_t free_slots = crypto_num_free_sendqueue_slots(
1688
8.32k
                              m->net_crypto,
1689
8.32k
                              friend_connection_crypt_connection_id(
1690
8.32k
                                  m->fr_c,
1691
8.32k
                                  m->friendlist[friendnumber].friendcon_id));
1692
1693
    // We keep MIN_SLOTS_FREE slots free for other packets, otherwise file
1694
    // transfers might block other traffic for a long time.
1695
8.32k
    free_slots = max_s32(0, (int32_t)free_slots - MIN_SLOTS_FREE);
1696
1697
    // Maximum number of outer loops below. If the client doesn't send file
1698
    // chunks from within the chunk request callback handler, we never realise
1699
    // that the file transfer has finished and may end up in an infinite loop.
1700
    //
1701
    // Request up to that number of chunks per file from the client
1702
    //
1703
    // TODO(Jfreegman): set this cap dynamically
1704
8.32k
    const uint32_t max_ft_loops = 128;
1705
1706
173k
    for (uint32_t i = 0; i < max_ft_loops; ++i) {
1707
172k
        if (!do_all_filetransfers(m, friendnumber, userdata, &free_slots)) {
1708
7.24k
            break;
1709
7.24k
        }
1710
1711
165k
        if (free_slots == 0) {
1712
            // stop when the buffer is full enough
1713
0
            break;
1714
0
        }
1715
165k
    }
1716
8.32k
}
1717
1718
1719
/** @brief Run this when the friend disconnects.
1720
 * Kill all current file transfers.
1721
 */
1722
static void break_files(const Messenger *m, int32_t friendnumber)
1723
38
{
1724
38
    Friend *const f = &m->friendlist[friendnumber];
1725
1726
    // TODO(irungentoo): Inform the client which file transfers get killed with a callback?
1727
9.76k
    for (uint32_t i = 0; i < MAX_CONCURRENT_FILE_PIPES; ++i) {
1728
9.72k
        f->file_sending[i].status = FILESTATUS_NONE;
1729
9.72k
        f->file_receiving[i].status = FILESTATUS_NONE;
1730
9.72k
    }
1731
38
}
1732
1733
non_null()
1734
static struct File_Transfers *get_file_transfer(bool outbound, uint8_t filenumber,
1735
        uint32_t *real_filenumber, Friend *sender)
1736
5
{
1737
5
    struct File_Transfers *ft;
1738
1739
5
    if (outbound) {
1740
5
        *real_filenumber = filenumber;
1741
5
        ft = &sender->file_sending[filenumber];
1742
5
    } else {
1743
0
        *real_filenumber = (filenumber + 1) << 16;
1744
0
        ft = &sender->file_receiving[filenumber];
1745
0
    }
1746
1747
5
    if (ft->status == FILESTATUS_NONE) {
1748
0
        return nullptr;
1749
0
    }
1750
1751
5
    return ft;
1752
5
}
1753
1754
/** @retval -1 on failure
1755
 * @retval 0 on success.
1756
 */
1757
non_null(1, 6) nullable(8)
1758
static int handle_filecontrol(Messenger *m, int32_t friendnumber, bool outbound, uint8_t filenumber,
1759
                              uint8_t control_type, const uint8_t *data, uint16_t length, void *userdata)
1760
5
{
1761
5
    uint32_t real_filenumber;
1762
5
    struct File_Transfers *ft = get_file_transfer(outbound, filenumber, &real_filenumber, &m->friendlist[friendnumber]);
1763
1764
5
    if (ft == nullptr) {
1765
0
        LOGGER_DEBUG(m->log, "file control (friend %d, file %d): file transfer does not exist; telling the other to kill it",
1766
0
                     friendnumber, filenumber);
1767
0
        send_file_control_packet(m, friendnumber, !outbound, filenumber, FILECONTROL_KILL, nullptr, 0);
1768
0
        return -1;
1769
0
    }
1770
1771
5
    switch (control_type) {
1772
3
        case FILECONTROL_ACCEPT: {
1773
3
            if (outbound && ft->status == FILESTATUS_NOT_ACCEPTED) {
1774
3
                ft->status = FILESTATUS_TRANSFERRING;
1775
3
                ++m->friendlist[friendnumber].num_sending_files;
1776
3
            } else {
1777
0
                if ((ft->paused & FILE_PAUSE_OTHER) != 0) {
1778
0
                    ft->paused ^= FILE_PAUSE_OTHER;
1779
0
                } else {
1780
0
                    LOGGER_DEBUG(m->log, "file control (friend %d, file %d): friend told us to resume file transfer that wasn't paused",
1781
0
                                 friendnumber, filenumber);
1782
0
                    return -1;
1783
0
                }
1784
0
            }
1785
1786
3
            if (m->file_filecontrol != nullptr) {
1787
3
                m->file_filecontrol(m, friendnumber, real_filenumber, control_type, userdata);
1788
3
            }
1789
1790
3
            return 0;
1791
3
        }
1792
1793
0
        case FILECONTROL_PAUSE: {
1794
0
            if ((ft->paused & FILE_PAUSE_OTHER) != 0 || ft->status != FILESTATUS_TRANSFERRING) {
1795
0
                LOGGER_DEBUG(m->log, "file control (friend %d, file %d): friend told us to pause file transfer that is already paused",
1796
0
                             friendnumber, filenumber);
1797
0
                return -1;
1798
0
            }
1799
1800
0
            ft->paused |= FILE_PAUSE_OTHER;
1801
1802
0
            if (m->file_filecontrol != nullptr) {
1803
0
                m->file_filecontrol(m, friendnumber, real_filenumber, control_type, userdata);
1804
0
            }
1805
1806
0
            return 0;
1807
0
        }
1808
1809
0
        case FILECONTROL_KILL: {
1810
0
            if (m->file_filecontrol != nullptr) {
1811
0
                m->file_filecontrol(m, friendnumber, real_filenumber, control_type, userdata);
1812
0
            }
1813
1814
0
            if (outbound && (ft->status == FILESTATUS_TRANSFERRING || ft->status == FILESTATUS_FINISHED)) {
1815
0
                --m->friendlist[friendnumber].num_sending_files;
1816
0
            }
1817
1818
0
            ft->status = FILESTATUS_NONE;
1819
1820
0
            return 0;
1821
0
        }
1822
1823
2
        case FILECONTROL_SEEK: {
1824
2
            uint64_t position;
1825
1826
2
            if (length != sizeof(position)) {
1827
0
                LOGGER_DEBUG(m->log, "file control (friend %d, file %d): expected payload of length %d, but got %d",
1828
0
                             friendnumber, filenumber, (uint32_t)sizeof(position), length);
1829
0
                return -1;
1830
0
            }
1831
1832
            /* seek can only be sent by the receiver to seek before resuming broken transfers. */
1833
2
            if (ft->status != FILESTATUS_NOT_ACCEPTED || !outbound) {
1834
0
                LOGGER_DEBUG(m->log,
1835
0
                             "file control (friend %d, file %d): seek was either sent by a sender or by the receiver after accepting",
1836
0
                             friendnumber, filenumber);
1837
0
                return -1;
1838
0
            }
1839
1840
2
            net_unpack_u64(data, &position);
1841
1842
2
            if (position >= ft->size) {
1843
0
                LOGGER_DEBUG(m->log,
1844
0
                             "file control (friend %d, file %d): seek position %ld exceeds file size %ld",
1845
0
                             friendnumber, filenumber, (unsigned long)position, (unsigned long)ft->size);
1846
0
                return -1;
1847
0
            }
1848
1849
2
            ft->requested = position;
1850
2
            ft->transferred = position;
1851
2
            return 0;
1852
2
        }
1853
1854
0
        default: {
1855
0
            LOGGER_DEBUG(m->log, "file control (friend %d, file %d): invalid file control: %d",
1856
0
                         friendnumber, filenumber, control_type);
1857
0
            return -1;
1858
2
        }
1859
5
    }
1860
5
}
1861
1862
/** @brief Set the callback for msi packets. */
1863
void m_callback_msi_packet(Messenger *m, m_msi_packet_cb *function, void *userdata)
1864
12
{
1865
12
    m->msi_packet = function;
1866
12
    m->msi_packet_userdata = userdata;
1867
12
}
1868
1869
/** @brief Send an msi packet.
1870
 *
1871
 * @retval true on success
1872
 * @retval false on failure
1873
 */
1874
bool m_msi_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint16_t length)
1875
44
{
1876
44
    return write_cryptpacket_id(m, friendnumber, PACKET_ID_MSI, data, length, false);
1877
44
}
1878
1879
static int m_handle_lossy_packet(void *object, int friendcon_id, const uint8_t *data, uint16_t length,
1880
                                 void *userdata)
1881
10.9k
{
1882
10.9k
    Messenger *m = (Messenger *)object;
1883
1884
10.9k
    if (!m_friend_exists(m, friendcon_id)) {
1885
0
        return 1;
1886
0
    }
1887
1888
10.9k
    if (data[0] <= PACKET_ID_RANGE_LOSSY_AV_END) {
1889
10.9k
        const RTP_Packet_Handler *const ph =
1890
10.9k
            &m->friendlist[friendcon_id].lossy_rtp_packethandlers[data[0] % PACKET_ID_RANGE_LOSSY_AV_SIZE];
1891
1892
10.9k
        if (ph->function != nullptr) {
1893
321
            return ph->function(m, friendcon_id, data, length, ph->object);
1894
321
        }
1895
1896
10.6k
        return 1;
1897
10.9k
    }
1898
1899
9
    if (m->lossy_packethandler != nullptr) {
1900
9
        m->lossy_packethandler(m, friendcon_id, data[0], data, length, userdata);
1901
9
    }
1902
1903
9
    return 1;
1904
10.9k
}
1905
1906
void custom_lossy_packet_registerhandler(Messenger *m, m_friend_lossy_packet_cb *lossy_packethandler)
1907
3.34k
{
1908
3.34k
    m->lossy_packethandler = lossy_packethandler;
1909
3.34k
}
1910
1911
int m_callback_rtp_packet(Messenger *m, int32_t friendnumber, uint8_t byte, m_lossy_rtp_packet_cb *function,
1912
                          void *object)
1913
148
{
1914
148
    if (!m_friend_exists(m, friendnumber)) {
1915
0
        return -1;
1916
0
    }
1917
1918
148
    if (byte < PACKET_ID_RANGE_LOSSY_AV_START || byte > PACKET_ID_RANGE_LOSSY_AV_END) {
1919
0
        return -1;
1920
0
    }
1921
1922
148
    m->friendlist[friendnumber].lossy_rtp_packethandlers[byte % PACKET_ID_RANGE_LOSSY_AV_SIZE].function = function;
1923
148
    m->friendlist[friendnumber].lossy_rtp_packethandlers[byte % PACKET_ID_RANGE_LOSSY_AV_SIZE].object = object;
1924
148
    return 0;
1925
148
}
1926
1927
1928
/** @brief High level function to send custom lossy packets.
1929
 *
1930
 * TODO(oxij): this name is confusing, because this function sends both av and custom lossy packets.
1931
 * Meanwhile, m_handle_lossy_packet routes custom packets to custom_lossy_packet_registerhandler
1932
 * as you would expect from its name.
1933
 *
1934
 * I.e. custom_lossy_packet_registerhandler's "custom lossy packet" and this "custom lossy packet"
1935
 * are not the same set of packets.
1936
 *
1937
 * @retval -1 if friend invalid.
1938
 * @retval -2 if length wrong.
1939
 * @retval -3 if first byte invalid.
1940
 * @retval -4 if friend offline.
1941
 * @retval -5 if packet failed to send because of other error.
1942
 * @retval 0 on success.
1943
 */
1944
int m_send_custom_lossy_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint32_t length)
1945
353
{
1946
353
    if (!m_friend_exists(m, friendnumber)) {
1947
0
        return -1;
1948
0
    }
1949
1950
353
    if (length == 0 || length > MAX_CRYPTO_DATA_SIZE) {
1951
12
        return -2;
1952
12
    }
1953
1954
    // TODO(oxij): send_lossy_cryptpacket makes this check already, similarly for other similar places
1955
341
    if (data[0] < PACKET_ID_RANGE_LOSSY_START || data[0] > PACKET_ID_RANGE_LOSSY_END) {
1956
0
        return -3;
1957
0
    }
1958
1959
341
    if (m->friendlist[friendnumber].status != FRIEND_ONLINE) {
1960
0
        return -4;
1961
0
    }
1962
1963
341
    if (send_lossy_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
1964
341
                               m->friendlist[friendnumber].friendcon_id), data, length) == -1) {
1965
2
        return -5;
1966
2
    }
1967
1968
339
    return 0;
1969
341
}
1970
1971
non_null(1, 3) nullable(5)
1972
static int handle_custom_lossless_packet(void *object, int friend_num, const uint8_t *packet, uint16_t length,
1973
        void *userdata)
1974
8.76k
{
1975
8.76k
    Messenger *m = (Messenger *)object;
1976
1977
8.76k
    if (!m_friend_exists(m, friend_num)) {
1978
0
        return -1;
1979
0
    }
1980
1981
8.76k
    if (packet[0] < PACKET_ID_RANGE_LOSSLESS_CUSTOM_START || packet[0] > PACKET_ID_RANGE_LOSSLESS_CUSTOM_END) {
1982
8.75k
        return -1;
1983
8.75k
    }
1984
1985
11
    if (m->lossless_packethandler != nullptr) {
1986
11
        m->lossless_packethandler(m, friend_num, packet[0], packet, length, userdata);
1987
11
    }
1988
1989
11
    return 1;
1990
8.76k
}
1991
1992
void custom_lossless_packet_registerhandler(Messenger *m, m_friend_lossless_packet_cb *lossless_packethandler)
1993
3.34k
{
1994
3.34k
    m->lossless_packethandler = lossless_packethandler;
1995
3.34k
}
1996
1997
int send_custom_lossless_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint32_t length)
1998
24
{
1999
24
    if (!m_friend_exists(m, friendnumber)) {
2000
0
        return -1;
2001
0
    }
2002
2003
24
    if (length == 0 || length > MAX_CRYPTO_DATA_SIZE) {
2004
12
        return -2;
2005
12
    }
2006
2007
12
    if ((data[0] < PACKET_ID_RANGE_LOSSLESS_CUSTOM_START || data[0] > PACKET_ID_RANGE_LOSSLESS_CUSTOM_END)
2008
12
            && data[0] != PACKET_ID_MSI) {
2009
0
        return -3;
2010
0
    }
2011
2012
12
    if (m->friendlist[friendnumber].status != FRIEND_ONLINE) {
2013
0
        return -4;
2014
0
    }
2015
2016
12
    if (write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
2017
12
                          m->friendlist[friendnumber].friendcon_id), data, length, true) == -1) {
2018
1
        return -5;
2019
1
    }
2020
2021
11
    return 0;
2022
12
}
2023
2024
/** Function to filter out some friend requests*/
2025
non_null()
2026
static int friend_already_added(void *object, const uint8_t *public_key)
2027
179
{
2028
179
    const Messenger *m = (const Messenger *)object;
2029
2030
179
    if (getfriend_id(m, public_key) == -1) {
2031
179
        return 0;
2032
179
    }
2033
2034
0
    return -1;
2035
179
}
2036
2037
/** @brief Check for and handle a timed-out friend request.
2038
 *
2039
 * If the request has timed-out then the friend status is set back to FRIEND_ADDED.
2040
 * @param friendcon_id friendlist index of the timed-out friend
2041
 * @param t time
2042
 */
2043
non_null(1) nullable(4)
2044
static void check_friend_request_timed_out(Messenger *m, uint32_t friendcon_id, uint64_t t, void *userdata)
2045
2.41k
{
2046
2.41k
    Friend *f = &m->friendlist[friendcon_id];
2047
2048
2.41k
    if (f->friendrequest_lastsent + f->friendrequest_timeout < t) {
2049
1
        set_friend_status(m, friendcon_id, FRIEND_ADDED, userdata);
2050
        /* Double the default timeout every time if friendrequest is assumed
2051
         * to have been sent unsuccessfully.
2052
         */
2053
1
        f->friendrequest_timeout *= 2;
2054
1
    }
2055
2.41k
}
2056
2057
non_null(1) nullable(4)
2058
static int m_handle_status(void *object, int friendcon_id, bool status, void *userdata)
2059
1.40k
{
2060
1.40k
    Messenger *m = (Messenger *)object;
2061
2062
1.40k
    if (status) { /* Went online. */
2063
1.36k
        send_online_packet(m, m->friendlist[friendcon_id].friendcon_id);
2064
1.36k
    } else { /* Went offline. */
2065
39
        if (m->friendlist[friendcon_id].status == FRIEND_ONLINE) {
2066
38
            set_friend_status(m, friendcon_id, FRIEND_CONFIRMED, userdata);
2067
38
        }
2068
39
    }
2069
2070
1.40k
    return 0;
2071
1.40k
}
2072
2073
non_null(1, 3) nullable(5)
2074
static int m_handle_packet_offline(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, void *userdata)
2075
0
{
2076
0
    if (data_length == 0) {
2077
0
        set_friend_status(m, friendcon_id, FRIEND_CONFIRMED, userdata);
2078
0
    }
2079
2080
0
    return 0;
2081
0
}
2082
2083
non_null(1, 3) nullable(5)
2084
static int m_handle_packet_nickname(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, void *userdata)
2085
1.31k
{
2086
1.31k
    if (data_length > MAX_NAME_LENGTH) {
2087
0
        return 0;
2088
0
    }
2089
2090
    /* Make sure the NULL terminator is present. */
2091
1.31k
    VLA(uint8_t, data_terminated, data_length + 1);
2092
1.31k
    memcpy(data_terminated, data, data_length);
2093
1.31k
    data_terminated[data_length] = 0;
2094
2095
    /* inform of namechange before we overwrite the old name */
2096
1.31k
    if (m->friend_namechange != nullptr) {
2097
1.31k
        m->friend_namechange(m, friendcon_id, data_terminated, data_length, userdata);
2098
1.31k
    }
2099
2100
1.31k
    memcpy(m->friendlist[friendcon_id].name, data_terminated, data_length);
2101
1.31k
    m->friendlist[friendcon_id].name_length = data_length;
2102
2103
1.31k
    return 0;
2104
1.31k
}
2105
2106
non_null(1, 3) nullable(5)
2107
static int m_handle_packet_statusmessage(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, void *userdata)
2108
1.28k
{
2109
1.28k
    if (data_length > MAX_STATUSMESSAGE_LENGTH) {
2110
0
        return 0;
2111
0
    }
2112
2113
    /* Make sure the NULL terminator is present. */
2114
1.28k
    VLA(uint8_t, data_terminated, data_length + 1);
2115
1.28k
    memcpy(data_terminated, data, data_length);
2116
1.28k
    data_terminated[data_length] = 0;
2117
2118
1.28k
    if (m->friend_statusmessagechange != nullptr) {
2119
1.28k
        m->friend_statusmessagechange(m, friendcon_id, data_terminated, data_length, userdata);
2120
1.28k
    }
2121
2122
1.28k
    set_friend_statusmessage(m, friendcon_id, data_terminated, data_length);
2123
2124
1.28k
    return 0;
2125
1.28k
}
2126
2127
non_null(1, 3) nullable(5)
2128
static int m_handle_packet_userstatus(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, void *userdata)
2129
1.28k
{
2130
1.28k
    if (data_length != 1) {
2131
0
        return 0;
2132
0
    }
2133
2134
1.28k
    Userstatus status;
2135
1.28k
    if (!userstatus_from_int(data[0], &status)) {
2136
0
        return 0;
2137
0
    }
2138
2139
1.28k
    if (m->friend_userstatuschange != nullptr) {
2140
1.28k
        m->friend_userstatuschange(m, friendcon_id, status, userdata);
2141
1.28k
    }
2142
2143
1.28k
    set_friend_userstatus(m, friendcon_id, status);
2144
2145
1.28k
    return 0;
2146
1.28k
}
2147
2148
non_null(1, 3) nullable(5)
2149
static int m_handle_packet_typing(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, void *userdata)
2150
1.28k
{
2151
1.28k
    if (data_length != 1) {
2152
0
        return 0;
2153
0
    }
2154
2155
1.28k
    const bool typing = data[0] != 0;
2156
2157
1.28k
    set_friend_typing(m, friendcon_id, typing);
2158
2159
1.28k
    if (m->friend_typingchange != nullptr) {
2160
1.28k
        m->friend_typingchange(m, friendcon_id, typing, userdata);
2161
1.28k
    }
2162
2163
1.28k
    return 0;
2164
1.28k
}
2165
2166
non_null(1, 3) nullable(6)
2167
static int m_handle_packet_message(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, const Message_Type message_type, void *userdata)
2168
41.6k
{
2169
41.6k
    if (data_length == 0) {
2170
0
        return 0;
2171
0
    }
2172
2173
41.6k
    const uint8_t *message = data;
2174
41.6k
    const uint16_t message_length = data_length;
2175
2176
    /* Make sure the NULL terminator is present. */
2177
41.6k
    VLA(uint8_t, message_terminated, message_length + 1);
2178
41.6k
    memcpy(message_terminated, message, message_length);
2179
41.6k
    message_terminated[message_length] = 0;
2180
2181
41.6k
    if (m->friend_message != nullptr) {
2182
41.6k
        m->friend_message(m, friendcon_id, message_type, message_terminated, message_length, userdata);
2183
41.6k
    }
2184
2185
41.6k
    return 0;
2186
41.6k
}
2187
2188
non_null(1, 3) nullable(5)
2189
static int m_handle_packet_invite_conference(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, void *userdata)
2190
122
{
2191
122
    if (data_length == 0) {
2192
0
        return 0;
2193
0
    }
2194
2195
122
    if (m->conference_invite != nullptr) {
2196
122
        m->conference_invite(m, friendcon_id, data, data_length, userdata);
2197
122
    }
2198
2199
122
    return 0;
2200
122
}
2201
2202
non_null(1, 3) nullable(5)
2203
static int m_handle_packet_file_sendrequest(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, void *userdata)
2204
3
{
2205
3
    const unsigned int head_length = 1 + sizeof(uint32_t) + sizeof(uint64_t) + FILE_ID_LENGTH;
2206
2207
3
    if (data_length < head_length) {
2208
0
        return 0;
2209
0
    }
2210
2211
3
    const uint8_t filenumber = data[0];
2212
2213
#if UINT8_MAX >= MAX_CONCURRENT_FILE_PIPES
2214
2215
    if (filenumber >= MAX_CONCURRENT_FILE_PIPES) {
2216
        return 0;
2217
    }
2218
2219
#endif /* UINT8_MAX >= MAX_CONCURRENT_FILE_PIPES */
2220
2221
3
    uint64_t filesize;
2222
3
    uint32_t file_type;
2223
3
    const uint16_t filename_length = data_length - head_length;
2224
2225
3
    if (filename_length > MAX_FILENAME_LENGTH) {
2226
0
        return 0;
2227
0
    }
2228
2229
3
    memcpy(&file_type, data + 1, sizeof(file_type));
2230
3
    file_type = net_ntohl(file_type);
2231
2232
3
    net_unpack_u64(data + 1 + sizeof(uint32_t), &filesize);
2233
3
    struct File_Transfers *ft = &m->friendlist[friendcon_id].file_receiving[filenumber];
2234
2235
3
    if (ft->status != FILESTATUS_NONE) {
2236
0
        return 0;
2237
0
    }
2238
2239
3
    ft->status = FILESTATUS_NOT_ACCEPTED;
2240
3
    ft->size = filesize;
2241
3
    ft->transferred = 0;
2242
3
    ft->paused = FILE_PAUSE_NOT;
2243
3
    memcpy(ft->id, data + 1 + sizeof(uint32_t) + sizeof(uint64_t), FILE_ID_LENGTH);
2244
2245
3
    VLA(uint8_t, filename_terminated, filename_length + 1);
2246
3
    const uint8_t *filename = nullptr;
2247
2248
3
    if (filename_length > 0) {
2249
        /* Force NULL terminate file name. */
2250
3
        memcpy(filename_terminated, data + head_length, filename_length);
2251
3
        filename_terminated[filename_length] = 0;
2252
3
        filename = filename_terminated;
2253
3
    }
2254
2255
3
    uint32_t real_filenumber = filenumber;
2256
3
    real_filenumber += 1;
2257
3
    real_filenumber <<= 16;
2258
2259
3
    if (m->file_sendrequest != nullptr) {
2260
3
        m->file_sendrequest(m, friendcon_id, real_filenumber, file_type, filesize, filename, filename_length,
2261
3
                            userdata);
2262
3
    }
2263
2264
3
    return 0;
2265
3
}
2266
2267
non_null(1, 3) nullable(5)
2268
static int m_handle_packet_file_control(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, void *userdata)
2269
5
{
2270
5
    if (data_length < 3) {
2271
0
        return 0;
2272
0
    }
2273
2274
    // On the other side, "outbound" is "inbound", i.e. if they send 1,
2275
    // that means "inbound" on their side, but we call it "outbound"
2276
    // here.
2277
5
    const bool outbound = data[0] == 1;
2278
5
    const uint8_t filenumber = data[1];
2279
5
    const uint8_t control_type = data[2];
2280
2281
#if UINT8_MAX >= MAX_CONCURRENT_FILE_PIPES
2282
2283
    if (filenumber >= MAX_CONCURRENT_FILE_PIPES) {
2284
        return 0;
2285
    }
2286
2287
#endif /* UINT8_MAX >= MAX_CONCURRENT_FILE_PIPES */
2288
2289
5
    if (handle_filecontrol(m, friendcon_id, outbound, filenumber, control_type, data + 3, data_length - 3, userdata) == -1) {
2290
        // TODO(iphydf): Do something different here? Right now, this
2291
        // check is pointless.
2292
0
        return 0;
2293
0
    }
2294
2295
5
    return 0;
2296
5
}
2297
2298
non_null(1, 3) nullable(5)
2299
static int m_handle_packet_file_data(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, void *userdata)
2300
76.5k
{
2301
76.5k
    if (data_length < 1) {
2302
0
        return 0;
2303
0
    }
2304
2305
76.5k
    const uint8_t filenumber = data[0];
2306
2307
#if UINT8_MAX >= MAX_CONCURRENT_FILE_PIPES
2308
2309
    if (filenumber >= MAX_CONCURRENT_FILE_PIPES) {
2310
        return 0;
2311
    }
2312
2313
#endif /* UINT8_MAX >= MAX_CONCURRENT_FILE_PIPES */
2314
2315
76.5k
    struct File_Transfers *ft = &m->friendlist[friendcon_id].file_receiving[filenumber];
2316
2317
76.5k
    if (ft->status != FILESTATUS_TRANSFERRING) {
2318
0
        return 0;
2319
0
    }
2320
2321
76.5k
    uint64_t position = ft->transferred;
2322
76.5k
    uint32_t real_filenumber = filenumber;
2323
76.5k
    real_filenumber += 1;
2324
76.5k
    real_filenumber <<= 16;
2325
76.5k
    uint16_t file_data_length = data_length - 1;
2326
76.5k
    const uint8_t *file_data;
2327
2328
76.5k
    if (file_data_length == 0) {
2329
1
        file_data = nullptr;
2330
76.5k
    } else {
2331
76.5k
        file_data = data + 1;
2332
76.5k
    }
2333
2334
    /* Prevent more data than the filesize from being passed to clients. */
2335
76.5k
    if ((ft->transferred + file_data_length) > ft->size) {
2336
0
        file_data_length = ft->size - ft->transferred;
2337
0
    }
2338
2339
76.5k
    if (m->file_filedata != nullptr) {
2340
76.5k
        m->file_filedata(m, friendcon_id, real_filenumber, position, file_data, file_data_length, userdata);
2341
76.5k
    }
2342
2343
76.5k
    ft->transferred += file_data_length;
2344
2345
76.5k
    if (file_data_length > 0 && (ft->transferred >= ft->size || file_data_length != MAX_FILE_DATA_SIZE)) {
2346
2
        file_data_length = 0;
2347
2
        file_data = nullptr;
2348
2
        position = ft->transferred;
2349
2350
        /* Full file received. */
2351
2
        if (m->file_filedata != nullptr) {
2352
2
            m->file_filedata(m, friendcon_id, real_filenumber, position, file_data, file_data_length, userdata);
2353
2
        }
2354
2
    }
2355
2356
    /* Data is zero, filetransfer is over. */
2357
76.5k
    if (file_data_length == 0) {
2358
3
        ft->status = FILESTATUS_NONE;
2359
3
    }
2360
2361
76.5k
    return 0;
2362
76.5k
}
2363
2364
non_null(1, 3) nullable(5)
2365
static int m_handle_packet_msi(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, void *userdata)
2366
44
{
2367
44
    if (data_length == 0) {
2368
0
        return 0;
2369
0
    }
2370
2371
44
    if (m->msi_packet != nullptr) {
2372
44
        m->msi_packet(m, friendcon_id, data, data_length, m->msi_packet_userdata);
2373
44
    }
2374
2375
44
    return 0;
2376
44
}
2377
2378
non_null(1, 3) nullable(5)
2379
static int m_handle_packet_invite_groupchat(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, void *userdata)
2380
272
{
2381
    // first two bytes are messenger packet type and group invite type
2382
272
    if (data_length < 2 + GC_JOIN_DATA_LENGTH) {
2383
0
        return 0;
2384
0
    }
2385
2386
272
    const uint8_t invite_type = data[1];
2387
272
    const uint8_t *join_data = data + 2;
2388
272
    const uint32_t join_data_len = data_length - 2;
2389
2390
272
    if (m->group_invite != nullptr && data[1] == GROUP_INVITE && data_length != 2 + GC_JOIN_DATA_LENGTH) {
2391
98
        if (group_not_added(m->group_handler, join_data, join_data_len)) {
2392
98
            m->group_invite(m, friendcon_id, join_data, GC_JOIN_DATA_LENGTH,
2393
98
                            join_data + GC_JOIN_DATA_LENGTH, join_data_len - GC_JOIN_DATA_LENGTH, userdata);
2394
98
        }
2395
174
    } else if (invite_type == GROUP_INVITE_ACCEPTED) {
2396
87
        handle_gc_invite_accepted_packet(m->group_handler, friendcon_id, join_data, join_data_len);
2397
87
    } else if (invite_type == GROUP_INVITE_CONFIRMATION) {
2398
87
        handle_gc_invite_confirmed_packet(m->group_handler, friendcon_id, join_data, join_data_len);
2399
87
    }
2400
2401
272
    return 0;
2402
272
}
2403
2404
non_null(1, 3) nullable(5)
2405
static int m_handle_packet(void *object, int friendcon_id, const uint8_t *data, uint16_t length, void *userdata)
2406
132k
{
2407
132k
    Messenger *m = (Messenger *)object;
2408
2409
132k
    if (length == 0) {
2410
0
        return -1;
2411
0
    }
2412
2413
132k
    const uint8_t packet_id = data[0];
2414
132k
    const uint8_t *payload = data + 1;
2415
132k
    const uint16_t payload_length = length - 1;
2416
2417
132k
    if (m->friendlist[friendcon_id].status != FRIEND_ONLINE) {
2418
1.38k
        if (packet_id == PACKET_ID_ONLINE && length == 1) {
2419
1.34k
            set_friend_status(m, friendcon_id, FRIEND_ONLINE, userdata);
2420
1.34k
            send_online_packet(m, m->friendlist[friendcon_id].friendcon_id);
2421
1.34k
        } else {
2422
45
            return -1;
2423
45
        }
2424
1.38k
    }
2425
2426
132k
    switch (packet_id) {
2427
        // TODO(Green-Sky): now all return 0 on error AND success, make errors errors?
2428
0
        case PACKET_ID_OFFLINE:
2429
0
            return m_handle_packet_offline(m, friendcon_id, payload, payload_length, userdata);
2430
1.31k
        case PACKET_ID_NICKNAME:
2431
1.31k
            return m_handle_packet_nickname(m, friendcon_id, payload, payload_length, userdata);
2432
1.28k
        case PACKET_ID_STATUSMESSAGE:
2433
1.28k
            return m_handle_packet_statusmessage(m, friendcon_id, payload, payload_length, userdata);
2434
1.28k
        case PACKET_ID_USERSTATUS:
2435
1.28k
            return m_handle_packet_userstatus(m, friendcon_id, payload, payload_length, userdata);
2436
1.28k
        case PACKET_ID_TYPING:
2437
1.28k
            return m_handle_packet_typing(m, friendcon_id, payload, payload_length, userdata);
2438
41.6k
        case PACKET_ID_MESSAGE:
2439
41.6k
            return m_handle_packet_message(m, friendcon_id, payload, payload_length, MESSAGE_NORMAL, userdata);
2440
0
        case PACKET_ID_ACTION:
2441
0
            return m_handle_packet_message(m, friendcon_id, payload, payload_length, MESSAGE_ACTION, userdata);
2442
122
        case PACKET_ID_INVITE_CONFERENCE:
2443
122
            return m_handle_packet_invite_conference(m, friendcon_id, payload, payload_length, userdata);
2444
3
        case PACKET_ID_FILE_SENDREQUEST:
2445
3
            return m_handle_packet_file_sendrequest(m, friendcon_id, payload, payload_length, userdata);
2446
5
        case PACKET_ID_FILE_CONTROL:
2447
5
            return m_handle_packet_file_control(m, friendcon_id, payload, payload_length, userdata);
2448
76.5k
        case PACKET_ID_FILE_DATA:
2449
76.5k
            return m_handle_packet_file_data(m, friendcon_id, payload, payload_length, userdata);
2450
44
        case PACKET_ID_MSI:
2451
44
            return m_handle_packet_msi(m, friendcon_id, payload, payload_length, userdata);
2452
272
        case PACKET_ID_INVITE_GROUPCHAT:
2453
272
            return m_handle_packet_invite_groupchat(m, friendcon_id, payload, payload_length, userdata);
2454
132k
    }
2455
2456
8.76k
    return handle_custom_lossless_packet(object, friendcon_id, data, length, userdata);
2457
132k
}
2458
2459
non_null(1) nullable(2)
2460
static void do_friends(Messenger *m, void *userdata)
2461
212k
{
2462
212k
    const uint64_t temp_time = mono_time_get(m->mono_time);
2463
2464
397k
    for (uint32_t i = 0; i < m->numfriends; ++i) {
2465
185k
        if (m->friendlist[i].status == FRIEND_ADDED) {
2466
11.9k
            const int fr = send_friend_request_packet(m->fr_c, m->friendlist[i].friendcon_id, m->friendlist[i].friendrequest_nospam,
2467
11.9k
                                                m->friendlist[i].info,
2468
11.9k
                                                m->friendlist[i].info_size);
2469
2470
11.9k
            if (fr >= 0) {
2471
140
                set_friend_status(m, i, FRIEND_REQUESTED, userdata);
2472
140
                m->friendlist[i].friendrequest_lastsent = temp_time;
2473
140
            }
2474
11.9k
        }
2475
2476
185k
        if (m->friendlist[i].status == FRIEND_REQUESTED
2477
185k
                || m->friendlist[i].status == FRIEND_CONFIRMED) { /* friend is not online. */
2478
82.3k
            if (m->friendlist[i].status == FRIEND_REQUESTED) {
2479
                /* If we didn't connect to friend after successfully sending him a friend request the request is deemed
2480
                 * unsuccessful so we set the status back to FRIEND_ADDED and try again.
2481
                 */
2482
2.41k
                check_friend_request_timed_out(m, i, temp_time, userdata);
2483
2.41k
            }
2484
82.3k
        }
2485
2486
185k
        if (m->friendlist[i].status == FRIEND_ONLINE) { /* friend is online. */
2487
91.1k
            if (!m->friendlist[i].name_sent) {
2488
1.37k
                if (m_sendname(m, i, m->name, m->name_length)) {
2489
1.36k
                    m->friendlist[i].name_sent = true;
2490
1.36k
                }
2491
1.37k
            }
2492
2493
91.1k
            if (!m->friendlist[i].statusmessage_sent) {
2494
1.34k
                if (send_statusmessage(m, i, m->statusmessage, m->statusmessage_length)) {
2495
1.33k
                    m->friendlist[i].statusmessage_sent = true;
2496
1.33k
                }
2497
1.34k
            }
2498
2499
91.1k
            if (!m->friendlist[i].userstatus_sent) {
2500
1.34k
                if (send_userstatus(m, i, m->userstatus)) {
2501
1.33k
                    m->friendlist[i].userstatus_sent = true;
2502
1.33k
                }
2503
1.34k
            }
2504
2505
91.1k
            if (!m->friendlist[i].user_istyping_sent) {
2506
1.36k
                if (send_user_istyping(m, i, m->friendlist[i].user_istyping)) {
2507
1.35k
                    m->friendlist[i].user_istyping_sent = true;
2508
1.35k
                }
2509
1.36k
            }
2510
2511
91.1k
            check_friend_tcp_udp(m, i, userdata);
2512
91.1k
            do_receipts(m, i, userdata);
2513
91.1k
            do_reqchunk_filecb(m, i, userdata);
2514
2515
91.1k
            m->friendlist[i].last_seen_time = (uint64_t) time(nullptr);
2516
91.1k
        }
2517
185k
    }
2518
212k
}
2519
2520
non_null(1) nullable(2)
2521
static void m_connection_status_callback(Messenger *m, void *userdata)
2522
212k
{
2523
212k
    const Onion_Connection_Status conn_status = onion_connection_status(m->onion_c);
2524
2525
212k
    if (conn_status != m->last_connection_status) {
2526
1.08k
        if (m->core_connection_change != nullptr) {
2527
1.08k
            m->core_connection_change(m, conn_status, userdata);
2528
1.08k
        }
2529
2530
1.08k
        m->last_connection_status = conn_status;
2531
1.08k
    }
2532
212k
}
2533
2534
2535
212k
#define DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS 60UL
2536
2537
9.49k
#define IDSTRING_LEN (CRYPTO_PUBLIC_KEY_SIZE * 2 + 1)
2538
/** id_str should be of length at least IDSTRING_LEN */
2539
non_null()
2540
static char *id_to_string(const uint8_t *pk, char *id_str, size_t length)
2541
9.49k
{
2542
9.49k
    if (length < IDSTRING_LEN) {
2543
0
        snprintf(id_str, length, "Bad buf length");
2544
0
        return id_str;
2545
0
    }
2546
2547
313k
    for (uint32_t i = 0; i < CRYPTO_PUBLIC_KEY_SIZE; ++i) {
2548
303k
        snprintf(&id_str[i * 2], length - i * 2, "%02X", pk[i]);
2549
303k
    }
2550
2551
9.49k
    id_str[CRYPTO_PUBLIC_KEY_SIZE * 2] = '\0';
2552
9.49k
    return id_str;
2553
9.49k
}
2554
2555
/** @brief Minimum messenger run interval in ms
2556
 * TODO(mannol): A/V
2557
 */
2558
41.2k
#define MIN_RUN_INTERVAL 50
2559
2560
/**
2561
 * @brief Return the time in milliseconds before `do_messenger()` should be called again
2562
 *   for optimal performance.
2563
 *
2564
 * @return time (in ms) before the next `do_messenger()` needs to be run on success.
2565
 */
2566
uint32_t messenger_run_interval(const Messenger *m)
2567
25.5k
{
2568
25.5k
    const uint32_t crypto_interval = crypto_run_interval(m->net_crypto);
2569
2570
25.5k
    if (crypto_interval > MIN_RUN_INTERVAL) {
2571
15.6k
        return MIN_RUN_INTERVAL;
2572
15.6k
    }
2573
2574
9.86k
    return crypto_interval;
2575
25.5k
}
2576
2577
/** @brief Attempts to create a DHT announcement for a group chat with our connection info. An
2578
 * announcement can only be created if we either have a UDP or TCP connection to the network.
2579
 *
2580
 * @retval true if success.
2581
 */
2582
non_null()
2583
static bool self_announce_group(const Messenger *m, GC_Chat *chat, Onion_Friend *onion_friend)
2584
276
{
2585
276
    GC_Public_Announce announce = {{{{{0}}}}};
2586
2587
276
    const bool ip_port_is_set = chat->self_udp_status != SELF_UDP_STATUS_NONE;
2588
276
    const int tcp_num = tcp_copy_connected_relays(chat->tcp_conn, announce.base_announce.tcp_relays,
2589
276
                        GCA_MAX_ANNOUNCED_TCP_RELAYS);
2590
2591
276
    if (tcp_num == 0 && !ip_port_is_set) {
2592
0
        onion_friend_set_gc_data(onion_friend, nullptr, 0);
2593
0
        return false;
2594
0
    }
2595
2596
276
    announce.base_announce.tcp_relays_count = (uint8_t)tcp_num;
2597
276
    announce.base_announce.ip_port_is_set = (uint8_t)(ip_port_is_set ? 1 : 0);
2598
2599
276
    if (ip_port_is_set) {
2600
276
        memcpy(&announce.base_announce.ip_port, &chat->self_ip_port, sizeof(IP_Port));
2601
276
    }
2602
2603
276
    memcpy(announce.base_announce.peer_public_key, chat->self_public_key, ENC_PUBLIC_KEY_SIZE);
2604
276
    memcpy(announce.chat_public_key, get_chat_id(chat->chat_public_key), ENC_PUBLIC_KEY_SIZE);
2605
2606
276
    uint8_t gc_data[GCA_MAX_DATA_LENGTH];
2607
276
    const int length = gca_pack_public_announce(m->log, gc_data, GCA_MAX_DATA_LENGTH, &announce);
2608
2609
276
    if (length <= 0) {
2610
0
        onion_friend_set_gc_data(onion_friend, nullptr, 0);
2611
0
        return false;
2612
0
    }
2613
2614
276
    if (gca_add_announce(m->mono_time, m->group_announce, &announce) == nullptr) {
2615
0
        onion_friend_set_gc_data(onion_friend, nullptr, 0);
2616
0
        return false;
2617
0
    }
2618
2619
276
    onion_friend_set_gc_data(onion_friend, gc_data, (uint16_t)length);
2620
276
    chat->update_self_announces = false;
2621
276
    chat->last_time_self_announce = mono_time_get(chat->mono_time);
2622
2623
276
    if (tcp_num > 0) {
2624
0
        pk_copy(chat->announced_tcp_relay_pk, announce.base_announce.tcp_relays[0].public_key);
2625
276
    } else {
2626
276
        memzero(chat->announced_tcp_relay_pk, sizeof(chat->announced_tcp_relay_pk));
2627
276
    }
2628
2629
276
    LOGGER_DEBUG(chat->log, "Published group announce. TCP relays: %d, UDP status: %d", tcp_num,
2630
276
                 chat->self_udp_status);
2631
276
    return true;
2632
276
}
2633
2634
non_null()
2635
static void do_gc_onion_friends(const Messenger *m)
2636
212k
{
2637
212k
    const uint16_t num_friends = onion_get_friend_count(m->onion_c);
2638
2639
455k
    for (uint16_t i = 0; i < num_friends; ++i) {
2640
242k
        Onion_Friend *onion_friend = onion_get_friend(m->onion_c, i);
2641
2642
242k
        if (!onion_friend_is_groupchat(onion_friend)) {
2643
235k
            continue;
2644
235k
        }
2645
2646
6.64k
        GC_Chat *chat = gc_get_group_by_public_key(m->group_handler, onion_friend_get_gc_public_key(onion_friend));
2647
2648
6.64k
        if (chat == nullptr) {
2649
0
            continue;
2650
0
        }
2651
2652
6.64k
        if (chat->update_self_announces) {
2653
276
            self_announce_group(m, chat, onion_friend);
2654
276
        }
2655
6.64k
    }
2656
212k
}
2657
2658
/** @brief The main loop that needs to be run at least 20 times per second. */
2659
void do_messenger(Messenger *m, void *userdata)
2660
212k
{
2661
    // Add the TCP relays, but only if this is the first time calling do_messenger
2662
212k
    if (!m->has_added_relays) {
2663
1.84k
        m->has_added_relays = true;
2664
2665
1.84k
        for (uint16_t i = 0; i < m->num_loaded_relays; ++i) {
2666
2
            add_tcp_relay(m->net_crypto, &m->loaded_relays[i].ip_port, m->loaded_relays[i].public_key);
2667
2
        }
2668
2669
1.84k
        m->num_loaded_relays = 0;
2670
2671
1.84k
        if (m->tcp_server != nullptr) {
2672
            /* Add self tcp server. */
2673
14
            IP_Port local_ip_port;
2674
14
            local_ip_port.port = net_htons(m->options.tcp_server_port);
2675
14
            local_ip_port.ip.family = net_family_ipv4();
2676
14
            local_ip_port.ip.ip.v4 = get_ip4_loopback();
2677
14
            add_tcp_relay(m->net_crypto, &local_ip_port, tcp_server_public_key(m->tcp_server));
2678
14
        }
2679
1.84k
    }
2680
2681
212k
    if (!m->options.udp_disabled) {
2682
146k
        networking_poll(m->net, userdata);
2683
146k
        do_dht(m->dht);
2684
146k
    }
2685
2686
212k
    if (m->tcp_server != nullptr) {
2687
2.36k
        do_tcp_server(m->tcp_server, m->mono_time);
2688
2.36k
    }
2689
2690
212k
    do_net_crypto(m->net_crypto, userdata);
2691
212k
    do_onion_client(m->onion_c);
2692
212k
    do_friend_connections(m->fr_c, userdata);
2693
212k
    do_friends(m, userdata);
2694
212k
    do_gc(m->group_handler, userdata);
2695
212k
    do_gca(m->mono_time, m->group_announce);
2696
212k
    do_gc_onion_friends(m);
2697
212k
    m_connection_status_callback(m, userdata);
2698
2699
212k
    if (mono_time_get(m->mono_time) > m->lastdump + DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS) {
2700
2.10k
        m->lastdump = mono_time_get(m->mono_time);
2701
2.10k
        uint32_t last_pinged;
2702
2703
2.15M
        for (uint32_t client = 0; client < LCLIENT_LIST; ++client) {
2704
2.15M
            const Client_data *cptr = dht_get_close_client(m->dht, client);
2705
2.15M
            const IPPTsPng *const assocs[] = { &cptr->assoc4, &cptr->assoc6, nullptr };
2706
2707
6.46M
            for (const IPPTsPng * const *it = assocs; *it != nullptr; ++it) {
2708
4.31M
                const IPPTsPng *const assoc = *it;
2709
2710
4.31M
                if (ip_isset(&assoc->ip_port.ip)) {
2711
897
                    last_pinged = m->lastdump - assoc->last_pinged;
2712
2713
897
                    if (last_pinged > 999) {
2714
0
                        last_pinged = 999;
2715
0
                    }
2716
2717
897
                    Ip_Ntoa ip_str;
2718
897
                    char id_str[IDSTRING_LEN];
2719
897
                    LOGGER_TRACE(m->log, "C[%2u] %s:%u [%3u] %s",
2720
897
                                 client, net_ip_ntoa(&assoc->ip_port.ip, &ip_str),
2721
897
                                 net_ntohs(assoc->ip_port.port), last_pinged,
2722
897
                                 id_to_string(cptr->public_key, id_str, sizeof(id_str)));
2723
897
                }
2724
4.31M
            }
2725
2.15M
        }
2726
2727
2728
        /* dht contains additional "friends" (requests) */
2729
2.10k
        const uint32_t num_dhtfriends = dht_get_num_friends(m->dht);
2730
2.10k
        VLA(int32_t, m2dht, num_dhtfriends);
2731
2.10k
        VLA(int32_t, dht2m, num_dhtfriends);
2732
2733
6.71k
        for (uint32_t friend_idx = 0; friend_idx < num_dhtfriends; ++friend_idx) {
2734
4.61k
            m2dht[friend_idx] = -1;
2735
4.61k
            dht2m[friend_idx] = -1;
2736
2737
4.61k
            if (friend_idx >= m->numfriends) {
2738
3.23k
                continue;
2739
3.23k
            }
2740
2741
5.25k
            for (uint32_t dhtfriend = 0; dhtfriend < dht_get_num_friends(m->dht); ++dhtfriend) {
2742
3.87k
                if (pk_equal(m->friendlist[friend_idx].real_pk, dht_get_friend_public_key(m->dht, dhtfriend))) {
2743
0
                    assert(dhtfriend < INT32_MAX);
2744
0
                    m2dht[friend_idx] = (int32_t)dhtfriend;
2745
0
                    break;
2746
0
                }
2747
3.87k
            }
2748
1.38k
        }
2749
2750
6.71k
        for (uint32_t friend_idx = 0; friend_idx < num_dhtfriends; ++friend_idx) {
2751
4.61k
            if (m2dht[friend_idx] >= 0) {
2752
0
                assert(friend_idx < INT32_MAX);
2753
0
                dht2m[m2dht[friend_idx]] = (int32_t)friend_idx;
2754
0
            }
2755
4.61k
        }
2756
2757
2.10k
        if (m->numfriends != dht_get_num_friends(m->dht)) {
2758
1.99k
            LOGGER_TRACE(m->log, "Friend num in DHT %u != friend num in msger %u", dht_get_num_friends(m->dht), m->numfriends);
2759
1.99k
        }
2760
2761
6.71k
        for (uint32_t friend_idx = 0; friend_idx < num_dhtfriends; ++friend_idx) {
2762
4.61k
            const Friend *const msgfptr = dht2m[friend_idx] >= 0 ?  &m->friendlist[dht2m[friend_idx]] : nullptr;
2763
4.61k
            const DHT_Friend *const dhtfptr = dht_get_friend(m->dht, friend_idx);
2764
2765
4.61k
            if (msgfptr != nullptr) {
2766
0
                char id_str[IDSTRING_LEN];
2767
0
                LOGGER_TRACE(m->log, "F[%2u:%2u] <%s> %s",
2768
0
                             dht2m[friend_idx], friend_idx, msgfptr->name,
2769
0
                             id_to_string(msgfptr->real_pk, id_str, sizeof(id_str)));
2770
4.61k
            } else {
2771
4.61k
                char id_str[IDSTRING_LEN];
2772
4.61k
                LOGGER_TRACE(m->log, "F[--:%2u] %s", friend_idx,
2773
4.61k
                             id_to_string(dht_friend_public_key(dhtfptr), id_str, sizeof(id_str)));
2774
4.61k
            }
2775
2776
41.4k
            for (uint32_t client = 0; client < MAX_FRIEND_CLIENTS; ++client) {
2777
36.8k
                const Client_data *cptr = dht_friend_client(dhtfptr, client);
2778
36.8k
                const IPPTsPng *const assocs[] = {&cptr->assoc4, &cptr->assoc6};
2779
2780
110k
                for (size_t a = 0; a < sizeof(assocs) / sizeof(assocs[0]); ++a) {
2781
73.7k
                    const IPPTsPng *const assoc = assocs[a];
2782
2783
73.7k
                    if (ip_isset(&assoc->ip_port.ip)) {
2784
3.98k
                        last_pinged = m->lastdump - assoc->last_pinged;
2785
2786
3.98k
                        if (last_pinged > 999) {
2787
0
                            last_pinged = 999;
2788
0
                        }
2789
2790
3.98k
                        Ip_Ntoa ip_str;
2791
3.98k
                        char id_str[IDSTRING_LEN];
2792
3.98k
                        LOGGER_TRACE(m->log, "F[%2u] => C[%2u] %s:%u [%3u] %s",
2793
3.98k
                                     friend_idx, client, net_ip_ntoa(&assoc->ip_port.ip, &ip_str),
2794
3.98k
                                     net_ntohs(assoc->ip_port.port), last_pinged,
2795
3.98k
                                     id_to_string(cptr->public_key, id_str, sizeof(id_str)));
2796
3.98k
                    }
2797
73.7k
                }
2798
36.8k
            }
2799
4.61k
        }
2800
2.10k
    }
2801
212k
}
2802
2803
/** new messenger format for load/save, more robust and forward compatible */
2804
2805
25
#define SAVED_FRIEND_REQUEST_SIZE 1024
2806
4.12k
#define NUM_SAVED_PATH_NODES 8
2807
2808
struct Saved_Friend {
2809
    uint8_t status;
2810
    uint8_t real_pk[CRYPTO_PUBLIC_KEY_SIZE];
2811
    uint8_t info[SAVED_FRIEND_REQUEST_SIZE]; // the data that is sent during the friend requests we do.
2812
    uint16_t info_size; // Length of the info.
2813
    uint8_t name[MAX_NAME_LENGTH];
2814
    uint16_t name_length;
2815
    uint8_t statusmessage[MAX_STATUSMESSAGE_LENGTH];
2816
    uint16_t statusmessage_length;
2817
    uint8_t userstatus;
2818
    uint32_t friendrequest_nospam;
2819
    uint8_t last_seen_time[sizeof(uint64_t)];
2820
};
2821
2822
static uint32_t friend_size(void)
2823
4.46k
{
2824
4.46k
    uint32_t data = 0;
2825
4.46k
    const struct Saved_Friend *const temp = nullptr;
2826
2827
4.46k
#define VALUE_MEMBER(data, name) \
2828
26.7k
    do {                         \
2829
26.7k
        data += sizeof(name);    \
2830
26.7k
    } while (0)
2831
4.46k
#define ARRAY_MEMBER(data, name) \
2832
22.3k
    do {                         \
2833
22.3k
        data += sizeof(name);    \
2834
22.3k
    } while (0)
2835
2836
    // Exactly the same in friend_load, friend_save, and friend_size
2837
4.46k
    VALUE_MEMBER(data, temp->status);
2838
4.46k
    ARRAY_MEMBER(data, temp->real_pk);
2839
4.46k
    ARRAY_MEMBER(data, temp->info);
2840
4.46k
    ++data; // padding
2841
4.46k
    VALUE_MEMBER(data, temp->info_size);
2842
4.46k
    ARRAY_MEMBER(data, temp->name);
2843
4.46k
    VALUE_MEMBER(data, temp->name_length);
2844
4.46k
    ARRAY_MEMBER(data, temp->statusmessage);
2845
4.46k
    ++data; // padding
2846
4.46k
    VALUE_MEMBER(data, temp->statusmessage_length);
2847
4.46k
    VALUE_MEMBER(data, temp->userstatus);
2848
4.46k
    data += 3; // padding
2849
4.46k
    VALUE_MEMBER(data, temp->friendrequest_nospam);
2850
4.46k
    ARRAY_MEMBER(data, temp->last_seen_time);
2851
2852
4.46k
#undef VALUE_MEMBER
2853
4.46k
#undef ARRAY_MEMBER
2854
2855
4.46k
    return data;
2856
4.46k
}
2857
2858
non_null()
2859
static uint8_t *friend_save(const struct Saved_Friend *temp, uint8_t *data)
2860
179
{
2861
179
#define VALUE_MEMBER(data, name)           \
2862
1.07k
    do {                                   \
2863
1.07k
        memcpy(data, &name, sizeof(name)); \
2864
1.07k
        data += sizeof(name);              \
2865
1.07k
    } while (0)
2866
2867
179
#define ARRAY_MEMBER(data, name)          \
2868
895
    do {                                  \
2869
895
        memcpy(data, name, sizeof(name)); \
2870
895
        data += sizeof(name);             \
2871
895
    } while (0)
2872
2873
    // Exactly the same in friend_load, friend_save, and friend_size
2874
179
    VALUE_MEMBER(data, temp->status);
2875
179
    ARRAY_MEMBER(data, temp->real_pk);
2876
179
    ARRAY_MEMBER(data, temp->info);
2877
179
    ++data; // padding
2878
179
    VALUE_MEMBER(data, temp->info_size);
2879
179
    ARRAY_MEMBER(data, temp->name);
2880
179
    VALUE_MEMBER(data, temp->name_length);
2881
179
    ARRAY_MEMBER(data, temp->statusmessage);
2882
179
    ++data; // padding
2883
179
    VALUE_MEMBER(data, temp->statusmessage_length);
2884
179
    VALUE_MEMBER(data, temp->userstatus);
2885
179
    data += 3; // padding
2886
179
    VALUE_MEMBER(data, temp->friendrequest_nospam);
2887
179
    ARRAY_MEMBER(data, temp->last_seen_time);
2888
2889
179
#undef VALUE_MEMBER
2890
179
#undef ARRAY_MEMBER
2891
2892
179
    return data;
2893
179
}
2894
2895
2896
non_null()
2897
static const uint8_t *friend_load(struct Saved_Friend *temp, const uint8_t *data)
2898
383
{
2899
383
#define VALUE_MEMBER(data, name)           \
2900
2.29k
    do {                                   \
2901
2.29k
        memcpy(&name, data, sizeof(name)); \
2902
2.29k
        data += sizeof(name);              \
2903
2.29k
    } while (0)
2904
2905
383
#define ARRAY_MEMBER(data, name)          \
2906
1.91k
    do {                                  \
2907
1.91k
        memcpy(name, data, sizeof(name)); \
2908
1.91k
        data += sizeof(name);             \
2909
1.91k
    } while (0)
2910
2911
    // Exactly the same in friend_load, friend_save, and friend_size
2912
383
    VALUE_MEMBER(data, temp->status);
2913
383
    ARRAY_MEMBER(data, temp->real_pk);
2914
383
    ARRAY_MEMBER(data, temp->info);
2915
383
    ++data; // padding
2916
383
    VALUE_MEMBER(data, temp->info_size);
2917
383
    ARRAY_MEMBER(data, temp->name);
2918
383
    VALUE_MEMBER(data, temp->name_length);
2919
383
    ARRAY_MEMBER(data, temp->statusmessage);
2920
383
    ++data; // padding
2921
383
    VALUE_MEMBER(data, temp->statusmessage_length);
2922
383
    VALUE_MEMBER(data, temp->userstatus);
2923
383
    data += 3; // padding
2924
383
    VALUE_MEMBER(data, temp->friendrequest_nospam);
2925
383
    ARRAY_MEMBER(data, temp->last_seen_time);
2926
2927
383
#undef VALUE_MEMBER
2928
383
#undef ARRAY_MEMBER
2929
2930
383
    return data;
2931
383
}
2932
2933
2934
non_null()
2935
static uint32_t m_state_plugins_size(const Messenger *m)
2936
1.91k
{
2937
1.91k
    const uint32_t size32 = sizeof(uint32_t);
2938
1.91k
    const uint32_t sizesubhead = size32 * 2;
2939
2940
1.91k
    uint32_t size = 0;
2941
2942
1.91k
    for (const Messenger_State_Plugin *plugin = m->options.state_plugins;
2943
19.1k
            plugin != m->options.state_plugins + m->options.state_plugins_length;
2944
17.2k
            ++plugin) {
2945
17.2k
        size += sizesubhead + plugin->size(m);
2946
17.2k
    }
2947
2948
1.91k
    return size;
2949
1.91k
}
2950
2951
/** @brief Registers a state plugin for saving, loading, and getting the size of a section of the save.
2952
 *
2953
 * @retval true on success
2954
 * @retval false on error
2955
 */
2956
bool m_register_state_plugin(Messenger *m, State_Type type, m_state_size_cb *size_callback,
2957
                             m_state_load_cb *load_callback,
2958
                             m_state_save_cb *save_callback)
2959
32.9k
{
2960
32.9k
    const uint32_t new_length = m->options.state_plugins_length + 1;
2961
32.9k
    Messenger_State_Plugin *temp = (Messenger_State_Plugin *)mem_vrealloc(
2962
32.9k
            m->mem, m->options.state_plugins, new_length, sizeof(Messenger_State_Plugin));
2963
2964
32.9k
    if (temp == nullptr) {
2965
3.21k
        return false;
2966
3.21k
    }
2967
2968
29.7k
    m->options.state_plugins = temp;
2969
29.7k
    m->options.state_plugins_length = new_length;
2970
2971
29.7k
    const uint8_t index = m->options.state_plugins_length - 1;
2972
29.7k
    m->options.state_plugins[index].type = type;
2973
29.7k
    m->options.state_plugins[index].size = size_callback;
2974
29.7k
    m->options.state_plugins[index].load = load_callback;
2975
29.7k
    m->options.state_plugins[index].save = save_callback;
2976
2977
29.7k
    return true;
2978
32.9k
}
2979
2980
non_null()
2981
static uint32_t m_plugin_size(const Messenger *m, State_Type type)
2982
6.42k
{
2983
22.1k
    for (uint8_t i = 0; i < m->options.state_plugins_length; ++i) {
2984
22.1k
        const Messenger_State_Plugin plugin = m->options.state_plugins[i];
2985
2986
22.1k
        if (plugin.type == type) {
2987
6.42k
            return plugin.size(m);
2988
6.42k
        }
2989
22.1k
    }
2990
2991
0
    LOGGER_ERROR(m->log, "Unknown type encountered: %u", type);
2992
2993
0
    return UINT32_MAX;
2994
6.42k
}
2995
2996
/** return size of the messenger data (for saving). */
2997
uint32_t messenger_size(const Messenger *m)
2998
1.91k
{
2999
1.91k
    return m_state_plugins_size(m);
3000
1.91k
}
3001
3002
/** Save the messenger in data (must be allocated memory of size at least `Messenger_size()`) */
3003
uint8_t *messenger_save(const Messenger *m, uint8_t *data)
3004
958
{
3005
9.58k
    for (uint8_t i = 0; i < m->options.state_plugins_length; ++i) {
3006
8.62k
        const Messenger_State_Plugin plugin = m->options.state_plugins[i];
3007
8.62k
        data = plugin.save(m, data);
3008
8.62k
    }
3009
3010
958
    return data;
3011
958
}
3012
3013
// nospam state plugin
3014
non_null()
3015
static uint32_t nospam_keys_size(const Messenger *m)
3016
3.33k
{
3017
3.33k
    return sizeof(uint32_t) + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_SECRET_KEY_SIZE;
3018
3.33k
}
3019
3020
non_null()
3021
static State_Load_Status load_nospam_keys(Messenger *m, const uint8_t *data, uint32_t length)
3022
460
{
3023
460
    if (length != m_plugin_size(m, STATE_TYPE_NOSPAMKEYS)) {
3024
1
        return STATE_LOAD_STATUS_ERROR;
3025
1
    }
3026
3027
459
    uint32_t nospam;
3028
459
    lendian_bytes_to_host32(&nospam, data);
3029
459
    set_nospam(m->fr, nospam);
3030
459
    load_secret_key(m->net_crypto, data + sizeof(uint32_t) + CRYPTO_PUBLIC_KEY_SIZE);
3031
3032
459
    if (!pk_equal(data + sizeof(uint32_t), nc_get_self_public_key(m->net_crypto))) {
3033
5
        LOGGER_ERROR(m->log, "public key stored in savedata does not match its secret key");
3034
5
        return STATE_LOAD_STATUS_ERROR;
3035
5
    }
3036
3037
454
    return STATE_LOAD_STATUS_CONTINUE;
3038
459
}
3039
3040
non_null()
3041
static uint8_t *save_nospam_keys(const Messenger *m, uint8_t *data)
3042
958
{
3043
958
    const uint32_t len = m_plugin_size(m, STATE_TYPE_NOSPAMKEYS);
3044
958
    static_assert(sizeof(get_nospam(m->fr)) == sizeof(uint32_t), "nospam doesn't fit in a 32 bit int");
3045
958
    data = state_write_section_header(data, STATE_COOKIE_TYPE, len, STATE_TYPE_NOSPAMKEYS);
3046
958
    const uint32_t nospam = get_nospam(m->fr);
3047
958
    host_to_lendian_bytes32(data, nospam);
3048
958
    save_keys(m->net_crypto, data + sizeof(uint32_t));
3049
958
    data += len;
3050
958
    return data;
3051
958
}
3052
3053
// DHT state plugin
3054
non_null()
3055
static uint32_t m_dht_size(const Messenger *m)
3056
2.87k
{
3057
2.87k
    return dht_size(m->dht);
3058
2.87k
}
3059
3060
non_null()
3061
static uint8_t *save_dht(const Messenger *m, uint8_t *data)
3062
958
{
3063
958
    const uint32_t len = m_plugin_size(m, STATE_TYPE_DHT);
3064
958
    data = state_write_section_header(data, STATE_COOKIE_TYPE, len, STATE_TYPE_DHT);
3065
958
    dht_save(m->dht, data);
3066
958
    data += len;
3067
958
    return data;
3068
958
}
3069
3070
non_null()
3071
static State_Load_Status m_dht_load(Messenger *m, const uint8_t *data, uint32_t length)
3072
568
{
3073
568
    dht_load(m->dht, data, length); // TODO(endoffile78): Should we throw an error if dht_load fails?
3074
568
    return STATE_LOAD_STATUS_CONTINUE;
3075
568
}
3076
3077
// friendlist state plugin
3078
non_null()
3079
static uint32_t saved_friendslist_size(const Messenger *m)
3080
2.87k
{
3081
2.87k
    return count_friendlist(m) * friend_size();
3082
2.87k
}
3083
3084
non_null()
3085
static uint8_t *friends_list_save(const Messenger *m, uint8_t *data)
3086
958
{
3087
958
    const uint32_t len = m_plugin_size(m, STATE_TYPE_FRIENDS);
3088
958
    data = state_write_section_header(data, STATE_COOKIE_TYPE, len, STATE_TYPE_FRIENDS);
3089
3090
958
    uint32_t num = 0;
3091
958
    uint8_t *cur_data = data;
3092
3093
1.13k
    for (uint32_t i = 0; i < m->numfriends; ++i) {
3094
179
        if (m->friendlist[i].status > 0) {
3095
179
            struct Saved_Friend temp = { 0 };
3096
179
            temp.status = m->friendlist[i].status;
3097
179
            memcpy(temp.real_pk, m->friendlist[i].real_pk, CRYPTO_PUBLIC_KEY_SIZE);
3098
3099
179
            if (temp.status < 3) {
3100
                // TODO(iphydf): Use uint16_t and min_u16 here.
3101
25
                const size_t friendrequest_length =
3102
25
                    min_u32(m->friendlist[i].info_size,
3103
25
                            min_u32(SAVED_FRIEND_REQUEST_SIZE, MAX_FRIEND_REQUEST_DATA_SIZE));
3104
25
                memcpy(temp.info, m->friendlist[i].info, friendrequest_length);
3105
3106
25
                temp.info_size = net_htons(m->friendlist[i].info_size);
3107
25
                temp.friendrequest_nospam = m->friendlist[i].friendrequest_nospam;
3108
154
            } else {
3109
154
                temp.status = 3;
3110
154
                memcpy(temp.name, m->friendlist[i].name, m->friendlist[i].name_length);
3111
154
                temp.name_length = net_htons(m->friendlist[i].name_length);
3112
154
                memcpy(temp.statusmessage, m->friendlist[i].statusmessage, m->friendlist[i].statusmessage_length);
3113
154
                temp.statusmessage_length = net_htons(m->friendlist[i].statusmessage_length);
3114
154
                temp.userstatus = m->friendlist[i].userstatus;
3115
3116
154
                net_pack_u64(temp.last_seen_time, m->friendlist[i].last_seen_time);
3117
154
            }
3118
3119
179
            uint8_t *next_data = friend_save(&temp, cur_data);
3120
179
            assert(next_data - cur_data == friend_size());
3121
179
#ifdef __LP64__
3122
179
            assert(memcmp(cur_data, &temp, friend_size()) == 0);
3123
179
#endif /* __LP64__ */
3124
179
            cur_data = next_data;
3125
179
            ++num;
3126
179
        }
3127
179
    }
3128
3129
958
    assert(cur_data - data == num * friend_size());
3130
958
    data += len;
3131
3132
958
    return data;
3133
958
}
3134
3135
non_null()
3136
static State_Load_Status friends_list_load(Messenger *m, const uint8_t *data, uint32_t length)
3137
272
{
3138
272
    const uint32_t l_friend_size = friend_size();
3139
3140
272
    if (length % l_friend_size != 0) {
3141
1
        return STATE_LOAD_STATUS_ERROR; // TODO(endoffile78): error or continue?
3142
1
    }
3143
3144
271
    const uint32_t num = length / l_friend_size;
3145
271
    const uint8_t *cur_data = data;
3146
3147
654
    for (uint32_t i = 0; i < num; ++i) {
3148
383
        struct Saved_Friend temp = { 0 };
3149
383
        const uint8_t *next_data = friend_load(&temp, cur_data);
3150
383
        assert(next_data - cur_data == l_friend_size);
3151
3152
383
        cur_data = next_data;
3153
3154
383
        if (temp.status >= 3) {
3155
227
            const int fnum = m_addfriend_norequest(m, temp.real_pk);
3156
3157
227
            if (fnum < 0) {
3158
42
                continue;
3159
42
            }
3160
3161
185
            setfriendname(m, fnum, temp.name, net_ntohs(temp.name_length));
3162
185
            set_friend_statusmessage(m, fnum, temp.statusmessage, net_ntohs(temp.statusmessage_length));
3163
185
            set_friend_userstatus(m, fnum, temp.userstatus);
3164
185
            net_unpack_u64(temp.last_seen_time, &m->friendlist[fnum].last_seen_time);
3165
185
        } else if (temp.status != 0) {
3166
            /* TODO(irungentoo): This is not a good way to do this. */
3167
120
            uint8_t address[FRIEND_ADDRESS_SIZE];
3168
120
            pk_copy(address, temp.real_pk);
3169
120
            memcpy(address + CRYPTO_PUBLIC_KEY_SIZE, &temp.friendrequest_nospam, sizeof(uint32_t));
3170
120
            uint16_t checksum = data_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum));
3171
120
            memcpy(address + CRYPTO_PUBLIC_KEY_SIZE + sizeof(uint32_t), &checksum, sizeof(checksum));
3172
120
            m_addfriend(m, address, temp.info, net_ntohs(temp.info_size));
3173
120
        }
3174
383
    }
3175
3176
271
    return STATE_LOAD_STATUS_CONTINUE;
3177
271
}
3178
3179
non_null()
3180
static void pack_groupchats(const GC_Session *c, Bin_Pack *bp)
3181
2.35k
{
3182
2.35k
    assert(bp != nullptr && c != nullptr);
3183
2.35k
    bin_pack_array(bp, gc_count_groups(c));
3184
3185
6.25k
    for (uint32_t i = 0; i < c->chats_index; ++i) { // this loop must match the one in gc_count_groups()
3186
3.90k
        const GC_Chat *chat = &c->chats[i];
3187
3188
3.90k
        if (!gc_group_is_valid(chat)) {
3189
1.25k
            continue;
3190
1.25k
        }
3191
3192
2.64k
        gc_group_save(chat, bp);
3193
2.64k
    }
3194
2.35k
}
3195
3196
non_null()
3197
static bool pack_groupchats_handler(const void *obj, const Logger *logger, Bin_Pack *bp)
3198
2.35k
{
3199
2.35k
    const GC_Session *session = (const GC_Session *)obj;
3200
2.35k
    pack_groupchats(session, bp);
3201
2.35k
    return true;  // TODO(iphydf): Return bool from pack functions.
3202
2.35k
}
3203
3204
non_null()
3205
static uint32_t saved_groups_size(const Messenger *m)
3206
2.13k
{
3207
2.13k
    const GC_Session *session = m->group_handler;
3208
2.13k
    return bin_pack_obj_size(pack_groupchats_handler, session, m->log);
3209
2.13k
}
3210
3211
non_null()
3212
static uint8_t *groups_save(const Messenger *m, uint8_t *data)
3213
958
{
3214
958
    const GC_Session *c = m->group_handler;
3215
3216
958
    const uint32_t num_groups = gc_count_groups(c);
3217
3218
958
    if (num_groups == 0) {
3219
740
        return data;
3220
740
    }
3221
3222
218
    const uint32_t len = m_plugin_size(m, STATE_TYPE_GROUPS);
3223
3224
218
    if (len == 0) {
3225
0
        return data;
3226
0
    }
3227
3228
218
    data = state_write_section_header(data, STATE_COOKIE_TYPE, len, STATE_TYPE_GROUPS);
3229
3230
218
    if (!bin_pack_obj(pack_groupchats_handler, c, m->log, data, len)) {
3231
0
        LOGGER_FATAL(m->log, "failed to pack group chats into buffer of length %u", len);
3232
0
        return data;
3233
0
    }
3234
3235
218
    data += len;
3236
3237
218
    LOGGER_DEBUG(m->log, "Saved %u groups (length %u)", num_groups, len);
3238
3239
218
    return data;
3240
218
}
3241
3242
non_null()
3243
static bool handle_groups_load(void *obj, Bin_Unpack *bu)
3244
14.1k
{
3245
14.1k
    Messenger *m = (Messenger *)obj;
3246
3247
14.1k
    uint32_t num_groups;
3248
14.1k
    if (!bin_unpack_array(bu, &num_groups)) {
3249
47
        LOGGER_ERROR(m->log, "msgpack failed to unpack groupchats array: expected array");
3250
47
        return false;
3251
47
    }
3252
3253
14.1k
    LOGGER_DEBUG(m->log, "Loading %u groups", num_groups);
3254
3255
14.2k
    for (uint32_t i = 0; i < num_groups; ++i) {
3256
14.2k
        const int group_number = gc_group_load(m->group_handler, bu);
3257
3258
14.2k
        if (group_number < 0) {
3259
14.0k
            LOGGER_WARNING(m->log, "Failed to load group %u", i);
3260
            // Can't recover trivially. We may need to skip over some data here.
3261
14.0k
            break;
3262
14.0k
        }
3263
14.2k
    }
3264
3265
14.1k
    LOGGER_DEBUG(m->log, "Successfully loaded %u groups", gc_count_groups(m->group_handler));
3266
3267
14.1k
    return true;
3268
14.1k
}
3269
3270
non_null()
3271
static State_Load_Status groups_load(Messenger *m, const uint8_t *data, uint32_t length)
3272
14.1k
{
3273
14.1k
    if (!bin_unpack_obj(handle_groups_load, m, data, length)) {
3274
47
        LOGGER_ERROR(m->log, "msgpack failed to unpack groupchats array");
3275
47
        return STATE_LOAD_STATUS_ERROR;
3276
47
    }
3277
3278
14.1k
    return STATE_LOAD_STATUS_CONTINUE;
3279
14.1k
}
3280
3281
// name state plugin
3282
non_null()
3283
static uint32_t name_size(const Messenger *m)
3284
2.87k
{
3285
2.87k
    return m->name_length;
3286
2.87k
}
3287
3288
non_null()
3289
static uint8_t *save_name(const Messenger *m, uint8_t *data)
3290
958
{
3291
958
    const uint32_t len = m_plugin_size(m, STATE_TYPE_NAME);
3292
958
    data = state_write_section_header(data, STATE_COOKIE_TYPE, len, STATE_TYPE_NAME);
3293
958
    memcpy(data, m->name, len);
3294
958
    data += len;
3295
958
    return data;
3296
958
}
3297
3298
non_null()
3299
static State_Load_Status load_name(Messenger *m, const uint8_t *data, uint32_t length)
3300
440
{
3301
440
    if (length > 0 && length <= MAX_NAME_LENGTH) {
3302
287
        setname(m, data, length);
3303
287
    }
3304
3305
440
    return STATE_LOAD_STATUS_CONTINUE;
3306
440
}
3307
3308
// status message state plugin
3309
non_null()
3310
static uint32_t status_message_size(const Messenger *m)
3311
2.87k
{
3312
2.87k
    return m->statusmessage_length;
3313
2.87k
}
3314
3315
non_null()
3316
static uint8_t *save_status_message(const Messenger *m, uint8_t *data)
3317
958
{
3318
958
    const uint32_t len = m_plugin_size(m, STATE_TYPE_STATUSMESSAGE);
3319
958
    data = state_write_section_header(data, STATE_COOKIE_TYPE, len, STATE_TYPE_STATUSMESSAGE);
3320
958
    memcpy(data, m->statusmessage, len);
3321
958
    data += len;
3322
958
    return data;
3323
958
}
3324
3325
non_null()
3326
static State_Load_Status load_status_message(Messenger *m, const uint8_t *data, uint32_t length)
3327
696
{
3328
696
    if (length > 0 && length <= MAX_STATUSMESSAGE_LENGTH) {
3329
592
        m_set_statusmessage(m, data, length);
3330
592
    }
3331
3332
696
    return STATE_LOAD_STATUS_CONTINUE;
3333
696
}
3334
3335
// status state plugin
3336
non_null()
3337
static uint32_t status_size(const Messenger *m)
3338
2.87k
{
3339
2.87k
    return 1;
3340
2.87k
}
3341
3342
non_null()
3343
static uint8_t *save_status(const Messenger *m, uint8_t *data)
3344
958
{
3345
958
    const uint32_t len = m_plugin_size(m, STATE_TYPE_STATUS);
3346
958
    data = state_write_section_header(data, STATE_COOKIE_TYPE, len, STATE_TYPE_STATUS);
3347
958
    *data = m->userstatus;
3348
958
    data += len;
3349
958
    return data;
3350
958
}
3351
3352
non_null()
3353
static State_Load_Status load_status(Messenger *m, const uint8_t *data, uint32_t length)
3354
561
{
3355
561
    if (length == 1) {
3356
525
        m_set_userstatus(m, *data);
3357
525
    }
3358
3359
561
    return STATE_LOAD_STATUS_CONTINUE;
3360
561
}
3361
3362
// TCP Relay state plugin
3363
non_null()
3364
static uint32_t tcp_relay_size(const Messenger *m)
3365
1.91k
{
3366
1.91k
    return NUM_SAVED_TCP_RELAYS * packed_node_size(net_family_tcp_ipv6());
3367
1.91k
}
3368
3369
non_null()
3370
static uint8_t *save_tcp_relays(const Messenger *m, uint8_t *data)
3371
958
{
3372
958
    Node_format relays[NUM_SAVED_TCP_RELAYS] = {{{0}}};
3373
958
    uint8_t *temp_data = data;
3374
958
    data = state_write_section_header(temp_data, STATE_COOKIE_TYPE, 0, STATE_TYPE_TCP_RELAY);
3375
3376
958
    if (m->num_loaded_relays > 0) {
3377
24
        memcpy(relays, m->loaded_relays, sizeof(Node_format) * m->num_loaded_relays);
3378
24
    }
3379
3380
958
    uint32_t num = m->num_loaded_relays;
3381
958
    num += copy_connected_tcp_relays(m->net_crypto, relays + num, NUM_SAVED_TCP_RELAYS - num);
3382
3383
958
    const int l = pack_nodes(m->log, data, NUM_SAVED_TCP_RELAYS * packed_node_size(net_family_tcp_ipv6()), relays, num);
3384
3385
958
    if (l > 0) {
3386
26
        const uint32_t len = l;
3387
26
        data = state_write_section_header(temp_data, STATE_COOKIE_TYPE, len, STATE_TYPE_TCP_RELAY);
3388
26
        data += len;
3389
26
    }
3390
3391
958
    return data;
3392
958
}
3393
3394
non_null()
3395
static State_Load_Status load_tcp_relays(Messenger *m, const uint8_t *data, uint32_t length)
3396
613
{
3397
613
    if (length > 0) {
3398
518
        const int num = unpack_nodes(m->loaded_relays, NUM_SAVED_TCP_RELAYS, nullptr, data, length, true);
3399
3400
518
        if (num == -1) {
3401
137
            m->num_loaded_relays = 0;
3402
137
            return STATE_LOAD_STATUS_CONTINUE;
3403
137
        }
3404
3405
381
        m->num_loaded_relays = num;
3406
381
        m->has_added_relays = false;
3407
381
    }
3408
3409
476
    return STATE_LOAD_STATUS_CONTINUE;
3410
613
}
3411
3412
// path node state plugin
3413
non_null()
3414
static uint32_t path_node_size(const Messenger *m)
3415
1.91k
{
3416
1.91k
    return NUM_SAVED_PATH_NODES * packed_node_size(net_family_tcp_ipv6());
3417
1.91k
}
3418
3419
non_null()
3420
static uint8_t *save_path_nodes(const Messenger *m, uint8_t *data)
3421
958
{
3422
958
    Node_format nodes[NUM_SAVED_PATH_NODES] = {{{0}}};
3423
958
    uint8_t *temp_data = data;
3424
958
    data = state_write_section_header(data, STATE_COOKIE_TYPE, 0, STATE_TYPE_PATH_NODE);
3425
958
    const unsigned int num = onion_backup_nodes(m->onion_c, nodes, NUM_SAVED_PATH_NODES);
3426
958
    const int l = pack_nodes(m->log, data, NUM_SAVED_PATH_NODES * packed_node_size(net_family_tcp_ipv6()), nodes, num);
3427
3428
958
    if (l > 0) {
3429
82
        const uint32_t len = l;
3430
82
        data = state_write_section_header(temp_data, STATE_COOKIE_TYPE, len, STATE_TYPE_PATH_NODE);
3431
82
        data += len;
3432
82
    }
3433
3434
958
    return data;
3435
958
}
3436
3437
non_null()
3438
static State_Load_Status load_path_nodes(Messenger *m, const uint8_t *data, uint32_t length)
3439
363
{
3440
363
    if (length > 0) {
3441
289
        Node_format nodes[NUM_SAVED_PATH_NODES];
3442
289
        const int num = unpack_nodes(nodes, NUM_SAVED_PATH_NODES, nullptr, data, length, false);
3443
3444
289
        if (num == -1) {
3445
102
            return STATE_LOAD_STATUS_CONTINUE;
3446
102
        }
3447
3448
1.54k
        for (int i = 0; i < num; ++i) {
3449
1.35k
            onion_add_bs_path_node(m->onion_c, &nodes[i].ip_port, nodes[i].public_key);
3450
1.35k
        }
3451
187
    }
3452
3453
261
    return STATE_LOAD_STATUS_CONTINUE;
3454
363
}
3455
3456
non_null()
3457
static void m_register_default_plugins(Messenger *m)
3458
3.66k
{
3459
3.66k
    m_register_state_plugin(m, STATE_TYPE_NOSPAMKEYS, nospam_keys_size, load_nospam_keys, save_nospam_keys);
3460
3.66k
    m_register_state_plugin(m, STATE_TYPE_DHT, m_dht_size, m_dht_load, save_dht);
3461
3.66k
    m_register_state_plugin(m, STATE_TYPE_FRIENDS, saved_friendslist_size, friends_list_load, friends_list_save);
3462
3.66k
    m_register_state_plugin(m, STATE_TYPE_NAME, name_size, load_name, save_name);
3463
3.66k
    m_register_state_plugin(m, STATE_TYPE_STATUSMESSAGE, status_message_size, load_status_message,
3464
3.66k
                            save_status_message);
3465
3.66k
    m_register_state_plugin(m, STATE_TYPE_STATUS, status_size, load_status, save_status);
3466
3.66k
    m_register_state_plugin(m, STATE_TYPE_GROUPS, saved_groups_size, groups_load, groups_save);
3467
3.66k
    m_register_state_plugin(m, STATE_TYPE_TCP_RELAY, tcp_relay_size, load_tcp_relays, save_tcp_relays);
3468
3.66k
    m_register_state_plugin(m, STATE_TYPE_PATH_NODE, path_node_size, load_path_nodes, save_path_nodes);
3469
3.66k
}
3470
3471
bool messenger_load_state_section(Messenger *m, const uint8_t *data, uint32_t length, uint16_t type,
3472
                                  State_Load_Status *status)
3473
18.7k
{
3474
124k
    for (uint8_t i = 0; i < m->options.state_plugins_length; ++i) {
3475
124k
        const Messenger_State_Plugin *const plugin = &m->options.state_plugins[i];
3476
3477
124k
        if (plugin->type == type) {
3478
18.1k
            *status = plugin->load(m, data, length);
3479
18.1k
            return true;
3480
18.1k
        }
3481
124k
    }
3482
3483
626
    return false;
3484
18.7k
}
3485
3486
/** @brief Return the number of friends in the instance m.
3487
 *
3488
 * You should use this to determine how much memory to allocate
3489
 * for copy_friendlist.
3490
 */
3491
uint32_t count_friendlist(const Messenger *m)
3492
52.0k
{
3493
52.0k
    uint32_t ret = 0;
3494
3495
125k
    for (uint32_t i = 0; i < m->numfriends; ++i) {
3496
73.3k
        if (m->friendlist[i].status > 0) {
3497
73.3k
            ++ret;
3498
73.3k
        }
3499
73.3k
    }
3500
3501
52.0k
    return ret;
3502
52.0k
}
3503
3504
/** @brief Copy a list of valid friend IDs into the array out_list.
3505
 * If out_list is NULL, returns 0.
3506
 * Otherwise, returns the number of elements copied.
3507
 * If the array was too small, the contents
3508
 * of out_list will be truncated to list_size.
3509
 */
3510
uint32_t copy_friendlist(Messenger const *m, uint32_t *out_list, uint32_t list_size)
3511
0
{
3512
0
    if (out_list == nullptr) {
3513
0
        return 0;
3514
0
    }
3515
3516
0
    if (m->numfriends == 0) {
3517
0
        return 0;
3518
0
    }
3519
3520
0
    uint32_t ret = 0;
3521
3522
0
    for (uint32_t i = 0; i < m->numfriends; ++i) {
3523
0
        if (ret >= list_size) {
3524
0
            break; /* Abandon ship */
3525
0
        }
3526
3527
0
        if (m->friendlist[i].status > 0) {
3528
0
            out_list[ret] = i;
3529
0
            ++ret;
3530
0
        }
3531
0
    }
3532
3533
0
    return ret;
3534
0
}
3535
3536
static fr_friend_request_cb m_handle_friend_request;
3537
non_null(1, 2, 3) nullable(5)
3538
static void m_handle_friend_request(
3539
    void *object, const uint8_t *public_key, const uint8_t *message, size_t length, void *user_data)
3540
179
{
3541
179
    Messenger *m = (Messenger *)object;
3542
179
    assert(m != nullptr);
3543
179
    m->friend_request(m, public_key, message, length, user_data);
3544
179
}
3545
3546
/** @brief Run this at startup.
3547
 *
3548
 * @return allocated instance of Messenger on success.
3549
 * @retval 0 if there are problems.
3550
 *
3551
 * if error is not NULL it will be set to one of the values in the enum above.
3552
 */
3553
Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random *rng, const Network *ns,
3554
                         Messenger_Options *options, Messenger_Error *error)
3555
4.18k
{
3556
4.18k
    if (options == nullptr) {
3557
0
        return nullptr;
3558
0
    }
3559
3560
4.18k
    if (error != nullptr) {
3561
4.18k
        *error = MESSENGER_ERROR_OTHER;
3562
4.18k
    }
3563
3564
4.18k
    Messenger *m = (Messenger *)mem_alloc(mem, sizeof(Messenger));
3565
3566
4.18k
    if (m == nullptr) {
3567
13
        return nullptr;
3568
13
    }
3569
3570
4.17k
    m->mono_time = mono_time;
3571
4.17k
    m->mem = mem;
3572
4.17k
    m->rng = rng;
3573
4.17k
    m->ns = ns;
3574
3575
4.17k
    m->fr = friendreq_new();
3576
3577
4.17k
    if (m->fr == nullptr) {
3578
12
        mem_delete(mem, m);
3579
12
        return nullptr;
3580
12
    }
3581
3582
4.15k
    m->log = logger_new();
3583
3584
4.15k
    if (m->log == nullptr) {
3585
12
        friendreq_kill(m->fr);
3586
12
        mem_delete(mem, m);
3587
12
        return nullptr;
3588
12
    }
3589
3590
4.14k
    logger_callback_log(m->log, options->log_callback, options->log_context, options->log_user_data);
3591
3592
4.14k
    unsigned int net_err = 0;
3593
3594
4.14k
    if (!options->udp_disabled && options->proxy_info.proxy_type != TCP_PROXY_NONE) {
3595
        // We don't currently support UDP over proxy.
3596
181
        LOGGER_INFO(m->log, "UDP enabled and proxy set: disabling UDP");
3597
181
        options->udp_disabled = true;
3598
181
    }
3599
3600
4.14k
    if (options->udp_disabled) {
3601
225
        m->net = new_networking_no_udp(m->log, m->mem, m->ns);
3602
3.92k
    } else {
3603
3.92k
        IP ip;
3604
3.92k
        ip_init(&ip, options->ipv6enabled);
3605
3.92k
        m->net = new_networking_ex(m->log, m->mem, m->ns, &ip, options->port_range[0], options->port_range[1], &net_err);
3606
3.92k
    }
3607
3608
4.14k
    if (m->net == nullptr) {
3609
41
        friendreq_kill(m->fr);
3610
3611
41
        if (error != nullptr && net_err == 1) {
3612
27
            LOGGER_WARNING(m->log, "network initialisation failed (no ports available)");
3613
27
            *error = MESSENGER_ERROR_PORT;
3614
27
        }
3615
3616
41
        logger_kill(m->log);
3617
41
        mem_delete(mem, m);
3618
41
        return nullptr;
3619
41
    }
3620
3621
4.10k
    m->dht = new_dht(m->log, m->mem, m->rng, m->ns, m->mono_time, m->net, options->hole_punching_enabled, options->local_discovery_enabled);
3622
3623
4.10k
    if (m->dht == nullptr) {
3624
224
        kill_networking(m->net);
3625
224
        friendreq_kill(m->fr);
3626
224
        logger_kill(m->log);
3627
224
        mem_delete(mem, m);
3628
224
        return nullptr;
3629
224
    }
3630
3631
3.88k
    m->net_crypto = new_net_crypto(m->log, m->mem, m->rng, m->ns, m->mono_time, m->dht, &options->proxy_info);
3632
3633
3.88k
    if (m->net_crypto == nullptr) {
3634
82
        LOGGER_WARNING(m->log, "net_crypto initialisation failed");
3635
3636
82
        kill_dht(m->dht);
3637
82
        kill_networking(m->net);
3638
82
        friendreq_kill(m->fr);
3639
82
        logger_kill(m->log);
3640
82
        mem_delete(mem, m);
3641
82
        return nullptr;
3642
82
    }
3643
3644
3.79k
    m->group_announce = new_gca_list();
3645
3646
3.79k
    if (m->group_announce == nullptr) {
3647
20
        LOGGER_WARNING(m->log, "DHT group chats initialisation failed");
3648
3649
20
        kill_net_crypto(m->net_crypto);
3650
20
        kill_dht(m->dht);
3651
20
        kill_networking(m->net);
3652
20
        friendreq_kill(m->fr);
3653
20
        logger_kill(m->log);
3654
20
        mem_delete(mem, m);
3655
20
        return nullptr;
3656
20
    }
3657
3658
3.77k
    if (options->dht_announcements_enabled) {
3659
3.77k
        m->forwarding = new_forwarding(m->log, m->rng, m->mono_time, m->dht);
3660
3.77k
        if (m->forwarding != nullptr) {
3661
3.75k
            m->announce = new_announcements(m->log, m->mem, m->rng, m->mono_time, m->forwarding);
3662
3.75k
        } else {
3663
20
            m->announce = nullptr;
3664
20
        }
3665
3.77k
    } else {
3666
0
        m->forwarding = nullptr;
3667
0
        m->announce = nullptr;
3668
0
    }
3669
3670
3.77k
    m->onion = new_onion(m->log, m->mem, m->mono_time, m->rng, m->dht);
3671
3.77k
    m->onion_a = new_onion_announce(m->log, m->mem, m->rng, m->mono_time, m->dht);
3672
3.77k
    m->onion_c = new_onion_client(m->log, m->mem, m->rng, m->mono_time, m->net_crypto);
3673
3.77k
    if (m->onion_c != nullptr) {
3674
3.71k
        m->fr_c = new_friend_connections(m->log, m->mono_time, m->ns, m->onion_c, options->local_discovery_enabled);
3675
3.71k
    }
3676
3677
3.77k
    if ((options->dht_announcements_enabled && (m->forwarding == nullptr || m->announce == nullptr)) ||
3678
3.77k
            m->onion == nullptr || m->onion_a == nullptr || m->onion_c == nullptr || m->fr_c == nullptr) {
3679
92
        LOGGER_WARNING(m->log, "onion initialisation failed");
3680
3681
92
        kill_onion(m->onion);
3682
92
        kill_onion_announce(m->onion_a);
3683
92
        kill_onion_client(m->onion_c);
3684
92
        kill_gca(m->group_announce);
3685
92
        kill_friend_connections(m->fr_c);
3686
92
        kill_announcements(m->announce);
3687
92
        kill_forwarding(m->forwarding);
3688
92
        kill_net_crypto(m->net_crypto);
3689
92
        kill_dht(m->dht);
3690
92
        kill_networking(m->net);
3691
92
        friendreq_kill(m->fr);
3692
92
        logger_kill(m->log);
3693
92
        mem_delete(mem, m);
3694
92
        return nullptr;
3695
92
    }
3696
3697
3.68k
    gca_onion_init(m->group_announce, m->onion_a);
3698
3699
3.68k
    m->group_handler = new_dht_groupchats(m);
3700
3701
3.68k
    if (m->group_handler == nullptr) {
3702
20
        LOGGER_WARNING(m->log, "conferences initialisation failed");
3703
3704
20
        kill_onion(m->onion);
3705
20
        kill_onion_announce(m->onion_a);
3706
20
        kill_onion_client(m->onion_c);
3707
20
        kill_gca(m->group_announce);
3708
20
        kill_friend_connections(m->fr_c);
3709
20
        kill_announcements(m->announce);
3710
20
        kill_forwarding(m->forwarding);
3711
20
        kill_net_crypto(m->net_crypto);
3712
20
        kill_dht(m->dht);
3713
20
        kill_networking(m->net);
3714
20
        friendreq_kill(m->fr);
3715
20
        logger_kill(m->log);
3716
20
        mem_delete(mem, m);
3717
20
        return nullptr;
3718
20
    }
3719
3720
3.66k
    if (options->tcp_server_port != 0) {
3721
22
        m->tcp_server = new_tcp_server(m->log, m->mem, m->rng, m->ns, options->ipv6enabled, 1,
3722
22
                                       &options->tcp_server_port, dht_get_self_secret_key(m->dht),
3723
22
                                       m->onion, m->forwarding);
3724
3725
22
        if (m->tcp_server == nullptr) {
3726
2
            LOGGER_WARNING(m->log, "TCP server initialisation failed");
3727
3728
2
            kill_onion(m->onion);
3729
2
            kill_onion_announce(m->onion_a);
3730
2
            kill_dht_groupchats(m->group_handler);
3731
2
            kill_friend_connections(m->fr_c);
3732
2
            kill_onion_client(m->onion_c);
3733
2
            kill_gca(m->group_announce);
3734
2
            kill_announcements(m->announce);
3735
2
            kill_forwarding(m->forwarding);
3736
2
            kill_net_crypto(m->net_crypto);
3737
2
            kill_dht(m->dht);
3738
2
            kill_networking(m->net);
3739
2
            friendreq_kill(m->fr);
3740
2
            logger_kill(m->log);
3741
2
            mem_delete(mem, m);
3742
3743
2
            if (error != nullptr) {
3744
2
                *error = MESSENGER_ERROR_TCP_SERVER;
3745
2
            }
3746
3747
2
            return nullptr;
3748
2
        }
3749
22
    }
3750
3751
3.66k
    m->options = *options;
3752
3.66k
    friendreq_init(m->fr, m->fr_c);
3753
3.66k
    set_nospam(m->fr, random_u32(m->rng));
3754
3.66k
    set_filter_function(m->fr, &friend_already_added, m);
3755
3756
3.66k
    m->lastdump = 0;
3757
3.66k
    m->is_receiving_file = 0;
3758
3759
3.66k
    m_register_default_plugins(m);
3760
3.66k
    callback_friendrequest(m->fr, m_handle_friend_request, m);
3761
3762
3.66k
    if (error != nullptr) {
3763
3.66k
        *error = MESSENGER_ERROR_NONE;
3764
3.66k
    }
3765
3766
3.66k
    return m;
3767
3.66k
}
3768
3769
/** @brief Run this before closing shop.
3770
 *
3771
 * Free all datastructures.
3772
 */
3773
void kill_messenger(Messenger *m)
3774
2.61k
{
3775
2.61k
    if (m == nullptr) {
3776
0
        return;
3777
0
    }
3778
3779
2.61k
    if (m->tcp_server != nullptr) {
3780
20
        kill_tcp_server(m->tcp_server);
3781
20
    }
3782
3783
2.61k
    kill_onion(m->onion);
3784
2.61k
    kill_onion_announce(m->onion_a);
3785
2.61k
    kill_dht_groupchats(m->group_handler);
3786
2.61k
    kill_friend_connections(m->fr_c);
3787
2.61k
    kill_onion_client(m->onion_c);
3788
2.61k
    kill_gca(m->group_announce);
3789
2.61k
    kill_announcements(m->announce);
3790
2.61k
    kill_forwarding(m->forwarding);
3791
2.61k
    kill_net_crypto(m->net_crypto);
3792
2.61k
    kill_dht(m->dht);
3793
2.61k
    kill_networking(m->net);
3794
3795
3.74k
    for (uint32_t i = 0; i < m->numfriends; ++i) {
3796
1.12k
        clear_receipts(m, i);
3797
1.12k
    }
3798
3799
2.61k
    mem_delete(m->mem, m->friendlist);
3800
2.61k
    friendreq_kill(m->fr);
3801
3802
2.61k
    mem_delete(m->mem, m->options.state_plugins);
3803
2.61k
    logger_kill(m->log);
3804
2.61k
    mem_delete(m->mem, m);
3805
2.61k
}
3806
3807
bool m_is_receiving_file(Messenger *m)
3808
25.5k
{
3809
    // Only run the expensive loop below once every 64 tox_iterate calls.
3810
25.5k
    const uint8_t skip_count = 64;
3811
3812
25.5k
    if (m->is_receiving_file != 0) {
3813
7.61k
        --m->is_receiving_file;
3814
7.61k
        return true;
3815
7.61k
    }
3816
3817
    // TODO(iphydf): This is a very expensive loop. Consider keeping track of
3818
    // the number of live file transfers.
3819
27.2k
    for (size_t friend_number = 0; friend_number < m->numfriends; ++friend_number) {
3820
2.39M
        for (size_t i = 0; i < MAX_CONCURRENT_FILE_PIPES; ++i) {
3821
2.38M
            if (m->friendlist[friend_number].file_receiving[i].status == FILESTATUS_TRANSFERRING) {
3822
119
                m->is_receiving_file = skip_count;
3823
119
                return true;
3824
119
            }
3825
2.38M
        }
3826
9.43k
    }
3827
3828
17.7k
    return false;
3829
17.9k
}