summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--auto_tests/TCP_test.c13
-rw-r--r--auto_tests/network_test.c10
-rw-r--r--toxcore/Messenger.c2
-rw-r--r--toxcore/network.c39
-rw-r--r--toxcore/network.h4
5 files changed, 38 insertions, 30 deletions
diff --git a/auto_tests/TCP_test.c b/auto_tests/TCP_test.c
index 5c656541..898c5e08 100644
--- a/auto_tests/TCP_test.c
+++ b/auto_tests/TCP_test.c
@@ -35,7 +35,7 @@ START_TEST(test_basic)
35 Socket sock = net_socket(TOX_AF_INET6, TOX_SOCK_STREAM, TOX_PROTO_TCP); 35 Socket sock = net_socket(TOX_AF_INET6, TOX_SOCK_STREAM, TOX_PROTO_TCP);
36 IP_Port ip_port_loopback; 36 IP_Port ip_port_loopback;
37 ip_port_loopback.ip.family = TOX_AF_INET6; 37 ip_port_loopback.ip.family = TOX_AF_INET6;
38 ip_port_loopback.ip.ip6 = IP6_LOOPBACK; 38 ip_port_loopback.ip.ip6 = get_ip6_loopback();
39 ip_port_loopback.port = net_htons(ports[rand() % NUM_PORTS]); 39 ip_port_loopback.port = net_htons(ports[rand() % NUM_PORTS]);
40 40
41 int ret = net_connect(sock, ip_port_loopback); 41 int ret = net_connect(sock, ip_port_loopback);
@@ -135,8 +135,7 @@ static struct sec_TCP_con *new_TCP_con(TCP_Server *tcp_s)
135 IP_Port ip_port_loopback; 135 IP_Port ip_port_loopback;
136 ip_port_loopback.ip.family = TOX_AF_INET6; 136 ip_port_loopback.ip.family = TOX_AF_INET6;
137 ip_port_loopback.ip.ip6.uint64[0] = 0; 137 ip_port_loopback.ip.ip6.uint64[0] = 0;
138 ip_port_loopback.ip.ip6.uint64[1] = 0; 138 ip_port_loopback.ip.ip4 = get_ip4_loopback();
139 ip_port_loopback.ip.ip6.uint8[15] = 1; // ::1
140 ip_port_loopback.port = net_htons(ports[rand() % NUM_PORTS]); 139 ip_port_loopback.port = net_htons(ports[rand() % NUM_PORTS]);
141 140
142 int ret = net_connect(sock, ip_port_loopback); 141 int ret = net_connect(sock, ip_port_loopback);
@@ -404,7 +403,7 @@ START_TEST(test_client)
404 403
405 ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); 404 ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]);
406 ip_port_tcp_s.ip.family = TOX_AF_INET6; 405 ip_port_tcp_s.ip.family = TOX_AF_INET6;
407 ip_port_tcp_s.ip.ip6 = IP6_LOOPBACK; 406 ip_port_tcp_s.ip.ip6 = get_ip6_loopback();
408 TCP_Client_Connection *conn = new_TCP_connection(ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, 0); 407 TCP_Client_Connection *conn = new_TCP_connection(ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, 0);
409 c_sleep(50); 408 c_sleep(50);
410 do_TCP_connection(conn, NULL); 409 do_TCP_connection(conn, NULL);
@@ -502,7 +501,7 @@ START_TEST(test_client_invalid)
502 501
503 ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); 502 ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]);
504 ip_port_tcp_s.ip.family = TOX_AF_INET6; 503 ip_port_tcp_s.ip.family = TOX_AF_INET6;
505 ip_port_tcp_s.ip.ip6 = IP6_LOOPBACK; 504 ip_port_tcp_s.ip.ip6 = get_ip6_loopback();
506 TCP_Client_Connection *conn = new_TCP_connection(ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, 0); 505 TCP_Client_Connection *conn = new_TCP_connection(ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, 0);
507 c_sleep(50); 506 c_sleep(50);
508 do_TCP_connection(conn, NULL); 507 do_TCP_connection(conn, NULL);
@@ -571,7 +570,7 @@ START_TEST(test_tcp_connection)
571 570
572 ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); 571 ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]);
573 ip_port_tcp_s.ip.family = TOX_AF_INET6; 572 ip_port_tcp_s.ip.family = TOX_AF_INET6;
574 ip_port_tcp_s.ip.ip6 = IP6_LOOPBACK; 573 ip_port_tcp_s.ip.ip6 = get_ip6_loopback();
575 574
576 int connection = new_tcp_connection_to(tc_1, tcp_connections_public_key(tc_2), 123); 575 int connection = new_tcp_connection_to(tc_1, tcp_connections_public_key(tc_2), 123);
577 ck_assert_msg(connection == 0, "Connection id wrong"); 576 ck_assert_msg(connection == 0, "Connection id wrong");
@@ -680,7 +679,7 @@ START_TEST(test_tcp_connection2)
680 679
681 ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); 680 ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]);
682 ip_port_tcp_s.ip.family = TOX_AF_INET6; 681 ip_port_tcp_s.ip.family = TOX_AF_INET6;
683 ip_port_tcp_s.ip.ip6 = IP6_LOOPBACK; 682 ip_port_tcp_s.ip.ip6 = get_ip6_loopback();
684 683
685 int connection = new_tcp_connection_to(tc_1, tcp_connections_public_key(tc_2), 123); 684 int connection = new_tcp_connection_to(tc_1, tcp_connections_public_key(tc_2), 123);
686 ck_assert_msg(connection == 0, "Connection id wrong"); 685 ck_assert_msg(connection == 0, "Connection id wrong");
diff --git a/auto_tests/network_test.c b/auto_tests/network_test.c
index 200c04db..4713528d 100644
--- a/auto_tests/network_test.c
+++ b/auto_tests/network_test.c
@@ -38,7 +38,7 @@ START_TEST(test_addr_resolv_localhost)
38 38
39 char ip_str[IP_NTOA_LEN]; 39 char ip_str[IP_NTOA_LEN];
40 ck_assert_msg(ip.family == TOX_AF_INET, "Expected family TOX_AF_INET, got %u.", ip.family); 40 ck_assert_msg(ip.family == TOX_AF_INET, "Expected family TOX_AF_INET, got %u.", ip.family);
41 const uint32_t loopback = net_htonl(IP4_LOOPBACK.uint32); 41 const uint32_t loopback = get_ip4_loopback().uint32;
42 ck_assert_msg(ip.ip4.uint32 == loopback, "Expected 127.0.0.1, got %s.", 42 ck_assert_msg(ip.ip4.uint32 == loopback, "Expected 127.0.0.1, got %s.",
43 ip_ntoa(&ip, ip_str, sizeof(ip_str))); 43 ip_ntoa(&ip, ip_str, sizeof(ip_str)));
44 44
@@ -53,7 +53,8 @@ START_TEST(test_addr_resolv_localhost)
53 ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno)); 53 ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno));
54 54
55 ck_assert_msg(ip.family == TOX_AF_INET6, "Expected family TOX_AF_INET6 (%u), got %u.", TOX_AF_INET6, ip.family); 55 ck_assert_msg(ip.family == TOX_AF_INET6, "Expected family TOX_AF_INET6 (%u), got %u.", TOX_AF_INET6, ip.family);
56 ck_assert_msg(!memcmp(&ip.ip6, &IP6_LOOPBACK, sizeof(IP6)), "Expected ::1, got %s.", 56 IP6 ip6_loopback = get_ip6_loopback();
57 ck_assert_msg(!memcmp(&ip.ip6, &ip6_loopback, sizeof(IP6)), "Expected ::1, got %s.",
57 ip_ntoa(&ip, ip_str, sizeof(ip_str))); 58 ip_ntoa(&ip, ip_str, sizeof(ip_str)));
58 59
59 if (localhost_split) { 60 if (localhost_split) {
@@ -69,7 +70,7 @@ START_TEST(test_addr_resolv_localhost)
69 ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno)); 70 ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno));
70 71
71 ck_assert_msg(ip.family == TOX_AF_INET6, "Expected family TOX_AF_INET6 (%u), got %u.", TOX_AF_INET6, ip.family); 72 ck_assert_msg(ip.family == TOX_AF_INET6, "Expected family TOX_AF_INET6 (%u), got %u.", TOX_AF_INET6, ip.family);
72 ck_assert_msg(!memcmp(&ip.ip6, &IP6_LOOPBACK, sizeof(IP6)), "Expected ::1, got %s.", 73 ck_assert_msg(!memcmp(&ip.ip6, &ip6_loopback, sizeof(IP6)), "Expected ::1, got %s.",
73 ip_ntoa(&ip, ip_str, sizeof(ip_str))); 74 ip_ntoa(&ip, ip_str, sizeof(ip_str)));
74 75
75 ck_assert_msg(extra.family == TOX_AF_INET, "Expected family TOX_AF_INET (%u), got %u.", TOX_AF_INET, extra.family); 76 ck_assert_msg(extra.family == TOX_AF_INET, "Expected family TOX_AF_INET (%u), got %u.", TOX_AF_INET, extra.family);
@@ -127,7 +128,8 @@ START_TEST(test_ip_equal)
127 ck_assert_msg(res != 0, "ip_equal( {TOX_AF_INET, 127.0.0.1}, {TOX_AF_INET6, ::ffff:127.0.0.1} ): " 128 ck_assert_msg(res != 0, "ip_equal( {TOX_AF_INET, 127.0.0.1}, {TOX_AF_INET6, ::ffff:127.0.0.1} ): "
128 "expected result != 0, got 0."); 129 "expected result != 0, got 0.");
129 130
130 memcpy(&ip2.ip6, &IP6_LOOPBACK, sizeof(IP6)); 131 IP6 ip6_loopback = get_ip6_loopback();
132 memcpy(&ip2.ip6, &ip6_loopback, sizeof(IP6));
131 res = ip_equal(&ip1, &ip2); 133 res = ip_equal(&ip1, &ip2);
132 ck_assert_msg(res == 0, "ip_equal( {TOX_AF_INET, 127.0.0.1}, {TOX_AF_INET6, ::1} ): expected result 0, got %u.", res); 134 ck_assert_msg(res == 0, "ip_equal( {TOX_AF_INET, 127.0.0.1}, {TOX_AF_INET6, ::1} ): expected result 0, got %u.", res);
133 135
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 47d6d57a..4dd7d0c4 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -2527,7 +2527,7 @@ void do_messenger(Messenger *m, void *userdata)
2527 IP_Port local_ip_port; 2527 IP_Port local_ip_port;
2528 local_ip_port.port = m->options.tcp_server_port; 2528 local_ip_port.port = m->options.tcp_server_port;
2529 local_ip_port.ip.family = TOX_AF_INET; 2529 local_ip_port.ip.family = TOX_AF_INET;
2530 local_ip_port.ip.ip4 = IP4_LOOPBACK; 2530 local_ip_port.ip.ip4 = get_ip4_loopback();
2531 add_tcp_relay(m->net_crypto, local_ip_port, 2531 add_tcp_relay(m->net_crypto, local_ip_port,
2532 tcp_server_public_key(m->tcp_server)); 2532 tcp_server_public_key(m->tcp_server));
2533 } 2533 }
diff --git a/toxcore/network.c b/toxcore/network.c
index aeaf4d79..5c43bf57 100644
--- a/toxcore/network.c
+++ b/toxcore/network.c
@@ -138,21 +138,6 @@ static int inet_pton(Family family, const char *addrString, void *addrbuf)
138 138
139#endif 139#endif
140 140
141#if !defined(INADDR_LOOPBACK)
142#define INADDR_LOOPBACK 0x7f000001
143#endif
144
145const IP4 IP4_LOOPBACK = { INADDR_LOOPBACK };
146const IP6 IP6_LOOPBACK = {
147 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }
148};
149
150
151const IP4 IP4_BROADCAST = { INADDR_BROADCAST };
152const IP6 IP6_BROADCAST = {
153 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }
154};
155
156#if TOX_INET6_ADDRSTRLEN < INET6_ADDRSTRLEN 141#if TOX_INET6_ADDRSTRLEN < INET6_ADDRSTRLEN
157#error TOX_INET6_ADDRSTRLEN should be greater or equal to INET6_ADDRSTRLEN (#INET6_ADDRSTRLEN) 142#error TOX_INET6_ADDRSTRLEN should be greater or equal to INET6_ADDRSTRLEN (#INET6_ADDRSTRLEN)
158#endif 143#endif
@@ -188,6 +173,29 @@ static void fill_addr6(IP6 ip, struct in6_addr *addr)
188 memcpy(addr->s6_addr, ip.uint8, sizeof(ip.uint8)); 173 memcpy(addr->s6_addr, ip.uint8, sizeof(ip.uint8));
189} 174}
190 175
176#if !defined(INADDR_LOOPBACK)
177#define INADDR_LOOPBACK 0x7f000001
178#endif
179
180const IP4 IP4_BROADCAST = { INADDR_BROADCAST };
181const IP6 IP6_BROADCAST = {
182 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }
183};
184
185IP4 get_ip4_loopback()
186{
187 IP4 loopback;
188 loopback.uint32 = htonl(INADDR_LOOPBACK);
189 return loopback;
190}
191
192IP6 get_ip6_loopback()
193{
194 IP6 loopback;
195 get_ip6(&loopback, &in6addr_loopback);
196 return loopback;
197}
198
191/* Check if socket is valid. 199/* Check if socket is valid.
192 * 200 *
193 * return 1 if valid 201 * return 1 if valid
@@ -1436,4 +1444,3 @@ uint16_t net_ntohs(uint16_t hostshort)
1436{ 1444{
1437 return ntohs(hostshort); 1445 return ntohs(hostshort);
1438} 1446}
1439
diff --git a/toxcore/network.h b/toxcore/network.h
index 3c98c168..0b9da5a4 100644
--- a/toxcore/network.h
+++ b/toxcore/network.h
@@ -132,7 +132,7 @@ typedef union {
132} 132}
133IP4; 133IP4;
134 134
135extern const IP4 IP4_LOOPBACK; 135IP4 get_ip4_loopback(void);
136extern const IP4 IP4_BROADCAST; 136extern const IP4 IP4_BROADCAST;
137 137
138typedef union { 138typedef union {
@@ -143,7 +143,7 @@ typedef union {
143} 143}
144IP6; 144IP6;
145 145
146extern const IP6 IP6_LOOPBACK; 146IP6 get_ip6_loopback(void);
147extern const IP6 IP6_BROADCAST; 147extern const IP6 IP6_BROADCAST;
148 148
149typedef struct { 149typedef struct {