blob: 6c030dc5c5b48c1de2b0f2ca65f5fef224165e48 [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
Alexander Afanasyev09c613f2014-01-29 00:23:58 -08008#include "common.hpp"
9#include "interest.hpp"
Alexander Afanasyeve9fdb802014-02-05 17:36:51 -080010#include "util/random.hpp"
Alexander Afanasyev840139f2013-12-28 15:02:50 -080011
Jeff Thompsonb7f95562013-07-03 18:36:42 -070012using namespace std;
13
14namespace ndn {
Alexander Afanasyev84681982014-01-03 13:26:09 -080015
Alexander Afanasyev929e86d2014-01-17 16:11:25 -080016const Milliseconds DEFAULT_INTEREST_LIFETIME = 4000;
17
Alexander Afanasyev85480842014-01-06 14:46:54 -080018const uint32_t&
Alexander Afanasyev840139f2013-12-28 15:02:50 -080019Interest::getNonce() const
20{
Alexander Afanasyev840139f2013-12-28 15:02:50 -080021 if (nonce_ == 0)
Alexander Afanasyeve9fdb802014-02-05 17:36:51 -080022 nonce_ = random::generateWord32();
Alexander Afanasyev840139f2013-12-28 15:02:50 -080023
24 return nonce_;
25}
26
27
Alexander Afanasyev84681982014-01-03 13:26:09 -080028bool
29Interest::matchesName(const Name &name) const
Jeff Thompson25b4e612013-10-10 16:03:24 -070030{
Alexander Afanasyev84681982014-01-03 13:26:09 -080031 if (!name_.isPrefixOf(name))
32 return false;
33
34 if (minSuffixComponents_ >= 0 &&
35 // Add 1 for the implicit digest.
36 !(name.size() + 1 - name_.size() >= minSuffixComponents_))
37 return false;
38
39 if (maxSuffixComponents_ >= 0 &&
40 // Add 1 for the implicit digest.
41 !(name.size() + 1 - name_.size() <= maxSuffixComponents_))
42 return false;
43
44 if (!exclude_.empty() && name.size() > name_.size() &&
45 exclude_.isExcluded(name[name_.size()]))
46 return false;
47
48 return true;
Jeff Thompson25b4e612013-10-10 16:03:24 -070049}
50
Alexander Afanasyev84681982014-01-03 13:26:09 -080051std::ostream &
52operator << (std::ostream &os, const Interest &interest)
Jeff Thompsonfe556862013-07-09 13:52:55 -070053{
Alexander Afanasyev84681982014-01-03 13:26:09 -080054 os << interest.getName();
Jeff Thompsonfe556862013-07-09 13:52:55 -070055
Alexander Afanasyev84681982014-01-03 13:26:09 -080056 char delim = '?';
57
58 if (interest.getMinSuffixComponents() >= 0) {
59 os << delim << "ndn.MinSuffixComponents=" << interest.getMinSuffixComponents();
60 delim = '&';
Jeff Thompsonfe556862013-07-09 13:52:55 -070061 }
Alexander Afanasyev84681982014-01-03 13:26:09 -080062 if (interest.getMaxSuffixComponents() >= 0) {
63 os << delim << "ndn.MaxSuffixComponents=" << interest.getMaxSuffixComponents();
64 delim = '&';
Jeff Thompson37527d62013-08-21 11:15:54 -070065 }
Alexander Afanasyev84681982014-01-03 13:26:09 -080066 if (interest.getChildSelector() >= 0) {
67 os << delim << "ndn.ChildSelector=" << interest.getChildSelector();
68 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -080069 }
Alexander Afanasyev84681982014-01-03 13:26:09 -080070 if (interest.getMustBeFresh()) {
71 os << delim << "ndn.MustBeFresh=" << interest.getMustBeFresh();
72 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -080073 }
Alexander Afanasyev84681982014-01-03 13:26:09 -080074 if (interest.getScope() >= 0) {
75 os << delim << "ndn.Scope=" << interest.getScope();
76 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -080077 }
Alexander Afanasyev929e86d2014-01-17 16:11:25 -080078 if (interest.getInterestLifetime() >= 0 && interest.getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) {
Alexander Afanasyev84681982014-01-03 13:26:09 -080079 os << delim << "ndn.InterestLifetime=" << interest.getInterestLifetime();
80 delim = '&';
81 }
82
83 if (interest.getNonce() > 0) {
84 os << delim << "ndn.Nonce=" << interest.getNonce();
85 delim = '&';
86 }
87 if (!interest.getExclude().empty()) {
88 os << delim << "ndn.Exclude=" << interest.getExclude();
89 delim = '&';
90 }
91
92 return os;
Jeff Thompson13e280b2013-12-03 13:12:23 -080093}
94
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -080095const Block&
96Interest::wireEncode() const
97{
98 if (wire_.hasWire())
99 return wire_;
100
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800101 // Interest ::= INTEREST-TYPE TLV-LENGTH
102 // Name
103 // Selectors?
104 // Nonce
105 // Scope?
106 // InterestLifetime?
107
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800108 wire_ = Block(Tlv::Interest);
109 wire_.push_back(getName().wireEncode());
110
111 // selectors
112 {
113 Block selectors(Tlv::Selectors);
114
115 if (getMinSuffixComponents() >= 0) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800116 selectors.push_back
117 (nonNegativeIntegerBlock(Tlv::MinSuffixComponents, getMinSuffixComponents()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800118 }
119 if (getMaxSuffixComponents() >= 0) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800120 selectors.push_back
121 (nonNegativeIntegerBlock(Tlv::MaxSuffixComponents, getMaxSuffixComponents()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800122 }
123 if (!getExclude().empty()) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800124 selectors.push_back
125 (getExclude().wireEncode());
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800126 }
127 if (getChildSelector() >= 0) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800128 selectors.push_back
129 (nonNegativeIntegerBlock(Tlv::ChildSelector, getChildSelector()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800130 }
131 if (getMustBeFresh()) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800132 selectors.push_back
133 (booleanBlock(Tlv::MustBeFresh));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800134 }
135
Alexander Afanasyev840139f2013-12-28 15:02:50 -0800136 if (!selectors.getAll().empty())
137 {
138 selectors.encode();
139 wire_.push_back(selectors);
140 }
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800141 }
142
143 // Nonce
144 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800145 wire_.push_back
146 (nonNegativeIntegerBlock(Tlv::Nonce, getNonce()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800147 }
Alexander Afanasyev929e86d2014-01-17 16:11:25 -0800148
149 // Scope
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800150 if (getScope() >= 0) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800151 wire_.push_back
152 (nonNegativeIntegerBlock(Tlv::Scope, getScope()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800153 }
Alexander Afanasyev929e86d2014-01-17 16:11:25 -0800154
155 // InterestLifetime
156 if (getInterestLifetime() >= 0 && getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800157 wire_.push_back
158 (nonNegativeIntegerBlock(Tlv::InterestLifetime, getInterestLifetime()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800159 }
160
161 wire_.encode();
162 return wire_;
163}
164
165void
166Interest::wireDecode(const Block &wire)
167{
168 wire_ = wire;
169 wire_.parse();
170
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800171 // Interest ::= INTEREST-TYPE TLV-LENGTH
172 // Name
173 // Selectors?
174 // Nonce
175 // Scope?
176 // InterestLifetime?
177
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800178 // Name
179 name_.wireDecode(wire_.get(Tlv::Name));
180
181 // Selectors
182 Block::element_iterator selectors = wire_.find(Tlv::Selectors);
183 if (selectors != wire_.getAll().end())
184 {
185 selectors->parse();
186
187 // MinSuffixComponents
188 Block::element_iterator val = selectors->find(Tlv::MinSuffixComponents);
189 if (val != selectors->getAll().end())
190 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800191 minSuffixComponents_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800192 }
193
194 // MaxSuffixComponents
195 val = selectors->find(Tlv::MaxSuffixComponents);
196 if (val != selectors->getAll().end())
197 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800198 maxSuffixComponents_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800199 }
200
201 // Exclude
202 val = selectors->find(Tlv::Exclude);
203 if (val != selectors->getAll().end())
204 {
205 exclude_.wireDecode(*val);
206 }
207
208 // ChildSelector
209 val = selectors->find(Tlv::ChildSelector);
210 if (val != selectors->getAll().end())
211 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800212 childSelector_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800213 }
214
215 //MustBeFresh aka AnswerOriginKind
216 val = selectors->find(Tlv::MustBeFresh);
217 if (val != selectors->getAll().end())
218 {
219 mustBeFresh_ = true;
220 }
221 }
222
223 // Nonce
224 Block::element_iterator val = wire_.find(Tlv::Nonce);
225 if (val != wire_.getAll().end())
226 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800227 nonce_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800228 }
229
230 // Scope
231 val = wire_.find(Tlv::Scope);
232 if (val != wire_.getAll().end())
233 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800234 scope_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800235 }
236
237 // InterestLifetime
238 val = wire_.find(Tlv::InterestLifetime);
239 if (val != wire_.getAll().end())
240 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800241 interestLifetime_ = readNonNegativeInteger(*val);
Alexander Afanasyev929e86d2014-01-17 16:11:25 -0800242 }
243 else
244 {
245 interestLifetime_ = DEFAULT_INTEREST_LIFETIME;
246 }
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800247}
248
249
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700250}