blob: 9e5b129efa1d20d9281799b5378e18e774181283 [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 Afanasyeve881e932014-06-08 14:47:03 +030032uint32_t
Alexander Afanasyev840139f2013-12-28 15:02:50 -080033Interest::getNonce() const
34{
Alexander Afanasyeve881e932014-06-08 14:47:03 +030035 if (!m_nonce.hasWire())
36 const_cast<Interest*>(this)->setNonce(random::generateWord32());
Alexander Afanasyev840139f2013-12-28 15:02:50 -080037
Alexander Afanasyeve881e932014-06-08 14:47:03 +030038 if (m_nonce.value_size() == sizeof(uint32_t))
39 return *reinterpret_cast<const uint32_t*>(m_nonce.value());
40 else {
41 // for compatibility reasons. Should be removed eventually
42 return readNonNegativeInteger(m_nonce);
43 }
Alexander Afanasyev840139f2013-12-28 15:02:50 -080044}
45
Alexander Afanasyeve881e932014-06-08 14:47:03 +030046Interest&
47Interest::setNonce(uint32_t nonce)
48{
49 if (m_wire.hasWire() && m_nonce.value_size() == sizeof(uint32_t)) {
50 std::memcpy(const_cast<uint8_t*>(m_nonce.value()), &nonce, sizeof(nonce));
51 }
52 else {
53 m_nonce = dataBlock(Tlv::Nonce,
54 reinterpret_cast<const uint8_t*>(&nonce),
55 sizeof(nonce));
56 m_wire.reset();
57 }
58 return *this;
59}
Alexander Afanasyev840139f2013-12-28 15:02:50 -080060
Alexander Afanasyev84681982014-01-03 13:26:09 -080061bool
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070062Interest::matchesName(const Name& name) const
Jeff Thompson25b4e612013-10-10 16:03:24 -070063{
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070064 if (name.size() < m_name.size())
65 return false;
66
Alexander Afanasyevc348f832014-02-17 16:35:17 -080067 if (!m_name.isPrefixOf(name))
Alexander Afanasyev84681982014-01-03 13:26:09 -080068 return false;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070069
Alexander Afanasyevc348f832014-02-17 16:35:17 -080070 if (getMinSuffixComponents() >= 0 &&
Alexander Afanasyev84681982014-01-03 13:26:09 -080071 // Add 1 for the implicit digest.
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070072 !(name.size() + 1 - m_name.size() >= static_cast<size_t>(getMinSuffixComponents())))
Alexander Afanasyev84681982014-01-03 13:26:09 -080073 return false;
74
Alexander Afanasyevc348f832014-02-17 16:35:17 -080075 if (getMaxSuffixComponents() >= 0 &&
Alexander Afanasyev84681982014-01-03 13:26:09 -080076 // Add 1 for the implicit digest.
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070077 !(name.size() + 1 - m_name.size() <= static_cast<size_t>(getMaxSuffixComponents())))
Alexander Afanasyev84681982014-01-03 13:26:09 -080078 return false;
79
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070080 if (!getExclude().empty() &&
81 name.size() > m_name.size() &&
Alexander Afanasyevc348f832014-02-17 16:35:17 -080082 getExclude().isExcluded(name[m_name.size()]))
Alexander Afanasyev84681982014-01-03 13:26:09 -080083 return false;
84
85 return true;
Jeff Thompson25b4e612013-10-10 16:03:24 -070086}
87
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070088bool
89Interest::matchesData(const Data& data) const
90{
91 if (!this->matchesName(data.getName())) {
92 return false;
93 }
94
95 const KeyLocator& publisherPublicKeyLocator = this->getPublisherPublicKeyLocator();
96 if (!publisherPublicKeyLocator.empty()) {
97 const Signature& signature = data.getSignature();
98 const Block& signatureInfo = signature.getInfo();
99 Block::element_const_iterator it = signatureInfo.find(Tlv::KeyLocator);
100 if (it == signatureInfo.elements_end()) {
101 return false;
102 }
103 if (publisherPublicKeyLocator.wireEncode() != *it) {
104 return false;
105 }
106 }
107
108 return true;
109}
110
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700111std::ostream&
112operator<<(std::ostream& os, const Interest& interest)
Jeff Thompsonfe556862013-07-09 13:52:55 -0700113{
Alexander Afanasyev84681982014-01-03 13:26:09 -0800114 os << interest.getName();
Jeff Thompsonfe556862013-07-09 13:52:55 -0700115
Alexander Afanasyev84681982014-01-03 13:26:09 -0800116 char delim = '?';
117
118 if (interest.getMinSuffixComponents() >= 0) {
119 os << delim << "ndn.MinSuffixComponents=" << interest.getMinSuffixComponents();
120 delim = '&';
Jeff Thompsonfe556862013-07-09 13:52:55 -0700121 }
Alexander Afanasyev84681982014-01-03 13:26:09 -0800122 if (interest.getMaxSuffixComponents() >= 0) {
123 os << delim << "ndn.MaxSuffixComponents=" << interest.getMaxSuffixComponents();
124 delim = '&';
Jeff Thompson37527d62013-08-21 11:15:54 -0700125 }
Alexander Afanasyev84681982014-01-03 13:26:09 -0800126 if (interest.getChildSelector() >= 0) {
127 os << delim << "ndn.ChildSelector=" << interest.getChildSelector();
128 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -0800129 }
Alexander Afanasyev84681982014-01-03 13:26:09 -0800130 if (interest.getMustBeFresh()) {
131 os << delim << "ndn.MustBeFresh=" << interest.getMustBeFresh();
132 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -0800133 }
Alexander Afanasyev84681982014-01-03 13:26:09 -0800134 if (interest.getScope() >= 0) {
135 os << delim << "ndn.Scope=" << interest.getScope();
136 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -0800137 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700138 if (interest.getInterestLifetime() >= time::milliseconds::zero()
139 && interest.getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) {
Alexander Afanasyev84681982014-01-03 13:26:09 -0800140 os << delim << "ndn.InterestLifetime=" << interest.getInterestLifetime();
141 delim = '&';
142 }
143
Alexander Afanasyeve881e932014-06-08 14:47:03 +0300144 if (interest.hasNonce()) {
Alexander Afanasyev84681982014-01-03 13:26:09 -0800145 os << delim << "ndn.Nonce=" << interest.getNonce();
146 delim = '&';
147 }
148 if (!interest.getExclude().empty()) {
149 os << delim << "ndn.Exclude=" << interest.getExclude();
150 delim = '&';
151 }
152
153 return os;
Jeff Thompson13e280b2013-12-03 13:12:23 -0800154}
155
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700156}