fw: Add default HopLimit to Interest when missing

Refs: #5171
Change-Id: I5973811b5ca7c7cf5ff344872afa51b253b9cae8
diff --git a/daemon/fw/forwarder.cpp b/daemon/fw/forwarder.cpp
index fea18f6..3683734 100644
--- a/daemon/fw/forwarder.cpp
+++ b/daemon/fw/forwarder.cpp
@@ -40,6 +40,8 @@
 
 NFD_LOG_INIT(Forwarder);
 
+const std::string CFGSEC_FORWARDER = "forwarder";
+
 static Name
 getDefaultStrategyName()
 {
@@ -186,6 +188,11 @@
   NFD_LOG_DEBUG("onContentStoreMiss interest=" << interest.getName());
   ++m_counters.nCsMisses;
 
+  // attach HopLimit if configured and not present in Interest
+  if (m_config.defaultHopLimit > 0 && !interest.getHopLimit()) {
+    const_cast<Interest&>(interest).setHopLimit(m_config.defaultHopLimit);
+  }
+
   // insert in-record
   pitEntry->insertOrUpdateInRecord(ingress.face, interest);
 
@@ -597,4 +604,30 @@
   }
 }
 
+void
+Forwarder::setConfigFile(ConfigFile& configFile)
+{
+  configFile.addSectionHandler(CFGSEC_FORWARDER, bind(&Forwarder::processConfig, this, _1, _2, _3));
+}
+
+void
+Forwarder::processConfig(const ConfigSection& configSection, bool isDryRun, const std::string&)
+{
+  Config config;
+
+  for (const auto& pair : configSection) {
+    const std::string& key = pair.first;
+    if (key == "default_hop_limit") {
+      config.defaultHopLimit = ConfigFile::parseNumber<uint8_t>(pair, CFGSEC_FORWARDER);
+    }
+    else {
+      NDN_THROW(ConfigFile::Error("Unrecognized option " + CFGSEC_FORWARDER + "." + key));
+    }
+  }
+
+  if (!isDryRun) {
+    m_config = config;
+  }
+}
+
 } // namespace nfd
diff --git a/daemon/fw/forwarder.hpp b/daemon/fw/forwarder.hpp
index 6a245de..47c6e03 100644
--- a/daemon/fw/forwarder.hpp
+++ b/daemon/fw/forwarder.hpp
@@ -29,6 +29,7 @@
 #include "face-table.hpp"
 #include "forwarder-counters.hpp"
 #include "unsolicited-data-policy.hpp"
+#include "common/config-file.hpp"
 #include "face/face-endpoint.hpp"
 #include "table/fib.hpp"
 #include "table/pit.hpp"
@@ -125,6 +126,11 @@
     return m_networkRegionTable;
   }
 
+  /** \brief register handler for forwarder section of NFD configuration file
+   */
+  void
+  setConfigFile(ConfigFile& configFile);
+
 NFD_PUBLIC_WITH_TESTS_ELSE_PRIVATE: // pipelines
   /** \brief incoming Interest pipeline
    *  \param interest the incoming Interest, must be well-formed and created with make_shared
@@ -213,6 +219,22 @@
   void
   insertDeadNonceList(pit::Entry& pitEntry, const Face* upstream);
 
+  void
+  processConfig(const ConfigSection& configSection, bool isDryRun,
+                const std::string& filename);
+
+NFD_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
+  /**
+   * \brief Configuration options from "forwarder" section
+   */
+  struct Config
+  {
+    /// Initial value of HopLimit that should be added to Interests that don't have one.
+    /// A value of zero disables the feature.
+    uint8_t defaultHopLimit = 0;
+  };
+  Config m_config;
+
 private:
   ForwarderCounters m_counters;
 
diff --git a/daemon/nfd.cpp b/daemon/nfd.cpp
index e07b5d8..a178fe4 100644
--- a/daemon/nfd.cpp
+++ b/daemon/nfd.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2019,  Regents of the University of California,
+ * Copyright (c) 2014-2021,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -150,6 +150,8 @@
   ConfigFile config(&ignoreRibAndLogSections);
   general::setConfigFile(config);
 
+  m_forwarder->setConfigFile(config);
+
   TablesConfigSection tablesConfig(*m_forwarder);
   tablesConfig.setConfigFile(config);
 
@@ -183,6 +185,8 @@
   ConfigFile config(&ignoreRibAndLogSections);
   general::setConfigFile(config);
 
+  m_forwarder->setConfigFile(config);
+
   TablesConfigSection tablesConfig(*m_forwarder);
   tablesConfig.setConfigFile(config);