From 39703f7d3919444e53784c16b71afa67942a7fc5 Mon Sep 17 00:00:00 2001 From: Wang Qixiang <43193572+wqx6@users.noreply.github.com> Date: Sat, 4 May 2024 00:09:07 +0800 Subject: [PATCH] esp32: service type should be case-insensitive for endpoint filter of mdns packets (#33206) --- src/platform/ESP32/ESP32EndpointQueueFilter.h | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/platform/ESP32/ESP32EndpointQueueFilter.h b/src/platform/ESP32/ESP32EndpointQueueFilter.h index 031ea29b64d798..aa283d9b7fd770 100644 --- a/src/platform/ESP32/ESP32EndpointQueueFilter.h +++ b/src/platform/ESP32/ESP32EndpointQueueFilter.h @@ -44,7 +44,8 @@ class ESP32EndpointQueueFilter : public EndpointQueueFilter } // Drop the mDNS packets which don't contain 'matter' or ''. const uint8_t matterBytes[] = { 'm', 'a', 't', 't', 'e', 'r' }; - if (PayloadContains(pktPayload, ByteSpan(matterBytes)) || PayloadContainsHostNameCaseInsensitive(pktPayload)) + if (PayloadContainsCaseInsensitive(pktPayload, ByteSpan(matterBytes)) || + PayloadContainsCaseInsensitive(pktPayload, ByteSpan(mHostNameBuffer))) { return FilterOutcome::kAllowPacket; } @@ -79,34 +80,34 @@ class ESP32EndpointQueueFilter : public EndpointQueueFilter return false; } - static bool PayloadContains(const chip::System::PacketBufferHandle & payload, const chip::ByteSpan & byteSpan) + static bool BytesCaseInsensitiveCompare(const uint8_t * buf1, const uint8_t * buf2, size_t size) { - if (payload->HasChainedBuffer() || payload->TotalLength() < byteSpan.size()) - { - return false; - } - for (size_t i = 0; i <= payload->TotalLength() - byteSpan.size(); ++i) + for (size_t i = 0; i < size; ++i) { - if (memcmp(payload->Start() + i, byteSpan.data(), byteSpan.size()) == 0) + uint8_t byte1 = (buf1[i] >= 'A' && buf1[i] <= 'Z') ? buf1[i] - 'A' + 'a' : buf1[i]; + uint8_t byte2 = (buf2[i] >= 'A' && buf2[i] <= 'Z') ? buf2[i] - 'A' + 'a' : buf2[i]; + if (byte1 != byte2) { - return true; + return false; } } - return false; + return true; } - bool PayloadContainsHostNameCaseInsensitive(const chip::System::PacketBufferHandle & payload) + static bool PayloadContainsCaseInsensitive(const chip::System::PacketBufferHandle & payload, const chip::ByteSpan & byteSpan) { - uint8_t hostNameLowerCase[12]; - memcpy(hostNameLowerCase, mHostNameBuffer, sizeof(mHostNameBuffer)); - for (size_t i = 0; i < sizeof(hostNameLowerCase); ++i) + if (payload->HasChainedBuffer() || payload->TotalLength() < byteSpan.size()) + { + return false; + } + for (size_t i = 0; i <= payload->TotalLength() - byteSpan.size(); ++i) { - if (hostNameLowerCase[i] <= 'F' && hostNameLowerCase[i] >= 'A') + if (BytesCaseInsensitiveCompare(payload->Start() + i, byteSpan.data(), byteSpan.size())) { - hostNameLowerCase[i] = static_cast('a' + hostNameLowerCase[i] - 'A'); + return true; } } - return PayloadContains(payload, ByteSpan(mHostNameBuffer)) || PayloadContains(payload, ByteSpan(hostNameLowerCase)); + return false; } static bool IsValidMdnsHostName(const chip::CharSpan & hostName)