lease_mgr.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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 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/subnet.h>
  21. #include <exceptions/exceptions.h>
  22. #include <boost/noncopyable.hpp>
  23. #include <boost/shared_ptr.hpp>
  24. #include <fstream>
  25. #include <iostream>
  26. #include <map>
  27. #include <string>
  28. #include <utility>
  29. #include <vector>
  30. /// @file lease_mgr.h
  31. /// @brief An abstract API for lease database
  32. ///
  33. /// This file contains declarations of Lease4, Lease6 and LeaseMgr classes.
  34. /// They are essential components of the interface to any database backend.
  35. /// Each concrete database backend (e.g. MySQL) will define a class derived
  36. /// from LeaseMgr class.
  37. ///
  38. /// Failover considerations:
  39. /// There are no intermediate plans to implement DHCPv4 failover
  40. /// (draft-ietf-dhc-failover-12.txt). Currently (Oct. 2012) the DHCPv6 failover
  41. /// is being defined in DHC WG in IETF (draft-ietf-dhcpv6-failover-requirements,
  42. /// draft-ietf-dhcpv6-failover-design), but the work is not advanced enough
  43. /// for implementation plans yet. v4 failover requires additional parameters
  44. /// to be kept with a lease. It is likely that v6 failover will require similar
  45. /// fields. Such implementation will require database schema extension.
  46. /// We have designed a way to expand/upgrade schemas during upgrades: a database
  47. /// schema is versioned and sanity checks about required version will be done
  48. /// upon start and/or upgrade. With this mechanism in place, we can add new
  49. /// fields to the database. In particular we can use that capability to
  50. /// introduce failover related fields.
  51. ///
  52. /// However, there is another approach that can be reliably used to provide
  53. /// failover, even without the actual failover protocol implemented. As the
  54. /// first backend will use MySQL, we will be able to use Multi-Master capability
  55. /// offered by MySQL and use two separatate Kea instances connecting to the
  56. /// same database.
  57. ///
  58. /// Nevertheless, we hope to have failover protocol eventually implemented in
  59. /// the Kea.
  60. namespace isc {
  61. namespace dhcp {
  62. /// @brief Exception thrown if name of database is not specified
  63. class NoDatabaseName : public Exception {
  64. public:
  65. NoDatabaseName(const char* file, size_t line, const char* what) :
  66. isc::Exception(file, line, what) {}
  67. };
  68. /// @brief Exception thrown on failure to open database
  69. class DbOpenError : public Exception {
  70. public:
  71. DbOpenError(const char* file, size_t line, const char* what) :
  72. isc::Exception(file, line, what) {}
  73. };
  74. /// @brief Exception thrown on failure to execute a database function
  75. class DbOperationError : public Exception {
  76. public:
  77. DbOperationError(const char* file, size_t line, const char* what) :
  78. isc::Exception(file, line, what) {}
  79. };
  80. /// @brief Multiple lease records found where one expected
  81. class MultipleRecords : public Exception {
  82. public:
  83. MultipleRecords(const char* file, size_t line, const char* what) :
  84. isc::Exception(file, line, what) {}
  85. };
  86. /// @brief Attempt to update lease that was not there
  87. class NoSuchLease : public Exception {
  88. public:
  89. NoSuchLease(const char* file, size_t line, const char* what) :
  90. isc::Exception(file, line, what) {}
  91. };
  92. /// @brief Data is truncated
  93. class DataTruncated : public Exception {
  94. public:
  95. DataTruncated(const char* file, size_t line, const char* what) :
  96. isc::Exception(file, line, what) {}
  97. };
  98. /// @brief a common structure for IPv4 and IPv6 leases
  99. ///
  100. /// This structure holds all information that is common between IPv4 and IPv6
  101. /// leases.
  102. struct Lease {
  103. /// @brief Constructor
  104. ///
  105. /// @param addr IP address
  106. /// @param t1 renewal time
  107. /// @param t2 rebinding time
  108. /// @param valid_lft Lifetime of the lease
  109. /// @param subnet_id Subnet identification
  110. /// @param cltt Client last transmission time
  111. Lease(const isc::asiolink::IOAddress& addr, uint32_t t1, uint32_t t2,
  112. uint32_t valid_lft, SubnetID subnet_id, time_t cltt);
  113. /// @brief Destructor
  114. virtual ~Lease() {}
  115. /// @brief IPv4 ot IPv6 address
  116. ///
  117. /// IPv4, IPv6 address or, in the case of a prefix delegation, the prefix.
  118. isc::asiolink::IOAddress addr_;
  119. /// @brief Renewal timer
  120. ///
  121. /// Specifies renewal time. Although technically it is a property of the
  122. /// IA container and not the address itself, since our data model does not
  123. /// define a separate IA entity, we are keeping it in the lease. In the
  124. /// case of multiple addresses/prefixes for the same IA, each must have
  125. /// consistent T1 and T2 values. This is specified in seconds since cltt.
  126. uint32_t t1_;
  127. /// @brief Rebinding timer
  128. ///
  129. /// Specifies rebinding time. Although technically it is a property of the
  130. /// IA container and not the address itself, since our data model does not
  131. /// define a separate IA entity, we are keeping it in the lease. In the
  132. /// case of multiple addresses/prefixes for the same IA, each must have
  133. /// consistent T1 and T2 values. This is specified in seconds since cltt.
  134. uint32_t t2_;
  135. /// @brief Valid lifetime
  136. ///
  137. /// Expressed as number of seconds since cltt.
  138. uint32_t valid_lft_;
  139. /// @brief Client last transmission time
  140. ///
  141. /// Specifies a timestamp giving the time when the last transmission from a
  142. /// client was received.
  143. time_t cltt_;
  144. /// @brief Subnet identifier
  145. ///
  146. /// Specifies the identification of the subnet to which the lease belongs.
  147. SubnetID subnet_id_;
  148. /// @brief Fixed lease?
  149. ///
  150. /// Fixed leases are kept after they are released/expired.
  151. bool fixed_;
  152. /// @brief Client hostname
  153. ///
  154. /// This field may be empty
  155. std::string hostname_;
  156. /// @brief Forward zone updated?
  157. ///
  158. /// Set true if the DNS AAAA record for this lease has been updated.
  159. bool fqdn_fwd_;
  160. /// @brief Reverse zone updated?
  161. ///
  162. /// Set true if the DNS PTR record for this lease has been updated.
  163. bool fqdn_rev_;
  164. /// @brief Lease comments
  165. ///
  166. /// Currently not used. It may be used for keeping comments made by the
  167. /// system administrator.
  168. std::string comments_;
  169. /// @brief Convert Lease to Printable Form
  170. ///
  171. /// @return String form of the lease
  172. virtual std::string toText() const = 0;
  173. /// @brief returns true if the lease is expired
  174. /// @return true if the lease is expired
  175. bool expired() const;
  176. };
  177. /// @brief Structure that holds a lease for IPv4 address
  178. ///
  179. /// For performance reasons it is a simple structure, not a class. If we chose
  180. /// make it a class, all fields would have to made private and getters/setters
  181. /// would be required. As this is a critical part of the code that will be used
  182. /// extensively, direct access is warranted.
  183. struct Lease4 : public Lease {
  184. /// @brief Maximum size of a hardware address
  185. static const size_t HWADDR_MAX = 20;
  186. /// @brief Address extension
  187. ///
  188. /// It is envisaged that in some cases IPv4 address will be accompanied
  189. /// with some additional data. One example of such use are Address + Port
  190. /// solutions (or Port-restricted Addresses), where several clients may get
  191. /// the same address, but different port ranges. This feature is not
  192. /// expected to be widely used. Under normal circumstances, the value
  193. /// should be 0.
  194. uint32_t ext_;
  195. /// @brief Hardware address
  196. std::vector<uint8_t> hwaddr_;
  197. /// @brief Client identifier
  198. ///
  199. /// @todo Should this be a pointer to a client ID or the ID itself?
  200. /// Compare with the DUID in the Lease6 structure.
  201. ClientIdPtr client_id_;
  202. /// @brief Constructor
  203. ///
  204. /// @param addr IPv4 address.
  205. /// @param hwaddr Hardware address buffer
  206. /// @param hwaddr_len Length of hardware address buffer
  207. /// @param clientid Client identification buffer
  208. /// @param clientid_len Length of client identification buffer
  209. /// @param valid_lft Lifetime of the lease
  210. /// @param t1 renewal time
  211. /// @param t2 rebinding time
  212. /// @param cltt Client last transmission time
  213. /// @param subnet_id Subnet identification
  214. Lease4(const isc::asiolink::IOAddress& addr, const uint8_t* hwaddr, size_t hwaddr_len,
  215. const uint8_t* clientid, size_t clientid_len, uint32_t valid_lft,
  216. uint32_t t1, uint32_t t2, time_t cltt, uint32_t subnet_id)
  217. : Lease(addr, t1, t2, valid_lft, subnet_id, cltt),
  218. ext_(0), hwaddr_(hwaddr, hwaddr + hwaddr_len) {
  219. if (clientid_len) {
  220. client_id_.reset(new ClientId(clientid, clientid_len));
  221. }
  222. }
  223. /// @brief Default constructor
  224. ///
  225. /// Initialize fields that don't have a default constructor.
  226. Lease4() : Lease(0, 0, 0, 0, 0, 0) {
  227. }
  228. /// @brief Compare two leases for equality
  229. ///
  230. /// @param other lease6 object with which to compare
  231. bool operator==(const Lease4& other) const;
  232. /// @brief Compare two leases for inequality
  233. ///
  234. /// @param other lease6 object with which to compare
  235. bool operator!=(const Lease4& other) const {
  236. return (!operator==(other));
  237. }
  238. /// @brief Convert lease to printable form
  239. ///
  240. /// @return Textual represenation of lease data
  241. virtual std::string toText() const;
  242. /// @todo: Add DHCPv4 failover related fields here
  243. };
  244. /// @brief Pointer to a Lease4 structure.
  245. typedef boost::shared_ptr<Lease4> Lease4Ptr;
  246. /// @brief A collection of IPv4 leases.
  247. typedef std::vector<Lease4Ptr> Lease4Collection;
  248. /// @brief Structure that holds a lease for IPv6 address and/or prefix
  249. ///
  250. /// For performance reasons it is a simple structure, not a class. If we chose
  251. /// make it a class, all fields would have to made private and getters/setters
  252. /// would be required. As this is a critical part of the code that will be used
  253. /// extensively, direct access is warranted.
  254. struct Lease6 : public Lease {
  255. /// @brief Type of lease contents
  256. typedef enum {
  257. LEASE_IA_NA, /// the lease contains non-temporary IPv6 address
  258. LEASE_IA_TA, /// the lease contains temporary IPv6 address
  259. LEASE_IA_PD /// the lease contains IPv6 prefix (for prefix delegation)
  260. } LeaseType;
  261. /// @brief Lease type
  262. ///
  263. /// One of normal address, temporary address, or prefix.
  264. LeaseType type_;
  265. /// @brief IPv6 prefix length
  266. ///
  267. /// This is used only for prefix delegations and is ignored otherwise.
  268. uint8_t prefixlen_;
  269. /// @brief Identity Association Identifier (IAID)
  270. ///
  271. /// DHCPv6 stores all addresses and prefixes in IA containers (IA_NA,
  272. /// IA_TA, IA_PD). All containers may appear more than once in a message.
  273. /// To differentiate between them, the IAID field is present
  274. uint32_t iaid_;
  275. /// @brief Client identifier
  276. DuidPtr duid_;
  277. /// @brief preferred lifetime
  278. ///
  279. /// This parameter specifies the preferred lifetime since the lease was
  280. /// assigned or renewed (cltt), expressed in seconds.
  281. uint32_t preferred_lft_;
  282. /// @todo: Add DHCPv6 failover related fields here
  283. /// @brief Constructor
  284. Lease6(LeaseType type, const isc::asiolink::IOAddress& addr, DuidPtr duid,
  285. uint32_t iaid, uint32_t preferred, uint32_t valid, uint32_t t1,
  286. uint32_t t2, SubnetID subnet_id, uint8_t prefixlen_ = 0);
  287. /// @brief Constructor
  288. ///
  289. /// Initialize fields that don't have a default constructor.
  290. Lease6() : Lease(isc::asiolink::IOAddress("::"), 0, 0, 0, 0, 0),
  291. type_(LEASE_IA_NA) {
  292. }
  293. /// @brief Compare two leases for equality
  294. ///
  295. /// @param other lease6 object with which to compare
  296. bool operator==(const Lease6& other) const;
  297. /// @brief Compare two leases for inequality
  298. ///
  299. /// @param other lease6 object with which to compare
  300. bool operator!=(const Lease6& other) const {
  301. return (!operator==(other));
  302. }
  303. /// @brief Convert Lease to Printable Form
  304. ///
  305. /// @return String form of the lease
  306. virtual std::string toText() const;
  307. };
  308. /// @brief Pointer to a Lease6 structure.
  309. typedef boost::shared_ptr<Lease6> Lease6Ptr;
  310. /// @brief Pointer to a const Lease6 structure.
  311. typedef boost::shared_ptr<const Lease6> ConstLease6Ptr;
  312. /// @brief A collection of IPv6 leases.
  313. typedef std::vector<Lease6Ptr> Lease6Collection;
  314. /// @brief Abstract Lease Manager
  315. ///
  316. /// This is an abstract API for lease database backends. It provides unified
  317. /// interface to all backends. As this is an abstract class, it should not
  318. /// be used directly, but rather specialized derived class should be used
  319. /// instead.
  320. ///
  321. /// As all methods are virtual, this class throws no exceptions. However,
  322. /// methods in concrete implementations of this class may throw exceptions:
  323. /// see the documentation of those classes for details.
  324. class LeaseMgr {
  325. public:
  326. /// Database configuration parameter map
  327. typedef std::map<std::string, std::string> ParameterMap;
  328. /// @brief Constructor
  329. ///
  330. /// @param parameters A data structure relating keywords and values
  331. /// concerned with the database.
  332. LeaseMgr(const ParameterMap& parameters) : parameters_(parameters)
  333. {}
  334. /// @brief Destructor
  335. virtual ~LeaseMgr()
  336. {}
  337. /// @brief Adds an IPv4 lease.
  338. ///
  339. /// @param lease lease to be added
  340. ///
  341. /// @result true if the lease was added, false if not (because a lease
  342. /// with the same address was already there).
  343. virtual bool addLease(const Lease4Ptr& lease) = 0;
  344. /// @brief Adds an IPv6 lease.
  345. ///
  346. /// @param lease lease to be added
  347. ///
  348. /// @result true if the lease was added, false if not (because a lease
  349. /// with the same address was already there).
  350. virtual bool addLease(const Lease6Ptr& lease) = 0;
  351. /// @brief Returns an IPv4 lease for specified IPv4 address
  352. ///
  353. /// This method return a lease that is associated with a given address.
  354. /// For other query types (by hardware addr, by client-id) there can be
  355. /// several leases in different subnets (e.g. for mobile clients that
  356. /// got address in different subnets). However, for a single address
  357. /// there can be only one lease, so this method returns a pointer to
  358. /// a single lease, not a container of leases.
  359. ///
  360. /// @param addr address of the searched lease
  361. ///
  362. /// @return smart pointer to the lease (or NULL if a lease is not found)
  363. virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress& addr) const = 0;
  364. /// @brief Returns existing IPv4 leases for specified hardware address.
  365. ///
  366. /// Although in the usual case there will be only one lease, for mobile
  367. /// clients or clients with multiple static/fixed/reserved leases there
  368. /// can be more than one. Thus return type is a container, not a single
  369. /// pointer.
  370. ///
  371. /// @param hwaddr hardware address of the client
  372. ///
  373. /// @return lease collection
  374. virtual Lease4Collection getLease4(const isc::dhcp::HWAddr& hwaddr) const = 0;
  375. /// @brief Returns existing IPv4 leases for specified hardware address
  376. /// and a subnet
  377. ///
  378. /// There can be at most one lease for a given HW address in a single
  379. /// pool, so this method with either return a single lease or NULL.
  380. ///
  381. /// @param hwaddr hardware address of the client
  382. /// @param subnet_id identifier of the subnet that lease must belong to
  383. ///
  384. /// @return a pointer to the lease (or NULL if a lease is not found)
  385. virtual Lease4Ptr getLease4(const isc::dhcp::HWAddr& hwaddr,
  386. SubnetID subnet_id) const = 0;
  387. /// @brief Returns existing IPv4 lease for specified client-id
  388. ///
  389. /// Although in the usual case there will be only one lease, for mobile
  390. /// clients or clients with multiple static/fixed/reserved leases there
  391. /// can be more than one. Thus return type is a container, not a single
  392. /// pointer.
  393. ///
  394. /// @param clientid client identifier
  395. ///
  396. /// @return lease collection
  397. virtual Lease4Collection getLease4(const ClientId& clientid) const = 0;
  398. /// @brief Returns existing IPv4 lease for specified client-id
  399. ///
  400. /// There can be at most one lease for a given HW address in a single
  401. /// pool, so this method with either return a single lease or NULL.
  402. ///
  403. /// @param clientid client identifier
  404. /// @param subnet_id identifier of the subnet that lease must belong to
  405. ///
  406. /// @return a pointer to the lease (or NULL if a lease is not found)
  407. virtual Lease4Ptr getLease4(const ClientId& clientid,
  408. SubnetID subnet_id) const = 0;
  409. /// @brief Returns existing IPv6 lease for a given IPv6 address.
  410. ///
  411. /// For a given address, we assume that there will be only one lease.
  412. /// The assumption here is that there will not be site or link-local
  413. /// addresses used, so there is no way of having address duplication.
  414. ///
  415. /// @param addr address of the searched lease
  416. ///
  417. /// @return smart pointer to the lease (or NULL if a lease is not found)
  418. virtual Lease6Ptr getLease6(const isc::asiolink::IOAddress& addr) const = 0;
  419. /// @brief Returns existing IPv6 leases for a given DUID+IA combination
  420. ///
  421. /// Although in the usual case there will be only one lease, for mobile
  422. /// clients or clients with multiple static/fixed/reserved leases there
  423. /// can be more than one. Thus return type is a container, not a single
  424. /// pointer.
  425. ///
  426. /// @param duid client DUID
  427. /// @param iaid IA identifier
  428. ///
  429. /// @return smart pointer to the lease (or NULL if a lease is not found)
  430. virtual Lease6Collection getLease6(const DUID& duid,
  431. uint32_t iaid) const = 0;
  432. /// @brief Returns existing IPv6 lease for a given DUID+IA combination
  433. ///
  434. /// @param duid client DUID
  435. /// @param iaid IA identifier
  436. /// @param subnet_id subnet id of the subnet the lease belongs to
  437. ///
  438. /// @return smart pointer to the lease (or NULL if a lease is not found)
  439. virtual Lease6Ptr getLease6(const DUID& duid, uint32_t iaid,
  440. SubnetID subnet_id) const = 0;
  441. /// @brief Updates IPv4 lease.
  442. ///
  443. /// @param lease4 The lease to be updated.
  444. ///
  445. /// If no such lease is present, an exception will be thrown.
  446. virtual void updateLease4(const Lease4Ptr& lease4) = 0;
  447. /// @brief Updates IPv6 lease.
  448. ///
  449. /// @param lease6 The lease to be updated.
  450. virtual void updateLease6(const Lease6Ptr& lease6) = 0;
  451. /// @brief Deletes a lease.
  452. ///
  453. /// @param addr Address of the lease to be deleted. (This can be IPv4 or
  454. /// IPv6.)
  455. ///
  456. /// @return true if deletion was successful, false if no such lease exists
  457. virtual bool deleteLease(const isc::asiolink::IOAddress& addr) = 0;
  458. /// @brief Return backend type
  459. ///
  460. /// Returns the type of the backend (e.g. "mysql", "memfile" etc.)
  461. ///
  462. /// @return Type of the backend.
  463. virtual std::string getType() const = 0;
  464. /// @brief Returns backend name.
  465. ///
  466. /// If the backend is a database, this is the name of the database or the
  467. /// file. Otherwise it is just the same as the type.
  468. ///
  469. /// @return Name of the backend.
  470. virtual std::string getName() const = 0;
  471. /// @brief Returns description of the backend.
  472. ///
  473. /// This description may be multiline text that describes the backend.
  474. ///
  475. /// @return Description of the backend.
  476. virtual std::string getDescription() const = 0;
  477. /// @brief Returns backend version.
  478. ///
  479. /// @return Version number as a pair of unsigned integers. "first" is the
  480. /// major version number, "second" the minor number.
  481. ///
  482. /// @todo: We will need to implement 3 version functions eventually:
  483. /// A. abstract API version
  484. /// B. backend version
  485. /// C. database version (stored in the database scheme)
  486. ///
  487. /// and then check that:
  488. /// B>=A and B=C (it is ok to have newer backend, as it should be backward
  489. /// compatible)
  490. /// Also if B>C, some database upgrade procedure may be triggered
  491. virtual std::pair<uint32_t, uint32_t> getVersion() const = 0;
  492. /// @brief Commit Transactions
  493. ///
  494. /// Commits all pending database operations. On databases that don't
  495. /// support transactions, this is a no-op.
  496. virtual void commit() = 0;
  497. /// @brief Rollback Transactions
  498. ///
  499. /// Rolls back all pending database operations. On databases that don't
  500. /// support transactions, this is a no-op.
  501. virtual void rollback() = 0;
  502. /// @todo: Add host management here
  503. /// As host reservation is outside of scope for 2012, support for hosts
  504. /// is currently postponed.
  505. /// @brief returns value of the parameter
  506. virtual std::string getParameter(const std::string& name) const;
  507. private:
  508. /// @brief list of parameters passed in dbconfig
  509. ///
  510. /// That will be mostly used for storing database name, username,
  511. /// password and other parameters required for DB access. It is not
  512. /// intended to keep any DHCP-related parameters.
  513. ParameterMap parameters_;
  514. };
  515. }; // end of isc::dhcp namespace
  516. }; // end of isc namespace
  517. #endif // LEASE_MGR_H