lease_mgr.cc 7.61 KB
Newer Older
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/lease_mgr.h>
16
#include <exceptions/exceptions.h>
17

18 19
#include <boost/foreach.hpp>
#include <boost/algorithm/string.hpp>
20 21

#include <algorithm>
22
#include <iostream>
23
#include <iterator>
24 25
#include <map>
#include <sstream>
26 27
#include <string>

28
#include <time.h>
29 30 31

using namespace std;

32 33
namespace isc {
namespace dhcp {
34

35
Lease::Lease(const isc::asiolink::IOAddress& addr, uint32_t t1, uint32_t t2,
36 37 38
             uint32_t valid_lft, SubnetID subnet_id, time_t cltt,
             const bool fqdn_fwd, const bool fqdn_rev,
             const std::string& hostname)
39
    :addr_(addr), t1_(t1), t2_(t2), valid_lft_(valid_lft), cltt_(cltt),
40 41
     subnet_id_(subnet_id), fixed_(false), hostname_(hostname),
     fqdn_fwd_(fqdn_fwd), fqdn_rev_(fqdn_rev) {
42 43
}

44 45
Lease4::Lease4(const Lease4& other)
    : Lease(other.addr_, other.t1_, other.t2_, other.valid_lft_,
46 47
            other.subnet_id_, other.cltt_, other.fqdn_fwd_,
            other.fqdn_rev_, other.hostname_), ext_(other.ext_),
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
      hwaddr_(other.hwaddr_) {

    fixed_ = other.fixed_;
    comments_ = other.comments_;

    if (other.client_id_) {
        client_id_.reset(new ClientId(other.client_id_->getClientId()));

    } else {
        client_id_.reset();

    }
}

Lease4&
Lease4::operator=(const Lease4& other) {
    if (this != &other) {
        addr_ = other.addr_;
        t1_ = other.t1_;
        t2_ = other.t2_;
        valid_lft_ = other.valid_lft_;
        cltt_ = other.cltt_;
        subnet_id_ = other.subnet_id_;
        fixed_ = other.fixed_;
        hostname_ = other.hostname_;
        fqdn_fwd_ = other.fqdn_fwd_;
        fqdn_rev_ = other.fqdn_rev_;
        comments_ = other.comments_;
        ext_ = other.ext_;
        hwaddr_ = other.hwaddr_;

        if (other.client_id_) {
            client_id_.reset(new ClientId(other.client_id_->getClientId()));
        } else {
            client_id_.reset();
        }
    }
    return (*this);
}

88 89 90
Lease6::Lease6(LeaseType type, const isc::asiolink::IOAddress& addr,
               DuidPtr duid, uint32_t iaid, uint32_t preferred, uint32_t valid,
               uint32_t t1, uint32_t t2, SubnetID subnet_id, uint8_t prefixlen)
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    : Lease(addr, t1, t2, valid, subnet_id, 0/*cltt*/, false, false, ""),
      type_(type), prefixlen_(prefixlen), iaid_(iaid), duid_(duid),
      preferred_lft_(preferred) {
    if (!duid) {
        isc_throw(InvalidOperation, "DUID must be specified for a lease");
    }

    cltt_ = time(NULL);
}

Lease6::Lease6(LeaseType type, const isc::asiolink::IOAddress& addr,
               DuidPtr duid, uint32_t iaid, uint32_t preferred, uint32_t valid,
               uint32_t t1, uint32_t t2, SubnetID subnet_id,
               const bool fqdn_fwd, const bool fqdn_rev,
               const std::string& hostname, uint8_t prefixlen)
    : Lease(addr, t1, t2, valid, subnet_id, 0/*cltt*/,
            fqdn_fwd, fqdn_rev, hostname),
108 109
      type_(type), prefixlen_(prefixlen), iaid_(iaid), duid_(duid),
      preferred_lft_(preferred) {
Tomek Mrugalski's avatar
Tomek Mrugalski committed
110
    if (!duid) {
111 112
        isc_throw(InvalidOperation, "DUID must be specified for a lease");
    }
113

114
    cltt_ = time(NULL);
115 116
}

117
bool Lease::expired() const {
118 119 120 121 122 123 124

    // Let's use int64 to avoid problems with negative/large uint32 values
    int64_t expire_time = cltt_ + valid_lft_;
    return (expire_time < time(NULL));
}


Tomek Mrugalski's avatar
Tomek Mrugalski committed
125
std::string LeaseMgr::getParameter(const std::string& name) const {
126
    ParameterMap::const_iterator param = parameters_.find(name);
Tomek Mrugalski's avatar
Tomek Mrugalski committed
127 128 129 130
    if (param == parameters_.end()) {
        isc_throw(BadValue, "Parameter not found");
    }
    return (param->second);
131 132
}

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
Lease6Ptr
LeaseMgr::getLease6(Lease6::LeaseType type, const DUID& duid,
                    uint32_t iaid, SubnetID subnet_id) const {
    Lease6Collection col = getLeases6(type, duid, iaid, subnet_id);

    if (col.size() > 1) {
        isc_throw(MultipleRecords, "More than one lease found for type "
                  << static_cast<int>(type) << ", duid "
                  << duid.toText() << ", iaid " << iaid
                  << " and subnet-id " << subnet_id);
    }
    if (col.empty()) {
        return (Lease6Ptr());
    }
    return (*col.begin());
}

150 151
std::string
Lease6::toText() const {
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    ostringstream stream;

    stream << "Type:          " << static_cast<int>(type_) << " (";
    switch (type_) {
        case Lease6::LEASE_IA_NA:
            stream << "IA_NA)\n";
            break;
        case Lease6::LEASE_IA_TA:
            stream << "IA_TA)\n";
            break;
        case Lease6::LEASE_IA_PD:
            stream << "IA_PD)\n";
            break;
        default:
            stream << "unknown)\n";
    }
    stream << "Address:       " << addr_.toText() << "\n"
           << "Prefix length: " << static_cast<int>(prefixlen_) << "\n"
           << "IAID:          " << iaid_ << "\n"
           << "Pref life:     " << preferred_lft_ << "\n"
           << "Valid life:    " << valid_lft_ << "\n"
           << "Cltt:          " << cltt_ << "\n"
           << "Subnet ID:     " << subnet_id_ << "\n";

    return (stream.str());
}

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
std::string
Lease4::toText() const {
    ostringstream stream;

    stream << "Address:       " << addr_.toText() << "\n"
           << "Valid life:    " << valid_lft_ << "\n"
           << "T1:            " << t1_ << "\n"
           << "T2:            " << t2_ << "\n"
           << "Cltt:          " << cltt_ << "\n"
           << "Subnet ID:     " << subnet_id_ << "\n";

    return (stream.str());
}


194 195
bool
Lease4::operator==(const Lease4& other) const {
196 197 198 199 200 201 202 203 204 205 206 207
    if ( (client_id_ && !other.client_id_) ||
         (!client_id_ && other.client_id_) ) {
        // One lease has client-id, but the other doesn't
        return false;
    }

    if (client_id_ && other.client_id_ &&
        *client_id_ != *other.client_id_) {
        // Different client-ids
        return false;
    }

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
    return (
        addr_ == other.addr_ &&
        ext_ == other.ext_ &&
        hwaddr_ == other.hwaddr_ &&
        t1_ == other.t1_ &&
        t2_ == other.t2_ &&
        valid_lft_ == other.valid_lft_ &&
        cltt_ == other.cltt_ &&
        subnet_id_ == other.subnet_id_ &&
        fixed_ == other.fixed_ &&
        hostname_ == other.hostname_ &&
        fqdn_fwd_ == other.fqdn_fwd_ &&
        fqdn_rev_ == other.fqdn_rev_ &&
        comments_ == other.comments_
    );
}

225 226 227 228
bool
Lease6::operator==(const Lease6& other) const {
    return (
        addr_ == other.addr_ &&
229
        type_ == other.type_ &&
230 231 232 233 234
        prefixlen_ == other.prefixlen_ &&
        iaid_ == other.iaid_ &&
        *duid_ == *other.duid_ &&
        preferred_lft_ == other.preferred_lft_ &&
        valid_lft_ == other.valid_lft_ &&
235 236
        t1_ == other.t1_ &&
        t2_ == other.t2_ &&
237
        cltt_ == other.cltt_ &&
238 239 240 241 242 243 244
        subnet_id_ == other.subnet_id_ &&
        fixed_ == other.fixed_ &&
        hostname_ == other.hostname_ &&
        fqdn_fwd_ == other.fqdn_fwd_ &&
        fqdn_rev_ == other.fqdn_rev_ &&
        comments_ == other.comments_
    );
245
}
246 247 248

} // namespace isc::dhcp
} // namespace isc