summaryrefslogtreecommitdiff
path: root/toxcore/onion_client.h
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/onion_client.h')
-rw-r--r--toxcore/onion_client.h94
1 files changed, 63 insertions, 31 deletions
diff --git a/toxcore/onion_client.h b/toxcore/onion_client.h
index cf0975d3..6851d929 100644
--- a/toxcore/onion_client.h
+++ b/toxcore/onion_client.h
@@ -29,28 +29,38 @@
29#include "ping_array.h" 29#include "ping_array.h"
30 30
31#define MAX_ONION_CLIENTS 8 31#define MAX_ONION_CLIENTS 8
32#define ONION_NODE_PING_INTERVAL 30 32#define ONION_NODE_PING_INTERVAL 20
33#define ONION_NODE_TIMEOUT (ONION_NODE_PING_INTERVAL * 4) 33#define ONION_NODE_TIMEOUT (ONION_NODE_PING_INTERVAL * 3)
34 34
35/* The interval in seconds at which to tell our friends where we are */ 35/* The interval in seconds at which to tell our friends where we are */
36#define ONION_FAKEID_INTERVAL 30 36#define ONION_DHTPK_SEND_INTERVAL 30
37#define DHT_FAKEID_INTERVAL 20 37#define DHT_DHTPK_SEND_INTERVAL 20
38 38
39#define NUMBER_ONION_PATHS 3 39#define NUMBER_ONION_PATHS 6
40 40
41/* The timeout the first time the path is added and 41/* The timeout the first time the path is added and
42 then for all the next consecutive times */ 42 then for all the next consecutive times */
43#define ONION_PATH_FIRST_TIMEOUT 5 43#define ONION_PATH_FIRST_TIMEOUT 4
44#define ONION_PATH_TIMEOUT 30 44#define ONION_PATH_TIMEOUT 10
45#define ONION_PATH_MAX_LIFETIME 600 45#define ONION_PATH_MAX_LIFETIME 1200
46#define ONION_PATH_MAX_NO_RESPONSE_USES 4
46 47
47#define MAX_STORED_PINGED_NODES 9 48#define MAX_STORED_PINGED_NODES 9
48#define MIN_NODE_PING_TIME 10 49#define MIN_NODE_PING_TIME 10
49 50
50#define MAX_PATH_NODES 32 51#define MAX_PATH_NODES 32
51 52
53/* If no packets are received within that interval tox will
54 * be considered offline.
55 */
56#define ONION_OFFLINE_TIMEOUT (ONION_NODE_PING_INTERVAL * 1.25)
57
58/* Onion data packet ids. */
59#define ONION_DATA_FRIEND_REQ CRYPTO_PACKET_FRIEND_REQ
60#define ONION_DATA_DHTPK CRYPTO_PACKET_DHTPK
61
52typedef struct { 62typedef struct {
53 uint8_t client_id[CLIENT_ID_SIZE]; 63 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
54 IP_Port ip_port; 64 IP_Port ip_port;
55 uint8_t ping_id[ONION_PING_ID_SIZE]; 65 uint8_t ping_id[ONION_PING_ID_SIZE];
56 uint8_t data_public_key[crypto_box_PUBLICKEYBYTES]; 66 uint8_t data_public_key[crypto_box_PUBLICKEYBYTES];
@@ -66,11 +76,14 @@ typedef struct {
66typedef struct { 76typedef struct {
67 Onion_Path paths[NUMBER_ONION_PATHS]; 77 Onion_Path paths[NUMBER_ONION_PATHS];
68 uint64_t last_path_success[NUMBER_ONION_PATHS]; 78 uint64_t last_path_success[NUMBER_ONION_PATHS];
79 uint64_t last_path_used[NUMBER_ONION_PATHS];
69 uint64_t path_creation_time[NUMBER_ONION_PATHS]; 80 uint64_t path_creation_time[NUMBER_ONION_PATHS];
81 /* number of times used without success. */
82 unsigned int last_path_used_times[NUMBER_ONION_PATHS];
70} Onion_Client_Paths; 83} Onion_Client_Paths;
71 84
72typedef struct { 85typedef struct {
73 uint8_t client_id[CLIENT_ID_SIZE]; 86 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
74 uint64_t timestamp; 87 uint64_t timestamp;
75} Last_Pinged; 88} Last_Pinged;
76 89
@@ -78,24 +91,21 @@ typedef struct {
78 uint8_t status; /* 0 if friend is not valid, 1 if friend is valid.*/ 91 uint8_t status; /* 0 if friend is not valid, 1 if friend is valid.*/
79 uint8_t is_online; /* Set by the onion_set_friend_status function. */ 92 uint8_t is_online; /* Set by the onion_set_friend_status function. */
80 93
81 uint8_t is_fake_clientid; /* 0 if we don't know the fake client id of the other 1 if we do. */ 94 uint8_t know_dht_public_key; /* 0 if we don't know the dht public key of the other, 1 if we do. */
82 uint64_t fake_client_id_timestamp; 95 uint8_t dht_public_key[crypto_box_PUBLICKEYBYTES];
83 uint8_t fake_client_id[crypto_box_PUBLICKEYBYTES]; 96 uint8_t real_public_key[crypto_box_PUBLICKEYBYTES];
84 uint8_t real_client_id[crypto_box_PUBLICKEYBYTES];
85 97
86 Onion_Node clients_list[MAX_ONION_CLIENTS]; 98 Onion_Node clients_list[MAX_ONION_CLIENTS];
87 uint8_t temp_public_key[crypto_box_PUBLICKEYBYTES]; 99 uint8_t temp_public_key[crypto_box_PUBLICKEYBYTES];
88 uint8_t temp_secret_key[crypto_box_SECRETKEYBYTES]; 100 uint8_t temp_secret_key[crypto_box_SECRETKEYBYTES];
89 101
90 uint64_t last_fakeid_onion_sent; 102 uint64_t last_dht_pk_onion_sent;
91 uint64_t last_fakeid_dht_sent; 103 uint64_t last_dht_pk_dht_sent;
92 104
93 uint64_t last_noreplay; 105 uint64_t last_noreplay;
94 106
95 uint64_t last_seen; 107 uint64_t last_seen;
96 108
97 Onion_Client_Paths onion_paths;
98
99 Last_Pinged last_pinged[MAX_STORED_PINGED_NODES]; 109 Last_Pinged last_pinged[MAX_STORED_PINGED_NODES];
100 uint8_t last_pinged_index; 110 uint8_t last_pinged_index;
101 111
@@ -103,11 +113,15 @@ typedef struct {
103 void *tcp_relay_node_callback_object; 113 void *tcp_relay_node_callback_object;
104 uint32_t tcp_relay_node_callback_number; 114 uint32_t tcp_relay_node_callback_number;
105 115
116 void (*dht_pk_callback)(void *data, int32_t number, const uint8_t *dht_public_key);
117 void *dht_pk_callback_object;
118 uint32_t dht_pk_callback_number;
119
106 uint32_t run_count; 120 uint32_t run_count;
107} Onion_Friend; 121} Onion_Friend;
108 122
109typedef int (*oniondata_handler_callback)(void *object, const uint8_t *source_pubkey, const uint8_t *data, 123typedef int (*oniondata_handler_callback)(void *object, const uint8_t *source_pubkey, const uint8_t *data,
110 uint32_t len); 124 uint16_t len);
111 125
112typedef struct { 126typedef struct {
113 DHT *dht; 127 DHT *dht;
@@ -118,7 +132,8 @@ typedef struct {
118 132
119 Onion_Node clients_announce_list[MAX_ONION_CLIENTS]; 133 Onion_Node clients_announce_list[MAX_ONION_CLIENTS];
120 134
121 Onion_Client_Paths onion_paths; 135 Onion_Client_Paths onion_paths_self;
136 Onion_Client_Paths onion_paths_friends;
122 137
123 uint8_t secret_symmetric_key[crypto_box_KEYBYTES]; 138 uint8_t secret_symmetric_key[crypto_box_KEYBYTES];
124 uint64_t last_run; 139 uint64_t last_run;
@@ -131,21 +146,26 @@ typedef struct {
131 Node_format path_nodes[MAX_PATH_NODES]; 146 Node_format path_nodes[MAX_PATH_NODES];
132 uint16_t path_nodes_index; 147 uint16_t path_nodes_index;
133 148
149 Node_format path_nodes_bs[MAX_PATH_NODES];
150 uint16_t path_nodes_index_bs;
151
134 Ping_Array announce_ping_array; 152 Ping_Array announce_ping_array;
135 uint8_t last_pinged_index; 153 uint8_t last_pinged_index;
136 struct { 154 struct {
137 oniondata_handler_callback function; 155 oniondata_handler_callback function;
138 void *object; 156 void *object;
139 } Onion_Data_Handlers[256]; 157 } Onion_Data_Handlers[256];
158
159 uint64_t last_packet_recv;
140} Onion_Client; 160} Onion_Client;
141 161
142 162
143/* Add a node to the path_nodes array. 163/* Add a node to the path_nodes bootstrap array.
144 * 164 *
145 * return -1 on failure 165 * return -1 on failure
146 * return 0 on success 166 * return 0 on success
147 */ 167 */
148int onion_add_path_node(Onion_Client *onion_c, IP_Port ip_port, const uint8_t *client_id); 168int onion_add_bs_path_node(Onion_Client *onion_c, IP_Port ip_port, const uint8_t *public_key);
149 169
150/* Put up to max_num nodes in nodes. 170/* Put up to max_num nodes in nodes.
151 * 171 *
@@ -158,14 +178,14 @@ uint16_t onion_backup_nodes(const Onion_Client *onion_c, Node_format *nodes, uin
158 * return -1 on failure. 178 * return -1 on failure.
159 * return the friend number on success or if the friend was already added. 179 * return the friend number on success or if the friend was already added.
160 */ 180 */
161int onion_friend_num(const Onion_Client *onion_c, const uint8_t *client_id); 181int onion_friend_num(const Onion_Client *onion_c, const uint8_t *public_key);
162 182
163/* Add a friend who we want to connect to. 183/* Add a friend who we want to connect to.
164 * 184 *
165 * return -1 on failure. 185 * return -1 on failure.
166 * return the friend number on success. 186 * return the friend number on success.
167 */ 187 */
168int onion_addfriend(Onion_Client *onion_c, const uint8_t *client_id); 188int onion_addfriend(Onion_Client *onion_c, const uint8_t *public_key);
169 189
170/* Delete a friend. 190/* Delete a friend.
171 * 191 *
@@ -187,9 +207,9 @@ int onion_set_friend_online(Onion_Client *onion_c, int friend_num, uint8_t is_on
187 207
188/* Get the ip of friend friendnum and put it in ip_port 208/* Get the ip of friend friendnum and put it in ip_port
189 * 209 *
190 * return -1, -- if client_id does NOT refer to a friend 210 * return -1, -- if public_key does NOT refer to a friend
191 * return 0, -- if client_id refers to a friend and we failed to find the friend (yet) 211 * return 0, -- if public_key refers to a friend and we failed to find the friend (yet)
192 * return 1, ip if client_id refers to a friend and we found him 212 * return 1, ip if public_key refers to a friend and we found him
193 * 213 *
194 */ 214 */
195int onion_getfriendip(const Onion_Client *onion_c, int friend_num, IP_Port *ip_port); 215int onion_getfriendip(const Onion_Client *onion_c, int friend_num, IP_Port *ip_port);
@@ -205,6 +225,18 @@ int onion_getfriendip(const Onion_Client *onion_c, int friend_num, IP_Port *ip_p
205int recv_tcp_relay_handler(Onion_Client *onion_c, int friend_num, int (*tcp_relay_node_callback)(void *object, 225int recv_tcp_relay_handler(Onion_Client *onion_c, int friend_num, int (*tcp_relay_node_callback)(void *object,
206 uint32_t number, IP_Port ip_port, const uint8_t *public_key), void *object, uint32_t number); 226 uint32_t number, IP_Port ip_port, const uint8_t *public_key), void *object, uint32_t number);
207 227
228
229/* Set the function for this friend that will be callbacked with object and number
230 * when that friend gives us his DHT temporary public key.
231 *
232 * object and number will be passed as argument to this function.
233 *
234 * return -1 on failure.
235 * return 0 on success.
236 */
237int onion_dht_pk_callback(Onion_Client *onion_c, int friend_num, void (*function)(void *data, int32_t number,
238 const uint8_t *dht_public_key), void *object, uint32_t number);
239
208/* Set a friends DHT public key. 240/* Set a friends DHT public key.
209 * timestamp is the time (current_time_monotonic()) at which the key was last confirmed belonging to 241 * timestamp is the time (current_time_monotonic()) at which the key was last confirmed belonging to
210 * the other peer. 242 * the other peer.
@@ -212,14 +244,14 @@ int recv_tcp_relay_handler(Onion_Client *onion_c, int friend_num, int (*tcp_rela
212 * return -1 on failure. 244 * return -1 on failure.
213 * return 0 on success. 245 * return 0 on success.
214 */ 246 */
215int onion_set_friend_DHT_pubkey(Onion_Client *onion_c, int friend_num, const uint8_t *dht_key, uint64_t timestamp); 247int onion_set_friend_DHT_pubkey(Onion_Client *onion_c, int friend_num, const uint8_t *dht_key);
216 248
217/* Copy friends DHT public key into dht_key. 249/* Copy friends DHT public key into dht_key.
218 * 250 *
219 * return 0 on failure (no key copied). 251 * return 0 on failure (no key copied).
220 * return timestamp on success (key copied). 252 * return 1 on success (key copied).
221 */ 253 */
222uint64_t onion_getfriend_DHT_pubkey(const Onion_Client *onion_c, int friend_num, uint8_t *dht_key); 254unsigned int onion_getfriend_DHT_pubkey(const Onion_Client *onion_c, int friend_num, uint8_t *dht_key);
223 255
224#define ONION_DATA_IN_RESPONSE_MIN_SIZE (crypto_box_PUBLICKEYBYTES + crypto_box_MACBYTES) 256#define ONION_DATA_IN_RESPONSE_MIN_SIZE (crypto_box_PUBLICKEYBYTES + crypto_box_MACBYTES)
225#define ONION_CLIENT_MAX_DATA_SIZE (MAX_DATA_REQUEST_SIZE - ONION_DATA_IN_RESPONSE_MIN_SIZE) 257#define ONION_CLIENT_MAX_DATA_SIZE (MAX_DATA_REQUEST_SIZE - ONION_DATA_IN_RESPONSE_MIN_SIZE)
@@ -233,7 +265,7 @@ uint64_t onion_getfriend_DHT_pubkey(const Onion_Client *onion_c, int friend_num,
233 * return the number of packets sent on success 265 * return the number of packets sent on success
234 * return -1 on failure. 266 * return -1 on failure.
235 */ 267 */
236int send_onion_data(const Onion_Client *onion_c, int friend_num, const uint8_t *data, uint32_t length); 268int send_onion_data(Onion_Client *onion_c, int friend_num, const uint8_t *data, uint16_t length);
237 269
238/* Function to call when onion data packet with contents beginning with byte is received. */ 270/* Function to call when onion data packet with contents beginning with byte is received. */
239void oniondata_registerhandler(Onion_Client *onion_c, uint8_t byte, oniondata_handler_callback cb, void *object); 271void oniondata_registerhandler(Onion_Client *onion_c, uint8_t byte, oniondata_handler_callback cb, void *object);