From 62cac82ad8f41f297b7dc61d0e8acb52dbe9ee5c Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Tue, 4 Feb 2025 08:14:52 -0800 Subject: [PATCH] [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). --- src/core/net/mdns.cpp | 41 +++++++++++++++++++++++------------------ src/core/net/mdns.hpp | 29 ++++++++++++++++------------- 2 files changed, 39 insertions(+), 31 deletions(-) diff --git a/src/core/net/mdns.cpp b/src/core/net/mdns.cpp index 7273036ff..977513567 100644 --- a/src/core/net/mdns.cpp +++ b/src/core/net/mdns.cpp @@ -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(kCrc16AnsiPolynomial).Feed(aMessage, offsetRange); + mCrc32 = CrcCalculator(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); } diff --git a/src/core/net/mdns.hpp b/src/core/net/mdns.hpp index e5f7531b3..549fc0a2a 100644 --- a/src/core/net/mdns.hpp +++ b/src/core/net/mdns.hpp @@ -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, public Heap::Allocatable + struct MsgInfo : public Clearable, public Equatable { - 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, public Heap::Allocatable + { + 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; - OwningList mHashEntries; - TxMsgHistoryTimer mTimer; + OwningList mMsgEntries; + TxMsgHistoryTimer mTimer; }; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -