DHTEntryPointNameResolveCommand.cc 7.4 KB

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