summaryrefslogtreecommitdiff
path: root/toxcore/net_crypto.h
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/net_crypto.h')
-rw-r--r--toxcore/net_crypto.h66
1 files changed, 14 insertions, 52 deletions
diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h
index fab72cb3..9eb5e2d3 100644
--- a/toxcore/net_crypto.h
+++ b/toxcore/net_crypto.h
@@ -26,7 +26,7 @@
26 26
27#include "DHT.h" 27#include "DHT.h"
28#include "LAN_discovery.h" 28#include "LAN_discovery.h"
29#include "TCP_client.h" 29#include "TCP_connection.h"
30#include <pthread.h> 30#include <pthread.h>
31 31
32#define CRYPTO_CONN_NO_CONNECTION 0 32#define CRYPTO_CONN_NO_CONNECTION 0
@@ -34,7 +34,6 @@
34#define CRYPTO_CONN_HANDSHAKE_SENT 2 //send handshake packets 34#define CRYPTO_CONN_HANDSHAKE_SENT 2 //send handshake packets
35#define CRYPTO_CONN_NOT_CONFIRMED 3 //send handshake packets, we have received one from the other 35#define CRYPTO_CONN_NOT_CONFIRMED 3 //send handshake packets, we have received one from the other
36#define CRYPTO_CONN_ESTABLISHED 4 36#define CRYPTO_CONN_ESTABLISHED 4
37#define CRYPTO_CONN_TIMED_OUT 5
38 37
39/* Maximum size of receiving and sending packet buffers. */ 38/* Maximum size of receiving and sending packet buffers. */
40#define CRYPTO_PACKET_BUFFER_SIZE 16384 /* Must be a power of 2 */ 39#define CRYPTO_PACKET_BUFFER_SIZE 16384 /* Must be a power of 2 */
@@ -61,7 +60,7 @@
61#define MAX_NUM_SENDPACKET_TRIES 8 60#define MAX_NUM_SENDPACKET_TRIES 8
62 61
63/* The timeout of no received UDP packets before the direct UDP connection is considered dead. */ 62/* The timeout of no received UDP packets before the direct UDP connection is considered dead. */
64#define UDP_DIRECT_TIMEOUT ((MAX_NUM_SENDPACKET_TRIES * CRYPTO_SEND_PACKET_INTERVAL) / 2) 63#define UDP_DIRECT_TIMEOUT ((MAX_NUM_SENDPACKET_TRIES * CRYPTO_SEND_PACKET_INTERVAL) / 1000)
65 64
66#define PACKET_ID_PADDING 0 /* Denotes padding */ 65#define PACKET_ID_PADDING 0 /* Denotes padding */
67#define PACKET_ID_REQUEST 1 /* Used to request unreceived packets */ 66#define PACKET_ID_REQUEST 1 /* Used to request unreceived packets */
@@ -73,11 +72,6 @@
73#define MAX_TCP_CONNECTIONS 64 72#define MAX_TCP_CONNECTIONS 64
74#define MAX_TCP_RELAYS_PEER 4 73#define MAX_TCP_RELAYS_PEER 4
75 74
76#define STATUS_TCP_NULL 0
77#define STATUS_TCP_OFFLINE 1
78#define STATUS_TCP_INVISIBLE 2 /* we know the other peer is connected to this relay but he isn't appearing online */
79#define STATUS_TCP_ONLINE 3
80
81/* All packets starting with a byte in this range are considered lossy packets. */ 75/* All packets starting with a byte in this range are considered lossy packets. */
82#define PACKET_ID_LOSSY_RANGE_START 192 76#define PACKET_ID_LOSSY_RANGE_START 192
83#define PACKET_ID_LOSSY_RANGE_SIZE 63 77#define PACKET_ID_LOSSY_RANGE_SIZE 63
@@ -112,11 +106,9 @@ typedef struct {
112 * 2 if we are sending handshake packets 106 * 2 if we are sending handshake packets
113 * 3 if connection is not confirmed yet (we have received a handshake but no data packets yet), 107 * 3 if connection is not confirmed yet (we have received a handshake but no data packets yet),
114 * 4 if the connection is established. 108 * 4 if the connection is established.
115 * 5 if the connection is timed out.
116 */ 109 */
117 uint64_t cookie_request_number; /* number used in the cookie request packets for this connection */ 110 uint64_t cookie_request_number; /* number used in the cookie request packets for this connection */
118 uint8_t dht_public_key[crypto_box_PUBLICKEYBYTES]; /* The dht public key of the peer */ 111 uint8_t dht_public_key[crypto_box_PUBLICKEYBYTES]; /* The dht public key of the peer */
119 uint8_t dht_public_key_set; /* True if the dht public key is set, false if it isn't. */
120 112
121 uint8_t *temp_packet; /* Where the cookie request/handshake packet is stored while it is being sent. */ 113 uint8_t *temp_packet; /* Where the cookie request/handshake packet is stored while it is being sent. */
122 uint16_t temp_packet_length; 114 uint16_t temp_packet_length;
@@ -155,15 +147,8 @@ typedef struct {
155 long signed int last_num_packets_sent[CONGESTION_QUEUE_ARRAY_SIZE]; 147 long signed int last_num_packets_sent[CONGESTION_QUEUE_ARRAY_SIZE];
156 uint32_t packets_sent; 148 uint32_t packets_sent;
157 149
158 uint8_t killed; /* set to 1 to kill the connection. */ 150 /* TCP_connection connection_number */
159 151 unsigned int connection_number_tcp;
160 uint8_t status_tcp[MAX_TCP_CONNECTIONS]; /* set to one of STATUS_TCP_* */
161 uint8_t con_number_tcp[MAX_TCP_CONNECTIONS];
162 unsigned int last_relay_sentto;
163 unsigned int num_tcp_online;
164
165 Node_format tcp_relays[MAX_TCP_RELAYS_PEER];
166 uint16_t num_tcp_relays;
167 152
168 uint8_t maximum_speed_reached; 153 uint8_t maximum_speed_reached;
169 154
@@ -186,10 +171,9 @@ typedef struct {
186 171
187typedef struct { 172typedef struct {
188 DHT *dht; 173 DHT *dht;
174 TCP_Connections *tcp_c;
189 175
190 Crypto_Connection *crypto_connections; 176 Crypto_Connection *crypto_connections;
191 TCP_Client_Connection *tcp_connections_new[MAX_TCP_CONNECTIONS];
192 TCP_Client_Connection *tcp_connections[MAX_TCP_CONNECTIONS];
193 pthread_mutex_t tcp_mutex; 177 pthread_mutex_t tcp_mutex;
194 178
195 pthread_mutex_t connections_mutex; 179 pthread_mutex_t connections_mutex;
@@ -211,11 +195,6 @@ typedef struct {
211 uint32_t current_sleep_time; 195 uint32_t current_sleep_time;
212 196
213 BS_LIST ip_port_list; 197 BS_LIST ip_port_list;
214
215 int (*tcp_onion_callback)(void *object, const uint8_t *data, uint16_t length);
216 void *tcp_onion_callback_object;
217
218 TCP_Proxy_Info proxy_info;
219} Net_Crypto; 198} Net_Crypto;
220 199
221 200
@@ -241,21 +220,7 @@ int accept_crypto_connection(Net_Crypto *c, New_Connection *n_c);
241 * return -1 on failure. 220 * return -1 on failure.
242 * return connection id on success. 221 * return connection id on success.
243 */ 222 */
244int new_crypto_connection(Net_Crypto *c, const uint8_t *real_public_key); 223int new_crypto_connection(Net_Crypto *c, const uint8_t *real_public_key, const uint8_t *dht_public_key);
245
246/* Copy friends DHT public key into dht_key.
247 *
248 * return 0 on failure (no key copied).
249 * return 1 on success (key copied).
250 */
251unsigned int get_connection_dht_key(const Net_Crypto *c, int crypt_connection_id, uint8_t *dht_public_key);
252
253/* Set the DHT public key of the crypto connection.
254 *
255 * return -1 on failure.
256 * return 0 on success.
257 */
258int set_connection_dht_public_key(Net_Crypto *c, int crypt_connection_id, const uint8_t *dht_public_key);
259 224
260/* Set the direct ip of the crypto connection. 225/* Set the direct ip of the crypto connection.
261 * 226 *
@@ -301,8 +266,10 @@ int connection_lossy_data_handler(Net_Crypto *c, int crypt_connection_id,
301 int (*connection_lossy_data_callback)(void *object, int id, const uint8_t *data, uint16_t length), void *object, 266 int (*connection_lossy_data_callback)(void *object, int id, const uint8_t *data, uint16_t length), void *object,
302 int id); 267 int id);
303 268
304/* Set the function for this friend that will be callbacked with object and number 269/* Set the function for this friend that will be callbacked with object and number if
305 * when that friend gives us his DHT temporary public key. 270 * the friend sends us a different dht public key than we have associated to him.
271 *
272 * If this function is called, the connection should be recreated with the new public key.
306 * 273 *
307 * object and number will be passed as argument to this function. 274 * object and number will be passed as argument to this function.
308 * 275 *
@@ -364,11 +331,6 @@ int add_tcp_relay_peer(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port,
364 */ 331 */
365int add_tcp_relay(Net_Crypto *c, IP_Port ip_port, const uint8_t *public_key); 332int add_tcp_relay(Net_Crypto *c, IP_Port ip_port, const uint8_t *public_key);
366 333
367/* Set the function to be called when an onion response packet is received by one of the TCP connections.
368 */
369void tcp_onion_response_handler(Net_Crypto *c, int (*tcp_onion_callback)(void *object, const uint8_t *data,
370 uint16_t length), void *object);
371
372/* Return a random TCP connection number for use in send_tcp_onion_request. 334/* Return a random TCP connection number for use in send_tcp_onion_request.
373 * 335 *
374 * return TCP connection number on success. 336 * return TCP connection number on success.
@@ -389,7 +351,7 @@ int send_tcp_onion_request(Net_Crypto *c, unsigned int TCP_conn_number, const ui
389 * return number of relays copied to tcp_relays on success. 351 * return number of relays copied to tcp_relays on success.
390 * return 0 on failure. 352 * return 0 on failure.
391 */ 353 */
392unsigned int copy_connected_tcp_relays(const Net_Crypto *c, Node_format *tcp_relays, uint16_t num); 354unsigned int copy_connected_tcp_relays(Net_Crypto *c, Node_format *tcp_relays, uint16_t num);
393 355
394/* Kill a crypto connection. 356/* Kill a crypto connection.
395 * 357 *
@@ -398,13 +360,13 @@ unsigned int copy_connected_tcp_relays(const Net_Crypto *c, Node_format *tcp_rel
398 */ 360 */
399int crypto_kill(Net_Crypto *c, int crypt_connection_id); 361int crypto_kill(Net_Crypto *c, int crypt_connection_id);
400 362
401
402/* return one of CRYPTO_CONN_* values indicating the state of the connection. 363/* return one of CRYPTO_CONN_* values indicating the state of the connection.
403 * 364 *
404 * sets direct_connected to 1 if connection connects directly to other, 0 if it isn't. 365 * sets direct_connected to 1 if connection connects directly to other, 0 if it isn't.
366 * sets online_tcp_relays to the number of connected tcp relays this connection has.
405 */ 367 */
406unsigned int crypto_connection_status(const Net_Crypto *c, int crypt_connection_id, uint8_t *direct_connected); 368unsigned int crypto_connection_status(const Net_Crypto *c, int crypt_connection_id, _Bool *direct_connected,
407 369 unsigned int *online_tcp_relays);
408 370
409/* Generate our public and private keys. 371/* Generate our public and private keys.
410 * Only call this function the first time the program starts. 372 * Only call this function the first time the program starts.