summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2018-02-27 02:07:59 +0000
committeriphydf <iphydf@users.noreply.github.com>2018-05-20 15:42:42 +0000
commitbe797d4b039b40d2c876f0a71cd1e48454a74065 (patch)
treec759c0f15a3ac0285e0bd941dc3edad06ade015d
parent291a849a5a0afb4450bcd0d0c50b7aeccaac04f5 (diff)
Move system header includes from network.h to network.c
-rw-r--r--appveyor.yml2
-rw-r--r--auto_tests/TCP_test.c24
-rw-r--r--auto_tests/monolith_test.cpp4
-rw-r--r--other/bootstrap_node_packets.c2
-rw-r--r--toxav/audio.c5
-rw-r--r--toxav/bwcontroller.c8
-rw-r--r--toxav/groupav.c3
-rw-r--r--toxav/rtp.c9
-rw-r--r--toxav/video.c7
-rw-r--r--toxcore/BUILD.bazel5
-rw-r--r--toxcore/DHT.c4
-rw-r--r--toxcore/LAN_discovery.c34
-rw-r--r--toxcore/Messenger.c8
-rw-r--r--toxcore/TCP_client.c23
-rw-r--r--toxcore/TCP_connection.c6
-rw-r--r--toxcore/TCP_server.c94
-rw-r--r--toxcore/TCP_server.h10
-rw-r--r--toxcore/crypto_core.api.h2
-rw-r--r--toxcore/crypto_core.h2
-rw-r--r--toxcore/friend_connection.c4
-rw-r--r--toxcore/friend_requests.c3
-rw-r--r--toxcore/group.c3
-rw-r--r--toxcore/list.c3
-rw-r--r--toxcore/list.h2
-rw-r--r--toxcore/net_crypto.c6
-rw-r--r--toxcore/network.c263
-rw-r--r--toxcore/network.h84
-rw-r--r--toxcore/onion.c5
-rw-r--r--toxcore/onion_announce.c3
-rw-r--r--toxcore/onion_client.c3
-rw-r--r--toxcore/ping.c5
-rw-r--r--toxcore/ping_array.c3
-rw-r--r--toxcore/tox.c2
-rw-r--r--toxcore/util.c1
-rw-r--r--toxencryptsave/toxencryptsave.c1
35 files changed, 381 insertions, 262 deletions
diff --git a/appveyor.yml b/appveyor.yml
index 91f727f7..c921d761 100644
--- a/appveyor.yml
+++ b/appveyor.yml
@@ -10,7 +10,7 @@ install:
10- cd third_party 10- cd third_party
11# libsodium 11# libsodium
12- mkdir libsodium && cd libsodium 12- mkdir libsodium && cd libsodium
13- if not exist %APPDATA%\downloads\libsodium.zip curl -L https://download.libsodium.org/libsodium/releases/libsodium-1.0.12-msvc.zip -o %APPDATA%\downloads\libsodium.zip 13- if not exist %APPDATA%\downloads\libsodium.zip curl -L https://download.libsodium.org/libsodium/releases/libsodium-1.0.16-msvc.zip -o %APPDATA%\downloads\libsodium.zip
14- unzip %APPDATA%\downloads\libsodium.zip 14- unzip %APPDATA%\downloads\libsodium.zip
15- cd .. 15- cd ..
16# pthreads-win32 16# pthreads-win32
diff --git a/auto_tests/TCP_test.c b/auto_tests/TCP_test.c
index 144d7e14..41125116 100644
--- a/auto_tests/TCP_test.c
+++ b/auto_tests/TCP_test.c
@@ -83,14 +83,14 @@ START_TEST(test_basic)
83 TCP_HANDSHAKE_PLAIN_SIZE, handshake + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE); 83 TCP_HANDSHAKE_PLAIN_SIZE, handshake + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE);
84 ck_assert_msg(ret == TCP_CLIENT_HANDSHAKE_SIZE - (CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE), 84 ck_assert_msg(ret == TCP_CLIENT_HANDSHAKE_SIZE - (CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE),
85 "Encrypt failed."); 85 "Encrypt failed.");
86 ck_assert_msg(send(sock, (const char *)handshake, TCP_CLIENT_HANDSHAKE_SIZE - 1, 0) == TCP_CLIENT_HANDSHAKE_SIZE - 1, 86 ck_assert_msg(net_send(sock, handshake, TCP_CLIENT_HANDSHAKE_SIZE - 1) == TCP_CLIENT_HANDSHAKE_SIZE - 1,
87 "send Failed."); 87 "send Failed.");
88 c_sleep(50); 88 c_sleep(50);
89 do_TCP_server(tcp_s); 89 do_TCP_server(tcp_s);
90 c_sleep(50); 90 c_sleep(50);
91 do_TCP_server(tcp_s); 91 do_TCP_server(tcp_s);
92 c_sleep(50); 92 c_sleep(50);
93 ck_assert_msg(send(sock, (const char *)(handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1)), 1, 0) == 1, "send Failed."); 93 ck_assert_msg(net_send(sock, handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1), 1) == 1, "send Failed.");
94 c_sleep(50); 94 c_sleep(50);
95 do_TCP_server(tcp_s); 95 do_TCP_server(tcp_s);
96 c_sleep(50); 96 c_sleep(50);
@@ -98,7 +98,7 @@ START_TEST(test_basic)
98 c_sleep(50); 98 c_sleep(50);
99 uint8_t response[TCP_SERVER_HANDSHAKE_SIZE]; 99 uint8_t response[TCP_SERVER_HANDSHAKE_SIZE];
100 uint8_t response_plain[TCP_HANDSHAKE_PLAIN_SIZE]; 100 uint8_t response_plain[TCP_HANDSHAKE_PLAIN_SIZE];
101 ck_assert_msg(recv(sock, (char *)response, TCP_SERVER_HANDSHAKE_SIZE, 0) == TCP_SERVER_HANDSHAKE_SIZE, "recv Failed."); 101 ck_assert_msg(net_recv(sock, response, TCP_SERVER_HANDSHAKE_SIZE) == TCP_SERVER_HANDSHAKE_SIZE, "recv Failed.");
102 ret = decrypt_data(self_public_key, f_secret_key, response, response + CRYPTO_NONCE_SIZE, 102 ret = decrypt_data(self_public_key, f_secret_key, response, response + CRYPTO_NONCE_SIZE,
103 TCP_SERVER_HANDSHAKE_SIZE - CRYPTO_NONCE_SIZE, response_plain); 103 TCP_SERVER_HANDSHAKE_SIZE - CRYPTO_NONCE_SIZE, response_plain);
104 ck_assert_msg(ret == TCP_HANDSHAKE_PLAIN_SIZE, "Decrypt Failed."); 104 ck_assert_msg(ret == TCP_HANDSHAKE_PLAIN_SIZE, "Decrypt Failed.");
@@ -118,8 +118,10 @@ START_TEST(test_basic)
118 uint32_t i; 118 uint32_t i;
119 119
120 for (i = 0; i < sizeof(r_req); ++i) { 120 for (i = 0; i < sizeof(r_req); ++i) {
121 ck_assert_msg(send(sock, (const char *)(r_req + i), 1, 0) == 1, "send Failed."); 121 ck_assert_msg(net_send(sock, r_req + i, 1) == 1, "send Failed.");
122 //ck_assert_msg(send(sock, (const char *)r_req, sizeof(r_req), 0) == sizeof(r_req), "send Failed."); 122#if 0
123 ck_assert_msg(net_send(sock, r_req, sizeof(r_req)) == sizeof(r_req), "send Failed.");
124#endif
123 do_TCP_server(tcp_s); 125 do_TCP_server(tcp_s);
124 c_sleep(50); 126 c_sleep(50);
125 } 127 }
@@ -127,7 +129,7 @@ START_TEST(test_basic)
127 do_TCP_server(tcp_s); 129 do_TCP_server(tcp_s);
128 c_sleep(50); 130 c_sleep(50);
129 uint8_t packet_resp[4096]; 131 uint8_t packet_resp[4096];
130 int recv_data_len = recv(sock, (char *)packet_resp, 2 + 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE, 0); 132 int recv_data_len = net_recv(sock, packet_resp, 2 + 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE);
131 ck_assert_msg(recv_data_len == 2 + 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE, 133 ck_assert_msg(recv_data_len == 2 + 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE,
132 "recv Failed. %u", recv_data_len); 134 "recv Failed. %u", recv_data_len);
133 memcpy(&size, packet_resp, 2); 135 memcpy(&size, packet_resp, 2);
@@ -180,16 +182,16 @@ static struct sec_TCP_con *new_TCP_con(TCP_Server *tcp_s)
180 TCP_HANDSHAKE_PLAIN_SIZE, handshake + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE); 182 TCP_HANDSHAKE_PLAIN_SIZE, handshake + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE);
181 ck_assert_msg(ret == TCP_CLIENT_HANDSHAKE_SIZE - (CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE), 183 ck_assert_msg(ret == TCP_CLIENT_HANDSHAKE_SIZE - (CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE),
182 "Encrypt failed."); 184 "Encrypt failed.");
183 ck_assert_msg(send(sock, (const char *)handshake, TCP_CLIENT_HANDSHAKE_SIZE - 1, 0) == TCP_CLIENT_HANDSHAKE_SIZE - 1, 185 ck_assert_msg(net_send(sock, handshake, TCP_CLIENT_HANDSHAKE_SIZE - 1) == TCP_CLIENT_HANDSHAKE_SIZE - 1,
184 "send Failed."); 186 "send Failed.");
185 do_TCP_server(tcp_s); 187 do_TCP_server(tcp_s);
186 c_sleep(50); 188 c_sleep(50);
187 ck_assert_msg(send(sock, (const char *)(handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1)), 1, 0) == 1, "send Failed."); 189 ck_assert_msg(net_send(sock, handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1), 1) == 1, "send Failed.");
188 c_sleep(50); 190 c_sleep(50);
189 do_TCP_server(tcp_s); 191 do_TCP_server(tcp_s);
190 uint8_t response[TCP_SERVER_HANDSHAKE_SIZE]; 192 uint8_t response[TCP_SERVER_HANDSHAKE_SIZE];
191 uint8_t response_plain[TCP_HANDSHAKE_PLAIN_SIZE]; 193 uint8_t response_plain[TCP_HANDSHAKE_PLAIN_SIZE];
192 ck_assert_msg(recv(sock, (char *)response, TCP_SERVER_HANDSHAKE_SIZE, 0) == TCP_SERVER_HANDSHAKE_SIZE, "recv Failed."); 194 ck_assert_msg(net_recv(sock, response, TCP_SERVER_HANDSHAKE_SIZE) == TCP_SERVER_HANDSHAKE_SIZE, "recv Failed.");
193 ret = decrypt_data(tcp_server_public_key(tcp_s), f_secret_key, response, response + CRYPTO_NONCE_SIZE, 195 ret = decrypt_data(tcp_server_public_key(tcp_s), f_secret_key, response, response + CRYPTO_NONCE_SIZE,
194 TCP_SERVER_HANDSHAKE_SIZE - CRYPTO_NONCE_SIZE, response_plain); 196 TCP_SERVER_HANDSHAKE_SIZE - CRYPTO_NONCE_SIZE, response_plain);
195 ck_assert_msg(ret == TCP_HANDSHAKE_PLAIN_SIZE, "Decrypt Failed."); 197 ck_assert_msg(ret == TCP_HANDSHAKE_PLAIN_SIZE, "Decrypt Failed.");
@@ -219,13 +221,13 @@ static int write_packet_TCP_secure_connection(struct sec_TCP_con *con, uint8_t *
219 221
220 increment_nonce(con->sent_nonce); 222 increment_nonce(con->sent_nonce);
221 223
222 ck_assert_msg(send(con->sock, (const char *)packet, SIZEOF_VLA(packet), 0) == SIZEOF_VLA(packet), "send failed"); 224 ck_assert_msg(net_send(con->sock, packet, SIZEOF_VLA(packet)) == SIZEOF_VLA(packet), "send failed");
223 return 0; 225 return 0;
224} 226}
225 227
226static int read_packet_sec_TCP(struct sec_TCP_con *con, uint8_t *data, uint16_t length) 228static int read_packet_sec_TCP(struct sec_TCP_con *con, uint8_t *data, uint16_t length)
227{ 229{
228 int len = recv(con->sock, (char *)data, length, 0); 230 int len = net_recv(con->sock, data, length);
229 ck_assert_msg(len == length, "wrong len %i\n", len); 231 ck_assert_msg(len == length, "wrong len %i\n", len);
230 len = decrypt_data_symmetric(con->shared_key, con->recv_nonce, data + 2, length - 2, data); 232 len = decrypt_data_symmetric(con->shared_key, con->recv_nonce, data + 2, length - 2, data);
231 ck_assert_msg(len != -1, "Decrypt failed"); 233 ck_assert_msg(len != -1, "Decrypt failed");
diff --git a/auto_tests/monolith_test.cpp b/auto_tests/monolith_test.cpp
index 50c59995..853c88ec 100644
--- a/auto_tests/monolith_test.cpp
+++ b/auto_tests/monolith_test.cpp
@@ -286,7 +286,11 @@ int main(int argc, char *argv[])
286 // toxcore/TCP_server 286 // toxcore/TCP_server
287 CHECK_SIZE(TCP_Priority_List, 16); 287 CHECK_SIZE(TCP_Priority_List, 16);
288 CHECK_SIZE(TCP_Secure_Connection, 11816); 288 CHECK_SIZE(TCP_Secure_Connection, 11816);
289#ifdef TCP_SERVER_USE_EPOLL
290 CHECK_SIZE(TCP_Server, 6049968); // 6MB!
291#else
289 CHECK_SIZE(TCP_Server, 6049952); // 6MB! 292 CHECK_SIZE(TCP_Server, 6049952); // 6MB!
293#endif
290 // toxcore/tox 294 // toxcore/tox
291 CHECK_SIZE(Tox_Options, 64); 295 CHECK_SIZE(Tox_Options, 64);
292#endif 296#endif
diff --git a/other/bootstrap_node_packets.c b/other/bootstrap_node_packets.c
index 614cb720..5b447c62 100644
--- a/other/bootstrap_node_packets.c
+++ b/other/bootstrap_node_packets.c
@@ -25,6 +25,8 @@
25 */ 25 */
26#include "bootstrap_node_packets.h" 26#include "bootstrap_node_packets.h"
27 27
28#include <string.h>
29
28#define INFO_REQUEST_PACKET_LENGTH 78 30#define INFO_REQUEST_PACKET_LENGTH 78
29 31
30static uint32_t bootstrap_version; 32static uint32_t bootstrap_version;
diff --git a/toxav/audio.c b/toxav/audio.c
index 58a93add..85639f16 100644
--- a/toxav/audio.c
+++ b/toxav/audio.c
@@ -23,12 +23,13 @@
23 23
24#include "audio.h" 24#include "audio.h"
25 25
26#include <stdlib.h>
27#include <string.h>
28
26#include "rtp.h" 29#include "rtp.h"
27 30
28#include "../toxcore/logger.h" 31#include "../toxcore/logger.h"
29 32
30#include <stdlib.h>
31
32static struct JitterBuffer *jbuf_new(uint32_t capacity); 33static struct JitterBuffer *jbuf_new(uint32_t capacity);
33static void jbuf_clear(struct JitterBuffer *q); 34static void jbuf_clear(struct JitterBuffer *q);
34static void jbuf_free(struct JitterBuffer *q); 35static void jbuf_free(struct JitterBuffer *q);
diff --git a/toxav/bwcontroller.c b/toxav/bwcontroller.c
index d786080e..2d7dd659 100644
--- a/toxav/bwcontroller.c
+++ b/toxav/bwcontroller.c
@@ -23,14 +23,16 @@
23 23
24#include "bwcontroller.h" 24#include "bwcontroller.h"
25 25
26#include <assert.h>
27#include <errno.h>
28#include <stdlib.h>
29#include <string.h>
30
26#include "ring_buffer.h" 31#include "ring_buffer.h"
27 32
28#include "../toxcore/logger.h" 33#include "../toxcore/logger.h"
29#include "../toxcore/util.h" 34#include "../toxcore/util.h"
30 35
31#include <assert.h>
32#include <errno.h>
33
34#define BWC_PACKET_ID 196 36#define BWC_PACKET_ID 196
35#define BWC_SEND_INTERVAL_MS 950 /* 0.95s */ 37#define BWC_SEND_INTERVAL_MS 950 /* 0.95s */
36#define BWC_AVG_PKT_COUNT 20 38#define BWC_AVG_PKT_COUNT 20
diff --git a/toxav/groupav.c b/toxav/groupav.c
index e9595b4e..f0cb3a5b 100644
--- a/toxav/groupav.c
+++ b/toxav/groupav.c
@@ -23,6 +23,9 @@
23 23
24#include "groupav.h" 24#include "groupav.h"
25 25
26#include <stdlib.h>
27#include <string.h>
28
26#include "../toxcore/logger.h" 29#include "../toxcore/logger.h"
27#include "../toxcore/util.h" 30#include "../toxcore/util.h"
28 31
diff --git a/toxav/rtp.c b/toxav/rtp.c
index a65dbafe..d40ad9d3 100644
--- a/toxav/rtp.c
+++ b/toxav/rtp.c
@@ -23,16 +23,17 @@
23 23
24#include "rtp.h" 24#include "rtp.h"
25 25
26#include <assert.h>
27#include <errno.h>
28#include <stdlib.h>
29#include <string.h>
30
26#include "bwcontroller.h" 31#include "bwcontroller.h"
27 32
28#include "../toxcore/Messenger.h" 33#include "../toxcore/Messenger.h"
29#include "../toxcore/logger.h" 34#include "../toxcore/logger.h"
30#include "../toxcore/util.h" 35#include "../toxcore/util.h"
31 36
32#include <assert.h>
33#include <errno.h>
34#include <stdlib.h>
35
36enum { 37enum {
37 /** 38 /**
38 * The number of milliseconds we want to keep a keyframe in the buffer for, 39 * The number of milliseconds we want to keep a keyframe in the buffer for,
diff --git a/toxav/video.c b/toxav/video.c
index 1d01d08a..8c31cf75 100644
--- a/toxav/video.c
+++ b/toxav/video.c
@@ -23,6 +23,10 @@
23 23
24#include "video.h" 24#include "video.h"
25 25
26#include <assert.h>
27#include <stdlib.h>
28#include <string.h>
29
26#include "msi.h" 30#include "msi.h"
27#include "ring_buffer.h" 31#include "ring_buffer.h"
28#include "rtp.h" 32#include "rtp.h"
@@ -30,9 +34,6 @@
30#include "../toxcore/logger.h" 34#include "../toxcore/logger.h"
31#include "../toxcore/network.h" 35#include "../toxcore/network.h"
32 36
33#include <assert.h>
34#include <stdlib.h>
35
36/** 37/**
37 * Soft deadline the decoder should attempt to meet, in "us" (microseconds). 38 * Soft deadline the decoder should attempt to meet, in "us" (microseconds).
38 * Set to zero for unlimited. 39 * Set to zero for unlimited.
diff --git a/toxcore/BUILD.bazel b/toxcore/BUILD.bazel
index 3dfa1648..1889a702 100644
--- a/toxcore/BUILD.bazel
+++ b/toxcore/BUILD.bazel
@@ -123,6 +123,11 @@ cc_library(
123 "TCP_connection.c", 123 "TCP_connection.c",
124 "TCP_server.c", 124 "TCP_server.c",
125 ], 125 ],
126 copts = select({
127 "//tools/config:freebsd": [],
128 "//tools/config:linux": ["-DTCP_SERVER_USE_EPOLL=1"],
129 "//tools/config:osx": [],
130 }),
126 hdrs = [ 131 hdrs = [
127 "TCP_client.h", 132 "TCP_client.h",
128 "TCP_connection.h", 133 "TCP_connection.h",
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index f611fca6..31d2ca0a 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -3,7 +3,7 @@
3 */ 3 */
4 4
5/* 5/*
6 * Copyright © 2016-2017 The TokTok team. 6 * Copyright © 2016-2018 The TokTok team.
7 * Copyright © 2013 Tox project. 7 * Copyright © 2013 Tox project.
8 * 8 *
9 * This file is part of Tox, the free peer to peer instant messenger. 9 * This file is part of Tox, the free peer to peer instant messenger.
@@ -34,6 +34,8 @@
34#include "util.h" 34#include "util.h"
35 35
36#include <assert.h> 36#include <assert.h>
37#include <stdlib.h>
38#include <string.h>
37 39
38/* The timeout after which a node is discarded completely. */ 40/* The timeout after which a node is discarded completely. */
39#define KILL_NODE_TIMEOUT (BAD_NODE_TIMEOUT + PING_INTERVAL) 41#define KILL_NODE_TIMEOUT (BAD_NODE_TIMEOUT + PING_INTERVAL)
diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c
index 70b71fea..0bb96926 100644
--- a/toxcore/LAN_discovery.c
+++ b/toxcore/LAN_discovery.c
@@ -27,6 +27,8 @@
27 27
28#include "LAN_discovery.h" 28#include "LAN_discovery.h"
29 29
30#include <string.h>
31
30#include "util.h" 32#include "util.h"
31 33
32#define MAX_INTERFACES 16 34#define MAX_INTERFACES 16
@@ -37,7 +39,15 @@
37static int broadcast_count = -1; 39static int broadcast_count = -1;
38static IP_Port broadcast_ip_ports[MAX_INTERFACES]; 40static IP_Port broadcast_ip_ports[MAX_INTERFACES];
39 41
40#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) 42#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
43
44// The mingw32/64 Windows library warns about including winsock2.h after
45// windows.h even though with the above it's a valid thing to do. So, to make
46// mingw32 headers happy, we include winsock2.h first.
47#include <winsock2.h>
48
49#include <windows.h>
50#include <ws2tcpip.h>
41 51
42#include <iphlpapi.h> 52#include <iphlpapi.h>
43 53
@@ -108,6 +118,12 @@ static void fetch_broadcast_info(uint16_t port)
108 118
109#elif defined(__linux__) || defined(__FreeBSD__) 119#elif defined(__linux__) || defined(__FreeBSD__)
110 120
121#include <netinet/in.h>
122#include <sys/ioctl.h>
123#include <sys/socket.h>
124#include <sys/types.h>
125#include <unistd.h>
126
111#ifdef __linux__ 127#ifdef __linux__
112#include <linux/netdevice.h> 128#include <linux/netdevice.h>
113#endif 129#endif
@@ -116,8 +132,6 @@ static void fetch_broadcast_info(uint16_t port)
116#include <net/if.h> 132#include <net/if.h>
117#endif 133#endif
118 134
119#include <sys/ioctl.h>
120
121static void fetch_broadcast_info(uint16_t port) 135static void fetch_broadcast_info(uint16_t port)
122{ 136{
123 /* Not sure how many platforms this will run on, 137 /* Not sure how many platforms this will run on,
@@ -127,7 +141,7 @@ static void fetch_broadcast_info(uint16_t port)
127 broadcast_count = 0; 141 broadcast_count = 0;
128 const Socket sock = net_socket(TOX_AF_INET, TOX_SOCK_STREAM, 0); 142 const Socket sock = net_socket(TOX_AF_INET, TOX_SOCK_STREAM, 0);
129 143
130 if (sock < 0) { 144 if (!sock_valid(sock)) {
131 return; 145 return;
132 } 146 }
133 147
@@ -139,8 +153,8 @@ static void fetch_broadcast_info(uint16_t port)
139 ifconf.ifc_buf = (char *)i_faces; 153 ifconf.ifc_buf = (char *)i_faces;
140 ifconf.ifc_len = sizeof(i_faces); 154 ifconf.ifc_len = sizeof(i_faces);
141 155
142 if (ioctl(sock, SIOCGIFCONF, &ifconf) < 0) { 156 if (ioctl(sock.socket, SIOCGIFCONF, &ifconf) < 0) {
143 close(sock); 157 kill_sock(sock);
144 return; 158 return;
145 } 159 }
146 160
@@ -160,12 +174,12 @@ static void fetch_broadcast_info(uint16_t port)
160 174
161 for (int i = 0; i < n; i++) { 175 for (int i = 0; i < n; i++) {
162 /* there are interfaces with are incapable of broadcast */ 176 /* there are interfaces with are incapable of broadcast */
163 if (ioctl(sock, SIOCGIFBRDADDR, &i_faces[i]) < 0) { 177 if (ioctl(sock.socket, SIOCGIFBRDADDR, &i_faces[i]) < 0) {
164 continue; 178 continue;
165 } 179 }
166 180
167 /* moot check: only TOX_AF_INET returned (backwards compat.) */ 181 /* moot check: only AF_INET returned (backwards compat.) */
168 if (i_faces[i].ifr_broadaddr.sa_family != TOX_AF_INET) { 182 if (i_faces[i].ifr_broadaddr.sa_family != AF_INET) {
169 continue; 183 continue;
170 } 184 }
171 185
@@ -187,7 +201,7 @@ static void fetch_broadcast_info(uint16_t port)
187 count++; 201 count++;
188 } 202 }
189 203
190 close(sock); 204 kill_sock(sock);
191 205
192 broadcast_count = count; 206 broadcast_count = count;
193 207
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 2408c844..c381db0c 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -27,12 +27,16 @@
27 27
28#include "Messenger.h" 28#include "Messenger.h"
29 29
30#include <assert.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <time.h>
35
30#include "logger.h" 36#include "logger.h"
31#include "network.h" 37#include "network.h"
32#include "util.h" 38#include "util.h"
33 39
34#include <assert.h>
35
36static int write_cryptpacket_id(const Messenger *m, int32_t friendnumber, uint8_t packet_id, const uint8_t *data, 40static int write_cryptpacket_id(const Messenger *m, int32_t friendnumber, uint8_t packet_id, const uint8_t *data,
37 uint32_t length, uint8_t congestion_control); 41 uint32_t length, uint8_t congestion_control);
38 42
diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c
index a6c32f9c..58e8de49 100644
--- a/toxcore/TCP_client.c
+++ b/toxcore/TCP_client.c
@@ -27,11 +27,11 @@
27 27
28#include "TCP_client.h" 28#include "TCP_client.h"
29 29
30#include "util.h" 30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
31 33
32#if !defined(_WIN32) && !defined(__WIN32__) && !defined (WIN32) 34#include "util.h"
33#include <sys/ioctl.h>
34#endif
35 35
36struct TCP_Client_Connection { 36struct TCP_Client_Connection {
37 TCP_CLIENT_STATUS status; 37 TCP_CLIENT_STATUS status;
@@ -177,7 +177,7 @@ static int proxy_http_read_connection_response(TCP_Client_Connection *TCP_conn)
177 177
178 if (strstr((char *)data, success)) { 178 if (strstr((char *)data, success)) {
179 // drain all data 179 // drain all data
180 unsigned int data_left = TCP_socket_data_recv_buffer(TCP_conn->sock); 180 unsigned int data_left = net_socket_data_recv_buffer(TCP_conn->sock);
181 181
182 if (data_left) { 182 if (data_left) {
183 VLA(uint8_t, temp_data, data_left); 183 VLA(uint8_t, temp_data, data_left);
@@ -332,9 +332,8 @@ static int client_send_pending_data_nonpriority(TCP_Client_Connection *con)
332 return 0; 332 return 0;
333 } 333 }
334 334
335 uint16_t left = con->last_packet_length - con->last_packet_sent; 335 const uint16_t left = con->last_packet_length - con->last_packet_sent;
336 const char *data = (const char *)(con->last_packet + con->last_packet_sent); 336 const int len = net_send(con->sock, con->last_packet + con->last_packet_sent, left);
337 int len = send(con->sock, data, left, MSG_NOSIGNAL);
338 337
339 if (len <= 0) { 338 if (len <= 0) {
340 return -1; 339 return -1;
@@ -363,8 +362,8 @@ static int client_send_pending_data(TCP_Client_Connection *con)
363 TCP_Priority_List *p = con->priority_queue_start; 362 TCP_Priority_List *p = con->priority_queue_start;
364 363
365 while (p) { 364 while (p) {
366 uint16_t left = p->size - p->sent; 365 const uint16_t left = p->size - p->sent;
367 int len = send(con->sock, (const char *)(p->data + p->sent), left, MSG_NOSIGNAL); 366 const int len = net_send(con->sock, p->data + p->sent, left);
368 367
369 if (len != left) { 368 if (len != left) {
370 if (len > 0) { 369 if (len > 0) {
@@ -459,7 +458,7 @@ static int write_packet_TCP_client_secure_connection(TCP_Client_Connection *con,
459 } 458 }
460 459
461 if (priority) { 460 if (priority) {
462 len = sendpriority ? send(con->sock, (const char *)packet, SIZEOF_VLA(packet), MSG_NOSIGNAL) : 0; 461 len = sendpriority ? net_send(con->sock, packet, SIZEOF_VLA(packet)) : 0;
463 462
464 if (len <= 0) { 463 if (len <= 0) {
465 len = 0; 464 len = 0;
@@ -474,7 +473,7 @@ static int write_packet_TCP_client_secure_connection(TCP_Client_Connection *con,
474 return client_add_priority(con, packet, SIZEOF_VLA(packet), len); 473 return client_add_priority(con, packet, SIZEOF_VLA(packet), len);
475 } 474 }
476 475
477 len = send(con->sock, (const char *)packet, SIZEOF_VLA(packet), MSG_NOSIGNAL); 476 len = net_send(con->sock, packet, SIZEOF_VLA(packet));
478 477
479 if (len <= 0) { 478 if (len <= 0) {
480 return 0; 479 return 0;
diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c
index d10114f6..72642264 100644
--- a/toxcore/TCP_connection.c
+++ b/toxcore/TCP_connection.c
@@ -27,9 +27,11 @@
27 27
28#include "TCP_connection.h" 28#include "TCP_connection.h"
29 29
30#include "util.h"
31
32#include <assert.h> 30#include <assert.h>
31#include <stdlib.h>
32#include <string.h>
33
34#include "util.h"
33 35
34 36
35struct TCP_Connections { 37struct TCP_Connections {
diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c
index 4541fce0..28a441ad 100644
--- a/toxcore/TCP_server.c
+++ b/toxcore/TCP_server.c
@@ -27,12 +27,20 @@
27 27
28#include "TCP_server.h" 28#include "TCP_server.h"
29 29
30#include "util.h" 30#include <stdio.h>
31 31#include <stdlib.h>
32#include <string.h>
32#if !defined(_WIN32) && !defined(__WIN32__) && !defined (WIN32) 33#if !defined(_WIN32) && !defined(__WIN32__) && !defined (WIN32)
33#include <sys/ioctl.h> 34#include <sys/ioctl.h>
34#endif 35#endif
35 36
37#ifdef TCP_SERVER_USE_EPOLL
38#include <sys/epoll.h>
39#include <unistd.h>
40#endif
41
42#include "util.h"
43
36typedef struct TCP_Secure_Connection { 44typedef struct TCP_Secure_Connection {
37 Socket sock; 45 Socket sock;
38 uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; 46 uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE];
@@ -231,22 +239,6 @@ static int del_accepted(TCP_Server *TCP_server, int index)
231 return 0; 239 return 0;
232} 240}
233 241
234/* return the amount of data in the tcp recv buffer.
235 * return 0 on failure.
236 */
237unsigned int TCP_socket_data_recv_buffer(Socket sock)
238{
239#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
240 unsigned long count = 0;
241 ioctlsocket(sock, FIONREAD, &count);
242#else
243 int count = 0;
244 ioctl(sock, FIONREAD, &count);
245#endif
246
247 return count;
248}
249
250/* Read the next two bytes in TCP stream then convert them to 242/* Read the next two bytes in TCP stream then convert them to
251 * length (host byte order). 243 * length (host byte order).
252 * 244 *
@@ -256,11 +248,11 @@ unsigned int TCP_socket_data_recv_buffer(Socket sock)
256 */ 248 */
257uint16_t read_TCP_length(Socket sock) 249uint16_t read_TCP_length(Socket sock)
258{ 250{
259 unsigned int count = TCP_socket_data_recv_buffer(sock); 251 const unsigned int count = net_socket_data_recv_buffer(sock);
260 252
261 if (count >= sizeof(uint16_t)) { 253 if (count >= sizeof(uint16_t)) {
262 uint16_t length; 254 uint16_t length;
263 int len = recv(sock, (char *)&length, sizeof(uint16_t), MSG_NOSIGNAL); 255 const int len = net_recv(sock, &length, sizeof(uint16_t));
264 256
265 if (len != sizeof(uint16_t)) { 257 if (len != sizeof(uint16_t)) {
266 fprintf(stderr, "FAIL recv packet\n"); 258 fprintf(stderr, "FAIL recv packet\n");
@@ -286,10 +278,10 @@ uint16_t read_TCP_length(Socket sock)
286 */ 278 */
287int read_TCP_packet(Socket sock, uint8_t *data, uint16_t length) 279int read_TCP_packet(Socket sock, uint8_t *data, uint16_t length)
288{ 280{
289 unsigned int count = TCP_socket_data_recv_buffer(sock); 281 unsigned int count = net_socket_data_recv_buffer(sock);
290 282
291 if (count >= length) { 283 if (count >= length) {
292 int len = recv(sock, (char *)data, length, MSG_NOSIGNAL); 284 const int len = net_recv(sock, data, length);
293 285
294 if (len != length) { 286 if (len != length) {
295 fprintf(stderr, "FAIL recv packet\n"); 287 fprintf(stderr, "FAIL recv packet\n");
@@ -356,8 +348,8 @@ static int send_pending_data_nonpriority(TCP_Secure_Connection *con)
356 return 0; 348 return 0;
357 } 349 }
358 350
359 uint16_t left = con->last_packet_length - con->last_packet_sent; 351 const uint16_t left = con->last_packet_length - con->last_packet_sent;
360 int len = send(con->sock, (const char *)(con->last_packet + con->last_packet_sent), left, MSG_NOSIGNAL); 352 const int len = net_send(con->sock, con->last_packet + con->last_packet_sent, left);
361 353
362 if (len <= 0) { 354 if (len <= 0) {
363 return -1; 355 return -1;
@@ -386,8 +378,8 @@ static int send_pending_data(TCP_Secure_Connection *con)
386 TCP_Priority_List *p = con->priority_queue_start; 378 TCP_Priority_List *p = con->priority_queue_start;
387 379
388 while (p) { 380 while (p) {
389 uint16_t left = p->size - p->sent; 381 const uint16_t left = p->size - p->sent;
390 int len = send(con->sock, (const char *)(p->data + p->sent), left, MSG_NOSIGNAL); 382 const int len = net_send(con->sock, p->data + p->sent, left);
391 383
392 if (len != left) { 384 if (len != left) {
393 if (len > 0) { 385 if (len > 0) {
@@ -462,7 +454,7 @@ static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const
462 454
463 VLA(uint8_t, packet, sizeof(uint16_t) + length + CRYPTO_MAC_SIZE); 455 VLA(uint8_t, packet, sizeof(uint16_t) + length + CRYPTO_MAC_SIZE);
464 456
465 uint16_t c_length = net_htons(length + CRYPTO_MAC_SIZE); 457 const uint16_t c_length = net_htons(length + CRYPTO_MAC_SIZE);
466 memcpy(packet, &c_length, sizeof(uint16_t)); 458 memcpy(packet, &c_length, sizeof(uint16_t));
467 int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t)); 459 int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t));
468 460
@@ -471,7 +463,7 @@ static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const
471 } 463 }
472 464
473 if (priority) { 465 if (priority) {
474 len = sendpriority ? send(con->sock, (const char *)packet, SIZEOF_VLA(packet), MSG_NOSIGNAL) : 0; 466 len = sendpriority ? net_send(con->sock, packet, SIZEOF_VLA(packet)) : 0;
475 467
476 if (len <= 0) { 468 if (len <= 0) {
477 len = 0; 469 len = 0;
@@ -486,7 +478,7 @@ static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const
486 return add_priority(con, packet, SIZEOF_VLA(packet), len); 478 return add_priority(con, packet, SIZEOF_VLA(packet), len);
487 } 479 }
488 480
489 len = send(con->sock, (const char *)packet, SIZEOF_VLA(packet), MSG_NOSIGNAL); 481 len = net_send(con->sock, packet, SIZEOF_VLA(packet));
490 482
491 if (len <= 0) { 483 if (len <= 0) {
492 return 0; 484 return 0;
@@ -583,7 +575,7 @@ static int handle_TCP_handshake(TCP_Secure_Connection *con, const uint8_t *data,
583 return -1; 575 return -1;
584 } 576 }
585 577
586 if (TCP_SERVER_HANDSHAKE_SIZE != send(con->sock, (const char *)response, TCP_SERVER_HANDSHAKE_SIZE, MSG_NOSIGNAL)) { 578 if (TCP_SERVER_HANDSHAKE_SIZE != net_send(con->sock, response, TCP_SERVER_HANDSHAKE_SIZE)) {
587 return -1; 579 return -1;
588 } 580 }
589 581
@@ -1005,12 +997,12 @@ static int accept_connection(TCP_Server *TCP_server, Socket sock)
1005 return index; 997 return index;
1006} 998}
1007 999
1008static Socket new_listening_TCP_socket(int family, uint16_t port) 1000static Socket new_listening_TCP_socket(Family family, uint16_t port)
1009{ 1001{
1010 Socket sock = net_socket(family, TOX_SOCK_STREAM, TOX_PROTO_TCP); 1002 Socket sock = net_socket(family, TOX_SOCK_STREAM, TOX_PROTO_TCP);
1011 1003
1012 if (!sock_valid(sock)) { 1004 if (!sock_valid(sock)) {
1013 return ~0; 1005 return net_invalid_socket;
1014 } 1006 }
1015 1007
1016 int ok = set_socket_nonblock(sock); 1008 int ok = set_socket_nonblock(sock);
@@ -1023,11 +1015,11 @@ static Socket new_listening_TCP_socket(int family, uint16_t port)
1023 ok = set_socket_reuseaddr(sock); 1015 ok = set_socket_reuseaddr(sock);
1024 } 1016 }
1025 1017
1026 ok = ok && bind_to_port(sock, family, port) && (listen(sock, TCP_MAX_BACKLOG) == 0); 1018 ok = ok && bind_to_port(sock, family, port) && (net_listen(sock, TCP_MAX_BACKLOG) == 0);
1027 1019
1028 if (!ok) { 1020 if (!ok) {
1029 kill_sock(sock); 1021 kill_sock(sock);
1030 return ~0; 1022 return net_invalid_socket;
1031 } 1023 }
1032 1024
1033 return sock; 1025 return sock;
@@ -1087,9 +1079,9 @@ TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, const uin
1087 if (sock_valid(sock)) { 1079 if (sock_valid(sock)) {
1088#ifdef TCP_SERVER_USE_EPOLL 1080#ifdef TCP_SERVER_USE_EPOLL
1089 ev.events = EPOLLIN | EPOLLET; 1081 ev.events = EPOLLIN | EPOLLET;
1090 ev.data.u64 = sock | ((uint64_t)TCP_SOCKET_LISTENING << 32); 1082 ev.data.u64 = sock.socket | ((uint64_t)TCP_SOCKET_LISTENING << 32);
1091 1083
1092 if (epoll_ctl(temp->efd, EPOLL_CTL_ADD, sock, &ev) == -1) { 1084 if (epoll_ctl(temp->efd, EPOLL_CTL_ADD, sock.socket, &ev) == -1) {
1093 continue; 1085 continue;
1094 } 1086 }
1095 1087
@@ -1119,20 +1111,20 @@ TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, const uin
1119 return temp; 1111 return temp;
1120} 1112}
1121 1113
1114#ifndef TCP_SERVER_USE_EPOLL
1122static void do_TCP_accept_new(TCP_Server *TCP_server) 1115static void do_TCP_accept_new(TCP_Server *TCP_server)
1123{ 1116{
1124 uint32_t i; 1117 uint32_t i;
1125 1118
1126 for (i = 0; i < TCP_server->num_listening_socks; ++i) { 1119 for (i = 0; i < TCP_server->num_listening_socks; ++i) {
1127 struct sockaddr_storage addr;
1128 socklen_t addrlen = sizeof(addr);
1129 Socket sock; 1120 Socket sock;
1130 1121
1131 do { 1122 do {
1132 sock = accept(TCP_server->socks_listening[i], (struct sockaddr *)&addr, &addrlen); 1123 sock = net_accept(TCP_server->socks_listening[i]);
1133 } while (accept_connection(TCP_server, sock) != -1); 1124 } while (accept_connection(TCP_server, sock) != -1);
1134 } 1125 }
1135} 1126}
1127#endif
1136 1128
1137static int do_incoming(TCP_Server *TCP_server, uint32_t i) 1129static int do_incoming(TCP_Server *TCP_server, uint32_t i)
1138{ 1130{
@@ -1208,6 +1200,7 @@ static void do_confirmed_recv(TCP_Server *TCP_server, uint32_t i)
1208 } 1200 }
1209} 1201}
1210 1202
1203#ifndef TCP_SERVER_USE_EPOLL
1211static void do_TCP_incoming(TCP_Server *TCP_server) 1204static void do_TCP_incoming(TCP_Server *TCP_server)
1212{ 1205{
1213 uint32_t i; 1206 uint32_t i;
@@ -1225,6 +1218,7 @@ static void do_TCP_unconfirmed(TCP_Server *TCP_server)
1225 do_unconfirmed(TCP_server, i); 1218 do_unconfirmed(TCP_server, i);
1226 } 1219 }
1227} 1220}
1221#endif
1228 1222
1229static void do_TCP_confirmed(TCP_Server *TCP_server) 1223static void do_TCP_confirmed(TCP_Server *TCP_server)
1230{ 1224{
@@ -1294,8 +1288,9 @@ static void do_TCP_epoll(TCP_Server *TCP_server)
1294 int n; 1288 int n;
1295 1289
1296 for (n = 0; n < nfds; ++n) { 1290 for (n = 0; n < nfds; ++n) {
1297 Socket sock = events[n].data.u64 & 0xFFFFFFFF; 1291 const Socket sock = {(int)(events[n].data.u64 & 0xFFFFFFFF)};
1298 int status = (events[n].data.u64 >> 32) & 0xFF, index = (events[n].data.u64 >> 40); 1292 const int status = (events[n].data.u64 >> 32) & 0xFF;
1293 const int index = events[n].data.u64 >> 40;
1299 1294
1300 if ((events[n].events & EPOLLERR) || (events[n].events & EPOLLHUP) || (events[n].events & EPOLLRDHUP)) { 1295 if ((events[n].events & EPOLLERR) || (events[n].events & EPOLLHUP) || (events[n].events & EPOLLRDHUP)) {
1301 switch (status) { 1296 switch (status) {
@@ -1331,11 +1326,8 @@ static void do_TCP_epoll(TCP_Server *TCP_server)
1331 switch (status) { 1326 switch (status) {
1332 case TCP_SOCKET_LISTENING: { 1327 case TCP_SOCKET_LISTENING: {
1333 //socket is from socks_listening, accept connection 1328 //socket is from socks_listening, accept connection
1334 struct sockaddr_storage addr;
1335 socklen_t addrlen = sizeof(addr);
1336
1337 while (1) { 1329 while (1) {
1338 Socket sock_new = accept(sock, (struct sockaddr *)&addr, &addrlen); 1330 Socket sock_new = net_accept(sock);
1339 1331
1340 if (!sock_valid(sock_new)) { 1332 if (!sock_valid(sock_new)) {
1341 break; 1333 break;
@@ -1349,10 +1341,10 @@ static void do_TCP_epoll(TCP_Server *TCP_server)
1349 1341
1350 struct epoll_event ev = { 1342 struct epoll_event ev = {
1351 .events = EPOLLIN | EPOLLET | EPOLLRDHUP, 1343 .events = EPOLLIN | EPOLLET | EPOLLRDHUP,
1352 .data.u64 = sock_new | ((uint64_t)TCP_SOCKET_INCOMING << 32) | ((uint64_t)index_new << 40) 1344 .data.u64 = sock_new.socket | ((uint64_t)TCP_SOCKET_INCOMING << 32) | ((uint64_t)index_new << 40)
1353 }; 1345 };
1354 1346
1355 if (epoll_ctl(TCP_server->efd, EPOLL_CTL_ADD, sock_new, &ev) == -1) { 1347 if (epoll_ctl(TCP_server->efd, EPOLL_CTL_ADD, sock_new.socket, &ev) == -1) {
1356 kill_TCP_secure_connection(&TCP_server->incoming_connection_queue[index_new]); 1348 kill_TCP_secure_connection(&TCP_server->incoming_connection_queue[index_new]);
1357 continue; 1349 continue;
1358 } 1350 }
@@ -1366,9 +1358,9 @@ static void do_TCP_epoll(TCP_Server *TCP_server)
1366 1358
1367 if ((index_new = do_incoming(TCP_server, index)) != -1) { 1359 if ((index_new = do_incoming(TCP_server, index)) != -1) {
1368 events[n].events = EPOLLIN | EPOLLET | EPOLLRDHUP; 1360 events[n].events = EPOLLIN | EPOLLET | EPOLLRDHUP;
1369 events[n].data.u64 = sock | ((uint64_t)TCP_SOCKET_UNCONFIRMED << 32) | ((uint64_t)index_new << 40); 1361 events[n].data.u64 = sock.socket | ((uint64_t)TCP_SOCKET_UNCONFIRMED << 32) | ((uint64_t)index_new << 40);
1370 1362
1371 if (epoll_ctl(TCP_server->efd, EPOLL_CTL_MOD, sock, &events[n]) == -1) { 1363 if (epoll_ctl(TCP_server->efd, EPOLL_CTL_MOD, sock.socket, &events[n]) == -1) {
1372 kill_TCP_secure_connection(&TCP_server->unconfirmed_connection_queue[index_new]); 1364 kill_TCP_secure_connection(&TCP_server->unconfirmed_connection_queue[index_new]);
1373 break; 1365 break;
1374 } 1366 }
@@ -1382,9 +1374,9 @@ static void do_TCP_epoll(TCP_Server *TCP_server)
1382 1374
1383 if ((index_new = do_unconfirmed(TCP_server, index)) != -1) { 1375 if ((index_new = do_unconfirmed(TCP_server, index)) != -1) {
1384 events[n].events = EPOLLIN | EPOLLET | EPOLLRDHUP; 1376 events[n].events = EPOLLIN | EPOLLET | EPOLLRDHUP;
1385 events[n].data.u64 = sock | ((uint64_t)TCP_SOCKET_CONFIRMED << 32) | ((uint64_t)index_new << 40); 1377 events[n].data.u64 = sock.socket | ((uint64_t)TCP_SOCKET_CONFIRMED << 32) | ((uint64_t)index_new << 40);
1386 1378
1387 if (epoll_ctl(TCP_server->efd, EPOLL_CTL_MOD, sock, &events[n]) == -1) { 1379 if (epoll_ctl(TCP_server->efd, EPOLL_CTL_MOD, sock.socket, &events[n]) == -1) {
1388 //remove from confirmed connections 1380 //remove from confirmed connections
1389 kill_accepted(TCP_server, index_new); 1381 kill_accepted(TCP_server, index_new);
1390 break; 1382 break;
diff --git a/toxcore/TCP_server.h b/toxcore/TCP_server.h
index 234f5ad1..c8cbeda0 100644
--- a/toxcore/TCP_server.h
+++ b/toxcore/TCP_server.h
@@ -32,11 +32,6 @@
32#include <sys/epoll.h> 32#include <sys/epoll.h>
33#endif 33#endif
34 34
35// Disable MSG_NOSIGNAL on systems not supporting it, e.g. Windows, FreeBSD
36#if !defined(MSG_NOSIGNAL)
37#define MSG_NOSIGNAL 0
38#endif
39
40#define MAX_INCOMING_CONNECTIONS 256 35#define MAX_INCOMING_CONNECTIONS 256
41 36
42#define TCP_MAX_BACKLOG MAX_INCOMING_CONNECTIONS 37#define TCP_MAX_BACKLOG MAX_INCOMING_CONNECTIONS
@@ -108,11 +103,6 @@ void do_TCP_server(TCP_Server *TCP_server);
108 */ 103 */
109void kill_TCP_server(TCP_Server *TCP_server); 104void kill_TCP_server(TCP_Server *TCP_server);
110 105
111/* return the amount of data in the tcp recv buffer.
112 * return 0 on failure.
113 */
114unsigned int TCP_socket_data_recv_buffer(Socket sock);
115
116/* Read the next two bytes in TCP stream then convert them to 106/* Read the next two bytes in TCP stream then convert them to
117 * length (host byte order). 107 * length (host byte order).
118 * 108 *
diff --git a/toxcore/crypto_core.api.h b/toxcore/crypto_core.api.h
index e9e8aeb2..4b40860d 100644
--- a/toxcore/crypto_core.api.h
+++ b/toxcore/crypto_core.api.h
@@ -26,8 +26,8 @@
26#define CRYPTO_CORE_H 26#define CRYPTO_CORE_H
27 27
28#include <stdbool.h> 28#include <stdbool.h>
29#include <stddef.h>
29#include <stdint.h> 30#include <stdint.h>
30#include <stdlib.h>
31 31
32#ifdef __cplusplus 32#ifdef __cplusplus
33extern "C" { 33extern "C" {
diff --git a/toxcore/crypto_core.h b/toxcore/crypto_core.h
index e7e913b6..18c1339e 100644
--- a/toxcore/crypto_core.h
+++ b/toxcore/crypto_core.h
@@ -25,8 +25,8 @@
25#define CRYPTO_CORE_H 25#define CRYPTO_CORE_H
26 26
27#include <stdbool.h> 27#include <stdbool.h>
28#include <stddef.h>
28#include <stdint.h> 29#include <stdint.h>
29#include <stdlib.h>
30 30
31#ifdef __cplusplus 31#ifdef __cplusplus
32extern "C" { 32extern "C" {
diff --git a/toxcore/friend_connection.c b/toxcore/friend_connection.c
index 6f5685fb..0ba8178f 100644
--- a/toxcore/friend_connection.c
+++ b/toxcore/friend_connection.c
@@ -27,6 +27,10 @@
27 27
28#include "friend_connection.h" 28#include "friend_connection.h"
29 29
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33
30#include "util.h" 34#include "util.h"
31 35
32#define PORTS_PER_DISCOVERY 10 36#define PORTS_PER_DISCOVERY 10
diff --git a/toxcore/friend_requests.c b/toxcore/friend_requests.c
index 13c89eae..3123e4ff 100644
--- a/toxcore/friend_requests.c
+++ b/toxcore/friend_requests.c
@@ -27,6 +27,9 @@
27 27
28#include "friend_requests.h" 28#include "friend_requests.h"
29 29
30#include <stdlib.h>
31#include <string.h>
32
30#include "util.h" 33#include "util.h"
31 34
32struct Friend_Requests { 35struct Friend_Requests {
diff --git a/toxcore/group.c b/toxcore/group.c
index 37b4437d..b77ae38a 100644
--- a/toxcore/group.c
+++ b/toxcore/group.c
@@ -27,6 +27,9 @@
27 27
28#include "group.h" 28#include "group.h"
29 29
30#include <stdlib.h>
31#include <string.h>
32
30#include "util.h" 33#include "util.h"
31 34
32/* return 1 if the groupnumber is not valid. 35/* return 1 if the groupnumber is not valid.
diff --git a/toxcore/list.c b/toxcore/list.c
index 8fd1f5fb..7b65e184 100644
--- a/toxcore/list.c
+++ b/toxcore/list.c
@@ -29,6 +29,9 @@
29 29
30#include "list.h" 30#include "list.h"
31 31
32#include <stdlib.h>
33#include <string.h>
34
32#include "ccompat.h" 35#include "ccompat.h"
33 36
34/* Basically, the elements in the list are placed in order so that they can be searched for easily 37/* Basically, the elements in the list are placed in order so that they can be searched for easily
diff --git a/toxcore/list.h b/toxcore/list.h
index cb3b328c..9ad8f44d 100644
--- a/toxcore/list.h
+++ b/toxcore/list.h
@@ -27,8 +27,6 @@
27#define LIST_H 27#define LIST_H
28 28
29#include <stdint.h> 29#include <stdint.h>
30#include <stdlib.h>
31#include <string.h>
32 30
33typedef struct { 31typedef struct {
34 uint32_t n; //number of elements 32 uint32_t n; //number of elements
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index 2e8299f1..b9d8a360 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -29,9 +29,11 @@
29 29
30#include "net_crypto.h" 30#include "net_crypto.h"
31 31
32#include "util.h"
33
34#include <math.h> 32#include <math.h>
33#include <stdlib.h>
34#include <string.h>
35
36#include "util.h"
35 37
36typedef struct { 38typedef struct {
37 uint64_t sent_time; 39 uint64_t sent_time;
diff --git a/toxcore/network.c b/toxcore/network.c
index 984071d4..f357d887 100644
--- a/toxcore/network.c
+++ b/toxcore/network.c
@@ -38,44 +38,65 @@
38#define _WIN32_WINNT 0x501 38#define _WIN32_WINNT 0x501
39#endif 39#endif
40 40
41#include "network.h" 41#if !defined(OS_WIN32) && (defined(_WIN32) || defined(__WIN32__) || defined(WIN32))
42#define OS_WIN32
43#endif
42 44
43#include "logger.h" 45#ifdef OS_WIN32
44#include "util.h" 46#ifndef WINVER
47//Windows XP
48#define WINVER 0x0501
49#endif
50#endif
45 51
46#include <assert.h> 52#ifdef PLAN9
47#ifdef __APPLE__ 53#include <u.h> // Plan 9 requires this is imported first
48#include <mach/clock.h> 54// Comment line here to avoid reordering by source code formatters.
49#include <mach/mach.h> 55#include <libc.h>
50#endif 56#endif
51 57
52#ifndef IPV6_ADD_MEMBERSHIP 58#ifdef OS_WIN32 /* Put win32 includes here */
53#ifdef IPV6_JOIN_GROUP 59// The mingw32/64 Windows library warns about including winsock2.h after
54#define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP 60// windows.h even though with the above it's a valid thing to do. So, to make
61// mingw32 headers happy, we include winsock2.h first.
62#include <winsock2.h>
63// Comment line here to avoid reordering by source code formatters.
64#include <windows.h>
65#include <ws2tcpip.h>
55#endif 66#endif
67
68#include "network.h"
69
70#ifdef __APPLE__
71#include <mach/clock.h>
72#include <mach/mach.h>
56#endif 73#endif
57 74
58#if !(defined(_WIN32) || defined(__WIN32__) || defined(WIN32)) 75#if !defined(OS_WIN32)
59 76
77#include <arpa/inet.h>
60#include <errno.h> 78#include <errno.h>
61#include <fcntl.h> 79#include <fcntl.h>
62#include <netdb.h> 80#include <netdb.h>
81#include <netinet/in.h>
82#include <sys/ioctl.h>
83#include <sys/socket.h>
63#include <sys/time.h> 84#include <sys/time.h>
64#include <sys/types.h> 85#include <sys/types.h>
86#include <unistd.h>
65 87
66#define TOX_EWOULDBLOCK EWOULDBLOCK 88#define TOX_EWOULDBLOCK EWOULDBLOCK
67 89
68#else 90#else
69
70#ifndef IPV6_V6ONLY 91#ifndef IPV6_V6ONLY
71#define IPV6_V6ONLY 27 92#define IPV6_V6ONLY 27
72#endif 93#endif
73 94
74#define TOX_EWOULDBLOCK WSAEWOULDBLOCK 95#define TOX_EWOULDBLOCK WSAEWOULDBLOCK
75 96
76static const char *inet_ntop(Family family, const void *addr, char *buf, size_t bufsize) 97static const char *inet_ntop(int family, const void *addr, char *buf, size_t bufsize)
77{ 98{
78 if (family == TOX_AF_INET) { 99 if (family == AF_INET) {
79 struct sockaddr_in saddr; 100 struct sockaddr_in saddr;
80 memset(&saddr, 0, sizeof(saddr)); 101 memset(&saddr, 0, sizeof(saddr));
81 102
@@ -89,7 +110,7 @@ static const char *inet_ntop(Family family, const void *addr, char *buf, size_t
89 } 110 }
90 111
91 return buf; 112 return buf;
92 } else if (family == TOX_AF_INET6) { 113 } else if (family == AF_INET6) {
93 struct sockaddr_in6 saddr; 114 struct sockaddr_in6 saddr;
94 memset(&saddr, 0, sizeof(saddr)); 115 memset(&saddr, 0, sizeof(saddr));
95 116
@@ -108,9 +129,9 @@ static const char *inet_ntop(Family family, const void *addr, char *buf, size_t
108 return nullptr; 129 return nullptr;
109} 130}
110 131
111static int inet_pton(Family family, const char *addrString, void *addrbuf) 132static int inet_pton(int family, const char *addrString, void *addrbuf)
112{ 133{
113 if (family == TOX_AF_INET) { 134 if (family == AF_INET) {
114 struct sockaddr_in saddr; 135 struct sockaddr_in saddr;
115 memset(&saddr, 0, sizeof(saddr)); 136 memset(&saddr, 0, sizeof(saddr));
116 137
@@ -123,7 +144,7 @@ static int inet_pton(Family family, const char *addrString, void *addrbuf)
123 *(struct in_addr *)addrbuf = saddr.sin_addr; 144 *(struct in_addr *)addrbuf = saddr.sin_addr;
124 145
125 return 1; 146 return 1;
126 } else if (family == TOX_AF_INET6) { 147 } else if (family == AF_INET6) {
127 struct sockaddr_in6 saddr; 148 struct sockaddr_in6 saddr;
128 memset(&saddr, 0, sizeof(saddr)); 149 memset(&saddr, 0, sizeof(saddr));
129 150
@@ -143,6 +164,25 @@ static int inet_pton(Family family, const char *addrString, void *addrbuf)
143 164
144#endif 165#endif
145 166
167#include <assert.h>
168#include <stdio.h>
169#include <stdlib.h>
170#include <string.h>
171
172#include "logger.h"
173#include "util.h"
174
175// Disable MSG_NOSIGNAL on systems not supporting it, e.g. Windows, FreeBSD
176#if !defined(MSG_NOSIGNAL)
177#define MSG_NOSIGNAL 0
178#endif
179
180#ifndef IPV6_ADD_MEMBERSHIP
181#ifdef IPV6_JOIN_GROUP
182#define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
183#endif
184#endif
185
146#if TOX_INET6_ADDRSTRLEN < INET6_ADDRSTRLEN 186#if TOX_INET6_ADDRSTRLEN < INET6_ADDRSTRLEN
147#error TOX_INET6_ADDRSTRLEN should be greater or equal to INET6_ADDRSTRLEN (#INET6_ADDRSTRLEN) 187#error TOX_INET6_ADDRSTRLEN should be greater or equal to INET6_ADDRSTRLEN (#INET6_ADDRSTRLEN)
148#endif 188#endif
@@ -153,8 +193,40 @@ static int inet_pton(Family family, const char *addrString, void *addrbuf)
153 193
154static int make_proto(int proto); 194static int make_proto(int proto);
155static int make_socktype(int type); 195static int make_socktype(int type);
156static int make_family(int tox_family); 196
157static int make_tox_family(int family); 197static int make_family(Family tox_family)
198{
199 switch (tox_family) {
200 case TOX_AF_INET:
201 return AF_INET;
202
203 case TOX_AF_INET6:
204 return AF_INET6;
205
206 case TOX_AF_UNSPEC:
207 return AF_UNSPEC;
208
209 default:
210 return tox_family;
211 }
212}
213
214static Family make_tox_family(int family)
215{
216 switch (family) {
217 case AF_INET:
218 return TOX_AF_INET;
219
220 case AF_INET6:
221 return TOX_AF_INET6;
222
223 case AF_UNSPEC:
224 return TOX_AF_UNSPEC;
225
226 default:
227 return family;
228 }
229}
158 230
159static void get_ip4(IP4 *result, const struct in_addr *addr) 231static void get_ip4(IP4 *result, const struct in_addr *addr)
160{ 232{
@@ -201,6 +273,14 @@ IP6 get_ip6_loopback(void)
201 return loopback; 273 return loopback;
202} 274}
203 275
276const Socket net_invalid_socket = {
277#ifdef OS_WIN32
278 (int)INVALID_SOCKET,
279#else
280 -1,
281#endif
282};
283
204/* Check if socket is valid. 284/* Check if socket is valid.
205 * 285 *
206 * return 1 if valid 286 * return 1 if valid
@@ -208,27 +288,17 @@ IP6 get_ip6_loopback(void)
208 */ 288 */
209int sock_valid(Socket sock) 289int sock_valid(Socket sock)
210{ 290{
211#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) 291 return sock.socket != net_invalid_socket.socket;
212
213 if (sock == INVALID_SOCKET) {
214#else
215
216 if (sock < 0) {
217#endif
218 return 0;
219 }
220
221 return 1;
222} 292}
223 293
224/* Close the socket. 294/* Close the socket.
225 */ 295 */
226void kill_sock(Socket sock) 296void kill_sock(Socket sock)
227{ 297{
228#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) 298#ifdef OS_WIN32
229 closesocket(sock); 299 closesocket(sock.socket);
230#else 300#else
231 close(sock); 301 close(sock.socket);
232#endif 302#endif
233} 303}
234 304
@@ -239,11 +309,11 @@ void kill_sock(Socket sock)
239 */ 309 */
240int set_socket_nonblock(Socket sock) 310int set_socket_nonblock(Socket sock)
241{ 311{
242#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) 312#ifdef OS_WIN32
243 u_long mode = 1; 313 u_long mode = 1;
244 return (ioctlsocket(sock, FIONBIO, &mode) == 0); 314 return (ioctlsocket(sock.socket, FIONBIO, &mode) == 0);
245#else 315#else
246 return (fcntl(sock, F_SETFL, O_NONBLOCK, 1) == 0); 316 return (fcntl(sock.socket, F_SETFL, O_NONBLOCK, 1) == 0);
247#endif 317#endif
248} 318}
249 319
@@ -256,7 +326,7 @@ int set_socket_nosigpipe(Socket sock)
256{ 326{
257#if defined(__MACH__) 327#if defined(__MACH__)
258 int set = 1; 328 int set = 1;
259 return (setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, (const char *)&set, sizeof(int)) == 0); 329 return setsockopt(sock.socket, SOL_SOCKET, SO_NOSIGPIPE, (const char *)&set, sizeof(int)) == 0;
260#else 330#else
261 return 1; 331 return 1;
262#endif 332#endif
@@ -270,7 +340,7 @@ int set_socket_nosigpipe(Socket sock)
270int set_socket_reuseaddr(Socket sock) 340int set_socket_reuseaddr(Socket sock)
271{ 341{
272 int set = 1; 342 int set = 1;
273 return (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char *)&set, sizeof(set)) == 0); 343 return setsockopt(sock.socket, SOL_SOCKET, SO_REUSEADDR, (const char *)&set, sizeof(set)) == 0;
274} 344}
275 345
276/* Set socket to dual (IPv4 + IPv6 socket) 346/* Set socket to dual (IPv4 + IPv6 socket)
@@ -282,14 +352,14 @@ int set_socket_dualstack(Socket sock)
282{ 352{
283 int ipv6only = 0; 353 int ipv6only = 0;
284 socklen_t optsize = sizeof(ipv6only); 354 socklen_t optsize = sizeof(ipv6only);
285 int res = getsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&ipv6only, &optsize); 355 int res = getsockopt(sock.socket, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&ipv6only, &optsize);
286 356
287 if ((res == 0) && (ipv6only == 0)) { 357 if ((res == 0) && (ipv6only == 0)) {
288 return 1; 358 return 1;
289 } 359 }
290 360
291 ipv6only = 0; 361 ipv6only = 0;
292 return (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (const char *)&ipv6only, sizeof(ipv6only)) == 0); 362 return setsockopt(sock.socket, IPPROTO_IPV6, IPV6_V6ONLY, (const char *)&ipv6only, sizeof(ipv6only)) == 0;
293} 363}
294 364
295 365
@@ -297,7 +367,7 @@ int set_socket_dualstack(Socket sock)
297static uint64_t current_time_actual(void) 367static uint64_t current_time_actual(void)
298{ 368{
299 uint64_t time; 369 uint64_t time;
300#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) 370#ifdef OS_WIN32
301 /* This probably works fine */ 371 /* This probably works fine */
302 FILETIME ft; 372 FILETIME ft;
303 GetSystemTimeAsFileTime(&ft); 373 GetSystemTimeAsFileTime(&ft);
@@ -315,7 +385,7 @@ static uint64_t current_time_actual(void)
315} 385}
316 386
317 387
318#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) 388#ifdef OS_WIN32
319static uint64_t last_monotime; 389static uint64_t last_monotime;
320static uint64_t add_monotime; 390static uint64_t add_monotime;
321#endif 391#endif
@@ -324,7 +394,7 @@ static uint64_t add_monotime;
324uint64_t current_time_monotonic(void) 394uint64_t current_time_monotonic(void)
325{ 395{
326 uint64_t time; 396 uint64_t time;
327#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) 397#ifdef OS_WIN32
328 uint64_t old_add_monotime = add_monotime; 398 uint64_t old_add_monotime = add_monotime;
329 time = (uint64_t)GetTickCount() + add_monotime; 399 time = (uint64_t)GetTickCount() + add_monotime;
330 400
@@ -472,11 +542,11 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint1
472 addr6->sin6_flowinfo = 0; 542 addr6->sin6_flowinfo = 0;
473 addr6->sin6_scope_id = 0; 543 addr6->sin6_scope_id = 0;
474 } else { 544 } else {
475 /* unknown address type*/ 545 LOGGER_WARNING(net->log, "unknown address type: %d", ip_port.ip.family);
476 return -1; 546 return -1;
477 } 547 }
478 548
479 const int res = sendto(net->sock, (const char *) data, length, 0, (struct sockaddr *)&addr, addrsize); 549 const int res = sendto(net->sock.socket, (const char *) data, length, 0, (struct sockaddr *)&addr, addrsize);
480 550
481 loglogdata(net->log, "O=>", data, length, ip_port, res); 551 loglogdata(net->log, "O=>", data, length, ip_port, res);
482 552
@@ -492,13 +562,13 @@ static int receivepacket(Logger *log, Socket sock, IP_Port *ip_port, uint8_t *da
492{ 562{
493 memset(ip_port, 0, sizeof(IP_Port)); 563 memset(ip_port, 0, sizeof(IP_Port));
494 struct sockaddr_storage addr; 564 struct sockaddr_storage addr;
495#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) 565#ifdef OS_WIN32
496 int addrlen = sizeof(addr); 566 int addrlen = sizeof(addr);
497#else 567#else
498 socklen_t addrlen = sizeof(addr); 568 socklen_t addrlen = sizeof(addr);
499#endif 569#endif
500 *length = 0; 570 *length = 0;
501 int fail_or_len = recvfrom(sock, (char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen); 571 int fail_or_len = recvfrom(sock.socket, (char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen);
502 572
503 if (fail_or_len < 0) { 573 if (fail_or_len < 0) {
504 int error = net_error(); 574 int error = net_error();
@@ -597,7 +667,7 @@ int networking_at_startup(void)
597 667
598#endif/*VANILLA_NACL*/ 668#endif/*VANILLA_NACL*/
599 669
600#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) 670#ifdef OS_WIN32
601 WSADATA wsaData; 671 WSADATA wsaData;
602 672
603 if (WSAStartup(MAKEWORD(2, 2), &wsaData) != NO_ERROR) { 673 if (WSAStartup(MAKEWORD(2, 2), &wsaData) != NO_ERROR) {
@@ -614,7 +684,7 @@ int networking_at_startup(void)
614#if 0 684#if 0
615static void at_shutdown(void) 685static void at_shutdown(void)
616{ 686{
617#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) 687#ifdef OS_WIN32
618 WSACleanup(); 688 WSACleanup();
619#endif 689#endif
620} 690}
@@ -703,12 +773,12 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1
703 /* Functions to increase the size of the send and receive UDP buffers. 773 /* Functions to increase the size of the send and receive UDP buffers.
704 */ 774 */
705 int n = 1024 * 1024 * 2; 775 int n = 1024 * 1024 * 2;
706 setsockopt(temp->sock, SOL_SOCKET, SO_RCVBUF, (const char *)&n, sizeof(n)); 776 setsockopt(temp->sock.socket, SOL_SOCKET, SO_RCVBUF, (const char *)&n, sizeof(n));
707 setsockopt(temp->sock, SOL_SOCKET, SO_SNDBUF, (const char *)&n, sizeof(n)); 777 setsockopt(temp->sock.socket, SOL_SOCKET, SO_SNDBUF, (const char *)&n, sizeof(n));
708 778
709 /* Enable broadcast on socket */ 779 /* Enable broadcast on socket */
710 int broadcast = 1; 780 int broadcast = 1;
711 setsockopt(temp->sock, SOL_SOCKET, SO_BROADCAST, (const char *)&broadcast, sizeof(broadcast)); 781 setsockopt(temp->sock.socket, SOL_SOCKET, SO_BROADCAST, (const char *)&broadcast, sizeof(broadcast));
712 782
713 /* iOS UDP sockets are weird and apparently can SIGPIPE */ 783 /* iOS UDP sockets are weird and apparently can SIGPIPE */
714 if (!set_socket_nosigpipe(temp->sock)) { 784 if (!set_socket_nosigpipe(temp->sock)) {
@@ -776,7 +846,7 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1
776 mreq.ipv6mr_multiaddr.s6_addr[ 1] = 0x02; 846 mreq.ipv6mr_multiaddr.s6_addr[ 1] = 0x02;
777 mreq.ipv6mr_multiaddr.s6_addr[15] = 0x01; 847 mreq.ipv6mr_multiaddr.s6_addr[15] = 0x01;
778 mreq.ipv6mr_interface = 0; 848 mreq.ipv6mr_interface = 0;
779 int res = setsockopt(temp->sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (const char *)&mreq, sizeof(mreq)); 849 const int res = setsockopt(temp->sock.socket, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (const char *)&mreq, sizeof(mreq));
780 850
781 int neterror = net_error(); 851 int neterror = net_error();
782 const char *strerror = net_new_strerror(neterror); 852 const char *strerror = net_new_strerror(neterror);
@@ -806,7 +876,7 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1
806 int tries; 876 int tries;
807 877
808 for (tries = port_from; tries <= port_to; tries++) { 878 for (tries = port_from; tries <= port_to; tries++) {
809 int res = bind(temp->sock, (struct sockaddr *)&addr, addrsize); 879 int res = bind(temp->sock.socket, (struct sockaddr *)&addr, addrsize);
810 880
811 if (!res) { 881 if (!res) {
812 temp->port = *portptr; 882 temp->port = *portptr;
@@ -1290,7 +1360,7 @@ int net_connect(Socket sock, IP_Port ip_port)
1290 return 0; 1360 return 0;
1291 } 1361 }
1292 1362
1293 return connect(sock, (struct sockaddr *)&addr, addrsize); 1363 return connect(sock.socket, (struct sockaddr *)&addr, addrsize);
1294} 1364}
1295 1365
1296int32_t net_getipport(const char *node, IP_Port **res, int tox_type) 1366int32_t net_getipport(const char *node, IP_Port **res, int tox_type)
@@ -1391,41 +1461,7 @@ int bind_to_port(Socket sock, int family, uint16_t port)
1391 return 0; 1461 return 0;
1392 } 1462 }
1393 1463
1394 return (bind(sock, (struct sockaddr *)&addr, addrsize) == 0); 1464 return bind(sock.socket, (struct sockaddr *)&addr, addrsize) == 0;
1395}
1396
1397static int make_tox_family(int family)
1398{
1399 switch (family) {
1400 case AF_INET:
1401 return TOX_AF_INET;
1402
1403 case AF_INET6:
1404 return TOX_AF_INET6;
1405
1406 case AF_UNSPEC:
1407 return TOX_AF_UNSPEC;
1408
1409 default:
1410 return family;
1411 }
1412}
1413
1414static int make_family(int tox_family)
1415{
1416 switch (tox_family) {
1417 case TOX_AF_INET:
1418 return AF_INET;
1419
1420 case TOX_AF_INET6:
1421 return AF_INET6;
1422
1423 case TOX_AF_UNSPEC:
1424 return AF_UNSPEC;
1425
1426 default:
1427 return tox_family;
1428 }
1429} 1465}
1430 1466
1431static int make_socktype(int type) 1467static int make_socktype(int type)
@@ -1456,12 +1492,47 @@ static int make_proto(int proto)
1456 } 1492 }
1457} 1493}
1458 1494
1459Socket net_socket(int domain, int type, int protocol) 1495Socket net_socket(Family domain, int type, int protocol)
1496{
1497 const int platform_domain = make_family(domain);
1498 const int platform_type = make_socktype(type);
1499 const int platform_prot = make_proto(protocol);
1500 const Socket sock = {(int)socket(platform_domain, platform_type, platform_prot)};
1501 return sock;
1502}
1503
1504int net_send(Socket sock, const void *buf, size_t len)
1460{ 1505{
1461 int platform_domain = make_family(domain); 1506 return send(sock.socket, (const char *)buf, len, MSG_NOSIGNAL);
1462 int platform_type = make_socktype(type); 1507}
1463 int platform_prot = make_proto(protocol); 1508
1464 return socket(platform_domain, platform_type, platform_prot); 1509int net_recv(Socket sock, void *buf, size_t len)
1510{
1511 return recv(sock.socket, (char *)buf, len, MSG_NOSIGNAL);
1512}
1513
1514int net_listen(Socket sock, int backlog)
1515{
1516 return listen(sock.socket, backlog);
1517}
1518
1519Socket net_accept(Socket sock)
1520{
1521 const Socket newsock = {accept(sock.socket, nullptr, nullptr)};
1522 return newsock;
1523}
1524
1525size_t net_socket_data_recv_buffer(Socket sock)
1526{
1527#ifdef OS_WIN32
1528 unsigned long count = 0;
1529 ioctlsocket(sock.socket, FIONREAD, &count);
1530#else
1531 int count = 0;
1532 ioctl(sock.socket, FIONREAD, &count);
1533#endif
1534
1535 return count;
1465} 1536}
1466 1537
1467uint32_t net_htonl(uint32_t hostlong) 1538uint32_t net_htonl(uint32_t hostlong)
diff --git a/toxcore/network.h b/toxcore/network.h
index 10ddef02..bdfebd7e 100644
--- a/toxcore/network.h
+++ b/toxcore/network.h
@@ -24,54 +24,55 @@
24#ifndef NETWORK_H 24#ifndef NETWORK_H
25#define NETWORK_H 25#define NETWORK_H
26 26
27#ifdef PLAN9
28#include <u.h> // Plan 9 requires this is imported first
29// Comment line here to avoid reordering by source code formatters.
30#include <libc.h>
31#endif
32
33#include "ccompat.h"
34#include "logger.h" 27#include "logger.h"
35 28
36#include <stdbool.h> 29#include <stdbool.h> // bool
37#include <stdint.h> 30#include <stddef.h> // size_t
38#include <stdio.h> 31#include <stdint.h> // uint*_t
39#include <stdlib.h>
40#include <string.h>
41#include <time.h>
42
43#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) /* Put win32 includes here */
44#ifndef WINVER
45//Windows XP
46#define WINVER 0x0501
47#endif
48 32
49// The mingw32/64 Windows library warns about including winsock2.h after 33#ifdef __cplusplus
50// windows.h even though with the above it's a valid thing to do. So, to make 34extern "C" {
51// mingw32 headers happy, we include winsock2.h first. 35#endif
52#include <winsock2.h>
53 36
54#include <windows.h> 37typedef uint8_t Family;
55#include <ws2tcpip.h>
56 38
57#else // UNIX includes 39typedef struct Socket {
40 int socket;
41} Socket;
58 42
59#include <sys/socket.h> 43Socket net_socket(Family domain, int type, int protocol);
60#include <netinet/in.h>
61#include <arpa/inet.h>
62#include <netdb.h>
63#include <unistd.h>
64 44
65#endif 45/* Check if socket is valid.
46 *
47 * return 1 if valid
48 * return 0 if not valid
49 */
50int sock_valid(Socket sock);
66 51
67#ifdef __cplusplus 52extern const Socket net_invalid_socket;
68extern "C" {
69#endif
70 53
71typedef short Family; 54/**
55 * Calls send(sockfd, buf, len, MSG_NOSIGNAL).
56 */
57int net_send(Socket sockfd, const void *buf, size_t len);
58/**
59 * Calls recv(sockfd, buf, len, MSG_NOSIGNAL).
60 */
61int net_recv(Socket sockfd, void *buf, size_t len);
62/**
63 * Calls listen(sockfd, backlog).
64 */
65int net_listen(Socket sockfd, int backlog);
66/**
67 * Calls accept(sockfd, nullptr, nullptr).
68 */
69Socket net_accept(Socket sockfd);
72 70
73typedef int Socket; 71/**
74Socket net_socket(int domain, int type, int protocol); 72 * return the amount of data in the tcp recv buffer.
73 * return 0 on failure.
74 */
75size_t net_socket_data_recv_buffer(Socket sock);
75 76
76#define MAX_UDP_PACKET_SIZE 2048 77#define MAX_UDP_PACKET_SIZE 2048
77 78
@@ -326,13 +327,6 @@ uint16_t net_port(const Networking_Core *net);
326 */ 327 */
327int networking_at_startup(void); 328int networking_at_startup(void);
328 329
329/* Check if socket is valid.
330 *
331 * return 1 if valid
332 * return 0 if not valid
333 */
334int sock_valid(Socket sock);
335
336/* Close the socket. 330/* Close the socket.
337 */ 331 */
338void kill_sock(Socket sock); 332void kill_sock(Socket sock);
diff --git a/toxcore/onion.c b/toxcore/onion.c
index 740c25ce..476f76e9 100644
--- a/toxcore/onion.c
+++ b/toxcore/onion.c
@@ -3,7 +3,7 @@
3 */ 3 */
4 4
5/* 5/*
6 * Copyright © 2016-2017 The TokTok team. 6 * Copyright © 2016-2018 The TokTok team.
7 * Copyright © 2013 Tox project. 7 * Copyright © 2013 Tox project.
8 * 8 *
9 * This file is part of Tox, the free peer to peer instant messenger. 9 * This file is part of Tox, the free peer to peer instant messenger.
@@ -27,6 +27,9 @@
27 27
28#include "onion.h" 28#include "onion.h"
29 29
30#include <stdlib.h>
31#include <string.h>
32
30#include "util.h" 33#include "util.h"
31 34
32#define RETURN_1 ONION_RETURN_1 35#define RETURN_1 ONION_RETURN_1
diff --git a/toxcore/onion_announce.c b/toxcore/onion_announce.c
index 8e49f7bd..5aec3492 100644
--- a/toxcore/onion_announce.c
+++ b/toxcore/onion_announce.c
@@ -27,6 +27,9 @@
27 27
28#include "onion_announce.h" 28#include "onion_announce.h"
29 29
30#include <stdlib.h>
31#include <string.h>
32
30#include "LAN_discovery.h" 33#include "LAN_discovery.h"
31#include "util.h" 34#include "util.h"
32 35
diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c
index c4eb3fc1..3ee3e0ca 100644
--- a/toxcore/onion_client.c
+++ b/toxcore/onion_client.c
@@ -28,6 +28,9 @@
28 28
29#include "onion_client.h" 29#include "onion_client.h"
30 30
31#include <stdlib.h>
32#include <string.h>
33
31#include "LAN_discovery.h" 34#include "LAN_discovery.h"
32#include "util.h" 35#include "util.h"
33 36
diff --git a/toxcore/ping.c b/toxcore/ping.c
index bede07a1..cc77f2b1 100644
--- a/toxcore/ping.c
+++ b/toxcore/ping.c
@@ -29,13 +29,14 @@
29 29
30#include "ping.h" 30#include "ping.h"
31 31
32#include <stdlib.h>
33#include <string.h>
34
32#include "DHT.h" 35#include "DHT.h"
33#include "network.h" 36#include "network.h"
34#include "ping_array.h" 37#include "ping_array.h"
35#include "util.h" 38#include "util.h"
36 39
37#include <stdint.h>
38
39#define PING_NUM_MAX 512 40#define PING_NUM_MAX 512
40 41
41/* Maximum newly announced nodes to ping per TIME_TO_PING seconds. */ 42/* Maximum newly announced nodes to ping per TIME_TO_PING seconds. */
diff --git a/toxcore/ping_array.c b/toxcore/ping_array.c
index a54ebfe2..bcab9fe2 100644
--- a/toxcore/ping_array.c
+++ b/toxcore/ping_array.c
@@ -27,6 +27,9 @@
27 27
28#include "ping_array.h" 28#include "ping_array.h"
29 29
30#include <stdlib.h>
31#include <string.h>
32
30#include "crypto_core.h" 33#include "crypto_core.h"
31#include "util.h" 34#include "util.h"
32 35
diff --git a/toxcore/tox.c b/toxcore/tox.c
index 3db2bd35..6b50c5e8 100644
--- a/toxcore/tox.c
+++ b/toxcore/tox.c
@@ -31,6 +31,8 @@
31typedef struct Messenger Tox; 31typedef struct Messenger Tox;
32#include "tox.h" 32#include "tox.h"
33 33
34#include <string.h>
35
34#include "Messenger.h" 36#include "Messenger.h"
35#include "group.h" 37#include "group.h"
36#include "logger.h" 38#include "logger.h"
diff --git a/toxcore/util.c b/toxcore/util.c
index b7a8fda6..082e453f 100644
--- a/toxcore/util.c
+++ b/toxcore/util.c
@@ -36,6 +36,7 @@
36#include "crypto_core.h" /* for CRYPTO_PUBLIC_KEY_SIZE */ 36#include "crypto_core.h" /* for CRYPTO_PUBLIC_KEY_SIZE */
37#include "network.h" /* for current_time_monotonic */ 37#include "network.h" /* for current_time_monotonic */
38 38
39#include <string.h>
39#include <time.h> 40#include <time.h>
40 41
41 42
diff --git a/toxencryptsave/toxencryptsave.c b/toxencryptsave/toxencryptsave.c
index b83d6f1c..c1300348 100644
--- a/toxencryptsave/toxencryptsave.c
+++ b/toxencryptsave/toxencryptsave.c
@@ -40,6 +40,7 @@
40#include <sodium.h> 40#include <sodium.h>
41#endif 41#endif
42 42
43#include <stdlib.h>
43#include <string.h> 44#include <string.h>
44 45
45#if TOX_PASS_SALT_LENGTH != crypto_pwhash_scryptsalsa208sha256_SALTBYTES 46#if TOX_PASS_SALT_LENGTH != crypto_pwhash_scryptsalsa208sha256_SALTBYTES