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