diff options
-rw-r--r-- | toxcore/onion_client.c | 27 | ||||
-rw-r--r-- | toxcore/onion_client.h | 14 |
2 files changed, 39 insertions, 2 deletions
diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index db6bd39a..329b1d13 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c | |||
@@ -291,6 +291,22 @@ static int client_add_to_list(Onion_Client *onion_c, uint32_t num, uint8_t *publ | |||
291 | return 0; | 291 | return 0; |
292 | } | 292 | } |
293 | 293 | ||
294 | static int good_to_ping(Last_Pinged *last_pinged, uint8_t *last_pinged_index, uint8_t *client_id) | ||
295 | { | ||
296 | uint32_t i; | ||
297 | |||
298 | for (i = 0; i < MAX_STORED_PINGED_NODES; ++i) { | ||
299 | if (!is_timeout(last_pinged[i].timestamp, MIN_NODE_PING_TIME)) | ||
300 | if (memcmp(last_pinged[i].client_id, client_id, crypto_box_PUBLICKEYBYTES) == 0) | ||
301 | return 0; | ||
302 | } | ||
303 | |||
304 | memcpy(last_pinged[*last_pinged_index % MAX_STORED_PINGED_NODES].client_id, client_id, crypto_box_PUBLICKEYBYTES); | ||
305 | last_pinged[*last_pinged_index % MAX_STORED_PINGED_NODES].timestamp = unix_time(); | ||
306 | ++*last_pinged_index; | ||
307 | return 1; | ||
308 | } | ||
309 | |||
294 | static int client_ping_nodes(Onion_Client *onion_c, uint32_t num, Node_format *nodes, uint16_t num_nodes, | 310 | static int client_ping_nodes(Onion_Client *onion_c, uint32_t num, Node_format *nodes, uint16_t num_nodes, |
295 | IP_Port source) | 311 | IP_Port source) |
296 | { | 312 | { |
@@ -304,14 +320,21 @@ static int client_ping_nodes(Onion_Client *onion_c, uint32_t num, Node_format *n | |||
304 | uint8_t *reference_id = NULL; | 320 | uint8_t *reference_id = NULL; |
305 | uint32_t *ping_nodes_sent_second = NULL; | 321 | uint32_t *ping_nodes_sent_second = NULL; |
306 | 322 | ||
323 | Last_Pinged *last_pinged = NULL; | ||
324 | uint8_t *last_pinged_index = NULL; | ||
325 | |||
307 | if (num == 0) { | 326 | if (num == 0) { |
308 | list_nodes = onion_c->clients_announce_list; | 327 | list_nodes = onion_c->clients_announce_list; |
309 | reference_id = onion_c->dht->c->self_public_key; | 328 | reference_id = onion_c->dht->c->self_public_key; |
310 | ping_nodes_sent_second = &onion_c->ping_nodes_sent_second; | 329 | ping_nodes_sent_second = &onion_c->ping_nodes_sent_second; |
330 | last_pinged = onion_c->last_pinged; | ||
331 | last_pinged_index = &onion_c->last_pinged_index; | ||
311 | } else { | 332 | } else { |
312 | list_nodes = onion_c->friends_list[num - 1].clients_list; | 333 | list_nodes = onion_c->friends_list[num - 1].clients_list; |
313 | reference_id = onion_c->friends_list[num - 1].real_client_id; | 334 | reference_id = onion_c->friends_list[num - 1].real_client_id; |
314 | ping_nodes_sent_second = &onion_c->friends_list[num - 1].ping_nodes_sent_second; | 335 | ping_nodes_sent_second = &onion_c->friends_list[num - 1].ping_nodes_sent_second; |
336 | last_pinged = onion_c->friends_list[num - 1].last_pinged; | ||
337 | last_pinged_index = &onion_c->friends_list[num - 1].last_pinged_index; | ||
315 | } | 338 | } |
316 | 339 | ||
317 | uint32_t i, j; | 340 | uint32_t i, j; |
@@ -337,7 +360,7 @@ static int client_ping_nodes(Onion_Client *onion_c, uint32_t num, Node_format *n | |||
337 | } | 360 | } |
338 | } | 361 | } |
339 | 362 | ||
340 | if (j == MAX_ONION_CLIENTS) { | 363 | if (j == MAX_ONION_CLIENTS && good_to_ping(last_pinged, last_pinged_index, nodes[i].client_id)) { |
341 | if (client_send_announce_request(onion_c, num, nodes[i].ip_port, nodes[i].client_id, NULL, ~0) == 0) | 364 | if (client_send_announce_request(onion_c, num, nodes[i].ip_port, nodes[i].client_id, NULL, ~0) == 0) |
342 | ++*ping_nodes_sent_second; | 365 | ++*ping_nodes_sent_second; |
343 | } | 366 | } |
@@ -888,7 +911,7 @@ void oniondata_registerhandler(Onion_Client *onion_c, uint8_t byte, oniondata_ha | |||
888 | onion_c->Onion_Data_Handlers[byte].object = object; | 911 | onion_c->Onion_Data_Handlers[byte].object = object; |
889 | } | 912 | } |
890 | 913 | ||
891 | #define ANNOUNCE_INTERVAL_NOT_ANNOUNCED 7 | 914 | #define ANNOUNCE_INTERVAL_NOT_ANNOUNCED 10 |
892 | #define ANNOUNCE_INTERVAL_ANNOUNCED ONION_NODE_PING_INTERVAL | 915 | #define ANNOUNCE_INTERVAL_ANNOUNCED ONION_NODE_PING_INTERVAL |
893 | 916 | ||
894 | static void do_announce(Onion_Client *onion_c) | 917 | static void do_announce(Onion_Client *onion_c) |
diff --git a/toxcore/onion_client.h b/toxcore/onion_client.h index 684c03cc..4045cc0e 100644 --- a/toxcore/onion_client.h +++ b/toxcore/onion_client.h | |||
@@ -47,6 +47,9 @@ | |||
47 | second per peer. */ | 47 | second per peer. */ |
48 | #define MAX_PING_NODES_SECOND_PEER 5 | 48 | #define MAX_PING_NODES_SECOND_PEER 5 |
49 | 49 | ||
50 | #define MAX_STORED_PINGED_NODES 9 | ||
51 | #define MIN_NODE_PING_TIME 10 | ||
52 | |||
50 | typedef struct { | 53 | typedef struct { |
51 | uint8_t client_id[CLIENT_ID_SIZE]; | 54 | uint8_t client_id[CLIENT_ID_SIZE]; |
52 | IP_Port ip_port; | 55 | IP_Port ip_port; |
@@ -68,6 +71,11 @@ typedef struct { | |||
68 | } Onion_Client_Paths; | 71 | } Onion_Client_Paths; |
69 | 72 | ||
70 | typedef struct { | 73 | typedef struct { |
74 | uint8_t client_id[CLIENT_ID_SIZE]; | ||
75 | uint64_t timestamp; | ||
76 | } Last_Pinged; | ||
77 | |||
78 | typedef struct { | ||
71 | uint8_t status; /* 0 if friend is not valid, 1 if friend is valid.*/ | 79 | uint8_t status; /* 0 if friend is not valid, 1 if friend is valid.*/ |
72 | uint8_t is_online; /* Set by the onion_set_friend_status function. */ | 80 | uint8_t is_online; /* Set by the onion_set_friend_status function. */ |
73 | 81 | ||
@@ -88,6 +96,9 @@ typedef struct { | |||
88 | 96 | ||
89 | Onion_Client_Paths onion_paths; | 97 | Onion_Client_Paths onion_paths; |
90 | uint32_t ping_nodes_sent_second; | 98 | uint32_t ping_nodes_sent_second; |
99 | |||
100 | Last_Pinged last_pinged[MAX_STORED_PINGED_NODES]; | ||
101 | uint8_t last_pinged_index; | ||
91 | } Onion_Friend; | 102 | } Onion_Friend; |
92 | 103 | ||
93 | typedef int (*oniondata_handler_callback)(void *object, uint8_t *source_pubkey, uint8_t *data, uint32_t len); | 104 | typedef int (*oniondata_handler_callback)(void *object, uint8_t *source_pubkey, uint8_t *data, uint32_t len); |
@@ -109,6 +120,9 @@ typedef struct { | |||
109 | uint8_t temp_secret_key[crypto_box_SECRETKEYBYTES]; | 120 | uint8_t temp_secret_key[crypto_box_SECRETKEYBYTES]; |
110 | 121 | ||
111 | uint32_t ping_nodes_sent_second; | 122 | uint32_t ping_nodes_sent_second; |
123 | |||
124 | Last_Pinged last_pinged[MAX_STORED_PINGED_NODES]; | ||
125 | uint8_t last_pinged_index; | ||
112 | struct { | 126 | struct { |
113 | oniondata_handler_callback function; | 127 | oniondata_handler_callback function; |
114 | void *object; | 128 | void *object; |