memfile_lease_mgr.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // Copyright (C) 2012 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_MGR_H
  15. #define MEMFILE_LEASE_MGR_H
  16. #include <dhcpsrv/hwaddr.h>
  17. #include <dhcpsrv/lease_mgr.h>
  18. #include <boost/multi_index/indexed_by.hpp>
  19. #include <boost/multi_index/member.hpp>
  20. #include <boost/multi_index/ordered_index.hpp>
  21. #include <boost/multi_index_container.hpp>
  22. namespace isc {
  23. namespace dhcp {
  24. // This is a concrete implementation of a Lease database.
  25. //
  26. // It is for testing purposes only. It is NOT a production code.
  27. //
  28. // It does not do anything useful now, and is used for abstract LeaseMgr
  29. // class testing. It may later evolve into more useful backend if the
  30. // need arises. We can reuse code from memfile benchmark. See code in
  31. // tests/tools/dhcp-ubench/memfile_bench.{cc|h}
  32. class Memfile_LeaseMgr : public LeaseMgr {
  33. public:
  34. /// @brief The sole lease manager constructor
  35. ///
  36. /// dbconfig is a generic way of passing parameters. Parameters
  37. /// are passed in the "name=value" format, separated by spaces.
  38. /// Values may be enclosed in double quotes, if needed.
  39. ///
  40. /// @param parameters A data structure relating keywords and values
  41. /// concerned with the database.
  42. Memfile_LeaseMgr(const ParameterMap& parameters);
  43. /// @brief Destructor (closes file)
  44. virtual ~Memfile_LeaseMgr();
  45. /// @brief Adds an IPv4 lease.
  46. ///
  47. /// @todo Not implemented yet
  48. /// @param lease lease to be added
  49. virtual bool addLease(const Lease4Ptr& lease);
  50. /// @brief Adds an IPv6 lease.
  51. ///
  52. /// @param lease lease to be added
  53. virtual bool addLease(const Lease6Ptr& lease);
  54. /// @brief Returns existing IPv4 lease for specified IPv4 address.
  55. ///
  56. /// @todo Not implemented yet
  57. /// @param addr address of the searched lease
  58. ///
  59. /// @return a collection of leases
  60. virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress& addr) const;
  61. /// @brief Returns existing IPv4 leases for specified hardware address.
  62. ///
  63. /// @todo Not implemented yet
  64. ///
  65. /// Although in the usual case there will be only one lease, for mobile
  66. /// clients or clients with multiple static/fixed/reserved leases there
  67. /// can be more than one. Thus return type is a container, not a single
  68. /// pointer.
  69. ///
  70. /// @param hwaddr hardware address of the client
  71. ///
  72. /// @return lease collection
  73. virtual Lease4Collection getLease4(const HWAddr& hwaddr) const;
  74. /// @brief Returns existing IPv4 leases for specified hardware address
  75. /// and a subnet
  76. ///
  77. /// @todo Not implemented yet
  78. ///
  79. /// There can be at most one lease for a given HW address in a single
  80. /// pool, so this method with either return a single lease or NULL.
  81. ///
  82. /// @param hwaddr hardware address of the client
  83. /// @param subnet_id identifier of the subnet that lease must belong to
  84. ///
  85. /// @return a pointer to the lease (or NULL if a lease is not found)
  86. virtual Lease4Ptr getLease4(const HWAddr& hwaddr,
  87. SubnetID subnet_id) const;
  88. /// @brief Returns existing IPv4 lease for specified client-id
  89. ///
  90. /// @todo Not implemented yet
  91. ///
  92. /// @param clientid client identifier
  93. virtual Lease4Collection getLease4(const ClientId& clientid) const;
  94. /// @brief Returns existing IPv4 lease for specified client-id
  95. ///
  96. /// There can be at most one lease for a given HW address in a single
  97. /// pool, so this method with either return a single lease or NULL.
  98. ///
  99. /// @todo Not implemented yet
  100. ///
  101. /// @param clientid client identifier
  102. /// @param subnet_id identifier of the subnet that lease must belong to
  103. ///
  104. /// @return a pointer to the lease (or NULL if a lease is not found)
  105. virtual Lease4Ptr getLease4(const ClientId& clientid,
  106. SubnetID subnet_id) const;
  107. /// @brief Returns existing IPv6 lease for a given IPv6 address.
  108. ///
  109. /// @param addr address of the searched lease
  110. ///
  111. /// @return smart pointer to the lease (or NULL if a lease is not found)
  112. virtual Lease6Ptr getLease6(const isc::asiolink::IOAddress& addr) const;
  113. /// @brief Returns existing IPv6 lease for a given DUID+IA combination
  114. ///
  115. /// @todo Not implemented yet
  116. ///
  117. /// @param duid client DUID
  118. /// @param iaid IA identifier
  119. ///
  120. /// @return collection of IPv6 leases
  121. virtual Lease6Collection getLease6(const DUID& duid, uint32_t iaid) const;
  122. /// @brief Returns existing IPv6 lease for a given DUID+IA combination
  123. ///
  124. /// @todo Not implemented yet
  125. ///
  126. /// @param duid client DUID
  127. /// @param iaid IA identifier
  128. /// @param subnet_id identifier of the subnet the lease must belong to
  129. ///
  130. /// @return smart pointer to the lease (or NULL if a lease is not found)
  131. virtual Lease6Ptr getLease6(const DUID& duid, uint32_t iaid, SubnetID subnet_id) const;
  132. /// @brief Updates IPv4 lease.
  133. ///
  134. /// @todo Not implemented yet
  135. ///
  136. /// @param lease4 The lease to be updated.
  137. ///
  138. /// If no such lease is present, an exception will be thrown.
  139. virtual void updateLease4(const Lease4Ptr& lease4);
  140. /// @brief Updates IPv4 lease.
  141. ///
  142. /// @todo Not implemented yet
  143. ///
  144. /// @param lease6 The lease to be updated.
  145. ///
  146. /// If no such lease is present, an exception will be thrown.
  147. virtual void updateLease6(const Lease6Ptr& lease6);
  148. /// @brief Deletes a lease.
  149. ///
  150. /// @param addr Address of the lease to be deleted. (This can be IPv4 or
  151. /// IPv6.)
  152. ///
  153. /// @return true if deletion was successful, false if no such lease exists
  154. virtual bool deleteLease(const isc::asiolink::IOAddress& addr);
  155. /// @brief Return backend type
  156. ///
  157. /// Returns the type of the backend.
  158. ///
  159. /// @return Type of the backend.
  160. virtual std::string getType() const {
  161. return (std::string("memfile"));
  162. }
  163. /// @brief Returns backend name.
  164. ///
  165. /// For now, memfile can only store data in memory.
  166. ///
  167. /// @return Name of the backend.
  168. virtual std::string getName() const {
  169. return ("memory");
  170. }
  171. /// @brief Returns description of the backend.
  172. ///
  173. /// This description may be multiline text that describes the backend.
  174. ///
  175. /// @return Description of the backend.
  176. virtual std::string getDescription() const;
  177. /// @brief Returns backend version.
  178. ///
  179. /// @return Version number as a pair of unsigned integers. "first" is the
  180. /// major version number, "second" the minor number.
  181. virtual std::pair<uint32_t, uint32_t> getVersion() const {
  182. return (std::make_pair(1, 0));
  183. }
  184. /// @brief Commit Transactions
  185. ///
  186. /// Commits all pending database operations. On databases that don't
  187. /// support transactions, this is a no-op.
  188. virtual void commit();
  189. /// @brief Rollback Transactions
  190. ///
  191. /// Rolls back all pending database operations. On databases that don't
  192. /// support transactions, this is a no-op.
  193. virtual void rollback();
  194. protected:
  195. typedef boost::multi_index_container< // this is a multi-index container...
  196. Lease6Ptr, // it will hold shared_ptr to leases6
  197. boost::multi_index::indexed_by< // and will be sorted by
  198. // IPv6 address that are unique. That particular key is a member
  199. // of the Lease6 structure, is of type IOAddress and can be accessed
  200. // by doing &Lease6::addr_
  201. boost::multi_index::ordered_unique<
  202. boost::multi_index::member<Lease6, isc::asiolink::IOAddress, &Lease6::addr_>
  203. >
  204. >
  205. > Lease6Storage; // Let the whole contraption be called Lease6Storage.
  206. Lease6Storage storage6_;
  207. };
  208. }; // end of isc::dhcp namespace
  209. }; // end of isc namespace
  210. #endif // MEMFILE_LEASE_MGR