blob: ca54dfeae99d6b86bfaf992326768fff1ccecef8 [file] [log] [blame]
Junxiao Shifef73e42016-03-29 14:15:05 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2016, 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.
10 *
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/>.
24 */
25
26#include "pit-algorithm.hpp"
27
28namespace nfd {
29namespace fw {
30
31namespace scope_prefix {
32const Name LOCALHOST("ndn:/localhost");
33const Name LOCALHOP("ndn:/localhop");
34} // namespace scope_prefix
35
36bool
37violatesScope(const pit::Entry& pitEntry, const Face& outFace)
38{
39 if (outFace.getScope() == ndn::nfd::FACE_SCOPE_LOCAL) {
40 return false;
41 }
42 BOOST_ASSERT(outFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL);
43
44 if (scope_prefix::LOCALHOST.isPrefixOf(pitEntry.getName())) {
45 // face is non-local, violates localhost scope
46 return true;
47 }
48
49 if (scope_prefix::LOCALHOP.isPrefixOf(pitEntry.getName())) {
50 // face is non-local, violates localhop scope unless PIT entry has local in-record
Junxiao Shi4846f372016-04-05 13:39:30 -070051 return std::none_of(pitEntry.in_begin(), pitEntry.in_end(),
Junxiao Shifef73e42016-03-29 14:15:05 -070052 [] (const pit::InRecord& inRecord) { return inRecord.getFace()->getScope() == ndn::nfd::FACE_SCOPE_LOCAL; });
53 }
54
55 // Name is not subject to scope control
56 return false;
57}
58
59bool
60canForwardToLegacy(const pit::Entry& pitEntry, const Face& face)
61{
62 time::steady_clock::TimePoint now = time::steady_clock::now();
63
Junxiao Shi4846f372016-04-05 13:39:30 -070064 bool hasUnexpiredOutRecord = std::any_of(pitEntry.out_begin(), pitEntry.out_end(),
Junxiao Shifef73e42016-03-29 14:15:05 -070065 [&face, &now] (const pit::OutRecord& outRecord) {
66 return outRecord.getFace().get() == &face && outRecord.getExpiry() >= now;
67 });
68 if (hasUnexpiredOutRecord) {
69 return false;
70 }
71
Junxiao Shi4846f372016-04-05 13:39:30 -070072 bool hasUnexpiredOtherInRecord = std::any_of(pitEntry.in_begin(), pitEntry.in_end(),
Junxiao Shifef73e42016-03-29 14:15:05 -070073 [&face, &now] (const pit::InRecord& inRecord) {
74 return inRecord.getFace().get() != &face && inRecord.getExpiry() >= now;
75 });
76 if (!hasUnexpiredOtherInRecord) {
77 return false;
78 }
79
80 return !violatesScope(pitEntry, face);
81}
82
83int
84findDuplicateNonce(const pit::Entry& pitEntry, uint32_t nonce, const Face& face)
85{
86 int dnw = DUPLICATE_NONCE_NONE;
87
88 for (const pit::InRecord& inRecord : pitEntry.getInRecords()) {
89 if (inRecord.getLastNonce() == nonce) {
90 if (inRecord.getFace().get() == &face) {
91 dnw |= DUPLICATE_NONCE_IN_SAME;
92 }
93 else {
94 dnw |= DUPLICATE_NONCE_IN_OTHER;
95 }
96 }
97 }
98
99 for (const pit::OutRecord& outRecord : pitEntry.getOutRecords()) {
100 if (outRecord.getLastNonce() == nonce) {
101 if (outRecord.getFace().get() == &face) {
102 dnw |= DUPLICATE_NONCE_OUT_SAME;
103 }
104 else {
105 dnw |= DUPLICATE_NONCE_OUT_OTHER;
106 }
107 }
108 }
109
110 return dnw;
111}
112
113bool
114hasPendingOutRecords(const pit::Entry& pitEntry)
115{
116 time::steady_clock::TimePoint now = time::steady_clock::now();
Junxiao Shi4846f372016-04-05 13:39:30 -0700117 return std::any_of(pitEntry.out_begin(), pitEntry.out_end(),
Junxiao Shifef73e42016-03-29 14:15:05 -0700118 [&now] (const pit::OutRecord& outRecord) { return outRecord.getExpiry() >= now; });
119}
120
121} // namespace fw
122} // namespace nfd