diff --git a/include/opendht/dht.h b/include/opendht/dht.h index 061e2219..8d423078 100644 --- a/include/opendht/dht.h +++ b/include/opendht/dht.h @@ -61,9 +61,6 @@ struct LocalListener; */ class OPENDHT_PUBLIC Dht final : public DhtInterface { public: - - Dht(); - /** * Initialise the Dht with two open sockets (for IPv4 and IP6) * and an ID for the node. diff --git a/include/opendht/infohash.h b/include/opendht/infohash.h index bf61483a..f95728e4 100644 --- a/include/opendht/infohash.h +++ b/include/opendht/infohash.h @@ -54,9 +54,9 @@ namespace crypto { } /** - * Represents an InfoHash. - * An InfoHash is a byte array of HASH_LEN bytes. - * InfoHashes identify nodes and values in the Dht. + * Represents an Hash, + * a byte array of N bytes. + * Hashes identify nodes and values in the Dht. */ template class OPENDHT_PUBLIC Hash { @@ -93,6 +93,8 @@ public: iterator end() { return data_.end(); } const_iterator cend() const { return data_.cend(); } + static constexpr inline Hash zero() noexcept { return Hash{}; } + bool operator==(const Hash& h) const { auto a = reinterpret_cast(data_.data()); auto b = reinterpret_cast(h.data_.data()); @@ -112,6 +114,14 @@ public: return false; } + Hash operator^(const Hash& o) const { + Hash result; + for(auto i = 0u; i < N; i++) { + result[i] = data_[i] ^ o.data_[i]; + } + return result; + } + explicit operator bool() const { auto a = reinterpret_cast(data_.data()); auto b = reinterpret_cast(data_.data() + N); @@ -142,10 +152,6 @@ public: return 8 * i + j; } - /** - * Forget about the ``XOR-metric''. An id is just a path from the - * root of the tree, so bits are numbered from the start. - */ static inline int cmp(const Hash& id1, const Hash& id2) { return std::memcmp(id1.data_.data(), id2.data_.data(), N); } @@ -179,16 +185,12 @@ public: int xorCmp(const Hash& id1, const Hash& id2) const { - for(unsigned i = 0; i < N; i++) { - uint8_t xor1, xor2; + for (unsigned i = 0; i < N; i++) { if(id1.data_[i] == id2.data_[i]) continue; - xor1 = id1.data_[i] ^ data_[i]; - xor2 = id2.data_[i] ^ data_[i]; - if(xor1 < xor2) - return -1; - else - return 1; + uint8_t xor1 = id1.data_[i] ^ data_[i]; + uint8_t xor2 = id2.data_[i] ^ data_[i]; + return (xor1 < xor2) ? -1 : 1; } return 0; } @@ -246,14 +248,6 @@ public: template OPENDHT_PUBLIC friend std::istream& operator>> (std::istream& s, Hash& h); - Hash operator^(const Hash& o) const { - Hash result; - for(auto i = 0u; i < N; i++) { - result[i] = data_[i] ^ o.data_[i]; - } - return result; - } - const char* to_c_str() const; std::string toString() const; @@ -398,8 +392,6 @@ Hash::toString() const return std::string(to_c_str(), N*2); } -const InfoHash zeroes {}; - struct OPENDHT_PUBLIC NodeExport { InfoHash id; sockaddr_storage ss; diff --git a/include/opendht/network_engine.h b/include/opendht/network_engine.h index 3523125f..849d2476 100644 --- a/include/opendht/network_engine.h +++ b/include/opendht/network_engine.h @@ -214,7 +214,6 @@ public: using RequestErrorCb = std::function; using RequestExpiredCb = std::function; - NetworkEngine(const Sp& log, std::mt19937_64& rd, Scheduler& scheduler, std::unique_ptr&& sock); NetworkEngine( InfoHash& myid, NetworkConfig config, @@ -293,7 +292,7 @@ public: */ Sp sendPing(SockAddr&& sa, RequestCb&& on_done, RequestExpiredCb&& on_expired) { - return sendPing(std::make_shared(zeroes, std::move(sa), rd), + return sendPing(std::make_shared(InfoHash::zero(), std::move(sa), rd), std::forward(on_done), std::forward(on_expired)); } diff --git a/src/dht.cpp b/src/dht.cpp index 6de05a35..f5a23263 100644 --- a/src/dht.cpp +++ b/src/dht.cpp @@ -1747,8 +1747,6 @@ fromDhtConfig(const Config& config) return netConf; } -Dht::Dht() : store(), network_engine(logger_, rd, scheduler, {}) {} - Dht::Dht(std::unique_ptr&& sock, const Config& config, const Sp& l) : DhtInterface(l), myid(config.node_id ? config.node_id : InfoHash::getRandom(rd)), diff --git a/src/network_engine.cpp b/src/network_engine.cpp index fe518da6..90ec1eba 100644 --- a/src/network_engine.cpp +++ b/src/network_engine.cpp @@ -82,10 +82,6 @@ RequestAnswer::RequestAnswer(ParsedMessage&& msg) nodes6(std::move(msg.nodes6)) {} -NetworkEngine::NetworkEngine(const Sp& log, std::mt19937_64& rand, Scheduler& scheduler, std::unique_ptr&& sock) - : myid(zeroes), dht_socket(std::move(sock)), logger_(log), rd(rand), cache(rd), rate_limiter((size_t)-1), scheduler(scheduler) -{} - NetworkEngine::NetworkEngine(InfoHash& myid, NetworkConfig c, std::unique_ptr&& sock, const Sp& log, @@ -380,7 +376,7 @@ NetworkEngine::isMartian(const SockAddr& addr) const uint8_t* address = (const uint8_t*)&sin6.sin6_addr; return address[0] == 0xFF || (address[0] == 0xFE && (address[1] & 0xC0) == 0x80) || - memcmp(address, zeroes.data(), 16) == 0 || + memcmp(address, InfoHash::zero().data(), 16) == 0 || memcmp(address, v4prefix, 12) == 0; } default: