Skip to content

Commit

Permalink
cleanup: clarify and isolate use of custom IP_Port values to denote T…
Browse files Browse the repository at this point in the history
…CP connections
  • Loading branch information
zugz authored and iphydf committed Apr 3, 2022
1 parent dec1399 commit 7cee48d
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 37 deletions.
24 changes: 24 additions & 0 deletions toxcore/TCP_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,30 @@ void set_onion_packet_tcp_connection_callback(TCP_Connections *tcp_c, tcp_onion_
tcp_c->tcp_onion_callback_object = object;
}

/** @brief Encode tcp_connections_number as a custom ip_port.
*
* return ip_port.
*/
IP_Port tcp_connections_number_to_ip_port(unsigned int tcp_connections_number)
{
IP_Port ip_port = {{{0}}};
ip_port.ip.family = net_family_tcp_server;
ip_port.ip.ip.v6.uint32[0] = tcp_connections_number;
return ip_port;
}

/** @brief Decode ip_port created by tcp_connections_number_to_ip_port to tcp_connections_number.
*
* return true on success.
* return false if ip_port is invalid.
*/
non_null()
bool ip_port_to_tcp_connections_number(const IP_Port *ip_port, unsigned int *tcp_connections_number)
{
*tcp_connections_number = ip_port->ip.ip.v6.uint32[0];
return net_family_is_tcp_server(ip_port->ip.family);
}

/** @brief Find the TCP connection with public_key.
*
* return connections_number on success.
Expand Down
14 changes: 14 additions & 0 deletions toxcore/TCP_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,20 @@ typedef int tcp_oob_cb(void *object, const uint8_t *public_key, unsigned int tcp
non_null()
void set_oob_packet_tcp_connection_callback(TCP_Connections *tcp_c, tcp_oob_cb *tcp_oob_callback, void *object);

/** @brief Encode tcp_connections_number as a custom ip_port.
*
* return ip_port.
*/
IP_Port tcp_connections_number_to_ip_port(unsigned int tcp_connections_number);

/** @brief Decode ip_port created by tcp_connections_number_to_ip_port to tcp_connections_number.
*
* return true on success.
* return false if ip_port is invalid.
*/
non_null()
bool ip_port_to_tcp_connections_number(const IP_Port *ip_port, unsigned int *tcp_connections_number);

/** @brief Create a new TCP connection to public_key.
*
* public_key must be the counterpart to the secret key that the other peer used with `new_tcp_connections()`.
Expand Down
44 changes: 32 additions & 12 deletions toxcore/TCP_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -566,22 +566,47 @@ static int rm_connection_index(TCP_Server *tcp_server, TCP_Secure_Connection *co
return -1;
}

/* Encode con_id and identifier as a custom IP_Port.
*
* return ip_port.
*/
static IP_Port con_id_to_ip_port(uint32_t con_id, uint64_t identifier)
{
IP_Port ip_port = {{{0}}};
ip_port.ip.family = net_family_tcp_client;
ip_port.ip.ip.v6.uint32[0] = con_id;
ip_port.ip.ip.v6.uint64[1] = identifier;
return ip_port;

}

/* Decode ip_port created by con_id_to_ip_port to con_id.
*
* return true on success.
* return false if ip_port is invalid.
*/
non_null()
static bool ip_port_to_con_id(const TCP_Server *tcp_server, const IP_Port *ip_port, uint32_t *con_id)
{
*con_id = ip_port->ip.ip.v6.uint32[0];

return (net_family_is_tcp_client(ip_port->ip.family) &&
*con_id < tcp_server->size_accepted_connections &&
tcp_server->accepted_connection_array[*con_id].identifier == ip_port->ip.ip.v6.uint64[1]);
}

