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-2024 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 
49 class Interest : public PacketBase, public std::enable_shared_from_this<Interest>
50 {
51 public:
52  class Error : public tlv::Error
53  {
54  public:
55  using tlv::Error::Error;
56  };
57 
58  class Nonce final : public std::array<uint8_t, 4>, private boost::equality_comparable<Nonce>
59  {
60  public:
61  Nonce() = default;
62 
63  // implicit conversion from uint32_t
64  Nonce(uint32_t n) noexcept
65  {
66  boost::endian::native_to_big_inplace(n);
67  std::memcpy(data(), &n, sizeof(n));
68  }
69 
70  Nonce(uint8_t n1, uint8_t n2, uint8_t n3, uint8_t n4) noexcept
71  {
72  (*this)[0] = n1;
73  (*this)[1] = n2;
74  (*this)[2] = n3;
75  (*this)[3] = n4;
76  }
77 
78  private: // non-member operators
79  // NOTE: the following "hidden friend" operators are available via
80  // argument-dependent lookup only and must be defined inline.
81  // boost::equality_comparable provides != operator.
82 
83  friend bool
84  operator==(const Nonce& lhs, const Nonce& rhs) noexcept
85  {
86  return std::equal(lhs.begin(), lhs.end(), rhs.begin());
87  }
88 
89  friend std::ostream&
90  operator<<(std::ostream& os, const Nonce& nonce)
91  {
92  printHex(os, nonce, false);
93  return os;
94  }
95  };
96 
104  explicit
105  Interest(const Name& name = {}, time::milliseconds lifetime = DEFAULT_INTEREST_LIFETIME);
106 
113  explicit
114  Interest(const Block& wire);
115 
119  template<encoding::Tag TAG>
120  size_t
121  wireEncode(EncodingImpl<TAG>& encoder) const;
122 
126  const Block&
127  wireEncode() const;
128 
132  void
133  wireDecode(const Block& wire);
134 
138  bool
139  hasWire() const noexcept
140  {
141  return m_wire.hasWire();
142  }
143 
153  std::string
154  toUri() const;
155 
156 public: // matching
163  [[nodiscard]] bool
164  matchesData(const Data& data) const;
165 
171  [[nodiscard]] bool
172  matchesInterest(const Interest& other) const;
173 
174 public: // Interest fields
178  const Name&
179  getName() const noexcept
180  {
181  return m_name;
182  }
183 
188  Interest&
189  setName(const Name& name);
190 
194  bool
195  getCanBePrefix() const noexcept
196  {
197  return m_canBePrefix;
198  }
199 
204  Interest&
205  setCanBePrefix(bool canBePrefix);
206 
210  bool
211  getMustBeFresh() const noexcept
212  {
213  return m_mustBeFresh;
214  }
215 
220  Interest&
221  setMustBeFresh(bool mustBeFresh);
222 
226  span<const Name>
227  getForwardingHint() const noexcept
228  {
229  return m_forwardingHint;
230  }
231 
237  Interest&
238  setForwardingHint(std::vector<Name> value);
239 
243  bool
244  hasNonce() const noexcept
245  {
246  return m_nonce.has_value();
247  }
248 
254  Nonce
255  getNonce() const;
256 
262  Interest&
263  setNonce(std::optional<Nonce> nonce);
264 
271  void
272  refreshNonce();
273 
282  getInterestLifetime() const noexcept;
283 
288  Interest&
289  setInterestLifetime(time::milliseconds lifetime);
290 
294  std::optional<uint8_t>
295  getHopLimit() const noexcept
296  {
297  return m_hopLimit;
298  }
299 
305  Interest&
306  setHopLimit(std::optional<uint8_t> hopLimit);
307 
311  bool
312  hasApplicationParameters() const noexcept
313  {
314  return !m_parameters.empty();
315  }
316 
324  Block
326  {
327  if (m_parameters.empty())
328  return {};
329  else
330  return m_parameters.front();
331  }
332 
346  Interest&
347  setApplicationParameters(const Block& block);
348 
358  Interest&
359  setApplicationParameters(span<const uint8_t> value);
360 
370  Interest&
371  setApplicationParameters(std::string_view value);
372 
382  Interest&
384 
385  Interest&
386  setApplicationParameters(std::nullptr_t) = delete;
387 
396  Interest&
398 
404  bool
405  isSigned() const noexcept;
406 
410  std::optional<SignatureInfo>
411  getSignatureInfo() const;
412 
416  Interest&
418 
424  Block
425  getSignatureValue() const;
426 
435  Interest&
436  setSignatureValue(span<const uint8_t> value);
437 
446  Interest&
448 
449  Interest&
450  setSignatureValue(std::nullptr_t) = delete;
451 
457  [[nodiscard]] InputBuffers
458  extractSignedRanges() const;
459 
460 public: // ParametersSha256DigestComponent support
461  static bool
463  {
464  return s_autoCheckParametersDigest;
465  }
466 
467  static void
469  {
470  s_autoCheckParametersDigest = b;
471  }
472 
481  bool
482  isParametersDigestValid() const;
483 
484 private:
485  Interest&
486  setApplicationParametersInternal(Block parameters);
487 
488  Interest&
489  setSignatureValueInternal(Block sigValue);
490 
491  [[nodiscard]] shared_ptr<Buffer>
492  computeParametersDigest() const;
493 
501  void
502  addOrReplaceParametersDigestComponent();
503 
510  static ssize_t
511  findParametersDigestComponent(const Name& name);
512 
513  std::vector<Block>::const_iterator
514  findFirstParameter(uint32_t type) const;
515 
516 private:
517  Name m_name;
518  std::vector<Name> m_forwardingHint;
519  mutable std::optional<Nonce> m_nonce;
520  uint64_t m_interestLifetime = DEFAULT_INTEREST_LIFETIME.count();
521  std::optional<uint8_t> m_hopLimit;
522  bool m_canBePrefix = false;
523  bool m_mustBeFresh = false;
524 
525  // Stores the "Interest parameters", i.e., all maybe-unrecognized non-critical TLV
526  // elements that appear at the end of the Interest, starting from ApplicationParameters.
527  // If the Interest does not contain any ApplicationParameters TLV, this vector will
528  // be empty. Conversely, if this vector is not empty, the first element will always
529  // be an ApplicationParameters block. All blocks in this vector are covered by the
530  // digest in the ParametersSha256DigestComponent.
531  std::vector<Block> m_parameters;
532 
533  mutable Block m_wire;
534 
535  static inline bool s_autoCheckParametersDigest = true;
536 };
537 
539 
540 std::ostream&
541 operator<<(std::ostream& os, const Interest& interest);
542 
543 } // namespace ndn
544 
545 #endif // NDN_CXX_INTEREST_HPP
Represents a TLV element of the NDN packet format.
Definition: block.hpp:45
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition: block.hpp:205
Represents a Data packet.
Definition: data.hpp:39
Nonce(uint32_t n) noexcept
Definition: interest.hpp:64
Nonce(uint8_t n1, uint8_t n2, uint8_t n3, uint8_t n4) noexcept
Definition: interest.hpp:70
friend std::ostream & operator<<(std::ostream &os, const Nonce &nonce)
Definition: interest.hpp:90
friend bool operator==(const Nonce &lhs, const Nonce &rhs) noexcept
Definition: interest.hpp:84
Represents an Interest packet.
Definition: interest.hpp:50
static void setAutoCheckParametersDigest(bool b)
Definition: interest.hpp:468
void wireDecode(const Block &wire)
Decode from wire.
Definition: interest.cpp:142
Interest & setHopLimit(std::optional< uint8_t > hopLimit)
Set the Interest's hop limit.
Definition: interest.cpp:466
std::optional< SignatureInfo > getSignatureInfo() const
Get the InterestSignatureInfo element.
Definition: interest.cpp:552
Interest & setCanBePrefix(bool canBePrefix)
Add or remove CanBePrefix element.
Definition: interest.cpp:372
Interest & setNonce(std::optional< Nonce > nonce)
Set the Interest's nonce.
Definition: interest.cpp:419
const Name & getName() const noexcept
Get the Interest name.
Definition: interest.hpp:179
Block getApplicationParameters() const
Get the ApplicationParameters element.
Definition: interest.hpp:325
Nonce getNonce() const
Get nonce value.
Definition: interest.cpp:409
Interest & setSignatureValue(span< const uint8_t > value)
Set InterestSignatureValue by copying from a contiguous sequence of bytes.
Definition: interest.cpp:641
span< const Name > getForwardingHint() const noexcept
Get the delegations (names) in the ForwardingHint.
Definition: interest.hpp:227
bool matchesInterest(const Interest &other) const
Check if this Interest matches other.
Definition: interest.cpp:344
bool hasApplicationParameters() const noexcept
Return whether this Interest has any ApplicationParameters element.
Definition: interest.hpp:312
time::milliseconds getInterestLifetime() const noexcept
Get the Interest's lifetime.
Definition: interest.cpp:442
bool matchesData(const Data &data) const
Check if this Interest can be satisfied by data.
Definition: interest.cpp:319
Interest & setMustBeFresh(bool mustBeFresh)
Add or remove MustBeFresh element.
Definition: interest.cpp:382
bool getMustBeFresh() const noexcept
Check whether the MustBeFresh element is present.
Definition: interest.hpp:211
Block getSignatureValue() const
Get the InterestSignatureValue element.
Definition: interest.cpp:596
InputBuffers extractSignedRanges() const
Extract ranges of Interest covered by the signature.
Definition: interest.cpp:657
bool hasNonce() const noexcept
Check if the Nonce element is present.
Definition: interest.hpp:244
std::optional< uint8_t > getHopLimit() const noexcept
Get the Interest's hop limit.
Definition: interest.hpp:295
bool getCanBePrefix() const noexcept
Check whether the CanBePrefix element is present.
Definition: interest.hpp:195
void refreshNonce()
Change nonce value.
Definition: interest.cpp:429
bool isSigned() const noexcept
Return whether the Interest is signed.
Definition: interest.cpp:542
Interest & unsetApplicationParameters()
Remove the ApplicationParameters element from this Interest.
Definition: interest.cpp:530
bool isParametersDigestValid() const
Check if the ParametersSha256DigestComponent in the name is valid.
Definition: interest.cpp:692
Interest & setName(const Name &name)
Set the Interest name.
Definition: interest.cpp:354
Interest & setApplicationParameters(std::nullptr_t)=delete
Interest & setSignatureInfo(const SignatureInfo &info)
Set the InterestSignatureInfo element.
Definition: interest.cpp:562
Interest & setApplicationParameters(const Block &block)
Set ApplicationParameters from a Block.
Definition: interest.cpp:493
Interest & setForwardingHint(std::vector< Name > value)
Set the ForwardingHint delegations (names).
Definition: interest.cpp:392
std::string toUri() const
Return a URI-like string that represents the Interest.
Definition: interest.cpp:309
static bool getAutoCheckParametersDigest()
Definition: interest.hpp:462
const Block & wireEncode() const
Encode into a Block.
Definition: interest.cpp:126
Interest(const Name &name={}, time::milliseconds lifetime=DEFAULT_INTEREST_LIFETIME)
Construct an Interest with given name and lifetime.
Definition: interest.cpp:38
bool hasWire() const noexcept
Check if this instance has cached wire encoding.
Definition: interest.hpp:139
Interest & setInterestLifetime(time::milliseconds lifetime)
Set the Interest's lifetime.
Definition: interest.cpp:451
Represents an absolute name.
Definition: name.hpp:45
Base class to allow simple management of packet tags.
Definition: packet-base.hpp:32
Represents a SignatureInfo or InterestSignatureInfo TLV element.
Represents an error in TLV encoding or decoding.
Definition: tlv.hpp:54
Error(const char *expectedType, uint32_t actualType)
Definition: tlv.cpp:28
#define NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
::boost::chrono::milliseconds milliseconds
Definition: time.hpp:52
@ Data
Definition: tlv.hpp:69
@ Interest
Definition: tlv.hpp:68
@ Nonce
Definition: tlv.hpp:85
Definition: data.cpp:25
void printHex(std::ostream &os, uint64_t num, bool wantUpperCase)
Output the hex representation of num to the output stream os.
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:377
constexpr time::milliseconds DEFAULT_INTEREST_LIFETIME
Default value of InterestLifetime.
Definition: interest.hpp:43
std::shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:140
SignatureInfo info