blob: 2cf881f9d7e5b1f68331c3531d6a709884046efb [file] [log] [blame]
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -07001/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07002 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -07004 * See COPYING for copyright and distribution information.
5 */
6
Jeff Thompson48917f02013-08-21 17:12:45 -07007#include <sys/time.h>
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -07008#include "encoding/binary-xml-decoder.hpp"
9#include "c/encoding/binary-xml.h"
Jeff Thompson9cc4be42013-08-27 18:12:41 -070010#include "forwarding-entry.hpp"
11#include "security/key-chain.hpp"
Jeff Thompson20af0732013-09-12 17:01:45 -070012#include "sha256-with-rsa-signature.hpp"
Jeff Thompsonb09fcc12013-08-22 10:37:10 -070013#include "node.hpp"
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070014
15using namespace std;
16using namespace ndn::ptr_lib;
17
18namespace ndn {
19
Jeff Thompson48917f02013-08-21 17:12:45 -070020// Use gettimeofday to return the current time in milliseconds.
Jeff Thompson0050abe2013-09-17 12:50:25 -070021static inline double
22getNowMilliseconds()
Jeff Thompson557b81e2013-08-21 15:13:51 -070023{
Jeff Thompson48917f02013-08-21 17:12:45 -070024 timeval t;
25 gettimeofday(&t, NULL);
26 return t.tv_sec * 1000.0 + t.tv_usec / 1000.0;
27}
Jeff Thompson557b81e2013-08-21 15:13:51 -070028
Jeff Thompson1656e6a2013-08-29 18:01:48 -070029Node::Node(const ptr_lib::shared_ptr<Transport>& transport, const ptr_lib::shared_ptr<const Transport::ConnectionInfo>& connectionInfo)
Jeff Thompsonfb29cda2013-08-24 10:26:54 -070030: transport_(transport), connectionInfo_(connectionInfo),
31 ndndIdFetcherInterest_(Name("/%C1.M.S.localhost/%C1.M.SRV/ndnd/KEY"), 4000.0)
Jeff Thompson557b81e2013-08-21 15:13:51 -070032{
Jeff Thompson557b81e2013-08-21 15:13:51 -070033}
34
Jeff Thompson0050abe2013-09-17 12:50:25 -070035void
36Node::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.
39 if (!transport_->getIsConnected())
40 transport_->connect(*connectionInfo_, *this);
41
Jeff Thompson9cc4be42013-08-27 18:12:41 -070042 pit_.push_back(shared_ptr<PitEntry>(new PitEntry(shared_ptr<const Interest>(new Interest(interest)), onData, onTimeout)));
Jeff Thompson557b81e2013-08-21 15:13:51 -070043
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070044 Blob encoding = interest.wireEncode();
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070045 transport_->send(*encoding);
46}
47
Jeff Thompson0050abe2013-09-17 12:50:25 -070048void
49Node::registerPrefix(const Name& prefix, const OnInterest& onInterest, int flags)
Jeff Thompson86507bc2013-08-23 20:51:38 -070050{
51 if (ndndId_.size() == 0) {
52 // First fetch the ndndId of the connected hub.
53 NdndIdFetcher fetcher(make_shared<NdndIdFetcher::Info>(this, prefix, onInterest, flags));
54 // It is OK for func_lib::function make a copy of the function object because the Info is in a shared_ptr.
55 expressInterest(ndndIdFetcherInterest_, fetcher, fetcher);
56 }
57 else
58 registerPrefixHelper(prefix, onInterest, flags);
59}
60
Jeff Thompson0050abe2013-09-17 12:50:25 -070061void
62Node::NdndIdFetcher::operator()(const ptr_lib::shared_ptr<const Interest>& interest, const ptr_lib::shared_ptr<Data>& ndndIdData)
Jeff Thompson86507bc2013-08-23 20:51:38 -070063{
Jeff Thompson20af0732013-09-12 17:01:45 -070064 Sha256WithRsaSignature *signature = dynamic_cast<Sha256WithRsaSignature*>(ndndIdData->getSignature());
65 if (signature && signature->getPublisherPublicKeyDigest().getPublisherPublicKeyDigest().size() > 0) {
Jeff Thompson86507bc2013-08-23 20:51:38 -070066 // Set the ndndId_ and continue.
Jeff Thompson7a67cb62013-08-26 11:43:18 -070067 // TODO: If there are multiple connected hubs, the NDN ID is really stored per connected hub.
Jeff Thompson20af0732013-09-12 17:01:45 -070068 info_->node_.ndndId_ = signature->getPublisherPublicKeyDigest().getPublisherPublicKeyDigest();
Jeff Thompson86507bc2013-08-23 20:51:38 -070069 info_->node_.registerPrefixHelper(info_->prefix_, info_->onInterest_, info_->flags_);
70 }
71 // TODO: else need to log not getting the ndndId.
72}
73
Jeff Thompson0050abe2013-09-17 12:50:25 -070074void
75Node::NdndIdFetcher::operator()(const ptr_lib::shared_ptr<const Interest>& timedOutInterest)
Jeff Thompson86507bc2013-08-23 20:51:38 -070076{
77 // TODO: Log the timeout.
78}
79
Jeff Thompson0050abe2013-09-17 12:50:25 -070080void
81Node::registerPrefixHelper(const Name& prefix, const OnInterest& onInterest, int flags)
Jeff Thompson86507bc2013-08-23 20:51:38 -070082{
Jeff Thompson9cc4be42013-08-27 18:12:41 -070083 // Create a ForwardingEntry.
84 ForwardingEntry forwardingEntry("selfreg", prefix, PublisherPublicKeyDigest(), -1, 3, 2147483647);
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070085 Blob content = forwardingEntry.wireEncode();
Jeff Thompson9cc4be42013-08-27 18:12:41 -070086
87 // Set the ForwardingEntry as the content of a Data packet and sign.
88 Data data;
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070089 data.setContent(content);
Jeff Thompsonfec716d2013-09-11 13:54:36 -070090 data.getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0);
Jeff Thompson9cc4be42013-08-27 18:12:41 -070091 // TODO: Should we sign with a different key?
92 KeyChain::defaultSign(data);
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070093 Blob encodedData = data.wireEncode();
Jeff Thompson9cc4be42013-08-27 18:12:41 -070094
95 // Create an interest where the name has the encoded Data packet.
96 Name interestName;
97 const unsigned char component0[] = "ndnx";
98 const unsigned char component2[] = "selfreg";
99 interestName.addComponent(component0, sizeof(component0) - 1);
100 interestName.addComponent(ndndId_);
101 interestName.addComponent(component2, sizeof(component2) - 1);
Jeff Thompsonc2b7b142013-09-12 15:29:04 -0700102 interestName.addComponent(encodedData);
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700103
104 Interest interest(interestName);
105 interest.setScope(1);
Jeff Thompsonc2b7b142013-09-12 15:29:04 -0700106 Blob encodedInterest = interest.wireEncode();
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700107
108 // Save the onInterest callback and send the registration interest.
109 registeredPrefixTable_.push_back(shared_ptr<PrefixEntry>(new PrefixEntry(shared_ptr<const Name>(new Name(prefix)), onInterest)));
110
111 transport_->send(*encodedInterest);
Jeff Thompson86507bc2013-08-23 20:51:38 -0700112}
113
Jeff Thompson0050abe2013-09-17 12:50:25 -0700114void
115Node::processEvents()
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700116{
117 transport_->processEvents();
Jeff Thompson48917f02013-08-21 17:12:45 -0700118
119 // Check for PIT entry timeouts. Go backwards through the list so we can erase entries.
120 double nowMilliseconds = getNowMilliseconds();
121 for (int i = (int)pit_.size() - 1; i >= 0; --i) {
122 if (pit_[i]->checkTimeout(this, nowMilliseconds)) {
123 pit_.erase(pit_.begin() + i);
124
125 // Refresh now since the timeout callback might have delayed.
126 nowMilliseconds = getNowMilliseconds();
127 }
128 }
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700129}
130
Jeff Thompson0050abe2013-09-17 12:50:25 -0700131void
132Node::onReceivedElement(const unsigned char *element, unsigned int elementLength)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700133{
134 BinaryXmlDecoder decoder(element, elementLength);
135
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700136 if (decoder.peekDTag(ndn_BinaryXml_DTag_Interest)) {
137 shared_ptr<Interest> interest(new Interest());
138 interest->wireDecode(element, elementLength);
139
140 PrefixEntry *entry = getEntryForRegisteredPrefix(interest->getName());
141 if (entry)
142 entry->getOnInterest()(entry->getPrefix(), interest, *transport_);
143 }
144 else if (decoder.peekDTag(ndn_BinaryXml_DTag_ContentObject)) {
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700145 shared_ptr<Data> data(new Data());
146 data->wireDecode(element, elementLength);
147
Jeff Thompson557b81e2013-08-21 15:13:51 -0700148 int iPitEntry = getEntryIndexForExpressedInterest(data->getName());
149 if (iPitEntry >= 0) {
Jeff Thompson7aec0252013-08-22 17:29:57 -0700150 // Copy pointers to the needed objects and remove the PIT entry before the calling the callback.
151 const OnData onData = pit_[iPitEntry]->getOnData();
152 const ptr_lib::shared_ptr<const Interest> interest = pit_[iPitEntry]->getInterest();
Jeff Thompson557b81e2013-08-21 15:13:51 -0700153 pit_.erase(pit_.begin() + iPitEntry);
Jeff Thompson7aec0252013-08-22 17:29:57 -0700154 onData(interest, data);
Jeff Thompson557b81e2013-08-21 15:13:51 -0700155 }
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700156 }
157}
158
Jeff Thompson0050abe2013-09-17 12:50:25 -0700159void
160Node::shutdown()
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700161{
162 transport_->close();
163}
164
Jeff Thompson0050abe2013-09-17 12:50:25 -0700165int
166Node::getEntryIndexForExpressedInterest(const Name& name)
Jeff Thompson557b81e2013-08-21 15:13:51 -0700167{
168 // TODO: Doesn't this belong in the Name class?
169 vector<struct ndn_NameComponent> nameComponents;
170 nameComponents.reserve(name.getComponentCount());
171 struct ndn_Name nameStruct;
Jeff Thompsond1427fb2013-08-29 17:20:32 -0700172 ndn_Name_initialize(&nameStruct, &nameComponents[0], nameComponents.capacity());
Jeff Thompson557b81e2013-08-21 15:13:51 -0700173 name.get(nameStruct);
174
175 int iResult = -1;
176
177 for (unsigned int i = 0; i < pit_.size(); ++i) {
178 if (ndn_Interest_matchesName((struct ndn_Interest *)&pit_[i]->getInterestStruct(), &nameStruct)) {
179 if (iResult < 0 ||
Jeff Thompson48917f02013-08-21 17:12:45 -0700180 pit_[i]->getInterestStruct().name.nComponents > pit_[iResult]->getInterestStruct().name.nComponents)
181 // Update to the longer match.
Jeff Thompson557b81e2013-08-21 15:13:51 -0700182 iResult = i;
183 }
184 }
185
186 return iResult;
187}
Jeff Thompson86507bc2013-08-23 20:51:38 -0700188
Jeff Thompson0050abe2013-09-17 12:50:25 -0700189Node::PrefixEntry*
190Node::getEntryForRegisteredPrefix(const Name& name)
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700191{
192 int iResult = -1;
193
Jeff Thompson7a57f672013-08-28 09:55:39 -0700194 for (unsigned int i = 0; i < registeredPrefixTable_.size(); ++i) {
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700195 if (registeredPrefixTable_[i]->getPrefix()->match(name)) {
196 if (iResult < 0 ||
197 registeredPrefixTable_[i]->getPrefix()->getComponentCount() > registeredPrefixTable_[iResult]->getPrefix()->getComponentCount())
198 // Update to the longer match.
199 iResult = i;
200 }
201 }
202
203 if (iResult >= 0)
204 return registeredPrefixTable_[iResult].get();
205 else
206 return 0;
207}
208
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700209Node::PitEntry::PitEntry(const ptr_lib::shared_ptr<const Interest>& interest, const OnData& onData, const OnTimeout& onTimeout)
Jeff Thompson86507bc2013-08-23 20:51:38 -0700210: interest_(interest), onData_(onData), onTimeout_(onTimeout)
211{
212 // Set up timeoutTime_.
213 if (interest_->getInterestLifetimeMilliseconds() >= 0.0)
214 timeoutTimeMilliseconds_ = getNowMilliseconds() + interest_->getInterestLifetimeMilliseconds();
215 else
216 // No timeout.
217 timeoutTimeMilliseconds_ = -1.0;
218
219 // Set up interestStruct_.
220 // TODO: Doesn't this belong in the Interest class?
221 nameComponents_.reserve(interest_->getName().getComponentCount());
222 excludeEntries_.reserve(interest_->getExclude().getEntryCount());
Jeff Thompsond1427fb2013-08-29 17:20:32 -0700223 ndn_Interest_initialize
Jeff Thompson86507bc2013-08-23 20:51:38 -0700224 (&interestStruct_, &nameComponents_[0], nameComponents_.capacity(), &excludeEntries_[0], excludeEntries_.capacity());
225 interest_->get(interestStruct_);
226}
227
Jeff Thompson0050abe2013-09-17 12:50:25 -0700228bool
229Node::PitEntry::checkTimeout(Node *parent, double nowMilliseconds)
Jeff Thompson86507bc2013-08-23 20:51:38 -0700230{
231 if (timeoutTimeMilliseconds_ >= 0.0 && nowMilliseconds >= timeoutTimeMilliseconds_) {
232 if (onTimeout_) {
233 // Ignore all exceptions.
234 try {
235 onTimeout_(interest_);
236 }
237 catch (...) { }
238 }
239
240 return true;
241 }
242 else
243 return false;
244}
Jeff Thompson557b81e2013-08-21 15:13:51 -0700245
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700246}