blob: c4046a4da1e7fce32a453ba0a6a05127d615f304 [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 Afanasyev929e86d2014-01-17 16:11:25 -080030const Milliseconds DEFAULT_INTEREST_LIFETIME = 4000;
31
Alexander Afanasyev85480842014-01-06 14:46:54 -080032const uint32_t&
Alexander Afanasyev840139f2013-12-28 15:02:50 -080033Interest::getNonce() const
34{
35 static CryptoPP::AutoSeededRandomPool rng;
36
37 if (nonce_ == 0)
38 nonce_ = rng.GenerateWord32();
39
40 return nonce_;
41}
42
43
Alexander Afanasyev84681982014-01-03 13:26:09 -080044bool
45Interest::matchesName(const Name &name) const
Jeff Thompson25b4e612013-10-10 16:03:24 -070046{
Alexander Afanasyev84681982014-01-03 13:26:09 -080047 if (!name_.isPrefixOf(name))
48 return false;
49
50 if (minSuffixComponents_ >= 0 &&
51 // Add 1 for the implicit digest.
52 !(name.size() + 1 - name_.size() >= minSuffixComponents_))
53 return false;
54
55 if (maxSuffixComponents_ >= 0 &&
56 // Add 1 for the implicit digest.
57 !(name.size() + 1 - name_.size() <= maxSuffixComponents_))
58 return false;
59
60 if (!exclude_.empty() && name.size() > name_.size() &&
61 exclude_.isExcluded(name[name_.size()]))
62 return false;
63
64 return true;
Jeff Thompson25b4e612013-10-10 16:03:24 -070065}
66
Alexander Afanasyev84681982014-01-03 13:26:09 -080067std::ostream &
68operator << (std::ostream &os, const Interest &interest)
Jeff Thompsonfe556862013-07-09 13:52:55 -070069{
Alexander Afanasyev84681982014-01-03 13:26:09 -080070 os << interest.getName();
Jeff Thompsonfe556862013-07-09 13:52:55 -070071
Alexander Afanasyev84681982014-01-03 13:26:09 -080072 char delim = '?';
73
74 if (interest.getMinSuffixComponents() >= 0) {
75 os << delim << "ndn.MinSuffixComponents=" << interest.getMinSuffixComponents();
76 delim = '&';
Jeff Thompsonfe556862013-07-09 13:52:55 -070077 }
Alexander Afanasyev84681982014-01-03 13:26:09 -080078 if (interest.getMaxSuffixComponents() >= 0) {
79 os << delim << "ndn.MaxSuffixComponents=" << interest.getMaxSuffixComponents();
80 delim = '&';
Jeff Thompson37527d62013-08-21 11:15:54 -070081 }
Alexander Afanasyev84681982014-01-03 13:26:09 -080082 if (interest.getChildSelector() >= 0) {
83 os << delim << "ndn.ChildSelector=" << interest.getChildSelector();
84 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -080085 }
Alexander Afanasyev84681982014-01-03 13:26:09 -080086 if (interest.getMustBeFresh()) {
87 os << delim << "ndn.MustBeFresh=" << interest.getMustBeFresh();
88 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -080089 }
Alexander Afanasyev84681982014-01-03 13:26:09 -080090 if (interest.getScope() >= 0) {
91 os << delim << "ndn.Scope=" << interest.getScope();
92 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -080093 }
Alexander Afanasyev929e86d2014-01-17 16:11:25 -080094 if (interest.getInterestLifetime() >= 0 && interest.getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) {
Alexander Afanasyev84681982014-01-03 13:26:09 -080095 os << delim << "ndn.InterestLifetime=" << interest.getInterestLifetime();
96 delim = '&';
97 }
98
99 if (interest.getNonce() > 0) {
100 os << delim << "ndn.Nonce=" << interest.getNonce();
101 delim = '&';
102 }
103 if (!interest.getExclude().empty()) {
104 os << delim << "ndn.Exclude=" << interest.getExclude();
105 delim = '&';
106 }
107
108 return os;
Jeff Thompson13e280b2013-12-03 13:12:23 -0800109}
110
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800111const Block&
112Interest::wireEncode() const
113{
114 if (wire_.hasWire())
115 return wire_;
116
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800117 // Interest ::= INTEREST-TYPE TLV-LENGTH
118 // Name
119 // Selectors?
120 // Nonce
121 // Scope?
122 // InterestLifetime?
123
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800124 wire_ = Block(Tlv::Interest);
125 wire_.push_back(getName().wireEncode());
126
127 // selectors
128 {
129 Block selectors(Tlv::Selectors);
130
131 if (getMinSuffixComponents() >= 0) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800132 selectors.push_back
133 (nonNegativeIntegerBlock(Tlv::MinSuffixComponents, getMinSuffixComponents()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800134 }
135 if (getMaxSuffixComponents() >= 0) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800136 selectors.push_back
137 (nonNegativeIntegerBlock(Tlv::MaxSuffixComponents, getMaxSuffixComponents()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800138 }
139 if (!getExclude().empty()) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800140 selectors.push_back
141 (getExclude().wireEncode());
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800142 }
143 if (getChildSelector() >= 0) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800144 selectors.push_back
145 (nonNegativeIntegerBlock(Tlv::ChildSelector, getChildSelector()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800146 }
147 if (getMustBeFresh()) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800148 selectors.push_back
149 (booleanBlock(Tlv::MustBeFresh));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800150 }
151
Alexander Afanasyev840139f2013-12-28 15:02:50 -0800152 if (!selectors.getAll().empty())
153 {
154 selectors.encode();
155 wire_.push_back(selectors);
156 }
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800157 }
158
159 // Nonce
160 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800161 wire_.push_back
162 (nonNegativeIntegerBlock(Tlv::Nonce, getNonce()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800163 }
Alexander Afanasyev929e86d2014-01-17 16:11:25 -0800164
165 // Scope
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800166 if (getScope() >= 0) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800167 wire_.push_back
168 (nonNegativeIntegerBlock(Tlv::Scope, getScope()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800169 }
Alexander Afanasyev929e86d2014-01-17 16:11:25 -0800170
171 // InterestLifetime
172 if (getInterestLifetime() >= 0 && getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800173 wire_.push_back
174 (nonNegativeIntegerBlock(Tlv::InterestLifetime, getInterestLifetime()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800175 }
176
177 wire_.encode();
178 return wire_;
179}
180
181void
182Interest::wireDecode(const Block &wire)
183{
184 wire_ = wire;
185 wire_.parse();
186
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800187 // Interest ::= INTEREST-TYPE TLV-LENGTH
188 // Name
189 // Selectors?
190 // Nonce
191 // Scope?
192 // InterestLifetime?
193
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800194 // Name
195 name_.wireDecode(wire_.get(Tlv::Name));
196
197 // Selectors
198 Block::element_iterator selectors = wire_.find(Tlv::Selectors);
199 if (selectors != wire_.getAll().end())
200 {
201 selectors->parse();
202
203 // MinSuffixComponents
204 Block::element_iterator val = selectors->find(Tlv::MinSuffixComponents);
205 if (val != selectors->getAll().end())
206 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800207 minSuffixComponents_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800208 }
209
210 // MaxSuffixComponents
211 val = selectors->find(Tlv::MaxSuffixComponents);
212 if (val != selectors->getAll().end())
213 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800214 maxSuffixComponents_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800215 }
216
217 // Exclude
218 val = selectors->find(Tlv::Exclude);
219 if (val != selectors->getAll().end())
220 {
221 exclude_.wireDecode(*val);
222 }
223
224 // ChildSelector
225 val = selectors->find(Tlv::ChildSelector);
226 if (val != selectors->getAll().end())
227 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800228 childSelector_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800229 }
230
231 //MustBeFresh aka AnswerOriginKind
232 val = selectors->find(Tlv::MustBeFresh);
233 if (val != selectors->getAll().end())
234 {
235 mustBeFresh_ = true;
236 }
237 }
238
239 // Nonce
240 Block::element_iterator val = wire_.find(Tlv::Nonce);
241 if (val != wire_.getAll().end())
242 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800243 nonce_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800244 }
245
246 // Scope
247 val = wire_.find(Tlv::Scope);
248 if (val != wire_.getAll().end())
249 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800250 scope_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800251 }
252
253 // InterestLifetime
254 val = wire_.find(Tlv::InterestLifetime);
255 if (val != wire_.getAll().end())
256 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800257 interestLifetime_ = readNonNegativeInteger(*val);
Alexander Afanasyev929e86d2014-01-17 16:11:25 -0800258 }
259 else
260 {
261 interestLifetime_ = DEFAULT_INTEREST_LIFETIME;
262 }
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800263}
264
265
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700266}