blob: cd3271e304e127d2bc9af6d7d8766662247ff032 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Jeff Thompson47eecfc2013-07-07 22:56:46 -07002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * Based on code originally written by Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompsonb7f95562013-07-03 18:36:42 -070022 */
23
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080024#include "common.hpp"
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080025
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080026#include "interest.hpp"
Alexander Afanasyeve9fdb802014-02-05 17:36:51 -080027#include "util/random.hpp"
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070028#include "data.hpp"
Alexander Afanasyev840139f2013-12-28 15:02:50 -080029
Jeff Thompsonb7f95562013-07-03 18:36:42 -070030namespace ndn {
Alexander Afanasyev84681982014-01-03 13:26:09 -080031
Alexander Afanasyev85480842014-01-06 14:46:54 -080032const uint32_t&
Alexander Afanasyev840139f2013-12-28 15:02:50 -080033Interest::getNonce() const
34{
Alexander Afanasyevc348f832014-02-17 16:35:17 -080035 if (m_nonce == 0)
36 m_nonce = random::generateWord32();
Alexander Afanasyev840139f2013-12-28 15:02:50 -080037
Alexander Afanasyevc348f832014-02-17 16:35:17 -080038 return m_nonce;
Alexander Afanasyev840139f2013-12-28 15:02:50 -080039}
40
41
Alexander Afanasyev84681982014-01-03 13:26:09 -080042bool
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070043Interest::matchesName(const Name& name) const
Jeff Thompson25b4e612013-10-10 16:03:24 -070044{
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070045 if (name.size() < m_name.size())
46 return false;
47
Alexander Afanasyevc348f832014-02-17 16:35:17 -080048 if (!m_name.isPrefixOf(name))
Alexander Afanasyev84681982014-01-03 13:26:09 -080049 return false;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070050
Alexander Afanasyevc348f832014-02-17 16:35:17 -080051 if (getMinSuffixComponents() >= 0 &&
Alexander Afanasyev84681982014-01-03 13:26:09 -080052 // Add 1 for the implicit digest.
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070053 !(name.size() + 1 - m_name.size() >= static_cast<size_t>(getMinSuffixComponents())))
Alexander Afanasyev84681982014-01-03 13:26:09 -080054 return false;
55
Alexander Afanasyevc348f832014-02-17 16:35:17 -080056 if (getMaxSuffixComponents() >= 0 &&
Alexander Afanasyev84681982014-01-03 13:26:09 -080057 // Add 1 for the implicit digest.
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070058 !(name.size() + 1 - m_name.size() <= static_cast<size_t>(getMaxSuffixComponents())))
Alexander Afanasyev84681982014-01-03 13:26:09 -080059 return false;
60
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070061 if (!getExclude().empty() &&
62 name.size() > m_name.size() &&
Alexander Afanasyevc348f832014-02-17 16:35:17 -080063 getExclude().isExcluded(name[m_name.size()]))
Alexander Afanasyev84681982014-01-03 13:26:09 -080064 return false;
65
66 return true;
Jeff Thompson25b4e612013-10-10 16:03:24 -070067}
68
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070069bool
70Interest::matchesData(const Data& data) const
71{
72 if (!this->matchesName(data.getName())) {
73 return false;
74 }
75
76 const KeyLocator& publisherPublicKeyLocator = this->getPublisherPublicKeyLocator();
77 if (!publisherPublicKeyLocator.empty()) {
78 const Signature& signature = data.getSignature();
79 const Block& signatureInfo = signature.getInfo();
80 Block::element_const_iterator it = signatureInfo.find(Tlv::KeyLocator);
81 if (it == signatureInfo.elements_end()) {
82 return false;
83 }
84 if (publisherPublicKeyLocator.wireEncode() != *it) {
85 return false;
86 }
87 }
88
89 return true;
90}
91
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070092std::ostream&
93operator<<(std::ostream& os, const Interest& interest)
Jeff Thompsonfe556862013-07-09 13:52:55 -070094{
Alexander Afanasyev84681982014-01-03 13:26:09 -080095 os << interest.getName();
Jeff Thompsonfe556862013-07-09 13:52:55 -070096
Alexander Afanasyev84681982014-01-03 13:26:09 -080097 char delim = '?';
98
99 if (interest.getMinSuffixComponents() >= 0) {
100 os << delim << "ndn.MinSuffixComponents=" << interest.getMinSuffixComponents();
101 delim = '&';
Jeff Thompsonfe556862013-07-09 13:52:55 -0700102 }
Alexander Afanasyev84681982014-01-03 13:26:09 -0800103 if (interest.getMaxSuffixComponents() >= 0) {
104 os << delim << "ndn.MaxSuffixComponents=" << interest.getMaxSuffixComponents();
105 delim = '&';
Jeff Thompson37527d62013-08-21 11:15:54 -0700106 }
Alexander Afanasyev84681982014-01-03 13:26:09 -0800107 if (interest.getChildSelector() >= 0) {
108 os << delim << "ndn.ChildSelector=" << interest.getChildSelector();
109 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -0800110 }
Alexander Afanasyev84681982014-01-03 13:26:09 -0800111 if (interest.getMustBeFresh()) {
112 os << delim << "ndn.MustBeFresh=" << interest.getMustBeFresh();
113 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -0800114 }
Alexander Afanasyev84681982014-01-03 13:26:09 -0800115 if (interest.getScope() >= 0) {
116 os << delim << "ndn.Scope=" << interest.getScope();
117 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -0800118 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700119 if (interest.getInterestLifetime() >= time::milliseconds::zero()
120 && interest.getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) {
Alexander Afanasyev84681982014-01-03 13:26:09 -0800121 os << delim << "ndn.InterestLifetime=" << interest.getInterestLifetime();
122 delim = '&';
123 }
124
125 if (interest.getNonce() > 0) {
126 os << delim << "ndn.Nonce=" << interest.getNonce();
127 delim = '&';
128 }
129 if (!interest.getExclude().empty()) {
130 os << delim << "ndn.Exclude=" << interest.getExclude();
131 delim = '&';
132 }
133
134 return os;
Jeff Thompson13e280b2013-12-03 13:12:23 -0800135}
136
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700137}