d2_cfg_mgr.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // Copyright (C) 2014-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 D2_CFG_MGR_H
  7. #define D2_CFG_MGR_H
  8. #include <asiolink/io_service.h>
  9. #include <cc/data.h>
  10. #include <exceptions/exceptions.h>
  11. #include <d2/d2_config.h>
  12. #include <process/d_cfg_mgr.h>
  13. #include <stdint.h>
  14. #include <string>
  15. namespace isc {
  16. namespace d2 {
  17. class D2CfgContext;
  18. /// @brief Pointer to a configuration context.
  19. typedef boost::shared_ptr<D2CfgContext> D2CfgContextPtr;
  20. /// @brief DHCP-DDNS Configuration Context
  21. ///
  22. /// Implements the storage container for configuration context.
  23. /// It provides a single enclosure for the storage of configuration parameters
  24. /// and any other DHCP-DDNS specific information that needs to be accessible
  25. /// during configuration parsing as well as to the application as a whole.
  26. /// It is derived from the context base class, DCfgContextBase.
  27. class D2CfgContext : public process::DCfgContextBase {
  28. public:
  29. /// @brief Constructor
  30. D2CfgContext();
  31. /// @brief Destructor
  32. virtual ~D2CfgContext();
  33. /// @brief Creates a clone of this context object.
  34. ///
  35. /// @return returns a pointer to the new clone.
  36. virtual process::DCfgContextBasePtr clone() {
  37. return (process::DCfgContextBasePtr(new D2CfgContext(*this)));
  38. }
  39. /// @brief Fetches a reference to the D2Params
  40. D2ParamsPtr& getD2Params() {
  41. return (d2_params_);
  42. }
  43. /// @brief Fetches the forward DNS domain list manager.
  44. ///
  45. /// @return returns a pointer to the forward manager.
  46. DdnsDomainListMgrPtr getForwardMgr() {
  47. return (forward_mgr_);
  48. }
  49. /// @brief Sets the forward domain list manager
  50. /// @param forward_mgr pointer to the new forward manager
  51. void setForwardMgr(DdnsDomainListMgrPtr forward_mgr) {
  52. forward_mgr_ = forward_mgr;
  53. }
  54. /// @brief Fetches the reverse DNS domain list manager.
  55. ///
  56. /// @return returns a pointer to the reverse manager.
  57. DdnsDomainListMgrPtr getReverseMgr() {
  58. return (reverse_mgr_);
  59. }
  60. /// @brief Sets the reverse domain list manager
  61. /// @param reverse_mgr pointer to the new reverse manager
  62. void setReverseMgr(DdnsDomainListMgrPtr reverse_mgr) {
  63. reverse_mgr_ = reverse_mgr;
  64. }
  65. /// @brief Fetches the map of TSIG keys.
  66. ///
  67. /// @return returns a pointer to the key map.
  68. TSIGKeyInfoMapPtr getKeys() {
  69. return (keys_);
  70. }
  71. /// @brief Sets the map of TSIG keys
  72. ///
  73. /// @param keys pointer to the new TSIG key map
  74. void setKeys(const TSIGKeyInfoMapPtr& keys) {
  75. keys_ = keys;
  76. }
  77. /// @brief Unparse a configuration objet
  78. ///
  79. /// @return a pointer to a configuration
  80. virtual isc::data::ElementPtr toElement() const;
  81. protected:
  82. /// @brief Copy constructor for use by derivations in clone().
  83. D2CfgContext(const D2CfgContext& rhs);
  84. private:
  85. /// @brief Private assignment operator to avoid potential for slicing.
  86. D2CfgContext& operator=(const D2CfgContext& rhs);
  87. /// @brief Global level parameter storage
  88. D2ParamsPtr d2_params_;
  89. /// @brief Forward domain list manager.
  90. DdnsDomainListMgrPtr forward_mgr_;
  91. /// @brief Reverse domain list manager.
  92. DdnsDomainListMgrPtr reverse_mgr_;
  93. /// @brief Storage for the map of TSIGKeyInfos
  94. TSIGKeyInfoMapPtr keys_;
  95. };
  96. /// @brief Defines a pointer for DdnsDomain instances.
  97. typedef boost::shared_ptr<DdnsDomainListMgr> DdnsDomainListMgrPtr;
  98. /// @brief DHCP-DDNS Configuration Manager
  99. ///
  100. /// Provides the mechanisms for managing the DHCP-DDNS application's
  101. /// configuration. This includes services for parsing sets of configuration
  102. /// values, storing the parsed information in its converted form,
  103. /// and retrieving the information on demand.
  104. class D2CfgMgr : public process::DCfgMgrBase {
  105. public:
  106. /// @brief Reverse zone suffix added to IPv4 addresses for reverse lookups
  107. /// @todo This should be configurable.
  108. static const char* IPV4_REV_ZONE_SUFFIX;
  109. /// @brief Reverse zone suffix added to IPv6 addresses for reverse lookups
  110. /// @todo This should be configurable.
  111. static const char* IPV6_REV_ZONE_SUFFIX;
  112. /// @brief Constructor
  113. D2CfgMgr();
  114. /// @brief Destructor
  115. virtual ~D2CfgMgr();
  116. /// @brief Convenience method that returns the D2 configuration context.
  117. ///
  118. /// @return returns a pointer to the configuration context.
  119. D2CfgContextPtr getD2CfgContext() {
  120. return (boost::dynamic_pointer_cast<D2CfgContext>(getContext()));
  121. }
  122. /// @brief Returns whether or not forward updates are enabled.
  123. ///
  124. /// This method currently uses the presence or absence of Forward DDNS
  125. /// Domains to determine if forward updates are enabled or disabled.
  126. /// @todo This could be expanded to include the check of a configurable
  127. /// boolean value.
  128. ///
  129. /// @return true if forward updates are enabled, false otherwise.
  130. bool forwardUpdatesEnabled();
  131. /// @brief Returns whether or not reverse updates are enabled.
  132. ///
  133. /// This method currently uses the presence or absence of Reverse DDNS
  134. /// Domains to determine if reverse updates are enabled or disabled.
  135. /// @todo This could be expanded to include the check of a configurable
  136. /// boolean value.
  137. ///
  138. /// @return true if reverse updates are enabled, false otherwise.
  139. bool reverseUpdatesEnabled();
  140. /// @brief Matches a given FQDN to a forward domain.
  141. ///
  142. /// This calls the matchDomain method of the forward domain manager to
  143. /// match the given FQDN to a forward domain.
  144. ///
  145. /// @param fqdn is the name for which to look.
  146. /// @param domain receives the matching domain. Note that it will be reset
  147. /// upon entry and only set if a match is subsequently found.
  148. ///
  149. /// @return returns true if a match is found, false otherwise.
  150. /// @throw throws D2CfgError if given an invalid fqdn.
  151. bool matchForward(const std::string& fqdn, DdnsDomainPtr& domain);
  152. /// @brief Matches a given IP address to a reverse domain.
  153. ///
  154. /// This calls the matchDomain method of the reverse domain manager to
  155. /// match the given IPv4 or IPv6 address to a reverse domain.
  156. ///
  157. /// @param ip_address is the name for which to look.
  158. /// @param domain receives the matching domain. Note that it will be reset
  159. /// upon entry and only set if a match is subsequently found.
  160. ///
  161. /// @return returns true if a match is found, false otherwise.
  162. /// @throw throws D2CfgError if given an invalid fqdn.
  163. bool matchReverse(const std::string& ip_address, DdnsDomainPtr& domain);
  164. /// @brief Generate a reverse order string for the given IP address
  165. ///
  166. /// This method creates a string containing the given IP address
  167. /// contents in reverse order. This format is used for matching
  168. /// against reverse DDNS domains in DHCP_DDNS configuration.
  169. /// After reversing the syllables of the address, it appends the
  170. /// appropriate suffix.
  171. ///
  172. /// @param address string containing a valid IPv4 or IPv6 address.
  173. ///
  174. /// @return a std::string containing the reverse order address.
  175. ///
  176. /// @throw D2CfgError if given an invalid address.
  177. static std::string reverseIpAddress(const std::string& address);
  178. /// @brief Generate a reverse order string for the given IP address
  179. ///
  180. /// This method creates a string containing the given IP address
  181. /// contents in reverse order. This format is used for matching
  182. /// against reverse DDNS domains in DHCP_DDNS configuration.
  183. /// After reversing the syllables of the address, it appends the
  184. /// appropriate suffix.
  185. ///
  186. /// Example:
  187. /// input: 192.168.1.15
  188. /// output: 15.1.168.192.in-addr.arpa.
  189. ///
  190. /// @param ioaddr is the IPv4 IOaddress to convert
  191. ///
  192. /// @return a std::string containing the reverse order address.
  193. ///
  194. /// @throw D2CfgError if not given an IPv4 address.
  195. static std::string reverseV4Address(const isc::asiolink::IOAddress& ioaddr);
  196. /// @brief Generate a reverse order string for the given IP address
  197. ///
  198. /// This method creates a string containing the given IPv6 address
  199. /// contents in reverse order. This format is used for matching
  200. /// against reverse DDNS domains in DHCP_DDNS configuration.
  201. /// After reversing the syllables of the address, it appends the
  202. /// appropriate suffix.
  203. ///
  204. /// IPv6 example:
  205. /// input: 2001:db8:302:99::
  206. /// output:
  207. ///0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.9.9.0.0.2.0.3.0.8.B.D.0.1.0.0.2.ip6.arpa.
  208. ///
  209. /// @param ioaddr string containing a valid IPv6 address.
  210. ///
  211. /// @return a std::string containing the reverse order address.
  212. ///
  213. /// @throw D2CfgError if not given an IPv6 address.
  214. static std::string reverseV6Address(const isc::asiolink::IOAddress& ioaddr);
  215. /// @brief Convenience method fetches the D2Params from context
  216. /// @return reference to const D2ParamsPtr
  217. const D2ParamsPtr& getD2Params();
  218. /// @brief Returns configuration summary in the textual format.
  219. ///
  220. /// @param selection Bitfield which describes the parts of the configuration
  221. /// to be returned. This parameter is ignored for the D2.
  222. ///
  223. /// @return Summary of the configuration in the textual format.
  224. virtual std::string getConfigSummary(const uint32_t selection);
  225. protected:
  226. /// @brief Parses an element using alternate parsers
  227. ///
  228. /// Each element to be parsed is passed first into this method to allow
  229. /// it to be processed by SimpleParser derivations if they've been
  230. /// implemented. The method should return true if it has processed the
  231. /// element or false if the element should be passed onto the original
  232. /// DhcpConfigParer mechanisms. This method is invoked in both
  233. /// @c DCfgMgrBase::buildParams() and DCfgMgrBase::buildAndCommit().
  234. ///
  235. /// @param element_id name of the element as it is expected in the cfg
  236. /// @param element value of the element as ElementPtr
  237. virtual void parseElement(const std::string& element_id,
  238. isc::data::ConstElementPtr element);
  239. /// @brief Adds default values to the given config
  240. ///
  241. /// Adds the D2 default values to the configuration Element map. This
  242. /// method is invoked by @c DCfgMgrBase::paserConfig().
  243. ///
  244. /// @param mutable_config - configuration to which defaults should be added
  245. virtual void setCfgDefaults(isc::data::ElementPtr mutable_config);
  246. /// @brief Performs the parsing of the given "params" element.
  247. ///
  248. /// Iterates over the set of parameters, creating a parser based on the
  249. /// parameter's id and then invoking its build method passing in the
  250. /// parameter's configuration value.
  251. ///
  252. /// It then fetches the parameters, validating their values and if
  253. /// valid instantiates a D2Params instance. Invalid values result in
  254. /// a throw.
  255. ///
  256. /// @param params_config set of scalar configuration elements to parse
  257. ///
  258. /// @throw D2CfgError if any of the following are true:
  259. /// -# ip_address is 0.0.0.0 or ::
  260. /// -# port is 0
  261. /// -# dns_server_timeout is < 1
  262. /// -# ncr_protocol is invalid, currently only NCR_UDP is supported
  263. /// -# ncr_format is invalid, currently only FMT_JSON is supported
  264. virtual void buildParams(isc::data::ConstElementPtr params_config);
  265. /// @brief Creates an new, blank D2CfgContext context
  266. ///
  267. /// This method is used at the beginning of configuration process to
  268. /// create a fresh, empty copy of a D2CfgContext. This new context will
  269. /// be populated during the configuration process and will replace the
  270. /// existing context provided the configuration process completes without
  271. /// error.
  272. ///
  273. /// @return Returns a DCfgContextBasePtr to the new context instance.
  274. virtual process::DCfgContextBasePtr createNewContext();
  275. };
  276. /// @brief Defines a shared pointer to D2CfgMgr.
  277. typedef boost::shared_ptr<D2CfgMgr> D2CfgMgrPtr;
  278. }; // end of isc::d2 namespace
  279. }; // end of isc namespace
  280. #endif // D2_CFG_MGR_H