Skip to content

Commit 028b017

Browse files
committed
perf: Slightly reduce bandwidth usage when there are few nodes.
This mainly saves spam in test logs, but may save some packets here and there, if nodes are randomly selected twice for GET_NODES and onion routing packets.
1 parent 90f7496 commit 028b017

24 files changed

Lines changed: 246 additions & 83 deletions

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ unit_test(toxcore bin_pack)
435435
unit_test(toxcore crypto_core)
436436
unit_test(toxcore group_announce)
437437
unit_test(toxcore group_moderation)
438+
unit_test(toxcore list)
438439
unit_test(toxcore mem)
439440
unit_test(toxcore mono_time)
440441
unit_test(toxcore ping_array)

auto_tests/onion_test.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static int handle_test_1(void *object, const IP_Port *source, const uint8_t *pac
5353
res_packet[0] = NET_PACKET_ANNOUNCE_RESPONSE;
5454
memcpy(res_packet + 1, res_message, sizeof(res_message));
5555

56-
if (send_onion_response(onion->net, source, res_packet, sizeof(res_packet),
56+
if (send_onion_response(onion->log, onion->net, source, res_packet, sizeof(res_packet),
5757
packet + sizeof(res_packet)) == -1) {
5858
return 1;
5959
}
@@ -293,7 +293,7 @@ static void test_basic(void)
293293
uint64_t s;
294294
memcpy(&s, sb_data, sizeof(uint64_t));
295295
memcpy(test_3_pub_key, nodes[3].public_key, CRYPTO_PUBLIC_KEY_SIZE);
296-
int ret = send_announce_request(onion1->net, rng, &path, &nodes[3],
296+
int ret = send_announce_request(log1, onion1->net, rng, &path, &nodes[3],
297297
dht_get_self_public_key(onion1->dht),
298298
dht_get_self_secret_key(onion1->dht),
299299
zeroes,
@@ -315,7 +315,7 @@ static void test_basic(void)
315315
memcpy(onion_announce_entry_public_key(onion2_a, 1), dht_get_self_public_key(onion2->dht), CRYPTO_PUBLIC_KEY_SIZE);
316316
onion_announce_entry_set_time(onion2_a, 1, mono_time_get(mono_time2));
317317
networking_registerhandler(onion1->net, NET_PACKET_ONION_DATA_RESPONSE, &handle_test_4, onion1);
318-
send_announce_request(onion1->net, rng, &path, &nodes[3],
318+
send_announce_request(log1, onion1->net, rng, &path, &nodes[3],
319319
dht_get_self_public_key(onion1->dht),
320320
dht_get_self_secret_key(onion1->dht),
321321
test_3_ping_id,
@@ -340,7 +340,7 @@ static void test_basic(void)
340340
ck_assert_msg((onion3 != nullptr), "Onion failed initializing.");
341341

342342
random_nonce(rng, nonce);
343-
ret = send_data_request(onion3->net, rng, &path, &nodes[3].ip_port,
343+
ret = send_data_request(log3, onion3->net, rng, &path, &nodes[3].ip_port,
344344
dht_get_self_public_key(onion1->dht),
345345
dht_get_self_public_key(onion1->dht),
346346
nonce, (const uint8_t *)"Install gentoo", sizeof("Install gentoo"));
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
849ec5686eeaea448f4ef99650b016c883e6ea13d5fa2e7b2a344c9275a10431 /usr/local/bin/tox-bootstrapd
1+
d9f100677e54c65fc1651da701a59c7a184ffe06381c6376102c668a8034a570 /usr/local/bin/tox-bootstrapd

toxav/msi.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,7 @@ static void handle_pop(MSICall *call, const MSIMessage *msg)
821821
switch (call->state) {
822822
case MSI_CALL_INACTIVE: {
823823
LOGGER_FATAL(call->session->messenger->log, "Handling what should be impossible case");
824+
break;
824825
}
825826

826827
case MSI_CALL_ACTIVE: {

toxcore/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ cc_library(
201201
":attributes",
202202
":ccompat",
203203
":mem",
204+
":util",
204205
"@pthread",
205206
],
206207
)
@@ -230,6 +231,7 @@ cc_library(
230231
deps = [
231232
":ccompat",
232233
":crypto_core",
234+
":logger",
233235
":mem",
234236
":mono_time",
235237
],

toxcore/DHT.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,7 +1828,9 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co
18281828
++not_kill;
18291829

18301830
if (mono_time_is_timeout(dht->mono_time, assoc->last_pinged, PING_INTERVAL)) {
1831-
dht_getnodes(dht, &assoc->ip_port, client->public_key, public_key);
1831+
const IP_Port *target = &assoc->ip_port;
1832+
const uint8_t *target_key = client->public_key;
1833+
dht_getnodes(dht, target, target_key, public_key);
18321834
assoc->last_pinged = temp_time;
18331835
}
18341836

@@ -1861,7 +1863,9 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co
18611863
rand_node += random_range_u32(dht->rng, num_nodes - (rand_node + 1));
18621864
}
18631865

1864-
dht_getnodes(dht, &assoc_list[rand_node]->ip_port, client_list[rand_node]->public_key, public_key);
1866+
const IP_Port *target = &assoc_list[rand_node]->ip_port;
1867+
const uint8_t *target_key = client_list[rand_node]->public_key;
1868+
dht_getnodes(dht, target, target_key, public_key);
18651869

18661870
*lastgetnode = temp_time;
18671871
++*bootstrap_times;
@@ -1890,8 +1894,7 @@ static void do_dht_friends(DHT *dht)
18901894
dht_friend->num_to_bootstrap = 0;
18911895

18921896
do_ping_and_sendnode_requests(dht, &dht_friend->lastgetnode, dht_friend->public_key, dht_friend->client_list,
1893-
MAX_FRIEND_CLIENTS,
1894-
&dht_friend->bootstrap_times, true);
1897+
MAX_FRIEND_CLIENTS, &dht_friend->bootstrap_times, true);
18951898
}
18961899
}
18971900

@@ -2634,6 +2637,7 @@ DHT *new_dht(const Logger *log, const Memory *mem, const Random *rng, const Netw
26342637
DHT *const dht = (DHT *)mem_alloc(mem, sizeof(DHT));
26352638

26362639
if (dht == nullptr) {
2640+
LOGGER_ERROR(log, "failed to allocate DHT struct (%ld bytes)", (unsigned long)sizeof(DHT));
26372641
return nullptr;
26382642
}
26392643

@@ -2651,6 +2655,7 @@ DHT *new_dht(const Logger *log, const Memory *mem, const Random *rng, const Netw
26512655
dht->ping = ping_new(mem, mono_time, rng, dht);
26522656

26532657
if (dht->ping == nullptr) {
2658+
LOGGER_ERROR(log, "failed to initialise ping");
26542659
kill_dht(dht);
26552660
return nullptr;
26562661
}
@@ -2667,10 +2672,11 @@ DHT *new_dht(const Logger *log, const Memory *mem, const Random *rng, const Netw
26672672

26682673
crypto_new_keypair(rng, dht->self_public_key, dht->self_secret_key);
26692674

2670-
dht->shared_keys_recv = shared_key_cache_new(mono_time, mem, dht->self_secret_key, KEYS_TIMEOUT, MAX_KEYS_PER_SLOT);
2671-
dht->shared_keys_sent = shared_key_cache_new(mono_time, mem, dht->self_secret_key, KEYS_TIMEOUT, MAX_KEYS_PER_SLOT);
2675+
dht->shared_keys_recv = shared_key_cache_new(log, mono_time, mem, dht->self_secret_key, KEYS_TIMEOUT, MAX_KEYS_PER_SLOT);
2676+
dht->shared_keys_sent = shared_key_cache_new(log, mono_time, mem, dht->self_secret_key, KEYS_TIMEOUT, MAX_KEYS_PER_SLOT);
26722677

26732678
if (dht->shared_keys_recv == nullptr || dht->shared_keys_sent == nullptr) {
2679+
LOGGER_ERROR(log, "failed to initialise shared key cache");
26742680
kill_dht(dht);
26752681
return nullptr;
26762682
}
@@ -2679,6 +2685,7 @@ DHT *new_dht(const Logger *log, const Memory *mem, const Random *rng, const Netw
26792685
dht->dht_ping_array = ping_array_new(mem, DHT_PING_ARRAY_SIZE, PING_TIMEOUT);
26802686

26812687
if (dht->dht_ping_array == nullptr) {
2688+
LOGGER_ERROR(log, "failed to initialise ping array");
26822689
kill_dht(dht);
26832690
return nullptr;
26842691
}
@@ -2691,6 +2698,7 @@ DHT *new_dht(const Logger *log, const Memory *mem, const Random *rng, const Netw
26912698

26922699
uint32_t token; // We don't intend to delete these ever, but need to pass the token
26932700
if (dht_addfriend(dht, random_public_key_bytes, nullptr, nullptr, 0, &token) != 0) {
2701+
LOGGER_ERROR(log, "failed to add initial random seed DHT friends");
26942702
kill_dht(dht);
26952703
return nullptr;
26962704
}

toxcore/Messenger.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3583,6 +3583,7 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random *
35833583
mem_delete(mem, m);
35843584

35853585
if (error != nullptr && net_err == 1) {
3586+
LOGGER_WARNING(m->log, "network initialisation failed (no ports available)");
35863587
*error = MESSENGER_ERROR_PORT;
35873588
}
35883589

@@ -3602,6 +3603,8 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random *
36023603
m->net_crypto = new_net_crypto(m->log, m->mem, m->rng, m->ns, m->mono_time, m->dht, &options->proxy_info);
36033604

36043605
if (m->net_crypto == nullptr) {
3606+
LOGGER_WARNING(m->log, "net_crypto initialisation failed");
3607+
36053608
kill_dht(m->dht);
36063609
kill_networking(m->net);
36073610
friendreq_kill(m->fr);
@@ -3614,6 +3617,8 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random *
36143617
m->group_announce = new_gca_list();
36153618

36163619
if (m->group_announce == nullptr) {
3620+
LOGGER_WARNING(m->log, "DHT group chats initialisation failed");
3621+
36173622
kill_net_crypto(m->net_crypto);
36183623
kill_dht(m->dht);
36193624
kill_networking(m->net);
@@ -3642,6 +3647,8 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random *
36423647

36433648
if ((options->dht_announcements_enabled && (m->forwarding == nullptr || m->announce == nullptr)) ||
36443649
m->onion == nullptr || m->onion_a == nullptr || m->onion_c == nullptr || m->fr_c == nullptr) {
3650+
LOGGER_WARNING(m->log, "onion initialisation failed");
3651+
36453652
kill_onion(m->onion);
36463653
kill_onion_announce(m->onion_a);
36473654
kill_onion_client(m->onion_c);
@@ -3666,6 +3673,8 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random *
36663673
m->group_handler = new_dht_groupchats(m);
36673674

36683675
if (m->group_handler == nullptr) {
3676+
LOGGER_WARNING(m->log, "conferences initialisation failed");
3677+
36693678
kill_onion(m->onion);
36703679
kill_onion_announce(m->onion_a);
36713680
kill_onion_client(m->onion_c);
@@ -3690,6 +3699,8 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random *
36903699
m->onion, m->forwarding);
36913700

36923701
if (m->tcp_server == nullptr) {
3702+
LOGGER_WARNING(m->log, "TCP server initialisation failed");
3703+
36933704
kill_onion(m->onion);
36943705
kill_onion_announce(m->onion_a);
36953706
#ifndef VANILLA_NACL

toxcore/TCP_common.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,11 @@ int read_tcp_packet(
201201
const uint16_t count = net_socket_data_recv_buffer(ns, sock);
202202

203203
if (count < length) {
204-
LOGGER_TRACE(logger, "recv buffer has %d bytes, but requested %d bytes", count, length);
204+
if (count != 0) {
205+
// Only log when there are some bytes available, as empty buffer
206+
// is a very common case and this spams our logs.
207+
LOGGER_TRACE(logger, "recv buffer has %d bytes, but requested %d bytes", count, length);
208+
}
205209
return -1;
206210
}
207211

toxcore/TCP_connection.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ bool tcp_get_random_conn_ip_port(const TCP_Connections *tcp_c, IP_Port *ip_port)
120120
* return -1 on failure.
121121
*/
122122
non_null()
123-
int tcp_send_onion_request(TCP_Connections *tcp_c, unsigned int tcp_connections_number, const uint8_t *data,
123+
int tcp_send_onion_request(TCP_Connections *tcp_c, uint32_t tcp_connections_number, const uint8_t *data,
124124
uint16_t length);
125125

126126
/** @brief Set if we want TCP_connection to allocate some connection for onion use.

toxcore/announce.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ Announcements *new_announcements(const Logger *log, const Memory *mem, const Ran
660660
announce->public_key = dht_get_self_public_key(announce->dht);
661661
announce->secret_key = dht_get_self_secret_key(announce->dht);
662662
new_hmac_key(announce->rng, announce->hmac_key);
663-
announce->shared_keys = shared_key_cache_new(mono_time, mem, announce->secret_key, KEYS_TIMEOUT, MAX_KEYS_PER_SLOT);
663+
announce->shared_keys = shared_key_cache_new(log, mono_time, mem, announce->secret_key, KEYS_TIMEOUT, MAX_KEYS_PER_SLOT);
664664
if (announce->shared_keys == nullptr) {
665665
free(announce);
666666
return nullptr;

0 commit comments

Comments
 (0)