DHTEntryPointNameResolveCommand.cc 6.5 KB

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