summaryrefslogtreecommitdiff
path: root/toxcore/group_chats.c
blob: f37c6a9c9758110faf90932566f46864bacca4f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/* group_chats.c
 *
 * An implementation of massive text only group chats.
 *
 *
 *  Copyright (C) 2013 Tox project All Rights Reserved.
 *
 *  This file is part of Tox.
 *
 *  Tox is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Tox is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Tox.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "group_chats.h"


#define GROUPCHAT_MAXDATA_LENGTH (MAX_DATA_SIZE - (1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES))
#define GROUPCHAT_MAXPLAINDATA_LENGTH (GROUPCHAT_MAXDATA_LENGTH - crypto_box_MACBYTES)

#define GROUP_MAX_SENDNODES (GROUP_CLOSE_CONNECTIONS * 2)

typedef struct {
    uint64_t   pingid;
    //uint8_t    client_id[crypto_box_PUBLICKEYBYTES];

} getnodes_data;

typedef struct {
    uint8_t    client_id[crypto_box_PUBLICKEYBYTES];
    IP_Port    ip_port;

} groupchat_nodes;

typedef struct {
    uint64_t   pingid;
    groupchat_nodes nodes[GROUP_CLOSE_CONNECTIONS];
    //uint8_t    client_id[crypto_box_PUBLICKEYBYTES];

} sendnodes_data;

/* Compares client_id1 and client_id2 with client_id
 * return 0 if both are same distance
 * return 1 if client_id1 is closer
 * return 2 if client_id2 is closer
 */
static int id_closest(uint8_t *id, uint8_t *id1, uint8_t *id2)
{
    size_t   i;
    uint8_t distance1, distance2;

    for (i = 0; i < CLIENT_ID_SIZE; ++i) {

        distance1 = abs(((int8_t *)id)[i] ^ ((int8_t *)id1)[i]);
        distance2 = abs(((int8_t *)id)[i] ^ ((int8_t *)id2)[i]);

        if (distance1 < distance2)
            return 1;

        if (distance1 > distance2)
            return 2;
    }

    return 0;
}


/*
 * check if peer with client_id is in peer array.
 *
 * return peer number if peer is in chat.
 * return -1 if peer is not in chat.
 *
 * TODO: make this more efficient.
 */

static int peer_in_chat(Group_Chat *chat, uint8_t *client_id)
{
    uint32_t i;

    for (i = 0; i < chat->numpeers; ++i) {
        /* Equal */
        if (memcmp(chat->group[i].client_id, client_id, crypto_box_PUBLICKEYBYTES) == 0)
            return i;
    }

    return -1;
}

#define BAD_NODE_TIMEOUT 30

/*
 * Check if peer is closer to us that the other peers in the list and if the peer is in the list.
 * Return the number of peers it is closer to if it is not in the closelist.
 * Return -1 if the peer is in the closelist.
 */

static int peer_okping(Group_Chat *chat, uint8_t *client_id)
{
    uint32_t i, j = 0;
    uint64_t temp_time = unix_time();

    if (memcmp(chat->self_public_key, client_id, crypto_box_PUBLICKEYBYTES) == 0)
        return -1;

    for (i = 0; i < GROUP_CLOSE_CONNECTIONS; ++i) {
        if (chat->close[i].last_recv + BAD_NODE_TIMEOUT < temp_time) {
            ++j;
            continue;
        }

        /* Equal */
        if (memcmp(chat->close[i].client_id, client_id, crypto_box_PUBLICKEYBYTES) == 0)
            return -1;

        if (id_closest(chat->self_public_key, chat->close[i].client_id, client_id) == 2)
            ++j;
    }

    return j;
}



/* Attempt to add a peer to the close list.
 * Update last_recv if it is in list.
 * Attempt to add it to list if it is not.
 *
 * Return 0 if success.
 * Return -1 if peer was not put in list/updated.
 */
static int add_closepeer(Group_Chat *chat, uint8_t *client_id, IP_Port ip_port)
{
    uint32_t i;
    uint64_t temp_time = unix_time();

    for (i = 0; i < GROUP_CLOSE_CONNECTIONS; ++i) { /* Check if node is already in list, if it is update its last_recv */
        if (memcmp(chat->close[i].client_id, client_id, crypto_box_PUBLICKEYBYTES) == 0) {
            chat->close[i].last_recv = temp_time;
            return 0;
        }
    }

    for (i = 0; i < GROUP_CLOSE_CONNECTIONS; ++i) { /* Try replacing bad nodes first */
        if (chat->close[i].last_recv + BAD_NODE_TIMEOUT < temp_time) {
            memcpy(chat->close[i].client_id, client_id, crypto_box_PUBLICKEYBYTES);
            chat->close[i].ip_port = ip_port;
            chat->close[i].last_recv = temp_time;
            return 0;
        }
    }

    for (i = 0; i < GROUP_CLOSE_CONNECTIONS; ++i) { /* Replace nodes if given one is closer. */
        if (id_closest(chat->self_public_key, chat->close[i].client_id, client_id) == 2) {
            memcpy(chat->close[i].client_id, client_id, crypto_box_PUBLICKEYBYTES);
            chat->close[i].ip_port = ip_port;
            chat->close[i].last_recv = temp_time;
            return 0;
        }
    }

    return -1;
}

