[dataset] relocate IsTlvValid() to Dataset (#10060)

This commit relocates `IsTlvValid()` to the `Dataset` class,
emphasizing its specificity for validating Dataset TLVs and not other
MeshCop TLVs.
This commit is contained in:
Abtin Keshavarzian
2024-04-25 08:26:45 -07:00
committed by GitHub
parent 79a6a85fed
commit 9a9853bb7c
5 changed files with 67 additions and 64 deletions
+52 -2
View File
@@ -172,14 +172,64 @@ bool Dataset::IsValid(void) const
for (const Tlv *cur = GetTlvsStart(); cur < end; cur = cur->GetNext())
{
VerifyOrExit(!cur->IsExtended() && (cur + 1) <= end && cur->GetNext() <= end && Tlv::IsValid(*cur),
rval = false);
VerifyOrExit(!cur->IsExtended() && (cur + 1) <= end && cur->GetNext() <= end && IsTlvValid(*cur), rval = false);
}
exit:
return rval;
}
bool Dataset::IsTlvValid(const Tlv &aTlv)
{
bool isValid = true;
uint8_t minLength = 0;
switch (aTlv.GetType())
{
case Tlv::kPanId:
minLength = sizeof(PanIdTlv::UintValueType);
break;
case Tlv::kExtendedPanId:
minLength = sizeof(ExtendedPanIdTlv::ValueType);
break;
case Tlv::kPskc:
minLength = sizeof(PskcTlv::ValueType);
break;
case Tlv::kNetworkKey:
minLength = sizeof(NetworkKeyTlv::ValueType);
break;
case Tlv::kMeshLocalPrefix:
minLength = sizeof(MeshLocalPrefixTlv::ValueType);
break;
case Tlv::kChannel:
VerifyOrExit(aTlv.GetLength() >= sizeof(ChannelTlvValue), isValid = false);
isValid = aTlv.ReadValueAs<ChannelTlv>().IsValid();
break;
case Tlv::kNetworkName:
isValid = As<NetworkNameTlv>(aTlv).IsValid();
break;
case Tlv::kSecurityPolicy:
isValid = As<SecurityPolicyTlv>(aTlv).IsValid();
break;
case Tlv::kChannelMask:
isValid = As<ChannelMaskTlv>(aTlv).IsValid();
break;
default:
break;
}
if (minLength > 0)
{
isValid = (aTlv.GetLength() >= minLength);
}
exit:
return isValid;
}
const Tlv *Dataset::FindTlv(Tlv::Type aType) const { return As<Tlv>(Tlv::FindTlv(mTlvs, mLength, aType)); }
void Dataset::ConvertTo(Info &aDatasetInfo) const
+14
View File
@@ -252,6 +252,20 @@ public:
*/
bool IsValid(void) const;
/**
* Validates the format and value of a given MeshCoP TLV used in Dataset.
*
* TLV types that can appear in an Active or Pending Operational Dataset are validated. Other TLV types including
* unknown TLV types are considered as valid.
*
* @param[in] aTlv The TLV to validate.
*
* @retval TRUE The TLV format and value is valid, or TLV type is unknown (not supported in Dataset).
* @retval FALSE The TLV format or value is invalid.
*
*/
static bool IsTlvValid(const Tlv &aTlv);
/**
* Indicates whether or not a given TLV type is present in the Dataset.
*
+1 -1
View File
@@ -291,7 +291,7 @@ Error DatasetManager::DatasetTlv::ReadFromMessage(const Message &aMessage, uint1
SuccessOrExit(error = aMessage.Read(aOffset, this, sizeof(Tlv)));
VerifyOrExit(GetLength() <= Dataset::kMaxValueSize, error = kErrorParse);
SuccessOrExit(error = aMessage.Read(aOffset + sizeof(Tlv), mValue, GetLength()));
VerifyOrExit(Tlv::IsValid(*this), error = kErrorParse);
VerifyOrExit(Dataset::IsTlvValid(*this), error = kErrorParse);
exit:
return error;
-51
View File
@@ -43,57 +43,6 @@
namespace ot {
namespace MeshCoP {
bool Tlv::IsValid(const Tlv &aTlv)
{
bool isValid = true;
uint8_t minLength = 0;
switch (aTlv.GetType())
{
case Tlv::kPanId:
minLength = sizeof(PanIdTlv::UintValueType);
break;
case Tlv::kExtendedPanId:
minLength = sizeof(ExtendedPanIdTlv::ValueType);
break;
case Tlv::kPskc:
minLength = sizeof(PskcTlv::ValueType);
break;
case Tlv::kNetworkKey:
minLength = sizeof(NetworkKeyTlv::ValueType);
break;
case Tlv::kMeshLocalPrefix:
minLength = sizeof(MeshLocalPrefixTlv::ValueType);
break;
case Tlv::kChannel:
VerifyOrExit(aTlv.GetLength() >= sizeof(ChannelTlvValue), isValid = false);
isValid = aTlv.ReadValueAs<ChannelTlv>().IsValid();
break;
case Tlv::kNetworkName:
isValid = As<NetworkNameTlv>(aTlv).IsValid();
break;
case Tlv::kSecurityPolicy:
isValid = As<SecurityPolicyTlv>(aTlv).IsValid();
break;
case Tlv::kChannelMask:
isValid = As<ChannelMaskTlv>(aTlv).IsValid();
break;
default:
break;
}
if (minLength > 0)
{
isValid = (aTlv.GetLength() >= minLength);
}
exit:
return isValid;
}
NameData NetworkNameTlv::GetNetworkName(void) const
{
uint8_t len = GetLength();
-10
View File
@@ -160,16 +160,6 @@ public:
*/
const Tlv *GetNext(void) const { return As<Tlv>(ot::Tlv::GetNext()); }
/**
* Indicates whether a TLV appears to be well-formed.
*
* @param[in] aTlv A reference to the TLV.
*
* @returns TRUE if the TLV appears to be well-formed, FALSE otherwise.
*
*/
static bool IsValid(const Tlv &aTlv);
} OT_TOOL_PACKED_END;
/**