all: Refactoring work with time using boost::chrono
Now the library has two clocks: time::steady_clock and
time::system_clock, following (boost|std)::chrono. In addition to
standard now() method, the library contains several helpers to convert
to/from UnixTimestamp (microsecond resolution) and IsoString (optional
microsecond resolution). The IsoString conversions use
boost::posix_time routines, since boost::chrono supports extended IO
support only starting boost version 1.52 (Boost Chrono V2).
This commit breaks compatibility with previous API. All time-related
Data/Interest calls must explicitly use time units to specify
FreshnessPeriod/InterestLifetime.
Brief usage/conversion guide:
- creation of time units does not support double/float types. If
necessary to create time unit from double, ``ndn::duration<double>`` (for
seconds) needs to be used instead. In some cases, this would also
require ``ndn::duration_cast``, if the target is not ``ndn::nanoseconds``.
- ndn::getNow, ndn::ndn_getNowMilliseconds, ndn::time::now are all
removed in favor of the now() method in a specific clock:
* time::system_clock::now();
* time::steady_clock::now();
- When necessary to convert system_clock::TimePoint to unix timestamp,
``time::toUnixTimestamp`` can be used. This method return number of
milliseconds since UNIX epoch as ``ndn::time::milliseconds`` type.
Use count() method to obtain number as an integral value.
Change-Id: Icd688bc6766e008d60c3d2888173627874526e47
Refs: #1152
diff --git a/src/util/command-interest-validator.hpp b/src/util/command-interest-validator.hpp
index af8c796..257f476 100644
--- a/src/util/command-interest-validator.hpp
+++ b/src/util/command-interest-validator.hpp
@@ -24,10 +24,11 @@
GRACE_INTERVAL = 3000 // ms
};
-
- CommandInterestValidator(int64_t graceInterval = GRACE_INTERVAL/*ms*/)
+
+ CommandInterestValidator(const time::milliseconds& graceInterval = time::milliseconds(static_cast<int>(GRACE_INTERVAL)))
+ : m_graceInterval(graceInterval < time::milliseconds::zero() ?
+ time::milliseconds(static_cast<int>(GRACE_INTERVAL)) : graceInterval)
{
- m_graceInterval = (graceInterval < 0 ? GRACE_INTERVAL : graceInterval);
}
virtual
@@ -43,26 +44,28 @@
protected:
virtual void
- checkPolicy (const Data& data,
- int stepCount,
- const OnDataValidated &onValidated,
+ checkPolicy (const Data& data,
+ int stepCount,
+ const OnDataValidated &onValidated,
const OnDataValidationFailed &onValidationFailed,
std::vector<shared_ptr<ValidationRequest> > &nextSteps)
{
onValidationFailed(data.shared_from_this(), "No policy for data checking");
}
-
+
virtual void
- checkPolicy (const Interest& interest,
- int stepCount,
- const OnInterestValidated &onValidated,
+ checkPolicy (const Interest& interest,
+ int stepCount,
+ const OnInterestValidated &onValidated,
const OnInterestValidationFailed &onValidationFailed,
std::vector<shared_ptr<ValidationRequest> > &nextSteps);
private:
- int64_t m_graceInterval; //ms
+ time::milliseconds m_graceInterval; //ms
std::map<Name, PublicKey> m_trustAnchorsForInterest;
std::list<SecRuleSpecific> m_trustScopeForInterest;
- std::map<Name, uint64_t> m_lastTimestamp;
+
+ typedef std::map<Name, time::system_clock::TimePoint> LastTimestampMap;
+ LastTimestampMap m_lastTimestamp;
};
inline void
@@ -82,28 +85,28 @@
}
inline void
-CommandInterestValidator::checkPolicy (const Interest& interest,
- int stepCount,
- const OnInterestValidated &onValidated,
+CommandInterestValidator::checkPolicy (const Interest& interest,
+ int stepCount,
+ const OnInterestValidated &onValidated,
const OnInterestValidationFailed &onValidationFailed,
std::vector<shared_ptr<ValidationRequest> > &nextSteps)
{
const Name& interestName = interest.getName();
-
- //Prepare
+
+ //Prepare
if (interestName.size() < 4)
- return onValidationFailed(interest.shared_from_this(),
+ return onValidationFailed(interest.shared_from_this(),
"Interest is not signed: " + interest.getName().toUri());
-
- Signature signature(interestName[POS_SIG_INFO].blockFromValue(),
+
+ Signature signature(interestName[POS_SIG_INFO].blockFromValue(),
interestName[POS_SIG_VALUE].blockFromValue());
-
+
SignatureSha256WithRsa sig(signature);
const Name& keyLocatorName = sig.getKeyLocator().getName();
Name keyName = IdentityCertificate::certificateNameToPublicKeyName(keyLocatorName);
-
+
//Check if command is in the trusted scope
- bool inScope = false;
+ bool inScope = false;
for(std::list<SecRuleSpecific>::iterator scopeIt = m_trustScopeForInterest.begin();
scopeIt != m_trustScopeForInterest.end();
++scopeIt)
@@ -115,23 +118,28 @@
}
}
if(inScope == false)
- return onValidationFailed(interest.shared_from_this(),
+ return onValidationFailed(interest.shared_from_this(),
"Signer cannot be authorized for the command: " + keyName.toUri());
//Check if timestamp is valid
- uint64_t timestamp = interestName.get(POS_TIMESTAMP).toNumber();
- uint64_t current = static_cast<uint64_t>(time::now()/1000000);
- std::map<Name, uint64_t>::const_iterator timestampIt = m_lastTimestamp.find(keyName);
- if(timestampIt == m_lastTimestamp.end())
+ time::system_clock::TimePoint interestTime = time::fromUnixTimestamp(
+ time::milliseconds(interestName.get(POS_TIMESTAMP).toNumber()));
+ time::system_clock::TimePoint currentTime = time::system_clock::now();
+
+ LastTimestampMap::iterator timestampIt = m_lastTimestamp.find(keyName);
+ if (timestampIt == m_lastTimestamp.end())
{
- if(timestamp > (current + m_graceInterval) || (timestamp + m_graceInterval) < current)
- return onValidationFailed(interest.shared_from_this(),
- "The command is not in grace interval: " + interest.getName().toUri());
+ if (!(currentTime - m_graceInterval <= interestTime &&
+ interestTime <= currentTime + m_graceInterval))
+ {
+ return onValidationFailed(interest.shared_from_this(),
+ "The command is not in grace interval: " + interest.getName().toUri());
+ }
}
- else
+ else
{
- if(m_lastTimestamp[keyName] >= timestamp)
- return onValidationFailed(interest.shared_from_this(),
+ if(interestTime <= timestampIt->second)
+ return onValidationFailed(interest.shared_from_this(),
"The command is outdated: " + interest.getName().toUri());
}
@@ -139,16 +147,22 @@
if(!Validator::verifySignature(interestName.wireEncode().value(),
interestName.wireEncode().value_size() - interestName[-1].size(),
sig, m_trustAnchorsForInterest[keyName]))
- return onValidationFailed(interest.shared_from_this(),
+ return onValidationFailed(interest.shared_from_this(),
"Signature cannot be validated: " + interest.getName().toUri());
//Update timestamp
- m_lastTimestamp[keyName] = timestamp;
+ if (timestampIt == m_lastTimestamp.end())
+ {
+ m_lastTimestamp[keyName] = interestTime;
+ }
+ else
+ {
+ timestampIt->second = interestTime;
+ }
return onValidated(interest.shared_from_this());
}
-
} // namespace ndn
#endif // NDN_HELPERS_COMMAND_INTEREST_VALIDATOR_HPP