static int send_groupchatpacket(Group_Chat *chat, IP_Port ip_port, uint8_t *public_key, uint8_t *data, uint32_t length,
                                uint8_t request_id)
{
    if (memcmp(chat->self_public_key, public_key, crypto_box_PUBLICKEYBYTES) == 0)
        return -1;

    uint8_t packet[MAX_DATA_SIZE];
    int len = create_request(chat->self_public_key, chat->self_secret_key, packet, public_key, data, length, request_id);
    packet[0] = NET_PACKET_GROUP_CHATS;

    if (len == -1)
        return -1;

    if (sendpacket(chat->net, ip_port, packet, len) == len)
        return 0;

    return -1;

}

/*
 * Send data to all peers in close peer list.
 *
 * return the number of peers the packet was sent to.
 */
static uint8_t sendto_allpeers(Group_Chat *chat, uint8_t *data, uint16_t length, uint8_t request_id)
{
    uint16_t sent = 0;
    uint32_t i;
    uint64_t temp_time = unix_time();

    for (i = 0; i < GROUP_CLOSE_CONNECTIONS; ++i) {
        if (ip_isset(&chat->close[i].ip_port.ip) && chat->close[i].last_recv + BAD_NODE_TIMEOUT > temp_time) {
            if (send_groupchatpacket(chat, chat->close[i].ip_port, chat->close[i].client_id, data, length, request_id) == 0)
                ++sent;
        }
    }

    return sent;
}


/*
 * Add a peer to the group chat.
 *
 * return peernum if success or peer already in chat.
 * return -1 if error.
 */
static int addpeer(Group_Chat *chat, uint8_t *client_id)
{
    int peernum = peer_in_chat(chat, client_id);

    if (peernum != -1)
        return peernum;

    Group_Peer *temp;
    temp = realloc(chat->group, sizeof(Group_Peer) * (chat->numpeers + 1));
    memset(&(temp[chat->numpeers]), 0, sizeof(Group_Peer));

    if (temp == NULL)
        return -1;

    chat->group = temp;
    memcpy(chat->group[chat->numpeers].client_id, client_id, crypto_box_PUBLICKEYBYTES);
    ++chat->numpeers;
    return (chat->numpeers - 1);
}

/*
 * Delete a peer to the group chat.
 *
 * return 0 if success
 * return -1 if error.
 */
static int delpeer(Group_Chat *chat, uint8_t *client_id)
{
    uint32_t i;
    Group_Peer *temp;

    for (i = 0; i < chat->numpeers; ++i) {
        /* Equal */
        if (memcmp(chat->group[i].client_id, client_id, crypto_box_PUBLICKEYBYTES) == 0) {
            --chat->numpeers;

            if (chat->numpeers != i) {
                memcpy( chat->group[i].client_id,
                        chat->group[chat->numpeers].client_id,
                        crypto_box_PUBLICKEYBYTES );
            }

            temp = realloc(chat->group, sizeof(Group_Peer) * (chat->numpeers));

            if (temp == NULL)
                return -1;

            chat->group = temp;
            return 0;
        }
    }

    return -1;
}
/* min time between pings sent to one peer in seconds */
#define PING_TIMEOUT 5
static int send_getnodes(Group_Chat *chat, IP_Port ip_port, int peernum)
{
    if ((uint32_t)peernum >= chat->numpeers)
        return -1;

    uint64_t temp_time = unix_time();

    getnodes_data contents;

    if (chat->group[peernum].last_pinged + PING_TIMEOUT > temp_time)
        return -1;

    contents.pingid = ((uint64_t)random_int() << 32) + random_int();
    chat->group[peernum].last_pinged = temp_time;
    chat->group[peernum].pingid = contents.pingid;
    return send_groupchatpacket(chat, ip_port, chat->group[peernum].client_id, (uint8_t *)&contents, sizeof(contents), 48);
}

