Coverage Report

Created: 2025-10-08 19:34

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