123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- // Copyright (C) 2012-2014 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/cfgmgr.h>
- #include <dhcpsrv/dhcpsrv_log.h>
- #include <dhcpsrv/memfile_lease_mgr.h>
- #include <exceptions/exceptions.h>
- #include <iostream>
- using namespace isc::dhcp;
- Memfile_LeaseMgr::Memfile_LeaseMgr(const ParameterMap& parameters)
- : LeaseMgr(parameters) {
- // Get the lease files locations.
- lease_file4_ = initLeaseFilePath(V4);
- lease_file6_ = initLeaseFilePath(V6);
- }
- Memfile_LeaseMgr::~Memfile_LeaseMgr() {
- }
- bool
- Memfile_LeaseMgr::addLease(const Lease4Ptr& lease) {
- LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
- DHCPSRV_MEMFILE_ADD_ADDR4).arg(lease->addr_.toText());
- if (getLease4(lease->addr_)) {
- // there is a lease with specified address already
- return (false);
- }
- storage4_.insert(lease);
- return (true);
- }
- bool
- Memfile_LeaseMgr::addLease(const Lease6Ptr& lease) {
- LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
- DHCPSRV_MEMFILE_ADD_ADDR6).arg(lease->addr_.toText());
- if (getLease6(lease->type_, lease->addr_)) {
- // there is a lease with specified address already
- return (false);
- }
- storage6_.insert(lease);
- return (true);
- }
- Lease4Ptr
- Memfile_LeaseMgr::getLease4(const isc::asiolink::IOAddress& addr) const {
- LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
- DHCPSRV_MEMFILE_GET_ADDR4).arg(addr.toText());
- typedef Lease4Storage::nth_index<0>::type SearchIndex;
- const SearchIndex& idx = storage4_.get<0>();
- Lease4Storage::iterator l = idx.find(addr);
- if (l == storage4_.end()) {
- return (Lease4Ptr());
- } else {
- return (Lease4Ptr(new Lease4(**l)));
- }
- }
- Lease4Collection
- Memfile_LeaseMgr::getLease4(const HWAddr& hwaddr) const {
- LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
- DHCPSRV_MEMFILE_GET_HWADDR).arg(hwaddr.toText());
- typedef Lease4Storage::nth_index<0>::type SearchIndex;
- Lease4Collection collection;
- const SearchIndex& idx = storage4_.get<0>();
- for(SearchIndex::const_iterator lease = idx.begin();
- lease != idx.end(); ++lease) {
- // Every Lease4 has a hardware address, so we can compare it
- if ((*lease)->hwaddr_ == hwaddr.hwaddr_) {
- collection.push_back((*lease));
- }
- }
- return (collection);
- }
- Lease4Ptr
- Memfile_LeaseMgr::getLease4(const HWAddr& hwaddr, SubnetID subnet_id) const {
- LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
- DHCPSRV_MEMFILE_GET_SUBID_HWADDR).arg(subnet_id)
- .arg(hwaddr.toText());
- // We are going to use index #1 of the multi index container.
- // We define SearchIndex locally in this function because
- // currently only this function uses this index.
- typedef Lease4Storage::nth_index<1>::type SearchIndex;
- // Get the index.
- const SearchIndex& idx = storage4_.get<1>();
- // Try to find the lease using HWAddr and subnet id.
- SearchIndex::const_iterator lease =
- idx.find(boost::make_tuple(hwaddr.hwaddr_, subnet_id));
- // Lease was not found. Return empty pointer to the caller.
- if (lease == idx.end()) {
- return (Lease4Ptr());
- }
- // Lease was found. Return it to the caller.
- return (Lease4Ptr(new Lease4(**lease)));
- }
- Lease4Collection
- Memfile_LeaseMgr::getLease4(const ClientId& client_id) const {
- LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
- DHCPSRV_MEMFILE_GET_CLIENTID).arg(client_id.toText());
- typedef Memfile_LeaseMgr::Lease4Storage::nth_index<0>::type SearchIndex;
- Lease4Collection collection;
- const SearchIndex& idx = storage4_.get<0>();
- for(SearchIndex::const_iterator lease = idx.begin();
- lease != idx.end(); ++ lease) {
- // client-id is not mandatory in DHCPv4. There can be a lease that does
- // not have a client-id. Dereferencing null pointer would be a bad thing
- if((*lease)->client_id_ && *(*lease)->client_id_ == client_id) {
- collection.push_back((*lease));
- }
- }
- return (collection);
- }
- Lease4Ptr
- Memfile_LeaseMgr::getLease4(const ClientId& client_id,
- const HWAddr& hwaddr,
- SubnetID subnet_id) const {
- LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
- DHCPSRV_MEMFILE_GET_CLIENTID_HWADDR_SUBID).arg(client_id.toText())
- .arg(hwaddr.toText())
- .arg(subnet_id);
- // We are going to use index #3 of the multi index container.
- // We define SearchIndex locally in this function because
- // currently only this function uses this index.
- typedef Lease4Storage::nth_index<3>::type SearchIndex;
- // Get the index.
- const SearchIndex& idx = storage4_.get<3>();
- // Try to get the lease using client id, hardware address and subnet id.
- SearchIndex::const_iterator lease =
- idx.find(boost::make_tuple(client_id.getClientId(), hwaddr.hwaddr_,
- subnet_id));
- if (lease == idx.end()) {
- // Lease was not found. Return empty pointer to the caller.
- return (Lease4Ptr());
- }
- // Lease was found. Return it to the caller.
- return (*lease);
- }
- Lease4Ptr
- Memfile_LeaseMgr::getLease4(const ClientId& client_id,
- SubnetID subnet_id) const {
- LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
- DHCPSRV_MEMFILE_GET_SUBID_CLIENTID).arg(subnet_id)
- .arg(client_id.toText());
- // We are going to use index #2 of the multi index container.
- // We define SearchIndex locally in this function because
- // currently only this function uses this index.
- typedef Lease4Storage::nth_index<2>::type SearchIndex;
- // Get the index.
- const SearchIndex& idx = storage4_.get<2>();
- // Try to get the lease using client id and subnet id.
- SearchIndex::const_iterator lease =
- idx.find(boost::make_tuple(client_id.getClientId(), subnet_id));
- // Lease was not found. Return empty pointer to the caller.
- if (lease == idx.end()) {
- return (Lease4Ptr());
- }
- // Lease was found. Return it to the caller.
- return (Lease4Ptr(new Lease4(**lease)));
- }
- Lease6Ptr
- Memfile_LeaseMgr::getLease6(Lease::Type /* not used yet */,
- const isc::asiolink::IOAddress& addr) const {
- LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
- DHCPSRV_MEMFILE_GET_ADDR6).arg(addr.toText());
- Lease6Storage::iterator l = storage6_.find(addr);
- if (l == storage6_.end()) {
- return (Lease6Ptr());
- } else {
- return (Lease6Ptr(new Lease6(**l)));
- }
- }
- Lease6Collection
- Memfile_LeaseMgr::getLeases6(Lease::Type /* not used yet */,
- const DUID& duid, uint32_t iaid) const {
- LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
- DHCPSRV_MEMFILE_GET_IAID_DUID).arg(iaid).arg(duid.toText());
- /// @todo Not implemented.
- return (Lease6Collection());
- }
- Lease6Collection
- Memfile_LeaseMgr::getLeases6(Lease::Type /* not used yet */,
- const DUID& duid, uint32_t iaid,
- SubnetID subnet_id) const {
- LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
- DHCPSRV_MEMFILE_GET_IAID_SUBID_DUID)
- .arg(iaid).arg(subnet_id).arg(duid.toText());
- // We are going to use index #1 of the multi index container.
- // We define SearchIndex locally in this function because
- // currently only this function uses this index.
- typedef Lease6Storage::nth_index<1>::type SearchIndex;
- // Get the index.
- const SearchIndex& idx = storage6_.get<1>();
- // Try to get the lease using the DUID, IAID and Subnet ID.
- SearchIndex::const_iterator lease =
- idx.find(boost::make_tuple(duid.getDuid(), iaid, subnet_id));
- // Lease was not found. Return empty pointer.
- if (lease == idx.end()) {
- return (Lease6Collection());
- }
- // Lease was found, return it to the caller.
- /// @todo: allow multiple leases for a single duid+iaid+subnet_id tuple
- Lease6Collection collection;
- collection.push_back(Lease6Ptr(new Lease6(**lease)));
- return (collection);
- }
- void
- Memfile_LeaseMgr::updateLease4(const Lease4Ptr& lease) {
- LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
- DHCPSRV_MEMFILE_UPDATE_ADDR4).arg(lease->addr_.toText());
- Lease4Storage::iterator lease_it = storage4_.find(lease->addr_);
- if (lease_it == storage4_.end()) {
- isc_throw(NoSuchLease, "failed to update the lease with address "
- << lease->addr_ << " - no such lease");
- }
- **lease_it = *lease;
- }
- void
- Memfile_LeaseMgr::updateLease6(const Lease6Ptr& lease) {
- LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
- DHCPSRV_MEMFILE_UPDATE_ADDR6).arg(lease->addr_.toText());
- Lease6Storage::iterator lease_it = storage6_.find(lease->addr_);
- if (lease_it == storage6_.end()) {
- isc_throw(NoSuchLease, "failed to update the lease with address "
- << lease->addr_ << " - no such lease");
- }
- **lease_it = *lease;
- }
- bool
- Memfile_LeaseMgr::deleteLease(const isc::asiolink::IOAddress& addr) {
- LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
- DHCPSRV_MEMFILE_DELETE_ADDR).arg(addr.toText());
- if (addr.isV4()) {
- // v4 lease
- Lease4Storage::iterator l = storage4_.find(addr);
- if (l == storage4_.end()) {
- // No such lease
- return (false);
- } else {
- storage4_.erase(l);
- return (true);
- }
- } else {
- // v6 lease
- Lease6Storage::iterator l = storage6_.find(addr);
- if (l == storage6_.end()) {
- // No such lease
- return (false);
- } else {
- storage6_.erase(l);
- return (true);
- }
- }
- }
- std::string
- Memfile_LeaseMgr::getDescription() const {
- return (std::string("This is a dummy memfile backend implementation.\n"
- "It does not offer any useful lease management and its only\n"
- "purpose is to test abstract lease manager API."));
- }
- void
- Memfile_LeaseMgr::commit() {
- LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_MEMFILE_COMMIT);
- }
- void
- Memfile_LeaseMgr::rollback() {
- LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
- DHCPSRV_MEMFILE_ROLLBACK);
- }
- std::string
- Memfile_LeaseMgr::getDefaultLeaseFilePath(Universe u) const {
- std::ostringstream s;
- s << CfgMgr::instance().getDataDir() << "/kea-leases";
- s << (u == V4 ? "4" : "6");
- s << ".csv";
- return (s.str());
- }
- bool
- Memfile_LeaseMgr::persistLeases(Universe u) const {
- // Currently, if the lease file is empty, it means that writes to disk have
- // been explicitly disabled by the administrator. At some point, there may
- // be a dedicated ON/OFF flag implemented to control this.
- return (u == V4 ? !lease_file4_.empty() : !lease_file6_.empty());
- }
- std::string
- Memfile_LeaseMgr::initLeaseFilePath(Universe u) {
- std::string param_name = (u == V4 ? "leasefile4" : "leasefile6");
- std::string lease_file;
- try {
- lease_file = getParameter(param_name);
- } catch (const Exception& ex) {
- lease_file = getDefaultLeaseFilePath(u);
- }
- return (lease_file);
- }
|