static int send_sendnodes(Group_Chat *chat, IP_Port ip_port, int peernum, uint64_t pingid)
{
    if ((uint32_t)peernum >= chat->numpeers)
        return -1;

    sendnodes_data contents;
    contents.pingid = pingid;
    uint32_t i, j = 0;
    uint64_t temp_time = unix_time();

    for (i = 0; i < GROUP_CLOSE_CONNECTIONS; ++i) {
        if (chat->close[i].last_recv + BAD_NODE_TIMEOUT > temp_time) {
            memcpy(contents.nodes[j].client_id, chat->close[i].client_id, crypto_box_PUBLICKEYBYTES);
            contents.nodes[j].ip_port = chat->close[i].ip_port;
            ++j;
        }
    }

    return send_groupchatpacket(chat, ip_port, chat->group[peernum].client_id, (uint8_t *)&contents,
                                sizeof(contents.pingid) + sizeof(groupchat_nodes) * j, 49);
}

static int handle_getnodes(Group_Chat *chat, IP_Port source, int peernum, uint8_t *data, uint32_t len)
{
    if (len != sizeof(getnodes_data))
        return 1;

    if ((uint32_t)peernum >= chat->numpeers)
        return 1;

    getnodes_data contents;
    memcpy(&contents, data, sizeof(contents));
    send_sendnodes(chat, source, peernum, contents.pingid);

    if (peer_okping(chat, chat->group[peernum].client_id) > 0)
        send_getnodes(chat, source, peernum);

    return 0;
}

static int handle_sendnodes(Group_Chat *chat, IP_Port source, int peernum, uint8_t *data, uint32_t len)
{
    if ((uint32_t)peernum >= chat->numpeers)
        return 1;

    if (len > sizeof(sendnodes_data) || len < sizeof(uint64_t))
        return 1;

    if ((len - sizeof(uint64_t)) % sizeof(groupchat_nodes) != 0)
        return 1;

    if (chat->group[peernum].last_pinged + PING_TIMEOUT < unix_time())
        return 1;

    sendnodes_data contents;
    memcpy(&contents, data, len);

    if (contents.pingid != chat->group[peernum].pingid)
        return 1;

    uint16_t numnodes = (len - sizeof(contents.pingid)) / sizeof(groupchat_nodes);
    uint32_t i;

    for (i = 0; i < numnodes; ++i) {
        if (peer_okping(chat, contents.nodes[i].client_id) > 0) {
            int peern = peer_in_chat(chat, contents.nodes[i].client_id);

            if (peern == -1) { /*NOTE: This is just for testing and will be removed later.*/
                peern = addpeer(chat, contents.nodes[i].client_id);
            }

            if (peern == -1)
                continue;

            send_getnodes(chat, contents.nodes[i].ip_port, peern);
        }
    }

    add_closepeer(chat, chat->group[peernum].client_id, source);
    return 0;
}
#define GROUP_DATA_MIN_SIZE (crypto_box_PUBLICKEYBYTES + sizeof(uint32_t) + 1)
static int handle_data(Group_Chat *chat, uint8_t *data, uint32_t len)
{
    if (len < GROUP_DATA_MIN_SIZE)
        return 1;

//TODO:
    int peernum = peer_in_chat(chat, data);

    if (peernum == -1) { /*NOTE: This is just for testing and will be removed later.*/
        peernum = addpeer(chat, data);
    }

    if (peernum == -1)
        return 1;

    uint64_t temp_time = unix_time();
    /* Spam prevention (1 message per peer per second limit.)

        if (chat->group[peernum].last_recv == temp_time)
            return 1;

        chat->group[peernum].last_recv = temp_time;
    */
    uint32_t message_num;
    memcpy(&message_num, data + crypto_box_PUBLICKEYBYTES, sizeof(uint32_t));
    message_num = ntohl(message_num);

    if (chat->group[peernum].last_message_number == 0) {
        chat->group[peernum].last_message_number = message_num;
    } else if (message_num - chat->group[peernum].last_message_number > 64 ||
               message_num == chat->group[peernum].last_message_number)
        return 1;

    chat->group[peernum].last_message_number = message_num;

    int handled = 1;
    uint8_t *contents = data + GROUP_DATA_MIN_SIZE;
    uint16_t contents_len = len - GROUP_DATA_MIN_SIZE;

    switch (data[crypto_box_PUBLICKEYBYTES + sizeof(message_num)]) {
        case 0: /* If message is ping */
            if (contents_len != 0)
                return 1;

            chat->group[peernum].last_recv_msgping = temp_time;

        case 16: /* If message is new peer */
            if (contents_len != crypto_box_PUBLICKEYBYTES)
                return 1;

            addpeer(chat, contents);
            break;

        case 64: /* If message is chat message */
            if (chat->group_message != NULL)
                (*chat->group_message)(chat, peernum, contents, contents_len, chat->group_message_userdata);

            break;

        default:
            handled = 0;
            break;

    }

    if (handled == 1) {
        sendto_allpeers(chat, data, len, 50);
        return 0;
    }

    return 1;
}

