srv_config.cc 5.5 KB
Newer Older
1
// Copyright (C) 2014-2015 Internet Systems Consortium, Inc. ("ISC")
2 3 4 5 6 7 8 9 10 11 12 13 14
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

15
#include <config.h>
16
#include <dhcpsrv/cfgmgr.h>
17
#include <dhcpsrv/srv_config.h>
18 19
#include <log/logger_manager.h>
#include <log/logger_specification.h>
20
#include <dhcp/pkt.h> // Needed for HWADDR_SOURCE_*
21
#include <list>
22 23
#include <sstream>

24 25
using namespace isc::log;

26 27 28
namespace isc {
namespace dhcp {

29
SrvConfig::SrvConfig()
30 31 32
    : sequence_(0), cfg_iface_(new CfgIface()),
      cfg_option_def_(new CfgOptionDef()), cfg_option_(new CfgOption()),
      cfg_subnets4_(new CfgSubnets4()), cfg_subnets6_(new CfgSubnets6()),
33
      cfg_hosts_(new CfgHosts()), cfg_rsoo_(new CfgRSOO()) {
34 35
}

36
SrvConfig::SrvConfig(const uint32_t sequence)
37 38 39
    : sequence_(sequence), cfg_iface_(new CfgIface()),
      cfg_option_def_(new CfgOptionDef()), cfg_option_(new CfgOption()),
      cfg_subnets4_(new CfgSubnets4()), cfg_subnets6_(new CfgSubnets6()),
40
      cfg_hosts_(new CfgHosts()), cfg_rsoo_(new CfgRSOO()) {
41 42
}

43
std::string
44
SrvConfig::getConfigSummary(const uint32_t selection) const {
45 46 47
    std::ostringstream s;
    size_t subnets_num;
    if ((selection & CFGSEL_SUBNET4) == CFGSEL_SUBNET4) {
48
        subnets_num = getCfgSubnets4()->getAll()->size();
49 50 51 52 53 54 55 56 57
        if (subnets_num > 0) {
            s << "added IPv4 subnets: " << subnets_num;
        } else {
            s << "no IPv4 subnets!";
        }
        s << "; ";
    }

    if ((selection & CFGSEL_SUBNET6) == CFGSEL_SUBNET6) {
58
        subnets_num = getCfgSubnets6()->getAll()->size();
59 60 61 62 63 64 65 66
        if (subnets_num > 0) {
            s << "added IPv6 subnets: " << subnets_num;
        } else {
            s << "no IPv6 subnets!";
        }
        s << "; ";
    }

67 68 69 70 71
    if ((selection & CFGSEL_DDNS) == CFGSEL_DDNS) {
        bool ddns_enabled = CfgMgr::instance().ddnsEnabled();
        s << "DDNS: " << (ddns_enabled ? "enabled" : "disabled") << "; ";
    }

72
    if (s.tellp() == static_cast<std::streampos>(0)) {
73 74 75 76 77 78 79 80 81 82 83
        s << "no config details available";
    }

    std::string summary = s.str();
    size_t last_separator_pos = summary.find_last_of(";");
    if (last_separator_pos == summary.length() - 2) {
        summary.erase(last_separator_pos);
    }
    return (summary);
}

84
bool
85
SrvConfig::sequenceEquals(const SrvConfig& other) {
86 87 88
    return (getSequence() == other.getSequence());
}

89
void
90
SrvConfig::copy(SrvConfig& new_config) const {
91 92 93 94 95 96 97
    // We will entirely replace loggers in the new configuration.
    new_config.logging_info_.clear();
    for (LoggingInfoStorage::const_iterator it = logging_info_.begin();
         it != logging_info_.end(); ++it) {
        new_config.addLoggingInfo(*it);
    }
    // Replace interface configuration.
98
    new_config.cfg_iface_.reset(new CfgIface(*cfg_iface_));
99
    // Replace option definitions.
100
    cfg_option_def_->copyTo(*new_config.cfg_option_def_);
101
    cfg_option_->copyTo(*new_config.cfg_option_);
102 103
}

104
void
105
SrvConfig::applyLoggingCfg() const {
106 107 108 109 110 111 112 113 114 115

    std::list<LoggerSpecification> specs;
    for (LoggingInfoStorage::const_iterator it = logging_info_.begin();
         it != logging_info_.end(); ++it) {
        specs.push_back(it->toSpec());
    }
    LoggerManager manager;
    manager.process(specs.begin(), specs.end());
}

116
bool
117
SrvConfig::equals(const SrvConfig& other) const {
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    // If number of loggers is different, then configurations aren't equal.
    if (logging_info_.size() != other.logging_info_.size()) {
        return (false);
    }
    // Pass through all loggers and try to find the match for each of them
    // with the loggers from the other configuration. The order doesn't
    // matter so we can't simply compare the vectors.
    for (LoggingInfoStorage::const_iterator this_it =
             logging_info_.begin(); this_it != logging_info_.end();
         ++this_it) {
        bool match = false;
        for (LoggingInfoStorage::const_iterator other_it =
                 other.logging_info_.begin();
             other_it != other.logging_info_.end(); ++other_it) {
            if (this_it->equals(*other_it)) {
                match = true;
                break;
            }
        }
        // No match found for the particular logger so return false.
        if (!match) {
            return (false);
        }
    }
    // Logging information is equal between objects, so check other values.
143
    return ((*cfg_iface_ == *other.cfg_iface_) &&
144 145
            (*cfg_option_def_ == *other.cfg_option_def_) &&
            (*cfg_option_ == *other.cfg_option_));
146 147
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
void
SrvConfig::removeStatistics() {

    // For now, this method only removes statistics for v4 subnets, but in the
    // near future, we'll also get statistics for v6 subnets.
    getCfgSubnets4()->removeStatistics();
}

void
SrvConfig::updateStatistics() {
    // For now, this method only updates statistics for v4 subnets, but in the
    // near future, we'll also get statistics for v6 subnets.
    getCfgSubnets4()->updateStatistics();
}

163 164
}
}