Kea 2.5.8
isc::dhcp::OptionDataTypeUtil Class Reference

Utility class for option data types. More...

#include <option_data_types.h>

Static Public Member Functions

static OptionDataType getDataType (const std::string &data_type)
 Return option data type from its name.
 
static int getDataTypeLen (const OptionDataType data_type)
 Get data type buffer length.
 
static const std::string & getDataTypeName (const OptionDataType data_type)
 Return option data type name from the data type enumerator.
 
static unsigned int getLabelCount (const std::string &text_name)
 Return the number of labels in the Name.
 
static OpaqueDataTuple::LengthFieldType getTupleLenFieldType (Option::Universe u)
 Returns Length Field Type for a tuple.
 
static asiolink::IOAddress readAddress (const std::vector< uint8_t > &buf, const short family)
 Read IPv4 or IPv6 address from a buffer.
 
static bool readBool (const std::vector< uint8_t > &buf)
 Read boolean value from a buffer.
 
static std::string readFqdn (const std::vector< uint8_t > &buf)
 Read FQDN from a buffer as a string value.
 
template<typename T >
static T readInt (const std::vector< uint8_t > &buf)
 Read integer value from a buffer.
 
static PrefixTuple readPrefix (const std::vector< uint8_t > &buf)
 Read prefix from a buffer.
 
static PSIDTuple readPsid (const std::vector< uint8_t > &buf)
 Read PSID length / value tuple from a buffer.
 
static std::string readString (const std::vector< uint8_t > &buf)
 Read string value from a buffer.
 
static void readTuple (const std::vector< uint8_t > &buf, OpaqueDataTuple &tuple)
 Read length and string tuple from a buffer.
 
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 void writeBinary (const std::string &hex_str, std::vector< uint8_t > &buf)
 Append hex-encoded binary values to a buffer.
 
static void writeBool (const bool value, std::vector< uint8_t > &buf)
 Append boolean value into a buffer.
 
static void writeFqdn (const std::string &fqdn, std::vector< uint8_t > &buf, const bool downcase=false)
 Append FQDN into a buffer.
 
template<typename T >
static void writeInt (const T value, std::vector< uint8_t > &buf)
 Append integer or unsigned integer value to 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 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 OpaqueDataTuple &tuple, std::vector< uint8_t > &buf)
 Append length and string tuple to 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.
 

Detailed Description

Utility class for option data types.

This class provides a set of utility functions to operate on supported DHCP option data types. It includes conversion between enumerator values representing data types and data type names. It also includes a set of functions that write data into option buffers and read data from option buffers. The data being written and read are converted from/to actual data types.

Note
This is a singleton class but it can be accessed via static methods only.

Definition at line 330 of file option_data_types.h.

Member Function Documentation

◆ getDataType()

OptionDataType isc::dhcp::OptionDataTypeUtil::getDataType ( const std::string &  data_type)
static

Return option data type from its name.

Parameters
data_typedata type name.
Returns
option data type.

Definition at line 83 of file option_data_types.cc.

Referenced by isc::dhcp::OptionDefinition::OptionDefinition(), and isc::dhcp::OptionDefinition::addRecordField().

◆ getDataTypeLen()

int isc::dhcp::OptionDataTypeUtil::getDataTypeLen ( const OptionDataType  data_type)
static

Get data type buffer length.

This function returns the size of a particular data type. Values returned by this function correspond to the data type sizes defined in OptionDataTypeTraits (IPV4_ADDRESS_TYPE and IPV6_ADDRESS_TYPE are exceptions here) so they rather indicate the fixed length of the data being written into the buffer, not the size of the particular data type. Thus for data types such as string, binary etc. for which the buffer length can't be determined this function returns 0. In addition, this function returns the data sizes for IPV4_ADDRESS_TYPE and IPV6_ADDRESS_TYPE as their buffer representations have fixed data lengths: 4 and 16 respectively.

Parameters
data_typedata type which size is to be returned.
Returns
data type size or zero for variable length types.

Definition at line 98 of file option_data_types.cc.

