Kea 2.7.6
option6_client_fqdn.cc
Go to the documentation of this file.
1// Copyright (C) 2013-2024 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/dhcp6.h>
11#include <dns/labelsequence.h>
12#include <util/buffer.h>
13#include <util/str.h>
14#include <sstream>
15
16namespace isc {
17namespace dhcp {
18
30public:
32 uint8_t flags_;
34 boost::shared_ptr<isc::dns::Name> domain_name_;
37
45 Option6ClientFqdnImpl(const uint8_t flags,
46 const std::string& domain_name,
47 const Option6ClientFqdn::DomainNameType name_type);
48
57
62
67
73 void setDomainName(const std::string& domain_name,
74 const Option6ClientFqdn::DomainNameType name_type);
75
87 static void checkFlags(const uint8_t flags, const bool check_mbz);
88
97
98};
99
101Option6ClientFqdnImpl(const uint8_t flags,
102 const std::string& domain_name,
103 // cppcheck 1.57 complains that const enum value is not
104 // passed by reference. Note that it accepts the non-const
105 // enum to be passed by value. In both cases it is
106 // unnecessary to pass the enum by reference.
107 // cppcheck-suppress passedByValue
108 const Option6ClientFqdn::DomainNameType name_type)
109 : flags_(flags),
110 domain_name_(),
111 domain_name_type_(name_type) {
112
113 // Check if flags are correct. Also check if MBZ bits are set.
114 checkFlags(flags_, true);
115 // Set domain name. It may throw an exception if domain name has wrong
116 // format.
117 setDomainName(domain_name, name_type);
118}
119
122 parseWireData(first, last);
123 // Verify that flags value was correct. Do not check if MBZ bits are
124 // set because we should ignore those bits in received message.
125 checkFlags(flags_, false);
126}
127
130 : flags_(source.flags_),
131 domain_name_(),
132 domain_name_type_(source.domain_name_type_) {
133 if (source.domain_name_) {
134 domain_name_.reset(new isc::dns::Name(*source.domain_name_));
135 }
136}
137
139// This assignment operator handles assignment to self, it copies all
140// required values.
141// cppcheck-suppress operatorEqToSelf
143 if (source.domain_name_) {
144 domain_name_.reset(new isc::dns::Name(*source.domain_name_));
145
146 } else {
147 domain_name_.reset();
148
149 }
150
151 // This assignment should be exception safe.
152 flags_ = source.flags_;
154
155 return (*this);
156}
157
158void
160setDomainName(const std::string& domain_name,
161 // cppcheck 1.57 complains that const enum value is not
162 // passed by reference. Note that it accepts the non-const
163 // enum to be passed by value. In both cases it is
164 // unnecessary to pass the enum by reference.
165 // cppcheck-suppress passedByValue
166 const Option6ClientFqdn::DomainNameType name_type) {
167 // domain-name must be trimmed. Otherwise, string comprising spaces only
168 // would be treated as a fully qualified name.
169 std::string name = isc::util::str::trim(domain_name);
170 if (name.empty()) {
171 if (name_type == Option6ClientFqdn::FULL) {
173 "fully qualified domain-name must not be empty"
174 << " when setting new domain-name for DHCPv6 Client"
175 << " FQDN Option");
176 }
177 // The special case when domain-name is empty is marked by setting the
178 // pointer to the domain-name object to NULL.
179 domain_name_.reset();
180
181 } else {
182 try {
183 domain_name_.reset(new isc::dns::Name(name, true));
184
185 } catch (const Exception&) {
186 isc_throw(InvalidOption6FqdnDomainName, "invalid domain-name value '"
187 << domain_name << "' when setting new domain-name for"
188 << " DHCPv6 Client FQDN Option");
189
190 }
191 }
192
193 domain_name_type_ = name_type;
194}
195
196void
197Option6ClientFqdnImpl::checkFlags(const uint8_t flags, const bool check_mbz) {
198 // The Must Be Zero (MBZ) bits must not be set.
199 if (check_mbz && ((flags & ~Option6ClientFqdn::FLAG_MASK) != 0)) {
201 "invalid DHCPv6 Client FQDN Option flags: 0x"
202 << std::hex << static_cast<int>(flags) << std::dec);
203 }
204
205 // According to RFC 4704, section 4.1. if the N bit is 1, the S bit
206 // MUST be 0. Checking it here.
210 "both N and S flag of the DHCPv6 Client FQDN Option are set."
211 << " According to RFC 4704, if the N bit is 1 the S bit"
212 << " MUST be 0");
213 }
214}
215
216void
219
220 // Buffer must comprise at least one byte with the flags.
221 // The domain-name may be empty.
222 if (std::distance(first, last) < Option6ClientFqdn::FLAG_FIELD_LEN) {
223 isc_throw(OutOfRange, "DHCPv6 Client FQDN Option ("
224 << D6O_CLIENT_FQDN << ") is truncated. Minimal option"
225 << " size is " << Option6ClientFqdn::FLAG_FIELD_LEN
226 << ", got option with size " << std::distance(first, last));
227 }
228
229 // Parse flags
230 flags_ = *(first++);
231
232 // Parse domain-name if any.
233 if (std::distance(first, last) > 0) {
234 // The FQDN may comprise a partial domain-name. In this case it lacks
235 // terminating 0. If this is the case, we will need to add zero at
236 // the end because Name object constructor requires it.
237 if (*(last - 1) != 0) {
238 // Create temporary buffer and add terminating zero.
239 OptionBuffer buf(first, last);
240 buf.push_back(0);
241 // Reset domain name.
242 isc::util::InputBuffer name_buf(&buf[0], buf.size());
243 try {
244 domain_name_.reset(new isc::dns::Name(name_buf, true));
245 } catch (const Exception&) {
247 isc_throw(SkipThisOptionError, "failed to parse "
248 "partial domain-name from wire format");
249 } else {
250 isc_throw(InvalidOption6FqdnDomainName, "failed to parse "
251 "partial domain-name from wire format");
252 }
253 }
254 // Terminating zero was missing, so set the domain-name type
255 // to partial.
257 } else {
258 // We are dealing with fully qualified domain name so there is
259 // no need to add terminating zero. Simply pass the buffer to
260 // Name object constructor.
261 isc::util::InputBuffer name_buf(&(*first),
262 std::distance(first, last));
263 try {
264 domain_name_.reset(new isc::dns::Name(name_buf, true));
265 } catch (const Exception&) {
267 isc_throw(SkipThisOptionError, "failed to parse "
268 "fully qualified domain-name from wire format");
269 } else {
270 isc_throw(InvalidOption6FqdnDomainName, "failed to parse "
271 "fully qualified domain-name from wire format");
272 }
273 }
274 // Set the domain-type to fully qualified domain name.
276 }
277 }
278}
279
282 impl_(new Option6ClientFqdnImpl(flag, "", PARTIAL)) {
283}
284
286 const std::string& domain_name,
287 const DomainNameType domain_name_type)
289 impl_(new Option6ClientFqdnImpl(flag, domain_name, domain_name_type)) {
290}
291
297
299 delete(impl_);
300}
301
303 : Option(source),
304 impl_(new Option6ClientFqdnImpl(*source.impl_)) {
305}
306
311
313// This assignment operator handles assignment to self, it uses copy
314// constructor of Option6ClientFqdnImpl to copy all required values.
315// cppcheck-suppress operatorEqToSelf
317 Option::operator=(source);
318 Option6ClientFqdnImpl* old_impl = impl_;
319 impl_ = new Option6ClientFqdnImpl(*source.impl_);
320 delete(old_impl);
321 return (*this);
322}
323
324bool
325Option6ClientFqdn::getFlag(const uint8_t flag) const {
326 // Caller should query for one of the: N, S or O flags. Any other
327 // value is invalid.
328 if (flag != FLAG_S && flag != FLAG_O && flag != FLAG_N) {
329 isc_throw(InvalidOption6FqdnFlags, "invalid DHCPv6 Client FQDN"
330 << " Option flag specified, expected N, S or O");
331 }
332
333 return ((impl_->flags_ & flag) != 0);
334}
335
336void
337Option6ClientFqdn::setFlag(const uint8_t flag, const bool set_flag) {
338 // Check that flag is in range between 0x1 and 0x7. Note that this
339 // allows to set or clear multiple flags concurrently. Setting
340 // concurrent bits is discouraged (see header file) but it is not
341 // checked here so it will work.
342 if (((flag & ~FLAG_MASK) != 0) || (flag == 0)) {
343 isc_throw(InvalidOption6FqdnFlags, "invalid DHCPv6 Client FQDN"
344 << " Option flag 0x" << std::hex
345 << static_cast<int>(flag) << std::dec
346 << " is being set. Expected: N, S or O");
347 }
348
349 // Copy the current flags into local variable. That way we will be able
350 // to test new flags settings before applying them.
351 uint8_t new_flag = impl_->flags_;
352 if (set_flag) {
353 new_flag |= flag;
354 } else {
355 new_flag &= ~flag;
356 }
357
358 // Check new flags. If they are valid, apply them.
359 Option6ClientFqdnImpl::checkFlags(new_flag, true);
360 impl_->flags_ = new_flag;
361}
362
363void
365 impl_->flags_ = 0;
366}
367
368std::string
370 if (impl_->domain_name_) {
371 return (impl_->domain_name_->toText(impl_->domain_name_type_ ==
372 PARTIAL));
373 }
374 // If an object holding domain-name is NULL it means that the domain-name
375 // is empty.
376 return ("");
377}
378
379void
381 // There is nothing to do if domain-name is empty.
382 if (!impl_->domain_name_) {
383 return;
384 }
385
386 // Domain name, encoded as a set of labels.
388 if (labels.getDataLength() > 0) {
389 size_t read_len = 0;
390 const uint8_t* data = labels.getData(&read_len);
391 if (impl_->domain_name_type_ == PARTIAL) {
392 --read_len;
393 }
394 buf.writeData(data, read_len);
395 }
396}
397
398void
399Option6ClientFqdn::setDomainName(const std::string& domain_name,
400 const DomainNameType domain_name_type) {
401 impl_->setDomainName(domain_name, domain_name_type);
402}
403
404void
408
411 return (impl_->domain_name_type_);
412}
413
414void
416 // Header = option code and length.
417 packHeader(buf);
418 // Flags field.
419 buf.writeUint8(impl_->flags_);
420 // Domain name.
421 packDomainName(buf);
422}
423
424void
427 setData(first, last);
428 impl_->parseWireData(first, last);
429 // Check that the flags in the received option are valid. Ignore MBZ bits
430 // because we don't want to discard the whole option because of MBZ bits
431 // being set.
432 impl_->checkFlags(impl_->flags_, false);
433}
434
435std::string
436Option6ClientFqdn::toText(int indent) const {
437 std::ostringstream stream;
438 std::string in(indent, ' '); // base indentation
439 stream << in << "type=" << type_ << "(CLIENT_FQDN), "
440 << "flags: ("
441 << "N=" << (getFlag(FLAG_N) ? "1" : "0") << ", "
442 << "O=" << (getFlag(FLAG_O) ? "1" : "0") << ", "
443 << "S=" << (getFlag(FLAG_S) ? "1" : "0") << "), "
444 << "domain-name='" << getDomainName() << "' ("
445 << (getDomainNameType() == PARTIAL ? "partial" : "full")
446 << ")";
447
448 return (stream.str());
449}
450
451uint16_t
453 uint16_t domain_name_length = 0;
454 if (impl_->domain_name_) {
455 // If domain name is partial, the NULL terminating character
456 // is not included and the option. Length has to be adjusted.
457 domain_name_length = impl_->domain_name_type_ == FULL ?
458 impl_->domain_name_->getLength() :
459 impl_->domain_name_->getLength() - 1;
460 }
461 return (getHeaderLen() + FLAG_FIELD_LEN + domain_name_length);
462}
463
464} // end of isc::dhcp namespace
465} // end of isc namespace
This is a base class for exceptions thrown from the DNS library module.
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
Exception thrown when invalid domain name is specified.
Exception thrown when invalid flags have been specified for DHCPv6 Client Fqdn Option.
Implements the logic for the Option6ClientFqdn class.
Option6ClientFqdnImpl & operator=(const Option6ClientFqdnImpl &source)
Assignment operator.
boost::shared_ptr< isc::dns::Name > domain_name_
Holds the pointer to a domain name carried in the option.
uint8_t flags_
Holds flags carried by the option.
Option6ClientFqdn::DomainNameType domain_name_type_
Indicates whether domain name is partial or fully qualified.
void setDomainName(const std::string &domain_name, const Option6ClientFqdn::DomainNameType name_type)
Set a new domain name for the option.
void parseWireData(OptionBufferConstIter first, OptionBufferConstIter last)
Parse the Option provided in the wire format.
Option6ClientFqdnImpl(const uint8_t flags, const std::string &domain_name, const Option6ClientFqdn::DomainNameType name_type)
Constructor, from domain name.
static void checkFlags(const uint8_t flags, const bool check_mbz)
Check if flags are valid.
Represents DHCPv6 Client FQDN Option (code 39).
void resetFlags()
Sets the flag field value to 0.
static const uint8_t FLAG_S
S bit.
static const uint8_t FLAG_N
N bit.
DomainNameType getDomainNameType() const
Returns enumerator value which indicates whether domain-name is partial or full.
virtual OptionPtr clone() const
Copies this option and returns a pointer to the copy.
static const uint16_t FLAG_FIELD_LEN
The length of the flag field within DHCPv6 Client Fqdn Option.
void setDomainName(const std::string &domain_name, const DomainNameType domain_name_type)
Set new domain-name.
void resetDomainName()
Set empty domain-name.
static const uint8_t FLAG_O
O bit.
static const uint8_t FLAG_MASK
Mask which zeroes MBZ flag bits.
virtual uint16_t len() const
Returns length of the complete option (data length + DHCPv6 option header).
virtual void unpack(OptionBufferConstIter first, OptionBufferConstIter last)
Parses option from the received buffer.
Option6ClientFqdn(const uint8_t flags, const std::string &domain_name, const DomainNameType domain_name_type=FULL)
Constructor, creates option instance using flags and domain name.
std::string getDomainName() const
Returns the domain-name in the text format.
DomainNameType
Type of the domain-name: partial or full.
virtual void pack(isc::util::OutputBuffer &buf, bool check=true) const
Writes option in the wire format into a buffer.
Option6ClientFqdn & operator=(const Option6ClientFqdn &source)
Assignment operator.
void packDomainName(isc::util::OutputBuffer &buf) const
Writes domain-name in the wire format into a buffer.
virtual std::string toText(int indent=0) const
Returns string representation of the option.
bool getFlag(const uint8_t flag) const
Checks if the specified flag of the DHCPv6 Client FQDN Option is set.
virtual ~Option6ClientFqdn()
Destructor.
void setFlag(const uint8_t flag, const bool set)
Modifies the value of the specified DHCPv6 Client Fqdn Option flag.
uint16_t type_
option type (0-255 for DHCPv4, 0-65535 for DHCPv6)
Definition option.h:597
static bool lenient_parsing_
Governs whether options should be parsed less strictly.
Definition option.h:490
virtual uint16_t getHeaderLen() const
Returns length of header (2 for v4, 4 for v6)
Definition option.cc:327
Option & operator=(const Option &rhs)
Assignment operator.
Definition option.cc:73
void setData(InputIterator first, InputIterator last)
Sets content of this option from buffer.
Definition option.h:434
OptionPtr cloneInternal() const
Copies this option and returns a pointer to the copy.
Definition option.h:504
void packHeader(isc::util::OutputBuffer &buf, bool check=true) const
Store option's header in a buffer.
Definition option.cc:119
Exception thrown during option unpacking This exception is thrown when an error has occurred unpackin...
Definition option.h:67
Light-weight Accessor to Name data.
const uint8_t * getData(size_t *len) const
Return the wire-format data for this LabelSequence.
size_t getDataLength() const
Return the length of the wire-format data of this LabelSequence.
The Name class encapsulates DNS names.
Definition name.h:219
The InputBuffer class is a buffer abstraction for manipulating read-only data.
Definition buffer.h:81
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition buffer.h:343
void writeUint8(uint8_t data)
Write an unsigned 8-bit integer into the buffer.
Definition buffer.h:473
void writeData(const void *data, size_t len)
Copy an arbitrary length of data into the buffer.
Definition buffer.h:556
@ D6O_CLIENT_FQDN
Definition dhcp6.h:59
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
OptionBuffer::const_iterator OptionBufferConstIter
const_iterator for walking over OptionBuffer
Definition option.h:30
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition option.h:24
boost::shared_ptr< Option > OptionPtr
Definition option.h:37
string trim(const string &input)
Trim leading and trailing spaces.
Definition str.cc:32
Defines the logger used by the top-level component of kea-lfc.