Skip to content

Commit 532629d

Browse files
committed
Merge branch 'network-test-magic' of https://github.com/roman-yepishev/toxcore
2 parents aafeb7d + c886f90 commit 532629d

File tree

3 files changed

+35
-26
lines changed

3 files changed

+35
-26
lines changed

auto_tests/network_test.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ START_TEST(test_addr_resolv_localhost)
2828
int localhost_split = 0;
2929

3030
IP ip;
31-
ip_init(&ip, 0);
31+
ip_init(&ip, 0); // ipv6enabled = 0
3232

3333
int res = addr_resolve(localhost, &ip, NULL);
3434

@@ -39,34 +39,34 @@ START_TEST(test_addr_resolv_localhost)
3939
ck_assert_msg(ip.ip4.uint32 == htonl(0x7F000001), "Expected 127.0.0.1, got %s.", inet_ntoa(ip.ip4.in_addr));
4040
}
4141

42-
ip_init(&ip, 1);
42+
ip_init(&ip, 1); // ipv6enabled = 1
4343
res = addr_resolve(localhost, &ip, NULL);
4444

45-
if (res < 1) {
45+
if (!(res & TOX_ADDR_RESOLVE_INET6)) {
4646
res = addr_resolve("ip6-localhost", &ip, NULL);
4747
localhost_split = 1;
4848
}
4949

5050
ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno));
5151

5252
if (res > 0) {
53-
ck_assert_msg(ip.family == AF_INET6, "Expected family AF_INET6, got %u.", ip.family);
53+
ck_assert_msg(ip.family == AF_INET6, "Expected family AF_INET6 (%u), got %u.", AF_INET6, ip.family);
5454
ck_assert_msg(!memcmp(&ip.ip6, &in6addr_loopback, sizeof(IP6)), "Expected ::1, got %s.", ip_ntoa(&ip));
5555
}
5656

5757
if (!localhost_split) {
58-
ip_init(&ip, 1);
58+
ip_init(&ip, 1); // ipv6enabled = 1
5959
ip.family = AF_UNSPEC;
6060
IP extra;
6161
ip_reset(&extra);
6262
res = addr_resolve(localhost, &ip, &extra);
6363
ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno));
6464

6565
if (res > 0) {
66-
ck_assert_msg(ip.family == AF_INET6, "Expected family AF_INET6, got %u.", ip.family);
66+
ck_assert_msg(ip.family == AF_INET6, "Expected family AF_INET6 (%u), got %u.", AF_INET6, ip.family);
6767
ck_assert_msg(!memcmp(&ip.ip6, &in6addr_loopback, sizeof(IP6)), "Expected ::1, got %s.", ip_ntoa(&ip));
6868

69-
ck_assert_msg(extra.family == AF_INET, "Expected family AF_INET, got %u.", extra.family);
69+
ck_assert_msg(extra.family == AF_INET, "Expected family AF_INET (%u), got %u.", AF_INET, extra.family);
7070
ck_assert_msg(extra.ip4.uint32 == htonl(0x7F000001), "Expected 127.0.0.1, got %s.", inet_ntoa(extra.ip4.in_addr));
7171
}
7272
} else {
@@ -144,7 +144,7 @@ Suite *network_suite(void)
144144
return s;
145145
}
146146

