mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Added dynamic memory allocation to crypto connections.
Also fixed some possible bugs in Messenger.c
This commit is contained in:
parent
35376d85aa
commit
7d092c3467
|
@ -42,7 +42,6 @@ int realloc_friendlist(Messenger *m, uint32_t num)
|
|||
if (newfriendlist == NULL)
|
||||
return -1;
|
||||
|
||||
memset(&newfriendlist[num - 1], 0, sizeof(Friend));
|
||||
m->friendlist = newfriendlist;
|
||||
return 0;
|
||||
}
|
||||
|
@ -165,6 +164,8 @@ int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length)
|
|||
if (realloc_friendlist(m, m->numfriends + 1) != 0)
|
||||
return FAERR_NOMEM;
|
||||
|
||||
memset(&(m->friendlist[m->numfriends]), 0, sizeof(Friend));
|
||||
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i <= m->numfriends; ++i) {
|
||||
|
@ -184,7 +185,9 @@ int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length)
|
|||
m->friendlist[i].receives_read_receipts = 1; /* default: YES */
|
||||
memcpy(&(m->friendlist[i].friendrequest_nospam), address + crypto_box_PUBLICKEYBYTES, sizeof(uint32_t));
|
||||
|
||||
++ m->numfriends;
|
||||
if (m->numfriends == i)
|
||||
++ m->numfriends;
|
||||
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
@ -201,6 +204,8 @@ int m_addfriend_norequest(Messenger *m, uint8_t *client_id)
|
|||
if (realloc_friendlist(m, m->numfriends + 1) != 0)
|
||||
return FAERR_NOMEM;
|
||||
|
||||
memset(&(m->friendlist[m->numfriends]), 0, sizeof(Friend));
|
||||
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i <= m->numfriends; ++i) {
|
||||
|
@ -215,7 +220,10 @@ int m_addfriend_norequest(Messenger *m, uint8_t *client_id)
|
|||
m->friendlist[i].userstatus = USERSTATUS_NONE;
|
||||
m->friendlist[i].message_id = 0;
|
||||
m->friendlist[i].receives_read_receipts = 1; /* default: YES */
|
||||
++ m->numfriends;
|
||||
|
||||
if (m->numfriends == i)
|
||||
++ m->numfriends;
|
||||
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
@ -244,7 +252,7 @@ int m_delfriend(Messenger *m, int friendnumber)
|
|||
|
||||
m->numfriends = i;
|
||||
|
||||
if (realloc_friendlist(m, m->numfriends + 1) != 0)
|
||||
if (realloc_friendlist(m, m->numfriends) != 0)
|
||||
return FAERR_NOMEM;
|
||||
|
||||
return 0;
|
||||
|
@ -851,14 +859,14 @@ void doMessenger(Messenger *m)
|
|||
uint32_t Messenger_size(Messenger *m)
|
||||
{
|
||||
return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES
|
||||
+ sizeof(uint32_t) // nospam
|
||||
+ sizeof(uint32_t) // DHT size
|
||||
+ DHT_size() // DHT itself
|
||||
+ sizeof(uint32_t) // Friendlist size
|
||||
+ sizeof(Friend) * m->numfriends // Friendlist itself
|
||||
+ sizeof(uint16_t) // Own nickname length
|
||||
+ m->name_length // Own nickname
|
||||
;
|
||||
+ sizeof(uint32_t) // nospam
|
||||
+ sizeof(uint32_t) // DHT size
|
||||
+ DHT_size() // DHT itself
|
||||
+ sizeof(uint32_t) // Friendlist size
|
||||
+ sizeof(Friend) * m->numfriends // Friendlist itself
|
||||
+ sizeof(uint16_t) // Own nickname length
|
||||
+ m->name_length // Own nickname
|
||||
;
|
||||
}
|
||||
|
||||
/* save the messenger in data of size Messenger_size() */
|
||||
|
|
|
@ -45,9 +45,11 @@ typedef struct {
|
|||
|
||||
} Crypto_Connection;
|
||||
|
||||
#define MAX_CRYPTO_CONNECTIONS 256
|
||||
static Crypto_Connection *crypto_connections;
|
||||
|
||||
static Crypto_Connection crypto_connections[MAX_CRYPTO_CONNECTIONS];
|
||||
static uint32_t crypto_connections_length; /* Length of connections array */
|
||||
|
||||
#define MAX_CRYPTO_CONNECTIONS crypto_connections_length
|
||||
|
||||
#define CONN_NO_CONNECTION 0
|
||||
#define CONN_HANDSHAKE_SENT 1
|
||||
|
@ -400,6 +402,19 @@ static int getcryptconnection_id(uint8_t *public_key)
|
|||
return -1;
|
||||
}
|
||||
|
||||
/* set the size of the friend list to numfriends
|
||||
return -1 if realloc fails */
|
||||
int realloc_cryptoconnection(uint32_t num)
|
||||
{
|
||||
Crypto_Connection *newcrypto_connections = realloc(crypto_connections, num * sizeof(Crypto_Connection));
|
||||
|
||||
if (newcrypto_connections == NULL)
|
||||
return -1;
|
||||
|
||||
crypto_connections = newcrypto_connections;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Start a secure connection with other peer who has public_key and ip_port
|
||||
returns -1 if failure
|
||||
returns crypt_connection_id of the initialized connection if everything went well. */
|
||||
|
@ -415,7 +430,13 @@ int crypto_connect(uint8_t *public_key, IP_Port ip_port)
|
|||
return -1;
|
||||
}
|
||||
|
||||
for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) {
|
||||
if (realloc_cryptoconnection(crypto_connections_length + 1) == -1)
|
||||
return -1;
|
||||
|
||||
memset(&crypto_connections[crypto_connections_length], 0, sizeof(Crypto_Connection));
|
||||
crypto_connections[crypto_connections_length].number = ~0;
|
||||
|
||||
for (i = 0; i <= MAX_CRYPTO_CONNECTIONS; ++i) {
|
||||
if (crypto_connections[i].status == CONN_NO_CONNECTION) {
|
||||
int id = new_connection(ip_port);
|
||||
|
||||
|
@ -428,6 +449,9 @@ int crypto_connect(uint8_t *public_key, IP_Port ip_port)
|
|||
memcpy(crypto_connections[i].public_key, public_key, crypto_box_PUBLICKEYBYTES);
|
||||
crypto_box_keypair(crypto_connections[i].sessionpublic_key, crypto_connections[i].sessionsecret_key);
|
||||
|
||||
if (crypto_connections_length == i)
|
||||
++crypto_connections_length;
|
||||
|
||||
if (send_cryptohandshake(id, public_key, crypto_connections[i].recv_nonce,
|
||||
crypto_connections[i].sessionpublic_key) == 1) {
|
||||
increment_nonce(crypto_connections[i].recv_nonce);
|
||||
|
@ -489,6 +513,15 @@ int crypto_kill(int crypt_connection_id)
|
|||
kill_connection(crypto_connections[crypt_connection_id].number);
|
||||
memset(&crypto_connections[crypt_connection_id], 0 , sizeof(Crypto_Connection));
|
||||
crypto_connections[crypt_connection_id].number = ~0;
|
||||
uint32_t i;
|
||||
|
||||
for (i = crypto_connections_length; i != 0; --i) {
|
||||
if (crypto_connections[i - 1].status != CONN_NO_CONNECTION)
|
||||
break;
|
||||
}
|
||||
|
||||
crypto_connections_length = i;
|
||||
realloc_cryptoconnection(crypto_connections_length);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -510,7 +543,13 @@ int accept_crypto_inbound(int connection_id, uint8_t *public_key, uint8_t *secre
|
|||
{
|
||||
return -1;
|
||||
}*/
|
||||
for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) {
|
||||
if (realloc_cryptoconnection(crypto_connections_length + 1) == -1)
|
||||
return -1;
|
||||
|
||||
memset(&crypto_connections[crypto_connections_length], 0, sizeof(Crypto_Connection));
|
||||
crypto_connections[crypto_connections_length].number = ~0;
|
||||
|
||||
for (i = 0; i <= MAX_CRYPTO_CONNECTIONS; ++i) {
|
||||
if (crypto_connections[i].status == CONN_NO_CONNECTION) {
|
||||
crypto_connections[i].number = connection_id;
|
||||
crypto_connections[i].status = CONN_NOT_CONFIRMED;
|
||||
|
@ -522,6 +561,9 @@ int accept_crypto_inbound(int connection_id, uint8_t *public_key, uint8_t *secre
|
|||
|
||||
crypto_box_keypair(crypto_connections[i].sessionpublic_key, crypto_connections[i].sessionsecret_key);
|
||||
|
||||
if (crypto_connections_length == i)
|
||||
++crypto_connections_length;
|
||||
|
||||
if (send_cryptohandshake(connection_id, public_key, crypto_connections[i].recv_nonce,
|
||||
crypto_connections[i].sessionpublic_key) == 1) {
|
||||
increment_nonce(crypto_connections[i].recv_nonce);
|
||||
|
@ -680,13 +722,8 @@ static void receive_crypto(void)
|
|||
sets all the global connection variables to their default values. */
|
||||
void initNetCrypto(void)
|
||||
{
|
||||
memset(crypto_connections, 0 , sizeof(crypto_connections));
|
||||
memset(incoming_connections, -1 , sizeof(incoming_connections));
|
||||
networking_registerhandler(32, &cryptopacket_handle);
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i)
|
||||
crypto_connections[i].number = ~0;
|
||||
}
|
||||
|
||||
static void killTimedout(void)
|
||||
|
|
Loading…
Reference in New Issue
Block a user