diff options
author | Coren[m] <Break@Ocean> | 2013-10-24 22:47:23 +0200 |
---|---|---|
committer | Coren[m] <Break@Ocean> | 2013-10-24 22:47:23 +0200 |
commit | 065495cd7c269389af7f834e568d12105589dd97 (patch) | |
tree | c38eb6cfd8117b9a721b5d41144502736ed18bac /toxcore/net_crypto.c | |
parent | 0a4c3d7e2e080dafd66d25f7a5806b89f7be1bcf (diff) | |
parent | f3be5609049d278ee3f46754ea4b0eb929876dba (diff) |
Merge remote-tracking branch 'upstream/master' into cleanup_unix_time_id_eq_cpy_is_timeout
Conflicts:
toxcore/net_crypto.c
Diffstat (limited to 'toxcore/net_crypto.c')
-rw-r--r-- | toxcore/net_crypto.c | 64 |
1 files changed, 29 insertions, 35 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 0a7ff665..7ae7c502 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c | |||
@@ -31,12 +31,6 @@ | |||
31 | #include "net_crypto.h" | 31 | #include "net_crypto.h" |
32 | #include "util.h" | 32 | #include "util.h" |
33 | 33 | ||
34 | #define CONN_NO_CONNECTION 0 | ||
35 | #define CONN_HANDSHAKE_SENT 1 | ||
36 | #define CONN_NOT_CONFIRMED 2 | ||
37 | #define CONN_ESTABLISHED 3 | ||
38 | #define CONN_TIMED_OUT 4 | ||
39 | |||
40 | static uint8_t crypt_connection_id_not_valid(Net_Crypto *c, int crypt_connection_id) | 34 | static uint8_t crypt_connection_id_not_valid(Net_Crypto *c, int crypt_connection_id) |
41 | { | 35 | { |
42 | return (uint32_t)crypt_connection_id >= c->crypto_connections_length; | 36 | return (uint32_t)crypt_connection_id >= c->crypto_connections_length; |
@@ -153,7 +147,7 @@ void random_nonce(uint8_t *nonce) | |||
153 | static uint8_t base_nonce[crypto_box_NONCEBYTES]; | 147 | static uint8_t base_nonce[crypto_box_NONCEBYTES]; |
154 | static uint8_t nonce_set = 0; | 148 | static uint8_t nonce_set = 0; |
155 | 149 | ||
156 | /*Gives a nonce guaranteed to be different from previous ones.*/ | 150 | /* Gives a nonce guaranteed to be different from previous ones.*/ |
157 | void new_nonce(uint8_t *nonce) | 151 | void new_nonce(uint8_t *nonce) |
158 | { | 152 | { |
159 | if (nonce_set == 0) { | 153 | if (nonce_set == 0) { |
@@ -175,7 +169,7 @@ int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data) | |||
175 | if (crypt_connection_id_not_valid(c, crypt_connection_id)) | 169 | if (crypt_connection_id_not_valid(c, crypt_connection_id)) |
176 | return 0; | 170 | return 0; |
177 | 171 | ||
178 | if (c->crypto_connections[crypt_connection_id].status != CONN_ESTABLISHED) | 172 | if (c->crypto_connections[crypt_connection_id].status != CRYPTO_CONN_ESTABLISHED) |
179 | return 0; | 173 | return 0; |
180 | 174 | ||
181 | uint8_t temp_data[MAX_DATA_SIZE]; | 175 | uint8_t temp_data[MAX_DATA_SIZE]; |
@@ -221,7 +215,7 @@ int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uin | |||
221 | if (length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE - 1) | 215 | if (length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE - 1) |
222 | return 0; | 216 | return 0; |
223 | 217 | ||
224 | if (c->crypto_connections[crypt_connection_id].status != CONN_ESTABLISHED) | 218 | if (c->crypto_connections[crypt_connection_id].status != CRYPTO_CONN_ESTABLISHED) |
225 | return 0; | 219 | return 0; |
226 | 220 | ||
227 | uint8_t temp_data[MAX_DATA_SIZE]; | 221 | uint8_t temp_data[MAX_DATA_SIZE]; |
@@ -241,7 +235,7 @@ int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uin | |||
241 | return 1; | 235 | return 1; |
242 | } | 236 | } |
243 | 237 | ||
244 | /* Ceate a request to peer. | 238 | /* Create a request to peer. |
245 | * send_public_key and send_secret_key are the pub/secret keys of the sender. | 239 | * send_public_key and send_secret_key are the pub/secret keys of the sender. |
246 | * recv_public_key is public key of reciever. | 240 | * recv_public_key is public key of reciever. |
247 | * packet must be an array of MAX_DATA_SIZE big. | 241 | * packet must be an array of MAX_DATA_SIZE big. |
@@ -421,7 +415,7 @@ static int getcryptconnection_id(Net_Crypto *c, uint8_t *public_key) | |||
421 | uint32_t i; | 415 | uint32_t i; |
422 | 416 | ||
423 | for (i = 0; i < c->crypto_connections_length; ++i) | 417 | for (i = 0; i < c->crypto_connections_length; ++i) |
424 | if (c->crypto_connections[i].status != CONN_NO_CONNECTION) | 418 | if (c->crypto_connections[i].status != CRYPTO_CONN_NO_CONNECTION) |
425 | if (id_equal(public_key, c->crypto_connections[i].public_key)) | 419 | if (id_equal(public_key, c->crypto_connections[i].public_key)) |
426 | return i; | 420 | return i; |
427 | 421 | ||
@@ -474,14 +468,14 @@ int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port) | |||
474 | c->crypto_connections[c->crypto_connections_length].number = ~0; | 468 | c->crypto_connections[c->crypto_connections_length].number = ~0; |
475 | 469 | ||
476 | for (i = 0; i <= c->crypto_connections_length; ++i) { | 470 | for (i = 0; i <= c->crypto_connections_length; ++i) { |
477 | if (c->crypto_connections[i].status == CONN_NO_CONNECTION) { | 471 | if (c->crypto_connections[i].status == CRYPTO_CONN_NO_CONNECTION) { |
478 | int id_new = new_connection(c->lossless_udp, ip_port); | 472 | int id_new = new_connection(c->lossless_udp, ip_port); |
479 | 473 | ||
480 | if (id_new == -1) | 474 | if (id_new == -1) |
481 | return -1; | 475 | return -1; |
482 | 476 | ||
483 | c->crypto_connections[i].number = id_new; | 477 | c->crypto_connections[i].number = id_new; |
484 | c->crypto_connections[i].status = CONN_HANDSHAKE_SENT; | 478 | c->crypto_connections[i].status = CRYPTO_CONN_HANDSHAKE_SENT; |
485 | random_nonce(c->crypto_connections[i].recv_nonce); | 479 | random_nonce(c->crypto_connections[i].recv_nonce); |
486 | id_copy(c->crypto_connections[i].public_key, public_key); | 480 | id_copy(c->crypto_connections[i].public_key, public_key); |
487 | crypto_box_keypair(c->crypto_connections[i].sessionpublic_key, c->crypto_connections[i].sessionsecret_key); | 481 | crypto_box_keypair(c->crypto_connections[i].sessionpublic_key, c->crypto_connections[i].sessionsecret_key); |
@@ -550,15 +544,15 @@ int crypto_kill(Net_Crypto *c, int crypt_connection_id) | |||
550 | if (crypt_connection_id_not_valid(c, crypt_connection_id)) | 544 | if (crypt_connection_id_not_valid(c, crypt_connection_id)) |
551 | return 1; | 545 | return 1; |
552 | 546 | ||
553 | if (c->crypto_connections[crypt_connection_id].status != CONN_NO_CONNECTION) { | 547 | if (c->crypto_connections[crypt_connection_id].status != CRYPTO_CONN_NO_CONNECTION) { |
554 | c->crypto_connections[crypt_connection_id].status = CONN_NO_CONNECTION; | 548 | c->crypto_connections[crypt_connection_id].status = CRYPTO_CONN_NO_CONNECTION; |
555 | kill_connection(c->lossless_udp, c->crypto_connections[crypt_connection_id].number); | 549 | kill_connection(c->lossless_udp, c->crypto_connections[crypt_connection_id].number); |
556 | memset(&(c->crypto_connections[crypt_connection_id]), 0 , sizeof(Crypto_Connection)); | 550 | memset(&(c->crypto_connections[crypt_connection_id]), 0 , sizeof(Crypto_Connection)); |
557 | c->crypto_connections[crypt_connection_id].number = ~0; | 551 | c->crypto_connections[crypt_connection_id].number = ~0; |
558 | uint32_t i; | 552 | uint32_t i; |
559 | 553 | ||
560 | for (i = c->crypto_connections_length; i != 0; --i) { | 554 | for (i = c->crypto_connections_length; i != 0; --i) { |
561 | if (c->crypto_connections[i - 1].status != CONN_NO_CONNECTION) | 555 | if (c->crypto_connections[i - 1].status != CRYPTO_CONN_NO_CONNECTION) |
562 | break; | 556 | break; |
563 | } | 557 | } |
564 | 558 | ||
@@ -598,9 +592,9 @@ int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, | |||
598 | c->crypto_connections[c->crypto_connections_length].number = ~0; | 592 | c->crypto_connections[c->crypto_connections_length].number = ~0; |
599 | 593 | ||
600 | for (i = 0; i <= c->crypto_connections_length; ++i) { | 594 | for (i = 0; i <= c->crypto_connections_length; ++i) { |
601 | if (c->crypto_connections[i].status == CONN_NO_CONNECTION) { | 595 | if (c->crypto_connections[i].status == CRYPTO_CONN_NO_CONNECTION) { |
602 | c->crypto_connections[i].number = connection_id; | 596 | c->crypto_connections[i].number = connection_id; |
603 | c->crypto_connections[i].status = CONN_NOT_CONFIRMED; | 597 | c->crypto_connections[i].status = CRYPTO_CONN_NOT_CONFIRMED; |
604 | c->crypto_connections[i].timeout = unix_time() + CRYPTO_HANDSHAKE_TIMEOUT; | 598 | c->crypto_connections[i].timeout = unix_time() + CRYPTO_HANDSHAKE_TIMEOUT; |
605 | random_nonce(c->crypto_connections[i].recv_nonce); | 599 | random_nonce(c->crypto_connections[i].recv_nonce); |
606 | memcpy(c->crypto_connections[i].sent_nonce, secret_nonce, crypto_box_NONCEBYTES); | 600 | memcpy(c->crypto_connections[i].sent_nonce, secret_nonce, crypto_box_NONCEBYTES); |
@@ -621,9 +615,9 @@ int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, | |||
621 | c->crypto_connections[i].sessionsecret_key, | 615 | c->crypto_connections[i].sessionsecret_key, |
622 | c->crypto_connections[i].shared_key); | 616 | c->crypto_connections[i].shared_key); |
623 | c->crypto_connections[i].status = | 617 | c->crypto_connections[i].status = |
624 | CONN_ESTABLISHED; /* Connection status needs to be 3 for write_cryptpacket() to work. */ | 618 | CRYPTO_CONN_ESTABLISHED; /* Connection status needs to be 3 for write_cryptpacket() to work. */ |
625 | write_cryptpacket(c, i, ((uint8_t *)&zero), sizeof(zero)); | 619 | write_cryptpacket(c, i, ((uint8_t *)&zero), sizeof(zero)); |
626 | c->crypto_connections[i].status = CONN_NOT_CONFIRMED; /* Set it to its proper value right after. */ | 620 | c->crypto_connections[i].status = CRYPTO_CONN_NOT_CONFIRMED; /* Set it to its proper value right after. */ |
627 | return i; | 621 | return i; |
628 | } | 622 | } |
629 | 623 | ||
@@ -645,7 +639,7 @@ int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id) | |||
645 | if ((unsigned int)crypt_connection_id < c->crypto_connections_length) | 639 | if ((unsigned int)crypt_connection_id < c->crypto_connections_length) |
646 | return c->crypto_connections[crypt_connection_id].status; | 640 | return c->crypto_connections[crypt_connection_id].status; |
647 | 641 | ||
648 | return CONN_NO_CONNECTION; | 642 | return CRYPTO_CONN_NO_CONNECTION; |
649 | } | 643 | } |
650 | 644 | ||
651 | void new_keys(Net_Crypto *c) | 645 | void new_keys(Net_Crypto *c) |
@@ -678,10 +672,10 @@ static void receive_crypto(Net_Crypto *c) | |||
678 | uint64_t temp_time = unix_time(); | 672 | uint64_t temp_time = unix_time(); |
679 | 673 | ||
680 | for (i = 0; i < c->crypto_connections_length; ++i) { | 674 | for (i = 0; i < c->crypto_connections_length; ++i) { |
681 | if (c->crypto_connections[i].status == CONN_NO_CONNECTION) | 675 | if (c->crypto_connections[i].status == CRYPTO_CONN_NO_CONNECTION) |
682 | continue; | 676 | continue; |
683 | 677 | ||
684 | if (c->crypto_connections[i].status == CONN_HANDSHAKE_SENT) { | 678 | if (c->crypto_connections[i].status == CRYPTO_CONN_HANDSHAKE_SENT) { |
685 | uint8_t temp_data[MAX_DATA_SIZE]; | 679 | uint8_t temp_data[MAX_DATA_SIZE]; |
686 | uint8_t secret_nonce[crypto_box_NONCEBYTES]; | 680 | uint8_t secret_nonce[crypto_box_NONCEBYTES]; |
687 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | 681 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; |
@@ -701,25 +695,25 @@ static void receive_crypto(Net_Crypto *c) | |||
701 | c->crypto_connections[i].sessionsecret_key, | 695 | c->crypto_connections[i].sessionsecret_key, |
702 | c->crypto_connections[i].shared_key); | 696 | c->crypto_connections[i].shared_key); |
703 | c->crypto_connections[i].status = | 697 | c->crypto_connections[i].status = |
704 | CONN_ESTABLISHED; /* Connection status needs to be 3 for write_cryptpacket() to work. */ | 698 | CRYPTO_CONN_ESTABLISHED; /* Connection status needs to be 3 for write_cryptpacket() to work. */ |
705 | write_cryptpacket(c, i, ((uint8_t *)&zero), sizeof(zero)); | 699 | write_cryptpacket(c, i, ((uint8_t *)&zero), sizeof(zero)); |
706 | c->crypto_connections[i].status = CONN_NOT_CONFIRMED; /* Set it to its proper value right after. */ | 700 | c->crypto_connections[i].status = CRYPTO_CONN_NOT_CONFIRMED; /* Set it to its proper value right after. */ |
707 | } else { | 701 | } else { |
708 | /* This should not happen, timeout the connection if it does. */ | 702 | /* This should not happen, timeout the connection if it does. */ |
709 | c->crypto_connections[i].status = CONN_TIMED_OUT; | 703 | c->crypto_connections[i].status = CRYPTO_CONN_TIMED_OUT; |
710 | } | 704 | } |
711 | } else { | 705 | } else { |
712 | /* This should not happen, timeout the connection if it does. */ | 706 | /* This should not happen, timeout the connection if it does. */ |
713 | c->crypto_connections[i].status = CONN_TIMED_OUT; | 707 | c->crypto_connections[i].status = CRYPTO_CONN_TIMED_OUT; |
714 | } | 708 | } |
715 | } else if (id_packet(c->lossless_udp, | 709 | } else if (id_packet(c->lossless_udp, |
716 | c->crypto_connections[i].number) != -1) { | 710 | c->crypto_connections[i].number) != -1) { |
717 | /* This should not happen, timeout the connection if it does. */ | 711 | /* This should not happen, timeout the connection if it does. */ |
718 | c->crypto_connections[i].status = CONN_TIMED_OUT; | 712 | c->crypto_connections[i].status = CRYPTO_CONN_TIMED_OUT; |
719 | } | 713 | } |
720 | } | 714 | } |
721 | 715 | ||
722 | if (c->crypto_connections[i].status == CONN_NOT_CONFIRMED) { | 716 | if (c->crypto_connections[i].status == CRYPTO_CONN_NOT_CONFIRMED) { |
723 | if (id_packet(c->lossless_udp, c->crypto_connections[i].number) == 3) { | 717 | if (id_packet(c->lossless_udp, c->crypto_connections[i].number) == 3) { |
724 | uint8_t temp_data[MAX_DATA_SIZE]; | 718 | uint8_t temp_data[MAX_DATA_SIZE]; |
725 | uint8_t data[MAX_DATA_SIZE]; | 719 | uint8_t data[MAX_DATA_SIZE]; |
@@ -734,22 +728,22 @@ static void receive_crypto(Net_Crypto *c) | |||
734 | encrypt_precompute(c->crypto_connections[i].peersessionpublic_key, | 728 | encrypt_precompute(c->crypto_connections[i].peersessionpublic_key, |
735 | c->crypto_connections[i].sessionsecret_key, | 729 | c->crypto_connections[i].sessionsecret_key, |
736 | c->crypto_connections[i].shared_key); | 730 | c->crypto_connections[i].shared_key); |
737 | c->crypto_connections[i].status = CONN_ESTABLISHED; | 731 | c->crypto_connections[i].status = CRYPTO_CONN_ESTABLISHED; |
738 | c->crypto_connections[i].timeout = ~0; | 732 | c->crypto_connections[i].timeout = ~0; |
739 | /* Connection is accepted. */ | 733 | /* Connection is accepted. */ |
740 | confirm_connection(c->lossless_udp, c->crypto_connections[i].number); | 734 | confirm_connection(c->lossless_udp, c->crypto_connections[i].number); |
741 | } else { | 735 | } else { |
742 | /* This should not happen, timeout the connection if it does. */ | 736 | /* This should not happen, timeout the connection if it does. */ |
743 | c->crypto_connections[i].status = CONN_TIMED_OUT; | 737 | c->crypto_connections[i].status = CRYPTO_CONN_TIMED_OUT; |
744 | } | 738 | } |
745 | } else if (id_packet(c->lossless_udp, c->crypto_connections[i].number) != -1) { | 739 | } else if (id_packet(c->lossless_udp, c->crypto_connections[i].number) != -1) { |
746 | /* This should not happen, timeout the connection if it does. */ | 740 | /* This should not happen, timeout the connection if it does. */ |
747 | c->crypto_connections[i].status = CONN_TIMED_OUT; | 741 | c->crypto_connections[i].status = CRYPTO_CONN_TIMED_OUT; |
748 | } | 742 | } |
749 | } | 743 | } |
750 | 744 | ||
751 | if (temp_time > c->crypto_connections[i].timeout) { | 745 | if (temp_time > c->crypto_connections[i].timeout) { |
752 | c->crypto_connections[i].status = CONN_TIMED_OUT; | 746 | c->crypto_connections[i].status = CRYPTO_CONN_TIMED_OUT; |
753 | } | 747 | } |
754 | } | 748 | } |
755 | } | 749 | } |
@@ -788,9 +782,9 @@ static void kill_timedout(Net_Crypto *c) | |||
788 | uint32_t i; | 782 | uint32_t i; |
789 | 783 | ||
790 | for (i = 0; i < c->crypto_connections_length; ++i) { | 784 | for (i = 0; i < c->crypto_connections_length; ++i) { |
791 | if (c->crypto_connections[i].status != CONN_NO_CONNECTION | 785 | if (c->crypto_connections[i].status != CRYPTO_CONN_NO_CONNECTION |
792 | && is_connected(c->lossless_udp, c->crypto_connections[i].number) == LUDP_TIMED_OUT) | 786 | && is_connected(c->lossless_udp, c->crypto_connections[i].number) == LUDP_TIMED_OUT) |
793 | c->crypto_connections[i].status = CONN_TIMED_OUT; | 787 | c->crypto_connections[i].status = CRYPTO_CONN_TIMED_OUT; |
794 | } | 788 | } |
795 | } | 789 | } |
796 | 790 | ||