Coverage Report

Created: 2026-10-24 08:16

/work/toxcore/friend_connection.h
Line
Count
Source
1
/* SPDX-License-Identifier: GPL-3.0-or-later
2
 * Copyright © 2016-2026 The TokTok team.
3
 * Copyright © 2014 Tox project.
4
 */
5
6
/**
7
 * Connection to friends.
8
 */
9
#ifndef C_TOXCORE_TOXCORE_FRIEND_CONNECTION_H
10
#define C_TOXCORE_TOXCORE_FRIEND_CONNECTION_H
11
12
#include <stdbool.h>
13
#include <stdint.h>
14
15
#include "DHT.h"
16
#include "attributes.h"
17
#include "logger.h"
18
#include "mem.h"
19
#include "mono_time.h"
20
#include "net.h"
21
#include "net_crypto.h"
22
#include "network.h"
23
#include "onion_client.h"
24
25
269k
#define MAX_FRIEND_CONNECTION_CALLBACKS 2
26
875
#define MESSENGER_CALLBACK_INDEX 0
27
165
#define GROUPCHAT_CALLBACK_INDEX 1
28
29
89.4k
#define PACKET_ID_ALIVE 16
30
88.0k
#define PACKET_ID_SHARE_RELAYS 17
31
88.7k
#define PACKET_ID_FRIEND_REQUESTS 18
32
33
/** Interval between the sending of ping packets. */
34
103k
#define FRIEND_PING_INTERVAL 8
35
36
/** If no packets are received from friend in this time interval, kill the connection. */
37
51.8k
#define FRIEND_CONNECTION_TIMEOUT (FRIEND_PING_INTERVAL * 4)
38
39
/** Time before friend is removed from the DHT after last hearing about him. */
40
265k
#define FRIEND_DHT_TIMEOUT BAD_NODE_TIMEOUT
41
42
28.2k
#define FRIEND_MAX_STORED_TCP_RELAYS (MAX_FRIEND_TCP_CONNECTIONS * 4)
43
44
/** Max number of tcp relays sent to friends */
45
103k
#define MAX_SHARED_RELAYS RECOMMENDED_FRIEND_TCP_CONNECTIONS
46
47
/** How often we share our TCP relays with each friend connection */
48
51.8k
#define SHARE_RELAYS_INTERVAL (60 * 2)
49
50
typedef enum Friendconn_Status {
51
    FRIENDCONN_STATUS_NONE,
52
    FRIENDCONN_STATUS_CONNECTING,
53
    FRIENDCONN_STATUS_CONNECTED,
54
} Friendconn_Status;
55
56
#ifdef __cplusplus
57
extern "C" {
58
#endif
59
60
typedef struct Friend_Connections Friend_Connections;
61
62
Net_Crypto *_Nonnull friendconn_net_crypto(const Friend_Connections *_Nonnull fr_c);
63
64
/**
65
 * @return friendcon_id corresponding to the real public key on success.
66
 * @retval -1 on failure.
67
 */
68
int getfriend_conn_id_pk(const Friend_Connections *_Nonnull fr_c, const uint8_t *_Nonnull real_pk);
69
70
/** @brief Increases lock_count for the connection with friendcon_id by 1.
71
 *
72
 * @retval 0 on success.
73
 * @retval -1 on failure.
74
 */
75
int friend_connection_lock(const Friend_Connections *_Nonnull fr_c, int friendcon_id);
76
77
/**
78
 * @retval FRIENDCONN_STATUS_CONNECTED if the friend is connected.
79
 * @retval FRIENDCONN_STATUS_CONNECTING if the friend isn't connected.
80
 * @retval FRIENDCONN_STATUS_NONE on failure.
81
 */
82
unsigned int friend_con_connected(const Friend_Connections *_Nonnull fr_c, int friendcon_id);
83
84
/** @brief Copy public keys associated to friendcon_id.
85
 *
86
 * @retval 0 on success.
87
 * @retval -1 on failure.
88
 */
89
int get_friendcon_public_keys(uint8_t *_Nullable real_pk, uint8_t *_Nullable dht_temp_pk, const Friend_Connections *_Nonnull fr_c, int friendcon_id);
90
/** Set temp dht key for connection. */
91
void set_dht_temp_pk(Friend_Connections *_Nonnull fr_c, int friendcon_id, const uint8_t *_Nonnull dht_temp_pk, void *_Nullable userdata);
92
93
typedef int global_status_cb(void *_Nullable object, int friendcon_id, bool status, void *_Nullable userdata);
94
95
typedef int fc_status_cb(void *_Nullable object, int friendcon_id, bool status, void *_Nullable userdata);
96
typedef int fc_data_cb(void *_Nullable object, int friendcon_id, const uint8_t *_Nonnull data, uint16_t length, void *_Nullable userdata);
97
typedef int fc_lossy_data_cb(void *_Nullable object, int friendcon_id, const uint8_t *_Nonnull data, uint16_t length, void *_Nullable userdata);
98
99
/** Set global status callback for friend connections. */
100
void set_global_status_callback(Friend_Connections *_Nonnull fr_c, global_status_cb *_Nullable global_status_callback, void *_Nullable object);
101
/** @brief Set the callbacks for the friend connection.
102
 * @param index is the index (0 to (MAX_FRIEND_CONNECTION_CALLBACKS - 1)) we
103
 *   want the callback to set in the array.
104
 *
105
 * @retval 0 on success.
106
 * @retval -1 on failure
107
 */
108
int friend_connection_callbacks(const Friend_Connections *_Nonnull fr_c, int friendcon_id, unsigned int index,
109
                                fc_status_cb *_Nullable status_callback,
110
                                fc_data_cb *_Nullable data_callback,
111
                                fc_lossy_data_cb *_Nullable lossy_data_callback,
112
                                void *_Nullable object, int number);
113
/** @brief return the crypt_connection_id for the connection.
114
 *
115
 * @return crypt_connection_id on success.
116
 * @retval -1 on failure.
117
 */
118
int friend_connection_crypt_connection_id(const Friend_Connections *_Nonnull fr_c, int friendcon_id);
119
120
/** @brief Create a new friend connection.
121
 * If one to that real public key already exists, increase lock count and return it.
122
 *
123
 * @retval -1 on failure.
124
 * @return connection id on success.
125
 */
126
int new_friend_connection(Friend_Connections *_Nonnull fr_c, const uint8_t *_Nonnull real_public_key);
127
128
/** @brief Kill a friend connection.
129
 *
130
 * @retval -1 on failure.
131
 * @retval 0 on success.
132
 */
133
int kill_friend_connection(Friend_Connections *_Nonnull fr_c, int friendcon_id);
134
135
/** @brief Send a Friend request packet.
136
 *
137
 * @retval -1 if failure.
138
 * @retval  0 if it sent the friend request directly to the friend.
139
 * @return the number of peers it was routed through if it did not send it directly.
140
 */
141
int send_friend_request_packet(Friend_Connections *_Nonnull fr_c, int friendcon_id, uint32_t nospam_num, const uint8_t *_Nonnull data, uint16_t length);
142
143
typedef int fr_request_cb(
144
    void *_Nonnull object, const uint8_t *_Nonnull source_pubkey, const uint8_t *_Nonnull data, uint16_t length, void *_Nullable userdata);
145
146
/** @brief Set friend request callback.
147
 *
148
 * This function will be called every time a friend request packet is received.
149
 */
150
void set_friend_request_callback(Friend_Connections *_Nonnull fr_c, fr_request_cb *_Nullable fr_request_callback, void *_Nullable object);
151
152
/** Create new friend_connections instance. */
153
Friend_Connections *_Nullable new_friend_connections(const Logger *_Nonnull logger, const Memory *_Nonnull mem, const Mono_Time *_Nonnull mono_time, const Network *_Nonnull ns,
154
        Onion_Client *_Nonnull onion_c, DHT *_Nonnull dht, Net_Crypto *_Nonnull net_crypto, Networking_Core *_Nonnull net,
155
        bool local_discovery_enabled);
156
157
/** main friend_connections loop. */
158
void do_friend_connections(Friend_Connections *_Nonnull fr_c, void *_Nullable userdata);
159
160
/** Free everything related with friend_connections. */
161
void kill_friend_connections(Friend_Connections *_Nullable fr_c);
162
typedef struct Friend_Conn Friend_Conn;
163
164
Friend_Conn *_Nullable get_conn(const Friend_Connections *_Nonnull fr_c, int friendcon_id);
165
int friend_conn_get_onion_friendnum(const Friend_Conn *_Nonnull fc);
166
const IP_Port *_Nullable friend_conn_get_dht_ip_port(const Friend_Conn *_Nonnull fc);
167
168
#ifdef __cplusplus
169
} /* extern "C" */
170
#endif
171
172
#endif /* C_TOXCORE_TOXCORE_FRIEND_CONNECTION_H */