1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
// Copyright (C) 2017-2023 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <config.h>
#include <dhcp/dhcp4.h>
#include <dhcp/option_custom.h>
#include <dhcp/option_space.h>
#include <dhcpsrv/network.h>
#include <boost/pointer_cast.hpp>
using namespace isc::asiolink;
using namespace isc::data;
using namespace isc::util;
namespace isc {
namespace dhcp {
void
Network::RelayInfo::addAddress(const asiolink::IOAddress& addr) {
if (containsAddress(addr)) {
isc_throw (BadValue, "RelayInfo already contains address: "
<< addr.toText());
}
addresses_.push_back(addr);
}
bool
Network::RelayInfo::hasAddresses() const {
return (!addresses_.empty());
}
bool
Network::RelayInfo::containsAddress(const asiolink::IOAddress& addr) const {
auto const& index = addresses_.get<IOAddressListSetTag>();
return (index.find(addr) != index.end());
}
const IOAddressList&
Network::RelayInfo::getAddresses() const {
return (addresses_);
}
void
Network::addRelayAddress(const asiolink::IOAddress& addr) {
relay_.addAddress(addr);
}
bool
Network::hasRelays() const {
return (relay_.hasAddresses());
}
bool
Network::hasRelayAddress(const asiolink::IOAddress& addr) const {
return (relay_.containsAddress(addr));
}
const IOAddressList&
Network::getRelayAddresses() const {
return (relay_.getAddresses());
}
bool
Network::clientSupported(const isc::dhcp::ClientClasses& classes) const {
if (client_class_.empty()) {
// There is no class defined for this network, so we do
// support everyone.
return (true);
}
return (classes.contains(client_class_));
}
void
Network::allowClientClass(const isc::dhcp::ClientClass& class_name) {
client_class_ = class_name;
}
void
Network::requireClientClass(const isc::dhcp::ClientClass& class_name) {
if (!required_classes_.contains(class_name)) {
required_classes_.insert(class_name);
}
}
const ClientClasses&
Network::getRequiredClasses() const {
return (required_classes_);
}
Optional<IOAddress>
Network::getGlobalProperty(Optional<IOAddress> property,
const int global_index,
const int /*min_index*/,
const int /*max_index*/) const {
if ((global_index >= 0) && fetch_globals_fn_) {
ConstCfgGlobalsPtr globals = fetch_globals_fn_();
if (globals) {
ConstElementPtr global_param = globals->get(global_index);
if (global_param) {
std::string global_str = global_param->stringValue();
if (!global_str.empty()) {
return (IOAddress(global_str));
}
}
}
}
return (property);
}
ElementPtr
Network::toElement() const {
ElementPtr map = Element::createMap();
// Set user-context
contextToElement(map);
// Set interface
if (!iface_name_.unspecified()) {
map->set("interface", Element::create(iface_name_.get()));
}
ElementPtr relay_map = Element::createMap();
ElementPtr address_list = Element::createList();
const IOAddressList addresses = getRelayAddresses();
for (auto address = addresses.begin(); address != addresses.end(); ++address) {
address_list->add(Element::create((*address).toText()));
}
relay_map->set("ip-addresses", address_list);
map->set("relay", relay_map);
// Set client-class
if (!client_class_.unspecified()) {
map->set("client-class", Element::create(client_class_.get()));
}
// Set require-client-classes
const ClientClasses& classes = getRequiredClasses();
if (!classes.empty()) {
ElementPtr class_list = Element::createList();
for (ClientClasses::const_iterator it = classes.cbegin();
it != classes.cend(); ++it) {
class_list->add(Element::create(*it));
}
map->set("require-client-classes", class_list);
}
// T1, T2, and Valid are optional for SharedNetworks, and
// T1 and T2 are optional for Subnet4 thus we will only
// output them if they are marked as specified.
if (!t1_.unspecified()) {
map->set("renew-timer",
Element::create(static_cast<long long>(t1_.get())));
}
// Set rebind-timer
if (!t2_.unspecified()) {
map->set("rebind-timer",
Element::create(static_cast<long long>(t2_.get())));
}
// Set valid-lifetime
if (!valid_.unspecified()) {
map->set("valid-lifetime",
Element::create(static_cast<long long>(valid_.get())));
map->set("min-valid-lifetime",
Element::create(static_cast<long long>(valid_.getMin())));
map->set("max-valid-lifetime",
Element::create(static_cast<long long>(valid_.getMax())));
}
// Set reservations-global
if (!reservations_global_.unspecified()) {
map->set("reservations-global",
Element::create(reservations_global_.get()));
}
// Set reservations-in-subnet
if (!reservations_in_subnet_.unspecified()) {
map->set("reservations-in-subnet",
Element::create(reservations_in_subnet_.get()));
}
// Set reservations-out-of-pool
if (!reservations_out_of_pool_.unspecified()) {
map->set("reservations-out-of-pool",
Element::create(reservations_out_of_pool_.get()));
}
// Set options
ConstCfgOptionPtr opts = getCfgOption();
map->set("option-data", opts->toElement());
// Output calculate-tee-times and percentages if calculation is enabled.
if (!calculate_tee_times_.unspecified()) {
map->set("calculate-tee-times", Element::create(calculate_tee_times_));
}
if (!t1_percent_.unspecified()) {
map->set("t1-percent", Element::create(t1_percent_));
}
if (!t2_percent_.unspecified()) {
map->set("t2-percent", Element::create(t2_percent_));
}
if (!ddns_send_updates_.unspecified()) {
map->set("ddns-send-updates", Element::create(ddns_send_updates_));
}
if (!ddns_override_no_update_.unspecified()) {
map->set("ddns-override-no-update", Element::create(ddns_override_no_update_));
}
if (!ddns_override_client_update_.unspecified()) {
map->set("ddns-override-client-update", Element::create(ddns_override_client_update_));
}
if (!ddns_replace_client_name_mode_.unspecified()) {
map->set("ddns-replace-client-name",
Element::create(D2ClientConfig::
replaceClientNameModeToString(ddns_replace_client_name_mode_)));
}
if (!ddns_generated_prefix_.unspecified()) {
map->set("ddns-generated-prefix", Element::create(ddns_generated_prefix_));
}
if (!ddns_qualifying_suffix_.unspecified()) {
map->set("ddns-qualifying-suffix", Element::create(ddns_qualifying_suffix_));
}
if (!hostname_char_set_.unspecified()) {
map->set("hostname-char-set", Element::create(hostname_char_set_));
}
if (!hostname_char_replacement_.unspecified()) {
map->set("hostname-char-replacement", Element::create(hostname_char_replacement_));
}
if (!store_extended_info_.unspecified()) {
map->set("store-extended-info", Element::create(store_extended_info_));
}
if (!cache_threshold_.unspecified()) {
map->set("cache-threshold", Element::create(cache_threshold_));
}
if (!cache_max_age_.unspecified()) {
map->set("cache-max-age",
Element::create(static_cast<long long>(cache_max_age_)));
}
if (!ddns_update_on_renew_.unspecified()) {
map->set("ddns-update-on-renew", Element::create(ddns_update_on_renew_));
}
if (!ddns_use_conflict_resolution_.unspecified()) {
map->set("ddns-use-conflict-resolution", Element::create(ddns_use_conflict_resolution_));
}
if (!allocator_type_.unspecified()) {
map->set("allocator", Element::create(allocator_type_));
}
return (map);
}
void
Network4::setSiaddr(const Optional<IOAddress>& siaddr) {
if (!siaddr.get().isV4()) {
isc_throw(BadValue, "Can't set siaddr to non-IPv4 address "
<< siaddr);
}
siaddr_ = siaddr;
}
void
Network4::setSname(const Optional<std::string>& sname) {
sname_ = sname;
}
void
Network4::setFilename(const Optional<std::string>& filename) {
filename_ = filename;
}
ElementPtr
Network4::toElement() const {
ElementPtr map = Network::toElement();
// Set match-client-id
if (!match_client_id_.unspecified()) {
map->set("match-client-id", Element::create(match_client_id_.get()));
}
// Set authoritative
if (!authoritative_.unspecified()) {
map->set("authoritative", Element::create(authoritative_.get()));
}
// Set next-server
if (!siaddr_.unspecified()) {
map->set("next-server", Element::create(siaddr_.get().toText()));
}
// Set server-hostname
if (!sname_.unspecified()) {
map->set("server-hostname", Element::create(sname_.get()));
}
// Set boot-file-name
if (!filename_.unspecified()) {
map->set("boot-file-name",Element::create(filename_.get()));
}
// Set offer-lifetime
if (!offer_lft_.unspecified()) {
map->set("offer-lifetime",Element::create(offer_lft_.get()));
}
return (map);
}
IOAddress
Network4::getServerId() const {
try {
OptionCustomPtr opt_server_id = boost::dynamic_pointer_cast<OptionCustom>
(cfg_option_->get(DHCP4_OPTION_SPACE, DHO_DHCP_SERVER_IDENTIFIER).option_);
if (opt_server_id) {
return (opt_server_id->readAddress());
}
} catch (const std::exception&) {
// Ignore any exceptions and simply return empty buffer.
}
return (IOAddress::IPV4_ZERO_ADDRESS());
}
ElementPtr
Network6::toElement() const {
ElementPtr map = Network::toElement();
// Set preferred-lifetime
if (!preferred_.unspecified()) {
map->set("preferred-lifetime",
Element::create(static_cast<long long>(preferred_.get())));
map->set("min-preferred-lifetime",
Element::create(static_cast<long long>(preferred_.getMin())));
map->set("max-preferred-lifetime",
Element::create(static_cast<long long>(preferred_.getMax())));
}
// Set interface-id
if (interface_id_) {
std::vector<uint8_t> bin = interface_id_->getData();
std::string ifid;
ifid.resize(bin.size());
if (!bin.empty()) {
std::memcpy(&ifid[0], &bin[0], bin.size());
}
map->set("interface-id", Element::create(ifid));
}
// Set rapid-commit
if (!rapid_commit_.unspecified()) {
map->set("rapid-commit", Element::create(rapid_commit_.get()));
}
// Set pd-allocator
if (!pd_allocator_type_.unspecified()) {
map->set("pd-allocator", Element::create(pd_allocator_type_));
}
return (map);
}
} // end of namespace isc::dhcp
} // end of namespace isc
|