Coverage Report

Created: 2025-10-08 19:34

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