Coverage Report

Created: 2026-01-21 10:41

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-2025 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.60k
{
64
1.60k
    const DHT *dht = (const DHT *)obj;
65
1.60k
    return dht_get_self_secret_key(dht);
66
1.60k
}
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
329
{
90
329
    if (num == 0) {
91
0
        mem_delete(m->mem, m->friendlist);
92
0
        m->friendlist = nullptr;
93
0
        return 0;
94
0
    }
95
96
329
    Friend *newfriendlist = (Friend *)mem_vrealloc(m->mem, m->friendlist, num, sizeof(Friend));
97
98
329
    if (newfriendlist == nullptr) {
99
0
        return -1;
100
0
    }
101
102
329
    m->friendlist = newfriendlist;
103
329
    return 0;
104
329
}
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.32k
{
111
13.8k
    for (uint32_t i = 0; i < m->numfriends; ++i) {
112
5.84k
        if (m->friendlist[i].status > 0 && pk_equal(real_pk, m->friendlist[i].real_pk)) {
113
268
            return i;
114
268
        }
115
5.84k
    }
116
117
8.05k
    return -1;
118
8.32k
}
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
329
{
183
329
    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
329
    if (realloc_friendlist(m, m->numfriends + 1) != 0) {
191
0
        return FAERR_NOMEM;
192
0
    }
193
194
329
    m->friendlist[m->numfriends] = empty_friend;
195
196
329
    const int friendcon_id = new_friend_connection(m->fr_c, real_pk);
197
198
329
    if (friendcon_id == -1) {
199
0
        return FAERR_NOMEM;
200
0
    }
201
202
777
    for (uint32_t i = 0; i <= m->numfriends; ++i) {
203
777
        if (m->friendlist[i].status == NOFRIEND) {
204
329
            m->friendlist[i].status = status;
205
329
            m->friendlist[i].friendcon_id = friendcon_id;
206
329
            m->friendlist[i].friendrequest_lastsent = 0;
207
329
            pk_copy(m->friendlist[i].real_pk, real_pk);
208
329
            m->friendlist[i].statusmessage_length = 0;
209
329
            m->friendlist[i].userstatus = USERSTATUS_NONE;
210
329
            m->friendlist[i].is_typing = false;
211
329
            m->friendlist[i].message_id = 0;
212
329
            friend_connection_callbacks(m->fr_c, friendcon_id, MESSENGER_CALLBACK_INDEX, &m_handle_status, &m_handle_packet,
213
329
                                        &m_handle_lossy_packet, m, i);
214
215
329
            if (m->numfriends == i) {
216
329
                ++m->numfriends;
217
329
            }
218
219
329
            if (friend_con_connected(m->fr_c, friendcon_id) == FRIENDCONN_STATUS_CONNECTED) {
220
0
                send_online_packet(m, friendcon_id);
221
0
            }
222
223
329
            return i;
224
329
        }
225
777
    }
226
227
0
    return FAERR_NOMEM;
228
329
}
229
230
static int32_t m_add_friend_contact_norequest(Messenger *_Nonnull m, const uint8_t *_Nonnull real_pk)
231
296
{
232
296
    if (getfriend_id(m, real_pk) != -1) {
233
11
        return FAERR_ALREADYSENT;
234
11
    }
235
236
285
    if (pk_equal(real_pk, nc_get_self_public_key(m->net_crypto))) {
237
0
        return FAERR_OWNKEY;
238
0
    }
239
240
285
    return init_new_friend(m, real_pk, FRIEND_CONFIRMED);
241
285
}
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
129
{
266
129
    if (length > MAX_FRIEND_REQUEST_DATA_SIZE) {
267
11
        return FAERR_TOOLONG;
268
11
    }
269
270
118
    uint8_t real_pk[CRYPTO_PUBLIC_KEY_SIZE];
271
118
    pk_copy(real_pk, address);
272
273
118
    if (!public_key_valid(real_pk)) {
274
10
        return FAERR_BADCHECKSUM;
275
10
    }
276
277
108
    uint16_t check;
278
108
    const uint16_t checksum = data_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum));
279
108
    memcpy(&check, address + CRYPTO_PUBLIC_KEY_SIZE + sizeof(uint32_t), sizeof(check));
280
281
108
    if (check != checksum) {
282
0
        return FAERR_BADCHECKSUM;
283
0
    }
284
285
108
    if (length < 1) {
286
10
        return FAERR_NOMESSAGE;
287
10
    }
288
289
98
    if (pk_equal(real_pk, nc_get_self_public_key(m->net_crypto))) {
290
10
        return FAERR_OWNKEY;
291
10
    }
292
293
88
    const int32_t friend_id = getfriend_id(m, real_pk);
294
295
88
    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
44
    const int32_t ret = init_new_friend(m, real_pk, FRIEND_ADDED);
312
313
44
    if (ret < 0) {
314
0
        return ret;
315
0
    }
316
317
44
    m->friendlist[ret].friendrequest_timeout = FRIENDREQUEST_TIMEOUT;
318
44
    memcpy(m->friendlist[ret].info, data, length);
319
44
    m->friendlist[ret].info_size = length;
320
44
    memcpy(&m->friendlist[ret].friendrequest_nospam, address + CRYPTO_PUBLIC_KEY_SIZE, sizeof(uint32_t));
321
322
44
    return ret;
323
44
}
324
325
int32_t m_addfriend_norequest(Messenger *m, const uint8_t *real_pk)
326
337
{
327
337
    if (!public_key_valid(real_pk)) {
328
31
        return FAERR_BADCHECKSUM;
329
31
    }
330
331
306
    if (pk_equal(real_pk, nc_get_self_public_key(m->net_crypto))) {
332
10
        return FAERR_OWNKEY;
333
10
    }
334
335
296
    return m_add_friend_contact_norequest(m, real_pk);
336
306
}
337
338
static int clear_receipts(Messenger *_Nonnull m, int32_t friendnumber)
339
329
{
340
329
    if (!m_friend_exists(m, friendnumber)) {
341
0
        return -1;
342
0
    }
343
344
329
    struct Receipts *receipts = m->friendlist[friendnumber].receipts_start;
345
346
329
    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
329
    m->friendlist[friendnumber].receipts_start = nullptr;
353
329
    m->friendlist[friendnumber].receipts_end = nullptr;
354
329
    return 0;
355
329
}
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
85
{
398
85
    random_bytes(m->rng, chat->m_group_public_key, CRYPTO_PUBLIC_KEY_SIZE);
399
85
    const int friendcon_id = new_friend_connection(m->fr_c, chat->m_group_public_key);
400
401
85
    if (friendcon_id == -1) {
402
0
        return false;
403
0
    }
404
405
85
    const Friend_Conn *connection = get_conn(m->fr_c, friendcon_id);
406
407
85
    if (connection == nullptr) {
408
0
        return false;
409
0
    }
410
411
85
    chat->friend_connection_id = friendcon_id;
412
413
85
    if (friend_con_connected(m->fr_c, friendcon_id) == FRIENDCONN_STATUS_CONNECTED) {
414
0
        send_online_packet(m, friendcon_id);
415
0
    }
416
417
85
    const int onion_friend_number = friend_conn_get_onion_friendnum(connection);
418
85
    Onion_Friend *onion_friend = onion_get_friend(m->onion_c, (uint16_t)onion_friend_number);
419
420
85
    onion_friend_set_gc_public_key(onion_friend, get_chat_id(&chat->chat_public_key));
421
85
    onion_friend_set_gc_data(onion_friend, nullptr, 0);
422
423
85
    return true;
424
85
}
425
426
/**
427
 * Kills the friend connection for a groupchat.
428
 */
429
void m_kill_group_connection(Messenger *m, const GC_Chat *chat)
430
85
{
431
85
    remove_request_received(m->fr, chat->m_group_public_key);
432
433
85
    friend_connection_callbacks(m->fr_c, chat->friend_connection_id, MESSENGER_CALLBACK_INDEX, nullptr,
434
85
                                nullptr, nullptr, nullptr, 0);
435
436
85
    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
85
    kill_friend_connection(m->fr_c, chat->friend_connection_id);
441
85
}
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
899
{
559
899
    return (unsigned int)friendnumber < m->numfriends && m->friendlist[friendnumber].status != 0;
560
899
}
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
285
{
663
285
    if (!m_friend_exists(m, friendnumber)) {
664
0
        return -1;
665
0
    }
666
667
285
    if (length > MAX_NAME_LENGTH || length == 0) {
668
237
        return -1;
669
237
    }
670
671
48
    m->friendlist[friendnumber].name_length = length;
672
48
    memcpy(m->friendlist[friendnumber].name, name, length);
673
48
    return 0;
674
285
}
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
317
{
687
317
    if (length > MAX_NAME_LENGTH) {
688
0
        return -1;
689
0
    }
690
691
317
    if (m->name_length == length && (length == 0 || memcmp(name, m->name, length) == 0)) {
692
82
        return 0;
693
82
    }
694
695
235
    if (length > 0) {
696
235
        memcpy(m->name, name, length);
697
235
    }
698
699
235
    m->name_length = length;
700
701
563
    for (uint32_t i = 0; i < m->numfriends; ++i) {
702
328
        m->friendlist[i].name_sent = false;
703
328
    }
704
705
235
    return 0;
706
317
}
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
637
{
761
637
    if (length > MAX_STATUSMESSAGE_LENGTH) {
762
0
        return -1;
763
0
    }
764
765
637
    if (m->statusmessage_length == length && (length == 0 || memcmp(m->statusmessage, status, length) == 0)) {
766
168
        return 0;
767
168
    }
768
769
469
    if (length > 0) {
770
469
        memcpy(m->statusmessage, status, length);
771
469
    }
772
773
469
    m->statusmessage_length = length;
774
775
821
    for (uint32_t i = 0; i < m->numfriends; ++i) {
776
352
        m->friendlist[i].statusmessage_sent = false;
777
352
    }
778
779
469
    return 0;
780
637
}
781
782
static bool userstatus_from_int(uint8_t status, Userstatus *_Nonnull out_enum)
783
445
{
784
445
    switch (status) {
785
184
        case USERSTATUS_NONE: {
786
184
            *out_enum = USERSTATUS_NONE;
787
184
            return true;
788
0
        }
789
790
7
        case USERSTATUS_AWAY: {
791
7
            *out_enum = USERSTATUS_AWAY;
792
7
            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
173
        default: {
806
173
            *out_enum = USERSTATUS_INVALID;
807
173
            return false;
808
0
        }
809
445
    }
810
445
}
811
812
int m_set_userstatus(Messenger *m, uint8_t status)
813
574
{
814
574
    if (status >= USERSTATUS_INVALID) {
815
258
        return -1;
816
258
    }
817
818
316
    if (m->userstatus == status) {
819
156
        return 0;
820
156
    }
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
316
}
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
285
{
955
285
    if (!m_friend_exists(m, friendnumber)) {
956
0
        return -1;
957
0
    }
958
959
285
    if (length > MAX_STATUSMESSAGE_LENGTH) {
960
175
        return -1;
961
175
    }
962
963
110
    if (length > 0) {
964
57
        memcpy(m->friendlist[friendnumber].statusmessage, status, length);
965
57
    }
966
967
110
    m->friendlist[friendnumber].statusmessage_length = length;
968
110
    return 0;
969
285
}
970
971
static void set_friend_userstatus(const Messenger *_Nonnull m, int32_t friendnumber, uint8_t status)
972
285
{
973
285
    userstatus_from_int(status, &m->friendlist[friendnumber].userstatus);
974
285
}
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.05k
{
984
1.05k
    m->friend_request = function;
985
1.05k
}
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.05k
{
990
1.05k
    m->friend_message = function;
991
1.05k
}
992
993
void m_callback_namechange(Messenger *m, m_friend_name_cb *function)
994
1.05k
{
995
1.05k
    m->friend_namechange = function;
996
1.05k
}
997
998
void m_callback_statusmessage(Messenger *m, m_friend_status_message_cb *function)
999
1.05k
{
1000
1.05k
    m->friend_statusmessagechange = function;
1001
1.05k
}
1002
1003
void m_callback_userstatus(Messenger *m, m_friend_status_cb *function)
1004
1.05k
{
1005
1.05k
    m->friend_userstatuschange = function;
1006
1.05k
}
1007
1008
void m_callback_typingchange(Messenger *m, m_friend_typing_cb *function)
1009
1.05k
{
1010
1.05k
    m->friend_typingchange = function;
1011
1.05k
}
1012
1013
void m_callback_read_receipt(Messenger *m, m_friend_read_receipt_cb *function)
1014
1.05k
{
1015
1.05k
    m->read_receipt = function;
1016
1.05k
}
1017
1018
void m_callback_connectionstatus(Messenger *m, m_friend_connection_status_cb *function)
1019
1.05k
{
1020
1.05k
    m->friend_connectionstatuschange = function;
1021
1.05k
}
1022
1023
void m_callback_core_connection(Messenger *m, m_self_connection_status_cb *function)
1024
1.05k
{
1025
1.05k
    m->core_connection_change = function;
1026
1.05k
}
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.94k
{
1085
2.94k
    m->conference_invite = function;
1086
2.94k
}
1087
1088
/** @brief the callback for group invites. */
1089
void m_callback_group_invite(Messenger *m, m_group_invite_cb *function)
1090
1.05k
{
1091
1.05k
    m->group_invite = function;
1092
1.05k
}
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.05k
{
1118
1.05k
    m->file_sendrequest = function;
1119
1.05k
}
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.05k
{
1124
1.05k
    m->file_filecontrol = function;
1125
1.05k
}
1126
1127
/** @brief Set the callback for file data. */
1128
void callback_file_data(Messenger *m, m_file_recv_chunk_cb *function)
1129
1.05k
{
1130
1.05k
    m->file_filedata = function;
1131
1.05k
}
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.05k
{
1136
1.05k
    m->file_reqchunk = function;
1137
1.05k
}
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
/** @brief Send a file send request.
1188
 * Maximum filename length is 255 bytes.
1189
 * @retval 1 on success
1190
 * @retval 0 on failure
1191
 */
1192
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,
1193
                             const uint8_t *_Nonnull filename, uint16_t filename_length)
1194
0
{
1195
0
    if (!m_friend_exists(m, friendnumber)) {
1196
0
        return false;
1197
0
    }
1198
1199
0
    if (filename_length > MAX_FILENAME_LENGTH) {
1200
0
        return false;
1201
0
    }
1202
1203
0
    const uint16_t packet_size = 1 + sizeof(file_type) + sizeof(filesize) + FILE_ID_LENGTH + filename_length;
1204
0
    VLA(uint8_t, packet, packet_size);
1205
0
    packet[0] = filenumber;
1206
0
    file_type = net_htonl(file_type);
1207
0
    memcpy(packet + 1, &file_type, sizeof(file_type));
1208
0
    net_pack_u64(packet + 1 + sizeof(file_type), filesize);
1209
0
    memcpy(packet + 1 + sizeof(file_type) + sizeof(filesize), file_id, FILE_ID_LENGTH);
1210
1211
0
    if (filename_length > 0) {
1212
0
        memcpy(packet + 1 + sizeof(file_type) + sizeof(filesize) + FILE_ID_LENGTH, filename, filename_length);
1213
0
    }
1214
1215
0
    return write_cryptpacket_id(m, friendnumber, PACKET_ID_FILE_SENDREQUEST, packet, packet_size, false);
1216
0
}
1217
1218
/** @brief Send a file send request.
1219
 *
1220
 * Maximum filename length is 255 bytes.
1221
 *
1222
 * @return file number on success
1223
 * @retval -1 if friend not found.
1224
 * @retval -2 if filename length invalid.
1225
 * @retval -3 if no more file sending slots left.
1226
 * @retval -4 if could not send packet (friend offline).
1227
 */
