Kea 3.3.4
lease.h
Go to the documentation of this file.
1// Copyright (C) 2013-2026 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#ifndef LEASE_H
8#define LEASE_H
9
10#include <asiolink/io_address.h>
11#include <cc/cfg_to_element.h>
12#include <cc/user_context.h>
13#include <dhcp/duid.h>
14#include <dhcp/hwaddr.h>
15#include <dhcp/option.h>
16#include <dhcpsrv/subnet_id.h>
17#include <util/dhcp_space.h>
18
19#include <cstdint>
20
21namespace isc {
22namespace dhcp {
23
24struct Lease;
25
27typedef boost::shared_ptr<Lease> LeasePtr;
28
34
36 static const uint32_t INFINITY_LFT = 0xffffffff;
37
45 static std::string lifetimeToText(uint32_t lifetime);
46
48 enum Type : uint16_t {
49 TYPE_NA = 0,
50 TYPE_TA = 1,
51 TYPE_PD = 2,
52 TYPE_V4 = 3,
53 };
54
58 static std::string typeToText(Type type);
59
65 static Type textToType(const std::string& text);
66
68
69
71 static constexpr uint32_t STATE_DEFAULT = 0;
72
74 static constexpr uint32_t STATE_DECLINED = 1;
75
77 static constexpr uint32_t STATE_EXPIRED_RECLAIMED = 2;
78
80 static constexpr uint32_t STATE_RELEASED = 3;
81
83 static constexpr uint32_t STATE_REGISTERED = 4;
84
92 static std::string basicStatesToText(const uint32_t state);
93
112 uint32_t valid_lft, SubnetID subnet_id, time_t cltt,
113 const bool fqdn_fwd, const bool fqdn_rev,
114 const std::string& hostname,
115 const HWAddrPtr& hwaddr);
116
118 virtual ~Lease() {}
119
123 virtual Lease::Type getType() const = 0;
124
129
133 uint32_t valid_lft_;
134
139
146
151 time_t cltt_;
152
158
163
167 uint32_t pool_id_;
168
172 std::string hostname_;
173
178
183
188
198 uint32_t state_;
199
203 virtual std::string toText() const = 0;
204
207 bool expired() const;
208
213 bool stateExpiredReclaimed() const;
214
218 bool stateDeclined() const;
219
223 bool stateRegistered() const;
224
233 bool hasIdenticalFqdn(const Lease& other) const;
234
243 const std::vector<uint8_t>& getHWAddrVector() const;
244
249 int64_t getExpirationTime() const;
250
261 virtual void decline(uint32_t probation_period) = 0;
262
265
276 static void syncCurrentExpirationTime(const Lease& from, Lease& to);
277
286
297 bool updateUserContextISC(const std::string elem_name,
298 data::ConstElementPtr new_values);
299
304 data::ConstElementPtr getFromUserContextISC(const std::string elem_name) const;
305
312 bool deleteFromUserContextISC(const std::string elem_name);
313
318 std::string getDdnsReverseZone() const;
319
323 void setDdnsReverseZone(const std::string& zone);
324
325protected:
326
335 static void fromElementCommon(const LeasePtr& lease,
336 const data::ConstElementPtr& element);
337
338};
339
340struct Lease4;
341
343typedef boost::shared_ptr<Lease4> Lease4Ptr;
344
351struct Lease4 : public Lease {
352
358
371 Lease4(const isc::asiolink::IOAddress& addr, const HWAddrPtr& hwaddr,
372 const uint8_t* clientid, size_t clientid_len, uint32_t valid_lft,
373 time_t cltt, uint32_t subnet_id,
374 const bool fqdn_fwd = false, const bool fqdn_rev = false,
375 const std::string& hostname = "")
376 : Lease(addr, valid_lft, subnet_id, cltt, fqdn_fwd, fqdn_rev,
377 hostname, hwaddr) {
378 if (clientid_len) {
379 client_id_.reset(new ClientId(clientid, clientid_len));
380 }
381 }
382
394 Lease4(const isc::asiolink::IOAddress& address,
395 const HWAddrPtr& hw_address,
396 const ClientIdPtr& client_id,
397 const uint32_t valid_lifetime,
398 const time_t cltt,
399 const SubnetID subnet_id,
400 const bool fqdn_fwd = false,
401 const bool fqdn_rev = false,
402 const std::string& hostname = "");
403
404
408 Lease4();
409
416 virtual Lease::Type getType() const {
417 return (Lease::TYPE_V4);
418 }
419
428 static std::string statesToText(const uint32_t state);
429
439 const std::vector<uint8_t>& getClientIdVector() const;
440
501 bool belongsToClient(const HWAddrPtr& hw_address,
502 const ClientIdPtr& client_id) const;
503
507 bool operator==(const Lease4& other) const;
508
512 bool operator!=(const Lease4& other) const {
513 return (!operator==(other));
514 }
515
519 virtual std::string toText() const;
520
526 void decline(uint32_t probation_period);
527
529 virtual isc::data::ElementPtr toElement() const;
530
536 static Lease4Ptr fromElement(const data::ConstElementPtr& element);
537
539
541 std::vector<uint8_t> remote_id_;
542
544 std::vector<uint8_t> relay_id_;
545};
546
548typedef std::vector<Lease4Ptr> Lease4Collection;
549
551typedef boost::shared_ptr<Lease4Collection> Lease4CollectionPtr;
552
553struct Lease6;
554
556typedef boost::shared_ptr<Lease6> Lease6Ptr;
557
564struct Lease6 : public Lease {
565
570
574 uint8_t prefixlen_;
575
581 uint32_t iaid_;
582
585
591
599
606
609
611
622 Lease6(Lease::Type type, const isc::asiolink::IOAddress& addr, DuidPtr duid,
623 uint32_t iaid, uint32_t preferred, uint32_t valid,
624 SubnetID subnet_id, const HWAddrPtr& hwaddr = HWAddrPtr(),
625 uint8_t prefixlen = 128);
626
641 Lease6(Lease::Type type, const isc::asiolink::IOAddress& addr, DuidPtr duid,
642 uint32_t iaid, uint32_t preferred, uint32_t valid,
643 SubnetID subnet_id, const bool fqdn_fwd,
644 const bool fqdn_rev, const std::string& hostname,
645 const HWAddrPtr& hwaddr = HWAddrPtr(), uint8_t prefixlen = 128);
646
650 Lease6();
651
658 virtual Lease::Type getType() const {
659 return (type_);
660 }
661
670 static std::string statesToText(const uint32_t state);
671
680 const std::vector<uint8_t>& getDuidVector() const;
681
687 void decline(uint32_t probation_period);
688
692 bool operator==(const Lease6& other) const;
693
697 bool operator!=(const Lease6& other) const {
698 return (!operator==(other));
699 }
700
704 virtual std::string toText() const;
705
707 virtual isc::data::ElementPtr toElement() const;
708
714 static Lease6Ptr fromElement(const data::ConstElementPtr& element);
715};
716
718typedef boost::shared_ptr<const Lease6> ConstLease6Ptr;
719
721typedef std::vector<Lease6Ptr> Lease6Collection;
722
724typedef boost::shared_ptr<Lease6Collection> Lease6CollectionPtr;
725
732std::ostream&
733operator<<(std::ostream& os, const Lease& lease);
734
737namespace {
738
739template <isc::util::DhcpSpace D>
740struct AdapterLease {};
741
742template <>
743struct AdapterLease<isc::util::DHCPv4> {
744 using type = Lease4;
745};
746
747template <>
748struct AdapterLease<isc::util::DHCPv6> {
749 using type = Lease6;
750};
751
752} // namespace
753
754template <isc::util::DhcpSpace D>
755using LeaseT = typename AdapterLease<D>::type;
756
757template <isc::util::DhcpSpace D>
758using LeaseTPtr = boost::shared_ptr<LeaseT<D>>;
760
761} // end of isc::dhcp namespace
762} // end of isc namespace
763
764#endif // LEASE_H
Holds Client identifier or client IPv4 address.
Definition duid.h:222
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:30
boost::shared_ptr< Element > ElementPtr
Definition data.h:29
boost::shared_ptr< Lease4Collection > Lease4CollectionPtr
A shared pointer to the collection of IPv4 leases.
Definition lease.h:551
boost::shared_ptr< DUID > DuidPtr
Definition duid.h:136
boost::shared_ptr< Lease6 > Lease6Ptr
Pointer to a Lease6 structure.
Definition lease.h:556
std::vector< Lease6Ptr > Lease6Collection
A collection of IPv6 leases.
Definition lease.h:721
boost::shared_ptr< LeaseT< D > > LeaseTPtr
Definition lease.h:758
boost::shared_ptr< HWAddr > HWAddrPtr
Shared pointer to a hardware address structure.
Definition hwaddr.h:166
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
Definition subnet_id.h:25
boost::shared_ptr< Lease > LeasePtr
Pointer to the lease object.
Definition lease.h:27
boost::shared_ptr< ClientId > ClientIdPtr
Shared pointer to a Client ID.
Definition duid.h:216
boost::shared_ptr< Lease6Collection > Lease6CollectionPtr
A shared pointer to the collection of IPv6 leases.
Definition lease.h:724
boost::shared_ptr< const Lease6 > ConstLease6Ptr
Pointer to a const Lease6 structure.
Definition lease.h:718
typename AdapterLease< D >::type LeaseT
Definition lease.h:755
std::vector< Lease4Ptr > Lease4Collection
A collection of IPv4 leases.
Definition lease.h:548
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition lease.h:343
Defines the logger used by the top-level component of kea-lfc.
Abstract class for configuration Cfg_* classes.
virtual isc::data::ElementPtr toElement() const =0
Unparse a configuration object.
Base class for user context.
Structure that holds a lease for IPv4 address.
Definition lease.h:351
ClientIdPtr client_id_
Client identifier.
Definition lease.h:357
void decline(uint32_t probation_period)
Sets IPv4 lease to declined state.
Definition lease.cc:380
static std::string statesToText(const uint32_t state)
Returns name of the lease states specific to DHCPv4.
Definition lease.cc:339
Lease4(const isc::asiolink::IOAddress &addr, const HWAddrPtr &hwaddr, const uint8_t *clientid, size_t clientid_len, uint32_t valid_lft, time_t cltt, uint32_t subnet_id, const bool fqdn_fwd=false, const bool fqdn_rev=false, const std::string &hostname="")
Constructor.
Definition lease.h:371
bool operator==(const Lease4 &other) const
Compare two leases for equality.
Definition lease.cc:603
virtual Lease::Type getType() const
Returns Lease type.
Definition lease.h:416
std::vector< uint8_t > remote_id_
Remote identifier for Bulk Lease Query.
Definition lease.h:541
std::vector< uint8_t > relay_id_
Relay identifier for Bulk Lease Query.
Definition lease.h:544
bool operator!=(const Lease4 &other) const
Compare two leases for inequality.
Definition lease.h:512
const std::vector< uint8_t > & getClientIdVector() const
Returns a client identifier.
Definition lease.cc:344
virtual isc::data::ElementPtr toElement() const
Return the JSON representation of a lease.
Definition lease.cc:393
bool belongsToClient(const HWAddrPtr &hw_address, const ClientIdPtr &client_id) const
Check if the lease belongs to the client with the given identifiers.
Definition lease.cc:363
Lease4()
Default constructor.
Definition lease.cc:335
virtual std::string toText() const
Convert lease to printable form.
Definition lease.cc:579
static Lease4Ptr fromElement(const data::ConstElementPtr &element)
Returns pointer to the IPv4 lease created from JSON representation.
Definition lease.cc:421
Structure that holds a lease for IPv6 address and/or prefix.
Definition lease.h:564
ExtendedInfoAction extended_info_action_
Record the action on extended info tables in the lease.
Definition lease.h:608
const std::vector< uint8_t > & getDuidVector() const
Returns a reference to a vector representing a DUID.
Definition lease.cc:530
virtual Lease::Type getType() const
Returns Lease type.
Definition lease.h:658
virtual std::string toText() const
Convert Lease to Printable Form.
Definition lease.cc:553
Lease6(Lease::Type type, const isc::asiolink::IOAddress &addr, DuidPtr duid, uint32_t iaid, uint32_t preferred, uint32_t valid, SubnetID subnet_id, const HWAddrPtr &hwaddr=HWAddrPtr(), uint8_t prefixlen=128)
Constructor.
Definition lease.cc:457
bool operator==(const Lease6 &other) const
Compare two leases for equality.
Definition lease.cc:622
static std::string statesToText(const uint32_t state)
Returns name of the lease states specific to DHCPv6.
Definition lease.cc:525
Lease6()
Constructor.
Definition lease.cc:517
Lease::Type type_
Lease type.
Definition lease.h:569
ExtendedInfoAction
Action on extended info tables.
Definition lease.h:601
@ ACTION_UPDATE
update extended info tables.
Definition lease.h:604
@ ACTION_DELETE
delete reference to the lease
Definition lease.h:603
@ ACTION_IGNORE
ignore extended info,
Definition lease.h:602
uint32_t reuseable_preferred_lft_
Remaining preferred lifetime.
Definition lease.h:598
bool operator!=(const Lease6 &other) const
Compare two leases for inequality.
Definition lease.h:697
uint32_t iaid_
Identity Association Identifier (IAID).
Definition lease.h:581
uint32_t preferred_lft_
Preferred lifetime.
Definition lease.h:590
DuidPtr duid_
Client identifier.
Definition lease.h:584
static Lease6Ptr fromElement(const data::ConstElementPtr &element)
Returns pointer to the IPv6 lease created from JSON representation.
Definition lease.cc:680
uint8_t prefixlen_
IPv6 prefix length.
Definition lease.h:574
virtual isc::data::ElementPtr toElement() const
Return the JSON representation of a lease.
Definition lease.cc:646
void decline(uint32_t probation_period)
Sets IPv6 lease to declined state.
Definition lease.cc:540
a common structure for IPv4 and IPv6 leases
Definition lease.h:33
void updateCurrentExpirationTime()
Update lease current expiration time with new value, so that additional operations can be done withou...
Definition lease.cc:311
bool hasIdenticalFqdn(const Lease &other) const
Returns true if the other lease has equal FQDN data.
Definition lease.cc:136
virtual void decline(uint32_t probation_period)=0
Sets lease to DECLINED state.
static const uint32_t INFINITY_LFT
Infinity (means static, i.e. never expire).
Definition lease.h:36
static constexpr uint32_t STATE_DEFAULT
A lease in the default state.
Definition lease.h:71
virtual ~Lease()
Destructor.
Definition lease.h:118
uint32_t reuseable_valid_lft_
Remaining valid lifetime.
Definition lease.h:145
static std::string lifetimeToText(uint32_t lifetime)
Print lifetime.
Definition lease.cc:34
void setDdnsReverseZone(const std::string &zone)
Sets the lease's ddns-reverse-zone in the lease's user-context.
Definition lease.cc:859
static constexpr uint32_t STATE_EXPIRED_RECLAIMED
Expired and reclaimed lease.
Definition lease.h:77
bool stateDeclined() const
Indicates if the lease is in the "declined" state.
Definition lease.cc:121
bool deleteFromUserContextISC(const std::string elem_name)
Remove an entry the ISC map in the lease's user-context.
Definition lease.cc:813
bool stateExpiredReclaimed() const
Indicates if the lease is in the "expired-reclaimed" state.
Definition lease.cc:116
static constexpr uint32_t STATE_DECLINED
Declined lease.
Definition lease.h:74
static constexpr uint32_t STATE_REGISTERED
Registered self-generated lease.
Definition lease.h:83
data::ConstElementPtr getFromUserContextISC(const std::string elem_name) const
Gets an entry from the ISC map in the lease's user-context.
Definition lease.cc:799
uint32_t current_valid_lft_
Current valid lifetime.
Definition lease.h:138
SubnetID subnet_id_
Subnet identifier.
Definition lease.h:162
uint32_t pool_id_
The pool id.
Definition lease.h:167
const std::vector< uint8_t > & getHWAddrVector() const
Returns raw (as vector) hardware address.
Definition lease.cc:354
uint32_t valid_lft_
Valid lifetime.
Definition lease.h:133
bool stateRegistered() const
Indicates if the lease is in the "registered" state.
Definition lease.cc:126
static std::string basicStatesToText(const uint32_t state)
Returns name(s) of the basic lease state(s).
Definition lease.cc:89
bool expired() const
returns true if the lease is expired
Definition lease.cc:111
static constexpr uint32_t STATE_RELEASED
Released lease held in the database for lease affinity.
Definition lease.h:80
Lease(const isc::asiolink::IOAddress &addr, uint32_t valid_lft, SubnetID subnet_id, time_t cltt, const bool fqdn_fwd, const bool fqdn_rev, const std::string &hostname, const HWAddrPtr &hwaddr)
Constructor.
Definition lease.cc:44
static void syncCurrentExpirationTime(const Lease &from, Lease &to)
Sync lease current expiration time with new value from another lease, so that additional operations c...
Definition lease.cc:316
std::string hostname_
Client hostname.
Definition lease.h:172
std::string getDdnsReverseZone() const
Retrieves the reverse zone from the lease's user-context.
Definition lease.cc:849
uint32_t state_
Holds the lease state(s).
Definition lease.h:198
virtual Lease::Type getType() const =0
Returns Lease type.
int64_t getExpirationTime() const
Returns lease expiration time.
Definition lease.cc:131
Type
Type of lease or pool.
Definition lease.h:48
@ TYPE_TA
deprecated (temporary IPv6 address)
Definition lease.h:50
@ TYPE_PD
the lease contains IPv6 prefix (for prefix delegation)
Definition lease.h:51
@ TYPE_V4
IPv4 lease.
Definition lease.h:52
@ TYPE_NA
the lease contains non-temporary IPv6 address
Definition lease.h:49
bool fqdn_fwd_
Forward zone updated?
Definition lease.h:177
time_t cltt_
Client last transmission time.
Definition lease.h:151
bool updateUserContextISC(const std::string elem_name, data::ConstElementPtr new_values)
Update the ISC entry in the lease's user-context.
Definition lease.cc:765
virtual std::string toText() const =0
Convert Lease to Printable Form.
static void fromElementCommon(const LeasePtr &lease, const data::ConstElementPtr &element)
Sets common (for v4 and v6) properties of the lease object.
Definition lease.cc:143
static std::string typeToText(Type type)
returns text representation of a lease type
Definition lease.cc:56
static Type textToType(const std::string &text)
Converts type name to the actual type.
Definition lease.cc:74
HWAddrPtr hwaddr_
Client's MAC/hardware address.
Definition lease.h:187
bool fqdn_rev_
Reverse zone updated?
Definition lease.h:182
isc::asiolink::IOAddress addr_
IPv4 ot IPv6 address.
Definition lease.h:128
time_t current_cltt_
Current client last transmission time.
Definition lease.h:157