DHTEntryPointNameResolveCommand.cc 6.5 KB

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