esp8266ndn
NDN Arduino library for ESP8266 and more
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
validity-period.hpp
Go to the documentation of this file.
1#ifndef NDNPH_KEYCHAIN_VALIDITY_PERIOD_HPP
2#define NDNPH_KEYCHAIN_VALIDITY_PERIOD_HPP
3
4#include "../packet/an.hpp"
5#include "../port/unixtime/port.hpp"
6#include "../tlv/ev-decoder.hpp"
7#include "../tlv/value.hpp"
8
9namespace ndnph {
10namespace detail {
11
13public:
15 const char* tz = getenv("TZ");
16 if (tz == nullptr || strlen(tz) >= sizeof(m_tz)) {
17 m_tz[0] = '\0';
18 } else {
19 strncpy(m_tz, tz, sizeof(m_tz));
20 }
21 setenv("TZ", "UTC", 1);
22 }
23
25 if (m_tz[0] != '\0') {
26 setenv("TZ", m_tz, 1);
27 }
28 }
29
30private:
31 char m_tz[64];
32};
33
34} // namespace detail
35
38public:
41 return ValidityPeriod(540109800, MAX_TIME);
42 }
43
45 static ValidityPeriod secondsFromNow(uint64_t seconds) {
46 time_t now = port::UnixTime::now() / 1000000;
47 return ValidityPeriod(now, now + seconds);
48 }
49
51 static ValidityPeriod daysFromNow(uint64_t days) {
52 return secondsFromNow(86400 * days);
53 }
54
55 ValidityPeriod() = default;
56
57 explicit ValidityPeriod(time_t notBefore, time_t notAfter)
59 , notAfter(notAfter) {}
60
62 bool includes(time_t t) {
63 return notBefore <= t && t <= notAfter;
64 }
65
67 bool includesUnix(uint64_t t = port::UnixTime::now()) {
68 return includes(t / 1000000);
69 }
70
73 return ValidityPeriod(std::max(notBefore, other.notBefore), std::min(notAfter, other.notAfter));
74 }
75
76 void encodeTo(Encoder& encoder) const {
77 encoder.prependTlv(TT::ValidityPeriod, TimestampEncoder(TT::NotBefore, notBefore),
78 TimestampEncoder(TT::NotAfter, notAfter));
79 }
80
81 bool decodeFrom(const Decoder::Tlv& input) {
83 EvDecoder::def<TT::NotBefore>([this](const Decoder::Tlv& d) {
84 return decodeTimestamp(d, &notBefore);
85 }),
86 EvDecoder::def<TT::NotAfter>([this](const Decoder::Tlv& d) {
87 return decodeTimestamp(d, &notAfter);
88 }));
89 }
90
91private:
92 static constexpr time_t MAX_TIME =
93 sizeof(time_t) <= 4 ? std::numeric_limits<time_t>::max() : 253402300799;
94 static const constexpr char* const TIMESTAMP_FMT = "%04d%02d%02dT%02d%02d%02d";
95 static constexpr size_t TIMESTAMP_LEN = 15;
96
97 class TimestampEncoder {
98 public:
99 explicit TimestampEncoder(uint32_t tlvType, time_t t)
100 : tlvType(tlvType)
101 , t(t) {}
102
103 void encodeTo(Encoder& encoder) const {
104 struct tm* m = gmtime(&t);
105 if (m == nullptr) {
106 encoder.setError();
107 return;
108 }
109
110 char buf[TIMESTAMP_LEN + 1];
111 int res = snprintf(buf, sizeof(buf), TIMESTAMP_FMT, 1900 + m->tm_year, 1 + m->tm_mon,
112 m->tm_mday, m->tm_hour, m->tm_min, m->tm_sec);
113 NDNPH_ASSERT(res >= 0 && res < static_cast<int>(sizeof(buf)));
114 encoder.prependTlv(tlvType, tlv::Value(reinterpret_cast<const uint8_t*>(buf), TIMESTAMP_LEN));
115 }
116
117 public:
118 uint32_t tlvType = 0;
119 time_t t = 0;
120 };
121
122 static bool decodeTimestamp(const Decoder::Tlv& d, time_t* v) {
123 if (d.length != TIMESTAMP_LEN) {
124 return false;
125 }
126
127 char buf[TIMESTAMP_LEN + 1];
128 std::copy_n(d.value, TIMESTAMP_LEN, buf);
129 buf[TIMESTAMP_LEN] = '\0';
130
131 tm m{};
132 if (sscanf(buf, TIMESTAMP_FMT, &m.tm_year, &m.tm_mon, &m.tm_mday, &m.tm_hour, &m.tm_min,
133 &m.tm_sec) != 6) {
134 return false;
135 }
136 m.tm_year -= 1900;
137 m.tm_mon -= 1;
138
139 detail::UtcTimezone useUtc;
140 *v = mktime(&m);
141 if (sizeof(time_t) <= 4 && *v < 0 && (1900 + m.tm_year) >= 2038) {
142 *v = MAX_TIME;
143 }
144 return *v >= 0;
145 }
146
147public:
149 time_t notBefore = 0;
150
152 time_t notAfter = 0;
153};
154
155inline bool
156operator==(const ValidityPeriod& lhs, const ValidityPeriod& rhs) {
157 return lhs.notBefore == rhs.notBefore && lhs.notAfter == rhs.notAfter;
158}
159
160NDNPH_DECLARE_NE(ValidityPeriod, inline)
161
162
163inline ValidityPeriod
164operator&&(const ValidityPeriod& lhs, const ValidityPeriod& rhs) {
165 ValidityPeriod intersection;
166 intersection.notBefore = std::max(lhs.notBefore, rhs.notBefore);
167 intersection.notAfter = std::min(lhs.notAfter, rhs.notAfter);
168 return intersection;
169}
170
171} // namespace ndnph
172
173#endif // NDNPH_KEYCHAIN_VALIDITY_PERIOD_HPP
Decoded TLV.
Definition decoder.hpp:13
TLV encoder that accepts items in reverse order.
Definition encoder.hpp:10
bool prependTlv(uint32_t type, OmitEmptyTag omitEmpty, const Arg &... arg)
Prepend TLV, measuring TLV-LENGTH automatically.
Definition encoder.hpp:143
static bool decode(const Decoder::Tlv &input, std::initializer_list< uint32_t > topTypes, const E &... defs)
Decode input TLV with a sequence of element definitions.
Definition ev-decoder.hpp:115
ValidityPeriod of a certificate.
Definition validity-period.hpp:37
bool decodeFrom(const Decoder::Tlv &input)
Definition validity-period.hpp:81
bool includes(time_t t)
Determine whether the timestamp (in seconds) is within validity period.
Definition validity-period.hpp:62
time_t notBefore
NotBefore field in seconds since Unix epoch.
Definition validity-period.hpp:149
static ValidityPeriod daysFromNow(uint64_t days)
Get a ValidityPeriod from now until days later.
Definition validity-period.hpp:51
static ValidityPeriod secondsFromNow(uint64_t seconds)
Get a ValidityPeriod from now until seconds later.
Definition validity-period.hpp:45
ValidityPeriod(time_t notBefore, time_t notAfter)
Definition validity-period.hpp:57
time_t notAfter
NotAfter field in seconds since Unix epoch.
Definition validity-period.hpp:152
ValidityPeriod intersect(const ValidityPeriod &other) const
Calculate the intersection of this and other ValidityPeriod.
Definition validity-period.hpp:72
static ValidityPeriod getMax()
Get a very long ValidityPeriod.
Definition validity-period.hpp:40
void encodeTo(Encoder &encoder) const
Definition validity-period.hpp:76
bool includesUnix(uint64_t t=port::UnixTime::now())
Determine whether the Unix timestamp (in microseconds) is within validity period.
Definition validity-period.hpp:67
Definition validity-period.hpp:12
~UtcTimezone()
Definition validity-period.hpp:24
UtcTimezone()
Definition validity-period.hpp:14
#define NDNPH_ASSERT(x)
Definition common.hpp:30
@ NotAfter
Definition an.hpp:59
@ ValidityPeriod
Definition an.hpp:57
@ NotBefore
Definition an.hpp:58
Definition fs.hpp:33
bool operator==(const ValidityPeriod &lhs, const ValidityPeriod &rhs)
Definition validity-period.hpp:156
#define NDNPH_DECLARE_NE(T, specifier)
Declare operator!= in terms of operator==.
Definition operators.hpp:7