blob: 314bdcac8a9819d6bab8f1b2d44d9c60a47c1ef7 [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompson47eecfc2013-07-07 22:56:46 -07002/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07003 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson47eecfc2013-07-07 22:56:46 -07005 * See COPYING for copyright and distribution information.
Jeff Thompsonb7f95562013-07-03 18:36:42 -07006 */
7
Alexander Afanasyev09c613f2014-01-29 00:23:58 -08008#include "common.hpp"
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -08009
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080010#include "interest.hpp"
Alexander Afanasyeve9fdb802014-02-05 17:36:51 -080011#include "util/random.hpp"
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070012#include "data.hpp"
Alexander Afanasyev840139f2013-12-28 15:02:50 -080013
Jeff Thompsonb7f95562013-07-03 18:36:42 -070014using namespace std;
15
16namespace ndn {
Alexander Afanasyev84681982014-01-03 13:26:09 -080017
Alexander Afanasyev85480842014-01-06 14:46:54 -080018const uint32_t&
Alexander Afanasyev840139f2013-12-28 15:02:50 -080019Interest::getNonce() const
20{
Alexander Afanasyevc348f832014-02-17 16:35:17 -080021 if (m_nonce == 0)
22 m_nonce = random::generateWord32();
Alexander Afanasyev840139f2013-12-28 15:02:50 -080023
Alexander Afanasyevc348f832014-02-17 16:35:17 -080024 return m_nonce;
Alexander Afanasyev840139f2013-12-28 15:02:50 -080025}
26
27
Alexander Afanasyev84681982014-01-03 13:26:09 -080028bool
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070029Interest::matchesName(const Name& name) const
Jeff Thompson25b4e612013-10-10 16:03:24 -070030{
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070031 if (name.size() < m_name.size())
32 return false;
33
Alexander Afanasyevc348f832014-02-17 16:35:17 -080034 if (!m_name.isPrefixOf(name))
Alexander Afanasyev84681982014-01-03 13:26:09 -080035 return false;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070036
Alexander Afanasyevc348f832014-02-17 16:35:17 -080037 if (getMinSuffixComponents() >= 0 &&
Alexander Afanasyev84681982014-01-03 13:26:09 -080038 // Add 1 for the implicit digest.
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070039 !(name.size() + 1 - m_name.size() >= static_cast<size_t>(getMinSuffixComponents())))
Alexander Afanasyev84681982014-01-03 13:26:09 -080040 return false;
41
Alexander Afanasyevc348f832014-02-17 16:35:17 -080042 if (getMaxSuffixComponents() >= 0 &&
Alexander Afanasyev84681982014-01-03 13:26:09 -080043 // Add 1 for the implicit digest.
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070044 !(name.size() + 1 - m_name.size() <= static_cast<size_t>(getMaxSuffixComponents())))
Alexander Afanasyev84681982014-01-03 13:26:09 -080045 return false;
46
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070047 if (!getExclude().empty() &&
48 name.size() > m_name.size() &&
Alexander Afanasyevc348f832014-02-17 16:35:17 -080049 getExclude().isExcluded(name[m_name.size()]))
Alexander Afanasyev84681982014-01-03 13:26:09 -080050 return false;
51
52 return true;
Jeff Thompson25b4e612013-10-10 16:03:24 -070053}
54
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070055bool
56Interest::matchesData(const Data& data) const
57{
58 if (!this->matchesName(data.getName())) {
59 return false;
60 }
61
62 const KeyLocator& publisherPublicKeyLocator = this->getPublisherPublicKeyLocator();
63 if (!publisherPublicKeyLocator.empty()) {
64 const Signature& signature = data.getSignature();
65 const Block& signatureInfo = signature.getInfo();
66 Block::element_const_iterator it = signatureInfo.find(Tlv::KeyLocator);
67 if (it == signatureInfo.elements_end()) {
68 return false;
69 }
70 if (publisherPublicKeyLocator.wireEncode() != *it) {
71 return false;
72 }
73 }
74
75 return true;
76}
77
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070078std::ostream&
79operator<<(std::ostream& os, const Interest& interest)
Jeff Thompsonfe556862013-07-09 13:52:55 -070080{
Alexander Afanasyev84681982014-01-03 13:26:09 -080081 os << interest.getName();
Jeff Thompsonfe556862013-07-09 13:52:55 -070082
Alexander Afanasyev84681982014-01-03 13:26:09 -080083 char delim = '?';
84
85 if (interest.getMinSuffixComponents() >= 0) {
86 os << delim << "ndn.MinSuffixComponents=" << interest.getMinSuffixComponents();
87 delim = '&';
Jeff Thompsonfe556862013-07-09 13:52:55 -070088 }
Alexander Afanasyev84681982014-01-03 13:26:09 -080089 if (interest.getMaxSuffixComponents() >= 0) {
90 os << delim << "ndn.MaxSuffixComponents=" << interest.getMaxSuffixComponents();
91 delim = '&';
Jeff Thompson37527d62013-08-21 11:15:54 -070092 }
Alexander Afanasyev84681982014-01-03 13:26:09 -080093 if (interest.getChildSelector() >= 0) {
94 os << delim << "ndn.ChildSelector=" << interest.getChildSelector();
95 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -080096 }
Alexander Afanasyev84681982014-01-03 13:26:09 -080097 if (interest.getMustBeFresh()) {
98 os << delim << "ndn.MustBeFresh=" << interest.getMustBeFresh();
99 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -0800100 }
Alexander Afanasyev84681982014-01-03 13:26:09 -0800101 if (interest.getScope() >= 0) {
102 os << delim << "ndn.Scope=" << interest.getScope();
103 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -0800104 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700105 if (interest.getInterestLifetime() >= time::milliseconds::zero()
106 && interest.getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) {
Alexander Afanasyev84681982014-01-03 13:26:09 -0800107 os << delim << "ndn.InterestLifetime=" << interest.getInterestLifetime();
108 delim = '&';
109 }
110
111 if (interest.getNonce() > 0) {
112 os << delim << "ndn.Nonce=" << interest.getNonce();
113 delim = '&';
114 }
115 if (!interest.getExclude().empty()) {
116 os << delim << "ndn.Exclude=" << interest.getExclude();
117 delim = '&';
118 }
119
120 return os;
Jeff Thompson13e280b2013-12-03 13:12:23 -0800121}
122
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700123}