/work/toxcore/onion_client.h
Line | Count | Source |
1 | | /* SPDX-License-Identifier: GPL-3.0-or-later |
2 | | * Copyright © 2016-2018 The TokTok team. |
3 | | * Copyright © 2013 Tox project. |
4 | | */ |
5 | | |
6 | | /** |
7 | | * Implementation of the client part of docs/Prevent_Tracking.txt (The part that |
8 | | * uses the onion stuff to connect to the friend) |
9 | | */ |
10 | | #ifndef C_TOXCORE_TOXCORE_ONION_CLIENT_H |
11 | | #define C_TOXCORE_TOXCORE_ONION_CLIENT_H |
12 | | |
13 | | #include <stdbool.h> |
14 | | |
15 | | #include "net_crypto.h" |
16 | | #include "onion_announce.h" |
17 | | #include "ping_array.h" |
18 | | |
19 | 323k | #define MAX_ONION_CLIENTS 8 |
20 | 958k | #define MAX_ONION_CLIENTS_ANNOUNCE 12 // Number of nodes to announce ourselves to. |
21 | 348k | #define ONION_NODE_PING_INTERVAL 15 |
22 | 49.8k | #define ONION_NODE_TIMEOUT ONION_NODE_PING_INTERVAL |
23 | | |
24 | | /** The interval in seconds at which to tell our friends where we are */ |
25 | 9.47k | #define ONION_DHTPK_SEND_INTERVAL 30 |
26 | 9.47k | #define DHT_DHTPK_SEND_INTERVAL 20 |
27 | | |
28 | 775k | #define NUMBER_ONION_PATHS 6 |
29 | | |
30 | | /** |
31 | | * The timeout the first time the path is added and |
32 | | * then for all the next consecutive times |
33 | | */ |
34 | 47.0k | #define ONION_PATH_FIRST_TIMEOUT 4 |
35 | 385k | #define ONION_PATH_TIMEOUT 10 |
36 | 201k | #define ONION_PATH_MAX_LIFETIME 1200 |
37 | 411k | #define ONION_PATH_MAX_NO_RESPONSE_USES 4 |
38 | | |
39 | 189k | #define MAX_STORED_PINGED_NODES 9 |
40 | 164k | #define MIN_NODE_PING_TIME 10 |
41 | | |
42 | 2.46M | #define ONION_NODE_MAX_PINGS 3 |
43 | | |
44 | 1.70M | #define MAX_PATH_NODES 32 |
45 | | |
46 | 276 | #define GCA_MAX_DATA_LENGTH GCA_PUBLIC_ANNOUNCE_MAX_SIZE |
47 | | |
48 | | /** |
49 | | * If no announce response packets are received within this interval tox will |
50 | | * be considered offline. We give time for a node to be pinged often enough |
51 | | * that it times out, which leads to the network being thoroughly tested as it |
52 | | * is replaced. |
53 | | */ |
54 | 36.9k | #define ONION_OFFLINE_TIMEOUT (ONION_NODE_PING_INTERVAL * (ONION_NODE_MAX_PINGS+2)) |
55 | | |
56 | | /** Onion data packet ids. */ |
57 | | #define ONION_DATA_FRIEND_REQ CRYPTO_PACKET_FRIEND_REQ |
58 | 21.0k | #define ONION_DATA_DHTPK CRYPTO_PACKET_DHTPK |
59 | | |
60 | | typedef struct Onion_Client Onion_Client; |
61 | | |
62 | | non_null() |
63 | | DHT *onion_get_dht(const Onion_Client *onion_c); |
64 | | non_null() |
65 | | Net_Crypto *onion_get_net_crypto(const Onion_Client *onion_c); |
66 | | |
67 | | /** @brief Add a node to the path_nodes bootstrap array. |
68 | | * |
69 | | * If a node with the given public key was already in the bootstrap array, this function has no |
70 | | * effect and returns successfully. There is currently no way to update the IP/port for a bootstrap |
71 | | * node, so if it changes, the Onion_Client must be recreated. |
72 | | * |
73 | | * @param onion_c The onion client object. |
74 | | * @param ip_port IP/port for the bootstrap node. |
75 | | * @param public_key DHT public key for the bootstrap node. |
76 | | * |
77 | | * @retval false on failure |
78 | | * @retval true on success |
79 | | */ |
80 | | non_null() |
81 | | bool onion_add_bs_path_node(Onion_Client *onion_c, const IP_Port *ip_port, const uint8_t *public_key); |
82 | | |
83 | | /** @brief Put up to max_num nodes in nodes. |
84 | | * |
85 | | * return the number of nodes. |
86 | | */ |
87 | | non_null() |
88 | | uint16_t onion_backup_nodes(const Onion_Client *onion_c, Node_format *nodes, uint16_t max_num); |
89 | | |
90 | | /** @brief Get the friend_num of a friend. |
91 | | * |
92 | | * return -1 on failure. |
93 | | * return friend number on success. |
94 | | */ |
95 | | non_null() |
96 | | int onion_friend_num(const Onion_Client *onion_c, const uint8_t *public_key); |
97 | | |
98 | | /** @brief Add a friend who we want to connect to. |
99 | | * |
100 | | * return -1 on failure. |
101 | | * return the friend number on success or if the friend was already added. |
102 | | */ |
103 | | non_null() |
104 | | int onion_addfriend(Onion_Client *onion_c, const uint8_t *public_key); |
105 | | |
106 | | /** @brief Delete a friend. |
107 | | * |
108 | | * return -1 on failure. |
109 | | * return the deleted friend number on success. |
110 | | */ |
111 | | non_null() |
112 | | int onion_delfriend(Onion_Client *onion_c, int friend_num); |
113 | | |
114 | | /** @brief Set if friend is online or not. |
115 | | * |
116 | | * NOTE: This function is there and should be used so that we don't send |
117 | | * useless packets to the friend if they are online. |
118 | | * |
119 | | * return -1 on failure. |
120 | | * return 0 on success. |
121 | | */ |
122 | | non_null() |
123 | | int onion_set_friend_online(Onion_Client *onion_c, int friend_num, bool is_online); |
124 | | |
125 | | /** @brief Get the ip of friend friendnum and put it in ip_port |
126 | | * |
127 | | * @retval -1 if public_key does NOT refer to a friend |
128 | | * @retval 0 if public_key refers to a friend and we failed to find the friend (yet) |
129 | | * @retval 1 if public_key refers to a friend and we found them |
130 | | */ |
131 | | non_null() |
132 | | int onion_getfriendip(const Onion_Client *onion_c, int friend_num, IP_Port *ip_port); |
133 | | |
134 | | typedef int recv_tcp_relay_cb(void *object, uint32_t number, const IP_Port *ip_port, const uint8_t *public_key); |
135 | | |
136 | | /** @brief Set the function for this friend that will be callbacked with object and number |
137 | | * when that friend gives us one of the TCP relays they are connected to. |
138 | | * |
139 | | * object and number will be passed as argument to this function. |
140 | | * |
141 | | * return -1 on failure. |
142 | | * return 0 on success. |
143 | | */ |
144 | | non_null() |
145 | | int recv_tcp_relay_handler(Onion_Client *onion_c, int friend_num, |
146 | | recv_tcp_relay_cb *callback, void *object, uint32_t number); |
147 | | |
148 | | typedef void onion_dht_pk_cb(void *object, int32_t number, const uint8_t *dht_public_key, void *userdata); |
149 | | |
150 | | /** @brief Set the function for this friend that will be callbacked with object and number |
151 | | * when that friend gives us their DHT temporary public key. |
152 | | * |
153 | | * object and number will be passed as argument to this function. |
154 | | * |
155 | | * return -1 on failure. |
156 | | * return 0 on success. |
157 | | */ |
158 | | non_null() |
159 | | int onion_dht_pk_callback(Onion_Client *onion_c, int friend_num, onion_dht_pk_cb *function, void *object, |
160 | | uint32_t number); |
161 | | |
162 | | /** @brief Set a friend's DHT public key. |
163 | | * |
164 | | * return -1 on failure. |
165 | | * return 0 on success. |
166 | | */ |
167 | | non_null() |
168 | | int onion_set_friend_dht_pubkey(Onion_Client *onion_c, int friend_num, const uint8_t *dht_key); |
169 | | |
170 | | /** @brief Copy friends DHT public key into dht_key. |
171 | | * |
172 | | * return 0 on failure (no key copied). |
173 | | * return 1 on success (key copied). |
174 | | */ |
175 | | non_null() |
176 | | unsigned int onion_getfriend_dht_pubkey(const Onion_Client *onion_c, int friend_num, uint8_t *dht_key); |
177 | | |
178 | 46.7k | #define ONION_DATA_IN_RESPONSE_MIN_SIZE (CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE) |
179 | 24.5k | #define ONION_CLIENT_MAX_DATA_SIZE (MAX_DATA_REQUEST_SIZE - ONION_DATA_IN_RESPONSE_MIN_SIZE) |
180 | | |
181 | | /** @brief Send data of length length to friendnum. |
182 | | * Maximum length of data is ONION_CLIENT_MAX_DATA_SIZE. |
183 | | * This data will be received by the friend using the Onion_Data_Handlers callbacks. |
184 | | * |
185 | | * Even if this function succeeds, the friend might not receive any data. |
186 | | * |
187 | | * return the number of packets sent on success |
188 | | * return -1 on failure. |
189 | | */ |
190 | | non_null() |
191 | | int send_onion_data(Onion_Client *onion_c, int friend_num, const uint8_t *data, uint16_t length); |
192 | | |
193 | | typedef int oniondata_handler_cb(void *object, const uint8_t *source_pubkey, const uint8_t *data, |
194 | | uint16_t length, void *userdata); |
195 | | |
196 | | /** Function to call when onion data packet with contents beginning with byte is received. */ |
197 | | non_null(1) nullable(3, 4) |
198 | | void oniondata_registerhandler(Onion_Client *onion_c, uint8_t byte, oniondata_handler_cb *cb, void *object); |
199 | | |
200 | | typedef bool onion_group_announce_cb(Onion_Client *onion_c, uint32_t sendback_num, const uint8_t *data, |
201 | | size_t data_length, void *user_data); |
202 | | |
203 | | /** Function to call when the onion gets a group announce response. */ |
204 | | non_null(1) nullable(2, 3) |
205 | | void onion_group_announce_register(Onion_Client *onion_c, onion_group_announce_cb *func, void *user_data); |
206 | | |
207 | | non_null() |
208 | | void do_onion_client(Onion_Client *onion_c); |
209 | | |
210 | | non_null() |
211 | | Onion_Client *new_onion_client(const Logger *logger, const Memory *mem, const Random *rng, const Mono_Time *mono_time, Net_Crypto *c); |
212 | | |
213 | | nullable(1) |
214 | | void kill_onion_client(Onion_Client *onion_c); |
215 | | |
216 | | |
217 | | typedef enum Onion_Connection_Status { |
218 | | /** We are not connected to the network. */ |
219 | | ONION_CONNECTION_STATUS_NONE = 0, |
220 | | /** We are connected with TCP only. */ |
221 | | ONION_CONNECTION_STATUS_TCP = 1, |
222 | | /** We are also connected with UDP. */ |
223 | | ONION_CONNECTION_STATUS_UDP = 2, |
224 | | } Onion_Connection_Status; |
225 | | |
226 | | non_null() |
227 | | Onion_Connection_Status onion_connection_status(const Onion_Client *onion_c); |
228 | | |
229 | | typedef struct Onion_Friend Onion_Friend; |
230 | | |
231 | | non_null() uint16_t onion_get_friend_count(const Onion_Client *onion_c); |
232 | | non_null() Onion_Friend *onion_get_friend(const Onion_Client *onion_c, uint16_t friend_num); |
233 | | non_null() const uint8_t *onion_friend_get_gc_public_key(const Onion_Friend *onion_friend); |
234 | | non_null() const uint8_t *onion_friend_get_gc_public_key_num(const Onion_Client *onion_c, uint32_t num); |
235 | | non_null() void onion_friend_set_gc_public_key(Onion_Friend *onion_friend, const uint8_t *public_key); |
236 | | non_null(1) nullable(2) |
237 | | void onion_friend_set_gc_data(Onion_Friend *onion_friend, const uint8_t *gc_data, uint16_t gc_data_length); |
238 | | non_null() bool onion_friend_is_groupchat(const Onion_Friend *onion_friend); |
239 | | |
240 | | #endif /* C_TOXCORE_TOXCORE_ONION_CLIENT_H */ |