diff --git a/include/openthread/instance.h b/include/openthread/instance.h index 1a45a97e2..f917e7f77 100644 --- a/include/openthread/instance.h +++ b/include/openthread/instance.h @@ -53,7 +53,7 @@ extern "C" { * @note This number versions both OpenThread platform and user APIs. * */ -#define OPENTHREAD_API_VERSION (207) +#define OPENTHREAD_API_VERSION (208) /** * @addtogroup api-instance diff --git a/include/openthread/srp_server.h b/include/openthread/srp_server.h index 1eceb2b4d..c9fd220e8 100644 --- a/include/openthread/srp_server.h +++ b/include/openthread/srp_server.h @@ -167,6 +167,32 @@ typedef struct otSrpServerLeaseConfig uint32_t mMaxKeyLease; ///< The maximum KEY-LEASE interval in seconds. } otSrpServerLeaseConfig; +/** + * This structure includes SRP server lease information of a host/service. + * + */ +typedef struct otSrpServerLeaseInfo +{ + uint32_t mLease; ///< The lease time of a host/service in milliseconds. + uint32_t mKeyLease; ///< The key lease time of a host/service in milliseconds. + uint32_t mRemainingLease; ///< The remaining lease time of the host/service in milliseconds. + uint32_t mRemainingKeyLease; ///< The remaining key lease time of a host/service in milliseconds. +} otSrpServerLeaseInfo; + +/** + * This structure includes the statistics of SRP server responses. + * + */ +typedef struct otSrpServerResponseCounters +{ + uint32_t mSuccess; ///< The number of successful responses. + uint32_t mServerFailure; ///< The number of server failure responses. + uint32_t mFormatError; ///< The number of format error responses. + uint32_t mNameExists; ///< The number of 'name exists' responses. + uint32_t mRefused; ///< The number of refused responses. + uint32_t mOther; ///< The number of other responses. +} otSrpServerResponseCounters; + /** * This function returns the domain authorized to the SRP server. * @@ -207,6 +233,16 @@ otError otSrpServerSetDomain(otInstance *aInstance, const char *aDomain); */ otSrpServerState otSrpServerGetState(otInstance *aInstance); +/** + * This function returns the port the SRP server is listening to. + * + * @param[in] aInstance A pointer to an OpenThread instance. + * + * @returns The port of the SRP server. It returns 0 if the server is not running. + * + */ +uint16_t otSrpServerGetPort(otInstance *aInstance); + /** * This function returns the address mode being used by the SRP server. * @@ -365,6 +401,16 @@ void otSrpServerHandleServiceUpdateResult(otInstance *aInstance, otSrpServerServ */ const otSrpServerHost *otSrpServerGetNextHost(otInstance *aInstance, const otSrpServerHost *aHost); +/** + * This function returns the response counters of the SRP server. + * + * @param[in] aInstance A pointer to an OpenThread instance. + * + * @returns A pointer to the response counters of the SRP server. + * + */ +const otSrpServerResponseCounters *otSrpServerGetResponseCounters(otInstance *aInstance); + /** * This function tells if the SRP service host has been deleted. * @@ -399,6 +445,15 @@ const char *otSrpServerHostGetFullName(const otSrpServerHost *aHost); */ const otIp6Address *otSrpServerHostGetAddresses(const otSrpServerHost *aHost, uint8_t *aAddressesNum); +/** + * This function returns the LEASE and KEY-LEASE information of a given host. + * + * @param[in] aHost A pointer to the SRP server host. + * @param[out] aLeaseInfo A pointer to where to output the LEASE and KEY-LEASE information. + * + */ +void otSrpServerHostGetLeaseInfo(const otSrpServerHost *aHost, otSrpServerLeaseInfo *aLeaseInfo); + /** * This function returns the next service (excluding any sub-type services) of given host. * @@ -583,6 +638,14 @@ const uint8_t *otSrpServerServiceGetTxtData(const otSrpServerService *aService, */ const otSrpServerHost *otSrpServerServiceGetHost(const otSrpServerService *aService); +/** + * This function returns the LEASE and KEY-LEASE information of a given service. + * + * @param[in] aService A pointer to the SRP server service. + * @param[out] aLeaseInfo A pointer to where to output the LEASE and KEY-LEASE information. + * + */ +void otSrpServerServiceGetLeaseInfo(const otSrpServerService *aService, otSrpServerLeaseInfo *aLeaseInfo); /** * @} * diff --git a/src/core/api/srp_server_api.cpp b/src/core/api/srp_server_api.cpp index 8c59319a6..950f25e3b 100644 --- a/src/core/api/srp_server_api.cpp +++ b/src/core/api/srp_server_api.cpp @@ -57,6 +57,11 @@ otSrpServerState otSrpServerGetState(otInstance *aInstance) return MapEnum(AsCoreType(aInstance).Get().GetState()); } +uint16_t otSrpServerGetPort(otInstance *aInstance) +{ + return AsCoreType(aInstance).Get().GetPort(); +} + otSrpServerAddressMode otSrpServerGetAddressMode(otInstance *aInstance) { return MapEnum(AsCoreType(aInstance).Get().GetAddressMode()); @@ -109,6 +114,11 @@ const otSrpServerHost *otSrpServerGetNextHost(otInstance *aInstance, const otSrp return AsCoreType(aInstance).Get().GetNextHost(AsCoreTypePtr(aHost)); } +const otSrpServerResponseCounters *otSrpServerGetResponseCounters(otInstance *aInstance) +{ + return AsCoreType(aInstance).Get().GetResponseCounters(); +} + bool otSrpServerHostIsDeleted(const otSrpServerHost *aHost) { return AsCoreType(aHost).IsDeleted(); @@ -124,6 +134,16 @@ const otIp6Address *otSrpServerHostGetAddresses(const otSrpServerHost *aHost, ui return AsCoreType(aHost).GetAddresses(*aAddressesNum); } +void otSrpServerHostGetLeaseInfo(const otSrpServerHost *aHost, otSrpServerLeaseInfo *aLeaseInfo) +{ + AsCoreType(aHost).GetLeaseInfo(*aLeaseInfo); +} + +uint32_t otSrpServerHostGetKeyLease(const otSrpServerHost *aHost) +{ + return AsCoreType(aHost).GetKeyLease(); +} + const otSrpServerService *otSrpServerHostGetNextService(const otSrpServerHost * aHost, const otSrpServerService *aService) { @@ -196,4 +216,9 @@ const otSrpServerHost *otSrpServerServiceGetHost(const otSrpServerService *aServ return &AsCoreType(aService).GetHost(); } +void otSrpServerServiceGetLeaseInfo(const otSrpServerService *aService, otSrpServerLeaseInfo *aLeaseInfo) +{ + AsCoreType(aService).GetLeaseInfo(*aLeaseInfo); +} + #endif // OPENTHREAD_CONFIG_SRP_SERVER_ENABLE diff --git a/src/core/net/srp_server.cpp b/src/core/net/srp_server.cpp index b3d59f555..9621ff9e3 100644 --- a/src/core/net/srp_server.cpp +++ b/src/core/net/srp_server.cpp @@ -1236,6 +1236,8 @@ void Server::SendResponse(const Dns::UpdateHeader & aHeader, LogInfo("Send success response"); } + UpdateResponseCounters(aResponseCode); + exit: if (error != kErrorNone) { @@ -1283,6 +1285,8 @@ void Server::SendResponse(const Dns::UpdateHeader &aHeader, LogInfo("Send success response with granted lease: %u and key lease: %u", aLease, aKeyLease); + UpdateResponseCounters(Dns::UpdateHeader::kResponseSuccess); + exit: if (error != kErrorNone) { @@ -1482,6 +1486,31 @@ const char *Server::AddressModeToString(AddressMode aMode) return kAddressModeStrings[aMode]; } +void Server::UpdateResponseCounters(Dns::UpdateHeader::Response aResponseCode) +{ + switch (aResponseCode) + { + case Dns::UpdateHeader::kResponseSuccess: + ++mResponseCounters.mSuccess; + break; + case Dns::UpdateHeader::kResponseServerFailure: + ++mResponseCounters.mServerFailure; + break; + case Dns::UpdateHeader::kResponseFormatError: + ++mResponseCounters.mFormatError; + break; + case Dns::UpdateHeader::kResponseNameExists: + ++mResponseCounters.mNameExists; + break; + case Dns::UpdateHeader::kResponseRefused: + ++mResponseCounters.mRefused; + break; + default: + ++mResponseCounters.mOther; + break; + } +} + //--------------------------------------------------------------------------------------------------------------------- // Server::Service @@ -1540,6 +1569,18 @@ TimeMilli Server::Service::GetKeyExpireTime(void) const return mUpdateTime + Time::SecToMsec(mDescription->mKeyLease); } +void Server::Service::GetLeaseInfo(LeaseInfo &aLeaseInfo) const +{ + TimeMilli now = TimerMilli::GetNow(); + TimeMilli expireTime = GetExpireTime(); + TimeMilli keyExpireTime = GetKeyExpireTime(); + + aLeaseInfo.mLease = Time::SecToMsec(GetLease()); + aLeaseInfo.mKeyLease = Time::SecToMsec(GetKeyLease()); + aLeaseInfo.mRemainingLease = (now <= expireTime) ? (expireTime - now) : 0; + aLeaseInfo.mRemainingKeyLease = (now <= keyExpireTime) ? (keyExpireTime - now) : 0; +} + bool Server::Service::MatchesInstanceName(const char *aInstanceName) const { return StringMatch(mDescription->mInstanceName.AsCString(), aInstanceName, kStringCaseInsensitiveMatch); @@ -1739,6 +1780,18 @@ TimeMilli Server::Host::GetKeyExpireTime(void) const return mUpdateTime + Time::SecToMsec(mKeyLease); } +void Server::Host::GetLeaseInfo(LeaseInfo &aLeaseInfo) const +{ + TimeMilli now = TimerMilli::GetNow(); + TimeMilli expireTime = GetExpireTime(); + TimeMilli keyExpireTime = GetKeyExpireTime(); + + aLeaseInfo.mLease = Time::SecToMsec(GetLease()); + aLeaseInfo.mKeyLease = Time::SecToMsec(GetKeyLease()); + aLeaseInfo.mRemainingLease = (now <= expireTime) ? (expireTime - now) : 0; + aLeaseInfo.mRemainingKeyLease = (now <= keyExpireTime) ? (keyExpireTime - now) : 0; +} + const Server::Service *Server::Host::FindNextService(const Service *aPrevService, Service::Flags aFlags, const char * aServiceName, diff --git a/src/core/net/srp_server.hpp b/src/core/net/srp_server.hpp index c74eb07be..6289f3a2a 100644 --- a/src/core/net/srp_server.hpp +++ b/src/core/net/srp_server.hpp @@ -129,6 +129,12 @@ public: */ typedef otSrpServerServiceUpdateId ServiceUpdateId; + /** + * The SRP server lease information of a host/service. + * + */ + typedef otSrpServerLeaseInfo LeaseInfo; + /** * This enumeration represents the address mode used by the SRP server. * @@ -301,6 +307,22 @@ public: */ const Host &GetHost(void) const { return *mDescription->mHost; } + /** + * This method returns the LEASE time of the service. + * + * @returns The LEASE time in seconds. + * + */ + uint32_t GetLease(void) const { return mDescription->mLease; } + + /** + * This method returns the KEY-LEASE time of the key of the service. + * + * @returns The KEY-LEASE time in seconds. + * + */ + uint32_t GetKeyLease(void) const { return mDescription->mKeyLease; } + /** * This method returns the expire time (in milliseconds) of the service. * @@ -317,6 +339,15 @@ public: */ TimeMilli GetKeyExpireTime(void) const; + /** + * This method 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; + /** * This method indicates whether this service matches a given service instance name. * @@ -452,6 +483,15 @@ public: */ uint32_t GetKeyLease(void) const { return mKeyLease; } + /** + * This method 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; + /** * This method returns the KEY resource record of the host. * @@ -704,6 +744,14 @@ public: */ State GetState(void) const { return mState; } + /** + * This method tells the port the SRP server is listening to. + * + * @returns An integer that represents the port of the server. It returns 0 if the SRP server is not running. + * + */ + uint16_t GetPort(void) const { return IsRunning() ? mPort : 0; } + /** * This method enables/disables the SRP server. * @@ -745,6 +793,14 @@ public: */ const Host *GetNextHost(const Host *aHost); + /** + * This method returns the response counters of the SRP server. + * + * @returns A pointer to the response counters of the SRP server. + * + */ + const otSrpServerResponseCounters *GetResponseCounters(void) const { return &mResponseCounters; } + /** * This method receives the service update result from service handler set by * SetServiceHandler. @@ -886,6 +942,8 @@ private: const UpdateMetadata *FindOutstandingUpdate(const MessageMetadata &aMessageMetadata) const; static const char * AddressModeToString(AddressMode aMode); + void UpdateResponseCounters(Dns::Header::Response aResponseCode); + Ip6::Udp::Socket mSocket; otSrpServerServiceUpdateHandler mServiceUpdateHandler; void * mServiceUpdateHandlerContext; @@ -906,6 +964,8 @@ private: AddressMode mAddressMode; uint8_t mAnycastSequenceNumber; bool mHasRegisteredAnyService : 1; + + otSrpServerResponseCounters mResponseCounters; }; } // namespace Srp