memfile_lease_mgr.cc 7.58 KB
Newer Older
Stephen Morris's avatar
Stephen Morris committed
1
// Copyright (C) 2012-2013 Internet Systems Consortium, Inc. ("ISC")
2 3 4 5 6 7 8 9 10 11 12 13 14
//
// 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.

15
#include <dhcpsrv/dhcpsrv_log.h>
16
#include <dhcpsrv/memfile_lease_mgr.h>
17
#include <exceptions/exceptions.h>
18

19
#include <iostream>
20 21 22

using namespace isc::dhcp;

23 24
Memfile_LeaseMgr::Memfile_LeaseMgr(const ParameterMap& parameters)
    : LeaseMgr(parameters) {
25
    LOG_WARN(dhcpsrv_logger, DHCPSRV_MEMFILE_WARNING);
26 27 28 29 30
}

Memfile_LeaseMgr::~Memfile_LeaseMgr() {
}

31
bool Memfile_LeaseMgr::addLease(const Lease4Ptr& lease) {
32 33
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
              DHCPSRV_MEMFILE_ADD_ADDR4).arg(lease->addr_.toText());
34

35 36 37 38 39 40
    if (getLease4(lease->addr_)) {
        // there is a lease with specified address already
        return (false);
    }
    storage4_.insert(lease);
    return (true);
41 42
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
43
bool Memfile_LeaseMgr::addLease(const Lease6Ptr& lease) {
44 45
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
              DHCPSRV_MEMFILE_ADD_ADDR6).arg(lease->addr_.toText());
46

47 48 49 50 51 52 53 54
    if (getLease6(lease->addr_)) {
        // there is a lease with specified address already
        return (false);
    }
    storage6_.insert(lease);
    return (true);
}

55
Lease4Ptr Memfile_LeaseMgr::getLease4(const isc::asiolink::IOAddress& addr) const {
56 57
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
              DHCPSRV_MEMFILE_GET_ADDR4).arg(addr.toText());
58

59 60 61
    typedef Lease4Storage::nth_index<0>::type SearchIndex;
    const SearchIndex& idx = storage4_.get<0>();
    Lease4Storage::iterator l = idx.find(addr);
62 63 64 65 66
    if (l == storage4_.end()) {
        return (Lease4Ptr());
    } else {
        return (*l);
    }
67 68
}

69
Lease4Collection Memfile_LeaseMgr::getLease4(const HWAddr& hwaddr) const {
70
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
71
              DHCPSRV_MEMFILE_GET_HWADDR).arg(hwaddr.toText());
72

73
    isc_throw(NotImplemented, "getLease4(HWaddr x) method not implemented yet");
74 75
}

76 77
Lease4Ptr Memfile_LeaseMgr::getLease4(const HWAddr& hwaddr,
                                      SubnetID subnet_id) const {
78 79
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
              DHCPSRV_MEMFILE_GET_SUBID_HWADDR).arg(subnet_id)
80 81
        .arg(hwaddr.toText());

82 83 84 85 86 87 88 89 90 91
    typedef Lease4Storage::nth_index<1>::type SearchIndex;
    const SearchIndex& idx = storage4_.get<1>();
    SearchIndex::const_iterator lease = idx.find(boost::make_tuple(hwaddr.hwaddr_,
                                                                   subnet_id));
    if (lease == idx.end()) {
        return Lease4Ptr();
    }

    return (*lease);

92 93
    Lease4Storage::iterator l;
    for (l = storage4_.begin(); l != storage4_.end(); ++l) {
94 95
        if ( ((*l)->hwaddr_ == hwaddr.hwaddr_) &&
             ((*l)->subnet_id_ == subnet_id)) {
96 97 98 99 100
            return (*l);
        }
    }

    // not found
101 102 103
    return (Lease4Ptr());
}

104
Lease4Collection Memfile_LeaseMgr::getLease4(const ClientId& clientid) const {
105 106
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
              DHCPSRV_MEMFILE_GET_CLIENTID).arg(clientid.toText());
107
    isc_throw(NotImplemented, "getLease4(ClientId) not implemented");
108
}
109

