From 47d4780eb448f839fc6b0644135395f879080ba4 Mon Sep 17 00:00:00 2001 From: Jesper Jensen Date: Sun, 23 Jul 2023 12:15:34 +0200 Subject: I don't remember --- src/routing.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'src/routing.c') diff --git a/src/routing.c b/src/routing.c index 313a7cc..4851dda 100644 --- a/src/routing.c +++ b/src/routing.c @@ -7,6 +7,7 @@ #include #include #include +#include // The DHT routing table has a keyspace of 0 -- 2^160 split into buckets of 8. // When a bucket becomes full, we split it in half. As we further expand the @@ -53,14 +54,18 @@ void routing_flush() { static uint8_t prefix(struct nodeid* a, struct nodeid* b) { uint8_t c = 0; for(uint8_t i = 0; i < 5; i++) { - uint32_t word = a->inner[i] ^ b->inner[i]; - - // This word is different, find the location of the difference - if(word != 0) - return c + __builtin_clz(word); - - // This word is completely the same - c += sizeof(word) * CHAR_BIT; + // Since the nodeids are stored in host byteorder in the words we have + // to make sure they're big endian before doing the prefix match, + // otherwise we end up with prefix matching that's different from the + // rest of the network + uint32_t word = htonl(a->inner[i]) ^ htonl(b->inner[i]); + + // This word is different, find the location of the difference + if (word != 0) + return c + __builtin_clz(word); + + // This word is completely the same + c += sizeof(word) * CHAR_BIT; } return c; -- cgit v1.2.3