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