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 | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 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. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * Based on code originally written by Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 22 | */ |
| 23 | |
Alexander Afanasyev | 09c613f | 2014-01-29 00:23:58 -0800 | [diff] [blame] | 24 | #include "interest.hpp" |
Alexander Afanasyev | e9fdb80 | 2014-02-05 17:36:51 -0800 | [diff] [blame] | 25 | #include "util/random.hpp" |
Junxiao Shi | 42c2362 | 2014-07-03 00:55:11 -0700 | [diff] [blame^] | 26 | #include "util/crypto.hpp" |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 27 | #include "data.hpp" |
Alexander Afanasyev | 840139f | 2013-12-28 15:02:50 -0800 | [diff] [blame] | 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 | |
Alexander Afanasyev | e881e93 | 2014-06-08 14:47:03 +0300 | [diff] [blame] | 31 | uint32_t |
Alexander Afanasyev | 840139f | 2013-12-28 15:02:50 -0800 | [diff] [blame] | 32 | Interest::getNonce() const |
| 33 | { |
Alexander Afanasyev | e881e93 | 2014-06-08 14:47:03 +0300 | [diff] [blame] | 34 | if (!m_nonce.hasWire()) |
| 35 | const_cast<Interest*>(this)->setNonce(random::generateWord32()); |
Alexander Afanasyev | 840139f | 2013-12-28 15:02:50 -0800 | [diff] [blame] | 36 | |
Alexander Afanasyev | e881e93 | 2014-06-08 14:47:03 +0300 | [diff] [blame] | 37 | if (m_nonce.value_size() == sizeof(uint32_t)) |
| 38 | return *reinterpret_cast<const uint32_t*>(m_nonce.value()); |
| 39 | else { |
| 40 | // for compatibility reasons. Should be removed eventually |
| 41 | return readNonNegativeInteger(m_nonce); |
| 42 | } |
Alexander Afanasyev | 840139f | 2013-12-28 15:02:50 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Alexander Afanasyev | e881e93 | 2014-06-08 14:47:03 +0300 | [diff] [blame] | 45 | Interest& |
| 46 | Interest::setNonce(uint32_t nonce) |
| 47 | { |
| 48 | if (m_wire.hasWire() && m_nonce.value_size() == sizeof(uint32_t)) { |
| 49 | std::memcpy(const_cast<uint8_t*>(m_nonce.value()), &nonce, sizeof(nonce)); |
| 50 | } |
| 51 | else { |
| 52 | m_nonce = dataBlock(Tlv::Nonce, |
| 53 | reinterpret_cast<const uint8_t*>(&nonce), |
| 54 | sizeof(nonce)); |
| 55 | m_wire.reset(); |
| 56 | } |
| 57 | return *this; |
| 58 | } |
Alexander Afanasyev | 840139f | 2013-12-28 15:02:50 -0800 | [diff] [blame] | 59 | |
Alexander Afanasyev | c393217 | 2014-07-10 18:53:56 -0700 | [diff] [blame] | 60 | void |
| 61 | Interest::refreshNonce() |
| 62 | { |
| 63 | if (!hasNonce()) |
| 64 | return; |
| 65 | |
| 66 | uint32_t oldNonce = getNonce(); |
| 67 | uint32_t newNonce = oldNonce; |
| 68 | while (newNonce == oldNonce) |
| 69 | newNonce = random::generateWord32(); |
| 70 | |
| 71 | setNonce(newNonce); |
| 72 | } |
| 73 | |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 74 | bool |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 75 | Interest::matchesName(const Name& name) const |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 76 | { |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 77 | if (name.size() < m_name.size()) |
| 78 | return false; |
| 79 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 80 | if (!m_name.isPrefixOf(name)) |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 81 | return false; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 82 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 83 | if (getMinSuffixComponents() >= 0 && |
Alexander Afanasyev | 3b70310 | 2014-06-13 17:01:14 -0700 | [diff] [blame] | 84 | // name must include implicit digest |
| 85 | !(name.size() - m_name.size() >= static_cast<size_t>(getMinSuffixComponents()))) |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 86 | return false; |
| 87 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 88 | if (getMaxSuffixComponents() >= 0 && |
Alexander Afanasyev | 3b70310 | 2014-06-13 17:01:14 -0700 | [diff] [blame] | 89 | // name must include implicit digest |
| 90 | !(name.size() - m_name.size() <= static_cast<size_t>(getMaxSuffixComponents()))) |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 91 | return false; |
| 92 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 93 | if (!getExclude().empty() && |
| 94 | name.size() > m_name.size() && |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 95 | getExclude().isExcluded(name[m_name.size()])) |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 96 | return false; |
| 97 | |
| 98 | return true; |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 101 | bool |
| 102 | Interest::matchesData(const Data& data) const |
| 103 | { |
Junxiao Shi | 42c2362 | 2014-07-03 00:55:11 -0700 | [diff] [blame^] | 104 | size_t interestNameLength = m_name.size(); |
| 105 | const Name& dataName = data.getName(); |
| 106 | size_t fullNameLength = dataName.size() + 1; |
| 107 | |
| 108 | // check MinSuffixComponents |
| 109 | bool hasMinSuffixComponents = getMinSuffixComponents() >= 0; |
| 110 | size_t minSuffixComponents = hasMinSuffixComponents ? |
| 111 | static_cast<size_t>(getMinSuffixComponents()) : 0; |
| 112 | if (!(interestNameLength + minSuffixComponents <= fullNameLength)) |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 113 | return false; |
Junxiao Shi | 42c2362 | 2014-07-03 00:55:11 -0700 | [diff] [blame^] | 114 | |
| 115 | // check MaxSuffixComponents |
| 116 | bool hasMaxSuffixComponents = getMaxSuffixComponents() >= 0; |
| 117 | if (hasMaxSuffixComponents && |
| 118 | !(interestNameLength + getMaxSuffixComponents() >= fullNameLength)) |
| 119 | return false; |
| 120 | |
| 121 | // check prefix |
| 122 | if (interestNameLength == fullNameLength) { |
| 123 | bool mightEndWithDigest = (interestNameLength > 0) && |
| 124 | (m_name.get(-1).value_size() == crypto::SHA256_DIGEST_SIZE); |
| 125 | if (mightEndWithDigest) { |
| 126 | // Interest Name is same length as Data full Name, last component could match digest |
| 127 | if (!m_name.isPrefixOf(data.getFullName())) |
| 128 | return false; |
| 129 | } |
| 130 | else { |
| 131 | // Interest Name is same length as Data full Name, but last component isn't digest |
| 132 | // so there's no possibility of matching |
| 133 | return false; |
| 134 | } |
| 135 | } |
| 136 | else { |
| 137 | // Interest Name is a strict prefix of Data full Name |
| 138 | if (!m_name.isPrefixOf(dataName)) |
| 139 | return false; |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Junxiao Shi | 42c2362 | 2014-07-03 00:55:11 -0700 | [diff] [blame^] | 142 | // check Exclude |
| 143 | // Exclude won't be violated if Interest Name is same as Data full Name |
| 144 | if (!getExclude().empty() && fullNameLength > interestNameLength) { |
| 145 | if (interestNameLength == fullNameLength - 1) { |
| 146 | // component to exclude is the digest |
| 147 | if (getExclude().isExcluded(data.getFullName().get(interestNameLength))) |
| 148 | return false; |
| 149 | // There's opportunity to inspect the Exclude filter and determine whether |
| 150 | // the digest would make a difference. |
| 151 | // eg. "Exclude=<Any>AA" won't exclude any digest - fullName not needed |
| 152 | // "Exclude=ZZ<Any>" excludes all digests - fullName not needed |
| 153 | // "Exclude=<Any>80000000000000000000000000000000" |
| 154 | // excludes half of the digests - fullName required |
| 155 | // But Interests that contain the exact Data Name before digest and also |
| 156 | // contain Exclude filter is too rare to optimize for, so we request |
| 157 | // fullName no mater what's in the Exclude filter. |
| 158 | } |
| 159 | else { |
| 160 | // component to exclude is not the digest |
| 161 | if (getExclude().isExcluded(dataName.get(interestNameLength))) |
| 162 | return false; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // check PublisherPublicKeyLocator |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 167 | const KeyLocator& publisherPublicKeyLocator = this->getPublisherPublicKeyLocator(); |
| 168 | if (!publisherPublicKeyLocator.empty()) { |
| 169 | const Signature& signature = data.getSignature(); |
| 170 | const Block& signatureInfo = signature.getInfo(); |
| 171 | Block::element_const_iterator it = signatureInfo.find(Tlv::KeyLocator); |
| 172 | if (it == signatureInfo.elements_end()) { |
| 173 | return false; |
| 174 | } |
| 175 | if (publisherPublicKeyLocator.wireEncode() != *it) { |
| 176 | return false; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | return true; |
| 181 | } |
| 182 | |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 183 | template<bool T> |
| 184 | size_t |
| 185 | Interest::wireEncode(EncodingImpl<T>& block) const |
| 186 | { |
| 187 | size_t totalLength = 0; |
| 188 | |
| 189 | // Interest ::= INTEREST-TYPE TLV-LENGTH |
| 190 | // Name |
| 191 | // Selectors? |
| 192 | // Nonce |
| 193 | // Scope? |
| 194 | // InterestLifetime? |
| 195 | |
| 196 | // (reverse encoding) |
| 197 | |
| 198 | // InterestLifetime |
| 199 | if (getInterestLifetime() >= time::milliseconds::zero() && |
| 200 | getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) |
| 201 | { |
| 202 | totalLength += prependNonNegativeIntegerBlock(block, |
| 203 | Tlv::InterestLifetime, |
| 204 | getInterestLifetime().count()); |
| 205 | } |
| 206 | |
| 207 | // Scope |
| 208 | if (getScope() >= 0) |
| 209 | { |
| 210 | totalLength += prependNonNegativeIntegerBlock(block, Tlv::Scope, getScope()); |
| 211 | } |
| 212 | |
| 213 | // Nonce |
| 214 | getNonce(); // to ensure that Nonce is properly set |
| 215 | totalLength += block.prependBlock(m_nonce); |
| 216 | |
| 217 | // Selectors |
| 218 | if (hasSelectors()) |
| 219 | { |
| 220 | totalLength += getSelectors().wireEncode(block); |
| 221 | } |
| 222 | |
| 223 | // Name |
| 224 | totalLength += getName().wireEncode(block); |
| 225 | |
| 226 | totalLength += block.prependVarNumber(totalLength); |
| 227 | totalLength += block.prependVarNumber(Tlv::Interest); |
| 228 | return totalLength; |
| 229 | } |
| 230 | |
| 231 | const Block& |
| 232 | Interest::wireEncode() const |
| 233 | { |
| 234 | if (m_wire.hasWire()) |
| 235 | return m_wire; |
| 236 | |
| 237 | EncodingEstimator estimator; |
| 238 | size_t estimatedSize = wireEncode(estimator); |
| 239 | |
| 240 | EncodingBuffer buffer(estimatedSize, 0); |
| 241 | wireEncode(buffer); |
| 242 | |
| 243 | // to ensure that Nonce block points to the right memory location |
| 244 | const_cast<Interest*>(this)->wireDecode(buffer.block()); |
| 245 | |
| 246 | return m_wire; |
| 247 | } |
| 248 | |
| 249 | void |
| 250 | Interest::wireDecode(const Block& wire) |
| 251 | { |
| 252 | m_wire = wire; |
| 253 | m_wire.parse(); |
| 254 | |
| 255 | // Interest ::= INTEREST-TYPE TLV-LENGTH |
| 256 | // Name |
| 257 | // Selectors? |
| 258 | // Nonce |
| 259 | // Scope? |
| 260 | // InterestLifetime? |
| 261 | |
| 262 | if (m_wire.type() != Tlv::Interest) |
| 263 | throw Tlv::Error("Unexpected TLV number when decoding Interest"); |
| 264 | |
| 265 | // Name |
| 266 | m_name.wireDecode(m_wire.get(Tlv::Name)); |
| 267 | |
| 268 | // Selectors |
| 269 | Block::element_const_iterator val = m_wire.find(Tlv::Selectors); |
| 270 | if (val != m_wire.elements_end()) |
| 271 | { |
| 272 | m_selectors.wireDecode(*val); |
| 273 | } |
| 274 | else |
| 275 | m_selectors = Selectors(); |
| 276 | |
| 277 | // Nonce |
| 278 | m_nonce = m_wire.get(Tlv::Nonce); |
| 279 | |
| 280 | // Scope |
| 281 | val = m_wire.find(Tlv::Scope); |
| 282 | if (val != m_wire.elements_end()) |
| 283 | { |
| 284 | m_scope = readNonNegativeInteger(*val); |
| 285 | } |
| 286 | else |
| 287 | m_scope = -1; |
| 288 | |
| 289 | // InterestLifetime |
| 290 | val = m_wire.find(Tlv::InterestLifetime); |
| 291 | if (val != m_wire.elements_end()) |
| 292 | { |
| 293 | m_interestLifetime = time::milliseconds(readNonNegativeInteger(*val)); |
| 294 | } |
| 295 | else |
| 296 | { |
| 297 | m_interestLifetime = DEFAULT_INTEREST_LIFETIME; |
| 298 | } |
| 299 | } |
| 300 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 301 | std::ostream& |
| 302 | operator<<(std::ostream& os, const Interest& interest) |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 303 | { |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 304 | os << interest.getName(); |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 305 | |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 306 | char delim = '?'; |
| 307 | |
| 308 | if (interest.getMinSuffixComponents() >= 0) { |
| 309 | os << delim << "ndn.MinSuffixComponents=" << interest.getMinSuffixComponents(); |
| 310 | delim = '&'; |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 311 | } |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 312 | if (interest.getMaxSuffixComponents() >= 0) { |
| 313 | os << delim << "ndn.MaxSuffixComponents=" << interest.getMaxSuffixComponents(); |
| 314 | delim = '&'; |
Jeff Thompson | 37527d6 | 2013-08-21 11:15:54 -0700 | [diff] [blame] | 315 | } |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 316 | if (interest.getChildSelector() >= 0) { |
| 317 | os << delim << "ndn.ChildSelector=" << interest.getChildSelector(); |
| 318 | delim = '&'; |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 319 | } |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 320 | if (interest.getMustBeFresh()) { |
| 321 | os << delim << "ndn.MustBeFresh=" << interest.getMustBeFresh(); |
| 322 | delim = '&'; |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 323 | } |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 324 | if (interest.getScope() >= 0) { |
| 325 | os << delim << "ndn.Scope=" << interest.getScope(); |
| 326 | delim = '&'; |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 327 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 328 | if (interest.getInterestLifetime() >= time::milliseconds::zero() |
| 329 | && interest.getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) { |
Alexander Afanasyev | a0c5f83 | 2014-06-19 13:27:56 -0700 | [diff] [blame] | 330 | os << delim << "ndn.InterestLifetime=" << interest.getInterestLifetime().count(); |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 331 | delim = '&'; |
| 332 | } |
| 333 | |
Alexander Afanasyev | e881e93 | 2014-06-08 14:47:03 +0300 | [diff] [blame] | 334 | if (interest.hasNonce()) { |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 335 | os << delim << "ndn.Nonce=" << interest.getNonce(); |
| 336 | delim = '&'; |
| 337 | } |
| 338 | if (!interest.getExclude().empty()) { |
| 339 | os << delim << "ndn.Exclude=" << interest.getExclude(); |
| 340 | delim = '&'; |
| 341 | } |
| 342 | |
| 343 | return os; |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 344 | } |
| 345 | |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 346 | } |