diff --git a/include/opendht/dhtrunner.h b/include/opendht/dhtrunner.h index 97a940d6b5549dd9a2291ae4aa562f333d44e232..8a7b07e0ece18ade0039fd95f01a8199befef3f8 100644 --- a/include/opendht/dhtrunner.h +++ b/include/opendht/dhtrunner.h @@ -217,6 +217,22 @@ public: return getNodeId(); } + /** + * Returns the currently bound address. + * @param f: address family of the bound address to retreive. + */ + const Address& getBound(sa_family_t f = AF_INET) const { + return (f == AF_INET) ? bound4 : bound6; + } + + /** + * Returns the currently bound port. + * @param f: address family of the bound port to retreive. + */ + in_port_t getBoundPort(sa_family_t f = AF_INET) const { + return ((sockaddr_in*)&getBound(f).first)->sin_port; + } + std::vector<NodeExport> exportNodes() const { std::lock_guard<std::mutex> lck(dht_mtx); if (!dht_) @@ -393,6 +409,9 @@ private: Dht::Status status4 {Dht::Status::Disconnected}, status6 {Dht::Status::Disconnected}; StatusCallback statusCb {nullptr}; + + Address bound4 {}; + Address bound6 {}; }; } diff --git a/src/dhtrunner.cpp b/src/dhtrunner.cpp index a416a529216a24eab0cda5a11a087e8d846c2ccf..63638c931f8b92f841483111b234b99fb6f349fb 100644 --- a/src/dhtrunner.cpp +++ b/src/dhtrunner.cpp @@ -137,6 +137,8 @@ DhtRunner::join() dht_.reset(); status4 = Dht::Status::Disconnected; status6 = Dht::Status::Disconnected; + bound4 = {}; + bound6 = {}; } } @@ -200,16 +202,21 @@ DhtRunner::doRun(const sockaddr_in* sin4, const sockaddr_in6* sin6, SecureDht::C int s4 = -1, s6 = -1; + + bound4 = {}; if (sin4) { s4 = socket(PF_INET, SOCK_DGRAM, 0); if(s4 >= 0) { int rc = bind(s4, (sockaddr*)sin4, sizeof(sockaddr_in)); if(rc < 0) throw DhtException("Can't bind IPv4 socket on " + dht::print_addr((sockaddr*)sin4, sizeof(sockaddr_in))); + bound4.second = sizeof(bound4.first); + getsockname(s4, (sockaddr*)&bound4.first, &bound4.second); } } #if 1 + bound6 = {}; if (sin6) { s6 = socket(PF_INET6, SOCK_DGRAM, 0); if(s6 >= 0) { @@ -221,6 +228,8 @@ DhtRunner::doRun(const sockaddr_in* sin4, const sockaddr_in6* sin6, SecureDht::C rc = bind(s6, (sockaddr*)sin6, sizeof(sockaddr_in6)); if(rc < 0) throw DhtException("Can't bind IPv6 socket on " + dht::print_addr((sockaddr*)sin6, sizeof(sockaddr_in6))); + bound6.second = sizeof(bound6.first); + getsockname(s6, (sockaddr*)&bound6.first, &bound6.second); } } #endif diff --git a/tools/dhtnode.cpp b/tools/dhtnode.cpp index f4c3f2555b4055a9c71281d3df261cc35d0dc4d0..a340924d119557d6e0950574b512df639a6117bb 100644 --- a/tools/dhtnode.cpp +++ b/tools/dhtnode.cpp @@ -49,7 +49,7 @@ void print_id_req() { } void print_node_info(const DhtRunner& dht, const dht_params& params) { - std::cout << "OpenDht node " << dht.getNodeId() << " running on port " << params.port << std::endl; + std::cout << "OpenDht node " << dht.getNodeId() << " running on port " << dht.getBoundPort() << std::endl; if (params.is_bootstrap_node) std::cout << "Running in bootstrap mode (discouraged)." << std::endl; if (params.generate_identity) diff --git a/tools/tools_common.h b/tools/tools_common.h index 4c7cc750180bc0ec598a52d751171b77693b307a..c722d2eaf6e851bd3a2767e23b0c8f2b6efe2f58 100644 --- a/tools/tools_common.h +++ b/tools/tools_common.h @@ -139,7 +139,7 @@ static const constexpr in_port_t DHT_DEFAULT_PORT = 4222; struct dht_params { bool help {false}; // print help and exit bool log {false}; - in_port_t port {DHT_DEFAULT_PORT}; + in_port_t port {0}; bool is_bootstrap_node {false}; bool generate_identity {false}; std::pair<std::string, std::string> bootstrap {}; @@ -162,7 +162,7 @@ parseArgs(int argc, char **argv) { switch (opt) { case 'p': { int port_arg = atoi(optarg); - if (port_arg > 0 && port_arg < 0x10000) + if (port_arg >= 0 && port_arg < 0x10000) params.port = port_arg; else std::cout << "Invalid port: " << port_arg << std::endl;