summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--auto_tests/network_test.c14
-rw-r--r--toxcore/network.c37
-rw-r--r--toxcore/network.h4
3 files changed, 32 insertions, 23 deletions
diff --git a/auto_tests/network_test.c b/auto_tests/network_test.c
index c1ac8084..adf0189a 100644
--- a/auto_tests/network_test.c
+++ b/auto_tests/network_test.c
@@ -28,7 +28,7 @@ START_TEST(test_addr_resolv_localhost)
28 int localhost_split = 0; 28 int localhost_split = 0;
29 29
30 IP ip; 30 IP ip;
31 ip_init(&ip, 0); 31 ip_init(&ip, /* ipv6? */ 0);
32 32
33 int res = addr_resolve(localhost, &ip, NULL); 33 int res = addr_resolve(localhost, &ip, NULL);
34 34
@@ -39,10 +39,10 @@ START_TEST(test_addr_resolv_localhost)
39 ck_assert_msg(ip.ip4.uint32 == htonl(0x7F000001), "Expected 127.0.0.1, got %s.", inet_ntoa(ip.ip4.in_addr)); 39 ck_assert_msg(ip.ip4.uint32 == htonl(0x7F000001), "Expected 127.0.0.1, got %s.", inet_ntoa(ip.ip4.in_addr));
40 } 40 }
41 41
42 ip_init(&ip, 1); 42 ip_init(&ip, /* ipv6? */ 1);
43 res = addr_resolve(localhost, &ip, NULL); 43 res = addr_resolve(localhost, &ip, NULL);
44 44
45 if (res < 1) { 45 if (!(res & TOX_ADDR_RESOLVE_INET6)) {
46 res = addr_resolve("ip6-localhost", &ip, NULL); 46 res = addr_resolve("ip6-localhost", &ip, NULL);
47 localhost_split = 1; 47 localhost_split = 1;
48 } 48 }
@@ -50,12 +50,12 @@ START_TEST(test_addr_resolv_localhost)
50 ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno)); 50 ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno));
51 51
52 if (res > 0) { 52 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);
54 ck_assert_msg(!memcmp(&ip.ip6, &in6addr_loopback, sizeof(IP6)), "Expected ::1, got %s.", ip_ntoa(&ip)); 54 ck_assert_msg(!memcmp(&ip.ip6, &in6addr_loopback, sizeof(IP6)), "Expected ::1, got %s.", ip_ntoa(&ip));
55 } 55 }
56 56
57 if (!localhost_split) { 57 if (!localhost_split) {
58 ip_init(&ip, 1); 58 ip_init(&ip, /* ipv6? */ 1);
59 ip.family = AF_UNSPEC; 59 ip.family = AF_UNSPEC;
60 IP extra; 60 IP extra;
61 ip_reset(&extra); 61 ip_reset(&extra);
@@ -63,10 +63,10 @@ START_TEST(test_addr_resolv_localhost)
63 ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno)); 63 ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno));
64 64
65 if (res > 0) { 65 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);
67 ck_assert_msg(!memcmp(&ip.ip6, &in6addr_loopback, sizeof(IP6)), "Expected ::1, got %s.", ip_ntoa(&ip)); 67 ck_assert_msg(!memcmp(&ip.ip6, &in6addr_loopback, sizeof(IP6)), "Expected ::1, got %s.", ip_ntoa(&ip));
68 68
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);
70 ck_assert_msg(extra.ip4.uint32 == htonl(0x7F000001), "Expected 127.0.0.1, got %s.", inet_ntoa(extra.ip4.in_addr)); 70 ck_assert_msg(extra.ip4.uint32 == htonl(0x7F000001), "Expected 127.0.0.1, got %s.", inet_ntoa(extra.ip4.in_addr));
71 } 71 }
72 } else { 72 } else {
diff --git a/toxcore/network.c b/toxcore/network.c
index 04efd03b..5ab480b7 100644
--- a/toxcore/network.c
+++ b/toxcore/network.c
@@ -939,7 +939,7 @@ int addr_parse_ip(const char *address, IP *to)
939 * returns in *to a valid IPAny (v4/v6), 939 * returns in *to a valid IPAny (v4/v6),
940 * prefers v6 if ip.family was AF_UNSPEC and both available 940 * prefers v6 if ip.family was AF_UNSPEC and both available
941 * returns in *extra an IPv4 address, if family was AF_UNSPEC and *to is AF_INET6 941 * 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.
943 */ 943 */
944int addr_resolve(const char *address, IP *to, IP *extra) 944int addr_resolve(const char *address, IP *to, IP *extra)
945{ 945{
@@ -951,7 +951,9 @@ int addr_resolve(const char *address, IP *to, IP *extra)
951 struct addrinfo *server = NULL; 951 struct addrinfo *server = NULL;
952 struct addrinfo *walker = NULL; 952 struct addrinfo *walker = NULL;
953 struct addrinfo hints; 953 struct addrinfo hints;
954 int rc; 954 int rc;
955 int result = 0;
956 int done = 0;
955 957
956 memset(&hints, 0, sizeof(hints)); 958 memset(&hints, 0, sizeof(hints));
957 hints.ai_family = family; 959 hints.ai_family = family;
@@ -972,17 +974,18 @@ int addr_resolve(const char *address, IP *to, IP *extra)
972 IP ip6; 974 IP ip6;
973 ip_init(&ip6, /* ipv6? */ true); 975 ip_init(&ip6, /* ipv6? */ true);
974 976
975 for (walker = server; (walker != NULL) && (rc != 3); walker = walker->ai_next) { 977 for (walker = server; (walker != NULL) && !done; walker = walker->ai_next) {
976 switch (walker->ai_family) { 978 switch (walker->ai_family) {
977 case AF_INET: 979 case AF_INET:
978 if (walker->ai_family == family) { /* AF_INET requested, done */ 980 if (walker->ai_family == family) { /* AF_INET requested, done */
979 struct sockaddr_in *addr = (struct sockaddr_in *)walker->ai_addr; 981 struct sockaddr_in *addr = (struct sockaddr_in *)walker->ai_addr;
980 to->ip4.in_addr = addr->sin_addr; 982 to->ip4.in_addr = addr->sin_addr;
981 rc = 3; // TODO do we really have to reuse variable instead of creating a new one? 983 result = TOX_ADDR_RESOLVE_INET;
982 } else if (!(rc & 1)) { /* AF_UNSPEC requested, store away */ 984 done = 1;
985 } else if (!(result & TOX_ADDR_RESOLVE_INET)) { /* AF_UNSPEC requested, store away */
983 struct sockaddr_in *addr = (struct sockaddr_in *)walker->ai_addr; 986 struct sockaddr_in *addr = (struct sockaddr_in *)walker->ai_addr;
984 ip4.ip4.in_addr = addr->sin_addr; 987 ip4.ip4.in_addr = addr->sin_addr;
985 rc |= 1; // FIXME magic number 988 result |= TOX_ADDR_RESOLVE_INET;
986 } 989 }
987 990
988 break; /* switch */ 991 break; /* switch */
@@ -992,13 +995,14 @@ int addr_resolve(const char *address, IP *to, IP *extra)
992 if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) { 995 if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) {
993 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)walker->ai_addr; 996 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)walker->ai_addr;
994 to->ip6.in6_addr = addr->sin6_addr; 997 to->ip6.in6_addr = addr->sin6_addr;
995 rc = 3; 998 result = TOX_ADDR_RESOLVE_INET6;
999 done = 1;
996 } 1000 }
997 } else if (!(rc & 2)) { /* AF_UNSPEC requested, store away */ 1001 } else if (!(result & TOX_ADDR_RESOLVE_INET6)) { /* AF_UNSPEC requested, store away */
998 if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) { 1002 if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) {
999 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)walker->ai_addr; 1003 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)walker->ai_addr;
1000 ip6.ip6.in6_addr = addr->sin6_addr; 1004 ip6.ip6.in6_addr = addr->sin6_addr;
1001 rc |= 2; 1005 result |= TOX_ADDR_RESOLVE_INET6;
1002 } 1006 }
1003 } 1007 }
1004 1008
@@ -1006,21 +1010,22 @@ int addr_resolve(const char *address, IP *to, IP *extra)
1006 } 1010 }
1007 } 1011 }
1008 1012
1009 if (to->family == AF_UNSPEC) { 1013 if (family == AF_UNSPEC) {
1010 if (rc & 2) { // FIXME magic number 1014 if (result & TOX_ADDR_RESOLVE_INET6) {
1011 ip_copy(to, &ip6); 1015 ip_copy(to, &ip6);
1012 1016
1013 if ((rc & 1) && (extra != NULL)) { 1017 if ((result & TOX_ADDR_RESOLVE_INET) && (extra != NULL)) {
1014 ip_copy(extra, &ip4); 1018 ip_copy(extra, &ip4);
1015 } 1019 }
1016 } else if (rc & 1) { 1020 } else if (result & TOX_ADDR_RESOLVE_INET) {
1017 ip_copy(to, &ip4); 1021 ip_copy(to, &ip4);
1018 } else 1022 } else {
1019 rc = 0; 1023 result = 0;
1024 }
1020 } 1025 }
1021 1026
1022 freeaddrinfo(server); 1027 freeaddrinfo(server);
1023 return rc; 1028 return result;
1024} 1029}
1025 1030
1026/* 1031/*
diff --git a/toxcore/network.h b/toxcore/network.h
index 56494562..8d2ccfce 100644
--- a/toxcore/network.h
+++ b/toxcore/network.h
@@ -176,6 +176,10 @@ IP_Port;
176 176
177#define TOX_ENABLE_IPV6_DEFAULT 1 177#define TOX_ENABLE_IPV6_DEFAULT 1
178 178
179/* addr_resolve return values */
180#define TOX_ADDR_RESOLVE_INET 1
181#define TOX_ADDR_RESOLVE_INET6 2
182
179/* ip_ntoa 183/* ip_ntoa
180 * converts ip into a string 184 * converts ip into a string
181 * uses a static buffer, so mustn't used multiple times in the same output 185 * uses a static buffer, so mustn't used multiple times in the same output