From 478552d33817e8e5aca4aa281ed143d8c57e9c02 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Mon, 11 May 2015 12:41:53 -0400 Subject: 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. --- toxcore/tox.c | 94 +++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 76 insertions(+), 18 deletions(-) (limited to 'toxcore') 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 (port == 0) { + SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_PORT); + return 0; + } - if (!ret) { + struct addrinfo *root, *info; + + if (getaddrinfo(address, NULL, NULL, &root) != 0) { + SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); 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; + 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) -- cgit v1.2.3