Browse Source

[1784] reject unsupported addServer options.

JINMEI Tatuya 13 years ago
parent
commit
8db9dee362

+ 6 - 0
src/lib/asiodns/dns_service.cc

@@ -22,6 +22,8 @@
 
 #include <log/dummylog.h>
 
+#include <exceptions/exceptions.h>
+
 #include <asio.hpp>
 #include <dns_service.h>
 #include <asiolink/io_service.h>
@@ -208,6 +210,10 @@ void DNSService::addServerTCPFromFD(int fd, int af) {
 }
 
 void DNSService::addServerUDPFromFD(int fd, int af, ServerFlag options) {
+    if ((~SERVER_DEFINED_FLAGS & static_cast<int>(options)) != 0) {
+        isc_throw(isc::InvalidParameter, "Invalid DNS/UDP server option: "
+                  << options);
+    }
     if ((options & SERVER_SYNC_OK) != 0) {
         impl_->addServerFromFD<DNSServiceImpl::SyncUDPServerPtr,
             SyncUDPServer>(fd, af);

+ 9 - 1
src/lib/asiodns/dns_service.h

@@ -75,6 +75,7 @@ public:
                            ///< information given by the client.
     };
 
+public:
     /// \brief The destructor.
     virtual ~DNSServiceBase() {}
 
@@ -104,6 +105,12 @@ private:
     DNSService(const DNSService& source);
     DNSService& operator=(const DNSService& source);
 
+private:
+    // Bit or'ed all defined \c ServerFlag values.  Used internally for
+    // compatibility check.  Note that this doesn't have to be used by
+    // applications, and doesn't have to be defined in the "base" class.
+    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.
@@ -187,7 +194,8 @@ public:
     ///     AF_INET or AF_INET6.
     /// \param options Optional properties of the server (see ServerFlag).
     ///
-    /// \throw isc::InvalidParameter if af is neither AF_INET nor AF_INET6.
+    /// \throw isc::InvalidParameter if af is neither AF_INET nor AF_INET6,
+    ///     or the given \c options include an unsupported or invalid value.
     /// \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.
     virtual void addServerUDPFromFD(int fd, int af,

+ 10 - 0
src/lib/asiodns/tests/dns_service_unittest.cc

@@ -15,6 +15,8 @@
 #include <config.h>
 #include <gtest/gtest.h>
 
+#include <exceptions/exceptions.h>
+
 #include <asio.hpp>
 #include <asiolink/asiolink.h>
 #include <asiodns/asiodns.h>
@@ -310,4 +312,12 @@ TEST_F(UDPDNSServiceTest, syncUDPServerFromFD) {
     EXPECT_EQ(first_buffer_, second_buffer_);
 }
 
+TEST_F(UDPDNSServiceTest, addUDPServerFromFDWithUnknownOption) {
+    // Use of undefined/incompatible options should result in an exception.
+    EXPECT_THROW(dns_service.addServerUDPFromFD(
+                     getSocketFD(AF_INET6, TEST_IPV6_ADDR, TEST_SERVER_PORT),
+                     AF_INET6, static_cast<DNSService::ServerFlag>(2)),
+                 isc::InvalidParameter);
+}
+
 } // unnamed namespace