| /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| /** |
| * Copyright (c) 2013-2014 Regents of the University of California. |
| * |
| * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| * |
| * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| * terms of the GNU Lesser General Public License as published by the Free Software |
| * Foundation, either version 3 of the License, or (at your option) any later version. |
| * |
| * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| * |
| * You should have received copies of the GNU General Public License and GNU Lesser |
| * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| * <http://www.gnu.org/licenses/>. |
| * |
| * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| * |
| * Based on code originally written by Jeff Thompson <jefft0@remap.ucla.edu> |
| */ |
| |
| #include "common.hpp" |
| |
| #include "interest.hpp" |
| #include "util/random.hpp" |
| #include "data.hpp" |
| |
| namespace ndn { |
| |
| uint32_t |
| Interest::getNonce() const |
| { |
| if (!m_nonce.hasWire()) |
| const_cast<Interest*>(this)->setNonce(random::generateWord32()); |
| |
| if (m_nonce.value_size() == sizeof(uint32_t)) |
| return *reinterpret_cast<const uint32_t*>(m_nonce.value()); |
| else { |
| // for compatibility reasons. Should be removed eventually |
| return readNonNegativeInteger(m_nonce); |
| } |
| } |
| |
| Interest& |
| Interest::setNonce(uint32_t nonce) |
| { |
| if (m_wire.hasWire() && m_nonce.value_size() == sizeof(uint32_t)) { |
| std::memcpy(const_cast<uint8_t*>(m_nonce.value()), &nonce, sizeof(nonce)); |
| } |
| else { |
| m_nonce = dataBlock(Tlv::Nonce, |
| reinterpret_cast<const uint8_t*>(&nonce), |
| sizeof(nonce)); |
| m_wire.reset(); |
| } |
| return *this; |
| } |
| |
| bool |
| Interest::matchesName(const Name& name) const |
| { |
| if (name.size() < m_name.size()) |
| return false; |
| |
| if (!m_name.isPrefixOf(name)) |
| return false; |
| |
| if (getMinSuffixComponents() >= 0 && |
| // name must include implicit digest |
| !(name.size() - m_name.size() >= static_cast<size_t>(getMinSuffixComponents()))) |
| return false; |
| |
| if (getMaxSuffixComponents() >= 0 && |
| // name must include implicit digest |
| !(name.size() - m_name.size() <= static_cast<size_t>(getMaxSuffixComponents()))) |
| return false; |
| |
| if (!getExclude().empty() && |
| name.size() > m_name.size() && |
| getExclude().isExcluded(name[m_name.size()])) |
| return false; |
| |
| return true; |
| } |
| |
| bool |
| Interest::matchesData(const Data& data) const |
| { |
| if (!this->matchesName(data.getFullName())) { |
| return false; |
| } |
| |
| const KeyLocator& publisherPublicKeyLocator = this->getPublisherPublicKeyLocator(); |
| if (!publisherPublicKeyLocator.empty()) { |
| const Signature& signature = data.getSignature(); |
| const Block& signatureInfo = signature.getInfo(); |
| Block::element_const_iterator it = signatureInfo.find(Tlv::KeyLocator); |
| if (it == signatureInfo.elements_end()) { |
| return false; |
| } |
| if (publisherPublicKeyLocator.wireEncode() != *it) { |
| return false; |
| } |
| } |
| |
| return true; |
| } |
| |
| template<bool T> |
| size_t |
| Interest::wireEncode(EncodingImpl<T>& block) const |
| { |
| size_t totalLength = 0; |
| |
| // Interest ::= INTEREST-TYPE TLV-LENGTH |
| // Name |
| // Selectors? |
| // Nonce |
| // Scope? |
| // InterestLifetime? |
| |
| // (reverse encoding) |
| |
| // InterestLifetime |
| if (getInterestLifetime() >= time::milliseconds::zero() && |
| getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) |
| { |
| totalLength += prependNonNegativeIntegerBlock(block, |
| Tlv::InterestLifetime, |
| getInterestLifetime().count()); |
| } |
| |
| // Scope |
| if (getScope() >= 0) |
| { |
| totalLength += prependNonNegativeIntegerBlock(block, Tlv::Scope, getScope()); |
| } |
| |
| // Nonce |
| getNonce(); // to ensure that Nonce is properly set |
| totalLength += block.prependBlock(m_nonce); |
| |
| // Selectors |
| if (hasSelectors()) |
| { |
| totalLength += getSelectors().wireEncode(block); |
| } |
| |
| // Name |
| totalLength += getName().wireEncode(block); |
| |
| totalLength += block.prependVarNumber(totalLength); |
| totalLength += block.prependVarNumber(Tlv::Interest); |
| return totalLength; |
| } |
| |
| const Block& |
| Interest::wireEncode() const |
| { |
| if (m_wire.hasWire()) |
| return m_wire; |
| |
| EncodingEstimator estimator; |
| size_t estimatedSize = wireEncode(estimator); |
| |
| EncodingBuffer buffer(estimatedSize, 0); |
| wireEncode(buffer); |
| |
| // to ensure that Nonce block points to the right memory location |
| const_cast<Interest*>(this)->wireDecode(buffer.block()); |
| |
| return m_wire; |
| } |
| |
| void |
| Interest::wireDecode(const Block& wire) |
| { |
| m_wire = wire; |
| m_wire.parse(); |
| |
| // Interest ::= INTEREST-TYPE TLV-LENGTH |
| // Name |
| // Selectors? |
| // Nonce |
| // Scope? |
| // InterestLifetime? |
| |
| if (m_wire.type() != Tlv::Interest) |
| throw Tlv::Error("Unexpected TLV number when decoding Interest"); |
| |
| // Name |
| m_name.wireDecode(m_wire.get(Tlv::Name)); |
| |
| // Selectors |
| Block::element_const_iterator val = m_wire.find(Tlv::Selectors); |
| if (val != m_wire.elements_end()) |
| { |
| m_selectors.wireDecode(*val); |
| } |
| else |
| m_selectors = Selectors(); |
| |
| // Nonce |
| m_nonce = m_wire.get(Tlv::Nonce); |
| |
| // Scope |
| val = m_wire.find(Tlv::Scope); |
| if (val != m_wire.elements_end()) |
| { |
| m_scope = readNonNegativeInteger(*val); |
| } |
| else |
| m_scope = -1; |
| |
| // InterestLifetime |
| val = m_wire.find(Tlv::InterestLifetime); |
| if (val != m_wire.elements_end()) |
| { |
| m_interestLifetime = time::milliseconds(readNonNegativeInteger(*val)); |
| } |
| else |
| { |
| m_interestLifetime = DEFAULT_INTEREST_LIFETIME; |
| } |
| } |
| |
| std::ostream& |
| operator<<(std::ostream& os, const Interest& interest) |
| { |
| os << interest.getName(); |
| |
| char delim = '?'; |
| |
| if (interest.getMinSuffixComponents() >= 0) { |
| os << delim << "ndn.MinSuffixComponents=" << interest.getMinSuffixComponents(); |
| delim = '&'; |
| } |
| if (interest.getMaxSuffixComponents() >= 0) { |
| os << delim << "ndn.MaxSuffixComponents=" << interest.getMaxSuffixComponents(); |
| delim = '&'; |
| } |
| if (interest.getChildSelector() >= 0) { |
| os << delim << "ndn.ChildSelector=" << interest.getChildSelector(); |
| delim = '&'; |
| } |
| if (interest.getMustBeFresh()) { |
| os << delim << "ndn.MustBeFresh=" << interest.getMustBeFresh(); |
| delim = '&'; |
| } |
| if (interest.getScope() >= 0) { |
| os << delim << "ndn.Scope=" << interest.getScope(); |
| delim = '&'; |
| } |
| if (interest.getInterestLifetime() >= time::milliseconds::zero() |
| && interest.getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) { |
| os << delim << "ndn.InterestLifetime=" << interest.getInterestLifetime(); |
| delim = '&'; |
| } |
| |
| if (interest.hasNonce()) { |
| os << delim << "ndn.Nonce=" << interest.getNonce(); |
| delim = '&'; |
| } |
| if (!interest.getExclude().empty()) { |
| os << delim << "ndn.Exclude=" << interest.getExclude(); |
| delim = '&'; |
| } |
| |
| return os; |
| } |
| |
| } |