diff --git a/auto_tests/conference_av_test.c b/auto_tests/conference_av_test.c index bf4adfe8a1..9430534eb0 100644 --- a/auto_tests/conference_av_test.c +++ b/auto_tests/conference_av_test.c @@ -329,7 +329,7 @@ static void run_conference_tests(Tox **toxes, State *state) for (uint32_t i = 0; i < NUM_AV_GROUP_TOX; ++i) { if (restarting[i]) { - struct Tox_Options *const options = tox_options_new(nullptr); + Tox_Options *const options = tox_options_new(nullptr); tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE); tox_options_set_savedata_data(options, save[i], save_size[i]); toxes[i] = tox_new_log(options, nullptr, &state[i].index); diff --git a/auto_tests/conference_invite_merge_test.c b/auto_tests/conference_invite_merge_test.c index 0e285c1ef5..95c393344a 100644 --- a/auto_tests/conference_invite_merge_test.c +++ b/auto_tests/conference_invite_merge_test.c @@ -86,7 +86,7 @@ static void reload(Tox **toxes, State *state, uint32_t n) fprintf(stderr, "Reloading #%u\n", state[n].index); ck_assert(state[n].save_state != nullptr); - struct Tox_Options *const options = tox_options_new(nullptr); + Tox_Options *const options = tox_options_new(nullptr); ck_assert(options != nullptr); tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE); tox_options_set_savedata_data(options, state[n].save_state, state[n].save_size); diff --git a/auto_tests/conference_test.c b/auto_tests/conference_test.c index ac1bdaa8ec..2179528804 100644 --- a/auto_tests/conference_test.c +++ b/auto_tests/conference_test.c @@ -245,7 +245,7 @@ static void run_conference_tests(Tox **toxes, State *state) for (uint32_t i = 0; i < NUM_GROUP_TOX; ++i) { if (restarting[i]) { - struct Tox_Options *const options = tox_options_new(nullptr); + Tox_Options *const options = tox_options_new(nullptr); ck_assert(options != nullptr); tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE); tox_options_set_savedata_data(options, save[i], save_size[i]); diff --git a/auto_tests/encryptsave_test.c b/auto_tests/encryptsave_test.c index c764b3d574..5aaeb5b8ae 100644 --- a/auto_tests/encryptsave_test.c +++ b/auto_tests/encryptsave_test.c @@ -71,7 +71,7 @@ static void test_save_friend(void) ck_assert_msg(ret, "failed to encrypted save: %d", error1); ck_assert_msg(tox_is_data_encrypted(enc_data), "magic number missing"); - struct Tox_Options *options = tox_options_new(nullptr); + Tox_Options *options = tox_options_new(nullptr); ck_assert(options != nullptr); tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE); tox_options_set_savedata_data(options, enc_data, size2); diff --git a/auto_tests/file_saving_test.c b/auto_tests/file_saving_test.c index 09031dee49..4190a544cd 100644 --- a/auto_tests/file_saving_test.c +++ b/auto_tests/file_saving_test.c @@ -25,7 +25,7 @@ static const char *savefile = "./save"; static void save_data_encrypted(void) { - struct Tox_Options *options = tox_options_new(nullptr); + Tox_Options *options = tox_options_new(nullptr); Tox *t = tox_new_log(options, nullptr, nullptr); tox_options_free(options); @@ -79,7 +79,7 @@ static void load_data_decrypted(void) ck_assert_msg(tox_pass_decrypt(cipher, size, (const uint8_t *)pphrase, strlen(pphrase), clear, &derr), "Could not decrypt, error code %d.", derr); - struct Tox_Options *options = tox_options_new(nullptr); + Tox_Options *options = tox_options_new(nullptr); ck_assert(options != nullptr); tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE); diff --git a/auto_tests/invalid_tcp_proxy_test.c b/auto_tests/invalid_tcp_proxy_test.c index 1b8afa97ef..cec4ed8bc2 100644 --- a/auto_tests/invalid_tcp_proxy_test.c +++ b/auto_tests/invalid_tcp_proxy_test.c @@ -20,7 +20,7 @@ int main(void) { setvbuf(stdout, nullptr, _IONBF, 0); - struct Tox_Options *opts = tox_options_new(nullptr); + Tox_Options *opts = tox_options_new(nullptr); tox_options_set_udp_enabled(opts, false); tox_options_set_proxy_type(opts, TOX_PROXY_TYPE_SOCKS5); tox_options_set_proxy_host(opts, "localhost"); diff --git a/auto_tests/invalid_udp_proxy_test.c b/auto_tests/invalid_udp_proxy_test.c index 5bb8667ce5..094957189e 100644 --- a/auto_tests/invalid_udp_proxy_test.c +++ b/auto_tests/invalid_udp_proxy_test.c @@ -20,7 +20,7 @@ int main(void) { setvbuf(stdout, nullptr, _IONBF, 0); - struct Tox_Options *opts = tox_options_new(nullptr); + Tox_Options *opts = tox_options_new(nullptr); tox_options_set_udp_enabled(opts, true); tox_options_set_proxy_type(opts, TOX_PROXY_TYPE_SOCKS5); tox_options_set_proxy_host(opts, "localhost"); diff --git a/auto_tests/save_compatibility_test.c b/auto_tests/save_compatibility_test.c index 437f3971aa..ce413ed408 100644 --- a/auto_tests/save_compatibility_test.c +++ b/auto_tests/save_compatibility_test.c @@ -69,20 +69,19 @@ static uint8_t *read_save(const char *save_path, size_t *length) static void test_save_compatibility(const char *save_path) { - struct Tox_Options options = {0}; - tox_options_default(&options); + Tox_Options *options = tox_options_new(nullptr); + ck_assert(options != nullptr); size_t size = 0; uint8_t *save_data = read_save(save_path, &size); ck_assert_msg(save_data != nullptr, "error while reading save file '%s'", save_path); - options.savedata_data = save_data; - options.savedata_length = size; - options.savedata_type = TOX_SAVEDATA_TYPE_TOX_SAVE; + tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE); + tox_options_set_savedata_data(options, save_data, size); size_t index = 0; Tox_Err_New err; - Tox *tox = tox_new_log(&options, &err, &index); + Tox *tox = tox_new_log(options, &err, &index); ck_assert_msg(tox, "failed to create tox, error number: %d", err); free(save_data); @@ -128,6 +127,7 @@ static void test_save_compatibility(const char *save_path) tox_iterate(tox, nullptr); tox_kill(tox); + tox_options_free(options); } int main(int argc, char *argv[]) diff --git a/auto_tests/save_friend_test.c b/auto_tests/save_friend_test.c index 110ad2bf15..f72e0d2020 100644 --- a/auto_tests/save_friend_test.c +++ b/auto_tests/save_friend_test.c @@ -111,7 +111,7 @@ int main(void) VLA(uint8_t, savedata, save_size); tox_get_savedata(tox1, savedata); - struct Tox_Options *const options = tox_options_new(nullptr); + Tox_Options *const options = tox_options_new(nullptr); tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE); tox_options_set_savedata_data(options, savedata, save_size); diff --git a/auto_tests/save_load_test.c b/auto_tests/save_load_test.c index 5dee55bc14..3bd817a640 100644 --- a/auto_tests/save_load_test.c +++ b/auto_tests/save_load_test.c @@ -52,7 +52,7 @@ static void tox_connection_status(Tox *tox, Tox_Connection connection_status, vo * b) a saved state can be loaded back successfully * c) a second save is of equal size * d) the second save is of equal content */ -static void reload_tox(Tox **tox, struct Tox_Options *const in_opts, void *user_data) +static void reload_tox(Tox **tox, Tox_Options *const in_opts, void *user_data) { const size_t extra = 64; const size_t save_size1 = tox_get_savedata_size(*tox); @@ -71,7 +71,7 @@ static void reload_tox(Tox **tox, struct Tox_Options *const in_opts, void *user_ ck_assert_msg(buffer[extra + save_size1 + i] == 0xCD, "Buffer overwritten from tox_get_savedata() @%u", (unsigned)i); } - struct Tox_Options *const options = (in_opts == nullptr) ? tox_options_new(nullptr) : in_opts; + Tox_Options *const options = (in_opts == nullptr) ? tox_options_new(nullptr) : in_opts; tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE); @@ -108,20 +108,20 @@ static void test_few_clients(void) uint32_t index[] = { 1, 2, 3 }; time_t con_time = 0, cur_time = time(nullptr); - struct Tox_Options *opts1 = tox_options_new(nullptr); + Tox_Options *opts1 = tox_options_new(nullptr); tox_options_set_tcp_port(opts1, TCP_RELAY_PORT); Tox_Err_New t_n_error; Tox *tox1 = tox_new_log(opts1, &t_n_error, &index[0]); ck_assert_msg(t_n_error == TOX_ERR_NEW_OK, "Failed to create tox instance: %d", t_n_error); tox_options_free(opts1); - struct Tox_Options *opts2 = tox_options_new(nullptr); + Tox_Options *opts2 = tox_options_new(nullptr); tox_options_set_udp_enabled(opts2, false); tox_options_set_local_discovery_enabled(opts2, false); Tox *tox2 = tox_new_log(opts2, &t_n_error, &index[1]); ck_assert_msg(t_n_error == TOX_ERR_NEW_OK, "Failed to create tox instance: %d", t_n_error); - struct Tox_Options *opts3 = tox_options_new(nullptr); + Tox_Options *opts3 = tox_options_new(nullptr); tox_options_set_local_discovery_enabled(opts3, false); Tox *tox3 = tox_new_log(opts3, &t_n_error, &index[2]); ck_assert_msg(t_n_error == TOX_ERR_NEW_OK, "Failed to create tox instance: %d", t_n_error); diff --git a/auto_tests/tox_many_tcp_test.c b/auto_tests/tox_many_tcp_test.c index 9275704182..94000fb9b9 100644 --- a/auto_tests/tox_many_tcp_test.c +++ b/auto_tests/tox_many_tcp_test.c @@ -50,7 +50,7 @@ START_TEST(test_many_clients_tcp) uint32_t to_comp = 974536; for (i = 0; i < NUM_TOXES_TCP; ++i) { - struct Tox_Options *opts = tox_options_new(nullptr); + Tox_Options *opts = tox_options_new(nullptr); if (i == 0) { tox_options_set_tcp_port(opts, TCP_RELAY_PORT); @@ -144,7 +144,7 @@ START_TEST(test_many_clients_tcp_b) uint32_t to_comp = 974536; for (i = 0; i < NUM_TOXES_TCP; ++i) { - struct Tox_Options *opts = tox_options_new(nullptr); + Tox_Options *opts = tox_options_new(nullptr); if (i < NUM_TCP_RELAYS) { tox_options_set_tcp_port(opts, TCP_RELAY_PORT + i); diff --git a/auto_tests/tox_one_test.c b/auto_tests/tox_one_test.c index ebfe46fe11..2ea038f13d 100644 --- a/auto_tests/tox_one_test.c +++ b/auto_tests/tox_one_test.c @@ -82,7 +82,7 @@ static void test_one(void) tox_kill(tox2); Tox_Err_New err_n; - struct Tox_Options *options = tox_options_new(nullptr); + Tox_Options *options = tox_options_new(nullptr); ck_assert(options != nullptr); tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE); tox_options_set_savedata_data(options, data, save_size); @@ -111,7 +111,10 @@ static void test_one(void) tox_self_get_secret_key(tox2, sk); tox_kill(tox2); - tox_options_default(options); + tox_options_free(options); + options = tox_options_new(nullptr); + ck_assert(options != nullptr); + tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_SECRET_KEY); tox_options_set_savedata_data(options, sk, sizeof(sk)); tox2 = tox_new_log(options, &err_n, &index[1]); diff --git a/testing/afl_toxsave.c b/testing/afl_toxsave.c index c254111b06..3624845674 100644 --- a/testing/afl_toxsave.c +++ b/testing/afl_toxsave.c @@ -29,7 +29,7 @@ int main(int argc, char **argv) Tox_Err_Options_New error_options; - struct Tox_Options *tox_options = tox_options_new(&error_options); + Tox_Options *tox_options = tox_options_new(&error_options); if (error_options != TOX_ERR_OPTIONS_NEW_OK) { free(buffer); diff --git a/testing/misc_tools.c b/testing/misc_tools.c index 1ff75153cc..2b539fc3f0 100644 --- a/testing/misc_tools.c +++ b/testing/misc_tools.c @@ -174,9 +174,9 @@ void print_debug_log(Tox *m, Tox_Log_Level level, const char *file, uint32_t lin } } -Tox *tox_new_log_lan(struct Tox_Options *options, Tox_Err_New *err, void *log_user_data, bool lan_discovery) +Tox *tox_new_log_lan(Tox_Options *options, Tox_Err_New *err, void *log_user_data, bool lan_discovery) { - struct Tox_Options *log_options = options; + Tox_Options *log_options = options; if (log_options == nullptr) { log_options = tox_options_new(nullptr); @@ -198,7 +198,7 @@ Tox *tox_new_log_lan(struct Tox_Options *options, Tox_Err_New *err, void *log_us return tox; } -Tox *tox_new_log(struct Tox_Options *options, Tox_Err_New *err, void *log_user_data) +Tox *tox_new_log(Tox_Options *options, Tox_Err_New *err, void *log_user_data) { return tox_new_log_lan(options, err, log_user_data, false); } diff --git a/testing/misc_tools.h b/testing/misc_tools.h index b9c3ca3e36..d847c740eb 100644 --- a/testing/misc_tools.h +++ b/testing/misc_tools.h @@ -20,8 +20,8 @@ int cmdline_parsefor_ipv46(int argc, char **argv, bool *ipv6enabled); void print_debug_log(Tox *m, Tox_Log_Level level, const char *file, uint32_t line, const char *func, const char *message, void *user_data); -Tox *tox_new_log(struct Tox_Options *options, Tox_Err_New *err, void *log_user_data); -Tox *tox_new_log_lan(struct Tox_Options *options, Tox_Err_New *err, void *log_user_data, bool lan_discovery); +Tox *tox_new_log(Tox_Options *options, Tox_Err_New *err, void *log_user_data); +Tox *tox_new_log_lan(Tox_Options *options, Tox_Err_New *err, void *log_user_data, bool lan_discovery); int use_test_rng(uint32_t seed); diff --git a/toxcore/tox.c b/toxcore/tox.c index eaf5516cf3..b37fdd2b0a 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -373,7 +373,7 @@ static int tox_load(Tox *tox, const uint8_t *data, uint32_t length) } -Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) +Tox *tox_new(const Tox_Options *options, Tox_Err_New *error) { Tox *tox = (Tox *)calloc(1, sizeof(Tox)); @@ -387,7 +387,7 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) bool load_savedata_sk = false; bool load_savedata_tox = false; - struct Tox_Options *default_options = nullptr; + Tox_Options *default_options = nullptr; if (options == nullptr) { Tox_Err_Options_New err; @@ -406,7 +406,7 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) } } - const struct Tox_Options *const opts = options != nullptr ? options : default_options; + const Tox_Options *const opts = options != nullptr ? options : default_options; assert(opts != nullptr); if (tox_options_get_savedata_type(opts) != TOX_SAVEDATA_TYPE_NONE) { diff --git a/toxcore/tox.h b/toxcore/tox.h index 72f36f7dba..cc9d67d812 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -516,247 +516,77 @@ typedef void tox_log_cb(Tox *tox, Tox_Log_Level level, const char *file, uint32_ /** - * This struct contains all the startup options for Tox. You must tox_options_new to - * allocate an object of this type. - * - * WARNING: Although this struct happens to be visible in the API, it is - * effectively private. Do not allocate this yourself or access members - * directly, as it *will* break binary compatibility frequently. - * - * @deprecated The memory layout of this struct (size, alignment, and field - * order) is not part of the ABI. To remain compatible, prefer to use tox_options_new to - * allocate the object and accessor functions to set the members. The struct - * will become opaque (i.e. the definition will become private) in v0.3.0. + * This struct contains all the startup options for Tox. You must use tox_options_new to + * allocate an object of this type and use tox_options_free to free it. */ -struct Tox_Options { +#ifndef TOX_OPTIONS_DEFINED +#define TOX_OPTIONS_DEFINED +typedef struct Tox_Options Tox_Options; +#endif /* TOX_OPTIONS_DEFINED */ - /** - * The type of socket to create. - * - * If this is set to false, an IPv4 socket is created, which subsequently - * only allows IPv4 communication. - * If it is set to true, an IPv6 socket is created, allowing both IPv4 and - * IPv6 communication. - */ - bool ipv6_enabled; +bool tox_options_get_ipv6_enabled(const Tox_Options *options); +void tox_options_set_ipv6_enabled(Tox_Options *options, bool ipv6_enabled); - /** - * Enable the use of UDP communication when available. - * - * Setting this to false will force Tox to use TCP only. Communications will - * need to be relayed through a TCP relay node, potentially slowing them down. - * - * If a proxy is enabled, UDP will be disabled if either toxcore or the - * proxy don't support proxying UDP messages. - */ - bool udp_enabled; +bool tox_options_get_udp_enabled(const Tox_Options *options); +void tox_options_set_udp_enabled(Tox_Options *options, bool udp_enabled); - /** - * Enable local network peer discovery. - * - * Disabling this will cause Tox to not look for peers on the local network. - */ - bool local_discovery_enabled; +bool tox_options_get_local_discovery_enabled(const Tox_Options *options); +void tox_options_set_local_discovery_enabled(Tox_Options *options, bool local_discovery_enabled); - /** - * Pass communications through a proxy. - */ - Tox_Proxy_Type proxy_type; +Tox_Proxy_Type tox_options_get_proxy_type(const Tox_Options *options); +void tox_options_set_proxy_type(Tox_Options *options, Tox_Proxy_Type type); - /** - * The IP address or DNS name of the proxy to be used. - * - * If used, this must be non-NULL and be a valid DNS name. The name must not - * exceed TOX_MAX_HOSTNAME_LENGTH characters, and be in a NUL-terminated C string - * format (TOX_MAX_HOSTNAME_LENGTH includes the NUL byte). - * - * This member is ignored (it can be NULL) if proxy_type is TOX_PROXY_TYPE_NONE. - * - * The data pointed at by this member is owned by the user, so must - * outlive the options object. - */ - const char *proxy_host; +const char *tox_options_get_proxy_host(const Tox_Options *options); +void tox_options_set_proxy_host(Tox_Options *options, const char *host); - /** - * The port to use to connect to the proxy server. - * - * Ports must be in the range (1, 65535). The value is ignored if - * proxy_type is TOX_PROXY_TYPE_NONE. - */ - uint16_t proxy_port; +uint16_t tox_options_get_proxy_port(const Tox_Options *options); +void tox_options_set_proxy_port(Tox_Options *options, uint16_t port); - /** - * The start port of the inclusive port range to attempt to use. - * - * If both start_port and end_port are 0, the default port range will be - * used: `[33445, 33545]`. - * - * If either start_port or end_port is 0 while the other is non-zero, the - * non-zero port will be the only port in the range. - * - * Having start_port > end_port will yield the same behavior as if start_port - * and end_port were swapped. - */ - uint16_t start_port; +uint16_t tox_options_get_start_port(const Tox_Options *options); +void tox_options_set_start_port(Tox_Options *options, uint16_t start_port); - /** - * The end port of the inclusive port range to attempt to use. - */ - uint16_t end_port; +uint16_t tox_options_get_end_port(const Tox_Options *options); +void tox_options_set_end_port(Tox_Options *options, uint16_t end_port); - /** - * The port to use for the TCP server (relay). If 0, the TCP server is - * disabled. - * - * Enabling it is not required for Tox to function properly. - * - * When enabled, your Tox instance can act as a TCP relay for other Tox - * instance. This leads to increased traffic, thus when writing a client - * it is recommended to enable TCP server only if the user has an option - * to disable it. - */ - uint16_t tcp_port; +uint16_t tox_options_get_tcp_port(const Tox_Options *options); +void tox_options_set_tcp_port(Tox_Options *options, uint16_t tcp_port); - /** - * Enables or disables UDP hole-punching in toxcore. (Default: enabled). - */ - bool hole_punching_enabled; +bool tox_options_get_hole_punching_enabled(const Tox_Options *options); +void tox_options_set_hole_punching_enabled(Tox_Options *options, bool hole_punching_enabled); - /** - * The type of savedata to load from. - */ - Tox_Savedata_Type savedata_type; +Tox_Savedata_Type tox_options_get_savedata_type(const Tox_Options *options); +void tox_options_set_savedata_type(Tox_Options *options, Tox_Savedata_Type type); - /** - * The savedata. - * - * The data pointed at by this member is owned by the user, so must - * outlive the options object. - */ - const uint8_t *savedata_data; +const uint8_t *tox_options_get_savedata_data(const Tox_Options *options); +void tox_options_set_savedata_data(Tox_Options *options, const uint8_t *data, size_t length); - /** - * The length of the savedata. - */ - size_t savedata_length; +size_t tox_options_get_savedata_length(const Tox_Options *options); +void tox_options_set_savedata_length(Tox_Options *options, size_t length); - /** - * Logging callback for the new tox instance. - */ - tox_log_cb *log_callback; +tox_log_cb *tox_options_get_log_callback(const Tox_Options *options); +void tox_options_set_log_callback(Tox_Options *options, tox_log_cb *callback); - /** - * User data pointer passed to the logging callback. - */ - void *log_user_data; +void *tox_options_get_log_user_data(const Tox_Options *options); +void tox_options_set_log_user_data(Tox_Options *options, void *user_data); - /** - * These options are experimental, so avoid writing code that depends on - * them. Options marked "experimental" may change their behaviour or go away - * entirely in the future, or may be renamed to something non-experimental - * if they become part of the supported API. - */ - /** - * Make public API functions thread-safe using a per-instance lock. - * - * Default: false. - */ - bool experimental_thread_safety; - -}; - - -bool tox_options_get_ipv6_enabled(const struct Tox_Options *options); - -void tox_options_set_ipv6_enabled(struct Tox_Options *options, bool ipv6_enabled); - -bool tox_options_get_udp_enabled(const struct Tox_Options *options); - -void tox_options_set_udp_enabled(struct Tox_Options *options, bool udp_enabled); - -bool tox_options_get_local_discovery_enabled(const struct Tox_Options *options); - -void tox_options_set_local_discovery_enabled(struct Tox_Options *options, bool local_discovery_enabled); - -Tox_Proxy_Type tox_options_get_proxy_type(const struct Tox_Options *options); - -void tox_options_set_proxy_type(struct Tox_Options *options, Tox_Proxy_Type type); - -const char *tox_options_get_proxy_host(const struct Tox_Options *options); - -void tox_options_set_proxy_host(struct Tox_Options *options, const char *host); - -uint16_t tox_options_get_proxy_port(const struct Tox_Options *options); - -void tox_options_set_proxy_port(struct Tox_Options *options, uint16_t port); - -uint16_t tox_options_get_start_port(const struct Tox_Options *options); - -void tox_options_set_start_port(struct Tox_Options *options, uint16_t start_port); - -uint16_t tox_options_get_end_port(const struct Tox_Options *options); +bool tox_options_get_experimental_thread_safety(const Tox_Options *options); -void tox_options_set_end_port(struct Tox_Options *options, uint16_t end_port); - -uint16_t tox_options_get_tcp_port(const struct Tox_Options *options); - -void tox_options_set_tcp_port(struct Tox_Options *options, uint16_t tcp_port); - -bool tox_options_get_hole_punching_enabled(const struct Tox_Options *options); - -void tox_options_set_hole_punching_enabled(struct Tox_Options *options, bool hole_punching_enabled); - -Tox_Savedata_Type tox_options_get_savedata_type(const struct Tox_Options *options); - -void tox_options_set_savedata_type(struct Tox_Options *options, Tox_Savedata_Type type); - -const uint8_t *tox_options_get_savedata_data(const struct Tox_Options *options); - -void tox_options_set_savedata_data(struct Tox_Options *options, const uint8_t *data, size_t length); - -size_t tox_options_get_savedata_length(const struct Tox_Options *options); - -void tox_options_set_savedata_length(struct Tox_Options *options, size_t length); - -tox_log_cb *tox_options_get_log_callback(const struct Tox_Options *options); - -void tox_options_set_log_callback(struct Tox_Options *options, tox_log_cb *callback); - -void *tox_options_get_log_user_data(const struct Tox_Options *options); - -void tox_options_set_log_user_data(struct Tox_Options *options, void *user_data); - -bool tox_options_get_experimental_thread_safety(const struct Tox_Options *options); - -void tox_options_set_experimental_thread_safety(struct Tox_Options *options, bool thread_safety); - -/** - * Initialises a Tox_Options object with the default options. - * - * The result of this function is independent of the original options. All - * values will be overwritten, no values will be read (so it is permissible - * to pass an uninitialised object). - * - * If options is NULL, this function has no effect. - * - * @param options An options object to be filled with default options. - */ -void tox_options_default(struct Tox_Options *options); +void tox_options_set_experimental_thread_safety(Tox_Options *options, bool thread_safety); typedef enum Tox_Err_Options_New { @@ -783,7 +613,7 @@ typedef enum Tox_Err_Options_New { * * @return A new Tox_Options object with default options or NULL on failure. */ -struct Tox_Options *tox_options_new(Tox_Err_Options_New *error); +Tox_Options *tox_options_new(Tox_Err_Options_New *error); /** * Releases all resources associated with an options objects. @@ -791,7 +621,7 @@ struct Tox_Options *tox_options_new(Tox_Err_Options_New *error); * Passing a pointer that was not returned by tox_options_new results in * undefined behaviour. */ -void tox_options_free(struct Tox_Options *options); +void tox_options_free(Tox_Options *options); /******************************************************************************* @@ -881,7 +711,7 @@ typedef enum Tox_Err_New { * * @return A new Tox instance pointer on success or NULL on failure. */ -Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error); +Tox *tox_new(const Tox_Options *options, Tox_Err_New *error); /** * Releases all resources associated with the Tox instance and disconnects from diff --git a/toxcore/tox_api.c b/toxcore/tox_api.c index fe37a7c3ec..a9df563102 100644 --- a/toxcore/tox_api.c +++ b/toxcore/tox_api.c @@ -2,6 +2,7 @@ * Copyright © 2016-2021 The TokTok team. */ #include "tox.h" +#include "tox_private.h" #include "ccompat.h" @@ -44,11 +45,11 @@ CONST_FUNCTION(max_hostname_length, MAX_HOSTNAME_LENGTH) #define ACCESSORS(type, ns, name) \ -type tox_options_get_##ns##name(const struct Tox_Options *options) \ +type tox_options_get_##ns##name(const Tox_Options *options) \ { \ return options->ns##name; \ } \ -void tox_options_set_##ns##name(struct Tox_Options *options, type name) \ +void tox_options_set_##ns##name(Tox_Options *options, type name) \ { \ options->ns##name = name; \ } @@ -71,46 +72,46 @@ ACCESSORS(bool,, experimental_thread_safety) //!TOKSTYLE+ -const uint8_t *tox_options_get_savedata_data(const struct Tox_Options *options) +const uint8_t *tox_options_get_savedata_data(const Tox_Options *options) { return options->savedata_data; } -void tox_options_set_savedata_data(struct Tox_Options *options, const uint8_t *data, size_t length) +void tox_options_set_savedata_data(Tox_Options *options, const uint8_t *data, size_t length) { options->savedata_data = data; options->savedata_length = length; } -void tox_options_default(struct Tox_Options *options) +Tox_Options *tox_options_new(Tox_Err_Options_New *error) { - if (options) { - struct Tox_Options default_options = { 0 }; - *options = default_options; - tox_options_set_ipv6_enabled(options, true); - tox_options_set_udp_enabled(options, true); - tox_options_set_proxy_type(options, TOX_PROXY_TYPE_NONE); - tox_options_set_hole_punching_enabled(options, true); - tox_options_set_local_discovery_enabled(options, true); - tox_options_set_experimental_thread_safety(options, false); + Tox_Options *options = (Tox_Options *)malloc(sizeof(Tox_Options)); + + if (options == nullptr) { + SET_ERROR_PARAMETER(error, TOX_ERR_OPTIONS_NEW_MALLOC); + return nullptr; } -} -struct Tox_Options *tox_options_new(Tox_Err_Options_New *error) -{ - struct Tox_Options *options = (struct Tox_Options *)malloc(sizeof(struct Tox_Options)); + Tox_Options default_options = { 0 }; - if (options) { - tox_options_default(options); - SET_ERROR_PARAMETER(error, TOX_ERR_OPTIONS_NEW_OK); - return options; - } + *options = default_options; + + tox_options_set_ipv6_enabled(options, true); + + tox_options_set_udp_enabled(options, true); + + tox_options_set_proxy_type(options, TOX_PROXY_TYPE_NONE); + + tox_options_set_hole_punching_enabled(options, true); + + tox_options_set_local_discovery_enabled(options, true); + + tox_options_set_experimental_thread_safety(options, false); - SET_ERROR_PARAMETER(error, TOX_ERR_OPTIONS_NEW_MALLOC); - return nullptr; + return options; } -void tox_options_free(struct Tox_Options *options) +void tox_options_free(Tox_Options *options) { free(options); } diff --git a/toxcore/tox_private.h b/toxcore/tox_private.h index 593282f7c8..2aa3274e30 100644 --- a/toxcore/tox_private.h +++ b/toxcore/tox_private.h @@ -37,6 +37,150 @@ void tox_callback_friend_lossless_packet_per_pktid(Tox *tox, tox_friend_lossless void tox_set_av_object(Tox *tox, void *object); void *tox_get_av_object(const Tox *tox); +struct Tox_Options { + /** + * The type of socket to create. + * + * If this is set to false, an IPv4 socket is created, which subsequently + * only allows IPv4 communication. + * If it is set to true, an IPv6 socket is created, allowing both IPv4 and + * IPv6 communication. + */ + bool ipv6_enabled; + + + /** + * Enable the use of UDP communication when available. + * + * Setting this to false will force Tox to use TCP only. Communications will + * need to be relayed through a TCP relay node, potentially slowing them down. + * + * If a proxy is enabled, UDP will be disabled if either toxcore or the + * proxy don't support proxying UDP messages. + */ + bool udp_enabled; + + + /** + * Enable local network peer discovery. + * + * Disabling this will cause Tox to not look for peers on the local network. + */ + bool local_discovery_enabled; + + + /** + * Pass communications through a proxy. + */ + Tox_Proxy_Type proxy_type; + + + /** + * The IP address or DNS name of the proxy to be used. + * + * If used, this must be non-NULL and be a valid DNS name. The name must not + * exceed TOX_MAX_HOSTNAME_LENGTH characters, and be in a NUL-terminated C string + * format (TOX_MAX_HOSTNAME_LENGTH includes the NUL byte). + * + * This member is ignored (it can be NULL) if proxy_type is TOX_PROXY_TYPE_NONE. + */ + const char *proxy_host; + + + /** + * The port to use to connect to the proxy server. + * + * Ports must be in the range (1, 65535). The value is ignored if + * proxy_type is TOX_PROXY_TYPE_NONE. + */ + uint16_t proxy_port; + + + /** + * The start port of the inclusive port range to attempt to use. + * + * If both start_port and end_port are 0, the default port range will be + * used: `[33445, 33545]`. + * + * If either start_port or end_port is 0 while the other is non-zero, the + * non-zero port will be the only port in the range. + * + * Having start_port > end_port will yield the same behavior as if start_port + * and end_port were swapped. + */ + uint16_t start_port; + + + /** + * The end port of the inclusive port range to attempt to use. + */ + uint16_t end_port; + + + /** + * The port to use for the TCP server (relay). If 0, the TCP server is + * disabled. + * + * Enabling it is not required for Tox to function properly. + * + * When enabled, your Tox instance can act as a TCP relay for other Tox + * instance. This leads to increased traffic, thus when writing a client + * it is recommended to enable TCP server only if the user has an option + * to disable it. + */ + uint16_t tcp_port; + + + /** + * Enables or disables UDP hole-punching in toxcore. (Default: enabled). + */ + bool hole_punching_enabled; + + + /** + * 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; + + + /** + * Logging callback for the new tox instance. + */ + tox_log_cb *log_callback; + + + /** + * User data pointer passed to the logging callback. + */ + void *log_user_data; + + + /** + * These options are experimental, so avoid writing code that depends on + * them. Options marked "experimental" may change their behaviour or go away + * entirely in the future, or may be renamed to something non-experimental + * if they become part of the supported API. + */ + /** + * Make public API functions thread-safe using a per-instance lock. + * + * Default: false. + */ + bool experimental_thread_safety; +}; + #ifdef __cplusplus } #endif