diff options
Diffstat (limited to 'src/routing.c')
| -rw-r--r-- | src/routing.c | 21 |
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; |
