123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925 |
- // Copyright (C) 2012-2013 Internet Systems Consortium, Inc. ("ISC")
- //
- // Permission to use, copy, modify, and/or distribute this software for any
- // purpose with or without fee is hereby granted, provided that the above
- // copyright notice and this permission notice appear in all copies.
- //
- // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
- // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
- // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
- // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
- // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
- // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
- // PERFORMANCE OF THIS SOFTWARE.
- #include <dhcpsrv/alloc_engine.h>
- #include <dhcpsrv/dhcpsrv_log.h>
- #include <dhcpsrv/lease_mgr_factory.h>
- #include <hooks/server_hooks.h>
- #include <hooks/hooks_manager.h>
- #include <cstring>
- #include <vector>
- #include <string.h>
- using namespace isc::asiolink;
- using namespace isc::hooks;
- namespace {
- /// Structure that holds registered hook indexes
- struct AllocEngineHooks {
- int hook_index_lease4_select_; ///< index for "lease4_receive" hook point
- int hook_index_lease4_renew_; ///< index for "lease4_renew" hook point
- int hook_index_lease6_select_; ///< index for "lease6_receive" hook point
- /// Constructor that registers hook points for AllocationEngine
- AllocEngineHooks() {
- hook_index_lease4_select_ = HooksManager::registerHook("lease4_select");
- hook_index_lease4_renew_ = HooksManager::registerHook("lease4_renew");
- hook_index_lease6_select_ = HooksManager::registerHook("lease6_select");
- }
- };
- // Declare a Hooks object. As this is outside any function or method, it
- // will be instantiated (and the constructor run) when the module is loaded.
- // As a result, the hook indexes will be defined before any method in this
- // module is called.
- AllocEngineHooks Hooks;
- }; // anonymous namespace
- namespace isc {
- namespace dhcp {
- AllocEngine::IterativeAllocator::IterativeAllocator()
- :Allocator() {
- }
- isc::asiolink::IOAddress
- AllocEngine::IterativeAllocator::increaseAddress(const isc::asiolink::IOAddress& addr) {
- // Get a buffer holding an address.
- const std::vector<uint8_t>& vec = addr.toBytes();
- // Get the address length.
- const int len = vec.size();
- // Since the same array will be used to hold the IPv4 and IPv6
- // address we have to make sure that the size of the array
- // we allocate will work for both types of address.
- BOOST_STATIC_ASSERT(V4ADDRESS_LEN <= V6ADDRESS_LEN);
- uint8_t packed[V6ADDRESS_LEN];
- // Copy the address. It can be either V4 or V6.
- std::memcpy(packed, &vec[0], len);
- // Start increasing the least significant byte
- for (int i = len - 1; i >= 0; --i) {
- ++packed[i];
- // if we haven't overflowed (0xff -> 0x0), than we are done
- if (packed[i] != 0) {
- break;
- }
- }
- return (IOAddress::fromBytes(addr.getFamily(), packed));
- }
- isc::asiolink::IOAddress
- AllocEngine::IterativeAllocator::pickAddress(const SubnetPtr& subnet,
- const DuidPtr&,
- const IOAddress&) {
- // Let's get the last allocated address. It is usually set correctly,
- // but there are times when it won't be (like after removing a pool or
- // perhaps restarting the server).
- IOAddress last = subnet->getLastAllocated();
- const PoolCollection& pools = subnet->getPools();
- if (pools.empty()) {
- isc_throw(AllocFailed, "No pools defined in selected subnet");
- }
- // first we need to find a pool the last address belongs to.
- PoolCollection::const_iterator it;
- for (it = pools.begin(); it != pools.end(); ++it) {
- if ((*it)->inRange(last)) {
- break;
- }
- }
- // last one was bogus for one of several reasons:
- // - we just booted up and that's the first address we're allocating
- // - a subnet was removed or other reconfiguration just completed
- // - perhaps allocation algorithm was changed
- if (it == pools.end()) {
- // ok to access first element directly. We checked that pools is non-empty
- IOAddress next = pools[0]->getFirstAddress();
- subnet->setLastAllocated(next);
- return (next);
- }
- // Ok, we have a pool that the last address belonged to, let's use it.
- IOAddress next = increaseAddress(last); // basically addr++
- if ((*it)->inRange(next)) {
- // the next one is in the pool as well, so we haven't hit pool boundary yet
- subnet->setLastAllocated(next);
- return (next);
- }
- // We hit pool boundary, let's try to jump to the next pool and try again
- ++it;
- if (it == pools.end()) {
- // Really out of luck today. That was the last pool. Let's rewind
- // to the beginning.
- next = pools[0]->getFirstAddress();
- subnet->setLastAllocated(next);
- return (next);
- }
- // there is a next pool, let's try first address from it
- next = (*it)->getFirstAddress();
- subnet->setLastAllocated(next);
- return (next);
- }
- AllocEngine::HashedAllocator::HashedAllocator()
- :Allocator() {
- isc_throw(NotImplemented, "Hashed allocator is not implemented");
- }
- isc::asiolink::IOAddress
- AllocEngine::HashedAllocator::pickAddress(const SubnetPtr&,
- const DuidPtr&,
- const IOAddress&) {
- isc_throw(NotImplemented, "Hashed allocator is not implemented");
- }
- AllocEngine::RandomAllocator::RandomAllocator()
- :Allocator() {
- isc_throw(NotImplemented, "Random allocator is not implemented");
- }
- isc::asiolink::IOAddress
- AllocEngine::RandomAllocator::pickAddress(const SubnetPtr&,
- const DuidPtr&,
- const IOAddress&) {
- isc_throw(NotImplemented, "Random allocator is not implemented");
- }
- AllocEngine::AllocEngine(AllocType engine_type, unsigned int attempts)
- :attempts_(attempts) {
- switch (engine_type) {
- case ALLOC_ITERATIVE:
- allocator_ = boost::shared_ptr<Allocator>(new IterativeAllocator());
- break;
- case ALLOC_HASHED:
- allocator_ = boost::shared_ptr<Allocator>(new HashedAllocator());
- break;
- case ALLOC_RANDOM:
- allocator_ = boost::shared_ptr<Allocator>(new RandomAllocator());
- break;
- default:
- isc_throw(BadValue, "Invalid/unsupported allocation algorithm");
- }
- // Register hook points
- hook_index_lease4_select_ = Hooks.hook_index_lease4_select_;
- hook_index_lease6_select_ = Hooks.hook_index_lease6_select_;
- }
- Lease6Collection
- AllocEngine::allocateAddress6(const Subnet6Ptr& subnet,
- const DuidPtr& duid,
- uint32_t iaid,
- const IOAddress& hint,
- const bool fwd_dns_update,
- const bool rev_dns_update,
- const std::string& hostname,
- bool fake_allocation,
- const isc::hooks::CalloutHandlePtr& callout_handle) {
- try {
- // That check is not necessary. We create allocator in AllocEngine
- // constructor
- if (!allocator_) {
- isc_throw(InvalidOperation, "No allocator selected");
- }
- if (!subnet) {
- isc_throw(InvalidOperation, "Subnet is required for allocation");
- }
- if (!duid) {
- isc_throw(InvalidOperation, "DUID is mandatory for allocation");
- }
- // check if there's existing lease for that subnet/duid/iaid combination.
- /// @todo: Make this generic (cover temp. addrs and prefixes)
- Lease6Collection existing = LeaseMgrFactory::instance().getLeases6(
- Lease6::LEASE_IA_NA, *duid, iaid, subnet->getID());
- if (!existing.empty()) {
- // we have at least one lease already. This is a returning client,
- // probably after his reboot.
- return (existing);
- }
- // check if the hint is in pool and is available
- if (subnet->inPool(hint)) {
- /// @todo: We support only one hint for now
- Lease6Ptr lease = LeaseMgrFactory::instance().getLease6(
- Lease6::LEASE_IA_NA, hint);
- if (!lease) {
- /// @todo: check if the hint is reserved once we have host support
- /// implemented
- // the hint is valid and not currently used, let's create a lease for it
- /// @todo: We support only one lease per ia for now
- lease = createLease6(subnet, duid, iaid, hint, fwd_dns_update,
- rev_dns_update, hostname, callout_handle,
- fake_allocation);
- // It can happen that the lease allocation failed (we could have lost
- // the race condition. That means that the hint is lo longer usable and
- // we need to continue the regular allocation path.
- if (lease) {
- /// @todo: We support only one lease per ia for now
- Lease6Collection collection;
- collection.push_back(lease);
- return (collection);
- }
- } else {
- if (lease->expired()) {
- /// We found a lease and it is expired, so we can reuse it
- /// @todo: We support only one lease per ia for now
- lease = reuseExpiredLease(lease, subnet, duid, iaid,
- fwd_dns_update, rev_dns_update,
- hostname, callout_handle,
- fake_allocation);
- Lease6Collection collection;
- collection.push_back(lease);
- return (collection);
- }
- }
- }
- // Hint is in the pool but is not available. Search the pool until first of
- // the following occurs:
- // - we find a free address
- // - we find an address for which the lease has expired
- // - we exhaust number of tries
- //
- // @todo: Current code does not handle pool exhaustion well. It will be
- // improved. Current problems:
- // 1. with attempts set to too large value (e.g. 1000) and a small pool (e.g.
- // 10 addresses), we will iterate over it 100 times before giving up
- // 2. attempts 0 mean unlimited (this is really UINT_MAX, not infinite)
- // 3. the whole concept of infinite attempts is just asking for infinite loop
- // We may consider some form or reference counting (this pool has X addresses
- // left), but this has one major problem. We exactly control allocation
- // moment, but we currently do not control expiration time at all
- unsigned int i = attempts_;
- do {
- IOAddress candidate = allocator_->pickAddress(subnet, duid, hint);
- /// @todo: check if the address is reserved once we have host support
- /// implemented
- Lease6Ptr existing = LeaseMgrFactory::instance().getLease6(
- Lease6::LEASE_IA_NA, candidate);
- if (!existing) {
- // there's no existing lease for selected candidate, so it is
- // free. Let's allocate it.
- Lease6Ptr lease = createLease6(subnet, duid, iaid, candidate,
- fwd_dns_update, rev_dns_update,
- hostname,
- callout_handle, fake_allocation);
- if (lease) {
- Lease6Collection collection;
- collection.push_back(lease);
- return (collection);
- }
- // Although the address was free just microseconds ago, it may have
- // been taken just now. If the lease insertion fails, we continue
- // allocation attempts.
- } else {
- if (existing->expired()) {
- existing = reuseExpiredLease(existing, subnet, duid, iaid,
- fwd_dns_update, rev_dns_update,
- hostname, callout_handle,
- fake_allocation);
- Lease6Collection collection;
- collection.push_back(existing);
- return (collection);
- }
- }
- // Continue trying allocation until we run out of attempts
- // (or attempts are set to 0, which means infinite)
- --i;
- } while ((i > 0) || !attempts_);
- // Unable to allocate an address, return an empty lease.
- LOG_WARN(dhcpsrv_logger, DHCPSRV_ADDRESS6_ALLOC_FAIL).arg(attempts_);
- } catch (const isc::Exception& e) {
- // Some other error, return an empty lease.
- LOG_ERROR(dhcpsrv_logger, DHCPSRV_ADDRESS6_ALLOC_ERROR).arg(e.what());
- }
- return (Lease6Collection());
- }
- Lease4Ptr
- AllocEngine::allocateAddress4(const SubnetPtr& subnet,
- const ClientIdPtr& clientid,
- const HWAddrPtr& hwaddr,
- const IOAddress& hint,
- const bool fwd_dns_update,
- const bool rev_dns_update,
- const std::string& hostname,
- bool fake_allocation,
- const isc::hooks::CalloutHandlePtr& callout_handle,
- Lease4Ptr& old_lease) {
- // The NULL pointer indicates that the old lease didn't exist. It may
- // be later set to non NULL value if existing lease is found in the
- // database.
- old_lease.reset();
- try {
- // Allocator is always created in AllocEngine constructor and there is
- // currently no other way to set it, so that check is not really necessary.
- if (!allocator_) {
- isc_throw(InvalidOperation, "No allocator selected");
- }
- if (!subnet) {
- isc_throw(InvalidOperation, "Can't allocate IPv4 address without subnet");
- }
- if (!hwaddr) {
- isc_throw(InvalidOperation, "HWAddr must be defined");
- }
- // Check if there's existing lease for that subnet/clientid/hwaddr combination.
- Lease4Ptr existing = LeaseMgrFactory::instance().getLease4(*hwaddr, subnet->getID());
- if (existing) {
- // Save the old lease, before renewal.
- old_lease.reset(new Lease4(*existing));
- // We have a lease already. This is a returning client, probably after
- // its reboot.
- existing = renewLease4(subnet, clientid, hwaddr,
- fwd_dns_update, rev_dns_update, hostname,
- existing, callout_handle, fake_allocation);
- if (existing) {
- return (existing);
- }
- // If renewal failed (e.g. the lease no longer matches current configuration)
- // let's continue the allocation process
- }
- if (clientid) {
- existing = LeaseMgrFactory::instance().getLease4(*clientid, subnet->getID());
- if (existing) {
- // Save the old lease before renewal.
- old_lease.reset(new Lease4(*existing));
- // we have a lease already. This is a returning client, probably after
- // its reboot.
- existing = renewLease4(subnet, clientid, hwaddr,
- fwd_dns_update, rev_dns_update,
- hostname, existing, callout_handle,
- fake_allocation);
- // @todo: produce a warning. We haven't found him using MAC address, but
- // we found him using client-id
- if (existing) {
- return (existing);
- }
- }
- }
- // check if the hint is in pool and is available
- if (subnet->inPool(hint)) {
- existing = LeaseMgrFactory::instance().getLease4(hint);
- if (!existing) {
- /// @todo: Check if the hint is reserved once we have host support
- /// implemented
- // The hint is valid and not currently used, let's create a lease for it
- Lease4Ptr lease = createLease4(subnet, clientid, hwaddr, hint,
- fwd_dns_update, rev_dns_update,
- hostname, callout_handle,
- fake_allocation);
- // It can happen that the lease allocation failed (we could have lost
- // the race condition. That means that the hint is lo longer usable and
- // we need to continue the regular allocation path.
- if (lease) {
- return (lease);
- }
- } else {
- if (existing->expired()) {
- // Save the old lease, before reusing it.
- old_lease.reset(new Lease4(*existing));
- return (reuseExpiredLease(existing, subnet, clientid, hwaddr,
- fwd_dns_update, rev_dns_update,
- hostname, callout_handle,
- fake_allocation));
- }
- }
- }
- // Hint is in the pool but is not available. Search the pool until first of
- // the following occurs:
- // - we find a free address
- // - we find an address for which the lease has expired
- // - we exhaust the number of tries
- //
- // @todo: Current code does not handle pool exhaustion well. It will be
- // improved. Current problems:
- // 1. with attempts set to too large value (e.g. 1000) and a small pool (e.g.
- // 10 addresses), we will iterate over it 100 times before giving up
- // 2. attempts 0 mean unlimited (this is really UINT_MAX, not infinite)
- // 3. the whole concept of infinite attempts is just asking for infinite loop
- // We may consider some form or reference counting (this pool has X addresses
- // left), but this has one major problem. We exactly control allocation
- // moment, but we currently do not control expiration time at all
- unsigned int i = attempts_;
- do {
- IOAddress candidate = allocator_->pickAddress(subnet, clientid, hint);
- /// @todo: check if the address is reserved once we have host support
- /// implemented
- Lease4Ptr existing = LeaseMgrFactory::instance().getLease4(candidate);
- if (!existing) {
- // there's no existing lease for selected candidate, so it is
- // free. Let's allocate it.
- Lease4Ptr lease = createLease4(subnet, clientid, hwaddr,
- candidate, fwd_dns_update,
- rev_dns_update, hostname,
- callout_handle, fake_allocation);
- if (lease) {
- return (lease);
- }
- // Although the address was free just microseconds ago, it may have
- // been taken just now. If the lease insertion fails, we continue
- // allocation attempts.
- } else {
- if (existing->expired()) {
- // Save old lease before reusing it.
- old_lease.reset(new Lease4(*existing));
- return (reuseExpiredLease(existing, subnet, clientid, hwaddr,
- fwd_dns_update, rev_dns_update,
- hostname, callout_handle,
- fake_allocation));
- }
- }
- // Continue trying allocation until we run out of attempts
- // (or attempts are set to 0, which means infinite)
- --i;
- } while ((i > 0) || !attempts_);
- // Unable to allocate an address, return an empty lease.
- LOG_WARN(dhcpsrv_logger, DHCPSRV_ADDRESS4_ALLOC_FAIL).arg(attempts_);
- } catch (const isc::Exception& e) {
- // Some other error, return an empty lease.
- LOG_ERROR(dhcpsrv_logger, DHCPSRV_ADDRESS4_ALLOC_ERROR).arg(e.what());
- }
- return (Lease4Ptr());
- }
- Lease4Ptr AllocEngine::renewLease4(const SubnetPtr& subnet,
- const ClientIdPtr& clientid,
- const HWAddrPtr& hwaddr,
- const bool fwd_dns_update,
- const bool rev_dns_update,
- const std::string& hostname,
- const Lease4Ptr& lease,
- const isc::hooks::CalloutHandlePtr& callout_handle,
- bool fake_allocation /* = false */) {
- if (!lease) {
- isc_throw(InvalidOperation, "Lease4 must be specified");
- }
- // Let's keep the old data. This is essential if we are using memfile
- // (the lease returned points directly to the lease4 object in the database)
- // We'll need it if we want to skip update (i.e. roll back renewal)
- /// @todo: remove this once #3083 is implemented
- Lease4 old_values = *lease;
- lease->subnet_id_ = subnet->getID();
- lease->hwaddr_ = hwaddr->hwaddr_;
- lease->client_id_ = clientid;
- lease->cltt_ = time(NULL);
- lease->t1_ = subnet->getT1();
- lease->t2_ = subnet->getT2();
- lease->valid_lft_ = subnet->getValid();
- lease->fqdn_fwd_ = fwd_dns_update;
- lease->fqdn_rev_ = rev_dns_update;
- lease->hostname_ = hostname;
- bool skip = false;
- // Execute all callouts registered for packet6_send
- if (HooksManager::getHooksManager().calloutsPresent(Hooks.hook_index_lease4_renew_)) {
- // Delete all previous arguments
- callout_handle->deleteAllArguments();
- // Subnet from which we do the allocation. Convert the general subnet
- // pointer to a pointer to a Subnet4. Note that because we are using
- // boost smart pointers here, we need to do the cast using the boost
- // version of dynamic_pointer_cast.
- Subnet4Ptr subnet4 = boost::dynamic_pointer_cast<Subnet4>(subnet);
- // Pass the parameters
- callout_handle->setArgument("subnet4", subnet4);
- callout_handle->setArgument("clientid", clientid);
- callout_handle->setArgument("hwaddr", hwaddr);
- // Pass the lease to be updated
- callout_handle->setArgument("lease4", lease);
- // Call all installed callouts
- HooksManager::callCallouts(Hooks.hook_index_lease4_renew_, *callout_handle);
- // Callouts decided to skip the next processing step. The next
- // processing step would to actually renew the lease, so skip at this
- // stage means "keep the old lease as it is".
- if (callout_handle->getSkip()) {
- skip = true;
- LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_HOOKS, DHCPSRV_HOOK_LEASE4_RENEW_SKIP);
- }
- }
- if (!fake_allocation && !skip) {
- // for REQUEST we do update the lease
- LeaseMgrFactory::instance().updateLease4(lease);
- }
- if (skip) {
- // Rollback changes (really useful only for memfile)
- /// @todo: remove this once #3083 is implemented
- *lease = old_values;
- }
- return (lease);
- }
- Lease6Ptr AllocEngine::reuseExpiredLease(Lease6Ptr& expired,
- const Subnet6Ptr& subnet,
- const DuidPtr& duid,
- uint32_t iaid,
- 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 (!expired->expired()) {
- isc_throw(BadValue, "Attempt to recycle lease that is still valid");
- }
- // address, lease type and prefixlen (0) stay the same
- expired->iaid_ = iaid;
- expired->duid_ = duid;
- expired->preferred_lft_ = subnet->getPreferred();
- expired->valid_lft_ = subnet->getValid();
- expired->t1_ = subnet->getT1();
- expired->t2_ = subnet->getT2();
- expired->cltt_ = time(NULL);
- expired->subnet_id_ = subnet->getID();
- expired->fixed_ = false;
- expired->hostname_ = hostname;
- expired->fqdn_fwd_ = fwd_dns_update;
- expired->fqdn_rev_ = rev_dns_update;
- /// @todo: log here that the lease was reused (there's ticket #2524 for
- /// logging in libdhcpsrv)
- // Let's execute all callouts registered for lease6_select
- if (callout_handle &&
- HooksManager::getHooksManager().calloutsPresent(hook_index_lease6_select_)) {
- // Delete all previous arguments
- callout_handle->deleteAllArguments();
- // Pass necessary arguments
- // Subnet from which we do the allocation
- callout_handle->setArgument("subnet6", subnet);
- // Is this solicit (fake = true) or request (fake = false)
- callout_handle->setArgument("fake_allocation", fake_allocation);
- // The lease that will be assigned to a client
- callout_handle->setArgument("lease6", expired);
- // Call the callouts
- HooksManager::callCallouts(hook_index_lease6_select_, *callout_handle);
- // 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
- // won't be inserted into the database.
- if (callout_handle->getSkip()) {
- LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_HOOKS, DHCPSRV_HOOK_LEASE6_SELECT_SKIP);
- return (Lease6Ptr());
- }
- // Let's use whatever callout returned. Hopefully it is the same lease
- // we handled to it.
- callout_handle->getArgument("lease6", expired);
- }
- if (!fake_allocation) {
- // for REQUEST we do update the lease
- LeaseMgrFactory::instance().updateLease6(expired);
- }
- // We do nothing for SOLICIT. We'll just update database when
- // the client gets back to us with REQUEST message.
- // it's not really expired at this stage anymore - let's return it as
- // an updated lease
- return (expired);
- }
- Lease4Ptr AllocEngine::reuseExpiredLease(Lease4Ptr& expired,
- const SubnetPtr& subnet,
- const ClientIdPtr& clientid,
- const HWAddrPtr& hwaddr,
- 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 (!expired->expired()) {
- isc_throw(BadValue, "Attempt to recycle lease that is still valid");
- }
- // address, lease type and prefixlen (0) stay the same
- expired->client_id_ = clientid;
- expired->hwaddr_ = hwaddr->hwaddr_;
- expired->valid_lft_ = subnet->getValid();
- expired->t1_ = subnet->getT1();
- expired->t2_ = subnet->getT2();
- expired->cltt_ = time(NULL);
- expired->subnet_id_ = subnet->getID();
- expired->fixed_ = false;
- expired->hostname_ = hostname;
- expired->fqdn_fwd_ = fwd_dns_update;
- expired->fqdn_rev_ = rev_dns_update;
- /// @todo: log here that the lease was reused (there's ticket #2524 for
- /// logging in libdhcpsrv)
- // Let's execute all callouts registered for lease4_select
- if (callout_handle &&
- HooksManager::getHooksManager().calloutsPresent(hook_index_lease4_select_)) {
- // Delete all previous arguments
- callout_handle->deleteAllArguments();
- // Pass necessary arguments
- // Subnet from which we do the allocation. Convert the general subnet
- // pointer to a pointer to a Subnet4. Note that because we are using
- // boost smart pointers here, we need to do the cast using the boost
- // version of dynamic_pointer_cast.
- Subnet4Ptr subnet4 = boost::dynamic_pointer_cast<Subnet4>(subnet);
- callout_handle->setArgument("subnet4", subnet4);
- // Is this solicit (fake = true) or request (fake = false)
- callout_handle->setArgument("fake_allocation", fake_allocation);
- // The lease that will be assigned to a client
- callout_handle->setArgument("lease4", expired);
- // Call the callouts
- HooksManager::callCallouts(hook_index_lease6_select_, *callout_handle);
- // 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
- // won't be inserted into the database.
- if (callout_handle->getSkip()) {
- LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_HOOKS, DHCPSRV_HOOK_LEASE4_SELECT_SKIP);
- return (Lease4Ptr());
- }
- // Let's use whatever callout returned. Hopefully it is the same lease
- // we handled to it.
- callout_handle->getArgument("lease4", expired);
- }
- if (!fake_allocation) {
- // for REQUEST we do update the lease
- LeaseMgrFactory::instance().updateLease4(expired);
- }
- // We do nothing for SOLICIT. We'll just update database when
- // the client gets back to us with REQUEST message.
- // it's not really expired at this stage anymore - let's return it as
- // an updated lease
- return (expired);
- }
- Lease6Ptr AllocEngine::createLease6(const Subnet6Ptr& subnet,
- const DuidPtr& duid,
- uint32_t iaid,
- 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 */ ) {
- Lease6Ptr lease(new Lease6(Lease6::LEASE_IA_NA, addr, duid, iaid,
- subnet->getPreferred(), subnet->getValid(),
- subnet->getT1(), subnet->getT2(), subnet->getID()));
- lease->fqdn_fwd_ = fwd_dns_update;
- lease->fqdn_rev_ = rev_dns_update;
- lease->hostname_ = hostname;
- // Let's execute all callouts registered for lease6_select
- if (callout_handle &&
- HooksManager::getHooksManager().calloutsPresent(hook_index_lease6_select_)) {
- // Delete all previous arguments
- callout_handle->deleteAllArguments();
- // Pass necessary arguments
- // Subnet from which we do the allocation
- callout_handle->setArgument("subnet6", subnet);
- // Is this solicit (fake = true) or request (fake = false)
- callout_handle->setArgument("fake_allocation", fake_allocation);
- callout_handle->setArgument("lease6", lease);
- // This is the first callout, so no need to clear any arguments
- HooksManager::callCallouts(hook_index_lease6_select_, *callout_handle);
- // 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
- // won't be inserted into the database.
- if (callout_handle->getSkip()) {
- LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_HOOKS, DHCPSRV_HOOK_LEASE6_SELECT_SKIP);
- return (Lease6Ptr());
- }
- // Let's use whatever callout returned. Hopefully it is the same lease
- // we handled to it.
- callout_handle->getArgument("lease6", lease);
- }
- if (!fake_allocation) {
- // That is a real (REQUEST) allocation
- bool status = LeaseMgrFactory::instance().addLease(lease);
- if (status) {
- return (lease);
- } else {
- // One of many failures with LeaseMgr (e.g. lost connection to the
- // database, database failed etc.). One notable case for that
- // is that we are working in multi-process mode and we lost a race
- // (some other process got that address first)
- return (Lease6Ptr());
- }
- } else {
- // That is only fake (SOLICIT without rapid-commit) allocation
- // It is for advertise only. We should not insert the lease into LeaseMgr,
- // but rather check that we could have inserted it.
- Lease6Ptr existing = LeaseMgrFactory::instance().getLease6(
- Lease6::LEASE_IA_NA, addr);
- if (!existing) {
- return (lease);
- } else {
- return (Lease6Ptr());
- }
- }
- }
- 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) {
- isc_throw(BadValue, "Can't create a lease with NULL HW address");
- }
- time_t now = time(NULL);
- // @todo: remove this kludge after ticket #2590 is implemented
- std::vector<uint8_t> local_copy;
- if (clientid) {
- local_copy = clientid->getDuid();
- }
- Lease4Ptr lease(new Lease4(addr, &hwaddr->hwaddr_[0], hwaddr->hwaddr_.size(),
- &local_copy[0], local_copy.size(), subnet->getValid(),
- subnet->getT1(), subnet->getT2(), now,
- subnet->getID()));
- // Set FQDN specific lease parameters.
- lease->fqdn_fwd_ = fwd_dns_update;
- lease->fqdn_rev_ = rev_dns_update;
- lease->hostname_ = hostname;
- // Let's execute all callouts registered for lease4_select
- if (callout_handle &&
- HooksManager::getHooksManager().calloutsPresent(hook_index_lease4_select_)) {
- // Delete all previous arguments
- callout_handle->deleteAllArguments();
- // Pass necessary arguments
- // Subnet from which we do the allocation (That's as far as we can go
- // with using SubnetPtr to point to Subnet4 object. Users should not
- // be confused with dynamic_pointer_casts. They should get a concrete
- // pointer (Subnet4Ptr) pointing to a Subnet4 object.
- Subnet4Ptr subnet4 = boost::dynamic_pointer_cast<Subnet4>(subnet);
- callout_handle->setArgument("subnet4", subnet4);
- // Is this solicit (fake = true) or request (fake = false)
- callout_handle->setArgument("fake_allocation", fake_allocation);
- // Pass the intended lease as well
- callout_handle->setArgument("lease4", lease);
- // This is the first callout, so no need to clear any arguments
- HooksManager::callCallouts(hook_index_lease4_select_, *callout_handle);
- // 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
- // won't be inserted into the database.
- if (callout_handle->getSkip()) {
- LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_HOOKS, DHCPSRV_HOOK_LEASE4_SELECT_SKIP);
- return (Lease4Ptr());
- }
- // Let's use whatever callout returned. Hopefully it is the same lease
- // we handled to it.
- callout_handle->getArgument("lease4", lease);
- }
- if (!fake_allocation) {
- // That is a real (REQUEST) allocation
- bool status = LeaseMgrFactory::instance().addLease(lease);
- if (status) {
- return (lease);
- } else {
- // One of many failures with LeaseMgr (e.g. lost connection to the
- // database, database failed etc.). One notable case for that
- // is that we are working in multi-process mode and we lost a race
- // (some other process got that address first)
- return (Lease4Ptr());
- }
- } else {
- // That is only fake (DISCOVER) allocation
- // It is for OFFER only. We should not insert the lease into LeaseMgr,
- // but rather check that we could have inserted it.
- Lease4Ptr existing = LeaseMgrFactory::instance().getLease4(addr);
- if (!existing) {
- return (lease);
- } else {
- return (Lease4Ptr());
- }
- }
- }
- AllocEngine::~AllocEngine() {
- // no need to delete allocator. smart_ptr will do the trick for us
- }
- }; // end of isc::dhcp namespace
- }; // end of isc namespace
|