cfgmgr.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. // Copyright (C) 2012-2017 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/srv_config.h>
  15. #include <util/buffer.h>
  16. #include <boost/shared_ptr.hpp>
  17. #include <boost/noncopyable.hpp>
  18. #include <map>
  19. #include <string>
  20. #include <vector>
  21. #include <list>
  22. namespace isc {
  23. namespace dhcp {
  24. /// @brief Exception thrown when the same interface has been specified twice.
  25. ///
  26. /// In particular, this exception is thrown when adding interface to the set
  27. /// of interfaces on which server is supposed to listen.
  28. class DuplicateListeningIface : public Exception {
  29. public:
  30. DuplicateListeningIface(const char* file, size_t line, const char* what) :
  31. isc::Exception(file, line, what) { };
  32. };
  33. /// @brief Configuration Manager
  34. ///
  35. /// This singleton class holds the whole configuration for DHCPv4 and DHCPv6
  36. /// servers.
  37. ///
  38. /// Below is a sketch of configuration inheritance.
  39. /// Let's investigate the following configuration:
  40. ///
  41. /// @code
  42. /// preferred-lifetime 500;
  43. /// valid-lifetime 1000;
  44. /// subnet6 2001:db8:1::/48 {
  45. /// pool6 2001::db8:1::1 - 2001::db8:1::ff;
  46. /// };
  47. /// subnet6 2001:db8:2::/48 {
  48. /// valid-lifetime 2000;
  49. /// pool6 2001::db8:2::1 - 2001::db8:2::ff;
  50. /// };
  51. /// @endcode
  52. ///
  53. /// Parameters defined in a global scope are applicable to everything until
  54. /// they are overwritten in a smaller scope, in this case subnet6.
  55. /// In the example above, the first subnet6 has preferred lifetime of 500s
  56. /// and a valid lifetime of 1000s. The second subnet has preferred lifetime
  57. /// of 500s, but valid lifetime of 2000s.
  58. ///
  59. /// Parameter inheritance is implemented in dedicated classes. See
  60. /// @ref isc::dhcp::SimpleParser4::deriveParameters and
  61. /// @ref isc::dhcp::SimpleParser6::deriveParameters.
  62. class CfgMgr : public boost::noncopyable {
  63. public:
  64. /// @brief A number of configurations held by @c CfgMgr.
  65. ///
  66. /// @todo Make it configurable.
  67. static const size_t CONFIG_LIST_SIZE;
  68. /// @brief returns a single instance of Configuration Manager
  69. ///
  70. /// CfgMgr is a singleton and this method is the only way of
  71. /// accessing it.
  72. static CfgMgr& instance();
  73. /// @brief returns path do the data directory
  74. ///
  75. /// This method returns a path to writable directory that DHCP servers
  76. /// can store data in.
  77. /// @return data directory
  78. std::string getDataDir() const;
  79. /// @brief Sets new data directory.
  80. ///
  81. /// @param datadir New data directory.
  82. void setDataDir(const std::string& datadir);
  83. /// @brief Updates the DHCP-DDNS client configuration to the given value.
  84. ///
  85. /// Passes the new configuration to the D2ClientMgr instance,
  86. /// d2_client_mgr_, which will attempt to apply the new configuration
  87. /// by shutting down its sender and opening a new connection per the new
  88. /// configuration (see @c D2ClientMgr::setD2ClientConfig()).
  89. ///
  90. /// @param new_config pointer to the new client configuration.
  91. ///
  92. /// @throw Underlying method(s) will throw D2ClientError if given an empty
  93. /// pointer.
  94. void setD2ClientConfig(D2ClientConfigPtr& new_config);
  95. /// @brief Convenience method for checking if DHCP-DDNS updates are enabled.
  96. ///
  97. /// @return True if the D2 configuration is enabled.
  98. bool ddnsEnabled();
  99. /// @brief Fetches the DHCP-DDNS configuration pointer.
  100. ///
  101. /// @return a reference to the current configuration pointer.
  102. const D2ClientConfigPtr& getD2ClientConfig() const;
  103. /// @brief Fetches the DHCP-DDNS manager.
  104. ///
  105. /// @return a reference to the DHCP-DDNS manager.
  106. D2ClientMgr& getD2ClientMgr();
  107. /// @name Methods managing the collection of configurations.
  108. ///
  109. /// The following methods manage the process of preparing a configuration
  110. /// without affecting a currently used configuration and then commiting
  111. /// the configuration to replace current configuration atomically.
  112. /// They also allow for keeping a history of previous configurations so
  113. /// as the @c CfgMgr can revert to the historical configuration when
  114. /// required.
  115. ///
  116. /// @todo Migrate all configuration parameters to use the model supported
  117. /// by these functions.
  118. ///
  119. /// @todo Make the size of the configurations history configurable.
  120. ///
  121. //@{
  122. /// @brief Removes current, staging and all previous configurations.
  123. ///
  124. /// This function removes all configurations, including current and
  125. /// staging configurations. It creates a new current configuration with
  126. /// default settings.
  127. ///
  128. /// This function is exception safe.
  129. void clear();
  130. /// @brief Commits the staging configuration.
  131. ///
  132. /// The staging configuration becomes current configuration when this
  133. /// function is called. It removes the oldest configuration held in the
  134. /// history so as the size of the list of configuration does not exceed
  135. /// the @c CONFIG_LIST_SIZE.
  136. ///
  137. /// This function is exception safe.
  138. void commit();
  139. /// @brief Removes staging configuration.
  140. ///
  141. /// This function should be called when there is a staging configuration
  142. /// (likely created in the previous configuration attempt) but the entirely
  143. /// new configuration should be created. It removes the existing staging
  144. /// configuration and the next call to @c CfgMgr::getStagingCfg will return a
  145. /// fresh (default) configuration.
  146. ///
  147. /// This function is exception safe.
  148. void rollback();
  149. /// @brief Reverts to one of the previous configurations.
  150. ///
  151. /// This function reverts to selected previous configuration. The previous
  152. /// configuration is entirely copied to a new @c SrvConfig instance. This
  153. /// new instance has a unique sequence id (sequence id is not copied). The
  154. /// previous configuration (being copied) is not modified by this operation.
  155. ///
  156. /// The configuration to be copied is identified by the index value which
  157. /// is the distance between the current (most recent) and desired
  158. /// configuration. If the index is out of range an exception is thrown.
  159. ///
  160. /// @warning Revert operation will rollback any changes to the staging
  161. /// configuration (if it exists).
  162. ///
  163. /// @warning This function requires that the entire previous configuration
  164. /// is copied to the new configuration object. This is not working for
  165. /// some of the complex configuration objects, e.g. subnets. Hence, the
  166. /// "revert" operation is not really usable at this point.
  167. ///
  168. /// @param index A distance from the current configuration to the
  169. /// past configuration to be reverted. The minimal value is 1 which points
  170. /// to the nearest configuration.
  171. ///
  172. /// @throw isc::OutOfRange if the specified index is out of range.
  173. void revert(const size_t index);
  174. /// @brief Returns a pointer to the current configuration.
  175. ///
  176. /// This function returns pointer to the current configuration. If the
  177. /// current configuration is not set it will create a default configuration
  178. /// and return it. Current configuration returned is read-only.
  179. ///
  180. /// @return Non-null const pointer to the current configuration.
  181. ConstSrvConfigPtr getCurrentCfg();
  182. /// @brief Returns a pointer to the staging configuration.
  183. ///
  184. /// The staging configuration is used by the configuration parsers to
  185. /// create new configuration. The staging configuration doesn't affect the
  186. /// server's operation until it is committed. The staging configuration
  187. /// is a non-const object which can be modified by the caller.
  188. ///
  189. /// Multiple consecutive calls to this function return the same object
  190. /// which can be modified from various places of the code (e.g. various
  191. /// configuration parsers).
  192. ///
  193. /// @return non-null pointer to the staging configuration.
  194. SrvConfigPtr getStagingCfg();
  195. //@}
  196. /// @name Methods setting/accessing global configuration for the process.
  197. ///
  198. //@{
  199. /// @brief Sets verbose mode.
  200. ///
  201. /// @param verbose A boolean value indicating if the process should run
  202. /// in verbose (true) or non-verbose mode.
  203. void setVerbose(const bool verbose) {
  204. verbose_mode_ = verbose;
  205. }
  206. /// @brief Checks if the process has been run in verbose mode.
  207. ///
  208. /// @return true if verbose mode enabled, false otherwise.
  209. bool isVerbose() const {
  210. return (verbose_mode_);
  211. }
  212. /// @brief Sets the default logger name.
  213. ///
  214. /// This name is used in cases when a user doesn't provide a configuration
  215. /// for logger in the Kea configuration file.
  216. void setDefaultLoggerName(const std::string& name) {
  217. default_logger_name_ = name;
  218. }
  219. /// @brief Returns default logger name.
  220. std::string getDefaultLoggerName() const {
  221. return (default_logger_name_);
  222. }
  223. /// @brief Sets address family (AF_INET or AF_INET6)
  224. void setFamily(uint16_t family) {
  225. family_ = family == AF_INET ? AF_INET : AF_INET6;
  226. }
  227. /// @brief Returns address family.
  228. uint16_t getFamily() const {
  229. return (family_);
  230. }
  231. //@}
  232. protected:
  233. /// @brief Protected constructor.
  234. ///
  235. /// This constructor is protected for 2 reasons. First, it forbids any
  236. /// instantiations of this class (CfgMgr is a singleton). Second, it
  237. /// allows derived class to instantiate it. That is useful for testing
  238. /// purposes.
  239. CfgMgr();
  240. /// @brief virtual destructor
  241. virtual ~CfgMgr();
  242. private:
  243. /// @brief Checks if current configuration is created and creates it if needed.
  244. ///
  245. /// This private method is called to ensure that the current configuration
  246. /// is created. If current configuration is not set, it creates the
  247. /// default current configuration.
  248. void ensureCurrentAllocated();
  249. /// @brief directory where data files (e.g. server-id) are stored
  250. std::string datadir_;
  251. /// @brief Manages the DHCP-DDNS client and its configuration.
  252. D2ClientMgr d2_client_mgr_;
  253. /// @brief Server configuration
  254. ///
  255. /// This is a structure that will hold all configuration.
  256. /// @todo: migrate all other parameters to that structure.
  257. SrvConfigPtr configuration_;
  258. /// @name Configuration List.
  259. ///
  260. //@{
  261. /// @brief Server configuration list type.
  262. typedef std::list<SrvConfigPtr> SrvConfigList;
  263. /// @brief Container holding all previous and current configurations.
  264. SrvConfigList configs_;
  265. //@}
  266. /// @brief Indicates if a process has been ran in the verbose mode.
  267. bool verbose_mode_;
  268. /// @brief Default logger name.
  269. std::string default_logger_name_;
  270. /// @brief Address family.
  271. uint16_t family_;
  272. };
  273. } // namespace isc::dhcp
  274. } // namespace isc
  275. #endif // CFGMGR_H