lease.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. // Copyright (C) 2013 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. };
  122. /// @brief Structure that holds a lease for IPv4 address
  123. ///
  124. /// For performance reasons it is a simple structure, not a class. If we chose
  125. /// make it a class, all fields would have to made private and getters/setters
  126. /// would be required. As this is a critical part of the code that will be used
  127. /// extensively, direct access is warranted.
  128. struct Lease4 : public Lease {
  129. /// @brief Address extension
  130. ///
  131. /// It is envisaged that in some cases IPv4 address will be accompanied
  132. /// with some additional data. One example of such use are Address + Port
  133. /// solutions (or Port-restricted Addresses), where several clients may get
  134. /// the same address, but different port ranges. This feature is not
  135. /// expected to be widely used. Under normal circumstances, the value
  136. /// should be 0.
  137. uint32_t ext_;
  138. /// @brief Hardware address
  139. std::vector<uint8_t> hwaddr_;
  140. /// @brief Client identifier
  141. ///
  142. /// @todo Should this be a pointer to a client ID or the ID itself?
  143. /// Compare with the DUID in the Lease6 structure.
  144. ClientIdPtr client_id_;
  145. /// @brief Constructor
  146. ///
  147. /// @param addr IPv4 address.
  148. /// @param hwaddr Hardware address buffer
  149. /// @param hwaddr_len Length of hardware address buffer
  150. /// @param clientid Client identification buffer
  151. /// @param clientid_len Length of client identification buffer
  152. /// @param valid_lft Lifetime of the lease
  153. /// @param t1 renewal time
  154. /// @param t2 rebinding time
  155. /// @param cltt Client last transmission time
  156. /// @param subnet_id Subnet identification
  157. /// @param fqdn_fwd If true, forward DNS update is performed for a lease.
  158. /// @param fqdn_rev If true, reverse DNS update is performed for a lease.
  159. /// @param hostname FQDN of the client which gets the lease.
  160. Lease4(const isc::asiolink::IOAddress& addr, const uint8_t* hwaddr, size_t hwaddr_len,
  161. const uint8_t* clientid, size_t clientid_len, uint32_t valid_lft,
  162. uint32_t t1, uint32_t t2, time_t cltt, uint32_t subnet_id,
  163. const bool fqdn_fwd = false, const bool fqdn_rev = false,
  164. const std::string& hostname = "")
  165. : Lease(addr, t1, t2, valid_lft, subnet_id, cltt, fqdn_fwd, fqdn_rev,
  166. hostname),
  167. ext_(0), hwaddr_(hwaddr, hwaddr + hwaddr_len) {
  168. if (clientid_len) {
  169. client_id_.reset(new ClientId(clientid, clientid_len));
  170. }
  171. }
  172. /// @brief Default constructor
  173. ///
  174. /// Initialize fields that don't have a default constructor.
  175. Lease4() : Lease(0, 0, 0, 0, 0, 0, false, false, "") {
  176. }
  177. /// @brief Copy constructor
  178. ///
  179. /// @param other the @c Lease4 object to be copied.
  180. Lease4(const Lease4& other);
  181. /// @brief Returns a client identifier.
  182. ///
  183. /// @warning Since the function returns the reference to a vector (not a
  184. /// copy), the returned object should be used with caution because it will
  185. /// remain valid only for the period of time when an object which returned
  186. /// it exists.
  187. ///
  188. /// @return A reference to a vector holding client identifier,
  189. /// or an empty vector if client identifier is NULL.
  190. const std::vector<uint8_t>& getClientIdVector() const;
  191. /// @brief Check if two objects encapsulate the lease for the same
  192. /// client.
  193. ///
  194. /// Checks if two @c Lease4 objects have the same address, client id,
  195. /// HW address and ext_ value. If these parameters match it is an
  196. /// indication that both objects describe the lease for the same
  197. /// client but apparently one is a result of renewal of the other. The
  198. /// special case of the matching lease is the one that is equal to another.
  199. ///
  200. /// @param other A lease to compare with.
  201. ///
  202. /// @return true if the selected parameters of the two leases match.
  203. bool matches(const Lease4& other) const;
  204. /// @brief Assignment operator.
  205. ///
  206. /// @param other the @c Lease4 object to be assigned.
  207. Lease4& operator=(const Lease4& other);
  208. /// @brief Compare two leases for equality
  209. ///
  210. /// @param other lease6 object with which to compare
  211. bool operator==(const Lease4& other) const;
  212. /// @brief Compare two leases for inequality
  213. ///
  214. /// @param other lease6 object with which to compare
  215. bool operator!=(const Lease4& other) const {
  216. return (!operator==(other));
  217. }
  218. /// @brief Convert lease to printable form
  219. ///
  220. /// @return Textual represenation of lease data
  221. virtual std::string toText() const;
  222. /// @todo: Add DHCPv4 failover related fields here
  223. };
  224. /// @brief Pointer to a Lease4 structure.
  225. typedef boost::shared_ptr<Lease4> Lease4Ptr;
  226. /// @brief A collection of IPv4 leases.
  227. typedef std::vector<Lease4Ptr> Lease4Collection;
  228. /// @brief Structure that holds a lease for IPv6 address and/or prefix
  229. ///
  230. /// For performance reasons it is a simple structure, not a class. If we chose
  231. /// make it a class, all fields would have to made private and getters/setters
  232. /// would be required. As this is a critical part of the code that will be used
  233. /// extensively, direct access is warranted.
  234. struct Lease6 : public Lease {
  235. /// @brief Lease type
  236. ///
  237. /// One of normal address, temporary address, or prefix.
  238. Type type_;
  239. /// @brief IPv6 prefix length
  240. ///
  241. /// This is used only for prefix delegations and is ignored otherwise.
  242. uint8_t prefixlen_;
  243. /// @brief Identity Association Identifier (IAID)
  244. ///
  245. /// DHCPv6 stores all addresses and prefixes in IA containers (IA_NA,
  246. /// IA_TA, IA_PD). All containers may appear more than once in a message.
  247. /// To differentiate between them, the IAID field is present
  248. uint32_t iaid_;
  249. /// @brief Client identifier
  250. DuidPtr duid_;
  251. /// @brief preferred lifetime
  252. ///
  253. /// This parameter specifies the preferred lifetime since the lease was
  254. /// assigned or renewed (cltt), expressed in seconds.
  255. uint32_t preferred_lft_;
  256. /// @todo: Add DHCPv6 failover related fields here
  257. /// @brief Constructor
  258. /// @param type Lease type.
  259. /// @param addr Assigned address.
  260. /// @param duid A pointer to an object representing DUID.
  261. /// @param iaid IAID.
  262. /// @param preferred Preferred lifetime.
  263. /// @param valid Valid lifetime.
  264. /// @param t1 A value of the T1 timer.
  265. /// @param t2 A value of the T2 timer.
  266. /// @param subnet_id A Subnet identifier.
  267. /// @param prefixlen An address prefix length.
  268. Lease6(Type type, const isc::asiolink::IOAddress& addr, DuidPtr duid,
  269. uint32_t iaid, uint32_t preferred, uint32_t valid, uint32_t t1,
  270. uint32_t t2, SubnetID subnet_id, uint8_t prefixlen = 128);
  271. /// @brief Constructor, including FQDN data.
  272. ///
  273. /// @param type Lease type.
  274. /// @param addr Assigned address.
  275. /// @param duid A pointer to an object representing DUID.
  276. /// @param iaid IAID.
  277. /// @param preferred Preferred lifetime.
  278. /// @param valid Valid lifetime.
  279. /// @param t1 A value of the T1 timer.
  280. /// @param t2 A value of the T2 timer.
  281. /// @param subnet_id A Subnet identifier.
  282. /// @param fqdn_fwd If true, forward DNS update is performed for a lease.
  283. /// @param fqdn_rev If true, reverse DNS update is performed for a lease.
  284. /// @param hostname FQDN of the client which gets the lease.
  285. /// @param prefixlen An address prefix length.
  286. Lease6(Type type, const isc::asiolink::IOAddress& addr, DuidPtr duid,
  287. uint32_t iaid, uint32_t preferred, uint32_t valid, uint32_t t1,
  288. uint32_t t2, SubnetID subnet_id, const bool fqdn_fwd,
  289. const bool fqdn_rev, const std::string& hostname,
  290. uint8_t prefixlen = 0);
  291. /// @brief Constructor
  292. ///
  293. /// Initialize fields that don't have a default constructor.
  294. Lease6() : Lease(isc::asiolink::IOAddress("::"), 0, 0, 0, 0, 0,
  295. false, false, ""),
  296. type_(TYPE_NA) {
  297. }
  298. /// @brief Returns a reference to a vector representing a DUID.
  299. ///
  300. /// @warning Since the function returns the reference to a vector (not a
  301. /// copy), the returned object should be used with caution because it will
  302. /// remain valid only for the period of time when an object which returned
  303. /// it exists.
  304. ///
  305. /// @return A reference to a vector holding a DUID.
  306. const std::vector<uint8_t>& getDuidVector() const;
  307. /// @brief Checks if two lease objects encapsulate the lease for the same
  308. /// client.
  309. ///
  310. /// This function compares address, type, prefix length, IAID and DUID
  311. /// parameters between two @c Lease6 objects. If these parameters match
  312. /// it is an indication that both objects describe the lease for the same
  313. /// client but apparently one is a result of renewal of the other. The
  314. /// special case of the matching lease is the one that is equal to another.
  315. ///
  316. /// @param other A lease to compare to.
  317. ///
  318. /// @return true if selected parameters of the two leases match.
  319. bool matches(const Lease6& other) const;
  320. /// @brief Compare two leases for equality
  321. ///
  322. /// @param other lease6 object with which to compare
  323. bool operator==(const Lease6& other) const;
  324. /// @brief Compare two leases for inequality
  325. ///
  326. /// @param other lease6 object with which to compare
  327. bool operator!=(const Lease6& other) const {
  328. return (!operator==(other));
  329. }
  330. /// @brief Convert Lease to Printable Form
  331. ///
  332. /// @return String form of the lease
  333. virtual std::string toText() const;
  334. };
  335. /// @brief Pointer to a Lease6 structure.
  336. typedef boost::shared_ptr<Lease6> Lease6Ptr;
  337. /// @brief Pointer to a const Lease6 structure.
  338. typedef boost::shared_ptr<const Lease6> ConstLease6Ptr;
  339. /// @brief A collection of IPv6 leases.
  340. typedef std::vector<Lease6Ptr> Lease6Collection;
  341. }; // end of isc::dhcp namespace
  342. }; // end of isc namespace
  343. #endif // LEASE_H