Kea 2.7.6
network.cc
Go to the documentation of this file.
1// Copyright (C) 2017-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>
8
9#include <dhcp/dhcp4.h>
10#include <dhcp/option_custom.h>
11#include <dhcp/option_space.h>
12#include <dhcpsrv/network.h>
13#include <boost/pointer_cast.hpp>
14
15using namespace isc::asiolink;
16using namespace isc::data;
17using namespace isc::util;
18
19namespace isc {
20namespace dhcp {
21
22void
24 if (containsAddress(addr)) {
25 isc_throw (BadValue, "RelayInfo already contains address: "
26 << addr.toText());
27 }
28
29 addresses_.push_back(addr);
30}
31
32bool
34 return (!addresses_.empty());
35}
36
37bool
39 auto const& index = addresses_.get<IOAddressListSetTag>();
40 return (index.find(addr) != index.end());
41}
42
43const IOAddressList&
45 return (addresses_);
46}
47
48void
52
53bool
55 return (relay_.hasAddresses());
56}
57
58bool
60 return (relay_.containsAddress(addr));
61}
62
63const IOAddressList&
65 return (relay_.getAddresses());
66}
67
68bool
70 if (client_classes_.empty()) {
71 // Empty list for this network, so we support everyone.
72 return (true);
73 }
74
75 return (client_classes_.intersects(classes));
76}
77
78void
80 if (!client_classes_.contains(class_name)) {
81 client_classes_.insert(class_name);
82 }
83}
84
85void
87 if (!additional_classes_.contains(class_name)) {
88 additional_classes_.insert(class_name);
89 }
90}
91
94 const int global_index,
95 const int /*min_index*/,
96 const int /*max_index*/) const {
97 if ((global_index >= 0) && fetch_globals_fn_) {
99 if (globals) {
100 ConstElementPtr global_param = globals->get(global_index);
101 if (global_param) {
102 std::string global_str = global_param->stringValue();
103 if (!global_str.empty()) {
104 return (IOAddress(global_str));
105 }
106 }
107 }
108 }
109 return (property);
110}
111
115
116 // Set user-context
117 contextToElement(map);
118
119 // Set interface
120 if (!iface_name_.unspecified()) {
121 map->set("interface", Element::create(iface_name_.get()));
122 }
123
124 ElementPtr relay_map = Element::createMap();
125 ElementPtr address_list = Element::createList();
126 const IOAddressList addresses = getRelayAddresses();
127 for (auto const& address : addresses) {
128 address_list->add(Element::create(address.toText()));
129 }
130
131 relay_map->set("ip-addresses", address_list);
132 map->set("relay", relay_map);
133
134 // Set client-classes
135 if (!client_classes_.empty()) {
136 map->set("client-classes", client_classes_.toElement());
137 }
138
139 // Set evaluate-additional-classes
140 if (!additional_classes_.empty()) {
141 map->set("evaluate-additional-classes",
143 }
144
145 // T1, T2, and Valid are optional for SharedNetworks, and
146 // T1 and T2 are optional for Subnet4 thus we will only
147 // output them if they are marked as specified.
148 if (!t1_.unspecified()) {
149 map->set("renew-timer",
150 Element::create(static_cast<long long>(t1_.get())));
151 }
152
153 // Set rebind-timer
154 if (!t2_.unspecified()) {
155 map->set("rebind-timer",
156 Element::create(static_cast<long long>(t2_.get())));
157 }
158
159 // Set valid-lifetime
160 if (!valid_.unspecified()) {
161 map->set("valid-lifetime",
162 Element::create(static_cast<long long>(valid_.get())));
163 map->set("min-valid-lifetime",
164 Element::create(static_cast<long long>(valid_.getMin())));
165 map->set("max-valid-lifetime",
166 Element::create(static_cast<long long>(valid_.getMax())));
167 }
168
169 // Set reservations-global
171 map->set("reservations-global",
173 }
174
175 // Set reservations-in-subnet
177 map->set("reservations-in-subnet",
179 }
180
181 // Set reservations-out-of-pool
183 map->set("reservations-out-of-pool",
185 }
186
187 // Set options
189 map->set("option-data", opts->toElement());
190
191 // Output calculate-tee-times and percentages if calculation is enabled.
193 map->set("calculate-tee-times", Element::create(calculate_tee_times_));
194 }
195
196 if (!t1_percent_.unspecified()) {
197 map->set("t1-percent", Element::create(t1_percent_));
198 }
199
200 if (!t2_percent_.unspecified()) {
201 map->set("t2-percent", Element::create(t2_percent_));
202 }
203
205 map->set("ddns-send-updates", Element::create(ddns_send_updates_));
206 }
207
209 map->set("ddns-override-no-update", Element::create(ddns_override_no_update_));
210 }
211
213 map->set("ddns-override-client-update", Element::create(ddns_override_client_update_));
214 }
215
217 map->set("ddns-replace-client-name",
219 replaceClientNameModeToString(ddns_replace_client_name_mode_)));
220 }
221
223 map->set("ddns-generated-prefix", Element::create(ddns_generated_prefix_));
224 }
225
227 map->set("ddns-qualifying-suffix", Element::create(ddns_qualifying_suffix_));
228 }
229
231 map->set("ddns-ttl-percent", Element::create(ddns_ttl_percent_));
232 }
233
234 if (!ddns_ttl_.unspecified()) {
235 map->set("ddns-ttl", Element::create(ddns_ttl_));
236 }
237
238 if (!ddns_ttl_min_.unspecified()) {
239 map->set("ddns-ttl-min", Element::create(ddns_ttl_min_));
240 }
241
242 if (!ddns_ttl_max_.unspecified()) {
243 map->set("ddns-ttl-max", Element::create(ddns_ttl_max_));
244 }
245
247 map->set("hostname-char-set", Element::create(hostname_char_set_));
248 }
249
251 map->set("hostname-char-replacement", Element::create(hostname_char_replacement_));
252 }
253
255 map->set("store-extended-info", Element::create(store_extended_info_));
256 }
257
259 map->set("cache-threshold", Element::create(cache_threshold_));
260 }
261
263 map->set("cache-max-age",
264 Element::create(static_cast<long long>(cache_max_age_)));
265 }
266
268 map->set("ddns-update-on-renew", Element::create(ddns_update_on_renew_));
269 }
270
272 map->set("ddns-conflict-resolution-mode", Element::create(ddns_conflict_resolution_mode_));
273 }
274
276 map->set("allocator", Element::create(allocator_type_));
277 }
278
279 return (map);
280}
281
282void
284 if (!siaddr.get().isV4()) {
285 isc_throw(BadValue, "Can't set siaddr to non-IPv4 address "
286 << siaddr);
287 }
288 siaddr_ = siaddr;
289}
290
291void
293 sname_ = sname;
294}
295
296void
298 filename_ = filename;
299}
300
304
305 // Set match-client-id
306 if (!match_client_id_.unspecified()) {
307 map->set("match-client-id", Element::create(match_client_id_.get()));
308 }
309
310 // Set authoritative
311 if (!authoritative_.unspecified()) {
312 map->set("authoritative", Element::create(authoritative_.get()));
313 }
314
315 // Set next-server
316 if (!siaddr_.unspecified()) {
317 map->set("next-server", Element::create(siaddr_.get().toText()));
318 }
319
320 // Set server-hostname
321 if (!sname_.unspecified()) {
322 map->set("server-hostname", Element::create(sname_.get()));
323 }
324
325 // Set boot-file-name
326 if (!filename_.unspecified()) {
327 map->set("boot-file-name",Element::create(filename_.get()));
328 }
329
330 // Set offer-lifetime
331 if (!offer_lft_.unspecified()) {
332 map->set("offer-lifetime",Element::create(offer_lft_.get()));
333 }
334
335 return (map);
336}
337
340 try {
341 OptionCustomPtr opt_server_id = boost::dynamic_pointer_cast<OptionCustom>
343 if (opt_server_id) {
344 return (opt_server_id->readAddress());
345 }
346 } catch (const std::exception&) {
347 // Ignore any exceptions and simply return empty buffer.
348 }
349
351}
352
356
357 // Set preferred-lifetime
358 if (!preferred_.unspecified()) {
359 map->set("preferred-lifetime",
360 Element::create(static_cast<long long>(preferred_.get())));
361 map->set("min-preferred-lifetime",
362 Element::create(static_cast<long long>(preferred_.getMin())));
363 map->set("max-preferred-lifetime",
364 Element::create(static_cast<long long>(preferred_.getMax())));
365 }
366
367 // Set interface-id
368 if (interface_id_) {
369 std::vector<uint8_t> bin = interface_id_->getData();
370 std::string ifid;
371 ifid.resize(bin.size());
372 if (!bin.empty()) {
373 std::memcpy(&ifid[0], &bin[0], bin.size());
374 }
375 map->set("interface-id", Element::create(ifid));
376 }
377
378 // Set rapid-commit
379 if (!rapid_commit_.unspecified()) {
380 map->set("rapid-commit", Element::create(rapid_commit_.get()));
381 }
382
383 // Set pd-allocator
384 if (!pd_allocator_type_.unspecified()) {
385 map->set("pd-allocator", Element::create(pd_allocator_type_));
386 }
387
388 return (map);
389}
390
391} // end of namespace isc::dhcp
392} // end of namespace isc
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition data.cc:249
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:304
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition data.cc:299
Container for storing client class names.
Definition classify.h:109
bool contains(const ClientClass &x) const
returns if class x belongs to the defined classes
Definition classify.cc:55
void insert(const ClientClass &class_name)
Insert an element.
Definition classify.h:159
bool empty() const
Check if classes is empty.
Definition classify.h:169
virtual isc::data::ElementPtr toElement() const
Returns all class names as an ElementPtr of type ListElement.
Definition classify.cc:95
bool intersects(const ClientClasses &cclasses) const
returns whether this container has at least one class in common with a given container.
Definition classify.cc:61
Acts as a storage vault for D2 client configuration.
void setSiaddr(const util::Optional< asiolink::IOAddress > &siaddr)
Sets siaddr for the network.
Definition network.cc:283
void setFilename(const util::Optional< std::string > &filename)
Sets boot file name for the network.
Definition network.cc:297
virtual asiolink::IOAddress getServerId() const
Returns binary representation of the dhcp-server-identifier option (54).
Definition network.cc:339
virtual data::ElementPtr toElement() const
Unparses network object.
Definition network.cc:302
void setSname(const util::Optional< std::string > &sname)
Sets server hostname for the network.
Definition network.cc:292
virtual data::ElementPtr toElement() const
Unparses network object.
Definition network.cc:354
const IOAddressList & getAddresses() const
Returns const reference to the list of addresses.
Definition network.cc:44
void addAddress(const asiolink::IOAddress &addr)
Adds an address to the list of addresses.
Definition network.cc:23
bool containsAddress(const asiolink::IOAddress &addr) const
Checks the address list for the given address.
Definition network.cc:38
bool hasAddresses() const
Indicates whether or not the address list has entries.
Definition network.cc:33
virtual bool clientSupported(const isc::dhcp::ClientClasses &client_classes) const
Checks whether this network supports a client that belongs to the specified classes.
Definition network.cc:69
isc::util::Triplet< uint32_t > t2_
a isc::util::Triplet (min/default/max) holding allowed rebind timer values
Definition network.h:1217
void addRelayAddress(const asiolink::IOAddress &addr)
Adds an address to the list addresses in the network's relay info.
Definition network.cc:49
util::Optional< std::string > hostname_char_replacement_
A string to replace invalid characters when scrubbing hostnames.
Definition network.h:1273
RelayInfo relay_
Relay information.
Definition network.h:1196
util::Optional< bool > reservations_out_of_pool_
Enables out-of-pool reservations optimization.
Definition network.h:1233
util::Optional< bool > ddns_update_on_renew_
Should Kea perform updates when leases are extended.
Definition network.h:1286
util::Optional< std::string > allocator_type_
Allocator used for IP address allocations.
Definition network.h:1304
CfgOptionPtr cfg_option_
Pointer to the option data configuration for this subnet.
Definition network.h:1236
const IOAddressList & getRelayAddresses() const
Returns the list of relay addresses from the network's relay info.
Definition network.cc:64
ClientClasses additional_classes_
Additional classes.
Definition network.h:1211
void allowClientClass(const isc::dhcp::ClientClass &class_name)
Adds class clas_name to the allowed client classes list.
Definition network.cc:79
util::Optional< bool > reservations_global_
Enables global reservations.
Definition network.h:1223
util::Optional< bool > ddns_override_client_update_
Should Kea perform updates, even if client requested delegation.
Definition network.h:1256
util::Optional< bool > reservations_in_subnet_
Enables subnet reservations.
Definition network.h:1226
FetchNetworkGlobalsFn fetch_globals_fn_
Pointer to the optional callback used to fetch globally configured parameters inherited to the Networ...
Definition network.h:1322
util::Optional< double > t2_percent_
Percentage of the lease lifetime to use when calculating T2 timer.
Definition network.h:1245
ClientClasses client_classes_
List of client classes allowed to use this network.
Definition network.h:1204
util::Optional< std::string > ddns_generated_prefix_
Prefix Kea should use when generating domain-names.
Definition network.h:1262
util::Optional< double > t1_percent_
Percentage of the lease lifetime to use when calculating T1 timer.
Definition network.h:1242
bool hasRelays() const
Indicates if network's relay info has relay addresses.
Definition network.cc:54
util::Optional< uint32_t > ddns_ttl_min_
Minimum value to use for DNS TTL.
Definition network.h:1298
virtual data::ElementPtr toElement() const
Unparses network object.
Definition network.cc:113
util::Optional< std::string > hostname_char_set_
Regular expression describing invalid characters for client hostnames.
Definition network.h:1269
bool hasRelayAddress(const asiolink::IOAddress &address) const
Tests if the network's relay info contains the given address.
Definition network.cc:59
void addAdditionalClass(const isc::dhcp::ClientClass &class_name)
Adds class class_name to the additional classes list.
Definition network.cc:86
util::Optional< std::string > iface_name_
Holds interface name for which this network is selected.
Definition network.h:1191
util::Optional< bool > ddns_send_updates_
Should Kea perform DNS updates.
Definition network.h:1249
util::Optional< std::string > ddns_conflict_resolution_mode_
DDNS conflict resolution mode.
Definition network.h:1289
util::Optional< bool > store_extended_info_
Should Kea store additional client query data (e.g.
Definition network.h:1277
isc::util::Triplet< uint32_t > valid_
a isc::util::Triplet (min/default/max) holding allowed valid lifetime values
Definition network.h:1220
util::Optional< bool > calculate_tee_times_
Enables calculation of T1 and T2 timers.
Definition network.h:1239
util::Optional< uint32_t > cache_max_age_
Value in seconds to use as cache maximal age.
Definition network.h:1283
ReturnType getGlobalProperty(ReturnType property, const int global_index, const int min_index=-1, const int max_index=-1) const
Returns a value of global configuration parameter with a given index.
Definition network.h:947
util::Optional< bool > ddns_override_no_update_
Should Kea perform updates, even if client requested no updates.
Definition network.h:1253
util::Optional< std::string > ddns_qualifying_suffix_
Suffix Kea should use when to qualify partial domain-names.
Definition network.h:1265
util::Optional< uint32_t > ddns_ttl_max_
Maximum value to use for DNS TTL.
Definition network.h:1301
util::Optional< double > ddns_ttl_percent_
Percentage of the lease lifetime to use for DNS TTL.
Definition network.h:1292
util::Optional< uint32_t > ddns_ttl_
Explicit value to use for DNS TTL.
Definition network.h:1295
util::Optional< D2ClientConfig::ReplaceClientNameMode > ddns_replace_client_name_mode_
How Kea should handle the domain-name supplied by the client.
Definition network.h:1259
isc::util::Triplet< uint32_t > t1_
a isc::util::Triplet (min/default/max) holding allowed renew timer values
Definition network.h:1214
CfgOptionPtr getCfgOption()
Returns pointer to the option data configuration for this network.
Definition network.h:477
util::Optional< double > cache_threshold_
Percentage of the lease lifetime to use as cache threshold.
Definition network.h:1280
A template representing an optional value.
Definition optional.h:36
T get() const
Retrieves the encapsulated value.
Definition optional.h:114
void unspecified(bool unspecified)
Modifies the flag that indicates whether the value is specified or unspecified.
Definition optional.h:136
T get(T hint) const
Returns value with a hint.
Definition triplet.h:99
T getMax() const
Returns a maximum allowed value.
Definition triplet.h:112
T getMin() const
Returns a minimum allowed value.
Definition triplet.h:85
#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
std::string ClientClass
Defines a single class name.
Definition classify.h:43
boost::shared_ptr< const CfgGlobals > ConstCfgGlobalsPtr
Const shared pointer to a CfgGlobals instance.
@ DHO_DHCP_SERVER_IDENTIFIER
Definition dhcp4.h:123
boost::shared_ptr< OptionCustom > OptionCustomPtr
A pointer to the OptionCustom object.
boost::multi_index_container< asiolink::IOAddress, boost::multi_index::indexed_by< boost::multi_index::sequenced< boost::multi_index::tag< IOAddressListListTag > >, boost::multi_index::hashed_unique< boost::multi_index::tag< IOAddressListSetTag >, boost::multi_index::identity< asiolink::IOAddress > > > > IOAddressList
List of IO addresses.
Definition network.h:67
boost::shared_ptr< const CfgOption > ConstCfgOptionPtr
Const pointer.
Definition cfg_option.h:835
Defines the logger used by the top-level component of kea-lfc.
#define DHCP4_OPTION_SPACE
global std option spaces
void contextToElement(data::ElementPtr map) const
Merge unparse a user_context object.
Tag for the list of IO addresses as a set.
Definition network.h:49