Coverage Report

Created: 2026-10-24 08:16

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