diff options
author | Marcin Siodelski <marcin@isc.org> | 2016-04-14 14:40:55 +0200 |
---|---|---|
committer | Marcin Siodelski <marcin@isc.org> | 2016-04-14 14:40:55 +0200 |
commit | 440fd061d391e5b65382f7a70a71456ebd017ef0 (patch) | |
tree | d4029e16f9ee0f3182e2f0b5ff7486d411214010 /src/lib/dhcpsrv | |
parent | [4303] Implemented parser for 'host-reservation-identifiers' list. (diff) | |
download | kea-440fd061d391e5b65382f7a70a71456ebd017ef0.tar.xz kea-440fd061d391e5b65382f7a70a71456ebd017ef0.zip |
[4303] Values in 'host-reservation-identifiers' used by the servers.
Both DHCPv4 and DHCPv6.
Diffstat (limited to 'src/lib/dhcpsrv')
-rw-r--r-- | src/lib/dhcpsrv/alloc_engine.cc | 92 | ||||
-rw-r--r-- | src/lib/dhcpsrv/alloc_engine.h | 26 |
2 files changed, 72 insertions, 46 deletions
diff --git a/src/lib/dhcpsrv/alloc_engine.cc b/src/lib/dhcpsrv/alloc_engine.cc index 7c7feec451..aa5c703ae5 100644 --- a/src/lib/dhcpsrv/alloc_engine.cc +++ b/src/lib/dhcpsrv/alloc_engine.cc @@ -12,6 +12,7 @@ #include <dhcp_ddns/ncr_msg.h> #include <dhcpsrv/alloc_engine.h> #include <dhcpsrv/alloc_engine_log.h> +#include <dhcpsrv/cfgmgr.h> #include <dhcpsrv/dhcpsrv_log.h> #include <dhcpsrv/host_mgr.h> #include <dhcpsrv/host.h> @@ -27,6 +28,7 @@ #include <boost/foreach.hpp> +#include <algorithm> #include <cstring> #include <sstream> #include <limits> @@ -301,6 +303,40 @@ AllocEngine::AllocatorPtr AllocEngine::getAllocator(Lease::Type type) { return (alloc->second); } +template<typename ContextType> +void +AllocEngine::findReservationInternal(ContextType& ctx, + const ConstCfgHostReservationsPtr& cfg, + const AllocEngine::HostGetFunc& host_get) { + ctx.host_.reset(); + + // We can only search for the reservation if a subnet has been selected. + if (ctx.subnet_) { + // Check which host reservation mode is supported in this subnet. + Subnet::HRMode hr_mode = ctx.subnet_->getHostReservationMode(); + + // Check if there is a host reseravtion for this client. Attempt to + // get host information + if (hr_mode != Subnet::HR_DISABLED) { + // Iterate over configured identifiers in the order of preference + // and try to use each of them to search for the reservations. + BOOST_FOREACH(const Host::IdentifierType& id, cfg->getIdentifierTypes()) { + IdentifierMap::const_iterator id_ctx = ctx.host_identifiers_.find(id); + if (id_ctx != ctx.host_identifiers_.end()) { + // Attempt to find a host using a specified identifier. + ctx.host_ = host_get(ctx.subnet_->getID(), id, + &id_ctx->second[0], id_ctx->second.size()); + // If we found matching host, return. + if (ctx.host_) { + return; + } + } + } + } + } +} + + // ########################################################################## // # DHCPv6 lease allocation code starts here. // ########################################################################## @@ -339,30 +375,12 @@ AllocEngine::ClientContext6::ClientContext6(const Subnet6Ptr& subnet, const Duid } -void AllocEngine::findReservation(ClientContext6& ctx) const { - if (!ctx.subnet_ || !ctx.duid_) { - return; - } - - ctx.host_.reset(); - - // Check which host reservation mode is supported in this subnet. - Subnet::HRMode hr_mode = ctx.subnet_->getHostReservationMode(); - - // Check if there's a host reservation for this client. Attempt to get - // host info only if reservations are not disabled. - if (hr_mode != Subnet::HR_DISABLED) { - - BOOST_FOREACH(const IdentifierPair& id, ctx.host_identifiers_) { - ctx.host_ = HostMgr::instance().get6(ctx.subnet_->getID(), - id.first, &id.second[0], - id.second.size()); - // If we found matching host, return. - if (ctx.host_) { - return; - } - } - } +void AllocEngine::findReservation(ClientContext6& ctx) { + ConstCfgHostReservationsPtr cfg = + CfgMgr::instance().getCurrentCfg()->getCfgHostReservations6(); + findReservationInternal(ctx, cfg, boost::bind(&HostMgr::get6, + &HostMgr::instance(), + _1, _2, _3, _4)); } Lease6Collection @@ -2117,27 +2135,11 @@ AllocEngine::allocateLease4(ClientContext4& ctx) { void AllocEngine::findReservation(ClientContext4& ctx) { - ctx.host_.reset(); - - // We can only search for the reservation if a subnet has been selected. - if (ctx.subnet_) { - // Check which host reservation mode is supported in this subnet. - Subnet::HRMode hr_mode = ctx.subnet_->getHostReservationMode(); - - // Check if there is a host reseravtion for this client. Attempt to - // get host information - if (hr_mode != Subnet::HR_DISABLED) { - BOOST_FOREACH(const IdentifierPair& id, ctx.host_identifiers_) { - ctx.host_ = HostMgr::instance().get4(ctx.subnet_->getID(), - id.first, &id.second[0], - id.second.size()); - // If we found matching host, return. - if (ctx.host_) { - return; - } - } - } - } + ConstCfgHostReservationsPtr cfg = + CfgMgr::instance().getCurrentCfg()->getCfgHostReservations4(); + findReservationInternal(ctx, cfg, boost::bind(&HostMgr::get4, + &HostMgr::instance(), + _1, _2, _3, _4)); } Lease4Ptr diff --git a/src/lib/dhcpsrv/alloc_engine.h b/src/lib/dhcpsrv/alloc_engine.h index b23f0ad1bf..25d2c975f3 100644 --- a/src/lib/dhcpsrv/alloc_engine.h +++ b/src/lib/dhcpsrv/alloc_engine.h @@ -13,6 +13,7 @@ #include <dhcp/pkt4.h> #include <dhcp/pkt6.h> #include <dhcp/option6_ia.h> +#include <dhcpsrv/cfg_host_reservations.h> #include <dhcpsrv/host.h> #include <dhcpsrv/subnet.h> #include <dhcpsrv/lease_mgr.h> @@ -617,10 +618,33 @@ public: /// Attempts to find appropriate host reservation in HostMgr. If found, it /// will be set in ctx.host_. /// @param ctx Client context that contains all necessary information. - void findReservation(ClientContext6& ctx) const; + static void findReservation(ClientContext6& ctx); private: + /// @brief Type of the function used by @ref findReservationInternal to + /// retrieve reservations by subnet identifier and host identifier. + typedef boost::function<ConstHostPtr(const SubnetID&, + const Host::IdentifierType&, + const uint8_t*, const size_t)> HostGetFunc; + + /// @brief Common function for searching host reservations. + /// + /// This is a common function called by variants of @ref findReservation + /// functions. + /// + /// @param ctx Reference to a @ref ClientContext6 or @ref ClientContext4. + /// @param cfg Pointer to object holding general configuration for host + /// reservations. It uses this object to read the desired order of + /// host identifiers to be used to search for reservations. + /// @param host_get Pointer to the @ref HostMgr functions to be used + /// to retrieve reservation by subnet identifier and host identifier. + /// @tparam ContextType Either @ref ClientContext6 or @ref ClientContext4. + template<typename ContextType> + static void findReservationInternal(ContextType& ctx, + const ConstCfgHostReservationsPtr& cfg, + const HostGetFunc& host_get); + /// @brief creates a lease and inserts it in LeaseMgr if necessary /// /// Creates a lease based on specified parameters and tries to insert it |