summaryrefslogtreecommitdiff
path: root/toxcore/tox_api.c
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2016-12-15 17:35:54 +0000
committeriphydf <iphydf@users.noreply.github.com>2016-12-22 10:53:39 +0000
commitdb71602731da030c999f2821b2b4a64611b90b99 (patch)
treed90d14a7b1a467111d75984137e9ca41fc02abe7 /toxcore/tox_api.c
parent2328cb74abccd563f0cd8d14d30e5314822d321e (diff)
Use `tox_options_set_*` instead of direct member access.
Also added a `tox_options_copy` function for cloning an options object. This can be useful when creating several Tox instances with slightly varying options.
Diffstat (limited to 'toxcore/tox_api.c')
-rw-r--r--toxcore/tox_api.c102
1 files changed, 102 insertions, 0 deletions
diff --git a/toxcore/tox_api.c b/toxcore/tox_api.c
new file mode 100644
index 00000000..af93bef4
--- /dev/null
+++ b/toxcore/tox_api.c
@@ -0,0 +1,102 @@
1#include "tox.h"
2
3#include <stdlib.h>
4#include <string.h>
5
6#define SET_ERROR_PARAMETER(param, x) {if(param) {*param = x;}}
7
8
9#define CONST_FUNCTION(lowercase, uppercase) \
10uint32_t tox_##lowercase(void) \
11{ \
12 return TOX_##uppercase; \
13}
14
15CONST_FUNCTION(version_major, VERSION_MAJOR)
16CONST_FUNCTION(version_minor, VERSION_MINOR)
17CONST_FUNCTION(version_patch, VERSION_PATCH)
18CONST_FUNCTION(public_key_size, PUBLIC_KEY_SIZE)
19CONST_FUNCTION(secret_key_size, SECRET_KEY_SIZE)
20CONST_FUNCTION(address_size, ADDRESS_SIZE)
21CONST_FUNCTION(max_name_length, MAX_NAME_LENGTH)
22CONST_FUNCTION(max_status_message_length, MAX_STATUS_MESSAGE_LENGTH)
23CONST_FUNCTION(max_friend_request_length, MAX_FRIEND_REQUEST_LENGTH)
24CONST_FUNCTION(max_message_length, MAX_MESSAGE_LENGTH)
25CONST_FUNCTION(max_custom_packet_size, MAX_CUSTOM_PACKET_SIZE)
26CONST_FUNCTION(hash_length, HASH_LENGTH)
27CONST_FUNCTION(file_id_length, FILE_ID_LENGTH)
28CONST_FUNCTION(max_filename_length, MAX_FILENAME_LENGTH)
29
30
31#define ACCESSORS(type, ns, name) \
32type tox_options_get_##ns##name(const struct Tox_Options *options) \
33{ \
34 return options->ns##name; \
35} \
36void tox_options_set_##ns##name(struct Tox_Options *options, type name) \
37{ \
38 options->ns##name = name; \
39}
40
41ACCESSORS(bool, , ipv6_enabled)
42ACCESSORS(bool, , udp_enabled)
43ACCESSORS(TOX_PROXY_TYPE, proxy_ , type)
44ACCESSORS(const char *, proxy_ , host)
45ACCESSORS(uint16_t, proxy_ , port)
46ACCESSORS(uint16_t, , start_port)
47ACCESSORS(uint16_t, , end_port)
48ACCESSORS(uint16_t, , tcp_port)
49ACCESSORS(bool, , hole_punching_enabled)
50ACCESSORS(TOX_SAVEDATA_TYPE, savedata_, type)
51ACCESSORS(size_t, savedata_, length)
52ACCESSORS(tox_log_cb *, log_, callback)
53ACCESSORS(void *, log_, user_data)
54ACCESSORS(bool, , local_discovery_enabled)
55
56const uint8_t *tox_options_get_savedata_data(const struct Tox_Options *options)
57{
58 return options->savedata_data;
59}
60
61void tox_options_set_savedata_data(struct Tox_Options *options, const uint8_t *data, size_t length)
62{
63 options->savedata_data = data;
64 options->savedata_length = length;
65}
66
67void tox_options_default(struct Tox_Options *options)
68{
69 if (options) {
70 struct Tox_Options default_options = { 0 };
71 *options = default_options;
72 tox_options_set_ipv6_enabled(options, true);
73 tox_options_set_udp_enabled(options, true);
74 tox_options_set_proxy_type(options, TOX_PROXY_TYPE_NONE);
75 tox_options_set_hole_punching_enabled(options, true);
76 tox_options_set_local_discovery_enabled(options, true);
77 }
78}
79
80struct Tox_Options *tox_options_new(TOX_ERR_OPTIONS_NEW *error)
81{
82 struct Tox_Options *options = (struct Tox_Options *)malloc(sizeof(struct Tox_Options));
83
84 if (options) {
85 tox_options_default(options);
86 SET_ERROR_PARAMETER(error, TOX_ERR_OPTIONS_NEW_OK);
87 return options;
88 }
89
90 SET_ERROR_PARAMETER(error, TOX_ERR_OPTIONS_NEW_MALLOC);
91 return NULL;
92}
93
94void tox_options_copy(struct Tox_Options *lhs, const struct Tox_Options *rhs)
95{
96 *lhs = *rhs;
97}
98
99void tox_options_free(struct Tox_Options *options)
100{
101 free(options);
102}