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