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