Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 1 | /* -*- 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 | |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 26 | #include "algorithm.hpp" |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 27 | |
| 28 | namespace nfd { |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 29 | namespace scope_prefix { |
| 30 | const Name LOCALHOST("ndn:/localhost"); |
| 31 | const Name LOCALHOP("ndn:/localhop"); |
| 32 | } // namespace scope_prefix |
| 33 | |
Junxiao Shi | f3410d8 | 2016-04-05 13:49:44 -0700 | [diff] [blame] | 34 | namespace fw { |
| 35 | |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 36 | bool |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 37 | wouldViolateScope(const Face& inFace, const Interest& interest, const Face& outFace) |
| 38 | { |
| 39 | if (outFace.getScope() == ndn::nfd::FACE_SCOPE_LOCAL) { |
| 40 | // forwarding to a local face is always allowed |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | if (scope_prefix::LOCALHOST.isPrefixOf(interest.getName())) { |
| 45 | // localhost Interests cannot be forwarded to a non-local face |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | if (scope_prefix::LOCALHOP.isPrefixOf(interest.getName())) { |
| 50 | // localhop Interests can be forwarded to a non-local face only if it comes from a local face |
| 51 | return inFace.getScope() != ndn::nfd::FACE_SCOPE_LOCAL; |
| 52 | } |
| 53 | |
| 54 | // Interest name is not subject to scope control |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | bool |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 59 | violatesScope(const pit::Entry& pitEntry, const Face& outFace) |
| 60 | { |
| 61 | if (outFace.getScope() == ndn::nfd::FACE_SCOPE_LOCAL) { |
| 62 | return false; |
| 63 | } |
| 64 | BOOST_ASSERT(outFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL); |
| 65 | |
| 66 | if (scope_prefix::LOCALHOST.isPrefixOf(pitEntry.getName())) { |
| 67 | // face is non-local, violates localhost scope |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | if (scope_prefix::LOCALHOP.isPrefixOf(pitEntry.getName())) { |
| 72 | // face is non-local, violates localhop scope unless PIT entry has local in-record |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 73 | return std::none_of(pitEntry.in_begin(), pitEntry.in_end(), |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 74 | [] (const pit::InRecord& inRecord) { return inRecord.getFace().getScope() == ndn::nfd::FACE_SCOPE_LOCAL; }); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | // Name is not subject to scope control |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | bool |
| 82 | canForwardToLegacy(const pit::Entry& pitEntry, const Face& face) |
| 83 | { |
| 84 | time::steady_clock::TimePoint now = time::steady_clock::now(); |
| 85 | |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 86 | bool hasUnexpiredOutRecord = std::any_of(pitEntry.out_begin(), pitEntry.out_end(), |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 87 | [&face, &now] (const pit::OutRecord& outRecord) { |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 88 | return &outRecord.getFace() == &face && outRecord.getExpiry() >= now; |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 89 | }); |
| 90 | if (hasUnexpiredOutRecord) { |
| 91 | return false; |
| 92 | } |
| 93 | |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 94 | bool hasUnexpiredOtherInRecord = std::any_of(pitEntry.in_begin(), pitEntry.in_end(), |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 95 | [&face, &now] (const pit::InRecord& inRecord) { |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 96 | return &inRecord.getFace() != &face && inRecord.getExpiry() >= now; |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 97 | }); |
| 98 | if (!hasUnexpiredOtherInRecord) { |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | return !violatesScope(pitEntry, face); |
| 103 | } |
| 104 | |
| 105 | int |
| 106 | findDuplicateNonce(const pit::Entry& pitEntry, uint32_t nonce, const Face& face) |
| 107 | { |
| 108 | int dnw = DUPLICATE_NONCE_NONE; |
| 109 | |
| 110 | for (const pit::InRecord& inRecord : pitEntry.getInRecords()) { |
| 111 | if (inRecord.getLastNonce() == nonce) { |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 112 | if (&inRecord.getFace() == &face) { |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 113 | dnw |= DUPLICATE_NONCE_IN_SAME; |
| 114 | } |
| 115 | else { |
| 116 | dnw |= DUPLICATE_NONCE_IN_OTHER; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | for (const pit::OutRecord& outRecord : pitEntry.getOutRecords()) { |
| 122 | if (outRecord.getLastNonce() == nonce) { |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 123 | if (&outRecord.getFace() == &face) { |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 124 | dnw |= DUPLICATE_NONCE_OUT_SAME; |
| 125 | } |
| 126 | else { |
| 127 | dnw |= DUPLICATE_NONCE_OUT_OTHER; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return dnw; |
| 133 | } |
| 134 | |
| 135 | bool |
| 136 | hasPendingOutRecords(const pit::Entry& pitEntry) |
| 137 | { |
| 138 | time::steady_clock::TimePoint now = time::steady_clock::now(); |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 139 | return std::any_of(pitEntry.out_begin(), pitEntry.out_end(), |
Junxiao Shi | 8744c97 | 2016-04-06 10:33:46 -0700 | [diff] [blame] | 140 | [&now] (const pit::OutRecord& outRecord) { |
| 141 | return outRecord.getExpiry() >= now && |
| 142 | outRecord.getIncomingNack() == nullptr; |
| 143 | }); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | } // namespace fw |
| 147 | } // namespace nfd |