blob: 432f68c69f825b338901b0ac063a4b1c0562ad4d [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi899277a2017-07-07 22:12:12 +00002/*
Davide Pesaventofccb2dc2019-02-09 01:02:35 -05003 * Copyright (c) 2013-2019 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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.
Jeff Thompsonb7f95562013-07-03 18:36:42 -070020 */
21
22#ifndef NDN_INTEREST_HPP
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070023#define NDN_INTEREST_HPP
Jeff Thompsonb7f95562013-07-03 18:36:42 -070024
Davide Pesavento7e780642018-11-24 15:51:34 -050025#include "ndn-cxx/delegation-list.hpp"
26#include "ndn-cxx/name.hpp"
Davide Pesavento7e780642018-11-24 15:51:34 -050027#include "ndn-cxx/selectors.hpp"
Junxiao Shi1e36ceb2018-12-17 13:20:26 -070028#include "ndn-cxx/detail/packet-base.hpp"
Davide Pesavento7e780642018-11-24 15:51:34 -050029#include "ndn-cxx/util/time.hpp"
30
Junxiao Shib55e5d32018-07-18 13:32:00 -060031#include <boost/logic/tribool.hpp>
Jeff Thompsonb7f95562013-07-03 18:36:42 -070032
33namespace ndn {
Alexander Afanasyevc348f832014-02-17 16:35:17 -080034
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070035class Data;
36
Junxiao Shi7007a3c2014-11-20 22:37:55 -070037/** @var const unspecified_duration_type DEFAULT_INTEREST_LIFETIME;
38 * @brief default value for InterestLifetime
39 */
Davide Pesavento0f830802018-01-16 23:58:58 -050040const time::milliseconds DEFAULT_INTEREST_LIFETIME = 4_s;
Alexander Afanasyevc348f832014-02-17 16:35:17 -080041
Junxiao Shi8d3f8342018-04-04 12:46:37 +000042/** @brief Represents an Interest packet.
Jeff Thompson8238d002013-07-10 11:56:49 -070043 */
Davide Pesavento1fd00242018-05-20 00:11:01 -040044class Interest : public PacketBase, public std::enable_shared_from_this<Interest>
Alexander Afanasyevc348f832014-02-17 16:35:17 -080045{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070046public:
Junxiao Shic2b8d242014-11-04 08:35:29 -070047 class Error : public tlv::Error
48 {
49 public:
Junxiao Shi68b53852018-07-25 13:56:38 -060050 using tlv::Error::Error;
Junxiao Shic2b8d242014-11-04 08:35:29 -070051 };
52
Junxiao Shi8d3f8342018-04-04 12:46:37 +000053 /** @brief Construct an Interest with given @p name and @p lifetime.
Davide Pesaventoadc9aa22019-06-30 19:00:20 -040054 *
55 * @throw std::invalid_argument @p name is invalid or @p lifetime is negative
Junxiao Shi8d3f8342018-04-04 12:46:37 +000056 * @warning In certain contexts that use `Interest::shared_from_this()`, Interest must be created
57 * using `make_shared`. Otherwise, `shared_from_this()` will trigger undefined behavior.
Alexander Afanasyevc348f832014-02-17 16:35:17 -080058 */
Junxiao Shi909ffef2017-07-07 22:12:27 +000059 explicit
Junxiao Shi8d3f8342018-04-04 12:46:37 +000060 Interest(const Name& name = Name(), time::milliseconds lifetime = DEFAULT_INTEREST_LIFETIME);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070061
Junxiao Shi8d3f8342018-04-04 12:46:37 +000062 /** @brief Construct an Interest by decoding from @p wire.
Davide Pesaventoadc9aa22019-06-30 19:00:20 -040063 *
Junxiao Shi8d3f8342018-04-04 12:46:37 +000064 * @warning In certain contexts that use `Interest::shared_from_this()`, Interest must be created
65 * using `make_shared`. Otherwise, `shared_from_this()` will trigger undefined behavior.
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070066 */
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080067 explicit
Junxiao Shi2af905b2014-11-27 13:10:54 -070068 Interest(const Block& wire);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080069
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -070070 /** @brief Prepend wire encoding to @p encoder.
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080071 */
Alexander Afanasyev74633892015-02-08 18:08:46 -080072 template<encoding::Tag TAG>
Alexander Afanasyev197e5652014-06-13 16:56:31 -070073 size_t
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070074 wireEncode(EncodingImpl<TAG>& encoder) const;
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080075
Junxiao Shi6efa3b72018-04-14 15:54:08 +000076 /** @brief Encode to a @c Block.
77 *
Davide Pesavento9c19a392019-04-06 15:07:54 -040078 * Encodes into NDN Packet Format v0.3 if ApplicationParameters element is present, in which
79 * case Selectors are not encoded. Otherwise, encodes into NDN Packet Format v0.2.
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080080 */
Alexander Afanasyev197e5652014-06-13 16:56:31 -070081 const Block&
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -080082 wireEncode() const;
Alexander Afanasyevc348f832014-02-17 16:35:17 -080083
Junxiao Shi6efa3b72018-04-14 15:54:08 +000084 /** @brief Decode from @p wire in NDN Packet Format v0.2 or v0.3.
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080085 */
Alexander Afanasyev197e5652014-06-13 16:56:31 -070086 void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070087 wireDecode(const Block& wire);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080088
Junxiao Shi8d3f8342018-04-04 12:46:37 +000089 /** @brief Check if this instance has cached wire encoding.
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080090 */
Alexander Afanasyev197e5652014-06-13 16:56:31 -070091 bool
Junxiao Shi2af905b2014-11-27 13:10:54 -070092 hasWire() const
93 {
94 return m_wire.hasWire();
95 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070096
Junxiao Shi8d3f8342018-04-04 12:46:37 +000097 /** @brief Return a URI-like string that represents the Interest.
Alexander Afanasyev770827c2014-05-13 17:42:55 -070098 *
Junxiao Shi8d3f8342018-04-04 12:46:37 +000099 * The string starts with `getName().toUri()`.
100 * If the Interest contains selectors, they are included as a query string.
101 * Example: "/test/name?ndn.MustBeFresh=1"
Jeff Thompson13e280b2013-12-03 13:12:23 -0800102 */
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700103 std::string
Jeff Thompson13e280b2013-12-03 13:12:23 -0800104 toUri() const;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700105
Junxiao Shi2af905b2014-11-27 13:10:54 -0700106public: // matching
107 /** @brief Check if Interest, including selectors, matches the given @p name
108 * @param name The name to be matched. If this is a Data name, it shall contain the
109 * implicit digest component
Alexander Afanasyev84681982014-01-03 13:26:09 -0800110 */
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000111 [[deprecated("use matchesData")]]
Alexander Afanasyev84681982014-01-03 13:26:09 -0800112 bool
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700113 matchesName(const Name& name) const;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800114
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000115 /** @brief Check if Interest can be satisfied by @p data.
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700116 *
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000117 * This method considers Name, CanBePrefix, and MustBeFresh. However, MustBeFresh processing
118 * is limited to rejecting Data with zero/omitted FreshnessPeriod.
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700119 */
120 bool
121 matchesData(const Data& data) const;
122
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000123 /** @brief Check if this Interest matches @p other
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800124 *
Junxiao Shi2ad2fbe2019-05-24 03:11:05 +0000125 * Two Interests match if both have the same Name, CanBePrefix, and MustBeFresh.
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800126 */
127 bool
128 matchesInterest(const Interest& other) const;
129
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000130public: // element access
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700131 const Name&
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800132 getName() const
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700133 {
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800134 return m_name;
135 }
136
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400137 /** @brief Set the Interest's name.
138 * @throw std::invalid_argument @p name is invalid
139 */
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800140 Interest&
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400141 setName(const Name& name);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700142
Junxiao Shib55e5d32018-07-18 13:32:00 -0600143 /** @brief Declare the default CanBePrefix setting of the application.
144 *
145 * As part of transitioning to NDN Packet Format v0.3, the default setting for CanBePrefix
146 * will be changed from "true" to "false". Application developers are advised to review all
147 * Interests expressed by their application and decide what CanBePrefix setting is appropriate
148 * for each Interest, to avoid breaking changes when the transition occurs. Application may
149 * either set CanBePrefix on a per-Interest basis, or declare a default CanBePrefix setting for
150 * all Interests expressed by the application using this function. If an application neither
151 * declares a default nor sets CanBePrefix on every Interest, Interest::wireEncode will print a
152 * one-time warning message.
153 *
154 * @note This function should not be used in libraries or in ndn-cxx unit tests.
155 * @sa https://redmine.named-data.net/projects/nfd/wiki/Packet03Transition
156 */
157 static void
158 setDefaultCanBePrefix(bool canBePrefix)
159 {
160 s_defaultCanBePrefix = canBePrefix;
161 }
162
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000163 /** @brief Check whether the CanBePrefix element is present.
164 *
165 * This is a getter for the CanBePrefix element as defined in NDN Packet Format v0.3.
166 * In this implementation, it is mapped to the closest v0.2 semantics:
167 * MaxSuffixComponents=1 means CanBePrefix is absent.
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300168 */
169 bool
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000170 getCanBePrefix() const
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800171 {
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000172 return m_selectors.getMaxSuffixComponents() != 1;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800173 }
174
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000175 /** @brief Add or remove CanBePrefix element.
176 * @param canBePrefix whether CanBePrefix element should be present.
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300177 *
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000178 * This is a setter for the CanBePrefix element as defined in NDN Packet Format v0.3.
179 * In this implementation, it is mapped to the closest v0.2 semantics:
180 * MaxSuffixComponents=1 means CanBePrefix is absent.
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300181 */
182 Interest&
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000183 setCanBePrefix(bool canBePrefix)
Junxiao Shi899277a2017-07-07 22:12:12 +0000184 {
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000185 m_selectors.setMaxSuffixComponents(canBePrefix ? -1 : 1);
186 m_wire.reset();
Junxiao Shib55e5d32018-07-18 13:32:00 -0600187 m_isCanBePrefixSet = true;
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000188 return *this;
Junxiao Shi899277a2017-07-07 22:12:12 +0000189 }
190
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000191 /** @brief Check whether the MustBeFresh element is present.
192 *
193 * This is a getter for the MustBeFresh element as defined in NDN Packet Format v0.3.
194 * In this implementation, it is mapped to the closest v0.2 semantics and appears as
195 * MustBeFresh element under Selectors.
196 */
197 bool
198 getMustBeFresh() const
199 {
200 return m_selectors.getMustBeFresh();
201 }
202
203 /** @brief Add or remove MustBeFresh element.
204 * @param mustBeFresh whether MustBeFresh element should be present.
205 *
206 * This is a setter for the MustBeFresh element as defined in NDN Packet Format v0.3.
207 * In this implementation, it is mapped to the closest v0.2 semantics and appears as
208 * MustBeFresh element under Selectors.
Junxiao Shi899277a2017-07-07 22:12:12 +0000209 */
210 Interest&
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000211 setMustBeFresh(bool mustBeFresh)
212 {
213 m_selectors.setMustBeFresh(mustBeFresh);
214 m_wire.reset();
215 return *this;
216 }
Junxiao Shi899277a2017-07-07 22:12:12 +0000217
Junxiao Shi9c154cb2017-07-07 22:14:54 +0000218 const DelegationList&
219 getForwardingHint() const
220 {
221 return m_forwardingHint;
222 }
223
224 Interest&
225 setForwardingHint(const DelegationList& value);
226
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000227 /** @brief Modify ForwardingHint in-place.
Junxiao Shib2a70332017-07-07 22:15:03 +0000228 * @tparam Modifier a unary function that accepts DelegationList&
229 *
230 * This is equivalent to, but more efficient (avoids copying) than:
231 * @code
232 * auto fh = interest.getForwardingHint();
233 * modifier(fh);
234 * interest.setForwardingHint(fh);
235 * @endcode
236 */
237 template<typename Modifier>
238 Interest&
239 modifyForwardingHint(const Modifier& modifier)
240 {
241 modifier(m_forwardingHint);
242 m_wire.reset();
243 return *this;
244 }
245
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000246 /** @brief Check if the Nonce element is present.
Junxiao Shi2af905b2014-11-27 13:10:54 -0700247 */
248 bool
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400249 hasNonce() const noexcept
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000250 {
Davide Pesaventofccb2dc2019-02-09 01:02:35 -0500251 return m_nonce.has_value();
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000252 }
253
254 /** @brief Get nonce value.
255 *
256 * If nonce was not present, it is added and assigned a random value.
257 */
258 uint32_t
259 getNonce() const;
260
261 /** @brief Set nonce value.
262 */
263 Interest&
264 setNonce(uint32_t nonce);
265
266 /** @brief Change nonce value.
267 *
268 * If the Nonce element is present, the new nonce value will differ from the old value.
269 * If the Nonce element is not present, this method does nothing.
270 */
271 void
272 refreshNonce();
273
274 time::milliseconds
275 getInterestLifetime() const
276 {
277 return m_interestLifetime;
278 }
279
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400280 /** @brief Set the Interest's lifetime.
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000281 * @throw std::invalid_argument @p lifetime is negative
282 */
283 Interest&
284 setInterestLifetime(time::milliseconds lifetime);
285
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400286 optional<uint8_t>
287 getHopLimit() const
288 {
289 return m_hopLimit;
290 }
291
292 /** @brief Set the Interest's hop limit.
293 *
294 * Use `setHopLimit(nullopt)` to remove any hop limit from the Interest.
295 */
296 Interest&
297 setHopLimit(optional<uint8_t> hopLimit);
298
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700299 bool
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400300 hasApplicationParameters() const noexcept
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700301 {
302 return !m_parameters.empty();
303 }
304
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400305 Block
Davide Pesavento9c19a392019-04-06 15:07:54 -0400306 getApplicationParameters() const
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700307 {
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400308 if (m_parameters.empty())
309 return {};
310 else
311 return m_parameters.front();
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700312 }
313
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400314 /** @brief Set ApplicationParameters from a Block.
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700315 * @return a reference to this Interest
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400316 *
317 * If the block is default-constructed, this will set a zero-length ApplicationParameters
318 * element. Else, if the block's TLV-TYPE is ApplicationParameters, it will be used directly
319 * as this Interest's ApplicationParameters element. Else, the block will be nested into an
320 * ApplicationParameters element.
321 *
322 * This function will also recompute the value of the ParametersSha256DigestComponent in the
323 * Interest's name. If the name does not contain a ParametersSha256DigestComponent, one will
324 * be appended to it.
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700325 */
326 Interest&
Davide Pesavento9c19a392019-04-06 15:07:54 -0400327 setApplicationParameters(const Block& parameters);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700328
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400329 /** @brief Set ApplicationParameters by copying from a raw buffer.
330 * @param value points to a buffer from which the TLV-VALUE of the parameters will be copied;
331 * may be nullptr if @p length is zero
332 * @param length size of the buffer
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700333 * @return a reference to this Interest
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400334 *
335 * This function will also recompute the value of the ParametersSha256DigestComponent in the
336 * Interest's name. If the name does not contain a ParametersSha256DigestComponent, one will
337 * be appended to it.
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700338 */
339 Interest&
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400340 setApplicationParameters(const uint8_t* value, size_t length);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700341
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400342 /** @brief Set ApplicationParameters from a shared buffer.
343 * @param value buffer containing the TLV-VALUE of the parameters; must not be nullptr
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700344 * @return a reference to this Interest
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400345 *
346 * This function will also recompute the value of the ParametersSha256DigestComponent in the
347 * Interest's name. If the name does not contain a ParametersSha256DigestComponent, one will
348 * be appended to it.
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700349 */
350 Interest&
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400351 setApplicationParameters(ConstBufferPtr value);
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700352
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400353 /** @brief Remove the ApplicationParameters element from this Interest.
Davide Pesavento9c19a392019-04-06 15:07:54 -0400354 * @post hasApplicationParameters() == false
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400355 *
356 * This function will also remove any ParametersSha256DigestComponents from the Interest's name.
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700357 */
358 Interest&
Davide Pesavento9c19a392019-04-06 15:07:54 -0400359 unsetApplicationParameters();
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700360
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400361public: // ParametersSha256DigestComponent support
362 static bool
363 getAutoCheckParametersDigest()
364 {
365 return s_autoCheckParametersDigest;
366 }
367
368 static void
369 setAutoCheckParametersDigest(bool b)
370 {
371 s_autoCheckParametersDigest = b;
372 }
373
374 /** @brief Check if the ParametersSha256DigestComponent in the name is valid.
375 *
376 * Returns true if there is a single ParametersSha256DigestComponent in the name and the digest
377 * value is correct, or if there is no ParametersSha256DigestComponent in the name and the
378 * Interest does not contain any parameters.
379 * Returns false otherwise.
380 */
381 bool
382 isParametersDigestValid() const;
383
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000384public: // Selectors (deprecated)
385 /** @brief Check if Interest has any selector present.
386 */
Davide Pesavento1fd00242018-05-20 00:11:01 -0400387 [[deprecated]]
Junxiao Shi8d3f8342018-04-04 12:46:37 +0000388 bool
Junxiao Shi2af905b2014-11-27 13:10:54 -0700389 hasSelectors() const
390 {
391 return !m_selectors.empty();
392 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700393
Davide Pesavento1fd00242018-05-20 00:11:01 -0400394 [[deprecated]]
Junxiao Shi2af905b2014-11-27 13:10:54 -0700395 const Selectors&
396 getSelectors() const
397 {
398 return m_selectors;
399 }
400
Davide Pesavento1fd00242018-05-20 00:11:01 -0400401 [[deprecated]]
Junxiao Shi2af905b2014-11-27 13:10:54 -0700402 Interest&
403 setSelectors(const Selectors& selectors)
404 {
405 m_selectors = selectors;
406 m_wire.reset();
407 return *this;
408 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700409
Davide Pesavento1fd00242018-05-20 00:11:01 -0400410 [[deprecated]]
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800411 int
412 getMinSuffixComponents() const
413 {
414 return m_selectors.getMinSuffixComponents();
415 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700416
Davide Pesavento1fd00242018-05-20 00:11:01 -0400417 [[deprecated]]
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800418 Interest&
419 setMinSuffixComponents(int minSuffixComponents)
420 {
421 m_selectors.setMinSuffixComponents(minSuffixComponents);
422 m_wire.reset();
423 return *this;
424 }
425
Davide Pesavento1fd00242018-05-20 00:11:01 -0400426 [[deprecated]]
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800427 int
428 getMaxSuffixComponents() const
429 {
430 return m_selectors.getMaxSuffixComponents();
431 }
432
Davide Pesavento1fd00242018-05-20 00:11:01 -0400433 [[deprecated]]
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800434 Interest&
435 setMaxSuffixComponents(int maxSuffixComponents)
436 {
437 m_selectors.setMaxSuffixComponents(maxSuffixComponents);
438 m_wire.reset();
439 return *this;
440 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700441
Davide Pesavento1fd00242018-05-20 00:11:01 -0400442 [[deprecated]]
Junxiao Shib332e782014-03-31 14:23:46 -0700443 const KeyLocator&
444 getPublisherPublicKeyLocator() const
445 {
446 return m_selectors.getPublisherPublicKeyLocator();
447 }
448
Davide Pesavento1fd00242018-05-20 00:11:01 -0400449 [[deprecated]]
Junxiao Shib332e782014-03-31 14:23:46 -0700450 Interest&
451 setPublisherPublicKeyLocator(const KeyLocator& keyLocator)
452 {
453 m_selectors.setPublisherPublicKeyLocator(keyLocator);
454 m_wire.reset();
455 return *this;
456 }
457
Davide Pesavento1fd00242018-05-20 00:11:01 -0400458 [[deprecated]]
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800459 const Exclude&
460 getExclude() const
461 {
462 return m_selectors.getExclude();
463 }
464
Davide Pesavento1fd00242018-05-20 00:11:01 -0400465 [[deprecated]]
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800466 Interest&
467 setExclude(const Exclude& exclude)
468 {
469 m_selectors.setExclude(exclude);
470 m_wire.reset();
471 return *this;
472 }
473
Davide Pesavento1fd00242018-05-20 00:11:01 -0400474 [[deprecated]]
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700475 int
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800476 getChildSelector() const
477 {
478 return m_selectors.getChildSelector();
479 }
480
Davide Pesavento1fd00242018-05-20 00:11:01 -0400481 [[deprecated]]
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800482 Interest&
483 setChildSelector(int childSelector)
484 {
485 m_selectors.setChildSelector(childSelector);
486 m_wire.reset();
487 return *this;
488 }
489
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800490private:
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700491 /** @brief Prepend wire encoding to @p encoder in NDN Packet Format v0.2.
492 */
493 template<encoding::Tag TAG>
494 size_t
495 encode02(EncodingImpl<TAG>& encoder) const;
496
497 /** @brief Prepend wire encoding to @p encoder in NDN Packet Format v0.3.
498 */
499 template<encoding::Tag TAG>
500 size_t
501 encode03(EncodingImpl<TAG>& encoder) const;
502
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000503 /** @brief Decode @c m_wire as NDN Packet Format v0.2.
504 * @retval true decoding successful.
505 * @retval false decoding failed due to structural error.
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400506 * @throw tlv::Error decoding error.
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000507 */
508 bool
509 decode02();
510
511 /** @brief Decode @c m_wire as NDN Packet Format v0.3.
512 * @throw tlv::Error decoding error.
513 */
514 void
515 decode03();
516
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400517 void
518 setApplicationParametersInternal(Block parameters);
519
520 shared_ptr<Buffer>
521 computeParametersDigest() const;
522
523 /** @brief Append a ParametersSha256DigestComponent to the Interest's name
524 * or update the digest value in the existing component.
525 *
526 * @pre The name is assumed to be valid, i.e., it must not contain more than one
527 * ParametersSha256DigestComponent.
528 * @pre hasApplicationParameters() == true
529 */
530 void
531 addOrReplaceParametersDigestComponent();
532
533 /** @brief Return the index of the ParametersSha256DigestComponent in @p name.
534 *
535 * @retval pos The name contains exactly one ParametersSha256DigestComponent at index `pos`.
536 * @retval -1 The name contains zero ParametersSha256DigestComponents.
537 * @retval -2 The name contains more than one ParametersSha256DigestComponents.
538 */
539 static ssize_t
540 findParametersDigestComponent(const Name& name);
541
Junxiao Shib55e5d32018-07-18 13:32:00 -0600542#ifdef NDN_CXX_HAVE_TESTS
543public:
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400544 /// If true, not setting CanBePrefix results in an error in wireEncode().
Junxiao Shib55e5d32018-07-18 13:32:00 -0600545 static bool s_errorIfCanBePrefixUnset;
546#endif // NDN_CXX_HAVE_TESTS
547
Junxiao Shi6efa3b72018-04-14 15:54:08 +0000548private:
Junxiao Shib55e5d32018-07-18 13:32:00 -0600549 static boost::logic::tribool s_defaultCanBePrefix;
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400550 static bool s_autoCheckParametersDigest;
Junxiao Shib55e5d32018-07-18 13:32:00 -0600551
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800552 Name m_name;
Arthi Padmanabhanb38664e2018-07-18 11:13:12 -0700553 Selectors m_selectors; // NDN Packet Format v0.2 only
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400554 mutable bool m_isCanBePrefixSet = false;
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400555 DelegationList m_forwardingHint;
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000556 mutable optional<uint32_t> m_nonce;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700557 time::milliseconds m_interestLifetime;
Davide Pesavento2b0cc7b2019-07-14 16:50:04 -0400558 optional<uint8_t> m_hopLimit;
Davide Pesaventoadc9aa22019-06-30 19:00:20 -0400559
560 // Stores the "Interest parameters", i.e., all maybe-unrecognized non-critical TLV
561 // elements that appear at the end of the Interest, starting from ApplicationParameters.
562 // If the Interest does not contain any ApplicationParameters TLV, this vector will
563 // be empty. Conversely, if this vector is not empty, the first element will always
564 // be an ApplicationParameters block. All blocks in this vector are covered by the
565 // digest in the ParametersSha256DigestComponent.
566 std::vector<Block> m_parameters; // NDN Packet Format v0.3 only
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800567
568 mutable Block m_wire;
Junxiao Shib55e5d32018-07-18 13:32:00 -0600569
570 friend bool operator==(const Interest& lhs, const Interest& rhs);
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700571};
Alexander Afanasyev84681982014-01-03 13:26:09 -0800572
Davide Pesavento88a0d812017-08-19 21:31:42 -0400573NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(Interest);
574
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700575std::ostream&
576operator<<(std::ostream& os, const Interest& interest);
Alexander Afanasyev84681982014-01-03 13:26:09 -0800577
Junxiao Shib55e5d32018-07-18 13:32:00 -0600578bool
579operator==(const Interest& lhs, const Interest& rhs);
Junxiao Shi899277a2017-07-07 22:12:12 +0000580
581inline bool
582operator!=(const Interest& lhs, const Interest& rhs)
583{
584 return !(lhs == rhs);
585}
586
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800587} // namespace ndn
588
589#endif // NDN_INTEREST_HPP