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