DHTEntryPointNameResolveCommand.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 "DlAbortEx.h"
  39. #include "prefs.h"
  40. #include "message.h"
  41. #include "util.h"
  42. #include "Option.h"
  43. #include "DHTNode.h"
  44. #include "DHTTaskQueue.h"
  45. #include "DHTTaskFactory.h"
  46. #include "DHTRoutingTable.h"
  47. #include "DHTTask.h"
  48. #include "RequestGroupMan.h"
  49. #include "Logger.h"
  50. #include "LogFactory.h"
  51. #include "fmt.h"
  52. #include "SocketCore.h"
  53. #ifdef ENABLE_ASYNC_DNS
  54. #include "AsyncNameResolverMan.h"
  55. #endif // ENABLE_ASYNC_DNS
  56. namespace aria2 {
  57. DHTEntryPointNameResolveCommand::DHTEntryPointNameResolveCommand
  58. (cuid_t cuid, DownloadEngine* e,
  59. const std::vector<std::pair<std::string, uint16_t> >& entryPoints):
  60. Command(cuid),
  61. e_(e),
  62. asyncNameResolverMan_(new AsyncNameResolverMan()),
  63. entryPoints_(entryPoints.begin(), entryPoints.end()),
  64. numSuccess_(0),
  65. bootstrapEnabled_(false)
  66. {
  67. #ifdef ENABLE_ASYNC_DNS
  68. configureAsyncNameResolverMan(asyncNameResolverMan_.get(), e_->getOption());
  69. #endif // ENABLE_ASYNC_DNS
  70. }
  71. DHTEntryPointNameResolveCommand::~DHTEntryPointNameResolveCommand()
  72. {
  73. #ifdef ENABLE_ASYNC_DNS
  74. asyncNameResolverMan_->disableNameResolverCheck(e_, this);
  75. #endif // ENABLE_ASYNC_DNS
  76. }
  77. bool DHTEntryPointNameResolveCommand::execute()
  78. {
  79. if(e_->getRequestGroupMan()->downloadFinished() || e_->isHaltRequested()) {
  80. return true;
  81. }
  82. try {
  83. #ifdef ENABLE_ASYNC_DNS
  84. if(e_->getOption()->getAsBool(PREF_ASYNC_DNS)) {
  85. while(!entryPoints_.empty()) {
  86. std::string hostname = entryPoints_.front().first;
  87. if(util::isNumericHost(hostname)) {
  88. ++numSuccess_;
  89. std::pair<std::string, uint16_t> p(hostname,
  90. entryPoints_.front().second);
  91. addPingTask(p);
  92. } else {
  93. std::vector<std::string> res;
  94. int rv = resolveHostname(res, hostname);
  95. if(rv == 0) {
  96. e_->addCommand(this);
  97. return false;
  98. } else {
  99. if(rv == 1) {
  100. ++numSuccess_;
  101. std::pair<std::string, uint16_t> p
  102. (res.front(), entryPoints_.front().second);
  103. addPingTask(p);
  104. }
  105. asyncNameResolverMan_->reset(e_, this);
  106. }
  107. }
  108. entryPoints_.pop_front();
  109. }
  110. } else
  111. #endif // ENABLE_ASYNC_DNS
  112. {
  113. NameResolver res;
  114. res.setSocktype(SOCK_DGRAM);
  115. while(!entryPoints_.empty()) {
  116. std::string hostname = entryPoints_.front().first;
  117. try {
  118. std::vector<std::string> addrs;
  119. res.resolve(addrs, hostname);
  120. ++numSuccess_;
  121. std::pair<std::string, uint16_t> p(addrs.front(),
  122. entryPoints_.front().second);
  123. addPingTask(p);
  124. } catch(RecoverableException& e) {
  125. A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
  126. }
  127. entryPoints_.pop_front();
  128. }
  129. }
  130. if(bootstrapEnabled_ && numSuccess_) {
  131. taskQueue_->addPeriodicTask1(taskFactory_->createNodeLookupTask
  132. (localNode_->getID()));
  133. taskQueue_->addPeriodicTask1(taskFactory_->createBucketRefreshTask());
  134. }
  135. } catch(RecoverableException& e) {
  136. A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
  137. }
  138. return true;
  139. }
  140. void DHTEntryPointNameResolveCommand::addPingTask
  141. (const std::pair<std::string, uint16_t>& addr)
  142. {
  143. SharedHandle<DHTNode> entryNode(new DHTNode());
  144. entryNode->setIPAddress(addr.first);
  145. entryNode->setPort(addr.second);
  146. taskQueue_->addPeriodicTask1(taskFactory_->createPingTask(entryNode, 10));
  147. }
  148. #ifdef ENABLE_ASYNC_DNS
  149. int DHTEntryPointNameResolveCommand::resolveHostname
  150. (std::vector<std::string>& res, const std::string& hostname)
  151. {
  152. if(!asyncNameResolverMan_->started()) {
  153. asyncNameResolverMan_->startAsync(hostname, e_, this);
  154. return 0;
  155. } else {
  156. switch(asyncNameResolverMan_->getStatus()) {
  157. case -1:
  158. A2_LOG_INFO
  159. (fmt(MSG_NAME_RESOLUTION_FAILED, getCuid(), hostname.c_str(),
  160. asyncNameResolverMan_->getLastError().c_str()));
  161. return -1;
  162. case 0:
  163. return 0;
  164. case 1:
  165. asyncNameResolverMan_->getResolvedAddress(res);
  166. if(res.empty()) {
  167. A2_LOG_INFO
  168. (fmt(MSG_NAME_RESOLUTION_FAILED, getCuid(), hostname.c_str(),
  169. "No address returned"));
  170. return -1;
  171. } else {
  172. A2_LOG_INFO(fmt(MSG_NAME_RESOLUTION_COMPLETE,
  173. getCuid(), hostname.c_str(), res.front().c_str()));
  174. return 1;
  175. }
  176. }
  177. }
  178. // Unreachable
  179. return 0;
  180. }
  181. #endif // ENABLE_ASYNC_DNS
  182. void DHTEntryPointNameResolveCommand::setBootstrapEnabled(bool f)
  183. {
  184. bootstrapEnabled_ = f;
  185. }
  186. void DHTEntryPointNameResolveCommand::setTaskQueue
  187. (const SharedHandle<DHTTaskQueue>& taskQueue)
  188. {
  189. taskQueue_ = taskQueue;
  190. }
  191. void DHTEntryPointNameResolveCommand::setTaskFactory
  192. (const SharedHandle<DHTTaskFactory>& taskFactory)
  193. {
  194. taskFactory_ = taskFactory;
  195. }
  196. void DHTEntryPointNameResolveCommand::setRoutingTable
  197. (const SharedHandle<DHTRoutingTable>& routingTable)
  198. {
  199. routingTable_ = routingTable;
  200. }
  201. void DHTEntryPointNameResolveCommand::setLocalNode
  202. (const SharedHandle<DHTNode>& localNode)
  203. {
  204. localNode_ = localNode;
  205. }
  206. } // namespace aria2