blob: a16ccc3617f0402f0d26d1a09b58eff442115007 [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 Pesaventoe422f9e2022-06-03 01:30:23 -04003 * Copyright (c) 2014-2022, 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"
Davide Pesavento58091582021-03-05 19:02:48 -050027#include "scope-prefix.hpp"
Junxiao Shifef73e42016-03-29 14:15:05 -070028
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040029namespace nfd::fw {
Junxiao Shif3410d82016-04-05 13:49:44 -070030
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
Junxiao Shifef73e42016-03-29 14:15:05 -070053int
Davide Pesavento51cf75c2020-03-11 22:21:13 -040054findDuplicateNonce(const pit::Entry& pitEntry, Interest::Nonce nonce, const Face& face)
Junxiao Shifef73e42016-03-29 14:15:05 -070055{
56 int dnw = DUPLICATE_NONCE_NONE;
57
58 for (const pit::InRecord& inRecord : pitEntry.getInRecords()) {
59 if (inRecord.getLastNonce() == nonce) {
Junxiao Shi9cff7792016-08-01 21:45:11 +000060 if (&inRecord.getFace() == &face) {
Junxiao Shifef73e42016-03-29 14:15:05 -070061 dnw |= DUPLICATE_NONCE_IN_SAME;
62 }
63 else {
64 dnw |= DUPLICATE_NONCE_IN_OTHER;
65 }
66 }
67 }
68
69 for (const pit::OutRecord& outRecord : pitEntry.getOutRecords()) {
70 if (outRecord.getLastNonce() == nonce) {
Junxiao Shi9cff7792016-08-01 21:45:11 +000071 if (&outRecord.getFace() == &face) {
Junxiao Shifef73e42016-03-29 14:15:05 -070072 dnw |= DUPLICATE_NONCE_OUT_SAME;
73 }
74 else {
75 dnw |= DUPLICATE_NONCE_OUT_OTHER;
76 }
77 }
78 }
79
80 return dnw;
81}
82
83bool
84hasPendingOutRecords(const pit::Entry& pitEntry)
85{
Davide Pesavento412c9822021-07-02 00:21:05 -040086 auto now = time::steady_clock::now();
Junxiao Shi4846f372016-04-05 13:39:30 -070087 return std::any_of(pitEntry.out_begin(), pitEntry.out_end(),
Junxiao Shi8744c972016-04-06 10:33:46 -070088 [&now] (const pit::OutRecord& outRecord) {
89 return outRecord.getExpiry() >= now &&
90 outRecord.getIncomingNack() == nullptr;
91 });
Junxiao Shifef73e42016-03-29 14:15:05 -070092}
93
Davide Pesavento412c9822021-07-02 00:21:05 -040094time::steady_clock::time_point
Ashlesh Gawande90015992017-07-11 17:25:48 -050095getLastOutgoing(const pit::Entry& pitEntry)
96{
97 pit::OutRecordCollection::const_iterator lastOutgoing = std::max_element(
98 pitEntry.out_begin(), pitEntry.out_end(),
99 [] (const pit::OutRecord& a, const pit::OutRecord& b) {
100 return a.getLastRenewed() < b.getLastRenewed();
101 });
102 BOOST_ASSERT(lastOutgoing != pitEntry.out_end());
103
104 return lastOutgoing->getLastRenewed();
105}
106
Saurab Dulala6dec222019-04-01 00:15:10 -0500107fib::NextHopList::const_iterator
108findEligibleNextHopWithEarliestOutRecord(const Face& inFace, const Interest& interest,
109 const fib::NextHopList& nexthops,
110 const shared_ptr<pit::Entry>& pitEntry)
111{
112 auto found = nexthops.end();
Davide Pesavento412c9822021-07-02 00:21:05 -0400113 auto earliestRenewed = time::steady_clock::time_point::max();
Saurab Dulala6dec222019-04-01 00:15:10 -0500114
115 for (auto it = nexthops.begin(); it != nexthops.end(); ++it) {
116 if (!isNextHopEligible(inFace, interest, *it, pitEntry))
117 continue;
118
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000119 auto outRecord = pitEntry->getOutRecord(it->getFace());
Saurab Dulala6dec222019-04-01 00:15:10 -0500120 BOOST_ASSERT(outRecord != pitEntry->out_end());
121 if (outRecord->getLastRenewed() < earliestRenewed) {
122 found = it;
123 earliestRenewed = outRecord->getLastRenewed();
124 }
125 }
126 return found;
127}
128
129bool
130isNextHopEligible(const Face& inFace, const Interest& interest,
131 const fib::NextHop& nexthop,
132 const shared_ptr<pit::Entry>& pitEntry,
133 bool wantUnused,
Davide Pesavento412c9822021-07-02 00:21:05 -0400134 time::steady_clock::time_point now)
Saurab Dulala6dec222019-04-01 00:15:10 -0500135{
136 const Face& outFace = nexthop.getFace();
137
138 // do not forward back to the same face, unless it is ad hoc
139 if ((outFace.getId() == inFace.getId() && outFace.getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) ||
140 (wouldViolateScope(inFace, interest, outFace)))
141 return false;
142
143 if (wantUnused) {
144 // nexthop must not have unexpired out-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000145 auto outRecord = pitEntry->getOutRecord(outFace);
Saurab Dulala6dec222019-04-01 00:15:10 -0500146 if (outRecord != pitEntry->out_end() && outRecord->getExpiry() > now) {
147 return false;
148 }
149 }
150 return true;
151}
152
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400153} // namespace nfd::fw