Parcourir la source

[2313] Added OptionSpace6 class that holds enterprise numbers.

Marcin Siodelski il y a 12 ans
Parent
commit
a146bf1575

+ 17 - 0
src/lib/dhcpsrv/option_space.cc

@@ -42,5 +42,22 @@ OptionSpace::validateName(const std::string& name) {
     return (false);
 }
 
+OptionSpace6::OptionSpace6(const std::string& name)
+    : OptionSpace(name),
+      enterprise_number_(0) {
+}
+
+OptionSpace6::OptionSpace6(const std::string& name,
+                           const uint32_t enterprise_id)
+    : OptionSpace(name, true),
+      enterprise_number_(enterprise_id) {
+}
+
+void
+OptionSpace6::setVendorSpace(const uint32_t enterprise_id) {
+    enterprise_number_ = enterprise_id;
+    OptionSpace::setVendorSpace();
+}
+
 } // end of isc::dhcp namespace
 } // end of isc namespace

+ 51 - 6
src/lib/dhcpsrv/option_space.h

@@ -18,6 +18,7 @@
 #include <exceptions/exceptions.h>
 #include <boost/shared_ptr.hpp>
 #include <map>
+#include <stdint.h>
 #include <string>
 
 namespace isc {
@@ -63,6 +64,11 @@ public:
     /// correct.
     OptionSpace(const std::string& name, const bool vendor_space = false);
 
+    /// @brief Mark option space as non-vendor space.
+    void clearVendorSpace() {
+        vendor_space_ = false;
+    }
+
     /// @brief Return option space name.
     ///
     /// @return option space name.
@@ -74,12 +80,9 @@ public:
     /// the vendor space.
     bool isVendorSpace() const { return (vendor_space_); }
 
-    /// @brief Mark option space as vendor space or non-vendor space.
-    ///
-    /// @param vendor_space a boolean value indicating that this option
-    /// space is a vendor space (true) or non-vendor space (false).
-    void setVendorSpace(const bool vendor_space) {
-        vendor_space_ = vendor_space;
+    /// @brief Mark option space as vendor space.
+    void setVendorSpace() {
+        vendor_space_ = true;
     }
 
     /// @brief Checks that the provided option space name is valid.
@@ -101,6 +104,48 @@ private:
 
 };
 
+/// @brief DHCPv6 option space.
+///
+/// This class extends the base class with support for enterprise numbers.
+/// The enterprise numbers are assigned by IANA to various organizations
+/// and they are carried as uint32_t integers in DHCPv6 Vendor Specific
+/// Information Options (VSIO). For more information refer to RFC3315.
+/// All option spaces that group VSIO options must have enterprise number
+/// set. It can be set using a constructor or \ref setVendorSpace function.
+class OptionSpace6 : public OptionSpace {
+public:
+
+    /// @brief Constructor for non-vendor-specific options.
+    ///
+    /// This constructor marks option space as non-vendor specific.
+    ///
+    /// @param name option space name.
+    OptionSpace6(const std::string& name);
+
+    /// @brief Constructor for vendor-specific options.
+    ///
+    /// This constructor marks option space as vendor specific and sets
+    /// enterprise number to a given value.
+    ///
+    /// @param name option space name.
+    /// @param enterprise_number enterprise number.
+    OptionSpace6(const std::string& name, const uint32_t enterprise_number);
+
+    /// @brief Return enterprise number for the option space.
+    ///
+    /// @return enterprise number.
+    uint32_t getEnterpriseNumber() const { return (enterprise_number_); }
+
+    /// @brief Mark option space as VSIO option space.
+    ///
+    /// @param enterprise_number enterprise number.
+    void setVendorSpace(const uint32_t enterprise_number);
+
+private:
+    
+    uint32_t enterprise_number_; ///< IANA assigned enterprise number.
+};
+
 } // namespace isc::dhcp
 } // namespace isc
 

+ 49 - 1
src/lib/dhcpsrv/tests/option_space_unittest.cc

@@ -50,7 +50,7 @@ TEST(OptionSpaceTest, setVendorSpace) {
     EXPECT_TRUE(space.isVendorSpace());
 
     // Override the vendor space flag.
-    space.setVendorSpace(false);
+    space.clearVendorSpace();
     EXPECT_FALSE(space.isVendorSpace());
 }
 
@@ -92,4 +92,52 @@ TEST(OptionSpaceTest, validateName) {
     }
 }
 
+// The purpose of this test is to verify that the constructors of the
+// OptionSpace6 class set the class members to correct values.
+TEST(OptionSpace6Test, constructor) {
+    // Create some option space and do not specify enterprise number.
+    // In such case the vendor space flag is expected to be
+    // set to false.
+    OptionSpace6 space1("abcd");
+    EXPECT_EQ("abcd", space1.getName());
+    EXPECT_FALSE(space1.isVendorSpace());
+
+    // Create an option space and specify an enterprise number. In this
+    // case the vendor space flag is expected to be set to true and the
+    // enterprise number should be set to a desired value.
+    OptionSpace6 space2("abcd", 2145);
+    EXPECT_EQ("abcd", space2.getName());
+    EXPECT_TRUE(space2.isVendorSpace());
+    EXPECT_EQ(2145, space2.getEnterpriseNumber());
+
+    // Verify that constructors throw an exception when invalid option
+    // space name has been specified.
+    EXPECT_THROW(OptionSpace6("isc dhcp"), InvalidOptionSpace);
+    EXPECT_THROW(OptionSpace6("isc%dhcp", 2145), InvalidOptionSpace);
+}
+
+// The purpose of this test is to verify an option space can be marked
+// vendor option space and enterprise number can be set.
+TEST(OptionSpace6Test, setVendorSpace) {
+    OptionSpace6 space("isc");
+    EXPECT_EQ("isc", space.getName());
+    EXPECT_FALSE(space.isVendorSpace());
+
+    // Mark it vendor option space and set enterprise id.
+    space.setVendorSpace(1234);
+    EXPECT_TRUE(space.isVendorSpace());
+    EXPECT_EQ(1234, space.getEnterpriseNumber());
+
+    // Override the enterprise number to make sure and make sure that
+    // the new number is returned by the object.
+    space.setVendorSpace(2345);
+    EXPECT_TRUE(space.isVendorSpace());
+    EXPECT_EQ(2345, space.getEnterpriseNumber());
+
+    // Clear the vendor option space flag.
+    space.clearVendorSpace();
+    EXPECT_FALSE(space.isVendorSpace());
+}
+
+
 }; // end of anonymous namespace