Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make TCP_Client_Connection a module-private type. #674

Merged
merged 1 commit into from
Jan 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions auto_tests/TCP_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -426,26 +426,26 @@ START_TEST(test_client)
TCP_Client_Connection *conn = new_TCP_connection(ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, 0);
c_sleep(50);
do_TCP_connection(conn, NULL);
ck_assert_msg(conn->status == TCP_CLIENT_UNCONFIRMED, "Wrong status. Expected: %u, is: %u", TCP_CLIENT_UNCONFIRMED,
conn->status);
ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_UNCONFIRMED, "Wrong status. Expected: %u, is: %u",
TCP_CLIENT_UNCONFIRMED, tcp_con_status(conn));
c_sleep(50);
do_TCP_server(tcp_s);
c_sleep(50);
do_TCP_connection(conn, NULL);
ck_assert_msg(conn->status == TCP_CLIENT_CONFIRMED, "Wrong status. Expected: %u, is: %u", TCP_CLIENT_CONFIRMED,
conn->status);
ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_CONFIRMED, "Wrong status. Expected: %u, is: %u", TCP_CLIENT_CONFIRMED,
tcp_con_status(conn));
c_sleep(500);
do_TCP_connection(conn, NULL);
ck_assert_msg(conn->status == TCP_CLIENT_CONFIRMED, "Wrong status. Expected: %u, is: %u", TCP_CLIENT_CONFIRMED,
conn->status);
ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_CONFIRMED, "Wrong status. Expected: %u, is: %u", TCP_CLIENT_CONFIRMED,
tcp_con_status(conn));
c_sleep(500);
do_TCP_connection(conn, NULL);
ck_assert_msg(conn->status == TCP_CLIENT_CONFIRMED, "Wrong status. Expected: %u, is: %u", TCP_CLIENT_CONFIRMED,
conn->status);
ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_CONFIRMED, "Wrong status. Expected: %u, is: %u", TCP_CLIENT_CONFIRMED,
tcp_con_status(conn));
do_TCP_server(tcp_s);
c_sleep(50);
ck_assert_msg(conn->status == TCP_CLIENT_CONFIRMED, "Wrong status. Expected: %u, is: %u", TCP_CLIENT_CONFIRMED,
conn->status);
ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_CONFIRMED, "Wrong status. Expected: %u, is: %u", TCP_CLIENT_CONFIRMED,
tcp_con_status(conn));

uint8_t f2_public_key[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t f2_secret_key[CRYPTO_SECRET_KEY_SIZE];
Expand Down Expand Up @@ -523,16 +523,16 @@ START_TEST(test_client_invalid)
TCP_Client_Connection *conn = new_TCP_connection(ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, 0);
c_sleep(50);
do_TCP_connection(conn, NULL);
ck_assert_msg(conn->status == TCP_CLIENT_CONNECTING, "Wrong status. Expected: %u, is: %u", TCP_CLIENT_CONNECTING,
conn->status);
ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_CONNECTING, "Wrong status. Expected: %u, is: %u",
TCP_CLIENT_CONNECTING, tcp_con_status(conn));
c_sleep(5000);
do_TCP_connection(conn, NULL);
ck_assert_msg(conn->status == TCP_CLIENT_CONNECTING, "Wrong status. Expected: %u, is: %u", TCP_CLIENT_CONNECTING,
conn->status);
ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_CONNECTING, "Wrong status. Expected: %u, is: %u",
TCP_CLIENT_CONNECTING, tcp_con_status(conn));
c_sleep(6000);
do_TCP_connection(conn, NULL);
ck_assert_msg(conn->status == TCP_CLIENT_DISCONNECTED, "Wrong status. Expected: %u, is: %u", TCP_CLIENT_DISCONNECTED,
conn->status);
ck_assert_msg(tcp_con_status(conn) == TCP_CLIENT_DISCONNECTED, "Wrong status. Expected: %u, is: %u",
TCP_CLIENT_DISCONNECTED, tcp_con_status(conn));

kill_TCP_connection(conn);
}
Expand Down
82 changes: 82 additions & 0 deletions toxcore/TCP_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,88 @@
#include <sys/ioctl.h>
#endif

