Kea  2.3.5-git
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  std::string hostname_;
160 
164  bool fqdn_fwd_;
165 
169  bool fqdn_rev_;
170 
175 
185  uint32_t state_;
186 
188  typedef enum {
193 
195  ExtendedInfoAction extended_info_action_;
196 
200  virtual std::string toText() const = 0;
201 
204  bool expired() const;
205 
210  bool stateExpiredReclaimed() const;
211 
215  bool stateDeclined() const;
216 
225  bool hasIdenticalFqdn(const Lease& other) const;
226 
235  const std::vector<uint8_t>& getHWAddrVector() const;
236 
241  int64_t getExpirationTime() const;
242 
253  virtual void decline(uint32_t probation_period) = 0;
254 
257 
268  static void syncCurrentExpirationTime(const Lease& from, Lease& to);
269 
278 
279 protected:
280 
289  static void fromElementCommon(const LeasePtr& lease,
290  const data::ConstElementPtr& element);
291 
292 };
293 
294 struct Lease4;
295 
297 typedef boost::shared_ptr<Lease4> Lease4Ptr;
298 
305 struct Lease4 : public Lease {
306 
312 
325  Lease4(const isc::asiolink::IOAddress& addr, const HWAddrPtr& hwaddr,
326  const uint8_t* clientid, size_t clientid_len, uint32_t valid_lft,
327  time_t cltt, uint32_t subnet_id,
328  const bool fqdn_fwd = false, const bool fqdn_rev = false,
329  const std::string& hostname = "")
330  : Lease(addr, valid_lft, subnet_id, cltt, fqdn_fwd, fqdn_rev,
331  hostname, hwaddr) {
332  if (clientid_len) {
333  client_id_.reset(new ClientId(clientid, clientid_len));
334  }
335  }
336 
348  Lease4(const isc::asiolink::IOAddress& address,
349  const HWAddrPtr& hw_address,
350  const ClientIdPtr& client_id,
351  const uint32_t valid_lifetime,
352  const time_t cltt,
353  const SubnetID subnet_id,
354  const bool fqdn_fwd = false,
355  const bool fqdn_rev = false,
356  const std::string& hostname = "");
357 
358 
362  Lease4() : Lease(0, 0, 0, 0, false, false, "", HWAddrPtr())
363  {
364  }
365 
369  Lease4(const Lease4& other);
370 
377  virtual Lease::Type getType() const {
378  return (Lease::TYPE_V4);
379  }
380 
389  static std::string statesToText(const uint32_t state);
390 
400  const std::vector<uint8_t>& getClientIdVector() const;
401 
462  bool belongsToClient(const HWAddrPtr& hw_address,
463  const ClientIdPtr& client_id) const;
464 
468  Lease4& operator=(const Lease4& other);
469 
473  bool operator==(const Lease4& other) const;
474 
478  bool operator!=(const Lease4& other) const {
479  return (!operator==(other));
480  }
481 
485  virtual std::string toText() const;
486 
492  void decline(uint32_t probation_period);
493 
495  virtual isc::data::ElementPtr toElement() const;
496 
502  static Lease4Ptr fromElement(const data::ConstElementPtr& element);
503 
505 
507  std::vector<uint8_t> remote_id_;
508 
510  std::vector<uint8_t> relay_id_;
511 };
512 
514 typedef std::vector<Lease4Ptr> Lease4Collection;
515 
517 typedef boost::shared_ptr<Lease4Collection> Lease4CollectionPtr;
518 
519 struct Lease6;
520 
522 typedef boost::shared_ptr<Lease6> Lease6Ptr;
523 
530 struct Lease6 : public Lease {
531 
536 
540  uint8_t prefixlen_;
541 
547  uint32_t iaid_;
548 
551 
556  uint32_t preferred_lft_;
557 
565 
567 
578  Lease6(Lease::Type type, const isc::asiolink::IOAddress& addr, DuidPtr duid,
579  uint32_t iaid, uint32_t preferred, uint32_t valid,
580  SubnetID subnet_id, const HWAddrPtr& hwaddr = HWAddrPtr(),
581  uint8_t prefixlen = 128);
582 
597  Lease6(Lease::Type type, const isc::asiolink::IOAddress& addr, DuidPtr duid,
598  uint32_t iaid, uint32_t preferred, uint32_t valid,
599  SubnetID subnet_id, const bool fqdn_fwd,
600  const bool fqdn_rev, const std::string& hostname,
601  const HWAddrPtr& hwaddr = HWAddrPtr(), uint8_t prefixlen = 128);
602 
606  Lease6();
607 
614  virtual Lease::Type getType() const {
615  return (type_);
616  }
617 
626  static std::string statesToText(const uint32_t state);
627 
636  const std::vector<uint8_t>& getDuidVector() const;
637 
643  void decline(uint32_t probation_period);
644 
648  bool operator==(const Lease6& other) const;
649 
653  bool operator!=(const Lease6& other) const {
654  return (!operator==(other));
655  }
656 
660  virtual std::string toText() const;
661 
663  virtual isc::data::ElementPtr toElement() const;
664 
670  static Lease6Ptr fromElement(const data::ConstElementPtr& element);
671 };
672 
674 typedef boost::shared_ptr<const Lease6> ConstLease6Ptr;
675 
677 typedef std::vector<Lease6Ptr> Lease6Collection;
678 
680 typedef boost::shared_ptr<Lease6Collection> Lease6CollectionPtr;
681 
688 std::ostream&
689 operator<<(std::ostream& os, const Lease& lease);
690 
693 namespace {
694 
695 template <isc::util::DhcpSpace D>
696 struct AdapterLease {};
697 
698 template <>
699 struct AdapterLease<isc::util::DHCPv4> {
700  using type = Lease4;
701 };
702 
703 template <>
704 struct AdapterLease<isc::util::DHCPv6> {
705  using type = Lease6;
706 };
707 
708 } // namespace
709 
710 template <isc::util::DhcpSpace D>
711 using LeaseT = typename AdapterLease<D>::type;
712 
713 template <isc::util::DhcpSpace D>
714 using LeaseTPtr = boost::shared_ptr<LeaseT<D>>;
716 
717 }; // end of isc::dhcp namespace
718 }; // end of isc namespace
719 
720 #endif // LEASE_H
static std::string lifetimeToText(uint32_t lifetime)
Print lifetime.
Definition: lease.cc:29
uint32_t state_
Holds the lease state(s).
Definition: lease.h:185
boost::shared_ptr< DUID > DuidPtr
Definition: duid.h:20
HWAddrPtr hwaddr_
Client&#39;s MAC/hardware address.
Definition: lease.h:174
uint32_t iaid_
Identity Association Identifier (IAID)
Definition: lease.h:547
typename AdapterLease< D >::type LeaseT
Definition: lease.h:711
static const uint32_t STATE_EXPIRED_RECLAIMED
Expired and reclaimed lease.
Definition: lease.h:75
Structure that holds a lease for IPv4 address.
Definition: lease.h:305
std::vector< uint8_t > relay_id_
Relay identifier for Bulk Lease Query.
Definition: lease.h:510
uint32_t reuseable_preferred_lft_
Remaining preferred lifetime.
Definition: lease.h:564
bool fqdn_rev_
Reverse zone updated?
Definition: lease.h:169
virtual void decline(uint32_t probation_period)=0
Sets lease to DECLINED state.
boost::shared_ptr< HWAddr > HWAddrPtr
Shared pointer to a hardware address structure.
Definition: hwaddr.h:154
static std::string typeToText(Type type)
returns text representation of a lease type
Definition: lease.cc:53
const std::vector< uint8_t > & getHWAddrVector() const
Returns raw (as vector) hardware address.
Definition: lease.cc:356
Base class for user context.
Definition: user_context.h:22
time_t cltt_
Client last transmission time.
Definition: lease.h:143
uint32_t preferred_lft_
Preferred lifetime.
Definition: lease.h:556
bool fqdn_fwd_
Forward zone updated?
Definition: lease.h:164
std::vector< Lease4Ptr > Lease4Collection
A collection of IPv4 leases.
Definition: lease.h:514
virtual Lease::Type getType() const
Returns Lease type.
Definition: lease.h:377
the lease contains IPv6 prefix (for prefix delegation)
Definition: lease.h:49
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
std::string hostname_
Client hostname.
Definition: lease.h:159
boost::shared_ptr< const Lease6 > ConstLease6Ptr
Pointer to a const Lease6 structure.
Definition: lease.h:674
SubnetID subnet_id_
Subnet identifier.
Definition: lease.h:154
ignore extended info,
Definition: lease.h:189
ExtendedInfoAction extended_info_action_
Record the action on extended info tables in the lease.
Definition: lease.h:195
virtual std::string toText() const =0
Convert Lease to Printable Form.
boost::shared_ptr< Lease > LeasePtr
Pointer to the lease object.
Definition: lease.h:22
bool operator!=(const Lease4 &other) const
Compare two leases for inequality.
Definition: lease.h:478
bool operator==(const Element &a, const Element &b)
Definition: data.cc:210
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:292
int64_t getExpirationTime() const
Returns lease expiration time.
Definition: lease.cc:124
boost::shared_ptr< Lease4 > Lease4Ptr
Pointer to a Lease4 structure.
Definition: lease.h:294
virtual Lease::Type getType() const
Returns Lease type.
Definition: lease.h:614
Abstract class for configuration Cfg_* classes.
boost::shared_ptr< Lease4Collection > Lease4CollectionPtr
A shared pointer to the collection of IPv4 leases.
Definition: lease.h:517
Lease4()
Default constructor.
Definition: lease.h:362
boost::shared_ptr< LeaseT< D > > LeaseTPtr
Definition: lease.h:714
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:325
IPv4 lease.
Definition: lease.h:50
void updateCurrentExpirationTime()
Update lease current expiration time with new value, so that additional operations can be done withou...
Definition: lease.cc:287
virtual Lease::Type getType() const =0
Returns Lease type.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
DuidPtr duid_
Client identifier.
Definition: lease.h:550
Structure that holds a lease for IPv6 address and/or prefix.
Definition: lease.h:530
ClientIdPtr client_id_
Client identifier.
Definition: lease.h:311
Lease::Type type_
Lease type.
Definition: lease.h:535
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
uint8_t prefixlen_
IPv6 prefix length.
Definition: lease.h:540
std::vector< Lease6Ptr > Lease6Collection
A collection of IPv6 leases.
Definition: lease.h:677
boost::shared_ptr< ClientId > ClientIdPtr
Shared pointer to a Client ID.
Definition: duid.h:103
the lease contains temporary IPv6 address
Definition: lease.h:48
the lease contains non-temporary IPv6 address
Definition: lease.h:47
bool expired() const
returns true if the lease is expired
Definition: lease.cc:109
time_t current_cltt_
Current client last transmission time.
Definition: lease.h:149
Defines the logger used by the top-level component of kea-lfc.
static Type textToType(const std::string &text)
Converts type name to the actual type.
Definition: lease.cc:73
bool operator!=(const Lease6 &other) const
Compare two leases for inequality.
Definition: lease.h:653
update extended info tables.
Definition: lease.h:191
bool stateExpiredReclaimed() const
Indicates if the lease is in the "expired-reclaimed" state.
Definition: lease.cc:114
static const uint32_t INFINITY_LFT
Infinity (means static, i.e. never expire)
Definition: lease.h:34
a common structure for IPv4 and IPv6 leases
Definition: lease.h:31
Type
Type of lease or pool.
Definition: lease.h:46
std::ostream & operator<<(std::ostream &os, const OpaqueDataTuple &tuple)
Inserts the OpaqueDataTuple as a string into stream.
Holds Client identifier or client IPv4 address.
Definition: duid.h:111
static std::string basicStatesToText(const uint32_t state)
Returns name(s) of the basic lease state(s).
Definition: lease.cc:91
boost::shared_ptr< Lease6Collection > Lease6CollectionPtr
A shared pointer to the collection of IPv6 leases.
Definition: lease.h:680
virtual ~Lease()
Destructor.
Definition: lease.h:110
uint32_t reuseable_valid_lft_
Remaining valid lifetime.
Definition: lease.h:137
isc::asiolink::IOAddress addr_
IPv4 ot IPv6 address.
Definition: lease.h:120
static const uint32_t STATE_DEFAULT
A lease in the default state.
Definition: lease.h:69
static const uint32_t STATE_DECLINED
Declined lease.
Definition: lease.h:72
bool stateDeclined() const
Indicates if the lease is in the "declined" state.
Definition: lease.cc:119
ExtendedInfoAction
Action on extended info tables.
Definition: lease.h:188
uint32_t valid_lft_
Valid lifetime.
Definition: lease.h:125
virtual isc::data::ElementPtr toElement() const =0
Unparse a configuration object.
static void fromElementCommon(const LeasePtr &lease, const data::ConstElementPtr &element)
Sets common (for v4 and v6) properties of the lease object.
Definition: lease.cc:136
std::vector< uint8_t > remote_id_
Remote identifier for Bulk Lease Query.
Definition: lease.h:507
boost::shared_ptr< Lease6 > Lease6Ptr
Pointer to a Lease6 structure.
Definition: lease.h:519
delete reference to the lease
Definition: lease.h:190
uint32_t current_valid_lft_
Current valid lifetime.
Definition: lease.h:130
bool hasIdenticalFqdn(const Lease &other) const
Returns true if the other lease has equal FQDN data.
Definition: lease.cc:129
uint32_t SubnetID
Defines unique IPv4 or IPv6 subnet identifier.
Definition: subnet_id.h:25