From 8e80ced6cea5c50e628c1931f6b3b764b97efb41 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Fri, 22 May 2015 18:23:56 -0400 Subject: Move savedata to options struct. Add a way to select the type of savedata (normal savedata, load a secret key, potentially others?) to load. --- auto_tests/encryptsave_test.c | 20 ++++++++++++---- auto_tests/tox_test.c | 25 ++++++++++++-------- auto_tests/toxav_basic_test.c | 6 ++--- auto_tests/toxav_many_test.c | 10 ++++---- other/apidsl/tox.in.h | 43 ++++++++++++++++++++++++++++------ testing/irc_syncbot.c | 2 +- testing/nTox.c | 14 +++++++++-- testing/tox_shell.c | 2 +- testing/tox_sync.c | 2 +- toxcore/tox.c | 54 ++++++++++++++++++++++++++++--------------- toxcore/tox.h | 49 +++++++++++++++++++++++++++++++++------ 11 files changed, 166 insertions(+), 61 deletions(-) diff --git a/auto_tests/encryptsave_test.c b/auto_tests/encryptsave_test.c index a239bcee..b0828964 100644 --- a/auto_tests/encryptsave_test.c +++ b/auto_tests/encryptsave_test.c @@ -55,8 +55,8 @@ END_TEST START_TEST(test_save_friend) { - Tox *tox1 = tox_new(0, 0, 0, 0); - Tox *tox2 = tox_new(0, 0, 0, 0); + Tox *tox1 = tox_new(0, 0); + Tox *tox2 = tox_new(0, 0); ck_assert_msg(tox1 || tox2, "Failed to create 2 tox instances"); uint32_t to_compare = 974536; tox_callback_friend_request(tox2, accept_friend_request, &to_compare); @@ -75,15 +75,23 @@ START_TEST(test_save_friend) ck_assert_msg(ret, "failed to encrypted save: %u", error1); ck_assert_msg(tox_is_data_encrypted(enc_data), "magic number missing"); + struct Tox_Options options; + tox_options_default(&options); + options.savedata_type = TOX_SAVEDATA_TYPE_TOX_SAVE; + options.savedata_data = enc_data; + options.savedata_length = size2; + TOX_ERR_NEW err2; - Tox *tox3 = tox_new(0, enc_data, size2, &err2); + Tox *tox3 = tox_new(&options, &err2); ck_assert_msg(err2 == TOX_ERR_NEW_LOAD_ENCRYPTED, "wrong error! %u. should fail with %u", err2, TOX_ERR_NEW_LOAD_ENCRYPTED); uint8_t dec_data[size]; TOX_ERR_DECRYPTION err3; ret = tox_pass_decrypt(enc_data, size2, "correcthorsebatterystaple", 25, dec_data, &err3); ck_assert_msg(ret, "failed to decrypt save: %u", err3); - tox3 = tox_new(0, dec_data, size, &err2); + options.savedata_data = dec_data; + options.savedata_length = size; + tox3 = tox_new(&options, &err2); ck_assert_msg(err2 == TOX_ERR_NEW_OK, "failed to load from decrypted data: %u", err2); uint8_t address2[TOX_PUBLIC_KEY_SIZE]; ret = tox_friend_get_public_key(tox3, 0, address2, 0); @@ -111,7 +119,9 @@ START_TEST(test_save_friend) // and now with the code in use (I only bothered with manually to debug this, and it seems a waste // to remove the manual check now that it's there) - Tox *tox4 = tox_new(0, out1, size, &err2); + options.savedata_data = out1; + options.savedata_length = size; + Tox *tox4 = tox_new(&options, &err2); ck_assert_msg(err2 == TOX_ERR_NEW_OK, "failed to new the third"); uint8_t address5[TOX_PUBLIC_KEY_SIZE]; ret = tox_friend_get_public_key(tox4, 0, address5, 0); diff --git a/auto_tests/tox_test.c b/auto_tests/tox_test.c index 40022b0a..22528330 100644 --- a/auto_tests/tox_test.c +++ b/auto_tests/tox_test.c @@ -286,8 +286,8 @@ void tox_connection_status(Tox *tox, TOX_CONNECTION connection_status, void *use START_TEST(test_one) { - Tox *tox1 = tox_new(0, 0, 0, 0); - Tox *tox2 = tox_new(0, 0, 0, 0); + Tox *tox1 = tox_new(0, 0); + Tox *tox2 = tox_new(0, 0); { TOX_ERR_GET_PORT error; @@ -340,7 +340,12 @@ START_TEST(test_one) tox_kill(tox2); TOX_ERR_NEW err_n; - tox2 = tox_new(0, data, save_size, &err_n); + struct Tox_Options options; + tox_options_default(&options); + options.savedata_type = TOX_SAVEDATA_TYPE_TOX_SAVE; + options.savedata_data = data; + options.savedata_length = save_size; + tox2 = tox_new(&options, &err_n); ck_assert_msg(err_n == TOX_ERR_NEW_OK, "Load failed"); ck_assert_msg(tox_self_get_name_size(tox2) == sizeof name, "Wrong name size."); @@ -361,11 +366,11 @@ START_TEST(test_few_clients) { long long unsigned int con_time, cur_time = time(NULL); TOX_ERR_NEW t_n_error; - Tox *tox1 = tox_new(0, 0, 0, &t_n_error); + Tox *tox1 = tox_new(0, &t_n_error); ck_assert_msg(t_n_error == TOX_ERR_NEW_OK, "wrong error"); - Tox *tox2 = tox_new(0, 0, 0, &t_n_error); + Tox *tox2 = tox_new(0, &t_n_error); ck_assert_msg(t_n_error == TOX_ERR_NEW_OK, "wrong error"); - Tox *tox3 = tox_new(0, 0, 0, &t_n_error); + Tox *tox3 = tox_new(0, &t_n_error); ck_assert_msg(t_n_error == TOX_ERR_NEW_OK, "wrong error"); ck_assert_msg(tox1 || tox2 || tox3, "Failed to create 3 tox instances"); @@ -745,7 +750,7 @@ START_TEST(test_many_clients) uint32_t to_comp = 974536; for (i = 0; i < NUM_TOXES; ++i) { - toxes[i] = tox_new(0, 0, 0, 0); + toxes[i] = tox_new(0, 0); ck_assert_msg(toxes[i] != 0, "Failed to create tox instances %u", i); tox_callback_friend_request(toxes[i], accept_friend_request, &to_comp); } @@ -832,7 +837,7 @@ START_TEST(test_many_clients_tcp) opts.udp_enabled = 0; } - toxes[i] = tox_new(&opts, 0, 0, 0); + toxes[i] = tox_new(&opts, 0); ck_assert_msg(toxes[i] != 0, "Failed to create tox instances %u", i); tox_callback_friend_request(toxes[i], accept_friend_request, &to_comp); uint8_t dpk[TOX_PUBLIC_KEY_SIZE]; @@ -926,7 +931,7 @@ START_TEST(test_many_clients_tcp_b) opts.udp_enabled = 0; } - toxes[i] = tox_new(&opts, 0, 0, 0); + toxes[i] = tox_new(&opts, 0); ck_assert_msg(toxes[i] != 0, "Failed to create tox instances %u", i); tox_callback_friend_request(toxes[i], accept_friend_request, &to_comp); uint8_t dpk[TOX_PUBLIC_KEY_SIZE]; @@ -1062,7 +1067,7 @@ START_TEST(test_many_group) uint32_t to_comp = 234212; for (i = 0; i < NUM_GROUP_TOX; ++i) { - toxes[i] = tox_new(0, 0, 0, 0); + toxes[i] = tox_new(0, 0); ck_assert_msg(toxes[i] != 0, "Failed to create tox instances %u", i); tox_callback_friend_request(toxes[i], &g_accept_friend_request, &to_comp); tox_callback_group_invite(toxes[i], &print_group_invite_callback, &to_comp); diff --git a/auto_tests/toxav_basic_test.c b/auto_tests/toxav_basic_test.c index a43b7c2f..af8d91e9 100644 --- a/auto_tests/toxav_basic_test.c +++ b/auto_tests/toxav_basic_test.c @@ -250,9 +250,9 @@ if (status_control.Alice.status == Ended && status_control.Bob.status == Ended) START_TEST(test_AV_flows) { long long unsigned int cur_time = time(NULL); - Tox *bootstrap_node = tox_new(0, 0, 0, 0); - Tox *Alice = tox_new(0, 0, 0, 0); - Tox *Bob = tox_new(0, 0, 0, 0); + Tox *bootstrap_node = tox_new(0, 0); + Tox *Alice = tox_new(0, 0); + Tox *Bob = tox_new(0, 0); ck_assert_msg(bootstrap_node || Alice || Bob, "Failed to create 3 tox instances"); diff --git a/auto_tests/toxav_many_test.c b/auto_tests/toxav_many_test.c index 4287118f..6017e526 100644 --- a/auto_tests/toxav_many_test.c +++ b/auto_tests/toxav_many_test.c @@ -264,12 +264,12 @@ START_TEST(test_AV_three_calls) // void test_AV_three_calls() { long long unsigned int cur_time = time(NULL); - Tox *bootstrap_node = tox_new(0, 0, 0, 0); - Tox *caller = tox_new(0, 0, 0, 0); + Tox *bootstrap_node = tox_new(0, 0); + Tox *caller = tox_new(0, 0); Tox *callees[3] = { - tox_new(0, 0, 0, 0), - tox_new(0, 0, 0, 0), - tox_new(0, 0, 0, 0), + tox_new(0, 0), + tox_new(0, 0), + tox_new(0, 0), }; diff --git a/other/apidsl/tox.in.h b/other/apidsl/tox.in.h index dd47df23..b769135a 100644 --- a/other/apidsl/tox.in.h +++ b/other/apidsl/tox.in.h @@ -340,6 +340,24 @@ enum class PROXY_TYPE { SOCKS5, } +/** + * Type of savedata to create the Tox instance from. + */ +enum class SAVEDATA_TYPE { + /** + * No savedata. + */ + NONE, + /** + * Savedata is one that was obtained from ${savedata.get} + */ + TOX_SAVE, + /** + * Savedata is a secret key of length ${SECRET_KEY_SIZE} + */ + SECRET_KEY, +} + static class options { /** @@ -416,6 +434,23 @@ static class options { * The port to use for the TCP server. If 0, the tcp server is disabled. */ uint16_t tcp_port; + + namespace savedata { + /** + * The type of savedata to load from. + */ + SAVEDATA_TYPE type; + + /** + * The savedata. + */ + const uint8_t[length] data; + + /** + * The length of the savedata. + */ + size_t length; + } } @@ -474,21 +509,15 @@ static class options { * This function will bring the instance into a valid state. Running the event * loop with a new instance will operate correctly. * - * If the data parameter is not NULL, this function will load the Tox instance - * from a byte array previously filled by ${savedata.get}. - * * If loading failed or succeeded only partially, the new or partially loaded * instance is returned and an error code is set. * * @param options An options object as described above. If this parameter is * NULL, the default options are used. - * @param data A byte array containing data previously stored by ${savedata.get}. - * @param length The length of the byte array data. If this parameter is 0, the - * data parameter is ignored. * * @see $iterate for the event loop. */ -static this new(const options_t *options, const uint8_t[length] data) { +static this new(const options_t *options) { NULL, /** * The function was unable to allocate enough memory to store the internal diff --git a/testing/irc_syncbot.c b/testing/irc_syncbot.c index 2d326c4b..a4e2254a 100644 --- a/testing/irc_syncbot.c +++ b/testing/irc_syncbot.c @@ -222,7 +222,7 @@ Tox *init_tox(int argc, char *argv[]) exit(0); } - Tox *tox = tox_new(0, 0, 0, 0); + Tox *tox = tox_new(0, 0); if (!tox) exit(1); diff --git a/testing/nTox.c b/testing/nTox.c index 77f812b5..03f17da5 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -942,7 +942,17 @@ static Tox *load_data() return 0; } - Tox *m = tox_new(0, data, size, NULL); + struct Tox_Options options; + + tox_options_default(&options); + + options.savedata_type = TOX_SAVEDATA_TYPE_TOX_SAVE; + + options.savedata_data = data; + + options.savedata_length = size; + + Tox *m = tox_new(&options, NULL); if (fclose(data_file) < 0) { perror("[!] fclose failed"); @@ -953,7 +963,7 @@ static Tox *load_data() return m; } - return tox_new(0, 0, 0, NULL); + return tox_new(NULL, NULL); } static int save_data(Tox *m) diff --git a/testing/tox_shell.c b/testing/tox_shell.c index 23fa320a..521e9f36 100644 --- a/testing/tox_shell.c +++ b/testing/tox_shell.c @@ -95,7 +95,7 @@ int main(int argc, char *argv[]) printf("error setting flags\n"); } - Tox *tox = tox_new(0, 0, 0, 0); + Tox *tox = tox_new(0, 0); tox_callback_friend_connection_status(tox, print_online, NULL); tox_callback_friend_message(tox, print_message, master); diff --git a/testing/tox_sync.c b/testing/tox_sync.c index be1fe32d..738c2f2d 100644 --- a/testing/tox_sync.c +++ b/testing/tox_sync.c @@ -243,7 +243,7 @@ int main(int argc, char *argv[]) exit(0); } - Tox *tox = tox_new(0, 0, 0, 0); + Tox *tox = tox_new(0, 0); tox_callback_file_recv_chunk(tox, write_file, NULL); tox_callback_file_recv_control(tox, file_print_control, NULL); tox_callback_file_recv(tox, file_request_accept, NULL); diff --git a/toxcore/tox.c b/toxcore/tox.c index de615768..3081a226 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -121,33 +121,46 @@ void tox_options_free(struct Tox_Options *options) free(options); } -Tox *tox_new(const struct Tox_Options *options, const uint8_t *data, size_t length, TOX_ERR_NEW *error) +Tox *tox_new(const struct Tox_Options *options, TOX_ERR_NEW *error) { if (!logger_get_global()) logger_set_global(logger_new(LOGGER_OUTPUT_FILE, LOGGER_LEVEL, "toxcore")); - if (data == NULL && length != 0) { - SET_ERROR_PARAMETER(error, TOX_ERR_NEW_NULL); - return NULL; - } - - if (data && length) { - if (length < TOX_ENC_SAVE_MAGIC_LENGTH) { - SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); - return NULL; - } - - if (memcmp(data, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) == 0) { - SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_ENCRYPTED); - return NULL; - } - } - Messenger_Options m_options = {0}; + _Bool load_savedata_sk = 0, load_savedata_tox = 0; + if (options == NULL) { m_options.ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; } else { + if (options->savedata_type != TOX_SAVEDATA_TYPE_NONE) { + if (options->savedata_data == NULL || options->savedata_length == 0) { + SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); + return NULL; + } + } + + if (options->savedata_type == TOX_SAVEDATA_TYPE_SECRET_KEY) { + if (options->savedata_length != TOX_SECRET_KEY_SIZE) { + SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); + return NULL; + } + + load_savedata_sk = 1; + } else if (options->savedata_type == TOX_SAVEDATA_TYPE_TOX_SAVE) { + if (options->savedata_length < TOX_ENC_SAVE_MAGIC_LENGTH) { + SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); + return NULL; + } + + if (memcmp(options->savedata_data, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) == 0) { + SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_ENCRYPTED); + return NULL; + } + + load_savedata_tox = 1; + } + m_options.ipv6enabled = options->ipv6_enabled; m_options.udp_disabled = !options->udp_enabled; m_options.port_range[0] = options->start_port; @@ -208,8 +221,11 @@ Tox *tox_new(const struct Tox_Options *options, const uint8_t *data, size_t leng return NULL; } - if (data && length && messenger_load(m, data, length) == -1) { + if (load_savedata_tox && messenger_load(m, options->savedata_data, options->savedata_length) == -1) { SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); + } else if (load_savedata_sk) { + load_secret_key(m->net_crypto, options->savedata_data); + SET_ERROR_PARAMETER(error, TOX_ERR_NEW_OK); } else { SET_ERROR_PARAMETER(error, TOX_ERR_NEW_OK); } diff --git a/toxcore/tox.h b/toxcore/tox.h index 4afdf7c3..b556d138 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -351,6 +351,29 @@ typedef enum TOX_PROXY_TYPE { } TOX_PROXY_TYPE; +/** + * Type of savedata to create the Tox instance from. + */ +typedef enum TOX_SAVEDATA_TYPE { + + /** + * No savedata. + */ + TOX_SAVEDATA_TYPE_NONE, + + /** + * Savedata is one that was obtained from tox_get_savedata + */ + TOX_SAVEDATA_TYPE_TOX_SAVE, + + /** + * Savedata is a secret key of length TOX_SECRET_KEY_SIZE + */ + TOX_SAVEDATA_TYPE_SECRET_KEY, + +} TOX_SAVEDATA_TYPE; + + /** * This struct contains all the startup options for Tox. You can either allocate * this object yourself, and pass it to tox_options_default, or call @@ -432,6 +455,24 @@ struct Tox_Options { */ uint16_t tcp_port; + + /** + * The type of savedata to load from. + */ + TOX_SAVEDATA_TYPE savedata_type; + + + /** + * The savedata. + */ + const uint8_t *savedata_data; + + + /** + * The length of the savedata. + */ + size_t savedata_length; + }; @@ -561,21 +602,15 @@ typedef enum TOX_ERR_NEW { * This function will bring the instance into a valid state. Running the event * loop with a new instance will operate correctly. * - * If the data parameter is not NULL, this function will load the Tox instance - * from a byte array previously filled by tox_get_savedata. - * * If loading failed or succeeded only partially, the new or partially loaded * instance is returned and an error code is set. * * @param options An options object as described above. If this parameter is * NULL, the default options are used. - * @param data A byte array containing data previously stored by tox_get_savedata. - * @param length The length of the byte array data. If this parameter is 0, the - * data parameter is ignored. * * @see tox_iterate for the event loop. */ -Tox *tox_new(const struct Tox_Options *options, const uint8_t *data, size_t length, TOX_ERR_NEW *error); +Tox *tox_new(const struct Tox_Options *options, TOX_ERR_NEW *error); /** * Releases all resources associated with the Tox instance and disconnects from -- cgit v1.2.3