Kea  2.1.7-git
cfg_globals.cc
Go to the documentation of this file.
1 // Copyright (C) 2021-2022 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 
54  // DHCPv4 specific parameters.
55  { "echo-client-id", ECHO_CLIENT_ID },
56  { "match-client-id", MATCH_CLIENT_ID },
57  { "authoritative", AUTHORITATIVE },
58  { "next-server", NEXT_SERVER },
59  { "server-hostname", SERVER_HOSTNAME },
60  { "boot-file-name", BOOT_FILE_NAME },
61 
62  // DHCPv6 specific parameters.
63  { "data-directory", DATA_DIRECTORY },
64  { "preferred-lifetime", PREFERRED_LIFETIME },
65  { "min-preferred-lifetime", MIN_PREFERRED_LIFETIME },
66  { "max-preferred-lifetime", MAX_PREFERRED_LIFETIME }
67 };
68 
69 // Load time sanity check.
70 namespace {
71 struct CfgGlobalsChecks {
72  CfgGlobalsChecks() {
73  // Check the size for missing entries.
74  if (CfgGlobals::nameToIndex.size() != CfgGlobals::SIZE) {
75  isc_throw(Unexpected, "CfgGlobals::nameToIndex has "
76  << CfgGlobals::nameToIndex.size()
77  << " elements (expected " << CfgGlobals::SIZE << ")");
78  }
79 
80  // Build the name vector.
81  std::vector<std::string> names;
82  names.resize(CfgGlobals::SIZE);
83  for (auto it = CfgGlobals::nameToIndex.cbegin();
84  it != CfgGlobals::nameToIndex.cend(); ++it) {
85  int idx = it->second;
86  if ((idx < 0) || (idx >= CfgGlobals::SIZE)) {
87  isc_throw(Unexpected, "invalid index " << idx
88  << " for name " << it->first);
89  }
90  if (!names[idx].empty()) {
91  isc_throw(Unexpected, "duplicated names for " << idx
92  << " got " << names[idx]);
93  }
94  names[idx] = it->first;
95  }
96 
97  // No name should be empty.
98  for (int idx = 0; idx < CfgGlobals::SIZE; ++idx) {
99  if (names[idx].empty()) {
100  isc_throw(Unexpected, "missing name for " << idx);
101  }
102  }
103  }
104 };
105 
106 CfgGlobalsChecks check;
107 } // end of anonymous namespace
108 
109 CfgGlobals::CfgGlobals() : values_(SIZE) {
110 }
111 
113 CfgGlobals::get(const std::string& name) const {
114  auto const& it = nameToIndex.find(name);
115  if (it == nameToIndex.cend()) {
116  isc_throw(NotFound, "invalid global parameter name '" << name << "'");
117  }
118  return (get(it->second));
119 }
120 
122 CfgGlobals::get(int index) const {
123  if ((index < 0) || (index >= CfgGlobals::SIZE)) {
124  isc_throw(OutOfRange, "invalid global parameter index " << index);
125  }
126  return (values_[index]);
127 }
128 
129 void
130 CfgGlobals::set(const std::string& name, ConstElementPtr value) {
131  auto const& it = nameToIndex.find(name);
132  if (it == nameToIndex.cend()) {
133  isc_throw(NotFound, "invalid global parameter name '" << name << "'");
134  }
135  set(it->second, value);
136 }
137 
138 void
139 CfgGlobals::set(int index, ConstElementPtr value) {
140  if ((index < 0) || (index >= CfgGlobals::SIZE)) {
141  isc_throw(OutOfRange, "invalid global parameter index " << index);
142  }
143  values_[index] = value;
144 }
145 
146 void
148  for (int idx = 0; idx < CfgGlobals::SIZE; ++idx) {
149  if (values_[idx]) {
150  values_[idx] = ConstElementPtr();
151  }
152  }
153 }
154 
157  MapType map;
158  for (auto it = nameToIndex.cbegin(); it != nameToIndex.cend(); ++it) {
159  int idx = it->second;
160  ConstElementPtr value = values_[idx];
161  if (value) {
162  map.insert(make_pair(it->first, value));
163  }
164  }
165  return (map);
166 }
167 
170  ElementPtr result = Element::createMap();
171  for (auto it = nameToIndex.cbegin(); it != nameToIndex.cend(); ++it) {
172  int idx = it->second;
173  ConstElementPtr value = values_[idx];
174  if (value) {
175  result->set(it->first, value);
176  }
177  }
178  return (result);
179 }
180 
181 } // namespace isc::dhcp
182 } // namespace isc
static const std::map< std::string, int > nameToIndex
Name to index map.
Definition: cfg_globals.h:96
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
std::vector< isc::data::ConstElementPtr > values_
Vectors of values.
Definition: cfg_globals.h:152
std::map< std::string, isc::data::ConstElementPtr > MapType
Type of name and value map.
Definition: cfg_globals.h:137
#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:156
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
void clear()
Clear configured parameter values.
Definition: cfg_globals.cc:147
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:130
isc::data::ConstElementPtr get(const std::string &name) const
Get a configured parameter value by name.
Definition: cfg_globals.cc:113
#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:169