Coverage Report

Created: 2026-10-24 08:16

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