]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[5272] getSubnet implemented.
authorTomek Mrugalski <tomasz@isc.org>
Tue, 1 Aug 2017 18:52:15 +0000 (20:52 +0200)
committerTomek Mrugalski <tomasz@isc.org>
Tue, 1 Aug 2017 19:13:43 +0000 (21:13 +0200)
src/lib/dhcpsrv/cfg_subnets4.cc
src/lib/dhcpsrv/cfg_subnets4.h
src/lib/dhcpsrv/cfg_subnets6.cc
src/lib/dhcpsrv/cfg_subnets6.h
src/lib/dhcpsrv/tests/cfg_subnets4_unittest.cc
src/lib/dhcpsrv/tests/cfg_subnets6_unittest.cc

index 136f213d454c7219933727cea65b2e83861ee571..ef3579ce69691d4880703d82464cbaeb38272865 100644 (file)
@@ -195,6 +195,19 @@ CfgSubnets4::selectSubnet(const std::string& iface,
     return (Subnet4Ptr());
 }
 
+Subnet4Ptr
+CfgSubnets4::getSubnet(const SubnetID id) const {
+
+    /// @todo: Once this code is migrated to multi-index container, use
+    /// an index rather than full scan.
+    for (auto subnet = subnets_.begin(); subnet != subnets_.end(); ++subnet) {
+        if ((*subnet)->getID() == id) {
+            return (*subnet);
+        }
+    }
+    return (Subnet4Ptr());
+}
+
 Subnet4Ptr
 CfgSubnets4::selectSubnet(const IOAddress& address,
                  const ClientClasses& client_classes) const {
index c2b72429e03c6991cbfa5afbebf751c9b51dfef2..d37aa112af5fd72543eba1e4c309125290d6e330 100644 (file)
@@ -97,6 +97,14 @@ public:
     /// or they are insufficient to select a subnet.
     Subnet4Ptr selectSubnet(const SubnetSelector& selector) const;
 
+    /// @brief Returns subnet with specified subnet-id value
+    ///
+    /// Warning: this method uses full scan. Its use is not recommeded for
+    /// packet processing.
+    ///
+    /// @return Subnet (or NULL)
+    Subnet4Ptr getSubnet(const SubnetID id) const;
+
     /// @brief Returns a pointer to a subnet if provided address is in its range.
     ///
     /// This method returns a pointer to the subnet if the address passed in
index a795fe4a609d1c334ce5db36e0ddf6d9eb32665d..03ec44f84e5b6b8bb6d1ed429ea8549147c511bc 100644 (file)
@@ -166,6 +166,20 @@ CfgSubnets6::selectSubnet(const OptionPtr& interface_id,
     return (Subnet6Ptr());
 }
 
+Subnet6Ptr
+CfgSubnets6::getSubnet(const SubnetID id) const {
+
+    /// @todo: Once this code is migrated to multi-index container, use
+    /// an index rather than full scan.
+    for (auto subnet = subnets_.begin(); subnet != subnets_.end(); ++subnet) {
+        if ((*subnet)->getID() == id) {
+            return (*subnet);
+        }
+    }
+    return (Subnet6Ptr());
+}
+
+
 bool
 CfgSubnets6::isDuplicate(const Subnet6& subnet) const {
     for (Subnet6Collection::const_iterator subnet_it = subnets_.begin();
index 18e1ca866f66d86adc329a6e13b27deab188f3ab..e5ed5adb065fa54e3d6e988318f8de20169216f2 100644 (file)
@@ -87,6 +87,14 @@ public:
     /// @return Pointer to the selected subnet or NULL if no subnet found.
     Subnet6Ptr selectSubnet(const SubnetSelector& selector) const;
 
+    /// @brief Returns subnet with specified subnet-id value
+    ///
+    /// Warning: this method uses full scan. Its use is not recommeded for
+    /// packet processing.
+    ///
+    /// @return Subnet (or NULL)
+    Subnet6Ptr getSubnet(const SubnetID id) const;
+
     /// @brief Selects the subnet using a specified address.
     ///
     /// This method searches for the subnet using the specified address. If
index d7433f2d8c311e248a50ec41a33e56410c48eaf1..2ff407a829c3cb92a60c60ab0c8a280a30810954 100644 (file)
@@ -524,7 +524,7 @@ TEST(CfgSubnets4Test, unparsePool) {
     subnet->addPool(pool1);
     subnet->addPool(pool2);
     cfg.add(subnet);
-    
+
     // Unparse
     std::string expected = "[\n"
         "{\n"
@@ -555,4 +555,24 @@ TEST(CfgSubnets4Test, unparsePool) {
     runToElementTest<CfgSubnets4>(expected, cfg);
 }
 
+// This test verifies that it is possible to retrieve a subnet using subnet-id.
+TEST(CfgSubnets4Test, getSubnet) {
+    CfgSubnets4 cfg;
+
+    // Create 3 subnets.
+    Subnet4Ptr subnet1(new Subnet4(IOAddress("192.0.2.0"), 26, 1, 2, 3, 100));
+    Subnet4Ptr subnet2(new Subnet4(IOAddress("192.0.2.64"), 26, 1, 2, 3, 200));
+    Subnet4Ptr subnet3(new Subnet4(IOAddress("192.0.2.128"), 26, 1, 2, 3, 300));
+
+    // Add one subnet and make sure it is returned.
+    cfg.add(subnet1);
+    cfg.add(subnet2);
+    cfg.add(subnet3);
+
+    EXPECT_EQ(subnet1, cfg.getSubnet(100));
+    EXPECT_EQ(subnet2, cfg.getSubnet(200));
+    EXPECT_EQ(subnet3, cfg.getSubnet(300));
+    EXPECT_EQ(Subnet4Ptr(), cfg.getSubnet(400)); // no such subnet
+}
+
 } // end of anonymous namespace
index c655686597d2aa155f462ddbb8f003caf0eb4c53..a9bcb3de80e76c46aed1c7c2dc100f373f3a1b2e 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2015,2017 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2014-2017 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -434,7 +434,7 @@ TEST(CfgSubnets6Test, unparsePool) {
     subnet->addPool(pool1);
     subnet->addPool(pool2);
     cfg.add(subnet);
-    
+
     // Unparse
     std::string expected = "[\n"
         "{\n"
@@ -480,7 +480,7 @@ TEST(CfgSubnets6Test, unparsePdPool) {
     subnet->addPool(pdpool1);
     subnet->addPool(pdpool2);
     cfg.add(subnet);
-    
+
     // Unparse
     std::string expected = "[\n"
         "{\n"
@@ -518,4 +518,22 @@ TEST(CfgSubnets6Test, unparsePdPool) {
     runToElementTest<CfgSubnets6>(expected, cfg);
 }
 
+// This test verifies that it is possible to retrieve a subnet using subnet-id.
+TEST(CfgSubnets6Test, getSubnet) {
+    CfgSubnets6 cfg;
+
+    // Let's configure 3 subnets
+    Subnet6Ptr subnet1(new Subnet6(IOAddress("2001:db8:1::"), 48, 1, 2, 3, 4, 100));
+    Subnet6Ptr subnet2(new Subnet6(IOAddress("2001:db8:2::"), 48, 1, 2, 3, 4, 200));
+    Subnet6Ptr subnet3(new Subnet6(IOAddress("2001:db8:3::"), 48, 1, 2, 3, 4, 300));
+    cfg.add(subnet1);
+    cfg.add(subnet2);
+    cfg.add(subnet3);
+
+    EXPECT_EQ(subnet1, cfg.getSubnet(100));
+    EXPECT_EQ(subnet2, cfg.getSubnet(200));
+    EXPECT_EQ(subnet3, cfg.getSubnet(300));
+    EXPECT_EQ(Subnet6Ptr(), cfg.getSubnet(400)); // no such subnet
+}
+
 } // end of anonymous namespace