blob: d82f607bdf53a4935b33d8cc09403a429d992d46 [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 Pesaventob124b8e2024-02-16 16:54:26 -05003 * Copyright (c) 2014-2024, 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"
Davide Pesavento972de802024-02-16 18:42:55 -050028#include "face/face.hpp"
Junxiao Shifef73e42016-03-29 14:15:05 -070029
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040030namespace nfd::fw {
Junxiao Shif3410d82016-04-05 13:49:44 -070031
Junxiao Shifef73e42016-03-29 14:15:05 -070032bool
Junxiao Shi00dc9142016-11-21 14:23:12 +000033wouldViolateScope(const Face& inFace, const Interest& interest, const Face& outFace)
34{
35 if (outFace.getScope() == ndn::nfd::FACE_SCOPE_LOCAL) {
36 // forwarding to a local face is always allowed
37 return false;
38 }
39
40 if (scope_prefix::LOCALHOST.isPrefixOf(interest.getName())) {
41 // localhost Interests cannot be forwarded to a non-local face
42 return true;
43 }
44
45 if (scope_prefix::LOCALHOP.isPrefixOf(interest.getName())) {
46 // localhop Interests can be forwarded to a non-local face only if it comes from a local face
47 return inFace.getScope() != ndn::nfd::FACE_SCOPE_LOCAL;
48 }
49
50 // Interest name is not subject to scope control
51 return false;
52}
53
Junxiao Shifef73e42016-03-29 14:15:05 -070054int
Davide Pesavento51cf75c2020-03-11 22:21:13 -040055findDuplicateNonce(const pit::Entry& pitEntry, Interest::Nonce nonce, const Face& face)
Junxiao Shifef73e42016-03-29 14:15:05 -070056{
57 int dnw = DUPLICATE_NONCE_NONE;
58
59 for (const pit::InRecord& inRecord : pitEntry.getInRecords()) {
60 if (inRecord.getLastNonce() == nonce) {
Junxiao Shi9cff7792016-08-01 21:45:11 +000061 if (&inRecord.getFace() == &face) {
Junxiao Shifef73e42016-03-29 14:15:05 -070062 dnw |= DUPLICATE_NONCE_IN_SAME;
63 }
64 else {
65 dnw |= DUPLICATE_NONCE_IN_OTHER;
66 }
67 }
68 }
69
70 for (const pit::OutRecord& outRecord : pitEntry.getOutRecords()) {
71 if (outRecord.getLastNonce() == nonce) {
Junxiao Shi9cff7792016-08-01 21:45:11 +000072 if (&outRecord.getFace() == &face) {
Junxiao Shifef73e42016-03-29 14:15:05 -070073 dnw |= DUPLICATE_NONCE_OUT_SAME;
74 }
75 else {
76 dnw |= DUPLICATE_NONCE_OUT_OTHER;
77 }
78 }
79 }
80
81 return dnw;
82}
83
84bool
85hasPendingOutRecords(const pit::Entry& pitEntry)
86{
Davide Pesavento412c9822021-07-02 00:21:05 -040087 auto now = time::steady_clock::now();
Junxiao Shi4846f372016-04-05 13:39:30 -070088 return std::any_of(pitEntry.out_begin(), pitEntry.out_end(),
Junxiao Shi8744c972016-04-06 10:33:46 -070089 [&now] (const pit::OutRecord& outRecord) {
90 return outRecord.getExpiry() >= now &&
91 outRecord.getIncomingNack() == nullptr;
92 });
Junxiao Shifef73e42016-03-29 14:15:05 -070093}
94
Davide Pesavento412c9822021-07-02 00:21:05 -040095time::steady_clock::time_point
Ashlesh Gawande90015992017-07-11 17:25:48 -050096getLastOutgoing(const pit::Entry& pitEntry)
97{
98 pit::OutRecordCollection::const_iterator lastOutgoing = std::max_element(
99 pitEntry.out_begin(), pitEntry.out_end(),
100 [] (const pit::OutRecord& a, const pit::OutRecord& b) {
101 return a.getLastRenewed() < b.getLastRenewed();
102 });
103 BOOST_ASSERT(lastOutgoing != pitEntry.out_end());
104
105 return lastOutgoing->getLastRenewed();
106}
107
Saurab Dulala6dec222019-04-01 00:15:10 -0500108fib::NextHopList::const_iterator
109findEligibleNextHopWithEarliestOutRecord(const Face& inFace, const Interest& interest,
110 const fib::NextHopList& nexthops,
111 const shared_ptr<pit::Entry>& pitEntry)
112{
113 auto found = nexthops.end();
Davide Pesavento412c9822021-07-02 00:21:05 -0400114 auto earliestRenewed = time::steady_clock::time_point::max();
Saurab Dulala6dec222019-04-01 00:15:10 -0500115
116 for (auto it = nexthops.begin(); it != nexthops.end(); ++it) {
117 if (!isNextHopEligible(inFace, interest, *it, pitEntry))
118 continue;
119
Davide Pesaventob124b8e2024-02-16 16:54:26 -0500120 auto outRecord = pitEntry->findOutRecord(it->getFace());
Saurab Dulala6dec222019-04-01 00:15:10 -0500121 BOOST_ASSERT(outRecord != pitEntry->out_end());
122 if (outRecord->getLastRenewed() < earliestRenewed) {
123 found = it;
124 earliestRenewed = outRecord->getLastRenewed();
125 }
126 }
127 return found;
128}
129
130bool
131isNextHopEligible(const Face& inFace, const Interest& interest,
132 const fib::NextHop& nexthop,
133 const shared_ptr<pit::Entry>& pitEntry,
134 bool wantUnused,
Davide Pesavento412c9822021-07-02 00:21:05 -0400135 time::steady_clock::time_point now)
Saurab Dulala6dec222019-04-01 00:15:10 -0500136{
137 const Face& outFace = nexthop.getFace();
138
139 // do not forward back to the same face, unless it is ad hoc
140 if ((outFace.getId() == inFace.getId() && outFace.getLinkType() != ndn::nfd::LINK_TYPE_AD_HOC) ||
141 (wouldViolateScope(inFace, interest, outFace)))
142 return false;
143
144 if (wantUnused) {
145 // nexthop must not have unexpired out-record
Davide Pesaventob124b8e2024-02-16 16:54:26 -0500146 auto outRecord = pitEntry->findOutRecord(outFace);
Saurab Dulala6dec222019-04-01 00:15:10 -0500147 if (outRecord != pitEntry->out_end() && outRecord->getExpiry() > now) {
148 return false;
149 }
150 }
151 return true;
152}
153
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400154} // namespace nfd::fw