blob: 7dada21c1fe855f057697929504f59e896337ede [file] [log] [blame]
Junxiao Shifef73e42016-03-29 14:15:05 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande90015992017-07-11 17:25:48 -05002/*
Davide Pesavento51cf75c2020-03-11 22:21:13 -04003 * Copyright (c) 2014-2020, Regents of the University of California,
Junxiao Shifef73e42016-03-29 14:15:05 -07004 * 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
Junxiao Shi00dc9142016-11-21 14:23:12 +000026#include "algorithm.hpp"
Junxiao Shifef73e42016-03-29 14:15:05 -070027
28namespace nfd {
Junxiao Shif3410d82016-04-05 13:49:44 -070029namespace fw {
30
Junxiao Shifef73e42016-03-29 14:15:05 -070031bool
Junxiao Shi00dc9142016-11-21 14:23:12 +000032wouldViolateScope(const Face& inFace, const Interest& interest, const Face& outFace)
33{
34 if (outFace.getScope() == ndn::nfd::FACE_SCOPE_LOCAL) {
35 // forwarding to a local face is always allowed
36 return false;
37 }
38
39 if (scope_prefix::LOCALHOST.isPrefixOf(interest.getName())) {
40 // localhost Interests cannot be forwarded to a non-local face
41 return true;
42 }
43
44 if (scope_prefix::LOCALHOP.isPrefixOf(interest.getName())) {
45 // localhop Interests can be forwarded to a non-local face only if it comes from a local face
46 return inFace.getScope() != ndn::nfd::FACE_SCOPE_LOCAL;
47 }
48
49 // Interest name is not subject to scope control
50 return false;
51}
52
53bool
Junxiao Shifef73e42016-03-29 14:15:05 -070054canForwardToLegacy(const pit::Entry& pitEntry, const Face& face)
55{
56 time::steady_clock::TimePoint now = time::steady_clock::now();
57
Junxiao Shi4846f372016-04-05 13:39:30 -070058 bool hasUnexpiredOutRecord = std::any_of(pitEntry.out_begin(), pitEntry.out_end(),
Junxiao Shifef73e42016-03-29 14:15:05 -070059 [&face, &now] (const pit::OutRecord& outRecord) {
Junxiao Shi9cff7792016-08-01 21:45:11 +000060 return &outRecord.getFace() == &face && outRecord.getExpiry() >= now;
Junxiao Shifef73e42016-03-29 14:15:05 -070061 });
62 if (hasUnexpiredOutRecord) {
63 return false;
64 }
65
Junxiao Shi4846f372016-04-05 13:39:30 -070066 bool hasUnexpiredOtherInRecord = std::any_of(pitEntry.in_begin(), pitEntry.in_end(),
Junxiao Shifef73e42016-03-29 14:15:05 -070067 [&face, &now] (const pit::InRecord& inRecord) {
Junxiao Shi9cff7792016-08-01 21:45:11 +000068 return &inRecord.getFace() != &face && inRecord.getExpiry() >= now;
Junxiao Shifef73e42016-03-29 14:15:05 -070069 });
70 if (!hasUnexpiredOtherInRecord) {
71 return false;
72 }
73
Junxiao Shie21b3f32016-11-24 14:13:46 +000074 return true;
Junxiao Shifef73e42016-03-29 14:15:05 -070075}
76
77int
Davide Pesavento51cf75c2020-03-11 22:21:13 -040078findDuplicateNonce(const pit::Entry& pitEntry, Interest::Nonce nonce, const Face& face)
Junxiao Shifef73e42016-03-29 14:15:05 -070079{
80 int dnw = DUPLICATE_NONCE_NONE;
81
82 for (const pit::InRecord& inRecord : pitEntry.getInRecords()) {
83 if (inRecord.getLastNonce() == nonce) {
Junxiao Shi9cff7792016-08-01 21:45:11 +000084 if (&inRecord.getFace() == &face) {
Junxiao Shifef73e42016-03-29 14:15:05 -070085 dnw |= DUPLICATE_NONCE_IN_SAME;
86 }
87 else {
88 dnw |= DUPLICATE_NONCE_IN_OTHER;
89 }
90 }
91 }
92
93 for (const pit::OutRecord& outRecord : pitEntry.getOutRecords()) {
94 if (outRecord.getLastNonce() == nonce) {
Junxiao Shi9cff7792016-08-01 21:45:11 +000095 if (&outRecord.getFace() == &face) {
Junxiao Shifef73e42016-03-29 14:15:05 -070096 dnw |= DUPLICATE_NONCE_OUT_SAME;
97 }
98 else {
99 dnw |= DUPLICATE_NONCE_OUT_OTHER;
100 }
101 }
102 }
103
104 return dnw;
105}
106
107bool
108hasPendingOutRecords(const pit::Entry& pitEntry)
109{
110 time::steady_clock::TimePoint now = time::steady_clock::now();
Junxiao Shi4846f372016-04-05 13:39:30 -0700111 return std::any_of(pitEntry.out_begin(), pitEntry.out_end(),
Junxiao Shi8744c972016-04-06 10:33:46 -0700112 [&now] (const pit::OutRecord& outRecord) {
113 return outRecord.getExpiry() >= now &&
114 outRecord.getIncomingNack() == nullptr;
115 });
Junxiao Shifef73e42016-03-29 14:15:05 -0700116}
117
Ashlesh Gawande90015992017-07-11 17:25:48 -0500118time::steady_clock::TimePoint
119getLastOutgoing(const pit::Entry& pitEntry)
120{
121 pit::OutRecordCollection::const_iterator lastOutgoing = std::max_element(
122 pitEntry.out_begin(), pitEntry.out_end(),
123 [] (const pit::OutRecord& a, const pit::OutRecord& b) {
124 return a.getLastRenewed() < b.getLastRenewed();
125 });
126 BOOST_ASSERT(lastOutgoing != pitEntry.out_end());
127
128 return lastOutgoing->getLastRenewed();
129}
130
Saurab Dulala6dec222019-04-01 00:15:10 -0500131fib::NextHopList::const_iterator
132findEligibleNextHopWithEarliestOutRecord(const Face& inFace, const Interest& interest,
133 const fib::NextHopList& nexthops,
134 const shared_ptr<pit::Entry>& pitEntry)
135{
136 auto found = nexthops.end();
137 auto earliestRenewed = time::steady_clock::TimePoint::max();
138
139 for (auto it = nexthops.begin(); it != nexthops.end(); ++it) {
140 if (!isNextHopEligible(inFace, interest, *it, pitEntry))
141 continue;
142
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000143 auto outRecord = pitEntry->getOutRecord(it->getFace());
Saurab Dulala6dec222019-04-01 00:15:10 -0500144 BOOST_ASSERT(outRecord != pitEntry->out_end());
145 if (outRecord->getLastRenewed() < earliestRenewed) {
146 found = it;
147 earliestRenewed = outRecord->getLastRenewed();
148 }
149 }
150 return found;
151}
152
153bool
154isNextHopEligible(const Face& inFace, const Interest& interest,
155 const fib::NextHop& nexthop,
156 const shared_ptr<pit::Entry>& pitEntry,
157 bool wantUnused,
158 time::steady_clock::TimePoint now)
159{
160 const Face& outFace = nexthop.getFace();
161
162 // do not forward back to the same face, unless it is ad hoc
163 if ((outFace.getId() == inFace.getId() && outFace.getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) ||
164 (wouldViolateScope(inFace, interest, outFace)))
165 return false;
166
167 if (wantUnused) {
168 // nexthop must not have unexpired out-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000169 auto outRecord = pitEntry->getOutRecord(outFace);
Saurab Dulala6dec222019-04-01 00:15:10 -0500170 if (outRecord != pitEntry->out_end() && outRecord->getExpiry() > now) {
171 return false;
172 }
173 }
174 return true;
175}
176
Junxiao Shifef73e42016-03-29 14:15:05 -0700177} // namespace fw
178} // namespace nfd