Skip to content

Commit f0ce64b

Browse files
committed
Add Compare IPAddress in discoveredNode
1 parent c3ef110 commit f0ce64b

File tree

2 files changed

+127
-3
lines changed

2 files changed

+127
-3
lines changed

src/controller/AbstractDnssdDiscoveryController.cpp

+26-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,31 @@
2525
namespace chip {
2626
namespace Controller {
2727

28+
bool compareIpAddresses(const size_t sourceNumIPs, const size_t destinationNumIPs, const Inet::IPAddress *source, const Inet::IPAddress *destination)
29+
{
30+
size_t sameIpAddress = 0;
31+
bool addressUsed[chip::Dnssd::CommonResolutionData::kMaxIPAddresses] = {false};
32+
if (sourceNumIPs != destinationNumIPs)
33+
{
34+
return false;
35+
}
36+
37+
for (size_t s = 0 ; s < sourceNumIPs ; s++)
38+
{
39+
for (size_t d = 0 ; d < destinationNumIPs ; d++)
40+
{
41+
if (!addressUsed[d] && source[s] == destination[d])
42+
{
43+
// Change the user flag so that the compared target is no longer used
44+
addressUsed[d] = true;
45+
sameIpAddress++;
46+
break;
47+
}
48+
}
49+
}
50+
return sameIpAddress == destinationNumIPs;
51+
}
52+
2853
void AbstractDnssdDiscoveryController::OnNodeDiscovered(const chip::Dnssd::DiscoveredNodeData & discNodeData)
2954
{
3055
VerifyOrReturn(discNodeData.Is<chip::Dnssd::CommissionNodeData>());
@@ -38,10 +63,8 @@ void AbstractDnssdDiscoveryController::OnNodeDiscovered(const chip::Dnssd::Disco
3863
{
3964
continue;
4065
}
41-
// TODO(#32576) Check if IP address are the same. Must account for `numIPs` in the list of `ipAddress`.
42-
// Additionally, must NOT assume that the ordering is consistent.
4366
if (strcmp(discoveredNode.hostName, nodeData.hostName) == 0 && discoveredNode.port == nodeData.port &&
44-
discoveredNode.numIPs == nodeData.numIPs)
67+
compareIpAddresses(discoveredNode.numIPs, nodeData.numIPs, discoveredNode.ipAddress, nodeData.ipAddress))
4568
{
4669
discoveredNode = nodeData;
4770
if (mDeviceDiscoveryDelegate != nullptr)

src/controller/tests/TestCommissionableNodeController.cpp

+101
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,105 @@ TEST_F(TestCommissionableNodeController, TestDiscoverCommissioners_DiscoverCommi
182182
EXPECT_NE(controller.DiscoverCommissioners(), CHIP_NO_ERROR);
183183
}
184184

185+
TEST_F(TestCommissionableNodeController, TestGetDiscoveredCommissioner_MultipleIPAddressDiscover)
186+
{
187+
MockResolver resolver;
188+
CommissionableNodeController controller(&resolver);
189+
190+
// example 1
191+
chip::Dnssd::DiscoveredNodeData discNodeData1;
192+
discNodeData1.Set<chip::Dnssd::CommissionNodeData>();
193+
chip::Dnssd::CommissionNodeData & inNodeData1 = discNodeData1.Get<chip::Dnssd::CommissionNodeData>();
194+
Platform::CopyString(inNodeData1.hostName, "mockHostName");
195+
Inet::IPAddress::FromString("fd11:1111:1122:2222:1111:2222:3333:4444", inNodeData1.ipAddress[0]);
196+
inNodeData1.numIPs++;
197+
Inet::IPAddress::FromString("fd11:1111:1122:2222:2222:3333:4444:5555", inNodeData1.ipAddress[1]);
198+
inNodeData1.numIPs++;
199+
inNodeData1.port = 5540;
200+
201+
controller.OnNodeDiscovered(discNodeData1);
202+
203+
// example 5 - exactly same as example 1
204+
chip::Dnssd::DiscoveredNodeData discNodeData5;
205+
discNodeData5.Set<chip::Dnssd::CommissionNodeData>();
206+
chip::Dnssd::CommissionNodeData & inNodeData5 = discNodeData5.Get<chip::Dnssd::CommissionNodeData>();
207+
Platform::CopyString(inNodeData1.hostName, "mockHostName");
208+
Inet::IPAddress::FromString("fd11:1111:1122:2222:1111:2222:3333:4444", inNodeData5.ipAddress[0]);
209+
inNodeData5.numIPs++;
210+
Inet::IPAddress::FromString("fd11:1111:1122:2222:2222:3333:4444:5555", inNodeData5.ipAddress[1]);
211+
inNodeData5.numIPs++;
212+
inNodeData5.port = 5540;
213+
214+
controller.OnNodeDiscovered(discNodeData5);
215+
216+
// example 2 - same as example 1 (IPAdress sequence is only different.)
217+
chip::Dnssd::DiscoveredNodeData discNodeData2;
218+
discNodeData2.Set<chip::Dnssd::CommissionNodeData>();
219+
chip::Dnssd::CommissionNodeData & inNodeData2 = discNodeData2.Get<chip::Dnssd::CommissionNodeData>();
220+
Platform::CopyString(inNodeData2.hostName, "mockHostName");
221+
Inet::IPAddress::FromString("fd11:1111:1122:2222:2222:3333:4444:5555", inNodeData2.ipAddress[0]);
222+
inNodeData2.numIPs++;
223+
Inet::IPAddress::FromString("fd11:1111:1122:2222:1111:2222:3333:4444", inNodeData2.ipAddress[1]);
224+
inNodeData2.numIPs++;
225+
inNodeData2.port = 5540;
226+
227+
controller.OnNodeDiscovered(discNodeData2);
228+
229+
// example 3 - different example
230+
chip::Dnssd::DiscoveredNodeData discNodeData3;
231+
discNodeData3.Set<chip::Dnssd::CommissionNodeData>();
232+
chip::Dnssd::CommissionNodeData & inNodeData3 = discNodeData3.Get<chip::Dnssd::CommissionNodeData>();
233+
Platform::CopyString(inNodeData3.hostName, "mockHostName");
234+
Inet::IPAddress::FromString("fd11:1111:1122:2222:1111:2222:3333:4444", inNodeData3.ipAddress[0]);
235+
inNodeData3.numIPs++;
236+
Inet::IPAddress::FromString("fd11:1111:1122:2222:2222:3333:4444:6666", inNodeData3.ipAddress[1]);
237+
inNodeData3.numIPs++;
238+
inNodeData3.port = 5540;
239+
240+
controller.OnNodeDiscovered(discNodeData3);
241+
242+
// example 4 - different example (Different IP count)
243+
chip::Dnssd::DiscoveredNodeData discNodeData4;
244+
discNodeData4.Set<chip::Dnssd::CommissionNodeData>();
245+
chip::Dnssd::CommissionNodeData & inNodeData4 = discNodeData4.Get<chip::Dnssd::CommissionNodeData>();
246+
Platform::CopyString(inNodeData4.hostName, "mockHostName");
247+
Inet::IPAddress::FromString("fd11:1111:1122:2222:1111:2222:3333:4444", inNodeData4.ipAddress[0]);
248+
inNodeData4.numIPs++;
249+
Inet::IPAddress::FromString("fd11:1111:1122:2222:2222:3333:4444:6666", inNodeData4.ipAddress[1]);
250+
inNodeData4.numIPs++;
251+
Inet::IPAddress::FromString("fd11:1111:1122:2222:2222:3333:4444:7777", inNodeData4.ipAddress[2]);
252+
inNodeData4.numIPs++;
253+
inNodeData4.port = 5540;
254+
255+
controller.OnNodeDiscovered(discNodeData4);
256+
257+
// Example 2 result - example 1 is removed. (reason : duplicate)
258+
ASSERT_NE(controller.GetDiscoveredCommissioner(0), nullptr);
259+
EXPECT_STREQ(inNodeData1.hostName, controller.GetDiscoveredCommissioner(0)->hostName);
260+
EXPECT_EQ(inNodeData2.ipAddress[0], controller.GetDiscoveredCommissioner(0)->ipAddress[0]);
261+
EXPECT_EQ(inNodeData2.ipAddress[1], controller.GetDiscoveredCommissioner(0)->ipAddress[1]);
262+
EXPECT_EQ(controller.GetDiscoveredCommissioner(0)->port, 5540);
263+
EXPECT_EQ(controller.GetDiscoveredCommissioner(0)->numIPs, 2u);
264+
265+
// Example 3 result
266+
ASSERT_NE(controller.GetDiscoveredCommissioner(1), nullptr);
267+
EXPECT_STREQ(inNodeData3.hostName, controller.GetDiscoveredCommissioner(1)->hostName);
268+
EXPECT_EQ(inNodeData3.ipAddress[0], controller.GetDiscoveredCommissioner(1)->ipAddress[0]);
269+
EXPECT_EQ(inNodeData3.ipAddress[1], controller.GetDiscoveredCommissioner(1)->ipAddress[1]);
270+
EXPECT_EQ(controller.GetDiscoveredCommissioner(1)->port, 5540);
271+
EXPECT_EQ(controller.GetDiscoveredCommissioner(1)->numIPs, 2u);
272+
273+
// Example 4 result
274+
ASSERT_NE(controller.GetDiscoveredCommissioner(2), nullptr);
275+
EXPECT_STREQ(inNodeData4.hostName, controller.GetDiscoveredCommissioner(2)->hostName);
276+
EXPECT_EQ(inNodeData4.ipAddress[0], controller.GetDiscoveredCommissioner(2)->ipAddress[0]);
277+
EXPECT_EQ(inNodeData4.ipAddress[1], controller.GetDiscoveredCommissioner(2)->ipAddress[1]);
278+
EXPECT_EQ(inNodeData4.ipAddress[2], controller.GetDiscoveredCommissioner(2)->ipAddress[2]);
279+
EXPECT_EQ(controller.GetDiscoveredCommissioner(2)->port, 5540);
280+
EXPECT_EQ(controller.GetDiscoveredCommissioner(2)->numIPs, 3u);
281+
282+
// Total is 3. (Not 4)
283+
ASSERT_EQ(controller.GetDiscoveredCommissioner(3), nullptr);
284+
}
285+
185286
} // namespace

0 commit comments

Comments
 (0)