diff options
-rw-r--r-- | toxcore/net_crypto.c | 58 | ||||
-rw-r--r-- | toxcore/net_crypto.h | 28 |
2 files changed, 45 insertions, 41 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index bae5ee19..f4c6335b 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c | |||
@@ -35,19 +35,19 @@ | |||
35 | 35 | ||
36 | #include "util.h" | 36 | #include "util.h" |
37 | 37 | ||
38 | typedef struct { | 38 | typedef struct Packet_Data { |
39 | uint64_t sent_time; | 39 | uint64_t sent_time; |
40 | uint16_t length; | 40 | uint16_t length; |
41 | uint8_t data[MAX_CRYPTO_DATA_SIZE]; | 41 | uint8_t data[MAX_CRYPTO_DATA_SIZE]; |
42 | } Packet_Data; | 42 | } Packet_Data; |
43 | 43 | ||
44 | typedef struct { | 44 | typedef struct Packets_Array { |
45 | Packet_Data *buffer[CRYPTO_PACKET_BUFFER_SIZE]; | 45 | Packet_Data *buffer[CRYPTO_PACKET_BUFFER_SIZE]; |
46 | uint32_t buffer_start; | 46 | uint32_t buffer_start; |
47 | uint32_t buffer_end; /* packet numbers in array: {buffer_start, buffer_end) */ | 47 | uint32_t buffer_end; /* packet numbers in array: {buffer_start, buffer_end) */ |
48 | } Packets_Array; | 48 | } Packets_Array; |
49 | 49 | ||
50 | typedef struct { | 50 | typedef struct Crypto_Connection { |
51 | uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; /* The real public key of the peer. */ | 51 | uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; /* The real public key of the peer. */ |
52 | uint8_t recv_nonce[CRYPTO_NONCE_SIZE]; /* Nonce of received packets. */ | 52 | uint8_t recv_nonce[CRYPTO_NONCE_SIZE]; /* Nonce of received packets. */ |
53 | uint8_t sent_nonce[CRYPTO_NONCE_SIZE]; /* Nonce of sent packets. */ | 53 | uint8_t sent_nonce[CRYPTO_NONCE_SIZE]; /* Nonce of sent packets. */ |
@@ -62,7 +62,7 @@ typedef struct { | |||
62 | * 3 if connection is not confirmed yet (we have received a handshake but no data packets yet), | 62 | * 3 if connection is not confirmed yet (we have received a handshake but no data packets yet), |
63 | * 4 if the connection is established. | 63 | * 4 if the connection is established. |
64 | */ | 64 | */ |
65 | CRYPTO_CONN_STATE status; | 65 | Crypto_Conn_State status; |
66 | uint64_t cookie_request_number; /* number used in the cookie request packets for this connection */ | 66 | uint64_t cookie_request_number; /* number used in the cookie request packets for this connection */ |
67 | uint8_t dht_public_key[CRYPTO_PUBLIC_KEY_SIZE]; /* The dht public key of the peer */ | 67 | uint8_t dht_public_key[CRYPTO_PUBLIC_KEY_SIZE]; /* The dht public key of the peer */ |
68 | 68 | ||
@@ -81,15 +81,15 @@ typedef struct { | |||
81 | Packets_Array send_array; | 81 | Packets_Array send_array; |
82 | Packets_Array recv_array; | 82 | Packets_Array recv_array; |
83 | 83 | ||
84 | int (*connection_status_callback)(void *object, int id, uint8_t status, void *userdata); | 84 | connection_status_cb *connection_status_callback; |
85 | void *connection_status_callback_object; | 85 | void *connection_status_callback_object; |
86 | int connection_status_callback_id; | 86 | int connection_status_callback_id; |
87 | 87 | ||
88 | int (*connection_data_callback)(void *object, int id, const uint8_t *data, uint16_t length, void *userdata); | 88 | connection_data_cb *connection_data_callback; |
89 | void *connection_data_callback_object; | 89 | void *connection_data_callback_object; |
90 | int connection_data_callback_id; | 90 | int connection_data_callback_id; |
91 | 91 | ||
92 | int (*connection_lossy_data_callback)(void *object, int id, const uint8_t *data, uint16_t length, void *userdata); | 92 | connection_lossy_data_cb *connection_lossy_data_callback; |
93 | void *connection_lossy_data_callback_object; | 93 | void *connection_lossy_data_callback_object; |
94 | int connection_lossy_data_callback_id; | 94 | int connection_lossy_data_callback_id; |
95 | 95 | ||
@@ -110,10 +110,12 @@ typedef struct { | |||
110 | uint64_t last_packets_left_requested_set; | 110 | uint64_t last_packets_left_requested_set; |
111 | double last_packets_left_requested_rem; | 111 | double last_packets_left_requested_rem; |
112 | 112 | ||
113 | uint32_t last_sendqueue_size[CONGESTION_QUEUE_ARRAY_SIZE], last_sendqueue_counter; | 113 | uint32_t last_sendqueue_size[CONGESTION_QUEUE_ARRAY_SIZE]; |
114 | long signed int last_num_packets_sent[CONGESTION_LAST_SENT_ARRAY_SIZE], | 114 | uint32_t last_sendqueue_counter; |
115 | last_num_packets_resent[CONGESTION_LAST_SENT_ARRAY_SIZE]; | 115 | long signed int last_num_packets_sent[CONGESTION_LAST_SENT_ARRAY_SIZE]; |
116 | uint32_t packets_sent, packets_resent; | 116 | long signed int last_num_packets_resent[CONGESTION_LAST_SENT_ARRAY_SIZE]; |
117 | uint32_t packets_sent; | ||
118 | uint32_t packets_resent; | ||
117 | uint64_t last_congestion_event; | 119 | uint64_t last_congestion_event; |
118 | uint64_t rtt_time; | 120 | uint64_t rtt_time; |
119 | 121 | ||
@@ -124,7 +126,7 @@ typedef struct { | |||
124 | 126 | ||
125 | pthread_mutex_t mutex; | 127 | pthread_mutex_t mutex; |
126 | 128 | ||
127 | void (*dht_pk_callback)(void *data, int32_t number, const uint8_t *dht_public_key, void *userdata); | 129 | dht_pk_cb *dht_pk_callback; |
128 | void *dht_pk_callback_object; | 130 | void *dht_pk_callback_object; |
129 | uint32_t dht_pk_callback_number; | 131 | uint32_t dht_pk_callback_number; |
130 | } Crypto_Connection; | 132 | } Crypto_Connection; |
@@ -150,7 +152,7 @@ struct Net_Crypto { | |||
150 | /* The secret key used for cookies */ | 152 | /* The secret key used for cookies */ |
151 | uint8_t secret_symmetric_key[CRYPTO_SYMMETRIC_KEY_SIZE]; | 153 | uint8_t secret_symmetric_key[CRYPTO_SYMMETRIC_KEY_SIZE]; |
152 | 154 | ||
153 | int (*new_connection_callback)(void *object, New_Connection *n_c); | 155 | new_connection_cb *new_connection_callback; |
154 | void *new_connection_callback_object; | 156 | void *new_connection_callback_object; |
155 | 157 | ||
156 | /* The current optimal sleep time */ | 158 | /* The current optimal sleep time */ |
@@ -1863,8 +1865,7 @@ static int crypto_connection_add_source(Net_Crypto *c, int crypt_connection_id, | |||
1863 | * | 1865 | * |
1864 | * n_c is only valid for the duration of the function call. | 1866 | * n_c is only valid for the duration of the function call. |
1865 | */ | 1867 | */ |
1866 | void new_connection_handler(Net_Crypto *c, int (*new_connection_callback)(void *object, New_Connection *n_c), | 1868 | void new_connection_handler(Net_Crypto *c, new_connection_cb *new_connection_callback, void *object) |
1867 | void *object) | ||
1868 | { | 1869 | { |
1869 | c->new_connection_callback = new_connection_callback; | 1870 | c->new_connection_callback = new_connection_callback; |
1870 | c->new_connection_callback_object = object; | 1871 | c->new_connection_callback_object = object; |
@@ -2267,7 +2268,7 @@ static void do_tcp(Net_Crypto *c, void *userdata) | |||
2267 | * return 0 on success. | 2268 | * return 0 on success. |
2268 | */ | 2269 | */ |
2269 | int connection_status_handler(const Net_Crypto *c, int crypt_connection_id, | 2270 | int connection_status_handler(const Net_Crypto *c, int crypt_connection_id, |
2270 | int (*connection_status_callback)(void *object, int id, uint8_t status, void *userdata), void *object, int id) | 2271 | connection_status_cb *connection_status_callback, void *object, int id) |
2271 | { | 2272 | { |
2272 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 2273 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
2273 | 2274 | ||
@@ -2289,8 +2290,8 @@ int connection_status_handler(const Net_Crypto *c, int crypt_connection_id, | |||
2289 | * return -1 on failure. | 2290 | * return -1 on failure. |
2290 | * return 0 on success. | 2291 | * return 0 on success. |
2291 | */ | 2292 | */ |
2292 | int connection_data_handler(const Net_Crypto *c, int crypt_connection_id, int (*connection_data_callback)(void *object, | 2293 | int connection_data_handler(const Net_Crypto *c, int crypt_connection_id, |
2293 | int id, const uint8_t *data, uint16_t length, void *userdata), void *object, int id) | 2294 | connection_data_cb *connection_data_callback, void *object, int id) |
2294 | { | 2295 | { |
2295 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 2296 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
2296 | 2297 | ||
@@ -2313,7 +2314,7 @@ int connection_data_handler(const Net_Crypto *c, int crypt_connection_id, int (* | |||
2313 | * return 0 on success. | 2314 | * return 0 on success. |
2314 | */ | 2315 | */ |
2315 | int connection_lossy_data_handler(Net_Crypto *c, int crypt_connection_id, | 2316 | int connection_lossy_data_handler(Net_Crypto *c, int crypt_connection_id, |
2316 | int (*connection_lossy_data_callback)(void *object, int id, const uint8_t *data, uint16_t length, void *userdata), | 2317 | connection_lossy_data_cb *connection_lossy_data_callback, |
2317 | void *object, int id) | 2318 | void *object, int id) |
2318 | { | 2319 | { |
2319 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 2320 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
@@ -2339,8 +2340,7 @@ int connection_lossy_data_handler(Net_Crypto *c, int crypt_connection_id, | |||
2339 | * return -1 on failure. | 2340 | * return -1 on failure. |
2340 | * return 0 on success. | 2341 | * return 0 on success. |
2341 | */ | 2342 | */ |
2342 | int nc_dht_pk_callback(Net_Crypto *c, int crypt_connection_id, void (*function)(void *data, int32_t number, | 2343 | int nc_dht_pk_callback(Net_Crypto *c, int crypt_connection_id, dht_pk_cb *function, void *object, uint32_t number) |
2343 | const uint8_t *dht_public_key, void *userdata), void *object, uint32_t number) | ||
2344 | { | 2344 | { |
2345 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 2345 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
2346 | 2346 | ||
@@ -2523,9 +2523,8 @@ static void send_crypto_packets(Net_Crypto *c) | |||
2523 | bool direct_connected = 0; | 2523 | bool direct_connected = 0; |
2524 | crypto_connection_status(c, i, &direct_connected, nullptr); | 2524 | crypto_connection_status(c, i, &direct_connected, nullptr); |
2525 | 2525 | ||
2526 | if (direct_connected && conn->last_tcp_sent + CONGESTION_EVENT_TIMEOUT > temp_time) { | 2526 | /* When switching from TCP to UDP, don't change the packet send rate for CONGESTION_EVENT_TIMEOUT ms. */ |
2527 | /* When switching from TCP to UDP, don't change the packet send rate for CONGESTION_EVENT_TIMEOUT ms. */ | 2527 | if (!(direct_connected && conn->last_tcp_sent + CONGESTION_EVENT_TIMEOUT > temp_time)) { |
2528 | } else { | ||
2529 | long signed int total_sent = 0, total_resent = 0; | 2528 | long signed int total_sent = 0, total_resent = 0; |
2530 | 2529 | ||
2531 | // TODO(irungentoo): use real delay | 2530 | // TODO(irungentoo): use real delay |
@@ -2586,8 +2585,10 @@ static void send_crypto_packets(Net_Crypto *c) | |||
2586 | } | 2585 | } |
2587 | 2586 | ||
2588 | if (conn->last_packets_left_set == 0 || conn->last_packets_left_requested_set == 0) { | 2587 | if (conn->last_packets_left_set == 0 || conn->last_packets_left_requested_set == 0) { |
2589 | conn->last_packets_left_requested_set = conn->last_packets_left_set = temp_time; | 2588 | conn->last_packets_left_requested_set = temp_time; |
2590 | conn->packets_left_requested = conn->packets_left = CRYPTO_MIN_QUEUE_LENGTH; | 2589 | conn->last_packets_left_set = temp_time; |
2590 | conn->packets_left_requested = CRYPTO_MIN_QUEUE_LENGTH; | ||
2591 | conn->packets_left = CRYPTO_MIN_QUEUE_LENGTH; | ||
2591 | } else { | 2592 | } else { |
2592 | if (((uint64_t)((1000.0 / conn->packet_send_rate) + 0.5) + conn->last_packets_left_set) <= temp_time) { | 2593 | if (((uint64_t)((1000.0 / conn->packet_send_rate) + 0.5) + conn->last_packets_left_set) <= temp_time) { |
2593 | double n_packets = conn->packet_send_rate * (((double)(temp_time - conn->last_packets_left_set)) / 1000.0); | 2594 | double n_packets = conn->packet_send_rate * (((double)(temp_time - conn->last_packets_left_set)) / 1000.0); |
@@ -2740,7 +2741,7 @@ int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, const uint8_t | |||
2740 | if (congestion_control) { | 2741 | if (congestion_control) { |
2741 | --conn->packets_left; | 2742 | --conn->packets_left; |
2742 | --conn->packets_left_requested; | 2743 | --conn->packets_left_requested; |
2743 | conn->packets_sent++; | 2744 | ++conn->packets_sent; |
2744 | } | 2745 | } |
2745 | 2746 | ||
2746 | return ret; | 2747 | return ret; |
@@ -2868,7 +2869,7 @@ int crypto_kill(Net_Crypto *c, int crypt_connection_id) | |||
2868 | * sets direct_connected to 1 if connection connects directly to other, 0 if it isn't. | 2869 | * sets direct_connected to 1 if connection connects directly to other, 0 if it isn't. |
2869 | * sets online_tcp_relays to the number of connected tcp relays this connection has. | 2870 | * sets online_tcp_relays to the number of connected tcp relays this connection has. |
2870 | */ | 2871 | */ |
2871 | CRYPTO_CONN_STATE crypto_connection_status(const Net_Crypto *c, int crypt_connection_id, bool *direct_connected, | 2872 | Crypto_Conn_State crypto_connection_status(const Net_Crypto *c, int crypt_connection_id, bool *direct_connected, |
2872 | unsigned int *online_tcp_relays) | 2873 | unsigned int *online_tcp_relays) |
2873 | { | 2874 | { |
2874 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 2875 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
@@ -3002,6 +3003,7 @@ static void kill_timedout(Net_Crypto *c, void *userdata) | |||
3002 | 3003 | ||
3003 | if (conn->status == CRYPTO_CONN_ESTABLISHED) { | 3004 | if (conn->status == CRYPTO_CONN_ESTABLISHED) { |
3004 | // TODO(irungentoo): add a timeout here? | 3005 | // TODO(irungentoo): add a timeout here? |
3006 | do_timeout_here(); | ||
3005 | } | 3007 | } |
3006 | 3008 | ||
3007 | #endif | 3009 | #endif |
diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h index e6a4efd7..2ed35883 100644 --- a/toxcore/net_crypto.h +++ b/toxcore/net_crypto.h | |||
@@ -31,13 +31,13 @@ | |||
31 | 31 | ||
32 | #include <pthread.h> | 32 | #include <pthread.h> |
33 | 33 | ||
34 | typedef enum CRYPTO_CONN_STATE { | 34 | typedef enum Crypto_Conn_State { |
35 | CRYPTO_CONN_NO_CONNECTION = 0, | 35 | CRYPTO_CONN_NO_CONNECTION = 0, |
36 | CRYPTO_CONN_COOKIE_REQUESTING = 1, // send cookie request packets | 36 | CRYPTO_CONN_COOKIE_REQUESTING = 1, // send cookie request packets |
37 | CRYPTO_CONN_HANDSHAKE_SENT = 2, // send handshake packets | 37 | CRYPTO_CONN_HANDSHAKE_SENT = 2, // send handshake packets |
38 | CRYPTO_CONN_NOT_CONFIRMED = 3, // send handshake packets, we have received one from the other | 38 | CRYPTO_CONN_NOT_CONFIRMED = 3, // send handshake packets, we have received one from the other |
39 | CRYPTO_CONN_ESTABLISHED = 4, | 39 | CRYPTO_CONN_ESTABLISHED = 4, |
40 | } CRYPTO_CONN_STATE; | 40 | } Crypto_Conn_State; |
41 | 41 | ||
42 | /* Maximum size of receiving and sending packet buffers. */ | 42 | /* Maximum size of receiving and sending packet buffers. */ |
43 | #define CRYPTO_PACKET_BUFFER_SIZE 32768 /* Must be a power of 2 */ | 43 | #define CRYPTO_PACKET_BUFFER_SIZE 32768 /* Must be a power of 2 */ |
@@ -108,14 +108,19 @@ typedef struct New_Connection { | |||
108 | uint8_t cookie_length; | 108 | uint8_t cookie_length; |
109 | } New_Connection; | 109 | } New_Connection; |
110 | 110 | ||
111 | typedef int connection_status_cb(void *object, int id, uint8_t status, void *userdata); | ||
112 | typedef int connection_data_cb(void *object, int id, const uint8_t *data, uint16_t length, void *userdata); | ||
113 | typedef int connection_lossy_data_cb(void *object, int id, const uint8_t *data, uint16_t length, void *userdata); | ||
114 | typedef void dht_pk_cb(void *data, int32_t number, const uint8_t *dht_public_key, void *userdata); | ||
115 | typedef int new_connection_cb(void *object, New_Connection *n_c); | ||
116 | |||
111 | /* Set function to be called when someone requests a new connection to us. | 117 | /* Set function to be called when someone requests a new connection to us. |
112 | * | 118 | * |
113 | * The set function should return -1 on failure and 0 on success. | 119 | * The set function should return -1 on failure and 0 on success. |
114 | * | 120 | * |
115 | * n_c is only valid for the duration of the function call. | 121 | * n_c is only valid for the duration of the function call. |
116 | */ | 122 | */ |
117 | void new_connection_handler(Net_Crypto *c, int (*new_connection_callback)(void *object, New_Connection *n_c), | 123 | void new_connection_handler(Net_Crypto *c, new_connection_cb *new_connection_callback, void *object); |
118 | void *object); | ||
119 | 124 | ||
120 | /* Accept a crypto connection. | 125 | /* Accept a crypto connection. |
121 | * | 126 | * |
@@ -152,7 +157,7 @@ int set_direct_ip_port(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port, | |||
152 | * return 0 on success. | 157 | * return 0 on success. |
153 | */ | 158 | */ |
154 | int connection_status_handler(const Net_Crypto *c, int crypt_connection_id, | 159 | int connection_status_handler(const Net_Crypto *c, int crypt_connection_id, |
155 | int (*connection_status_callback)(void *object, int id, uint8_t status, void *userdata), void *object, int id); | 160 | connection_status_cb *connection_status_callback, void *object, int id); |
156 | 161 | ||
157 | /* Set function to be called when connection with crypt_connection_id receives a lossless data packet of length. | 162 | /* Set function to be called when connection with crypt_connection_id receives a lossless data packet of length. |
158 | * | 163 | * |
@@ -162,8 +167,8 @@ int connection_status_handler(const Net_Crypto *c, int crypt_connection_id, | |||
162 | * return -1 on failure. | 167 | * return -1 on failure. |
163 | * return 0 on success. | 168 | * return 0 on success. |
164 | */ | 169 | */ |
165 | int connection_data_handler(const Net_Crypto *c, int crypt_connection_id, int (*connection_data_callback)(void *object, | 170 | int connection_data_handler(const Net_Crypto *c, int crypt_connection_id, |
166 | int id, const uint8_t *data, uint16_t length, void *userdata), void *object, int id); | 171 | connection_data_cb *connection_data_callback, void *object, int id); |
167 | 172 | ||
168 | 173 | ||
169 | /* Set function to be called when connection with crypt_connection_id receives a lossy data packet of length. | 174 | /* Set function to be called when connection with crypt_connection_id receives a lossy data packet of length. |
@@ -175,9 +180,7 @@ int connection_data_handler(const Net_Crypto *c, int crypt_connection_id, int (* | |||
175 | * return 0 on success. | 180 | * return 0 on success. |
176 | */ | 181 | */ |
177 | int connection_lossy_data_handler(Net_Crypto *c, int crypt_connection_id, | 182 | int connection_lossy_data_handler(Net_Crypto *c, int crypt_connection_id, |
178 | int (*connection_lossy_data_callback)(void *object, int id, const uint8_t *data, uint16_t length, void *userdata), | 183 | connection_lossy_data_cb *connection_lossy_data_callback, void *object, int id); |
179 | void *object, | ||
180 | int id); | ||
181 | 184 | ||
182 | /* Set the function for this friend that will be callbacked with object and number if | 185 | /* Set the function for this friend that will be callbacked with object and number if |
183 | * the friend sends us a different dht public key than we have associated to him. | 186 | * the friend sends us a different dht public key than we have associated to him. |
@@ -189,8 +192,7 @@ int connection_lossy_data_handler(Net_Crypto *c, int crypt_connection_id, | |||
189 | * return -1 on failure. | 192 | * return -1 on failure. |
190 | * return 0 on success. | 193 | * return 0 on success. |
191 | */ | 194 | */ |
192 | int nc_dht_pk_callback(Net_Crypto *c, int crypt_connection_id, void (*function)(void *data, int32_t number, | 195 | int nc_dht_pk_callback(Net_Crypto *c, int crypt_connection_id, dht_pk_cb *function, void *object, uint32_t number); |
193 | const uint8_t *dht_public_key, void *userdata), void *object, uint32_t number); | ||
194 | 196 | ||
195 | /* returns the number of packet slots left in the sendbuffer. | 197 | /* returns the number of packet slots left in the sendbuffer. |
196 | * return 0 if failure. | 198 | * return 0 if failure. |
@@ -278,7 +280,7 @@ int crypto_kill(Net_Crypto *c, int crypt_connection_id); | |||
278 | * sets direct_connected to 1 if connection connects directly to other, 0 if it isn't. | 280 | * sets direct_connected to 1 if connection connects directly to other, 0 if it isn't. |
279 | * sets online_tcp_relays to the number of connected tcp relays this connection has. | 281 | * sets online_tcp_relays to the number of connected tcp relays this connection has. |
280 | */ | 282 | */ |
281 | CRYPTO_CONN_STATE crypto_connection_status(const Net_Crypto *c, int crypt_connection_id, bool *direct_connected, | 283 | Crypto_Conn_State crypto_connection_status(const Net_Crypto *c, int crypt_connection_id, bool *direct_connected, |
282 | unsigned int *online_tcp_relays); | 284 | unsigned int *online_tcp_relays); |
283 | 285 | ||
284 | /* Generate our public and private keys. | 286 | /* Generate our public and private keys. |