blob: 22df4a48ab2c54c10ce06d027eb2bcd7bcf176ae [file] [log] [blame]
Junxiao Shicbba04c2014-01-26 14:21:22 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shicde37ad2015-12-24 01:02:05 -07003 * Copyright (c) 2014-2015, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Alexander Afanasyev28d586a2014-07-10 20:10:54 -070024 */
Junxiao Shicbba04c2014-01-26 14:21:22 -070025
26#include "pit-entry.hpp"
27#include <algorithm>
28
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080029namespace nfd {
Junxiao Shicbba04c2014-01-26 14:21:22 -070030namespace pit {
31
Junxiao Shi57f0f312014-03-16 11:52:20 -070032const Name Entry::LOCALHOST_NAME("ndn:/localhost");
33const Name Entry::LOCALHOP_NAME("ndn:/localhop");
34
Junxiao Shicbba04c2014-01-26 14:21:22 -070035Entry::Entry(const Interest& interest)
Alexander Afanasyev28d586a2014-07-10 20:10:54 -070036 : m_interest(interest.shared_from_this())
Junxiao Shicbba04c2014-01-26 14:21:22 -070037{
38}
39
40const Name&
41Entry::getName() const
42{
Alexander Afanasyev28d586a2014-07-10 20:10:54 -070043 return m_interest->getName();
Junxiao Shicbba04c2014-01-26 14:21:22 -070044}
45
Junxiao Shi11bd9c22014-03-13 20:44:13 -070046bool
47Entry::hasLocalInRecord() const
48{
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070049 return std::any_of(m_inRecords.begin(), m_inRecords.end(),
Junxiao Shicde37ad2015-12-24 01:02:05 -070050 [] (const InRecord& inRecord) { return inRecord.getFace()->getScope() == ndn::nfd::FACE_SCOPE_LOCAL; });
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070051}
52
53bool
Junxiao Shi57f0f312014-03-16 11:52:20 -070054Entry::canForwardTo(const Face& face) const
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070055{
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070056 time::steady_clock::TimePoint now = time::steady_clock::now();
57
58 bool hasUnexpiredOutRecord = std::any_of(m_outRecords.begin(), m_outRecords.end(),
59 [&face, &now] (const OutRecord& outRecord) {
60 return outRecord.getFace().get() == &face && outRecord.getExpiry() >= now;
61 });
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070062 if (hasUnexpiredOutRecord) {
63 return false;
64 }
Junxiao Shi11bd9c22014-03-13 20:44:13 -070065
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070066 bool hasUnexpiredOtherInRecord = std::any_of(m_inRecords.begin(), m_inRecords.end(),
67 [&face, &now] (const InRecord& inRecord) {
68 return inRecord.getFace().get() != &face && inRecord.getExpiry() >= now;
69 });
Junxiao Shi57f0f312014-03-16 11:52:20 -070070 if (!hasUnexpiredOtherInRecord) {
71 return false;
72 }
73
74 return !this->violatesScope(face);
75}
76
77bool
78Entry::violatesScope(const Face& face) const
79{
80 // /localhost scope
Junxiao Shicde37ad2015-12-24 01:02:05 -070081 bool isViolatingLocalhost = face.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shi57f0f312014-03-16 11:52:20 -070082 LOCALHOST_NAME.isPrefixOf(this->getName());
83 if (isViolatingLocalhost) {
84 return true;
85 }
86
87 // /localhop scope
Junxiao Shicde37ad2015-12-24 01:02:05 -070088 bool isViolatingLocalhop = face.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL &&
Junxiao Shi57f0f312014-03-16 11:52:20 -070089 LOCALHOP_NAME.isPrefixOf(this->getName()) &&
90 !this->hasLocalInRecord();
91 if (isViolatingLocalhop) {
92 return true;
93 }
94
95 return false;
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070096}
97
Junxiao Shia110f262014-10-12 12:35:20 -070098int
99Entry::findNonce(uint32_t nonce, const Face& face) const
Junxiao Shicbba04c2014-01-26 14:21:22 -0700100{
Junxiao Shia110f262014-10-12 12:35:20 -0700101 // TODO should we ignore expired in/out records?
102
103 int dnw = DUPLICATE_NONCE_NONE;
104
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700105 for (const InRecord& inRecord : m_inRecords) {
106 if (inRecord.getLastNonce() == nonce) {
107 if (inRecord.getFace().get() == &face) {
Junxiao Shia110f262014-10-12 12:35:20 -0700108 dnw |= DUPLICATE_NONCE_IN_SAME;
109 }
110 else {
111 dnw |= DUPLICATE_NONCE_IN_OTHER;
112 }
113 }
114 }
115
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700116 for (const OutRecord& outRecord : m_outRecords) {
117 if (outRecord.getLastNonce() == nonce) {
118 if (outRecord.getFace().get() == &face) {
Junxiao Shia110f262014-10-12 12:35:20 -0700119 dnw |= DUPLICATE_NONCE_OUT_SAME;
120 }
121 else {
122 dnw |= DUPLICATE_NONCE_OUT_OTHER;
123 }
124 }
125 }
126
127 return dnw;
Junxiao Shicbba04c2014-01-26 14:21:22 -0700128}
129
Junxiao Shicbba04c2014-01-26 14:21:22 -0700130InRecordCollection::iterator
131Entry::insertOrUpdateInRecord(shared_ptr<Face> face, const Interest& interest)
132{
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700133 auto it = std::find_if(m_inRecords.begin(), m_inRecords.end(),
134 [&face] (const InRecord& inRecord) { return inRecord.getFace() == face; });
Junxiao Shicbba04c2014-01-26 14:21:22 -0700135 if (it == m_inRecords.end()) {
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700136 m_inRecords.emplace_front(face);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700137 it = m_inRecords.begin();
138 }
Junxiao Shi11bd9c22014-03-13 20:44:13 -0700139
Junxiao Shicbba04c2014-01-26 14:21:22 -0700140 it->update(interest);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700141 return it;
142}
143
Junxiao Shi66f91f82014-05-10 17:28:58 -0700144InRecordCollection::const_iterator
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700145Entry::getInRecord(const Face& face) const
Junxiao Shi66f91f82014-05-10 17:28:58 -0700146{
147 return std::find_if(m_inRecords.begin(), m_inRecords.end(),
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700148 [&face] (const InRecord& inRecord) { return inRecord.getFace().get() == &face; });
Junxiao Shi66f91f82014-05-10 17:28:58 -0700149}
150
Junxiao Shicbba04c2014-01-26 14:21:22 -0700151void
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700152Entry::deleteInRecord(const Face& face)
153{
154 auto it = std::find_if(m_inRecords.begin(), m_inRecords.end(),
155 [&face] (const InRecord& inRecord) { return inRecord.getFace().get() == &face; });
156 if (it != m_inRecords.end()) {
157 m_inRecords.erase(it);
158 }
159}
160
161void
Junxiao Shicbba04c2014-01-26 14:21:22 -0700162Entry::deleteInRecords()
163{
164 m_inRecords.clear();
165}
166
167OutRecordCollection::iterator
168Entry::insertOrUpdateOutRecord(shared_ptr<Face> face, const Interest& interest)
169{
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700170 auto it = std::find_if(m_outRecords.begin(), m_outRecords.end(),
171 [&face] (const OutRecord& outRecord) { return outRecord.getFace() == face; });
Junxiao Shicbba04c2014-01-26 14:21:22 -0700172 if (it == m_outRecords.end()) {
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700173 m_outRecords.emplace_front(face);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700174 it = m_outRecords.begin();
175 }
Junxiao Shi11bd9c22014-03-13 20:44:13 -0700176
Junxiao Shicbba04c2014-01-26 14:21:22 -0700177 it->update(interest);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700178 return it;
179}
180
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700181OutRecordCollection::iterator
182Entry::getOutRecord(const Face& face)
Junxiao Shi66f91f82014-05-10 17:28:58 -0700183{
184 return std::find_if(m_outRecords.begin(), m_outRecords.end(),
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700185 [&face] (const OutRecord& outRecord) { return outRecord.getFace().get() == &face; });
Junxiao Shi66f91f82014-05-10 17:28:58 -0700186}
187
Junxiao Shicbba04c2014-01-26 14:21:22 -0700188void
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700189Entry::deleteOutRecord(const Face& face)
Junxiao Shicbba04c2014-01-26 14:21:22 -0700190{
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700191 auto it = std::find_if(m_outRecords.begin(), m_outRecords.end(),
192 [&face] (const OutRecord& outRecord) { return outRecord.getFace().get() == &face; });
Junxiao Shicbba04c2014-01-26 14:21:22 -0700193 if (it != m_outRecords.end()) {
194 m_outRecords.erase(it);
195 }
196}
197
Junxiao Shi57f0f312014-03-16 11:52:20 -0700198bool
199Entry::hasUnexpiredOutRecords() const
200{
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700201 time::steady_clock::TimePoint now = time::steady_clock::now();
202
203 return std::any_of(m_outRecords.begin(), m_outRecords.end(),
204 [&now] (const OutRecord& outRecord) { return outRecord.getExpiry() >= now; });
Junxiao Shi57f0f312014-03-16 11:52:20 -0700205}
Junxiao Shicbba04c2014-01-26 14:21:22 -0700206
207} // namespace pit
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800208} // namespace nfd