blob: da7c6eda03a60f0bb97baf3c8ce554abab522b22 [file] [log] [blame]
Junxiao Shicbba04c2014-01-26 14:21:22 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev28d586a2014-07-10 20:10:54 -07003 * Copyright (c) 2014, 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(),
50 [] (const InRecord& inRecord) { return inRecord.getFace()->isLocal(); });
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
81 bool isViolatingLocalhost = !face.isLocal() &&
82 LOCALHOST_NAME.isPrefixOf(this->getName());
83 if (isViolatingLocalhost) {
84 return true;
85 }
86
87 // /localhop scope
88 bool isViolatingLocalhop = !face.isLocal() &&
89 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
152Entry::deleteInRecords()
153{
154 m_inRecords.clear();
155}
156
157OutRecordCollection::iterator
158Entry::insertOrUpdateOutRecord(shared_ptr<Face> face, const Interest& interest)
159{
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700160 auto it = std::find_if(m_outRecords.begin(), m_outRecords.end(),
161 [&face] (const OutRecord& outRecord) { return outRecord.getFace() == face; });
Junxiao Shicbba04c2014-01-26 14:21:22 -0700162 if (it == m_outRecords.end()) {
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700163 m_outRecords.emplace_front(face);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700164 it = m_outRecords.begin();
165 }
Junxiao Shi11bd9c22014-03-13 20:44:13 -0700166
Junxiao Shicbba04c2014-01-26 14:21:22 -0700167 it->update(interest);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700168 return it;
169}
170
Junxiao Shi66f91f82014-05-10 17:28:58 -0700171OutRecordCollection::const_iterator
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700172Entry::getOutRecord(const Face& face) const
Junxiao Shi66f91f82014-05-10 17:28:58 -0700173{
174 return std::find_if(m_outRecords.begin(), m_outRecords.end(),
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700175 [&face] (const OutRecord& outRecord) { return outRecord.getFace().get() == &face; });
Junxiao Shi66f91f82014-05-10 17:28:58 -0700176}
177
Junxiao Shicbba04c2014-01-26 14:21:22 -0700178void
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700179Entry::deleteOutRecord(const Face& face)
Junxiao Shicbba04c2014-01-26 14:21:22 -0700180{
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700181 auto it = std::find_if(m_outRecords.begin(), m_outRecords.end(),
182 [&face] (const OutRecord& outRecord) { return outRecord.getFace().get() == &face; });
Junxiao Shicbba04c2014-01-26 14:21:22 -0700183 if (it != m_outRecords.end()) {
184 m_outRecords.erase(it);
185 }
186}
187
Junxiao Shi57f0f312014-03-16 11:52:20 -0700188bool
189Entry::hasUnexpiredOutRecords() const
190{
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700191 time::steady_clock::TimePoint now = time::steady_clock::now();
192
193 return std::any_of(m_outRecords.begin(), m_outRecords.end(),
194 [&now] (const OutRecord& outRecord) { return outRecord.getExpiry() >= now; });
Junxiao Shi57f0f312014-03-16 11:52:20 -0700195}
Junxiao Shicbba04c2014-01-26 14:21:22 -0700196
197} // namespace pit
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800198} // namespace nfd