Kea: option_custom.h Source File

| Kea 2.2.0

option_custom.h
Go to the documentation of this file.
1// Copyright (C) 2012-2022 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#ifndef OPTION_CUSTOM_H
8#define OPTION_CUSTOM_H
9
10#include <asiolink/io_address.h>
11#include <dhcp/option.h>
12#include <dhcp/option_definition.h>
13#include <exceptions/isc_assert.h>
14#include <util/io_utilities.h>
15
16namespace isc {
17namespace dhcp {
18
32 class OptionCustom : public Option {
33public:
34
44 OptionCustom(const OptionDefinition& def, Universe u);
45
61 OptionCustom(const OptionDefinition& def, Universe u, const OptionBuffer& data);
62
81 OptionCustom(const OptionDefinition& def, Universe u,
82 OptionBufferConstIter first, OptionBufferConstIter last);
83
85 virtual OptionPtr clone() const;
86
91 void addArrayDataField(const asiolink::IOAddress& address);
92
96 void addArrayDataField(const bool value);
97
102 template<typename T>
103 void addArrayDataField(const T value) {
104 checkArrayType();
105 OptionDataType data_type = definition_.getType();
106 // Handle record last field.
107 if (data_type == OPT_RECORD_TYPE) {
108 data_type = definition_.getRecordFields().back();
109 }
110 if (OptionDataTypeTraits<T>::type != data_type) {
111 isc_throw(isc::dhcp::InvalidDataType,
112 "specified data type " << data_type << " does not"
113 " match the data type in an option definition");
114 }
115
116 OptionBuffer buf;
117 OptionDataTypeUtil::writeInt<T>(value, buf);
118 buffers_.push_back(buf);
119 }
120
124 void addArrayDataField(const std::string& value);
125
129 void addArrayDataField(const OpaqueDataTuple& value);
130
135 void addArrayDataField(const PrefixLen& prefix_len,
136 const asiolink::IOAddress& prefix);
137
142 void addArrayDataField(const PSIDLen& psid_len, const PSID& psid);
143
147 uint32_t getDataFieldsNum() const { return (buffers_.size()); }
148
155 asiolink::IOAddress readAddress(const uint32_t index = 0) const;
156
164 void writeAddress(const asiolink::IOAddress& address,
165 const uint32_t index = 0);
166
173 const OptionBuffer& readBinary(const uint32_t index = 0) const;
174
179 void writeBinary(const OptionBuffer& buf, const uint32_t index = 0);
180
187 std::string readTuple(const uint32_t index = 0) const;
188
195 void readTuple(OpaqueDataTuple& tuple, const uint32_t index = 0) const;
196
201 void writeTuple(const std::string& value, const uint32_t index = 0);
202
207 void writeTuple(const OpaqueDataTuple& value, const uint32_t index = 0);
208
215 bool readBoolean(const uint32_t index = 0) const;
216
223 void writeBoolean(const bool value, const uint32_t index = 0);
224
233 std::string readFqdn(const uint32_t index = 0) const;
234
241 void writeFqdn(const std::string& fqdn, const uint32_t index = 0);
242
251 template<typename T>
252 T readInteger(const uint32_t index = 0) const {
253 // Check that the index is not out of range.
254 checkIndex(index);
255 // Check that T points to a valid integer type and this type
256 // is consistent with an option definition.
257 checkDataType<T>(index);
258 // When we created the buffer we have checked that it has a
259 // valid size so this condition here should be always fulfilled.
260 isc_throw_assert(buffers_[index].size() == OptionDataTypeTraits<T>::len);
261 // Read an integer value.
262 return (OptionDataTypeUtil::readInt<T>(buffers_[index]));
263 }
264
273 template<typename T>
274 void writeInteger(const T value, const uint32_t index = 0) {
275 // Check that the index is not out of range.
276 checkIndex(index);
277 // Check that T points to a valid integer type and this type
278 // is consistent with an option definition.
279 checkDataType<T>(index);
280 // Get some temporary buffer.
281 OptionBuffer buf;
282 // Try to write to the buffer.
283 OptionDataTypeUtil::writeInt<T>(value, buf);
284 // If successful, replace the old buffer with new one.
285 std::swap(buffers_[index], buf);
286 }
287
294 PrefixTuple readPrefix(const uint32_t index = 0) const;
295
303 void writePrefix(const PrefixLen& prefix_len,
304 const asiolink::IOAddress& prefix,
305 const uint32_t index = 0);
306
313 PSIDTuple readPsid(const uint32_t index = 0) const;
314
324 void writePsid(const PSIDLen& psid_len, const PSID& psid,
325 const uint32_t index = 0);
326
333 std::string readString(const uint32_t index = 0) const;
334
339 void writeString(const std::string& text,
340 const uint32_t index = 0);
341
346 virtual void pack(isc::util::OutputBuffer& buf, bool check = true) const;
347
352 virtual void unpack(OptionBufferConstIter begin,
353 OptionBufferConstIter end);
354
360 virtual std::string toText(int indent = 0) const;
361
366 virtual uint16_t len() const;
367
374 void initialize(const OptionBufferConstIter first,
375 const OptionBufferConstIter last);
376
377private:
378
386 inline void checkArrayType() const {
387 if (!definition_.getArrayType()) {
388 isc_throw(InvalidOperation, "failed to add new array entry to an"
389 << " option. The option is not an array.");
390 }
391 }
392
403 template<typename T>
404 // cppcheck-suppress unusedPrivateFunction
405 void checkDataType(const uint32_t index) const;
406
412 void checkIndex(const uint32_t index) const;
413
418 void createBuffer(OptionBuffer& buffer,
419 const OptionDataType data_type) const;
420
422 void createBuffers();
423
433 size_t bufferLength(const OptionDataType data_type, bool in_array,
434 OptionBuffer::const_iterator begin,
435 OptionBuffer::const_iterator end) const;
436
440 void createBuffers(const OptionBuffer& data_buf);
441
448 std::string dataFieldToText(const OptionDataType data_type,
449 const uint32_t index) const;
450
454 using Option::setData;
455
457 OptionDefinition definition_;
458
462 std::vector<OptionBuffer> buffers_;
463};
464
466 typedef boost::shared_ptr<OptionCustom> OptionCustomPtr;
467
468template<typename T>
469void
470OptionCustom::checkDataType(const uint32_t index) const {
471 // Check that the requested return type is a supported integer.
472 if (!OptionDataTypeTraits<T>::integer_type) {
473 isc_throw(isc::dhcp::InvalidDataType, "specified data type"
474 " is not a supported integer type.");
475 }
476
477 // Get the option definition type.
478 OptionDataType data_type = definition_.getType();
479 if (data_type == OPT_RECORD_TYPE) {
480 const OptionDefinition::RecordFieldsCollection& record_fields =
481 definition_.getRecordFields();
482 if (definition_.getArrayType()) {
483 // If the array flag is set the last record field is an array.
484 if (index < record_fields.size()) {
485 // Get the data type to be returned.
486 data_type = record_fields[index];
487 } else {
488 // Get the data type to be returned from the last record field.
489 data_type = record_fields.back();
490 }
491 } else {
492 // When we initialized buffers we have already checked that
493 // the number of these buffers is equal to number of option
494 // fields in the record so the condition below should be met.
495 isc_throw_assert(index < record_fields.size());
496 // Get the data type to be returned.
497 data_type = record_fields[index];
498 }
499 }
500
501 if (OptionDataTypeTraits<T>::type != data_type) {
502 isc_throw(isc::dhcp::InvalidDataType,
503 "specified data type " << data_type << " does not"
504 " match the data type in an option definition for field"
505 " index " << index);
506 }
507}
508} // namespace isc::dhcp
509} // namespace isc
510
511#endif // OPTION_CUSTOM_H
isc::InvalidOperation
A generic exception that is thrown if a function is called in a prohibited way.
Definition: exceptions/exceptions.h:143
isc::asiolink::IOAddress
The IOAddress class represents an IP addresses (version agnostic)
Definition: io_address.h:45
isc::dhcp::InvalidDataType
Exception to be thrown when invalid type specified as template parameter.
Definition: option_data_types.h:23
isc::dhcp::OpaqueDataTuple
Represents a single instance of the opaque data preceded by length.
Definition: opaque_data_tuple.h:47
isc::dhcp::OptionCustom
Option with defined data fields represented as buffers that can be accessed using data field index.
Definition: option_custom.h:32
isc::dhcp::OptionCustom::readString
std::string readString(const uint32_t index=0) const
Read a buffer as string value.
Definition: option_custom.cc:638
isc::dhcp::OptionCustom::addArrayDataField
void addArrayDataField(const T value)
Create new buffer and store integer value in it.
Definition: option_custom.h:103
isc::dhcp::OptionCustom::readBoolean
bool readBoolean(const uint32_t index=0) const
Read a buffer as boolean value.
Definition: option_custom.cc:562
isc::dhcp::OptionCustom::len
virtual uint16_t len() const
Returns length of the complete option (data length + DHCPv4/DHCPv6 option header)
Definition: option_custom.cc:665
isc::dhcp::OptionCustom::readTuple
std::string readTuple(const uint32_t index=0) const
Read a buffer as length and string tuple.
Definition: option_custom.cc:529
isc::dhcp::OptionCustom::writeFqdn
void writeFqdn(const std::string &fqdn, const uint32_t index=0)
Write an FQDN into a buffer.
Definition: option_custom.cc:582
isc::dhcp::OptionCustom::readFqdn
std::string readFqdn(const uint32_t index=0) const
Read a buffer as FQDN.
Definition: option_custom.cc:576
isc::dhcp::OptionCustom::writePrefix
void writePrefix(const PrefixLen &prefix_len, const asiolink::IOAddress &prefix, const uint32_t index=0)
Write prefix length and value into a buffer.
Definition: option_custom.cc:605
isc::dhcp::OptionCustom::unpack
virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end)
Parses received buffer.
Definition: option_custom.cc:659
isc::dhcp::OptionCustom::writeAddress
void writeAddress(const asiolink::IOAddress &address, const uint32_t index=0)
Write an IP address into a buffer.
Definition: option_custom.cc:498
isc::dhcp::OptionCustom::pack
virtual void pack(isc::util::OutputBuffer &buf, bool check=true) const
Writes DHCP option in a wire format to a buffer.
Definition: option_custom.cc:458
isc::dhcp::OptionCustom::initialize
void initialize(const OptionBufferConstIter first, const OptionBufferConstIter last)
Sets content of this option from buffer.
Definition: option_custom.cc:685
isc::dhcp::OptionCustom::readBinary
const OptionBuffer & readBinary(const uint32_t index=0) const
Read a buffer as binary data.
Definition: option_custom.cc:516
isc::dhcp::OptionCustom::readPrefix
PrefixTuple readPrefix(const uint32_t index=0) const
Read a buffer as variable length prefix.
Definition: option_custom.cc:599
isc::dhcp::OptionCustom::writePsid
void writePsid(const PSIDLen &psid_len, const PSID &psid, const uint32_t index=0)
Write PSID length / value into a buffer.
Definition: option_custom.cc:625
isc::dhcp::OptionCustom::writeBoolean
void writeBoolean(const bool value, const uint32_t index=0)
Write a boolean value into a buffer.
Definition: option_custom.cc:568
isc::dhcp::OptionCustom::readAddress
asiolink::IOAddress readAddress(const uint32_t index=0) const
Read a buffer as IP address.
Definition: option_custom.cc:480
isc::dhcp::OptionCustom::readPsid
PSIDTuple readPsid(const uint32_t index=0) const
Read a buffer as a PSID length / value tuple.
Definition: option_custom.cc:619
isc::dhcp::OptionCustom::writeString
void writeString(const std::string &text, const uint32_t index=0)
Write a string value into a buffer.
Definition: option_custom.cc:644
isc::dhcp::OptionCustom::readInteger
T readInteger(const uint32_t index=0) const
Read a buffer as integer value.
Definition: option_custom.h:252
isc::dhcp::OptionCustom::writeBinary
void writeBinary(const OptionBuffer &buf, const uint32_t index=0)
Write binary data into a buffer.
Definition: option_custom.cc:522
isc::dhcp::OptionCustom::addArrayDataField
void addArrayDataField(const asiolink::IOAddress &address)
Create new buffer and set its value as an IP address.
Definition: option_custom.cc:52
isc::dhcp::OptionCustom::clone
virtual OptionPtr clone() const
Copies this option and returns a pointer to the copy.
Definition: option_custom.cc:47
isc::dhcp::OptionCustom::writeTuple
void writeTuple(const std::string &value, const uint32_t index=0)
Write a length and string tuple into a buffer.
Definition: option_custom.cc:544
isc::dhcp::OptionCustom::toText
virtual std::string toText(int indent=0) const
Returns string representation of the option.
Definition: option_custom.cc:694
isc::dhcp::OptionCustom::OptionCustom
OptionCustom(const OptionDefinition &def, Universe u)
Constructor, used for options to be sent.
Definition: option_custom.cc:19
isc::dhcp::OptionCustom::getDataFieldsNum
uint32_t getDataFieldsNum() const
Return a number of the data fields.
Definition: option_custom.h:147
isc::dhcp::OptionCustom::writeInteger
void writeInteger(const T value, const uint32_t index=0)
Write an integer value into a buffer.
Definition: option_custom.h:274
isc::dhcp::OptionDefinition
Base class representing a DHCP option definition.
Definition: option_definition.h:138
isc::dhcp::OptionDefinition::getType
OptionDataType getType() const
Return option data type.
Definition: option_definition.h:387
isc::dhcp::OptionDefinition::getRecordFields
const RecordFieldsCollection & getRecordFields() const
Return list of record fields.
Definition: option_definition.h:373
isc::dhcp::OptionDefinition::RecordFieldsCollection
std::vector< OptionDataType > RecordFieldsCollection
List of fields within the record.
Definition: option_definition.h:142
isc::dhcp::OptionDefinition::getArrayType
bool getArrayType() const
Return array type indicator.
Definition: option_definition.h:351
isc::dhcp::Option
Definition: option.h:74
isc::dhcp::Option::Universe
Universe
defines option universe DHCPv4 or DHCPv6
Definition: option.h:83
isc::dhcp::Option::setData
void setData(InputIterator first, InputIterator last)
Sets content of this option from buffer.
Definition: option.h:427
isc::dhcp::Option::check
void check() const
A protected method used for option correctness.
Definition: option.cc:90
isc::dhcp::PSIDLen
Encapsulates PSID length.
Definition: option_data_types.h:201
isc::dhcp::PSID
Encapsulates PSID value.
Definition: option_data_types.h:246
isc::dhcp::PrefixLen
Encapsulates prefix length.
Definition: option_data_types.h:277
isc::util::OutputBuffer
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition: buffer.h:294
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: exceptions/exceptions.h:210
io_address.h
io_utilities.h
isc_assert.h
isc_throw_assert
#define isc_throw_assert(expr)
Replacement for assert() that throws if the expression is false.
Definition: isc_assert.h:18
isc::dhcp::PSIDTuple
std::pair< PSIDLen, PSID > PSIDTuple
Defines a pair of PSID length / value.
Definition: option_data_types.h:274
isc::dhcp::OptionDataType
OptionDataType
Data types of DHCP option fields.
Definition: option_data_types.h:44
isc::dhcp::OPT_RECORD_TYPE
@ OPT_RECORD_TYPE
Definition: option_data_types.h:62
isc::dhcp::OptionCustomPtr
boost::shared_ptr< OptionCustom > OptionCustomPtr
A pointer to the OptionCustom object.
Definition: option_custom.h:466
isc::dhcp::OptionBufferConstIter
OptionBuffer::const_iterator OptionBufferConstIter
const_iterator for walking over OptionBuffer
Definition: option.h:30
isc::dhcp::PrefixTuple
std::pair< PrefixLen, asiolink::IOAddress > PrefixTuple
Defines a pair of prefix length / value.
Definition: option_data_types.h:315
isc::dhcp::OptionBuffer
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition: option.h:24
isc::dhcp::OptionPtr
boost::shared_ptr< Option > OptionPtr
Definition: option.h:36
isc
Defines the logger used by the top-level component of kea-lfc.
Definition: agent_parser.cc:143
option.h
option_definition.h
isc::dhcp::OptionDataTypeTraits
Trait class for data types supported in DHCP option definitions.
Definition: option_data_types.h:92