Kea 2.5.5
network.cc
Go to the documentation of this file.
1// Copyright (C) 2017-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
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
50 relay_.addAddress(addr);
51}
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_class_.empty()) {
71 // There is no class defined for this network, so we do
72 // support everyone.
73 return (true);
74 }
75
76 return (classes.contains(client_class_));
77}
78
79void
81 client_class_ = class_name;
82}
83
84void
86 if (!required_classes_.contains(class_name)) {
87 required_classes_.insert(class_name);
88 }
89}
90
91const ClientClasses&
93 return (required_classes_);
94}
95
98 const int global_index,
99 const int /*min_index*/,
100 const int /*max_index*/) const {
101 if ((global_index >= 0) && fetch_globals_fn_) {
103 if (globals) {
104 ConstElementPtr global_param = globals->get(global_index);
105 if (global_param) {
106 std::string global_str = global_param->stringValue();
107 if (!global_str.empty()) {
108 return (IOAddress(global_str));
109 }
110 }
111 }
112 }
113 return (property);
114}
115
119
120 // Set user-context
121 contextToElement(map);
122
123 // Set interface
124 if (!iface_name_.unspecified()) {
125 map->set("interface", Element::create(iface_name_.get()));
126 }
127
128 ElementPtr relay_map = Element::createMap();
129 ElementPtr address_list = Element::createList();
130 const IOAddressList addresses = getRelayAddresses();
131 for (auto address = addresses.begin(); address != addresses.end(); ++address) {
132 address_list->add(Element::create((*address).toText()));
133 }
134
135 relay_map->set("ip-addresses", address_list);
136 map->set("relay", relay_map);
137
138 // Set client-class
139 if (!client_class_.unspecified()) {
140 map->set("client-class", Element::create(client_class_.get()));
141 }
142
143 // Set require-client-classes
144 const ClientClasses& classes = getRequiredClasses();
145 if (!classes.empty()) {
146 ElementPtr class_list = Element::createList();
147 for (ClientClasses::const_iterator it = classes.cbegin();
148 it != classes.cend(); ++it) {
149 class_list->add(Element::create(*it));
150 }
151 map->set("require-client-classes", class_list);
152 }
153
154 // T1, T2, and Valid are optional for SharedNetworks, and
155 // T1 and T2 are optional for Subnet4 thus we will only
156 // output them if they are marked as specified.
157 if (!t1_.unspecified()) {
158 map->set("renew-timer",
159 Element::create(static_cast<long long>(t1_.get())));
160 }
161
162 // Set rebind-timer
163 if (!t2_.unspecified()) {
164 map->set("rebind-timer",
165 Element::create(static_cast<long long>(t2_.get())));
166 }
167
168 // Set valid-lifetime
169 if (!valid_.unspecified()) {
170 map->set("valid-lifetime",
171 Element::create(static_cast<long long>(valid_.get())));
172 map->set("min-valid-lifetime",
173 Element::create(static_cast<long long>(valid_.getMin())));
174 map->set("max-valid-lifetime",
175 Element::create(static_cast<long long>(valid_.getMax())));
176 }
177
178 // Set reservations-global
180 map->set("reservations-global",
182 }
183
184 // Set reservations-in-subnet
186 map->set("reservations-in-subnet",
188 }
189
190 // Set reservations-out-of-pool
192 map->set("reservations-out-of-pool",
194 }
195
196 // Set options
198 map->set("option-data", opts->toElement());
199
200 // Output calculate-tee-times and percentages if calculation is enabled.
202 map->set("calculate-tee-times", Element::create(calculate_tee_times_));
203 }
204
205 if (!t1_percent_.unspecified()) {
206 map->set("t1-percent", Element::create(t1_percent_));
207 }
208
209 if (!t2_percent_.unspecified()) {
210 map->set("t2-percent", Element::create(t2_percent_));
211 }
212
214 map->set("ddns-send-updates", Element::create(ddns_send_updates_));
215 }
216
218 map->set("ddns-override-no-update", Element::create(ddns_override_no_update_));
219 }
220
222 map->set("ddns-override-client-update", Element::create(ddns_override_client_update_));
223 }
224
226 map->set("ddns-replace-client-name",
228 replaceClientNameModeToString(ddns_replace_client_name_mode_)));
229 }
230
232 map->set("ddns-generated-prefix", Element::create(ddns_generated_prefix_));
233 }
234
236 map->set("ddns-qualifying-suffix", Element::create(ddns_qualifying_suffix_));
237 }
238
240 map->set("ddns-ttl-percent", Element::create(ddns_ttl_percent_));
241 }
242
244 map->set("hostname-char-set", Element::create(hostname_char_set_));
245 }
246
248 map->set("hostname-char-replacement", Element::create(hostname_char_replacement_));
249 }
250
252 map->set("store-extended-info", Element::create(store_extended_info_));
253 }
254
256 map->set("cache-threshold", Element::create(cache_threshold_));
257 }
258
260 map->set("cache-max-age",
261 Element::create(static_cast<long long>(cache_max_age_)));
262 }
263
265 map->set("ddns-update-on-renew", Element::create(ddns_update_on_renew_));
266 }
267
269 map->set("ddns-conflict-resolution-mode", Element::create(ddns_conflict_resolution_mode_));
270 }
271
273 map->set("allocator", Element::create(allocator_type_));
274 }
275
276 return (map);
277}
278
279void
281 if (!siaddr.get().isV4()) {
282 isc_throw(BadValue, "Can't set siaddr to non-IPv4 address "
283 << siaddr);
284 }
285 siaddr_ = siaddr;
286}
287
288void
290 sname_ = sname;
291}
292
293void
295 filename_ = filename;
296}
297
301
302 // Set match-client-id
303 if (!match_client_id_.unspecified()) {
304 map->set("match-client-id", Element::create(match_client_id_.get()));
305 }
306
307 // Set authoritative
308 if (!authoritative_.unspecified()) {
309 map->set("authoritative", Element::create(authoritative_.get()));
310 }
311
312 // Set next-server
313 if (!siaddr_.unspecified()) {
314 map->set("next-server", Element::create(siaddr_.get().toText()));
315 }
316
317 // Set server-hostname
318 if (!sname_.unspecified()) {
319 map->set("server-hostname", Element::create(sname_.get()));
320 }
321
322 // Set boot-file-name
323 if (!filename_.unspecified()) {
324 map->set("boot-file-name",Element::create(filename_.get()));
325 }
326
327 // Set offer-lifetime
328 if (!offer_lft_.unspecified()) {
329 map->set("offer-lifetime",Element::create(offer_lft_.get()));
330 }
331
332 return (map);
333}
334
337 try {
338 OptionCustomPtr opt_server_id = boost::dynamic_pointer_cast<OptionCustom>
340 if (opt_server_id) {
341 return (opt_server_id->readAddress());
342 }
343 } catch (const std::exception&) {
344 // Ignore any exceptions and simply return empty buffer.
345 }
346
348}
349
353
354 // Set preferred-lifetime
355 if (!preferred_.unspecified()) {
356 map->set("preferred-lifetime",
357 Element::create(static_cast<long long>(preferred_.get())));
358 map->set("min-preferred-lifetime",
359 Element::create(static_cast<long long>(preferred_.getMin())));
360 map->set("max-preferred-lifetime",
361 Element::create(static_cast<long long>(preferred_.getMax())));
362 }
363
364 // Set interface-id
365 if (interface_id_) {
366 std::vector<uint8_t> bin = interface_id_->getData();
367 std::string ifid;
368 ifid.resize(bin.size());
369 if (!bin.empty()) {
370 std::memcpy(&ifid[0], &bin[0], bin.size());
371 }
372 map->set("interface-id", Element::create(ifid));
373 }
374
375 // Set rapid-commit
376 if (!rapid_commit_.unspecified()) {
377 map->set("rapid-commit", Element::create(rapid_commit_.get()));
378 }
379
380 // Set pd-allocator
381 if (!pd_allocator_type_.unspecified()) {
382 map->set("pd-allocator", Element::create(pd_allocator_type_));
383 }
384
385 return (map);
386}
387
388} // end of namespace isc::dhcp
389} // 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:108
ClientClassContainer::const_iterator const_iterator
Type of iterators.
Definition: classify.h:112
bool contains(const ClientClass &x) const
returns if class x belongs to the defined classes
Definition: classify.cc:49
void insert(const ClientClass &class_name)
Insert an element.
Definition: classify.h:128
bool empty() const
Check if classes is empty.
Definition: classify.h:138
const_iterator cbegin() const
Iterators to the first element.
Definition: classify.h:152
const_iterator cend() const
Iterators to the past the end element.
Definition: classify.h:165
Acts as a storage vault for D2 client configuration.
Definition: d2_client_cfg.h:57
void setSiaddr(const util::Optional< asiolink::IOAddress > &siaddr)
Sets siaddr for the network.
Definition: network.cc:280
void setFilename(const util::Optional< std::string > &filename)
Sets boot file name for the network.
Definition: network.cc:294
virtual asiolink::IOAddress getServerId() const
Returns binary representation of the dhcp-server-identifier option (54).
Definition: network.cc:336
virtual data::ElementPtr toElement() const
Unparses network object.
Definition: network.cc:299
void setSname(const util::Optional< std::string > &sname)
Sets server hostname for the network.
Definition: network.cc:289
virtual data::ElementPtr toElement() const
Unparses network object.
Definition: network.cc:351
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 client that belongs to 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:1160
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:1216
RelayInfo relay_
Relay information.
Definition: network.h:1141
util::Optional< bool > reservations_out_of_pool_
Enables out-of-pool reservations optimization.
Definition: network.h:1176
util::Optional< bool > ddns_update_on_renew_
Should Kea perform updates when leases are extended.
Definition: network.h:1229
util::Optional< std::string > allocator_type_
Allocator used for IP address allocations.
Definition: network.h:1238
CfgOptionPtr cfg_option_
Pointer to the option data configuration for this subnet.
Definition: network.h:1179
void requireClientClass(const isc::dhcp::ClientClass &class_name)
Adds class class_name to classes required to be evaluated.
Definition: network.cc:85
const IOAddressList & getRelayAddresses() const
Returns the list of relay addresses from the network's relay info.
Definition: network.cc:64
void allowClientClass(const isc::dhcp::ClientClass &class_name)
Sets the supported class to class class_name.
Definition: network.cc:80
util::Optional< bool > reservations_global_
Enables global reservations.
Definition: network.h:1166
util::Optional< bool > ddns_override_client_update_
Should Kea perform updates, even if client requested delegation.
Definition: network.h:1199
util::Optional< bool > reservations_in_subnet_
Enables subnet reservations.
Definition: network.h:1169
FetchNetworkGlobalsFn fetch_globals_fn_
Pointer to the optional callback used to fetch globally configured parameters inherited to the Networ...
Definition: network.h:1256
util::Optional< double > t2_percent_
Percentage of the lease lifetime to use when calculating T2 timer.
Definition: network.h:1188
util::Optional< std::string > ddns_generated_prefix_
Prefix Kea should use when generating domain-names.
Definition: network.h:1205
util::Optional< double > t1_percent_
Percentage of the lease lifetime to use when calculating T1 timer.
Definition: network.h:1185
bool hasRelays() const
Indicates if network's relay info has relay addresses.
Definition: network.cc:54
util::Optional< ClientClass > client_class_
Optional definition of a client class.
Definition: network.h:1148
virtual data::ElementPtr toElement() const
Unparses network object.
Definition: network.cc:117
util::Optional< std::string > hostname_char_set_
Regular expression describing invalid characters for client hostnames.
Definition: network.h:1212
bool hasRelayAddress(const asiolink::IOAddress &address) const
Tests if the network's relay info contains the given address.
Definition: network.cc:59
util::Optional< std::string > iface_name_
Holds interface name for which this network is selected.
Definition: network.h:1136
const ClientClasses & getRequiredClasses() const
Returns classes which are required to be evaluated.
Definition: network.cc:92
util::Optional< bool > ddns_send_updates_
Should Kea perform DNS updates.
Definition: network.h:1192
util::Optional< std::string > ddns_conflict_resolution_mode_
DDNS conflict resolution mode.
Definition: network.h:1232
util::Optional< bool > store_extended_info_
Should Kea store additional client query data (e.g.
Definition: network.h:1220
isc::util::Triplet< uint32_t > valid_
a isc::util::Triplet (min/default/max) holding allowed valid lifetime values
Definition: network.h:1163
util::Optional< bool > calculate_tee_times_
Enables calculation of T1 and T2 timers.
Definition: network.h:1182
util::Optional< uint32_t > cache_max_age_
Value in seconds to use as cache maximal age.
Definition: network.h:1226
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:892
util::Optional< bool > ddns_override_no_update_
Should Kea perform updates, even if client requested no updates.
Definition: network.h:1196
util::Optional< std::string > ddns_qualifying_suffix_
Suffix Kea should use when to qualify partial domain-names.
Definition: network.h:1208
ClientClasses required_classes_
Required classes.
Definition: network.h:1154
util::Optional< double > ddns_ttl_percent_
Percentage of the lease lifetime to use for DNS TTL.
Definition: network.h:1235
util::Optional< D2ClientConfig::ReplaceClientNameMode > ddns_replace_client_name_mode_
How Kea should handle the domain-name supplied by the client.
Definition: network.h:1202
isc::util::Triplet< uint32_t > t1_
a isc::util::Triplet (min/default/max) holding allowed renew timer values
Definition: network.h:1157
CfgOptionPtr getCfgOption()
Returns pointer to the option data configuration for this network.
Definition: network.h:475
util::Optional< double > cache_threshold_
Percentage of the lease lifetime to use as cache threshold.
Definition: network.h:1223
A template representing an optional value.
Definition: optional.h:36
T get() const
Retrieves the encapsulated value.
Definition: optional.h:114
bool empty() const
Checks if the encapsulated value is empty.
Definition: optional.h:153
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:42
boost::shared_ptr< const CfgGlobals > ConstCfgGlobalsPtr
Const shared pointer to a CfgGlobals instance.
Definition: cfg_globals.h:163
@ 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:806
Definition: edns.h:19
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.
Definition: user_context.cc:15
Tag for the list of IO addresses as a set.
Definition: network.h:49