Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2019 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 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. |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #ifndef NDN_INTEREST_HPP |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 23 | #define NDN_INTEREST_HPP |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 24 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 25 | #include "ndn-cxx/delegation-list.hpp" |
| 26 | #include "ndn-cxx/name.hpp" |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 27 | #include "ndn-cxx/selectors.hpp" |
Junxiao Shi | 1e36ceb | 2018-12-17 13:20:26 -0700 | [diff] [blame] | 28 | #include "ndn-cxx/detail/packet-base.hpp" |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 29 | #include "ndn-cxx/util/time.hpp" |
| 30 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 31 | #include <boost/logic/tribool.hpp> |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 32 | |
| 33 | namespace ndn { |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 34 | |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 35 | class Data; |
| 36 | |
Junxiao Shi | 7007a3c | 2014-11-20 22:37:55 -0700 | [diff] [blame] | 37 | /** @var const unspecified_duration_type DEFAULT_INTEREST_LIFETIME; |
| 38 | * @brief default value for InterestLifetime |
| 39 | */ |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 40 | const time::milliseconds DEFAULT_INTEREST_LIFETIME = 4_s; |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 41 | |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 42 | /** @brief Represents an Interest packet. |
Jeff Thompson | 8238d00 | 2013-07-10 11:56:49 -0700 | [diff] [blame] | 43 | */ |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 44 | class Interest : public PacketBase, public std::enable_shared_from_this<Interest> |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 45 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 46 | public: |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 47 | class Error : public tlv::Error |
| 48 | { |
| 49 | public: |
Junxiao Shi | 68b5385 | 2018-07-25 13:56:38 -0600 | [diff] [blame] | 50 | using tlv::Error::Error; |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 53 | /** @brief Construct an Interest with given @p name and @p lifetime. |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 54 | * |
| 55 | * @throw std::invalid_argument @p name is invalid or @p lifetime is negative |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 56 | * @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 Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 58 | */ |
Junxiao Shi | 909ffef | 2017-07-07 22:12:27 +0000 | [diff] [blame] | 59 | explicit |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 60 | Interest(const Name& name = Name(), time::milliseconds lifetime = DEFAULT_INTEREST_LIFETIME); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 61 | |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 62 | /** @brief Construct an Interest by decoding from @p wire. |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 63 | * |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 64 | * @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 Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 66 | */ |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 67 | explicit |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 68 | Interest(const Block& wire); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 69 | |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 70 | /** @brief Prepend wire encoding to @p encoder. |
Jeff Thompson | 1b4a7b1 | 2013-11-15 12:00:02 -0800 | [diff] [blame] | 71 | */ |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 72 | template<encoding::Tag TAG> |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 73 | size_t |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 74 | wireEncode(EncodingImpl<TAG>& encoder) const; |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 75 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 76 | /** @brief Encode to a @c Block. |
| 77 | * |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 78 | * 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 Thompson | 1b4a7b1 | 2013-11-15 12:00:02 -0800 | [diff] [blame] | 80 | */ |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 81 | const Block& |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 82 | wireEncode() const; |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 83 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 84 | /** @brief Decode from @p wire in NDN Packet Format v0.2 or v0.3. |
Jeff Thompson | 1b4a7b1 | 2013-11-15 12:00:02 -0800 | [diff] [blame] | 85 | */ |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 86 | void |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 87 | wireDecode(const Block& wire); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 88 | |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 89 | /** @brief Check if this instance has cached wire encoding. |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 90 | */ |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 91 | bool |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 92 | hasWire() const |
| 93 | { |
| 94 | return m_wire.hasWire(); |
| 95 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 96 | |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 97 | /** @brief Return a URI-like string that represents the Interest. |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 98 | * |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 99 | * 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 Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 102 | */ |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 103 | std::string |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 104 | toUri() const; |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 105 | |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 106 | public: // 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 Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 110 | */ |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 111 | [[deprecated("use matchesData")]] |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 112 | bool |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 113 | matchesName(const Name& name) const; |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 114 | |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 115 | /** @brief Check if Interest can be satisfied by @p data. |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 116 | * |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 117 | * This method considers Name, CanBePrefix, and MustBeFresh. However, MustBeFresh processing |
| 118 | * is limited to rejecting Data with zero/omitted FreshnessPeriod. |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 119 | */ |
| 120 | bool |
| 121 | matchesData(const Data& data) const; |
| 122 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 123 | /** @brief Check if this Interest matches @p other |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 124 | * |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 125 | * Two Interests match if both have the same Name, CanBePrefix, and MustBeFresh. |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 126 | */ |
| 127 | bool |
| 128 | matchesInterest(const Interest& other) const; |
| 129 | |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 130 | public: // element access |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 131 | const Name& |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 132 | getName() const |
Jeff Thompson | c0486c1 | 2013-07-16 14:36:16 -0700 | [diff] [blame] | 133 | { |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 134 | return m_name; |
| 135 | } |
| 136 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 137 | /** @brief Set the Interest's name. |
| 138 | * @throw std::invalid_argument @p name is invalid |
| 139 | */ |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 140 | Interest& |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 141 | setName(const Name& name); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 142 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 143 | /** @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 Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 163 | /** @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 Afanasyev | e881e93 | 2014-06-08 14:47:03 +0300 | [diff] [blame] | 168 | */ |
| 169 | bool |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 170 | getCanBePrefix() const |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 171 | { |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 172 | return m_selectors.getMaxSuffixComponents() != 1; |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 175 | /** @brief Add or remove CanBePrefix element. |
| 176 | * @param canBePrefix whether CanBePrefix element should be present. |
Alexander Afanasyev | e881e93 | 2014-06-08 14:47:03 +0300 | [diff] [blame] | 177 | * |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 178 | * 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 Afanasyev | e881e93 | 2014-06-08 14:47:03 +0300 | [diff] [blame] | 181 | */ |
| 182 | Interest& |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 183 | setCanBePrefix(bool canBePrefix) |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 184 | { |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 185 | m_selectors.setMaxSuffixComponents(canBePrefix ? -1 : 1); |
| 186 | m_wire.reset(); |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 187 | m_isCanBePrefixSet = true; |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 188 | return *this; |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 191 | /** @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 Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 209 | */ |
| 210 | Interest& |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 211 | setMustBeFresh(bool mustBeFresh) |
| 212 | { |
| 213 | m_selectors.setMustBeFresh(mustBeFresh); |
| 214 | m_wire.reset(); |
| 215 | return *this; |
| 216 | } |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 217 | |
Junxiao Shi | 9c154cb | 2017-07-07 22:14:54 +0000 | [diff] [blame] | 218 | const DelegationList& |
| 219 | getForwardingHint() const |
| 220 | { |
| 221 | return m_forwardingHint; |
| 222 | } |
| 223 | |
| 224 | Interest& |
| 225 | setForwardingHint(const DelegationList& value); |
| 226 | |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 227 | /** @brief Modify ForwardingHint in-place. |
Junxiao Shi | b2a7033 | 2017-07-07 22:15:03 +0000 | [diff] [blame] | 228 | * @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 Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 246 | /** @brief Check if the Nonce element is present. |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 247 | */ |
| 248 | bool |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 249 | hasNonce() const noexcept |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 250 | { |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 251 | return m_nonce.has_value(); |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 252 | } |
| 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 Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 280 | /** @brief Set the Interest's lifetime. |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 281 | * @throw std::invalid_argument @p lifetime is negative |
| 282 | */ |
| 283 | Interest& |
| 284 | setInterestLifetime(time::milliseconds lifetime); |
| 285 | |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame^] | 286 | 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 Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 299 | bool |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 300 | hasApplicationParameters() const noexcept |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 301 | { |
| 302 | return !m_parameters.empty(); |
| 303 | } |
| 304 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 305 | Block |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 306 | getApplicationParameters() const |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 307 | { |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 308 | if (m_parameters.empty()) |
| 309 | return {}; |
| 310 | else |
| 311 | return m_parameters.front(); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 314 | /** @brief Set ApplicationParameters from a Block. |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 315 | * @return a reference to this Interest |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 316 | * |
| 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 Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 325 | */ |
| 326 | Interest& |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 327 | setApplicationParameters(const Block& parameters); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 328 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 329 | /** @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 Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 333 | * @return a reference to this Interest |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 334 | * |
| 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 Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 338 | */ |
| 339 | Interest& |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 340 | setApplicationParameters(const uint8_t* value, size_t length); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 341 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 342 | /** @brief Set ApplicationParameters from a shared buffer. |
| 343 | * @param value buffer containing the TLV-VALUE of the parameters; must not be nullptr |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 344 | * @return a reference to this Interest |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 345 | * |
| 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 Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 349 | */ |
| 350 | Interest& |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 351 | setApplicationParameters(ConstBufferPtr value); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 352 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 353 | /** @brief Remove the ApplicationParameters element from this Interest. |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 354 | * @post hasApplicationParameters() == false |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 355 | * |
| 356 | * This function will also remove any ParametersSha256DigestComponents from the Interest's name. |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 357 | */ |
| 358 | Interest& |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 359 | unsetApplicationParameters(); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 360 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 361 | public: // 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 Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 384 | public: // Selectors (deprecated) |
| 385 | /** @brief Check if Interest has any selector present. |
| 386 | */ |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 387 | [[deprecated]] |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 388 | bool |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 389 | hasSelectors() const |
| 390 | { |
| 391 | return !m_selectors.empty(); |
| 392 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 393 | |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 394 | [[deprecated]] |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 395 | const Selectors& |
| 396 | getSelectors() const |
| 397 | { |
| 398 | return m_selectors; |
| 399 | } |
| 400 | |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 401 | [[deprecated]] |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 402 | Interest& |
| 403 | setSelectors(const Selectors& selectors) |
| 404 | { |
| 405 | m_selectors = selectors; |
| 406 | m_wire.reset(); |
| 407 | return *this; |
| 408 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 409 | |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 410 | [[deprecated]] |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 411 | int |
| 412 | getMinSuffixComponents() const |
| 413 | { |
| 414 | return m_selectors.getMinSuffixComponents(); |
| 415 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 416 | |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 417 | [[deprecated]] |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 418 | Interest& |
| 419 | setMinSuffixComponents(int minSuffixComponents) |
| 420 | { |
| 421 | m_selectors.setMinSuffixComponents(minSuffixComponents); |
| 422 | m_wire.reset(); |
| 423 | return *this; |
| 424 | } |
| 425 | |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 426 | [[deprecated]] |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 427 | int |
| 428 | getMaxSuffixComponents() const |
| 429 | { |
| 430 | return m_selectors.getMaxSuffixComponents(); |
| 431 | } |
| 432 | |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 433 | [[deprecated]] |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 434 | Interest& |
| 435 | setMaxSuffixComponents(int maxSuffixComponents) |
| 436 | { |
| 437 | m_selectors.setMaxSuffixComponents(maxSuffixComponents); |
| 438 | m_wire.reset(); |
| 439 | return *this; |
| 440 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 441 | |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 442 | [[deprecated]] |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 443 | const KeyLocator& |
| 444 | getPublisherPublicKeyLocator() const |
| 445 | { |
| 446 | return m_selectors.getPublisherPublicKeyLocator(); |
| 447 | } |
| 448 | |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 449 | [[deprecated]] |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 450 | Interest& |
| 451 | setPublisherPublicKeyLocator(const KeyLocator& keyLocator) |
| 452 | { |
| 453 | m_selectors.setPublisherPublicKeyLocator(keyLocator); |
| 454 | m_wire.reset(); |
| 455 | return *this; |
| 456 | } |
| 457 | |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 458 | [[deprecated]] |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 459 | const Exclude& |
| 460 | getExclude() const |
| 461 | { |
| 462 | return m_selectors.getExclude(); |
| 463 | } |
| 464 | |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 465 | [[deprecated]] |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 466 | Interest& |
| 467 | setExclude(const Exclude& exclude) |
| 468 | { |
| 469 | m_selectors.setExclude(exclude); |
| 470 | m_wire.reset(); |
| 471 | return *this; |
| 472 | } |
| 473 | |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 474 | [[deprecated]] |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 475 | int |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 476 | getChildSelector() const |
| 477 | { |
| 478 | return m_selectors.getChildSelector(); |
| 479 | } |
| 480 | |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 481 | [[deprecated]] |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 482 | Interest& |
| 483 | setChildSelector(int childSelector) |
| 484 | { |
| 485 | m_selectors.setChildSelector(childSelector); |
| 486 | m_wire.reset(); |
| 487 | return *this; |
| 488 | } |
| 489 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 490 | private: |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 491 | /** @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 Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 503 | /** @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 Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 506 | * @throw tlv::Error decoding error. |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 507 | */ |
| 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 Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 517 | 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 Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 542 | #ifdef NDN_CXX_HAVE_TESTS |
| 543 | public: |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 544 | /// If true, not setting CanBePrefix results in an error in wireEncode(). |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 545 | static bool s_errorIfCanBePrefixUnset; |
| 546 | #endif // NDN_CXX_HAVE_TESTS |
| 547 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 548 | private: |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 549 | static boost::logic::tribool s_defaultCanBePrefix; |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 550 | static bool s_autoCheckParametersDigest; |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 551 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 552 | Name m_name; |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 553 | Selectors m_selectors; // NDN Packet Format v0.2 only |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 554 | mutable bool m_isCanBePrefixSet = false; |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame^] | 555 | DelegationList m_forwardingHint; |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 556 | mutable optional<uint32_t> m_nonce; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 557 | time::milliseconds m_interestLifetime; |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame^] | 558 | optional<uint8_t> m_hopLimit; |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 559 | |
| 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 Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 567 | |
| 568 | mutable Block m_wire; |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 569 | |
| 570 | friend bool operator==(const Interest& lhs, const Interest& rhs); |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 571 | }; |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 572 | |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 573 | NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(Interest); |
| 574 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 575 | std::ostream& |
| 576 | operator<<(std::ostream& os, const Interest& interest); |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 577 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 578 | bool |
| 579 | operator==(const Interest& lhs, const Interest& rhs); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 580 | |
| 581 | inline bool |
| 582 | operator!=(const Interest& lhs, const Interest& rhs) |
| 583 | { |
| 584 | return !(lhs == rhs); |
| 585 | } |
| 586 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 587 | } // namespace ndn |
| 588 | |
| 589 | #endif // NDN_INTEREST_HPP |