diff --git a/docs/dht_sec.html b/docs/dht_sec.html index 65267d4b0..57d8dcdd1 100644 --- a/docs/dht_sec.html +++ b/docs/dht_sec.html @@ -109,11 +109,11 @@ of IPs, as well as allowing more than one node ID per external IP, the node ID can be restricted at each class level of the IP.

The expression to calculate a valid ID prefix (from an IPv4 address) is:

-sha1((ip & 0x30f3fff) .. r)
+sha1((ip & 0x01071f7f) .. r)
 

And for an IPv6 address (ip is the high 64 bits of the address):

-sha1((ip & 0x103070f1f3f7fff) ..  r)
+sha1((ip & 0x000103070f1f3f7f) ..  r)
 

r is a random number in the range [0, 7]. The resulting integer, representing the masked IP address is supposed to be big-endian before @@ -131,8 +131,8 @@ uint8_t* ip; // our external IPv4 or IPv6 address (network byte order) int num_octets; // the number of octets to consider in ip (4 or 8) uint8_t node_id[20]; // resulting node ID -uint8_t v4mask[] = { 0x03, 0x0f, 0x3f, 0xff }; -uint8_t v6mask[] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff }; +uint8_t v4mask[] = { 0x01, 0x07, 0x1f, 0x7f }; +uint8_t v6mask[] = { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f }; uint8_t* mask = num_octets == 4 ? v4_mask : v8_mask; for (int i = 0; i < num_octets; ++i) @@ -152,11 +152,11 @@ node_id[19] = rand;

 IP           rand  example node ID
 ============ ===== ==========================================
-124.31.75.21   1   8a84ac4d 0c5d6a4ec8a88e4c6ab4c28b95eee4 01
-21.75.31.124  86   1167d8b9 4e7a08645677bbd1cfe7d8f956d532 56
-65.23.51.170  22   508e075d bc8f112a3d426c84764f8c2a1150e6 16
-84.124.73.14  65   095d76cb 1bb1fe518101ceef99462b947a01ff 41
-43.213.53.83  90   f62b5a2f 5b7c4be0237986d5243b87aa6d5130 5a
+124.31.75.21   1   f766f9f5 0c5d6a4ec8a88e4c6ab4c28b95eee4 01
+21.75.31.124  86   7ee04779 4e7a08645677bbd1cfe7d8f956d532 56
+65.23.51.170  22   76a626ff bc8f112a3d426c84764f8c2a1150e6 16
+84.124.73.14  65   beb4e619 1bb1fe518101ceef99462b947a01ff 41
+43.213.53.83  90   ace5613a 5b7c4be0237986d5243b87aa6d5130 5a
 

The bold parts of the node ID are the important parts. The rest are random numbers.

