Kea 3.1.1
base_network_parser.h
Go to the documentation of this file.
1// Copyright (C) 2019-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#ifndef BASE_NETWORK_PARSER_H
8#define BASE_NETWORK_PARSER_H
9
10#include <cc/data.h>
11#include <cc/simple_parser.h>
12#include <dhcpsrv/cfg_globals.h>
13#include <dhcpsrv/network.h>
14
15namespace isc {
16namespace dhcp {
17
21protected:
22
38 void parseCommon(const data::ConstElementPtr& network_data,
39 NetworkPtr& network);
40
55 void parseTeePercents(const data::ConstElementPtr& network_data,
56 NetworkPtr& network);
57
71 void parseCacheParams(const data::ConstElementPtr& network_data,
72 NetworkPtr& network);
73
92 void parseDdnsParams(const data::ConstElementPtr& network_data,
93 NetworkPtr& network);
94
104 void parseAllocatorParams(const data::ConstElementPtr& network_data,
105 NetworkPtr& network);
106
117 void parsePdAllocatorParams(const data::ConstElementPtr& network_data,
118 Network6Ptr& network);
119
132 void parseAdaptiveLeaseTimeParam(const data::ConstElementPtr& network_data,
133 NetworkPtr& network);
134
142 void parseOfferLft(const data::ConstElementPtr& network_data,
143 Network4Ptr& network);
144public:
145 typedef std::function<void(const isc::dhcp::ClientClass&)> ClassAdderFunc;
146
154 ClassAdderFunc adder_func);
155
163 ClassAdderFunc adder_func);
164
190 template<typename DdnsOwnerTypePtr>
192 DdnsOwnerTypePtr owner) {
193
194 if (config->contains("ddns-send-updates")) {
195 owner->setDdnsSendUpdates(getBoolean(config, "ddns-send-updates"));
196 }
197
198 if (config->contains("ddns-override-no-update")) {
199 owner->setDdnsOverrideNoUpdate(getBoolean(config, "ddns-override-no-update"));
200 }
201
202 if (config->contains("ddns-override-client-update")) {
203 owner->setDdnsOverrideClientUpdate(getBoolean(config, "ddns-override-client-update"));
204 }
205
206 if (config->contains("ddns-replace-client-name")) {
207 owner->setDdnsReplaceClientNameMode(getAndConvert<D2ClientConfig::ReplaceClientNameMode,
209 (config, "ddns-replace-client-name",
210 "ReplaceClientName mode"));
211 }
212
213 if (config->contains("ddns-generated-prefix")) {
214 owner->setDdnsGeneratedPrefix(getString(config, "ddns-generated-prefix"));
215 }
216
217 if (config->contains("ddns-qualifying-suffix")) {
218 owner->setDdnsQualifyingSuffix(getString(config, "ddns-qualifying-suffix"));
219 }
220
221 std::string hostname_char_set;
222 if (config->contains("hostname-char-set")) {
223 hostname_char_set = getString(config, "hostname-char-set");
224 owner->setHostnameCharSet(hostname_char_set);
225 }
226
227 std::string hostname_char_replacement;
228 if (config->contains("hostname-char-replacement")) {
229 hostname_char_replacement = getString(config, "hostname-char-replacement");
230 owner->setHostnameCharReplacement(hostname_char_replacement);
231 }
232
233 // We need to validate sanitizer values here so we can detect problems and
234 // cause a configuration. We don't retain the compilation because it's not
235 // something we can inherit.
236 if (!hostname_char_set.empty()) {
237 try {
239 new util::str::StringSanitizer(hostname_char_set,
240 hostname_char_replacement));
241 } catch (const std::exception& ex) {
242 isc_throw(BadValue, "hostname-char-set '" << hostname_char_set
243 << "' is not a valid regular expression");
244 }
245 }
246
247 if (config->contains("ddns-update-on-renew")) {
248 owner->setDdnsUpdateOnRenew(getBoolean(config, "ddns-update-on-renew"));
249 }
250
251 bool has_ddns_ttl = false;
252 uint32_t ddns_ttl = 0;
253 if (config->contains("ddns-ttl")) {
254 ddns_ttl = getInteger(config, "ddns-ttl");
255 owner->setDdnsTtl(ddns_ttl);
256 has_ddns_ttl = true;
257 }
258
259 if (config->contains("ddns-ttl-percent")) {
260 if (has_ddns_ttl) {
261 isc_throw(BadValue, "cannot specify both ddns-ttl-percent and ddns-ttl");
262 }
263
264 owner->setDdnsTtlPercent(getDouble(config, "ddns-ttl-percent"));
265 }
266
267 uint32_t ddns_ttl_min = 0;
268 if (config->contains("ddns-ttl-min")) {
269 if (has_ddns_ttl) {
270 isc_throw(BadValue, "cannot specify both ddns-ttl-min and ddns-ttl");
271 }
272
273 ddns_ttl_min = getInteger(config, "ddns-ttl-min");
274 owner->setDdnsTtlMin(ddns_ttl_min);
275 }
276
277 if (config->contains("ddns-ttl-max")) {
278 if (has_ddns_ttl) {
279 isc_throw(BadValue, "cannot specify both ddns-ttl-max and ddns-ttl");
280 }
281
282 uint32_t ddns_ttl_max = getInteger(config, "ddns-ttl-max");
283 if (ddns_ttl_max < ddns_ttl_min) {
284 isc_throw(BadValue, "ddns-ttl-max: " << ddns_ttl_max
285 << " must be greater than ddns-ttl-min: " << ddns_ttl_min);
286 }
287
288 owner->setDdnsTtlMax(ddns_ttl_max);
289 }
290
291 // For backward compatibility, ddns-conflict-resolution-mode is optional.
292 if (config->contains("ddns-conflict-resolution-mode")) {
293 owner->setDdnsConflictResolutionMode(getString(config,
294 "ddns-conflict-resolution-mode"));
295 }
296 }
297};
298
299} // end of namespace isc::dhcp
300} // end of namespace isc
301
302#endif
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
target_type getAndConvert(isc::data::ConstElementPtr scope, const std::string &name, const std::string &type_name)
Returns a converted value from a scope.
static double getDouble(const ConstElementPtr &scope, const std::string &name)
Returns a floating point parameter from a scope.
static std::string getString(isc::data::ConstElementPtr scope, const std::string &name)
Returns a string parameter from a scope.
static bool getBoolean(isc::data::ConstElementPtr scope, const std::string &name)
Returns a boolean parameter from a scope.
static int64_t getInteger(isc::data::ConstElementPtr scope, const std::string &name)
Returns an integer parameter from a scope.
Common configuration parser for shared networks and subnets.
void parseCacheParams(const data::ConstElementPtr &network_data, NetworkPtr &network)
Parses parameters related to lease cache settings.
void parseAllocatorParams(const data::ConstElementPtr &network_data, NetworkPtr &network)
Parses parameters pertaining to allocator selection.
static void getClientClassesElem(data::ConstElementPtr params, ClassAdderFunc adder_func)
Fetches the element for either 'client-classes' or deprecated 'client-class'.
void parseDdnsParams(const data::ConstElementPtr &network_data, NetworkPtr &network)
Parses parameters pertaining to DDNS behavior.
void parseCommon(const data::ConstElementPtr &network_data, NetworkPtr &network)
Parses common parameters.
void parseAdaptiveLeaseTimeParam(const data::ConstElementPtr &network_data, NetworkPtr &network)
Parses parameter related to adaptive lease time.
std::function< void(const isc::dhcp::ClientClass &)> ClassAdderFunc
void parseDdnsParameters(const data::ConstElementPtr &config, DdnsOwnerTypePtr owner)
Parses parameters pertaining to DDNS behavior.
static void getAdditionalClassesElem(data::ConstElementPtr params, ClassAdderFunc adder_func)
Fetches the element for either 'evaluate-additional-classes' or deprecated 'require-client-classes'.
void parseTeePercents(const data::ConstElementPtr &network_data, NetworkPtr &network)
Parses parameters related to "percent" timers settings.
void parsePdAllocatorParams(const data::ConstElementPtr &network_data, Network6Ptr &network)
Parses parameters pertaining to prefix delegation allocator selection.
void parseOfferLft(const data::ConstElementPtr &network_data, Network4Ptr &network)
Parses offer-lifetime parameter (v4 only)
static ReplaceClientNameMode stringToReplaceClientNameMode(const std::string &mode_str)
Converts labels to ReplaceClientNameMode enum values.
ReplaceClientNameMode
Defines the client name replacement modes.
Implements a regular expression based string scrubber.
Definition str.h:222
#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
std::string ClientClass
Defines a single class name.
Definition classify.h:44
boost::shared_ptr< Network4 > Network4Ptr
Pointer to the Network4 object.
Definition network.h:1503
boost::shared_ptr< Network6 > Network6Ptr
Pointer to the Network6 object.
Definition network.h:1508
boost::shared_ptr< Network > NetworkPtr
Pointer to the Network object.
Definition network.h:73
std::unique_ptr< StringSanitizer > StringSanitizerPtr
Type representing the pointer to the StringSanitizer.
Definition str.h:263
Defines the logger used by the top-level component of kea-lfc.