mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Bug fixed, Loading and saving added to core.
This commit is contained in:
parent
ffa809b379
commit
4864cb9edb
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -10,9 +10,14 @@
|
|||
* 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
|
||||
*
|
||||
*
|
||||
* Command line arguments are the ip and port of a node (for bootstrapping).
|
||||
* Command line arguments are the ip, port and public_key of a node (for bootstrapping).
|
||||
*
|
||||
* EX: ./test 127.0.0.1 33445 CDCFD319CE3460824B33BE58FD86B8941C9585181D8FBD7C79C5721D7C2E9F7C
|
||||
*
|
||||
* Or the argument can be the path to the save file.
|
||||
*
|
||||
* EX: ./test Save.bak
|
||||
*
|
||||
* EX: ./test 127.0.0.1 33445
|
||||
*/
|
||||
|
||||
#include "../core/Messenger.h"
|
||||
|
@ -77,11 +82,29 @@ void print_message(int friendnumber, uint8_t * string, uint16_t length)
|
|||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (argc < 4) {
|
||||
printf("usage %s ip port public_key (of the DHT bootstrap node)\n", argv[0]);
|
||||
if (argc < 4 && argc != 2) {
|
||||
printf("usage %s ip port public_key (of the DHT bootstrap node)\n or\n %s Save.bak\n", argv[0], argv[0]);
|
||||
exit(0);
|
||||
}
|
||||
initMessenger();
|
||||
if(argc > 3)
|
||||
{
|
||||
IP_Port bootstrap_ip_port;
|
||||
bootstrap_ip_port.port = htons(atoi(argv[2]));
|
||||
bootstrap_ip_port.ip.i = inet_addr(argv[1]);
|
||||
DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3]));
|
||||
}
|
||||
else
|
||||
{
|
||||
FILE *file = fopen(argv[1], "rb");
|
||||
if ( file==NULL ){return 1;}
|
||||
int read;
|
||||
uint8_t buffer[128000];
|
||||
read = fread(buffer, 1, 128000, file);
|
||||
printf("Messenger loaded: %i\n", Messenger_load(buffer, read));
|
||||
fclose(file);
|
||||
|
||||
}
|
||||
m_callback_friendrequest(print_request);
|
||||
m_callback_friendmessage(print_message);
|
||||
|
||||
|
@ -103,16 +126,19 @@ int main(int argc, char *argv[])
|
|||
int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo"));
|
||||
|
||||
perror("Initialization");
|
||||
IP_Port bootstrap_ip_port;
|
||||
bootstrap_ip_port.port = htons(atoi(argv[2]));
|
||||
bootstrap_ip_port.ip.i = inet_addr(argv[1]);
|
||||
DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3]));
|
||||
|
||||
|
||||
while(1)
|
||||
{
|
||||
m_sendmessage(num, (uint8_t*)"Test", 5);
|
||||
doMessenger();
|
||||
c_sleep(30);
|
||||
FILE *file = fopen("Save.bak", "wb");
|
||||
if ( file==NULL ){return 1;}
|
||||
uint8_t * buffer = malloc(Messenger_size());
|
||||
Messenger_save(buffer);
|
||||
fwrite(buffer, 1, Messenger_size(), file);
|
||||
free(buffer);
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user