Name IdentityStorage::getNewKeyName (const Name& identityName, bool useKsk) { uint64_t timestamp = (uint64_t)::floor(ndn_getNowMilliseconds() / 1000.0); while (timestamp <= lastTimestamp_) // Make the timestamp unique. timestamp += 1; lastTimestamp_ = timestamp; // Get the number of seconds as a string. ostringstream oss; oss << timestamp; string keyIdStr; if (useKsk) keyIdStr = ("ksk-" + oss.str()); else keyIdStr = ("dsk-" + oss.str()); Name keyName = Name(identityName).append(keyIdStr); if (doesKeyExist(keyName)) throw SecurityException("Key name already exists"); return keyName; }
IdentityStorage::IdentityStorage() { if (!lastTimestampIsInitialized_) { lastTimestampIsInitialized_ = true; lastTimestamp_ = (uint64_t)::floor(ndn_getNowMilliseconds() / 1000.0); } }
void CommandInterestGenerator::generate (Interest& interest, KeyChain& keyChain, const Name& certificateName, WireFormat& wireFormat) { MillisecondsSince1970 timestamp = ::round(ndn_getNowMilliseconds()); while (timestamp <= lastTimestamp_) timestamp += 1.0; // The timestamp is encoded as a TLV nonNegativeInteger. TlvEncoder encoder(8); encoder.writeNonNegativeInteger((uint64_t)timestamp); interest.getName().append(Blob(encoder.finish())); // The random value is a TLV nonNegativeInteger too, but we know it is 8 bytes, // so we don't need to call the nonNegativeInteger encoder. uint8_t randomBuffer[8]; ndn_Error error; if ((error = CryptoLite::generateRandomBytes(randomBuffer, sizeof(randomBuffer)))) throw runtime_error(ndn_getErrorString(error)); interest.getName().append(randomBuffer, sizeof(randomBuffer)); keyChain.sign(interest, certificateName, wireFormat); if (interest.getInterestLifetimeMilliseconds() < 0) // The caller has not set the interest lifetime, so set it here. interest.setInterestLifetimeMilliseconds(1000.0); // We successfully signed the interest, so update the timestamp. lastTimestamp_ = timestamp; }
MemoryContentCache::StaleTimeContent::StaleTimeContent(const Data& data) // wireEncode returns the cached encoding if available. : Content(data) { // Set up staleTimeMilliseconds_. staleTimeMilliseconds_ = ndn_getNowMilliseconds() + data.getMetaInfo().getFreshnessPeriod(); }
MemoryContentCache::MemoryContentCache (Face* face, Milliseconds cleanupIntervalMilliseconds) : face_(face), cleanupIntervalMilliseconds_(cleanupIntervalMilliseconds), nextCleanupTime_(ndn_getNowMilliseconds() + cleanupIntervalMilliseconds), // We don't expect this callback to be stored outside of this object, so // don't worry about using shared_from_this(). storePendingInterestCallback_(bind (&MemoryContentCache::storePendingInterestCallback, this, _1, _2, _3, _4, _5)) { }
MemoryContentCache::PendingInterest::PendingInterest (const ptr_lib::shared_ptr<const Interest>& interest, Face& face) : interest_(interest), face_(face), timeoutPeriodStart_(ndn_getNowMilliseconds()) { // Set up timeoutTime_. if (interest_->getInterestLifetimeMilliseconds() >= 0.0) timeoutTimeMilliseconds_ = timeoutPeriodStart_ + interest_->getInterestLifetimeMilliseconds(); else // No timeout. timeoutTimeMilliseconds_ = -1.0; }
void DelayedCallTable::callTimedOut() { MillisecondsSince1970 now = ndn_getNowMilliseconds(); // table_ is sorted on _callTime, so we only need to process the timed-out // entries at the front, then quit. while (table_.size() > 0 && table_.front()->getCallTime() <= now) { ptr_lib::shared_ptr<Entry> entry = table_.front(); table_.erase(table_.begin()); entry->callCallback(); } }
void MemoryContentCache::doCleanup() { MillisecondsSince1970 now = ndn_getNowMilliseconds(); if (now >= nextCleanupTime_) { // staleTimeCache_ is sorted on staleTimeMilliseconds_, so we only need to // erase the stale entries at the front, then quit. while (staleTimeCache_.size() > 0 && staleTimeCache_.front()->isStale(now)) staleTimeCache_.erase(staleTimeCache_.begin()); nextCleanupTime_ = now + cleanupIntervalMilliseconds_; } }
void MemoryContentCache::add(const Data& data) { doCleanup(); if (data.getMetaInfo().getFreshnessPeriod() >= 0.0) { // The content will go stale, so use staleTimeCache_. ptr_lib::shared_ptr<const StaleTimeContent> content(new StaleTimeContent(data)); // Insert into staleTimeCache_, sorted on content->staleTimeMilliseconds_. staleTimeCache_.insert (std::lower_bound(staleTimeCache_.begin(), staleTimeCache_.end(), content, contentCompare_), content); } else // The data does not go stale, so use noStaleTimeCache_. noStaleTimeCache_.push_back (ptr_lib::make_shared<const Content>(data)); // Remove timed-out interests and check if the data packet matches any pending // interest. // Go backwards through the list so we can erase entries. MillisecondsSince1970 nowMilliseconds = ndn_getNowMilliseconds(); for (int i = (int)pendingInterestTable_.size() - 1; i >= 0; --i) { if (pendingInterestTable_[i]->isTimedOut(nowMilliseconds)) { pendingInterestTable_.erase(pendingInterestTable_.begin() + i); continue; } if (pendingInterestTable_[i]->getInterest()->matchesName(data.getName())) { try { // Send to the same transport from the original call to onInterest. // wireEncode returns the cached encoding if available. pendingInterestTable_[i]->getFace().send(*data.wireEncode()); } catch (std::exception& e) { _LOG_DEBUG("Error in send: " << e.what()); return; } // The pending interest is satisfied, so remove it. pendingInterestTable_.erase(pendingInterestTable_.begin() + i); } } }
void Node::processEvents() { transport_->processEvents(); // Check for delayed calls. Since callLater does a sorted insert into // delayedCallTable_, the check for timeouts is quick and does not require // searching the entire table. If callLater is overridden to use a different // mechanism, then processEvents is not needed to check for delayed calls. MillisecondsSince1970 now = ndn_getNowMilliseconds(); // delayedCallTable_ is sorted on _callTime, so we only need to process // the timed-out entries at the front, then quit. while (delayedCallTable_.size() > 0 && delayedCallTable_.front()->getCallTime() <= now) { ptr_lib::shared_ptr<DelayedCall> delayedCall = delayedCallTable_.front(); delayedCallTable_.erase(delayedCallTable_.begin()); delayedCall->callCallback(); } }
void MemoryContentCache::getPendingInterestsForName (const Name& name, vector<ptr_lib::shared_ptr<const PendingInterest> >& pendingInterests) { pendingInterests.clear(); // Remove timed-out interests as we add results. // Go backwards through the list so we can erase entries. MillisecondsSince1970 nowMilliseconds = ndn_getNowMilliseconds(); for (int i = (int)pendingInterestTable_.size() - 1; i >= 0; --i) { if (pendingInterestTable_[i]->isTimedOut(nowMilliseconds)) { pendingInterestTable_.erase(pendingInterestTable_.begin() + i); continue; } if (pendingInterestTable_[i]->getInterest()->matchesName(name)) pendingInterests.push_back(pendingInterestTable_[i]); } }
ptr_lib::shared_ptr<IdentityCertificate> IdentityManager::selfSign(const Name& keyName) { ptr_lib::shared_ptr<IdentityCertificate> certificate(new IdentityCertificate()); Blob keyBlob = identityStorage_->getKey(keyName); ptr_lib::shared_ptr<PublicKey> publicKey(new PublicKey(keyBlob)); #if NDN_CPP_HAVE_GMTIME_SUPPORT time_t nowSeconds = time(NULL); struct tm current = *gmtime(&nowSeconds); current.tm_hour = 0; current.tm_min = 0; current.tm_sec = 0; MillisecondsSince1970 notBefore = timegm(¤t) * 1000.0; current.tm_year = current.tm_year + 2; MillisecondsSince1970 notAfter = timegm(¤t) * 1000.0; certificate->setNotBefore(notBefore); certificate->setNotAfter(notAfter); #else // Don't really expect this to happen. throw SecurityException("selfSign: Can't set certificate validity because time functions are not supported by the standard library."); #endif Name certificateName = keyName.getPrefix(-1).append("KEY").append (keyName.get(-1)).append("ID-CERT").append (Name::Component::fromNumber((uint64_t)ndn_getNowMilliseconds())); certificate->setName(certificateName); certificate->setPublicKeyInfo(*publicKey); certificate->addSubjectDescription(CertificateSubjectDescription("2.5.4.41", keyName.toUri())); certificate->encode(); signByCertificate(*certificate, certificate->getName()); return certificate; }
Node::DelayedCall::DelayedCall (Milliseconds delayMilliseconds, const Face::Callback& callback) : callback_(callback), callTime_(ndn_getNowMilliseconds() + delayMilliseconds) { }
DelayedCallTable::Entry::Entry (Milliseconds delayMilliseconds, const Face::Callback& callback) : callback_(callback), callTime_(ndn_getNowMilliseconds() + delayMilliseconds) { }
CommandInterestGenerator::CommandInterestGenerator() : lastTimestamp_(::round(ndn_getNowMilliseconds())) { }