References isc::dhcp::OPT_BOOLEAN_TYPE, isc::dhcp::OPT_INT16_TYPE, isc::dhcp::OPT_INT32_TYPE, isc::dhcp::OPT_INT8_TYPE, isc::dhcp::OPT_IPV4_ADDRESS_TYPE, isc::dhcp::OPT_IPV6_ADDRESS_TYPE, isc::dhcp::OPT_PSID_TYPE, isc::dhcp::OPT_UINT16_TYPE, isc::dhcp::OPT_UINT32_TYPE, and isc::dhcp::OPT_UINT8_TYPE.

◆ getDataTypeName()

const std::string & isc::dhcp::OptionDataTypeUtil::getDataTypeName ( const OptionDataType  data_type)
static

Return option data type name from the data type enumerator.

Parameters
data_typeoption data type.
Returns
option data type name.

Definition at line 129 of file option_data_types.cc.

Referenced by isc::dhcp::OptionDefinition::addRecordField(), isc::dhcp::CfgOptionDef::toElementWithMetadata(), isc::dhcp::OptionInt< T >::toText(), and isc::dhcp::OptionIntArray< T >::toText().

◆ getLabelCount()

unsigned int isc::dhcp::OptionDataTypeUtil::getLabelCount ( const std::string &  text_name)
static

Return the number of labels in the Name.

If the specified name is empty the 0 is returned.

Parameters
text_nameA text representation of the name.
Returns
A number of labels in the provided name or 0 if the name string is empty.
Exceptions
isc::dhcp::BadDataTypeCastif provided name is malformed.

Definition at line 376 of file option_data_types.cc.

References isc::dns::Name::getLabelCount(), isc_throw, and isc::Exception::what().

+ Here is the call graph for this function:

◆ getTupleLenFieldType()

OpaqueDataTuple::LengthFieldType isc::dhcp::OptionDataTypeUtil::getTupleLenFieldType ( Option::Universe  u)
static

Returns Length Field Type for a tuple.

Returns Length Field Type for a tuple basing on the given Option v4/v6 Universe.

Parameters
uspecifies universe (V4 or V6)
Returns
By default 1 octet Length Field Type for V4 option or 2 octets Length Field Type for V6 option

Definition at line 197 of file option_data_types.cc.

References isc::dhcp::OpaqueDataTuple::LENGTH_1_BYTE, isc::dhcp::OpaqueDataTuple::LENGTH_2_BYTES, and isc::dhcp::Option::V4.

Referenced by isc::dhcp::OptionOpaqueDataTuples::OptionOpaqueDataTuples(), isc::dhcp::OptionCustom::addArrayDataField(), isc::dhcp::OptionVendorClass::addTuple(), isc::dhcp::OptionCustom::readTuple(), isc::dhcp::OptionVendorClass::setTuple(), isc::dhcp::OptionVendorClass::unpack(), isc::dhcp::DnrInstance::unpackAdn(), and isc::dhcp::OptionCustom::writeTuple().

◆ readAddress()

asiolink::IOAddress isc::dhcp::OptionDataTypeUtil::readAddress ( const std::vector< uint8_t > &  buf,
const short  family 
)
static

Read IPv4 or IPv6 address from a buffer.

Parameters
bufinput buffer.
familyaddress family: AF_INET or AF_INET6.
Exceptions
isc::dhcp::BadDataTypeCastwhen the data being read is truncated.
Returns
address being read.

Definition at line 150 of file option_data_types.cc.

References isc::asiolink::IOAddress::fromBytes(), and isc_throw.

Referenced by isc::dhcp::OptionCustom::readAddress().

+ Here is the call graph for this function:

◆ readBool()

bool isc::dhcp::OptionDataTypeUtil::readBool ( const std::vector< uint8_t > &  buf)
static

Read boolean value from a buffer.

Parameters
bufinput buffer.
Exceptions
isc::dhcp::BadDataTypeCastwhen the data being read is truncated or the value is invalid (neither 1 nor 0).
Returns
boolean value read from a buffer.

Definition at line 317 of file option_data_types.cc.

References isc_throw.

Referenced by isc::dhcp::OptionCustom::readBoolean().

◆ readFqdn()

