lease_mgr.h 12.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Copyright (C) 2012 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 <string>
#include <fstream>
#include <vector>
18 19
#include <map>
#include <asiolink/io_address.h>
20
#include <boost/shared_ptr.hpp>
21 22
#include <dhcp/option.h>
#include <dhcp/duid.h>
23 24 25 26

namespace isc {
namespace dhcp {

27 28 29 30 31 32 33
/// @brief Structure that holds a lease for IPv4 address
///
/// For performance reasons it is a simple structure, not a class. If we chose
/// make it a class, all fields would have to made private and getters/setters
/// would be required. As this is a critical part of the code that will be used
/// extensively, direct access is warranted.
struct Lease4 {
Tomek Mrugalski's avatar
Tomek Mrugalski committed
34 35
    /// IPv4 address
    isc::asiolink::IOAddress addr_;
36 37 38 39 40 41 42 43

    /// @brief Address extension
    ///
    /// It is envisaged that in some cases IPv4 address will be accompanied with some
    /// additional data. One example of such use are Address + Port solutions (or
    /// Port-restricted Addresses), where several clients may get the same address, but
    /// different port ranges. This feature is not expected to be widely used.
    /// Under normal circumstances, the value should be 0.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
44
    uint32_t ext_;
45 46

    /// @brief hardware address
47
    std::vector<uint8_t> hwaddr_;
48 49

    /// @brief client identifier
Tomek Mrugalski's avatar
Tomek Mrugalski committed
50
    boost::shared_ptr<ClientId> client_id_;
51

52 53 54 55
    /// @brief valid lifetime
    ///
    /// Expressed as number of seconds since cltt
    uint32_t valid_lft_;
56 57 58 59 60 61 62

    /// @brief Recycle timer
    ///
    /// Typically, the lease return to free pool immediately after it is released
    /// or expired. This timer specifies number of seconds that it should be kept
    /// after release or expiration, in case the client returns. This feature is not
    /// currently used and this value is set to 0.
63
    uint32_t recycle_time_;
64 65 66 67

    /// @brief client last transmission time
    ///
    /// Specifies a timestamp, when last transmission from a client was received.
68
    time_t cltt_;
69 70 71 72

    /// @brief Pool identifier
    ///
    /// Specifies pool-id of the pool that the lease belongs to
73
    uint32_t pool_id_;
74 75 76 77

    /// @brief Is this a fixed lease?
    ///
    /// Fixed leases are kept after they are released/expired.
78
    bool fixed_;
79 80 81 82

    /// @brief client hostname
    ///
    /// This field may be empty
83
    std::string hostname_;
84 85

    /// @brief did we update AAAA record for this lease?
86
    bool fqdn_fwd_;
87

88 89
    /// @brief did we update PTR record for this lease?
    bool fqdn_rev_;
90 91 92

    /// @brief additional options stored with this lease
    ///
93 94 95
    /// This field is currently not used.
    /// @todo We need a way to store options in the databased.
    Option::OptionCollection options_;
96 97 98 99 100

    /// @brief Lease comments.
    ///
    /// Currently not used. It may be used for keeping comments made by the
    /// system administrator.
101
    std::string comments_;
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
};

/// @brief Pointer to a Lease4 structure.
typedef boost::shared_ptr<Lease4> Lease4Ptr;


/// @brief Structure that holds a lease for IPv6 address and/or prefix
///
/// For performance reasons it is a simple structure, not a class. Had we chose to
/// make it a class, all fields would have to be made private and getters/setters
/// would be required. As this is a critical part of the code that will be used
/// extensively, direct access rather than through getters/setters is warranted.
struct Lease6 {
    typedef enum {
        LEASE_IA_NA, /// the lease contains non-temporary IPv6 address
        LEASE_IA_TA, /// the lease contains temporary IPv6 address
        LEASE_IA_PD  /// the lease contains IPv6 prefix (for prefix delegation)
    } LeaseType;

    /// @brief specifies lease type (normal addr, temporary addr, prefix)
    LeaseType type_;

    /// IPv6 address
    isc::asiolink::IOAddress addr_;

    /// IPv6 prefix length (used only for PD)
    uint8_t prefixlen_;

    /// @brief IAID
    ///
    /// Identity Association IDentifier. DHCPv6 stores all addresses and prefixes
    /// in IA containers (IA_NA, IA_TA, IA_PD). Most containers may appear more
    /// than once in a message. To differentiate between them, IAID field is present
    uint32_t iaid_;

    /// @brief hardware address
    ///
    /// This field is not really used and is optional at best. The concept of identifying
    /// clients by their hardware address was replaced in DHCPv6 by DUID concept. Each
    /// client has its own unique DUID (DHCP Unique IDentifier). Furthermore, client's
    /// HW address is not always available, because client may be behind a relay (relay
    /// stores only link-local address).
Tomek Mrugalski's avatar
Tomek Mrugalski committed
144
    std::vector<uint8_t> hwaddr_;
145 146 147 148 149 150 151 152

