1 #ifndef NDNPH_PACKET_SIG_INFO_HPP
2 #define NDNPH_PACKET_SIG_INFO_HPP
4 #include "../port/random/port.hpp"
5 #include "../port/unixtime/port.hpp"
6 #include "../tlv/ev-decoder.hpp"
18 if (currentOrder < 1000) {
25 EvDecoder::def<TT::KeyLocator, false, 2>([
this](
const Decoder::Tlv& d) {
38 encoder.prependTlv(TT::KeyLocator, name);
73 EvDecoder::defNni<TT::SigTime>(&
timestamp),
74 EvDecoder::defNni<TT::SigSeqNum>(&
seqNum));
88 using Order = std::integral_constant<int, order>;
113 template<
int nonceLength = 8,
int nTrackedNonces = 16>
116 using Order = std::integral_constant<int, 1>;
117 static_assert(nonceLength > 0,
"");
118 static_assert(nTrackedNonces > 0,
"");
120 class Value :
public std::array<uint8_t, nonceLength> {
127 std::copy(
input.begin(),
input.end(), this->begin());
140 return valid && other.
valid && std::equal(this->begin(), this->end(), other.begin());
154 }
while (exists(value));
161 return !exists(value);
170 bool exists(
const Value& value)
const {
171 return std::find(m_nonces.begin(), m_nonces.end(), value) != m_nonces.end();
174 void append(
const Value& value) {
176 m_nonces[m_pos] = value;
177 if (++m_pos == m_nonces.size()) {
183 std::array<Value, nTrackedNonces> m_nonces = {};
191 template<
int maxClockOffset = 60000>
194 using Order = std::integral_constant<int, 2>;
197 uint64_t timestamp = std::max(now(), m_last + 1);
203 static_assert(maxClockOffset >= 0,
"");
205 std::abs(
static_cast<int64_t
>(now() - f.
timestamp)) <= maxClockOffset;
213 uint64_t now()
const {
215 return port::UnixTime::now() / 1000;
225 using Order = std::integral_constant<int, 3>;
235 return f.
seqNum >= m_next;
239 m_next = std::max(m_next, f.
seqNum) + 1;
254 template<
typename R0,
typename R1 = detail::Skip<11>,
typename R2 = detail::Skip<12>>
257 explicit Policy(
const R0& r0 = R0(),
const R1& r1 = R1(),
const R2& r2 = R2())
271 if (!encoder.
prepend(m_r0.create(), m_r1.create(), m_r2.create())) {
287 if (f.
decode(si) && m_r0.check(f) && m_r1.check(f) && m_r2.check(f)) {
301 static_assert(R0::Order::value < R1::Order::value,
"");
302 static_assert(R1::Order::value < R2::Order::value,
"");
310 template<
typename... R>
313 return Policy<R...>(std::forward<R>(rule)...);
static bool generate(uint8_t *output, size_t count)
SignatureInfo on Data.
Definition: sig-info.hpp:59
void encodeTo(Encoder &encoder) const
Definition: sig-info.hpp:61
Decoded TLV.
Definition: decoder.hpp:13
const uint8_t * tlv
Definition: decoder.hpp:42
TLV encoder that accepts items in reverse order.
Definition: encoder.hpp:10
bool prepend(const First &first, const Arg &... arg)
Prepend a sequence of values.
Definition: encoder.hpp:123
void trim() const
Release unused space to the Region.
Definition: encoder.hpp:58
void discard()
Release all space to the Region.
Definition: encoder.hpp:72
bool prependTlv(uint32_t type, OmitEmptyTag omitEmpty, const Arg &... arg)
Prepend TLV, measuring TLV-LENGTH automatically.
Definition: encoder.hpp:143
void setError()
Indicate an error has occurred.
Definition: encoder.hpp:166
Definition: ev-decoder.hpp:102
static bool decodeEx(const Decoder::Tlv &input, std::initializer_list< uint32_t > topTypes, const UnknownCallback &unknownCb, const IsCritical &isCritical, const E &... defs)
Decode input TLV with a sequence of element definitions.
Definition: ev-decoder.hpp:145
static bool decodeValue(const Decoder &input, const E &... defs)
Decode input TLV-VALUE with a sequence of element definitions.
Definition: ev-decoder.hpp:126
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
SignatureInfo on Interest.
Definition: sig-info.hpp:51
void encodeTo(Encoder &encoder) const
Definition: sig-info.hpp:53
Name.
Definition: name.hpp:14
size_t size() const
Get number of components.
Definition: name.hpp:86
Region-based memory allocator thats owns memory of NDNph objects.
Definition: region.hpp:9
SignatureInfo.
Definition: sig-info.hpp:12
void encodeImpl(uint32_t type, Encoder &encoder) const
Definition: sig-info.hpp:33
Name name
Definition: sig-info.hpp:45
tlv::Value extensions
Definition: sig-info.hpp:46
bool decodeFrom(const Decoder::Tlv &input)
Definition: sig-info.hpp:14
uint8_t sigType
Definition: sig-info.hpp:47
Parsed extension fields from Interest SigInfo.
Definition: sig-info.hpp:69
tlv::Value nonce
Definition: sig-info.hpp:78
uint64_t seqNum
Definition: sig-info.hpp:80
uint64_t timestamp
Definition: sig-info.hpp:79
bool decode(const ISigInfo &si)
Definition: sig-info.hpp:71
Definition: sig-info.hpp:120
Value(tlv::Value input)
Definition: sig-info.hpp:124
void encodeTo(Encoder &encoder) const
Definition: sig-info.hpp:131
bool valid
Definition: sig-info.hpp:144
bool operator==(const Value &other) const
Definition: sig-info.hpp:139
Require SigNonce field in Interest SigInfo.
Definition: sig-info.hpp:114
void save(const Fields &f)
Definition: sig-info.hpp:164
Value create()
Definition: sig-info.hpp:147
std::integral_constant< int, 1 > Order
Definition: sig-info.hpp:116
bool check(const Fields &f) const
Definition: sig-info.hpp:159
Validation policy for SigInfo fields in signed Interest.
Definition: sig-info.hpp:255
bool check(const ISigInfo &si)
Check that SigInfo fields fulfill current policy.
Definition: sig-info.hpp:285
Policy(const R0 &r0=R0(), const R1 &r1=R1(), const R2 &r2=R2())
Definition: sig-info.hpp:257
bool create(Region ®ion, ISigInfo &si)
Assign SigInfo extension fields.
Definition: sig-info.hpp:269
Require SigSeqNum field in Interest SigInfo.
Definition: sig-info.hpp:223
SeqNum(uint64_t next=0)
Definition: sig-info.hpp:227
std::integral_constant< int, 3 > Order
Definition: sig-info.hpp:225
tlv::NniElement create()
Definition: sig-info.hpp:230
void save(const Fields &f)
Definition: sig-info.hpp:238
bool check(const Fields &f) const
Definition: sig-info.hpp:234
Require SigTime field in Interest SigInfo.
Definition: sig-info.hpp:192
void save(const Fields &f)
Definition: sig-info.hpp:208
tlv::NniElement create()
Definition: sig-info.hpp:196
std::integral_constant< int, 2 > Order
Definition: sig-info.hpp:194
bool check(const Fields &f) const
Definition: sig-info.hpp:202
Definition: sig-info.hpp:90
void encodeTo(Encoder &) const
Definition: sig-info.hpp:92
Definition: sig-info.hpp:86
bool check(const Fields &) const
Definition: sig-info.hpp:99
EncodeValue create()
Definition: sig-info.hpp:95
std::integral_constant< int, order > Order
Definition: sig-info.hpp:88
void save(const Fields &)
Definition: sig-info.hpp:103
Encode to a TLV element where TLV-VALUE is a NonNegativeInteger.
Definition: nni.hpp:170
A sequence of bytes, usually TLV-VALUE.
Definition: value.hpp:11
Decoder makeDecoder() const
Create a Decoder over this value buffer.
Definition: value.hpp:64
#define NDNPH_ASSERT(x)
Definition: common.hpp:30
@ DSigInfo
Definition: an.hpp:47
@ SigSeqNum
Definition: an.hpp:55
@ SigTime
Definition: an.hpp:54
@ ISigInfo
Definition: an.hpp:38
@ SigNonce
Definition: an.hpp:53
@ SigType
Definition: an.hpp:50
bool input(Region ®ion, T &target, std::istream &is=std::cin)
Read and decode from input stream.
Definition: io.hpp:15
Policy< R... > makePolicy(R &&... rule)
Create Interest SigInfo validation policy.
Definition: sig-info.hpp:312