[mdns] add API to get the list of local host IP addresses (#11404)

This commit introduces an API to iterate over the local host IPv6 and
IPv4 addresses known to the OpenThread mDNS module.

The platform layer is responsible for monitoring and reporting all
host IPv4 and IPv6 addresses to the OpenThread mDNS module, which
then tracks the full address list
(see `otPlatMdnsHandleHostAddressEvent()`). The newly added function
allows iteration through this tracked list, primarily intended for
information and debugging purposes.

This commit also adds a CLI command to utilize the new API.
Additionally, the `test_mdns` unit test has been updated to validate
the functionality of the newly added API.
This commit is contained in:
Abtin Keshavarzian
2025-04-18 12:11:16 -07:00
committed by GitHub
parent b25d1af5f5
commit f70749d21d
7 changed files with 244 additions and 1 deletions
+81
View File
@@ -2164,6 +2164,69 @@ void TestHostReg(void)
//---------------------------------------------------------------------------------------------------------------------
void ValidateLocalHostAddresses(Core &aMdns, const LocalHost &aLocalHost)
{
#if OPENTHREAD_CONFIG_MULTICAST_DNS_ENTRY_ITERATION_API_ENABLE
Core::Iterator *iterator;
Array<Ip6::Address, LocalHost::kMaxAddrs> ip6Addrs;
Array<Ip4::Address, LocalHost::kMaxAddrs> ip4Addrs;
iterator = aMdns.AllocateIterator();
VerifyOrQuit(iterator != nullptr);
Log("Verifying local host addresses (%u IPv6 and %u IPv4 addresses)", aLocalHost.mIp6Addrs.GetLength(),
aLocalHost.mIp4Addrs.GetLength());
while (true)
{
Error error;
Core::LocalHostAddress addr;
error = aMdns.GetNextLocalHostAddress(*iterator, addr);
if (error == kErrorNotFound)
{
break;
}
SuccessOrQuit(error);
VerifyOrQuit(addr.mInfraIfIndex == kInfraIfIndex);
if (addr.mIsIp6)
{
SuccessOrQuit(ip6Addrs.PushBack(AsCoreType(&addr.mAddress.mIp6)));
Log(" %s", ip6Addrs.Back()->ToString().AsCString());
}
else
{
SuccessOrQuit(ip4Addrs.PushBack(AsCoreType(&addr.mAddress.mIp4)));
Log(" %s", ip4Addrs.Back()->ToString().AsCString());
}
}
aMdns.FreeIterator(*iterator);
VerifyOrQuit(ip6Addrs.GetLength() == aLocalHost.mIp6Addrs.GetLength());
for (const Ip6::Address &ip6Addr : ip6Addrs)
{
VerifyOrQuit(aLocalHost.mIp6Addrs.Contains(ip6Addr));
}
VerifyOrQuit(ip4Addrs.GetLength() == aLocalHost.mIp4Addrs.GetLength());
for (const Ip4::Address &ip4Addr : ip4Addrs)
{
VerifyOrQuit(aLocalHost.mIp4Addrs.Contains(ip4Addr));
}
#else
OT_UNUSED_VARIABLE(aMdns);
OT_UNUSED_VARIABLE(aLocalHost);
#endif
}
void TestLocalHost(void)
{
Core *mdns = InitTest();
@@ -2188,6 +2251,8 @@ void TestLocalHost(void)
hostFullName.Append("%s.local.", localHost.mName);
ValidateLocalHostAddresses(*mdns, localHost);
Log("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
Log("Add an IP6 address and IP4 address for local host, check probes and announcements");
@@ -2230,6 +2295,8 @@ void TestLocalHost(void)
VerifyOrQuit(dnsMsg->GetNext() == nullptr);
}
ValidateLocalHostAddresses(*mdns, localHost);
Log("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
Log("Send a query for AAAA record and validate the response");
@@ -2314,6 +2381,8 @@ void TestLocalHost(void)
sDnsMessages.Clear();
}
ValidateLocalHostAddresses(*mdns, localHost);
Log("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
Log("Signal new host IPv6 addresses added and removed");
@@ -2351,6 +2420,8 @@ void TestLocalHost(void)
sDnsMessages.Clear();
}
ValidateLocalHostAddresses(*mdns, localHost);
Log("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
Log("Signal three new host IPv4 addresses added");
@@ -2387,6 +2458,8 @@ void TestLocalHost(void)
sDnsMessages.Clear();
}
ValidateLocalHostAddresses(*mdns, localHost);
Log("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
Log("Signal removal of all host addresses and add them all back");
@@ -2532,6 +2605,8 @@ void TestLocalHost(void)
AdvanceTime(10 * 1000);
VerifyOrQuit(sDnsMessages.IsEmpty());
ValidateLocalHostAddresses(*mdns, localHost);
Log("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
Log("Signal removal of an IPv6 host address which was not added earlier");
@@ -2545,6 +2620,8 @@ void TestLocalHost(void)
AdvanceTime(10 * 1000);
VerifyOrQuit(sDnsMessages.IsEmpty());
ValidateLocalHostAddresses(*mdns, localHost);
Log("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
Log("Signal remove and re-add of the same host IPv6 address quickly");
@@ -2560,6 +2637,8 @@ void TestLocalHost(void)
AdvanceTime(10 * 1000);
VerifyOrQuit(sDnsMessages.IsEmpty());
ValidateLocalHostAddresses(*mdns, localHost);
Log("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
Log("Validate `SetLocalHostName()`");
@@ -2629,6 +2708,8 @@ void TestLocalHost(void)
VerifyOrQuit(dnsMsg->GetNext() == nullptr);
}
ValidateLocalHostAddresses(*mdns, localHost);
AdvanceTime(1000);
Log("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");