validity-period.hpp
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 
22 #ifndef NDN_CXX_SECURITY_VALIDITY_PERIOD_HPP
23 #define NDN_CXX_SECURITY_VALIDITY_PERIOD_HPP
24 
26 #include "ndn-cxx/encoding/tlv.hpp"
27 #include "ndn-cxx/util/time.hpp"
28 
29 namespace ndn {
30 namespace security {
31 
37 {
38 public:
39  class Error : public tlv::Error
40  {
41  public:
42  using tlv::Error::Error;
43  };
44 
53  static ValidityPeriod
54  makeRelative(time::seconds validFrom, time::seconds validUntil,
56 
60 
63  explicit
64  ValidityPeriod(const Block& block);
65 
75  const time::system_clock::TimePoint& notAfter);
76 
81  bool
83 
94  const time::system_clock::TimePoint& notAfter);
95 
98  std::pair<time::system_clock::TimePoint, time::system_clock::TimePoint>
99  getPeriod() const;
100 
103  template<encoding::Tag TAG>
104  size_t
105  wireEncode(EncodingImpl<TAG>& encoder) const;
106 
109  const Block&
110  wireEncode() const;
111 
115  void
116  wireDecode(const Block& wire);
117 
118 private: // EqualityComparable concept
119  // NOTE: the following "hidden friend" operators are available via
120  // argument-dependent lookup only and must be defined inline.
121 
122  friend bool
123  operator==(const ValidityPeriod& lhs, const ValidityPeriod& rhs)
124  {
125  return !(lhs != rhs);
126  }
127 
128  friend bool
129  operator!=(const ValidityPeriod& lhs, const ValidityPeriod& rhs)
130  {
131  return lhs.m_notBefore != rhs.m_notBefore ||
132  lhs.m_notAfter != rhs.m_notAfter;
133  }
134 
135 private:
136  typedef boost::chrono::time_point<time::system_clock, time::seconds> TimePoint;
137 
138  TimePoint m_notBefore;
139  TimePoint m_notAfter;
140 
141  mutable Block m_wire;
142 };
143 
145 
146 std::ostream&
147 operator<<(std::ostream& os, const ValidityPeriod& period);
148 
149 } // namespace security
150 } // namespace ndn
151 
152 #endif // NDN_CXX_SECURITY_VALIDITY_PERIOD_HPP
Represents a TLV element of the NDN packet format.
Definition: block.hpp:45
Represents a ValidityPeriod TLV element.
ValidityPeriod()
Set validity period [UNIX epoch + 1 nanosecond, UNIX epoch] that is always invalid.
bool isValid(const time::system_clock::TimePoint &now=time::system_clock::now()) const
Check if now falls within the validity period.
void wireDecode(const Block &wire)
Decode ValidityPeriod from 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.
std::pair< time::system_clock::TimePoint, time::system_clock::TimePoint > getPeriod() const
Get the stored validity period.
friend bool operator==(const ValidityPeriod &lhs, const ValidityPeriod &rhs)
friend bool operator!=(const ValidityPeriod &lhs, const ValidityPeriod &rhs)
ValidityPeriod & setPeriod(const time::system_clock::TimePoint &notBefore, const time::system_clock::TimePoint &notAfter)
Set validity period [notBefore, notAfter].
const Block & wireEncode() const
Encode ValidityPeriod into TLV block.
static time_point now() noexcept
Definition: time.cpp:46
time_point TimePoint
Definition: time.hpp:203
Represents an error in TLV encoding or decoding.
Definition: tlv.hpp:54
Error(const char *expectedType, uint32_t actualType)
Definition: tlv.cpp:27
#define NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
std::ostream & operator<<(std::ostream &os, const AdditionalDescription &desc)
boost::chrono::seconds seconds
Definition: time.hpp:47
@ ValidityPeriod
Definition: tlv.hpp:107
Definition: data.cpp:25