Skip to content

Commit

Permalink
Move system header includes from network.h to network.c
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Mar 18, 2018
1 parent 9706d9a commit 006e4bd
Show file tree
Hide file tree
Showing 34 changed files with 380 additions and 261 deletions.
24 changes: 13 additions & 11 deletions auto_tests/TCP_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,22 @@ 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);
do_TCP_server(tcp_s);
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.");
Expand All @@ -117,16 +117,18 @@ 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);
}

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);
Expand Down Expand Up @@ -179,16 +181,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.");
Expand Down Expand Up @@ -218,13 +220,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");
Expand Down
4 changes: 4 additions & 0 deletions auto_tests/monolith_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions other/bootstrap_node_packets.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
*/
#include "bootstrap_node_packets.h"

#include <string.h>

#define INFO_REQUEST_PACKET_LENGTH 78

static uint32_t bootstrap_version;
Expand Down
5 changes: 3 additions & 2 deletions toxav/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@

#include "audio.h"

#include <stdlib.h>
#include <string.h>

#include "rtp.h"

#include "../toxcore/logger.h"

#include <stdlib.h>

static struct JitterBuffer *jbuf_new(uint32_t capacity);
static void jbuf_clear(struct JitterBuffer *q);
static void jbuf_free(struct JitterBuffer *q);
Expand Down
8 changes: 5 additions & 3 deletions toxav/bwcontroller.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@

#include "bwcontroller.h"

#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>

#include "ring_buffer.h"

#include "../toxcore/logger.h"
#include "../toxcore/util.h"

#include <assert.h>
#include <errno.h>

#define BWC_PACKET_ID 196
#define BWC_SEND_INTERVAL_MS 950 /* 0.95s */
#define BWC_AVG_PKT_COUNT 20
Expand Down
3 changes: 3 additions & 0 deletions toxav/groupav.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

#include "groupav.h"

#include <stdlib.h>
#include <string.h>

#include "../toxcore/logger.h"
#include "../toxcore/util.h"

Expand Down
9 changes: 5 additions & 4 deletions toxav/rtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@

#include "rtp.h"

#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>

#include "bwcontroller.h"

#include "../toxcore/Messenger.h"
#include "../toxcore/logger.h"
#include "../toxcore/util.h"

#include <assert.h>
#include <errno.h>
#include <stdlib.h>

enum {
/**
* The number of milliseconds we want to keep a keyframe in the buffer for,
Expand Down
7 changes: 4 additions & 3 deletions toxav/video.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@

#include "video.h"

#include <assert.h>
#include <stdlib.h>
#include <string.h>

#include "msi.h"
#include "ring_buffer.h"
#include "rtp.h"

#include "../toxcore/logger.h"
#include "../toxcore/network.h"

#include <assert.h>
#include <stdlib.h>

/**
* Soft deadline the decoder should attempt to meet, in "us" (microseconds).
* Set to zero for unlimited.
Expand Down
5 changes: 5 additions & 0 deletions toxcore/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion toxcore/DHT.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -34,6 +34,8 @@
#include "util.h"

#include <assert.h>
#include <stdlib.h>
#include <string.h>

/* The timeout after which a node is discarded completely. */
#define KILL_NODE_TIMEOUT (BAD_NODE_TIMEOUT + PING_INTERVAL)
Expand Down
34 changes: 24 additions & 10 deletions toxcore/LAN_discovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

#include "LAN_discovery.h"

#include <string.h>

#include "util.h"

#define MAX_INTERFACES 16
Expand All @@ -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 <winsock2.h>

#include <windows.h>
#include <ws2tcpip.h>

#include <iphlpapi.h>

Expand Down Expand Up @@ -108,6 +118,12 @@ static void fetch_broadcast_info(uint16_t port)

#elif defined(__linux__) || defined(__FreeBSD__)

#include <netinet/in.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>

#ifdef __linux__
#include <linux/netdevice.h>
#endif
Expand All @@ -116,8 +132,6 @@ static void fetch_broadcast_info(uint16_t port)
#include <net/if.h>
#endif

#include <sys/ioctl.h>

static void fetch_broadcast_info(uint16_t port)
{
/* Not sure how many platforms this will run on,
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -187,7 +201,7 @@ static void fetch_broadcast_info(uint16_t port)
count++;
}

close(sock);
kill_sock(sock);

broadcast_count = count;

Expand Down
8 changes: 6 additions & 2 deletions toxcore/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@

#include "Messenger.h"

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "logger.h"
#include "network.h"
#include "util.h"

#include <assert.h>

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);

Expand Down
Loading

0 comments on commit 006e4bd

Please sign in to comment.