non_null()
static int handle_onion_recv_1(void *object, const IP_Port *dest, const uint8_t *data, uint16_t length)
{
TCP_Server *tcp_server = (TCP_Server *)object;
const uint32_t index = dest->ip.ip.v6.uint32[0];
uint32_t index;

if (index >= tcp_server->size_accepted_connections) {
if (!ip_port_to_con_id(tcp_server, dest, &index)) {
return 1;
}

TCP_Secure_Connection *con = &tcp_server->accepted_connection_array[index];

if (con->identifier != dest->ip.ip.v6.uint64[1]) {
return 1;
}

VLA(uint8_t, packet, 1 + length);
memcpy(packet + 1, data, length);
packet[0] = TCP_PACKET_ONION_RESPONSE;
Expand Down Expand Up @@ -688,12 +713,7 @@ static int handle_TCP_packet(TCP_Server *tcp_server, uint32_t con_id, const uint
return -1;
}

IP_Port source;
source.port = 0; // dummy initialise
source.ip.family = net_family_tcp_onion;
source.ip.ip.v6.uint32[0] = con_id;
source.ip.ip.v6.uint32[1] = 0;
source.ip.ip.v6.uint64[1] = con->identifier;
IP_Port source = con_id_to_ip_port(con_id, con->identifier);
onion_send_1(tcp_server->onion, data + 1 + CRYPTO_NONCE_SIZE, length - (1 + CRYPTO_NONCE_SIZE), &source,
data + 1);
}
Expand Down
11 changes: 5 additions & 6 deletions toxcore/net_crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -1983,8 +1983,10 @@ static int crypto_connection_add_source(Net_Crypto *c, int crypt_connection_id,
return 0;
}

if (net_family_is_tcp_family(source->ip.family)) {
if (add_tcp_number_relay_connection(c->tcp_c, conn->connection_number_tcp, source->ip.ip.v6.uint32[0]) == 0) {
unsigned int tcp_connections_number;

if (ip_port_to_tcp_connections_number(source, &tcp_connections_number)) {
if (add_tcp_number_relay_connection(c->tcp_c, conn->connection_number_tcp, tcp_connections_number) == 0) {
return 1;
}
}
Expand Down Expand Up @@ -2268,10 +2270,7 @@ static int tcp_oob_callback(void *object, const uint8_t *public_key, unsigned in
}

if (data[0] == NET_PACKET_CRYPTO_HS) {
IP_Port source;
source.port = 0;
source.ip.family = net_family_tcp_family;
source.ip.ip.v6.uint32[0] = tcp_connections_number;
IP_Port source = tcp_connections_number_to_ip_port(tcp_connections_number);

if (handle_new_connection_handshake(c, &source, data, length, userdata) != 0) {
return -1;
Expand Down
12 changes: 6 additions & 6 deletions toxcore/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,8 @@ const Socket net_invalid_socket = { (int)INVALID_SOCKET };
const Family net_family_unspec = {TOX_AF_UNSPEC};
const Family net_family_ipv4 = {TOX_AF_INET};
const Family net_family_ipv6 = {TOX_AF_INET6};
const Family net_family_tcp_family = {TCP_FAMILY};
const Family net_family_tcp_onion = {TCP_ONION_FAMILY};
const Family net_family_tcp_server = {TCP_SERVER_FAMILY};
const Family net_family_tcp_client = {TCP_CLIENT_FAMILY};
const Family net_family_tcp_ipv4 = {TCP_INET};
const Family net_family_tcp_ipv6 = {TCP_INET6};
const Family net_family_tox_tcp_ipv4 = {TOX_TCP_INET};
Expand All @@ -393,14 +393,14 @@ bool net_family_is_ipv6(Family family)
return family.value == net_family_ipv6.value;
}

bool net_family_is_tcp_family(Family family)
bool net_family_is_tcp_server(Family family)
{
return family.value == net_family_tcp_family.value;
return family.value == net_family_tcp_server.value;
}

bool net_family_is_tcp_onion(Family family)
bool net_family_is_tcp_client(Family family)
{
return family.value == net_family_tcp_onion.value;
return family.value == net_family_tcp_client.value;
}

bool net_family_is_tcp_ipv4(Family family)
Expand Down
12 changes: 6 additions & 6 deletions toxcore/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ typedef struct Family {
bool net_family_is_unspec(Family family);
bool net_family_is_ipv4(Family family);
bool net_family_is_ipv6(Family family);
bool net_family_is_tcp_family(Family family);
bool net_family_is_tcp_onion(Family family);
bool net_family_is_tcp_server(Family family);
bool net_family_is_tcp_client(Family family);
bool net_family_is_tcp_ipv4(Family family);
bool net_family_is_tcp_ipv6(Family family);
bool net_family_is_tox_tcp_ipv4(Family family);
Expand All @@ -87,8 +87,8 @@ bool net_family_is_tox_tcp_ipv6(Family family);
extern const Family net_family_unspec;
extern const Family net_family_ipv4;
extern const Family net_family_ipv6;
extern const Family net_family_tcp_family;
extern const Family net_family_tcp_onion;
extern const Family net_family_tcp_server;
extern const Family net_family_tcp_client;
extern const Family net_family_tcp_ipv4;
extern const Family net_family_tcp_ipv6;
extern const Family net_family_tox_tcp_ipv4;
Expand Down Expand Up @@ -195,10 +195,10 @@ typedef enum Net_Packet_Type {
#define TOX_PROTO_UDP 2

/** TCP related */
#define TCP_ONION_FAMILY (TOX_AF_INET6 + 1)
#define TCP_CLIENT_FAMILY (TOX_AF_INET6 + 1)
#define TCP_INET (TOX_AF_INET6 + 2)
#define TCP_INET6 (TOX_AF_INET6 + 3)
#define TCP_FAMILY (TOX_AF_INET6 + 4)
#define TCP_SERVER_FAMILY (TOX_AF_INET6 + 4)

#define SIZE_IP4 4
#define SIZE_IP6 16
Expand Down
15 changes: 8 additions & 7 deletions toxcore/onion_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,7 @@ static uint16_t random_nodes_path_onion(const Onion_Client *onion_c, Node_format

if (num_nodes >= 2) {
nodes[0] = empty_node_format;
nodes[0].ip_port.ip.family = net_family_tcp_family;
nodes[0].ip_port.ip.ip.v4.uint32 = random_tcp;
nodes[0].ip_port = tcp_connections_number_to_ip_port(random_tcp);

for (unsigned int i = 1; i < max_num; ++i) {
const uint32_t rand_idx = random_range_u32(onion_c->rng, num_nodes);
Expand All @@ -306,8 +305,7 @@ static uint16_t random_nodes_path_onion(const Onion_Client *onion_c, Node_format
}

nodes[0] = empty_node_format;
nodes[0].ip_port.ip.family = net_family_tcp_family;
nodes[0].ip_port.ip.ip.v4.uint32 = random_tcp;
nodes[0].ip_port = tcp_connections_number_to_ip_port(random_tcp);

for (unsigned int i = 1; i < max_num; ++i) {
const uint32_t rand_idx = random_range_u32(onion_c->rng, num_nodes_bs);
Expand Down Expand Up @@ -492,15 +490,17 @@ static int send_onion_packet_tcp_udp(const Onion_Client *onion_c, const Onion_Pa
return 0;
}

if (net_family_is_tcp_family(path->ip_port1.ip.family)) {
unsigned int tcp_connections_number;

if (ip_port_to_tcp_connections_number(&path->ip_port1, &tcp_connections_number)) {
uint8_t packet[ONION_MAX_PACKET_SIZE];
const int len = create_onion_packet_tcp(onion_c->rng, packet, sizeof(packet), path, dest, data, length);

if (len == -1) {
return -1;
}

return send_tcp_onion_request(onion_c->c, path->ip_port1.ip.ip.v4.uint32, packet, len);
return send_tcp_onion_request(onion_c->c, tcp_connections_number, packet, len);
}

return -1;
Expand Down Expand Up @@ -1042,7 +1042,7 @@ static int handle_tcp_onion(void *object, const uint8_t *data, uint16_t length,
}

IP_Port ip_port = {{{0}}};
ip_port.ip.family = net_family_tcp_family;
ip_port.ip.family = net_family_tcp_server;

if (data[0] == NET_PACKET_ANNOUNCE_RESPONSE_OLD) {
return handle_announce_response(object, &ip_port, data, length, userdata);
Expand Down Expand Up @@ -1548,6 +1548,7 @@ static void populate_path_nodes_tcp(Onion_Client *onion_c)
const unsigned int num_nodes = copy_connected_tcp_relays(onion_c->c, node_list, MAX_SENT_NODES);

for (unsigned int i = 0; i < num_nodes; ++i) {
// XXX: but ip_port is TCP, so will be rejected by onion_add_bs_path_node...
onion_add_bs_path_node(onion_c, &node_list[i].ip_port, node_list[i].public_key);
}
}
Expand Down

0 comments on commit 7cee48d

Please sign in to comment.