blob: 85ce0c1da77cd30e733b36ca086c47cbc8c61693 [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 Afanasyev840139f2013-12-28 15:02:50 -080012#include <cryptopp/osrng.h>
13
Jeff Thompsonb7f95562013-07-03 18:36:42 -070014using namespace std;
15
16namespace ndn {
Alexander Afanasyev84681982014-01-03 13:26:09 -080017
Alexander Afanasyev840139f2013-12-28 15:02:50 -080018uint32_t
19Interest::getNonce() const
20{
21 static CryptoPP::AutoSeededRandomPool rng;
22
23 if (nonce_ == 0)
24 nonce_ = rng.GenerateWord32();
25
26 return nonce_;
27}
28
29
Alexander Afanasyev84681982014-01-03 13:26:09 -080030bool
31Interest::matchesName(const Name &name) const
Jeff Thompson25b4e612013-10-10 16:03:24 -070032{
Alexander Afanasyev84681982014-01-03 13:26:09 -080033 if (!name_.isPrefixOf(name))
34 return false;
35
36 if (minSuffixComponents_ >= 0 &&
37 // Add 1 for the implicit digest.
38 !(name.size() + 1 - name_.size() >= minSuffixComponents_))
39 return false;
40
41 if (maxSuffixComponents_ >= 0 &&
42 // Add 1 for the implicit digest.
43 !(name.size() + 1 - name_.size() <= maxSuffixComponents_))
44 return false;
45
46 if (!exclude_.empty() && name.size() > name_.size() &&
47 exclude_.isExcluded(name[name_.size()]))
48 return false;
49
50 return true;
Jeff Thompson25b4e612013-10-10 16:03:24 -070051}
52
Alexander Afanasyev84681982014-01-03 13:26:09 -080053std::ostream &
54operator << (std::ostream &os, const Interest &interest)
Jeff Thompsonfe556862013-07-09 13:52:55 -070055{
Alexander Afanasyev84681982014-01-03 13:26:09 -080056 os << interest.getName();
Jeff Thompsonfe556862013-07-09 13:52:55 -070057
Alexander Afanasyev84681982014-01-03 13:26:09 -080058 char delim = '?';
59
60 if (interest.getMinSuffixComponents() >= 0) {
61 os << delim << "ndn.MinSuffixComponents=" << interest.getMinSuffixComponents();
62 delim = '&';
Jeff Thompsonfe556862013-07-09 13:52:55 -070063 }
Alexander Afanasyev84681982014-01-03 13:26:09 -080064 if (interest.getMaxSuffixComponents() >= 0) {
65 os << delim << "ndn.MaxSuffixComponents=" << interest.getMaxSuffixComponents();
66 delim = '&';
Jeff Thompson37527d62013-08-21 11:15:54 -070067 }
Alexander Afanasyev84681982014-01-03 13:26:09 -080068 if (interest.getChildSelector() >= 0) {
69 os << delim << "ndn.ChildSelector=" << interest.getChildSelector();
70 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -080071 }
Alexander Afanasyev84681982014-01-03 13:26:09 -080072 if (interest.getMustBeFresh()) {
73 os << delim << "ndn.MustBeFresh=" << interest.getMustBeFresh();
74 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -080075 }
Alexander Afanasyev84681982014-01-03 13:26:09 -080076 if (interest.getScope() >= 0) {
77 os << delim << "ndn.Scope=" << interest.getScope();
78 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -080079 }
Alexander Afanasyev84681982014-01-03 13:26:09 -080080 if (interest.getInterestLifetime() >= 0) {
81 os << delim << "ndn.InterestLifetime=" << interest.getInterestLifetime();
82 delim = '&';
83 }
84
85 if (interest.getNonce() > 0) {
86 os << delim << "ndn.Nonce=" << interest.getNonce();
87 delim = '&';
88 }
89 if (!interest.getExclude().empty()) {
90 os << delim << "ndn.Exclude=" << interest.getExclude();
91 delim = '&';
92 }
93
94 return os;
Jeff Thompson13e280b2013-12-03 13:12:23 -080095}
96
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -080097const Block&
98Interest::wireEncode() const
99{
100 if (wire_.hasWire())
101 return wire_;
102
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800103 // Interest ::= INTEREST-TYPE TLV-LENGTH
104 // Name
105 // Selectors?
106 // Nonce
107 // Scope?
108 // InterestLifetime?
109
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800110 wire_ = Block(Tlv::Interest);
111 wire_.push_back(getName().wireEncode());
112
113 // selectors
114 {
115 Block selectors(Tlv::Selectors);
116
117 if (getMinSuffixComponents() >= 0) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800118 selectors.push_back
119 (nonNegativeIntegerBlock(Tlv::MinSuffixComponents, getMinSuffixComponents()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800120 }
121 if (getMaxSuffixComponents() >= 0) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800122 selectors.push_back
123 (nonNegativeIntegerBlock(Tlv::MaxSuffixComponents, getMaxSuffixComponents()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800124 }
125 if (!getExclude().empty()) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800126 selectors.push_back
127 (getExclude().wireEncode());
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800128 }
129 if (getChildSelector() >= 0) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800130 selectors.push_back
131 (nonNegativeIntegerBlock(Tlv::ChildSelector, getChildSelector()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800132 }
133 if (getMustBeFresh()) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800134 selectors.push_back
135 (booleanBlock(Tlv::MustBeFresh));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800136 }
137
Alexander Afanasyev840139f2013-12-28 15:02:50 -0800138 if (!selectors.getAll().empty())
139 {
140 selectors.encode();
141 wire_.push_back(selectors);
142 }
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800143 }
144
145 // Nonce
146 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800147 wire_.push_back
148 (nonNegativeIntegerBlock(Tlv::Nonce, getNonce()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800149 }
150
151 if (getScope() >= 0) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800152 wire_.push_back
153 (nonNegativeIntegerBlock(Tlv::Scope, getScope()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800154 }
155 if (getInterestLifetime() >= 0) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800156 wire_.push_back
157 (nonNegativeIntegerBlock(Tlv::InterestLifetime, getInterestLifetime()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800158 }
159
160 wire_.encode();
161 return wire_;
162}
163
164void
165Interest::wireDecode(const Block &wire)
166{
167 wire_ = wire;
168 wire_.parse();
169
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800170 // Interest ::= INTEREST-TYPE TLV-LENGTH
171 // Name
172 // Selectors?
173 // Nonce
174 // Scope?
175 // InterestLifetime?
176
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800177 // Name
178 name_.wireDecode(wire_.get(Tlv::Name));
179
180 // Selectors
181 Block::element_iterator selectors = wire_.find(Tlv::Selectors);
182 if (selectors != wire_.getAll().end())
183 {
184 selectors->parse();
185
186 // MinSuffixComponents
187 Block::element_iterator val = selectors->find(Tlv::MinSuffixComponents);
188 if (val != selectors->getAll().end())
189 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800190 minSuffixComponents_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800191 }
192
193 // MaxSuffixComponents
194 val = selectors->find(Tlv::MaxSuffixComponents);
195 if (val != selectors->getAll().end())
196 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800197 maxSuffixComponents_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800198 }
199
200 // Exclude
201 val = selectors->find(Tlv::Exclude);
202 if (val != selectors->getAll().end())
203 {
204 exclude_.wireDecode(*val);
205 }
206
207 // ChildSelector
208 val = selectors->find(Tlv::ChildSelector);
209 if (val != selectors->getAll().end())
210 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800211 childSelector_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800212 }
213
214 //MustBeFresh aka AnswerOriginKind
215 val = selectors->find(Tlv::MustBeFresh);
216 if (val != selectors->getAll().end())
217 {
218 mustBeFresh_ = true;
219 }
220 }
221
222 // Nonce
223 Block::element_iterator val = wire_.find(Tlv::Nonce);
224 if (val != wire_.getAll().end())
225 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800226 nonce_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800227 }
228
229 // Scope
230 val = wire_.find(Tlv::Scope);
231 if (val != wire_.getAll().end())
232 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800233 scope_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800234 }
235
236 // InterestLifetime
237 val = wire_.find(Tlv::InterestLifetime);
238 if (val != wire_.getAll().end())
239 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800240 interestLifetime_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800241 }
242}
243
244
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700245}