diff options
author | iphydf <iphydf@users.noreply.github.com> | 2016-12-15 17:35:54 +0000 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2016-12-22 10:53:39 +0000 |
commit | db71602731da030c999f2821b2b4a64611b90b99 (patch) | |
tree | d90d14a7b1a467111d75984137e9ca41fc02abe7 /toxcore/tox.c | |
parent | 2328cb74abccd563f0cd8d14d30e5314822d321e (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.c')
-rw-r--r-- | toxcore/tox.c | 146 |
1 files changed, 23 insertions, 123 deletions
diff --git a/toxcore/tox.c b/toxcore/tox.c index 7a9a708c..37298ce2 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c | |||
@@ -69,20 +69,6 @@ typedef struct Messenger Tox; | |||
69 | #error TOX_MAX_STATUS_MESSAGE_LENGTH is assumed to be equal to MAX_STATUSMESSAGE_LENGTH | 69 | #error TOX_MAX_STATUS_MESSAGE_LENGTH is assumed to be equal to MAX_STATUSMESSAGE_LENGTH |
70 | #endif | 70 | #endif |
71 | 71 | ||
72 | uint32_t tox_version_major(void) | ||
73 | { | ||
74 | return TOX_VERSION_MAJOR; | ||
75 | } | ||
76 | |||
77 | uint32_t tox_version_minor(void) | ||
78 | { | ||
79 | return TOX_VERSION_MINOR; | ||
80 | } | ||
81 | |||
82 | uint32_t tox_version_patch(void) | ||
83 | { | ||
84 | return TOX_VERSION_PATCH; | ||
85 | } | ||
86 | 72 | ||
87 | bool tox_version_is_compatible(uint32_t major, uint32_t minor, uint32_t patch) | 73 | bool tox_version_is_compatible(uint32_t major, uint32_t minor, uint32_t patch) |
88 | { | 74 | { |
@@ -90,93 +76,6 @@ bool tox_version_is_compatible(uint32_t major, uint32_t minor, uint32_t patch) | |||
90 | } | 76 | } |
91 | 77 | ||
92 | 78 | ||
93 | #define CONST_FUNCTION(lowercase, uppercase) \ | ||
94 | uint32_t tox_##lowercase(void) \ | ||
95 | { \ | ||
96 | return TOX_##uppercase; \ | ||
97 | } | ||
98 | |||
99 | CONST_FUNCTION(public_key_size, PUBLIC_KEY_SIZE) | ||
100 | CONST_FUNCTION(secret_key_size, SECRET_KEY_SIZE) | ||
101 | CONST_FUNCTION(address_size, ADDRESS_SIZE) | ||
102 | CONST_FUNCTION(max_name_length, MAX_NAME_LENGTH) | ||
103 | CONST_FUNCTION(max_status_message_length, MAX_STATUS_MESSAGE_LENGTH) | ||
104 | CONST_FUNCTION(max_friend_request_length, MAX_FRIEND_REQUEST_LENGTH) | ||
105 | CONST_FUNCTION(max_message_length, MAX_MESSAGE_LENGTH) | ||
106 | CONST_FUNCTION(max_custom_packet_size, MAX_CUSTOM_PACKET_SIZE) | ||
107 | CONST_FUNCTION(hash_length, HASH_LENGTH) | ||
108 | CONST_FUNCTION(file_id_length, FILE_ID_LENGTH) | ||
109 | CONST_FUNCTION(max_filename_length, MAX_FILENAME_LENGTH) | ||
110 | |||
111 | |||
112 | #define ACCESSORS(type, ns, name) \ | ||
113 | type tox_options_get_##ns##name(const struct Tox_Options *options) \ | ||
114 | { \ | ||
115 | return options->ns##name; \ | ||
116 | } \ | ||
117 | void tox_options_set_##ns##name(struct Tox_Options *options, type name) \ | ||
118 | { \ | ||
119 | options->ns##name = name; \ | ||
120 | } | ||
121 | |||
122 | ACCESSORS(bool, , ipv6_enabled) | ||
123 | ACCESSORS(bool, , udp_enabled) | ||
124 | ACCESSORS(TOX_PROXY_TYPE, proxy_ , type) | ||
125 | ACCESSORS(const char *, proxy_ , host) | ||
126 | ACCESSORS(uint16_t, proxy_ , port) | ||
127 | ACCESSORS(uint16_t, , start_port) | ||
128 | ACCESSORS(uint16_t, , end_port) | ||
129 | ACCESSORS(uint16_t, , tcp_port) | ||
130 | ACCESSORS(bool, , hole_punching_enabled) | ||
131 | ACCESSORS(TOX_SAVEDATA_TYPE, savedata_, type) | ||
132 | ACCESSORS(size_t, savedata_, length) | ||
133 | ACCESSORS(tox_log_cb *, log_, callback) | ||
134 | ACCESSORS(void *, log_, user_data) | ||
135 | ACCESSORS(bool, , local_discovery_enabled) | ||
136 | |||
137 | const uint8_t *tox_options_get_savedata_data(const struct Tox_Options *options) | ||
138 | { | ||
139 | return options->savedata_data; | ||
140 | } | ||
141 | |||
142 | void tox_options_set_savedata_data(struct Tox_Options *options, const uint8_t *data, size_t length) | ||
143 | { | ||
144 | options->savedata_data = data; | ||
145 | options->savedata_length = length; | ||
146 | } | ||
147 | |||
148 | |||
149 | void tox_options_default(struct Tox_Options *options) | ||
150 | { | ||
151 | if (options) { | ||
152 | memset(options, 0, sizeof(struct Tox_Options)); | ||
153 | options->ipv6_enabled = 1; | ||
154 | options->udp_enabled = 1; | ||
155 | options->proxy_type = TOX_PROXY_TYPE_NONE; | ||
156 | options->hole_punching_enabled = true; | ||
157 | options->local_discovery_enabled = true; | ||
158 | } | ||
159 | } | ||
160 | |||
161 | struct Tox_Options *tox_options_new(TOX_ERR_OPTIONS_NEW *error) | ||
162 | { | ||
163 | struct Tox_Options *options = (struct Tox_Options *)calloc(sizeof(struct Tox_Options), 1); | ||
164 | |||
165 | if (options) { | ||
166 | tox_options_default(options); | ||
167 | SET_ERROR_PARAMETER(error, TOX_ERR_OPTIONS_NEW_OK); | ||
168 | return options; | ||
169 | } | ||
170 | |||
171 | SET_ERROR_PARAMETER(error, TOX_ERR_OPTIONS_NEW_MALLOC); | ||
172 | return NULL; | ||
173 | } | ||
174 | |||
175 | void tox_options_free(struct Tox_Options *options) | ||
176 | { | ||
177 | free(options); | ||
178 | } | ||
179 | |||
180 | Tox *tox_new(const struct Tox_Options *options, TOX_ERR_NEW *error) | 79 | Tox *tox_new(const struct Tox_Options *options, TOX_ERR_NEW *error) |
181 | { | 80 | { |
182 | Messenger_Options m_options = {0}; | 81 | Messenger_Options m_options = {0}; |
@@ -186,27 +85,27 @@ Tox *tox_new(const struct Tox_Options *options, TOX_ERR_NEW *error) | |||
186 | if (options == NULL) { | 85 | if (options == NULL) { |
187 | m_options.ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; | 86 | m_options.ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; |
188 | } else { | 87 | } else { |
189 | if (options->savedata_type != TOX_SAVEDATA_TYPE_NONE) { | 88 | if (tox_options_get_savedata_type(options) != TOX_SAVEDATA_TYPE_NONE) { |
190 | if (options->savedata_data == NULL || options->savedata_length == 0) { | 89 | if (tox_options_get_savedata_data(options) == NULL || tox_options_get_savedata_length(options) == 0) { |
191 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); | 90 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); |
192 | return NULL; | 91 | return NULL; |
193 | } | 92 | } |
194 | } | 93 | } |
195 | 94 | ||
196 | if (options->savedata_type == TOX_SAVEDATA_TYPE_SECRET_KEY) { | 95 | if (tox_options_get_savedata_type(options) == TOX_SAVEDATA_TYPE_SECRET_KEY) { |
197 | if (options->savedata_length != TOX_SECRET_KEY_SIZE) { | 96 | if (tox_options_get_savedata_length(options) != TOX_SECRET_KEY_SIZE) { |
198 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); | 97 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); |
199 | return NULL; | 98 | return NULL; |
200 | } | 99 | } |
201 | 100 | ||
202 | load_savedata_sk = 1; | 101 | load_savedata_sk = 1; |
203 | } else if (options->savedata_type == TOX_SAVEDATA_TYPE_TOX_SAVE) { | 102 | } else if (tox_options_get_savedata_type(options) == TOX_SAVEDATA_TYPE_TOX_SAVE) { |
204 | if (options->savedata_length < TOX_ENC_SAVE_MAGIC_LENGTH) { | 103 | if (tox_options_get_savedata_length(options) < TOX_ENC_SAVE_MAGIC_LENGTH) { |
205 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); | 104 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); |
206 | return NULL; | 105 | return NULL; |
207 | } | 106 | } |
208 | 107 | ||
209 | if (crypto_memcmp(options->savedata_data, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) == 0) { | 108 | if (crypto_memcmp(tox_options_get_savedata_data(options), TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) == 0) { |
210 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_ENCRYPTED); | 109 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_ENCRYPTED); |
211 | return NULL; | 110 | return NULL; |
212 | } | 111 | } |
@@ -214,18 +113,18 @@ Tox *tox_new(const struct Tox_Options *options, TOX_ERR_NEW *error) | |||
214 | load_savedata_tox = 1; | 113 | load_savedata_tox = 1; |
215 | } | 114 | } |
216 | 115 | ||
217 | m_options.ipv6enabled = options->ipv6_enabled; | 116 | m_options.ipv6enabled = tox_options_get_ipv6_enabled(options); |
218 | m_options.udp_disabled = !options->udp_enabled; | 117 | m_options.udp_disabled = !tox_options_get_udp_enabled(options); |
219 | m_options.port_range[0] = options->start_port; | 118 | m_options.port_range[0] = tox_options_get_start_port(options); |
220 | m_options.port_range[1] = options->end_port; | 119 | m_options.port_range[1] = tox_options_get_end_port(options); |
221 | m_options.tcp_server_port = options->tcp_port; | 120 | m_options.tcp_server_port = tox_options_get_tcp_port(options); |
222 | m_options.hole_punching_enabled = options->hole_punching_enabled; | 121 | m_options.hole_punching_enabled = tox_options_get_hole_punching_enabled(options); |
223 | m_options.local_discovery_enabled = options->local_discovery_enabled; | 122 | m_options.local_discovery_enabled = tox_options_get_local_discovery_enabled(options); |
224 | 123 | ||
225 | m_options.log_callback = (logger_cb *)options->log_callback; | 124 | m_options.log_callback = (logger_cb *)tox_options_get_log_callback(options); |
226 | m_options.log_user_data = options->log_user_data; | 125 | m_options.log_user_data = tox_options_get_log_user_data(options); |
227 | 126 | ||
228 | switch (options->proxy_type) { | 127 | switch (tox_options_get_proxy_type(options)) { |
229 | case TOX_PROXY_TYPE_HTTP: | 128 | case TOX_PROXY_TYPE_HTTP: |
230 | m_options.proxy_info.proxy_type = TCP_PROXY_HTTP; | 129 | m_options.proxy_info.proxy_type = TCP_PROXY_HTTP; |
231 | break; | 130 | break; |
@@ -244,7 +143,7 @@ Tox *tox_new(const struct Tox_Options *options, TOX_ERR_NEW *error) | |||
244 | } | 143 | } |
245 | 144 | ||
246 | if (m_options.proxy_info.proxy_type != TCP_PROXY_NONE) { | 145 | if (m_options.proxy_info.proxy_type != TCP_PROXY_NONE) { |
247 | if (options->proxy_port == 0) { | 146 | if (tox_options_get_proxy_port(options) == 0) { |
248 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PROXY_BAD_PORT); | 147 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PROXY_BAD_PORT); |
249 | return NULL; | 148 | return NULL; |
250 | } | 149 | } |
@@ -255,13 +154,13 @@ Tox *tox_new(const struct Tox_Options *options, TOX_ERR_NEW *error) | |||
255 | m_options.proxy_info.ip_port.ip.family = AF_UNSPEC; | 154 | m_options.proxy_info.ip_port.ip.family = AF_UNSPEC; |
256 | } | 155 | } |
257 | 156 | ||
258 | if (!addr_resolve_or_parse_ip(options->proxy_host, &m_options.proxy_info.ip_port.ip, NULL)) { | 157 | if (!addr_resolve_or_parse_ip(tox_options_get_proxy_host(options), &m_options.proxy_info.ip_port.ip, NULL)) { |
259 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PROXY_BAD_HOST); | 158 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PROXY_BAD_HOST); |
260 | // TODO(irungentoo): TOX_ERR_NEW_PROXY_NOT_FOUND if domain. | 159 | // TODO(irungentoo): TOX_ERR_NEW_PROXY_NOT_FOUND if domain. |
261 | return NULL; | 160 | return NULL; |
262 | } | 161 | } |
263 | 162 | ||
264 | m_options.proxy_info.ip_port.port = htons(options->proxy_port); | 163 | m_options.proxy_info.ip_port.port = htons(tox_options_get_proxy_port(options)); |
265 | } | 164 | } |
266 | } | 165 | } |
267 | 166 | ||
@@ -282,10 +181,11 @@ Tox *tox_new(const struct Tox_Options *options, TOX_ERR_NEW *error) | |||
282 | return NULL; | 181 | return NULL; |
283 | } | 182 | } |
284 | 183 | ||
285 | if (load_savedata_tox && messenger_load(m, options->savedata_data, options->savedata_length) == -1) { | 184 | if (load_savedata_tox |
185 | && messenger_load(m, tox_options_get_savedata_data(options), tox_options_get_savedata_length(options)) == -1) { | ||
286 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); | 186 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); |
287 | } else if (load_savedata_sk) { | 187 | } else if (load_savedata_sk) { |
288 | load_secret_key(m->net_crypto, options->savedata_data); | 188 | load_secret_key(m->net_crypto, tox_options_get_savedata_data(options)); |
289 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_OK); | 189 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_OK); |
290 | } else { | 190 | } else { |
291 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_OK); | 191 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_OK); |