blob: b9048ed8270053e2a124aefb849606f8b407e604 [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"
20#pragma GCC diagnostic ignored "-Wtautological-compare"
21#pragma GCC diagnostic ignored "-Wunused-variable"
22#pragma GCC diagnostic ignored "-Wunused-function"
23#endif
24
Alexander Afanasyev840139f2013-12-28 15:02:50 -080025#include <cryptopp/osrng.h>
26
Jeff Thompsonb7f95562013-07-03 18:36:42 -070027using namespace std;
28
29namespace ndn {
Alexander Afanasyev84681982014-01-03 13:26:09 -080030
Alexander Afanasyev85480842014-01-06 14:46:54 -080031const uint32_t&
Alexander Afanasyev840139f2013-12-28 15:02:50 -080032Interest::getNonce() const
33{
34 static CryptoPP::AutoSeededRandomPool rng;
35
36 if (nonce_ == 0)
37 nonce_ = rng.GenerateWord32();
38
39 return nonce_;
40}
41
42
Alexander Afanasyev84681982014-01-03 13:26:09 -080043bool
44Interest::matchesName(const Name &name) const
Jeff Thompson25b4e612013-10-10 16:03:24 -070045{
Alexander Afanasyev84681982014-01-03 13:26:09 -080046 if (!name_.isPrefixOf(name))
47 return false;
48
49 if (minSuffixComponents_ >= 0 &&
50 // Add 1 for the implicit digest.
51 !(name.size() + 1 - name_.size() >= minSuffixComponents_))
52 return false;
53
54 if (maxSuffixComponents_ >= 0 &&
55 // Add 1 for the implicit digest.
56 !(name.size() + 1 - name_.size() <= maxSuffixComponents_))
57 return false;
58
59 if (!exclude_.empty() && name.size() > name_.size() &&
60 exclude_.isExcluded(name[name_.size()]))
61 return false;
62
63 return true;
Jeff Thompson25b4e612013-10-10 16:03:24 -070064}
65
Alexander Afanasyev84681982014-01-03 13:26:09 -080066std::ostream &
67operator << (std::ostream &os, const Interest &interest)
Jeff Thompsonfe556862013-07-09 13:52:55 -070068{
Alexander Afanasyev84681982014-01-03 13:26:09 -080069 os << interest.getName();
Jeff Thompsonfe556862013-07-09 13:52:55 -070070
Alexander Afanasyev84681982014-01-03 13:26:09 -080071 char delim = '?';
72
73 if (interest.getMinSuffixComponents() >= 0) {
74 os << delim << "ndn.MinSuffixComponents=" << interest.getMinSuffixComponents();
75 delim = '&';
Jeff Thompsonfe556862013-07-09 13:52:55 -070076 }
Alexander Afanasyev84681982014-01-03 13:26:09 -080077 if (interest.getMaxSuffixComponents() >= 0) {
78 os << delim << "ndn.MaxSuffixComponents=" << interest.getMaxSuffixComponents();
79 delim = '&';
Jeff Thompson37527d62013-08-21 11:15:54 -070080 }
Alexander Afanasyev84681982014-01-03 13:26:09 -080081 if (interest.getChildSelector() >= 0) {
82 os << delim << "ndn.ChildSelector=" << interest.getChildSelector();
83 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -080084 }
Alexander Afanasyev84681982014-01-03 13:26:09 -080085 if (interest.getMustBeFresh()) {
86 os << delim << "ndn.MustBeFresh=" << interest.getMustBeFresh();
87 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -080088 }
Alexander Afanasyev84681982014-01-03 13:26:09 -080089 if (interest.getScope() >= 0) {
90 os << delim << "ndn.Scope=" << interest.getScope();
91 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -080092 }
Alexander Afanasyev84681982014-01-03 13:26:09 -080093 if (interest.getInterestLifetime() >= 0) {
94 os << delim << "ndn.InterestLifetime=" << interest.getInterestLifetime();
95 delim = '&';
96 }
97
98 if (interest.getNonce() > 0) {
99 os << delim << "ndn.Nonce=" << interest.getNonce();
100 delim = '&';
101 }
102 if (!interest.getExclude().empty()) {
103 os << delim << "ndn.Exclude=" << interest.getExclude();
104 delim = '&';
105 }
106
107 return os;
Jeff Thompson13e280b2013-12-03 13:12:23 -0800108}
109
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800110const Block&
111Interest::wireEncode() const
112{
113 if (wire_.hasWire())
114 return wire_;
115
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800116 // Interest ::= INTEREST-TYPE TLV-LENGTH
117 // Name
118 // Selectors?
119 // Nonce
120 // Scope?
121 // InterestLifetime?
122
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800123 wire_ = Block(Tlv::Interest);
124 wire_.push_back(getName().wireEncode());
125
126 // selectors
127 {
128 Block selectors(Tlv::Selectors);
129
130 if (getMinSuffixComponents() >= 0) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800131 selectors.push_back
132 (nonNegativeIntegerBlock(Tlv::MinSuffixComponents, getMinSuffixComponents()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800133 }
134 if (getMaxSuffixComponents() >= 0) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800135 selectors.push_back
136 (nonNegativeIntegerBlock(Tlv::MaxSuffixComponents, getMaxSuffixComponents()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800137 }
138 if (!getExclude().empty()) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800139 selectors.push_back
140 (getExclude().wireEncode());
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800141 }
142 if (getChildSelector() >= 0) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800143 selectors.push_back
144 (nonNegativeIntegerBlock(Tlv::ChildSelector, getChildSelector()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800145 }
146 if (getMustBeFresh()) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800147 selectors.push_back
148 (booleanBlock(Tlv::MustBeFresh));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800149 }
150
Alexander Afanasyev840139f2013-12-28 15:02:50 -0800151 if (!selectors.getAll().empty())
152 {
153 selectors.encode();
154 wire_.push_back(selectors);
155 }
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800156 }
157
158 // Nonce
159 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800160 wire_.push_back
161 (nonNegativeIntegerBlock(Tlv::Nonce, getNonce()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800162 }
163
164 if (getScope() >= 0) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800165 wire_.push_back
166 (nonNegativeIntegerBlock(Tlv::Scope, getScope()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800167 }
168 if (getInterestLifetime() >= 0) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800169 wire_.push_back
170 (nonNegativeIntegerBlock(Tlv::InterestLifetime, getInterestLifetime()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800171 }
172
173 wire_.encode();
174 return wire_;
175}
176
177void
178Interest::wireDecode(const Block &wire)
179{
180 wire_ = wire;
181 wire_.parse();
182
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800183 // Interest ::= INTEREST-TYPE TLV-LENGTH
184 // Name
185 // Selectors?
186 // Nonce
187 // Scope?
188 // InterestLifetime?
189
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800190 // Name
191 name_.wireDecode(wire_.get(Tlv::Name));
192
193 // Selectors
194 Block::element_iterator selectors = wire_.find(Tlv::Selectors);
195 if (selectors != wire_.getAll().end())
196 {
197 selectors->parse();
198
199 // MinSuffixComponents
200 Block::element_iterator val = selectors->find(Tlv::MinSuffixComponents);
201 if (val != selectors->getAll().end())
202 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800203 minSuffixComponents_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800204 }
205
206 // MaxSuffixComponents
207 val = selectors->find(Tlv::MaxSuffixComponents);
208 if (val != selectors->getAll().end())
209 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800210 maxSuffixComponents_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800211 }
212
213 // Exclude
214 val = selectors->find(Tlv::Exclude);
215 if (val != selectors->getAll().end())
216 {
217 exclude_.wireDecode(*val);
218 }
219
220 // ChildSelector
221 val = selectors->find(Tlv::ChildSelector);
222 if (val != selectors->getAll().end())
223 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800224 childSelector_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800225 }
226
227 //MustBeFresh aka AnswerOriginKind
228 val = selectors->find(Tlv::MustBeFresh);
229 if (val != selectors->getAll().end())
230 {
231 mustBeFresh_ = true;
232 }
233 }
234
235 // Nonce
236 Block::element_iterator val = wire_.find(Tlv::Nonce);
237 if (val != wire_.getAll().end())
238 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800239 nonce_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800240 }
241
242 // Scope
243 val = wire_.find(Tlv::Scope);
244 if (val != wire_.getAll().end())
245 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800246 scope_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800247 }
248
249 // InterestLifetime
250 val = wire_.find(Tlv::InterestLifetime);
251 if (val != wire_.getAll().end())
252 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800253 interestLifetime_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800254 }
255}
256
257
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700258}