Skip to content

Commit

Permalink
Make functions take const pointer to IP_Port wherever possible
Browse files Browse the repository at this point in the history
An IP_Port is a fairly large data structure, and copying them down
the call stack creates a lot of unnecessary overhead.
  • Loading branch information
JFreegman committed Feb 2, 2022
1 parent 71965a0 commit 55cc79b
Show file tree
Hide file tree
Showing 30 changed files with 351 additions and 334 deletions.
38 changes: 19 additions & 19 deletions auto_tests/TCP_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ static void test_basic(void)
for (uint8_t i = 0; i < NUM_PORTS; i++) {
sock = net_socket(net_family_ipv6, TOX_SOCK_STREAM, TOX_PROTO_TCP);
localhost.port = net_htons(ports[i]);
int ret = net_connect(logger, sock, localhost);
int ret = net_connect(logger, sock, &localhost);
ck_assert_msg(ret == 0, "Failed to connect to created TCP relay server on port %d.", ports[i]);

// Leave open one connection for the next test.
Expand Down Expand Up @@ -98,20 +98,20 @@ static void test_basic(void)

// Sending the handshake
ck_assert_msg(net_send(logger, sock, handshake, TCP_CLIENT_HANDSHAKE_SIZE - 1,
localhost) == TCP_CLIENT_HANDSHAKE_SIZE - 1,
&localhost) == TCP_CLIENT_HANDSHAKE_SIZE - 1,
"An attempt to send the initial handshake minus last byte failed.");

do_TCP_server_delay(tcp_s, mono_time, 50);

ck_assert_msg(net_send(logger, sock, handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1), 1, localhost) == 1,
ck_assert_msg(net_send(logger, sock, handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1), 1, &localhost) == 1,
"The attempt to send the last byte of handshake failed.");

do_TCP_server_delay(tcp_s, mono_time, 50);

// Receiving server response and decrypting it
uint8_t response[TCP_SERVER_HANDSHAKE_SIZE];
uint8_t response_plain[TCP_HANDSHAKE_PLAIN_SIZE];
ck_assert_msg(net_recv(logger, sock, response, TCP_SERVER_HANDSHAKE_SIZE, localhost) == TCP_SERVER_HANDSHAKE_SIZE,
ck_assert_msg(net_recv(logger, sock, response, TCP_SERVER_HANDSHAKE_SIZE, &localhost) == TCP_SERVER_HANDSHAKE_SIZE,
"Could/did not receive a server response to the initial handshake.");
ret = decrypt_data(self_public_key, f_secret_key, response, response + CRYPTO_NONCE_SIZE,
TCP_SERVER_HANDSHAKE_SIZE - CRYPTO_NONCE_SIZE, response_plain);
Expand Down Expand Up @@ -140,7 +140,7 @@ static void test_basic(void)
msg_length = sizeof(r_req) - i;
}

ck_assert_msg(net_send(logger, sock, r_req + i, msg_length, localhost) == msg_length,
ck_assert_msg(net_send(logger, sock, r_req + i, msg_length, &localhost) == msg_length,
"Failed to send request after completing the handshake.");
i += msg_length;

Expand All @@ -151,7 +151,7 @@ static void test_basic(void)

// Receiving the second response and verifying its validity
uint8_t packet_resp[4096];
int recv_data_len = net_recv(logger, sock, packet_resp, 2 + 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE, localhost);
int recv_data_len = net_recv(logger, sock, packet_resp, 2 + 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE, &localhost);
ck_assert_msg(recv_data_len == 2 + 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE,
"Failed to receive server response to request. %d", recv_data_len);
memcpy(&size, packet_resp, 2);
Expand Down Expand Up @@ -194,7 +194,7 @@ static struct sec_TCP_con *new_TCP_con(const Logger *logger, TCP_Server *tcp_s,
localhost.ip = get_loopback();
localhost.port = net_htons(ports[random_u32() % NUM_PORTS]);

int ret = net_connect(logger, sock, localhost);
int ret = net_connect(logger, sock, &localhost);
ck_assert_msg(ret == 0, "Failed to connect to the test TCP relay server.");

uint8_t f_secret_key[CRYPTO_SECRET_KEY_SIZE];
Expand All @@ -215,19 +215,19 @@ static struct sec_TCP_con *new_TCP_con(const Logger *logger, TCP_Server *tcp_s,
"Failed to encrypt the outgoing handshake.");

ck_assert_msg(net_send(logger, sock, handshake, TCP_CLIENT_HANDSHAKE_SIZE - 1,
localhost) == TCP_CLIENT_HANDSHAKE_SIZE - 1,
&localhost) == TCP_CLIENT_HANDSHAKE_SIZE - 1,
"Failed to send the first portion of the handshake to the TCP relay server.");

do_TCP_server_delay(tcp_s, mono_time, 50);

ck_assert_msg(net_send(logger, sock, handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1), 1, localhost) == 1,
ck_assert_msg(net_send(logger, sock, handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1), 1, &localhost) == 1,
"Failed to send last byte of handshake.");

