From 4864cb9edbe567af6f774bcf6ad90211449a258f Mon Sep 17 00:00:00 2001 From: irungentoo Date: Wed, 17 Jul 2013 12:07:19 -0400 Subject: Bug fixed, Loading and saving added to core. --- core/Messenger.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- core/Messenger.h | 12 +++++++++ core/net_crypto.c | 23 ++++++++++++++--- 3 files changed, 107 insertions(+), 4 deletions(-) (limited to 'core') diff --git a/core/Messenger.c b/core/Messenger.c index 25313db0..2a846ba3 100644 --- a/core/Messenger.c +++ b/core/Messenger.c @@ -280,6 +280,7 @@ static void doFriends() break; case 4: crypto_kill(friendlist[i].crypt_connection_id); + friendlist[i].crypt_connection_id = -1; break; default: break; @@ -300,6 +301,7 @@ static void doFriends() if(is_cryptoconnected(friendlist[i].crypt_connection_id) == 4)//if the connection timed out, kill it { crypto_kill(friendlist[i].crypt_connection_id); + friendlist[i].crypt_connection_id = -1; friendlist[i].status = 3; } break; @@ -333,6 +335,7 @@ static void doInbound() int friend_id = getfriend_id(public_key); if(friend_id != -1) { + crypto_kill(friendlist[friend_id].crypt_connection_id); friendlist[friend_id].crypt_connection_id = accept_crypto_inbound(inconnection, public_key, secret_nonce, session_key); @@ -362,7 +365,7 @@ void doMessenger() printf("Received handled packet with length: %u\n", length); } //} - printf("Status: %u %u\n",friendlist[0].status ,is_cryptoconnected(friendlist[0].crypt_connection_id) ); + printf("Status: %u %u %u\n",friendlist[0].status ,is_cryptoconnected(friendlist[0].crypt_connection_id), friendlist[0].crypt_connection_id); #else DHT_handlepacket(data, length, ip_port); LosslessUDP_handlepacket(data, length, ip_port); @@ -377,3 +380,74 @@ void doMessenger() doFriends(); } +//returns the size of the messenger data (for saving) +uint32_t Messenger_size() +{ + return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + + sizeof(uint32_t) + DHT_size() + sizeof(uint32_t) + sizeof(Friend) * numfriends; +} + +//save the messenger in data of size Messenger_size() +void Messenger_save(uint8_t * data) +{ + save_keys(data); + data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; + uint32_t size = DHT_size(); + memcpy(data, &size, sizeof(size)); + data += sizeof(size); + DHT_save(data); + data += size; + size = sizeof(Friend) * numfriends; + memcpy(data, &size, sizeof(size)); + data += sizeof(size); + memcpy(data, friendlist, sizeof(Friend) * numfriends); +} + +//load the messenger from data of size length. +int Messenger_load(uint8_t * data, uint32_t length) +{ + if(length == ~0) + { + return -1; + } + if(length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2) + { + return -1; + } + length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2; + load_keys(data); + data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; + uint32_t size; + memcpy(&size, data, sizeof(size)); + data += sizeof(size); + + if(length < size) + { + return -1; + } + length -= size; + if(DHT_load(data, size) == -1) + { + return -1; + } + data += size; + memcpy(&size, data, sizeof(size)); + data += sizeof(size); + if(length != size || length % sizeof(Friend) != 0) + { + return -1; + } + + Friend * temp = malloc(size); + memcpy(temp, data, size); + + uint16_t num = size / sizeof(Friend); + + uint32_t i; + for(i = 0; i < num; i++) + { + m_addfriend_norequest(temp[i].client_id); + } + free(temp); + return 0; +} diff --git a/core/Messenger.h b/core/Messenger.h index 95d1d28e..0a3ae309 100644 --- a/core/Messenger.h +++ b/core/Messenger.h @@ -89,4 +89,16 @@ void initMessenger(); //the main loop that needs to be run at least 200 times per second. void doMessenger(); + +//SAVING AND LOADING FUNCTIONS: + +//returns the size of the messenger data (for saving) +uint32_t Messenger_size(); + +//save the messenger in data (must be allocated memory of size Messenger_size()) +void Messenger_save(uint8_t * data); + +//load the messenger from data of size length. +int Messenger_load(uint8_t * data, uint32_t length); + #endif diff --git a/core/net_crypto.c b/core/net_crypto.c index d4a3ba7b..62df614e 100644 --- a/core/net_crypto.c +++ b/core/net_crypto.c @@ -423,9 +423,14 @@ int getcryptconnection_id(uint8_t * public_key) int crypto_connect(uint8_t * public_key, IP_Port ip_port) { uint32_t i; - if(getcryptconnection_id(public_key) != -1) + int id = getcryptconnection_id(public_key); + if(id != -1) { - return -1; + IP_Port c_ip = connection_ip(crypto_connections[id].number); + if(c_ip.ip.i == ip_port.ip.i && c_ip.port == ip_port.port) + { + return -1; + } } for(i = 0; i < MAX_CRYPTO_CONNECTIONS; i++) { @@ -503,6 +508,7 @@ int crypto_kill(int crypt_connection_id) { crypto_connections[crypt_connection_id].status = 0; kill_connection(crypto_connections[crypt_connection_id].number); + crypto_connections[crypt_connection_id].number = ~0; return 0; } return 1; @@ -519,10 +525,11 @@ int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * sec { return -1; } + /* if(getcryptconnection_id(public_key) != -1) { return -1; - } + }*/ for(i = 0; i < MAX_CRYPTO_CONNECTIONS; i++) { if(crypto_connections[i].status == 0) @@ -709,6 +716,11 @@ void initNetCrypto() memset(crypto_connections, 0 ,sizeof(crypto_connections)); memset(outbound_friendrequests, -1 ,sizeof(outbound_friendrequests)); memset(incoming_connections, -1 ,sizeof(incoming_connections)); + uint32_t i; + for(i = 0; i < MAX_CRYPTO_CONNECTIONS; i++) + { + crypto_connections[i].number = ~0; + } } static void killTimedout() @@ -720,6 +732,11 @@ static void killTimedout() { crypto_connections[i].status = 4; } + else if(is_connected(crypto_connections[i].number) == 4) + { + kill_connection(crypto_connections[i].number); + crypto_connections[i].number = ~0; + } } } -- cgit v1.2.3