struct TCP_Client_Connection {
TCP_CLIENT_STATUS status;
Socket sock;
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; /* our public key */
uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; /* public key of the server */
IP_Port ip_port; /* The ip and port of the server */
TCP_Proxy_Info proxy_info;
uint8_t recv_nonce[CRYPTO_NONCE_SIZE]; /* Nonce of received packets. */
uint8_t sent_nonce[CRYPTO_NONCE_SIZE]; /* Nonce of sent packets. */
uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE];
uint16_t next_packet_length;

uint8_t temp_secret_key[CRYPTO_SECRET_KEY_SIZE];

uint8_t last_packet[2 + MAX_PACKET_SIZE];
uint16_t last_packet_length;
uint16_t last_packet_sent;

TCP_Priority_List *priority_queue_start, *priority_queue_end;

uint64_t kill_at;

uint64_t last_pinged;
uint64_t ping_id;

uint64_t ping_response_id;
uint64_t ping_request_id;

struct {
uint8_t status; /* 0 if not used, 1 if other is offline, 2 if other is online. */
uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE];
uint32_t number;
} connections[NUM_CLIENT_CONNECTIONS];
int (*response_callback)(void *object, uint8_t connection_id, const uint8_t *public_key);
void *response_callback_object;
int (*status_callback)(void *object, uint32_t number, uint8_t connection_id, uint8_t status);
void *status_callback_object;
int (*data_callback)(void *object, uint32_t number, uint8_t connection_id, const uint8_t *data, uint16_t length,
void *userdata);
void *data_callback_object;
int (*oob_data_callback)(void *object, const uint8_t *public_key, const uint8_t *data, uint16_t length, void *userdata);
void *oob_data_callback_object;

int (*onion_callback)(void *object, const uint8_t *data, uint16_t length, void *userdata);
void *onion_callback_object;

/* Can be used by user. */
void *custom_object;
uint32_t custom_uint;
};

const uint8_t *tcp_con_public_key(const TCP_Client_Connection *con)
{
return con->public_key;
}

IP_Port tcp_con_ip_port(const TCP_Client_Connection *con)
{
return con->ip_port;
}

TCP_CLIENT_STATUS tcp_con_status(const TCP_Client_Connection *con)
{
return con->status;
}
void *tcp_con_custom_object(const TCP_Client_Connection *con)
{
return con->custom_object;
}
uint32_t tcp_con_custom_uint(const TCP_Client_Connection *con)
{
return con->custom_uint;
}
void tcp_con_set_custom_object(TCP_Client_Connection *con, void *object)
{
con->custom_object = object;
}
void tcp_con_set_custom_uint(TCP_Client_Connection *con, uint32_t uint)
{
con->custom_uint = uint;
}

/* return 1 on success
* return 0 on failure
*/
Expand Down
64 changes: 12 additions & 52 deletions toxcore/TCP_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ typedef struct {
uint8_t proxy_type; // a value from TCP_PROXY_TYPE
} TCP_Proxy_Info;

enum {
typedef enum {
TCP_CLIENT_NO_STATUS,
TCP_CLIENT_PROXY_HTTP_CONNECTING,
TCP_CLIENT_PROXY_SOCKS5_CONNECTING,
Expand All @@ -49,57 +49,17 @@ enum {
TCP_CLIENT_UNCONFIRMED,
TCP_CLIENT_CONFIRMED,
TCP_CLIENT_DISCONNECTED,
};
typedef struct {
uint8_t status;
Socket sock;
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; /* our public key */
uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; /* public key of the server */
IP_Port ip_port; /* The ip and port of the server */
TCP_Proxy_Info proxy_info;
uint8_t recv_nonce[CRYPTO_NONCE_SIZE]; /* Nonce of received packets. */
uint8_t sent_nonce[CRYPTO_NONCE_SIZE]; /* Nonce of sent packets. */
uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE];
uint16_t next_packet_length;

uint8_t temp_secret_key[CRYPTO_SECRET_KEY_SIZE];

uint8_t last_packet[2 + MAX_PACKET_SIZE];
uint16_t last_packet_length;
uint16_t last_packet_sent;

TCP_Priority_List *priority_queue_start, *priority_queue_end;

uint64_t kill_at;

uint64_t last_pinged;
uint64_t ping_id;

uint64_t ping_response_id;
uint64_t ping_request_id;

struct {
uint8_t status; /* 0 if not used, 1 if other is offline, 2 if other is online. */
uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE];
uint32_t number;
} connections[NUM_CLIENT_CONNECTIONS];
int (*response_callback)(void *object, uint8_t connection_id, const uint8_t *public_key);
void *response_callback_object;
int (*status_callback)(void *object, uint32_t number, uint8_t connection_id, uint8_t status);
void *status_callback_object;
int (*data_callback)(void *object, uint32_t number, uint8_t connection_id, const uint8_t *data, uint16_t length,
void *userdata);
void *data_callback_object;
int (*oob_data_callback)(void *object, const uint8_t *public_key, const uint8_t *data, uint16_t length, void *userdata);
void *oob_data_callback_object;

int (*onion_callback)(void *object, const uint8_t *data, uint16_t length, void *userdata);
void *onion_callback_object;

/* Can be used by user. */
void *custom_object;
uint32_t custom_uint;
} TCP_Client_Connection;
} TCP_CLIENT_STATUS;
typedef struct TCP_Client_Connection TCP_Client_Connection;

