summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2014-02-28 21:14:55 -0500
committerirungentoo <irungentoo@gmail.com>2014-02-28 21:14:55 -0500
commit16b93e823bffaf68213d2f66d5af12c5a07ef290 (patch)
treeefae5d5dd3b540f47899c7ab3ea0914e7bf133cc
parentd0d9f95b347b8d7d92d487c256dd8e105d2caa2a (diff)
Very simple fix to lower bandwidth usage at startup.
-rw-r--r--toxcore/onion_client.c12
-rw-r--r--toxcore/onion_client.h8
2 files changed, 19 insertions, 1 deletions
diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c
index 6d7785f3..9b75a234 100644
--- a/toxcore/onion_client.c
+++ b/toxcore/onion_client.c
@@ -300,19 +300,26 @@ static int client_ping_nodes(Onion_Client *onion_c, uint32_t num, Node_format *n
300 300
301 Onion_Node *list_nodes = NULL; 301 Onion_Node *list_nodes = NULL;
302 uint8_t *reference_id = NULL; 302 uint8_t *reference_id = NULL;
303 uint32_t *ping_nodes_sent_second = NULL;
303 304
304 if (num == 0) { 305 if (num == 0) {
305 list_nodes = onion_c->clients_announce_list; 306 list_nodes = onion_c->clients_announce_list;
306 reference_id = onion_c->dht->c->self_public_key; 307 reference_id = onion_c->dht->c->self_public_key;
308 ping_nodes_sent_second = &onion_c->ping_nodes_sent_second;
307 } else { 309 } else {
308 list_nodes = onion_c->friends_list[num - 1].clients_list; 310 list_nodes = onion_c->friends_list[num - 1].clients_list;
309 reference_id = onion_c->friends_list[num - 1].real_client_id; 311 reference_id = onion_c->friends_list[num - 1].real_client_id;
312 ping_nodes_sent_second = &onion_c->friends_list[num - 1].ping_nodes_sent_second;
310 } 313 }
311 314
312 uint32_t i, j; 315 uint32_t i, j;
313 int lan_ips_accepted = (LAN_ip(source.ip) == 0); 316 int lan_ips_accepted = (LAN_ip(source.ip) == 0);
314 317
315 for (i = 0; i < num_nodes; ++i) { 318 for (i = 0; i < num_nodes; ++i) {
319
320 if (*ping_nodes_sent_second > MAX_PING_NODES_SECOND_PEER)
321 return 0;
322
316 to_host_family(&nodes[i].ip_port.ip); 323 to_host_family(&nodes[i].ip_port.ip);
317 324
318 if (!lan_ips_accepted) 325 if (!lan_ips_accepted)
@@ -329,7 +336,8 @@ static int client_ping_nodes(Onion_Client *onion_c, uint32_t num, Node_format *n
329 } 336 }
330 337
331 if (j == MAX_ONION_CLIENTS) { 338 if (j == MAX_ONION_CLIENTS) {
332 client_send_announce_request(onion_c, num, nodes[i].ip_port, nodes[i].client_id, NULL, ~0); 339 if (client_send_announce_request(onion_c, num, nodes[i].ip_port, nodes[i].client_id, NULL, ~0) == 0)
340 ++*ping_nodes_sent_second;
333 } 341 }
334 } 342 }
335 } 343 }
@@ -923,8 +931,10 @@ void do_onion_client(Onion_Client *onion_c)
923 for (i = 0; i < onion_c->num_friends; ++i) { 931 for (i = 0; i < onion_c->num_friends; ++i) {
924 do_friend(onion_c, i); 932 do_friend(onion_c, i);
925 cleanup_friend(onion_c, i); 933 cleanup_friend(onion_c, i);
934 onion_c->friends_list[i].ping_nodes_sent_second = 0;
926 } 935 }
927 936
937 onion_c->ping_nodes_sent_second = 0;
928 onion_c->last_run = unix_time(); 938 onion_c->last_run = unix_time();
929} 939}
930 940
diff --git a/toxcore/onion_client.h b/toxcore/onion_client.h
index d14406a2..3235c3f4 100644
--- a/toxcore/onion_client.h
+++ b/toxcore/onion_client.h
@@ -40,6 +40,11 @@
40#define ONION_PATH_FIRST_TIMEOUT 5 40#define ONION_PATH_FIRST_TIMEOUT 5
41#define ONION_PATH_TIMEOUT 30 41#define ONION_PATH_TIMEOUT 30
42 42
43/* A cheap way of making it take less bandwidth at startup:
44 by limiting the number of ping packets we can send per
45 second per peer. */
46#define MAX_PING_NODES_SECOND_PEER 5
47
43typedef struct { 48typedef struct {
44 uint8_t client_id[CLIENT_ID_SIZE]; 49 uint8_t client_id[CLIENT_ID_SIZE];
45 IP_Port ip_port; 50 IP_Port ip_port;
@@ -79,6 +84,7 @@ typedef struct {
79 uint64_t last_seen; 84 uint64_t last_seen;
80 85
81 Onion_Client_Paths onion_paths; 86 Onion_Client_Paths onion_paths;
87 uint32_t ping_nodes_sent_second;
82} Onion_Friend; 88} Onion_Friend;
83 89
84typedef int (*oniondata_handler_callback)(void *object, uint8_t *source_pubkey, uint8_t *data, uint32_t len); 90typedef int (*oniondata_handler_callback)(void *object, uint8_t *source_pubkey, uint8_t *data, uint32_t len);
@@ -98,6 +104,8 @@ typedef struct {
98 104
99 uint8_t temp_public_key[crypto_box_PUBLICKEYBYTES]; 105 uint8_t temp_public_key[crypto_box_PUBLICKEYBYTES];
100 uint8_t temp_secret_key[crypto_box_SECRETKEYBYTES]; 106 uint8_t temp_secret_key[crypto_box_SECRETKEYBYTES];
107
108 uint32_t ping_nodes_sent_second;
101 struct { 109 struct {
102 oniondata_handler_callback function; 110 oniondata_handler_callback function;
103 void *object; 111 void *object;