blob: e13297009899c196b63c3d4f42c78df461947a4c [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 Thompson2ce8f492013-09-17 18:01:25 -070091#if 0
Jeff Thompson9cc4be42013-08-27 18:12:41 -070092 KeyChain::defaultSign(data);
Jeff Thompson2ce8f492013-09-17 18:01:25 -070093#else
94#warning "Should we have the application use KeyChain.signData?"
95#endif
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070096 Blob encodedData = data.wireEncode();
Jeff Thompson9cc4be42013-08-27 18:12:41 -070097
98 // Create an interest where the name has the encoded Data packet.
99 Name interestName;
100 const unsigned char component0[] = "ndnx";
101 const unsigned char component2[] = "selfreg";
102 interestName.addComponent(component0, sizeof(component0) - 1);
103 interestName.addComponent(ndndId_);
104 interestName.addComponent(component2, sizeof(component2) - 1);
Jeff Thompsonc2b7b142013-09-12 15:29:04 -0700105 interestName.addComponent(encodedData);
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700106
107 Interest interest(interestName);
108 interest.setScope(1);
Jeff Thompsonc2b7b142013-09-12 15:29:04 -0700109 Blob encodedInterest = interest.wireEncode();
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700110
111 // Save the onInterest callback and send the registration interest.
112 registeredPrefixTable_.push_back(shared_ptr<PrefixEntry>(new PrefixEntry(shared_ptr<const Name>(new Name(prefix)), onInterest)));
113
114 transport_->send(*encodedInterest);
Jeff Thompson86507bc2013-08-23 20:51:38 -0700115}
116
Jeff Thompson0050abe2013-09-17 12:50:25 -0700117void
118Node::processEvents()
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700119{
120 transport_->processEvents();
Jeff Thompson48917f02013-08-21 17:12:45 -0700121
122 // Check for PIT entry timeouts. Go backwards through the list so we can erase entries.
123 double nowMilliseconds = getNowMilliseconds();
124 for (int i = (int)pit_.size() - 1; i >= 0; --i) {
125 if (pit_[i]->checkTimeout(this, nowMilliseconds)) {
126 pit_.erase(pit_.begin() + i);
127
128 // Refresh now since the timeout callback might have delayed.
129 nowMilliseconds = getNowMilliseconds();
130 }
131 }
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700132}
133
Jeff Thompson0050abe2013-09-17 12:50:25 -0700134void
135Node::onReceivedElement(const unsigned char *element, unsigned int elementLength)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700136{
137 BinaryXmlDecoder decoder(element, elementLength);
138
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700139 if (decoder.peekDTag(ndn_BinaryXml_DTag_Interest)) {
140 shared_ptr<Interest> interest(new Interest());
141 interest->wireDecode(element, elementLength);
142
143 PrefixEntry *entry = getEntryForRegisteredPrefix(interest->getName());
144 if (entry)
145 entry->getOnInterest()(entry->getPrefix(), interest, *transport_);
146 }
147 else if (decoder.peekDTag(ndn_BinaryXml_DTag_ContentObject)) {
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700148 shared_ptr<Data> data(new Data());
149 data->wireDecode(element, elementLength);
150
Jeff Thompson557b81e2013-08-21 15:13:51 -0700151 int iPitEntry = getEntryIndexForExpressedInterest(data->getName());
152 if (iPitEntry >= 0) {
Jeff Thompson7aec0252013-08-22 17:29:57 -0700153 // Copy pointers to the needed objects and remove the PIT entry before the calling the callback.
154 const OnData onData = pit_[iPitEntry]->getOnData();
155 const ptr_lib::shared_ptr<const Interest> interest = pit_[iPitEntry]->getInterest();
Jeff Thompson557b81e2013-08-21 15:13:51 -0700156 pit_.erase(pit_.begin() + iPitEntry);
Jeff Thompson7aec0252013-08-22 17:29:57 -0700157 onData(interest, data);
Jeff Thompson557b81e2013-08-21 15:13:51 -0700158 }
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700159 }
160}
161
Jeff Thompson0050abe2013-09-17 12:50:25 -0700162void
163Node::shutdown()
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700164{
165 transport_->close();
166}
167
Jeff Thompson0050abe2013-09-17 12:50:25 -0700168int
169Node::getEntryIndexForExpressedInterest(const Name& name)
Jeff Thompson557b81e2013-08-21 15:13:51 -0700170{
171 // TODO: Doesn't this belong in the Name class?
172 vector<struct ndn_NameComponent> nameComponents;
173 nameComponents.reserve(name.getComponentCount());
174 struct ndn_Name nameStruct;
Jeff Thompsond1427fb2013-08-29 17:20:32 -0700175 ndn_Name_initialize(&nameStruct, &nameComponents[0], nameComponents.capacity());
Jeff Thompson557b81e2013-08-21 15:13:51 -0700176 name.get(nameStruct);
177
178 int iResult = -1;
179
180 for (unsigned int i = 0; i < pit_.size(); ++i) {
181 if (ndn_Interest_matchesName((struct ndn_Interest *)&pit_[i]->getInterestStruct(), &nameStruct)) {
182 if (iResult < 0 ||
Jeff Thompson48917f02013-08-21 17:12:45 -0700183 pit_[i]->getInterestStruct().name.nComponents > pit_[iResult]->getInterestStruct().name.nComponents)
184 // Update to the longer match.
Jeff Thompson557b81e2013-08-21 15:13:51 -0700185 iResult = i;
186 }
187 }
188
189 return iResult;
190}
Jeff Thompson86507bc2013-08-23 20:51:38 -0700191
Jeff Thompson0050abe2013-09-17 12:50:25 -0700192Node::PrefixEntry*
193Node::getEntryForRegisteredPrefix(const Name& name)
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700194{
195 int iResult = -1;
196
Jeff Thompson7a57f672013-08-28 09:55:39 -0700197 for (unsigned int i = 0; i < registeredPrefixTable_.size(); ++i) {
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700198 if (registeredPrefixTable_[i]->getPrefix()->match(name)) {
199 if (iResult < 0 ||
200 registeredPrefixTable_[i]->getPrefix()->getComponentCount() > registeredPrefixTable_[iResult]->getPrefix()->getComponentCount())
201 // Update to the longer match.
202 iResult = i;
203 }
204 }
205
206 if (iResult >= 0)
207 return registeredPrefixTable_[iResult].get();
208 else
209 return 0;
210}
211
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700212Node::PitEntry::PitEntry(const ptr_lib::shared_ptr<const Interest>& interest, const OnData& onData, const OnTimeout& onTimeout)
Jeff Thompson86507bc2013-08-23 20:51:38 -0700213: interest_(interest), onData_(onData), onTimeout_(onTimeout)
214{
215 // Set up timeoutTime_.
216 if (interest_->getInterestLifetimeMilliseconds() >= 0.0)
217 timeoutTimeMilliseconds_ = getNowMilliseconds() + interest_->getInterestLifetimeMilliseconds();
218 else
219 // No timeout.
220 timeoutTimeMilliseconds_ = -1.0;
221
222 // Set up interestStruct_.
223 // TODO: Doesn't this belong in the Interest class?
224 nameComponents_.reserve(interest_->getName().getComponentCount());
225 excludeEntries_.reserve(interest_->getExclude().getEntryCount());
Jeff Thompsond1427fb2013-08-29 17:20:32 -0700226 ndn_Interest_initialize
Jeff Thompson86507bc2013-08-23 20:51:38 -0700227 (&interestStruct_, &nameComponents_[0], nameComponents_.capacity(), &excludeEntries_[0], excludeEntries_.capacity());
228 interest_->get(interestStruct_);
229}
230
Jeff Thompson0050abe2013-09-17 12:50:25 -0700231bool
232Node::PitEntry::checkTimeout(Node *parent, double nowMilliseconds)
Jeff Thompson86507bc2013-08-23 20:51:38 -0700233{
234 if (timeoutTimeMilliseconds_ >= 0.0 && nowMilliseconds >= timeoutTimeMilliseconds_) {
235 if (onTimeout_) {
236 // Ignore all exceptions.
237 try {
238 onTimeout_(interest_);
239 }
240 catch (...) { }
241 }
242
243 return true;
244 }
245 else
246 return false;
247}
Jeff Thompson557b81e2013-08-21 15:13:51 -0700248
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700249}