[srp-server] add API for getting LEASE and KEY-LEASE (#6517)

This commit is contained in:
kangping
2021-05-07 21:26:36 -07:00
committed by GitHub
parent e99d16e81c
commit 6f2b818381
9 changed files with 183 additions and 85 deletions
+1 -1
View File
@@ -53,7 +53,7 @@ extern "C" {
* @note This number versions both OpenThread platform and user APIs.
*
*/
#define OPENTHREAD_API_VERSION (110)
#define OPENTHREAD_API_VERSION (111)
/**
* @addtogroup api-instance
+42 -28
View File
@@ -74,7 +74,19 @@ typedef void otSrpServerService;
typedef uint32_t otSrpServerServiceUpdateId;
/**
* This method returns the domain authorized to the SRP server.
* This structure includes SRP server LEASE and KEY-LEASE configurations.
*
*/
typedef struct otSrpServerLeaseConfig
{
uint32_t mMinLease; ///< The minimum LEASE interval in seconds.
uint32_t mMaxLease; ///< The maximum LEASE interval in seconds.
uint32_t mMinKeyLease; ///< The minimum KEY-LEASE interval in seconds.
uint32_t mMaxKeyLease; ///< The maximum KEY-LEASE interval in seconds.
} otSrpServerLeaseConfig;
/**
* This function returns the domain authorized to the SRP server.
*
* If the domain if not set by SetDomain, "default.service.arpa." will be returned.
* A trailing dot is always appended even if the domain is set without it.
@@ -87,10 +99,10 @@ typedef uint32_t otSrpServerServiceUpdateId;
const char *otSrpServerGetDomain(otInstance *aInstance);
/**
* This method sets the domain on the SRP server.
* This function sets the domain on the SRP server.
*
* A trailing dot will be appended to @p aDomain if it is not already there.
* This method should only be called before the SRP server is enabled.
* This function should only be called before the SRP server is enabled.
*
* @param[in] aInstance A pointer to an OpenThread instance.
* @param[in] aDomain The domain to be set. MUST NOT be NULL.
@@ -104,7 +116,7 @@ const char *otSrpServerGetDomain(otInstance *aInstance);
otError otSrpServerSetDomain(otInstance *aInstance, const char *aDomain);
/**
* This method enables/disables the SRP server.
* This function enables/disables the SRP server.
*
* @param[in] aInstance A pointer to an OpenThread instance.
* @param[in] aEnabled A boolean to enable/disable the SRP server.
@@ -113,7 +125,16 @@ otError otSrpServerSetDomain(otInstance *aInstance, const char *aDomain);
void otSrpServerSetEnabled(otInstance *aInstance, bool aEnabled);
/**
* This method sets LEASE & KEY-LEASE range that is acceptable by the SRP server.
* This function returns SRP server LEASE and KEY-LEASE configurations.
*
* @param[in] aInstance A pointer to an OpenThread instance.
* @param[out] aLeaseConfig A pointer to an `otSrpServerLeaseConfig` instance.
*
*/
void otSrpServerGetLeaseConfig(otInstance *aInstance, otSrpServerLeaseConfig *aLeaseConfig);
/**
* This function sets SRP server LEASE and KEY-LEASE configurations.
*
* When a non-zero LEASE time is requested from a client, the granted value will be
* limited in range [aMinLease, aMaxLease]; and a non-zero KEY-LEASE will be granted
@@ -121,23 +142,16 @@ void otSrpServerSetEnabled(otInstance *aInstance, bool aEnabled);
* be granted.
*
* @param[in] aInstance A pointer to an OpenThread instance.
* @param[in] aMinLease The minimum LEASE interval in seconds.
* @param[in] aMaxLease The maximum LEASE interval in seconds.
* @param[in] aMinKeyLease The minimum KEY-LEASE interval in seconds.
* @param[in] aMaxKeyLease The maximum KEY-LEASE interval in seconds.
* @param[in] aLeaseConfig A pointer to an `otSrpServerLeaseConfig` instance.
*
* @retval OT_ERROR_NONE Successfully set the LEASE and KEY-LEASE ranges.
* @retval OT_ERROR_INVALID_ARGS The LEASE or KEY-LEASE range is not valid.
*
*/
otError otSrpServerSetLeaseRange(otInstance *aInstance,
uint32_t aMinLease,
uint32_t aMaxLease,
uint32_t aMinKeyLease,
uint32_t aMaxKeyLease);
otError otSrpServerSetLeaseConfig(otInstance *aInstance, const otSrpServerLeaseConfig *aLeaseConfig);
/**
* This method handles SRP service updates.
* This function handles SRP service updates.
*
* This function is called by the SRP server to notify that a SRP host and possibly SRP services
* are being updated. It is important that the SRP updates are not committed until the handler
@@ -175,7 +189,7 @@ typedef void (*otSrpServerServiceUpdateHandler)(otSrpServerServiceUpdateId aId,
void * aContext);
/**
* This method sets the SRP service updates handler on SRP server.
* This function sets the SRP service updates handler on SRP server.
*
* @param[in] aInstance A pointer to an OpenThread instance.
* @param[in] aServiceHandler A pointer to a service handler. Use NULL to remove the handler.
@@ -188,7 +202,7 @@ void otSrpServerSetServiceUpdateHandler(otInstance * aInstanc
void * aContext);
/**
* This method reports the result of processing a SRP update to the SRP server.
* This function reports the result of processing a SRP update to the SRP server.
*
* The Service Update Handler should call this function to return the result of its
* processing of a SRP update.
@@ -203,7 +217,7 @@ void otSrpServerSetServiceUpdateHandler(otInstance * aInstanc
void otSrpServerHandleServiceUpdateResult(otInstance *aInstance, otSrpServerServiceUpdateId aId, otError aError);
/**
* This method returns the next registered host on the SRP server.
* This function returns the next registered host on the SRP server.
*
* @param[in] aInstance A pointer to an OpenThread instance.
* @param[in] aHost A pointer to current host; use NULL to get the first host.
@@ -214,7 +228,7 @@ void otSrpServerHandleServiceUpdateResult(otInstance *aInstance, otSrpServerServ
const otSrpServerHost *otSrpServerGetNextHost(otInstance *aInstance, const otSrpServerHost *aHost);
/**
* This method tells if the SRP service host has been deleted.
* This function tells if the SRP service host has been deleted.
*
* A SRP service host can be deleted but retains its name for future uses.
* In this case, the host instance is not removed from the SRP server/registry.
@@ -227,7 +241,7 @@ const otSrpServerHost *otSrpServerGetNextHost(otInstance *aInstance, const otSrp
bool otSrpServerHostIsDeleted(const otSrpServerHost *aHost);
/**
* This method returns the full name of the host.
* This function returns the full name of the host.
*
* @param[in] aHost A pointer to the SRP service host.
*
@@ -237,7 +251,7 @@ bool otSrpServerHostIsDeleted(const otSrpServerHost *aHost);
const char *otSrpServerHostGetFullName(const otSrpServerHost *aHost);
/**
* This method returns the addresses of given host.
* This function returns the addresses of given host.
*
* @param[in] aHost A pointer to the SRP service host.
* @param[out] aAddressesNum A pointer to where we should output the number of the addresses to.
@@ -248,7 +262,7 @@ const char *otSrpServerHostGetFullName(const otSrpServerHost *aHost);
const otIp6Address *otSrpServerHostGetAddresses(const otSrpServerHost *aHost, uint8_t *aAddressesNum);
/**
* This method returns the next service of given host.
* This function returns the next service of given host.
*
* @param[in] aHost A pointer to the SRP service host.
* @param[in] aService A pointer to current SRP service instance; use NULL to get the first service.
@@ -260,7 +274,7 @@ const otSrpServerService *otSrpServerHostGetNextService(const otSrpServerHost *
const otSrpServerService *aService);
/**
* This method tells if the SRP service has been deleted.
* This function tells if the SRP service has been deleted.
*
* A SRP service can be deleted but retains its name for future uses.
* In this case, the service instance is not removed from the SRP server/registry.
@@ -274,7 +288,7 @@ const otSrpServerService *otSrpServerHostGetNextService(const otSrpServerHost *
bool otSrpServerServiceIsDeleted(const otSrpServerService *aService);
/**
* This method returns the full name of the service.
* This function returns the full name of the service.
*
* @param[in] aService A pointer to the SRP service.
*
@@ -284,7 +298,7 @@ bool otSrpServerServiceIsDeleted(const otSrpServerService *aService);
const char *otSrpServerServiceGetFullName(const otSrpServerService *aService);
/**
* This method returns the port of the service instance.
* This function returns the port of the service instance.
*
* @param[in] aService A pointer to the SRP service.
*
@@ -294,7 +308,7 @@ const char *otSrpServerServiceGetFullName(const otSrpServerService *aService);
uint16_t otSrpServerServiceGetPort(const otSrpServerService *aService);
/**
* This method returns the weight of the service instance.
* This function returns the weight of the service instance.
*
* @param[in] aService A pointer to the SRP service.
*
@@ -304,7 +318,7 @@ uint16_t otSrpServerServiceGetPort(const otSrpServerService *aService);
uint16_t otSrpServerServiceGetWeight(const otSrpServerService *aService);
/**
* This method returns the priority of the service instance.
* This function returns the priority of the service instance.
*
* @param[in] aService A pointer to the SRP service.
*
@@ -325,7 +339,7 @@ uint16_t otSrpServerServiceGetPriority(const otSrpServerService *aService);
const uint8_t *otSrpServerServiceGetTxtData(const otSrpServerService *aService, uint16_t *aDataLength);
/**
* This method returns the host which the service instance reside on.
* This function returns the host which the service instance reside on.
*
* @param[in] aService A pointer to the SRP service.
*
+23 -1
View File
@@ -92,7 +92,29 @@ srp-api-test-0.default.service.arpa.
Done
```
### srp server service
### lease
Usage: `srp server lease [<min-lease>] [<max-lease>] [<min-key-lease>] [<max-key-lease>]`
Get LEASE and KEY-LEASE values.
```bash
> srp server lease
min lease: 1800
max lease: 7200
min key-lease: 86400
max key-lease: 1209600
Done
```
Set LEASE and KEY-LEASE values.
```bash
> srp server lease 1800 7200 86400 1209600
Done
```
### service
Usage: `srp server service`
+22 -12
View File
@@ -100,19 +100,29 @@ otError SrpServer::ProcessDisable(uint8_t aArgsLength, Arg aArgs[])
otError SrpServer::ProcessLease(uint8_t aArgsLength, Arg aArgs[])
{
otError error = OT_ERROR_NONE;
uint32_t minLease;
uint32_t maxLease;
uint32_t minKeyLease;
uint32_t maxKeyLease;
otError error = OT_ERROR_NONE;
otSrpServerLeaseConfig leaseConfig;
VerifyOrExit(aArgsLength == 5, error = OT_ERROR_INVALID_ARGS);
SuccessOrExit(error = aArgs[1].ParseAsUint32(minLease));
SuccessOrExit(error = aArgs[2].ParseAsUint32(maxLease));
SuccessOrExit(error = aArgs[3].ParseAsUint32(minKeyLease));
SuccessOrExit(error = aArgs[4].ParseAsUint32(maxKeyLease));
error = otSrpServerSetLeaseRange(mInterpreter.mInstance, minLease, maxLease, minKeyLease, maxKeyLease);
if (aArgsLength == 5)
{
SuccessOrExit(error = aArgs[1].ParseAsUint32(leaseConfig.mMinLease));
SuccessOrExit(error = aArgs[2].ParseAsUint32(leaseConfig.mMaxLease));
SuccessOrExit(error = aArgs[3].ParseAsUint32(leaseConfig.mMinKeyLease));
SuccessOrExit(error = aArgs[4].ParseAsUint32(leaseConfig.mMaxKeyLease));
error = otSrpServerSetLeaseConfig(mInterpreter.mInstance, &leaseConfig);
}
else if (aArgsLength == 1)
{
otSrpServerGetLeaseConfig(mInterpreter.mInstance, &leaseConfig);
mInterpreter.OutputLine("min lease: %u", leaseConfig.mMinLease);
mInterpreter.OutputLine("max lease: %u", leaseConfig.mMaxLease);
mInterpreter.OutputLine("min key-lease: %u", leaseConfig.mMinKeyLease);
mInterpreter.OutputLine("max key-lease: %u", leaseConfig.mMaxKeyLease);
}
else
{
ExitNow(error = OT_ERROR_INVALID_ARGS);
}
exit:
return error;
+9 -6
View File
@@ -63,15 +63,18 @@ void otSrpServerSetEnabled(otInstance *aInstance, bool aEnabled)
instance.Get<Srp::Server>().SetEnabled(aEnabled);
}
otError otSrpServerSetLeaseRange(otInstance *aInstance,
uint32_t aMinLease,
uint32_t aMaxLease,
uint32_t aMinKeyLease,
uint32_t aMaxKeyLease)
void otSrpServerGetLeaseConfig(otInstance *aInstance, otSrpServerLeaseConfig *aLeaseConfig)
{
Instance &instance = *static_cast<Instance *>(aInstance);
return instance.Get<Srp::Server>().SetLeaseRange(aMinLease, aMaxLease, aMinKeyLease, aMaxKeyLease);
instance.Get<Srp::Server>().GetLeaseConfig(static_cast<Srp::Server::LeaseConfig &>(*aLeaseConfig));
}
otError otSrpServerSetLeaseConfig(otInstance *aInstance, const otSrpServerLeaseConfig *aLeaseConfig)
{
Instance &instance = *static_cast<Instance *>(aInstance);
return instance.Get<Srp::Server>().SetLeaseConfig(static_cast<const Srp::Server::LeaseConfig &>(*aLeaseConfig));
}
void otSrpServerSetServiceUpdateHandler(otInstance * aInstance,
+37 -20
View File
@@ -81,10 +81,6 @@ Server::Server(Instance &aInstance)
, mServiceUpdateHandler(nullptr)
, mServiceUpdateHandlerContext(nullptr)
, mDomain(nullptr)
, mMinLease(kDefaultMinLease)
, mMaxLease(kDefaultMaxLease)
, mMinKeyLease(kDefaultMinKeyLease)
, mMaxKeyLease(kDefaultMaxKeyLease)
, mLeaseTimer(aInstance, HandleLeaseTimer)
, mOutstandingUpdatesTimer(aInstance, HandleOutstandingUpdatesTimer)
, mServiceUpdateId(Random::NonCrypto::GetUint32())
@@ -128,41 +124,62 @@ exit:
return;
}
Error Server::SetLeaseRange(uint32_t aMinLease, uint32_t aMaxLease, uint32_t aMinKeyLease, uint32_t aMaxKeyLease)
Server::LeaseConfig::LeaseConfig(void)
{
Error error = kErrorNone;
mMinLease = kDefaultMinLease;
mMaxLease = kDefaultMaxLease;
mMinKeyLease = kDefaultMinKeyLease;
mMaxKeyLease = kDefaultMaxKeyLease;
}
bool Server::LeaseConfig::IsValid(void) const
{
bool valid = false;
// TODO: Support longer LEASE.
// We use milliseconds timer for LEASE & KEY-LEASE, this is to avoid overflow.
VerifyOrExit(aMaxKeyLease <= Time::MsecToSec(TimerMilli::kMaxDelay), error = kErrorInvalidArgs);
VerifyOrExit(aMinLease <= aMaxLease, error = kErrorInvalidArgs);
VerifyOrExit(aMinKeyLease <= aMaxKeyLease, error = kErrorInvalidArgs);
VerifyOrExit(aMinLease <= aMinKeyLease, error = kErrorInvalidArgs);
VerifyOrExit(aMaxLease <= aMaxKeyLease, error = kErrorInvalidArgs);
VerifyOrExit(mMaxKeyLease <= Time::MsecToSec(TimerMilli::kMaxDelay));
VerifyOrExit(mMinLease <= mMaxLease);
VerifyOrExit(mMinKeyLease <= mMaxKeyLease);
VerifyOrExit(mMinLease <= mMinKeyLease);
VerifyOrExit(mMaxLease <= mMaxKeyLease);
mMinLease = aMinLease;
mMaxLease = aMaxLease;
mMinKeyLease = aMinKeyLease;
mMaxKeyLease = aMaxKeyLease;
valid = true;
exit:
return error;
return valid;
}
uint32_t Server::GrantLease(uint32_t aLease) const
uint32_t Server::LeaseConfig::GrantLease(uint32_t aLease) const
{
OT_ASSERT(mMinLease <= mMaxLease);
return (aLease == 0) ? 0 : OT_MAX(mMinLease, OT_MIN(mMaxLease, aLease));
}
uint32_t Server::GrantKeyLease(uint32_t aKeyLease) const
uint32_t Server::LeaseConfig::GrantKeyLease(uint32_t aKeyLease) const
{
OT_ASSERT(mMinKeyLease <= mMaxKeyLease);
return (aKeyLease == 0) ? 0 : OT_MAX(mMinKeyLease, OT_MIN(mMaxKeyLease, aKeyLease));
}
void Server::GetLeaseConfig(LeaseConfig &aLeaseConfig) const
{
aLeaseConfig = mLeaseConfig;
}
Error Server::SetLeaseConfig(const LeaseConfig &aLeaseConfig)
{
Error error = kErrorNone;
VerifyOrExit(aLeaseConfig.IsValid(), error = kErrorInvalidArgs);
mLeaseConfig = aLeaseConfig;
exit:
return error;
}
const char *Server::GetDomain(void) const
{
return mDomain;
@@ -340,8 +357,8 @@ void Server::CommitSrpUpdate(Error aError,
hostLease = aHost.GetLease();
hostKeyLease = aHost.GetKeyLease();
grantedLease = GrantLease(hostLease);
grantedKeyLease = GrantKeyLease(hostKeyLease);
grantedLease = mLeaseConfig.GrantLease(hostLease);
grantedKeyLease = mLeaseConfig.GrantKeyLease(hostKeyLease);
aHost.SetLease(grantedLease);
aHost.SetKeyLease(grantedKeyLease);
+38 -17
View File
@@ -402,6 +402,27 @@ public:
LinkedList<Service> mServices;
};
/**
* This class handles LEASE and KEY-LEASE configurations.
*
*/
class LeaseConfig : public otSrpServerLeaseConfig
{
friend class Server;
public:
/**
* This constructor initialize to default LEASE and KEY-LEASE configurations.
*
*/
LeaseConfig(void);
private:
bool IsValid(void) const;
uint32_t GrantLease(uint32_t aLease) const;
uint32_t GrantKeyLease(uint32_t aKeyLease) const;
};
/**
* This constructor initializes the SRP server object.
*
@@ -469,22 +490,27 @@ public:
void SetEnabled(bool aEnabled);
/**
* This method sets LEASE & KEY-LEASE range that is acceptable by the SRP server.
* This method returns the LEASE and KEY-LEASE configurations.
*
* @param[out] aLeaseConfig A reference to the `LeaseConfig` instance.
*
*/
void GetLeaseConfig(LeaseConfig &aLeaseConfig) const;
/**
* This method sets the LEASE and KEY-LEASE configurations.
*
* When a LEASE time is requested from a client, the granted value will be
* limited in range [aMinLease, aMaxLease]; and a KEY-LEASE will be granted
* in range [aMinKeyLease, aMaxKeyLease].
*
* @param[in] aMinLease The minimum LEASE interval in seconds.
* @param[in] aMaxLease The maximum LEASE interval in seconds.
* @param[in] aMinKeyLease The minimum KEY-LEASE interval in seconds.
* @param[in] aMaxKeyLease The maximum KEY-LEASE interval in seconds.
* @param[in] aLeaseConfig A reference to the `LeaseConfig` instance.
*
* @retval kErrorNone Successfully set the LEASE and KEY-LEASE ranges.
* @retval kErrorInvalidArgs The LEASE or KEY-LEASE range is not valid.
*
*/
Error SetLeaseRange(uint32_t aMinLease, uint32_t aMaxLease, uint32_t aMinKeyLease, uint32_t aMaxKeyLease);
Error SetLeaseConfig(const LeaseConfig &aLeaseConfig);
/**
* This method returns the next registered SRP host.
@@ -554,13 +580,11 @@ private:
UpdateMetadata * mNext;
};
void Start(void);
void Stop(void);
void HandleNotifierEvents(Events aEvents);
Error PublishServerData(void);
void UnpublishServerData(void);
uint32_t GrantLease(uint32_t aLease) const;
uint32_t GrantKeyLease(uint32_t aKeyLease) const;
void Start(void);
void Stop(void);
void HandleNotifierEvents(Events aEvents);
Error PublishServerData(void);
void UnpublishServerData(void);
ServiceUpdateId AllocateId(void) { return mServiceUpdateId++; }
@@ -638,10 +662,7 @@ private:
char *mDomain;
uint32_t mMinLease; // The minimum lease time in seconds.
uint32_t mMaxLease; // The maximum lease time in seconds.
uint32_t mMinKeyLease; // The minimum key-lease time in seconds.
uint32_t mMaxKeyLease; // The maximum key-lease time in seconds.
LeaseConfig mLeaseConfig;
LinkedList<Host> mHosts;
TimerMilli mLeaseTimer;
+5
View File
@@ -947,6 +947,11 @@ class OTCI(object):
return txt_dict
def srp_server_get_lease(self) -> Tuple[int, int, int, int]:
"""Get SRP server LEASE & KEY-LEASE range (in seconds)."""
lines = self.execute_command(f'srp server lease')
return tuple([int(line.split(':')[1].strip()) for line in lines])
def srp_server_set_lease(self, min_lease: int, max_lease: int, min_key_lease: int, max_key_lease: int):
"""Configure SRP server LEASE & KEY-LEASE range (in seconds)."""
self.execute_command(f'srp server lease {min_lease} {max_lease} {min_key_lease} {max_key_lease}')
+6
View File
@@ -373,6 +373,12 @@ class TestOTCI(unittest.TestCase):
server.srp_server_set_domain('default.service.arpa.')
self.assertEqual('default.service.arpa.', server.srp_server_get_domain())
default_leases = server.srp_server_get_lease()
self.assertEqual(default_leases, (1800, 7200, 86400, 1209600))
server.srp_server_set_lease(1801, 7201, 86401, 1209601)
leases = server.srp_server_get_lease()
self.assertEqual(leases, (1801, 7201, 86401, 1209601))
self.assertFalse(client.srp_client_get_state())
self.assertEqual('Removed', client.srp_client_get_host_state())
self.assertEqual(('::', 0), client.srp_client_get_server())