1
1
#include " EndpointQueueFilter.h"
2
2
#include < string.h>
3
+ #include < lib/support/BytesToHex.h>
3
4
#include < support/CodeUtils.h>
4
5
#include < support/logging/CHIPLogging.h>
5
- #include < string_view>
6
-
7
6
namespace chip {
8
7
namespace Inet {
9
8
10
9
using FilterOutcome = EndpointQueueFilter::FilterOutcome;
11
10
12
11
namespace {
13
12
14
- bool IsValidMdnsHostName (uint8_t * hostName)
13
+ bool IsValidMdnsHostName (const CharSpan & hostName)
15
14
{
16
- for (size_t i = 0 ; i < sizeof ( hostName); ++i)
15
+ for (size_t i = 0 ; i < hostName. size ( ); ++i)
17
16
{
18
- char ch_data = hostName[i] ;
17
+ char ch_data = *( hostName. data () + i) ;
19
18
if (!((ch_data >= ' 0' && ch_data <= ' 9' ) || (ch_data >= ' A' && ch_data <= ' F' )))
20
19
{
21
20
return false ;
@@ -24,16 +23,36 @@ bool IsValidMdnsHostName(uint8_t * hostName)
24
23
return true ;
25
24
}
26
25
27
- bool IsMdnsBroadcastPacket (const void * endpoint, const IPPacketInfo & pktInfo, const System::PacketBufferHandle & pktPayload)
26
+ bool IsMdnsBroadcastPacket (const IPPacketInfo & pktInfo, const System::PacketBufferHandle & pktPayload)
28
27
{
29
28
// if the packet is not a broadcast packet to mDNS port, drop it.
30
29
VerifyOrReturnValue (pktInfo.DestPort == 5353 , false );
31
30
#if INET_CONFIG_ENABLE_IPV4
32
31
ip_addr_t mdnsIPv4BroadcastAddr = IPADDR4_INIT_BYTES (224 , 0 , 0 , 251 );
33
- VerifyOrReturnValue (pktInfo.DestAddress == Inet::IPAddress (mdnsIPv4BroadcastAddr), false );
32
+ if (pktInfo.DestAddress == Inet::IPAddress (mdnsIPv4BroadcastAddr))
33
+ {
34
+ return true ;
35
+ }
34
36
#endif
35
37
ip_addr_t mdnsIPv6BroadcastAddr = IPADDR6_INIT_HOST (0xFF020000 , 0 , 0 , 0xFB );
36
- VerifyOrReturnValue (pktInfo.DestAddress == Inet::IPAddress (mdnsIPv6BroadcastAddr), false );
38
+ if (pktInfo.DestAddress == Inet::IPAddress (mdnsIPv6BroadcastAddr))
39
+ {
40
+ return true ;
41
+ }
42
+ return false ;
43
+ }
44
+
45
+ static bool CaseInsensitiveCompare (const CharSpan & buffer1, const CharSpan & buffer2, size_t size)
46
+ {
47
+ for (size_t i = 0 ; i < size; ++i)
48
+ {
49
+ char byte1 = (*(buffer1.data () + i) >= ' A' && *(buffer1.data () + i) <= ' Z' ) ? *(buffer1.data () + i) - ' A' + ' a' : *(buffer1.data () + i);
50
+ char byte2 = (*(buffer2.data () + i) >= ' A' && *(buffer2.data () + i) <= ' Z' ) ? *(buffer2.data () + i) - ' A' + ' a' : *(buffer2.data () + i);
51
+ if (byte1 != byte2)
52
+ {
53
+ return false ;
54
+ }
55
+ }
37
56
return true ;
38
57
}
39
58
@@ -49,9 +68,17 @@ bool PayloadContainsCaseInsensitive(const System::PacketBufferHandle & payload,
49
68
return false ;
50
69
}
51
70
52
- std::basic_string_view<uint8_t > payloadView (payload->Start (), payload->TotalLength ());
53
- std::basic_string_view<uint8_t > patternView (pattern.data (), pattern.size ());
54
- return payloadView.find (patternView) != std::basic_string_view<uint8_t >::npos;
71
+ CharSpan payloadView (reinterpret_cast <char *>(payload->Start ()), payload->TotalLength ());
72
+ CharSpan patternView ((char *)(pattern.data ()), pattern.size ());
73
+
74
+ for (size_t i = 0 ; i <= payloadView.size () - patternView.size (); ++i)
75
+ {
76
+ if (CaseInsensitiveCompare (payloadView.SubSpan (i, patternView.size ()), patternView, patternView.size ()))
77
+ {
78
+ return true ;
79
+ }
80
+ }
81
+ return false ;
55
82
}
56
83
57
84
} // namespace
@@ -66,15 +93,13 @@ FilterOutcome HostNameFilter::Filter(const void * endpoint, const IPPacketInfo &
66
93
{
67
94
return FilterOutcome::kAllowPacket ;
68
95
}
69
-
70
96
return FilterOutcome::kDropPacket ;
71
97
}
72
98
73
- CHIP_ERROR HostNameFilter::SetHostName (const ByteSpan & hostName)
99
+ CHIP_ERROR HostNameFilter::SetHostName (const CharSpan & hostName)
74
100
{
75
- const uint8_t * p = hostName.data ();
76
- snprintf ((char *) mHostName , sizeof (mHostName ), " %02X%02X%02X%02X%02X%02X" , p[0 ], p[1 ], p[2 ], p[3 ], p[4 ], p[5 ]);
77
- ReturnErrorCodeIf (!IsValidMdnsHostName (mHostName ), CHIP_ERROR_INVALID_ARGUMENT);
101
+ ReturnErrorCodeIf (!IsValidMdnsHostName (hostName), CHIP_ERROR_INVALID_ARGUMENT);
102
+ memcpy (mHostName , hostName.data (), hostName.size ());
78
103
return CHIP_NO_ERROR;
79
104
}
80
105
@@ -90,7 +115,7 @@ FilterOutcome EndpointQueueFilter::FilterBeforeEnqueue(const void * endpoint, co
90
115
VerifyOrReturnError (FilterOutcome::kAllowPacket == mTooManyFilter .FilterBeforeEnqueue (endpoint, pktInfo, pktPayload),
91
116
FilterOutcome::kDropPacket );
92
117
93
- if (!IsMdnsBroadcastPacket (endpoint, pktInfo, pktPayload))
118
+ if (!IsMdnsBroadcastPacket (pktInfo, pktPayload))
94
119
{
95
120
return FilterOutcome::kAllowPacket ;
96
121
}
0 commit comments