blob: 8a06b55461d107877adbc79c58a360a8b2b27965 [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
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070017using namespace std;
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070018
19namespace ndn {
20
Jeff Thompson62992e42013-10-07 18:50:51 -070021uint64_t Node::PendingInterest::lastPendingInterestId_ = 0;
22uint64_t Node::RegisteredPrefix::lastRegisteredPrefixId_ = 0;
Jeff Thompson11095142013-10-01 16:20:28 -070023
Alexander Afanasyev0b688dc2013-12-18 16:43:37 -080024Node::Node(const ptr_lib::shared_ptr<Transport>& transport)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080025 : timer_ (ioService_)
26 , transport_(transport)
27 , ndndIdFetcherInterest_(Name("/%C1.M.S.localhost/%C1.M.SRV/ndnd/KEY"), 4000.0)
Jeff Thompson557b81e2013-08-21 15:13:51 -070028{
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080029 timer_.expires_from_now(boost::posix_time::milliseconds(100));
30 timer_.async_wait(func_lib::bind(&Node::checkPitExpire, this));
Jeff Thompson557b81e2013-08-21 15:13:51 -070031}
32
Jeff Thompson62992e42013-10-07 18:50:51 -070033uint64_t
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080034Node::expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070035{
Jeff Thompson86507bc2013-08-23 20:51:38 -070036 // TODO: Properly check if we are already connected to the expected host.
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080037 if (!transport_->isConnected())
38 transport_->connect(ioService_, ptr_lib::bind(&Node::onReceiveElement, this, _1));
Jeff Thompson86507bc2013-08-23 20:51:38 -070039
Jeff Thompson62992e42013-10-07 18:50:51 -070040 uint64_t pendingInterestId = PendingInterest::getNextPendingInterestId();
Jeff Thompsonce115762013-12-18 14:59:56 -080041 pendingInterestTable_.push_back(ptr_lib::shared_ptr<PendingInterest>(new PendingInterest
42 (pendingInterestId, ptr_lib::shared_ptr<const Interest>(new Interest(interest)), onData, onTimeout)));
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080043
44 transport_->send(interest.wireEncode());
Jeff Thompson11095142013-10-01 16:20:28 -070045
46 return pendingInterestId;
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070047}
48
Jeff Thompson11095142013-10-01 16:20:28 -070049void
Jeff Thompson62992e42013-10-07 18:50:51 -070050Node::removePendingInterest(uint64_t pendingInterestId)
Jeff Thompson11095142013-10-01 16:20:28 -070051{
52 // Go backwards through the list so we can erase entries.
53 // Remove all entries even though pendingInterestId should be unique.
54 for (int i = (int)pendingInterestTable_.size() - 1; i >= 0; --i) {
55 if (pendingInterestTable_[i]->getPendingInterestId() == pendingInterestId)
56 pendingInterestTable_.erase(pendingInterestTable_.begin() + i);
57 }
58}
59
Jeff Thompson62992e42013-10-07 18:50:51 -070060uint64_t
Jeff Thompson590ec232013-09-18 15:55:56 -070061Node::registerPrefix
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080062 (const Name& prefix, const OnInterest& onInterest, const OnRegisterFailed& onRegisterFailed, const ForwardingFlags& flags)
Jeff Thompson86507bc2013-08-23 20:51:38 -070063{
Jeff Thompson11095142013-10-01 16:20:28 -070064 // Get the registeredPrefixId now so we can return it to the caller.
Jeff Thompson62992e42013-10-07 18:50:51 -070065 uint64_t registeredPrefixId = RegisteredPrefix::getNextRegisteredPrefixId();
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080066 ptr_lib::shared_ptr<const Name> prefixPtr = ptr_lib::make_shared<const Name>(prefix);
67
Jeff Thompson86507bc2013-08-23 20:51:38 -070068 if (ndndId_.size() == 0) {
69 // First fetch the ndndId of the connected hub.
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080070 NdndIdFetcher fetcher(ndndId_,
71 func_lib::bind(&Node::registerPrefixHelper, this,
72 registeredPrefixId, prefixPtr, onInterest, onRegisterFailed, flags),
73 func_lib::bind(onRegisterFailed, prefixPtr));
74
75 // @todo: Check if this crash
Jeff Thompsonce115762013-12-18 14:59:56 -080076 // 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 -080077 expressInterest(ndndIdFetcherInterest_, fetcher, fetcher);
Jeff Thompson86507bc2013-08-23 20:51:38 -070078 }
79 else
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080080 registerPrefixHelper(registeredPrefixId, prefixPtr, onInterest, onRegisterFailed, flags);
Jeff Thompson11095142013-10-01 16:20:28 -070081
82 return registeredPrefixId;
83}
84
85void
Jeff Thompson62992e42013-10-07 18:50:51 -070086Node::removeRegisteredPrefix(uint64_t registeredPrefixId)
Jeff Thompson11095142013-10-01 16:20:28 -070087{
88 // Go backwards through the list so we can erase entries.
89 // Remove all entries even though pendingInterestId should be unique.
90 for (int i = (int)registeredPrefixTable_.size() - 1; i >= 0; --i) {
91 if (registeredPrefixTable_[i]->getRegisteredPrefixId() == registeredPrefixId)
92 registeredPrefixTable_.erase(registeredPrefixTable_.begin() + i);
93 }
Jeff Thompson86507bc2013-08-23 20:51:38 -070094}
95
Jeff Thompson0050abe2013-09-17 12:50:25 -070096void
Alexander Afanasyev79100492014-01-03 15:35:38 -080097Node::registerPrefixHelper(uint64_t registeredPrefixId,
98 const ptr_lib::shared_ptr<const Name>& prefix,
99 const OnInterest& onInterest,
100 const OnRegisterFailed& onRegisterFailed,
101 const ForwardingFlags& flags)
Jeff Thompson86507bc2013-08-23 20:51:38 -0700102{
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700103 // Create a ForwardingEntry.
Alexander Afanasyevfbdfa092013-12-28 20:44:49 -0800104
105 // AlexA: ndnd ignores any freshness that is larger than 3600 sec and sets 300 sec instead
106 // to register "forever" (=2000000000 sec), freshnessPeriod must be omitted
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800107 ForwardingEntry forwardingEntry("selfreg", *prefix, -1, flags, -1);
108 Block content = forwardingEntry.wireEncode();
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700109
110 // Set the ForwardingEntry as the content of a Data packet and sign.
111 Data data;
Jeff Thompsonc2b7b142013-09-12 15:29:04 -0700112 data.setContent(content);
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700113
Alexander Afanasyev79100492014-01-03 15:35:38 -0800114 // Create an empty signature, since nobody going to verify it for now
115 // @todo In the future, we may require real signatures to do the registration
116 SignatureSha256WithRsa signature;
117 signature.setValue(Block(Tlv::SignatureValue, ptr_lib::make_shared<Buffer>()));
118 data.setSignature(signature);
119
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700120 // Create an interest where the name has the encoded Data packet.
121 Name interestName;
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700122 const uint8_t component0[] = "ndnx";
123 const uint8_t component2[] = "selfreg";
Jeff Thompson3a715632013-10-31 11:36:35 -0700124 interestName.append(component0, sizeof(component0) - 1);
125 interestName.append(ndndId_);
126 interestName.append(component2, sizeof(component2) - 1);
Alexander Afanasyev79100492014-01-03 15:35:38 -0800127 interestName.append(data.wireEncode());
128
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700129 Interest interest(interestName);
130 interest.setScope(1);
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700131
132 // Save the onInterest callback and send the registration interest.
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800133 registeredPrefixTable_.push_back(ptr_lib::make_shared<RegisteredPrefix>(registeredPrefixId, prefix, onInterest));
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700134
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800135 transport_->send(interest.wireEncode());
Jeff Thompson86507bc2013-08-23 20:51:38 -0700136}
137
Jeff Thompson0050abe2013-09-17 12:50:25 -0700138void
139Node::processEvents()
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700140{
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800141 ioService_.run();
142
143 // auto_ptr<boost::asio::io_service::work> work(new boost::asio::io_service::work(ioService_));
144 // work.reset(); // Allow run() to exit.
145}
146
147void
148Node::checkPitExpire()
149{
Jeff Thompson48917f02013-08-21 17:12:45 -0700150 // Check for PIT entry timeouts. Go backwards through the list so we can erase entries.
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700151 MillisecondsSince1970 nowMilliseconds = ndn_getNowMilliseconds();
Jeff Thompson11095142013-10-01 16:20:28 -0700152 for (int i = (int)pendingInterestTable_.size() - 1; i >= 0; --i) {
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800153 if (pendingInterestTable_[i]->isTimedOut(nowMilliseconds)) {
154 // Save the PendingInterest and remove it from the PIT. Then call the callback.
Jeff Thompsonce115762013-12-18 14:59:56 -0800155 ptr_lib::shared_ptr<PendingInterest> pendingInterest = pendingInterestTable_[i];
Jeff Thompson11095142013-10-01 16:20:28 -0700156 pendingInterestTable_.erase(pendingInterestTable_.begin() + i);
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800157 pendingInterest->callTimeout();
Jeff Thompson48917f02013-08-21 17:12:45 -0700158
159 // Refresh now since the timeout callback might have delayed.
Jeff Thompson9ae4d782013-10-17 10:25:54 -0700160 nowMilliseconds = ndn_getNowMilliseconds();
Jeff Thompson48917f02013-08-21 17:12:45 -0700161 }
162 }
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800163
164 timer_.expires_from_now(boost::posix_time::milliseconds(100));
165 timer_.async_wait(func_lib::bind(&Node::checkPitExpire, this));
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700166}
167
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800168
Jeff Thompson0050abe2013-09-17 12:50:25 -0700169void
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800170Node::onReceiveElement(const Block &block)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700171{
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800172 if (block.type() == Tlv::Interest)
Alexander Afanasyev96d914f2014-01-02 22:24:29 -0800173 {
174 ptr_lib::shared_ptr<Interest> interest(new Interest());
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800175 interest->wireDecode(block);
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700176
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800177 RegisteredPrefixTable::iterator entry = getEntryForRegisteredPrefix(interest->getName());
178 if (entry != registeredPrefixTable_.end()) {
179 (*entry)->getOnInterest()((*entry)->getPrefix(), interest, *transport_, (*entry)->getRegisteredPrefixId());
180 }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700181 }
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800182 else if (block.type() == Tlv::Data)
Alexander Afanasyev96d914f2014-01-02 22:24:29 -0800183 {
184 ptr_lib::shared_ptr<Data> data(new Data());
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800185 data->wireDecode(block);
186
187 PendingInterestTable::iterator entry = getEntryIndexForExpressedInterest(data->getName());
188 if (entry != pendingInterestTable_.end()) {
Alexander Afanasyev96d914f2014-01-02 22:24:29 -0800189 // Copy pointers to the needed objects and remove the PIT entry before the calling the callback.
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800190 const OnData onData = (*entry)->getOnData();
191 const ptr_lib::shared_ptr<const Interest> interest = (*entry)->getInterest();
192 pendingInterestTable_.erase(entry);
Alexander Afanasyev96d914f2014-01-02 22:24:29 -0800193 onData(interest, data);
194 }
195 }
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700196}
197
Jeff Thompson0050abe2013-09-17 12:50:25 -0700198void
199Node::shutdown()
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700200{
201 transport_->close();
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800202 ioService_.stop();
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700203}
204
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800205Node::PendingInterestTable::iterator
Jeff Thompson0050abe2013-09-17 12:50:25 -0700206Node::getEntryIndexForExpressedInterest(const Name& name)
Jeff Thompson557b81e2013-08-21 15:13:51 -0700207{
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800208 for (PendingInterestTable::iterator i = pendingInterestTable_.begin ();
209 i != pendingInterestTable_.end(); ++i)
210 {
211 if ((*i)->getInterest()->matchesName(name))
212 {
213 return i;
214 }
Jeff Thompson557b81e2013-08-21 15:13:51 -0700215 }
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800216
217 return pendingInterestTable_.end();
Jeff Thompson557b81e2013-08-21 15:13:51 -0700218}
Jeff Thompson86507bc2013-08-23 20:51:38 -0700219
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800220Node::RegisteredPrefixTable::iterator
Jeff Thompson0050abe2013-09-17 12:50:25 -0700221Node::getEntryForRegisteredPrefix(const Name& name)
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700222{
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800223 RegisteredPrefixTable::iterator longestPrefix = registeredPrefixTable_.end();
224
225 for (RegisteredPrefixTable::iterator i = registeredPrefixTable_.begin();
226 i != registeredPrefixTable_.end();
227 ++i)
228 {
229 if (longestPrefix == registeredPrefixTable_.end() ||
230 (*i)->getPrefix()->size() > (*longestPrefix)->getPrefix()->size())
231 {
232 longestPrefix = i;
233 }
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700234 }
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800235 return longestPrefix;
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700236}
237
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800238Node::PendingInterest::PendingInterest(uint64_t pendingInterestId,
239 const ptr_lib::shared_ptr<const Interest>& interest,
240 const OnData& onData, const OnTimeout& onTimeout)
241: pendingInterestId_(pendingInterestId),
242 interest_(interest),
243 onData_(onData), onTimeout_(onTimeout)
Jeff Thompson86507bc2013-08-23 20:51:38 -0700244{
245 // Set up timeoutTime_.
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -0800246 if (interest_->getInterestLifetime() >= 0)
247 timeoutTimeMilliseconds_ = ndn_getNowMilliseconds() + interest_->getInterestLifetime();
Jeff Thompson86507bc2013-08-23 20:51:38 -0700248 else
249 // No timeout.
Alexander Afanasyevee940312013-12-24 19:43:15 -0800250 timeoutTimeMilliseconds_ = -1;
Jeff Thompson86507bc2013-08-23 20:51:38 -0700251}
252
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800253void
254Node::PendingInterest::callTimeout()
Jeff Thompson86507bc2013-08-23 20:51:38 -0700255{
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800256 if (onTimeout_) {
257 // Ignore all exceptions.
258 try {
259 onTimeout_(interest_);
Jeff Thompson86507bc2013-08-23 20:51:38 -0700260 }
Jeff Thompson3b0ed532013-11-05 13:43:40 -0800261 catch (...) { }
Jeff Thompson86507bc2013-08-23 20:51:38 -0700262 }
Jeff Thompson86507bc2013-08-23 20:51:38 -0700263}
Jeff Thompson557b81e2013-08-21 15:13:51 -0700264
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700265}