blob: 17a7aa499219899733bf6f4b03d71ac102743317 [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 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 Shi42c23622014-07-03 00:55:11 -070024#include "util/crypto.hpp"
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070025#include "data.hpp"
Alexander Afanasyev840139f2013-12-28 15:02:50 -080026
Davide Pesaventoe1789892017-02-26 15:50:52 -050027#include <cstring>
28
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 Shi2af905b2014-11-27 13:10:54 -070038Interest::Interest()
Alexander Afanasyev117f5ef2015-06-03 15:07:24 -070039 : m_interestLifetime(time::milliseconds::min())
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -070040 , m_selectedDelegationIndex(INVALID_SELECTED_DELEGATION_INDEX)
Junxiao Shi2af905b2014-11-27 13:10:54 -070041{
42}
43
44Interest::Interest(const Name& name)
45 : m_name(name)
Junxiao Shi2af905b2014-11-27 13:10:54 -070046 , m_interestLifetime(time::milliseconds::min())
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -070047 , m_selectedDelegationIndex(INVALID_SELECTED_DELEGATION_INDEX)
Junxiao Shi2af905b2014-11-27 13:10:54 -070048{
49}
50
51Interest::Interest(const Name& name, const time::milliseconds& interestLifetime)
52 : m_name(name)
Junxiao Shi2af905b2014-11-27 13:10:54 -070053 , m_interestLifetime(interestLifetime)
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -070054 , m_selectedDelegationIndex(INVALID_SELECTED_DELEGATION_INDEX)
Junxiao Shi2af905b2014-11-27 13:10:54 -070055{
56}
57
Junxiao Shi2af905b2014-11-27 13:10:54 -070058Interest::Interest(const Block& wire)
59{
60 wireDecode(wire);
61}
62
Alexander Afanasyeve881e932014-06-08 14:47:03 +030063uint32_t
Alexander Afanasyev840139f2013-12-28 15:02:50 -080064Interest::getNonce() const
65{
Alexander Afanasyeve881e932014-06-08 14:47:03 +030066 if (!m_nonce.hasWire())
67 const_cast<Interest*>(this)->setNonce(random::generateWord32());
Alexander Afanasyev840139f2013-12-28 15:02:50 -080068
Alexander Afanasyeve881e932014-06-08 14:47:03 +030069 if (m_nonce.value_size() == sizeof(uint32_t))
70 return *reinterpret_cast<const uint32_t*>(m_nonce.value());
71 else {
72 // for compatibility reasons. Should be removed eventually
73 return readNonNegativeInteger(m_nonce);
74 }
Alexander Afanasyev840139f2013-12-28 15:02:50 -080075}
76
Alexander Afanasyeve881e932014-06-08 14:47:03 +030077Interest&
78Interest::setNonce(uint32_t nonce)
79{
80 if (m_wire.hasWire() && m_nonce.value_size() == sizeof(uint32_t)) {
81 std::memcpy(const_cast<uint8_t*>(m_nonce.value()), &nonce, sizeof(nonce));
82 }
83 else {
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070084 m_nonce = makeBinaryBlock(tlv::Nonce,
85 reinterpret_cast<const uint8_t*>(&nonce),
86 sizeof(nonce));
Alexander Afanasyeve881e932014-06-08 14:47:03 +030087 m_wire.reset();
88 }
89 return *this;
90}
Alexander Afanasyev840139f2013-12-28 15:02:50 -080091
Alexander Afanasyevc3932172014-07-10 18:53:56 -070092void
93Interest::refreshNonce()
94{
95 if (!hasNonce())
96 return;
97
98 uint32_t oldNonce = getNonce();
99 uint32_t newNonce = oldNonce;
100 while (newNonce == oldNonce)
101 newNonce = random::generateWord32();
102
103 setNonce(newNonce);
104}
105
Alexander Afanasyev84681982014-01-03 13:26:09 -0800106bool
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700107Interest::matchesName(const Name& name) const
Jeff Thompson25b4e612013-10-10 16:03:24 -0700108{
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700109 if (name.size() < m_name.size())
110 return false;
111
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800112 if (!m_name.isPrefixOf(name))
Alexander Afanasyev84681982014-01-03 13:26:09 -0800113 return false;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700114
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800115 if (getMinSuffixComponents() >= 0 &&
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700116 // name must include implicit digest
117 !(name.size() - m_name.size() >= static_cast<size_t>(getMinSuffixComponents())))
Alexander Afanasyev84681982014-01-03 13:26:09 -0800118 return false;
119
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800120 if (getMaxSuffixComponents() >= 0 &&
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700121 // name must include implicit digest
122 !(name.size() - m_name.size() <= static_cast<size_t>(getMaxSuffixComponents())))
Alexander Afanasyev84681982014-01-03 13:26:09 -0800123 return false;
124
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700125 if (!getExclude().empty() &&
126 name.size() > m_name.size() &&
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800127 getExclude().isExcluded(name[m_name.size()]))
Alexander Afanasyev84681982014-01-03 13:26:09 -0800128 return false;
129
130 return true;
Jeff Thompson25b4e612013-10-10 16:03:24 -0700131}
132
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700133bool
134Interest::matchesData(const Data& data) const
135{
Junxiao Shi42c23622014-07-03 00:55:11 -0700136 size_t interestNameLength = m_name.size();
137 const Name& dataName = data.getName();
138 size_t fullNameLength = dataName.size() + 1;
139
140 // check MinSuffixComponents
141 bool hasMinSuffixComponents = getMinSuffixComponents() >= 0;
142 size_t minSuffixComponents = hasMinSuffixComponents ?
143 static_cast<size_t>(getMinSuffixComponents()) : 0;
144 if (!(interestNameLength + minSuffixComponents <= fullNameLength))
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700145 return false;
Junxiao Shi42c23622014-07-03 00:55:11 -0700146
147 // check MaxSuffixComponents
148 bool hasMaxSuffixComponents = getMaxSuffixComponents() >= 0;
149 if (hasMaxSuffixComponents &&
150 !(interestNameLength + getMaxSuffixComponents() >= fullNameLength))
151 return false;
152
153 // check prefix
154 if (interestNameLength == fullNameLength) {
Alexander Afanasyev56860f52014-11-07 11:51:17 -0800155 if (m_name.get(-1).isImplicitSha256Digest()) {
156 if (m_name != data.getFullName())
Junxiao Shi42c23622014-07-03 00:55:11 -0700157 return false;
158 }
159 else {
160 // Interest Name is same length as Data full Name, but last component isn't digest
161 // so there's no possibility of matching
162 return false;
163 }
164 }
165 else {
166 // Interest Name is a strict prefix of Data full Name
167 if (!m_name.isPrefixOf(dataName))
168 return false;
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700169 }
170
Junxiao Shi42c23622014-07-03 00:55:11 -0700171 // check Exclude
172 // Exclude won't be violated if Interest Name is same as Data full Name
173 if (!getExclude().empty() && fullNameLength > interestNameLength) {
174 if (interestNameLength == fullNameLength - 1) {
175 // component to exclude is the digest
176 if (getExclude().isExcluded(data.getFullName().get(interestNameLength)))
177 return false;
178 // There's opportunity to inspect the Exclude filter and determine whether
179 // the digest would make a difference.
Junxiao Shi08d07082014-12-03 11:31:44 -0700180 // eg. "<NameComponent>AA</NameComponent><Any/>" doesn't exclude any digest -
181 // fullName not needed;
182 // "<Any/><NameComponent>AA</NameComponent>" and
183 // "<Any/><ImplicitSha256DigestComponent>ffffffffffffffffffffffffffffffff
184 // </ImplicitSha256DigestComponent>"
185 // excludes all digests - fullName not needed;
186 // "<Any/><ImplicitSha256DigestComponent>80000000000000000000000000000000
187 // </ImplicitSha256DigestComponent>"
188 // excludes some digests - fullName required
Junxiao Shi42c23622014-07-03 00:55:11 -0700189 // But Interests that contain the exact Data Name before digest and also
190 // contain Exclude filter is too rare to optimize for, so we request
191 // fullName no mater what's in the Exclude filter.
192 }
193 else {
194 // component to exclude is not the digest
195 if (getExclude().isExcluded(dataName.get(interestNameLength)))
196 return false;
197 }
198 }
199
200 // check PublisherPublicKeyLocator
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700201 const KeyLocator& publisherPublicKeyLocator = this->getPublisherPublicKeyLocator();
202 if (!publisherPublicKeyLocator.empty()) {
203 const Signature& signature = data.getSignature();
204 const Block& signatureInfo = signature.getInfo();
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600205 Block::element_const_iterator it = signatureInfo.find(tlv::KeyLocator);
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700206 if (it == signatureInfo.elements_end()) {
207 return false;
208 }
209 if (publisherPublicKeyLocator.wireEncode() != *it) {
210 return false;
211 }
212 }
213
214 return true;
215}
216
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800217bool
218Interest::matchesInterest(const Interest& other) const
219{
220 /// @todo #3162 match Link field
221 return (this->getName() == other.getName() &&
222 this->getSelectors() == other.getSelectors());
223}
224
Alexander Afanasyev74633892015-02-08 18:08:46 -0800225template<encoding::Tag TAG>
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700226size_t
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700227Interest::wireEncode(EncodingImpl<TAG>& encoder) const
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700228{
229 size_t totalLength = 0;
230
231 // Interest ::= INTEREST-TYPE TLV-LENGTH
232 // Name
233 // Selectors?
234 // Nonce
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700235 // InterestLifetime?
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700236 // Link?
237 // SelectedDelegation?
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700238
239 // (reverse encoding)
240
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700241 if (hasLink()) {
242 if (hasSelectedDelegation()) {
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700243 totalLength += prependNonNegativeIntegerBlock(encoder,
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700244 tlv::SelectedDelegation,
245 m_selectedDelegationIndex);
246 }
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700247 totalLength += encoder.prependBlock(m_link);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700248 }
249 else {
250 BOOST_ASSERT(!hasSelectedDelegation());
251 }
252
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700253 // InterestLifetime
254 if (getInterestLifetime() >= time::milliseconds::zero() &&
255 getInterestLifetime() != DEFAULT_INTEREST_LIFETIME)
256 {
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700257 totalLength += prependNonNegativeIntegerBlock(encoder,
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600258 tlv::InterestLifetime,
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700259 getInterestLifetime().count());
260 }
261
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700262 // Nonce
263 getNonce(); // to ensure that Nonce is properly set
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700264 totalLength += encoder.prependBlock(m_nonce);
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700265
266 // Selectors
267 if (hasSelectors())
268 {
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700269 totalLength += getSelectors().wireEncode(encoder);
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700270 }
271
272 // Name
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700273 totalLength += getName().wireEncode(encoder);
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700274
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700275 totalLength += encoder.prependVarNumber(totalLength);
276 totalLength += encoder.prependVarNumber(tlv::Interest);
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700277 return totalLength;
278}
279
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700280template size_t
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700281Interest::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const;
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700282
283template size_t
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700284Interest::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const;
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700285
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700286const Block&
287Interest::wireEncode() const
288{
289 if (m_wire.hasWire())
290 return m_wire;
291
292 EncodingEstimator estimator;
293 size_t estimatedSize = wireEncode(estimator);
294
295 EncodingBuffer buffer(estimatedSize, 0);
296 wireEncode(buffer);
297
298 // to ensure that Nonce block points to the right memory location
299 const_cast<Interest*>(this)->wireDecode(buffer.block());
300
301 return m_wire;
302}
303
304void
305Interest::wireDecode(const Block& wire)
306{
307 m_wire = wire;
308 m_wire.parse();
309
310 // Interest ::= INTEREST-TYPE TLV-LENGTH
311 // Name
312 // Selectors?
313 // Nonce
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700314 // InterestLifetime?
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700315 // Link?
316 // SelectedDelegation?
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700317
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600318 if (m_wire.type() != tlv::Interest)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700319 BOOST_THROW_EXCEPTION(Error("Unexpected TLV number when decoding Interest"));
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700320
321 // Name
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600322 m_name.wireDecode(m_wire.get(tlv::Name));
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700323
324 // Selectors
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600325 Block::element_const_iterator val = m_wire.find(tlv::Selectors);
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700326 if (val != m_wire.elements_end()) {
327 m_selectors.wireDecode(*val);
328 }
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700329 else
330 m_selectors = Selectors();
331
332 // Nonce
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600333 m_nonce = m_wire.get(tlv::Nonce);
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700334
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700335 // InterestLifetime
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600336 val = m_wire.find(tlv::InterestLifetime);
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700337 if (val != m_wire.elements_end()) {
338 m_interestLifetime = time::milliseconds(readNonNegativeInteger(*val));
339 }
340 else {
341 m_interestLifetime = DEFAULT_INTEREST_LIFETIME;
342 }
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700343
344 // Link object
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700345 m_linkCached.reset();
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700346 val = m_wire.find(tlv::Data);
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700347 if (val != m_wire.elements_end()) {
348 m_link = (*val);
349 }
350 else {
351 m_link = Block();
352 }
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700353
354 // SelectedDelegation
355 val = m_wire.find(tlv::SelectedDelegation);
356 if (val != m_wire.elements_end()) {
357 if (!this->hasLink()) {
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700358 BOOST_THROW_EXCEPTION(Error("Interest contains SelectedDelegation, but no LINK object"));
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700359 }
360 uint64_t selectedDelegation = readNonNegativeInteger(*val);
361 if (selectedDelegation < uint64_t(Link::countDelegationsFromWire(m_link))) {
362 m_selectedDelegationIndex = static_cast<size_t>(selectedDelegation);
363 }
364 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700365 BOOST_THROW_EXCEPTION(Error("Invalid selected delegation index when decoding Interest"));
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700366 }
367 }
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700368 else {
369 m_selectedDelegationIndex = INVALID_SELECTED_DELEGATION_INDEX;
370 }
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700371}
372
373bool
374Interest::hasLink() const
375{
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700376 return m_link.hasWire();
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700377}
378
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700379const Link&
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700380Interest::getLink() const
381{
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700382 if (hasLink()) {
383 if (!m_linkCached) {
384 m_linkCached = make_shared<Link>(m_link);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700385 }
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700386 return *m_linkCached;
387 }
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700388 BOOST_THROW_EXCEPTION(Error("There is no encapsulated link object"));
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700389}
390
391void
392Interest::setLink(const Block& link)
393{
394 m_link = link;
395 if (!link.hasWire()) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700396 BOOST_THROW_EXCEPTION(Error("The given link does not have a wire format"));
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700397 }
398 m_wire.reset();
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700399 m_linkCached.reset();
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700400 this->unsetSelectedDelegation();
401}
402
403void
404Interest::unsetLink()
405{
406 m_link.reset();
407 m_wire.reset();
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700408 m_linkCached.reset();
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700409 this->unsetSelectedDelegation();
410}
411
412bool
413Interest::hasSelectedDelegation() const
414{
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700415 return m_selectedDelegationIndex != INVALID_SELECTED_DELEGATION_INDEX;
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700416}
417
418Name
419Interest::getSelectedDelegation() const
420{
421 if (!hasSelectedDelegation()) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700422 BOOST_THROW_EXCEPTION(Error("There is no encapsulated selected delegation"));
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700423 }
424 return std::get<1>(Link::getDelegationFromWire(m_link, m_selectedDelegationIndex));
425}
426
427void
428Interest::setSelectedDelegation(const Name& delegationName)
429{
430 size_t delegationIndex = Link::findDelegationFromWire(m_link, delegationName);
431 if (delegationIndex != INVALID_SELECTED_DELEGATION_INDEX) {
432 m_selectedDelegationIndex = delegationIndex;
433 }
434 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700435 BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid selected delegation name"));
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700436 }
437 m_wire.reset();
438}
439
440void
441Interest::setSelectedDelegation(size_t delegationIndex)
442{
443 if (delegationIndex >= Link(m_link).getDelegations().size()) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700444 BOOST_THROW_EXCEPTION(Error("Invalid selected delegation index"));
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700445 }
446 m_selectedDelegationIndex = delegationIndex;
447 m_wire.reset();
448}
449
450void
451Interest::unsetSelectedDelegation()
452{
453 m_selectedDelegationIndex = INVALID_SELECTED_DELEGATION_INDEX;
454 m_wire.reset();
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700455}
456
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700457std::ostream&
458operator<<(std::ostream& os, const Interest& interest)
Jeff Thompsonfe556862013-07-09 13:52:55 -0700459{
Alexander Afanasyev84681982014-01-03 13:26:09 -0800460 os << interest.getName();
Jeff Thompsonfe556862013-07-09 13:52:55 -0700461
Alexander Afanasyev84681982014-01-03 13:26:09 -0800462 char delim = '?';
463
464 if (interest.getMinSuffixComponents() >= 0) {
465 os << delim << "ndn.MinSuffixComponents=" << interest.getMinSuffixComponents();
466 delim = '&';
Jeff Thompsonfe556862013-07-09 13:52:55 -0700467 }
Alexander Afanasyev84681982014-01-03 13:26:09 -0800468 if (interest.getMaxSuffixComponents() >= 0) {
469 os << delim << "ndn.MaxSuffixComponents=" << interest.getMaxSuffixComponents();
470 delim = '&';
Jeff Thompson37527d62013-08-21 11:15:54 -0700471 }
Alexander Afanasyev84681982014-01-03 13:26:09 -0800472 if (interest.getChildSelector() >= 0) {
473 os << delim << "ndn.ChildSelector=" << interest.getChildSelector();
474 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -0800475 }
Alexander Afanasyev84681982014-01-03 13:26:09 -0800476 if (interest.getMustBeFresh()) {
477 os << delim << "ndn.MustBeFresh=" << interest.getMustBeFresh();
478 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -0800479 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700480 if (interest.getInterestLifetime() >= time::milliseconds::zero()
481 && interest.getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) {
Alexander Afanasyeva0c5f832014-06-19 13:27:56 -0700482 os << delim << "ndn.InterestLifetime=" << interest.getInterestLifetime().count();
Alexander Afanasyev84681982014-01-03 13:26:09 -0800483 delim = '&';
484 }
485
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300486 if (interest.hasNonce()) {
Alexander Afanasyev84681982014-01-03 13:26:09 -0800487 os << delim << "ndn.Nonce=" << interest.getNonce();
488 delim = '&';
489 }
490 if (!interest.getExclude().empty()) {
491 os << delim << "ndn.Exclude=" << interest.getExclude();
492 delim = '&';
493 }
494
495 return os;
Jeff Thompson13e280b2013-12-03 13:12:23 -0800496}
497
Junxiao Shi08d07082014-12-03 11:31:44 -0700498} // namespace ndn