lease_mgr.h 10.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 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 144 145 146 147 148 149 150 151 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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
// 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>
#include <boost/shared_ptr.hpp>
#include "benchmark.h"

/// @brief Holds DUID (DHCPv6 Unique Identifier)
///
/// This class holds DUID, that is used in client-id, server-id and
/// several other options. It is used to identify DHCPv6 entity.
class DUID {

    /// @brief specifies DUID type
    typedef enum {
        DUID_UNKNOWN = 0, // invalid/unknown type
        DUID_LLT = 1, // link-layer + time, see RFC3315, section 9.2
        DUID_EN = 2,  // enterprise-id, see RFC3315, section 9.3
        DUID_LL = 3,  // link-layer, see RFC3315, section 9.4
        DUID_UUID = 4, // UUID, see RFC6355
    } DUIDType;

 public:
    DUID(const std::vector<uint8_t>& duid);
    DUID(const char *duid, size_t len);
    const std::vector<uint8_t> getDuid() const;
    DUIDType getType();
    bool operator == (const DUID& other);
 protected:
    std::vector<uint8_t> duid_;
    
}

/// @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 {
    uint32_t addr_; /// IPv4 address

    /// @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.
    uint32_t ext_; /// address extension

    /// @brief hardware address
    std::vector<uint8_t> hwaddr;

    /// @brief client identifier
    std::vector<uint8_t> client_id; 

    /// valid lifetime timestamp
    uint32_t valid_lft;

    /// @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.
    uint32_t recycle_time; 

    /// @brief client last transmission time
    ///
    /// Specifies a timestamp, when last transmission from a client was received.
    time_t cltt;

    /// @brief Pool identifier
    ///
    /// Specifies pool-id of the pool that the lease belongs to
    uint32_t pool_id;

    /// @brief Is this a fixed lease?
    ///
    /// Fixed leases are kept after they are released/expired.
    bool fixed;

    /// @brief client hostname
    ///
    /// This field may be empty
    std::string hostname;

    /// @brief did we update AAAA record for this lease?
    bool fqdn_fwd;

    /// @brief did we update PTR record for this lease? 
    bool fqdn_rev;

    /// @brief additional options stored with this lease
    ///
    /// Currently not used.
    std::string options; 

    /// @brief Lease comments.
    ///
    /// Currently not used. It may be used for keeping comments made by the
    /// system administrator.
    std::string comments; 
};

/// @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).
    std::vector<uint8_t> hwaddr;

    /// @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.
    uint32_t preferred_lft;

    /// @brief valid lifetime
    uint32_t valid_lft;

    /// @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
    /// for the same IA, each must have consistent T1 and T2 values.
    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
    /// for the same IA, each must have consistent T1 and T2 values.
    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.
    uint32_t recycle_time; 

    /// @brief client last transmission time
    ///
    /// Specifies a timestamp, when last transmission from a client was received.
    time_t cltt;

    /// @brief Pool identifier
    ///
    /// Specifies pool-id of the pool that the lease belongs to
    uint32_t pool_id;

    /// @brief Is this a fixed lease?
    ///
    /// Fixed leases are kept after they are released/expired.
    bool fixed;

    /// @brief client hostname
    ///
    /// This field may be empty
    std::string hostname;

    /// @brief did we update AAAA record for this lease?
    bool fqdn_fwd;

    /// @brief did we update PTR record for this lease? 
    bool fqdn_rev;

    /// @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.
    /// @todo: Define this as a container of options
    std::string options; 

    /// @brief /// comments on that lease
    ///
    /// (currently not used)
    std::string comments; 
};

/// @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;


/// @brief In-memory + lease file database implementation
///
/// This is a simplified in-memory database that mimics ISC DHCP4 implementation.
/// It uses STL and boost: std::map for storage, boost::shared ptr for memory
/// management. It does use C file operations (fopen, fwrite, etc.), because
/// C++ streams does not offer any easy way to flush their contents, like
/// fflush() and fsync() does.
///
/// IPv4 address is used as a key in the hash.
class LeaseMgr {
public:

    /// @brief The sole lease manager constructor
    ///
    /// @param filename name of the lease file (will be overwritten)
    /// @param sync should operations be
    LeaseMgr(const std::string& filename, bool sync);

    /// @brief Destructor (closes file)
    ~LeaseMgr();

    /// @brief adds an IPv4 lease
    ///
    /// @param lease lease to be added
    bool addLease(Lease4Ptr lease);

    /// @brief adds a IPv6 lease
    ///
    /// @param lease lease to be added
    bool addLease(Lease6Ptr lease);

    /// @brief returns existing IPv4 lease
    ///
    /// @param addr address of the searched lease
    ///
    /// @return smart pointer to the lease (or NULL if lease is not found)
    Lease4Ptr getLease4(isc::asiolink::IOAddress addr);

#error "Implement this"
    Lease4Ptr getLease4(hwaddress );

    Lease4Ptr getLease4(client-id);

    /// @brief returns existing IPv4 lease
    ///
    /// @param addr address of the searched lease
    ///
    /// @return smart pointer to the lease (or NULL if lease is not found)
    Lease6Ptr getLease6(isc::asiolink::IOAddress addr);

#error "Implement this"
    Lease6Ptr getLease6(DUID);

    Lease6Ptr getLease6(DUID, iaid);


#error "Implement this"
    bool updateLease4(Lease4Ptr lease4);

    bool updateLease6(Lease6Ptr lease6);

    /// @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
    bool deleteLease4(uint32_t addr);

    /// @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
    bool deleteLease6(isc::asiolink::IOAddress addr);

protected:

};




};