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 server part of Tox.
8  */
9 #ifndef C_TOXCORE_TOXCORE_TCP_SERVER_H
10 #define C_TOXCORE_TOXCORE_TCP_SERVER_H
11 
12 #include "crypto_core.h"
13 #include "list.h"
14 #include "onion.h"
15 
16 #define MAX_INCOMING_CONNECTIONS 256
17 
18 #define TCP_MAX_BACKLOG MAX_INCOMING_CONNECTIONS
19 
20 #define MAX_PACKET_SIZE 2048
21 
22 #define TCP_HANDSHAKE_PLAIN_SIZE (CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE)
23 #define TCP_SERVER_HANDSHAKE_SIZE (CRYPTO_NONCE_SIZE + TCP_HANDSHAKE_PLAIN_SIZE + CRYPTO_MAC_SIZE)
24 #define TCP_CLIENT_HANDSHAKE_SIZE (CRYPTO_PUBLIC_KEY_SIZE + TCP_SERVER_HANDSHAKE_SIZE)
25 #define TCP_MAX_OOB_DATA_LENGTH 1024
26 
27 #define NUM_RESERVED_PORTS 16
28 #define NUM_CLIENT_CONNECTIONS (256 - NUM_RESERVED_PORTS)
29 
30 #define TCP_PACKET_ROUTING_REQUEST  0
31 #define TCP_PACKET_ROUTING_RESPONSE 1
32 #define TCP_PACKET_CONNECTION_NOTIFICATION 2
33 #define TCP_PACKET_DISCONNECT_NOTIFICATION 3
34 #define TCP_PACKET_PING 4
35 #define TCP_PACKET_PONG 5
36 #define TCP_PACKET_OOB_SEND 6
37 #define TCP_PACKET_OOB_RECV 7
38 #define TCP_PACKET_ONION_REQUEST  8
39 #define TCP_PACKET_ONION_RESPONSE 9
40 
41 #define ARRAY_ENTRY_SIZE 6
42 
43 /* frequency to ping connected nodes and timeout in seconds */
44 #define TCP_PING_FREQUENCY 30
45 #define TCP_PING_TIMEOUT 10
46 
47 typedef enum TCP_Status {
48     TCP_STATUS_NO_STATUS,
49     TCP_STATUS_CONNECTED,
50     TCP_STATUS_UNCONFIRMED,
51     TCP_STATUS_CONFIRMED,
52 } TCP_Status;
53 
54 typedef struct TCP_Priority_List TCP_Priority_List;
55 
56 struct TCP_Priority_List {
57     TCP_Priority_List *next;
58     uint16_t size;
59     uint16_t sent;
60     uint8_t data[];
61 };
62 
63 void wipe_priority_list(TCP_Priority_List *p);
64 
65 typedef struct TCP_Server TCP_Server;
66 
67 const uint8_t *tcp_server_public_key(const TCP_Server *tcp_server);
68 size_t tcp_server_listen_count(const TCP_Server *tcp_server);
69 
70 /* Create new TCP server instance.
71  */
72 TCP_Server *new_TCP_server(const Logger *logger, uint8_t ipv6_enabled, uint16_t num_sockets, const uint16_t *ports,
73                            const uint8_t *secret_key, Onion *onion);
74 
75 /* Run the TCP_server
76  */
77 void do_TCP_server(TCP_Server *tcp_server, Mono_Time *mono_time);
78 
79 /* Kill the TCP server
80  */
81 void kill_TCP_server(TCP_Server *tcp_server);
82 
83 /* Read the next two bytes in TCP stream then convert them to
84  * length (host byte order).
85  *
86  * return length on success
87  * return 0 if nothing has been read from socket.
88  * return -1 on failure.
89  */
90 uint16_t read_TCP_length(const Logger *logger, Socket sock);
91 
92 /* Read length bytes from socket.
93  *
94  * return length on success
95  * return -1 on failure/no data in buffer.
96  */
97 int read_TCP_packet(const Logger *logger, Socket sock, uint8_t *data, uint16_t length);
98 
99 /* return length of received packet on success.
100  * return 0 if could not read any packet.
101  * return -1 on failure (connection must be killed).
102  */
103 int read_packet_TCP_secure_connection(const Logger *logger, Socket sock, uint16_t *next_packet_length,
104                                       const uint8_t *shared_key, uint8_t *recv_nonce, uint8_t *data, uint16_t max_len);
105 
106 
107 #endif
108