diff --git a/docs/dht_sec.rst b/docs/dht_sec.rst index 8e1176bc2..df5290dc5 100644 --- a/docs/dht_sec.rst +++ b/docs/dht_sec.rst @@ -65,11 +65,11 @@ ID can be restricted at each class level of the IP. The expression to calculate a valid ID prefix (from an IPv4 address) is:: - sha1((ip & 0x30f3fff) .. r) + sha1((ip & 0x01071f7f) .. r) And for an IPv6 address (``ip`` is the high 64 bits of the address):: - sha1((ip & 0x103070f1f3f7fff) .. r) + sha1((ip & 0x000103070f1f3f7f) .. r) ``r`` is a random number in the range [0, 7]. The resulting integer, representing the masked IP address is supposed to be big-endian before @@ -91,8 +91,8 @@ Example code code for calculating a valid node ID:: int num_octets; // the number of octets to consider in ip (4 or 8) uint8_t node_id[20]; // resulting node ID - uint8_t v4mask[] = { 0x03, 0x0f, 0x3f, 0xff }; - uint8_t v6mask[] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff }; + uint8_t v4mask[] = { 0x01, 0x07, 0x1f, 0x7f }; + uint8_t v6mask[] = { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f }; uint8_t* mask = num_octets == 4 ? v4_mask : v8_mask; for (int i = 0; i < num_octets; ++i) @@ -114,11 +114,11 @@ test vectors: IP rand example node ID ============ ===== ========================================== - 124.31.75.21 1 **8a84ac4d** 0c5d6a4ec8a88e4c6ab4c28b95eee4 **01** - 21.75.31.124 86 **1167d8b9** 4e7a08645677bbd1cfe7d8f956d532 **56** - 65.23.51.170 22 **508e075d** bc8f112a3d426c84764f8c2a1150e6 **16** - 84.124.73.14 65 **095d76cb** 1bb1fe518101ceef99462b947a01ff **41** - 43.213.53.83 90 **f62b5a2f** 5b7c4be0237986d5243b87aa6d5130 **5a** + 124.31.75.21 1 **f766f9f5** 0c5d6a4ec8a88e4c6ab4c28b95eee4 **01** + 21.75.31.124 86 **7ee04779** 4e7a08645677bbd1cfe7d8f956d532 **56** + 65.23.51.170 22 **76a626ff** bc8f112a3d426c84764f8c2a1150e6 **16** + 84.124.73.14 65 **beb4e619** 1bb1fe518101ceef99462b947a01ff **41** + 43.213.53.83 90 **ace5613a** 5b7c4be0237986d5243b87aa6d5130 **5a** The bold parts of the node ID are the important parts. The rest are random numbers. diff --git a/docs/ip_id_v4.png b/docs/ip_id_v4.png index c3a209503..7c17deb63 100644 Binary files a/docs/ip_id_v4.png and b/docs/ip_id_v4.png differ diff --git a/docs/ip_id_v6.png b/docs/ip_id_v6.png index c8dba4288..0422adb50 100644 Binary files a/docs/ip_id_v6.png and b/docs/ip_id_v6.png differ diff --git a/docs/ips.py b/docs/ips.py index d2bf9d644..b633aede8 100644 --- a/docs/ips.py +++ b/docs/ips.py @@ -5,15 +5,21 @@ import sys def num_ids(bits, total_bits): - ret = 8; - modulus = 0x100 - mod_shift = 6 * 32 / total_bits - while bits >= 0: - ret *= min(1 << bits, 256) - ret = min(ret, modulus) + if total_bits == 32: + bit_dec = 2 + else: + bit_dec = 1 + + num_used = 7; + ret = 3 + + while bits > 0: + ret += min(num_used, bits) + num_used -= bit_dec + if num_used < 0: num_used = 0 bits -= 8 - modulus <<= mod_shift - return ret + + return 1 << ret f = open('ip_id_v4.dat', 'w+') for i in range(0, 33): diff --git a/src/kademlia/node_id.cpp b/src/kademlia/node_id.cpp index 602423d9a..1dee7840c 100644 --- a/src/kademlia/node_id.cpp +++ b/src/kademlia/node_id.cpp @@ -102,8 +102,8 @@ node_id generate_id_impl(address const& ip_, boost::uint32_t r) { boost::uint8_t* ip = 0; - const static boost::uint8_t v4mask[] = { 0x03, 0x0f, 0x3f, 0xff }; - const static boost::uint8_t v6mask[] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff }; + const static boost::uint8_t v4mask[] = { 0x01, 0x07, 0x1f, 0x7f }; + const static boost::uint8_t v6mask[] = { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f }; boost::uint8_t const* mask = 0; int num_octets = 0; diff --git a/test/test_primitives.cpp b/test/test_primitives.cpp index f2fe53f93..d1f061118 100644 --- a/test/test_primitives.cpp +++ b/test/test_primitives.cpp @@ -1733,11 +1733,11 @@ int test_main() boost::uint8_t prefixes[][4] = { - {0x8a, 0x84, 0xac, 0x4d}, - {0x11, 0x67, 0xd8, 0xb9 }, - {0x50, 0x8e, 0x07, 0x5d }, - {0x09, 0x5d, 0x76, 0xcb }, - {0xf6, 0x2b, 0x5a, 0x2f }, + {0xf7, 0x66, 0xf9, 0xf5}, + {0x7e, 0xe0, 0x47, 0x79 }, + {0x76, 0xa6, 0x26, 0xff }, + {0xbe, 0xb4, 0xe6, 0x19 }, + {0xac, 0xe5, 0x61, 0x3a }, }; for (int i = 0; i < 5; ++i)