Merge pull request #794 from aria2/fix-dht-node-resolv

Take into account address family when resolving DHT node address
pull/798/head
Tatsuhiro Tsujikawa 2016-12-04 10:31:53 +09:00 committed by GitHub
commit 7a089ae04a
4 changed files with 42 additions and 35 deletions

View File

@ -57,7 +57,7 @@
namespace aria2 { namespace aria2 {
DHTEntryPointNameResolveCommand::DHTEntryPointNameResolveCommand( DHTEntryPointNameResolveCommand::DHTEntryPointNameResolveCommand(
cuid_t cuid, DownloadEngine* e, cuid_t cuid, DownloadEngine* e, int family,
const std::vector<std::pair<std::string, uint16_t>>& entryPoints) const std::vector<std::pair<std::string, uint16_t>>& entryPoints)
: Command{cuid}, : Command{cuid},
e_{e}, e_{e},
@ -68,11 +68,14 @@ DHTEntryPointNameResolveCommand::DHTEntryPointNameResolveCommand(
taskFactory_{nullptr}, taskFactory_{nullptr},
routingTable_{nullptr}, routingTable_{nullptr},
entryPoints_(std::begin(entryPoints), std::end(entryPoints)), entryPoints_(std::begin(entryPoints), std::end(entryPoints)),
family_{family},
numSuccess_{0}, numSuccess_{0},
bootstrapEnabled_{false} bootstrapEnabled_{false}
{ {
#ifdef ENABLE_ASYNC_DNS #ifdef ENABLE_ASYNC_DNS
configureAsyncNameResolverMan(asyncNameResolverMan_.get(), e_->getOption()); configureAsyncNameResolverMan(asyncNameResolverMan_.get(), e_->getOption());
asyncNameResolverMan_->setIPv4(family_ == AF_INET);
asyncNameResolverMan_->setIPv6(family_ == AF_INET6);
#endif // ENABLE_ASYNC_DNS #endif // ENABLE_ASYNC_DNS
} }
@ -93,13 +96,6 @@ bool DHTEntryPointNameResolveCommand::execute()
if (e_->getOption()->getAsBool(PREF_ASYNC_DNS)) { if (e_->getOption()->getAsBool(PREF_ASYNC_DNS)) {
while (!entryPoints_.empty()) { while (!entryPoints_.empty()) {
std::string hostname = entryPoints_.front().first; std::string hostname = entryPoints_.front().first;
if (util::isNumericHost(hostname)) {
++numSuccess_;
std::pair<std::string, uint16_t> p(hostname,
entryPoints_.front().second);
addPingTask(p);
}
else {
std::vector<std::string> res; std::vector<std::string> res;
int rv = resolveHostname(res, hostname); int rv = resolveHostname(res, hostname);
if (rv == 0) { if (rv == 0) {
@ -115,7 +111,6 @@ bool DHTEntryPointNameResolveCommand::execute()
} }
asyncNameResolverMan_->reset(e_, this); asyncNameResolverMan_->reset(e_, this);
} }
}
entryPoints_.pop_front(); entryPoints_.pop_front();
} }
} }
@ -124,9 +119,7 @@ bool DHTEntryPointNameResolveCommand::execute()
{ {
NameResolver res; NameResolver res;
res.setSocktype(SOCK_DGRAM); res.setSocktype(SOCK_DGRAM);
if (e_->getOption()->getAsBool(PREF_DISABLE_IPV6)) { res.setFamily(family_);
res.setFamily(AF_INET);
}
while (!entryPoints_.empty()) { while (!entryPoints_.empty()) {
std::string hostname = entryPoints_.front().first; std::string hostname = entryPoints_.front().first;
try { try {

View File

@ -72,6 +72,8 @@ private:
std::deque<std::pair<std::string, uint16_t>> entryPoints_; std::deque<std::pair<std::string, uint16_t>> entryPoints_;
int family_;
int numSuccess_; int numSuccess_;
bool bootstrapEnabled_; bool bootstrapEnabled_;
@ -85,7 +87,7 @@ private:
public: public:
DHTEntryPointNameResolveCommand( DHTEntryPointNameResolveCommand(
cuid_t cuid, DownloadEngine* e, cuid_t cuid, DownloadEngine* e, int family,
const std::vector<std::pair<std::string, uint16_t>>& entryPoints); const std::vector<std::pair<std::string, uint16_t>>& entryPoints);
virtual ~DHTEntryPointNameResolveCommand(); virtual ~DHTEntryPointNameResolveCommand();

View File

@ -195,7 +195,7 @@ DHTSetup::setup(DownloadEngine* e, int family)
std::vector<std::pair<std::string, uint16_t>> entryPoints; std::vector<std::pair<std::string, uint16_t>> entryPoints;
entryPoints.push_back(addr); entryPoints.push_back(addr);
auto command = make_unique<DHTEntryPointNameResolveCommand>( auto command = make_unique<DHTEntryPointNameResolveCommand>(
e->newCUID(), e, entryPoints); e->newCUID(), e, family, entryPoints);
command->setBootstrapEnabled(true); command->setBootstrapEnabled(true);
command->setTaskQueue(taskQueue.get()); command->setTaskQueue(taskQueue.get());
command->setTaskFactory(taskFactory.get()); command->setTaskFactory(taskFactory.get());

View File

@ -371,17 +371,29 @@ void RequestGroup::createInitialCommand(
} }
} }
const auto& nodes = torrentAttrs->nodes; const auto& nodes = torrentAttrs->nodes;
// TODO Are nodes in torrent IPv4 only? if (!torrentAttrs->privateTorrent && !nodes.empty()) {
if (!torrentAttrs->privateTorrent && !nodes.empty() && if (DHTRegistry::isInitialized()) {
DHTRegistry::isInitialized()) {
auto command = make_unique<DHTEntryPointNameResolveCommand>( auto command = make_unique<DHTEntryPointNameResolveCommand>(
e->newCUID(), e, nodes); e->newCUID(), e, AF_INET, nodes);
command->setTaskQueue(DHTRegistry::getData().taskQueue.get()); const auto& data = DHTRegistry::getData();
command->setTaskFactory(DHTRegistry::getData().taskFactory.get()); command->setTaskQueue(data.taskQueue.get());
command->setRoutingTable(DHTRegistry::getData().routingTable.get()); command->setTaskFactory(data.taskFactory.get());
command->setLocalNode(DHTRegistry::getData().localNode); command->setRoutingTable(data.routingTable.get());
command->setLocalNode(data.localNode);
e->addCommand(std::move(command)); e->addCommand(std::move(command));
} }
if (DHTRegistry::isInitialized6()) {
auto command = make_unique<DHTEntryPointNameResolveCommand>(
e->newCUID(), e, AF_INET6, nodes);
const auto& data = DHTRegistry::getData6();
command->setTaskQueue(data.taskQueue.get());
command->setTaskFactory(data.taskFactory.get());
command->setRoutingTable(data.routingTable.get());
command->setLocalNode(data.localNode);
e->addCommand(std::move(command));
}
}
} }
else if (metadataGetMode) { else if (metadataGetMode) {
A2_LOG_NOTICE(_("For BitTorrent Magnet URI, enabling DHT is strongly" A2_LOG_NOTICE(_("For BitTorrent Magnet URI, enabling DHT is strongly"