147-
int main(int argc, char *argv[])
147+
int main()
148148
{
149149
srand((unsigned int) time(NULL));
150150

toxcore/network.c

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ int addr_parse_ip(const char *address, IP *to)
939939
* returns in *to a valid IPAny (v4/v6),
940940
* prefers v6 if ip.family was AF_UNSPEC and both available
941941
* returns in *extra an IPv4 address, if family was AF_UNSPEC and *to is AF_INET6
942-
* returns 0 on failure
942+
* returns 0 on failure, TOX_ADDR_RESOLVE_* on success.
943943
*/
944944
int addr_resolve(const char *address, IP *to, IP *extra)
945945
{
@@ -951,7 +951,9 @@ int addr_resolve(const char *address, IP *to, IP *extra)
951951
struct addrinfo *server = NULL;
952952
struct addrinfo *walker = NULL;
953953
struct addrinfo hints;
954-
int rc;
954+
int rc;
955+
int result = 0;
956+
int done = 0;
955957

956958
memset(&hints, 0, sizeof(hints));
957959
hints.ai_family = family;
@@ -968,21 +970,22 @@ int addr_resolve(const char *address, IP *to, IP *extra)
968970
}
969971

970972
IP ip4;
971-
ip_init(&ip4, /* ipv6? */ false);
973+
ip_init(&ip4, 0); // ipv6enabled = 0
972974
IP ip6;
973-
ip_init(&ip6, /* ipv6? */ true);
975+
ip_init(&ip6, 1); // ipv6enabled = 1
974976

975-
for (walker = server; (walker != NULL) && (rc != 3); walker = walker->ai_next) {
977+
for (walker = server; (walker != NULL) && !done; walker = walker->ai_next) {
976978
switch (walker->ai_family) {
977979
case AF_INET:
978980
if (walker->ai_family == family) { /* AF_INET requested, done */
979981
struct sockaddr_in *addr = (struct sockaddr_in *)walker->ai_addr;
980982
to->ip4.in_addr = addr->sin_addr;
981-
rc = 3; // TODO do we really have to reuse variable instead of creating a new one?
982-
} else if (!(rc & 1)) { /* AF_UNSPEC requested, store away */
983+
result = TOX_ADDR_RESOLVE_INET;
984+
done = 1;
985+
} else if (!(result & TOX_ADDR_RESOLVE_INET)) { /* AF_UNSPEC requested, store away */
983986
struct sockaddr_in *addr = (struct sockaddr_in *)walker->ai_addr;
984987
ip4.ip4.in_addr = addr->sin_addr;
985-
rc |= 1; // FIXME magic number
988+
result |= TOX_ADDR_RESOLVE_INET;
986989
}
987990

988991
break; /* switch */
@@ -992,35 +995,37 @@ int addr_resolve(const char *address, IP *to, IP *extra)
992995
if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) {
993996
struct sockaddr_in6 *addr = (struct sockaddr_in6 *)walker->ai_addr;
994997
to->ip6.in6_addr = addr->sin6_addr;
995-
rc = 3;
998+
result = TOX_ADDR_RESOLVE_INET6;
999+
done = 1;
9961000
}
997-
} else if (!(rc & 2)) { /* AF_UNSPEC requested, store away */
1001+
} else if (!(result & TOX_ADDR_RESOLVE_INET6)) { /* AF_UNSPEC requested, store away */
9981002
if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) {
9991003
struct sockaddr_in6 *addr = (struct sockaddr_in6 *)walker->ai_addr;
10001004
ip6.ip6.in6_addr = addr->sin6_addr;
1001-
rc |= 2;
1005+
result |= TOX_ADDR_RESOLVE_INET6;
10021006
}
10031007
}
10041008

10051009
break; /* switch */
10061010
}
10071011
}
10081012

1009-
if (to->family == AF_UNSPEC) {
1010-
if (rc & 2) { // FIXME magic number
1013+
if (family == AF_UNSPEC) {
1014+
if (result & TOX_ADDR_RESOLVE_INET6) {
10111015
ip_copy(to, &ip6);
10121016

1013-
if ((rc & 1) && (extra != NULL)) {
1017+
if ((result & TOX_ADDR_RESOLVE_INET) && (extra != NULL)) {
10141018
ip_copy(extra, &ip4);
10151019
}
1016-
} else if (rc & 1) {
1020+
} else if (result & TOX_ADDR_RESOLVE_INET) {
10171021
ip_copy(to, &ip4);
1018-
} else
1019-
rc = 0;
1022+
} else {
1023+
result = 0;
1024+
}
10201025
}
10211026

10221027
freeaddrinfo(server);
1023-
return rc;
1028+
return result;
10241029
}
10251030

10261031
/*

toxcore/network.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ IP_Port;
176176

177177
#define TOX_ENABLE_IPV6_DEFAULT 1
178178

179+
/* addr_resolve return values */
180+
#define TOX_ADDR_RESOLVE_INET 1
181+
#define TOX_ADDR_RESOLVE_INET6 2
182+
179183
/* ip_ntoa
180184
* converts ip into a string
181185
* uses a static buffer, so mustn't used multiple times in the same output

0 commit comments

Comments
 (0)