Compare commits

...
17 changed files with 81 additions and 56 deletions

No files matched your search

+4 -2
View File
@@ -159,7 +159,6 @@ list (APPEND opendht_SOURCES
src/peer_discovery.cpp
src/network_utils.cpp
src/thread_pool.cpp
src/http.cpp
)
list (APPEND opendht_HEADERS
@@ -186,7 +185,6 @@ list (APPEND opendht_HEADERS
include/opendht/peer_discovery.h
include/opendht/thread_pool.h
include/opendht/network_utils.h
include/opendht/http.h
include/opendht.h
)
@@ -230,6 +228,10 @@ if (OPENDHT_PROXY_SERVER OR OPENDHT_PROXY_CLIENT)
endif ()
list (APPEND opendht_HEADERS
include/opendht/proxy.h
include/opendht/http.h
)
list (APPEND opendht_SOURCES
src/http.cpp
)
endif ()
+7 -7
View File
@@ -89,23 +89,23 @@ struct OPENDHT_PUBLIC NodeInfo {
*/
struct OPENDHT_PUBLIC Config {
/** DHT node ID */
InfoHash node_id;
InfoHash node_id {};
/**
* DHT network ID. A node will only talk with other nodes having
* the same network ID.
* Network ID 0 (default) represents the main public network.
*/
NetId network;
NetId network {0};
/** For testing purposes only, enables bootstrap mode */
bool is_bootstrap;
bool is_bootstrap {false};
/** Makes the DHT responsible to maintain its stored values. Consumes more ressources. */
bool maintain_storage;
bool maintain_storage {false};
/** If set, the dht will load its state from this file on start and save its state in this file on shutdown */
std::string persist_path;
std::string persist_path {};
};
/**
@@ -113,8 +113,8 @@ struct OPENDHT_PUBLIC Config {
*/
struct OPENDHT_PUBLIC SecureDhtConfig
{
Config node_config;
crypto::Identity id;
Config node_config {};
crypto::Identity id {};
};
static constexpr size_t DEFAULT_STORAGE_LIMIT {1024 * 1024 * 64};
+1 -1
View File
@@ -122,7 +122,7 @@ public:
void pingNode(const sockaddr*, socklen_t, DoneCallbackSimple&& cb={}) override;
time_point periodic(const uint8_t *buf, size_t buflen, const SockAddr&) 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 {
return periodic(buf, buflen, SockAddr(from, fromlen));
}
+1 -1
View File
@@ -79,7 +79,7 @@ public:
virtual void pingNode(const sockaddr*, socklen_t, DoneCallbackSimple&& cb={}) = 0;
virtual time_point periodic(const uint8_t *buf, size_t buflen, const SockAddr&) = 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;
/**
+1 -1
View File
@@ -196,7 +196,7 @@ public:
*/
void pushNotificationReceived(const std::map<std::string, std::string>& notification);
time_point periodic(const uint8_t*, size_t, const SockAddr&);
time_point periodic(const uint8_t*, size_t, SockAddr);
time_point periodic(const uint8_t *buf, size_t buflen, const sockaddr* from, socklen_t fromlen) {
return periodic(buf, buflen, SockAddr(from, fromlen));
}
+12 -24
View File
@@ -56,12 +56,12 @@ public:
using StatusCallback = std::function<void(NodeStatus, NodeStatus)>;
struct Config {
SecureDhtConfig dht_config;
bool threaded;
std::string proxy_server;
std::string push_node_id;
bool peer_discovery;
bool peer_publish;
SecureDhtConfig dht_config {};
bool threaded {true};
std::string proxy_server {};
std::string push_node_id {};
bool peer_discovery {false};
bool peer_publish {false};
};
struct Context {
@@ -365,24 +365,12 @@ 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) {
run(port, {
/*.dht_config = */{
/*.node_config = */{
/*.node_id = */{},
/*.network = */network,
/*.is_bootstrap = */false,
/*.maintain_storage = */false,
/*.persist_path = */{}
},
/*.id = */identity
},
/*.threaded = */threaded,
/*.proxy_server = */"",
/*.push_node_id = */"",
/*.peer_discovery = */true,
/*.peer_publish = */true,
});
void run(in_port_t port = 4222, const crypto::Identity& identity = {}, bool threaded = true, NetId network = 0) {
Config config;
config.dht_config.node_config.network = network;
config.dht_config.id = identity;
config.threaded = threaded;
run(port, config);
}
void run(in_port_t port, const Config& config, Context&& context = {});
+1 -1
View File
@@ -397,7 +397,7 @@ public:
* @param fromlen The length of the corresponding sockaddr structure.
* @param now The time to adjust the clock in the network engine.
*/
void processMessage(const uint8_t *buf, size_t buflen, const SockAddr& addr);
void processMessage(const uint8_t *buf, size_t buflen, SockAddr addr);
Sp<Node> insertNode(const InfoHash& myid, const SockAddr& addr) {
auto n = cache.getNode(myid, addr, scheduler.time(), 0);
+2 -2
View File
@@ -195,8 +195,8 @@ public:
std::vector<SockAddr> getPublicAddress(sa_family_t family = 0) override {
return dht_->getPublicAddress(family);
}
time_point periodic(const uint8_t *buf, size_t buflen, const SockAddr& sa) override {
return dht_->periodic(buf, buflen, sa);
time_point periodic(const uint8_t *buf, size_t buflen, SockAddr sa) override {
return dht_->periodic(buf, buflen, std::move(sa));
}
time_point periodic(const uint8_t *buf, size_t buflen, const sockaddr* from, socklen_t fromlen) override {
return dht_->periodic(buf, buflen, from, fromlen);
+4 -3
View File
@@ -59,7 +59,7 @@ public:
SockAddr(const SockAddr& o) {
set(o.get(), o.getLength());
}
SockAddr(SockAddr&& o) noexcept : len(o.len), addr(std::move(o.addr)) {
SockAddr(SockAddr&& o) noexcept : addr(std::move(o.addr)), len(o.len) {
o.len = 0;
}
@@ -248,7 +248,8 @@ public:
bool isUnspecified() const;
bool isMappedIPv4() const;
SockAddr getMappedIPv4() const;
SockAddr getMappedIPv4();
SockAddr getMappedIPv6();
/**
* A comparator to classify IP addresses, only considering the
@@ -279,9 +280,9 @@ public:
}
};
private:
socklen_t len {0};
struct free_delete { void operator()(void* p) { ::free(p); } };
std::unique_ptr<sockaddr, free_delete> addr {};
socklen_t len {0};
void set(const sockaddr* sa, socklen_t length) {
if (len != length) {
+2 -2
View File
@@ -49,8 +49,8 @@ public:
return ret->get_future();
}
template<class T>
std::shared_ptr<std::future<T>> getShared(std::function<T()>&& cb) {
return std::make_shared<std::future<T>>(get(std::move(cb)));
std::shared_future<T> getShared(std::function<T()>&& cb) {
return get(std::move(cb));
}
void stop();
+2 -2
View File
@@ -1901,12 +1901,12 @@ Dht::maintainStorage(decltype(store)::value_type& storage, bool force, const Don
}
time_point
Dht::periodic(const uint8_t *buf, size_t buflen, const SockAddr& from)
Dht::periodic(const uint8_t *buf, size_t buflen, SockAddr from)
{
scheduler.syncTime();
if (buflen) {
try {
network_engine.processMessage(buf, buflen, from);
network_engine.processMessage(buf, buflen, std::move(from));
} catch (const std::exception& e) {
DHT_LOG.e("Can't process message from %s: %s", from.toString().c_str(), e.what());
}
+1 -1
View File
@@ -237,7 +237,7 @@ DhtProxyClient::isRunning(sa_family_t af) const
}
time_point
DhtProxyClient::periodic(const uint8_t*, size_t, const SockAddr&)
DhtProxyClient::periodic(const uint8_t*, size_t, SockAddr)
{
// Exec all currently stored callbacks
decltype(callbacks_) callbacks;
+1 -1
View File
@@ -528,7 +528,7 @@ DhtRunner::loop_()
if (clock::now() - pck->received > RX_QUEUE_MAX_DELAY)
dropped++;
else
wakeup = dht->periodic(pck->data.data(), pck->data.size(), pck->from);
wakeup = dht->periodic(pck->data.data(), pck->data.size(), std::move(pck->from));
received.pop();
}
} else {
+2 -1
View File
@@ -357,8 +357,9 @@ NetworkEngine::isNodeBlacklisted(const SockAddr& addr) const
}
void
NetworkEngine::processMessage(const uint8_t *buf, size_t buflen, const SockAddr& from)
NetworkEngine::processMessage(const uint8_t *buf, size_t buflen, SockAddr f)
{
auto from = f.getMappedIPv4();
if (isMartian(from)) {
DHT_LOG.w("Received packet from martian node %s", from.toString().c_str());
return;
+20 -5
View File
@@ -197,10 +197,24 @@ UdpSocket::openSockets(const SockAddr& bind4, const SockAddr& bind6)
#if 1
bound6 = {};
if (bind6) {
try {
s6 = bindSocket(bind6, bound6);
} catch (const DhtException& e) {
logger.e("Can't bind inet6 socket: %s", e.what());
if (bind6.getPort() == 0) {
// Attempt to use the same port as IPv4 with IPv6
if (auto p4 = bound4.getPort()) {
auto b6 = bind6;
b6.setPort(p4);
try {
s6 = bindSocket(b6, bound6);
} catch (const DhtException& e) {
logger.e("Can't bind inet6 socket: %s", e.what());
}
}
}
if (s6 == -1) {
try {
s6 = bindSocket(bind6, bound6);
} catch (const DhtException& e) {
logger.e("Can't bind inet6 socket: %s", e.what());
}
}
}
#endif
@@ -211,6 +225,7 @@ UdpSocket::openSockets(const SockAddr& bind4, const SockAddr& bind6)
running = true;
rcv_thread = std::thread([this, stop_readfd]() {
int selectFd = std::max({s4, s6, stop_readfd}) + 1;
try {
while (running) {
fd_set readfds;
@@ -222,7 +237,6 @@ UdpSocket::openSockets(const SockAddr& bind4, const SockAddr& bind6)
if(s6 >= 0)
FD_SET(s6, &readfds);
int selectFd = std::max({s4, s6, stop_readfd}) + 1;
int rc = select(selectFd, &readfds, nullptr, nullptr, nullptr);
if (rc < 0) {
if (errno != EINTR) {
@@ -280,6 +294,7 @@ UdpSocket::openSockets(const SockAddr& bind4, const SockAddr& bind6)
}
if (s4 < 0 && s6 < 0)
break;
selectFd = std::max({s4, s6, stop_readfd}) + 1;
}
}
}
+18 -2
View File
@@ -201,10 +201,10 @@ SockAddr::isMappedIPv4() const
}
SockAddr
SockAddr::getMappedIPv4() const
SockAddr::getMappedIPv4()
{
if (not isMappedIPv4())
return *this;
return std::move(*this);
SockAddr ret;
ret.setFamily(AF_INET);
ret.setPort(getPort());
@@ -215,6 +215,22 @@ SockAddr::getMappedIPv4() const
return ret;
}
SockAddr
SockAddr::getMappedIPv6()
{
auto family = getFamily();
if (family != AF_INET)
return std::move(*this);
SockAddr ret;
ret.setFamily(AF_INET6);
ret.setPort(getPort());
auto addr4 = reinterpret_cast<const uint8_t*>(&getIPv4().sin_addr);
auto addr6 = reinterpret_cast<uint8_t*>(&ret.getIPv6().sin6_addr);
std::copy(MAPPED_IPV4_PREFIX.begin(), MAPPED_IPV4_PREFIX.end(), addr6);
std::copy_n(addr4, sizeof(in_addr), addr6 + MAPPED_IPV4_PREFIX.size());
return ret;
}
bool operator==(const SockAddr& a, const SockAddr& b) {
return a.equals(b);
}
+2
View File
@@ -303,6 +303,8 @@ void signal_handler(int sig)
case SIGHUP:
break;
case SIGINT:
close(STDIN_FILENO);
// fall through
case SIGTERM:
runner.kill();
break;