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