Skip to content

Commit 39703f7

Browse files
authored
esp32: service type should be case-insensitive for endpoint filter of mdns packets (project-chip#33206)
1 parent 9ba6d0c commit 39703f7

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

src/platform/ESP32/ESP32EndpointQueueFilter.h

+18-17
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ class ESP32EndpointQueueFilter : public EndpointQueueFilter
4444
}
4545
// Drop the mDNS packets which don't contain 'matter' or '<device-hostname>'.
4646
const uint8_t matterBytes[] = { 'm', 'a', 't', 't', 'e', 'r' };
47-
if (PayloadContains(pktPayload, ByteSpan(matterBytes)) || PayloadContainsHostNameCaseInsensitive(pktPayload))
47+
if (PayloadContainsCaseInsensitive(pktPayload, ByteSpan(matterBytes)) ||
48+
PayloadContainsCaseInsensitive(pktPayload, ByteSpan(mHostNameBuffer)))
4849
{
4950
return FilterOutcome::kAllowPacket;
5051
}
@@ -79,34 +80,34 @@ class ESP32EndpointQueueFilter : public EndpointQueueFilter
7980
return false;
8081
}
8182

82-
static bool PayloadContains(const chip::System::PacketBufferHandle & payload, const chip::ByteSpan & byteSpan)
83+
static bool BytesCaseInsensitiveCompare(const uint8_t * buf1, const uint8_t * buf2, size_t size)
8384
{
84-
if (payload->HasChainedBuffer() || payload->TotalLength() < byteSpan.size())
85-
{
86-
return false;
87-
}
88-
for (size_t i = 0; i <= payload->TotalLength() - byteSpan.size(); ++i)
85+
for (size_t i = 0; i < size; ++i)
8986
{
90-
if (memcmp(payload->Start() + i, byteSpan.data(), byteSpan.size()) == 0)
87+
uint8_t byte1 = (buf1[i] >= 'A' && buf1[i] <= 'Z') ? buf1[i] - 'A' + 'a' : buf1[i];
88+
uint8_t byte2 = (buf2[i] >= 'A' && buf2[i] <= 'Z') ? buf2[i] - 'A' + 'a' : buf2[i];
89+
if (byte1 != byte2)
9190
{
92-
return true;
91+
return false;
9392
}
9493
}
95-
return false;
94+
return true;
9695
}
9796

98-
bool PayloadContainsHostNameCaseInsensitive(const chip::System::PacketBufferHandle & payload)
97+
static bool PayloadContainsCaseInsensitive(const chip::System::PacketBufferHandle & payload, const chip::ByteSpan & byteSpan)
9998
{
100-
uint8_t hostNameLowerCase[12];
101-
memcpy(hostNameLowerCase, mHostNameBuffer, sizeof(mHostNameBuffer));
102-
for (size_t i = 0; i < sizeof(hostNameLowerCase); ++i)
99+
if (payload->HasChainedBuffer() || payload->TotalLength() < byteSpan.size())
100+
{
101+
return false;
102+
}
103+
for (size_t i = 0; i <= payload->TotalLength() - byteSpan.size(); ++i)
103104
{
104-
if (hostNameLowerCase[i] <= 'F' && hostNameLowerCase[i] >= 'A')
105+
if (BytesCaseInsensitiveCompare(payload->Start() + i, byteSpan.data(), byteSpan.size()))
105106
{
106-
hostNameLowerCase[i] = static_cast<uint8_t>('a' + hostNameLowerCase[i] - 'A');
107+
return true;
107108
}
108109
}
109-
return PayloadContains(payload, ByteSpan(mHostNameBuffer)) || PayloadContains(payload, ByteSpan(hostNameLowerCase));
110+
return false;
110111
}
111112

112113
static bool IsValidMdnsHostName(const chip::CharSpan & hostName)

0 commit comments

Comments
 (0)