1228
long int new_filesender(const Messenger *m, int32_t friendnumber, uint32_t file_type, uint64_t filesize,
1229
                        const uint8_t *file_id, const uint8_t *filename, uint16_t filename_length)
1230
0
{
1231
0
    if (!m_friend_exists(m, friendnumber)) {
1232
0
        return -1;
1233
0
    }
1234
1235
0
    if (filename_length > MAX_FILENAME_LENGTH) {
1236
0
        return -2;
1237
0
    }
1238
1239
0
    uint32_t i;
1240
1241
0
    for (i = 0; i < MAX_CONCURRENT_FILE_PIPES; ++i) {
1242
0
        if (m->friendlist[friendnumber].file_sending[i].status == FILESTATUS_NONE) {
1243
0
            break;
1244
0
        }
1245
0
    }
1246
1247
0
    if (i == MAX_CONCURRENT_FILE_PIPES) {
1248
0
        return -3;
1249
0
    }
1250
1251
0
    if (!file_sendrequest(m, friendnumber, i, file_type, filesize, file_id, filename, filename_length)) {
1252
0
        return -4;
1253
0
    }
1254
1255
0
    struct File_Transfers *ft = &m->friendlist[friendnumber].file_sending[i];
1256
1257
0
    ft->status = FILESTATUS_NOT_ACCEPTED;
1258
1259
0
    ft->size = filesize;
1260
1261
0
    ft->transferred = 0;
1262
1263
0
    ft->requested = 0;
1264
1265
0
    ft->paused = FILE_PAUSE_NOT;
1266
1267
0
    memcpy(ft->id, file_id, FILE_ID_LENGTH);
1268
1269
0
    return i;
1270
0
}
1271
1272
static bool send_file_control_packet(const Messenger *_Nonnull m, int32_t friendnumber, bool inbound, uint8_t filenumber,
1273
                                     uint8_t control_type, const uint8_t *_Nullable data, uint16_t data_length)
1274
0
{
1275
0
    assert(data_length == 0 || data != nullptr);
1276
0
    if ((unsigned int)(1 + 3 + data_length) > MAX_CRYPTO_DATA_SIZE) {
1277
0
        return false;
1278
0
    }
1279
1280
0
    const uint16_t packet_size = 3 + data_length;
1281
0
    VLA(uint8_t, packet, packet_size);
1282
1283
0
    packet[0] = inbound ? 1 : 0;
1284
0
    packet[1] = filenumber;
1285
0
    packet[2] = control_type;
1286
1287
0
    if (data_length > 0) {
1288
0
        memcpy(packet + 3, data, data_length);
1289
0
    }
1290
1291
0
    return write_cryptpacket_id(m, friendnumber, PACKET_ID_FILE_CONTROL, packet, packet_size, false);
1292
0
}
1293
1294
/** @brief Send a file control request.
1295
 *
1296
 * @retval 0 on success
1297
 * @retval -1 if friend not valid.
1298
 * @retval -2 if friend not online.
1299
 * @retval -3 if file number invalid.
1300
 * @retval -4 if file control is bad.
1301
 * @retval -5 if file already paused.
1302
 * @retval -6 if resume file failed because it was only paused by the other.
1303
 * @retval -7 if resume file failed because it wasn't paused.
1304
 * @retval -8 if packet failed to send.
1305
 */
1306
int file_control(const Messenger *m, int32_t friendnumber, uint32_t filenumber, unsigned int control)
1307
0
{
1308
0
    if (!m_friend_exists(m, friendnumber)) {
1309
0
        return -1;
1310
0
    }
1311
1312
0
    if (m->friendlist[friendnumber].status != FRIEND_ONLINE) {
1313
0
        return -2;
1314
0
    }
1315
1316
0
    uint32_t temp_filenum;
1317
0
    bool inbound;
1318
0
    uint8_t file_number;
1319
1320
0
    if (filenumber >= (1 << 16)) {
1321
0
        inbound = true;
1322
0
        temp_filenum = (filenumber >> 16) - 1;
1323
0
    } else {
1324
0
        inbound = false;
1325
0
        temp_filenum = filenumber;
1326
0
    }
1327
1328
0
    if (temp_filenum >= MAX_CONCURRENT_FILE_PIPES) {
1329
0
        return -3;
1330
0
    }
1331
1332
0
    file_number = temp_filenum;
1333
1334
0
    struct File_Transfers *ft;
1335
1336
0
    if (inbound) {
1337
0
        ft = &m->friendlist[friendnumber].file_receiving[file_number];
1338
0
    } else {
1339
0
        ft = &m->friendlist[friendnumber].file_sending[file_number];
1340
0
    }
1341
1342
0
    if (ft->status == FILESTATUS_NONE) {
1343
0
        return -3;
1344
0
    }
1345
1346
0
    if (control > FILECONTROL_KILL) {
1347
0
        return -4;
1348
0
    }
1349
1350
0
    if (control == FILECONTROL_PAUSE && ((ft->paused & FILE_PAUSE_US) != 0 || ft->status != FILESTATUS_TRANSFERRING)) {
1351
0
        return -5;
1352
0
    }
1353
1354
0
    if (control == FILECONTROL_ACCEPT) {
1355
0
        if (ft->status == FILESTATUS_TRANSFERRING) {
1356
0
            if ((ft->paused & FILE_PAUSE_US) == 0) {
1357
0
                if ((ft->paused & FILE_PAUSE_OTHER) != 0) {
1358
0
                    return -6;
1359
0
                }
1360
1361
0
                return -7;
1362
0
            }
1363
0
        } else {
1364
0
            if (ft->status != FILESTATUS_NOT_ACCEPTED) {
1365
0
                return -7;
1366
0
            }
1367
1368
0
            if (!inbound) {
1369
0
                return -6;
1370
0
            }
1371
0
        }
1372
0
    }
1373
1374
0
    if (send_file_control_packet(m, friendnumber, inbound, file_number, control, nullptr, 0)) {
1375
0
        switch (control) {
1376
0
            case FILECONTROL_KILL: {
1377
0
                if (!inbound && (ft->status == FILESTATUS_TRANSFERRING || ft->status == FILESTATUS_FINISHED)) {
1378
                    // We are actively sending that file, remove from list
1379
0
                    --m->friendlist[friendnumber].num_sending_files;
1380
0
                }
1381
1382
0
                ft->status = FILESTATUS_NONE;
1383
0
                break;
1384
0
            }
1385
0
            case FILECONTROL_PAUSE: {
1386
0
                ft->paused |= FILE_PAUSE_US;
1387
0
                break;
1388
0
            }
1389
0
            case FILECONTROL_ACCEPT: {
1390
0
                ft->status = FILESTATUS_TRANSFERRING;
1391
1392
0
                if ((ft->paused & FILE_PAUSE_US) != 0) {
1393
0
                    ft->paused ^= FILE_PAUSE_US;
1394
0
                }
1395
0
                break;
1396
0
            }
1397
0
        }
1398
0
    } else {
1399
0
        return -8;
1400
0
    }
1401
1402
0
    return 0;
1403
0
}
1404
1405
/** @brief Send a seek file control request.
1406
 *
1407
 * @retval 0 on success
1408
 * @retval -1 if friend not valid.
1409
 * @retval -2 if friend not online.
1410
 * @retval -3 if file number invalid.
1411
 * @retval -4 if not receiving file.
1412
 * @retval -5 if file status wrong.
1413
 * @retval -6 if position bad.
1414
 * @retval -8 if packet failed to send.
1415
 */
1416
int file_seek(const Messenger *m, int32_t friendnumber, uint32_t filenumber, uint64_t position)
1417
0
{
1418
0
    if (!m_friend_exists(m, friendnumber)) {
1419
0
        return -1;
1420
0
    }
1421
1422
0
    if (m->friendlist[friendnumber].status != FRIEND_ONLINE) {
1423
0
        return -2;
1424
0
    }
1425
1426
0
    if (filenumber < (1 << 16)) {
1427
        // Not receiving.
1428
0
        return -4;
1429
0
    }
1430
1431
0
    const uint32_t temp_filenum = (filenumber >> 16) - 1;
1432
1433
0
    if (temp_filenum >= MAX_CONCURRENT_FILE_PIPES) {
1434
0
        return -3;
1435
0
    }
1436
1437
0
    assert(temp_filenum <= UINT8_MAX);
1438
0
    const uint8_t file_number = temp_filenum;
1439
1440
    // We're always receiving at this point.
1441
0
    struct File_Transfers *ft = &m->friendlist[friendnumber].file_receiving[file_number];
1442
1443
0
    if (ft->status == FILESTATUS_NONE) {
1444
0
        return -3;
1445
0
    }
1446
1447
0
    if (ft->status != FILESTATUS_NOT_ACCEPTED) {
1448
0
        return -5;
1449
0
    }
1450
1451
0
    if (position >= ft->size) {
1452
0
        return -6;
1453
0
    }
1454
1455
0
    uint8_t sending_pos[sizeof(uint64_t)];
1456
0
    net_pack_u64(sending_pos, position);
1457
1458
0
    if (send_file_control_packet(m, friendnumber, true, file_number, FILECONTROL_SEEK, sending_pos,
1459
0
                                 sizeof(sending_pos))) {
1460
0
        ft->transferred = position;
1461
0
    } else {
1462
0
        return -8;
1463
0
    }
1464
1465
0
    return 0;
1466
0
}
1467
1468
/** @return packet number on success.
1469
 * @retval -1 on failure.
1470
 */
1471
static int64_t send_file_data_packet(const Messenger *_Nonnull m, int32_t friendnumber, uint8_t filenumber, const uint8_t *_Nullable data,
1472
                                     uint16_t length)
1473
0
{
1474
0
    assert(length == 0 || data != nullptr);
1475
0
    if (!m_friend_exists(m, friendnumber)) {
1476
0
        return -1;
1477
0
    }
1478
1479
0
    const uint16_t packet_size = 2 + length;
1480
0
    VLA(uint8_t, packet, packet_size);
1481
0
    packet[0] = PACKET_ID_FILE_DATA;
1482
0
    packet[1] = filenumber;
1483
1484
0
    if (length > 0) {
1485
0
        memcpy(packet + 2, data, length);
1486
0
    }
1487
1488
0
    return write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
1489
0
                             m->friendlist[friendnumber].friendcon_id), packet, packet_size, true);
1490
0
}
1491
1492
0
#define MAX_FILE_DATA_SIZE (MAX_CRYPTO_DATA_SIZE - 2)
1493
0
#define MIN_SLOTS_FREE (CRYPTO_MIN_QUEUE_LENGTH / 4)
1494
/** @brief Send file data.
1495
 *
1496
 * @retval 0 on success
1497
 * @retval -1 if friend not valid.
1498
 * @retval -2 if friend not online.
1499
 * @retval -3 if filenumber invalid.
1500
 * @retval -4 if file transfer not transferring.
1501
 * @retval -5 if bad data size.
1502
 * @retval -6 if packet queue full.
1503
 * @retval -7 if wrong position.
1504
 */
1505
int send_file_data(const Messenger *m, int32_t friendnumber, uint32_t filenumber, uint64_t position,
1506
                   const uint8_t *data, uint16_t length)
1507
0
{
1508
0
    assert(length == 0 || data != nullptr);
1509
1510
0
    if (!m_friend_exists(m, friendnumber)) {
1511
0
        return -1;
1512
0
    }
1513
1514
0
    if (m->friendlist[friendnumber].status != FRIEND_ONLINE) {
1515
0
        return -2;
1516
0
    }
1517
1518
0
    if (filenumber >= MAX_CONCURRENT_FILE_PIPES) {
1519
0
        return -3;
1520
0
    }
1521
1522
0
    struct File_Transfers *ft = &m->friendlist[friendnumber].file_sending[filenumber];
1523
1524
0
    if (ft->status != FILESTATUS_TRANSFERRING) {
1525
0
        return -4;
1526
0
    }
1527
1528
0
    if (length > MAX_FILE_DATA_SIZE) {
1529
0
        return -5;
1530
0
    }
1531
1532
0
    if (ft->size - ft->transferred < length) {
1533
0
        return -5;
1534
0
    }
1535
1536
0
    if (ft->size != UINT64_MAX && length != MAX_FILE_DATA_SIZE && (ft->transferred + length) != ft->size) {
1537
0
        return -5;
1538
0
    }
1539
1540
0
    if (position != ft->transferred || (ft->requested <= position && ft->size != 0)) {
1541
0
        return -7;
1542
0
    }
1543
1544
    /* Prevent file sending from filling up the entire buffer preventing messages from being sent.
1545
     * TODO(irungentoo): remove */
1546
0
    if (crypto_num_free_sendqueue_slots(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
1547
0
                                        m->friendlist[friendnumber].friendcon_id)) < MIN_SLOTS_FREE) {
1548
0
        return -6;
1549
0
    }
1550
1551
0
    const int64_t ret = send_file_data_packet(m, friendnumber, filenumber, data, length);
1552
1553
0
    if (ret != -1) {
1554
        // TODO(irungentoo): record packet ids to check if other received complete file.
1555
0
        ft->transferred += length;
1556
1557
0
        if (length != MAX_FILE_DATA_SIZE || ft->size == ft->transferred) {
1558
0
            ft->status = FILESTATUS_FINISHED;
1559
0
            ft->last_packet_number = ret;
1560
0
        }
1561
1562
0
        return 0;
1563
0
    }
1564
1565
0
    return -6;
1566
0
}
1567
1568
/**
1569
 * Iterate over all file transfers and request chunks (from the client) for each
1570
 * of them.
1571
 *
1572
 * The free_slots parameter is updated by this function.
1573
 *
1574
 * @param m Our messenger object.
1575
 * @param friendnumber The friend we're sending files to.
1576
 * @param userdata The client userdata to pass along to chunk request callbacks.
1577
 * @param free_slots A pointer to the number of free send queue slots in the
1578
 *   crypto connection.
1579
 * @return true if there's still work to do, false otherwise.
1580
 *
1581
 */
