diff options
-rw-r--r-- | core/Messenger.c | 32 | ||||
-rw-r--r-- | core/net_crypto.c | 55 |
2 files changed, 66 insertions, 21 deletions
diff --git a/core/Messenger.c b/core/Messenger.c index 04e4626a..2c9e134b 100644 --- a/core/Messenger.c +++ b/core/Messenger.c | |||
@@ -42,7 +42,6 @@ int realloc_friendlist(Messenger *m, uint32_t num) | |||
42 | if (newfriendlist == NULL) | 42 | if (newfriendlist == NULL) |
43 | return -1; | 43 | return -1; |
44 | 44 | ||
45 | memset(&newfriendlist[num - 1], 0, sizeof(Friend)); | ||
46 | m->friendlist = newfriendlist; | 45 | m->friendlist = newfriendlist; |
47 | return 0; | 46 | return 0; |
48 | } | 47 | } |
@@ -165,6 +164,8 @@ int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length) | |||
165 | if (realloc_friendlist(m, m->numfriends + 1) != 0) | 164 | if (realloc_friendlist(m, m->numfriends + 1) != 0) |
166 | return FAERR_NOMEM; | 165 | return FAERR_NOMEM; |
167 | 166 | ||
167 | memset(&(m->friendlist[m->numfriends]), 0, sizeof(Friend)); | ||
168 | |||
168 | uint32_t i; | 169 | uint32_t i; |
169 | 170 | ||
170 | for (i = 0; i <= m->numfriends; ++i) { | 171 | 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) | |||
184 | m->friendlist[i].receives_read_receipts = 1; /* default: YES */ | 185 | m->friendlist[i].receives_read_receipts = 1; /* default: YES */ |
185 | memcpy(&(m->friendlist[i].friendrequest_nospam), address + crypto_box_PUBLICKEYBYTES, sizeof(uint32_t)); | 186 | memcpy(&(m->friendlist[i].friendrequest_nospam), address + crypto_box_PUBLICKEYBYTES, sizeof(uint32_t)); |
186 | 187 | ||
187 | ++ m->numfriends; | 188 | if (m->numfriends == i) |
189 | ++ m->numfriends; | ||
190 | |||
188 | return i; | 191 | return i; |
189 | } | 192 | } |
190 | } | 193 | } |
@@ -201,6 +204,8 @@ int m_addfriend_norequest(Messenger *m, uint8_t *client_id) | |||
201 | if (realloc_friendlist(m, m->numfriends + 1) != 0) | 204 | if (realloc_friendlist(m, m->numfriends + 1) != 0) |
202 | return FAERR_NOMEM; | 205 | return FAERR_NOMEM; |
203 | 206 | ||
207 | memset(&(m->friendlist[m->numfriends]), 0, sizeof(Friend)); | ||
208 | |||
204 | uint32_t i; | 209 | uint32_t i; |
205 | 210 | ||
206 | for (i = 0; i <= m->numfriends; ++i) { | 211 | for (i = 0; i <= m->numfriends; ++i) { |
@@ -215,7 +220,10 @@ int m_addfriend_norequest(Messenger *m, uint8_t *client_id) | |||
215 | m->friendlist[i].userstatus = USERSTATUS_NONE; | 220 | m->friendlist[i].userstatus = USERSTATUS_NONE; |
216 | m->friendlist[i].message_id = 0; | 221 | m->friendlist[i].message_id = 0; |
217 | m->friendlist[i].receives_read_receipts = 1; /* default: YES */ | 222 | m->friendlist[i].receives_read_receipts = 1; /* default: YES */ |
218 | ++ m->numfriends; | 223 | |
224 | if (m->numfriends == i) | ||
225 | ++ m->numfriends; | ||
226 | |||
219 | return i; | 227 | return i; |
220 | } | 228 | } |
221 | } | 229 | } |
@@ -244,7 +252,7 @@ int m_delfriend(Messenger *m, int friendnumber) | |||
244 | 252 | ||
245 | m->numfriends = i; | 253 | m->numfriends = i; |
246 | 254 | ||
247 | if (realloc_friendlist(m, m->numfriends + 1) != 0) | 255 | if (realloc_friendlist(m, m->numfriends) != 0) |
248 | return FAERR_NOMEM; | 256 | return FAERR_NOMEM; |
249 | 257 | ||
250 | return 0; | 258 | return 0; |
@@ -851,14 +859,14 @@ void doMessenger(Messenger *m) | |||
851 | uint32_t Messenger_size(Messenger *m) | 859 | uint32_t Messenger_size(Messenger *m) |
852 | { | 860 | { |
853 | return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES | 861 | return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES |
854 | + sizeof(uint32_t) // nospam | 862 | + sizeof(uint32_t) // nospam |
855 | + sizeof(uint32_t) // DHT size | 863 | + sizeof(uint32_t) // DHT size |
856 | + DHT_size() // DHT itself | 864 | + DHT_size() // DHT itself |
857 | + sizeof(uint32_t) // Friendlist size | 865 | + sizeof(uint32_t) // Friendlist size |
858 | + sizeof(Friend) * m->numfriends // Friendlist itself | 866 | + sizeof(Friend) * m->numfriends // Friendlist itself |
859 | + sizeof(uint16_t) // Own nickname length | 867 | + sizeof(uint16_t) // Own nickname length |
860 | + m->name_length // Own nickname | 868 | + m->name_length // Own nickname |
861 | ; | 869 | ; |
862 | } | 870 | } |
863 | 871 | ||
864 | /* save the messenger in data of size Messenger_size() */ | 872 | /* save the messenger in data of size Messenger_size() */ |
diff --git a/core/net_crypto.c b/core/net_crypto.c index 4b7fa043..2dbe12ad 100644 --- a/core/net_crypto.c +++ b/core/net_crypto.c | |||
@@ -45,9 +45,11 @@ typedef struct { | |||
45 | 45 | ||
46 | } Crypto_Connection; | 46 | } Crypto_Connection; |
47 | 47 | ||
48 | #define MAX_CRYPTO_CONNECTIONS 256 | 48 | static Crypto_Connection *crypto_connections; |
49 | 49 | ||
50 | static Crypto_Connection crypto_connections[MAX_CRYPTO_CONNECTIONS]; | 50 | static uint32_t crypto_connections_length; /* Length of connections array */ |
51 | |||
52 | #define MAX_CRYPTO_CONNECTIONS crypto_connections_length | ||
51 | 53 | ||
52 | #define CONN_NO_CONNECTION 0 | 54 | #define CONN_NO_CONNECTION 0 |
53 | #define CONN_HANDSHAKE_SENT 1 | 55 | #define CONN_HANDSHAKE_SENT 1 |
@@ -400,6 +402,19 @@ static int getcryptconnection_id(uint8_t *public_key) | |||
400 | return -1; | 402 | return -1; |
401 | } | 403 | } |
402 | 404 | ||
405 | /* set the size of the friend list to numfriends | ||
406 | return -1 if realloc fails */ | ||
407 | int realloc_cryptoconnection(uint32_t num) | ||
408 | { | ||
409 | Crypto_Connection *newcrypto_connections = realloc(crypto_connections, num * sizeof(Crypto_Connection)); | ||
410 | |||
411 | if (newcrypto_connections == NULL) | ||
412 | return -1; | ||
413 | |||
414 | crypto_connections = newcrypto_connections; | ||
415 | return 0; | ||
416 | } | ||
417 | |||
403 | /* Start a secure connection with other peer who has public_key and ip_port | 418 | /* Start a secure connection with other peer who has public_key and ip_port |
404 | returns -1 if failure | 419 | returns -1 if failure |
405 | returns crypt_connection_id of the initialized connection if everything went well. */ | 420 | 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) | |||
415 | return -1; | 430 | return -1; |
416 | } | 431 | } |
417 | 432 | ||
418 | for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) { | 433 | if (realloc_cryptoconnection(crypto_connections_length + 1) == -1) |
434 | return -1; | ||
435 | |||
436 | memset(&crypto_connections[crypto_connections_length], 0, sizeof(Crypto_Connection)); | ||
437 | crypto_connections[crypto_connections_length].number = ~0; | ||
438 | |||
439 | for (i = 0; i <= MAX_CRYPTO_CONNECTIONS; ++i) { | ||
419 | if (crypto_connections[i].status == CONN_NO_CONNECTION) { | 440 | if (crypto_connections[i].status == CONN_NO_CONNECTION) { |
420 | int id = new_connection(ip_port); | 441 | int id = new_connection(ip_port); |
421 | 442 | ||
@@ -428,6 +449,9 @@ int crypto_connect(uint8_t *public_key, IP_Port ip_port) | |||
428 | memcpy(crypto_connections[i].public_key, public_key, crypto_box_PUBLICKEYBYTES); | 449 | memcpy(crypto_connections[i].public_key, public_key, crypto_box_PUBLICKEYBYTES); |
429 | crypto_box_keypair(crypto_connections[i].sessionpublic_key, crypto_connections[i].sessionsecret_key); | 450 | crypto_box_keypair(crypto_connections[i].sessionpublic_key, crypto_connections[i].sessionsecret_key); |
430 | 451 | ||
452 | if (crypto_connections_length == i) | ||
453 | ++crypto_connections_length; | ||
454 | |||
431 | if (send_cryptohandshake(id, public_key, crypto_connections[i].recv_nonce, | 455 | if (send_cryptohandshake(id, public_key, crypto_connections[i].recv_nonce, |
432 | crypto_connections[i].sessionpublic_key) == 1) { | 456 | crypto_connections[i].sessionpublic_key) == 1) { |
433 | increment_nonce(crypto_connections[i].recv_nonce); | 457 | increment_nonce(crypto_connections[i].recv_nonce); |
@@ -489,6 +513,15 @@ int crypto_kill(int crypt_connection_id) | |||
489 | kill_connection(crypto_connections[crypt_connection_id].number); | 513 | kill_connection(crypto_connections[crypt_connection_id].number); |
490 | memset(&crypto_connections[crypt_connection_id], 0 , sizeof(Crypto_Connection)); | 514 | memset(&crypto_connections[crypt_connection_id], 0 , sizeof(Crypto_Connection)); |
491 | crypto_connections[crypt_connection_id].number = ~0; | 515 | crypto_connections[crypt_connection_id].number = ~0; |
516 | uint32_t i; | ||
517 | |||
518 | for (i = crypto_connections_length; i != 0; --i) { | ||
519 | if (crypto_connections[i - 1].status != CONN_NO_CONNECTION) | ||
520 | break; | ||
521 | } | ||
522 | |||
523 | crypto_connections_length = i; | ||
524 | realloc_cryptoconnection(crypto_connections_length); | ||
492 | return 0; | 525 | return 0; |
493 | } | 526 | } |
494 | 527 | ||
@@ -510,7 +543,13 @@ int accept_crypto_inbound(int connection_id, uint8_t *public_key, uint8_t *secre | |||
510 | { | 543 | { |
511 | return -1; | 544 | return -1; |
512 | }*/ | 545 | }*/ |
513 | for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) { | 546 | if (realloc_cryptoconnection(crypto_connections_length + 1) == -1) |
547 | return -1; | ||
548 | |||
549 | memset(&crypto_connections[crypto_connections_length], 0, sizeof(Crypto_Connection)); | ||
550 | crypto_connections[crypto_connections_length].number = ~0; | ||
551 | |||
552 | for (i = 0; i <= MAX_CRYPTO_CONNECTIONS; ++i) { | ||
514 | if (crypto_connections[i].status == CONN_NO_CONNECTION) { | 553 | if (crypto_connections[i].status == CONN_NO_CONNECTION) { |
515 | crypto_connections[i].number = connection_id; | 554 | crypto_connections[i].number = connection_id; |
516 | crypto_connections[i].status = CONN_NOT_CONFIRMED; | 555 | 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 | |||
522 | 561 | ||
523 | crypto_box_keypair(crypto_connections[i].sessionpublic_key, crypto_connections[i].sessionsecret_key); | 562 | crypto_box_keypair(crypto_connections[i].sessionpublic_key, crypto_connections[i].sessionsecret_key); |
524 | 563 | ||
564 | if (crypto_connections_length == i) | ||
565 | ++crypto_connections_length; | ||
566 | |||
525 | if (send_cryptohandshake(connection_id, public_key, crypto_connections[i].recv_nonce, | 567 | if (send_cryptohandshake(connection_id, public_key, crypto_connections[i].recv_nonce, |
526 | crypto_connections[i].sessionpublic_key) == 1) { | 568 | crypto_connections[i].sessionpublic_key) == 1) { |
527 | increment_nonce(crypto_connections[i].recv_nonce); | 569 | increment_nonce(crypto_connections[i].recv_nonce); |
@@ -680,13 +722,8 @@ static void receive_crypto(void) | |||
680 | sets all the global connection variables to their default values. */ | 722 | sets all the global connection variables to their default values. */ |
681 | void initNetCrypto(void) | 723 | void initNetCrypto(void) |
682 | { | 724 | { |
683 | memset(crypto_connections, 0 , sizeof(crypto_connections)); | ||
684 | memset(incoming_connections, -1 , sizeof(incoming_connections)); | 725 | memset(incoming_connections, -1 , sizeof(incoming_connections)); |
685 | networking_registerhandler(32, &cryptopacket_handle); | 726 | networking_registerhandler(32, &cryptopacket_handle); |
686 | uint32_t i; | ||
687 | |||
688 | for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) | ||
689 | crypto_connections[i].number = ~0; | ||
690 | } | 727 | } |
691 | 728 | ||
692 | static void killTimedout(void) | 729 | static void killTimedout(void) |