summaryrefslogtreecommitdiff
path: root/src/routing.c
diff options
context:
space:
mode:
authorJesper Jensen <jesper@jnsn.dev>2023-07-23 12:15:34 +0200
committerJesper Jensen <jesper@jnsn.dev>2025-04-12 10:21:20 +0200
commit47d4780eb448f839fc6b0644135395f879080ba4 (patch)
tree2c9a84bf45a4b8af5411331db50173fc5a34f630 /src/routing.c
parent3515701479fe04f73e9194ee3074457bb85030f6 (diff)
I don't remember
Diffstat (limited to 'src/routing.c')
-rw-r--r--src/routing.c21
1 files changed, 13 insertions, 8 deletions
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 <string.h>
#include <stdbool.h>
#include <stdlib.h>
+#include <arpa/inet.h>
// 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;