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 #include <boost/logic/tribool.hpp>
36 
37 namespace ndn {
38 
39 class Data;
40 
45 
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>
59  {
60  using Base = std::array<uint8_t, 4>;
61 
62  public:
63  Nonce() = default;
64 
65  // implicit conversion from uint32_t
66  Nonce(uint32_t n) noexcept
67  {
68  boost::endian::native_to_big_inplace(n);
69  std::memcpy(data(), &n, sizeof(n));
70  }
71 
72  Nonce(uint8_t n1, uint8_t n2, uint8_t n3, uint8_t n4) noexcept
73  {
74  data()[0] = n1;
75  data()[1] = n2;
76  data()[2] = n3;
77  data()[3] = n4;
78  }
79 
80  private: // non-member operators
81  // NOTE: the following "hidden friend" operators are available via
82  // argument-dependent lookup only and must be defined inline.
83 
84  friend bool
85  operator==(const Nonce& lhs, const Nonce& rhs) noexcept
86  {
87  return static_cast<const Base&>(lhs) == static_cast<const Base&>(rhs);
88  }
89 
90  friend bool
91  operator!=(const Nonce& lhs, const Nonce& rhs) noexcept
92  {
93  return static_cast<const Base&>(lhs) != static_cast<const Base&>(rhs);
94  }
95 
96  friend std::ostream&
97  operator<<(std::ostream& os, const Nonce& nonce)
98  {
99  printHex(os, nonce, false);
100  return os;
101  }
102  };
103 
110  explicit
111  Interest(const Name& name = Name(), time::milliseconds lifetime = DEFAULT_INTEREST_LIFETIME);
112 
118  explicit
119  Interest(const Block& wire);
120 
123  template<encoding::Tag TAG>
124  size_t
125  wireEncode(EncodingImpl<TAG>& encoder) const;
126 
129  const Block&
130  wireEncode() const;
131 
134  void
135  wireDecode(const Block& wire);
136 
139  bool
140  hasWire() const noexcept
141  {
142  return m_wire.hasWire();
143  }
144 
152  std::string
153  toUri() const;
154 
155 public: // matching
161  bool
162  matchesData(const Data& data) const;
163 
168  bool
169  matchesInterest(const Interest& other) const;
170 
171 public: // element access
172  const Name&
173  getName() const noexcept
174  {
175  return m_name;
176  }
177 
181  Interest&
182  setName(const Name& name);
183 
197  [[deprecated]]
198  static void
199  setDefaultCanBePrefix(bool canBePrefix)
200  {
201  s_defaultCanBePrefix = canBePrefix;
202  }
203 
206  bool
207  getCanBePrefix() const noexcept
208  {
209  return m_canBePrefix;
210  }
211 
215  Interest&
216  setCanBePrefix(bool canBePrefix)
217  {
218  m_canBePrefix = canBePrefix;
219  m_wire.reset();
220  return *this;
221  }
222 
225  bool
226  getMustBeFresh() const noexcept
227  {
228  return m_mustBeFresh;
229  }
230 
234  Interest&
235  setMustBeFresh(bool mustBeFresh)
236  {
237  m_mustBeFresh = mustBeFresh;
238  m_wire.reset();
239  return *this;
240  }
241 
242  span<const Name>
243  getForwardingHint() const noexcept
244  {
245  return m_forwardingHint;
246  }
247 
248  Interest&
249  setForwardingHint(std::vector<Name> value);
250 
253  bool
254  hasNonce() const noexcept
255  {
256  return m_nonce.has_value();
257  }
258 
263  Nonce
264  getNonce() const;
265 
270  Interest&
271  setNonce(optional<Nonce> nonce);
272 
278  void
279  refreshNonce();
280 
282  getInterestLifetime() const noexcept
283  {
284  return m_interestLifetime;
285  }
286 
290  Interest&
292 
293  optional<uint8_t>
294  getHopLimit() const noexcept
295  {
296  return m_hopLimit;
297  }
298 
303  Interest&
304  setHopLimit(optional<uint8_t> hopLimit);
305 
309  bool
310  hasApplicationParameters() const noexcept
311  {
312  return !m_parameters.empty();
313  }
314 
322  Block
324  {
325  if (m_parameters.empty())
326  return {};
327  else
328  return m_parameters.front();
329  }
330 
344  Interest&
345  setApplicationParameters(const Block& block);
346 
356  Interest&
357  setApplicationParameters(span<const uint8_t> value);
358 
371  [[deprecated("use the overload that takes a span<>")]]
372  Interest&
373  setApplicationParameters(const uint8_t* value, size_t length);
374 
384  Interest&
386 
395  Interest&
397 
402  bool
403  isSigned() const noexcept;
404 
408  optional<SignatureInfo>
409  getSignatureInfo() const;
410 
413  Interest&
415 
420  Block
421  getSignatureValue() const;
422 
429  Interest&
431 
436  InputBuffers
437  extractSignedRanges() const;
438 
439 public: // ParametersSha256DigestComponent support
440  static bool
442  {
443  return s_autoCheckParametersDigest;
444  }
445 
446  static void
448  {
449  s_autoCheckParametersDigest = b;
450  }
451 
459  bool
460  isParametersDigestValid() const;
461 
462 private:
463  void
464  setApplicationParametersInternal(Block parameters);
465 
466  NDN_CXX_NODISCARD shared_ptr<Buffer>
467  computeParametersDigest() const;
468 
476  void
477  addOrReplaceParametersDigestComponent();
478 
485  static ssize_t
486  findParametersDigestComponent(const Name& name);
487 
488  std::vector<Block>::const_iterator
489  findFirstParameter(uint32_t type) const;
490 
491 private:
492  static boost::logic::tribool s_defaultCanBePrefix;
493  static bool s_autoCheckParametersDigest;
494 
495  Name m_name;
496  std::vector<Name> m_forwardingHint;
497  mutable optional<Nonce> m_nonce;
498  time::milliseconds m_interestLifetime;
499  optional<uint8_t> m_hopLimit;
500  bool m_canBePrefix = false;
501  bool m_mustBeFresh = false;
502 
503  // Stores the "Interest parameters", i.e., all maybe-unrecognized non-critical TLV
504  // elements that appear at the end of the Interest, starting from ApplicationParameters.
505  // If the Interest does not contain any ApplicationParameters TLV, this vector will
506  // be empty. Conversely, if this vector is not empty, the first element will always
507  // be an ApplicationParameters block. All blocks in this vector are covered by the
508  // digest in the ParametersSha256DigestComponent.
509  std::vector<Block> m_parameters;
510 
511  mutable Block m_wire;
512 };
513 
515 
516 std::ostream&
517 operator<<(std::ostream& os, const Interest& interest);
518 
519 } // namespace ndn
520 
521 #endif // NDN_CXX_INTEREST_HPP
#define NDN_CXX_NODISCARD
Definition: backports.hpp:68
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:241
void reset() noexcept
Reset the Block to a default-constructed state.
Definition: block.cpp:265
Represents a Data packet.
Definition: data.hpp:38
Nonce(uint32_t n) noexcept
Definition: interest.hpp:66
friend bool operator!=(const Nonce &lhs, const Nonce &rhs) noexcept
Definition: interest.hpp:91
Nonce(uint8_t n1, uint8_t n2, uint8_t n3, uint8_t n4) noexcept
Definition: interest.hpp:72
friend std::ostream & operator<<(std::ostream &os, const Nonce &nonce)
Definition: interest.hpp:97
friend bool operator==(const Nonce &lhs, const Nonce &rhs) noexcept
Definition: interest.hpp:85
Represents an Interest packet.
Definition: interest.hpp:50
static void setDefaultCanBePrefix(bool canBePrefix)
Declare the default CanBePrefix setting of the application.
Definition: interest.hpp:199
Interest & setMustBeFresh(bool mustBeFresh)
Add or remove MustBeFresh element.
Definition: interest.hpp:235
static void setAutoCheckParametersDigest(bool b)
Definition: interest.hpp:447
void wireDecode(const Block &wire)
Decode from wire.
Definition: interest.cpp:155
Interest(const Name &name=Name(), time::milliseconds lifetime=DEFAULT_INTEREST_LIFETIME)
Construct an Interest with given name and lifetime.
Definition: interest.cpp:46
const Name & getName() const noexcept
Definition: interest.hpp:173
Block getApplicationParameters() const
Get the ApplicationParameters.
Definition: interest.hpp:323
Interest & setHopLimit(optional< uint8_t > hopLimit)
Set the Interest's hop limit.
Definition: interest.cpp:454
Nonce getNonce() const
Get nonce value.
Definition: interest.cpp:407
span< const Name > getForwardingHint() const noexcept
Definition: interest.hpp:243
bool matchesInterest(const Interest &other) const
Check if this Interest matches other.
Definition: interest.cpp:362
Interest & setSignatureValue(ConstBufferPtr value)
Set the InterestSignatureValue.
Definition: interest.cpp:603
bool hasApplicationParameters() const noexcept
Return whether this Interest has any ApplicationParameters.
Definition: interest.hpp:310
time::milliseconds getInterestLifetime() const noexcept
Definition: interest.hpp:282
bool matchesData(const Data &data) const
Check if Interest can be satisfied by data.
Definition: interest.cpp:330
bool getMustBeFresh() const noexcept
Check whether the MustBeFresh element is present.
Definition: interest.hpp:226
Block getSignatureValue() const
Get the InterestSignatureValue.
Definition: interest.cpp:593
InputBuffers extractSignedRanges() const
Extract ranges of Interest covered by the signature in Packet Specification v0.3.
Definition: interest.cpp:643
bool hasNonce() const noexcept
Check if the Nonce element is present.
Definition: interest.hpp:254
bool getCanBePrefix() const noexcept
Check whether the CanBePrefix element is present.
Definition: interest.hpp:207
void refreshNonce()
Change nonce value.
Definition: interest.cpp:427
optional< uint8_t > getHopLimit() const noexcept
Definition: interest.hpp:294
Interest & setCanBePrefix(bool canBePrefix)
Add or remove CanBePrefix element.
Definition: interest.hpp:216
bool isSigned() const noexcept
Return whether the Interest is signed.
Definition: interest.cpp:539
optional< SignatureInfo > getSignatureInfo() const
Get the InterestSignatureInfo.
Definition: interest.cpp:549
Interest & unsetApplicationParameters()
Remove the ApplicationParameters element from this Interest.
Definition: interest.cpp:527
bool isParametersDigestValid() const
Check if the ParametersSha256DigestComponent in the name is valid.
Definition: interest.cpp:678
Interest & setName(const Name &name)
Set the Interest's name.
Definition: interest.cpp:372
Interest & setSignatureInfo(const SignatureInfo &info)
Set the InterestSignatureInfo.
Definition: interest.cpp:559
Interest & setApplicationParameters(const Block &block)
Set ApplicationParameters from a Block.
Definition: interest.cpp:477
Interest & setForwardingHint(std::vector< Name > value)
Definition: interest.cpp:390
std::string toUri() const
Return a URI-like string that represents the Interest.
Definition: interest.cpp:320
static bool getAutoCheckParametersDigest()
Definition: interest.hpp:441
Interest & setNonce(optional< Nonce > nonce)
Set the Interest's nonce.
Definition: interest.cpp:417
const Block & wireEncode() const
Encode into a Block.
Definition: interest.cpp:139
bool hasWire() const noexcept
Check if this instance has cached wire encoding.
Definition: interest.hpp:140
Interest & setInterestLifetime(time::milliseconds lifetime)
Set the Interest's lifetime.
Definition: interest.cpp:440
Represents an absolute name.
Definition: name.hpp:46
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:53
Error(const char *expectedType, uint32_t actualType)
Definition: tlv.cpp:27
#define NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
boost::chrono::milliseconds milliseconds
Definition: time.hpp:48
@ Name
Definition: tlv.hpp:67
@ Data
Definition: tlv.hpp:66
@ Interest
Definition: tlv.hpp:65
@ Nonce
Definition: tlv.hpp:74
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.
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:139
const time::milliseconds DEFAULT_INTEREST_LIFETIME
default value for InterestLifetime
Definition: interest.hpp:44
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:376
SignatureInfo info