From: Thomas Markwalder Date: Thu, 10 Aug 2017 17:54:51 +0000 (-0400) Subject: [master] Added subnet manipulation support X-Git-Tag: trac5124a_base~9 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=894aeb048b7281f947f87a5d6dc5112fac4a86fb;p=thirdparty%2Fkea.git [master] Added subnet manipulation support Merge branch 'trac5314' --- 894aeb048b7281f947f87a5d6dc5112fac4a86fb diff --cc src/bin/dhcp6/tests/get_config_unittest.cc index f7c7eccbad,3eb6a96e86..12e285a51b --- a/src/bin/dhcp6/tests/get_config_unittest.cc +++ b/src/bin/dhcp6/tests/get_config_unittest.cc @@@ -3248,11 -3189,8 +3192,9 @@@ const char* UNPARSED_CONFIGS[] = " \"subnet6\": [\n" " {\n" " \"id\": 1,\n" - " \"interface\": \"\",\n" - " \"interface-id\": \"\",\n" " \"option-data\": [\n" " {\n" +" \"always-send\": false,\n" " \"code\": 38,\n" " \"csv-format\": false,\n" " \"data\": \"ABCDEF0105\",\n" @@@ -3660,11 -3586,8 +3598,9 @@@ " \"subnet6\": [\n" " {\n" " \"id\": 1,\n" - " \"interface\": \"\",\n" - " \"interface-id\": \"\",\n" " \"option-data\": [\n" " {\n" +" \"always-send\": false,\n" " \"code\": 38,\n" " \"csv-format\": false,\n" " \"data\": \"0102030405060708090A\",\n" @@@ -3693,11 -3616,8 +3629,9 @@@ " },\n" " {\n" " \"id\": 2,\n" - " \"interface\": \"\",\n" - " \"interface-id\": \"\",\n" " \"option-data\": [\n" " {\n" +" \"always-send\": false,\n" " \"code\": 15,\n" " \"csv-format\": false,\n" " \"data\": \"FFFEFDFCFB\",\n" @@@ -3784,11 -3702,8 +3716,9 @@@ " \"pd-pools\": [\n" " {\n" " \"delegated-len\": 64,\n" - " \"excluded-prefix\": \"::\",\n" - " \"excluded-prefix-len\": 0,\n" " \"option-data\": [\n" " {\n" +" \"always-send\": false,\n" " \"code\": 38,\n" " \"csv-format\": false,\n" " \"data\": \"112233445566\",\n" @@@ -3801,11 -3716,8 +3731,9 @@@ " },\n" " {\n" " \"delegated-len\": 64,\n" - " \"excluded-prefix\": \"::\",\n" - " \"excluded-prefix-len\": 0,\n" " \"option-data\": [\n" " {\n" +" \"always-send\": false,\n" " \"code\": 15,\n" " \"csv-format\": false,\n" " \"data\": \"AABBCCDDEE\",\n" diff --cc src/lib/dhcpsrv/cfg_subnets4.cc index ef3579ce69,26b079b2c9..f5fcd232c0 --- a/src/lib/dhcpsrv/cfg_subnets4.cc +++ b/src/lib/dhcpsrv/cfg_subnets4.cc @@@ -34,6 -34,20 +34,20 @@@ CfgSubnets4::add(const Subnet4Ptr& subn subnets_.push_back(subnet); } + ConstSubnet4Ptr + CfgSubnets4::getBySubnetId(const SubnetID& subnet_id) const { - const auto& index = subnets_.get(); ++ const auto& index = subnets_.get(); + auto subnet_it = index.find(subnet_id); + return ((subnet_it != index.cend()) ? (*subnet_it) : ConstSubnet4Ptr()); + } + + ConstSubnet4Ptr + CfgSubnets4::getByPrefix(const std::string& subnet_text) const { + const auto& index = subnets_.get(); + auto subnet_it = index.find(subnet_text); + return ((subnet_it != index.cend()) ? (*subnet_it) : ConstSubnet4Ptr()); + } + Subnet4Ptr CfgSubnets4::selectSubnet4o6(const SubnetSelector& selector) const { diff --cc src/lib/dhcpsrv/cfg_subnets6.cc index 03ec44f84e,491fdb227e..111fed0997 --- a/src/lib/dhcpsrv/cfg_subnets6.cc +++ b/src/lib/dhcpsrv/cfg_subnets6.cc @@@ -33,6 -33,20 +33,20 @@@ CfgSubnets6::add(const Subnet6Ptr& subn subnets_.push_back(subnet); } + ConstSubnet6Ptr + CfgSubnets6::getBySubnetId(const SubnetID& subnet_id) const { - const auto& index = subnets_.get(); ++ const auto& index = subnets_.get(); + auto subnet_it = index.find(subnet_id); + return ((subnet_it != index.cend()) ? (*subnet_it) : ConstSubnet6Ptr()); + } + + ConstSubnet6Ptr + CfgSubnets6::getByPrefix(const std::string& subnet_text) const { + const auto& index = subnets_.get(); + auto subnet_it = index.find(subnet_text); + return ((subnet_it != index.cend()) ? (*subnet_it) : ConstSubnet6Ptr()); + } + Subnet6Ptr CfgSubnets6::selectSubnet(const SubnetSelector& selector) const { Subnet6Ptr subnet; diff --cc src/lib/dhcpsrv/subnet.h index 5fc2112d15,638eb11cf8..3d91ae8bb9 --- a/src/lib/dhcpsrv/subnet.h +++ b/src/lib/dhcpsrv/subnet.h @@@ -710,8 -738,77 +738,77 @@@ typedef boost::shared_ptr Subnet6Ptr; - /// @brief A collection of Subnet6 objects - typedef std::vector Subnet6Collection; + /// @name Definition of the multi index container holding subnet information + /// + //@{ + + /// @brief Tag for the random access index. + struct SubnetRandomAccessIndexTag { }; + + /// @brief Tag for the index for searching by subnet identifier. -struct SubnetIdIndexTag { }; ++struct SubnetSubnetIdIndexTag { }; + + /// @brief Tag for the index for searching by subnet prefix. + struct SubnetPrefixIndexTag { }; + + /// @brief Multi index container holding subnets. + /// + /// This multi index container can hold pointers to @ref Subnet4 or + /// @ref Subnet6 objects representing subnets. It provides indexes for + /// subnet lookups using subnet properties such as: subnet identifier + /// or subnet prefix. It also provides a random access index which + /// allows for using the container like a vector. + /// + /// The random access index is used by the DHCP servers which perform + /// a full scan on subnets to find the one that matches some specific + /// criteria for subnet selection. + /// + /// The remaining indexes are used for searching for a specific subnet + /// as a result of receiving a command over the control API, e.g. + /// when 'subnet-get' command is received. + /// + /// @todo We should consider optimizing subnet selection by leveraging + /// the indexing capabilities of this container, e.g. searching for + /// a subnet by interface name, relay address etc. + /// + /// @tparam SubnetType Type of the subnet: @ref Subnet4 or @ref Subnet6. + template + using SubnetCollection = boost::multi_index_container< + // Multi index container holds pointers to the subnets. + boost::shared_ptr, + // The following holds all indexes. + boost::multi_index::indexed_by< + // First is the random access index allowing for accessing + // objects just like we'd do with a vector. + boost::multi_index::random_access< + boost::multi_index::tag + >, + // Second index allows for searching using subnet identifier. + boost::multi_index::ordered_unique< - boost::multi_index::tag, ++ boost::multi_index::tag, + boost::multi_index::const_mem_fun + >, + // Third index allows for searching using an output from toText function. + boost::multi_index::ordered_unique< + boost::multi_index::tag, + boost::multi_index::const_mem_fun + > + > + >; + + /// @brief A collection of @c Subnet4 objects + /// + /// This container provides a set of indexes which can be used to retrieve + /// subnets by various properties. + typedef SubnetCollection Subnet4Collection; + + /// @brief A collection of @c Subnet6 objects + /// + /// This container provides a set of indexes which can be used to retrieve + /// subnets by various properties. + typedef SubnetCollection Subnet6Collection; + + //@} } // end of isc::dhcp namespace } // end of isc namespace