std::string isc::dhcp::OptionDataTypeUtil::readFqdn ( const std::vector< uint8_t > &  buf)
static

Read FQDN from a buffer as a string value.

The format of an FQDN within a buffer complies with RFC1035, section 3.1.

Parameters
bufinput buffer holding a FQDN.
Exceptions
BadDataTypeCastif a FQDN stored within a buffer is invalid (e.g. empty, contains invalid characters, truncated).
Returns
fully qualified domain name in a text form.

Definition at line 338 of file option_data_types.cc.

References isc_throw, isc::dns::Name::toText(), and isc::Exception::what().

Referenced by isc::dhcp::OptionCustom::readFqdn().

+ Here is the call graph for this function:

◆ readInt()

template<typename T >
static T isc::dhcp::OptionDataTypeUtil::readInt ( const std::vector< uint8_t > &  buf)
inlinestatic

Read integer value from a buffer.

Parameters
bufinput buffer.
Template Parameters
integertype of the returned value.
Exceptions
isc::dhcp::BadDataTypeCastwhen the data in the buffer is truncated.
Returns
integer value being read.

Definition at line 461 of file option_data_types.h.

References isc_throw, isc::util::readUint16(), and isc::util::readUint32().

+ Here is the call graph for this function:

◆ readPrefix()

PrefixTuple isc::dhcp::OptionDataTypeUtil::readPrefix ( const std::vector< uint8_t > &  buf)
static

Read prefix from a buffer.

This method reads prefix length and a prefix value from a buffer. The prefix value has variable length and this length is determined from the first byte of the buffer. If the length is not divisible by 8, the prefix is padded with zeros to the next byte boundary.

Parameters
bufinput buffer holding a prefix length / prefix tuple.
Returns
Prefix length and value.

Definition at line 394 of file option_data_types.cc.

References isc::dhcp::PrefixLen::asUint8(), isc::dhcp::PrefixLen::asUnsigned(), isc::asiolink::IOAddress::fromBytes(), isc::asiolink::IOAddress::IPV6_ZERO_ADDRESS(), isc_throw, and isc::Exception::what().

Referenced by isc::dhcp::OptionCustom::readPrefix().

+ Here is the call graph for this function:

◆ readPsid()

PSIDTuple isc::dhcp::OptionDataTypeUtil::readPsid ( const std::vector< uint8_t > &  buf)
static

Read PSID length / value tuple from a buffer.

This method reads three bytes from a buffer. The first byte holds a PSID length value. The remaining two bytes contain a zero padded PSID value.

Returns
PSID length / value tuple.
Exceptions
isc::dhcp::BadDataTypeCastif PSID length or value held in the buffer is incorrect or the buffer is truncated.

Definition at line 528 of file option_data_types.cc.

References isc_throw, and isc::util::readUint16().

Referenced by isc::dhcp::OptionCustom::readPsid().

+ Here is the call graph for this function:

◆ readString()

std::string isc::dhcp::OptionDataTypeUtil::readString ( const std::vector< uint8_t > &  buf)
static

Read string value from a buffer.

To be compliant with RFC 2132, Sec. 2, trailing NULLs are trimmed.

Parameters
bufinput buffer.
Returns
string value being read.
Exceptions
isc::dhcp::OutOfRangeis the payload contains only NULLs.

Definition at line 594 of file option_data_types.cc.

References isc_throw, and isc::util::str::seekTrimmed().

Referenced by isc::dhcp::OptionCustom::readString().

+ Here is the call graph for this function:

◆ readTuple() [1/2]

void isc::dhcp::OptionDataTypeUtil::readTuple ( const std::vector< uint8_t > &  buf,
OpaqueDataTuple tuple 
)
static

Read length and string tuple from a buffer.

Parameters
bufinput buffer.
tuplereference of the tuple to read into
Exceptions
isc::dhcp::BadDataTypeCastwhen the data being read is truncated.

Definition at line 247 of file option_data_types.cc.

References isc_throw, isc::dhcp::OpaqueDataTuple::unpack(), and isc::Exception::what().

+ Here is the call graph for this function:

