浏览代码

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

Take into account address family when resolving DHT node address
Tatsuhiro Tsujikawa 8 年之前
父节点
当前提交
7a089ae04a
共有 4 个文件被更改,包括 42 次插入35 次删除
  1. 16 23
      src/DHTEntryPointNameResolveCommand.cc
  2. 3 1
      src/DHTEntryPointNameResolveCommand.h
  3. 1 1
      src/DHTSetup.cc
  4. 22 10
      src/RequestGroup.cc

+ 16 - 23
src/DHTEntryPointNameResolveCommand.cc

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

+ 3 - 1
src/DHTEntryPointNameResolveCommand.h

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

+ 1 - 1
src/DHTSetup.cc

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

+ 22 - 10
src/RequestGroup.cc

@@ -371,16 +371,28 @@ void RequestGroup::createInitialCommand(
         }
       }
       const auto& nodes = torrentAttrs->nodes;
-      // TODO Are nodes in torrent IPv4 only?
-      if (!torrentAttrs->privateTorrent && !nodes.empty() &&
-          DHTRegistry::isInitialized()) {
-        auto command = make_unique<DHTEntryPointNameResolveCommand>(
-            e->newCUID(), e, nodes);
-        command->setTaskQueue(DHTRegistry::getData().taskQueue.get());
-        command->setTaskFactory(DHTRegistry::getData().taskFactory.get());
-        command->setRoutingTable(DHTRegistry::getData().routingTable.get());
-        command->setLocalNode(DHTRegistry::getData().localNode);
-        e->addCommand(std::move(command));
+      if (!torrentAttrs->privateTorrent && !nodes.empty()) {
+        if (DHTRegistry::isInitialized()) {
+          auto command = make_unique<DHTEntryPointNameResolveCommand>(
+              e->newCUID(), e, AF_INET, nodes);
+          const auto& data = DHTRegistry::getData();
+          command->setTaskQueue(data.taskQueue.get());
+          command->setTaskFactory(data.taskFactory.get());
+          command->setRoutingTable(data.routingTable.get());
+          command->setLocalNode(data.localNode);
+          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) {