Fixed DHT bind address and converted to using sockaddr.

This commit is contained in:
Jari Sundell
2025-08-22 19:54:54 +02:00
committed by GitHub
parent b0c68aa398
commit bb1cdfc0e7
7 changed files with 58 additions and 68 deletions
+10 -4
View File
@@ -31,11 +31,11 @@ apply_dht_add_node(const std::string& arg) {
if (!torrent::dht_controller()->is_valid())
throw torrent::input_error("DHT not enabled.");
int port, ret;
int port;
char dummy;
char host[1024];
ret = std::sscanf(arg.c_str(), "%1023[^:]:%i%c", host, &port, &dummy);
int ret = std::sscanf(arg.c_str(), "%1023[^:]:%i%c", host, &port, &dummy);
if (ret == 1)
port = 6881;
@@ -47,13 +47,18 @@ apply_dht_add_node(const std::string& arg) {
assert(std::this_thread::get_id() == torrent::main_thread::thread()->thread_id());
auto host_str = std::string(host);
// TODO: Move this lookup to DhtController.
// Currently discarding SOCK_STREAM.
torrent::this_thread::resolver()->resolve_specific(nullptr, host, PF_INET, [port](torrent::c_sa_shared_ptr sa, int err) {
torrent::this_thread::resolver()->resolve_specific(nullptr, host_str, PF_INET, [host_str, port](torrent::c_sa_shared_ptr sa, int err) {
if (sa == nullptr) {
lt_log_print(torrent::LOG_DHT_WARN, "Could not resolve host: %s", gai_strerror(err));
lt_log_print(torrent::LOG_DHT_ERROR, "dht.add_node : could not resolve host : %s (%s)", gai_strerror(err), host_str.c_str());
return;
}
lt_log_print(torrent::LOG_DHT_CONTROLLER, "dht.add_node : %s", host_str.c_str());
torrent::dht_controller()->add_node(sa.get(), port);
});
@@ -138,6 +143,7 @@ initialize_command_tracker() {
CMD2_VAR_BOOL ("trackers.use_udp", true);
CMD2_ANY_STRING_V ("dht.mode.set", std::bind(&core::DhtManager::set_mode, control->dht_manager(), std::placeholders::_2));
// TODO: This should query DhtController.
CMD2_VAR_VALUE ("dht.port", int64_t(6881));
CMD2_ANY_STRING ("dht.add_node", std::bind(&apply_dht_add_node, std::placeholders::_2));
CMD2_ANY ("dht.statistics", std::bind(&core::DhtManager::dht_statistics, control->dht_manager()));
+3 -3
View File
@@ -67,14 +67,14 @@ Control::initialize() {
m_core->listen_open();
m_core->download_store()->enable(rpc::call_command_value("session.use_lock"));
m_core->set_hashing_view(*m_viewManager->find_throw("hashing"));
m_dhtManager->set_port(torrent::connection_manager()->listen_port());
m_ui->init(this);
if(!display::Canvas::daemon()) {
if(!display::Canvas::daemon())
m_inputStdin->insert(torrent::this_thread::poll());
}
}
void
+30 -15
View File
@@ -18,8 +18,11 @@
#include "download_store.h"
#include "manager.h"
#define LT_LOG_THIS(log_fmt, ...) \
lt_log_print_subsystem(torrent::LOG_DHT_MANAGER, "dht_manager", log_fmt, __VA_ARGS__);
#define LT_LOG(log_fmt, ...) \
lt_log_print_subsystem(torrent::LOG_DHT_CONTROLLER, "dht_manager", log_fmt, __VA_ARGS__);
#define LT_LOG_ERROR(log_fmt, ...) \
lt_log_print_subsystem(torrent::LOG_DHT_ERROR, "dht_manager", log_fmt, __VA_ARGS__);
namespace core {
@@ -30,10 +33,20 @@ DhtManager::~DhtManager() {
torrent::this_thread::scheduler()->erase(&m_stop_timeout);
}
void
DhtManager::set_port(uint16_t port) {
if (torrent::dht_controller()->is_active()) {
LT_LOG_ERROR("cannot change port while DHT is active", 0);
throw torrent::input_error("cannot change port while DHT is active");
}
m_port = port;
}
void
DhtManager::load_dht_cache() {
if (m_start == dht_disable || !control->core()->download_store()->is_enabled()) {
LT_LOG_THIS("ignoring cache file", 0);
LT_LOG("ignoring cache file", 0);
return;
}
@@ -48,14 +61,14 @@ DhtManager::load_dht_cache() {
// If the cache file is corrupted we will just discard it with an
// error message.
if (cache_stream.fail()) {
LT_LOG_THIS("cache file corrupted, discarding (path:%s)", cache_filename.c_str());
LT_LOG_ERROR("cache file corrupted, discarding (path:%s)", cache_filename.c_str());
cache = torrent::Object::create_map();
} else {
LT_LOG_THIS("cache file read (path:%s)", cache_filename.c_str());
LT_LOG("cache file read (path:%s)", cache_filename.c_str());
}
} else {
LT_LOG_THIS("could not open cache file (path:%s)", cache_filename.c_str());
LT_LOG("could not open cache file (path:%s)", cache_filename.c_str());
}
torrent::dht_controller()->initialize(cache);
@@ -69,12 +82,12 @@ DhtManager::start_dht() {
torrent::this_thread::scheduler()->erase(&m_stop_timeout);
if (!torrent::dht_controller()->is_valid()) {
LT_LOG_THIS("server start skipped, manager is uninitialized", 0);
LT_LOG_ERROR("server start skipped, manager is uninitialized", 0);
return;
}
if (torrent::dht_controller()->is_active()) {
LT_LOG_THIS("server start skipped, already active", 0);
LT_LOG_ERROR("server start skipped, already active", 0);
return;
}
@@ -82,12 +95,14 @@ DhtManager::start_dht() {
torrent::dht_controller()->set_upload_throttle(throttles.first);
torrent::dht_controller()->set_download_throttle(throttles.second);
int port = rpc::call_command_value("dht.port");
if (port <= 0)
if (m_port <= 0) {
LT_LOG("server start skipped, port not set", 0);
return;
}
if (!torrent::dht_controller()->start(port)) {
LT_LOG("starting server : port=%" PRIu16, m_port);
if (!torrent::dht_controller()->start(m_port)) {
m_start = dht_off;
return;
}
@@ -112,7 +127,7 @@ DhtManager::stop_dht() {
torrent::this_thread::scheduler()->erase(&m_stop_timeout);
if (torrent::dht_controller()->is_active()) {
LT_LOG_THIS("stopping server", 0);
LT_LOG("stopping server", 0);
log_statistics(true);
torrent::dht_controller()->stop();
@@ -196,7 +211,7 @@ DhtManager::log_statistics(bool force) {
// We should have had clients ping us at least but have received
// nothing, that means the UDP port is probably unreachable.
if (torrent::dht_controller()->is_receiving_requests())
LT_LOG_THIS("listening port appears to be unreachable, no queries received", 0);
LT_LOG("listening port appears to be unreachable, no queries received", 0);
torrent::dht_controller()->set_receive_requests(false);
}
@@ -204,7 +219,7 @@ DhtManager::log_statistics(bool force) {
if (stats.queries_sent - m_dhtPrevQueriesSent > stats.num_nodes * 2 + 20 && stats.replies_received == m_dhtPrevRepliesReceived) {
// No replies to over 20 queries plus two per node we have. Probably firewalled.
if (!m_warned)
LT_LOG_THIS("listening port appears to be firewalled, no replies received", 0);
LT_LOG("listening port appears to be firewalled, no replies received", 0);
m_warned = true;
return false;
+5
View File
@@ -10,6 +10,9 @@ class DhtManager {
public:
~DhtManager();
uint16_t port() const { return m_port; }
void set_port(uint16_t port);
void load_dht_cache();
void save_dht_cache();
torrent::Object dht_statistics();
@@ -48,6 +51,8 @@ private:
bool m_warned{};
int m_start{dht_off};
uint16_t m_port{0};
std::string m_throttleName;
};
+3 -7
View File
@@ -159,15 +159,11 @@ Manager::listen_open() {
int portFirst, portLast;
torrent::Object portRange = rpc::call_command("network.port_range");
if (portRange.is_string()) {
if (std::sscanf(portRange.as_string().c_str(), "%i-%i", &portFirst, &portLast) != 2)
throw torrent::input_error("Invalid port_range argument.");
if (!portRange.is_string())
throw torrent::input_error("Invalid port_range argument type.");
// } else if (portRange.is_list()) {
} else {
if (std::sscanf(portRange.as_string().c_str(), "%i-%i", &portFirst, &portLast) != 2)
throw torrent::input_error("Invalid port_range argument.");
}
if (portFirst > portLast || portLast >= (1 << 16))
throw torrent::input_error("Invalid port range.");
+6 -2
View File
@@ -175,8 +175,12 @@ main(int argc, char** argv) {
if (torrent::utils::Thread::should_handle_sigusr1())
SignalHandler::set_handler(SIGUSR1, std::bind(&do_nothing));
torrent::log_add_group_output(torrent::LOG_NOTICE, "important");
torrent::log_add_group_output(torrent::LOG_INFO, "complete");
torrent::log_add_group_output(torrent::LOG_NOTICE, "important");
torrent::log_add_group_output(torrent::LOG_DHT_ERROR, "important");
torrent::log_add_group_output(torrent::LOG_INFO, "complete");
torrent::log_add_group_output(torrent::LOG_DHT_ERROR, "complete");
torrent::log_add_group_output(torrent::LOG_DHT_CONTROLLER, "complete");
torrent::initialize();
torrent::set_main_thread_slots(std::bind(&client_perform));
+1 -37
View File
@@ -1,39 +1,3 @@
// rTorrent - BitTorrent client
// Copyright (C) 2005-2011, Jari Sundell
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// In addition, as a special exception, the copyright holders give
// permission to link the code of portions of this program with the
// OpenSSL library under certain conditions as described in each
// individual source file, and distribute linked combinations
// including the two.
//
// You must obey the GNU General Public License in all respects for
// all of the code used other than OpenSSL. If you modify file(s)
// with this exception, you may extend this exception to your version
// of the file(s), but you are not obligated to do so. If you do not
// wish to do so, delete this exception statement from your version.
// If you delete this exception statement from all source files in the
// program, then also delete it here.
//
// Contact: Jari Sundell <jaris@ifi.uio.no>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY
#ifndef RTORRENT_UI_ELEMENT_LOG_COMPLETE_H
#define RTORRENT_UI_ELEMENT_LOG_COMPLETE_H
@@ -64,7 +28,7 @@ private:
void received_update();
WLogComplete* m_window{};
torrent::log_buffer* m_log;
};