srv_config.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. #include <config.h>
  7. #include <dhcpsrv/cfgmgr.h>
  8. #include <dhcpsrv/srv_config.h>
  9. #include <dhcpsrv/lease_mgr_factory.h>
  10. #include <log/logger_manager.h>
  11. #include <log/logger_specification.h>
  12. #include <dhcp/pkt.h> // Needed for HWADDR_SOURCE_*
  13. #include <list>
  14. #include <sstream>
  15. using namespace isc::log;
  16. namespace isc {
  17. namespace dhcp {
  18. SrvConfig::SrvConfig()
  19. : sequence_(0), cfg_iface_(new CfgIface()),
  20. cfg_option_def_(new CfgOptionDef()), cfg_option_(new CfgOption()),
  21. cfg_subnets4_(new CfgSubnets4()), cfg_subnets6_(new CfgSubnets6()),
  22. cfg_hosts_(new CfgHosts()), cfg_rsoo_(new CfgRSOO()),
  23. cfg_expiration_(new CfgExpiration()), cfg_duid_(new CfgDUID()),
  24. cfg_db_access_(new CfgDbAccess()),
  25. cfg_host_operations4_(CfgHostOperations::createConfig4()),
  26. cfg_host_operations6_(CfgHostOperations::createConfig6()),
  27. class_dictionary_(new ClientClassDictionary()),
  28. decline_timer_(0), echo_v4_client_id_(true), dhcp4o6_port_(0),
  29. d2_client_config_(new D2ClientConfig()) {
  30. }
  31. SrvConfig::SrvConfig(const uint32_t sequence)
  32. : sequence_(sequence), cfg_iface_(new CfgIface()),
  33. cfg_option_def_(new CfgOptionDef()), cfg_option_(new CfgOption()),
  34. cfg_subnets4_(new CfgSubnets4()), cfg_subnets6_(new CfgSubnets6()),
  35. cfg_hosts_(new CfgHosts()), cfg_rsoo_(new CfgRSOO()),
  36. cfg_expiration_(new CfgExpiration()), cfg_duid_(new CfgDUID()),
  37. cfg_db_access_(new CfgDbAccess()),
  38. cfg_host_operations4_(CfgHostOperations::createConfig4()),
  39. cfg_host_operations6_(CfgHostOperations::createConfig6()),
  40. class_dictionary_(new ClientClassDictionary()),
  41. decline_timer_(0), echo_v4_client_id_(true), dhcp4o6_port_(0),
  42. d2_client_config_(new D2ClientConfig()) {
  43. }
  44. std::string
  45. SrvConfig::getConfigSummary(const uint32_t selection) const {
  46. std::ostringstream s;
  47. size_t subnets_num;
  48. if ((selection & CFGSEL_SUBNET4) == CFGSEL_SUBNET4) {
  49. subnets_num = getCfgSubnets4()->getAll()->size();
  50. if (subnets_num > 0) {
  51. s << "added IPv4 subnets: " << subnets_num;
  52. } else {
  53. s << "no IPv4 subnets!";
  54. }
  55. s << "; ";
  56. }
  57. if ((selection & CFGSEL_SUBNET6) == CFGSEL_SUBNET6) {
  58. subnets_num = getCfgSubnets6()->getAll()->size();
  59. if (subnets_num > 0) {
  60. s << "added IPv6 subnets: " << subnets_num;
  61. } else {
  62. s << "no IPv6 subnets!";
  63. }
  64. s << "; ";
  65. }
  66. if ((selection & CFGSEL_DDNS) == CFGSEL_DDNS) {
  67. bool ddns_enabled = getD2ClientConfig()->getEnableUpdates();
  68. s << "DDNS: " << (ddns_enabled ? "enabled" : "disabled") << "; ";
  69. }
  70. if (s.tellp() == static_cast<std::streampos>(0)) {
  71. s << "no config details available";
  72. }
  73. std::string summary = s.str();
  74. size_t last_separator_pos = summary.find_last_of(";");
  75. if (last_separator_pos == summary.length() - 2) {
  76. summary.erase(last_separator_pos);
  77. }
  78. return (summary);
  79. }
  80. bool
  81. SrvConfig::sequenceEquals(const SrvConfig& other) {
  82. return (getSequence() == other.getSequence());
  83. }
  84. void
  85. SrvConfig::copy(SrvConfig& new_config) const {
  86. // We will entirely replace loggers in the new configuration.
  87. new_config.logging_info_.clear();
  88. for (LoggingInfoStorage::const_iterator it = logging_info_.begin();
  89. it != logging_info_.end(); ++it) {
  90. new_config.addLoggingInfo(*it);
  91. }
  92. // Replace interface configuration.
  93. new_config.cfg_iface_.reset(new CfgIface(*cfg_iface_));
  94. // Replace option definitions.
  95. cfg_option_def_->copyTo(*new_config.cfg_option_def_);
  96. cfg_option_->copyTo(*new_config.cfg_option_);
  97. // Replace the client class dictionary
  98. new_config.class_dictionary_.reset(new ClientClassDictionary(*class_dictionary_));
  99. // Replace the D2 client configuration
  100. new_config.setD2ClientConfig(getD2ClientConfig());
  101. // Replace configured hooks libraries.
  102. new_config.hooks_config_.clear();
  103. using namespace isc::hooks;
  104. for (HookLibsCollection::const_iterator it =
  105. hooks_config_.get().begin();
  106. it != hooks_config_.get().end(); ++it) {
  107. new_config.hooks_config_.add(it->first, it->second);
  108. }
  109. }
  110. void
  111. SrvConfig::applyLoggingCfg() const {
  112. std::list<LoggerSpecification> specs;
  113. for (LoggingInfoStorage::const_iterator it = logging_info_.begin();
  114. it != logging_info_.end(); ++it) {
  115. specs.push_back(it->toSpec());
  116. }
  117. LoggerManager manager;
  118. manager.process(specs.begin(), specs.end());
  119. }
  120. bool
  121. SrvConfig::equals(const SrvConfig& other) const {
  122. // If number of loggers is different, then configurations aren't equal.
  123. if (logging_info_.size() != other.logging_info_.size()) {
  124. return (false);
  125. }
  126. // Pass through all loggers and try to find the match for each of them
  127. // with the loggers from the other configuration. The order doesn't
  128. // matter so we can't simply compare the vectors.
  129. for (LoggingInfoStorage::const_iterator this_it =
  130. logging_info_.begin(); this_it != logging_info_.end();
  131. ++this_it) {
  132. bool match = false;
  133. for (LoggingInfoStorage::const_iterator other_it =
  134. other.logging_info_.begin();
  135. other_it != other.logging_info_.end(); ++other_it) {
  136. if (this_it->equals(*other_it)) {
  137. match = true;
  138. break;
  139. }
  140. }
  141. // No match found for the particular logger so return false.
  142. if (!match) {
  143. return (false);
  144. }
  145. }
  146. // Logging information is equal between objects, so check other values.
  147. if ((*cfg_iface_ != *other.cfg_iface_) ||
  148. (*cfg_option_def_ != *other.cfg_option_def_) ||
  149. (*cfg_option_ != *other.cfg_option_) ||
  150. (*class_dictionary_ != *other.class_dictionary_) ||
  151. (*d2_client_config_ != *other.d2_client_config_)) {
  152. return (false);
  153. }
  154. // Now only configured hooks libraries can differ.
  155. // If number of configured hooks libraries are different, then
  156. // configurations aren't equal.
  157. if (hooks_config_.get().size() != other.hooks_config_.get().size()) {
  158. return (false);
  159. }
  160. // Pass through all configured hooks libraries.
  161. return (hooks_config_.equal(other.hooks_config_));
  162. }
  163. void
  164. SrvConfig::removeStatistics() {
  165. // Removes statistics for v4 and v6 subnets
  166. getCfgSubnets4()->removeStatistics();
  167. getCfgSubnets6()->removeStatistics();
  168. }
  169. void
  170. SrvConfig::updateStatistics() {
  171. // Updating subnet statistics involves updating lease statistics, which
  172. // is done by the LeaseMgr. Since servers with subnets, must have a
  173. // LeaseMgr, we do not bother updating subnet stats for servers without
  174. // a lease manager, such as D2. @todo We should probably examine why
  175. // "SrvConfig" is being used by D2.
  176. if (LeaseMgrFactory::haveInstance()) {
  177. // Updates statistics for v4 and v6 subnets
  178. getCfgSubnets4()->updateStatistics();
  179. getCfgSubnets6()->updateStatistics();
  180. }
  181. }
  182. }
  183. }