fw: /localhost scope control for incoming Interest

refs #1230

Change-Id: I86fd071ef144caa506b26a723f14232e2af5e2de
diff --git a/daemon/fw/forwarder.cpp b/daemon/fw/forwarder.cpp
index 67aeba7..6ab6fd4 100644
--- a/daemon/fw/forwarder.cpp
+++ b/daemon/fw/forwarder.cpp
@@ -12,6 +12,8 @@
 
 NFD_LOG_INIT("Forwarder");
 
+const Name Forwarder::s_localhostName("ndn:/localhost");
+
 Forwarder::Forwarder(boost::asio::io_service& ioService)
   : m_scheduler(ioService)
   , m_lastFaceId(0)
@@ -75,6 +77,15 @@
   NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId() << " interest=" << interest.getName());
   const_cast<Interest&>(interest).setIncomingFaceId(inFace.getId());
   
+  // /localhost scope control
+  bool violatesLocalhost = !inFace.isLocal() &&
+                           s_localhostName.isPrefixOf(interest.getName());
+  if (violatesLocalhost) {
+    NFD_LOG_DEBUG("onIncomingInterest face=" << inFace.getId()
+      << " interest=" << interest.getName() << " violates /localhost");
+    return;
+  }
+  
   // PIT insert
   std::pair<shared_ptr<pit::Entry>, bool>
     pitInsertResult = m_pit.insert(interest);
@@ -123,8 +134,7 @@
   // TODO use Fib::findParent(pitEntry)
   
   // dispatch to strategy
-  m_strategy->afterReceiveInterest(inFace, interest, fibEntry, pitEntry);
-  // TODO dispatch according to fibEntry
+  this->dispatchToStrategy(inFace, interest, fibEntry, pitEntry);
 }
 
 void
@@ -184,6 +194,15 @@
   NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() << " data=" << data.getName());
   const_cast<Data&>(data).setIncomingFaceId(inFace.getId());
   
+  // /localhost scope control
+  bool violatesLocalhost = !inFace.isLocal() &&
+                           s_localhostName.isPrefixOf(data.getName());
+  if (violatesLocalhost) {
+    NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId()
+      << " interest=" << data.getName() << " violates /localhost");
+    return;
+  }
+  
   // PIT match
   shared_ptr<pit::DataMatchResult> pitMatches = m_pit.findAllDataMatches(data);
   if (pitMatches->begin() == pitMatches->end()) {
@@ -298,6 +317,14 @@
   m_scheduler.cancelEvent(pitEntry->m_stragglerTimer);
 }
 
-
+void
+Forwarder::dispatchToStrategy(const Face& inFace,
+                              const Interest& interest,
+                              shared_ptr<fib::Entry> fibEntry,
+                              shared_ptr<pit::Entry> pitEntry)
+{
+  m_strategy->afterReceiveInterest(inFace, interest, fibEntry, pitEntry);
+  // TODO dispatch according to fibEntry
+}
 
 } // namespace nfd
diff --git a/daemon/fw/forwarder.hpp b/daemon/fw/forwarder.hpp
index 0ea838b..ae9fb6a 100644
--- a/daemon/fw/forwarder.hpp
+++ b/daemon/fw/forwarder.hpp
@@ -25,6 +25,7 @@
 class Forwarder
 {
 public:
+  explicit
   Forwarder(boost::asio::io_service& ioService);
 
   void
@@ -52,57 +53,63 @@
   shared_ptr<Face>
   getFace(FaceId id);
 
-private: // pipelines
+PUBLIC_WITH_TESTS_ELSE_PRIVATE: // pipelines
   /** \brief incoming Interest pipeline
    */
-  void
+  VIRTUAL_WITH_TESTS void
   onIncomingInterest(Face& inFace, const Interest& interest);
 
   /** \brief Interest loop pipeline
    */
-  void
+  VIRTUAL_WITH_TESTS void
   onInterestLoop(Face& inFace, const Interest& interest,
                  shared_ptr<pit::Entry> pitEntry);
   
   /** \brief outgoing Interest pipeline
    */
-  void
+  VIRTUAL_WITH_TESTS void
   onOutgoingInterest(shared_ptr<pit::Entry> pitEntry, Face& outFace);
   
   /** \brief Interest rebuff pipeline
    */
-  void
+  VIRTUAL_WITH_TESTS void
   onInterestRebuff(shared_ptr<pit::Entry> pitEntry);
   
   /** \brief Interest unsatisfied pipeline
    */
-  void
+  VIRTUAL_WITH_TESTS void
   onInterestUnsatisfied(shared_ptr<pit::Entry> pitEntry);
   
   /** \brief incoming Data pipeline
    */
-  void
+  VIRTUAL_WITH_TESTS void
   onIncomingData(Face& inFace, const Data& data);
   
   /** \brief Data unsolicited pipeline
    */
-  void
+  VIRTUAL_WITH_TESTS void
   onDataUnsolicited(Face& inFace, const Data& data);
   
   /** \brief outgoing Data pipeline
    */
-  void
+  VIRTUAL_WITH_TESTS void
   onOutgoingData(const Data& data, Face& outFace);
 
-private:
-  void
+PROTECTED_WITH_TESTS_ELSE_PRIVATE:
+  VIRTUAL_WITH_TESTS void
   setUnsatisfyTimer(shared_ptr<pit::Entry> pitEntry);
   
-  void
+  VIRTUAL_WITH_TESTS void
   setStragglerTimer(shared_ptr<pit::Entry> pitEntry);
   
-  void
+  VIRTUAL_WITH_TESTS void
   cancelUnsatisfyAndStragglerTimer(shared_ptr<pit::Entry> pitEntry);
+  
+  VIRTUAL_WITH_TESTS void
+  dispatchToStrategy(const Face& inFace,
+                     const Interest& interest,
+                     shared_ptr<fib::Entry> fibEntry,
+                     shared_ptr<pit::Entry> pitEntry);
 
 private:
   Scheduler m_scheduler;
@@ -116,6 +123,8 @@
   /// the active strategy (only one strategy in mock)
   shared_ptr<fw::Strategy> m_strategy;
   
+  static const Name s_localhostName;
+  
   // allow Strategy (base class) to enter pipelines
   friend class fw::Strategy;
 };