1582
static bool do_all_filetransfers(Messenger *_Nonnull m, int32_t friendnumber, void *_Nullable userdata, uint32_t *_Nonnull free_slots)
1583
0
{
1584
0
    Friend *const friendcon = &m->friendlist[friendnumber];
1585
1586
    // Iterate over file transfers as long as we're sending files
1587
0
    for (uint32_t i = 0; i < MAX_CONCURRENT_FILE_PIPES; ++i) {
1588
0
        if (friendcon->num_sending_files == 0) {
1589
            // no active file transfers anymore
1590
0
            return false;
1591
0
        }
1592
1593
0
        if (*free_slots == 0) {
1594
            // send buffer full enough
1595
0
            return false;
1596
0
        }
1597
1598
0
        struct File_Transfers *const ft = &friendcon->file_sending[i];
1599
1600
0
        if (ft->status == FILESTATUS_NONE || ft->status == FILESTATUS_NOT_ACCEPTED) {
1601
            // Filetransfers not actively sending, nothing to do
1602
0
            continue;
1603
0
        }
1604
1605
0
        if (max_speed_reached(m->net_crypto, friend_connection_crypt_connection_id(
1606
0
                                  m->fr_c, friendcon->friendcon_id))) {
1607
0
            LOGGER_DEBUG(m->log, "maximum connection speed reached");
1608
            // connection doesn't support any more data
1609
0
            return false;
1610
0
        }
1611
1612
        // If the file transfer is complete, we request a chunk of size 0.
1613
0
        if (ft->status == FILESTATUS_FINISHED && friend_received_packet(m, friendnumber, ft->last_packet_number) == 0) {
1614
0
            if (m->file_reqchunk != nullptr) {
1615
0
                m->file_reqchunk(m, friendnumber, i, ft->transferred, 0, userdata);
1616
0
            }
1617
1618
            // Now it's inactive, we're no longer sending this.
1619
0
            ft->status = FILESTATUS_NONE;
1620
0
            --friendcon->num_sending_files;
1621
0
        } else if (ft->status == FILESTATUS_TRANSFERRING && ft->paused == FILE_PAUSE_NOT) {
1622
0
            if (ft->size == 0) {
1623
                /* Send 0 data to friend if file is 0 length. */
1624
0
                send_file_data(m, friendnumber, i, 0, nullptr, 0);
1625
0
                continue;
1626
0
            }
1627
1628
0
            if (ft->size == ft->requested) {
1629
                // This file transfer is done.
1630
0
                continue;
1631
0
            }
1632
1633
0
            const uint16_t length = min_u64(ft->size - ft->requested, MAX_FILE_DATA_SIZE);
1634
0
            const uint64_t position = ft->requested;
1635
0
            ft->requested += length;
1636
1637
0
            if (m->file_reqchunk != nullptr) {
1638
0
                m->file_reqchunk(m, friendnumber, i, position, length, userdata);
1639
0
            }
1640
1641
            // The allocated slot is no longer free.
1642
0
            --*free_slots;
1643
0
        }
1644
0
    }
1645
1646
0
    return true;
1647
0
}
1648
1649
static void do_reqchunk_filecb(Messenger *_Nonnull m, int32_t friendnumber, void *_Nullable userdata)
1650
0
{
1651
    // We're not currently doing any file transfers.
1652
0
    if (m->friendlist[friendnumber].num_sending_files == 0) {
1653
0
        return;
1654
0
    }
1655
1656
    // The number of packet slots left in the sendbuffer.
1657
    // This is a per friend count (CRYPTO_PACKET_BUFFER_SIZE).
1658
0
    uint32_t free_slots = crypto_num_free_sendqueue_slots(
1659
0
                              m->net_crypto,
1660
0
                              friend_connection_crypt_connection_id(
1661
0
                                  m->fr_c,
1662
0
                                  m->friendlist[friendnumber].friendcon_id));
1663
1664
    // We keep MIN_SLOTS_FREE slots free for other packets, otherwise file
1665
    // transfers might block other traffic for a long time.
1666
0
    free_slots = max_s32(0, (int32_t)free_slots - MIN_SLOTS_FREE);
1667
1668
    // Maximum number of outer loops below. If the client doesn't send file
1669
    // chunks from within the chunk request callback handler, we never realise
1670
    // that the file transfer has finished and may end up in an infinite loop.
1671
    //
1672
    // Request up to that number of chunks per file from the client
1673
    //
1674
    // TODO(Jfreegman): set this cap dynamically
1675
0
    const uint32_t max_ft_loops = 128;
1676
1677
0
    for (uint32_t i = 0; i < max_ft_loops; ++i) {
1678
0
        if (!do_all_filetransfers(m, friendnumber, userdata, &free_slots)) {
1679
0
            break;
1680
0
        }
1681
1682
0
        if (free_slots == 0) {
1683
            // stop when the buffer is full enough
1684
0
            break;
1685
0
        }
1686
0
    }
1687
0
}
1688
1689
/** @brief Run this when the friend disconnects.
1690
 * Kill all current file transfers.
1691
 */
1692
static void break_files(const Messenger *m, int32_t friendnumber)
1693
0
{
1694
0
    Friend *const f = &m->friendlist[friendnumber];
1695
1696
    // TODO(irungentoo): Inform the client which file transfers get killed with a callback?
1697
0
    for (uint32_t i = 0; i < MAX_CONCURRENT_FILE_PIPES; ++i) {
1698
0
        f->file_sending[i].status = FILESTATUS_NONE;
1699
0
        f->file_receiving[i].status = FILESTATUS_NONE;
1700
0
    }
1701
0
}
1702
1703
static struct File_Transfers *get_file_transfer(bool outbound, uint8_t filenumber, uint32_t *_Nonnull real_filenumber, Friend *_Nonnull sender)
1704
0
{
1705
0
    struct File_Transfers *ft;
1706
1707
0
    if (outbound) {
1708
0
        *real_filenumber = filenumber;
1709
0
        ft = &sender->file_sending[filenumber];
1710
0
    } else {
1711
0
        *real_filenumber = (filenumber + 1) << 16;
1712
0
        ft = &sender->file_receiving[filenumber];
1713
0
    }
1714
1715
0
    if (ft->status == FILESTATUS_NONE) {
1716
0
        return nullptr;
1717
0
    }
1718
1719
0
    return ft;
1720
0
}
1721
1722
/** @retval -1 on failure
1723
 * @retval 0 on success.
1724
 */
1725
static int handle_filecontrol(Messenger *_Nonnull m, int32_t friendnumber, bool outbound, uint8_t filenumber,
1726
                              uint8_t control_type, const uint8_t *_Nonnull data, uint16_t length, void *_Nullable userdata)
1727
0
{
1728
0
    uint32_t real_filenumber;
1729
0
    struct File_Transfers *ft = get_file_transfer(outbound, filenumber, &real_filenumber, &m->friendlist[friendnumber]);
1730
1731
0
    if (ft == nullptr) {
1732
0
        LOGGER_DEBUG(m->log, "file control (friend %d, file %d): file transfer does not exist; telling the other to kill it",
1733
0
                     friendnumber, filenumber);
1734
0
        send_file_control_packet(m, friendnumber, !outbound, filenumber, FILECONTROL_KILL, nullptr, 0);
1735
0
        return -1;
1736
0
    }
1737
1738
0
    switch (control_type) {
1739
0
        case FILECONTROL_ACCEPT: {
1740
0
            if (outbound && ft->status == FILESTATUS_NOT_ACCEPTED) {
1741
0
                ft->status = FILESTATUS_TRANSFERRING;
1742
0
                ++m->friendlist[friendnumber].num_sending_files;
1743
0
            } else {
1744
0
                if ((ft->paused & FILE_PAUSE_OTHER) != 0) {
1745
0
                    ft->paused ^= FILE_PAUSE_OTHER;
1746
0
                } else {
1747
0
                    LOGGER_DEBUG(m->log, "file control (friend %d, file %d): friend told us to resume file transfer that wasn't paused",
1748
0
                                 friendnumber, filenumber);
1749
0
                    return -1;
1750
0
                }
1751
0
            }
1752
1753
0
            if (m->file_filecontrol != nullptr) {
1754
0
                m->file_filecontrol(m, friendnumber, real_filenumber, control_type, userdata);
1755
0
            }
1756
1757
0
            return 0;
1758
0
        }
1759
1760
0
        case FILECONTROL_PAUSE: {
1761
0
            if ((ft->paused & FILE_PAUSE_OTHER) != 0 || ft->status != FILESTATUS_TRANSFERRING) {
1762
0
                LOGGER_DEBUG(m->log, "file control (friend %d, file %d): friend told us to pause file transfer that is already paused",
1763
0
                             friendnumber, filenumber);
1764
0
                return -1;
1765
0
            }
1766
1767
0
            ft->paused |= FILE_PAUSE_OTHER;
1768
1769
0
            if (m->file_filecontrol != nullptr) {
1770
0
                m->file_filecontrol(m, friendnumber, real_filenumber, control_type, userdata);
1771
0
            }
1772
1773
0
            return 0;
1774
0
        }
1775
1776
0
        case FILECONTROL_KILL: {
1777
0
            if (m->file_filecontrol != nullptr) {
1778
0
                m->file_filecontrol(m, friendnumber, real_filenumber, control_type, userdata);
1779
0
            }
1780
1781
0
            if (outbound && (ft->status == FILESTATUS_TRANSFERRING || ft->status == FILESTATUS_FINISHED)) {
1782
0
                --m->friendlist[friendnumber].num_sending_files;
1783
0
            }
1784
1785
0
            ft->status = FILESTATUS_NONE;
1786
1787
0
            return 0;
1788
0
        }
1789
1790
0
        case FILECONTROL_SEEK: {
1791
0
            uint64_t position;
1792
1793
0
            if (length != sizeof(position)) {
1794
0
                LOGGER_DEBUG(m->log, "file control (friend %d, file %d): expected payload of length %u, but got %d",
1795
0
                             friendnumber, filenumber, (unsigned int)sizeof(position), length);
1796
0
                return -1;
1797
0
            }
1798
1799
            /* seek can only be sent by the receiver to seek before resuming broken transfers. */
1800
0
            if (ft->status != FILESTATUS_NOT_ACCEPTED || !outbound) {
1801
0
                LOGGER_DEBUG(m->log,
1802
0
                             "file control (friend %d, file %d): seek was either sent by a sender or by the receiver after accepting",
1803
0
                             friendnumber, filenumber);
1804
0
                return -1;
1805
0
            }
1806
1807
0
            net_unpack_u64(data, &position);
1808
1809
0
            if (position >= ft->size) {
1810
0
                LOGGER_DEBUG(m->log,
1811
0
                             "file control (friend %d, file %d): seek position %lu exceeds file size %lu",
1812
0
                             friendnumber, filenumber, (unsigned long)position, (unsigned long)ft->size);
1813
0
                return -1;
1814
0
            }
1815
1816
0
            ft->requested = position;
1817
0
            ft->transferred = position;
1818
0
            return 0;
1819
0
        }
1820
1821
0
        default: {
1822
0
            LOGGER_DEBUG(m->log, "file control (friend %d, file %d): invalid file control: %d",
1823
0
                         friendnumber, filenumber, control_type);
1824
0
            return -1;
1825
0
        }
1826
0
    }
1827
0
}
1828
1829
static int m_handle_lossy_packet(void *object, int friendcon_id, const uint8_t *data, uint16_t length,
1830
                                 void *userdata)
1831
0
{
1832
0
    Messenger *m = (Messenger *)object;
1833
1834
0
    if (!m_friend_exists(m, friendcon_id)) {
1835
0
        return 1;
1836
0
    }
1837
1838
0
    if (m->lossy_packethandler != nullptr) {
1839
0
        m->lossy_packethandler(m, friendcon_id, data[0], data, length, userdata);
1840
0
    }
1841
1842
0
    return 1;
1843
0
}
1844
1845
void custom_lossy_packet_registerhandler(Messenger *m, m_friend_lossy_packet_cb *lossy_packethandler)
1846
1.05k
{
1847
1.05k
    m->lossy_packethandler = lossy_packethandler;
1848
1.05k
}
1849
1850
int m_send_custom_lossy_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint32_t length)
1851
0
{
1852
0
    if (!m_friend_exists(m, friendnumber)) {
1853
0
        return -1;
1854
0
    }
1855
1856
0
    if (length == 0 || length > MAX_CRYPTO_DATA_SIZE) {
1857
0
        return -2;
1858
0
    }
1859
1860
0
    if (data[0] < PACKET_ID_RANGE_LOSSY_START || data[0] > PACKET_ID_RANGE_LOSSY_END) {
1861
0
        return -3;
1862
0
    }
1863
1864
0
    if (m->friendlist[friendnumber].status != FRIEND_ONLINE) {
1865
0
        return -4;
1866
0
    }
1867
1868
0
    if (send_lossy_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
1869
0
                               m->friendlist[friendnumber].friendcon_id), data, length) == -1) {
1870
0
        return -5;
1871
0
    }
1872
1873
0
    return 0;
1874
0
}
1875
1876
static int handle_custom_lossless_packet(void *_Nonnull object, int friend_num, const uint8_t *_Nonnull packet, uint16_t length,
1877
        void *_Nullable userdata)
1878
0
{
1879
0
    Messenger *m = (Messenger *)object;
1880
0
    if (!m_friend_exists(m, friend_num)) {
1881
0
        return -1;
1882
0
    }
1883
1884
0
    if (packet[0] < PACKET_ID_RANGE_LOSSLESS_CUSTOM_START || packet[0] > PACKET_ID_RANGE_LOSSLESS_CUSTOM_END) {
1885
        // allow PACKET_ID_MSI packets to be handled by custom packet handler
1886
0
        if (packet[0] != PACKET_ID_MSI) {
1887
0
            return -1;
1888
0
        }
1889
0
    }
1890
1891
0
    if (m->lossless_packethandler != nullptr) {
1892
0
        m->lossless_packethandler(m, friend_num, packet[0], packet, length, userdata);
1893
0
    }
1894
1895
0
    return 1;
1896
0
}
1897
1898
void custom_lossless_packet_registerhandler(Messenger *m, m_friend_lossless_packet_cb *lossless_packethandler)
1899
1.05k
{
1900
1.05k
    m->lossless_packethandler = lossless_packethandler;
1901
1.05k
}
1902
1903
int send_custom_lossless_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint32_t length)
1904
0
{
1905
0
    if (!m_friend_exists(m, friendnumber)) {
1906
0
        return -1;
1907
0
    }
1908
1909
0
    if (length == 0 || length > MAX_CRYPTO_DATA_SIZE) {
1910
0
        return -2;
1911
0
    }
1912
1913
0
    if ((data[0] < PACKET_ID_RANGE_LOSSLESS_CUSTOM_START || data[0] > PACKET_ID_RANGE_LOSSLESS_CUSTOM_END)
1914
0
            && data[0] != PACKET_ID_MSI) {
1915
0
        return -3;
1916
0
    }
1917
1918
0
    if (m->friendlist[friendnumber].status != FRIEND_ONLINE) {
1919
0
        return -4;
1920
0
    }
1921
1922
0
    if (write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
1923
0
                          m->friendlist[friendnumber].friendcon_id), data, length, true) == -1) {
1924
0
        return -5;
1925
0
    }
1926
1927
0
    return 0;
1928
0
}
1929
1930
/** Function to filter out some friend requests*/
1931
static int friend_already_added(void *_Nonnull object, const uint8_t *_Nonnull public_key)
1932
0
{
1933
0
    const Messenger *m = (const Messenger *)object;
1934
1935
0
    if (getfriend_id(m, public_key) == -1) {
1936
0
        return 0;
1937
0
    }
1938
1939
0
    return -1;
1940
0
}
1941
1942
/** @brief Check for and handle a timed-out friend request.
1943
 *
1944
 * If the request has timed-out then the friend status is set back to FRIEND_ADDED.
1945
 * @param friendcon_id friendlist index of the timed-out friend
1946
 * @param t time
1947
 */
