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