1 /* SPDX-License-Identifier: GPL-3.0-or-later
2  * Copyright © 2016-2018 The TokTok team.
3  * Copyright © 2014 Tox project.
4  */
5 
6 /*
7  * Implementation of the TCP relay client part of Tox.
8  */
9 #ifndef C_TOXCORE_TOXCORE_TCP_CLIENT_H
10 #define C_TOXCORE_TOXCORE_TCP_CLIENT_H
11 
12 #include "TCP_server.h"
13 #include "crypto_core.h"
14 
15 #define TCP_CONNECTION_TIMEOUT 10
16 
17 typedef enum TCP_Proxy_Type {
18     TCP_PROXY_NONE,
19     TCP_PROXY_HTTP,
20     TCP_PROXY_SOCKS5,
21 } TCP_Proxy_Type;
22 
23 typedef struct TCP_Proxy_Info {
24     IP_Port ip_port;
25     uint8_t proxy_type; // a value from TCP_PROXY_TYPE
26 } TCP_Proxy_Info;
27 
28 typedef enum TCP_Client_Status {
29     TCP_CLIENT_NO_STATUS,
30     TCP_CLIENT_PROXY_HTTP_CONNECTING,
31     TCP_CLIENT_PROXY_SOCKS5_CONNECTING,
32     TCP_CLIENT_PROXY_SOCKS5_UNCONFIRMED,
33     TCP_CLIENT_CONNECTING,
34     TCP_CLIENT_UNCONFIRMED,
35     TCP_CLIENT_CONFIRMED,
36     TCP_CLIENT_DISCONNECTED,
37 } TCP_Client_Status;
38 
39 typedef struct TCP_Client_Connection TCP_Client_Connection;
40 
41 const uint8_t *tcp_con_public_key(const TCP_Client_Connection *con);
42 IP_Port tcp_con_ip_port(const TCP_Client_Connection *con);
43 TCP_Client_Status tcp_con_status(const TCP_Client_Connection *con);
44 
45 void *tcp_con_custom_object(const TCP_Client_Connection *con);
46 uint32_t tcp_con_custom_uint(const TCP_Client_Connection *con);
47 void tcp_con_set_custom_object(TCP_Client_Connection *con, void *object);
48 void tcp_con_set_custom_uint(TCP_Client_Connection *con, uint32_t value);
49 
50 /* Create new TCP connection to ip_port/public_key
51  */
52 TCP_Client_Connection *new_TCP_connection(const Mono_Time *mono_time, IP_Port ip_port, const uint8_t *public_key,
53         const uint8_t *self_public_key, const uint8_t *self_secret_key, TCP_Proxy_Info *proxy_info);
54 
55 /* Run the TCP connection
56  */
57 void do_TCP_connection(const Logger *logger, Mono_Time *mono_time, TCP_Client_Connection *tcp_connection,
58                        void *userdata);
59 
60 /* Kill the TCP connection
61  */
62 void kill_TCP_connection(TCP_Client_Connection *tcp_connection);
63 
64 typedef int tcp_onion_response_cb(void *object, const uint8_t *data, uint16_t length, void *userdata);
65 
66 /* return 1 on success.
67  * return 0 if could not send packet.
68  * return -1 on failure (connection must be killed).
69  */
70 int send_onion_request(TCP_Client_Connection *con, const uint8_t *data, uint16_t length);
71 void onion_response_handler(TCP_Client_Connection *con, tcp_onion_response_cb *onion_callback, void *object);
72 
73 typedef int tcp_routing_response_cb(void *object, uint8_t connection_id, const uint8_t *public_key);
74 typedef int tcp_routing_status_cb(void *object, uint32_t number, uint8_t connection_id, uint8_t status);
75 
76 /* return 1 on success.
77  * return 0 if could not send packet.
78  * return -1 on failure (connection must be killed).
79  */
80 int send_routing_request(TCP_Client_Connection *con, uint8_t *public_key);
81 void routing_response_handler(TCP_Client_Connection *con, tcp_routing_response_cb *response_callback, void *object);
82 void routing_status_handler(TCP_Client_Connection *con, tcp_routing_status_cb *status_callback, void *object);
83 
84 /* return 1 on success.
85  * return 0 if could not send packet.
86  * return -1 on failure (connection must be killed).
87  */
88 int send_disconnect_request(TCP_Client_Connection *con, uint8_t con_id);
89 
90 /* Set the number that will be used as an argument in the callbacks related to con_id.
91  *
92  * When not set by this function, the number is -1.
93  *
94  * return 0 on success.
95  * return -1 on failure.
96  */
97 int set_tcp_connection_number(TCP_Client_Connection *con, uint8_t con_id, uint32_t number);
98 
99 typedef int tcp_routing_data_cb(void *object, uint32_t number, uint8_t connection_id, const uint8_t *data,
100                                 uint16_t length, void *userdata);
101 
102 /* return 1 on success.
103  * return 0 if could not send packet.
104  * return -1 on failure.
105  */
106 int send_data(TCP_Client_Connection *con, uint8_t con_id, const uint8_t *data, uint16_t length);
107 void routing_data_handler(TCP_Client_Connection *con, tcp_routing_data_cb *data_callback, void *object);
108 
109 typedef int tcp_oob_data_cb(void *object, const uint8_t *public_key, const uint8_t *data, uint16_t length,
110                             void *userdata);
111 
112 /* return 1 on success.
113  * return 0 if could not send packet.
114  * return -1 on failure.
115  */
116 int send_oob_packet(TCP_Client_Connection *con, const uint8_t *public_key, const uint8_t *data, uint16_t length);
117 void oob_data_handler(TCP_Client_Connection *con, tcp_oob_data_cb *oob_data_callback, void *object);
118 
119 
120 #endif
121