[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
+1 -1
View File
@@ -52,7 +52,7 @@ extern "C" {
*
* @note This number versions both OpenThread platform and user APIs.
*/
#define OPENTHREAD_API_VERSION (502)
#define OPENTHREAD_API_VERSION (503)
/**
* @addtogroup api-instance
+2 -1
View File
@@ -311,7 +311,8 @@ otError otMdnsUnregisterHost(otInstance *aInstance, const otMdnsHost *aHost);
* contain dot `.` character which is allowed in a service instance label.
* - The `mServiceType` specifies the service type (e.g., "_tst._udp"). It is treated as multiple dot `.` separated
* labels. It MUST NOT contain the domain name.
* - The `mHostName` field specifies the host name of the service. MUST NOT contain the domain name.
* - The `mHostName` field specifies the host name of the service if it is not NULL. Otherwise, if it is NULL, it
* indicates that this service is for the local host (this device itself).
* - The `mSubTypeLabels` is an array of strings representing sub-types associated with the service. Each array entry
* is a sub-type label. The `mSubTypeLabels can be NULL if there is no sub-type. Otherwise, the array length is
* specified by `mSubTypeLabelsLength`.
+5 -2
View File
@@ -2332,7 +2332,8 @@ exit:
void Core::ServiceEntry::Register(const Service &aService, const Callback &aCallback)
{
uint32_t ttl = DetermineTtl(aService.mTtl, kDefaultTtl);
const char *hostName;
uint32_t ttl = DetermineTtl(aService.mTtl, kDefaultTtl);
if (GetState() == kRemoving)
{
@@ -2377,8 +2378,10 @@ void Core::ServiceEntry::Register(const Service &aService, const Callback &aCall
// Register SRV record info.
hostName = (aService.mHostName != nullptr) ? aService.mHostName : Get<Core>().mLocalHost.GetName();
mSrvRecord.UpdateTtl(ttl);
mSrvRecord.UpdateProperty(mHostName, aService.mHostName);
mSrvRecord.UpdateProperty(mHostName, hostName);
mSrvRecord.UpdateProperty(mPriority, aService.mPriority);
mSrvRecord.UpdateProperty(mWeight, aService.mWeight);
mSrvRecord.UpdateProperty(mPort, aService.mPort);
+2 -1
View File
@@ -301,7 +301,8 @@ public:
* contain dot `.` character which is allowed in a service instance label.
* - The `mServiceType` specifies the service type (e.g., "_tst._udp"). It is treated as multiple dot `.` separated
* labels. It MUST NOT contain the domain name.
* - The `mHostName` field specifies the host name of the service. MUST NOT contain the domain name.
* - The `mHostName` field specifies the host name of the service if it is not `nullptr`. Otherwise, if it is
* `nullptr`, it indicates that this service is for the local host (this device itself).
* - The `mSubTypeLabels` is an array of strings representing sub-types associated with the service. Each array
* entry is a sub-type label. The `mSubTypeLabels can be `nullptr` if there are no sub-types. Otherwise, the
* array length is specified by `mSubTypeLabelsLength`.
+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");