diff --git a/src/core/net/srp_server.cpp b/src/core/net/srp_server.cpp index 7bdc091cf..363accc0b 100644 --- a/src/core/net/srp_server.cpp +++ b/src/core/net/srp_server.cpp @@ -366,7 +366,7 @@ void Server::CommitSrpUpdate(Error aError, } else if (existingHost != nullptr) { - const Service *service = nullptr; + Service *service = nullptr; // Merge current updates into existing host. @@ -386,9 +386,9 @@ void Server::CommitSrpUpdate(Error aError, Service *newService = existingHost->AddService(service->GetFullName()); VerifyOrExit(newService != nullptr, aError = kErrorNoBufs); - SuccessOrExit(aError = newService->CopyResourcesFrom(*service)); + newService->TakeResourcesFrom(*service); otLogInfoSrp("[server] %s service %s", (existingService != nullptr) ? "update existing" : "add new", - service->GetFullName()); + newService->GetFullName()); } } @@ -1354,27 +1354,6 @@ TimeMilli Server::Service::GetKeyExpireTime(void) const return mTimeLastUpdate + Time::SecToMsec(GetHost().GetKeyLease()); } -Error Server::Service::SetTxtData(const uint8_t *aTxtData, uint16_t aTxtDataLength) -{ - Error error = kErrorNone; - uint8_t *txtData; - - txtData = static_cast(Instance::HeapCAlloc(1, aTxtDataLength)); - VerifyOrExit(txtData != nullptr, error = kErrorNoBufs); - - memcpy(txtData, aTxtData, aTxtDataLength); - - Instance::HeapFree(mTxtData); - mTxtData = txtData; - mTxtLength = aTxtDataLength; - - // If a TXT RR is associated to this service, the service will retain. - mIsDeleted = false; - -exit: - return error; -} - Error Server::Service::SetTxtDataFromMessage(const Message &aMessage, uint16_t aOffset, uint16_t aLength) { Error error = kErrorNone; @@ -1408,20 +1387,22 @@ void Server::Service::ClearResources(void) mTxtLength = 0; } -Error Server::Service::CopyResourcesFrom(const Service &aService) +void Server::Service::TakeResourcesFrom(Service &aService) { - Error error; + // Take ownership and move the heap allocated `mTxtData` buffer + // from `aService`. + Instance::HeapFree(mTxtData); + mTxtData = aService.mTxtData; + mTxtLength = aService.mTxtLength; + aService.mTxtData = nullptr; + aService.mTxtLength = 0; - SuccessOrExit(error = SetTxtData(aService.mTxtData, aService.mTxtLength)); mPriority = aService.mPriority; mWeight = aService.mWeight; mPort = aService.mPort; mIsDeleted = false; mTimeLastUpdate = TimerMilli::GetNow(); - -exit: - return error; } bool Server::Service::MatchesServiceName(const char *aServiceName) const diff --git a/src/core/net/srp_server.hpp b/src/core/net/srp_server.hpp index e6d5931bc..3542275f8 100644 --- a/src/core/net/srp_server.hpp +++ b/src/core/net/srp_server.hpp @@ -230,9 +230,8 @@ public: private: explicit Service(void); Error SetFullName(const char *aFullName) { return mFullName.Set(aFullName); } - Error SetTxtData(const uint8_t *aTxtData, uint16_t aTxtDataLength); Error SetTxtDataFromMessage(const Message &aMessage, uint16_t aOffset, uint16_t aLength); - Error CopyResourcesFrom(const Service &aService); + void TakeResourcesFrom(Service &aService); void ClearResources(void); HeapString mFullName;