diff options
author | irungentoo <irungentoo@gmail.com> | 2013-10-23 14:32:09 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-10-23 14:32:09 -0400 |
commit | a67b4f8c6d85b5d77cbd8b04a1f7a90a4470947b (patch) | |
tree | c3cac91476290c4400d65d0eae0d510966ae5939 /toxcore/net_crypto.c | |
parent | b515eac0a3bec4d111e759922fd206236b56b4b1 (diff) |
Code cleanups.
Diffstat (limited to 'toxcore/net_crypto.c')
-rw-r--r-- | toxcore/net_crypto.c | 60 |
1 files changed, 27 insertions, 33 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 14831868..a9aa77f9 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c | |||
@@ -30,12 +30,6 @@ | |||
30 | 30 | ||
31 | #include "net_crypto.h" | 31 | #include "net_crypto.h" |
32 | 32 | ||
33 | #define CONN_NO_CONNECTION 0 | ||
34 | #define CONN_HANDSHAKE_SENT 1 | ||
35 | #define CONN_NOT_CONFIRMED 2 | ||
36 | #define CONN_ESTABLISHED 3 | ||
37 | #define CONN_TIMED_OUT 4 | ||
38 | |||
39 | static uint8_t crypt_connection_id_not_valid(Net_Crypto *c, int crypt_connection_id) | 33 | static uint8_t crypt_connection_id_not_valid(Net_Crypto *c, int crypt_connection_id) |
40 | { | 34 | { |
41 | return (uint32_t)crypt_connection_id >= c->crypto_connections_length; | 35 | return (uint32_t)crypt_connection_id >= c->crypto_connections_length; |
@@ -174,7 +168,7 @@ int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data) | |||
174 | if (crypt_connection_id_not_valid(c, crypt_connection_id)) | 168 | if (crypt_connection_id_not_valid(c, crypt_connection_id)) |
175 | return 0; | 169 | return 0; |
176 | 170 | ||
177 | if (c->crypto_connections[crypt_connection_id].status != CONN_ESTABLISHED) | 171 | if (c->crypto_connections[crypt_connection_id].status != CRYPTO_CONN_ESTABLISHED) |
178 | return 0; | 172 | return 0; |
179 | 173 | ||
180 | uint8_t temp_data[MAX_DATA_SIZE]; | 174 | uint8_t temp_data[MAX_DATA_SIZE]; |
@@ -220,7 +214,7 @@ int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uin | |||
220 | if (length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE - 1) | 214 | if (length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE - 1) |
221 | return 0; | 215 | return 0; |
222 | 216 | ||
223 | if (c->crypto_connections[crypt_connection_id].status != CONN_ESTABLISHED) | 217 | if (c->crypto_connections[crypt_connection_id].status != CRYPTO_CONN_ESTABLISHED) |
224 | return 0; | 218 | return 0; |
225 | 219 | ||
226 | uint8_t temp_data[MAX_DATA_SIZE]; | 220 | uint8_t temp_data[MAX_DATA_SIZE]; |
@@ -420,7 +414,7 @@ static int getcryptconnection_id(Net_Crypto *c, uint8_t *public_key) | |||
420 | uint32_t i; | 414 | uint32_t i; |
421 | 415 | ||
422 | for (i = 0; i < c->crypto_connections_length; ++i) { | 416 | for (i = 0; i < c->crypto_connections_length; ++i) { |
423 | if (c->crypto_connections[i].status != CONN_NO_CONNECTION) | 417 | if (c->crypto_connections[i].status != CRYPTO_CONN_NO_CONNECTION) |
424 | if (memcmp(public_key, c->crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0) | 418 | if (memcmp(public_key, c->crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0) |
425 | return i; | 419 | return i; |
426 | } | 420 | } |
@@ -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 | memcpy(c->crypto_connections[i].public_key, public_key, crypto_box_PUBLICKEYBYTES); | 480 | memcpy(c->crypto_connections[i].public_key, public_key, crypto_box_PUBLICKEYBYTES); |
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 | ||