summaryrefslogtreecommitdiff
path: root/core/Messenger.c
diff options
context:
space:
mode:
Diffstat (limited to 'core/Messenger.c')
-rw-r--r--core/Messenger.c58
1 files changed, 31 insertions, 27 deletions
diff --git a/core/Messenger.c b/core/Messenger.c
index 7fd6a569..cb3d2551 100644
--- a/core/Messenger.c
+++ b/core/Messenger.c
@@ -246,7 +246,7 @@ int m_delfriend(Messenger *m, int friendnumber)
246 return -1; 246 return -1;
247 247
248 DHT_delfriend(m->friendlist[friendnumber].client_id); 248 DHT_delfriend(m->friendlist[friendnumber].client_id);
249 crypto_kill(m->friendlist[friendnumber].crypt_connection_id); 249 crypto_kill(m->net_crypto, m->friendlist[friendnumber].crypt_connection_id);
250 free(m->friendlist[friendnumber].statusmessage); 250 free(m->friendlist[friendnumber].statusmessage);
251 memset(&(m->friendlist[friendnumber]), 0, sizeof(Friend)); 251 memset(&(m->friendlist[friendnumber]), 0, sizeof(Friend));
252 uint32_t i; 252 uint32_t i;
@@ -606,7 +606,7 @@ int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint
606 if (length != 0) 606 if (length != 0)
607 memcpy(packet + 1, data, length); 607 memcpy(packet + 1, data, length);
608 608
609 return write_cryptpacket(m->friendlist[friendnumber].crypt_connection_id, packet, length + 1); 609 return write_cryptpacket(m->net_crypto, m->friendlist[friendnumber].crypt_connection_id, packet, length + 1);
610} 610}
611 611
612 612
@@ -626,21 +626,25 @@ int LANdiscovery(timer *t, void *arg)
626Messenger *initMessenger(void) 626Messenger *initMessenger(void)
627{ 627{
628 Messenger *m = calloc(1, sizeof(Messenger)); 628 Messenger *m = calloc(1, sizeof(Messenger));
629
630 if ( ! m ) 629 if ( ! m )
631 return 0; 630 return 0;
632
633 new_keys();
634 m_set_statusmessage(m, (uint8_t *)"Online", sizeof("Online"));
635 initNetCrypto();
636 IP ip; 631 IP ip;
637 ip.i = 0; 632 ip.i = 0;
638 633 m->net = new_networking(ip, PORT);
639 if (init_networking(ip, PORT) == -1) 634 if (m->net == NULL) {
640 return 0; 635 free(m);
636 return NULL;
637 }
638 m->net_crypto = new_net_crypto(m->net);
639 if (m->net_crypto == NULL) {
640 free(m);
641 free(m->net);
642 return NULL;
643 }
644 new_keys(m->net_crypto);
645 m_set_statusmessage(m, (uint8_t *)"Online", sizeof("Online"));
641 646
642 DHT_init(); 647 DHT_init();
643 LosslessUDP_init();
644 friendreq_init(); 648 friendreq_init();
645 LANdiscovery_init(); 649 LANdiscovery_init();
646 set_nospam(random_int()); 650 set_nospam(random_int());
@@ -657,6 +661,7 @@ void cleanupMessenger(Messenger *m)
657 /* FIXME TODO ideally cleanupMessenger will mirror initMessenger 661 /* FIXME TODO ideally cleanupMessenger will mirror initMessenger
658 * this requires the other modules to expose cleanup functions 662 * this requires the other modules to expose cleanup functions
659 */ 663 */
664 kill_net_crypto(m->net_crypto);
660 free(m->friendlist); 665 free(m->friendlist);
661 free(m); 666 free(m);
662} 667}
@@ -696,10 +701,10 @@ void doFriends(Messenger *m)
696 701
697 IP_Port friendip = DHT_getfriendip(m->friendlist[i].client_id); 702 IP_Port friendip = DHT_getfriendip(m->friendlist[i].client_id);
698 703
699 switch (is_cryptoconnected(m->friendlist[i].crypt_connection_id)) { 704 switch (is_cryptoconnected(m->net_crypto, m->friendlist[i].crypt_connection_id)) {
700 case 0: 705 case 0:
701 if (friendip.ip.i > 1) 706 if (friendip.ip.i > 1)
702 m->friendlist[i].crypt_connection_id = crypto_connect(m->friendlist[i].client_id, friendip); 707 m->friendlist[i].crypt_connection_id = crypto_connect(m->net_crypto, m->friendlist[i].client_id, friendip);
703 708
704 break; 709 break;
705 710
@@ -712,7 +717,7 @@ void doFriends(Messenger *m)
712 break; 717 break;
713 718
714 case 4: 719 case 4:
715 crypto_kill(m->friendlist[i].crypt_connection_id); 720 crypto_kill(m->net_crypto, m->friendlist[i].crypt_connection_id);
716 m->friendlist[i].crypt_connection_id = -1; 721 m->friendlist[i].crypt_connection_id = -1;
717 break; 722 break;
718 723
@@ -741,7 +746,7 @@ void doFriends(Messenger *m)
741 send_ping(m, i); 746 send_ping(m, i);
742 } 747 }
743 748
744 len = read_cryptpacket(m->friendlist[i].crypt_connection_id, temp); 749 len = read_cryptpacket(m->net_crypto, m->friendlist[i].crypt_connection_id, temp);
745 uint8_t packet_id = temp[0]; 750 uint8_t packet_id = temp[0];
746 uint8_t *data = temp + 1; 751 uint8_t *data = temp + 1;
747 int data_length = len - 1; 752 int data_length = len - 1;
@@ -833,8 +838,8 @@ void doFriends(Messenger *m)
833 } 838 }
834 } 839 }
835 } else { 840 } else {
836 if (is_cryptoconnected(m->friendlist[i].crypt_connection_id) == 4) { /* if the connection timed out, kill it */ 841 if (is_cryptoconnected(m->net_crypto, m->friendlist[i].crypt_connection_id) == 4) { /* if the connection timed out, kill it */
837 crypto_kill(m->friendlist[i].crypt_connection_id); 842 crypto_kill(m->net_crypto, m->friendlist[i].crypt_connection_id);
838 m->friendlist[i].crypt_connection_id = -1; 843 m->friendlist[i].crypt_connection_id = -1;
839 set_friend_status(m, i, FRIEND_CONFIRMED); 844 set_friend_status(m, i, FRIEND_CONFIRMED);
840 } 845 }
@@ -844,7 +849,7 @@ void doFriends(Messenger *m)
844 849
845 if (m->friendlist[i].ping_lastrecv + FRIEND_CONNECTION_TIMEOUT < temp_time) { 850 if (m->friendlist[i].ping_lastrecv + FRIEND_CONNECTION_TIMEOUT < temp_time) {
846 /* if we stopped recieving ping packets kill it */ 851 /* if we stopped recieving ping packets kill it */
847 crypto_kill(m->friendlist[i].crypt_connection_id); 852 crypto_kill(m->net_crypto, m->friendlist[i].crypt_connection_id);
848 m->friendlist[i].crypt_connection_id = -1; 853 m->friendlist[i].crypt_connection_id = -1;
849 set_friend_status(m, i, FRIEND_CONFIRMED); 854 set_friend_status(m, i, FRIEND_CONFIRMED);
850 } 855 }
@@ -857,15 +862,15 @@ void doInbound(Messenger *m)
857 uint8_t secret_nonce[crypto_box_NONCEBYTES]; 862 uint8_t secret_nonce[crypto_box_NONCEBYTES];
858 uint8_t public_key[crypto_box_PUBLICKEYBYTES]; 863 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
859 uint8_t session_key[crypto_box_PUBLICKEYBYTES]; 864 uint8_t session_key[crypto_box_PUBLICKEYBYTES];
860 int inconnection = crypto_inbound(public_key, secret_nonce, session_key); 865 int inconnection = crypto_inbound(m->net_crypto, public_key, secret_nonce, session_key);
861 866
862 if (inconnection != -1) { 867 if (inconnection != -1) {
863 int friend_id = getfriend_id(m, public_key); 868 int friend_id = getfriend_id(m, public_key);
864 869
865 if (friend_id != -1) { 870 if (friend_id != -1) {
866 crypto_kill(m->friendlist[friend_id].crypt_connection_id); 871 crypto_kill(m->net_crypto, m->friendlist[friend_id].crypt_connection_id);
867 m->friendlist[friend_id].crypt_connection_id = 872 m->friendlist[friend_id].crypt_connection_id =
868 accept_crypto_inbound(inconnection, public_key, secret_nonce, session_key); 873 accept_crypto_inbound(m->net_crypto, inconnection, public_key, secret_nonce, session_key);
869 874
870 set_friend_status(m, friend_id, FRIEND_CONFIRMED); 875 set_friend_status(m, friend_id, FRIEND_CONFIRMED);
871 } 876 }
@@ -873,14 +878,13 @@ void doInbound(Messenger *m)
873} 878}
874 879
875 880
876/* the main loop that needs to be run at least 200 times per second. */ 881/* the main loop that needs to be run at least 20 times per second. */
877void doMessenger(Messenger *m) 882void doMessenger(Messenger *m)
878{ 883{
879 networking_poll(); 884 networking_poll(m->net);
880 885
881 doDHT(); 886 doDHT();
882 doLossless_UDP(); 887 do_net_crypto(m->net_crypto);
883 doNetCrypto();
884 doInbound(m); 888 doInbound(m);
885 doFriends(m); 889 doFriends(m);
886 890
@@ -904,7 +908,7 @@ uint32_t Messenger_size(Messenger *m)
904/* save the messenger in data of size Messenger_size() */ 908/* save the messenger in data of size Messenger_size() */
905void Messenger_save(Messenger *m, uint8_t *data) 909void Messenger_save(Messenger *m, uint8_t *data)
906{ 910{
907 save_keys(data); 911 save_keys(m->net_crypto, data);
908 data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; 912 data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
909 uint32_t nospam = get_nospam(); 913 uint32_t nospam = get_nospam();
910 memcpy(data, &nospam, sizeof(nospam)); 914 memcpy(data, &nospam, sizeof(nospam));
@@ -935,7 +939,7 @@ int Messenger_load(Messenger *m, uint8_t *data, uint32_t length)
935 return -1; 939 return -1;
936 940
937 length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 3; 941 length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 3;
938 load_keys(data); 942 load_keys(m->net_crypto, data);
939 data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; 943 data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
940 uint32_t nospam; 944 uint32_t nospam;
941 memcpy(&nospam, data, sizeof(nospam)); 945 memcpy(&nospam, data, sizeof(nospam));