Coverage Report

Created: 2025-10-08 19:34

/work/toxcore/TCP_common.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
#ifndef C_TOXCORE_TOXCORE_TCP_COMMON_H
7
#define C_TOXCORE_TOXCORE_TCP_COMMON_H
8
9
#include "attributes.h"
10
#include "crypto_core.h"
11
#include "logger.h"
12
#include "mem.h"
13
#include "net_profile.h"
14
#include "network.h"
15
16
typedef struct TCP_Priority_List TCP_Priority_List;
17
struct TCP_Priority_List {
18
    TCP_Priority_List *_Nullable next;
19
    uint16_t size;
20
    uint16_t sent;
21
    uint8_t *_Nonnull data;
22
};
23
24
void wipe_priority_list(const Memory *_Nonnull mem, TCP_Priority_List *_Nullable p);
25
49.2k
#define NUM_RESERVED_PORTS 16
26
43.2k
#define NUM_CLIENT_CONNECTIONS (256 - NUM_RESERVED_PORTS)
27
28
typedef enum Tcp_Packet {
29
    TCP_PACKET_ROUTING_REQUEST          = 0,
30
    TCP_PACKET_ROUTING_RESPONSE         = 1,
31
    TCP_PACKET_CONNECTION_NOTIFICATION  = 2,
32
    TCP_PACKET_DISCONNECT_NOTIFICATION  = 3,
33
    TCP_PACKET_PING                     = 4,
34
    TCP_PACKET_PONG                     = 5,
35
    TCP_PACKET_OOB_SEND                 = 6,
36
    TCP_PACKET_OOB_RECV                 = 7,
37
    TCP_PACKET_ONION_REQUEST            = 8,
38
    TCP_PACKET_ONION_RESPONSE           = 9,
39
    TCP_PACKET_FORWARD_REQUEST          = 10,
40
    TCP_PACKET_FORWARDING               = 11,
41
} Tcp_Packet;
42
43
678
#define TCP_HANDSHAKE_PLAIN_SIZE (CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE)
44
417
#define TCP_SERVER_HANDSHAKE_SIZE (CRYPTO_NONCE_SIZE + TCP_HANDSHAKE_PLAIN_SIZE + CRYPTO_MAC_SIZE)
45
225
#define TCP_CLIENT_HANDSHAKE_SIZE (CRYPTO_PUBLIC_KEY_SIZE + TCP_SERVER_HANDSHAKE_SIZE)
46
346
#define TCP_MAX_OOB_DATA_LENGTH 1024
47
48
/** frequency to ping connected nodes and timeout in seconds */
49
24.5k
#define TCP_PING_FREQUENCY 30
50
366
#define TCP_PING_TIMEOUT 10
51
52
9.01k
#define MAX_PACKET_SIZE 2048
53
54
typedef struct TCP_Connection {
55
    const Memory *_Nonnull mem;
56
    const Random *_Nonnull rng;
57
    const Network *_Nonnull ns;
58
    Socket sock;
59
    IP_Port ip_port;  // for debugging.
60
    uint8_t sent_nonce[CRYPTO_NONCE_SIZE]; /* Nonce of sent packets. */
61
    uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE];
62
    uint8_t last_packet[2 + MAX_PACKET_SIZE];
63
    uint16_t last_packet_length;
64
    uint16_t last_packet_sent;
65
66
    TCP_Priority_List *_Nullable priority_queue_start;
67
    TCP_Priority_List *_Nullable priority_queue_end;
68
69
    // This is a shared pointer to the parent's respective Net_Profile object
70
    // (either TCP_Server for TCP server packets or TCP_Connections for TCP client packets).
71
    Net_Profile *_Nullable net_profile;
72
} TCP_Connection;
73
74
/**
75
 * @retval 0 if pending data was sent completely
76
 * @retval -1 if it wasn't
77
 */
78
int send_pending_data_nonpriority(const Logger *_Nonnull logger, TCP_Connection *_Nonnull con);
79
80
/**
81
 * @retval 0 if pending data was sent completely
82
 * @retval -1 if it wasn't
83
 */
84
int send_pending_data(const Logger *_Nonnull logger, TCP_Connection *_Nonnull con);
85
86
/**
87
 * @retval 1 on success.
88
 * @retval 0 if could not send packet.
89
 * @retval -1 on failure (connection must be killed).
90
 */
91
int write_packet_tcp_secure_connection(const Logger *_Nonnull logger, TCP_Connection *_Nonnull con, const uint8_t *_Nonnull data, uint16_t length, 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
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,
99
                    const IP_Port *_Nonnull ip_port);
100
101
/**
102
 * @return length of received packet on success.
103
 * @retval 0 if could not read any packet.
104
 * @retval -1 on failure (connection must be killed).
105
 */
106
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,
107
                                      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);
108
109
#endif /* C_TOXCORE_TOXCORE_TCP_COMMON_H */