Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 1 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 2 | * Copyright (C) 2013 Regents of the University of California. |
| 3 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 7 | #include <sys/time.h> |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 8 | #include "encoding/binary-xml-decoder.hpp" |
| 9 | #include "c/encoding/binary-xml.h" |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 10 | #include "forwarding-entry.hpp" |
| 11 | #include "security/key-chain.hpp" |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame] | 12 | #include "sha256-with-rsa-signature.hpp" |
Jeff Thompson | b09fcc1 | 2013-08-22 10:37:10 -0700 | [diff] [blame] | 13 | #include "node.hpp" |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 14 | |
| 15 | using namespace std; |
| 16 | using namespace ndn::ptr_lib; |
| 17 | |
| 18 | namespace ndn { |
| 19 | |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 20 | // Use gettimeofday to return the current time in milliseconds. |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 21 | static inline double |
| 22 | getNowMilliseconds() |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 23 | { |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 24 | timeval t; |
| 25 | gettimeofday(&t, NULL); |
| 26 | return t.tv_sec * 1000.0 + t.tv_usec / 1000.0; |
| 27 | } |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 28 | |
Jeff Thompson | 590ec23 | 2013-09-18 15:55:56 -0700 | [diff] [blame^] | 29 | Node::Node(const shared_ptr<Transport>& transport, const shared_ptr<const Transport::ConnectionInfo>& connectionInfo) |
Jeff Thompson | fb29cda | 2013-08-24 10:26:54 -0700 | [diff] [blame] | 30 | : transport_(transport), connectionInfo_(connectionInfo), |
| 31 | ndndIdFetcherInterest_(Name("/%C1.M.S.localhost/%C1.M.SRV/ndnd/KEY"), 4000.0) |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 32 | { |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 35 | void |
| 36 | Node::expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout) |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 37 | { |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 38 | // TODO: Properly check if we are already connected to the expected host. |
| 39 | if (!transport_->getIsConnected()) |
| 40 | transport_->connect(*connectionInfo_, *this); |
| 41 | |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 42 | pit_.push_back(shared_ptr<PitEntry>(new PitEntry(shared_ptr<const Interest>(new Interest(interest)), onData, onTimeout))); |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 43 | |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 44 | Blob encoding = interest.wireEncode(); |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 45 | transport_->send(*encoding); |
| 46 | } |
| 47 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 48 | void |
Jeff Thompson | 590ec23 | 2013-09-18 15:55:56 -0700 | [diff] [blame^] | 49 | Node::registerPrefix |
| 50 | (const Name& prefix, const OnInterest& onInterest, const OnRegisterFailed& onRegisterFailed, KeyChain &keyChain, |
| 51 | const Name& signerName, bool byKeyName, int flags, WireFormat& wireFormat) |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 52 | { |
| 53 | if (ndndId_.size() == 0) { |
| 54 | // First fetch the ndndId of the connected hub. |
Jeff Thompson | 590ec23 | 2013-09-18 15:55:56 -0700 | [diff] [blame^] | 55 | NdndIdFetcher fetcher |
| 56 | (shared_ptr<NdndIdFetcher::Info>(new NdndIdFetcher::Info |
| 57 | (this, prefix, onInterest, onRegisterFailed, keyChain, signerName, byKeyName, flags, wireFormat))); |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 58 | // It is OK for func_lib::function make a copy of the function object because the Info is in a shared_ptr. |
| 59 | expressInterest(ndndIdFetcherInterest_, fetcher, fetcher); |
| 60 | } |
| 61 | else |
Jeff Thompson | 590ec23 | 2013-09-18 15:55:56 -0700 | [diff] [blame^] | 62 | registerPrefixHelper |
| 63 | (make_shared<const Name>(prefix), onInterest, onRegisterFailed, keyChain, make_shared<const Name>(signerName), byKeyName, |
| 64 | flags, wireFormat); |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 67 | void |
Jeff Thompson | 590ec23 | 2013-09-18 15:55:56 -0700 | [diff] [blame^] | 68 | Node::NdndIdFetcher::operator()(const shared_ptr<const Interest>& interest, const shared_ptr<Data>& ndndIdData) |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 69 | { |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame] | 70 | Sha256WithRsaSignature *signature = dynamic_cast<Sha256WithRsaSignature*>(ndndIdData->getSignature()); |
| 71 | if (signature && signature->getPublisherPublicKeyDigest().getPublisherPublicKeyDigest().size() > 0) { |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 72 | // Set the ndndId_ and continue. |
Jeff Thompson | 7a67cb6 | 2013-08-26 11:43:18 -0700 | [diff] [blame] | 73 | // TODO: If there are multiple connected hubs, the NDN ID is really stored per connected hub. |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame] | 74 | info_->node_.ndndId_ = signature->getPublisherPublicKeyDigest().getPublisherPublicKeyDigest(); |
Jeff Thompson | 590ec23 | 2013-09-18 15:55:56 -0700 | [diff] [blame^] | 75 | info_->node_.registerPrefixHelper |
| 76 | (info_->prefix_, info_->onInterest_, info_->onRegisterFailed_, info_->keyChain_, info_->signerName_, |
| 77 | info_->byKeyName_, info_->flags_, info_->wireFormat_); |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 78 | } |
Jeff Thompson | 590ec23 | 2013-09-18 15:55:56 -0700 | [diff] [blame^] | 79 | else |
| 80 | info_->onRegisterFailed_(info_->prefix_); |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 83 | void |
Jeff Thompson | 590ec23 | 2013-09-18 15:55:56 -0700 | [diff] [blame^] | 84 | Node::NdndIdFetcher::operator()(const shared_ptr<const Interest>& timedOutInterest) |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 85 | { |
Jeff Thompson | 590ec23 | 2013-09-18 15:55:56 -0700 | [diff] [blame^] | 86 | info_->onRegisterFailed_(info_->prefix_); |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 89 | void |
Jeff Thompson | 590ec23 | 2013-09-18 15:55:56 -0700 | [diff] [blame^] | 90 | Node::registerPrefixHelper |
| 91 | (const shared_ptr<const Name>& prefix, const OnInterest& onInterest, const OnRegisterFailed& onRegisterFailed, |
| 92 | KeyChain &keyChain, const shared_ptr<const Name>& signerName, bool byKeyName, int flags, WireFormat& wireFormat) |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 93 | { |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 94 | // Create a ForwardingEntry. |
Jeff Thompson | 590ec23 | 2013-09-18 15:55:56 -0700 | [diff] [blame^] | 95 | ForwardingEntry forwardingEntry("selfreg", *prefix, PublisherPublicKeyDigest(), -1, 3, 2147483647); |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 96 | Blob content = forwardingEntry.wireEncode(); |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 97 | |
| 98 | // Set the ForwardingEntry as the content of a Data packet and sign. |
| 99 | Data data; |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 100 | data.setContent(content); |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 101 | data.getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0); |
Jeff Thompson | 590ec23 | 2013-09-18 15:55:56 -0700 | [diff] [blame^] | 102 | keyChain.signData(data, *signerName, byKeyName, wireFormat); |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 103 | Blob encodedData = data.wireEncode(); |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 104 | |
| 105 | // Create an interest where the name has the encoded Data packet. |
| 106 | Name interestName; |
| 107 | const unsigned char component0[] = "ndnx"; |
| 108 | const unsigned char component2[] = "selfreg"; |
| 109 | interestName.addComponent(component0, sizeof(component0) - 1); |
| 110 | interestName.addComponent(ndndId_); |
| 111 | interestName.addComponent(component2, sizeof(component2) - 1); |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 112 | interestName.addComponent(encodedData); |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 113 | |
| 114 | Interest interest(interestName); |
| 115 | interest.setScope(1); |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 116 | Blob encodedInterest = interest.wireEncode(); |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 117 | |
| 118 | // Save the onInterest callback and send the registration interest. |
Jeff Thompson | 590ec23 | 2013-09-18 15:55:56 -0700 | [diff] [blame^] | 119 | registeredPrefixTable_.push_back(shared_ptr<PrefixEntry>(new PrefixEntry(prefix, onInterest))); |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 120 | |
| 121 | transport_->send(*encodedInterest); |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 124 | void |
| 125 | Node::processEvents() |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 126 | { |
| 127 | transport_->processEvents(); |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 128 | |
| 129 | // Check for PIT entry timeouts. Go backwards through the list so we can erase entries. |
| 130 | double nowMilliseconds = getNowMilliseconds(); |
| 131 | for (int i = (int)pit_.size() - 1; i >= 0; --i) { |
| 132 | if (pit_[i]->checkTimeout(this, nowMilliseconds)) { |
| 133 | pit_.erase(pit_.begin() + i); |
| 134 | |
| 135 | // Refresh now since the timeout callback might have delayed. |
| 136 | nowMilliseconds = getNowMilliseconds(); |
| 137 | } |
| 138 | } |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 141 | void |
| 142 | Node::onReceivedElement(const unsigned char *element, unsigned int elementLength) |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 143 | { |
| 144 | BinaryXmlDecoder decoder(element, elementLength); |
| 145 | |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 146 | if (decoder.peekDTag(ndn_BinaryXml_DTag_Interest)) { |
| 147 | shared_ptr<Interest> interest(new Interest()); |
| 148 | interest->wireDecode(element, elementLength); |
| 149 | |
| 150 | PrefixEntry *entry = getEntryForRegisteredPrefix(interest->getName()); |
| 151 | if (entry) |
| 152 | entry->getOnInterest()(entry->getPrefix(), interest, *transport_); |
| 153 | } |
| 154 | else if (decoder.peekDTag(ndn_BinaryXml_DTag_ContentObject)) { |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 155 | shared_ptr<Data> data(new Data()); |
| 156 | data->wireDecode(element, elementLength); |
| 157 | |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 158 | int iPitEntry = getEntryIndexForExpressedInterest(data->getName()); |
| 159 | if (iPitEntry >= 0) { |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 160 | // Copy pointers to the needed objects and remove the PIT entry before the calling the callback. |
| 161 | const OnData onData = pit_[iPitEntry]->getOnData(); |
Jeff Thompson | 590ec23 | 2013-09-18 15:55:56 -0700 | [diff] [blame^] | 162 | const shared_ptr<const Interest> interest = pit_[iPitEntry]->getInterest(); |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 163 | pit_.erase(pit_.begin() + iPitEntry); |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 164 | onData(interest, data); |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 165 | } |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 169 | void |
| 170 | Node::shutdown() |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 171 | { |
| 172 | transport_->close(); |
| 173 | } |
| 174 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 175 | int |
| 176 | Node::getEntryIndexForExpressedInterest(const Name& name) |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 177 | { |
| 178 | // TODO: Doesn't this belong in the Name class? |
| 179 | vector<struct ndn_NameComponent> nameComponents; |
| 180 | nameComponents.reserve(name.getComponentCount()); |
| 181 | struct ndn_Name nameStruct; |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 182 | ndn_Name_initialize(&nameStruct, &nameComponents[0], nameComponents.capacity()); |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 183 | name.get(nameStruct); |
| 184 | |
| 185 | int iResult = -1; |
| 186 | |
| 187 | for (unsigned int i = 0; i < pit_.size(); ++i) { |
| 188 | if (ndn_Interest_matchesName((struct ndn_Interest *)&pit_[i]->getInterestStruct(), &nameStruct)) { |
| 189 | if (iResult < 0 || |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 190 | pit_[i]->getInterestStruct().name.nComponents > pit_[iResult]->getInterestStruct().name.nComponents) |
| 191 | // Update to the longer match. |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 192 | iResult = i; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | return iResult; |
| 197 | } |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 198 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 199 | Node::PrefixEntry* |
| 200 | Node::getEntryForRegisteredPrefix(const Name& name) |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 201 | { |
| 202 | int iResult = -1; |
| 203 | |
Jeff Thompson | 7a57f67 | 2013-08-28 09:55:39 -0700 | [diff] [blame] | 204 | for (unsigned int i = 0; i < registeredPrefixTable_.size(); ++i) { |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 205 | if (registeredPrefixTable_[i]->getPrefix()->match(name)) { |
| 206 | if (iResult < 0 || |
| 207 | registeredPrefixTable_[i]->getPrefix()->getComponentCount() > registeredPrefixTable_[iResult]->getPrefix()->getComponentCount()) |
| 208 | // Update to the longer match. |
| 209 | iResult = i; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | if (iResult >= 0) |
| 214 | return registeredPrefixTable_[iResult].get(); |
| 215 | else |
| 216 | return 0; |
| 217 | } |
| 218 | |
Jeff Thompson | 590ec23 | 2013-09-18 15:55:56 -0700 | [diff] [blame^] | 219 | Node::PitEntry::PitEntry(const shared_ptr<const Interest>& interest, const OnData& onData, const OnTimeout& onTimeout) |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 220 | : interest_(interest), onData_(onData), onTimeout_(onTimeout) |
| 221 | { |
| 222 | // Set up timeoutTime_. |
| 223 | if (interest_->getInterestLifetimeMilliseconds() >= 0.0) |
| 224 | timeoutTimeMilliseconds_ = getNowMilliseconds() + interest_->getInterestLifetimeMilliseconds(); |
| 225 | else |
| 226 | // No timeout. |
| 227 | timeoutTimeMilliseconds_ = -1.0; |
| 228 | |
| 229 | // Set up interestStruct_. |
| 230 | // TODO: Doesn't this belong in the Interest class? |
| 231 | nameComponents_.reserve(interest_->getName().getComponentCount()); |
| 232 | excludeEntries_.reserve(interest_->getExclude().getEntryCount()); |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 233 | ndn_Interest_initialize |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 234 | (&interestStruct_, &nameComponents_[0], nameComponents_.capacity(), &excludeEntries_[0], excludeEntries_.capacity()); |
| 235 | interest_->get(interestStruct_); |
| 236 | } |
| 237 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 238 | bool |
| 239 | Node::PitEntry::checkTimeout(Node *parent, double nowMilliseconds) |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 240 | { |
| 241 | if (timeoutTimeMilliseconds_ >= 0.0 && nowMilliseconds >= timeoutTimeMilliseconds_) { |
| 242 | if (onTimeout_) { |
| 243 | // Ignore all exceptions. |
| 244 | try { |
| 245 | onTimeout_(interest_); |
| 246 | } |
| 247 | catch (...) { } |
| 248 | } |
| 249 | |
| 250 | return true; |
| 251 | } |
| 252 | else |
| 253 | return false; |
| 254 | } |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 255 | |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 256 | } |