1948
static void check_friend_request_timed_out(Messenger *_Nonnull m, uint32_t friendcon_id, uint64_t t, void *_Nullable userdata)
1949
0
{
1950
0
    Friend *f = &m->friendlist[friendcon_id];
1951
0
    if (f->friendrequest_lastsent + f->friendrequest_timeout < t) {
1952
0
        set_friend_status(m, friendcon_id, FRIEND_ADDED, userdata);
1953
        /* Double the default timeout every time if friendrequest is assumed
1954
         * to have been sent unsuccessfully.
1955
         */
1956
0
        f->friendrequest_timeout *= 2;
1957
0
    }
1958
0
}
1959
1960
static int m_handle_status(void *_Nonnull object, int friendcon_id, bool status, void *_Nullable userdata)
1961
0
{
1962
0
    Messenger *m = (Messenger *)object;
1963
0
    if (status) { /* Went online. */
1964
0
        send_online_packet(m, m->friendlist[friendcon_id].friendcon_id);
1965
0
    } else { /* Went offline. */
1966
0
        if (m->friendlist[friendcon_id].status == FRIEND_ONLINE) {
1967
0
            set_friend_status(m, friendcon_id, FRIEND_CONFIRMED, userdata);
1968
0
        }
1969
0
    }
1970
1971
0
    return 0;
1972
0
}
1973
1974
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)
1975
0
{
1976
0
    if (data_length == 0) {
1977
0
        set_friend_status(m, friendcon_id, FRIEND_CONFIRMED, userdata);
1978
0
    }
1979
1980
0
    return 0;
1981
0
}
1982
1983
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)
1984
0
{
1985
0
    if (data_length > MAX_NAME_LENGTH) {
1986
0
        return 0;
1987
0
    }
1988
1989
    /* Make sure the NULL terminator is present. */
1990
0
    VLA(uint8_t, data_terminated, data_length + 1);
1991
0
    memcpy(data_terminated, data, data_length);
1992
0
    data_terminated[data_length] = 0;
1993
1994
    /* inform of namechange before we overwrite the old name */
1995
0
    if (m->friend_namechange != nullptr) {
1996
0
        m->friend_namechange(m, friendcon_id, data_terminated, data_length, userdata);
1997
0
    }
1998
1999
0
    memcpy(m->friendlist[friendcon_id].name, data_terminated, data_length);
2000
0
    m->friendlist[friendcon_id].name_length = data_length;
2001
2002
0
    return 0;
2003
0
}
2004
2005
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)
2006
0
{
2007
0
    if (data_length > MAX_STATUSMESSAGE_LENGTH) {
2008
0
        return 0;
2009
0
    }
2010
2011
    /* Make sure the NULL terminator is present. */
2012
0
    VLA(uint8_t, data_terminated, data_length + 1);
2013
0
    memcpy(data_terminated, data, data_length);
2014
0
    data_terminated[data_length] = 0;
2015
2016
0
    if (m->friend_statusmessagechange != nullptr) {
2017
0
        m->friend_statusmessagechange(m, friendcon_id, data_terminated, data_length, userdata);
2018
0
    }
2019
2020
0
    set_friend_statusmessage(m, friendcon_id, data_terminated, data_length);
2021
2022
0
    return 0;
2023
0
}
2024
2025
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)
2026
0
{
2027
0
    if (data_length != 1) {
2028
0
        return 0;
2029
0
    }
2030
2031
0
    Userstatus status;
2032
0
    if (!userstatus_from_int(data[0], &status)) {
2033
0
        return 0;
2034
0
    }
2035
2036
0
    if (m->friend_userstatuschange != nullptr) {
2037
0
        m->friend_userstatuschange(m, friendcon_id, status, userdata);
2038
0
    }
2039
2040
0
    set_friend_userstatus(m, friendcon_id, status);
2041
2042
0
    return 0;
2043
0
}
2044
2045
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)
2046
0
{
2047
0
    if (data_length != 1) {
2048
0
        return 0;
2049
0
    }
2050
2051
0
    const bool typing = data[0] != 0;
2052
2053
0
    set_friend_typing(m, friendcon_id, typing);
2054
2055
0
    if (m->friend_typingchange != nullptr) {
2056
0
        m->friend_typingchange(m, friendcon_id, typing, userdata);
2057
0
    }
2058
2059
0
    return 0;
2060
0
}
2061
2062
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,
2063
                                   void *_Nullable userdata)
2064
0
{
2065
0
    if (data_length == 0) {
2066
0
        return 0;
2067
0
    }
2068
2069
0
    const uint8_t *message = data;
2070
0
    const uint16_t message_length = data_length;
2071
2072
    /* Make sure the NULL terminator is present. */
2073
0
    VLA(uint8_t, message_terminated, message_length + 1);
2074
0
    memcpy(message_terminated, message, message_length);
2075
0
    message_terminated[message_length] = 0;
2076
2077
0
    if (m->friend_message != nullptr) {
2078
0
        m->friend_message(m, friendcon_id, message_type, message_terminated, message_length, userdata);
2079
0
    }
2080
2081
0
    return 0;
2082
0
}
2083
2084
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)
2085
0
{
2086
0
    if (data_length == 0) {
2087
0
        return 0;
2088
0
    }
2089
2090
0
    if (m->conference_invite != nullptr) {
2091
0
        m->conference_invite(m, friendcon_id, data, data_length, userdata);
2092
0
    }
2093
2094
0
    return 0;
2095
0
}
2096
2097
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)
2098
0
{
2099
0
    const unsigned int head_length = 1 + sizeof(uint32_t) + sizeof(uint64_t) + FILE_ID_LENGTH;
2100
0
    if (data_length < head_length) {
2101
0
        return 0;
2102
0
    }
2103
2104
0
    const uint8_t filenumber = data[0];
2105
2106
#if UINT8_MAX >= MAX_CONCURRENT_FILE_PIPES
2107
2108
    if (filenumber >= MAX_CONCURRENT_FILE_PIPES) {
2109
        return 0;
2110
    }
2111
2112
#endif /* UINT8_MAX >= MAX_CONCURRENT_FILE_PIPES */
2113
2114
0
    uint64_t filesize;
2115
0
    uint32_t file_type;
2116
0
    const uint16_t filename_length = data_length - head_length;
2117
2118
0
    if (filename_length > MAX_FILENAME_LENGTH) {
2119
0
        return 0;
2120
0
    }
2121
2122
0
    memcpy(&file_type, data + 1, sizeof(file_type));
2123
0
    file_type = net_ntohl(file_type);
2124
2125
0
    net_unpack_u64(data + 1 + sizeof(uint32_t), &filesize);
2126
0
    struct File_Transfers *ft = &m->friendlist[friendcon_id].file_receiving[filenumber];
2127
2128
0
    if (ft->status != FILESTATUS_NONE) {
2129
0
        return 0;
2130
0
    }
2131
2132
0
    ft->status = FILESTATUS_NOT_ACCEPTED;
2133
0
    ft->size = filesize;
2134
0
    ft->transferred = 0;
2135
0
    ft->paused = FILE_PAUSE_NOT;
2136
0
    memcpy(ft->id, data + 1 + sizeof(uint32_t) + sizeof(uint64_t), FILE_ID_LENGTH);
2137
2138
0
    VLA(uint8_t, filename_terminated, filename_length + 1);
2139
0
    const uint8_t *filename = nullptr;
2140
2141
0
    if (filename_length > 0) {
2142
        /* Force NULL terminate file name. */
2143
0
        memcpy(filename_terminated, data + head_length, filename_length);
2144
0
        filename_terminated[filename_length] = 0;
2145
0
        filename = filename_terminated;
2146
0
    }
2147
2148
0
    uint32_t real_filenumber = filenumber;
2149
0
    real_filenumber += 1;
2150
0
    real_filenumber <<= 16;
2151
2152
0
    if (m->file_sendrequest != nullptr) {
2153
0
        m->file_sendrequest(m, friendcon_id, real_filenumber, file_type, filesize, filename, filename_length,
2154
0
                            userdata);
2155
0
    }
2156
2157
0
    return 0;
2158
0
}
2159
2160
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)
2161
0
{
2162
0
    if (data_length < 3) {
2163
0
        return 0;
2164
0
    }
2165
2166
    // On the other side, "outbound" is "inbound", i.e. if they send 1,
2167
    // that means "inbound" on their side, but we call it "outbound"
2168
    // here.
2169
0
    const bool outbound = data[0] == 1;
2170
0
    const uint8_t filenumber = data[1];
2171
0
    const uint8_t control_type = data[2];
2172
2173
#if UINT8_MAX >= MAX_CONCURRENT_FILE_PIPES
2174
2175
    if (filenumber >= MAX_CONCURRENT_FILE_PIPES) {
2176
        return 0;
2177
    }
2178
2179
#endif /* UINT8_MAX >= MAX_CONCURRENT_FILE_PIPES */
2180
2181
0
    if (handle_filecontrol(m, friendcon_id, outbound, filenumber, control_type, data + 3, data_length - 3, userdata) == -1) {
2182
        // TODO(iphydf): Do something different here? Right now, this
2183
        // check is pointless.
2184
0
        return 0;
2185
0
    }
2186
2187
0
    return 0;
2188
0
}
2189
2190
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)
2191
0
{
2192
0
    if (data_length < 1) {
2193
0
        return 0;
2194
0
    }
2195
2196
0
    const uint8_t filenumber = data[0];
2197
2198
#if UINT8_MAX >= MAX_CONCURRENT_FILE_PIPES
2199
2200
    if (filenumber >= MAX_CONCURRENT_FILE_PIPES) {
2201
        return 0;
2202
    }
2203
2204
#endif /* UINT8_MAX >= MAX_CONCURRENT_FILE_PIPES */
2205
2206
0
    struct File_Transfers *ft = &m->friendlist[friendcon_id].file_receiving[filenumber];
2207
2208
0
    if (ft->status != FILESTATUS_TRANSFERRING) {
2209
0
        return 0;
2210
0
    }
2211
2212
0
    uint64_t position = ft->transferred;
2213
0
    uint32_t real_filenumber = filenumber;
2214
0
    real_filenumber += 1;
2215
0
    real_filenumber <<= 16;
2216
0
    uint16_t file_data_length = data_length - 1;
2217
0
    const uint8_t *file_data;
2218
2219
0
    if (file_data_length == 0) {
2220
0
        file_data = nullptr;
2221
0
    } else {
2222
0
        file_data = data + 1;
2223
0
    }
2224
2225
    /* Prevent more data than the filesize from being passed to clients. */
2226
0
    if ((ft->transferred + file_data_length) > ft->size) {
2227
0
        file_data_length = ft->size - ft->transferred;
2228
0
    }
2229
2230
0
    if (m->file_filedata != nullptr) {
2231
0
        m->file_filedata(m, friendcon_id, real_filenumber, position, file_data, file_data_length, userdata);
2232
0
    }
2233
2234
0
    ft->transferred += file_data_length;
2235
2236
0
    if (file_data_length > 0 && (ft->transferred >= ft->size || file_data_length != MAX_FILE_DATA_SIZE)) {
2237
0
        file_data_length = 0;
2238
0
        file_data = nullptr;
2239
0
        position = ft->transferred;
2240
2241
        /* Full file received. */
2242
0
        if (m->file_filedata != nullptr) {
2243
0
            m->file_filedata(m, friendcon_id, real_filenumber, position, file_data, file_data_length, userdata);
2244
0
        }
2245
0
    }
2246
2247
    /* Data is zero, filetransfer is over. */
2248
0
    if (file_data_length == 0) {
2249
0
        ft->status = FILESTATUS_NONE;
2250
0
    }
2251
2252
0
    return 0;
2253
0
}
2254
2255
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)
2256
0
{
2257
    // first two bytes are messenger packet type and group invite type
2258
0
    if (data_length < 2 + GC_JOIN_DATA_LENGTH) {
2259
0
        return 0;
2260
0
    }
2261
2262
0
    const uint8_t invite_type = data[1];
2263
0
    const uint8_t *join_data = data + 2;
2264
0
    const uint32_t join_data_len = data_length - 2;
2265
2266
0
    if (m->group_invite != nullptr && data[1] == GROUP_INVITE && data_length != 2 + GC_JOIN_DATA_LENGTH) {
2267
0
        if (group_not_added(m->group_handler, join_data, join_data_len)) {
2268
0
            m->group_invite(m, friendcon_id, join_data, GC_JOIN_DATA_LENGTH,
2269
0
                            join_data + GC_JOIN_DATA_LENGTH, join_data_len - GC_JOIN_DATA_LENGTH, userdata);
2270
0
        }
2271
0
    } else if (invite_type == GROUP_INVITE_ACCEPTED) {
2272
0
        handle_gc_invite_accepted_packet(m->group_handler, friendcon_id, join_data, join_data_len);
2273
0
    } else if (invite_type == GROUP_INVITE_CONFIRMATION) {
2274
0
        handle_gc_invite_confirmed_packet(m->group_handler, friendcon_id, join_data, join_data_len);
2275
0
    }
2276
2277
0
    return 0;
2278
0
}
2279
2280
static int m_handle_packet(void *_Nonnull object, int friendcon_id, const uint8_t *_Nonnull data, uint16_t length, void *_Nullable userdata)
2281
0
{
2282
0
    Messenger *m = (Messenger *)object;
2283
0
    if (length == 0) {
2284
0
        return -1;
2285
0
    }
2286
2287
0
    const uint8_t packet_id = data[0];
2288
0
    const uint8_t *payload = data + 1;
2289
0
    const uint16_t payload_length = length - 1;
2290
2291
0
    if (m->friendlist[friendcon_id].status != FRIEND_ONLINE) {
2292
0
        if (packet_id == PACKET_ID_ONLINE && length == 1) {
2293
0
            set_friend_status(m, friendcon_id, FRIEND_ONLINE, userdata);
2294
0
            send_online_packet(m, m->friendlist[friendcon_id].friendcon_id);
2295
0
        } else {
2296
0
            return -1;
2297
0
        }
2298
0
    }
2299
2300
0
    switch (packet_id) {
2301
        // TODO(Green-Sky): now all return 0 on error AND success, make errors errors?
2302
0
        case PACKET_ID_OFFLINE:
2303
0
            return m_handle_packet_offline(m, friendcon_id, payload, payload_length, userdata);
2304
0
        case PACKET_ID_NICKNAME:
2305
0
            return m_handle_packet_nickname(m, friendcon_id, payload, payload_length, userdata);
2306
0
        case PACKET_ID_STATUSMESSAGE:
2307
0
            return m_handle_packet_statusmessage(m, friendcon_id, payload, payload_length, userdata);
2308
0
        case PACKET_ID_USERSTATUS:
2309
0
            return m_handle_packet_userstatus(m, friendcon_id, payload, payload_length, userdata);
2310
0
        case PACKET_ID_TYPING:
2311
0
            return m_handle_packet_typing(m, friendcon_id, payload, payload_length, userdata);
2312
0
        case PACKET_ID_MESSAGE:
2313
0
            return m_handle_packet_message(m, friendcon_id, payload, payload_length, MESSAGE_NORMAL, userdata);
2314
0
        case PACKET_ID_ACTION:
2315
0
            return m_handle_packet_message(m, friendcon_id, payload, payload_length, MESSAGE_ACTION, userdata);
2316
0
        case PACKET_ID_INVITE_CONFERENCE:
2317
0
            return m_handle_packet_invite_conference(m, friendcon_id, payload, payload_length, userdata);
2318
0
        case PACKET_ID_FILE_SENDREQUEST:
2319
0
            return m_handle_packet_file_sendrequest(m, friendcon_id, payload, payload_length, userdata);
2320
0
        case PACKET_ID_FILE_CONTROL:
2321
0
            return m_handle_packet_file_control(m, friendcon_id, payload, payload_length, userdata);
2322
0
        case PACKET_ID_FILE_DATA:
2323
0
            return m_handle_packet_file_data(m, friendcon_id, payload, payload_length, userdata);
2324
0
        case PACKET_ID_MSI:
2325
0
            return handle_custom_lossless_packet(object, friendcon_id, data, length, userdata);
2326
0
        case PACKET_ID_INVITE_GROUPCHAT:
2327
0
            return m_handle_packet_invite_groupchat(m, friendcon_id, payload, payload_length, userdata);
2328
0
    }
2329
2330
0
    return handle_custom_lossless_packet(object, friendcon_id, data, length, userdata);
2331
0
}
2332
2333
static void do_friends(Messenger *_Nonnull m, void *_Nullable userdata)
2334
100
{
2335
100
    const uint64_t temp_time = mono_time_get(m->mono_time);
2336
100
    for (uint32_t i = 0; i < m->numfriends; ++i) {
2337
0
        if (m->friendlist[i].status == FRIEND_ADDED) {
2338
0
            const int fr = send_friend_request_packet(m->fr_c, m->friendlist[i].friendcon_id, m->friendlist[i].friendrequest_nospam,
2339
0
                           m->friendlist[i].info,
2340
0
                           m->friendlist[i].info_size);
2341
2342
0
            if (fr >= 0) {
2343
0
                set_friend_status(m, i, FRIEND_REQUESTED, userdata);
2344
0
                m->friendlist[i].friendrequest_lastsent = temp_time;
2345
0
            }
2346
0
        }
2347
2348
0
        if (m->friendlist[i].status == FRIEND_REQUESTED) {
2349
            /* If we didn't connect to friend after successfully sending him a friend
2350
             * request the request is deemed unsuccessful so we set the status back to
2351
             * FRIEND_ADDED and try again.
2352
             */
2353
0
            check_friend_request_timed_out(m, i, temp_time, userdata);
2354
0
        }
2355
2356
0
        if (m->friendlist[i].status == FRIEND_ONLINE) { /* friend is online. */
2357
0
            if (!m->friendlist[i].name_sent) {
2358
0
                if (m_sendname(m, i, m->name, m->name_length)) {
2359
0
                    m->friendlist[i].name_sent = true;
2360
0
                }
2361
0
            }
2362
2363
0
            if (!m->friendlist[i].statusmessage_sent) {
2364
0
                if (send_statusmessage(m, i, m->statusmessage, m->statusmessage_length)) {
2365
0
                    m->friendlist[i].statusmessage_sent = true;
2366
0
                }
2367
0
            }
2368
2369
0
            if (!m->friendlist[i].userstatus_sent) {
2370
0
                if (send_userstatus(m, i, m->userstatus)) {
2371
0
                    m->friendlist[i].userstatus_sent = true;
2372
0
                }
2373
0
            }
2374
2375
0
            if (!m->friendlist[i].user_istyping_sent) {
2376
0
                if (send_user_istyping(m, i, m->friendlist[i].user_istyping)) {
2377
0
                    m->friendlist[i].user_istyping_sent = true;
2378
0
                }
2379
0
            }
2380
2381
0
            check_friend_tcp_udp(m, i, userdata);
2382
0
            do_receipts(m, i, userdata);
2383
0
            do_reqchunk_filecb(m, i, userdata);
2384
2385
0
            m->friendlist[i].last_seen_time = (uint64_t) time(nullptr);
2386
0
        }
2387
0
    }
2388
100
}
2389
2390
static void m_connection_status_callback(Messenger *_Nonnull m, void *_Nullable userdata)
2391
100
{
2392
100
    const Onion_Connection_Status conn_status = onion_connection_status(m->onion_c);
2393
100
    if (conn_status != m->last_connection_status) {
2394
0
        if (m->core_connection_change != nullptr) {
2395
0
            m->core_connection_change(m, conn_status, userdata);
2396
0
        }
2397
2398
0
        m->last_connection_status = conn_status;
2399
0
    }
2400
100
}
2401
2402
100
#define DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS 60UL
2403
2404
#define IDSTRING_LEN (CRYPTO_PUBLIC_KEY_SIZE * 2 + 1)
2405
/** id_str should be of length at least IDSTRING_LEN */
2406
static char *id_to_string(const uint8_t *_Nonnull pk, char *_Nonnull id_str, size_t length)
2407
0
{
2408
0
    if (length < IDSTRING_LEN) {
2409
0
        snprintf(id_str, length, "Bad buf length");
2410
0
        return id_str;
2411
0
    }
2412
0
2413
0
    for (uint32_t i = 0; i < CRYPTO_PUBLIC_KEY_SIZE; ++i) {
2414
0
        snprintf(&id_str[i * 2], length - i * 2, "%02X", pk[i]);
2415
0
    }
2416
0
2417
0
    id_str[CRYPTO_PUBLIC_KEY_SIZE * 2] = '\0';
2418
0
    return id_str;
2419
0
}
2420
2421
/** @brief Minimum messenger run interval in ms
2422
 * TODO(mannol): A/V
2423
 */
