DHTEntryPointNameResolveCommand.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. #ifdef ENABLE_ASYNC_DNS
  53. #include "AsyncNameResolver.h"
  54. #endif // ENABLE_ASYNC_DNS
  55. namespace aria2 {
  56. DHTEntryPointNameResolveCommand::DHTEntryPointNameResolveCommand
  57. (cuid_t cuid, DownloadEngine* e,
  58. const std::vector<std::pair<std::string, uint16_t> >& entryPoints):
  59. Command(cuid),
  60. e_(e),
  61. entryPoints_(entryPoints.begin(), entryPoints.end()),
  62. bootstrapEnabled_(false)
  63. {}
  64. DHTEntryPointNameResolveCommand::~DHTEntryPointNameResolveCommand()
  65. {
  66. #ifdef ENABLE_ASYNC_DNS
  67. disableNameResolverCheck(resolver_);
  68. #endif // ENABLE_ASYNC_DNS
  69. }
  70. bool DHTEntryPointNameResolveCommand::execute()
  71. {
  72. if(e_->getRequestGroupMan()->downloadFinished() || e_->isHaltRequested()) {
  73. return true;
  74. }
  75. #ifdef ENABLE_ASYNC_DNS
  76. if(!resolver_) {
  77. int family;
  78. if(e_->getOption()->getAsBool(PREF_ENABLE_ASYNC_DNS6)) {
  79. family = AF_UNSPEC;
  80. } else {
  81. family = AF_INET;
  82. }
  83. resolver_.reset(new AsyncNameResolver(family
  84. #ifdef HAVE_ARES_ADDR_NODE
  85. , e_->getAsyncDNSServers()
  86. #endif // HAVE_ARES_ADDR_NODE
  87. ));
  88. }
  89. #endif // ENABLE_ASYNC_DNS
  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. try {
  96. if(util::isNumericHost(hostname)) {
  97. std::pair<std::string, uint16_t> p
  98. (hostname, entryPoints_.front().second);
  99. resolvedEntryPoints_.push_back(p);
  100. addPingTask(p);
  101. } else if(resolveHostname(hostname, resolver_)) {
  102. hostname = resolver_->getResolvedAddresses().front();
  103. std::pair<std::string, uint16_t> p(hostname,
  104. entryPoints_.front().second);
  105. resolvedEntryPoints_.push_back(p);
  106. addPingTask(p);
  107. } else {
  108. e_->addCommand(this);
  109. return false;
  110. }
  111. } catch(RecoverableException& e) {
  112. A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
  113. }
  114. resolver_->reset();
  115. entryPoints_.pop_front();
  116. }
  117. } else
  118. #endif // ENABLE_ASYNC_DNS
  119. {
  120. NameResolver res;
  121. res.setSocktype(SOCK_DGRAM);
  122. while(!entryPoints_.empty()) {
  123. std::string hostname = entryPoints_.front().first;
  124. try {
  125. std::vector<std::string> addrs;
  126. res.resolve(addrs, hostname);
  127. std::pair<std::string, uint16_t> p(addrs.front(),
  128. entryPoints_.front().second);
  129. resolvedEntryPoints_.push_back(p);
  130. addPingTask(p);
  131. } catch(RecoverableException& e) {
  132. A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
  133. }
  134. entryPoints_.pop_front();
  135. }
  136. }
  137. if(bootstrapEnabled_ && resolvedEntryPoints_.size()) {
  138. taskQueue_->addPeriodicTask1(taskFactory_->createNodeLookupTask
  139. (localNode_->getID()));
  140. taskQueue_->addPeriodicTask1(taskFactory_->createBucketRefreshTask());
  141. }
  142. } catch(RecoverableException& e) {
  143. A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
  144. }
  145. return true;
  146. }
  147. void DHTEntryPointNameResolveCommand::addPingTask
  148. (const std::pair<std::string, uint16_t>& addr)
  149. {
  150. SharedHandle<DHTNode> entryNode(new DHTNode());
  151. entryNode->setIPAddress(addr.first);
  152. entryNode->setPort(addr.second);
  153. taskQueue_->addPeriodicTask1(taskFactory_->createPingTask(entryNode, 10));
  154. }
  155. #ifdef ENABLE_ASYNC_DNS
  156. bool DHTEntryPointNameResolveCommand::resolveHostname
  157. (const std::string& hostname,
  158. const SharedHandle<AsyncNameResolver>& resolver)
  159. {
  160. switch(resolver->getStatus()) {
  161. case AsyncNameResolver::STATUS_READY:
  162. A2_LOG_INFO(fmt(MSG_RESOLVING_HOSTNAME,
  163. getCuid(),
  164. hostname.c_str()));
  165. resolver->resolve(hostname);
  166. setNameResolverCheck(resolver);
  167. return false;
  168. case AsyncNameResolver::STATUS_SUCCESS:
  169. A2_LOG_INFO(fmt(MSG_NAME_RESOLUTION_COMPLETE,
  170. getCuid(),
  171. resolver->getHostname().c_str(),
  172. resolver->getResolvedAddresses().front().c_str()));
  173. return true;
  174. break;
  175. case AsyncNameResolver::STATUS_ERROR:
  176. throw DL_ABORT_EX
  177. (fmt(MSG_NAME_RESOLUTION_FAILED,
  178. getCuid(),
  179. hostname.c_str(),
  180. resolver->getError().c_str()));
  181. default:
  182. return false;
  183. }
  184. }
  185. void DHTEntryPointNameResolveCommand::setNameResolverCheck
  186. (const SharedHandle<AsyncNameResolver>& resolver)
  187. {
  188. e_->addNameResolverCheck(resolver, this);
  189. }
  190. void DHTEntryPointNameResolveCommand::disableNameResolverCheck
  191. (const SharedHandle<AsyncNameResolver>& resolver)
  192. {
  193. e_->deleteNameResolverCheck(resolver, this);
  194. }
  195. #endif // ENABLE_ASYNC_DNS
  196. void DHTEntryPointNameResolveCommand::setBootstrapEnabled(bool f)
  197. {
  198. bootstrapEnabled_ = f;
  199. }
  200. void DHTEntryPointNameResolveCommand::setTaskQueue
  201. (const SharedHandle<DHTTaskQueue>& taskQueue)
  202. {
  203. taskQueue_ = taskQueue;
  204. }
  205. void DHTEntryPointNameResolveCommand::setTaskFactory
  206. (const SharedHandle<DHTTaskFactory>& taskFactory)
  207. {
  208. taskFactory_ = taskFactory;
  209. }
  210. void DHTEntryPointNameResolveCommand::setRoutingTable
  211. (const SharedHandle<DHTRoutingTable>& routingTable)
  212. {
  213. routingTable_ = routingTable;
  214. }
  215. void DHTEntryPointNameResolveCommand::setLocalNode
  216. (const SharedHandle<DHTNode>& localNode)
  217. {
  218. localNode_ = localNode;
  219. }
  220. } // namespace aria2