prefix-announcement.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2020 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 
24 
25 namespace ndn {
26 
27 const name::Component KEYWORD_PA_COMP = "20 02 5041"_block; // 32=PA
28 
30 
32  : m_data(std::move(data))
33 {
34  const Name& dataName = m_data->getName();
35  if (dataName.size() < 3 || dataName[-3] != KEYWORD_PA_COMP ||
36  !dataName[-2].isVersion() || !dataName[-1].isSegment()) {
37  NDN_THROW(Error("Data is not a prefix announcement: wrong name structure"));
38  }
39  m_announcedName = dataName.getPrefix(-3);
40 
41  if (m_data->getContentType() != tlv::ContentType_PrefixAnn) {
42  NDN_THROW(Error("Data is not a prefix announcement: ContentType is " +
43  to_string(m_data->getContentType())));
44  }
45 
46  if (m_data->getContent().value_size() == 0) {
47  NDN_THROW(Error("Prefix announcement is empty"));
48  }
49 
50  const Block& payload = m_data->getContent();
51  payload.parse();
52 
53  m_expiration = time::milliseconds(readNonNegativeInteger(payload.get(tlv::nfd::ExpirationPeriod)));
54 
55  auto validityElement = payload.find(tlv::ValidityPeriod);
56  if (validityElement != payload.elements_end()) {
57  m_validity.emplace(*validityElement);
58  }
59 
60  for (const Block& element : payload.elements()) {
61  if (element.type() != tlv::nfd::ExpirationPeriod && element.type() != tlv::ValidityPeriod &&
62  tlv::isCriticalType(element.type())) {
63  NDN_THROW(Error("Unrecognized element of critical type " + to_string(element.type())));
64  }
65  }
66 }
67 
68 const Data&
70  optional<uint64_t> version) const
71 {
72  if (!m_data) {
73  Name dataName = m_announcedName;
74  dataName.append(KEYWORD_PA_COMP);
75  dataName.appendVersion(version.value_or(time::toUnixTimestamp(time::system_clock::now()).count()));
76  dataName.appendSegment(0);
77  m_data.emplace(dataName);
78  m_data->setContentType(tlv::ContentType_PrefixAnn);
79 
80  Block content(tlv::Content);
82  m_expiration.count()));
83  if (m_validity) {
84  content.push_back(m_validity->wireEncode());
85  }
86  content.encode();
87  m_data->setContent(content);
88 
89  keyChain.sign(*m_data, si);
90  }
91  return *m_data;
92 }
93 
96 {
97  m_data.reset();
98  m_announcedName = std::move(name);
99  return *this;
100 }
101 
103 PrefixAnnouncement::setExpiration(time::milliseconds expiration)
104 {
105  if (expiration < 0_ms) {
106  NDN_THROW(std::invalid_argument("expiration period is negative"));
107  }
108  m_data.reset();
109  m_expiration = expiration;
110  return *this;
111 }
112 
114 PrefixAnnouncement::setValidityPeriod(optional<security::ValidityPeriod> validity)
115 {
116  m_data.reset();
117  m_validity = std::move(validity);
118  return *this;
119 }
120 
121 bool
123 {
124  return lhs.getAnnouncedName() == rhs.getAnnouncedName() &&
125  lhs.getExpiration() == rhs.getExpiration() &&
126  lhs.getValidityPeriod() == rhs.getValidityPeriod();
127 }
128 
129 std::ostream&
130 operator<<(std::ostream& os, const PrefixAnnouncement& pa)
131 {
132  os << pa.getAnnouncedName() << " expires=" << pa.getExpiration();
133  if (pa.getValidityPeriod()) {
134  os << " validity=" << *pa.getValidityPeriod();
135  }
136  return os;
137 }
138 
139 } // namespace ndn
PartialName getPrefix(ssize_t nComponents) const
Returns a prefix of the name.
Definition: name.hpp:212
Definition: data.cpp:26
The interface of signing key management.
Definition: key-chain.hpp:45
std::string to_string(const T &val)
Definition: backports.hpp:101
constexpr bool isCriticalType(uint32_t type)
Determine whether a TLV-TYPE is "critical" for evolvability purpose.
Definition: tlv.hpp:177
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:383
PrefixAnnouncement & setValidityPeriod(optional< security::ValidityPeriod > validity)
Set absolute validity period.
prefix announcement
Definition: tlv.hpp:165
STL namespace.
void sign(Data &data, const SigningInfo &params=SigningInfo())
Sign a Data packet according to the supplied signing information.
Definition: key-chain.cpp:456
void parse() const
Parse TLV-VALUE into sub-elements.
Definition: block.cpp:325
Represents a TLV element of the NDN packet format.
Definition: block.hpp:42
static time_point now() noexcept
Definition: time.cpp:46
Block makeNonNegativeIntegerBlock(uint32_t type, uint64_t value)
Create a TLV block containing a non-negative integer.
Name & append(const Component &component)
Append a component.
Definition: name.hpp:278
const Data & toData(KeyChain &keyChain, const ndn::security::SigningInfo &si=security::SigningInfo(), optional< uint64_t > version=nullopt) const
Create a Data packet representing the prefix announcement, if it does not exist.
Signing parameters passed to KeyChain.
uint64_t readNonNegativeInteger(const Block &block)
Read a non-negative integer from a TLV element.
Name & appendVersion(optional< uint64_t > version=nullopt)
Append a version component.
Definition: name.cpp:230
A prefix announcement object that represents an application&#39;s intent of registering a prefix toward i...
const element_container & elements() const
Get container of sub-elements.
Definition: block.hpp:378
#define NDN_THROW(e)
Definition: exception.hpp:61
element_const_iterator elements_end() const
Equivalent to elements().end()
Definition: block.hpp:394
const Block & get(uint32_t type) const
Return the first sub-element of the specified TLV-TYPE.
Definition: block.cpp:414
const name::Component KEYWORD_PA_COMP
element_const_iterator find(uint32_t type) const
Find the first sub-element of the specified TLV-TYPE.
Definition: block.cpp:426
PrefixAnnouncement & setExpiration(time::milliseconds expiration)
Set relative expiration period.
const Name & getAnnouncedName() const
Return announced name.
Represents an absolute name.
Definition: name.hpp:44
void push_back(const Block &element)
Append a sub-element.
Definition: block.cpp:457
PrefixAnnouncement & setAnnouncedName(Name name)
Set announced name.
size_t size() const
Returns the number of components.
Definition: name.hpp:154
optional< security::ValidityPeriod > getValidityPeriod() const
Return absolute validity period.
Represents a name component.
void encode()
Encode sub-elements into TLV-VALUE.
Definition: block.cpp:353
milliseconds toUnixTimestamp(const system_clock::TimePoint &point)
Convert system_clock::TimePoint to UNIX timestamp.
Definition: time.cpp:113
bool operator==(const Data &lhs, const Data &rhs)
Definition: data.cpp:373
PrefixAnnouncement()
Construct an empty prefix announcement.
time::milliseconds getExpiration() const
Return relative expiration period.
Represents a Data packet.
Definition: data.hpp:39
Name & appendSegment(uint64_t segmentNo)
Append a segment number (sequential) component.
Definition: name.hpp:416