fw: /localhop scope control on outgoing Interests

refs #1253

Change-Id: I359747688568934070d1583995957835ac690f28
diff --git a/daemon/fw/forwarder.cpp b/daemon/fw/forwarder.cpp
index e6d3438..a723a17 100644
--- a/daemon/fw/forwarder.cpp
+++ b/daemon/fw/forwarder.cpp
@@ -15,6 +15,7 @@
 
 const ndn::Milliseconds Forwarder::DEFAULT_INTEREST_LIFETIME(static_cast<ndn::Milliseconds>(4000));
 const Name Forwarder::LOCALHOST_NAME("ndn:/localhost");
+const Name Forwarder::LOCALHOP_NAME("ndn:/localhop");
 
 Forwarder::Forwarder()
   : m_faceTable(*this)
@@ -140,6 +141,16 @@
     return;
   }
 
+  // /localhop scope control
+  bool isViolatingLocalhop = !outFace.isLocal() &&
+                             LOCALHOP_NAME.isPrefixOf(pitEntry->getName()) &&
+                             !pitEntry->hasLocalInRecord();
+  if (isViolatingLocalhop) {
+    NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() <<
+                  " interest=" << pitEntry->getName() << " violates /localhop");
+    return;
+  }
+
   // pick Interest
   const pit::InRecordCollection& inRecords = pitEntry->getInRecords();
   pit::InRecordCollection::const_iterator pickedInRecord = std::max_element(
diff --git a/daemon/fw/forwarder.hpp b/daemon/fw/forwarder.hpp
index c6e3529..e229dcf 100644
--- a/daemon/fw/forwarder.hpp
+++ b/daemon/fw/forwarder.hpp
@@ -159,6 +159,7 @@
 
   static const ndn::Milliseconds DEFAULT_INTEREST_LIFETIME;
   static const Name LOCALHOST_NAME;
+  static const Name LOCALHOP_NAME;
 
   // allow Strategy (base class) to enter pipelines
   friend class fw::Strategy;
diff --git a/daemon/table/pit-entry.cpp b/daemon/table/pit-entry.cpp
index 2d016ca..7523504 100644
--- a/daemon/table/pit-entry.cpp
+++ b/daemon/table/pit-entry.cpp
@@ -34,6 +34,20 @@
 }
 
 static inline bool
+predicate_InRecord_isLocal(const InRecord& inRecord)
+{
+  return inRecord.getFace()->isLocal();
+}
+
+bool
+Entry::hasLocalInRecord() const
+{
+  InRecordCollection::const_iterator it = std::find_if(
+    m_inRecords.begin(), m_inRecords.end(), &predicate_InRecord_isLocal);
+  return it != m_inRecords.end();
+}
+
+static inline bool
 predicate_FaceRecord_Face(const FaceRecord& faceRecord, shared_ptr<Face> face)
 {
   return faceRecord.getFace() == face;
@@ -57,7 +71,7 @@
   if (hasUnexpiredOutRecord) {
     return false;
   }
-  
+
   InRecordCollection::const_iterator inIt = std::find_if(
     m_inRecords.begin(), m_inRecords.end(),
     bind(&predicate_FaceRecord_ne_Face_and_unexpired, _1, face, time::now()));
@@ -83,7 +97,7 @@
     m_inRecords.push_front(InRecord(face));
     it = m_inRecords.begin();
   }
-  
+
   it->update(interest);
   return it;
 }
@@ -103,7 +117,7 @@
     m_outRecords.push_front(OutRecord(face));
     it = m_outRecords.begin();
   }
-  
+
   it->update(interest);
   m_nonces.insert(interest.getNonce());
   return it;
diff --git a/daemon/table/pit-entry.hpp b/daemon/table/pit-entry.hpp
index aeff8d8..1e342e2 100644
--- a/daemon/table/pit-entry.hpp
+++ b/daemon/table/pit-entry.hpp
@@ -53,6 +53,14 @@
   const OutRecordCollection&
   getOutRecords() const;
 
+  /** \brief determines whether any InRecord is a local Face
+   *
+   *  \return true if any InRecord is a local Face,
+   *          false if all InRecords are non-local Faces
+   */
+  bool
+  hasLocalInRecord() const;
+
   /** \brief decides whether Interest can be forwarded to face
    *
    *  \return true if OutRecord of this face does not exist or has expired,