    /// @brief client identifier
    boost::shared_ptr<DUID> duid_;

    /// @brief preferred lifetime
    ///
    /// This parameter specifies preferred lifetime since the lease was assigned/renewed
    /// (cltt), expressed in seconds.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
153
    uint32_t preferred_lft_;
154 155

    /// @brief valid lifetime
Tomek Mrugalski's avatar
Tomek Mrugalski committed
156 157 158 159
    ///
    /// This parameter specified valid lifetime since the lease was assigned/renewed
    /// (cltt), expressed in seconds.
    uint32_t valid_lft_;
160 161 162 163 164 165

    /// @brief T1 timer
    ///
    /// Specifies renewal time. Although technically it is a property of IA container,
    /// not the address itself, since our data model does not define separate IA
    /// entity, we are keeping it in the lease. In case of multiple addresses/prefixes
Tomek Mrugalski's avatar
Tomek Mrugalski committed
166 167
    /// for the same IA, each must have consistent T1 and T2 values. Specified in
    /// seconds since cltt.
168 169 170 171 172 173 174
    uint32_t t1_;

    /// @brief T2 timer
    ///
    /// Specifies rebinding time. Although technically it is a property of IA container,
    /// not the address itself, since our data model does not define separate IA
    /// entity, we are keeping it in the lease. In case of multiple addresses/prefixes
Tomek Mrugalski's avatar
Tomek Mrugalski committed
175 176
    /// for the same IA, each must have consistent T1 and T2 values. Specified in
    /// seconds since cltt.
177 178 179 180 181 182 183 184
    uint32_t t2_;

    /// @brief Recycle timer
    ///
    /// Typically, the lease return to free pool immediately after it is released
    /// or expired. This timer specifies number of seconds that it should be kept
    /// after release or expiration, in case the client returns. This feature is not
    /// currently used and this value is set to 0.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
185
    uint32_t recycle_time_;
186 187 188 189

    /// @brief client last transmission time
    ///
    /// Specifies a timestamp, when last transmission from a client was received.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
190
    time_t cltt_;
191 192 193 194

    /// @brief Pool identifier
    ///
    /// Specifies pool-id of the pool that the lease belongs to
Tomek Mrugalski's avatar
Tomek Mrugalski committed
195
    uint32_t pool_id_;
196 197 198 199

    /// @brief Is this a fixed lease?
    ///
    /// Fixed leases are kept after they are released/expired.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
200
    bool fixed_;
201 202 203 204

    /// @brief client hostname
    ///
    /// This field may be empty
Tomek Mrugalski's avatar
Tomek Mrugalski committed
205
    std::string hostname_;
206 207

    /// @brief did we update AAAA record for this lease?
Tomek Mrugalski's avatar
Tomek Mrugalski committed
208
    bool fqdn_fwd_;
209

210
    /// @brief did we update PTR record for this lease?
Tomek Mrugalski's avatar
Tomek Mrugalski committed
211
    bool fqdn_rev_;
212 213 214 215 216

    /// @brief additional options stored with this lease
    ///
    /// That field is currently not used. We may keep extra options assigned
    /// for leasequery and possibly other purposes.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
217 218
    /// @todo We need a way to store options in the databased.
    Option::OptionCollection options_;
219

Tomek Mrugalski's avatar
Tomek Mrugalski committed
220
    /// @brief Lease comments
221
    ///
Tomek Mrugalski's avatar
Tomek Mrugalski committed
222 223
    /// This field is currently not used.
    std::string comments_;
224 225 226 227 228 229 230 231 232
};

/// @brief Pointer to a Lease6 structure.
typedef boost::shared_ptr<Lease6> Lease6Ptr;

/// @brief Const pointer to a Lease6 structure.
typedef boost::shared_ptr<const Lease6> ConstLease6Ptr;


233
/// @brief Abstract Lease Manager
234
///
235 236 237 238
/// This is an abstract API for lease database backends. It provides unified
/// interface to all backends. As this is an abstract class, it should not
/// be used directly, but rather specialized derived class should be used
/// instead.
239 240 241
class LeaseMgr {
public:

242 243 244
    /// Client Hardware address
    typedef std::vector<uint8_t> HWAddr;

245 246
    /// @brief The sole lease manager constructor
    ///
247 248 249 250 251 252
    /// dbconfig is a generic way of passing parameters. Parameters
    /// are passed in the "name=value" format, separated by spaces.
    /// Values may be enclosed in double quotes, if needed.
    ///
    /// @param dbconfig database configuration
    LeaseMgr(const std::string& dbconfig);
253 254

