diff options
-rw-r--r-- | toxcore/Messenger.c | 101 | ||||
-rw-r--r-- | toxcore/net_crypto.c | 12 |
2 files changed, 40 insertions, 73 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index f53a1d04..96005237 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c | |||
@@ -2477,9 +2477,10 @@ int wait_cleanup_messenger(Messenger *m, uint8_t *data) | |||
2477 | #define MESSENGER_STATE_TYPE_NAME 4 | 2477 | #define MESSENGER_STATE_TYPE_NAME 4 |
2478 | #define MESSENGER_STATE_TYPE_STATUSMESSAGE 5 | 2478 | #define MESSENGER_STATE_TYPE_STATUSMESSAGE 5 |
2479 | #define MESSENGER_STATE_TYPE_STATUS 6 | 2479 | #define MESSENGER_STATE_TYPE_STATUS 6 |
2480 | #define MESSENGER_STATE_TYPE_TCP_RELAY 10 | ||
2480 | 2481 | ||
2481 | #define SAVED_FRIEND_REQUEST_SIZE 1024 | 2482 | #define SAVED_FRIEND_REQUEST_SIZE 1024 |
2482 | 2483 | #define NUM_SAVED_TCP_RELAYS 8 | |
2483 | struct SAVED_FRIEND { | 2484 | struct SAVED_FRIEND { |
2484 | uint8_t status; | 2485 | uint8_t status; |
2485 | uint8_t client_id[CLIENT_ID_SIZE]; | 2486 | uint8_t client_id[CLIENT_ID_SIZE]; |
@@ -2614,6 +2615,7 @@ uint32_t messenger_size(Messenger *m) | |||
2614 | + sizesubhead + m->name_length // Own nickname. | 2615 | + sizesubhead + m->name_length // Own nickname. |
2615 | + sizesubhead + m->statusmessage_length // status message | 2616 | + sizesubhead + m->statusmessage_length // status message |
2616 | + sizesubhead + 1 // status | 2617 | + sizesubhead + 1 // status |
2618 | + sizesubhead + NUM_SAVED_TCP_RELAYS * sizeof(Node_format) //TCP relays | ||
2617 | ; | 2619 | ; |
2618 | } | 2620 | } |
2619 | 2621 | ||
@@ -2678,74 +2680,15 @@ void messenger_save(Messenger *m, uint8_t *data) | |||
2678 | data = z_state_save_subheader(data, len, type); | 2680 | data = z_state_save_subheader(data, len, type); |
2679 | *data = m->userstatus; | 2681 | *data = m->userstatus; |
2680 | data += len; | 2682 | data += len; |
2681 | } | ||
2682 | |||
2683 | static int messenger_load_state_callback_old(void *outer, uint8_t *data, uint32_t length, uint16_t type) | ||
2684 | { | ||
2685 | Messenger *m = outer; | ||
2686 | |||
2687 | switch (type) { | ||
2688 | case MESSENGER_STATE_TYPE_NOSPAMKEYS: | ||
2689 | if (length == crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t)) { | ||
2690 | set_nospam(&(m->fr), *(uint32_t *)data); | ||
2691 | load_keys(m->net_crypto, &data[sizeof(uint32_t)]); | ||
2692 | #ifdef ENABLE_ASSOC_DHT | ||
2693 | 2683 | ||
2694 | if (m->dht->assoc) | 2684 | Node_format relays[NUM_SAVED_TCP_RELAYS]; |
2695 | Assoc_self_client_id_changed(m->dht->assoc, m->net_crypto->self_public_key); | 2685 | len = sizeof(relays); |
2696 | 2686 | type = MESSENGER_STATE_TYPE_TCP_RELAY; | |
2697 | #endif | 2687 | data = z_state_save_subheader(data, len, type); |
2698 | } else | 2688 | memset(relays, 0, len); |
2699 | return -1; /* critical */ | 2689 | copy_connected_tcp_relays(m->net_crypto, relays, NUM_SAVED_TCP_RELAYS); |
2700 | 2690 | memcpy(data, relays, len); | |
2701 | break; | 2691 | data += len; |
2702 | |||
2703 | case MESSENGER_STATE_TYPE_DHT: | ||
2704 | DHT_load(m->dht, data, length); | ||
2705 | break; | ||
2706 | |||
2707 | case MESSENGER_STATE_TYPE_FRIENDS: | ||
2708 | if (!(length % sizeof(Friend))) { | ||
2709 | uint16_t num = length / sizeof(Friend); | ||
2710 | Friend *friends = (Friend *)data; | ||
2711 | uint32_t i; | ||
2712 | |||
2713 | for (i = 0; i < num; ++i) { | ||
2714 | if (friends[i].status >= 3) { | ||
2715 | int fnum = m_addfriend_norequest(m, friends[i].client_id); | ||
2716 | setfriendname(m, fnum, friends[i].name, friends[i].name_length); | ||
2717 | /* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */ | ||
2718 | } else if (friends[i].status != 0) { | ||
2719 | /* TODO: This is not a good way to do this. */ | ||
2720 | uint8_t address[FRIEND_ADDRESS_SIZE]; | ||
2721 | id_copy(address, friends[i].client_id); | ||
2722 | memcpy(address + crypto_box_PUBLICKEYBYTES, &(friends[i].friendrequest_nospam), sizeof(uint32_t)); | ||
2723 | uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum)); | ||
2724 | memcpy(address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), &checksum, sizeof(checksum)); | ||
2725 | m_addfriend(m, address, friends[i].info, friends[i].info_size); | ||
2726 | } | ||
2727 | } | ||
2728 | } | ||
2729 | |||
2730 | break; | ||
2731 | |||
2732 | case MESSENGER_STATE_TYPE_NAME: | ||
2733 | if ((length > 0) && (length < MAX_NAME_LENGTH)) { | ||
2734 | setname(m, data, length); | ||
2735 | } | ||
2736 | |||
2737 | break; | ||
2738 | |||
2739 | #ifdef DEBUG | ||
2740 | |||
2741 | default: | ||
2742 | fprintf(stderr, "Load state: contains unrecognized part (len %u, type %u)\n", | ||
2743 | length, type); | ||
2744 | break; | ||
2745 | #endif | ||
2746 | } | ||
2747 | |||
2748 | return 0; | ||
2749 | } | 2692 | } |
2750 | 2693 | ||
2751 | static int messenger_load_state_callback(void *outer, uint8_t *data, uint32_t length, uint16_t type) | 2694 | static int messenger_load_state_callback(void *outer, uint8_t *data, uint32_t length, uint16_t type) |
@@ -2796,6 +2739,22 @@ static int messenger_load_state_callback(void *outer, uint8_t *data, uint32_t le | |||
2796 | } | 2739 | } |
2797 | 2740 | ||
2798 | break; | 2741 | break; |
2742 | |||
2743 | case MESSENGER_STATE_TYPE_TCP_RELAY: { | ||
2744 | Node_format relays[NUM_SAVED_TCP_RELAYS]; | ||
2745 | |||
2746 | if (length != sizeof(relays)) { | ||
2747 | return -1; | ||
2748 | } | ||
2749 | |||
2750 | memcpy(relays, data, length); | ||
2751 | uint32_t i; | ||
2752 | |||
2753 | for (i = 0; i < NUM_SAVED_TCP_RELAYS; ++i) { | ||
2754 | add_tcp_relay(m->net_crypto, relays[i].ip_port, relays[i].client_id); | ||
2755 | } | ||
2756 | } | ||
2757 | break; | ||
2799 | #ifdef DEBUG | 2758 | #ifdef DEBUG |
2800 | 2759 | ||
2801 | default: | 2760 | default: |
@@ -2822,11 +2781,7 @@ int messenger_load(Messenger *m, uint8_t *data, uint32_t length) | |||
2822 | if (!data32[0] && (data32[1] == MESSENGER_STATE_COOKIE_GLOBAL)) | 2781 | if (!data32[0] && (data32[1] == MESSENGER_STATE_COOKIE_GLOBAL)) |
2823 | return load_state(messenger_load_state_callback, m, data + cookie_len, | 2782 | return load_state(messenger_load_state_callback, m, data + cookie_len, |
2824 | length - cookie_len, MESSENGER_STATE_COOKIE_TYPE); | 2783 | length - cookie_len, MESSENGER_STATE_COOKIE_TYPE); |
2825 | 2784 | else | |
2826 | else if (!data32[0] && (data32[1] == MESSENGER_STATE_COOKIE_GLOBAL_OLD)) | ||
2827 | return load_state(messenger_load_state_callback_old, m, data + cookie_len, | ||
2828 | length - cookie_len, MESSENGER_STATE_COOKIE_TYPE); | ||
2829 | else /* old state file */ | ||
2830 | return -1; | 2785 | return -1; |
2831 | } | 2786 | } |
2832 | 2787 | ||
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 1fcd434e..875c639f 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c | |||
@@ -1778,6 +1778,9 @@ int add_tcp_relay_peer(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port, | |||
1778 | ip_port.ip.family = AF_INET6; | 1778 | ip_port.ip.family = AF_INET6; |
1779 | } | 1779 | } |
1780 | 1780 | ||
1781 | if (ip_port.ip.family != AF_INET && ip_port.ip.family != AF_INET6) | ||
1782 | return -1; | ||
1783 | |||
1781 | uint32_t i; | 1784 | uint32_t i; |
1782 | 1785 | ||
1783 | for (i = 0; i < conn->num_tcp_relays; ++i) { | 1786 | for (i = 0; i < conn->num_tcp_relays; ++i) { |
@@ -1817,6 +1820,15 @@ int add_tcp_relay_peer(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port, | |||
1817 | */ | 1820 | */ |
1818 | int add_tcp_relay(Net_Crypto *c, IP_Port ip_port, uint8_t *public_key) | 1821 | int add_tcp_relay(Net_Crypto *c, IP_Port ip_port, uint8_t *public_key) |
1819 | { | 1822 | { |
1823 | if (ip_port.ip.family == TCP_INET) { | ||
1824 | ip_port.ip.family = AF_INET; | ||
1825 | } else if (ip_port.ip.family == TCP_INET6) { | ||
1826 | ip_port.ip.family = AF_INET6; | ||
1827 | } | ||
1828 | |||
1829 | if (ip_port.ip.family != AF_INET && ip_port.ip.family != AF_INET6) | ||
1830 | return -1; | ||
1831 | |||
1820 | if (tcp_connection_check(c, public_key) != 0) | 1832 | if (tcp_connection_check(c, public_key) != 0) |
1821 | return -1; | 1833 | return -1; |
1822 | 1834 | ||