From 478552d33817e8e5aca4aa281ed143d8c57e9c02 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Mon, 11 May 2015 12:41:53 -0400 Subject: [PATCH] Fixes and changes to tox_bootstrap and tox_add_tcp_relay. Functionality of both no longer overlaps. If address has more than 1 ip, call the internal function on all of them. --- auto_tests/tox_test.c | 6 +-- toxcore/tox.c | 96 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 80 insertions(+), 22 deletions(-) diff --git a/auto_tests/tox_test.c b/auto_tests/tox_test.c index 2e2d4538..4dcc6ada 100644 --- a/auto_tests/tox_test.c +++ b/auto_tests/tox_test.c @@ -809,7 +809,7 @@ loop_top: } END_TEST -#define TCP_RELAY_PORT 33445 +#define TCP_RELAY_PORT 33448 START_TEST(test_many_clients_tcp) { @@ -833,8 +833,8 @@ START_TEST(test_many_clients_tcp) tox_callback_friend_request(toxes[i], accept_friend_request, &to_comp); uint8_t dpk[TOX_PUBLIC_KEY_SIZE]; tox_self_get_dht_id(toxes[0], dpk); - tox_add_tcp_relay(toxes[i], "::1", TCP_RELAY_PORT, dpk, 0); - tox_bootstrap(toxes[i], "::1", 33445, dpk, 0); + ck_assert_msg(tox_add_tcp_relay(toxes[i], "::1", TCP_RELAY_PORT, dpk, 0), "add relay error"); + ck_assert_msg(tox_bootstrap(toxes[i], "::1", 33445, dpk, 0), "Bootstrap error"); } { 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 * return 0; } - Messenger *m = tox; - bool ret = tox_add_tcp_relay(tox, address, port, public_key, error); - - if (!ret) { + if (port == 0) { + SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_PORT); return 0; } - if (m->options.udp_disabled) { - return ret; - } else { /* DHT only works on UDP. */ - if (DHT_bootstrap_from_address(m->dht, address, m->options.ipv6enabled, htons(port), public_key) == 0) { - SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); - return 0; + struct addrinfo *root, *info; + + if (getaddrinfo(address, NULL, NULL, &root) != 0) { + SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); + return 0; + } + + info = root; + + unsigned int count = 0; + + do { + IP_Port ip_port; + ip_port.port = htons(port); + ip_port.ip.family = info->ai_family; + + if (info->ai_socktype && info->ai_socktype != SOCK_DGRAM) { + continue; } + if (info->ai_family == AF_INET) { + ip_port.ip.ip4.in_addr = ((struct sockaddr_in *)info->ai_addr)->sin_addr; + } else if (info->ai_family == AF_INET6) { + ip_port.ip.ip6.in6_addr = ((struct sockaddr_in6 *)info->ai_addr)->sin6_addr; + } else { + continue; + } + + Messenger *m = tox; + onion_add_bs_path_node(m->onion_c, ip_port, public_key); + DHT_bootstrap(m->dht, ip_port, public_key); + ++count; + } while ((info = info->ai_next)); + + freeaddrinfo(root); + + if (count) { SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_OK); return 1; + } else { + SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); + return 0; } } @@ -299,25 +329,53 @@ bool tox_add_tcp_relay(Tox *tox, const char *address, uint16_t port, const uint8 return 0; } - Messenger *m = tox; - IP_Port ip_port, ip_port_v4; - if (port == 0) { SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_PORT); return 0; } - if (address_to_ip(m, address, &ip_port, &ip_port_v4) == -1) { + struct addrinfo *root, *info; + + if (getaddrinfo(address, NULL, NULL, &root) != 0) { SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); return 0; } - ip_port.port = htons(port); - add_tcp_relay(m->net_crypto, ip_port, public_key); - onion_add_bs_path_node(m->onion_c, ip_port, public_key); //TODO: move this + info = root; - SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_OK); - return 1; + unsigned int count = 0; + + do { + IP_Port ip_port; + ip_port.port = htons(port); + ip_port.ip.family = info->ai_family; + + if (info->ai_socktype && info->ai_socktype != SOCK_STREAM) { + continue; + } + + if (info->ai_family == AF_INET) { + ip_port.ip.ip4.in_addr = ((struct sockaddr_in *)info->ai_addr)->sin_addr; + } else if (info->ai_family == AF_INET6) { + ip_port.ip.ip6.in6_addr = ((struct sockaddr_in6 *)info->ai_addr)->sin6_addr; + } else { + continue; + } + + Messenger *m = tox; + add_tcp_relay(m->net_crypto, ip_port, public_key); + ++count; + } while ((info = info->ai_next)); + + freeaddrinfo(root); + + if (count) { + SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_OK); + return 1; + } else { + SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); + return 0; + } } TOX_CONNECTION tox_self_get_connection_status(const Tox *tox)