From 84a9d899d53ecb5cf1fb46e718ec1a392e600692 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20B=C3=A9raud?= Date: Fri, 2 Aug 2019 14:40:49 -0400 Subject: [PATCH] dhtrunner: migrate to SockAddr, use default port if not set --- include/opendht/dht.h | 5 +---- include/opendht/dht_interface.h | 3 +-- include/opendht/dht_proxy_client.h | 15 +++++++-------- include/opendht/dhtrunner.h | 4 ++-- include/opendht/network_engine.h | 4 ++-- include/opendht/network_utils.h | 2 ++ include/opendht/node.h | 1 + include/opendht/securedht.h | 7 ++----- src/dht.cpp | 8 ++++---- src/dhtrunner.cpp | 13 ++++++++----- src/node.cpp | 7 +++++++ 11 files changed, 37 insertions(+), 32 deletions(-) diff --git a/include/opendht/dht.h b/include/opendht/dht.h index 37d9b07d..bd45eefa 100644 --- a/include/opendht/dht.h +++ b/include/opendht/dht.h @@ -113,14 +113,11 @@ public: * used to bootstrap efficiently from previously known nodes. */ void insertNode(const InfoHash& id, const SockAddr&) override; - void insertNode(const InfoHash& id, const sockaddr* sa, socklen_t salen) override { - insertNode(id, SockAddr(sa, salen)); - } void insertNode(const NodeExport& n) override { insertNode(n.id, SockAddr(n.ss, n.sslen)); } - void pingNode(const sockaddr*, socklen_t, DoneCallbackSimple&& cb={}) override; + void pingNode(SockAddr, DoneCallbackSimple&& cb={}) override; time_point periodic(const uint8_t *buf, size_t buflen, SockAddr) override; time_point periodic(const uint8_t *buf, size_t buflen, const sockaddr* from, socklen_t fromlen) override { diff --git a/include/opendht/dht_interface.h b/include/opendht/dht_interface.h index ff30a635..b5b3c331 100644 --- a/include/opendht/dht_interface.h +++ b/include/opendht/dht_interface.h @@ -74,10 +74,9 @@ public: * used to bootstrap efficiently from previously known nodes. */ virtual void insertNode(const InfoHash& id, const SockAddr&) = 0; - virtual void insertNode(const InfoHash& id, const sockaddr* sa, socklen_t salen) = 0; virtual void insertNode(const NodeExport& n) = 0; - virtual void pingNode(const sockaddr*, socklen_t, DoneCallbackSimple&& cb={}) = 0; + virtual void pingNode(SockAddr, DoneCallbackSimple&& cb={}) = 0; virtual time_point periodic(const uint8_t *buf, size_t buflen, SockAddr) = 0; virtual time_point periodic(const uint8_t *buf, size_t buflen, const sockaddr* from, socklen_t fromlen) = 0; diff --git a/include/opendht/dht_proxy_client.h b/include/opendht/dht_proxy_client.h index 7c06f56c..89480ba4 100644 --- a/include/opendht/dht_proxy_client.h +++ b/include/opendht/dht_proxy_client.h @@ -204,12 +204,12 @@ public: /** * Get data currently being put at the given hash. */ - std::vector> getPut(const InfoHash&) const; + std::vector> getPut(const InfoHash&) const override; /** * Get data currently being put at the given hash with the given id. */ - Sp getPut(const InfoHash&, const Value::Id&) const; + Sp getPut(const InfoHash&, const Value::Id&) const override; /** * Stop any put/announce operation at the given location, @@ -217,7 +217,7 @@ public: */ bool cancelPut(const InfoHash&, const Value::Id&); - void pingNode(const sockaddr*, socklen_t, DoneCallbackSimple&& /*cb*/={}) { } + void pingNode(SockAddr, DoneCallbackSimple&& /*cb*/={}) override { } virtual void registerType(const ValueType& type) { types.registerType(type); @@ -226,16 +226,15 @@ public: return types.getType(type_id); } - std::vector> getLocal(const InfoHash& k, const Value::Filter& filter) const; - Sp getLocalById(const InfoHash& k, Value::Id id) const; + std::vector> getLocal(const InfoHash& k, const Value::Filter& filter) const override; + Sp getLocalById(const InfoHash& k, Value::Id id) const override; /** * NOTE: The following methods will not be implemented because the * DhtProxyClient doesn't have any storage nor synchronization process */ - void insertNode(const InfoHash&, const SockAddr&) { } - void insertNode(const InfoHash&, const sockaddr*, socklen_t) { } - void insertNode(const NodeExport&) { } + void insertNode(const InfoHash&, const SockAddr&) override { } + void insertNode(const NodeExport&) override { } std::pair getStoreSize() const { return {}; } std::vector exportNodes() const { return {}; } std::vector exportValues() const { return {}; } diff --git a/include/opendht/dhtrunner.h b/include/opendht/dhtrunner.h index 8fed0277..623d1da3 100644 --- a/include/opendht/dhtrunner.h +++ b/include/opendht/dhtrunner.h @@ -255,7 +255,7 @@ public: * Insert known nodes to the routing table, without necessarly ping them. * Usefull to restart a node and get things running fast without putting load on the network. */ - void bootstrap(const std::vector& nodes, DoneCallbackSimple&& cb={}); + void bootstrap(std::vector nodes, DoneCallbackSimple&& cb={}); void bootstrap(const SockAddr& addr, DoneCallbackSimple&& cb={}); /** @@ -365,7 +365,7 @@ public: * @param threaded: If false, ::loop() must be called periodically. Otherwise a thread is launched. * @param cb: Optional callback to receive general state information. */ - void run(in_port_t port = 4222, const crypto::Identity& identity = {}, bool threaded = true, NetId network = 0) { + void run(in_port_t port = dht::net::DHT_DEFAULT_PORT, const crypto::Identity& identity = {}, bool threaded = true, NetId network = 0) { Config config; config.dht_config.node_config.network = network; config.dht_config.id = identity; diff --git a/include/opendht/network_engine.h b/include/opendht/network_engine.h index 1ec4d6fe..f4b0eeec 100644 --- a/include/opendht/network_engine.h +++ b/include/opendht/network_engine.h @@ -275,8 +275,8 @@ public: * @return the request with information concerning its success. */ Sp - sendPing(const sockaddr* sa, socklen_t salen, RequestCb&& on_done, RequestExpiredCb&& on_expired) { - return sendPing(std::make_shared(zeroes, sa, salen), + sendPing(SockAddr&& sa, RequestCb&& on_done, RequestExpiredCb&& on_expired) { + return sendPing(std::make_shared(zeroes, std::move(sa)), std::forward(on_done), std::forward(on_expired)); } diff --git a/include/opendht/network_utils.h b/include/opendht/network_utils.h index 3a2726ba..e5670ce2 100644 --- a/include/opendht/network_utils.h +++ b/include/opendht/network_utils.h @@ -40,6 +40,8 @@ namespace dht { namespace net { +static const constexpr in_port_t DHT_DEFAULT_PORT = 4222; + int bindSocket(const SockAddr& addr, SockAddr& bound); bool setNonblocking(int fd, bool nonblocking = true); diff --git a/include/opendht/node.h b/include/opendht/node.h index cdc622c3..587a1755 100644 --- a/include/opendht/node.h +++ b/include/opendht/node.h @@ -48,6 +48,7 @@ struct Node { const InfoHash id; Node(const InfoHash& id, const SockAddr& addr, bool client=false); + Node(const InfoHash& id, SockAddr&& addr, bool client=false); Node(const InfoHash& id, const sockaddr* sa, socklen_t salen) : Node(id, SockAddr(sa, salen)) {} diff --git a/include/opendht/securedht.h b/include/opendht/securedht.h index 2682ce0e..19389215 100644 --- a/include/opendht/securedht.h +++ b/include/opendht/securedht.h @@ -219,14 +219,11 @@ public: void insertNode(const InfoHash& id, const SockAddr& sa) override { dht_->insertNode(id, sa); } - void insertNode(const InfoHash& id, const sockaddr* sa, socklen_t salen) override { - dht_->insertNode(id, sa, salen); - } void insertNode(const NodeExport& n) override { dht_->insertNode(n); } - void pingNode(const sockaddr* sa, socklen_t salen, DoneCallbackSimple&& cb={}) override { - dht_->pingNode(sa, salen, std::move(cb)); + void pingNode(SockAddr sa, DoneCallbackSimple&& cb={}) override { + dht_->pingNode(std::move(sa), std::move(cb)); } void query(const InfoHash& key, QueryCallback cb, DoneCallback done_cb = {}, Query&& q = {}) override { dht_->query(key, cb, done_cb, std::move(q)); diff --git a/src/dht.cpp b/src/dht.cpp index da431f19..0e7c33bc 100644 --- a/src/dht.cpp +++ b/src/dht.cpp @@ -2070,13 +2070,13 @@ Dht::insertNode(const InfoHash& id, const SockAddr& addr) } void -Dht::pingNode(const sockaddr* sa, socklen_t salen, DoneCallbackSimple&& cb) +Dht::pingNode(SockAddr sa, DoneCallbackSimple&& cb) { scheduler.syncTime(); - DHT_LOG.d("Sending ping to %s", print_addr(sa, salen).c_str()); - auto& count = sa->sa_family == AF_INET ? pending_pings4 : pending_pings6; + DHT_LOG.d("Sending ping to %s", sa.toString().c_str()); + auto& count = sa.getFamily() == AF_INET ? pending_pings4 : pending_pings6; count++; - network_engine.sendPing(sa, salen, [&count,cb](const net::Request&, net::RequestAnswer&&) { + network_engine.sendPing(std::move(sa), [&count,cb](const net::Request&, net::RequestAnswer&&) { count--; if (cb) cb(true); diff --git a/src/dhtrunner.cpp b/src/dhtrunner.cpp index a04311ba..76d1d0c9 100644 --- a/src/dhtrunner.cpp +++ b/src/dhtrunner.cpp @@ -840,19 +840,22 @@ DhtRunner::clearBootstrap() } void -DhtRunner::bootstrap(const std::vector& nodes, DoneCallbackSimple&& cb) +DhtRunner::bootstrap(std::vector nodes, DoneCallbackSimple&& cb) { std::lock_guard lck(storage_mtx); pending_ops_prio.emplace([=](SecureDht& dht) mutable { auto rem = cb ? std::make_shared>(nodes.size(), false) : nullptr; - for (const auto& node : nodes) - dht.pingNode(node.get(), node.getLength(), cb ? [rem,cb](bool ok) { + for (auto& node : nodes) { + if (node.getPort() == 0) + node.setPort(net::DHT_DEFAULT_PORT); + dht.pingNode(std::move(node), cb ? [rem,cb](bool ok) { auto& r = *rem; r.first--; r.second |= ok; if (not r.first) cb(r.second); } : DoneCallbackSimple{}); + } }); cv.notify_all(); } @@ -861,8 +864,8 @@ void DhtRunner::bootstrap(const SockAddr& addr, DoneCallbackSimple&& cb) { std::lock_guard lck(storage_mtx); - pending_ops_prio.emplace([addr,cb](SecureDht& dht) mutable { - dht.pingNode(addr.get(), addr.getLength(), std::move(cb)); + pending_ops_prio.emplace([addr, cb](SecureDht& dht) mutable { + dht.pingNode(std::move(addr), std::move(cb)); }); cv.notify_all(); } diff --git a/src/node.cpp b/src/node.cpp index f1217da8..d2229c2c 100644 --- a/src/node.cpp +++ b/src/node.cpp @@ -37,6 +37,13 @@ Node::Node(const InfoHash& id, const SockAddr& addr, bool client) transaction_id = std::uniform_int_distribution{1}(rd); } +Node::Node(const InfoHash& id, SockAddr&& addr, bool client) +: id(id), addr(std::move(addr)), is_client(client), sockets_() +{ + thread_local crypto::random_device rd {}; + transaction_id = std::uniform_int_distribution{1}(rd); +} + /* This is our definition of a known-good node. */ bool Node::isGood(time_point now) const