DHTEntryPointNameResolveCommand.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "DHTEntryPointNameResolveCommand.h"
  36. #include "DownloadEngine.h"
  37. #include "NameResolver.h"
  38. #include "DlAbortEx.h"
  39. #include "prefs.h"
  40. #include "message.h"
  41. #include "util.h"
  42. #include "Option.h"
  43. #include "DHTNode.h"
  44. #include "DHTTaskQueue.h"
  45. #include "DHTTaskFactory.h"
  46. #include "DHTRoutingTable.h"
  47. #include "DHTTask.h"
  48. #include "RequestGroupMan.h"
  49. #include "Logger.h"
  50. #include "LogFactory.h"
  51. #include "fmt.h"
  52. #include "SocketCore.h"
  53. #ifdef ENABLE_ASYNC_DNS
  54. #include "AsyncNameResolverMan.h"
  55. #endif // ENABLE_ASYNC_DNS
  56. namespace aria2 {
  57. DHTEntryPointNameResolveCommand::DHTEntryPointNameResolveCommand(
  58. cuid_t cuid, DownloadEngine* e, int family,
  59. const std::vector<std::pair<std::string, uint16_t>>& entryPoints)
  60. : Command{cuid},
  61. e_{e},
  62. #ifdef ENABLE_ASYNC_DNS
  63. asyncNameResolverMan_{make_unique<AsyncNameResolverMan>()},
  64. #endif // ENABLE_ASYNC_DNS
  65. taskQueue_{nullptr},
  66. taskFactory_{nullptr},
  67. routingTable_{nullptr},
  68. entryPoints_(std::begin(entryPoints), std::end(entryPoints)),
  69. family_{family},
  70. numSuccess_{0},
  71. bootstrapEnabled_{false}
  72. {
  73. #ifdef ENABLE_ASYNC_DNS
  74. configureAsyncNameResolverMan(asyncNameResolverMan_.get(), e_->getOption());
  75. asyncNameResolverMan_->setIPv4(family_ == AF_INET);
  76. asyncNameResolverMan_->setIPv6(family_ == AF_INET6);
  77. #endif // ENABLE_ASYNC_DNS
  78. }
  79. DHTEntryPointNameResolveCommand::~DHTEntryPointNameResolveCommand()
  80. {
  81. #ifdef ENABLE_ASYNC_DNS
  82. asyncNameResolverMan_->disableNameResolverCheck(e_, this);
  83. #endif // ENABLE_ASYNC_DNS
  84. }
  85. bool DHTEntryPointNameResolveCommand::execute()
  86. {
  87. if (e_->getRequestGroupMan()->downloadFinished() || e_->isHaltRequested()) {
  88. return true;
  89. }
  90. try {
  91. #ifdef ENABLE_ASYNC_DNS
  92. if (e_->getOption()->getAsBool(PREF_ASYNC_DNS)) {
  93. while (!entryPoints_.empty()) {
  94. std::string hostname = entryPoints_.front().first;
  95. std::vector<std::string> res;
  96. int rv = resolveHostname(res, hostname);
  97. if (rv == 0) {
  98. e_->addCommand(std::unique_ptr<Command>(this));
  99. return false;
  100. }
  101. else {
  102. if (rv == 1) {
  103. ++numSuccess_;
  104. std::pair<std::string, uint16_t> p(res.front(),
  105. entryPoints_.front().second);
  106. addPingTask(p);
  107. }
  108. asyncNameResolverMan_->reset(e_, this);
  109. }
  110. entryPoints_.pop_front();
  111. }
  112. }
  113. else
  114. #endif // ENABLE_ASYNC_DNS
  115. {
  116. NameResolver res;
  117. res.setSocktype(SOCK_DGRAM);
  118. res.setFamily(family_);
  119. while (!entryPoints_.empty()) {
  120. std::string hostname = entryPoints_.front().first;
  121. try {
  122. std::vector<std::string> addrs;
  123. res.resolve(addrs, hostname);
  124. ++numSuccess_;
  125. std::pair<std::string, uint16_t> p(addrs.front(),
  126. entryPoints_.front().second);
  127. addPingTask(p);
  128. }
  129. catch (RecoverableException& e) {
  130. A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
  131. }
  132. entryPoints_.pop_front();
  133. }
  134. }
  135. if (bootstrapEnabled_ && numSuccess_) {
  136. taskQueue_->addPeriodicTask1(
  137. taskFactory_->createNodeLookupTask(localNode_->getID()));
  138. taskQueue_->addPeriodicTask1(taskFactory_->createBucketRefreshTask());
  139. }
  140. }
  141. catch (RecoverableException& e) {
  142. A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
  143. }
  144. return true;
  145. }
  146. void DHTEntryPointNameResolveCommand::addPingTask(
  147. const std::pair<std::string, uint16_t>& addr)
  148. {
  149. auto entryNode = std::make_shared<DHTNode>();
  150. entryNode->setIPAddress(addr.first);
  151. entryNode->setPort(addr.second);
  152. taskQueue_->addPeriodicTask1(taskFactory_->createPingTask(entryNode, 10));
  153. }
  154. #ifdef ENABLE_ASYNC_DNS
  155. int DHTEntryPointNameResolveCommand::resolveHostname(
  156. std::vector<std::string>& res, const std::string& hostname)
  157. {
  158. if (!asyncNameResolverMan_->started()) {
  159. asyncNameResolverMan_->startAsync(hostname, e_, this);
  160. }
  161. switch (asyncNameResolverMan_->getStatus()) {
  162. case -1:
  163. A2_LOG_INFO(fmt(MSG_NAME_RESOLUTION_FAILED, getCuid(), hostname.c_str(),
  164. asyncNameResolverMan_->getLastError().c_str()));
  165. return -1;
  166. case 0:
  167. return 0;
  168. case 1:
  169. asyncNameResolverMan_->getResolvedAddress(res);
  170. if (res.empty()) {
  171. A2_LOG_INFO(fmt(MSG_NAME_RESOLUTION_FAILED, getCuid(), hostname.c_str(),
  172. "No address returned"));
  173. return -1;
  174. }
  175. else {
  176. A2_LOG_INFO(fmt(MSG_NAME_RESOLUTION_COMPLETE, getCuid(), hostname.c_str(),
  177. res.front().c_str()));
  178. return 1;
  179. }
  180. }
  181. // Unreachable
  182. return 0;
  183. }
  184. #endif // ENABLE_ASYNC_DNS
  185. void DHTEntryPointNameResolveCommand::setBootstrapEnabled(bool f)
  186. {
  187. bootstrapEnabled_ = f;
  188. }
  189. void DHTEntryPointNameResolveCommand::setTaskQueue(DHTTaskQueue* taskQueue)
  190. {
  191. taskQueue_ = taskQueue;
  192. }
  193. void DHTEntryPointNameResolveCommand::setTaskFactory(
  194. DHTTaskFactory* taskFactory)
  195. {
  196. taskFactory_ = taskFactory;
  197. }
  198. void DHTEntryPointNameResolveCommand::setRoutingTable(
  199. DHTRoutingTable* routingTable)
  200. {
  201. routingTable_ = routingTable;
  202. }
  203. void DHTEntryPointNameResolveCommand::setLocalNode(
  204. const std::shared_ptr<DHTNode>& localNode)
  205. {
  206. localNode_ = localNode;
  207. }
  208. } // namespace aria2