diff options
Diffstat (limited to 'toxcore/net_crypto.c')
-rw-r--r-- | toxcore/net_crypto.c | 65 |
1 files changed, 37 insertions, 28 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index b168fc76..59a068c8 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c | |||
@@ -149,9 +149,9 @@ void random_nonce(uint8_t *nonce) | |||
149 | } | 149 | } |
150 | } | 150 | } |
151 | 151 | ||
152 | /* return 0 if there is no received data in the buffer. | 152 | /* return 0 if there is no received data in the buffer. |
153 | * return -1 if the packet was discarded. | 153 | * return -1 if the packet was discarded. |
154 | * return length of received data if successful. | 154 | * return length of received data if successful. |
155 | */ | 155 | */ |
156 | int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data) | 156 | int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data) |
157 | { | 157 | { |
@@ -182,8 +182,8 @@ int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data) | |||
182 | return -1; | 182 | return -1; |
183 | } | 183 | } |
184 | 184 | ||
185 | /* return 0 if data could not be put in packet queue. | 185 | /* return 0 if data could not be put in packet queue. |
186 | * return 1 if data was put into the queue. | 186 | * return 1 if data was put into the queue. |
187 | */ | 187 | */ |
188 | int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length) | 188 | int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length) |
189 | { | 189 | { |
@@ -220,8 +220,8 @@ int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uin | |||
220 | * Data represents the data we send with the request with length being the length of the data. | 220 | * Data represents the data we send with the request with length being the length of the data. |
221 | * request_id is the id of the request (32 = friend request, 254 = ping request). | 221 | * request_id is the id of the request (32 = friend request, 254 = ping request). |
222 | * | 222 | * |
223 | * returns -1 on failure. | 223 | * return -1 on failure. |
224 | * returns the length of the created packet on success. | 224 | * return the length of the created packet on success. |
225 | */ | 225 | */ |
226 | int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *packet, uint8_t *recv_public_key, | 226 | int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *packet, uint8_t *recv_public_key, |
227 | uint8_t *data, uint32_t length, uint8_t request_id) | 227 | uint8_t *data, uint32_t length, uint8_t request_id) |
@@ -251,7 +251,8 @@ int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t * | |||
251 | /* Puts the senders public key in the request in public_key, the data from the request | 251 | /* Puts the senders public key in the request in public_key, the data from the request |
252 | * in data if a friend or ping request was sent to us and returns the length of the data. | 252 | * in data if a friend or ping request was sent to us and returns the length of the data. |
253 | * packet is the request packet and length is its length. | 253 | * packet is the request packet and length is its length. |
254 | * return -1 if not valid request. | 254 | * |
255 | * return -1 if not valid request. | ||
255 | */ | 256 | */ |
256 | static int handle_request(Net_Crypto *c, uint8_t *public_key, uint8_t *data, uint8_t *request_id, uint8_t *packet, | 257 | static int handle_request(Net_Crypto *c, uint8_t *public_key, uint8_t *data, uint8_t *request_id, uint8_t *packet, |
257 | uint16_t length) | 258 | uint16_t length) |
@@ -348,8 +349,9 @@ static int send_cryptohandshake(Net_Crypto *c, int connection_id, uint8_t *publi | |||
348 | } | 349 | } |
349 | 350 | ||
350 | /* Extract secret nonce, session public key and public_key from a packet(data) with length length. | 351 | /* Extract secret nonce, session public key and public_key from a packet(data) with length length. |
351 | * return 1 if successful. | 352 | * |
352 | * return 0 if failure. | 353 | * return 1 if successful. |
354 | * return 0 if failure. | ||
353 | */ | 355 | */ |
354 | static int handle_cryptohandshake(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, | 356 | static int handle_cryptohandshake(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, |
355 | uint8_t *session_key, uint8_t *data, uint16_t length) | 357 | uint8_t *session_key, uint8_t *data, uint16_t length) |
@@ -381,8 +383,9 @@ static int handle_cryptohandshake(Net_Crypto *c, uint8_t *public_key, uint8_t *s | |||
381 | } | 383 | } |
382 | 384 | ||
383 | /* Get crypto connection id from public key of peer. | 385 | /* Get crypto connection id from public key of peer. |
384 | * return -1 if there are no connections like we are looking for. | 386 | * |
385 | * return id if it found it. | 387 | * return -1 if there are no connections like we are looking for. |
388 | * return id if it found it. | ||
386 | */ | 389 | */ |
387 | static int getcryptconnection_id(Net_Crypto *c, uint8_t *public_key) | 390 | static int getcryptconnection_id(Net_Crypto *c, uint8_t *public_key) |
388 | { | 391 | { |
@@ -398,7 +401,8 @@ static int getcryptconnection_id(Net_Crypto *c, uint8_t *public_key) | |||
398 | } | 401 | } |
399 | 402 | ||
400 | /* Set the size of the friend list to numfriends. | 403 | /* Set the size of the friend list to numfriends. |
401 | * return -1 if realloc fails. | 404 | * |
405 | * return -1 if realloc fails. | ||
402 | */ | 406 | */ |
403 | int realloc_cryptoconnection(Net_Crypto *c, uint32_t num) | 407 | int realloc_cryptoconnection(Net_Crypto *c, uint32_t num) |
404 | { | 408 | { |
@@ -418,8 +422,9 @@ int realloc_cryptoconnection(Net_Crypto *c, uint32_t num) | |||
418 | } | 422 | } |
419 | 423 | ||
420 | /* Start a secure connection with other peer who has public_key and ip_port. | 424 | /* Start a secure connection with other peer who has public_key and ip_port. |
421 | * returns -1 if failure. | 425 | * |
422 | * returns crypt_connection_id of the initialized connection if everything went well. | 426 | * return -1 if failure. |
427 | * return crypt_connection_id of the initialized connection if everything went well. | ||
423 | */ | 428 | */ |
424 | int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port) | 429 | int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port) |
425 | { | 430 | { |
@@ -469,8 +474,9 @@ int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port) | |||
469 | } | 474 | } |
470 | 475 | ||
471 | /* Handle an incoming connection. | 476 | /* Handle an incoming connection. |
472 | * return -1 if no crypto inbound connection. | 477 | * |
473 | * return incoming connection id (Lossless_UDP one) if there is an incoming crypto connection. | 478 | * return -1 if no crypto inbound connection. |
479 | * return incoming connection id (Lossless_UDP one) if there is an incoming crypto connection. | ||
474 | * | 480 | * |
475 | * Put the public key of the peer in public_key, the secret_nonce from the handshake into secret_nonce | 481 | * Put the public key of the peer in public_key, the secret_nonce from the handshake into secret_nonce |
476 | * and the session public key for the connection in session_key. | 482 | * and the session public key for the connection in session_key. |
@@ -507,8 +513,9 @@ int crypto_inbound(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, ui | |||
507 | } | 513 | } |
508 | 514 | ||
509 | /* Kill a crypto connection. | 515 | /* Kill a crypto connection. |
510 | * return 0 if killed successfully. | 516 | * |
511 | * return 1 if there was a problem. | 517 | * return 0 if killed successfully. |
518 | * return 1 if there was a problem. | ||
512 | */ | 519 | */ |
513 | int crypto_kill(Net_Crypto *c, int crypt_connection_id) | 520 | int crypto_kill(Net_Crypto *c, int crypt_connection_id) |
514 | { | 521 | { |
@@ -536,8 +543,9 @@ int crypto_kill(Net_Crypto *c, int crypt_connection_id) | |||
536 | } | 543 | } |
537 | 544 | ||
538 | /* Accept an incoming connection using the parameters provided by crypto_inbound. | 545 | /* Accept an incoming connection using the parameters provided by crypto_inbound. |
539 | * return -1 if not successful. | 546 | * |
540 | * returns the crypt_connection_id if successful. | 547 | * return -1 if not successful. |
548 | * return the crypt_connection_id if successful. | ||
541 | */ | 549 | */ |
542 | int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, uint8_t *secret_nonce, | 550 | int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, uint8_t *secret_nonce, |
543 | uint8_t *session_key) | 551 | uint8_t *session_key) |
@@ -595,11 +603,11 @@ int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, | |||
595 | return -1; | 603 | return -1; |
596 | } | 604 | } |
597 | 605 | ||
598 | /* return 0 if no connection. | 606 | /* return 0 if no connection. |
599 | * return 1 we have sent a handshake. | 607 | * return 1 we have sent a handshake. |
600 | * return 2 if connection is not confirmed yet (we have received a handshake but no empty data packet). | 608 | * return 2 if connection is not confirmed yet (we have received a handshake but no empty data packet). |
601 | * return 3 if the connection is established. | 609 | * return 3 if the connection is established. |
602 | * return 4 if the connection is timed out and waiting to be killed. | 610 | * return 4 if the connection is timed out and waiting to be killed. |
603 | */ | 611 | */ |
604 | int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id) | 612 | int is_cryptoconnected(Net_Crypto *c, int crypt_connection_id) |
605 | { | 613 | { |
@@ -633,9 +641,10 @@ void load_keys(Net_Crypto *c, uint8_t *keys) | |||
633 | } | 641 | } |
634 | 642 | ||
635 | /* Adds an incoming connection to the incoming_connection list. | 643 | /* Adds an incoming connection to the incoming_connection list. |
636 | * returns 0 if successful | ||
637 | * returns 1 if failure. | ||
638 | * TODO: Optimize this. | 644 | * TODO: Optimize this. |
645 | * | ||
646 | * returns 0 if successful | ||
647 | * returns 1 if failure. | ||
639 | */ | 648 | */ |
640 | static int new_incoming(Net_Crypto *c, int id) | 649 | static int new_incoming(Net_Crypto *c, int id) |
641 | { | 650 | { |