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 | /* |
Junxiao Shi | 4ffbb9d | 2018-03-31 17:16:35 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 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 | |
Alexander Afanasyev | 09c613f | 2014-01-29 00:23:58 -0800 | [diff] [blame] | 22 | #include "interest.hpp" |
Alexander Afanasyev | e9fdb80 | 2014-02-05 17:36:51 -0800 | [diff] [blame] | 23 | #include "util/random.hpp" |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 24 | #include "data.hpp" |
Alexander Afanasyev | 840139f | 2013-12-28 15:02:50 -0800 | [diff] [blame] | 25 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 26 | #include <boost/scope_exit.hpp> |
| 27 | |
Davide Pesavento | e178989 | 2017-02-26 15:50:52 -0500 | [diff] [blame] | 28 | #include <cstring> |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 29 | #include <iostream> |
Davide Pesavento | a84f464 | 2017-08-23 16:14:51 -0400 | [diff] [blame] | 30 | #include <sstream> |
Davide Pesavento | e178989 | 2017-02-26 15:50:52 -0500 | [diff] [blame] | 31 | |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 32 | namespace ndn { |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 33 | |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 34 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Interest>)); |
| 35 | BOOST_CONCEPT_ASSERT((WireEncodable<Interest>)); |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 36 | BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<Interest>)); |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 37 | BOOST_CONCEPT_ASSERT((WireDecodable<Interest>)); |
| 38 | static_assert(std::is_base_of<tlv::Error, Interest::Error>::value, |
| 39 | "Interest::Error must inherit from tlv::Error"); |
| 40 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 41 | #ifdef NDN_CXX_HAVE_TESTS |
| 42 | bool Interest::s_errorIfCanBePrefixUnset = true; |
| 43 | #endif // NDN_CXX_HAVE_TESTS |
| 44 | boost::logic::tribool Interest::s_defaultCanBePrefix = boost::logic::indeterminate; |
| 45 | |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 46 | Interest::Interest(const Name& name, time::milliseconds lifetime) |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 47 | : m_name(name) |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 48 | , m_isCanBePrefixSet(false) |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 49 | , m_interestLifetime(lifetime) |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 50 | { |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 51 | if (lifetime < time::milliseconds::zero()) { |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 52 | BOOST_THROW_EXCEPTION(std::invalid_argument("InterestLifetime must be >= 0")); |
| 53 | } |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 54 | |
| 55 | if (!boost::logic::indeterminate(s_defaultCanBePrefix)) { |
| 56 | setCanBePrefix(static_cast<bool>(s_defaultCanBePrefix)); |
| 57 | } |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 60 | Interest::Interest(const Block& wire) |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 61 | : m_isCanBePrefixSet(true) |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 62 | { |
| 63 | wireDecode(wire); |
| 64 | } |
| 65 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 66 | // ---- encode and decode ---- |
Alexander Afanasyev | 840139f | 2013-12-28 15:02:50 -0800 | [diff] [blame] | 67 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 68 | template<encoding::Tag TAG> |
| 69 | size_t |
| 70 | Interest::wireEncode(EncodingImpl<TAG>& encoder) const |
| 71 | { |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 72 | static bool hasDefaultCanBePrefixWarning = false; |
| 73 | if (!m_isCanBePrefixSet) { |
| 74 | if (!hasDefaultCanBePrefixWarning) { |
| 75 | std::cerr << "WARNING: Interest.CanBePrefix will be set to 0 in the near future. " |
| 76 | << "Please declare a preferred setting via Interest::setDefaultCanBePrefix."; |
| 77 | hasDefaultCanBePrefixWarning = true; |
| 78 | } |
| 79 | #ifdef NDN_CXX_HAVE_TESTS |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 80 | if (s_errorIfCanBePrefixUnset) { |
| 81 | BOOST_THROW_EXCEPTION(std::logic_error("Interest.CanBePrefix is unset")); |
| 82 | } |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 83 | #endif // NDN_CXX_HAVE_TESTS |
| 84 | } |
| 85 | |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 86 | if (hasParameters()) { |
| 87 | return encode03(encoder); |
| 88 | } |
| 89 | else { |
| 90 | return encode02(encoder); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | template<encoding::Tag TAG> |
| 95 | size_t |
| 96 | Interest::encode02(EncodingImpl<TAG>& encoder) const |
| 97 | { |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 98 | size_t totalLength = 0; |
| 99 | |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 100 | // Encode as NDN Packet Format v0.2 |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 101 | // Interest ::= INTEREST-TYPE TLV-LENGTH |
| 102 | // Name |
| 103 | // Selectors? |
| 104 | // Nonce |
| 105 | // InterestLifetime? |
Junxiao Shi | 9c154cb | 2017-07-07 22:14:54 +0000 | [diff] [blame] | 106 | // ForwardingHint? |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 107 | |
| 108 | // (reverse encoding) |
| 109 | |
Junxiao Shi | 9c154cb | 2017-07-07 22:14:54 +0000 | [diff] [blame] | 110 | // ForwardingHint |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 111 | if (getForwardingHint().size() > 0) { |
| 112 | totalLength += getForwardingHint().wireEncode(encoder); |
Junxiao Shi | 9c154cb | 2017-07-07 22:14:54 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 115 | // InterestLifetime |
| 116 | if (getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) { |
| 117 | totalLength += prependNonNegativeIntegerBlock(encoder, |
| 118 | tlv::InterestLifetime, |
| 119 | getInterestLifetime().count()); |
| 120 | } |
| 121 | |
| 122 | // Nonce |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 123 | uint32_t nonce = getNonce(); // if nonce was unset, getNonce generates a random nonce |
| 124 | totalLength += encoder.prependByteArrayBlock(tlv::Nonce, reinterpret_cast<uint8_t*>(&nonce), sizeof(nonce)); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 125 | |
| 126 | // Selectors |
| 127 | if (hasSelectors()) { |
| 128 | totalLength += getSelectors().wireEncode(encoder); |
| 129 | } |
| 130 | |
| 131 | // Name |
| 132 | totalLength += getName().wireEncode(encoder); |
| 133 | |
| 134 | totalLength += encoder.prependVarNumber(totalLength); |
| 135 | totalLength += encoder.prependVarNumber(tlv::Interest); |
| 136 | return totalLength; |
Alexander Afanasyev | 840139f | 2013-12-28 15:02:50 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 139 | template<encoding::Tag TAG> |
| 140 | size_t |
| 141 | Interest::encode03(EncodingImpl<TAG>& encoder) const |
| 142 | { |
| 143 | size_t totalLength = 0; |
| 144 | |
| 145 | // Encode as NDN Packet Format v0.3 |
| 146 | // Interest ::= INTEREST-TYPE TLV-LENGTH |
| 147 | // Name |
| 148 | // CanBePrefix? |
| 149 | // MustBeFresh? |
| 150 | // ForwardingHint? |
| 151 | // Nonce? |
| 152 | // InterestLifetime? |
| 153 | // HopLimit? |
| 154 | // Parameters? |
| 155 | |
| 156 | // (reverse encoding) |
| 157 | |
| 158 | // Parameters |
| 159 | if (hasParameters()) { |
| 160 | totalLength += encoder.prependBlock(getParameters()); |
| 161 | } |
| 162 | |
| 163 | // HopLimit: not yet supported |
| 164 | |
| 165 | // InterestLifetime |
| 166 | if (getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) { |
| 167 | totalLength += prependNonNegativeIntegerBlock(encoder, |
| 168 | tlv::InterestLifetime, |
| 169 | getInterestLifetime().count()); |
| 170 | } |
| 171 | |
| 172 | // Nonce |
| 173 | uint32_t nonce = getNonce(); // if nonce was unset, getNonce generates a random nonce |
| 174 | totalLength += encoder.prependByteArrayBlock(tlv::Nonce, reinterpret_cast<uint8_t*>(&nonce), sizeof(nonce)); |
| 175 | |
| 176 | // ForwardingHint |
| 177 | if (getForwardingHint().size() > 0) { |
| 178 | totalLength += getForwardingHint().wireEncode(encoder); |
| 179 | } |
| 180 | |
| 181 | // MustBeFresh |
| 182 | if (getMustBeFresh()) { |
| 183 | totalLength += prependEmptyBlock(encoder, tlv::MustBeFresh); |
| 184 | } |
| 185 | |
| 186 | // CanBePrefix |
| 187 | if (getCanBePrefix()) { |
| 188 | totalLength += prependEmptyBlock(encoder, tlv::CanBePrefix); |
| 189 | } |
| 190 | |
| 191 | // Name |
| 192 | totalLength += getName().wireEncode(encoder); |
| 193 | |
| 194 | totalLength += encoder.prependVarNumber(totalLength); |
| 195 | totalLength += encoder.prependVarNumber(tlv::Interest); |
| 196 | return totalLength; |
| 197 | } |
| 198 | |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 199 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(Interest); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 200 | |
| 201 | const Block& |
| 202 | Interest::wireEncode() const |
Alexander Afanasyev | e881e93 | 2014-06-08 14:47:03 +0300 | [diff] [blame] | 203 | { |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 204 | if (m_wire.hasWire()) |
| 205 | return m_wire; |
| 206 | |
| 207 | EncodingEstimator estimator; |
| 208 | size_t estimatedSize = wireEncode(estimator); |
| 209 | |
| 210 | EncodingBuffer buffer(estimatedSize, 0); |
| 211 | wireEncode(buffer); |
| 212 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 213 | const_cast<Interest*>(this)->wireDecode(buffer.block()); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 214 | return m_wire; |
Alexander Afanasyev | e881e93 | 2014-06-08 14:47:03 +0300 | [diff] [blame] | 215 | } |
Alexander Afanasyev | 840139f | 2013-12-28 15:02:50 -0800 | [diff] [blame] | 216 | |
Alexander Afanasyev | c393217 | 2014-07-10 18:53:56 -0700 | [diff] [blame] | 217 | void |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 218 | Interest::wireDecode(const Block& wire) |
Alexander Afanasyev | c393217 | 2014-07-10 18:53:56 -0700 | [diff] [blame] | 219 | { |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 220 | m_wire = wire; |
| 221 | m_wire.parse(); |
Alexander Afanasyev | c393217 | 2014-07-10 18:53:56 -0700 | [diff] [blame] | 222 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 223 | if (m_wire.type() != tlv::Interest) { |
| 224 | BOOST_THROW_EXCEPTION(Error("expecting Interest element, got " + to_string(m_wire.type()))); |
| 225 | } |
| 226 | |
| 227 | if (!decode02()) { |
| 228 | decode03(); |
| 229 | if (!hasNonce()) { |
| 230 | setNonce(getNonce()); |
| 231 | } |
| 232 | } |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 233 | |
| 234 | m_isCanBePrefixSet = true; // don't trigger warning from decoded packet |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | bool |
| 238 | Interest::decode02() |
| 239 | { |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 240 | auto element = m_wire.elements_begin(); |
Alexander Afanasyev | c393217 | 2014-07-10 18:53:56 -0700 | [diff] [blame] | 241 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 242 | // Name |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 243 | if (element != m_wire.elements_end() && element->type() == tlv::Name) { |
| 244 | m_name.wireDecode(*element); |
| 245 | ++element; |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 246 | } |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 247 | else { |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | // Selectors? |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 252 | if (element != m_wire.elements_end() && element->type() == tlv::Selectors) { |
| 253 | m_selectors.wireDecode(*element); |
| 254 | ++element; |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 255 | } |
| 256 | else { |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 257 | m_selectors = Selectors(); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 258 | } |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 259 | |
| 260 | // Nonce |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 261 | if (element != m_wire.elements_end() && element->type() == tlv::Nonce) { |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 262 | uint32_t nonce = 0; |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 263 | if (element->value_size() != sizeof(nonce)) { |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 264 | BOOST_THROW_EXCEPTION(Error("Nonce element is malformed")); |
| 265 | } |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 266 | std::memcpy(&nonce, element->value(), sizeof(nonce)); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 267 | m_nonce = nonce; |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 268 | ++element; |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 269 | } |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 270 | else { |
| 271 | return false; |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 272 | } |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 273 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 274 | // InterestLifetime? |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 275 | if (element != m_wire.elements_end() && element->type() == tlv::InterestLifetime) { |
| 276 | m_interestLifetime = time::milliseconds(readNonNegativeInteger(*element)); |
| 277 | ++element; |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 278 | } |
| 279 | else { |
| 280 | m_interestLifetime = DEFAULT_INTEREST_LIFETIME; |
| 281 | } |
| 282 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 283 | // ForwardingHint? |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 284 | if (element != m_wire.elements_end() && element->type() == tlv::ForwardingHint) { |
| 285 | m_forwardingHint.wireDecode(*element, false); |
| 286 | ++element; |
Junxiao Shi | 9c154cb | 2017-07-07 22:14:54 +0000 | [diff] [blame] | 287 | } |
| 288 | else { |
| 289 | m_forwardingHint = DelegationList(); |
| 290 | } |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 291 | |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 292 | return element == m_wire.elements_end(); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | void |
| 296 | Interest::decode03() |
| 297 | { |
| 298 | // Interest ::= INTEREST-TYPE TLV-LENGTH |
| 299 | // Name |
| 300 | // CanBePrefix? |
| 301 | // MustBeFresh? |
| 302 | // ForwardingHint? |
| 303 | // Nonce? |
| 304 | // InterestLifetime? |
| 305 | // HopLimit? |
| 306 | // Parameters? |
| 307 | |
Junxiao Shi | 8b753a2 | 2018-10-24 01:51:40 +0000 | [diff] [blame^] | 308 | auto element = m_wire.elements_begin(); |
| 309 | if (element == m_wire.elements_end() || element->type() != tlv::Name) { |
| 310 | BOOST_THROW_EXCEPTION(Error("Name element is missing or out of order")); |
| 311 | } |
| 312 | m_name.wireDecode(*element); |
| 313 | if (m_name.empty()) { |
| 314 | BOOST_THROW_EXCEPTION(Error("Name has zero name components")); |
| 315 | } |
| 316 | int lastElement = 1; // last recognized element index, in spec order |
| 317 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 318 | m_selectors = Selectors().setMaxSuffixComponents(1); // CanBePrefix=0 |
| 319 | m_nonce.reset(); |
| 320 | m_interestLifetime = DEFAULT_INTEREST_LIFETIME; |
| 321 | m_forwardingHint = DelegationList(); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 322 | m_parameters = Block(); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 323 | |
Junxiao Shi | 8b753a2 | 2018-10-24 01:51:40 +0000 | [diff] [blame^] | 324 | for (++element; element != m_wire.elements_end(); ++element) { |
| 325 | switch (element->type()) { |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 326 | case tlv::CanBePrefix: { |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 327 | if (lastElement >= 2) { |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 328 | BOOST_THROW_EXCEPTION(Error("CanBePrefix element is out of order")); |
| 329 | } |
Junxiao Shi | 8b753a2 | 2018-10-24 01:51:40 +0000 | [diff] [blame^] | 330 | if (element->value_size() != 0) { |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 331 | BOOST_THROW_EXCEPTION(Error("CanBePrefix element has non-zero TLV-LENGTH")); |
| 332 | } |
| 333 | m_selectors.setMaxSuffixComponents(-1); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 334 | lastElement = 2; |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 335 | break; |
| 336 | } |
| 337 | case tlv::MustBeFresh: { |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 338 | if (lastElement >= 3) { |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 339 | BOOST_THROW_EXCEPTION(Error("MustBeFresh element is out of order")); |
| 340 | } |
Junxiao Shi | 8b753a2 | 2018-10-24 01:51:40 +0000 | [diff] [blame^] | 341 | if (element->value_size() != 0) { |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 342 | BOOST_THROW_EXCEPTION(Error("MustBeFresh element has non-zero TLV-LENGTH")); |
| 343 | } |
| 344 | m_selectors.setMustBeFresh(true); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 345 | lastElement = 3; |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 346 | break; |
| 347 | } |
| 348 | case tlv::ForwardingHint: { |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 349 | if (lastElement >= 4) { |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 350 | BOOST_THROW_EXCEPTION(Error("ForwardingHint element is out of order")); |
| 351 | } |
Junxiao Shi | 8b753a2 | 2018-10-24 01:51:40 +0000 | [diff] [blame^] | 352 | m_forwardingHint.wireDecode(*element); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 353 | lastElement = 4; |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 354 | break; |
| 355 | } |
| 356 | case tlv::Nonce: { |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 357 | if (lastElement >= 5) { |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 358 | BOOST_THROW_EXCEPTION(Error("Nonce element is out of order")); |
| 359 | } |
| 360 | uint32_t nonce = 0; |
Junxiao Shi | 8b753a2 | 2018-10-24 01:51:40 +0000 | [diff] [blame^] | 361 | if (element->value_size() != sizeof(nonce)) { |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 362 | BOOST_THROW_EXCEPTION(Error("Nonce element is malformed")); |
| 363 | } |
Junxiao Shi | 8b753a2 | 2018-10-24 01:51:40 +0000 | [diff] [blame^] | 364 | std::memcpy(&nonce, element->value(), sizeof(nonce)); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 365 | m_nonce = nonce; |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 366 | lastElement = 5; |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 367 | break; |
| 368 | } |
| 369 | case tlv::InterestLifetime: { |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 370 | if (lastElement >= 6) { |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 371 | BOOST_THROW_EXCEPTION(Error("InterestLifetime element is out of order")); |
| 372 | } |
Junxiao Shi | 8b753a2 | 2018-10-24 01:51:40 +0000 | [diff] [blame^] | 373 | m_interestLifetime = time::milliseconds(readNonNegativeInteger(*element)); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 374 | lastElement = 6; |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 375 | break; |
| 376 | } |
| 377 | case tlv::HopLimit: { |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 378 | if (lastElement >= 7) { |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 379 | break; // HopLimit is non-critical, ignore out-of-order appearance |
| 380 | } |
Junxiao Shi | 8b753a2 | 2018-10-24 01:51:40 +0000 | [diff] [blame^] | 381 | if (element->value_size() != 1) { |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 382 | BOOST_THROW_EXCEPTION(Error("HopLimit element is malformed")); |
| 383 | } |
| 384 | // TLV-VALUE is ignored |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 385 | lastElement = 7; |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 386 | break; |
| 387 | } |
| 388 | case tlv::Parameters: { |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 389 | if (lastElement >= 8) { |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 390 | BOOST_THROW_EXCEPTION(Error("Parameters element is out of order")); |
| 391 | } |
Junxiao Shi | 8b753a2 | 2018-10-24 01:51:40 +0000 | [diff] [blame^] | 392 | m_parameters = *element; |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 393 | lastElement = 8; |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 394 | break; |
| 395 | } |
| 396 | default: { |
Junxiao Shi | 8b753a2 | 2018-10-24 01:51:40 +0000 | [diff] [blame^] | 397 | if (tlv::isCriticalType(element->type())) { |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 398 | BOOST_THROW_EXCEPTION(Error("unrecognized element of critical type " + |
Junxiao Shi | 8b753a2 | 2018-10-24 01:51:40 +0000 | [diff] [blame^] | 399 | to_string(element->type()))); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 400 | } |
| 401 | break; |
| 402 | } |
| 403 | } |
| 404 | } |
Alexander Afanasyev | c393217 | 2014-07-10 18:53:56 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Davide Pesavento | a84f464 | 2017-08-23 16:14:51 -0400 | [diff] [blame] | 407 | std::string |
| 408 | Interest::toUri() const |
| 409 | { |
| 410 | std::ostringstream os; |
| 411 | os << *this; |
| 412 | return os.str(); |
| 413 | } |
| 414 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 415 | // ---- matching ---- |
| 416 | |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 417 | bool |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 418 | Interest::matchesName(const Name& name) const |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 419 | { |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 420 | if (name.size() < m_name.size()) |
| 421 | return false; |
| 422 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 423 | if (!m_name.isPrefixOf(name)) |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 424 | return false; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 425 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 426 | if (getMinSuffixComponents() >= 0 && |
Alexander Afanasyev | 3b70310 | 2014-06-13 17:01:14 -0700 | [diff] [blame] | 427 | // name must include implicit digest |
| 428 | !(name.size() - m_name.size() >= static_cast<size_t>(getMinSuffixComponents()))) |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 429 | return false; |
| 430 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 431 | if (getMaxSuffixComponents() >= 0 && |
Alexander Afanasyev | 3b70310 | 2014-06-13 17:01:14 -0700 | [diff] [blame] | 432 | // name must include implicit digest |
| 433 | !(name.size() - m_name.size() <= static_cast<size_t>(getMaxSuffixComponents()))) |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 434 | return false; |
| 435 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 436 | if (!getExclude().empty() && |
| 437 | name.size() > m_name.size() && |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 438 | getExclude().isExcluded(name[m_name.size()])) |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 439 | return false; |
| 440 | |
| 441 | return true; |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 444 | bool |
| 445 | Interest::matchesData(const Data& data) const |
| 446 | { |
Junxiao Shi | 42c2362 | 2014-07-03 00:55:11 -0700 | [diff] [blame] | 447 | size_t interestNameLength = m_name.size(); |
| 448 | const Name& dataName = data.getName(); |
| 449 | size_t fullNameLength = dataName.size() + 1; |
| 450 | |
| 451 | // check MinSuffixComponents |
| 452 | bool hasMinSuffixComponents = getMinSuffixComponents() >= 0; |
| 453 | size_t minSuffixComponents = hasMinSuffixComponents ? |
| 454 | static_cast<size_t>(getMinSuffixComponents()) : 0; |
| 455 | if (!(interestNameLength + minSuffixComponents <= fullNameLength)) |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 456 | return false; |
Junxiao Shi | 42c2362 | 2014-07-03 00:55:11 -0700 | [diff] [blame] | 457 | |
| 458 | // check MaxSuffixComponents |
| 459 | bool hasMaxSuffixComponents = getMaxSuffixComponents() >= 0; |
| 460 | if (hasMaxSuffixComponents && |
| 461 | !(interestNameLength + getMaxSuffixComponents() >= fullNameLength)) |
| 462 | return false; |
| 463 | |
| 464 | // check prefix |
| 465 | if (interestNameLength == fullNameLength) { |
Alexander Afanasyev | 56860f5 | 2014-11-07 11:51:17 -0800 | [diff] [blame] | 466 | if (m_name.get(-1).isImplicitSha256Digest()) { |
| 467 | if (m_name != data.getFullName()) |
Junxiao Shi | 42c2362 | 2014-07-03 00:55:11 -0700 | [diff] [blame] | 468 | return false; |
| 469 | } |
| 470 | else { |
| 471 | // Interest Name is same length as Data full Name, but last component isn't digest |
| 472 | // so there's no possibility of matching |
| 473 | return false; |
| 474 | } |
| 475 | } |
| 476 | else { |
| 477 | // Interest Name is a strict prefix of Data full Name |
| 478 | if (!m_name.isPrefixOf(dataName)) |
| 479 | return false; |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Junxiao Shi | 42c2362 | 2014-07-03 00:55:11 -0700 | [diff] [blame] | 482 | // check Exclude |
| 483 | // Exclude won't be violated if Interest Name is same as Data full Name |
| 484 | if (!getExclude().empty() && fullNameLength > interestNameLength) { |
| 485 | if (interestNameLength == fullNameLength - 1) { |
| 486 | // component to exclude is the digest |
| 487 | if (getExclude().isExcluded(data.getFullName().get(interestNameLength))) |
| 488 | return false; |
| 489 | // There's opportunity to inspect the Exclude filter and determine whether |
| 490 | // the digest would make a difference. |
Junxiao Shi | 4ffbb9d | 2018-03-31 17:16:35 +0000 | [diff] [blame] | 491 | // eg. "<GenericNameComponent>AA</GenericNameComponent><Any/>" doesn't exclude |
| 492 | // any digest - fullName not needed; |
| 493 | // "<Any/><GenericNameComponent>AA</GenericNameComponent>" and |
Junxiao Shi | 08d0708 | 2014-12-03 11:31:44 -0700 | [diff] [blame] | 494 | // "<Any/><ImplicitSha256DigestComponent>ffffffffffffffffffffffffffffffff |
| 495 | // </ImplicitSha256DigestComponent>" |
| 496 | // excludes all digests - fullName not needed; |
| 497 | // "<Any/><ImplicitSha256DigestComponent>80000000000000000000000000000000 |
| 498 | // </ImplicitSha256DigestComponent>" |
| 499 | // excludes some digests - fullName required |
Junxiao Shi | 42c2362 | 2014-07-03 00:55:11 -0700 | [diff] [blame] | 500 | // But Interests that contain the exact Data Name before digest and also |
| 501 | // contain Exclude filter is too rare to optimize for, so we request |
Junxiao Shi | 6824783 | 2017-07-03 22:06:49 +0000 | [diff] [blame] | 502 | // fullName no matter what's in the Exclude filter. |
Junxiao Shi | 42c2362 | 2014-07-03 00:55:11 -0700 | [diff] [blame] | 503 | } |
| 504 | else { |
| 505 | // component to exclude is not the digest |
| 506 | if (getExclude().isExcluded(dataName.get(interestNameLength))) |
| 507 | return false; |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | // check PublisherPublicKeyLocator |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 512 | const KeyLocator& publisherPublicKeyLocator = this->getPublisherPublicKeyLocator(); |
| 513 | if (!publisherPublicKeyLocator.empty()) { |
| 514 | const Signature& signature = data.getSignature(); |
| 515 | const Block& signatureInfo = signature.getInfo(); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 516 | Block::element_const_iterator it = signatureInfo.find(tlv::KeyLocator); |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 517 | if (it == signatureInfo.elements_end()) { |
| 518 | return false; |
| 519 | } |
| 520 | if (publisherPublicKeyLocator.wireEncode() != *it) { |
| 521 | return false; |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | return true; |
| 526 | } |
| 527 | |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 528 | bool |
| 529 | Interest::matchesInterest(const Interest& other) const |
| 530 | { |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 531 | /// @todo #3162 match ForwardingHint field |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 532 | return (this->getName() == other.getName() && |
| 533 | this->getSelectors() == other.getSelectors()); |
| 534 | } |
| 535 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 536 | // ---- field accessors ---- |
| 537 | |
| 538 | uint32_t |
| 539 | Interest::getNonce() const |
| 540 | { |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 541 | if (!m_nonce) { |
| 542 | m_nonce = random::generateWord32(); |
Junxiao Shi | c2ac5d2 | 2017-07-17 22:18:31 +0000 | [diff] [blame] | 543 | } |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 544 | return *m_nonce; |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | Interest& |
| 548 | Interest::setNonce(uint32_t nonce) |
| 549 | { |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 550 | m_nonce = nonce; |
| 551 | m_wire.reset(); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 552 | return *this; |
| 553 | } |
| 554 | |
| 555 | void |
| 556 | Interest::refreshNonce() |
| 557 | { |
| 558 | if (!hasNonce()) |
| 559 | return; |
| 560 | |
| 561 | uint32_t oldNonce = getNonce(); |
| 562 | uint32_t newNonce = oldNonce; |
| 563 | while (newNonce == oldNonce) |
| 564 | newNonce = random::generateWord32(); |
| 565 | |
| 566 | setNonce(newNonce); |
| 567 | } |
| 568 | |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 569 | Interest& |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 570 | Interest::setInterestLifetime(time::milliseconds lifetime) |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 571 | { |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 572 | if (lifetime < time::milliseconds::zero()) { |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 573 | BOOST_THROW_EXCEPTION(std::invalid_argument("InterestLifetime must be >= 0")); |
| 574 | } |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 575 | m_interestLifetime = lifetime; |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 576 | m_wire.reset(); |
| 577 | return *this; |
| 578 | } |
| 579 | |
Junxiao Shi | 9c154cb | 2017-07-07 22:14:54 +0000 | [diff] [blame] | 580 | Interest& |
| 581 | Interest::setForwardingHint(const DelegationList& value) |
| 582 | { |
| 583 | m_forwardingHint = value; |
| 584 | m_wire.reset(); |
| 585 | return *this; |
| 586 | } |
| 587 | |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 588 | Interest& |
| 589 | Interest::setParameters(const Block& parameters) |
| 590 | { |
| 591 | if (parameters.type() == tlv::Parameters) { |
| 592 | m_parameters = parameters; |
| 593 | } |
| 594 | else { |
| 595 | m_parameters = Block(tlv::Parameters, parameters); |
| 596 | } |
| 597 | m_wire.reset(); |
| 598 | return *this; |
| 599 | } |
| 600 | |
| 601 | Interest& |
| 602 | Interest::setParameters(const uint8_t* buffer, size_t bufferSize) |
| 603 | { |
| 604 | m_parameters = makeBinaryBlock(tlv::Parameters, buffer, bufferSize); |
| 605 | m_wire.reset(); |
| 606 | return *this; |
| 607 | } |
| 608 | |
| 609 | Interest& |
| 610 | Interest::setParameters(ConstBufferPtr buffer) |
| 611 | { |
| 612 | m_parameters = Block(tlv::Parameters, std::move(buffer)); |
| 613 | m_wire.reset(); |
| 614 | return *this; |
| 615 | } |
| 616 | |
| 617 | Interest& |
| 618 | Interest::unsetParameters() |
| 619 | { |
| 620 | m_parameters = Block(); |
| 621 | m_wire.reset(); |
| 622 | return *this; |
| 623 | } |
| 624 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 625 | // ---- operators ---- |
| 626 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 627 | bool |
| 628 | operator==(const Interest& lhs, const Interest& rhs) |
| 629 | { |
| 630 | bool wasCanBePrefixSetOnLhs = lhs.m_isCanBePrefixSet; |
| 631 | bool wasCanBePrefixSetOnRhs = rhs.m_isCanBePrefixSet; |
| 632 | lhs.m_isCanBePrefixSet = true; |
| 633 | rhs.m_isCanBePrefixSet = true; |
| 634 | BOOST_SCOPE_EXIT_ALL(&) { |
| 635 | lhs.m_isCanBePrefixSet = wasCanBePrefixSetOnLhs; |
| 636 | rhs.m_isCanBePrefixSet = wasCanBePrefixSetOnRhs; |
| 637 | }; |
| 638 | |
| 639 | return lhs.wireEncode() == rhs.wireEncode(); |
| 640 | } |
| 641 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 642 | std::ostream& |
| 643 | operator<<(std::ostream& os, const Interest& interest) |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 644 | { |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 645 | os << interest.getName(); |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 646 | |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 647 | char delim = '?'; |
| 648 | |
| 649 | if (interest.getMinSuffixComponents() >= 0) { |
| 650 | os << delim << "ndn.MinSuffixComponents=" << interest.getMinSuffixComponents(); |
| 651 | delim = '&'; |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 652 | } |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 653 | if (interest.getMaxSuffixComponents() >= 0) { |
| 654 | os << delim << "ndn.MaxSuffixComponents=" << interest.getMaxSuffixComponents(); |
| 655 | delim = '&'; |
Jeff Thompson | 37527d6 | 2013-08-21 11:15:54 -0700 | [diff] [blame] | 656 | } |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 657 | if (interest.getChildSelector() != DEFAULT_CHILD_SELECTOR) { |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 658 | os << delim << "ndn.ChildSelector=" << interest.getChildSelector(); |
| 659 | delim = '&'; |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 660 | } |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 661 | if (interest.getMustBeFresh()) { |
| 662 | os << delim << "ndn.MustBeFresh=" << interest.getMustBeFresh(); |
| 663 | delim = '&'; |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 664 | } |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 665 | if (interest.getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) { |
Alexander Afanasyev | a0c5f83 | 2014-06-19 13:27:56 -0700 | [diff] [blame] | 666 | os << delim << "ndn.InterestLifetime=" << interest.getInterestLifetime().count(); |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 667 | delim = '&'; |
| 668 | } |
| 669 | |
Alexander Afanasyev | e881e93 | 2014-06-08 14:47:03 +0300 | [diff] [blame] | 670 | if (interest.hasNonce()) { |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 671 | os << delim << "ndn.Nonce=" << interest.getNonce(); |
| 672 | delim = '&'; |
| 673 | } |
| 674 | if (!interest.getExclude().empty()) { |
| 675 | os << delim << "ndn.Exclude=" << interest.getExclude(); |
| 676 | delim = '&'; |
| 677 | } |
| 678 | |
| 679 | return os; |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 680 | } |
| 681 | |
Junxiao Shi | 08d0708 | 2014-12-03 11:31:44 -0700 | [diff] [blame] | 682 | } // namespace ndn |