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 | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 29 | Node::Node(const ptr_lib::shared_ptr<Transport>& transport, const ptr_lib::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 |
| 49 | Node::registerPrefix(const Name& prefix, const OnInterest& onInterest, int flags) |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 50 | { |
| 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 Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame^] | 61 | void |
| 62 | Node::NdndIdFetcher::operator()(const ptr_lib::shared_ptr<const Interest>& interest, const ptr_lib::shared_ptr<Data>& ndndIdData) |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 63 | { |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame] | 64 | Sha256WithRsaSignature *signature = dynamic_cast<Sha256WithRsaSignature*>(ndndIdData->getSignature()); |
| 65 | if (signature && signature->getPublisherPublicKeyDigest().getPublisherPublicKeyDigest().size() > 0) { |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 66 | // Set the ndndId_ and continue. |
Jeff Thompson | 7a67cb6 | 2013-08-26 11:43:18 -0700 | [diff] [blame] | 67 | // 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] | 68 | info_->node_.ndndId_ = signature->getPublisherPublicKeyDigest().getPublisherPublicKeyDigest(); |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 69 | info_->node_.registerPrefixHelper(info_->prefix_, info_->onInterest_, info_->flags_); |
| 70 | } |
| 71 | // TODO: else need to log not getting the ndndId. |
| 72 | } |
| 73 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame^] | 74 | void |
| 75 | Node::NdndIdFetcher::operator()(const ptr_lib::shared_ptr<const Interest>& timedOutInterest) |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 76 | { |
| 77 | // TODO: Log the timeout. |
| 78 | } |
| 79 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame^] | 80 | void |
| 81 | Node::registerPrefixHelper(const Name& prefix, const OnInterest& onInterest, int flags) |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 82 | { |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 83 | // Create a ForwardingEntry. |
| 84 | ForwardingEntry forwardingEntry("selfreg", prefix, PublisherPublicKeyDigest(), -1, 3, 2147483647); |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 85 | Blob content = forwardingEntry.wireEncode(); |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 86 | |
| 87 | // Set the ForwardingEntry as the content of a Data packet and sign. |
| 88 | Data data; |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 89 | data.setContent(content); |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 90 | data.getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0); |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 91 | // TODO: Should we sign with a different key? |
| 92 | KeyChain::defaultSign(data); |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 93 | Blob encodedData = data.wireEncode(); |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 94 | |
| 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 Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 102 | interestName.addComponent(encodedData); |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 103 | |
| 104 | Interest interest(interestName); |
| 105 | interest.setScope(1); |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 106 | Blob encodedInterest = interest.wireEncode(); |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 107 | |
| 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 Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame^] | 114 | void |
| 115 | Node::processEvents() |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 116 | { |
| 117 | transport_->processEvents(); |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 118 | |
| 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 Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame^] | 131 | void |
| 132 | Node::onReceivedElement(const unsigned char *element, unsigned int elementLength) |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 133 | { |
| 134 | BinaryXmlDecoder decoder(element, elementLength); |
| 135 | |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 136 | 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 Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 145 | shared_ptr<Data> data(new Data()); |
| 146 | data->wireDecode(element, elementLength); |
| 147 | |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 148 | int iPitEntry = getEntryIndexForExpressedInterest(data->getName()); |
| 149 | if (iPitEntry >= 0) { |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 150 | // 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 Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 153 | pit_.erase(pit_.begin() + iPitEntry); |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 154 | onData(interest, data); |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 155 | } |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame^] | 159 | void |
| 160 | Node::shutdown() |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 161 | { |
| 162 | transport_->close(); |
| 163 | } |
| 164 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame^] | 165 | int |
| 166 | Node::getEntryIndexForExpressedInterest(const Name& name) |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 167 | { |
| 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 Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 172 | ndn_Name_initialize(&nameStruct, &nameComponents[0], nameComponents.capacity()); |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 173 | 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 Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 180 | pit_[i]->getInterestStruct().name.nComponents > pit_[iResult]->getInterestStruct().name.nComponents) |
| 181 | // Update to the longer match. |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 182 | iResult = i; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | return iResult; |
| 187 | } |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 188 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame^] | 189 | Node::PrefixEntry* |
| 190 | Node::getEntryForRegisteredPrefix(const Name& name) |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 191 | { |
| 192 | int iResult = -1; |
| 193 | |
Jeff Thompson | 7a57f67 | 2013-08-28 09:55:39 -0700 | [diff] [blame] | 194 | for (unsigned int i = 0; i < registeredPrefixTable_.size(); ++i) { |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 195 | 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 Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 209 | Node::PitEntry::PitEntry(const ptr_lib::shared_ptr<const Interest>& interest, const OnData& onData, const OnTimeout& onTimeout) |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 210 | : 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 Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 223 | ndn_Interest_initialize |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 224 | (&interestStruct_, &nameComponents_[0], nameComponents_.capacity(), &excludeEntries_[0], excludeEntries_.capacity()); |
| 225 | interest_->get(interestStruct_); |
| 226 | } |
| 227 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame^] | 228 | bool |
| 229 | Node::PitEntry::checkTimeout(Node *parent, double nowMilliseconds) |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 230 | { |
| 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 Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 245 | |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 246 | } |