summaryrefslogtreecommitdiff
path: root/core/Messenger.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-07-17 12:07:19 -0400
committerirungentoo <irungentoo@gmail.com>2013-07-17 12:07:19 -0400
commit4864cb9edbe567af6f774bcf6ad90211449a258f (patch)
treec23d1d72b7de039dd0df1e3ce60963bdddf7e4a1 /core/Messenger.c
parentffa809b3797e1e8fba4c1c495c4990cae44477d7 (diff)
Bug fixed, Loading and saving added to core.
Diffstat (limited to 'core/Messenger.c')
-rw-r--r--core/Messenger.c76
1 files changed, 75 insertions, 1 deletions
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()
280 break; 280 break;
281 case 4: 281 case 4:
282 crypto_kill(friendlist[i].crypt_connection_id); 282 crypto_kill(friendlist[i].crypt_connection_id);
283 friendlist[i].crypt_connection_id = -1;
283 break; 284 break;
284 default: 285 default:
285 break; 286 break;
@@ -300,6 +301,7 @@ static void doFriends()
300 if(is_cryptoconnected(friendlist[i].crypt_connection_id) == 4)//if the connection timed out, kill it 301 if(is_cryptoconnected(friendlist[i].crypt_connection_id) == 4)//if the connection timed out, kill it
301 { 302 {
302 crypto_kill(friendlist[i].crypt_connection_id); 303 crypto_kill(friendlist[i].crypt_connection_id);
304 friendlist[i].crypt_connection_id = -1;
303 friendlist[i].status = 3; 305 friendlist[i].status = 3;
304 } 306 }
305 break; 307 break;
@@ -333,6 +335,7 @@ static void doInbound()
333 int friend_id = getfriend_id(public_key); 335 int friend_id = getfriend_id(public_key);
334 if(friend_id != -1) 336 if(friend_id != -1)
335 { 337 {
338 crypto_kill(friendlist[friend_id].crypt_connection_id);
336 friendlist[friend_id].crypt_connection_id = 339 friendlist[friend_id].crypt_connection_id =
337 accept_crypto_inbound(inconnection, public_key, secret_nonce, session_key); 340 accept_crypto_inbound(inconnection, public_key, secret_nonce, session_key);
338 341
@@ -362,7 +365,7 @@ void doMessenger()
362 printf("Received handled packet with length: %u\n", length); 365 printf("Received handled packet with length: %u\n", length);
363 } 366 }
364 //} 367 //}
365 printf("Status: %u %u\n",friendlist[0].status ,is_cryptoconnected(friendlist[0].crypt_connection_id) ); 368 printf("Status: %u %u %u\n",friendlist[0].status ,is_cryptoconnected(friendlist[0].crypt_connection_id), friendlist[0].crypt_connection_id);
366#else 369#else
367 DHT_handlepacket(data, length, ip_port); 370 DHT_handlepacket(data, length, ip_port);
368 LosslessUDP_handlepacket(data, length, ip_port); 371 LosslessUDP_handlepacket(data, length, ip_port);
@@ -377,3 +380,74 @@ void doMessenger()
377 doFriends(); 380 doFriends();
378} 381}
379 382
383//returns the size of the messenger data (for saving)
384uint32_t Messenger_size()
385{
386 return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES
387 + sizeof(uint32_t) + DHT_size() + sizeof(uint32_t) + sizeof(Friend) * numfriends;
388}
389
390//save the messenger in data of size Messenger_size()
391void Messenger_save(uint8_t * data)
392{
393 save_keys(data);
394 data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
395 uint32_t size = DHT_size();
396 memcpy(data, &size, sizeof(size));
397 data += sizeof(size);
398 DHT_save(data);
399 data += size;
400 size = sizeof(Friend) * numfriends;
401 memcpy(data, &size, sizeof(size));
402 data += sizeof(size);
403 memcpy(data, friendlist, sizeof(Friend) * numfriends);
404}
405
406//load the messenger from data of size length.
407int Messenger_load(uint8_t * data, uint32_t length)
408{
409 if(length == ~0)
410 {
411 return -1;
412 }
413 if(length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2)
414 {
415 return -1;
416 }
417 length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2;
418 load_keys(data);
419 data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
420 uint32_t size;
421 memcpy(&size, data, sizeof(size));
422 data += sizeof(size);
423
424 if(length < size)
425 {
426 return -1;
427 }
428 length -= size;
429 if(DHT_load(data, size) == -1)
430 {
431 return -1;
432 }
433 data += size;
434 memcpy(&size, data, sizeof(size));
435 data += sizeof(size);
436 if(length != size || length % sizeof(Friend) != 0)
437 {
438 return -1;
439 }
440
441 Friend * temp = malloc(size);
442 memcpy(temp, data, size);
443
444 uint16_t num = size / sizeof(Friend);
445
446 uint32_t i;
447 for(i = 0; i < num; i++)
448 {
449 m_addfriend_norequest(temp[i].client_id);
450 }
451 free(temp);
452 return 0;
453}