summaryrefslogtreecommitdiff
path: root/toxcore/group_chats.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-09-09 14:16:53 -0400
committerirungentoo <irungentoo@gmail.com>2013-09-09 14:16:53 -0400
commitc137ec9d4d37dedf4d2a40d8c573eed4239a8ec1 (patch)
treea0f4924cac57968c3f06d85fba575311e278e1ed /toxcore/group_chats.c
parent00c9c8ab57a21253406d2b73e5ca9404f44f3583 (diff)
move group chats to /toxcore
Diffstat (limited to 'toxcore/group_chats.c')
-rw-r--r--toxcore/group_chats.c579
1 files changed, 579 insertions, 0 deletions
diff --git a/toxcore/group_chats.c b/toxcore/group_chats.c
new file mode 100644
index 00000000..3c134348
--- /dev/null
+++ b/toxcore/group_chats.c
@@ -0,0 +1,579 @@
1/* group_chats.c
2 *
3 * An implementation of massive text only group chats.
4 *
5 *
6 * Copyright (C) 2013 Tox project All Rights Reserved.
7 *
8 * This file is part of Tox.
9 *
10 * Tox is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * Tox is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
22 *
23 */
24
25#include "group_chats.h"
26
27
28#define GROUPCHAT_MAXDATA_LENGTH (MAX_DATA_SIZE - (1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES))
29#define GROUPCHAT_MAXPLAINDATA_LENGTH (GROUPCHAT_MAXDATA_LENGTH - crypto_box_MACBYTES)
30
31#define GROUP_MAX_SENDNODES (GROUP_CLOSE_CONNECTIONS * 2)
32
33typedef struct {
34 uint64_t pingid;
35 //uint8_t client_id[crypto_box_PUBLICKEYBYTES];
36
37} getnodes_data;
38
39typedef struct {
40 uint8_t client_id[crypto_box_PUBLICKEYBYTES];
41 IP_Port ip_port;
42
43} groupchat_nodes;
44
45typedef struct {
46 uint64_t pingid;
47 groupchat_nodes nodes[GROUP_CLOSE_CONNECTIONS];
48 //uint8_t client_id[crypto_box_PUBLICKEYBYTES];
49
50} sendnodes_data;
51
52/* Compares client_id1 and client_id2 with client_id
53 * return 0 if both are same distance
54 * return 1 if client_id1 is closer
55 * return 2 if client_id2 is closer
56 */
57static int id_closest(uint8_t *id, uint8_t *id1, uint8_t *id2)
58{
59 size_t i;
60 uint8_t distance1, distance2;
61
62 for (i = 0; i < CLIENT_ID_SIZE; ++i) {
63
64 distance1 = abs(((int8_t *)id)[i] ^ ((int8_t *)id1)[i]);
65 distance2 = abs(((int8_t *)id)[i] ^ ((int8_t *)id2)[i]);
66
67 if (distance1 < distance2)
68 return 1;
69
70 if (distance1 > distance2)
71 return 2;
72 }
73
74 return 0;
75}
76
77
78/*
79 * check if peer with client_id is in peer array.
80 *
81 * return peer number if peer is in chat.
82 * return -1 if peer is not in chat.
83 *
84 * TODO: make this more efficient.
85 */
86
87static int peer_in_chat(Group_Chat *chat, uint8_t *client_id)
88{
89 uint32_t i;
90
91 for (i = 0; i < chat->numpeers; ++i) {
92 /* Equal */
93 if (memcmp(chat->group[i].client_id, client_id, crypto_box_PUBLICKEYBYTES) == 0)
94 return i;
95 }
96
97 return -1;
98}
99
100#define BAD_NODE_TIMEOUT 30
101
102/*
103 * Check if peer is closer to us that the other peers in the list and if the peer is in the list.
104 * Return the number of peers it is closer to if it is not in the closelist.
105 * Return -1 if the peer is in the closelist.
106 */
107
108static int peer_okping(Group_Chat *chat, uint8_t *client_id)
109{
110 uint32_t i, j = 0;
111 uint64_t temp_time = unix_time();
112
113 if (memcmp(chat->self_public_key, client_id, crypto_box_PUBLICKEYBYTES) == 0)
114 return -1;
115
116 for (i = 0; i < GROUP_CLOSE_CONNECTIONS; ++i) {
117 if (chat->close[i].last_recv + BAD_NODE_TIMEOUT < temp_time) {
118 ++j;
119 continue;
120 }
121
122 /* Equal */
123 if (memcmp(chat->close[i].client_id, client_id, crypto_box_PUBLICKEYBYTES) == 0)
124 return -1;
125
126 if (id_closest(chat->self_public_key, chat->close[i].client_id, client_id) == 2)
127 ++j;
128 }
129
130 return j;
131}
132
133
134
135/* Attempt to add a peer to the close list.
136 * Update last_recv if it is in list.
137 * Attempt to add it to list if it is not.
138 *
139 * Return 0 if success.
140 * Return -1 if peer was not put in list/updated.
141 */
142static int add_closepeer(Group_Chat *chat, uint8_t *client_id, IP_Port ip_port)
143{
144 uint32_t i;
145 uint64_t temp_time = unix_time();
146
147 for (i = 0; i < GROUP_CLOSE_CONNECTIONS; ++i) { /* Check if node is already in list, if it is update its last_recv */
148 if (memcmp(chat->close[i].client_id, client_id, crypto_box_PUBLICKEYBYTES) == 0) {
149 chat->close[i].last_recv = temp_time;
150 return 0;
151 }
152 }
153
154 for (i = 0; i < GROUP_CLOSE_CONNECTIONS; ++i) { /* Try replacing bad nodes first */
155 if (chat->close[i].last_recv + BAD_NODE_TIMEOUT < temp_time) {
156 memcpy(chat->close[i].client_id, client_id, crypto_box_PUBLICKEYBYTES);
157 chat->close[i].ip_port = ip_port;
158 chat->close[i].last_recv = temp_time;
159 return 0;
160 }
161 }
162
163 for (i = 0; i < GROUP_CLOSE_CONNECTIONS; ++i) { /* Replace nodes if given one is closer. */
164 if (id_closest(chat->self_public_key, chat->close[i].client_id, client_id) == 2) {
165 memcpy(chat->close[i].client_id, client_id, crypto_box_PUBLICKEYBYTES);
166 chat->close[i].ip_port = ip_port;
167 chat->close[i].last_recv = temp_time;
168 return 0;
169 }
170 }
171
172 return -1;
173}
174
175static int send_groupchatpacket(Group_Chat *chat, IP_Port ip_port, uint8_t *public_key, uint8_t *data, uint32_t length,
176 uint8_t request_id)
177{
178 if (memcmp(chat->self_public_key, public_key, crypto_box_PUBLICKEYBYTES) == 0)
179 return -1;
180
181 uint8_t packet[MAX_DATA_SIZE];
182 int len = create_request(chat->self_public_key, chat->self_secret_key, packet, public_key, data, length, request_id);
183 packet[0] = 48;
184
185 if (len == -1)
186 return -1;
187
188 if (sendpacket(chat->net->sock, ip_port, packet, len) == len)
189 return 0;
190
191 return -1;
192
193}
194
195/*
196 * Send data to all peers in close peer list.
197 *
198 * return the number of peers the packet was sent to.
199 */
200static uint8_t sendto_allpeers(Group_Chat *chat, uint8_t *data, uint16_t length, uint8_t request_id)
201{
202 uint16_t sent = 0;
203 uint32_t i;
204 uint64_t temp_time = unix_time();
205
206 for (i = 0; i < GROUP_CLOSE_CONNECTIONS; ++i) {
207 if (chat->close[i].ip_port.ip.uint32 != 0 && chat->close[i].last_recv + BAD_NODE_TIMEOUT > temp_time) {
208 if (send_groupchatpacket(chat, chat->close[i].ip_port, chat->close[i].client_id, data, length, request_id) == 0)
209 ++sent;
210 }
211 }
212
213 return sent;
214}
215
216
217/*
218 * Add a peer to the group chat.
219 *
220 * return peernum if success or peer already in chat.
221 * return -1 if error.
222 */
223static int addpeer(Group_Chat *chat, uint8_t *client_id)
224{
225 int peernum = peer_in_chat(chat, client_id);
226
227 if (peernum != -1)
228 return peernum;
229
230 Group_Peer *temp;
231 temp = realloc(chat->group, sizeof(Group_Peer) * (chat->numpeers + 1));
232 memset(&(temp[chat->numpeers]), 0, sizeof(Group_Peer));
233
234 if (temp == NULL)
235 return -1;
236
237 chat->group = temp;
238 memcpy(chat->group[chat->numpeers].client_id, client_id, crypto_box_PUBLICKEYBYTES);
239 ++chat->numpeers;
240 return (chat->numpeers - 1);
241}
242
243/*
244 * Delete a peer to the group chat.
245 *
246 * return 0 if success
247 * return -1 if error.
248 */
249static int delpeer(Group_Chat *chat, uint8_t *client_id)
250{
251 uint32_t i;
252 Group_Peer *temp;
253
254 for (i = 0; i < chat->numpeers; ++i) {
255 /* Equal */
256 if (memcmp(chat->group[i].client_id, client_id, crypto_box_PUBLICKEYBYTES) == 0) {
257 --chat->numpeers;
258
259 if (chat->numpeers != i) {
260 memcpy( chat->group[i].client_id,
261 chat->group[chat->numpeers].client_id,
262 crypto_box_PUBLICKEYBYTES );
263 }
264
265 temp = realloc(chat->group, sizeof(Group_Peer) * (chat->numpeers));
266
267 if (temp == NULL)
268 return -1;
269
270 chat->group = temp;
271 return 0;
272 }
273 }
274
275 return -1;
276}
277/* min time between pings sent to one peer in seconds */
278#define PING_TIMEOUT 5
279static int send_getnodes(Group_Chat *chat, IP_Port ip_port, int peernum)
280{
281 if ((uint32_t)peernum >= chat->numpeers)
282 return -1;
283
284 uint64_t temp_time = unix_time();
285
286 getnodes_data contents;
287
288 if (chat->group[peernum].last_pinged + PING_TIMEOUT > temp_time)
289 return -1;
290
291 contents.pingid = ((uint64_t)random_int() << 32) + random_int();
292 chat->group[peernum].last_pinged = temp_time;
293 chat->group[peernum].pingid = contents.pingid;
294 return send_groupchatpacket(chat, ip_port, chat->group[peernum].client_id, (uint8_t *)&contents, sizeof(contents), 48);
295}
296
297static int send_sendnodes(Group_Chat *chat, IP_Port ip_port, int peernum, uint64_t pingid)
298{
299 if ((uint32_t)peernum >= chat->numpeers)
300 return -1;
301
302 sendnodes_data contents;
303 contents.pingid = pingid;
304 uint32_t i, j = 0;
305 uint64_t temp_time = unix_time();
306
307 for (i = 0; i < GROUP_CLOSE_CONNECTIONS; ++i) {
308 if (chat->close[i].last_recv + BAD_NODE_TIMEOUT > temp_time) {
309 memcpy(contents.nodes[j].client_id, chat->close[i].client_id, crypto_box_PUBLICKEYBYTES);
310 contents.nodes[j].ip_port = chat->close[i].ip_port;
311 ++j;
312 }
313 }
314
315 return send_groupchatpacket(chat, ip_port, chat->group[peernum].client_id, (uint8_t *)&contents,
316 sizeof(contents.pingid) + sizeof(groupchat_nodes) * j, 49);
317}
318
319static int handle_getnodes(Group_Chat *chat, IP_Port source, int peernum, uint8_t *data, uint32_t len)
320{
321 if (len != sizeof(getnodes_data))
322 return 1;
323
324 if ((uint32_t)peernum >= chat->numpeers)
325 return 1;
326
327 getnodes_data contents;
328 memcpy(&contents, data, sizeof(contents));
329 send_sendnodes(chat, source, peernum, contents.pingid);
330
331 if (peer_okping(chat, chat->group[peernum].client_id) > 0)
332 send_getnodes(chat, source, peernum);
333
334 return 0;
335}
336
337static int handle_sendnodes(Group_Chat *chat, IP_Port source, int peernum, uint8_t *data, uint32_t len)
338{
339 if ((uint32_t)peernum >= chat->numpeers)
340 return 1;
341
342 if (len > sizeof(sendnodes_data) || len < sizeof(uint64_t))
343 return 1;
344
345 if ((len - sizeof(uint64_t)) % sizeof(groupchat_nodes) != 0)
346 return 1;
347
348 if (chat->group[peernum].last_pinged + PING_TIMEOUT < unix_time())
349 return 1;
350
351 sendnodes_data contents;
352 memcpy(&contents, data, len);
353
354 if (contents.pingid != chat->group[peernum].pingid)
355 return 1;
356
357 uint16_t numnodes = (len - sizeof(contents.pingid)) / sizeof(groupchat_nodes);
358 uint32_t i;
359
360 for (i = 0; i < numnodes; ++i) {
361 if (peer_okping(chat, contents.nodes[i].client_id) > 0) {
362 int peern = peer_in_chat(chat, contents.nodes[i].client_id);
363
364 if (peern == -1) { /*NOTE: This is just for testing and will be removed later.*/
365 peern = addpeer(chat, contents.nodes[i].client_id);
366 }
367
368 if (peern == -1)
369 continue;
370
371 send_getnodes(chat, contents.nodes[i].ip_port, peern);
372 }
373 }
374
375 add_closepeer(chat, chat->group[peernum].client_id, source);
376 return 0;
377}
378#define GROUP_DATA_MIN_SIZE (crypto_box_PUBLICKEYBYTES + sizeof(uint32_t) + 1)
379static int handle_data(Group_Chat *chat, uint8_t *data, uint32_t len)
380{
381 if (len < GROUP_DATA_MIN_SIZE)
382 return 1;
383
384//TODO:
385 int peernum = peer_in_chat(chat, data);
386
387 if (peernum == -1) { /*NOTE: This is just for testing and will be removed later.*/
388 peernum = addpeer(chat, data);
389 }
390
391 if (peernum == -1)
392 return 1;
393
394 uint64_t temp_time = unix_time();
395 /* Spam prevention (1 message per peer per second limit.)
396
397 if (chat->group[peernum].last_recv == temp_time)
398 return 1;
399
400 chat->group[peernum].last_recv = temp_time;
401 */
402 uint32_t message_num;
403 memcpy(&message_num, data + crypto_box_PUBLICKEYBYTES, sizeof(uint32_t));
404 message_num = ntohl(message_num);
405
406 if (chat->group[peernum].last_message_number == 0) {
407 chat->group[peernum].last_message_number = message_num;
408 } else if (message_num - chat->group[peernum].last_message_number > 64 ||
409 message_num == chat->group[peernum].last_message_number)
410 return 1;
411
412 chat->group[peernum].last_message_number = message_num;
413
414 int handled = 1;
415 uint8_t *contents = data + GROUP_DATA_MIN_SIZE;
416 uint16_t contents_len = len - GROUP_DATA_MIN_SIZE;
417
418 switch (data[crypto_box_PUBLICKEYBYTES + sizeof(message_num)]) {
419 case 0: /* If message is ping */
420 if (contents_len != 0)
421 return 1;
422
423 chat->group[peernum].last_recv_msgping = temp_time;
424
425 case 16: /* If message is new peer */
426 if (contents_len != crypto_box_PUBLICKEYBYTES)
427 return 1;
428
429 addpeer(chat, contents);
430 break;
431
432 case 64: /* If message is chat message */
433 if (chat->group_message != NULL)
434 (*chat->group_message)(chat, peernum, contents, contents_len, chat->group_message_userdata);
435
436 break;
437
438 default:
439 handled = 0;
440 break;
441
442 }
443
444 if (handled == 1) {
445 sendto_allpeers(chat, data, len, 50);
446 return 0;
447 }
448
449 return 1;
450}
451
452static uint8_t send_data(Group_Chat *chat, uint8_t *data, uint32_t len, uint8_t message_id)
453{
454 if (len + GROUP_DATA_MIN_SIZE > MAX_DATA_SIZE) /*NOTE: not the real maximum len.*/
455 return 1;
456
457 uint8_t packet[MAX_DATA_SIZE];
458 ++chat->message_number;
459
460 if (chat->message_number == 0)
461 chat->message_number = 1;
462
463 uint32_t message_num = htonl(chat->message_number);
464//TODO
465 memcpy(packet, chat->self_public_key, crypto_box_PUBLICKEYBYTES);
466 memcpy(packet + crypto_box_PUBLICKEYBYTES, &message_num, sizeof(message_num));
467 memcpy(packet + GROUP_DATA_MIN_SIZE, data, len);
468 packet[crypto_box_PUBLICKEYBYTES + sizeof(message_num)] = message_id;
469 return sendto_allpeers(chat, packet, len + GROUP_DATA_MIN_SIZE, 50);
470}
471/*
472 * Handle get nodes group packet.
473 *
474 * return 0 if handled correctly.
475 * return 1 if error.
476 */
477
478int handle_groupchatpacket(Group_Chat *chat, IP_Port source, uint8_t *packet, uint32_t length)
479{
480 if (length > MAX_DATA_SIZE)
481 return 1;
482
483 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
484 uint8_t data[MAX_DATA_SIZE];
485 uint8_t number;
486 int len = handle_request(chat->self_public_key, chat->self_secret_key, public_key, data, &number, packet, length);
487
488 if (len <= 0)
489 return 1;
490
491 if (memcmp(chat->self_public_key, public_key, crypto_box_PUBLICKEYBYTES) == 0)
492 return 1;
493
494 int peernum = peer_in_chat(chat, public_key);
495
496 if (peernum == -1)
497 return 1;
498
499 switch (number) {
500 case 48:
501 return handle_getnodes(chat, source, peernum, data, len);
502
503 case 49:
504 return handle_sendnodes(chat, source, peernum, data, len);
505
506 case 50:
507 return handle_data(chat, data, len);
508
509 default:
510 return 1;
511 }
512
513 return 1;
514}
515
516uint32_t group_sendmessage(Group_Chat *chat, uint8_t *message, uint32_t length)
517{
518 return send_data(chat, message, length, 64); //TODO: better return values?
519}
520
521uint32_t group_newpeer(Group_Chat *chat, uint8_t *client_id)
522{
523 addpeer(chat, client_id);
524 return send_data(chat, client_id, crypto_box_PUBLICKEYBYTES, 16); //TODO: better return values?
525}
526
527void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat, int, uint8_t *, uint16_t, void *),
528 void *userdata)
529{
530 chat->group_message = function;
531 chat->group_message_userdata = userdata;
532}
533
534Group_Chat *new_groupchat(Networking_Core *net)
535{
536 if (net == 0)
537 return 0;
538
539 Group_Chat *chat = calloc(1, sizeof(Group_Chat));
540 chat->net = net;
541 crypto_box_keypair(chat->self_public_key, chat->self_secret_key);
542 return chat;
543}
544
545#define NODE_PING_INTERVAL 10
546
547static void ping_close(Group_Chat *chat)
548{
549 uint32_t i;
550 uint64_t temp_time = unix_time();
551
552 for (i = 0; i < GROUP_CLOSE_CONNECTIONS; ++i) {
553 if (chat->close[i].last_recv < temp_time + BAD_NODE_TIMEOUT) {
554 int peernum = peer_in_chat(chat, chat->close[i].client_id);
555
556 if (peernum == -1)
557 continue;
558
559 if (chat->group[peernum].last_pinged + NODE_PING_INTERVAL < temp_time)
560 send_getnodes(chat, chat->close[i].ip_port, peernum);
561 }
562 }
563}
564
565void do_groupchat(Group_Chat *chat)
566{
567 ping_close(chat);
568}
569
570void kill_groupchat(Group_Chat *chat)
571{
572 free(chat->group);
573 free(chat);
574}
575
576void chat_bootstrap(Group_Chat *chat, IP_Port ip_port, uint8_t *client_id)
577{
578 send_getnodes(chat, ip_port, addpeer(chat, client_id));
579}