lease_mgr.h 17 KB

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