diff options
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/tox.c | 94 |
1 files changed, 76 insertions, 18 deletions
diff --git a/toxcore/tox.c b/toxcore/tox.c index 92318cf9..4f3613ee 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c | |||
@@ -271,23 +271,53 @@ bool tox_bootstrap(Tox *tox, const char *address, uint16_t port, const uint8_t * | |||
271 | return 0; | 271 | return 0; |
272 | } | 272 | } |
273 | 273 | ||
274 | Messenger *m = tox; | 274 | if (port == 0) { |
275 | bool ret = tox_add_tcp_relay(tox, address, port, public_key, error); | 275 | SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_PORT); |
276 | return 0; | ||
277 | } | ||
276 | 278 | ||
277 | if (!ret) { | 279 | struct addrinfo *root, *info; |
280 | |||
281 | if (getaddrinfo(address, NULL, NULL, &root) != 0) { | ||
282 | SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); | ||
278 | return 0; | 283 | return 0; |
279 | } | 284 | } |
280 | 285 | ||
281 | if (m->options.udp_disabled) { | 286 | info = root; |
282 | return ret; | 287 | |
283 | } else { /* DHT only works on UDP. */ | 288 | unsigned int count = 0; |
284 | if (DHT_bootstrap_from_address(m->dht, address, m->options.ipv6enabled, htons(port), public_key) == 0) { | 289 | |
285 | SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); | 290 | do { |
286 | return 0; | 291 | IP_Port ip_port; |
292 | ip_port.port = htons(port); | ||
293 | ip_port.ip.family = info->ai_family; | ||
294 | |||
295 | if (info->ai_socktype && info->ai_socktype != SOCK_DGRAM) { | ||
296 | continue; | ||
297 | } | ||
298 | |||
299 | if (info->ai_family == AF_INET) { | ||
300 | ip_port.ip.ip4.in_addr = ((struct sockaddr_in *)info->ai_addr)->sin_addr; | ||
301 | } else if (info->ai_family == AF_INET6) { | ||
302 | ip_port.ip.ip6.in6_addr = ((struct sockaddr_in6 *)info->ai_addr)->sin6_addr; | ||
303 | } else { | ||
304 | continue; | ||
287 | } | 305 | } |
288 | 306 | ||
307 | Messenger *m = tox; | ||
308 | onion_add_bs_path_node(m->onion_c, ip_port, public_key); | ||
309 | DHT_bootstrap(m->dht, ip_port, public_key); | ||
310 | ++count; | ||
311 | } while ((info = info->ai_next)); | ||
312 | |||
313 | freeaddrinfo(root); | ||
314 | |||
315 | if (count) { | ||
289 | SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_OK); | 316 | SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_OK); |
290 | return 1; | 317 | return 1; |
318 | } else { | ||
319 | SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); | ||
320 | return 0; | ||
291 | } | 321 | } |
292 | } | 322 | } |
293 | 323 | ||
@@ -299,25 +329,53 @@ bool tox_add_tcp_relay(Tox *tox, const char *address, uint16_t port, const uint8 | |||
299 | return 0; | 329 | return 0; |
300 | } | 330 | } |
301 | 331 | ||
302 | Messenger *m = tox; | ||
303 | IP_Port ip_port, ip_port_v4; | ||
304 | |||
305 | if (port == 0) { | 332 | if (port == 0) { |
306 | SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_PORT); | 333 | SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_PORT); |
307 | return 0; | 334 | return 0; |
308 | } | 335 | } |
309 | 336 | ||
310 | if (address_to_ip(m, address, &ip_port, &ip_port_v4) == -1) { | 337 | struct addrinfo *root, *info; |
338 | |||
339 | if (getaddrinfo(address, NULL, NULL, &root) != 0) { | ||
311 | SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); | 340 | SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); |
312 | return 0; | 341 | return 0; |
313 | } | 342 | } |
314 | 343 | ||
315 | ip_port.port = htons(port); | 344 | info = root; |
316 | add_tcp_relay(m->net_crypto, ip_port, public_key); | ||
317 | onion_add_bs_path_node(m->onion_c, ip_port, public_key); //TODO: move this | ||
318 | 345 | ||
319 | SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_OK); | 346 | unsigned int count = 0; |
320 | return 1; | 347 | |
348 | do { | ||
349 | IP_Port ip_port; | ||
350 | ip_port.port = htons(port); | ||
351 | ip_port.ip.family = info->ai_family; | ||
352 | |||
353 | if (info->ai_socktype && info->ai_socktype != SOCK_STREAM) { | ||
354 | continue; | ||
355 | } | ||
356 | |||
357 | if (info->ai_family == AF_INET) { | ||
358 | ip_port.ip.ip4.in_addr = ((struct sockaddr_in *)info->ai_addr)->sin_addr; | ||
359 | } else if (info->ai_family == AF_INET6) { | ||
360 | ip_port.ip.ip6.in6_addr = ((struct sockaddr_in6 *)info->ai_addr)->sin6_addr; | ||
361 | } else { | ||
362 | continue; | ||
363 | } | ||
364 | |||
365 | Messenger *m = tox; | ||
366 | add_tcp_relay(m->net_crypto, ip_port, public_key); | ||
367 | ++count; | ||
368 | } while ((info = info->ai_next)); | ||
369 | |||
370 | freeaddrinfo(root); | ||
371 | |||
372 | if (count) { | ||
373 | SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_OK); | ||
374 | return 1; | ||
375 | } else { | ||
376 | SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); | ||
377 | return 0; | ||
378 | } | ||
321 | } | 379 | } |
322 | 380 | ||
323 | TOX_CONNECTION tox_self_get_connection_status(const Tox *tox) | 381 | TOX_CONNECTION tox_self_get_connection_status(const Tox *tox) |