From 92c518a2ba214fe605b213830fc2ec3fd5d0fe14 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 16 May 2012 23:02:55 +0900 Subject: [PATCH] Moved parseAsyncDNSServers() to AsyncNameResolver.cc and refactored. The parseAsyncDNSServers() now uses net::getBinAddr() internally, which makes the function simpler. Also added unit test. --- src/AsyncNameResolver.cc | 33 +++++++++++++++++++++++ src/AsyncNameResolver.h | 7 +++++ src/MultiUrlRequestInfo.cc | 50 +++-------------------------------- test/AsyncNameResolverTest.cc | 48 +++++++++++++++++++++++++++++++++ test/Makefile.am | 4 +++ 5 files changed, 95 insertions(+), 47 deletions(-) create mode 100644 test/AsyncNameResolverTest.cc diff --git a/src/AsyncNameResolver.cc b/src/AsyncNameResolver.cc index 4cba7a25..59021509 100644 --- a/src/AsyncNameResolver.cc +++ b/src/AsyncNameResolver.cc @@ -39,6 +39,7 @@ #include "A2STR.h" #include "LogFactory.h" #include "SocketCore.h" +#include "util.h" namespace aria2 { @@ -137,4 +138,36 @@ void AsyncNameResolver::reset() ares_init(&channel_); } +#ifdef HAVE_ARES_ADDR_NODE + +ares_addr_node* parseAsyncDNSServers(const std::string& serversOpt) +{ + std::vector servers; + util::split(serversOpt.begin(), serversOpt.end(), + std::back_inserter(servers), + ',', + true /* doStrip */); + ares_addr_node root; + root.next = 0; + ares_addr_node* tail = &root; + ares_addr_node* node = 0; + for(std::vector::const_iterator i = servers.begin(), + eoi = servers.end(); i != eoi; ++i) { + if(node == 0) { + node = new ares_addr_node(); + } + size_t len = net::getBinAddr(&node->addr, (*i).c_str()); + if(len != 0) { + node->next = 0; + node->family = (len == 4 ? AF_INET : AF_INET6); + tail->next = node; + tail = node; + node = 0; + } + } + return root.next; +} + +#endif // HAVE_ARES_ADDR_NODE + } // namespace aria2 diff --git a/src/AsyncNameResolver.h b/src/AsyncNameResolver.h index 8f80b614..aef111e3 100644 --- a/src/AsyncNameResolver.h +++ b/src/AsyncNameResolver.h @@ -117,6 +117,13 @@ public: }; +#ifdef HAVE_ARES_ADDR_NODE + +ares_addr_node* parseAsyncDNSServers(const std::string& serversOpt); + +#endif // HAVE_ARES_ADDR_NODE + + } // namespace aria2 #endif // D_ASYNC_NAME_RESOLVER_H diff --git a/src/MultiUrlRequestInfo.cc b/src/MultiUrlRequestInfo.cc index c0636a06..5e241305 100644 --- a/src/MultiUrlRequestInfo.cc +++ b/src/MultiUrlRequestInfo.cc @@ -71,6 +71,9 @@ #ifdef ENABLE_SSL # include "TLSContext.h" #endif // ENABLE_SSL +#ifdef ENABLE_ASYNC_DNS +#include "AsyncNameResolver.h" +#endif // ENABLE_ASYNC_DNS namespace aria2 { @@ -100,53 +103,6 @@ void handler(int signal) { } } // namespace -#ifdef HAVE_ARES_ADDR_NODE - -namespace { - -ares_addr_node* parseAsyncDNSServers(const std::string& serversOpt) -{ - std::vector servers; - util::split(serversOpt.begin(), serversOpt.end(), - std::back_inserter(servers), - ',', - true /* doStrip */); - ares_addr_node root; - root.next = 0; - ares_addr_node* tail = &root; - for(std::vector::const_iterator i = servers.begin(), - eoi = servers.end(); i != eoi; ++i) { - struct addrinfo* res; - int s = callGetaddrinfo(&res, (*i).c_str(), 0, AF_UNSPEC, - 0, AI_NUMERICHOST, 0); - if(s != 0) { - continue; - } - WSAAPI_AUTO_DELETE resDeleter(res, freeaddrinfo); - if(res) { - ares_addr_node* node = new ares_addr_node(); - node->next = 0; - node->family = res->ai_family; - if(node->family == AF_INET) { - sockaddr_in* in = - &reinterpret_cast(res->ai_addr)->in; - memcpy(&node->addr.addr4, &(in->sin_addr), 4); - } else { - sockaddr_in6* in = - &reinterpret_cast(res->ai_addr)->in6; - memcpy(&node->addr.addr6, &(in->sin6_addr), 16); - } - tail->next = node; - tail = node; - } - } - return root.next; -} - -} // namespace - -#endif // HAVE_ARES_ADDR_NODE - MultiUrlRequestInfo::MultiUrlRequestInfo (const std::vector >& requestGroups, const SharedHandle