AsyncNameResolverMan.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 "AsyncNameResolverMan.h"
  36. #include "AsyncNameResolver.h"
  37. #include "DownloadEngine.h"
  38. #include "Command.h"
  39. #include "message.h"
  40. #include "fmt.h"
  41. #include "LogFactory.h"
  42. namespace aria2 {
  43. AsyncNameResolverMan::AsyncNameResolverMan()
  44. : numResolver_(0),
  45. resolverCheck_(0),
  46. ipv4_(true),
  47. ipv6_(true)
  48. {}
  49. AsyncNameResolverMan::~AsyncNameResolverMan()
  50. {
  51. assert(!resolverCheck_);
  52. }
  53. bool AsyncNameResolverMan::started() const
  54. {
  55. for(size_t i = 0; i < numResolver_; ++i) {
  56. if(asyncNameResolver_[i]) {
  57. return true;
  58. }
  59. }
  60. return false;
  61. }
  62. void AsyncNameResolverMan::startAsync(const std::string& hostname,
  63. DownloadEngine* e,
  64. Command* command)
  65. {
  66. numResolver_ = 0;
  67. if(ipv4_) {
  68. startAsyncFamily(hostname, AF_INET, e, command);
  69. ++numResolver_;
  70. }
  71. if(ipv6_) {
  72. startAsyncFamily(hostname, AF_INET6, e, command);
  73. ++numResolver_;
  74. }
  75. A2_LOG_INFO(fmt(MSG_RESOLVING_HOSTNAME, command->getCuid(),
  76. hostname.c_str()));
  77. }
  78. void AsyncNameResolverMan::startAsyncFamily(const std::string& hostname,
  79. int family,
  80. DownloadEngine* e,
  81. Command* command)
  82. {
  83. asyncNameResolver_[numResolver_].reset
  84. (new AsyncNameResolver(family
  85. #ifdef HAVE_ARES_ADDR_NODE
  86. ,
  87. e->getAsyncDNSServers()
  88. #endif // HAVE_ARES_ADDR_NODE
  89. ));
  90. asyncNameResolver_[numResolver_]->resolve(hostname);
  91. setNameResolverCheck(numResolver_, e, command);
  92. }
  93. void AsyncNameResolverMan::getResolvedAddress(std::vector<std::string>& res)
  94. const
  95. {
  96. for(size_t i = 0; i < numResolver_; ++i) {
  97. if(asyncNameResolver_[i]->getStatus() ==
  98. AsyncNameResolver::STATUS_SUCCESS) {
  99. const std::vector<std::string>& addrs =
  100. asyncNameResolver_[i]->getResolvedAddresses();
  101. res.insert(res.end(), addrs.begin(), addrs.end());
  102. }
  103. }
  104. return;
  105. }
  106. void AsyncNameResolverMan::setNameResolverCheck(DownloadEngine* e,
  107. Command* command)
  108. {
  109. for(size_t i = 0; i < numResolver_; ++i) {
  110. setNameResolverCheck(i, e, command);
  111. }
  112. }
  113. void AsyncNameResolverMan::setNameResolverCheck(size_t index,
  114. DownloadEngine* e,
  115. Command* command)
  116. {
  117. if(asyncNameResolver_[index]) {
  118. assert((resolverCheck_ & (1 << index)) == 0);
  119. resolverCheck_ |= 1 << index;
  120. e->addNameResolverCheck(asyncNameResolver_[index], command);
  121. }
  122. }
  123. void AsyncNameResolverMan::disableNameResolverCheck(DownloadEngine* e,
  124. Command* command)
  125. {
  126. for(size_t i = 0; i < numResolver_; ++i) {
  127. disableNameResolverCheck(i, e, command);
  128. }
  129. }
  130. void AsyncNameResolverMan::disableNameResolverCheck(size_t index,
  131. DownloadEngine* e,
  132. Command* command)
  133. {
  134. if(asyncNameResolver_[index] && (resolverCheck_ & (1 << index))) {
  135. resolverCheck_ &= ~(1 << index);
  136. e->deleteNameResolverCheck(asyncNameResolver_[index], command);
  137. }
  138. }
  139. int AsyncNameResolverMan::getStatus() const
  140. {
  141. size_t error = 0;
  142. for(size_t i = 0; i < numResolver_; ++i) {
  143. switch(asyncNameResolver_[i]->getStatus()) {
  144. case AsyncNameResolver::STATUS_SUCCESS:
  145. return 1;
  146. case AsyncNameResolver::STATUS_ERROR:
  147. ++error;
  148. break;
  149. default:
  150. break;
  151. }
  152. }
  153. return error == numResolver_ ? -1 : 0;
  154. }
  155. const std::string& AsyncNameResolverMan::getLastError() const
  156. {
  157. for(size_t i = 0; i < numResolver_; ++i) {
  158. if(asyncNameResolver_[i]->getStatus() == AsyncNameResolver::STATUS_ERROR) {
  159. // TODO This is not last error chronologically.
  160. return asyncNameResolver_[i]->getError();
  161. }
  162. }
  163. return A2STR::NIL;
  164. }
  165. } // namespace aria2