From 9b887f6bd1437170fc9bcb726f32185ccb3cb841 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Thu, 5 Mar 2026 14:54:39 -0800 Subject: [PATCH] [srp-server] introduce `LeaseTracker` base class for `Host` and `Service` (#12628) This commit extracts the common lease tracking variables (`mLease`, `mKeyLease`, `mTtl`, and `mUpdateTime`) and their associated methods from the `Srp::Server::Host` and `Srp::Server::Service` classes into a new shared base class, `LeaseTracker`. By having both `Host` and `Service` inherit from `LeaseTracker`, we eliminate duplicated logic for calculating expiration times, handling lease info, and processing TTL updates. This refactoring simplifies the SRP server codebase and ensures consistent lease management behavior across both entities. --- src/core/net/srp_advertising_proxy.cpp | 14 +-- src/core/net/srp_server.cpp | 154 +++++++++-------------- src/core/net/srp_server.hpp | 166 ++++++++++--------------- 3 files changed, 134 insertions(+), 200 deletions(-) diff --git a/src/core/net/srp_advertising_proxy.cpp b/src/core/net/srp_advertising_proxy.cpp index e31657f43..b40abf408 100644 --- a/src/core/net/srp_advertising_proxy.cpp +++ b/src/core/net/srp_advertising_proxy.cpp @@ -259,7 +259,7 @@ void AdvertisingProxy::AdvertiseRemovalOf(Host &aHost) { service->mShouldAdvertise = true; - if (aHost.mKeyLease == 0) + if (aHost.GetKeyLease() == 0) { advService.mIsKeyRegistered = false; } @@ -270,7 +270,7 @@ void AdvertisingProxy::AdvertiseRemovalOf(Host &aHost) advService.mIsReplaced = true; } - if (aHost.mKeyLease == 0) + if (aHost.GetKeyLease() == 0) { advHost.mIsKeyRegistered = false; } @@ -291,7 +291,7 @@ void AdvertisingProxy::AdvertiseRemovalOf(Host &aHost) UnregisterService(service); } - if (aHost.mKeyLease == 0) + if (aHost.GetKeyLease() == 0) { UnregisterKey(service); } @@ -302,7 +302,7 @@ void AdvertisingProxy::AdvertiseRemovalOf(Host &aHost) UnregisterHost(aHost); } - if (aHost.mKeyLease == 0) + if (aHost.GetKeyLease() == 0) { UnregisterKey(aHost); } @@ -352,7 +352,7 @@ void AdvertisingProxy::AdvertiseRemovalOf(Service &aService) UnregisterService(aService); } - if (aService.mKeyLease == 0) + if (aService.GetKeyLease() == 0) { UnregisterKey(aService); } @@ -473,7 +473,7 @@ exit: void AdvertisingProxy::Advertise(Host &aHost) { bool shouldUnregisterHostAndServices = aHost.IsDeleted(); - bool shouldUnregisterKeys = (aHost.mKeyLease == 0); + bool shouldUnregisterKeys = (aHost.GetKeyLease() == 0); DecideToAdvertise(aHost, shouldUnregisterHostAndServices, shouldUnregisterKeys); @@ -817,7 +817,7 @@ bool AdvertisingProxy::CompareAndUpdateHost(Host &aHost, Host &aExistingHost) // we need to unregister keys for any services on // existing host that are not present in `aHost`. - if (aHost.mKeyLease == 0) + if (aHost.GetKeyLease() == 0) { for (Service &existingService : aExistingHost.mServices) { diff --git a/src/core/net/srp_server.cpp b/src/core/net/srp_server.cpp index b418e7074..6b72cd507 100644 --- a/src/core/net/srp_server.cpp +++ b/src/core/net/srp_server.cpp @@ -454,7 +454,7 @@ void Server::RemoveHost(Host *aHost, RetainName aRetainName) { VerifyOrExit(aHost != nullptr); - aHost->mLease = 0; + aHost->SetLease(0); aHost->ClearResources(); if (aRetainName) @@ -463,7 +463,7 @@ void Server::RemoveHost(Host *aHost, RetainName aRetainName) } else { - aHost->mKeyLease = 0; + aHost->SetKeyLease(0); IgnoreError(mHosts.Remove(*aHost)); LogInfo("Fully remove host %s", aHost->GetFullName()); } @@ -642,9 +642,10 @@ void Server::CommitSrpUpdate(Error aError, for (Service &service : aHost.mServices) { - service.mLease = grantedLease; - service.mKeyLease = grantedKeyLease; - service.mTtl = grantedTtl; + service.SetLease(service.mIsDeleted ? 0 : grantedLease); + service.SetKeyLease(grantedKeyLease); + service.SetTtl(grantedTtl); + service.mIsCommitted = true; #if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_INFO) @@ -1311,7 +1312,7 @@ Error Server::ProcessServiceDescriptionInstructions(Host &aHost, VerifyOrExit(!service->mParsedSrv, error = kErrorParse); service->mParsedSrv = true; - service->mTtl = srvRecord.GetTtl(); + service->SetTtl(srvRecord.GetTtl()); service->mPriority = srvRecord.GetPriority(); service->mWeight = srvRecord.GetWeight(); service->mPort = srvRecord.GetPort(); @@ -1395,8 +1396,8 @@ Error Server::ProcessAdditionalSection(Host *aHost, const Message &aMessage, Mes service.mIsDeleted = true; } - service.mLease = service.mIsDeleted ? 0 : leaseOption.GetLeaseInterval(); - service.mKeyLease = leaseOption.GetKeyLeaseInterval(); + service.SetLease(service.mIsDeleted ? 0 : leaseOption.GetLeaseInterval()); + service.SetKeyLease(leaseOption.GetKeyLeaseInterval()); } // If the client included the short variant of Lease Option, @@ -1525,7 +1526,7 @@ void Server::HandleUpdate(Host &aHost, const MessageMetadata &aMetadata) SuccessOrExit(error = service->mServiceName.Set(existingService.GetServiceName())); service->mIsDeleted = true; - service->mKeyLease = existingService.mKeyLease; + service->SetKeyLease(existingService.GetKeyLease()); } exit: @@ -1940,6 +1941,51 @@ exit: } #endif +//--------------------------------------------------------------------------------------------------------------------- +// Server::LeaseTracker + +void Server::LeaseTracker::Init(TimeMilli aUpdateTime) +{ + mLease = 0; + mKeyLease = 0; + mTtl = 0; + mUpdateTime = aUpdateTime; +} + +TimeMilli Server::LeaseTracker::GetExpireTime(void) const { return mUpdateTime + Time::SecToMsec(mLease); } + +TimeMilli Server::LeaseTracker::GetKeyExpireTime(void) const { return mUpdateTime + Time::SecToMsec(mKeyLease); } + +void Server::LeaseTracker::GetLeaseInfo(LeaseInfo &aLeaseInfo) const +{ + TimeMilli now = TimerMilli::GetNow(); + + aLeaseInfo.mLease = Time::SecToMsec(mLease); + aLeaseInfo.mKeyLease = Time::SecToMsec(mKeyLease); + aLeaseInfo.mRemainingLease = (mLease != 0) ? GetExpireTime().DetermineRemainingDurationFrom(now) : 0; + aLeaseInfo.mRemainingKeyLease = GetKeyExpireTime().DetermineRemainingDurationFrom(now); +} + +Error Server::LeaseTracker::ProcessTtl(uint32_t aTtl) +{ + // This method processes the TTL value received in a resource record. + // + // If no TTL value is stored, this method will set the stored value to @p aTtl and return `kErrorNone`. + // If a TTL value is stored and @p aTtl equals the stored value, this method returns `kErrorNone`. + // Otherwise, this method returns `kErrorRejected`. + + Error error = kErrorRejected; + + VerifyOrExit(aTtl && (mTtl == 0 || mTtl == aTtl)); + + mTtl = aTtl; + + error = kErrorNone; + +exit: + return error; +} + //--------------------------------------------------------------------------------------------------------------------- // Server::Service @@ -1947,15 +1993,13 @@ Error Server::Service::Init(const char *aInstanceName, const char *aInstanceLabe { Error error; + LeaseTracker::Init(aUpdateTime); + mNext = nullptr; mHost = &aHost; mPriority = 0; mWeight = 0; - mTtl = 0; mPort = 0; - mLease = 0; - mKeyLease = 0; - mUpdateTime = aUpdateTime; mIsDeleted = false; mIsCommitted = false; #if OPENTHREAD_CONFIG_SRP_SERVER_ADVERTISING_PROXY_ENABLE @@ -2014,34 +2058,6 @@ exit: return error; } -TimeMilli Server::Service::GetExpireTime(void) const -{ - OT_ASSERT(!mIsDeleted); - OT_ASSERT(!GetHost().IsDeleted()); - - return mUpdateTime + Time::SecToMsec(mLease); -} - -TimeMilli Server::Service::GetKeyExpireTime(void) const { return mUpdateTime + Time::SecToMsec(mKeyLease); } - -void Server::Service::GetLeaseInfo(LeaseInfo &aLeaseInfo) const -{ - TimeMilli now = TimerMilli::GetNow(); - - aLeaseInfo.mLease = Time::SecToMsec(GetLease()); - aLeaseInfo.mKeyLease = Time::SecToMsec(GetKeyLease()); - aLeaseInfo.mRemainingKeyLease = GetKeyExpireTime().DetermineRemainingDurationFrom(now); - - if (!mIsDeleted) - { - aLeaseInfo.mRemainingLease = GetExpireTime().DetermineRemainingDurationFrom(now); - } - else - { - aLeaseInfo.mRemainingLease = 0; - } -} - bool Server::Service::MatchesInstanceName(const char *aInstanceName) const { return Matches(aInstanceName); } bool Server::Service::MatchesServiceName(const char *aServiceName) const @@ -2129,10 +2145,6 @@ exit: Server::Host::Host(Instance &aInstance, TimeMilli aUpdateTime) : InstanceLocator(aInstance) , mNext(nullptr) - , mTtl(0) - , mLease(0) - , mKeyLease(0) - , mUpdateTime(aUpdateTime) , mParsedKey(false) , mUseShortLeaseOption(false) #if OPENTHREAD_CONFIG_SRP_SERVER_ADVERTISING_PROXY_ENABLE @@ -2145,6 +2157,7 @@ Server::Host::Host(Instance &aInstance, TimeMilli aUpdateTime) , mKeyAdvId(kInvalidRequestId) #endif { + LeaseTracker::Init(aUpdateTime); } Server::Host::~Host(void) { FreeAllServices(); } @@ -2174,53 +2187,6 @@ bool Server::Host::Matches(const char *aFullName) const return StringMatch(mFullName.AsCString(), aFullName, kStringCaseInsensitiveMatch); } -TimeMilli Server::Host::GetExpireTime(void) const -{ - OT_ASSERT(!IsDeleted()); - - return mUpdateTime + Time::SecToMsec(mLease); -} - -TimeMilli Server::Host::GetKeyExpireTime(void) const { return mUpdateTime + Time::SecToMsec(mKeyLease); } - -void Server::Host::GetLeaseInfo(LeaseInfo &aLeaseInfo) const -{ - TimeMilli now = TimerMilli::GetNow(); - - aLeaseInfo.mLease = Time::SecToMsec(GetLease()); - aLeaseInfo.mKeyLease = Time::SecToMsec(GetKeyLease()); - aLeaseInfo.mRemainingKeyLease = GetKeyExpireTime().DetermineRemainingDurationFrom(now); - - if (!IsDeleted()) - { - aLeaseInfo.mRemainingLease = GetExpireTime().DetermineRemainingDurationFrom(now); - } - else - { - aLeaseInfo.mRemainingLease = 0; - } -} - -Error Server::Host::ProcessTtl(uint32_t aTtl) -{ - // This method processes the TTL value received in a resource record. - // - // If no TTL value is stored, this method will set the stored value to @p aTtl and return `kErrorNone`. - // If a TTL value is stored and @p aTtl equals the stored value, this method returns `kErrorNone`. - // Otherwise, this method returns `kErrorRejected`. - - Error error = kErrorRejected; - - VerifyOrExit(aTtl && (mTtl == 0 || mTtl == aTtl)); - - mTtl = aTtl; - - error = kErrorNone; - -exit: - return error; -} - const Server::Service *Server::Host::GetNextService(const Service *aPrevService) const { return (aPrevService == nullptr) ? mServices.GetHead() : aPrevService->GetNext(); @@ -2252,11 +2218,11 @@ void Server::Host::RemoveService(Service *aService, RetainName aRetainName, Noti VerifyOrExit(aService != nullptr); aService->mIsDeleted = true; - aService->mLease = 0; + aService->SetLease(0); if (!aRetainName) { - aService->mKeyLease = 0; + aService->SetKeyLease(0); } aService->Log(aRetainName ? Service::kRemoveButRetainName : Service::kFullyRemove); diff --git a/src/core/net/srp_server.hpp b/src/core/net/srp_server.hpp index 3dd928c1e..6ce3b20e4 100644 --- a/src/core/net/srp_server.hpp +++ b/src/core/net/srp_server.hpp @@ -177,10 +177,75 @@ public: kStateStopped = OT_SRP_SERVER_STATE_STOPPED, ///< Server is enabled but stopped. }; + /** + * Base class for `Host` and `Service`, tracking lease, key-lease, and expiration times. + */ + class LeaseTracker + { + public: + /** + * Returns the LEASE time. + * + * @returns The LEASE time in seconds. + */ + uint32_t GetLease(void) const { return mLease; } + + /** + * Returns the KEY-LEASE time. + * + * @returns The KEY-LEASE time in seconds. + */ + uint32_t GetKeyLease(void) const { return mKeyLease; } + + /** + * Returns the TTL. + * + * @returns The TTL. + */ + uint32_t GetTtl(void) const { return mTtl; } + + /** + * Returns the expire time. + * + * @returns The expire time + */ + TimeMilli GetExpireTime(void) const; + + /** + * Returns the key expire time. + * + * @returns The service key expire time. + */ + TimeMilli GetKeyExpireTime(void) const; + + /** + * Gets the LEASE and KEY-LEASE information. + * + * @param[out] aLeaseInfo A reference to a LeaseInfo instance to populate + */ + void GetLeaseInfo(LeaseInfo &aLeaseInfo) const; + + protected: + LeaseTracker(void) = default; + + void Init(TimeMilli aUpdateTime); + void SetTtl(uint32_t aTtl) { mTtl = aTtl; } + void SetLease(uint32_t aLease) { mLease = aLease; } + void SetKeyLease(uint32_t aKeyLease) { mKeyLease = aKeyLease; } + Error ProcessTtl(uint32_t aTtl); + + private: + uint32_t mLease; + uint32_t mKeyLease; + uint32_t mTtl; + TimeMilli mUpdateTime; + }; + /** * Implements a server-side SRP service. */ class Service : public otSrpServerService, + public LeaseTracker, public LinkedListEntry, private Heap::Allocatable, private NonCopyable @@ -268,13 +333,6 @@ public: */ static Error ParseSubTypeServiceName(const char *aSubTypeServiceName, char *aLabel, uint8_t aLabelSize); - /** - * Returns the TTL of the service instance. - * - * @returns The TTL of the service instance. - */ - uint32_t GetTtl(void) const { return mTtl; } - /** * Returns the port of the service instance. * @@ -317,42 +375,6 @@ public: */ const Host &GetHost(void) const { return *mHost; } - /** - * Returns the LEASE time of the service. - * - * @returns The LEASE time in seconds. - */ - uint32_t GetLease(void) const { return mLease; } - - /** - * Returns the KEY-LEASE time of the key of the service. - * - * @returns The KEY-LEASE time in seconds. - */ - uint32_t GetKeyLease(void) const { return mKeyLease; } - - /** - * Returns the expire time (in milliseconds) of the service. - * - * @returns The service expire time in milliseconds. - */ - TimeMilli GetExpireTime(void) const; - - /** - * Returns the key expire time (in milliseconds) of the service. - * - * @returns The service key expire time in milliseconds. - */ - TimeMilli GetKeyExpireTime(void) const; - - /** - * Gets the LEASE and KEY-LEASE information of a given service. - * - * @param[out] aLeaseInfo A reference to a LeaseInfo instance. It contains the LEASE time, KEY-LEASE time, - * remaining LEASE time and the remaining KEY-LEASE time. - */ - void GetLeaseInfo(LeaseInfo &aLeaseInfo) const; - /** * Indicates whether this service matches a given service instance name. * @@ -406,10 +428,6 @@ public: uint16_t mPriority; uint16_t mWeight; uint16_t mPort; - uint32_t mTtl; // In seconds - uint32_t mLease; // In seconds - uint32_t mKeyLease; // In seconds - TimeMilli mUpdateTime; bool mIsDeleted : 1; bool mIsCommitted : 1; bool mParsedDeleteAllRrset : 1; @@ -431,6 +449,7 @@ public: */ class Host : public otSrpServerHost, public InstanceLocator, + public LeaseTracker, public LinkedListEntry, private Heap::Allocatable, private NonCopyable @@ -451,7 +470,7 @@ public: * * @returns TRUE if the host is deleted, FALSE if the host is not deleted. */ - bool IsDeleted(void) const { return (mLease == 0); } + bool IsDeleted(void) const { return (GetLease() == 0); } /** * Returns the full name of the host. @@ -474,35 +493,6 @@ public: return mAddresses.AsCArray(); } - /** - * Returns the TTL of the host. - * - * @returns The TTL of the host. - */ - uint32_t GetTtl(void) const { return mTtl; } - - /** - * Returns the LEASE time of the host. - * - * @returns The LEASE time in seconds. - */ - uint32_t GetLease(void) const { return mLease; } - - /** - * Returns the KEY-LEASE time of the key of the host. - * - * @returns The KEY-LEASE time in seconds. - */ - uint32_t GetKeyLease(void) const { return mKeyLease; } - - /** - * Gets the LEASE and KEY-LEASE information of a given host. - * - * @param[out] aLeaseInfo A reference to a LeaseInfo instance. It contains the LEASE time, KEY-LEASE time, - * remaining LEASE time and the remaining KEY-LEASE time. - */ - void GetLeaseInfo(LeaseInfo &aLeaseInfo) const; - /** * Returns the key associated with this host. * @@ -510,20 +500,6 @@ public: */ const Key &GetKey(void) const { return mKey; } - /** - * Returns the expire time (in milliseconds) of the host. - * - * @returns The expire time in milliseconds. - */ - TimeMilli GetExpireTime(void) const; - - /** - * Returns the expire time (in milliseconds) of the key of the host. - * - * @returns The expire time of the key in milliseconds. - */ - TimeMilli GetKeyExpireTime(void) const; - /** * Returns the `Service` linked list associated with the host. * @@ -554,12 +530,8 @@ public: ~Host(void); Error SetFullName(const char *aFullName); - void SetTtl(uint32_t aTtl) { mTtl = aTtl; } - void SetLease(uint32_t aLease) { mLease = aLease; } - void SetKeyLease(uint32_t aKeyLease) { mKeyLease = aKeyLease; } void SetUseShortLeaseOption(bool aUse) { mUseShortLeaseOption = aUse; } bool ShouldUseShortLeaseOption(void) const { return mUseShortLeaseOption; } - Error ProcessTtl(uint32_t aTtl); Service *AddNewService(const char *aInstanceName, const char *aInstanceLabel, TimeMilli aUpdateTime); void AddService(Service &aService); @@ -575,10 +547,6 @@ public: Heap::String mFullName; Heap::Array mAddresses; Key mKey; - uint32_t mTtl; // The TTL in seconds. - uint32_t mLease; // The LEASE time in seconds. - uint32_t mKeyLease; // The KEY-LEASE time in seconds. - TimeMilli mUpdateTime; LinkedList mServices; bool mParsedKey : 1; bool mUseShortLeaseOption : 1; // Use short lease option (lease only 4 bytes).