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 | |
| 308 | bool hasName = false; |
| 309 | m_selectors = Selectors().setMaxSuffixComponents(1); // CanBePrefix=0 |
| 310 | m_nonce.reset(); |
| 311 | m_interestLifetime = DEFAULT_INTEREST_LIFETIME; |
| 312 | m_forwardingHint = DelegationList(); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 313 | m_parameters = Block(); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 314 | |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 315 | int lastElement = 0; // last recognized element index, in spec order |
| 316 | for (const Block& element : m_wire.elements()) { |
| 317 | switch (element.type()) { |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 318 | case tlv::Name: { |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 319 | if (lastElement >= 1) { |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 320 | BOOST_THROW_EXCEPTION(Error("Name element is out of order")); |
| 321 | } |
| 322 | hasName = true; |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 323 | m_name.wireDecode(element); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 324 | if (m_name.empty()) { |
| 325 | BOOST_THROW_EXCEPTION(Error("Name has zero name components")); |
| 326 | } |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 327 | lastElement = 1; |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 328 | break; |
| 329 | } |
| 330 | case tlv::CanBePrefix: { |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 331 | if (lastElement >= 2) { |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 332 | BOOST_THROW_EXCEPTION(Error("CanBePrefix element is out of order")); |
| 333 | } |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 334 | if (element.value_size() != 0) { |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 335 | BOOST_THROW_EXCEPTION(Error("CanBePrefix element has non-zero TLV-LENGTH")); |
| 336 | } |
| 337 | m_selectors.setMaxSuffixComponents(-1); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 338 | lastElement = 2; |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 339 | break; |
| 340 | } |
| 341 | case tlv::MustBeFresh: { |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 342 | if (lastElement >= 3) { |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 343 | BOOST_THROW_EXCEPTION(Error("MustBeFresh element is out of order")); |
| 344 | } |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 345 | if (element.value_size() != 0) { |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 346 | BOOST_THROW_EXCEPTION(Error("MustBeFresh element has non-zero TLV-LENGTH")); |
| 347 | } |
| 348 | m_selectors.setMustBeFresh(true); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 349 | lastElement = 3; |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 350 | break; |
| 351 | } |
| 352 | case tlv::ForwardingHint: { |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 353 | if (lastElement >= 4) { |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 354 | BOOST_THROW_EXCEPTION(Error("ForwardingHint element is out of order")); |
| 355 | } |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 356 | m_forwardingHint.wireDecode(element); |
| 357 | lastElement = 4; |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 358 | break; |
| 359 | } |
| 360 | case tlv::Nonce: { |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 361 | if (lastElement >= 5) { |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 362 | BOOST_THROW_EXCEPTION(Error("Nonce element is out of order")); |
| 363 | } |
| 364 | uint32_t nonce = 0; |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 365 | if (element.value_size() != sizeof(nonce)) { |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 366 | BOOST_THROW_EXCEPTION(Error("Nonce element is malformed")); |
| 367 | } |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 368 | std::memcpy(&nonce, element.value(), sizeof(nonce)); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 369 | m_nonce = nonce; |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 370 | lastElement = 5; |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 371 | break; |
| 372 | } |
| 373 | case tlv::InterestLifetime: { |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 374 | if (lastElement >= 6) { |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 375 | BOOST_THROW_EXCEPTION(Error("InterestLifetime element is out of order")); |
| 376 | } |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 377 | m_interestLifetime = time::milliseconds(readNonNegativeInteger(element)); |
| 378 | lastElement = 6; |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 379 | break; |
| 380 | } |
| 381 | case tlv::HopLimit: { |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 382 | if (lastElement >= 7) { |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 383 | break; // HopLimit is non-critical, ignore out-of-order appearance |
| 384 | } |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 385 | if (element.value_size() != 1) { |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 386 | BOOST_THROW_EXCEPTION(Error("HopLimit element is malformed")); |
| 387 | } |
| 388 | // TLV-VALUE is ignored |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 389 | lastElement = 7; |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 390 | break; |
| 391 | } |
| 392 | case tlv::Parameters: { |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 393 | if (lastElement >= 8) { |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 394 | BOOST_THROW_EXCEPTION(Error("Parameters element is out of order")); |
| 395 | } |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 396 | m_parameters = element; |
| 397 | lastElement = 8; |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 398 | break; |
| 399 | } |
| 400 | default: { |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 401 | if (tlv::isCriticalType(element.type())) { |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 402 | BOOST_THROW_EXCEPTION(Error("unrecognized element of critical type " + |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 403 | to_string(element.type()))); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 404 | } |
| 405 | break; |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | if (!hasName) { |
| 411 | BOOST_THROW_EXCEPTION(Error("Name element is missing")); |
| 412 | } |
Alexander Afanasyev | c393217 | 2014-07-10 18:53:56 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Davide Pesavento | a84f464 | 2017-08-23 16:14:51 -0400 | [diff] [blame] | 415 | std::string |
| 416 | Interest::toUri() const |
| 417 | { |
| 418 | std::ostringstream os; |
| 419 | os << *this; |
| 420 | return os.str(); |
| 421 | } |
| 422 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 423 | // ---- matching ---- |
| 424 | |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 425 | bool |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 426 | Interest::matchesName(const Name& name) const |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 427 | { |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 428 | if (name.size() < m_name.size()) |
| 429 | return false; |
| 430 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 431 | if (!m_name.isPrefixOf(name)) |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 432 | return false; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 433 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 434 | if (getMinSuffixComponents() >= 0 && |
Alexander Afanasyev | 3b70310 | 2014-06-13 17:01:14 -0700 | [diff] [blame] | 435 | // name must include implicit digest |
| 436 | !(name.size() - m_name.size() >= static_cast<size_t>(getMinSuffixComponents()))) |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 437 | return false; |
| 438 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 439 | if (getMaxSuffixComponents() >= 0 && |
Alexander Afanasyev | 3b70310 | 2014-06-13 17:01:14 -0700 | [diff] [blame] | 440 | // name must include implicit digest |
| 441 | !(name.size() - m_name.size() <= static_cast<size_t>(getMaxSuffixComponents()))) |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 442 | return false; |
| 443 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 444 | if (!getExclude().empty() && |
| 445 | name.size() > m_name.size() && |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 446 | getExclude().isExcluded(name[m_name.size()])) |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 447 | return false; |
| 448 | |
| 449 | return true; |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 452 | bool |
| 453 | Interest::matchesData(const Data& data) const |
| 454 | { |
Junxiao Shi | 42c2362 | 2014-07-03 00:55:11 -0700 | [diff] [blame] | 455 | size_t interestNameLength = m_name.size(); |
| 456 | const Name& dataName = data.getName(); |
| 457 | size_t fullNameLength = dataName.size() + 1; |
| 458 | |
| 459 | // check MinSuffixComponents |
| 460 | bool hasMinSuffixComponents = getMinSuffixComponents() >= 0; |
| 461 | size_t minSuffixComponents = hasMinSuffixComponents ? |
| 462 | static_cast<size_t>(getMinSuffixComponents()) : 0; |
| 463 | if (!(interestNameLength + minSuffixComponents <= fullNameLength)) |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 464 | return false; |
Junxiao Shi | 42c2362 | 2014-07-03 00:55:11 -0700 | [diff] [blame] | 465 | |
| 466 | // check MaxSuffixComponents |
| 467 | bool hasMaxSuffixComponents = getMaxSuffixComponents() >= 0; |
| 468 | if (hasMaxSuffixComponents && |
| 469 | !(interestNameLength + getMaxSuffixComponents() >= fullNameLength)) |
| 470 | return false; |
| 471 | |
| 472 | // check prefix |
| 473 | if (interestNameLength == fullNameLength) { |
Alexander Afanasyev | 56860f5 | 2014-11-07 11:51:17 -0800 | [diff] [blame] | 474 | if (m_name.get(-1).isImplicitSha256Digest()) { |
| 475 | if (m_name != data.getFullName()) |
Junxiao Shi | 42c2362 | 2014-07-03 00:55:11 -0700 | [diff] [blame] | 476 | return false; |
| 477 | } |
| 478 | else { |
| 479 | // Interest Name is same length as Data full Name, but last component isn't digest |
| 480 | // so there's no possibility of matching |
| 481 | return false; |
| 482 | } |
| 483 | } |
| 484 | else { |
| 485 | // Interest Name is a strict prefix of Data full Name |
| 486 | if (!m_name.isPrefixOf(dataName)) |
| 487 | return false; |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Junxiao Shi | 42c2362 | 2014-07-03 00:55:11 -0700 | [diff] [blame] | 490 | // check Exclude |
| 491 | // Exclude won't be violated if Interest Name is same as Data full Name |
| 492 | if (!getExclude().empty() && fullNameLength > interestNameLength) { |
| 493 | if (interestNameLength == fullNameLength - 1) { |
| 494 | // component to exclude is the digest |
| 495 | if (getExclude().isExcluded(data.getFullName().get(interestNameLength))) |
| 496 | return false; |
| 497 | // There's opportunity to inspect the Exclude filter and determine whether |
| 498 | // the digest would make a difference. |
Junxiao Shi | 4ffbb9d | 2018-03-31 17:16:35 +0000 | [diff] [blame] | 499 | // eg. "<GenericNameComponent>AA</GenericNameComponent><Any/>" doesn't exclude |
| 500 | // any digest - fullName not needed; |
| 501 | // "<Any/><GenericNameComponent>AA</GenericNameComponent>" and |
Junxiao Shi | 08d0708 | 2014-12-03 11:31:44 -0700 | [diff] [blame] | 502 | // "<Any/><ImplicitSha256DigestComponent>ffffffffffffffffffffffffffffffff |
| 503 | // </ImplicitSha256DigestComponent>" |
| 504 | // excludes all digests - fullName not needed; |
| 505 | // "<Any/><ImplicitSha256DigestComponent>80000000000000000000000000000000 |
| 506 | // </ImplicitSha256DigestComponent>" |
| 507 | // excludes some digests - fullName required |
Junxiao Shi | 42c2362 | 2014-07-03 00:55:11 -0700 | [diff] [blame] | 508 | // But Interests that contain the exact Data Name before digest and also |
| 509 | // contain Exclude filter is too rare to optimize for, so we request |
Junxiao Shi | 6824783 | 2017-07-03 22:06:49 +0000 | [diff] [blame] | 510 | // fullName no matter what's in the Exclude filter. |
Junxiao Shi | 42c2362 | 2014-07-03 00:55:11 -0700 | [diff] [blame] | 511 | } |
| 512 | else { |
| 513 | // component to exclude is not the digest |
| 514 | if (getExclude().isExcluded(dataName.get(interestNameLength))) |
| 515 | return false; |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | // check PublisherPublicKeyLocator |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 520 | const KeyLocator& publisherPublicKeyLocator = this->getPublisherPublicKeyLocator(); |
| 521 | if (!publisherPublicKeyLocator.empty()) { |
| 522 | const Signature& signature = data.getSignature(); |
| 523 | const Block& signatureInfo = signature.getInfo(); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 524 | Block::element_const_iterator it = signatureInfo.find(tlv::KeyLocator); |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 525 | if (it == signatureInfo.elements_end()) { |
| 526 | return false; |
| 527 | } |
| 528 | if (publisherPublicKeyLocator.wireEncode() != *it) { |
| 529 | return false; |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | return true; |
| 534 | } |
| 535 | |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 536 | bool |
| 537 | Interest::matchesInterest(const Interest& other) const |
| 538 | { |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 539 | /// @todo #3162 match ForwardingHint field |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 540 | return (this->getName() == other.getName() && |
| 541 | this->getSelectors() == other.getSelectors()); |
| 542 | } |
| 543 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 544 | // ---- field accessors ---- |
| 545 | |
| 546 | uint32_t |
| 547 | Interest::getNonce() const |
| 548 | { |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 549 | if (!m_nonce) { |
| 550 | m_nonce = random::generateWord32(); |
Junxiao Shi | c2ac5d2 | 2017-07-17 22:18:31 +0000 | [diff] [blame] | 551 | } |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 552 | return *m_nonce; |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | Interest& |
| 556 | Interest::setNonce(uint32_t nonce) |
| 557 | { |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 558 | m_nonce = nonce; |
| 559 | m_wire.reset(); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 560 | return *this; |
| 561 | } |
| 562 | |
| 563 | void |
| 564 | Interest::refreshNonce() |
| 565 | { |
| 566 | if (!hasNonce()) |
| 567 | return; |
| 568 | |
| 569 | uint32_t oldNonce = getNonce(); |
| 570 | uint32_t newNonce = oldNonce; |
| 571 | while (newNonce == oldNonce) |
| 572 | newNonce = random::generateWord32(); |
| 573 | |
| 574 | setNonce(newNonce); |
| 575 | } |
| 576 | |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 577 | Interest& |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 578 | Interest::setInterestLifetime(time::milliseconds lifetime) |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 579 | { |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 580 | if (lifetime < time::milliseconds::zero()) { |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 581 | BOOST_THROW_EXCEPTION(std::invalid_argument("InterestLifetime must be >= 0")); |
| 582 | } |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 583 | m_interestLifetime = lifetime; |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 584 | m_wire.reset(); |
| 585 | return *this; |
| 586 | } |
| 587 | |
Junxiao Shi | 9c154cb | 2017-07-07 22:14:54 +0000 | [diff] [blame] | 588 | Interest& |
| 589 | Interest::setForwardingHint(const DelegationList& value) |
| 590 | { |
| 591 | m_forwardingHint = value; |
| 592 | m_wire.reset(); |
| 593 | return *this; |
| 594 | } |
| 595 | |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 596 | Interest& |
| 597 | Interest::setParameters(const Block& parameters) |
| 598 | { |
| 599 | if (parameters.type() == tlv::Parameters) { |
| 600 | m_parameters = parameters; |
| 601 | } |
| 602 | else { |
| 603 | m_parameters = Block(tlv::Parameters, parameters); |
| 604 | } |
| 605 | m_wire.reset(); |
| 606 | return *this; |
| 607 | } |
| 608 | |
| 609 | Interest& |
| 610 | Interest::setParameters(const uint8_t* buffer, size_t bufferSize) |
| 611 | { |
| 612 | m_parameters = makeBinaryBlock(tlv::Parameters, buffer, bufferSize); |
| 613 | m_wire.reset(); |
| 614 | return *this; |
| 615 | } |
| 616 | |
| 617 | Interest& |
| 618 | Interest::setParameters(ConstBufferPtr buffer) |
| 619 | { |
| 620 | m_parameters = Block(tlv::Parameters, std::move(buffer)); |
| 621 | m_wire.reset(); |
| 622 | return *this; |
| 623 | } |
| 624 | |
| 625 | Interest& |
| 626 | Interest::unsetParameters() |
| 627 | { |
| 628 | m_parameters = Block(); |
| 629 | m_wire.reset(); |
| 630 | return *this; |
| 631 | } |
| 632 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 633 | // ---- operators ---- |
| 634 | |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 635 | bool |
| 636 | operator==(const Interest& lhs, const Interest& rhs) |
| 637 | { |
| 638 | bool wasCanBePrefixSetOnLhs = lhs.m_isCanBePrefixSet; |
| 639 | bool wasCanBePrefixSetOnRhs = rhs.m_isCanBePrefixSet; |
| 640 | lhs.m_isCanBePrefixSet = true; |
| 641 | rhs.m_isCanBePrefixSet = true; |
| 642 | BOOST_SCOPE_EXIT_ALL(&) { |
| 643 | lhs.m_isCanBePrefixSet = wasCanBePrefixSetOnLhs; |
| 644 | rhs.m_isCanBePrefixSet = wasCanBePrefixSetOnRhs; |
| 645 | }; |
| 646 | |
| 647 | return lhs.wireEncode() == rhs.wireEncode(); |
| 648 | } |
| 649 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 650 | std::ostream& |
| 651 | operator<<(std::ostream& os, const Interest& interest) |
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 | os << interest.getName(); |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 654 | |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 655 | char delim = '?'; |
| 656 | |
| 657 | if (interest.getMinSuffixComponents() >= 0) { |
| 658 | os << delim << "ndn.MinSuffixComponents=" << interest.getMinSuffixComponents(); |
| 659 | delim = '&'; |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 660 | } |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 661 | if (interest.getMaxSuffixComponents() >= 0) { |
| 662 | os << delim << "ndn.MaxSuffixComponents=" << interest.getMaxSuffixComponents(); |
| 663 | delim = '&'; |
Jeff Thompson | 37527d6 | 2013-08-21 11:15:54 -0700 | [diff] [blame] | 664 | } |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 665 | if (interest.getChildSelector() != DEFAULT_CHILD_SELECTOR) { |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 666 | os << delim << "ndn.ChildSelector=" << interest.getChildSelector(); |
| 667 | delim = '&'; |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 668 | } |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 669 | if (interest.getMustBeFresh()) { |
| 670 | os << delim << "ndn.MustBeFresh=" << interest.getMustBeFresh(); |
| 671 | delim = '&'; |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 672 | } |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 673 | if (interest.getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) { |
Alexander Afanasyev | a0c5f83 | 2014-06-19 13:27:56 -0700 | [diff] [blame] | 674 | os << delim << "ndn.InterestLifetime=" << interest.getInterestLifetime().count(); |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 675 | delim = '&'; |
| 676 | } |
| 677 | |
Alexander Afanasyev | e881e93 | 2014-06-08 14:47:03 +0300 | [diff] [blame] | 678 | if (interest.hasNonce()) { |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 679 | os << delim << "ndn.Nonce=" << interest.getNonce(); |
| 680 | delim = '&'; |
| 681 | } |
| 682 | if (!interest.getExclude().empty()) { |
| 683 | os << delim << "ndn.Exclude=" << interest.getExclude(); |
| 684 | delim = '&'; |
| 685 | } |
| 686 | |
| 687 | return os; |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 688 | } |
| 689 | |
Junxiao Shi | 08d0708 | 2014-12-03 11:31:44 -0700 | [diff] [blame] | 690 | } // namespace ndn |