srv_config.cc 5.5 KB

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