static uint8_t send_data(Group_Chat *chat, uint8_t *data, uint32_t len, uint8_t message_id)
{
    if (len + GROUP_DATA_MIN_SIZE > MAX_DATA_SIZE) /*NOTE: not the real maximum len.*/
        return 1;

    uint8_t packet[MAX_DATA_SIZE];
    ++chat->message_number;

    if (chat->message_number == 0)
        chat->message_number = 1;

    uint32_t message_num = htonl(chat->message_number);
//TODO
    memcpy(packet, chat->self_public_key, crypto_box_PUBLICKEYBYTES);
    memcpy(packet + crypto_box_PUBLICKEYBYTES, &message_num, sizeof(message_num));
    memcpy(packet + GROUP_DATA_MIN_SIZE, data, len);
    packet[crypto_box_PUBLICKEYBYTES + sizeof(message_num)] = message_id;
    return sendto_allpeers(chat, packet, len + GROUP_DATA_MIN_SIZE, 50);
}
/*
 * Handle get nodes group packet.
 *
 * return 0 if handled correctly.
 * return 1 if error.
 */

int handle_groupchatpacket(Group_Chat *chat, IP_Port source, uint8_t *packet, uint32_t length)
{
    if (length > MAX_DATA_SIZE)
        return 1;

    uint8_t public_key[crypto_box_PUBLICKEYBYTES];
    uint8_t data[MAX_DATA_SIZE];
    uint8_t number;
    int len = handle_request(chat->self_public_key, chat->self_secret_key, public_key, data, &number, packet, length);

    if (len <= 0)
        return 1;

    if (memcmp(chat->self_public_key, public_key, crypto_box_PUBLICKEYBYTES) == 0)
        return 1;

    int peernum = peer_in_chat(chat, public_key);

    if (peernum == -1)
        return 1;

    switch (number) {
        case 48:
            return handle_getnodes(chat, source, peernum, data, len);

        case 49:
            return handle_sendnodes(chat, source, peernum, data, len);

        case 50:
            return handle_data(chat, data, len);

        default:
            return 1;
    }

    return 1;
}

uint32_t group_sendmessage(Group_Chat *chat, uint8_t *message, uint32_t length)
{
    return send_data(chat, message, length, 64); //TODO: better return values?
}

uint32_t group_newpeer(Group_Chat *chat, uint8_t *client_id)
{
    addpeer(chat, client_id);
    return send_data(chat, client_id, crypto_box_PUBLICKEYBYTES, 16); //TODO: better return values?
}

void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat, int, uint8_t *, uint16_t, void *),
                           void *userdata)
{
    chat->group_message = function;
    chat->group_message_userdata = userdata;
}

Group_Chat *new_groupchat(Networking_Core *net)
{
    if (net == 0)
        return 0;

    Group_Chat *chat = calloc(1, sizeof(Group_Chat));
    chat->net = net;
    crypto_box_keypair(chat->self_public_key, chat->self_secret_key);
    return chat;
}

#define NODE_PING_INTERVAL 10

static void ping_close(Group_Chat *chat)
{
    uint32_t i;
    uint64_t temp_time = unix_time();

    for (i = 0; i < GROUP_CLOSE_CONNECTIONS; ++i) {
        if (chat->close[i].last_recv < temp_time + BAD_NODE_TIMEOUT) {
            int peernum = peer_in_chat(chat, chat->close[i].client_id);

            if (peernum == -1)
                continue;

            if (chat->group[peernum].last_pinged + NODE_PING_INTERVAL < temp_time)
                send_getnodes(chat, chat->close[i].ip_port, peernum);
        }
    }
}

void do_groupchat(Group_Chat *chat)
{
    ping_close(chat);
}

void kill_groupchat(Group_Chat *chat)
{
    free(chat->group);
    free(chat);
}

void chat_bootstrap(Group_Chat *chat, IP_Port ip_port, uint8_t *client_id)
{
    send_getnodes(chat, ip_port, addpeer(chat, client_id));
}

void chat_bootstrap_nonlazy(Group_Chat *chat, IP_Port ip_port, uint8_t *client_id)
{
    send_getnodes(chat, ip_port, addpeer(chat, client_id));
    add_closepeer(chat, client_id, ip_port);
}