d2_client.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // Copyright (C) 2013-2014 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef D2_CLIENT_H
  15. #define D2_CLIENT_H
  16. /// @file d2_client.h Defines the D2ClientConfig and D2ClientMgr classes.
  17. /// This file defines the classes Kea uses to act as a client of the b10-
  18. /// dhcp-ddns module (aka D2).
  19. ///
  20. #include <asiolink/io_address.h>
  21. #include <dhcp_ddns/ncr_io.h>
  22. #include <exceptions/exceptions.h>
  23. #include <boost/shared_ptr.hpp>
  24. #include <stdint.h>
  25. #include <string>
  26. #include <vector>
  27. namespace isc {
  28. namespace dhcp {
  29. /// An exception that is thrown if an error occurs while configuring
  30. /// the D2 DHCP DDNS client.
  31. class D2ClientError : public isc::Exception {
  32. public:
  33. /// @brief constructor
  34. ///
  35. /// @param file name of the file, where exception occurred
  36. /// @param line line of the file, where exception occurred
  37. /// @param what text description of the issue that caused exception
  38. D2ClientError(const char* file, size_t line, const char* what)
  39. : isc::Exception(file, line, what) {}
  40. };
  41. /// @brief Acts as a storage vault for D2 client configuration
  42. ///
  43. /// A simple container class for storing and retrieving the configuration
  44. /// parameters associated with DHCP-DDNS and acting as a client of D2.
  45. /// Instances of this class may be constructed through configuration parsing.
  46. ///
  47. class D2ClientConfig {
  48. public:
  49. /// @brief Constructor
  50. ///
  51. /// @param enable_updates Enables DHCP-DDNS updates
  52. /// @param server_ip IP address of the b10-dhcp-ddns server (IPv4 or IPv6)
  53. /// @param server_port IP port of the b10-dhcp-ddns server
  54. /// @param ncr_protocol Socket protocol to use with b10-dhcp-ddns
  55. /// Currently only UDP is supported.
  56. /// @param ncr_format Format of the b10-dhcp-ddns requests.
  57. /// Currently only JSON format is supported.
  58. /// @param remove_on_renew Enables DNS Removes when renewing a lease
  59. /// If true, Kea should request an explicit DNS remove prior to requesting
  60. /// a DNS update when renewing a lease.
  61. /// (Note: b10-dhcp-ddns is implemented per RFC 4703 and such a remove
  62. /// is unnecessary).
  63. /// @param always_include_fqdn Enables always including the FQDN option in
  64. /// DHCP responses.
  65. /// @param override_no_update Enables updates, even if clients request no
  66. /// updates.
  67. /// @param override_client_update Perform updates, even if client requested
  68. /// delegation.
  69. /// @param replace_client_name enables replacement of the domain-name
  70. /// supplied by the client with a generated name.
  71. /// @param generated_prefix Prefix to use when generating domain-names.
  72. /// @param qualifying_suffix Suffix to use to qualify partial domain-names.
  73. ///
  74. /// @throw D2ClientError if given an invalid protocol or format.
  75. D2ClientConfig(const bool enable_updates,
  76. const isc::asiolink::IOAddress& server_ip,
  77. const size_t server_port,
  78. const dhcp_ddns::NameChangeProtocol& ncr_protocol,
  79. const dhcp_ddns::NameChangeFormat& ncr_format,
  80. const bool remove_on_renew,
  81. const bool always_include_fqdn,
  82. const bool override_no_update,
  83. const bool override_client_update,
  84. const bool replace_client_name,
  85. const std::string& generated_prefix,
  86. const std::string& qualifying_suffix);
  87. /// @brief Default constructor
  88. /// The default constructor creates an instance that has updates disabled.
  89. D2ClientConfig();
  90. /// @brief Destructor
  91. virtual ~D2ClientConfig();
  92. /// @brief Return whether or not DHCP-DDNS updating is enabled.
  93. bool getEnableUpdates() const {
  94. return(enable_updates_);
  95. }
  96. /// @brief Return the IP address of b10-dhcp-ddns (IPv4 or IPv6).
  97. const isc::asiolink::IOAddress& getServerIp() const {
  98. return(server_ip_);
  99. }
  100. /// @brief Return the IP port of b10-dhcp-ddns.
  101. size_t getServerPort() const {
  102. return(server_port_);
  103. }
  104. /// @brief Return the socket protocol to use with b10-dhcp-ddns.
  105. const dhcp_ddns::NameChangeProtocol& getNcrProtocol() const {
  106. return(ncr_protocol_);
  107. }
  108. /// @brief Return the b10-dhcp-ddns request format.
  109. const dhcp_ddns::NameChangeFormat& getNcrFormat() const {
  110. return(ncr_format_);
  111. }
  112. /// @brief Return whether or not removes should be sent for lease renewals.
  113. bool getRemoveOnRenew() const {
  114. return(remove_on_renew_);
  115. }
  116. /// @brief Return whether or not FQDN is always included in DHCP responses.
  117. bool getAlwaysIncludeFqdn() const {
  118. return(always_include_fqdn_);
  119. }
  120. /// @brief Return if updates are done even if clients request no updates.
  121. bool getOverrideNoUpdate() const {
  122. return(override_no_update_);
  123. }
  124. /// @brief Return if updates are done even when clients request delegation.
  125. bool getOverrideClientUpdate() const {
  126. return(override_client_update_);
  127. }
  128. /// @brief Return whether or not client's domain-name is always replaced.
  129. bool getReplaceClientName() const {
  130. return(replace_client_name_);
  131. }
  132. /// @brief Return the prefix to use when generating domain-names.
  133. const std::string& getGeneratedPrefix() const {
  134. return(generated_prefix_);
  135. }
  136. /// @brief Return the suffix to use to qualify partial domain-names.
  137. const std::string& getQualifyingSuffix() const {
  138. return(qualifying_suffix_);
  139. }
  140. /// @brief Compares two D2ClientConfigs for equality
  141. bool operator == (const D2ClientConfig& other) const;
  142. /// @brief Compares two D2ClientConfigs for inequality
  143. bool operator != (const D2ClientConfig& other) const;
  144. /// @brief Generates a string representation of the class contents.
  145. std::string toText() const;
  146. protected:
  147. /// @brief Validates member values.
  148. ///
  149. /// Method is used by the constructor to validate member contents.
  150. ///
  151. /// @throw D2ClientError if given an invalid protocol or format.
  152. virtual void validateContents();
  153. private:
  154. /// @brief Indicates whether or not DHCP DDNS updating is enabled.
  155. bool enable_updates_;
  156. /// @brief IP address of the b10-dhcp-ddns server (IPv4 or IPv6).
  157. isc::asiolink::IOAddress server_ip_;
  158. /// @brief IP port of the b10-dhcp-ddns server.
  159. size_t server_port_;
  160. /// @brief The socket protocol to use with b10-dhcp-ddns.
  161. /// Currently only UPD is supported.
  162. dhcp_ddns::NameChangeProtocol ncr_protocol_;
  163. /// @brief Format of the b10-dhcp-ddns requests.
  164. /// Currently only JSON format is supported.
  165. dhcp_ddns::NameChangeFormat ncr_format_;
  166. /// @brief Should Kea request a DNS Remove when renewing a lease.
  167. /// If true, Kea should request an explicit DNS remove prior to requesting
  168. /// a DNS update when renewing a lease.
  169. /// (Note: b10-dhcp-ddns is implemented per RFC 4703 and such a remove
  170. /// is unnecessary).
  171. bool remove_on_renew_;
  172. /// @brief Should Kea always include the FQDN option in its response.
  173. bool always_include_fqdn_;
  174. /// @brief Should Kea perform updates, even if client requested no updates.
  175. /// Overrides the client request for no updates via the N flag.
  176. bool override_no_update_;
  177. /// @brief Should Kea perform updates, even if client requested delegation.
  178. bool override_client_update_;
  179. /// @brief Should Kea replace the domain-name supplied by the client.
  180. bool replace_client_name_;
  181. /// @brief Prefix Kea should use when generating domain-names.
  182. std::string generated_prefix_;
  183. /// @brief Suffix Kea should use when to qualify partial domain-names.
  184. std::string qualifying_suffix_;
  185. };
  186. std::ostream&
  187. operator<<(std::ostream& os, const D2ClientConfig& config);
  188. /// @brief Defines a pointer for D2ClientConfig instances.
  189. typedef boost::shared_ptr<D2ClientConfig> D2ClientConfigPtr;
  190. /// @brief D2ClientMgr isolates Kea from the details of being a D2 client.
  191. ///
  192. /// Provides services for managing the current D2ClientConfig and managing
  193. /// communications with D2. (@todo The latter will be added once communication
  194. /// with D2 is implemented through the integration of
  195. /// dhcp_ddns::NameChangeSender interface(s)).
  196. ///
  197. class D2ClientMgr {
  198. public:
  199. /// @brief Constructor
  200. ///
  201. /// Default constructor which constructs an instance which has DHCP-DDNS
  202. /// updates disabled.
  203. D2ClientMgr();
  204. /// @brief Destructor.
  205. ~D2ClientMgr();
  206. /// @brief Updates the DHCP-DDNS client configuration to the given value.
  207. ///
  208. /// @param new_config pointer to the new client configuration.
  209. /// @throw D2ClientError if passed an empty pointer.
  210. void setD2ClientConfig(D2ClientConfigPtr& new_config);
  211. /// @brief Convenience method for checking if DHCP-DDNS is enabled.
  212. ///
  213. /// @return True if the D2 configuration is enabled.
  214. bool ddnsEnabled();
  215. /// @brief Fetches the DHCP-DDNS configuration pointer.
  216. ///
  217. /// @return a reference to the current configuration pointer.
  218. const D2ClientConfigPtr& getD2ClientConfig() const;
  219. private:
  220. /// @brief Container class for DHCP-DDNS configuration parameters.
  221. D2ClientConfigPtr d2_client_config_;
  222. };
  223. /// @brief Defines a pointer for D2ClientMgr instances.
  224. typedef boost::shared_ptr<D2ClientMgr> D2ClientMgrPtr;
  225. } // namespace isc
  226. } // namespace dhcp
  227. #endif