diff --git a/appveyor.yml b/appveyor.yml index 91f727f7..c921d761 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -10,7 +10,7 @@ install: - cd third_party # libsodium - mkdir libsodium && cd libsodium -- 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 +- 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 - unzip %APPDATA%\downloads\libsodium.zip - cd .. # 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) TCP_HANDSHAKE_PLAIN_SIZE, handshake + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE); ck_assert_msg(ret == TCP_CLIENT_HANDSHAKE_SIZE - (CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE), "Encrypt failed."); - ck_assert_msg(send(sock, (const char *)handshake, TCP_CLIENT_HANDSHAKE_SIZE - 1, 0) == TCP_CLIENT_HANDSHAKE_SIZE - 1, + ck_assert_msg(net_send(sock, handshake, TCP_CLIENT_HANDSHAKE_SIZE - 1) == TCP_CLIENT_HANDSHAKE_SIZE - 1, "send Failed."); c_sleep(50); do_TCP_server(tcp_s); c_sleep(50); do_TCP_server(tcp_s); c_sleep(50); - ck_assert_msg(send(sock, (const char *)(handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1)), 1, 0) == 1, "send Failed."); + ck_assert_msg(net_send(sock, handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1), 1) == 1, "send Failed."); c_sleep(50); do_TCP_server(tcp_s); c_sleep(50); @@ -98,7 +98,7 @@ START_TEST(test_basic) c_sleep(50); uint8_t response[TCP_SERVER_HANDSHAKE_SIZE]; uint8_t response_plain[TCP_HANDSHAKE_PLAIN_SIZE]; - ck_assert_msg(recv(sock, (char *)response, TCP_SERVER_HANDSHAKE_SIZE, 0) == TCP_SERVER_HANDSHAKE_SIZE, "recv Failed."); + ck_assert_msg(net_recv(sock, response, TCP_SERVER_HANDSHAKE_SIZE) == TCP_SERVER_HANDSHAKE_SIZE, "recv Failed."); ret = decrypt_data(self_public_key, f_secret_key, response, response + CRYPTO_NONCE_SIZE, TCP_SERVER_HANDSHAKE_SIZE - CRYPTO_NONCE_SIZE, response_plain); ck_assert_msg(ret == TCP_HANDSHAKE_PLAIN_SIZE, "Decrypt Failed."); @@ -118,8 +118,10 @@ START_TEST(test_basic) uint32_t i; for (i = 0; i < sizeof(r_req); ++i) { - ck_assert_msg(send(sock, (const char *)(r_req + i), 1, 0) == 1, "send Failed."); - //ck_assert_msg(send(sock, (const char *)r_req, sizeof(r_req), 0) == sizeof(r_req), "send Failed."); + ck_assert_msg(net_send(sock, r_req + i, 1) == 1, "send Failed."); +#if 0 + ck_assert_msg(net_send(sock, r_req, sizeof(r_req)) == sizeof(r_req), "send Failed."); +#endif do_TCP_server(tcp_s); c_sleep(50); } @@ -127,7 +129,7 @@ START_TEST(test_basic) do_TCP_server(tcp_s); c_sleep(50); uint8_t packet_resp[4096]; - int recv_data_len = recv(sock, (char *)packet_resp, 2 + 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE, 0); + int recv_data_len = net_recv(sock, packet_resp, 2 + 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE); ck_assert_msg(recv_data_len == 2 + 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE, "recv Failed. %u", recv_data_len); memcpy(&size, packet_resp, 2); @@ -180,16 +182,16 @@ static struct sec_TCP_con *new_TCP_con(TCP_Server *tcp_s) TCP_HANDSHAKE_PLAIN_SIZE, handshake + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE); ck_assert_msg(ret == TCP_CLIENT_HANDSHAKE_SIZE - (CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE), "Encrypt failed."); - ck_assert_msg(send(sock, (const char *)handshake, TCP_CLIENT_HANDSHAKE_SIZE - 1, 0) == TCP_CLIENT_HANDSHAKE_SIZE - 1, + ck_assert_msg(net_send(sock, handshake, TCP_CLIENT_HANDSHAKE_SIZE - 1) == TCP_CLIENT_HANDSHAKE_SIZE - 1, "send Failed."); do_TCP_server(tcp_s); c_sleep(50); - ck_assert_msg(send(sock, (const char *)(handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1)), 1, 0) == 1, "send Failed."); + ck_assert_msg(net_send(sock, handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1), 1) == 1, "send Failed."); c_sleep(50); do_TCP_server(tcp_s); uint8_t response[TCP_SERVER_HANDSHAKE_SIZE]; uint8_t response_plain[TCP_HANDSHAKE_PLAIN_SIZE]; - ck_assert_msg(recv(sock, (char *)response, TCP_SERVER_HANDSHAKE_SIZE, 0) == TCP_SERVER_HANDSHAKE_SIZE, "recv Failed."); + ck_assert_msg(net_recv(sock, response, TCP_SERVER_HANDSHAKE_SIZE) == TCP_SERVER_HANDSHAKE_SIZE, "recv Failed."); ret = decrypt_data(tcp_server_public_key(tcp_s), f_secret_key, response, response + CRYPTO_NONCE_SIZE, TCP_SERVER_HANDSHAKE_SIZE - CRYPTO_NONCE_SIZE, response_plain); 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 * increment_nonce(con->sent_nonce); - ck_assert_msg(send(con->sock, (const char *)packet, SIZEOF_VLA(packet), 0) == SIZEOF_VLA(packet), "send failed"); + ck_assert_msg(net_send(con->sock, packet, SIZEOF_VLA(packet)) == SIZEOF_VLA(packet), "send failed"); return 0; } static int read_packet_sec_TCP(struct sec_TCP_con *con, uint8_t *data, uint16_t length) { - int len = recv(con->sock, (char *)data, length, 0); + int len = net_recv(con->sock, data, length); ck_assert_msg(len == length, "wrong len %i\n", len); len = decrypt_data_symmetric(con->shared_key, con->recv_nonce, data + 2, length - 2, data); 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[]) // toxcore/TCP_server CHECK_SIZE(TCP_Priority_List, 16); CHECK_SIZE(TCP_Secure_Connection, 11816); +#ifdef TCP_SERVER_USE_EPOLL + CHECK_SIZE(TCP_Server, 6049968); // 6MB! +#else CHECK_SIZE(TCP_Server, 6049952); // 6MB! +#endif // toxcore/tox CHECK_SIZE(Tox_Options, 64); #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 @@ */ #include "bootstrap_node_packets.h" +#include + #define INFO_REQUEST_PACKET_LENGTH 78 static 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 @@ #include "audio.h" +#include +#include + #include "rtp.h" #include "../toxcore/logger.h" -#include - static struct JitterBuffer *jbuf_new(uint32_t capacity); static void jbuf_clear(struct JitterBuffer *q); static 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 @@ #include "bwcontroller.h" +#include +#include +#include +#include + #include "ring_buffer.h" #include "../toxcore/logger.h" #include "../toxcore/util.h" -#include -#include - #define BWC_PACKET_ID 196 #define BWC_SEND_INTERVAL_MS 950 /* 0.95s */ #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 @@ #include "groupav.h" +#include +#include + #include "../toxcore/logger.h" #include "../toxcore/util.h" 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 @@ #include "rtp.h" +#include +#include +#include +#include + #include "bwcontroller.h" #include "../toxcore/Messenger.h" #include "../toxcore/logger.h" #include "../toxcore/util.h" -#include -#include -#include - enum { /** * 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 @@ #include "video.h" +#include +#include +#include + #include "msi.h" #include "ring_buffer.h" #include "rtp.h" @@ -30,9 +34,6 @@ #include "../toxcore/logger.h" #include "../toxcore/network.h" -#include -#include - /** * Soft deadline the decoder should attempt to meet, in "us" (microseconds). * 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( "TCP_connection.c", "TCP_server.c", ], + copts = select({ + "//tools/config:freebsd": [], + "//tools/config:linux": ["-DTCP_SERVER_USE_EPOLL=1"], + "//tools/config:osx": [], + }), hdrs = [ "TCP_client.h", "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 @@ */ /* - * Copyright © 2016-2017 The TokTok team. + * Copyright © 2016-2018 The TokTok team. * Copyright © 2013 Tox project. * * This file is part of Tox, the free peer to peer instant messenger. @@ -34,6 +34,8 @@ #include "util.h" #include +#include +#include /* The timeout after which a node is discarded completely. */ #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 @@ #include "LAN_discovery.h" +#include + #include "util.h" #define MAX_INTERFACES 16 @@ -37,7 +39,15 @@ static int broadcast_count = -1; static IP_Port broadcast_ip_ports[MAX_INTERFACES]; -#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) +#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) + +// The mingw32/64 Windows library warns about including winsock2.h after +// windows.h even though with the above it's a valid thing to do. So, to make +// mingw32 headers happy, we include winsock2.h first. +#include + +#include +#include #include @@ -108,6 +118,12 @@ static void fetch_broadcast_info(uint16_t port) #elif defined(__linux__) || defined(__FreeBSD__) +#include +#include +#include +#include +#include + #ifdef __linux__ #include #endif @@ -116,8 +132,6 @@ static void fetch_broadcast_info(uint16_t port) #include #endif -#include - static void fetch_broadcast_info(uint16_t port) { /* Not sure how many platforms this will run on, @@ -127,7 +141,7 @@ static void fetch_broadcast_info(uint16_t port) broadcast_count = 0; const Socket sock = net_socket(TOX_AF_INET, TOX_SOCK_STREAM, 0); - if (sock < 0) { + if (!sock_valid(sock)) { return; } @@ -139,8 +153,8 @@ static void fetch_broadcast_info(uint16_t port) ifconf.ifc_buf = (char *)i_faces; ifconf.ifc_len = sizeof(i_faces); - if (ioctl(sock, SIOCGIFCONF, &ifconf) < 0) { - close(sock); + if (ioctl(sock.socket, SIOCGIFCONF, &ifconf) < 0) { + kill_sock(sock); return; } @@ -160,12 +174,12 @@ static void fetch_broadcast_info(uint16_t port) for (int i = 0; i < n; i++) { /* there are interfaces with are incapable of broadcast */ - if (ioctl(sock, SIOCGIFBRDADDR, &i_faces[i]) < 0) { + if (ioctl(sock.socket, SIOCGIFBRDADDR, &i_faces[i]) < 0) { continue; } - /* moot check: only TOX_AF_INET returned (backwards compat.) */ - if (i_faces[i].ifr_broadaddr.sa_family != TOX_AF_INET) { + /* moot check: only AF_INET returned (backwards compat.) */ + if (i_faces[i].ifr_broadaddr.sa_family != AF_INET) { continue; } @@ -187,7 +201,7 @@ static void fetch_broadcast_info(uint16_t port) count++; } - close(sock); + kill_sock(sock); broadcast_count = count; 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 @@ #include "Messenger.h" +#include +#include +#include +#include +#include + #include "logger.h" #include "network.h" #include "util.h" -#include - static int write_cryptpacket_id(const Messenger *m, int32_t friendnumber, uint8_t packet_id, const uint8_t *data, uint32_t length, uint8_t congestion_control); 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 @@ #include "TCP_client.h" -#include "util.h" +#include +#include +#include -#if !defined(_WIN32) && !defined(__WIN32__) && !defined (WIN32) -#include -#endif +#include "util.h" struct TCP_Client_Connection { TCP_CLIENT_STATUS status; @@ -177,7 +177,7 @@ static int proxy_http_read_connection_response(TCP_Client_Connection *TCP_conn) if (strstr((char *)data, success)) { // drain all data - unsigned int data_left = TCP_socket_data_recv_buffer(TCP_conn->sock); + unsigned int data_left = net_socket_data_recv_buffer(TCP_conn->sock); if (data_left) { VLA(uint8_t, temp_data, data_left); @@ -332,9 +332,8 @@ static int client_send_pending_data_nonpriority(TCP_Client_Connection *con) return 0; } - uint16_t left = con->last_packet_length - con->last_packet_sent; - const char *data = (const char *)(con->last_packet + con->last_packet_sent); - int len = send(con->sock, data, left, MSG_NOSIGNAL); + const uint16_t left = con->last_packet_length - con->last_packet_sent; + const int len = net_send(con->sock, con->last_packet + con->last_packet_sent, left); if (len <= 0) { return -1; @@ -363,8 +362,8 @@ static int client_send_pending_data(TCP_Client_Connection *con) TCP_Priority_List *p = con->priority_queue_start; while (p) { - uint16_t left = p->size - p->sent; - int len = send(con->sock, (const char *)(p->data + p->sent), left, MSG_NOSIGNAL); + const uint16_t left = p->size - p->sent; + const int len = net_send(con->sock, p->data + p->sent, left); if (len != left) { if (len > 0) { @@ -459,7 +458,7 @@ static int write_packet_TCP_client_secure_connection(TCP_Client_Connection *con, } if (priority) { - len = sendpriority ? send(con->sock, (const char *)packet, SIZEOF_VLA(packet), MSG_NOSIGNAL) : 0; + len = sendpriority ? net_send(con->sock, packet, SIZEOF_VLA(packet)) : 0; if (len <= 0) { len = 0; @@ -474,7 +473,7 @@ static int write_packet_TCP_client_secure_connection(TCP_Client_Connection *con, return client_add_priority(con, packet, SIZEOF_VLA(packet), len); } - len = send(con->sock, (const char *)packet, SIZEOF_VLA(packet), MSG_NOSIGNAL); + len = net_send(con->sock, packet, SIZEOF_VLA(packet)); if (len <= 0) { 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 @@ #include "TCP_connection.h" -#include "util.h" - #include +#include +#include + +#include "util.h" struct 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 @@ #include "TCP_server.h" -#include "util.h" - +#include +#include +#include #if !defined(_WIN32) && !defined(__WIN32__) && !defined (WIN32) #include #endif +#ifdef TCP_SERVER_USE_EPOLL +#include +#include +#endif + +#include "util.h" + typedef struct TCP_Secure_Connection { Socket sock; uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; @@ -231,22 +239,6 @@ static int del_accepted(TCP_Server *TCP_server, int index) return 0; } -/* return the amount of data in the tcp recv buffer. - * return 0 on failure. - */ -unsigned int TCP_socket_data_recv_buffer(Socket sock) -{ -#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) - unsigned long count = 0; - ioctlsocket(sock, FIONREAD, &count); -#else - int count = 0; - ioctl(sock, FIONREAD, &count); -#endif - - return count; -} - /* Read the next two bytes in TCP stream then convert them to * length (host byte order). * @@ -256,11 +248,11 @@ unsigned int TCP_socket_data_recv_buffer(Socket sock) */ uint16_t read_TCP_length(Socket sock) { - unsigned int count = TCP_socket_data_recv_buffer(sock); + const unsigned int count = net_socket_data_recv_buffer(sock); if (count >= sizeof(uint16_t)) { uint16_t length; - int len = recv(sock, (char *)&length, sizeof(uint16_t), MSG_NOSIGNAL); + const int len = net_recv(sock, &length, sizeof(uint16_t)); if (len != sizeof(uint16_t)) { fprintf(stderr, "FAIL recv packet\n"); @@ -286,10 +278,10 @@ uint16_t read_TCP_length(Socket sock) */ int read_TCP_packet(Socket sock, uint8_t *data, uint16_t length) { - unsigned int count = TCP_socket_data_recv_buffer(sock); + unsigned int count = net_socket_data_recv_buffer(sock); if (count >= length) { - int len = recv(sock, (char *)data, length, MSG_NOSIGNAL); + const int len = net_recv(sock, data, length); if (len != length) { fprintf(stderr, "FAIL recv packet\n"); @@ -356,8 +348,8 @@ static int send_pending_data_nonpriority(TCP_Secure_Connection *con) return 0; } - uint16_t left = con->last_packet_length - con->last_packet_sent; - int len = send(con->sock, (const char *)(con->last_packet + con->last_packet_sent), left, MSG_NOSIGNAL); + const uint16_t left = con->last_packet_length - con->last_packet_sent; + const int len = net_send(con->sock, con->last_packet + con->last_packet_sent, left); if (len <= 0) { return -1; @@ -386,8 +378,8 @@ static int send_pending_data(TCP_Secure_Connection *con) TCP_Priority_List *p = con->priority_queue_start; while (p) { - uint16_t left = p->size - p->sent; - int len = send(con->sock, (const char *)(p->data + p->sent), left, MSG_NOSIGNAL); + const uint16_t left = p->size - p->sent; + const int len = net_send(con->sock, p->data + p->sent, left); if (len != left) { if (len > 0) { @@ -462,7 +454,7 @@ static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const VLA(uint8_t, packet, sizeof(uint16_t) + length + CRYPTO_MAC_SIZE); - uint16_t c_length = net_htons(length + CRYPTO_MAC_SIZE); + const uint16_t c_length = net_htons(length + CRYPTO_MAC_SIZE); memcpy(packet, &c_length, sizeof(uint16_t)); int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t)); @@ -471,7 +463,7 @@ static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const } if (priority) { - len = sendpriority ? send(con->sock, (const char *)packet, SIZEOF_VLA(packet), MSG_NOSIGNAL) : 0; + len = sendpriority ? net_send(con->sock, packet, SIZEOF_VLA(packet)) : 0; if (len <= 0) { len = 0; @@ -486,7 +478,7 @@ static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const return add_priority(con, packet, SIZEOF_VLA(packet), len); } - len = send(con->sock, (const char *)packet, SIZEOF_VLA(packet), MSG_NOSIGNAL); + len = net_send(con->sock, packet, SIZEOF_VLA(packet)); if (len <= 0) { return 0; @@ -583,7 +575,7 @@ static int handle_TCP_handshake(TCP_Secure_Connection *con, const uint8_t *data, return -1; } - if (TCP_SERVER_HANDSHAKE_SIZE != send(con->sock, (const char *)response, TCP_SERVER_HANDSHAKE_SIZE, MSG_NOSIGNAL)) { + if (TCP_SERVER_HANDSHAKE_SIZE != net_send(con->sock, response, TCP_SERVER_HANDSHAKE_SIZE)) { return -1; } @@ -1005,12 +997,12 @@ static int accept_connection(TCP_Server *TCP_server, Socket sock) return index; } -static Socket new_listening_TCP_socket(int family, uint16_t port) +static Socket new_listening_TCP_socket(Family family, uint16_t port) { Socket sock = net_socket(family, TOX_SOCK_STREAM, TOX_PROTO_TCP); if (!sock_valid(sock)) { - return ~0; + return net_invalid_socket; } int ok = set_socket_nonblock(sock); @@ -1023,11 +1015,11 @@ static Socket new_listening_TCP_socket(int family, uint16_t port) ok = set_socket_reuseaddr(sock); } - ok = ok && bind_to_port(sock, family, port) && (listen(sock, TCP_MAX_BACKLOG) == 0); + ok = ok && bind_to_port(sock, family, port) && (net_listen(sock, TCP_MAX_BACKLOG) == 0); if (!ok) { kill_sock(sock); - return ~0; + return net_invalid_socket; } return sock; @@ -1087,9 +1079,9 @@ TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, const uin if (sock_valid(sock)) { #ifdef TCP_SERVER_USE_EPOLL ev.events = EPOLLIN | EPOLLET; - ev.data.u64 = sock | ((uint64_t)TCP_SOCKET_LISTENING << 32); + ev.data.u64 = sock.socket | ((uint64_t)TCP_SOCKET_LISTENING << 32); - if (epoll_ctl(temp->efd, EPOLL_CTL_ADD, sock, &ev) == -1) { + if (epoll_ctl(temp->efd, EPOLL_CTL_ADD, sock.socket, &ev) == -1) { continue; } @@ -1119,20 +1111,20 @@ TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, const uin return temp; } +#ifndef TCP_SERVER_USE_EPOLL static void do_TCP_accept_new(TCP_Server *TCP_server) { uint32_t i; for (i = 0; i < TCP_server->num_listening_socks; ++i) { - struct sockaddr_storage addr; - socklen_t addrlen = sizeof(addr); Socket sock; do { - sock = accept(TCP_server->socks_listening[i], (struct sockaddr *)&addr, &addrlen); + sock = net_accept(TCP_server->socks_listening[i]); } while (accept_connection(TCP_server, sock) != -1); } } +#endif static int do_incoming(TCP_Server *TCP_server, uint32_t i) { @@ -1208,6 +1200,7 @@ static void do_confirmed_recv(TCP_Server *TCP_server, uint32_t i) } } +#ifndef TCP_SERVER_USE_EPOLL static void do_TCP_incoming(TCP_Server *TCP_server) { uint32_t i; @@ -1225,6 +1218,7 @@ static void do_TCP_unconfirmed(TCP_Server *TCP_server) do_unconfirmed(TCP_server, i); } } +#endif static void do_TCP_confirmed(TCP_Server *TCP_server) { @@ -1294,8 +1288,9 @@ static void do_TCP_epoll(TCP_Server *TCP_server) int n; for (n = 0; n < nfds; ++n) { - Socket sock = events[n].data.u64 & 0xFFFFFFFF; - int status = (events[n].data.u64 >> 32) & 0xFF, index = (events[n].data.u64 >> 40); + const Socket sock = {(int)(events[n].data.u64 & 0xFFFFFFFF)}; + const int status = (events[n].data.u64 >> 32) & 0xFF; + const int index = events[n].data.u64 >> 40; if ((events[n].events & EPOLLERR) || (events[n].events & EPOLLHUP) || (events[n].events & EPOLLRDHUP)) { switch (status) { @@ -1331,11 +1326,8 @@ static void do_TCP_epoll(TCP_Server *TCP_server) switch (status) { case TCP_SOCKET_LISTENING: { //socket is from socks_listening, accept connection - struct sockaddr_storage addr; - socklen_t addrlen = sizeof(addr); - while (1) { - Socket sock_new = accept(sock, (struct sockaddr *)&addr, &addrlen); + Socket sock_new = net_accept(sock); if (!sock_valid(sock_new)) { break; @@ -1349,10 +1341,10 @@ static void do_TCP_epoll(TCP_Server *TCP_server) struct epoll_event ev = { .events = EPOLLIN | EPOLLET | EPOLLRDHUP, - .data.u64 = sock_new | ((uint64_t)TCP_SOCKET_INCOMING << 32) | ((uint64_t)index_new << 40) + .data.u64 = sock_new.socket | ((uint64_t)TCP_SOCKET_INCOMING << 32) | ((uint64_t)index_new << 40) }; - if (epoll_ctl(TCP_server->efd, EPOLL_CTL_ADD, sock_new, &ev) == -1) { + if (epoll_ctl(TCP_server->efd, EPOLL_CTL_ADD, sock_new.socket, &ev) == -1) { kill_TCP_secure_connection(&TCP_server->incoming_connection_queue[index_new]); continue; } @@ -1366,9 +1358,9 @@ static void do_TCP_epoll(TCP_Server *TCP_server) if ((index_new = do_incoming(TCP_server, index)) != -1) { events[n].events = EPOLLIN | EPOLLET | EPOLLRDHUP; - events[n].data.u64 = sock | ((uint64_t)TCP_SOCKET_UNCONFIRMED << 32) | ((uint64_t)index_new << 40); + events[n].data.u64 = sock.socket | ((uint64_t)TCP_SOCKET_UNCONFIRMED << 32) | ((uint64_t)index_new << 40); - if (epoll_ctl(TCP_server->efd, EPOLL_CTL_MOD, sock, &events[n]) == -1) { + if (epoll_ctl(TCP_server->efd, EPOLL_CTL_MOD, sock.socket, &events[n]) == -1) { kill_TCP_secure_connection(&TCP_server->unconfirmed_connection_queue[index_new]); break; } @@ -1382,9 +1374,9 @@ static void do_TCP_epoll(TCP_Server *TCP_server) if ((index_new = do_unconfirmed(TCP_server, index)) != -1) { events[n].events = EPOLLIN | EPOLLET | EPOLLRDHUP; - events[n].data.u64 = sock | ((uint64_t)TCP_SOCKET_CONFIRMED << 32) | ((uint64_t)index_new << 40); + events[n].data.u64 = sock.socket | ((uint64_t)TCP_SOCKET_CONFIRMED << 32) | ((uint64_t)index_new << 40); - if (epoll_ctl(TCP_server->efd, EPOLL_CTL_MOD, sock, &events[n]) == -1) { + if (epoll_ctl(TCP_server->efd, EPOLL_CTL_MOD, sock.socket, &events[n]) == -1) { //remove from confirmed connections kill_accepted(TCP_server, index_new); 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 @@ #include #endif -// Disable MSG_NOSIGNAL on systems not supporting it, e.g. Windows, FreeBSD -#if !defined(MSG_NOSIGNAL) -#define MSG_NOSIGNAL 0 -#endif - #define MAX_INCOMING_CONNECTIONS 256 #define TCP_MAX_BACKLOG MAX_INCOMING_CONNECTIONS @@ -108,11 +103,6 @@ void do_TCP_server(TCP_Server *TCP_server); */ void kill_TCP_server(TCP_Server *TCP_server); -/* return the amount of data in the tcp recv buffer. - * return 0 on failure. - */ -unsigned int TCP_socket_data_recv_buffer(Socket sock); - /* Read the next two bytes in TCP stream then convert them to * length (host byte order). * 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 @@ #define CRYPTO_CORE_H #include +#include #include -#include #ifdef __cplusplus extern "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 @@ #define CRYPTO_CORE_H #include +#include #include -#include #ifdef __cplusplus extern "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 @@ #include "friend_connection.h" +#include +#include +#include + #include "util.h" #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 @@ #include "friend_requests.h" +#include +#include + #include "util.h" struct 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 @@ #include "group.h" +#include +#include + #include "util.h" /* 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 @@ #include "list.h" +#include +#include + #include "ccompat.h" /* 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 @@ #define LIST_H #include -#include -#include typedef struct { 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 @@ #include "net_crypto.h" -#include "util.h" - #include +#include +#include + +#include "util.h" typedef struct { 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 @@ #define _WIN32_WINNT 0x501 #endif +#if !defined(OS_WIN32) && (defined(_WIN32) || defined(__WIN32__) || defined(WIN32)) +#define OS_WIN32 +#endif + +#ifdef OS_WIN32 +#ifndef WINVER +//Windows XP +#define WINVER 0x0501 +#endif +#endif + +#ifdef PLAN9 +#include // Plan 9 requires this is imported first +// Comment line here to avoid reordering by source code formatters. +#include +#endif + +#ifdef OS_WIN32 /* Put win32 includes here */ +// The mingw32/64 Windows library warns about including winsock2.h after +// windows.h even though with the above it's a valid thing to do. So, to make +// mingw32 headers happy, we include winsock2.h first. +#include +// Comment line here to avoid reordering by source code formatters. +#include +#include +#endif + #include "network.h" -#include "logger.h" -#include "util.h" - -#include #ifdef __APPLE__ #include #include #endif -#ifndef IPV6_ADD_MEMBERSHIP -#ifdef IPV6_JOIN_GROUP -#define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP -#endif -#endif - -#if !(defined(_WIN32) || defined(__WIN32__) || defined(WIN32)) +#if !defined(OS_WIN32) +#include #include #include #include +#include +#include +#include #include #include +#include #define TOX_EWOULDBLOCK EWOULDBLOCK #else - #ifndef IPV6_V6ONLY #define IPV6_V6ONLY 27 #endif #define TOX_EWOULDBLOCK WSAEWOULDBLOCK -static const char *inet_ntop(Family family, const void *addr, char *buf, size_t bufsize) +static const char *inet_ntop(int family, const void *addr, char *buf, size_t bufsize) { - if (family == TOX_AF_INET) { + if (family == AF_INET) { struct sockaddr_in saddr; memset(&saddr, 0, sizeof(saddr)); @@ -89,7 +110,7 @@ static const char *inet_ntop(Family family, const void *addr, char *buf, size_t } return buf; - } else if (family == TOX_AF_INET6) { + } else if (family == AF_INET6) { struct sockaddr_in6 saddr; memset(&saddr, 0, sizeof(saddr)); @@ -108,9 +129,9 @@ static const char *inet_ntop(Family family, const void *addr, char *buf, size_t return nullptr; } -static int inet_pton(Family family, const char *addrString, void *addrbuf) +static int inet_pton(int family, const char *addrString, void *addrbuf) { - if (family == TOX_AF_INET) { + if (family == AF_INET) { struct sockaddr_in saddr; memset(&saddr, 0, sizeof(saddr)); @@ -123,7 +144,7 @@ static int inet_pton(Family family, const char *addrString, void *addrbuf) *(struct in_addr *)addrbuf = saddr.sin_addr; return 1; - } else if (family == TOX_AF_INET6) { + } else if (family == AF_INET6) { struct sockaddr_in6 saddr; memset(&saddr, 0, sizeof(saddr)); @@ -143,6 +164,25 @@ static int inet_pton(Family family, const char *addrString, void *addrbuf) #endif +#include +#include +#include +#include + +#include "logger.h" +#include "util.h" + +// Disable MSG_NOSIGNAL on systems not supporting it, e.g. Windows, FreeBSD +#if !defined(MSG_NOSIGNAL) +#define MSG_NOSIGNAL 0 +#endif + +#ifndef IPV6_ADD_MEMBERSHIP +#ifdef IPV6_JOIN_GROUP +#define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP +#endif +#endif + #if TOX_INET6_ADDRSTRLEN < INET6_ADDRSTRLEN #error TOX_INET6_ADDRSTRLEN should be greater or equal to INET6_ADDRSTRLEN (#INET6_ADDRSTRLEN) #endif @@ -153,8 +193,40 @@ static int inet_pton(Family family, const char *addrString, void *addrbuf) static int make_proto(int proto); static int make_socktype(int type); -static int make_family(int tox_family); -static int make_tox_family(int family); + +static int make_family(Family tox_family) +{ + switch (tox_family) { + case TOX_AF_INET: + return AF_INET; + + case TOX_AF_INET6: + return AF_INET6; + + case TOX_AF_UNSPEC: + return AF_UNSPEC; + + default: + return tox_family; + } +} + +static Family make_tox_family(int family) +{ + switch (family) { + case AF_INET: + return TOX_AF_INET; + + case AF_INET6: + return TOX_AF_INET6; + + case AF_UNSPEC: + return TOX_AF_UNSPEC; + + default: + return family; + } +} static void get_ip4(IP4 *result, const struct in_addr *addr) { @@ -201,6 +273,14 @@ IP6 get_ip6_loopback(void) return loopback; } +const Socket net_invalid_socket = { +#ifdef OS_WIN32 + (int)INVALID_SOCKET, +#else + -1, +#endif +}; + /* Check if socket is valid. * * return 1 if valid @@ -208,27 +288,17 @@ IP6 get_ip6_loopback(void) */ int sock_valid(Socket sock) { -#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) - - if (sock == INVALID_SOCKET) { -#else - - if (sock < 0) { -#endif - return 0; - } - - return 1; + return sock.socket != net_invalid_socket.socket; } /* Close the socket. */ void kill_sock(Socket sock) { -#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) - closesocket(sock); +#ifdef OS_WIN32 + closesocket(sock.socket); #else - close(sock); + close(sock.socket); #endif } @@ -239,11 +309,11 @@ void kill_sock(Socket sock) */ int set_socket_nonblock(Socket sock) { -#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) +#ifdef OS_WIN32 u_long mode = 1; - return (ioctlsocket(sock, FIONBIO, &mode) == 0); + return (ioctlsocket(sock.socket, FIONBIO, &mode) == 0); #else - return (fcntl(sock, F_SETFL, O_NONBLOCK, 1) == 0); + return (fcntl(sock.socket, F_SETFL, O_NONBLOCK, 1) == 0); #endif } @@ -256,7 +326,7 @@ int set_socket_nosigpipe(Socket sock) { #if defined(__MACH__) int set = 1; - return (setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, (const char *)&set, sizeof(int)) == 0); + return setsockopt(sock.socket, SOL_SOCKET, SO_NOSIGPIPE, (const char *)&set, sizeof(int)) == 0; #else return 1; #endif @@ -270,7 +340,7 @@ int set_socket_nosigpipe(Socket sock) int set_socket_reuseaddr(Socket sock) { int set = 1; - return (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char *)&set, sizeof(set)) == 0); + return setsockopt(sock.socket, SOL_SOCKET, SO_REUSEADDR, (const char *)&set, sizeof(set)) == 0; } /* Set socket to dual (IPv4 + IPv6 socket) @@ -282,14 +352,14 @@ int set_socket_dualstack(Socket sock) { int ipv6only = 0; socklen_t optsize = sizeof(ipv6only); - int res = getsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&ipv6only, &optsize); + int res = getsockopt(sock.socket, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&ipv6only, &optsize); if ((res == 0) && (ipv6only == 0)) { return 1; } ipv6only = 0; - return (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (const char *)&ipv6only, sizeof(ipv6only)) == 0); + return setsockopt(sock.socket, IPPROTO_IPV6, IPV6_V6ONLY, (const char *)&ipv6only, sizeof(ipv6only)) == 0; } @@ -297,7 +367,7 @@ int set_socket_dualstack(Socket sock) static uint64_t current_time_actual(void) { uint64_t time; -#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) +#ifdef OS_WIN32 /* This probably works fine */ FILETIME ft; GetSystemTimeAsFileTime(&ft); @@ -315,7 +385,7 @@ static uint64_t current_time_actual(void) } -#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) +#ifdef OS_WIN32 static uint64_t last_monotime; static uint64_t add_monotime; #endif @@ -324,7 +394,7 @@ static uint64_t add_monotime; uint64_t current_time_monotonic(void) { uint64_t time; -#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) +#ifdef OS_WIN32 uint64_t old_add_monotime = add_monotime; time = (uint64_t)GetTickCount() + add_monotime; @@ -472,11 +542,11 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint1 addr6->sin6_flowinfo = 0; addr6->sin6_scope_id = 0; } else { - /* unknown address type*/ + LOGGER_WARNING(net->log, "unknown address type: %d", ip_port.ip.family); return -1; } - const int res = sendto(net->sock, (const char *) data, length, 0, (struct sockaddr *)&addr, addrsize); + const int res = sendto(net->sock.socket, (const char *) data, length, 0, (struct sockaddr *)&addr, addrsize); loglogdata(net->log, "O=>", data, length, ip_port, res); @@ -492,13 +562,13 @@ static int receivepacket(Logger *log, Socket sock, IP_Port *ip_port, uint8_t *da { memset(ip_port, 0, sizeof(IP_Port)); struct sockaddr_storage addr; -#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) +#ifdef OS_WIN32 int addrlen = sizeof(addr); #else socklen_t addrlen = sizeof(addr); #endif *length = 0; - int fail_or_len = recvfrom(sock, (char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen); + int fail_or_len = recvfrom(sock.socket, (char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen); if (fail_or_len < 0) { int error = net_error(); @@ -597,7 +667,7 @@ int networking_at_startup(void) #endif/*VANILLA_NACL*/ -#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) +#ifdef OS_WIN32 WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 2), &wsaData) != NO_ERROR) { @@ -614,7 +684,7 @@ int networking_at_startup(void) #if 0 static void at_shutdown(void) { -#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) +#ifdef OS_WIN32 WSACleanup(); #endif } @@ -703,12 +773,12 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1 /* Functions to increase the size of the send and receive UDP buffers. */ int n = 1024 * 1024 * 2; - setsockopt(temp->sock, SOL_SOCKET, SO_RCVBUF, (const char *)&n, sizeof(n)); - setsockopt(temp->sock, SOL_SOCKET, SO_SNDBUF, (const char *)&n, sizeof(n)); + setsockopt(temp->sock.socket, SOL_SOCKET, SO_RCVBUF, (const char *)&n, sizeof(n)); + setsockopt(temp->sock.socket, SOL_SOCKET, SO_SNDBUF, (const char *)&n, sizeof(n)); /* Enable broadcast on socket */ int broadcast = 1; - setsockopt(temp->sock, SOL_SOCKET, SO_BROADCAST, (const char *)&broadcast, sizeof(broadcast)); + setsockopt(temp->sock.socket, SOL_SOCKET, SO_BROADCAST, (const char *)&broadcast, sizeof(broadcast)); /* iOS UDP sockets are weird and apparently can SIGPIPE */ if (!set_socket_nosigpipe(temp->sock)) { @@ -776,7 +846,7 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1 mreq.ipv6mr_multiaddr.s6_addr[ 1] = 0x02; mreq.ipv6mr_multiaddr.s6_addr[15] = 0x01; mreq.ipv6mr_interface = 0; - int res = setsockopt(temp->sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (const char *)&mreq, sizeof(mreq)); + const int res = setsockopt(temp->sock.socket, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (const char *)&mreq, sizeof(mreq)); int neterror = net_error(); 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 int tries; for (tries = port_from; tries <= port_to; tries++) { - int res = bind(temp->sock, (struct sockaddr *)&addr, addrsize); + int res = bind(temp->sock.socket, (struct sockaddr *)&addr, addrsize); if (!res) { temp->port = *portptr; @@ -1290,7 +1360,7 @@ int net_connect(Socket sock, IP_Port ip_port) return 0; } - return connect(sock, (struct sockaddr *)&addr, addrsize); + return connect(sock.socket, (struct sockaddr *)&addr, addrsize); } int32_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) return 0; } - return (bind(sock, (struct sockaddr *)&addr, addrsize) == 0); -} - -static int make_tox_family(int family) -{ - switch (family) { - case AF_INET: - return TOX_AF_INET; - - case AF_INET6: - return TOX_AF_INET6; - - case AF_UNSPEC: - return TOX_AF_UNSPEC; - - default: - return family; - } -} - -static int make_family(int tox_family) -{ - switch (tox_family) { - case TOX_AF_INET: - return AF_INET; - - case TOX_AF_INET6: - return AF_INET6; - - case TOX_AF_UNSPEC: - return AF_UNSPEC; - - default: - return tox_family; - } + return bind(sock.socket, (struct sockaddr *)&addr, addrsize) == 0; } static int make_socktype(int type) @@ -1456,12 +1492,47 @@ static int make_proto(int proto) } } -Socket net_socket(int domain, int type, int protocol) +Socket net_socket(Family domain, int type, int protocol) { - int platform_domain = make_family(domain); - int platform_type = make_socktype(type); - int platform_prot = make_proto(protocol); - return socket(platform_domain, platform_type, platform_prot); + const int platform_domain = make_family(domain); + const int platform_type = make_socktype(type); + const int platform_prot = make_proto(protocol); + const Socket sock = {(int)socket(platform_domain, platform_type, platform_prot)}; + return sock; +} + +int net_send(Socket sock, const void *buf, size_t len) +{ + return send(sock.socket, (const char *)buf, len, MSG_NOSIGNAL); +} + +int net_recv(Socket sock, void *buf, size_t len) +{ + return recv(sock.socket, (char *)buf, len, MSG_NOSIGNAL); +} + +int net_listen(Socket sock, int backlog) +{ + return listen(sock.socket, backlog); +} + +Socket net_accept(Socket sock) +{ + const Socket newsock = {accept(sock.socket, nullptr, nullptr)}; + return newsock; +} + +size_t net_socket_data_recv_buffer(Socket sock) +{ +#ifdef OS_WIN32 + unsigned long count = 0; + ioctlsocket(sock.socket, FIONREAD, &count); +#else + int count = 0; + ioctl(sock.socket, FIONREAD, &count); +#endif + + return count; } uint32_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 @@ #ifndef NETWORK_H #define NETWORK_H -#ifdef PLAN9 -#include // Plan 9 requires this is imported first -// Comment line here to avoid reordering by source code formatters. -#include -#endif - -#include "ccompat.h" #include "logger.h" -#include -#include -#include -#include -#include -#include - -#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) /* Put win32 includes here */ -#ifndef WINVER -//Windows XP -#define WINVER 0x0501 -#endif - -// The mingw32/64 Windows library warns about including winsock2.h after -// windows.h even though with the above it's a valid thing to do. So, to make -// mingw32 headers happy, we include winsock2.h first. -#include - -#include -#include - -#else // UNIX includes - -#include -#include -#include -#include -#include - -#endif +#include // bool +#include // size_t +#include // uint*_t #ifdef __cplusplus extern "C" { #endif -typedef short Family; +typedef uint8_t Family; -typedef int Socket; -Socket net_socket(int domain, int type, int protocol); +typedef struct Socket { + int socket; +} Socket; + +Socket net_socket(Family domain, int type, int protocol); + +/* Check if socket is valid. + * + * return 1 if valid + * return 0 if not valid + */ +int sock_valid(Socket sock); + +extern const Socket net_invalid_socket; + +/** + * Calls send(sockfd, buf, len, MSG_NOSIGNAL). + */ +int net_send(Socket sockfd, const void *buf, size_t len); +/** + * Calls recv(sockfd, buf, len, MSG_NOSIGNAL). + */ +int net_recv(Socket sockfd, void *buf, size_t len); +/** + * Calls listen(sockfd, backlog). + */ +int net_listen(Socket sockfd, int backlog); +/** + * Calls accept(sockfd, nullptr, nullptr). + */ +Socket net_accept(Socket sockfd); + +/** + * return the amount of data in the tcp recv buffer. + * return 0 on failure. + */ +size_t net_socket_data_recv_buffer(Socket sock); #define MAX_UDP_PACKET_SIZE 2048 @@ -326,13 +327,6 @@ uint16_t net_port(const Networking_Core *net); */ int networking_at_startup(void); -/* Check if socket is valid. - * - * return 1 if valid - * return 0 if not valid - */ -int sock_valid(Socket sock); - /* Close the socket. */ void 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 @@ */ /* - * Copyright © 2016-2017 The TokTok team. + * Copyright © 2016-2018 The TokTok team. * Copyright © 2013 Tox project. * * This file is part of Tox, the free peer to peer instant messenger. @@ -27,6 +27,9 @@ #include "onion.h" +#include +#include + #include "util.h" #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 @@ #include "onion_announce.h" +#include +#include + #include "LAN_discovery.h" #include "util.h" 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 @@ #include "onion_client.h" +#include +#include + #include "LAN_discovery.h" #include "util.h" 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 @@ #include "ping.h" +#include +#include + #include "DHT.h" #include "network.h" #include "ping_array.h" #include "util.h" -#include - #define PING_NUM_MAX 512 /* 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 @@ #include "ping_array.h" +#include +#include + #include "crypto_core.h" #include "util.h" 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 @@ typedef struct Messenger Tox; #include "tox.h" +#include + #include "Messenger.h" #include "group.h" #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 @@ #include "crypto_core.h" /* for CRYPTO_PUBLIC_KEY_SIZE */ #include "network.h" /* for current_time_monotonic */ +#include #include 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 @@ #include #endif +#include #include #if TOX_PASS_SALT_LENGTH != crypto_pwhash_scryptsalsa208sha256_SALTBYTES