const uint8_t *tcp_con_public_key(const TCP_Client_Connection *con);
IP_Port tcp_con_ip_port(const TCP_Client_Connection *con);
TCP_CLIENT_STATUS tcp_con_status(const TCP_Client_Connection *con);

void *tcp_con_custom_object(const TCP_Client_Connection *con);
uint32_t tcp_con_custom_uint(const TCP_Client_Connection *con);
void tcp_con_set_custom_object(TCP_Client_Connection *con, void *object);
void tcp_con_set_custom_uint(TCP_Client_Connection *con, uint32_t uint);

/* Create new TCP connection to ip_port/public_key
*/
Expand Down
38 changes: 19 additions & 19 deletions toxcore/TCP_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ static int find_tcp_connection_relay(TCP_Connections *tcp_c, const uint8_t *rela
return i;
}
} else {
if (public_key_cmp(tcp_con->connection->public_key, relay_pk) == 0) {
if (public_key_cmp(tcp_con_public_key(tcp_con->connection), relay_pk) == 0) {
return i;
}
}
Expand Down Expand Up @@ -768,9 +768,9 @@ static int reconnect_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connec
return -1;
}

IP_Port ip_port = tcp_con->connection->ip_port;
IP_Port ip_port = tcp_con_ip_port(tcp_con->connection);
uint8_t relay_pk[CRYPTO_PUBLIC_KEY_SIZE];
memcpy(relay_pk, tcp_con->connection->public_key, CRYPTO_PUBLIC_KEY_SIZE);
memcpy(relay_pk, tcp_con_public_key(tcp_con->connection), CRYPTO_PUBLIC_KEY_SIZE);
kill_TCP_connection(tcp_con->connection);
tcp_con->connection = new_TCP_connection(ip_port, relay_pk, tcp_c->self_public_key, tcp_c->self_secret_key,
&tcp_c->proxy_info);
Expand Down Expand Up @@ -820,8 +820,8 @@ static int sleep_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connection
return -1;
}

tcp_con->ip_port = tcp_con->connection->ip_port;
memcpy(tcp_con->relay_pk, tcp_con->connection->public_key, CRYPTO_PUBLIC_KEY_SIZE);
tcp_con->ip_port = tcp_con_ip_port(tcp_con->connection);
memcpy(tcp_con->relay_pk, tcp_con_public_key(tcp_con->connection), CRYPTO_PUBLIC_KEY_SIZE);

