Reduced the number of useless onion packets sent.

This commit is contained in:
irungentoo 2014-03-09 20:51:04 -04:00
parent 8d7ba013a5
commit d0e3712924
2 changed files with 39 additions and 2 deletions

View File

@ -291,6 +291,22 @@ static int client_add_to_list(Onion_Client *onion_c, uint32_t num, uint8_t *publ
return 0; return 0;
} }
static int good_to_ping(Last_Pinged *last_pinged, uint8_t *last_pinged_index, uint8_t *client_id)
{
uint32_t i;
for (i = 0; i < MAX_STORED_PINGED_NODES; ++i) {
if (!is_timeout(last_pinged[i].timestamp, MIN_NODE_PING_TIME))
if (memcmp(last_pinged[i].client_id, client_id, crypto_box_PUBLICKEYBYTES) == 0)
return 0;
}
memcpy(last_pinged[*last_pinged_index % MAX_STORED_PINGED_NODES].client_id, client_id, crypto_box_PUBLICKEYBYTES);
last_pinged[*last_pinged_index % MAX_STORED_PINGED_NODES].timestamp = unix_time();
++*last_pinged_index;
return 1;
}
static int client_ping_nodes(Onion_Client *onion_c, uint32_t num, Node_format *nodes, uint16_t num_nodes, static int client_ping_nodes(Onion_Client *onion_c, uint32_t num, Node_format *nodes, uint16_t num_nodes,
IP_Port source) IP_Port source)
{ {
@ -304,14 +320,21 @@ static int client_ping_nodes(Onion_Client *onion_c, uint32_t num, Node_format *n
uint8_t *reference_id = NULL; uint8_t *reference_id = NULL;
uint32_t *ping_nodes_sent_second = NULL; uint32_t *ping_nodes_sent_second = NULL;
Last_Pinged *last_pinged = NULL;
uint8_t *last_pinged_index = NULL;
if (num == 0) { if (num == 0) {
list_nodes = onion_c->clients_announce_list; list_nodes = onion_c->clients_announce_list;
reference_id = onion_c->dht->c->self_public_key; reference_id = onion_c->dht->c->self_public_key;
ping_nodes_sent_second = &onion_c->ping_nodes_sent_second; ping_nodes_sent_second = &onion_c->ping_nodes_sent_second;
last_pinged = onion_c->last_pinged;
last_pinged_index = &onion_c->last_pinged_index;
} else { } else {
list_nodes = onion_c->friends_list[num - 1].clients_list; list_nodes = onion_c->friends_list[num - 1].clients_list;
reference_id = onion_c->friends_list[num - 1].real_client_id; reference_id = onion_c->friends_list[num - 1].real_client_id;
ping_nodes_sent_second = &onion_c->friends_list[num - 1].ping_nodes_sent_second; ping_nodes_sent_second = &onion_c->friends_list[num - 1].ping_nodes_sent_second;
last_pinged = onion_c->friends_list[num - 1].last_pinged;
last_pinged_index = &onion_c->friends_list[num - 1].last_pinged_index;
} }
uint32_t i, j; uint32_t i, j;
@ -337,7 +360,7 @@ static int client_ping_nodes(Onion_Client *onion_c, uint32_t num, Node_format *n
} }
} }
if (j == MAX_ONION_CLIENTS) { if (j == MAX_ONION_CLIENTS && good_to_ping(last_pinged, last_pinged_index, nodes[i].client_id)) {
if (client_send_announce_request(onion_c, num, nodes[i].ip_port, nodes[i].client_id, NULL, ~0) == 0) if (client_send_announce_request(onion_c, num, nodes[i].ip_port, nodes[i].client_id, NULL, ~0) == 0)
++*ping_nodes_sent_second; ++*ping_nodes_sent_second;
} }
@ -888,7 +911,7 @@ void oniondata_registerhandler(Onion_Client *onion_c, uint8_t byte, oniondata_ha
onion_c->Onion_Data_Handlers[byte].object = object; onion_c->Onion_Data_Handlers[byte].object = object;
} }
#define ANNOUNCE_INTERVAL_NOT_ANNOUNCED 7 #define ANNOUNCE_INTERVAL_NOT_ANNOUNCED 10
#define ANNOUNCE_INTERVAL_ANNOUNCED ONION_NODE_PING_INTERVAL #define ANNOUNCE_INTERVAL_ANNOUNCED ONION_NODE_PING_INTERVAL
static void do_announce(Onion_Client *onion_c) static void do_announce(Onion_Client *onion_c)

View File

@ -47,6 +47,9 @@
second per peer. */ second per peer. */
#define MAX_PING_NODES_SECOND_PEER 5 #define MAX_PING_NODES_SECOND_PEER 5
#define MAX_STORED_PINGED_NODES 9
#define MIN_NODE_PING_TIME 10
typedef struct { typedef struct {
uint8_t client_id[CLIENT_ID_SIZE]; uint8_t client_id[CLIENT_ID_SIZE];
IP_Port ip_port; IP_Port ip_port;
@ -67,6 +70,11 @@ typedef struct {
uint64_t path_creation_time[NUMBER_ONION_PATHS]; uint64_t path_creation_time[NUMBER_ONION_PATHS];
} Onion_Client_Paths; } Onion_Client_Paths;
typedef struct {
uint8_t client_id[CLIENT_ID_SIZE];
uint64_t timestamp;
} Last_Pinged;
typedef struct { typedef struct {
uint8_t status; /* 0 if friend is not valid, 1 if friend is valid.*/ uint8_t status; /* 0 if friend is not valid, 1 if friend is valid.*/
uint8_t is_online; /* Set by the onion_set_friend_status function. */ uint8_t is_online; /* Set by the onion_set_friend_status function. */
@ -88,6 +96,9 @@ typedef struct {
Onion_Client_Paths onion_paths; Onion_Client_Paths onion_paths;
uint32_t ping_nodes_sent_second; uint32_t ping_nodes_sent_second;
Last_Pinged last_pinged[MAX_STORED_PINGED_NODES];
uint8_t last_pinged_index;
} Onion_Friend; } Onion_Friend;
typedef int (*oniondata_handler_callback)(void *object, uint8_t *source_pubkey, uint8_t *data, uint32_t len); typedef int (*oniondata_handler_callback)(void *object, uint8_t *source_pubkey, uint8_t *data, uint32_t len);
@ -109,6 +120,9 @@ typedef struct {
uint8_t temp_secret_key[crypto_box_SECRETKEYBYTES]; uint8_t temp_secret_key[crypto_box_SECRETKEYBYTES];
uint32_t ping_nodes_sent_second; uint32_t ping_nodes_sent_second;
Last_Pinged last_pinged[MAX_STORED_PINGED_NODES];
uint8_t last_pinged_index;
struct { struct {
oniondata_handler_callback function; oniondata_handler_callback function;
void *object; void *object;