base_host_data_source.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // Copyright (C) 2014-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 BASE_HOST_DATA_SOURCE_H
  7. #define BASE_HOST_DATA_SOURCE_H
  8. #include <asiolink/io_address.h>
  9. #include <dhcp/duid.h>
  10. #include <dhcp/hwaddr.h>
  11. #include <dhcpsrv/host.h>
  12. #include <exceptions/exceptions.h>
  13. #include <boost/shared_ptr.hpp>
  14. namespace isc {
  15. namespace dhcp {
  16. /// @brief Exception thrown when the duplicate @c Host object is detected.
  17. class DuplicateHost : public Exception {
  18. public:
  19. DuplicateHost(const char* file, size_t line, const char* what) :
  20. isc::Exception(file, line, what) { };
  21. };
  22. /// @brief Exception thrown when an address is already reserved by a @c Host
  23. /// object (DuplicateHost is same identity, ReservedAddress same address).
  24. class ReservedAddress : public Exception {
  25. public:
  26. ReservedAddress(const char* file, size_t line, const char* what) :
  27. isc::Exception(file, line, what) { };
  28. };
  29. /// @brief Exception thrown when invalid IP address has been specified for
  30. /// @c Host.
  31. class BadHostAddress : public isc::BadValue {
  32. public:
  33. BadHostAddress(const char* file, size_t line, const char* what) :
  34. isc::BadValue(file, line, what) { };
  35. };
  36. /// @brief Base interface for the classes implementing simple data source
  37. /// for host reservations.
  38. ///
  39. /// This abstract class defines an interface for the classes implementing
  40. /// basic data source for host reservations. This interface allows for
  41. /// adding new reservations (represented by @c Host objects) and retrieving
  42. /// these reservations using various parameters such as HW address or DUID,
  43. /// subnet identifier (either IPv4 or IPv6) or reserved IP address.
  44. ///
  45. /// This interface DOES NOT specify the methods to manage existing
  46. /// host reservations such as to remove one IPv6 reservation but leave
  47. /// other reservations. It also lacks the methods used for preparing
  48. /// the data to be added to the SQL database: commit, rollback etc.
  49. /// Such methods are declared in other interfaces.
  50. class BaseHostDataSource {
  51. public:
  52. /// @brief Specifies the type of an identifier.
  53. ///
  54. /// This is currently used only by MySQL host data source for now, but
  55. /// it is envisagad that it will be used by other host data sources
  56. /// in the future. Also, this list will grow over time. It is likely
  57. /// that we'll implement other identifiers in the future, e.g. remote-id.
  58. ///
  59. /// Those value correspond direclty to dhcp_identifier_type in hosts
  60. /// table in MySQL schema.
  61. enum IdType {
  62. ID_HWADDR = 0, ///< Hardware address
  63. ID_DUID = 1 ///< DUID/client-id
  64. };
  65. /// @brief Default destructor implementation.
  66. virtual ~BaseHostDataSource() { }
  67. /// @brief Return all hosts for the specified HW address or DUID.
  68. ///
  69. /// This method returns all @c Host objects which represent reservations
  70. /// for the specified HW address or DUID. Note, that this method may
  71. /// return multiple reservations because a particular client may have
  72. /// reservations in multiple subnets and the same client may be identified
  73. /// by HW address or DUID. The server is unable to verify that the specific
  74. /// DUID and HW address belong to the same client, until the client sends
  75. /// a DHCP message.
  76. ///
  77. /// Specifying both hardware address and DUID is allowed for this method
  78. /// and results in returning all objects that are associated with hardware
  79. /// address OR duid. For example: if one host is associated with the
  80. /// specified hardware address and another host is associated with the
  81. /// specified DUID, two hosts will be returned.
  82. ///
  83. /// @param hwaddr HW address of the client or NULL if no HW address
  84. /// available.
  85. /// @param duid client id or NULL if not available, e.g. DHCPv4 client case.
  86. ///
  87. /// @return Collection of const @c Host objects.
  88. virtual ConstHostCollection
  89. getAll(const HWAddrPtr& hwaddr, const DuidPtr& duid = DuidPtr()) const = 0;
  90. /// @brief Return all hosts connected to any subnet for which reservations
  91. /// have been made using a specified identifier.
  92. ///
  93. /// This method returns all @c Host objects which represent reservations
  94. /// for a specified identifier. This method may return multiple hosts
  95. /// because a particular client may have reservations in multiple subnets.
  96. ///
  97. /// @param identifier_type Identifier type.
  98. /// @param identifier_begin Pointer to a begining of a buffer containing
  99. /// an identifier.
  100. /// @param identifier_len Identifier length.
  101. ///
  102. /// @return Collection of const @c Host objects.
  103. virtual ConstHostCollection
  104. getAll(const Host::IdentifierType& identifier_type,
  105. const uint8_t* identifier_begin,
  106. const size_t identifier_len) const = 0;
  107. /// @brief Returns a collection of hosts using the specified IPv4 address.
  108. ///
  109. /// This method may return multiple @c Host objects if they are connected
  110. /// to different subnets.
  111. ///
  112. /// @param address IPv4 address for which the @c Host object is searched.
  113. ///
  114. /// @return Collection of const @c Host objects.
  115. virtual ConstHostCollection
  116. getAll4(const asiolink::IOAddress& address) const = 0;
  117. /// @brief Returns a host connected to the IPv4 subnet.
  118. ///
  119. /// Implementations of this method should guard against the case when
  120. /// mutliple instances of the @c Host are present, e.g. when two
  121. /// @c Host objects are found, one for the DUID, another one for the
  122. /// HW address. In such case, an implementation of this method
  123. /// should throw an exception.
  124. ///
  125. /// @param subnet_id Subnet identifier.
  126. /// @param hwaddr HW address of the client or NULL if no HW address
  127. /// available.
  128. /// @param duid client id or NULL if not available.
  129. ///
  130. /// @return Const @c Host object using a specified HW address or DUID.
  131. virtual ConstHostPtr
  132. get4(const SubnetID& subnet_id, const HWAddrPtr& hwaddr,
  133. const DuidPtr& duid = DuidPtr()) const = 0;
  134. /// @brief Returns a host connected to the IPv4 subnet.
  135. ///
  136. /// @param subnet_id Subnet identifier.
  137. /// @param identifier_type Identifier type.
  138. /// @param identifier_begin Pointer to a begining of a buffer containing
  139. /// an identifier.
  140. /// @param identifier_len Identifier length.
  141. ///
  142. /// @return Const @c Host object for which reservation has been made using
  143. /// the specified identifier.
  144. virtual ConstHostPtr
  145. get4(const SubnetID& subnet_id,
  146. const Host::IdentifierType& identifier_type,
  147. const uint8_t* identifier_begin,
  148. const size_t identifier_len) const = 0;
  149. /// @brief Returns a host connected to the IPv4 subnet and having
  150. /// a reservation for a specified IPv4 address.
  151. ///
  152. /// One of the use cases for this method is to detect collisions between
  153. /// dynamically allocated addresses and reserved addresses. When the new
  154. /// address is assigned to a client, the allocation mechanism should check
  155. /// if this address is not reserved for some other host and do not allocate
  156. /// this address if reservation is present.
  157. ///
  158. /// Implementations of this method should guard against invalid addresses,
  159. /// such as IPv6 address.
  160. ///
  161. /// @param subnet_id Subnet identifier.
  162. /// @param address reserved IPv4 address.
  163. ///
  164. /// @return Const @c Host object using a specified IPv4 address.
  165. virtual ConstHostPtr
  166. get4(const SubnetID& subnet_id,
  167. const asiolink::IOAddress& address) const = 0;
  168. /// @brief Returns a host connected to the IPv6 subnet.
  169. ///
  170. /// Implementations of this method should guard against the case when
  171. /// mutliple instances of the @c Host are present, e.g. when two
  172. /// @c Host objects are found, one for the DUID, another one for the
  173. /// HW address. In such case, an implementation of this method
  174. /// should throw an exception.
  175. ///
  176. /// @param subnet_id Subnet identifier.
  177. /// @param hwaddr HW address of the client or NULL if no HW address
  178. /// available.
  179. /// @param duid DUID or NULL if not available.
  180. ///
  181. /// @return Const @c Host object using a specified HW address or DUID.
  182. virtual ConstHostPtr
  183. get6(const SubnetID& subnet_id, const DuidPtr& duid,
  184. const HWAddrPtr& hwaddr = HWAddrPtr()) const = 0;
  185. /// @brief Returns a host connected to the IPv6 subnet.
  186. ///
  187. /// @param subnet_id Subnet identifier.
  188. /// @param identifier_type Identifier type.
  189. /// @param identifier_begin Pointer to a begining of a buffer containing
  190. /// an identifier.
  191. /// @param identifier_len Identifier length.
  192. ///
  193. /// @return Const @c Host object for which reservation has been made using
  194. /// the specified identifier.
  195. virtual ConstHostPtr
  196. get6(const SubnetID& subnet_id,
  197. const Host::IdentifierType& identifier_type,
  198. const uint8_t* identifier_begin,
  199. const size_t identifier_len) const = 0;
  200. /// @brief Returns a host using the specified IPv6 prefix.
  201. ///
  202. /// @param prefix IPv6 prefix for which the @c Host object is searched.
  203. /// @param prefix_len IPv6 prefix length.
  204. ///
  205. /// @return Const @c Host object using a specified HW address or DUID.
  206. virtual ConstHostPtr
  207. get6(const asiolink::IOAddress& prefix, const uint8_t prefix_len) const = 0;
  208. /// @brief Returns a host connected to the IPv6 subnet and having
  209. /// a reservation for a specified IPv6 address or prefix.
  210. ///
  211. /// @param subnet_id Subnet identifier.
  212. /// @param address reserved IPv6 address/prefix.
  213. ///
  214. /// @return Const @c Host object using a specified IPv6 address/prefix.
  215. virtual ConstHostPtr
  216. get6(const SubnetID& subnet_id, const asiolink::IOAddress& address) const = 0;
  217. /// @brief Adds a new host to the collection.
  218. ///
  219. /// The implementations of this method should guard against duplicate
  220. /// reservations for the same host, where possible. For example, when the
  221. /// reservation for the same HW address and subnet id is added twice, the
  222. /// implementation should throw an exception. Note, that usually it is
  223. /// impossible to guard against adding duplicated host, where one instance
  224. /// is identified by HW address, another one by DUID.
  225. ///
  226. /// @param host Pointer to the new @c Host object being added.
  227. virtual void add(const HostPtr& host) = 0;
  228. /// @brief Return backend type
  229. ///
  230. /// Returns the type of the backend (e.g. "mysql", "memfile" etc.)
  231. ///
  232. /// @return Type of the backend.
  233. virtual std::string getType() const = 0;
  234. /// @brief Commit Transactions
  235. ///
  236. /// Commits all pending database operations. On databases that don't
  237. /// support transactions, this is a no-op.
  238. virtual void commit() {};
  239. /// @brief Rollback Transactions
  240. ///
  241. /// Rolls back all pending database operations. On databases that don't
  242. /// support transactions, this is a no-op.
  243. virtual void rollback() {};
  244. };
  245. /// @brief HostDataSource pointer
  246. typedef boost::shared_ptr<BaseHostDataSource> HostDataSourcePtr;
  247. }
  248. }
  249. #endif // BASE_HOST_DATA_SOURCE_H