memfile_lease_mgr.cc 6.77 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 62 63 64
    Lease4Storage::iterator l = storage4_.find(addr);
    if (l == storage4_.end()) {
        return (Lease4Ptr());
    } else {
        return (*l);
    }
65 66
}

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

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

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

80 81
    Lease4Storage::iterator l;
    for (l = storage4_.begin(); l != storage4_.end(); ++l) {
82 83
        if ( ((*l)->hwaddr_ == hwaddr.hwaddr_) &&
             ((*l)->subnet_id_ == subnet_id)) {
84 85 86 87 88
            return (*l);
        }
    }

    // not found
89 90 91
    return (Lease4Ptr());
}

92
Lease4Collection Memfile_LeaseMgr::getLease4(const ClientId& clientid) const {
93 94
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
              DHCPSRV_MEMFILE_GET_CLIENTID).arg(clientid.toText());
95
    isc_throw(NotImplemented, "getLease4(ClientId) not implemented");
96
}
97

98
Lease4Ptr Memfile_LeaseMgr::getLease4(const ClientId& client_id,
99
                                      SubnetID subnet_id) const {
100 101
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
              DHCPSRV_MEMFILE_GET_SUBID_CLIENTID).arg(subnet_id)
102
              .arg(client_id.toText());
103 104 105 106 107 108 109 110 111
    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
112 113 114
    return (Lease4Ptr());
}

115 116
Lease6Ptr Memfile_LeaseMgr::getLease6(
        const isc::asiolink::IOAddress& addr) const {
117 118
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
              DHCPSRV_MEMFILE_GET_ADDR6).arg(addr.toText());
119 120 121 122 123 124 125 126 127

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

128 129
Lease6Collection Memfile_LeaseMgr::getLease6(const DUID& duid,
                                             uint32_t iaid) const {
130 131
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
              DHCPSRV_MEMFILE_GET_IAID_DUID).arg(iaid).arg(duid.toText());
132

133 134 135
    return (Lease6Collection());
}

136 137
Lease6Ptr Memfile_LeaseMgr::getLease6(const DUID& duid, uint32_t iaid,
                                      SubnetID subnet_id) const {
138
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
139 140 141
              DHCPSRV_MEMFILE_GET_IAID_SUBID_DUID)
              .arg(iaid).arg(subnet_id).arg(duid.toText());

142 143 144 145 146 147 148 149
    /// @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);
        }
    }
150 151 152
    return (Lease6Ptr());
}

153
void Memfile_LeaseMgr::updateLease4(const Lease4Ptr& lease) {
154 155
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
              DHCPSRV_MEMFILE_UPDATE_ADDR4).arg(lease->addr_.toText());
156

157 158
}

159
void Memfile_LeaseMgr::updateLease6(const Lease6Ptr& lease) {
160 161
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
              DHCPSRV_MEMFILE_UPDATE_ADDR6).arg(lease->addr_.toText());
162

163 164 165

}

166
bool Memfile_LeaseMgr::deleteLease(const isc::asiolink::IOAddress& addr) {
167 168
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
              DHCPSRV_MEMFILE_DELETE_ADDR).arg(addr.toText());
169
    if (addr.isV4()) {
170 171 172 173 174 175 176 177 178
        // v4 lease
        Lease4Storage::iterator l = storage4_.find(addr);
        if (l == storage4_.end()) {
            // No such lease
            return (false);
        } else {
            storage4_.erase(l);
            return (true);
        }
179

180
    } else {
181
        // v6 lease
182 183 184 185 186 187 188 189
        Lease6Storage::iterator l = storage6_.find(addr);
        if (l == storage6_.end()) {
            // No such lease
            return (false);
        } else {
            storage6_.erase(l);
            return (true);
        }
190 191 192 193 194 195 196 197
    }
}

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."));
}
198 199 200

void
Memfile_LeaseMgr::commit() {
201
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL, DHCPSRV_MEMFILE_COMMIT);
202 203 204 205
}

void
Memfile_LeaseMgr::rollback() {
206 207
    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
              DHCPSRV_MEMFILE_ROLLBACK);
208
}