[mdns] allow service registration for local host (#11450)

This commit updates the mDNS service registration to allow services
for the local host. The `mHostName` field in an `otMdnsService`
structure can now be set to `NULL` to indicate that the service
if for the local host.

The `test_mdns` unit test is also updated to verify this new
functionality.
This commit is contained in:
Abtin Keshavarzian
2025-04-29 14:07:16 -07:00
committed by GitHub
parent 7a2e337493
commit 9e9522aaac
5 changed files with 34 additions and 6 deletions
+24 -1
View File
@@ -481,7 +481,8 @@ struct DnsRecords : public OwningList<DnsRecord>
bool contains = false;
DnsNameString hostName;
hostName.Append("%s.local.", aService.mHostName);
hostName.Append("%s.local.",
aService.mHostName != nullptr ? aService.mHostName : sInstance->Get<Core>().GetLocalHostName());
for (const DnsRecord &record : *this)
{
@@ -3144,6 +3145,28 @@ void TestServiceReg(void)
sDnsMessages.Clear();
}
Log("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
Log("Update service host name to use local host and validate new announcements of SRV record");
service.mHostName = nullptr;
sRegCallbacks[1].Reset();
sDnsMessages.Clear();
SuccessOrQuit(mdns->RegisterService(service, 1, HandleSuccessCallback));
for (uint8_t anncCount = 0; anncCount < kNumAnnounces; anncCount++)
{
AdvanceTime((anncCount == 0) ? 0 : (1U << (anncCount - 1)) * 1000);
VerifyOrQuit(sRegCallbacks[1].mWasCalled);
VerifyOrQuit(!sDnsMessages.IsEmpty());
dnsMsg = sDnsMessages.GetHead();
dnsMsg->ValidateHeader(kMulticastResponse, /* Q */ 0, /* Ans */ 1, /* Auth */ 0, /* Addnl */ 1);
dnsMsg->Validate(service, kInAnswerSection, kCheckSrv);
VerifyOrQuit(dnsMsg->GetNext() == nullptr);
sDnsMessages.Clear();
}
Log("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
Log("Update service host name and validate new announcements of SRV record");