memfile_lease_storage.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 INMEMORY_LEASE_STORAGE_H
  15. #define INMEMORY_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 A multi index container holding DHCPv6 leases.
  29. ///
  30. /// The leases in the container may be accessed using different indexes:
  31. /// - using an IPv6 address,
  32. /// - using a composite index: DUID, IAID and lease type.
  33. typedef boost::multi_index_container<
  34. // It holds pointers to Lease6 objects.
  35. Lease6Ptr,
  36. boost::multi_index::indexed_by<
  37. // Specification of the first index starts here.
  38. // This index sorts leases by IPv6 addresses represented as
  39. // IOAddress objects.
  40. boost::multi_index::ordered_unique<
  41. boost::multi_index::member<Lease, isc::asiolink::IOAddress, &Lease::addr_>
  42. >,
  43. // Specification of the second index starts here.
  44. boost::multi_index::ordered_non_unique<
  45. // This is a composite index that will be used to search for
  46. // the lease using three attributes: DUID, IAID and lease type.
  47. boost::multi_index::composite_key<
  48. Lease6,
  49. // The DUID can be retrieved from the Lease6 object using
  50. // a getDuidVector const function.
  51. boost::multi_index::const_mem_fun<Lease6, const std::vector<uint8_t>&,
  52. &Lease6::getDuidVector>,
  53. // The two other ingredients of this index are IAID and
  54. // lease type.
  55. boost::multi_index::member<Lease6, uint32_t, &Lease6::iaid_>,
  56. boost::multi_index::member<Lease6, Lease::Type, &Lease6::type_>
  57. >
  58. >
  59. >
  60. > Lease6Storage; // Specify the type name of this container.
  61. /// @brief A multi index container holding DHCPv4 leases.
  62. ///
  63. /// The leases in the container may be accessed using different indexes:
  64. /// - IPv6 address,
  65. /// - composite index: HW address and subnet id,
  66. /// - composite index: client id and subnet id,
  67. /// - composite index: HW address, client id and subnet id
  68. typedef boost::multi_index_container<
  69. // It holds pointers to Lease4 objects.
  70. Lease4Ptr,
  71. // Specification of search indexes starts here.
  72. boost::multi_index::indexed_by<
  73. // Specification of the first index starts here.
  74. // This index sorts leases by IPv4 addresses represented as
  75. // IOAddress objects.
  76. boost::multi_index::ordered_unique<
  77. // The IPv4 address are held in addr_ members that belong to
  78. // Lease class.
  79. boost::multi_index::member<Lease, isc::asiolink::IOAddress, &Lease::addr_>
  80. >,
  81. // Specification of the second index starts here.
  82. boost::multi_index::ordered_non_unique<
  83. // This is a composite index that combines two attributes of the
  84. // Lease4 object: hardware address and subnet id.
  85. boost::multi_index::composite_key<
  86. Lease4,
  87. // The hardware address is held in the hwaddr_ member of the
  88. // Lease4 object, which is a HWAddr object. Boost does not
  89. // provide a key extractor for getting a member of a member,
  90. // so we need a simple method for that.
  91. boost::multi_index::const_mem_fun<Lease, const std::vector<uint8_t>&,
  92. &Lease::getHWAddrVector>,
  93. // The subnet id is held in the subnet_id_ member of Lease4
  94. // class. Note that the subnet_id_ is defined in the base
  95. // class (Lease) so we have to point to this class rather
  96. // than derived class: Lease4.
  97. boost::multi_index::member<Lease, SubnetID, &Lease::subnet_id_>
  98. >
  99. >,
  100. // Specification of the third index starts here.
  101. boost::multi_index::ordered_non_unique<
  102. // This is a composite index that uses two values to search for a
  103. // lease: client id and subnet id.
  104. boost::multi_index::composite_key<
  105. Lease4,
  106. // The client id can be retrieved from the Lease4 object by
  107. // calling getClientIdVector const function.
  108. boost::multi_index::const_mem_fun<Lease4, const std::vector<uint8_t>&,
  109. &Lease4::getClientIdVector>,
  110. // The subnet id is accessed through the subnet_id_ member.
  111. boost::multi_index::member<Lease, uint32_t, &Lease::subnet_id_>
  112. >
  113. >,
  114. // Specification of the fourth index starts here.
  115. boost::multi_index::ordered_non_unique<
  116. // This is a composite index that uses three values to search for a
  117. // lease: client id, HW address and subnet id.
  118. boost::multi_index::composite_key<
  119. Lease4,
  120. // The client id can be retrieved from the Lease4 object by
  121. // calling getClientIdVector const function.
  122. boost::multi_index::const_mem_fun<Lease4, const std::vector<uint8_t>&,
  123. &Lease4::getClientIdVector>,
  124. // The hardware address is held in the hwaddr_ object. We can
  125. // access the raw data using lease->hwaddr_->hwaddr_, but Boost
  126. // doesn't seem to provide a way to use member of a member for this,
  127. // so we need a simple key extractor method (getHWAddrVector).
  128. boost::multi_index::const_mem_fun<Lease, const std::vector<uint8_t>&,
  129. &Lease::getHWAddrVector>,
  130. // The subnet id is accessed through the subnet_id_ member.
  131. boost::multi_index::member<Lease, SubnetID, &Lease::subnet_id_>
  132. >
  133. >
  134. >
  135. > Lease4Storage; // Specify the type name for this container.
  136. } // end of isc::dhcp namespace
  137. } // end of isc namespace
  138. #endif // INMEMORY_LEASE_STORAGE_H