cfgmgr.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. // Copyright (C) 2012-2015 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #ifndef CFGMGR_H
  7. #define CFGMGR_H
  8. #include <asiolink/io_address.h>
  9. #include <dhcp/option.h>
  10. #include <dhcp/option_space.h>
  11. #include <dhcp/classify.h>
  12. #include <dhcpsrv/d2_client_mgr.h>
  13. #include <dhcpsrv/pool.h>
  14. #include <dhcpsrv/subnet.h>
  15. #include <dhcpsrv/srv_config.h>
  16. #include <util/buffer.h>
  17. #include <boost/shared_ptr.hpp>
  18. #include <boost/noncopyable.hpp>
  19. #include <map>
  20. #include <string>
  21. #include <vector>
  22. #include <list>
  23. namespace isc {
  24. namespace dhcp {
  25. /// @brief Exception thrown when the same interface has been specified twice.
  26. ///
  27. /// In particular, this exception is thrown when adding interface to the set
  28. /// of interfaces on which server is supposed to listen.
  29. class DuplicateListeningIface : public Exception {
  30. public:
  31. DuplicateListeningIface(const char* file, size_t line, const char* what) :
  32. isc::Exception(file, line, what) { };
  33. };
  34. /// @brief Configuration Manager
  35. ///
  36. /// This singleton class holds the whole configuration for DHCPv4 and DHCPv6
  37. /// servers. It currently holds information about zero or more subnets6.
  38. /// Each subnet may contain zero or more pools. Pool4 and Pool6 is the most
  39. /// basic "chunk" of configuration. It contains a range of assignable
  40. /// addresses.
  41. ///
  42. /// Below is a sketch of configuration inheritance (not implemented yet).
  43. /// Let's investigate the following configuration:
  44. ///
  45. /// @code
  46. /// preferred-lifetime 500;
  47. /// valid-lifetime 1000;
  48. /// subnet6 2001:db8:1::/48 {
  49. /// pool6 2001::db8:1::1 - 2001::db8:1::ff;
  50. /// };
  51. /// subnet6 2001:db8:2::/48 {
  52. /// valid-lifetime 2000;
  53. /// pool6 2001::db8:2::1 - 2001::db8:2::ff;
  54. /// };
  55. /// @endcode
  56. ///
  57. /// Parameters defined in a global scope are applicable to everything until
  58. /// they are overwritten in a smaller scope, in this case subnet6.
  59. /// In the example above, the first subnet6 has preferred lifetime of 500s
  60. /// and a valid lifetime of 1000s. The second subnet has preferred lifetime
  61. /// of 500s, but valid lifetime of 2000s.
  62. ///
  63. /// Parameter inheritance is likely to be implemented in configuration handling
  64. /// routines, so there is no storage capability in a global scope for
  65. /// subnet-specific parameters.
  66. ///
  67. /// @todo: Implement Subnet4 support (ticket #2237)
  68. /// @todo: Implement option definition support
  69. /// @todo: Implement parameter inheritance
  70. class CfgMgr : public boost::noncopyable {
  71. public:
  72. /// @brief A number of configurations held by @c CfgMgr.
  73. ///
  74. /// @todo Make it configurable.
  75. static const size_t CONFIG_LIST_SIZE;
  76. /// @brief returns a single instance of Configuration Manager
  77. ///
  78. /// CfgMgr is a singleton and this method is the only way of
  79. /// accessing it.
  80. static CfgMgr& instance();
  81. /// @brief Adds new DHCPv4 option space to the collection.
  82. ///
  83. /// @param space option space to be added.
  84. ///
  85. /// @throw isc::dhcp::InvalidOptionSpace invalid option space
  86. /// has been specified.
  87. void addOptionSpace4(const OptionSpacePtr& space);
  88. /// @brief Adds new DHCPv6 option space to the collection.
  89. ///
  90. /// @param space option space to be added.
  91. ///
  92. /// @throw isc::dhcp::InvalidOptionSpace invalid option space
  93. /// has been specified.
  94. void addOptionSpace6(const OptionSpacePtr& space);
  95. /// @brief Return option spaces for DHCPv4.
  96. ///
  97. /// @return A collection of option spaces.
  98. const OptionSpaceCollection& getOptionSpaces4() const {
  99. return (spaces4_);
  100. }
  101. /// @brief Return option spaces for DHCPv6.
  102. ///
  103. /// @return A collection of option spaces.
  104. const OptionSpaceCollection& getOptionSpaces6() const {
  105. return (spaces6_);
  106. }
  107. /// @brief returns path do the data directory
  108. ///
  109. /// This method returns a path to writeable directory that DHCP servers
  110. /// can store data in.
  111. /// @return data directory
  112. std::string getDataDir() const;
  113. /// @brief Sets new data directory.
  114. ///
  115. /// @param datadir New data directory.
  116. void setDataDir(const std::string& datadir);
  117. /// @brief Sets whether server should send back client-id in DHCPv4
  118. ///
  119. /// This is a compatibility flag. The default (true) is compliant with
  120. /// RFC6842. False is for backward compatibility.
  121. ///
  122. /// @param echo should the client-id be sent or not
  123. void echoClientId(const bool echo) {
  124. echo_v4_client_id_ = echo;
  125. }
  126. /// @brief Returns whether server should send back client-id in DHCPv4.
  127. /// @return true if client-id should be returned, false otherwise.
  128. bool echoClientId() const {
  129. return (echo_v4_client_id_);
  130. }
  131. /// @brief Updates the DHCP-DDNS client configuration to the given value.
  132. ///
  133. /// @param new_config pointer to the new client configuration.
  134. ///
  135. /// @throw Underlying method(s) will throw D2ClientError if given an empty
  136. /// pointer.
  137. void setD2ClientConfig(D2ClientConfigPtr& new_config);
  138. /// @brief Convenience method for checking if DHCP-DDNS updates are enabled.
  139. ///
  140. /// @return True if the D2 configuration is enabled.
  141. bool ddnsEnabled();
  142. /// @brief Fetches the DHCP-DDNS configuration pointer.
  143. ///
  144. /// @return a reference to the current configuration pointer.
  145. const D2ClientConfigPtr& getD2ClientConfig() const;
  146. /// @brief Fetches the DHCP-DDNS manager.
  147. ///
  148. /// @return a reference to the DHCP-DDNS manager.
  149. D2ClientMgr& getD2ClientMgr();
  150. /// @name Methods managing the collection of configurations.
  151. ///
  152. /// The following methods manage the process of preparing a configuration
  153. /// without affecting a currently used configuration and then commiting
  154. /// the configuration to replace current configuration atomically.
  155. /// They also allow for keeping a history of previous configurations so
  156. /// as the @c CfgMgr can revert to the historical configuration when
  157. /// required.
  158. ///
  159. /// @todo Migrate all configuration parameters to use the model supported
  160. /// by these functions.
  161. ///
  162. /// @todo Make the size of the configurations history configurable.
  163. ///
  164. //@{
  165. /// @brief Removes current, staging and all previous configurations.
  166. ///
  167. /// This function removes all configurations, including current and
  168. /// staging configurations. It creates a new current configuration with
  169. /// default settings.
  170. ///
  171. /// This function is exception safe.
  172. void clear();
  173. /// @brief Commits the staging configuration.
  174. ///
  175. /// The staging configuration becomes current configuration when this
  176. /// function is called. It removes the oldest configuration held in the
  177. /// history so as the size of the list of configuration does not exceed
  178. /// the @c CONFIG_LIST_SIZE.
  179. ///
  180. /// This function is exception safe.
  181. void commit();
  182. /// @brief Removes staging configuration.
  183. ///
  184. /// This function should be called when there is a staging configuration
  185. /// (likely created in the previous configuration attempt) but the entirely
  186. /// new configuration should be created. It removes the existing staging
  187. /// configuration and the next call to @c CfgMgr::getStagingCfg will return a
  188. /// fresh (default) configuration.
  189. ///
  190. /// This function is exception safe.
  191. void rollback();
  192. /// @brief Reverts to one of the previous configurations.
  193. ///
  194. /// This function reverts to selected previous configuration. The previous
  195. /// configuration is entirely copied to a new @c SrvConfig instance. This
  196. /// new instance has a unique sequence id (sequence id is not copied). The
  197. /// previous configuration (being copied) is not modified by this operation.
  198. ///
  199. /// The configuration to be copied is identified by the index value which
  200. /// is the distance between the current (most recent) and desired
  201. /// configuration. If the index is out of range an exception is thrown.
  202. ///
  203. /// @warning Revert operation will rollback any changes to the staging
  204. /// configuration (if it exists).
  205. ///
  206. /// @warning This function requires that the entire previous configuration
  207. /// is copied to the new configuration object. This is not working for
  208. /// some of the complex configuration objects, e.g. subnets. Hence, the
  209. /// "revert" operation is not really usable at this point.
  210. ///
  211. /// @param index A distance from the current configuration to the
  212. /// past configuration to be reverted. The minimal value is 1 which points
  213. /// to the nearest configuration.
  214. ///
  215. /// @throw isc::OutOfRange if the specified index is out of range.
  216. void revert(const size_t index);
  217. /// @brief Returns a pointer to the current configuration.
  218. ///
  219. /// This function returns pointer to the current configuration. If the
  220. /// current configuration is not set it will create a default configuration
  221. /// and return it. Current configuration returned is read-only.
  222. ///
  223. /// @return Non-null const pointer to the current configuration.
  224. ConstSrvConfigPtr getCurrentCfg();
  225. /// @brief Returns a pointer to the staging configuration.
  226. ///
  227. /// The staging configuration is used by the configuration parsers to
  228. /// create new configuration. The staging configuration doesn't affect the
  229. /// server's operation until it is committed. The staging configuration
  230. /// is a non-const object which can be modified by the caller.
  231. ///
  232. /// Multiple consecutive calls to this function return the same object
  233. /// which can be modified from various places of the code (e.g. various
  234. /// configuration parsers).
  235. ///
  236. /// @return non-null pointer to the staging configuration.
  237. SrvConfigPtr getStagingCfg();
  238. //@}
  239. /// @name Methods setting/accessing global configuration for the process.
  240. ///
  241. //@{
  242. /// @brief Sets verbose mode.
  243. ///
  244. /// @param verbose A boolean value indicating if the process should run
  245. /// in verbose (true) or non-verbose mode.
  246. void setVerbose(const bool verbose) {
  247. verbose_mode_ = verbose;
  248. }
  249. /// @brief Checks if the process has been run in verbose mode.
  250. ///
  251. /// @return true if verbose mode enabled, false otherwise.
  252. bool isVerbose() const {
  253. return (verbose_mode_);
  254. }
  255. /// @brief Sets the default logger name.
  256. ///
  257. /// This name is used in cases when a user doesn't provide a configuration
  258. /// for logger in the Kea configuration file.
  259. void setDefaultLoggerName(const std::string& name) {
  260. default_logger_name_ = name;
  261. }
  262. /// @brief Returns default logger name.
  263. std::string getDefaultLoggerName() const {
  264. return (default_logger_name_);
  265. }
  266. //@}
  267. protected:
  268. /// @brief Protected constructor.
  269. ///
  270. /// This constructor is protected for 2 reasons. First, it forbids any
  271. /// instantiations of this class (CfgMgr is a singleton). Second, it
  272. /// allows derived class to instantiate it. That is useful for testing
  273. /// purposes.
  274. CfgMgr();
  275. /// @brief virtual destructor
  276. virtual ~CfgMgr();
  277. /// @brief a container for IPv6 subnets.
  278. ///
  279. /// That is a simple vector of pointers. It does not make much sense to
  280. /// optimize access time (e.g. using a map), because typical search
  281. /// pattern will use calling inRange() method on each subnet until
  282. /// a match is found.
  283. Subnet6Collection subnets6_;
  284. private:
  285. /// @brief Checks if current configuration is created and creates it if needed.
  286. ///
  287. /// This private method is called to ensure that the current configuration
  288. /// is created. If current configuration is not set, it creates the
  289. /// default current configuration.
  290. void ensureCurrentAllocated();
  291. /// @brief Checks that the IPv6 subnet with the given id already exists.
  292. ///
  293. /// @param subnet Subnet for which this function will check if the other
  294. /// subnet with equal id already exists.
  295. /// @return true if the duplicate subnet exists.
  296. bool isDuplicate(const Subnet6& subnet) const;
  297. /// @brief Container for defined DHCPv6 option spaces.
  298. OptionSpaceCollection spaces6_;
  299. /// @brief Container for defined DHCPv4 option spaces.
  300. OptionSpaceCollection spaces4_;
  301. /// @brief directory where data files (e.g. server-id) are stored
  302. std::string datadir_;
  303. /// Indicates whether v4 server should send back client-id
  304. bool echo_v4_client_id_;
  305. /// @brief Manages the DHCP-DDNS client and its configuration.
  306. D2ClientMgr d2_client_mgr_;
  307. /// @brief Server configuration
  308. ///
  309. /// This is a structure that will hold all configuration.
  310. /// @todo: migrate all other parameters to that structure.
  311. SrvConfigPtr configuration_;
  312. /// @name Configuration List.
  313. ///
  314. //@{
  315. /// @brief Server configuration list type.
  316. typedef std::list<SrvConfigPtr> SrvConfigList;
  317. /// @brief Container holding all previous and current configurations.
  318. SrvConfigList configs_;
  319. //@}
  320. /// @brief Indicates if a process has been ran in the verbose mode.
  321. bool verbose_mode_;
  322. /// @brief Default logger name.
  323. std::string default_logger_name_;
  324. };
  325. } // namespace isc::dhcp
  326. } // namespace isc
  327. #endif // CFGMGR_H