summaryrefslogtreecommitdiff
path: root/toxcore/Messenger.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/Messenger.c')
-rw-r--r--toxcore/Messenger.c65
1 files changed, 37 insertions, 28 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index cf744e23..b955ad5a 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -965,19 +965,23 @@ void Messenger_save(Messenger *m, uint8_t *data)
965{ 965{
966 save_keys(m->net_crypto, data); 966 save_keys(m->net_crypto, data);
967 data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; 967 data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
968
968 uint32_t nospam = get_nospam(&(m->fr)); 969 uint32_t nospam = get_nospam(&(m->fr));
969 memcpy(data, &nospam, sizeof(nospam)); 970 memcpy(data, &nospam, sizeof(nospam));
970 data += sizeof(nospam); 971 data += sizeof(nospam);
972
971 uint32_t size = DHT_size(m->dht); 973 uint32_t size = DHT_size(m->dht);
972 memcpy(data, &size, sizeof(size)); 974 memcpy(data, &size, sizeof(size));
973 data += sizeof(size); 975 data += sizeof(size);
974 DHT_save(m->dht, data); 976 DHT_save(m->dht, data);
975 data += size; 977 data += size;
978
976 size = sizeof(Friend) * m->numfriends; 979 size = sizeof(Friend) * m->numfriends;
977 memcpy(data, &size, sizeof(size)); 980 memcpy(data, &size, sizeof(size));
978 data += sizeof(size); 981 data += sizeof(size);
979 memcpy(data, m->friendlist, sizeof(Friend) * m->numfriends); 982 memcpy(data, m->friendlist, sizeof(Friend) * m->numfriends);
980 data += size; 983 data += size;
984
981 uint16_t small_size = m->name_length; 985 uint16_t small_size = m->name_length;
982 memcpy(data, &small_size, sizeof(small_size)); 986 memcpy(data, &small_size, sizeof(small_size));
983 data += sizeof(small_size); 987 data += sizeof(small_size);
@@ -990,59 +994,64 @@ int Messenger_load(Messenger *m, uint8_t *data, uint32_t length)
990 if (length == ~((uint32_t)0)) 994 if (length == ~((uint32_t)0))
991 return -1; 995 return -1;
992 996
993 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)
994 return -1; 999 return -1;
995 1000
996 length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 3;
997 load_keys(m->net_crypto, data); 1001 load_keys(m->net_crypto, data);
998 data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; 1002 data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
1003 length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
1004
999 uint32_t nospam; 1005 uint32_t nospam;
1000 memcpy(&nospam, data, sizeof(nospam)); 1006 memcpy(&nospam, data, sizeof(nospam));
1001 set_nospam(&(m->fr), nospam); 1007 set_nospam(&(m->fr), nospam);
1002 data += sizeof(nospam); 1008 data += sizeof(nospam);
1009 length -= sizeof(nospam);
1010
1003 uint32_t size; 1011 uint32_t size;
1004 memcpy(&size, data, sizeof(size)); 1012 memcpy(&size, data, sizeof(size));
1005 data += sizeof(size); 1013 data += sizeof(size);
1014 length -= sizeof(size);
1006 1015
1007 if (length < size) 1016 if (length < size)
1008 return -1; 1017 return -1;
1009 1018
1010 length -= size;
1011
1012 if (DHT_load(m->dht, data, size) == -1) 1019 if (DHT_load(m->dht, data, size) == -1)
1013 return -1; 1020 fprintf(stderr, "Data file: Something wicked happened to the stored connections.\n");
1021
1022 /* go on, friends still might be intact */
1014 1023
1015 data += size; 1024 data += size;
1025 length -= size;
1026
1016 memcpy(&size, data, sizeof(size)); 1027 memcpy(&size, data, sizeof(size));
1017 data += sizeof(size); 1028 data += sizeof(size);
1018 1029 if (length < size)
1019 if (length < size || size % sizeof(Friend) != 0)
1020 return -1; 1030 return -1;
1021 1031
1022 Friend *temp = malloc(size); 1032 if (!(size % sizeof(Friend))) {
1023 memcpy(temp, data, size); 1033 uint16_t num = size / sizeof(Friend);
1024 1034 Friend temp[num];
1025 uint16_t num = size / sizeof(Friend); 1035 memcpy(temp, data, size);
1026 1036
1027 uint32_t i; 1037 uint32_t i;
1028 1038 for (i = 0; i < num; ++i) {
1029 for (i = 0; i < num; ++i) { 1039 if (temp[i].status >= 3) {
1030 if (temp[i].status >= 3) { 1040 int fnum = m_addfriend_norequest(m, temp[i].client_id);
1031 int fnum = m_addfriend_norequest(m, temp[i].client_id); 1041 setfriendname(m, fnum, temp[i].name, temp[i].name_length);
1032 setfriendname(m, fnum, temp[i].name, temp[i].name_length); 1042 /* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */
1033 /* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */ 1043 } else if (temp[i].status != 0) {
1034 } else if (temp[i].status != 0) { 1044 /* TODO: This is not a good way to do this. */
1035 /* TODO: This is not a good way to do this. */ 1045 uint8_t address[FRIEND_ADDRESS_SIZE];
1036 uint8_t address[FRIEND_ADDRESS_SIZE]; 1046 memcpy(address, temp[i].client_id, crypto_box_PUBLICKEYBYTES);
1037 memcpy(address, temp[i].client_id, crypto_box_PUBLICKEYBYTES); 1047 memcpy(address + crypto_box_PUBLICKEYBYTES, &(temp[i].friendrequest_nospam), sizeof(uint32_t));
1038 memcpy(address + crypto_box_PUBLICKEYBYTES, &(temp[i].friendrequest_nospam), sizeof(uint32_t)); 1048 uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum));
1039 uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum)); 1049 memcpy(address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), &checksum, sizeof(checksum));
1040 memcpy(address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), &checksum, sizeof(checksum)); 1050 m_addfriend(m, address, temp[i].info, temp[i].info_size);
1041 m_addfriend(m, address, temp[i].info, temp[i].info_size); 1051 }
1042 } 1052 }
1043 } 1053 }
1044 1054
1045 free(temp);
1046 data += size; 1055 data += size;
1047 length -= size; 1056 length -= size;
1048 1057