DHTEntryPointNameResolveCommand.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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.isNull()) {
  80. _resolver.reset(new AsyncNameResolver());
  81. }
  82. #endif // ENABLE_ASYNC_DNS
  83. try {
  84. #ifdef ENABLE_ASYNC_DNS
  85. if(_e->getOption()->getAsBool(PREF_ASYNC_DNS)) {
  86. while(!_entryPoints.empty()) {
  87. std::string hostname = _entryPoints.front().first;
  88. try {
  89. if(resolveHostname(hostname, _resolver)) {
  90. hostname = _resolver->getResolvedAddresses().front();
  91. std::pair<std::string, uint16_t> p(hostname,
  92. _entryPoints.front().second);
  93. _resolvedEntryPoints.push_back(p);
  94. addPingTask(p);
  95. } else {
  96. _e->addCommand(this);
  97. return false;
  98. }
  99. } catch(RecoverableException& e) {
  100. getLogger()->error(EX_EXCEPTION_CAUGHT, e);
  101. }
  102. _resolver->reset();
  103. _entryPoints.pop_front();
  104. }
  105. } else
  106. #endif // ENABLE_ASYNC_DNS
  107. {
  108. NameResolver res;
  109. res.setSocktype(SOCK_DGRAM);
  110. while(!_entryPoints.empty()) {
  111. std::string hostname = _entryPoints.front().first;
  112. try {
  113. std::vector<std::string> addrs;
  114. res.resolve(addrs, hostname);
  115. std::pair<std::string, uint16_t> p(addrs.front(),
  116. _entryPoints.front().second);
  117. _resolvedEntryPoints.push_back(p);
  118. addPingTask(p);
  119. } catch(RecoverableException& e) {
  120. getLogger()->error(EX_EXCEPTION_CAUGHT, e);
  121. }
  122. _entryPoints.pop_front();
  123. }
  124. }
  125. if(_bootstrapEnabled && _resolvedEntryPoints.size()) {
  126. _taskQueue->addPeriodicTask1(_taskFactory->createNodeLookupTask
  127. (_localNode->getID()));
  128. _taskQueue->addPeriodicTask1(_taskFactory->createBucketRefreshTask());
  129. }
  130. } catch(RecoverableException& e) {
  131. getLogger()->error(EX_EXCEPTION_CAUGHT, e);
  132. }
  133. return true;
  134. }
  135. void DHTEntryPointNameResolveCommand::addPingTask
  136. (const std::pair<std::string, uint16_t>& addr)
  137. {
  138. SharedHandle<DHTNode> entryNode(new DHTNode());
  139. entryNode->setIPAddress(addr.first);
  140. entryNode->setPort(addr.second);
  141. _taskQueue->addPeriodicTask1(_taskFactory->createPingTask(entryNode, 10));
  142. }
  143. #ifdef ENABLE_ASYNC_DNS
  144. bool DHTEntryPointNameResolveCommand::resolveHostname
  145. (const std::string& hostname,
  146. const SharedHandle<AsyncNameResolver>& resolver)
  147. {
  148. switch(resolver->getStatus()) {
  149. case AsyncNameResolver::STATUS_READY:
  150. if(getLogger()->info()) {
  151. getLogger()->info(MSG_RESOLVING_HOSTNAME,
  152. util::itos(getCuid()).c_str(), hostname.c_str());
  153. }
  154. resolver->resolve(hostname);
  155. setNameResolverCheck(resolver);
  156. return false;
  157. case AsyncNameResolver::STATUS_SUCCESS:
  158. if(getLogger()->info()) {
  159. getLogger()->info(MSG_NAME_RESOLUTION_COMPLETE,
  160. util::itos(getCuid()).c_str(),
  161. resolver->getHostname().c_str(),
  162. resolver->getResolvedAddresses().front().c_str());
  163. }
  164. return true;
  165. break;
  166. case AsyncNameResolver::STATUS_ERROR:
  167. throw DL_ABORT_EX
  168. (StringFormat(MSG_NAME_RESOLUTION_FAILED,
  169. util::itos(getCuid()).c_str(),
  170. hostname.c_str(),
  171. resolver->getError().c_str()).str());
  172. default:
  173. return false;
  174. }
  175. }
  176. void DHTEntryPointNameResolveCommand::setNameResolverCheck
  177. (const SharedHandle<AsyncNameResolver>& resolver)
  178. {
  179. _e->addNameResolverCheck(resolver, this);
  180. }
  181. void DHTEntryPointNameResolveCommand::disableNameResolverCheck
  182. (const SharedHandle<AsyncNameResolver>& resolver)
  183. {
  184. _e->deleteNameResolverCheck(resolver, this);
  185. }
  186. #endif // ENABLE_ASYNC_DNS
  187. void DHTEntryPointNameResolveCommand::setBootstrapEnabled(bool f)
  188. {
  189. _bootstrapEnabled = f;
  190. }
  191. void DHTEntryPointNameResolveCommand::setTaskQueue
  192. (const SharedHandle<DHTTaskQueue>& taskQueue)
  193. {
  194. _taskQueue = taskQueue;
  195. }
  196. void DHTEntryPointNameResolveCommand::setTaskFactory
  197. (const SharedHandle<DHTTaskFactory>& taskFactory)
  198. {
  199. _taskFactory = taskFactory;
  200. }
  201. void DHTEntryPointNameResolveCommand::setRoutingTable
  202. (const SharedHandle<DHTRoutingTable>& routingTable)
  203. {
  204. _routingTable = routingTable;
  205. }
  206. void DHTEntryPointNameResolveCommand::setLocalNode
  207. (const SharedHandle<DHTNode>& localNode)
  208. {
  209. _localNode = localNode;
  210. }
  211. } // namespace aria2