Browse Source

[3709] createLease4() now uses ClientContext4

Tomek Mrugalski 10 years ago
parent
commit
9c8ff76f41
2 changed files with 51 additions and 65 deletions
  1. 28 39
      src/lib/dhcpsrv/alloc_engine.cc
  2. 23 26
      src/lib/dhcpsrv/alloc_engine.h

+ 28 - 39
src/lib/dhcpsrv/alloc_engine.cc

@@ -926,13 +926,7 @@ AllocEngine::allocateLease4(ClientContext4& ctx) {
                 if (!existing) {
                 if (!existing) {
                     // The candidate address is currently unused. Let's create a
                     // The candidate address is currently unused. Let's create a
                     // lease for it.
                     // lease for it.
-                    Lease4Ptr lease = createLease4(ctx.subnet_, ctx.clientid_,
-                                                   ctx.hwaddr_, candidate,
-                                                   ctx.fwd_dns_update_,
-                                                   ctx.rev_dns_update_,
-                                                   ctx.hostname_,
-                                                   ctx.callout_handle_,
-                                                   ctx.fake_allocation_);
+                    Lease4Ptr lease = createLease4(ctx, candidate);
 
 
                     // If we have allocated the lease let's return it. Also,
                     // If we have allocated the lease let's return it. Also,
                     // always return when tried to allocate reserved address,
                     // always return when tried to allocate reserved address,
@@ -995,10 +989,7 @@ AllocEngine::allocateLease4(ClientContext4& ctx) {
             if (!existing) {
             if (!existing) {
                 // there's no existing lease for selected candidate, so it is
                 // there's no existing lease for selected candidate, so it is
                 // free. Let's allocate it.
                 // free. Let's allocate it.
-                Lease4Ptr lease = createLease4(ctx.subnet_, ctx.clientid_, ctx.hwaddr_,
-                                               candidate, ctx.fwd_dns_update_,
-                                               ctx.rev_dns_update_, ctx.hostname_,
-                                               ctx.callout_handle_, ctx.fake_allocation_);
+                Lease4Ptr lease = createLease4(ctx, candidate);
                 if (lease) {
                 if (lease) {
                     return (lease);
                     return (lease);
                 }
                 }
@@ -1457,41 +1448,39 @@ Lease6Ptr AllocEngine::createLease6(ClientContext6& ctx,
     }
     }
 }
 }
 
 
-Lease4Ptr AllocEngine::createLease4(const SubnetPtr& subnet,
-                                    const DuidPtr& clientid,
-                                    const HWAddrPtr& hwaddr,
-                                    const IOAddress& addr,
-                                    const bool fwd_dns_update,
-                                    const bool rev_dns_update,
-                                    const std::string& hostname,
-                                    const isc::hooks::CalloutHandlePtr& callout_handle,
-                                    bool fake_allocation /*= false */ ) {
-    if (!hwaddr) {
+Lease4Ptr AllocEngine::createLease4(const ClientContext4& ctx,
+                                    const IOAddress& addr) {
+    if (!ctx.hwaddr_) {
         isc_throw(BadValue, "Can't create a lease with NULL HW address");
         isc_throw(BadValue, "Can't create a lease with NULL HW address");
     }
     }
+    if (!ctx.subnet_) {
+        isc_throw(BadValue, "Can't create a lease without a subnet");
+    }
+
     time_t now = time(NULL);
     time_t now = time(NULL);
 
 
     // @todo: remove this kludge after ticket #2590 is implemented
     // @todo: remove this kludge after ticket #2590 is implemented
     std::vector<uint8_t> local_copy;
     std::vector<uint8_t> local_copy;
-    if (clientid) {
-        local_copy = clientid->getDuid();
+    if (ctx.clientid_) {
+        local_copy = ctx.clientid_->getDuid();
     }
     }
 
 
-    Lease4Ptr lease(new Lease4(addr, hwaddr, &local_copy[0], local_copy.size(),
-                               subnet->getValid(), subnet->getT1(), subnet->getT2(),
-                               now, subnet->getID()));
+    Lease4Ptr lease(new Lease4(addr, ctx.hwaddr_, &local_copy[0], local_copy.size(),
+                               ctx.subnet_->getValid(), ctx.subnet_->getT1(),
+                               ctx.subnet_->getT2(),
+                               now, ctx.subnet_->getID()));
 
 
     // Set FQDN specific lease parameters.
     // Set FQDN specific lease parameters.
-    lease->fqdn_fwd_ = fwd_dns_update;
-    lease->fqdn_rev_ = rev_dns_update;
-    lease->hostname_ = hostname;
+    lease->fqdn_fwd_ = ctx.fwd_dns_update_;
+    lease->fqdn_rev_ = ctx.rev_dns_update_;
+    lease->hostname_ = ctx.hostname_;
 
 
     // Let's execute all callouts registered for lease4_select
     // Let's execute all callouts registered for lease4_select
-    if (callout_handle &&
+    if (ctx.callout_handle_ &&
         HooksManager::getHooksManager().calloutsPresent(hook_index_lease4_select_)) {
         HooksManager::getHooksManager().calloutsPresent(hook_index_lease4_select_)) {
 
 
         // Delete all previous arguments
         // Delete all previous arguments
-        callout_handle->deleteAllArguments();
+        ctx.callout_handle_->deleteAllArguments();
 
 
         // Pass necessary arguments
         // Pass necessary arguments
 
 
@@ -1499,32 +1488,32 @@ Lease4Ptr AllocEngine::createLease4(const SubnetPtr& subnet,
         // with using SubnetPtr to point to Subnet4 object. Users should not
         // with using SubnetPtr to point to Subnet4 object. Users should not
         // be confused with dynamic_pointer_casts. They should get a concrete
         // be confused with dynamic_pointer_casts. They should get a concrete
         // pointer (Subnet4Ptr) pointing to a Subnet4 object.
         // pointer (Subnet4Ptr) pointing to a Subnet4 object.
-        Subnet4Ptr subnet4 = boost::dynamic_pointer_cast<Subnet4>(subnet);
-        callout_handle->setArgument("subnet4", subnet4);
+        Subnet4Ptr subnet4 = boost::dynamic_pointer_cast<Subnet4>(ctx.subnet_);
+        ctx.callout_handle_->setArgument("subnet4", subnet4);
 
 
         // Is this solicit (fake = true) or request (fake = false)
         // Is this solicit (fake = true) or request (fake = false)
-        callout_handle->setArgument("fake_allocation", fake_allocation);
+        ctx.callout_handle_->setArgument("fake_allocation", ctx.fake_allocation_);
 
 
         // Pass the intended lease as well
         // Pass the intended lease as well
-        callout_handle->setArgument("lease4", lease);
+        ctx.callout_handle_->setArgument("lease4", lease);
 
 
         // This is the first callout, so no need to clear any arguments
         // This is the first callout, so no need to clear any arguments
-        HooksManager::callCallouts(hook_index_lease4_select_, *callout_handle);
+        HooksManager::callCallouts(hook_index_lease4_select_, *ctx.callout_handle_);
 
 
         // Callouts decided to skip the action. This means that the lease is not
         // Callouts decided to skip the action. This means that the lease is not
         // assigned, so the client will get NoAddrAvail as a result. The lease
         // assigned, so the client will get NoAddrAvail as a result. The lease
         // won't be inserted into the database.
         // won't be inserted into the database.
-        if (callout_handle->getSkip()) {
+        if (ctx.callout_handle_->getSkip()) {
             LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_HOOKS, DHCPSRV_HOOK_LEASE4_SELECT_SKIP);
             LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_HOOKS, DHCPSRV_HOOK_LEASE4_SELECT_SKIP);
             return (Lease4Ptr());
             return (Lease4Ptr());
         }
         }
 
 
         // Let's use whatever callout returned. Hopefully it is the same lease
         // Let's use whatever callout returned. Hopefully it is the same lease
         // we handled to it.
         // we handled to it.
-        callout_handle->getArgument("lease4", lease);
+        ctx.callout_handle_->getArgument("lease4", lease);
     }
     }
 
 
-    if (!fake_allocation) {
+    if (!ctx.fake_allocation_) {
         // That is a real (REQUEST) allocation
         // That is a real (REQUEST) allocation
         bool status = LeaseMgrFactory::instance().addLease(lease);
         bool status = LeaseMgrFactory::instance().addLease(lease);
         if (status) {
         if (status) {

+ 23 - 26
src/lib/dhcpsrv/alloc_engine.h

@@ -234,10 +234,11 @@ protected:
         }
         }
 
 
         /// @brief Constructor with parameters
         /// @brief Constructor with parameters
+        ///
         /// @param subnet subnet the allocation should come from (mandatory)
         /// @param subnet subnet the allocation should come from (mandatory)
         /// @param clientid Client identifier (optional)
         /// @param clientid Client identifier (optional)
         /// @param hwaddr Client's hardware address info (mandatory)
         /// @param hwaddr Client's hardware address info (mandatory)
-        /// @param hint A hint that the client provided (may be 0.0.0.0)
+        /// @param requested_addr A hint that the client provided (may be 0.0.0.0)
         /// @param fwd_dns_update Indicates whether forward DNS
         /// @param fwd_dns_update Indicates whether forward DNS
         ///      update will be performed for the client (true) or not (false).
         ///      update will be performed for the client (true) or not (false).
         /// @param rev_dns_update Indicates whether reverse DNS
         /// @param rev_dns_update Indicates whether reverse DNS
@@ -586,7 +587,7 @@ protected:
     /// @ref ClientContext4::subnet_ subnet the allocation should come from
     /// @ref ClientContext4::subnet_ subnet the allocation should come from
     /// @ref ClientContext4::clientid_ Client identifier
     /// @ref ClientContext4::clientid_ Client identifier
     /// @ref ClientContext4::hwaddr_ Client's hardware address info
     /// @ref ClientContext4::hwaddr_ Client's hardware address info
-    /// @ref ClientContext4::hint_ A hint that the client provided
+    /// @ref ClientContext4::requested_address_ A hint that the client provided
     /// @ref ClientContext4::fwd_dns_update_ Indicates whether forward DNS
     /// @ref ClientContext4::fwd_dns_update_ Indicates whether forward DNS
     ///      update will be performed for the client (true) or not (false).
     ///      update will be performed for the client (true) or not (false).
     /// @ref ClientContext4::rev_dns_update_ Indicates whether reverse DNS
     /// @ref ClientContext4::rev_dns_update_ Indicates whether reverse DNS
@@ -702,7 +703,6 @@ protected:
     Lease6Collection
     Lease6Collection
     allocateLeases6(ClientContext6& ctx);
     allocateLeases6(ClientContext6& ctx);
 
 
-
     /// @brief Renews existing DHCPv6 leases for a given IA.
     /// @brief Renews existing DHCPv6 leases for a given IA.
     ///
     ///
     /// This method updates the leases associated with a specified IA container.
     /// This method updates the leases associated with a specified IA container.
@@ -725,8 +725,6 @@ protected:
     Lease6Collection
     Lease6Collection
     renewLeases6(ClientContext6& ctx);
     renewLeases6(ClientContext6& ctx);
 
 
-
-
     /// @brief returns allocator for a given pool type
     /// @brief returns allocator for a given pool type
     /// @param type type of pool (V4, IA, TA or PD)
     /// @param type type of pool (V4, IA, TA or PD)
     /// @throw BadValue if allocator for a given type is missing
     /// @throw BadValue if allocator for a given type is missing
@@ -743,30 +741,29 @@ private:
     /// into the database. That may fail in some cases, e.g. when there is another
     /// into the database. That may fail in some cases, e.g. when there is another
     /// allocation process and we lost a race to a specific lease.
     /// allocation process and we lost a race to a specific lease.
     ///
     ///
-    /// @param subnet Subnet the lease is allocated from
-    /// @param clientid Client identifier
-    /// @param hwaddr Client's hardware address
     /// @param addr An address that was selected and is confirmed to be available
     /// @param addr An address that was selected and is confirmed to be available
-    /// @param fwd_dns_update Indicates whether forward DNS update will be
-    ///        performed for the client (true) or not (false).
-    /// @param rev_dns_update Indicates whether reverse DNS update will be
-    ///        performed for the client (true) or not (false).
-    /// @param hostname A string carrying hostname to be used for DNS updates.
-    /// @param callout_handle a callout handle (used in hooks). A lease callouts
-    ///        will be executed if this parameter is passed (and there are callouts
-    ///        registered)
-    /// @param fake_allocation Is this real i.e. REQUEST (false) or just picking
-    ///        an address for DISCOVER that is not really allocated (true)
+    /// @param ctx client context that contains additional parameters.
+    ///
+    /// In particular, the following fields from Client context are used:
+    /// @ref ClientContext4::subnet_ Subnet the lease is allocated from
+    /// @ref ClientContext4::clientid_ Client identifier
+    /// @ref ClientContext4::hwaddr_ Client's hardware address
+    /// @ref ClientContext4::fwd_dns_update_ Indicates whether forward DNS update
+    ///        will be performed for the client (true) or not (false).
+    /// @ref ClientContext4::rev_dns_update_ Indicates whether reverse DNS update
+    ///        will be performed for the client (true) or not (false).
+    /// @ref ClientContext4::hostname_ A string carrying hostname to be used for
+    ///        DNS updates.
+    /// @ref ClientContext4::callout_handle_ a callout handle (used in hooks).
+    ///        A lease callouts will be executed if this parameter is passed
+    ///        (and there are callouts registered)
+    /// @ref ClientContext4::fake_allocation_ Is this real i.e. REQUEST (false)
+    ///        or just picking an address for DISCOVER that is not really
+    ///        allocated (true)
     /// @return allocated lease (or NULL in the unlikely case of the lease just
     /// @return allocated lease (or NULL in the unlikely case of the lease just
     ///        becomed unavailable)
     ///        becomed unavailable)
-    Lease4Ptr createLease4(const SubnetPtr& subnet, const DuidPtr& clientid,
-                           const HWAddrPtr& hwaddr,
-                           const isc::asiolink::IOAddress& addr,
-                           const bool fwd_dns_update,
-                           const bool rev_dns_update,
-                           const std::string& hostname,
-                           const isc::hooks::CalloutHandlePtr& callout_handle,
-                           bool fake_allocation = false);
+    Lease4Ptr createLease4(const ClientContext4& ctx,
+                           const isc::asiolink::IOAddress& addr);
 
 
     /// @brief Updates the specified lease with the information from a context.
     /// @brief Updates the specified lease with the information from a context.
     ///
     ///