srv_config.cc 4.7 KB

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