blob: e19c553c501a02b89b258e2e9dad9639ed34bb49 [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/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
11 *
12 * Based on code originally written by Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompsonb7f95562013-07-03 18:36:42 -070013 */
14
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080015#include "common.hpp"
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080016
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080017#include "interest.hpp"
Alexander Afanasyeve9fdb802014-02-05 17:36:51 -080018#include "util/random.hpp"
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070019#include "data.hpp"
Alexander Afanasyev840139f2013-12-28 15:02:50 -080020
Jeff Thompsonb7f95562013-07-03 18:36:42 -070021namespace ndn {
Alexander Afanasyev84681982014-01-03 13:26:09 -080022
Alexander Afanasyev85480842014-01-06 14:46:54 -080023const uint32_t&
Alexander Afanasyev840139f2013-12-28 15:02:50 -080024Interest::getNonce() const
25{
Alexander Afanasyevc348f832014-02-17 16:35:17 -080026 if (m_nonce == 0)
27 m_nonce = random::generateWord32();
Alexander Afanasyev840139f2013-12-28 15:02:50 -080028
Alexander Afanasyevc348f832014-02-17 16:35:17 -080029 return m_nonce;
Alexander Afanasyev840139f2013-12-28 15:02:50 -080030}
31
32
Alexander Afanasyev84681982014-01-03 13:26:09 -080033bool
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070034Interest::matchesName(const Name& name) const
Jeff Thompson25b4e612013-10-10 16:03:24 -070035{
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070036 if (name.size() < m_name.size())
37 return false;
38
Alexander Afanasyevc348f832014-02-17 16:35:17 -080039 if (!m_name.isPrefixOf(name))
Alexander Afanasyev84681982014-01-03 13:26:09 -080040 return false;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070041
Alexander Afanasyevc348f832014-02-17 16:35:17 -080042 if (getMinSuffixComponents() >= 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>(getMinSuffixComponents())))
Alexander Afanasyev84681982014-01-03 13:26:09 -080045 return false;
46
Alexander Afanasyevc348f832014-02-17 16:35:17 -080047 if (getMaxSuffixComponents() >= 0 &&
Alexander Afanasyev84681982014-01-03 13:26:09 -080048 // Add 1 for the implicit digest.
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070049 !(name.size() + 1 - m_name.size() <= static_cast<size_t>(getMaxSuffixComponents())))
Alexander Afanasyev84681982014-01-03 13:26:09 -080050 return false;
51
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070052 if (!getExclude().empty() &&
53 name.size() > m_name.size() &&
Alexander Afanasyevc348f832014-02-17 16:35:17 -080054 getExclude().isExcluded(name[m_name.size()]))
Alexander Afanasyev84681982014-01-03 13:26:09 -080055 return false;
56
57 return true;
Jeff Thompson25b4e612013-10-10 16:03:24 -070058}
59
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070060bool
61Interest::matchesData(const Data& data) const
62{
63 if (!this->matchesName(data.getName())) {
64 return false;
65 }
66
67 const KeyLocator& publisherPublicKeyLocator = this->getPublisherPublicKeyLocator();
68 if (!publisherPublicKeyLocator.empty()) {
69 const Signature& signature = data.getSignature();
70 const Block& signatureInfo = signature.getInfo();
71 Block::element_const_iterator it = signatureInfo.find(Tlv::KeyLocator);
72 if (it == signatureInfo.elements_end()) {
73 return false;
74 }
75 if (publisherPublicKeyLocator.wireEncode() != *it) {
76 return false;
77 }
78 }
79
80 return true;
81}
82
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070083std::ostream&
84operator<<(std::ostream& os, const Interest& interest)
Jeff Thompsonfe556862013-07-09 13:52:55 -070085{
Alexander Afanasyev84681982014-01-03 13:26:09 -080086 os << interest.getName();
Jeff Thompsonfe556862013-07-09 13:52:55 -070087
Alexander Afanasyev84681982014-01-03 13:26:09 -080088 char delim = '?';
89
90 if (interest.getMinSuffixComponents() >= 0) {
91 os << delim << "ndn.MinSuffixComponents=" << interest.getMinSuffixComponents();
92 delim = '&';
Jeff Thompsonfe556862013-07-09 13:52:55 -070093 }
Alexander Afanasyev84681982014-01-03 13:26:09 -080094 if (interest.getMaxSuffixComponents() >= 0) {
95 os << delim << "ndn.MaxSuffixComponents=" << interest.getMaxSuffixComponents();
96 delim = '&';
Jeff Thompson37527d62013-08-21 11:15:54 -070097 }
Alexander Afanasyev84681982014-01-03 13:26:09 -080098 if (interest.getChildSelector() >= 0) {
99 os << delim << "ndn.ChildSelector=" << interest.getChildSelector();
100 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -0800101 }
Alexander Afanasyev84681982014-01-03 13:26:09 -0800102 if (interest.getMustBeFresh()) {
103 os << delim << "ndn.MustBeFresh=" << interest.getMustBeFresh();
104 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -0800105 }
Alexander Afanasyev84681982014-01-03 13:26:09 -0800106 if (interest.getScope() >= 0) {
107 os << delim << "ndn.Scope=" << interest.getScope();
108 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -0800109 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700110 if (interest.getInterestLifetime() >= time::milliseconds::zero()
111 && interest.getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) {
Alexander Afanasyev84681982014-01-03 13:26:09 -0800112 os << delim << "ndn.InterestLifetime=" << interest.getInterestLifetime();
113 delim = '&';
114 }
115
116 if (interest.getNonce() > 0) {
117 os << delim << "ndn.Nonce=" << interest.getNonce();
118 delim = '&';
119 }
120 if (!interest.getExclude().empty()) {
121 os << delim << "ndn.Exclude=" << interest.getExclude();
122 delim = '&';
123 }
124
125 return os;
Jeff Thompson13e280b2013-12-03 13:12:23 -0800126}
127
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700128}