NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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-2018 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 #include <boost/lexical_cast.hpp>
26 
27 namespace ndn {
28 
29 static const name::Component KEYWORD_PA_COMP = "20025041"_block;
30 
32 
34  : m_data(std::move(data))
35 {
36  if (m_data->getContentType() != tlv::ContentType_PrefixAnn) {
37  BOOST_THROW_EXCEPTION(Error("Data is not a prefix announcement: ContentType is " +
38  boost::lexical_cast<std::string>(m_data->getContentType())));
39  }
40 
41  const Name& dataName = m_data->getName();
42  if (dataName.size() < 3 || dataName[-3] != KEYWORD_PA_COMP ||
43  !dataName[-2].isVersion() || !dataName[-1].isSegment()) {
44  BOOST_THROW_EXCEPTION(Error("Data is not a prefix announcement: wrong name structure"));
45  }
46  m_announcedName = dataName.getPrefix(-3);
47 
48  const Block& payload = m_data->getContent();
49  payload.parse();
50 
51  m_expiration = time::milliseconds(readNonNegativeInteger(payload.get(tlv::nfd::ExpirationPeriod)));
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  BOOST_THROW_EXCEPTION(Error("unrecognized element of critical type " +
62  to_string(element.type())));
63  }
64  }
65 }
66 
67 const Data&
69  optional<uint64_t> version) const
70 {
71  if (!m_data) {
72  Name dataName = m_announcedName;
73  dataName.append(KEYWORD_PA_COMP);
74  dataName.appendVersion(version.value_or(time::toUnixTimestamp(time::system_clock::now()).count()));
75  dataName.appendSegment(0);
76  m_data.emplace(dataName);
77  m_data->setContentType(tlv::ContentType_PrefixAnn);
78 
79  Block content(tlv::Content);
81  m_expiration.count()));
82  if (m_validity) {
83  content.push_back(m_validity->wireEncode());
84  }
85  content.encode();
86  m_data->setContent(content);
87 
88  keyChain.sign(*m_data, si);
89  }
90  return *m_data;
91 }
92 
95 {
96  m_data.reset();
97  m_announcedName = std::move(name);
98  return *this;
99 }
100 
102 PrefixAnnouncement::setExpiration(time::milliseconds expiration)
103 {
104  if (expiration < 0_ms) {
105  BOOST_THROW_EXCEPTION(std::invalid_argument("expiration period is negative"));
106  }
107  m_data.reset();
108  m_expiration = expiration;
109  return *this;
110 }
111 
113 PrefixAnnouncement::setValidityPeriod(optional<security::ValidityPeriod> validity)
114 {
115  m_data.reset();
116  m_validity = std::move(validity);
117  return *this;
118 }
119 
120 bool
122 {
123  return lhs.getAnnouncedName() == rhs.getAnnouncedName() &&
124  lhs.getExpiration() == rhs.getExpiration() &&
125  lhs.getValidityPeriod() == rhs.getValidityPeriod();
126 }
127 
128 std::ostream&
129 operator<<(std::ostream& os, const PrefixAnnouncement& pa)
130 {
131  os << pa.getAnnouncedName() << " expires=" << pa.getExpiration();
132  if (pa.getValidityPeriod()) {
133  os << " validity=" << *pa.getValidityPeriod();
134  }
135  return os;
136 }
137 
138 } // namespace ndn
PartialName getPrefix(ssize_t nComponents) const
Extract a prefix of the name.
Definition: name.hpp:203
Copyright (c) 2011-2015 Regents of the University of California.
The interface of signing key management.
Definition: key-chain.hpp:46
constexpr bool isCriticalType(uint32_t type)
Determine whether a TLV-TYPE is "critical" for evolvability purpose.
Definition: tlv.hpp:171
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:322
PrefixAnnouncement & setValidityPeriod(optional< security::ValidityPeriod > validity)
Set absolute validity period.
prefix announcement
Definition: tlv.hpp:159
void parse() const
Parse TLV-VALUE into sub elements.
Definition: block.cpp:333
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
static time_point now() noexcept
Definition: time.cpp:46
const Data & toData(KeyChain &keyChain, const ndn::security::SigningInfo &si=KeyChain::getDefaultSigningInfo(), optional< uint64_t > version=nullopt) const
Create a Data packet representing the prefix announcement, if it does not exist.
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:249
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:214
A prefix announcement object that represents an application's intent of registering a prefix toward i...
const element_container & elements() const
Get container of sub elements.
Definition: block.hpp:361
element_const_iterator elements_end() const
Equivalent to elements().end()
Definition: block.hpp:377
const Block & get(uint32_t type) const
Get the first sub element of specified TLV-TYPE.
Definition: block.cpp:422
static const name::Component KEYWORD_PA_COMP
element_const_iterator find(uint32_t type) const
Find the first sub element of specified TLV-TYPE.
Definition: block.cpp:434
PrefixAnnouncement & setExpiration(time::milliseconds expiration)
Set relative expiration period.
const Name & getAnnouncedName() const
Return announced name.
Represents an absolute name.
Definition: name.hpp:43
void push_back(const Block &element)
Append a sub element.
Definition: block.cpp:465
PrefixAnnouncement & setAnnouncedName(Name name)
Set announced name.
size_t size() const
Get number of components.
Definition: name.hpp:147
void sign(Data &data, const SigningInfo &params=getDefaultSigningInfo())
Sign data according to the supplied signing information.
Definition: key-chain.cpp:430
optional< security::ValidityPeriod > getValidityPeriod() const
Return absolute validity period.
Represents a name component.
void encode()
Encode sub elements into TLV-VALUE.
Definition: block.cpp:361
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:313
PrefixAnnouncement()
Construct an empty prefix announcement.
std::string to_string(const V &v)
Definition: backports.hpp:67
time::milliseconds getExpiration() const
Return relative expiration period.
Represents a Data packet.
Definition: data.hpp:35
Name & appendSegment(uint64_t segmentNo)
Append a segment number (sequential) component.
Definition: name.hpp:371