diff options
author | zugz (tox) <mbays+tox@sdf.org> | 2018-09-09 23:21:20 +0200 |
---|---|---|
committer | zugz (tox) <mbays+tox@sdf.org> | 2018-10-20 11:03:10 +0200 |
commit | 744dc2f5da2c54ad1e4d7d6ce229bc1756d81263 (patch) | |
tree | 35f6590fe3150118bc6b1336fbb0a7db06543668 /toxcore/Messenger.c | |
parent | aa5c7828802b3fcaedfd8d6fe9a08ca714b9aa2b (diff) |
Make saving and loading the responsibility of Tox rather than Messenger
Diffstat (limited to 'toxcore/Messenger.c')
-rw-r--r-- | toxcore/Messenger.c | 139 |
1 files changed, 39 insertions, 100 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index e092a9f2..ae2d1d3b 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c | |||
@@ -2739,10 +2739,6 @@ void do_messenger(Messenger *m, void *userdata) | |||
2739 | 2739 | ||
2740 | /* new messenger format for load/save, more robust and forward compatible */ | 2740 | /* new messenger format for load/save, more robust and forward compatible */ |
2741 | 2741 | ||
2742 | #define MESSENGER_STATE_COOKIE_GLOBAL 0x15ed1b1f | ||
2743 | |||
2744 | #define MESSENGER_STATE_COOKIE_TYPE 0x01ce | ||
2745 | |||
2746 | #define SAVED_FRIEND_REQUEST_SIZE 1024 | 2742 | #define SAVED_FRIEND_REQUEST_SIZE 1024 |
2747 | #define NUM_SAVED_PATH_NODES 8 | 2743 | #define NUM_SAVED_PATH_NODES 8 |
2748 | 2744 | ||
@@ -2881,7 +2877,7 @@ static uint32_t m_state_plugins_size(const Messenger *m) | |||
2881 | * returns true on success | 2877 | * returns true on success |
2882 | * returns false on failure | 2878 | * returns false on failure |
2883 | */ | 2879 | */ |
2884 | bool m_register_state_plugin(Messenger *m, Messenger_State_Type type, m_state_size_cb size_callback, | 2880 | bool m_register_state_plugin(Messenger *m, State_Type type, m_state_size_cb size_callback, |
2885 | m_state_load_cb load_callback, | 2881 | m_state_load_cb load_callback, |
2886 | m_state_save_cb save_callback) | 2882 | m_state_save_cb save_callback) |
2887 | { | 2883 | { |
@@ -2904,7 +2900,7 @@ bool m_register_state_plugin(Messenger *m, Messenger_State_Type type, m_state_si | |||
2904 | return true; | 2900 | return true; |
2905 | } | 2901 | } |
2906 | 2902 | ||
2907 | static uint32_t m_plugin_size(const Messenger *m, Messenger_State_Type type) | 2903 | static uint32_t m_plugin_size(const Messenger *m, State_Type type) |
2908 | { | 2904 | { |
2909 | for (uint8_t i = 0; i < m->options.state_plugins_length; ++i) { | 2905 | for (uint8_t i = 0; i < m->options.state_plugins_length; ++i) { |
2910 | const Messenger_State_Plugin plugin = m->options.state_plugins[i]; | 2906 | const Messenger_State_Plugin plugin = m->options.state_plugins[i]; |
@@ -2922,30 +2918,18 @@ static uint32_t m_plugin_size(const Messenger *m, Messenger_State_Type type) | |||
2922 | /* return size of the messenger data (for saving) */ | 2918 | /* return size of the messenger data (for saving) */ |
2923 | uint32_t messenger_size(const Messenger *m) | 2919 | uint32_t messenger_size(const Messenger *m) |
2924 | { | 2920 | { |
2925 | const uint32_t size32 = sizeof(uint32_t); | 2921 | return m_state_plugins_size(m); |
2926 | const uint32_t sizesubhead = size32 * 2; | ||
2927 | return size32 * 2 // global cookie | ||
2928 | + m_state_plugins_size(m) | ||
2929 | + sizesubhead; | ||
2930 | } | 2922 | } |
2931 | 2923 | ||
2932 | /* Save the messenger in data of size Messenger_size(). */ | 2924 | /* Save the messenger in data of size messenger_size(). */ |
2933 | void messenger_save(const Messenger *m, uint8_t *data) | 2925 | uint8_t *messenger_save(const Messenger *m, uint8_t *data) |
2934 | { | 2926 | { |
2935 | memset(data, 0, messenger_size(m)); | ||
2936 | |||
2937 | const uint32_t size32 = sizeof(uint32_t); | ||
2938 | |||
2939 | // write cookie | ||
2940 | memset(data, 0, size32); | ||
2941 | data += size32; | ||
2942 | host_to_lendian32(data, MESSENGER_STATE_COOKIE_GLOBAL); | ||
2943 | data += size32; | ||
2944 | |||
2945 | for (uint8_t i = 0; i < m->options.state_plugins_length; ++i) { | 2927 | for (uint8_t i = 0; i < m->options.state_plugins_length; ++i) { |
2946 | const Messenger_State_Plugin plugin = m->options.state_plugins[i]; | 2928 | const Messenger_State_Plugin plugin = m->options.state_plugins[i]; |
2947 | data = plugin.save(m, data); | 2929 | data = plugin.save(m, data); |
2948 | } | 2930 | } |
2931 | |||
2932 | return data; | ||
2949 | } | 2933 | } |
2950 | 2934 | ||
2951 | // nospam state plugin | 2935 | // nospam state plugin |
@@ -2956,12 +2940,12 @@ static uint32_t nospam_keys_size(const Messenger *m) | |||
2956 | 2940 | ||
2957 | static State_Load_Status load_nospam_keys(Messenger *m, const uint8_t *data, uint32_t length) | 2941 | static State_Load_Status load_nospam_keys(Messenger *m, const uint8_t *data, uint32_t length) |
2958 | { | 2942 | { |
2959 | if (length != m_plugin_size(m, MESSENGER_STATE_TYPE_NOSPAMKEYS)) { | 2943 | if (length != m_plugin_size(m, STATE_TYPE_NOSPAMKEYS)) { |
2960 | return STATE_LOAD_STATUS_ERROR; | 2944 | return STATE_LOAD_STATUS_ERROR; |
2961 | } | 2945 | } |
2962 | 2946 | ||
2963 | uint32_t nospam; | 2947 | uint32_t nospam; |
2964 | lendian_to_host32(&nospam, data); | 2948 | lendian_bytes_to_host32(&nospam, data); |
2965 | set_nospam(m->fr, nospam); | 2949 | set_nospam(m->fr, nospam); |
2966 | load_secret_key(m->net_crypto, data + sizeof(uint32_t) + CRYPTO_PUBLIC_KEY_SIZE); | 2950 | load_secret_key(m->net_crypto, data + sizeof(uint32_t) + CRYPTO_PUBLIC_KEY_SIZE); |
2967 | 2951 | ||
@@ -2974,11 +2958,11 @@ static State_Load_Status load_nospam_keys(Messenger *m, const uint8_t *data, uin | |||
2974 | 2958 | ||
2975 | static uint8_t *save_nospam_keys(const Messenger *m, uint8_t *data) | 2959 | static uint8_t *save_nospam_keys(const Messenger *m, uint8_t *data) |
2976 | { | 2960 | { |
2977 | const uint32_t len = m_plugin_size(m, MESSENGER_STATE_TYPE_NOSPAMKEYS); | 2961 | const uint32_t len = m_plugin_size(m, STATE_TYPE_NOSPAMKEYS); |
2978 | assert(sizeof(get_nospam(m->fr)) == sizeof(uint32_t)); | 2962 | assert(sizeof(get_nospam(m->fr)) == sizeof(uint32_t)); |
2979 | data = state_write_section_header(data, MESSENGER_STATE_COOKIE_TYPE, len, MESSENGER_STATE_TYPE_NOSPAMKEYS); | 2963 | data = state_write_section_header(data, STATE_COOKIE_TYPE, len, STATE_TYPE_NOSPAMKEYS); |
2980 | uint32_t nospam = get_nospam(m->fr); | 2964 | uint32_t nospam = get_nospam(m->fr); |
2981 | host_to_lendian32(data, nospam); | 2965 | host_to_lendian_bytes32(data, nospam); |
2982 | save_keys(m->net_crypto, data + sizeof(uint32_t)); | 2966 | save_keys(m->net_crypto, data + sizeof(uint32_t)); |
2983 | data += len; | 2967 | data += len; |
2984 | return data; | 2968 | return data; |
@@ -2992,8 +2976,8 @@ static uint32_t m_dht_size(const Messenger *m) | |||
2992 | 2976 | ||
2993 | static uint8_t *save_dht(const Messenger *m, uint8_t *data) | 2977 | static uint8_t *save_dht(const Messenger *m, uint8_t *data) |
2994 | { | 2978 | { |
2995 | const uint32_t len = m_plugin_size(m, MESSENGER_STATE_TYPE_DHT); | 2979 | const uint32_t len = m_plugin_size(m, STATE_TYPE_DHT); |
2996 | data = state_write_section_header(data, MESSENGER_STATE_COOKIE_TYPE, len, MESSENGER_STATE_TYPE_DHT); | 2980 | data = state_write_section_header(data, STATE_COOKIE_TYPE, len, STATE_TYPE_DHT); |
2997 | dht_save(m->dht, data); | 2981 | dht_save(m->dht, data); |
2998 | data += len; | 2982 | data += len; |
2999 | return data; | 2983 | return data; |
@@ -3013,8 +2997,8 @@ static uint32_t saved_friendslist_size(const Messenger *m) | |||
3013 | 2997 | ||
3014 | static uint8_t *friends_list_save(const Messenger *m, uint8_t *data) | 2998 | static uint8_t *friends_list_save(const Messenger *m, uint8_t *data) |
3015 | { | 2999 | { |
3016 | const uint32_t len = m_plugin_size(m, MESSENGER_STATE_TYPE_FRIENDS); | 3000 | const uint32_t len = m_plugin_size(m, STATE_TYPE_FRIENDS); |
3017 | data = state_write_section_header(data, MESSENGER_STATE_COOKIE_TYPE, len, MESSENGER_STATE_TYPE_FRIENDS); | 3001 | data = state_write_section_header(data, STATE_COOKIE_TYPE, len, STATE_TYPE_FRIENDS); |
3018 | 3002 | ||
3019 | uint32_t num = 0; | 3003 | uint32_t num = 0; |
3020 | uint8_t *cur_data = data; | 3004 | uint8_t *cur_data = data; |
@@ -3119,8 +3103,8 @@ static uint32_t name_size(const Messenger *m) | |||
3119 | 3103 | ||
3120 | static uint8_t *save_name(const Messenger *m, uint8_t *data) | 3104 | static uint8_t *save_name(const Messenger *m, uint8_t *data) |
3121 | { | 3105 | { |
3122 | const uint32_t len = m_plugin_size(m, MESSENGER_STATE_TYPE_NAME); | 3106 | const uint32_t len = m_plugin_size(m, STATE_TYPE_NAME); |
3123 | data = state_write_section_header(data, MESSENGER_STATE_COOKIE_TYPE, len, MESSENGER_STATE_TYPE_NAME); | 3107 | data = state_write_section_header(data, STATE_COOKIE_TYPE, len, STATE_TYPE_NAME); |
3124 | memcpy(data, m->name, len); | 3108 | memcpy(data, m->name, len); |
3125 | data += len; | 3109 | data += len; |
3126 | return data; | 3110 | return data; |
@@ -3143,8 +3127,8 @@ static uint32_t status_message_size(const Messenger *m) | |||
3143 | 3127 | ||
3144 | static uint8_t *save_status_message(const Messenger *m, uint8_t *data) | 3128 | static uint8_t *save_status_message(const Messenger *m, uint8_t *data) |
3145 | { | 3129 | { |
3146 | const uint32_t len = m_plugin_size(m, MESSENGER_STATE_TYPE_STATUSMESSAGE); | 3130 | const uint32_t len = m_plugin_size(m, STATE_TYPE_STATUSMESSAGE); |
3147 | data = state_write_section_header(data, MESSENGER_STATE_COOKIE_TYPE, len, MESSENGER_STATE_TYPE_STATUSMESSAGE); | 3131 | data = state_write_section_header(data, STATE_COOKIE_TYPE, len, STATE_TYPE_STATUSMESSAGE); |
3148 | memcpy(data, m->statusmessage, len); | 3132 | memcpy(data, m->statusmessage, len); |
3149 | data += len; | 3133 | data += len; |
3150 | return data; | 3134 | return data; |
@@ -3167,8 +3151,8 @@ static uint32_t status_size(const Messenger *m) | |||
3167 | 3151 | ||
3168 | static uint8_t *save_status(const Messenger *m, uint8_t *data) | 3152 | static uint8_t *save_status(const Messenger *m, uint8_t *data) |
3169 | { | 3153 | { |
3170 | const uint32_t len = m_plugin_size(m, MESSENGER_STATE_TYPE_STATUS); | 3154 | const uint32_t len = m_plugin_size(m, STATE_TYPE_STATUS); |
3171 | data = state_write_section_header(data, MESSENGER_STATE_COOKIE_TYPE, len, MESSENGER_STATE_TYPE_STATUS); | 3155 | data = state_write_section_header(data, STATE_COOKIE_TYPE, len, STATE_TYPE_STATUS); |
3172 | *data = m->userstatus; | 3156 | *data = m->userstatus; |
3173 | data += len; | 3157 | data += len; |
3174 | return data; | 3158 | return data; |
@@ -3193,7 +3177,7 @@ static uint8_t *save_tcp_relays(const Messenger *m, uint8_t *data) | |||
3193 | { | 3177 | { |
3194 | Node_format relays[NUM_SAVED_TCP_RELAYS]; | 3178 | Node_format relays[NUM_SAVED_TCP_RELAYS]; |
3195 | uint8_t *temp_data = data; | 3179 | uint8_t *temp_data = data; |
3196 | data = state_write_section_header(temp_data, MESSENGER_STATE_COOKIE_TYPE, 0, MESSENGER_STATE_TYPE_TCP_RELAY); | 3180 | data = state_write_section_header(temp_data, STATE_COOKIE_TYPE, 0, STATE_TYPE_TCP_RELAY); |
3197 | uint32_t num = copy_connected_tcp_relays(m->net_crypto, relays, NUM_SAVED_TCP_RELAYS); | 3181 | uint32_t num = copy_connected_tcp_relays(m->net_crypto, relays, NUM_SAVED_TCP_RELAYS); |
3198 | 3182 | ||
3199 | if (m->num_loaded_relays > 0) { | 3183 | if (m->num_loaded_relays > 0) { |
@@ -3205,7 +3189,7 @@ static uint8_t *save_tcp_relays(const Messenger *m, uint8_t *data) | |||
3205 | 3189 | ||
3206 | if (l > 0) { | 3190 | if (l > 0) { |
3207 | const uint32_t len = l; | 3191 | const uint32_t len = l; |
3208 | data = state_write_section_header(temp_data, MESSENGER_STATE_COOKIE_TYPE, len, MESSENGER_STATE_TYPE_TCP_RELAY); | 3192 | data = state_write_section_header(temp_data, STATE_COOKIE_TYPE, len, STATE_TYPE_TCP_RELAY); |
3209 | data += len; | 3193 | data += len; |
3210 | } | 3194 | } |
3211 | 3195 | ||
@@ -3232,14 +3216,14 @@ static uint8_t *save_path_nodes(const Messenger *m, uint8_t *data) | |||
3232 | { | 3216 | { |
3233 | Node_format nodes[NUM_SAVED_PATH_NODES]; | 3217 | Node_format nodes[NUM_SAVED_PATH_NODES]; |
3234 | uint8_t *temp_data = data; | 3218 | uint8_t *temp_data = data; |
3235 | data = state_write_section_header(data, MESSENGER_STATE_COOKIE_TYPE, 0, MESSENGER_STATE_TYPE_PATH_NODE); | 3219 | data = state_write_section_header(data, STATE_COOKIE_TYPE, 0, STATE_TYPE_PATH_NODE); |
3236 | memset(nodes, 0, sizeof(nodes)); | 3220 | memset(nodes, 0, sizeof(nodes)); |
3237 | const unsigned int num = onion_backup_nodes(m->onion_c, nodes, NUM_SAVED_PATH_NODES); | 3221 | const unsigned int num = onion_backup_nodes(m->onion_c, nodes, NUM_SAVED_PATH_NODES); |
3238 | const int l = pack_nodes(data, NUM_SAVED_PATH_NODES * packed_node_size(net_family_tcp_ipv6), nodes, num); | 3222 | const int l = pack_nodes(data, NUM_SAVED_PATH_NODES * packed_node_size(net_family_tcp_ipv6), nodes, num); |
3239 | 3223 | ||
3240 | if (l > 0) { | 3224 | if (l > 0) { |
3241 | const uint32_t len = l; | 3225 | const uint32_t len = l; |
3242 | data = state_write_section_header(temp_data, MESSENGER_STATE_COOKIE_TYPE, len, MESSENGER_STATE_TYPE_PATH_NODE); | 3226 | data = state_write_section_header(temp_data, STATE_COOKIE_TYPE, len, STATE_TYPE_PATH_NODE); |
3243 | data += len; | 3227 | data += len; |
3244 | } | 3228 | } |
3245 | 3229 | ||
@@ -3261,77 +3245,32 @@ static State_Load_Status load_path_nodes(Messenger *m, const uint8_t *data, uint | |||
3261 | return STATE_LOAD_STATUS_CONTINUE; | 3245 | return STATE_LOAD_STATUS_CONTINUE; |
3262 | } | 3246 | } |
3263 | 3247 | ||
3264 | // end state plugin | ||
3265 | static uint32_t end_size(const Messenger *m) | ||
3266 | { | ||
3267 | return 0; | ||
3268 | } | ||
3269 | |||
3270 | static uint8_t *save_end(const Messenger *m, uint8_t *data) | ||
3271 | { | ||
3272 | return state_write_section_header(data, MESSENGER_STATE_COOKIE_TYPE, 0, MESSENGER_STATE_TYPE_END); | ||
3273 | } | ||
3274 | |||
3275 | static State_Load_Status load_end(Messenger *m, const uint8_t *data, uint32_t length) | ||
3276 | { | ||
3277 | if (length != 0) { | ||
3278 | return STATE_LOAD_STATUS_ERROR; | ||
3279 | } | ||
3280 | |||
3281 | return STATE_LOAD_STATUS_END; | ||
3282 | } | ||
3283 | |||
3284 | static void m_register_default_plugins(Messenger *m) | 3248 | static void m_register_default_plugins(Messenger *m) |
3285 | { | 3249 | { |
3286 | m_register_state_plugin(m, MESSENGER_STATE_TYPE_NOSPAMKEYS, nospam_keys_size, load_nospam_keys, save_nospam_keys); | 3250 | m_register_state_plugin(m, STATE_TYPE_NOSPAMKEYS, nospam_keys_size, load_nospam_keys, save_nospam_keys); |
3287 | m_register_state_plugin(m, MESSENGER_STATE_TYPE_DHT, m_dht_size, m_dht_load, save_dht); | 3251 | m_register_state_plugin(m, STATE_TYPE_DHT, m_dht_size, m_dht_load, save_dht); |
3288 | m_register_state_plugin(m, MESSENGER_STATE_TYPE_FRIENDS, saved_friendslist_size, friends_list_load, friends_list_save); | 3252 | m_register_state_plugin(m, STATE_TYPE_FRIENDS, saved_friendslist_size, friends_list_load, friends_list_save); |
3289 | m_register_state_plugin(m, MESSENGER_STATE_TYPE_NAME, name_size, load_name, save_name); | 3253 | m_register_state_plugin(m, STATE_TYPE_NAME, name_size, load_name, save_name); |
3290 | m_register_state_plugin(m, MESSENGER_STATE_TYPE_STATUSMESSAGE, status_message_size, load_status_message, | 3254 | m_register_state_plugin(m, STATE_TYPE_STATUSMESSAGE, status_message_size, load_status_message, |
3291 | save_status_message); | 3255 | save_status_message); |
3292 | m_register_state_plugin(m, MESSENGER_STATE_TYPE_STATUS, status_size, load_status, save_status); | 3256 | m_register_state_plugin(m, STATE_TYPE_STATUS, status_size, load_status, save_status); |
3293 | m_register_state_plugin(m, MESSENGER_STATE_TYPE_TCP_RELAY, tcp_relay_size, load_tcp_relays, save_tcp_relays); | 3257 | m_register_state_plugin(m, STATE_TYPE_TCP_RELAY, tcp_relay_size, load_tcp_relays, save_tcp_relays); |
3294 | m_register_state_plugin(m, MESSENGER_STATE_TYPE_PATH_NODE, path_node_size, load_path_nodes, save_path_nodes); | 3258 | m_register_state_plugin(m, STATE_TYPE_PATH_NODE, path_node_size, load_path_nodes, save_path_nodes); |
3295 | m_register_state_plugin(m, MESSENGER_STATE_TYPE_END, end_size, load_end, save_end); | ||
3296 | } | 3259 | } |
3297 | 3260 | ||
3298 | static State_Load_Status messenger_load_state_callback(void *outer, const uint8_t *data, uint32_t length, uint16_t type) | 3261 | bool messenger_load_state_section(Messenger *m, const uint8_t *data, uint32_t length, uint16_t type, |
3262 | State_Load_Status *status) | ||
3299 | { | 3263 | { |
3300 | Messenger *m = (Messenger *)outer; | ||
3301 | |||
3302 | for (uint8_t i = 0; i < m->options.state_plugins_length; ++i) { | 3264 | for (uint8_t i = 0; i < m->options.state_plugins_length; ++i) { |
3303 | const Messenger_State_Plugin *const plugin = &m->options.state_plugins[i]; | 3265 | const Messenger_State_Plugin *const plugin = &m->options.state_plugins[i]; |
3304 | 3266 | ||
3305 | if (plugin->type == type) { | 3267 | if (plugin->type == type) { |
3306 | return plugin->load(m, data, length); | 3268 | *status = plugin->load(m, data, length); |
3269 | return true; | ||
3307 | } | 3270 | } |
3308 | } | 3271 | } |
3309 | 3272 | ||
3310 | LOGGER_ERROR(m->log, "Load state: contains unrecognized part (len %u, type %u)\n", | 3273 | return false; |
3311 | length, type); | ||
3312 | |||
3313 | return STATE_LOAD_STATUS_CONTINUE; | ||
3314 | } | ||
3315 | |||
3316 | /* Load the messenger from data of size length. */ | ||
3317 | int messenger_load(Messenger *m, const uint8_t *data, uint32_t length) | ||
3318 | { | ||
3319 | uint32_t data32[2]; | ||
3320 | uint32_t cookie_len = sizeof(data32); | ||
3321 | |||
3322 | if (length < cookie_len) { | ||
3323 | return -1; | ||
3324 | } | ||
3325 | |||
3326 | memcpy(data32, data, sizeof(uint32_t)); | ||
3327 | lendian_to_host32(data32 + 1, data + sizeof(uint32_t)); | ||
3328 | |||
3329 | if (!data32[0] && (data32[1] == MESSENGER_STATE_COOKIE_GLOBAL)) { | ||
3330 | return state_load(m->log, messenger_load_state_callback, m, data + cookie_len, | ||
3331 | length - cookie_len, MESSENGER_STATE_COOKIE_TYPE); | ||
3332 | } | ||
3333 | |||
3334 | return -1; | ||
3335 | } | 3274 | } |
3336 | 3275 | ||
3337 | /* Return the number of friends in the instance m. | 3276 | /* Return the number of friends in the instance m. |