diff --git a/src/core/net/srp_server.cpp b/src/core/net/srp_server.cpp index c42eed71e..7bdc091cf 100644 --- a/src/core/net/srp_server.cpp +++ b/src/core/net/srp_server.cpp @@ -80,7 +80,6 @@ Server::Server(Instance &aInstance) , mSocket(aInstance) , mServiceUpdateHandler(nullptr) , mServiceUpdateHandlerContext(nullptr) - , mDomain(nullptr) , mLeaseTimer(aInstance, HandleLeaseTimer) , mOutstandingUpdatesTimer(aInstance, HandleOutstandingUpdatesTimer) , mServiceUpdateId(Random::NonCrypto::GetUint32()) @@ -90,11 +89,6 @@ Server::Server(Instance &aInstance) IgnoreError(SetDomain(kDefaultDomain)); } -Server::~Server(void) -{ - Instance::HeapFree(mDomain); -} - void Server::SetServiceHandler(otSrpServerServiceUpdateHandler aServiceHandler, void *aServiceHandlerContext) { mServiceUpdateHandler = aServiceHandler; @@ -173,36 +167,34 @@ exit: Error Server::SetDomain(const char *aDomain) { - Error error = kErrorNone; - char * buf = nullptr; - size_t appendTrailingDot = 0; - size_t length = strlen(aDomain); + Error error = kErrorNone; + uint16_t length; VerifyOrExit(!mEnabled, error = kErrorInvalidState); - VerifyOrExit(length > 0 && length < Dns::Name::kMaxNameSize, error = kErrorInvalidArgs); - if (aDomain[length - 1] != '.') + length = StringLength(aDomain, Dns::Name::kMaxNameSize); + VerifyOrExit((length > 0) && (length < Dns::Name::kMaxNameSize), error = kErrorInvalidArgs); + + if (aDomain[length - 1] == '.') { - appendTrailingDot = 1; + error = mDomain.Set(aDomain); } - - buf = static_cast(Instance::HeapCAlloc(1, length + appendTrailingDot + 1)); - VerifyOrExit(buf != nullptr, error = kErrorNoBufs); - - strcpy(buf, aDomain); - if (appendTrailingDot) + else { + // Need to append dot at the end + + char buf[Dns::Name::kMaxNameSize]; + + VerifyOrExit(length < Dns::Name::kMaxNameSize - 1, error = kErrorInvalidArgs); + + memcpy(buf, aDomain, length); buf[length] = '.'; buf[length + 1] = '\0'; + + error = mDomain.Set(buf); } - Instance::HeapFree(mDomain); - mDomain = buf; exit: - if (error != kErrorNone) - { - Instance::HeapFree(buf); - } return error; } @@ -228,13 +220,13 @@ void Server::RemoveHost(Host *aHost, bool aRetainName, bool aNotifyServiceHandle if (aRetainName) { - otLogInfoSrp("[server] remove host '%s' (but retain its name)", aHost->mFullName); + otLogInfoSrp("[server] remove host '%s' (but retain its name)", aHost->GetFullName()); } else { aHost->mKeyLease = 0; IgnoreError(mHosts.Remove(*aHost)); - otLogInfoSrp("[server] fully remove host '%s'", aHost->mFullName); + otLogInfoSrp("[server] fully remove host '%s'", aHost->GetFullName()); } if (aNotifyServiceHandler && mServiceUpdateHandler != nullptr) @@ -286,7 +278,7 @@ bool Server::HasNameConflictsWith(Host &aHost) const // Check not only services of this host but all hosts. while ((service = aHost.GetNextService(service)) != nullptr) { - const Service *existingService = FindService(service->mFullName); + const Service *existingService = FindService(service->GetFullName()); if (existingService != nullptr && *service->GetHost().GetKey() != *existingService->GetHost().GetKey()) { ExitNow(hasConflicts = true); @@ -383,7 +375,7 @@ void Server::CommitSrpUpdate(Error aError, existingHost->CopyResourcesFrom(aHost); while ((service = aHost.GetNextService(service)) != nullptr) { - Service *existingService = existingHost->FindService(service->mFullName); + Service *existingService = existingHost->FindService(service->GetFullName()); if (service->mIsDeleted) { @@ -391,12 +383,12 @@ void Server::CommitSrpUpdate(Error aError, } else { - Service *newService = existingHost->AddService(service->mFullName); + Service *newService = existingHost->AddService(service->GetFullName()); VerifyOrExit(newService != nullptr, aError = kErrorNoBufs); SuccessOrExit(aError = newService->CopyResourcesFrom(*service)); otLogInfoSrp("[server] %s service %s", (existingService != nullptr) ? "update existing" : "add new", - service->mFullName); + service->GetFullName()); } } @@ -1030,7 +1022,7 @@ void Server::HandleUpdate(const Dns::UpdateHeader &aDnsHeader, Host *aHost, cons { if (!existingService->mIsDeleted) { - Service *service = aHost->AddService(existingService->mFullName); + Service *service = aHost->AddService(existingService->GetFullName()); VerifyOrExit(service != nullptr, error = kErrorNoBufs); service->mIsDeleted = true; } @@ -1216,7 +1208,7 @@ void Server::HandleLeaseTimer(void) if (service->GetKeyExpireTime() <= now) { - otLogInfoSrp("[server] KEY LEASE of service %s expired", service->mFullName); + otLogInfoSrp("[server] KEY LEASE of service %s expired", service->GetFullName()); host->RemoveService(service, /* aRetainName */ false, /* aNotifyServiceHandler */ true); } else @@ -1264,7 +1256,7 @@ void Server::HandleLeaseTimer(void) } else if (service->GetExpireTime() <= now) { - otLogInfoSrp("[server] LEASE of service %s expired", service->mFullName); + otLogInfoSrp("[server] LEASE of service %s expired", service->GetFullName()); // The service is expired, delete it. host->RemoveService(service, /* aRetainName */ true, /* aNotifyServiceHandler */ true); @@ -1332,14 +1324,13 @@ exit: void Server::Service::Free(void) { - Instance::HeapFree(mFullName); + mFullName.Free(); Instance::HeapFree(mTxtData); Instance::HeapFree(this); } Server::Service::Service(void) - : mFullName(nullptr) - , mPriority(0) + : mPriority(0) , mWeight(0) , mPort(0) , mTxtLength(0) @@ -1350,23 +1341,6 @@ Server::Service::Service(void) { } -Error Server::Service::SetFullName(const char *aFullName) -{ - OT_ASSERT(aFullName != nullptr); - - Error error = kErrorNone; - char *nameCopy = static_cast(Instance::HeapCAlloc(1, strlen(aFullName) + 1)); - - VerifyOrExit(nameCopy != nullptr, error = kErrorNoBufs); - strcpy(nameCopy, aFullName); - - Instance::HeapFree(mFullName); - mFullName = nameCopy; - -exit: - return error; -} - TimeMilli Server::Service::GetExpireTime(void) const { OT_ASSERT(!mIsDeleted); @@ -1450,23 +1424,18 @@ exit: return error; } -bool Server::Service::Matches(const char *aFullName) const -{ - return (mFullName != nullptr) && (strcmp(mFullName, aFullName) == 0); -} - bool Server::Service::MatchesServiceName(const char *aServiceName) const { - uint8_t i = static_cast(strlen(mFullName)); + uint8_t i = static_cast(strlen(GetFullName())); uint8_t j = static_cast(strlen(aServiceName)); - while (i > 0 && j > 0 && mFullName[i - 1] == aServiceName[j - 1]) + while (i > 0 && j > 0 && GetFullName()[i - 1] == aServiceName[j - 1]) { i--; j--; } - return j == 0 && i > 0 && mFullName[i - 1] == '.'; + return j == 0 && i > 0 && GetFullName()[i - 1] == '.'; } Server::Host *Server::Host::New(Instance &aInstance) @@ -1486,13 +1455,12 @@ exit: void Server::Host::Free(void) { FreeAllServices(); - Instance::HeapFree(mFullName); + mFullName.Free(); Instance::HeapFree(this); } Server::Host::Host(Instance &aInstance) : InstanceLocator(aInstance) - , mFullName(nullptr) , mAddressesNum(0) , mNext(nullptr) , mLease(0) @@ -1502,26 +1470,6 @@ Server::Host::Host(Instance &aInstance) mKey.Clear(); } -Error Server::Host::SetFullName(const char *aFullName) -{ - OT_ASSERT(aFullName != nullptr); - - Error error = kErrorNone; - char *nameCopy = static_cast(Instance::HeapCAlloc(1, strlen(aFullName) + 1)); - - VerifyOrExit(nameCopy != nullptr, error = kErrorNoBufs); - strcpy(nameCopy, aFullName); - - if (mFullName != nullptr) - { - Instance::HeapFree(mFullName); - } - mFullName = nameCopy; - -exit: - return error; -} - void Server::Host::SetKey(Dns::Ecdsa256KeyRecord &aKey) { OT_ASSERT(aKey.IsValid()); @@ -1571,11 +1519,11 @@ void Server::Host::RemoveService(Service *aService, bool aRetainName, bool aNoti if (aRetainName) { aService->ClearResources(); - otLogInfoSrp("[server] remove service '%s' (but retain its name)", aService->mFullName); + otLogInfoSrp("[server] remove service '%s' (but retain its name)", aService->GetFullName()); } else { - otLogInfoSrp("[server] fully remove service '%s'", aService->mFullName); + otLogInfoSrp("[server] fully remove service '%s'", aService->GetFullName()); } if (aNotifyServiceHandler && server.mServiceUpdateHandler != nullptr) @@ -1665,11 +1613,6 @@ exit: return error; } -bool Server::Host::Matches(const char *aName) const -{ - return mFullName != nullptr && strcmp(mFullName, aName) == 0; -} - Server::UpdateMetadata *Server::UpdateMetadata::New(Instance & aInstance, const Dns::UpdateHeader &aHeader, Host * aHost, diff --git a/src/core/net/srp_server.hpp b/src/core/net/srp_server.hpp index e16f5a7a2..e6d5931bc 100644 --- a/src/core/net/srp_server.hpp +++ b/src/core/net/srp_server.hpp @@ -50,6 +50,7 @@ #include #include "common/clearable.hpp" +#include "common/heap_string.hpp" #include "common/linked_list.hpp" #include "common/locator.hpp" #include "common/non_copyable.hpp" @@ -137,7 +138,7 @@ public: * @returns A pointer to the null-terminated service name string. * */ - const char *GetFullName(void) const { return mFullName; } + const char *GetFullName(void) const { return mFullName.AsCString(); } /** * This method returns the port of the service instance. @@ -213,7 +214,7 @@ public: * @returns TRUE if the service matches the full name, FALSE if doesn't match. * */ - bool Matches(const char *aFullName) const; + bool Matches(const char *aFullName) const { return (mFullName == aFullName); } /** * This method tells whether this service matches a given service name .. @@ -228,13 +229,13 @@ public: private: explicit Service(void); - Error SetFullName(const char *aFullName); + 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 ClearResources(void); - char * mFullName; + HeapString mFullName; uint16_t mPriority; uint16_t mWeight; uint16_t mPort; @@ -291,7 +292,7 @@ public: * @returns A pointer to the null-terminated full host name. * */ - const char *GetFullName(void) const { return mFullName; } + const char *GetFullName(void) const { return mFullName.AsCString(); } /** * This method returns addresses of the host. @@ -369,7 +370,7 @@ public: * @returns A boolean that indicates whether the host matches the given name. * */ - bool Matches(const char *aName) const; + bool Matches(const char *aFullName) const { return (mFullName == aFullName); } private: enum : uint8_t @@ -378,7 +379,7 @@ public: }; explicit Host(Instance &aInstance); - Error SetFullName(const char *aFullName); + Error SetFullName(const char *aFullName) { return mFullName.Set(aFullName); } void SetKey(Dns::Ecdsa256KeyRecord &aKey); void SetLease(uint32_t aLease) { mLease = aLease; } void SetKeyLease(uint32_t aKeyLease) { mKeyLease = aKeyLease; } @@ -392,7 +393,7 @@ public: const Service *FindService(const char *aFullName) const; Error AddIp6Address(const Ip6::Address &aIp6Address); - char * mFullName; + HeapString mFullName; Ip6::Address mAddresses[kMaxAddressesNum]; uint8_t mAddressesNum; Host * mNext; @@ -432,7 +433,6 @@ public: * */ explicit Server(Instance &aInstance); - ~Server(void); /** * This method sets the SRP service events handler. @@ -457,7 +457,7 @@ public: * @returns A pointer to the dot-joined domain string. * */ - const char *GetDomain(void) const { return mDomain; } + const char *GetDomain(void) const { return mDomain.AsCString(); } /** * This method sets the domain on the SRP server. @@ -662,7 +662,7 @@ private: otSrpServerServiceUpdateHandler mServiceUpdateHandler; void * mServiceUpdateHandlerContext; - char *mDomain; + HeapString mDomain; LeaseConfig mLeaseConfig;