diff options
author | irungentoo <irungentoo@gmail.com> | 2013-07-17 12:07:19 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-07-17 12:07:19 -0400 |
commit | 4864cb9edbe567af6f774bcf6ad90211449a258f (patch) | |
tree | c23d1d72b7de039dd0df1e3ce60963bdddf7e4a1 /core | |
parent | ffa809b3797e1e8fba4c1c495c4990cae44477d7 (diff) |
Bug fixed, Loading and saving added to core.
Diffstat (limited to 'core')
-rw-r--r-- | core/Messenger.c | 76 | ||||
-rw-r--r-- | core/Messenger.h | 12 | ||||
-rw-r--r-- | core/net_crypto.c | 23 |
3 files changed, 107 insertions, 4 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) | ||
384 | uint32_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() | ||
391 | void 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. | ||
407 | int 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 | } | ||
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(); | |||
89 | //the main loop that needs to be run at least 200 times per second. | 89 | //the main loop that needs to be run at least 200 times per second. |
90 | void doMessenger(); | 90 | void doMessenger(); |
91 | 91 | ||
92 | |||
93 | //SAVING AND LOADING FUNCTIONS: | ||
94 | |||
95 | //returns the size of the messenger data (for saving) | ||
96 | uint32_t Messenger_size(); | ||
97 | |||
98 | //save the messenger in data (must be allocated memory of size Messenger_size()) | ||
99 | void Messenger_save(uint8_t * data); | ||
100 | |||
101 | //load the messenger from data of size length. | ||
102 | int Messenger_load(uint8_t * data, uint32_t length); | ||
103 | |||
92 | #endif | 104 | #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) | |||
423 | int crypto_connect(uint8_t * public_key, IP_Port ip_port) | 423 | int crypto_connect(uint8_t * public_key, IP_Port ip_port) |
424 | { | 424 | { |
425 | uint32_t i; | 425 | uint32_t i; |
426 | if(getcryptconnection_id(public_key) != -1) | 426 | int id = getcryptconnection_id(public_key); |
427 | if(id != -1) | ||
427 | { | 428 | { |
428 | return -1; | 429 | IP_Port c_ip = connection_ip(crypto_connections[id].number); |
430 | if(c_ip.ip.i == ip_port.ip.i && c_ip.port == ip_port.port) | ||
431 | { | ||
432 | return -1; | ||
433 | } | ||
429 | } | 434 | } |
430 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; i++) | 435 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; i++) |
431 | { | 436 | { |
@@ -503,6 +508,7 @@ int crypto_kill(int crypt_connection_id) | |||
503 | { | 508 | { |
504 | crypto_connections[crypt_connection_id].status = 0; | 509 | crypto_connections[crypt_connection_id].status = 0; |
505 | kill_connection(crypto_connections[crypt_connection_id].number); | 510 | kill_connection(crypto_connections[crypt_connection_id].number); |
511 | crypto_connections[crypt_connection_id].number = ~0; | ||
506 | return 0; | 512 | return 0; |
507 | } | 513 | } |
508 | return 1; | 514 | return 1; |
@@ -519,10 +525,11 @@ int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * sec | |||
519 | { | 525 | { |
520 | return -1; | 526 | return -1; |
521 | } | 527 | } |
528 | /* | ||
522 | if(getcryptconnection_id(public_key) != -1) | 529 | if(getcryptconnection_id(public_key) != -1) |
523 | { | 530 | { |
524 | return -1; | 531 | return -1; |
525 | } | 532 | }*/ |
526 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; i++) | 533 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; i++) |
527 | { | 534 | { |
528 | if(crypto_connections[i].status == 0) | 535 | if(crypto_connections[i].status == 0) |
@@ -709,6 +716,11 @@ void initNetCrypto() | |||
709 | memset(crypto_connections, 0 ,sizeof(crypto_connections)); | 716 | memset(crypto_connections, 0 ,sizeof(crypto_connections)); |
710 | memset(outbound_friendrequests, -1 ,sizeof(outbound_friendrequests)); | 717 | memset(outbound_friendrequests, -1 ,sizeof(outbound_friendrequests)); |
711 | memset(incoming_connections, -1 ,sizeof(incoming_connections)); | 718 | memset(incoming_connections, -1 ,sizeof(incoming_connections)); |
719 | uint32_t i; | ||
720 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; i++) | ||
721 | { | ||
722 | crypto_connections[i].number = ~0; | ||
723 | } | ||
712 | } | 724 | } |
713 | 725 | ||
714 | static void killTimedout() | 726 | static void killTimedout() |
@@ -720,6 +732,11 @@ static void killTimedout() | |||
720 | { | 732 | { |
721 | crypto_connections[i].status = 4; | 733 | crypto_connections[i].status = 4; |
722 | } | 734 | } |
735 | else if(is_connected(crypto_connections[i].number) == 4) | ||
736 | { | ||
737 | kill_connection(crypto_connections[i].number); | ||
738 | crypto_connections[i].number = ~0; | ||
739 | } | ||
723 | } | 740 | } |
724 | } | 741 | } |
725 | 742 | ||