Kea 3.1.1
network.cc
Go to the documentation of this file.
1// Copyright (C) 2017-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#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_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",
142 additional_classes_.toElement());
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
170 if (!reservations_global_.unspecified()) {
171 map->set("reservations-global",
173 }
174
175 // Set reservations-in-subnet
176 if (!reservations_in_subnet_.unspecified()) {
177 map->set("reservations-in-subnet",
179 }
180
181 // Set reservations-out-of-pool
182 if (!reservations_out_of_pool_.unspecified()) {
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.
192 if (!calculate_tee_times_.unspecified()) {
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
204 if (!ddns_send_updates_.unspecified()) {
205 map->set("ddns-send-updates", Element::create(ddns_send_updates_));
206 }
207
208 if (!ddns_override_no_update_.unspecified()) {
209 map->set("ddns-override-no-update", Element::create(ddns_override_no_update_));
210 }
211
212 if (!ddns_override_client_update_.unspecified()) {
213 map->set("ddns-override-client-update", Element::create(ddns_override_client_update_));
214 }
215
216 if (!ddns_replace_client_name_mode_.unspecified()) {
217 map->set("ddns-replace-client-name",
219 replaceClientNameModeToString(ddns_replace_client_name_mode_)));
220 }
221
222 if (!ddns_generated_prefix_.unspecified()) {
223 map->set("ddns-generated-prefix", Element::create(ddns_generated_prefix_));
224 }
225
226 if (!ddns_qualifying_suffix_.unspecified()) {
227 map->set("ddns-qualifying-suffix", Element::create(ddns_qualifying_suffix_));
228 }
229
230 if (!ddns_ttl_percent_.unspecified()) {
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
246 if (!hostname_char_set_.unspecified()) {
247 map->set("hostname-char-set", Element::create(hostname_char_set_));
248 }
249
250 if (!hostname_char_replacement_.unspecified()) {
251 map->set("hostname-char-replacement", Element::create(hostname_char_replacement_));
252 }
253
254 if (!store_extended_info_.unspecified()) {
255 map->set("store-extended-info", Element::create(store_extended_info_));
256 }
257
258 if (!cache_threshold_.unspecified()) {
259 map->set("cache-threshold", Element::create(cache_threshold_));
260 }
261
262 if (!cache_max_age_.unspecified()) {
263 map->set("cache-max-age",
264 Element::create(static_cast<long long>(cache_max_age_)));
265 }
266
267 if (!ddns_update_on_renew_.unspecified()) {
268 map->set("ddns-update-on-renew", Element::create(ddns_update_on_renew_));
269 }
270
271 if (!ddns_conflict_resolution_mode_.unspecified()) {
272 map->set("ddns-conflict-resolution-mode", Element::create(ddns_conflict_resolution_mode_));
273 }
274
275 if (!allocator_type_.unspecified()) {
276 map->set("allocator", Element::create(allocator_type_));
277 }
278
279 if (!adaptive_lease_time_threshold_.unspecified()) {
280 map->set("adaptive-lease-time-threshold",
282 }
283
284 return (map);
285}
286
287void
289 if (!siaddr.get().isV4()) {
290 isc_throw(BadValue, "Can't set siaddr to non-IPv4 address "
291 << siaddr);
292 }
293 siaddr_ = siaddr;
294}
295
296void
298 sname_ = sname;
299}
300
301void
303 filename_ = filename;
304}
305
309
310 // Set match-client-id
311 if (!match_client_id_.unspecified()) {
312 map->set("match-client-id", Element::create(match_client_id_.get()));
313 }
314
315 // Set authoritative
316 if (!authoritative_.unspecified()) {
317 map->set("authoritative", Element::create(authoritative_.get()));
318 }
319
320 // Set next-server
321 if (!siaddr_.unspecified()) {
322 map->set("next-server", Element::create(siaddr_.get().toText()));
323 }
324
325 // Set server-hostname
326 if (!sname_.unspecified()) {
327 map->set("server-hostname", Element::create(sname_.get()));
328 }
329
330 // Set boot-file-name
331 if (!filename_.unspecified()) {
332 map->set("boot-file-name",Element::create(filename_.get()));
333 }
334
335 // Set offer-lifetime
336 if (!offer_lft_.unspecified()) {
337 map->set("offer-lifetime",Element::create(offer_lft_.get()));
338 }
339
340 return (map);
341}
342
345 try {
346 OptionCustomPtr opt_server_id = boost::dynamic_pointer_cast<OptionCustom>
348 if (opt_server_id) {
349 return (opt_server_id->readAddress());
350 }
351 } catch (const std::exception&) {
352 // Ignore any exceptions and simply return empty buffer.
353 }
354
356}
357
361
362 // Set preferred-lifetime
363 if (!preferred_.unspecified()) {
364 map->set("preferred-lifetime",
365 Element::create(static_cast<long long>(preferred_.get())));
366 map->set("min-preferred-lifetime",
367 Element::create(static_cast<long long>(preferred_.getMin())));
368 map->set("max-preferred-lifetime",
369 Element::create(static_cast<long long>(preferred_.getMax())));
370 }
371
372 // Set interface-id
373 if (interface_id_) {
374 std::vector<uint8_t> bin = interface_id_->getData();
375 std::string ifid;
376 ifid.resize(bin.size());
377 if (!bin.empty()) {
378 std::memcpy(&ifid[0], &bin[0], bin.size());
379 }
380 map->set("interface-id", Element::create(ifid));
381 }
382
383 // Set rapid-commit
384 if (!rapid_commit_.unspecified()) {
385 map->set("rapid-commit", Element::create(rapid_commit_.get()));
386 }
387
388 // Set pd-allocator
389 if (!pd_allocator_type_.unspecified()) {
390 map->set("pd-allocator", Element::create(pd_allocator_type_));
391 }
392
393 return (map);
394}
395
396} // end of namespace isc::dhcp
397} // 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:110
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:288
void setFilename(const util::Optional< std::string > &filename)
Sets boot file name for the network.
Definition network.cc:302
virtual asiolink::IOAddress getServerId() const
Returns binary representation of the dhcp-server-identifier option (54).
Definition network.cc:344
virtual data::ElementPtr toElement() const
Unparses network object.
Definition network.cc:307
void setSname(const util::Optional< std::string > &sname)
Sets server hostname for the network.
Definition network.cc:297
virtual data::ElementPtr toElement() const
Unparses network object.
Definition network.cc:359
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:1236
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:1292
RelayInfo relay_
Relay information.
Definition network.h:1215
util::Optional< bool > reservations_out_of_pool_
Enables out-of-pool reservations optimization.
Definition network.h:1252
util::Optional< bool > ddns_update_on_renew_
Should Kea perform updates when leases are extended.
Definition network.h:1305
util::Optional< std::string > allocator_type_
Allocator used for IP address allocations.
Definition network.h:1323
CfgOptionPtr cfg_option_
Pointer to the option data configuration for this subnet.
Definition network.h:1255
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:1230
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:1242
util::Optional< bool > ddns_override_client_update_
Should Kea perform updates, even if client requested delegation.
Definition network.h:1275
util::Optional< bool > reservations_in_subnet_
Enables subnet reservations.
Definition network.h:1245
FetchNetworkGlobalsFn fetch_globals_fn_
Pointer to the optional callback used to fetch globally configured parameters inherited to the Networ...
Definition network.h:1346
util::Optional< double > t2_percent_
Percentage of the lease lifetime to use when calculating T2 timer.
Definition network.h:1264
ClientClasses client_classes_
List of client classes allowed to use this network.
Definition network.h:1223
util::Optional< double > adaptive_lease_time_threshold_
Percentage of the adaptive lease time threshold.
Definition network.h:1334
util::Optional< std::string > ddns_generated_prefix_
Prefix Kea should use when generating domain-names.
Definition network.h:1281
util::Optional< double > t1_percent_
Percentage of the lease lifetime to use when calculating T1 timer.
Definition network.h:1261
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:1317
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:1288
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:1210
util::Optional< bool > ddns_send_updates_
Should Kea perform DNS updates.
Definition network.h:1268
util::Optional< std::string > ddns_conflict_resolution_mode_
DDNS conflict resolution mode.
Definition network.h:1308
util::Optional< bool > store_extended_info_
Should Kea store additional client query data (e.g.
Definition network.h:1296
isc::util::Triplet< uint32_t > valid_
a isc::util::Triplet (min/default/max) holding allowed valid lifetime values
Definition network.h:1239
util::Optional< bool > calculate_tee_times_
Enables calculation of T1 and T2 timers.
Definition network.h:1258
util::Optional< uint32_t > cache_max_age_
Value in seconds to use as cache maximal age.
Definition network.h:1302
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:966
util::Optional< bool > ddns_override_no_update_
Should Kea perform updates, even if client requested no updates.
Definition network.h:1272
util::Optional< std::string > ddns_qualifying_suffix_
Suffix Kea should use when to qualify partial domain-names.
Definition network.h:1284
util::Optional< uint32_t > ddns_ttl_max_
Maximum value to use for DNS TTL.
Definition network.h:1320
util::Optional< double > ddns_ttl_percent_
Percentage of the lease lifetime to use for DNS TTL.
Definition network.h:1311
util::Optional< uint32_t > ddns_ttl_
Explicit value to use for DNS TTL.
Definition network.h:1314
util::Optional< D2ClientConfig::ReplaceClientNameMode > ddns_replace_client_name_mode_
How Kea should handle the domain-name supplied by the client.
Definition network.h:1278
isc::util::Triplet< uint32_t > t1_
a isc::util::Triplet (min/default/max) holding allowed renew timer values
Definition network.h:1233
CfgOptionPtr getCfgOption()
Returns pointer to the option data configuration for this network.
Definition network.h:478
util::Optional< double > cache_threshold_
Percentage of the lease lifetime to use as cache threshold.
Definition network.h:1299
A template representing an optional value.
Definition optional.h:36
T get() const
Retrieves the encapsulated value.
Definition optional.h:114
#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:44
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:976
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