Browse Source

[3242] Added function to check that the iface's address belongs to subnet.

Marcin Siodelski 11 years ago
parent
commit
52dc56691f

+ 22 - 1
src/lib/dhcpsrv/cfgmgr.cc

@@ -1,4 +1,4 @@
-// Copyright (C) 2012-2013 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2012-2014 Internet Systems Consortium, Inc. ("ISC")
 //
 // Permission to use, copy, modify, and/or distribute this software for any
 // purpose with or without fee is hereby granted, provided that the above
@@ -13,6 +13,7 @@
 // PERFORMANCE OF THIS SOFTWARE.
 
 #include <asiolink/io_address.h>
+#include <dhcp/iface_mgr.h>
 #include <dhcp/libdhcp++.h>
 #include <dhcpsrv/cfgmgr.h>
 #include <dhcpsrv/dhcpsrv_log.h>
@@ -241,6 +242,26 @@ CfgMgr::getSubnet4(const isc::asiolink::IOAddress& hint) {
     return (Subnet4Ptr());
 }
 
+bool
+CfgMgr::belongsToSubnet4(const std::string& iface_name) const {
+    Iface* iface = IfaceMgr::instance().getIface(iface_name);
+    if (iface == NULL) {
+        isc_throw(isc::BadValue, "interface " << iface_name << " doesn't exist");
+    }
+    for (Iface::AddressCollection::const_iterator addr = iface->getAddresses().begin();
+         addr != iface->getAddresses().end(); ++addr) {
+        if (addr->isV4()) {
+            for (Subnet4Collection::const_iterator subnet = subnets4_.begin();
+                 subnet != subnets4_.end(); ++subnet) {
+                if ((*subnet)->inRange(*addr)) {
+                    return (true);
+                }
+            }
+        }
+    }
+    return (false);
+}
+
 void CfgMgr::addSubnet4(const Subnet4Ptr& subnet) {
     /// @todo: Check that this new subnet does not cross boundaries of any
     /// other already defined subnet.

+ 13 - 1
src/lib/dhcpsrv/cfgmgr.h

@@ -1,4 +1,4 @@
-// Copyright (C) 2012-2013 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2012-2014 Internet Systems Consortium, Inc. ("ISC")
 //
 // Permission to use, copy, modify, and/or distribute this software for any
 // purpose with or without fee is hereby granted, provided that the above
@@ -246,6 +246,18 @@ public:
     /// @return a subnet object
     Subnet4Ptr getSubnet4(const isc::asiolink::IOAddress& hint);
 
+    /// @brief Checks that the IP address assigned to an interface belongs to
+    /// any subnet.
+    ///
+    /// This function checks that the IP address assigned to the specified
+    /// interface belongs to any IPv4 subnet configured.
+    ///
+    /// @param iface Short name of the interface which is being checked.
+    ///
+    /// @return Boolean value which indicates that the IP address belons to any
+    /// subnet (if true), false otherwise.
+    bool belongsToSubnet4(const std::string& iface) const;
+
     /// @brief adds a subnet4
     void addSubnet4(const Subnet4Ptr& subnet);
 

+ 1 - 0
src/lib/dhcpsrv/tests/Makefile.am

@@ -88,6 +88,7 @@ libdhcpsrv_unittests_CXXFLAGS += -Wno-unused-variable -Wno-unused-parameter
 endif
 
 libdhcpsrv_unittests_LDADD  = $(top_builddir)/src/lib/dhcpsrv/libb10-dhcpsrv.la
+libdhcpsrv_unittests_LDADD += $(top_builddir)/src/lib/dhcp/tests/libdhcptest.la
 libdhcpsrv_unittests_LDADD += $(top_builddir)/src/lib/dhcp/libb10-dhcp++.la
 libdhcpsrv_unittests_LDADD += $(top_builddir)/src/lib/dhcp_ddns/libb10-dhcp_ddns.la
 libdhcpsrv_unittests_LDADD += $(top_builddir)/src/lib/config/libb10-cfgclient.la

+ 24 - 0
src/lib/dhcpsrv/tests/cfgmgr_unittest.cc

@@ -18,6 +18,7 @@
 #include <dhcpsrv/dhcp_parsers.h>
 #include <exceptions/exceptions.h>
 #include <dhcp/dhcp6.h>
+#include <dhcp/tests/iface_mgr_test_config.h>
 
 #include <gtest/gtest.h>
 
@@ -29,6 +30,7 @@
 using namespace std;
 using namespace isc::asiolink;
 using namespace isc::dhcp;
+using namespace isc::dhcp::test;
 using namespace isc::util;
 using namespace isc;
 
@@ -709,6 +711,28 @@ TEST_F(CfgMgrTest, d2ClientConfig) {
     EXPECT_NE(*original_config, *updated_config);
 }
 
+// This test verfies that CfgMgr correctly determines that the address of the
+// interface belongs to existing IPv4 subnet.
+TEST_F(CfgMgrTest, belongsToSubnet4) {
+    IfaceMgrTestConfig config(true);
+
+    ASSERT_FALSE(CfgMgr::instance().belongsToSubnet4("eth0"));
+    ASSERT_FALSE(CfgMgr::instance().belongsToSubnet4("eth1"));
+
+    Subnet4Ptr subnet1(new Subnet4(IOAddress("10.0.0.1"), 24, 1, 2, 3));
+    CfgMgr::instance().addSubnet4(subnet1);
+
+    EXPECT_TRUE(CfgMgr::instance().belongsToSubnet4("eth0"));
+    EXPECT_FALSE(CfgMgr::instance().belongsToSubnet4("eth1"));
+
+    Subnet4Ptr subnet2(new Subnet4(IOAddress("192.0.2.1"), 24, 1, 2, 3));
+    CfgMgr::instance().addSubnet4(subnet2);
+
+    EXPECT_TRUE(CfgMgr::instance().belongsToSubnet4("eth0"));
+    EXPECT_TRUE(CfgMgr::instance().belongsToSubnet4("eth1"));
+
+}
+
 
 /// @todo Add unit-tests for testing:
 /// - addActiveIface() with invalid interface name