lease.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. // Copyright (C) 2013-2014 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef LEASE_H
  15. #define LEASE_H
  16. #include <asiolink/io_address.h>
  17. #include <dhcp/duid.h>
  18. #include <dhcp/option.h>
  19. #include <dhcp/hwaddr.h>
  20. namespace isc {
  21. namespace dhcp {
  22. /// @brief Unique identifier for a subnet (both v4 and v6)
  23. ///
  24. /// Let's copy SubnetID definition from subnet.h. We can't include it directly,
  25. /// because subnet.h needs Lease::Type, so it includes lease.h
  26. typedef uint32_t SubnetID;
  27. /// @brief a common structure for IPv4 and IPv6 leases
  28. ///
  29. /// This structure holds all information that is common between IPv4 and IPv6
  30. /// leases.
  31. struct Lease {
  32. /// @brief Type of lease or pool
  33. typedef enum {
  34. TYPE_NA = 0, /// the lease contains non-temporary IPv6 address
  35. TYPE_TA = 1, /// the lease contains temporary IPv6 address
  36. TYPE_PD = 2, /// the lease contains IPv6 prefix (for prefix delegation)
  37. TYPE_V4 = 3 /// IPv4 lease
  38. } Type;
  39. /// @brief returns text representation of a lease type
  40. /// @param type lease or pool type to be converted
  41. /// @return text decription
  42. static std::string typeToText(Type type);
  43. /// @brief Constructor
  44. ///
  45. /// @param addr IP address
  46. /// @param t1 renewal time
  47. /// @param t2 rebinding time
  48. /// @param valid_lft Lifetime of the lease
  49. /// @param subnet_id Subnet identification
  50. /// @param cltt Client last transmission time
  51. /// @param fqdn_fwd If true, forward DNS update is performed for a lease.
  52. /// @param fqdn_rev If true, reverse DNS update is performed for a lease.
  53. /// @param hostname FQDN of the client which gets the lease.
  54. /// @param hwaddr Hardware/MAC address
  55. Lease(const isc::asiolink::IOAddress& addr, uint32_t t1, uint32_t t2,
  56. uint32_t valid_lft, SubnetID subnet_id, time_t cltt,
  57. const bool fqdn_fwd, const bool fqdn_rev,
  58. const std::string& hostname,
  59. const HWAddrPtr& hwaddr);
  60. /// @brief Destructor
  61. virtual ~Lease() {}
  62. /// @brief IPv4 ot IPv6 address
  63. ///
  64. /// IPv4, IPv6 address or, in the case of a prefix delegation, the prefix.
  65. isc::asiolink::IOAddress addr_;
  66. /// @brief Renewal timer
  67. ///
  68. /// Specifies renewal time. Although technically it is a property of the
  69. /// IA container and not the address itself, since our data model does not
  70. /// define a separate IA entity, we are keeping it in the lease. In the
  71. /// case of multiple addresses/prefixes for the same IA, each must have
  72. /// consistent T1 and T2 values. This is specified in seconds since cltt.
  73. uint32_t t1_;
  74. /// @brief Rebinding timer
  75. ///
  76. /// Specifies rebinding time. Although technically it is a property of the
  77. /// IA container and not the address itself, since our data model does not
  78. /// define a separate IA entity, we are keeping it in the lease. In the
  79. /// case of multiple addresses/prefixes for the same IA, each must have
  80. /// consistent T1 and T2 values. This is specified in seconds since cltt.
  81. uint32_t t2_;
  82. /// @brief Valid lifetime
  83. ///
  84. /// Expressed as number of seconds since cltt.
  85. uint32_t valid_lft_;
  86. /// @brief Client last transmission time
  87. ///
  88. /// Specifies a timestamp giving the time when the last transmission from a
  89. /// client was received.
  90. time_t cltt_;
  91. /// @brief Subnet identifier
  92. ///
  93. /// Specifies the identification of the subnet to which the lease belongs.
  94. SubnetID subnet_id_;
  95. /// @brief Fixed lease?
  96. ///
  97. /// Fixed leases are kept after they are released/expired.
  98. bool fixed_;
  99. /// @brief Client hostname
  100. ///
  101. /// This field may be empty
  102. std::string hostname_;
  103. /// @brief Forward zone updated?
  104. ///
  105. /// Set true if the DNS AAAA record for this lease has been updated.
  106. bool fqdn_fwd_;
  107. /// @brief Reverse zone updated?
  108. ///
  109. /// Set true if the DNS PTR record for this lease has been updated.
  110. bool fqdn_rev_;
  111. /// @brief Client's MAC/hardware address
  112. ///
  113. /// This information may not be available in certain cases.
  114. HWAddrPtr hwaddr_;
  115. /// @brief Lease comments
  116. ///
  117. /// Currently not used. It may be used for keeping comments made by the
  118. /// system administrator.
  119. std::string comments_;
  120. /// @brief Convert Lease to Printable Form
  121. ///
  122. /// @return String form of the lease
  123. virtual std::string toText() const = 0;
  124. /// @brief returns true if the lease is expired
  125. /// @return true if the lease is expired
  126. bool expired() const;
  127. /// @brief Returns true if the other lease has equal FQDN data.
  128. ///
  129. /// @param other Lease which FQDN data is to be compared with our lease.
  130. ///
  131. /// @return Boolean value which indicates whether FQDN data of the other
  132. /// lease is equal to the FQDN data of our lease (true) or not (false).
  133. bool hasIdenticalFqdn(const Lease& other) const;
  134. /// @brief Returns raw (as vector) hardware address
  135. ///
  136. /// This method is needed in multi-index container as key extractor.
  137. /// The const reference is only valid as long as the object that returned it.
  138. /// In the unlikely case when Lease4 does not have a hardware address,
  139. /// the function will return an empty vector.
  140. ///
  141. /// @return const reference to the hardware address
  142. const std::vector<uint8_t>& getHWAddrVector() const;
  143. };
  144. /// @brief Structure that holds a lease for IPv4 address
  145. ///
  146. /// For performance reasons it is a simple structure, not a class. If we chose
  147. /// make it a class, all fields would have to made private and getters/setters
  148. /// would be required. As this is a critical part of the code that will be used
  149. /// extensively, direct access is warranted.
  150. struct Lease4 : public Lease {
  151. /// @brief Address extension
  152. ///
  153. /// It is envisaged that in some cases IPv4 address will be accompanied
  154. /// with some additional data. One example of such use are Address + Port
  155. /// solutions (or Port-restricted Addresses), where several clients may get
  156. /// the same address, but different port ranges. This feature is not
  157. /// expected to be widely used. Under normal circumstances, the value
  158. /// should be 0.
  159. uint32_t ext_;
  160. /// @brief Client identifier
  161. ///
  162. /// @todo Should this be a pointer to a client ID or the ID itself?
  163. /// Compare with the DUID in the Lease6 structure.
  164. ClientIdPtr client_id_;
  165. /// @brief Constructor
  166. ///
  167. /// @param addr IPv4 address.
  168. /// @param hwaddr A pointer to HWAddr structure
  169. /// @param clientid Client identification buffer
  170. /// @param clientid_len Length of client identification buffer
  171. /// @param valid_lft Lifetime of the lease
  172. /// @param t1 renewal time
  173. /// @param t2 rebinding time
  174. /// @param cltt Client last transmission time
  175. /// @param subnet_id Subnet identification
  176. /// @param fqdn_fwd If true, forward DNS update is performed for a lease.
  177. /// @param fqdn_rev If true, reverse DNS update is performed for a lease.
  178. /// @param hostname FQDN of the client which gets the lease.
  179. Lease4(const isc::asiolink::IOAddress& addr, const HWAddrPtr& hwaddr,
  180. const uint8_t* clientid, size_t clientid_len, uint32_t valid_lft,
  181. uint32_t t1, uint32_t t2, time_t cltt, uint32_t subnet_id,
  182. const bool fqdn_fwd = false, const bool fqdn_rev = false,
  183. const std::string& hostname = "")
  184. : Lease(addr, t1, t2, valid_lft, subnet_id, cltt, fqdn_fwd, fqdn_rev,
  185. hostname, hwaddr), ext_(0) {
  186. if (clientid_len) {
  187. client_id_.reset(new ClientId(clientid, clientid_len));
  188. }
  189. }
  190. /// @brief Default constructor
  191. ///
  192. /// Initialize fields that don't have a default constructor.
  193. Lease4() : Lease(0, 0, 0, 0, 0, 0, false, false, "", HWAddrPtr()) {
  194. }
  195. /// @brief Copy constructor
  196. ///
  197. /// @param other the @c Lease4 object to be copied.
  198. Lease4(const Lease4& other);
  199. /// @brief Returns a client identifier.
  200. ///
  201. /// @warning Since the function returns the reference to a vector (not a
  202. /// copy), the returned object should be used with caution because it will
  203. /// remain valid only for the period of time when an object which returned
  204. /// it exists.
  205. ///
  206. /// @return A reference to a vector holding client identifier,
  207. /// or an empty vector if client identifier is NULL.
  208. const std::vector<uint8_t>& getClientIdVector() const;
  209. /// @brief Check if two objects encapsulate the lease for the same
  210. /// client.
  211. ///
  212. /// Checks if two @c Lease4 objects have the same address, client id,
  213. /// HW address and ext_ value. If these parameters match it is an
  214. /// indication that both objects describe the lease for the same
  215. /// client but apparently one is a result of renewal of the other. The
  216. /// special case of the matching lease is the one that is equal to another.
  217. ///
  218. /// @param other A lease to compare with.
  219. ///
  220. /// @return true if the selected parameters of the two leases match.
  221. bool matches(const Lease4& other) const;
  222. /// @brief Assignment operator.
  223. ///
  224. /// @param other the @c Lease4 object to be assigned.
  225. Lease4& operator=(const Lease4& other);
  226. /// @brief Compare two leases for equality
  227. ///
  228. /// @param other lease6 object with which to compare
  229. bool operator==(const Lease4& other) const;
  230. /// @brief Compare two leases for inequality
  231. ///
  232. /// @param other lease6 object with which to compare
  233. bool operator!=(const Lease4& other) const {
  234. return (!operator==(other));
  235. }
  236. /// @brief Convert lease to printable form
  237. ///
  238. /// @return Textual represenation of lease data
  239. virtual std::string toText() const;
  240. /// @todo: Add DHCPv4 failover related fields here
  241. };
  242. /// @brief Pointer to a Lease4 structure.
  243. typedef boost::shared_ptr<Lease4> Lease4Ptr;
  244. /// @brief A collection of IPv4 leases.
  245. typedef std::vector<Lease4Ptr> Lease4Collection;
  246. /// @brief Structure that holds a lease for IPv6 address and/or prefix
  247. ///
  248. /// For performance reasons it is a simple structure, not a class. If we chose
  249. /// make it a class, all fields would have to made private and getters/setters
  250. /// would be required. As this is a critical part of the code that will be used
  251. /// extensively, direct access is warranted.
  252. struct Lease6 : public Lease {
  253. /// @brief Lease type
  254. ///
  255. /// One of normal address, temporary address, or prefix.
  256. Type type_;
  257. /// @brief IPv6 prefix length
  258. ///
  259. /// This is used only for prefix delegations and is ignored otherwise.
  260. uint8_t prefixlen_;
  261. /// @brief Identity Association Identifier (IAID)
  262. ///
  263. /// DHCPv6 stores all addresses and prefixes in IA containers (IA_NA,
  264. /// IA_TA, IA_PD). All containers may appear more than once in a message.
  265. /// To differentiate between them, the IAID field is present
  266. uint32_t iaid_;
  267. /// @brief Client identifier
  268. DuidPtr duid_;
  269. /// @brief preferred lifetime
  270. ///
  271. /// This parameter specifies the preferred lifetime since the lease was
  272. /// assigned or renewed (cltt), expressed in seconds.
  273. uint32_t preferred_lft_;
  274. /// @todo: Add DHCPv6 failover related fields here
  275. /// @brief Constructor
  276. /// @param type Lease type.
  277. /// @param addr Assigned address.
  278. /// @param duid A pointer to an object representing DUID.
  279. /// @param iaid IAID.
  280. /// @param preferred Preferred lifetime.
  281. /// @param valid Valid lifetime.
  282. /// @param t1 A value of the T1 timer.
  283. /// @param t2 A value of the T2 timer.
  284. /// @param subnet_id A Subnet identifier.
  285. /// @param hwaddr hardware/MAC address (optional)
  286. /// @param prefixlen An address prefix length (optional, defaults to 128)
  287. Lease6(Type type, const isc::asiolink::IOAddress& addr, DuidPtr duid,
  288. uint32_t iaid, uint32_t preferred, uint32_t valid, uint32_t t1,
  289. uint32_t t2, SubnetID subnet_id, const HWAddrPtr& hwaddr = HWAddrPtr(),
  290. uint8_t prefixlen = 128);
  291. /// @brief Constructor, including FQDN data.
  292. ///
  293. /// @param type Lease type.
  294. /// @param addr Assigned address.
  295. /// @param duid A pointer to an object representing DUID.
  296. /// @param iaid IAID.
  297. /// @param preferred Preferred lifetime.
  298. /// @param valid Valid lifetime.
  299. /// @param t1 A value of the T1 timer.
  300. /// @param t2 A value of the T2 timer.
  301. /// @param subnet_id A Subnet identifier.
  302. /// @param fqdn_fwd If true, forward DNS update is performed for a lease.
  303. /// @param fqdn_rev If true, reverse DNS update is performed for a lease.
  304. /// @param hostname FQDN of the client which gets the lease.
  305. /// @param hwaddr hardware address (MAC), may be NULL
  306. /// @param prefixlen An address prefix length.
  307. Lease6(Type type, const isc::asiolink::IOAddress& addr, DuidPtr duid,
  308. uint32_t iaid, uint32_t preferred, uint32_t valid, uint32_t t1,
  309. uint32_t t2, SubnetID subnet_id, const bool fqdn_fwd,
  310. const bool fqdn_rev, const std::string& hostname,
  311. const HWAddrPtr& hwaddr = HWAddrPtr(), uint8_t prefixlen = 0);
  312. /// @brief Constructor
  313. ///
  314. /// Initialize fields that don't have a default constructor.
  315. Lease6();
  316. /// @brief Returns a reference to a vector representing a DUID.
  317. ///
  318. /// @warning Since the function returns the reference to a vector (not a
  319. /// copy), the returned object should be used with caution because it will
  320. /// remain valid only for the period of time when an object which returned
  321. /// it exists.
  322. ///
  323. /// @return A reference to a vector holding a DUID.
  324. const std::vector<uint8_t>& getDuidVector() const;
  325. /// @brief Checks if two lease objects encapsulate the lease for the same
  326. /// client.
  327. ///
  328. /// This function compares address, type, prefix length, IAID and DUID
  329. /// parameters between two @c Lease6 objects. If these parameters match
  330. /// it is an indication that both objects describe the lease for the same
  331. /// client but apparently one is a result of renewal of the other. The
  332. /// special case of the matching lease is the one that is equal to another.
  333. ///
  334. /// @param other A lease to compare to.
  335. ///
  336. /// @return true if selected parameters of the two leases match.
  337. bool matches(const Lease6& other) const;
  338. /// @brief Compare two leases for equality
  339. ///
  340. /// @param other lease6 object with which to compare
  341. bool operator==(const Lease6& other) const;
  342. /// @brief Compare two leases for inequality
  343. ///
  344. /// @param other lease6 object with which to compare
  345. bool operator!=(const Lease6& other) const {
  346. return (!operator==(other));
  347. }
  348. /// @brief Convert Lease to Printable Form
  349. ///
  350. /// @return String form of the lease
  351. virtual std::string toText() const;
  352. };
  353. /// @brief Pointer to a Lease6 structure.
  354. typedef boost::shared_ptr<Lease6> Lease6Ptr;
  355. /// @brief Pointer to a const Lease6 structure.
  356. typedef boost::shared_ptr<const Lease6> ConstLease6Ptr;
  357. /// @brief A collection of IPv6 leases.
  358. typedef std::vector<Lease6Ptr> Lease6Collection;
  359. /// @brief A non-const iterator for IPv6 leases collection
  360. typedef std::vector<Lease6Ptr>::iterator Lease6CollectionIter;
  361. /// @brief A const iterator for IPv6 leases collection
  362. typedef std::vector<Lease6Ptr>::const_iterator Lease6CollectionConstIter;
  363. /// @brief Stream output operator.
  364. ///
  365. /// Dumps the output of Lease::toText to the given stream.
  366. /// @param os output stream to which the output is
  367. /// @param lease reference to Lease object to dump
  368. /// @return a reference to the output stream paramater
  369. std::ostream&
  370. operator<<(std::ostream& os, const Lease& lease);
  371. }; // end of isc::dhcp namespace
  372. }; // end of isc namespace
  373. #endif // LEASE_H