◆ readTuple() [2/2]

std::string isc::dhcp::OptionDataTypeUtil::readTuple ( const std::vector< uint8_t > &  buf,
OpaqueDataTuple::LengthFieldType  lengthfieldtype 
)
static

Read length and string tuple from a buffer.

Parameters
bufinput buffer.
lengthfieldtypeLENGTH_1_BYTE (DHCPv4) or LENGTH_2_BYTES (DHCPv6)
Exceptions
isc::dhcp::BadDataTypeCastwhen the data being read is truncated.
Returns
string being read.

Definition at line 205 of file option_data_types.cc.

References isc_throw, isc::dhcp::OpaqueDataTuple::LENGTH_1_BYTE, isc::dhcp::OpaqueDataTuple::LENGTH_2_BYTES, and isc::util::readUint16().

Referenced by isc::dhcp::OptionCustom::readTuple().

+ Here is the call graph for this function:

◆ writeAddress()

void isc::dhcp::OptionDataTypeUtil::writeAddress ( const asiolink::IOAddress address,
std::vector< uint8_t > &  buf 
)
static

Append IPv4 or IPv6 address to a buffer.

Parameters
addressIPv4 or IPv6 address.
[out]bufoutput buffer.

Definition at line 172 of file option_data_types.cc.

References isc::asiolink::IOAddress::toBytes().

Referenced by isc::dhcp::OptionCustom::addArrayDataField(), and isc::dhcp::OptionCustom::writeAddress().

+ Here is the call graph for this function:

◆ writeBinary()

void isc::dhcp::OptionDataTypeUtil::writeBinary ( const std::string &  hex_str,
std::vector< uint8_t > &  buf 
)
static

Append hex-encoded binary values to a buffer.

Parameters
hex_strstring representing a binary value encoded with hexadecimal digits (without 0x prefix).
[out]bufoutput buffer.

Definition at line 179 of file option_data_types.cc.

References isc::util::encode::decodeHex(), isc_throw, and isc::Exception::what().

+ Here is the call graph for this function:

◆ writeBool()

void isc::dhcp::OptionDataTypeUtil::writeBool ( const bool  value,
std::vector< uint8_t > &  buf 
)
static

Append boolean value into a buffer.

The bool value is encoded in a buffer in such a way that "1" means "true" and "0" means "false".

Parameters
valueboolean value to be written.
[out]bufoutput buffer.

Definition at line 332 of file option_data_types.cc.

Referenced by isc::dhcp::OptionCustom::addArrayDataField(), and isc::dhcp::OptionCustom::writeBoolean().

◆ writeFqdn()

void isc::dhcp::OptionDataTypeUtil::writeFqdn ( const std::string &  fqdn,
std::vector< uint8_t > &  buf,
const bool  downcase = false 
)
static

Append FQDN into a buffer.

This method appends the Fully Qualified Domain Name (FQDN) represented as string value into a buffer. The format of the FQDN being stored into a buffer complies with RFC1035, section 3.1.

Parameters
fqdnfully qualified domain name to be written.
[out]bufoutput buffer.
downcaseindicates if the FQDN should be converted to lower case (if true). By default it is not converted.
Exceptions
isc::dhcp::BadDataTypeCastif provided FQDN is invalid.

Definition at line 359 of file option_data_types.cc.

References isc::dns::LabelSequence::getData(), isc::dns::LabelSequence::getDataLength(), isc_throw, and isc::Exception::what().

Referenced by isc::dhcp::OptionCustom::writeFqdn().

+ Here is the call graph for this function:

◆ writeInt()

template<typename T >
static void isc::dhcp::OptionDataTypeUtil::writeInt ( const T  value,
std::vector< uint8_t > &  buf 
)
inlinestatic

Append integer or unsigned integer value to a buffer.

Parameters
valuean integer value to be written into a buffer.
[out]bufoutput buffer.
Template Parameters
datatype of the value.

Definition at line 503 of file option_data_types.h.

References isc_throw, isc::util::writeUint16(), and isc::util::writeUint32().

+ Here is the call graph for this function:

◆ writePrefix()

