diff options
author | iphydf <iphydf@users.noreply.github.com> | 2018-06-23 12:25:21 +0000 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2018-06-23 13:29:11 +0000 |
commit | 9853f4a535d4fd9dd89bdb5197f03f3b65da461b (patch) | |
tree | 4e3776281f03a92b912271d7d799df2af03f4980 /toxcore/tox.c | |
parent | cfff361679608bdfa49cd55a8062e86797587571 (diff) |
Make NULL options behave the same as default options.
I.e. make tox_new(0, 0) behave the same as tox_new(tox_options_new(0), 0).
Changing defaults in the options does not currently affect NULL options.
Diffstat (limited to 'toxcore/tox.c')
-rw-r--r-- | toxcore/tox.c | 149 |
1 files changed, 87 insertions, 62 deletions
diff --git a/toxcore/tox.c b/toxcore/tox.c index 2f210209..d2dcd93e 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c | |||
@@ -31,6 +31,7 @@ | |||
31 | typedef struct Messenger Tox; | 31 | typedef struct Messenger Tox; |
32 | #include "tox.h" | 32 | #include "tox.h" |
33 | 33 | ||
34 | #include <assert.h> | ||
34 | #include <string.h> | 35 | #include <string.h> |
35 | 36 | ||
36 | #include "Messenger.h" | 37 | #include "Messenger.h" |
@@ -86,86 +87,108 @@ Tox *tox_new(const struct Tox_Options *options, TOX_ERR_NEW *error) | |||
86 | 87 | ||
87 | bool load_savedata_sk = false, load_savedata_tox = false; | 88 | bool load_savedata_sk = false, load_savedata_tox = false; |
88 | 89 | ||
90 | struct Tox_Options *default_options = nullptr; | ||
91 | |||
89 | if (options == nullptr) { | 92 | if (options == nullptr) { |
90 | m_options.ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; | 93 | TOX_ERR_OPTIONS_NEW err; |
91 | } else { | 94 | default_options = tox_options_new(&err); |
92 | if (tox_options_get_savedata_type(options) != TOX_SAVEDATA_TYPE_NONE) { | 95 | |
93 | if (tox_options_get_savedata_data(options) == nullptr || tox_options_get_savedata_length(options) == 0) { | 96 | switch (err) { |
94 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); | 97 | case TOX_ERR_OPTIONS_NEW_OK: |
98 | break; | ||
99 | |||
100 | case TOX_ERR_OPTIONS_NEW_MALLOC: | ||
101 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC); | ||
95 | return nullptr; | 102 | return nullptr; |
96 | } | ||
97 | } | 103 | } |
104 | } | ||
98 | 105 | ||
99 | if (tox_options_get_savedata_type(options) == TOX_SAVEDATA_TYPE_SECRET_KEY) { | 106 | const struct Tox_Options *const opts = options != nullptr ? options : default_options; |
100 | if (tox_options_get_savedata_length(options) != TOX_SECRET_KEY_SIZE) { | 107 | assert(opts != nullptr); |
101 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); | ||
102 | return nullptr; | ||
103 | } | ||
104 | 108 | ||
105 | load_savedata_sk = true; | 109 | if (tox_options_get_savedata_type(opts) != TOX_SAVEDATA_TYPE_NONE) { |
106 | } else if (tox_options_get_savedata_type(options) == TOX_SAVEDATA_TYPE_TOX_SAVE) { | 110 | if (tox_options_get_savedata_data(opts) == nullptr || tox_options_get_savedata_length(opts) == 0) { |
107 | if (tox_options_get_savedata_length(options) < TOX_ENC_SAVE_MAGIC_LENGTH) { | 111 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); |
108 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); | 112 | tox_options_free(default_options); |
109 | return nullptr; | 113 | return nullptr; |
110 | } | 114 | } |
115 | } | ||
111 | 116 | ||
112 | if (crypto_memcmp(tox_options_get_savedata_data(options), TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) == 0) { | 117 | if (tox_options_get_savedata_type(opts) == TOX_SAVEDATA_TYPE_SECRET_KEY) { |
113 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_ENCRYPTED); | 118 | if (tox_options_get_savedata_length(opts) != TOX_SECRET_KEY_SIZE) { |
114 | return nullptr; | 119 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); |
115 | } | 120 | tox_options_free(default_options); |
121 | return nullptr; | ||
122 | } | ||
116 | 123 | ||
117 | load_savedata_tox = true; | 124 | load_savedata_sk = true; |
125 | } else if (tox_options_get_savedata_type(opts) == TOX_SAVEDATA_TYPE_TOX_SAVE) { | ||
126 | if (tox_options_get_savedata_length(opts) < TOX_ENC_SAVE_MAGIC_LENGTH) { | ||
127 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); | ||
128 | tox_options_free(default_options); | ||
129 | return nullptr; | ||
118 | } | 130 | } |
119 | 131 | ||
120 | m_options.ipv6enabled = tox_options_get_ipv6_enabled(options); | 132 | if (crypto_memcmp(tox_options_get_savedata_data(opts), TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) == 0) { |
121 | m_options.udp_disabled = !tox_options_get_udp_enabled(options); | 133 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_ENCRYPTED); |
122 | m_options.port_range[0] = tox_options_get_start_port(options); | 134 | tox_options_free(default_options); |
123 | m_options.port_range[1] = tox_options_get_end_port(options); | 135 | return nullptr; |
124 | m_options.tcp_server_port = tox_options_get_tcp_port(options); | 136 | } |
125 | m_options.hole_punching_enabled = tox_options_get_hole_punching_enabled(options); | ||
126 | m_options.local_discovery_enabled = tox_options_get_local_discovery_enabled(options); | ||
127 | 137 | ||
128 | m_options.log_callback = (logger_cb *)tox_options_get_log_callback(options); | 138 | load_savedata_tox = true; |
129 | m_options.log_user_data = tox_options_get_log_user_data(options); | 139 | } |
130 | 140 | ||
131 | switch (tox_options_get_proxy_type(options)) { | 141 | m_options.ipv6enabled = tox_options_get_ipv6_enabled(opts); |
132 | case TOX_PROXY_TYPE_HTTP: | 142 | m_options.udp_disabled = !tox_options_get_udp_enabled(opts); |
133 | m_options.proxy_info.proxy_type = TCP_PROXY_HTTP; | 143 | m_options.port_range[0] = tox_options_get_start_port(opts); |
134 | break; | 144 | m_options.port_range[1] = tox_options_get_end_port(opts); |
145 | m_options.tcp_server_port = tox_options_get_tcp_port(opts); | ||
146 | m_options.hole_punching_enabled = tox_options_get_hole_punching_enabled(opts); | ||
147 | m_options.local_discovery_enabled = tox_options_get_local_discovery_enabled(opts); | ||
135 | 148 | ||
136 | case TOX_PROXY_TYPE_SOCKS5: | 149 | m_options.log_callback = (logger_cb *)tox_options_get_log_callback(opts); |
137 | m_options.proxy_info.proxy_type = TCP_PROXY_SOCKS5; | 150 | m_options.log_user_data = tox_options_get_log_user_data(opts); |
138 | break; | ||
139 | 151 | ||
140 | case TOX_PROXY_TYPE_NONE: | 152 | switch (tox_options_get_proxy_type(opts)) { |
141 | m_options.proxy_info.proxy_type = TCP_PROXY_NONE; | 153 | case TOX_PROXY_TYPE_HTTP: |
142 | break; | 154 | m_options.proxy_info.proxy_type = TCP_PROXY_HTTP; |
155 | break; | ||
143 | 156 | ||
144 | default: | 157 | case TOX_PROXY_TYPE_SOCKS5: |
145 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PROXY_BAD_TYPE); | 158 | m_options.proxy_info.proxy_type = TCP_PROXY_SOCKS5; |
146 | return nullptr; | 159 | break; |
147 | } | ||
148 | 160 | ||
149 | if (m_options.proxy_info.proxy_type != TCP_PROXY_NONE) { | 161 | case TOX_PROXY_TYPE_NONE: |
150 | if (tox_options_get_proxy_port(options) == 0) { | 162 | m_options.proxy_info.proxy_type = TCP_PROXY_NONE; |
151 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PROXY_BAD_PORT); | 163 | break; |
152 | return nullptr; | ||
153 | } | ||
154 | 164 | ||
155 | ip_init(&m_options.proxy_info.ip_port.ip, m_options.ipv6enabled); | 165 | default: |
166 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PROXY_BAD_TYPE); | ||
167 | tox_options_free(default_options); | ||
168 | return nullptr; | ||
169 | } | ||
156 | 170 | ||
157 | if (m_options.ipv6enabled) { | 171 | if (m_options.proxy_info.proxy_type != TCP_PROXY_NONE) { |
158 | m_options.proxy_info.ip_port.ip.family = net_family_unspec; | 172 | if (tox_options_get_proxy_port(opts) == 0) { |
159 | } | 173 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PROXY_BAD_PORT); |
174 | tox_options_free(default_options); | ||
175 | return nullptr; | ||
176 | } | ||
160 | 177 | ||
161 | if (addr_resolve_or_parse_ip(tox_options_get_proxy_host(options), &m_options.proxy_info.ip_port.ip, nullptr) == 0) { | 178 | ip_init(&m_options.proxy_info.ip_port.ip, m_options.ipv6enabled); |
162 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PROXY_BAD_HOST); | ||
163 | // TODO(irungentoo): TOX_ERR_NEW_PROXY_NOT_FOUND if domain. | ||
164 | return nullptr; | ||
165 | } | ||
166 | 179 | ||
167 | m_options.proxy_info.ip_port.port = net_htons(tox_options_get_proxy_port(options)); | 180 | if (m_options.ipv6enabled) { |
181 | m_options.proxy_info.ip_port.ip.family = net_family_unspec; | ||
168 | } | 182 | } |
183 | |||
184 | if (addr_resolve_or_parse_ip(tox_options_get_proxy_host(opts), &m_options.proxy_info.ip_port.ip, nullptr) == 0) { | ||
185 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PROXY_BAD_HOST); | ||
186 | // TODO(irungentoo): TOX_ERR_NEW_PROXY_NOT_FOUND if domain. | ||
187 | tox_options_free(default_options); | ||
188 | return nullptr; | ||
189 | } | ||
190 | |||
191 | m_options.proxy_info.ip_port.port = net_htons(tox_options_get_proxy_port(opts)); | ||
169 | } | 192 | } |
170 | 193 | ||
171 | unsigned int m_error; | 194 | unsigned int m_error; |
@@ -182,19 +205,21 @@ Tox *tox_new(const struct Tox_Options *options, TOX_ERR_NEW *error) | |||
182 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC); | 205 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC); |
183 | } | 206 | } |
184 | 207 | ||
208 | tox_options_free(default_options); | ||
185 | return nullptr; | 209 | return nullptr; |
186 | } | 210 | } |
187 | 211 | ||
188 | if (load_savedata_tox | 212 | if (load_savedata_tox |
189 | && messenger_load(m, tox_options_get_savedata_data(options), tox_options_get_savedata_length(options)) == -1) { | 213 | && messenger_load(m, tox_options_get_savedata_data(opts), tox_options_get_savedata_length(opts)) == -1) { |
190 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); | 214 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); |
191 | } else if (load_savedata_sk) { | 215 | } else if (load_savedata_sk) { |
192 | load_secret_key(m->net_crypto, tox_options_get_savedata_data(options)); | 216 | load_secret_key(m->net_crypto, tox_options_get_savedata_data(opts)); |
193 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_OK); | 217 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_OK); |
194 | } else { | 218 | } else { |
195 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_OK); | 219 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_OK); |
196 | } | 220 | } |
197 | 221 | ||
222 | tox_options_free(default_options); | ||
198 | return m; | 223 | return m; |
199 | } | 224 | } |
200 | 225 | ||