summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--other/DHT_bootstrap.c8
-rw-r--r--testing/nTox.c45
-rw-r--r--testing/nTox.h1
-rw-r--r--toxcore/DHT.c4
-rw-r--r--toxcore/LAN_discovery.h2
-rw-r--r--toxcore/Messenger.c123
-rw-r--r--toxcore/Messenger.h56
-rw-r--r--toxcore/group_chats.h1
-rw-r--r--toxcore/net_crypto.c24
-rw-r--r--toxcore/net_crypto.h3
-rw-r--r--toxcore/network.c5
-rw-r--r--toxcore/network.h2
-rw-r--r--toxcore/ping.c4
-rw-r--r--toxcore/tox.c75
-rw-r--r--toxcore/tox.h53
15 files changed, 383 insertions, 23 deletions
diff --git a/other/DHT_bootstrap.c b/other/DHT_bootstrap.c
index 654b759f..aed17020 100644
--- a/other/DHT_bootstrap.c
+++ b/other/DHT_bootstrap.c
@@ -31,6 +31,7 @@
31#endif 31#endif
32 32
33#include "../toxcore/DHT.h" 33#include "../toxcore/DHT.h"
34#include "../toxcore/LAN_discovery.h"
34#include "../toxcore/friend_requests.h" 35#include "../toxcore/friend_requests.h"
35#include "../testing/misc_tools.c" 36#include "../testing/misc_tools.c"
36 37
@@ -123,6 +124,9 @@ int main(int argc, char *argv[])
123 124
124 int is_waiting_for_dht_connection = 1; 125 int is_waiting_for_dht_connection = 1;
125 126
127 uint64_t last_LANdiscovery = 0;
128 LANdiscovery_init(dht);
129
126 while (1) { 130 while (1) {
127 if (is_waiting_for_dht_connection && DHT_isconnected(dht)) { 131 if (is_waiting_for_dht_connection && DHT_isconnected(dht)) {
128 printf("Connected to other bootstrap server successfully.\n"); 132 printf("Connected to other bootstrap server successfully.\n");
@@ -130,6 +134,10 @@ int main(int argc, char *argv[])
130 } 134 }
131 135
132 do_DHT(dht); 136 do_DHT(dht);
137 if (last_LANdiscovery + (is_waiting_for_dht_connection ? 5 : LAN_DISCOVERY_INTERVAL) < unix_time()) {
138 send_LANdiscovery(htons(PORT), dht->c);
139 last_LANdiscovery = unix_time();
140 }
133 141
134 networking_poll(dht->c->lossless_udp->net); 142 networking_poll(dht->c->lossless_udp->net);
135 143
diff --git a/testing/nTox.c b/testing/nTox.c
index 438468bd..fa3b5b8c 100644
--- a/testing/nTox.c
+++ b/testing/nTox.c
@@ -348,13 +348,34 @@ void line_eval(Tox *m, char *line)
348 do_refresh(); 348 do_refresh();
349 } else if (inpt_command == 'h') { //help 349 } else if (inpt_command == 'h') { //help
350 new_lines(help); 350 new_lines(help);
351 } else if (inpt_command == 'i') { //info 351 } else if (inpt_command == 'x') { //info
352 char idstring[200]; 352 char idstring[200];
353 get_id(m, idstring); 353 get_id(m, idstring);
354 new_lines(idstring); 354 new_lines(idstring);
355 } 355 } else if (inpt_command == 'g') { //create new group chat
356 char msg[256];
357 sprintf(msg, "[g] Created new group chat with number: %u", tox_add_groupchat(m));
358 new_lines(msg);
359 } else if (inpt_command == 'i') { //invite friendnum to groupnum
360 char *posi[1];
361 int friendnumber = strtoul(line + prompt_offset, posi, 0);
362 int groupnumber = strtoul(*posi + 1, NULL, 0);
363 char msg[256];
364 sprintf(msg, "[g] Invited friend number %u to group number %u, returned: %u (0 means success)", friendnumber,
365 groupnumber, tox_invite_friend(m, friendnumber, groupnumber));
366 new_lines(msg);
367 } else if (inpt_command == 'z') { //send message to groupnum
368 char *posi[1];
369 int groupnumber = strtoul(line + prompt_offset, posi, 0);
370
371 if (**posi != 0) {
372 char msg[256 + 1024];
373 sprintf(msg, "[g] sent message: %s to group num: %u returned: %u (0 means success)", *posi + 1, groupnumber,
374 tox_group_message_send(m, groupnumber, (uint8_t *)*posi + 1, strlen(*posi + 1) + 1));
375 new_lines(msg);
376 }
356 377
357 else if (inpt_command == 'q') { //exit 378 } else if (inpt_command == 'q') { //exit
358 endwin(); 379 endwin();
359 exit(EXIT_SUCCESS); 380 exit(EXIT_SUCCESS);
360 } else { 381 } else {
@@ -535,6 +556,22 @@ void print_help(void)
535 puts("\t-f\t-\tSpecify a keyfile to read (or write to) from."); 556 puts("\t-f\t-\tSpecify a keyfile to read (or write to) from.");
536} 557}
537 558
559void print_invite(Tox *m, int friendnumber, uint8_t *group_public_key, void *userdata)
560{
561 char msg[256];
562 sprintf(msg, "[i] recieved group chat invite from: %u, auto accepting and joining. group number: %u", friendnumber,
563 tox_join_groupchat(m, friendnumber, group_public_key));
564 new_lines(msg);
565}
566
567void print_groupmessage(Tox *m, int groupnumber, uint8_t *message, uint16_t length, void *userdata)
568{
569 char msg[256 + length];
570 sprintf(msg, "[g] %u: %s", groupnumber, message);
571 new_lines(msg);
572}
573
574
538int main(int argc, char *argv[]) 575int main(int argc, char *argv[])
539{ 576{
540 int on = 0; 577 int on = 0;
@@ -579,6 +616,8 @@ int main(int argc, char *argv[])
579 tox_callback_friendmessage(m, print_message, NULL); 616 tox_callback_friendmessage(m, print_message, NULL);
580 tox_callback_namechange(m, print_nickchange, NULL); 617 tox_callback_namechange(m, print_nickchange, NULL);
581 tox_callback_statusmessage(m, print_statuschange, NULL); 618 tox_callback_statusmessage(m, print_statuschange, NULL);
619 tox_callback_group_invite(m, print_invite, NULL);
620 tox_callback_group_message(m, print_groupmessage, NULL);
582 621
583 initscr(); 622 initscr();
584 noecho(); 623 noecho();
diff --git a/testing/nTox.h b/testing/nTox.h
index a72ce0c2..2cd5db09 100644
--- a/testing/nTox.h
+++ b/testing/nTox.h
@@ -31,7 +31,6 @@
31#include <ctype.h> 31#include <ctype.h>
32 32
33#include "../toxcore/tox.h" 33#include "../toxcore/tox.h"
34
35#define STRING_LENGTH 256 34#define STRING_LENGTH 256
36#define HISTORY 50 35#define HISTORY 50
37#define PUB_KEY_BYTES 32 36#define PUB_KEY_BYTES 32
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index 774dd6f2..0b866940 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -498,7 +498,7 @@ static int getnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cli
498 uint8_t plain[sizeof(ping_id) + CLIENT_ID_SIZE]; 498 uint8_t plain[sizeof(ping_id) + CLIENT_ID_SIZE];
499 uint8_t encrypt[sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING]; 499 uint8_t encrypt[sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING];
500 uint8_t nonce[crypto_box_NONCEBYTES]; 500 uint8_t nonce[crypto_box_NONCEBYTES];
501 random_nonce(nonce); 501 new_nonce(nonce);
502 502
503 memcpy(plain, &ping_id, sizeof(ping_id)); 503 memcpy(plain, &ping_id, sizeof(ping_id));
504 memcpy(plain + sizeof(ping_id), client_id, CLIENT_ID_SIZE); 504 memcpy(plain + sizeof(ping_id), client_id, CLIENT_ID_SIZE);
@@ -540,7 +540,7 @@ static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cl
540 uint8_t plain[sizeof(ping_id) + sizeof(Node_format) * MAX_SENT_NODES]; 540 uint8_t plain[sizeof(ping_id) + sizeof(Node_format) * MAX_SENT_NODES];
541 uint8_t encrypt[sizeof(ping_id) + sizeof(Node_format) * MAX_SENT_NODES + ENCRYPTION_PADDING]; 541 uint8_t encrypt[sizeof(ping_id) + sizeof(Node_format) * MAX_SENT_NODES + ENCRYPTION_PADDING];
542 uint8_t nonce[crypto_box_NONCEBYTES]; 542 uint8_t nonce[crypto_box_NONCEBYTES];
543 random_nonce(nonce); 543 new_nonce(nonce);
544 544
545 memcpy(plain, &ping_id, sizeof(ping_id)); 545 memcpy(plain, &ping_id, sizeof(ping_id));
546 memcpy(plain + sizeof(ping_id), nodes_list, num_nodes * sizeof(Node_format)); 546 memcpy(plain + sizeof(ping_id), nodes_list, num_nodes * sizeof(Node_format));
diff --git a/toxcore/LAN_discovery.h b/toxcore/LAN_discovery.h
index 78990936..3e9d9de5 100644
--- a/toxcore/LAN_discovery.h
+++ b/toxcore/LAN_discovery.h
@@ -35,6 +35,8 @@
35#include <linux/netdevice.h> 35#include <linux/netdevice.h>
36#endif 36#endif
37 37
38/* Interval in seconds between LAN discovery packet sending. */
39#define LAN_DISCOVERY_INTERVAL 60
38 40
39/* Send a LAN discovery pcaket to the broadcast address with port port. */ 41/* Send a LAN discovery pcaket to the broadcast address with port port. */
40int send_LANdiscovery(uint16_t port, Net_Crypto *c); 42int send_LANdiscovery(uint16_t port, Net_Crypto *c);
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index e17c9344..682cb1be 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -673,6 +673,40 @@ static int group_num(Messenger *m, uint8_t *group_public_key)
673 return -1; 673 return -1;
674} 674}
675 675
676/* Set the callback for group invites.
677 *
678 * Function(Messenger *m, int friendnumber, uint8_t *group_public_key, void *userdata)
679 */
680void m_callback_group_invite(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, void *), void *userdata)
681{
682 m->group_invite = function;
683 m->group_invite_userdata = userdata;
684}
685
686/* Set the callback for group messages.
687 *
688 * Function(Messenger *m, int groupnumber, uint8_t * message, uint16_t length, void *userdata)
689 */
690void m_callback_group_message(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *),
691 void *userdata)
692{
693 m->group_message = function;
694 m->group_message_userdata = userdata;
695}
696static void group_message_function(Group_Chat *chat, int peer_number, uint8_t *message, uint16_t length, void *userdata)
697{
698 Messenger *m = userdata;
699 uint32_t i;
700
701 for (i = 0; i < m->numchats; ++i) { //TODO: remove this
702 if (m->chats[i] == chat)
703 break;
704 }
705
706 if (m->group_message)
707 (*m->group_message)(m, i, message, length, m->group_invite_userdata);
708}
709
676/* Creates a new groupchat and puts it in the chats array. 710/* Creates a new groupchat and puts it in the chats array.
677 * 711 *
678 * return group number on success. 712 * return group number on success.
@@ -689,6 +723,7 @@ int add_groupchat(Messenger *m)
689 if (newchat == NULL) 723 if (newchat == NULL)
690 return -1; 724 return -1;
691 725
726 callback_groupmessage(newchat, &group_message_function, m);
692 m->chats[i] = newchat; 727 m->chats[i] = newchat;
693 return i; 728 return i;
694 } 729 }
@@ -705,6 +740,8 @@ int add_groupchat(Messenger *m)
705 if (temp[m->numchats] == NULL) 740 if (temp[m->numchats] == NULL)
706 return -1; 741 return -1;
707 742
743 m->chats = temp;
744 callback_groupmessage(temp[m->numchats], &group_message_function, m);
708 ++m->numchats; 745 ++m->numchats;
709 return (m->numchats - 1); 746 return (m->numchats - 1);
710} 747}
@@ -714,7 +751,7 @@ int add_groupchat(Messenger *m)
714 * return 0 on success. 751 * return 0 on success.
715 * return -1 if failure. 752 * return -1 if failure.
716 */ 753 */
717static int del_groupchat(Messenger *m, int groupnumber) 754int del_groupchat(Messenger *m, int groupnumber)
718{ 755{
719 if ((unsigned int)groupnumber >= m->numchats) 756 if ((unsigned int)groupnumber >= m->numchats)
720 return -1; 757 return -1;
@@ -748,9 +785,42 @@ static int del_groupchat(Messenger *m, int groupnumber)
748 return 0; 785 return 0;
749} 786}
750 787
788/* return 1 if that friend was invited to the group
789 * return 0 if the friend was not or error.
790 */
791static uint8_t group_invited(Messenger *m, int friendnumber, int groupnumber)
792{
793 //TODO: this function;
794 return 1;
795}
796
797/* invite friendnumber to groupnumber
798 * return 0 on success
799 * return -1 on failure
800 */
801int invite_friend(Messenger *m, int friendnumber, int groupnumber)
802{
803 if (friend_not_valid(m, friendnumber) || (unsigned int)groupnumber >= m->numchats)
804 return -1;
805
806 if (m->chats == NULL)
807 return -1;
808
809 if (m->friendlist[friendnumber].status == NOFRIEND || m->chats[groupnumber] == NULL)
810 return -1;
811
812 //TODO: store invited friends.
813 if (write_cryptpacket_id(m, friendnumber, PACKET_ID_INVITE_GROUPCHAT, m->chats[groupnumber]->self_public_key,
814 crypto_box_PUBLICKEYBYTES) == 0)
815 return -1;
816
817 return 0;
818}
819
820
751/* Join a group (you need to have been invited first.) 821/* Join a group (you need to have been invited first.)
752 * 822 *
753 * returns 0 on success 823 * returns group number on success
754 * returns -1 on failure. 824 * returns -1 on failure.
755 */ 825 */
756int join_groupchat(Messenger *m, int friendnumber, uint8_t *friend_group_public_key) 826int join_groupchat(Messenger *m, int friendnumber, uint8_t *friend_group_public_key)
@@ -770,12 +840,34 @@ int join_groupchat(Messenger *m, int friendnumber, uint8_t *friend_group_public_
770 if (write_cryptpacket_id(m, friendnumber, PACKET_ID_JOIN_GROUPCHAT, data, sizeof(data))) { 840 if (write_cryptpacket_id(m, friendnumber, PACKET_ID_JOIN_GROUPCHAT, data, sizeof(data))) {
771 chat_bootstrap_nonlazy(m->chats[groupnum], get_friend_ipport(m, friendnumber), 841 chat_bootstrap_nonlazy(m->chats[groupnum], get_friend_ipport(m, friendnumber),
772 friend_group_public_key); //TODO: check if ip returned is zero? 842 friend_group_public_key); //TODO: check if ip returned is zero?
773 return 0; 843 return groupnum;
774 } 844 }
775 845
776 return -1; 846 return -1;
777} 847}
778 848
849/* send a group message
850 * return 0 on success
851 * return -1 on failure
852 */
853
854int group_message_send(Messenger *m, int groupnumber, uint8_t *message, uint32_t length)
855{
856 if ((unsigned int)groupnumber >= m->numchats)
857 return -1;
858
859 if (m->chats == NULL)
860 return -1;
861
862 if (m->chats[groupnumber] == NULL)
863 return -1;
864
865 if (group_sendmessage(m->chats[groupnumber], message, length) > 0)
866 return 0;
867
868 return -1;
869}
870
779static int handle_group(void *object, IP_Port source, uint8_t *packet, uint32_t length) 871static int handle_group(void *object, IP_Port source, uint8_t *packet, uint32_t length)
780{ 872{
781 Messenger *m = object; 873 Messenger *m = object;
@@ -809,8 +901,6 @@ static void do_allgroupchats(Messenger *m)
809 901
810/*********************************/ 902/*********************************/
811 903
812/* Interval in seconds between LAN discovery packet sending. */
813#define LAN_DISCOVERY_INTERVAL 60
814#define PORT 33445 904#define PORT 33445
815 905
816/* Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds. */ 906/* Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds. */
@@ -1064,6 +1154,29 @@ void doFriends(Messenger *m)
1064 1154
1065 break; 1155 break;
1066 } 1156 }
1157
1158 case PACKET_ID_INVITE_GROUPCHAT: {
1159 if (data_length != crypto_box_PUBLICKEYBYTES)
1160 break;
1161
1162 if (m->group_invite)
1163 (*m->group_invite)(m, i, data, m->group_invite_userdata);
1164 }
1165
1166 case PACKET_ID_JOIN_GROUPCHAT: {
1167 if (data_length != crypto_box_PUBLICKEYBYTES * 2)
1168 break;
1169
1170 int groupnum = group_num(m, data);
1171
1172 if (groupnum == -1)
1173 break;
1174
1175 if (!group_invited(m, i, groupnum))
1176 break;
1177
1178 group_newpeer(m->chats[groupnum], data + crypto_box_PUBLICKEYBYTES);
1179 }
1067 } 1180 }
1068 } else { 1181 } else {
1069 if (is_cryptoconnected(m->net_crypto, 1182 if (is_cryptoconnected(m->net_crypto,
diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h
index 7649a779..0656c736 100644
--- a/toxcore/Messenger.h
+++ b/toxcore/Messenger.h
@@ -157,7 +157,10 @@ typedef struct Messenger {
157 void *friend_statuschange_userdata; 157 void *friend_statuschange_userdata;
158 void (*friend_connectionstatuschange)(struct Messenger *m, int, uint8_t, void *); 158 void (*friend_connectionstatuschange)(struct Messenger *m, int, uint8_t, void *);
159 void *friend_connectionstatuschange_userdata; 159 void *friend_connectionstatuschange_userdata;
160 160 void (*group_invite)(struct Messenger *m, int, uint8_t *, void *);
161 void *group_invite_userdata;
162 void (*group_message)(struct Messenger *m, int, uint8_t *, uint16_t, void *);
163 void *group_message_userdata;
161 164
162} Messenger; 165} Messenger;
163 166
@@ -369,6 +372,57 @@ void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, u
369 */ 372 */
370void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t, void *), void *userdata); 373void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t, void *), void *userdata);
371 374
375/**********GROUP CHATS************/
376
377/* Set the callback for group invites.
378 *
379 * Function(Messenger *m, int friendnumber, uint8_t *group_public_key, void *userdata)
380 */
381void m_callback_group_invite(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, void *), void *userdata);
382
383/* Set the callback for group messages.
384 *
385 * Function(Messenger *m, int groupnumber, uint8_t * message, uint16_t length, void *userdata)
386 */
387void m_callback_group_message(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *),
388 void *userdata);
389
390/* Creates a new groupchat and puts it in the chats array.
391 *
392 * return group number on success.
393 * return -1 on failure.
394 */
395int add_groupchat(Messenger *m);
396
397/* Delete a groupchat from the chats array.
398 *
399 * return 0 on success.
400 * return -1 if failure.
401 */
402int del_groupchat(Messenger *m, int groupnumber);
403
404/* invite friendnumber to groupnumber
405 * return 0 on success
406 * return -1 on failure
407 */
408int invite_friend(Messenger *m, int friendnumber, int groupnumber);
409
410/* Join a group (you need to have been invited first.)
411 *
412 * returns group number on success
413 * returns -1 on failure.
414 */
415int join_groupchat(Messenger *m, int friendnumber, uint8_t *friend_group_public_key);
416
417/* send a group message
418 * return 0 on success
419 * return -1 on failure
420 */
421
422int group_message_send(Messenger *m, int groupnumber, uint8_t *message, uint32_t length);
423
424/*********************************/
425
372/* Run this at startup. 426/* Run this at startup.
373 * return allocated instance of Messenger on success. 427 * return allocated instance of Messenger on success.
374 * return 0 if there are problems. 428 * return 0 if there are problems.
diff --git a/toxcore/group_chats.h b/toxcore/group_chats.h
index 535b46db..78a5488c 100644
--- a/toxcore/group_chats.h
+++ b/toxcore/group_chats.h
@@ -77,6 +77,7 @@ void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat,
77/* 77/*
78 * Send a message to the group. 78 * Send a message to the group.
79 * 79 *
80 * returns the number of peers it has sent it to.
80 */ 81 */
81uint32_t group_sendmessage(Group_Chat *chat, uint8_t *message, uint32_t length); 82uint32_t group_sendmessage(Group_Chat *chat, uint8_t *message, uint32_t length);
82 83
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index a182bb53..8163701e 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -145,14 +145,26 @@ static void increment_nonce(uint8_t *nonce)
145/* Fill the given nonce with random bytes. */ 145/* Fill the given nonce with random bytes. */
146void random_nonce(uint8_t *nonce) 146void random_nonce(uint8_t *nonce)
147{ 147{
148 uint32_t i, temp; 148 randombytes(nonce, crypto_box_NONCEBYTES);
149}
150
151
152static uint8_t base_nonce[crypto_box_NONCEBYTES];
153static uint8_t nonce_set = 0;
149 154
150 for (i = 0; i < crypto_box_NONCEBYTES / 4; ++i) { 155/*Gives a nonce guaranteed to be different from previous ones.*/
151 temp = random_int(); 156void new_nonce(uint8_t *nonce)
152 memcpy(nonce + 4 * i, &temp, 4); 157{
158 if (nonce_set == 0) {
159 random_nonce(base_nonce);
160 nonce_set = 1;
153 } 161 }
162
163 increment_nonce(base_nonce);
164 memcpy(nonce, base_nonce, crypto_box_NONCEBYTES);
154} 165}
155 166
167
156/* return 0 if there is no received data in the buffer. 168/* return 0 if there is no received data in the buffer.
157 * return -1 if the packet was discarded. 169 * return -1 if the packet was discarded.
158 * return length of received data if successful. 170 * return length of received data if successful.
@@ -237,7 +249,7 @@ int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *
237 uint8_t temp[MAX_DATA_SIZE]; 249 uint8_t temp[MAX_DATA_SIZE];
238 memcpy(temp + 1, data, length); 250 memcpy(temp + 1, data, length);
239 temp[0] = request_id; 251 temp[0] = request_id;
240 random_nonce(nonce); 252 new_nonce(nonce);
241 int len = encrypt_data(recv_public_key, send_secret_key, nonce, temp, length + 1, 253 int len = encrypt_data(recv_public_key, send_secret_key, nonce, temp, length + 1,
242 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + packet); 254 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + packet);
243 255
@@ -336,7 +348,7 @@ static int send_cryptohandshake(Net_Crypto *c, int connection_id, uint8_t *publi
336 uint8_t temp[crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES]; 348 uint8_t temp[crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES];
337 uint8_t nonce[crypto_box_NONCEBYTES]; 349 uint8_t nonce[crypto_box_NONCEBYTES];
338 350
339 random_nonce(nonce); 351 new_nonce(nonce);
340 memcpy(temp, secret_nonce, crypto_box_NONCEBYTES); 352 memcpy(temp, secret_nonce, crypto_box_NONCEBYTES);
341 memcpy(temp + crypto_box_NONCEBYTES, session_key, crypto_box_PUBLICKEYBYTES); 353 memcpy(temp + crypto_box_NONCEBYTES, session_key, crypto_box_PUBLICKEYBYTES);
342 354
diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h
index e5dfcae0..55c1e3e3 100644
--- a/toxcore/net_crypto.h
+++ b/toxcore/net_crypto.h
@@ -115,6 +115,9 @@ int decrypt_data_fast(uint8_t *enc_key, uint8_t *nonce,
115/* Fill the given nonce with random bytes. */ 115/* Fill the given nonce with random bytes. */
116void random_nonce(uint8_t *nonce); 116void random_nonce(uint8_t *nonce);
117 117
118/*Gives a nonce guaranteed to be different from previous ones.*/
119void new_nonce(uint8_t *nonce);
120
118/* return 0 if there is no received data in the buffer. 121/* return 0 if there is no received data in the buffer.
119 * return -1 if the packet was discarded. 122 * return -1 if the packet was discarded.
120 * return length of received data if successful. 123 * return length of received data if successful.
diff --git a/toxcore/network.c b/toxcore/network.c
index ed3dff8a..c6c4965e 100644
--- a/toxcore/network.c
+++ b/toxcore/network.c
@@ -49,7 +49,6 @@ uint64_t current_time(void)
49} 49}
50 50
51/* return a random number. 51/* return a random number.
52 * NOTE: This function should probably not be used where cryptographic randomness is absolutely necessary.
53 */ 52 */
54uint32_t random_int(void) 53uint32_t random_int(void)
55{ 54{
@@ -57,7 +56,9 @@ uint32_t random_int(void)
57 /* NOTE: this function comes from libsodium. */ 56 /* NOTE: this function comes from libsodium. */
58 return randombytes_random(); 57 return randombytes_random();
59#else 58#else
60 return random(); 59 uint32_t randnum;
60 randombytes((uint8_t *)&randnum , sizeof(randnum));
61 return randnum;
61#endif 62#endif
62} 63}
63 64
diff --git a/toxcore/network.h b/toxcore/network.h
index 98307e5b..e1f9b212 100644
--- a/toxcore/network.h
+++ b/toxcore/network.h
@@ -57,6 +57,7 @@
57#include <sodium.h> 57#include <sodium.h>
58#else 58#else
59#include <crypto_box.h> 59#include <crypto_box.h>
60#include <randombytes.h>
60#define crypto_box_MACBYTES (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES) 61#define crypto_box_MACBYTES (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES)
61#endif 62#endif
62 63
@@ -130,7 +131,6 @@ typedef struct {
130uint64_t current_time(void); 131uint64_t current_time(void);
131 132
132/* return a random number. 133/* return a random number.
133 * NOTE: this function should probably not be used where cryptographic randomness is absolutely necessary.
134 */ 134 */
135uint32_t random_int(void); 135uint32_t random_int(void);
136 136
diff --git a/toxcore/ping.c b/toxcore/ping.c
index 3b39d911..3a189f23 100644
--- a/toxcore/ping.c
+++ b/toxcore/ping.c
@@ -135,7 +135,7 @@ int send_ping_request(void *ping, Net_Crypto *c, IP_Port ipp, uint8_t *client_id
135 135
136 pk[0] = NET_PACKET_PING_REQUEST; 136 pk[0] = NET_PACKET_PING_REQUEST;
137 id_cpy(pk + 1, c->self_public_key); // Our pubkey 137 id_cpy(pk + 1, c->self_public_key); // Our pubkey
138 random_nonce(pk + 1 + CLIENT_ID_SIZE); // Generate random nonce 138 new_nonce(pk + 1 + CLIENT_ID_SIZE); // Generate new nonce
139 139
140 // Encrypt ping_id using recipient privkey 140 // Encrypt ping_id using recipient privkey
141 rc = encrypt_data(client_id, 141 rc = encrypt_data(client_id,
@@ -160,7 +160,7 @@ int send_ping_response(Net_Crypto *c, IP_Port ipp, uint8_t *client_id, uint64_t
160 160
161 pk[0] = NET_PACKET_PING_RESPONSE; 161 pk[0] = NET_PACKET_PING_RESPONSE;
162 id_cpy(pk + 1, c->self_public_key); // Our pubkey 162 id_cpy(pk + 1, c->self_public_key); // Our pubkey
163 random_nonce(pk + 1 + CLIENT_ID_SIZE); // Generate random nonce 163 new_nonce(pk + 1 + CLIENT_ID_SIZE); // Generate new nonce
164 164
165 // Encrypt ping_id using recipient privkey 165 // Encrypt ping_id using recipient privkey
166 rc = encrypt_data(client_id, 166 rc = encrypt_data(client_id,
diff --git a/toxcore/tox.c b/toxcore/tox.c
index 54bbd9f0..417f1af3 100644
--- a/toxcore/tox.c
+++ b/toxcore/tox.c
@@ -366,6 +366,81 @@ void tox_callback_connectionstatus(void *tox, void (*function)(Messenger *tox, i
366 m_callback_connectionstatus(m, function, userdata); 366 m_callback_connectionstatus(m, function, userdata);
367} 367}
368 368
369/**********GROUP CHAT FUNCTIONS: WARNING WILL BREAK A LOT************/
370
371/* Set the callback for group invites.
372 *
373 * Function(Tox *tox, int friendnumber, uint8_t *group_public_key, void *userdata)
374 */
375void tox_callback_group_invite(void *tox, void (*function)(Messenger *tox, int, uint8_t *, void *), void *userdata)
376{
377 Messenger *m = tox;
378 m_callback_group_invite(m, function, userdata);
379}
380/* Set the callback for group messages.
381 *
382 * Function(Tox *tox, int groupnumber, uint8_t * message, uint16_t length, void *userdata)
383 */
384void tox_callback_group_message(void *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *),
385 void *userdata)
386{
387 Messenger *m = tox;
388 m_callback_group_message(m, function, userdata);
389}
390/* Creates a new groupchat and puts it in the chats array.
391 *
392 * return group number on success.
393 * return -1 on failure.
394 */
395int tox_add_groupchat(void *tox)
396{
397 Messenger *m = tox;
398 return add_groupchat(m);
399}
400/* Delete a groupchat from the chats array.
401 *
402 * return 0 on success.
403 * return -1 if failure.
404 */
405int tox_del_groupchat(void *tox, int groupnumber)
406{
407 Messenger *m = tox;
408 return del_groupchat(m, groupnumber);
409}
410/* invite friendnumber to groupnumber
411 * return 0 on success
412 * return -1 on failure
413 */
414int tox_invite_friend(void *tox, int friendnumber, int groupnumber)
415{
416 Messenger *m = tox;
417 return invite_friend(m, friendnumber, groupnumber);
418}
419/* Join a group (you need to have been invited first.)
420 *
421 * returns group number on success
422 * returns -1 on failure.
423 */
424int tox_join_groupchat(void *tox, int friendnumber, uint8_t *friend_group_public_key)
425{
426 Messenger *m = tox;
427 return join_groupchat(m, friendnumber, friend_group_public_key);
428}
429
430/* send a group message
431 * return 0 on success
432 * return -1 on failure
433 */
434int tox_group_message_send(void *tox, int groupnumber, uint8_t *message, uint32_t length)
435{
436 Messenger *m = tox;
437 return group_message_send(m, groupnumber, message, length);
438}
439
440
441
442/******************END OF GROUP CHAT FUNCTIONS************************/
443
369/* Use this function to bootstrap the client. 444/* Use this function to bootstrap the client.
370 * Sends a get nodes request to the given node with ip port and public_key. 445 * Sends a get nodes request to the given node with ip port and public_key.
371 */ 446 */
diff --git a/toxcore/tox.h b/toxcore/tox.h
index 811e798b..6d5db49f 100644
--- a/toxcore/tox.h
+++ b/toxcore/tox.h
@@ -289,6 +289,59 @@ void tox_callback_read_receipt(Tox *tox, void (*function)(Tox *tox, int, uint32_
289 */ 289 */
290void tox_callback_connectionstatus(Tox *tox, void (*function)(Tox *tox, int, uint8_t, void *), void *userdata); 290void tox_callback_connectionstatus(Tox *tox, void (*function)(Tox *tox, int, uint8_t, void *), void *userdata);
291 291
292/**********GROUP CHAT FUNCTIONS: WARNING WILL BREAK A LOT************/
293
294/* Set the callback for group invites.
295 *
296 * Function(Tox *tox, int friendnumber, uint8_t *group_public_key, void *userdata)
297 */
298void tox_callback_group_invite(Tox *tox, void (*function)(Tox *tox, int, uint8_t *, void *), void *userdata);
299
300/* Set the callback for group messages.
301 *
302 * Function(Tox *tox, int groupnumber, uint8_t * message, uint16_t length, void *userdata)
303 */
304void tox_callback_group_message(Tox *tox, void (*function)(Tox *tox, int, uint8_t *, uint16_t, void *),
305 void *userdata);
306
307/* Creates a new groupchat and puts it in the chats array.
308 *
309 * return group number on success.
310 * return -1 on failure.
311 */
312int tox_add_groupchat(Tox *tox);
313
314/* Delete a groupchat from the chats array.
315 *
316 * return 0 on success.
317 * return -1 if failure.
318 */
319int tox_del_groupchat(Tox *tox, int groupnumber);
320
321/* invite friendnumber to groupnumber
322 * return 0 on success
323 * return -1 on failure
324 */
325int tox_invite_friend(Tox *tox, int friendnumber, int groupnumber);
326
327/* Join a group (you need to have been invited first.)
328 *
329 * returns group number on success
330 * returns -1 on failure.
331 */
332int tox_join_groupchat(Tox *tox, int friendnumber, uint8_t *friend_group_public_key);
333
334
335/* send a group message
336 * return 0 on success
337 * return -1 on failure
338 */
339int tox_group_message_send(Tox *tox, int groupnumber, uint8_t *message, uint32_t length);
340
341
342
343/******************END OF GROUP CHAT FUNCTIONS************************/
344
292/* Use this function to bootstrap the client. 345/* Use this function to bootstrap the client.
293 * Sends a get nodes request to the given node with ip port and public_key. 346 * Sends a get nodes request to the given node with ip port and public_key.
294 */ 347 */