configuration.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/configuration.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. Configuration::Configuration()
  24. : sequence_(0) {
  25. }
  26. Configuration::Configuration(uint32_t sequence)
  27. : sequence_(sequence) {
  28. }
  29. std::string
  30. Configuration::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. Configuration::sequenceEquals(const Configuration& other) {
  67. return (getSequence() == other.getSequence());
  68. }
  69. void
  70. Configuration::copy(Configuration& 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. Configuration::applyLoggingCfg() const {
  82. /// @todo Remove the hardcoded location.
  83. setenv("KEA_LOCKFILE_DIR_FROM_BUILD", "/tmp", 1);
  84. std::list<LoggerSpecification> specs;
  85. for (LoggingInfoStorage::const_iterator it = logging_info_.begin();
  86. it != logging_info_.end(); ++it) {
  87. specs.push_back(it->toSpec());
  88. }
  89. LoggerManager manager;
  90. manager.process(specs.begin(), specs.end());
  91. }
  92. bool
  93. Configuration::equals(const Configuration& other) const {
  94. // If number of loggers is different, then configurations aren't equal.
  95. if (logging_info_.size() != other.logging_info_.size()) {
  96. return (false);
  97. }
  98. // Pass through all loggers and try to find the match for each of them
  99. // with the loggers from the other configuration. The order doesn't
  100. // matter so we can't simply compare the vectors.
  101. for (LoggingInfoStorage::const_iterator this_it =
  102. logging_info_.begin(); this_it != logging_info_.end();
  103. ++this_it) {
  104. bool match = false;
  105. for (LoggingInfoStorage::const_iterator other_it =
  106. other.logging_info_.begin();
  107. other_it != other.logging_info_.end(); ++other_it) {
  108. if (this_it->equals(*other_it)) {
  109. match = true;
  110. break;
  111. }
  112. }
  113. // No match found for the particular logger so return false.
  114. if (!match) {
  115. return (false);
  116. }
  117. }
  118. // Logging information is equal between objects, so check other values.
  119. return (cfg_iface_ == other.cfg_iface_);
  120. }
  121. }
  122. }