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 | /* |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 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 | |
Davide Pesavento | e178989 | 2017-02-26 15:50:52 -0500 | [diff] [blame] | 26 | #include <cstring> |
Davide Pesavento | a84f464 | 2017-08-23 16:14:51 -0400 | [diff] [blame^] | 27 | #include <sstream> |
Davide Pesavento | e178989 | 2017-02-26 15:50:52 -0500 | [diff] [blame] | 28 | |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 29 | namespace ndn { |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 30 | |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 31 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Interest>)); |
| 32 | BOOST_CONCEPT_ASSERT((WireEncodable<Interest>)); |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 33 | BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<Interest>)); |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 34 | BOOST_CONCEPT_ASSERT((WireDecodable<Interest>)); |
| 35 | static_assert(std::is_base_of<tlv::Error, Interest::Error>::value, |
| 36 | "Interest::Error must inherit from tlv::Error"); |
| 37 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 38 | Interest::Interest(const Name& name, time::milliseconds interestLifetime) |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 39 | : m_name(name) |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 40 | , m_interestLifetime(interestLifetime) |
| 41 | { |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 42 | if (interestLifetime < time::milliseconds::zero()) { |
| 43 | BOOST_THROW_EXCEPTION(std::invalid_argument("InterestLifetime must be >= 0")); |
| 44 | } |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 47 | Interest::Interest(const Block& wire) |
| 48 | { |
| 49 | wireDecode(wire); |
| 50 | } |
| 51 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 52 | // ---- encode and decode ---- |
Alexander Afanasyev | 840139f | 2013-12-28 15:02:50 -0800 | [diff] [blame] | 53 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 54 | template<encoding::Tag TAG> |
| 55 | size_t |
| 56 | Interest::wireEncode(EncodingImpl<TAG>& encoder) const |
| 57 | { |
| 58 | size_t totalLength = 0; |
| 59 | |
| 60 | // Interest ::= INTEREST-TYPE TLV-LENGTH |
| 61 | // Name |
| 62 | // Selectors? |
| 63 | // Nonce |
| 64 | // InterestLifetime? |
Junxiao Shi | 9c154cb | 2017-07-07 22:14:54 +0000 | [diff] [blame] | 65 | // ForwardingHint? |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 66 | |
| 67 | // (reverse encoding) |
| 68 | |
Junxiao Shi | 9c154cb | 2017-07-07 22:14:54 +0000 | [diff] [blame] | 69 | // ForwardingHint |
| 70 | if (m_forwardingHint.size() > 0) { |
| 71 | totalLength += m_forwardingHint.wireEncode(encoder); |
| 72 | } |
| 73 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 74 | // InterestLifetime |
| 75 | if (getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) { |
| 76 | totalLength += prependNonNegativeIntegerBlock(encoder, |
| 77 | tlv::InterestLifetime, |
| 78 | getInterestLifetime().count()); |
| 79 | } |
| 80 | |
| 81 | // Nonce |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 82 | uint32_t nonce = this->getNonce(); // assigns random Nonce if needed |
| 83 | totalLength += encoder.prependByteArray(reinterpret_cast<uint8_t*>(&nonce), sizeof(nonce)); |
| 84 | totalLength += encoder.prependVarNumber(sizeof(nonce)); |
| 85 | totalLength += encoder.prependVarNumber(tlv::Nonce); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 86 | |
| 87 | // Selectors |
| 88 | if (hasSelectors()) { |
| 89 | totalLength += getSelectors().wireEncode(encoder); |
| 90 | } |
| 91 | |
| 92 | // Name |
| 93 | totalLength += getName().wireEncode(encoder); |
| 94 | |
| 95 | totalLength += encoder.prependVarNumber(totalLength); |
| 96 | totalLength += encoder.prependVarNumber(tlv::Interest); |
| 97 | return totalLength; |
Alexander Afanasyev | 840139f | 2013-12-28 15:02:50 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 100 | template size_t |
| 101 | Interest::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const; |
| 102 | |
| 103 | template size_t |
| 104 | Interest::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const; |
| 105 | |
| 106 | const Block& |
| 107 | Interest::wireEncode() const |
Alexander Afanasyev | e881e93 | 2014-06-08 14:47:03 +0300 | [diff] [blame] | 108 | { |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 109 | if (m_wire.hasWire()) |
| 110 | return m_wire; |
| 111 | |
| 112 | EncodingEstimator estimator; |
| 113 | size_t estimatedSize = wireEncode(estimator); |
| 114 | |
| 115 | EncodingBuffer buffer(estimatedSize, 0); |
| 116 | wireEncode(buffer); |
| 117 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 118 | const_cast<Interest*>(this)->wireDecode(buffer.block()); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 119 | return m_wire; |
Alexander Afanasyev | e881e93 | 2014-06-08 14:47:03 +0300 | [diff] [blame] | 120 | } |
Alexander Afanasyev | 840139f | 2013-12-28 15:02:50 -0800 | [diff] [blame] | 121 | |
Alexander Afanasyev | c393217 | 2014-07-10 18:53:56 -0700 | [diff] [blame] | 122 | void |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 123 | Interest::wireDecode(const Block& wire) |
Alexander Afanasyev | c393217 | 2014-07-10 18:53:56 -0700 | [diff] [blame] | 124 | { |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 125 | m_wire = wire; |
| 126 | m_wire.parse(); |
Alexander Afanasyev | c393217 | 2014-07-10 18:53:56 -0700 | [diff] [blame] | 127 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 128 | if (m_wire.type() != tlv::Interest) |
| 129 | BOOST_THROW_EXCEPTION(Error("Unexpected TLV number when decoding Interest")); |
Alexander Afanasyev | c393217 | 2014-07-10 18:53:56 -0700 | [diff] [blame] | 130 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 131 | // Name |
| 132 | m_name.wireDecode(m_wire.get(tlv::Name)); |
| 133 | |
| 134 | // Selectors |
| 135 | Block::element_const_iterator val = m_wire.find(tlv::Selectors); |
| 136 | if (val != m_wire.elements_end()) { |
| 137 | m_selectors.wireDecode(*val); |
| 138 | } |
| 139 | else |
| 140 | m_selectors = Selectors(); |
| 141 | |
| 142 | // Nonce |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 143 | val = m_wire.find(tlv::Nonce); |
| 144 | if (val == m_wire.elements_end()) { |
| 145 | BOOST_THROW_EXCEPTION(Error("Nonce element is missing")); |
| 146 | } |
| 147 | uint32_t nonce = 0; |
| 148 | if (val->value_size() != sizeof(nonce)) { |
| 149 | BOOST_THROW_EXCEPTION(Error("Nonce element is malformed")); |
| 150 | } |
| 151 | std::memcpy(&nonce, val->value(), sizeof(nonce)); |
| 152 | m_nonce = nonce; |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 153 | |
| 154 | // InterestLifetime |
| 155 | val = m_wire.find(tlv::InterestLifetime); |
| 156 | if (val != m_wire.elements_end()) { |
| 157 | m_interestLifetime = time::milliseconds(readNonNegativeInteger(*val)); |
| 158 | } |
| 159 | else { |
| 160 | m_interestLifetime = DEFAULT_INTEREST_LIFETIME; |
| 161 | } |
| 162 | |
Junxiao Shi | 9c154cb | 2017-07-07 22:14:54 +0000 | [diff] [blame] | 163 | // ForwardingHint |
| 164 | val = m_wire.find(tlv::ForwardingHint); |
| 165 | if (val != m_wire.elements_end()) { |
| 166 | m_forwardingHint.wireDecode(*val, false); |
| 167 | } |
| 168 | else { |
| 169 | m_forwardingHint = DelegationList(); |
| 170 | } |
Alexander Afanasyev | c393217 | 2014-07-10 18:53:56 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Davide Pesavento | a84f464 | 2017-08-23 16:14:51 -0400 | [diff] [blame^] | 173 | std::string |
| 174 | Interest::toUri() const |
| 175 | { |
| 176 | std::ostringstream os; |
| 177 | os << *this; |
| 178 | return os.str(); |
| 179 | } |
| 180 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 181 | // ---- matching ---- |
| 182 | |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 183 | bool |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 184 | Interest::matchesName(const Name& name) const |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 185 | { |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 186 | if (name.size() < m_name.size()) |
| 187 | return false; |
| 188 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 189 | if (!m_name.isPrefixOf(name)) |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 190 | return false; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 191 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 192 | if (getMinSuffixComponents() >= 0 && |
Alexander Afanasyev | 3b70310 | 2014-06-13 17:01:14 -0700 | [diff] [blame] | 193 | // name must include implicit digest |
| 194 | !(name.size() - m_name.size() >= static_cast<size_t>(getMinSuffixComponents()))) |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 195 | return false; |
| 196 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 197 | if (getMaxSuffixComponents() >= 0 && |
Alexander Afanasyev | 3b70310 | 2014-06-13 17:01:14 -0700 | [diff] [blame] | 198 | // name must include implicit digest |
| 199 | !(name.size() - m_name.size() <= static_cast<size_t>(getMaxSuffixComponents()))) |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 200 | return false; |
| 201 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 202 | if (!getExclude().empty() && |
| 203 | name.size() > m_name.size() && |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 204 | getExclude().isExcluded(name[m_name.size()])) |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 205 | return false; |
| 206 | |
| 207 | return true; |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 210 | bool |
| 211 | Interest::matchesData(const Data& data) const |
| 212 | { |
Junxiao Shi | 42c2362 | 2014-07-03 00:55:11 -0700 | [diff] [blame] | 213 | size_t interestNameLength = m_name.size(); |
| 214 | const Name& dataName = data.getName(); |
| 215 | size_t fullNameLength = dataName.size() + 1; |
| 216 | |
| 217 | // check MinSuffixComponents |
| 218 | bool hasMinSuffixComponents = getMinSuffixComponents() >= 0; |
| 219 | size_t minSuffixComponents = hasMinSuffixComponents ? |
| 220 | static_cast<size_t>(getMinSuffixComponents()) : 0; |
| 221 | if (!(interestNameLength + minSuffixComponents <= fullNameLength)) |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 222 | return false; |
Junxiao Shi | 42c2362 | 2014-07-03 00:55:11 -0700 | [diff] [blame] | 223 | |
| 224 | // check MaxSuffixComponents |
| 225 | bool hasMaxSuffixComponents = getMaxSuffixComponents() >= 0; |
| 226 | if (hasMaxSuffixComponents && |
| 227 | !(interestNameLength + getMaxSuffixComponents() >= fullNameLength)) |
| 228 | return false; |
| 229 | |
| 230 | // check prefix |
| 231 | if (interestNameLength == fullNameLength) { |
Alexander Afanasyev | 56860f5 | 2014-11-07 11:51:17 -0800 | [diff] [blame] | 232 | if (m_name.get(-1).isImplicitSha256Digest()) { |
| 233 | if (m_name != data.getFullName()) |
Junxiao Shi | 42c2362 | 2014-07-03 00:55:11 -0700 | [diff] [blame] | 234 | return false; |
| 235 | } |
| 236 | else { |
| 237 | // Interest Name is same length as Data full Name, but last component isn't digest |
| 238 | // so there's no possibility of matching |
| 239 | return false; |
| 240 | } |
| 241 | } |
| 242 | else { |
| 243 | // Interest Name is a strict prefix of Data full Name |
| 244 | if (!m_name.isPrefixOf(dataName)) |
| 245 | return false; |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Junxiao Shi | 42c2362 | 2014-07-03 00:55:11 -0700 | [diff] [blame] | 248 | // check Exclude |
| 249 | // Exclude won't be violated if Interest Name is same as Data full Name |
| 250 | if (!getExclude().empty() && fullNameLength > interestNameLength) { |
| 251 | if (interestNameLength == fullNameLength - 1) { |
| 252 | // component to exclude is the digest |
| 253 | if (getExclude().isExcluded(data.getFullName().get(interestNameLength))) |
| 254 | return false; |
| 255 | // There's opportunity to inspect the Exclude filter and determine whether |
| 256 | // the digest would make a difference. |
Junxiao Shi | 08d0708 | 2014-12-03 11:31:44 -0700 | [diff] [blame] | 257 | // eg. "<NameComponent>AA</NameComponent><Any/>" doesn't exclude any digest - |
| 258 | // fullName not needed; |
| 259 | // "<Any/><NameComponent>AA</NameComponent>" and |
| 260 | // "<Any/><ImplicitSha256DigestComponent>ffffffffffffffffffffffffffffffff |
| 261 | // </ImplicitSha256DigestComponent>" |
| 262 | // excludes all digests - fullName not needed; |
| 263 | // "<Any/><ImplicitSha256DigestComponent>80000000000000000000000000000000 |
| 264 | // </ImplicitSha256DigestComponent>" |
| 265 | // excludes some digests - fullName required |
Junxiao Shi | 42c2362 | 2014-07-03 00:55:11 -0700 | [diff] [blame] | 266 | // But Interests that contain the exact Data Name before digest and also |
| 267 | // contain Exclude filter is too rare to optimize for, so we request |
Junxiao Shi | 6824783 | 2017-07-03 22:06:49 +0000 | [diff] [blame] | 268 | // fullName no matter what's in the Exclude filter. |
Junxiao Shi | 42c2362 | 2014-07-03 00:55:11 -0700 | [diff] [blame] | 269 | } |
| 270 | else { |
| 271 | // component to exclude is not the digest |
| 272 | if (getExclude().isExcluded(dataName.get(interestNameLength))) |
| 273 | return false; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | // check PublisherPublicKeyLocator |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 278 | const KeyLocator& publisherPublicKeyLocator = this->getPublisherPublicKeyLocator(); |
| 279 | if (!publisherPublicKeyLocator.empty()) { |
| 280 | const Signature& signature = data.getSignature(); |
| 281 | const Block& signatureInfo = signature.getInfo(); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 282 | Block::element_const_iterator it = signatureInfo.find(tlv::KeyLocator); |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 283 | if (it == signatureInfo.elements_end()) { |
| 284 | return false; |
| 285 | } |
| 286 | if (publisherPublicKeyLocator.wireEncode() != *it) { |
| 287 | return false; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | return true; |
| 292 | } |
| 293 | |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 294 | bool |
| 295 | Interest::matchesInterest(const Interest& other) const |
| 296 | { |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 297 | /// @todo #3162 match ForwardingHint field |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 298 | return (this->getName() == other.getName() && |
| 299 | this->getSelectors() == other.getSelectors()); |
| 300 | } |
| 301 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 302 | // ---- field accessors ---- |
| 303 | |
| 304 | uint32_t |
| 305 | Interest::getNonce() const |
| 306 | { |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 307 | if (!m_nonce) { |
| 308 | m_nonce = random::generateWord32(); |
Junxiao Shi | c2ac5d2 | 2017-07-17 22:18:31 +0000 | [diff] [blame] | 309 | } |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 310 | return *m_nonce; |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | Interest& |
| 314 | Interest::setNonce(uint32_t nonce) |
| 315 | { |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 316 | m_nonce = nonce; |
| 317 | m_wire.reset(); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 318 | return *this; |
| 319 | } |
| 320 | |
| 321 | void |
| 322 | Interest::refreshNonce() |
| 323 | { |
| 324 | if (!hasNonce()) |
| 325 | return; |
| 326 | |
| 327 | uint32_t oldNonce = getNonce(); |
| 328 | uint32_t newNonce = oldNonce; |
| 329 | while (newNonce == oldNonce) |
| 330 | newNonce = random::generateWord32(); |
| 331 | |
| 332 | setNonce(newNonce); |
| 333 | } |
| 334 | |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 335 | Interest& |
| 336 | Interest::setInterestLifetime(time::milliseconds interestLifetime) |
| 337 | { |
| 338 | if (interestLifetime < time::milliseconds::zero()) { |
| 339 | BOOST_THROW_EXCEPTION(std::invalid_argument("InterestLifetime must be >= 0")); |
| 340 | } |
| 341 | m_interestLifetime = interestLifetime; |
| 342 | m_wire.reset(); |
| 343 | return *this; |
| 344 | } |
| 345 | |
Junxiao Shi | 9c154cb | 2017-07-07 22:14:54 +0000 | [diff] [blame] | 346 | Interest& |
| 347 | Interest::setForwardingHint(const DelegationList& value) |
| 348 | { |
| 349 | m_forwardingHint = value; |
| 350 | m_wire.reset(); |
| 351 | return *this; |
| 352 | } |
| 353 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 354 | // ---- operators ---- |
| 355 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 356 | std::ostream& |
| 357 | operator<<(std::ostream& os, const Interest& interest) |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 358 | { |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 359 | os << interest.getName(); |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 360 | |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 361 | char delim = '?'; |
| 362 | |
| 363 | if (interest.getMinSuffixComponents() >= 0) { |
| 364 | os << delim << "ndn.MinSuffixComponents=" << interest.getMinSuffixComponents(); |
| 365 | delim = '&'; |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 366 | } |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 367 | if (interest.getMaxSuffixComponents() >= 0) { |
| 368 | os << delim << "ndn.MaxSuffixComponents=" << interest.getMaxSuffixComponents(); |
| 369 | delim = '&'; |
Jeff Thompson | 37527d6 | 2013-08-21 11:15:54 -0700 | [diff] [blame] | 370 | } |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 371 | if (interest.getChildSelector() != DEFAULT_CHILD_SELECTOR) { |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 372 | os << delim << "ndn.ChildSelector=" << interest.getChildSelector(); |
| 373 | delim = '&'; |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 374 | } |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 375 | if (interest.getMustBeFresh()) { |
| 376 | os << delim << "ndn.MustBeFresh=" << interest.getMustBeFresh(); |
| 377 | delim = '&'; |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 378 | } |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 379 | if (interest.getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) { |
Alexander Afanasyev | a0c5f83 | 2014-06-19 13:27:56 -0700 | [diff] [blame] | 380 | os << delim << "ndn.InterestLifetime=" << interest.getInterestLifetime().count(); |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 381 | delim = '&'; |
| 382 | } |
| 383 | |
Alexander Afanasyev | e881e93 | 2014-06-08 14:47:03 +0300 | [diff] [blame] | 384 | if (interest.hasNonce()) { |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 385 | os << delim << "ndn.Nonce=" << interest.getNonce(); |
| 386 | delim = '&'; |
| 387 | } |
| 388 | if (!interest.getExclude().empty()) { |
| 389 | os << delim << "ndn.Exclude=" << interest.getExclude(); |
| 390 | delim = '&'; |
| 391 | } |
| 392 | |
| 393 | return os; |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 394 | } |
| 395 | |
Junxiao Shi | 08d0708 | 2014-12-03 11:31:44 -0700 | [diff] [blame] | 396 | } // namespace ndn |