/work/toxcore/TCP_client.h
Line | Count | Source |
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 "crypto_core.h" |
13 | | #include "forwarding.h" |
14 | | #include "mono_time.h" |
15 | | #include "network.h" |
16 | | |
17 | 412 | #define TCP_CONNECTION_TIMEOUT 10 |
18 | | |
19 | | typedef enum TCP_Proxy_Type { |
20 | | TCP_PROXY_NONE, |
21 | | TCP_PROXY_HTTP, |
22 | | TCP_PROXY_SOCKS5, |
23 | | } TCP_Proxy_Type; |
24 | | |
25 | | typedef struct TCP_Proxy_Info { |
26 | | IP_Port ip_port; |
27 | | uint8_t proxy_type; // a value from TCP_PROXY_TYPE |
28 | | } TCP_Proxy_Info; |
29 | | |
30 | | typedef enum TCP_Client_Status { |
31 | | TCP_CLIENT_NO_STATUS, |
32 | | TCP_CLIENT_PROXY_HTTP_CONNECTING, |
33 | | TCP_CLIENT_PROXY_SOCKS5_CONNECTING, |
34 | | TCP_CLIENT_PROXY_SOCKS5_UNCONFIRMED, |
35 | | TCP_CLIENT_CONNECTING, |
36 | | TCP_CLIENT_UNCONFIRMED, |
37 | | TCP_CLIENT_CONFIRMED, |
38 | | TCP_CLIENT_DISCONNECTED, |
39 | | } TCP_Client_Status; |
40 | | |
41 | | typedef struct TCP_Client_Connection TCP_Client_Connection; |
42 | | |
43 | | non_null() |
44 | | const uint8_t *tcp_con_public_key(const TCP_Client_Connection *con); |
45 | | non_null() |
46 | | IP_Port tcp_con_ip_port(const TCP_Client_Connection *con); |
47 | | non_null() |
48 | | TCP_Client_Status tcp_con_status(const TCP_Client_Connection *con); |
49 | | |
50 | | non_null() |
51 | | void *tcp_con_custom_object(const TCP_Client_Connection *con); |
52 | | non_null() |
53 | | uint32_t tcp_con_custom_uint(const TCP_Client_Connection *con); |
54 | | non_null() |
55 | | void tcp_con_set_custom_object(TCP_Client_Connection *con, void *object); |
56 | | non_null() |
57 | | void tcp_con_set_custom_uint(TCP_Client_Connection *con, uint32_t value); |
58 | | |
59 | | /** Create new TCP connection to ip_port/public_key */ |
60 | | non_null(1, 2, 3, 4, 5, 6, 7, 8, 9) nullable(10) |
61 | | TCP_Client_Connection *new_tcp_connection( |
62 | | const Logger *logger, const Memory *mem, const Mono_Time *mono_time, const Random *rng, const Network *ns, |
63 | | const IP_Port *ip_port, const uint8_t *public_key, const uint8_t *self_public_key, const uint8_t *self_secret_key, |
64 | | const TCP_Proxy_Info *proxy_info); |
65 | | |
66 | | /** Run the TCP connection */ |
67 | | non_null(1, 2, 3) nullable(4) |
68 | | void do_tcp_connection(const Logger *logger, const Mono_Time *mono_time, |
69 | | TCP_Client_Connection *tcp_connection, void *userdata); |
70 | | |
71 | | /** Kill the TCP connection */ |
72 | | nullable(1) |
73 | | void kill_tcp_connection(TCP_Client_Connection *tcp_connection); |
74 | | |
75 | | typedef int tcp_onion_response_cb(void *object, const uint8_t *data, uint16_t length, void *userdata); |
76 | | |
77 | | /** |
78 | | * @retval 1 on success. |
79 | | * @retval 0 if could not send packet. |
80 | | * @retval -1 on failure (connection must be killed). |
81 | | */ |
82 | | non_null() |
83 | | int send_onion_request(const Logger *logger, TCP_Client_Connection *con, const uint8_t *data, uint16_t length); |
84 | | non_null() |
85 | | void onion_response_handler(TCP_Client_Connection *con, tcp_onion_response_cb *onion_callback, void *object); |
86 | | |
87 | | non_null() |
88 | | int send_forward_request_tcp(const Logger *logger, TCP_Client_Connection *con, const IP_Port *dest, const uint8_t *data, |
89 | | uint16_t length); |
90 | | non_null() |
91 | | void forwarding_handler(TCP_Client_Connection *con, forwarded_response_cb *forwarded_response_callback, void *object); |
92 | | |
93 | | typedef int tcp_routing_response_cb(void *object, uint8_t connection_id, const uint8_t *public_key); |
94 | | typedef int tcp_routing_status_cb(void *object, uint32_t number, uint8_t connection_id, uint8_t status); |
95 | | |
96 | | /** |
97 | | * @retval 1 on success. |
98 | | * @retval 0 if could not send packet. |
99 | | * @retval -1 on failure (connection must be killed). |
100 | | */ |
101 | | non_null() |
102 | | int send_routing_request(const Logger *logger, TCP_Client_Connection *con, const uint8_t *public_key); |
103 | | non_null() |
104 | | void routing_response_handler(TCP_Client_Connection *con, tcp_routing_response_cb *response_callback, void *object); |
105 | | non_null() |
106 | | void routing_status_handler(TCP_Client_Connection *con, tcp_routing_status_cb *status_callback, void *object); |
107 | | |
108 | | /** |
109 | | * @retval 1 on success. |
110 | | * @retval 0 if could not send packet. |
111 | | * @retval -1 on failure (connection must be killed). |
112 | | */ |
113 | | non_null() |
114 | | int send_disconnect_request(const Logger *logger, TCP_Client_Connection *con, uint8_t con_id); |
115 | | |
116 | | /** @brief Set the number that will be used as an argument in the callbacks related to con_id. |
117 | | * |
118 | | * When not set by this function, the number is -1. |
119 | | * |
120 | | * return 0 on success. |
121 | | * return -1 on failure. |
122 | | */ |
123 | | non_null() |
124 | | int set_tcp_connection_number(TCP_Client_Connection *con, uint8_t con_id, uint32_t number); |
125 | | |
126 | | typedef int tcp_routing_data_cb(void *object, uint32_t number, uint8_t connection_id, const uint8_t *data, |
127 | | uint16_t length, void *userdata); |
128 | | |
129 | | /** |
130 | | * @retval 1 on success. |
131 | | * @retval 0 if could not send packet. |
132 | | * @retval -1 on failure. |
133 | | */ |
134 | | non_null() |
135 | | int send_data(const Logger *logger, TCP_Client_Connection *con, uint8_t con_id, const uint8_t *data, uint16_t length); |
136 | | non_null() |
137 | | void routing_data_handler(TCP_Client_Connection *con, tcp_routing_data_cb *data_callback, void *object); |
138 | | |
139 | | typedef int tcp_oob_data_cb(void *object, const uint8_t *public_key, const uint8_t *data, uint16_t length, |
140 | | void *userdata); |
141 | | |
142 | | /** |
143 | | * @retval 1 on success. |
144 | | * @retval 0 if could not send packet. |
145 | | * @retval -1 on failure. |
146 | | */ |
147 | | non_null() |
148 | | int send_oob_packet(const Logger *logger, TCP_Client_Connection *con, const uint8_t *public_key, const uint8_t *data, |
149 | | uint16_t length); |
150 | | non_null() |
151 | | void oob_data_handler(TCP_Client_Connection *con, tcp_oob_data_cb *oob_data_callback, void *object); |
152 | | |
153 | | |
154 | | #endif /* C_TOXCORE_TOXCORE_TCP_CLIENT_H */ |