do_TCP_server_delay(tcp_s, mono_time, 50);

uint8_t response[TCP_SERVER_HANDSHAKE_SIZE];
uint8_t response_plain[TCP_HANDSHAKE_PLAIN_SIZE];
ck_assert_msg(net_recv(logger, sock, response, TCP_SERVER_HANDSHAKE_SIZE, localhost) == TCP_SERVER_HANDSHAKE_SIZE,
ck_assert_msg(net_recv(logger, sock, response, TCP_SERVER_HANDSHAKE_SIZE, &localhost) == TCP_SERVER_HANDSHAKE_SIZE,
"Failed to receive server handshake response.");
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);
Expand Down Expand Up @@ -263,7 +263,7 @@ static int write_packet_TCP_test_connection(const Logger *logger, struct sec_TCP
localhost.ip = get_loopback();
localhost.port = 0;

ck_assert_msg(net_send(logger, con->sock, packet, SIZEOF_VLA(packet), localhost) == SIZEOF_VLA(packet),
ck_assert_msg(net_send(logger, con->sock, packet, SIZEOF_VLA(packet), &localhost) == SIZEOF_VLA(packet),
"Failed to send a packet.");
return 0;
}
Expand All @@ -274,7 +274,7 @@ static int read_packet_sec_TCP(const Logger *logger, struct sec_TCP_con *con, ui
localhost.ip = get_loopback();
localhost.port = 0;

int rlen = net_recv(logger, con->sock, data, length, localhost);
int rlen = net_recv(logger, con->sock, data, length, &localhost);
ck_assert_msg(rlen == length, "Did not receive packet of correct length. Wanted %i, instead got %i", length, rlen);
rlen = decrypt_data_symmetric(con->shared_key, con->recv_nonce, data + 2, length - 2, data);
ck_assert_msg(rlen != -1, "Failed to decrypt a received packet from the Relay server.");
Expand Down Expand Up @@ -490,7 +490,7 @@ static void test_client(void)
ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]);
ip_port_tcp_s.ip = get_loopback();

