summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
authorCoren[m] <Break@Ocean>2013-09-09 21:31:11 +0200
committerCoren[m] <Break@Ocean>2013-09-09 21:31:11 +0200
commitb91b87f28548261a582c295afa397a6c569089d7 (patch)
tree0dd5cdcd2929812f8600239d11e7fba08afb9dd4 /toxcore
parent447fdc74ec0794d334d2fa3b5b88091aa582e14a (diff)
parent1f5bfab3278164293dd798ca24ead081c54708da (diff)
Merge branch 'master' into Integration
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/Makefile.inc2
-rw-r--r--toxcore/Messenger.c9
-rw-r--r--toxcore/Messenger.h4
-rw-r--r--toxcore/group_chats.c579
-rw-r--r--toxcore/group_chats.h123
-rw-r--r--toxcore/tox.c2
6 files changed, 714 insertions, 5 deletions
diff --git a/toxcore/Makefile.inc b/toxcore/Makefile.inc
index 5a9002ef..025f23c8 100644
--- a/toxcore/Makefile.inc
+++ b/toxcore/Makefile.inc
@@ -25,6 +25,8 @@ libtoxcore_la_SOURCES = ../toxcore/DHT.h \
25 ../toxcore/tox.c \ 25 ../toxcore/tox.c \
26 ../toxcore/util.h \ 26 ../toxcore/util.h \
27 ../toxcore/util.c \ 27 ../toxcore/util.c \
28 ../toxcore/group_chats.h \
29 ../toxcore/group_chats.c \
28 ../toxcore/misc_tools.h 30 ../toxcore/misc_tools.h
29 31
30libtoxcore_la_CFLAGS = -I$(top_srcdir) \ 32libtoxcore_la_CFLAGS = -I$(top_srcdir) \
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 48d40f43..61a76a76 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -346,17 +346,18 @@ static int m_sendname(Messenger *m, int friendnumber, uint8_t *name, uint16_t le
346 return write_cryptpacket_id(m, friendnumber, PACKET_ID_NICKNAME, name, length); 346 return write_cryptpacket_id(m, friendnumber, PACKET_ID_NICKNAME, name, length);
347} 347}
348 348
349/* Set the name of a friend. 349/* Set the name and name_length of a friend.
350 * 350 *
351 * return 0 if success. 351 * return 0 if success.
352 * return -1 if failure. 352 * return -1 if failure.
353 */ 353 */
354static int setfriendname(Messenger *m, int friendnumber, uint8_t *name) 354static int setfriendname(Messenger *m, int friendnumber, uint8_t *name, uint16_t len)
355{ 355{
356 if (friend_not_valid(m, friendnumber)) 356 if (friend_not_valid(m, friendnumber))
357 return -1; 357 return -1;
358 358
359 memcpy(m->friendlist[friendnumber].name, name, MAX_NAME_LENGTH); 359 m->friendlist[friendnumber].name_length = len;
360 memcpy(m->friendlist[friendnumber].name, name, len);
360 return 0; 361 return 0;
361} 362}
362 363
@@ -1033,7 +1034,7 @@ int Messenger_load(Messenger *m, uint8_t *data, uint32_t length)
1033 for (i = 0; i < num; ++i) { 1034 for (i = 0; i < num; ++i) {
1034 if (temp[i].status >= 3) { 1035 if (temp[i].status >= 3) {
1035 int fnum = m_addfriend_norequest(m, temp[i].client_id); 1036 int fnum = m_addfriend_norequest(m, temp[i].client_id);
1036 setfriendname(m, fnum, temp[i].name); 1037 setfriendname(m, fnum, temp[i].name, temp[i].name_length);
1037 /* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */ 1038 /* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */
1038 } else if (temp[i].status != 0) { 1039 } else if (temp[i].status != 0) {
1039 /* TODO: This is not a good way to do this. */ 1040 /* TODO: This is not a good way to do this. */
diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h
index 34131a20..dfb3f7c6 100644
--- a/toxcore/Messenger.h
+++ b/toxcore/Messenger.h
@@ -30,6 +30,7 @@
30#include "DHT.h" 30#include "DHT.h"
31#include "friend_requests.h" 31#include "friend_requests.h"
32#include "LAN_discovery.h" 32#include "LAN_discovery.h"
33#include "group_chats.h"
33 34
34#define MAX_NAME_LENGTH 128 35#define MAX_NAME_LENGTH 128
35#define MAX_STATUSMESSAGE_LENGTH 1007 36#define MAX_STATUSMESSAGE_LENGTH 1007
@@ -133,6 +134,9 @@ typedef struct Messenger {
133 Friend *friendlist; 134 Friend *friendlist;
134 uint32_t numfriends; 135 uint32_t numfriends;
135 136
137 Group_Chat *chats;
138 uint32_t numchats;
139
136 uint64_t last_LANdiscovery; 140 uint64_t last_LANdiscovery;
137 141
138 void (*friend_message)(struct Messenger *m, int, uint8_t *, uint16_t, void *); 142 void (*friend_message)(struct Messenger *m, int, uint8_t *, uint16_t, void *);
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}
diff --git a/toxcore/group_chats.h b/toxcore/group_chats.h
new file mode 100644
index 00000000..7af13d8d
--- /dev/null
+++ b/toxcore/group_chats.h
@@ -0,0 +1,123 @@
1/* group_chats.h
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#ifndef GROUP_CHATS_H
26#define GROUP_CHATS_H
27
28#include "net_crypto.h"
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34typedef struct {
35 uint8_t client_id[crypto_box_PUBLICKEYBYTES];
36 uint64_t pingid;
37 uint64_t last_pinged;
38
39 uint64_t last_recv;
40 uint64_t last_recv_msgping;
41 uint32_t last_message_number;
42} Group_Peer;
43
44typedef struct {
45 uint8_t client_id[crypto_box_PUBLICKEYBYTES];
46 IP_Port ip_port;
47 uint64_t last_recv;
48
49} Group_Close;
50
51#define GROUP_CLOSE_CONNECTIONS 6
52
53typedef struct Group_Chat {
54 Networking_Core *net;
55 uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
56 uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
57
58 Group_Peer *group;
59 Group_Close close[GROUP_CLOSE_CONNECTIONS];
60 uint32_t numpeers;
61
62 uint32_t message_number;
63 void (*group_message)(struct Group_Chat *m, int, uint8_t *, uint16_t, void *);
64 void *group_message_userdata;
65
66} Group_Chat;
67
68/*
69 * Set callback function for chat messages.
70 *
71 * format of function is: function(Group_Chat *chat, peer number, message, message length, userdata)
72 */
73
74void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat, int, uint8_t *, uint16_t, void *),
75 void *userdata);
76
77/*
78 * Send a message to the group.
79 *
80 */
81uint32_t group_sendmessage(Group_Chat *chat, uint8_t *message, uint32_t length);
82
83
84/*
85 * Tell everyone about a new peer (a person we are inviting for example.)
86 *
87 */
88uint32_t group_newpeer(Group_Chat *chat, uint8_t *client_id);
89
90
91/* Create a new group chat.
92 *
93 * Returns a new group chat instance if success.
94 *
95 * Returns a NULL pointer if fail.
96 */
97Group_Chat *new_groupchat(Networking_Core *net);
98
99
100/* Kill a group chat
101 *
102 * Frees the memory and everything.
103 */
104void kill_groupchat(Group_Chat *chat);
105
106/*
107 * This is the main loop.
108 */
109void do_groupchat(Group_Chat *chat);
110
111/* if we receive a group chat packet we call this function so it can be handled.
112 return 0 if packet is handled correctly.
113 return 1 if it didn't handle the packet or if the packet was shit. */
114int handle_groupchatpacket(Group_Chat *chat, IP_Port source, uint8_t *packet, uint32_t length);
115
116
117void chat_bootstrap(Group_Chat *chat, IP_Port ip_port, uint8_t *client_id);
118
119#ifdef __cplusplus
120}
121#endif
122
123#endif
diff --git a/toxcore/tox.c b/toxcore/tox.c
index d48de0d8..e37eac3e 100644
--- a/toxcore/tox.c
+++ b/toxcore/tox.c
@@ -184,7 +184,7 @@ uint16_t tox_getselfname(void *tox, uint8_t *name, uint16_t nlen)
184/* Get name of friendnumber and put it in name. 184/* Get name of friendnumber and put it in name.
185 * name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. 185 * name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes.
186 * 186 *
187 * return 0 if success. 187 * return length of name (with the NULL terminator) if success.
188 * return -1 if failure. 188 * return -1 if failure.
189 */ 189 */
190int tox_getname(void *tox, int friendnumber, uint8_t *name) 190int tox_getname(void *tox, int friendnumber, uint8_t *name)