lease_mgr.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. // Copyright (C) 2012-2013 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 LEASE_MGR_H
  15. #define LEASE_MGR_H
  16. #include <asiolink/io_address.h>
  17. #include <dhcp/duid.h>
  18. #include <dhcp/option.h>
  19. #include <dhcp/hwaddr.h>
  20. #include <dhcpsrv/lease.h>
  21. #include <dhcpsrv/subnet.h>
  22. #include <exceptions/exceptions.h>
  23. #include <boost/noncopyable.hpp>
  24. #include <boost/shared_ptr.hpp>
  25. #include <fstream>
  26. #include <iostream>
  27. #include <map>
  28. #include <string>
  29. #include <utility>
  30. #include <vector>
  31. /// @file lease_mgr.h
  32. /// @brief An abstract API for lease database
  33. ///
  34. /// This file contains declarations of Lease4, Lease6 and LeaseMgr classes.
  35. /// They are essential components of the interface to any database backend.
  36. /// Each concrete database backend (e.g. MySQL) will define a class derived
  37. /// from LeaseMgr class.
  38. ///
  39. /// Failover considerations:
  40. /// There are no intermediate plans to implement DHCPv4 failover
  41. /// (draft-ietf-dhc-failover-12.txt). Currently (Oct. 2012) the DHCPv6 failover
  42. /// is being defined in DHC WG in IETF (draft-ietf-dhcpv6-failover-requirements,
  43. /// draft-ietf-dhcpv6-failover-design), but the work is not advanced enough
  44. /// for implementation plans yet. v4 failover requires additional parameters
  45. /// to be kept with a lease. It is likely that v6 failover will require similar
  46. /// fields. Such implementation will require database schema extension.
  47. /// We have designed a way to expand/upgrade schemas during upgrades: a database
  48. /// schema is versioned and sanity checks about required version will be done
  49. /// upon start and/or upgrade. With this mechanism in place, we can add new
  50. /// fields to the database. In particular we can use that capability to
  51. /// introduce failover related fields.
  52. ///
  53. /// However, there is another approach that can be reliably used to provide
  54. /// failover, even without the actual failover protocol implemented. As the
  55. /// first backend will use MySQL, we will be able to use Multi-Master capability
  56. /// offered by MySQL and use two separatate Kea instances connecting to the
  57. /// same database.
  58. ///
  59. /// Nevertheless, we hope to have failover protocol eventually implemented in
  60. /// the Kea.
  61. namespace isc {
  62. namespace dhcp {
  63. /// @brief Exception thrown if name of database is not specified
  64. class NoDatabaseName : public Exception {
  65. public:
  66. NoDatabaseName(const char* file, size_t line, const char* what) :
  67. isc::Exception(file, line, what) {}
  68. };
  69. /// @brief Exception thrown on failure to open database
  70. class DbOpenError : public Exception {
  71. public:
  72. DbOpenError(const char* file, size_t line, const char* what) :
  73. isc::Exception(file, line, what) {}
  74. };
  75. /// @brief Exception thrown on failure to execute a database function
  76. class DbOperationError : public Exception {
  77. public:
  78. DbOperationError(const char* file, size_t line, const char* what) :
  79. isc::Exception(file, line, what) {}
  80. };
  81. /// @brief Multiple lease records found where one expected
  82. class MultipleRecords : public Exception {
  83. public:
  84. MultipleRecords(const char* file, size_t line, const char* what) :
  85. isc::Exception(file, line, what) {}
  86. };
  87. /// @brief Attempt to update lease that was not there
  88. class NoSuchLease : public Exception {
  89. public:
  90. NoSuchLease(const char* file, size_t line, const char* what) :
  91. isc::Exception(file, line, what) {}
  92. };
  93. /// @brief Data is truncated
  94. class DataTruncated : public Exception {
  95. public:
  96. DataTruncated(const char* file, size_t line, const char* what) :
  97. isc::Exception(file, line, what) {}
  98. };
  99. /// @brief Abstract Lease Manager
  100. ///
  101. /// This is an abstract API for lease database backends. It provides unified
  102. /// interface to all backends. As this is an abstract class, it should not
  103. /// be used directly, but rather specialized derived class should be used
  104. /// instead.
  105. ///
  106. /// As all methods are virtual, this class throws no exceptions. However,
  107. /// methods in concrete implementations of this class may throw exceptions:
  108. /// see the documentation of those classes for details.
  109. class LeaseMgr {
  110. public:
  111. /// @brief Defines maximum value for time that can be reliably stored.
  112. // If I'm still alive I'll be too old to care. You fix it.
  113. static const time_t MAX_DB_TIME;
  114. /// Database configuration parameter map
  115. typedef std::map<std::string, std::string> ParameterMap;
  116. /// @brief Constructor
  117. ///
  118. /// @param parameters A data structure relating keywords and values
  119. /// concerned with the database.
  120. LeaseMgr(const ParameterMap& parameters) : parameters_(parameters)
  121. {}
  122. /// @brief Destructor
  123. virtual ~LeaseMgr()
  124. {}
  125. /// @brief Adds an IPv4 lease.
  126. ///
  127. /// @param lease lease to be added
  128. ///
  129. /// @result true if the lease was added, false if not (because a lease
  130. /// with the same address was already there).
  131. virtual bool addLease(const isc::dhcp::Lease4Ptr& lease) = 0;
  132. /// @brief Adds an IPv6 lease.
  133. ///
  134. /// @param lease lease to be added
  135. ///
  136. /// @result true if the lease was added, false if not (because a lease
  137. /// with the same address was already there).
  138. virtual bool addLease(const Lease6Ptr& lease) = 0;
  139. /// @brief Returns an IPv4 lease for specified IPv4 address
  140. ///
  141. /// This method return a lease that is associated with a given address.
  142. /// For other query types (by hardware addr, by client-id) there can be
  143. /// several leases in different subnets (e.g. for mobile clients that
  144. /// got address in different subnets). However, for a single address
  145. /// there can be only one lease, so this method returns a pointer to
  146. /// a single lease, not a container of leases.
  147. ///
  148. /// @param addr address of the searched lease
  149. ///
  150. /// @return smart pointer to the lease (or NULL if a lease is not found)
  151. virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress& addr) const = 0;
  152. /// @brief Returns existing IPv4 leases for specified hardware address.
  153. ///
  154. /// Although in the usual case there will be only one lease, for mobile
  155. /// clients or clients with multiple static/fixed/reserved leases there
  156. /// can be more than one. Thus return type is a container, not a single
  157. /// pointer.
  158. ///
  159. /// @param hwaddr hardware address of the client
  160. ///
  161. /// @return lease collection
  162. virtual Lease4Collection getLease4(const isc::dhcp::HWAddr& hwaddr) const = 0;
  163. /// @brief Returns existing IPv4 leases for specified hardware address
  164. /// and a subnet
  165. ///
  166. /// There can be at most one lease for a given HW address in a single
  167. /// pool, so this method with either return a single lease or NULL.
  168. ///
  169. /// @param hwaddr hardware address of the client
  170. /// @param subnet_id identifier of the subnet that lease must belong to
  171. ///
  172. /// @return a pointer to the lease (or NULL if a lease is not found)
  173. virtual Lease4Ptr getLease4(const isc::dhcp::HWAddr& hwaddr,
  174. SubnetID subnet_id) const = 0;
  175. /// @brief Returns existing IPv4 lease for specified client-id
  176. ///
  177. /// Although in the usual case there will be only one lease, for mobile
  178. /// clients or clients with multiple static/fixed/reserved leases there
  179. /// can be more than one. Thus return type is a container, not a single
  180. /// pointer.
  181. ///
  182. /// @param clientid client identifier
  183. ///
  184. /// @return lease collection
  185. virtual Lease4Collection getLease4(const ClientId& clientid) const = 0;
  186. /// @brief Returns existing IPv4 lease for a given client identifier,
  187. /// HW address and subnet identifier.
  188. ///
  189. /// @todo Consider whether this function is needed or not. In the basic
  190. /// DHCPv4 server implementation it is not used by the allocation engine.
  191. ///
  192. /// @param client_id A client identifier.
  193. /// @param hwaddr Hardware address.
  194. /// @param subnet_id A subnet identifier.
  195. ///
  196. /// @return A pointer to the lease or NULL if the lease is not found.
  197. virtual Lease4Ptr getLease4(const ClientId& client_id, const HWAddr& hwaddr,
  198. SubnetID subnet_id) const = 0;
  199. /// @brief Returns existing IPv4 lease for specified client-id
  200. ///
  201. /// There can be at most one lease for a given HW address in a single
  202. /// pool, so this method with either return a single lease or NULL.
  203. ///
  204. /// @param clientid client identifier
  205. /// @param subnet_id identifier of the subnet that lease must belong to
  206. ///
  207. /// @return a pointer to the lease (or NULL if a lease is not found)
  208. virtual Lease4Ptr getLease4(const ClientId& clientid,
  209. SubnetID subnet_id) const = 0;
  210. /// @brief Returns existing IPv6 lease for a given IPv6 address.
  211. ///
  212. /// For a given address, we assume that there will be only one lease.
  213. /// The assumption here is that there will not be site or link-local
  214. /// addresses used, so there is no way of having address duplication.
  215. ///
  216. /// @param type specifies lease type: (NA, TA or PD)
  217. /// @param addr address of the searched lease
  218. ///
  219. /// @return smart pointer to the lease (or NULL if a lease is not found)
  220. virtual Lease6Ptr getLease6(Lease::Type type,
  221. const isc::asiolink::IOAddress& addr) const = 0;
  222. /// @brief Returns existing IPv6 leases for a given DUID+IA combination
  223. ///
  224. /// Although in the usual case there will be only one lease, for mobile
  225. /// clients or clients with multiple static/fixed/reserved leases there
  226. /// can be more than one. Thus return type is a container, not a single
  227. /// pointer.
  228. ///
  229. /// @param type specifies lease type: (NA, TA or PD)
  230. /// @param duid client DUID
  231. /// @param iaid IA identifier
  232. ///
  233. /// @return Lease collection (may be empty if no lease is found)
  234. virtual Lease6Collection getLeases6(Lease::Type type, const DUID& duid,
  235. uint32_t iaid) const = 0;
  236. /// @brief Returns existing IPv6 lease for a given DUID+IA combination
  237. ///
  238. /// There may be more than one address, temp. address or prefix
  239. /// for specified duid/iaid/subnet-id tuple.
  240. ///
  241. /// @param type specifies lease type: (NA, TA or PD)
  242. /// @param duid client DUID
  243. /// @param iaid IA identifier
  244. /// @param subnet_id subnet id of the subnet the lease belongs to
  245. ///
  246. /// @return Lease collection (may be empty if no lease is found)
  247. virtual Lease6Collection getLeases6(Lease::Type type, const DUID& duid,
  248. uint32_t iaid, SubnetID subnet_id) const = 0;
  249. /// @brief returns zero or one IPv6 lease for a given duid+iaid+subnet_id
  250. ///
  251. /// This function is mostly intended to be used in unit-tests during the
  252. /// transition from single to multi address per IA. It may also be used
  253. /// in other cases where at most one lease is expected in the database.
  254. ///
  255. /// It is a wrapper around getLease6(), which returns a collection of
  256. /// leases. That collection can be converted into a single pointer if
  257. /// there are no leases (NULL pointer) or one lease (use that single lease).
  258. /// If there are more leases in the collection, the function will
  259. /// throw MultipleRecords exception.
  260. ///
  261. /// Note: This method is not virtual on purpose. It is common for all
  262. /// backends.
  263. ///
  264. /// @param type specifies lease type: (NA, TA or PD)
  265. /// @param duid client DUID
  266. /// @param iaid IA identifier
  267. /// @param subnet_id subnet id of the subnet the lease belongs to
  268. ///
  269. /// @throw MultipleRecords if there is more than one lease matching
  270. ///
  271. /// @return Lease pointer (or NULL if none is found)
  272. Lease6Ptr getLease6(Lease::Type type, const DUID& duid,
  273. uint32_t iaid, SubnetID subnet_id) const;
  274. /// @brief Updates IPv4 lease.
  275. ///
  276. /// @param lease4 The lease to be updated.
  277. ///
  278. /// If no such lease is present, an exception will be thrown.
  279. virtual void updateLease4(const Lease4Ptr& lease4) = 0;
  280. /// @brief Updates IPv6 lease.
  281. ///
  282. /// @param lease6 The lease to be updated.
  283. virtual void updateLease6(const Lease6Ptr& lease6) = 0;
  284. /// @brief Deletes a lease.
  285. ///
  286. /// @param addr Address of the lease to be deleted. (This can be IPv4 or
  287. /// IPv6.)
  288. ///
  289. /// @return true if deletion was successful, false if no such lease exists
  290. virtual bool deleteLease(const isc::asiolink::IOAddress& addr) = 0;
  291. /// @brief Return backend type
  292. ///
  293. /// Returns the type of the backend (e.g. "mysql", "memfile" etc.)
  294. ///
  295. /// @return Type of the backend.
  296. virtual std::string getType() const = 0;
  297. /// @brief Returns backend name.
  298. ///
  299. /// If the backend is a database, this is the name of the database or the
  300. /// file. Otherwise it is just the same as the type.
  301. ///
  302. /// @return Name of the backend.
  303. virtual std::string getName() const = 0;
  304. /// @brief Returns description of the backend.
  305. ///
  306. /// This description may be multiline text that describes the backend.
  307. ///
  308. /// @return Description of the backend.
  309. virtual std::string getDescription() const = 0;
  310. /// @brief Returns backend version.
  311. ///
  312. /// @return Version number as a pair of unsigned integers. "first" is the
  313. /// major version number, "second" the minor number.
  314. ///
  315. /// @todo: We will need to implement 3 version functions eventually:
  316. /// A. abstract API version
  317. /// B. backend version
  318. /// C. database version (stored in the database scheme)
  319. ///
  320. /// and then check that:
  321. /// B>=A and B=C (it is ok to have newer backend, as it should be backward
  322. /// compatible)
  323. /// Also if B>C, some database upgrade procedure may be triggered
  324. virtual std::pair<uint32_t, uint32_t> getVersion() const = 0;
  325. /// @brief Commit Transactions
  326. ///
  327. /// Commits all pending database operations. On databases that don't
  328. /// support transactions, this is a no-op.
  329. virtual void commit() = 0;
  330. /// @brief Rollback Transactions
  331. ///
  332. /// Rolls back all pending database operations. On databases that don't
  333. /// support transactions, this is a no-op.
  334. virtual void rollback() = 0;
  335. /// @todo: Add host management here
  336. /// As host reservation is outside of scope for 2012, support for hosts
  337. /// is currently postponed.
  338. /// @brief returns value of the parameter
  339. virtual std::string getParameter(const std::string& name) const;
  340. private:
  341. /// @brief list of parameters passed in dbconfig
  342. ///
  343. /// That will be mostly used for storing database name, username,
  344. /// password and other parameters required for DB access. It is not
  345. /// intended to keep any DHCP-related parameters.
  346. ParameterMap parameters_;
  347. };
  348. }; // end of isc::dhcp namespace
  349. }; // end of isc namespace
  350. #endif // LEASE_MGR_H