DHTEntryPointNameResolveCommand.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. namespace aria2 {
  56. DHTEntryPointNameResolveCommand::DHTEntryPointNameResolveCommand(int32_t cuid, DownloadEngine* e, const std::deque<std::pair<std::string, uint16_t> >& entryPoints):
  57. Command(cuid),
  58. _e(e),
  59. _entryPoints(entryPoints),
  60. _bootstrapEnabled(false)
  61. {}
  62. DHTEntryPointNameResolveCommand::~DHTEntryPointNameResolveCommand()
  63. {
  64. #ifdef ENABLE_ASYNC_DNS
  65. disableNameResolverCheck(_resolver);
  66. #endif // ENABLE_ASYNC_DNS
  67. }
  68. bool DHTEntryPointNameResolveCommand::execute()
  69. {
  70. if(_e->_requestGroupMan->downloadFinished() || _e->isHaltRequested()) {
  71. return true;
  72. }
  73. #ifdef ENABLE_ASYNC_DNS
  74. if(_resolver.isNull()) {
  75. _resolver.reset(new AsyncNameResolver());
  76. }
  77. #endif // ENABLE_ASYNC_DNS
  78. try {
  79. #ifdef ENABLE_ASYNC_DNS
  80. if(_e->option->getAsBool(PREF_ASYNC_DNS)) {
  81. while(_entryPoints.size()) {
  82. std::string hostname = _entryPoints.front().first;
  83. try {
  84. if(resolveHostname(hostname, _resolver)) {
  85. hostname = _resolver->getResolvedAddresses().front();
  86. std::pair<std::string, uint16_t> p(hostname,
  87. _entryPoints.front().second);
  88. _resolvedEntryPoints.push_back(p);
  89. addPingTask(p);
  90. } else {
  91. _e->commands.push_back(this);
  92. return false;
  93. }
  94. } catch(RecoverableException& e) {
  95. logger->error(EX_EXCEPTION_CAUGHT, e);
  96. }
  97. _resolver->reset();
  98. _entryPoints.erase(_entryPoints.begin());
  99. }
  100. } else
  101. #endif // ENABLE_ASYNC_DNS
  102. {
  103. NameResolver res;
  104. res.setSocktype(SOCK_DGRAM);
  105. while(_entryPoints.size()) {
  106. std::string hostname = _entryPoints.front().first;
  107. try {
  108. std::deque<std::string> addrs;
  109. res.resolve(addrs, hostname);
  110. std::pair<std::string, uint16_t> p(addrs.front(),
  111. _entryPoints.front().second);
  112. _resolvedEntryPoints.push_back(p);
  113. addPingTask(p);
  114. } catch(RecoverableException& e) {
  115. logger->error(EX_EXCEPTION_CAUGHT, e);
  116. }
  117. _entryPoints.erase(_entryPoints.begin());
  118. }
  119. }
  120. if(_bootstrapEnabled && _resolvedEntryPoints.size()) {
  121. _taskQueue->addPeriodicTask1(_taskFactory->createNodeLookupTask(_localNode->getID()));
  122. _taskQueue->addPeriodicTask1(_taskFactory->createBucketRefreshTask());
  123. }
  124. } catch(RecoverableException& e) {
  125. logger->error(EX_EXCEPTION_CAUGHT, e);
  126. }
  127. return true;
  128. }
  129. void DHTEntryPointNameResolveCommand::addPingTask(const std::pair<std::string, uint16_t>& addr)
  130. {
  131. SharedHandle<DHTNode> entryNode(new DHTNode());
  132. entryNode->setIPAddress(addr.first);
  133. entryNode->setPort(addr.second);
  134. _taskQueue->addPeriodicTask1(_taskFactory->createPingTask(entryNode, 10));
  135. }
  136. #ifdef ENABLE_ASYNC_DNS
  137. bool DHTEntryPointNameResolveCommand::resolveHostname
  138. (const std::string& hostname,
  139. const SharedHandle<AsyncNameResolver>& resolver)
  140. {
  141. switch(resolver->getStatus()) {
  142. case AsyncNameResolver::STATUS_READY:
  143. logger->info(MSG_RESOLVING_HOSTNAME, cuid, hostname.c_str());
  144. resolver->resolve(hostname);
  145. setNameResolverCheck(resolver);
  146. return false;
  147. case AsyncNameResolver::STATUS_SUCCESS:
  148. logger->info(MSG_NAME_RESOLUTION_COMPLETE, cuid,
  149. resolver->getHostname().c_str(),
  150. resolver->getResolvedAddresses().front().c_str());
  151. return true;
  152. break;
  153. case AsyncNameResolver::STATUS_ERROR:
  154. throw DL_ABORT_EX
  155. (StringFormat(MSG_NAME_RESOLUTION_FAILED, cuid,
  156. hostname.c_str(),
  157. resolver->getError().c_str()).str());
  158. default:
  159. return false;
  160. }
  161. }
  162. void DHTEntryPointNameResolveCommand::setNameResolverCheck
  163. (const SharedHandle<AsyncNameResolver>& resolver)
  164. {
  165. _e->addNameResolverCheck(resolver, this);
  166. }
  167. void DHTEntryPointNameResolveCommand::disableNameResolverCheck
  168. (const SharedHandle<AsyncNameResolver>& resolver)
  169. {
  170. _e->deleteNameResolverCheck(resolver, this);
  171. }
  172. #endif // ENABLE_ASYNC_DNS
  173. void DHTEntryPointNameResolveCommand::setBootstrapEnabled(bool f)
  174. {
  175. _bootstrapEnabled = f;
  176. }
  177. void DHTEntryPointNameResolveCommand::setTaskQueue(const SharedHandle<DHTTaskQueue>& taskQueue)
  178. {
  179. _taskQueue = taskQueue;
  180. }
  181. void DHTEntryPointNameResolveCommand::setTaskFactory(const SharedHandle<DHTTaskFactory>& taskFactory)
  182. {
  183. _taskFactory = taskFactory;
  184. }
  185. void DHTEntryPointNameResolveCommand::setRoutingTable(const SharedHandle<DHTRoutingTable>& routingTable)
  186. {
  187. _routingTable = routingTable;
  188. }
  189. void DHTEntryPointNameResolveCommand::setLocalNode(const SharedHandle<DHTNode>& localNode)
  190. {
  191. _localNode = localNode;
  192. }
  193. } // namespace aria2