Kea  2.3.5-git
cfg_globals.cc
Go to the documentation of this file.
1 // Copyright (C) 2021-2023 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 
10 using namespace isc::data;
11 
12 namespace isc {
13 namespace dhcp {
14 
15 const 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  { "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  { "ddns-use-conflict-resolution", DDNS_USE_CONFLICT_RESOLUTION },
52  { "parked-packet-limit", PARKED_PACKET_LIMIT },
53  { "allocator", ALLOCATOR },
54 
55  // DHCPv4 specific parameters.
56  { "echo-client-id", ECHO_CLIENT_ID },
57  { "match-client-id", MATCH_CLIENT_ID },
58  { "authoritative", AUTHORITATIVE },
59  { "next-server", NEXT_SERVER },
60  { "server-hostname", SERVER_HOSTNAME },
61  { "boot-file-name", BOOT_FILE_NAME },
62 
63  // DHCPv6 specific parameters.
64  { "data-directory", DATA_DIRECTORY },
65  { "preferred-lifetime", PREFERRED_LIFETIME },
66  { "min-preferred-lifetime", MIN_PREFERRED_LIFETIME },
67  { "max-preferred-lifetime", MAX_PREFERRED_LIFETIME },
68  { "pd-allocator", PD_ALLOCATOR }
69 };
70 
71 // Load time sanity check.
72 namespace {
73 struct CfgGlobalsChecks {
74  CfgGlobalsChecks() {
75  // Check the size for missing entries.
76  if (CfgGlobals::nameToIndex.size() != CfgGlobals::SIZE) {
77  isc_throw(Unexpected, "CfgGlobals::nameToIndex has "
78  << CfgGlobals::nameToIndex.size()
79  << " elements (expected " << CfgGlobals::SIZE << ")");
80  }
81 
82  // Build the name vector.
83  std::vector<std::string> names;
84  names.resize(CfgGlobals::SIZE);
85  for (auto it = CfgGlobals::nameToIndex.cbegin();
86  it != CfgGlobals::nameToIndex.cend(); ++it) {
87  int idx = it->second;
88  if ((idx < 0) || (idx >= CfgGlobals::SIZE)) {
89  isc_throw(Unexpected, "invalid index " << idx
90  << " for name " << it->first);
91  }
92  if (!names[idx].empty()) {
93  isc_throw(Unexpected, "duplicated names for " << idx
94  << " got " << names[idx]);
95  }
96  names[idx] = it->first;
97  }
98 
99  // No name should be empty.
100  for (int idx = 0; idx < CfgGlobals::SIZE; ++idx) {
101  if (names[idx].empty()) {
102  isc_throw(Unexpected, "missing name for " << idx);
103  }
104  }
105  }
106 };
107 
108 CfgGlobalsChecks check;
109 } // end of anonymous namespace
110 
111 CfgGlobals::CfgGlobals() : values_(SIZE) {
112 }
113 
115 CfgGlobals::get(const std::string& name) const {
116  auto const& it = nameToIndex.find(name);
117  if (it == nameToIndex.cend()) {
118  isc_throw(NotFound, "invalid global parameter name '" << name << "'");
119  }
120  return (get(it->second));
121 }
122 
124 CfgGlobals::get(int index) const {
125  if ((index < 0) || (index >= CfgGlobals::SIZE)) {
126  isc_throw(OutOfRange, "invalid global parameter index " << index);
127  }
128  return (values_[index]);
129 }
130 
131 void
132 CfgGlobals::set(const std::string& name, ConstElementPtr value) {
133  auto const& it = nameToIndex.find(name);
134  if (it == nameToIndex.cend()) {
135  isc_throw(NotFound, "invalid global parameter name '" << name << "'");
136  }
137  set(it->second, value);
138 }
139 
140 void
141 CfgGlobals::set(int index, ConstElementPtr value) {
142  if ((index < 0) || (index >= CfgGlobals::SIZE)) {
143  isc_throw(OutOfRange, "invalid global parameter index " << index);
144  }
145  values_[index] = value;
146 }
147 
148 void
150  for (int idx = 0; idx < CfgGlobals::SIZE; ++idx) {
151  if (values_[idx]) {
152  values_[idx] = ConstElementPtr();
153  }
154  }
155 }
156 
159  MapType map;
160  for (auto it = nameToIndex.cbegin(); it != nameToIndex.cend(); ++it) {
161  int idx = it->second;
162  ConstElementPtr value = values_[idx];
163  if (value) {
164  map.insert(make_pair(it->first, value));
165  }
166  }
167  return (map);
168 }
169 
172  ElementPtr result = Element::createMap();
173  for (auto it = nameToIndex.cbegin(); it != nameToIndex.cend(); ++it) {
174  int idx = it->second;
175  ConstElementPtr value = values_[idx];
176  if (value) {
177  result->set(it->first, value);
178  }
179  }
180  return (result);
181 }
182 
183 } // namespace isc::dhcp
184 } // namespace isc
static const std::map< std::string, int > nameToIndex
Name to index map.
Definition: cfg_globals.h:98
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
std::vector< isc::data::ConstElementPtr > values_
Vectors of values.
Definition: cfg_globals.h:154
std::map< std::string, isc::data::ConstElementPtr > MapType
Type of name and value map.
Definition: cfg_globals.h:139
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition: data.cc:291
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
const MapType valuesMap() const
Returns configured parameters as a map.
Definition: cfg_globals.cc:158
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
void clear()
Clear configured parameter values.
Definition: cfg_globals.cc:149
Defines the logger used by the top-level component of kea-lfc.
void set(const std::string &name, isc::data::ConstElementPtr value)
Set a configured parameter value by name.
Definition: cfg_globals.cc:132
isc::data::ConstElementPtr get(const std::string &name) const
Get a configured parameter value by name.
Definition: cfg_globals.cc:115
#define COMMENT
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 object can not be found.
isc::data::ElementPtr toElement() const
Unparse configured global parameters.
Definition: cfg_globals.cc:171