kill_TCP_connection(tcp_con->connection);
tcp_con->connection = NULL;
Expand Down Expand Up @@ -905,9 +905,9 @@ static int send_tcp_relay_routing_request(TCP_Connections *tcp_c, int tcp_connec
static int tcp_response_callback(void *object, uint8_t connection_id, const uint8_t *public_key)
{
TCP_Client_Connection *TCP_client_con = (TCP_Client_Connection *)object;
TCP_Connections *tcp_c = (TCP_Connections *)TCP_client_con->custom_object;
TCP_Connections *tcp_c = (TCP_Connections *)tcp_con_custom_object(TCP_client_con);

unsigned int tcp_connections_number = TCP_client_con->custom_uint;
unsigned int tcp_connections_number = tcp_con_custom_uint(TCP_client_con);
TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);

if (!tcp_con) {
Expand Down Expand Up @@ -938,9 +938,9 @@ static int tcp_response_callback(void *object, uint8_t connection_id, const uint
static int tcp_status_callback(void *object, uint32_t number, uint8_t connection_id, uint8_t status)
{
TCP_Client_Connection *TCP_client_con = (TCP_Client_Connection *)object;
TCP_Connections *tcp_c = (TCP_Connections *)TCP_client_con->custom_object;
TCP_Connections *tcp_c = (TCP_Connections *)tcp_con_custom_object(TCP_client_con);

unsigned int tcp_connections_number = TCP_client_con->custom_uint;
unsigned int tcp_connections_number = tcp_con_custom_uint(TCP_client_con);
TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
TCP_Connection_to *con_to = get_connection(tcp_c, number);

Expand Down Expand Up @@ -981,9 +981,9 @@ static int tcp_conn_data_callback(void *object, uint32_t number, uint8_t connect
}

TCP_Client_Connection *TCP_client_con = (TCP_Client_Connection *)object;
TCP_Connections *tcp_c = (TCP_Connections *)TCP_client_con->custom_object;
TCP_Connections *tcp_c = (TCP_Connections *)tcp_con_custom_object(TCP_client_con);

unsigned int tcp_connections_number = TCP_client_con->custom_uint;
unsigned int tcp_connections_number = tcp_con_custom_uint(TCP_client_con);
TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);

if (!tcp_con) {
Expand Down Expand Up @@ -1011,9 +1011,9 @@ static int tcp_conn_oob_callback(void *object, const uint8_t *public_key, const
}

TCP_Client_Connection *TCP_client_con = (TCP_Client_Connection *)object;
TCP_Connections *tcp_c = (TCP_Connections *)TCP_client_con->custom_object;
TCP_Connections *tcp_c = (TCP_Connections *)tcp_con_custom_object(TCP_client_con);

unsigned int tcp_connections_number = TCP_client_con->custom_uint;
unsigned int tcp_connections_number = tcp_con_custom_uint(TCP_client_con);
TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);

if (!tcp_con) {
Expand Down Expand Up @@ -1062,8 +1062,8 @@ static int tcp_relay_set_callbacks(TCP_Connections *tcp_c, int tcp_connections_n

TCP_Client_Connection *con = tcp_con->connection;

con->custom_object = tcp_c;
con->custom_uint = tcp_connections_number;
tcp_con_set_custom_object(con, tcp_c);
tcp_con_set_custom_uint(con, tcp_connections_number);
onion_response_handler(con, &tcp_onion_callback, tcp_c);
routing_response_handler(con, &tcp_response_callback, con);
routing_status_handler(con, &tcp_status_callback, con);
Expand Down Expand Up @@ -1273,8 +1273,8 @@ unsigned int tcp_copy_connected_relays(TCP_Connections *tcp_c, Node_format *tcp_
}

if (tcp_con->status == TCP_CONN_CONNECTED) {
memcpy(tcp_relays[copied].public_key, tcp_con->connection->public_key, CRYPTO_PUBLIC_KEY_SIZE);
tcp_relays[copied].ip_port = tcp_con->connection->ip_port;
memcpy(tcp_relays[copied].public_key, tcp_con_public_key(tcp_con->connection), CRYPTO_PUBLIC_KEY_SIZE);
tcp_relays[copied].ip_port = tcp_con_ip_port(tcp_con->connection);

if (tcp_relays[copied].ip_port.ip.family == TOX_AF_INET) {
tcp_relays[copied].ip_port.ip.family = TCP_INET;
Expand Down Expand Up @@ -1402,7 +1402,7 @@ static void do_tcp_conns(TCP_Connections *tcp_c, void *userdata)
// Make sure the TCP connection wasn't dropped in any of the callbacks.
assert(tcp_con != NULL);

if (tcp_con->connection->status == TCP_CLIENT_DISCONNECTED) {
if (tcp_con_status(tcp_con->connection) == TCP_CLIENT_DISCONNECTED) {
if (tcp_con->status == TCP_CONN_CONNECTED) {
reconnect_tcp_relay_connection(tcp_c, i);
} else {
Expand All @@ -1412,7 +1412,7 @@ static void do_tcp_conns(TCP_Connections *tcp_c, void *userdata)
continue;
}

if (tcp_con->status == TCP_CONN_VALID && tcp_con->connection->status == TCP_CLIENT_CONFIRMED) {
if (tcp_con->status == TCP_CONN_VALID && tcp_con_status(tcp_con->connection) == TCP_CLIENT_CONFIRMED) {
tcp_relay_on_online(tcp_c, i);
}

Expand Down