summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDiadlo <polsha3@gmail.com>2017-01-03 19:26:15 +0300
committerDiadlo <polsha3@gmail.com>2017-01-26 23:11:15 +0300
commitf00006cf1d966270b6142519ad7d8487fb5eff5a (patch)
treeb62ef423ad71abfe2f6a53ca0fede7510c07fdd5
parent287a29b8269eabfb33f07b9e0388a68be7ad2ba0 (diff)
Add platform-independent Socket and IP implementation
-rw-r--r--auto_tests/TCP_test.c14
-rw-r--r--auto_tests/network_test.c10
-rw-r--r--auto_tests/resource_leak_test.c4
-rw-r--r--testing/dns3_test.c4
-rw-r--r--toxav/bwcontroller.c1
-rw-r--r--toxav/rtp.c1
-rw-r--r--toxav/toxav.c1
-rw-r--r--toxcore/LAN_discovery.c5
-rw-r--r--toxcore/TCP_client.c8
-rw-r--r--toxcore/TCP_client.h2
-rw-r--r--toxcore/TCP_server.c30
-rw-r--r--toxcore/TCP_server.h10
-rw-r--r--toxcore/network.c111
-rw-r--r--toxcore/network.h62
-rw-r--r--toxcore/tox.c12
15 files changed, 159 insertions, 116 deletions
diff --git a/auto_tests/TCP_test.c b/auto_tests/TCP_test.c
index 6a439261..a1de4874 100644
--- a/auto_tests/TCP_test.c
+++ b/auto_tests/TCP_test.c
@@ -38,7 +38,7 @@ START_TEST(test_basic)
38 ck_assert_msg(tcp_s != NULL, "Failed to create TCP relay server"); 38 ck_assert_msg(tcp_s != NULL, "Failed to create TCP relay server");
39 ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind to all ports"); 39 ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind to all ports");
40 40
41 sock_t sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); 41 Socket sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
42 struct sockaddr_in6 addr6_loopback = {0}; 42 struct sockaddr_in6 addr6_loopback = {0};
43 addr6_loopback.sin6_family = AF_INET6; 43 addr6_loopback.sin6_family = AF_INET6;
44 addr6_loopback.sin6_port = htons(ports[rand() % NUM_PORTS]); 44 addr6_loopback.sin6_port = htons(ports[rand() % NUM_PORTS]);
@@ -126,7 +126,7 @@ START_TEST(test_basic)
126END_TEST 126END_TEST
127 127
128struct sec_TCP_con { 128struct sec_TCP_con {
129 sock_t sock; 129 Socket sock;
130 uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; 130 uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE];
131 uint8_t recv_nonce[CRYPTO_NONCE_SIZE]; 131 uint8_t recv_nonce[CRYPTO_NONCE_SIZE];
132 uint8_t sent_nonce[CRYPTO_NONCE_SIZE]; 132 uint8_t sent_nonce[CRYPTO_NONCE_SIZE];
@@ -136,7 +136,7 @@ struct sec_TCP_con {
136static struct sec_TCP_con *new_TCP_con(TCP_Server *tcp_s) 136static struct sec_TCP_con *new_TCP_con(TCP_Server *tcp_s)
137{ 137{
138 struct sec_TCP_con *sec_c = (struct sec_TCP_con *)malloc(sizeof(struct sec_TCP_con)); 138 struct sec_TCP_con *sec_c = (struct sec_TCP_con *)malloc(sizeof(struct sec_TCP_con));
139 sock_t sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); 139 Socket sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
140 struct sockaddr_in6 addr6_loopback = {0}; 140 struct sockaddr_in6 addr6_loopback = {0};
141 addr6_loopback.sin6_family = AF_INET6; 141 addr6_loopback.sin6_family = AF_INET6;
142 addr6_loopback.sin6_port = htons(ports[rand() % NUM_PORTS]); 142 addr6_loopback.sin6_port = htons(ports[rand() % NUM_PORTS]);
@@ -407,7 +407,7 @@ START_TEST(test_client)
407 407
408 ip_port_tcp_s.port = htons(ports[rand() % NUM_PORTS]); 408 ip_port_tcp_s.port = htons(ports[rand() % NUM_PORTS]);
409 ip_port_tcp_s.ip.family = AF_INET6; 409 ip_port_tcp_s.ip.family = AF_INET6;
410 ip_port_tcp_s.ip.ip6.in6_addr = in6addr_loopback; 410 get_ip6(&ip_port_tcp_s.ip.ip6, &in6addr_loopback);
411 TCP_Client_Connection *conn = new_TCP_connection(ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, 0); 411 TCP_Client_Connection *conn = new_TCP_connection(ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, 0);
412 c_sleep(50); 412 c_sleep(50);
413 do_TCP_connection(conn, NULL); 413 do_TCP_connection(conn, NULL);
@@ -505,7 +505,7 @@ START_TEST(test_client_invalid)
505 505
506 ip_port_tcp_s.port = htons(ports[rand() % NUM_PORTS]); 506 ip_port_tcp_s.port = htons(ports[rand() % NUM_PORTS]);
507 ip_port_tcp_s.ip.family = AF_INET6; 507 ip_port_tcp_s.ip.family = AF_INET6;
508 ip_port_tcp_s.ip.ip6.in6_addr = in6addr_loopback; 508 get_ip6(&ip_port_tcp_s.ip.ip6, &in6addr_loopback);
509 TCP_Client_Connection *conn = new_TCP_connection(ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, 0); 509 TCP_Client_Connection *conn = new_TCP_connection(ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, 0);
510 c_sleep(50); 510 c_sleep(50);
511 do_TCP_connection(conn, NULL); 511 do_TCP_connection(conn, NULL);
@@ -574,7 +574,7 @@ START_TEST(test_tcp_connection)
574 574
575 ip_port_tcp_s.port = htons(ports[rand() % NUM_PORTS]); 575 ip_port_tcp_s.port = htons(ports[rand() % NUM_PORTS]);
576 ip_port_tcp_s.ip.family = AF_INET6; 576 ip_port_tcp_s.ip.family = AF_INET6;
577 ip_port_tcp_s.ip.ip6.in6_addr = in6addr_loopback; 577 get_ip6(&ip_port_tcp_s.ip.ip6, &in6addr_loopback);
578 578
579 int connection = new_tcp_connection_to(tc_1, tcp_connections_public_key(tc_2), 123); 579 int connection = new_tcp_connection_to(tc_1, tcp_connections_public_key(tc_2), 123);
580 ck_assert_msg(connection == 0, "Connection id wrong"); 580 ck_assert_msg(connection == 0, "Connection id wrong");
@@ -683,7 +683,7 @@ START_TEST(test_tcp_connection2)
683 683
684 ip_port_tcp_s.port = htons(ports[rand() % NUM_PORTS]); 684 ip_port_tcp_s.port = htons(ports[rand() % NUM_PORTS]);
685 ip_port_tcp_s.ip.family = AF_INET6; 685 ip_port_tcp_s.ip.family = AF_INET6;
686 ip_port_tcp_s.ip.ip6.in6_addr = in6addr_loopback; 686 get_ip6(&ip_port_tcp_s.ip.ip6, &in6addr_loopback);
687 687
688 int connection = new_tcp_connection_to(tc_1, tcp_connections_public_key(tc_2), 123); 688 int connection = new_tcp_connection_to(tc_1, tcp_connections_public_key(tc_2), 123);
689 ck_assert_msg(connection == 0, "Connection id wrong"); 689 ck_assert_msg(connection == 0, "Connection id wrong");
diff --git a/auto_tests/network_test.c b/auto_tests/network_test.c
index 2ce6d0b5..ea08c1b8 100644
--- a/auto_tests/network_test.c
+++ b/auto_tests/network_test.c
@@ -3,6 +3,7 @@
3#endif 3#endif
4 4
5#include <check.h> 5#include <check.h>
6#include <errno.h>
6#include <stdint.h> 7#include <stdint.h>
7#include <stdlib.h> 8#include <stdlib.h>
8#include <string.h> 9#include <string.h>
@@ -36,8 +37,9 @@ START_TEST(test_addr_resolv_localhost)
36 37
37 if (res > 0) { 38 if (res > 0) {
38 ck_assert_msg(ip.family == AF_INET, "Expected family AF_INET, got %u.", ip.family); 39 ck_assert_msg(ip.family == AF_INET, "Expected family AF_INET, got %u.", ip.family);
39 ck_assert_msg(ip.ip4.uint32 == htonl(0x7F000001), "Expected 127.0.0.1, got %s.", 40 struct in_addr addr;
40 inet_ntoa(ip.ip4.in_addr)); 41 fill_addr4(ip.ip4, &addr);
42 ck_assert_msg(ip.ip4.uint32 == htonl(0x7F000001), "Expected 127.0.0.1, got %s.", inet_ntoa(addr));
41 } 43 }
42 44
43 ip_init(&ip, 1); // ipv6enabled = 1 45 ip_init(&ip, 1); // ipv6enabled = 1
@@ -71,8 +73,10 @@ START_TEST(test_addr_resolv_localhost)
71 ck_assert_msg(!memcmp(&ip.ip6, &in6addr_loopback, sizeof(IP6)), "Expected ::1, got %s.", 73 ck_assert_msg(!memcmp(&ip.ip6, &in6addr_loopback, sizeof(IP6)), "Expected ::1, got %s.",
72 ip_ntoa(&ip, ip_str, sizeof(ip_str))); 74 ip_ntoa(&ip, ip_str, sizeof(ip_str)));
73 75
76 struct in_addr addr;
77 fill_addr4(ip.ip4, &addr);
74 ck_assert_msg(extra.family == AF_INET, "Expected family AF_INET (%u), got %u.", AF_INET, extra.family); 78 ck_assert_msg(extra.family == AF_INET, "Expected family AF_INET (%u), got %u.", AF_INET, extra.family);
75 ck_assert_msg(extra.ip4.uint32 == htonl(0x7F000001), "Expected 127.0.0.1, got %s.", inet_ntoa(extra.ip4.in_addr)); 79 ck_assert_msg(extra.ip4.uint32 == htonl(0x7F000001), "Expected 127.0.0.1, got %s.", inet_ntoa(addr));
76 } 80 }
77 } else { 81 } else {
78 printf("Localhost seems to be split in two.\n"); 82 printf("Localhost seems to be split in two.\n");
diff --git a/auto_tests/resource_leak_test.c b/auto_tests/resource_leak_test.c
index aab1fcd7..3a87e245 100644
--- a/auto_tests/resource_leak_test.c
+++ b/auto_tests/resource_leak_test.c
@@ -1,5 +1,9 @@
1#include "helpers.h" 1#include "helpers.h"
2 2
3#if defined(__AIX__)
4# define _XOPEN_SOURCE 1
5#endif
6
3// See man 2 sbrk. 7// See man 2 sbrk.
4#if _BSD_SOURCE || _SVID_SOURCE || \ 8#if _BSD_SOURCE || _SVID_SOURCE || \
5 (_XOPEN_SOURCE >= 500 || \ 9 (_XOPEN_SOURCE >= 500 || \
diff --git a/testing/dns3_test.c b/testing/dns3_test.c
index a2c0ea5b..021a09f9 100644
--- a/testing/dns3_test.c
+++ b/testing/dns3_test.c
@@ -50,7 +50,7 @@ int main(int argc, char *argv[])
50 50
51 IP ip = {0}; 51 IP ip = {0};
52 ip.family = AF_INET; 52 ip.family = AF_INET;
53 sock_t sock = socket(ip.family, SOCK_DGRAM, IPPROTO_UDP); 53 Socket sock = socket(ip.family, SOCK_DGRAM, IPPROTO_UDP);
54 54
55 if (!sock_valid(sock)) { 55 if (!sock_valid(sock)) {
56 return -1; 56 return -1;
@@ -66,7 +66,7 @@ int main(int argc, char *argv[])
66 66
67 target.sin_family = AF_INET; 67 target.sin_family = AF_INET;
68 68
69 target.sin_addr = ip.ip4.in_addr; 69 fill_addr4(ip.ip4, &target.sin_addr);
70 70
71 target.sin_port = htons(53); 71 target.sin_port = htons(53);
72 72
diff --git a/toxav/bwcontroller.c b/toxav/bwcontroller.c
index 19d9c719..397cebff 100644
--- a/toxav/bwcontroller.c
+++ b/toxav/bwcontroller.c
@@ -29,6 +29,7 @@
29#include "../toxcore/util.h" 29#include "../toxcore/util.h"
30 30
31#include <assert.h> 31#include <assert.h>
32#include <errno.h>
32 33
33#define BWC_PACKET_ID 196 34#define BWC_PACKET_ID 196
34#define BWC_SEND_INTERVAL_MS 1000 35#define BWC_SEND_INTERVAL_MS 1000
diff --git a/toxav/rtp.c b/toxav/rtp.c
index 5aff6af2..9b7b1bfe 100644
--- a/toxav/rtp.c
+++ b/toxav/rtp.c
@@ -30,6 +30,7 @@
30#include "../toxcore/util.h" 30#include "../toxcore/util.h"
31 31
32#include <assert.h> 32#include <assert.h>
33#include <errno.h>
33#include <stdlib.h> 34#include <stdlib.h>
34 35
35 36
diff --git a/toxav/toxav.c b/toxav/toxav.c
index 0b953650..1710f2a4 100644
--- a/toxav/toxav.c
+++ b/toxav/toxav.c
@@ -31,6 +31,7 @@
31#include "../toxcore/util.h" 31#include "../toxcore/util.h"
32 32
33#include <assert.h> 33#include <assert.h>
34#include <errno.h>
34#include <stdlib.h> 35#include <stdlib.h>
35#include <string.h> 36#include <string.h>
36 37
diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c
index 40c64cda..e26f7eea 100644
--- a/toxcore/LAN_discovery.c
+++ b/toxcore/LAN_discovery.c
@@ -121,7 +121,8 @@ static void fetch_broadcast_info(uint16_t port)
121 * so it's wrapped in __linux for now. 121 * so it's wrapped in __linux for now.
122 * Definitely won't work like this on Windows... 122 * Definitely won't work like this on Windows...
123 */ 123 */
124 sock_t sock = 0; 124 broadcast_count = 0;
125 Socket sock = 0;
125 126
126 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { 127 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
127 return; 128 return;
@@ -173,7 +174,7 @@ static void fetch_broadcast_info(uint16_t port)
173 174
174 IP_Port *ip_port = &ip_ports[count]; 175 IP_Port *ip_port = &ip_ports[count];
175 ip_port->ip.family = AF_INET; 176 ip_port->ip.family = AF_INET;
176 ip_port->ip.ip4.in_addr = sock4->sin_addr; 177 get_ip4(&ip_port->ip.ip4, &sock4->sin_addr);
177 178
178 if (ip_port->ip.ip4.uint32 == 0) { 179 if (ip_port->ip.ip4.uint32 == 0) {
179 continue; 180 continue;
diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c
index e6d8e3b4..3d65415e 100644
--- a/toxcore/TCP_client.c
+++ b/toxcore/TCP_client.c
@@ -36,7 +36,7 @@
36/* return 1 on success 36/* return 1 on success
37 * return 0 on failure 37 * return 0 on failure
38 */ 38 */
39static int connect_sock_to(sock_t sock, IP_Port ip_port, TCP_Proxy_Info *proxy_info) 39static int connect_sock_to(Socket sock, IP_Port ip_port, TCP_Proxy_Info *proxy_info)
40{ 40{
41 if (proxy_info->proxy_type != TCP_PROXY_NONE) { 41 if (proxy_info->proxy_type != TCP_PROXY_NONE) {
42 ip_port = proxy_info->ip_port; 42 ip_port = proxy_info->ip_port;
@@ -51,14 +51,14 @@ static int connect_sock_to(sock_t sock, IP_Port ip_port, TCP_Proxy_Info *proxy_i
51 51
52 addrsize = sizeof(struct sockaddr_in); 52 addrsize = sizeof(struct sockaddr_in);
53 addr4->sin_family = AF_INET; 53 addr4->sin_family = AF_INET;
54 addr4->sin_addr = ip_port.ip.ip4.in_addr; 54 fill_addr4(ip_port.ip.ip4, &addr4->sin_addr);
55 addr4->sin_port = ip_port.port; 55 addr4->sin_port = ip_port.port;
56 } else if (ip_port.ip.family == AF_INET6) { 56 } else if (ip_port.ip.family == AF_INET6) {
57 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; 57 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr;
58 58
59 addrsize = sizeof(struct sockaddr_in6); 59 addrsize = sizeof(struct sockaddr_in6);
60 addr6->sin6_family = AF_INET6; 60 addr6->sin6_family = AF_INET6;
61 addr6->sin6_addr = ip_port.ip.ip6.in6_addr; 61 fill_addr6(ip_port.ip.ip6, &addr6->sin6_addr);
62 addr6->sin6_port = ip_port.port; 62 addr6->sin6_port = ip_port.port;
63 } else { 63 } else {
64 return 0; 64 return 0;
@@ -653,7 +653,7 @@ TCP_Client_Connection *new_TCP_connection(IP_Port ip_port, const uint8_t *public
653 family = proxy_info->ip_port.ip.family; 653 family = proxy_info->ip_port.ip.family;
654 } 654 }
655 655
656 sock_t sock = socket(family, SOCK_STREAM, IPPROTO_TCP); 656 Socket sock = socket(family, SOCK_STREAM, IPPROTO_TCP);
657 657
658 if (!sock_valid(sock)) { 658 if (!sock_valid(sock)) {
659 return NULL; 659 return NULL;
diff --git a/toxcore/TCP_client.h b/toxcore/TCP_client.h
index 5e912d2a..21254314 100644
--- a/toxcore/TCP_client.h
+++ b/toxcore/TCP_client.h
@@ -52,7 +52,7 @@ enum {
52}; 52};
53typedef struct { 53typedef struct {
54 uint8_t status; 54 uint8_t status;
55 sock_t sock; 55 Socket sock;
56 uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; /* our public key */ 56 uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; /* our public key */
57 uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; /* public key of the server */ 57 uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; /* public key of the server */
58 IP_Port ip_port; /* The ip and port of the server */ 58 IP_Port ip_port; /* The ip and port of the server */
diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c
index 7a7a4fa7..47ad9933 100644
--- a/toxcore/TCP_server.c
+++ b/toxcore/TCP_server.c
@@ -40,7 +40,7 @@ struct TCP_Server {
40 int efd; 40 int efd;
41 uint64_t last_run_pinged; 41 uint64_t last_run_pinged;
42#endif 42#endif
43 sock_t *socks_listening; 43 Socket *socks_listening;
44 unsigned int num_listening_socks; 44 unsigned int num_listening_socks;
45 45
46 uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; 46 uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE];
@@ -78,7 +78,7 @@ size_t tcp_server_listen_count(const TCP_Server *tcp_server)
78/* return 1 on success 78/* return 1 on success
79 * return 0 on failure 79 * return 0 on failure
80 */ 80 */
81static int bind_to_port(sock_t sock, int family, uint16_t port) 81static int bind_to_port(Socket sock, int family, uint16_t port)
82{ 82{
83 struct sockaddr_storage addr = {0}; 83 struct sockaddr_storage addr = {0};
84 size_t addrsize; 84 size_t addrsize;
@@ -232,7 +232,7 @@ static int del_accepted(TCP_Server *TCP_server, int index)
232/* return the amount of data in the tcp recv buffer. 232/* return the amount of data in the tcp recv buffer.
233 * return 0 on failure. 233 * return 0 on failure.
234 */ 234 */
235unsigned int TCP_socket_data_recv_buffer(sock_t sock) 235unsigned int TCP_socket_data_recv_buffer(Socket sock)
236{ 236{
237#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) 237#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
238 unsigned long count = 0; 238 unsigned long count = 0;
@@ -252,7 +252,7 @@ unsigned int TCP_socket_data_recv_buffer(sock_t sock)
252 * return 0 if nothing has been read from socket. 252 * return 0 if nothing has been read from socket.
253 * return ~0 on failure. 253 * return ~0 on failure.
254 */ 254 */
255uint16_t read_TCP_length(sock_t sock) 255uint16_t read_TCP_length(Socket sock)
256{ 256{
257 unsigned int count = TCP_socket_data_recv_buffer(sock); 257 unsigned int count = TCP_socket_data_recv_buffer(sock);
258 258
@@ -282,7 +282,7 @@ uint16_t read_TCP_length(sock_t sock)
282 * return length on success 282 * return length on success
283 * return -1 on failure/no data in buffer. 283 * return -1 on failure/no data in buffer.
284 */ 284 */
285int read_TCP_packet(sock_t sock, uint8_t *data, uint16_t length) 285int read_TCP_packet(Socket sock, uint8_t *data, uint16_t length)
286{ 286{
287 unsigned int count = TCP_socket_data_recv_buffer(sock); 287 unsigned int count = TCP_socket_data_recv_buffer(sock);
288 288
@@ -304,7 +304,7 @@ int read_TCP_packet(sock_t sock, uint8_t *data, uint16_t length)
304 * return 0 if could not read any packet. 304 * return 0 if could not read any packet.
305 * return -1 on failure (connection must be killed). 305 * return -1 on failure (connection must be killed).
306 */ 306 */
307int read_packet_TCP_secure_connection(sock_t sock, uint16_t *next_packet_length, const uint8_t *shared_key, 307int read_packet_TCP_secure_connection(Socket sock, uint16_t *next_packet_length, const uint8_t *shared_key,
308 uint8_t *recv_nonce, uint8_t *data, uint16_t max_len) 308 uint8_t *recv_nonce, uint8_t *data, uint16_t max_len)
309{ 309{
310 if (*next_packet_length == 0) { 310 if (*next_packet_length == 0) {
@@ -529,7 +529,7 @@ static int kill_accepted(TCP_Server *TCP_server, int index)
529 rm_connection_index(TCP_server, &TCP_server->accepted_connection_array[index], i); 529 rm_connection_index(TCP_server, &TCP_server->accepted_connection_array[index], i);
530 } 530 }
531 531
532 sock_t sock = TCP_server->accepted_connection_array[index].sock; 532 Socket sock = TCP_server->accepted_connection_array[index].sock;
533 533
534 if (del_accepted(TCP_server, index) != 0) { 534 if (del_accepted(TCP_server, index) != 0) {
535 return -1; 535 return -1;
@@ -971,7 +971,7 @@ static int confirm_TCP_connection(TCP_Server *TCP_server, TCP_Secure_Connection
971/* return index on success 971/* return index on success
972 * return -1 on failure 972 * return -1 on failure
973 */ 973 */
974static int accept_connection(TCP_Server *TCP_server, sock_t sock) 974static int accept_connection(TCP_Server *TCP_server, Socket sock)
975{ 975{
976 if (!sock_valid(sock)) { 976 if (!sock_valid(sock)) {
977 return -1; 977 return -1;
@@ -1003,9 +1003,9 @@ static int accept_connection(TCP_Server *TCP_server, sock_t sock)
1003 return index; 1003 return index;
1004} 1004}
1005 1005
1006static sock_t new_listening_TCP_socket(int family, uint16_t port) 1006static Socket new_listening_TCP_socket(int family, uint16_t port)
1007{ 1007{
1008 sock_t sock = socket(family, SOCK_STREAM, IPPROTO_TCP); 1008 Socket sock = socket(family, SOCK_STREAM, IPPROTO_TCP);
1009 1009
1010 if (!sock_valid(sock)) { 1010 if (!sock_valid(sock)) {
1011 return ~0; 1011 return ~0;
@@ -1048,7 +1048,7 @@ TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, const uin
1048 return NULL; 1048 return NULL;
1049 } 1049 }
1050 1050
1051 temp->socks_listening = (sock_t *)calloc(num_sockets, sizeof(sock_t)); 1051 temp->socks_listening = (Socket *)calloc(num_sockets, sizeof(Socket));
1052 1052
1053 if (temp->socks_listening == NULL) { 1053 if (temp->socks_listening == NULL) {
1054 free(temp); 1054 free(temp);
@@ -1080,7 +1080,7 @@ TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, const uin
1080#endif 1080#endif
1081 1081
1082 for (i = 0; i < num_sockets; ++i) { 1082 for (i = 0; i < num_sockets; ++i) {
1083 sock_t sock = new_listening_TCP_socket(family, ports[i]); 1083 Socket sock = new_listening_TCP_socket(family, ports[i]);
1084 1084
1085 if (sock_valid(sock)) { 1085 if (sock_valid(sock)) {
1086#ifdef TCP_SERVER_USE_EPOLL 1086#ifdef TCP_SERVER_USE_EPOLL
@@ -1124,7 +1124,7 @@ static void do_TCP_accept_new(TCP_Server *TCP_server)
1124 for (i = 0; i < TCP_server->num_listening_socks; ++i) { 1124 for (i = 0; i < TCP_server->num_listening_socks; ++i) {
1125 struct sockaddr_storage addr; 1125 struct sockaddr_storage addr;
1126 socklen_t addrlen = sizeof(addr); 1126 socklen_t addrlen = sizeof(addr);
1127 sock_t sock; 1127 Socket sock;
1128 1128
1129 do { 1129 do {
1130 sock = accept(TCP_server->socks_listening[i], (struct sockaddr *)&addr, &addrlen); 1130 sock = accept(TCP_server->socks_listening[i], (struct sockaddr *)&addr, &addrlen);
@@ -1292,7 +1292,7 @@ static void do_TCP_epoll(TCP_Server *TCP_server)
1292 int n; 1292 int n;
1293 1293
1294 for (n = 0; n < nfds; ++n) { 1294 for (n = 0; n < nfds; ++n) {
1295 sock_t sock = events[n].data.u64 & 0xFFFFFFFF; 1295 Socket sock = events[n].data.u64 & 0xFFFFFFFF;
1296 int status = (events[n].data.u64 >> 32) & 0xFF, index = (events[n].data.u64 >> 40); 1296 int status = (events[n].data.u64 >> 32) & 0xFF, index = (events[n].data.u64 >> 40);
1297 1297
1298 if ((events[n].events & EPOLLERR) || (events[n].events & EPOLLHUP) || (events[n].events & EPOLLRDHUP)) { 1298 if ((events[n].events & EPOLLERR) || (events[n].events & EPOLLHUP) || (events[n].events & EPOLLRDHUP)) {
@@ -1333,7 +1333,7 @@ static void do_TCP_epoll(TCP_Server *TCP_server)
1333 socklen_t addrlen = sizeof(addr); 1333 socklen_t addrlen = sizeof(addr);
1334 1334
1335 while (1) { 1335 while (1) {
1336 sock_t sock_new = accept(sock, (struct sockaddr *)&addr, &addrlen); 1336 Socket sock_new = accept(sock, (struct sockaddr *)&addr, &addrlen);
1337 1337
1338 if (!sock_valid(sock_new)) { 1338 if (!sock_valid(sock_new)) {
1339 break; 1339 break;
diff --git a/toxcore/TCP_server.h b/toxcore/TCP_server.h
index b1c42957..7c1db65e 100644
--- a/toxcore/TCP_server.h
+++ b/toxcore/TCP_server.h
@@ -90,7 +90,7 @@ struct TCP_Priority_List {
90}; 90};
91 91
92typedef struct TCP_Secure_Connection { 92typedef struct TCP_Secure_Connection {
93 sock_t sock; 93 Socket sock;
94 uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; 94 uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE];
95 uint8_t recv_nonce[CRYPTO_NONCE_SIZE]; /* Nonce of received packets. */ 95 uint8_t recv_nonce[CRYPTO_NONCE_SIZE]; /* Nonce of received packets. */
96 uint8_t sent_nonce[CRYPTO_NONCE_SIZE]; /* Nonce of sent packets. */ 96 uint8_t sent_nonce[CRYPTO_NONCE_SIZE]; /* Nonce of sent packets. */
@@ -137,7 +137,7 @@ void kill_TCP_server(TCP_Server *TCP_server);
137/* return the amount of data in the tcp recv buffer. 137/* return the amount of data in the tcp recv buffer.
138 * return 0 on failure. 138 * return 0 on failure.
139 */ 139 */
140unsigned int TCP_socket_data_recv_buffer(sock_t sock); 140unsigned int TCP_socket_data_recv_buffer(Socket sock);
141 141
142/* Read the next two bytes in TCP stream then convert them to 142/* Read the next two bytes in TCP stream then convert them to
143 * length (host byte order). 143 * length (host byte order).
@@ -146,20 +146,20 @@ unsigned int TCP_socket_data_recv_buffer(sock_t sock);
146 * return 0 if nothing has been read from socket. 146 * return 0 if nothing has been read from socket.
147 * return ~0 on failure. 147 * return ~0 on failure.
148 */ 148 */
149uint16_t read_TCP_length(sock_t sock); 149uint16_t read_TCP_length(Socket sock);
150 150
151/* Read length bytes from socket. 151/* Read length bytes from socket.
152 * 152 *
153 * return length on success 153 * return length on success
154 * return -1 on failure/no data in buffer. 154 * return -1 on failure/no data in buffer.
155 */ 155 */
156int read_TCP_packet(sock_t sock, uint8_t *data, uint16_t length); 156int read_TCP_packet(Socket sock, uint8_t *data, uint16_t length);
157 157
158/* return length of received packet on success. 158/* return length of received packet on success.
159 * return 0 if could not read any packet. 159 * return 0 if could not read any packet.
160 * return -1 on failure (connection must be killed). 160 * return -1 on failure (connection must be killed).
161 */ 161 */
162int read_packet_TCP_secure_connection(sock_t sock, uint16_t *next_packet_length, const uint8_t *shared_key, 162int read_packet_TCP_secure_connection(Socket sock, uint16_t *next_packet_length, const uint8_t *shared_key,
163 uint8_t *recv_nonce, uint8_t *data, uint16_t max_len); 163 uint8_t *recv_nonce, uint8_t *data, uint16_t max_len);
164 164
165 165
diff --git a/toxcore/network.c b/toxcore/network.c
index d267a216..6df13fb8 100644
--- a/toxcore/network.c
+++ b/toxcore/network.c
@@ -37,16 +37,38 @@
37#include "logger.h" 37#include "logger.h"
38#include "util.h" 38#include "util.h"
39 39
40#if !defined(_WIN32) && !defined(__WIN32__) && !defined (WIN32) 40#include <assert.h>
41#include <errno.h>
42#endif
43
44#ifdef __APPLE__ 41#ifdef __APPLE__
45#include <mach/clock.h> 42#include <mach/clock.h>
46#include <mach/mach.h> 43#include <mach/mach.h>
47#endif 44#endif
48 45
49#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) 46#ifndef IPV6_ADD_MEMBERSHIP
47#ifdef IPV6_JOIN_GROUP
48#define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
49#define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
50#endif
51#endif
52
53#if !(defined(_WIN32) || defined(__WIN32__) || defined(WIN32))
54
55#include <arpa/inet.h>
56#include <errno.h>
57#include <fcntl.h>
58#include <netinet/in.h>
59#include <sys/time.h>
60#include <sys/types.h>
61#include <sys/socket.h>
62
63#else
64
65#ifndef IPV6_V6ONLY
66#define IPV6_V6ONLY 27
67#endif
68
69#ifndef EWOULDBLOCK
70#define EWOULDBLOCK WSAEWOULDBLOCK
71#endif
50 72
51static const char *inet_ntop(sa_family_t family, const void *addr, char *buf, size_t bufsize) 73static const char *inet_ntop(sa_family_t family, const void *addr, char *buf, size_t bufsize)
52{ 74{
@@ -123,7 +145,7 @@ static int inet_pton(sa_family_t family, const char *addrString, void *addrbuf)
123 * return 1 if valid 145 * return 1 if valid
124 * return 0 if not valid 146 * return 0 if not valid
125 */ 147 */
126int sock_valid(sock_t sock) 148int sock_valid(Socket sock)
127{ 149{
128#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) 150#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
129 151
@@ -140,7 +162,7 @@ int sock_valid(sock_t sock)
140 162
141/* Close the socket. 163/* Close the socket.
142 */ 164 */
143void kill_sock(sock_t sock) 165void kill_sock(Socket sock)
144{ 166{
145#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) 167#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
146 closesocket(sock); 168 closesocket(sock);
@@ -154,7 +176,7 @@ void kill_sock(sock_t sock)
154 * return 1 on success 176 * return 1 on success
155 * return 0 on failure 177 * return 0 on failure
156 */ 178 */
157int set_socket_nonblock(sock_t sock) 179int set_socket_nonblock(Socket sock)
158{ 180{
159#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) 181#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
160 u_long mode = 1; 182 u_long mode = 1;
@@ -169,7 +191,7 @@ int set_socket_nonblock(sock_t sock)
169 * return 1 on success 191 * return 1 on success
170 * return 0 on failure 192 * return 0 on failure
171 */ 193 */
172int set_socket_nosigpipe(sock_t sock) 194int set_socket_nosigpipe(Socket sock)
173{ 195{
174#if defined(__MACH__) 196#if defined(__MACH__)
175 int set = 1; 197 int set = 1;
@@ -184,7 +206,7 @@ int set_socket_nosigpipe(sock_t sock)
184 * return 1 on success 206 * return 1 on success
185 * return 0 on failure 207 * return 0 on failure
186 */ 208 */
187int set_socket_reuseaddr(sock_t sock) 209int set_socket_reuseaddr(Socket sock)
188{ 210{
189 int set = 1; 211 int set = 1;
190 return (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char *)&set, sizeof(set)) == 0); 212 return (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char *)&set, sizeof(set)) == 0);
@@ -195,7 +217,7 @@ int set_socket_reuseaddr(sock_t sock)
195 * return 1 on success 217 * return 1 on success
196 * return 0 on failure 218 * return 0 on failure
197 */ 219 */
198int set_socket_dualstack(sock_t sock) 220int set_socket_dualstack(Socket sock)
199{ 221{
200 int ipv6only = 0; 222 int ipv6only = 0;
201 socklen_t optsize = sizeof(ipv6only); 223 socklen_t optsize = sizeof(ipv6only);
@@ -310,6 +332,28 @@ static void loglogdata(Logger *log, const char *message, const uint8_t *buffer,
310 } 332 }
311} 333}
312 334
335void get_ip4(IP4 *result, const struct in_addr *addr)
336{
337 result->uint32 = addr->s_addr;
338}
339
340void get_ip6(IP6 *result, const struct in6_addr *addr)
341{
342 assert(sizeof(result->uint8) == sizeof(addr->s6_addr));
343 memcpy(result->uint8, addr->s6_addr, sizeof(result->uint8));
344}
345
346
347void fill_addr4(IP4 ip, struct in_addr *addr)
348{
349 addr->s_addr = ip.uint32;
350}
351
352void fill_addr6(IP6 ip, struct in6_addr *addr)
353{
354 assert(sizeof(ip.uint8) == sizeof(addr->s6_addr));
355 memcpy(addr->s6_addr, ip.uint8, sizeof(ip.uint8));
356}
313 357
314/* Basic network functions: 358/* Basic network functions:
315 * Function to send packet(data) of length length to ip_port. 359 * Function to send packet(data) of length length to ip_port.
@@ -346,7 +390,7 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint1
346 ip6.uint32[1] = 0; 390 ip6.uint32[1] = 0;
347 ip6.uint32[2] = htonl(0xFFFF); 391 ip6.uint32[2] = htonl(0xFFFF);
348 ip6.uint32[3] = ip_port.ip.ip4.uint32; 392 ip6.uint32[3] = ip_port.ip.ip4.uint32;
349 addr6->sin6_addr = ip6.in6_addr; 393 fill_addr6(ip6, &addr6->sin6_addr);
350 394
351 addr6->sin6_flowinfo = 0; 395 addr6->sin6_flowinfo = 0;
352 addr6->sin6_scope_id = 0; 396 addr6->sin6_scope_id = 0;
@@ -355,7 +399,7 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint1
355 399
356 addrsize = sizeof(struct sockaddr_in); 400 addrsize = sizeof(struct sockaddr_in);
357 addr4->sin_family = AF_INET; 401 addr4->sin_family = AF_INET;
358 addr4->sin_addr = ip_port.ip.ip4.in_addr; 402 fill_addr4(ip_port.ip.ip4, &addr4->sin_addr);
359 addr4->sin_port = ip_port.port; 403 addr4->sin_port = ip_port.port;
360 } 404 }
361 } else if (ip_port.ip.family == AF_INET6) { 405 } else if (ip_port.ip.family == AF_INET6) {
@@ -364,7 +408,7 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint1
364 addrsize = sizeof(struct sockaddr_in6); 408 addrsize = sizeof(struct sockaddr_in6);
365 addr6->sin6_family = AF_INET6; 409 addr6->sin6_family = AF_INET6;
366 addr6->sin6_port = ip_port.port; 410 addr6->sin6_port = ip_port.port;
367 addr6->sin6_addr = ip_port.ip.ip6.in6_addr; 411 fill_addr6(ip_port.ip.ip6, &addr6->sin6_addr);
368 412
369 addr6->sin6_flowinfo = 0; 413 addr6->sin6_flowinfo = 0;
370 addr6->sin6_scope_id = 0; 414 addr6->sin6_scope_id = 0;
@@ -385,7 +429,7 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint1
385 * Packet data is put into data. 429 * Packet data is put into data.
386 * Packet length is put into length. 430 * Packet length is put into length.
387 */ 431 */
388static int receivepacket(Logger *log, sock_t sock, IP_Port *ip_port, uint8_t *data, uint32_t *length) 432static int receivepacket(Logger *log, Socket sock, IP_Port *ip_port, uint8_t *data, uint32_t *length)
389{ 433{
390 memset(ip_port, 0, sizeof(IP_Port)); 434 memset(ip_port, 0, sizeof(IP_Port));
391 struct sockaddr_storage addr; 435 struct sockaddr_storage addr;
@@ -412,12 +456,12 @@ static int receivepacket(Logger *log, sock_t sock, IP_Port *ip_port, uint8_t *da
412 struct sockaddr_in *addr_in = (struct sockaddr_in *)&addr; 456 struct sockaddr_in *addr_in = (struct sockaddr_in *)&addr;
413 457
414 ip_port->ip.family = addr_in->sin_family; 458 ip_port->ip.family = addr_in->sin_family;
415 ip_port->ip.ip4.in_addr = addr_in->sin_addr; 459 get_ip4(&ip_port->ip.ip4, &addr_in->sin_addr);
416 ip_port->port = addr_in->sin_port; 460 ip_port->port = addr_in->sin_port;
417 } else if (addr.ss_family == AF_INET6) { 461 } else if (addr.ss_family == AF_INET6) {
418 struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)&addr; 462 struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)&addr;
419 ip_port->ip.family = addr_in6->sin6_family; 463 ip_port->ip.family = addr_in6->sin6_family;
420 ip_port->ip.ip6.in6_addr = addr_in6->sin6_addr; 464 get_ip6(&ip_port->ip.ip6, &addr_in6->sin6_addr);
421 ip_port->port = addr_in6->sin6_port; 465 ip_port->port = addr_in6->sin6_port;
422 466
423 if (IPV6_IPV4_IN_V6(ip_port->ip.ip6)) { 467 if (IPV6_IPV4_IN_V6(ip_port->ip.ip6)) {
@@ -634,7 +678,7 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1
634 addrsize = sizeof(struct sockaddr_in); 678 addrsize = sizeof(struct sockaddr_in);
635 addr4->sin_family = AF_INET; 679 addr4->sin_family = AF_INET;
636 addr4->sin_port = 0; 680 addr4->sin_port = 0;
637 addr4->sin_addr = ip.ip4.in_addr; 681 fill_addr4(ip.ip4, &addr4->sin_addr);
638 682
639 portptr = &addr4->sin_port; 683 portptr = &addr4->sin_port;
640 } else if (temp->family == AF_INET6) { 684 } else if (temp->family == AF_INET6) {
@@ -643,7 +687,7 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1
643 addrsize = sizeof(struct sockaddr_in6); 687 addrsize = sizeof(struct sockaddr_in6);
644 addr6->sin6_family = AF_INET6; 688 addr6->sin6_family = AF_INET6;
645 addr6->sin6_port = 0; 689 addr6->sin6_port = 0;
646 addr6->sin6_addr = ip.ip6.in6_addr; 690 fill_addr6(ip.ip6, &addr6->sin6_addr);
647 691
648 addr6->sin6_flowinfo = 0; 692 addr6->sin6_flowinfo = 0;
649 addr6->sin6_scope_id = 0; 693 addr6->sin6_scope_id = 0;
@@ -767,11 +811,16 @@ int ip_equal(const IP *a, const IP *b)
767 /* same family */ 811 /* same family */
768 if (a->family == b->family) { 812 if (a->family == b->family) {
769 if (a->family == AF_INET) { 813 if (a->family == AF_INET) {
770 return (a->ip4.in_addr.s_addr == b->ip4.in_addr.s_addr); 814 struct in_addr addr_a;
815 struct in_addr addr_b;
816 fill_addr4(a->ip4, &addr_a);
817 fill_addr4(b->ip4, &addr_b);
818 return addr_a.s_addr == addr_b.s_addr;
771 } 819 }
772 820
773 if (a->family == AF_INET6) { 821 if (a->family == AF_INET6) {
774 return a->ip6.uint64[0] == b->ip6.uint64[0] && a->ip6.uint64[1] == b->ip6.uint64[1]; 822 return a->ip6.uint64[0] == b->ip6.uint64[0] &&
823 a->ip6.uint64[1] == b->ip6.uint64[1];
775 } 824 }
776 825
777 return 0; 826 return 0;
@@ -780,11 +829,15 @@ int ip_equal(const IP *a, const IP *b)
780 /* different family: check on the IPv6 one if it is the IPv4 one embedded */ 829 /* different family: check on the IPv6 one if it is the IPv4 one embedded */
781 if ((a->family == AF_INET) && (b->family == AF_INET6)) { 830 if ((a->family == AF_INET) && (b->family == AF_INET6)) {
782 if (IPV6_IPV4_IN_V6(b->ip6)) { 831 if (IPV6_IPV4_IN_V6(b->ip6)) {
783 return (a->ip4.in_addr.s_addr == b->ip6.uint32[3]); 832 struct in_addr addr_a;
833 fill_addr4(a->ip4, &addr_a);
834 return addr_a.s_addr == b->ip6.uint32[3];
784 } 835 }
785 } else if ((a->family == AF_INET6) && (b->family == AF_INET)) { 836 } else if ((a->family == AF_INET6) && (b->family == AF_INET)) {
786 if (IPV6_IPV4_IN_V6(a->ip6)) { 837 if (IPV6_IPV4_IN_V6(a->ip6)) {
787 return (a->ip6.uint32[3] == b->ip4.in_addr.s_addr); 838 struct in_addr addr_b;
839 fill_addr4(b->ip4, &addr_b);
840 return a->ip6.uint32[3] == addr_b.s_addr;
788 } 841 }
789 } 842 }
790 843
@@ -976,7 +1029,7 @@ int addr_parse_ip(const char *address, IP *to)
976 1029
977 if (1 == inet_pton(AF_INET, address, &addr4)) { 1030 if (1 == inet_pton(AF_INET, address, &addr4)) {
978 to->family = AF_INET; 1031 to->family = AF_INET;
979 to->ip4.in_addr = addr4; 1032 get_ip4(&to->ip4, &addr4);
980 return 1; 1033 return 1;
981 } 1034 }
982 1035
@@ -984,7 +1037,7 @@ int addr_parse_ip(const char *address, IP *to)
984 1037
985 if (1 == inet_pton(AF_INET6, address, &addr6)) { 1038 if (1 == inet_pton(AF_INET6, address, &addr6)) {
986 to->family = AF_INET6; 1039 to->family = AF_INET6;
987 to->ip6.in6_addr = addr6; 1040 get_ip6(&to->ip6, &addr6);
988 return 1; 1041 return 1;
989 } 1042 }
990 1043
@@ -1048,12 +1101,12 @@ int addr_resolve(const char *address, IP *to, IP *extra)
1048 case AF_INET: 1101 case AF_INET:
1049 if (walker->ai_family == family) { /* AF_INET requested, done */ 1102 if (walker->ai_family == family) { /* AF_INET requested, done */
1050 struct sockaddr_in *addr = (struct sockaddr_in *)walker->ai_addr; 1103 struct sockaddr_in *addr = (struct sockaddr_in *)walker->ai_addr;
1051 to->ip4.in_addr = addr->sin_addr; 1104 get_ip4(&to->ip4, &addr->sin_addr);
1052 result = TOX_ADDR_RESOLVE_INET; 1105 result = TOX_ADDR_RESOLVE_INET;
1053 done = 1; 1106 done = 1;
1054 } else if (!(result & TOX_ADDR_RESOLVE_INET)) { /* AF_UNSPEC requested, store away */ 1107 } else if (!(result & TOX_ADDR_RESOLVE_INET)) { /* AF_UNSPEC requested, store away */
1055 struct sockaddr_in *addr = (struct sockaddr_in *)walker->ai_addr; 1108 struct sockaddr_in *addr = (struct sockaddr_in *)walker->ai_addr;
1056 ip4.ip4.in_addr = addr->sin_addr; 1109 get_ip4(&ip4.ip4, &addr->sin_addr);
1057 result |= TOX_ADDR_RESOLVE_INET; 1110 result |= TOX_ADDR_RESOLVE_INET;
1058 } 1111 }
1059 1112
@@ -1063,14 +1116,14 @@ int addr_resolve(const char *address, IP *to, IP *extra)
1063 if (walker->ai_family == family) { /* AF_INET6 requested, done */ 1116 if (walker->ai_family == family) { /* AF_INET6 requested, done */
1064 if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) { 1117 if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) {
1065 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)walker->ai_addr; 1118 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)walker->ai_addr;
1066 to->ip6.in6_addr = addr->sin6_addr; 1119 get_ip6(&to->ip6, &addr->sin6_addr);
1067 result = TOX_ADDR_RESOLVE_INET6; 1120 result = TOX_ADDR_RESOLVE_INET6;
1068 done = 1; 1121 done = 1;
1069 } 1122 }
1070 } else if (!(result & TOX_ADDR_RESOLVE_INET6)) { /* AF_UNSPEC requested, store away */ 1123 } else if (!(result & TOX_ADDR_RESOLVE_INET6)) { /* AF_UNSPEC requested, store away */
1071 if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) { 1124 if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) {
1072 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)walker->ai_addr; 1125 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)walker->ai_addr;
1073 ip6.ip6.in6_addr = addr->sin6_addr; 1126 get_ip6(&ip6.ip6, &addr->sin6_addr);
1074 result |= TOX_ADDR_RESOLVE_INET6; 1127 result |= TOX_ADDR_RESOLVE_INET6;
1075 } 1128 }
1076 } 1129 }
diff --git a/toxcore/network.h b/toxcore/network.h
index 26126a22..a3b746c9 100644
--- a/toxcore/network.h
+++ b/toxcore/network.h
@@ -58,53 +58,21 @@
58#include <windows.h> 58#include <windows.h>
59#include <ws2tcpip.h> 59#include <ws2tcpip.h>
60 60
61#ifndef IPV6_V6ONLY
62#define IPV6_V6ONLY 27
63#endif
64
65typedef unsigned int sock_t;
66/* sa_family_t is the sockaddr_in / sockaddr_in6 family field */ 61/* sa_family_t is the sockaddr_in / sockaddr_in6 family field */
67typedef short sa_family_t; 62typedef short sa_family_t;
68 63
69#ifndef EWOULDBLOCK
70#define EWOULDBLOCK WSAEWOULDBLOCK
71#endif
72
73#else // Linux includes 64#else // Linux includes
74 65
75#include <arpa/inet.h> 66#include <arpa/inet.h>
76#include <errno.h>
77#include <fcntl.h>
78#include <netdb.h> 67#include <netdb.h>
79#include <netinet/in.h>
80#include <sys/socket.h>
81#include <sys/time.h>
82#include <sys/types.h>
83#include <unistd.h> 68#include <unistd.h>
84 69
85typedef int sock_t;
86
87#endif
88
89#if defined(__AIX__)
90# define _XOPEN_SOURCE 1
91#endif 70#endif
92 71
93#if defined(__sun__) 72struct in_addr;
94#define __EXTENSIONS__ 1 // SunOS! 73struct in6_addr;
95#if defined(__SunOS5_6__) || defined(__SunOS5_7__) || defined(__SunOS5_8__) || defined(__SunOS5_9__) || defined(__SunOS5_10__)
96//Nothing needed
97#else
98#define __MAKECONTEXT_V2_SOURCE 1
99#endif
100#endif
101 74
102#ifndef IPV6_ADD_MEMBERSHIP 75typedef int Socket;
103#ifdef IPV6_JOIN_GROUP
104#define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
105#define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
106#endif
107#endif
108 76
109#define MAX_UDP_PACKET_SIZE 2048 77#define MAX_UDP_PACKET_SIZE 2048
110 78
@@ -154,7 +122,6 @@ typedef union {
154 uint8_t uint8[4]; 122 uint8_t uint8[4];
155 uint16_t uint16[2]; 123 uint16_t uint16[2];
156 uint32_t uint32; 124 uint32_t uint32;
157 struct in_addr in_addr;
158} 125}
159IP4; 126IP4;
160 127
@@ -163,7 +130,6 @@ typedef union {
163 uint16_t uint16[8]; 130 uint16_t uint16[8];
164 uint32_t uint32[4]; 131 uint32_t uint32[4];
165 uint64_t uint64[2]; 132 uint64_t uint64[2];
166 struct in6_addr in6_addr;
167} 133}
168IP6; 134IP6;
169 135
@@ -182,6 +148,14 @@ typedef struct {
182} 148}
183IP_Port; 149IP_Port;
184 150
151/* Convert in_addr to IP */
152void get_ip4(IP4 *ip, const struct in_addr *addr);
153void get_ip6(IP6 *ip, const struct in6_addr *addr);
154
155/* Conevrt IP to in_addr */
156void fill_addr4(IP4 ip, struct in_addr *addr);
157void fill_addr6(IP6 ip, struct in6_addr *addr);
158
185/* Does the IP6 struct a contain an IPv4 address in an IPv6 one? */ 159/* Does the IP6 struct a contain an IPv4 address in an IPv6 one? */
186#define IPV6_IPV4_IN_V6(a) ((a.uint64[0] == 0) && (a.uint32[2] == htonl (0xffff))) 160#define IPV6_IPV4_IN_V6(a) ((a.uint64[0] == 0) && (a.uint32[2] == htonl (0xffff)))
187 161
@@ -326,7 +300,7 @@ typedef struct {
326 sa_family_t family; 300 sa_family_t family;
327 uint16_t port; 301 uint16_t port;
328 /* Our UDP socket. */ 302 /* Our UDP socket. */
329 sock_t sock; 303 Socket sock;
330} Networking_Core; 304} Networking_Core;
331 305
332/* Run this before creating sockets. 306/* Run this before creating sockets.
@@ -341,39 +315,39 @@ int networking_at_startup(void);
341 * return 1 if valid 315 * return 1 if valid
342 * return 0 if not valid 316 * return 0 if not valid
343 */ 317 */
344int sock_valid(sock_t sock); 318int sock_valid(Socket sock);
345 319
346/* Close the socket. 320/* Close the socket.
347 */ 321 */
348void kill_sock(sock_t sock); 322void kill_sock(Socket sock);
349 323
350/* Set socket as nonblocking 324/* Set socket as nonblocking
351 * 325 *
352 * return 1 on success 326 * return 1 on success
353 * return 0 on failure 327 * return 0 on failure
354 */ 328 */
355int set_socket_nonblock(sock_t sock); 329int set_socket_nonblock(Socket sock);
356 330
357/* Set socket to not emit SIGPIPE 331/* Set socket to not emit SIGPIPE
358 * 332 *
359 * return 1 on success 333 * return 1 on success
360 * return 0 on failure 334 * return 0 on failure
361 */ 335 */
362int set_socket_nosigpipe(sock_t sock); 336int set_socket_nosigpipe(Socket sock);
363 337
364/* Enable SO_REUSEADDR on socket. 338/* Enable SO_REUSEADDR on socket.
365 * 339 *
366 * return 1 on success 340 * return 1 on success
367 * return 0 on failure 341 * return 0 on failure
368 */ 342 */
369int set_socket_reuseaddr(sock_t sock); 343int set_socket_reuseaddr(Socket sock);
370 344
371/* Set socket to dual (IPv4 + IPv6 socket) 345/* Set socket to dual (IPv4 + IPv6 socket)
372 * 346 *
373 * return 1 on success 347 * return 1 on success
374 * return 0 on failure 348 * return 0 on failure
375 */ 349 */
376int set_socket_dualstack(sock_t sock); 350int set_socket_dualstack(Socket sock);
377 351
378/* return current monotonic time in milliseconds (ms). */ 352/* return current monotonic time in milliseconds (ms). */
379uint64_t current_time_monotonic(void); 353uint64_t current_time_monotonic(void);
diff --git a/toxcore/tox.c b/toxcore/tox.c
index 0acd551b..adc53753 100644
--- a/toxcore/tox.c
+++ b/toxcore/tox.c
@@ -254,9 +254,11 @@ bool tox_bootstrap(Tox *tox, const char *address, uint16_t port, const uint8_t *
254 } 254 }
255 255
256 if (info->ai_family == AF_INET) { 256 if (info->ai_family == AF_INET) {
257 ip_port.ip.ip4.in_addr = ((struct sockaddr_in *)info->ai_addr)->sin_addr; 257 struct sockaddr_in *addr = (struct sockaddr_in *)info->ai_addr;
258 get_ip4(&ip_port.ip.ip4, &addr->sin_addr);
258 } else if (info->ai_family == AF_INET6) { 259 } else if (info->ai_family == AF_INET6) {
259 ip_port.ip.ip6.in6_addr = ((struct sockaddr_in6 *)info->ai_addr)->sin6_addr; 260 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)info->ai_addr;
261 get_ip6(&ip_port.ip.ip6, &addr->sin6_addr);
260 } else { 262 } else {
261 continue; 263 continue;
262 } 264 }
@@ -312,9 +314,11 @@ bool tox_add_tcp_relay(Tox *tox, const char *address, uint16_t port, const uint8
312 } 314 }
313 315
314 if (info->ai_family == AF_INET) { 316 if (info->ai_family == AF_INET) {
315 ip_port.ip.ip4.in_addr = ((struct sockaddr_in *)info->ai_addr)->sin_addr; 317 struct sockaddr_in *addr = (struct sockaddr_in *)info->ai_addr;
318 get_ip4(&ip_port.ip.ip4, &addr->sin_addr);
316 } else if (info->ai_family == AF_INET6) { 319 } else if (info->ai_family == AF_INET6) {
317 ip_port.ip.ip6.in6_addr = ((struct sockaddr_in6 *)info->ai_addr)->sin6_addr; 320 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)info->ai_addr;
321 get_ip6(&ip_port.ip.ip6, &addr->sin6_addr);
318 } else { 322 } else {
319 continue; 323 continue;
320 } 324 }