mirror of
https://github.com/espressif/openthread.git
synced 2026-08-09 20:27:47 +00:00
[mdns] update TxMessageHistory to use message length and CRCs (#11183)
This commit updates `TxMessageHistory` to track the message length along with calculated CRC-16 and CRC-32 values of the message content. This replaces the use of SHA256 hash, simplifying the code and removing the dependency of the mDNS core module on the crypto platform layer (and mbedtls).
This commit is contained in:
+23
-18
@@ -30,6 +30,7 @@
|
||||
|
||||
#if OPENTHREAD_CONFIG_MULTICAST_DNS_ENABLE
|
||||
|
||||
#include "common/crc.hpp"
|
||||
#include "instance/instance.hpp"
|
||||
|
||||
/**
|
||||
@@ -4368,25 +4369,25 @@ Core::TxMessageHistory::TxMessageHistory(Instance &aInstance)
|
||||
|
||||
void Core::TxMessageHistory::Clear(void)
|
||||
{
|
||||
mHashEntries.Clear();
|
||||
mMsgEntries.Clear();
|
||||
mTimer.Stop();
|
||||
}
|
||||
|
||||
void Core::TxMessageHistory::Add(const Message &aMessage)
|
||||
{
|
||||
Hash hash;
|
||||
HashEntry *entry;
|
||||
MsgInfo info;
|
||||
MsgEntry *entry;
|
||||
|
||||
CalculateHash(aMessage, hash);
|
||||
info.InitFrom(aMessage);
|
||||
|
||||
entry = mHashEntries.FindMatching(hash);
|
||||
entry = mMsgEntries.FindMatching(info);
|
||||
|
||||
if (entry == nullptr)
|
||||
{
|
||||
entry = HashEntry::Allocate();
|
||||
entry = MsgEntry::Allocate();
|
||||
OT_ASSERT(entry != nullptr);
|
||||
entry->mHash = hash;
|
||||
mHashEntries.Push(*entry);
|
||||
entry->mInfo = info;
|
||||
mMsgEntries.Push(*entry);
|
||||
}
|
||||
|
||||
entry->mExpireTime = TimerMilli::GetNow() + kExpireInterval;
|
||||
@@ -4395,28 +4396,32 @@ void Core::TxMessageHistory::Add(const Message &aMessage)
|
||||
|
||||
bool Core::TxMessageHistory::Contains(const Message &aMessage) const
|
||||
{
|
||||
Hash hash;
|
||||
MsgInfo info;
|
||||
|
||||
CalculateHash(aMessage, hash);
|
||||
return mHashEntries.ContainsMatching(hash);
|
||||
info.InitFrom(aMessage);
|
||||
|
||||
return mMsgEntries.ContainsMatching(info);
|
||||
}
|
||||
|
||||
void Core::TxMessageHistory::CalculateHash(const Message &aMessage, Hash &aHash)
|
||||
void Core::TxMessageHistory::MsgInfo::InitFrom(const Message &aMessage)
|
||||
{
|
||||
Crypto::Sha256 sha256;
|
||||
OffsetRange offsetRange;
|
||||
|
||||
sha256.Start();
|
||||
sha256.Update(aMessage, /* aOffset */ 0, aMessage.GetLength());
|
||||
sha256.Finish(aHash);
|
||||
offsetRange.InitFromMessageFullLength(aMessage);
|
||||
|
||||
Clear();
|
||||
mMsgLength = aMessage.GetLength();
|
||||
mCrc16 = CrcCalculator<uint16_t>(kCrc16AnsiPolynomial).Feed(aMessage, offsetRange);
|
||||
mCrc32 = CrcCalculator<uint32_t>(kCrc32AnsiPolynomial).Feed(aMessage, offsetRange);
|
||||
}
|
||||
|
||||
void Core::TxMessageHistory::HandleTimer(void)
|
||||
{
|
||||
NextFireTime nextTime;
|
||||
|
||||
mHashEntries.RemoveAndFreeAllMatching(ExpireChecker(nextTime.GetNow()));
|
||||
mMsgEntries.RemoveAndFreeAllMatching(ExpireChecker(nextTime.GetNow()));
|
||||
|
||||
for (const HashEntry &entry : mHashEntries)
|
||||
for (const MsgEntry &entry : mMsgEntries)
|
||||
{
|
||||
nextTime.UpdateIfEarlier(entry.mExpireTime);
|
||||
}
|
||||
|
||||
+16
-13
@@ -49,9 +49,7 @@
|
||||
#include "common/locator.hpp"
|
||||
#include "common/owned_ptr.hpp"
|
||||
#include "common/owning_list.hpp"
|
||||
#include "common/retain_ptr.hpp"
|
||||
#include "common/timer.hpp"
|
||||
#include "crypto/sha256.hpp"
|
||||
#include "net/dns_types.hpp"
|
||||
|
||||
#if OPENTHREAD_CONFIG_MULTICAST_DNS_AUTO_ENABLE_ON_INFRA_IF && !OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE
|
||||
@@ -1432,24 +1430,29 @@ private:
|
||||
private:
|
||||
static constexpr uint32_t kExpireInterval = TimeMilli::SecToMsec(10); // in msec
|
||||
|
||||
typedef Crypto::Sha256::Hash Hash;
|
||||
|
||||
struct HashEntry : public LinkedListEntry<HashEntry>, public Heap::Allocatable<HashEntry>
|
||||
struct MsgInfo : public Clearable<MsgInfo>, public Equatable<MsgInfo>
|
||||
{
|
||||
bool Matches(const Hash &aHash) const { return aHash == mHash; }
|
||||
bool Matches(const ExpireChecker &aExpireChecker) const { return mExpireTime <= aExpireChecker.mNow; }
|
||||
void InitFrom(const Message &aMessage);
|
||||
|
||||
HashEntry *mNext;
|
||||
Hash mHash;
|
||||
TimeMilli mExpireTime;
|
||||
uint16_t mMsgLength;
|
||||
uint16_t mCrc16;
|
||||
uint32_t mCrc32;
|
||||
};
|
||||
|
||||
static void CalculateHash(const Message &aMessage, Hash &aHash);
|
||||
struct MsgEntry : public LinkedListEntry<MsgEntry>, public Heap::Allocatable<MsgEntry>
|
||||
{
|
||||
bool Matches(const MsgInfo &aInfo) const { return mInfo == aInfo; }
|
||||
bool Matches(const ExpireChecker &aExpireChecker) const { return mExpireTime <= aExpireChecker.mNow; }
|
||||
|
||||
MsgEntry *mNext;
|
||||
MsgInfo mInfo;
|
||||
TimeMilli mExpireTime;
|
||||
};
|
||||
|
||||
using TxMsgHistoryTimer = TimerMilliIn<Core, &Core::HandleTxMessageHistoryTimer>;
|
||||
|
||||
OwningList<HashEntry> mHashEntries;
|
||||
TxMsgHistoryTimer mTimer;
|
||||
OwningList<MsgEntry> mMsgEntries;
|
||||
TxMsgHistoryTimer mTimer;
|
||||
};
|
||||
|
||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
||||
Reference in New Issue
Block a user