blob: 5449a88fe5c97bc05c37fb90b343981bd5f3efb1 [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -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 Thompsonbf50a1a2013-08-20 18:01:01 -07005 * See COPYING for copyright and distribution information.
6 */
7
Jeff Thompsonea141d72013-09-19 14:40:10 -07008#include <stdexcept>
Jeff Thompson9ae4d782013-10-17 10:25:54 -07009#include "c/util/time.h"
Alexander Afanasyev96d914f2014-01-02 22:24:29 -080010
Jeff Thompson25b4e612013-10-10 16:03:24 -070011#include <ndn-cpp/forwarding-entry.hpp>
Jeff Thompson25b4e612013-10-10 16:03:24 -070012#include <ndn-cpp/node.hpp>
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070013
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080014#include "util/ndnd-id-fetcher.hpp"
Alexander Afanasyev79100492014-01-03 15:35:38 -080015#include "security/signature/signature-sha256-with-rsa.hpp"
Alexander Afanasyev96d914f2014-01-02 22:24:29 -080016
Alexander Afanasyev18371872014-01-05 23:00:26 -080017#include "status-response.hpp"
18
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070019using namespace std;
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070020
21namespace ndn {
22
Jeff Thompson62992e42013-10-07 18:50:51 -070023uint64_t Node::PendingInterest::lastPendingInterestId_ = 0;
24uint64_t Node::RegisteredPrefix::lastRegisteredPrefixId_ = 0;
Jeff Thompson11095142013-10-01 16:20:28 -070025
Alexander Afanasyev0b688dc2013-12-18 16:43:37 -080026Node::Node(const ptr_lib::shared_ptr<Transport>& transport)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080027 : timer_ (ioService_)
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080028 , processEventsTimeoutTimer_(ioService_)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080029 , transport_(transport)
30 , ndndIdFetcherInterest_(Name("/%C1.M.S.localhost/%C1.M.SRV/ndnd/KEY"), 4000.0)
Jeff Thompson557b81e2013-08-21 15:13:51 -070031{
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080032 timer_.expires_from_now(boost::posix_time::milliseconds(100));
33 timer_.async_wait(func_lib::bind(&Node::checkPitExpire, this));
Jeff Thompson557b81e2013-08-21 15:13:51 -070034}
35
Jeff Thompson62992e42013-10-07 18:50:51 -070036uint64_t
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080037Node::expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070038{
Jeff Thompson86507bc2013-08-23 20:51:38 -070039 // TODO: Properly check if we are already connected to the expected host.
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080040 if (!transport_->isConnected())
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080041 transport_->connect(ioService_,
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -080042 ptr_lib::bind(&Node::onReceiveElement, this, _1));
Jeff Thompson86507bc2013-08-23 20:51:38 -070043
Jeff Thompson62992e42013-10-07 18:50:51 -070044 uint64_t pendingInterestId = PendingInterest::getNextPendingInterestId();
Jeff Thompsonce115762013-12-18 14:59:56 -080045 pendingInterestTable_.push_back(ptr_lib::shared_ptr<PendingInterest>(new PendingInterest
46 (pendingInterestId, ptr_lib::shared_ptr<const Interest>(new Interest(interest)), onData, onTimeout)));
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080047
48 transport_->send(interest.wireEncode());
Jeff Thompson11095142013-10-01 16:20:28 -070049
50 return pendingInterestId;
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070051}
52
Jeff Thompson11095142013-10-01 16:20:28 -070053void
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080054Node::put(const Data &data)
55{
56 // TODO: Properly check if we are already connected to the expected host.
57 if (!transport_->isConnected())
58 transport_->connect(ioService_,
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -080059 ptr_lib::bind(&Node::onReceiveElement, this, _1));
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080060
61 transport_->send(data.wireEncode());
62}
63
64
65void
Jeff Thompson62992e42013-10-07 18:50:51 -070066Node::removePendingInterest(uint64_t pendingInterestId)
Jeff Thompson11095142013-10-01 16:20:28 -070067{
68 // Go backwards through the list so we can erase entries.
69 // Remove all entries even though pendingInterestId should be unique.
70 for (int i = (int)pendingInterestTable_.size() - 1; i >= 0; --i) {
71 if (pendingInterestTable_[i]->getPendingInterestId() == pendingInterestId)
72 pendingInterestTable_.erase(pendingInterestTable_.begin() + i);
73 }
74}
75
Jeff Thompson62992e42013-10-07 18:50:51 -070076uint64_t
Jeff Thompson590ec232013-09-18 15:55:56 -070077Node::registerPrefix
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080078 (const Name& prefix, const OnInterest& onInterest, const OnRegisterFailed& onRegisterFailed, const ForwardingFlags& flags)
Jeff Thompson86507bc2013-08-23 20:51:38 -070079{
Jeff Thompson11095142013-10-01 16:20:28 -070080 // Get the registeredPrefixId now so we can return it to the caller.
Jeff Thompson62992e42013-10-07 18:50:51 -070081 uint64_t registeredPrefixId = RegisteredPrefix::getNextRegisteredPrefixId();
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080082 ptr_lib::shared_ptr<const Name> prefixPtr = ptr_lib::make_shared<const Name>(prefix);
83
Jeff Thompson86507bc2013-08-23 20:51:38 -070084 if (ndndId_.size() == 0) {
85 // First fetch the ndndId of the connected hub.
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080086 NdndIdFetcher fetcher(ndndId_,
87 func_lib::bind(&Node::registerPrefixHelper, this,
88 registeredPrefixId, prefixPtr, onInterest, onRegisterFailed, flags),
89 func_lib::bind(onRegisterFailed, prefixPtr));
90
91 // @todo: Check if this crash
Jeff Thompsonce115762013-12-18 14:59:56 -080092 // It is OK for func_lib::function make a copy of the function object because the Info is in a ptr_lib::shared_ptr.
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080093 expressInterest(ndndIdFetcherInterest_, fetcher, fetcher);
Jeff Thompson86507bc2013-08-23 20:51:38 -070094 }
95 else
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080096 registerPrefixHelper(registeredPrefixId, prefixPtr, onInterest, onRegisterFailed, flags);
Jeff Thompson11095142013-10-01 16:20:28 -070097
98 return registeredPrefixId;
99}
100
101void
Jeff Thompson62992e42013-10-07 18:50:51 -0700102Node::removeRegisteredPrefix(uint64_t registeredPrefixId)
Jeff Thompson11095142013-10-01 16:20:28 -0700103{
104 // Go backwards through the list so we can erase entries.
105 // Remove all entries even though pendingInterestId should be unique.
106 for (int i = (int)registeredPrefixTable_.size() - 1; i >= 0; --i) {
107 if (registeredPrefixTable_[i]->getRegisteredPrefixId() == registeredPrefixId)
108 registeredPrefixTable_.erase(registeredPrefixTable_.begin() + i);
109 }
Jeff Thompson86507bc2013-08-23 20:51:38 -0700110}
111
Jeff Thompson0050abe2013-09-17 12:50:25 -0700112void
Alexander Afanasyev79100492014-01-03 15:35:38 -0800113Node::registerPrefixHelper(uint64_t registeredPrefixId,
114 const ptr_lib::shared_ptr<const Name>& prefix,
115 const OnInterest& onInterest,
116 const OnRegisterFailed& onRegisterFailed,
117 const ForwardingFlags& flags)
Jeff Thompson86507bc2013-08-23 20:51:38 -0700118{
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700119 // Create a ForwardingEntry.
Alexander Afanasyevfbdfa092013-12-28 20:44:49 -0800120
121 // AlexA: ndnd ignores any freshness that is larger than 3600 sec and sets 300 sec instead
122 // to register "forever" (=2000000000 sec), freshnessPeriod must be omitted
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800123 ForwardingEntry forwardingEntry("selfreg", *prefix, -1, flags, -1);
124 Block content = forwardingEntry.wireEncode();
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700125
126 // Set the ForwardingEntry as the content of a Data packet and sign.
127 Data data;
Jeff Thompsonc2b7b142013-09-12 15:29:04 -0700128 data.setContent(content);
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700129
Alexander Afanasyev79100492014-01-03 15:35:38 -0800130 // Create an empty signature, since nobody going to verify it for now
131 // @todo In the future, we may require real signatures to do the registration
132 SignatureSha256WithRsa signature;
133 signature.setValue(Block(Tlv::SignatureValue, ptr_lib::make_shared<Buffer>()));
134 data.setSignature(signature);
135
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700136 // Create an interest where the name has the encoded Data packet.
137 Name interestName;
Alexander Afanasyev18371872014-01-05 23:00:26 -0800138 interestName.append("ndnx");
Jeff Thompson3a715632013-10-31 11:36:35 -0700139 interestName.append(ndndId_);
Alexander Afanasyev18371872014-01-05 23:00:26 -0800140 interestName.append("selfreg");
Alexander Afanasyev79100492014-01-03 15:35:38 -0800141 interestName.append(data.wireEncode());
Alexander Afanasyev18371872014-01-05 23:00:26 -0800142
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700143 Interest interest(interestName);
144 interest.setScope(1);
Alexander Afanasyev18371872014-01-05 23:00:26 -0800145 interest.setInterestLifetime(1000);
146
147 expressInterest(interest,
148 func_lib::bind(&Node::registerPrefixFinal, this,
149 registeredPrefixId, prefix, onInterest, onRegisterFailed, _1, _2),
150 func_lib::bind(onRegisterFailed, prefix));
151}
152
153void
154Node::registerPrefixFinal(uint64_t registeredPrefixId,
155 const ptr_lib::shared_ptr<const Name>& prefix,
156 const OnInterest& onInterest,
157 const OnRegisterFailed& onRegisterFailed,
158 const ptr_lib::shared_ptr<const Interest>&, const ptr_lib::shared_ptr<Data>&data)
159{
160 Block content = data->getContent();
161 content.parse();
162
163 if (content.getAll().empty())
164 {
165 onRegisterFailed(prefix);
166 return;
167 }
168
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800169 Block::element_iterator val = content.getAll().begin();
170
171 switch(val->type())
Alexander Afanasyev18371872014-01-05 23:00:26 -0800172 {
173 case Tlv::FaceManagement::ForwardingEntry:
174 {
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800175 ForwardingEntry entry;
176 entry.wireDecode(*val);
177
178 // Save the onInterest callback and send the registration interest.
179 registeredPrefixTable_.push_back(ptr_lib::make_shared<RegisteredPrefix>(registeredPrefixId, prefix, onInterest));
180
181 /// @todo Notify user about successful registration
182
Alexander Afanasyev18371872014-01-05 23:00:26 -0800183 // succeeded
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800184 return;
Alexander Afanasyev18371872014-01-05 23:00:26 -0800185 }
186 case Tlv::FaceManagement::StatusResponse:
187 {
188 // failed :(
189 StatusResponse resp;
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800190 resp.wireDecode(*val);
Alexander Afanasyev18371872014-01-05 23:00:26 -0800191
192 std::cerr << "StatusReponse: " << resp << std::endl;
193
194 onRegisterFailed(prefix);
195 return;
Alexander Afanasyev18371872014-01-05 23:00:26 -0800196 }
197 default:
198 {
199 // failed :(
200
201 onRegisterFailed(prefix);
202 return;
Alexander Afanasyev18371872014-01-05 23:00:26 -0800203 }
204 }
Jeff Thompson86507bc2013-08-23 20:51:38 -0700205}
206
Jeff Thompson0050abe2013-09-17 12:50:25 -0700207void
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800208Node::processEvents(Milliseconds timeout/* = 0 */)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700209{
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800210 if (timeout > 0)
211 {
212 processEventsTimeoutTimer_.expires_from_now(boost::posix_time::milliseconds(timeout));
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800213 processEventsTimeoutTimer_.async_wait(fireProcessEventsTimeout);
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800214 }
215 try
216 {
217 ioService_.run();
218 ioService_.reset();
219 }
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800220 catch(Node::ProcessEventsTimeout &)
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800221 {
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800222 // break
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800223 }
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800224}
225
226void
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -0800227Node::fireProcessEventsTimeout(const boost::system::error_code& error)
228{
229 if (!error) // can fire for some other reason, e.g., cancelled
230 throw Node::ProcessEventsTimeout();
231}
232
233void
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800234Node::checkPitExpire()
235{
Jeff Thompson48917f02013-08-21 17:12:45 -0700236 // Check for PIT entry timeouts. Go backwards through the list so we can erase entries.
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700237 MillisecondsSince1970 nowMilliseconds = ndn_getNowMilliseconds();
Jeff Thompson11095142013-10-01 16:20:28 -0700238 for (int i = (int)pendingInterestTable_.size() - 1; i >= 0; --i) {
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800239 if (pendingInterestTable_[i]->isTimedOut(nowMilliseconds)) {
240 // Save the PendingInterest and remove it from the PIT. Then call the callback.
Jeff Thompsonce115762013-12-18 14:59:56 -0800241 ptr_lib::shared_ptr<PendingInterest> pendingInterest = pendingInterestTable_[i];
Jeff Thompson11095142013-10-01 16:20:28 -0700242 pendingInterestTable_.erase(pendingInterestTable_.begin() + i);
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800243 pendingInterest->callTimeout();
Jeff Thompson48917f02013-08-21 17:12:45 -0700244
245 // Refresh now since the timeout callback might have delayed.
Jeff Thompson9ae4d782013-10-17 10:25:54 -0700246 nowMilliseconds = ndn_getNowMilliseconds();
Jeff Thompson48917f02013-08-21 17:12:45 -0700247 }
248 }
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800249
250 timer_.expires_from_now(boost::posix_time::milliseconds(100));
251 timer_.async_wait(func_lib::bind(&Node::checkPitExpire, this));
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700252}
253
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800254
Jeff Thompson0050abe2013-09-17 12:50:25 -0700255void
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800256Node::onReceiveElement(const Block &block)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700257{
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800258 if (block.type() == Tlv::Interest)
Alexander Afanasyev96d914f2014-01-02 22:24:29 -0800259 {
260 ptr_lib::shared_ptr<Interest> interest(new Interest());
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800261 interest->wireDecode(block);
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700262
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800263 RegisteredPrefixTable::iterator entry = getEntryForRegisteredPrefix(interest->getName());
264 if (entry != registeredPrefixTable_.end()) {
265 (*entry)->getOnInterest()((*entry)->getPrefix(), interest, *transport_, (*entry)->getRegisteredPrefixId());
266 }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700267 }
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800268 else if (block.type() == Tlv::Data)
Alexander Afanasyev96d914f2014-01-02 22:24:29 -0800269 {
270 ptr_lib::shared_ptr<Data> data(new Data());
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800271 data->wireDecode(block);
272
273 PendingInterestTable::iterator entry = getEntryIndexForExpressedInterest(data->getName());
274 if (entry != pendingInterestTable_.end()) {
Alexander Afanasyev96d914f2014-01-02 22:24:29 -0800275 // Copy pointers to the needed objects and remove the PIT entry before the calling the callback.
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800276 const OnData onData = (*entry)->getOnData();
277 const ptr_lib::shared_ptr<const Interest> interest = (*entry)->getInterest();
278 pendingInterestTable_.erase(entry);
Alexander Afanasyev96d914f2014-01-02 22:24:29 -0800279 onData(interest, data);
280 }
281 }
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700282}
283
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -0800284void
285Node::onTransportError()
286{
287 /// @todo Set some error code
288
289 ioService_.stop();
290 throw Error("TransportError");
291}
292
Jeff Thompson0050abe2013-09-17 12:50:25 -0700293void
294Node::shutdown()
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700295{
296 transport_->close();
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800297 ioService_.stop();
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700298}
299
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800300Node::PendingInterestTable::iterator
Jeff Thompson0050abe2013-09-17 12:50:25 -0700301Node::getEntryIndexForExpressedInterest(const Name& name)
Jeff Thompson557b81e2013-08-21 15:13:51 -0700302{
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800303 for (PendingInterestTable::iterator i = pendingInterestTable_.begin ();
304 i != pendingInterestTable_.end(); ++i)
305 {
306 if ((*i)->getInterest()->matchesName(name))
307 {
308 return i;
309 }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700310 }
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800311
312 return pendingInterestTable_.end();
Jeff Thompson557b81e2013-08-21 15:13:51 -0700313}
Jeff Thompson86507bc2013-08-23 20:51:38 -0700314
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800315Node::RegisteredPrefixTable::iterator
Jeff Thompson0050abe2013-09-17 12:50:25 -0700316Node::getEntryForRegisteredPrefix(const Name& name)
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700317{
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800318 RegisteredPrefixTable::iterator longestPrefix = registeredPrefixTable_.end();
319
320 for (RegisteredPrefixTable::iterator i = registeredPrefixTable_.begin();
321 i != registeredPrefixTable_.end();
322 ++i)
323 {
324 if (longestPrefix == registeredPrefixTable_.end() ||
325 (*i)->getPrefix()->size() > (*longestPrefix)->getPrefix()->size())
326 {
327 longestPrefix = i;
328 }
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700329 }
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800330 return longestPrefix;
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700331}
332
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800333Node::PendingInterest::PendingInterest(uint64_t pendingInterestId,
334 const ptr_lib::shared_ptr<const Interest>& interest,
335 const OnData& onData, const OnTimeout& onTimeout)
336: pendingInterestId_(pendingInterestId),
337 interest_(interest),
338 onData_(onData), onTimeout_(onTimeout)
Jeff Thompson86507bc2013-08-23 20:51:38 -0700339{
340 // Set up timeoutTime_.
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800341 if (interest_->getInterestLifetime() >= 0)
342 timeoutTimeMilliseconds_ = ndn_getNowMilliseconds() + interest_->getInterestLifetime();
Jeff Thompson86507bc2013-08-23 20:51:38 -0700343 else
344 // No timeout.
Alexander Afanasyevb24a68a2013-12-28 16:53:21 -0800345 /**
346 * @todo Set more meaningful default timeout. This timeout MUST exist.
347 */
348 timeoutTimeMilliseconds_ = ndn_getNowMilliseconds() + 4000;
Jeff Thompson86507bc2013-08-23 20:51:38 -0700349}
350
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800351void
352Node::PendingInterest::callTimeout()
Jeff Thompson86507bc2013-08-23 20:51:38 -0700353{
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800354 if (onTimeout_) {
355 // Ignore all exceptions.
356 try {
357 onTimeout_(interest_);
Jeff Thompson86507bc2013-08-23 20:51:38 -0700358 }
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800359 catch (...) { }
Jeff Thompson86507bc2013-08-23 20:51:38 -0700360 }
Jeff Thompson86507bc2013-08-23 20:51:38 -0700361}
Jeff Thompson557b81e2013-08-21 15:13:51 -0700362
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700363}