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