AsyncNameResolver.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 "AsyncNameResolver.h"
  36. #include <cstring>
  37. #include "A2STR.h"
  38. #include "LogFactory.h"
  39. #include "SocketCore.h"
  40. #include "util.h"
  41. namespace aria2 {
  42. void callback(void* arg, int status, int timeouts, struct hostent* host)
  43. {
  44. AsyncNameResolver* resolverPtr = reinterpret_cast<AsyncNameResolver*>(arg);
  45. if (status != ARES_SUCCESS) {
  46. resolverPtr->error_ = ares_strerror(status);
  47. resolverPtr->status_ = AsyncNameResolver::STATUS_ERROR;
  48. return;
  49. }
  50. for (char** ap = host->h_addr_list; *ap; ++ap) {
  51. char addrstring[NI_MAXHOST];
  52. if (inetNtop(host->h_addrtype, *ap, addrstring, sizeof(addrstring)) == 0) {
  53. resolverPtr->resolvedAddresses_.push_back(addrstring);
  54. }
  55. }
  56. if (resolverPtr->resolvedAddresses_.empty()) {
  57. resolverPtr->error_ = "no address returned or address conversion failed";
  58. resolverPtr->status_ = AsyncNameResolver::STATUS_ERROR;
  59. }
  60. else {
  61. resolverPtr->status_ = AsyncNameResolver::STATUS_SUCCESS;
  62. }
  63. }
  64. AsyncNameResolver::AsyncNameResolver(int family
  65. #ifdef HAVE_ARES_ADDR_NODE
  66. ,
  67. ares_addr_node* servers
  68. #endif // HAVE_ARES_ADDR_NODE
  69. )
  70. : status_(STATUS_READY), family_(family)
  71. {
  72. // TODO evaluate return value
  73. ares_init(&channel_);
  74. #if defined(HAVE_ARES_SET_SERVERS) && defined(HAVE_ARES_ADDR_NODE)
  75. if (servers) {
  76. // ares_set_servers has been added since c-ares 1.7.1
  77. if (ares_set_servers(channel_, servers) != ARES_SUCCESS) {
  78. A2_LOG_DEBUG("ares_set_servers failed");
  79. }
  80. }
  81. #endif // HAVE_ARES_SET_SERVERS && HAVE_ARES_ADDR_NODE
  82. }
  83. AsyncNameResolver::~AsyncNameResolver() { ares_destroy(channel_); }
  84. void AsyncNameResolver::resolve(const std::string& name)
  85. {
  86. hostname_ = name;
  87. status_ = STATUS_QUERYING;
  88. ares_gethostbyname(channel_, name.c_str(), family_, callback, this);
  89. }
  90. int AsyncNameResolver::getFds(fd_set* rfdsPtr, fd_set* wfdsPtr) const
  91. {
  92. return ares_fds(channel_, rfdsPtr, wfdsPtr);
  93. }
  94. void AsyncNameResolver::process(fd_set* rfdsPtr, fd_set* wfdsPtr)
  95. {
  96. ares_process(channel_, rfdsPtr, wfdsPtr);
  97. }
  98. #ifdef HAVE_LIBCARES
  99. int AsyncNameResolver::getsock(sock_t* sockets) const
  100. {
  101. return ares_getsock(channel_, reinterpret_cast<ares_socket_t*>(sockets),
  102. ARES_GETSOCK_MAXNUM);
  103. }
  104. void AsyncNameResolver::process(ares_socket_t readfd, ares_socket_t writefd)
  105. {
  106. ares_process_fd(channel_, readfd, writefd);
  107. }
  108. #endif // HAVE_LIBCARES
  109. bool AsyncNameResolver::operator==(const AsyncNameResolver& resolver) const
  110. {
  111. return this == &resolver;
  112. }
  113. void AsyncNameResolver::reset()
  114. {
  115. hostname_ = A2STR::NIL;
  116. resolvedAddresses_.clear();
  117. status_ = STATUS_READY;
  118. ares_destroy(channel_);
  119. // TODO evaluate return value
  120. ares_init(&channel_);
  121. }
  122. #ifdef HAVE_ARES_ADDR_NODE
  123. ares_addr_node* parseAsyncDNSServers(const std::string& serversOpt)
  124. {
  125. std::vector<std::string> servers;
  126. util::split(std::begin(serversOpt), std::end(serversOpt),
  127. std::back_inserter(servers), ',', true /* doStrip */);
  128. ares_addr_node root;
  129. root.next = nullptr;
  130. ares_addr_node* tail = &root;
  131. for (const auto& s : servers) {
  132. auto node = make_unique<ares_addr_node>();
  133. size_t len = net::getBinAddr(&node->addr, s.c_str());
  134. if (len != 0) {
  135. node->next = nullptr;
  136. node->family = (len == 4 ? AF_INET : AF_INET6);
  137. tail->next = node.release();
  138. tail = tail->next;
  139. }
  140. }
  141. return root.next;
  142. }
  143. #endif // HAVE_ARES_ADDR_NODE
  144. } // namespace aria2