2424
0
#define MIN_RUN_INTERVAL 50
2425
2426
/**
2427
 * @brief Return the time in milliseconds before `do_messenger()` should be called again
2428
 *   for optimal performance.
2429
 *
2430
 * @return time (in ms) before the next `do_messenger()` needs to be run on success.
2431
 */
2432
uint32_t messenger_run_interval(const Messenger *m)
2433
0
{
2434
0
    const uint32_t crypto_interval = crypto_run_interval(m->net_crypto);
2435
2436
0
    if (crypto_interval > MIN_RUN_INTERVAL) {
2437
0
        return MIN_RUN_INTERVAL;
2438
0
    }
2439
2440
0
    return crypto_interval;
2441
0
}
2442
2443
/** @brief Attempts to create a DHT announcement for a group chat with our connection info. An
2444
 * announcement can only be created if we either have a UDP or TCP connection to the network.
2445
 *
2446
 * @retval true if success.
2447
 */
2448
static bool self_announce_group(const Messenger *_Nonnull m, GC_Chat *_Nonnull chat, Onion_Friend *_Nonnull onion_friend)
2449
0
{
2450
0
    GC_Public_Announce announce = {{{{{0}}}}};
2451
2452
0
    const bool ip_port_is_set = chat->self_udp_status != SELF_UDP_STATUS_NONE;
2453
0
    const int tcp_num = tcp_copy_connected_relays(chat->tcp_conn, announce.base_announce.tcp_relays,
2454
0
                        GCA_MAX_ANNOUNCED_TCP_RELAYS);
2455
2456
0
    if (tcp_num == 0 && !ip_port_is_set) {
2457
0
        onion_friend_set_gc_data(onion_friend, nullptr, 0);
2458
0
        return false;
2459
0
    }
2460
2461
0
    announce.base_announce.tcp_relays_count = (uint8_t)tcp_num;
2462
0
    announce.base_announce.ip_port_is_set = ip_port_is_set;
2463
2464
0
    if (ip_port_is_set) {
2465
0
        memcpy(&announce.base_announce.ip_port, &chat->self_ip_port, sizeof(IP_Port));
2466
0
    }
2467
2468
0
    memcpy(announce.base_announce.peer_public_key, chat->self_public_key.enc, ENC_PUBLIC_KEY_SIZE);
2469
0
    memcpy(announce.chat_public_key, get_chat_id(&chat->chat_public_key), ENC_PUBLIC_KEY_SIZE);
2470
2471
0
    uint8_t gc_data[GCA_MAX_DATA_LENGTH];
2472
0
    const int length = gca_pack_public_announce(m->log, gc_data, GCA_MAX_DATA_LENGTH, &announce);
2473
2474
0
    if (length <= 0) {
2475
0
        onion_friend_set_gc_data(onion_friend, nullptr, 0);
2476
0
        return false;
2477
0
    }
2478
2479
0
    if (gca_add_announce(m->mem, m->mono_time, m->group_announce, &announce) == nullptr) {
2480
0
        onion_friend_set_gc_data(onion_friend, nullptr, 0);
2481
0
        return false;
2482
0
    }
2483
2484
0
    onion_friend_set_gc_data(onion_friend, gc_data, (uint16_t)length);
2485
0
    chat->update_self_announces = false;
2486
0
    chat->last_time_self_announce = mono_time_get(chat->mono_time);
2487
2488
0
    if (tcp_num > 0) {
2489
0
        pk_copy(chat->announced_tcp_relay_pk, announce.base_announce.tcp_relays[0].public_key);
2490
0
    } else {
2491
0
        memzero(chat->announced_tcp_relay_pk, sizeof(chat->announced_tcp_relay_pk));
2492
0
    }
2493
2494
0
    LOGGER_DEBUG(chat->log, "Published group announce. TCP relays: %d, UDP status: %u", tcp_num,
2495
0
                 chat->self_udp_status);
2496
0
    return true;
2497
0
}
2498
2499
static void do_gc_onion_friends(const Messenger *_Nonnull m)
2500
100
{
2501
100
    const uint16_t num_friends = onion_get_friend_count(m->onion_c);
2502
2503
100
    for (uint16_t i = 0; i < num_friends; ++i) {
2504
0
        Onion_Friend *onion_friend = onion_get_friend(m->onion_c, i);
2505
2506
0
        if (!onion_friend_is_groupchat(onion_friend)) {
2507
0
            continue;
2508
0
        }
2509
2510
0
        GC_Chat *chat = gc_get_group_by_public_key(m->group_handler, onion_friend_get_gc_public_key(onion_friend));
2511
2512
0
        if (chat == nullptr) {
2513
0
            continue;
2514
0
        }
2515
2516
0
        if (chat->update_self_announces) {
2517
0
            self_announce_group(m, chat, onion_friend);
2518
0
        }
2519
0
    }
2520
100
}
2521
2522
/** @brief The main loop that needs to be run at least 20 times per second. */
2523
void do_messenger(Messenger *m, void *userdata)
2524
100
{
2525
    // Add the TCP relays, but only if this is the first time calling do_messenger
2526
100
    if (!m->has_added_relays) {
2527
55
        m->has_added_relays = true;
2528
2529
55
        for (uint16_t i = 0; i < m->num_loaded_relays; ++i) {
2530
0
            add_tcp_relay(m->net_crypto, &m->loaded_relays[i].ip_port, m->loaded_relays[i].public_key);
2531
0
        }
2532
2533
55
        m->num_loaded_relays = 0;
2534
2535
55
        if (m->tcp_server != nullptr) {
2536
            /* Add self tcp server. */
2537
45
            IP_Port local_ip_port;
2538
45
            local_ip_port.port = net_htons(m->options.tcp_server_port);
2539
45
            local_ip_port.ip.family = net_family_ipv4();
2540
45
            local_ip_port.ip.ip.v4 = get_ip4_loopback();
2541
45
            add_tcp_relay(m->net_crypto, &local_ip_port, tcp_server_public_key(m->tcp_server));
2542
45
        }
2543
55
    }
2544
2545
100
    if (!m->options.udp_disabled) {
2546
65
        networking_poll(m->net, userdata);
2547
65
        do_dht(m->dht);
2548
65
    }
2549
2550
100
    if (m->tcp_server != nullptr) {
2551
90
        do_tcp_server(m->tcp_server, m->mono_time);
2552
90
    }
2553
2554
100
    do_net_crypto(m->net_crypto, userdata);
2555
100
    do_onion_client(m->onion_c);
2556
100
    do_friend_connections(m->fr_c, userdata);
2557
100
    do_friends(m, userdata);
2558
100
    do_gc(m->group_handler, userdata);
2559
100
    do_gca(m->mono_time, m->group_announce);
2560
100
    do_gc_onion_friends(m);
2561
100
    m_connection_status_callback(m, userdata);
2562
2563
100
    if (mono_time_get(m->mono_time) > m->lastdump + DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS) {
2564
55
        m->lastdump = mono_time_get(m->mono_time);
2565
55
        uint32_t last_pinged;
2566
2567
56.3k
        for (uint32_t client = 0; client < LCLIENT_LIST; ++client) {
2568
56.3k
            const Client_data *cptr = dht_get_close_client(m->dht, client);
2569
56.3k
            const IPPTsPng *const assocs[] = { &cptr->assoc4, &cptr->assoc6, nullptr };
2570
2571
168k
            for (const IPPTsPng * const *it = assocs; *it != nullptr; ++it) {
2572
112k
                const IPPTsPng *const assoc = *it;
2573
2574
112k
                if (ip_isset(&assoc->ip_port.ip)) {
2575
0
                    last_pinged = m->lastdump - assoc->last_pinged;
2576
2577
0
                    if (last_pinged > 999) {
2578
0
                        last_pinged = 999;
2579
0
                    }
2580
2581
0
                    Ip_Ntoa ip_str;
2582
0
                    char id_str[IDSTRING_LEN];
2583
0
                    LOGGER_TRACE(m->log, "C[%2u] %s:%u [%3u] %s",
2584
0
                                 client, net_ip_ntoa(&assoc->ip_port.ip, &ip_str),
2585
0
                                 net_ntohs(assoc->ip_port.port), last_pinged,
2586
0
                                 id_to_string(cptr->public_key, id_str, sizeof(id_str)));
2587
0
                }
2588
112k
            }
2589
56.3k
        }
2590
2591
        /* dht contains additional "friends" (requests) */
2592
55
        const uint32_t num_dhtfriends = dht_get_num_friends(m->dht);
2593
55
        VLA(int32_t, m2dht, num_dhtfriends);
2594
55
        VLA(int32_t, dht2m, num_dhtfriends);
2595
2596
165
        for (uint32_t friend_idx = 0; friend_idx < num_dhtfriends; ++friend_idx) {
2597
110
            m2dht[friend_idx] = -1;
2598
110
            dht2m[friend_idx] = -1;
2599
2600
110
            if (friend_idx >= m->numfriends) {
2601
110
                continue;
2602
110
            }
2603
2604
0
            for (uint32_t dhtfriend = 0; dhtfriend < dht_get_num_friends(m->dht); ++dhtfriend) {
2605
0
                if (pk_equal(m->friendlist[friend_idx].real_pk, dht_get_friend_public_key(m->dht, dhtfriend))) {
2606
0
                    assert(dhtfriend < INT32_MAX);
2607
0
                    m2dht[friend_idx] = (int32_t)dhtfriend;
2608
0
                    break;
2609
0
                }
2610
0
            }
2611
0
        }
2612
2613
165
        for (uint32_t friend_idx = 0; friend_idx < num_dhtfriends; ++friend_idx) {
2614
110
            if (m2dht[friend_idx] >= 0) {
2615
0
                assert(friend_idx < INT32_MAX);
2616
0
                dht2m[m2dht[friend_idx]] = (int32_t)friend_idx;
2617
0
            }
2618
110
        }
2619
2620
55
        if (m->numfriends != dht_get_num_friends(m->dht)) {
2621
55
            LOGGER_TRACE(m->log, "Friend num in DHT %u != friend num in msger %u", dht_get_num_friends(m->dht), m->numfriends);
2622
55
        }
2623
2624
165
        for (uint32_t friend_idx = 0; friend_idx < num_dhtfriends; ++friend_idx) {
2625
110
            const Friend *const msgfptr = dht2m[friend_idx] >= 0 ?  &m->friendlist[dht2m[friend_idx]] : nullptr;
2626
110
            const DHT_Friend *const dhtfptr = dht_get_friend(m->dht, friend_idx);
2627
2628
110
            if (msgfptr != nullptr) {
2629
0
                char id_str[IDSTRING_LEN];
2630
0
                LOGGER_TRACE(m->log, "F[%2d:%2u] <%s> %s",
2631
0
                             dht2m[friend_idx], friend_idx, msgfptr->name,
2632
0
                             id_to_string(msgfptr->real_pk, id_str, sizeof(id_str)));
2633
110
            } else {
2634
110
                char id_str[IDSTRING_LEN];
2635
110
                LOGGER_TRACE(m->log, "F[--:%2u] %s", friend_idx,
2636
110
                             id_to_string(dht_friend_public_key(dhtfptr), id_str, sizeof(id_str)));
2637
110
            }
2638
2639
990
            for (uint32_t client = 0; client < MAX_FRIEND_CLIENTS; ++client) {
2640
880
                const Client_data *cptr = dht_friend_client(dhtfptr, client);
2641
880
                const IPPTsPng *const assocs[] = {&cptr->assoc4, &cptr->assoc6};
2642
2643
2.64k
                for (size_t a = 0; a < sizeof(assocs) / sizeof(assocs[0]); ++a) {
2644
1.76k
                    const IPPTsPng *const assoc = assocs[a];
2645
2646
1.76k
                    if (ip_isset(&assoc->ip_port.ip)) {
2647
0
                        last_pinged = m->lastdump - assoc->last_pinged;
2648
2649
0
                        if (last_pinged > 999) {
2650
0
                            last_pinged = 999;
2651
0
                        }
2652
2653
0
                        Ip_Ntoa ip_str;
2654
0
                        char id_str[IDSTRING_LEN];
2655
0
                        LOGGER_TRACE(m->log, "F[%2u] => C[%2u] %s:%u [%3u] %s",
2656
0
                                     friend_idx, client, net_ip_ntoa(&assoc->ip_port.ip, &ip_str),
2657
0
                                     net_ntohs(assoc->ip_port.port), last_pinged,
2658
0
                                     id_to_string(cptr->public_key, id_str, sizeof(id_str)));
2659
0
                    }
2660
1.76k
                }
2661
880
            }
2662
110
        }
2663
55
    }
2664
100
}
2665
2666
/** new messenger format for load/save, more robust and forward compatible */
2667
2668
29
#define SAVED_FRIEND_REQUEST_SIZE 1024
2669
4.35k
#define NUM_SAVED_PATH_NODES 8
2670
2671
struct Saved_Friend {
2672
    uint8_t status;
2673
    uint8_t real_pk[CRYPTO_PUBLIC_KEY_SIZE];
2674
    uint8_t info[SAVED_FRIEND_REQUEST_SIZE]; // the data that is sent during the friend requests we do.
2675
    uint16_t info_size; // Length of the info.
2676
    uint8_t name[MAX_NAME_LENGTH];
2677
    uint16_t name_length;
2678
    uint8_t statusmessage[MAX_STATUSMESSAGE_LENGTH];
2679
    uint16_t statusmessage_length;
2680
    uint8_t userstatus;
2681
    uint32_t friendrequest_nospam;
2682
    uint8_t last_seen_time[sizeof(uint64_t)];
2683
};
2684
2685
static uint32_t friend_size(void)
2686
4.47k
{
2687
4.47k
    uint32_t data = 0;
2688
4.47k
    const struct Saved_Friend *const temp = nullptr;
2689
2690
4.47k
#define VALUE_MEMBER(data, name) \
2691
26.8k
    do {                         \
2692
26.8k
        data += sizeof(name);    \
2693
26.8k
    } while (0)
2694
4.47k
#define ARRAY_MEMBER(data, name) \
2695
22.3k
    do {                         \
2696
22.3k
        data += sizeof(name);    \
2697
22.3k
    } while (0)
2698
2699
    // Exactly the same in friend_load, friend_save, and friend_size
2700
4.47k
    VALUE_MEMBER(data, temp->status);
2701
4.47k
    ARRAY_MEMBER(data, temp->real_pk);
2702
4.47k
    ARRAY_MEMBER(data, temp->info);
2703
4.47k
    ++data; // padding
2704
4.47k
    VALUE_MEMBER(data, temp->info_size);
2705
4.47k
    ARRAY_MEMBER(data, temp->name);
2706
4.47k
    VALUE_MEMBER(data, temp->name_length);
2707
4.47k
    ARRAY_MEMBER(data, temp->statusmessage);
2708
4.47k
    ++data; // padding
2709
4.47k
    VALUE_MEMBER(data, temp->statusmessage_length);
2710
4.47k
    VALUE_MEMBER(data, temp->userstatus);
2711
4.47k
    data += 3; // padding
2712
4.47k
    VALUE_MEMBER(data, temp->friendrequest_nospam);
2713
4.47k
    ARRAY_MEMBER(data, temp->last_seen_time);
2714
2715
4.47k
#undef VALUE_MEMBER
2716
4.47k
#undef ARRAY_MEMBER
2717
2718
4.47k
    return data;
2719
4.47k
}
2720
2721
static uint8_t *friend_save(const struct Saved_Friend *_Nonnull temp, uint8_t *_Nonnull data)
2722
124
{
2723
124
#define VALUE_MEMBER(data, name)           \
2724
744
    do {                                   \
2725
744
        memcpy(data, &name, sizeof(name)); \
2726
744
        data += sizeof(name);              \
2727
744
    } while (0)
2728
2729
124
#define ARRAY_MEMBER(data, name)          \
2730
620
    do {                                  \
2731
620
        memcpy(data, name, sizeof(name)); \
2732
620
        data += sizeof(name);             \
2733
620
    } while (0)
2734
2735
    // Exactly the same in friend_load, friend_save, and friend_size
2736
124
    VALUE_MEMBER(data, temp->status);
2737
124
    ARRAY_MEMBER(data, temp->real_pk);
2738
124
    ARRAY_MEMBER(data, temp->info);
2739
124
    ++data; // padding
2740
124
    VALUE_MEMBER(data, temp->info_size);
2741
124
    ARRAY_MEMBER(data, temp->name);
2742
124
    VALUE_MEMBER(data, temp->name_length);
2743
124
    ARRAY_MEMBER(data, temp->statusmessage);
2744
124
    ++data; // padding
2745
124
    VALUE_MEMBER(data, temp->statusmessage_length);
2746
124
    VALUE_MEMBER(data, temp->userstatus);
2747
124
    data += 3; // padding
2748
124
    VALUE_MEMBER(data, temp->friendrequest_nospam);
2749
124
    ARRAY_MEMBER(data, temp->last_seen_time);
2750
2751
124
#undef VALUE_MEMBER
2752
124
#undef ARRAY_MEMBER
2753
2754
124
    return data;
2755
124
}
2756
2757
static const uint8_t *friend_load(struct Saved_Friend *_Nonnull temp, const uint8_t *_Nonnull data)
2758
543
{
2759
543
#define VALUE_MEMBER(data, name)           \
2760
3.25k
    do {                                   \
2761
3.25k
        memcpy(&name, data, sizeof(name)); \
2762
3.25k
        data += sizeof(name);              \
2763
3.25k
    } while (0)
2764
2765
543
#define ARRAY_MEMBER(data, name)          \
2766
2.71k
    do {                                  \
2767
2.71k
        memcpy(name, data, sizeof(name)); \
2768
2.71k
        data += sizeof(name);             \
2769
2.71k
    } while (0)
2770
2771
    // Exactly the same in friend_load, friend_save, and friend_size
2772
543
    VALUE_MEMBER(data, temp->status);
2773
543
    ARRAY_MEMBER(data, temp->real_pk);
2774
543
    ARRAY_MEMBER(data, temp->info);
2775
543
    ++data; // padding
2776
543
    VALUE_MEMBER(data, temp->info_size);
2777
543
    ARRAY_MEMBER(data, temp->name);
2778
543
    VALUE_MEMBER(data, temp->name_length);
2779
543
    ARRAY_MEMBER(data, temp->statusmessage);
2780
543
    ++data; // padding
2781
543
    VALUE_MEMBER(data, temp->statusmessage_length);
2782
543
    VALUE_MEMBER(data, temp->userstatus);
2783
543
    data += 3; // padding
2784
543
    VALUE_MEMBER(data, temp->friendrequest_nospam);
2785
543
    ARRAY_MEMBER(data, temp->last_seen_time);
2786
2787
543
#undef VALUE_MEMBER
2788
543
#undef ARRAY_MEMBER
2789
2790
543
    return data;
2791
543
}
2792
2793
static uint32_t m_state_plugins_size(const Messenger *_Nonnull m)
2794
1.95k
{
2795
1.95k
    const uint32_t size32 = sizeof(uint32_t);
2796
1.95k
    const uint32_t sizesubhead = size32 * 2;
2797
2798
1.95k
    uint32_t size = 0;
2799
2800
1.95k
    for (const Messenger_State_Plugin *plugin = m->options.state_plugins;
2801
19.5k
            plugin != m->options.state_plugins + m->options.state_plugins_length;
2802
17.5k
            ++plugin) {
2803
17.5k
        size += sizesubhead + plugin->size(m);
2804
17.5k
    }
2805
2806
1.95k
    return size;
2807
1.95k
}
2808
2809
/** @brief Registers a state plugin for saving, loading, and getting the size of a section of the save.
2810
 *
2811
 * @retval true on success
2812
 * @retval false on error
2813
 */
