Kea 2.5.8
cfg_globals.cc
Go to the documentation of this file.
1// Copyright (C) 2021-2024 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
9
10using namespace isc::data;
11
12namespace isc {
13namespace dhcp {
14
15const std::map<std::string, int>
17 // Common parameters.
18 { "valid-lifetime", VALID_LIFETIME },
19 { "min-valid-lifetime", MIN_VALID_LIFETIME },
20 { "max-valid-lifetime", MAX_VALID_LIFETIME },
21 { "renew-timer", RENEW_TIMER },
22 { "rebind-timer", REBIND_TIMER },
23 { "decline-probation-period", DECLINE_PROBATION_PERIOD },
24 { "dhcp4o6-port", DHCP4O6_PORT },
25 { "comment", COMMENT },
26 { "server-tag", SERVER_TAG },
27 { "reservation-mode", RESERVATION_MODE },
28 { "reservations-global", RESERVATIONS_GLOBAL },
29 { "reservations-in-subnet", RESERVATIONS_IN_SUBNET },
30 { "reservations-out-of-pool", RESERVATIONS_OUT_OF_POOL },
31 { "calculate-tee-times", CALCULATE_TEE_TIMES },
32 { "t1-percent", T1_PERCENT },
33 { "t2-percent", T2_PERCENT },
34 { "hostname-char-set", HOSTNAME_CHAR_SET },
35 { "hostname-char-replacement", HOSTNAME_CHAR_REPLACEMENT },
36 { "ddns-send-updates", DDNS_SEND_UPDATES },
37 { "ddns-override-no-update", DDNS_OVERRIDE_NO_UPDATE },
38 { "ddns-override-client-update", DDNS_OVERRIDE_CLIENT_UPDATE },
39 { "ddns-replace-client-name", DDNS_REPLACE_CLIENT_NAME },
40 { "ddns-generated-prefix", DDNS_GENERATED_PREFIX },
41 { "ddns-qualifying-suffix", DDNS_QUALIFYING_SUFFIX },
42 { "store-extended-info", STORE_EXTENDED_INFO },
43 { "statistic-default-sample-count", STATISTIC_DEFAULT_SAMPLE_COUNT },
44 { "statistic-default-sample-age", STATISTIC_DEFAULT_SAMPLE_AGE },
45 { "cache-threshold", CACHE_THRESHOLD },
46 { "cache-max-age", CACHE_MAX_AGE },
47 { "early-global-reservations-lookup", EARLY_GLOBAL_RESERVATIONS_LOOKUP },
48 { "ip-reservations-unique", IP_RESERVATIONS_UNIQUE },
49 { "reservations-lookup-first", RESERVATIONS_LOOKUP_FIRST },
50 { "ddns-update-on-renew", DDNS_UPDATE_ON_RENEW },
51 { "parked-packet-limit", PARKED_PACKET_LIMIT },
52 { "allocator", ALLOCATOR },
53 { "ddns-ttl-percent", DDNS_TTL_PERCENT },
54 { "ddns-conflict-resolution-mode", DDNS_CONFLICT_RESOLUTION_MODE },
55 { "compatibility", COMPATIBILITY },
56 { "control-socket", CONTROL_SOCKET },
57 { "dhcp-ddns", DHCP_DDNS },
58 { "expired-leases-processing", EXPIRED_LEASES_PROCESSING },
59 { "multi-threading", MULTI_THREADING },
60 { "sanity-checks", SANITY_CHECKS },
61 { "dhcp-queue-control", DHCP_QUEUE_CONTROL },
62
63 // DHCPv4 specific parameters.
64 { "echo-client-id", ECHO_CLIENT_ID },
65 { "match-client-id", MATCH_CLIENT_ID },
66 { "authoritative", AUTHORITATIVE },
67 { "next-server", NEXT_SERVER },
68 { "server-hostname", SERVER_HOSTNAME },
69 { "boot-file-name", BOOT_FILE_NAME },
70 { "offer-lifetime", OFFER_LIFETIME },
71
72 // DHCPv6 specific parameters.
73 { "data-directory", DATA_DIRECTORY },
74 { "preferred-lifetime", PREFERRED_LIFETIME },
75 { "min-preferred-lifetime", MIN_PREFERRED_LIFETIME },
76 { "max-preferred-lifetime", MAX_PREFERRED_LIFETIME },
77 { "pd-allocator", PD_ALLOCATOR },
78 { "server-id", SERVER_ID }
79};
80
81// Load time sanity check.
82namespace {
83struct CfgGlobalsChecks {
84 CfgGlobalsChecks() {
85 // Check the size for missing entries.
87 isc_throw(Unexpected, "CfgGlobals::nameToIndex has "
89 << " elements (expected " << CfgGlobals::SIZE << ")");
90 }
91
92 // Build the name vector.
93 std::vector<std::string> names;
94 names.resize(CfgGlobals::SIZE);
95 for (auto const& it : CfgGlobals::nameToIndex) {
96 int idx = it.second;
97 if ((idx < 0) || (idx >= CfgGlobals::SIZE)) {
98 isc_throw(Unexpected, "invalid index " << idx
99 << " for name " << it.first);
100 }
101 if (!names[idx].empty()) {
102 isc_throw(Unexpected, "duplicated names for " << idx
103 << " got " << names[idx]);
104 }
105 names[idx] = it.first;
106 }
107
108 // No name should be empty.
109 for (int idx = 0; idx < CfgGlobals::SIZE; ++idx) {
110 if (names[idx].empty()) {
111 isc_throw(Unexpected, "missing name for " << idx);
112 }
113 }
114 }
115};
116
117CfgGlobalsChecks check;
118} // end of anonymous namespace
119
120CfgGlobals::CfgGlobals() : values_(SIZE) {
121}
122
124CfgGlobals::get(const std::string& name) const {
125 auto const& it = nameToIndex.find(name);
126 if (it == nameToIndex.cend()) {
127 isc_throw(NotFound, "invalid global parameter name '" << name << "'");
128 }
129 return (get(it->second));
130}
131
133CfgGlobals::get(int index) const {
134 if ((index < 0) || (index >= CfgGlobals::SIZE)) {
135 isc_throw(OutOfRange, "invalid global parameter index " << index);
136 }
137 return (values_[index]);
138}
139
140void
141CfgGlobals::set(const std::string& name, ConstElementPtr value) {
142 auto const& it = nameToIndex.find(name);
143 if (it == nameToIndex.cend()) {
144 isc_throw(NotFound, "invalid global parameter name '" << name << "'");
145 }
146
147 set(it->second, value);
148}
149
150void
152 if ((index < 0) || (index >= CfgGlobals::SIZE)) {
153 isc_throw(OutOfRange, "invalid global parameter index " << index);
154 }
155 values_[index] = value;
156}
157
158void
160 for (int idx = 0; idx < CfgGlobals::SIZE; ++idx) {
161 if (values_[idx]) {
162 values_[idx] = ConstElementPtr();
163 }
164 }
165}
166
169 MapType map;
170 for (auto const& it : nameToIndex) {
171 int idx = it.second;
172 ConstElementPtr value = values_[idx];
173 if (value) {
174 map.insert(make_pair(it.first, value));
175 }
176 }
177 return (map);
178}
179
183 for (auto const& it : nameToIndex) {
184 int idx = it.second;
185 ConstElementPtr value = values_[idx];
186 if (value) {
187 result->set(it.first, value);
188 }
189 }
190 return (result);
191}
192
193} // namespace isc::dhcp
194} // namespace isc
#define COMMENT
A generic exception that is thrown when an object can not be found.
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
A generic exception that is thrown when an unexpected error condition occurs.
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition: data.cc:304
void clear()
Clear configured parameter values.
Definition: cfg_globals.cc:159
void set(const std::string &name, isc::data::ConstElementPtr value)
Set a configured parameter value by name.
Definition: cfg_globals.cc:141
const MapType valuesMap() const
Returns configured parameters as a map.
Definition: cfg_globals.cc:168
CfgGlobals()
Instance members.
Definition: cfg_globals.cc:120
static const std::map< std::string, int > nameToIndex
Name to index map.
Definition: cfg_globals.h:108
std::map< std::string, isc::data::ConstElementPtr > MapType
Type of name and value map.
Definition: cfg_globals.h:149
isc::data::ElementPtr toElement() const
Unparse configured global parameters.
Definition: cfg_globals.cc:181
isc::data::ConstElementPtr get(const std::string &name) const
Get a configured parameter value by name.
Definition: cfg_globals.cc:124
std::vector< isc::data::ConstElementPtr > values_
Vectors of values.
Definition: cfg_globals.h:164
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:29
boost::shared_ptr< Element > ElementPtr
Definition: data.h:28
Defines the logger used by the top-level component of kea-lfc.