From: Marcin Siodelski Date: Tue, 5 Sep 2017 12:08:52 +0000 (+0200) Subject: [5305] Network4/Network6 specific params are now output by SharedNetworks X-Git-Tag: trac5073a_base~11^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=95fdb0e54c13cc51667d6a542c711409d7b52b71;p=thirdparty%2Fkea.git [5305] Network4/Network6 specific params are now output by SharedNetworks --- diff --git a/src/lib/dhcpsrv/network.cc b/src/lib/dhcpsrv/network.cc index 1b766c5027..ce0c9bdc23 100644 --- a/src/lib/dhcpsrv/network.cc +++ b/src/lib/dhcpsrv/network.cc @@ -102,5 +102,43 @@ Network::toElement() const { return (map); } +ElementPtr +Network4::toElement() const { + ElementPtr map = Network::toElement(); + + // Set match-client-id + map->set("match-client-id", Element::create(getMatchClientId())); + + return (map); +} + +ElementPtr +Network6::toElement() const { + ElementPtr map = Network::toElement(); + + // Set preferred-lifetime + map->set("preferred-lifetime", + Element::create(static_cast + (getPreferred().get()))); + + // Set interface-id + const OptionPtr& ifaceid = getInterfaceId(); + if (ifaceid) { + std::vector bin = ifaceid->getData(); + std::string ifid; + ifid.resize(bin.size()); + if (!bin.empty()) { + std::memcpy(&ifid[0], &bin[0], bin.size()); + } + map->set("interface-id", Element::create(ifid)); + } + + // Set rapid-commit + bool rapid_commit = getRapidCommit(); + map->set("rapid-commit", Element::create(rapid_commit)); + + return (map); +} + } // end of namespace isc::dhcp } // end of namespace isc diff --git a/src/lib/dhcpsrv/network.h b/src/lib/dhcpsrv/network.h index c5d9b9491d..9c91cc54cc 100644 --- a/src/lib/dhcpsrv/network.h +++ b/src/lib/dhcpsrv/network.h @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -304,12 +305,117 @@ typedef boost::shared_ptr NetworkPtr; /// @brief Weak pointer to the @ref Network object. typedef boost::weak_ptr WeakNetworkPtr; +/// @brief Specialization of the @ref Network object for DHCPv4 case. class Network4 : public Network { public: + + /// @brief Constructor. + Network4() + : Network(), match_client_id_(true) { + } + + /// @brief Returns the flag indicating if the client identifiers should + /// be used to identify the client's lease. + /// + /// @return true if client identifiers should be used, false otherwise. + bool getMatchClientId() const { + return (match_client_id_); + } + + /// @brief Sets the flag indicating if the client identifier should be + /// used to identify the client's lease. + /// + /// @param match If this value is true, the client identifiers are not + /// used for lease lookup. + void setMatchClientId(const bool match) { + match_client_id_ = match; + } + + /// @brief Unparses network object. + /// + /// @return A pointer to unparsed network configuration. + virtual data::ElementPtr toElement() const; + +private: + + /// @brief Should server use client identifiers for client lease + /// lookup. + bool match_client_id_; }; +/// @brief Specialization of the @ref Network object for DHCPv6 case. class Network6 : public Network { public: + + /// @brief Constructor. + Network6() + : Network(), preferred_(0), interface_id_(), rapid_commit_(false) { + setRelayInfo(asiolink::IOAddress::IPV6_ZERO_ADDRESS()); + } + + /// @brief Returns preferred lifetime (in seconds) + /// + /// @return a triplet with preferred lifetime + Triplet getPreferred() const { + return (preferred_); + } + + /// @brief Sets new preferred lifetime for a network. + /// + /// @param valid New preferred lifetime in seconds. + void setPreferred(const Triplet& preferred) { + preferred_ = preferred; + } + + /// @brief Returns interface-id value (if specified) + /// + /// @return interface-id option (if defined) + OptionPtr getInterfaceId() const { + return (interface_id_); + } + + /// @brief sets interface-id option (if defined) + /// + /// @param ifaceid pointer to interface-id option + void setInterfaceId(const OptionPtr& ifaceid) { + interface_id_ = ifaceid; + } + + /// @brief Returns boolean value indicating that the Rapid Commit option + /// is supported or unsupported for the subnet. + /// + /// @return true if the Rapid Commit option is supported, false otherwise. + bool getRapidCommit() const { + return (rapid_commit_); + } + + /// @brief Enables or disables Rapid Commit option support for the subnet. + /// + /// @param rapid_commit A boolean value indicating that the Rapid Commit + /// option support is enabled (if true), or disabled (if false). + void setRapidCommit(const bool rapid_commit) { + rapid_commit_ = rapid_commit; + }; + + /// @brief Unparses network object. + /// + /// @return A pointer to unparsed network configuration. + virtual data::ElementPtr toElement() const; + +private: + + /// @brief a triplet with preferred lifetime (in seconds) + Triplet preferred_; + + /// @brief specifies optional interface-id + OptionPtr interface_id_; + + /// @brief A flag indicating if Rapid Commit option is supported + /// for this network. + /// + /// It's default value is false, which indicates that the Rapid + /// Commit is disabled for the subnet. + bool rapid_commit_; }; } // end of namespace isc::dhcp diff --git a/src/lib/dhcpsrv/shared_network.cc b/src/lib/dhcpsrv/shared_network.cc index 17c9442829..5a38e0356a 100644 --- a/src/lib/dhcpsrv/shared_network.cc +++ b/src/lib/dhcpsrv/shared_network.cc @@ -241,7 +241,7 @@ SharedNetwork4::getNextSubnet(const Subnet4Ptr& first_subnet, ElementPtr SharedNetwork4::toElement() const { - ElementPtr map = Network::toElement(); + ElementPtr map = Network4::toElement(); // Set shared network name. if (!name_.empty()) { @@ -290,7 +290,7 @@ SharedNetwork6::getNextSubnet(const Subnet6Ptr& first_subnet, ElementPtr SharedNetwork6::toElement() const { - ElementPtr map = Network::toElement(); + ElementPtr map = Network6::toElement(); // Set shared network name. if (!name_.empty()) { diff --git a/src/lib/dhcpsrv/subnet.cc b/src/lib/dhcpsrv/subnet.cc index 8395a2b9e4..4317277a45 100644 --- a/src/lib/dhcpsrv/subnet.cc +++ b/src/lib/dhcpsrv/subnet.cc @@ -162,8 +162,8 @@ Subnet4::Subnet4(const isc::asiolink::IOAddress& prefix, uint8_t length, const Triplet& t2, const Triplet& valid_lifetime, const SubnetID id) - : Subnet(prefix, length, id), Network(), - siaddr_(IOAddress("0.0.0.0")), match_client_id_(true) { + : Subnet(prefix, length, id), Network4(), + siaddr_(IOAddress("0.0.0.0")) { if (!prefix.isV4()) { isc_throw(BadValue, "Non IPv4 prefix " << prefix.toText() << " specified in subnet4"); @@ -415,8 +415,7 @@ Subnet6::Subnet6(const isc::asiolink::IOAddress& prefix, uint8_t length, const Triplet& preferred_lifetime, const Triplet& valid_lifetime, const SubnetID id) - :Subnet(prefix, length, id), Network(), - preferred_(preferred_lifetime), rapid_commit_(false) { + : Subnet(prefix, length, id), Network6() { if (!prefix.isV6()) { isc_throw(BadValue, "Non IPv6 prefix " << prefix << " specified in subnet6"); @@ -427,6 +426,7 @@ Subnet6::Subnet6(const isc::asiolink::IOAddress& prefix, uint8_t length, // Timers. setT1(t1); setT2(t2); + setPreferred(preferred_lifetime); setValid(valid_lifetime); } @@ -457,13 +457,10 @@ data::ElementPtr Subnet4::toElement() const { // Prepare the map ElementPtr map = Subnet::toElement(); - ElementPtr network_map = Network::toElement(); + ElementPtr network_map = Network4::toElement(); merge(map, network_map); - // Set match-client-id - map->set("match-client-id", Element::create(getMatchClientId())); - // Set DHCP4o6 const Cfg4o6& d4o6 = get4o6(); isc::data::merge(map, d4o6.toElement()); @@ -488,30 +485,10 @@ data::ElementPtr Subnet6::toElement() const { // Prepare the map ElementPtr map = Subnet::toElement(); - ElementPtr network_map = Network::toElement(); + ElementPtr network_map = Network6::toElement(); merge(map, network_map); - // Set interface-id - const OptionPtr& ifaceid = getInterfaceId(); - if (ifaceid) { - std::vector bin = ifaceid->getData(); - std::string ifid; - ifid.resize(bin.size()); - if (!bin.empty()) { - std::memcpy(&ifid[0], &bin[0], bin.size()); - } - map->set("interface-id", Element::create(ifid)); - } - - // Set preferred-lifetime - map->set("preferred-lifetime", - Element::create(static_cast - (getPreferred().get()))); - // Set rapid-commit - bool rapid_commit = getRapidCommit(); - map->set("rapid-commit", Element::create(rapid_commit)); - // Set pools const PoolCollection& pools = getPools(Lease::TYPE_NA); ElementPtr pool_list = Element::createList(); diff --git a/src/lib/dhcpsrv/subnet.h b/src/lib/dhcpsrv/subnet.h index 9042619dc5..e086d95ae6 100644 --- a/src/lib/dhcpsrv/subnet.h +++ b/src/lib/dhcpsrv/subnet.h @@ -9,7 +9,6 @@ #include #include -#include #include #include #include @@ -359,7 +358,7 @@ typedef boost::shared_ptr SubnetPtr; /// @brief A configuration holder for IPv4 subnet. /// /// This class represents an IPv4 subnet. -class Subnet4 : public Subnet, public Network { +class Subnet4 : public Subnet, public Network4 { public: /// @brief Constructor with all parameters @@ -391,23 +390,6 @@ public: /// @return siaddr value isc::asiolink::IOAddress getSiaddr() const; - /// @brief Sets the flag indicating if the client identifier should be - /// used to identify the client's lease. - /// - /// @param match If this value is true, the client identifiers are not - /// used for lease lookup. - void setMatchClientId(const bool match) { - match_client_id_ = match; - } - - /// @brief Returns the flag indicating if the client identifiers should - /// be used to identify the client's lease. - /// - /// @return true if client identifiers should be used, false otherwise. - bool getMatchClientId() const { - return (match_client_id_); - } - /// @brief Returns DHCP4o6 configuration parameters. /// /// This structure is always available. If the 4o6 is not enabled, its @@ -448,10 +430,6 @@ private: /// @brief siaddr value for this subnet isc::asiolink::IOAddress siaddr_; - /// @brief Should server use client identifiers for client lease - /// lookup. - bool match_client_id_; - /// @brief All the information related to DHCP4o6 Cfg4o6 dhcp4o6_; }; @@ -466,7 +444,7 @@ typedef boost::shared_ptr Subnet4Ptr; /// @brief A configuration holder for IPv6 subnet. /// /// This class represents an IPv6 subnet. -class Subnet6 : public Subnet, public Network { +class Subnet6 : public Subnet, public Network6 { public: /// @brief Constructor with all parameters @@ -488,42 +466,6 @@ public: const Triplet& valid_lifetime, const SubnetID id = 0); - /// @brief Returns preferred lifetime (in seconds) - /// - /// @return a triplet with preferred lifetime - Triplet getPreferred() const { - return (preferred_); - } - - /// @brief sets interface-id option (if defined) - /// - /// @param ifaceid pointer to interface-id option - void setInterfaceId(const OptionPtr& ifaceid) { - interface_id_ = ifaceid; - } - - /// @brief returns interface-id value (if specified) - /// @return interface-id option (if defined) - OptionPtr getInterfaceId() const { - return interface_id_; - } - - /// @brief Enables or disables Rapid Commit option support for the subnet. - /// - /// @param rapid_commit A boolean value indicating that the Rapid Commit - /// option support is enabled (if true), or disabled (if false). - void setRapidCommit(const bool rapid_commit) { - rapid_commit_ = rapid_commit; - }; - - /// @brief Returns boolean value indicating that the Rapid Commit option - /// is supported or unsupported for the subnet. - /// - /// @return true if the Rapid Commit option is supported, false otherwise. - bool getRapidCommit() const { - return (rapid_commit_); - } - /// @brief Unparse a subnet object. /// /// @return A pointer to unparsed subnet configuration. @@ -545,19 +487,6 @@ private: /// @throw BadValue if invalid value is used virtual void checkType(Lease::Type type) const; - /// @brief specifies optional interface-id - OptionPtr interface_id_; - - /// @brief a triplet with preferred lifetime (in seconds) - Triplet preferred_; - - /// @brief A flag indicating if Rapid Commit option is supported - /// for this subnet. - /// - /// It's default value is false, which indicates that the Rapid - /// Commit is disabled for the subnet. - bool rapid_commit_; - }; /// @brief A const pointer to a @c Subnet6 object. diff --git a/src/lib/dhcpsrv/tests/cfg_shared_networks4_unittest.cc b/src/lib/dhcpsrv/tests/cfg_shared_networks4_unittest.cc index 47f5d553be..ff625e87aa 100644 --- a/src/lib/dhcpsrv/tests/cfg_shared_networks4_unittest.cc +++ b/src/lib/dhcpsrv/tests/cfg_shared_networks4_unittest.cc @@ -81,15 +81,31 @@ TEST(CfgSharedNetworks4Test, unparse) { "[\n" " {\n" " \"interface\": \"eth1\",\n" + " \"match-client-id\": true,\n" " \"name\": \"dog\",\n" " \"option-data\": [ ],\n" - " \"subnet4\": [ ]\n" + " \"rebind-timer\": 0,\n" + " \"relay\": {\n" + " \"ip-address\": \"0.0.0.0\"\n" + " },\n" + " \"renew-timer\": 0,\n" + " \"reservation-mode\": \"all\"," + " \"subnet4\": [ ],\n" + " \"valid-lifetime\": 0\n" " },\n" " {\n" " \"interface\": \"eth0\",\n" + " \"match-client-id\": true,\n" " \"name\": \"frog\",\n" " \"option-data\": [ ],\n" - " \"subnet4\": [ ]\n" + " \"rebind-timer\": 0,\n" + " \"relay\": {\n" + " \"ip-address\": \"0.0.0.0\"\n" + " },\n" + " \"renew-timer\": 0,\n" + " \"reservation-mode\": \"all\"," + " \"subnet4\": [ ],\n" + " \"valid-lifetime\": 0\n" " }\n" "]\n"; diff --git a/src/lib/dhcpsrv/tests/cfg_shared_networks6_unittest.cc b/src/lib/dhcpsrv/tests/cfg_shared_networks6_unittest.cc index 45f3bdcdee..3f261782fb 100644 --- a/src/lib/dhcpsrv/tests/cfg_shared_networks6_unittest.cc +++ b/src/lib/dhcpsrv/tests/cfg_shared_networks6_unittest.cc @@ -83,13 +83,31 @@ TEST(CfgSharedNetworks6Test, unparse) { " \"interface\": \"eth1\",\n" " \"name\": \"dog\",\n" " \"option-data\": [ ],\n" - " \"subnet6\": [ ]\n" + " \"preferred-lifetime\": 0,\n" + " \"rapid-commit\": false,\n" + " \"rebind-timer\": 0,\n" + " \"relay\": {\n" + " \"ip-address\": \"::\"\n" + " },\n" + " \"renew-timer\": 0,\n" + " \"reservation-mode\": \"all\"," + " \"subnet6\": [ ],\n" + " \"valid-lifetime\": 0\n" " },\n" " {\n" " \"interface\": \"eth0\",\n" " \"name\": \"frog\",\n" " \"option-data\": [ ],\n" - " \"subnet6\": [ ]\n" + " \"preferred-lifetime\": 0,\n" + " \"rapid-commit\": false,\n" + " \"rebind-timer\": 0,\n" + " \"relay\": {\n" + " \"ip-address\": \"::\"\n" + " },\n" + " \"renew-timer\": 0,\n" + " \"reservation-mode\": \"all\"," + " \"subnet6\": [ ],\n" + " \"valid-lifetime\": 0\n" " }\n" "]\n"; diff --git a/src/lib/dhcpsrv/tests/shared_network_unittest.cc b/src/lib/dhcpsrv/tests/shared_network_unittest.cc index d2b6bff743..8a90bb51c1 100644 --- a/src/lib/dhcpsrv/tests/shared_network_unittest.cc +++ b/src/lib/dhcpsrv/tests/shared_network_unittest.cc @@ -190,6 +190,11 @@ TEST(SharedNetwork4Test, unparse) { // Set interface name. network->setIface("eth1"); + network->setT1(100); + network->setT2(150); + network->setValid(200); + network->setMatchClientId(false); + // Add several subnets. Subnet4Ptr subnet1(new Subnet4(IOAddress("10.0.0.0"), 8, 10, 20, 30, SubnetID(1))); @@ -200,8 +205,15 @@ TEST(SharedNetwork4Test, unparse) { std::string expected = "{\n" " \"interface\": \"eth1\",\n" + " \"match-client-id\": false,\n" " \"name\": \"frog\",\n" " \"option-data\": [ ],\n" + " \"rebind-timer\": 150,\n" + " \"relay\": {\n" + " \"ip-address\": \"0.0.0.0\"\n" + " },\n" + " \"renew-timer\": 100,\n" + " \"reservation-mode\": \"all\"," " \"subnet4\": [\n" " {\n" " \"4o6-interface\": \"\",\n" @@ -239,7 +251,8 @@ TEST(SharedNetwork4Test, unparse) { " \"subnet\": \"192.0.2.0/24\",\n" " \"valid-lifetime\": 30\n" " }\n" - " ]\n" + " ],\n" + " \"valid-lifetime\": 200\n" "}\n"; test::runToElementTest(expected, *network); @@ -436,6 +449,11 @@ TEST(SharedNetwork6Test, getNextSubnet) { TEST(SharedNetwork6Test, unparse) { SharedNetwork6Ptr network(new SharedNetwork6("frog")); network->setIface("eth1"); + network->setT1(100); + network->setT2(150); + network->setPreferred(200); + network->setValid(300); + network->setRapidCommit(true); // Add several subnets. Subnet6Ptr subnet1(new Subnet6(IOAddress("2001:db8:1::"), 64, 10, 20, 30, @@ -449,6 +467,14 @@ TEST(SharedNetwork6Test, unparse) { " \"interface\": \"eth1\",\n" " \"name\": \"frog\",\n" " \"option-data\": [ ],\n" + " \"preferred-lifetime\": 200,\n" + " \"rapid-commit\": true,\n" + " \"rebind-timer\": 150,\n" + " \"relay\": {\n" + " \"ip-address\": \"::\"\n" + " },\n" + " \"renew-timer\": 100,\n" + " \"reservation-mode\": \"all\"," " \"subnet6\": [\n" " {\n" " \"id\": 1,\n" @@ -482,7 +508,8 @@ TEST(SharedNetwork6Test, unparse) { " \"subnet\": \"3000::/16\",\n" " \"valid-lifetime\": 40\n" " }\n" - " ]\n" + " ],\n" + " \"valid-lifetime\": 300\n" "}\n"; test::runToElementTest(expected, *network);