diff options
Diffstat (limited to 'toxcore/net_crypto.c')
-rw-r--r-- | toxcore/net_crypto.c | 712 |
1 files changed, 450 insertions, 262 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index f8a85adf..16b3c6d2 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c | |||
@@ -28,20 +28,25 @@ | |||
28 | #endif | 28 | #endif |
29 | 29 | ||
30 | #include "net_crypto.h" | 30 | #include "net_crypto.h" |
31 | |||
31 | #include "util.h" | 32 | #include "util.h" |
32 | #include "math.h" | 33 | |
33 | #include "logger.h" | 34 | #include <math.h> |
35 | |||
34 | 36 | ||
35 | static uint8_t crypt_connection_id_not_valid(const Net_Crypto *c, int crypt_connection_id) | 37 | static uint8_t crypt_connection_id_not_valid(const Net_Crypto *c, int crypt_connection_id) |
36 | { | 38 | { |
37 | if ((uint32_t)crypt_connection_id >= c->crypto_connections_length) | 39 | if ((uint32_t)crypt_connection_id >= c->crypto_connections_length) { |
38 | return 1; | 40 | return 1; |
41 | } | ||
39 | 42 | ||
40 | if (c->crypto_connections == NULL) | 43 | if (c->crypto_connections == NULL) { |
41 | return 1; | 44 | return 1; |
45 | } | ||
42 | 46 | ||
43 | if (c->crypto_connections[crypt_connection_id].status == CRYPTO_CONN_NO_CONNECTION) | 47 | if (c->crypto_connections[crypt_connection_id].status == CRYPTO_CONN_NO_CONNECTION) { |
44 | return 1; | 48 | return 1; |
49 | } | ||
45 | 50 | ||
46 | return 0; | 51 | return 0; |
47 | } | 52 | } |
@@ -83,8 +88,9 @@ static int create_cookie_request(const Net_Crypto *c, uint8_t *packet, uint8_t * | |||
83 | int len = encrypt_data_symmetric(shared_key, nonce, plain, sizeof(plain), | 88 | int len = encrypt_data_symmetric(shared_key, nonce, plain, sizeof(plain), |
84 | packet + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES); | 89 | packet + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES); |
85 | 90 | ||
86 | if (len != COOKIE_REQUEST_PLAIN_LENGTH + crypto_box_MACBYTES) | 91 | if (len != COOKIE_REQUEST_PLAIN_LENGTH + crypto_box_MACBYTES) { |
87 | return -1; | 92 | return -1; |
93 | } | ||
88 | 94 | ||
89 | return (1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + len); | 95 | return (1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + len); |
90 | } | 96 | } |
@@ -103,8 +109,9 @@ static int create_cookie(uint8_t *cookie, const uint8_t *bytes, const uint8_t *e | |||
103 | new_nonce(cookie); | 109 | new_nonce(cookie); |
104 | int len = encrypt_data_symmetric(encryption_key, cookie, contents, sizeof(contents), cookie + crypto_box_NONCEBYTES); | 110 | int len = encrypt_data_symmetric(encryption_key, cookie, contents, sizeof(contents), cookie + crypto_box_NONCEBYTES); |
105 | 111 | ||
106 | if (len != COOKIE_LENGTH - crypto_box_NONCEBYTES) | 112 | if (len != COOKIE_LENGTH - crypto_box_NONCEBYTES) { |
107 | return -1; | 113 | return -1; |
114 | } | ||
108 | 115 | ||
109 | return 0; | 116 | return 0; |
110 | } | 117 | } |
@@ -120,15 +127,17 @@ static int open_cookie(uint8_t *bytes, const uint8_t *cookie, const uint8_t *enc | |||
120 | int len = decrypt_data_symmetric(encryption_key, cookie, cookie + crypto_box_NONCEBYTES, | 127 | int len = decrypt_data_symmetric(encryption_key, cookie, cookie + crypto_box_NONCEBYTES, |
121 | COOKIE_LENGTH - crypto_box_NONCEBYTES, contents); | 128 | COOKIE_LENGTH - crypto_box_NONCEBYTES, contents); |
122 | 129 | ||
123 | if (len != sizeof(contents)) | 130 | if (len != sizeof(contents)) { |
124 | return -1; | 131 | return -1; |
132 | } | ||
125 | 133 | ||
126 | uint64_t cookie_time; | 134 | uint64_t cookie_time; |
127 | memcpy(&cookie_time, contents, sizeof(cookie_time)); | 135 | memcpy(&cookie_time, contents, sizeof(cookie_time)); |
128 | uint64_t temp_time = unix_time(); | 136 | uint64_t temp_time = unix_time(); |
129 | 137 | ||
130 | if (cookie_time + COOKIE_TIMEOUT < temp_time || temp_time < cookie_time) | 138 | if (cookie_time + COOKIE_TIMEOUT < temp_time || temp_time < cookie_time) { |
131 | return -1; | 139 | return -1; |
140 | } | ||
132 | 141 | ||
133 | memcpy(bytes, contents + sizeof(cookie_time), COOKIE_DATA_LENGTH); | 142 | memcpy(bytes, contents + sizeof(cookie_time), COOKIE_DATA_LENGTH); |
134 | return 0; | 143 | return 0; |
@@ -150,16 +159,18 @@ static int create_cookie_response(const Net_Crypto *c, uint8_t *packet, const ui | |||
150 | memcpy(cookie_plain + crypto_box_PUBLICKEYBYTES, dht_public_key, crypto_box_PUBLICKEYBYTES); | 159 | memcpy(cookie_plain + crypto_box_PUBLICKEYBYTES, dht_public_key, crypto_box_PUBLICKEYBYTES); |
151 | uint8_t plain[COOKIE_LENGTH + sizeof(uint64_t)]; | 160 | uint8_t plain[COOKIE_LENGTH + sizeof(uint64_t)]; |
152 | 161 | ||
153 | if (create_cookie(plain, cookie_plain, c->secret_symmetric_key) != 0) | 162 | if (create_cookie(plain, cookie_plain, c->secret_symmetric_key) != 0) { |
154 | return -1; | 163 | return -1; |
164 | } | ||
155 | 165 | ||
156 | memcpy(plain + COOKIE_LENGTH, request_plain + COOKIE_DATA_LENGTH, sizeof(uint64_t)); | 166 | memcpy(plain + COOKIE_LENGTH, request_plain + COOKIE_DATA_LENGTH, sizeof(uint64_t)); |
157 | packet[0] = NET_PACKET_COOKIE_RESPONSE; | 167 | packet[0] = NET_PACKET_COOKIE_RESPONSE; |
158 | new_nonce(packet + 1); | 168 | new_nonce(packet + 1); |
159 | int len = encrypt_data_symmetric(shared_key, packet + 1, plain, sizeof(plain), packet + 1 + crypto_box_NONCEBYTES); | 169 | int len = encrypt_data_symmetric(shared_key, packet + 1, plain, sizeof(plain), packet + 1 + crypto_box_NONCEBYTES); |
160 | 170 | ||
161 | if (len != COOKIE_RESPONSE_LENGTH - (1 + crypto_box_NONCEBYTES)) | 171 | if (len != COOKIE_RESPONSE_LENGTH - (1 + crypto_box_NONCEBYTES)) { |
162 | return -1; | 172 | return -1; |
173 | } | ||
163 | 174 | ||
164 | return COOKIE_RESPONSE_LENGTH; | 175 | return COOKIE_RESPONSE_LENGTH; |
165 | } | 176 | } |
@@ -174,8 +185,9 @@ static int create_cookie_response(const Net_Crypto *c, uint8_t *packet, const ui | |||
174 | static int handle_cookie_request(const Net_Crypto *c, uint8_t *request_plain, uint8_t *shared_key, | 185 | static int handle_cookie_request(const Net_Crypto *c, uint8_t *request_plain, uint8_t *shared_key, |
175 | uint8_t *dht_public_key, const uint8_t *packet, uint16_t length) | 186 | uint8_t *dht_public_key, const uint8_t *packet, uint16_t length) |
176 | { | 187 | { |
177 | if (length != COOKIE_REQUEST_LENGTH) | 188 | if (length != COOKIE_REQUEST_LENGTH) { |
178 | return -1; | 189 | return -1; |
190 | } | ||
179 | 191 | ||
180 | memcpy(dht_public_key, packet + 1, crypto_box_PUBLICKEYBYTES); | 192 | memcpy(dht_public_key, packet + 1, crypto_box_PUBLICKEYBYTES); |
181 | DHT_get_shared_key_sent(c->dht, shared_key, dht_public_key); | 193 | DHT_get_shared_key_sent(c->dht, shared_key, dht_public_key); |
@@ -183,31 +195,36 @@ static int handle_cookie_request(const Net_Crypto *c, uint8_t *request_plain, ui | |||
183 | packet + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES, COOKIE_REQUEST_PLAIN_LENGTH + crypto_box_MACBYTES, | 195 | packet + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES, COOKIE_REQUEST_PLAIN_LENGTH + crypto_box_MACBYTES, |
184 | request_plain); | 196 | request_plain); |
185 | 197 | ||
186 | if (len != COOKIE_REQUEST_PLAIN_LENGTH) | 198 | if (len != COOKIE_REQUEST_PLAIN_LENGTH) { |
187 | return -1; | 199 | return -1; |
200 | } | ||
188 | 201 | ||
189 | return 0; | 202 | return 0; |
190 | } | 203 | } |
191 | 204 | ||
192 | /* Handle the cookie request packet (for raw UDP) | 205 | /* Handle the cookie request packet (for raw UDP) |
193 | */ | 206 | */ |
194 | static int udp_handle_cookie_request(void *object, IP_Port source, const uint8_t *packet, uint16_t length) | 207 | static int udp_handle_cookie_request(void *object, IP_Port source, const uint8_t *packet, uint16_t length, |
208 | void *userdata) | ||
195 | { | 209 | { |
196 | Net_Crypto *c = object; | 210 | Net_Crypto *c = (Net_Crypto *)object; |
197 | uint8_t request_plain[COOKIE_REQUEST_PLAIN_LENGTH]; | 211 | uint8_t request_plain[COOKIE_REQUEST_PLAIN_LENGTH]; |
198 | uint8_t shared_key[crypto_box_BEFORENMBYTES]; | 212 | uint8_t shared_key[crypto_box_BEFORENMBYTES]; |
199 | uint8_t dht_public_key[crypto_box_PUBLICKEYBYTES]; | 213 | uint8_t dht_public_key[crypto_box_PUBLICKEYBYTES]; |
200 | 214 | ||
201 | if (handle_cookie_request(c, request_plain, shared_key, dht_public_key, packet, length) != 0) | 215 | if (handle_cookie_request(c, request_plain, shared_key, dht_public_key, packet, length) != 0) { |
202 | return 1; | 216 | return 1; |
217 | } | ||
203 | 218 | ||
204 | uint8_t data[COOKIE_RESPONSE_LENGTH]; | 219 | uint8_t data[COOKIE_RESPONSE_LENGTH]; |
205 | 220 | ||
206 | if (create_cookie_response(c, data, request_plain, shared_key, dht_public_key) != sizeof(data)) | 221 | if (create_cookie_response(c, data, request_plain, shared_key, dht_public_key) != sizeof(data)) { |
207 | return 1; | 222 | return 1; |
223 | } | ||
208 | 224 | ||
209 | if ((uint32_t)sendpacket(c->dht->net, source, data, sizeof(data)) != sizeof(data)) | 225 | if ((uint32_t)sendpacket(c->dht->net, source, data, sizeof(data)) != sizeof(data)) { |
210 | return 1; | 226 | return 1; |
227 | } | ||
211 | 228 | ||
212 | return 0; | 229 | return 0; |
213 | } | 230 | } |
@@ -220,13 +237,15 @@ static int tcp_handle_cookie_request(Net_Crypto *c, int connections_number, cons | |||
220 | uint8_t shared_key[crypto_box_BEFORENMBYTES]; | 237 | uint8_t shared_key[crypto_box_BEFORENMBYTES]; |
221 | uint8_t dht_public_key[crypto_box_PUBLICKEYBYTES]; | 238 | uint8_t dht_public_key[crypto_box_PUBLICKEYBYTES]; |
222 | 239 | ||
223 | if (handle_cookie_request(c, request_plain, shared_key, dht_public_key, packet, length) != 0) | 240 | if (handle_cookie_request(c, request_plain, shared_key, dht_public_key, packet, length) != 0) { |
224 | return -1; | 241 | return -1; |
242 | } | ||
225 | 243 | ||
226 | uint8_t data[COOKIE_RESPONSE_LENGTH]; | 244 | uint8_t data[COOKIE_RESPONSE_LENGTH]; |
227 | 245 | ||
228 | if (create_cookie_response(c, data, request_plain, shared_key, dht_public_key) != sizeof(data)) | 246 | if (create_cookie_response(c, data, request_plain, shared_key, dht_public_key) != sizeof(data)) { |
229 | return -1; | 247 | return -1; |
248 | } | ||
230 | 249 | ||
231 | int ret = send_packet_tcp_connection(c->tcp_c, connections_number, data, sizeof(data)); | 250 | int ret = send_packet_tcp_connection(c->tcp_c, connections_number, data, sizeof(data)); |
232 | return ret; | 251 | return ret; |
@@ -241,16 +260,19 @@ static int tcp_oob_handle_cookie_request(const Net_Crypto *c, unsigned int tcp_c | |||
241 | uint8_t shared_key[crypto_box_BEFORENMBYTES]; | 260 | uint8_t shared_key[crypto_box_BEFORENMBYTES]; |
242 | uint8_t dht_public_key_temp[crypto_box_PUBLICKEYBYTES]; | 261 | uint8_t dht_public_key_temp[crypto_box_PUBLICKEYBYTES]; |
243 | 262 | ||
244 | if (handle_cookie_request(c, request_plain, shared_key, dht_public_key_temp, packet, length) != 0) | 263 | if (handle_cookie_request(c, request_plain, shared_key, dht_public_key_temp, packet, length) != 0) { |
245 | return -1; | 264 | return -1; |
265 | } | ||
246 | 266 | ||
247 | if (public_key_cmp(dht_public_key, dht_public_key_temp) != 0) | 267 | if (public_key_cmp(dht_public_key, dht_public_key_temp) != 0) { |
248 | return -1; | 268 | return -1; |
269 | } | ||
249 | 270 | ||
250 | uint8_t data[COOKIE_RESPONSE_LENGTH]; | 271 | uint8_t data[COOKIE_RESPONSE_LENGTH]; |
251 | 272 | ||
252 | if (create_cookie_response(c, data, request_plain, shared_key, dht_public_key) != sizeof(data)) | 273 | if (create_cookie_response(c, data, request_plain, shared_key, dht_public_key) != sizeof(data)) { |
253 | return -1; | 274 | return -1; |
275 | } | ||
254 | 276 | ||
255 | int ret = tcp_send_oob_packet(c->tcp_c, tcp_connections_number, dht_public_key, data, sizeof(data)); | 277 | int ret = tcp_send_oob_packet(c->tcp_c, tcp_connections_number, dht_public_key, data, sizeof(data)); |
256 | return ret; | 278 | return ret; |
@@ -267,15 +289,17 @@ static int tcp_oob_handle_cookie_request(const Net_Crypto *c, unsigned int tcp_c | |||
267 | static int handle_cookie_response(uint8_t *cookie, uint64_t *number, const uint8_t *packet, uint16_t length, | 289 | static int handle_cookie_response(uint8_t *cookie, uint64_t *number, const uint8_t *packet, uint16_t length, |
268 | const uint8_t *shared_key) | 290 | const uint8_t *shared_key) |
269 | { | 291 | { |
270 | if (length != COOKIE_RESPONSE_LENGTH) | 292 | if (length != COOKIE_RESPONSE_LENGTH) { |
271 | return -1; | 293 | return -1; |
294 | } | ||
272 | 295 | ||
273 | uint8_t plain[COOKIE_LENGTH + sizeof(uint64_t)]; | 296 | uint8_t plain[COOKIE_LENGTH + sizeof(uint64_t)]; |
274 | int len = decrypt_data_symmetric(shared_key, packet + 1, packet + 1 + crypto_box_NONCEBYTES, | 297 | int len = decrypt_data_symmetric(shared_key, packet + 1, packet + 1 + crypto_box_NONCEBYTES, |
275 | length - (1 + crypto_box_NONCEBYTES), plain); | 298 | length - (1 + crypto_box_NONCEBYTES), plain); |
276 | 299 | ||
277 | if (len != sizeof(plain)) | 300 | if (len != sizeof(plain)) { |
278 | return -1; | 301 | return -1; |
302 | } | ||
279 | 303 | ||
280 | memcpy(cookie, plain, COOKIE_LENGTH); | 304 | memcpy(cookie, plain, COOKIE_LENGTH); |
281 | memcpy(number, plain + COOKIE_LENGTH, sizeof(uint64_t)); | 305 | memcpy(number, plain + COOKIE_LENGTH, sizeof(uint64_t)); |
@@ -303,15 +327,17 @@ static int create_crypto_handshake(const Net_Crypto *c, uint8_t *packet, const u | |||
303 | memcpy(cookie_plain + crypto_box_PUBLICKEYBYTES, peer_dht_pubkey, crypto_box_PUBLICKEYBYTES); | 327 | memcpy(cookie_plain + crypto_box_PUBLICKEYBYTES, peer_dht_pubkey, crypto_box_PUBLICKEYBYTES); |
304 | 328 | ||
305 | if (create_cookie(plain + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + crypto_hash_sha512_BYTES, cookie_plain, | 329 | if (create_cookie(plain + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + crypto_hash_sha512_BYTES, cookie_plain, |
306 | c->secret_symmetric_key) != 0) | 330 | c->secret_symmetric_key) != 0) { |
307 | return -1; | 331 | return -1; |
332 | } | ||
308 | 333 | ||
309 | new_nonce(packet + 1 + COOKIE_LENGTH); | 334 | new_nonce(packet + 1 + COOKIE_LENGTH); |
310 | int len = encrypt_data(peer_real_pk, c->self_secret_key, packet + 1 + COOKIE_LENGTH, plain, sizeof(plain), | 335 | int len = encrypt_data(peer_real_pk, c->self_secret_key, packet + 1 + COOKIE_LENGTH, plain, sizeof(plain), |
311 | packet + 1 + COOKIE_LENGTH + crypto_box_NONCEBYTES); | 336 | packet + 1 + COOKIE_LENGTH + crypto_box_NONCEBYTES); |
312 | 337 | ||
313 | if (len != HANDSHAKE_PACKET_LENGTH - (1 + COOKIE_LENGTH + crypto_box_NONCEBYTES)) | 338 | if (len != HANDSHAKE_PACKET_LENGTH - (1 + COOKIE_LENGTH + crypto_box_NONCEBYTES)) { |
314 | return -1; | 339 | return -1; |
340 | } | ||
315 | 341 | ||
316 | packet[0] = NET_PACKET_CRYPTO_HS; | 342 | packet[0] = NET_PACKET_CRYPTO_HS; |
317 | memcpy(packet + 1, cookie, COOKIE_LENGTH); | 343 | memcpy(packet + 1, cookie, COOKIE_LENGTH); |
@@ -340,17 +366,21 @@ static int create_crypto_handshake(const Net_Crypto *c, uint8_t *packet, const u | |||
340 | static int handle_crypto_handshake(const Net_Crypto *c, uint8_t *nonce, uint8_t *session_pk, uint8_t *peer_real_pk, | 366 | static int handle_crypto_handshake(const Net_Crypto *c, uint8_t *nonce, uint8_t *session_pk, uint8_t *peer_real_pk, |
341 | uint8_t *dht_public_key, uint8_t *cookie, const uint8_t *packet, uint16_t length, const uint8_t *expected_real_pk) | 367 | uint8_t *dht_public_key, uint8_t *cookie, const uint8_t *packet, uint16_t length, const uint8_t *expected_real_pk) |
342 | { | 368 | { |
343 | if (length != HANDSHAKE_PACKET_LENGTH) | 369 | if (length != HANDSHAKE_PACKET_LENGTH) { |
344 | return -1; | 370 | return -1; |
371 | } | ||
345 | 372 | ||
346 | uint8_t cookie_plain[COOKIE_DATA_LENGTH]; | 373 | uint8_t cookie_plain[COOKIE_DATA_LENGTH]; |
347 | 374 | ||
348 | if (open_cookie(cookie_plain, packet + 1, c->secret_symmetric_key) != 0) | 375 | if (open_cookie(cookie_plain, packet + 1, c->secret_symmetric_key) != 0) { |
349 | return -1; | 376 | return -1; |
377 | } | ||
350 | 378 | ||
351 | if (expected_real_pk) | 379 | if (expected_real_pk) { |
352 | if (public_key_cmp(cookie_plain, expected_real_pk) != 0) | 380 | if (public_key_cmp(cookie_plain, expected_real_pk) != 0) { |
353 | return -1; | 381 | return -1; |
382 | } | ||
383 | } | ||
354 | 384 | ||
355 | uint8_t cookie_hash[crypto_hash_sha512_BYTES]; | 385 | uint8_t cookie_hash[crypto_hash_sha512_BYTES]; |
356 | crypto_hash_sha512(cookie_hash, packet + 1, COOKIE_LENGTH); | 386 | crypto_hash_sha512(cookie_hash, packet + 1, COOKIE_LENGTH); |
@@ -360,12 +390,14 @@ static int handle_crypto_handshake(const Net_Crypto *c, uint8_t *nonce, uint8_t | |||
360 | packet + 1 + COOKIE_LENGTH + crypto_box_NONCEBYTES, | 390 | packet + 1 + COOKIE_LENGTH + crypto_box_NONCEBYTES, |
361 | HANDSHAKE_PACKET_LENGTH - (1 + COOKIE_LENGTH + crypto_box_NONCEBYTES), plain); | 391 | HANDSHAKE_PACKET_LENGTH - (1 + COOKIE_LENGTH + crypto_box_NONCEBYTES), plain); |
362 | 392 | ||
363 | if (len != sizeof(plain)) | 393 | if (len != sizeof(plain)) { |
364 | return -1; | 394 | return -1; |
395 | } | ||
365 | 396 | ||
366 | if (sodium_memcmp(cookie_hash, plain + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES, | 397 | if (sodium_memcmp(cookie_hash, plain + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES, |
367 | crypto_hash_sha512_BYTES) != 0) | 398 | crypto_hash_sha512_BYTES) != 0) { |
368 | return -1; | 399 | return -1; |
400 | } | ||
369 | 401 | ||
370 | memcpy(nonce, plain, crypto_box_NONCEBYTES); | 402 | memcpy(nonce, plain, crypto_box_NONCEBYTES); |
371 | memcpy(session_pk, plain + crypto_box_NONCEBYTES, crypto_box_PUBLICKEYBYTES); | 403 | memcpy(session_pk, plain + crypto_box_NONCEBYTES, crypto_box_PUBLICKEYBYTES); |
@@ -378,8 +410,9 @@ static int handle_crypto_handshake(const Net_Crypto *c, uint8_t *nonce, uint8_t | |||
378 | 410 | ||
379 | static Crypto_Connection *get_crypto_connection(const Net_Crypto *c, int crypt_connection_id) | 411 | static Crypto_Connection *get_crypto_connection(const Net_Crypto *c, int crypt_connection_id) |
380 | { | 412 | { |
381 | if (crypt_connection_id_not_valid(c, crypt_connection_id)) | 413 | if (crypt_connection_id_not_valid(c, crypt_connection_id)) { |
382 | return 0; | 414 | return 0; |
415 | } | ||
383 | 416 | ||
384 | return &c->crypto_connections[crypt_connection_id]; | 417 | return &c->crypto_connections[crypt_connection_id]; |
385 | } | 418 | } |
@@ -394,13 +427,15 @@ static int add_ip_port_connection(Net_Crypto *c, int crypt_connection_id, IP_Por | |||
394 | { | 427 | { |
395 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 428 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
396 | 429 | ||
397 | if (conn == 0) | 430 | if (conn == 0) { |
398 | return -1; | 431 | return -1; |
432 | } | ||
399 | 433 | ||
400 | if (ip_port.ip.family == AF_INET) { | 434 | if (ip_port.ip.family == AF_INET) { |
401 | if (!ipport_equal(&ip_port, &conn->ip_portv4) && LAN_ip(conn->ip_portv4.ip) != 0) { | 435 | if (!ipport_equal(&ip_port, &conn->ip_portv4) && LAN_ip(conn->ip_portv4.ip) != 0) { |
402 | if (!bs_list_add(&c->ip_port_list, (uint8_t *)&ip_port, crypt_connection_id)) | 436 | if (!bs_list_add(&c->ip_port_list, (uint8_t *)&ip_port, crypt_connection_id)) { |
403 | return -1; | 437 | return -1; |
438 | } | ||
404 | 439 | ||
405 | bs_list_remove(&c->ip_port_list, (uint8_t *)&conn->ip_portv4, crypt_connection_id); | 440 | bs_list_remove(&c->ip_port_list, (uint8_t *)&conn->ip_portv4, crypt_connection_id); |
406 | conn->ip_portv4 = ip_port; | 441 | conn->ip_portv4 = ip_port; |
@@ -408,8 +443,9 @@ static int add_ip_port_connection(Net_Crypto *c, int crypt_connection_id, IP_Por | |||
408 | } | 443 | } |
409 | } else if (ip_port.ip.family == AF_INET6) { | 444 | } else if (ip_port.ip.family == AF_INET6) { |
410 | if (!ipport_equal(&ip_port, &conn->ip_portv6)) { | 445 | if (!ipport_equal(&ip_port, &conn->ip_portv6)) { |
411 | if (!bs_list_add(&c->ip_port_list, (uint8_t *)&ip_port, crypt_connection_id)) | 446 | if (!bs_list_add(&c->ip_port_list, (uint8_t *)&ip_port, crypt_connection_id)) { |
412 | return -1; | 447 | return -1; |
448 | } | ||
413 | 449 | ||
414 | bs_list_remove(&c->ip_port_list, (uint8_t *)&conn->ip_portv6, crypt_connection_id); | 450 | bs_list_remove(&c->ip_port_list, (uint8_t *)&conn->ip_portv6, crypt_connection_id); |
415 | conn->ip_portv6 = ip_port; | 451 | conn->ip_portv6 = ip_port; |
@@ -425,18 +461,19 @@ static int add_ip_port_connection(Net_Crypto *c, int crypt_connection_id, IP_Por | |||
425 | * return IP_Port with family 0 on failure. | 461 | * return IP_Port with family 0 on failure. |
426 | * return IP_Port on success. | 462 | * return IP_Port on success. |
427 | */ | 463 | */ |
428 | IP_Port return_ip_port_connection(Net_Crypto *c, int crypt_connection_id) | 464 | static IP_Port return_ip_port_connection(Net_Crypto *c, int crypt_connection_id) |
429 | { | 465 | { |
430 | IP_Port empty; | 466 | IP_Port empty; |
431 | empty.ip.family = 0; | 467 | empty.ip.family = 0; |
432 | 468 | ||
433 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 469 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
434 | 470 | ||
435 | if (conn == 0) | 471 | if (conn == 0) { |
436 | return empty; | 472 | return empty; |
473 | } | ||
437 | 474 | ||
438 | uint64_t current_time = unix_time(); | 475 | uint64_t current_time = unix_time(); |
439 | _Bool v6 = 0, v4 = 0; | 476 | bool v6 = 0, v4 = 0; |
440 | 477 | ||
441 | if ((UDP_DIRECT_TIMEOUT + conn->direct_lastrecv_timev4) > current_time) { | 478 | if ((UDP_DIRECT_TIMEOUT + conn->direct_lastrecv_timev4) > current_time) { |
442 | v4 = 1; | 479 | v4 = 1; |
@@ -448,13 +485,17 @@ IP_Port return_ip_port_connection(Net_Crypto *c, int crypt_connection_id) | |||
448 | 485 | ||
449 | if (v4 && LAN_ip(conn->ip_portv4.ip) == 0) { | 486 | if (v4 && LAN_ip(conn->ip_portv4.ip) == 0) { |
450 | return conn->ip_portv4; | 487 | return conn->ip_portv4; |
451 | } else if (v6 && conn->ip_portv6.ip.family == AF_INET6) { | 488 | } |
489 | |||
490 | if (v6 && conn->ip_portv6.ip.family == AF_INET6) { | ||
452 | return conn->ip_portv6; | 491 | return conn->ip_portv6; |
453 | } else if (conn->ip_portv4.ip.family == AF_INET) { | 492 | } |
493 | |||
494 | if (conn->ip_portv4.ip.family == AF_INET) { | ||
454 | return conn->ip_portv4; | 495 | return conn->ip_portv4; |
455 | } else { | ||
456 | return empty; | ||
457 | } | 496 | } |
497 | |||
498 | return empty; | ||
458 | } | 499 | } |
459 | 500 | ||
460 | /* Sends a packet to the peer using the fastest route. | 501 | /* Sends a packet to the peer using the fastest route. |
@@ -464,33 +505,34 @@ IP_Port return_ip_port_connection(Net_Crypto *c, int crypt_connection_id) | |||
464 | */ | 505 | */ |
465 | static int send_packet_to(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length) | 506 | static int send_packet_to(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length) |
466 | { | 507 | { |
467 | //TODO TCP, etc... | 508 | // TODO(irungentoo): TCP, etc... |
468 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 509 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
469 | 510 | ||
470 | if (conn == 0) | 511 | if (conn == 0) { |
471 | return -1; | 512 | return -1; |
513 | } | ||
472 | 514 | ||
473 | int direct_send_attempt = 0; | 515 | int direct_send_attempt = 0; |
474 | 516 | ||
475 | pthread_mutex_lock(&conn->mutex); | 517 | pthread_mutex_lock(&conn->mutex); |
476 | IP_Port ip_port = return_ip_port_connection(c, crypt_connection_id); | 518 | IP_Port ip_port = return_ip_port_connection(c, crypt_connection_id); |
477 | 519 | ||
478 | //TODO: on bad networks, direct connections might not last indefinitely. | 520 | // TODO(irungentoo): on bad networks, direct connections might not last indefinitely. |
479 | if (ip_port.ip.family != 0) { | 521 | if (ip_port.ip.family != 0) { |
480 | _Bool direct_connected = 0; | 522 | bool direct_connected = 0; |
481 | crypto_connection_status(c, crypt_connection_id, &direct_connected, NULL); | 523 | crypto_connection_status(c, crypt_connection_id, &direct_connected, NULL); |
482 | 524 | ||
483 | if (direct_connected) { | 525 | if (direct_connected) { |
484 | if ((uint32_t)sendpacket(c->dht->net, ip_port, data, length) == length) { | 526 | if ((uint32_t)sendpacket(c->dht->net, ip_port, data, length) == length) { |
485 | pthread_mutex_unlock(&conn->mutex); | 527 | pthread_mutex_unlock(&conn->mutex); |
486 | return 0; | 528 | return 0; |
487 | } else { | ||
488 | pthread_mutex_unlock(&conn->mutex); | ||
489 | return -1; | ||
490 | } | 529 | } |
530 | |||
531 | pthread_mutex_unlock(&conn->mutex); | ||
532 | return -1; | ||
491 | } | 533 | } |
492 | 534 | ||
493 | //TODO: a better way of sending packets directly to confirm the others ip. | 535 | // TODO(irungentoo): a better way of sending packets directly to confirm the others ip. |
494 | uint64_t current_time = unix_time(); | 536 | uint64_t current_time = unix_time(); |
495 | 537 | ||
496 | if ((((UDP_DIRECT_TIMEOUT / 2) + conn->direct_send_attempt_time) > current_time && length < 96) | 538 | if ((((UDP_DIRECT_TIMEOUT / 2) + conn->direct_send_attempt_time) > current_time && length < 96) |
@@ -540,24 +582,28 @@ static uint32_t num_packets_array(const Packets_Array *array) | |||
540 | */ | 582 | */ |
541 | static int add_data_to_buffer(Packets_Array *array, uint32_t number, const Packet_Data *data) | 583 | static int add_data_to_buffer(Packets_Array *array, uint32_t number, const Packet_Data *data) |
542 | { | 584 | { |
543 | if (number - array->buffer_start > CRYPTO_PACKET_BUFFER_SIZE) | 585 | if (number - array->buffer_start > CRYPTO_PACKET_BUFFER_SIZE) { |
544 | return -1; | 586 | return -1; |
587 | } | ||
545 | 588 | ||
546 | uint32_t num = number % CRYPTO_PACKET_BUFFER_SIZE; | 589 | uint32_t num = number % CRYPTO_PACKET_BUFFER_SIZE; |
547 | 590 | ||
548 | if (array->buffer[num]) | 591 | if (array->buffer[num]) { |
549 | return -1; | 592 | return -1; |
593 | } | ||
550 | 594 | ||
551 | Packet_Data *new_d = malloc(sizeof(Packet_Data)); | 595 | Packet_Data *new_d = (Packet_Data *)malloc(sizeof(Packet_Data)); |
552 | 596 | ||
553 | if (new_d == NULL) | 597 | if (new_d == NULL) { |
554 | return -1; | 598 | return -1; |
599 | } | ||
555 | 600 | ||
556 | memcpy(new_d, data, sizeof(Packet_Data)); | 601 | memcpy(new_d, data, sizeof(Packet_Data)); |
557 | array->buffer[num] = new_d; | 602 | array->buffer[num] = new_d; |
558 | 603 | ||
559 | if ((number - array->buffer_start) >= (array->buffer_end - array->buffer_start)) | 604 | if ((number - array->buffer_start) >= (array->buffer_end - array->buffer_start)) { |
560 | array->buffer_end = number + 1; | 605 | array->buffer_end = number + 1; |
606 | } | ||
561 | 607 | ||
562 | return 0; | 608 | return 0; |
563 | } | 609 | } |
@@ -572,13 +618,15 @@ static int get_data_pointer(const Packets_Array *array, Packet_Data **data, uint | |||
572 | { | 618 | { |
573 | uint32_t num_spots = array->buffer_end - array->buffer_start; | 619 | uint32_t num_spots = array->buffer_end - array->buffer_start; |
574 | 620 | ||
575 | if (array->buffer_end - number > num_spots || number - array->buffer_start >= num_spots) | 621 | if (array->buffer_end - number > num_spots || number - array->buffer_start >= num_spots) { |
576 | return -1; | 622 | return -1; |
623 | } | ||
577 | 624 | ||
578 | uint32_t num = number % CRYPTO_PACKET_BUFFER_SIZE; | 625 | uint32_t num = number % CRYPTO_PACKET_BUFFER_SIZE; |
579 | 626 | ||
580 | if (!array->buffer[num]) | 627 | if (!array->buffer[num]) { |
581 | return 0; | 628 | return 0; |
629 | } | ||
582 | 630 | ||
583 | *data = array->buffer[num]; | 631 | *data = array->buffer[num]; |
584 | return 1; | 632 | return 1; |
@@ -591,13 +639,15 @@ static int get_data_pointer(const Packets_Array *array, Packet_Data **data, uint | |||
591 | */ | 639 | */ |
592 | static int64_t add_data_end_of_buffer(Packets_Array *array, const Packet_Data *data) | 640 | static int64_t add_data_end_of_buffer(Packets_Array *array, const Packet_Data *data) |
593 | { | 641 | { |
594 | if (num_packets_array(array) >= CRYPTO_PACKET_BUFFER_SIZE) | 642 | if (num_packets_array(array) >= CRYPTO_PACKET_BUFFER_SIZE) { |
595 | return -1; | 643 | return -1; |
644 | } | ||
596 | 645 | ||
597 | Packet_Data *new_d = malloc(sizeof(Packet_Data)); | 646 | Packet_Data *new_d = (Packet_Data *)malloc(sizeof(Packet_Data)); |
598 | 647 | ||
599 | if (new_d == NULL) | 648 | if (new_d == NULL) { |
600 | return -1; | 649 | return -1; |
650 | } | ||
601 | 651 | ||
602 | memcpy(new_d, data, sizeof(Packet_Data)); | 652 | memcpy(new_d, data, sizeof(Packet_Data)); |
603 | uint32_t id = array->buffer_end; | 653 | uint32_t id = array->buffer_end; |
@@ -613,13 +663,15 @@ static int64_t add_data_end_of_buffer(Packets_Array *array, const Packet_Data *d | |||
613 | */ | 663 | */ |
614 | static int64_t read_data_beg_buffer(Packets_Array *array, Packet_Data *data) | 664 | static int64_t read_data_beg_buffer(Packets_Array *array, Packet_Data *data) |
615 | { | 665 | { |
616 | if (array->buffer_end == array->buffer_start) | 666 | if (array->buffer_end == array->buffer_start) { |
617 | return -1; | 667 | return -1; |
668 | } | ||
618 | 669 | ||
619 | uint32_t num = array->buffer_start % CRYPTO_PACKET_BUFFER_SIZE; | 670 | uint32_t num = array->buffer_start % CRYPTO_PACKET_BUFFER_SIZE; |
620 | 671 | ||
621 | if (!array->buffer[num]) | 672 | if (!array->buffer[num]) { |
622 | return -1; | 673 | return -1; |
674 | } | ||
623 | 675 | ||
624 | memcpy(data, array->buffer[num], sizeof(Packet_Data)); | 676 | memcpy(data, array->buffer[num], sizeof(Packet_Data)); |
625 | uint32_t id = array->buffer_start; | 677 | uint32_t id = array->buffer_start; |
@@ -638,8 +690,9 @@ static int clear_buffer_until(Packets_Array *array, uint32_t number) | |||
638 | { | 690 | { |
639 | uint32_t num_spots = array->buffer_end - array->buffer_start; | 691 | uint32_t num_spots = array->buffer_end - array->buffer_start; |
640 | 692 | ||
641 | if (array->buffer_end - number >= num_spots || number - array->buffer_start > num_spots) | 693 | if (array->buffer_end - number >= num_spots || number - array->buffer_start > num_spots) { |
642 | return -1; | 694 | return -1; |
695 | } | ||
643 | 696 | ||
644 | uint32_t i; | 697 | uint32_t i; |
645 | 698 | ||
@@ -680,11 +733,13 @@ static int clear_buffer(Packets_Array *array) | |||
680 | */ | 733 | */ |
681 | static int set_buffer_end(Packets_Array *array, uint32_t number) | 734 | static int set_buffer_end(Packets_Array *array, uint32_t number) |
682 | { | 735 | { |
683 | if ((number - array->buffer_start) > CRYPTO_PACKET_BUFFER_SIZE) | 736 | if ((number - array->buffer_start) > CRYPTO_PACKET_BUFFER_SIZE) { |
684 | return -1; | 737 | return -1; |
738 | } | ||
685 | 739 | ||
686 | if ((number - array->buffer_end) > CRYPTO_PACKET_BUFFER_SIZE) | 740 | if ((number - array->buffer_end) > CRYPTO_PACKET_BUFFER_SIZE) { |
687 | return -1; | 741 | return -1; |
742 | } | ||
688 | 743 | ||
689 | array->buffer_end = number; | 744 | array->buffer_end = number; |
690 | return 0; | 745 | return 0; |
@@ -698,18 +753,21 @@ static int set_buffer_end(Packets_Array *array, uint32_t number) | |||
698 | */ | 753 | */ |
699 | static int generate_request_packet(uint8_t *data, uint16_t length, const Packets_Array *recv_array) | 754 | static int generate_request_packet(uint8_t *data, uint16_t length, const Packets_Array *recv_array) |
700 | { | 755 | { |
701 | if (length == 0) | 756 | if (length == 0) { |
702 | return -1; | 757 | return -1; |
758 | } | ||
703 | 759 | ||
704 | data[0] = PACKET_ID_REQUEST; | 760 | data[0] = PACKET_ID_REQUEST; |
705 | 761 | ||
706 | uint16_t cur_len = 1; | 762 | uint16_t cur_len = 1; |
707 | 763 | ||
708 | if (recv_array->buffer_start == recv_array->buffer_end) | 764 | if (recv_array->buffer_start == recv_array->buffer_end) { |
709 | return cur_len; | 765 | return cur_len; |
766 | } | ||
710 | 767 | ||
711 | if (length <= cur_len) | 768 | if (length <= cur_len) { |
712 | return cur_len; | 769 | return cur_len; |
770 | } | ||
713 | 771 | ||
714 | uint32_t i, n = 1; | 772 | uint32_t i, n = 1; |
715 | 773 | ||
@@ -721,16 +779,17 @@ static int generate_request_packet(uint8_t *data, uint16_t length, const Packets | |||
721 | n = 0; | 779 | n = 0; |
722 | ++cur_len; | 780 | ++cur_len; |
723 | 781 | ||
724 | if (length <= cur_len) | 782 | if (length <= cur_len) { |
725 | return cur_len; | 783 | return cur_len; |
726 | 784 | } | |
727 | } else if (n == 255) { | 785 | } else if (n == 255) { |
728 | data[cur_len] = 0; | 786 | data[cur_len] = 0; |
729 | n = 0; | 787 | n = 0; |
730 | ++cur_len; | 788 | ++cur_len; |
731 | 789 | ||
732 | if (length <= cur_len) | 790 | if (length <= cur_len) { |
733 | return cur_len; | 791 | return cur_len; |
792 | } | ||
734 | } | 793 | } |
735 | 794 | ||
736 | ++n; | 795 | ++n; |
@@ -748,14 +807,17 @@ static int generate_request_packet(uint8_t *data, uint16_t length, const Packets | |||
748 | static int handle_request_packet(Packets_Array *send_array, const uint8_t *data, uint16_t length, | 807 | static int handle_request_packet(Packets_Array *send_array, const uint8_t *data, uint16_t length, |
749 | uint64_t *latest_send_time, uint64_t rtt_time) | 808 | uint64_t *latest_send_time, uint64_t rtt_time) |
750 | { | 809 | { |
751 | if (length < 1) | 810 | if (length < 1) { |
752 | return -1; | 811 | return -1; |
812 | } | ||
753 | 813 | ||
754 | if (data[0] != PACKET_ID_REQUEST) | 814 | if (data[0] != PACKET_ID_REQUEST) { |
755 | return -1; | 815 | return -1; |
816 | } | ||
756 | 817 | ||
757 | if (length == 1) | 818 | if (length == 1) { |
758 | return 0; | 819 | return 0; |
820 | } | ||
759 | 821 | ||
760 | ++data; | 822 | ++data; |
761 | --length; | 823 | --length; |
@@ -767,8 +829,9 @@ static int handle_request_packet(Packets_Array *send_array, const uint8_t *data, | |||
767 | uint64_t l_sent_time = ~0; | 829 | uint64_t l_sent_time = ~0; |
768 | 830 | ||
769 | for (i = send_array->buffer_start; i != send_array->buffer_end; ++i) { | 831 | for (i = send_array->buffer_start; i != send_array->buffer_end; ++i) { |
770 | if (length == 0) | 832 | if (length == 0) { |
771 | break; | 833 | break; |
834 | } | ||
772 | 835 | ||
773 | uint32_t num = i % CRYPTO_PACKET_BUFFER_SIZE; | 836 | uint32_t num = i % CRYPTO_PACKET_BUFFER_SIZE; |
774 | 837 | ||
@@ -789,8 +852,9 @@ static int handle_request_packet(Packets_Array *send_array, const uint8_t *data, | |||
789 | if (send_array->buffer[num]) { | 852 | if (send_array->buffer[num]) { |
790 | uint64_t sent_time = send_array->buffer[num]->sent_time; | 853 | uint64_t sent_time = send_array->buffer[num]->sent_time; |
791 | 854 | ||
792 | if (l_sent_time < sent_time) | 855 | if (l_sent_time < sent_time) { |
793 | l_sent_time = sent_time; | 856 | l_sent_time = sent_time; |
857 | } | ||
794 | 858 | ||
795 | free(send_array->buffer[num]); | 859 | free(send_array->buffer[num]); |
796 | send_array->buffer[num] = NULL; | 860 | send_array->buffer[num] = NULL; |
@@ -800,8 +864,9 @@ static int handle_request_packet(Packets_Array *send_array, const uint8_t *data, | |||
800 | if (n == 255) { | 864 | if (n == 255) { |
801 | n = 1; | 865 | n = 1; |
802 | 866 | ||
803 | if (data[0] != 0) | 867 | if (data[0] != 0) { |
804 | return -1; | 868 | return -1; |
869 | } | ||
805 | 870 | ||
806 | ++data; | 871 | ++data; |
807 | --length; | 872 | --length; |
@@ -810,8 +875,9 @@ static int handle_request_packet(Packets_Array *send_array, const uint8_t *data, | |||
810 | } | 875 | } |
811 | } | 876 | } |
812 | 877 | ||
813 | if (*latest_send_time < l_sent_time) | 878 | if (*latest_send_time < l_sent_time) { |
814 | *latest_send_time = l_sent_time; | 879 | *latest_send_time = l_sent_time; |
880 | } | ||
815 | 881 | ||
816 | return requested; | 882 | return requested; |
817 | } | 883 | } |
@@ -827,13 +893,15 @@ static int handle_request_packet(Packets_Array *send_array, const uint8_t *data, | |||
827 | */ | 893 | */ |
828 | static int send_data_packet(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length) | 894 | static int send_data_packet(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length) |
829 | { | 895 | { |
830 | if (length == 0 || length + (1 + sizeof(uint16_t) + crypto_box_MACBYTES) > MAX_CRYPTO_PACKET_SIZE) | 896 | if (length == 0 || length + (1 + sizeof(uint16_t) + crypto_box_MACBYTES) > MAX_CRYPTO_PACKET_SIZE) { |
831 | return -1; | 897 | return -1; |
898 | } | ||
832 | 899 | ||
833 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 900 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
834 | 901 | ||
835 | if (conn == 0) | 902 | if (conn == 0) { |
836 | return -1; | 903 | return -1; |
904 | } | ||
837 | 905 | ||
838 | pthread_mutex_lock(&conn->mutex); | 906 | pthread_mutex_lock(&conn->mutex); |
839 | uint8_t packet[1 + sizeof(uint16_t) + length + crypto_box_MACBYTES]; | 907 | uint8_t packet[1 + sizeof(uint16_t) + length + crypto_box_MACBYTES]; |
@@ -860,8 +928,9 @@ static int send_data_packet(Net_Crypto *c, int crypt_connection_id, const uint8_ | |||
860 | static int send_data_packet_helper(Net_Crypto *c, int crypt_connection_id, uint32_t buffer_start, uint32_t num, | 928 | static int send_data_packet_helper(Net_Crypto *c, int crypt_connection_id, uint32_t buffer_start, uint32_t num, |
861 | const uint8_t *data, uint16_t length) | 929 | const uint8_t *data, uint16_t length) |
862 | { | 930 | { |
863 | if (length == 0 || length > MAX_CRYPTO_DATA_SIZE) | 931 | if (length == 0 || length > MAX_CRYPTO_DATA_SIZE) { |
864 | return -1; | 932 | return -1; |
933 | } | ||
865 | 934 | ||
866 | num = htonl(num); | 935 | num = htonl(num); |
867 | buffer_start = htonl(buffer_start); | 936 | buffer_start = htonl(buffer_start); |
@@ -879,8 +948,9 @@ static int reset_max_speed_reached(Net_Crypto *c, int crypt_connection_id) | |||
879 | { | 948 | { |
880 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 949 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
881 | 950 | ||
882 | if (conn == 0) | 951 | if (conn == 0) { |
883 | return -1; | 952 | return -1; |
953 | } | ||
884 | 954 | ||
885 | /* If last packet send failed, try to send packet again. | 955 | /* If last packet send failed, try to send packet again. |
886 | If sending it fails we won't be able to send the new packet. */ | 956 | If sending it fails we won't be able to send the new packet. */ |
@@ -918,13 +988,15 @@ static int reset_max_speed_reached(Net_Crypto *c, int crypt_connection_id) | |||
918 | static int64_t send_lossless_packet(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length, | 988 | static int64_t send_lossless_packet(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length, |
919 | uint8_t congestion_control) | 989 | uint8_t congestion_control) |
920 | { | 990 | { |
921 | if (length == 0 || length > MAX_CRYPTO_DATA_SIZE) | 991 | if (length == 0 || length > MAX_CRYPTO_DATA_SIZE) { |
922 | return -1; | 992 | return -1; |
993 | } | ||
923 | 994 | ||
924 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 995 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
925 | 996 | ||
926 | if (conn == 0) | 997 | if (conn == 0) { |
927 | return -1; | 998 | return -1; |
999 | } | ||
928 | 1000 | ||
929 | /* If last packet send failed, try to send packet again. | 1001 | /* If last packet send failed, try to send packet again. |
930 | If sending it fails we won't be able to send the new packet. */ | 1002 | If sending it fails we won't be able to send the new packet. */ |
@@ -942,8 +1014,9 @@ static int64_t send_lossless_packet(Net_Crypto *c, int crypt_connection_id, cons | |||
942 | int64_t packet_num = add_data_end_of_buffer(&conn->send_array, &dt); | 1014 | int64_t packet_num = add_data_end_of_buffer(&conn->send_array, &dt); |
943 | pthread_mutex_unlock(&conn->mutex); | 1015 | pthread_mutex_unlock(&conn->mutex); |
944 | 1016 | ||
945 | if (packet_num == -1) | 1017 | if (packet_num == -1) { |
946 | return -1; | 1018 | return -1; |
1019 | } | ||
947 | 1020 | ||
948 | if (!congestion_control && conn->maximum_speed_reached) { | 1021 | if (!congestion_control && conn->maximum_speed_reached) { |
949 | return packet_num; | 1022 | return packet_num; |
@@ -952,11 +1025,12 @@ static int64_t send_lossless_packet(Net_Crypto *c, int crypt_connection_id, cons | |||
952 | if (send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, packet_num, data, length) == 0) { | 1025 | if (send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, packet_num, data, length) == 0) { |
953 | Packet_Data *dt1 = NULL; | 1026 | Packet_Data *dt1 = NULL; |
954 | 1027 | ||
955 | if (get_data_pointer(&conn->send_array, &dt1, packet_num) == 1) | 1028 | if (get_data_pointer(&conn->send_array, &dt1, packet_num) == 1) { |
956 | dt1->sent_time = current_time_monotonic(); | 1029 | dt1->sent_time = current_time_monotonic(); |
1030 | } | ||
957 | } else { | 1031 | } else { |
958 | conn->maximum_speed_reached = 1; | 1032 | conn->maximum_speed_reached = 1; |
959 | LOGGER_ERROR("send_data_packet failed\n"); | 1033 | LOGGER_ERROR(c->log, "send_data_packet failed\n"); |
960 | } | 1034 | } |
961 | 1035 | ||
962 | return packet_num; | 1036 | return packet_num; |
@@ -984,13 +1058,15 @@ static uint16_t get_nonce_uint16(const uint8_t *nonce) | |||
984 | static int handle_data_packet(const Net_Crypto *c, int crypt_connection_id, uint8_t *data, const uint8_t *packet, | 1058 | static int handle_data_packet(const Net_Crypto *c, int crypt_connection_id, uint8_t *data, const uint8_t *packet, |
985 | uint16_t length) | 1059 | uint16_t length) |
986 | { | 1060 | { |
987 | if (length <= (1 + sizeof(uint16_t) + crypto_box_MACBYTES) || length > MAX_CRYPTO_PACKET_SIZE) | 1061 | if (length <= (1 + sizeof(uint16_t) + crypto_box_MACBYTES) || length > MAX_CRYPTO_PACKET_SIZE) { |
988 | return -1; | 1062 | return -1; |
1063 | } | ||
989 | 1064 | ||
990 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 1065 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
991 | 1066 | ||
992 | if (conn == 0) | 1067 | if (conn == 0) { |
993 | return -1; | 1068 | return -1; |
1069 | } | ||
994 | 1070 | ||
995 | uint8_t nonce[crypto_box_NONCEBYTES]; | 1071 | uint8_t nonce[crypto_box_NONCEBYTES]; |
996 | memcpy(nonce, conn->recv_nonce, crypto_box_NONCEBYTES); | 1072 | memcpy(nonce, conn->recv_nonce, crypto_box_NONCEBYTES); |
@@ -1003,8 +1079,9 @@ static int handle_data_packet(const Net_Crypto *c, int crypt_connection_id, uint | |||
1003 | int len = decrypt_data_symmetric(conn->shared_key, nonce, packet + 1 + sizeof(uint16_t), | 1079 | int len = decrypt_data_symmetric(conn->shared_key, nonce, packet + 1 + sizeof(uint16_t), |
1004 | length - (1 + sizeof(uint16_t)), data); | 1080 | length - (1 + sizeof(uint16_t)), data); |
1005 | 1081 | ||
1006 | if ((unsigned int)len != length - (1 + sizeof(uint16_t) + crypto_box_MACBYTES)) | 1082 | if ((unsigned int)len != length - (1 + sizeof(uint16_t) + crypto_box_MACBYTES)) { |
1007 | return -1; | 1083 | return -1; |
1084 | } | ||
1008 | 1085 | ||
1009 | if (diff > DATA_NUM_THRESHOLD * 2) { | 1086 | if (diff > DATA_NUM_THRESHOLD * 2) { |
1010 | increment_nonce_number(conn->recv_nonce, DATA_NUM_THRESHOLD); | 1087 | increment_nonce_number(conn->recv_nonce, DATA_NUM_THRESHOLD); |
@@ -1022,14 +1099,16 @@ static int send_request_packet(Net_Crypto *c, int crypt_connection_id) | |||
1022 | { | 1099 | { |
1023 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 1100 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
1024 | 1101 | ||
1025 | if (conn == 0) | 1102 | if (conn == 0) { |
1026 | return -1; | 1103 | return -1; |
1104 | } | ||
1027 | 1105 | ||
1028 | uint8_t data[MAX_CRYPTO_DATA_SIZE]; | 1106 | uint8_t data[MAX_CRYPTO_DATA_SIZE]; |
1029 | int len = generate_request_packet(data, sizeof(data), &conn->recv_array); | 1107 | int len = generate_request_packet(data, sizeof(data), &conn->recv_array); |
1030 | 1108 | ||
1031 | if (len == -1) | 1109 | if (len == -1) { |
1032 | return -1; | 1110 | return -1; |
1111 | } | ||
1033 | 1112 | ||
1034 | return send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, conn->send_array.buffer_end, data, | 1113 | return send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, conn->send_array.buffer_end, data, |
1035 | len); | 1114 | len); |
@@ -1042,13 +1121,15 @@ static int send_request_packet(Net_Crypto *c, int crypt_connection_id) | |||
1042 | */ | 1121 | */ |
1043 | static int send_requested_packets(Net_Crypto *c, int crypt_connection_id, uint32_t max_num) | 1122 | static int send_requested_packets(Net_Crypto *c, int crypt_connection_id, uint32_t max_num) |
1044 | { | 1123 | { |
1045 | if (max_num == 0) | 1124 | if (max_num == 0) { |
1046 | return -1; | 1125 | return -1; |
1126 | } | ||
1047 | 1127 | ||
1048 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 1128 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
1049 | 1129 | ||
1050 | if (conn == 0) | 1130 | if (conn == 0) { |
1051 | return -1; | 1131 | return -1; |
1132 | } | ||
1052 | 1133 | ||
1053 | uint64_t temp_time = current_time_monotonic(); | 1134 | uint64_t temp_time = current_time_monotonic(); |
1054 | uint32_t i, num_sent = 0, array_size = num_packets_array(&conn->send_array); | 1135 | uint32_t i, num_sent = 0, array_size = num_packets_array(&conn->send_array); |
@@ -1060,7 +1141,9 @@ static int send_requested_packets(Net_Crypto *c, int crypt_connection_id, uint32 | |||
1060 | 1141 | ||
1061 | if (ret == -1) { | 1142 | if (ret == -1) { |
1062 | return -1; | 1143 | return -1; |
1063 | } else if (ret == 0) { | 1144 | } |
1145 | |||
1146 | if (ret == 0) { | ||
1064 | continue; | 1147 | continue; |
1065 | } | 1148 | } |
1066 | 1149 | ||
@@ -1074,8 +1157,9 @@ static int send_requested_packets(Net_Crypto *c, int crypt_connection_id, uint32 | |||
1074 | ++num_sent; | 1157 | ++num_sent; |
1075 | } | 1158 | } |
1076 | 1159 | ||
1077 | if (num_sent >= max_num) | 1160 | if (num_sent >= max_num) { |
1078 | break; | 1161 | break; |
1162 | } | ||
1079 | } | 1163 | } |
1080 | 1164 | ||
1081 | return num_sent; | 1165 | return num_sent; |
@@ -1089,21 +1173,25 @@ static int send_requested_packets(Net_Crypto *c, int crypt_connection_id, uint32 | |||
1089 | */ | 1173 | */ |
1090 | static int new_temp_packet(const Net_Crypto *c, int crypt_connection_id, const uint8_t *packet, uint16_t length) | 1174 | static int new_temp_packet(const Net_Crypto *c, int crypt_connection_id, const uint8_t *packet, uint16_t length) |
1091 | { | 1175 | { |
1092 | if (length == 0 || length > MAX_CRYPTO_PACKET_SIZE) | 1176 | if (length == 0 || length > MAX_CRYPTO_PACKET_SIZE) { |
1093 | return -1; | 1177 | return -1; |
1178 | } | ||
1094 | 1179 | ||
1095 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 1180 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
1096 | 1181 | ||
1097 | if (conn == 0) | 1182 | if (conn == 0) { |
1098 | return -1; | 1183 | return -1; |
1184 | } | ||
1099 | 1185 | ||
1100 | uint8_t *temp_packet = malloc(length); | 1186 | uint8_t *temp_packet = (uint8_t *)malloc(length); |
1101 | 1187 | ||
1102 | if (temp_packet == 0) | 1188 | if (temp_packet == 0) { |
1103 | return -1; | 1189 | return -1; |
1190 | } | ||
1104 | 1191 | ||
1105 | if (conn->temp_packet) | 1192 | if (conn->temp_packet) { |
1106 | free(conn->temp_packet); | 1193 | free(conn->temp_packet); |
1194 | } | ||
1107 | 1195 | ||
1108 | memcpy(temp_packet, packet, length); | 1196 | memcpy(temp_packet, packet, length); |
1109 | conn->temp_packet = temp_packet; | 1197 | conn->temp_packet = temp_packet; |
@@ -1122,11 +1210,13 @@ static int clear_temp_packet(const Net_Crypto *c, int crypt_connection_id) | |||
1122 | { | 1210 | { |
1123 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 1211 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
1124 | 1212 | ||
1125 | if (conn == 0) | 1213 | if (conn == 0) { |
1126 | return -1; | 1214 | return -1; |
1215 | } | ||
1127 | 1216 | ||
1128 | if (conn->temp_packet) | 1217 | if (conn->temp_packet) { |
1129 | free(conn->temp_packet); | 1218 | free(conn->temp_packet); |
1219 | } | ||
1130 | 1220 | ||
1131 | conn->temp_packet = 0; | 1221 | conn->temp_packet = 0; |
1132 | conn->temp_packet_length = 0; | 1222 | conn->temp_packet_length = 0; |
@@ -1145,14 +1235,17 @@ static int send_temp_packet(Net_Crypto *c, int crypt_connection_id) | |||
1145 | { | 1235 | { |
1146 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 1236 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
1147 | 1237 | ||
1148 | if (conn == 0) | 1238 | if (conn == 0) { |
1149 | return -1; | 1239 | return -1; |
1240 | } | ||
1150 | 1241 | ||
1151 | if (!conn->temp_packet) | 1242 | if (!conn->temp_packet) { |
1152 | return -1; | 1243 | return -1; |
1244 | } | ||
1153 | 1245 | ||
1154 | if (send_packet_to(c, crypt_connection_id, conn->temp_packet, conn->temp_packet_length) != 0) | 1246 | if (send_packet_to(c, crypt_connection_id, conn->temp_packet, conn->temp_packet_length) != 0) { |
1155 | return -1; | 1247 | return -1; |
1248 | } | ||
1156 | 1249 | ||
1157 | conn->temp_packet_sent_time = current_time_monotonic(); | 1250 | conn->temp_packet_sent_time = current_time_monotonic(); |
1158 | ++conn->temp_packet_num_sent; | 1251 | ++conn->temp_packet_num_sent; |
@@ -1170,17 +1263,20 @@ static int create_send_handshake(Net_Crypto *c, int crypt_connection_id, const u | |||
1170 | { | 1263 | { |
1171 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 1264 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
1172 | 1265 | ||
1173 | if (conn == 0) | 1266 | if (conn == 0) { |
1174 | return -1; | 1267 | return -1; |
1268 | } | ||
1175 | 1269 | ||
1176 | uint8_t handshake_packet[HANDSHAKE_PACKET_LENGTH]; | 1270 | uint8_t handshake_packet[HANDSHAKE_PACKET_LENGTH]; |
1177 | 1271 | ||
1178 | if (create_crypto_handshake(c, handshake_packet, cookie, conn->sent_nonce, conn->sessionpublic_key, | 1272 | if (create_crypto_handshake(c, handshake_packet, cookie, conn->sent_nonce, conn->sessionpublic_key, |
1179 | conn->public_key, dht_public_key) != sizeof(handshake_packet)) | 1273 | conn->public_key, dht_public_key) != sizeof(handshake_packet)) { |
1180 | return -1; | 1274 | return -1; |
1275 | } | ||
1181 | 1276 | ||
1182 | if (new_temp_packet(c, crypt_connection_id, handshake_packet, sizeof(handshake_packet)) != 0) | 1277 | if (new_temp_packet(c, crypt_connection_id, handshake_packet, sizeof(handshake_packet)) != 0) { |
1183 | return -1; | 1278 | return -1; |
1279 | } | ||
1184 | 1280 | ||
1185 | send_temp_packet(c, crypt_connection_id); | 1281 | send_temp_packet(c, crypt_connection_id); |
1186 | return 0; | 1282 | return 0; |
@@ -1195,23 +1291,26 @@ static int send_kill_packet(Net_Crypto *c, int crypt_connection_id) | |||
1195 | { | 1291 | { |
1196 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 1292 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
1197 | 1293 | ||
1198 | if (conn == 0) | 1294 | if (conn == 0) { |
1199 | return -1; | 1295 | return -1; |
1296 | } | ||
1200 | 1297 | ||
1201 | uint8_t kill_packet = PACKET_ID_KILL; | 1298 | uint8_t kill_packet = PACKET_ID_KILL; |
1202 | return send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, conn->send_array.buffer_end, | 1299 | return send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, conn->send_array.buffer_end, |
1203 | &kill_packet, sizeof(kill_packet)); | 1300 | &kill_packet, sizeof(kill_packet)); |
1204 | } | 1301 | } |
1205 | 1302 | ||
1206 | static void connection_kill(Net_Crypto *c, int crypt_connection_id) | 1303 | static void connection_kill(Net_Crypto *c, int crypt_connection_id, void *userdata) |
1207 | { | 1304 | { |
1208 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 1305 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
1209 | 1306 | ||
1210 | if (conn == 0) | 1307 | if (conn == 0) { |
1211 | return; | 1308 | return; |
1309 | } | ||
1212 | 1310 | ||
1213 | if (conn->connection_status_callback) { | 1311 | if (conn->connection_status_callback) { |
1214 | conn->connection_status_callback(conn->connection_status_callback_object, conn->connection_status_callback_id, 0); | 1312 | conn->connection_status_callback(conn->connection_status_callback_object, conn->connection_status_callback_id, 0, |
1313 | userdata); | ||
1215 | } | 1314 | } |
1216 | 1315 | ||
1217 | crypto_kill(c, crypt_connection_id); | 1316 | crypto_kill(c, crypt_connection_id); |
@@ -1223,21 +1322,24 @@ static void connection_kill(Net_Crypto *c, int crypt_connection_id) | |||
1223 | * return 0 on success. | 1322 | * return 0 on success. |
1224 | */ | 1323 | */ |
1225 | static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, const uint8_t *packet, uint16_t length, | 1324 | static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, const uint8_t *packet, uint16_t length, |
1226 | _Bool udp) | 1325 | bool udp, void *userdata) |
1227 | { | 1326 | { |
1228 | if (length > MAX_CRYPTO_PACKET_SIZE || length <= CRYPTO_DATA_PACKET_MIN_SIZE) | 1327 | if (length > MAX_CRYPTO_PACKET_SIZE || length <= CRYPTO_DATA_PACKET_MIN_SIZE) { |
1229 | return -1; | 1328 | return -1; |
1329 | } | ||
1230 | 1330 | ||
1231 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 1331 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
1232 | 1332 | ||
1233 | if (conn == 0) | 1333 | if (conn == 0) { |
1234 | return -1; | 1334 | return -1; |
1335 | } | ||
1235 | 1336 | ||
1236 | uint8_t data[MAX_DATA_DATA_PACKET_SIZE]; | 1337 | uint8_t data[MAX_DATA_DATA_PACKET_SIZE]; |
1237 | int len = handle_data_packet(c, crypt_connection_id, data, packet, length); | 1338 | int len = handle_data_packet(c, crypt_connection_id, data, packet, length); |
1238 | 1339 | ||
1239 | if (len <= (int)(sizeof(uint32_t) * 2)) | 1340 | if (len <= (int)(sizeof(uint32_t) * 2)) { |
1240 | return -1; | 1341 | return -1; |
1342 | } | ||
1241 | 1343 | ||
1242 | uint32_t buffer_start, num; | 1344 | uint32_t buffer_start, num; |
1243 | memcpy(&buffer_start, data, sizeof(uint32_t)); | 1345 | memcpy(&buffer_start, data, sizeof(uint32_t)); |
@@ -1266,12 +1368,13 @@ static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, con | |||
1266 | ++real_data; | 1368 | ++real_data; |
1267 | --real_length; | 1369 | --real_length; |
1268 | 1370 | ||
1269 | if (real_length == 0) | 1371 | if (real_length == 0) { |
1270 | return -1; | 1372 | return -1; |
1373 | } | ||
1271 | } | 1374 | } |
1272 | 1375 | ||
1273 | if (real_data[0] == PACKET_ID_KILL) { | 1376 | if (real_data[0] == PACKET_ID_KILL) { |
1274 | connection_kill(c, crypt_connection_id); | 1377 | connection_kill(c, crypt_connection_id, userdata); |
1275 | return 0; | 1378 | return 0; |
1276 | } | 1379 | } |
1277 | 1380 | ||
@@ -1279,8 +1382,10 @@ static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, con | |||
1279 | clear_temp_packet(c, crypt_connection_id); | 1382 | clear_temp_packet(c, crypt_connection_id); |
1280 | conn->status = CRYPTO_CONN_ESTABLISHED; | 1383 | conn->status = CRYPTO_CONN_ESTABLISHED; |
1281 | 1384 | ||
1282 | if (conn->connection_status_callback) | 1385 | if (conn->connection_status_callback) { |
1283 | conn->connection_status_callback(conn->connection_status_callback_object, conn->connection_status_callback_id, 1); | 1386 | conn->connection_status_callback(conn->connection_status_callback_object, conn->connection_status_callback_id, 1, |
1387 | userdata); | ||
1388 | } | ||
1284 | } | 1389 | } |
1285 | 1390 | ||
1286 | if (real_data[0] == PACKET_ID_REQUEST) { | 1391 | if (real_data[0] == PACKET_ID_REQUEST) { |
@@ -1296,37 +1401,40 @@ static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, con | |||
1296 | 1401 | ||
1297 | if (requested == -1) { | 1402 | if (requested == -1) { |
1298 | return -1; | 1403 | return -1; |
1299 | } else { | ||
1300 | //TODO? | ||
1301 | } | 1404 | } |
1302 | 1405 | ||
1406 | // else { /* TODO(irungentoo): ? */ } | ||
1407 | |||
1303 | set_buffer_end(&conn->recv_array, num); | 1408 | set_buffer_end(&conn->recv_array, num); |
1304 | } else if (real_data[0] >= CRYPTO_RESERVED_PACKETS && real_data[0] < PACKET_ID_LOSSY_RANGE_START) { | 1409 | } else if (real_data[0] >= CRYPTO_RESERVED_PACKETS && real_data[0] < PACKET_ID_LOSSY_RANGE_START) { |
1305 | Packet_Data dt; | 1410 | Packet_Data dt; |
1306 | dt.length = real_length; | 1411 | dt.length = real_length; |
1307 | memcpy(dt.data, real_data, real_length); | 1412 | memcpy(dt.data, real_data, real_length); |
1308 | 1413 | ||
1309 | if (add_data_to_buffer(&conn->recv_array, num, &dt) != 0) | 1414 | if (add_data_to_buffer(&conn->recv_array, num, &dt) != 0) { |
1310 | return -1; | 1415 | return -1; |
1311 | 1416 | } | |
1312 | 1417 | ||
1313 | while (1) { | 1418 | while (1) { |
1314 | pthread_mutex_lock(&conn->mutex); | 1419 | pthread_mutex_lock(&conn->mutex); |
1315 | int ret = read_data_beg_buffer(&conn->recv_array, &dt); | 1420 | int ret = read_data_beg_buffer(&conn->recv_array, &dt); |
1316 | pthread_mutex_unlock(&conn->mutex); | 1421 | pthread_mutex_unlock(&conn->mutex); |
1317 | 1422 | ||
1318 | if (ret == -1) | 1423 | if (ret == -1) { |
1319 | break; | 1424 | break; |
1425 | } | ||
1320 | 1426 | ||
1321 | if (conn->connection_data_callback) | 1427 | if (conn->connection_data_callback) { |
1322 | conn->connection_data_callback(conn->connection_data_callback_object, conn->connection_data_callback_id, dt.data, | 1428 | conn->connection_data_callback(conn->connection_data_callback_object, conn->connection_data_callback_id, dt.data, |
1323 | dt.length); | 1429 | dt.length, userdata); |
1430 | } | ||
1324 | 1431 | ||
1325 | /* conn might get killed in callback. */ | 1432 | /* conn might get killed in callback. */ |
1326 | conn = get_crypto_connection(c, crypt_connection_id); | 1433 | conn = get_crypto_connection(c, crypt_connection_id); |
1327 | 1434 | ||
1328 | if (conn == 0) | 1435 | if (conn == 0) { |
1329 | return -1; | 1436 | return -1; |
1437 | } | ||
1330 | } | 1438 | } |
1331 | 1439 | ||
1332 | /* Packet counter. */ | 1440 | /* Packet counter. */ |
@@ -1336,10 +1444,10 @@ static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, con | |||
1336 | 1444 | ||
1337 | set_buffer_end(&conn->recv_array, num); | 1445 | set_buffer_end(&conn->recv_array, num); |
1338 | 1446 | ||
1339 | if (conn->connection_lossy_data_callback) | 1447 | if (conn->connection_lossy_data_callback) { |
1340 | conn->connection_lossy_data_callback(conn->connection_lossy_data_callback_object, | 1448 | conn->connection_lossy_data_callback(conn->connection_lossy_data_callback_object, |
1341 | conn->connection_lossy_data_callback_id, real_data, real_length); | 1449 | conn->connection_lossy_data_callback_id, real_data, real_length, userdata); |
1342 | 1450 | } | |
1343 | } else { | 1451 | } else { |
1344 | return -1; | 1452 | return -1; |
1345 | } | 1453 | } |
@@ -1347,8 +1455,9 @@ static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, con | |||
1347 | if (rtt_calc_time != 0) { | 1455 | if (rtt_calc_time != 0) { |
1348 | uint64_t rtt_time = current_time_monotonic() - rtt_calc_time; | 1456 | uint64_t rtt_time = current_time_monotonic() - rtt_calc_time; |
1349 | 1457 | ||
1350 | if (rtt_time < conn->rtt_time) | 1458 | if (rtt_time < conn->rtt_time) { |
1351 | conn->rtt_time = rtt_time; | 1459 | conn->rtt_time = rtt_time; |
1460 | } | ||
1352 | } | 1461 | } |
1353 | 1462 | ||
1354 | return 0; | 1463 | return 0; |
@@ -1360,32 +1469,38 @@ static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, con | |||
1360 | * return 0 on success. | 1469 | * return 0 on success. |
1361 | */ | 1470 | */ |
1362 | static int handle_packet_connection(Net_Crypto *c, int crypt_connection_id, const uint8_t *packet, uint16_t length, | 1471 | static int handle_packet_connection(Net_Crypto *c, int crypt_connection_id, const uint8_t *packet, uint16_t length, |
1363 | _Bool udp) | 1472 | bool udp, void *userdata) |
1364 | { | 1473 | { |
1365 | if (length == 0 || length > MAX_CRYPTO_PACKET_SIZE) | 1474 | if (length == 0 || length > MAX_CRYPTO_PACKET_SIZE) { |
1366 | return -1; | 1475 | return -1; |
1476 | } | ||
1367 | 1477 | ||
1368 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 1478 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
1369 | 1479 | ||
1370 | if (conn == 0) | 1480 | if (conn == 0) { |
1371 | return -1; | 1481 | return -1; |
1482 | } | ||
1372 | 1483 | ||
1373 | switch (packet[0]) { | 1484 | switch (packet[0]) { |
1374 | case NET_PACKET_COOKIE_RESPONSE: { | 1485 | case NET_PACKET_COOKIE_RESPONSE: { |
1375 | if (conn->status != CRYPTO_CONN_COOKIE_REQUESTING) | 1486 | if (conn->status != CRYPTO_CONN_COOKIE_REQUESTING) { |
1376 | return -1; | 1487 | return -1; |
1488 | } | ||
1377 | 1489 | ||
1378 | uint8_t cookie[COOKIE_LENGTH]; | 1490 | uint8_t cookie[COOKIE_LENGTH]; |
1379 | uint64_t number; | 1491 | uint64_t number; |
1380 | 1492 | ||
1381 | if (handle_cookie_response(cookie, &number, packet, length, conn->shared_key) != sizeof(cookie)) | 1493 | if (handle_cookie_response(cookie, &number, packet, length, conn->shared_key) != sizeof(cookie)) { |
1382 | return -1; | 1494 | return -1; |
1495 | } | ||
1383 | 1496 | ||
1384 | if (number != conn->cookie_request_number) | 1497 | if (number != conn->cookie_request_number) { |
1385 | return -1; | 1498 | return -1; |
1499 | } | ||
1386 | 1500 | ||
1387 | if (create_send_handshake(c, crypt_connection_id, cookie, conn->dht_public_key) != 0) | 1501 | if (create_send_handshake(c, crypt_connection_id, cookie, conn->dht_public_key) != 0) { |
1388 | return -1; | 1502 | return -1; |
1503 | } | ||
1389 | 1504 | ||
1390 | conn->status = CRYPTO_CONN_HANDSHAKE_SENT; | 1505 | conn->status = CRYPTO_CONN_HANDSHAKE_SENT; |
1391 | return 0; | 1506 | return 0; |
@@ -1399,23 +1514,25 @@ static int handle_packet_connection(Net_Crypto *c, int crypt_connection_id, cons | |||
1399 | uint8_t cookie[COOKIE_LENGTH]; | 1514 | uint8_t cookie[COOKIE_LENGTH]; |
1400 | 1515 | ||
1401 | if (handle_crypto_handshake(c, conn->recv_nonce, conn->peersessionpublic_key, peer_real_pk, dht_public_key, cookie, | 1516 | if (handle_crypto_handshake(c, conn->recv_nonce, conn->peersessionpublic_key, peer_real_pk, dht_public_key, cookie, |
1402 | packet, length, conn->public_key) != 0) | 1517 | packet, length, conn->public_key) != 0) { |
1403 | return -1; | 1518 | return -1; |
1519 | } | ||
1404 | 1520 | ||
1405 | if (public_key_cmp(dht_public_key, conn->dht_public_key) == 0) { | 1521 | if (public_key_cmp(dht_public_key, conn->dht_public_key) == 0) { |
1406 | encrypt_precompute(conn->peersessionpublic_key, conn->sessionsecret_key, conn->shared_key); | 1522 | encrypt_precompute(conn->peersessionpublic_key, conn->sessionsecret_key, conn->shared_key); |
1407 | 1523 | ||
1408 | if (conn->status == CRYPTO_CONN_COOKIE_REQUESTING) { | 1524 | if (conn->status == CRYPTO_CONN_COOKIE_REQUESTING) { |
1409 | if (create_send_handshake(c, crypt_connection_id, cookie, dht_public_key) != 0) | 1525 | if (create_send_handshake(c, crypt_connection_id, cookie, dht_public_key) != 0) { |
1410 | return -1; | 1526 | return -1; |
1527 | } | ||
1411 | } | 1528 | } |
1412 | 1529 | ||
1413 | conn->status = CRYPTO_CONN_NOT_CONFIRMED; | 1530 | conn->status = CRYPTO_CONN_NOT_CONFIRMED; |
1414 | } else { | 1531 | } else { |
1415 | if (conn->dht_pk_callback) | 1532 | if (conn->dht_pk_callback) { |
1416 | conn->dht_pk_callback(conn->dht_pk_callback_object, conn->dht_pk_callback_number, dht_public_key); | 1533 | conn->dht_pk_callback(conn->dht_pk_callback_object, conn->dht_pk_callback_number, dht_public_key, userdata); |
1534 | } | ||
1417 | } | 1535 | } |
1418 | |||
1419 | } else { | 1536 | } else { |
1420 | return -1; | 1537 | return -1; |
1421 | } | 1538 | } |
@@ -1425,12 +1542,10 @@ static int handle_packet_connection(Net_Crypto *c, int crypt_connection_id, cons | |||
1425 | 1542 | ||
1426 | case NET_PACKET_CRYPTO_DATA: { | 1543 | case NET_PACKET_CRYPTO_DATA: { |
1427 | if (conn->status == CRYPTO_CONN_NOT_CONFIRMED || conn->status == CRYPTO_CONN_ESTABLISHED) { | 1544 | if (conn->status == CRYPTO_CONN_NOT_CONFIRMED || conn->status == CRYPTO_CONN_ESTABLISHED) { |
1428 | return handle_data_packet_helper(c, crypt_connection_id, packet, length, udp); | 1545 | return handle_data_packet_helper(c, crypt_connection_id, packet, length, udp, userdata); |
1429 | } else { | ||
1430 | return -1; | ||
1431 | } | 1546 | } |
1432 | 1547 | ||
1433 | return 0; | 1548 | return -1; |
1434 | } | 1549 | } |
1435 | 1550 | ||
1436 | default: { | 1551 | default: { |
@@ -1454,10 +1569,12 @@ static int realloc_cryptoconnection(Net_Crypto *c, uint32_t num) | |||
1454 | return 0; | 1569 | return 0; |
1455 | } | 1570 | } |
1456 | 1571 | ||
1457 | Crypto_Connection *newcrypto_connections = realloc(c->crypto_connections, num * sizeof(Crypto_Connection)); | 1572 | Crypto_Connection *newcrypto_connections = (Crypto_Connection *)realloc(c->crypto_connections, |
1573 | num * sizeof(Crypto_Connection)); | ||
1458 | 1574 | ||
1459 | if (newcrypto_connections == NULL) | 1575 | if (newcrypto_connections == NULL) { |
1460 | return -1; | 1576 | return -1; |
1577 | } | ||
1461 | 1578 | ||
1462 | c->crypto_connections = newcrypto_connections; | 1579 | c->crypto_connections = newcrypto_connections; |
1463 | return 0; | 1580 | return 0; |
@@ -1474,11 +1591,12 @@ static int create_crypto_connection(Net_Crypto *c) | |||
1474 | uint32_t i; | 1591 | uint32_t i; |
1475 | 1592 | ||
1476 | for (i = 0; i < c->crypto_connections_length; ++i) { | 1593 | for (i = 0; i < c->crypto_connections_length; ++i) { |
1477 | if (c->crypto_connections[i].status == CRYPTO_CONN_NO_CONNECTION) | 1594 | if (c->crypto_connections[i].status == CRYPTO_CONN_NO_CONNECTION) { |
1478 | return i; | 1595 | return i; |
1596 | } | ||
1479 | } | 1597 | } |
1480 | 1598 | ||
1481 | while (1) { /* TODO: is this really the best way to do this? */ | 1599 | while (1) { /* TODO(irungentoo): is this really the best way to do this? */ |
1482 | pthread_mutex_lock(&c->connections_mutex); | 1600 | pthread_mutex_lock(&c->connections_mutex); |
1483 | 1601 | ||
1484 | if (!c->connection_use_counter) { | 1602 | if (!c->connection_use_counter) { |
@@ -1512,8 +1630,9 @@ static int create_crypto_connection(Net_Crypto *c) | |||
1512 | */ | 1630 | */ |
1513 | static int wipe_crypto_connection(Net_Crypto *c, int crypt_connection_id) | 1631 | static int wipe_crypto_connection(Net_Crypto *c, int crypt_connection_id) |
1514 | { | 1632 | { |
1515 | if (crypt_connection_id_not_valid(c, crypt_connection_id)) | 1633 | if (crypt_connection_id_not_valid(c, crypt_connection_id)) { |
1516 | return -1; | 1634 | return -1; |
1635 | } | ||
1517 | 1636 | ||
1518 | uint32_t i; | 1637 | uint32_t i; |
1519 | 1638 | ||
@@ -1548,9 +1667,11 @@ static int getcryptconnection_id(const Net_Crypto *c, const uint8_t *public_key) | |||
1548 | uint32_t i; | 1667 | uint32_t i; |
1549 | 1668 | ||
1550 | for (i = 0; i < c->crypto_connections_length; ++i) { | 1669 | for (i = 0; i < c->crypto_connections_length; ++i) { |
1551 | if (c->crypto_connections[i].status != CRYPTO_CONN_NO_CONNECTION) | 1670 | if (c->crypto_connections[i].status != CRYPTO_CONN_NO_CONNECTION) { |
1552 | if (public_key_cmp(public_key, c->crypto_connections[i].public_key) == 0) | 1671 | if (public_key_cmp(public_key, c->crypto_connections[i].public_key) == 0) { |
1553 | return i; | 1672 | return i; |
1673 | } | ||
1674 | } | ||
1554 | } | 1675 | } |
1555 | 1676 | ||
1556 | return -1; | 1677 | return -1; |
@@ -1567,12 +1688,14 @@ static int crypto_connection_add_source(Net_Crypto *c, int crypt_connection_id, | |||
1567 | { | 1688 | { |
1568 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 1689 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
1569 | 1690 | ||
1570 | if (conn == 0) | 1691 | if (conn == 0) { |
1571 | return -1; | 1692 | return -1; |
1693 | } | ||
1572 | 1694 | ||
1573 | if (source.ip.family == AF_INET || source.ip.family == AF_INET6) { | 1695 | if (source.ip.family == AF_INET || source.ip.family == AF_INET6) { |
1574 | if (add_ip_port_connection(c, crypt_connection_id, source) != 0) | 1696 | if (add_ip_port_connection(c, crypt_connection_id, source) != 0) { |
1575 | return -1; | 1697 | return -1; |
1698 | } | ||
1576 | 1699 | ||
1577 | if (source.ip.family == AF_INET) { | 1700 | if (source.ip.family == AF_INET) { |
1578 | conn->direct_lastrecv_timev4 = unix_time(); | 1701 | conn->direct_lastrecv_timev4 = unix_time(); |
@@ -1581,9 +1704,12 @@ static int crypto_connection_add_source(Net_Crypto *c, int crypt_connection_id, | |||
1581 | } | 1704 | } |
1582 | 1705 | ||
1583 | return 0; | 1706 | return 0; |
1584 | } else if (source.ip.family == TCP_FAMILY) { | 1707 | } |
1585 | if (add_tcp_number_relay_connection(c->tcp_c, conn->connection_number_tcp, source.ip.ip6.uint32[0]) == 0) | 1708 | |
1709 | if (source.ip.family == TCP_FAMILY) { | ||
1710 | if (add_tcp_number_relay_connection(c->tcp_c, conn->connection_number_tcp, source.ip.ip6.uint32[0]) == 0) { | ||
1586 | return 1; | 1711 | return 1; |
1712 | } | ||
1587 | } | 1713 | } |
1588 | 1714 | ||
1589 | return -1; | 1715 | return -1; |
@@ -1609,13 +1735,15 @@ void new_connection_handler(Net_Crypto *c, int (*new_connection_callback)(void * | |||
1609 | * return -1 on failure. | 1735 | * return -1 on failure. |
1610 | * return 0 on success. | 1736 | * return 0 on success. |
1611 | */ | 1737 | */ |
1612 | static int handle_new_connection_handshake(Net_Crypto *c, IP_Port source, const uint8_t *data, uint16_t length) | 1738 | static int handle_new_connection_handshake(Net_Crypto *c, IP_Port source, const uint8_t *data, uint16_t length, |
1739 | void *userdata) | ||
1613 | { | 1740 | { |
1614 | New_Connection n_c; | 1741 | New_Connection n_c; |
1615 | n_c.cookie = malloc(COOKIE_LENGTH); | 1742 | n_c.cookie = (uint8_t *)malloc(COOKIE_LENGTH); |
1616 | 1743 | ||
1617 | if (n_c.cookie == NULL) | 1744 | if (n_c.cookie == NULL) { |
1618 | return -1; | 1745 | return -1; |
1746 | } | ||
1619 | 1747 | ||
1620 | n_c.source = source; | 1748 | n_c.source = source; |
1621 | n_c.cookie_length = COOKIE_LENGTH; | 1749 | n_c.cookie_length = COOKIE_LENGTH; |
@@ -1632,7 +1760,7 @@ static int handle_new_connection_handshake(Net_Crypto *c, IP_Port source, const | |||
1632 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 1760 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
1633 | 1761 | ||
1634 | if (public_key_cmp(n_c.dht_public_key, conn->dht_public_key) != 0) { | 1762 | if (public_key_cmp(n_c.dht_public_key, conn->dht_public_key) != 0) { |
1635 | connection_kill(c, crypt_connection_id); | 1763 | connection_kill(c, crypt_connection_id, userdata); |
1636 | } else { | 1764 | } else { |
1637 | int ret = -1; | 1765 | int ret = -1; |
1638 | 1766 | ||
@@ -1666,25 +1794,29 @@ static int handle_new_connection_handshake(Net_Crypto *c, IP_Port source, const | |||
1666 | */ | 1794 | */ |
1667 | int accept_crypto_connection(Net_Crypto *c, New_Connection *n_c) | 1795 | int accept_crypto_connection(Net_Crypto *c, New_Connection *n_c) |
1668 | { | 1796 | { |
1669 | if (getcryptconnection_id(c, n_c->public_key) != -1) | 1797 | if (getcryptconnection_id(c, n_c->public_key) != -1) { |
1670 | return -1; | 1798 | return -1; |
1799 | } | ||
1671 | 1800 | ||
1672 | int crypt_connection_id = create_crypto_connection(c); | 1801 | int crypt_connection_id = create_crypto_connection(c); |
1673 | 1802 | ||
1674 | if (crypt_connection_id == -1) | 1803 | if (crypt_connection_id == -1) { |
1675 | return -1; | 1804 | return -1; |
1805 | } | ||
1676 | 1806 | ||
1677 | Crypto_Connection *conn = &c->crypto_connections[crypt_connection_id]; | 1807 | Crypto_Connection *conn = &c->crypto_connections[crypt_connection_id]; |
1678 | 1808 | ||
1679 | if (n_c->cookie_length != COOKIE_LENGTH) | 1809 | if (n_c->cookie_length != COOKIE_LENGTH) { |
1680 | return -1; | 1810 | return -1; |
1811 | } | ||
1681 | 1812 | ||
1682 | pthread_mutex_lock(&c->tcp_mutex); | 1813 | pthread_mutex_lock(&c->tcp_mutex); |
1683 | int connection_number_tcp = new_tcp_connection_to(c->tcp_c, n_c->dht_public_key, crypt_connection_id); | 1814 | int connection_number_tcp = new_tcp_connection_to(c->tcp_c, n_c->dht_public_key, crypt_connection_id); |
1684 | pthread_mutex_unlock(&c->tcp_mutex); | 1815 | pthread_mutex_unlock(&c->tcp_mutex); |
1685 | 1816 | ||
1686 | if (connection_number_tcp == -1) | 1817 | if (connection_number_tcp == -1) { |
1687 | return -1; | 1818 | return -1; |
1819 | } | ||
1688 | 1820 | ||
1689 | conn->connection_number_tcp = connection_number_tcp; | 1821 | conn->connection_number_tcp = connection_number_tcp; |
1690 | memcpy(conn->public_key, n_c->public_key, crypto_box_PUBLICKEYBYTES); | 1822 | memcpy(conn->public_key, n_c->public_key, crypto_box_PUBLICKEYBYTES); |
@@ -1722,25 +1854,29 @@ int new_crypto_connection(Net_Crypto *c, const uint8_t *real_public_key, const u | |||
1722 | { | 1854 | { |
1723 | int crypt_connection_id = getcryptconnection_id(c, real_public_key); | 1855 | int crypt_connection_id = getcryptconnection_id(c, real_public_key); |
1724 | 1856 | ||
1725 | if (crypt_connection_id != -1) | 1857 | if (crypt_connection_id != -1) { |
1726 | return crypt_connection_id; | 1858 | return crypt_connection_id; |
1859 | } | ||
1727 | 1860 | ||
1728 | crypt_connection_id = create_crypto_connection(c); | 1861 | crypt_connection_id = create_crypto_connection(c); |
1729 | 1862 | ||
1730 | if (crypt_connection_id == -1) | 1863 | if (crypt_connection_id == -1) { |
1731 | return -1; | 1864 | return -1; |
1865 | } | ||
1732 | 1866 | ||
1733 | Crypto_Connection *conn = &c->crypto_connections[crypt_connection_id]; | 1867 | Crypto_Connection *conn = &c->crypto_connections[crypt_connection_id]; |
1734 | 1868 | ||
1735 | if (conn == 0) | 1869 | if (conn == 0) { |
1736 | return -1; | 1870 | return -1; |
1871 | } | ||
1737 | 1872 | ||
1738 | pthread_mutex_lock(&c->tcp_mutex); | 1873 | pthread_mutex_lock(&c->tcp_mutex); |
1739 | int connection_number_tcp = new_tcp_connection_to(c->tcp_c, dht_public_key, crypt_connection_id); | 1874 | int connection_number_tcp = new_tcp_connection_to(c->tcp_c, dht_public_key, crypt_connection_id); |
1740 | pthread_mutex_unlock(&c->tcp_mutex); | 1875 | pthread_mutex_unlock(&c->tcp_mutex); |
1741 | 1876 | ||
1742 | if (connection_number_tcp == -1) | 1877 | if (connection_number_tcp == -1) { |
1743 | return -1; | 1878 | return -1; |
1879 | } | ||
1744 | 1880 | ||
1745 | conn->connection_number_tcp = connection_number_tcp; | 1881 | conn->connection_number_tcp = connection_number_tcp; |
1746 | memcpy(conn->public_key, real_public_key, crypto_box_PUBLICKEYBYTES); | 1882 | memcpy(conn->public_key, real_public_key, crypto_box_PUBLICKEYBYTES); |
@@ -1776,12 +1912,13 @@ int new_crypto_connection(Net_Crypto *c, const uint8_t *real_public_key, const u | |||
1776 | * return -1 on failure. | 1912 | * return -1 on failure. |
1777 | * return 0 on success. | 1913 | * return 0 on success. |
1778 | */ | 1914 | */ |
1779 | int set_direct_ip_port(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port, _Bool connected) | 1915 | int set_direct_ip_port(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port, bool connected) |
1780 | { | 1916 | { |
1781 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 1917 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
1782 | 1918 | ||
1783 | if (conn == 0) | 1919 | if (conn == 0) { |
1784 | return -1; | 1920 | return -1; |
1921 | } | ||
1785 | 1922 | ||
1786 | if (add_ip_port_connection(c, crypt_connection_id, ip_port) == 0) { | 1923 | if (add_ip_port_connection(c, crypt_connection_id, ip_port) == 0) { |
1787 | if (connected) { | 1924 | if (connected) { |
@@ -1805,56 +1942,63 @@ int set_direct_ip_port(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port, | |||
1805 | } | 1942 | } |
1806 | 1943 | ||
1807 | 1944 | ||
1808 | static int tcp_data_callback(void *object, int id, const uint8_t *data, uint16_t length) | 1945 | static int tcp_data_callback(void *object, int id, const uint8_t *data, uint16_t length, void *userdata) |
1809 | { | 1946 | { |
1810 | if (length == 0 || length > MAX_CRYPTO_PACKET_SIZE) | 1947 | if (length == 0 || length > MAX_CRYPTO_PACKET_SIZE) { |
1811 | return -1; | 1948 | return -1; |
1949 | } | ||
1812 | 1950 | ||
1813 | Net_Crypto *c = object; | 1951 | Net_Crypto *c = (Net_Crypto *)object; |
1814 | 1952 | ||
1815 | Crypto_Connection *conn = get_crypto_connection(c, id); | 1953 | Crypto_Connection *conn = get_crypto_connection(c, id); |
1816 | 1954 | ||
1817 | if (conn == 0) | 1955 | if (conn == 0) { |
1818 | return -1; | 1956 | return -1; |
1957 | } | ||
1819 | 1958 | ||
1820 | if (data[0] == NET_PACKET_COOKIE_REQUEST) { | 1959 | if (data[0] == NET_PACKET_COOKIE_REQUEST) { |
1821 | return tcp_handle_cookie_request(c, conn->connection_number_tcp, data, length); | 1960 | return tcp_handle_cookie_request(c, conn->connection_number_tcp, data, length); |
1822 | } | 1961 | } |
1823 | 1962 | ||
1824 | pthread_mutex_unlock(&c->tcp_mutex); | 1963 | pthread_mutex_unlock(&c->tcp_mutex); |
1825 | int ret = handle_packet_connection(c, id, data, length, 0); | 1964 | int ret = handle_packet_connection(c, id, data, length, 0, userdata); |
1826 | pthread_mutex_lock(&c->tcp_mutex); | 1965 | pthread_mutex_lock(&c->tcp_mutex); |
1827 | 1966 | ||
1828 | if (ret != 0) | 1967 | if (ret != 0) { |
1829 | return -1; | 1968 | return -1; |
1969 | } | ||
1830 | 1970 | ||
1831 | //TODO detect and kill bad TCP connections. | 1971 | // TODO(irungentoo): detect and kill bad TCP connections. |
1832 | return 0; | 1972 | return 0; |
1833 | } | 1973 | } |
1834 | 1974 | ||
1835 | static int tcp_oob_callback(void *object, const uint8_t *public_key, unsigned int tcp_connections_number, | 1975 | static int tcp_oob_callback(void *object, const uint8_t *public_key, unsigned int tcp_connections_number, |
1836 | const uint8_t *data, uint16_t length) | 1976 | const uint8_t *data, uint16_t length, void *userdata) |
1837 | { | 1977 | { |
1838 | if (length == 0 || length > MAX_CRYPTO_PACKET_SIZE) | 1978 | if (length == 0 || length > MAX_CRYPTO_PACKET_SIZE) { |
1839 | return -1; | 1979 | return -1; |
1980 | } | ||
1840 | 1981 | ||
1841 | Net_Crypto *c = object; | 1982 | Net_Crypto *c = (Net_Crypto *)object; |
1842 | 1983 | ||
1843 | if (data[0] == NET_PACKET_COOKIE_REQUEST) { | 1984 | if (data[0] == NET_PACKET_COOKIE_REQUEST) { |
1844 | return tcp_oob_handle_cookie_request(c, tcp_connections_number, public_key, data, length); | 1985 | return tcp_oob_handle_cookie_request(c, tcp_connections_number, public_key, data, length); |
1845 | } else if (data[0] == NET_PACKET_CRYPTO_HS) { | 1986 | } |
1987 | |||
1988 | if (data[0] == NET_PACKET_CRYPTO_HS) { | ||
1846 | IP_Port source; | 1989 | IP_Port source; |
1847 | source.port = 0; | 1990 | source.port = 0; |
1848 | source.ip.family = TCP_FAMILY; | 1991 | source.ip.family = TCP_FAMILY; |
1849 | source.ip.ip6.uint32[0] = tcp_connections_number; | 1992 | source.ip.ip6.uint32[0] = tcp_connections_number; |
1850 | 1993 | ||
1851 | if (handle_new_connection_handshake(c, source, data, length) != 0) | 1994 | if (handle_new_connection_handshake(c, source, data, length, userdata) != 0) { |
1852 | return -1; | 1995 | return -1; |
1996 | } | ||
1853 | 1997 | ||
1854 | return 0; | 1998 | return 0; |
1855 | } else { | ||
1856 | return -1; | ||
1857 | } | 1999 | } |
2000 | |||
2001 | return -1; | ||
1858 | } | 2002 | } |
1859 | 2003 | ||
1860 | /* Add a tcp relay, associating it to a crypt_connection_id. | 2004 | /* Add a tcp relay, associating it to a crypt_connection_id. |
@@ -1866,8 +2010,9 @@ int add_tcp_relay_peer(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port, | |||
1866 | { | 2010 | { |
1867 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 2011 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
1868 | 2012 | ||
1869 | if (conn == 0) | 2013 | if (conn == 0) { |
1870 | return -1; | 2014 | return -1; |
2015 | } | ||
1871 | 2016 | ||
1872 | pthread_mutex_lock(&c->tcp_mutex); | 2017 | pthread_mutex_lock(&c->tcp_mutex); |
1873 | int ret = add_tcp_relay_connection(c->tcp_c, conn->connection_number_tcp, ip_port, public_key); | 2018 | int ret = add_tcp_relay_connection(c->tcp_c, conn->connection_number_tcp, ip_port, public_key); |
@@ -1890,7 +2035,7 @@ int add_tcp_relay(Net_Crypto *c, IP_Port ip_port, const uint8_t *public_key) | |||
1890 | 2035 | ||
1891 | /* Return a random TCP connection number for use in send_tcp_onion_request. | 2036 | /* Return a random TCP connection number for use in send_tcp_onion_request. |
1892 | * | 2037 | * |
1893 | * TODO: This number is just the index of an array that the elements can | 2038 | * TODO(irungentoo): This number is just the index of an array that the elements can |
1894 | * change without warning. | 2039 | * change without warning. |
1895 | * | 2040 | * |
1896 | * return TCP connection number on success. | 2041 | * return TCP connection number on success. |
@@ -1927,8 +2072,9 @@ int send_tcp_onion_request(Net_Crypto *c, unsigned int tcp_connections_number, c | |||
1927 | */ | 2072 | */ |
1928 | unsigned int copy_connected_tcp_relays(Net_Crypto *c, Node_format *tcp_relays, uint16_t num) | 2073 | unsigned int copy_connected_tcp_relays(Net_Crypto *c, Node_format *tcp_relays, uint16_t num) |
1929 | { | 2074 | { |
1930 | if (num == 0) | 2075 | if (num == 0) { |
1931 | return 0; | 2076 | return 0; |
2077 | } | ||
1932 | 2078 | ||
1933 | pthread_mutex_lock(&c->tcp_mutex); | 2079 | pthread_mutex_lock(&c->tcp_mutex); |
1934 | unsigned int ret = tcp_copy_connected_relays(c->tcp_c, tcp_relays, num); | 2080 | unsigned int ret = tcp_copy_connected_relays(c->tcp_c, tcp_relays, num); |
@@ -1937,10 +2083,10 @@ unsigned int copy_connected_tcp_relays(Net_Crypto *c, Node_format *tcp_relays, u | |||
1937 | return ret; | 2083 | return ret; |
1938 | } | 2084 | } |
1939 | 2085 | ||
1940 | static void do_tcp(Net_Crypto *c) | 2086 | static void do_tcp(Net_Crypto *c, void *userdata) |
1941 | { | 2087 | { |
1942 | pthread_mutex_lock(&c->tcp_mutex); | 2088 | pthread_mutex_lock(&c->tcp_mutex); |
1943 | do_tcp_connections(c->tcp_c); | 2089 | do_tcp_connections(c->tcp_c, userdata); |
1944 | pthread_mutex_unlock(&c->tcp_mutex); | 2090 | pthread_mutex_unlock(&c->tcp_mutex); |
1945 | 2091 | ||
1946 | uint32_t i; | 2092 | uint32_t i; |
@@ -1948,11 +2094,12 @@ static void do_tcp(Net_Crypto *c) | |||
1948 | for (i = 0; i < c->crypto_connections_length; ++i) { | 2094 | for (i = 0; i < c->crypto_connections_length; ++i) { |
1949 | Crypto_Connection *conn = get_crypto_connection(c, i); | 2095 | Crypto_Connection *conn = get_crypto_connection(c, i); |
1950 | 2096 | ||
1951 | if (conn == 0) | 2097 | if (conn == 0) { |
1952 | return; | 2098 | return; |
2099 | } | ||
1953 | 2100 | ||
1954 | if (conn->status == CRYPTO_CONN_ESTABLISHED) { | 2101 | if (conn->status == CRYPTO_CONN_ESTABLISHED) { |
1955 | _Bool direct_connected = 0; | 2102 | bool direct_connected = 0; |
1956 | crypto_connection_status(c, i, &direct_connected, NULL); | 2103 | crypto_connection_status(c, i, &direct_connected, NULL); |
1957 | 2104 | ||
1958 | if (direct_connected) { | 2105 | if (direct_connected) { |
@@ -1979,12 +2126,13 @@ static void do_tcp(Net_Crypto *c) | |||
1979 | * return 0 on success. | 2126 | * return 0 on success. |
1980 | */ | 2127 | */ |
1981 | int connection_status_handler(const Net_Crypto *c, int crypt_connection_id, | 2128 | int connection_status_handler(const Net_Crypto *c, int crypt_connection_id, |
1982 | int (*connection_status_callback)(void *object, int id, uint8_t status), void *object, int id) | 2129 | int (*connection_status_callback)(void *object, int id, uint8_t status, void *userdata), void *object, int id) |
1983 | { | 2130 | { |
1984 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 2131 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
1985 | 2132 | ||
1986 | if (conn == 0) | 2133 | if (conn == 0) { |
1987 | return -1; | 2134 | return -1; |
2135 | } | ||
1988 | 2136 | ||
1989 | conn->connection_status_callback = connection_status_callback; | 2137 | conn->connection_status_callback = connection_status_callback; |
1990 | conn->connection_status_callback_object = object; | 2138 | conn->connection_status_callback_object = object; |
@@ -2001,12 +2149,13 @@ int connection_status_handler(const Net_Crypto *c, int crypt_connection_id, | |||
2001 | * return 0 on success. | 2149 | * return 0 on success. |
2002 | */ | 2150 | */ |
2003 | int connection_data_handler(const Net_Crypto *c, int crypt_connection_id, int (*connection_data_callback)(void *object, | 2151 | int connection_data_handler(const Net_Crypto *c, int crypt_connection_id, int (*connection_data_callback)(void *object, |
2004 | int id, uint8_t *data, uint16_t length), void *object, int id) | 2152 | int id, const uint8_t *data, uint16_t length, void *userdata), void *object, int id) |
2005 | { | 2153 | { |
2006 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 2154 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
2007 | 2155 | ||
2008 | if (conn == 0) | 2156 | if (conn == 0) { |
2009 | return -1; | 2157 | return -1; |
2158 | } | ||
2010 | 2159 | ||
2011 | conn->connection_data_callback = connection_data_callback; | 2160 | conn->connection_data_callback = connection_data_callback; |
2012 | conn->connection_data_callback_object = object; | 2161 | conn->connection_data_callback_object = object; |
@@ -2023,12 +2172,14 @@ int connection_data_handler(const Net_Crypto *c, int crypt_connection_id, int (* | |||
2023 | * return 0 on success. | 2172 | * return 0 on success. |
2024 | */ | 2173 | */ |
2025 | int connection_lossy_data_handler(Net_Crypto *c, int crypt_connection_id, | 2174 | int connection_lossy_data_handler(Net_Crypto *c, int crypt_connection_id, |
2026 | int (*connection_lossy_data_callback)(void *object, int id, const uint8_t *data, uint16_t length), void *object, int id) | 2175 | int (*connection_lossy_data_callback)(void *object, int id, const uint8_t *data, uint16_t length, void *userdata), |
2176 | void *object, int id) | ||
2027 | { | 2177 | { |
2028 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 2178 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
2029 | 2179 | ||
2030 | if (conn == 0) | 2180 | if (conn == 0) { |
2031 | return -1; | 2181 | return -1; |
2182 | } | ||
2032 | 2183 | ||
2033 | conn->connection_lossy_data_callback = connection_lossy_data_callback; | 2184 | conn->connection_lossy_data_callback = connection_lossy_data_callback; |
2034 | conn->connection_lossy_data_callback_object = object; | 2185 | conn->connection_lossy_data_callback_object = object; |
@@ -2048,12 +2199,13 @@ int connection_lossy_data_handler(Net_Crypto *c, int crypt_connection_id, | |||
2048 | * return 0 on success. | 2199 | * return 0 on success. |
2049 | */ | 2200 | */ |
2050 | int nc_dht_pk_callback(Net_Crypto *c, int crypt_connection_id, void (*function)(void *data, int32_t number, | 2201 | int nc_dht_pk_callback(Net_Crypto *c, int crypt_connection_id, void (*function)(void *data, int32_t number, |
2051 | const uint8_t *dht_public_key), void *object, uint32_t number) | 2202 | const uint8_t *dht_public_key, void *userdata), void *object, uint32_t number) |
2052 | { | 2203 | { |
2053 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 2204 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
2054 | 2205 | ||
2055 | if (conn == 0) | 2206 | if (conn == 0) { |
2056 | return -1; | 2207 | return -1; |
2208 | } | ||
2057 | 2209 | ||
2058 | conn->dht_pk_callback = function; | 2210 | conn->dht_pk_callback = function; |
2059 | conn->dht_pk_callback_object = object; | 2211 | conn->dht_pk_callback_object = object; |
@@ -2081,31 +2233,36 @@ static int crypto_id_ip_port(const Net_Crypto *c, IP_Port ip_port) | |||
2081 | * Crypto data packets. | 2233 | * Crypto data packets. |
2082 | * | 2234 | * |
2083 | */ | 2235 | */ |
2084 | static int udp_handle_packet(void *object, IP_Port source, const uint8_t *packet, uint16_t length) | 2236 | static int udp_handle_packet(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata) |
2085 | { | 2237 | { |
2086 | if (length <= CRYPTO_MIN_PACKET_SIZE || length > MAX_CRYPTO_PACKET_SIZE) | 2238 | if (length <= CRYPTO_MIN_PACKET_SIZE || length > MAX_CRYPTO_PACKET_SIZE) { |
2087 | return 1; | 2239 | return 1; |
2240 | } | ||
2088 | 2241 | ||
2089 | Net_Crypto *c = object; | 2242 | Net_Crypto *c = (Net_Crypto *)object; |
2090 | int crypt_connection_id = crypto_id_ip_port(c, source); | 2243 | int crypt_connection_id = crypto_id_ip_port(c, source); |
2091 | 2244 | ||
2092 | if (crypt_connection_id == -1) { | 2245 | if (crypt_connection_id == -1) { |
2093 | if (packet[0] != NET_PACKET_CRYPTO_HS) | 2246 | if (packet[0] != NET_PACKET_CRYPTO_HS) { |
2094 | return 1; | 2247 | return 1; |
2248 | } | ||
2095 | 2249 | ||
2096 | if (handle_new_connection_handshake(c, source, packet, length) != 0) | 2250 | if (handle_new_connection_handshake(c, source, packet, length, userdata) != 0) { |
2097 | return 1; | 2251 | return 1; |
2252 | } | ||
2098 | 2253 | ||
2099 | return 0; | 2254 | return 0; |
2100 | } | 2255 | } |
2101 | 2256 | ||
2102 | if (handle_packet_connection(c, crypt_connection_id, packet, length, 1) != 0) | 2257 | if (handle_packet_connection(c, crypt_connection_id, packet, length, 1, userdata) != 0) { |
2103 | return 1; | 2258 | return 1; |
2259 | } | ||
2104 | 2260 | ||
2105 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 2261 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
2106 | 2262 | ||
2107 | if (conn == 0) | 2263 | if (conn == 0) { |
2108 | return -1; | 2264 | return -1; |
2265 | } | ||
2109 | 2266 | ||
2110 | pthread_mutex_lock(&conn->mutex); | 2267 | pthread_mutex_lock(&conn->mutex); |
2111 | 2268 | ||
@@ -2147,8 +2304,9 @@ static void send_crypto_packets(Net_Crypto *c) | |||
2147 | for (i = 0; i < c->crypto_connections_length; ++i) { | 2304 | for (i = 0; i < c->crypto_connections_length; ++i) { |
2148 | Crypto_Connection *conn = get_crypto_connection(c, i); | 2305 | Crypto_Connection *conn = get_crypto_connection(c, i); |
2149 | 2306 | ||
2150 | if (conn == 0) | 2307 | if (conn == 0) { |
2151 | return; | 2308 | return; |
2309 | } | ||
2152 | 2310 | ||
2153 | if (CRYPTO_SEND_PACKET_INTERVAL + conn->temp_packet_sent_time < temp_time) { | 2311 | if (CRYPTO_SEND_PACKET_INTERVAL + conn->temp_packet_sent_time < temp_time) { |
2154 | send_temp_packet(c, i); | 2312 | send_temp_packet(c, i); |
@@ -2159,7 +2317,6 @@ static void send_crypto_packets(Net_Crypto *c) | |||
2159 | if (send_request_packet(c, i) == 0) { | 2317 | if (send_request_packet(c, i) == 0) { |
2160 | conn->last_request_packet_sent = temp_time; | 2318 | conn->last_request_packet_sent = temp_time; |
2161 | } | 2319 | } |
2162 | |||
2163 | } | 2320 | } |
2164 | 2321 | ||
2165 | if (conn->status == CRYPTO_CONN_ESTABLISHED) { | 2322 | if (conn->status == CRYPTO_CONN_ESTABLISHED) { |
@@ -2170,14 +2327,17 @@ static void send_crypto_packets(Net_Crypto *c) | |||
2170 | double request_packet_interval2 = ((CRYPTO_PACKET_MIN_RATE / conn->packet_recv_rate) * | 2327 | double request_packet_interval2 = ((CRYPTO_PACKET_MIN_RATE / conn->packet_recv_rate) * |
2171 | (double)CRYPTO_SEND_PACKET_INTERVAL) + (double)PACKET_COUNTER_AVERAGE_INTERVAL; | 2328 | (double)CRYPTO_SEND_PACKET_INTERVAL) + (double)PACKET_COUNTER_AVERAGE_INTERVAL; |
2172 | 2329 | ||
2173 | if (request_packet_interval2 < request_packet_interval) | 2330 | if (request_packet_interval2 < request_packet_interval) { |
2174 | request_packet_interval = request_packet_interval2; | 2331 | request_packet_interval = request_packet_interval2; |
2332 | } | ||
2175 | 2333 | ||
2176 | if (request_packet_interval < PACKET_COUNTER_AVERAGE_INTERVAL) | 2334 | if (request_packet_interval < PACKET_COUNTER_AVERAGE_INTERVAL) { |
2177 | request_packet_interval = PACKET_COUNTER_AVERAGE_INTERVAL; | 2335 | request_packet_interval = PACKET_COUNTER_AVERAGE_INTERVAL; |
2336 | } | ||
2178 | 2337 | ||
2179 | if (request_packet_interval > CRYPTO_SEND_PACKET_INTERVAL) | 2338 | if (request_packet_interval > CRYPTO_SEND_PACKET_INTERVAL) { |
2180 | request_packet_interval = CRYPTO_SEND_PACKET_INTERVAL; | 2339 | request_packet_interval = CRYPTO_SEND_PACKET_INTERVAL; |
2340 | } | ||
2181 | 2341 | ||
2182 | if (temp_time - conn->last_request_packet_sent > (uint64_t)request_packet_interval) { | 2342 | if (temp_time - conn->last_request_packet_sent > (uint64_t)request_packet_interval) { |
2183 | if (send_request_packet(c, i) == 0) { | 2343 | if (send_request_packet(c, i) == 0) { |
@@ -2221,7 +2381,7 @@ static void send_crypto_packets(Net_Crypto *c) | |||
2221 | conn->last_num_packets_sent[n_p_pos] = packets_sent; | 2381 | conn->last_num_packets_sent[n_p_pos] = packets_sent; |
2222 | conn->last_num_packets_resent[n_p_pos] = packets_resent; | 2382 | conn->last_num_packets_resent[n_p_pos] = packets_resent; |
2223 | 2383 | ||
2224 | _Bool direct_connected = 0; | 2384 | bool direct_connected = 0; |
2225 | crypto_connection_status(c, i, &direct_connected, NULL); | 2385 | crypto_connection_status(c, i, &direct_connected, NULL); |
2226 | 2386 | ||
2227 | if (direct_connected && conn->last_tcp_sent + CONGESTION_EVENT_TIMEOUT > temp_time) { | 2387 | if (direct_connected && conn->last_tcp_sent + CONGESTION_EVENT_TIMEOUT > temp_time) { |
@@ -2229,7 +2389,7 @@ static void send_crypto_packets(Net_Crypto *c) | |||
2229 | } else { | 2389 | } else { |
2230 | long signed int total_sent = 0, total_resent = 0; | 2390 | long signed int total_sent = 0, total_resent = 0; |
2231 | 2391 | ||
2232 | //TODO use real delay | 2392 | // TODO(irungentoo): use real delay |
2233 | unsigned int delay = (unsigned int)((conn->rtt_time / PACKET_COUNTER_AVERAGE_INTERVAL) + 0.5); | 2393 | unsigned int delay = (unsigned int)((conn->rtt_time / PACKET_COUNTER_AVERAGE_INTERVAL) + 0.5); |
2234 | unsigned int packets_set_rem_array = (CONGESTION_LAST_SENT_ARRAY_SIZE - CONGESTION_QUEUE_ARRAY_SIZE); | 2394 | unsigned int packets_set_rem_array = (CONGESTION_LAST_SENT_ARRAY_SIZE - CONGESTION_QUEUE_ARRAY_SIZE); |
2235 | 2395 | ||
@@ -2246,8 +2406,9 @@ static void send_crypto_packets(Net_Crypto *c) | |||
2246 | if (sum > 0) { | 2406 | if (sum > 0) { |
2247 | total_sent -= sum; | 2407 | total_sent -= sum; |
2248 | } else { | 2408 | } else { |
2249 | if (total_resent > -sum) | 2409 | if (total_resent > -sum) { |
2250 | total_resent = -sum; | 2410 | total_resent = -sum; |
2411 | } | ||
2251 | } | 2412 | } |
2252 | 2413 | ||
2253 | /* if queue is too big only allow resending packets. */ | 2414 | /* if queue is too big only allow resending packets. */ |
@@ -2258,12 +2419,13 @@ static void send_crypto_packets(Net_Crypto *c) | |||
2258 | double min_speed_request = 1000.0 * (((double)(total_sent + total_resent)) / ((double)( | 2419 | double min_speed_request = 1000.0 * (((double)(total_sent + total_resent)) / ((double)( |
2259 | CONGESTION_QUEUE_ARRAY_SIZE) * PACKET_COUNTER_AVERAGE_INTERVAL)); | 2420 | CONGESTION_QUEUE_ARRAY_SIZE) * PACKET_COUNTER_AVERAGE_INTERVAL)); |
2260 | 2421 | ||
2261 | if (min_speed < CRYPTO_PACKET_MIN_RATE) | 2422 | if (min_speed < CRYPTO_PACKET_MIN_RATE) { |
2262 | min_speed = CRYPTO_PACKET_MIN_RATE; | 2423 | min_speed = CRYPTO_PACKET_MIN_RATE; |
2424 | } | ||
2263 | 2425 | ||
2264 | double send_array_ratio = (((double)npackets) / min_speed); | 2426 | double send_array_ratio = (((double)npackets) / min_speed); |
2265 | 2427 | ||
2266 | //TODO: Improve formula? | 2428 | // TODO(irungentoo): Improve formula? |
2267 | if (send_array_ratio > SEND_QUEUE_RATIO && CRYPTO_MIN_QUEUE_LENGTH < npackets) { | 2429 | if (send_array_ratio > SEND_QUEUE_RATIO && CRYPTO_MIN_QUEUE_LENGTH < npackets) { |
2268 | conn->packet_send_rate = min_speed * (1.0 / (send_array_ratio / SEND_QUEUE_RATIO)); | 2430 | conn->packet_send_rate = min_speed * (1.0 / (send_array_ratio / SEND_QUEUE_RATIO)); |
2269 | } else if (conn->last_congestion_event + CONGESTION_EVENT_TIMEOUT < temp_time) { | 2431 | } else if (conn->last_congestion_event + CONGESTION_EVENT_TIMEOUT < temp_time) { |
@@ -2282,7 +2444,6 @@ static void send_crypto_packets(Net_Crypto *c) | |||
2282 | conn->packet_send_rate_requested = conn->packet_send_rate; | 2444 | conn->packet_send_rate_requested = conn->packet_send_rate; |
2283 | } | 2445 | } |
2284 | } | 2446 | } |
2285 | |||
2286 | } | 2447 | } |
2287 | 2448 | ||
2288 | if (conn->last_packets_left_set == 0 || conn->last_packets_left_requested_set == 0) { | 2449 | if (conn->last_packets_left_set == 0 || conn->last_packets_left_requested_set == 0) { |
@@ -2320,8 +2481,9 @@ static void send_crypto_packets(Net_Crypto *c) | |||
2320 | conn->last_packets_left_requested_rem = rem; | 2481 | conn->last_packets_left_requested_rem = rem; |
2321 | } | 2482 | } |
2322 | 2483 | ||
2323 | if (conn->packets_left > conn->packets_left_requested) | 2484 | if (conn->packets_left > conn->packets_left_requested) { |
2324 | conn->packets_left_requested = conn->packets_left; | 2485 | conn->packets_left_requested = conn->packets_left; |
2486 | } | ||
2325 | } | 2487 | } |
2326 | 2488 | ||
2327 | int ret = send_requested_packets(c, i, conn->packets_left_requested); | 2489 | int ret = send_requested_packets(c, i, conn->packets_left_requested); |
@@ -2369,7 +2531,7 @@ static void send_crypto_packets(Net_Crypto *c) | |||
2369 | /* Return 1 if max speed was reached for this connection (no more data can be physically through the pipe). | 2531 | /* Return 1 if max speed was reached for this connection (no more data can be physically through the pipe). |
2370 | * Return 0 if it wasn't reached. | 2532 | * Return 0 if it wasn't reached. |
2371 | */ | 2533 | */ |
2372 | _Bool max_speed_reached(Net_Crypto *c, int crypt_connection_id) | 2534 | bool max_speed_reached(Net_Crypto *c, int crypt_connection_id) |
2373 | { | 2535 | { |
2374 | return reset_max_speed_reached(c, crypt_connection_id) != 0; | 2536 | return reset_max_speed_reached(c, crypt_connection_id) != 0; |
2375 | } | 2537 | } |
@@ -2381,16 +2543,17 @@ uint32_t crypto_num_free_sendqueue_slots(const Net_Crypto *c, int crypt_connecti | |||
2381 | { | 2543 | { |
2382 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 2544 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
2383 | 2545 | ||
2384 | if (conn == 0) | 2546 | if (conn == 0) { |
2385 | return 0; | 2547 | return 0; |
2548 | } | ||
2386 | 2549 | ||
2387 | uint32_t max_packets = CRYPTO_PACKET_BUFFER_SIZE - num_packets_array(&conn->send_array); | 2550 | uint32_t max_packets = CRYPTO_PACKET_BUFFER_SIZE - num_packets_array(&conn->send_array); |
2388 | 2551 | ||
2389 | if (conn->packets_left < max_packets) { | 2552 | if (conn->packets_left < max_packets) { |
2390 | return conn->packets_left; | 2553 | return conn->packets_left; |
2391 | } else { | ||
2392 | return max_packets; | ||
2393 | } | 2554 | } |
2555 | |||
2556 | return max_packets; | ||
2394 | } | 2557 | } |
2395 | 2558 | ||
2396 | /* Sends a lossless cryptopacket. | 2559 | /* Sends a lossless cryptopacket. |
@@ -2403,30 +2566,37 @@ uint32_t crypto_num_free_sendqueue_slots(const Net_Crypto *c, int crypt_connecti | |||
2403 | int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length, | 2566 | int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length, |
2404 | uint8_t congestion_control) | 2567 | uint8_t congestion_control) |
2405 | { | 2568 | { |
2406 | if (length == 0) | 2569 | if (length == 0) { |
2407 | return -1; | 2570 | return -1; |
2571 | } | ||
2408 | 2572 | ||
2409 | if (data[0] < CRYPTO_RESERVED_PACKETS) | 2573 | if (data[0] < CRYPTO_RESERVED_PACKETS) { |
2410 | return -1; | 2574 | return -1; |
2575 | } | ||
2411 | 2576 | ||
2412 | if (data[0] >= PACKET_ID_LOSSY_RANGE_START) | 2577 | if (data[0] >= PACKET_ID_LOSSY_RANGE_START) { |
2413 | return -1; | 2578 | return -1; |
2579 | } | ||
2414 | 2580 | ||
2415 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 2581 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
2416 | 2582 | ||
2417 | if (conn == 0) | 2583 | if (conn == 0) { |
2418 | return -1; | 2584 | return -1; |
2585 | } | ||
2419 | 2586 | ||
2420 | if (conn->status != CRYPTO_CONN_ESTABLISHED) | 2587 | if (conn->status != CRYPTO_CONN_ESTABLISHED) { |
2421 | return -1; | 2588 | return -1; |
2589 | } | ||
2422 | 2590 | ||
2423 | if (congestion_control && conn->packets_left == 0) | 2591 | if (congestion_control && conn->packets_left == 0) { |
2424 | return -1; | 2592 | return -1; |
2593 | } | ||
2425 | 2594 | ||
2426 | int64_t ret = send_lossless_packet(c, crypt_connection_id, data, length, congestion_control); | 2595 | int64_t ret = send_lossless_packet(c, crypt_connection_id, data, length, congestion_control); |
2427 | 2596 | ||
2428 | if (ret == -1) | 2597 | if (ret == -1) { |
2429 | return -1; | 2598 | return -1; |
2599 | } | ||
2430 | 2600 | ||
2431 | if (congestion_control) { | 2601 | if (congestion_control) { |
2432 | --conn->packets_left; | 2602 | --conn->packets_left; |
@@ -2448,17 +2618,18 @@ int cryptpacket_received(Net_Crypto *c, int crypt_connection_id, uint32_t packet | |||
2448 | { | 2618 | { |
2449 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 2619 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
2450 | 2620 | ||
2451 | if (conn == 0) | 2621 | if (conn == 0) { |
2452 | return -1; | 2622 | return -1; |
2623 | } | ||
2453 | 2624 | ||
2454 | uint32_t num = conn->send_array.buffer_end - conn->send_array.buffer_start; | 2625 | uint32_t num = conn->send_array.buffer_end - conn->send_array.buffer_start; |
2455 | uint32_t num1 = packet_number - conn->send_array.buffer_start; | 2626 | uint32_t num1 = packet_number - conn->send_array.buffer_start; |
2456 | 2627 | ||
2457 | if (num < num1) { | 2628 | if (num < num1) { |
2458 | return 0; | 2629 | return 0; |
2459 | } else { | ||
2460 | return -1; | ||
2461 | } | 2630 | } |
2631 | |||
2632 | return -1; | ||
2462 | } | 2633 | } |
2463 | 2634 | ||
2464 | /* return -1 on failure. | 2635 | /* return -1 on failure. |
@@ -2468,14 +2639,17 @@ int cryptpacket_received(Net_Crypto *c, int crypt_connection_id, uint32_t packet | |||
2468 | */ | 2639 | */ |
2469 | int send_lossy_cryptpacket(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length) | 2640 | int send_lossy_cryptpacket(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length) |
2470 | { | 2641 | { |
2471 | if (length == 0 || length > MAX_CRYPTO_DATA_SIZE) | 2642 | if (length == 0 || length > MAX_CRYPTO_DATA_SIZE) { |
2472 | return -1; | 2643 | return -1; |
2644 | } | ||
2473 | 2645 | ||
2474 | if (data[0] < PACKET_ID_LOSSY_RANGE_START) | 2646 | if (data[0] < PACKET_ID_LOSSY_RANGE_START) { |
2475 | return -1; | 2647 | return -1; |
2648 | } | ||
2476 | 2649 | ||
2477 | if (data[0] >= (PACKET_ID_LOSSY_RANGE_START + PACKET_ID_LOSSY_RANGE_SIZE)) | 2650 | if (data[0] >= (PACKET_ID_LOSSY_RANGE_START + PACKET_ID_LOSSY_RANGE_SIZE)) { |
2478 | return -1; | 2651 | return -1; |
2652 | } | ||
2479 | 2653 | ||
2480 | pthread_mutex_lock(&c->connections_mutex); | 2654 | pthread_mutex_lock(&c->connections_mutex); |
2481 | ++c->connection_use_counter; | 2655 | ++c->connection_use_counter; |
@@ -2507,7 +2681,7 @@ int send_lossy_cryptpacket(Net_Crypto *c, int crypt_connection_id, const uint8_t | |||
2507 | */ | 2681 | */ |
2508 | int crypto_kill(Net_Crypto *c, int crypt_connection_id) | 2682 | int crypto_kill(Net_Crypto *c, int crypt_connection_id) |
2509 | { | 2683 | { |
2510 | while (1) { /* TODO: is this really the best way to do this? */ | 2684 | while (1) { /* TODO(irungentoo): is this really the best way to do this? */ |
2511 | pthread_mutex_lock(&c->connections_mutex); | 2685 | pthread_mutex_lock(&c->connections_mutex); |
2512 | 2686 | ||
2513 | if (!c->connection_use_counter) { | 2687 | if (!c->connection_use_counter) { |
@@ -2522,8 +2696,9 @@ int crypto_kill(Net_Crypto *c, int crypt_connection_id) | |||
2522 | int ret = -1; | 2696 | int ret = -1; |
2523 | 2697 | ||
2524 | if (conn) { | 2698 | if (conn) { |
2525 | if (conn->status == CRYPTO_CONN_ESTABLISHED) | 2699 | if (conn->status == CRYPTO_CONN_ESTABLISHED) { |
2526 | send_kill_packet(c, crypt_connection_id); | 2700 | send_kill_packet(c, crypt_connection_id); |
2701 | } | ||
2527 | 2702 | ||
2528 | pthread_mutex_lock(&c->tcp_mutex); | 2703 | pthread_mutex_lock(&c->tcp_mutex); |
2529 | kill_tcp_connection_to(c->tcp_c, conn->connection_number_tcp); | 2704 | kill_tcp_connection_to(c->tcp_c, conn->connection_number_tcp); |
@@ -2547,24 +2722,27 @@ int crypto_kill(Net_Crypto *c, int crypt_connection_id) | |||
2547 | * sets direct_connected to 1 if connection connects directly to other, 0 if it isn't. | 2722 | * sets direct_connected to 1 if connection connects directly to other, 0 if it isn't. |
2548 | * sets online_tcp_relays to the number of connected tcp relays this connection has. | 2723 | * sets online_tcp_relays to the number of connected tcp relays this connection has. |
2549 | */ | 2724 | */ |
2550 | unsigned int crypto_connection_status(const Net_Crypto *c, int crypt_connection_id, _Bool *direct_connected, | 2725 | unsigned int crypto_connection_status(const Net_Crypto *c, int crypt_connection_id, bool *direct_connected, |
2551 | unsigned int *online_tcp_relays) | 2726 | unsigned int *online_tcp_relays) |
2552 | { | 2727 | { |
2553 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 2728 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
2554 | 2729 | ||
2555 | if (conn == 0) | 2730 | if (conn == 0) { |
2556 | return CRYPTO_CONN_NO_CONNECTION; | 2731 | return CRYPTO_CONN_NO_CONNECTION; |
2732 | } | ||
2557 | 2733 | ||
2558 | if (direct_connected) { | 2734 | if (direct_connected) { |
2559 | *direct_connected = 0; | 2735 | *direct_connected = 0; |
2560 | 2736 | ||
2561 | uint64_t current_time = unix_time(); | 2737 | uint64_t current_time = unix_time(); |
2562 | 2738 | ||
2563 | if ((UDP_DIRECT_TIMEOUT + conn->direct_lastrecv_timev4) > current_time) | 2739 | if ((UDP_DIRECT_TIMEOUT + conn->direct_lastrecv_timev4) > current_time) { |
2564 | *direct_connected = 1; | 2740 | *direct_connected = 1; |
2741 | } | ||
2565 | 2742 | ||
2566 | if ((UDP_DIRECT_TIMEOUT + conn->direct_lastrecv_timev6) > current_time) | 2743 | if ((UDP_DIRECT_TIMEOUT + conn->direct_lastrecv_timev6) > current_time) { |
2567 | *direct_connected = 1; | 2744 | *direct_connected = 1; |
2745 | } | ||
2568 | } | 2746 | } |
2569 | 2747 | ||
2570 | if (online_tcp_relays) { | 2748 | if (online_tcp_relays) { |
@@ -2582,7 +2760,7 @@ void new_keys(Net_Crypto *c) | |||
2582 | /* Save the public and private keys to the keys array. | 2760 | /* Save the public and private keys to the keys array. |
2583 | * Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES. | 2761 | * Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES. |
2584 | * | 2762 | * |
2585 | * TODO: Save only secret key. | 2763 | * TODO(irungentoo): Save only secret key. |
2586 | */ | 2764 | */ |
2587 | void save_keys(const Net_Crypto *c, uint8_t *keys) | 2765 | void save_keys(const Net_Crypto *c, uint8_t *keys) |
2588 | { | 2766 | { |
@@ -2602,17 +2780,21 @@ void load_secret_key(Net_Crypto *c, const uint8_t *sk) | |||
2602 | /* Run this to (re)initialize net_crypto. | 2780 | /* Run this to (re)initialize net_crypto. |
2603 | * Sets all the global connection variables to their default values. | 2781 | * Sets all the global connection variables to their default values. |
2604 | */ | 2782 | */ |
2605 | Net_Crypto *new_net_crypto(DHT *dht, TCP_Proxy_Info *proxy_info) | 2783 | Net_Crypto *new_net_crypto(Logger *log, DHT *dht, TCP_Proxy_Info *proxy_info) |
2606 | { | 2784 | { |
2607 | unix_time_update(); | 2785 | unix_time_update(); |
2608 | 2786 | ||
2609 | if (dht == NULL) | 2787 | if (dht == NULL) { |
2610 | return NULL; | 2788 | return NULL; |
2789 | } | ||
2611 | 2790 | ||
2612 | Net_Crypto *temp = calloc(1, sizeof(Net_Crypto)); | 2791 | Net_Crypto *temp = (Net_Crypto *)calloc(1, sizeof(Net_Crypto)); |
2613 | 2792 | ||
2614 | if (temp == NULL) | 2793 | if (temp == NULL) { |
2615 | return NULL; | 2794 | return NULL; |
2795 | } | ||
2796 | |||
2797 | temp->log = log; | ||
2616 | 2798 | ||
2617 | temp->tcp_c = new_tcp_connections(dht->self_secret_key, proxy_info); | 2799 | temp->tcp_c = new_tcp_connections(dht->self_secret_key, proxy_info); |
2618 | 2800 | ||
@@ -2648,7 +2830,7 @@ Net_Crypto *new_net_crypto(DHT *dht, TCP_Proxy_Info *proxy_info) | |||
2648 | return temp; | 2830 | return temp; |
2649 | } | 2831 | } |
2650 | 2832 | ||
2651 | static void kill_timedout(Net_Crypto *c) | 2833 | static void kill_timedout(Net_Crypto *c, void *userdata) |
2652 | { | 2834 | { |
2653 | uint32_t i; | 2835 | uint32_t i; |
2654 | //uint64_t temp_time = current_time_monotonic(); | 2836 | //uint64_t temp_time = current_time_monotonic(); |
@@ -2656,24 +2838,30 @@ static void kill_timedout(Net_Crypto *c) | |||
2656 | for (i = 0; i < c->crypto_connections_length; ++i) { | 2838 | for (i = 0; i < c->crypto_connections_length; ++i) { |
2657 | Crypto_Connection *conn = get_crypto_connection(c, i); | 2839 | Crypto_Connection *conn = get_crypto_connection(c, i); |
2658 | 2840 | ||
2659 | if (conn == 0) | 2841 | if (conn == 0) { |
2660 | return; | 2842 | return; |
2843 | } | ||
2661 | 2844 | ||
2662 | if (conn->status == CRYPTO_CONN_NO_CONNECTION) | 2845 | if (conn->status == CRYPTO_CONN_NO_CONNECTION) { |
2663 | continue; | 2846 | continue; |
2847 | } | ||
2664 | 2848 | ||
2665 | if (conn->status == CRYPTO_CONN_COOKIE_REQUESTING || conn->status == CRYPTO_CONN_HANDSHAKE_SENT | 2849 | if (conn->status == CRYPTO_CONN_COOKIE_REQUESTING || conn->status == CRYPTO_CONN_HANDSHAKE_SENT |
2666 | || conn->status == CRYPTO_CONN_NOT_CONFIRMED) { | 2850 | || conn->status == CRYPTO_CONN_NOT_CONFIRMED) { |
2667 | if (conn->temp_packet_num_sent < MAX_NUM_SENDPACKET_TRIES) | 2851 | if (conn->temp_packet_num_sent < MAX_NUM_SENDPACKET_TRIES) { |
2668 | continue; | 2852 | continue; |
2853 | } | ||
2669 | 2854 | ||
2670 | connection_kill(c, i); | 2855 | connection_kill(c, i, userdata); |
2671 | |||
2672 | } | 2856 | } |
2673 | 2857 | ||
2858 | #if 0 | ||
2859 | |||
2674 | if (conn->status == CRYPTO_CONN_ESTABLISHED) { | 2860 | if (conn->status == CRYPTO_CONN_ESTABLISHED) { |
2675 | //TODO: add a timeout here? | 2861 | // TODO(irungentoo): add a timeout here? |
2676 | } | 2862 | } |
2863 | |||
2864 | #endif | ||
2677 | } | 2865 | } |
2678 | } | 2866 | } |
2679 | 2867 | ||
@@ -2685,11 +2873,11 @@ uint32_t crypto_run_interval(const Net_Crypto *c) | |||
2685 | } | 2873 | } |
2686 | 2874 | ||
2687 | /* Main loop. */ | 2875 | /* Main loop. */ |
2688 | void do_net_crypto(Net_Crypto *c) | 2876 | void do_net_crypto(Net_Crypto *c, void *userdata) |
2689 | { | 2877 | { |
2690 | unix_time_update(); | 2878 | unix_time_update(); |
2691 | kill_timedout(c); | 2879 | kill_timedout(c, userdata); |
2692 | do_tcp(c); | 2880 | do_tcp(c, userdata); |
2693 | send_crypto_packets(c); | 2881 | send_crypto_packets(c); |
2694 | } | 2882 | } |
2695 | 2883 | ||