Kea: cfg_globals.cc Source File

| Kea 3.2.0-git

cfg_globals.cc
Go to the documentation of this file.
1// Copyright (C) 2021-2025 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>
8#include <dhcpsrv/cfg_globals.h>
9
10using namespace isc::data;
11
12namespace isc {
13namespace dhcp {
14
15const std::map<std::string, int>
16 CfgGlobals::nameToIndex = {
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 { "reservations-global", RESERVATIONS_GLOBAL },
28 { "reservations-in-subnet", RESERVATIONS_IN_SUBNET },
29 { "reservations-out-of-pool", RESERVATIONS_OUT_OF_POOL },
30 { "calculate-tee-times", CALCULATE_TEE_TIMES },
31 { "t1-percent", T1_PERCENT },
32 { "t2-percent", T2_PERCENT },
33 { "hostname-char-set", HOSTNAME_CHAR_SET },
34 { "hostname-char-replacement", HOSTNAME_CHAR_REPLACEMENT },
35 { "ddns-send-updates", DDNS_SEND_UPDATES },
36 { "ddns-override-no-update", DDNS_OVERRIDE_NO_UPDATE },
37 { "ddns-override-client-update", DDNS_OVERRIDE_CLIENT_UPDATE },
38 { "ddns-replace-client-name", DDNS_REPLACE_CLIENT_NAME },
39 { "ddns-generated-prefix", DDNS_GENERATED_PREFIX },
40 { "ddns-qualifying-suffix", DDNS_QUALIFYING_SUFFIX },
41 { "store-extended-info", STORE_EXTENDED_INFO },
42 { "statistic-default-sample-count", STATISTIC_DEFAULT_SAMPLE_COUNT },
43 { "statistic-default-sample-age", STATISTIC_DEFAULT_SAMPLE_AGE },
44 { "cache-threshold", CACHE_THRESHOLD },
45 { "cache-max-age", CACHE_MAX_AGE },
46 { "early-global-reservations-lookup", EARLY_GLOBAL_RESERVATIONS_LOOKUP },
47 { "ip-reservations-unique", IP_RESERVATIONS_UNIQUE },
48 { "reservations-lookup-first", RESERVATIONS_LOOKUP_FIRST },
49 { "ddns-update-on-renew", DDNS_UPDATE_ON_RENEW },
50 { "parked-packet-limit", PARKED_PACKET_LIMIT },
51 { "allocator", ALLOCATOR },
52 { "ddns-ttl-percent", DDNS_TTL_PERCENT },
53 { "ddns-conflict-resolution-mode", DDNS_CONFLICT_RESOLUTION_MODE },
54 { "compatibility", COMPATIBILITY },
55 { "dhcp-ddns", DHCP_DDNS },
56 { "expired-leases-processing", EXPIRED_LEASES_PROCESSING },
57 { "multi-threading", MULTI_THREADING },
58 { "sanity-checks", SANITY_CHECKS },
59 { "dhcp-queue-control", DHCP_QUEUE_CONTROL },
60 { "ddns-ttl", DDNS_TTL },
61 { "ddns-ttl-min", DDNS_TTL_MIN },
62 { "ddns-ttl-max", DDNS_TTL_MAX },
63 { "host-reservation-identifiers", HOST_RESERVATION_IDENTIFIERS },
64 { "adaptive-lease-time-threshold", ADAPTIVE_LEASE_TIME_THRESHOLD },
65
66 // DHCPv4 specific parameters.
67 { "echo-client-id", ECHO_CLIENT_ID },
68 { "match-client-id", MATCH_CLIENT_ID },
69 { "authoritative", AUTHORITATIVE },
70 { "next-server", NEXT_SERVER },
71 { "server-hostname", SERVER_HOSTNAME },
72 { "boot-file-name", BOOT_FILE_NAME },
73 { "offer-lifetime", OFFER_LIFETIME },
74 { "stash-agent-options", STASH_AGENT_OPTIONS },
75
76 // DHCPv6 specific parameters.
77 { "data-directory", DATA_DIRECTORY },
78 { "preferred-lifetime", PREFERRED_LIFETIME },
79 { "min-preferred-lifetime", MIN_PREFERRED_LIFETIME },
80 { "max-preferred-lifetime", MAX_PREFERRED_LIFETIME },
81 { "pd-allocator", PD_ALLOCATOR },
82 { "server-id", SERVER_ID },
83 { "allow-address-registration", ALLOW_ADDRESS_REGISTRATION },
84};
85
86// Load time sanity check.
87namespace {
88struct CfgGlobalsChecks {
89 CfgGlobalsChecks() {
90 // Check the size for missing entries.
91 if (CfgGlobals::nameToIndex.size() != CfgGlobals::SIZE) {
92 isc_throw(Unexpected, "CfgGlobals::nameToIndex has "
93 << CfgGlobals::nameToIndex.size()
94 << " elements (expected " << CfgGlobals::SIZE << ")");
95 }
96
97 // Build the name vector.
98 std::vector<std::string> names;
99 names.resize(CfgGlobals::SIZE);
100 for (auto const& it : CfgGlobals::nameToIndex) {
101 int idx = it.second;
102 if ((idx < 0) || (idx >= CfgGlobals::SIZE)) {
103 isc_throw(Unexpected, "invalid index " << idx
104 << " for name " << it.first);
105 }
106 if (!names[idx].empty()) {
107 isc_throw(Unexpected, "duplicated names for " << idx
108 << " got " << names[idx]);
109 }
110 names[idx] = it.first;
111 }
112
113 // No name should be empty.
114 for (int idx = 0; idx < CfgGlobals::SIZE; ++idx) {
115 if (names[idx].empty()) {
116 isc_throw(Unexpected, "missing name for " << idx);
117 }
118 }
119 }
120};
121
122CfgGlobalsChecks check;
123} // end of anonymous namespace
124
125CfgGlobals::CfgGlobals() : values_(SIZE) {
126}
127
128 ConstElementPtr
129CfgGlobals::get(const std::string& name) const {
130 auto const& it = nameToIndex.find(name);
131 if (it == nameToIndex.cend()) {
132 isc_throw(NotFound, "invalid global parameter name '" << name << "'");
133 }
134 return (get(it->second));
135}
136
137 ConstElementPtr
138 CfgGlobals::get(int index) const {
139 if ((index < 0) || (index >= CfgGlobals::SIZE)) {
140 isc_throw(OutOfRange, "invalid global parameter index " << index);
141 }
142 return (values_[index]);
143}
144
145void
146CfgGlobals::set(const std::string& name, ConstElementPtr value) {
147 auto const& it = nameToIndex.find(name);
148 if (it == nameToIndex.cend()) {
149 isc_throw(NotFound, "invalid global parameter name '" << name << "'");
150 }
151
152 set(it->second, value);
153}
154
155void
156 CfgGlobals::set(int index, ConstElementPtr value) {
157 if ((index < 0) || (index >= CfgGlobals::SIZE)) {
158 isc_throw(OutOfRange, "invalid global parameter index " << index);
159 }
160 values_[index] = value;
161}
162
163void
164CfgGlobals::clear() {
165 for (int idx = 0; idx < CfgGlobals::SIZE; ++idx) {
166 if (values_[idx]) {
167 values_[idx] = ConstElementPtr();
168 }
169 }
170}
171
172const CfgGlobals::MapType
173CfgGlobals::valuesMap() const {
174 MapType map;
175 for (auto const& it : nameToIndex) {
176 int idx = it.second;
177 ConstElementPtr value = values_[idx];
178 if (value) {
179 map.insert(make_pair(it.first, value));
180 }
181 }
182 return (map);
183}
184
185 ElementPtr
186CfgGlobals::toElement() const {
187 ElementPtr result = Element::createMap();
188 for (auto const& it : nameToIndex) {
189 int idx = it.second;
190 ConstElementPtr value = values_[idx];
191 if (value) {
192 result->set(it.first, value);
193 }
194 }
195 return (result);
196}
197
198} // namespace isc::dhcp
199} // namespace isc
cfg_globals.h
isc::NotFound
A generic exception that is thrown when an object can not be found.
Definition exceptions/exceptions.h:175
isc::OutOfRange
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
Definition exceptions/exceptions.h:115
isc::data::Element::createMap
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:354
isc::dhcp::CfgGlobals::clear
void clear()
Clear configured parameter values.
Definition cfg_globals.cc:164
isc::dhcp::CfgGlobals::set
void set(const std::string &name, isc::data::ConstElementPtr value)
Set a configured parameter value by name.
Definition cfg_globals.cc:146
isc::dhcp::CfgGlobals::valuesMap
const MapType valuesMap() const
Returns configured parameters as a map.
Definition cfg_globals.cc:173
isc::dhcp::CfgGlobals::SIZE
@ SIZE
Definition cfg_globals.h:109
isc::dhcp::CfgGlobals::CfgGlobals
CfgGlobals()
Instance members.
Definition cfg_globals.cc:125
isc::dhcp::CfgGlobals::nameToIndex
static const std::map< std::string, int > nameToIndex
Name to index map.
Definition cfg_globals.h:113
isc::dhcp::CfgGlobals::MapType
std::map< std::string, isc::data::ConstElementPtr > MapType
Type of name and value map.
Definition cfg_globals.h:154
isc::dhcp::CfgGlobals::toElement
isc::data::ElementPtr toElement() const
Unparse configured global parameters.
Definition cfg_globals.cc:186
isc::dhcp::CfgGlobals::get
isc::data::ConstElementPtr get(const std::string &name) const
Get a configured parameter value by name.
Definition cfg_globals.cc:129
isc::dhcp::CfgGlobals::values_
std::vector< isc::data::ConstElementPtr > values_
Vectors of values.
Definition cfg_globals.h:169
COMMENT
#define COMMENT
Definition d2_lexer.cc:1272
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
isc::data
Definition base_stamped_element.cc:12
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:30
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition data.h:29
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18