    /// @brief Destructor (closes file)
255
    virtual ~LeaseMgr();
256

257
    /// @brief Adds an IPv4 lease.
258 259
    ///
    /// @param lease lease to be added
260
    virtual bool addLease(Lease4Ptr lease) = 0;
261

262
    /// @brief Adds an IPv6 lease.
263 264
    ///
    /// @param lease lease to be added
265
    virtual bool addLease(Lease6Ptr lease) = 0;
266

267
    /// @brief Returns existing IPv4 lease for specified IPv4 address.
268 269 270
    ///
    /// @param addr address of the searched lease
    ///
271 272
    /// @return smart pointer to the lease (or NULL if a lease is not found)
    virtual Lease4Ptr getLease4(isc::asiolink::IOAddress addr) const = 0;
273

274 275 276 277 278 279
    /// @brief Returns existing IPv4 lease for specified hardware address.
    ///
    /// @param hwaddr hardware address of the client
    ///
    /// @return smart pointer to the lease (or NULL if a lease is not found)
    virtual Lease4Ptr getLease4(const HWAddr& hwaddr) const = 0;
280

281 282 283 284
    /// @brief Returns existing IPv4 lease for specified client-id
    ///
    /// @param clientid client identifier
    virtual Lease4Ptr getLease4(const ClientId& clientid) const = 0;
285

286
    /// @brief Returns existing IPv6 lease for a given IPv6 address.
287 288 289
    ///
    /// @param addr address of the searched lease
    ///
290 291
    /// @return smart pointer to the lease (or NULL if a lease is not found)
    virtual Lease6Ptr getLease6(isc::asiolink::IOAddress addr) const = 0;
292

293 294 295 296 297 298 299
    /// @brief Returns existing IPv6 lease for a given DUID+IA combination
    ///
    /// @param duid client DUID
    /// @param iaid IA identifier
    ///
    /// @return smart pointer to the lease (or NULL if a lease is not found)
    virtual Lease6Ptr getLease6(const DUID& duid, uint32_t iaid) const = 0;
300

301 302 303 304 305 306
    /// @brief Updates IPv4 lease.
    ///
    /// @param lease4 The lease to be updated.
    ///
    /// If no such lease is present, an exception will be thrown.
    virtual void updateLease4(Lease4Ptr lease4) = 0;
307

308 309 310 311 312 313
    /// @brief Updates IPv4 lease.
    ///
    /// @param lease4 The lease to be updated.
    ///
    /// If no such lease is present, an exception will be thrown.
    virtual void updateLease6(Lease6Ptr lease6) = 0;
314 315 316 317 318 319

    /// @brief Deletes a lease.
    ///
    /// @param addr IPv4 address of the lease to be deleted.
    ///
    /// @return true if deletion was successful, false if no such lease exists
320
    virtual bool deleteLease4(uint32_t addr) = 0;
321 322 323 324 325 326

    /// @brief Deletes a lease.
    ///
    /// @param addr IPv4 address of the lease to be deleted.
    ///
    /// @return true if deletion was successful, false if no such lease exists
327
    virtual bool deleteLease6(isc::asiolink::IOAddress addr) = 0;
328

329 330 331 332
    /// @brief Returns backend name.
    ///
    /// Each backend have specific name, e.g. "mysql" or "sqlite".
    virtual std::string getName() const = 0;
333

334 335 336 337
    /// @brief Returns description of the backend.
    ///
    /// This description may be multiline text that describes the backend.
    virtual std::string getDescription() const = 0;
338

339
    /// @brief Returns backend version.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
340 341 342 343 344 345 346 347 348 349
    ///
    /// @todo: We will need to implement 3 version functions eventually:
    /// A. abstract API version
    /// B. backend version
    /// C. database version (stored in the database scheme)
    ///
    /// and then check that:
    /// B>=A and B=C (it is ok to have newer backend, as it should be backward
    /// compatible)
    /// Also if B>C, some database upgrade procedure may happen
350
    virtual std::string getVersion() const = 0;
351

352
    /// @todo: Add host management here
Tomek Mrugalski's avatar
Tomek Mrugalski committed
353 354 355
    /// As host reservation is outside of scope for 2012, support for hosts
    /// is currently postponed.

356
protected:
Tomek Mrugalski's avatar
Tomek Mrugalski committed
357 358
    /// @brief returns value of the parameter
    std::string getParameter(const std::string& name) const;
359

360
    /// @brief list of parameters passed in dbconfig
Tomek Mrugalski's avatar
Tomek Mrugalski committed
361 362 363 364
    ///
    /// That will be mostly used for storing database name, username,
    /// password and other parameters required for DB access. It is not
    /// intended to keep any DHCP-related parameters.
365
    std::map<std::string, std::string> parameters_;
366
};
367 368 369 370 371


}; // end of isc::dhcp namespace

}; // end of isc namespace