From: Tomek Mrugalski Date: Tue, 1 Aug 2017 18:52:15 +0000 (+0200) Subject: [5272] getSubnet implemented. X-Git-Tag: trac5124a_base~20^2~13 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c16e3e91e6430e9b7e37c5c91df1df93ea7501dd;p=thirdparty%2Fkea.git [5272] getSubnet implemented. --- diff --git a/src/lib/dhcpsrv/cfg_subnets4.cc b/src/lib/dhcpsrv/cfg_subnets4.cc index 136f213d45..ef3579ce69 100644 --- a/src/lib/dhcpsrv/cfg_subnets4.cc +++ b/src/lib/dhcpsrv/cfg_subnets4.cc @@ -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 { diff --git a/src/lib/dhcpsrv/cfg_subnets4.h b/src/lib/dhcpsrv/cfg_subnets4.h index c2b72429e0..d37aa112af 100644 --- a/src/lib/dhcpsrv/cfg_subnets4.h +++ b/src/lib/dhcpsrv/cfg_subnets4.h @@ -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 diff --git a/src/lib/dhcpsrv/cfg_subnets6.cc b/src/lib/dhcpsrv/cfg_subnets6.cc index a795fe4a60..03ec44f84e 100644 --- a/src/lib/dhcpsrv/cfg_subnets6.cc +++ b/src/lib/dhcpsrv/cfg_subnets6.cc @@ -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(); diff --git a/src/lib/dhcpsrv/cfg_subnets6.h b/src/lib/dhcpsrv/cfg_subnets6.h index 18e1ca866f..e5ed5adb06 100644 --- a/src/lib/dhcpsrv/cfg_subnets6.h +++ b/src/lib/dhcpsrv/cfg_subnets6.h @@ -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 diff --git a/src/lib/dhcpsrv/tests/cfg_subnets4_unittest.cc b/src/lib/dhcpsrv/tests/cfg_subnets4_unittest.cc index d7433f2d8c..2ff407a829 100644 --- a/src/lib/dhcpsrv/tests/cfg_subnets4_unittest.cc +++ b/src/lib/dhcpsrv/tests/cfg_subnets4_unittest.cc @@ -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(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 diff --git a/src/lib/dhcpsrv/tests/cfg_subnets6_unittest.cc b/src/lib/dhcpsrv/tests/cfg_subnets6_unittest.cc index c655686597..a9bcb3de80 100644 --- a/src/lib/dhcpsrv/tests/cfg_subnets6_unittest.cc +++ b/src/lib/dhcpsrv/tests/cfg_subnets6_unittest.cc @@ -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(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