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-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 
24 
25 namespace ndn {
26 
28 
30  : m_data(std::move(data))
31 {
32  const Name& dataName = m_data->getName();
33  if (dataName.size() < 3 || dataName[-3] != getKeywordComponent() ||
34  !dataName[-2].isVersion() || !dataName[-1].isSegment()) {
35  NDN_THROW(Error("Data is not a prefix announcement: wrong name structure"));
36  }
37  m_announcedName = dataName.getPrefix(-3);
38 
39  if (m_data->getContentType() != tlv::ContentType_PrefixAnn) {
40  NDN_THROW(Error("Data is not a prefix announcement: ContentType is " +
41  to_string(m_data->getContentType())));
42  }
43 
44  if (m_data->getContent().value_size() == 0) {
45  NDN_THROW(Error("Prefix announcement is empty"));
46  }
47 
48  const Block& payload = m_data->getContent();
49  payload.parse();
50 
52 
53  auto validityElement = payload.find(tlv::ValidityPeriod);
54  if (validityElement != payload.elements_end()) {
55  m_validity.emplace(*validityElement);
56  }
57 
58  for (const Block& element : payload.elements()) {
59  if (element.type() != tlv::nfd::ExpirationPeriod && element.type() != tlv::ValidityPeriod &&
60  tlv::isCriticalType(element.type())) {
61  NDN_THROW(Error("Unrecognized element of critical type " + to_string(element.type())));
62  }
63  }
64 }
65 
66 const Data&
68  std::optional<uint64_t> version) const
69 {
70  if (!m_data) {
71  Name dataName = m_announcedName;
72  dataName.append(getKeywordComponent())
73  .appendVersion(version.value_or(time::toUnixTimestamp(time::system_clock::now()).count()))
74  .appendSegment(0);
75  m_data.emplace(dataName);
76  m_data->setContentType(tlv::ContentType_PrefixAnn);
77 
78  Block content(tlv::Content);
80  m_expiration.count()));
81  if (m_validity) {
82  content.push_back(m_validity->wireEncode());
83  }
84  content.encode();
85  m_data->setContent(content);
86 
87  keyChain.sign(*m_data, si);
88  }
89  return *m_data;
90 }
91 
94 {
95  m_data.reset();
96  m_announcedName = std::move(name);
97  return *this;
98 }
99 
102 {
103  if (expiration < 0_ms) {
104  NDN_THROW(std::invalid_argument("expiration period is negative"));
105  }
106  m_data.reset();
107  m_expiration = expiration;
108  return *this;
109 }
110 
112 PrefixAnnouncement::setValidityPeriod(std::optional<security::ValidityPeriod> validity)
113 {
114  m_data.reset();
115  m_validity = std::move(validity);
116  return *this;
117 }
118 
119 const name::Component&
121 {
122  static const name::Component nc(tlv::KeywordNameComponent, {'P', 'A'});
123  return nc;
124 }
125 
126 std::ostream&
127 operator<<(std::ostream& os, const PrefixAnnouncement& pa)
128 {
129  os << pa.getAnnouncedName() << " expires=" << pa.getExpiration();
130  if (pa.getValidityPeriod()) {
131  os << " validity=" << *pa.getValidityPeriod();
132  }
133  return os;
134 }
135 
136 } // namespace ndn
Represents a TLV element of the NDN packet format.
Definition: block.hpp:45
element_const_iterator find(uint32_t type) const
Find the first sub-element of the specified TLV-TYPE.
Definition: block.cpp:425
const element_container & elements() const noexcept
Get container of sub-elements.
Definition: block.hpp:424
element_const_iterator elements_end() const noexcept
Equivalent to elements().end().
Definition: block.hpp:442
void push_back(const Block &element)
Append a sub-element.
Definition: block.cpp:456
void encode()
Encode sub-elements into TLV-VALUE.
Definition: block.cpp:353
void parse() const
Parse TLV-VALUE into sub-elements.
Definition: block.cpp:326
const Block & get(uint32_t type) const
Return the first sub-element of the specified TLV-TYPE.
Definition: block.cpp:414
Represents a Data packet.
Definition: data.hpp:39
Represents an absolute name.
Definition: name.hpp:45
PartialName getPrefix(ssize_t nComponents) const
Returns a prefix of the name.
Definition: name.hpp:241
Name & appendSegment(uint64_t segmentNo)
Append a segment number (sequential) component.
Definition: name.hpp:431
Name & appendVersion(const std::optional< uint64_t > &version=std::nullopt)
Append a version component.
Definition: name.cpp:205
size_t size() const noexcept
Returns the number of components.
Definition: name.hpp:180
Name & append(const Component &component)
Append a name component.
Definition: name.hpp:308
A prefix announcement object that represents an application's intent of registering a prefix toward i...
const Name & getAnnouncedName() const
Return announced name.
std::optional< security::ValidityPeriod > getValidityPeriod() const
Return absolute validity period.
PrefixAnnouncement & setValidityPeriod(std::optional< security::ValidityPeriod > validity)
Set absolute validity period.
time::milliseconds getExpiration() const
Return relative expiration period.
PrefixAnnouncement & setExpiration(time::milliseconds expiration)
Set relative expiration period.
PrefixAnnouncement()
Construct an empty prefix announcement.
PrefixAnnouncement & setAnnouncedName(Name name)
Set announced name.
const Data & toData(KeyChain &keyChain, const ndn::security::SigningInfo &si=security::SigningInfo(), std::optional< uint64_t > version=std::nullopt) const
Create a Data packet representing the prefix announcement, if it does not exist.
static const name::Component & getKeywordComponent()
Returns the well-known keyword name component used for prefix announcements (32=PA)
Represents a name component.
The main interface for signing key management.
Definition: key-chain.hpp:87
void sign(Data &data, const SigningInfo &params=SigningInfo())
Sign a Data packet according to the supplied signing information.
Definition: key-chain.cpp:400
Signing parameters passed to KeyChain.
static time_point now() noexcept
Definition: time.cpp:45
#define NDN_THROW(e)
Definition: exception.hpp:56
uint64_t readNonNegativeInteger(const Block &block)
Read a non-negative integer from a TLV element.
Block makeNonNegativeIntegerBlock(uint32_t type, uint64_t value)
Create a TLV block containing a non-negative integer.
std::string to_string(const errinfo_stacktrace &x)
Definition: exception.cpp:30
constexpr Duration toUnixTimestamp(const system_clock::time_point &tp)
Convert system_clock::time_point to UNIX timestamp.
Definition: time.hpp:265
::boost::chrono::milliseconds milliseconds
Definition: time.hpp:52
@ ExpirationPeriod
Definition: tlv-nfd.hpp:45
@ Content
Definition: tlv.hpp:93
@ ValidityPeriod
Definition: tlv.hpp:107
@ KeywordNameComponent
Definition: tlv.hpp:75
constexpr bool isCriticalType(uint32_t type) noexcept
Determine whether a TLV-TYPE is "critical" for evolvability purpose.
Definition: tlv.hpp:162
@ ContentType_PrefixAnn
prefix announcement
Definition: tlv.hpp:150
Definition: data.cpp:25
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:377