Skip to content

Commit

Permalink
ss
Browse files Browse the repository at this point in the history
  • Loading branch information
wqx6 committed Apr 28, 2024
1 parent 9ff29ed commit 5294994
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions src/platform/ESP32/ESP32EndpointQueueFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ class ESP32EndpointQueueFilter : public EndpointQueueFilter
}
// Drop the mDNS packets which don't contain 'matter' or '<device-hostname>'.
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;
}
Expand Down Expand Up @@ -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<uint8_t>('a' + hostNameLowerCase[i] - 'A');
return true;
}
}
return PayloadContains(payload, ByteSpan(mHostNameBuffer)) || PayloadContains(payload, ByteSpan(hostNameLowerCase));
return false;
}

static bool IsValidMdnsHostName(const chip::CharSpan & hostName)
Expand Down

0 comments on commit 5294994

Please sign in to comment.