simple_parser4.cc 5.82 KB
Newer Older
1
// Copyright (C) 2016-2017 Internet Systems Consortium, Inc. ("ISC")
2 3 4 5 6 7 8 9 10 11 12 13 14
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <dhcp4/simple_parser4.h>
#include <cc/data.h>
#include <boost/foreach.hpp>

using namespace isc::data;

namespace isc {
namespace dhcp {
15 16 17 18 19 20 21 22 23
/// @brief This sets of arrays define the default values and
///        values inherited (derived) between various scopes.
///
/// Each of those is documented in @file simple_parser4.cc. This
/// is different than most other comments in Kea code. The reason
/// for placing those in .cc rather than .h file is that it
/// is expected to be one centralized place to look at for
/// the default values. This is expected to be looked at also by
/// people who are not skilled in C or C++, so they may be
24
/// confused with the differences between declaration and definition.
25 26 27 28
/// As such, there's one file to look at that hopefully is readable
/// without any C or C++ skills.
///
/// @{
29

30 31 32 33
/// @brief This table defines default values for option definitions in DHCPv4.
///
/// Dhcp4 may contain an array called option-def that enumerates new option
/// definitions. This array lists default values for those option definitions.
34 35 36 37 38 39 40
const SimpleDefaults SimpleParser4::OPTION4_DEF_DEFAULTS = {
    { "record-types", Element::string,  ""},
    { "space",        Element::string,  "dhcp4"},
    { "array",        Element::boolean, "false"},
    { "encapsulate",  Element::string,  "" }
};

41 42 43 44 45
/// @brief This table defines default values for options in DHCPv4.
///
/// Dhcp4 usually contains option values (option-data) defined in global,
/// subnet, class or host reservations scopes. This array lists default values
/// for those option-data declarations.
46 47
const SimpleDefaults SimpleParser4::OPTION4_DEFAULTS = {
    { "space",        Element::string,  "dhcp4"},
48
    { "csv-format",   Element::boolean, "true"},
49
    { "always-send",  Element::boolean, "false"}
50 51
};

52 53 54 55 56
/// @brief This table defines default global values for DHCPv4
///
/// Some of the global parameters defined in the global scope (i.e. directly
/// in Dhcp4) are optional. If not defined, the following values will be
/// used.
57
const SimpleDefaults SimpleParser4::GLOBAL4_DEFAULTS = {
58 59 60 61 62 63 64
    { "renew-timer",              Element::integer, "900" },
    { "rebind-timer",             Element::integer, "1800" },
    { "valid-lifetime",           Element::integer, "7200" },
    { "decline-probation-period", Element::integer, "86400" }, // 24h
    { "dhcp4o6-port",             Element::integer, "0" },
    { "echo-client-id",           Element::boolean, "true" },
    { "match-client-id",          Element::boolean, "true" },
65
    { "next-server",              Element::string,  "0.0.0.0" }
66 67
};

68 69
/// @brief This table defines default values for each IPv4 subnet.
const SimpleDefaults SimpleParser4::SUBNET4_DEFAULTS = {
70 71 72 73 74 75 76
    { "id",               Element::integer, "0" }, // 0 means autogenerate
    { "interface",        Element::string,  "" },
    { "client-class",     Element::string,  "" },
    { "reservation-mode", Element::string,  "all" },
    { "4o6-interface",    Element::string,  "" },
    { "4o6-interface-id", Element::string,  "" },
    { "4o6-subnet",       Element::string,  "" },
77 78
};

79 80 81 82 83
/// @brief This table defines default values for interfaces for DHCPv4.
const SimpleDefaults SimpleParser4::IFACE4_DEFAULTS = {
    { "re-detect", Element::boolean, "true" }
};

84 85 86 87 88 89
/// @brief List of parameters that can be inherited from the global to subnet4 scope.
///
/// Some parameters may be defined on both global (directly in Dhcp4) and
/// subnet (Dhcp4/subnet4/...) scope. If not defined in the subnet scope,
/// the value is being inherited (derived) from the global scope. This
/// array lists all of such parameters.
90 91 92
const ParamsList SimpleParser4::INHERIT_GLOBAL_TO_SUBNET4 = {
    "renew-timer",
    "rebind-timer",
93 94 95
    "valid-lifetime",
    "match-client-id",
    "next-server"
96
};
97 98 99 100 101
/// @}

/// ---------------------------------------------------------------------------
/// --- end of default values -------------------------------------------------
/// ---------------------------------------------------------------------------
102 103 104 105 106 107 108

size_t SimpleParser4::setAllDefaults(isc::data::ElementPtr global) {
    size_t cnt = 0;

    // Set global defaults first.
    cnt = setDefaults(global, GLOBAL4_DEFAULTS);

109
    // Now set option definition defaults for each specified option definition
110 111 112 113 114 115 116
    ConstElementPtr option_defs = global->get("option-def");
    if (option_defs) {
        BOOST_FOREACH(ElementPtr option_def, option_defs->listValue()) {
            cnt += SimpleParser::setDefaults(option_def, OPTION4_DEF_DEFAULTS);
        }
    }

117
    // Set the defaults for option data
118 119
    ConstElementPtr options = global->get("option-data");
    if (options) {
120 121 122 123 124 125 126
        cnt += setListDefaults(options, OPTION4_DEFAULTS);
    }

    // Now set the defaults for defined subnets
    ConstElementPtr subnets = global->get("subnet4");
    if (subnets) {
        cnt += setListDefaults(subnets, SUBNET4_DEFAULTS);
127
    }
128

129 130 131 132 133 134 135
    // Set the defaults for interfaces config
    ConstElementPtr ifaces_cfg = global->get("interfaces-config");
    if (ifaces_cfg) {
        ElementPtr mutable_cfg = boost::const_pointer_cast<Element>(ifaces_cfg);
        cnt += setDefaults(mutable_cfg, IFACE4_DEFAULTS);
    }

136 137 138 139 140 141 142 143 144 145 146 147 148 149
    return (cnt);
}

size_t SimpleParser4::deriveParameters(isc::data::ElementPtr global) {
    size_t cnt = 0;

    // Now derive global parameters into subnets.
    ConstElementPtr subnets = global->get("subnet4");
    if (subnets) {
        BOOST_FOREACH(ElementPtr single_subnet, subnets->listValue()) {
            cnt += SimpleParser::deriveParams(global, single_subnet,
                                              INHERIT_GLOBAL_TO_SUBNET4);
        }
    }
150 151 152 153 154 155

    return (cnt);
}

};
};