NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: 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; -*- */
2 /*
3  * Copyright (c) 2013-2022 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  */
21 
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>));
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 
45 {
46  return ValidityPeriod(now + validFrom, now + validUntil);
47 }
48 
50  : ValidityPeriod(time::system_clock::TimePoint() + 1_ns,
51  time::system_clock::TimePoint())
52 {
53 }
54 
56  const time::system_clock::TimePoint& notAfter)
57  : m_notBefore(time_point_cast<TimePoint::duration>(notBefore + TimePoint::duration(1) -
58  time::system_clock::TimePoint::duration(1)))
59  , m_notAfter(time_point_cast<TimePoint::duration>(notAfter))
60 {
61 }
62 
64 {
65  wireDecode(block);
66 }
67 
68 template<encoding::Tag TAG>
69 size_t
71 {
72  size_t totalLength = 0;
73 
74  totalLength += prependStringBlock(encoder, tlv::NotAfter, time::toIsoString(m_notAfter));
75  totalLength += prependStringBlock(encoder, tlv::NotBefore, time::toIsoString(m_notBefore));
76 
77  totalLength += encoder.prependVarNumber(totalLength);
78  totalLength += encoder.prependVarNumber(tlv::ValidityPeriod);
79  return totalLength;
80 }
81 
83 
84 const Block&
86 {
87  if (m_wire.hasWire())
88  return m_wire;
89 
90  EncodingEstimator estimator;
91  size_t estimatedSize = wireEncode(estimator);
92 
93  EncodingBuffer buffer(estimatedSize, 0);
94  wireEncode(buffer);
95 
96  m_wire = buffer.block();
97  m_wire.parse();
98 
99  return m_wire;
100 }
101 
102 void
104 {
105  if (!wire.hasWire()) {
106  NDN_THROW(Error("The supplied block does not contain wire format"));
107  }
108 
109  m_wire = wire;
110  m_wire.parse();
111 
112  if (m_wire.type() != tlv::ValidityPeriod)
113  NDN_THROW(Error("ValidityPeriod", m_wire.type()));
114 
115  if (m_wire.elements_size() != 2)
116  NDN_THROW(Error("ValidityPeriod does not have two sub-TLVs"));
117 
118  if (m_wire.elements()[NOT_BEFORE_OFFSET].type() != tlv::NotBefore ||
119  m_wire.elements()[NOT_BEFORE_OFFSET].value_size() != ISO_DATETIME_SIZE ||
120  m_wire.elements()[NOT_AFTER_OFFSET].type() != tlv::NotAfter ||
121  m_wire.elements()[NOT_AFTER_OFFSET].value_size() != ISO_DATETIME_SIZE) {
122  NDN_THROW(Error("Invalid NotBefore or NotAfter field"));
123  }
124 
125  try {
126  m_notBefore = time_point_cast<TimePoint::duration>(
128  m_notAfter = time_point_cast<TimePoint::duration>(
130  }
131  catch (const std::bad_cast&) {
132  NDN_THROW(Error("Invalid date format in NOT-BEFORE or NOT-AFTER field"));
133  }
134 }
135 
138  const time::system_clock::TimePoint& notAfter)
139 {
140  m_wire.reset();
141  m_notBefore = time_point_cast<TimePoint::duration>(notBefore + TimePoint::duration(1) -
143  m_notAfter = time_point_cast<TimePoint::duration>(notAfter);
144  return *this;
145 }
146 
147 std::pair<time::system_clock::TimePoint, time::system_clock::TimePoint>
149 {
150  return {m_notBefore, m_notAfter};
151 }
152 
153 bool
155 {
156  return m_notBefore <= now && now <= m_notAfter;
157 }
158 
159 std::ostream&
160 operator<<(std::ostream& os, const ValidityPeriod& period)
161 {
162  os << "(" << time::toIsoString(period.getPeriod().first)
163  << ", " << time::toIsoString(period.getPeriod().second) << ")";
164  return os;
165 }
166 
167 } // namespace security
168 } // namespace ndn
boost::chrono::seconds seconds
Definition: time.hpp:47
Copyright (c) 2011-2015 Regents of the University of California.
const Block & wireEncode() const
Encode ValidityPeriod into TLV block.
static ValidityPeriod makeRelative(time::seconds validFrom, time::seconds validUntil, const time::system_clock::TimePoint &now=time::system_clock::now())
Construct ValidityPeriod relative to a timepoint.
void parse() const
Parse TLV-VALUE into sub-elements.
Definition: block.cpp:324
boost::chrono::duration< Rep, Period > duration
Definition: time.hpp:34
Represents a TLV element of the NDN packet format.
Definition: block.hpp:44
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)
Prepend a TLV element containing a string.
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition: block.hpp:221
static const size_t NOT_BEFORE_OFFSET
std::string readString(const Block &block)
Read TLV-VALUE of a TLV element as a string.
ValidityPeriod & setPeriod(const time::system_clock::TimePoint &notBefore, const time::system_clock::TimePoint &notAfter)
Set validity period [notBefore, notAfter].
const element_container & elements() const
Get container of sub-elements.
Definition: block.hpp:425
#define NDN_THROW(e)
Definition: exception.hpp:61
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:60
Represents a ValidityPeriod TLV element.
void reset() noexcept
Reset the Block to a default-constructed state.
Definition: block.cpp:254
static const size_t ISO_DATETIME_SIZE
std::pair< time::system_clock::TimePoint, time::system_clock::TimePoint > getPeriod() const
Get the stored validity period.
size_t elements_size() const
Equivalent to elements().size()
Definition: block.hpp:449
static const size_t NOT_AFTER_OFFSET
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(SafeBag)
system_clock::time_point fromIsoString(const std::string &isoString)
Convert from the ISO 8601 basic string format (YYYYMMDDTHHMMSS,fffffffff) to the internal time format...
Definition: time.cpp:168
std::string toIsoString(const system_clock::time_point &timePoint)
Convert to the ISO 8601 string representation, basic format (YYYYMMDDTHHMMSS,fffffffff).
Definition: time.cpp:143
uint32_t type() const noexcept
Return the TLV-TYPE of the Block.
Definition: block.hpp:277
ValidityPeriod()
Set validity period [UNIX epoch + 1 nanosecond, UNIX epoch] that is always invalid.
void wireDecode(const Block &wire)
Decode ValidityPeriod from TLV block.
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:44
a concept check for TLV abstraction with .wireDecode method and constructible from Block ...
Definition: concepts.hpp:80
std::ostream & operator<<(std::ostream &os, const SigningInfo &si)
EncodingImpl< EncoderTag > EncodingBuffer
EncodingImpl< EstimatorTag > EncodingEstimator
time_point TimePoint
Definition: time.hpp:203