Skip to content

Commit

Permalink
Reduced the number of useless onion packets sent.
Browse files Browse the repository at this point in the history
  • Loading branch information
irungentoo committed Mar 10, 2014
1 parent 8d7ba01 commit d0e3712
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
27 changes: 25 additions & 2 deletions toxcore/onion_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,22 @@ static int client_add_to_list(Onion_Client *onion_c, uint32_t num, uint8_t *publ
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,
IP_Port source)
{
Expand All @@ -304,14 +320,21 @@ static int client_ping_nodes(Onion_Client *onion_c, uint32_t num, Node_format *n
uint8_t *reference_id = NULL;
uint32_t *ping_nodes_sent_second = NULL;

Last_Pinged *last_pinged = NULL;
uint8_t *last_pinged_index = NULL;

if (num == 0) {
list_nodes = onion_c->clients_announce_list;
reference_id = onion_c->dht->c->self_public_key;
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 {
list_nodes = onion_c->friends_list[num - 1].clients_list;
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;
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;
Expand All @@ -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)
++*ping_nodes_sent_second;
}
Expand Down Expand Up @@ -888,7 +911,7 @@ void oniondata_registerhandler(Onion_Client *onion_c, uint8_t byte, oniondata_ha
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

static void do_announce(Onion_Client *onion_c)
Expand Down
14 changes: 14 additions & 0 deletions toxcore/onion_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
second per peer. */
#define MAX_PING_NODES_SECOND_PEER 5

#define MAX_STORED_PINGED_NODES 9
#define MIN_NODE_PING_TIME 10

typedef struct {
uint8_t client_id[CLIENT_ID_SIZE];
IP_Port ip_port;
Expand All @@ -67,6 +70,11 @@ typedef struct {
uint64_t path_creation_time[NUMBER_ONION_PATHS];
} Onion_Client_Paths;

typedef struct {
uint8_t client_id[CLIENT_ID_SIZE];
uint64_t timestamp;
} Last_Pinged;

typedef struct {
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. */
Expand All @@ -88,6 +96,9 @@ typedef struct {

Onion_Client_Paths onion_paths;
uint32_t ping_nodes_sent_second;

Last_Pinged last_pinged[MAX_STORED_PINGED_NODES];
uint8_t last_pinged_index;
} Onion_Friend;

typedef int (*oniondata_handler_callback)(void *object, uint8_t *source_pubkey, uint8_t *data, uint32_t len);
Expand All @@ -109,6 +120,9 @@ typedef struct {
uint8_t temp_secret_key[crypto_box_SECRETKEYBYTES];

uint32_t ping_nodes_sent_second;

Last_Pinged last_pinged[MAX_STORED_PINGED_NODES];
uint8_t last_pinged_index;
struct {
oniondata_handler_callback function;
void *object;
Expand Down

0 comments on commit d0e3712

Please sign in to comment.