summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2018-01-13 19:19:20 +0000
committeriphydf <iphydf@users.noreply.github.com>2018-01-14 19:13:32 +0000
commitbacd74c4dd1fc6edaaacf580b20a9292f3322532 (patch)
tree546a9fe5d7eca634410236038db1e7995974d7c5 /toxcore
parent7aca413e3231dfcfa7bc484bc44319c6780a4fc7 (diff)
Make TCP_Client_Connection a module-private type.
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/TCP_client.c82
-rw-r--r--toxcore/TCP_client.h64
-rw-r--r--toxcore/TCP_connection.c38
3 files changed, 113 insertions, 71 deletions
diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c
index 8a14c7cd..6e6e6b7b 100644
--- a/toxcore/TCP_client.c
+++ b/toxcore/TCP_client.c
@@ -33,6 +33,88 @@
33#include <sys/ioctl.h> 33#include <sys/ioctl.h>
34#endif 34#endif
35 35
36struct TCP_Client_Connection {
37 TCP_CLIENT_STATUS status;
38 Socket sock;
39 uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; /* our public key */
40 uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; /* public key of the server */
41 IP_Port ip_port; /* The ip and port of the server */
42 TCP_Proxy_Info proxy_info;
43 uint8_t recv_nonce[CRYPTO_NONCE_SIZE]; /* Nonce of received packets. */
44 uint8_t sent_nonce[CRYPTO_NONCE_SIZE]; /* Nonce of sent packets. */
45 uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE];
46 uint16_t next_packet_length;
47
48 uint8_t temp_secret_key[CRYPTO_SECRET_KEY_SIZE];
49
50 uint8_t last_packet[2 + MAX_PACKET_SIZE];
51 uint16_t last_packet_length;
52 uint16_t last_packet_sent;
53
54 TCP_Priority_List *priority_queue_start, *priority_queue_end;
55
56 uint64_t kill_at;
57
58 uint64_t last_pinged;
59 uint64_t ping_id;
60
61 uint64_t ping_response_id;
62 uint64_t ping_request_id;
63
64 struct {
65 uint8_t status; /* 0 if not used, 1 if other is offline, 2 if other is online. */
66 uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE];
67 uint32_t number;
68 } connections[NUM_CLIENT_CONNECTIONS];
69 int (*response_callback)(void *object, uint8_t connection_id, const uint8_t *public_key);
70 void *response_callback_object;
71 int (*status_callback)(void *object, uint32_t number, uint8_t connection_id, uint8_t status);
72 void *status_callback_object;
73 int (*data_callback)(void *object, uint32_t number, uint8_t connection_id, const uint8_t *data, uint16_t length,
74 void *userdata);
75 void *data_callback_object;
76 int (*oob_data_callback)(void *object, const uint8_t *public_key, const uint8_t *data, uint16_t length, void *userdata);
77 void *oob_data_callback_object;
78
79 int (*onion_callback)(void *object, const uint8_t *data, uint16_t length, void *userdata);
80 void *onion_callback_object;
81
82 /* Can be used by user. */
83 void *custom_object;
84 uint32_t custom_uint;
85};
86
87const uint8_t *tcp_con_public_key(const TCP_Client_Connection *con)
88{
89 return con->public_key;
90}
91
92IP_Port tcp_con_ip_port(const TCP_Client_Connection *con)
93{
94 return con->ip_port;
95}
96
97TCP_CLIENT_STATUS tcp_con_status(const TCP_Client_Connection *con)
98{
99 return con->status;
100}
101void *tcp_con_custom_object(const TCP_Client_Connection *con)
102{
103 return con->custom_object;
104}
105uint32_t tcp_con_custom_uint(const TCP_Client_Connection *con)
106{
107 return con->custom_uint;
108}
109void tcp_con_set_custom_object(TCP_Client_Connection *con, void *object)
110{
111 con->custom_object = object;
112}
113void tcp_con_set_custom_uint(TCP_Client_Connection *con, uint32_t uint)
114{
115 con->custom_uint = uint;
116}
117
36/* return 1 on success 118/* return 1 on success
37 * return 0 on failure 119 * return 0 on failure
38 */ 120 */
diff --git a/toxcore/TCP_client.h b/toxcore/TCP_client.h
index 21254314..96e04d67 100644
--- a/toxcore/TCP_client.h
+++ b/toxcore/TCP_client.h
@@ -40,7 +40,7 @@ typedef struct {
40 uint8_t proxy_type; // a value from TCP_PROXY_TYPE 40 uint8_t proxy_type; // a value from TCP_PROXY_TYPE
41} TCP_Proxy_Info; 41} TCP_Proxy_Info;
42 42
43enum { 43typedef enum {
44 TCP_CLIENT_NO_STATUS, 44 TCP_CLIENT_NO_STATUS,
45 TCP_CLIENT_PROXY_HTTP_CONNECTING, 45 TCP_CLIENT_PROXY_HTTP_CONNECTING,
46 TCP_CLIENT_PROXY_SOCKS5_CONNECTING, 46 TCP_CLIENT_PROXY_SOCKS5_CONNECTING,
@@ -49,57 +49,17 @@ enum {
49 TCP_CLIENT_UNCONFIRMED, 49 TCP_CLIENT_UNCONFIRMED,
50 TCP_CLIENT_CONFIRMED, 50 TCP_CLIENT_CONFIRMED,
51 TCP_CLIENT_DISCONNECTED, 51 TCP_CLIENT_DISCONNECTED,
52}; 52} TCP_CLIENT_STATUS;
53typedef struct { 53typedef struct TCP_Client_Connection TCP_Client_Connection;
54 uint8_t status; 54
55 Socket sock; 55const uint8_t *tcp_con_public_key(const TCP_Client_Connection *con);
56 uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; /* our public key */ 56IP_Port tcp_con_ip_port(const TCP_Client_Connection *con);
57 uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; /* public key of the server */ 57TCP_CLIENT_STATUS tcp_con_status(const TCP_Client_Connection *con);
58 IP_Port ip_port; /* The ip and port of the server */ 58
59 TCP_Proxy_Info proxy_info; 59void *tcp_con_custom_object(const TCP_Client_Connection *con);
60 uint8_t recv_nonce[CRYPTO_NONCE_SIZE]; /* Nonce of received packets. */ 60uint32_t tcp_con_custom_uint(const TCP_Client_Connection *con);
61 uint8_t sent_nonce[CRYPTO_NONCE_SIZE]; /* Nonce of sent packets. */ 61void tcp_con_set_custom_object(TCP_Client_Connection *con, void *object);
62 uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE]; 62void tcp_con_set_custom_uint(TCP_Client_Connection *con, uint32_t uint);
63 uint16_t next_packet_length;
64
65 uint8_t temp_secret_key[CRYPTO_SECRET_KEY_SIZE];
66
67 uint8_t last_packet[2 + MAX_PACKET_SIZE];
68 uint16_t last_packet_length;
69 uint16_t last_packet_sent;
70
71 TCP_Priority_List *priority_queue_start, *priority_queue_end;
72
73 uint64_t kill_at;
74
75 uint64_t last_pinged;
76 uint64_t ping_id;
77
78 uint64_t ping_response_id;
79 uint64_t ping_request_id;
80
81 struct {
82 uint8_t status; /* 0 if not used, 1 if other is offline, 2 if other is online. */
83 uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE];
84 uint32_t number;
85 } connections[NUM_CLIENT_CONNECTIONS];
86 int (*response_callback)(void *object, uint8_t connection_id, const uint8_t *public_key);
87 void *response_callback_object;
88 int (*status_callback)(void *object, uint32_t number, uint8_t connection_id, uint8_t status);
89 void *status_callback_object;
90 int (*data_callback)(void *object, uint32_t number, uint8_t connection_id, const uint8_t *data, uint16_t length,
91 void *userdata);
92 void *data_callback_object;
93 int (*oob_data_callback)(void *object, const uint8_t *public_key, const uint8_t *data, uint16_t length, void *userdata);
94 void *oob_data_callback_object;
95
96 int (*onion_callback)(void *object, const uint8_t *data, uint16_t length, void *userdata);
97 void *onion_callback_object;
98
99 /* Can be used by user. */
100 void *custom_object;
101 uint32_t custom_uint;
102} TCP_Client_Connection;
103 63
104/* Create new TCP connection to ip_port/public_key 64/* Create new TCP connection to ip_port/public_key
105 */ 65 */
diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c
index fd7e073c..a43069da 100644
--- a/toxcore/TCP_connection.c
+++ b/toxcore/TCP_connection.c
@@ -477,7 +477,7 @@ static int find_tcp_connection_relay(TCP_Connections *tcp_c, const uint8_t *rela
477 return i; 477 return i;
478 } 478 }
479 } else { 479 } else {
480 if (public_key_cmp(tcp_con->connection->public_key, relay_pk) == 0) { 480 if (public_key_cmp(tcp_con_public_key(tcp_con->connection), relay_pk) == 0) {
481 return i; 481 return i;
482 } 482 }
483 } 483 }
@@ -768,9 +768,9 @@ static int reconnect_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connec
768 return -1; 768 return -1;
769 } 769 }
770 770
771 IP_Port ip_port = tcp_con->connection->ip_port; 771 IP_Port ip_port = tcp_con_ip_port(tcp_con->connection);
772 uint8_t relay_pk[CRYPTO_PUBLIC_KEY_SIZE]; 772 uint8_t relay_pk[CRYPTO_PUBLIC_KEY_SIZE];
773 memcpy(relay_pk, tcp_con->connection->public_key, CRYPTO_PUBLIC_KEY_SIZE); 773 memcpy(relay_pk, tcp_con_public_key(tcp_con->connection), CRYPTO_PUBLIC_KEY_SIZE);
774 kill_TCP_connection(tcp_con->connection); 774 kill_TCP_connection(tcp_con->connection);
775 tcp_con->connection = new_TCP_connection(ip_port, relay_pk, tcp_c->self_public_key, tcp_c->self_secret_key, 775 tcp_con->connection = new_TCP_connection(ip_port, relay_pk, tcp_c->self_public_key, tcp_c->self_secret_key,
776 &tcp_c->proxy_info); 776 &tcp_c->proxy_info);
@@ -820,8 +820,8 @@ static int sleep_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connection
820 return -1; 820 return -1;
821 } 821 }
822 822
823 tcp_con->ip_port = tcp_con->connection->ip_port; 823 tcp_con->ip_port = tcp_con_ip_port(tcp_con->connection);
824 memcpy(tcp_con->relay_pk, tcp_con->connection->public_key, CRYPTO_PUBLIC_KEY_SIZE); 824 memcpy(tcp_con->relay_pk, tcp_con_public_key(tcp_con->connection), CRYPTO_PUBLIC_KEY_SIZE);
825 825
826 kill_TCP_connection(tcp_con->connection); 826 kill_TCP_connection(tcp_con->connection);
827 tcp_con->connection = NULL; 827 tcp_con->connection = NULL;
@@ -905,9 +905,9 @@ static int send_tcp_relay_routing_request(TCP_Connections *tcp_c, int tcp_connec
905static int tcp_response_callback(void *object, uint8_t connection_id, const uint8_t *public_key) 905static int tcp_response_callback(void *object, uint8_t connection_id, const uint8_t *public_key)
906{ 906{
907 TCP_Client_Connection *TCP_client_con = (TCP_Client_Connection *)object; 907 TCP_Client_Connection *TCP_client_con = (TCP_Client_Connection *)object;
908 TCP_Connections *tcp_c = (TCP_Connections *)TCP_client_con->custom_object; 908 TCP_Connections *tcp_c = (TCP_Connections *)tcp_con_custom_object(TCP_client_con);
909 909
910 unsigned int tcp_connections_number = TCP_client_con->custom_uint; 910 unsigned int tcp_connections_number = tcp_con_custom_uint(TCP_client_con);
911 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); 911 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
912 912
913 if (!tcp_con) { 913 if (!tcp_con) {
@@ -938,9 +938,9 @@ static int tcp_response_callback(void *object, uint8_t connection_id, const uint
938static int tcp_status_callback(void *object, uint32_t number, uint8_t connection_id, uint8_t status) 938static int tcp_status_callback(void *object, uint32_t number, uint8_t connection_id, uint8_t status)
939{ 939{
940 TCP_Client_Connection *TCP_client_con = (TCP_Client_Connection *)object; 940 TCP_Client_Connection *TCP_client_con = (TCP_Client_Connection *)object;
941 TCP_Connections *tcp_c = (TCP_Connections *)TCP_client_con->custom_object; 941 TCP_Connections *tcp_c = (TCP_Connections *)tcp_con_custom_object(TCP_client_con);
942 942
943 unsigned int tcp_connections_number = TCP_client_con->custom_uint; 943 unsigned int tcp_connections_number = tcp_con_custom_uint(TCP_client_con);
944 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); 944 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
945 TCP_Connection_to *con_to = get_connection(tcp_c, number); 945 TCP_Connection_to *con_to = get_connection(tcp_c, number);
946 946
@@ -981,9 +981,9 @@ static int tcp_conn_data_callback(void *object, uint32_t number, uint8_t connect
981 } 981 }
982 982
983 TCP_Client_Connection *TCP_client_con = (TCP_Client_Connection *)object; 983 TCP_Client_Connection *TCP_client_con = (TCP_Client_Connection *)object;
984 TCP_Connections *tcp_c = (TCP_Connections *)TCP_client_con->custom_object; 984 TCP_Connections *tcp_c = (TCP_Connections *)tcp_con_custom_object(TCP_client_con);
985 985
986 unsigned int tcp_connections_number = TCP_client_con->custom_uint; 986 unsigned int tcp_connections_number = tcp_con_custom_uint(TCP_client_con);
987 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); 987 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
988 988
989 if (!tcp_con) { 989 if (!tcp_con) {
@@ -1011,9 +1011,9 @@ static int tcp_conn_oob_callback(void *object, const uint8_t *public_key, const
1011 } 1011 }
1012 1012
1013 TCP_Client_Connection *TCP_client_con = (TCP_Client_Connection *)object; 1013 TCP_Client_Connection *TCP_client_con = (TCP_Client_Connection *)object;
1014 TCP_Connections *tcp_c = (TCP_Connections *)TCP_client_con->custom_object; 1014 TCP_Connections *tcp_c = (TCP_Connections *)tcp_con_custom_object(TCP_client_con);
1015 1015
1016 unsigned int tcp_connections_number = TCP_client_con->custom_uint; 1016 unsigned int tcp_connections_number = tcp_con_custom_uint(TCP_client_con);
1017 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); 1017 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
1018 1018
1019 if (!tcp_con) { 1019 if (!tcp_con) {
@@ -1062,8 +1062,8 @@ static int tcp_relay_set_callbacks(TCP_Connections *tcp_c, int tcp_connections_n
1062 1062
1063 TCP_Client_Connection *con = tcp_con->connection; 1063 TCP_Client_Connection *con = tcp_con->connection;
1064 1064
1065 con->custom_object = tcp_c; 1065 tcp_con_set_custom_object(con, tcp_c);
1066 con->custom_uint = tcp_connections_number; 1066 tcp_con_set_custom_uint(con, tcp_connections_number);
1067 onion_response_handler(con, &tcp_onion_callback, tcp_c); 1067 onion_response_handler(con, &tcp_onion_callback, tcp_c);
1068 routing_response_handler(con, &tcp_response_callback, con); 1068 routing_response_handler(con, &tcp_response_callback, con);
1069 routing_status_handler(con, &tcp_status_callback, con); 1069 routing_status_handler(con, &tcp_status_callback, con);
@@ -1273,8 +1273,8 @@ unsigned int tcp_copy_connected_relays(TCP_Connections *tcp_c, Node_format *tcp_
1273 } 1273 }
1274 1274
1275 if (tcp_con->status == TCP_CONN_CONNECTED) { 1275 if (tcp_con->status == TCP_CONN_CONNECTED) {
1276 memcpy(tcp_relays[copied].public_key, tcp_con->connection->public_key, CRYPTO_PUBLIC_KEY_SIZE); 1276 memcpy(tcp_relays[copied].public_key, tcp_con_public_key(tcp_con->connection), CRYPTO_PUBLIC_KEY_SIZE);
1277 tcp_relays[copied].ip_port = tcp_con->connection->ip_port; 1277 tcp_relays[copied].ip_port = tcp_con_ip_port(tcp_con->connection);
1278 1278
1279 if (tcp_relays[copied].ip_port.ip.family == TOX_AF_INET) { 1279 if (tcp_relays[copied].ip_port.ip.family == TOX_AF_INET) {
1280 tcp_relays[copied].ip_port.ip.family = TCP_INET; 1280 tcp_relays[copied].ip_port.ip.family = TCP_INET;
@@ -1402,7 +1402,7 @@ static void do_tcp_conns(TCP_Connections *tcp_c, void *userdata)
1402 // Make sure the TCP connection wasn't dropped in any of the callbacks. 1402 // Make sure the TCP connection wasn't dropped in any of the callbacks.
1403 assert(tcp_con != NULL); 1403 assert(tcp_con != NULL);
1404 1404
1405 if (tcp_con->connection->status == TCP_CLIENT_DISCONNECTED) { 1405 if (tcp_con_status(tcp_con->connection) == TCP_CLIENT_DISCONNECTED) {
1406 if (tcp_con->status == TCP_CONN_CONNECTED) { 1406 if (tcp_con->status == TCP_CONN_CONNECTED) {
1407 reconnect_tcp_relay_connection(tcp_c, i); 1407 reconnect_tcp_relay_connection(tcp_c, i);
1408 } else { 1408 } else {
@@ -1412,7 +1412,7 @@ static void do_tcp_conns(TCP_Connections *tcp_c, void *userdata)
1412 continue; 1412 continue;
1413 } 1413 }
1414 1414
1415 if (tcp_con->status == TCP_CONN_VALID && tcp_con->connection->status == TCP_CLIENT_CONFIRMED) { 1415 if (tcp_con->status == TCP_CONN_VALID && tcp_con_status(tcp_con->connection) == TCP_CLIENT_CONFIRMED) {
1416 tcp_relay_on_online(tcp_c, i); 1416 tcp_relay_on_online(tcp_c, i);
1417 } 1417 }
1418 1418