Merge branch 'network-test-magic' of https://github.com/roman-yepishev/toxcore

This commit is contained in:
irungentoo 2016-03-19 00:09:36 -04:00
commit 532629d486
No known key found for this signature in database
GPG Key ID: 10349DC9BED89E98
3 changed files with 35 additions and 26 deletions

View File

@ -28,7 +28,7 @@ START_TEST(test_addr_resolv_localhost)
int localhost_split = 0;
IP ip;
ip_init(&ip, 0);
ip_init(&ip, 0); // ipv6enabled = 0
int res = addr_resolve(localhost, &ip, NULL);
@ -39,10 +39,10 @@ START_TEST(test_addr_resolv_localhost)
ck_assert_msg(ip.ip4.uint32 == htonl(0x7F000001), "Expected 127.0.0.1, got %s.", inet_ntoa(ip.ip4.in_addr));
}
ip_init(&ip, 1);
ip_init(&ip, 1); // ipv6enabled = 1
res = addr_resolve(localhost, &ip, NULL);
if (res < 1) {
if (!(res & TOX_ADDR_RESOLVE_INET6)) {
res = addr_resolve("ip6-localhost", &ip, NULL);
localhost_split = 1;
}
@ -50,12 +50,12 @@ START_TEST(test_addr_resolv_localhost)
ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno));
if (res > 0) {
ck_assert_msg(ip.family == AF_INET6, "Expected family AF_INET6, got %u.", ip.family);
ck_assert_msg(ip.family == AF_INET6, "Expected family AF_INET6 (%u), got %u.", AF_INET6, ip.family);
ck_assert_msg(!memcmp(&ip.ip6, &in6addr_loopback, sizeof(IP6)), "Expected ::1, got %s.", ip_ntoa(&ip));
}
if (!localhost_split) {
ip_init(&ip, 1);
ip_init(&ip, 1); // ipv6enabled = 1
ip.family = AF_UNSPEC;
IP extra;
ip_reset(&extra);
@ -63,10 +63,10 @@ START_TEST(test_addr_resolv_localhost)
ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno));
if (res > 0) {
ck_assert_msg(ip.family == AF_INET6, "Expected family AF_INET6, got %u.", ip.family);
ck_assert_msg(ip.family == AF_INET6, "Expected family AF_INET6 (%u), got %u.", AF_INET6, ip.family);
ck_assert_msg(!memcmp(&ip.ip6, &in6addr_loopback, sizeof(IP6)), "Expected ::1, got %s.", ip_ntoa(&ip));
ck_assert_msg(extra.family == AF_INET, "Expected family AF_INET, got %u.", extra.family);
ck_assert_msg(extra.family == AF_INET, "Expected family AF_INET (%u), got %u.", AF_INET, extra.family);
ck_assert_msg(extra.ip4.uint32 == htonl(0x7F000001), "Expected 127.0.0.1, got %s.", inet_ntoa(extra.ip4.in_addr));
}
} else {
@ -144,7 +144,7 @@ Suite *network_suite(void)
return s;
}
int main(int argc, char *argv[])
int main()
{
srand((unsigned int) time(NULL));

View File

@ -939,7 +939,7 @@ int addr_parse_ip(const char *address, IP *to)
* returns in *to a valid IPAny (v4/v6),
* prefers v6 if ip.family was AF_UNSPEC and both available
* returns in *extra an IPv4 address, if family was AF_UNSPEC and *to is AF_INET6
* returns 0 on failure
* returns 0 on failure, TOX_ADDR_RESOLVE_* on success.
*/
int addr_resolve(const char *address, IP *to, IP *extra)
{
@ -951,7 +951,9 @@ int addr_resolve(const char *address, IP *to, IP *extra)
struct addrinfo *server = NULL;
struct addrinfo *walker = NULL;
struct addrinfo hints;
int rc;
int rc;
int result = 0;
int done = 0;
memset(&hints, 0, sizeof(hints));
hints.ai_family = family;
@ -968,21 +970,22 @@ int addr_resolve(const char *address, IP *to, IP *extra)
}
IP ip4;
ip_init(&ip4, /* ipv6? */ false);
ip_init(&ip4, 0); // ipv6enabled = 0
IP ip6;
ip_init(&ip6, /* ipv6? */ true);
ip_init(&ip6, 1); // ipv6enabled = 1
for (walker = server; (walker != NULL) && (rc != 3); walker = walker->ai_next) {
for (walker = server; (walker != NULL) && !done; walker = walker->ai_next) {
switch (walker->ai_family) {
case AF_INET:
if (walker->ai_family == family) { /* AF_INET requested, done */
struct sockaddr_in *addr = (struct sockaddr_in *)walker->ai_addr;
to->ip4.in_addr = addr->sin_addr;
rc = 3; // TODO do we really have to reuse variable instead of creating a new one?
} else if (!(rc & 1)) { /* AF_UNSPEC requested, store away */
result = TOX_ADDR_RESOLVE_INET;
done = 1;
} else if (!(result & TOX_ADDR_RESOLVE_INET)) { /* AF_UNSPEC requested, store away */
struct sockaddr_in *addr = (struct sockaddr_in *)walker->ai_addr;
ip4.ip4.in_addr = addr->sin_addr;
rc |= 1; // FIXME magic number
result |= TOX_ADDR_RESOLVE_INET;
}
break; /* switch */
@ -992,13 +995,14 @@ int addr_resolve(const char *address, IP *to, IP *extra)
if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) {
struct sockaddr_in6 *addr = (struct sockaddr_in6 *)walker->ai_addr;
to->ip6.in6_addr = addr->sin6_addr;
rc = 3;
result = TOX_ADDR_RESOLVE_INET6;
done = 1;
}
} else if (!(rc & 2)) { /* AF_UNSPEC requested, store away */
} else if (!(result & TOX_ADDR_RESOLVE_INET6)) { /* AF_UNSPEC requested, store away */
if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) {
struct sockaddr_in6 *addr = (struct sockaddr_in6 *)walker->ai_addr;
ip6.ip6.in6_addr = addr->sin6_addr;
rc |= 2;
result |= TOX_ADDR_RESOLVE_INET6;
}
}
@ -1006,21 +1010,22 @@ int addr_resolve(const char *address, IP *to, IP *extra)
}
}
if (to->family == AF_UNSPEC) {
if (rc & 2) { // FIXME magic number
if (family == AF_UNSPEC) {
if (result & TOX_ADDR_RESOLVE_INET6) {
ip_copy(to, &ip6);
if ((rc & 1) && (extra != NULL)) {
if ((result & TOX_ADDR_RESOLVE_INET) && (extra != NULL)) {
ip_copy(extra, &ip4);
}
} else if (rc & 1) {
} else if (result & TOX_ADDR_RESOLVE_INET) {
ip_copy(to, &ip4);
} else
rc = 0;
} else {
result = 0;
}
}
freeaddrinfo(server);
return rc;
return result;
}
/*

View File

@ -176,6 +176,10 @@ IP_Port;
#define TOX_ENABLE_IPV6_DEFAULT 1
/* addr_resolve return values */
#define TOX_ADDR_RESOLVE_INET 1
#define TOX_ADDR_RESOLVE_INET6 2
/* ip_ntoa
* converts ip into a string
* uses a static buffer, so mustn't used multiple times in the same output