blob: 304afcb071f46109147f54f6a64c64085a51047a [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
Alexander Afanasyeve881e932014-06-08 14:47:03 +030038uint32_t
Alexander Afanasyev840139f2013-12-28 15:02:50 -080039Interest::getNonce() const
40{
Alexander Afanasyeve881e932014-06-08 14:47:03 +030041 if (!m_nonce.hasWire())
42 const_cast<Interest*>(this)->setNonce(random::generateWord32());
Alexander Afanasyev840139f2013-12-28 15:02:50 -080043
Alexander Afanasyeve881e932014-06-08 14:47:03 +030044 if (m_nonce.value_size() == sizeof(uint32_t))
45 return *reinterpret_cast<const uint32_t*>(m_nonce.value());
46 else {
47 // for compatibility reasons. Should be removed eventually
48 return readNonNegativeInteger(m_nonce);
49 }
Alexander Afanasyev840139f2013-12-28 15:02:50 -080050}
51
Alexander Afanasyeve881e932014-06-08 14:47:03 +030052Interest&
53Interest::setNonce(uint32_t nonce)
54{
55 if (m_wire.hasWire() && m_nonce.value_size() == sizeof(uint32_t)) {
56 std::memcpy(const_cast<uint8_t*>(m_nonce.value()), &nonce, sizeof(nonce));
57 }
58 else {
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060059 m_nonce = dataBlock(tlv::Nonce,
Alexander Afanasyeve881e932014-06-08 14:47:03 +030060 reinterpret_cast<const uint8_t*>(&nonce),
61 sizeof(nonce));
62 m_wire.reset();
63 }
64 return *this;
65}
Alexander Afanasyev840139f2013-12-28 15:02:50 -080066
Alexander Afanasyevc3932172014-07-10 18:53:56 -070067void
68Interest::refreshNonce()
69{
70 if (!hasNonce())
71 return;
72
73 uint32_t oldNonce = getNonce();
74 uint32_t newNonce = oldNonce;
75 while (newNonce == oldNonce)
76 newNonce = random::generateWord32();
77
78 setNonce(newNonce);
79}
80
Alexander Afanasyev84681982014-01-03 13:26:09 -080081bool
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070082Interest::matchesName(const Name& name) const
Jeff Thompson25b4e612013-10-10 16:03:24 -070083{
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070084 if (name.size() < m_name.size())
85 return false;
86
Alexander Afanasyevc348f832014-02-17 16:35:17 -080087 if (!m_name.isPrefixOf(name))
Alexander Afanasyev84681982014-01-03 13:26:09 -080088 return false;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070089
Alexander Afanasyevc348f832014-02-17 16:35:17 -080090 if (getMinSuffixComponents() >= 0 &&
Alexander Afanasyev3b703102014-06-13 17:01:14 -070091 // name must include implicit digest
92 !(name.size() - m_name.size() >= static_cast<size_t>(getMinSuffixComponents())))
Alexander Afanasyev84681982014-01-03 13:26:09 -080093 return false;
94
Alexander Afanasyevc348f832014-02-17 16:35:17 -080095 if (getMaxSuffixComponents() >= 0 &&
Alexander Afanasyev3b703102014-06-13 17:01:14 -070096 // name must include implicit digest
97 !(name.size() - m_name.size() <= static_cast<size_t>(getMaxSuffixComponents())))
Alexander Afanasyev84681982014-01-03 13:26:09 -080098 return false;
99
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700100 if (!getExclude().empty() &&
101 name.size() > m_name.size() &&
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800102 getExclude().isExcluded(name[m_name.size()]))
Alexander Afanasyev84681982014-01-03 13:26:09 -0800103 return false;
104
105 return true;
Jeff Thompson25b4e612013-10-10 16:03:24 -0700106}
107
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700108bool
109Interest::matchesData(const Data& data) const
110{
Junxiao Shi42c23622014-07-03 00:55:11 -0700111 size_t interestNameLength = m_name.size();
112 const Name& dataName = data.getName();
113 size_t fullNameLength = dataName.size() + 1;
114
115 // check MinSuffixComponents
116 bool hasMinSuffixComponents = getMinSuffixComponents() >= 0;
117 size_t minSuffixComponents = hasMinSuffixComponents ?
118 static_cast<size_t>(getMinSuffixComponents()) : 0;
119 if (!(interestNameLength + minSuffixComponents <= fullNameLength))
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700120 return false;
Junxiao Shi42c23622014-07-03 00:55:11 -0700121
122 // check MaxSuffixComponents
123 bool hasMaxSuffixComponents = getMaxSuffixComponents() >= 0;
124 if (hasMaxSuffixComponents &&
125 !(interestNameLength + getMaxSuffixComponents() >= fullNameLength))
126 return false;
127
128 // check prefix
129 if (interestNameLength == fullNameLength) {
Alexander Afanasyev56860f52014-11-07 11:51:17 -0800130 if (m_name.get(-1).isImplicitSha256Digest()) {
131 if (m_name != data.getFullName())
Junxiao Shi42c23622014-07-03 00:55:11 -0700132 return false;
133 }
134 else {
135 // Interest Name is same length as Data full Name, but last component isn't digest
136 // so there's no possibility of matching
137 return false;
138 }
139 }
140 else {
141 // Interest Name is a strict prefix of Data full Name
142 if (!m_name.isPrefixOf(dataName))
143 return false;
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700144 }
145
Junxiao Shi42c23622014-07-03 00:55:11 -0700146 // check Exclude
147 // Exclude won't be violated if Interest Name is same as Data full Name
148 if (!getExclude().empty() && fullNameLength > interestNameLength) {
149 if (interestNameLength == fullNameLength - 1) {
150 // component to exclude is the digest
151 if (getExclude().isExcluded(data.getFullName().get(interestNameLength)))
152 return false;
153 // There's opportunity to inspect the Exclude filter and determine whether
154 // the digest would make a difference.
155 // eg. "Exclude=<Any>AA" won't exclude any digest - fullName not needed
156 // "Exclude=ZZ<Any>" excludes all digests - fullName not needed
157 // "Exclude=<Any>80000000000000000000000000000000"
158 // excludes half of the digests - fullName required
159 // But Interests that contain the exact Data Name before digest and also
160 // contain Exclude filter is too rare to optimize for, so we request
161 // fullName no mater what's in the Exclude filter.
162 }
163 else {
164 // component to exclude is not the digest
165 if (getExclude().isExcluded(dataName.get(interestNameLength)))
166 return false;
167 }
168 }
169
170 // check PublisherPublicKeyLocator
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700171 const KeyLocator& publisherPublicKeyLocator = this->getPublisherPublicKeyLocator();
172 if (!publisherPublicKeyLocator.empty()) {
173 const Signature& signature = data.getSignature();
174 const Block& signatureInfo = signature.getInfo();
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600175 Block::element_const_iterator it = signatureInfo.find(tlv::KeyLocator);
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700176 if (it == signatureInfo.elements_end()) {
177 return false;
178 }
179 if (publisherPublicKeyLocator.wireEncode() != *it) {
180 return false;
181 }
182 }
183
184 return true;
185}
186
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700187template<bool T>
188size_t
189Interest::wireEncode(EncodingImpl<T>& block) const
190{
191 size_t totalLength = 0;
192
193 // Interest ::= INTEREST-TYPE TLV-LENGTH
194 // Name
195 // Selectors?
196 // Nonce
197 // Scope?
198 // InterestLifetime?
199
200 // (reverse encoding)
201
202 // InterestLifetime
203 if (getInterestLifetime() >= time::milliseconds::zero() &&
204 getInterestLifetime() != DEFAULT_INTEREST_LIFETIME)
205 {
206 totalLength += prependNonNegativeIntegerBlock(block,
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600207 tlv::InterestLifetime,
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700208 getInterestLifetime().count());
209 }
210
211 // Scope
212 if (getScope() >= 0)
213 {
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600214 totalLength += prependNonNegativeIntegerBlock(block, tlv::Scope, getScope());
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700215 }
216
217 // Nonce
218 getNonce(); // to ensure that Nonce is properly set
219 totalLength += block.prependBlock(m_nonce);
220
221 // Selectors
222 if (hasSelectors())
223 {
224 totalLength += getSelectors().wireEncode(block);
225 }
226
227 // Name
228 totalLength += getName().wireEncode(block);
229
230 totalLength += block.prependVarNumber(totalLength);
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600231 totalLength += block.prependVarNumber(tlv::Interest);
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700232 return totalLength;
233}
234
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700235template size_t
236Interest::wireEncode<true>(EncodingImpl<true>& block) const;
237
238template size_t
239Interest::wireEncode<false>(EncodingImpl<false>& block) const;
240
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700241const Block&
242Interest::wireEncode() const
243{
244 if (m_wire.hasWire())
245 return m_wire;
246
247 EncodingEstimator estimator;
248 size_t estimatedSize = wireEncode(estimator);
249
250 EncodingBuffer buffer(estimatedSize, 0);
251 wireEncode(buffer);
252
253 // to ensure that Nonce block points to the right memory location
254 const_cast<Interest*>(this)->wireDecode(buffer.block());
255
256 return m_wire;
257}
258
259void
260Interest::wireDecode(const Block& wire)
261{
262 m_wire = wire;
263 m_wire.parse();
264
265 // Interest ::= INTEREST-TYPE TLV-LENGTH
266 // Name
267 // Selectors?
268 // Nonce
269 // Scope?
270 // InterestLifetime?
271
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600272 if (m_wire.type() != tlv::Interest)
Junxiao Shic2b8d242014-11-04 08:35:29 -0700273 throw Error("Unexpected TLV number when decoding Interest");
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700274
275 // Name
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600276 m_name.wireDecode(m_wire.get(tlv::Name));
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700277
278 // Selectors
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600279 Block::element_const_iterator val = m_wire.find(tlv::Selectors);
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700280 if (val != m_wire.elements_end())
281 {
282 m_selectors.wireDecode(*val);
283 }
284 else
285 m_selectors = Selectors();
286
287 // Nonce
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600288 m_nonce = m_wire.get(tlv::Nonce);
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700289
290 // Scope
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600291 val = m_wire.find(tlv::Scope);
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700292 if (val != m_wire.elements_end())
293 {
294 m_scope = readNonNegativeInteger(*val);
295 }
296 else
297 m_scope = -1;
298
299 // InterestLifetime
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600300 val = m_wire.find(tlv::InterestLifetime);
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700301 if (val != m_wire.elements_end())
302 {
303 m_interestLifetime = time::milliseconds(readNonNegativeInteger(*val));
304 }
305 else
306 {
307 m_interestLifetime = DEFAULT_INTEREST_LIFETIME;
308 }
309}
310
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700311std::ostream&
312operator<<(std::ostream& os, const Interest& interest)
Jeff Thompsonfe556862013-07-09 13:52:55 -0700313{
Alexander Afanasyev84681982014-01-03 13:26:09 -0800314 os << interest.getName();
Jeff Thompsonfe556862013-07-09 13:52:55 -0700315
Alexander Afanasyev84681982014-01-03 13:26:09 -0800316 char delim = '?';
317
318 if (interest.getMinSuffixComponents() >= 0) {
319 os << delim << "ndn.MinSuffixComponents=" << interest.getMinSuffixComponents();
320 delim = '&';
Jeff Thompsonfe556862013-07-09 13:52:55 -0700321 }
Alexander Afanasyev84681982014-01-03 13:26:09 -0800322 if (interest.getMaxSuffixComponents() >= 0) {
323 os << delim << "ndn.MaxSuffixComponents=" << interest.getMaxSuffixComponents();
324 delim = '&';
Jeff Thompson37527d62013-08-21 11:15:54 -0700325 }
Alexander Afanasyev84681982014-01-03 13:26:09 -0800326 if (interest.getChildSelector() >= 0) {
327 os << delim << "ndn.ChildSelector=" << interest.getChildSelector();
328 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -0800329 }
Alexander Afanasyev84681982014-01-03 13:26:09 -0800330 if (interest.getMustBeFresh()) {
331 os << delim << "ndn.MustBeFresh=" << interest.getMustBeFresh();
332 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -0800333 }
Alexander Afanasyev84681982014-01-03 13:26:09 -0800334 if (interest.getScope() >= 0) {
335 os << delim << "ndn.Scope=" << interest.getScope();
336 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -0800337 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700338 if (interest.getInterestLifetime() >= time::milliseconds::zero()
339 && interest.getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) {
Alexander Afanasyeva0c5f832014-06-19 13:27:56 -0700340 os << delim << "ndn.InterestLifetime=" << interest.getInterestLifetime().count();
Alexander Afanasyev84681982014-01-03 13:26:09 -0800341 delim = '&';
342 }
343
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300344 if (interest.hasNonce()) {
Alexander Afanasyev84681982014-01-03 13:26:09 -0800345 os << delim << "ndn.Nonce=" << interest.getNonce();
346 delim = '&';
347 }
348 if (!interest.getExclude().empty()) {
349 os << delim << "ndn.Exclude=" << interest.getExclude();
350 delim = '&';
351 }
352
353 return os;
Jeff Thompson13e280b2013-12-03 13:12:23 -0800354}
355
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700356}