mysql_host_data_source.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 MYSQL_HOST_DATA_SOURCE_H
  7. #define MYSQL_HOST_DATA_SOURCE_H
  8. #include <dhcpsrv/base_host_data_source.h>
  9. #include <dhcpsrv/db_exceptions.h>
  10. #include <dhcpsrv/mysql_connection.h>
  11. namespace isc {
  12. namespace dhcp {
  13. /// Forward declaration to the implementation of the @ref MySqlHostDataSource.
  14. class MySqlHostDataSourceImpl;
  15. /// @brief MySQL Host Data Source
  16. ///
  17. /// This class implements the @ref isc::dhcp::BaseHostDataSource interface to
  18. /// the MySQL database. Use of this backend presupposes that a MySQL database
  19. /// is available and that the Kea schema has been created within it.
  20. class MySqlHostDataSource: public BaseHostDataSource {
  21. public:
  22. /// @brief Constructor
  23. ///
  24. /// Uses the following keywords in the parameters passed to it to
  25. /// connect to the database:
  26. /// - name - Name of the database to which to connect (mandatory)
  27. /// - host - Host to which to connect (optional, defaults to "localhost")
  28. /// - user - Username under which to connect (optional)
  29. /// - password - Password for "user" on the database (optional)
  30. ///
  31. /// If the database is successfully opened, the version number in the
  32. /// schema_version table will be checked against hard-coded value in
  33. /// the implementation file.
  34. ///
  35. /// Finally, all the SQL commands are pre-compiled.
  36. ///
  37. /// @param parameters A data structure relating keywords and values
  38. /// concerned with the database.
  39. ///
  40. /// @throw isc::dhcp::NoDatabaseName Mandatory database name not given
  41. /// @throw isc::dhcp::DbOpenError Error opening the database
  42. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  43. /// failed.
  44. MySqlHostDataSource(const DatabaseConnection::ParameterMap& parameters);
  45. /// @brief Virtual destructor.
  46. ///
  47. /// Releases prepared MySQL statements used by the backend.
  48. virtual ~MySqlHostDataSource();
  49. /// @brief Return all hosts for the specified HW address or DUID.
  50. ///
  51. /// This method returns all @c Host objects which represent reservations
  52. /// for the specified HW address or DUID. Note, that this method may
  53. /// return multiple reservations because a particular client may have
  54. /// reservations in multiple subnets and the same client may be identified
  55. /// by HW address or DUID. The server is unable to verify that the specific
  56. /// DUID and HW address belong to the same client, until the client sends
  57. /// a DHCP message.
  58. ///
  59. /// Specifying both hardware address and DUID is allowed for this method
  60. /// and results in returning all objects that are associated with hardware
  61. /// address OR duid. For example: if one host is associated with the
  62. /// specified hardware address and another host is associated with the
  63. /// specified DUID, two hosts will be returned.
  64. ///
  65. /// @param hwaddr HW address of the client or NULL if no HW address
  66. /// available.
  67. /// @param duid client id or NULL if not available, e.g. DHCPv4 client case.
  68. ///
  69. /// @return Collection of const @c Host objects.
  70. virtual ConstHostCollection
  71. getAll(const HWAddrPtr& hwaddr, const DuidPtr& duid = DuidPtr()) const;
  72. /// @brief Return all hosts connected to any subnet for which reservations
  73. /// have been made using a specified identifier.
  74. ///
  75. /// This method returns all @c Host objects which represent reservations
  76. /// for a specified identifier. This method may return multiple hosts
  77. /// because a particular client may have reservations in multiple subnets.
  78. ///
  79. /// @param identifier_type Identifier type.
  80. /// @param identifier_begin Pointer to a begining of a buffer containing
  81. /// an identifier.
  82. /// @param identifier_len Identifier length.
  83. ///
  84. /// @return Collection of const @c Host objects.
  85. virtual ConstHostCollection
  86. getAll(const Host::IdentifierType& identifier_type,
  87. const uint8_t* identifier_begin, const size_t identifier_len) const;
  88. /// @brief Returns a collection of hosts using the specified IPv4 address.
  89. ///
  90. /// This method may return multiple @c Host objects if they are connected
  91. /// to different subnets.
  92. ///
  93. /// @param address IPv4 address for which the @c Host object is searched.
  94. ///
  95. /// @return Collection of const @c Host objects.
  96. virtual ConstHostCollection
  97. getAll4(const asiolink::IOAddress& address) const;
  98. /// @brief Returns a host connected to the IPv4 subnet.
  99. ///
  100. /// Implementations of this method should guard against the case when
  101. /// mutliple instances of the @c Host are present, e.g. when two
  102. /// @c Host objects are found, one for the DUID, another one for the
  103. /// HW address. In such case, an implementation of this method
  104. /// should throw an MultipleRecords exception.
  105. ///
  106. /// @param subnet_id Subnet identifier.
  107. /// @param hwaddr HW address of the client or NULL if no HW address
  108. /// available.
  109. /// @param duid client id or NULL if not available.
  110. ///
  111. /// @return Const @c Host object using a specified HW address or DUID.
  112. virtual ConstHostPtr
  113. get4(const SubnetID& subnet_id, const HWAddrPtr& hwaddr,
  114. const DuidPtr& duid = DuidPtr()) const;
  115. /// @brief Returns a host connected to the IPv4 subnet.
  116. ///
  117. /// @param subnet_id Subnet identifier.
  118. /// @param identifier_type Identifier type.
  119. /// @param identifier_begin Pointer to a begining of a buffer containing
  120. /// an identifier.
  121. /// @param identifier_len Identifier length.
  122. ///
  123. /// @return Const @c Host object for which reservation has been made using
  124. /// the specified identifier.
  125. virtual ConstHostPtr
  126. get4(const SubnetID& subnet_id, const Host::IdentifierType& identifier_type,
  127. const uint8_t* identifier_begin, const size_t identifier_len) const;
  128. /// @brief Returns a host connected to the IPv4 subnet and having
  129. /// a reservation for a specified IPv4 address.
  130. ///
  131. /// One of the use cases for this method is to detect collisions between
  132. /// dynamically allocated addresses and reserved addresses. When the new
  133. /// address is assigned to a client, the allocation mechanism should check
  134. /// if this address is not reserved for some other host and do not allocate
  135. /// this address if reservation is present.
  136. ///
  137. /// Implementations of this method should guard against invalid addresses,
  138. /// such as IPv6 address.
  139. ///
  140. /// @param subnet_id Subnet identifier.
  141. /// @param address reserved IPv4 address.
  142. ///
  143. /// @return Const @c Host object using a specified IPv4 address.
  144. virtual ConstHostPtr
  145. get4(const SubnetID& subnet_id, const asiolink::IOAddress& address) const;
  146. /// @brief Returns a host connected to the IPv6 subnet.
  147. ///
  148. /// Implementations of this method should guard against the case when
  149. /// mutliple instances of the @c Host are present, e.g. when two
  150. /// @c Host objects are found, one for the DUID, another one for the
  151. /// HW address. In such case, an implementation of this method
  152. /// should throw an MultipleRecords exception.
  153. ///
  154. /// @param subnet_id Subnet identifier.
  155. /// @param hwaddr HW address of the client or NULL if no HW address
  156. /// available.
  157. /// @param duid DUID or NULL if not available.
  158. ///
  159. /// @return Const @c Host object using a specified HW address or DUID.
  160. virtual ConstHostPtr
  161. get6(const SubnetID& subnet_id, const DuidPtr& duid,
  162. const HWAddrPtr& hwaddr = HWAddrPtr()) const;
  163. /// @brief Returns a host connected to the IPv6 subnet.
  164. ///
  165. /// @param subnet_id Subnet identifier.
  166. /// @param identifier_type Identifier type.
  167. /// @param identifier_begin Pointer to a begining of a buffer containing
  168. /// an identifier.
  169. /// @param identifier_len Identifier length.
  170. ///
  171. /// @return Const @c Host object for which reservation has been made using
  172. /// the specified identifier.
  173. virtual ConstHostPtr
  174. get6(const SubnetID& subnet_id, const Host::IdentifierType& identifier_type,
  175. const uint8_t* identifier_begin, const size_t identifier_len) const;
  176. /// @brief Returns a host using the specified IPv6 prefix.
  177. ///
  178. /// @param prefix IPv6 prefix for which the @c Host object is searched.
  179. /// @param prefix_len IPv6 prefix length.
  180. ///
  181. /// @return Const @c Host object using a specified HW address or DUID.
  182. virtual ConstHostPtr
  183. get6(const asiolink::IOAddress& prefix, const uint8_t prefix_len) const;
  184. /// @brief Adds a new host to the collection.
  185. ///
  186. /// The implementations of this method should guard against duplicate
  187. /// reservations for the same host, where possible. For example, when the
  188. /// reservation for the same HW address and subnet id is added twice, the
  189. /// addHost method should throw an DuplicateEntry exception. Note, that
  190. /// usually it is impossible to guard against adding duplicated host, where
  191. /// one instance is identified by HW address, another one by DUID.
  192. ///
  193. /// @param host Pointer to the new @c Host object being added.
  194. virtual void add(const HostPtr& host);
  195. /// @brief Return backend type
  196. ///
  197. /// Returns the type of the backend (e.g. "mysql", "memfile" etc.)
  198. ///
  199. /// @return Type of the backend.
  200. virtual std::string getType() const {
  201. return (std::string("mysql"));
  202. }
  203. /// @brief Returns backend name.
  204. ///
  205. /// Each backend have specific name.
  206. ///
  207. /// @return "mysql".
  208. virtual std::string getName() const;
  209. /// @brief Returns description of the backend.
  210. ///
  211. /// This description may be multiline text that describes the backend.
  212. ///
  213. /// @return Description of the backend.
  214. virtual std::string getDescription() const;
  215. /// @brief Returns backend version.
  216. ///
  217. /// @return Version number stored in the database, as a pair of unsigned
  218. /// integers. "first" is the major version number, "second" the
  219. /// minor number.
  220. ///
  221. /// @throw isc::dhcp::DbOperationError An operation on the open database
  222. /// has failed.
  223. virtual std::pair<uint32_t, uint32_t> getVersion() const;
  224. /// @brief Commit Transactions
  225. ///
  226. /// Commits all pending database operations.
  227. virtual void commit();
  228. /// @brief Rollback Transactions
  229. ///
  230. /// Rolls back all pending database operations.
  231. virtual void rollback();
  232. private:
  233. /// @brief Pointer to the implementation of the @ref MySqlHostDataSource.
  234. MySqlHostDataSourceImpl* impl_;
  235. };
  236. }
  237. }
  238. #endif // MYSQL_HOST_DATA_SOURCE_H