summaryrefslogtreecommitdiff
path: root/core/net_crypto.c
diff options
context:
space:
mode:
Diffstat (limited to 'core/net_crypto.c')
-rw-r--r--core/net_crypto.c55
1 files changed, 46 insertions, 9 deletions
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 48static Crypto_Connection *crypto_connections;
49 49
50static Crypto_Connection crypto_connections[MAX_CRYPTO_CONNECTIONS]; 50static 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 */
407int 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. */
681void initNetCrypto(void) 723void 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
692static void killTimedout(void) 729static void killTimedout(void)