Skip to content

Commit b66b8de

Browse files
committed
refactor: improve group stability, moderation determinism, and DHT dual-stack handling
* DHT Routing: Fixed dual-stack handling to clear stale IPv4/IPv6 associations when a node's address updates. * Performance: Switched to memcmp for public key/hash comparisons (safe for non-secrets). * Group Chat Reliability: * Added logic to use friend connection IPs as a fallback if DHT discovery is slow, with a fix to prevent overwriting existing valid IPs. * Increased handshake connection limits and fixed full-group sync behavior. * Enforced list sorting and fixed timestamps to ensure consistent moderation state hashes across all peers.
1 parent 4fbd7c1 commit b66b8de

7 files changed

Lines changed: 159 additions & 88 deletions

File tree

toxcore/DHT.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -591,9 +591,18 @@ static bool client_or_ip_port_in_list(const Logger *_Nonnull log, const Mono_Tim
591591

592592
LOGGER_DEBUG(log, "coipil[%u]: switching public_key (ipv%d)", index, ip_version);
593593

594-
/* kill the other address, if it was set */
595-
const IPPTsPng empty_ipptspng = {{{{0}}}};
596-
*assoc = empty_ipptspng;
594+
/* kill the other address, if it was set.
595+
* We just updated `assoc` (which is either assoc4 or assoc6) with the new public_key.
596+
* If there was an association for the other IP version, it's now invalid for this new identity.
597+
*/
598+
if (ip_version == 4) {
599+
const IPPTsPng empty_ipptspng = {{{{0}}}};
600+
list[index].assoc6 = empty_ipptspng;
601+
} else {
602+
const IPPTsPng empty_ipptspng = {{{{0}}}};
603+
list[index].assoc4 = empty_ipptspng;
604+
}
605+
597606
return true;
598607
}
599608

@@ -641,12 +650,6 @@ static void get_close_nodes_inner(uint64_t cur_time, const uint8_t *_Nonnull pub
641650

642651
for (uint32_t i = 0; i < client_list_length; ++i) {
643652
const Client_data *const client = &client_list[i];
644-
645-
/* node already in list? */
646-
if (index_of_node_pk(nodes_list, MAX_SENT_NODES, client->public_key) != UINT32_MAX) {
647-
continue;
648-
}
649-
650653
const IPPTsPng *ipptp;
651654

652655
if (net_family_is_ipv4(sa_family)) {
@@ -677,6 +680,11 @@ static void get_close_nodes_inner(uint64_t cur_time, const uint8_t *_Nonnull pub
677680

678681
#endif /* CHECK_ANNOUNCE_NODE */
679682

683+
/* node already in list? */
684+
if (index_of_node_pk(nodes_list, num_nodes, client->public_key) != UINT32_MAX) {
685+
continue;
686+
}
687+
680688
if (num_nodes < MAX_SENT_NODES) {
681689
memcpy(nodes_list[num_nodes].public_key, client->public_key, CRYPTO_PUBLIC_KEY_SIZE);
682690
nodes_list[num_nodes].ip_port = ipptp->ip_port;

toxcore/crypto_core.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,7 @@ bool crypto_memunlock(void *data, size_t length)
142142

143143
bool pk_equal(const uint8_t pk1[CRYPTO_PUBLIC_KEY_SIZE], const uint8_t pk2[CRYPTO_PUBLIC_KEY_SIZE])
144144
{
145-
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
146-
// Hope that this is better for the fuzzer
147145
return memcmp(pk1, pk2, CRYPTO_PUBLIC_KEY_SIZE) == 0;
148-
#else
149-
return crypto_verify_32(pk1, pk2) == 0;
150-
#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
151146
}
152147

153148
void pk_copy(uint8_t dest[CRYPTO_PUBLIC_KEY_SIZE], const uint8_t src[CRYPTO_PUBLIC_KEY_SIZE])
@@ -167,12 +162,7 @@ bool crypto_sha512_eq(const uint8_t cksum1[CRYPTO_SHA512_SIZE], const uint8_t ck
167162

168163
bool crypto_sha256_eq(const uint8_t cksum1[CRYPTO_SHA256_SIZE], const uint8_t cksum2[CRYPTO_SHA256_SIZE])
169164
{
170-
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
171-
// Hope that this is better for the fuzzer
172165
return memcmp(cksum1, cksum2, CRYPTO_SHA256_SIZE) == 0;
173-
#else
174-
return crypto_verify_32(cksum1, cksum2) == 0;
175-
#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
176166
}
177167

178168
uint8_t random_u08(const Random *rng)

toxcore/group.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3717,6 +3717,10 @@ Group_Chats *new_groupchats(const Mono_Time *mono_time, const Memory *mem, Messe
37173717
/** main groupchats loop. */
37183718
void do_groupchats(Group_Chats *g_c, void *userdata)
37193719
{
3720+
if (g_c == nullptr) {
3721+
return;
3722+
}
3723+
37203724
for (uint16_t i = 0; i < g_c->num_chats; ++i) {
37213725
Group_c *g = get_group_c(g_c, i);
37223726

0 commit comments

Comments
 (0)