fw: localhop scope restriction in best-route v1, multicast, ncc strategies

canForwardToLegacy function no longer checks scope. Strategies using this
function shall use wouldViolateScope separately.

refs #3841, #1756

Change-Id: Ib1bd3f9fe19e4cadfa27e86dcc730d16cfd4b0db
diff --git a/daemon/fw/algorithm.cpp b/daemon/fw/algorithm.cpp
index 7a1f2cc..0eb9cce 100644
--- a/daemon/fw/algorithm.cpp
+++ b/daemon/fw/algorithm.cpp
@@ -99,7 +99,7 @@
     return false;
   }
 
-  return !violatesScope(pitEntry, face);
+  return true;
 }
 
 int
diff --git a/daemon/fw/algorithm.hpp b/daemon/fw/algorithm.hpp
index ebf493a..88ad0b4 100644
--- a/daemon/fw/algorithm.hpp
+++ b/daemon/fw/algorithm.hpp
@@ -84,8 +84,7 @@
 /** \brief decide whether Interest can be forwarded to face
  *
  *  \return true if out-record of this face does not exist or has expired,
- *          and there is an in-record not of this face,
- *          and scope is not violated
+ *          and there is an in-record not of this face
  *
  *  \note This algorithm has a weakness that it does not permit consumer retransmissions
  *        before out-record expires. Therefore, it's not recommended to use this function
diff --git a/daemon/fw/best-route-strategy.cpp b/daemon/fw/best-route-strategy.cpp
index ebe0d08..a8877e1 100644
--- a/daemon/fw/best-route-strategy.cpp
+++ b/daemon/fw/best-route-strategy.cpp
@@ -48,16 +48,17 @@
 
   const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
   const fib::NextHopList& nexthops = fibEntry.getNextHops();
-  fib::NextHopList::const_iterator it = std::find_if(nexthops.begin(), nexthops.end(),
-    [&pitEntry] (const fib::NextHop& nexthop) { return canForwardToLegacy(*pitEntry, nexthop.getFace()); });
 
-  if (it == nexthops.end()) {
-    this->rejectPendingInterest(pitEntry);
-    return;
+  for (fib::NextHopList::const_iterator it = nexthops.begin(); it != nexthops.end(); ++it) {
+    Face& outFace = it->getFace();
+    if (!wouldViolateScope(inFace, interest, outFace) &&
+        canForwardToLegacy(*pitEntry, outFace)) {
+      this->sendInterest(pitEntry, outFace, interest);
+      return;
+    }
   }
 
-  Face& outFace = it->getFace();
-  this->sendInterest(pitEntry, outFace);
+  this->rejectPendingInterest(pitEntry);
 }
 
 } // namespace fw
diff --git a/daemon/fw/multicast-strategy.cpp b/daemon/fw/multicast-strategy.cpp
index 2821d71..3d6e8b2 100644
--- a/daemon/fw/multicast-strategy.cpp
+++ b/daemon/fw/multicast-strategy.cpp
@@ -46,8 +46,9 @@
 
   for (fib::NextHopList::const_iterator it = nexthops.begin(); it != nexthops.end(); ++it) {
     Face& outFace = it->getFace();
-    if (canForwardToLegacy(*pitEntry, outFace)) {
-      this->sendInterest(pitEntry, outFace);
+    if (!wouldViolateScope(inFace, interest, outFace) &&
+        canForwardToLegacy(*pitEntry, outFace)) {
+      this->sendInterest(pitEntry, outFace, interest);
     }
   }
 
diff --git a/daemon/fw/ncc-strategy.cpp b/daemon/fw/ncc-strategy.cpp
index 2ae2fd0..650182c 100644
--- a/daemon/fw/ncc-strategy.cpp
+++ b/daemon/fw/ncc-strategy.cpp
@@ -67,12 +67,13 @@
 
   shared_ptr<Face> bestFace = meInfo.getBestFace();
   if (bestFace != nullptr && fibEntry.hasNextHop(*bestFace) &&
+      !wouldViolateScope(inFace, interest, *bestFace) &&
       canForwardToLegacy(*pitEntry, *bestFace)) {
     // TODO Should we use `randlow = 100 + nrand48(h->seed) % 4096U;` ?
     deferFirst = meInfo.prediction;
     deferRange = time::microseconds((deferFirst.count() + 1) / 2);
     --nUpstreams;
-    this->sendInterest(pitEntry, *bestFace);
+    this->sendInterest(pitEntry, *bestFace, interest);
     pitEntryInfo->bestFaceTimeout = scheduler::schedule(
       meInfo.prediction,
       bind(&NccStrategy::timeoutOnBestFace, this, weak_ptr<pit::Entry>(pitEntry)));
@@ -80,16 +81,23 @@
   else {
     // use first eligible nexthop
     auto firstEligibleNexthop = std::find_if(nexthops.begin(), nexthops.end(),
-        [&pitEntry] (const fib::NextHop& nexthop) {
-          return canForwardToLegacy(*pitEntry, nexthop.getFace());
+        [&] (const fib::NextHop& nexthop) {
+          Face& outFace = nexthop.getFace();
+          return !wouldViolateScope(inFace, interest, outFace) &&
+                 canForwardToLegacy(*pitEntry, outFace);
         });
     if (firstEligibleNexthop != nexthops.end()) {
-      this->sendInterest(pitEntry, firstEligibleNexthop->getFace());
+      this->sendInterest(pitEntry, firstEligibleNexthop->getFace(), interest);
+    }
+    else {
+      this->rejectPendingInterest(pitEntry);
+      return;
     }
   }
 
   shared_ptr<Face> previousFace = meInfo.previousFace.lock();
   if (previousFace != nullptr && fibEntry.hasNextHop(*previousFace) &&
+      !wouldViolateScope(inFace, interest, *previousFace) &&
       canForwardToLegacy(*pitEntry, *previousFace)) {
     --nUpstreams;
   }
@@ -105,16 +113,25 @@
     pitEntryInfo->maxInterval = deferFirst;
   }
   pitEntryInfo->propagateTimer = scheduler::schedule(deferFirst,
-    bind(&NccStrategy::doPropagate, this, weak_ptr<pit::Entry>(pitEntry)));
+    bind(&NccStrategy::doPropagate, this, inFace.getId(), weak_ptr<pit::Entry>(pitEntry)));
 }
 
 void
-NccStrategy::doPropagate(weak_ptr<pit::Entry> pitEntryWeak)
+NccStrategy::doPropagate(FaceId inFaceId, weak_ptr<pit::Entry> pitEntryWeak)
 {
+  Face* inFace = this->getFace(inFaceId);
+  if (inFace == nullptr) {
+    return;
+  }
   shared_ptr<pit::Entry> pitEntry = pitEntryWeak.lock();
   if (pitEntry == nullptr) {
     return;
   }
+  pit::InRecordCollection::const_iterator inRecord = pitEntry->getInRecord(*inFace);
+  if (inRecord == pitEntry->in_end()) {
+    return;
+  }
+  const Interest& interest = inRecord->getInterest();
   const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
 
   PitEntryInfo* pitEntryInfo = pitEntry->getStrategyInfo<PitEntryInfo>();
@@ -126,17 +143,19 @@
 
   shared_ptr<Face> previousFace = meInfo.previousFace.lock();
   if (previousFace != nullptr && fibEntry.hasNextHop(*previousFace) &&
+      !wouldViolateScope(*inFace, interest, *previousFace) &&
       canForwardToLegacy(*pitEntry, *previousFace)) {
-    this->sendInterest(pitEntry, *previousFace);
+    this->sendInterest(pitEntry, *previousFace, interest);
   }
 
   const fib::NextHopList& nexthops = fibEntry.getNextHops();
   bool isForwarded = false;
   for (fib::NextHopList::const_iterator it = nexthops.begin(); it != nexthops.end(); ++it) {
     Face& face = it->getFace();
-    if (canForwardToLegacy(*pitEntry, face)) {
+    if (!wouldViolateScope(*inFace, interest, face) &&
+        canForwardToLegacy(*pitEntry, face)) {
       isForwarded = true;
-      this->sendInterest(pitEntry, face);
+      this->sendInterest(pitEntry, face, interest);
       break;
     }
   }
@@ -145,7 +164,7 @@
     std::uniform_int_distribution<time::nanoseconds::rep> dist(0, pitEntryInfo->maxInterval.count() - 1);
     time::nanoseconds deferNext = time::nanoseconds(dist(getGlobalRng()));
     pitEntryInfo->propagateTimer = scheduler::schedule(deferNext,
-      bind(&NccStrategy::doPropagate, this, weak_ptr<pit::Entry>(pitEntry)));
+      bind(&NccStrategy::doPropagate, this, inFaceId, weak_ptr<pit::Entry>(pitEntry)));
   }
 }
 
diff --git a/daemon/fw/ncc-strategy.hpp b/daemon/fw/ncc-strategy.hpp
index 5043ee3..9403dd5 100644
--- a/daemon/fw/ncc-strategy.hpp
+++ b/daemon/fw/ncc-strategy.hpp
@@ -122,7 +122,7 @@
 
   /// propagate to another upstream
   void
-  doPropagate(weak_ptr<pit::Entry> pitEntryWeak);
+  doPropagate(FaceId inFaceId, weak_ptr<pit::Entry> pitEntryWeak);
 
   /// best face did not reply within prediction
   void