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-2022 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_INTEREST_HPP
23 #define NDN_CXX_INTEREST_HPP
24 
26 #include "ndn-cxx/name.hpp"
30 #include "ndn-cxx/util/time.hpp"
31 
32 #include <array>
33 
34 #include <boost/endian/conversion.hpp>
35 
36 namespace ndn {
37 
38 class Data;
39 
44 
48 class Interest : public PacketBase, public std::enable_shared_from_this<Interest>
49 {
50 public:
51  class Error : public tlv::Error
52  {
53  public:
54  using tlv::Error::Error;
55  };
56 
57  class Nonce final : public std::array<uint8_t, 4>
58  {
59  using Base = std::array<uint8_t, 4>;
60 
61  public:
62  Nonce() = default;
63 
64  // implicit conversion from uint32_t
65  Nonce(uint32_t n) noexcept
66  {
67  boost::endian::native_to_big_inplace(n);
68  std::memcpy(data(), &n, sizeof(n));
69  }
70 
71  Nonce(uint8_t n1, uint8_t n2, uint8_t n3, uint8_t n4) noexcept
72  {
73  data()[0] = n1;
74  data()[1] = n2;
75  data()[2] = n3;
76  data()[3] = n4;
77  }
78 
79  private: // non-member operators
80  // NOTE: the following "hidden friend" operators are available via
81  // argument-dependent lookup only and must be defined inline.
82 
83  friend bool
84  operator==(const Nonce& lhs, const Nonce& rhs) noexcept
85  {
86  return static_cast<const Base&>(lhs) == static_cast<const Base&>(rhs);
87  }
88 
89  friend bool
90  operator!=(const Nonce& lhs, const Nonce& rhs) noexcept
91  {
92  return static_cast<const Base&>(lhs) != static_cast<const Base&>(rhs);
93  }
94 
95  friend std::ostream&
96  operator<<(std::ostream& os, const Nonce& nonce)
97  {
98  printHex(os, nonce, false);
99  return os;
100  }
101  };
102 
109  explicit
111 
117  explicit
118  Interest(const Block& wire);
119 
122  template<encoding::Tag TAG>
123  size_t
124  wireEncode(EncodingImpl<TAG>& encoder) const;
125 
128  const Block&
129  wireEncode() const;
130 
133  void
134  wireDecode(const Block& wire);
135 
138  bool
139  hasWire() const noexcept
140  {
141  return m_wire.hasWire();
142  }
143 
151  std::string
152  toUri() const;
153 
154 public: // matching
160  bool
161  matchesData(const Data& data) const;
162 
167  bool
168  matchesInterest(const Interest& other) const;
169 
170 public: // element access
171  const Name&
172  getName() const noexcept
173  {
174  return m_name;
175  }
176 
180  Interest&
181  setName(const Name& name);
182 
185  bool
186  getCanBePrefix() const noexcept
187  {
188  return m_canBePrefix;
189  }
190 
194  Interest&
195  setCanBePrefix(bool canBePrefix)
196  {
197  m_canBePrefix = canBePrefix;
198  m_wire.reset();
199  return *this;
200  }
201 
204  bool
205  getMustBeFresh() const noexcept
206  {
207  return m_mustBeFresh;
208  }
209 
213  Interest&
214  setMustBeFresh(bool mustBeFresh)
215  {
216  m_mustBeFresh = mustBeFresh;
217  m_wire.reset();
218  return *this;
219  }
220 
221  span<const Name>
222  getForwardingHint() const noexcept
223  {
224  return m_forwardingHint;
225  }
226 
227  Interest&
228  setForwardingHint(std::vector<Name> value);
229 
232  bool
233  hasNonce() const noexcept
234  {
235  return m_nonce.has_value();
236  }
237 
242  Nonce
243  getNonce() const;
244 
249  Interest&
250  setNonce(optional<Nonce> nonce);
251 
257  void
258  refreshNonce();
259 
261  getInterestLifetime() const noexcept
262  {
263  return m_interestLifetime;
264  }
265 
269  Interest&
271 
272  optional<uint8_t>
273  getHopLimit() const noexcept
274  {
275  return m_hopLimit;
276  }
277 
282  Interest&
283  setHopLimit(optional<uint8_t> hopLimit);
284 
288  bool
289  hasApplicationParameters() const noexcept
290  {
291  return !m_parameters.empty();
292  }
293 
301  Block
303  {
304  if (m_parameters.empty())
305  return {};
306  else
307  return m_parameters.front();
308  }
309 
323  Interest&
324  setApplicationParameters(const Block& block);
325 
335  Interest&
336  setApplicationParameters(span<const uint8_t> value);
337 
350  [[deprecated("use the overload that takes a span<>")]]
351  Interest&
352  setApplicationParameters(const uint8_t* value, size_t length);
353 
363  Interest&
365 
374  Interest&
376 
381  bool
382  isSigned() const noexcept;
383 
387  optional<SignatureInfo>
388  getSignatureInfo() const;
389 
392  Interest&
394 
399  Block
400  getSignatureValue() const;
401 
408  Interest&
410 
415  InputBuffers
416  extractSignedRanges() const;
417 
418 public: // ParametersSha256DigestComponent support
419  static bool
421  {
422  return s_autoCheckParametersDigest;
423  }
424 
425  static void
427  {
428  s_autoCheckParametersDigest = b;
429  }
430 
438  bool
439  isParametersDigestValid() const;
440 
441 private:
442  void
443  setApplicationParametersInternal(Block parameters);
444 
445  NDN_CXX_NODISCARD shared_ptr<Buffer>
446  computeParametersDigest() const;
447 
455  void
456  addOrReplaceParametersDigestComponent();
457 
464  static ssize_t
465  findParametersDigestComponent(const Name& name);
466 
467  std::vector<Block>::const_iterator
468  findFirstParameter(uint32_t type) const;
469 
470 private:
471  static bool s_autoCheckParametersDigest;
472 
473  Name m_name;
474  std::vector<Name> m_forwardingHint;
475  mutable optional<Nonce> m_nonce;
476  time::milliseconds m_interestLifetime = DEFAULT_INTEREST_LIFETIME;
477  optional<uint8_t> m_hopLimit;
478  bool m_canBePrefix = false;
479  bool m_mustBeFresh = false;
480 
481  // Stores the "Interest parameters", i.e., all maybe-unrecognized non-critical TLV
482  // elements that appear at the end of the Interest, starting from ApplicationParameters.
483  // If the Interest does not contain any ApplicationParameters TLV, this vector will
484  // be empty. Conversely, if this vector is not empty, the first element will always
485  // be an ApplicationParameters block. All blocks in this vector are covered by the
486  // digest in the ParametersSha256DigestComponent.
487  std::vector<Block> m_parameters;
488 
489  mutable Block m_wire;
490 };
491 
493 
494 std::ostream&
495 operator<<(std::ostream& os, const Interest& interest);
496 
497 } // namespace ndn
498 
499 #endif // NDN_CXX_INTEREST_HPP
bool isParametersDigestValid() const
Check if the ParametersSha256DigestComponent in the name is valid.
Definition: interest.cpp:673
bool getMustBeFresh() const noexcept
Check whether the MustBeFresh element is present.
Definition: interest.hpp:205
const Block & wireEncode() const
Encode into a Block.
Definition: interest.cpp:134
Copyright (c) 2011-2015 Regents of the University of California.
static bool getAutoCheckParametersDigest()
Definition: interest.hpp:420
Represents a SignatureInfo or InterestSignatureInfo TLV element.
Interest & setMustBeFresh(bool mustBeFresh)
Add or remove MustBeFresh element.
Definition: interest.hpp:214
span< const Name > getForwardingHint() const noexcept
Definition: interest.hpp:222
std::string toUri() const
Return a URI-like string that represents the Interest.
Definition: interest.cpp:315
void refreshNonce()
Change nonce value.
Definition: interest.cpp:422
bool getCanBePrefix() const noexcept
Check whether the CanBePrefix element is present.
Definition: interest.hpp:186
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:376
bool hasApplicationParameters() const noexcept
Return whether this Interest has any ApplicationParameters.
Definition: interest.hpp:289
bool matchesInterest(const Interest &other) const
Check if this Interest matches other.
Definition: interest.cpp:357
SignatureInfo info
InputBuffers extractSignedRanges() const
Extract ranges of Interest covered by the signature in Packet Specification v0.3. ...
Definition: interest.cpp:638
Represents a TLV element of the NDN packet format.
Definition: block.hpp:44
Represents an Interest packet.
Definition: interest.hpp:48
Nonce(uint32_t n) noexcept
Definition: interest.hpp:65
Interest & setNonce(optional< Nonce > nonce)
Set the Interest&#39;s nonce.
Definition: interest.cpp:412
optional< SignatureInfo > getSignatureInfo() const
Get the InterestSignatureInfo.
Definition: interest.cpp:544
Interest & setSignatureValue(ConstBufferPtr value)
Set the InterestSignatureValue.
Definition: interest.cpp:598
#define NDN_CXX_NODISCARD
Definition: backports.hpp:68
base class to allow simple management of packet tags
Definition: packet-base.hpp:31
Interest & setForwardingHint(std::vector< Name > value)
Definition: interest.cpp:385
Nonce getNonce() const
Get nonce value.
Definition: interest.cpp:402
bool matchesData(const Data &data) const
Check if Interest can be satisfied by data.
Definition: interest.cpp:325
bool hasNonce() const noexcept
Check if the Nonce element is present.
Definition: interest.hpp:233
optional< uint8_t > getHopLimit() const noexcept
Definition: interest.hpp:273
Represents an absolute name.
Definition: name.hpp:41
static void setAutoCheckParametersDigest(bool b)
Definition: interest.hpp:426
void wireDecode(const Block &wire)
Decode from wire.
Definition: interest.cpp:150
Interest & setHopLimit(optional< uint8_t > hopLimit)
Set the Interest&#39;s hop limit.
Definition: interest.cpp:449
Block getSignatureValue() const
Get the InterestSignatureValue.
Definition: interest.cpp:588
Interest & setSignatureInfo(const SignatureInfo &info)
Set the InterestSignatureInfo.
Definition: interest.cpp:554
bool hasWire() const noexcept
Check if this instance has cached wire encoding.
Definition: interest.hpp:139
const Name & getName() const noexcept
Definition: interest.hpp:172
Interest & setInterestLifetime(time::milliseconds lifetime)
Set the Interest&#39;s lifetime.
Definition: interest.cpp:435
const time::milliseconds DEFAULT_INTEREST_LIFETIME
default value for InterestLifetime
Definition: interest.hpp:43
time::milliseconds getInterestLifetime() const noexcept
Definition: interest.hpp:261
void printHex(std::ostream &os, uint64_t num, bool wantUpperCase)
Output the hex representation of num to the output stream os.
friend bool operator!=(const Nonce &lhs, const Nonce &rhs) noexcept
Definition: interest.hpp:90
Interest(const Name &name={}, time::milliseconds lifetime=DEFAULT_INTEREST_LIFETIME)
Construct an Interest with given name and lifetime.
Definition: interest.cpp:45
Interest & unsetApplicationParameters()
Remove the ApplicationParameters element from this Interest.
Definition: interest.cpp:522
Represents a Data packet.
Definition: data.hpp:37
Interest & setApplicationParameters(const Block &block)
Set ApplicationParameters from a Block.
Definition: interest.cpp:472
Error(const char *expectedType, uint32_t actualType)
Definition: tlv.cpp:27
NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(Interest)
represents an error in TLV encoding or decoding
Definition: tlv.hpp:52
friend bool operator==(const Nonce &lhs, const Nonce &rhs) noexcept
Definition: interest.hpp:84
bool isSigned() const noexcept
Return whether the Interest is signed.
Definition: interest.cpp:534
Interest & setCanBePrefix(bool canBePrefix)
Add or remove CanBePrefix element.
Definition: interest.hpp:195
Nonce(uint8_t n1, uint8_t n2, uint8_t n3, uint8_t n4) noexcept
Definition: interest.hpp:71
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:139
boost::chrono::milliseconds milliseconds
Definition: time.hpp:48
Interest & setName(const Name &name)
Set the Interest&#39;s name.
Definition: interest.cpp:367
Block getApplicationParameters() const
Get the ApplicationParameters.
Definition: interest.hpp:302