DHTEntryPointNameResolveCommand.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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,
  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. numSuccess_{0},
  70. bootstrapEnabled_{false}
  71. {
  72. #ifdef ENABLE_ASYNC_DNS
  73. configureAsyncNameResolverMan(asyncNameResolverMan_.get(), e_->getOption());
  74. #endif // ENABLE_ASYNC_DNS
  75. }
  76. DHTEntryPointNameResolveCommand::~DHTEntryPointNameResolveCommand()
  77. {
  78. #ifdef ENABLE_ASYNC_DNS
  79. asyncNameResolverMan_->disableNameResolverCheck(e_, this);
  80. #endif // ENABLE_ASYNC_DNS
  81. }
  82. bool DHTEntryPointNameResolveCommand::execute()
  83. {
  84. if (e_->getRequestGroupMan()->downloadFinished() || e_->isHaltRequested()) {
  85. return true;
  86. }
  87. try {
  88. #ifdef ENABLE_ASYNC_DNS
  89. if (e_->getOption()->getAsBool(PREF_ASYNC_DNS)) {
  90. while (!entryPoints_.empty()) {
  91. std::string hostname = entryPoints_.front().first;
  92. if (util::isNumericHost(hostname)) {
  93. ++numSuccess_;
  94. std::pair<std::string, uint16_t> p(hostname,
  95. entryPoints_.front().second);
  96. addPingTask(p);
  97. }
  98. else {
  99. std::vector<std::string> res;
  100. int rv = resolveHostname(res, hostname);
  101. if (rv == 0) {
  102. e_->addCommand(std::unique_ptr<Command>(this));
  103. return false;
  104. }
  105. else {
  106. if (rv == 1) {
  107. ++numSuccess_;
  108. std::pair<std::string, uint16_t> p(res.front(),
  109. entryPoints_.front().second);
  110. addPingTask(p);
  111. }
  112. asyncNameResolverMan_->reset(e_, this);
  113. }
  114. }
  115. entryPoints_.pop_front();
  116. }
  117. }
  118. else
  119. #endif // ENABLE_ASYNC_DNS
  120. {
  121. NameResolver res;
  122. res.setSocktype(SOCK_DGRAM);
  123. if (e_->getOption()->getAsBool(PREF_DISABLE_IPV6)) {
  124. res.setFamily(AF_INET);
  125. }
  126. while (!entryPoints_.empty()) {
  127. std::string hostname = entryPoints_.front().first;
  128. try {
  129. std::vector<std::string> addrs;
  130. res.resolve(addrs, hostname);
  131. ++numSuccess_;
  132. std::pair<std::string, uint16_t> p(addrs.front(),
  133. entryPoints_.front().second);
  134. addPingTask(p);
  135. }
  136. catch (RecoverableException& e) {
  137. A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
  138. }
  139. entryPoints_.pop_front();
  140. }
  141. }
  142. if (bootstrapEnabled_ && numSuccess_) {
  143. taskQueue_->addPeriodicTask1(
  144. taskFactory_->createNodeLookupTask(localNode_->getID()));
  145. taskQueue_->addPeriodicTask1(taskFactory_->createBucketRefreshTask());
  146. }
  147. }
  148. catch (RecoverableException& e) {
  149. A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
  150. }
  151. return true;
  152. }
  153. void DHTEntryPointNameResolveCommand::addPingTask(
  154. const std::pair<std::string, uint16_t>& addr)
  155. {
  156. auto entryNode = std::make_shared<DHTNode>();
  157. entryNode->setIPAddress(addr.first);
  158. entryNode->setPort(addr.second);
  159. taskQueue_->addPeriodicTask1(taskFactory_->createPingTask(entryNode, 10));
  160. }
  161. #ifdef ENABLE_ASYNC_DNS
  162. int DHTEntryPointNameResolveCommand::resolveHostname(
  163. std::vector<std::string>& res, const std::string& hostname)
  164. {
  165. if (!asyncNameResolverMan_->started()) {
  166. asyncNameResolverMan_->startAsync(hostname, e_, this);
  167. }
  168. switch (asyncNameResolverMan_->getStatus()) {
  169. case -1:
  170. A2_LOG_INFO(fmt(MSG_NAME_RESOLUTION_FAILED, getCuid(), hostname.c_str(),
  171. asyncNameResolverMan_->getLastError().c_str()));
  172. return -1;
  173. case 0:
  174. return 0;
  175. case 1:
  176. asyncNameResolverMan_->getResolvedAddress(res);
  177. if (res.empty()) {
  178. A2_LOG_INFO(fmt(MSG_NAME_RESOLUTION_FAILED, getCuid(), hostname.c_str(),
  179. "No address returned"));
  180. return -1;
  181. }
  182. else {
  183. A2_LOG_INFO(fmt(MSG_NAME_RESOLUTION_COMPLETE, getCuid(), hostname.c_str(),
  184. res.front().c_str()));
  185. return 1;
  186. }
  187. }
  188. // Unreachable
  189. return 0;
  190. }
  191. #endif // ENABLE_ASYNC_DNS
  192. void DHTEntryPointNameResolveCommand::setBootstrapEnabled(bool f)
  193. {
  194. bootstrapEnabled_ = f;
  195. }
  196. void DHTEntryPointNameResolveCommand::setTaskQueue(DHTTaskQueue* taskQueue)
  197. {
  198. taskQueue_ = taskQueue;
  199. }
  200. void DHTEntryPointNameResolveCommand::setTaskFactory(
  201. DHTTaskFactory* taskFactory)
  202. {
  203. taskFactory_ = taskFactory;
  204. }
  205. void DHTEntryPointNameResolveCommand::setRoutingTable(
  206. DHTRoutingTable* routingTable)
  207. {
  208. routingTable_ = routingTable;
  209. }
  210. void DHTEntryPointNameResolveCommand::setLocalNode(
  211. const std::shared_ptr<DHTNode>& localNode)
  212. {
  213. localNode_ = localNode;
  214. }
  215. } // namespace aria2