Coverage Report

Created: 2024-01-26 01:52

/work/toxcore/forwarding.h
Line
Count
Source
1
/* SPDX-License-Identifier: GPL-3.0-or-later
2
 * Copyright © 2019-2022 The TokTok team.
3
 */
4
5
#ifndef C_TOXCORE_TOXCORE_FORWARDING_H
6
#define C_TOXCORE_TOXCORE_FORWARDING_H
7
8
#include "DHT.h"
9
#include "network.h"
10
11
#ifdef __cplusplus
12
extern "C" {
13
#endif
14
15
120
#define SENDBACK_IPPORT 0
16
87
#define SENDBACK_FORWARD 1
17
14
#define SENDBACK_TCP 2
18
19
13.1k
#define MAX_SENDBACK_SIZE (0xff - 1)
20
266
#define MAX_FORWARD_DATA_SIZE (MAX_UDP_PACKET_SIZE - (1 + 1 + MAX_SENDBACK_SIZE))
21
22
126
#define MAX_FORWARD_CHAIN_LENGTH 4
23
24
12.4k
#define MAX_PACKED_IPPORT_SIZE (1 + SIZE_IP6 + sizeof(uint16_t))
25
26
typedef struct Forwarding Forwarding;
27
28
non_null()
29
DHT *forwarding_get_dht(const Forwarding *forwarding);
30
31
/**
32
 * @brief Send data to forwarder for forwarding via chain of dht nodes.
33
 * Destination is last key in the chain.
34
 *
35
 * @param data Must be of length at most MAX_FORWARD_DATA_SIZE.
36
 * @param chain_length Number of intermediate nodes in chain.
37
 *   Must be at least 1 and at most MAX_FORWARD_CHAIN_LENGTH.
38
 * @param chain_keys Public keys of chain nodes. Must be of length
39
 *   `chain_length * CRYPTO_PUBLIC_KEY_SIZE`.
40
 *
41
 * @return true on success, false otherwise.
42
 */
43
non_null()
44
bool send_forward_request(const Networking_Core *net, const IP_Port *forwarder,
45
                          const uint8_t *chain_keys, uint16_t chain_length,
46
                          const uint8_t *data, uint16_t data_length);
47
48
/** Returns size of packet written by create_forward_chain_packet. */
49
uint16_t forward_chain_packet_size(uint16_t chain_length, uint16_t data_length);
50
51
/**
52
 * @brief Create forward request packet for forwarding data via chain of dht nodes.
53
 * Destination is last key in the chain.
54
 *
55
 * @param data Must be of length at most MAX_FORWARD_DATA_SIZE.
56
 * @param chain_length Number of intermediate nodes in chain.
57
 *   Must be at least 1 and at most MAX_FORWARD_CHAIN_LENGTH.
58
 * @param chain_keys Public keys of chain nodes. Must be of length
59
 *   `chain_length * CRYPTO_PUBLIC_KEY_SIZE`.
60
 * @param packet Must be of size at least
61
 *   `forward_chain_packet_size(chain_length, data_length)` bytes.
62
 *
63
 * @return true on success, false otherwise.
64
 */
65
non_null()
66
bool create_forward_chain_packet(const uint8_t *chain_keys, uint16_t chain_length,
67
                                 const uint8_t *data, uint16_t data_length,
68
                                 uint8_t *packet);
69
70
/**
71
 * @brief Send reply to forwarded packet via forwarder.
72
 * @param sendback Must be of size at most MAX_SENDBACK_SIZE.
73
 * @param data Must be of size at most MAX_FORWARD_DATA_SIZE.
74
 *
75
 * @return true on success, false otherwise.
76
 */
77
non_null()
78
bool forward_reply(const Networking_Core *net, const IP_Port *forwarder,
79
                   const uint8_t *sendback, uint16_t sendback_length,
80
                   const uint8_t *data, uint16_t length);
81
82
83
/**
84
 * @brief Set callback to handle a forwarded request.
85
 * To reply to the packet, callback should use `forward_reply()` to send a reply
86
 * forwarded via forwarder, passing the provided sendback.
87
 */
88
typedef void forwarded_request_cb(void *object, const IP_Port *forwarder, const uint8_t *sendback,
89
                                  uint16_t sendback_length, const uint8_t *data,
90
                                  uint16_t length, void *userdata);
91
non_null(1) nullable(2, 3)
92
void set_callback_forwarded_request(Forwarding *forwarding, forwarded_request_cb *function, void *object);
93
94
/** @brief Set callback to handle a forwarded response. */
95
typedef void forwarded_response_cb(void *object, const uint8_t *data, uint16_t length, void *userdata);
96
non_null(1) nullable(2, 3)
97
void set_callback_forwarded_response(Forwarding *forwarding, forwarded_response_cb *function, void *object);
98
99
/** @brief Send forwarding packet to dest with given sendback data and data. */
100
non_null(1, 2, 5) nullable(3)
101
bool send_forwarding(const Forwarding *forwarding, const IP_Port *dest,
102
                     const uint8_t *sendback_data, uint16_t sendback_data_len,
103
                     const uint8_t *data, uint16_t length);
104
105
typedef bool forward_reply_cb(void *object, const uint8_t *sendback_data, uint16_t sendback_data_len,
106
                              const uint8_t *data, uint16_t length);
107
108
/**
109
 * @brief Set callback to handle a forward reply with an otherwise unhandled
110
 * sendback.
111
 */
112
non_null(1) nullable(2, 3)
113
void set_callback_forward_reply(Forwarding *forwarding, forward_reply_cb *function, void *object);
114
115
non_null()
116
Forwarding *new_forwarding(const Logger *log, const Random *rng, const Mono_Time *mono_time, DHT *dht);
117
118
nullable(1)
119
void kill_forwarding(Forwarding *forwarding);
120
121
#ifdef __cplusplus
122
} /* extern "C" */
123
#endif
124
125
#endif /* C_TOXCORE_TOXCORE_FORWARDING_H */