blob: 3f8352b85e4c16767c7b25963cd3e52bcefde8af [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi899277a2017-07-07 22:12:12 +00002/*
Alexander Afanasyev1013fd02017-01-03 13:19:03 -08003 * Copyright (c) 2013-2017 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.
Jeff Thompsonb7f95562013-07-03 18:36:42 -070020 */
21
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080022#include "interest.hpp"
Alexander Afanasyeve9fdb802014-02-05 17:36:51 -080023#include "util/random.hpp"
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070024#include "data.hpp"
Alexander Afanasyev840139f2013-12-28 15:02:50 -080025
Davide Pesaventoe1789892017-02-26 15:50:52 -050026#include <cstring>
27
Jeff Thompsonb7f95562013-07-03 18:36:42 -070028namespace ndn {
Alexander Afanasyev84681982014-01-03 13:26:09 -080029
Junxiao Shic2b8d242014-11-04 08:35:29 -070030BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Interest>));
31BOOST_CONCEPT_ASSERT((WireEncodable<Interest>));
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070032BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<Interest>));
Junxiao Shic2b8d242014-11-04 08:35:29 -070033BOOST_CONCEPT_ASSERT((WireDecodable<Interest>));
34static_assert(std::is_base_of<tlv::Error, Interest::Error>::value,
35 "Interest::Error must inherit from tlv::Error");
36
Junxiao Shi899277a2017-07-07 22:12:12 +000037Interest::Interest(const Name& name, time::milliseconds interestLifetime)
Junxiao Shi2af905b2014-11-27 13:10:54 -070038 : m_name(name)
Junxiao Shi2af905b2014-11-27 13:10:54 -070039 , m_interestLifetime(interestLifetime)
40{
Junxiao Shi899277a2017-07-07 22:12:12 +000041 if (interestLifetime < time::milliseconds::zero()) {
42 BOOST_THROW_EXCEPTION(std::invalid_argument("InterestLifetime must be >= 0"));
43 }
Junxiao Shi2af905b2014-11-27 13:10:54 -070044}
45
Junxiao Shi2af905b2014-11-27 13:10:54 -070046Interest::Interest(const Block& wire)
47{
48 wireDecode(wire);
49}
50
Junxiao Shi899277a2017-07-07 22:12:12 +000051// ---- encode and decode ----
Alexander Afanasyev840139f2013-12-28 15:02:50 -080052
Junxiao Shi899277a2017-07-07 22:12:12 +000053template<encoding::Tag TAG>
54size_t
55Interest::wireEncode(EncodingImpl<TAG>& encoder) const
56{
57 size_t totalLength = 0;
58
59 // Interest ::= INTEREST-TYPE TLV-LENGTH
60 // Name
61 // Selectors?
62 // Nonce
63 // InterestLifetime?
Junxiao Shi9c154cb2017-07-07 22:14:54 +000064 // ForwardingHint?
Junxiao Shi899277a2017-07-07 22:12:12 +000065
66 // (reverse encoding)
67
Junxiao Shi9c154cb2017-07-07 22:14:54 +000068 // ForwardingHint
69 if (m_forwardingHint.size() > 0) {
70 totalLength += m_forwardingHint.wireEncode(encoder);
71 }
72
Junxiao Shi899277a2017-07-07 22:12:12 +000073 // InterestLifetime
74 if (getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) {
75 totalLength += prependNonNegativeIntegerBlock(encoder,
76 tlv::InterestLifetime,
77 getInterestLifetime().count());
78 }
79
80 // Nonce
Junxiao Shi2dd711d2017-07-21 13:40:52 +000081 uint32_t nonce = this->getNonce(); // assigns random Nonce if needed
82 totalLength += encoder.prependByteArray(reinterpret_cast<uint8_t*>(&nonce), sizeof(nonce));
83 totalLength += encoder.prependVarNumber(sizeof(nonce));
84 totalLength += encoder.prependVarNumber(tlv::Nonce);
Junxiao Shi899277a2017-07-07 22:12:12 +000085
86 // Selectors
87 if (hasSelectors()) {
88 totalLength += getSelectors().wireEncode(encoder);
89 }
90
91 // Name
92 totalLength += getName().wireEncode(encoder);
93
94 totalLength += encoder.prependVarNumber(totalLength);
95 totalLength += encoder.prependVarNumber(tlv::Interest);
96 return totalLength;
Alexander Afanasyev840139f2013-12-28 15:02:50 -080097}
98
Junxiao Shi899277a2017-07-07 22:12:12 +000099template size_t
100Interest::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const;
101
102template size_t
103Interest::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const;
104
105const Block&
106Interest::wireEncode() const
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300107{
Junxiao Shi899277a2017-07-07 22:12:12 +0000108 if (m_wire.hasWire())
109 return m_wire;
110
111 EncodingEstimator estimator;
112 size_t estimatedSize = wireEncode(estimator);
113
114 EncodingBuffer buffer(estimatedSize, 0);
115 wireEncode(buffer);
116
Junxiao Shi899277a2017-07-07 22:12:12 +0000117 const_cast<Interest*>(this)->wireDecode(buffer.block());
Junxiao Shi899277a2017-07-07 22:12:12 +0000118 return m_wire;
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300119}
Alexander Afanasyev840139f2013-12-28 15:02:50 -0800120
Alexander Afanasyevc3932172014-07-10 18:53:56 -0700121void
Junxiao Shi899277a2017-07-07 22:12:12 +0000122Interest::wireDecode(const Block& wire)
Alexander Afanasyevc3932172014-07-10 18:53:56 -0700123{
Junxiao Shi899277a2017-07-07 22:12:12 +0000124 m_wire = wire;
125 m_wire.parse();
Alexander Afanasyevc3932172014-07-10 18:53:56 -0700126
Junxiao Shi899277a2017-07-07 22:12:12 +0000127 if (m_wire.type() != tlv::Interest)
128 BOOST_THROW_EXCEPTION(Error("Unexpected TLV number when decoding Interest"));
Alexander Afanasyevc3932172014-07-10 18:53:56 -0700129
Junxiao Shi899277a2017-07-07 22:12:12 +0000130 // Name
131 m_name.wireDecode(m_wire.get(tlv::Name));
132
133 // Selectors
134 Block::element_const_iterator val = m_wire.find(tlv::Selectors);
135 if (val != m_wire.elements_end()) {
136 m_selectors.wireDecode(*val);
137 }
138 else
139 m_selectors = Selectors();
140
141 // Nonce
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000142 val = m_wire.find(tlv::Nonce);
143 if (val == m_wire.elements_end()) {
144 BOOST_THROW_EXCEPTION(Error("Nonce element is missing"));
145 }
146 uint32_t nonce = 0;
147 if (val->value_size() != sizeof(nonce)) {
148 BOOST_THROW_EXCEPTION(Error("Nonce element is malformed"));
149 }
150 std::memcpy(&nonce, val->value(), sizeof(nonce));
151 m_nonce = nonce;
Junxiao Shi899277a2017-07-07 22:12:12 +0000152
153 // InterestLifetime
154 val = m_wire.find(tlv::InterestLifetime);
155 if (val != m_wire.elements_end()) {
156 m_interestLifetime = time::milliseconds(readNonNegativeInteger(*val));
157 }
158 else {
159 m_interestLifetime = DEFAULT_INTEREST_LIFETIME;
160 }
161
Junxiao Shi9c154cb2017-07-07 22:14:54 +0000162 // ForwardingHint
163 val = m_wire.find(tlv::ForwardingHint);
164 if (val != m_wire.elements_end()) {
165 m_forwardingHint.wireDecode(*val, false);
166 }
167 else {
168 m_forwardingHint = DelegationList();
169 }
Alexander Afanasyevc3932172014-07-10 18:53:56 -0700170}
171
Junxiao Shi899277a2017-07-07 22:12:12 +0000172// ---- matching ----
173
Alexander Afanasyev84681982014-01-03 13:26:09 -0800174bool
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700175Interest::matchesName(const Name& name) const
Jeff Thompson25b4e612013-10-10 16:03:24 -0700176{
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700177 if (name.size() < m_name.size())
178 return false;
179
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800180 if (!m_name.isPrefixOf(name))
Alexander Afanasyev84681982014-01-03 13:26:09 -0800181 return false;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700182
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800183 if (getMinSuffixComponents() >= 0 &&
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700184 // name must include implicit digest
185 !(name.size() - m_name.size() >= static_cast<size_t>(getMinSuffixComponents())))
Alexander Afanasyev84681982014-01-03 13:26:09 -0800186 return false;
187
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800188 if (getMaxSuffixComponents() >= 0 &&
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700189 // name must include implicit digest
190 !(name.size() - m_name.size() <= static_cast<size_t>(getMaxSuffixComponents())))
Alexander Afanasyev84681982014-01-03 13:26:09 -0800191 return false;
192
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700193 if (!getExclude().empty() &&
194 name.size() > m_name.size() &&
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800195 getExclude().isExcluded(name[m_name.size()]))
Alexander Afanasyev84681982014-01-03 13:26:09 -0800196 return false;
197
198 return true;
Jeff Thompson25b4e612013-10-10 16:03:24 -0700199}
200
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700201bool
202Interest::matchesData(const Data& data) const
203{
Junxiao Shi42c23622014-07-03 00:55:11 -0700204 size_t interestNameLength = m_name.size();
205 const Name& dataName = data.getName();
206 size_t fullNameLength = dataName.size() + 1;
207
208 // check MinSuffixComponents
209 bool hasMinSuffixComponents = getMinSuffixComponents() >= 0;
210 size_t minSuffixComponents = hasMinSuffixComponents ?
211 static_cast<size_t>(getMinSuffixComponents()) : 0;
212 if (!(interestNameLength + minSuffixComponents <= fullNameLength))
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700213 return false;
Junxiao Shi42c23622014-07-03 00:55:11 -0700214
215 // check MaxSuffixComponents
216 bool hasMaxSuffixComponents = getMaxSuffixComponents() >= 0;
217 if (hasMaxSuffixComponents &&
218 !(interestNameLength + getMaxSuffixComponents() >= fullNameLength))
219 return false;
220
221 // check prefix
222 if (interestNameLength == fullNameLength) {
Alexander Afanasyev56860f52014-11-07 11:51:17 -0800223 if (m_name.get(-1).isImplicitSha256Digest()) {
224 if (m_name != data.getFullName())
Junxiao Shi42c23622014-07-03 00:55:11 -0700225 return false;
226 }
227 else {
228 // Interest Name is same length as Data full Name, but last component isn't digest
229 // so there's no possibility of matching
230 return false;
231 }
232 }
233 else {
234 // Interest Name is a strict prefix of Data full Name
235 if (!m_name.isPrefixOf(dataName))
236 return false;
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700237 }
238
Junxiao Shi42c23622014-07-03 00:55:11 -0700239 // check Exclude
240 // Exclude won't be violated if Interest Name is same as Data full Name
241 if (!getExclude().empty() && fullNameLength > interestNameLength) {
242 if (interestNameLength == fullNameLength - 1) {
243 // component to exclude is the digest
244 if (getExclude().isExcluded(data.getFullName().get(interestNameLength)))
245 return false;
246 // There's opportunity to inspect the Exclude filter and determine whether
247 // the digest would make a difference.
Junxiao Shi08d07082014-12-03 11:31:44 -0700248 // eg. "<NameComponent>AA</NameComponent><Any/>" doesn't exclude any digest -
249 // fullName not needed;
250 // "<Any/><NameComponent>AA</NameComponent>" and
251 // "<Any/><ImplicitSha256DigestComponent>ffffffffffffffffffffffffffffffff
252 // </ImplicitSha256DigestComponent>"
253 // excludes all digests - fullName not needed;
254 // "<Any/><ImplicitSha256DigestComponent>80000000000000000000000000000000
255 // </ImplicitSha256DigestComponent>"
256 // excludes some digests - fullName required
Junxiao Shi42c23622014-07-03 00:55:11 -0700257 // But Interests that contain the exact Data Name before digest and also
258 // contain Exclude filter is too rare to optimize for, so we request
Junxiao Shi68247832017-07-03 22:06:49 +0000259 // fullName no matter what's in the Exclude filter.
Junxiao Shi42c23622014-07-03 00:55:11 -0700260 }
261 else {
262 // component to exclude is not the digest
263 if (getExclude().isExcluded(dataName.get(interestNameLength)))
264 return false;
265 }
266 }
267
268 // check PublisherPublicKeyLocator
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700269 const KeyLocator& publisherPublicKeyLocator = this->getPublisherPublicKeyLocator();
270 if (!publisherPublicKeyLocator.empty()) {
271 const Signature& signature = data.getSignature();
272 const Block& signatureInfo = signature.getInfo();
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600273 Block::element_const_iterator it = signatureInfo.find(tlv::KeyLocator);
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700274 if (it == signatureInfo.elements_end()) {
275 return false;
276 }
277 if (publisherPublicKeyLocator.wireEncode() != *it) {
278 return false;
279 }
280 }
281
282 return true;
283}
284
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800285bool
286Interest::matchesInterest(const Interest& other) const
287{
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000288 /// @todo #3162 match ForwardingHint field
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800289 return (this->getName() == other.getName() &&
290 this->getSelectors() == other.getSelectors());
291}
292
Junxiao Shi899277a2017-07-07 22:12:12 +0000293// ---- field accessors ----
294
295uint32_t
296Interest::getNonce() const
297{
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000298 if (!m_nonce) {
299 m_nonce = random::generateWord32();
Junxiao Shic2ac5d22017-07-17 22:18:31 +0000300 }
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000301 return *m_nonce;
Junxiao Shi899277a2017-07-07 22:12:12 +0000302}
303
304Interest&
305Interest::setNonce(uint32_t nonce)
306{
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000307 m_nonce = nonce;
308 m_wire.reset();
Junxiao Shi899277a2017-07-07 22:12:12 +0000309 return *this;
310}
311
312void
313Interest::refreshNonce()
314{
315 if (!hasNonce())
316 return;
317
318 uint32_t oldNonce = getNonce();
319 uint32_t newNonce = oldNonce;
320 while (newNonce == oldNonce)
321 newNonce = random::generateWord32();
322
323 setNonce(newNonce);
324}
325
Eric Newberryb555b002017-05-17 00:30:44 -0700326Interest&
327Interest::setInterestLifetime(time::milliseconds interestLifetime)
328{
329 if (interestLifetime < time::milliseconds::zero()) {
330 BOOST_THROW_EXCEPTION(std::invalid_argument("InterestLifetime must be >= 0"));
331 }
332 m_interestLifetime = interestLifetime;
333 m_wire.reset();
334 return *this;
335}
336
Junxiao Shi9c154cb2017-07-07 22:14:54 +0000337Interest&
338Interest::setForwardingHint(const DelegationList& value)
339{
340 m_forwardingHint = value;
341 m_wire.reset();
342 return *this;
343}
344
Junxiao Shi899277a2017-07-07 22:12:12 +0000345// ---- operators ----
346
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700347std::ostream&
348operator<<(std::ostream& os, const Interest& interest)
Jeff Thompsonfe556862013-07-09 13:52:55 -0700349{
Alexander Afanasyev84681982014-01-03 13:26:09 -0800350 os << interest.getName();
Jeff Thompsonfe556862013-07-09 13:52:55 -0700351
Alexander Afanasyev84681982014-01-03 13:26:09 -0800352 char delim = '?';
353
354 if (interest.getMinSuffixComponents() >= 0) {
355 os << delim << "ndn.MinSuffixComponents=" << interest.getMinSuffixComponents();
356 delim = '&';
Jeff Thompsonfe556862013-07-09 13:52:55 -0700357 }
Alexander Afanasyev84681982014-01-03 13:26:09 -0800358 if (interest.getMaxSuffixComponents() >= 0) {
359 os << delim << "ndn.MaxSuffixComponents=" << interest.getMaxSuffixComponents();
360 delim = '&';
Jeff Thompson37527d62013-08-21 11:15:54 -0700361 }
Eric Newberryb555b002017-05-17 00:30:44 -0700362 if (interest.getChildSelector() != DEFAULT_CHILD_SELECTOR) {
Alexander Afanasyev84681982014-01-03 13:26:09 -0800363 os << delim << "ndn.ChildSelector=" << interest.getChildSelector();
364 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -0800365 }
Alexander Afanasyev84681982014-01-03 13:26:09 -0800366 if (interest.getMustBeFresh()) {
367 os << delim << "ndn.MustBeFresh=" << interest.getMustBeFresh();
368 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -0800369 }
Eric Newberryb555b002017-05-17 00:30:44 -0700370 if (interest.getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) {
Alexander Afanasyeva0c5f832014-06-19 13:27:56 -0700371 os << delim << "ndn.InterestLifetime=" << interest.getInterestLifetime().count();
Alexander Afanasyev84681982014-01-03 13:26:09 -0800372 delim = '&';
373 }
374
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300375 if (interest.hasNonce()) {
Alexander Afanasyev84681982014-01-03 13:26:09 -0800376 os << delim << "ndn.Nonce=" << interest.getNonce();
377 delim = '&';
378 }
379 if (!interest.getExclude().empty()) {
380 os << delim << "ndn.Exclude=" << interest.getExclude();
381 delim = '&';
382 }
383
384 return os;
Jeff Thompson13e280b2013-12-03 13:12:23 -0800385}
386
Junxiao Shi08d07082014-12-03 11:31:44 -0700387} // namespace ndn