Kea  2.3.8
lease.h
Go to the documentation of this file.
1 // Copyright (C) 2013-2023 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 <dhcp/duid.h>
12 #include <dhcp/option.h>
13 #include <dhcp/hwaddr.h>
14 #include <dhcpsrv/subnet_id.h>
15 #include <cc/user_context.h>
16 #include <cc/cfg_to_element.h>
17 #include <util/dhcp_space.h>
18 
19 namespace isc {
20 namespace dhcp {
21 
22 struct Lease;
23 
25 typedef boost::shared_ptr<Lease> LeasePtr;
26 
32 
34  static const uint32_t INFINITY_LFT = 0xffffffff;
35 
43  static std::string lifetimeToText(uint32_t lifetime);
44 
46  typedef enum {
47  TYPE_NA = 0,
48  TYPE_TA = 1,
49  TYPE_PD = 2,
50  TYPE_V4 = 3
51  } Type;
52 
56  static std::string typeToText(Type type);
57 
63  static Type textToType(const std::string& text);
64 
66 
67  static const uint32_t STATE_DEFAULT;
70 
72  static const uint32_t STATE_DECLINED;
73 
75  static const uint32_t STATE_EXPIRED_RECLAIMED;
76 
84  static std::string basicStatesToText(const uint32_t state);
85 
103  Lease(const isc::asiolink::IOAddress& addr,
104  uint32_t valid_lft, SubnetID subnet_id, time_t cltt,
105  const bool fqdn_fwd, const bool fqdn_rev,
106  const std::string& hostname,
107  const HWAddrPtr& hwaddr);
108 
110  virtual ~Lease() {}
111 
115  virtual Lease::Type getType() const = 0;
116 
121 
125  uint32_t valid_lft_;
126 
131 
138 
143  time_t cltt_;
144 
150 
155 
159  uint32_t pool_id_;
160 
164  std::string hostname_;
165 
169  bool fqdn_fwd_;
170 
174  bool fqdn_rev_;
175 
180 
190  uint32_t state_;
191 
195  virtual std::string toText() const = 0;
196 
199  bool expired() const;
200 
205  bool stateExpiredReclaimed() const;
206 
210  bool stateDeclined() const;
211 
220  bool hasIdenticalFqdn(const Lease& other) const;
221 
230  const std::vector<uint8_t>& getHWAddrVector() const;
231 
236  int64_t getExpirationTime() const;
237 
248  virtual void decline(uint32_t probation_period) = 0;
249 
252 
263  static void syncCurrentExpirationTime(const Lease& from, Lease& to);
264 
273 
274 protected:
275 
284  static void fromElementCommon(const LeasePtr& lease,
285  const data::ConstElementPtr& element);
286 
287 };
288 
289 struct Lease4;
290 
292 typedef boost::shared_ptr<Lease4> Lease4Ptr;
293 
300 struct Lease4 : public Lease {
301 
307 
320  Lease4(const isc::asiolink::IOAddress& addr, const HWAddrPtr& hwaddr,
321  const uint8_t* clientid, size_t clientid_len, uint32_t valid_lft,
322  time_t cltt, uint32_t subnet_id,
323  const bool fqdn_fwd = false, const bool fqdn_rev = false,
324  const std::string& hostname = "")
325  : Lease(addr, valid_lft, subnet_id, cltt, fqdn_fwd, fqdn_rev,
326  hostname, hwaddr) {
327  if (clientid_len) {
328  client_id_.reset(new ClientId(clientid, clientid_len));
329  }
330  }
331 
343  Lease4(const isc::asiolink::IOAddress& address,
344  const HWAddrPtr& hw_address,
345  const ClientIdPtr& client_id,
346  const uint32_t valid_lifetime,
347  const time_t cltt,
348  const SubnetID subnet_id,
349  const bool fqdn_fwd = false,
350  const bool fqdn_rev = false,
351  const std::string& hostname = "");
352 
353 
357  Lease4();
358 
365  virtual Lease::Type getType() const {
366  return (Lease::TYPE_V4);
367  }
368 
377  static std::string statesToText(const uint32_t state);
378 
388  const std::vector<uint8_t>& getClientIdVector() const;
389 
450  bool belongsToClient(const HWAddrPtr& hw_address,
451  const ClientIdPtr& client_id) const;
452 
456  bool operator==(const Lease4& other) const;
457 
461  bool operator!=(const Lease4& other) const {
462  return (!operator==(other));
463  }
464 
468  virtual std::string toText() const;
469 
475  void decline(uint32_t probation_period);
476 
478  virtual isc::data::ElementPtr toElement() const;
479 
485  static Lease4Ptr fromElement(const data::ConstElementPtr& element);
486 
488 
490  std::vector<uint8_t> remote_id_;
491 
493  std::vector<uint8_t> relay_id_;
494 };
495 
497 typedef std::vector<Lease4Ptr> Lease4Collection;
498 
500 typedef boost::shared_ptr<Lease4Collection> Lease4CollectionPtr;
501 
502 struct Lease6;
503 
505 typedef boost::shared_ptr<Lease6> Lease6Ptr;
506 
513 struct Lease6 : public Lease {
514 
519 
523  uint8_t prefixlen_;
524 
530  uint32_t iaid_;
531 
534 
539  uint32_t preferred_lft_;
540 
548 
550  typedef enum {
555 
558 
560 
571  Lease6(Lease::Type type, const isc::asiolink::IOAddress& addr, DuidPtr duid,
572  uint32_t iaid, uint32_t preferred, uint32_t valid,
573  SubnetID subnet_id, const HWAddrPtr& hwaddr = HWAddrPtr(),
574  uint8_t prefixlen = 128);
575 
590  Lease6(Lease::Type type, const isc::asiolink::IOAddress& addr, DuidPtr duid,
591  uint32_t iaid, uint32_t preferred, uint32_t valid,
592  SubnetID subnet_id, const bool fqdn_fwd,
593  const bool fqdn_rev, const std::string& hostname,
594  const HWAddrPtr& hwaddr = HWAddrPtr(), uint8_t prefixlen = 128);
595 
599  Lease6();
600 
607  virtual Lease::Type getType() const {
608  return (type_);
609  }
610 
619  static std::string statesToText(const uint32_t state);
620 
629  const std::vector<uint8_t>& getDuidVector() const;
630 
636  void decline(uint32_t probation_period);
637 
641  bool operator==(const Lease6& other) const;
642 
646  bool operator!=(const Lease6& other) const {
647  return (!operator==(other));
648  }
649 
653  virtual std::string toText() const;
654 
656  virtual isc::data::ElementPtr toElement() const;
657 
663  static Lease6Ptr fromElement(const data::ConstElementPtr& element);
664 };
665 
667 typedef boost::shared_ptr<const Lease6> ConstLease6Ptr;
668 
670 typedef std::vector<Lease6Ptr> Lease6Collection;
671 
673 typedef boost::shared_ptr<Lease6Collection> Lease6CollectionPtr;
674 
681 std::ostream&
682 operator<<(std::ostream& os, const Lease& lease);
683 
686 namespace {
687 
688 template <isc::util::DhcpSpace D>
689 struct AdapterLease {};
690 
691 template <>
692 struct AdapterLease<isc::util::DHCPv4> {
693  using type = Lease4;
694 };
695 
696 template <>
697 struct AdapterLease<isc::util::DHCPv6> {
698  using type = Lease6;
699 };
700 
701 } // namespace
702 
703 template <isc::util::DhcpSpace D>
704 using LeaseT = typename AdapterLease<D>::type;
705 
706 template <isc::util::DhcpSpace D>
707 using LeaseTPtr = boost::shared_ptr<LeaseT<D>>;
709 
710 } // end of isc::dhcp namespace
711 } // end of isc namespace
712 
713 #endif // LEASE_H
Holds Client identifier or client IPv4 address.
Definition: duid.h:218
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:29
boost::shared_ptr< Element > ElementPtr
Definition: data.h:26
boost::shared_ptr< Lease4Collection > Lease4CollectionPtr
A shared pointer to the collection of IPv4 leases.
Definition: lease.h:500
boost::shared_ptr< DUID > DuidPtr
Definition: duid.h:131
boost::shared_ptr< Lease6 > Lease6Ptr
Pointer to a Lease6 structure.
Definition: lease.h:502
std::vector< Lease6Ptr > Lease6Collection
A collection of IPv6 leases.
Definition: lease.h:670
std::ostream & operator<<(std::ostream &os, const OpaqueDataTuple &tuple)
Inserts the OpaqueDataTuple as a string into stream.
boost::shared_ptr< HWAddr > HWAddrPtr
Shared pointer to a hardware address structure.
Definition: hwaddr.h:154
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:22
boost::shared_ptr< ClientId > ClientIdPtr
Shared pointer to a Client ID.
Definition: duid.h:210
boost::shared_ptr< Lease6Collection > Lease6CollectionPtr
A shared pointer to the collection of IPv6 leases.
Definition: lease.h:673
boost::shared_ptr< const Lease6 > ConstLease6Ptr
Pointer to a const Lease6 structure.
Definition: lease.h:667
boost::shared_ptr< LeaseT< D > > LeaseTPtr
Definition: lease.h:707
std::vector< Lease4Ptr > Lease4Collection
A collection of IPv4 leases.
Definition: lease.h:497
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition: lease.h:289
typename AdapterLease< D >::type LeaseT
Definition: lease.h:704
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.
Definition: user_context.h:22
Structure that holds a lease for IPv4 address.
Definition: lease.h:300
ClientIdPtr client_id_
Client identifier.
Definition: lease.h:306
void decline(uint32_t probation_period)
Sets IPv4 lease to declined state.
Definition: lease.cc:371
static std::string statesToText(const uint32_t state)
Returns name of the lease states specific to DHCPv4.
Definition: lease.cc:330
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:320
bool operator==(const Lease4 &other) const
Compare two leases for equality.
Definition: lease.cc:565
virtual Lease::Type getType() const
Returns Lease type.
Definition: lease.h:365
std::vector< uint8_t > remote_id_
Remote identifier for Bulk Lease Query.
Definition: lease.h:490
std::vector< uint8_t > relay_id_
Relay identifier for Bulk Lease Query.
Definition: lease.h:493
bool operator!=(const Lease4 &other) const
Compare two leases for inequality.
Definition: lease.h:461
const std::vector< uint8_t > & getClientIdVector() const
Returns a client identifier.
Definition: lease.cc:335
virtual isc::data::ElementPtr toElement() const
Return the JSON representation of a lease.
Definition: lease.cc:383
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:354
Lease4()
Default constructor.
Definition: lease.cc:326
virtual std::string toText() const
Convert lease to printable form.
Definition: lease.cc:541
static Lease4Ptr fromElement(const data::ConstElementPtr &element)
Returns pointer to the IPv4 lease created from JSON representation.
Definition: lease.cc:409
Structure that holds a lease for IPv6 address and/or prefix.
Definition: lease.h:513
ExtendedInfoAction extended_info_action_
Record the action on extended info tables in the lease.
Definition: lease.h:557
const std::vector< uint8_t > & getDuidVector() const
Returns a reference to a vector representing a DUID.
Definition: lease.cc:492
virtual Lease::Type getType() const
Returns Lease type.
Definition: lease.h:607
ExtendedInfoAction
Action on extended info tables.
Definition: lease.h:550
@ ACTION_UPDATE
update extended info tables.
Definition: lease.h:553
@ ACTION_DELETE
delete reference to the lease
Definition: lease.h:552
@ ACTION_IGNORE
ignore extended info,
Definition: lease.h:551
virtual std::string toText() const
Convert Lease to Printable Form.
Definition: lease.cc:515
bool operator==(const Lease6 &other) const
Compare two leases for equality.
Definition: lease.cc:584
static std::string statesToText(const uint32_t state)
Returns name of the lease states specific to DHCPv6.
Definition: lease.cc:487
Lease6()
Constructor.
Definition: lease.cc:479
Lease::Type type_
Lease type.
Definition: lease.h:518
uint32_t reuseable_preferred_lft_
Remaining preferred lifetime.
Definition: lease.h:547
bool operator!=(const Lease6 &other) const
Compare two leases for inequality.
Definition: lease.h:646
uint32_t iaid_
Identity Association Identifier (IAID)
Definition: lease.h:530
uint32_t preferred_lft_
Preferred lifetime.
Definition: lease.h:539
DuidPtr duid_
Client identifier.
Definition: lease.h:533
static Lease6Ptr fromElement(const data::ConstElementPtr &element)
Returns pointer to the IPv6 lease created from JSON representation.
Definition: lease.cc:640
uint8_t prefixlen_
IPv6 prefix length.
Definition: lease.h:523
virtual isc::data::ElementPtr toElement() const
Return the JSON representation of a lease.
Definition: lease.cc:608
void decline(uint32_t probation_period)
Sets IPv6 lease to declined state.
Definition: lease.cc:502
a common structure for IPv4 and IPv6 leases
Definition: lease.h:31
void updateCurrentExpirationTime()
Update lease current expiration time with new value, so that additional operations can be done withou...
Definition: lease.cc:302
bool hasIdenticalFqdn(const Lease &other) const
Returns true if the other lease has equal FQDN data.
Definition: lease.cc:127
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:34
virtual ~Lease()
Destructor.
Definition: lease.h:110
uint32_t reuseable_valid_lft_
Remaining valid lifetime.
Definition: lease.h:137
static std::string lifetimeToText(uint32_t lifetime)
Print lifetime.
Definition: lease.cc:29
bool stateDeclined() const
Indicates if the lease is in the "declined" state.
Definition: lease.cc:117
bool stateExpiredReclaimed() const
Indicates if the lease is in the "expired-reclaimed" state.
Definition: lease.cc:112
static const uint32_t STATE_DEFAULT
A lease in the default state.
Definition: lease.h:69
uint32_t current_valid_lft_
Current valid lifetime.
Definition: lease.h:130
SubnetID subnet_id_
Subnet identifier.
Definition: lease.h:154
uint32_t pool_id_
The pool id.
Definition: lease.h:159
const std::vector< uint8_t > & getHWAddrVector() const
Returns raw (as vector) hardware address.
Definition: lease.cc:345
uint32_t valid_lft_
Valid lifetime.
Definition: lease.h:125
static std::string basicStatesToText(const uint32_t state)
Returns name(s) of the basic lease state(s).
Definition: lease.cc:89
static const uint32_t STATE_DECLINED
Declined lease.
Definition: lease.h:72
bool expired() const
returns true if the lease is expired
Definition: lease.cc:107
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:39
static const uint32_t STATE_EXPIRED_RECLAIMED
Expired and reclaimed lease.
Definition: lease.h:75
Type
Type of lease or pool.
Definition: lease.h:46
@ TYPE_TA
the lease contains temporary IPv6 address
Definition: lease.h:48
@ TYPE_PD
the lease contains IPv6 prefix (for prefix delegation)
Definition: lease.h:49
@ TYPE_V4
IPv4 lease.
Definition: lease.h:50
@ TYPE_NA
the lease contains non-temporary IPv6 address
Definition: lease.h:47
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:307
std::string hostname_
Client hostname.
Definition: lease.h:164
uint32_t state_
Holds the lease state(s).
Definition: lease.h:190
virtual Lease::Type getType() const =0
Returns Lease type.
int64_t getExpirationTime() const
Returns lease expiration time.
Definition: lease.cc:122
bool fqdn_fwd_
Forward zone updated?
Definition: lease.h:169
time_t cltt_
Client last transmission time.
Definition: lease.h:143
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:134
static std::string typeToText(Type type)
returns text representation of a lease type
Definition: lease.cc:51
static Type textToType(const std::string &text)
Converts type name to the actual type.
Definition: lease.cc:71
HWAddrPtr hwaddr_
Client's MAC/hardware address.
Definition: lease.h:179
bool fqdn_rev_
Reverse zone updated?
Definition: lease.h:174
isc::asiolink::IOAddress addr_
IPv4 ot IPv6 address.
Definition: lease.h:120
time_t current_cltt_
Current client last transmission time.
Definition: lease.h:149