Kea 2.2.0
network.cc
Go to the documentation of this file.
1// Copyright (C) 2017-2022 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 for (auto address = addresses_.begin(); address != addresses_.end();
40 ++address) {
41 if ((*address) == addr) {
42 return (true);
43 }
44 }
45
46 return (false);
47}
48
49const IOAddressList&
51 return (addresses_);
52}
53
54void
56 relay_.addAddress(addr);
57}
58
59bool
61 return (relay_.hasAddresses());
62}
63
64bool
66 return (relay_.containsAddress(addr));
67}
68
69const IOAddressList&
71 return (relay_.getAddresses());
72}
73
74bool
76 if (client_class_.empty()) {
77 // There is no class defined for this network, so we do
78 // support everyone.
79 return (true);
80 }
81
82 return (classes.contains(client_class_));
83}
84
85void
87 client_class_ = class_name;
88}
89
90void
92 if (!required_classes_.contains(class_name)) {
93 required_classes_.insert(class_name);
94 }
95}
96
97const ClientClasses&
99 return (required_classes_);
100}
101
104 const int global_index,
105 const int /*min_index*/,
106 const int /*max_index*/) const {
107 if ((global_index >= 0) && fetch_globals_fn_) {
109 if (globals) {
110 ConstElementPtr global_param = globals->get(global_index);
111 if (global_param) {
112 std::string global_str = global_param->stringValue();
113 if (!global_str.empty()) {
114 return (IOAddress(global_str));
115 }
116 }
117 }
118 }
119 return (property);
120}
121
125
126 // Set user-context
127 contextToElement(map);
128
129 // Set interface
130 if (!iface_name_.unspecified()) {
131 map->set("interface", Element::create(iface_name_.get()));
132 }
133
134 ElementPtr relay_map = Element::createMap();
135 ElementPtr address_list = Element::createList();
136 const IOAddressList addresses = getRelayAddresses();
137 for (auto address = addresses.begin(); address != addresses.end(); ++address) {
138 address_list->add(Element::create((*address).toText()));
139 }
140
141 relay_map->set("ip-addresses", address_list);
142 map->set("relay", relay_map);
143
144 // Set client-class
145 if (!client_class_.unspecified()) {
146 map->set("client-class", Element::create(client_class_.get()));
147 }
148
149 // Set require-client-classes
150 const ClientClasses& classes = getRequiredClasses();
151 if (!classes.empty()) {
152 ElementPtr class_list = Element::createList();
153 for (ClientClasses::const_iterator it = classes.cbegin();
154 it != classes.cend(); ++it) {
155 class_list->add(Element::create(*it));
156 }
157 map->set("require-client-classes", class_list);
158 }
159
160 // T1, T2, and Valid are optional for SharedNetworks, and
161 // T1 and T2 are optional for Subnet4 thus we will only
162 // output them if they are marked as specified.
163 if (!t1_.unspecified()) {
164 map->set("renew-timer",
165 Element::create(static_cast<long long>(t1_.get())));
166 }
167
168 // Set rebind-timer
169 if (!t2_.unspecified()) {
170 map->set("rebind-timer",
171 Element::create(static_cast<long long>(t2_.get())));
172 }
173
174 // Set valid-lifetime
175 if (!valid_.unspecified()) {
176 map->set("valid-lifetime",
177 Element::create(static_cast<long long>(valid_.get())));
178 map->set("min-valid-lifetime",
179 Element::create(static_cast<long long>(valid_.getMin())));
180 map->set("max-valid-lifetime",
181 Element::create(static_cast<long long>(valid_.getMax())));
182 }
183
184 // Set reservations-global
186 map->set("reservations-global",
188 }
189
190 // Set reservations-in-subnet
192 map->set("reservations-in-subnet",
194 }
195
196 // Set reservations-out-of-pool
198 map->set("reservations-out-of-pool",
200 }
201
202 // Set options
204 map->set("option-data", opts->toElement());
205
206 // Output calculate-tee-times and percentages if calculation is enabled.
208 map->set("calculate-tee-times", Element::create(calculate_tee_times_));
209 }
210
211 if (!t1_percent_.unspecified()) {
212 map->set("t1-percent", Element::create(t1_percent_));
213 }
214
215 if (!t2_percent_.unspecified()) {
216 map->set("t2-percent", Element::create(t2_percent_));
217 }
218
220 map->set("ddns-send-updates", Element::create(ddns_send_updates_));
221 }
222
224 map->set("ddns-override-no-update", Element::create(ddns_override_no_update_));
225 }
226
228 map->set("ddns-override-client-update", Element::create(ddns_override_client_update_));
229 }
230
232 map->set("ddns-replace-client-name",
234 replaceClientNameModeToString(ddns_replace_client_name_mode_)));
235 }
236
238 map->set("ddns-generated-prefix", Element::create(ddns_generated_prefix_));
239 }
240
242 map->set("ddns-qualifying-suffix", Element::create(ddns_qualifying_suffix_));
243 }
244
246 map->set("hostname-char-set", Element::create(hostname_char_set_));
247 }
248
250 map->set("hostname-char-replacement", Element::create(hostname_char_replacement_));
251 }
252
254 map->set("store-extended-info", Element::create(store_extended_info_));
255 }
256
258 map->set("cache-threshold", Element::create(cache_threshold_));
259 }
260
262 map->set("cache-max-age",
263 Element::create(static_cast<long long>(cache_max_age_)));
264 }
265
267 map->set("ddns-update-on-renew", Element::create(ddns_update_on_renew_));
268 }
269
271 map->set("ddns-use-conflict-resolution", Element::create(ddns_use_conflict_resolution_));
272 }
273
274 return (map);
275}
276
277void
279 if (!siaddr.get().isV4()) {
280 isc_throw(BadValue, "Can't set siaddr to non-IPv4 address "
281 << siaddr);
282 }
283 siaddr_ = siaddr;
284}
285
286void
288 sname_ = sname;
289}
290
291void
293 filename_ = filename;
294}
295
299
300 // Set match-client-id
301 if (!match_client_id_.unspecified()) {
302 map->set("match-client-id", Element::create(match_client_id_.get()));
303 }
304
305 // Set authoritative
306 if (!authoritative_.unspecified()) {
307 map->set("authoritative", Element::create(authoritative_.get()));
308 }
309
310 // Set next-server
311 if (!siaddr_.unspecified()) {
312 map->set("next-server", Element::create(siaddr_.get().toText()));
313 }
314
315 // Set server-hostname
316 if (!sname_.unspecified()) {
317 map->set("server-hostname", Element::create(sname_.get()));
318 }
319
320 // Set boot-file-name
321 if (!filename_.unspecified()) {
322 map->set("boot-file-name",Element::create(filename_.get()));
323 }
324
325 return (map);
326}
327
330 try {
331 OptionCustomPtr opt_server_id = boost::dynamic_pointer_cast<OptionCustom>
333 if (opt_server_id) {
334 return (opt_server_id->readAddress());
335 }
336 } catch (const std::exception&) {
337 // Ignore any exceptions and simply return empty buffer.
338 }
339
341}
342
346
347 // Set preferred-lifetime
348 if (!preferred_.unspecified()) {
349 map->set("preferred-lifetime",
350 Element::create(static_cast<long long>(preferred_.get())));
351 map->set("min-preferred-lifetime",
352 Element::create(static_cast<long long>(preferred_.getMin())));
353 map->set("max-preferred-lifetime",
354 Element::create(static_cast<long long>(preferred_.getMax())));
355 }
356
357 // Set interface-id
358 if (interface_id_) {
359 std::vector<uint8_t> bin = interface_id_->getData();
360 std::string ifid;
361 ifid.resize(bin.size());
362 if (!bin.empty()) {
363 std::memcpy(&ifid[0], &bin[0], bin.size());
364 }
365 map->set("interface-id", Element::create(ifid));
366 }
367
368 // Set rapid-commit
369 if (!rapid_commit_.unspecified()) {
370 map->set("rapid-commit", Element::create(rapid_commit_.get()));
371 }
372
373 return (map);
374}
375
376} // end of namespace isc::dhcp
377} // 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:241
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition: data.cc:291
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition: data.cc:286
Container for storing client class names.
Definition: classify.h:70
ClientClassContainer::const_iterator const_iterator
Type of iterators.
Definition: classify.h:74
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:90
bool empty() const
Check if classes is empty.
Definition: classify.h:100
const_iterator cbegin() const
Iterators to the first element.
Definition: classify.h:114
const_iterator cend() const
Iterators to the past the end element.
Definition: classify.h:127
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:278
void setFilename(const util::Optional< std::string > &filename)
Sets boot file name for the network.
Definition: network.cc:292
virtual asiolink::IOAddress getServerId() const
Returns binary representation of the dhcp-server-identifier option (54).
Definition: network.cc:329
virtual data::ElementPtr toElement() const
Unparses network object.
Definition: network.cc:297
void setSname(const util::Optional< std::string > &sname)
Sets server hostname for the network.
Definition: network.cc:287
virtual data::ElementPtr toElement() const
Unparses network object.
Definition: network.cc:344
const IOAddressList & getAddresses() const
Returns const reference to the list of addresses.
Definition: network.cc:50
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:75
isc::util::Triplet< uint32_t > t2_
a isc::util::Triplet (min/default/max) holding allowed rebind timer values
Definition: network.h:1063
void addRelayAddress(const asiolink::IOAddress &addr)
Adds an address to the list addresses in the network's relay info.
Definition: network.cc:55
util::Optional< std::string > hostname_char_replacement_
A string to replace invalid characters when scrubbing hostnames.
Definition: network.h:1119
RelayInfo relay_
Relay information.
Definition: network.h:1044
util::Optional< bool > reservations_out_of_pool_
Enables out-of-pool reservations optimization.
Definition: network.h:1079
util::Optional< bool > ddns_update_on_renew_
Should Kea perform updates when leases are extended.
Definition: network.h:1132
CfgOptionPtr cfg_option_
Pointer to the option data configuration for this subnet.
Definition: network.h:1082
void requireClientClass(const isc::dhcp::ClientClass &class_name)
Adds class class_name to classes required to be evaluated.
Definition: network.cc:91
const IOAddressList & getRelayAddresses() const
Returns the list of relay addresses from the network's relay info.
Definition: network.cc:70
void allowClientClass(const isc::dhcp::ClientClass &class_name)
Sets the supported class to class class_name.
Definition: network.cc:86
util::Optional< bool > reservations_global_
Enables global reservations.
Definition: network.h:1069
util::Optional< bool > ddns_override_client_update_
Should Kea perform updates, even if client requested delegation.
Definition: network.h:1102
util::Optional< bool > reservations_in_subnet_
Enables subnet reservations.
Definition: network.h:1072
FetchNetworkGlobalsFn fetch_globals_fn_
Pointer to the optional callback used to fetch globally configured parameters inherited to the Networ...
Definition: network.h:1147
util::Optional< double > t2_percent_
Percentage of the lease lifetime to use when calculating T2 timer.
Definition: network.h:1091
util::Optional< std::string > ddns_generated_prefix_
Prefix Kea should use when generating domain-names.
Definition: network.h:1108
util::Optional< double > t1_percent_
Percentage of the lease lifetime to use when calculating T1 timer.
Definition: network.h:1088
bool hasRelays() const
Indicates if network's relay info has relay addresses.
Definition: network.cc:60
util::Optional< ClientClass > client_class_
Optional definition of a client class.
Definition: network.h:1051
virtual data::ElementPtr toElement() const
Unparses network object.
Definition: network.cc:123
util::Optional< std::string > hostname_char_set_
Regular expression describing invalid characters for client hostnames.
Definition: network.h:1115
bool hasRelayAddress(const asiolink::IOAddress &address) const
Tests if the network's relay info contains the given address.
Definition: network.cc:65
util::Optional< std::string > iface_name_
Holds interface name for which this network is selected.
Definition: network.h:1039
const ClientClasses & getRequiredClasses() const
Returns classes which are required to be evaluated.
Definition: network.cc:98
util::Optional< bool > ddns_send_updates_
Should Kea perform DNS updates.
Definition: network.h:1095
util::Optional< bool > store_extended_info_
Should Kea store additional client query data (e.g.
Definition: network.h:1123
isc::util::Triplet< uint32_t > valid_
a isc::util::Triplet (min/default/max) holding allowed valid lifetime values
Definition: network.h:1066
util::Optional< bool > calculate_tee_times_
Enables calculation of T1 and T2 timers.
Definition: network.h:1085
util::Optional< bool > ddns_use_conflict_resolution_
Used to to tell kea-dhcp-ddns whether or not to use conflict resolution.
Definition: network.h:1135
util::Optional< uint32_t > cache_max_age_
Value in seconds to use as cache maximal age.
Definition: network.h:1129
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:795
util::Optional< bool > ddns_override_no_update_
Should Kea perform updates, even if client requested no updates.
Definition: network.h:1099
util::Optional< std::string > ddns_qualifying_suffix_
Suffix Kea should use when to qualify partial domain-names.
Definition: network.h:1111
ClientClasses required_classes_
Required classes.
Definition: network.h:1057
util::Optional< D2ClientConfig::ReplaceClientNameMode > ddns_replace_client_name_mode_
How Kea should handle the domain-name supplied by the client.
Definition: network.h:1105
isc::util::Triplet< uint32_t > t1_
a isc::util::Triplet (min/default/max) holding allowed renew timer values
Definition: network.h:1060
CfgOptionPtr getCfgOption()
Returns pointer to the option data configuration for this network.
Definition: network.h:445
util::Optional< double > cache_threshold_
Percentage of the lease lifetime to use as cache threshold.
Definition: network.h:1126
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:27
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
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:159
@ DHO_DHCP_SERVER_IDENTIFIER
Definition: dhcp4.h:123
boost::shared_ptr< OptionCustom > OptionCustomPtr
A pointer to the OptionCustom object.
std::vector< isc::asiolink::IOAddress > IOAddressList
List of IOAddresses.
Definition: network.h:39
boost::shared_ptr< const CfgOption > ConstCfgOptionPtr
Const pointer.
Definition: cfg_option.h:709
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