Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Ashlesh Gawande | 9001599 | 2017-07-11 17:25:48 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | 5809158 | 2021-03-05 19:02:48 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2021, Regents of the University of California, |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 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 | |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 26 | #include "algorithm.hpp" |
Davide Pesavento | 5809158 | 2021-03-05 19:02:48 -0500 | [diff] [blame] | 27 | #include "scope-prefix.hpp" |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 28 | |
| 29 | namespace nfd { |
Junxiao Shi | f3410d8 | 2016-04-05 13:49:44 -0700 | [diff] [blame] | 30 | namespace fw { |
| 31 | |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 32 | bool |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 33 | wouldViolateScope(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 Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 54 | int |
Davide Pesavento | 51cf75c | 2020-03-11 22:21:13 -0400 | [diff] [blame] | 55 | findDuplicateNonce(const pit::Entry& pitEntry, Interest::Nonce nonce, const Face& face) |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 56 | { |
| 57 | int dnw = DUPLICATE_NONCE_NONE; |
| 58 | |
| 59 | for (const pit::InRecord& inRecord : pitEntry.getInRecords()) { |
| 60 | if (inRecord.getLastNonce() == nonce) { |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 61 | if (&inRecord.getFace() == &face) { |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 62 | 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 Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 72 | if (&outRecord.getFace() == &face) { |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 73 | dnw |= DUPLICATE_NONCE_OUT_SAME; |
| 74 | } |
| 75 | else { |
| 76 | dnw |= DUPLICATE_NONCE_OUT_OTHER; |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | return dnw; |
| 82 | } |
| 83 | |
| 84 | bool |
| 85 | hasPendingOutRecords(const pit::Entry& pitEntry) |
| 86 | { |
| 87 | time::steady_clock::TimePoint now = time::steady_clock::now(); |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 88 | return std::any_of(pitEntry.out_begin(), pitEntry.out_end(), |
Junxiao Shi | 8744c97 | 2016-04-06 10:33:46 -0700 | [diff] [blame] | 89 | [&now] (const pit::OutRecord& outRecord) { |
| 90 | return outRecord.getExpiry() >= now && |
| 91 | outRecord.getIncomingNack() == nullptr; |
| 92 | }); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Ashlesh Gawande | 9001599 | 2017-07-11 17:25:48 -0500 | [diff] [blame] | 95 | time::steady_clock::TimePoint |
| 96 | getLastOutgoing(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 Dulal | a6dec22 | 2019-04-01 00:15:10 -0500 | [diff] [blame] | 108 | fib::NextHopList::const_iterator |
| 109 | findEligibleNextHopWithEarliestOutRecord(const Face& inFace, const Interest& interest, |
| 110 | const fib::NextHopList& nexthops, |
| 111 | const shared_ptr<pit::Entry>& pitEntry) |
| 112 | { |
| 113 | auto found = nexthops.end(); |
| 114 | auto earliestRenewed = time::steady_clock::TimePoint::max(); |
| 115 | |
| 116 | for (auto it = nexthops.begin(); it != nexthops.end(); ++it) { |
| 117 | if (!isNextHopEligible(inFace, interest, *it, pitEntry)) |
| 118 | continue; |
| 119 | |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 120 | auto outRecord = pitEntry->getOutRecord(it->getFace()); |
Saurab Dulal | a6dec22 | 2019-04-01 00:15:10 -0500 | [diff] [blame] | 121 | 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 | |
| 130 | bool |
| 131 | isNextHopEligible(const Face& inFace, const Interest& interest, |
| 132 | const fib::NextHop& nexthop, |
| 133 | const shared_ptr<pit::Entry>& pitEntry, |
| 134 | bool wantUnused, |
| 135 | time::steady_clock::TimePoint now) |
| 136 | { |
| 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 |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 146 | auto outRecord = pitEntry->getOutRecord(outFace); |
Saurab Dulal | a6dec22 | 2019-04-01 00:15:10 -0500 | [diff] [blame] | 147 | if (outRecord != pitEntry->out_end() && outRecord->getExpiry() > now) { |
| 148 | return false; |
| 149 | } |
| 150 | } |
| 151 | return true; |
| 152 | } |
| 153 | |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 154 | } // namespace fw |
| 155 | } // namespace nfd |