TCP_Client_Connection *conn = new_TCP_connection(logger, mono_time, ip_port_tcp_s, self_public_key, f_public_key,
TCP_Client_Connection *conn = new_TCP_connection(logger, mono_time, &ip_port_tcp_s, self_public_key, f_public_key,
f_secret_key, nullptr);
do_TCP_connection(logger, mono_time, conn, nullptr);
c_sleep(50);
Expand Down Expand Up @@ -525,7 +525,7 @@ static void test_client(void)
uint8_t f2_secret_key[CRYPTO_SECRET_KEY_SIZE];
crypto_new_keypair(f2_public_key, f2_secret_key);
ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]);
TCP_Client_Connection *conn2 = new_TCP_connection(logger, mono_time, ip_port_tcp_s, self_public_key, f2_public_key,
TCP_Client_Connection *conn2 = new_TCP_connection(logger, mono_time, &ip_port_tcp_s, self_public_key, f2_public_key,
f2_secret_key, nullptr);

// The client should call this function (defined earlier) during the routing process.
Expand Down Expand Up @@ -614,7 +614,7 @@ static void test_client_invalid(void)

ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]);
ip_port_tcp_s.ip = get_loopback();
TCP_Client_Connection *conn = new_TCP_connection(logger, mono_time, ip_port_tcp_s, self_public_key, f_public_key,
TCP_Client_Connection *conn = new_TCP_connection(logger, mono_time, &ip_port_tcp_s, self_public_key, f_public_key,
f_secret_key, nullptr);

// Run the client's main loop but not the server.
Expand Down Expand Up @@ -699,13 +699,13 @@ static void test_tcp_connection(void)

int connection = new_tcp_connection_to(tc_1, tcp_connections_public_key(tc_2), 123);
ck_assert_msg(connection == 0, "Connection id wrong");
ck_assert_msg(add_tcp_relay_connection(tc_1, connection, ip_port_tcp_s, tcp_server_public_key(tcp_s)) == 0,
ck_assert_msg(add_tcp_relay_connection(tc_1, connection, &ip_port_tcp_s, tcp_server_public_key(tcp_s)) == 0,
"Could not add tcp relay to connection\n");

ip_port_tcp_s.port = net_htons(ports[random_u32() % NUM_PORTS]);
connection = new_tcp_connection_to(tc_2, tcp_connections_public_key(tc_1), 123);
ck_assert_msg(connection == 0, "Connection id wrong");
ck_assert_msg(add_tcp_relay_connection(tc_2, connection, ip_port_tcp_s, tcp_server_public_key(tcp_s)) == 0,
ck_assert_msg(add_tcp_relay_connection(tc_2, connection, &ip_port_tcp_s, tcp_server_public_key(tcp_s)) == 0,
"Could not add tcp relay to connection\n");

ck_assert_msg(new_tcp_connection_to(tc_2, tcp_connections_public_key(tc_1), 123) == -1,
Expand Down Expand Up @@ -807,10 +807,10 @@ static void test_tcp_connection2(void)

int connection = new_tcp_connection_to(tc_1, tcp_connections_public_key(tc_2), 123);
ck_assert_msg(connection == 0, "Connection id wrong");
ck_assert_msg(add_tcp_relay_connection(tc_1, connection, ip_port_tcp_s, tcp_server_public_key(tcp_s)) == 0,
ck_assert_msg(add_tcp_relay_connection(tc_1, connection, &ip_port_tcp_s, tcp_server_public_key(tcp_s)) == 0,
"Could not add tcp relay to connection\n");

ck_assert_msg(add_tcp_relay_global(tc_2, ip_port_tcp_s, tcp_server_public_key(tcp_s)) == 0,
ck_assert_msg(add_tcp_relay_global(tc_2, &ip_port_tcp_s, tcp_server_public_key(tcp_s)) == 0,
"Could not add global relay");

do_TCP_server_delay(tcp_s, mono_time, 50);
Expand Down
4 changes: 2 additions & 2 deletions auto_tests/dht_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ static void test_list(void)
}
}

static void ip_callback(void *data, int32_t number, IP_Port ip_port)
static void ip_callback(void *data, int32_t number, const IP_Port *ip_port)
{
}

Expand Down Expand Up @@ -568,7 +568,7 @@ static void test_DHT_test(void)
IP_Port ip_port;
ip_port.ip = get_loopback();
ip_port.port = net_htons(DHT_DEFAULT_PORT + i);
dht_bootstrap(dhts[(i - 1) % NUM_DHT], ip_port, dhts[i]->self_public_key);
dht_bootstrap(dhts[(i - 1) % NUM_DHT], &ip_port, dhts[i]->self_public_key);
}

while (true) {
Expand Down
20 changes: 10 additions & 10 deletions auto_tests/onion_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static void do_onion(Onion *onion)
}

static int handled_test_1;
static int handle_test_1(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata)
static int handle_test_1(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length, void *userdata)
{
Onion *onion = (Onion *)object;

Expand Down Expand Up @@ -62,7 +62,7 @@ static int handle_test_1(void *object, IP_Port source, const uint8_t *packet, ui
}

static int handled_test_2;
static int handle_test_2(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata)
static int handle_test_2(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length, void *userdata)
{
const char res_message[] = "install gentoo";
uint8_t res_packet[1 + sizeof(res_message)];
Expand Down Expand Up @@ -96,7 +96,7 @@ static uint8_t sb_data[ONION_ANNOUNCE_SENDBACK_DATA_LENGTH];
static int handled_test_3;
static uint8_t test_3_pub_key[CRYPTO_PUBLIC_KEY_SIZE];
static uint8_t test_3_ping_id[CRYPTO_SHA256_SIZE];
static int handle_test_3(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata)
static int handle_test_3(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length, void *userdata)
{
Onion *onion = (Onion *)object;

Expand Down Expand Up @@ -133,7 +133,7 @@ static int handle_test_3(void *object, IP_Port source, const uint8_t *packet, ui

static uint8_t nonce[CRYPTO_NONCE_SIZE];
static int handled_test_4;
static int handle_test_4(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata)
static int handle_test_4(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length, void *userdata)
{
Onion *onion = (Onion *)object;

Expand Down Expand Up @@ -202,7 +202,7 @@ static void test_basic(void)
nodes[3] = n2;
Onion_Path path;
create_onion_path(onion1->dht, &path, nodes);
int ret = send_onion_packet(onion1->net, &path, nodes[3].ip_port, req_packet, sizeof(req_packet));
int ret = send_onion_packet(onion1->net, &path, &nodes[3].ip_port, req_packet, sizeof(req_packet));
ck_assert_msg(ret == 0, "Failed to create/send onion packet.");

handled_test_1 = 0;
Expand Down Expand Up @@ -274,7 +274,7 @@ static void test_basic(void)
ck_assert_msg((onion3 != nullptr), "Onion failed initializing.");

random_nonce(nonce);
ret = send_data_request(onion3->net, &path, nodes[3].ip_port,
ret = send_data_request(onion3->net, &path, &nodes[3].ip_port,
dht_get_self_public_key(onion1->dht),
dht_get_self_public_key(onion1->dht),
nonce, (const uint8_t *)"Install gentoo", sizeof("Install gentoo"));
Expand Down Expand Up @@ -451,7 +451,7 @@ static void kill_onions(Onions *on)
#define NUM_LAST 37

static bool first_ip, last_ip;
static void dht_ip_callback(void *object, int32_t number, IP_Port ip_port)
static void dht_ip_callback(void *object, int32_t number, const IP_Port *ip_port)
{
if (NUM_FIRST == number) {
first_ip = 1;
Expand Down Expand Up @@ -519,11 +519,11 @@ static void test_announce(void)

for (i = 3; i < NUM_ONIONS; ++i) {
IP_Port ip_port = {ip, net_port(onions[i - 1]->onion->net)};
dht_bootstrap(onions[i]->onion->dht, ip_port, dht_get_self_public_key(onions[i - 1]->onion->dht));
dht_bootstrap(onions[i]->onion->dht, &ip_port, dht_get_self_public_key(onions[i - 1]->onion->dht));
IP_Port ip_port1 = {ip, net_port(onions[i - 2]->onion->net)};
dht_bootstrap(onions[i]->onion->dht, ip_port1, dht_get_self_public_key(onions[i - 2]->onion->dht));
dht_bootstrap(onions[i]->onion->dht, &ip_port1, dht_get_self_public_key(onions[i - 2]->onion->dht));
IP_Port ip_port2 = {ip, net_port(onions[i - 3]->onion->net)};
dht_bootstrap(onions[i]->onion->dht, ip_port2, dht_get_self_public_key(onions[i - 3]->onion->dht));
dht_bootstrap(onions[i]->onion->dht, &ip_port2, dht_get_self_public_key(onions[i - 3]->onion->dht));
}

uint32_t connected = 0;
Expand Down
3 changes: 2 additions & 1 deletion other/bootstrap_node_packets.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ static uint16_t bootstrap_motd_length;
/* To request this packet just send a packet of length INFO_REQUEST_PACKET_LENGTH
* with the first byte being BOOTSTRAP_INFO_PACKET_ID
*/
static int handle_info_request(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata)
static int handle_info_request(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length,
void *userdata)
{
if (length != INFO_REQUEST_PACKET_LENGTH) {
return 1;
Expand Down
Loading

0 comments on commit 55cc79b

Please sign in to comment.