summaryrefslogtreecommitdiff
path: root/toxcore/net_crypto.h
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/net_crypto.h')
-rw-r--r--toxcore/net_crypto.h74
1 files changed, 60 insertions, 14 deletions
diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h
index 3d58e5c1..5d4ff35b 100644
--- a/toxcore/net_crypto.h
+++ b/toxcore/net_crypto.h
@@ -44,6 +44,9 @@
44 44
45/* Interval in ms between sending cookie request/handshake packets. */ 45/* Interval in ms between sending cookie request/handshake packets. */
46#define CRYPTO_SEND_PACKET_INTERVAL 500 46#define CRYPTO_SEND_PACKET_INTERVAL 500
47/* The maximum number of times we try to send the cookie request and handshake
48 before giving up. */
49#define MAX_NUM_SENDPACKET_TRIES 10
47 50
48typedef struct { 51typedef struct {
49 uint8_t public_key[crypto_box_PUBLICKEYBYTES]; /* The real public key of the peer. */ 52 uint8_t public_key[crypto_box_PUBLICKEYBYTES]; /* The real public key of the peer. */
@@ -59,8 +62,6 @@ typedef struct {
59 * 4 if the connection is established. 62 * 4 if the connection is established.
60 * 5 if the connection is timed out. 63 * 5 if the connection is timed out.
61 */ 64 */
62 uint64_t timeout;
63
64 uint64_t cookie_request_number; /* number used in the cookie request packets for this connection */ 65 uint64_t cookie_request_number; /* number used in the cookie request packets for this connection */
65 uint8_t dht_public_key[crypto_box_PUBLICKEYBYTES]; /* The dht public key of the peer */ 66 uint8_t dht_public_key[crypto_box_PUBLICKEYBYTES]; /* The dht public key of the peer */
66 uint8_t dht_public_key_set; /* True if the dht public key is set, false if it isn't. */ 67 uint8_t dht_public_key_set; /* True if the dht public key is set, false if it isn't. */
@@ -68,9 +69,20 @@ typedef struct {
68 uint8_t *temp_packet; /* Where the cookie request/handshake packet is stored while it is being sent. */ 69 uint8_t *temp_packet; /* Where the cookie request/handshake packet is stored while it is being sent. */
69 uint16_t temp_packet_length; 70 uint16_t temp_packet_length;
70 uint64_t temp_packet_sent_time; /* The time at which the last temp_packet was sent in ms. */ 71 uint64_t temp_packet_sent_time; /* The time at which the last temp_packet was sent in ms. */
72 uint32_t temp_packet_num_sent;
71 73
72 IP_Port ip_port; /* The ip and port to contact this guy directly.*/ 74 IP_Port ip_port; /* The ip and port to contact this guy directly.*/
73 uint64_t direct_lastrecv_time; /* The Time at which we last receive a direct packet. */ 75 uint64_t direct_lastrecv_time; /* The Time at which we last receive a direct packet. */
76
77 int (*connection_status_callback)(void *object, int id, uint8_t status);
78 void *connection_status_callback_object;
79 int connection_status_callback_id;
80
81 int (*connection_data_callback)(void *object, int id, uint8_t *data, uint16_t length);
82 void *connection_data_callback_object;
83 int connection_data_callback_id;
84
85 uint64_t last_data_packet_sent;
74} Crypto_Connection; 86} Crypto_Connection;
75 87
76typedef struct { 88typedef struct {
@@ -117,12 +129,52 @@ void new_connection_handler(Net_Crypto *c, int (*new_connection_callback)(void *
117 */ 129 */
118int accept_crypto_connection(Net_Crypto *c, New_Connection *n_c); 130int accept_crypto_connection(Net_Crypto *c, New_Connection *n_c);
119 131
132/* Create a crypto connection.
133 * If one to that real public key already exists, return it.
134 *
135 * return -1 on failure.
136 * return connection id on success.
137 */
138int new_crypto_connection(Net_Crypto *c, uint8_t *real_public_key);
139
140/* Set the DHT public key of the crypto connection.
141 *
142 * return -1 on failure.
143 * return 0 on success.
144 */
145int set_conection_dht_public_key(Net_Crypto *c, int crypt_connection_id, uint8_t *dht_public_key);
146
147/* Set the direct ip of the crypto connection.
148 *
149 * return -1 on failure.
150 * return 0 on success.
151 */
152int set_direct_ip_port(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port);
153
154/* Set function to be called when connection with crypt_connection_id goes connects/disconnects.
155 *
156 * The set function should return -1 on failure and 0 on success.
157 * Note that if this function is set, the connection will clear itself on disconnect.
158 * Object and id will be passed to this function untouched.
159 * status is 1 if the connection is going online, 0 if it is going offline.
160 *
161 * return -1 on failure.
162 * return 0 on success.
163 */
164int connection_status_handler(Net_Crypto *c, int crypt_connection_id, int (*connection_status_callback)(void *object,
165 int id, uint8_t status), void *object, int id);
120 166
121/* return 0 if there is no received data in the buffer. 167/* Set function to be called when connection with crypt_connection_id receives a data packet of length.
122 * return -1 if the packet was discarded. 168 *
123 * return length of received data if successful. 169 * The set function should return -1 on failure and 0 on success.
170 * Object and id will be passed to this function untouched.
171 *
172 * return -1 on failure.
173 * return 0 on success.
124 */ 174 */
125int read_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data); 175int connection_data_handler(Net_Crypto *c, int crypt_connection_id, int (*connection_data_callback)(void *object,
176 int id, uint8_t *data, uint16_t length), void *object, int id);
177
126 178
127/* returns the number of packet slots left in the sendbuffer. 179/* returns the number of packet slots left in the sendbuffer.
128 * return 0 if failure. 180 * return 0 if failure.
@@ -134,17 +186,11 @@ uint32_t crypto_num_free_sendqueue_slots(Net_Crypto *c, int crypt_connection_id)
134 */ 186 */
135int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length); 187int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length);
136 188
137/* Start a secure connection with other peer who has public_key and ip_port.
138 *
139 * return -1 if failure.
140 * return crypt_connection_id of the initialized connection if everything went well.
141 */
142int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port);
143 189
144/* Kill a crypto connection. 190/* Kill a crypto connection.
145 * 191 *
146 * return 0 if killed successfully. 192 * return -1 on failure.
147 * return 1 if there was a problem. 193 * return 0 on success.
148 */ 194 */
149int crypto_kill(Net_Crypto *c, int crypt_connection_id); 195int crypto_kill(Net_Crypto *c, int crypt_connection_id);
150 196