Kea 2.7.1
option_data_types.h
Go to the documentation of this file.
1// Copyright (C) 2012-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#ifndef OPTION_DATA_TYPES_H
8#define OPTION_DATA_TYPES_H
9
10#include <asiolink/io_address.h>
12#include <dhcp/option.h>
14#include <util/io.h>
15
16#include <stdint.h>
17#include <utility>
18
19namespace isc {
20namespace dhcp {
21
23class InvalidDataType : public Exception {
24public:
25 InvalidDataType(const char* file, size_t line, const char* what) :
26 isc::Exception(file, line, what) { };
27};
28
30class BadDataTypeCast : public Exception {
31public:
32 BadDataTypeCast(const char* file, size_t line, const char* what) :
33 isc::Exception(file, line, what) { };
34};
35
68
71 const char* name; // option name
72 uint16_t code; // option code
73 const char* space; // option space
74 OptionDataType type; // data type
75 bool array; // is array
76 const OptionDataType* records; // record fields
77 size_t records_size; // number of fields in a record
78 const char* encapsulates; // option space encapsulated by the
79 // particular option.
80};
81
84 const struct OptionDefParams* optionDefParams; // parameters structure
85 const int size; // structure size
86 const char* space; // option space
87};
88
94template<typename T>
96 static const bool valid = false;
97 static const int len = 0;
98 static const bool integer_type = false;
100};
101
103template<>
105 static const bool valid = true;
106 static const int len = 0;
107 static const bool integer_type = false;
109};
110
112template<>
114 static const bool valid = true;
115 static const int len = sizeof(uint8_t);
116 static const bool integer_type = false;
118};
119
121template<>
122struct OptionDataTypeTraits<int8_t> {
123 static const bool valid = true;
124 static const int len = 1;
125 static const bool integer_type = true;
127};
128
130template<>
131struct OptionDataTypeTraits<int16_t> {
132 static const bool valid = true;
133 static const int len = 2;
134 static const bool integer_type = true;
136};
137
139template<>
140struct OptionDataTypeTraits<int32_t> {
141 static const bool valid = true;
142 static const int len = 4;
143 static const bool integer_type = true;
145};
146
148template<>
149struct OptionDataTypeTraits<uint8_t> {
150 static const bool valid = true;
151 static const int len = 1;
152 static const bool integer_type = true;
154};
155
157template<>
158struct OptionDataTypeTraits<uint16_t> {
159 static const bool valid = true;
160 static const int len = 2;
161 static const bool integer_type = true;
163};
164
166template<>
167struct OptionDataTypeTraits<uint32_t> {
168 static const bool valid = true;
169 static const int len = 4;
170 static const bool integer_type = true;
172};
173
175template<>
177 static const bool valid = true;
178 // The len value is used to determine the size of the data
179 // to be written to an option buffer. IOAddress object may
180 // either represent an IPv4 or IPv6 addresses which have
181 // different lengths. Thus we can't put fixed value here.
182 // The length of a data to be written into an option buffer
183 // have to be determined in the runtime for a particular
184 // IOAddress object. Thus setting len to zero.
185 static const int len = 0;
186 static const bool integer_type = false;
188};
189
191template<>
192struct OptionDataTypeTraits<std::string> {
193 static const bool valid = true;
194 // The len value is used to determine the size of the data
195 // to be written to an option buffer. For strings this
196 // size is unknown until we actually deal with the particular
197 // string to be written. Thus setting it to zero.
198 static const int len = 0;
199 static const bool integer_type = false;
201};
202
204class PSIDLen {
205public:
206
208 PSIDLen() : psid_len_(0) { }
209
217 explicit PSIDLen(const uint8_t psid_len)
218 : psid_len_(psid_len) {
219 if (psid_len_ > sizeof(uint16_t) * 8) {
220 isc_throw(isc::OutOfRange, "invalid value "
221 << asUnsigned() << " of PSID length");
222 }
223 }
224
226 uint8_t asUint8() const {
227 return (psid_len_);
228 }
229
238 unsigned int asUnsigned() const {
239 return (static_cast<unsigned>(psid_len_));
240 }
241
242private:
243
245 uint8_t psid_len_;
246};
247
249class PSID {
250public:
251
253 PSID() : psid_(0) { }
254
260 explicit PSID(const uint16_t psid)
261 : psid_(psid) {
262 }
263
265 uint16_t asUint16() const {
266 return (psid_);
267 }
268
269private:
270
272 uint16_t psid_;
273
274};
275
277typedef std::pair<PSIDLen, PSID> PSIDTuple;
278
281public:
282
284 PrefixLen() : prefix_len_(0) { }
285
293 explicit PrefixLen(const uint8_t prefix_len)
294 : prefix_len_(prefix_len) {
295 }
296
298 uint8_t asUint8() const {
299 return (prefix_len_);
300 }
301
307 unsigned int asUnsigned() const {
308 return (static_cast<unsigned>(prefix_len_));
309 }
310
311private:
312
314 uint8_t prefix_len_;
315};
316
318typedef std::pair<PrefixLen, asiolink::IOAddress> PrefixTuple;
319
332public:
333
338 static OptionDataType getDataType(const std::string& data_type);
339
344 static const std::string& getDataTypeName(const OptionDataType data_type);
345
362 static int getDataTypeLen(const OptionDataType data_type);
363
372 static asiolink::IOAddress readAddress(const std::vector<uint8_t>& buf,
373 const short family);
374
379 static void writeAddress(const asiolink::IOAddress& address,
380 std::vector<uint8_t>& buf);
381
387 static void writeBinary(const std::string& hex_str,
388 std::vector<uint8_t>& buf);
389
397 static std::string readTuple(const std::vector<uint8_t>& buf,
398 OpaqueDataTuple::LengthFieldType lengthfieldtype);
399
406 static void readTuple(const std::vector<uint8_t>& buf,
407 OpaqueDataTuple& tuple);
408
414 static void writeTuple(const std::string& value,
415 OpaqueDataTuple::LengthFieldType lengthfieldtype,
416 std::vector<uint8_t>& buf);
417
422 static void writeTuple(const OpaqueDataTuple& tuple,
423 std::vector<uint8_t>& buf);
424
434
442 static bool readBool(const std::vector<uint8_t>& buf);
443
451 static void writeBool(const bool value, std::vector<uint8_t>& buf);
452
461 template<typename T>
462 static T readInt(const std::vector<uint8_t>& buf) {
464 isc_throw(isc::dhcp::InvalidDataType, "specified data type to be returned"
465 " by readInteger is unsupported integer type");
466 }
467
468 if (buf.size() < OptionDataTypeTraits<T>::len) {
470 "failed to read an integer value from a buffer"
471 << " - buffer is truncated.");
472 }
473
474 T value;
476 case 1:
477 value = *(buf.begin());
478 break;
479 case 2:
480 // Calling readUint16 works either for unsigned
481 // or signed types.
482 value = isc::util::readUint16(&(*buf.begin()), buf.size());
483 break;
484 case 4:
485 // Calling readUint32 works either for unsigned
486 // or signed types.
487 value = isc::util::readUint32(&(*buf.begin()), buf.size());
488 break;
489 default:
490 // This should not happen because we made checks on data types
491 // but it does not hurt to keep throw statement here.
493 "invalid size of the data type to be read as integer.");
494 }
495 return (value);
496 }
497
503 template<typename T>
504 static void writeInt(const T value,
505 std::vector<uint8_t>& buf) {
507 isc_throw(InvalidDataType, "provided data type is not the supported.");
508 }
510 case 1:
511 buf.push_back(static_cast<uint8_t>(value));
512 break;
513 case 2:
514 buf.resize(buf.size() + 2);
515 isc::util::writeUint16(static_cast<uint16_t>(value), &buf[buf.size() - 2], 2);
516 break;
517 case 4:
518 buf.resize(buf.size() + 4);
519 isc::util::writeUint32(static_cast<uint32_t>(value), &buf[buf.size() - 4], 4);
520 break;
521 default:
522 // The cases above cover whole range of possible data lengths because
523 // we check at the beginning of this function that given data type is
524 // a supported integer type which can be only 1,2 or 4 bytes long.
525 ;
526 }
527 }
528
539 static std::string readFqdn(const std::vector<uint8_t>& buf);
540
555 static void writeFqdn(const std::string& fqdn,
556 std::vector<uint8_t>& buf,
557 const bool downcase = false);
558
568 static unsigned int getLabelCount(const std::string& text_name);
569
580 static PrefixTuple readPrefix(const std::vector<uint8_t>& buf);
581
590 static void writePrefix(const PrefixLen& prefix_len,
591 const asiolink::IOAddress& prefix,
592 std::vector<uint8_t>& buf);
593
603 static PSIDTuple readPsid(const std::vector<uint8_t>& buf);
604
620 static void writePsid(const PSIDLen& psid_len, const PSID& psid,
621 std::vector<uint8_t>& buf);
622
630 static std::string readString(const std::vector<uint8_t>& buf);
631
636 static void writeString(const std::string& value,
637 std::vector<uint8_t>& buf);
638private:
639
642 std::map<std::string, OptionDataType> data_types_;
643
646 std::map<OptionDataType, std::string> data_type_names_;
647
653
662 static OptionDataTypeUtil& instance();
663
668 OptionDataType getDataTypeImpl(const std::string& data_type) const;
669
674 const std::string& getDataTypeNameImpl(const OptionDataType data_type) const;
675};
676
677
678} // isc::dhcp namespace
679} // isc namespace
680
681#endif // OPTION_DATA_TYPES_H
This is a base class for exceptions thrown from the DNS library module.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
Exception to be thrown when cast to the data type was unsuccessful.
BadDataTypeCast(const char *file, size_t line, const char *what)
Exception to be thrown when invalid type specified as template parameter.
InvalidDataType(const char *file, size_t line, const char *what)
Represents a single instance of the opaque data preceded by length.
LengthFieldType
Size of the length field in the tuple.
Utility class for option data types.
static PrefixTuple readPrefix(const std::vector< uint8_t > &buf)
Read prefix from a buffer.
static asiolink::IOAddress readAddress(const std::vector< uint8_t > &buf, const short family)
Read IPv4 or IPv6 address from a buffer.
static unsigned int getLabelCount(const std::string &text_name)
Return the number of labels in the Name.
static void writeFqdn(const std::string &fqdn, std::vector< uint8_t > &buf, const bool downcase=false)
Append FQDN into a buffer.
static void writePrefix(const PrefixLen &prefix_len, const asiolink::IOAddress &prefix, std::vector< uint8_t > &buf)
Append prefix into a buffer.
static void writeInt(const T value, std::vector< uint8_t > &buf)
Append integer or unsigned integer value to a buffer.
static const std::string & getDataTypeName(const OptionDataType data_type)
Return option data type name from the data type enumerator.
static OptionDataType getDataType(const std::string &data_type)
Return option data type from its name.
static void writeBinary(const std::string &hex_str, std::vector< uint8_t > &buf)
Append hex-encoded binary values to a buffer.
static int getDataTypeLen(const OptionDataType data_type)
Get data type buffer length.
static T readInt(const std::vector< uint8_t > &buf)
Read integer value from a buffer.
static std::string readFqdn(const std::vector< uint8_t > &buf)
Read FQDN from a buffer as a string value.
static std::string readTuple(const std::vector< uint8_t > &buf, OpaqueDataTuple::LengthFieldType lengthfieldtype)
Read length and string tuple from a buffer.
static void writeAddress(const asiolink::IOAddress &address, std::vector< uint8_t > &buf)
Append IPv4 or IPv6 address to a buffer.
static PSIDTuple readPsid(const std::vector< uint8_t > &buf)
Read PSID length / value tuple from a buffer.
static void writePsid(const PSIDLen &psid_len, const PSID &psid, std::vector< uint8_t > &buf)
Append PSID length/value into a buffer.
static void writeString(const std::string &value, std::vector< uint8_t > &buf)
Write UTF8-encoded string into a buffer.
static void writeTuple(const std::string &value, OpaqueDataTuple::LengthFieldType lengthfieldtype, std::vector< uint8_t > &buf)
Append length and string tuple to a buffer.
static OpaqueDataTuple::LengthFieldType getTupleLenFieldType(Option::Universe u)
Returns Length Field Type for a tuple.
static void writeBool(const bool value, std::vector< uint8_t > &buf)
Append boolean value into a buffer.
static bool readBool(const std::vector< uint8_t > &buf)
Read boolean value from a buffer.
static std::string readString(const std::vector< uint8_t > &buf)
Read string value from a buffer.
Universe
defines option universe DHCPv4 or DHCPv6
Definition option.h:83
Encapsulates PSID length.
PSIDLen()
Default constructor.
uint8_t asUint8() const
Returns PSID length as uint8_t value.
unsigned int asUnsigned() const
Returns PSID length as unsigned int.
PSIDLen(const uint8_t psid_len)
Constructor.
Encapsulates PSID value.
uint16_t asUint16() const
Returns PSID value as a number.
PSID(const uint16_t psid)
Constructor.
PSID()
Default constructor.
Encapsulates prefix length.
PrefixLen(const uint8_t prefix_len)
Constructor.
unsigned int asUnsigned() const
Returns prefix length as unsigned int.
PrefixLen()
Default constructor.
uint8_t asUint8() const
Returns prefix length as uint8_t value.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
std::pair< PSIDLen, PSID > PSIDTuple
Defines a pair of PSID length / value.
OptionDataType
Data types of DHCP option fields.
std::pair< PrefixLen, asiolink::IOAddress > PrefixTuple
Defines a pair of prefix length / value.
std::vector< uint8_t > OptionBuffer
buffer types used in DHCP code.
Definition option.h:24
uint8_t * writeUint32(uint32_t const value, void *const buffer, size_t const length)
uint32_t wrapper over writeUint.
Definition io.h:100
uint16_t readUint16(void const *const buffer, size_t const length)
uint16_t wrapper over readUint.
Definition io.h:76
uint8_t * writeUint16(uint16_t const value, void *const buffer, size_t const length)
uint16_t wrapper over writeUint.
Definition io.h:94
uint32_t readUint32(void const *const buffer, size_t const length)
uint32_t wrapper over readUint.
Definition io.h:82
Defines the logger used by the top-level component of kea-lfc.
Trait class for data types supported in DHCP option definitions.
static const OptionDataType type
Encapsulation of option definition parameters and the structure size.
const struct OptionDefParams * optionDefParams
Parameters being used to make up an option definition.
const OptionDataType * records