blob: c9be23d14102eca20108d28f7f755b73b04878a2 [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompson47eecfc2013-07-07 22:56:46 -07002/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07003 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson47eecfc2013-07-07 22:56:46 -07005 * See COPYING for copyright and distribution information.
Jeff Thompsonb7f95562013-07-03 18:36:42 -07006 */
7
Jeff Thompsonfe556862013-07-09 13:52:55 -07008#include <stdexcept>
Jeff Thompson25b4e612013-10-10 16:03:24 -07009#include <ndn-cpp/common.hpp>
10#include <ndn-cpp/interest.hpp>
Jeff Thompsonb7f95562013-07-03 18:36:42 -070011
Alexander Afanasyev85480842014-01-06 14:46:54 -080012#if __clang__
13#pragma clang diagnostic push
14#pragma clang diagnostic ignored "-Wreorder"
15#pragma clang diagnostic ignored "-Wtautological-compare"
16#pragma clang diagnostic ignored "-Wunused-variable"
17#pragma clang diagnostic ignored "-Wunused-function"
18#elif __GNUC__
19#pragma GCC diagnostic ignored "-Wreorder"
Alexander Afanasyev85480842014-01-06 14:46:54 -080020#pragma GCC diagnostic ignored "-Wunused-variable"
21#pragma GCC diagnostic ignored "-Wunused-function"
22#endif
23
Alexander Afanasyev840139f2013-12-28 15:02:50 -080024#include <cryptopp/osrng.h>
25
Jeff Thompsonb7f95562013-07-03 18:36:42 -070026using namespace std;
27
28namespace ndn {
Alexander Afanasyev84681982014-01-03 13:26:09 -080029
Alexander Afanasyev85480842014-01-06 14:46:54 -080030const uint32_t&
Alexander Afanasyev840139f2013-12-28 15:02:50 -080031Interest::getNonce() const
32{
33 static CryptoPP::AutoSeededRandomPool rng;
34
35 if (nonce_ == 0)
36 nonce_ = rng.GenerateWord32();
37
38 return nonce_;
39}
40
41
Alexander Afanasyev84681982014-01-03 13:26:09 -080042bool
43Interest::matchesName(const Name &name) const
Jeff Thompson25b4e612013-10-10 16:03:24 -070044{
Alexander Afanasyev84681982014-01-03 13:26:09 -080045 if (!name_.isPrefixOf(name))
46 return false;
47
48 if (minSuffixComponents_ >= 0 &&
49 // Add 1 for the implicit digest.
50 !(name.size() + 1 - name_.size() >= minSuffixComponents_))
51 return false;
52
53 if (maxSuffixComponents_ >= 0 &&
54 // Add 1 for the implicit digest.
55 !(name.size() + 1 - name_.size() <= maxSuffixComponents_))
56 return false;
57
58 if (!exclude_.empty() && name.size() > name_.size() &&
59 exclude_.isExcluded(name[name_.size()]))
60 return false;
61
62 return true;
Jeff Thompson25b4e612013-10-10 16:03:24 -070063}
64
Alexander Afanasyev84681982014-01-03 13:26:09 -080065std::ostream &
66operator << (std::ostream &os, const Interest &interest)
Jeff Thompsonfe556862013-07-09 13:52:55 -070067{
Alexander Afanasyev84681982014-01-03 13:26:09 -080068 os << interest.getName();
Jeff Thompsonfe556862013-07-09 13:52:55 -070069
Alexander Afanasyev84681982014-01-03 13:26:09 -080070 char delim = '?';
71
72 if (interest.getMinSuffixComponents() >= 0) {
73 os << delim << "ndn.MinSuffixComponents=" << interest.getMinSuffixComponents();
74 delim = '&';
Jeff Thompsonfe556862013-07-09 13:52:55 -070075 }
Alexander Afanasyev84681982014-01-03 13:26:09 -080076 if (interest.getMaxSuffixComponents() >= 0) {
77 os << delim << "ndn.MaxSuffixComponents=" << interest.getMaxSuffixComponents();
78 delim = '&';
Jeff Thompson37527d62013-08-21 11:15:54 -070079 }
Alexander Afanasyev84681982014-01-03 13:26:09 -080080 if (interest.getChildSelector() >= 0) {
81 os << delim << "ndn.ChildSelector=" << interest.getChildSelector();
82 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -080083 }
Alexander Afanasyev84681982014-01-03 13:26:09 -080084 if (interest.getMustBeFresh()) {
85 os << delim << "ndn.MustBeFresh=" << interest.getMustBeFresh();
86 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -080087 }
Alexander Afanasyev84681982014-01-03 13:26:09 -080088 if (interest.getScope() >= 0) {
89 os << delim << "ndn.Scope=" << interest.getScope();
90 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -080091 }
Alexander Afanasyev84681982014-01-03 13:26:09 -080092 if (interest.getInterestLifetime() >= 0) {
93 os << delim << "ndn.InterestLifetime=" << interest.getInterestLifetime();
94 delim = '&';
95 }
96
97 if (interest.getNonce() > 0) {
98 os << delim << "ndn.Nonce=" << interest.getNonce();
99 delim = '&';
100 }
101 if (!interest.getExclude().empty()) {
102 os << delim << "ndn.Exclude=" << interest.getExclude();
103 delim = '&';
104 }
105
106 return os;
Jeff Thompson13e280b2013-12-03 13:12:23 -0800107}
108
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800109const Block&
110Interest::wireEncode() const
111{
112 if (wire_.hasWire())
113 return wire_;
114
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800115 // Interest ::= INTEREST-TYPE TLV-LENGTH
116 // Name
117 // Selectors?
118 // Nonce
119 // Scope?
120 // InterestLifetime?
121
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800122 wire_ = Block(Tlv::Interest);
123 wire_.push_back(getName().wireEncode());
124
125 // selectors
126 {
127 Block selectors(Tlv::Selectors);
128
129 if (getMinSuffixComponents() >= 0) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800130 selectors.push_back
131 (nonNegativeIntegerBlock(Tlv::MinSuffixComponents, getMinSuffixComponents()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800132 }
133 if (getMaxSuffixComponents() >= 0) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800134 selectors.push_back
135 (nonNegativeIntegerBlock(Tlv::MaxSuffixComponents, getMaxSuffixComponents()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800136 }
137 if (!getExclude().empty()) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800138 selectors.push_back
139 (getExclude().wireEncode());
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800140 }
141 if (getChildSelector() >= 0) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800142 selectors.push_back
143 (nonNegativeIntegerBlock(Tlv::ChildSelector, getChildSelector()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800144 }
145 if (getMustBeFresh()) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800146 selectors.push_back
147 (booleanBlock(Tlv::MustBeFresh));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800148 }
149
Alexander Afanasyev840139f2013-12-28 15:02:50 -0800150 if (!selectors.getAll().empty())
151 {
152 selectors.encode();
153 wire_.push_back(selectors);
154 }
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800155 }
156
157 // Nonce
158 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800159 wire_.push_back
160 (nonNegativeIntegerBlock(Tlv::Nonce, getNonce()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800161 }
162
163 if (getScope() >= 0) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800164 wire_.push_back
165 (nonNegativeIntegerBlock(Tlv::Scope, getScope()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800166 }
167 if (getInterestLifetime() >= 0) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800168 wire_.push_back
169 (nonNegativeIntegerBlock(Tlv::InterestLifetime, getInterestLifetime()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800170 }
171
172 wire_.encode();
173 return wire_;
174}
175
176void
177Interest::wireDecode(const Block &wire)
178{
179 wire_ = wire;
180 wire_.parse();
181
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800182 // Interest ::= INTEREST-TYPE TLV-LENGTH
183 // Name
184 // Selectors?
185 // Nonce
186 // Scope?
187 // InterestLifetime?
188
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800189 // Name
190 name_.wireDecode(wire_.get(Tlv::Name));
191
192 // Selectors
193 Block::element_iterator selectors = wire_.find(Tlv::Selectors);
194 if (selectors != wire_.getAll().end())
195 {
196 selectors->parse();
197
198 // MinSuffixComponents
199 Block::element_iterator val = selectors->find(Tlv::MinSuffixComponents);
200 if (val != selectors->getAll().end())
201 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800202 minSuffixComponents_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800203 }
204
205 // MaxSuffixComponents
206 val = selectors->find(Tlv::MaxSuffixComponents);
207 if (val != selectors->getAll().end())
208 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800209 maxSuffixComponents_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800210 }
211
212 // Exclude
213 val = selectors->find(Tlv::Exclude);
214 if (val != selectors->getAll().end())
215 {
216 exclude_.wireDecode(*val);
217 }
218
219 // ChildSelector
220 val = selectors->find(Tlv::ChildSelector);
221 if (val != selectors->getAll().end())
222 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800223 childSelector_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800224 }
225
226 //MustBeFresh aka AnswerOriginKind
227 val = selectors->find(Tlv::MustBeFresh);
228 if (val != selectors->getAll().end())
229 {
230 mustBeFresh_ = true;
231 }
232 }
233
234 // Nonce
235 Block::element_iterator val = wire_.find(Tlv::Nonce);
236 if (val != wire_.getAll().end())
237 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800238 nonce_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800239 }
240
241 // Scope
242 val = wire_.find(Tlv::Scope);
243 if (val != wire_.getAll().end())
244 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800245 scope_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800246 }
247
248 // InterestLifetime
249 val = wire_.find(Tlv::InterestLifetime);
250 if (val != wire_.getAll().end())
251 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800252 interestLifetime_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800253 }
254}
255
256
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700257}