From 7cee48d9c44ddd4ed8b5a9e1a8d81c91bc65ae69 Mon Sep 17 00:00:00 2001 From: "zugz (tox)" Date: Fri, 16 Aug 2019 00:00:00 +0000 Subject: [PATCH] cleanup: clarify and isolate use of custom IP_Port values to denote TCP connections --- toxcore/TCP_connection.c | 24 ++++++++++++++++++++++ toxcore/TCP_connection.h | 14 +++++++++++++ toxcore/TCP_server.c | 44 +++++++++++++++++++++++++++++----------- toxcore/net_crypto.c | 11 +++++----- toxcore/network.c | 12 +++++------ toxcore/network.h | 12 +++++------ toxcore/onion_client.c | 15 +++++++------- 7 files changed, 95 insertions(+), 37 deletions(-) diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c index c0011bcdd3..623a27a24a 100644 --- a/toxcore/TCP_connection.c +++ b/toxcore/TCP_connection.c @@ -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. diff --git a/toxcore/TCP_connection.h b/toxcore/TCP_connection.h index 5f0b9380a9..fd9e12cb51 100644 --- a/toxcore/TCP_connection.h +++ b/toxcore/TCP_connection.h @@ -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()`. diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c index 1020c15e44..f999930428 100644 --- a/toxcore/TCP_server.c +++ b/toxcore/TCP_server.c @@ -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; @@ -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); } diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 11483516a7..aba4d6526d 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -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; } } @@ -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; diff --git a/toxcore/network.c b/toxcore/network.c index e9a9e36a0d..7d01382f92 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -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}; @@ -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) diff --git a/toxcore/network.h b/toxcore/network.h index 1220d0368f..28d2159491 100644 --- a/toxcore/network.h +++ b/toxcore/network.h @@ -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); @@ -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; @@ -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 diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index f39ecaab79..9701f325cf 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c @@ -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); @@ -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); @@ -492,7 +490,9 @@ 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); @@ -500,7 +500,7 @@ static int send_onion_packet_tcp_udp(const Onion_Client *onion_c, const Onion_Pa 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; @@ -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); @@ -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); } }