d2_client_cfg.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // Copyright (C) 2013-2016 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_CLIENT_CFG_H
  7. #define D2_CLIENT_CFG_H
  8. /// @file d2_client_cfg.h Defines the D2ClientConfig class.
  9. /// This file defines the classes Kea uses to manage configuration needed to
  10. /// act as a client of the kea-dhcp-ddns module (aka D2).
  11. ///
  12. #include <asiolink/io_address.h>
  13. #include <dhcp_ddns/ncr_io.h>
  14. #include <exceptions/exceptions.h>
  15. #include <boost/shared_ptr.hpp>
  16. #include <stdint.h>
  17. #include <string>
  18. #include <vector>
  19. namespace isc {
  20. namespace dhcp {
  21. /// An exception that is thrown if an error occurs while configuring
  22. /// the D2 DHCP DDNS client.
  23. class D2ClientError : public isc::Exception {
  24. public:
  25. /// @brief constructor
  26. ///
  27. /// @param file name of the file, where exception occurred
  28. /// @param line line of the file, where exception occurred
  29. /// @param what text description of the issue that caused exception
  30. D2ClientError(const char* file, size_t line, const char* what)
  31. : isc::Exception(file, line, what) {}
  32. };
  33. /// @brief Acts as a storage vault for D2 client configuration
  34. ///
  35. /// A simple container class for storing and retrieving the configuration
  36. /// parameters associated with DHCP-DDNS and acting as a client of D2.
  37. /// Instances of this class may be constructed through configuration parsing.
  38. ///
  39. class D2ClientConfig {
  40. public:
  41. /// @brief Default configuration constants.
  42. /// @todo For now these are hard-coded as configuration layer cannot
  43. /// readily provide them (see Trac #3358).
  44. static const char* DFT_SERVER_IP;
  45. static const size_t DFT_SERVER_PORT;
  46. static const char* DFT_V4_SENDER_IP;
  47. static const char* DFT_V6_SENDER_IP;
  48. static const size_t DFT_SENDER_PORT;
  49. static const size_t DFT_MAX_QUEUE_SIZE;
  50. static const char* DFT_NCR_PROTOCOL;
  51. static const char* DFT_NCR_FORMAT;
  52. static const bool DFT_ALWAYS_INCLUDE_FQDN;
  53. static const bool DFT_OVERRIDE_NO_UPDATE;
  54. static const bool DFT_OVERRIDE_CLIENT_UPDATE;
  55. static const char* DFT_REPLACE_CLIENT_NAME_MODE;
  56. static const char* DFT_GENERATED_PREFIX;
  57. /// @brief Defines the client name replacement modes.
  58. enum ReplaceClientNameMode {
  59. RCM_NEVER,
  60. RCM_ALWAYS,
  61. RCM_WHEN_PRESENT,
  62. RCM_WHEN_NOT_PRESENT
  63. };
  64. /// @brief Constructor
  65. ///
  66. /// @param enable_updates Enables DHCP-DDNS updates
  67. /// @param server_ip IP address of the kea-dhcp-ddns server (IPv4 or IPv6)
  68. /// @param server_port IP port of the kea-dhcp-ddns server
  69. /// @param sender_ip IP address of the kea-dhcp-ddns server (IPv4 or IPv6)
  70. /// @param sender_port IP port of the kea-dhcp-ddns server
  71. /// @param max_queue_size maximum NCRs allowed in sender's queue
  72. /// @param ncr_protocol Socket protocol to use with kea-dhcp-ddns
  73. /// Currently only UDP is supported.
  74. /// @param ncr_format Format of the kea-dhcp-ddns requests.
  75. /// Currently only JSON format is supported.
  76. /// @param always_include_fqdn Enables always including the FQDN option in
  77. /// DHCP responses.
  78. /// @param override_no_update Enables updates, even if clients request no
  79. /// updates.
  80. /// @param override_client_update Perform updates, even if client requested
  81. /// delegation.
  82. /// @param replace_client_name_mode enables replacement of the domain-name
  83. /// supplied by the client with a generated name.
  84. /// @param generated_prefix Prefix to use when generating domain-names.
  85. /// @param qualifying_suffix Suffix to use to qualify partial domain-names.
  86. ///
  87. /// @c enable_updates is mandatory, @c qualifying_suffix is mandatory
  88. /// when updates are enabled, other parameters are optional.
  89. ///
  90. /// @throw D2ClientError if given an invalid protocol or format.
  91. D2ClientConfig(const bool enable_updates,
  92. const isc::asiolink::IOAddress& server_ip,
  93. const size_t server_port,
  94. const isc::asiolink::IOAddress& sender_ip,
  95. const size_t sender_port,
  96. const size_t max_queue_size,
  97. const dhcp_ddns::NameChangeProtocol& ncr_protocol,
  98. const dhcp_ddns::NameChangeFormat& ncr_format,
  99. const bool always_include_fqdn,
  100. const bool override_no_update,
  101. const bool override_client_update,
  102. const ReplaceClientNameMode replace_client_name_mode,
  103. const std::string& generated_prefix,
  104. const std::string& qualifying_suffix);
  105. /// @brief Default constructor
  106. /// The default constructor creates an instance that has updates disabled.
  107. D2ClientConfig();
  108. /// @brief Destructor
  109. virtual ~D2ClientConfig();
  110. /// @brief Return whether or not DHCP-DDNS updating is enabled.
  111. bool getEnableUpdates() const {
  112. return(enable_updates_);
  113. }
  114. /// @brief Return the IP address of kea-dhcp-ddns (IPv4 or IPv6).
  115. const isc::asiolink::IOAddress& getServerIp() const {
  116. return(server_ip_);
  117. }
  118. /// @brief Return the IP port of kea-dhcp-ddns.
  119. size_t getServerPort() const {
  120. return(server_port_);
  121. }
  122. /// @brief Return the IP address client should use to send
  123. const isc::asiolink::IOAddress& getSenderIp() const {
  124. return(sender_ip_);
  125. }
  126. /// @brief Return the IP port client should use to send
  127. size_t getSenderPort() const {
  128. return(sender_port_);
  129. }
  130. /// @brief Return Maximum sender queue size
  131. size_t getMaxQueueSize() const {
  132. return(max_queue_size_);
  133. }
  134. /// @brief Return the socket protocol to use with kea-dhcp-ddns.
  135. const dhcp_ddns::NameChangeProtocol& getNcrProtocol() const {
  136. return(ncr_protocol_);
  137. }
  138. /// @brief Return the kea-dhcp-ddns request format.
  139. const dhcp_ddns::NameChangeFormat& getNcrFormat() const {
  140. return(ncr_format_);
  141. }
  142. /// @brief Return whether or not FQDN is always included in DHCP responses.
  143. bool getAlwaysIncludeFqdn() const {
  144. return(always_include_fqdn_);
  145. }
  146. /// @brief Return if updates are done even if clients request no updates.
  147. bool getOverrideNoUpdate() const {
  148. return(override_no_update_);
  149. }
  150. /// @brief Return if updates are done even when clients request delegation.
  151. bool getOverrideClientUpdate() const {
  152. return(override_client_update_);
  153. }
  154. /// @brief Return mode of replacement to use regarding client's client's domain-name
  155. ReplaceClientNameMode getReplaceClientNameMode() const {
  156. return(replace_client_name_mode_);
  157. }
  158. /// @brief Return the prefix to use when generating domain-names.
  159. const std::string& getGeneratedPrefix() const {
  160. return(generated_prefix_);
  161. }
  162. /// @brief Return the suffix to use to qualify partial domain-names.
  163. const std::string& getQualifyingSuffix() const {
  164. return(qualifying_suffix_);
  165. }
  166. /// @brief Compares two D2ClientConfigs for equality
  167. bool operator == (const D2ClientConfig& other) const;
  168. /// @brief Compares two D2ClientConfigs for inequality
  169. bool operator != (const D2ClientConfig& other) const;
  170. /// @brief Generates a string representation of the class contents.
  171. std::string toText() const;
  172. /// @brief Sets enable-updates flag to the given value.
  173. ///
  174. /// This is the only value that may be altered outside the constructor
  175. /// as it may be desirable to toggle it off and on when dealing with
  176. /// D2 IO errors.
  177. ///
  178. /// @param enable boolean value to assign to the enable-updates flag
  179. void enableUpdates(bool enable);
  180. /// @brief Converts labels to ReplaceClientNameMode enum values.
  181. ///
  182. /// @param mode_str text to convert to an enum.
  183. /// Valid string values: "never", "always", "when-present",
  184. /// "when-not-present" (case-insensitive)
  185. ///
  186. /// @return NameChangeFormat value which maps to the given string.
  187. ///
  188. /// @throw isc::BadValue if given a string value which does not map to an
  189. /// enum value.
  190. static ReplaceClientNameMode stringToReplaceClientNameMode(const std::string& mode_str);
  191. /// @brief Converts NameChangeFormat enums to text labels.
  192. ///
  193. /// @param mode enum value to convert to label
  194. ///
  195. /// @return std:string containing the text label if the value is valid, or
  196. /// "unknown" if not.
  197. static std::string replaceClientNameModeToString(const ReplaceClientNameMode& mode);
  198. protected:
  199. /// @brief Validates member values.
  200. ///
  201. /// Method is used by the constructor to validate member contents.
  202. ///
  203. /// @throw D2ClientError if given an invalid protocol or format.
  204. virtual void validateContents();
  205. private:
  206. /// @brief Indicates whether or not DHCP DDNS updating is enabled.
  207. bool enable_updates_;
  208. /// @brief IP address of the kea-dhcp-ddns server (IPv4 or IPv6).
  209. isc::asiolink::IOAddress server_ip_;
  210. /// @brief IP port of the kea-dhcp-ddns server.
  211. size_t server_port_;
  212. /// @brief IP address on which the client should send
  213. isc::asiolink::IOAddress sender_ip_;
  214. /// @brief IP port on which the client should send
  215. size_t sender_port_;
  216. /// @brief Maximum number of NCRs allowed to queue waiting to send
  217. size_t max_queue_size_;
  218. /// @brief The socket protocol to use with kea-dhcp-ddns.
  219. /// Currently only UDP is supported.
  220. dhcp_ddns::NameChangeProtocol ncr_protocol_;
  221. /// @brief Format of the kea-dhcp-ddns requests.
  222. /// Currently only JSON format is supported.
  223. dhcp_ddns::NameChangeFormat ncr_format_;
  224. /// @brief Should Kea always include the FQDN option in its response.
  225. bool always_include_fqdn_;
  226. /// @brief Should Kea perform updates, even if client requested no updates.
  227. /// Overrides the client request for no updates via the N flag.
  228. bool override_no_update_;
  229. /// @brief Should Kea perform updates, even if client requested delegation.
  230. bool override_client_update_;
  231. /// @brief How Kea should handle the domain-name supplied by the client.
  232. ReplaceClientNameMode replace_client_name_mode_;
  233. /// @brief Prefix Kea should use when generating domain-names.
  234. std::string generated_prefix_;
  235. /// @brief Suffix Kea should use when to qualify partial domain-names.
  236. std::string qualifying_suffix_;
  237. };
  238. std::ostream&
  239. operator<<(std::ostream& os, const D2ClientConfig& config);
  240. /// @brief Defines a pointer for D2ClientConfig instances.
  241. typedef boost::shared_ptr<D2ClientConfig> D2ClientConfigPtr;
  242. } // namespace isc
  243. } // namespace dhcp
  244. #endif