d2_cfg_mgr.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // Copyright (C) 2014-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 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/d_cfg_mgr.h>
  12. #include <d2/d2_config.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 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 DCfgContextBasePtr clone() {
  37. return (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 Fetches the reverse DNS domain list manager.
  50. ///
  51. /// @return returns a pointer to the reverse manager.
  52. DdnsDomainListMgrPtr getReverseMgr() {
  53. return (reverse_mgr_);
  54. }
  55. /// @brief Fetches the map of TSIG keys.
  56. ///
  57. /// @return returns a pointer to the key map.
  58. TSIGKeyInfoMapPtr getKeys() {
  59. return (keys_);
  60. }
  61. protected:
  62. /// @brief Copy constructor for use by derivations in clone().
  63. D2CfgContext(const D2CfgContext& rhs);
  64. private:
  65. /// @brief Private assignment operator to avoid potential for slicing.
  66. D2CfgContext& operator=(const D2CfgContext& rhs);
  67. /// @brief Global level parameter storage
  68. D2ParamsPtr d2_params_;
  69. /// @brief Forward domain list manager.
  70. DdnsDomainListMgrPtr forward_mgr_;
  71. /// @brief Reverse domain list manager.
  72. DdnsDomainListMgrPtr reverse_mgr_;
  73. /// @brief Storage for the map of TSIGKeyInfos
  74. TSIGKeyInfoMapPtr keys_;
  75. };
  76. /// @brief Defines a pointer for DdnsDomain instances.
  77. typedef boost::shared_ptr<DdnsDomainListMgr> DdnsDomainListMgrPtr;
  78. /// @brief DHCP-DDNS Configuration Manager
  79. ///
  80. /// Provides the mechanisms for managing the DHCP-DDNS application's
  81. /// configuration. This includes services for parsing sets of configuration
  82. /// values, storing the parsed information in its converted form,
  83. /// and retrieving the information on demand.
  84. class D2CfgMgr : public DCfgMgrBase {
  85. public:
  86. /// @brief Reverse zone suffix added to IPv4 addresses for reverse lookups
  87. /// @todo This should be configurable.
  88. static const char* IPV4_REV_ZONE_SUFFIX;
  89. /// @brief Reverse zone suffix added to IPv6 addresses for reverse lookups
  90. /// @todo This should be configurable.
  91. static const char* IPV6_REV_ZONE_SUFFIX;
  92. /// @brief Constructor
  93. D2CfgMgr();
  94. /// @brief Destructor
  95. virtual ~D2CfgMgr();
  96. /// @brief Convenience method that returns the D2 configuration context.
  97. ///
  98. /// @return returns a pointer to the configuration context.
  99. D2CfgContextPtr getD2CfgContext() {
  100. return (boost::dynamic_pointer_cast<D2CfgContext>(getContext()));
  101. }
  102. /// @brief Returns whether or not forward updates are enabled.
  103. ///
  104. /// This method currently uses the presence or absence of Foward DDNS
  105. /// Domains to determine if forward updates are enabled or disabled.
  106. /// @todo This could be expanded to include the check of a configurable
  107. /// boolean value.
  108. ///
  109. /// @return true if forward updates are enabled, false otherwise.
  110. bool forwardUpdatesEnabled();
  111. /// @brief Returns whether or not reverse updates are enabled.
  112. ///
  113. /// This method currently uses the presence or absence of Reverse DDNS
  114. /// Domains to determine if reverse updates are enabled or disabled.
  115. /// @todo This could be expanded to include the check of a configurable
  116. /// boolean value.
  117. ///
  118. /// @return true if reverse updates are enabled, false otherwise.
  119. bool reverseUpdatesEnabled();
  120. /// @brief Matches a given FQDN to a forward domain.
  121. ///
  122. /// This calls the matchDomain method of the forward domain manager to
  123. /// match the given FQDN to a forward domain.
  124. ///
  125. /// @param fqdn is the name for which to look.
  126. /// @param domain receives the matching domain. Note that it will be reset
  127. /// upon entry and only set if a match is subsequently found.
  128. ///
  129. /// @return returns true if a match is found, false otherwise.
  130. /// @throw throws D2CfgError if given an invalid fqdn.
  131. bool matchForward(const std::string& fqdn, DdnsDomainPtr& domain);
  132. /// @brief Matches a given IP address to a reverse domain.
  133. ///
  134. /// This calls the matchDomain method of the reverse domain manager to
  135. /// match the given IPv4 or IPv6 address to a reverse domain.
  136. ///
  137. /// @param ip_address is the name for which to look.
  138. /// @param domain receives the matching domain. Note that it will be reset
  139. /// upon entry and only set if a match is subsequently found.
  140. ///
  141. /// @return returns true if a match is found, false otherwise.
  142. /// @throw throws D2CfgError if given an invalid fqdn.
  143. bool matchReverse(const std::string& ip_address, DdnsDomainPtr& domain);
  144. /// @brief Generate a reverse order string for the given IP address
  145. ///
  146. /// This method creates a string containing the given IP address
  147. /// contents in reverse order. This format is used for matching
  148. /// against reverse DDNS domains in DHCP_DDNS configuration.
  149. /// After reversing the syllables of the address, it appends the
  150. /// appropriate suffix.
  151. ///
  152. /// @param address string containing a valid IPv4 or IPv6 address.
  153. ///
  154. /// @return a std::string containing the reverse order address.
  155. ///
  156. /// @throw D2CfgError if given an invalid address.
  157. static std::string reverseIpAddress(const std::string& address);
  158. /// @brief Generate a reverse order string for the given IP address
  159. ///
  160. /// This method creates a string containing the given IP address
  161. /// contents in reverse order. This format is used for matching
  162. /// against reverse DDNS domains in DHCP_DDNS configuration.
  163. /// After reversing the syllables of the address, it appends the
  164. /// appropriate suffix.
  165. ///
  166. /// Example:
  167. /// input: 192.168.1.15
  168. /// output: 15.1.168.192.in-addr.arpa.
  169. ///
  170. /// @param ioaddr is the IPv4 IOaddress to convert
  171. ///
  172. /// @return a std::string containing the reverse order address.
  173. ///
  174. /// @throw D2CfgError if not given an IPv4 address.
  175. static std::string reverseV4Address(const isc::asiolink::IOAddress& ioaddr);
  176. /// @brief Generate a reverse order string for the given IP address
  177. ///
  178. /// This method creates a string containing the given IPv6 address
  179. /// contents in reverse order. This format is used for matching
  180. /// against reverse DDNS domains in DHCP_DDNS configuration.
  181. /// After reversing the syllables of the address, it appends the
  182. /// appropriate suffix.
  183. ///
  184. /// IPv6 example:
  185. /// input: 2001:db8:302:99::
  186. /// output:
  187. ///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.
  188. ///
  189. /// @param ioaddr string containing a valid IPv6 address.
  190. ///
  191. /// @return a std::string containing the reverse order address.
  192. ///
  193. /// @throw D2CfgError if not given an IPv6 address.
  194. static std::string reverseV6Address(const isc::asiolink::IOAddress& ioaddr);
  195. /// @brief Convenience method fetches the D2Params from context
  196. /// @return reference to const D2ParamsPtr
  197. const D2ParamsPtr& getD2Params();
  198. /// @brief Returns configuration summary in the textual format.
  199. ///
  200. /// @param selection Bitfield which describes the parts of the configuration
  201. /// to be returned. This parameter is ignored for the D2.
  202. ///
  203. /// @return Summary of the configuration in the textual format.
  204. virtual std::string getConfigSummary(const uint32_t selection);
  205. protected:
  206. /// @brief Performs the parsing of the given "params" element.
  207. ///
  208. /// Iterates over the set of parameters, creating a parser based on the
  209. /// parameter's id and then invoking its build method passing in the
  210. /// parameter's configuration value.
  211. ///
  212. /// It then fetches the parameters, validating their values and if
  213. /// valid instantiates a D2Params instance. Invalid values result in
  214. /// a throw.
  215. ///
  216. /// @param params_config set of scalar configuration elements to parse
  217. ///
  218. /// @throw D2CfgError if any of the following are true:
  219. /// -# ip_address is 0.0.0.0 or ::
  220. /// -# port is 0
  221. /// -# dns_server_timeout is < 1
  222. /// -# ncr_protocol is invalid, currently only NCR_UDP is supported
  223. /// -# ncr_format is invalid, currently only FMT_JSON is supported
  224. virtual void buildParams(isc::data::ConstElementPtr params_config);
  225. /// @brief Given an element_id returns an instance of the appropriate
  226. /// parser.
  227. ///
  228. /// It is responsible for top-level or outermost DHCP-DDNS configuration
  229. /// elements (see dhcp-ddns.spec):
  230. /// -# ip_address
  231. /// -# port
  232. /// -# dns_server_timeout
  233. /// -# ncr_protocol
  234. /// -# ncr_format
  235. /// -# tsig_keys
  236. /// -# forward_ddns
  237. /// -# reverse_ddns
  238. ///
  239. /// @param element_id is the string name of the element as it will appear
  240. /// in the configuration set.
  241. /// @param pos position within the configuration text (or file) of element
  242. /// to be parsed. This is passed for error messaging.
  243. ///
  244. /// @return returns a ParserPtr to the parser instance.
  245. /// @throw throws DCfgMgrBaseError if an error occurs.
  246. virtual isc::dhcp::ParserPtr
  247. createConfigParser(const std::string& element_id,
  248. const isc::data::Element::Position& pos =
  249. isc::data::Element::Position());
  250. /// @brief Creates an new, blank D2CfgContext context
  251. ///
  252. /// This method is used at the beginning of configuration process to
  253. /// create a fresh, empty copy of a D2CfgContext. This new context will
  254. /// be populated during the configuration process and will replace the
  255. /// existing context provided the configuration process completes without
  256. /// error.
  257. ///
  258. /// @return Returns a DCfgContextBasePtr to the new context instance.
  259. virtual DCfgContextBasePtr createNewContext();
  260. };
  261. /// @brief Defines a shared pointer to D2CfgMgr.
  262. typedef boost::shared_ptr<D2CfgMgr> D2CfgMgrPtr;
  263. }; // end of isc::d2 namespace
  264. }; // end of isc namespace
  265. #endif // D2_CFG_MGR_H