mirror of
https://github.com/aria2/aria2.git
synced 2025-01-01 07:36:24 +00:00
Moved parseAsyncDNSServers() to AsyncNameResolver.cc and refactored.
The parseAsyncDNSServers() now uses net::getBinAddr() internally, which makes the function simpler. Also added unit test.
This commit is contained in:
parent
36051cca5e
commit
92c518a2ba
@ -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<std::string> 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<std::string>::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
|
||||
|
@ -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
|
||||
|
@ -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<std::string> 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<std::string>::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<struct addrinfo*> 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<sockaddr_union*>(res->ai_addr)->in;
|
||||
memcpy(&node->addr.addr4, &(in->sin_addr), 4);
|
||||
} else {
|
||||
sockaddr_in6* in =
|
||||
&reinterpret_cast<sockaddr_union*>(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<SharedHandle<RequestGroup> >& requestGroups,
|
||||
const SharedHandle<Option>& op,
|
||||
|
48
test/AsyncNameResolverTest.cc
Normal file
48
test/AsyncNameResolverTest.cc
Normal file
@ -0,0 +1,48 @@
|
||||
#include "AsyncNameResolver.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include <cppunit/extensions/HelperMacros.h>
|
||||
|
||||
#include "SocketCore.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
class AsyncNameResolverTest:public CppUnit::TestFixture {
|
||||
|
||||
CPPUNIT_TEST_SUITE(AsyncNameResolverTest);
|
||||
CPPUNIT_TEST(testParseAsyncDNSServers);
|
||||
CPPUNIT_TEST_SUITE_END();
|
||||
public:
|
||||
void setUp() {}
|
||||
|
||||
void tearDown() {}
|
||||
|
||||
void testParseAsyncDNSServers();
|
||||
};
|
||||
|
||||
|
||||
CPPUNIT_TEST_SUITE_REGISTRATION(AsyncNameResolverTest);
|
||||
|
||||
void AsyncNameResolverTest::testParseAsyncDNSServers()
|
||||
{
|
||||
#ifdef HAVE_ARES_ADDR_NODE
|
||||
in_addr ans4;
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)4, net::getBinAddr(&ans4, "192.168.0.1"));
|
||||
in6_addr ans6;
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)16, net::getBinAddr(&ans6, "2001:db8::2:1"));
|
||||
|
||||
ares_addr_node* root;
|
||||
root = parseAsyncDNSServers("192.168.0.1,2001:db8::2:1");
|
||||
ares_addr_node* node = root;
|
||||
CPPUNIT_ASSERT(node);
|
||||
CPPUNIT_ASSERT_EQUAL(AF_INET, node->family);
|
||||
CPPUNIT_ASSERT(memcmp(&ans4, &node->addr, sizeof(ans4)) == 0);
|
||||
node = node->next;
|
||||
CPPUNIT_ASSERT(node);
|
||||
CPPUNIT_ASSERT_EQUAL(AF_INET6, node->family);
|
||||
CPPUNIT_ASSERT(memcmp(&ans6, &node->addr, sizeof(ans6)) == 0);
|
||||
#endif // HAVE_ARES_ADDR_NODE
|
||||
}
|
||||
|
||||
} // namespace aria2
|
@ -218,6 +218,10 @@ aria2c_SOURCES += MetalinkerTest.cc\
|
||||
MetalinkProcessorTest.cc
|
||||
endif # ENABLE_METALINK
|
||||
|
||||
if ENABLE_ASYNC_DNS
|
||||
aria2c_SOURCES += AsyncNameResolverTest.cc
|
||||
endif # ENABLE_ASYNC_DNS
|
||||
|
||||
aria2c_LDADD = ../src/libaria2c.a @LIBINTL@ @CPPUNIT_LIBS@
|
||||
AM_CPPFLAGS = -Wall\
|
||||
-I$(top_srcdir)/src\
|
||||
|
Loading…
Reference in New Issue
Block a user