void isc::dhcp::OptionDataTypeUtil::writePrefix ( const PrefixLen prefix_len,
const asiolink::IOAddress prefix,
std::vector< uint8_t > &  buf 
)
static

Append prefix into a buffer.

This method writes prefix length (1 byte) followed by a variable length prefix.

Parameters
prefix_lenPrefix length in bits (0 to 128).
prefixPrefix value.
[out]bufOutput buffer.

Definition at line 490 of file option_data_types.cc.

References isc::dhcp::PrefixLen::asUint8(), isc_throw, isc::asiolink::IOAddress::isV6(), and isc::asiolink::IOAddress::toBytes().

Referenced by isc::dhcp::OptionCustom::addArrayDataField(), and isc::dhcp::OptionCustom::writePrefix().

+ Here is the call graph for this function:

◆ writePsid()

void isc::dhcp::OptionDataTypeUtil::writePsid ( const PSIDLen psid_len,
const PSID psid,
std::vector< uint8_t > &  buf 
)
static

Append PSID length/value into a buffer.

This method appends 1 byte of PSID length and 2 bytes of PSID value into a buffer. The PSID value contains a PSID length number of significant bits, followed by 16 - PSID length zero bits.

Parameters
psid_lenPSID length in the range of 0 to 16 holding the number of significant bits within the PSID value.
psidPSID value, where the lowest value is 0, and the highest value is 2^(PSID length)-1.
[out]bufoutput buffer.
Exceptions
isc::dhcp::BadDataTypeCastif specified psid_len or psid value is incorrect.

Definition at line 571 of file option_data_types.cc.

References isc::dhcp::PSID::asUint16(), isc::dhcp::PSIDLen::asUint8(), isc::dhcp::PSIDLen::asUnsigned(), isc_throw, and isc::util::writeUint16().

Referenced by isc::dhcp::OptionCustom::addArrayDataField(), and isc::dhcp::OptionCustom::writePsid().

+ Here is the call graph for this function:

◆ writeString()

void isc::dhcp::OptionDataTypeUtil::writeString ( const std::string &  value,
std::vector< uint8_t > &  buf 
)
static

Write UTF8-encoded string into a buffer.

Parameters
valuestring value to be written into a buffer.
[out]bufoutput buffer.

Definition at line 612 of file option_data_types.cc.

Referenced by isc::dhcp::OptionCustom::writeString().

◆ writeTuple() [1/2]

void isc::dhcp::OptionDataTypeUtil::writeTuple ( const OpaqueDataTuple tuple,
std::vector< uint8_t > &  buf 
)
static

Append length and string tuple to a buffer.

Parameters
tuplelength and string tuple
[out]bufoutput buffer.

Definition at line 286 of file option_data_types.cc.

References isc::dhcp::OpaqueDataTuple::getData(), isc::dhcp::OpaqueDataTuple::getLength(), isc::dhcp::OpaqueDataTuple::getLengthFieldType(), isc_throw, isc::dhcp::OpaqueDataTuple::LENGTH_1_BYTE, isc::dhcp::OpaqueDataTuple::LENGTH_2_BYTES, and isc::util::writeUint16().

+ Here is the call graph for this function:

◆ writeTuple() [2/2]

void isc::dhcp::OptionDataTypeUtil::writeTuple ( const std::string &  value,
OpaqueDataTuple::LengthFieldType  lengthfieldtype,
std::vector< uint8_t > &  buf 
)
static

Append length and string tuple to a buffer.

Parameters
valuelength and string tuple
lengthfieldtypeLENGTH_1_BYTE (DHCPv4) or LENGTH_2_BYTES (DHCPv6)
[out]bufoutput buffer.

Definition at line 257 of file option_data_types.cc.

References isc_throw, isc::dhcp::OpaqueDataTuple::LENGTH_1_BYTE, isc::dhcp::OpaqueDataTuple::LENGTH_2_BYTES, and isc::util::writeUint16().

Referenced by isc::dhcp::OptionCustom::addArrayDataField(), and isc::dhcp::OptionCustom::writeTuple().

+ Here is the call graph for this function:

The documentation for this class was generated from the following files: