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 | 42c2362 | 2014-07-03 00:55:11 -0700 | [diff] [blame] | 24 | #include "util/crypto.hpp" |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 25 | #include "data.hpp" |
Alexander Afanasyev | 840139f | 2013-12-28 15:02:50 -0800 | [diff] [blame] | 26 | |
Davide Pesavento | e178989 | 2017-02-26 15:50:52 -0500 | [diff] [blame] | 27 | #include <cstring> |
| 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) |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 41 | , m_selectedDelegationIndex(INVALID_SELECTED_DELEGATION_INDEX) |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 42 | { |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 43 | if (interestLifetime < time::milliseconds::zero()) { |
| 44 | BOOST_THROW_EXCEPTION(std::invalid_argument("InterestLifetime must be >= 0")); |
| 45 | } |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 48 | Interest::Interest(const Block& wire) |
| 49 | { |
| 50 | wireDecode(wire); |
| 51 | } |
| 52 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 53 | // ---- encode and decode ---- |
Alexander Afanasyev | 840139f | 2013-12-28 15:02:50 -0800 | [diff] [blame] | 54 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 55 | template<encoding::Tag TAG> |
| 56 | size_t |
| 57 | Interest::wireEncode(EncodingImpl<TAG>& encoder) const |
| 58 | { |
| 59 | size_t totalLength = 0; |
| 60 | |
| 61 | // Interest ::= INTEREST-TYPE TLV-LENGTH |
| 62 | // Name |
| 63 | // Selectors? |
| 64 | // Nonce |
| 65 | // InterestLifetime? |
| 66 | // Link? |
| 67 | // SelectedDelegation? |
| 68 | |
| 69 | // (reverse encoding) |
| 70 | |
| 71 | // Link and SelectedDelegation |
| 72 | if (hasLink()) { |
| 73 | if (hasSelectedDelegation()) { |
| 74 | totalLength += prependNonNegativeIntegerBlock(encoder, |
| 75 | tlv::SelectedDelegation, |
| 76 | m_selectedDelegationIndex); |
| 77 | } |
| 78 | totalLength += encoder.prependBlock(m_link); |
Alexander Afanasyev | e881e93 | 2014-06-08 14:47:03 +0300 | [diff] [blame] | 79 | } |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 80 | else { |
| 81 | BOOST_ASSERT(!hasSelectedDelegation()); |
| 82 | } |
| 83 | |
| 84 | // InterestLifetime |
| 85 | if (getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) { |
| 86 | totalLength += prependNonNegativeIntegerBlock(encoder, |
| 87 | tlv::InterestLifetime, |
| 88 | getInterestLifetime().count()); |
| 89 | } |
| 90 | |
| 91 | // Nonce |
| 92 | getNonce(); // to ensure that Nonce is properly set |
| 93 | totalLength += encoder.prependBlock(m_nonce); |
| 94 | |
| 95 | // Selectors |
| 96 | if (hasSelectors()) { |
| 97 | totalLength += getSelectors().wireEncode(encoder); |
| 98 | } |
| 99 | |
| 100 | // Name |
| 101 | totalLength += getName().wireEncode(encoder); |
| 102 | |
| 103 | totalLength += encoder.prependVarNumber(totalLength); |
| 104 | totalLength += encoder.prependVarNumber(tlv::Interest); |
| 105 | return totalLength; |
Alexander Afanasyev | 840139f | 2013-12-28 15:02:50 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 108 | template size_t |
| 109 | Interest::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const; |
| 110 | |
| 111 | template size_t |
| 112 | Interest::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const; |
| 113 | |
| 114 | const Block& |
| 115 | Interest::wireEncode() const |
Alexander Afanasyev | e881e93 | 2014-06-08 14:47:03 +0300 | [diff] [blame] | 116 | { |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 117 | if (m_wire.hasWire()) |
| 118 | return m_wire; |
| 119 | |
| 120 | EncodingEstimator estimator; |
| 121 | size_t estimatedSize = wireEncode(estimator); |
| 122 | |
| 123 | EncodingBuffer buffer(estimatedSize, 0); |
| 124 | wireEncode(buffer); |
| 125 | |
| 126 | // to ensure that Nonce block points to the right memory location |
| 127 | const_cast<Interest*>(this)->wireDecode(buffer.block()); |
| 128 | |
| 129 | return m_wire; |
Alexander Afanasyev | e881e93 | 2014-06-08 14:47:03 +0300 | [diff] [blame] | 130 | } |
Alexander Afanasyev | 840139f | 2013-12-28 15:02:50 -0800 | [diff] [blame] | 131 | |
Alexander Afanasyev | c393217 | 2014-07-10 18:53:56 -0700 | [diff] [blame] | 132 | void |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 133 | Interest::wireDecode(const Block& wire) |
Alexander Afanasyev | c393217 | 2014-07-10 18:53:56 -0700 | [diff] [blame] | 134 | { |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 135 | m_wire = wire; |
| 136 | m_wire.parse(); |
Alexander Afanasyev | c393217 | 2014-07-10 18:53:56 -0700 | [diff] [blame] | 137 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 138 | if (m_wire.type() != tlv::Interest) |
| 139 | BOOST_THROW_EXCEPTION(Error("Unexpected TLV number when decoding Interest")); |
Alexander Afanasyev | c393217 | 2014-07-10 18:53:56 -0700 | [diff] [blame] | 140 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 141 | // Name |
| 142 | m_name.wireDecode(m_wire.get(tlv::Name)); |
| 143 | |
| 144 | // Selectors |
| 145 | Block::element_const_iterator val = m_wire.find(tlv::Selectors); |
| 146 | if (val != m_wire.elements_end()) { |
| 147 | m_selectors.wireDecode(*val); |
| 148 | } |
| 149 | else |
| 150 | m_selectors = Selectors(); |
| 151 | |
| 152 | // Nonce |
| 153 | m_nonce = m_wire.get(tlv::Nonce); |
| 154 | |
| 155 | // InterestLifetime |
| 156 | val = m_wire.find(tlv::InterestLifetime); |
| 157 | if (val != m_wire.elements_end()) { |
| 158 | m_interestLifetime = time::milliseconds(readNonNegativeInteger(*val)); |
| 159 | } |
| 160 | else { |
| 161 | m_interestLifetime = DEFAULT_INTEREST_LIFETIME; |
| 162 | } |
| 163 | |
| 164 | // Link |
| 165 | m_linkCached.reset(); |
| 166 | val = m_wire.find(tlv::Data); |
| 167 | if (val != m_wire.elements_end()) { |
| 168 | m_link = (*val); |
| 169 | } |
| 170 | else { |
| 171 | m_link = Block(); |
| 172 | } |
| 173 | |
| 174 | // SelectedDelegation |
| 175 | val = m_wire.find(tlv::SelectedDelegation); |
| 176 | if (val != m_wire.elements_end()) { |
| 177 | if (!this->hasLink()) { |
| 178 | BOOST_THROW_EXCEPTION(Error("Interest contains SelectedDelegation, but no LINK object")); |
| 179 | } |
| 180 | uint64_t selectedDelegation = readNonNegativeInteger(*val); |
| 181 | if (selectedDelegation < uint64_t(Link::countDelegationsFromWire(m_link))) { |
| 182 | m_selectedDelegationIndex = static_cast<size_t>(selectedDelegation); |
| 183 | } |
| 184 | else { |
| 185 | BOOST_THROW_EXCEPTION(Error("Invalid selected delegation index when decoding Interest")); |
| 186 | } |
| 187 | } |
| 188 | else { |
| 189 | m_selectedDelegationIndex = INVALID_SELECTED_DELEGATION_INDEX; |
| 190 | } |
Alexander Afanasyev | c393217 | 2014-07-10 18:53:56 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 193 | // ---- matching ---- |
| 194 | |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 195 | bool |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 196 | Interest::matchesName(const Name& name) const |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 197 | { |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 198 | if (name.size() < m_name.size()) |
| 199 | return false; |
| 200 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 201 | if (!m_name.isPrefixOf(name)) |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 202 | return false; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 203 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 204 | if (getMinSuffixComponents() >= 0 && |
Alexander Afanasyev | 3b70310 | 2014-06-13 17:01:14 -0700 | [diff] [blame] | 205 | // name must include implicit digest |
| 206 | !(name.size() - m_name.size() >= static_cast<size_t>(getMinSuffixComponents()))) |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 207 | return false; |
| 208 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 209 | if (getMaxSuffixComponents() >= 0 && |
Alexander Afanasyev | 3b70310 | 2014-06-13 17:01:14 -0700 | [diff] [blame] | 210 | // name must include implicit digest |
| 211 | !(name.size() - m_name.size() <= static_cast<size_t>(getMaxSuffixComponents()))) |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 212 | return false; |
| 213 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 214 | if (!getExclude().empty() && |
| 215 | name.size() > m_name.size() && |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 216 | getExclude().isExcluded(name[m_name.size()])) |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 217 | return false; |
| 218 | |
| 219 | return true; |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 222 | bool |
| 223 | Interest::matchesData(const Data& data) const |
| 224 | { |
Junxiao Shi | 42c2362 | 2014-07-03 00:55:11 -0700 | [diff] [blame] | 225 | size_t interestNameLength = m_name.size(); |
| 226 | const Name& dataName = data.getName(); |
| 227 | size_t fullNameLength = dataName.size() + 1; |
| 228 | |
| 229 | // check MinSuffixComponents |
| 230 | bool hasMinSuffixComponents = getMinSuffixComponents() >= 0; |
| 231 | size_t minSuffixComponents = hasMinSuffixComponents ? |
| 232 | static_cast<size_t>(getMinSuffixComponents()) : 0; |
| 233 | if (!(interestNameLength + minSuffixComponents <= fullNameLength)) |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 234 | return false; |
Junxiao Shi | 42c2362 | 2014-07-03 00:55:11 -0700 | [diff] [blame] | 235 | |
| 236 | // check MaxSuffixComponents |
| 237 | bool hasMaxSuffixComponents = getMaxSuffixComponents() >= 0; |
| 238 | if (hasMaxSuffixComponents && |
| 239 | !(interestNameLength + getMaxSuffixComponents() >= fullNameLength)) |
| 240 | return false; |
| 241 | |
| 242 | // check prefix |
| 243 | if (interestNameLength == fullNameLength) { |
Alexander Afanasyev | 56860f5 | 2014-11-07 11:51:17 -0800 | [diff] [blame] | 244 | if (m_name.get(-1).isImplicitSha256Digest()) { |
| 245 | if (m_name != data.getFullName()) |
Junxiao Shi | 42c2362 | 2014-07-03 00:55:11 -0700 | [diff] [blame] | 246 | return false; |
| 247 | } |
| 248 | else { |
| 249 | // Interest Name is same length as Data full Name, but last component isn't digest |
| 250 | // so there's no possibility of matching |
| 251 | return false; |
| 252 | } |
| 253 | } |
| 254 | else { |
| 255 | // Interest Name is a strict prefix of Data full Name |
| 256 | if (!m_name.isPrefixOf(dataName)) |
| 257 | return false; |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Junxiao Shi | 42c2362 | 2014-07-03 00:55:11 -0700 | [diff] [blame] | 260 | // check Exclude |
| 261 | // Exclude won't be violated if Interest Name is same as Data full Name |
| 262 | if (!getExclude().empty() && fullNameLength > interestNameLength) { |
| 263 | if (interestNameLength == fullNameLength - 1) { |
| 264 | // component to exclude is the digest |
| 265 | if (getExclude().isExcluded(data.getFullName().get(interestNameLength))) |
| 266 | return false; |
| 267 | // There's opportunity to inspect the Exclude filter and determine whether |
| 268 | // the digest would make a difference. |
Junxiao Shi | 08d0708 | 2014-12-03 11:31:44 -0700 | [diff] [blame] | 269 | // eg. "<NameComponent>AA</NameComponent><Any/>" doesn't exclude any digest - |
| 270 | // fullName not needed; |
| 271 | // "<Any/><NameComponent>AA</NameComponent>" and |
| 272 | // "<Any/><ImplicitSha256DigestComponent>ffffffffffffffffffffffffffffffff |
| 273 | // </ImplicitSha256DigestComponent>" |
| 274 | // excludes all digests - fullName not needed; |
| 275 | // "<Any/><ImplicitSha256DigestComponent>80000000000000000000000000000000 |
| 276 | // </ImplicitSha256DigestComponent>" |
| 277 | // excludes some digests - fullName required |
Junxiao Shi | 42c2362 | 2014-07-03 00:55:11 -0700 | [diff] [blame] | 278 | // But Interests that contain the exact Data Name before digest and also |
| 279 | // contain Exclude filter is too rare to optimize for, so we request |
Junxiao Shi | 6824783 | 2017-07-03 22:06:49 +0000 | [diff] [blame] | 280 | // fullName no matter what's in the Exclude filter. |
Junxiao Shi | 42c2362 | 2014-07-03 00:55:11 -0700 | [diff] [blame] | 281 | } |
| 282 | else { |
| 283 | // component to exclude is not the digest |
| 284 | if (getExclude().isExcluded(dataName.get(interestNameLength))) |
| 285 | return false; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | // check PublisherPublicKeyLocator |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 290 | const KeyLocator& publisherPublicKeyLocator = this->getPublisherPublicKeyLocator(); |
| 291 | if (!publisherPublicKeyLocator.empty()) { |
| 292 | const Signature& signature = data.getSignature(); |
| 293 | const Block& signatureInfo = signature.getInfo(); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 294 | Block::element_const_iterator it = signatureInfo.find(tlv::KeyLocator); |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 295 | if (it == signatureInfo.elements_end()) { |
| 296 | return false; |
| 297 | } |
| 298 | if (publisherPublicKeyLocator.wireEncode() != *it) { |
| 299 | return false; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | return true; |
| 304 | } |
| 305 | |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 306 | bool |
| 307 | Interest::matchesInterest(const Interest& other) const |
| 308 | { |
| 309 | /// @todo #3162 match Link field |
| 310 | return (this->getName() == other.getName() && |
| 311 | this->getSelectors() == other.getSelectors()); |
| 312 | } |
| 313 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 314 | // ---- field accessors ---- |
| 315 | |
| 316 | uint32_t |
| 317 | Interest::getNonce() const |
| 318 | { |
| 319 | if (!m_nonce.hasWire()) |
| 320 | const_cast<Interest*>(this)->setNonce(random::generateWord32()); |
| 321 | |
| 322 | if (m_nonce.value_size() == sizeof(uint32_t)) |
| 323 | return *reinterpret_cast<const uint32_t*>(m_nonce.value()); |
| 324 | else { |
| 325 | // for compatibility reasons. Should be removed eventually |
| 326 | return readNonNegativeInteger(m_nonce); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | Interest& |
| 331 | Interest::setNonce(uint32_t nonce) |
| 332 | { |
| 333 | if (m_wire.hasWire() && m_nonce.value_size() == sizeof(uint32_t)) { |
| 334 | std::memcpy(const_cast<uint8_t*>(m_nonce.value()), &nonce, sizeof(nonce)); |
| 335 | } |
| 336 | else { |
| 337 | m_nonce = makeBinaryBlock(tlv::Nonce, |
| 338 | reinterpret_cast<const uint8_t*>(&nonce), |
| 339 | sizeof(nonce)); |
| 340 | m_wire.reset(); |
| 341 | } |
| 342 | return *this; |
| 343 | } |
| 344 | |
| 345 | void |
| 346 | Interest::refreshNonce() |
| 347 | { |
| 348 | if (!hasNonce()) |
| 349 | return; |
| 350 | |
| 351 | uint32_t oldNonce = getNonce(); |
| 352 | uint32_t newNonce = oldNonce; |
| 353 | while (newNonce == oldNonce) |
| 354 | newNonce = random::generateWord32(); |
| 355 | |
| 356 | setNonce(newNonce); |
| 357 | } |
| 358 | |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 359 | Interest& |
| 360 | Interest::setInterestLifetime(time::milliseconds interestLifetime) |
| 361 | { |
| 362 | if (interestLifetime < time::milliseconds::zero()) { |
| 363 | BOOST_THROW_EXCEPTION(std::invalid_argument("InterestLifetime must be >= 0")); |
| 364 | } |
| 365 | m_interestLifetime = interestLifetime; |
| 366 | m_wire.reset(); |
| 367 | return *this; |
| 368 | } |
| 369 | |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 370 | bool |
| 371 | Interest::hasLink() const |
| 372 | { |
Alexander Afanasyev | cac0838 | 2015-09-02 14:52:40 -0700 | [diff] [blame] | 373 | return m_link.hasWire(); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Alexander Afanasyev | cac0838 | 2015-09-02 14:52:40 -0700 | [diff] [blame] | 376 | const Link& |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 377 | Interest::getLink() const |
| 378 | { |
Alexander Afanasyev | cac0838 | 2015-09-02 14:52:40 -0700 | [diff] [blame] | 379 | if (hasLink()) { |
| 380 | if (!m_linkCached) { |
| 381 | m_linkCached = make_shared<Link>(m_link); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 382 | } |
Alexander Afanasyev | cac0838 | 2015-09-02 14:52:40 -0700 | [diff] [blame] | 383 | return *m_linkCached; |
| 384 | } |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 385 | BOOST_THROW_EXCEPTION(Error("There is no encapsulated link object")); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | void |
| 389 | Interest::setLink(const Block& link) |
| 390 | { |
| 391 | m_link = link; |
| 392 | if (!link.hasWire()) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 393 | BOOST_THROW_EXCEPTION(Error("The given link does not have a wire format")); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 394 | } |
| 395 | m_wire.reset(); |
Alexander Afanasyev | cac0838 | 2015-09-02 14:52:40 -0700 | [diff] [blame] | 396 | m_linkCached.reset(); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 397 | this->unsetSelectedDelegation(); |
| 398 | } |
| 399 | |
| 400 | void |
| 401 | Interest::unsetLink() |
| 402 | { |
| 403 | m_link.reset(); |
| 404 | m_wire.reset(); |
Alexander Afanasyev | cac0838 | 2015-09-02 14:52:40 -0700 | [diff] [blame] | 405 | m_linkCached.reset(); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 406 | this->unsetSelectedDelegation(); |
| 407 | } |
| 408 | |
| 409 | bool |
| 410 | Interest::hasSelectedDelegation() const |
| 411 | { |
Alexander Afanasyev | cac0838 | 2015-09-02 14:52:40 -0700 | [diff] [blame] | 412 | return m_selectedDelegationIndex != INVALID_SELECTED_DELEGATION_INDEX; |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | Name |
| 416 | Interest::getSelectedDelegation() const |
| 417 | { |
| 418 | if (!hasSelectedDelegation()) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 419 | BOOST_THROW_EXCEPTION(Error("There is no encapsulated selected delegation")); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 420 | } |
| 421 | return std::get<1>(Link::getDelegationFromWire(m_link, m_selectedDelegationIndex)); |
| 422 | } |
| 423 | |
| 424 | void |
| 425 | Interest::setSelectedDelegation(const Name& delegationName) |
| 426 | { |
| 427 | size_t delegationIndex = Link::findDelegationFromWire(m_link, delegationName); |
| 428 | if (delegationIndex != INVALID_SELECTED_DELEGATION_INDEX) { |
| 429 | m_selectedDelegationIndex = delegationIndex; |
| 430 | } |
| 431 | else { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 432 | BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid selected delegation name")); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 433 | } |
| 434 | m_wire.reset(); |
| 435 | } |
| 436 | |
| 437 | void |
| 438 | Interest::setSelectedDelegation(size_t delegationIndex) |
| 439 | { |
| 440 | if (delegationIndex >= Link(m_link).getDelegations().size()) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 441 | BOOST_THROW_EXCEPTION(Error("Invalid selected delegation index")); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 442 | } |
| 443 | m_selectedDelegationIndex = delegationIndex; |
| 444 | m_wire.reset(); |
| 445 | } |
| 446 | |
| 447 | void |
| 448 | Interest::unsetSelectedDelegation() |
| 449 | { |
| 450 | m_selectedDelegationIndex = INVALID_SELECTED_DELEGATION_INDEX; |
| 451 | m_wire.reset(); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 454 | // ---- operators ---- |
| 455 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 456 | std::ostream& |
| 457 | operator<<(std::ostream& os, const Interest& interest) |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 458 | { |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 459 | os << interest.getName(); |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 460 | |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 461 | char delim = '?'; |
| 462 | |
| 463 | if (interest.getMinSuffixComponents() >= 0) { |
| 464 | os << delim << "ndn.MinSuffixComponents=" << interest.getMinSuffixComponents(); |
| 465 | delim = '&'; |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 466 | } |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 467 | if (interest.getMaxSuffixComponents() >= 0) { |
| 468 | os << delim << "ndn.MaxSuffixComponents=" << interest.getMaxSuffixComponents(); |
| 469 | delim = '&'; |
Jeff Thompson | 37527d6 | 2013-08-21 11:15:54 -0700 | [diff] [blame] | 470 | } |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 471 | if (interest.getChildSelector() != DEFAULT_CHILD_SELECTOR) { |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 472 | os << delim << "ndn.ChildSelector=" << interest.getChildSelector(); |
| 473 | delim = '&'; |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 474 | } |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 475 | if (interest.getMustBeFresh()) { |
| 476 | os << delim << "ndn.MustBeFresh=" << interest.getMustBeFresh(); |
| 477 | delim = '&'; |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 478 | } |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 479 | if (interest.getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) { |
Alexander Afanasyev | a0c5f83 | 2014-06-19 13:27:56 -0700 | [diff] [blame] | 480 | os << delim << "ndn.InterestLifetime=" << interest.getInterestLifetime().count(); |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 481 | delim = '&'; |
| 482 | } |
| 483 | |
Alexander Afanasyev | e881e93 | 2014-06-08 14:47:03 +0300 | [diff] [blame] | 484 | if (interest.hasNonce()) { |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 485 | os << delim << "ndn.Nonce=" << interest.getNonce(); |
| 486 | delim = '&'; |
| 487 | } |
| 488 | if (!interest.getExclude().empty()) { |
| 489 | os << delim << "ndn.Exclude=" << interest.getExclude(); |
| 490 | delim = '&'; |
| 491 | } |
| 492 | |
| 493 | return os; |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 494 | } |
| 495 | |
Junxiao Shi | 08d0708 | 2014-12-03 11:31:44 -0700 | [diff] [blame] | 496 | } // namespace ndn |