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-2020 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 
27 #include "ndn-cxx/name.hpp"
31 #include "ndn-cxx/util/time.hpp"
32 
33 #include <array>
34 
35 #include <boost/endian/conversion.hpp>
36 #include <boost/logic/tribool.hpp>
37 
38 namespace ndn {
39 
40 class Data;
41 
45 const time::milliseconds DEFAULT_INTEREST_LIFETIME = 4_s;
46 
50 class Interest : public PacketBase, public std::enable_shared_from_this<Interest>
51 {
52 public:
53  class Error : public tlv::Error
54  {
55  public:
56  using tlv::Error::Error;
57  };
58 
59  class Nonce final : public std::array<uint8_t, 4>
60  {
61  using Base = std::array<uint8_t, 4>;
62 
63  public:
64  Nonce() = default;
65 
66  // implicit conversion from uint32_t
67  Nonce(uint32_t n) noexcept
68  {
69  boost::endian::native_to_big_inplace(n);
70  std::memcpy(data(), &n, sizeof(n));
71  }
72 
73  Nonce(uint8_t n1, uint8_t n2, uint8_t n3, uint8_t n4) noexcept
74  {
75  data()[0] = n1;
76  data()[1] = n2;
77  data()[2] = n3;
78  data()[3] = n4;
79  }
80 
81  private: // non-member operators
82  // NOTE: the following "hidden friend" operators are available via
83  // argument-dependent lookup only and must be defined inline.
84 
85  friend bool
86  operator==(const Nonce& lhs, const Nonce& rhs) noexcept
87  {
88  return static_cast<const Base&>(lhs) == static_cast<const Base&>(rhs);
89  }
90 
91  friend bool
92  operator!=(const Nonce& lhs, const Nonce& rhs) noexcept
93  {
94  return static_cast<const Base&>(lhs) != static_cast<const Base&>(rhs);
95  }
96 
97  friend std::ostream&
98  operator<<(std::ostream& os, const Nonce& nonce)
99  {
100  printHex(os, nonce.data(), nonce.size(), false);
101  return os;
102  }
103  };
104 
111  explicit
112  Interest(const Name& name = Name(), time::milliseconds lifetime = DEFAULT_INTEREST_LIFETIME);
113 
119  explicit
120  Interest(const Block& wire);
121 
124  template<encoding::Tag TAG>
125  size_t
126  wireEncode(EncodingImpl<TAG>& encoder) const;
127 
130  const Block&
131  wireEncode() const;
132 
135  void
136  wireDecode(const Block& wire);
137 
140  bool
141  hasWire() const noexcept
142  {
143  return m_wire.hasWire();
144  }
145 
153  std::string
154  toUri() const;
155 
156 public: // matching
162  bool
163  matchesData(const Data& data) const;
164 
169  bool
170  matchesInterest(const Interest& other) const;
171 
172 public: // element access
173  const Name&
174  getName() const noexcept
175  {
176  return m_name;
177  }
178 
182  Interest&
183  setName(const Name& name);
184 
199  static void
200  setDefaultCanBePrefix(bool canBePrefix)
201  {
202  s_defaultCanBePrefix = canBePrefix;
203  }
204 
207  bool
208  getCanBePrefix() const noexcept
209  {
210  return m_canBePrefix;
211  }
212 
216  Interest&
217  setCanBePrefix(bool canBePrefix)
218  {
219  m_canBePrefix = canBePrefix;
220  m_wire.reset();
221  m_isCanBePrefixSet = true;
222  return *this;
223  }
224 
227  bool
228  getMustBeFresh() const noexcept
229  {
230  return m_mustBeFresh;
231  }
232 
236  Interest&
237  setMustBeFresh(bool mustBeFresh)
238  {
239  m_mustBeFresh = mustBeFresh;
240  m_wire.reset();
241  return *this;
242  }
243 
244  const DelegationList&
245  getForwardingHint() const noexcept
246  {
247  return m_forwardingHint;
248  }
249 
250  Interest&
251  setForwardingHint(const DelegationList& value);
252 
263  template<typename Modifier>
264  Interest&
265  modifyForwardingHint(const Modifier& modifier)
266  {
267  modifier(m_forwardingHint);
268  m_wire.reset();
269  return *this;
270  }
271 
274  bool
275  hasNonce() const noexcept
276  {
277  return m_nonce.has_value();
278  }
279 
284  Nonce
285  getNonce() const;
286 
291  Interest&
292  setNonce(optional<Nonce> nonce);
293 
299  void
300  refreshNonce();
301 
302  time::milliseconds
303  getInterestLifetime() const noexcept
304  {
305  return m_interestLifetime;
306  }
307 
311  Interest&
312  setInterestLifetime(time::milliseconds lifetime);
313 
314  optional<uint8_t>
315  getHopLimit() const noexcept
316  {
317  return m_hopLimit;
318  }
319 
324  Interest&
325  setHopLimit(optional<uint8_t> hopLimit);
326 
330  bool
331  hasApplicationParameters() const noexcept
332  {
333  return !m_parameters.empty();
334  }
335 
343  Block
345  {
346  if (m_parameters.empty())
347  return {};
348  else
349  return m_parameters.front();
350  }
351 
365  Interest&
366  setApplicationParameters(const Block& block);
367 
379  Interest&
380  setApplicationParameters(const uint8_t* value, size_t length);
381 
391  Interest&
393 
402  Interest&
404 
409  bool
410  isSigned() const noexcept;
411 
415  optional<SignatureInfo>
416  getSignatureInfo() const;
417 
420  Interest&
421  setSignatureInfo(const SignatureInfo& info);
422 
427  Block
428  getSignatureValue() const;
429 
436  Interest&
438 
443  InputBuffers
444  extractSignedRanges() const;
445 
446 public: // ParametersSha256DigestComponent support
447  static bool
449  {
450  return s_autoCheckParametersDigest;
451  }
452 
453  static void
455  {
456  s_autoCheckParametersDigest = b;
457  }
458 
466  bool
467  isParametersDigestValid() const;
468 
469 private:
470  void
471  setApplicationParametersInternal(Block parameters);
472 
473  NDN_CXX_NODISCARD shared_ptr<Buffer>
474  computeParametersDigest() const;
475 
483  void
484  addOrReplaceParametersDigestComponent();
485 
492  static ssize_t
493  findParametersDigestComponent(const Name& name);
494 
495  std::vector<Block>::const_iterator
496  findFirstParameter(uint32_t type) const;
497 
498 #ifdef NDN_CXX_HAVE_TESTS
499 public:
501  static bool s_errorIfCanBePrefixUnset;
502 #endif // NDN_CXX_HAVE_TESTS
503 
504 private:
505  static boost::logic::tribool s_defaultCanBePrefix;
506  static bool s_autoCheckParametersDigest;
507 
508  Name m_name;
509  DelegationList m_forwardingHint;
510  mutable optional<Nonce> m_nonce;
511  time::milliseconds m_interestLifetime;
512  optional<uint8_t> m_hopLimit;
513  mutable bool m_isCanBePrefixSet = false;
514  bool m_canBePrefix = true;
515  bool m_mustBeFresh = false;
516 
517  // Stores the "Interest parameters", i.e., all maybe-unrecognized non-critical TLV
518  // elements that appear at the end of the Interest, starting from ApplicationParameters.
519  // If the Interest does not contain any ApplicationParameters TLV, this vector will
520  // be empty. Conversely, if this vector is not empty, the first element will always
521  // be an ApplicationParameters block. All blocks in this vector are covered by the
522  // digest in the ParametersSha256DigestComponent.
523  std::vector<Block> m_parameters;
524 
525  mutable Block m_wire;
526 };
527 
529 
530 std::ostream&
531 operator<<(std::ostream& os, const Interest& interest);
532 
533 } // namespace ndn
534 
535 #endif // NDN_CXX_INTEREST_HPP
bool isParametersDigestValid() const
Check if the ParametersSha256DigestComponent in the name is valid.
Definition: interest.cpp:674
bool getMustBeFresh() const noexcept
Check whether the MustBeFresh element is present.
Definition: interest.hpp:228
const Block & wireEncode() const
Encode into a Block.
Definition: interest.cpp:171
Definition: data.cpp:26
static bool getAutoCheckParametersDigest()
Definition: interest.hpp:448
Represents a SignatureInfo or InterestSignatureInfo TLV element.
Interest & modifyForwardingHint(const Modifier &modifier)
Modify ForwardingHint in-place.
Definition: interest.hpp:265
Interest & setMustBeFresh(bool mustBeFresh)
Add or remove MustBeFresh element.
Definition: interest.hpp:237
std::string toUri() const
Return a URI-like string that represents the Interest.
Definition: interest.cpp:323
void refreshNonce()
Change nonce value.
Definition: interest.cpp:430
bool getCanBePrefix() const noexcept
Check whether the CanBePrefix element is present.
Definition: interest.hpp:208
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:383
Interest(const Name &name=Name(), time::milliseconds lifetime=DEFAULT_INTEREST_LIFETIME)
Construct an Interest with given name and lifetime.
Definition: interest.cpp:54
bool hasApplicationParameters() const noexcept
Return whether this Interest has any ApplicationParameters.
Definition: interest.hpp:331
bool matchesInterest(const Interest &other) const
Check if this Interest matches other.
Definition: interest.cpp:365
InputBuffers extractSignedRanges() const
Extract ranges of Interest covered by the signature in Packet Specification v0.3. ...
Definition: interest.cpp:640
Represents a TLV element of the NDN packet format.
Definition: block.hpp:42
Represents an Interest packet.
Definition: interest.hpp:50
Nonce(uint32_t n) noexcept
Definition: interest.hpp:67
#define NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
Interest & setNonce(optional< Nonce > nonce)
Set the Interest&#39;s nonce.
Definition: interest.cpp:420
optional< SignatureInfo > getSignatureInfo() const
Get the InterestSignatureInfo.
Definition: interest.cpp:546
Interest & setSignatureValue(ConstBufferPtr value)
Set the InterestSignatureValue.
Definition: interest.cpp:600
const DelegationList & getForwardingHint() const noexcept
Definition: interest.hpp:245
#define NDN_CXX_NODISCARD
Definition: backports.hpp:68
base class to allow simple management of packet tags
Definition: packet-base.hpp:31
Nonce getNonce() const
Get nonce value.
Definition: interest.cpp:410
bool matchesData(const Data &data) const
Check if Interest can be satisfied by data.
Definition: interest.cpp:333
bool hasNonce() const noexcept
Check if the Nonce element is present.
Definition: interest.hpp:275
optional< uint8_t > getHopLimit() const noexcept
Definition: interest.hpp:315
static void setDefaultCanBePrefix(bool canBePrefix)
Declare the default CanBePrefix setting of the application.
Definition: interest.hpp:200
Represents an absolute name.
Definition: name.hpp:44
static void setAutoCheckParametersDigest(bool b)
Definition: interest.hpp:454
void wireDecode(const Block &wire)
Decode from wire.
Definition: interest.cpp:187
Interest & setHopLimit(optional< uint8_t > hopLimit)
Set the Interest&#39;s hop limit.
Definition: interest.cpp:457
Block getSignatureValue() const
Get the InterestSignatureValue.
Definition: interest.cpp:590
Interest & setSignatureInfo(const SignatureInfo &info)
Set the InterestSignatureInfo.
Definition: interest.cpp:556
bool hasWire() const noexcept
Check if this instance has cached wire encoding.
Definition: interest.hpp:141
const Name & getName() const noexcept
Definition: interest.hpp:174
represents a list of Delegations
Interest & setForwardingHint(const DelegationList &value)
Definition: interest.cpp:393
Interest & setInterestLifetime(time::milliseconds lifetime)
Set the Interest&#39;s lifetime.
Definition: interest.cpp:443
const time::milliseconds DEFAULT_INTEREST_LIFETIME
default value for InterestLifetime
Definition: interest.hpp:45
time::milliseconds getInterestLifetime() const noexcept
Definition: interest.hpp:303
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:92
Interest & unsetApplicationParameters()
Remove the ApplicationParameters element from this Interest.
Definition: interest.cpp:524
Represents a Data packet.
Definition: data.hpp:39
Interest & setApplicationParameters(const Block &block)
Set ApplicationParameters from a Block.
Definition: interest.cpp:480
Error(const char *expectedType, uint32_t actualType)
Definition: tlv.cpp:27
represents an error in TLV encoding or decoding
Definition: tlv.hpp:51
friend bool operator==(const Nonce &lhs, const Nonce &rhs) noexcept
Definition: interest.hpp:86
bool isSigned() const noexcept
Return whether the Interest is signed.
Definition: interest.cpp:536
Interest & setCanBePrefix(bool canBePrefix)
Add or remove CanBePrefix element.
Definition: interest.hpp:217
Nonce(uint8_t n1, uint8_t n2, uint8_t n3, uint8_t n4) noexcept
Definition: interest.hpp:73
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:126
Interest & setName(const Name &name)
Set the Interest&#39;s name.
Definition: interest.cpp:375
Block getApplicationParameters() const
Get the ApplicationParameters.
Definition: interest.hpp:344