blob: c1bed46e0ce10271ba92d6519131e0a4799c967c [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Jeff Thompson47eecfc2013-07-07 22:56:46 -07002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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 Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * Based on code originally written by Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompsonb7f95562013-07-03 18:36:42 -070022 */
23
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080024#include "interest.hpp"
Alexander Afanasyeve9fdb802014-02-05 17:36:51 -080025#include "util/random.hpp"
Junxiao Shi42c23622014-07-03 00:55:11 -070026#include "util/crypto.hpp"
Junxiao Shic2b8d242014-11-04 08:35:29 -070027#include "util/concepts.hpp"
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070028#include "data.hpp"
Alexander Afanasyev840139f2013-12-28 15:02:50 -080029
Jeff Thompsonb7f95562013-07-03 18:36:42 -070030namespace ndn {
Alexander Afanasyev84681982014-01-03 13:26:09 -080031
Junxiao Shic2b8d242014-11-04 08:35:29 -070032BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Interest>));
33BOOST_CONCEPT_ASSERT((WireEncodable<Interest>));
34BOOST_CONCEPT_ASSERT((WireDecodable<Interest>));
35static_assert(std::is_base_of<tlv::Error, Interest::Error>::value,
36 "Interest::Error must inherit from tlv::Error");
37
Junxiao Shi2af905b2014-11-27 13:10:54 -070038Interest::Interest()
39 : m_scope(-1)
40 , m_interestLifetime(time::milliseconds::min())
41{
42}
43
44Interest::Interest(const Name& name)
45 : m_name(name)
46 , m_scope(-1)
47 , m_interestLifetime(time::milliseconds::min())
48{
49}
50
51Interest::Interest(const Name& name, const time::milliseconds& interestLifetime)
52 : m_name(name)
53 , m_scope(-1)
54 , m_interestLifetime(interestLifetime)
55{
56}
57
58Interest::Interest(const Name& name,
59 const Selectors& selectors,
60 int scope,
61 const time::milliseconds& interestLifetime,
62 uint32_t nonce)
63 : m_name(name)
64 , m_selectors(selectors)
65 , m_scope(scope)
66 , m_interestLifetime(interestLifetime)
67{
68 if (nonce > 0) {
69 setNonce(nonce);
70 }
71}
72
73Interest::Interest(const Block& wire)
74{
75 wireDecode(wire);
76}
77
Alexander Afanasyeve881e932014-06-08 14:47:03 +030078uint32_t
Alexander Afanasyev840139f2013-12-28 15:02:50 -080079Interest::getNonce() const
80{
Alexander Afanasyeve881e932014-06-08 14:47:03 +030081 if (!m_nonce.hasWire())
82 const_cast<Interest*>(this)->setNonce(random::generateWord32());
Alexander Afanasyev840139f2013-12-28 15:02:50 -080083
Alexander Afanasyeve881e932014-06-08 14:47:03 +030084 if (m_nonce.value_size() == sizeof(uint32_t))
85 return *reinterpret_cast<const uint32_t*>(m_nonce.value());
86 else {
87 // for compatibility reasons. Should be removed eventually
88 return readNonNegativeInteger(m_nonce);
89 }
Alexander Afanasyev840139f2013-12-28 15:02:50 -080090}
91
Alexander Afanasyeve881e932014-06-08 14:47:03 +030092Interest&
93Interest::setNonce(uint32_t nonce)
94{
95 if (m_wire.hasWire() && m_nonce.value_size() == sizeof(uint32_t)) {
96 std::memcpy(const_cast<uint8_t*>(m_nonce.value()), &nonce, sizeof(nonce));
97 }
98 else {
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060099 m_nonce = dataBlock(tlv::Nonce,
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300100 reinterpret_cast<const uint8_t*>(&nonce),
101 sizeof(nonce));
102 m_wire.reset();
103 }
104 return *this;
105}
Alexander Afanasyev840139f2013-12-28 15:02:50 -0800106
Alexander Afanasyevc3932172014-07-10 18:53:56 -0700107void
108Interest::refreshNonce()
109{
110 if (!hasNonce())
111 return;
112
113 uint32_t oldNonce = getNonce();
114 uint32_t newNonce = oldNonce;
115 while (newNonce == oldNonce)
116 newNonce = random::generateWord32();
117
118 setNonce(newNonce);
119}
120
Alexander Afanasyev84681982014-01-03 13:26:09 -0800121bool
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700122Interest::matchesName(const Name& name) const
Jeff Thompson25b4e612013-10-10 16:03:24 -0700123{
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700124 if (name.size() < m_name.size())
125 return false;
126
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800127 if (!m_name.isPrefixOf(name))
Alexander Afanasyev84681982014-01-03 13:26:09 -0800128 return false;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700129
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800130 if (getMinSuffixComponents() >= 0 &&
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700131 // name must include implicit digest
132 !(name.size() - m_name.size() >= static_cast<size_t>(getMinSuffixComponents())))
Alexander Afanasyev84681982014-01-03 13:26:09 -0800133 return false;
134
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800135 if (getMaxSuffixComponents() >= 0 &&
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700136 // name must include implicit digest
137 !(name.size() - m_name.size() <= static_cast<size_t>(getMaxSuffixComponents())))
Alexander Afanasyev84681982014-01-03 13:26:09 -0800138 return false;
139
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700140 if (!getExclude().empty() &&
141 name.size() > m_name.size() &&
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800142 getExclude().isExcluded(name[m_name.size()]))
Alexander Afanasyev84681982014-01-03 13:26:09 -0800143 return false;
144
145 return true;
Jeff Thompson25b4e612013-10-10 16:03:24 -0700146}
147
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700148bool
149Interest::matchesData(const Data& data) const
150{
Junxiao Shi42c23622014-07-03 00:55:11 -0700151 size_t interestNameLength = m_name.size();
152 const Name& dataName = data.getName();
153 size_t fullNameLength = dataName.size() + 1;
154
155 // check MinSuffixComponents
156 bool hasMinSuffixComponents = getMinSuffixComponents() >= 0;
157 size_t minSuffixComponents = hasMinSuffixComponents ?
158 static_cast<size_t>(getMinSuffixComponents()) : 0;
159 if (!(interestNameLength + minSuffixComponents <= fullNameLength))
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700160 return false;
Junxiao Shi42c23622014-07-03 00:55:11 -0700161
162 // check MaxSuffixComponents
163 bool hasMaxSuffixComponents = getMaxSuffixComponents() >= 0;
164 if (hasMaxSuffixComponents &&
165 !(interestNameLength + getMaxSuffixComponents() >= fullNameLength))
166 return false;
167
168 // check prefix
169 if (interestNameLength == fullNameLength) {
Alexander Afanasyev56860f52014-11-07 11:51:17 -0800170 if (m_name.get(-1).isImplicitSha256Digest()) {
171 if (m_name != data.getFullName())
Junxiao Shi42c23622014-07-03 00:55:11 -0700172 return false;
173 }
174 else {
175 // Interest Name is same length as Data full Name, but last component isn't digest
176 // so there's no possibility of matching
177 return false;
178 }
179 }
180 else {
181 // Interest Name is a strict prefix of Data full Name
182 if (!m_name.isPrefixOf(dataName))
183 return false;
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700184 }
185
Junxiao Shi42c23622014-07-03 00:55:11 -0700186 // check Exclude
187 // Exclude won't be violated if Interest Name is same as Data full Name
188 if (!getExclude().empty() && fullNameLength > interestNameLength) {
189 if (interestNameLength == fullNameLength - 1) {
190 // component to exclude is the digest
191 if (getExclude().isExcluded(data.getFullName().get(interestNameLength)))
192 return false;
193 // There's opportunity to inspect the Exclude filter and determine whether
194 // the digest would make a difference.
195 // eg. "Exclude=<Any>AA" won't exclude any digest - fullName not needed
196 // "Exclude=ZZ<Any>" excludes all digests - fullName not needed
197 // "Exclude=<Any>80000000000000000000000000000000"
198 // excludes half of the digests - fullName required
199 // But Interests that contain the exact Data Name before digest and also
200 // contain Exclude filter is too rare to optimize for, so we request
201 // fullName no mater what's in the Exclude filter.
202 }
203 else {
204 // component to exclude is not the digest
205 if (getExclude().isExcluded(dataName.get(interestNameLength)))
206 return false;
207 }
208 }
209
210 // check PublisherPublicKeyLocator
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700211 const KeyLocator& publisherPublicKeyLocator = this->getPublisherPublicKeyLocator();
212 if (!publisherPublicKeyLocator.empty()) {
213 const Signature& signature = data.getSignature();
214 const Block& signatureInfo = signature.getInfo();
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600215 Block::element_const_iterator it = signatureInfo.find(tlv::KeyLocator);
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700216 if (it == signatureInfo.elements_end()) {
217 return false;
218 }
219 if (publisherPublicKeyLocator.wireEncode() != *it) {
220 return false;
221 }
222 }
223
224 return true;
225}
226
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700227template<bool T>
228size_t
229Interest::wireEncode(EncodingImpl<T>& block) const
230{
231 size_t totalLength = 0;
232
233 // Interest ::= INTEREST-TYPE TLV-LENGTH
234 // Name
235 // Selectors?
236 // Nonce
237 // Scope?
238 // InterestLifetime?
239
240 // (reverse encoding)
241
242 // InterestLifetime
243 if (getInterestLifetime() >= time::milliseconds::zero() &&
244 getInterestLifetime() != DEFAULT_INTEREST_LIFETIME)
245 {
246 totalLength += prependNonNegativeIntegerBlock(block,
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600247 tlv::InterestLifetime,
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700248 getInterestLifetime().count());
249 }
250
251 // Scope
252 if (getScope() >= 0)
253 {
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600254 totalLength += prependNonNegativeIntegerBlock(block, tlv::Scope, getScope());
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700255 }
256
257 // Nonce
258 getNonce(); // to ensure that Nonce is properly set
259 totalLength += block.prependBlock(m_nonce);
260
261 // Selectors
262 if (hasSelectors())
263 {
264 totalLength += getSelectors().wireEncode(block);
265 }
266
267 // Name
268 totalLength += getName().wireEncode(block);
269
270 totalLength += block.prependVarNumber(totalLength);
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600271 totalLength += block.prependVarNumber(tlv::Interest);
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700272 return totalLength;
273}
274
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700275template size_t
276Interest::wireEncode<true>(EncodingImpl<true>& block) const;
277
278template size_t
279Interest::wireEncode<false>(EncodingImpl<false>& block) const;
280
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700281const Block&
282Interest::wireEncode() const
283{
284 if (m_wire.hasWire())
285 return m_wire;
286
287 EncodingEstimator estimator;
288 size_t estimatedSize = wireEncode(estimator);
289
290 EncodingBuffer buffer(estimatedSize, 0);
291 wireEncode(buffer);
292
293 // to ensure that Nonce block points to the right memory location
294 const_cast<Interest*>(this)->wireDecode(buffer.block());
295
296 return m_wire;
297}
298
299void
300Interest::wireDecode(const Block& wire)
301{
302 m_wire = wire;
303 m_wire.parse();
304
305 // Interest ::= INTEREST-TYPE TLV-LENGTH
306 // Name
307 // Selectors?
308 // Nonce
309 // Scope?
310 // InterestLifetime?
311
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600312 if (m_wire.type() != tlv::Interest)
Junxiao Shic2b8d242014-11-04 08:35:29 -0700313 throw Error("Unexpected TLV number when decoding Interest");
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700314
315 // Name
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600316 m_name.wireDecode(m_wire.get(tlv::Name));
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700317
318 // Selectors
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600319 Block::element_const_iterator val = m_wire.find(tlv::Selectors);
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700320 if (val != m_wire.elements_end())
321 {
322 m_selectors.wireDecode(*val);
323 }
324 else
325 m_selectors = Selectors();
326
327 // Nonce
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600328 m_nonce = m_wire.get(tlv::Nonce);
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700329
330 // Scope
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600331 val = m_wire.find(tlv::Scope);
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700332 if (val != m_wire.elements_end())
333 {
334 m_scope = readNonNegativeInteger(*val);
335 }
336 else
337 m_scope = -1;
338
339 // InterestLifetime
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600340 val = m_wire.find(tlv::InterestLifetime);
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700341 if (val != m_wire.elements_end())
342 {
343 m_interestLifetime = time::milliseconds(readNonNegativeInteger(*val));
344 }
345 else
346 {
347 m_interestLifetime = DEFAULT_INTEREST_LIFETIME;
348 }
349}
350
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700351std::ostream&
352operator<<(std::ostream& os, const Interest& interest)
Jeff Thompsonfe556862013-07-09 13:52:55 -0700353{
Alexander Afanasyev84681982014-01-03 13:26:09 -0800354 os << interest.getName();
Jeff Thompsonfe556862013-07-09 13:52:55 -0700355
Alexander Afanasyev84681982014-01-03 13:26:09 -0800356 char delim = '?';
357
358 if (interest.getMinSuffixComponents() >= 0) {
359 os << delim << "ndn.MinSuffixComponents=" << interest.getMinSuffixComponents();
360 delim = '&';
Jeff Thompsonfe556862013-07-09 13:52:55 -0700361 }
Alexander Afanasyev84681982014-01-03 13:26:09 -0800362 if (interest.getMaxSuffixComponents() >= 0) {
363 os << delim << "ndn.MaxSuffixComponents=" << interest.getMaxSuffixComponents();
364 delim = '&';
Jeff Thompson37527d62013-08-21 11:15:54 -0700365 }
Alexander Afanasyev84681982014-01-03 13:26:09 -0800366 if (interest.getChildSelector() >= 0) {
367 os << delim << "ndn.ChildSelector=" << interest.getChildSelector();
368 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -0800369 }
Alexander Afanasyev84681982014-01-03 13:26:09 -0800370 if (interest.getMustBeFresh()) {
371 os << delim << "ndn.MustBeFresh=" << interest.getMustBeFresh();
372 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -0800373 }
Alexander Afanasyev84681982014-01-03 13:26:09 -0800374 if (interest.getScope() >= 0) {
375 os << delim << "ndn.Scope=" << interest.getScope();
376 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -0800377 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700378 if (interest.getInterestLifetime() >= time::milliseconds::zero()
379 && interest.getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) {
Alexander Afanasyeva0c5f832014-06-19 13:27:56 -0700380 os << delim << "ndn.InterestLifetime=" << interest.getInterestLifetime().count();
Alexander Afanasyev84681982014-01-03 13:26:09 -0800381 delim = '&';
382 }
383
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300384 if (interest.hasNonce()) {
Alexander Afanasyev84681982014-01-03 13:26:09 -0800385 os << delim << "ndn.Nonce=" << interest.getNonce();
386 delim = '&';
387 }
388 if (!interest.getExclude().empty()) {
389 os << delim << "ndn.Exclude=" << interest.getExclude();
390 delim = '&';
391 }
392
393 return os;
Jeff Thompson13e280b2013-12-03 13:12:23 -0800394}
395
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700396}