mysql_lease_mgr.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. // Copyright (C) 2012-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 MYSQL_LEASE_MGR_H
  15. #define MYSQL_LEASE_MGR_H
  16. #include <dhcp/hwaddr.h>
  17. #include <dhcpsrv/lease_mgr.h>
  18. #include <dhcpsrv/mysql_connection.h>
  19. #include <boost/scoped_ptr.hpp>
  20. #include <boost/utility.hpp>
  21. #include <mysql.h>
  22. #include <time.h>
  23. namespace isc {
  24. namespace dhcp {
  25. // Define the current database schema values
  26. const uint32_t CURRENT_VERSION_VERSION = 3;
  27. const uint32_t CURRENT_VERSION_MINOR = 0;
  28. // Forward declaration of the Lease exchange objects. These classes are defined
  29. // in the .cc file.
  30. class MySqlLease4Exchange;
  31. class MySqlLease6Exchange;
  32. /// @brief MySQL Lease Manager
  33. ///
  34. /// This class provides the \ref isc::dhcp::LeaseMgr interface to the MySQL
  35. /// database. Use of this backend presupposes that a MySQL database is
  36. /// available and that the Kea schema has been created within it.
  37. class MySqlLeaseMgr : public LeaseMgr {
  38. public:
  39. /// @brief Constructor
  40. ///
  41. /// Uses the following keywords in the parameters passed to it to
  42. /// connect to the database:
  43. /// - name - Name of the database to which to connect (mandatory)
  44. /// - host - Host to which to connect (optional, defaults to "localhost")
  45. /// - user - Username under which to connect (optional)
  46. /// - password - Password for "user" on the database (optional)
  47. ///
  48. /// If the database is successfully opened, the version number in the
  49. /// schema_version table will be checked against hard-coded value in
  50. /// the implementation file.
  51. ///
  52. /// Finally, all the SQL commands are pre-compiled.
  53. ///
  54. /// @param parameters A data structure relating keywords and values
  55. /// concerned with the database.
  56. ///
  57. /// @throw isc::dhcp::NoDatabaseName Mandatory database name not given
  58. /// @throw isc::dhcp::DbOpenError Error opening the database
  59. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  60. /// failed.
  61. MySqlLeaseMgr(const DatabaseConnection::ParameterMap& parameters);
  62. /// @brief Destructor (closes database)
  63. virtual ~MySqlLeaseMgr();
  64. /// @brief Local version of getDBVersion() class method
  65. static std::string getDBVersion();
  66. /// @brief Adds an IPv4 lease
  67. ///
  68. /// @param lease lease to be added
  69. ///
  70. /// @result true if the lease was added, false if not (because a lease
  71. /// with the same address was already there).
  72. ///
  73. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  74. /// failed.
  75. virtual bool addLease(const Lease4Ptr& lease);
  76. /// @brief Adds an IPv6 lease
  77. ///
  78. /// @param lease lease to be added
  79. ///
  80. /// @result true if the lease was added, false if not (because a lease
  81. /// with the same address was already there).
  82. ///
  83. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  84. /// failed.
  85. virtual bool addLease(const Lease6Ptr& lease);
  86. /// @brief Returns an IPv4 lease for specified IPv4 address
  87. ///
  88. /// This method return a lease that is associated with a given address.
  89. /// For other query types (by hardware addr, by Client ID) there can be
  90. /// several leases in different subnets (e.g. for mobile clients that
  91. /// got address in different subnets). However, for a single address
  92. /// there can be only one lease, so this method returns a pointer to
  93. /// a single lease, not a container of leases.
  94. ///
  95. /// @param addr address of the searched lease
  96. ///
  97. /// @return smart pointer to the lease (or NULL if a lease is not found)
  98. ///
  99. /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
  100. /// fit into the space allocated for the result. This indicates a
  101. /// programming error.
  102. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  103. /// failed.
  104. virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress& addr) const;
  105. /// @brief Returns existing IPv4 leases for specified hardware address.
  106. ///
  107. /// Although in the usual case there will be only one lease, for mobile
  108. /// clients or clients with multiple static/fixed/reserved leases there
  109. /// can be more than one. Thus return type is a container, not a single
  110. /// pointer.
  111. ///
  112. /// @param hwaddr hardware address of the client
  113. ///
  114. /// @return lease collection
  115. ///
  116. /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
  117. /// fit into the space allocated for the result. This indicates a
  118. /// programming error.
  119. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  120. /// failed.
  121. virtual Lease4Collection getLease4(const isc::dhcp::HWAddr& hwaddr) const;
  122. /// @brief Returns existing IPv4 leases for specified hardware address
  123. /// and a subnet
  124. ///
  125. /// There can be at most one lease for a given HW address in a single
  126. /// pool, so this method with either return a single lease or NULL.
  127. ///
  128. /// @param hwaddr hardware address of the client
  129. /// @param subnet_id identifier of the subnet that lease must belong to
  130. ///
  131. /// @return a pointer to the lease (or NULL if a lease is not found)
  132. ///
  133. /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
  134. /// fit into the space allocated for the result. This indicates a
  135. /// programming error.
  136. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  137. /// failed.
  138. virtual Lease4Ptr getLease4(const isc::dhcp::HWAddr& hwaddr,
  139. SubnetID subnet_id) const;
  140. /// @brief Returns existing IPv4 lease for specified client-id
  141. ///
  142. /// Although in the usual case there will be only one lease, for mobile
  143. /// clients or clients with multiple static/fixed/reserved leases there
  144. /// can be more than one. Thus return type is a container, not a single
  145. /// pointer.
  146. ///
  147. /// @param clientid client identifier
  148. ///
  149. /// @return lease collection
  150. ///
  151. /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
  152. /// fit into the space allocated for the result. This indicates a
  153. /// programming error.
  154. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  155. /// failed.
  156. virtual Lease4Collection getLease4(const ClientId& clientid) const;
  157. /// @brief Returns IPv4 lease for the specified client identifier, HW
  158. /// address and subnet identifier.
  159. ///
  160. /// @param client_id A client identifier.
  161. /// @param hwaddr Hardware address.
  162. /// @param subnet_id A subnet identifier.
  163. ///
  164. /// @return A pointer to the lease or NULL if the lease is not found.
  165. /// @throw isc::NotImplemented On every call as this function is currently
  166. /// not implemented for the MySQL backend.
  167. virtual Lease4Ptr getLease4(const ClientId& client_id, const HWAddr& hwaddr,
  168. SubnetID subnet_id) const;
  169. /// @brief Returns existing IPv4 lease for specified client-id
  170. ///
  171. /// There can be at most one lease for a given HW address in a single
  172. /// pool, so this method with either return a single lease or NULL.
  173. ///
  174. /// @param clientid client identifier
  175. /// @param subnet_id identifier of the subnet that lease must belong to
  176. ///
  177. /// @return a pointer to the lease (or NULL if a lease is not found)
  178. ///
  179. /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
  180. /// fit into the space allocated for the result. This indicates a
  181. /// programming error.
  182. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  183. /// failed.
  184. virtual Lease4Ptr getLease4(const ClientId& clientid,
  185. SubnetID subnet_id) const;
  186. /// @brief Returns existing IPv6 lease for a given IPv6 address.
  187. ///
  188. /// For a given address, we assume that there will be only one lease.
  189. /// The assumption here is that there will not be site or link-local
  190. /// addresses used, so there is no way of having address duplication.
  191. ///
  192. /// @param type specifies lease type: (NA, TA or PD)
  193. /// @param addr address of the searched lease
  194. ///
  195. /// @return smart pointer to the lease (or NULL if a lease is not found)
  196. ///
  197. /// @throw isc::BadValue record retrieved from database had an invalid
  198. /// lease type field.
  199. /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
  200. /// fit into the space allocated for the result. This indicates a
  201. /// programming error.
  202. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  203. /// failed.
  204. virtual Lease6Ptr getLease6(Lease::Type type,
  205. const isc::asiolink::IOAddress& addr) const;
  206. /// @brief Returns existing IPv6 leases for a given DUID+IA combination
  207. ///
  208. /// Although in the usual case there will be only one lease, for mobile
  209. /// clients or clients with multiple static/fixed/reserved leases there
  210. /// can be more than one. Thus return type is a container, not a single
  211. /// pointer.
  212. ///
  213. /// @param type specifies lease type: (NA, TA or PD)
  214. /// @param duid client DUID
  215. /// @param iaid IA identifier
  216. ///
  217. /// @return smart pointer to the lease (or NULL if a lease is not found)
  218. ///
  219. /// @throw isc::BadValue record retrieved from database had an invalid
  220. /// lease type field.
  221. /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
  222. /// fit into the space allocated for the result. This indicates a
  223. /// programming error.
  224. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  225. /// failed.
  226. virtual Lease6Collection getLeases6(Lease::Type type, const DUID& duid,
  227. uint32_t iaid) const;
  228. /// @brief Returns existing IPv6 lease for a given DUID+IA combination
  229. ///
  230. /// @param type specifies lease type: (NA, TA or PD)
  231. /// @param duid client DUID
  232. /// @param iaid IA identifier
  233. /// @param subnet_id subnet id of the subnet the lease belongs to
  234. ///
  235. /// @return lease collection (may be empty if no lease is found)
  236. ///
  237. /// @throw isc::BadValue record retrieved from database had an invalid
  238. /// lease type field.
  239. /// @throw isc::dhcp::DataTruncation Data was truncated on retrieval to
  240. /// fit into the space allocated for the result. This indicates a
  241. /// programming error.
  242. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  243. /// failed.
  244. virtual Lease6Collection getLeases6(Lease::Type type, const DUID& duid,
  245. uint32_t iaid, SubnetID subnet_id) const;
  246. /// @brief Returns a collection of expired DHCPv6 leases.
  247. ///
  248. /// This method returns at most @c max_leases expired leases. The leases
  249. /// returned haven't been reclaimed, i.e. the database query must exclude
  250. /// reclaimed leases from the results returned.
  251. ///
  252. /// @param [out] expired_leases A container to which expired leases returned
  253. /// by the database backend are added.
  254. /// @param max_leases A maximum number of leases to be returned. If this
  255. /// value is set to 0, all expired (but not reclaimed) leases are returned.
  256. virtual void getExpiredLeases6(Lease6Collection& expired_leases,
  257. const size_t max_leases) const;
  258. /// @brief Returns a collection of expired DHCPv4 leases.
  259. ///
  260. /// This method returns at most @c max_leases expired leases. The leases
  261. /// returned haven't been reclaimed, i.e. the database query must exclude
  262. /// reclaimed leases from the results returned.
  263. ///
  264. /// @param [out] expired_leases A container to which expired leases returned
  265. /// by the database backend are added.
  266. /// @param max_leases A maximum number of leases to be returned. If this
  267. /// value is set to 0, all expired (but not reclaimed) leases are returned.
  268. virtual void getExpiredLeases4(Lease4Collection& expired_leases,
  269. const size_t max_leases) const;
  270. /// @brief Updates IPv4 lease.
  271. ///
  272. /// Updates the record of the lease in the database (as identified by the
  273. /// address) with the data in the passed lease object.
  274. ///
  275. /// @param lease4 The lease to be updated.
  276. ///
  277. /// @throw isc::dhcp::NoSuchLease Attempt to update a lease that did not
  278. /// exist.
  279. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  280. /// failed.
  281. virtual void updateLease4(const Lease4Ptr& lease4);
  282. /// @brief Updates IPv6 lease.
  283. ///
  284. /// Updates the record of the lease in the database (as identified by the
  285. /// address) with the data in the passed lease object.
  286. ///
  287. /// @param lease6 The lease to be updated.
  288. ///
  289. /// @throw isc::dhcp::NoSuchLease Attempt to update a lease that did not
  290. /// exist.
  291. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  292. /// failed.
  293. virtual void updateLease6(const Lease6Ptr& lease6);
  294. /// @brief Deletes a lease.
  295. ///
  296. /// @param addr Address of the lease to be deleted. This can be an IPv4
  297. /// address or an IPv6 address.
  298. ///
  299. /// @return true if deletion was successful, false if no such lease exists
  300. ///
  301. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  302. /// failed.
  303. virtual bool deleteLease(const isc::asiolink::IOAddress& addr);
  304. /// @brief Deletes all expired-reclaimed DHCPv4 leases.
  305. ///
  306. /// @param secs Number of seconds since expiration of leases before
  307. /// they can be removed. Leases which have expired later than this
  308. /// time will not be deleted.
  309. ///
  310. /// @return Number of leases deleted.
  311. virtual uint64_t deleteExpiredReclaimedLeases4(const uint32_t secs);
  312. /// @brief Deletes all expired-reclaimed DHCPv6 leases.
  313. ///
  314. /// @param secs Number of seconds since expiration of leases before
  315. /// they can be removed. Leases which have expired later than this
  316. /// time will not be deleted.
  317. ///
  318. /// @return Number of leases deleted.
  319. virtual uint64_t deleteExpiredReclaimedLeases6(const uint32_t secs);
  320. /// @brief Return backend type
  321. ///
  322. /// Returns the type of the backend (e.g. "mysql", "memfile" etc.)
  323. ///
  324. /// @return Type of the backend.
  325. virtual std::string getType() const {
  326. return (std::string("mysql"));
  327. }
  328. /// @brief Returns backend name.
  329. ///
  330. /// Each backend have specific name, e.g. "mysql" or "sqlite".
  331. ///
  332. /// @return Name of the backend.
  333. virtual std::string getName() const;
  334. /// @brief Returns description of the backend.
  335. ///
  336. /// This description may be multiline text that describes the backend.
  337. ///
  338. /// @return Description of the backend.
  339. virtual std::string getDescription() const;
  340. /// @brief Returns backend version.
  341. ///
  342. /// @return Version number as a pair of unsigned integers. "first" is the
  343. /// major version number, "second" the minor number.
  344. ///
  345. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  346. /// failed.
  347. virtual std::pair<uint32_t, uint32_t> getVersion() const;
  348. /// @brief Commit Transactions
  349. ///
  350. /// Commits all pending database operations. On databases that don't
  351. /// support transactions, this is a no-op.
  352. ///
  353. /// @throw DbOperationError Iif the commit failed.
  354. virtual void commit();
  355. /// @brief Rollback Transactions
  356. ///
  357. /// Rolls back all pending database operations. On databases that don't
  358. /// support transactions, this is a no-op.
  359. ///
  360. /// @throw DbOperationError If the rollback failed.
  361. virtual void rollback();
  362. ///@{
  363. /// The following methods are used to convert between times and time
  364. /// intervals stored in the Lease object, and the times stored in the
  365. /// database. The reason for the difference is because in the DHCP server,
  366. /// the cltt (Client Time Since Last Transmission) is the natural data; in
  367. /// the lease file - which may be read by the user - it is the expiry time
  368. /// of the lease.
  369. /// @brief Convert time_t value to database time.
  370. ///
  371. /// @param input_time A time_t value representing time.
  372. /// @param output_time Reference to MYSQL_TIME object where converted time
  373. /// will be put.
  374. static
  375. void convertToDatabaseTime(const time_t input_time, MYSQL_TIME& output_time);
  376. /// @brief Convert Lease Time to Database Times
  377. ///
  378. /// Within the DHCP servers, times are stored as client last transmit time
  379. /// and valid lifetime. In the database, the information is stored as
  380. /// valid lifetime and "expire" (time of expiry of the lease). They are
  381. /// related by the equation:
  382. ///
  383. /// - expire = client last transmit time + valid lifetime
  384. ///
  385. /// This method converts from the times in the lease object into times
  386. /// able to be added to the database.
  387. ///
  388. /// @param cltt Client last transmit time
  389. /// @param valid_lifetime Valid lifetime
  390. /// @param expire Reference to MYSQL_TIME object where the expiry time of
  391. /// the lease will be put.
  392. ///
  393. /// @throw isc::BadValue if the sum of the calculated expiration time is
  394. /// greater than the value of @c LeaseMgr::MAX_DB_TIME.
  395. static
  396. void convertToDatabaseTime(const time_t cltt, const uint32_t valid_lifetime,
  397. MYSQL_TIME& expire);
  398. /// @brief Convert Database Time to Lease Times
  399. ///
  400. /// Within the database, time is stored as "expire" (time of expiry of the
  401. /// lease) and valid lifetime. In the DHCP server, the information is
  402. /// stored client last transmit time and valid lifetime. These are related
  403. /// by the equation:
  404. ///
  405. /// - client last transmit time = expire - valid_lifetime
  406. ///
  407. /// This method converts from the times in the database into times
  408. /// able to be inserted into the lease object.
  409. ///
  410. /// @param expire Reference to MYSQL_TIME object from where the expiry
  411. /// time of the lease is taken.
  412. /// @param valid_lifetime lifetime of the lease.
  413. /// @param cltt Reference to location where client last transmit time
  414. /// is put.
  415. static
  416. void convertFromDatabaseTime(const MYSQL_TIME& expire,
  417. uint32_t valid_lifetime, time_t& cltt);
  418. ///@}
  419. /// @brief Statement Tags
  420. ///
  421. /// The contents of the enum are indexes into the list of SQL statements
  422. enum StatementIndex {
  423. DELETE_LEASE4, // Delete from lease4 by address
  424. DELETE_LEASE4_STATE_EXPIRED, // Delete expired lease4 in a given state
  425. DELETE_LEASE6, // Delete from lease6 by address
  426. DELETE_LEASE6_STATE_EXPIRED, // Delete expired lease6 in a given state
  427. GET_LEASE4_ADDR, // Get lease4 by address
  428. GET_LEASE4_CLIENTID, // Get lease4 by client ID
  429. GET_LEASE4_CLIENTID_SUBID, // Get lease4 by client ID & subnet ID
  430. GET_LEASE4_HWADDR, // Get lease4 by HW address
  431. GET_LEASE4_HWADDR_SUBID, // Get lease4 by HW address & subnet ID
  432. GET_LEASE4_EXPIRE, // Get lease4 by expiration.
  433. GET_LEASE6_ADDR, // Get lease6 by address
  434. GET_LEASE6_DUID_IAID, // Get lease6 by DUID and IAID
  435. GET_LEASE6_DUID_IAID_SUBID, // Get lease6 by DUID, IAID and subnet ID
  436. GET_LEASE6_EXPIRE, // Get lease6 by expiration.
  437. GET_VERSION, // Obtain version number
  438. INSERT_LEASE4, // Add entry to lease4 table
  439. INSERT_LEASE6, // Add entry to lease6 table
  440. UPDATE_LEASE4, // Update a Lease4 entry
  441. UPDATE_LEASE6, // Update a Lease6 entry
  442. NUM_STATEMENTS // Number of statements
  443. };
  444. private:
  445. /// @brief Add Lease Common Code
  446. ///
  447. /// This method performs the common actions for both flavours (V4 and V6)
  448. /// of the addLease method. It binds the contents of the lease object to
  449. /// the prepared statement and adds it to the database.
  450. ///
  451. /// @param stindex Index of statemnent being executed
  452. /// @param bind MYSQL_BIND array that has been created for the type
  453. /// of lease in question.
  454. ///
  455. /// @return true if the lease was added, false if it was not added because
  456. /// a lease with that address already exists in the database.
  457. ///
  458. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  459. /// failed.
  460. bool addLeaseCommon(StatementIndex stindex, std::vector<MYSQL_BIND>& bind);
  461. /// @brief Get Lease Collection Common Code
  462. ///
  463. /// This method performs the common actions for obtaining multiple leases
  464. /// from the database.
  465. ///
  466. /// @param stindex Index of statement being executed
  467. /// @param bind MYSQL_BIND array for input parameters
  468. /// @param exchange Exchange object to use
  469. /// @param lease LeaseCollection object returned. Note that any leases in
  470. /// the collection when this method is called are not erased: the
  471. /// new data is appended to the end.
  472. /// @param single If true, only a single data item is to be retrieved.
  473. /// If more than one is present, a MultipleRecords exception will
  474. /// be thrown.
  475. ///
  476. /// @throw isc::dhcp::BadValue Data retrieved from the database was invalid.
  477. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  478. /// failed.
  479. /// @throw isc::dhcp::MultipleRecords Multiple records were retrieved
  480. /// from the database where only one was expected.
  481. template <typename Exchange, typename LeaseCollection>
  482. void getLeaseCollection(StatementIndex stindex, MYSQL_BIND* bind,
  483. Exchange& exchange, LeaseCollection& result,
  484. bool single = false) const;
  485. /// @brief Get Lease Collection
  486. ///
  487. /// Gets a collection of Lease4 objects. This is just an interface to
  488. /// the get lease collection common code.
  489. ///
  490. /// @param stindex Index of statement being executed
  491. /// @param bind MYSQL_BIND array for input parameters
  492. /// @param lease LeaseCollection object returned. Note that any leases in
  493. /// the collection when this method is called are not erased: the
  494. /// new data is appended to the end.
  495. ///
  496. /// @throw isc::dhcp::BadValue Data retrieved from the database was invalid.
  497. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  498. /// failed.
  499. /// @throw isc::dhcp::MultipleRecords Multiple records were retrieved
  500. /// from the database where only one was expected.
  501. void getLeaseCollection(StatementIndex stindex, MYSQL_BIND* bind,
  502. Lease4Collection& result) const {
  503. getLeaseCollection(stindex, bind, exchange4_, result);
  504. }
  505. /// @brief Get Lease Collection
  506. ///
  507. /// Gets a collection of Lease6 objects. This is just an interface to
  508. /// the get lease collection common code.
  509. ///
  510. /// @param stindex Index of statement being executed
  511. /// @param bind MYSQL_BIND array for input parameters
  512. /// @param lease LeaseCollection object returned. Note that any existing
  513. /// data in the collection is erased first.
  514. ///
  515. /// @throw isc::dhcp::BadValue Data retrieved from the database was invalid.
  516. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  517. /// failed.
  518. /// @throw isc::dhcp::MultipleRecords Multiple records were retrieved
  519. /// from the database where only one was expected.
  520. void getLeaseCollection(StatementIndex stindex, MYSQL_BIND* bind,
  521. Lease6Collection& result) const {
  522. getLeaseCollection(stindex, bind, exchange6_, result);
  523. }
  524. /// @brief Get Lease4 Common Code
  525. ///
  526. /// This method performs the common actions for the various getLease4()
  527. /// methods. It acts as an interface to the getLeaseCollection() method,
  528. /// but retrieveing only a single lease.
  529. ///
  530. /// @param stindex Index of statement being executed
  531. /// @param bind MYSQL_BIND array for input parameters
  532. /// @param lease Lease4 object returned
  533. void getLease(StatementIndex stindex, MYSQL_BIND* bind,
  534. Lease4Ptr& result) const;
  535. /// @brief Get Lease6 Common Code
  536. ///
  537. /// This method performs the common actions for the various getLease46)
  538. /// methods. It acts as an interface to the getLeaseCollection() method,
  539. /// but retrieveing only a single lease.
  540. ///
  541. /// @param stindex Index of statement being executed
  542. /// @param bind MYSQL_BIND array for input parameters
  543. /// @param lease Lease6 object returned
  544. void getLease(StatementIndex stindex, MYSQL_BIND* bind,
  545. Lease6Ptr& result) const;
  546. /// @brief Get expired leases common code.
  547. ///
  548. /// This method retrieves expired and not reclaimed leases from the
  549. /// lease database. The returned leases are ordered by the expiration
  550. /// time. The maximum number of leases to be returned is specified
  551. /// as an argument.
  552. ///
  553. /// @param [out] expired_leases Reference to the container where the
  554. /// retrieved leases are put.
  555. /// @param max_leases Maximum number of leases to be returned.
  556. /// @param statement_index One of the @c GET_LEASE4_EXPIRE or
  557. /// @c GET_LEASE6_EXPIRE.
  558. ///
  559. /// @tparam One of the @c Lease4Collection or @c Lease6Collection.
  560. template<typename LeaseCollection>
  561. void getExpiredLeasesCommon(LeaseCollection& expired_leases,
  562. const size_t max_leases,
  563. StatementIndex statement_index) const;
  564. /// @brief Update lease common code
  565. ///
  566. /// Holds the common code for updating a lease. It binds the parameters
  567. /// to the prepared statement, executes it, then checks how many rows
  568. /// were affected.
  569. ///
  570. /// @param stindex Index of prepared statement to be executed
  571. /// @param bind Array of MYSQL_BIND objects representing the parameters.
  572. /// (Note that the number is determined by the number of parameters
  573. /// in the statement.)
  574. /// @param lease Pointer to the lease object whose record is being updated.
  575. ///
  576. /// @throw NoSuchLease Could not update a lease because no lease matches
  577. /// the address given.
  578. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  579. /// failed.
  580. template <typename LeasePtr>
  581. void updateLeaseCommon(StatementIndex stindex, MYSQL_BIND* bind,
  582. const LeasePtr& lease);
  583. /// @brief Delete lease common code
  584. ///
  585. /// Holds the common code for deleting a lease. It binds the parameters
  586. /// to the prepared statement, executes the statement and checks to
  587. /// see how many rows were deleted.
  588. ///
  589. /// @param stindex Index of prepared statement to be executed
  590. /// @param bind Array of MYSQL_BIND objects representing the parameters.
  591. /// (Note that the number is determined by the number of parameters
  592. /// in the statement.)
  593. ///
  594. /// @return Number of deleted leases.
  595. ///
  596. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  597. /// failed.
  598. uint64_t deleteLeaseCommon(StatementIndex stindex, MYSQL_BIND* bind);
  599. /// @brief Delete expired-reclaimed leases.
  600. ///
  601. /// @param secs Number of seconds since expiration of leases before
  602. /// they can be removed. Leases which have expired later than this
  603. /// time will not be deleted.
  604. /// @param statement_index One of the @c DELETE_LEASE4_STATE_EXPIRED or
  605. /// @c DELETE_LEASE6_STATE_EXPIRED.
  606. ///
  607. /// @return Number of leases deleted.
  608. uint64_t deleteExpiredReclaimedLeasesCommon(const uint32_t secs,
  609. StatementIndex statement_index);
  610. /// @brief Check Error and Throw Exception
  611. ///
  612. /// Virtually all MySQL functions return a status which, if non-zero,
  613. /// indicates an error. This function centralizes the error checking
  614. /// code.
  615. ///
  616. /// It is used to determine whether or not the function succeeded, and
  617. /// in the event of failures, decide whether or not those failures are
  618. /// recoverable.
  619. ///
  620. /// If the error is recoverable, the method will throw a DbOperationError.
  621. /// In the error is deemed unrecoverable, such as a loss of connectivity
  622. /// with the server, this method will log the error and call exit(-1);
  623. ///
  624. /// @todo Calling exit() is viewed as a short term solution for Kea 1.0.
  625. /// Two tickets are likely to alter this behavior, first is #3639, which
  626. /// calls for the ability to attempt to reconnect to the database. The
  627. /// second ticket, #4087 which calls for the implementation of a generic,
  628. /// FatalException class which will propagate outward.
  629. ///
  630. /// @param status Status code: non-zero implies an error
  631. /// @param index Index of statement that caused the error
  632. /// @param what High-level description of the error
  633. ///
  634. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  635. /// failed.
  636. void checkError(int status, StatementIndex index,
  637. const char* what) const;
  638. // Members
  639. /// The exchange objects are used for transfer of data to/from the database.
  640. /// They are pointed-to objects as the contents may change in "const" calls,
  641. /// while the rest of this object does not. (At alternative would be to
  642. /// declare them as "mutable".)
  643. boost::scoped_ptr<MySqlLease4Exchange> exchange4_; ///< Exchange object
  644. boost::scoped_ptr<MySqlLease6Exchange> exchange6_; ///< Exchange object
  645. /// @brief MySQL connection
  646. MySqlConnection conn_;
  647. };
  648. }; // end of isc::dhcp namespace
  649. }; // end of isc namespace
  650. #endif // MYSQL_LEASE_MGR_H