From: Piotrek Zadroga Date: Wed, 15 Mar 2023 22:34:15 +0000 (+0100) Subject: [#939] Refactor Length Field Type evaluation for tuples X-Git-Tag: Kea-2.3.6~39 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e73273efe2c2bff2249a9e2385882c23492fc20f;p=thirdparty%2Fkea.git [#939] Refactor Length Field Type evaluation for tuples --- diff --git a/src/lib/dhcp/option_custom.cc b/src/lib/dhcp/option_custom.cc index 301e883efb..b10aeb17b6 100644 --- a/src/lib/dhcp/option_custom.cc +++ b/src/lib/dhcp/option_custom.cc @@ -69,8 +69,7 @@ void OptionCustom::addArrayDataField(const std::string& value) { checkArrayType(); - OpaqueDataTuple::LengthFieldType lft = getUniverse() == Option::V4 ? - OpaqueDataTuple::LENGTH_1_BYTE : OpaqueDataTuple::LENGTH_2_BYTES; + OpaqueDataTuple::LengthFieldType lft = OptionDataTypeUtil::getTupleLenFieldType(getUniverse()); OptionBuffer buf; OptionDataTypeUtil::writeTuple(value, lft, buf); buffers_.push_back(buf); @@ -256,9 +255,7 @@ OptionCustom::bufferLength(const OptionDataType data_type, bool in_array, data_size = sizeof(uint8_t) + (prefix.first.asUint8() + 7) / 8; } else if (data_type == OPT_TUPLE_TYPE) { OpaqueDataTuple::LengthFieldType lft = - getUniverse() == Option::V4 ? - OpaqueDataTuple::LENGTH_1_BYTE : - OpaqueDataTuple::LENGTH_2_BYTES; + OptionDataTypeUtil::getTupleLenFieldType(getUniverse()); std::string value = OptionDataTypeUtil::readTuple(OptionBuffer(begin, end), lft); data_size = value.size(); @@ -528,8 +525,7 @@ OptionCustom::writeBinary(const OptionBuffer& buf, std::string OptionCustom::readTuple(const uint32_t index) const { checkIndex(index); - OpaqueDataTuple::LengthFieldType lft = getUniverse() == Option::V4 ? - OpaqueDataTuple::LENGTH_1_BYTE : OpaqueDataTuple::LENGTH_2_BYTES; + OpaqueDataTuple::LengthFieldType lft = OptionDataTypeUtil::getTupleLenFieldType(getUniverse()); return (OptionDataTypeUtil::readTuple(buffers_[index], lft)); } @@ -545,8 +541,7 @@ OptionCustom::writeTuple(const std::string& value, const uint32_t index) { checkIndex(index); buffers_[index].clear(); - OpaqueDataTuple::LengthFieldType lft = getUniverse() == Option::V4 ? - OpaqueDataTuple::LENGTH_1_BYTE : OpaqueDataTuple::LENGTH_2_BYTES; + OpaqueDataTuple::LengthFieldType lft = OptionDataTypeUtil::getTupleLenFieldType(getUniverse()); OptionDataTypeUtil::writeTuple(value, lft, buffers_[index]); } diff --git a/src/lib/dhcp/option_data_types.cc b/src/lib/dhcp/option_data_types.cc index 7d843bc7d6..31308b00d0 100644 --- a/src/lib/dhcp/option_data_types.cc +++ b/src/lib/dhcp/option_data_types.cc @@ -190,6 +190,14 @@ OptionDataTypeUtil::writeBinary(const std::string& hex_str, buf.insert(buf.end(), binary.begin(), binary.end()); } +OpaqueDataTuple::LengthFieldType +OptionDataTypeUtil::getTupleLenFieldType(Option::Universe u) { + if (u == Option::V4) { + return OpaqueDataTuple::LENGTH_1_BYTE; + } + return OpaqueDataTuple::LENGTH_2_BYTES; +} + std::string OptionDataTypeUtil::readTuple(const std::vector& buf, OpaqueDataTuple::LengthFieldType lengthfieldtype) { diff --git a/src/lib/dhcp/option_data_types.h b/src/lib/dhcp/option_data_types.h index 3b35dea5d5..7d506e1e67 100644 --- a/src/lib/dhcp/option_data_types.h +++ b/src/lib/dhcp/option_data_types.h @@ -419,6 +419,16 @@ public: static void writeTuple(const OpaqueDataTuple& tuple, std::vector& buf); + /// @brief Returns Length Field Type for a tuple. + /// + /// Returns Length Field Type for a tuple basing on the given + /// Option v4/v6 Universe. + /// + /// @param u specifies universe (V4 or V6) + /// @return By default 1 octet Length Field Type for V4 option + /// or 2 octets Length Field Type for V6 option + static OpaqueDataTuple::LengthFieldType getTupleLenFieldType(Option::Universe u); + /// @brief Read boolean value from a buffer. /// /// @param buf input buffer. diff --git a/src/lib/dhcp/option_definition.cc b/src/lib/dhcp/option_definition.cc index a9a6929eab..2dd86cbbe9 100644 --- a/src/lib/dhcp/option_definition.cc +++ b/src/lib/dhcp/option_definition.cc @@ -669,8 +669,12 @@ OptionDefinition::writeToBuffer(Option::Universe u, return; case OPT_TUPLE_TYPE: { - OpaqueDataTuple::LengthFieldType lft = u == Option::V4 ? - OpaqueDataTuple::LENGTH_1_BYTE : OpaqueDataTuple::LENGTH_2_BYTES; + OpaqueDataTuple::LengthFieldType lft; + if (getCode() == DHO_V4_SZTP_REDIRECT) { + lft = OpaqueDataTuple::LENGTH_2_BYTES; + } else { + lft = OptionDataTypeUtil::getTupleLenFieldType(u); + } OptionDataTypeUtil::writeTuple(value, lft, buf); return; } diff --git a/src/lib/dhcp/option_opaque_data_tuples.h b/src/lib/dhcp/option_opaque_data_tuples.h index 6e6f37cd1c..7d817359ab 100644 --- a/src/lib/dhcp/option_opaque_data_tuples.h +++ b/src/lib/dhcp/option_opaque_data_tuples.h @@ -13,6 +13,7 @@ #include #include #include +#include "option_data_types.h" #include namespace isc { @@ -174,8 +175,7 @@ private: if (prefLenFieldType_ != OpaqueDataTuple::LENGTH_EMPTY) { return (prefLenFieldType_); } - return (universe_ == Option::V6 ? OpaqueDataTuple::LENGTH_2_BYTES : - OpaqueDataTuple::LENGTH_1_BYTE); + return (OptionDataTypeUtil::getTupleLenFieldType(getUniverse())); } /// @brief Returns minimal length of the option for the given universe. diff --git a/src/lib/dhcp/option_vendor_class.cc b/src/lib/dhcp/option_vendor_class.cc index 4236f4c263..df4cf1cb34 100644 --- a/src/lib/dhcp/option_vendor_class.cc +++ b/src/lib/dhcp/option_vendor_class.cc @@ -70,7 +70,8 @@ OptionVendorClass::unpack(OptionBufferConstIter begin, size_t offset = 0; while (offset < std::distance(begin, end)) { // Parse a tuple. - OpaqueDataTuple tuple(getLengthFieldType(), begin + offset, end); + OpaqueDataTuple tuple(OptionDataTypeUtil::getTupleLenFieldType(getUniverse()), + begin + offset, end); addTuple(tuple); // The tuple has been parsed correctly which implies that it is safe to // advance the offset by its total length. @@ -105,7 +106,7 @@ OptionVendorClass::unpack(OptionBufferConstIter begin, void OptionVendorClass::addTuple(const OpaqueDataTuple& tuple) { - if (tuple.getLengthFieldType() != getLengthFieldType()) { + if (tuple.getLengthFieldType() != OptionDataTypeUtil::getTupleLenFieldType(getUniverse())) { isc_throw(isc::BadValue, "attempted to add opaque data tuple having" " invalid size of the length field " << tuple.getDataFieldSize() << " to Vendor Class option"); @@ -122,7 +123,7 @@ OptionVendorClass::setTuple(const size_t at, const OpaqueDataTuple& tuple) { " vendor option at position " << at << " which is out of" " range"); - } else if (tuple.getLengthFieldType() != getLengthFieldType()) { + } else if (tuple.getLengthFieldType() != OptionDataTypeUtil::getTupleLenFieldType(getUniverse())) { isc_throw(isc::BadValue, "attempted to set opaque data tuple having" " invalid size of the length field " << tuple.getDataFieldSize() << " to Vendor Class option"); diff --git a/src/lib/dhcp/option_vendor_class.h b/src/lib/dhcp/option_vendor_class.h index 6a56d26b90..71a73e9932 100644 --- a/src/lib/dhcp/option_vendor_class.h +++ b/src/lib/dhcp/option_vendor_class.h @@ -13,6 +13,7 @@ #include #include #include +#include "option_data_types.h" #include namespace isc { @@ -164,17 +165,6 @@ private: } } - /// @brief Returns the tuple length field type for the given universe. - /// - /// This function returns the length field type which should be used - /// for the opaque data tuples being added to this option. - /// - /// @return Tuple length field type for the universe this option belongs to. - OpaqueDataTuple::LengthFieldType getLengthFieldType() const { - return (getUniverse() == V4 ? OpaqueDataTuple::LENGTH_1_BYTE : - OpaqueDataTuple::LENGTH_2_BYTES); - } - /// @brief Returns minimal length of the option for the given universe. /// /// For DHCPv6, The Vendor Class option mandates a 2-byte