NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
validity-period.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "validity-period.hpp"
23 #include "../encoding/block-helpers.hpp"
24 #include "../util/concepts.hpp"
25 
26 namespace ndn {
27 namespace security {
28 
29 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<ValidityPeriod>));
30 BOOST_CONCEPT_ASSERT((WireEncodable<ValidityPeriod>));
32 BOOST_CONCEPT_ASSERT((WireDecodable<ValidityPeriod>));
33 static_assert(std::is_base_of<tlv::Error, ValidityPeriod::Error>::value,
34  "ValidityPeriod::Error must inherit from tlv::Error");
35 
36 static const size_t ISO_DATETIME_SIZE = 15;
37 static const size_t NOT_BEFORE_OFFSET = 0;
38 static const size_t NOT_AFTER_OFFSET = 1;
39 
40 using boost::chrono::time_point_cast;
41 
43  const time::system_clock::TimePoint& notAfter)
44  : m_notBefore(time_point_cast<TimePoint::duration>(notBefore + TimePoint::duration(1) -
45  time::system_clock::TimePoint::duration(1)))
46  , m_notAfter(time_point_cast<TimePoint::duration>(notAfter))
47 {
48 }
49 
51 {
52  wireDecode(block);
53 }
54 
55 template<encoding::Tag TAG>
56 size_t
58 {
59  size_t totalLength = 0;
60 
61  totalLength += prependStringBlock(encoder, tlv::NotAfter, time::toIsoString(m_notAfter));
62  totalLength += prependStringBlock(encoder, tlv::NotBefore, time::toIsoString(m_notBefore));
63 
64  totalLength += encoder.prependVarNumber(totalLength);
65  totalLength += encoder.prependVarNumber(tlv::ValidityPeriod);
66  return totalLength;
67 }
68 
69 template size_t
70 ValidityPeriod::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const;
71 
72 template size_t
73 ValidityPeriod::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const;
74 
75 const Block&
77 {
78  if (m_wire.hasWire())
79  return m_wire;
80 
81  EncodingEstimator estimator;
82  size_t estimatedSize = wireEncode(estimator);
83 
84  EncodingBuffer buffer(estimatedSize, 0);
85  wireEncode(buffer);
86 
87  m_wire = buffer.block();
88  m_wire.parse();
89 
90  return m_wire;
91 }
92 
93 void
95 {
96  if (!wire.hasWire()) {
97  BOOST_THROW_EXCEPTION(Error("The supplied block does not contain wire format"));
98  }
99 
100  m_wire = wire;
101  m_wire.parse();
102 
103  if (m_wire.type() != tlv::ValidityPeriod)
104  BOOST_THROW_EXCEPTION(Error("Unexpected TLV type when decoding ValidityPeriod"));
105 
106  if (m_wire.elements_size() != 2)
107  BOOST_THROW_EXCEPTION(Error("Does not have two sub-TLVs"));
108 
109  if (m_wire.elements()[NOT_BEFORE_OFFSET].type() != tlv::NotBefore ||
110  m_wire.elements()[NOT_BEFORE_OFFSET].value_size() != ISO_DATETIME_SIZE ||
111  m_wire.elements()[NOT_AFTER_OFFSET].type() != tlv::NotAfter ||
112  m_wire.elements()[NOT_AFTER_OFFSET].value_size() != ISO_DATETIME_SIZE) {
113  BOOST_THROW_EXCEPTION(Error("Invalid NotBefore or NotAfter field"));
114  }
115 
116  try {
117  m_notBefore = time_point_cast<TimePoint::duration>(
119  m_notAfter = time_point_cast<TimePoint::duration>(
121  }
122  catch (const std::bad_cast&) {
123  BOOST_THROW_EXCEPTION(Error("Invalid date format in NOT-BEFORE or NOT-AFTER field"));
124  }
125 }
126 
129  const time::system_clock::TimePoint& notAfter)
130 {
131  m_wire.reset();
132  m_notBefore = time_point_cast<TimePoint::duration>(notBefore + TimePoint::duration(1) -
133  time::system_clock::TimePoint::duration(1));
134  m_notAfter = time_point_cast<TimePoint::duration>(notAfter);
135  return *this;
136 }
137 
138 std::pair<time::system_clock::TimePoint, time::system_clock::TimePoint>
140 {
141  return std::make_pair(m_notBefore, m_notAfter);
142 }
143 
144 bool
146 {
147  return m_notBefore < now && now < m_notAfter;
148 }
149 
150 bool
152 {
153  return (this->m_notBefore == other.m_notBefore &&
154  this->m_notAfter == other.m_notAfter);
155 }
156 
157 bool
159 {
160  return !(*this == other);
161 }
162 
163 std::ostream&
164 operator<<(std::ostream& os, const ValidityPeriod& period)
165 {
166  os << "(" << time::toIsoString(period.getPeriod().first)
167  << ", " << time::toIsoString(period.getPeriod().second) << ")";
168  return os;
169 }
170 
171 } // namespace security
172 } // namespace ndn
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:471
Copyright (c) 2011-2015 Regents of the University of California.
bool operator!=(const ValidityPeriod &other) const
system_clock::TimePoint fromIsoString(const std::string &isoString)
Convert from the ISO string (YYYYMMDDTHHMMSS,fffffffff) representation to the internal time format...
Definition: time.cpp:147
const Block & wireEncode() const
Encode ValidityPeriod into TLV block.
bool operator==(const ValidityPeriod &other) const
EncodingImpl< EstimatorTag > EncodingEstimator
ValidityPeriod()=default
Set validity period (UNIX epoch, UNIX epoch) that is always invalid.
void parse() const
Parse wire buffer into subblocks.
Definition: block.cpp:322
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
bool isValid(const time::system_clock::TimePoint &now=time::system_clock::now()) const
Check if now falls within the validity period.
size_t prependStringBlock(EncodingImpl< TAG > &encoder, uint32_t type, const std::string &value)
Helper to prepend TLV block type type with value from a string value.
static const size_t NOT_BEFORE_OFFSET
std::string readString(const Block &block)
Helper to read a string value from a block.
ValidityPeriod & setPeriod(const time::system_clock::TimePoint &notBefore, const time::system_clock::TimePoint &notAfter)
Set validity period (notBefore, notAfter)
std::ostream & operator<<(std::ostream &os, CommandInterestValidator::ErrorCode error)
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:50
Abstraction of validity period.
EncodingImpl< EncoderTag > EncodingBuffer
static const size_t ISO_DATETIME_SIZE
std::pair< time::system_clock::TimePoint, time::system_clock::TimePoint > getPeriod() const
Get the stored validity period.
void reset()
Reset wire buffer of the element.
Definition: block.cpp:302
size_t elements_size() const
Definition: block.cpp:601
static const size_t NOT_AFTER_OFFSET
std::string toIsoString(const system_clock::TimePoint &timePoint)
Convert to the ISO string representation of the time (YYYYMMDDTHHMMSS,fffffffff)
Definition: time.cpp:130
time_point TimePoint
Definition: time.hpp:90
const element_container & elements() const
Get all subelements.
Definition: block.hpp:342
void wireDecode(const Block &wire)
Decode ValidityPeriod from TLV block.
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:34
a concept check for TLV abstraction with .wireDecode method and constructible from Block ...
Definition: concepts.hpp:70
uint32_t type() const
Definition: block.hpp:324