lease.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. Lease(const isc::asiolink::IOAddress& addr, uint32_t t1, uint32_t t2,
  55. uint32_t valid_lft, SubnetID subnet_id, time_t cltt,
  56. const bool fqdn_fwd, const bool fqdn_rev,
  57. const std::string& hostname);
  58. /// @brief Destructor
  59. virtual ~Lease() {}
  60. /// @brief IPv4 ot IPv6 address
  61. ///
  62. /// IPv4, IPv6 address or, in the case of a prefix delegation, the prefix.
  63. isc::asiolink::IOAddress addr_;
  64. /// @brief Renewal timer
  65. ///
  66. /// Specifies renewal time. Although technically it is a property of the
  67. /// IA container and not the address itself, since our data model does not
  68. /// define a separate IA entity, we are keeping it in the lease. In the
  69. /// case of multiple addresses/prefixes for the same IA, each must have
  70. /// consistent T1 and T2 values. This is specified in seconds since cltt.
  71. uint32_t t1_;
  72. /// @brief Rebinding timer
  73. ///
  74. /// Specifies rebinding time. Although technically it is a property of the
  75. /// IA container and not the address itself, since our data model does not
  76. /// define a separate IA entity, we are keeping it in the lease. In the
  77. /// case of multiple addresses/prefixes for the same IA, each must have
  78. /// consistent T1 and T2 values. This is specified in seconds since cltt.
  79. uint32_t t2_;
  80. /// @brief Valid lifetime
  81. ///
  82. /// Expressed as number of seconds since cltt.
  83. uint32_t valid_lft_;
  84. /// @brief Client last transmission time
  85. ///
  86. /// Specifies a timestamp giving the time when the last transmission from a
  87. /// client was received.
  88. time_t cltt_;
  89. /// @brief Subnet identifier
  90. ///
  91. /// Specifies the identification of the subnet to which the lease belongs.
  92. SubnetID subnet_id_;
  93. /// @brief Fixed lease?
  94. ///
  95. /// Fixed leases are kept after they are released/expired.
  96. bool fixed_;
  97. /// @brief Client hostname
  98. ///
  99. /// This field may be empty
  100. std::string hostname_;
  101. /// @brief Forward zone updated?
  102. ///
  103. /// Set true if the DNS AAAA record for this lease has been updated.
  104. bool fqdn_fwd_;
  105. /// @brief Reverse zone updated?
  106. ///
  107. /// Set true if the DNS PTR record for this lease has been updated.
  108. bool fqdn_rev_;
  109. /// @brief Lease comments
  110. ///
  111. /// Currently not used. It may be used for keeping comments made by the
  112. /// system administrator.
  113. std::string comments_;
  114. /// @brief Convert Lease to Printable Form
  115. ///
  116. /// @return String form of the lease
  117. virtual std::string toText() const = 0;
  118. /// @brief returns true if the lease is expired
  119. /// @return true if the lease is expired
  120. bool expired() const;
  121. /// @brief Returns true if the other lease has equal FQDN data.
  122. ///
  123. /// @param other Lease which FQDN data is to be compared with our lease.
  124. ///
  125. /// @return Boolean value which indicates whether FQDN data of the other
  126. /// lease is equal to the FQDN data of our lease (true) or not (false).
  127. bool hasIdenticalFqdn(const Lease& other) const;
  128. };
  129. /// @brief Structure that holds a lease for IPv4 address
  130. ///
  131. /// For performance reasons it is a simple structure, not a class. If we chose
  132. /// make it a class, all fields would have to made private and getters/setters
  133. /// would be required. As this is a critical part of the code that will be used
  134. /// extensively, direct access is warranted.
  135. struct Lease4 : public Lease {
  136. /// @brief Address extension
  137. ///
  138. /// It is envisaged that in some cases IPv4 address will be accompanied
  139. /// with some additional data. One example of such use are Address + Port
  140. /// solutions (or Port-restricted Addresses), where several clients may get
  141. /// the same address, but different port ranges. This feature is not
  142. /// expected to be widely used. Under normal circumstances, the value
  143. /// should be 0.
  144. uint32_t ext_;
  145. /// @brief Hardware address
  146. std::vector<uint8_t> hwaddr_;
  147. /// @brief Client identifier
  148. ///
  149. /// @todo Should this be a pointer to a client ID or the ID itself?
  150. /// Compare with the DUID in the Lease6 structure.
  151. ClientIdPtr client_id_;
  152. /// @brief Constructor
  153. ///
  154. /// @param addr IPv4 address.
  155. /// @param hwaddr Hardware address buffer
  156. /// @param hwaddr_len Length of hardware address buffer
  157. /// @param clientid Client identification buffer
  158. /// @param clientid_len Length of client identification buffer
  159. /// @param valid_lft Lifetime of the lease
  160. /// @param t1 renewal time
  161. /// @param t2 rebinding time
  162. /// @param cltt Client last transmission time
  163. /// @param subnet_id Subnet identification
  164. /// @param fqdn_fwd If true, forward DNS update is performed for a lease.
  165. /// @param fqdn_rev If true, reverse DNS update is performed for a lease.
  166. /// @param hostname FQDN of the client which gets the lease.
  167. Lease4(const isc::asiolink::IOAddress& addr, const uint8_t* hwaddr, size_t hwaddr_len,
  168. const uint8_t* clientid, size_t clientid_len, uint32_t valid_lft,
  169. uint32_t t1, uint32_t t2, time_t cltt, uint32_t subnet_id,
  170. const bool fqdn_fwd = false, const bool fqdn_rev = false,
  171. const std::string& hostname = "")
  172. : Lease(addr, t1, t2, valid_lft, subnet_id, cltt, fqdn_fwd, fqdn_rev,
  173. hostname),
  174. ext_(0), hwaddr_(hwaddr, hwaddr + hwaddr_len) {
  175. if (clientid_len) {
  176. client_id_.reset(new ClientId(clientid, clientid_len));
  177. }
  178. }
  179. /// @brief Default constructor
  180. ///
  181. /// Initialize fields that don't have a default constructor.
  182. Lease4() : Lease(0, 0, 0, 0, 0, 0, false, false, "") {
  183. }
  184. /// @brief Copy constructor
  185. ///
  186. /// @param other the @c Lease4 object to be copied.
  187. Lease4(const Lease4& other);
  188. /// @brief Returns a client identifier.
  189. ///
  190. /// @warning Since the function returns the reference to a vector (not a
  191. /// copy), the returned object should be used with caution because it will
  192. /// remain valid only for the period of time when an object which returned
  193. /// it exists.
  194. ///
  195. /// @return A reference to a vector holding client identifier,
  196. /// or an empty vector if client identifier is NULL.
  197. const std::vector<uint8_t>& getClientIdVector() const;
  198. /// @brief Check if two objects encapsulate the lease for the same
  199. /// client.
  200. ///
  201. /// Checks if two @c Lease4 objects have the same address, client id,
  202. /// HW address and ext_ value. If these parameters match it is an
  203. /// indication that both objects describe the lease for the same
  204. /// client but apparently one is a result of renewal of the other. The
  205. /// special case of the matching lease is the one that is equal to another.
  206. ///
  207. /// @param other A lease to compare with.
  208. ///
  209. /// @return true if the selected parameters of the two leases match.
  210. bool matches(const Lease4& other) const;
  211. /// @brief Assignment operator.
  212. ///
  213. /// @param other the @c Lease4 object to be assigned.
  214. Lease4& operator=(const Lease4& other);
  215. /// @brief Compare two leases for equality
  216. ///
  217. /// @param other lease6 object with which to compare
  218. bool operator==(const Lease4& other) const;
  219. /// @brief Compare two leases for inequality
  220. ///
  221. /// @param other lease6 object with which to compare
  222. bool operator!=(const Lease4& other) const {
  223. return (!operator==(other));
  224. }
  225. /// @brief Convert lease to printable form
  226. ///
  227. /// @return Textual represenation of lease data
  228. virtual std::string toText() const;
  229. /// @todo: Add DHCPv4 failover related fields here
  230. };
  231. /// @brief Pointer to a Lease4 structure.
  232. typedef boost::shared_ptr<Lease4> Lease4Ptr;
  233. /// @brief A collection of IPv4 leases.
  234. typedef std::vector<Lease4Ptr> Lease4Collection;
  235. /// @brief Structure that holds a lease for IPv6 address and/or prefix
  236. ///
  237. /// For performance reasons it is a simple structure, not a class. If we chose
  238. /// make it a class, all fields would have to made private and getters/setters
  239. /// would be required. As this is a critical part of the code that will be used
  240. /// extensively, direct access is warranted.
  241. struct Lease6 : public Lease {
  242. /// @brief Lease type
  243. ///
  244. /// One of normal address, temporary address, or prefix.
  245. Type type_;
  246. /// @brief IPv6 prefix length
  247. ///
  248. /// This is used only for prefix delegations and is ignored otherwise.
  249. uint8_t prefixlen_;
  250. /// @brief Identity Association Identifier (IAID)
  251. ///
  252. /// DHCPv6 stores all addresses and prefixes in IA containers (IA_NA,
  253. /// IA_TA, IA_PD). All containers may appear more than once in a message.
  254. /// To differentiate between them, the IAID field is present
  255. uint32_t iaid_;
  256. /// @brief Client identifier
  257. DuidPtr duid_;
  258. /// @brief preferred lifetime
  259. ///
  260. /// This parameter specifies the preferred lifetime since the lease was
  261. /// assigned or renewed (cltt), expressed in seconds.
  262. uint32_t preferred_lft_;
  263. /// @todo: Add DHCPv6 failover related fields here
  264. /// @brief Constructor
  265. /// @param type Lease type.
  266. /// @param addr Assigned address.
  267. /// @param duid A pointer to an object representing DUID.
  268. /// @param iaid IAID.
  269. /// @param preferred Preferred lifetime.
  270. /// @param valid Valid lifetime.
  271. /// @param t1 A value of the T1 timer.
  272. /// @param t2 A value of the T2 timer.
  273. /// @param subnet_id A Subnet identifier.
  274. /// @param prefixlen An address prefix length.
  275. Lease6(Type type, const isc::asiolink::IOAddress& addr, DuidPtr duid,
  276. uint32_t iaid, uint32_t preferred, uint32_t valid, uint32_t t1,
  277. uint32_t t2, SubnetID subnet_id, uint8_t prefixlen = 128);
  278. /// @brief Constructor, including FQDN data.
  279. ///
  280. /// @param type Lease type.
  281. /// @param addr Assigned address.
  282. /// @param duid A pointer to an object representing DUID.
  283. /// @param iaid IAID.
  284. /// @param preferred Preferred lifetime.
  285. /// @param valid Valid lifetime.
  286. /// @param t1 A value of the T1 timer.
  287. /// @param t2 A value of the T2 timer.
  288. /// @param subnet_id A Subnet identifier.
  289. /// @param fqdn_fwd If true, forward DNS update is performed for a lease.
  290. /// @param fqdn_rev If true, reverse DNS update is performed for a lease.
  291. /// @param hostname FQDN of the client which gets the lease.
  292. /// @param prefixlen An address prefix length.
  293. Lease6(Type type, const isc::asiolink::IOAddress& addr, DuidPtr duid,
  294. uint32_t iaid, uint32_t preferred, uint32_t valid, uint32_t t1,
  295. uint32_t t2, SubnetID subnet_id, const bool fqdn_fwd,
  296. const bool fqdn_rev, const std::string& hostname,
  297. uint8_t prefixlen = 0);
  298. /// @brief Constructor
  299. ///
  300. /// Initialize fields that don't have a default constructor.
  301. Lease6() : Lease(isc::asiolink::IOAddress("::"), 0, 0, 0, 0, 0,
  302. false, false, ""),
  303. type_(TYPE_NA) {
  304. }
  305. /// @brief Returns a reference to a vector representing a DUID.
  306. ///
  307. /// @warning Since the function returns the reference to a vector (not a
  308. /// copy), the returned object should be used with caution because it will
  309. /// remain valid only for the period of time when an object which returned
  310. /// it exists.
  311. ///
  312. /// @return A reference to a vector holding a DUID.
  313. const std::vector<uint8_t>& getDuidVector() const;
  314. /// @brief Checks if two lease objects encapsulate the lease for the same
  315. /// client.
  316. ///
  317. /// This function compares address, type, prefix length, IAID and DUID
  318. /// parameters between two @c Lease6 objects. If these parameters match
  319. /// it is an indication that both objects describe the lease for the same
  320. /// client but apparently one is a result of renewal of the other. The
  321. /// special case of the matching lease is the one that is equal to another.
  322. ///
  323. /// @param other A lease to compare to.
  324. ///
  325. /// @return true if selected parameters of the two leases match.
  326. bool matches(const Lease6& other) const;
  327. /// @brief Compare two leases for equality
  328. ///
  329. /// @param other lease6 object with which to compare
  330. bool operator==(const Lease6& other) const;
  331. /// @brief Compare two leases for inequality
  332. ///
  333. /// @param other lease6 object with which to compare
  334. bool operator!=(const Lease6& other) const {
  335. return (!operator==(other));
  336. }
  337. /// @brief Convert Lease to Printable Form
  338. ///
  339. /// @return String form of the lease
  340. virtual std::string toText() const;
  341. };
  342. /// @brief Pointer to a Lease6 structure.
  343. typedef boost::shared_ptr<Lease6> Lease6Ptr;
  344. /// @brief Pointer to a const Lease6 structure.
  345. typedef boost::shared_ptr<const Lease6> ConstLease6Ptr;
  346. /// @brief A collection of IPv6 leases.
  347. typedef std::vector<Lease6Ptr> Lease6Collection;
  348. }; // end of isc::dhcp namespace
  349. }; // end of isc namespace
  350. #endif // LEASE_H