80 lines
1.8 KiB
C++
80 lines
1.8 KiB
C++
#pragma once
|
|
|
|
#include <stdint.h>
|
|
|
|
struct TimeConfiguration {
|
|
|
|
/**
|
|
* @brief The timezone offset in seconds
|
|
*/
|
|
int32_t offsetToGMT;
|
|
|
|
/**
|
|
* @brief The daylight savings offset in seconds
|
|
*/
|
|
int32_t offsetDaylightSavings;
|
|
|
|
/**
|
|
* @brief The url of the NTP server
|
|
*/
|
|
const char* ntpServerUrl;
|
|
|
|
/**
|
|
* @brief The allowed discrepancy between the time of a received message
|
|
* and the device time (in seconds)
|
|
*
|
|
* A stricter (lower) value better prevents against replay attacks,
|
|
* but may lead to issues when dealing with slow networks and other
|
|
* routing delays.
|
|
*/
|
|
uint32_t allowedTimeOffset;
|
|
};
|
|
|
|
class TimeCheck {
|
|
|
|
public:
|
|
|
|
/**
|
|
* @brief Create a time checker instance
|
|
*/
|
|
TimeCheck();
|
|
|
|
/**
|
|
* @brief Set the configuration
|
|
*/
|
|
void configure(TimeConfiguration configuration);
|
|
|
|
/**
|
|
* @brief Configure the NTP server to get the current time
|
|
*/
|
|
void startNTP();
|
|
|
|
/**
|
|
* @brief Print the current time to the serial output
|
|
*
|
|
* The time must be initialized by calling `configureNTP()` before use.
|
|
*/
|
|
void printLocalTime();
|
|
|
|
/**
|
|
* Gets the current epoch time
|
|
*/
|
|
uint32_t getEpochTime();
|
|
|
|
/**
|
|
* @brief Check wether the time of a message is within the allowed bounds regarding freshness.
|
|
*
|
|
* The timestamp is used to ensure 'freshness' of the messages,
|
|
* i.e. that they are not unreasonably delayed or captured and
|
|
* later replayed by an attacker.
|
|
*
|
|
* @param messageTime The timestamp of the message (seconds since epoch)
|
|
* @return true The time is within the acceptable offset of the local time
|
|
* @return false The message time is invalid
|
|
*/
|
|
bool isMessageTimeAcceptable(uint32_t messageTime);
|
|
|
|
private:
|
|
|
|
TimeConfiguration config;
|
|
}; |