blob: 986a5ec2e02a8e6c09c8c12cb07fb5e665c73c92 [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 "common.hpp"
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080025
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080026#include "interest.hpp"
Alexander Afanasyeve9fdb802014-02-05 17:36:51 -080027#include "util/random.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
Alexander Afanasyeve881e932014-06-08 14:47:03 +030032uint32_t
Alexander Afanasyev840139f2013-12-28 15:02:50 -080033Interest::getNonce() const
34{
Alexander Afanasyeve881e932014-06-08 14:47:03 +030035 if (!m_nonce.hasWire())
36 const_cast<Interest*>(this)->setNonce(random::generateWord32());
Alexander Afanasyev840139f2013-12-28 15:02:50 -080037
Alexander Afanasyeve881e932014-06-08 14:47:03 +030038 if (m_nonce.value_size() == sizeof(uint32_t))
39 return *reinterpret_cast<const uint32_t*>(m_nonce.value());
40 else {
41 // for compatibility reasons. Should be removed eventually
42 return readNonNegativeInteger(m_nonce);
43 }
Alexander Afanasyev840139f2013-12-28 15:02:50 -080044}
45
Alexander Afanasyeve881e932014-06-08 14:47:03 +030046Interest&
47Interest::setNonce(uint32_t nonce)
48{
49 if (m_wire.hasWire() && m_nonce.value_size() == sizeof(uint32_t)) {
50 std::memcpy(const_cast<uint8_t*>(m_nonce.value()), &nonce, sizeof(nonce));
51 }
52 else {
53 m_nonce = dataBlock(Tlv::Nonce,
54 reinterpret_cast<const uint8_t*>(&nonce),
55 sizeof(nonce));
56 m_wire.reset();
57 }
58 return *this;
59}
Alexander Afanasyev840139f2013-12-28 15:02:50 -080060
Alexander Afanasyev84681982014-01-03 13:26:09 -080061bool
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070062Interest::matchesName(const Name& name) const
Jeff Thompson25b4e612013-10-10 16:03:24 -070063{
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070064 if (name.size() < m_name.size())
65 return false;
66
Alexander Afanasyevc348f832014-02-17 16:35:17 -080067 if (!m_name.isPrefixOf(name))
Alexander Afanasyev84681982014-01-03 13:26:09 -080068 return false;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070069
Alexander Afanasyevc348f832014-02-17 16:35:17 -080070 if (getMinSuffixComponents() >= 0 &&
Alexander Afanasyev3b703102014-06-13 17:01:14 -070071 // name must include implicit digest
72 !(name.size() - m_name.size() >= static_cast<size_t>(getMinSuffixComponents())))
Alexander Afanasyev84681982014-01-03 13:26:09 -080073 return false;
74
Alexander Afanasyevc348f832014-02-17 16:35:17 -080075 if (getMaxSuffixComponents() >= 0 &&
Alexander Afanasyev3b703102014-06-13 17:01:14 -070076 // name must include implicit digest
77 !(name.size() - m_name.size() <= static_cast<size_t>(getMaxSuffixComponents())))
Alexander Afanasyev84681982014-01-03 13:26:09 -080078 return false;
79
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070080 if (!getExclude().empty() &&
81 name.size() > m_name.size() &&
Alexander Afanasyevc348f832014-02-17 16:35:17 -080082 getExclude().isExcluded(name[m_name.size()]))
Alexander Afanasyev84681982014-01-03 13:26:09 -080083 return false;
84
85 return true;
Jeff Thompson25b4e612013-10-10 16:03:24 -070086}
87
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070088bool
89Interest::matchesData(const Data& data) const
90{
Alexander Afanasyev3b703102014-06-13 17:01:14 -070091 if (!this->matchesName(data.getFullName())) {
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070092 return false;
93 }
94
95 const KeyLocator& publisherPublicKeyLocator = this->getPublisherPublicKeyLocator();
96 if (!publisherPublicKeyLocator.empty()) {
97 const Signature& signature = data.getSignature();
98 const Block& signatureInfo = signature.getInfo();
99 Block::element_const_iterator it = signatureInfo.find(Tlv::KeyLocator);
100 if (it == signatureInfo.elements_end()) {
101 return false;
102 }
103 if (publisherPublicKeyLocator.wireEncode() != *it) {
104 return false;
105 }
106 }
107
108 return true;
109}
110
Alexander Afanasyev197e5652014-06-13 16:56:31 -0700111template<bool T>
112size_t
113Interest::wireEncode(EncodingImpl<T>& block) const
114{
115 size_t totalLength = 0;
116
117 // Interest ::= INTEREST-TYPE TLV-LENGTH
118 // Name
119 // Selectors?
120 // Nonce
121 // Scope?
122 // InterestLifetime?
123
124 // (reverse encoding)
125
126 // InterestLifetime
127 if (getInterestLifetime() >= time::milliseconds::zero() &&
128 getInterestLifetime() != DEFAULT_INTEREST_LIFETIME)
129 {
130 totalLength += prependNonNegativeIntegerBlock(block,
131 Tlv::InterestLifetime,
132 getInterestLifetime().count());
133 }
134
135 // Scope
136 if (getScope() >= 0)
137 {
138 totalLength += prependNonNegativeIntegerBlock(block, Tlv::Scope, getScope());
139 }
140
141 // Nonce
142 getNonce(); // to ensure that Nonce is properly set
143 totalLength += block.prependBlock(m_nonce);
144
145 // Selectors
146 if (hasSelectors())
147 {
148 totalLength += getSelectors().wireEncode(block);
149 }
150
151 // Name
152 totalLength += getName().wireEncode(block);
153
154 totalLength += block.prependVarNumber(totalLength);
155 totalLength += block.prependVarNumber(Tlv::Interest);
156 return totalLength;
157}
158
159const Block&
160Interest::wireEncode() const
161{
162 if (m_wire.hasWire())
163 return m_wire;
164
165 EncodingEstimator estimator;
166 size_t estimatedSize = wireEncode(estimator);
167
168 EncodingBuffer buffer(estimatedSize, 0);
169 wireEncode(buffer);
170
171 // to ensure that Nonce block points to the right memory location
172 const_cast<Interest*>(this)->wireDecode(buffer.block());
173
174 return m_wire;
175}
176
177void
178Interest::wireDecode(const Block& wire)
179{
180 m_wire = wire;
181 m_wire.parse();
182
183 // Interest ::= INTEREST-TYPE TLV-LENGTH
184 // Name
185 // Selectors?
186 // Nonce
187 // Scope?
188 // InterestLifetime?
189
190 if (m_wire.type() != Tlv::Interest)
191 throw Tlv::Error("Unexpected TLV number when decoding Interest");
192
193 // Name
194 m_name.wireDecode(m_wire.get(Tlv::Name));
195
196 // Selectors
197 Block::element_const_iterator val = m_wire.find(Tlv::Selectors);
198 if (val != m_wire.elements_end())
199 {
200 m_selectors.wireDecode(*val);
201 }
202 else
203 m_selectors = Selectors();
204
205 // Nonce
206 m_nonce = m_wire.get(Tlv::Nonce);
207
208 // Scope
209 val = m_wire.find(Tlv::Scope);
210 if (val != m_wire.elements_end())
211 {
212 m_scope = readNonNegativeInteger(*val);
213 }
214 else
215 m_scope = -1;
216
217 // InterestLifetime
218 val = m_wire.find(Tlv::InterestLifetime);
219 if (val != m_wire.elements_end())
220 {
221 m_interestLifetime = time::milliseconds(readNonNegativeInteger(*val));
222 }
223 else
224 {
225 m_interestLifetime = DEFAULT_INTEREST_LIFETIME;
226 }
227}
228
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700229std::ostream&
230operator<<(std::ostream& os, const Interest& interest)
Jeff Thompsonfe556862013-07-09 13:52:55 -0700231{
Alexander Afanasyev84681982014-01-03 13:26:09 -0800232 os << interest.getName();
Jeff Thompsonfe556862013-07-09 13:52:55 -0700233
Alexander Afanasyev84681982014-01-03 13:26:09 -0800234 char delim = '?';
235
236 if (interest.getMinSuffixComponents() >= 0) {
237 os << delim << "ndn.MinSuffixComponents=" << interest.getMinSuffixComponents();
238 delim = '&';
Jeff Thompsonfe556862013-07-09 13:52:55 -0700239 }
Alexander Afanasyev84681982014-01-03 13:26:09 -0800240 if (interest.getMaxSuffixComponents() >= 0) {
241 os << delim << "ndn.MaxSuffixComponents=" << interest.getMaxSuffixComponents();
242 delim = '&';
Jeff Thompson37527d62013-08-21 11:15:54 -0700243 }
Alexander Afanasyev84681982014-01-03 13:26:09 -0800244 if (interest.getChildSelector() >= 0) {
245 os << delim << "ndn.ChildSelector=" << interest.getChildSelector();
246 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -0800247 }
Alexander Afanasyev84681982014-01-03 13:26:09 -0800248 if (interest.getMustBeFresh()) {
249 os << delim << "ndn.MustBeFresh=" << interest.getMustBeFresh();
250 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -0800251 }
Alexander Afanasyev84681982014-01-03 13:26:09 -0800252 if (interest.getScope() >= 0) {
253 os << delim << "ndn.Scope=" << interest.getScope();
254 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -0800255 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700256 if (interest.getInterestLifetime() >= time::milliseconds::zero()
257 && interest.getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) {
Alexander Afanasyev84681982014-01-03 13:26:09 -0800258 os << delim << "ndn.InterestLifetime=" << interest.getInterestLifetime();
259 delim = '&';
260 }
261
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300262 if (interest.hasNonce()) {
Alexander Afanasyev84681982014-01-03 13:26:09 -0800263 os << delim << "ndn.Nonce=" << interest.getNonce();
264 delim = '&';
265 }
266 if (!interest.getExclude().empty()) {
267 os << delim << "ndn.Exclude=" << interest.getExclude();
268 delim = '&';
269 }
270
271 return os;
Jeff Thompson13e280b2013-12-03 13:12:23 -0800272}
273
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700274}