blob: 7c7c5f0568ac10437cf13473e2bbd113fe5d4dd7 [file] [log] [blame]
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -07001/**
2 * @author: Jeff Thompson
3 * See COPYING for copyright and distribution information.
4 */
5
Jeff Thompson48917f02013-08-21 17:12:45 -07006#include <sys/time.h>
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -07007#include "encoding/binary-xml-decoder.hpp"
8#include "c/encoding/binary-xml.h"
Jeff Thompson9cc4be42013-08-27 18:12:41 -07009#include "forwarding-entry.hpp"
10#include "security/key-chain.hpp"
Jeff Thompson20af0732013-09-12 17:01:45 -070011#include "sha256-with-rsa-signature.hpp"
Jeff Thompsonb09fcc12013-08-22 10:37:10 -070012#include "node.hpp"
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070013
14using namespace std;
15using namespace ndn::ptr_lib;
16
17namespace ndn {
18
Jeff Thompson48917f02013-08-21 17:12:45 -070019// Use gettimeofday to return the current time in milliseconds.
20static inline double getNowMilliseconds()
Jeff Thompson557b81e2013-08-21 15:13:51 -070021{
Jeff Thompson48917f02013-08-21 17:12:45 -070022 timeval t;
23 gettimeofday(&t, NULL);
24 return t.tv_sec * 1000.0 + t.tv_usec / 1000.0;
25}
Jeff Thompson557b81e2013-08-21 15:13:51 -070026
Jeff Thompson1656e6a2013-08-29 18:01:48 -070027Node::Node(const ptr_lib::shared_ptr<Transport>& transport, const ptr_lib::shared_ptr<const Transport::ConnectionInfo>& connectionInfo)
Jeff Thompsonfb29cda2013-08-24 10:26:54 -070028: transport_(transport), connectionInfo_(connectionInfo),
29 ndndIdFetcherInterest_(Name("/%C1.M.S.localhost/%C1.M.SRV/ndnd/KEY"), 4000.0)
Jeff Thompson557b81e2013-08-21 15:13:51 -070030{
Jeff Thompson557b81e2013-08-21 15:13:51 -070031}
32
Jeff Thompson1656e6a2013-08-29 18:01:48 -070033void Node::expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070034{
Jeff Thompson86507bc2013-08-23 20:51:38 -070035 // TODO: Properly check if we are already connected to the expected host.
36 if (!transport_->getIsConnected())
37 transport_->connect(*connectionInfo_, *this);
38
Jeff Thompson9cc4be42013-08-27 18:12:41 -070039 pit_.push_back(shared_ptr<PitEntry>(new PitEntry(shared_ptr<const Interest>(new Interest(interest)), onData, onTimeout)));
Jeff Thompson557b81e2013-08-21 15:13:51 -070040
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070041 Blob encoding = interest.wireEncode();
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -070042 transport_->send(*encoding);
43}
44
Jeff Thompson1656e6a2013-08-29 18:01:48 -070045void Node::registerPrefix(const Name& prefix, const OnInterest& onInterest, int flags)
Jeff Thompson86507bc2013-08-23 20:51:38 -070046{
47 if (ndndId_.size() == 0) {
48 // First fetch the ndndId of the connected hub.
49 NdndIdFetcher fetcher(make_shared<NdndIdFetcher::Info>(this, prefix, onInterest, flags));
50 // It is OK for func_lib::function make a copy of the function object because the Info is in a shared_ptr.
51 expressInterest(ndndIdFetcherInterest_, fetcher, fetcher);
52 }
53 else
54 registerPrefixHelper(prefix, onInterest, flags);
55}
56
Jeff Thompson1656e6a2013-08-29 18:01:48 -070057void Node::NdndIdFetcher::operator()(const ptr_lib::shared_ptr<const Interest>& interest, const ptr_lib::shared_ptr<Data>& ndndIdData)
Jeff Thompson86507bc2013-08-23 20:51:38 -070058{
Jeff Thompson20af0732013-09-12 17:01:45 -070059 Sha256WithRsaSignature *signature = dynamic_cast<Sha256WithRsaSignature*>(ndndIdData->getSignature());
60 if (signature && signature->getPublisherPublicKeyDigest().getPublisherPublicKeyDigest().size() > 0) {
Jeff Thompson86507bc2013-08-23 20:51:38 -070061 // Set the ndndId_ and continue.
Jeff Thompson7a67cb62013-08-26 11:43:18 -070062 // TODO: If there are multiple connected hubs, the NDN ID is really stored per connected hub.
Jeff Thompson20af0732013-09-12 17:01:45 -070063 info_->node_.ndndId_ = signature->getPublisherPublicKeyDigest().getPublisherPublicKeyDigest();
Jeff Thompson86507bc2013-08-23 20:51:38 -070064 info_->node_.registerPrefixHelper(info_->prefix_, info_->onInterest_, info_->flags_);
65 }
66 // TODO: else need to log not getting the ndndId.
67}
68
Jeff Thompson1656e6a2013-08-29 18:01:48 -070069void Node::NdndIdFetcher::operator()(const ptr_lib::shared_ptr<const Interest>& timedOutInterest)
Jeff Thompson86507bc2013-08-23 20:51:38 -070070{
71 // TODO: Log the timeout.
72}
73
Jeff Thompson1656e6a2013-08-29 18:01:48 -070074void Node::registerPrefixHelper(const Name& prefix, const OnInterest& onInterest, int flags)
Jeff Thompson86507bc2013-08-23 20:51:38 -070075{
Jeff Thompson9cc4be42013-08-27 18:12:41 -070076 // Create a ForwardingEntry.
77 ForwardingEntry forwardingEntry("selfreg", prefix, PublisherPublicKeyDigest(), -1, 3, 2147483647);
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070078 Blob content = forwardingEntry.wireEncode();
Jeff Thompson9cc4be42013-08-27 18:12:41 -070079
80 // Set the ForwardingEntry as the content of a Data packet and sign.
81 Data data;
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070082 data.setContent(content);
Jeff Thompsonfec716d2013-09-11 13:54:36 -070083 data.getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0);
Jeff Thompson9cc4be42013-08-27 18:12:41 -070084 // TODO: Should we sign with a different key?
85 KeyChain::defaultSign(data);
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070086 Blob encodedData = data.wireEncode();
Jeff Thompson9cc4be42013-08-27 18:12:41 -070087
88 // Create an interest where the name has the encoded Data packet.
89 Name interestName;
90 const unsigned char component0[] = "ndnx";
91 const unsigned char component2[] = "selfreg";
92 interestName.addComponent(component0, sizeof(component0) - 1);
93 interestName.addComponent(ndndId_);
94 interestName.addComponent(component2, sizeof(component2) - 1);
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070095 interestName.addComponent(encodedData);
Jeff Thompson9cc4be42013-08-27 18:12:41 -070096
97 Interest interest(interestName);
98 interest.setScope(1);
Jeff Thompsonc2b7b142013-09-12 15:29:04 -070099 Blob encodedInterest = interest.wireEncode();
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700100
101 // Save the onInterest callback and send the registration interest.
102 registeredPrefixTable_.push_back(shared_ptr<PrefixEntry>(new PrefixEntry(shared_ptr<const Name>(new Name(prefix)), onInterest)));
103
104 transport_->send(*encodedInterest);
Jeff Thompson86507bc2013-08-23 20:51:38 -0700105}
106
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700107void Node::processEvents()
108{
109 transport_->processEvents();
Jeff Thompson48917f02013-08-21 17:12:45 -0700110
111 // Check for PIT entry timeouts. Go backwards through the list so we can erase entries.
112 double nowMilliseconds = getNowMilliseconds();
113 for (int i = (int)pit_.size() - 1; i >= 0; --i) {
114 if (pit_[i]->checkTimeout(this, nowMilliseconds)) {
115 pit_.erase(pit_.begin() + i);
116
117 // Refresh now since the timeout callback might have delayed.
118 nowMilliseconds = getNowMilliseconds();
119 }
120 }
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700121}
122
Jeff Thompson8b173cc2013-08-21 17:54:12 -0700123void Node::onReceivedElement(const unsigned char *element, unsigned int elementLength)
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700124{
125 BinaryXmlDecoder decoder(element, elementLength);
126
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700127 if (decoder.peekDTag(ndn_BinaryXml_DTag_Interest)) {
128 shared_ptr<Interest> interest(new Interest());
129 interest->wireDecode(element, elementLength);
130
131 PrefixEntry *entry = getEntryForRegisteredPrefix(interest->getName());
132 if (entry)
133 entry->getOnInterest()(entry->getPrefix(), interest, *transport_);
134 }
135 else if (decoder.peekDTag(ndn_BinaryXml_DTag_ContentObject)) {
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700136 shared_ptr<Data> data(new Data());
137 data->wireDecode(element, elementLength);
138
Jeff Thompson557b81e2013-08-21 15:13:51 -0700139 int iPitEntry = getEntryIndexForExpressedInterest(data->getName());
140 if (iPitEntry >= 0) {
Jeff Thompson7aec0252013-08-22 17:29:57 -0700141 // Copy pointers to the needed objects and remove the PIT entry before the calling the callback.
142 const OnData onData = pit_[iPitEntry]->getOnData();
143 const ptr_lib::shared_ptr<const Interest> interest = pit_[iPitEntry]->getInterest();
Jeff Thompson557b81e2013-08-21 15:13:51 -0700144 pit_.erase(pit_.begin() + iPitEntry);
Jeff Thompson7aec0252013-08-22 17:29:57 -0700145 onData(interest, data);
Jeff Thompson557b81e2013-08-21 15:13:51 -0700146 }
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700147 }
148}
149
150void Node::shutdown()
151{
152 transport_->close();
153}
154
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700155int Node::getEntryIndexForExpressedInterest(const Name& name)
Jeff Thompson557b81e2013-08-21 15:13:51 -0700156{
157 // TODO: Doesn't this belong in the Name class?
158 vector<struct ndn_NameComponent> nameComponents;
159 nameComponents.reserve(name.getComponentCount());
160 struct ndn_Name nameStruct;
Jeff Thompsond1427fb2013-08-29 17:20:32 -0700161 ndn_Name_initialize(&nameStruct, &nameComponents[0], nameComponents.capacity());
Jeff Thompson557b81e2013-08-21 15:13:51 -0700162 name.get(nameStruct);
163
164 int iResult = -1;
165
166 for (unsigned int i = 0; i < pit_.size(); ++i) {
167 if (ndn_Interest_matchesName((struct ndn_Interest *)&pit_[i]->getInterestStruct(), &nameStruct)) {
168 if (iResult < 0 ||
Jeff Thompson48917f02013-08-21 17:12:45 -0700169 pit_[i]->getInterestStruct().name.nComponents > pit_[iResult]->getInterestStruct().name.nComponents)
170 // Update to the longer match.
Jeff Thompson557b81e2013-08-21 15:13:51 -0700171 iResult = i;
172 }
173 }
174
175 return iResult;
176}
Jeff Thompson86507bc2013-08-23 20:51:38 -0700177
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700178Node::PrefixEntry *Node::getEntryForRegisteredPrefix(const Name& name)
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700179{
180 int iResult = -1;
181
Jeff Thompson7a57f672013-08-28 09:55:39 -0700182 for (unsigned int i = 0; i < registeredPrefixTable_.size(); ++i) {
Jeff Thompson9cc4be42013-08-27 18:12:41 -0700183 if (registeredPrefixTable_[i]->getPrefix()->match(name)) {
184 if (iResult < 0 ||
185 registeredPrefixTable_[i]->getPrefix()->getComponentCount() > registeredPrefixTable_[iResult]->getPrefix()->getComponentCount())
186 // Update to the longer match.
187 iResult = i;
188 }
189 }
190
191 if (iResult >= 0)
192 return registeredPrefixTable_[iResult].get();
193 else
194 return 0;
195}
196
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700197Node::PitEntry::PitEntry(const ptr_lib::shared_ptr<const Interest>& interest, const OnData& onData, const OnTimeout& onTimeout)
Jeff Thompson86507bc2013-08-23 20:51:38 -0700198: interest_(interest), onData_(onData), onTimeout_(onTimeout)
199{
200 // Set up timeoutTime_.
201 if (interest_->getInterestLifetimeMilliseconds() >= 0.0)
202 timeoutTimeMilliseconds_ = getNowMilliseconds() + interest_->getInterestLifetimeMilliseconds();
203 else
204 // No timeout.
205 timeoutTimeMilliseconds_ = -1.0;
206
207 // Set up interestStruct_.
208 // TODO: Doesn't this belong in the Interest class?
209 nameComponents_.reserve(interest_->getName().getComponentCount());
210 excludeEntries_.reserve(interest_->getExclude().getEntryCount());
Jeff Thompsond1427fb2013-08-29 17:20:32 -0700211 ndn_Interest_initialize
Jeff Thompson86507bc2013-08-23 20:51:38 -0700212 (&interestStruct_, &nameComponents_[0], nameComponents_.capacity(), &excludeEntries_[0], excludeEntries_.capacity());
213 interest_->get(interestStruct_);
214}
215
216bool Node::PitEntry::checkTimeout(Node *parent, double nowMilliseconds)
217{
218 if (timeoutTimeMilliseconds_ >= 0.0 && nowMilliseconds >= timeoutTimeMilliseconds_) {
219 if (onTimeout_) {
220 // Ignore all exceptions.
221 try {
222 onTimeout_(interest_);
223 }
224 catch (...) { }
225 }
226
227 return true;
228 }
229 else
230 return false;
231}
Jeff Thompson557b81e2013-08-21 15:13:51 -0700232
Jeff Thompsonbf50a1a2013-08-20 18:01:01 -0700233}