diff options
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/LAN_discovery.c | 5 | ||||
-rw-r--r-- | toxcore/TCP_client.c | 8 | ||||
-rw-r--r-- | toxcore/TCP_client.h | 2 | ||||
-rw-r--r-- | toxcore/TCP_server.c | 30 | ||||
-rw-r--r-- | toxcore/TCP_server.h | 10 | ||||
-rw-r--r-- | toxcore/network.c | 111 | ||||
-rw-r--r-- | toxcore/network.h | 62 | ||||
-rw-r--r-- | toxcore/tox.c | 12 |
8 files changed, 136 insertions, 104 deletions
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 | */ |
39 | static int connect_sock_to(sock_t sock, IP_Port ip_port, TCP_Proxy_Info *proxy_info) | 39 | static 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 | }; |
53 | typedef struct { | 53 | typedef 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 | */ |
81 | static int bind_to_port(sock_t sock, int family, uint16_t port) | 81 | static 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 | */ |
235 | unsigned int TCP_socket_data_recv_buffer(sock_t sock) | 235 | unsigned 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 | */ |
255 | uint16_t read_TCP_length(sock_t sock) | 255 | uint16_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 | */ |
285 | int read_TCP_packet(sock_t sock, uint8_t *data, uint16_t length) | 285 | int 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 | */ |
307 | int read_packet_TCP_secure_connection(sock_t sock, uint16_t *next_packet_length, const uint8_t *shared_key, | 307 | int 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 | */ |
974 | static int accept_connection(TCP_Server *TCP_server, sock_t sock) | 974 | static 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 | ||
1006 | static sock_t new_listening_TCP_socket(int family, uint16_t port) | 1006 | static 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 | ||
92 | typedef struct TCP_Secure_Connection { | 92 | typedef 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 | */ |
140 | unsigned int TCP_socket_data_recv_buffer(sock_t sock); | 140 | unsigned 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 | */ |
149 | uint16_t read_TCP_length(sock_t sock); | 149 | uint16_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 | */ |
156 | int read_TCP_packet(sock_t sock, uint8_t *data, uint16_t length); | 156 | int 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 | */ |
162 | int read_packet_TCP_secure_connection(sock_t sock, uint16_t *next_packet_length, const uint8_t *shared_key, | 162 | int 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 | ||
51 | static const char *inet_ntop(sa_family_t family, const void *addr, char *buf, size_t bufsize) | 73 | static 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 | */ |
126 | int sock_valid(sock_t sock) | 148 | int 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 | */ |
143 | void kill_sock(sock_t sock) | 165 | void 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 | */ |
157 | int set_socket_nonblock(sock_t sock) | 179 | int 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 | */ |
172 | int set_socket_nosigpipe(sock_t sock) | 194 | int 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 | */ |
187 | int set_socket_reuseaddr(sock_t sock) | 209 | int 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 | */ |
198 | int set_socket_dualstack(sock_t sock) | 220 | int 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 | ||
335 | void get_ip4(IP4 *result, const struct in_addr *addr) | ||
336 | { | ||
337 | result->uint32 = addr->s_addr; | ||
338 | } | ||
339 | |||
340 | void 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 | |||
347 | void fill_addr4(IP4 ip, struct in_addr *addr) | ||
348 | { | ||
349 | addr->s_addr = ip.uint32; | ||
350 | } | ||
351 | |||
352 | void 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 | */ |
388 | static int receivepacket(Logger *log, sock_t sock, IP_Port *ip_port, uint8_t *data, uint32_t *length) | 432 | static 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 | |||
65 | typedef 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 */ |
67 | typedef short sa_family_t; | 62 | typedef 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 | ||
85 | typedef 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__) | 72 | struct in_addr; |
94 | #define __EXTENSIONS__ 1 // SunOS! | 73 | struct 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 | 75 | typedef 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 | } |
159 | IP4; | 126 | IP4; |
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 | } |
168 | IP6; | 134 | IP6; |
169 | 135 | ||
@@ -182,6 +148,14 @@ typedef struct { | |||
182 | } | 148 | } |
183 | IP_Port; | 149 | IP_Port; |
184 | 150 | ||
151 | /* Convert in_addr to IP */ | ||
152 | void get_ip4(IP4 *ip, const struct in_addr *addr); | ||
153 | void get_ip6(IP6 *ip, const struct in6_addr *addr); | ||
154 | |||
155 | /* Conevrt IP to in_addr */ | ||
156 | void fill_addr4(IP4 ip, struct in_addr *addr); | ||
157 | void 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 | */ |
344 | int sock_valid(sock_t sock); | 318 | int sock_valid(Socket sock); |
345 | 319 | ||
346 | /* Close the socket. | 320 | /* Close the socket. |
347 | */ | 321 | */ |
348 | void kill_sock(sock_t sock); | 322 | void 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 | */ |
355 | int set_socket_nonblock(sock_t sock); | 329 | int 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 | */ |
362 | int set_socket_nosigpipe(sock_t sock); | 336 | int 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 | */ |
369 | int set_socket_reuseaddr(sock_t sock); | 343 | int 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 | */ |
376 | int set_socket_dualstack(sock_t sock); | 350 | int set_socket_dualstack(Socket sock); |
377 | 351 | ||
378 | /* return current monotonic time in milliseconds (ms). */ | 352 | /* return current monotonic time in milliseconds (ms). */ |
379 | uint64_t current_time_monotonic(void); | 353 | uint64_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 | } |