srv_config.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright (C) 2014-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. #include <config.h>
  7. #include <dhcpsrv/cfgmgr.h>
  8. #include <dhcpsrv/srv_config.h>
  9. #include <log/logger_manager.h>
  10. #include <log/logger_specification.h>
  11. #include <dhcp/pkt.h> // Needed for HWADDR_SOURCE_*
  12. #include <list>
  13. #include <sstream>
  14. using namespace isc::log;
  15. namespace isc {
  16. namespace dhcp {
  17. SrvConfig::SrvConfig()
  18. : sequence_(0), cfg_iface_(new CfgIface()),
  19. cfg_option_def_(new CfgOptionDef()), cfg_option_(new CfgOption()),
  20. cfg_subnets4_(new CfgSubnets4()), cfg_subnets6_(new CfgSubnets6()),
  21. cfg_hosts_(new CfgHosts()), cfg_rsoo_(new CfgRSOO()),
  22. cfg_expiration_(new CfgExpiration()), cfg_duid_(new CfgDUID()),
  23. cfg_db_access_(new CfgDbAccess()),
  24. cfg_host_operations4_(CfgHostOperations::createConfig4()),
  25. cfg_host_operations6_(CfgHostOperations::createConfig6()),
  26. class_dictionary_(new ClientClassDictionary()),
  27. decline_timer_(0), dhcp4o6_port_(0) {
  28. }
  29. SrvConfig::SrvConfig(const uint32_t sequence)
  30. : sequence_(sequence), cfg_iface_(new CfgIface()),
  31. cfg_option_def_(new CfgOptionDef()), cfg_option_(new CfgOption()),
  32. cfg_subnets4_(new CfgSubnets4()), cfg_subnets6_(new CfgSubnets6()),
  33. cfg_hosts_(new CfgHosts()), cfg_rsoo_(new CfgRSOO()),
  34. cfg_expiration_(new CfgExpiration()), cfg_duid_(new CfgDUID()),
  35. cfg_db_access_(new CfgDbAccess()),
  36. cfg_host_operations4_(CfgHostOperations::createConfig4()),
  37. cfg_host_operations6_(CfgHostOperations::createConfig6()),
  38. class_dictionary_(new ClientClassDictionary()),
  39. decline_timer_(0), dhcp4o6_port_(0) {
  40. }
  41. std::string
  42. SrvConfig::getConfigSummary(const uint32_t selection) const {
  43. std::ostringstream s;
  44. size_t subnets_num;
  45. if ((selection & CFGSEL_SUBNET4) == CFGSEL_SUBNET4) {
  46. subnets_num = getCfgSubnets4()->getAll()->size();
  47. if (subnets_num > 0) {
  48. s << "added IPv4 subnets: " << subnets_num;
  49. } else {
  50. s << "no IPv4 subnets!";
  51. }
  52. s << "; ";
  53. }
  54. if ((selection & CFGSEL_SUBNET6) == CFGSEL_SUBNET6) {
  55. subnets_num = getCfgSubnets6()->getAll()->size();
  56. if (subnets_num > 0) {
  57. s << "added IPv6 subnets: " << subnets_num;
  58. } else {
  59. s << "no IPv6 subnets!";
  60. }
  61. s << "; ";
  62. }
  63. if ((selection & CFGSEL_DDNS) == CFGSEL_DDNS) {
  64. bool ddns_enabled = CfgMgr::instance().ddnsEnabled();
  65. s << "DDNS: " << (ddns_enabled ? "enabled" : "disabled") << "; ";
  66. }
  67. if (s.tellp() == static_cast<std::streampos>(0)) {
  68. s << "no config details available";
  69. }
  70. std::string summary = s.str();
  71. size_t last_separator_pos = summary.find_last_of(";");
  72. if (last_separator_pos == summary.length() - 2) {
  73. summary.erase(last_separator_pos);
  74. }
  75. return (summary);
  76. }
  77. bool
  78. SrvConfig::sequenceEquals(const SrvConfig& other) {
  79. return (getSequence() == other.getSequence());
  80. }
  81. void
  82. SrvConfig::copy(SrvConfig& new_config) const {
  83. // We will entirely replace loggers in the new configuration.
  84. new_config.logging_info_.clear();
  85. for (LoggingInfoStorage::const_iterator it = logging_info_.begin();
  86. it != logging_info_.end(); ++it) {
  87. new_config.addLoggingInfo(*it);
  88. }
  89. // Replace interface configuration.
  90. new_config.cfg_iface_.reset(new CfgIface(*cfg_iface_));
  91. // Replace option definitions.
  92. cfg_option_def_->copyTo(*new_config.cfg_option_def_);
  93. cfg_option_->copyTo(*new_config.cfg_option_);
  94. // Replace the client class dictionary
  95. new_config.class_dictionary_.reset(new ClientClassDictionary(*class_dictionary_));
  96. }
  97. void
  98. SrvConfig::applyLoggingCfg() const {
  99. std::list<LoggerSpecification> specs;
  100. for (LoggingInfoStorage::const_iterator it = logging_info_.begin();
  101. it != logging_info_.end(); ++it) {
  102. specs.push_back(it->toSpec());
  103. }
  104. LoggerManager manager;
  105. manager.process(specs.begin(), specs.end());
  106. }
  107. bool
  108. SrvConfig::equals(const SrvConfig& other) const {
  109. // If number of loggers is different, then configurations aren't equal.
  110. if (logging_info_.size() != other.logging_info_.size()) {
  111. return (false);
  112. }
  113. // Pass through all loggers and try to find the match for each of them
  114. // with the loggers from the other configuration. The order doesn't
  115. // matter so we can't simply compare the vectors.
  116. for (LoggingInfoStorage::const_iterator this_it =
  117. logging_info_.begin(); this_it != logging_info_.end();
  118. ++this_it) {
  119. bool match = false;
  120. for (LoggingInfoStorage::const_iterator other_it =
  121. other.logging_info_.begin();
  122. other_it != other.logging_info_.end(); ++other_it) {
  123. if (this_it->equals(*other_it)) {
  124. match = true;
  125. break;
  126. }
  127. }
  128. // No match found for the particular logger so return false.
  129. if (!match) {
  130. return (false);
  131. }
  132. }
  133. // Logging information is equal between objects, so check other values.
  134. return ((*cfg_iface_ == *other.cfg_iface_) &&
  135. (*cfg_option_def_ == *other.cfg_option_def_) &&
  136. (*cfg_option_ == *other.cfg_option_) &&
  137. (*class_dictionary_ == *other.class_dictionary_));
  138. }
  139. void
  140. SrvConfig::removeStatistics() {
  141. // Removes statistics for v4 and v6 subnets
  142. getCfgSubnets4()->removeStatistics();
  143. getCfgSubnets6()->removeStatistics();
  144. }
  145. void
  146. SrvConfig::updateStatistics() {
  147. // Updates statistics for v4 and v6 subnets
  148. getCfgSubnets4()->updateStatistics();
  149. getCfgSubnets6()->updateStatistics();
  150. }
  151. }
  152. }