Kea  1.5.0
network.cc
Go to the documentation of this file.
1 // Copyright (C) 2017-2018 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 
18 namespace isc {
19 namespace dhcp {
20 
21 void
22 Network::RelayInfo::addAddress(const asiolink::IOAddress& addr) {
23  if (containsAddress(addr)) {
24  isc_throw (BadValue, "RelayInfo already contains address: "
25  << addr.toText());
26  }
27 
28  addresses_.push_back(addr);
29 }
30 
31 bool
32 Network::RelayInfo::hasAddresses() const {
33  return (!addresses_.empty());
34 }
35 
36 bool
37 Network::RelayInfo::containsAddress(const asiolink::IOAddress& addr) const {
38  for (auto address = addresses_.begin(); address != addresses_.end();
39  ++address) {
40  if ((*address) == addr) {
41  return (true);
42  }
43  }
44 
45  return (false);
46 }
47 
48 const IOAddressList&
49 Network::RelayInfo::getAddresses() const {
50  return (addresses_);
51 }
52 
53 void
54 Network::addRelayAddress(const asiolink::IOAddress& addr) {
55  relay_.addAddress(addr);
56 }
57 
58 bool
59 Network::hasRelays() const {
60  return (relay_.hasAddresses());
61 }
62 
63 bool
64 Network::hasRelayAddress(const asiolink::IOAddress& addr) const {
65  return (relay_.containsAddress(addr));
66 }
67 
68 const IOAddressList&
69 Network::getRelayAddresses() const {
70  return (relay_.getAddresses());
71 }
72 
73 bool
74 Network::clientSupported(const isc::dhcp::ClientClasses& classes) const {
75  if (client_class_.empty()) {
76  // There is no class defined for this network, so we do
77  // support everyone.
78  return (true);
79  }
80 
81  return (classes.contains(client_class_));
82 }
83 
84 void
85 Network::allowClientClass(const isc::dhcp::ClientClass& class_name) {
86  client_class_ = class_name;
87 }
88 
89 void
90 Network::requireClientClass(const isc::dhcp::ClientClass& class_name) {
91  if (!required_classes_.contains(class_name)) {
92  required_classes_.insert(class_name);
93  }
94 }
95 
96 const ClientClasses&
97 Network::getRequiredClasses() const {
98  return (required_classes_);
99 }
100 
102 Network::toElement() const {
103  ElementPtr map = Element::createMap();
104 
105  // Set user-context
106  contextToElement(map);
107 
108  // Set interface
109  const std::string& iface = getIface();
110  if (!iface.empty()) {
111  map->set("interface", Element::create(iface));
112  }
113 
114  ElementPtr relay_map = Element::createMap();
115  ElementPtr address_list = Element::createList();
116  const IOAddressList addresses = getRelayAddresses();
117  for (auto address = addresses.begin(); address != addresses.end(); ++address) {
118  address_list->add(Element::create((*address).toText()));
119  }
120 
121  relay_map->set("ip-addresses", address_list);
122  map->set("relay", relay_map);
123 
124  // Set client-class
125  const ClientClass& cclass = getClientClass();
126  if (!cclass.empty()) {
127  map->set("client-class", Element::create(cclass));
128  }
129 
130  // Set require-client-classes
131  const ClientClasses& classes = getRequiredClasses();
132  if (!classes.empty()) {
133  ElementPtr class_list = Element::createList();
134  for (ClientClasses::const_iterator it = classes.cbegin();
135  it != classes.cend(); ++it) {
136  class_list->add(Element::create(*it));
137  }
138  map->set("require-client-classes", class_list);
139  }
140 
141  // T1, T2, and Valid are optional for SharedNetworks, and
142  // T1 and T2 are optional for Subnet4 thus we will only
143  // output them if they are marked as specified.
144  if (!getT1().unspecified()) {
145  map->set("renew-timer",
146  Element::create(static_cast<long long>(getT1().get())));
147  }
148 
149  // Set rebind-timer
150  if (!getT2().unspecified()) {
151  map->set("rebind-timer",
152  Element::create(static_cast<long long>(getT2().get())));
153  }
154 
155  // Set valid-lifetime
156  if (!getValid().unspecified()) {
157  map->set("valid-lifetime",
158  Element::create(static_cast<long long>
159  (getValid().get())));
160  }
161 
162  // Set reservation mode
163  Network::HRMode hrmode = getHostReservationMode();
164  std::string mode;
165  switch (hrmode) {
166  case HR_DISABLED:
167  mode = "disabled";
168  break;
169  case HR_OUT_OF_POOL:
170  mode = "out-of-pool";
171  break;
172  case HR_GLOBAL:
173  mode = "global";
174  break;
175  case HR_ALL:
176  mode = "all";
177  break;
178  default:
180  "invalid host reservation mode: " << hrmode);
181  }
182  map->set("reservation-mode", Element::create(mode));
183 
184  // Set options
185  ConstCfgOptionPtr opts = getCfgOption();
186  map->set("option-data", opts->toElement());
187 
188  return (map);
189 }
190 
192 Network4::toElement() const {
193  ElementPtr map = Network::toElement();
194 
195  // Set match-client-id
196  map->set("match-client-id", Element::create(getMatchClientId()));
197 
198  // Set authoritative
199  map->set("authoritative", Element::create(getAuthoritative()));
200 
201  return (map);
202 }
203 
204 IOAddress
205 Network4::getServerId() const {
206  try {
207  OptionCustomPtr opt_server_id = boost::dynamic_pointer_cast<OptionCustom>
208  (cfg_option_->get(DHCP4_OPTION_SPACE, DHO_DHCP_SERVER_IDENTIFIER).option_);
209  if (opt_server_id) {
210  return (opt_server_id->readAddress());
211  }
212  } catch (const std::exception&) {
213  // Ignore any exceptions and simply return empty buffer.
214  }
215 
216  return (IOAddress::IPV4_ZERO_ADDRESS());
217 }
218 
220 Network6::toElement() const {
221  ElementPtr map = Network::toElement();
222 
223  // Set preferred-lifetime
224  map->set("preferred-lifetime",
225  Element::create(static_cast<long long>
226  (getPreferred().get())));
227 
228  // Set interface-id
229  const OptionPtr& ifaceid = getInterfaceId();
230  if (ifaceid) {
231  std::vector<uint8_t> bin = ifaceid->getData();
232  std::string ifid;
233  ifid.resize(bin.size());
234  if (!bin.empty()) {
235  std::memcpy(&ifid[0], &bin[0], bin.size());
236  }
237  map->set("interface-id", Element::create(ifid));
238  }
239 
240  // Set rapid-commit
241  bool rapid_commit = getRapidCommit();
242  map->set("rapid-commit", Element::create(rapid_commit));
243 
244  return (map);
245 }
246 
247 } // end of namespace isc::dhcp
248 } // end of namespace isc
option_space.h
isc::dhcp::ConstCfgOptionPtr
boost::shared_ptr< const CfgOption > ConstCfgOptionPtr
Const pointer.
Definition: cfg_option.h:500
isc::data
Definition: cfg_to_element.h:25
dhcp4.h
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
isc::dhcp::DHO_DHCP_SERVER_IDENTIFIER
@ DHO_DHCP_SERVER_IDENTIFIER
Definition: dhcp4.h:123
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: exceptions/exceptions.h:192
isc::dhcp::ClientClasses::cbegin
const_iterator cbegin() const
Iterator to the first element.
Definition: classify.h:81
isc::BadValue
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Definition: exceptions/exceptions.h:132
isc::ToElementError
Cannot unparse error.
Definition: cfg_to_element.h:19
isc::dhcp::ClientClasses::contains
bool contains(const ClientClass &x) const
returns if class x belongs to the defined classes
Definition: classify.h:94
DHCP4_OPTION_SPACE
#define DHCP4_OPTION_SPACE
Definition: option_space.h:16
isc::dhcp::OptionCustomPtr
boost::shared_ptr< OptionCustom > OptionCustomPtr
A pointer to the OptionCustom object.
Definition: option_custom.h:464
isc::dhcp::ClientClasses::empty
bool empty() const
Check if classes is empty.
Definition: classify.h:68
isc::dhcp::ClientClasses::const_iterator
std::list< ClientClass >::const_iterator const_iterator
Type of iterators.
Definition: classify.h:47
isc::dhcp::ClientClass
std::string ClientClass
Defines a single class name.
Definition: classify.h:37
isc::dhcp::Network::HRMode
HRMode
Specifies allowed host reservation mode.
Definition: network.h:91
isc::dhcp::OptionPtr
boost::shared_ptr< Option > OptionPtr
Definition: option.h:37
network.h
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition: data.h:20
option_custom.h
isc::dhcp::ClientClasses::cend
const_iterator cend() const
Iterator to the past the end element.
Definition: classify.h:86
isc::dhcp::IOAddressList
std::vector< isc::asiolink::IOAddress > IOAddressList
List of IOAddresses.
Definition: network.h:29
isc::dhcp::ClientClasses
Container for storing client class names.
Definition: classify.h:43