mysql_lease_mgr.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 __MYSQL_LEASE_MGR_H
  15. #define __MYSQL_LEASE_MGR_H
  16. #include <time.h>
  17. #include <mysql.h>
  18. #include <dhcp/lease_mgr.h>
  19. namespace isc {
  20. namespace dhcp {
  21. /// @brief MySQL Lease Manager
  22. ///
  23. /// This is a concrete API for the backend for the MySQL database.
  24. class MySqlLeaseMgr : public LeaseMgr {
  25. public:
  26. /// @brief Constructor
  27. ///
  28. /// Uses the following keywords in the parameters passed to it to
  29. /// connect to the database:
  30. /// - name - Name of the database to which to connect (mandatory)
  31. /// - host - Host to which to connect (optional, defaults to "localhost")
  32. /// - user - Username under which to connect (optional)
  33. /// - password - Password for "user" on the database (optional)
  34. ///
  35. /// If the database is successfully opened, the version number in the
  36. /// schema_version table will be checked against hard-coded value in
  37. /// the implementation file.
  38. ///
  39. /// Finally, all the SQL commands are pre-compiled.
  40. ///
  41. /// @param parameters A data structure relating keywords and values
  42. /// concerned with the database.
  43. ///
  44. /// @exception NoDatabaseName Mandatory database name not given
  45. /// @exception DbOpenError Error opening the database
  46. /// @exception DbOperationError An operation on the open database has
  47. /// failed.
  48. MySqlLeaseMgr(const ParameterMap& parameters);
  49. /// @brief Destructor (closes database)
  50. virtual ~MySqlLeaseMgr();
  51. /// @brief Adds an IPv4 lease.
  52. ///
  53. /// @param lease lease to be added
  54. ///
  55. /// @result true if the lease was added, false if not (because a lease
  56. /// with the same address was already there).
  57. ///
  58. /// @exception DbOperationError Database function failed
  59. virtual bool addLease(const Lease4Ptr& lease);
  60. /// @brief Adds an IPv6 lease.
  61. ///
  62. /// @param lease lease to be added
  63. ///
  64. /// @result true if the lease was added, false if not (because a lease
  65. /// with the same address was already there).
  66. ///
  67. /// @exception DbOperationError Database function failed
  68. virtual bool addLease(const Lease6Ptr& lease);
  69. /// @brief Return IPv4 lease for specified IPv4 address and subnet_id
  70. ///
  71. /// This method is used to get a lease for specific subnet_id. There can be
  72. /// at most one lease for any given subnet, so this method returns a single
  73. /// pointer.
  74. ///
  75. /// @param addr address of the sought lease
  76. /// @param subnet_id ID of the subnet the lease must belong to
  77. ///
  78. /// @return smart pointer to the lease (or NULL if a lease is not found)
  79. virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress& addr,
  80. SubnetID subnet_id) const;
  81. /// @brief Returns an IPv4 lease for specified IPv4 address
  82. ///
  83. /// This method return a lease that is associated with a given address.
  84. /// For other query types (by hardware addr, by DUID) there can be
  85. /// several leases in different subnets (e.g. for mobile clients that
  86. /// got address in different subnets). However, for a single address
  87. /// there can be only one lease, so this method returns a pointer to
  88. /// a single lease, not a container of leases.
  89. ///
  90. /// @param addr address of the searched lease
  91. /// @param subnet_id ID of the subnet the lease must belong to
  92. ///
  93. /// @return smart pointer to the lease (or NULL if a lease is not found)
  94. virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress& addr) const;
  95. /// @brief Returns existing IPv4 leases for specified hardware address.
  96. ///
  97. /// Although in the usual case there will be only one lease, for mobile
  98. /// clients or clients with multiple static/fixed/reserved leases there
  99. /// can be more than one. Thus return type is a container, not a single
  100. /// pointer.
  101. ///
  102. /// @param hwaddr hardware address of the client
  103. ///
  104. /// @return lease collection
  105. virtual Lease4Collection getLease4(const HWAddr& hwaddr) const;
  106. /// @brief Returns existing IPv4 leases for specified hardware address
  107. /// and a subnet
  108. ///
  109. /// There can be at most one lease for a given HW address in a single
  110. /// pool, so this method with either return a single lease or NULL.
  111. ///
  112. /// @param hwaddr hardware address of the client
  113. /// @param subnet_id identifier of the subnet that lease must belong to
  114. ///
  115. /// @return a pointer to the lease (or NULL if a lease is not found)
  116. virtual Lease4Ptr getLease4(const HWAddr& hwaddr,
  117. SubnetID subnet_id) const;
  118. /// @brief Returns existing IPv4 lease for specified client-id
  119. ///
  120. /// Although in the usual case there will be only one lease, for mobile
  121. /// clients or clients with multiple static/fixed/reserved leases there
  122. /// can be more than one. Thus return type is a container, not a single
  123. /// pointer.
  124. ///
  125. /// @param clientid client identifier
  126. ///
  127. /// @return lease collection
  128. virtual Lease4Collection getLease4(const ClientId& clientid) const;
  129. /// @brief Returns existing IPv4 lease for specified client-id
  130. ///
  131. /// There can be at most one lease for a given HW address in a single
  132. /// pool, so this method with either return a single lease or NULL.
  133. ///
  134. /// @param clientid client identifier
  135. /// @param subnet_id identifier of the subnet that lease must belong to
  136. ///
  137. /// @return a pointer to the lease (or NULL if a lease is not found)
  138. virtual Lease4Ptr getLease4(const ClientId& clientid,
  139. SubnetID subnet_id) const;
  140. /// @brief Returns existing IPv6 lease for a given IPv6 address.
  141. ///
  142. /// For a given address, we assume that there will be only one lease.
  143. /// The assumtion here is that there will not be site or link-local
  144. /// addresses used, so there is no way of having address duplication.
  145. ///
  146. /// @param addr address of the searched lease
  147. ///
  148. /// @return smart pointer to the lease (or NULL if a lease is not found)
  149. ///
  150. /// @exception BadValue record retrieved from database had an invalid
  151. /// lease type field.
  152. /// @exception DbOperationError MySQL operation failed, exception will give
  153. /// text indicating the reason.
  154. virtual Lease6Ptr getLease6(const isc::asiolink::IOAddress& addr) const;
  155. /// @brief Returns existing IPv6 leases for a given DUID+IA combination
  156. ///
  157. /// Although in the usual case there will be only one lease, for mobile
  158. /// clients or clients with multiple static/fixed/reserved leases there
  159. /// can be more than one. Thus return type is a container, not a single
  160. /// pointer.
  161. ///
  162. /// @param duid client DUID
  163. /// @param iaid IA identifier
  164. ///
  165. /// @return smart pointer to the lease (or NULL if a lease is not found)
  166. virtual Lease6Collection getLease6(const DUID& duid,
  167. uint32_t iaid) const;
  168. /// @brief Returns existing IPv6 lease for a given DUID+IA combination
  169. ///
  170. /// @param duid client DUID
  171. /// @param iaid IA identifier
  172. /// @param subnet_id subnet id of the subnet the lease belongs to
  173. ///
  174. /// @return smart pointer to the lease (or NULL if a lease is not found)
  175. virtual Lease6Ptr getLease6(const DUID& duid, uint32_t iaid,
  176. SubnetID subnet_id) const;
  177. /// @brief Updates IPv4 lease.
  178. ///
  179. /// @param lease4 The lease to be updated.
  180. ///
  181. /// If no such lease is present, an exception will be thrown.
  182. virtual void updateLease4(const Lease4Ptr& lease4);
  183. /// @brief Updates IPv4 lease.
  184. ///
  185. /// @param lease4 The lease to be updated.
  186. ///
  187. /// If no such lease is present, an exception will be thrown.
  188. virtual void updateLease6(const Lease6Ptr& lease6);
  189. /// @brief Deletes a lease.
  190. ///
  191. /// @param addr IPv4 address of the lease to be deleted.
  192. ///
  193. /// @return true if deletion was successful, false if no such lease exists
  194. virtual bool deleteLease4(const isc::asiolink::IOAddress& addr);
  195. /// @brief Deletes a lease.
  196. ///
  197. /// @param addr IPv4 address of the lease to be deleted.
  198. ///
  199. /// @return true if deletion was successful, false if no such lease exists
  200. ///
  201. /// @exception DbOperationError MySQL operation failed, exception will give
  202. /// text indicating the reason.
  203. virtual bool deleteLease6(const isc::asiolink::IOAddress& addr);
  204. /// @brief Returns backend name.
  205. ///
  206. /// Each backend have specific name, e.g. "mysql" or "sqlite".
  207. virtual std::string getName() const;
  208. /// @brief Returns description of the backend.
  209. ///
  210. /// This description may be multiline text that describes the backend.
  211. virtual std::string getDescription() const;
  212. /// @brief Returns backend version.
  213. ///
  214. /// @return Version number as a pair of unsigned integers. "first" is the
  215. /// major version number, "second" the minor number.
  216. ///
  217. /// @todo: We will need to implement 3 version functions eventually:
  218. /// A. abstract API version
  219. /// B. backend version
  220. /// C. database version (stored in the database scheme)
  221. ///
  222. /// and then check that:
  223. /// B>=A and B=C (it is ok to have newer backend, as it should be backward
  224. /// compatible)
  225. /// Also if B>C, some database upgrade procedure may be triggered
  226. ///
  227. /// @exception DbOperationError MySQL operation failed, exception will give
  228. /// text indicating the reason.
  229. virtual std::pair<uint32_t, uint32_t> getVersion() const;
  230. /// @brief Commit Transactions
  231. ///
  232. /// Commits all pending database operations. On databases that don't
  233. /// support transactions, this is a no-op.
  234. ///
  235. /// @exception DbOperationError if the commit failed.
  236. virtual void commit();
  237. /// @brief Rollback Transactions
  238. ///
  239. /// Rolls back all pending database operations. On databases that don't
  240. /// support transactions, this is a no-op.
  241. ///
  242. /// @exception DbOperationError if the rollback failed.
  243. virtual void rollback();
  244. ///@{
  245. /// The following methods are used to convert between times and time
  246. /// intervals stored in the Lease object, and the times stored in the
  247. /// database. The reason for the difference is because in the DHCP server,
  248. /// the cltt (Client Time Since Last Transmission) is the natural data; in
  249. /// the lease file - which may be read by the user - it is the expiry time
  250. /// of the lease.
  251. /// @brief Convert Lease Time to Database Times
  252. ///
  253. /// Within the DHCP servers, times are stored as client last transmit time
  254. /// and valid lifetime. In the database, the information is stored as
  255. /// valid lifetime and "expire" (time of expiry of the lease). They are
  256. /// related by the equation:
  257. ///
  258. /// - expire = client last transmit time + valid lifetime
  259. ///
  260. /// This method converts from the times in the lease object into times
  261. /// able to be added to the database.
  262. ///
  263. /// @param cltt Client last transmit time
  264. /// @param valid_lifetime Valid lifetime
  265. /// @param expire Reference to MYSQL_TIME object where the expiry time of
  266. /// the lease will be put.
  267. static
  268. void convertToDatabaseTime(time_t cltt, uint32_t valid_lifetime,
  269. MYSQL_TIME& expire);
  270. /// @brief Convert Database Time to Lease Times
  271. ///
  272. /// Within the database, time is stored as "expire" (time of expiry of the
  273. /// lease) and valid lifetime. In the DHCP server, the information is
  274. /// stored client last transmit time and valid lifetime. These are related
  275. /// by the equation:
  276. ///
  277. /// - client last transmit time = expire - valid_lifetime
  278. ///
  279. /// This method converts from the times in the database into times
  280. /// able to be inserted into the lease object.
  281. ///
  282. /// @param expire Reference to MYSQL_TIME object from where the expiry
  283. /// time of the lease is taken.
  284. /// @param lease_time lifetime of the lease.
  285. /// @param cltt Reference to location where client last transmit time
  286. /// is put.
  287. static
  288. void convertFromDatabaseTime(const MYSQL_TIME& expire,
  289. uint32_t valid_lifetime, time_t& cltt);
  290. ///@}
  291. private:
  292. /// @brief Statement Tags
  293. ///
  294. /// The contents of the enum are indexes into the list of SQL statements
  295. enum StatementIndex {
  296. DELETE_LEASE6, // Delete from lease6 by address
  297. GET_LEASE6, // Get lease 6 by address
  298. GET_VERSION, // Obtain version number
  299. INSERT_LEASE6, // Add entry to lease6 table
  300. NUM_STATEMENTS // Number of statements
  301. };
  302. /// @brief Prepare Single Statement
  303. ///
  304. /// Creates a prepared statement from the text given and adds it to the
  305. /// statements_ vector at the given index.
  306. ///
  307. /// @param index Index into the statements_ vector into which the text
  308. /// should be placed. The vector must be big enough for the index
  309. /// to be valid, else an exception will be thrown.
  310. /// @param text Text of the SQL statement to be prepared.
  311. ///
  312. /// @exception DbOperationError MySQL operation failed, exception will give
  313. /// text indicating the reason.
  314. /// @exception InvalidParameter 'index' is not valid for the vector.
  315. void prepareStatement(StatementIndex index, const char* text);
  316. /// @brief Prepare statements
  317. ///
  318. /// Creates the prepared statements for all of the SQL statements used
  319. /// by the MySQL backend.
  320. ///
  321. /// @exception DbOperationError MySQL operation failed, exception will give
  322. /// text indicating the reason.
  323. /// @exception InvalidParameter 'index' is not valid for the vector. This
  324. /// represents an internal error within the code.
  325. void prepareStatements();
  326. /// @brief Open Database
  327. ///
  328. /// Opens the database using the information supplied in the parameters
  329. /// passed to the constructor.
  330. ///
  331. /// @exception NoDatabaseName Mandatory database name not given
  332. /// @exception DbOpenError Error opening the database
  333. void openDatabase();
  334. /// @brief Check Error and Throw Exception
  335. ///
  336. /// Virtually all MySQL functions return a status which, if non-zero,
  337. /// indicates an error. This inline function conceals a lot of error
  338. /// checking/exception-throwing code.
  339. ///
  340. /// @param status Status code: non-zero implies an error
  341. /// @param index Index of statement that caused the error
  342. /// @param what High-level description of the error
  343. ///
  344. /// @exception DbOperationError Error doing a database operation
  345. inline void checkError(int status, StatementIndex index,
  346. const char* what) const {
  347. if (status != 0) {
  348. isc_throw(DbOperationError, what << " for <" <<
  349. text_statements_[index] << ">, reason: " <<
  350. mysql_error(mysql_) << " (error code " <<
  351. mysql_errno(mysql_) << ")");
  352. }
  353. }
  354. // Members
  355. MYSQL* mysql_; ///< MySQL context object
  356. std::vector<std::string> text_statements_; ///< Raw text of statements
  357. std::vector<MYSQL_STMT*> statements_; ///< Prepared statements
  358. };
  359. }; // end of isc::dhcp namespace
  360. }; // end of isc namespace
  361. #endif // __MYSQL_LEASE_MGR_H