NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
interest.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2019 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_INTEREST_HPP
23 #define NDN_INTEREST_HPP
24 
26 #include "ndn-cxx/name.hpp"
28 #include "ndn-cxx/util/time.hpp"
29 
30 #include <boost/logic/tribool.hpp>
31 
32 namespace ndn {
33 
34 class Data;
35 
39 const time::milliseconds DEFAULT_INTEREST_LIFETIME = 4_s;
40 
43 class Interest : public PacketBase, public std::enable_shared_from_this<Interest>
44 {
45 public:
46  class Error : public tlv::Error
47  {
48  public:
49  using tlv::Error::Error;
50  };
51 
58  explicit
59  Interest(const Name& name = Name(), time::milliseconds lifetime = DEFAULT_INTEREST_LIFETIME);
60 
66  explicit
67  Interest(const Block& wire);
68 
71  template<encoding::Tag TAG>
72  size_t
73  wireEncode(EncodingImpl<TAG>& encoder) const;
74 
77  const Block&
78  wireEncode() const;
79 
82  void
83  wireDecode(const Block& wire);
84 
87  bool
88  hasWire() const
89  {
90  return m_wire.hasWire();
91  }
92 
100  std::string
101  toUri() const;
102 
103 public: // matching
109  bool
110  matchesData(const Data& data) const;
111 
116  bool
117  matchesInterest(const Interest& other) const;
118 
119 public: // element access
120  const Name&
121  getName() const noexcept
122  {
123  return m_name;
124  }
125 
129  Interest&
130  setName(const Name& name);
131 
146  static void
147  setDefaultCanBePrefix(bool canBePrefix)
148  {
149  s_defaultCanBePrefix = canBePrefix;
150  }
151 
154  bool
155  getCanBePrefix() const noexcept
156  {
157  return m_canBePrefix;
158  }
159 
163  Interest&
164  setCanBePrefix(bool canBePrefix)
165  {
166  m_canBePrefix = canBePrefix;
167  m_wire.reset();
168  m_isCanBePrefixSet = true;
169  return *this;
170  }
171 
174  bool
175  getMustBeFresh() const noexcept
176  {
177  return m_mustBeFresh;
178  }
179 
183  Interest&
184  setMustBeFresh(bool mustBeFresh)
185  {
186  m_mustBeFresh = mustBeFresh;
187  m_wire.reset();
188  return *this;
189  }
190 
191  const DelegationList&
192  getForwardingHint() const noexcept
193  {
194  return m_forwardingHint;
195  }
196 
197  Interest&
198  setForwardingHint(const DelegationList& value);
199 
210  template<typename Modifier>
211  Interest&
212  modifyForwardingHint(const Modifier& modifier)
213  {
214  modifier(m_forwardingHint);
215  m_wire.reset();
216  return *this;
217  }
218 
221  bool
222  hasNonce() const noexcept
223  {
224  return m_nonce.has_value();
225  }
226 
231  uint32_t
232  getNonce() const;
233 
236  Interest&
237  setNonce(uint32_t nonce);
238 
244  void
245  refreshNonce();
246 
247  time::milliseconds
248  getInterestLifetime() const noexcept
249  {
250  return m_interestLifetime;
251  }
252 
256  Interest&
257  setInterestLifetime(time::milliseconds lifetime);
258 
259  optional<uint8_t>
260  getHopLimit() const noexcept
261  {
262  return m_hopLimit;
263  }
264 
269  Interest&
270  setHopLimit(optional<uint8_t> hopLimit);
271 
272  bool
273  hasApplicationParameters() const noexcept
274  {
275  return !m_parameters.empty();
276  }
277 
278  Block
280  {
281  if (m_parameters.empty())
282  return {};
283  else
284  return m_parameters.front();
285  }
286 
299  Interest&
300  setApplicationParameters(const Block& parameters);
301 
312  Interest&
313  setApplicationParameters(const uint8_t* value, size_t length);
314 
323  Interest&
325 
331  Interest&
333 
334 public: // ParametersSha256DigestComponent support
335  static bool
337  {
338  return s_autoCheckParametersDigest;
339  }
340 
341  static void
343  {
344  s_autoCheckParametersDigest = b;
345  }
346 
354  bool
355  isParametersDigestValid() const;
356 
357 private:
358  void
359  setApplicationParametersInternal(Block parameters);
360 
361  NDN_CXX_NODISCARD shared_ptr<Buffer>
362  computeParametersDigest() const;
363 
371  void
372  addOrReplaceParametersDigestComponent();
373 
380  static ssize_t
381  findParametersDigestComponent(const Name& name);
382 
383 #ifdef NDN_CXX_HAVE_TESTS
384 public:
386  static bool s_errorIfCanBePrefixUnset;
387 #endif // NDN_CXX_HAVE_TESTS
388 
389 private:
390  static boost::logic::tribool s_defaultCanBePrefix;
391  static bool s_autoCheckParametersDigest;
392 
393  Name m_name;
394  DelegationList m_forwardingHint;
395  mutable optional<uint32_t> m_nonce;
396  time::milliseconds m_interestLifetime;
397  optional<uint8_t> m_hopLimit;
398  mutable bool m_isCanBePrefixSet = false;
399  bool m_canBePrefix = true;
400  bool m_mustBeFresh = false;
401 
402  // Stores the "Interest parameters", i.e., all maybe-unrecognized non-critical TLV
403  // elements that appear at the end of the Interest, starting from ApplicationParameters.
404  // If the Interest does not contain any ApplicationParameters TLV, this vector will
405  // be empty. Conversely, if this vector is not empty, the first element will always
406  // be an ApplicationParameters block. All blocks in this vector are covered by the
407  // digest in the ParametersSha256DigestComponent.
408  std::vector<Block> m_parameters;
409 
410  mutable Block m_wire;
411 };
412 
414 
415 std::ostream&
416 operator<<(std::ostream& os, const Interest& interest);
417 
418 } // namespace ndn
419 
420 #endif // NDN_INTEREST_HPP
ndn::Interest::getNonce
uint32_t getNonce() const
Get nonce value.
Definition: interest.cpp:400
ndn::Interest::setMustBeFresh
Interest & setMustBeFresh(bool mustBeFresh)
Add or remove MustBeFresh element.
Definition: interest.hpp:184
ndn::Interest::getCanBePrefix
bool getCanBePrefix() const noexcept
Check whether the CanBePrefix element is present.
Definition: interest.hpp:155
ndn::Interest::toUri
std::string toUri() const
Return a URI-like string that represents the Interest.
Definition: interest.cpp:323
ndn::Interest::setDefaultCanBePrefix
static void setDefaultCanBePrefix(bool canBePrefix)
Declare the default CanBePrefix setting of the application.
Definition: interest.hpp:147
ndn::Interest::setAutoCheckParametersDigest
static void setAutoCheckParametersDigest(bool b)
Definition: interest.hpp:342
ndn::NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS
NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(Interest)
ndn::Interest::setInterestLifetime
Interest & setInterestLifetime(time::milliseconds lifetime)
Set the Interest's lifetime.
Definition: interest.cpp:433
name.hpp
ndn::Interest::unsetApplicationParameters
Interest & unsetApplicationParameters()
Remove the ApplicationParameters element from this Interest.
Definition: interest.cpp:510
ndn::Interest::wireEncode
const Block & wireEncode() const
Encode into a Block according to NDN Packet Format v0.3.
Definition: interest.cpp:170
ndn::Interest::getAutoCheckParametersDigest
static bool getAutoCheckParametersDigest()
Definition: interest.hpp:336
ndn::Block::reset
void reset() noexcept
Reset the Block to a default-constructed state.
Definition: block.cpp:250
ndn::Interest::getMustBeFresh
bool getMustBeFresh() const noexcept
Check whether the MustBeFresh element is present.
Definition: interest.hpp:175
ndn::Interest::setApplicationParameters
Interest & setApplicationParameters(const Block &parameters)
Set ApplicationParameters from a Block.
Definition: interest.cpp:469
ndn::Interest::refreshNonce
void refreshNonce()
Change nonce value.
Definition: interest.cpp:420
ndn::Interest::setHopLimit
Interest & setHopLimit(optional< uint8_t > hopLimit)
Set the Interest's hop limit.
Definition: interest.cpp:446
ndn::Interest::hasWire
bool hasWire() const
Check if this instance has cached wire encoding.
Definition: interest.hpp:88
ndn::Name
Represents an absolute name.
Definition: name.hpp:44
ns3::ndn::Name
Name
Definition: ndn-common.cpp:25
NDN_CXX_NODISCARD
#define NDN_CXX_NODISCARD
Definition: backports.hpp:68
ndn::Interest::Error
Definition: interest.hpp:47
ndn::Interest::setCanBePrefix
Interest & setCanBePrefix(bool canBePrefix)
Add or remove CanBePrefix element.
Definition: interest.hpp:164
ndn::Interest::Interest
Interest(const Name &name=Name(), time::milliseconds lifetime=DEFAULT_INTEREST_LIFETIME)
Construct an Interest with given name and lifetime.
Definition: interest.cpp:52
ndn::Interest::getForwardingHint
const DelegationList & getForwardingHint() const noexcept
Definition: interest.hpp:192
ndn::Interest
Represents an Interest packet.
Definition: interest.hpp:44
ndn::DEFAULT_INTEREST_LIFETIME
const time::milliseconds DEFAULT_INTEREST_LIFETIME
default value for InterestLifetime
Definition: interest.hpp:39
ndn::DelegationList
represents a list of Delegations
Definition: delegation-list.hpp:38
ndn::Data
Represents a Data packet.
Definition: data.hpp:36
ndn::Interest::matchesData
bool matchesData(const Data &data) const
Check if Interest can be satisfied by data.
Definition: interest.cpp:333
ndn::Interest::getInterestLifetime
time::milliseconds getInterestLifetime() const noexcept
Definition: interest.hpp:248
ndn::Interest::setForwardingHint
Interest & setForwardingHint(const DelegationList &value)
Definition: interest.cpp:392
ndn::tlv::Data
@ Data
Definition: tlv.hpp:66
ndn::encoding::EncodingImpl
Definition: encoding-buffer-fwd.hpp:36
ndn::Interest::hasApplicationParameters
bool hasApplicationParameters() const noexcept
Definition: interest.hpp:273
ndn::Interest::hasNonce
bool hasNonce() const noexcept
Check if the Nonce element is present.
Definition: interest.hpp:222
ndn::Interest::setName
Interest & setName(const Name &name)
Set the Interest's name.
Definition: interest.cpp:375
ndn::Block
Represents a TLV element of NDN packet format.
Definition: block.hpp:43
ndn::Interest::getHopLimit
optional< uint8_t > getHopLimit() const noexcept
Definition: interest.hpp:260
packet-base.hpp
ndn::operator<<
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:322
ndn::name
Definition: name-component-types.hpp:33
ndn::Block::hasWire
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition: block.hpp:230
ndn::Interest::getApplicationParameters
Block getApplicationParameters() const
Definition: interest.hpp:279
ndn::Interest::getName
const Name & getName() const noexcept
Definition: interest.hpp:121
ndn::Interest::modifyForwardingHint
Interest & modifyForwardingHint(const Modifier &modifier)
Modify ForwardingHint in-place.
Definition: interest.hpp:212
time.hpp
delegation-list.hpp
ndn::PacketBase
base class to allow simple management of packet tags
Definition: packet-base.hpp:32
ndn::tlv::Error
represents an error in TLV encoding or decoding
Definition: tlv.hpp:53
ndn::Interest::setNonce
Interest & setNonce(uint32_t nonce)
Set nonce value.
Definition: interest.cpp:410
ndn::tlv::Error::Error
Error(const char *expectedType, uint32_t actualType)
Definition: tlv.cpp:27
ndn::Interest::isParametersDigestValid
bool isParametersDigestValid() const
Check if the ParametersSha256DigestComponent in the name is valid.
Definition: interest.cpp:524
ndn::ConstBufferPtr
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:126
ndn::Interest::matchesInterest
bool matchesInterest(const Interest &other) const
Check if this Interest matches other.
Definition: interest.cpp:365
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
ndn::Interest::wireDecode
void wireDecode(const Block &wire)
Decode from wire according to NDN Packet Format v0.3.
Definition: interest.cpp:186