From a3f1538ebc5aa4b0a6ee49fee142646ff3bfee2c Mon Sep 17 00:00:00 2001 From: JINMEI Tatuya Date: Mon, 2 Apr 2012 18:01:18 -0700 Subject: [PATCH] [1820] removed DNSService::addServer() as they were mostly unused. Likewise, some of the constructors of DNSService were removed too. the test code specific to the removed interfaces were also removed. --- src/lib/asiodns/dns_service.cc | 160 ++---------------- src/lib/asiodns/dns_service.h | 42 +---- src/lib/asiodns/tests/dns_service_unittest.cc | 91 ---------- 3 files changed, 20 insertions(+), 273 deletions(-) diff --git a/src/lib/asiodns/dns_service.cc b/src/lib/asiodns/dns_service.cc index ee7cf74461..2cfdea570e 100644 --- a/src/lib/asiodns/dns_service.cc +++ b/src/lib/asiodns/dns_service.cc @@ -14,30 +14,19 @@ #include -#include // for some IPC/network system calls -#include -#include - -#include - -#include - #include -#include #include + #include + +#include // xxx_server.h requires this to be included first #include #include #include -#include - -#include #include -using isc::log::dlog; - using namespace isc::asiolink; namespace isc { @@ -46,29 +35,13 @@ namespace asiodns { class DNSLookup; class DNSAnswer; -namespace { - -asio::ip::address -convertAddr(const std::string& address) { - asio::error_code err; - asio::ip::address addr = asio::ip::address::from_string(address, err); - if (err) { - isc_throw(IOError, "Invalid IP address '" << &address << "': " - << err.message()); - } - return (addr); -} - -} - - class DNSServiceImpl { public: - DNSServiceImpl(IOService& io_service, const char& port, - const asio::ip::address* v4addr, - const asio::ip::address* v6addr, - SimpleCallback* checkin, DNSLookup* lookup, - DNSAnswer* answe); + DNSServiceImpl(IOService& io_service, SimpleCallback* checkin, + DNSLookup* lookup, DNSAnswer* answer) : + io_service_(io_service), checkin_(checkin), lookup_(lookup), + answer_(answer) + {} IOService& io_service_; @@ -77,9 +50,9 @@ public: typedef boost::shared_ptr TCPServerPtr; typedef boost::shared_ptr DNSServerPtr; std::vector servers_; - SimpleCallback *checkin_; - DNSLookup *lookup_; - DNSAnswer *answer_; + SimpleCallback* checkin_; + DNSLookup* lookup_; + DNSAnswer* answer_; template void addServerFromFD(int fd, int af) { Ptr server(new Server(io_service_.get_io_service(), fd, af, checkin_, @@ -87,107 +60,12 @@ public: (*server)(); servers_.push_back(server); } - - void addServer(uint16_t port, const asio::ip::address& address) { - try { - dlog(std::string("Initialize TCP server at ") + - address.to_string() + ":" + - boost::lexical_cast(port)); - TCPServerPtr tcpServer(new TCPServer(io_service_.get_io_service(), - address, port, checkin_, - lookup_, answer_)); - (*tcpServer)(); - servers_.push_back(tcpServer); - dlog(std::string("Initialize UDP server at ") + - address.to_string() + ":" + - boost::lexical_cast(port)); - UDPServerPtr udpServer(new UDPServer(io_service_.get_io_service(), - address, port, checkin_, lookup_, answer_)); - (*udpServer)(); - servers_.push_back(udpServer); - } catch (const asio::system_error& err) { - // We need to catch and convert any ASIO level exceptions. - // This can happen for unavailable address, binding a privilege port - // without the privilege, etc. - isc_throw(IOError, "Failed to initialize network servers: " << - err.what()); - } - } - void addServer(const char& port, const asio::ip::address& address) { - uint16_t portnum; - try { - // XXX: SunStudio with stlport4 doesn't reject some invalid - // representation such as "-1" by lexical_cast, so - // we convert it into a signed integer of a larger size and perform - // range check ourselves. - const int32_t portnum32 = boost::lexical_cast(&port); - if (portnum32 < 0 || portnum32 > 65535) { - isc_throw(IOError, "Invalid port number '" << &port); - } - portnum = portnum32; - } catch (const boost::bad_lexical_cast& ex) { - isc_throw(IOError, "Invalid port number '" << &port << "': " << - ex.what()); - } - addServer(portnum, address); - } }; -DNSServiceImpl::DNSServiceImpl(IOService& io_service, - const char& port, - const asio::ip::address* const v4addr, - const asio::ip::address* const v6addr, - SimpleCallback* checkin, - DNSLookup* lookup, - DNSAnswer* answer) : - io_service_(io_service), - checkin_(checkin), - lookup_(lookup), - answer_(answer) -{ - - if (v4addr) { - addServer(port, *v4addr); - } - if (v6addr) { - addServer(port, *v6addr); - } -} - -DNSService::DNSService(IOService& io_service, - const char& port, const char& address, - SimpleCallback* checkin, - DNSLookup* lookup, - DNSAnswer* answer) : - impl_(new DNSServiceImpl(io_service, port, NULL, NULL, checkin, lookup, - answer)), - io_service_(io_service) -{ - addServer(port, &address); -} - -DNSService::DNSService(IOService& io_service, - const char& port, - const bool use_ipv4, const bool use_ipv6, - SimpleCallback* checkin, - DNSLookup* lookup, - DNSAnswer* answer) : - impl_(NULL), io_service_(io_service) -{ - const asio::ip::address v4addr_any = - asio::ip::address(asio::ip::address_v4::any()); - const asio::ip::address* const v4addrp = use_ipv4 ? &v4addr_any : NULL; - const asio::ip::address v6addr_any = - asio::ip::address(asio::ip::address_v6::any()); - const asio::ip::address* const v6addrp = use_ipv6 ? &v6addr_any : NULL; - impl_ = new DNSServiceImpl(io_service, port, v4addrp, v6addrp, checkin, - lookup, answer); -} - DNSService::DNSService(IOService& io_service, SimpleCallback* checkin, - DNSLookup* lookup, DNSAnswer *answer) : - impl_(new DNSServiceImpl(io_service, *"0", NULL, NULL, checkin, lookup, - answer)), io_service_(io_service) + DNSLookup* lookup, DNSAnswer *answer) : + impl_(new DNSServiceImpl(io_service, checkin, lookup, answer)), + io_service_(io_service) { } @@ -195,16 +73,6 @@ DNSService::~DNSService() { delete impl_; } -void -DNSService::addServer(const char& port, const std::string& address) { - impl_->addServer(port, convertAddr(address)); -} - -void -DNSService::addServer(uint16_t port, const std::string& address) { - impl_->addServer(port, convertAddr(address)); -} - void DNSService::addServerTCPFromFD(int fd, int af) { impl_->addServerFromFD(fd, af); } diff --git a/src/lib/asiodns/dns_service.h b/src/lib/asiodns/dns_service.h index 161658fba9..8f2f6d7fee 100644 --- a/src/lib/asiodns/dns_service.h +++ b/src/lib/asiodns/dns_service.h @@ -112,40 +112,14 @@ private: static const unsigned int SERVER_DEFINED_FLAGS = 1; public: - /// \brief The constructor with a specific IP address and port on which - /// the services listen on. - /// - /// \param io_service The IOService to work with - /// \param port the port to listen on - /// \param address the IP address to listen on - /// \param checkin Provider for cc-channel events (see \c SimpleCallback) - /// \param lookup The lookup provider (see \c DNSLookup) - /// \param answer The answer provider (see \c DNSAnswer) - DNSService(asiolink::IOService& io_service, const char& port, - const char& address, isc::asiolink::SimpleCallback* checkin, - DNSLookup* lookup, DNSAnswer* answer); - - /// \brief The constructor with a specific port on which the services - /// listen on. - /// - /// It effectively listens on "any" IPv4 and/or IPv6 addresses. - /// IPv4/IPv6 services will be available if and only if \c use_ipv4 - /// or \c use_ipv6 is \c true, respectively. - /// - /// \param io_service The IOService to work with - /// \param port the port to listen on - /// \param use_ipv4 If true, listen on ipv4 'any' - /// \param use_ipv6 If true, listen on ipv6 'any' - /// \param checkin Provider for cc-channel events (see \c SimpleCallback) - /// \param lookup The lookup provider (see \c DNSLookup) - /// \param answer The answer provider (see \c DNSAnswer) - DNSService(asiolink::IOService& io_service, const char& port, - const bool use_ipv4, const bool use_ipv6, - isc::asiolink::SimpleCallback* checkin, DNSLookup* lookup, - DNSAnswer* answer); /// \brief The constructor without any servers. /// - /// Use addServer() to add some servers. + /// Use addServerTCPFromFD() or addServerUDPFromFD() to add some servers. + /// + /// \param io_service The IOService to work with + /// \param checkin Provider for cc-channel events (see \c SimpleCallback) + /// \param lookup The lookup provider (see \c DNSLookup) + /// \param answer The answer provider (see \c DNSAnswer) DNSService(asiolink::IOService& io_service, isc::asiolink::SimpleCallback* checkin, DNSLookup* lookup, DNSAnswer* answer); @@ -154,10 +128,6 @@ public: virtual ~DNSService(); //@} - /// \brief Add another server to the service - void addServer(uint16_t port, const std::string &address); - void addServer(const char& port, const std::string& address); - /// \brief Add another TCP server/listener to the service from already /// opened file descriptor /// diff --git a/src/lib/asiodns/tests/dns_service_unittest.cc b/src/lib/asiodns/tests/dns_service_unittest.cc index beac02b245..ce8eee95bd 100644 --- a/src/lib/asiodns/tests/dns_service_unittest.cc +++ b/src/lib/asiodns/tests/dns_service_unittest.cc @@ -39,98 +39,7 @@ using boost::lexical_cast; namespace { const char* const TEST_SERVER_PORT = "53535"; -const char* const TEST_CLIENT_PORT = "53536"; const char* const TEST_IPV6_ADDR = "::1"; -const char* const TEST_IPV4_ADDR = "127.0.0.1"; - -TEST(IOServiceTest, badPort) { - IOService io_service; - EXPECT_THROW(DNSService(io_service, *"65536", true, false, NULL, NULL, NULL), IOError); - EXPECT_THROW(DNSService(io_service, *"53210.0", true, false, NULL, NULL, NULL), IOError); - EXPECT_THROW(DNSService(io_service, *"-1", true, false, NULL, NULL, NULL), IOError); - EXPECT_THROW(DNSService(io_service, *"domain", true, false, NULL, NULL, NULL), IOError); -} - -TEST(IOServiceTest, badAddress) { - IOService io_service; - EXPECT_THROW(DNSService(io_service, *TEST_SERVER_PORT, *"192.0.2.1.1", NULL, NULL, NULL), IOError); - EXPECT_THROW(DNSService(io_service, *TEST_SERVER_PORT, *"2001:db8:::1", NULL, NULL, NULL), IOError); - EXPECT_THROW(DNSService(io_service, *TEST_SERVER_PORT, *"localhost", NULL, NULL, NULL), IOError); -} - -TEST(IOServiceTest, unavailableAddress) { - IOService io_service; - // These addresses should generally be unavailable as a valid local - // address, although there's no guarantee in theory. - EXPECT_THROW(DNSService(io_service, *TEST_SERVER_PORT, *"192.0.2.0", NULL, NULL, NULL), IOError); - - // Some OSes would simply reject binding attempt for an AF_INET6 socket - // to an IPv4-mapped IPv6 address. Even if those that allow it, since - // the corresponding IPv4 address is the same as the one used in the - // AF_INET socket case above, it should at least show the same result - // as the previous one. - EXPECT_THROW(DNSService(io_service, *TEST_SERVER_PORT, *"::ffff:192.0.2.0", NULL, NULL, NULL), IOError); -} - -TEST(IOServiceTest, duplicateBind_v6) { - // In each sub test case, second attempt should fail due to duplicate bind - IOService io_service; - - // IPv6, "any" address - DNSService* dns_service = new DNSService(io_service, *TEST_SERVER_PORT, false, true, NULL, NULL, NULL); - EXPECT_THROW(DNSService(io_service, *TEST_SERVER_PORT, false, true, NULL, NULL, NULL), IOError); - delete dns_service; - -} - -TEST(IOServiceTest, duplicateBind_v6_address) { - // In each sub test case, second attempt should fail due to duplicate bind - IOService io_service; - - // IPv6, specific address - DNSService* dns_service = new DNSService(io_service, *TEST_SERVER_PORT, *TEST_IPV6_ADDR, NULL, NULL, NULL); - EXPECT_THROW(DNSService(io_service, *TEST_SERVER_PORT, *TEST_IPV6_ADDR, NULL, NULL, NULL), IOError); - delete dns_service; - -} - -TEST(IOServiceTest, duplicateBind_v4) { - // In each sub test case, second attempt should fail due to duplicate bind - IOService io_service; - - // IPv4, "any" address - DNSService* dns_service = new DNSService(io_service, *TEST_SERVER_PORT, true, false, NULL, NULL, NULL); - EXPECT_THROW(DNSService(io_service, *TEST_SERVER_PORT, true, false, NULL, NULL, NULL), IOError); - delete dns_service; - -} - -TEST(IOServiceTest, duplicateBind_v4_address) { - // In each sub test case, second attempt should fail due to duplicate bind - IOService io_service; - - // IPv4, specific address - DNSService* dns_service = new DNSService(io_service, *TEST_SERVER_PORT, *TEST_IPV4_ADDR, NULL, NULL, NULL); - EXPECT_THROW(DNSService(io_service, *TEST_SERVER_PORT, *TEST_IPV4_ADDR, NULL, NULL, NULL), IOError); - delete dns_service; -} - -// Disabled because IPv4-mapped addresses don't seem to be working with -// the IOService constructor -TEST(IOServiceTest, DISABLED_IPv4MappedDuplicateBind) { - IOService io_service; - // Duplicate bind on IPv4-mapped IPv6 address - DNSService* dns_service = new DNSService(io_service, *TEST_SERVER_PORT, *"127.0.0.1", NULL, NULL, NULL); - EXPECT_THROW(DNSService(io_service, *TEST_SERVER_PORT, *"::ffff:127.0.0.1", NULL, NULL, NULL), IOError); - delete dns_service; - - // XXX: - // Currently, this throws an "invalid argument" exception. I have - // not been able to get IPv4-mapped addresses to work. - dns_service = new DNSService(io_service, *TEST_SERVER_PORT, *"::ffff:127.0.0.1", NULL, NULL, NULL); - EXPECT_THROW(DNSService(io_service, *TEST_SERVER_PORT, *"127.0.0.1", NULL, NULL, NULL), IOError); - delete dns_service; -} // A simple lookup callback for DNS services. It records the pointer value of // to given output buffer each time the callback is called (up to two times)