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-2023 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::security {
30 
35 class ValidityPeriod : private boost::equality_comparable<ValidityPeriod>
36 {
37 public:
38  class Error : public tlv::Error
39  {
40  public:
41  using tlv::Error::Error;
42  };
43 
52  static ValidityPeriod
53  makeRelative(time::seconds validFrom, time::seconds validUntil,
55 
60 
64  explicit
65  ValidityPeriod(const Block& block);
66 
75  const time::system_clock::time_point& notAfter);
76 
82  bool
84 
94  const time::system_clock::time_point& notAfter);
95 
99  std::pair<time::system_clock::time_point, time::system_clock::time_point>
100  getPeriod() const;
101 
105  template<encoding::Tag TAG>
106  size_t
107  wireEncode(EncodingImpl<TAG>& encoder) const;
108 
112  const Block&
113  wireEncode() const;
114 
123  void
124  wireDecode(const Block& wire);
125 
126 private:
127  using TimePoint = boost::chrono::time_point<time::system_clock, time::seconds>;
128 
129  static TimePoint
130  toTimePointFloor(const time::system_clock::time_point& t);
131 
132  static TimePoint
133  toTimePointCeil(const time::system_clock::time_point& t);
134 
135  static TimePoint
136  decodeTimePoint(const Block& element);
137 
138 private: // non-member operators
139  // NOTE: the following "hidden friend" operators are available via
140  // argument-dependent lookup only and must be defined inline.
141  // boost::equality_comparable provides != operator.
142 
143  friend bool
144  operator==(const ValidityPeriod& lhs, const ValidityPeriod& rhs)
145  {
146  return lhs.m_notBefore == rhs.m_notBefore &&
147  lhs.m_notAfter == rhs.m_notAfter;
148  }
149 
150 private:
151  TimePoint m_notBefore;
152  TimePoint m_notAfter;
153 
154  mutable Block m_wire;
155 };
156 
158 
159 std::ostream&
160 operator<<(std::ostream& os, const ValidityPeriod& period);
161 
162 } // namespace ndn::security
163 
164 #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()
Create a validity period that is invalid for any timepoint.
static ValidityPeriod makeRelative(time::seconds validFrom, time::seconds validUntil, const time::system_clock::time_point &now=time::system_clock::now())
Construct ValidityPeriod relative to a timepoint.
std::pair< time::system_clock::time_point, time::system_clock::time_point > getPeriod() const
Get the stored validity period.
bool isValid(const time::system_clock::time_point &now=time::system_clock::now()) const
Check if now falls within the validity period.
void wireDecode(const Block &wire)
Decode ValidityPeriod from TLV block.
ValidityPeriod & setPeriod(const time::system_clock::time_point &notBefore, const time::system_clock::time_point &notAfter)
Set validity period [notBefore, notAfter].
friend bool operator==(const ValidityPeriod &lhs, const ValidityPeriod &rhs)
const Block & wireEncode() const
Encode ValidityPeriod into TLV block.
static time_point now() noexcept
Definition: time.cpp:45
::boost::chrono::time_point< system_clock > time_point
Definition: time.hpp:205
Represents an error in TLV encoding or decoding.
Definition: tlv.hpp:54
Error(const char *expectedType, uint32_t actualType)
Definition: tlv.cpp:28
#define NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
Contains the ndn-cxx security framework.
std::ostream & operator<<(std::ostream &os, const AdditionalDescription &desc)
::boost::chrono::seconds seconds
Definition: time.hpp:51
@ ValidityPeriod
Definition: tlv.hpp:107