Kea  2.1.7-git
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 
15 using namespace isc::asiolink;
16 using namespace isc::data;
17 using namespace isc::util;
18 
19 namespace isc {
20 namespace dhcp {
21 
22 void
23 Network::RelayInfo::addAddress(const asiolink::IOAddress& addr) {
24  if (containsAddress(addr)) {
25  isc_throw (BadValue, "RelayInfo already contains address: "
26  << addr.toText());
27  }
28 
29  addresses_.push_back(addr);
30 }
31 
32 bool
33 Network::RelayInfo::hasAddresses() const {
34  return (!addresses_.empty());
35 }
36 
37 bool
38 Network::RelayInfo::containsAddress(const asiolink::IOAddress& addr) const {
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 
49 const IOAddressList&
50 Network::RelayInfo::getAddresses() const {
51  return (addresses_);
52 }
53 
54 void
55 Network::addRelayAddress(const asiolink::IOAddress& addr) {
56  relay_.addAddress(addr);
57 }
58 
59 bool
60 Network::hasRelays() const {
61  return (relay_.hasAddresses());
62 }
63 
64 bool
65 Network::hasRelayAddress(const asiolink::IOAddress& addr) const {
66  return (relay_.containsAddress(addr));
67 }
68 
69 const IOAddressList&
70 Network::getRelayAddresses() const {
71  return (relay_.getAddresses());
72 }
73 
74 bool
75 Network::clientSupported(const isc::dhcp::ClientClasses& classes) const {
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 
85 void
86 Network::allowClientClass(const isc::dhcp::ClientClass& class_name) {
87  client_class_ = class_name;
88 }
89 
90 void
91 Network::requireClientClass(const isc::dhcp::ClientClass& class_name) {
92  if (!required_classes_.contains(class_name)) {
93  required_classes_.insert(class_name);
94  }
95 }
96 
97 const ClientClasses&
98 Network::getRequiredClasses() const {
99  return (required_classes_);
100 }
101 
103 Network::getGlobalProperty(Optional<IOAddress> property,
104  const int global_index,
105  const int /*min_index*/,
106  const int /*max_index*/) const {
107  if ((global_index >= 0) && fetch_globals_fn_) {
108  ConstCfgGlobalsPtr globals = 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 
123 Network::toElement() const {
124  ElementPtr map = Element::createMap();
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
185  if (!reservations_global_.unspecified()) {
186  map->set("reservations-global",
187  Element::create(reservations_global_.get()));
188  }
189 
190  // Set reservations-in-subnet
191  if (!reservations_in_subnet_.unspecified()) {
192  map->set("reservations-in-subnet",
193  Element::create(reservations_in_subnet_.get()));
194  }
195 
196  // Set reservations-out-of-pool
197  if (!reservations_out_of_pool_.unspecified()) {
198  map->set("reservations-out-of-pool",
199  Element::create(reservations_out_of_pool_.get()));
200  }
201 
202  // Set options
203  ConstCfgOptionPtr opts = getCfgOption();
204  map->set("option-data", opts->toElement());
205 
206  // Output calculate-tee-times and percentages if calculation is enabled.
207  if (!calculate_tee_times_.unspecified()) {
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 
219  if (!ddns_send_updates_.unspecified()) {
220  map->set("ddns-send-updates", Element::create(ddns_send_updates_));
221  }
222 
223  if (!ddns_override_no_update_.unspecified()) {
224  map->set("ddns-override-no-update", Element::create(ddns_override_no_update_));
225  }
226 
227  if (!ddns_override_client_update_.unspecified()) {
228  map->set("ddns-override-client-update", Element::create(ddns_override_client_update_));
229  }
230 
231  if (!ddns_replace_client_name_mode_.unspecified()) {
232  map->set("ddns-replace-client-name",
233  Element::create(D2ClientConfig::
234  replaceClientNameModeToString(ddns_replace_client_name_mode_)));
235  }
236 
237  if (!ddns_generated_prefix_.unspecified()) {
238  map->set("ddns-generated-prefix", Element::create(ddns_generated_prefix_));
239  }
240 
241  if (!ddns_qualifying_suffix_.unspecified()) {
242  map->set("ddns-qualifying-suffix", Element::create(ddns_qualifying_suffix_));
243  }
244 
245  if (!hostname_char_set_.unspecified()) {
246  map->set("hostname-char-set", Element::create(hostname_char_set_));
247  }
248 
249  if (!hostname_char_replacement_.unspecified()) {
250  map->set("hostname-char-replacement", Element::create(hostname_char_replacement_));
251  }
252 
253  if (!store_extended_info_.unspecified()) {
254  map->set("store-extended-info", Element::create(store_extended_info_));
255  }
256 
257  if (!cache_threshold_.unspecified()) {
258  map->set("cache-threshold", Element::create(cache_threshold_));
259  }
260 
261  if (!cache_max_age_.unspecified()) {
262  map->set("cache-max-age",
263  Element::create(static_cast<long long>(cache_max_age_)));
264  }
265 
266  if (!ddns_update_on_renew_.unspecified()) {
267  map->set("ddns-update-on-renew", Element::create(ddns_update_on_renew_));
268  }
269 
270  if (!ddns_use_conflict_resolution_.unspecified()) {
271  map->set("ddns-use-conflict-resolution", Element::create(ddns_use_conflict_resolution_));
272  }
273 
274  return (map);
275 }
276 
277 void
278 Network4::setSiaddr(const Optional<IOAddress>& siaddr) {
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 
286 void
287 Network4::setSname(const Optional<std::string>& sname) {
288  sname_ = sname;
289 }
290 
291 void
292 Network4::setFilename(const Optional<std::string>& filename) {
293  filename_ = filename;
294 }
295 
297 Network4::toElement() const {
298  ElementPtr map = Network::toElement();
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 
328 IOAddress
329 Network4::getServerId() const {
330  try {
331  OptionCustomPtr opt_server_id = boost::dynamic_pointer_cast<OptionCustom>
332  (cfg_option_->get(DHCP4_OPTION_SPACE, DHO_DHCP_SERVER_IDENTIFIER).option_);
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 
340  return (IOAddress::IPV4_ZERO_ADDRESS());
341 }
342 
344 Network6::toElement() const {
345  ElementPtr map = Network::toElement();
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
std::vector< isc::asiolink::IOAddress > IOAddressList
List of IOAddresses.
Definition: network.h:39
boost::shared_ptr< OptionCustom > OptionCustomPtr
A pointer to the OptionCustom object.
bool contains(const ClientClass &x) const
returns if class x belongs to the defined classes
Definition: classify.cc:43
boost::shared_ptr< const CfgOption > ConstCfgOptionPtr
Const pointer.
Definition: cfg_option.h:709
boost::shared_ptr< const CfgGlobals > ConstCfgGlobalsPtr
Const shared pointer to a CfgGlobals instance.
Definition: cfg_globals.h:159
T get() const
Retrieves the encapsulated value.
Definition: optional.h:114
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
const_iterator cbegin() const
Iterators to the first element.
Definition: classify.h:112
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
ClientClassContainer::const_iterator const_iterator
Type of iterators.
Definition: classify.h:72
Definition: edns.h:19
Acts as a storage vault for D2 client configuration.
Definition: d2_client_cfg.h:57
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
bool empty() const
Check if classes is empty.
Definition: classify.h:98
Defines the logger used by the top-level component of kea-lfc.
#define DHCP4_OPTION_SPACE
global std option spaces
const_iterator cend() const
Iterators to the past the end element.
Definition: classify.h:125
std::string ClientClass
Defines a single class name.
Definition: classify.h:40
Option with defined data fields represented as buffers that can be accessed using data field index...
Definition: option_custom.h:32
A template representing an optional value.
Definition: optional.h:36
Container for storing client class names.
Definition: classify.h:68