summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
authorCoren[m] <Break@Ocean>2013-09-12 19:39:21 +0200
committerCoren[m] <Break@Ocean>2013-09-12 19:39:21 +0200
commitd95df291f0ab07e77101d550bf02abb5d267c40a (patch)
treeec83885b50100e312ca2070d4f71f2e7eb753c02 /toxcore
parentb5db32585a89749ce8e91348d176292cdc20abfd (diff)
parentb37b521cd5fa9454d34cb5d5ebdc2d378529cac2 (diff)
Merge remote-tracking branch 'upstream/master' into DHT_bootstrap
Conflicts: toxcore/Messenger.c
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/Messenger.c174
-rw-r--r--toxcore/Messenger.h6
-rw-r--r--toxcore/group_chats.c9
-rw-r--r--toxcore/group_chats.h2
-rw-r--r--toxcore/network.h2
5 files changed, 188 insertions, 5 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 2ff5634a..571c7291 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -637,6 +637,178 @@ int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint
637 return write_cryptpacket(m->net_crypto, m->friendlist[friendnumber].crypt_connection_id, packet, length + 1); 637 return write_cryptpacket(m->net_crypto, m->friendlist[friendnumber].crypt_connection_id, packet, length + 1);
638} 638}
639 639
640/**********GROUP CHATS************/
641
642/* returns valid ip port of connected friend on success
643 * returns zeroed out IP_Port on failure
644 */
645static IP_Port get_friend_ipport(Messenger *m, int friendnumber)
646{
647 IP_Port zero;
648 memset(&zero, 0, sizeof(zero));
649
650 if (friend_not_valid(m, friendnumber))
651 return zero;
652
653 int crypt_id = m->friendlist[friendnumber].crypt_connection_id;
654
655 if (is_cryptoconnected(m->net_crypto, crypt_id) != 3)
656 return zero;
657
658 return connection_ip(m->net_crypto->lossless_udp, m->net_crypto->crypto_connections[crypt_id].number);
659}
660
661/* returns the group number of the chat with public key group_public_key.
662 * returns -1 on failure.
663 */
664static int group_num(Messenger *m, uint8_t *group_public_key)
665{
666 uint32_t i;
667
668 for (i = 0; i < m->numchats; ++i) {
669 if (memcmp(m->chats[i]->self_public_key, group_public_key, crypto_box_PUBLICKEYBYTES) == 0)
670 return i;
671 }
672
673 return -1;
674}
675
676/* Creates a new groupchat and puts it in the chats array.
677 *
678 * return group number on success.
679 * return -1 on failure.
680 */
681int add_groupchat(Messenger *m)
682{
683 uint32_t i;
684
685 for (i = 0; i < m->numchats; ++i) {
686 if (m->chats[i] == NULL) {
687 Group_Chat *newchat = new_groupchat(m->net);
688
689 if (newchat == NULL)
690 return -1;
691
692 m->chats[i] = newchat;
693 return i;
694 }
695 }
696
697 Group_Chat **temp;
698 temp = realloc(m->chats, sizeof(Group_Chat *) * (m->numchats + 1));
699
700 if (temp == NULL)
701 return -1;
702
703 temp[m->numchats] = new_groupchat(m->net);
704
705 if (temp[m->numchats] == NULL)
706 return -1;
707
708 ++m->numchats;
709 return (m->numchats - 1);
710}
711
712/* Delete a groupchat from the chats array.
713 *
714 * return 0 on success.
715 * return -1 if failure.
716 */
717static int del_groupchat(Messenger *m, int groupnumber)
718{
719 if ((unsigned int)groupnumber >= m->numchats)
720 return -1;
721
722 if (m->chats == NULL)
723 return -1;
724
725 if (m->chats[groupnumber] == NULL)
726 return -1;
727
728 kill_groupchat(m->chats[groupnumber]);
729 m->chats[groupnumber] = NULL;
730
731 uint32_t i;
732
733 for (i = m->numchats; i != 0; --i) {
734 if (m->chats[i - 1] != NULL)
735 break;
736 }
737
738 if (i == 0) {
739 free(m->chats);
740 m->chats = NULL;
741 } else {
742 Group_Chat **temp = realloc(m->chats, sizeof(Group_Chat *) * i);
743
744 if (temp != NULL)
745 m->chats = temp;
746 }
747
748 return 0;
749}
750
751/* Join a group (you need to have been invited first.)
752 *
753 * returns 0 on success
754 * returns -1 on failure.
755 */
756int join_groupchat(Messenger *m, int friendnumber, uint8_t *friend_group_public_key)
757{
758 if (friend_not_valid(m, friendnumber))
759 return -1;
760
761 uint8_t data[crypto_box_PUBLICKEYBYTES * 2];
762 int groupnum = add_groupchat(m);
763
764 if (groupnum == -1)
765 return -1;
766
767 memcpy(data, friend_group_public_key, crypto_box_PUBLICKEYBYTES);
768 memcpy(data + crypto_box_PUBLICKEYBYTES, m->chats[groupnum]->self_public_key, crypto_box_PUBLICKEYBYTES);
769
770 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),
772 friend_group_public_key); //TODO: check if ip returned is zero?
773 return 0;
774 }
775
776 return -1;
777}
778
779static int handle_group(void *object, IP_Port source, uint8_t *packet, uint32_t length)
780{
781 Messenger *m = object;
782
783 if (length < crypto_box_PUBLICKEYBYTES + 1) {
784 return 1;
785 }
786
787 uint32_t i;
788
789 for (i = 0; i < m->numchats; ++i) {
790 if (m->chats[i] == NULL)
791 continue;
792
793 if (memcmp(packet + 1, m->chats[i]->self_public_key, crypto_box_PUBLICKEYBYTES) == 0)
794 return handle_groupchatpacket(m->chats[i], source, packet, length);
795 }
796
797 return 1;
798}
799
800static void do_allgroupchats(Messenger *m)
801{
802 uint32_t i;
803
804 for (i = 0; i < m->numchats; ++i) {
805 if (m->chats[i] != NULL)
806 do_groupchat(m->chats[i]);
807 }
808}
809
810/*********************************/
811
640#define PORT 33445 812#define PORT 33445
641 813
642/* Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds. */ 814/* Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds. */
@@ -688,6 +860,7 @@ Messenger *initMessenger(void)
688 friendreq_init(&(m->fr), m->net_crypto); 860 friendreq_init(&(m->fr), m->net_crypto);
689 LANdiscovery_init(m->dht); 861 LANdiscovery_init(m->dht);
690 set_nospam(&(m->fr), random_int()); 862 set_nospam(&(m->fr), random_int());
863 networking_registerhandler(m->net, NET_PACKET_GROUP_CHATS, &handle_group, m);
691 864
692 return m; 865 return m;
693} 866}
@@ -940,6 +1113,7 @@ void doMessenger(Messenger *m)
940 do_net_crypto(m->net_crypto); 1113 do_net_crypto(m->net_crypto);
941 doInbound(m); 1114 doInbound(m);
942 doFriends(m); 1115 doFriends(m);
1116 do_allgroupchats(m);
943 LANdiscovery(m); 1117 LANdiscovery(m);
944} 1118}
945 1119
diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h
index 40617d08..7649a779 100644
--- a/toxcore/Messenger.h
+++ b/toxcore/Messenger.h
@@ -44,7 +44,9 @@
44#define PACKET_ID_RECEIPT 65 44#define PACKET_ID_RECEIPT 65
45#define PACKET_ID_MESSAGE 64 45#define PACKET_ID_MESSAGE 64
46#define PACKET_ID_ACTION 63 46#define PACKET_ID_ACTION 63
47 47#define PACKET_ID_INVITE_GROUPCHAT 144
48#define PACKET_ID_JOIN_GROUPCHAT 145
49#define PACKET_ID_ACCEPT_GROUPCHAT 146
48 50
49/* Status definitions. */ 51/* Status definitions. */
50enum { 52enum {
@@ -134,7 +136,7 @@ typedef struct Messenger {
134 Friend *friendlist; 136 Friend *friendlist;
135 uint32_t numfriends; 137 uint32_t numfriends;
136 138
137 Group_Chat *chats; 139 Group_Chat **chats;
138 uint32_t numchats; 140 uint32_t numchats;
139 141
140 uint64_t last_LANdiscovery; 142 uint64_t last_LANdiscovery;
diff --git a/toxcore/group_chats.c b/toxcore/group_chats.c
index 4581eb38..1e5309ad 100644
--- a/toxcore/group_chats.c
+++ b/toxcore/group_chats.c
@@ -184,7 +184,7 @@ static int send_groupchatpacket(Group_Chat *chat, IP_Port ip_port, uint8_t *publ
184 184
185 uint8_t packet[MAX_DATA_SIZE]; 185 uint8_t packet[MAX_DATA_SIZE];
186 int len = create_request(chat->self_public_key, chat->self_secret_key, packet, public_key, data, length, request_id); 186 int len = create_request(chat->self_public_key, chat->self_secret_key, packet, public_key, data, length, request_id);
187 packet[0] = 48; 187 packet[0] = NET_PACKET_GROUP_CHATS;
188 188
189 if (len == -1) 189 if (len == -1)
190 return -1; 190 return -1;
@@ -581,3 +581,10 @@ void chat_bootstrap(Group_Chat *chat, IP_Port ip_port, uint8_t *client_id)
581{ 581{
582 send_getnodes(chat, ip_port, addpeer(chat, client_id)); 582 send_getnodes(chat, ip_port, addpeer(chat, client_id));
583} 583}
584
585void chat_bootstrap_nonlazy(Group_Chat *chat, IP_Port ip_port, uint8_t *client_id)
586{
587 send_getnodes(chat, ip_port, addpeer(chat, client_id));
588 add_closepeer(chat, client_id, ip_port);
589}
590
diff --git a/toxcore/group_chats.h b/toxcore/group_chats.h
index 7af13d8d..535b46db 100644
--- a/toxcore/group_chats.h
+++ b/toxcore/group_chats.h
@@ -115,7 +115,7 @@ int handle_groupchatpacket(Group_Chat *chat, IP_Port source, uint8_t *packet, ui
115 115
116 116
117void chat_bootstrap(Group_Chat *chat, IP_Port ip_port, uint8_t *client_id); 117void chat_bootstrap(Group_Chat *chat, IP_Port ip_port, uint8_t *client_id);
118 118void chat_bootstrap_nonlazy(Group_Chat *chat, IP_Port ip_port, uint8_t *client_id);
119#ifdef __cplusplus 119#ifdef __cplusplus
120} 120}
121#endif 121#endif
diff --git a/toxcore/network.h b/toxcore/network.h
index 2d08e88d..98307e5b 100644
--- a/toxcore/network.h
+++ b/toxcore/network.h
@@ -72,7 +72,7 @@
72#define NET_PACKET_DATA 18 /* Data packet ID. */ 72#define NET_PACKET_DATA 18 /* Data packet ID. */
73#define NET_PACKET_CRYPTO 32 /* Encrypted data packet ID. */ 73#define NET_PACKET_CRYPTO 32 /* Encrypted data packet ID. */
74#define NET_PACKET_LAN_DISCOVERY 33 /* LAN discovery packet ID. */ 74#define NET_PACKET_LAN_DISCOVERY 33 /* LAN discovery packet ID. */
75 75#define NET_PACKET_GROUP_CHATS 48 /* Group chats packet ID. */
76 76
77/* Current time, unix format */ 77/* Current time, unix format */
78#define unix_time() ((uint64_t)time(NULL)) 78#define unix_time() ((uint64_t)time(NULL))