123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- #include <dhcpsrv/alloc_engine.h>
- #include <dhcpsrv/lease_mgr_factory.h>
- #include <cstring>
- #include <string.h>
- using namespace isc::asiolink;
- namespace isc {
- namespace dhcp {
- AllocEngine::IterativeAllocator::IterativeAllocator()
- :Allocator() {
- }
- isc::asiolink::IOAddress
- AllocEngine::IterativeAllocator::increaseAddress(const isc::asiolink::IOAddress& addr) {
-
- const std::vector<uint8_t>& vec = addr.toBytes();
-
- const int len = vec.size();
-
-
-
- BOOST_STATIC_ASSERT(V4ADDRESS_LEN <= V6ADDRESS_LEN);
- uint8_t packed[V6ADDRESS_LEN];
-
- std::memcpy(packed, &vec[0], len);
-
- for (int i = len - 1; i >= 0; --i) {
- ++packed[i];
-
- if (packed[i] != 0) {
- break;
- }
- }
- return (IOAddress::fromBytes(addr.getFamily(), packed));
- }
- isc::asiolink::IOAddress
- AllocEngine::IterativeAllocator::pickAddress(const Subnet6Ptr& subnet,
- const DuidPtr&,
- const IOAddress&) {
-
-
-
- IOAddress last = subnet->getLastAllocated();
- const Pool6Collection& pools = subnet->getPools();
- if (pools.empty()) {
- isc_throw(AllocFailed, "No pools defined in selected subnet");
- }
-
- Pool6Collection::const_iterator it;
- for (it = pools.begin(); it != pools.end(); ++it) {
- if ((*it)->inRange(last)) {
- break;
- }
- }
-
-
-
-
- if (it == pools.end()) {
-
- IOAddress next = pools[0]->getFirstAddress();
- subnet->setLastAllocated(next);
- return (next);
- }
-
- IOAddress next = increaseAddress(last);
- if ((*it)->inRange(next)) {
-
- subnet->setLastAllocated(next);
- return (next);
- }
-
- ++it;
- if (it == pools.end()) {
-
-
- next = pools[0]->getFirstAddress();
- subnet->setLastAllocated(next);
- return (next);
- }
-
- 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 Subnet6Ptr&,
- 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 Subnet6Ptr&,
- 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");
- }
- }
- Lease6Ptr
- AllocEngine::allocateAddress6(const Subnet6Ptr& subnet,
- const DuidPtr& duid,
- uint32_t iaid,
- const IOAddress& hint,
- bool fake_allocation ) {
-
-
- if (!allocator_) {
- isc_throw(InvalidOperation, "No allocator selected");
- }
-
- Lease6Ptr existing = LeaseMgrFactory::instance().getLease6(*duid, iaid, subnet->getID());
- if (existing) {
-
-
- return (existing);
- }
-
- if (subnet->inPool(hint)) {
- existing = LeaseMgrFactory::instance().getLease6(hint);
- if (!existing) {
-
-
-
- Lease6Ptr lease = createLease(subnet, duid, iaid, hint, fake_allocation);
-
-
-
- if (lease) {
- return (lease);
- }
- } else {
- if (existing->expired()) {
- return (reuseExpiredLease(existing, subnet, duid, iaid,
- fake_allocation));
- }
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- unsigned int i = attempts_;
- do {
- IOAddress candidate = allocator_->pickAddress(subnet, duid, hint);
-
-
- Lease6Ptr existing = LeaseMgrFactory::instance().getLease6(candidate);
- if (!existing) {
-
-
- Lease6Ptr lease = createLease(subnet, duid, iaid, candidate,
- fake_allocation);
- if (lease) {
- return (lease);
- }
-
-
-
- } else {
- if (existing->expired()) {
- return (reuseExpiredLease(existing, subnet, duid, iaid,
- fake_allocation));
- }
- }
-
-
- --i;
- } while ( i || !attempts_);
- isc_throw(AllocFailed, "Failed to allocate address after " << attempts_
- << " tries");
- }
- Lease6Ptr AllocEngine::reuseExpiredLease(Lease6Ptr& expired,
- const Subnet6Ptr& subnet,
- const DuidPtr& duid,
- uint32_t iaid,
- bool fake_allocation ) {
- if (!expired->expired()) {
- isc_throw(BadValue, "Attempt to recycle lease that is still valid");
- }
-
- 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_ = std::string("");
- expired->fqdn_fwd_ = false;
- expired->fqdn_rev_ = false;
-
-
- if (!fake_allocation) {
-
- LeaseMgrFactory::instance().updateLease6(expired);
- }
-
-
-
-
- return (expired);
- }
- Lease6Ptr AllocEngine::createLease(const Subnet6Ptr& subnet,
- const DuidPtr& duid,
- uint32_t iaid,
- const IOAddress& addr,
- bool fake_allocation ) {
- Lease6Ptr lease(new Lease6(Lease6::LEASE_IA_NA, addr, duid, iaid,
- subnet->getPreferred(), subnet->getValid(),
- subnet->getT1(), subnet->getT2(), subnet->getID()));
- if (!fake_allocation) {
-
- bool status = LeaseMgrFactory::instance().addLease(lease);
- if (status) {
- return (lease);
- } else {
-
-
-
-
- return (Lease6Ptr());
- }
- } else {
-
-
-
- Lease6Ptr existing = LeaseMgrFactory::instance().getLease6(addr);
- if (!existing) {
- return (lease);
- } else {
- return (Lease6Ptr());
- }
- }
- }
- AllocEngine::~AllocEngine() {
-
- }
- };
- };
|