Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @author: Jeff Thompson |
| 3 | * See COPYING for copyright and distribution information. |
| 4 | */ |
| 5 | |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 6 | #include <sys/time.h> |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 7 | #include "encoding/binary-xml-decoder.hpp" |
| 8 | #include "c/encoding/binary-xml.h" |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 9 | #include "forwarding-entry.hpp" |
| 10 | #include "security/key-chain.hpp" |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame^] | 11 | #include "sha256-with-rsa-signature.hpp" |
Jeff Thompson | b09fcc1 | 2013-08-22 10:37:10 -0700 | [diff] [blame] | 12 | #include "node.hpp" |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 13 | |
| 14 | using namespace std; |
| 15 | using namespace ndn::ptr_lib; |
| 16 | |
| 17 | namespace ndn { |
| 18 | |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 19 | // Use gettimeofday to return the current time in milliseconds. |
| 20 | static inline double getNowMilliseconds() |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 21 | { |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 22 | timeval t; |
| 23 | gettimeofday(&t, NULL); |
| 24 | return t.tv_sec * 1000.0 + t.tv_usec / 1000.0; |
| 25 | } |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 26 | |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 27 | 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] | 28 | : transport_(transport), connectionInfo_(connectionInfo), |
| 29 | 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] | 30 | { |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 33 | void Node::expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout) |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 34 | { |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 35 | // TODO: Properly check if we are already connected to the expected host. |
| 36 | if (!transport_->getIsConnected()) |
| 37 | transport_->connect(*connectionInfo_, *this); |
| 38 | |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 39 | 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] | 40 | |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 41 | Blob encoding = interest.wireEncode(); |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 42 | transport_->send(*encoding); |
| 43 | } |
| 44 | |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 45 | void Node::registerPrefix(const Name& prefix, const OnInterest& onInterest, int flags) |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 46 | { |
| 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 Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 57 | void 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] | 58 | { |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame^] | 59 | Sha256WithRsaSignature *signature = dynamic_cast<Sha256WithRsaSignature*>(ndndIdData->getSignature()); |
| 60 | if (signature && signature->getPublisherPublicKeyDigest().getPublisherPublicKeyDigest().size() > 0) { |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 61 | // Set the ndndId_ and continue. |
Jeff Thompson | 7a67cb6 | 2013-08-26 11:43:18 -0700 | [diff] [blame] | 62 | // 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^] | 63 | info_->node_.ndndId_ = signature->getPublisherPublicKeyDigest().getPublisherPublicKeyDigest(); |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 64 | info_->node_.registerPrefixHelper(info_->prefix_, info_->onInterest_, info_->flags_); |
| 65 | } |
| 66 | // TODO: else need to log not getting the ndndId. |
| 67 | } |
| 68 | |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 69 | void Node::NdndIdFetcher::operator()(const ptr_lib::shared_ptr<const Interest>& timedOutInterest) |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 70 | { |
| 71 | // TODO: Log the timeout. |
| 72 | } |
| 73 | |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 74 | void Node::registerPrefixHelper(const Name& prefix, const OnInterest& onInterest, int flags) |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 75 | { |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 76 | // Create a ForwardingEntry. |
| 77 | ForwardingEntry forwardingEntry("selfreg", prefix, PublisherPublicKeyDigest(), -1, 3, 2147483647); |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 78 | Blob content = forwardingEntry.wireEncode(); |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 79 | |
| 80 | // Set the ForwardingEntry as the content of a Data packet and sign. |
| 81 | Data data; |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 82 | data.setContent(content); |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 83 | data.getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0); |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 84 | // TODO: Should we sign with a different key? |
| 85 | KeyChain::defaultSign(data); |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 86 | Blob encodedData = data.wireEncode(); |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 87 | |
| 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 Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 95 | interestName.addComponent(encodedData); |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 96 | |
| 97 | Interest interest(interestName); |
| 98 | interest.setScope(1); |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 99 | Blob encodedInterest = interest.wireEncode(); |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 100 | |
| 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 Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 107 | void Node::processEvents() |
| 108 | { |
| 109 | transport_->processEvents(); |
Jeff Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 110 | |
| 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 Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Jeff Thompson | 8b173cc | 2013-08-21 17:54:12 -0700 | [diff] [blame] | 123 | void Node::onReceivedElement(const unsigned char *element, unsigned int elementLength) |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 124 | { |
| 125 | BinaryXmlDecoder decoder(element, elementLength); |
| 126 | |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 127 | 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 Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 136 | shared_ptr<Data> data(new Data()); |
| 137 | data->wireDecode(element, elementLength); |
| 138 | |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 139 | int iPitEntry = getEntryIndexForExpressedInterest(data->getName()); |
| 140 | if (iPitEntry >= 0) { |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 141 | // 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 Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 144 | pit_.erase(pit_.begin() + iPitEntry); |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 145 | onData(interest, data); |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 146 | } |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| 150 | void Node::shutdown() |
| 151 | { |
| 152 | transport_->close(); |
| 153 | } |
| 154 | |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 155 | int Node::getEntryIndexForExpressedInterest(const Name& name) |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 156 | { |
| 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 Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 161 | ndn_Name_initialize(&nameStruct, &nameComponents[0], nameComponents.capacity()); |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 162 | 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 Thompson | 48917f0 | 2013-08-21 17:12:45 -0700 | [diff] [blame] | 169 | pit_[i]->getInterestStruct().name.nComponents > pit_[iResult]->getInterestStruct().name.nComponents) |
| 170 | // Update to the longer match. |
Jeff Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 171 | iResult = i; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | return iResult; |
| 176 | } |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 177 | |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 178 | Node::PrefixEntry *Node::getEntryForRegisteredPrefix(const Name& name) |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 179 | { |
| 180 | int iResult = -1; |
| 181 | |
Jeff Thompson | 7a57f67 | 2013-08-28 09:55:39 -0700 | [diff] [blame] | 182 | for (unsigned int i = 0; i < registeredPrefixTable_.size(); ++i) { |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 183 | 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 Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 197 | 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] | 198 | : 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 Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 211 | ndn_Interest_initialize |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 212 | (&interestStruct_, &nameComponents_[0], nameComponents_.capacity(), &excludeEntries_[0], excludeEntries_.capacity()); |
| 213 | interest_->get(interestStruct_); |
| 214 | } |
| 215 | |
| 216 | bool 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 Thompson | 557b81e | 2013-08-21 15:13:51 -0700 | [diff] [blame] | 232 | |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 233 | } |