Skip to content

Commit 49352a1

Browse files
authored
[dnssd] introduce AppendServiceRecords() helper (openthread#11443)
This commit introduces a template helper `AppendServiceRecords()` designed to append service-related records (SRV, TXT, and host AAAA addresses) to the appropriate sections within a DNS `Response` message. This helper simplifies the codebase by removing repeated patterns. These patterns occur when resolving queries using either SRP service data or `ServiceInstanceInfo` retrieved from the platform (when the platform implements discovery proxy function).
1 parent d9710c6 commit 49352a1

2 files changed

Lines changed: 55 additions & 52 deletions

File tree

src/core/net/dnssd_server.cpp

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,15 @@ bool Server::Response::QueryNameMatches(const char *aName) const { return Server
452452

453453
Error Server::Response::AppendQueryName(void) { return Name::AppendPointerLabel(sizeof(Header), *mMessage); }
454454

455+
#if OPENTHREAD_CONFIG_SRP_SERVER_ENABLE
456+
Error Server::Response::AppendPtrRecord(const Srp::Server::Service &aService)
457+
{
458+
uint32_t ttl = TimeMilli::MsecToSec(aService.GetExpireTime() - TimerMilli::GetNow());
459+
460+
return AppendPtrRecord(aService.GetInstanceLabel(), ttl);
461+
}
462+
#endif
463+
455464
Error Server::Response::AppendPtrRecord(const char *aInstanceLabel, uint32_t aTtl)
456465
{
457466
Error error;
@@ -531,6 +540,11 @@ Error Server::Response::AppendSrvRecord(const char *aHostName,
531540
}
532541

533542
#if OPENTHREAD_CONFIG_SRP_SERVER_ENABLE
543+
Error Server::Response::AppendHostAddresses(const Srp::Server::Service &aService)
544+
{
545+
return AppendHostAddresses(aService.GetHost());
546+
}
547+
534548
Error Server::Response::AppendHostAddresses(const Srp::Server::Host &aHost)
535549
{
536550
const Ip6::Address *addrs;
@@ -698,6 +712,41 @@ Error Server::Response::AppendGenericRecord(uint16_t aRrType, const void *aData,
698712
return error;
699713
}
700714

715+
template <typename ServiceType> Error Server::Response::AppendServiceRecords(const ServiceType &aService)
716+
{
717+
static const Section kSections[] = {kAnswerSection, kAdditionalDataSection};
718+
719+
// Append SRV and TXT records along with associated host AAAA addresses
720+
// in the proper sections.
721+
722+
Error error = kErrorNone;
723+
724+
for (Section section : kSections)
725+
{
726+
mSection = section;
727+
728+
if (mSection == kAdditionalDataSection)
729+
{
730+
VerifyOrExit(!(Get<Server>().mTestMode & kTestModeEmptyAdditionalSection));
731+
}
732+
733+
if (mSection == mQuestions.SectionFor(kRrTypeSrv))
734+
{
735+
SuccessOrExit(error = AppendSrvRecord(aService));
736+
}
737+
738+
if (mSection == mQuestions.SectionFor(kRrTypeTxt))
739+
{
740+
SuccessOrExit(error = AppendTxtRecord(aService));
741+
}
742+
}
743+
744+
error = AppendHostAddresses(aService);
745+
746+
exit:
747+
return error;
748+
}
749+
701750
void Server::Response::IncResourceRecordCount(void)
702751
{
703752
switch (mSection)
@@ -729,8 +778,6 @@ void Server::Response::Log(void) const
729778

730779
Error Server::Response::ResolveBySrp(void)
731780
{
732-
static const Section kSections[] = {kAnswerSection, kAdditionalDataSection};
733-
734781
Error error = kErrorNone;
735782
const Srp::Server::Service *matchedService = nullptr;
736783
bool found = false;
@@ -772,9 +819,7 @@ Error Server::Response::ResolveBySrp(void)
772819
{
773820
if (QueryNameMatchesService(service))
774821
{
775-
uint32_t ttl = TimeMilli::MsecToSec(service.GetExpireTime() - TimerMilli::GetNow());
776-
777-
SuccessOrExit(error = AppendPtrRecord(service.GetInstanceLabel(), ttl));
822+
SuccessOrExit(error = AppendPtrRecord(service));
778823
matchedService = &service;
779824
}
780825
}
@@ -815,30 +860,7 @@ Error Server::Response::ResolveBySrp(void)
815860
VerifyOrExit(mQuestions.IsFor(kRrTypeSrv) || mQuestions.IsFor(kRrTypeTxt));
816861
}
817862

818-
// Append SRV and TXT records along with associated host AAAA addresses
819-
// in the proper sections.
820-
821-
for (Section section : kSections)
822-
{
823-
mSection = section;
824-
825-
if (mSection == kAdditionalDataSection)
826-
{
827-
VerifyOrExit(!(Get<Server>().mTestMode & kTestModeEmptyAdditionalSection));
828-
}
829-
830-
if (mSection == mQuestions.SectionFor(kRrTypeSrv))
831-
{
832-
SuccessOrExit(error = AppendSrvRecord(*matchedService));
833-
}
834-
835-
if (mSection == mQuestions.SectionFor(kRrTypeTxt))
836-
{
837-
SuccessOrExit(error = AppendTxtRecord(*matchedService));
838-
}
839-
}
840-
841-
SuccessOrExit(error = AppendHostAddresses(matchedService->GetHost()));
863+
error = AppendServiceRecords(*matchedService);
842864

843865
exit:
844866
return error;
@@ -1168,8 +1190,6 @@ void Server::Response::InitFrom(ProxyQuery &aQuery, const ProxyQueryInfo &aInfo)
11681190

11691191
void Server::Response::Answer(const ServiceInstanceInfo &aInstanceInfo, const Ip6::MessageInfo &aMessageInfo)
11701192
{
1171-
static const Section kSections[] = {kAnswerSection, kAdditionalDataSection};
1172-
11731193
Error error = kErrorNone;
11741194

11751195
if (mQuestions.IsFor(kRrTypePtr))
@@ -1181,27 +1201,7 @@ void Server::Response::Answer(const ServiceInstanceInfo &aInstanceInfo, const Ip
11811201
SuccessOrExit(error = AppendPtrRecord(instanceLabel, aInstanceInfo.mTtl));
11821202
}
11831203

1184-
for (Section section : kSections)
1185-
{
1186-
mSection = section;
1187-
1188-
if (mSection == kAdditionalDataSection)
1189-
{
1190-
VerifyOrExit(!(Get<Server>().mTestMode & kTestModeEmptyAdditionalSection));
1191-
}
1192-
1193-
if (mSection == mQuestions.SectionFor(kRrTypeSrv))
1194-
{
1195-
SuccessOrExit(error = AppendSrvRecord(aInstanceInfo));
1196-
}
1197-
1198-
if (mSection == mQuestions.SectionFor(kRrTypeTxt))
1199-
{
1200-
SuccessOrExit(error = AppendTxtRecord(aInstanceInfo));
1201-
}
1202-
}
1203-
1204-
error = AppendHostAddresses(aInstanceInfo);
1204+
error = AppendServiceRecords(aInstanceInfo);
12051205

12061206
exit:
12071207
if (error != kErrorNone)

src/core/net/dnssd_server.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,10 @@ class Server : public InstanceLocator, private NonCopyable
434434
#if OPENTHREAD_CONFIG_SRP_SERVER_ENABLE
435435
Error ResolveBySrp(void);
436436
bool QueryNameMatchesService(const Srp::Server::Service &aService) const;
437+
Error AppendPtrRecord(const Srp::Server::Service &aService);
437438
Error AppendSrvRecord(const Srp::Server::Service &aService);
438439
Error AppendTxtRecord(const Srp::Server::Service &aService);
440+
Error AppendHostAddresses(const Srp::Server::Service &aService);
439441
Error AppendHostAddresses(const Srp::Server::Host &aHost);
440442
Error AppendKeyRecord(const Srp::Server::Host &aHost);
441443
#endif
@@ -447,6 +449,7 @@ class Server : public InstanceLocator, private NonCopyable
447449
Error AppendHostIp4Addresses(const ProxyResult &aResult);
448450
Error AppendGenericRecord(const ProxyResult &aResult);
449451
#endif
452+
template <typename ServiceType> Error AppendServiceRecords(const ServiceType &aService);
450453

451454
#if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_INFO)
452455
void Log(void) const;

0 commit comments

Comments
 (0)