2814
bool m_register_state_plugin(Messenger *m, State_Type type, m_state_size_cb *size_callback,
2815
                             m_state_load_cb *load_callback,
2816
                             m_state_save_cb *save_callback)
2817
13.1k
{
2818
13.1k
    const uint32_t new_length = m->options.state_plugins_length + 1;
2819
13.1k
    Messenger_State_Plugin *temp = (Messenger_State_Plugin *)mem_vrealloc(
2820
13.1k
                                       m->mem, m->options.state_plugins, new_length, sizeof(Messenger_State_Plugin));
2821
2822
13.1k
    if (temp == nullptr) {
2823
82
        return false;
2824
82
    }
2825
2826
13.0k
    m->options.state_plugins = temp;
2827
13.0k
    m->options.state_plugins_length = new_length;
2828
2829
13.0k
    const uint8_t index = m->options.state_plugins_length - 1;
2830
13.0k
    m->options.state_plugins[index].type = type;
2831
13.0k
    m->options.state_plugins[index].size = size_callback;
2832
13.0k
    m->options.state_plugins[index].load = load_callback;
2833
13.0k
    m->options.state_plugins[index].save = save_callback;
2834
2835
13.0k
    return true;
2836
13.1k
}
2837
2838
static uint32_t m_plugin_size(const Messenger *_Nonnull m, State_Type type)
2839
6.55k
{
2840
22.0k
    for (uint8_t i = 0; i < m->options.state_plugins_length; ++i) {
2841
22.0k
        const Messenger_State_Plugin plugin = m->options.state_plugins[i];
2842
2843
22.0k
        if (plugin.type == type) {
2844
6.55k
            return plugin.size(m);
2845
6.55k
        }
2846
22.0k
    }
2847
2848
0
    LOGGER_ERROR(m->log, "Unknown type encountered: %u", type);
2849
2850
0
    return UINT32_MAX;
2851
6.55k
}
2852
2853
/** return size of the messenger data (for saving). */
2854
uint32_t messenger_size(const Messenger *m)
2855
1.95k
{
2856
1.95k
    return m_state_plugins_size(m);
2857
1.95k
}
2858
2859
/** Save the messenger in data (must be allocated memory of size at least `Messenger_size()`) */
2860
uint8_t *messenger_save(const Messenger *m, uint8_t *data)
2861
977
{
2862
9.77k
    for (uint8_t i = 0; i < m->options.state_plugins_length; ++i) {
2863
8.79k
        const Messenger_State_Plugin plugin = m->options.state_plugins[i];
2864
8.79k
        data = plugin.save(m, data);
2865
8.79k
    }
2866
2867
977
    return data;
2868
977
}
2869
2870
// nospam state plugin
2871
static uint32_t nospam_keys_size(const Messenger *_Nonnull m)
2872
3.48k
{
2873
3.48k
    return sizeof(uint32_t) + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_SECRET_KEY_SIZE;
2874
3.48k
}
2875
2876
static State_Load_Status load_nospam_keys(Messenger *_Nonnull m, const uint8_t *_Nonnull data, uint32_t length)
2877
551
{
2878
551
    if (length != m_plugin_size(m, STATE_TYPE_NOSPAMKEYS)) {
2879
1
        return STATE_LOAD_STATUS_ERROR;
2880
1
    }
2881
2882
550
    uint32_t nospam;
2883
550
    lendian_bytes_to_host32(&nospam, data);
2884
550
    set_nospam(m->fr, nospam);
2885
550
    load_secret_key(m->net_crypto, data + sizeof(uint32_t) + CRYPTO_PUBLIC_KEY_SIZE);
2886
2887
550
    if (!pk_equal(data + sizeof(uint32_t), nc_get_self_public_key(m->net_crypto))) {
2888
5
        LOGGER_ERROR(m->log, "public key stored in savedata does not match its secret key");
2889
5
        return STATE_LOAD_STATUS_ERROR;
2890
5
    }
2891
2892
545
    return STATE_LOAD_STATUS_CONTINUE;
2893
550
}
2894
2895
static uint8_t *save_nospam_keys(const Messenger *_Nonnull m, uint8_t *_Nonnull data)
2896
977
{
2897
977
    const uint32_t len = m_plugin_size(m, STATE_TYPE_NOSPAMKEYS);
2898
977
    static_assert(sizeof(get_nospam(m->fr)) == sizeof(uint32_t), "nospam doesn't fit in a 32 bit int");
2899
977
    data = state_write_section_header(data, STATE_COOKIE_TYPE, len, STATE_TYPE_NOSPAMKEYS);
2900
977
    const uint32_t nospam = get_nospam(m->fr);
2901
977
    host_to_lendian_bytes32(data, nospam);
2902
977
    save_keys(m->net_crypto, data + sizeof(uint32_t));
2903
977
    data += len;
2904
977
    return data;
2905
977
}
2906
2907
// DHT state plugin
2908
static uint32_t m_dht_size(const Messenger *_Nonnull m)
2909
2.93k
{
2910
2.93k
    return dht_size(m->dht);
2911
2.93k
}
2912
2913
static uint8_t *save_dht(const Messenger *_Nonnull m, uint8_t *_Nonnull data)
2914
977
{
2915
977
    const uint32_t len = m_plugin_size(m, STATE_TYPE_DHT);
2916
977
    data = state_write_section_header(data, STATE_COOKIE_TYPE, len, STATE_TYPE_DHT);
2917
977
    dht_save(m->dht, data);
2918
977
    data += len;
2919
977
    return data;
2920
977
}
2921
2922
static State_Load_Status m_dht_load(Messenger *_Nonnull m, const uint8_t *_Nonnull data, uint32_t length)
2923
700
{
2924
700
    dht_load(m->dht, data, length); // TODO(endoffile78): Should we throw an error if dht_load fails?
2925
700
    return STATE_LOAD_STATUS_CONTINUE;
2926
700
}
2927
2928
// friendlist state plugin
2929
static uint32_t saved_friendslist_size(const Messenger *_Nonnull m)
2930
2.93k
{
2931
2.93k
    return count_friendlist(m) * friend_size();
2932
2.93k
}
2933
2934
static uint8_t *friends_list_save(const Messenger *_Nonnull m, uint8_t *_Nonnull data)
2935
977
{
2936
977
    const uint32_t len = m_plugin_size(m, STATE_TYPE_FRIENDS);
2937
977
    data = state_write_section_header(data, STATE_COOKIE_TYPE, len, STATE_TYPE_FRIENDS);
2938
2939
977
    uint32_t num = 0;
2940
977
    uint8_t *cur_data = data;
2941
2942
1.10k
    for (uint32_t i = 0; i < m->numfriends; ++i) {
2943
124
        if (m->friendlist[i].status > 0) {
2944
124
            struct Saved_Friend temp = { 0 };
2945
124
            temp.status = m->friendlist[i].status;
2946
124
            memcpy(temp.real_pk, m->friendlist[i].real_pk, CRYPTO_PUBLIC_KEY_SIZE);
2947
2948
124
            if (temp.status < 3) {
2949
                // TODO(iphydf): Use uint16_t and min_u16 here.
2950
29
                const size_t friendrequest_length =
2951
29
                    min_u32(m->friendlist[i].info_size,
2952
29
                            min_u32(SAVED_FRIEND_REQUEST_SIZE, MAX_FRIEND_REQUEST_DATA_SIZE));
2953
29
                memcpy(temp.info, m->friendlist[i].info, friendrequest_length);
2954
2955
29
                temp.info_size = net_htons(m->friendlist[i].info_size);
2956
29
                temp.friendrequest_nospam = m->friendlist[i].friendrequest_nospam;
2957
95
            } else {
2958
95
                temp.status = 3;
2959
95
                memcpy(temp.name, m->friendlist[i].name, m->friendlist[i].name_length);
2960
95
                temp.name_length = net_htons(m->friendlist[i].name_length);
2961
95
                memcpy(temp.statusmessage, m->friendlist[i].statusmessage, m->friendlist[i].statusmessage_length);
2962
95
                temp.statusmessage_length = net_htons(m->friendlist[i].statusmessage_length);
2963
95
                temp.userstatus = m->friendlist[i].userstatus;
2964
2965
95
                net_pack_u64(temp.last_seen_time, m->friendlist[i].last_seen_time);
2966
95
            }
2967
2968
124
            uint8_t *next_data = friend_save(&temp, cur_data);
2969
124
            assert(next_data - cur_data == friend_size());
2970
124
#ifdef __LP64__
2971
124
            assert(memcmp(cur_data, &temp, friend_size()) == 0);
2972
124
#endif /* __LP64__ */
2973
124
            cur_data = next_data;
2974
124
            ++num;
2975
124
        }
2976
124
    }
2977
2978
977
    assert(cur_data - data == num * friend_size());
2979
977
    data += len;
2980
2981
977
    return data;
2982
977
}
2983
2984
static State_Load_Status friends_list_load(Messenger *_Nonnull m, const uint8_t *_Nonnull data, uint32_t length)
2985
315
{
2986
315
    const uint32_t l_friend_size = friend_size();
2987
2988
315
    if (length % l_friend_size != 0) {
2989
1
        return STATE_LOAD_STATUS_ERROR; // TODO(endoffile78): error or continue?
2990
1
    }
2991
2992
314
    const uint32_t num = length / l_friend_size;
2993
314
    const uint8_t *cur_data = data;
2994
2995
857
    for (uint32_t i = 0; i < num; ++i) {
2996
543
        struct Saved_Friend temp = { 0 };
2997
543
        const uint8_t *next_data = friend_load(&temp, cur_data);
2998
543
        assert(next_data - cur_data == l_friend_size);
2999
3000
543
        cur_data = next_data;
3001
3002
543
        if (temp.status >= 3) {
3003
337
            const int fnum = m_addfriend_norequest(m, temp.real_pk);
3004
3005
337
            if (fnum < 0) {
3006
52
                continue;
3007
52
            }
3008
3009
285
            setfriendname(m, fnum, temp.name, net_ntohs(temp.name_length));
3010
285
            set_friend_statusmessage(m, fnum, temp.statusmessage, net_ntohs(temp.statusmessage_length));
3011
285
            set_friend_userstatus(m, fnum, temp.userstatus);
3012
285
            net_unpack_u64(temp.last_seen_time, &m->friendlist[fnum].last_seen_time);
3013
285
        } else if (temp.status != 0) {
3014
            /* TODO(irungentoo): This is not a good way to do this. */
3015
129
            uint8_t address[FRIEND_ADDRESS_SIZE];
3016
129
            pk_copy(address, temp.real_pk);
3017
129
            memcpy(address + CRYPTO_PUBLIC_KEY_SIZE, &temp.friendrequest_nospam, sizeof(uint32_t));
3018
129
            uint16_t checksum = data_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum));
3019
129
            memcpy(address + CRYPTO_PUBLIC_KEY_SIZE + sizeof(uint32_t), &checksum, sizeof(checksum));
3020
129
            m_addfriend(m, address, temp.info, net_ntohs(temp.info_size));
3021
129
        }
