From: Xie Jiagui Date: Mon, 5 Mar 2012 03:56:54 +0000 (+0800) Subject: [1600] Changed the interface of DNSService to allow add UDPServer or X-Git-Tag: trac2351_base~226^2~116^2~134 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=95af6c2e54fb45fa5af1a2d3650376ccadd32c7b;p=thirdparty%2Fkea.git [1600] Changed the interface of DNSService to allow add UDPServer or SyncUDPServer by command. --- diff --git a/src/lib/asiodns/dns_service.cc b/src/lib/asiodns/dns_service.cc index 967ce15fea..3fcfce38bb 100644 --- a/src/lib/asiodns/dns_service.cc +++ b/src/lib/asiodns/dns_service.cc @@ -25,9 +25,9 @@ #include #include #include -#include #include #include +#include #include @@ -66,11 +66,13 @@ public: const asio::ip::address* v4addr, const asio::ip::address* v6addr, SimpleCallback* checkin, DNSLookup* lookup, - DNSAnswer* answer); + DNSAnswer* answer, + const UDPVersion param_flags); IOService& io_service_; typedef boost::shared_ptr UDPServerPtr; + typedef boost::shared_ptr SyncUDPServerPtr; typedef boost::shared_ptr TCPServerPtr; typedef boost::shared_ptr DNSServerPtr; std::vector servers_; @@ -85,7 +87,8 @@ public: servers_.push_back(server); } - void addServer(uint16_t port, const asio::ip::address& address) { + void addServer(uint16_t port, const asio::ip::address& address, + const UDPVersion param_flags) { try { dlog(std::string("Initialize TCP server at ") + address.to_string() + ":" + boost::lexical_cast(port)); TCPServerPtr tcpServer(new TCPServer(io_service_.get_io_service(), @@ -93,10 +96,21 @@ public: (*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); + + if(1 == param_flags) { + SyncUDPServerPtr syncUdpServer(new SyncUDPServer(io_service_.get_io_service(), + address, port, checkin_, lookup_, answer_)); + (*syncUdpServer)(); + servers_.push_back(syncUdpServer); + } else if(2 == param_flags) { + UDPServerPtr udpServer(new UDPServer(io_service_.get_io_service(), + address, port, checkin_, lookup_, answer_)); + (*udpServer)(); + servers_.push_back(udpServer); + } else { + // If nerther asyn UDPServer nor sync UDNServer, it throws. + isc_throw(IOError, "Bad UDPServer Version!"); + } } catch (const asio::system_error& err) { // We need to catch and convert any ASIO level exceptions. @@ -106,7 +120,8 @@ public: err.what()); } } - void addServer(const char& port, const asio::ip::address& address) { + void addServer(const char& port, const asio::ip::address& address, + const UDPVersion param_flags) { uint16_t portnum; try { // XXX: SunStudio with stlport4 doesn't reject some invalid @@ -122,7 +137,7 @@ public: isc_throw(IOError, "Invalid port number '" << &port << "': " << ex.what()); } - addServer(portnum, address); + addServer(portnum, address,param_flags); } }; @@ -132,7 +147,8 @@ DNSServiceImpl::DNSServiceImpl(IOService& io_service, const asio::ip::address* const v6addr, SimpleCallback* checkin, DNSLookup* lookup, - DNSAnswer* answer) : + DNSAnswer* answer, + const UDPVersion para_flags): io_service_(io_service), checkin_(checkin), lookup_(lookup), @@ -140,10 +156,10 @@ DNSServiceImpl::DNSServiceImpl(IOService& io_service, { if (v4addr) { - addServer(port, *v4addr); + addServer(port, *v4addr,para_flags); } if (v6addr) { - addServer(port, *v6addr); + addServer(port, *v6addr,para_flags); } } @@ -151,11 +167,12 @@ DNSService::DNSService(IOService& io_service, const char& port, const char& address, SimpleCallback* checkin, DNSLookup* lookup, - DNSAnswer* answer) : + DNSAnswer* answer, + const UDPVersion para_flags) : impl_(new DNSServiceImpl(io_service, port, NULL, NULL, checkin, lookup, - answer)), io_service_(io_service) + answer,para_flags)), io_service_(io_service) { - addServer(port, &address); + addServer(port, &address,para_flags); } DNSService::DNSService(IOService& io_service, @@ -163,7 +180,8 @@ DNSService::DNSService(IOService& io_service, const bool use_ipv4, const bool use_ipv6, SimpleCallback* checkin, DNSLookup* lookup, - DNSAnswer* answer) : + DNSAnswer* answer, + const UDPVersion para_flags) : impl_(NULL), io_service_(io_service) { const asio::ip::address v4addr_any = @@ -172,13 +190,13 @@ DNSService::DNSService(IOService& io_service, 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); + impl_ = new DNSServiceImpl(io_service, port, v4addrp, v6addrp, checkin, lookup, answer,para_flags); } DNSService::DNSService(IOService& io_service, SimpleCallback* checkin, - DNSLookup* lookup, DNSAnswer *answer) : + DNSLookup* lookup, DNSAnswer *answer,const UDPVersion para_flags) : impl_(new DNSServiceImpl(io_service, *"0", NULL, NULL, checkin, lookup, - answer)), io_service_(io_service) + answer,para_flags)), io_service_(io_service) { } @@ -187,21 +205,25 @@ DNSService::~DNSService() { } void -DNSService::addServer(const char& port, const std::string& address) { - impl_->addServer(port, convertAddr(address)); +DNSService::addServer(const char& port, const std::string& address,UDPVersion para_flags) { + impl_->addServer(port, convertAddr(address),para_flags); } void -DNSService::addServer(uint16_t port, const std::string& address) { - impl_->addServer(port, convertAddr(address)); +DNSService::addServer(uint16_t port, const std::string& address,UDPVersion para_flags) { + impl_->addServer(port, convertAddr(address),para_flags); } void DNSService::addServerTCPFromFD(int fd, int af) { impl_->addServerFromFD(fd, af); } -void DNSService::addServerUDPFromFD(int fd, int af) { - impl_->addServerFromFD(fd, af); +void DNSService::addServerUDPFromFD(int fd, int af,const UDPVersion param_flags) { + if(1 == param_flags) { + impl_->addServerFromFD(fd, af); + } else if(2 == param_flags) { + impl_->addServerFromFD(fd, af); + } } void diff --git a/src/lib/asiodns/dns_service.h b/src/lib/asiodns/dns_service.h index 66f8d33fed..efb68ef983 100644 --- a/src/lib/asiodns/dns_service.h +++ b/src/lib/asiodns/dns_service.h @@ -27,6 +27,15 @@ class DNSLookup; class DNSAnswer; class DNSServiceImpl; + +/// Codes for UDPServers used in addServer()method. +/// +/// Note: the codes only used in how to create the UDPServers. +enum UDPVersion { + SYNC_=1, ///< used synchronous UDPServer + ASYNC_=2 ///< used asynchronous UDPServer +}; + /// \brief Handle DNS Queries /// /// DNSService is the service that handles DNS queries and answers with @@ -57,7 +66,8 @@ public: /// \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); + DNSLookup* lookup, DNSAnswer* answer, + const UDPVersion param_flags = SYNC_); /// \brief The constructor with a specific port on which the services /// listen on. /// @@ -75,19 +85,23 @@ public: DNSService(asiolink::IOService& io_service, const char& port, const bool use_ipv4, const bool use_ipv6, isc::asiolink::SimpleCallback* checkin, DNSLookup* lookup, - DNSAnswer* answer); + DNSAnswer* answer, + const UDPVersion param_flags = SYNC_); /// \brief The constructor without any servers. /// /// Use addServer() to add some servers. DNSService(asiolink::IOService& io_service, isc::asiolink::SimpleCallback* checkin, - DNSLookup* lookup, DNSAnswer* answer); + DNSLookup* lookup, DNSAnswer* answer, + const UDPVersion param_flags = SYNC_); /// \brief The destructor. ~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); + void addServer(uint16_t port, const std::string &address, + const UDPVersion param_flags = SYNC_); + void addServer(const char &port, const std::string &address, + const UDPVersion param_flags = SYNC_); /// \brief Add another TCP server/listener to the service from already /// opened file descriptor @@ -122,7 +136,7 @@ public: /// \throw isc::InvalidParameter if af is neither AF_INET nor AF_INET6. /// \throw isc::asiolink::IOError when a low-level error happens, like the /// fd is not a valid descriptor or it can't be listened on. - void addServerUDPFromFD(int fd, int af); + void addServerUDPFromFD(int fd, int af,const UDPVersion param_flags = SYNC_); /// \brief Remove all servers from the service void clearServers(); diff --git a/src/lib/asiodns/tests/io_service_unittest.cc b/src/lib/asiodns/tests/io_service_unittest.cc index cc64022606..4b1889a56f 100644 --- a/src/lib/asiodns/tests/io_service_unittest.cc +++ b/src/lib/asiodns/tests/io_service_unittest.cc @@ -116,3 +116,8 @@ TEST(IOServiceTest, DISABLED_IPv4MappedDuplicateBind) { delete dns_service; } +TEST(IOServiceTest, BadUdpServerVersion) { + IOService io_service; + DNSService* dns_service = new DNSService(io_service,NULL,NULL,NULL); + EXPECT_THROW(dns_service->addServer(*TEST_SERVER_PORT,"127.0.0.1",UDPVersion(3)),IOError); +}