memfile_lease_storage.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // Copyright (C) 2015-2017 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. /// @brief Tag for indexs by subnet-id.
  33. struct SubnetIdIndexTag { };
  34. /// @name Multi index containers holding DHCPv4 and DHCPv6 leases.
  35. ///
  36. //@{
  37. /// @brief A multi index container holding DHCPv6 leases.
  38. ///
  39. /// The leases in the container may be accessed using different indexes:
  40. /// - using an IPv6 address,
  41. /// - using a composite index: DUID, IAID and lease type.
  42. /// - using a composite index: boolean flag indicating if the state is
  43. /// "expired-reclaimed" and expiration time.
  44. ///
  45. /// Indexes can be accessed using the index number (from 0 to 2) or a
  46. /// name tag. It is recommended to use the tags to access indexes as
  47. /// they do not depend on the order of indexes in the container.
  48. typedef boost::multi_index_container<
  49. // It holds pointers to Lease6 objects.
  50. Lease6Ptr,
  51. boost::multi_index::indexed_by<
  52. // Specification of the first index starts here.
  53. // This index sorts leases by IPv6 addresses represented as
  54. // IOAddress objects.
  55. boost::multi_index::ordered_unique<
  56. boost::multi_index::tag<AddressIndexTag>,
  57. boost::multi_index::member<Lease, isc::asiolink::IOAddress, &Lease::addr_>
  58. >,
  59. // Specification of the second index starts here.
  60. boost::multi_index::ordered_non_unique<
  61. boost::multi_index::tag<DuidIaidTypeIndexTag>,
  62. // This is a composite index that will be used to search for
  63. // the lease using three attributes: DUID, IAID and lease type.
  64. boost::multi_index::composite_key<
  65. Lease6,
  66. // The DUID can be retrieved from the Lease6 object using
  67. // a getDuidVector const function.
  68. boost::multi_index::const_mem_fun<Lease6, const std::vector<uint8_t>&,
  69. &Lease6::getDuidVector>,
  70. // The two other ingredients of this index are IAID and
  71. // lease type.
  72. boost::multi_index::member<Lease6, uint32_t, &Lease6::iaid_>,
  73. boost::multi_index::member<Lease6, Lease::Type, &Lease6::type_>
  74. >
  75. >,
  76. // Specification of the third index starts here.
  77. boost::multi_index::ordered_non_unique<
  78. boost::multi_index::tag<ExpirationIndexTag>,
  79. // This is a composite index that will be used to search for
  80. // the expired leases. Depending on the value of the first component
  81. // of the search key, the reclaimed or not reclaimed leases will can
  82. // be searched.
  83. boost::multi_index::composite_key<
  84. Lease6,
  85. // The boolean value specifying if lease is reclaimed or not.
  86. boost::multi_index::const_mem_fun<Lease, bool,
  87. &Lease::stateExpiredReclaimed>,
  88. // Lease expiration time.
  89. boost::multi_index::const_mem_fun<Lease, int64_t,
  90. &Lease::getExpirationTime>
  91. >
  92. >,
  93. // Specification of the fourth index starts here.
  94. // This index sorts leases by SubnetID.
  95. boost::multi_index::ordered_non_unique<
  96. boost::multi_index::tag<SubnetIdIndexTag>,
  97. boost::multi_index::member<Lease, isc::dhcp::SubnetID, &Lease::subnet_id_>
  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. // Specification of the sixth index starts here.
  202. // This index sorts leases by SubnetID.
  203. boost::multi_index::ordered_non_unique<
  204. boost::multi_index::tag<SubnetIdIndexTag>,
  205. boost::multi_index::member<Lease, isc::dhcp::SubnetID, &Lease::subnet_id_>
  206. >
  207. >
  208. > Lease4Storage; // Specify the type name for this container.
  209. //@}
  210. /// @name Indexes used by the multi index containers
  211. ///
  212. //@{
  213. /// @brief DHCPv6 lease storage index by address.
  214. typedef Lease6Storage::index<AddressIndexTag>::type Lease6StorageAddressIndex;
  215. /// @brief DHCPv6 lease storage index by DUID, IAID, lease type.
  216. typedef Lease6Storage::index<DuidIaidTypeIndexTag>::type Lease6StorageDuidIaidTypeIndex;
  217. /// @brief DHCPv6 lease storage index by expiration time.
  218. typedef Lease6Storage::index<ExpirationIndexTag>::type Lease6StorageExpirationIndex;
  219. /// @brief DHCPv6 lease storage index by Subnet-id.
  220. typedef Lease6Storage::index<SubnetIdIndexTag>::type Lease6StorageSubnetIdIndex;
  221. /// @brief DHCPv4 lease storage index by address.
  222. typedef Lease4Storage::index<AddressIndexTag>::type Lease4StorageAddressIndex;
  223. /// @brief DHCPv4 lease storage index by expiration time.
  224. typedef Lease4Storage::index<ExpirationIndexTag>::type Lease4StorageExpirationIndex;
  225. /// @brief DHCPv4 lease storage index by HW address and subnet identifier.
  226. typedef Lease4Storage::index<HWAddressSubnetIdIndexTag>::type
  227. Lease4StorageHWAddressSubnetIdIndex;
  228. /// @brief DHCPv4 lease storage index by client and subnet identifier.
  229. typedef Lease4Storage::index<ClientIdSubnetIdIndexTag>::type
  230. Lease4StorageClientIdSubnetIdIndex;
  231. /// @brief DHCPv4 lease storage index by client id, HW address and subnet id.
  232. typedef Lease4Storage::index<ClientIdHWAddressSubnetIdIndexTag>::type
  233. Lease4StorageClientIdHWAddressSubnetIdIndex;
  234. /// @brief DHCPv4 lease storage index by client id, HW address and subnet id.
  235. typedef Lease4Storage::index<SubnetIdIndexTag>::type Lease4StorageSubnetIdIndex;
  236. //@}
  237. } // end of isc::dhcp namespace
  238. } // end of isc namespace
  239. #endif // MEMFILE_LEASE_STORAGE_H