Coverage Report

Created: 2025-10-08 19:34

/work/toxcore/tox_private.h
Line
Count
Source (jump to first uncovered line)
1
/* SPDX-License-Identifier: GPL-3.0-or-later
2
 * Copyright © 2016-2025 The TokTok team.
3
 * Copyright © 2013 Tox project.
4
 */
5
6
#ifndef C_TOXCORE_TOXCORE_TOX_PRIVATE_H
7
#define C_TOXCORE_TOXCORE_TOX_PRIVATE_H
8
9
#include <stdbool.h>
10
#include <stddef.h>
11
#include <stdint.h>
12
13
#include "tox.h"
14
#include "tox_options.h"
15
16
#ifdef __cplusplus
17
extern "C" {
18
#endif
19
20
typedef uint64_t tox_mono_time_cb(void *user_data);
21
22
typedef struct Tox_System {
23
    tox_mono_time_cb *mono_time_callback;
24
    void *mono_time_user_data;
25
    const struct Tox_Random *rng;
26
    const struct Network *ns;
27
    const struct Tox_Memory *mem;
28
} Tox_System;
29
30
Tox_System tox_default_system(void);
31
32
const Tox_System *tox_get_system(Tox *tox);
33
34
typedef struct Tox_Options_Testing {
35
    const struct Tox_System *operating_system;
36
} Tox_Options_Testing;
37
38
typedef enum Tox_Err_New_Testing {
39
    TOX_ERR_NEW_TESTING_OK,
40
    TOX_ERR_NEW_TESTING_NULL,
41
} Tox_Err_New_Testing;
42
43
Tox *tox_new_testing(const Tox_Options *options, Tox_Err_New *error, const Tox_Options_Testing *testing, Tox_Err_New_Testing *testing_error);
44
45
void tox_lock(const Tox *tox);
46
void tox_unlock(const Tox *tox);
47
48
/**
49
 * Set the callback for the `friend_lossy_packet` event for a specific packet
50
 * ID. Pass NULL to unset.
51
 *
52
 * allowed packet ID range:
53
 * from `PACKET_ID_RANGE_LOSSY_START` to `PACKET_ID_RANGE_LOSSY_END` (both
54
 * inclusive)
55
 */
56
void tox_callback_friend_lossy_packet_per_pktid(Tox *tox, tox_friend_lossy_packet_cb *callback, uint8_t pktid);
57
58
/**
59
 * Set the callback for the `friend_lossless_packet` event for a specific packet
60
 * ID. Pass NULL to unset.
61
 *
62
 * allowed packet ID range:
63
 * from `PACKET_ID_RANGE_LOSSLESS_CUSTOM_START` to
64
 * `PACKET_ID_RANGE_LOSSLESS_CUSTOM_END` (both inclusive) and `PACKET_ID_MSI`
65
 */
66
void tox_callback_friend_lossless_packet_per_pktid(Tox *tox, tox_friend_lossless_packet_cb *callback, uint8_t pktid);
67
68
void tox_set_av_object(Tox *tox, void *object);
69
void *tox_get_av_object(const Tox *tox);
70
71
/*******************************************************************************
72
 *
73
 * :: DHT network queries.
74
 *
75
 ******************************************************************************/
76
77
/**
78
 * The minimum size of an IP string buffer in bytes.
79
 */
80
1
#define TOX_DHT_NODE_IP_STRING_SIZE      96
81
82
uint32_t tox_dht_node_ip_string_size(void);
83
84
/**
85
 * The size of a DHT node public key in bytes.
86
 */
87
484k
#define TOX_DHT_NODE_PUBLIC_KEY_SIZE     32
88
89
uint32_t tox_dht_node_public_key_size(void);
90
91
/**
92
 * @param public_key The node's public key.
93
 * @param ip The node's IP address, represented as a NUL-terminated C string.
94
 * @param port The node's port.
95
 */
96
typedef void tox_dht_nodes_response_cb(
97
    Tox *tox, const uint8_t *public_key, const char *ip, uint32_t ip_length,
98
    uint16_t port, void *user_data);
99
100
/**
101
 * Set the callback for the `dht_nodes_response` event. Pass NULL to unset.
102
 *
103
 * This event is triggered when a nodes response is received from a DHT peer.
104
 */
105
void tox_callback_dht_nodes_response(Tox *tox, tox_dht_nodes_response_cb *callback);
106
107
typedef enum Tox_Err_Dht_Send_Nodes_Request {
108
    /**
109
     * The function returned successfully.
110
     */
111
    TOX_ERR_DHT_SEND_NODES_REQUEST_OK,
112
113
    /**
114
     * UDP is disabled in Tox options; the DHT can only be queried when UDP is
115
     * enabled.
116
     */
117
    TOX_ERR_DHT_SEND_NODES_REQUEST_UDP_DISABLED,
118
119
    /**
120
     * One of the arguments to the function was NULL when it was not expected.
121
     */
122
    TOX_ERR_DHT_SEND_NODES_REQUEST_NULL,
123
124
    /**
125
     * The supplied port is invalid.
126
     */
127
    TOX_ERR_DHT_SEND_NODES_REQUEST_BAD_PORT,
128
129
    /**
130
     * The supplied IP address is invalid.
131
     */
132
    TOX_ERR_DHT_SEND_NODES_REQUEST_BAD_IP,
133
134
    /**
135
     * The nodes request failed. This usually means the packet failed to
136
     * send.
137
     */
138
    TOX_ERR_DHT_SEND_NODES_REQUEST_FAIL,
139
} Tox_Err_Dht_Send_Nodes_Request;
140
141
/**
142
 * This function sends a nodes request to a DHT node for its peers that
143
 * are "close" to the passed target public key according to the distance metric
144
 * used by the DHT implementation.
145
 *
146
 * @param public_key The public key of the node that we wish to query. This key
147
 *   must be at least `TOX_DHT_NODE_PUBLIC_KEY_SIZE` bytes in length.
148
 * @param ip A NUL-terminated C string representing the IP address of the node
149
 *   we wish to query.
150
 * @param port The port of the node we wish to query.
151
 * @param target_public_key The public key for which we want to find close
152
 *   nodes.
153
 *
154
 * @return true on success.
155
 */
156
bool tox_dht_send_nodes_request(const Tox *tox, const uint8_t *public_key, const char *ip, uint16_t port,
157
                                const uint8_t *target_public_key, Tox_Err_Dht_Send_Nodes_Request *error);
158
159
/**
160
 * This function returns the number of DHT nodes in the closelist.
161
 *
162
 * @return number
163
 */
164
uint16_t tox_dht_get_num_closelist(const Tox *tox);
165
166
/**
167
 * This function returns the number of DHT nodes in the closelist
168
 * that are capable of storing announce data (introduced in version 0.2.18).
169
 *
170
 * @return number
171
 */
172
uint16_t tox_dht_get_num_closelist_announce_capable(const Tox *tox);
173
174
/*******************************************************************************
175
 *
176
 * :: Network profiler
177
 *
178
 ******************************************************************************/
179
180
181
/**
182
 * Represents all of the network packet identifiers that Toxcore uses.
183
 *
184
 * Notes:
185
 * - Some packet ID's have different purposes depending on the
186
 * packet type. These ID's are given numeral names.
187
 *
188
 * - Queries for invalid packet ID's return undefined results. For example,
189
 *   querying a TCP-exclusive packet ID for UDP, or querying an ID that
190
 *   doesn't exist in this enum.
191
 */
192
typedef enum Tox_Netprof_Packet_Id {
193
    /**
194
     * Ping request packet (UDP).
195
     * Routing request (TCP).
196
     */
197
    TOX_NETPROF_PACKET_ID_ZERO                 = 0x00,
198
199
    /**
200
     * Ping response packet (UDP).
201
     * Routing response (TCP).
202
     */
203
    TOX_NETPROF_PACKET_ID_ONE                  = 0x01,
204
205
    /**
206
     * Nodes request packet (UDP).
207
     * Connection notification (TCP).
208
     */
209
    TOX_NETPROF_PACKET_ID_TWO                  = 0x02,
210
211
    /**
212
     * TCP disconnect notification.
213
     */
214
    TOX_NETPROF_PACKET_ID_TCP_DISCONNECT       = 0x03,
215
216
    /**
217
     * Nodes response packet (UDP).
218
     * Ping packet (TCP).
219
     */
220
    TOX_NETPROF_PACKET_ID_FOUR                 = 0x04,
221
222
    /**
223
     * TCP pong packet.
224
     */
225
    TOX_NETPROF_PACKET_ID_TCP_PONG             = 0x05,
226
227
    /**
228
     * TCP out-of-band send packet.
229
     */
230
    TOX_NETPROF_PACKET_ID_TCP_OOB_SEND         = 0x06,
231
232
    /**
233
     * TCP out-of-band receive packet.
234
     */
235
    TOX_NETPROF_PACKET_ID_TCP_OOB_RECV         = 0x07,
236
237
    /**
238
     * TCP onion request packet.
239
     */
240
    TOX_NETPROF_PACKET_ID_TCP_ONION_REQUEST    = 0x08,
241
242
    /**
243
     * TCP onion response packet.
244
     */
245
    TOX_NETPROF_PACKET_ID_TCP_ONION_RESPONSE   = 0x09,
246
247
    /**
248
     * TCP data packet.
249
     */
250
    TOX_NETPROF_PACKET_ID_TCP_DATA             = 0x10,
251
252
    /**
253
     * Cookie request packet.
254
     */
255
    TOX_NETPROF_PACKET_ID_COOKIE_REQUEST       = 0x18,
256
257
    /**
258
     * Cookie response packet.
259
     */
260
    TOX_NETPROF_PACKET_ID_COOKIE_RESPONSE      = 0x19,
261
262
    /**
263
     * Crypto handshake packet.
264
     */
265
    TOX_NETPROF_PACKET_ID_CRYPTO_HS            = 0x1a,
266
267
    /**
268
     * Crypto data packet.
269
     */
270
    TOX_NETPROF_PACKET_ID_CRYPTO_DATA          = 0x1b,
271
272
    /**
273
     * Encrypted data packet.
274
     */
275
    TOX_NETPROF_PACKET_ID_CRYPTO               = 0x20,
276
277
    /**
278
     * LAN discovery packet.
279
     */
280
    TOX_NETPROF_PACKET_ID_LAN_DISCOVERY        = 0x21,
281
282
    /**
283
     * DHT groupchat packets.
284
     */
285
    TOX_NETPROF_PACKET_ID_GC_HANDSHAKE         = 0x5a,
286
    TOX_NETPROF_PACKET_ID_GC_LOSSLESS          = 0x5b,
287
    TOX_NETPROF_PACKET_ID_GC_LOSSY             = 0x5c,
288
289
    /**
290
     * Onion send packets.
291
     */
292
    TOX_NETPROF_PACKET_ID_ONION_SEND_INITIAL   = 0x80,
293
    TOX_NETPROF_PACKET_ID_ONION_SEND_1         = 0x81,
294
    TOX_NETPROF_PACKET_ID_ONION_SEND_2         = 0x82,
295
296
    /**
297
     * DHT announce request packet (deprecated).
298
     */
299
    TOX_NETPROF_PACKET_ID_ANNOUNCE_REQUEST_OLD = 0x83,
300
301
    /**
302
     * DHT announce response packet (deprecated).
303
     */
304
    TOX_NETPROF_PACKET_ID_ANNOUNCE_RESPONSE_OLD = 0x84,
305
306
    /**
307
     * Onion data request packet.
308
     */
309
    TOX_NETPROF_PACKET_ID_ONION_DATA_REQUEST   = 0x85,
310
311
    /**
312
     * Onion data response packet.
313
     */
314
    TOX_NETPROF_PACKET_ID_ONION_DATA_RESPONSE  = 0x86,
315
316
    /**
317
     * DHT announce request packet.
318
     */
319
    TOX_NETPROF_PACKET_ID_ANNOUNCE_REQUEST     = 0x87,
320
321
    /**
322
     * DHT announce response packet.
323
     */
324
    TOX_NETPROF_PACKET_ID_ANNOUNCE_RESPONSE    = 0x88,
325
326
    /**
327
     * Onion receive packets.
328
     */
329
    TOX_NETPROF_PACKET_ID_ONION_RECV_3         = 0x8c,
330
    TOX_NETPROF_PACKET_ID_ONION_RECV_2         = 0x8d,
331
    TOX_NETPROF_PACKET_ID_ONION_RECV_1         = 0x8e,
332
333
    TOX_NETPROF_PACKET_ID_FORWARD_REQUEST      = 0x90,
334
    TOX_NETPROF_PACKET_ID_FORWARDING           = 0x91,
335
    TOX_NETPROF_PACKET_ID_FORWARD_REPLY        = 0x92,
336
337
    TOX_NETPROF_PACKET_ID_DATA_SEARCH_REQUEST     = 0x93,
338
    TOX_NETPROF_PACKET_ID_DATA_SEARCH_RESPONSE    = 0x94,
339
    TOX_NETPROF_PACKET_ID_DATA_RETRIEVE_REQUEST   = 0x95,
340
    TOX_NETPROF_PACKET_ID_DATA_RETRIEVE_RESPONSE  = 0x96,
341
    TOX_NETPROF_PACKET_ID_STORE_ANNOUNCE_REQUEST  = 0x97,
342
    TOX_NETPROF_PACKET_ID_STORE_ANNOUNCE_RESPONSE = 0x98,
343
344
    /**
345
     * Bootstrap info packet.
346
     */
347
    TOX_NETPROF_PACKET_ID_BOOTSTRAP_INFO       = 0xf0,
348
} Tox_Netprof_Packet_Id;
349
350
const char *tox_netprof_packet_id_to_string(Tox_Netprof_Packet_Id value);
351
352
/**
353
 * Specifies the packet type for a given query.
354
 */
355
typedef enum Tox_Netprof_Packet_Type {
356
    /**
357
     * TCP client packets.
358
     */
359
    TOX_NETPROF_PACKET_TYPE_TCP_CLIENT,
360
361
    /**
362
     * TCP server packets.
363
     */
364
    TOX_NETPROF_PACKET_TYPE_TCP_SERVER,
365
366
    /**
367
     * Combined TCP server and TCP client packets.
368
     */
369
    TOX_NETPROF_PACKET_TYPE_TCP,
370
371
    /**
372
     * UDP packets.
373
     */
374
    TOX_NETPROF_PACKET_TYPE_UDP,
375
} Tox_Netprof_Packet_Type;
376
377
const char *tox_netprof_packet_type_to_string(Tox_Netprof_Packet_Type value);
378
379
/**
380
 * Specifies the packet direction for a given query.
381
 */
382
typedef enum Tox_Netprof_Direction {
383
    /**
384
     * Outbound packets.
385
     */
386
    TOX_NETPROF_DIRECTION_SENT,
387
388
    /**
389
     * Inbound packets.
390
     */
391
    TOX_NETPROF_DIRECTION_RECV,
392
} Tox_Netprof_Direction;
393
394
const char *tox_netprof_direction_to_string(Tox_Netprof_Direction value);
395
396
/**
397
 * Return the number of packets sent or received for a specific packet ID.
398
 *
399
 * @param type The types of packets being queried.
400
 * @param id The packet ID being queried.
401
 * @param direction The packet direction.
402
 */
403
uint64_t tox_netprof_get_packet_id_count(const Tox *tox, Tox_Netprof_Packet_Type type, uint8_t id,
404
        Tox_Netprof_Direction direction);
405
406
/**
407
 * Return the total number of packets sent or received.
408
 *
409
 * @param type The types of packets being queried.
410
 * @param direction The packet direction.
411
 */
412
uint64_t tox_netprof_get_packet_total_count(const Tox *tox, Tox_Netprof_Packet_Type type,
413
        Tox_Netprof_Direction direction);
414
415
/**
416
 * Return the number of bytes sent or received for a specific packet ID.
417
 *
418
 * @param type The types of packets being queried.
419
 * @param id The packet ID being queried.
420
 * @param direction The packet direction.
421
 */
422
uint64_t tox_netprof_get_packet_id_bytes(const Tox *tox, Tox_Netprof_Packet_Type type, uint8_t id,
423
        Tox_Netprof_Direction direction);
424
425
/**
426
 * Return the total number of bytes sent or received.
427
 *
428
 * @param type The types of packets being queried.
429
 * @param direction The packet direction.
430
 */
431
uint64_t tox_netprof_get_packet_total_bytes(const Tox *tox, Tox_Netprof_Packet_Type type,
432
        Tox_Netprof_Direction direction);
433
434
435
/*******************************************************************************
436
 *
437
 * :: DHT groupchat queries.
438
 *
439
 ******************************************************************************/
440
441
/**
442
 * Maximum size of a peer IP address string.
443
 */
444
0
#define TOX_GROUP_PEER_IP_STRING_MAX_LENGTH 96
445
446
uint32_t tox_group_peer_ip_string_max_length(void);
447
448
/**
449
 * Return the length of the peer's IP address in string form. If the group
450
 * number or ID is invalid, the return value is unspecified.
451
 *
452
 * @param group_number The group number of the group we wish to query.
453
 * @param peer_id The ID of the peer whose IP address length we want to
454
 *   retrieve.
455
 */
456
size_t tox_group_peer_get_ip_address_size(const Tox *tox, uint32_t group_number, uint32_t peer_id,
457
        Tox_Err_Group_Peer_Query *error);
458
/**
459
 * Write the IP address associated with the designated peer_id for the
460
 * designated group number to ip_addr.
461
 *
462
 * If the peer is forcing TCP connections a placeholder value will be written
463
 * instead, indicating that their real IP address is unknown to us.
464
 *
465
 * If `peer_id` designates ourself, it will write either our own IP address or a
466
 * placeholder value, depending on whether or not we're forcing TCP connections.
467
 *
468
 * Call tox_group_peer_get_ip_address_size to determine the allocation size for
469
 * the `ip_addr` parameter.
470
 *
471
 * @param group_number The group number of the group we wish to query.
472
 * @param peer_id The ID of the peer whose public key we wish to retrieve.
473
 * @param ip_addr A valid memory region large enough to store the IP address
474
 *   string. If this parameter is NULL, this function call has no effect.
475
 *
476
 * @return true on success.
477
 */
478
bool tox_group_peer_get_ip_address(const Tox *tox, uint32_t group_number, uint32_t peer_id, uint8_t *ip_addr,
479
                                   Tox_Err_Group_Peer_Query *error);
480
481
#ifdef __cplusplus
482
} /* extern "C" */
483
#endif
484
485
#endif /* C_TOXCORE_TOXCORE_TOX_PRIVATE_H */