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
15using namespace isc::asiolink;
16using namespace isc::data;
17
18namespace isc {
19namespace dhcp {
20
21void
23 if (containsAddress(addr)) {
24 isc_throw (BadValue, "RelayInfo already contains address: "
25 << addr.toText());
26 }
27
28 addresses_.push_back(addr);
29}
30
31bool
33 return (!addresses_.empty());
34}
35
36bool
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
48const IOAddressList&
50 return (addresses_);
51}
52
53void
55 relay_.addAddress(addr);
56}
57
58bool
60 return (relay_.hasAddresses());
61}
62
63bool
65 return (relay_.containsAddress(addr));
66}
67
68const IOAddressList&
70 return (relay_.getAddresses());
71}
72
73bool
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
84void
86 client_class_ = class_name;
87}
88
89void
91 if (!required_classes_.contains(class_name)) {
92 required_classes_.insert(class_name);
93 }
94}
95
96const ClientClasses&
98 return (required_classes_);
99}
100
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
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
186 map->set("option-data", opts->toElement());
187
188 return (map);
189}
190
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
206 try {
207 OptionCustomPtr opt_server_id = boost::dynamic_pointer_cast<OptionCustom>
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
217}
218
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
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Cannot unparse error.
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition: data.cc:223
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition: data.cc:268
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition: data.cc:263
Container for storing client class names.
Definition: classify.h:43
bool contains(const ClientClass &x) const
returns if class x belongs to the defined classes
Definition: classify.h:94
void insert(const ClientClass &class_name)
Insert an element.
Definition: classify.h:62
std::list< ClientClass >::const_iterator const_iterator
Type of iterators.
Definition: classify.h:47
bool empty() const
Check if classes is empty.
Definition: classify.h:68
const_iterator cbegin() const
Iterator to the first element.
Definition: classify.h:81
const_iterator cend() const
Iterator to the past the end element.
Definition: classify.h:86
virtual asiolink::IOAddress getServerId() const
Returns binary representation of the dhcp-server-identifier option (54).
Definition: network.cc:205
virtual data::ElementPtr toElement() const
Unparses network object.
Definition: network.cc:192
virtual data::ElementPtr toElement() const
Unparses network object.
Definition: network.cc:220
const IOAddressList & getAddresses() const
Returns const reference to the list of addresses.
Definition: network.cc:49
void addAddress(const asiolink::IOAddress &addr)
Adds an address to the list of addresses.
Definition: network.cc:22
bool containsAddress(const asiolink::IOAddress &addr) const
Checks the address list for the given address.
Definition: network.cc:37
bool hasAddresses() const
Indicates whether or not the address list has entries.
Definition: network.cc:32
virtual bool clientSupported(const isc::dhcp::ClientClasses &client_classes) const
Checks whether this network supports client that belongs to specified classes.
Definition: network.cc:74
void addRelayAddress(const asiolink::IOAddress &addr)
Adds an address to the list addresses in the network's relay info.
Definition: network.cc:54
RelayInfo relay_
Relay information.
Definition: network.h:326
HRMode
Specifies allowed host reservation mode.
Definition: network.h:91
@ HR_DISABLED
None - host reservation is disabled.
Definition: network.h:95
@ HR_OUT_OF_POOL
Only out-of-pool reservations is allowed.
Definition: network.h:100
@ HR_GLOBAL
Only global reservations are allowed.
Definition: network.h:104
@ HR_ALL
Both out-of-pool and in-pool reservations are allowed.
Definition: network.h:112
Triplet< uint32_t > getValid() const
Return valid-lifetime for addresses in that prefix.
Definition: network.h:245
CfgOptionPtr cfg_option_
Pointer to the option data configuration for this subnet.
Definition: network.h:356
void requireClientClass(const isc::dhcp::ClientClass &class_name)
Adds class class_name to classes required to be evaluated.
Definition: network.cc:90
const IOAddressList & getRelayAddresses() const
Returns the list of relay addresses from the network's relay info.
Definition: network.cc:69
void allowClientClass(const isc::dhcp::ClientClass &class_name)
Sets the supported class to class class_name.
Definition: network.cc:85
bool hasRelays() const
Indicates if network's relay info has relay addresses.
Definition: network.cc:59
const isc::dhcp::ClientClass & getClientClass() const
returns the client class
Definition: network.h:240
std::string getIface() const
Returns name of the local interface for which this network is selected.
Definition: network.h:145
HRMode getHostReservationMode() const
Specifies what type of Host Reservations are supported.
Definition: network.h:289
virtual data::ElementPtr toElement() const
Unparses network object.
Definition: network.cc:102
bool hasRelayAddress(const asiolink::IOAddress &address) const
Tests if the network's relay info contains the given address.
Definition: network.cc:64
Triplet< uint32_t > getT2() const
Returns T2 (rebind timer), expressed in seconds.
Definition: network.h:269
const isc::dhcp::ClientClasses & getRequiredClasses() const
Returns classes which are required to be evaluated.
Definition: network.cc:97
ClientClass client_class_
Optional definition of a client class.
Definition: network.h:333
ClientClasses required_classes_
Required classes.
Definition: network.h:339
Triplet< uint32_t > getT1() const
Returns T1 (renew timer), expressed in seconds.
Definition: network.h:257
CfgOptionPtr getCfgOption()
Returns pointer to the option data configuration for this subnet.
Definition: network.h:303
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< Element > ElementPtr
Definition: data.h:22
std::string ClientClass
Defines a single class name.
Definition: classify.h:37
@ 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:29
boost::shared_ptr< Option > OptionPtr
Definition: option.h:38
boost::shared_ptr< const CfgOption > ConstCfgOptionPtr
Const pointer.
Definition: cfg_option.h:500
Defines the logger used by the top-level component of kea-dhcp-ddns.
#define DHCP4_OPTION_SPACE
Definition: option_space.h:16
void contextToElement(data::ElementPtr map) const
Merge unparse a user_context object.
Definition: user_context.cc:15