memfile_lease_storage.h 11 KB

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