/work/toxcore/TCP_common.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 | | #ifndef C_TOXCORE_TOXCORE_TCP_COMMON_H |
7 | | #define C_TOXCORE_TOXCORE_TCP_COMMON_H |
8 | | |
9 | | #include <stdbool.h> |
10 | | #include <stdint.h> |
11 | | |
12 | | #include "attributes.h" |
13 | | #include "crypto_core.h" |
14 | | #include "logger.h" |
15 | | #include "mem.h" |
16 | | #include "net.h" |
17 | | #include "net_profile.h" |
18 | | #include "network.h" |
19 | | #include "rng.h" |
20 | | |
21 | | #ifdef __cplusplus |
22 | | extern "C" { |
23 | | #endif |
24 | | |
25 | | typedef struct TCP_Priority_List TCP_Priority_List; |
26 | | struct TCP_Priority_List { |
27 | | TCP_Priority_List *_Nullable next; |
28 | | uint16_t size; |
29 | | uint16_t sent; |
30 | | uint8_t *_Nonnull data; |
31 | | }; |
32 | | |
33 | | void wipe_priority_list(const Memory *_Nonnull mem, TCP_Priority_List *_Nullable p); |
34 | 52.5k | #define NUM_RESERVED_PORTS 16 |
35 | 44.3k | #define NUM_CLIENT_CONNECTIONS (256 - NUM_RESERVED_PORTS) |
36 | | |
37 | | typedef enum Tcp_Packet { |
38 | | TCP_PACKET_ROUTING_REQUEST = 0, |
39 | | TCP_PACKET_ROUTING_RESPONSE = 1, |
40 | | TCP_PACKET_CONNECTION_NOTIFICATION = 2, |
41 | | TCP_PACKET_DISCONNECT_NOTIFICATION = 3, |
42 | | TCP_PACKET_PING = 4, |
43 | | TCP_PACKET_PONG = 5, |
44 | | TCP_PACKET_OOB_SEND = 6, |
45 | | TCP_PACKET_OOB_RECV = 7, |
46 | | TCP_PACKET_ONION_REQUEST = 8, |
47 | | TCP_PACKET_ONION_RESPONSE = 9, |
48 | | TCP_PACKET_FORWARD_REQUEST = 10, |
49 | | TCP_PACKET_FORWARDING = 11, |
50 | | } Tcp_Packet; |
51 | | |
52 | | const char *_Nonnull tcp_packet_type_to_string(Tcp_Packet type); |
53 | | bool tcp_packet_from_int(uint32_t value, Tcp_Packet *_Nonnull out_enum); |
54 | | |
55 | 606 | #define TCP_HANDSHAKE_PLAIN_SIZE (CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE) |
56 | 337 | #define TCP_SERVER_HANDSHAKE_SIZE (CRYPTO_NONCE_SIZE + TCP_HANDSHAKE_PLAIN_SIZE + CRYPTO_MAC_SIZE) |
57 | 137 | #define TCP_CLIENT_HANDSHAKE_SIZE (CRYPTO_PUBLIC_KEY_SIZE + TCP_SERVER_HANDSHAKE_SIZE) |
58 | 403 | #define TCP_MAX_OOB_DATA_LENGTH 1024 |
59 | | |
60 | | /** frequency to ping connected nodes and timeout in seconds */ |
61 | 42.2k | #define TCP_PING_FREQUENCY 30 |
62 | 355 | #define TCP_PING_TIMEOUT 10 |
63 | | |
64 | 41.3k | #define MAX_PACKET_SIZE 2048 |
65 | | |
66 | | typedef struct TCP_Connection { |
67 | | const Memory *_Nonnull mem; |
68 | | const Random *_Nonnull rng; |
69 | | const Network *_Nonnull ns; |
70 | | Socket sock; |
71 | | IP_Port ip_port; // for debugging. |
72 | | uint8_t sent_nonce[CRYPTO_NONCE_SIZE]; /* Nonce of sent packets. */ |
73 | | uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE]; |
74 | | uint8_t last_packet[2 + MAX_PACKET_SIZE]; |
75 | | uint16_t last_packet_length; |
76 | | uint16_t last_packet_sent; |
77 | | |
78 | | TCP_Priority_List *_Nullable priority_queue_start; |
79 | | TCP_Priority_List *_Nullable priority_queue_end; |
80 | | |
81 | | // This is a shared pointer to the parent's respective Net_Profile object |
82 | | // (either TCP_Server for TCP server packets or TCP_Connections for TCP client packets). |
83 | | Net_Profile *_Nullable net_profile; |
84 | | } TCP_Connection; |
85 | | |
86 | | /** |
87 | | * @retval 0 if pending data was sent completely |
88 | | * @retval -1 if it wasn't |
89 | | */ |
90 | | int send_pending_data_nonpriority(const Logger *_Nonnull logger, TCP_Connection *_Nonnull con); |
91 | | |
92 | | /** |
93 | | * @retval 0 if pending data was sent completely |
94 | | * @retval -1 if it wasn't |
95 | | */ |
96 | | int send_pending_data(const Logger *_Nonnull logger, TCP_Connection *_Nonnull con); |
97 | | |
98 | | /** |
99 | | * @retval 1 on success. |
100 | | * @retval 0 if could not send packet. |
101 | | * @retval -1 on failure (connection must be killed). |
102 | | */ |
103 | | int write_packet_tcp_secure_connection(const Logger *_Nonnull logger, TCP_Connection *_Nonnull con, const uint8_t *_Nonnull data, uint16_t length, bool priority); |
104 | | |
105 | | /** @brief Read length bytes from socket. |
106 | | * |
107 | | * return length on success |
108 | | * return -1 on failure/no data in buffer. |
109 | | */ |
110 | | int read_tcp_packet(const Logger *_Nonnull logger, const Memory *_Nonnull mem, const Network *_Nonnull ns, Socket sock, uint8_t *_Nonnull data, uint16_t length, |
111 | | const IP_Port *_Nonnull ip_port); |
112 | | |
113 | | /** |
114 | | * @return length of received packet on success. |
115 | | * @retval 0 if could not read any packet. |
116 | | * @retval -1 on failure (connection must be killed). |
117 | | */ |
118 | | int read_packet_tcp_secure_connection(const Logger *_Nonnull logger, const Memory *_Nonnull mem, const Network *_Nonnull ns, Socket sock, uint16_t *_Nonnull next_packet_length, |
119 | | const uint8_t *_Nonnull shared_key, uint8_t *_Nonnull recv_nonce, uint8_t *_Nonnull data, uint16_t max_len, const IP_Port *_Nonnull ip_port); |
120 | | |
121 | | #ifdef __cplusplus |
122 | | } /* extern "C" */ |
123 | | #endif |
124 | | |
125 | | #endif /* C_TOXCORE_TOXCORE_TCP_COMMON_H */ |