/* */ #include "AsyncNameResolverMan.h" #include #include "AsyncNameResolver.h" #include "DownloadEngine.h" #include "Command.h" #include "message.h" #include "fmt.h" #include "LogFactory.h" #include "Option.h" #include "SocketCore.h" #include "prefs.h" namespace aria2 { AsyncNameResolverMan::AsyncNameResolverMan() : numResolver_(0), resolverCheck_(0), ipv4_(true), ipv6_(true) { } AsyncNameResolverMan::~AsyncNameResolverMan() { assert(!resolverCheck_); } bool AsyncNameResolverMan::started() const { for (size_t i = 0; i < numResolver_; ++i) { if (asyncNameResolver_[i]) { return true; } } return false; } void AsyncNameResolverMan::startAsync(const std::string& hostname, DownloadEngine* e, Command* command) { numResolver_ = 0; // Set IPv6 resolver first, so that we can push IPv6 address in // front of IPv6 address in getResolvedAddress(). if (ipv6_) { startAsyncFamily(hostname, AF_INET6, e, command); ++numResolver_; } if (ipv4_) { startAsyncFamily(hostname, AF_INET, e, command); ++numResolver_; } A2_LOG_INFO( fmt(MSG_RESOLVING_HOSTNAME, command->getCuid(), hostname.c_str())); } void AsyncNameResolverMan::startAsyncFamily(const std::string& hostname, int family, DownloadEngine* e, Command* command) { asyncNameResolver_[numResolver_] = std::make_shared(family #ifdef HAVE_ARES_ADDR_NODE , e->getAsyncDNSServers() #endif // HAVE_ARES_ADDR_NODE ); asyncNameResolver_[numResolver_]->resolve(hostname); setNameResolverCheck(numResolver_, e, command); } void AsyncNameResolverMan::getResolvedAddress( std::vector& res) const { for (size_t i = 0; i < numResolver_; ++i) { if (asyncNameResolver_[i]->getStatus() == AsyncNameResolver::STATUS_SUCCESS) { auto& addrs = asyncNameResolver_[i]->getResolvedAddresses(); res.insert(std::end(res), std::begin(addrs), std::end(addrs)); } } return; } void AsyncNameResolverMan::setNameResolverCheck(DownloadEngine* e, Command* command) { for (size_t i = 0; i < numResolver_; ++i) { setNameResolverCheck(i, e, command); } } void AsyncNameResolverMan::setNameResolverCheck(size_t index, DownloadEngine* e, Command* command) { if (asyncNameResolver_[index]) { assert((resolverCheck_ & (1 << index)) == 0); resolverCheck_ |= 1 << index; e->addNameResolverCheck(asyncNameResolver_[index], command); } } void AsyncNameResolverMan::disableNameResolverCheck(DownloadEngine* e, Command* command) { for (size_t i = 0; i < numResolver_; ++i) { disableNameResolverCheck(i, e, command); } } void AsyncNameResolverMan::disableNameResolverCheck(size_t index, DownloadEngine* e, Command* command) { if (asyncNameResolver_[index] && (resolverCheck_ & (1 << index))) { resolverCheck_ &= ~(1 << index); e->deleteNameResolverCheck(asyncNameResolver_[index], command); } } int AsyncNameResolverMan::getStatus() const { size_t success = 0; size_t error = 0; bool ipv4Success = false; for (size_t i = 0; i < numResolver_; ++i) { switch (asyncNameResolver_[i]->getStatus()) { case AsyncNameResolver::STATUS_SUCCESS: ++success; if (asyncNameResolver_[i]->getFamily() == AF_INET) { ipv4Success = true; } break; case AsyncNameResolver::STATUS_ERROR: ++error; break; default: break; } } // If we got a IPv4 lookup response, we don't wait for a IPv6 lookup // response. This is because DNS servers may drop AAAA queries and we // have to wait for a long time before timeout. We don't do the // inverse, because, based on today's deployment of DNS servers, // almost all of them can respond to A queries just fine. if ((success && ipv4Success) || success == numResolver_) { return 1; } else if (error == numResolver_) { return -1; } else { return 0; } } const std::string& AsyncNameResolverMan::getLastError() const { for (size_t i = 0; i < numResolver_; ++i) { if (asyncNameResolver_[i]->getStatus() == AsyncNameResolver::STATUS_ERROR) { // TODO This is not last error chronologically. return asyncNameResolver_[i]->getError(); } } return A2STR::NIL; } void AsyncNameResolverMan::reset(DownloadEngine* e, Command* command) { disableNameResolverCheck(e, command); assert(resolverCheck_ == 0); for (size_t i = 0; i < numResolver_; ++i) { asyncNameResolver_[i].reset(); } numResolver_ = 0; } void configureAsyncNameResolverMan(AsyncNameResolverMan* asyncNameResolverMan, Option* option) { // Currently, aria2 checks configured addresses at the startup. But // there are chances that interfaces are not setup at that // moment. For example, if aria2 is used as daemon, it may start // before network interfaces up. To workaround this, we check // addresses again if both addresses are not configured at the // startup. if (!net::getIPv4AddrConfigured() && !net::getIPv6AddrConfigured()) { net::checkAddrconfig(); } if (!net::getIPv4AddrConfigured()) { asyncNameResolverMan->setIPv4(false); } if (!net::getIPv6AddrConfigured() || option->getAsBool(PREF_DISABLE_IPV6)) { asyncNameResolverMan->setIPv6(false); } } } // namespace aria2