diff options
Diffstat (limited to 'toxcore/Messenger.c')
-rw-r--r-- | toxcore/Messenger.c | 74 |
1 files changed, 42 insertions, 32 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index b712d142..b955ad5a 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c | |||
@@ -651,7 +651,7 @@ static void LANdiscovery(Messenger *m) | |||
651 | } | 651 | } |
652 | 652 | ||
653 | /* Run this at startup. */ | 653 | /* Run this at startup. */ |
654 | Messenger *initMessenger(void) | 654 | Messenger *initMessenger(uint8_t ipv6enabled) |
655 | { | 655 | { |
656 | Messenger *m = calloc(1, sizeof(Messenger)); | 656 | Messenger *m = calloc(1, sizeof(Messenger)); |
657 | 657 | ||
@@ -659,7 +659,7 @@ Messenger *initMessenger(void) | |||
659 | return NULL; | 659 | return NULL; |
660 | 660 | ||
661 | IP ip; | 661 | IP ip; |
662 | ip.uint32 = 0; | 662 | ip_init(&ip, ipv6enabled); |
663 | m->net = new_networking(ip, PORT); | 663 | m->net = new_networking(ip, PORT); |
664 | 664 | ||
665 | if (m->net == NULL) { | 665 | if (m->net == NULL) { |
@@ -743,11 +743,12 @@ void doFriends(Messenger *m) | |||
743 | } | 743 | } |
744 | } | 744 | } |
745 | 745 | ||
746 | IP_Port friendip = DHT_getfriendip(m->dht, m->friendlist[i].client_id); | 746 | IP_Port friendip; |
747 | int friendok = DHT_getfriendip(m->dht, m->friendlist[i].client_id, &friendip); | ||
747 | 748 | ||
748 | switch (is_cryptoconnected(m->net_crypto, m->friendlist[i].crypt_connection_id)) { | 749 | switch (is_cryptoconnected(m->net_crypto, m->friendlist[i].crypt_connection_id)) { |
749 | case 0: | 750 | case 0: |
750 | if (friendip.ip.uint32 > 1) | 751 | if (friendok == 1) |
751 | m->friendlist[i].crypt_connection_id = crypto_connect(m->net_crypto, m->friendlist[i].client_id, friendip); | 752 | m->friendlist[i].crypt_connection_id = crypto_connect(m->net_crypto, m->friendlist[i].client_id, friendip); |
752 | 753 | ||
753 | break; | 754 | break; |
@@ -964,19 +965,23 @@ void Messenger_save(Messenger *m, uint8_t *data) | |||
964 | { | 965 | { |
965 | save_keys(m->net_crypto, data); | 966 | save_keys(m->net_crypto, data); |
966 | data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; | 967 | data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; |
968 | |||
967 | uint32_t nospam = get_nospam(&(m->fr)); | 969 | uint32_t nospam = get_nospam(&(m->fr)); |
968 | memcpy(data, &nospam, sizeof(nospam)); | 970 | memcpy(data, &nospam, sizeof(nospam)); |
969 | data += sizeof(nospam); | 971 | data += sizeof(nospam); |
972 | |||
970 | uint32_t size = DHT_size(m->dht); | 973 | uint32_t size = DHT_size(m->dht); |
971 | memcpy(data, &size, sizeof(size)); | 974 | memcpy(data, &size, sizeof(size)); |
972 | data += sizeof(size); | 975 | data += sizeof(size); |
973 | DHT_save(m->dht, data); | 976 | DHT_save(m->dht, data); |
974 | data += size; | 977 | data += size; |
978 | |||
975 | size = sizeof(Friend) * m->numfriends; | 979 | size = sizeof(Friend) * m->numfriends; |
976 | memcpy(data, &size, sizeof(size)); | 980 | memcpy(data, &size, sizeof(size)); |
977 | data += sizeof(size); | 981 | data += sizeof(size); |
978 | memcpy(data, m->friendlist, sizeof(Friend) * m->numfriends); | 982 | memcpy(data, m->friendlist, sizeof(Friend) * m->numfriends); |
979 | data += size; | 983 | data += size; |
984 | |||
980 | uint16_t small_size = m->name_length; | 985 | uint16_t small_size = m->name_length; |
981 | memcpy(data, &small_size, sizeof(small_size)); | 986 | memcpy(data, &small_size, sizeof(small_size)); |
982 | data += sizeof(small_size); | 987 | data += sizeof(small_size); |
@@ -989,59 +994,64 @@ int Messenger_load(Messenger *m, uint8_t *data, uint32_t length) | |||
989 | if (length == ~((uint32_t)0)) | 994 | if (length == ~((uint32_t)0)) |
990 | return -1; | 995 | return -1; |
991 | 996 | ||
992 | if (length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 3) | 997 | /* BLOCK1: PUBKEY, SECKEY, NOSPAM, SIZE */ |
998 | if (length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2) | ||
993 | return -1; | 999 | return -1; |
994 | 1000 | ||
995 | length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 3; | ||
996 | load_keys(m->net_crypto, data); | 1001 | load_keys(m->net_crypto, data); |
997 | data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; | 1002 | data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; |
1003 | length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; | ||
1004 | |||
998 | uint32_t nospam; | 1005 | uint32_t nospam; |
999 | memcpy(&nospam, data, sizeof(nospam)); | 1006 | memcpy(&nospam, data, sizeof(nospam)); |
1000 | set_nospam(&(m->fr), nospam); | 1007 | set_nospam(&(m->fr), nospam); |
1001 | data += sizeof(nospam); | 1008 | data += sizeof(nospam); |
1009 | length -= sizeof(nospam); | ||
1010 | |||
1002 | uint32_t size; | 1011 | uint32_t size; |
1003 | memcpy(&size, data, sizeof(size)); | 1012 | memcpy(&size, data, sizeof(size)); |
1004 | data += sizeof(size); | 1013 | data += sizeof(size); |
1014 | length -= sizeof(size); | ||
1005 | 1015 | ||
1006 | if (length < size) | 1016 | if (length < size) |
1007 | return -1; | 1017 | return -1; |
1008 | 1018 | ||
1009 | length -= size; | ||
1010 | |||
1011 | if (DHT_load(m->dht, data, size) == -1) | 1019 | if (DHT_load(m->dht, data, size) == -1) |
1012 | return -1; | 1020 | fprintf(stderr, "Data file: Something wicked happened to the stored connections.\n"); |
1021 | |||
1022 | /* go on, friends still might be intact */ | ||
1013 | 1023 | ||
1014 | data += size; | 1024 | data += size; |
1025 | length -= size; | ||
1026 | |||
1015 | memcpy(&size, data, sizeof(size)); | 1027 | memcpy(&size, data, sizeof(size)); |
1016 | data += sizeof(size); | 1028 | data += sizeof(size); |
1017 | 1029 | if (length < size) | |
1018 | if (length < size || size % sizeof(Friend) != 0) | ||
1019 | return -1; | 1030 | return -1; |
1020 | 1031 | ||
1021 | Friend *temp = malloc(size); | 1032 | if (!(size % sizeof(Friend))) { |
1022 | memcpy(temp, data, size); | 1033 | uint16_t num = size / sizeof(Friend); |
1023 | 1034 | Friend temp[num]; | |
1024 | uint16_t num = size / sizeof(Friend); | 1035 | memcpy(temp, data, size); |
1025 | 1036 | ||
1026 | uint32_t i; | 1037 | uint32_t i; |
1027 | 1038 | for (i = 0; i < num; ++i) { | |
1028 | for (i = 0; i < num; ++i) { | 1039 | if (temp[i].status >= 3) { |
1029 | if (temp[i].status >= 3) { | 1040 | int fnum = m_addfriend_norequest(m, temp[i].client_id); |
1030 | int fnum = m_addfriend_norequest(m, temp[i].client_id); | 1041 | setfriendname(m, fnum, temp[i].name, temp[i].name_length); |
1031 | setfriendname(m, fnum, temp[i].name, temp[i].name_length); | 1042 | /* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */ |
1032 | /* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */ | 1043 | } else if (temp[i].status != 0) { |
1033 | } else if (temp[i].status != 0) { | 1044 | /* TODO: This is not a good way to do this. */ |
1034 | /* TODO: This is not a good way to do this. */ | 1045 | uint8_t address[FRIEND_ADDRESS_SIZE]; |
1035 | uint8_t address[FRIEND_ADDRESS_SIZE]; | 1046 | memcpy(address, temp[i].client_id, crypto_box_PUBLICKEYBYTES); |
1036 | memcpy(address, temp[i].client_id, crypto_box_PUBLICKEYBYTES); | 1047 | memcpy(address + crypto_box_PUBLICKEYBYTES, &(temp[i].friendrequest_nospam), sizeof(uint32_t)); |
1037 | memcpy(address + crypto_box_PUBLICKEYBYTES, &(temp[i].friendrequest_nospam), sizeof(uint32_t)); | 1048 | uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum)); |
1038 | uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum)); | 1049 | memcpy(address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), &checksum, sizeof(checksum)); |
1039 | memcpy(address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), &checksum, sizeof(checksum)); | 1050 | m_addfriend(m, address, temp[i].info, temp[i].info_size); |
1040 | m_addfriend(m, address, temp[i].info, temp[i].info_size); | 1051 | } |
1041 | } | 1052 | } |
1042 | } | 1053 | } |
1043 | 1054 | ||
1044 | free(temp); | ||
1045 | data += size; | 1055 | data += size; |
1046 | length -= size; | 1056 | length -= size; |
1047 | 1057 | ||