diff options
-rw-r--r-- | auto_tests/encryptsave_test.c | 20 | ||||
-rw-r--r-- | auto_tests/tox_test.c | 25 | ||||
-rw-r--r-- | auto_tests/toxav_basic_test.c | 6 | ||||
-rw-r--r-- | auto_tests/toxav_many_test.c | 10 | ||||
-rw-r--r-- | other/apidsl/tox.in.h | 43 | ||||
-rw-r--r-- | testing/irc_syncbot.c | 2 | ||||
-rw-r--r-- | testing/nTox.c | 14 | ||||
-rw-r--r-- | testing/tox_shell.c | 2 | ||||
-rw-r--r-- | testing/tox_sync.c | 2 | ||||
-rw-r--r-- | toxcore/tox.c | 54 | ||||
-rw-r--r-- | 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 | |||
55 | 55 | ||
56 | START_TEST(test_save_friend) | 56 | START_TEST(test_save_friend) |
57 | { | 57 | { |
58 | Tox *tox1 = tox_new(0, 0, 0, 0); | 58 | Tox *tox1 = tox_new(0, 0); |
59 | Tox *tox2 = tox_new(0, 0, 0, 0); | 59 | Tox *tox2 = tox_new(0, 0); |
60 | ck_assert_msg(tox1 || tox2, "Failed to create 2 tox instances"); | 60 | ck_assert_msg(tox1 || tox2, "Failed to create 2 tox instances"); |
61 | uint32_t to_compare = 974536; | 61 | uint32_t to_compare = 974536; |
62 | tox_callback_friend_request(tox2, accept_friend_request, &to_compare); | 62 | tox_callback_friend_request(tox2, accept_friend_request, &to_compare); |
@@ -75,15 +75,23 @@ START_TEST(test_save_friend) | |||
75 | ck_assert_msg(ret, "failed to encrypted save: %u", error1); | 75 | ck_assert_msg(ret, "failed to encrypted save: %u", error1); |
76 | ck_assert_msg(tox_is_data_encrypted(enc_data), "magic number missing"); | 76 | ck_assert_msg(tox_is_data_encrypted(enc_data), "magic number missing"); |
77 | 77 | ||
78 | struct Tox_Options options; | ||
79 | tox_options_default(&options); | ||
80 | options.savedata_type = TOX_SAVEDATA_TYPE_TOX_SAVE; | ||
81 | options.savedata_data = enc_data; | ||
82 | options.savedata_length = size2; | ||
83 | |||
78 | TOX_ERR_NEW err2; | 84 | TOX_ERR_NEW err2; |
79 | Tox *tox3 = tox_new(0, enc_data, size2, &err2); | 85 | Tox *tox3 = tox_new(&options, &err2); |
80 | ck_assert_msg(err2 == TOX_ERR_NEW_LOAD_ENCRYPTED, "wrong error! %u. should fail with %u", err2, | 86 | ck_assert_msg(err2 == TOX_ERR_NEW_LOAD_ENCRYPTED, "wrong error! %u. should fail with %u", err2, |
81 | TOX_ERR_NEW_LOAD_ENCRYPTED); | 87 | TOX_ERR_NEW_LOAD_ENCRYPTED); |
82 | uint8_t dec_data[size]; | 88 | uint8_t dec_data[size]; |
83 | TOX_ERR_DECRYPTION err3; | 89 | TOX_ERR_DECRYPTION err3; |
84 | ret = tox_pass_decrypt(enc_data, size2, "correcthorsebatterystaple", 25, dec_data, &err3); | 90 | ret = tox_pass_decrypt(enc_data, size2, "correcthorsebatterystaple", 25, dec_data, &err3); |
85 | ck_assert_msg(ret, "failed to decrypt save: %u", err3); | 91 | ck_assert_msg(ret, "failed to decrypt save: %u", err3); |
86 | tox3 = tox_new(0, dec_data, size, &err2); | 92 | options.savedata_data = dec_data; |
93 | options.savedata_length = size; | ||
94 | tox3 = tox_new(&options, &err2); | ||
87 | ck_assert_msg(err2 == TOX_ERR_NEW_OK, "failed to load from decrypted data: %u", err2); | 95 | ck_assert_msg(err2 == TOX_ERR_NEW_OK, "failed to load from decrypted data: %u", err2); |
88 | uint8_t address2[TOX_PUBLIC_KEY_SIZE]; | 96 | uint8_t address2[TOX_PUBLIC_KEY_SIZE]; |
89 | ret = tox_friend_get_public_key(tox3, 0, address2, 0); | 97 | ret = tox_friend_get_public_key(tox3, 0, address2, 0); |
@@ -111,7 +119,9 @@ START_TEST(test_save_friend) | |||
111 | 119 | ||
112 | // and now with the code in use (I only bothered with manually to debug this, and it seems a waste | 120 | // and now with the code in use (I only bothered with manually to debug this, and it seems a waste |
113 | // to remove the manual check now that it's there) | 121 | // to remove the manual check now that it's there) |
114 | Tox *tox4 = tox_new(0, out1, size, &err2); | 122 | options.savedata_data = out1; |
123 | options.savedata_length = size; | ||
124 | Tox *tox4 = tox_new(&options, &err2); | ||
115 | ck_assert_msg(err2 == TOX_ERR_NEW_OK, "failed to new the third"); | 125 | ck_assert_msg(err2 == TOX_ERR_NEW_OK, "failed to new the third"); |
116 | uint8_t address5[TOX_PUBLIC_KEY_SIZE]; | 126 | uint8_t address5[TOX_PUBLIC_KEY_SIZE]; |
117 | ret = tox_friend_get_public_key(tox4, 0, address5, 0); | 127 | 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 | |||
286 | 286 | ||
287 | START_TEST(test_one) | 287 | START_TEST(test_one) |
288 | { | 288 | { |
289 | Tox *tox1 = tox_new(0, 0, 0, 0); | 289 | Tox *tox1 = tox_new(0, 0); |
290 | Tox *tox2 = tox_new(0, 0, 0, 0); | 290 | Tox *tox2 = tox_new(0, 0); |
291 | 291 | ||
292 | { | 292 | { |
293 | TOX_ERR_GET_PORT error; | 293 | TOX_ERR_GET_PORT error; |
@@ -340,7 +340,12 @@ START_TEST(test_one) | |||
340 | tox_kill(tox2); | 340 | tox_kill(tox2); |
341 | TOX_ERR_NEW err_n; | 341 | TOX_ERR_NEW err_n; |
342 | 342 | ||
343 | tox2 = tox_new(0, data, save_size, &err_n); | 343 | struct Tox_Options options; |
344 | tox_options_default(&options); | ||
345 | options.savedata_type = TOX_SAVEDATA_TYPE_TOX_SAVE; | ||
346 | options.savedata_data = data; | ||
347 | options.savedata_length = save_size; | ||
348 | tox2 = tox_new(&options, &err_n); | ||
344 | ck_assert_msg(err_n == TOX_ERR_NEW_OK, "Load failed"); | 349 | ck_assert_msg(err_n == TOX_ERR_NEW_OK, "Load failed"); |
345 | 350 | ||
346 | ck_assert_msg(tox_self_get_name_size(tox2) == sizeof name, "Wrong name size."); | 351 | ck_assert_msg(tox_self_get_name_size(tox2) == sizeof name, "Wrong name size."); |
@@ -361,11 +366,11 @@ START_TEST(test_few_clients) | |||
361 | { | 366 | { |
362 | long long unsigned int con_time, cur_time = time(NULL); | 367 | long long unsigned int con_time, cur_time = time(NULL); |
363 | TOX_ERR_NEW t_n_error; | 368 | TOX_ERR_NEW t_n_error; |
364 | Tox *tox1 = tox_new(0, 0, 0, &t_n_error); | 369 | Tox *tox1 = tox_new(0, &t_n_error); |
365 | ck_assert_msg(t_n_error == TOX_ERR_NEW_OK, "wrong error"); | 370 | ck_assert_msg(t_n_error == TOX_ERR_NEW_OK, "wrong error"); |
366 | Tox *tox2 = tox_new(0, 0, 0, &t_n_error); | 371 | Tox *tox2 = tox_new(0, &t_n_error); |
367 | ck_assert_msg(t_n_error == TOX_ERR_NEW_OK, "wrong error"); | 372 | ck_assert_msg(t_n_error == TOX_ERR_NEW_OK, "wrong error"); |
368 | Tox *tox3 = tox_new(0, 0, 0, &t_n_error); | 373 | Tox *tox3 = tox_new(0, &t_n_error); |
369 | ck_assert_msg(t_n_error == TOX_ERR_NEW_OK, "wrong error"); | 374 | ck_assert_msg(t_n_error == TOX_ERR_NEW_OK, "wrong error"); |
370 | 375 | ||
371 | ck_assert_msg(tox1 || tox2 || tox3, "Failed to create 3 tox instances"); | 376 | ck_assert_msg(tox1 || tox2 || tox3, "Failed to create 3 tox instances"); |
@@ -745,7 +750,7 @@ START_TEST(test_many_clients) | |||
745 | uint32_t to_comp = 974536; | 750 | uint32_t to_comp = 974536; |
746 | 751 | ||
747 | for (i = 0; i < NUM_TOXES; ++i) { | 752 | for (i = 0; i < NUM_TOXES; ++i) { |
748 | toxes[i] = tox_new(0, 0, 0, 0); | 753 | toxes[i] = tox_new(0, 0); |
749 | ck_assert_msg(toxes[i] != 0, "Failed to create tox instances %u", i); | 754 | ck_assert_msg(toxes[i] != 0, "Failed to create tox instances %u", i); |
750 | tox_callback_friend_request(toxes[i], accept_friend_request, &to_comp); | 755 | tox_callback_friend_request(toxes[i], accept_friend_request, &to_comp); |
751 | } | 756 | } |
@@ -832,7 +837,7 @@ START_TEST(test_many_clients_tcp) | |||
832 | opts.udp_enabled = 0; | 837 | opts.udp_enabled = 0; |
833 | } | 838 | } |
834 | 839 | ||
835 | toxes[i] = tox_new(&opts, 0, 0, 0); | 840 | toxes[i] = tox_new(&opts, 0); |
836 | ck_assert_msg(toxes[i] != 0, "Failed to create tox instances %u", i); | 841 | ck_assert_msg(toxes[i] != 0, "Failed to create tox instances %u", i); |
837 | tox_callback_friend_request(toxes[i], accept_friend_request, &to_comp); | 842 | tox_callback_friend_request(toxes[i], accept_friend_request, &to_comp); |
838 | uint8_t dpk[TOX_PUBLIC_KEY_SIZE]; | 843 | uint8_t dpk[TOX_PUBLIC_KEY_SIZE]; |
@@ -926,7 +931,7 @@ START_TEST(test_many_clients_tcp_b) | |||
926 | opts.udp_enabled = 0; | 931 | opts.udp_enabled = 0; |
927 | } | 932 | } |
928 | 933 | ||
929 | toxes[i] = tox_new(&opts, 0, 0, 0); | 934 | toxes[i] = tox_new(&opts, 0); |
930 | ck_assert_msg(toxes[i] != 0, "Failed to create tox instances %u", i); | 935 | ck_assert_msg(toxes[i] != 0, "Failed to create tox instances %u", i); |
931 | tox_callback_friend_request(toxes[i], accept_friend_request, &to_comp); | 936 | tox_callback_friend_request(toxes[i], accept_friend_request, &to_comp); |
932 | uint8_t dpk[TOX_PUBLIC_KEY_SIZE]; | 937 | uint8_t dpk[TOX_PUBLIC_KEY_SIZE]; |
@@ -1062,7 +1067,7 @@ START_TEST(test_many_group) | |||
1062 | uint32_t to_comp = 234212; | 1067 | uint32_t to_comp = 234212; |
1063 | 1068 | ||
1064 | for (i = 0; i < NUM_GROUP_TOX; ++i) { | 1069 | for (i = 0; i < NUM_GROUP_TOX; ++i) { |
1065 | toxes[i] = tox_new(0, 0, 0, 0); | 1070 | toxes[i] = tox_new(0, 0); |
1066 | ck_assert_msg(toxes[i] != 0, "Failed to create tox instances %u", i); | 1071 | ck_assert_msg(toxes[i] != 0, "Failed to create tox instances %u", i); |
1067 | tox_callback_friend_request(toxes[i], &g_accept_friend_request, &to_comp); | 1072 | tox_callback_friend_request(toxes[i], &g_accept_friend_request, &to_comp); |
1068 | tox_callback_group_invite(toxes[i], &print_group_invite_callback, &to_comp); | 1073 | 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) | |||
250 | START_TEST(test_AV_flows) | 250 | START_TEST(test_AV_flows) |
251 | { | 251 | { |
252 | long long unsigned int cur_time = time(NULL); | 252 | long long unsigned int cur_time = time(NULL); |
253 | Tox *bootstrap_node = tox_new(0, 0, 0, 0); | 253 | Tox *bootstrap_node = tox_new(0, 0); |
254 | Tox *Alice = tox_new(0, 0, 0, 0); | 254 | Tox *Alice = tox_new(0, 0); |
255 | Tox *Bob = tox_new(0, 0, 0, 0); | 255 | Tox *Bob = tox_new(0, 0); |
256 | 256 | ||
257 | ck_assert_msg(bootstrap_node || Alice || Bob, "Failed to create 3 tox instances"); | 257 | ck_assert_msg(bootstrap_node || Alice || Bob, "Failed to create 3 tox instances"); |
258 | 258 | ||
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) | |||
264 | // void test_AV_three_calls() | 264 | // void test_AV_three_calls() |
265 | { | 265 | { |
266 | long long unsigned int cur_time = time(NULL); | 266 | long long unsigned int cur_time = time(NULL); |
267 | Tox *bootstrap_node = tox_new(0, 0, 0, 0); | 267 | Tox *bootstrap_node = tox_new(0, 0); |
268 | Tox *caller = tox_new(0, 0, 0, 0); | 268 | Tox *caller = tox_new(0, 0); |
269 | Tox *callees[3] = { | 269 | Tox *callees[3] = { |
270 | tox_new(0, 0, 0, 0), | 270 | tox_new(0, 0), |
271 | tox_new(0, 0, 0, 0), | 271 | tox_new(0, 0), |
272 | tox_new(0, 0, 0, 0), | 272 | tox_new(0, 0), |
273 | }; | 273 | }; |
274 | 274 | ||
275 | 275 | ||
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 { | |||
340 | SOCKS5, | 340 | SOCKS5, |
341 | } | 341 | } |
342 | 342 | ||
343 | /** | ||
344 | * Type of savedata to create the Tox instance from. | ||
345 | */ | ||
346 | enum class SAVEDATA_TYPE { | ||
347 | /** | ||
348 | * No savedata. | ||
349 | */ | ||
350 | NONE, | ||
351 | /** | ||
352 | * Savedata is one that was obtained from ${savedata.get} | ||
353 | */ | ||
354 | TOX_SAVE, | ||
355 | /** | ||
356 | * Savedata is a secret key of length ${SECRET_KEY_SIZE} | ||
357 | */ | ||
358 | SECRET_KEY, | ||
359 | } | ||
360 | |||
343 | 361 | ||
344 | static class options { | 362 | static class options { |
345 | /** | 363 | /** |
@@ -416,6 +434,23 @@ static class options { | |||
416 | * The port to use for the TCP server. If 0, the tcp server is disabled. | 434 | * The port to use for the TCP server. If 0, the tcp server is disabled. |
417 | */ | 435 | */ |
418 | uint16_t tcp_port; | 436 | uint16_t tcp_port; |
437 | |||
438 | namespace savedata { | ||
439 | /** | ||
440 | * The type of savedata to load from. | ||
441 | */ | ||
442 | SAVEDATA_TYPE type; | ||
443 | |||
444 | /** | ||
445 | * The savedata. | ||
446 | */ | ||
447 | const uint8_t[length] data; | ||
448 | |||
449 | /** | ||
450 | * The length of the savedata. | ||
451 | */ | ||
452 | size_t length; | ||
453 | } | ||
419 | } | 454 | } |
420 | 455 | ||
421 | 456 | ||
@@ -474,21 +509,15 @@ static class options { | |||
474 | * This function will bring the instance into a valid state. Running the event | 509 | * This function will bring the instance into a valid state. Running the event |
475 | * loop with a new instance will operate correctly. | 510 | * loop with a new instance will operate correctly. |
476 | * | 511 | * |
477 | * If the data parameter is not NULL, this function will load the Tox instance | ||
478 | * from a byte array previously filled by ${savedata.get}. | ||
479 | * | ||
480 | * If loading failed or succeeded only partially, the new or partially loaded | 512 | * If loading failed or succeeded only partially, the new or partially loaded |
481 | * instance is returned and an error code is set. | 513 | * instance is returned and an error code is set. |
482 | * | 514 | * |
483 | * @param options An options object as described above. If this parameter is | 515 | * @param options An options object as described above. If this parameter is |
484 | * NULL, the default options are used. | 516 | * NULL, the default options are used. |
485 | * @param data A byte array containing data previously stored by ${savedata.get}. | ||
486 | * @param length The length of the byte array data. If this parameter is 0, the | ||
487 | * data parameter is ignored. | ||
488 | * | 517 | * |
489 | * @see $iterate for the event loop. | 518 | * @see $iterate for the event loop. |
490 | */ | 519 | */ |
491 | static this new(const options_t *options, const uint8_t[length] data) { | 520 | static this new(const options_t *options) { |
492 | NULL, | 521 | NULL, |
493 | /** | 522 | /** |
494 | * The function was unable to allocate enough memory to store the internal | 523 | * 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[]) | |||
222 | exit(0); | 222 | exit(0); |
223 | } | 223 | } |
224 | 224 | ||
225 | Tox *tox = tox_new(0, 0, 0, 0); | 225 | Tox *tox = tox_new(0, 0); |
226 | 226 | ||
227 | if (!tox) | 227 | if (!tox) |
228 | exit(1); | 228 | 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() | |||
942 | return 0; | 942 | return 0; |
943 | } | 943 | } |
944 | 944 | ||
945 | Tox *m = tox_new(0, data, size, NULL); | 945 | struct Tox_Options options; |
946 | |||
947 | tox_options_default(&options); | ||
948 | |||
949 | options.savedata_type = TOX_SAVEDATA_TYPE_TOX_SAVE; | ||
950 | |||
951 | options.savedata_data = data; | ||
952 | |||
953 | options.savedata_length = size; | ||
954 | |||
955 | Tox *m = tox_new(&options, NULL); | ||
946 | 956 | ||
947 | if (fclose(data_file) < 0) { | 957 | if (fclose(data_file) < 0) { |
948 | perror("[!] fclose failed"); | 958 | perror("[!] fclose failed"); |
@@ -953,7 +963,7 @@ static Tox *load_data() | |||
953 | return m; | 963 | return m; |
954 | } | 964 | } |
955 | 965 | ||
956 | return tox_new(0, 0, 0, NULL); | 966 | return tox_new(NULL, NULL); |
957 | } | 967 | } |
958 | 968 | ||
959 | static int save_data(Tox *m) | 969 | 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[]) | |||
95 | printf("error setting flags\n"); | 95 | printf("error setting flags\n"); |
96 | } | 96 | } |
97 | 97 | ||
98 | Tox *tox = tox_new(0, 0, 0, 0); | 98 | Tox *tox = tox_new(0, 0); |
99 | tox_callback_friend_connection_status(tox, print_online, NULL); | 99 | tox_callback_friend_connection_status(tox, print_online, NULL); |
100 | tox_callback_friend_message(tox, print_message, master); | 100 | tox_callback_friend_message(tox, print_message, master); |
101 | 101 | ||
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[]) | |||
243 | exit(0); | 243 | exit(0); |
244 | } | 244 | } |
245 | 245 | ||
246 | Tox *tox = tox_new(0, 0, 0, 0); | 246 | Tox *tox = tox_new(0, 0); |
247 | tox_callback_file_recv_chunk(tox, write_file, NULL); | 247 | tox_callback_file_recv_chunk(tox, write_file, NULL); |
248 | tox_callback_file_recv_control(tox, file_print_control, NULL); | 248 | tox_callback_file_recv_control(tox, file_print_control, NULL); |
249 | tox_callback_file_recv(tox, file_request_accept, NULL); | 249 | 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) | |||
121 | free(options); | 121 | free(options); |
122 | } | 122 | } |
123 | 123 | ||
124 | Tox *tox_new(const struct Tox_Options *options, const uint8_t *data, size_t length, TOX_ERR_NEW *error) | 124 | Tox *tox_new(const struct Tox_Options *options, TOX_ERR_NEW *error) |
125 | { | 125 | { |
126 | if (!logger_get_global()) | 126 | if (!logger_get_global()) |
127 | logger_set_global(logger_new(LOGGER_OUTPUT_FILE, LOGGER_LEVEL, "toxcore")); | 127 | logger_set_global(logger_new(LOGGER_OUTPUT_FILE, LOGGER_LEVEL, "toxcore")); |
128 | 128 | ||
129 | if (data == NULL && length != 0) { | ||
130 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_NULL); | ||
131 | return NULL; | ||
132 | } | ||
133 | |||
134 | if (data && length) { | ||
135 | if (length < TOX_ENC_SAVE_MAGIC_LENGTH) { | ||
136 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); | ||
137 | return NULL; | ||
138 | } | ||
139 | |||
140 | if (memcmp(data, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) == 0) { | ||
141 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_ENCRYPTED); | ||
142 | return NULL; | ||
143 | } | ||
144 | } | ||
145 | |||
146 | Messenger_Options m_options = {0}; | 129 | Messenger_Options m_options = {0}; |
147 | 130 | ||
131 | _Bool load_savedata_sk = 0, load_savedata_tox = 0; | ||
132 | |||
148 | if (options == NULL) { | 133 | if (options == NULL) { |
149 | m_options.ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; | 134 | m_options.ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; |
150 | } else { | 135 | } else { |
136 | if (options->savedata_type != TOX_SAVEDATA_TYPE_NONE) { | ||
137 | if (options->savedata_data == NULL || options->savedata_length == 0) { | ||
138 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); | ||
139 | return NULL; | ||
140 | } | ||
141 | } | ||
142 | |||
143 | if (options->savedata_type == TOX_SAVEDATA_TYPE_SECRET_KEY) { | ||
144 | if (options->savedata_length != TOX_SECRET_KEY_SIZE) { | ||
145 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); | ||
146 | return NULL; | ||
147 | } | ||
148 | |||
149 | load_savedata_sk = 1; | ||
150 | } else if (options->savedata_type == TOX_SAVEDATA_TYPE_TOX_SAVE) { | ||
151 | if (options->savedata_length < TOX_ENC_SAVE_MAGIC_LENGTH) { | ||
152 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); | ||
153 | return NULL; | ||
154 | } | ||
155 | |||
156 | if (memcmp(options->savedata_data, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) == 0) { | ||
157 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_ENCRYPTED); | ||
158 | return NULL; | ||
159 | } | ||
160 | |||
161 | load_savedata_tox = 1; | ||
162 | } | ||
163 | |||
151 | m_options.ipv6enabled = options->ipv6_enabled; | 164 | m_options.ipv6enabled = options->ipv6_enabled; |
152 | m_options.udp_disabled = !options->udp_enabled; | 165 | m_options.udp_disabled = !options->udp_enabled; |
153 | m_options.port_range[0] = options->start_port; | 166 | 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 | |||
208 | return NULL; | 221 | return NULL; |
209 | } | 222 | } |
210 | 223 | ||
211 | if (data && length && messenger_load(m, data, length) == -1) { | 224 | if (load_savedata_tox && messenger_load(m, options->savedata_data, options->savedata_length) == -1) { |
212 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); | 225 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); |
226 | } else if (load_savedata_sk) { | ||
227 | load_secret_key(m->net_crypto, options->savedata_data); | ||
228 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_OK); | ||
213 | } else { | 229 | } else { |
214 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_OK); | 230 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_OK); |
215 | } | 231 | } |
diff --git a/toxcore/tox.h b/toxcore/tox.h index 4afdf7c3..b556d138 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h | |||
@@ -352,6 +352,29 @@ typedef enum TOX_PROXY_TYPE { | |||
352 | 352 | ||
353 | 353 | ||
354 | /** | 354 | /** |
355 | * Type of savedata to create the Tox instance from. | ||
356 | */ | ||
357 | typedef enum TOX_SAVEDATA_TYPE { | ||
358 | |||
359 | /** | ||
360 | * No savedata. | ||
361 | */ | ||
362 | TOX_SAVEDATA_TYPE_NONE, | ||
363 | |||
364 | /** | ||
365 | * Savedata is one that was obtained from tox_get_savedata | ||
366 | */ | ||
367 | TOX_SAVEDATA_TYPE_TOX_SAVE, | ||
368 | |||
369 | /** | ||
370 | * Savedata is a secret key of length TOX_SECRET_KEY_SIZE | ||
371 | */ | ||
372 | TOX_SAVEDATA_TYPE_SECRET_KEY, | ||
373 | |||
374 | } TOX_SAVEDATA_TYPE; | ||
375 | |||
376 | |||
377 | /** | ||
355 | * This struct contains all the startup options for Tox. You can either allocate | 378 | * This struct contains all the startup options for Tox. You can either allocate |
356 | * this object yourself, and pass it to tox_options_default, or call | 379 | * this object yourself, and pass it to tox_options_default, or call |
357 | * tox_options_new to get a new default options object. | 380 | * tox_options_new to get a new default options object. |
@@ -432,6 +455,24 @@ struct Tox_Options { | |||
432 | */ | 455 | */ |
433 | uint16_t tcp_port; | 456 | uint16_t tcp_port; |
434 | 457 | ||
458 | |||
459 | /** | ||
460 | * The type of savedata to load from. | ||
461 | */ | ||
462 | TOX_SAVEDATA_TYPE savedata_type; | ||
463 | |||
464 | |||
465 | /** | ||
466 | * The savedata. | ||
467 | */ | ||
468 | const uint8_t *savedata_data; | ||
469 | |||
470 | |||
471 | /** | ||
472 | * The length of the savedata. | ||
473 | */ | ||
474 | size_t savedata_length; | ||
475 | |||
435 | }; | 476 | }; |
436 | 477 | ||
437 | 478 | ||
@@ -561,21 +602,15 @@ typedef enum TOX_ERR_NEW { | |||
561 | * This function will bring the instance into a valid state. Running the event | 602 | * This function will bring the instance into a valid state. Running the event |
562 | * loop with a new instance will operate correctly. | 603 | * loop with a new instance will operate correctly. |
563 | * | 604 | * |
564 | * If the data parameter is not NULL, this function will load the Tox instance | ||
565 | * from a byte array previously filled by tox_get_savedata. | ||
566 | * | ||
567 | * If loading failed or succeeded only partially, the new or partially loaded | 605 | * If loading failed or succeeded only partially, the new or partially loaded |
568 | * instance is returned and an error code is set. | 606 | * instance is returned and an error code is set. |
569 | * | 607 | * |
570 | * @param options An options object as described above. If this parameter is | 608 | * @param options An options object as described above. If this parameter is |
571 | * NULL, the default options are used. | 609 | * NULL, the default options are used. |
572 | * @param data A byte array containing data previously stored by tox_get_savedata. | ||
573 | * @param length The length of the byte array data. If this parameter is 0, the | ||
574 | * data parameter is ignored. | ||
575 | * | 610 | * |
576 | * @see tox_iterate for the event loop. | 611 | * @see tox_iterate for the event loop. |
577 | */ | 612 | */ |
578 | Tox *tox_new(const struct Tox_Options *options, const uint8_t *data, size_t length, TOX_ERR_NEW *error); | 613 | Tox *tox_new(const struct Tox_Options *options, TOX_ERR_NEW *error); |
579 | 614 | ||
580 | /** | 615 | /** |
581 | * Releases all resources associated with the Tox instance and disconnects from | 616 | * Releases all resources associated with the Tox instance and disconnects from |