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
bool isParametersDigestValid() const
Check if the ParametersSha256DigestComponent in the name is valid.
Definition: interest.cpp:524
bool getMustBeFresh() const noexcept
Check whether the MustBeFresh element is present.
Definition: interest.hpp:175
const Block & wireEncode() const
Encode into a Block according to NDN Packet Format v0.3.
Definition: interest.cpp:170
Definition: data.cpp:26
static bool getAutoCheckParametersDigest()
Definition: interest.hpp:336
Interest & modifyForwardingHint(const Modifier &modifier)
Modify ForwardingHint in-place.
Definition: interest.hpp:212
Interest & setMustBeFresh(bool mustBeFresh)
Add or remove MustBeFresh element.
Definition: interest.hpp:184
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:420
bool getCanBePrefix() const noexcept
Check whether the CanBePrefix element is present.
Definition: interest.hpp:155
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:322
Interest(const Name &name=Name(), time::milliseconds lifetime=DEFAULT_INTEREST_LIFETIME)
Construct an Interest with given name and lifetime.
Definition: interest.cpp:52
bool hasApplicationParameters() const noexcept
Definition: interest.hpp:273
bool matchesInterest(const Interest &other) const
Check if this Interest matches other.
Definition: interest.cpp:365
Interest & setApplicationParameters(const Block &parameters)
Set ApplicationParameters from a Block.
Definition: interest.cpp:469
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
Represents an Interest packet.
Definition: interest.hpp:43
#define NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
const DelegationList & getForwardingHint() const noexcept
Definition: interest.hpp:192
uint32_t getNonce() const
Get nonce value.
Definition: interest.cpp:400
#define NDN_CXX_NODISCARD
Definition: backports.hpp:68
Interest & setNonce(uint32_t nonce)
Set nonce value.
Definition: interest.cpp:410
base class to allow simple management of packet tags
Definition: packet-base.hpp:31
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:222
optional< uint8_t > getHopLimit() const noexcept
Definition: interest.hpp:260
static void setDefaultCanBePrefix(bool canBePrefix)
Declare the default CanBePrefix setting of the application.
Definition: interest.hpp:147
Represents an absolute name.
Definition: name.hpp:43
static void setAutoCheckParametersDigest(bool b)
Definition: interest.hpp:342
bool hasWire() const
Check if this instance has cached wire encoding.
Definition: interest.hpp:88
void wireDecode(const Block &wire)
Decode from wire according to NDN Packet Format v0.3.
Definition: interest.cpp:186
Interest & setHopLimit(optional< uint8_t > hopLimit)
Set the Interest&#39;s hop limit.
Definition: interest.cpp:446
const Name & getName() const noexcept
Definition: interest.hpp:121
represents a list of Delegations
Interest & setForwardingHint(const DelegationList &value)
Definition: interest.cpp:392
Interest & setInterestLifetime(time::milliseconds lifetime)
Set the Interest&#39;s lifetime.
Definition: interest.cpp:433
const time::milliseconds DEFAULT_INTEREST_LIFETIME
default value for InterestLifetime
Definition: interest.hpp:39
time::milliseconds getInterestLifetime() const noexcept
Definition: interest.hpp:248
Interest & unsetApplicationParameters()
Remove the ApplicationParameters element from this Interest.
Definition: interest.cpp:510
Represents a Data packet.
Definition: data.hpp:35
Error(const char *expectedType, uint32_t actualType)
Definition: tlv.cpp:27
represents an error in TLV encoding or decoding
Definition: tlv.hpp:52
Interest & setCanBePrefix(bool canBePrefix)
Add or remove CanBePrefix element.
Definition: interest.hpp:164
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
Definition: interest.hpp:279