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