DHTEntryPointNameResolveCommand.cc 7.3 KB

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