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