blob: 60e284b46a6380e9498ebf8ef66a237446a9cfcb [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>
Davide Pesaventoa84f4642017-08-23 16:14:51 -040027#include <sstream>
Davide Pesaventoe1789892017-02-26 15:50:52 -050028
Jeff Thompsonb7f95562013-07-03 18:36:42 -070029namespace ndn {
Alexander Afanasyev84681982014-01-03 13:26:09 -080030
Junxiao Shic2b8d242014-11-04 08:35:29 -070031BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Interest>));
32BOOST_CONCEPT_ASSERT((WireEncodable<Interest>));
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070033BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<Interest>));
Junxiao Shic2b8d242014-11-04 08:35:29 -070034BOOST_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 Shi899277a2017-07-07 22:12:12 +000038Interest::Interest(const Name& name, time::milliseconds interestLifetime)
Junxiao Shi2af905b2014-11-27 13:10:54 -070039 : m_name(name)
Junxiao Shi2af905b2014-11-27 13:10:54 -070040 , m_interestLifetime(interestLifetime)
41{
Junxiao Shi899277a2017-07-07 22:12:12 +000042 if (interestLifetime < time::milliseconds::zero()) {
43 BOOST_THROW_EXCEPTION(std::invalid_argument("InterestLifetime must be >= 0"));
44 }
Junxiao Shi2af905b2014-11-27 13:10:54 -070045}
46
Junxiao Shi2af905b2014-11-27 13:10:54 -070047Interest::Interest(const Block& wire)
48{
49 wireDecode(wire);
50}
51
Junxiao Shi899277a2017-07-07 22:12:12 +000052// ---- encode and decode ----
Alexander Afanasyev840139f2013-12-28 15:02:50 -080053
Junxiao Shi899277a2017-07-07 22:12:12 +000054template<encoding::Tag TAG>
55size_t
56Interest::wireEncode(EncodingImpl<TAG>& encoder) const
57{
58 size_t totalLength = 0;
59
60 // Interest ::= INTEREST-TYPE TLV-LENGTH
61 // Name
62 // Selectors?
63 // Nonce
64 // InterestLifetime?
Junxiao Shi9c154cb2017-07-07 22:14:54 +000065 // ForwardingHint?
Junxiao Shi899277a2017-07-07 22:12:12 +000066
67 // (reverse encoding)
68
Junxiao Shi9c154cb2017-07-07 22:14:54 +000069 // ForwardingHint
70 if (m_forwardingHint.size() > 0) {
71 totalLength += m_forwardingHint.wireEncode(encoder);
72 }
73
Junxiao Shi899277a2017-07-07 22:12:12 +000074 // InterestLifetime
75 if (getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) {
76 totalLength += prependNonNegativeIntegerBlock(encoder,
77 tlv::InterestLifetime,
78 getInterestLifetime().count());
79 }
80
81 // Nonce
Junxiao Shi2dd711d2017-07-21 13:40:52 +000082 uint32_t nonce = this->getNonce(); // assigns random Nonce if needed
83 totalLength += encoder.prependByteArray(reinterpret_cast<uint8_t*>(&nonce), sizeof(nonce));
84 totalLength += encoder.prependVarNumber(sizeof(nonce));
85 totalLength += encoder.prependVarNumber(tlv::Nonce);
Junxiao Shi899277a2017-07-07 22:12:12 +000086
87 // Selectors
88 if (hasSelectors()) {
89 totalLength += getSelectors().wireEncode(encoder);
90 }
91
92 // Name
93 totalLength += getName().wireEncode(encoder);
94
95 totalLength += encoder.prependVarNumber(totalLength);
96 totalLength += encoder.prependVarNumber(tlv::Interest);
97 return totalLength;
Alexander Afanasyev840139f2013-12-28 15:02:50 -080098}
99
Davide Pesavento88a0d812017-08-19 21:31:42 -0400100NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(Interest);
Junxiao Shi899277a2017-07-07 22:12:12 +0000101
102const Block&
103Interest::wireEncode() const
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300104{
Junxiao Shi899277a2017-07-07 22:12:12 +0000105 if (m_wire.hasWire())
106 return m_wire;
107
108 EncodingEstimator estimator;
109 size_t estimatedSize = wireEncode(estimator);
110
111 EncodingBuffer buffer(estimatedSize, 0);
112 wireEncode(buffer);
113
Junxiao Shi899277a2017-07-07 22:12:12 +0000114 const_cast<Interest*>(this)->wireDecode(buffer.block());
Junxiao Shi899277a2017-07-07 22:12:12 +0000115 return m_wire;
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300116}
Alexander Afanasyev840139f2013-12-28 15:02:50 -0800117
Alexander Afanasyevc3932172014-07-10 18:53:56 -0700118void
Junxiao Shi899277a2017-07-07 22:12:12 +0000119Interest::wireDecode(const Block& wire)
Alexander Afanasyevc3932172014-07-10 18:53:56 -0700120{
Junxiao Shi899277a2017-07-07 22:12:12 +0000121 m_wire = wire;
122 m_wire.parse();
Alexander Afanasyevc3932172014-07-10 18:53:56 -0700123
Junxiao Shi899277a2017-07-07 22:12:12 +0000124 if (m_wire.type() != tlv::Interest)
125 BOOST_THROW_EXCEPTION(Error("Unexpected TLV number when decoding Interest"));
Alexander Afanasyevc3932172014-07-10 18:53:56 -0700126
Junxiao Shi899277a2017-07-07 22:12:12 +0000127 // Name
128 m_name.wireDecode(m_wire.get(tlv::Name));
129
130 // Selectors
131 Block::element_const_iterator val = m_wire.find(tlv::Selectors);
132 if (val != m_wire.elements_end()) {
133 m_selectors.wireDecode(*val);
134 }
135 else
136 m_selectors = Selectors();
137
138 // Nonce
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000139 val = m_wire.find(tlv::Nonce);
140 if (val == m_wire.elements_end()) {
141 BOOST_THROW_EXCEPTION(Error("Nonce element is missing"));
142 }
143 uint32_t nonce = 0;
144 if (val->value_size() != sizeof(nonce)) {
145 BOOST_THROW_EXCEPTION(Error("Nonce element is malformed"));
146 }
147 std::memcpy(&nonce, val->value(), sizeof(nonce));
148 m_nonce = nonce;
Junxiao Shi899277a2017-07-07 22:12:12 +0000149
150 // InterestLifetime
151 val = m_wire.find(tlv::InterestLifetime);
152 if (val != m_wire.elements_end()) {
153 m_interestLifetime = time::milliseconds(readNonNegativeInteger(*val));
154 }
155 else {
156 m_interestLifetime = DEFAULT_INTEREST_LIFETIME;
157 }
158
Junxiao Shi9c154cb2017-07-07 22:14:54 +0000159 // ForwardingHint
160 val = m_wire.find(tlv::ForwardingHint);
161 if (val != m_wire.elements_end()) {
162 m_forwardingHint.wireDecode(*val, false);
163 }
164 else {
165 m_forwardingHint = DelegationList();
166 }
Alexander Afanasyevc3932172014-07-10 18:53:56 -0700167}
168
Davide Pesaventoa84f4642017-08-23 16:14:51 -0400169std::string
170Interest::toUri() const
171{
172 std::ostringstream os;
173 os << *this;
174 return os.str();
175}
176
Junxiao Shi899277a2017-07-07 22:12:12 +0000177// ---- matching ----
178
Alexander Afanasyev84681982014-01-03 13:26:09 -0800179bool
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700180Interest::matchesName(const Name& name) const
Jeff Thompson25b4e612013-10-10 16:03:24 -0700181{
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700182 if (name.size() < m_name.size())
183 return false;
184
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800185 if (!m_name.isPrefixOf(name))
Alexander Afanasyev84681982014-01-03 13:26:09 -0800186 return false;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700187
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800188 if (getMinSuffixComponents() >= 0 &&
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700189 // name must include implicit digest
190 !(name.size() - m_name.size() >= static_cast<size_t>(getMinSuffixComponents())))
Alexander Afanasyev84681982014-01-03 13:26:09 -0800191 return false;
192
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800193 if (getMaxSuffixComponents() >= 0 &&
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700194 // name must include implicit digest
195 !(name.size() - m_name.size() <= static_cast<size_t>(getMaxSuffixComponents())))
Alexander Afanasyev84681982014-01-03 13:26:09 -0800196 return false;
197
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700198 if (!getExclude().empty() &&
199 name.size() > m_name.size() &&
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800200 getExclude().isExcluded(name[m_name.size()]))
Alexander Afanasyev84681982014-01-03 13:26:09 -0800201 return false;
202
203 return true;
Jeff Thompson25b4e612013-10-10 16:03:24 -0700204}
205
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700206bool
207Interest::matchesData(const Data& data) const
208{
Junxiao Shi42c23622014-07-03 00:55:11 -0700209 size_t interestNameLength = m_name.size();
210 const Name& dataName = data.getName();
211 size_t fullNameLength = dataName.size() + 1;
212
213 // check MinSuffixComponents
214 bool hasMinSuffixComponents = getMinSuffixComponents() >= 0;
215 size_t minSuffixComponents = hasMinSuffixComponents ?
216 static_cast<size_t>(getMinSuffixComponents()) : 0;
217 if (!(interestNameLength + minSuffixComponents <= fullNameLength))
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700218 return false;
Junxiao Shi42c23622014-07-03 00:55:11 -0700219
220 // check MaxSuffixComponents
221 bool hasMaxSuffixComponents = getMaxSuffixComponents() >= 0;
222 if (hasMaxSuffixComponents &&
223 !(interestNameLength + getMaxSuffixComponents() >= fullNameLength))
224 return false;
225
226 // check prefix
227 if (interestNameLength == fullNameLength) {
Alexander Afanasyev56860f52014-11-07 11:51:17 -0800228 if (m_name.get(-1).isImplicitSha256Digest()) {
229 if (m_name != data.getFullName())
Junxiao Shi42c23622014-07-03 00:55:11 -0700230 return false;
231 }
232 else {
233 // Interest Name is same length as Data full Name, but last component isn't digest
234 // so there's no possibility of matching
235 return false;
236 }
237 }
238 else {
239 // Interest Name is a strict prefix of Data full Name
240 if (!m_name.isPrefixOf(dataName))
241 return false;
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700242 }
243
Junxiao Shi42c23622014-07-03 00:55:11 -0700244 // check Exclude
245 // Exclude won't be violated if Interest Name is same as Data full Name
246 if (!getExclude().empty() && fullNameLength > interestNameLength) {
247 if (interestNameLength == fullNameLength - 1) {
248 // component to exclude is the digest
249 if (getExclude().isExcluded(data.getFullName().get(interestNameLength)))
250 return false;
251 // There's opportunity to inspect the Exclude filter and determine whether
252 // the digest would make a difference.
Junxiao Shi08d07082014-12-03 11:31:44 -0700253 // eg. "<NameComponent>AA</NameComponent><Any/>" doesn't exclude any digest -
254 // fullName not needed;
255 // "<Any/><NameComponent>AA</NameComponent>" and
256 // "<Any/><ImplicitSha256DigestComponent>ffffffffffffffffffffffffffffffff
257 // </ImplicitSha256DigestComponent>"
258 // excludes all digests - fullName not needed;
259 // "<Any/><ImplicitSha256DigestComponent>80000000000000000000000000000000
260 // </ImplicitSha256DigestComponent>"
261 // excludes some digests - fullName required
Junxiao Shi42c23622014-07-03 00:55:11 -0700262 // But Interests that contain the exact Data Name before digest and also
263 // contain Exclude filter is too rare to optimize for, so we request
Junxiao Shi68247832017-07-03 22:06:49 +0000264 // fullName no matter what's in the Exclude filter.
Junxiao Shi42c23622014-07-03 00:55:11 -0700265 }
266 else {
267 // component to exclude is not the digest
268 if (getExclude().isExcluded(dataName.get(interestNameLength)))
269 return false;
270 }
271 }
272
273 // check PublisherPublicKeyLocator
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700274 const KeyLocator& publisherPublicKeyLocator = this->getPublisherPublicKeyLocator();
275 if (!publisherPublicKeyLocator.empty()) {
276 const Signature& signature = data.getSignature();
277 const Block& signatureInfo = signature.getInfo();
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600278 Block::element_const_iterator it = signatureInfo.find(tlv::KeyLocator);
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700279 if (it == signatureInfo.elements_end()) {
280 return false;
281 }
282 if (publisherPublicKeyLocator.wireEncode() != *it) {
283 return false;
284 }
285 }
286
287 return true;
288}
289
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800290bool
291Interest::matchesInterest(const Interest& other) const
292{
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000293 /// @todo #3162 match ForwardingHint field
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800294 return (this->getName() == other.getName() &&
295 this->getSelectors() == other.getSelectors());
296}
297
Junxiao Shi899277a2017-07-07 22:12:12 +0000298// ---- field accessors ----
299
300uint32_t
301Interest::getNonce() const
302{
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000303 if (!m_nonce) {
304 m_nonce = random::generateWord32();
Junxiao Shic2ac5d22017-07-17 22:18:31 +0000305 }
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000306 return *m_nonce;
Junxiao Shi899277a2017-07-07 22:12:12 +0000307}
308
309Interest&
310Interest::setNonce(uint32_t nonce)
311{
Junxiao Shi2dd711d2017-07-21 13:40:52 +0000312 m_nonce = nonce;
313 m_wire.reset();
Junxiao Shi899277a2017-07-07 22:12:12 +0000314 return *this;
315}
316
317void
318Interest::refreshNonce()
319{
320 if (!hasNonce())
321 return;
322
323 uint32_t oldNonce = getNonce();
324 uint32_t newNonce = oldNonce;
325 while (newNonce == oldNonce)
326 newNonce = random::generateWord32();
327
328 setNonce(newNonce);
329}
330
Eric Newberryb555b002017-05-17 00:30:44 -0700331Interest&
332Interest::setInterestLifetime(time::milliseconds interestLifetime)
333{
334 if (interestLifetime < time::milliseconds::zero()) {
335 BOOST_THROW_EXCEPTION(std::invalid_argument("InterestLifetime must be >= 0"));
336 }
337 m_interestLifetime = interestLifetime;
338 m_wire.reset();
339 return *this;
340}
341
Junxiao Shi9c154cb2017-07-07 22:14:54 +0000342Interest&
343Interest::setForwardingHint(const DelegationList& value)
344{
345 m_forwardingHint = value;
346 m_wire.reset();
347 return *this;
348}
349
Junxiao Shi899277a2017-07-07 22:12:12 +0000350// ---- operators ----
351
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700352std::ostream&
353operator<<(std::ostream& os, const Interest& interest)
Jeff Thompsonfe556862013-07-09 13:52:55 -0700354{
Alexander Afanasyev84681982014-01-03 13:26:09 -0800355 os << interest.getName();
Jeff Thompsonfe556862013-07-09 13:52:55 -0700356
Alexander Afanasyev84681982014-01-03 13:26:09 -0800357 char delim = '?';
358
359 if (interest.getMinSuffixComponents() >= 0) {
360 os << delim << "ndn.MinSuffixComponents=" << interest.getMinSuffixComponents();
361 delim = '&';
Jeff Thompsonfe556862013-07-09 13:52:55 -0700362 }
Alexander Afanasyev84681982014-01-03 13:26:09 -0800363 if (interest.getMaxSuffixComponents() >= 0) {
364 os << delim << "ndn.MaxSuffixComponents=" << interest.getMaxSuffixComponents();
365 delim = '&';
Jeff Thompson37527d62013-08-21 11:15:54 -0700366 }
Eric Newberryb555b002017-05-17 00:30:44 -0700367 if (interest.getChildSelector() != DEFAULT_CHILD_SELECTOR) {
Alexander Afanasyev84681982014-01-03 13:26:09 -0800368 os << delim << "ndn.ChildSelector=" << interest.getChildSelector();
369 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -0800370 }
Alexander Afanasyev84681982014-01-03 13:26:09 -0800371 if (interest.getMustBeFresh()) {
372 os << delim << "ndn.MustBeFresh=" << interest.getMustBeFresh();
373 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -0800374 }
Eric Newberryb555b002017-05-17 00:30:44 -0700375 if (interest.getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) {
Alexander Afanasyeva0c5f832014-06-19 13:27:56 -0700376 os << delim << "ndn.InterestLifetime=" << interest.getInterestLifetime().count();
Alexander Afanasyev84681982014-01-03 13:26:09 -0800377 delim = '&';
378 }
379
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300380 if (interest.hasNonce()) {
Alexander Afanasyev84681982014-01-03 13:26:09 -0800381 os << delim << "ndn.Nonce=" << interest.getNonce();
382 delim = '&';
383 }
384 if (!interest.getExclude().empty()) {
385 os << delim << "ndn.Exclude=" << interest.getExclude();
386 delim = '&';
387 }
388
389 return os;
Jeff Thompson13e280b2013-12-03 13:12:23 -0800390}
391
Junxiao Shi08d07082014-12-03 11:31:44 -0700392} // namespace ndn