memfile_lease_storage.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // Copyright (C) 2015 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 MEMFILE_LEASE_STORAGE_H
  15. #define MEMFILE_LEASE_STORAGE_H
  16. #include <asiolink/io_address.h>
  17. #include <dhcpsrv/lease.h>
  18. #include <dhcpsrv/subnet_id.h>
  19. #include <boost/multi_index/indexed_by.hpp>
  20. #include <boost/multi_index/member.hpp>
  21. #include <boost/multi_index/mem_fun.hpp>
  22. #include <boost/multi_index/ordered_index.hpp>
  23. #include <boost/multi_index_container.hpp>
  24. #include <boost/multi_index/composite_key.hpp>
  25. #include <vector>
  26. namespace isc {
  27. namespace dhcp {
  28. /// @brief Tag for indexes by address.
  29. struct AddressIndexTag { };
  30. /// @brief Tag for indexes by DUID, IAID, lease type tuple.
  31. struct DuidIaidTypeIndexTag { };
  32. /// @brief Tag for indexes by expiration time.
  33. struct ExpirationIndexTag { };
  34. /// @brief Tag for indexes by HW address, subnet identifier tuple.
  35. struct HWAddressSubnetIdIndexTag { };
  36. /// @brief Tag for indexes by client and subnet identifiers.
  37. struct ClientIdSubnetIdIndexTag { };
  38. /// @brief Tag for indexes by client id, HW address and subnet id.
  39. struct ClientIdHWAddressSubnetIdIndexTag { };
  40. /// @name Multi index containers holding DHCPv4 and DHCPv6 leases.
  41. ///
  42. //@{
  43. /// @brief A multi index container holding DHCPv6 leases.
  44. ///
  45. /// The leases in the container may be accessed using different indexes:
  46. /// - using an IPv6 address,
  47. /// - using a composite index: DUID, IAID and lease type.
  48. /// - using a composite index: boolean flag indicating if the state is
  49. /// "expired-reclaimed" and expiration time.
  50. ///
  51. /// Indexes can be accessed using the index number (from 0 to 2) or a
  52. /// name tag. It is recommended to use the tags to access indexes as
  53. /// they do not depend on the order of indexes in the container.
  54. typedef boost::multi_index_container<
  55. // It holds pointers to Lease6 objects.
  56. Lease6Ptr,
  57. boost::multi_index::indexed_by<
  58. // Specification of the first index starts here.
  59. // This index sorts leases by IPv6 addresses represented as
  60. // IOAddress objects.
  61. boost::multi_index::ordered_unique<
  62. boost::multi_index::tag<AddressIndexTag>,
  63. boost::multi_index::member<Lease, isc::asiolink::IOAddress, &Lease::addr_>
  64. >,
  65. // Specification of the second index starts here.
  66. boost::multi_index::ordered_non_unique<
  67. boost::multi_index::tag<DuidIaidTypeIndexTag>,
  68. // This is a composite index that will be used to search for
  69. // the lease using three attributes: DUID, IAID and lease type.
  70. boost::multi_index::composite_key<
  71. Lease6,
  72. // The DUID can be retrieved from the Lease6 object using
  73. // a getDuidVector const function.
  74. boost::multi_index::const_mem_fun<Lease6, const std::vector<uint8_t>&,
  75. &Lease6::getDuidVector>,
  76. // The two other ingredients of this index are IAID and
  77. // lease type.
  78. boost::multi_index::member<Lease6, uint32_t, &Lease6::iaid_>,
  79. boost::multi_index::member<Lease6, Lease::Type, &Lease6::type_>
  80. >
  81. >,
  82. // Specification of the third index starts here.
  83. boost::multi_index::ordered_non_unique<
  84. boost::multi_index::tag<ExpirationIndexTag>,
  85. // This is a composite index that will be used to search for
  86. // the expired leases. Depending on the value of the first component
  87. // of the search key, the reclaimed or not reclaimed leases will can
  88. // be searched.
  89. boost::multi_index::composite_key<
  90. Lease6,
  91. // The boolean value specifying if lease is reclaimed or not.
  92. boost::multi_index::const_mem_fun<Lease, bool,
  93. &Lease::stateExpiredReclaimed>,
  94. // Lease expiration time.
  95. boost::multi_index::const_mem_fun<Lease, int64_t,
  96. &Lease::getExpirationTime>
  97. >
  98. >
  99. >
  100. > Lease6Storage; // Specify the type name of this container.
  101. /// @brief A multi index container holding DHCPv4 leases.
  102. ///
  103. /// The leases in the container may be accessed using different indexes:
  104. /// - IPv6 address,
  105. /// - composite index: HW address and subnet id,
  106. /// - composite index: client id and subnet id,
  107. /// - composite index: HW address, client id and subnet id
  108. /// - using a composite index: boolean flag indicating if the state is
  109. /// "expired-reclaimed" and expiration time.
  110. ///
  111. /// Indexes can be accessed using the index number (from 0 to 4) or a
  112. /// name tag. It is recommended to use the tags to access indexes as
  113. /// they do not depend on the order of indexes in the container.
  114. typedef boost::multi_index_container<
  115. // It holds pointers to Lease4 objects.
  116. Lease4Ptr,
  117. // Specification of search indexes starts here.
  118. boost::multi_index::indexed_by<
  119. // Specification of the first index starts here.
  120. // This index sorts leases by IPv4 addresses represented as
  121. // IOAddress objects.
  122. boost::multi_index::ordered_unique<
  123. boost::multi_index::tag<AddressIndexTag>,
  124. // The IPv4 address are held in addr_ members that belong to
  125. // Lease class.
  126. boost::multi_index::member<Lease, isc::asiolink::IOAddress, &Lease::addr_>
  127. >,
  128. // Specification of the second index starts here.
  129. boost::multi_index::ordered_non_unique<
  130. boost::multi_index::tag<HWAddressSubnetIdIndexTag>,
  131. // This is a composite index that combines two attributes of the
  132. // Lease4 object: hardware address and subnet id.
  133. boost::multi_index::composite_key<
  134. Lease4,
  135. // The hardware address is held in the hwaddr_ member of the
  136. // Lease4 object, which is a HWAddr object. Boost does not
  137. // provide a key extractor for getting a member of a member,
  138. // so we need a simple method for that.
  139. boost::multi_index::const_mem_fun<Lease, const std::vector<uint8_t>&,
  140. &Lease::getHWAddrVector>,
  141. // The subnet id is held in the subnet_id_ member of Lease4
  142. // class. Note that the subnet_id_ is defined in the base
  143. // class (Lease) so we have to point to this class rather
  144. // than derived class: Lease4.
  145. boost::multi_index::member<Lease, SubnetID, &Lease::subnet_id_>
  146. >
  147. >,
  148. // Specification of the third index starts here.
  149. boost::multi_index::ordered_non_unique<
  150. boost::multi_index::tag<ClientIdSubnetIdIndexTag>,
  151. // This is a composite index that uses two values to search for a
  152. // lease: client id and subnet id.
  153. boost::multi_index::composite_key<
  154. Lease4,
  155. // The client id can be retrieved from the Lease4 object by
  156. // calling getClientIdVector const function.
  157. boost::multi_index::const_mem_fun<Lease4, const std::vector<uint8_t>&,
  158. &Lease4::getClientIdVector>,
  159. // The subnet id is accessed through the subnet_id_ member.
  160. boost::multi_index::member<Lease, uint32_t, &Lease::subnet_id_>
  161. >
  162. >,
  163. // Specification of the fourth index starts here.
  164. boost::multi_index::ordered_non_unique<
  165. boost::multi_index::tag<ClientIdHWAddressSubnetIdIndexTag>,
  166. // This is a composite index that uses three values to search for a
  167. // lease: client id, HW address and subnet id.
  168. boost::multi_index::composite_key<
  169. Lease4,
  170. // The client id can be retrieved from the Lease4 object by
  171. // calling getClientIdVector const function.
  172. boost::multi_index::const_mem_fun<Lease4, const std::vector<uint8_t>&,
  173. &Lease4::getClientIdVector>,
  174. // The hardware address is held in the hwaddr_ object. We can
  175. // access the raw data using lease->hwaddr_->hwaddr_, but Boost
  176. // doesn't seem to provide a way to use member of a member for this,
  177. // so we need a simple key extractor method (getHWAddrVector).
  178. boost::multi_index::const_mem_fun<Lease, const std::vector<uint8_t>&,
  179. &Lease::getHWAddrVector>,
  180. // The subnet id is accessed through the subnet_id_ member.
  181. boost::multi_index::member<Lease, SubnetID, &Lease::subnet_id_>
  182. >
  183. >,
  184. // Specification of the fifth index starts here.
  185. boost::multi_index::ordered_non_unique<
  186. boost::multi_index::tag<ExpirationIndexTag>,
  187. // This is a composite index that will be used to search for
  188. // the expired leases. Depending on the value of the first component
  189. // of the search key, the reclaimed or not reclaimed leases will can
  190. // be searched.
  191. boost::multi_index::composite_key<
  192. Lease4,
  193. // The boolean value specifying if lease is reclaimed or not.
  194. boost::multi_index::const_mem_fun<Lease, bool,
  195. &Lease::stateExpiredReclaimed>,
  196. // Lease expiration time.
  197. boost::multi_index::const_mem_fun<Lease, int64_t,
  198. &Lease::getExpirationTime>
  199. >
  200. >
  201. >
  202. > Lease4Storage; // Specify the type name for this container.
  203. //@}
  204. /// @name Indexes used by the multi index containers
  205. ///
  206. //@{
  207. /// @brief DHCPv6 lease storage index by address.
  208. typedef Lease6Storage::index<AddressIndexTag>::type Lease6StorageAddressIndex;
  209. /// @brief DHCPv6 lease storage index by DUID, IAID, lease type.
  210. typedef Lease6Storage::index<DuidIaidTypeIndexTag>::type Lease6StorageDuidIaidTypeIndex;
  211. /// @brief DHCPv6 lease storage index by expiration time.
  212. typedef Lease6Storage::index<ExpirationIndexTag>::type Lease6StorageExpirationIndex;
  213. /// @brief DHCPv4 lease storage index by address.
  214. typedef Lease4Storage::index<AddressIndexTag>::type Lease4StorageAddressIndex;
  215. /// @brief DHCPv4 lease storage index by exiration time.
  216. typedef Lease4Storage::index<ExpirationIndexTag>::type Lease4StorageExpirationIndex;
  217. /// @brief DHCPv4 lease storage index by HW address and subnet identifier.
  218. typedef Lease4Storage::index<HWAddressSubnetIdIndexTag>::type
  219. Lease4StorageHWAddressSubnetIdIndex;
  220. /// @brief DHCPv4 lease storage index by client and subnet identifier.
  221. typedef Lease4Storage::index<ClientIdSubnetIdIndexTag>::type
  222. Lease4StorageClientIdSubnetIdIndex;
  223. /// @brief DHCPv4 lease storage index by client id, HW address and subnet id.
  224. typedef Lease4Storage::index<ClientIdHWAddressSubnetIdIndexTag>::type
  225. Lease4StorageClientIdHWAddressSubnetIdIndex;
  226. //@}
  227. } // end of isc::dhcp namespace
  228. } // end of isc namespace
  229. #endif // MEMFILE_LEASE_STORAGE_H