diff --git a/include/opendht/default_types.h b/include/opendht/default_types.h index bef2f659..58595163 100644 --- a/include/opendht/default_types.h +++ b/include/opendht/default_types.h @@ -93,7 +93,7 @@ public: static Value::Filter getFilter() { return Value::Filter::chain( BaseClass::getFilter(), - [](const Value& v){ return v.recipient != InfoHash(); } + [](const Value& v) { return static_cast(v.recipient); } ); } diff --git a/include/opendht/infohash.h b/include/opendht/infohash.h index ab50d5a7..57671186 100644 --- a/include/opendht/infohash.h +++ b/include/opendht/infohash.h @@ -138,7 +138,7 @@ using PkId = h256; */ class OPENDHT_PUBLIC InfoHash final : public std::array { public: - constexpr InfoHash() : std::array() {} + constexpr InfoHash() : std::array({{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}) {} constexpr InfoHash(const std::array& h) : std::array(h) {} InfoHash(const uint8_t* h, size_t h_len) : std::array() { if (h_len < HASH_LEN) @@ -267,6 +267,16 @@ public: return false; } + explicit operator bool() const { + auto a = reinterpret_cast(data()); + auto b = reinterpret_cast(data() + size()); + for (; a != b; a++) { + if (*a) + return true; + } + return false; + } + static inline InfoHash get(const std::string& data) { return get((const uint8_t*)data.data(), data.size()); } diff --git a/include/opendht/value.h b/include/opendht/value.h index 9fd10dfa..6e5a33db 100644 --- a/include/opendht/value.h +++ b/include/opendht/value.h @@ -424,11 +424,11 @@ struct OPENDHT_PUBLIC Value void msgpack_pack_to_sign(Packer& pk) const { bool has_owner = owner && *owner; - pk.pack_map((user_type.empty()?0:1) + (has_owner?(recipient == InfoHash() ? 4 : 5):2)); + pk.pack_map((user_type.empty()?0:1) + (has_owner?(recipient ? 5 : 4):2)); if (has_owner) { // isSigned pk.pack(std::string("seq")); pk.pack(seq); pk.pack(std::string("owner")); owner->msgpack_pack(pk); - if (recipient != InfoHash()) { + if (recipient) { pk.pack(std::string("to")); pk.pack(recipient); } } diff --git a/src/dht.cpp b/src/dht.cpp index 46f8326b..6dec3b26 100644 --- a/src/dht.cpp +++ b/src/dht.cpp @@ -1655,7 +1655,7 @@ Dht::~Dht() Dht::Dht() : store(), scheduler(DHT_LOG), network_engine(DHT_LOG, scheduler) {} Dht::Dht(int s, int s6, Config config) - : myid(config.node_id != zeroes ? config.node_id : InfoHash::getRandom()), + : myid(config.node_id ? config.node_id : InfoHash::getRandom()), is_bootstrap(config.is_bootstrap), maintain_storage(config.maintain_storage), store(), store_quota(), scheduler(DHT_LOG), @@ -2102,7 +2102,7 @@ Dht::onFindNode(Sp node, const InfoHash& target, want_t want) net::RequestAnswer Dht::onGetValues(Sp node, const InfoHash& hash, want_t, const Query& query) { - if (hash == zeroes) { + if (not hash) { DHT_LOG.w("[node %s] Eek! Got get_values with no info_hash", node->toString().c_str()); throw net::DhtProtocolException { net::DhtProtocolException::NON_AUTHORITATIVE_INFORMATION, @@ -2199,7 +2199,7 @@ void Dht::onGetValuesDone(const Sp& node, net::RequestAnswer Dht::onListen(Sp node, const InfoHash& hash, const Blob& token, size_t socket_id, const Query& query) { - if (hash == zeroes) { + if (not hash) { DHT_LOG.w(node->id, "[node %s] listen with no info_hash", node->toString().c_str()); throw net::DhtProtocolException { net::DhtProtocolException::NON_AUTHORITATIVE_INFORMATION, @@ -2238,7 +2238,7 @@ Dht::onAnnounce(Sp n, const time_point& creation_date) { auto& node = *n; - if (hash == zeroes) { + if (not hash) { DHT_LOG.w(node.id, "put with no info_hash"); throw net::DhtProtocolException { net::DhtProtocolException::NON_AUTHORITATIVE_INFORMATION, diff --git a/src/network_engine.cpp b/src/network_engine.cpp index cab07ec1..93a6cf88 100644 --- a/src/network_engine.cpp +++ b/src/network_engine.cpp @@ -206,7 +206,7 @@ NetworkEngine::requestStep(Sp sreq) if (req.isExpired(now)) { DHT_LOG.e(node.id, "[node %s] expired !", node.toString().c_str()); node.setExpired(); - if (node.id == zeroes) + if (not node.id) requests.erase(req.tid); return; } else if (req.attempt_count == 1) { @@ -221,7 +221,7 @@ NetworkEngine::requestStep(Sp sreq) err == EAFNOSUPPORT) { node.setExpired(); - if (node.id == zeroes) + if (not node.id) requests.erase(req.tid); } else { if (err != EAGAIN) { @@ -244,7 +244,7 @@ void NetworkEngine::sendRequest(const Sp& request) { auto& node = request->node; - if (node->id == zeroes) + if (not node->id) requests.emplace(request->tid, request); request->start = scheduler.time(); node->requested(request); @@ -373,7 +373,7 @@ NetworkEngine::processMessage(const uint8_t *buf, size_t buflen, const SockAddr& return; } - if (msg->id == myid || msg->id == zeroes) { + if (msg->id == myid or not msg->id) { DHT_LOG.d("Received message from self"); return; } @@ -426,7 +426,7 @@ NetworkEngine::process(std::unique_ptr&& msg, const SockAddr& fro /* either response for a request or data for an opened socket */ if (not req and not rsocket) { auto req_it = requests.find(msg->tid); - if (req_it != requests.end() and req_it->second->node->id == zeroes) { + if (req_it != requests.end() and not req_it->second->node->id) { req = req_it->second; req->node = node; requests.erase(req_it); @@ -451,7 +451,7 @@ NetworkEngine::process(std::unique_ptr&& msg, const SockAddr& fro switch (msg->type) { case MessageType::Error: { - if (msg->id != zeroes and req and ( + if (msg->id and req and ( (msg->error_code == DhtProtocolException::NOT_FOUND and req->getType() == MessageType::Refresh) or (msg->error_code == DhtProtocolException::UNAUTHORIZED and (req->getType() == MessageType::AnnounceValue or req->getType() == MessageType::Listen)))) diff --git a/src/node_cache.cpp b/src/node_cache.cpp index 485b208b..926768da 100644 --- a/src/node_cache.cpp +++ b/src/node_cache.cpp @@ -33,7 +33,7 @@ NodeCache::getNode(const InfoHash& id, sa_family_t family) { Sp NodeCache::getNode(const InfoHash& id, const SockAddr& addr, time_point now, bool confirm, bool client) { - if (id == zeroes) + if (not id) return std::make_shared(id, addr); return cache(addr.getFamily()).getNode(id, addr, now, confirm, client); } diff --git a/src/securedht.cpp b/src/securedht.cpp index ea15e125..88a1b118 100644 --- a/src/securedht.cpp +++ b/src/securedht.cpp @@ -35,7 +35,7 @@ namespace dht { Config& getConfig(SecureDht::Config& conf) { auto& c = conf.node_config; - if (c.node_id == InfoHash() and conf.id.second) + if (not c.node_id and conf.id.second) c.node_id = InfoHash::get("node:"+conf.id.second->getId().toString()); return c; } diff --git a/src/value.cpp b/src/value.cpp index 90d1aaf3..e3d3aece 100644 --- a/src/value.cpp +++ b/src/value.cpp @@ -41,7 +41,7 @@ std::ostream& operator<< (std::ostream& s, const Value& v) s << "encrypted "; else if (v.isSigned()) { s << "signed (v" << v.seq << ") "; - if (v.recipient != InfoHash()) + if (v.recipient) s << "decrypted "; } if (not v.isEncrypted()) {