diff options
Diffstat (limited to 'toxcore/net_crypto.c')
-rw-r--r-- | toxcore/net_crypto.c | 69 |
1 files changed, 17 insertions, 52 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 41c8c45c..2da2ba3b 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c | |||
@@ -503,27 +503,28 @@ int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port) | |||
503 | */ | 503 | */ |
504 | int crypto_inbound(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key) | 504 | int crypto_inbound(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key) |
505 | { | 505 | { |
506 | uint32_t i; | 506 | uint32_t i, j; |
507 | |||
508 | while (1) { | ||
509 | int incoming_con = incoming_connection(c->lossless_udp, 1); | ||
507 | 510 | ||
508 | for (i = 0; i < MAX_INCOMING; ++i) { | 511 | if (incoming_con != -1) { |
509 | if (c->incoming_connections[i] != -1) { | 512 | if (is_connected(c->lossless_udp, incoming_con) == 4 |
510 | if (is_connected(c->lossless_udp, c->incoming_connections[i]) == 4 | 513 | || is_connected(c->lossless_udp, incoming_con) == 0) { |
511 | || is_connected(c->lossless_udp, c->incoming_connections[i]) == 0) { | 514 | kill_connection(c->lossless_udp, incoming_con); |
512 | kill_connection(c->lossless_udp, c->incoming_connections[i]); | ||
513 | c->incoming_connections[i] = -1; | ||
514 | continue; | 515 | continue; |
515 | } | 516 | } |
516 | 517 | ||
517 | if (id_packet(c->lossless_udp, c->incoming_connections[i]) == 2) { | 518 | if (id_packet(c->lossless_udp, incoming_con) == 2) { |
518 | uint8_t temp_data[MAX_DATA_SIZE]; | 519 | uint8_t temp_data[MAX_DATA_SIZE]; |
519 | uint16_t len = read_packet_silent(c->lossless_udp, c->incoming_connections[i], temp_data); | 520 | uint16_t len = read_packet_silent(c->lossless_udp, incoming_con, temp_data); |
520 | 521 | ||
521 | if (handle_cryptohandshake(c, public_key, secret_nonce, session_key, temp_data, len)) { | 522 | if (handle_cryptohandshake(c, public_key, secret_nonce, session_key, temp_data, len)) { |
522 | int connection_id = c->incoming_connections[i]; | 523 | return incoming_con; |
523 | c->incoming_connections[i] = -1; /* Remove this connection from the incoming connection list. */ | ||
524 | return connection_id; | ||
525 | } | 524 | } |
526 | } | 525 | } |
526 | } else { | ||
527 | break; | ||
527 | } | 528 | } |
528 | } | 529 | } |
529 | 530 | ||
@@ -579,6 +580,10 @@ int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, | |||
579 | * return -1; | 580 | * return -1; |
580 | * } | 581 | * } |
581 | */ | 582 | */ |
583 | |||
584 | /* Connection is accepted. */ | ||
585 | confirm_connection(c->lossless_udp, connection_id); | ||
586 | |||
582 | if (realloc_cryptoconnection(c, c->crypto_connections_length + 1) == -1 | 587 | if (realloc_cryptoconnection(c, c->crypto_connections_length + 1) == -1 |
583 | || c->crypto_connections == NULL) | 588 | || c->crypto_connections == NULL) |
584 | return -1; | 589 | return -1; |
@@ -659,41 +664,6 @@ void load_keys(Net_Crypto *c, uint8_t *keys) | |||
659 | memcpy(c->self_secret_key, keys + crypto_box_PUBLICKEYBYTES, crypto_box_SECRETKEYBYTES); | 664 | memcpy(c->self_secret_key, keys + crypto_box_PUBLICKEYBYTES, crypto_box_SECRETKEYBYTES); |
660 | } | 665 | } |
661 | 666 | ||
662 | /* Adds an incoming connection to the incoming_connection list. | ||
663 | * TODO: Optimize this. | ||
664 | * | ||
665 | * returns 0 if successful | ||
666 | * returns 1 if failure. | ||
667 | */ | ||
668 | static int new_incoming(Net_Crypto *c, int id) | ||
669 | { | ||
670 | uint32_t i; | ||
671 | |||
672 | for (i = 0; i < MAX_INCOMING; ++i) { | ||
673 | if (c->incoming_connections[i] == -1) { | ||
674 | c->incoming_connections[i] = id; | ||
675 | return 0; | ||
676 | } | ||
677 | } | ||
678 | |||
679 | return 1; | ||
680 | } | ||
681 | |||
682 | /* Handle all new incoming connections. | ||
683 | * TODO: Optimize this. | ||
684 | */ | ||
685 | static void handle_incomings(Net_Crypto *c) | ||
686 | { | ||
687 | int income; | ||
688 | |||
689 | while (1) { | ||
690 | income = incoming_connection(c->lossless_udp); | ||
691 | |||
692 | if (income == -1 || new_incoming(c, income) ) | ||
693 | break; | ||
694 | } | ||
695 | } | ||
696 | |||
697 | /* Handle received packets for not yet established crypto connections. */ | 667 | /* Handle received packets for not yet established crypto connections. */ |
698 | static void receive_crypto(Net_Crypto *c) | 668 | static void receive_crypto(Net_Crypto *c) |
699 | { | 669 | { |
@@ -748,9 +718,6 @@ static void receive_crypto(Net_Crypto *c) | |||
748 | c->crypto_connections[i].sessionsecret_key, | 718 | c->crypto_connections[i].sessionsecret_key, |
749 | c->crypto_connections[i].shared_key); | 719 | c->crypto_connections[i].shared_key); |
750 | c->crypto_connections[i].status = CONN_ESTABLISHED; | 720 | c->crypto_connections[i].status = CONN_ESTABLISHED; |
751 | |||
752 | /* Connection is accepted. */ | ||
753 | confirm_connection(c->lossless_udp, c->crypto_connections[i].number); | ||
754 | } else { | 721 | } else { |
755 | /* This should not happen, kill the connection if it does. */ | 722 | /* This should not happen, kill the connection if it does. */ |
756 | crypto_kill(c, i); | 723 | crypto_kill(c, i); |
@@ -785,7 +752,6 @@ Net_Crypto *new_net_crypto(Networking_Core *net) | |||
785 | return NULL; | 752 | return NULL; |
786 | } | 753 | } |
787 | 754 | ||
788 | memset(temp->incoming_connections, -1 , sizeof(int) * MAX_INCOMING); | ||
789 | return temp; | 755 | return temp; |
790 | } | 756 | } |
791 | 757 | ||
@@ -810,7 +776,6 @@ static void kill_timedout(Net_Crypto *c) | |||
810 | void do_net_crypto(Net_Crypto *c) | 776 | void do_net_crypto(Net_Crypto *c) |
811 | { | 777 | { |
812 | do_lossless_udp(c->lossless_udp); | 778 | do_lossless_udp(c->lossless_udp); |
813 | handle_incomings(c); | ||
814 | kill_timedout(c); | 779 | kill_timedout(c); |
815 | receive_crypto(c); | 780 | receive_crypto(c); |
816 | } | 781 | } |