mysql_lease_mgr.h 15 KB

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