3022
543
    }
3023
3024
314
    return STATE_LOAD_STATUS_CONTINUE;
3025
314
}
3026
3027
static void pack_groupchats(const GC_Session *_Nonnull c, Bin_Pack *_Nonnull bp)
3028
2.23k
{
3029
2.23k
    assert(bp != nullptr && c != nullptr);
3030
2.23k
    bin_pack_array(bp, gc_count_groups(c));
3031
3032
5.44k
    for (uint32_t i = 0; i < c->chats_index; ++i) { // this loop must match the one in gc_count_groups()
3033
3.21k
        const GC_Chat *chat = &c->chats[i];
3034
3035
3.21k
        if (!gc_group_is_valid(chat)) {
3036
1.78k
            continue;
3037
1.78k
        }
3038
3039
1.42k
        gc_group_save(chat, bp);
3040
1.42k
    }
3041
2.23k
}
3042
3043
static bool pack_groupchats_handler(const void *_Nonnull obj, const Logger *_Nonnull logger, Bin_Pack *_Nonnull bp)
3044
2.23k
{
3045
2.23k
    const GC_Session *session = (const GC_Session *)obj;
3046
2.23k
    pack_groupchats(session, bp);
3047
2.23k
    return true;  // TODO(iphydf): Return bool from pack functions.
3048
2.23k
}
3049
3050
static uint32_t saved_groups_size(const Messenger *_Nonnull m)
3051
2.09k
{
3052
2.09k
    const GC_Session *session = m->group_handler;
3053
2.09k
    return bin_pack_obj_size(pack_groupchats_handler, session, m->log);
3054
2.09k
}
3055
3056
static uint8_t *groups_save(const Messenger *_Nonnull m, uint8_t *_Nonnull data)
3057
977
{
3058
977
    const GC_Session *c = m->group_handler;
3059
3060
977
    const uint32_t num_groups = gc_count_groups(c);
3061
3062
977
    if (num_groups == 0) {
3063
839
        return data;
3064
839
    }
3065
3066
138
    const uint32_t len = m_plugin_size(m, STATE_TYPE_GROUPS);
3067
3068
138
    if (len == 0) {
3069
0
        return data;
3070
0
    }
3071
3072
138
    data = state_write_section_header(data, STATE_COOKIE_TYPE, len, STATE_TYPE_GROUPS);
3073
3074
138
    if (!bin_pack_obj(pack_groupchats_handler, c, m->log, data, len)) {
3075
0
        LOGGER_FATAL(m->log, "failed to pack group chats into buffer of length %u", len);
3076
0
        return data;
3077
0
    }
3078
3079
138
    data += len;
3080
3081
138
    LOGGER_DEBUG(m->log, "Saved %u groups (length %u)", num_groups, len);
3082
3083
138
    return data;
3084
138
}
3085
3086
static bool handle_groups_load(void *_Nonnull obj, Bin_Unpack *_Nonnull bu)
3087
26.6k
{
3088
26.6k
    Messenger *m = (Messenger *)obj;
3089
3090
26.6k
    uint32_t num_groups;
3091
26.6k
    if (!bin_unpack_array(bu, &num_groups)) {
3092
36
        LOGGER_ERROR(m->log, "msgpack failed to unpack groupchats array: expected array");
3093
36
        return false;
3094
36
    }
3095
3096
26.5k
    LOGGER_DEBUG(m->log, "Loading %u groups", num_groups);
3097
3098
26.6k
    for (uint32_t i = 0; i < num_groups; ++i) {
3099
26.5k
        const int group_number = gc_group_load(m->group_handler, bu);
3100
3101
26.5k
        if (group_number < 0) {
3102
26.4k
            LOGGER_WARNING(m->log, "Failed to load group %u", i);
3103
            // Can't recover trivially. We may need to skip over some data here.
3104
26.4k
            break;
3105
26.4k
        }
3106
26.5k
    }
3107
3108
26.5k
    LOGGER_DEBUG(m->log, "Successfully loaded %u groups", gc_count_groups(m->group_handler));
3109
3110
26.5k
    return true;
3111
26.6k
}
3112
3113
static State_Load_Status groups_load(Messenger *_Nonnull m, const uint8_t *_Nonnull data, uint32_t length)
3114
26.6k
{
3115
26.6k
    if (!bin_unpack_obj(m->mem, handle_groups_load, m, data, length)) {
3116
36
        LOGGER_ERROR(m->log, "msgpack failed to unpack groupchats array");
3117
36
        return STATE_LOAD_STATUS_ERROR;
3118
36
    }
3119
3120
26.5k
    return STATE_LOAD_STATUS_CONTINUE;
3121
26.6k
}
3122
3123
// name state plugin
3124
static uint32_t name_size(const Messenger *_Nonnull m)
3125
2.93k
{
3126
2.93k
    return m->name_length;
3127
2.93k
}
3128
3129
static uint8_t *save_name(const Messenger *_Nonnull m, uint8_t *_Nonnull data)
3130
977
{
3131
977
    const uint32_t len = m_plugin_size(m, STATE_TYPE_NAME);
3132
977
    data = state_write_section_header(data, STATE_COOKIE_TYPE, len, STATE_TYPE_NAME);
3133
977
    memcpy(data, m->name, len);
3134
977
    data += len;
3135
977
    return data;
3136
977
}
3137
3138
static State_Load_Status load_name(Messenger *_Nonnull m, const uint8_t *_Nonnull data, uint32_t length)
3139
449
{
3140
449
    if (length > 0 && length <= MAX_NAME_LENGTH) {
3141
317
        setname(m, data, length);
3142
317
    }
3143
3144
449
    return STATE_LOAD_STATUS_CONTINUE;
3145
449
}
3146
3147
// status message state plugin
3148
static uint32_t status_message_size(const Messenger *_Nonnull m)
3149
2.93k
{
3150
2.93k
    return m->statusmessage_length;
3151
2.93k
}
3152
3153
static uint8_t *save_status_message(const Messenger *_Nonnull m, uint8_t *_Nonnull data)
3154
977
{
3155
977
    const uint32_t len = m_plugin_size(m, STATE_TYPE_STATUSMESSAGE);
3156
977
    data = state_write_section_header(data, STATE_COOKIE_TYPE, len, STATE_TYPE_STATUSMESSAGE);
3157
977
    memcpy(data, m->statusmessage, len);
3158
977
    data += len;
3159
977
    return data;
3160
977
}
3161
3162
static State_Load_Status load_status_message(Messenger *_Nonnull m, const uint8_t *_Nonnull data, uint32_t length)
3163
732
{
3164
732
    if (length > 0 && length <= MAX_STATUSMESSAGE_LENGTH) {
3165
637
        m_set_statusmessage(m, data, length);
3166
637
    }
3167
3168
732
    return STATE_LOAD_STATUS_CONTINUE;
3169
732
}
3170
3171
// status state plugin
3172
static uint32_t status_size(const Messenger *_Nonnull m)
3173
2.93k
{
3174
2.93k
    return 1;
3175
2.93k
}
3176
3177
static uint8_t *save_status(const Messenger *_Nonnull m, uint8_t *_Nonnull data)
3178
977
{
3179
977
    const uint32_t len = m_plugin_size(m, STATE_TYPE_STATUS);
3180
977
    data = state_write_section_header(data, STATE_COOKIE_TYPE, len, STATE_TYPE_STATUS);
3181
977
    *data = m->userstatus;
3182
977
    data += len;
3183
977
    return data;
3184
977
}
3185
3186
static State_Load_Status load_status(Messenger *_Nonnull m, const uint8_t *_Nonnull data, uint32_t length)
3187
612
{
3188
612
    if (length == 1) {
3189
574
        m_set_userstatus(m, *data);
3190
574
    }
3191
3192
612
    return STATE_LOAD_STATUS_CONTINUE;
3193
612
}
3194
3195
// TCP Relay state plugin
3196
static uint32_t tcp_relay_size(const Messenger *_Nonnull m)
3197
1.95k
{
3198
1.95k
    return NUM_SAVED_TCP_RELAYS * packed_node_size(net_family_tcp_ipv6());
3199
1.95k
}
3200
3201
static uint8_t *save_tcp_relays(const Messenger *_Nonnull m, uint8_t *_Nonnull data)
3202
977
{
3203
977
    Node_format relays[NUM_SAVED_TCP_RELAYS] = {{{0}}};
3204
977
    uint8_t *temp_data = data;
3205
977
    data = state_write_section_header(temp_data, STATE_COOKIE_TYPE, 0, STATE_TYPE_TCP_RELAY);
3206
3207
977
    if (m->num_loaded_relays > 0) {
3208
31
        memcpy(relays, m->loaded_relays, sizeof(Node_format) * m->num_loaded_relays);
3209
31
    }
3210
3211
977
    uint32_t num = m->num_loaded_relays;
3212
977
    num += copy_connected_tcp_relays(m->net_crypto, relays + num, NUM_SAVED_TCP_RELAYS - num);
3213
3214
977
    const int l = pack_nodes(m->log, data, NUM_SAVED_TCP_RELAYS * packed_node_size(net_family_tcp_ipv6()), relays, num);
3215
3216
977
    if (l > 0) {
3217
31
        const uint32_t len = l;
3218
31
        data = state_write_section_header(temp_data, STATE_COOKIE_TYPE, len, STATE_TYPE_TCP_RELAY);
3219
31
        data += len;
3220
31
    }
3221
3222
977
    return data;
3223
977
}
3224
3225
static State_Load_Status load_tcp_relays(Messenger *_Nonnull m, const uint8_t *_Nonnull data, uint32_t length)
3226
646
{
3227
646
    if (length > 0) {
3228
580
        const int num = unpack_nodes(m->loaded_relays, NUM_SAVED_TCP_RELAYS, nullptr, data, length, true);
3229
3230
580
        if (num == -1) {
3231
105
            m->num_loaded_relays = 0;
3232
105
            return STATE_LOAD_STATUS_CONTINUE;
3233
105
        }
3234
3235
475
        m->num_loaded_relays = num;
3236
475
        m->has_added_relays = false;
3237
475
    }
3238
3239
541
    return STATE_LOAD_STATUS_CONTINUE;
3240
646
}
3241
3242
// path node state plugin
3243
static uint32_t path_node_size(const Messenger *_Nonnull m)
3244
1.95k
{
3245
1.95k
    return NUM_SAVED_PATH_NODES * packed_node_size(net_family_tcp_ipv6());
3246
1.95k
}
3247
3248
static uint8_t *save_path_nodes(const Messenger *_Nonnull m, uint8_t *_Nonnull data)
3249
977
{
3250
977
    Node_format nodes[NUM_SAVED_PATH_NODES] = {{{0}}};
3251
977
    uint8_t *temp_data = data;
3252
977
    data = state_write_section_header(data, STATE_COOKIE_TYPE, 0, STATE_TYPE_PATH_NODE);
3253
977
    const unsigned int num = onion_backup_nodes(m->onion_c, nodes, NUM_SAVED_PATH_NODES);
3254
977
    const int l = pack_nodes(m->log, data, NUM_SAVED_PATH_NODES * packed_node_size(net_family_tcp_ipv6()), nodes, num);
3255
3256
977
    if (l > 0) {
3257
33
        const uint32_t len = l;
3258
33
        data = state_write_section_header(temp_data, STATE_COOKIE_TYPE, len, STATE_TYPE_PATH_NODE);
3259
33
        data += len;
3260
33
    }
3261
3262
977
    return data;
3263
977
}
3264
3265
static State_Load_Status load_path_nodes(Messenger *_Nonnull m, const uint8_t *_Nonnull data, uint32_t length)
3266
512
{
3267
512
    if (length > 0) {
3268
444
        Node_format nodes[NUM_SAVED_PATH_NODES];
3269
444
        const int num = unpack_nodes(nodes, NUM_SAVED_PATH_NODES, nullptr, data, length, false);
3270
3271
444
        if (num == -1) {
3272
46
            return STATE_LOAD_STATUS_CONTINUE;
3273
46
        }
3274
3275
3.10k
        for (int i = 0; i < num; ++i) {
3276
2.70k
            onion_add_bs_path_node(m->onion_c, &nodes[i].ip_port, nodes[i].public_key);
3277
2.70k
        }
3278
398
    }
3279
3280
466
    return STATE_LOAD_STATUS_CONTINUE;
3281
512
}
3282
3283
static void m_register_default_plugins(Messenger *_Nonnull m)
3284
1.47k
{
3285
1.47k
    m_register_state_plugin(m, STATE_TYPE_NOSPAMKEYS, nospam_keys_size, load_nospam_keys, save_nospam_keys);
3286
1.47k
    m_register_state_plugin(m, STATE_TYPE_DHT, m_dht_size, m_dht_load, save_dht);
3287
1.47k
    m_register_state_plugin(m, STATE_TYPE_FRIENDS, saved_friendslist_size, friends_list_load, friends_list_save);
3288
1.47k
    m_register_state_plugin(m, STATE_TYPE_NAME, name_size, load_name, save_name);
3289
1.47k
    m_register_state_plugin(m, STATE_TYPE_STATUSMESSAGE, status_message_size, load_status_message,
3290
1.47k
                            save_status_message);
3291
1.47k
    m_register_state_plugin(m, STATE_TYPE_STATUS, status_size, load_status, save_status);
3292
1.47k
    if (m->options.groups_persistence_enabled) {
3293
1.39k
        m_register_state_plugin(m, STATE_TYPE_GROUPS, saved_groups_size, groups_load, groups_save);
3294
1.39k
    }
3295
1.47k
    m_register_state_plugin(m, STATE_TYPE_TCP_RELAY, tcp_relay_size, load_tcp_relays, save_tcp_relays);
3296
1.47k
    m_register_state_plugin(m, STATE_TYPE_PATH_NODE, path_node_size, load_path_nodes, save_path_nodes);
3297
1.47k
}
3298
3299
bool messenger_load_state_section(Messenger *m, const uint8_t *data, uint32_t length, uint16_t type,
3300
                                  State_Load_Status *status)
