From: Tomek Mrugalski Date: Mon, 3 Feb 2014 20:15:23 +0000 (+0100) Subject: [3274] Client classification logic for IPv4 implemented. X-Git-Tag: bind10-1.2.0beta1-release~57^2~1^2~12 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=683c49b997b39ca4a064e21a11acbafc2ca7a4de;p=thirdparty%2Fkea.git [3274] Client classification logic for IPv4 implemented. --- diff --git a/src/lib/dhcpsrv/cfgmgr.cc b/src/lib/dhcpsrv/cfgmgr.cc index 4c5fb2a32d..4baaa02b23 100644 --- a/src/lib/dhcpsrv/cfgmgr.cc +++ b/src/lib/dhcpsrv/cfgmgr.cc @@ -211,7 +211,7 @@ void CfgMgr::addSubnet6(const Subnet6Ptr& subnet) { Subnet4Ptr CfgMgr::getSubnet4(const isc::asiolink::IOAddress& hint, - const isc::dhcp::ClientClasses& /*classes*/) { + const isc::dhcp::ClientClasses& classes) { // If there's only one subnet configured, let's just use it // The idea is to keep small deployments easy. In a small network - one @@ -231,6 +231,13 @@ CfgMgr::getSubnet4(const isc::asiolink::IOAddress& hint, // If there is more than one, we need to choose the proper one for (Subnet4Collection::iterator subnet = subnets4_.begin(); subnet != subnets4_.end(); ++subnet) { + + // If client is rejected because of not meeting client class criteria... + if (!(*subnet)->clientSupported(classes)) { + continue; + } + + // Let's check if the client belongs to the given subnet if ((*subnet)->inRange(hint)) { LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, DHCPSRV_CFGMGR_SUBNET4) diff --git a/src/lib/dhcpsrv/tests/cfgmgr_unittest.cc b/src/lib/dhcpsrv/tests/cfgmgr_unittest.cc index 29e826af30..f7689c9f0d 100644 --- a/src/lib/dhcpsrv/tests/cfgmgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/cfgmgr_unittest.cc @@ -388,6 +388,47 @@ TEST_F(CfgMgrTest, subnet4) { EXPECT_FALSE(cfg_mgr.getSubnet4(IOAddress("192.0.2.85"), classify_)); } +// This test verifies if the configuration manager is able to hold subnets with +// their classifier information and return proper subnets, based on those +// classes. +TEST_F(CfgMgrTest, classifySubnet4) { + CfgMgr& cfg_mgr = CfgMgr::instance(); + + // Let's configure 3 subnets + Subnet4Ptr subnet1(new Subnet4(IOAddress("192.0.2.0"), 26, 1, 2, 3)); + Subnet4Ptr subnet2(new Subnet4(IOAddress("192.0.2.64"), 26, 1, 2, 3)); + Subnet4Ptr subnet3(new Subnet4(IOAddress("192.0.2.128"), 26, 1, 2, 3)); + + cfg_mgr.addSubnet4(subnet1); + cfg_mgr.addSubnet4(subnet2); + cfg_mgr.addSubnet4(subnet3); + + // Let's sanity check that we can use that configuration. + EXPECT_EQ(subnet1, cfg_mgr.getSubnet4(IOAddress("192.0.2.5"), classify_)); + EXPECT_EQ(subnet2, cfg_mgr.getSubnet4(IOAddress("192.0.2.70"), classify_)); + EXPECT_EQ(subnet3, cfg_mgr.getSubnet4(IOAddress("192.0.2.130"), classify_)); + + // Client now belongs to bar class. + classify_.insert("bar"); + + // There are no class restrictions defined, so everything should work + // as before + EXPECT_EQ(subnet1, cfg_mgr.getSubnet4(IOAddress("192.0.2.5"), classify_)); + EXPECT_EQ(subnet2, cfg_mgr.getSubnet4(IOAddress("192.0.2.70"), classify_)); + EXPECT_EQ(subnet3, cfg_mgr.getSubnet4(IOAddress("192.0.2.130"), classify_)); + + // Now let's add client class restrictions. + subnet1->allowClientClass("foo"); // Serve here only clients from foo class + subnet2->allowClientClass("bar"); // Serve here only clients from bar class + subnet3->allowClientClass("baz"); // Serve here only clients from baz class + + // The same check as above should result in client being served only in + // bar class, i.e. subnet2 + EXPECT_FALSE(cfg_mgr.getSubnet4(IOAddress("192.0.2.5"), classify_)); + EXPECT_EQ(subnet2, cfg_mgr.getSubnet4(IOAddress("192.0.2.70"), classify_)); + EXPECT_FALSE(cfg_mgr.getSubnet4(IOAddress("192.0.2.130"), classify_)); +} + // This test verifies if the configuration manager is able to hold and return // valid leases TEST_F(CfgMgrTest, subnet6) {