diff options
Diffstat (limited to 'toxcore/tox.c')
-rw-r--r-- | toxcore/tox.c | 115 |
1 files changed, 108 insertions, 7 deletions
diff --git a/toxcore/tox.c b/toxcore/tox.c index b509fc3b..dbd8a96c 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c | |||
@@ -20,11 +20,7 @@ | |||
20 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | 20 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. |
21 | * | 21 | * |
22 | */ | 22 | */ |
23 | /* | 23 | |
24 | TODO: | ||
25 | -replace bool with uint8_t | ||
26 | -remove enums (typedef enum in api to uint8_t) | ||
27 | */ | ||
28 | #ifdef HAVE_CONFIG_H | 24 | #ifdef HAVE_CONFIG_H |
29 | #include "config.h" | 25 | #include "config.h" |
30 | #endif | 26 | #endif |
@@ -157,6 +153,7 @@ Tox *tox_new(struct Tox_Options const *options, uint8_t const *data, size_t leng | |||
157 | 153 | ||
158 | if (!new_groupchats(m)) { | 154 | if (!new_groupchats(m)) { |
159 | kill_messenger(m); | 155 | kill_messenger(m); |
156 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC); | ||
160 | return NULL; | 157 | return NULL; |
161 | } | 158 | } |
162 | 159 | ||
@@ -180,16 +177,120 @@ void tox_kill(Tox *tox) | |||
180 | logger_kill_global(); | 177 | logger_kill_global(); |
181 | } | 178 | } |
182 | 179 | ||
183 | |||
184 | size_t tox_save_size(Tox const *tox) | 180 | size_t tox_save_size(Tox const *tox) |
185 | { | 181 | { |
186 | const Messenger *m = tox; | 182 | const Messenger *m = tox; |
187 | return messenger_size(m); | 183 | return messenger_size(m); |
188 | } | 184 | } |
189 | 185 | ||
190 | |||
191 | void tox_save(Tox const *tox, uint8_t *data) | 186 | void tox_save(Tox const *tox, uint8_t *data) |
192 | { | 187 | { |
193 | const Messenger *m = tox; | 188 | const Messenger *m = tox; |
194 | messenger_save(m, data); | 189 | messenger_save(m, data); |
195 | } | 190 | } |
191 | |||
192 | static int address_to_ip(Messenger *m, char const *address, IP_Port *ip_port, IP_Port *ip_port_v4) | ||
193 | { | ||
194 | if (!addr_parse_ip(address, &ip_port->ip)) { | ||
195 | if (m->options.udp_disabled) { /* Disable DNS when udp is disabled. */ | ||
196 | return -1; | ||
197 | } | ||
198 | |||
199 | IP *ip_extra = NULL; | ||
200 | ip_init(&ip_port->ip, m->options.ipv6enabled); | ||
201 | |||
202 | if (m->options.ipv6enabled && ip_port_v4) { | ||
203 | /* setup for getting BOTH: an IPv6 AND an IPv4 address */ | ||
204 | ip_port->ip.family = AF_UNSPEC; | ||
205 | ip_reset(&ip_port_v4->ip); | ||
206 | ip_extra = &ip_port_v4->ip; | ||
207 | } | ||
208 | |||
209 | if (!addr_resolve(address, &ip_port->ip, ip_extra)) { | ||
210 | return -1; | ||
211 | } | ||
212 | } | ||
213 | |||
214 | return 0; | ||
215 | } | ||
216 | |||
217 | bool tox_bootstrap(Tox *tox, char const *address, uint16_t port, uint8_t const *public_key, TOX_ERR_BOOTSTRAP *error) | ||
218 | { | ||
219 | Messenger *m = tox; | ||
220 | bool ret = tox_add_tcp_relay(tox, address, port, public_key, error); | ||
221 | |||
222 | if (!ret) { | ||
223 | return 0; | ||
224 | } | ||
225 | |||
226 | if (m->options.udp_disabled) { | ||
227 | return ret; | ||
228 | } else { /* DHT only works on UDP. */ | ||
229 | if (DHT_bootstrap_from_address(m->dht, address, m->options.ipv6enabled, htons(port), public_key) == 0) { | ||
230 | SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_ADDRESS); | ||
231 | return 0; | ||
232 | } | ||
233 | |||
234 | SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_OK); | ||
235 | return 1; | ||
236 | } | ||
237 | } | ||
238 | |||
239 | bool tox_add_tcp_relay(Tox *tox, char const *address, uint16_t port, uint8_t const *public_key, | ||
240 | TOX_ERR_BOOTSTRAP *error) | ||
241 | { | ||
242 | Messenger *m = tox; | ||
243 | IP_Port ip_port, ip_port_v4; | ||
244 | |||
245 | if (port == 0) { | ||
246 | SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_PORT); | ||
247 | return 0; | ||
248 | } | ||
249 | |||
250 | if (address_to_ip(m, address, &ip_port, &ip_port_v4) == -1) { | ||
251 | SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_ADDRESS); | ||
252 | return 0; | ||
253 | } | ||
254 | |||
255 | ip_port.port = htons(port); | ||
256 | add_tcp_relay(m->net_crypto, ip_port, public_key); | ||
257 | onion_add_bs_path_node(m->onion_c, ip_port, public_key); //TODO: move this | ||
258 | |||
259 | SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_OK); | ||
260 | return 1; | ||
261 | } | ||
262 | |||
263 | TOX_CONNECTION tox_get_connection_status(Tox const *tox) | ||
264 | { | ||
265 | const Messenger *m = tox; | ||
266 | |||
267 | if (onion_isconnected(m->onion_c)) { | ||
268 | if (DHT_non_lan_connected(m->dht)) { | ||
269 | return TOX_CONNECTION_UDP; | ||
270 | } | ||
271 | |||
272 | return TOX_CONNECTION_TCP; | ||
273 | } | ||
274 | |||
275 | return TOX_CONNECTION_NONE; | ||
276 | } | ||
277 | |||
278 | |||
279 | void tox_callback_connection_status(Tox *tox, tox_connection_status_cb *function, void *user_data) | ||
280 | { | ||
281 | //TODO | ||
282 | } | ||
283 | |||
284 | uint32_t tox_iteration_interval(Tox const *tox) | ||
285 | { | ||
286 | const Messenger *m = tox; | ||
287 | return messenger_run_interval(m); | ||
288 | } | ||
289 | |||
290 | void tox_iteration(Tox *tox) | ||
291 | { | ||
292 | Messenger *m = tox; | ||
293 | do_messenger(m); | ||
294 | do_groupchats(m->group_chat_object); | ||
295 | } | ||
296 | |||