blob: 3610344a5474e944dfedcd858c3b1ccba227e56f [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_)
28 , transport_(transport)
29 , ndndIdFetcherInterest_(Name("/%C1.M.S.localhost/%C1.M.SRV/ndnd/KEY"), 4000.0)
Jeff Thompson557b81e2013-08-21 15:13:51 -070030{
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080031 timer_.expires_from_now(boost::posix_time::milliseconds(100));
32 timer_.async_wait(func_lib::bind(&Node::checkPitExpire, this));
Jeff Thompson557b81e2013-08-21 15:13:51 -070033}
34
Jeff Thompson62992e42013-10-07 18:50:51 -070035uint64_t
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080036Node::expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070037{
Jeff Thompson86507bc2013-08-23 20:51:38 -070038 // TODO: Properly check if we are already connected to the expected host.
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080039 if (!transport_->isConnected())
40 transport_->connect(ioService_, ptr_lib::bind(&Node::onReceiveElement, this, _1));
Jeff Thompson86507bc2013-08-23 20:51:38 -070041
Jeff Thompson62992e42013-10-07 18:50:51 -070042 uint64_t pendingInterestId = PendingInterest::getNextPendingInterestId();
Jeff Thompsonce115762013-12-18 14:59:56 -080043 pendingInterestTable_.push_back(ptr_lib::shared_ptr<PendingInterest>(new PendingInterest
44 (pendingInterestId, ptr_lib::shared_ptr<const Interest>(new Interest(interest)), onData, onTimeout)));
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080045
46 transport_->send(interest.wireEncode());
Jeff Thompson11095142013-10-01 16:20:28 -070047
48 return pendingInterestId;
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070049}
50
Jeff Thompson11095142013-10-01 16:20:28 -070051void
Jeff Thompson62992e42013-10-07 18:50:51 -070052Node::removePendingInterest(uint64_t pendingInterestId)
Jeff Thompson11095142013-10-01 16:20:28 -070053{
54 // Go backwards through the list so we can erase entries.
55 // Remove all entries even though pendingInterestId should be unique.
56 for (int i = (int)pendingInterestTable_.size() - 1; i >= 0; --i) {
57 if (pendingInterestTable_[i]->getPendingInterestId() == pendingInterestId)
58 pendingInterestTable_.erase(pendingInterestTable_.begin() + i);
59 }
60}
61
Jeff Thompson62992e42013-10-07 18:50:51 -070062uint64_t
Jeff Thompson590ec232013-09-18 15:55:56 -070063Node::registerPrefix
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080064 (const Name& prefix, const OnInterest& onInterest, const OnRegisterFailed& onRegisterFailed, const ForwardingFlags& flags)
Jeff Thompson86507bc2013-08-23 20:51:38 -070065{
Jeff Thompson11095142013-10-01 16:20:28 -070066 // Get the registeredPrefixId now so we can return it to the caller.
Jeff Thompson62992e42013-10-07 18:50:51 -070067 uint64_t registeredPrefixId = RegisteredPrefix::getNextRegisteredPrefixId();
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080068 ptr_lib::shared_ptr<const Name> prefixPtr = ptr_lib::make_shared<const Name>(prefix);
69
Jeff Thompson86507bc2013-08-23 20:51:38 -070070 if (ndndId_.size() == 0) {
71 // First fetch the ndndId of the connected hub.
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080072 NdndIdFetcher fetcher(ndndId_,
73 func_lib::bind(&Node::registerPrefixHelper, this,
74 registeredPrefixId, prefixPtr, onInterest, onRegisterFailed, flags),
75 func_lib::bind(onRegisterFailed, prefixPtr));
76
77 // @todo: Check if this crash
Jeff Thompsonce115762013-12-18 14:59:56 -080078 // 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 -080079 expressInterest(ndndIdFetcherInterest_, fetcher, fetcher);
Jeff Thompson86507bc2013-08-23 20:51:38 -070080 }
81 else
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080082 registerPrefixHelper(registeredPrefixId, prefixPtr, onInterest, onRegisterFailed, flags);
Jeff Thompson11095142013-10-01 16:20:28 -070083
84 return registeredPrefixId;
85}
86
87void
Jeff Thompson62992e42013-10-07 18:50:51 -070088Node::removeRegisteredPrefix(uint64_t registeredPrefixId)
Jeff Thompson11095142013-10-01 16:20:28 -070089{
90 // Go backwards through the list so we can erase entries.
91 // Remove all entries even though pendingInterestId should be unique.
92 for (int i = (int)registeredPrefixTable_.size() - 1; i >= 0; --i) {
93 if (registeredPrefixTable_[i]->getRegisteredPrefixId() == registeredPrefixId)
94 registeredPrefixTable_.erase(registeredPrefixTable_.begin() + i);
95 }
Jeff Thompson86507bc2013-08-23 20:51:38 -070096}
97
Jeff Thompson0050abe2013-09-17 12:50:25 -070098void
Alexander Afanasyev79100492014-01-03 15:35:38 -080099Node::registerPrefixHelper(uint64_t registeredPrefixId,
100 const ptr_lib::shared_ptr<const Name>& prefix,
101 const OnInterest& onInterest,
102 const OnRegisterFailed& onRegisterFailed,
103 const ForwardingFlags& flags)
Jeff Thompson86507bc2013-08-23 20:51:38 -0700104{
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700105 // Create a ForwardingEntry.
Alexander Afanasyevfbdfa092013-12-28 20:44:49 -0800106
107 // AlexA: ndnd ignores any freshness that is larger than 3600 sec and sets 300 sec instead
108 // to register "forever" (=2000000000 sec), freshnessPeriod must be omitted
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800109 ForwardingEntry forwardingEntry("selfreg", *prefix, -1, flags, -1);
110 Block content = forwardingEntry.wireEncode();
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700111
112 // Set the ForwardingEntry as the content of a Data packet and sign.
113 Data data;
Jeff Thompsonc2b7b142013-09-12 15:29:04 -0700114 data.setContent(content);
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700115
Alexander Afanasyev79100492014-01-03 15:35:38 -0800116 // Create an empty signature, since nobody going to verify it for now
117 // @todo In the future, we may require real signatures to do the registration
118 SignatureSha256WithRsa signature;
119 signature.setValue(Block(Tlv::SignatureValue, ptr_lib::make_shared<Buffer>()));
120 data.setSignature(signature);
121
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700122 // Create an interest where the name has the encoded Data packet.
123 Name interestName;
Alexander Afanasyev18371872014-01-05 23:00:26 -0800124 interestName.append("ndnx");
Jeff Thompson3a715632013-10-31 11:36:35 -0700125 interestName.append(ndndId_);
Alexander Afanasyev18371872014-01-05 23:00:26 -0800126 interestName.append("selfreg");
Alexander Afanasyev79100492014-01-03 15:35:38 -0800127 interestName.append(data.wireEncode());
Alexander Afanasyev18371872014-01-05 23:00:26 -0800128
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700129 Interest interest(interestName);
130 interest.setScope(1);
Alexander Afanasyev18371872014-01-05 23:00:26 -0800131 interest.setInterestLifetime(1000);
132
133 expressInterest(interest,
134 func_lib::bind(&Node::registerPrefixFinal, this,
135 registeredPrefixId, prefix, onInterest, onRegisterFailed, _1, _2),
136 func_lib::bind(onRegisterFailed, prefix));
137}
138
139void
140Node::registerPrefixFinal(uint64_t registeredPrefixId,
141 const ptr_lib::shared_ptr<const Name>& prefix,
142 const OnInterest& onInterest,
143 const OnRegisterFailed& onRegisterFailed,
144 const ptr_lib::shared_ptr<const Interest>&, const ptr_lib::shared_ptr<Data>&data)
145{
146 Block content = data->getContent();
147 content.parse();
148
149 if (content.getAll().empty())
150 {
151 onRegisterFailed(prefix);
152 return;
153 }
154
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800155 Block::element_iterator val = content.getAll().begin();
156
157 switch(val->type())
Alexander Afanasyev18371872014-01-05 23:00:26 -0800158 {
159 case Tlv::FaceManagement::ForwardingEntry:
160 {
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800161 ForwardingEntry entry;
162 entry.wireDecode(*val);
163
164 // Save the onInterest callback and send the registration interest.
165 registeredPrefixTable_.push_back(ptr_lib::make_shared<RegisteredPrefix>(registeredPrefixId, prefix, onInterest));
166
167 /// @todo Notify user about successful registration
168
Alexander Afanasyev18371872014-01-05 23:00:26 -0800169 // succeeded
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800170 return;
Alexander Afanasyev18371872014-01-05 23:00:26 -0800171 }
172 case Tlv::FaceManagement::StatusResponse:
173 {
174 // failed :(
175 StatusResponse resp;
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800176 resp.wireDecode(*val);
Alexander Afanasyev18371872014-01-05 23:00:26 -0800177
178 std::cerr << "StatusReponse: " << resp << std::endl;
179
180 onRegisterFailed(prefix);
181 return;
Alexander Afanasyev18371872014-01-05 23:00:26 -0800182 }
183 default:
184 {
185 // failed :(
186
187 onRegisterFailed(prefix);
188 return;
Alexander Afanasyev18371872014-01-05 23:00:26 -0800189 }
190 }
Jeff Thompson86507bc2013-08-23 20:51:38 -0700191}
192
Jeff Thompson0050abe2013-09-17 12:50:25 -0700193void
194Node::processEvents()
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700195{
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800196 ioService_.run();
197
198 // auto_ptr<boost::asio::io_service::work> work(new boost::asio::io_service::work(ioService_));
199 // work.reset(); // Allow run() to exit.
200}
201
202void
203Node::checkPitExpire()
204{
Jeff Thompson48917f02013-08-21 17:12:45 -0700205 // Check for PIT entry timeouts. Go backwards through the list so we can erase entries.
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700206 MillisecondsSince1970 nowMilliseconds = ndn_getNowMilliseconds();
Jeff Thompson11095142013-10-01 16:20:28 -0700207 for (int i = (int)pendingInterestTable_.size() - 1; i >= 0; --i) {
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800208 if (pendingInterestTable_[i]->isTimedOut(nowMilliseconds)) {
209 // Save the PendingInterest and remove it from the PIT. Then call the callback.
Jeff Thompsonce115762013-12-18 14:59:56 -0800210 ptr_lib::shared_ptr<PendingInterest> pendingInterest = pendingInterestTable_[i];
Jeff Thompson11095142013-10-01 16:20:28 -0700211 pendingInterestTable_.erase(pendingInterestTable_.begin() + i);
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800212 pendingInterest->callTimeout();
Jeff Thompson48917f02013-08-21 17:12:45 -0700213
214 // Refresh now since the timeout callback might have delayed.
Jeff Thompson9ae4d782013-10-17 10:25:54 -0700215 nowMilliseconds = ndn_getNowMilliseconds();
Jeff Thompson48917f02013-08-21 17:12:45 -0700216 }
217 }
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800218
219 timer_.expires_from_now(boost::posix_time::milliseconds(100));
220 timer_.async_wait(func_lib::bind(&Node::checkPitExpire, this));
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700221}
222
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800223
Jeff Thompson0050abe2013-09-17 12:50:25 -0700224void
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800225Node::onReceiveElement(const Block &block)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700226{
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800227 if (block.type() == Tlv::Interest)
Alexander Afanasyev96d914f2014-01-02 22:24:29 -0800228 {
229 ptr_lib::shared_ptr<Interest> interest(new Interest());
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800230 interest->wireDecode(block);
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700231
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800232 RegisteredPrefixTable::iterator entry = getEntryForRegisteredPrefix(interest->getName());
233 if (entry != registeredPrefixTable_.end()) {
234 (*entry)->getOnInterest()((*entry)->getPrefix(), interest, *transport_, (*entry)->getRegisteredPrefixId());
235 }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700236 }
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800237 else if (block.type() == Tlv::Data)
Alexander Afanasyev96d914f2014-01-02 22:24:29 -0800238 {
239 ptr_lib::shared_ptr<Data> data(new Data());
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800240 data->wireDecode(block);
241
242 PendingInterestTable::iterator entry = getEntryIndexForExpressedInterest(data->getName());
243 if (entry != pendingInterestTable_.end()) {
Alexander Afanasyev96d914f2014-01-02 22:24:29 -0800244 // Copy pointers to the needed objects and remove the PIT entry before the calling the callback.
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800245 const OnData onData = (*entry)->getOnData();
246 const ptr_lib::shared_ptr<const Interest> interest = (*entry)->getInterest();
247 pendingInterestTable_.erase(entry);
Alexander Afanasyev96d914f2014-01-02 22:24:29 -0800248 onData(interest, data);
249 }
250 }
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700251}
252
Jeff Thompson0050abe2013-09-17 12:50:25 -0700253void
254Node::shutdown()
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700255{
256 transport_->close();
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800257 ioService_.stop();
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700258}
259
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800260Node::PendingInterestTable::iterator
Jeff Thompson0050abe2013-09-17 12:50:25 -0700261Node::getEntryIndexForExpressedInterest(const Name& name)
Jeff Thompson557b81e2013-08-21 15:13:51 -0700262{
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800263 for (PendingInterestTable::iterator i = pendingInterestTable_.begin ();
264 i != pendingInterestTable_.end(); ++i)
265 {
266 if ((*i)->getInterest()->matchesName(name))
267 {
268 return i;
269 }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700270 }
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800271
272 return pendingInterestTable_.end();
Jeff Thompson557b81e2013-08-21 15:13:51 -0700273}
Jeff Thompson86507bc2013-08-23 20:51:38 -0700274
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800275Node::RegisteredPrefixTable::iterator
Jeff Thompson0050abe2013-09-17 12:50:25 -0700276Node::getEntryForRegisteredPrefix(const Name& name)
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700277{
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800278 RegisteredPrefixTable::iterator longestPrefix = registeredPrefixTable_.end();
279
280 for (RegisteredPrefixTable::iterator i = registeredPrefixTable_.begin();
281 i != registeredPrefixTable_.end();
282 ++i)
283 {
284 if (longestPrefix == registeredPrefixTable_.end() ||
285 (*i)->getPrefix()->size() > (*longestPrefix)->getPrefix()->size())
286 {
287 longestPrefix = i;
288 }
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700289 }
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800290 return longestPrefix;
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700291}
292
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800293Node::PendingInterest::PendingInterest(uint64_t pendingInterestId,
294 const ptr_lib::shared_ptr<const Interest>& interest,
295 const OnData& onData, const OnTimeout& onTimeout)
296: pendingInterestId_(pendingInterestId),
297 interest_(interest),
298 onData_(onData), onTimeout_(onTimeout)
Jeff Thompson86507bc2013-08-23 20:51:38 -0700299{
300 // Set up timeoutTime_.
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800301 if (interest_->getInterestLifetime() >= 0)
302 timeoutTimeMilliseconds_ = ndn_getNowMilliseconds() + interest_->getInterestLifetime();
Jeff Thompson86507bc2013-08-23 20:51:38 -0700303 else
304 // No timeout.
Alexander Afanasyevb24a68a2013-12-28 16:53:21 -0800305 /**
306 * @todo Set more meaningful default timeout. This timeout MUST exist.
307 */
308 timeoutTimeMilliseconds_ = ndn_getNowMilliseconds() + 4000;
Jeff Thompson86507bc2013-08-23 20:51:38 -0700309}
310
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800311void
312Node::PendingInterest::callTimeout()
Jeff Thompson86507bc2013-08-23 20:51:38 -0700313{
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800314 if (onTimeout_) {
315 // Ignore all exceptions.
316 try {
317 onTimeout_(interest_);
Jeff Thompson86507bc2013-08-23 20:51:38 -0700318 }
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800319 catch (...) { }
Jeff Thompson86507bc2013-08-23 20:51:38 -0700320 }
Jeff Thompson86507bc2013-08-23 20:51:38 -0700321}
Jeff Thompson557b81e2013-08-21 15:13:51 -0700322
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700323}