3301
32.0k
{
3302
216k
    for (uint8_t i = 0; i < m->options.state_plugins_length; ++i) {
3303
216k
        const Messenger_State_Plugin *const plugin = &m->options.state_plugins[i];
3304
3305
216k
        if (plugin->type == type) {
3306
31.1k
            *status = plugin->load(m, data, length);
3307
31.1k
            return true;
3308
31.1k
        }
3309
216k
    }
3310
3311
879
    return false;
3312
32.0k
}
3313
3314
/** @brief Return the number of friends in the instance m.
3315
 *
3316
 * You should use this to determine how much memory to allocate
3317
 * for copy_friendlist.
3318
 */
3319
uint32_t count_friendlist(const Messenger *m)
3320
2.93k
{
3321
2.93k
    uint32_t ret = 0;
3322
3323
3.30k
    for (uint32_t i = 0; i < m->numfriends; ++i) {
3324
372
        if (m->friendlist[i].status > 0) {
3325
372
            ++ret;
3326
372
        }
3327
372
    }
3328
3329
2.93k
    return ret;
3330
2.93k
}
3331
3332
/** @brief Copy a list of valid friend IDs into the array out_list.
3333
 * If out_list is NULL, returns 0.
3334
 * Otherwise, returns the number of elements copied.
3335
 * If the array was too small, the contents
3336
 * of out_list will be truncated to list_size.
3337
 */
3338
uint32_t copy_friendlist(const Messenger *m, uint32_t *out_list, uint32_t list_size)
3339
0
{
3340
0
    if (out_list == nullptr) {
3341
0
        return 0;
3342
0
    }
3343
3344
0
    if (m->numfriends == 0) {
3345
0
        return 0;
3346
0
    }
3347
3348
0
    uint32_t ret = 0;
3349
3350
0
    for (uint32_t i = 0; i < m->numfriends; ++i) {
3351
0
        if (ret >= list_size) {
3352
0
            break; /* Abandon ship */
3353
0
        }
3354
3355
0
        if (m->friendlist[i].status > 0) {
3356
0
            out_list[ret] = i;
3357
0
            ++ret;
3358
0
        }
3359
0
    }
3360
3361
0
    return ret;
3362
0
}
3363
3364
static fr_friend_request_cb m_handle_friend_request;
3365
static void m_handle_friend_request(
3366
    void *_Nonnull object, const uint8_t *_Nonnull public_key, const uint8_t *_Nonnull message, size_t length, void *_Nullable user_data)
3367
0
{
3368
0
    Messenger *m = (Messenger *)object;
3369
0
    assert(m != nullptr);
3370
0
    m->friend_request(m, public_key, message, length, user_data);
3371
0
}
3372
3373
/** @brief Run this at startup.
3374
 *
3375
 * @return allocated instance of Messenger on success.
3376
 * @retval 0 if there are problems.
3377
 *
3378
 * if error is not NULL it will be set to one of the values in the enum above.
3379
 */
3380
Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random *rng, const Network *ns,
3381
                         Messenger_Options *options, Messenger_Error *error)
3382
1.62k
{
3383
1.62k
    if (options == nullptr) {
3384
0
        return nullptr;
3385
0
    }
3386
3387
1.62k
    if (error != nullptr) {
3388
1.62k
        *error = MESSENGER_ERROR_OTHER;
3389
1.62k
    }
3390
3391
1.62k
    Messenger *m = (Messenger *)mem_alloc(mem, sizeof(Messenger));
3392
3393
1.62k
    if (m == nullptr) {
3394
1
        return nullptr;
3395
1
    }
3396
3397
1.61k
    m->log = options->log;
3398
1.61k
    m->mono_time = mono_time;
3399
1.61k
    m->mem = mem;
3400
1.61k
    m->rng = rng;
3401
1.61k
    m->ns = ns;
3402
1.61k
    m->forwarding = nullptr;
3403
1.61k
    m->announce = nullptr;
3404
1.61k
    m->tcp_server = nullptr;
3405
3406
1.61k
    Friend_Requests *fr = friendreq_new(mem);
3407
1.61k
    if (fr == nullptr) {
3408
1
        mem_delete(mem, m);
3409
1
        return nullptr;
3410
1
    }
3411
1.61k
    m->fr = fr;
3412
3413
1.61k
    unsigned int net_err = 0;
3414
3415
1.61k
    if (!options->udp_disabled && options->proxy_info.proxy_type != TCP_PROXY_NONE) {
3416
        // We don't currently support UDP over proxy.
3417
137
        LOGGER_INFO(m->log, "UDP enabled and proxy set: disabling UDP");
3418
137
        options->udp_disabled = true;
3419
137
    }
3420
3421
1.61k
    Networking_Core *net;
3422
1.61k
    if (options->udp_disabled) {
3423
137
        net = new_networking_no_udp(m->log, m->mem, m->ns);
3424
1.48k
    } else {
3425
1.48k
        IP ip;
3426
1.48k
        ip_init(&ip, options->ipv6enabled);
3427
1.48k
        net = new_networking_ex(m->log, m->mem, m->ns, &ip, options->port_range[0], options->port_range[1], &net_err);
3428
1.48k
    }
3429
3430
1.61k
    if (net == nullptr) {
3431
3
        friendreq_kill(m->fr);
3432
3433
3
        if (error != nullptr && net_err == 1) {
3434
0
            LOGGER_WARNING(m->log, "network initialisation failed (no ports available)");
3435
0
            *error = MESSENGER_ERROR_PORT;
3436
0
        }
3437
3438
3
        mem_delete(mem, m);
3439
3
        return nullptr;
3440
3
    }
3441
1.61k
    m->net = net;
3442
3443
1.61k
    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);
3444
1.61k
    if (dht == nullptr) {
3445
12
        kill_networking(m->net);
3446
12
        friendreq_kill(m->fr);
3447
12
        mem_delete(mem, m);
3448
12
        return nullptr;
3449
12
    }
3450
1.60k
    m->dht = dht;
3451
3452
1.60k
    Net_Profile *tcp_np = netprof_new(m->log, mem);
3453
1.60k
    if (tcp_np == nullptr) {
3454
1
        LOGGER_WARNING(m->log, "TCP netprof initialisation failed");
3455
1
        kill_dht(m->dht);
3456
1
        kill_networking(m->net);
3457
1
        friendreq_kill(m->fr);
3458
1
        mem_delete(mem, m);
3459
1
        return nullptr;
3460
1
    }
3461
1.60k
    m->tcp_np = tcp_np;
3462
3463
1.60k
    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);
3464
3465
1.60k
    if (net_crypto == nullptr) {
3466
3
        LOGGER_WARNING(m->log, "net_crypto initialisation failed");
3467
3468
3
        netprof_kill(mem, m->tcp_np);
3469
3
        kill_dht(m->dht);
3470
3
        kill_networking(m->net);
3471
3
        friendreq_kill(m->fr);
3472
3
        mem_delete(mem, m);
3473
3
        return nullptr;
3474
3
    }
3475
1.59k
    m->net_crypto = net_crypto;
3476
3477
1.59k
    GC_Announces_List *group_announce = new_gca_list(m->mem);
3478
1.59k
    if (group_announce == nullptr) {
3479
1
        LOGGER_WARNING(m->log, "DHT group chats initialisation failed");
3480
3481
1
        kill_net_crypto(m->net_crypto);
3482
1
        netprof_kill(mem, m->tcp_np);
3483
1
        kill_dht(m->dht);
3484
1
        kill_networking(m->net);
3485
1
        friendreq_kill(m->fr);
3486
1
        mem_delete(mem, m);
3487
1
        return nullptr;
3488
1
    }
3489
1.59k
    m->group_announce = group_announce;
3490
3491
1.59k
    if (options->dht_announcements_enabled) {
3492
1.59k
        m->forwarding = new_forwarding(m->log, m->mem, m->rng, m->mono_time, m->dht, m->net);
3493
1.59k
        if (m->forwarding != nullptr) {
3494
1.58k
            m->announce = new_announcements(m->log, m->mem, m->rng, m->mono_time, m->forwarding, m->dht, m->net);
3495
1.58k
        }
3496
1.59k
    }
3497
3498
1.59k
    Onion *onion = new_onion(m->log, m->mem, m->mono_time, m->rng, m->dht, m->net);
3499
1.59k
    Onion_Announce *onion_a = new_onion_announce(m->log, m->mem, m->rng, m->mono_time, m->dht, m->net);
3500
1.59k
    Onion_Client *onion_c = new_onion_client(m->log, m->mem, m->rng, m->mono_time, m->net_crypto, m->dht, m->net);
3501
1.59k
    Friend_Connections *fr_c = nullptr;
3502
3503
1.59k
    if (onion_c != nullptr) {
3504
1.59k
        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);
3505
1.59k
    }
3506
3507
1.59k
    if ((options->dht_announcements_enabled && (m->forwarding == nullptr || m->announce == nullptr)) ||
3508
1.57k
            onion == nullptr || onion_a == nullptr || onion_c == nullptr || fr_c == nullptr) {
3509
24
        LOGGER_WARNING(m->log, "onion initialisation failed");
3510
3511
24
        kill_onion(onion);
3512
24
        kill_onion_announce(onion_a);
3513
24
        kill_onion_client(onion_c);
3514
24
        kill_gca(m->group_announce);
3515
24
        kill_friend_connections(fr_c);
3516
24
        kill_announcements(m->announce);
3517
24
        kill_forwarding(m->forwarding);
3518
24
        kill_net_crypto(m->net_crypto);
3519
24
        netprof_kill(mem, m->tcp_np);
3520
24
        kill_dht(m->dht);
3521
24
        kill_networking(m->net);
3522
24
        friendreq_kill(m->fr);
3523
24
        mem_delete(mem, m);
3524
24
        return nullptr;
3525
24
    }
3526
1.57k
    m->onion = onion;
3527
1.57k
    m->onion_a = onion_a;
3528
1.57k
    m->onion_c = onion_c;
3529
1.57k
    m->fr_c = fr_c;
3530
3531
1.57k
    gca_onion_init(m->group_announce, m->onion_a);
3532
3533
1.57k
    GC_Session *group_handler = new_dht_groupchats(m);
3534
1.57k
    if (group_handler == nullptr) {
3535
1
        LOGGER_WARNING(m->log, "conferences initialisation failed");
3536
3537
1
        kill_onion(m->onion);
3538
1
        kill_onion_announce(m->onion_a);
3539
1
        kill_onion_client(m->onion_c);
3540
1
        kill_gca(m->group_announce);
3541
1
        kill_friend_connections(m->fr_c);
3542
1
        kill_announcements(m->announce);
3543
1
        kill_forwarding(m->forwarding);
3544
1
        kill_net_crypto(m->net_crypto);
3545
1
        netprof_kill(mem, m->tcp_np);
3546
1
        kill_dht(m->dht);
3547
1
        kill_networking(m->net);
3548
1
        friendreq_kill(m->fr);
3549
1
        mem_delete(mem, m);
3550
1
        return nullptr;
3551
1
    }
3552
1.57k
    m->group_handler = group_handler;
3553
3554
1.57k
    if (options->tcp_server_port != 0) {
3555
147
        m->tcp_server = new_tcp_server(m->log, m->mem, m->rng, m->ns, options->ipv6enabled, 1,
3556
147
                                       &options->tcp_server_port, dht_get_self_secret_key(m->dht),
3557
147
                                       m->onion, m->forwarding);
3558
3559
147
        if (m->tcp_server == nullptr) {
3560
102
            LOGGER_WARNING(m->log, "TCP server initialisation failed");
3561
3562
102
            kill_onion(m->onion);
3563
102
            kill_onion_announce(m->onion_a);
3564
102
            kill_dht_groupchats(m->group_handler);
3565
102
            kill_friend_connections(m->fr_c);
3566
102
            kill_onion_client(m->onion_c);
3567
102
            kill_gca(m->group_announce);
3568
102
            kill_announcements(m->announce);
3569
102
            kill_forwarding(m->forwarding);
3570
102
            kill_net_crypto(m->net_crypto);
3571
102
            netprof_kill(mem, m->tcp_np);
3572
102
            kill_dht(m->dht);
3573
102
            kill_networking(m->net);
3574
102
            friendreq_kill(m->fr);
3575
102
            mem_delete(mem, m);
3576
3577
102
            if (error != nullptr) {
3578
102
                *error = MESSENGER_ERROR_TCP_SERVER;
3579
102
            }
3580
3581
102
            return nullptr;
3582
102
        }
3583
147
    }
3584
3585
1.47k
    m->options = *options;
3586
1.47k
    friendreq_init(m->fr, m->fr_c);
3587
1.47k
    set_nospam(m->fr, random_u32(m->rng));
3588
1.47k
    set_filter_function(m->fr, &friend_already_added, m);
3589
3590
1.47k
    m->lastdump = 0;
3591
1.47k
    m->is_receiving_file = 0;
3592
3593
1.47k
    m_register_default_plugins(m);
3594
1.47k
    callback_friendrequest(m->fr, m_handle_friend_request, m);
3595
3596
1.47k
    if (error != nullptr) {
3597
1.47k
        *error = MESSENGER_ERROR_NONE;
3598
1.47k
    }
3599
3600
1.47k
    return m;
3601
1.57k
}
3602
3603
/** @brief Run this before closing shop.
3604
 *
3605
 * Free all datastructures.
3606
 */
3607
void kill_messenger(Messenger *m)
3608
1.47k
{
3609
1.47k
    if (m == nullptr) {
3610
0
        return;
3611
0
    }
3612
3613
1.47k
    if (m->tcp_server != nullptr) {
3614
45
        kill_tcp_server(m->tcp_server);
3615
45
    }
3616
3617
1.47k
    kill_onion(m->onion);
3618
1.47k
    kill_onion_announce(m->onion_a);
3619
1.47k
    kill_dht_groupchats(m->group_handler);
3620
1.47k
    kill_friend_connections(m->fr_c);
3621
1.47k
    kill_onion_client(m->onion_c);
3622
1.47k
    kill_gca(m->group_announce);
3623
1.47k
    kill_announcements(m->announce);
3624
1.47k
    kill_forwarding(m->forwarding);
3625
1.47k
    kill_net_crypto(m->net_crypto);
3626
1.47k
    netprof_kill(m->mem, m->tcp_np);
3627
1.47k
    kill_dht(m->dht);
3628
1.47k
    kill_networking(m->net);
3629
3630
1.80k
    for (uint32_t i = 0; i < m->numfriends; ++i) {
3631
329
        clear_receipts(m, i);
3632
329
    }
3633
3634
1.47k
    mem_delete(m->mem, m->friendlist);
3635
1.47k
    friendreq_kill(m->fr);
3636
3637
1.47k
    mem_delete(m->mem, m->options.state_plugins);
3638
1.47k
    mem_delete(m->mem, m);
3639
1.47k
}
3640
3641
bool m_is_receiving_file(Messenger *m)
3642
0
{
3643
    // Only run the expensive loop below once every 64 tox_iterate calls.
3644
0
    const uint8_t skip_count = 64;
3645
3646
0
    if (m->is_receiving_file != 0) {
3647
0
        --m->is_receiving_file;
3648
0
        return true;
3649
0
    }
3650
3651
    // TODO(iphydf): This is a very expensive loop. Consider keeping track of
3652
    // the number of live file transfers.
3653
0
    for (size_t friend_number = 0; friend_number < m->numfriends; ++friend_number) {
3654
0
        for (size_t i = 0; i < MAX_CONCURRENT_FILE_PIPES; ++i) {
3655
0
            if (m->friendlist[friend_number].file_receiving[i].status == FILESTATUS_TRANSFERRING) {
3656
0
                m->is_receiving_file = skip_count;
3657
0
                return true;
3658
0
            }
3659
0
        }
3660
0
    }
3661
3662
0
    return false;
3663
0
}