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() |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 39 | : m_interestLifetime(DEFAULT_INTEREST_LIFETIME) |
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) |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 46 | , m_interestLifetime(DEFAULT_INTEREST_LIFETIME) |
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 | |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 225 | Interest& |
| 226 | Interest::setInterestLifetime(time::milliseconds interestLifetime) |
| 227 | { |
| 228 | if (interestLifetime < time::milliseconds::zero()) { |
| 229 | BOOST_THROW_EXCEPTION(std::invalid_argument("InterestLifetime must be >= 0")); |
| 230 | } |
| 231 | m_interestLifetime = interestLifetime; |
| 232 | m_wire.reset(); |
| 233 | return *this; |
| 234 | } |
| 235 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 236 | template<encoding::Tag TAG> |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 237 | size_t |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 238 | Interest::wireEncode(EncodingImpl<TAG>& encoder) const |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 239 | { |
| 240 | size_t totalLength = 0; |
| 241 | |
| 242 | // Interest ::= INTEREST-TYPE TLV-LENGTH |
| 243 | // Name |
| 244 | // Selectors? |
| 245 | // Nonce |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 246 | // InterestLifetime? |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 247 | // Link? |
| 248 | // SelectedDelegation? |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 249 | |
| 250 | // (reverse encoding) |
| 251 | |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 252 | if (hasLink()) { |
| 253 | if (hasSelectedDelegation()) { |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 254 | totalLength += prependNonNegativeIntegerBlock(encoder, |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 255 | tlv::SelectedDelegation, |
| 256 | m_selectedDelegationIndex); |
| 257 | } |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 258 | totalLength += encoder.prependBlock(m_link); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 259 | } |
| 260 | else { |
| 261 | BOOST_ASSERT(!hasSelectedDelegation()); |
| 262 | } |
| 263 | |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 264 | // InterestLifetime |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 265 | if (getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) { |
| 266 | totalLength += prependNonNegativeIntegerBlock(encoder, |
| 267 | tlv::InterestLifetime, |
| 268 | getInterestLifetime().count()); |
| 269 | } |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 270 | |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 271 | // Nonce |
| 272 | getNonce(); // to ensure that Nonce is properly set |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 273 | totalLength += encoder.prependBlock(m_nonce); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 274 | |
| 275 | // Selectors |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 276 | if (hasSelectors()) { |
| 277 | totalLength += getSelectors().wireEncode(encoder); |
| 278 | } |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 279 | |
| 280 | // Name |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 281 | totalLength += getName().wireEncode(encoder); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 282 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 283 | totalLength += encoder.prependVarNumber(totalLength); |
| 284 | totalLength += encoder.prependVarNumber(tlv::Interest); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 285 | return totalLength; |
| 286 | } |
| 287 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 288 | template size_t |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 289 | Interest::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 290 | |
| 291 | template size_t |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 292 | Interest::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 293 | |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 294 | const Block& |
| 295 | Interest::wireEncode() const |
| 296 | { |
| 297 | if (m_wire.hasWire()) |
| 298 | return m_wire; |
| 299 | |
| 300 | EncodingEstimator estimator; |
| 301 | size_t estimatedSize = wireEncode(estimator); |
| 302 | |
| 303 | EncodingBuffer buffer(estimatedSize, 0); |
| 304 | wireEncode(buffer); |
| 305 | |
| 306 | // to ensure that Nonce block points to the right memory location |
| 307 | const_cast<Interest*>(this)->wireDecode(buffer.block()); |
| 308 | |
| 309 | return m_wire; |
| 310 | } |
| 311 | |
| 312 | void |
| 313 | Interest::wireDecode(const Block& wire) |
| 314 | { |
| 315 | m_wire = wire; |
| 316 | m_wire.parse(); |
| 317 | |
| 318 | // Interest ::= INTEREST-TYPE TLV-LENGTH |
| 319 | // Name |
| 320 | // Selectors? |
| 321 | // Nonce |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 322 | // InterestLifetime? |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 323 | // Link? |
| 324 | // SelectedDelegation? |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 325 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 326 | if (m_wire.type() != tlv::Interest) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 327 | BOOST_THROW_EXCEPTION(Error("Unexpected TLV number when decoding Interest")); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 328 | |
| 329 | // Name |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 330 | m_name.wireDecode(m_wire.get(tlv::Name)); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 331 | |
| 332 | // Selectors |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 333 | Block::element_const_iterator val = m_wire.find(tlv::Selectors); |
Alexander Afanasyev | cac0838 | 2015-09-02 14:52:40 -0700 | [diff] [blame] | 334 | if (val != m_wire.elements_end()) { |
| 335 | m_selectors.wireDecode(*val); |
| 336 | } |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 337 | else |
| 338 | m_selectors = Selectors(); |
| 339 | |
| 340 | // Nonce |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 341 | m_nonce = m_wire.get(tlv::Nonce); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 342 | |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 343 | // InterestLifetime |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 344 | val = m_wire.find(tlv::InterestLifetime); |
Alexander Afanasyev | cac0838 | 2015-09-02 14:52:40 -0700 | [diff] [blame] | 345 | if (val != m_wire.elements_end()) { |
| 346 | m_interestLifetime = time::milliseconds(readNonNegativeInteger(*val)); |
| 347 | } |
| 348 | else { |
| 349 | m_interestLifetime = DEFAULT_INTEREST_LIFETIME; |
| 350 | } |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 351 | |
| 352 | // Link object |
Alexander Afanasyev | cac0838 | 2015-09-02 14:52:40 -0700 | [diff] [blame] | 353 | m_linkCached.reset(); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 354 | val = m_wire.find(tlv::Data); |
Alexander Afanasyev | cac0838 | 2015-09-02 14:52:40 -0700 | [diff] [blame] | 355 | if (val != m_wire.elements_end()) { |
| 356 | m_link = (*val); |
| 357 | } |
| 358 | else { |
| 359 | m_link = Block(); |
| 360 | } |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 361 | |
| 362 | // SelectedDelegation |
| 363 | val = m_wire.find(tlv::SelectedDelegation); |
| 364 | if (val != m_wire.elements_end()) { |
| 365 | if (!this->hasLink()) { |
Alexander Afanasyev | cac0838 | 2015-09-02 14:52:40 -0700 | [diff] [blame] | 366 | BOOST_THROW_EXCEPTION(Error("Interest contains SelectedDelegation, but no LINK object")); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 367 | } |
| 368 | uint64_t selectedDelegation = readNonNegativeInteger(*val); |
| 369 | if (selectedDelegation < uint64_t(Link::countDelegationsFromWire(m_link))) { |
| 370 | m_selectedDelegationIndex = static_cast<size_t>(selectedDelegation); |
| 371 | } |
| 372 | else { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 373 | BOOST_THROW_EXCEPTION(Error("Invalid selected delegation index when decoding Interest")); |
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 | else { |
| 377 | m_selectedDelegationIndex = INVALID_SELECTED_DELEGATION_INDEX; |
| 378 | } |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | bool |
| 382 | Interest::hasLink() const |
| 383 | { |
Alexander Afanasyev | cac0838 | 2015-09-02 14:52:40 -0700 | [diff] [blame] | 384 | return m_link.hasWire(); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Alexander Afanasyev | cac0838 | 2015-09-02 14:52:40 -0700 | [diff] [blame] | 387 | const Link& |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 388 | Interest::getLink() const |
| 389 | { |
Alexander Afanasyev | cac0838 | 2015-09-02 14:52:40 -0700 | [diff] [blame] | 390 | if (hasLink()) { |
| 391 | if (!m_linkCached) { |
| 392 | m_linkCached = make_shared<Link>(m_link); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 393 | } |
Alexander Afanasyev | cac0838 | 2015-09-02 14:52:40 -0700 | [diff] [blame] | 394 | return *m_linkCached; |
| 395 | } |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 396 | BOOST_THROW_EXCEPTION(Error("There is no encapsulated link object")); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | void |
| 400 | Interest::setLink(const Block& link) |
| 401 | { |
| 402 | m_link = link; |
| 403 | if (!link.hasWire()) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 404 | 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] | 405 | } |
| 406 | m_wire.reset(); |
Alexander Afanasyev | cac0838 | 2015-09-02 14:52:40 -0700 | [diff] [blame] | 407 | m_linkCached.reset(); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 408 | this->unsetSelectedDelegation(); |
| 409 | } |
| 410 | |
| 411 | void |
| 412 | Interest::unsetLink() |
| 413 | { |
| 414 | m_link.reset(); |
| 415 | m_wire.reset(); |
Alexander Afanasyev | cac0838 | 2015-09-02 14:52:40 -0700 | [diff] [blame] | 416 | m_linkCached.reset(); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 417 | this->unsetSelectedDelegation(); |
| 418 | } |
| 419 | |
| 420 | bool |
| 421 | Interest::hasSelectedDelegation() const |
| 422 | { |
Alexander Afanasyev | cac0838 | 2015-09-02 14:52:40 -0700 | [diff] [blame] | 423 | return m_selectedDelegationIndex != INVALID_SELECTED_DELEGATION_INDEX; |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | Name |
| 427 | Interest::getSelectedDelegation() const |
| 428 | { |
| 429 | if (!hasSelectedDelegation()) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 430 | BOOST_THROW_EXCEPTION(Error("There is no encapsulated selected delegation")); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 431 | } |
| 432 | return std::get<1>(Link::getDelegationFromWire(m_link, m_selectedDelegationIndex)); |
| 433 | } |
| 434 | |
| 435 | void |
| 436 | Interest::setSelectedDelegation(const Name& delegationName) |
| 437 | { |
| 438 | size_t delegationIndex = Link::findDelegationFromWire(m_link, delegationName); |
| 439 | if (delegationIndex != INVALID_SELECTED_DELEGATION_INDEX) { |
| 440 | m_selectedDelegationIndex = delegationIndex; |
| 441 | } |
| 442 | else { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 443 | BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid selected delegation name")); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 444 | } |
| 445 | m_wire.reset(); |
| 446 | } |
| 447 | |
| 448 | void |
| 449 | Interest::setSelectedDelegation(size_t delegationIndex) |
| 450 | { |
| 451 | if (delegationIndex >= Link(m_link).getDelegations().size()) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 452 | BOOST_THROW_EXCEPTION(Error("Invalid selected delegation index")); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 453 | } |
| 454 | m_selectedDelegationIndex = delegationIndex; |
| 455 | m_wire.reset(); |
| 456 | } |
| 457 | |
| 458 | void |
| 459 | Interest::unsetSelectedDelegation() |
| 460 | { |
| 461 | m_selectedDelegationIndex = INVALID_SELECTED_DELEGATION_INDEX; |
| 462 | m_wire.reset(); |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 465 | std::ostream& |
| 466 | operator<<(std::ostream& os, const Interest& interest) |
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 | os << interest.getName(); |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 469 | |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 470 | char delim = '?'; |
| 471 | |
| 472 | if (interest.getMinSuffixComponents() >= 0) { |
| 473 | os << delim << "ndn.MinSuffixComponents=" << interest.getMinSuffixComponents(); |
| 474 | delim = '&'; |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 475 | } |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 476 | if (interest.getMaxSuffixComponents() >= 0) { |
| 477 | os << delim << "ndn.MaxSuffixComponents=" << interest.getMaxSuffixComponents(); |
| 478 | delim = '&'; |
Jeff Thompson | 37527d6 | 2013-08-21 11:15:54 -0700 | [diff] [blame] | 479 | } |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 480 | if (interest.getChildSelector() != DEFAULT_CHILD_SELECTOR) { |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 481 | os << delim << "ndn.ChildSelector=" << interest.getChildSelector(); |
| 482 | delim = '&'; |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 483 | } |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 484 | if (interest.getMustBeFresh()) { |
| 485 | os << delim << "ndn.MustBeFresh=" << interest.getMustBeFresh(); |
| 486 | delim = '&'; |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 487 | } |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 488 | if (interest.getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) { |
Alexander Afanasyev | a0c5f83 | 2014-06-19 13:27:56 -0700 | [diff] [blame] | 489 | os << delim << "ndn.InterestLifetime=" << interest.getInterestLifetime().count(); |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 490 | delim = '&'; |
| 491 | } |
| 492 | |
Alexander Afanasyev | e881e93 | 2014-06-08 14:47:03 +0300 | [diff] [blame] | 493 | if (interest.hasNonce()) { |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 494 | os << delim << "ndn.Nonce=" << interest.getNonce(); |
| 495 | delim = '&'; |
| 496 | } |
| 497 | if (!interest.getExclude().empty()) { |
| 498 | os << delim << "ndn.Exclude=" << interest.getExclude(); |
| 499 | delim = '&'; |
| 500 | } |
| 501 | |
| 502 | return os; |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 503 | } |
| 504 | |
Junxiao Shi | 08d0708 | 2014-12-03 11:31:44 -0700 | [diff] [blame] | 505 | } // namespace ndn |