Skip to content

Commit

Permalink
cleanup: Make addr_resolve a private function.
Browse files Browse the repository at this point in the history
This isn't used anywhere except in network_test. That test now checks
behaviour of the function actually used elsewhere in tox code, instead.
  • Loading branch information
iphydf committed Mar 27, 2022
1 parent 96ab891 commit b4cbb5e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
16 changes: 8 additions & 8 deletions auto_tests/network_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ static void test_addr_resolv_localhost(void)
IP ip;
ip_init(&ip, 0); // ipv6enabled = 0

int res = addr_resolve(localhost, &ip, nullptr);
bool res = addr_resolve_or_parse_ip(localhost, &ip, nullptr);

int error = net_error();
char *strerror = net_new_strerror(error);
ck_assert_msg(res > 0, "Resolver failed: %d, %s", error, strerror);
ck_assert_msg(res, "Resolver failed: %d, %s", error, strerror);
net_kill_strerror(strerror);

char ip_str[IP_NTOA_LEN];
Expand All @@ -39,20 +39,20 @@ static void test_addr_resolv_localhost(void)
ip_ntoa(&ip, ip_str, sizeof(ip_str)));

ip_init(&ip, 1); // ipv6enabled = 1
res = addr_resolve(localhost, &ip, nullptr);
res = addr_resolve_or_parse_ip(localhost, &ip, nullptr);

#if USE_IPV6

int localhost_split = 0;

if (!(res & TOX_ADDR_RESOLVE_INET6)) {
res = addr_resolve("ip6-localhost", &ip, nullptr);
if (!net_family_is_ipv6(ip.family)) {
res = addr_resolve_or_parse_ip("ip6-localhost", &ip, nullptr);
localhost_split = 1;
}

error = net_error();
strerror = net_new_strerror(error);
ck_assert_msg(res > 0, "Resolver failed: %d, %s", error, strerror);
ck_assert_msg(res, "Resolver failed: %d, %s", error, strerror);
net_kill_strerror(strerror);

ck_assert_msg(net_family_is_ipv6(ip.family), "Expected family TOX_AF_INET6 (%d), got %u.", TOX_AF_INET6,
Expand All @@ -72,10 +72,10 @@ static void test_addr_resolv_localhost(void)
ip.family = net_family_unspec;
IP extra;
ip_reset(&extra);
res = addr_resolve(localhost, &ip, &extra);
res = addr_resolve_or_parse_ip(localhost, &ip, &extra);
error = net_error();
strerror = net_new_strerror(error);
ck_assert_msg(res > 0, "Resolver failed: %d, %s", error, strerror);
ck_assert_msg(res, "Resolver failed: %d, %s", error, strerror);
net_kill_strerror(strerror);

#if USE_IPV6
Expand Down
26 changes: 25 additions & 1 deletion toxcore/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -1329,7 +1329,31 @@ bool addr_parse_ip(const char *address, IP *to)
return false;
}

int addr_resolve(const char *address, IP *to, IP *extra)
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
/** addr_resolve return values */
#define TOX_ADDR_RESOLVE_INET 1
#define TOX_ADDR_RESOLVE_INET6 2
#endif

/**
* Uses getaddrinfo to resolve an address into an IP address.
*
* Uses the first IPv4/IPv6 addresses returned by getaddrinfo.
*
* @param address a hostname (or something parseable to an IP address)
* @param to to.family MUST be initialized, either set to a specific IP version
* (TOX_AF_INET/TOX_AF_INET6) or to the unspecified TOX_AF_UNSPEC (0), if both
* IP versions are acceptable
* @param extra can be NULL and is only set in special circumstances, see returns
*
* Returns in `*to` a valid IPAny (v4/v6),
* prefers v6 if `ip.family` was TOX_AF_UNSPEC and both available
* Returns in `*extra` an IPv4 address, if family was TOX_AF_UNSPEC and `*to` is TOX_AF_INET6
*
* @return 0 on failure, `TOX_ADDR_RESOLVE_*` on success.
*/
non_null(1, 2) nullable(3)
static int addr_resolve(const char *address, IP *to, IP *extra)
{
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
return 0;
Expand Down
24 changes: 0 additions & 24 deletions toxcore/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,6 @@ bool ipv6_ipv4_in_v6(const IP6 *a);

#define TOX_ENABLE_IPV6_DEFAULT true

/** addr_resolve return values */
#define TOX_ADDR_RESOLVE_INET 1
#define TOX_ADDR_RESOLVE_INET6 2

#define TOX_INET6_ADDRSTRLEN 66
#define TOX_INET_ADDRSTRLEN 22

Expand Down Expand Up @@ -347,26 +343,6 @@ void ip_copy(IP *target, const IP *source);
non_null()
void ipport_copy(IP_Port *target, const IP_Port *source);

/**
* Uses getaddrinfo to resolve an address into an IP address.
*
* Uses the first IPv4/IPv6 addresses returned by getaddrinfo.
*
* @param address a hostname (or something parseable to an IP address)
* @param to to.family MUST be initialized, either set to a specific IP version
* (TOX_AF_INET/TOX_AF_INET6) or to the unspecified TOX_AF_UNSPEC (0), if both
* IP versions are acceptable
* @param extra can be NULL and is only set in special circumstances, see returns
*
* Returns in `*to` a valid IPAny (v4/v6),
* prefers v6 if `ip.family` was TOX_AF_UNSPEC and both available
* Returns in `*extra` an IPv4 address, if family was TOX_AF_UNSPEC and `*to` is TOX_AF_INET6
*
* @return 0 on failure, `TOX_ADDR_RESOLVE_*` on success.
*/
non_null(1, 2) nullable(3)
int addr_resolve(const char *address, IP *to, IP *extra);

/**
* Resolves string into an IP address
*
Expand Down

0 comments on commit b4cbb5e

Please sign in to comment.