Coverage Report

Created: 2026-02-04 10:53

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