srv_config.cc 4.3 KB

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