d2_client_cfg.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. #include <dhcp_ddns/ncr_udp.h>
  15. #include <dhcpsrv/d2_client_cfg.h>
  16. #include <dhcpsrv/dhcpsrv_log.h>
  17. #include <string>
  18. using namespace std;
  19. namespace isc {
  20. namespace dhcp {
  21. const char *D2ClientConfig::DFT_SERVER_IP = "127.0.0.1";
  22. const size_t D2ClientConfig::DFT_SERVER_PORT = 53001;
  23. const char *D2ClientConfig::DFT_V4_SENDER_IP = "0.0.0.0";
  24. const char *D2ClientConfig::DFT_V6_SENDER_IP = "::";
  25. const size_t D2ClientConfig::DFT_SENDER_PORT = 0;
  26. const size_t D2ClientConfig::DFT_MAX_QUEUE_SIZE = 1024;
  27. const char *D2ClientConfig::DFT_NCR_PROTOCOL = "UDP";
  28. const char *D2ClientConfig::DFT_NCR_FORMAT = "JSON";
  29. const bool D2ClientConfig::DFT_ALWAYS_INCLUDE_FQDN = false;
  30. const bool D2ClientConfig::DFT_OVERRIDE_NO_UPDATE = false;
  31. const bool D2ClientConfig::DFT_OVERRIDE_CLIENT_UPDATE = false;
  32. const bool D2ClientConfig::DFT_REPLACE_CLIENT_NAME = false;
  33. const char *D2ClientConfig::DFT_GENERATED_PREFIX = "myhost";
  34. const char *D2ClientConfig::DFT_QUALIFYING_SUFFIX = "example.com";
  35. D2ClientConfig::D2ClientConfig(const bool enable_updates,
  36. const isc::asiolink::IOAddress& server_ip,
  37. const size_t server_port,
  38. const isc::asiolink::IOAddress& sender_ip,
  39. const size_t sender_port,
  40. const size_t max_queue_size,
  41. const dhcp_ddns::
  42. NameChangeProtocol& ncr_protocol,
  43. const dhcp_ddns::
  44. NameChangeFormat& ncr_format,
  45. const bool always_include_fqdn,
  46. const bool override_no_update,
  47. const bool override_client_update,
  48. const bool replace_client_name,
  49. const std::string& generated_prefix,
  50. const std::string& qualifying_suffix)
  51. : enable_updates_(enable_updates),
  52. server_ip_(server_ip),
  53. server_port_(server_port),
  54. sender_ip_(sender_ip),
  55. sender_port_(sender_port),
  56. max_queue_size_(max_queue_size),
  57. ncr_protocol_(ncr_protocol),
  58. ncr_format_(ncr_format),
  59. always_include_fqdn_(always_include_fqdn),
  60. override_no_update_(override_no_update),
  61. override_client_update_(override_client_update),
  62. replace_client_name_(replace_client_name),
  63. generated_prefix_(generated_prefix),
  64. qualifying_suffix_(qualifying_suffix) {
  65. validateContents();
  66. }
  67. D2ClientConfig::D2ClientConfig()
  68. : enable_updates_(false),
  69. server_ip_(isc::asiolink::IOAddress(DFT_SERVER_IP)),
  70. server_port_(DFT_SERVER_PORT),
  71. sender_ip_(isc::asiolink::IOAddress(DFT_V4_SENDER_IP)),
  72. sender_port_(DFT_SENDER_PORT),
  73. max_queue_size_(DFT_MAX_QUEUE_SIZE),
  74. ncr_protocol_(dhcp_ddns::stringToNcrProtocol(DFT_NCR_PROTOCOL)),
  75. ncr_format_(dhcp_ddns::stringToNcrFormat(DFT_NCR_FORMAT)),
  76. always_include_fqdn_(DFT_ALWAYS_INCLUDE_FQDN),
  77. override_no_update_(DFT_OVERRIDE_NO_UPDATE),
  78. override_client_update_(DFT_OVERRIDE_CLIENT_UPDATE),
  79. replace_client_name_(DFT_REPLACE_CLIENT_NAME),
  80. generated_prefix_(DFT_GENERATED_PREFIX),
  81. qualifying_suffix_(DFT_QUALIFYING_SUFFIX) {
  82. validateContents();
  83. }
  84. D2ClientConfig::~D2ClientConfig(){};
  85. void
  86. D2ClientConfig::enableUpdates(bool enable) {
  87. enable_updates_ = enable;
  88. }
  89. void
  90. D2ClientConfig::validateContents() {
  91. if (ncr_format_ != dhcp_ddns::FMT_JSON) {
  92. isc_throw(D2ClientError, "D2ClientConfig: NCR Format:"
  93. << dhcp_ddns::ncrFormatToString(ncr_format_)
  94. << " is not yet supported");
  95. }
  96. if (ncr_protocol_ != dhcp_ddns::NCR_UDP) {
  97. isc_throw(D2ClientError, "D2ClientConfig: NCR Protocol:"
  98. << dhcp_ddns::ncrProtocolToString(ncr_protocol_)
  99. << " is not yet supported");
  100. }
  101. if (sender_ip_.getFamily() != server_ip_.getFamily()) {
  102. isc_throw(D2ClientError, "D2ClientConfig: address family mismatch: "
  103. << "server-ip: " << server_ip_.toText()
  104. << " is: " << (server_ip_.isV4() ? "IPv4" : "IPv6")
  105. << " while sender-ip: " << sender_ip_.toText()
  106. << " is: " << (sender_ip_.isV4() ? "IPv4" : "IPv6"));
  107. }
  108. if (server_ip_ == sender_ip_ && server_port_ == sender_port_) {
  109. isc_throw(D2ClientError, "D2ClientConfig: server and sender cannot"
  110. " share the exact same IP address/port: "
  111. << server_ip_.toText() << "/" << server_port_);
  112. }
  113. /// @todo perhaps more validation we should do yet?
  114. /// Are there any invalid combinations of options we need to test against?
  115. }
  116. bool
  117. D2ClientConfig::operator == (const D2ClientConfig& other) const {
  118. return ((enable_updates_ == other.enable_updates_) &&
  119. (server_ip_ == other.server_ip_) &&
  120. (server_port_ == other.server_port_) &&
  121. (sender_ip_ == other.sender_ip_) &&
  122. (sender_port_ == other.sender_port_) &&
  123. (max_queue_size_ == other.max_queue_size_) &&
  124. (ncr_protocol_ == other.ncr_protocol_) &&
  125. (ncr_format_ == other.ncr_format_) &&
  126. (always_include_fqdn_ == other.always_include_fqdn_) &&
  127. (override_no_update_ == other.override_no_update_) &&
  128. (override_client_update_ == other.override_client_update_) &&
  129. (replace_client_name_ == other.replace_client_name_) &&
  130. (generated_prefix_ == other.generated_prefix_) &&
  131. (qualifying_suffix_ == other.qualifying_suffix_));
  132. }
  133. bool
  134. D2ClientConfig::operator != (const D2ClientConfig& other) const {
  135. return (!(*this == other));
  136. }
  137. std::string
  138. D2ClientConfig::toText() const {
  139. std::ostringstream stream;
  140. stream << "enable_updates: " << (enable_updates_ ? "yes" : "no");
  141. if (enable_updates_) {
  142. stream << ", server_ip: " << server_ip_.toText()
  143. << ", server_port: " << server_port_
  144. << ", sender_ip: " << sender_ip_.toText()
  145. << ", sender_port: " << sender_port_
  146. << ", max_queue_size: " << max_queue_size_
  147. << ", ncr_protocol: " << ncr_protocol_
  148. << ", ncr_format: " << ncr_format_
  149. << ", always_include_fqdn: " << (always_include_fqdn_ ?
  150. "yes" : "no")
  151. << ", override_no_update: " << (override_no_update_ ?
  152. "yes" : "no")
  153. << ", override_client_update: " << (override_client_update_ ?
  154. "yes" : "no")
  155. << ", replace_client_name: " << (replace_client_name_ ?
  156. "yes" : "no")
  157. << ", generated_prefix: [" << generated_prefix_ << "]"
  158. << ", qualifying_suffix: [" << qualifying_suffix_ << "]";
  159. }
  160. return (stream.str());
  161. }
  162. std::ostream&
  163. operator<<(std::ostream& os, const D2ClientConfig& config) {
  164. os << config.toText();
  165. return (os);
  166. }
  167. }; // namespace dhcp
  168. }; // namespace isc