summaryrefslogtreecommitdiff
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
parentffa809b3797e1e8fba4c1c495c4990cae44477d7 (diff)
Bug fixed, Loading and saving added to core.
-rw-r--r--core/Messenger.c76
-rw-r--r--core/Messenger.h12
-rw-r--r--core/net_crypto.c23
-rw-r--r--testing/Messenger_test.c44
4 files changed, 142 insertions, 13 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}
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.
90void doMessenger(); 90void doMessenger();
91 91
92
93//SAVING AND LOADING FUNCTIONS:
94
95//returns the size of the messenger data (for saving)
96uint32_t Messenger_size();
97
98//save the messenger in data (must be allocated memory of size Messenger_size())
99void Messenger_save(uint8_t * data);
100
101//load the messenger from data of size length.
102int 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)
423int crypto_connect(uint8_t * public_key, IP_Port ip_port) 423int 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
714static void killTimedout() 726static 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
diff --git a/testing/Messenger_test.c b/testing/Messenger_test.c
index c049aa18..484098b9 100644
--- a/testing/Messenger_test.c
+++ b/testing/Messenger_test.c
@@ -10,9 +10,14 @@
10 * This is how I compile it: gcc -O2 -Wall -D VANILLA_NACL -o test ../core/Lossless_UDP.c ../core/network.c ../core/net_crypto.c ../core/Messenger.c ../core/DHT.c ../nacl/build/${HOSTNAME%.*}/lib/amd64/{cpucycles.o,libnacl.a,randombytes.o} Messenger_test.c 10 * This is how I compile it: gcc -O2 -Wall -D VANILLA_NACL -o test ../core/Lossless_UDP.c ../core/network.c ../core/net_crypto.c ../core/Messenger.c ../core/DHT.c ../nacl/build/${HOSTNAME%.*}/lib/amd64/{cpucycles.o,libnacl.a,randombytes.o} Messenger_test.c
11 * 11 *
12 * 12 *
13 * Command line arguments are the ip and port of a node (for bootstrapping). 13 * Command line arguments are the ip, port and public_key of a node (for bootstrapping).
14 *
15 * EX: ./test 127.0.0.1 33445 CDCFD319CE3460824B33BE58FD86B8941C9585181D8FBD7C79C5721D7C2E9F7C
16 *
17 * Or the argument can be the path to the save file.
18 *
19 * EX: ./test Save.bak
14 * 20 *
15 * EX: ./test 127.0.0.1 33445
16 */ 21 */
17 22
18#include "../core/Messenger.h" 23#include "../core/Messenger.h"
@@ -77,11 +82,29 @@ void print_message(int friendnumber, uint8_t * string, uint16_t length)
77 82
78int main(int argc, char *argv[]) 83int main(int argc, char *argv[])
79{ 84{
80 if (argc < 4) { 85 if (argc < 4 && argc != 2) {
81 printf("usage %s ip port public_key (of the DHT bootstrap node)\n", argv[0]); 86 printf("usage %s ip port public_key (of the DHT bootstrap node)\n or\n %s Save.bak\n", argv[0], argv[0]);
82 exit(0); 87 exit(0);
83 } 88 }
84 initMessenger(); 89 initMessenger();
90 if(argc > 3)
91 {
92 IP_Port bootstrap_ip_port;
93 bootstrap_ip_port.port = htons(atoi(argv[2]));
94 bootstrap_ip_port.ip.i = inet_addr(argv[1]);
95 DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3]));
96 }
97 else
98 {
99 FILE *file = fopen(argv[1], "rb");
100 if ( file==NULL ){return 1;}
101 int read;
102 uint8_t buffer[128000];
103 read = fread(buffer, 1, 128000, file);
104 printf("Messenger loaded: %i\n", Messenger_load(buffer, read));
105 fclose(file);
106
107 }
85 m_callback_friendrequest(print_request); 108 m_callback_friendrequest(print_request);
86 m_callback_friendmessage(print_message); 109 m_callback_friendmessage(print_message);
87 110
@@ -103,16 +126,19 @@ int main(int argc, char *argv[])
103 int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); 126 int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo"));
104 127
105 perror("Initialization"); 128 perror("Initialization");
106 IP_Port bootstrap_ip_port; 129
107 bootstrap_ip_port.port = htons(atoi(argv[2]));
108 bootstrap_ip_port.ip.i = inet_addr(argv[1]);
109 DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3]));
110
111 while(1) 130 while(1)
112 { 131 {
113 m_sendmessage(num, (uint8_t*)"Test", 5); 132 m_sendmessage(num, (uint8_t*)"Test", 5);
114 doMessenger(); 133 doMessenger();
115 c_sleep(30); 134 c_sleep(30);
135 FILE *file = fopen("Save.bak", "wb");
136 if ( file==NULL ){return 1;}
137 uint8_t * buffer = malloc(Messenger_size());
138 Messenger_save(buffer);
139 fwrite(buffer, 1, Messenger_size(), file);
140 free(buffer);
141 fclose(file);
116 } 142 }
117 143
118} 144}