Kea 2.7.5
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_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 (!additional_classes_.contains(class_name)) {
87 additional_classes_.insert(class_name);
88 }
89}
90
91const ClientClasses&
95
96Optional<IOAddress>
97Network::getGlobalProperty(Optional<IOAddress> property,
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 const& address : addresses) {
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 evaluate-additional-classes
144 const ClientClasses& classes = getAdditionalClasses();
145 if (!classes.empty()) {
146 ElementPtr class_list = Element::createList();
147 for (auto const& it : classes) {
148 class_list->add(Element::create(it));
149 }
150 map->set("evaluate-additional-classes", class_list);
151 }
152
153 // T1, T2, and Valid are optional for SharedNetworks, and
154 // T1 and T2 are optional for Subnet4 thus we will only
155 // output them if they are marked as specified.
156 if (!t1_.unspecified()) {
157 map->set("renew-timer",
158 Element::create(static_cast<long long>(t1_.get())));
159 }
160
161 // Set rebind-timer
162 if (!t2_.unspecified()) {
163 map->set("rebind-timer",
164 Element::create(static_cast<long long>(t2_.get())));
165 }
166
167 // Set valid-lifetime
168 if (!valid_.unspecified()) {
169 map->set("valid-lifetime",
170 Element::create(static_cast<long long>(valid_.get())));
171 map->set("min-valid-lifetime",
172 Element::create(static_cast<long long>(valid_.getMin())));
173 map->set("max-valid-lifetime",
174 Element::create(static_cast<long long>(valid_.getMax())));
175 }
176
177 // Set reservations-global
179 map->set("reservations-global",
181 }
182
183 // Set reservations-in-subnet
185 map->set("reservations-in-subnet",
187 }
188
189 // Set reservations-out-of-pool
191 map->set("reservations-out-of-pool",
193 }
194
195 // Set options
197 map->set("option-data", opts->toElement());
198
199 // Output calculate-tee-times and percentages if calculation is enabled.
201 map->set("calculate-tee-times", Element::create(calculate_tee_times_));
202 }
203
204 if (!t1_percent_.unspecified()) {
205 map->set("t1-percent", Element::create(t1_percent_));
206 }
207
208 if (!t2_percent_.unspecified()) {
209 map->set("t2-percent", Element::create(t2_percent_));
210 }
211
213 map->set("ddns-send-updates", Element::create(ddns_send_updates_));
214 }
215
217 map->set("ddns-override-no-update", Element::create(ddns_override_no_update_));
218 }
219
221 map->set("ddns-override-client-update", Element::create(ddns_override_client_update_));
222 }
223
225 map->set("ddns-replace-client-name",
227 replaceClientNameModeToString(ddns_replace_client_name_mode_)));
228 }
229
231 map->set("ddns-generated-prefix", Element::create(ddns_generated_prefix_));
232 }
233
235 map->set("ddns-qualifying-suffix", Element::create(ddns_qualifying_suffix_));
236 }
237
239 map->set("ddns-ttl-percent", Element::create(ddns_ttl_percent_));
240 }
241
243 map->set("hostname-char-set", Element::create(hostname_char_set_));
244 }
245
247 map->set("hostname-char-replacement", Element::create(hostname_char_replacement_));
248 }
249
251 map->set("store-extended-info", Element::create(store_extended_info_));
252 }
253
255 map->set("cache-threshold", Element::create(cache_threshold_));
256 }
257
259 map->set("cache-max-age",
260 Element::create(static_cast<long long>(cache_max_age_)));
261 }
262
264 map->set("ddns-update-on-renew", Element::create(ddns_update_on_renew_));
265 }
266
268 map->set("ddns-conflict-resolution-mode", Element::create(ddns_conflict_resolution_mode_));
269 }
270
272 map->set("allocator", Element::create(allocator_type_));
273 }
274
275 return (map);
276}
277
278void
279Network4::setSiaddr(const Optional<IOAddress>& siaddr) {
280 if (!siaddr.get().isV4()) {
281 isc_throw(BadValue, "Can't set siaddr to non-IPv4 address "
282 << siaddr);
283 }
284 siaddr_ = siaddr;
285}
286
287void
288Network4::setSname(const Optional<std::string>& sname) {
289 sname_ = sname;
290}
291
292void
293Network4::setFilename(const Optional<std::string>& filename) {
294 filename_ = filename;
295}
296
300
301 // Set match-client-id
302 if (!match_client_id_.unspecified()) {
303 map->set("match-client-id", Element::create(match_client_id_.get()));
304 }
305
306 // Set authoritative
307 if (!authoritative_.unspecified()) {
308 map->set("authoritative", Element::create(authoritative_.get()));
309 }
310
311 // Set next-server
312 if (!siaddr_.unspecified()) {
313 map->set("next-server", Element::create(siaddr_.get().toText()));
314 }
315
316 // Set server-hostname
317 if (!sname_.unspecified()) {
318 map->set("server-hostname", Element::create(sname_.get()));
319 }
320
321 // Set boot-file-name
322 if (!filename_.unspecified()) {
323 map->set("boot-file-name",Element::create(filename_.get()));
324 }
325
326 // Set offer-lifetime
327 if (!offer_lft_.unspecified()) {
328 map->set("offer-lifetime",Element::create(offer_lft_.get()));
329 }
330
331 return (map);
332}
333
336 try {
337 OptionCustomPtr opt_server_id = boost::dynamic_pointer_cast<OptionCustom>
339 if (opt_server_id) {
340 return (opt_server_id->readAddress());
341 }
342 } catch (const std::exception&) {
343 // Ignore any exceptions and simply return empty buffer.
344 }
345
347}
348
352
353 // Set preferred-lifetime
354 if (!preferred_.unspecified()) {
355 map->set("preferred-lifetime",
356 Element::create(static_cast<long long>(preferred_.get())));
357 map->set("min-preferred-lifetime",
358 Element::create(static_cast<long long>(preferred_.getMin())));
359 map->set("max-preferred-lifetime",
360 Element::create(static_cast<long long>(preferred_.getMax())));
361 }
362
363 // Set interface-id
364 if (interface_id_) {
365 std::vector<uint8_t> bin = interface_id_->getData();
366 std::string ifid;
367 ifid.resize(bin.size());
368 if (!bin.empty()) {
369 std::memcpy(&ifid[0], &bin[0], bin.size());
370 }
371 map->set("interface-id", Element::create(ifid));
372 }
373
374 // Set rapid-commit
375 if (!rapid_commit_.unspecified()) {
376 map->set("rapid-commit", Element::create(rapid_commit_.get()));
377 }
378
379 // Set pd-allocator
380 if (!pd_allocator_type_.unspecified()) {
381 map->set("pd-allocator", Element::create(pd_allocator_type_));
382 }
383
384 return (map);
385}
386
387} // end of namespace isc::dhcp
388} // 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
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:155
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:279
void setFilename(const util::Optional< std::string > &filename)
Sets boot file name for the network.
Definition network.cc:293
virtual asiolink::IOAddress getServerId() const
Returns binary representation of the dhcp-server-identifier option (54).
Definition network.cc:335
virtual data::ElementPtr toElement() const
Unparses network object.
Definition network.cc:298
void setSname(const util::Optional< std::string > &sname)
Sets server hostname for the network.
Definition network.cc:288
virtual data::ElementPtr toElement() const
Unparses network object.
Definition network.cc:350
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:1166
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:1222
RelayInfo relay_
Relay information.
Definition network.h:1146
util::Optional< bool > reservations_out_of_pool_
Enables out-of-pool reservations optimization.
Definition network.h:1182
util::Optional< bool > ddns_update_on_renew_
Should Kea perform updates when leases are extended.
Definition network.h:1235
util::Optional< std::string > allocator_type_
Allocator used for IP address allocations.
Definition network.h:1244
CfgOptionPtr cfg_option_
Pointer to the option data configuration for this subnet.
Definition network.h:1185
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:1160
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:1172
util::Optional< bool > ddns_override_client_update_
Should Kea perform updates, even if client requested delegation.
Definition network.h:1205
util::Optional< bool > reservations_in_subnet_
Enables subnet reservations.
Definition network.h:1175
FetchNetworkGlobalsFn fetch_globals_fn_
Pointer to the optional callback used to fetch globally configured parameters inherited to the Networ...
Definition network.h:1262
util::Optional< double > t2_percent_
Percentage of the lease lifetime to use when calculating T2 timer.
Definition network.h:1194
util::Optional< std::string > ddns_generated_prefix_
Prefix Kea should use when generating domain-names.
Definition network.h:1211
util::Optional< double > t1_percent_
Percentage of the lease lifetime to use when calculating T1 timer.
Definition network.h:1191
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:1153
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:1218
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:85
util::Optional< std::string > iface_name_
Holds interface name for which this network is selected.
Definition network.h:1141
util::Optional< bool > ddns_send_updates_
Should Kea perform DNS updates.
Definition network.h:1198
util::Optional< std::string > ddns_conflict_resolution_mode_
DDNS conflict resolution mode.
Definition network.h:1238
util::Optional< bool > store_extended_info_
Should Kea store additional client query data (e.g.
Definition network.h:1226
isc::util::Triplet< uint32_t > valid_
a isc::util::Triplet (min/default/max) holding allowed valid lifetime values
Definition network.h:1169
const ClientClasses & getAdditionalClasses() const
Returns the additional classes list.
Definition network.cc:92
util::Optional< bool > calculate_tee_times_
Enables calculation of T1 and T2 timers.
Definition network.h:1188
util::Optional< uint32_t > cache_max_age_
Value in seconds to use as cache maximal age.
Definition network.h:1232
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:897
util::Optional< bool > ddns_override_no_update_
Should Kea perform updates, even if client requested no updates.
Definition network.h:1202
util::Optional< std::string > ddns_qualifying_suffix_
Suffix Kea should use when to qualify partial domain-names.
Definition network.h:1214
util::Optional< double > ddns_ttl_percent_
Percentage of the lease lifetime to use for DNS TTL.
Definition network.h:1241
util::Optional< D2ClientConfig::ReplaceClientNameMode > ddns_replace_client_name_mode_
How Kea should handle the domain-name supplied by the client.
Definition network.h:1208
isc::util::Triplet< uint32_t > t1_
a isc::util::Triplet (min/default/max) holding allowed renew timer values
Definition network.h:1163
CfgOptionPtr getCfgOption()
Returns pointer to the option data configuration for this network.
Definition network.h:480
util::Optional< double > cache_threshold_
Percentage of the lease lifetime to use as cache threshold.
Definition network.h:1229
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.
@ 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