summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorFrancis Dupont <fdupont@isc.org>2019-06-21 12:56:45 +0200
committerFrancis Dupont <fdupont@isc.org>2019-06-21 13:02:53 +0200
commit67108ee94778a9a02a34199d6b8b11771bdaf97f (patch)
tree10b0eba80e51e92e1115a2cbde785a67824d4746 /src
parent[527-check-return-value-of-multi-index-push_back] Added either explicit cast ... (diff)
downloadkea-67108ee94778a9a02a34199d6b8b11771bdaf97f.tar.xz
kea-67108ee94778a9a02a34199d6b8b11771bdaf97f.zip
[527-check-return-value-of-multi-index-push_back] Addressed comments
Added shared network getSubnet by prefix Fixed and completed subnet duplicate unit tests
Diffstat (limited to 'src')
-rw-r--r--src/hooks/dhcp/mysql_cb/mysql_cb_impl.cc9
-rw-r--r--src/lib/dhcp/libdhcp++.cc9
-rw-r--r--src/lib/dhcpsrv/shared_network.cc28
-rw-r--r--src/lib/dhcpsrv/shared_network.h8
-rw-r--r--src/lib/dhcpsrv/tests/shared_network_unittest.cc16
5 files changed, 56 insertions, 14 deletions
diff --git a/src/hooks/dhcp/mysql_cb/mysql_cb_impl.cc b/src/hooks/dhcp/mysql_cb/mysql_cb_impl.cc
index 251e2e1c4c..7a3c4a5aac 100644
--- a/src/hooks/dhcp/mysql_cb/mysql_cb_impl.cc
+++ b/src/hooks/dhcp/mysql_cb/mysql_cb_impl.cc
@@ -367,12 +367,9 @@ MySqlConfigBackendImpl::getOptionDefs(const int index,
last_def->setServerTag(out_bindings[10]->getString());
// Store created option definition.
- auto ret = option_defs.push_back(last_def);
- if (!ret.second) {
- // option_defs is a multi-index container so push_back()
- // can in theory fails. Now it has no unique indexes...
- isc_throw(Unexpected, "can't store the option definition");
- }
+ // (option_defs is a multi-index container with no unique
+ // indexes so push_back can't fail).
+ static_cast<void>(option_defs.push_back(last_def));
}
});
}
diff --git a/src/lib/dhcp/libdhcp++.cc b/src/lib/dhcp/libdhcp++.cc
index 2cd286ae77..a55313e618 100644
--- a/src/lib/dhcp/libdhcp++.cc
+++ b/src/lib/dhcp/libdhcp++.cc
@@ -1024,11 +1024,8 @@ void initOptionSpace(OptionDefContainerPtr& defs,
throw;
}
- auto ret = defs->push_back(definition);
- if (!ret.second) {
- // defs points to a multi index container with only not unique
- // indexes so this is for sanity only.
- isc_throw(isc::Unexpected, "can't store option definition");
- }
+ // option_defs is a multi-index container with no unique indexes
+ // so push_back can't fail).
+ static_cast<void>(defs->push_back(definition));
}
}
diff --git a/src/lib/dhcpsrv/shared_network.cc b/src/lib/dhcpsrv/shared_network.cc
index 8f760892c3..d4a21535f5 100644
--- a/src/lib/dhcpsrv/shared_network.cc
+++ b/src/lib/dhcpsrv/shared_network.cc
@@ -54,6 +54,9 @@ public:
if (getSubnet<SubnetPtrType>(subnets, subnet->getID())) {
isc_throw(DuplicateSubnetID, "attempted to add subnet with a"
" duplicated subnet identifier " << subnet->getID());
+ } else if (getSubnet<SubnetPtrType>(subnets, subnet->toText())) {
+ isc_throw(DuplicateSubnetID, "attempted to add subnet with a"
+ " duplicated subnet pre3fix " << subnet->toText());
}
// Check if the subnet is already associated with some network.
@@ -165,6 +168,31 @@ public:
return (SubnetPtrType());
}
+ /// @brief Returns a subnet belonging to this network for a given subnet
+ /// prefix.
+ ///
+ /// @param subnets Container holding subnets for this shared network.
+ /// @param subnet_prefix Prefix of a subnet being retrieved.
+ ///
+ /// @tparam SubnetPtrType Type of a pointer to a subnet, i.e. Subnet4Ptr
+ /// or @ref Subnet6Ptr.
+ /// @tparam SubnetCollectionType Type of a container holding subnets, i.e.
+ /// @ref Subnet4Collection or @ref Subnet6Collection.
+ ///
+ /// @return Pointer to the subnet or null if the subnet doesn't exist.
+ template<typename SubnetPtrType, typename SubnetCollectionType>
+ static SubnetPtrType getSubnet(const SubnetCollectionType& subnets,
+ const std::string& subnet_prefix) {
+ const auto& index = subnets.template get<SubnetPrefixIndexTag>();
+ auto subnet_it = index.find(subnet_prefix);
+ if (subnet_it != index.cend()) {
+ return (*subnet_it);
+ }
+
+ // Subnet not found.
+ return (SubnetPtrType());
+ }
+
/// @brief Retrieves next available subnet within shared network.
///
/// This method returns next available subnet within a shared network.
diff --git a/src/lib/dhcpsrv/shared_network.h b/src/lib/dhcpsrv/shared_network.h
index 2c450cbcde..2c132daf97 100644
--- a/src/lib/dhcpsrv/shared_network.h
+++ b/src/lib/dhcpsrv/shared_network.h
@@ -133,6 +133,14 @@ public:
/// if such subnet doesn't exist within shared network.
Subnet4Ptr getSubnet(const SubnetID& subnet_id) const;
+ /// @brief Returns a subnet for a specified subnet prefix.
+ ///
+ /// @param subnet_prefix Subnet prefix.
+ ///
+ /// @return Shared pointer to a subnet using this prefix or null pointer
+ /// if such subnet doesn't exist within shared network.
+ Subnet4Ptr getSubnet(const std::string& subnet_prefix) const;
+
/// @brief Retrieves next available IPv4 subnet within shared network.
///
/// See documentation for @ref SharedNetwork4::getNextSubnet.
diff --git a/src/lib/dhcpsrv/tests/shared_network_unittest.cc b/src/lib/dhcpsrv/tests/shared_network_unittest.cc
index ad1a756525..f068c8675c 100644
--- a/src/lib/dhcpsrv/tests/shared_network_unittest.cc
+++ b/src/lib/dhcpsrv/tests/shared_network_unittest.cc
@@ -112,7 +112,13 @@ TEST(SharedNetwork4Test, addSubnet4) {
// same ID should cause an error.
Subnet4Ptr subnet2(new Subnet4(IOAddress("192.0.2.0"), 24, 10, 20, 30,
SubnetID(15)));
- ASSERT_THROW(network->add(subnet), DuplicateSubnetID);
+ ASSERT_THROW(network->add(subnet2), DuplicateSubnetID);
+
+ // Create another subnet with the same prefix. Adding a network with the
+ // same prefix should cause an error.
+ subnet2.reset(new Subnet4(IOAddress("10.0.0.0"), 8, 10, 20, 30,
+ SubnetID(1234)));
+ ASSERT_THROW(network->add(subnet2), DuplicateSubnetID);
// Create another network and try to add a subnet to it. It should fail
// because the subnet is already associated with the first network.
@@ -719,7 +725,13 @@ TEST(SharedNetwork6Test, addSubnet6) {
// same ID should cause an error.
Subnet6Ptr subnet2(new Subnet6(IOAddress("3000::"), 16, 10, 20, 30, 40,
SubnetID(15)));
- ASSERT_THROW(network->add(subnet), DuplicateSubnetID);
+ ASSERT_THROW(network->add(subnet2), DuplicateSubnetID);
+
+ // Create another subnet with the same prefix. Adding a network with the
+ // same prefix should cause an error.
+ subnet2.reset(new Subnet6(IOAddress("2001:db8:1::"), 64, 10, 20, 30, 40,
+ SubnetID(1234)));
+ ASSERT_THROW(network->add(subnet2), DuplicateSubnetID);
// Create another network and try to add a subnet to it. It should fail
// because the subnet is already associated with the first network.