@@ -452,6 +452,15 @@ bool Server::Response::QueryNameMatches(const char *aName) const { return Server
452452
453453Error 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+
455464Error 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+
534548Error 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+
701750void Server::Response::IncResourceRecordCount (void )
702751{
703752 switch (mSection )
@@ -729,8 +778,6 @@ void Server::Response::Log(void) const
729778
730779Error 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
843865exit:
844866 return error;
@@ -1168,8 +1190,6 @@ void Server::Response::InitFrom(ProxyQuery &aQuery, const ProxyQueryInfo &aInfo)
11681190
11691191void 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
12061206exit:
12071207 if (error != kErrorNone )
0 commit comments