diff --git a/src/kademlia/node_id.cpp b/src/kademlia/node_id.cpp index 92ef04d22..1be063ade 100644 --- a/src/kademlia/node_id.cpp +++ b/src/kademlia/node_id.cpp @@ -104,8 +104,8 @@ node_id generate_id_impl(address const& ip_, boost::uint32_t r) { boost::uint8_t* ip = 0; - const static boost::uint8_t v4mask[] = { 0x01, 0x07, 0x1f, 0x7f }; - const static boost::uint8_t v6mask[] = { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f }; + const static boost::uint8_t v4mask[] = { 0x03, 0x0f, 0x3f, 0xff }; + const static boost::uint8_t v6mask[] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff }; boost::uint8_t const* mask = 0; int num_octets = 0; @@ -141,10 +141,9 @@ node_id generate_id_impl(address const& ip_, boost::uint32_t r) id[0] = (c >> 24) & 0xff; id[1] = (c >> 16) & 0xff; - id[2] = (c >> 8) & 0xff; - id[3] = c & 0xff; + id[2] = ((c >> 8) & 0xf8) | (random() & 0x7); - for (int i = 4; i < 19; ++i) id[i] = random(); + for (int i = 3; i < 19; ++i) id[i] = random(); id[19] = r; return id; @@ -166,7 +165,7 @@ bool verify_id(node_id const& nid, address const& source_ip) if (is_local(source_ip)) return true; node_id h = generate_id_impl(source_ip, nid[19]); - return memcmp(&nid[0], &h[0], 4) == 0; + return nid[0] == h[0] && nid[1] == h[1] && (nid[2] & 0xf8) == (h[2] & 0xf8); } node_id generate_id(address const& ip) diff --git a/test/test_dht.cpp b/test/test_dht.cpp index 784ab44cd..b4c97faba 100644 --- a/test/test_dht.cpp +++ b/test/test_dht.cpp @@ -629,7 +629,7 @@ int test_main() // this is one of the test vectors from: // http://libtorrent.org/dht_sec.html source = udp::endpoint(address::from_string("124.31.75.21"), 20); - node_id nid = to_hash("1712f6c70c5d6a4ec8a88e4c6ab4c28b95eee401"); + node_id nid = to_hash("d2a6dfc70c5d6a4ec8a88e4c6ab4c28b95eee401"); send_dht_request(node, "find_node", source, &response, "10", 0, 0, std::string() , 0, "0101010101010101010101010101010101010101", 0, false, false, std::string(), std::string(), -1, 0, &nid); @@ -1344,21 +1344,23 @@ int test_main() int rs[] = { 1,86,22,65,90 }; - boost::uint8_t prefixes[][4] = + boost::uint8_t prefixes[][3] = { - { 0x17, 0x12, 0xf6, 0xc7 }, - { 0x94, 0x64, 0x06, 0xc1 }, - { 0xfe, 0xfd, 0x92, 0x20 }, - { 0xaf, 0x15, 0x46, 0xdd }, - { 0xa9, 0xe9, 0x20, 0xbf } + { 0xd2, 0xa6, 0xdf }, + { 0x51, 0xd0, 0x29 }, + { 0xfd, 0x33, 0x4a }, + { 0x6a, 0xa1, 0x69 }, + { 0xeb, 0x64, 0x34 } }; for (int i = 0; i < 5; ++i) { address a = address_v4::from_string(ips[i]); node_id id = generate_id_impl(a, rs[i]); - for (int j = 0; j < 4; ++j) - TEST_CHECK(id[j] == prefixes[i][j]); + TEST_CHECK(id[0] == prefixes[i][0]); + TEST_CHECK(id[1] == prefixes[i][1]); + TEST_CHECK((id[2] & 0xf8) == (prefixes[i][2] & 0xf8)); + TEST_CHECK(id[19] == rs[i]); fprintf(stderr, "IP address: %s r: %d node ID: %s\n", ips[i] , rs[i], to_hex(id.to_string()).c_str());