Skip to content

Commit 2ce6f5b

Browse files
committed
fix: Report failure to DHT bootstrap back to the client.
Also reduce log verbosity a bit.
1 parent de67c40 commit 2ce6f5b

File tree

14 files changed

+90
-53
lines changed

14 files changed

+90
-53
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ on:
44
pull_request:
55
branches: [master]
66

7+
# Cancel old PR builds when pushing new commits.
8+
concurrency:
9+
group: build-${{ github.event.pull_request.number || github.ref }}
10+
cancel-in-progress: true
11+
712
jobs:
813
common:
914
uses: TokTok/ci-tools/.github/workflows/common-ci.yml@master

auto_tests/dht_test.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -626,9 +626,9 @@ static void test_dht_create_packet(void)
626626

627627
#define MAX_COUNT 3
628628

629-
static void dht_pack_unpack(const Node_format *nodes, size_t size, uint8_t *data, size_t length)
629+
static void dht_pack_unpack(const Logger *logger, const Node_format *nodes, size_t size, uint8_t *data, size_t length)
630630
{
631-
int16_t packed_size = pack_nodes(data, length, nodes, size);
631+
int16_t packed_size = pack_nodes(logger, data, length, nodes, size);
632632
ck_assert_msg(packed_size != -1, "Wrong pack_nodes result");
633633

634634
uint16_t processed = 0;
@@ -677,6 +677,10 @@ static void random_ip(IP_Port *ipp, int family)
677677

678678
static void test_dht_node_packing(void)
679679
{
680+
Logger *log = logger_new();
681+
uint32_t index = 1;
682+
logger_callback_log(log, (logger_cb *)print_debug_log, nullptr, &index);
683+
680684
const uint16_t length = MAX_COUNT * PACKED_NODES_SIZE;
681685
uint8_t *data = (uint8_t *)malloc(length);
682686

@@ -690,28 +694,29 @@ static void test_dht_node_packing(void)
690694
random_ip(&nodes[0].ip_port, TOX_AF_INET);
691695
random_ip(&nodes[1].ip_port, TOX_AF_INET);
692696
random_ip(&nodes[2].ip_port, TOX_AF_INET);
693-
dht_pack_unpack(nodes, 3, data, length);
697+
dht_pack_unpack(log, nodes, 3, data, length);
694698

695699
random_ip(&nodes[0].ip_port, TOX_AF_INET);
696700
random_ip(&nodes[1].ip_port, TOX_AF_INET);
697701
random_ip(&nodes[2].ip_port, TCP_INET);
698-
dht_pack_unpack(nodes, 3, data, length);
702+
dht_pack_unpack(log, nodes, 3, data, length);
699703

700704
random_ip(&nodes[0].ip_port, TOX_AF_INET);
701705
random_ip(&nodes[1].ip_port, TOX_AF_INET6);
702706
random_ip(&nodes[2].ip_port, TCP_INET6);
703-
dht_pack_unpack(nodes, 3, data, length);
707+
dht_pack_unpack(log, nodes, 3, data, length);
704708

705709
random_ip(&nodes[0].ip_port, TCP_INET);
706710
random_ip(&nodes[1].ip_port, TCP_INET6);
707711
random_ip(&nodes[2].ip_port, TCP_INET);
708-
dht_pack_unpack(nodes, 3, data, length);
712+
dht_pack_unpack(log, nodes, 3, data, length);
709713

710714
random_ip(&nodes[0].ip_port, TOX_AF_INET6);
711715
random_ip(&nodes[1].ip_port, TOX_AF_INET6);
712716
random_ip(&nodes[2].ip_port, TOX_AF_INET6);
713-
dht_pack_unpack(nodes, 3, data, length);
717+
dht_pack_unpack(log, nodes, 3, data, length);
714718

719+
logger_kill(log);
715720
free(data);
716721
}
717722

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
e79b800f95e9dd714128de63824483facac7487788d57a9c89ef2e1c39d3588f /usr/local/bin/tox-bootstrapd
1+
b899ee4325e854deafa435b697c0e37430222f4f87537f4b12021eb40d12857d /usr/local/bin/tox-bootstrapd

toxav/toxav.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -973,13 +973,13 @@ bool toxav_video_send_frame(ToxAV *av, uint32_t friend_number, uint16_t width, u
973973
if (call->video_rtp->ssrc < VIDEO_SEND_X_KEYFRAMES_FIRST) {
974974
// Key frame flag for first frames
975975
vpx_encode_flags = VPX_EFLAG_FORCE_KF;
976-
LOGGER_INFO(av->m->log, "I_FRAME_FLAG:%d only-i-frame mode", call->video_rtp->ssrc);
976+
LOGGER_DEBUG(av->m->log, "I_FRAME_FLAG:%d only-i-frame mode", call->video_rtp->ssrc);
977977

978978
++call->video_rtp->ssrc;
979979
} else if (call->video_rtp->ssrc == VIDEO_SEND_X_KEYFRAMES_FIRST) {
980980
// normal keyframe placement
981981
vpx_encode_flags = 0;
982-
LOGGER_INFO(av->m->log, "I_FRAME_FLAG:%d normal mode", call->video_rtp->ssrc);
982+
LOGGER_DEBUG(av->m->log, "I_FRAME_FLAG:%d normal mode", call->video_rtp->ssrc);
983983

984984
++call->video_rtp->ssrc;
985985
}

toxcore/DHT.c

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ int packed_node_size(Family ip_family)
422422
* Returns size of packed IP_Port data on success
423423
* Return -1 on failure.
424424
*/
425-
int pack_ip_port(uint8_t *data, uint16_t length, const IP_Port *ip_port)
425+
int pack_ip_port(const Logger *logger, uint8_t *data, uint16_t length, const IP_Port *ip_port)
426426
{
427427
if (data == nullptr) {
428428
return -1;
@@ -445,6 +445,10 @@ int pack_ip_port(uint8_t *data, uint16_t length, const IP_Port *ip_port)
445445
is_ipv4 = false;
446446
net_family = TOX_TCP_INET6;
447447
} else {
448+
char ip_str[IP_NTOA_LEN];
449+
// TODO(iphydf): Find out why we're trying to pack invalid IPs, stop
450+
// doing that, and turn this into an error.
451+
LOGGER_TRACE(logger, "cannot pack invalid IP: %s", ip_ntoa(&ip_port->ip, ip_str, sizeof(ip_str)));
448452
return -1;
449453
}
450454

@@ -576,12 +580,12 @@ int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length, bool
576580
* return length of packed nodes on success.
577581
* return -1 on failure.
578582
*/
579-
int pack_nodes(uint8_t *data, uint16_t length, const Node_format *nodes, uint16_t number)
583+
int pack_nodes(const Logger *logger, uint8_t *data, uint16_t length, const Node_format *nodes, uint16_t number)
580584
{
581585
uint32_t packed_length = 0;
582586

583587
for (uint32_t i = 0; i < number && packed_length < length; ++i) {
584-
const int ipp_size = pack_ip_port(data + packed_length, length - packed_length, &nodes[i].ip_port);
588+
const int ipp_size = pack_ip_port(logger, data + packed_length, length - packed_length, &nodes[i].ip_port);
585589

586590
if (ipp_size == -1) {
587591
return -1;
@@ -1325,7 +1329,7 @@ bool dht_getnodes(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key, c
13251329
memcpy(receiver.public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE);
13261330
receiver.ip_port = *ip_port;
13271331

1328-
if (pack_nodes(plain_message, sizeof(plain_message), &receiver, 1) == -1) {
1332+
if (pack_nodes(dht->log, plain_message, sizeof(plain_message), &receiver, 1) == -1) {
13291333
return false;
13301334
}
13311335

@@ -1334,6 +1338,7 @@ bool dht_getnodes(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key, c
13341338
ping_id = ping_array_add(dht->dht_ping_array, dht->mono_time, plain_message, sizeof(receiver));
13351339

13361340
if (ping_id == 0) {
1341+
LOGGER_ERROR(dht->log, "adding ping id failed");
13371342
return false;
13381343
}
13391344

@@ -1352,6 +1357,7 @@ bool dht_getnodes(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key, c
13521357
crypto_memzero(shared_key, sizeof(shared_key));
13531358

13541359
if (len != sizeof(data)) {
1360+
LOGGER_ERROR(dht->log, "getnodes packet encryption failed");
13551361
return false;
13561362
}
13571363

@@ -1383,7 +1389,7 @@ static int sendnodes_ipv6(const DHT *dht, const IP_Port *ip_port, const uint8_t
13831389
int nodes_length = 0;
13841390

13851391
if (num_nodes > 0) {
1386-
nodes_length = pack_nodes(plain + 1, node_format_size * MAX_SENT_NODES, nodes_list, num_nodes);
1392+
nodes_length = pack_nodes(dht->log, plain + 1, node_format_size * MAX_SENT_NODES, nodes_list, num_nodes);
13871393

13881394
if (nodes_length <= 0) {
13891395
return -1;
@@ -1841,9 +1847,14 @@ static void do_Close(DHT *dht)
18411847
}
18421848
}
18431849

1844-
void dht_bootstrap(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key)
1850+
bool dht_bootstrap(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key)
18451851
{
1846-
dht_getnodes(dht, ip_port, public_key, dht->self_public_key);
1852+
if (id_equal(public_key, dht->self_public_key)) {
1853+
// Bootstrapping off ourselves is ok (onion paths are still set up).
1854+
return true;
1855+
}
1856+
1857+
return dht_getnodes(dht, ip_port, public_key, dht->self_public_key);
18471858
}
18481859

18491860
int dht_bootstrap_from_address(DHT *dht, const char *address, bool ipv6enabled,
@@ -2682,8 +2693,7 @@ void dht_save(const DHT *dht, uint8_t *data)
26822693
}
26832694
}
26842695

2685-
state_write_section_header(old_data, DHT_STATE_COOKIE_TYPE, pack_nodes(data, sizeof(Node_format) * num, clients, num),
2686-
DHT_STATE_TYPE_NODES);
2696+
state_write_section_header(old_data, DHT_STATE_COOKIE_TYPE, pack_nodes(dht->log, data, sizeof(Node_format) * num, clients, num), DHT_STATE_TYPE_NODES);
26872697

26882698
free(clients);
26892699
}

toxcore/DHT.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ int packed_node_size(Family ip_family);
148148
* Return -1 on failure.
149149
*/
150150
non_null()
151-
int pack_ip_port(uint8_t *data, uint16_t length, const IP_Port *ip_port);
151+
int pack_ip_port(const Logger *logger, uint8_t *data, uint16_t length, const IP_Port *ip_port);
152152

153153
/** Unpack IP_Port structure from data of max size length into ip_port.
154154
*
@@ -166,7 +166,7 @@ int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length, bool
166166
* return -1 on failure.
167167
*/
168168
non_null()
169-
int pack_nodes(uint8_t *data, uint16_t length, const Node_format *nodes, uint16_t number);
169+
int pack_nodes(const Logger *logger, uint8_t *data, uint16_t length, const Node_format *nodes, uint16_t number);
170170

171171
/** Unpack data of length into nodes of size max_num_nodes.
172172
* Put the length of the data processed in processed_data_len.
@@ -359,7 +359,7 @@ void do_dht(DHT *dht);
359359
* to setup connections
360360
*/
361361
non_null()
362-
void dht_bootstrap(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key);
362+
bool dht_bootstrap(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key);
363363

364364
/** Resolves address into an IP address. If successful, sends a "get nodes"
365365
* request to the given node with ip, port and public_key to setup connections

toxcore/Messenger.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,6 @@ int m_send_message_generic(Messenger *m, int32_t friendnumber, uint8_t type, con
533533
m->friendlist[friendnumber].friendcon_id), packet, length + 1, false);
534534

535535
if (packet_num == -1) {
536-
LOGGER_WARNING(m->log, "failed to write crypto packet for message of length %d to friend %d",
537-
length, friendnumber);
538536
return -4;
539537
}
540538

@@ -3008,7 +3006,7 @@ static uint8_t *save_tcp_relays(const Messenger *m, uint8_t *data)
30083006
uint32_t num = m->num_loaded_relays;
30093007
num += copy_connected_tcp_relays(m->net_crypto, relays + num, NUM_SAVED_TCP_RELAYS - num);
30103008

3011-
const int l = pack_nodes(data, NUM_SAVED_TCP_RELAYS * packed_node_size(net_family_tcp_ipv6), relays, num);
3009+
const int l = pack_nodes(m->log, data, NUM_SAVED_TCP_RELAYS * packed_node_size(net_family_tcp_ipv6), relays, num);
30123010

30133011
if (l > 0) {
30143012
const uint32_t len = l;
@@ -3052,7 +3050,7 @@ static uint8_t *save_path_nodes(const Messenger *m, uint8_t *data)
30523050
data = state_write_section_header(data, STATE_COOKIE_TYPE, 0, STATE_TYPE_PATH_NODE);
30533051
memset(nodes, 0, sizeof(nodes));
30543052
const unsigned int num = onion_backup_nodes(m->onion_c, nodes, NUM_SAVED_PATH_NODES);
3055-
const int l = pack_nodes(data, NUM_SAVED_PATH_NODES * packed_node_size(net_family_tcp_ipv6), nodes, num);
3053+
const int l = pack_nodes(m->log, data, NUM_SAVED_PATH_NODES * packed_node_size(net_family_tcp_ipv6), nodes, num);
30563054

30573055
if (l > 0) {
30583056
const uint32_t len = l;

toxcore/friend_connection.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ static unsigned int send_relays(Friend_Connections *fr_c, int friendcon_id)
298298
friend_add_tcp_relay(fr_c, friendcon_id, &nodes[i].ip_port, nodes[i].public_key);
299299
}
300300

301-
int length = pack_nodes(data + 1, sizeof(data) - 1, nodes, n);
301+
int length = pack_nodes(fr_c->logger, data + 1, sizeof(data) - 1, nodes, n);
302302

303303
if (length <= 0) {
304304
return 0;

toxcore/network.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,12 @@ int send_packet(const Networking_Core *net, const IP_Port *ip_port, Packet packe
558558
{
559559
IP_Port ipp_copy = *ip_port;
560560

561+
if (net_family_is_unspec(ip_port->ip.family)) {
562+
// TODO(iphydf): Make this an error. Currently this fails sometimes when
563+
// called from DHT.c:do_ping_and_sendnode_requests.
564+
return -1;
565+
}
566+
561567
if (net_family_is_unspec(net->family)) { /* Socket not initialized */
562568
// TODO(iphydf): Make this an error. Currently, the onion client calls
563569
// this via DHT getnodes.
@@ -611,9 +617,7 @@ int send_packet(const Networking_Core *net, const IP_Port *ip_port, Packet packe
611617
addr6->sin6_flowinfo = 0;
612618
addr6->sin6_scope_id = 0;
613619
} else {
614-
// TODO(iphydf): Make this an error. Currently this fails sometimes when
615-
// called from DHT.c:do_ping_and_sendnode_requests.
616-
LOGGER_WARNING(net->log, "unknown address type: %d", ipp_copy.ip.family.value);
620+
LOGGER_ERROR(net->log, "unknown address type: %d", ipp_copy.ip.family.value);
617621
return -1;
618622
}
619623

@@ -743,7 +747,9 @@ void networking_poll(const Networking_Core *net, void *userdata)
743747
const Packet_Handler *const handler = &net->packethandlers[data[0]];
744748

745749
if (handler->function == nullptr) {
746-
LOGGER_WARNING(net->log, "[%02u] -- Packet has no handler", data[0]);
750+
// TODO(https://github.com/TokTok/c-toxcore/issues/1115): Make this
751+
// a warning or error again.
752+
LOGGER_DEBUG(net->log, "[%02u] -- Packet has no handler", data[0]);
747753
continue;
748754
}
749755

@@ -962,8 +968,11 @@ Networking_Core *new_networking_ex(const Logger *log, const IP *ip, uint16_t por
962968

963969
if (net_family_is_ipv6(ip->family)) {
964970
const bool is_dualstack = set_socket_dualstack(temp->sock);
965-
LOGGER_DEBUG(log, "Dual-stack socket: %s",
966-
is_dualstack ? "enabled" : "Failed to enable, won't be able to receive from/send to IPv4 addresses");
971+
if (is_dualstack) {
972+
LOGGER_TRACE(log, "Dual-stack socket: enabled");
973+
} else {
974+
LOGGER_ERROR(log, "Dual-stack socket failed to enable, won't be able to receive from/send to IPv4 addresses");
975+
}
967976
/* multicast local nodes */
968977
struct ipv6_mreq mreq;
969978
memset(&mreq, 0, sizeof(mreq));
@@ -978,9 +987,9 @@ Networking_Core *new_networking_ex(const Logger *log, const IP *ip, uint16_t por
978987
char *strerror = net_new_strerror(neterror);
979988

980989
if (res < 0) {
981-
LOGGER_DEBUG(log, "Failed to activate local multicast membership. (%d, %s)", neterror, strerror);
990+
LOGGER_INFO(log, "Failed to activate local multicast membership in FF02::1. (%d, %s)", neterror, strerror);
982991
} else {
983-
LOGGER_DEBUG(log, "Local multicast group FF02::1 joined successfully. (%d, %s)", neterror, strerror);
992+
LOGGER_TRACE(log, "Local multicast group joined successfully. (%d, %s)", neterror, strerror);
984993
}
985994

986995
net_kill_strerror(strerror);

toxcore/onion_announce.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ static int handle_announce_request(void *object, const IP_Port *source, const ui
445445
int nodes_length = 0;
446446

447447
if (num_nodes != 0) {
448-
nodes_length = pack_nodes(pl + 1 + ONION_PING_ID_SIZE, sizeof(nodes_list), nodes_list, num_nodes);
448+
nodes_length = pack_nodes(onion_a->log, pl + 1 + ONION_PING_ID_SIZE, sizeof(nodes_list), nodes_list, num_nodes);
449449

450450
if (nodes_length <= 0) {
451451
return 1;

0 commit comments

Comments
 (0)