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
386
387
388
389
390
391
|
// Copyright (C) 2014-2016 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 <dhcpsrv/host.h>
#include <util/encode/hex.h>
#include <util/strutil.h>
#include <exceptions/exceptions.h>
#include <sstream>
namespace isc {
namespace dhcp {
IPv6Resrv::IPv6Resrv(const Type& type,
const asiolink::IOAddress& prefix,
const uint8_t prefix_len)
: type_(type), prefix_(asiolink::IOAddress("::")), prefix_len_(128) {
// Validate and set the actual values.
set(type, prefix, prefix_len);
}
void
IPv6Resrv::set(const Type& type, const asiolink::IOAddress& prefix,
const uint8_t prefix_len) {
if (!prefix.isV6() || prefix.isV6Multicast()) {
isc_throw(isc::BadValue, "invalid prefix '" << prefix
<< "' for new IPv6 reservation");
} else if (prefix_len > 128) {
isc_throw(isc::BadValue, "invalid prefix length '"
<< static_cast<int>(prefix_len)
<< "' for new IPv6 reservation");
} else if ((type == TYPE_NA) && (prefix_len != 128)) {
isc_throw(isc::BadValue, "invalid prefix length '"
<< static_cast<int>(prefix_len)
<< "' for reserved IPv6 address, expected 128");
}
type_ = type;
prefix_ = prefix;
prefix_len_ = prefix_len;
}
std::string
IPv6Resrv::toText() const {
std::ostringstream s;
s << prefix_;
// For PD, append prefix length.
if (getType() == TYPE_PD) {
s << "/" << static_cast<int>(prefix_len_);
}
return (s.str());
}
bool
IPv6Resrv::operator==(const IPv6Resrv& other) const {
return (type_ == other.type_ &&
prefix_ == other.prefix_ &&
prefix_len_ == other.prefix_len_);
}
bool
IPv6Resrv::operator!=(const IPv6Resrv& other) const {
return (!operator==(other));
}
Host::Host(const uint8_t* identifier, const size_t identifier_len,
const IdentifierType& identifier_type,
const SubnetID ipv4_subnet_id, const SubnetID ipv6_subnet_id,
const asiolink::IOAddress& ipv4_reservation,
const std::string& hostname,
const std::string& dhcp4_client_classes,
const std::string& dhcp6_client_classes)
: identifier_type_(identifier_type),
identifier_value_(), ipv4_subnet_id_(ipv4_subnet_id),
ipv6_subnet_id_(ipv6_subnet_id),
ipv4_reservation_(asiolink::IOAddress::IPV4_ZERO_ADDRESS()),
hostname_(hostname), dhcp4_client_classes_(dhcp4_client_classes),
dhcp6_client_classes_(dhcp6_client_classes), host_id_(0),
cfg_option4_(), cfg_option6_() {
// Initialize host identifier.
setIdentifier(identifier, identifier_len, identifier_type);
if (!ipv4_reservation.isV4Zero()) {
// Validate and set IPv4 address reservation.
setIPv4Reservation(ipv4_reservation);
}
}
Host::Host(const std::string& identifier, const std::string& identifier_name,
const SubnetID ipv4_subnet_id, const SubnetID ipv6_subnet_id,
const asiolink::IOAddress& ipv4_reservation,
const std::string& hostname,
const std::string& dhcp4_client_classes,
const std::string& dhcp6_client_classes)
: identifier_type_(IDENT_HWADDR),
identifier_value_(), ipv4_subnet_id_(ipv4_subnet_id),
ipv6_subnet_id_(ipv6_subnet_id),
ipv4_reservation_(asiolink::IOAddress::IPV4_ZERO_ADDRESS()),
hostname_(hostname), dhcp4_client_classes_(dhcp4_client_classes),
dhcp6_client_classes_(dhcp6_client_classes), host_id_(0),
cfg_option4_(new CfgOption()), cfg_option6_(new CfgOption()) {
// Initialize host identifier.
setIdentifier(identifier, identifier_name);
if (!ipv4_reservation.isV4Zero()) {
// Validate and set IPv4 address reservation.
setIPv4Reservation(ipv4_reservation);
}
}
const std::vector<uint8_t>&
Host::getIdentifier() const {
return (identifier_value_);
}
Host::IdentifierType
Host::getIdentifierType() const {
return (identifier_type_);
}
HWAddrPtr
Host::getHWAddress() const {
return ((identifier_type_ == IDENT_HWADDR) ?
HWAddrPtr(new HWAddr(identifier_value_, HTYPE_ETHER)) : HWAddrPtr());
}
DuidPtr
Host::getDuid() const {
return ((identifier_type_ == IDENT_DUID) ?
DuidPtr(new DUID(identifier_value_)) : DuidPtr());
}
std::string
Host::getIdentifierAsText() const {
return (getIdentifierAsText(identifier_type_, &identifier_value_[0],
identifier_value_.size()));
}
std::string
Host::getIdentifierAsText(const IdentifierType& type, const uint8_t* value,
const size_t length) {
// Convert identifier into <type>=<value> form.
std::ostringstream s;
switch (type) {
case IDENT_HWADDR:
s << "hwaddr";
break;
case IDENT_DUID:
s << "duid";
break;
case IDENT_CIRCUIT_ID:
s << "circuit-id";
break;
default:
// This should never happen actually, unless we add new identifier
// and forget to add a case for it above.
s << "(invalid-type)";
}
std::vector<uint8_t> vec(value, value + length);
s << "=" << (length > 0 ? util::encode::encodeHex(vec) : "(null)");
return (s.str());
}
std::string
Host::getIdentifierName(const IdentifierType& type) {
switch (type) {
case Host::IDENT_HWADDR:
return ("hw-address");
case Host::IDENT_DUID:
return ("duid");
case Host::IDENT_CIRCUIT_ID:
return ("circuit-id");
default:
;
}
return ("(unknown)");
}
void
Host::setIdentifier(const uint8_t* identifier, const size_t len,
const IdentifierType& type) {
if (len < 1) {
isc_throw(BadValue, "invalid client identifier length 0");
}
identifier_type_ = type;
identifier_value_.assign(identifier, identifier + len);
}
void
Host::setIdentifier(const std::string& identifier, const std::string& name) {
// HW address and DUID are special cases because they are typically
// specified as values with colons between consecutive octets. Thus,
// we use the HWAddr and DUID classes to validate them and to
// convert them into binary format.
if (name == "hw-address") {
HWAddr hwaddr(HWAddr::fromText(identifier));
identifier_type_= IDENT_HWADDR;
identifier_value_ = hwaddr.hwaddr_;
} else if (name == "duid") {
identifier_type_ = IDENT_DUID;
DUID duid(DUID::fromText(identifier));
identifier_value_ = duid.getDuid();
} else {
if (name == "circuit-id") {
identifier_type_ = IDENT_CIRCUIT_ID;
} else {
isc_throw(isc::BadValue, "invalid client identifier type '"
<< name << "' when creating host instance");
}
// Here we're converting values other than DUID and HW address. These
// values can either be specified as strings of hexadecimal digits or
// strings in quotes. The latter are copied to a vector excluding quote
// characters.
// Try to convert the values in quotes into a vector of ASCII codes.
// If the identifier lacks opening and closing quote, this will return
// an empty value, in which case we'll try to decode it as a string of
// hexadecimal digits.
std::vector<uint8_t> binary = util::str::quotedStringToBinary(identifier);
if (binary.empty()) {
try {
util::encode::decodeHex(identifier, binary);
} catch (...) {
// The string doesn't match any known pattern, so we have to
// report an error at this point.
isc_throw(isc::BadValue, "invalid host identifier value '"
<< identifier << "'");
}
}
// Successfully decoded the identifier, so let's use it.
identifier_value_.swap(binary);
}
}
void
Host::setIPv4Reservation(const asiolink::IOAddress& address) {
if (!address.isV4()) {
isc_throw(isc::BadValue, "address '" << address << "' is not a valid"
" IPv4 address");
} else if (address.isV4Zero() || address.isV4Bcast()) {
isc_throw(isc::BadValue, "must not make reservation for the '"
<< address << "' address");
}
ipv4_reservation_ = address;
}
void
Host::removeIPv4Reservation() {
ipv4_reservation_ = asiolink::IOAddress::IPV4_ZERO_ADDRESS();
}
void
Host::addReservation(const IPv6Resrv& reservation) {
// Check if it is not duplicating existing reservation.
if (hasReservation(reservation)) {
isc_throw(isc::InvalidOperation, "failed on attempt to add a duplicated"
" host reservation for " << reservation.toText());
}
// Add it.
ipv6_reservations_.insert(IPv6ResrvTuple(reservation.getType(),
reservation));
}
IPv6ResrvRange
Host::getIPv6Reservations(const IPv6Resrv::Type& type) const {
return (ipv6_reservations_.equal_range(type));
}
IPv6ResrvRange
Host::getIPv6Reservations() const {
return (IPv6ResrvRange(ipv6_reservations_.begin(),
ipv6_reservations_.end()));
}
bool
Host::hasIPv6Reservation() const {
return (!ipv6_reservations_.empty());
}
bool
Host::hasReservation(const IPv6Resrv& reservation) const {
IPv6ResrvRange reservations = getIPv6Reservations(reservation.getType());
if (std::distance(reservations.first, reservations.second) > 0) {
for (IPv6ResrvIterator it = reservations.first;
it != reservations.second; ++it) {
if (it->second == reservation) {
return (true);
}
}
}
// No matching reservations found.
return (false);
}
void
Host::addClientClass4(const std::string& class_name) {
addClientClassInternal(dhcp4_client_classes_, class_name);
}
void
Host::addClientClass6(const std::string& class_name) {
addClientClassInternal(dhcp6_client_classes_, class_name);
}
void
Host::addClientClassInternal(ClientClasses& classes,
const std::string& class_name) {
std::string trimmed = util::str::trim(class_name);
if (!trimmed.empty()) {
classes.insert(ClientClass(trimmed));
}
}
std::string
Host::toText() const {
std::ostringstream s;
// Add HW address or DUID.
s << getIdentifierAsText();
// Add IPv4 subnet id if exists (non-zero).
if (ipv4_subnet_id_) {
s << " ipv4_subnet_id=" << ipv4_subnet_id_;
}
// Add IPv6 subnet id if exists (non-zero).
if (ipv6_subnet_id_) {
s << " ipv6_subnet_id=" << ipv6_subnet_id_;
}
// Add hostname.
s << " hostname=" << (hostname_.empty() ? "(empty)" : hostname_);
// Add IPv4 reservation.
s << " ipv4_reservation=" << (ipv4_reservation_.isV4Zero() ? "(no)" :
ipv4_reservation_.toText());
if (ipv6_reservations_.empty()) {
s << " ipv6_reservations=(none)";
} else {
// Add all IPv6 reservations.
for (IPv6ResrvIterator resrv = ipv6_reservations_.begin();
resrv != ipv6_reservations_.end(); ++resrv) {
s << " ipv6_reservation"
<< std::distance(ipv6_reservations_.begin(), resrv)
<< "=" << resrv->second.toText();
}
}
// Add DHCPv4 client classes.
for (ClientClasses::const_iterator cclass = dhcp4_client_classes_.begin();
cclass != dhcp4_client_classes_.end(); ++cclass) {
s << " dhcp4_class"
<< std::distance(dhcp4_client_classes_.begin(), cclass)
<< "=" << *cclass;
}
// Add DHCPv6 client classes.
for (ClientClasses::const_iterator cclass = dhcp6_client_classes_.begin();
cclass != dhcp6_client_classes_.end(); ++cclass) {
s << " dhcp6_class"
<< std::distance(dhcp6_client_classes_.begin(), cclass)
<< "=" << *cclass;
}
return (s.str());
}
} // end of namespace isc::dhcp
} // end of namespace isc
|