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 "common.hpp" |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 25 | |
Alexander Afanasyev | 09c613f | 2014-01-29 00:23:58 -0800 | [diff] [blame] | 26 | #include "interest.hpp" |
Alexander Afanasyev | e9fdb80 | 2014-02-05 17:36:51 -0800 | [diff] [blame] | 27 | #include "util/random.hpp" |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 28 | #include "data.hpp" |
Alexander Afanasyev | 840139f | 2013-12-28 15:02:50 -0800 | [diff] [blame] | 29 | |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 30 | namespace ndn { |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 31 | |
Alexander Afanasyev | e881e93 | 2014-06-08 14:47:03 +0300 | [diff] [blame] | 32 | uint32_t |
Alexander Afanasyev | 840139f | 2013-12-28 15:02:50 -0800 | [diff] [blame] | 33 | Interest::getNonce() const |
| 34 | { |
Alexander Afanasyev | e881e93 | 2014-06-08 14:47:03 +0300 | [diff] [blame] | 35 | if (!m_nonce.hasWire()) |
| 36 | const_cast<Interest*>(this)->setNonce(random::generateWord32()); |
Alexander Afanasyev | 840139f | 2013-12-28 15:02:50 -0800 | [diff] [blame] | 37 | |
Alexander Afanasyev | e881e93 | 2014-06-08 14:47:03 +0300 | [diff] [blame] | 38 | if (m_nonce.value_size() == sizeof(uint32_t)) |
| 39 | return *reinterpret_cast<const uint32_t*>(m_nonce.value()); |
| 40 | else { |
| 41 | // for compatibility reasons. Should be removed eventually |
| 42 | return readNonNegativeInteger(m_nonce); |
| 43 | } |
Alexander Afanasyev | 840139f | 2013-12-28 15:02:50 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Alexander Afanasyev | e881e93 | 2014-06-08 14:47:03 +0300 | [diff] [blame] | 46 | Interest& |
| 47 | Interest::setNonce(uint32_t nonce) |
| 48 | { |
| 49 | if (m_wire.hasWire() && m_nonce.value_size() == sizeof(uint32_t)) { |
| 50 | std::memcpy(const_cast<uint8_t*>(m_nonce.value()), &nonce, sizeof(nonce)); |
| 51 | } |
| 52 | else { |
| 53 | m_nonce = dataBlock(Tlv::Nonce, |
| 54 | reinterpret_cast<const uint8_t*>(&nonce), |
| 55 | sizeof(nonce)); |
| 56 | m_wire.reset(); |
| 57 | } |
| 58 | return *this; |
| 59 | } |
Alexander Afanasyev | 840139f | 2013-12-28 15:02:50 -0800 | [diff] [blame] | 60 | |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 61 | bool |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 62 | Interest::matchesName(const Name& name) const |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 63 | { |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 64 | if (name.size() < m_name.size()) |
| 65 | return false; |
| 66 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 67 | if (!m_name.isPrefixOf(name)) |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 68 | return false; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 69 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 70 | if (getMinSuffixComponents() >= 0 && |
Alexander Afanasyev | 3b70310 | 2014-06-13 17:01:14 -0700 | [diff] [blame] | 71 | // name must include implicit digest |
| 72 | !(name.size() - m_name.size() >= static_cast<size_t>(getMinSuffixComponents()))) |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 73 | return false; |
| 74 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 75 | if (getMaxSuffixComponents() >= 0 && |
Alexander Afanasyev | 3b70310 | 2014-06-13 17:01:14 -0700 | [diff] [blame] | 76 | // name must include implicit digest |
| 77 | !(name.size() - m_name.size() <= static_cast<size_t>(getMaxSuffixComponents()))) |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 78 | return false; |
| 79 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 80 | if (!getExclude().empty() && |
| 81 | name.size() > m_name.size() && |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 82 | getExclude().isExcluded(name[m_name.size()])) |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 83 | return false; |
| 84 | |
| 85 | return true; |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 88 | bool |
| 89 | Interest::matchesData(const Data& data) const |
| 90 | { |
Alexander Afanasyev | 3b70310 | 2014-06-13 17:01:14 -0700 | [diff] [blame] | 91 | if (!this->matchesName(data.getFullName())) { |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 92 | return false; |
| 93 | } |
| 94 | |
| 95 | const KeyLocator& publisherPublicKeyLocator = this->getPublisherPublicKeyLocator(); |
| 96 | if (!publisherPublicKeyLocator.empty()) { |
| 97 | const Signature& signature = data.getSignature(); |
| 98 | const Block& signatureInfo = signature.getInfo(); |
| 99 | Block::element_const_iterator it = signatureInfo.find(Tlv::KeyLocator); |
| 100 | if (it == signatureInfo.elements_end()) { |
| 101 | return false; |
| 102 | } |
| 103 | if (publisherPublicKeyLocator.wireEncode() != *it) { |
| 104 | return false; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return true; |
| 109 | } |
| 110 | |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 111 | template<bool T> |
| 112 | size_t |
| 113 | Interest::wireEncode(EncodingImpl<T>& block) const |
| 114 | { |
| 115 | size_t totalLength = 0; |
| 116 | |
| 117 | // Interest ::= INTEREST-TYPE TLV-LENGTH |
| 118 | // Name |
| 119 | // Selectors? |
| 120 | // Nonce |
| 121 | // Scope? |
| 122 | // InterestLifetime? |
| 123 | |
| 124 | // (reverse encoding) |
| 125 | |
| 126 | // InterestLifetime |
| 127 | if (getInterestLifetime() >= time::milliseconds::zero() && |
| 128 | getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) |
| 129 | { |
| 130 | totalLength += prependNonNegativeIntegerBlock(block, |
| 131 | Tlv::InterestLifetime, |
| 132 | getInterestLifetime().count()); |
| 133 | } |
| 134 | |
| 135 | // Scope |
| 136 | if (getScope() >= 0) |
| 137 | { |
| 138 | totalLength += prependNonNegativeIntegerBlock(block, Tlv::Scope, getScope()); |
| 139 | } |
| 140 | |
| 141 | // Nonce |
| 142 | getNonce(); // to ensure that Nonce is properly set |
| 143 | totalLength += block.prependBlock(m_nonce); |
| 144 | |
| 145 | // Selectors |
| 146 | if (hasSelectors()) |
| 147 | { |
| 148 | totalLength += getSelectors().wireEncode(block); |
| 149 | } |
| 150 | |
| 151 | // Name |
| 152 | totalLength += getName().wireEncode(block); |
| 153 | |
| 154 | totalLength += block.prependVarNumber(totalLength); |
| 155 | totalLength += block.prependVarNumber(Tlv::Interest); |
| 156 | return totalLength; |
| 157 | } |
| 158 | |
| 159 | const Block& |
| 160 | Interest::wireEncode() const |
| 161 | { |
| 162 | if (m_wire.hasWire()) |
| 163 | return m_wire; |
| 164 | |
| 165 | EncodingEstimator estimator; |
| 166 | size_t estimatedSize = wireEncode(estimator); |
| 167 | |
| 168 | EncodingBuffer buffer(estimatedSize, 0); |
| 169 | wireEncode(buffer); |
| 170 | |
| 171 | // to ensure that Nonce block points to the right memory location |
| 172 | const_cast<Interest*>(this)->wireDecode(buffer.block()); |
| 173 | |
| 174 | return m_wire; |
| 175 | } |
| 176 | |
| 177 | void |
| 178 | Interest::wireDecode(const Block& wire) |
| 179 | { |
| 180 | m_wire = wire; |
| 181 | m_wire.parse(); |
| 182 | |
| 183 | // Interest ::= INTEREST-TYPE TLV-LENGTH |
| 184 | // Name |
| 185 | // Selectors? |
| 186 | // Nonce |
| 187 | // Scope? |
| 188 | // InterestLifetime? |
| 189 | |
| 190 | if (m_wire.type() != Tlv::Interest) |
| 191 | throw Tlv::Error("Unexpected TLV number when decoding Interest"); |
| 192 | |
| 193 | // Name |
| 194 | m_name.wireDecode(m_wire.get(Tlv::Name)); |
| 195 | |
| 196 | // Selectors |
| 197 | Block::element_const_iterator val = m_wire.find(Tlv::Selectors); |
| 198 | if (val != m_wire.elements_end()) |
| 199 | { |
| 200 | m_selectors.wireDecode(*val); |
| 201 | } |
| 202 | else |
| 203 | m_selectors = Selectors(); |
| 204 | |
| 205 | // Nonce |
| 206 | m_nonce = m_wire.get(Tlv::Nonce); |
| 207 | |
| 208 | // Scope |
| 209 | val = m_wire.find(Tlv::Scope); |
| 210 | if (val != m_wire.elements_end()) |
| 211 | { |
| 212 | m_scope = readNonNegativeInteger(*val); |
| 213 | } |
| 214 | else |
| 215 | m_scope = -1; |
| 216 | |
| 217 | // InterestLifetime |
| 218 | val = m_wire.find(Tlv::InterestLifetime); |
| 219 | if (val != m_wire.elements_end()) |
| 220 | { |
| 221 | m_interestLifetime = time::milliseconds(readNonNegativeInteger(*val)); |
| 222 | } |
| 223 | else |
| 224 | { |
| 225 | m_interestLifetime = DEFAULT_INTEREST_LIFETIME; |
| 226 | } |
| 227 | } |
| 228 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 229 | std::ostream& |
| 230 | operator<<(std::ostream& os, const Interest& interest) |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 231 | { |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 232 | os << interest.getName(); |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 233 | |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 234 | char delim = '?'; |
| 235 | |
| 236 | if (interest.getMinSuffixComponents() >= 0) { |
| 237 | os << delim << "ndn.MinSuffixComponents=" << interest.getMinSuffixComponents(); |
| 238 | delim = '&'; |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 239 | } |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 240 | if (interest.getMaxSuffixComponents() >= 0) { |
| 241 | os << delim << "ndn.MaxSuffixComponents=" << interest.getMaxSuffixComponents(); |
| 242 | delim = '&'; |
Jeff Thompson | 37527d6 | 2013-08-21 11:15:54 -0700 | [diff] [blame] | 243 | } |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 244 | if (interest.getChildSelector() >= 0) { |
| 245 | os << delim << "ndn.ChildSelector=" << interest.getChildSelector(); |
| 246 | delim = '&'; |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 247 | } |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 248 | if (interest.getMustBeFresh()) { |
| 249 | os << delim << "ndn.MustBeFresh=" << interest.getMustBeFresh(); |
| 250 | delim = '&'; |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 251 | } |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 252 | if (interest.getScope() >= 0) { |
| 253 | os << delim << "ndn.Scope=" << interest.getScope(); |
| 254 | delim = '&'; |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 255 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 256 | if (interest.getInterestLifetime() >= time::milliseconds::zero() |
| 257 | && interest.getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) { |
Alexander Afanasyev | a0c5f83 | 2014-06-19 13:27:56 -0700 | [diff] [blame^] | 258 | os << delim << "ndn.InterestLifetime=" << interest.getInterestLifetime().count(); |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 259 | delim = '&'; |
| 260 | } |
| 261 | |
Alexander Afanasyev | e881e93 | 2014-06-08 14:47:03 +0300 | [diff] [blame] | 262 | if (interest.hasNonce()) { |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 263 | os << delim << "ndn.Nonce=" << interest.getNonce(); |
| 264 | delim = '&'; |
| 265 | } |
| 266 | if (!interest.getExclude().empty()) { |
| 267 | os << delim << "ndn.Exclude=" << interest.getExclude(); |
| 268 | delim = '&'; |
| 269 | } |
| 270 | |
| 271 | return os; |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 272 | } |
| 273 | |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 274 | } |