blob: 64dc8171439b8923837a0b7e9c61703f32e380a4 [file] [log] [blame]
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
/**
* Copyright (C) 2013 Regents of the University of California.
* @author: Jeff Thompson <jefft0@remap.ucla.edu>
* See COPYING for copyright and distribution information.
*/
#include "common.hpp"
#include "interest.hpp"
#include "util/random.hpp"
#include "data.hpp"
namespace ndn {
const uint32_t&
Interest::getNonce() const
{
if (m_nonce == 0)
m_nonce = random::generateWord32();
return m_nonce;
}
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 &&
// Add 1 for the implicit digest.
!(name.size() + 1 - m_name.size() >= static_cast<size_t>(getMinSuffixComponents())))
return false;
if (getMaxSuffixComponents() >= 0 &&
// Add 1 for the implicit digest.
!(name.size() + 1 - 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.getName())) {
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;
}
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.getNonce() > 0) {
os << delim << "ndn.Nonce=" << interest.getNonce();
delim = '&';
}
if (!interest.getExclude().empty()) {
os << delim << "ndn.Exclude=" << interest.getExclude();
delim = '&';
}
return os;
}
}