blob: 7f4579f4ea25abe2c9710759b2eab1072b2a9d6f [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/**
Junxiao Shia1478db2016-09-09 04:13:15 +00003 * Copyright (c) 2013-2016 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
Jeff Thompsonb7f95562013-07-03 18:36:42 -070027namespace ndn {
Alexander Afanasyev84681982014-01-03 13:26:09 -080028
Junxiao Shic2b8d242014-11-04 08:35:29 -070029BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Interest>));
30BOOST_CONCEPT_ASSERT((WireEncodable<Interest>));
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070031BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<Interest>));
Junxiao Shic2b8d242014-11-04 08:35:29 -070032BOOST_CONCEPT_ASSERT((WireDecodable<Interest>));
33static_assert(std::is_base_of<tlv::Error, Interest::Error>::value,
34 "Interest::Error must inherit from tlv::Error");
35
Junxiao Shi2af905b2014-11-27 13:10:54 -070036Interest::Interest()
Alexander Afanasyev117f5ef2015-06-03 15:07:24 -070037 : m_interestLifetime(time::milliseconds::min())
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -070038 , m_selectedDelegationIndex(INVALID_SELECTED_DELEGATION_INDEX)
Junxiao Shi2af905b2014-11-27 13:10:54 -070039{
40}
41
42Interest::Interest(const Name& name)
43 : m_name(name)
Junxiao Shi2af905b2014-11-27 13:10:54 -070044 , m_interestLifetime(time::milliseconds::min())
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -070045 , m_selectedDelegationIndex(INVALID_SELECTED_DELEGATION_INDEX)
Junxiao Shi2af905b2014-11-27 13:10:54 -070046{
47}
48
49Interest::Interest(const Name& name, const time::milliseconds& interestLifetime)
50 : m_name(name)
Junxiao Shi2af905b2014-11-27 13:10:54 -070051 , m_interestLifetime(interestLifetime)
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -070052 , m_selectedDelegationIndex(INVALID_SELECTED_DELEGATION_INDEX)
Junxiao Shi2af905b2014-11-27 13:10:54 -070053{
54}
55
Junxiao Shi2af905b2014-11-27 13:10:54 -070056Interest::Interest(const Block& wire)
57{
58 wireDecode(wire);
59}
60
Alexander Afanasyeve881e932014-06-08 14:47:03 +030061uint32_t
Alexander Afanasyev840139f2013-12-28 15:02:50 -080062Interest::getNonce() const
63{
Alexander Afanasyeve881e932014-06-08 14:47:03 +030064 if (!m_nonce.hasWire())
65 const_cast<Interest*>(this)->setNonce(random::generateWord32());
Alexander Afanasyev840139f2013-12-28 15:02:50 -080066
Alexander Afanasyeve881e932014-06-08 14:47:03 +030067 if (m_nonce.value_size() == sizeof(uint32_t))
68 return *reinterpret_cast<const uint32_t*>(m_nonce.value());
69 else {
70 // for compatibility reasons. Should be removed eventually
71 return readNonNegativeInteger(m_nonce);
72 }
Alexander Afanasyev840139f2013-12-28 15:02:50 -080073}
74
Alexander Afanasyeve881e932014-06-08 14:47:03 +030075Interest&
76Interest::setNonce(uint32_t nonce)
77{
78 if (m_wire.hasWire() && m_nonce.value_size() == sizeof(uint32_t)) {
79 std::memcpy(const_cast<uint8_t*>(m_nonce.value()), &nonce, sizeof(nonce));
80 }
81 else {
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070082 m_nonce = makeBinaryBlock(tlv::Nonce,
83 reinterpret_cast<const uint8_t*>(&nonce),
84 sizeof(nonce));
Alexander Afanasyeve881e932014-06-08 14:47:03 +030085 m_wire.reset();
86 }
87 return *this;
88}
Alexander Afanasyev840139f2013-12-28 15:02:50 -080089
Alexander Afanasyevc3932172014-07-10 18:53:56 -070090void
91Interest::refreshNonce()
92{
93 if (!hasNonce())
94 return;
95
96 uint32_t oldNonce = getNonce();
97 uint32_t newNonce = oldNonce;
98 while (newNonce == oldNonce)
99 newNonce = random::generateWord32();
100
101 setNonce(newNonce);
102}
103
Alexander Afanasyev84681982014-01-03 13:26:09 -0800104bool
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700105Interest::matchesName(const Name& name) const
Jeff Thompson25b4e612013-10-10 16:03:24 -0700106{
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700107 if (name.size() < m_name.size())
108 return false;
109
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800110 if (!m_name.isPrefixOf(name))
Alexander Afanasyev84681982014-01-03 13:26:09 -0800111 return false;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700112
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800113 if (getMinSuffixComponents() >= 0 &&
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700114 // name must include implicit digest
115 !(name.size() - m_name.size() >= static_cast<size_t>(getMinSuffixComponents())))
Alexander Afanasyev84681982014-01-03 13:26:09 -0800116 return false;
117
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800118 if (getMaxSuffixComponents() >= 0 &&
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700119 // name must include implicit digest
120 !(name.size() - m_name.size() <= static_cast<size_t>(getMaxSuffixComponents())))
Alexander Afanasyev84681982014-01-03 13:26:09 -0800121 return false;
122
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700123 if (!getExclude().empty() &&
124 name.size() > m_name.size() &&
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800125 getExclude().isExcluded(name[m_name.size()]))
Alexander Afanasyev84681982014-01-03 13:26:09 -0800126 return false;
127
128 return true;
Jeff Thompson25b4e612013-10-10 16:03:24 -0700129}
130
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700131bool
132Interest::matchesData(const Data& data) const
133{
Junxiao Shi42c23622014-07-03 00:55:11 -0700134 size_t interestNameLength = m_name.size();
135 const Name& dataName = data.getName();
136 size_t fullNameLength = dataName.size() + 1;
137
138 // check MinSuffixComponents
139 bool hasMinSuffixComponents = getMinSuffixComponents() >= 0;
140 size_t minSuffixComponents = hasMinSuffixComponents ?
141 static_cast<size_t>(getMinSuffixComponents()) : 0;
142 if (!(interestNameLength + minSuffixComponents <= fullNameLength))
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700143 return false;
Junxiao Shi42c23622014-07-03 00:55:11 -0700144
145 // check MaxSuffixComponents
146 bool hasMaxSuffixComponents = getMaxSuffixComponents() >= 0;
147 if (hasMaxSuffixComponents &&
148 !(interestNameLength + getMaxSuffixComponents() >= fullNameLength))
149 return false;
150
151 // check prefix
152 if (interestNameLength == fullNameLength) {
Alexander Afanasyev56860f52014-11-07 11:51:17 -0800153 if (m_name.get(-1).isImplicitSha256Digest()) {
154 if (m_name != data.getFullName())
Junxiao Shi42c23622014-07-03 00:55:11 -0700155 return false;
156 }
157 else {
158 // Interest Name is same length as Data full Name, but last component isn't digest
159 // so there's no possibility of matching
160 return false;
161 }
162 }
163 else {
164 // Interest Name is a strict prefix of Data full Name
165 if (!m_name.isPrefixOf(dataName))
166 return false;
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700167 }
168
Junxiao Shi42c23622014-07-03 00:55:11 -0700169 // check Exclude
170 // Exclude won't be violated if Interest Name is same as Data full Name
171 if (!getExclude().empty() && fullNameLength > interestNameLength) {
172 if (interestNameLength == fullNameLength - 1) {
173 // component to exclude is the digest
174 if (getExclude().isExcluded(data.getFullName().get(interestNameLength)))
175 return false;
176 // There's opportunity to inspect the Exclude filter and determine whether
177 // the digest would make a difference.
Junxiao Shi08d07082014-12-03 11:31:44 -0700178 // eg. "<NameComponent>AA</NameComponent><Any/>" doesn't exclude any digest -
179 // fullName not needed;
180 // "<Any/><NameComponent>AA</NameComponent>" and
181 // "<Any/><ImplicitSha256DigestComponent>ffffffffffffffffffffffffffffffff
182 // </ImplicitSha256DigestComponent>"
183 // excludes all digests - fullName not needed;
184 // "<Any/><ImplicitSha256DigestComponent>80000000000000000000000000000000
185 // </ImplicitSha256DigestComponent>"
186 // excludes some digests - fullName required
Junxiao Shi42c23622014-07-03 00:55:11 -0700187 // But Interests that contain the exact Data Name before digest and also
188 // contain Exclude filter is too rare to optimize for, so we request
189 // fullName no mater what's in the Exclude filter.
190 }
191 else {
192 // component to exclude is not the digest
193 if (getExclude().isExcluded(dataName.get(interestNameLength)))
194 return false;
195 }
196 }
197
198 // check PublisherPublicKeyLocator
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700199 const KeyLocator& publisherPublicKeyLocator = this->getPublisherPublicKeyLocator();
200 if (!publisherPublicKeyLocator.empty()) {
201 const Signature& signature = data.getSignature();
202 const Block& signatureInfo = signature.getInfo();
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600203 Block::element_const_iterator it = signatureInfo.find(tlv::KeyLocator);
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700204 if (it == signatureInfo.elements_end()) {
205 return false;
206 }
207 if (publisherPublicKeyLocator.wireEncode() != *it) {
208 return false;
209 }
210 }
211
212 return true;
213}
214
Alexander Afanasyev74633892015-02-08 18:08:46 -0800215template<encoding::Tag TAG>
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700216size_t
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700217Interest::wireEncode(EncodingImpl<TAG>& encoder) const
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700218{
219 size_t totalLength = 0;
220
221 // Interest ::= INTEREST-TYPE TLV-LENGTH
222 // Name
223 // Selectors?
224 // Nonce
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700225 // InterestLifetime?
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700226 // Link?
227 // SelectedDelegation?
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700228
229 // (reverse encoding)
230
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700231 if (hasLink()) {
232 if (hasSelectedDelegation()) {
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700233 totalLength += prependNonNegativeIntegerBlock(encoder,
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700234 tlv::SelectedDelegation,
235 m_selectedDelegationIndex);
236 }
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700237 totalLength += encoder.prependBlock(m_link);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700238 }
239 else {
240 BOOST_ASSERT(!hasSelectedDelegation());
241 }
242
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700243 // InterestLifetime
244 if (getInterestLifetime() >= time::milliseconds::zero() &&
245 getInterestLifetime() != DEFAULT_INTEREST_LIFETIME)
246 {
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700247 totalLength += prependNonNegativeIntegerBlock(encoder,
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600248 tlv::InterestLifetime,
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700249 getInterestLifetime().count());
250 }
251
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700252 // Nonce
253 getNonce(); // to ensure that Nonce is properly set
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700254 totalLength += encoder.prependBlock(m_nonce);
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700255
256 // Selectors
257 if (hasSelectors())
258 {
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700259 totalLength += getSelectors().wireEncode(encoder);
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700260 }
261
262 // Name
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700263 totalLength += getName().wireEncode(encoder);
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700264
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700265 totalLength += encoder.prependVarNumber(totalLength);
266 totalLength += encoder.prependVarNumber(tlv::Interest);
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700267 return totalLength;
268}
269
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700270template size_t
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700271Interest::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const;
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700272
273template size_t
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -0700274Interest::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const;
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700275
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700276const Block&
277Interest::wireEncode() const
278{
279 if (m_wire.hasWire())
280 return m_wire;
281
282 EncodingEstimator estimator;
283 size_t estimatedSize = wireEncode(estimator);
284
285 EncodingBuffer buffer(estimatedSize, 0);
286 wireEncode(buffer);
287
288 // to ensure that Nonce block points to the right memory location
289 const_cast<Interest*>(this)->wireDecode(buffer.block());
290
291 return m_wire;
292}
293
294void
295Interest::wireDecode(const Block& wire)
296{
297 m_wire = wire;
298 m_wire.parse();
299
300 // Interest ::= INTEREST-TYPE TLV-LENGTH
301 // Name
302 // Selectors?
303 // Nonce
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700304 // InterestLifetime?
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700305 // Link?
306 // SelectedDelegation?
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700307
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600308 if (m_wire.type() != tlv::Interest)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700309 BOOST_THROW_EXCEPTION(Error("Unexpected TLV number when decoding Interest"));
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700310
311 // Name
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600312 m_name.wireDecode(m_wire.get(tlv::Name));
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700313
314 // Selectors
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600315 Block::element_const_iterator val = m_wire.find(tlv::Selectors);
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700316 if (val != m_wire.elements_end()) {
317 m_selectors.wireDecode(*val);
318 }
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700319 else
320 m_selectors = Selectors();
321
322 // Nonce
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600323 m_nonce = m_wire.get(tlv::Nonce);
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700324
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700325 // InterestLifetime
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600326 val = m_wire.find(tlv::InterestLifetime);
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700327 if (val != m_wire.elements_end()) {
328 m_interestLifetime = time::milliseconds(readNonNegativeInteger(*val));
329 }
330 else {
331 m_interestLifetime = DEFAULT_INTEREST_LIFETIME;
332 }
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700333
334 // Link object
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700335 m_linkCached.reset();
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700336 val = m_wire.find(tlv::Data);
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700337 if (val != m_wire.elements_end()) {
338 m_link = (*val);
339 }
340 else {
341 m_link = Block();
342 }
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700343
344 // SelectedDelegation
345 val = m_wire.find(tlv::SelectedDelegation);
346 if (val != m_wire.elements_end()) {
347 if (!this->hasLink()) {
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700348 BOOST_THROW_EXCEPTION(Error("Interest contains SelectedDelegation, but no LINK object"));
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700349 }
350 uint64_t selectedDelegation = readNonNegativeInteger(*val);
351 if (selectedDelegation < uint64_t(Link::countDelegationsFromWire(m_link))) {
352 m_selectedDelegationIndex = static_cast<size_t>(selectedDelegation);
353 }
354 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700355 BOOST_THROW_EXCEPTION(Error("Invalid selected delegation index when decoding Interest"));
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700356 }
357 }
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700358 else {
359 m_selectedDelegationIndex = INVALID_SELECTED_DELEGATION_INDEX;
360 }
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700361}
362
363bool
364Interest::hasLink() const
365{
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700366 return m_link.hasWire();
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700367}
368
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700369const Link&
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700370Interest::getLink() const
371{
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700372 if (hasLink()) {
373 if (!m_linkCached) {
374 m_linkCached = make_shared<Link>(m_link);
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700375 }
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700376 return *m_linkCached;
377 }
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700378 BOOST_THROW_EXCEPTION(Error("There is no encapsulated link object"));
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700379}
380
381void
382Interest::setLink(const Block& link)
383{
384 m_link = link;
385 if (!link.hasWire()) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700386 BOOST_THROW_EXCEPTION(Error("The given link does not have a wire format"));
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700387 }
388 m_wire.reset();
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700389 m_linkCached.reset();
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700390 this->unsetSelectedDelegation();
391}
392
393void
394Interest::unsetLink()
395{
396 m_link.reset();
397 m_wire.reset();
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700398 m_linkCached.reset();
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700399 this->unsetSelectedDelegation();
400}
401
402bool
403Interest::hasSelectedDelegation() const
404{
Alexander Afanasyevcac08382015-09-02 14:52:40 -0700405 return m_selectedDelegationIndex != INVALID_SELECTED_DELEGATION_INDEX;
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700406}
407
408Name
409Interest::getSelectedDelegation() const
410{
411 if (!hasSelectedDelegation()) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700412 BOOST_THROW_EXCEPTION(Error("There is no encapsulated selected delegation"));
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700413 }
414 return std::get<1>(Link::getDelegationFromWire(m_link, m_selectedDelegationIndex));
415}
416
417void
418Interest::setSelectedDelegation(const Name& delegationName)
419{
420 size_t delegationIndex = Link::findDelegationFromWire(m_link, delegationName);
421 if (delegationIndex != INVALID_SELECTED_DELEGATION_INDEX) {
422 m_selectedDelegationIndex = delegationIndex;
423 }
424 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700425 BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid selected delegation name"));
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700426 }
427 m_wire.reset();
428}
429
430void
431Interest::setSelectedDelegation(size_t delegationIndex)
432{
433 if (delegationIndex >= Link(m_link).getDelegations().size()) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700434 BOOST_THROW_EXCEPTION(Error("Invalid selected delegation index"));
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700435 }
436 m_selectedDelegationIndex = delegationIndex;
437 m_wire.reset();
438}
439
440void
441Interest::unsetSelectedDelegation()
442{
443 m_selectedDelegationIndex = INVALID_SELECTED_DELEGATION_INDEX;
444 m_wire.reset();
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700445}
446
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700447std::ostream&
448operator<<(std::ostream& os, const Interest& interest)
Jeff Thompsonfe556862013-07-09 13:52:55 -0700449{
Alexander Afanasyev84681982014-01-03 13:26:09 -0800450 os << interest.getName();
Jeff Thompsonfe556862013-07-09 13:52:55 -0700451
Alexander Afanasyev84681982014-01-03 13:26:09 -0800452 char delim = '?';
453
454 if (interest.getMinSuffixComponents() >= 0) {
455 os << delim << "ndn.MinSuffixComponents=" << interest.getMinSuffixComponents();
456 delim = '&';
Jeff Thompsonfe556862013-07-09 13:52:55 -0700457 }
Alexander Afanasyev84681982014-01-03 13:26:09 -0800458 if (interest.getMaxSuffixComponents() >= 0) {
459 os << delim << "ndn.MaxSuffixComponents=" << interest.getMaxSuffixComponents();
460 delim = '&';
Jeff Thompson37527d62013-08-21 11:15:54 -0700461 }
Alexander Afanasyev84681982014-01-03 13:26:09 -0800462 if (interest.getChildSelector() >= 0) {
463 os << delim << "ndn.ChildSelector=" << interest.getChildSelector();
464 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -0800465 }
Alexander Afanasyev84681982014-01-03 13:26:09 -0800466 if (interest.getMustBeFresh()) {
467 os << delim << "ndn.MustBeFresh=" << interest.getMustBeFresh();
468 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -0800469 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700470 if (interest.getInterestLifetime() >= time::milliseconds::zero()
471 && interest.getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) {
Alexander Afanasyeva0c5f832014-06-19 13:27:56 -0700472 os << delim << "ndn.InterestLifetime=" << interest.getInterestLifetime().count();
Alexander Afanasyev84681982014-01-03 13:26:09 -0800473 delim = '&';
474 }
475
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300476 if (interest.hasNonce()) {
Alexander Afanasyev84681982014-01-03 13:26:09 -0800477 os << delim << "ndn.Nonce=" << interest.getNonce();
478 delim = '&';
479 }
480 if (!interest.getExclude().empty()) {
481 os << delim << "ndn.Exclude=" << interest.getExclude();
482 delim = '&';
483 }
484
485 return os;
Jeff Thompson13e280b2013-12-03 13:12:23 -0800486}
487
Junxiao Shi08d07082014-12-03 11:31:44 -0700488} // namespace ndn