summaryrefslogtreecommitdiff
path: root/protocols/Tox/libtox/src/toxcore/net_crypto.h
diff options
context:
space:
mode:
authordartraiden <wowemuh@gmail.com>2022-02-07 00:34:21 +0300
committerdartraiden <wowemuh@gmail.com>2022-02-07 00:35:37 +0300
commit223306302f39455970b67a97ba62e4a542224f7a (patch)
tree21da0622af231bea688b413d1238d15315d658ce /protocols/Tox/libtox/src/toxcore/net_crypto.h
parent41317031f257ad6a06fa743860aff212074dc416 (diff)
Update libtox to 0.2.15
Diffstat (limited to 'protocols/Tox/libtox/src/toxcore/net_crypto.h')
-rw-r--r--protocols/Tox/libtox/src/toxcore/net_crypto.h127
1 files changed, 70 insertions, 57 deletions
diff --git a/protocols/Tox/libtox/src/toxcore/net_crypto.h b/protocols/Tox/libtox/src/toxcore/net_crypto.h
index 33275708af..9a3bd1e21c 100644
--- a/protocols/Tox/libtox/src/toxcore/net_crypto.h
+++ b/protocols/Tox/libtox/src/toxcore/net_crypto.h
@@ -3,45 +3,45 @@
* Copyright © 2013 Tox project.
*/
-/*
+/**
* Functions for the core network crypto.
*/
#ifndef C_TOXCORE_TOXCORE_NET_CRYPTO_H
#define C_TOXCORE_TOXCORE_NET_CRYPTO_H
+#include <pthread.h>
+
#include "DHT.h"
#include "LAN_discovery.h"
#include "TCP_connection.h"
#include "logger.h"
-#include <pthread.h>
-
-/** Crypto payloads. */
+/*** Crypto payloads. */
-/** Ranges. */
+/*** Ranges. */
-/* Packets in this range are reserved for net_crypto internal use. */
+/** Packets in this range are reserved for net_crypto internal use. */
#define PACKET_ID_RANGE_RESERVED_START 0
#define PACKET_ID_RANGE_RESERVED_END 15
-/* Packets in this range are reserved for Messenger use. */
+/** Packets in this range are reserved for Messenger use. */
#define PACKET_ID_RANGE_LOSSLESS_START 16
#define PACKET_ID_RANGE_LOSSLESS_NORMAL_START 16
#define PACKET_ID_RANGE_LOSSLESS_NORMAL_END 159
-/* Packets in this range can be used for anything. */
+/** Packets in this range can be used for anything. */
#define PACKET_ID_RANGE_LOSSLESS_CUSTOM_START 160
#define PACKET_ID_RANGE_LOSSLESS_CUSTOM_END 191
#define PACKET_ID_RANGE_LOSSLESS_END 191
-/* Packets in this range are reserved for AV use. */
+/** Packets in this range are reserved for AV use. */
#define PACKET_ID_RANGE_LOSSY_START 192
#define PACKET_ID_RANGE_LOSSY_AV_START 192
#define PACKET_ID_RANGE_LOSSY_AV_SIZE 8
#define PACKET_ID_RANGE_LOSSY_AV_END 199
-/* Packets in this range can be used for anything. */
+/** Packets in this range can be used for anything. */
#define PACKET_ID_RANGE_LOSSY_CUSTOM_START 200
#define PACKET_ID_RANGE_LOSSY_CUSTOM_END 254
#define PACKET_ID_RANGE_LOSSY_END 254
-/** Messages. */
+/*** Messages. */
#define PACKET_ID_PADDING 0 // Denotes padding
#define PACKET_ID_REQUEST 1 // Used to request unreceived packets
@@ -66,45 +66,45 @@
#define PACKET_ID_REJOIN_CONFERENCE 100
#define PACKET_ID_LOSSY_CONFERENCE 199
-/* Maximum size of receiving and sending packet buffers. */
+/** Maximum size of receiving and sending packet buffers. */
#define CRYPTO_PACKET_BUFFER_SIZE 32768 // Must be a power of 2
-/* Minimum packet rate per second. */
+/** Minimum packet rate per second. */
#define CRYPTO_PACKET_MIN_RATE 4.0
-/* Minimum packet queue max length. */
+/** Minimum packet queue max length. */
#define CRYPTO_MIN_QUEUE_LENGTH 64
-/* Maximum total size of packets that net_crypto sends. */
+/** Maximum total size of packets that net_crypto sends. */
#define MAX_CRYPTO_PACKET_SIZE (uint16_t)1400
#define CRYPTO_DATA_PACKET_MIN_SIZE (uint16_t)(1 + sizeof(uint16_t) + (sizeof(uint32_t) + sizeof(uint32_t)) + CRYPTO_MAC_SIZE)
-/* Max size of data in packets */
+/** Max size of data in packets */
#define MAX_CRYPTO_DATA_SIZE (uint16_t)(MAX_CRYPTO_PACKET_SIZE - CRYPTO_DATA_PACKET_MIN_SIZE)
-/* Interval in ms between sending cookie request/handshake packets. */
+/** Interval in ms between sending cookie request/handshake packets. */
#define CRYPTO_SEND_PACKET_INTERVAL 1000
-/* The maximum number of times we try to send the cookie request and handshake
+/** The maximum number of times we try to send the cookie request and handshake
* before giving up. */
#define MAX_NUM_SENDPACKET_TRIES 8
-/* The timeout of no received UDP packets before the direct UDP connection is considered dead. */
+/** The timeout of no received UDP packets before the direct UDP connection is considered dead. */
#define UDP_DIRECT_TIMEOUT 8
#define MAX_TCP_CONNECTIONS 64
#define MAX_TCP_RELAYS_PEER 4
-/* All packets will be padded a number of bytes based on this number. */
+/** All packets will be padded a number of bytes based on this number. */
#define CRYPTO_MAX_PADDING 8
-/* Base current transfer speed on last CONGESTION_QUEUE_ARRAY_SIZE number of points taken
+/** Base current transfer speed on last CONGESTION_QUEUE_ARRAY_SIZE number of points taken
* at the dT defined in net_crypto.c */
#define CONGESTION_QUEUE_ARRAY_SIZE 12
#define CONGESTION_LAST_SENT_ARRAY_SIZE (CONGESTION_QUEUE_ARRAY_SIZE * 2)
-/* Default connection ping in ms. */
+/** Default connection ping in ms. */
#define DEFAULT_PING_CONNECTION 1000
#define DEFAULT_TCP_PING_CONNECTION 500
@@ -129,9 +129,9 @@ typedef int connection_status_cb(void *object, int id, uint8_t status, void *use
typedef int connection_data_cb(void *object, int id, const uint8_t *data, uint16_t length, void *userdata);
typedef int connection_lossy_data_cb(void *object, int id, const uint8_t *data, uint16_t length, void *userdata);
typedef void dht_pk_cb(void *data, int32_t number, const uint8_t *dht_public_key, void *userdata);
-typedef int new_connection_cb(void *object, New_Connection *n_c);
+typedef int new_connection_cb(void *object, const New_Connection *n_c);
-/* Set function to be called when someone requests a new connection to us.
+/** Set function to be called when someone requests a new connection to us.
*
* The set function should return -1 on failure and 0 on success.
*
@@ -139,14 +139,14 @@ typedef int new_connection_cb(void *object, New_Connection *n_c);
*/
void new_connection_handler(Net_Crypto *c, new_connection_cb *new_connection_callback, void *object);
-/* Accept a crypto connection.
+/** Accept a crypto connection.
*
* return -1 on failure.
* return connection id on success.
*/
-int accept_crypto_connection(Net_Crypto *c, New_Connection *n_c);
+int accept_crypto_connection(Net_Crypto *c, const New_Connection *n_c);
-/* Create a crypto connection.
+/** Create a crypto connection.
* If one to that real public key already exists, return it.
*
* return -1 on failure.
@@ -154,16 +154,16 @@ int accept_crypto_connection(Net_Crypto *c, New_Connection *n_c);
*/
int new_crypto_connection(Net_Crypto *c, const uint8_t *real_public_key, const uint8_t *dht_public_key);
-/* Set the direct ip of the crypto connection.
+/** Set the direct ip of the crypto connection.
*
* Connected is 0 if we are not sure we are connected to that person, 1 if we are sure.
*
* return -1 on failure.
* return 0 on success.
*/
-int set_direct_ip_port(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port, bool connected);
+int set_direct_ip_port(Net_Crypto *c, int crypt_connection_id, const IP_Port *ip_port, bool connected);
-/* Set function to be called when connection with crypt_connection_id goes connects/disconnects.
+/** Set function to be called when connection with crypt_connection_id goes connects/disconnects.
*
* The set function should return -1 on failure and 0 on success.
* Note that if this function is set, the connection will clear itself on disconnect.
@@ -176,7 +176,7 @@ int set_direct_ip_port(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port,
int connection_status_handler(const Net_Crypto *c, int crypt_connection_id,
connection_status_cb *connection_status_callback, void *object, int id);
-/* Set function to be called when connection with crypt_connection_id receives a lossless data packet of length.
+/** Set function to be called when connection with crypt_connection_id receives a lossless data packet of length.
*
* The set function should return -1 on failure and 0 on success.
* Object and id will be passed to this function untouched.
@@ -188,7 +188,7 @@ int connection_data_handler(const Net_Crypto *c, int crypt_connection_id,
connection_data_cb *connection_data_callback, void *object, int id);
-/* Set function to be called when connection with crypt_connection_id receives a lossy data packet of length.
+/** Set function to be called when connection with crypt_connection_id receives a lossy data packet of length.
*
* The set function should return -1 on failure and 0 on success.
* Object and id will be passed to this function untouched.
@@ -196,10 +196,10 @@ int connection_data_handler(const Net_Crypto *c, int crypt_connection_id,
* return -1 on failure.
* return 0 on success.
*/
-int connection_lossy_data_handler(Net_Crypto *c, int crypt_connection_id,
+int connection_lossy_data_handler(const Net_Crypto *c, int crypt_connection_id,
connection_lossy_data_cb *connection_lossy_data_callback, void *object, int id);
-/* Set the function for this friend that will be callbacked with object and number if
+/** Set the function for this friend that will be callbacked with object and number if
* the friend sends us a different dht public key than we have associated to him.
*
* If this function is called, the connection should be recreated with the new public key.
@@ -209,19 +209,20 @@ int connection_lossy_data_handler(Net_Crypto *c, int crypt_connection_id,
* return -1 on failure.
* return 0 on success.
*/
-int nc_dht_pk_callback(Net_Crypto *c, int crypt_connection_id, dht_pk_cb *function, void *object, uint32_t number);
+int nc_dht_pk_callback(const Net_Crypto *c, int crypt_connection_id, dht_pk_cb *function, void *object,
+ uint32_t number);
-/* returns the number of packet slots left in the sendbuffer.
+/** returns the number of packet slots left in the sendbuffer.
* return 0 if failure.
*/
uint32_t crypto_num_free_sendqueue_slots(const Net_Crypto *c, int crypt_connection_id);
-/* Return 1 if max speed was reached for this connection (no more data can be physically through the pipe).
+/** Return 1 if max speed was reached for this connection (no more data can be physically through the pipe).
* Return 0 if it wasn't reached.
*/
bool max_speed_reached(Net_Crypto *c, int crypt_connection_id);
-/* Sends a lossless cryptopacket.
+/** Sends a lossless cryptopacket.
*
* return -1 if data could not be put in packet queue.
* return positive packet number if data was put into the queue.
@@ -233,16 +234,23 @@ bool max_speed_reached(Net_Crypto *c, int crypt_connection_id);
int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length,
uint8_t congestion_control);
-/* Check if packet_number was received by the other side.
+/** Check if packet_number was received by the other side.
*
* packet_number must be a valid packet number of a packet sent on this connection.
*
* return -1 on failure.
* return 0 on success.
+ *
+ * Note: The condition `buffer_end - buffer_start < packet_number - buffer_start` is
+ * a trick which handles situations `buffer_end >= buffer_start` and
+ * `buffer_end < buffer_start` (when buffer_end overflowed) both correctly.
+ *
+ * It CANNOT be simplified to `packet_number < buffer_start`, as it will fail
+ * when `buffer_end < buffer_start`.
*/
-int cryptpacket_received(Net_Crypto *c, int crypt_connection_id, uint32_t packet_number);
+int cryptpacket_received(const Net_Crypto *c, int crypt_connection_id, uint32_t packet_number);
-/* Sends a lossy cryptopacket.
+/** Sends a lossy cryptopacket.
*
* return -1 on failure.
* return 0 on success.
@@ -251,35 +259,38 @@ int cryptpacket_received(Net_Crypto *c, int crypt_connection_id, uint32_t packet
*/
int send_lossy_cryptpacket(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length);
-/* Add a tcp relay, associating it to a crypt_connection_id.
+/** Add a tcp relay, associating it to a crypt_connection_id.
*
* return 0 if it was added.
* return -1 if it wasn't.
*/
-int add_tcp_relay_peer(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port, const uint8_t *public_key);
+int add_tcp_relay_peer(Net_Crypto *c, int crypt_connection_id, const IP_Port *ip_port, const uint8_t *public_key);
-/* Add a tcp relay to the array.
+/** Add a tcp relay to the array.
*
* return 0 if it was added.
* return -1 if it wasn't.
*/
-int add_tcp_relay(Net_Crypto *c, IP_Port ip_port, const uint8_t *public_key);
+int add_tcp_relay(Net_Crypto *c, const IP_Port *ip_port, const uint8_t *public_key);
-/* Return a random TCP connection number for use in send_tcp_onion_request.
+/** Return a random TCP connection number for use in send_tcp_onion_request.
+ *
+ * TODO(irungentoo): This number is just the index of an array that the elements can
+ * change without warning.
*
* return TCP connection number on success.
* return -1 on failure.
*/
int get_random_tcp_con_number(Net_Crypto *c);
-/* Send an onion packet via the TCP relay corresponding to TCP_conn_number.
+/** Send an onion packet via the TCP relay corresponding to tcp_connections_number.
*
* return 0 on success.
* return -1 on failure.
*/
int send_tcp_onion_request(Net_Crypto *c, unsigned int tcp_connections_number, const uint8_t *data, uint16_t length);
-/* Copy a maximum of num TCP relays we are connected to to tcp_relays.
+/** Copy a maximum of num TCP relays we are connected to to tcp_relays.
* NOTE that the family of the copied ip ports will be set to TCP_INET or TCP_INET6.
*
* return number of relays copied to tcp_relays on success.
@@ -287,14 +298,14 @@ int send_tcp_onion_request(Net_Crypto *c, unsigned int tcp_connections_number, c
*/
unsigned int copy_connected_tcp_relays(Net_Crypto *c, Node_format *tcp_relays, uint16_t num);
-/* Kill a crypto connection.
+/** Kill a crypto connection.
*
* return -1 on failure.
* return 0 on success.
*/
int crypto_kill(Net_Crypto *c, int crypt_connection_id);
-/* return true if connection is valid, false otherwise
+/** return true if connection is valid, false otherwise
*
* sets direct_connected to 1 if connection connects directly to other, 0 if it isn't.
* sets online_tcp_relays to the number of connected tcp relays this connection has.
@@ -302,31 +313,33 @@ int crypto_kill(Net_Crypto *c, int crypt_connection_id);
bool crypto_connection_status(const Net_Crypto *c, int crypt_connection_id, bool *direct_connected,
unsigned int *online_tcp_relays);
-/* Generate our public and private keys.
+/** Generate our public and private keys.
* Only call this function the first time the program starts.
*/
void new_keys(Net_Crypto *c);
-/* Save the public and private keys to the keys array.
+/** Save the public and private keys to the keys array.
* Length must be CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_SECRET_KEY_SIZE.
+ *
+ * TODO(irungentoo): Save only secret key.
*/
void save_keys(const Net_Crypto *c, uint8_t *keys);
-/* Load the secret key.
+/** Load the secret key.
* Length must be CRYPTO_SECRET_KEY_SIZE.
*/
void load_secret_key(Net_Crypto *c, const uint8_t *sk);
-/* Create new instance of Net_Crypto.
+/** Create new instance of Net_Crypto.
* Sets all the global connection variables to their default values.
*/
-Net_Crypto *new_net_crypto(const Logger *log, Mono_Time *mono_time, DHT *dht, TCP_Proxy_Info *proxy_info);
+Net_Crypto *new_net_crypto(const Logger *log, Mono_Time *mono_time, DHT *dht, const TCP_Proxy_Info *proxy_info);
-/* return the optimal interval in ms for running do_net_crypto.
+/** return the optimal interval in ms for running do_net_crypto.
*/
uint32_t crypto_run_interval(const Net_Crypto *c);
-/* Main loop. */
+/** Main loop. */
void do_net_crypto(Net_Crypto *c, void *userdata);
void kill_net_crypto(Net_Crypto *c);