lease.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 Assignment operator.
  182. ///
  183. /// @param other the @c Lease4 object to be assigned.
  184. Lease4& operator=(const Lease4& other);
  185. /// @brief Compare two leases for equality
  186. ///
  187. /// @param other lease6 object with which to compare
  188. bool operator==(const Lease4& other) const;
  189. /// @brief Compare two leases for inequality
  190. ///
  191. /// @param other lease6 object with which to compare
  192. bool operator!=(const Lease4& other) const {
  193. return (!operator==(other));
  194. }
  195. /// @brief Convert lease to printable form
  196. ///
  197. /// @return Textual represenation of lease data
  198. virtual std::string toText() const;
  199. /// @todo: Add DHCPv4 failover related fields here
  200. };
  201. /// @brief Pointer to a Lease4 structure.
  202. typedef boost::shared_ptr<Lease4> Lease4Ptr;
  203. /// @brief A collection of IPv4 leases.
  204. typedef std::vector<Lease4Ptr> Lease4Collection;
  205. /// @brief Structure that holds a lease for IPv6 address and/or prefix
  206. ///
  207. /// For performance reasons it is a simple structure, not a class. If we chose
  208. /// make it a class, all fields would have to made private and getters/setters
  209. /// would be required. As this is a critical part of the code that will be used
  210. /// extensively, direct access is warranted.
  211. struct Lease6 : public Lease {
  212. /// @brief Lease type
  213. ///
  214. /// One of normal address, temporary address, or prefix.
  215. Type type_;
  216. /// @brief IPv6 prefix length
  217. ///
  218. /// This is used only for prefix delegations and is ignored otherwise.
  219. uint8_t prefixlen_;
  220. /// @brief Identity Association Identifier (IAID)
  221. ///
  222. /// DHCPv6 stores all addresses and prefixes in IA containers (IA_NA,
  223. /// IA_TA, IA_PD). All containers may appear more than once in a message.
  224. /// To differentiate between them, the IAID field is present
  225. uint32_t iaid_;
  226. /// @brief Client identifier
  227. DuidPtr duid_;
  228. /// @brief preferred lifetime
  229. ///
  230. /// This parameter specifies the preferred lifetime since the lease was
  231. /// assigned or renewed (cltt), expressed in seconds.
  232. uint32_t preferred_lft_;
  233. /// @todo: Add DHCPv6 failover related fields here
  234. /// @brief Constructor
  235. /// @param type Lease type.
  236. /// @param addr Assigned address.
  237. /// @param duid A pointer to an object representing DUID.
  238. /// @param iaid IAID.
  239. /// @param preferred Preferred lifetime.
  240. /// @param valid Valid lifetime.
  241. /// @param t1 A value of the T1 timer.
  242. /// @param t2 A value of the T2 timer.
  243. /// @param subnet_id A Subnet identifier.
  244. /// @param prefixlen An address prefix length.
  245. Lease6(Type type, const isc::asiolink::IOAddress& addr, DuidPtr duid,
  246. uint32_t iaid, uint32_t preferred, uint32_t valid, uint32_t t1,
  247. uint32_t t2, SubnetID subnet_id, uint8_t prefixlen = 128);
  248. /// @brief Constructor, including FQDN data.
  249. ///
  250. /// @param type Lease type.
  251. /// @param addr Assigned address.
  252. /// @param duid A pointer to an object representing DUID.
  253. /// @param iaid IAID.
  254. /// @param preferred Preferred lifetime.
  255. /// @param valid Valid lifetime.
  256. /// @param t1 A value of the T1 timer.
  257. /// @param t2 A value of the T2 timer.
  258. /// @param subnet_id A Subnet identifier.
  259. /// @param fqdn_fwd If true, forward DNS update is performed for a lease.
  260. /// @param fqdn_rev If true, reverse DNS update is performed for a lease.
  261. /// @param hostname FQDN of the client which gets the lease.
  262. /// @param prefixlen An address prefix length.
  263. Lease6(Type type, const isc::asiolink::IOAddress& addr, DuidPtr duid,
  264. uint32_t iaid, uint32_t preferred, uint32_t valid, uint32_t t1,
  265. uint32_t t2, SubnetID subnet_id, const bool fqdn_fwd,
  266. const bool fqdn_rev, const std::string& hostname,
  267. uint8_t prefixlen = 0);
  268. /// @brief Constructor
  269. ///
  270. /// Initialize fields that don't have a default constructor.
  271. Lease6() : Lease(isc::asiolink::IOAddress("::"), 0, 0, 0, 0, 0,
  272. false, false, ""),
  273. type_(TYPE_NA) {
  274. }
  275. /// @brief Compare two leases for equality
  276. ///
  277. /// @param other lease6 object with which to compare
  278. bool operator==(const Lease6& other) const;
  279. /// @brief Compare two leases for inequality
  280. ///
  281. /// @param other lease6 object with which to compare
  282. bool operator!=(const Lease6& other) const {
  283. return (!operator==(other));
  284. }
  285. /// @brief Convert Lease to Printable Form
  286. ///
  287. /// @return String form of the lease
  288. virtual std::string toText() const;
  289. };
  290. /// @brief Pointer to a Lease6 structure.
  291. typedef boost::shared_ptr<Lease6> Lease6Ptr;
  292. /// @brief Pointer to a const Lease6 structure.
  293. typedef boost::shared_ptr<const Lease6> ConstLease6Ptr;
  294. /// @brief A collection of IPv6 leases.
  295. typedef std::vector<Lease6Ptr> Lease6Collection;
  296. }; // end of isc::dhcp namespace
  297. }; // end of isc namespace
  298. #endif // LEASE_H