NameResolveCommand.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2013 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 "NameResolveCommand.h"
  36. #include "DownloadEngine.h"
  37. #include "NameResolver.h"
  38. #include "prefs.h"
  39. #include "message.h"
  40. #include "util.h"
  41. #include "Option.h"
  42. #include "RequestGroupMan.h"
  43. #include "Logger.h"
  44. #include "LogFactory.h"
  45. #include "fmt.h"
  46. #include "UDPTrackerRequest.h"
  47. #include "UDPTrackerClient.h"
  48. #include "BtRegistry.h"
  49. #ifdef ENABLE_ASYNC_DNS
  50. # include "AsyncNameResolverMan.h"
  51. #endif // ENABLE_ASYNC_DNS
  52. namespace aria2 {
  53. NameResolveCommand::NameResolveCommand(
  54. cuid_t cuid, DownloadEngine* e,
  55. const std::shared_ptr<UDPTrackerRequest>& req)
  56. : Command(cuid),
  57. e_(e),
  58. #ifdef ENABLE_ASYNC_DNS
  59. asyncNameResolverMan_(make_unique<AsyncNameResolverMan>()),
  60. #endif // ENABLE_ASYNC_DNS
  61. req_(req)
  62. {
  63. #ifdef ENABLE_ASYNC_DNS
  64. configureAsyncNameResolverMan(asyncNameResolverMan_.get(), e_->getOption());
  65. // Currently we only utilize IPv4 DHT for UDP tracker
  66. asyncNameResolverMan_->setIPv6(false);
  67. #endif // ENABLE_ASYNC_DNS
  68. setStatus(Command::STATUS_ONESHOT_REALTIME);
  69. }
  70. NameResolveCommand::~NameResolveCommand()
  71. {
  72. #ifdef ENABLE_ASYNC_DNS
  73. asyncNameResolverMan_->disableNameResolverCheck(e_, this);
  74. #endif // ENABLE_ASYNC_DNS
  75. }
  76. bool NameResolveCommand::execute()
  77. {
  78. // This is UDP tracker specific, but we need to keep this command
  79. // alive until force shutdown is
  80. // commencing. RequestGroupMan::downloadFinished() is useless here
  81. // at the moment.
  82. if (e_->isForceHaltRequested()) {
  83. onShutdown();
  84. return true;
  85. }
  86. const std::string& hostname = req_->remoteAddr;
  87. std::vector<std::string> res;
  88. if (util::isNumericHost(hostname)) {
  89. res.push_back(hostname);
  90. }
  91. else {
  92. #ifdef ENABLE_ASYNC_DNS
  93. if (e_->getOption()->getAsBool(PREF_ASYNC_DNS)) {
  94. if (resolveHostname(res, hostname) == 0) {
  95. e_->addCommand(std::unique_ptr<Command>(this));
  96. return false;
  97. }
  98. }
  99. else
  100. #endif // ENABLE_ASYNC_DNS
  101. {
  102. NameResolver resolver;
  103. resolver.setSocktype(SOCK_DGRAM);
  104. if (e_->getOption()->getAsBool(PREF_DISABLE_IPV6)) {
  105. resolver.setFamily(AF_INET);
  106. }
  107. try {
  108. resolver.resolve(res, hostname);
  109. }
  110. catch (RecoverableException& e) {
  111. A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
  112. }
  113. }
  114. }
  115. if (res.empty()) {
  116. onFailure();
  117. }
  118. else {
  119. onSuccess(res, e_);
  120. }
  121. return true;
  122. }
  123. void NameResolveCommand::onShutdown()
  124. {
  125. req_->state = UDPT_STA_COMPLETE;
  126. req_->error = UDPT_ERR_SHUTDOWN;
  127. }
  128. void NameResolveCommand::onFailure()
  129. {
  130. req_->state = UDPT_STA_COMPLETE;
  131. req_->error = UDPT_ERR_NETWORK;
  132. }
  133. void NameResolveCommand::onSuccess(const std::vector<std::string>& addrs,
  134. DownloadEngine* e)
  135. {
  136. req_->remoteAddr = addrs[0];
  137. e->getBtRegistry()->getUDPTrackerClient()->addRequest(req_);
  138. }
  139. #ifdef ENABLE_ASYNC_DNS
  140. int NameResolveCommand::resolveHostname(std::vector<std::string>& res,
  141. const std::string& hostname)
  142. {
  143. if (!asyncNameResolverMan_->started()) {
  144. asyncNameResolverMan_->startAsync(hostname, e_, this);
  145. }
  146. switch (asyncNameResolverMan_->getStatus()) {
  147. case -1:
  148. A2_LOG_INFO(fmt(MSG_NAME_RESOLUTION_FAILED, getCuid(), hostname.c_str(),
  149. asyncNameResolverMan_->getLastError().c_str()));
  150. return -1;
  151. case 0:
  152. return 0;
  153. case 1:
  154. asyncNameResolverMan_->getResolvedAddress(res);
  155. if (res.empty()) {
  156. A2_LOG_INFO(fmt(MSG_NAME_RESOLUTION_FAILED, getCuid(), hostname.c_str(),
  157. "No address returned"));
  158. return -1;
  159. }
  160. else {
  161. A2_LOG_INFO(fmt(MSG_NAME_RESOLUTION_COMPLETE, getCuid(), hostname.c_str(),
  162. res.front().c_str()));
  163. return 1;
  164. }
  165. }
  166. // Unreachable
  167. return 0;
  168. }
  169. #endif // ENABLE_ASYNC_DNS
  170. } // namespace aria2