blob: da3cab6a1d5c339f61b62b551ee6f0952f1a44f0 [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"
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -08009
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080010#include "interest.hpp"
Alexander Afanasyeve9fdb802014-02-05 17:36:51 -080011#include "util/random.hpp"
Alexander Afanasyev840139f2013-12-28 15:02:50 -080012
Jeff Thompsonb7f95562013-07-03 18:36:42 -070013using namespace std;
14
15namespace ndn {
Alexander Afanasyev84681982014-01-03 13:26:09 -080016
Alexander Afanasyev929e86d2014-01-17 16:11:25 -080017const Milliseconds DEFAULT_INTEREST_LIFETIME = 4000;
18
Alexander Afanasyev85480842014-01-06 14:46:54 -080019const uint32_t&
Alexander Afanasyev840139f2013-12-28 15:02:50 -080020Interest::getNonce() const
21{
Alexander Afanasyev840139f2013-12-28 15:02:50 -080022 if (nonce_ == 0)
Alexander Afanasyeve9fdb802014-02-05 17:36:51 -080023 nonce_ = random::generateWord32();
Alexander Afanasyev840139f2013-12-28 15:02:50 -080024
25 return nonce_;
26}
27
28
Alexander Afanasyev84681982014-01-03 13:26:09 -080029bool
30Interest::matchesName(const Name &name) const
Jeff Thompson25b4e612013-10-10 16:03:24 -070031{
Alexander Afanasyev84681982014-01-03 13:26:09 -080032 if (!name_.isPrefixOf(name))
33 return false;
34
35 if (minSuffixComponents_ >= 0 &&
36 // Add 1 for the implicit digest.
37 !(name.size() + 1 - name_.size() >= minSuffixComponents_))
38 return false;
39
40 if (maxSuffixComponents_ >= 0 &&
41 // Add 1 for the implicit digest.
42 !(name.size() + 1 - name_.size() <= maxSuffixComponents_))
43 return false;
44
45 if (!exclude_.empty() && name.size() > name_.size() &&
46 exclude_.isExcluded(name[name_.size()]))
47 return false;
48
49 return true;
Jeff Thompson25b4e612013-10-10 16:03:24 -070050}
51
Alexander Afanasyev84681982014-01-03 13:26:09 -080052std::ostream &
53operator << (std::ostream &os, const Interest &interest)
Jeff Thompsonfe556862013-07-09 13:52:55 -070054{
Alexander Afanasyev84681982014-01-03 13:26:09 -080055 os << interest.getName();
Jeff Thompsonfe556862013-07-09 13:52:55 -070056
Alexander Afanasyev84681982014-01-03 13:26:09 -080057 char delim = '?';
58
59 if (interest.getMinSuffixComponents() >= 0) {
60 os << delim << "ndn.MinSuffixComponents=" << interest.getMinSuffixComponents();
61 delim = '&';
Jeff Thompsonfe556862013-07-09 13:52:55 -070062 }
Alexander Afanasyev84681982014-01-03 13:26:09 -080063 if (interest.getMaxSuffixComponents() >= 0) {
64 os << delim << "ndn.MaxSuffixComponents=" << interest.getMaxSuffixComponents();
65 delim = '&';
Jeff Thompson37527d62013-08-21 11:15:54 -070066 }
Alexander Afanasyev84681982014-01-03 13:26:09 -080067 if (interest.getChildSelector() >= 0) {
68 os << delim << "ndn.ChildSelector=" << interest.getChildSelector();
69 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -080070 }
Alexander Afanasyev84681982014-01-03 13:26:09 -080071 if (interest.getMustBeFresh()) {
72 os << delim << "ndn.MustBeFresh=" << interest.getMustBeFresh();
73 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -080074 }
Alexander Afanasyev84681982014-01-03 13:26:09 -080075 if (interest.getScope() >= 0) {
76 os << delim << "ndn.Scope=" << interest.getScope();
77 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -080078 }
Alexander Afanasyev929e86d2014-01-17 16:11:25 -080079 if (interest.getInterestLifetime() >= 0 && interest.getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) {
Alexander Afanasyev84681982014-01-03 13:26:09 -080080 os << delim << "ndn.InterestLifetime=" << interest.getInterestLifetime();
81 delim = '&';
82 }
83
84 if (interest.getNonce() > 0) {
85 os << delim << "ndn.Nonce=" << interest.getNonce();
86 delim = '&';
87 }
88 if (!interest.getExclude().empty()) {
89 os << delim << "ndn.Exclude=" << interest.getExclude();
90 delim = '&';
91 }
92
93 return os;
Jeff Thompson13e280b2013-12-03 13:12:23 -080094}
95
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -080096const Block&
97Interest::wireEncode() const
98{
99 if (wire_.hasWire())
100 return wire_;
101
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800102 // Interest ::= INTEREST-TYPE TLV-LENGTH
103 // Name
104 // Selectors?
105 // Nonce
106 // Scope?
107 // InterestLifetime?
108
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800109 wire_ = Block(Tlv::Interest);
110 wire_.push_back(getName().wireEncode());
111
112 // selectors
113 {
114 Block selectors(Tlv::Selectors);
115
116 if (getMinSuffixComponents() >= 0) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800117 selectors.push_back
118 (nonNegativeIntegerBlock(Tlv::MinSuffixComponents, getMinSuffixComponents()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800119 }
120 if (getMaxSuffixComponents() >= 0) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800121 selectors.push_back
122 (nonNegativeIntegerBlock(Tlv::MaxSuffixComponents, getMaxSuffixComponents()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800123 }
124 if (!getExclude().empty()) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800125 selectors.push_back
126 (getExclude().wireEncode());
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800127 }
128 if (getChildSelector() >= 0) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800129 selectors.push_back
130 (nonNegativeIntegerBlock(Tlv::ChildSelector, getChildSelector()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800131 }
132 if (getMustBeFresh()) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800133 selectors.push_back
134 (booleanBlock(Tlv::MustBeFresh));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800135 }
136
Alexander Afanasyev840139f2013-12-28 15:02:50 -0800137 if (!selectors.getAll().empty())
138 {
139 selectors.encode();
140 wire_.push_back(selectors);
141 }
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800142 }
143
144 // Nonce
145 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800146 wire_.push_back
147 (nonNegativeIntegerBlock(Tlv::Nonce, getNonce()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800148 }
Alexander Afanasyev929e86d2014-01-17 16:11:25 -0800149
150 // Scope
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800151 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 }
Alexander Afanasyev929e86d2014-01-17 16:11:25 -0800155
156 // InterestLifetime
157 if (getInterestLifetime() >= 0 && getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800158 wire_.push_back
159 (nonNegativeIntegerBlock(Tlv::InterestLifetime, getInterestLifetime()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800160 }
161
162 wire_.encode();
163 return wire_;
164}
165
166void
167Interest::wireDecode(const Block &wire)
168{
169 wire_ = wire;
170 wire_.parse();
171
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800172 // Interest ::= INTEREST-TYPE TLV-LENGTH
173 // Name
174 // Selectors?
175 // Nonce
176 // Scope?
177 // InterestLifetime?
178
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800179 // Name
180 name_.wireDecode(wire_.get(Tlv::Name));
181
182 // Selectors
183 Block::element_iterator selectors = wire_.find(Tlv::Selectors);
184 if (selectors != wire_.getAll().end())
185 {
186 selectors->parse();
187
188 // MinSuffixComponents
189 Block::element_iterator val = selectors->find(Tlv::MinSuffixComponents);
190 if (val != selectors->getAll().end())
191 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800192 minSuffixComponents_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800193 }
194
195 // MaxSuffixComponents
196 val = selectors->find(Tlv::MaxSuffixComponents);
197 if (val != selectors->getAll().end())
198 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800199 maxSuffixComponents_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800200 }
201
202 // Exclude
203 val = selectors->find(Tlv::Exclude);
204 if (val != selectors->getAll().end())
205 {
206 exclude_.wireDecode(*val);
207 }
208
209 // ChildSelector
210 val = selectors->find(Tlv::ChildSelector);
211 if (val != selectors->getAll().end())
212 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800213 childSelector_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800214 }
215
216 //MustBeFresh aka AnswerOriginKind
217 val = selectors->find(Tlv::MustBeFresh);
218 if (val != selectors->getAll().end())
219 {
220 mustBeFresh_ = true;
221 }
222 }
223
224 // Nonce
225 Block::element_iterator val = wire_.find(Tlv::Nonce);
226 if (val != wire_.getAll().end())
227 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800228 nonce_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800229 }
230
231 // Scope
232 val = wire_.find(Tlv::Scope);
233 if (val != wire_.getAll().end())
234 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800235 scope_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800236 }
237
238 // InterestLifetime
239 val = wire_.find(Tlv::InterestLifetime);
240 if (val != wire_.getAll().end())
241 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800242 interestLifetime_ = readNonNegativeInteger(*val);
Alexander Afanasyev929e86d2014-01-17 16:11:25 -0800243 }
244 else
245 {
246 interestLifetime_ = DEFAULT_INTEREST_LIFETIME;
247 }
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800248}
249
250
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700251}