diff options
Diffstat (limited to 'toxcore/net_crypto.h')
-rw-r--r-- | toxcore/net_crypto.h | 202 |
1 files changed, 46 insertions, 156 deletions
diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h index c9554bfc..ec55bf6f 100644 --- a/toxcore/net_crypto.h +++ b/toxcore/net_crypto.h | |||
@@ -1,32 +1,34 @@ | |||
1 | /* net_crypto.h | 1 | /* |
2 | * | ||
3 | * Functions for the core network crypto. | 2 | * Functions for the core network crypto. |
3 | */ | ||
4 | |||
5 | /* | ||
6 | * Copyright © 2016-2017 The TokTok team. | ||
7 | * Copyright © 2013 Tox project. | ||
4 | * | 8 | * |
5 | * Copyright (C) 2013 Tox project All Rights Reserved. | 9 | * This file is part of Tox, the free peer to peer instant messenger. |
6 | * | ||
7 | * This file is part of Tox. | ||
8 | * | ||
9 | * Tox is free software: you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation, either version 3 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | 10 | * |
14 | * Tox is distributed in the hope that it will be useful, | 11 | * Tox is free software: you can redistribute it and/or modify |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 12 | * it under the terms of the GNU General Public License as published by |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 13 | * the Free Software Foundation, either version 3 of the License, or |
17 | * GNU General Public License for more details. | 14 | * (at your option) any later version. |
18 | * | 15 | * |
19 | * You should have received a copy of the GNU General Public License | 16 | * Tox is distributed in the hope that it will be useful, |
20 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
19 | * GNU General Public License for more details. | ||
21 | * | 20 | * |
21 | * You should have received a copy of the GNU General Public License | ||
22 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
22 | */ | 23 | */ |
23 | |||
24 | #ifndef NET_CRYPTO_H | 24 | #ifndef NET_CRYPTO_H |
25 | #define NET_CRYPTO_H | 25 | #define NET_CRYPTO_H |
26 | 26 | ||
27 | #include "DHT.h" | 27 | #include "DHT.h" |
28 | #include "LAN_discovery.h" | 28 | #include "LAN_discovery.h" |
29 | #include "TCP_connection.h" | 29 | #include "TCP_connection.h" |
30 | #include "logger.h" | ||
31 | |||
30 | #include <pthread.h> | 32 | #include <pthread.h> |
31 | 33 | ||
32 | #define CRYPTO_CONN_NO_CONNECTION 0 | 34 | #define CRYPTO_CONN_NO_CONNECTION 0 |
@@ -47,7 +49,7 @@ | |||
47 | /* Maximum total size of packets that net_crypto sends. */ | 49 | /* Maximum total size of packets that net_crypto sends. */ |
48 | #define MAX_CRYPTO_PACKET_SIZE 1400 | 50 | #define MAX_CRYPTO_PACKET_SIZE 1400 |
49 | 51 | ||
50 | #define CRYPTO_DATA_PACKET_MIN_SIZE (1 + sizeof(uint16_t) + (sizeof(uint32_t) + sizeof(uint32_t)) + crypto_box_MACBYTES) | 52 | #define CRYPTO_DATA_PACKET_MIN_SIZE (1 + sizeof(uint16_t) + (sizeof(uint32_t) + sizeof(uint32_t)) + CRYPTO_MAC_SIZE) |
51 | 53 | ||
52 | /* Max size of data in packets */ | 54 | /* Max size of data in packets */ |
53 | #define MAX_CRYPTO_DATA_SIZE (MAX_CRYPTO_PACKET_SIZE - CRYPTO_DATA_PACKET_MIN_SIZE) | 55 | #define MAX_CRYPTO_DATA_SIZE (MAX_CRYPTO_PACKET_SIZE - CRYPTO_DATA_PACKET_MIN_SIZE) |
@@ -60,7 +62,7 @@ | |||
60 | #define MAX_NUM_SENDPACKET_TRIES 8 | 62 | #define MAX_NUM_SENDPACKET_TRIES 8 |
61 | 63 | ||
62 | /* The timeout of no received UDP packets before the direct UDP connection is considered dead. */ | 64 | /* The timeout of no received UDP packets before the direct UDP connection is considered dead. */ |
63 | #define UDP_DIRECT_TIMEOUT ((MAX_NUM_SENDPACKET_TRIES * CRYPTO_SEND_PACKET_INTERVAL) / 1000) | 65 | #define UDP_DIRECT_TIMEOUT 8 |
64 | 66 | ||
65 | #define PACKET_ID_PADDING 0 /* Denotes padding */ | 67 | #define PACKET_ID_PADDING 0 /* Denotes padding */ |
66 | #define PACKET_ID_REQUEST 1 /* Used to request unreceived packets */ | 68 | #define PACKET_ID_REQUEST 1 /* Used to request unreceived packets */ |
@@ -87,136 +89,23 @@ | |||
87 | #define DEFAULT_PING_CONNECTION 1000 | 89 | #define DEFAULT_PING_CONNECTION 1000 |
88 | #define DEFAULT_TCP_PING_CONNECTION 500 | 90 | #define DEFAULT_TCP_PING_CONNECTION 500 |
89 | 91 | ||
90 | typedef struct { | 92 | typedef struct Net_Crypto Net_Crypto; |
91 | uint64_t sent_time; | 93 | |
92 | uint16_t length; | 94 | const uint8_t *nc_get_self_public_key(const Net_Crypto *c); |
93 | uint8_t data[MAX_CRYPTO_DATA_SIZE]; | 95 | const uint8_t *nc_get_self_secret_key(const Net_Crypto *c); |
94 | } Packet_Data; | 96 | TCP_Connections *nc_get_tcp_c(const Net_Crypto *c); |
95 | 97 | DHT *nc_get_dht(const Net_Crypto *c); | |
96 | typedef struct { | 98 | |
97 | Packet_Data *buffer[CRYPTO_PACKET_BUFFER_SIZE]; | 99 | typedef struct New_Connection { |
98 | uint32_t buffer_start; | ||
99 | uint32_t buffer_end; /* packet numbers in array: {buffer_start, buffer_end) */ | ||
100 | } Packets_Array; | ||
101 | |||
102 | typedef struct { | ||
103 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; /* The real public key of the peer. */ | ||
104 | uint8_t recv_nonce[crypto_box_NONCEBYTES]; /* Nonce of received packets. */ | ||
105 | uint8_t sent_nonce[crypto_box_NONCEBYTES]; /* Nonce of sent packets. */ | ||
106 | uint8_t sessionpublic_key[crypto_box_PUBLICKEYBYTES]; /* Our public key for this session. */ | ||
107 | uint8_t sessionsecret_key[crypto_box_SECRETKEYBYTES]; /* Our private key for this session. */ | ||
108 | uint8_t peersessionpublic_key[crypto_box_PUBLICKEYBYTES]; /* The public key of the peer. */ | ||
109 | uint8_t shared_key[crypto_box_BEFORENMBYTES]; /* The precomputed shared key from encrypt_precompute. */ | ||
110 | uint8_t status; /* 0 if no connection, 1 we are sending cookie request packets, | ||
111 | * 2 if we are sending handshake packets | ||
112 | * 3 if connection is not confirmed yet (we have received a handshake but no data packets yet), | ||
113 | * 4 if the connection is established. | ||
114 | */ | ||
115 | uint64_t cookie_request_number; /* number used in the cookie request packets for this connection */ | ||
116 | uint8_t dht_public_key[crypto_box_PUBLICKEYBYTES]; /* The dht public key of the peer */ | ||
117 | |||
118 | uint8_t *temp_packet; /* Where the cookie request/handshake packet is stored while it is being sent. */ | ||
119 | uint16_t temp_packet_length; | ||
120 | uint64_t temp_packet_sent_time; /* The time at which the last temp_packet was sent in ms. */ | ||
121 | uint32_t temp_packet_num_sent; | ||
122 | |||
123 | IP_Port ip_portv4; /* The ip and port to contact this guy directly.*/ | ||
124 | IP_Port ip_portv6; | ||
125 | uint64_t direct_lastrecv_timev4; /* The Time at which we last received a direct packet in ms. */ | ||
126 | uint64_t direct_lastrecv_timev6; | ||
127 | |||
128 | uint64_t last_tcp_sent; /* Time the last TCP packet was sent. */ | ||
129 | |||
130 | Packets_Array send_array; | ||
131 | Packets_Array recv_array; | ||
132 | |||
133 | int (*connection_status_callback)(void *object, int id, uint8_t status); | ||
134 | void *connection_status_callback_object; | ||
135 | int connection_status_callback_id; | ||
136 | |||
137 | int (*connection_data_callback)(void *object, int id, uint8_t *data, uint16_t length); | ||
138 | void *connection_data_callback_object; | ||
139 | int connection_data_callback_id; | ||
140 | |||
141 | int (*connection_lossy_data_callback)(void *object, int id, const uint8_t *data, uint16_t length); | ||
142 | void *connection_lossy_data_callback_object; | ||
143 | int connection_lossy_data_callback_id; | ||
144 | |||
145 | uint64_t last_request_packet_sent; | ||
146 | uint64_t direct_send_attempt_time; | ||
147 | |||
148 | uint32_t packet_counter; | ||
149 | double packet_recv_rate; | ||
150 | uint64_t packet_counter_set; | ||
151 | |||
152 | double packet_send_rate; | ||
153 | uint32_t packets_left; | ||
154 | uint64_t last_packets_left_set; | ||
155 | double last_packets_left_rem; | ||
156 | |||
157 | double packet_send_rate_requested; | ||
158 | uint32_t packets_left_requested; | ||
159 | uint64_t last_packets_left_requested_set; | ||
160 | double last_packets_left_requested_rem; | ||
161 | |||
162 | uint32_t last_sendqueue_size[CONGESTION_QUEUE_ARRAY_SIZE], last_sendqueue_counter; | ||
163 | long signed int last_num_packets_sent[CONGESTION_LAST_SENT_ARRAY_SIZE], | ||
164 | last_num_packets_resent[CONGESTION_LAST_SENT_ARRAY_SIZE]; | ||
165 | uint32_t packets_sent, packets_resent; | ||
166 | uint64_t last_congestion_event; | ||
167 | uint64_t rtt_time; | ||
168 | |||
169 | /* TCP_connection connection_number */ | ||
170 | unsigned int connection_number_tcp; | ||
171 | |||
172 | uint8_t maximum_speed_reached; | ||
173 | |||
174 | pthread_mutex_t mutex; | ||
175 | |||
176 | void (*dht_pk_callback)(void *data, int32_t number, const uint8_t *dht_public_key); | ||
177 | void *dht_pk_callback_object; | ||
178 | uint32_t dht_pk_callback_number; | ||
179 | } Crypto_Connection; | ||
180 | |||
181 | typedef struct { | ||
182 | IP_Port source; | 100 | IP_Port source; |
183 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; /* The real public key of the peer. */ | 101 | uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; /* The real public key of the peer. */ |
184 | uint8_t dht_public_key[crypto_box_PUBLICKEYBYTES]; /* The dht public key of the peer. */ | 102 | uint8_t dht_public_key[CRYPTO_PUBLIC_KEY_SIZE]; /* The dht public key of the peer. */ |
185 | uint8_t recv_nonce[crypto_box_NONCEBYTES]; /* Nonce of received packets. */ | 103 | uint8_t recv_nonce[CRYPTO_NONCE_SIZE]; /* Nonce of received packets. */ |
186 | uint8_t peersessionpublic_key[crypto_box_PUBLICKEYBYTES]; /* The public key of the peer. */ | 104 | uint8_t peersessionpublic_key[CRYPTO_PUBLIC_KEY_SIZE]; /* The public key of the peer. */ |
187 | uint8_t *cookie; | 105 | uint8_t *cookie; |
188 | uint8_t cookie_length; | 106 | uint8_t cookie_length; |
189 | } New_Connection; | 107 | } New_Connection; |
190 | 108 | ||
191 | typedef struct { | ||
192 | DHT *dht; | ||
193 | TCP_Connections *tcp_c; | ||
194 | |||
195 | Crypto_Connection *crypto_connections; | ||
196 | pthread_mutex_t tcp_mutex; | ||
197 | |||
198 | pthread_mutex_t connections_mutex; | ||
199 | unsigned int connection_use_counter; | ||
200 | |||
201 | uint32_t crypto_connections_length; /* Length of connections array. */ | ||
202 | |||
203 | /* Our public and secret keys. */ | ||
204 | uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; | ||
205 | uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; | ||
206 | |||
207 | /* The secret key used for cookies */ | ||
208 | uint8_t secret_symmetric_key[crypto_box_KEYBYTES]; | ||
209 | |||
210 | int (*new_connection_callback)(void *object, New_Connection *n_c); | ||
211 | void *new_connection_callback_object; | ||
212 | |||
213 | /* The current optimal sleep time */ | ||
214 | uint32_t current_sleep_time; | ||
215 | |||
216 | BS_LIST ip_port_list; | ||
217 | } Net_Crypto; | ||
218 | |||
219 | |||
220 | /* Set function to be called when someone requests a new connection to us. | 109 | /* Set function to be called when someone requests a new connection to us. |
221 | * | 110 | * |
222 | * The set function should return -1 on failure and 0 on success. | 111 | * The set function should return -1 on failure and 0 on success. |
@@ -248,7 +137,7 @@ int new_crypto_connection(Net_Crypto *c, const uint8_t *real_public_key, const u | |||
248 | * return -1 on failure. | 137 | * return -1 on failure. |
249 | * return 0 on success. | 138 | * return 0 on success. |
250 | */ | 139 | */ |
251 | int set_direct_ip_port(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port, _Bool connected); | 140 | int set_direct_ip_port(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port, bool connected); |
252 | 141 | ||
253 | /* Set function to be called when connection with crypt_connection_id goes connects/disconnects. | 142 | /* Set function to be called when connection with crypt_connection_id goes connects/disconnects. |
254 | * | 143 | * |
@@ -261,7 +150,7 @@ int set_direct_ip_port(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port, | |||
261 | * return 0 on success. | 150 | * return 0 on success. |
262 | */ | 151 | */ |
263 | int connection_status_handler(const Net_Crypto *c, int crypt_connection_id, | 152 | int connection_status_handler(const Net_Crypto *c, int crypt_connection_id, |
264 | int (*connection_status_callback)(void *object, int id, uint8_t status), void *object, int id); | 153 | int (*connection_status_callback)(void *object, int id, uint8_t status, void *userdata), void *object, int id); |
265 | 154 | ||
266 | /* Set function to be called when connection with crypt_connection_id receives a lossless data packet of length. | 155 | /* Set function to be called when connection with crypt_connection_id receives a lossless data packet of length. |
267 | * | 156 | * |
@@ -272,7 +161,7 @@ int connection_status_handler(const Net_Crypto *c, int crypt_connection_id, | |||
272 | * return 0 on success. | 161 | * return 0 on success. |
273 | */ | 162 | */ |
274 | int connection_data_handler(const Net_Crypto *c, int crypt_connection_id, int (*connection_data_callback)(void *object, | 163 | int connection_data_handler(const Net_Crypto *c, int crypt_connection_id, int (*connection_data_callback)(void *object, |
275 | int id, uint8_t *data, uint16_t length), void *object, int id); | 164 | int id, const uint8_t *data, uint16_t length, void *userdata), void *object, int id); |
276 | 165 | ||
277 | 166 | ||
278 | /* Set function to be called when connection with crypt_connection_id receives a lossy data packet of length. | 167 | /* Set function to be called when connection with crypt_connection_id receives a lossy data packet of length. |
@@ -284,7 +173,8 @@ int connection_data_handler(const Net_Crypto *c, int crypt_connection_id, int (* | |||
284 | * return 0 on success. | 173 | * return 0 on success. |
285 | */ | 174 | */ |
286 | int connection_lossy_data_handler(Net_Crypto *c, int crypt_connection_id, | 175 | int connection_lossy_data_handler(Net_Crypto *c, int crypt_connection_id, |
287 | int (*connection_lossy_data_callback)(void *object, int id, const uint8_t *data, uint16_t length), void *object, | 176 | int (*connection_lossy_data_callback)(void *object, int id, const uint8_t *data, uint16_t length, void *userdata), |
177 | void *object, | ||
288 | int id); | 178 | int id); |
289 | 179 | ||
290 | /* Set the function for this friend that will be callbacked with object and number if | 180 | /* Set the function for this friend that will be callbacked with object and number if |
@@ -298,7 +188,7 @@ int connection_lossy_data_handler(Net_Crypto *c, int crypt_connection_id, | |||
298 | * return 0 on success. | 188 | * return 0 on success. |
299 | */ | 189 | */ |
300 | int nc_dht_pk_callback(Net_Crypto *c, int crypt_connection_id, void (*function)(void *data, int32_t number, | 190 | int nc_dht_pk_callback(Net_Crypto *c, int crypt_connection_id, void (*function)(void *data, int32_t number, |
301 | const uint8_t *dht_public_key), void *object, uint32_t number); | 191 | const uint8_t *dht_public_key, void *userdata), void *object, uint32_t number); |
302 | 192 | ||
303 | /* returns the number of packet slots left in the sendbuffer. | 193 | /* returns the number of packet slots left in the sendbuffer. |
304 | * return 0 if failure. | 194 | * return 0 if failure. |
@@ -308,7 +198,7 @@ uint32_t crypto_num_free_sendqueue_slots(const Net_Crypto *c, int crypt_connecti | |||
308 | /* Return 1 if max speed was reached for this connection (no more data can be physically through the pipe). | 198 | /* Return 1 if max speed was reached for this connection (no more data can be physically through the pipe). |
309 | * Return 0 if it wasn't reached. | 199 | * Return 0 if it wasn't reached. |
310 | */ | 200 | */ |
311 | _Bool max_speed_reached(Net_Crypto *c, int crypt_connection_id); | 201 | bool max_speed_reached(Net_Crypto *c, int crypt_connection_id); |
312 | 202 | ||
313 | /* Sends a lossless cryptopacket. | 203 | /* Sends a lossless cryptopacket. |
314 | * | 204 | * |
@@ -364,7 +254,7 @@ int get_random_tcp_con_number(Net_Crypto *c); | |||
364 | * return 0 on success. | 254 | * return 0 on success. |
365 | * return -1 on failure. | 255 | * return -1 on failure. |
366 | */ | 256 | */ |
367 | int send_tcp_onion_request(Net_Crypto *c, unsigned int TCP_conn_number, const uint8_t *data, uint16_t length); | 257 | int send_tcp_onion_request(Net_Crypto *c, unsigned int tcp_connections_number, const uint8_t *data, uint16_t length); |
368 | 258 | ||
369 | /* Copy a maximum of num TCP relays we are connected to to tcp_relays. | 259 | /* Copy a maximum of num TCP relays we are connected to to tcp_relays. |
370 | * NOTE that the family of the copied ip ports will be set to TCP_INET or TCP_INET6. | 260 | * NOTE that the family of the copied ip ports will be set to TCP_INET or TCP_INET6. |
@@ -386,7 +276,7 @@ int crypto_kill(Net_Crypto *c, int crypt_connection_id); | |||
386 | * sets direct_connected to 1 if connection connects directly to other, 0 if it isn't. | 276 | * sets direct_connected to 1 if connection connects directly to other, 0 if it isn't. |
387 | * sets online_tcp_relays to the number of connected tcp relays this connection has. | 277 | * sets online_tcp_relays to the number of connected tcp relays this connection has. |
388 | */ | 278 | */ |
389 | unsigned int crypto_connection_status(const Net_Crypto *c, int crypt_connection_id, _Bool *direct_connected, | 279 | unsigned int crypto_connection_status(const Net_Crypto *c, int crypt_connection_id, bool *direct_connected, |
390 | unsigned int *online_tcp_relays); | 280 | unsigned int *online_tcp_relays); |
391 | 281 | ||
392 | /* Generate our public and private keys. | 282 | /* Generate our public and private keys. |
@@ -395,26 +285,26 @@ unsigned int crypto_connection_status(const Net_Crypto *c, int crypt_connection_ | |||
395 | void new_keys(Net_Crypto *c); | 285 | void new_keys(Net_Crypto *c); |
396 | 286 | ||
397 | /* Save the public and private keys to the keys array. | 287 | /* Save the public and private keys to the keys array. |
398 | * Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES. | 288 | * Length must be CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_SECRET_KEY_SIZE. |
399 | */ | 289 | */ |
400 | void save_keys(const Net_Crypto *c, uint8_t *keys); | 290 | void save_keys(const Net_Crypto *c, uint8_t *keys); |
401 | 291 | ||
402 | /* Load the secret key. | 292 | /* Load the secret key. |
403 | * Length must be crypto_box_SECRETKEYBYTES. | 293 | * Length must be CRYPTO_SECRET_KEY_SIZE. |
404 | */ | 294 | */ |
405 | void load_secret_key(Net_Crypto *c, const uint8_t *sk); | 295 | void load_secret_key(Net_Crypto *c, const uint8_t *sk); |
406 | 296 | ||
407 | /* Create new instance of Net_Crypto. | 297 | /* Create new instance of Net_Crypto. |
408 | * Sets all the global connection variables to their default values. | 298 | * Sets all the global connection variables to their default values. |
409 | */ | 299 | */ |
410 | Net_Crypto *new_net_crypto(DHT *dht, TCP_Proxy_Info *proxy_info); | 300 | Net_Crypto *new_net_crypto(Logger *log, DHT *dht, TCP_Proxy_Info *proxy_info); |
411 | 301 | ||
412 | /* return the optimal interval in ms for running do_net_crypto. | 302 | /* return the optimal interval in ms for running do_net_crypto. |
413 | */ | 303 | */ |
414 | uint32_t crypto_run_interval(const Net_Crypto *c); | 304 | uint32_t crypto_run_interval(const Net_Crypto *c); |
415 | 305 | ||
416 | /* Main loop. */ | 306 | /* Main loop. */ |
417 | void do_net_crypto(Net_Crypto *c); | 307 | void do_net_crypto(Net_Crypto *c, void *userdata); |
418 | 308 | ||
419 | void kill_net_crypto(Net_Crypto *c); | 309 | void kill_net_crypto(Net_Crypto *c); |
420 | 310 | ||