110
Lease4Ptr Memfile_LeaseMgr::getLease4(const ClientId& client_id,
111
                                      SubnetID subnet_id) const {
112 113
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
              DHCPSRV_MEMFILE_GET_SUBID_CLIENTID).arg(subnet_id)
114
              .arg(client_id.toText());
115 116 117 118 119 120 121 122 123 124 125

    typedef Lease4Storage::nth_index<2>::type SearchIndex;
    const SearchIndex& idx = storage4_.get<2>();
    SearchIndex::const_iterator lease = idx.find(boost::make_tuple(client_id.getClientId(),
                                                                   subnet_id));
    if (lease == idx.end()) {
        return Lease4Ptr();
    }

    return (*lease);

126 127 128 129 130 131 132 133 134
    Lease4Storage::iterator l;
    for (l = storage4_.begin(); l != storage4_.end(); ++l) {
        if ( (*(*l)->client_id_ == client_id) &&
             ((*l)->subnet_id_ == subnet_id)) {
            return (*l);
        }
    }

    // not found
135 136 137
    return (Lease4Ptr());
}

138 139
Lease6Ptr Memfile_LeaseMgr::getLease6(
        const isc::asiolink::IOAddress& addr) const {
140 141
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
              DHCPSRV_MEMFILE_GET_ADDR6).arg(addr.toText());
142 143 144 145 146 147 148 149 150

    Lease6Storage::iterator l = storage6_.find(addr);
    if (l == storage6_.end()) {
        return (Lease6Ptr());
    } else {
        return (*l);
    }
}

151 152
Lease6Collection Memfile_LeaseMgr::getLease6(const DUID& duid,
                                             uint32_t iaid) const {
153 154
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
              DHCPSRV_MEMFILE_GET_IAID_DUID).arg(iaid).arg(duid.toText());
155

156 157 158
    return (Lease6Collection());
}

159 160
Lease6Ptr Memfile_LeaseMgr::getLease6(const DUID& duid, uint32_t iaid,
                                      SubnetID subnet_id) const {
161
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
162 163 164
              DHCPSRV_MEMFILE_GET_IAID_SUBID_DUID)
              .arg(iaid).arg(subnet_id).arg(duid.toText());

165 166 167 168 169 170 171 172
    /// @todo: Slow, naive implementation. Write it using additional indexes
    for (Lease6Storage::iterator l = storage6_.begin(); l != storage6_.end(); ++l) {
        if ( (*((*l)->duid_) == duid) &&
             ( (*l)->iaid_ == iaid) &&
             ( (*l)->subnet_id_ == subnet_id)) {
            return (*l);
        }
    }
173 174 175
    return (Lease6Ptr());
}

176
void Memfile_LeaseMgr::updateLease4(const Lease4Ptr& lease) {
177 178
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
              DHCPSRV_MEMFILE_UPDATE_ADDR4).arg(lease->addr_.toText());
179

180 181
}

182
void Memfile_LeaseMgr::updateLease6(const Lease6Ptr& lease) {
183 184
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
              DHCPSRV_MEMFILE_UPDATE_ADDR6).arg(lease->addr_.toText());
185

186 187 188

}

189
bool Memfile_LeaseMgr::deleteLease(const isc::asiolink::IOAddress& addr) {
190 191
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
              DHCPSRV_MEMFILE_DELETE_ADDR).arg(addr.toText());
192
    if (addr.isV4()) {
193 194 195 196 197 198 199 200 201
        // v4 lease
        Lease4Storage::iterator l = storage4_.find(addr);
        if (l == storage4_.end()) {
            // No such lease
            return (false);
        } else {
            storage4_.erase(l);
            return (true);
        }
202

203
    } else {
204
        // v6 lease
205 206 207 208 209 210 211 212
        Lease6Storage::iterator l = storage6_.find(addr);
        if (l == storage6_.end()) {
            // No such lease
            return (false);
        } else {
            storage6_.erase(l);
            return (true);
        }
213 214 215 216 217 218 219 220
    }
}

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."));
}
221 222 223

void
Memfile_LeaseMgr::commit() {
224
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_MEMFILE_COMMIT);
225 226 227 228
}

void
Memfile_LeaseMgr::rollback() {
229 230
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
              DHCPSRV_MEMFILE_ROLLBACK);
231
}