rib: move entire subdir to daemon/rib

Refs: #4528
Change-Id: I7de03631ddef0f014f12f979373aa449f42486d1
diff --git a/daemon/rib/readvertise/client-to-nlsr-readvertise-policy.cpp b/daemon/rib/readvertise/client-to-nlsr-readvertise-policy.cpp
new file mode 100644
index 0000000..ca54132
--- /dev/null
+++ b/daemon/rib/readvertise/client-to-nlsr-readvertise-policy.cpp
@@ -0,0 +1,49 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2014-2018,  Regents of the University of California,
+ *                           Arizona Board of Regents,
+ *                           Colorado State University,
+ *                           University Pierre & Marie Curie, Sorbonne University,
+ *                           Washington University in St. Louis,
+ *                           Beijing Institute of Technology,
+ *                           The University of Memphis.
+ *
+ * This file is part of NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "client-to-nlsr-readvertise-policy.hpp"
+
+namespace nfd {
+namespace rib {
+
+optional<ReadvertiseAction>
+ClientToNlsrReadvertisePolicy::handleNewRoute(const RibRouteRef& ribRoute) const
+{
+  if (ribRoute.route->origin == ndn::nfd::ROUTE_ORIGIN_CLIENT) {
+    return ReadvertiseAction{ribRoute.entry->getName(), ndn::security::SigningInfo()};
+  }
+  else {
+    return nullopt;
+  }
+}
+
+time::milliseconds
+ClientToNlsrReadvertisePolicy::getRefreshInterval() const
+{
+  return 1_h;
+}
+
+} // namespace rib
+} // namespace nfd
diff --git a/daemon/rib/readvertise/client-to-nlsr-readvertise-policy.hpp b/daemon/rib/readvertise/client-to-nlsr-readvertise-policy.hpp
new file mode 100644
index 0000000..b1c5e29
--- /dev/null
+++ b/daemon/rib/readvertise/client-to-nlsr-readvertise-policy.hpp
@@ -0,0 +1,54 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2014-2019,  Regents of the University of California,
+ *                           Arizona Board of Regents,
+ *                           Colorado State University,
+ *                           University Pierre & Marie Curie, Sorbonne University,
+ *                           Washington University in St. Louis,
+ *                           Beijing Institute of Technology,
+ *                           The University of Memphis.
+ *
+ * This file is part of NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef NFD_DAEMON_RIB_READVERTISE_CLIENT_TO_NLSR_READVERTISE_POLICY_HPP
+#define NFD_DAEMON_RIB_READVERTISE_CLIENT_TO_NLSR_READVERTISE_POLICY_HPP
+
+#include "readvertise-policy.hpp"
+
+namespace nfd {
+namespace rib {
+
+/** \brief a policy to readvertise routes registered by end hosts into NLSR
+ */
+class ClientToNlsrReadvertisePolicy : public ReadvertisePolicy
+{
+public:
+  /** \brief advertise if the route's origin is client
+   *
+   *  If the route origin is "client" (typically from auto prefix propagation), readvertise it
+   *  using the default signing identity.
+   */
+  optional<ReadvertiseAction>
+  handleNewRoute(const RibRouteRef& ribRoute) const override;
+
+  time::milliseconds
+  getRefreshInterval() const override;
+};
+
+} // namespace rib
+} // namespace nfd
+
+#endif // NFD_DAEMON_RIB_READVERTISE_CLIENT_TO_NLSR_READVERTISE_POLICY_HPP
diff --git a/daemon/rib/readvertise/host-to-gateway-readvertise-policy.cpp b/daemon/rib/readvertise/host-to-gateway-readvertise-policy.cpp
new file mode 100644
index 0000000..2d508f8
--- /dev/null
+++ b/daemon/rib/readvertise/host-to-gateway-readvertise-policy.cpp
@@ -0,0 +1,91 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2014-2018,  Regents of the University of California,
+ *                           Arizona Board of Regents,
+ *                           Colorado State University,
+ *                           University Pierre & Marie Curie, Sorbonne University,
+ *                           Washington University in St. Louis,
+ *                           Beijing Institute of Technology,
+ *                           The University of Memphis.
+ *
+ * This file is part of NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "host-to-gateway-readvertise-policy.hpp"
+#include "core/scope-prefix.hpp"
+#include "rib/rib-manager.hpp"
+
+#include <ndn-cxx/security/pib/identity.hpp>
+#include <ndn-cxx/security/signing-helpers.hpp>
+
+namespace nfd {
+namespace rib {
+
+static const name::Component IGNORE_COMPONENT("nrd");
+static const time::seconds DEFAULT_REFRESH_INTERVAL = 25_s;
+
+HostToGatewayReadvertisePolicy::HostToGatewayReadvertisePolicy(const ndn::KeyChain& keyChain,
+                                                               const ConfigSection& section)
+  : m_keyChain(keyChain)
+{
+  auto interval = section.get_optional<uint64_t>("refresh_interval");
+  m_refreshInterval = interval ? time::seconds(*interval) : DEFAULT_REFRESH_INTERVAL;
+}
+
+optional<ReadvertiseAction>
+HostToGatewayReadvertisePolicy::handleNewRoute(const RibRouteRef& ribRoute) const
+{
+  auto ribEntryName = ribRoute.entry->getName();
+  if (scope_prefix::LOCALHOST.isPrefixOf(ribEntryName) ||
+      ribEntryName == RibManager::LOCALHOP_TOP_PREFIX) {
+    return nullopt;
+  }
+
+  // find out the shortest identity whose name is a prefix of the RIB entry name
+  auto prefixToAdvertise = ribEntryName;
+  ndn::security::pib::Identity signingIdentity;
+  bool isFound = false;
+
+  for (const auto& identity : m_keyChain.getPib().getIdentities()) {
+    auto prefix = identity.getName();
+
+    // ignore the identity name's last component if it is "nrd"
+    if (!prefix.empty() && IGNORE_COMPONENT == prefix.at(-1)) {
+      prefix = prefix.getPrefix(-1);
+    }
+
+    if (prefix.isPrefixOf(prefixToAdvertise)) {
+      isFound = true;
+      prefixToAdvertise = prefix;
+      signingIdentity = identity;
+    }
+  }
+
+  if (isFound) {
+    return ReadvertiseAction{prefixToAdvertise, ndn::security::signingByIdentity(signingIdentity)};
+  }
+  else {
+    return nullopt;
+  }
+}
+
+time::milliseconds
+HostToGatewayReadvertisePolicy::getRefreshInterval() const
+{
+  return m_refreshInterval;
+}
+
+} // namespace rib
+} // namespace nfd
diff --git a/daemon/rib/readvertise/host-to-gateway-readvertise-policy.hpp b/daemon/rib/readvertise/host-to-gateway-readvertise-policy.hpp
new file mode 100644
index 0000000..69e914a
--- /dev/null
+++ b/daemon/rib/readvertise/host-to-gateway-readvertise-policy.hpp
@@ -0,0 +1,60 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2014-2019,  Regents of the University of California,
+ *                           Arizona Board of Regents,
+ *                           Colorado State University,
+ *                           University Pierre & Marie Curie, Sorbonne University,
+ *                           Washington University in St. Louis,
+ *                           Beijing Institute of Technology,
+ *                           The University of Memphis.
+ *
+ * This file is part of NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef NFD_DAEMON_RIB_READVERTISE_HOST_TO_GATEWAY_READVERTISE_POLICY_HPP
+#define NFD_DAEMON_RIB_READVERTISE_HOST_TO_GATEWAY_READVERTISE_POLICY_HPP
+
+#include "readvertise-policy.hpp"
+#include "core/config-file.hpp"
+
+#include <ndn-cxx/security/key-chain.hpp>
+
+namespace nfd {
+namespace rib {
+
+/** \brief a policy to readvertise routes registered by local applications into remote gateway
+ */
+class HostToGatewayReadvertisePolicy : public ReadvertisePolicy
+{
+public:
+  HostToGatewayReadvertisePolicy(const ndn::KeyChain& keyChain,
+                                 const ConfigSection& section);
+
+public:
+  optional<ReadvertiseAction>
+  handleNewRoute(const RibRouteRef& ribRoute) const override;
+
+  time::milliseconds
+  getRefreshInterval() const override;
+
+private:
+  const ndn::KeyChain& m_keyChain;
+  time::seconds m_refreshInterval;
+};
+
+} // namespace rib
+} // namespace nfd
+
+#endif // NFD_DAEMON_RIB_READVERTISE_HOST_TO_GATEWAY_READVERTISE_POLICY_HPP
diff --git a/daemon/rib/readvertise/nfd-rib-readvertise-destination.cpp b/daemon/rib/readvertise/nfd-rib-readvertise-destination.cpp
new file mode 100644
index 0000000..f0a0482
--- /dev/null
+++ b/daemon/rib/readvertise/nfd-rib-readvertise-destination.cpp
@@ -0,0 +1,110 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2014-2018,  Regents of the University of California,
+ *                           Arizona Board of Regents,
+ *                           Colorado State University,
+ *                           University Pierre & Marie Curie, Sorbonne University,
+ *                           Washington University in St. Louis,
+ *                           Beijing Institute of Technology,
+ *                           The University of Memphis.
+ *
+ * This file is part of NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "nfd-rib-readvertise-destination.hpp"
+#include "core/logger.hpp"
+
+#include <ndn-cxx/mgmt/nfd/control-command.hpp>
+#include <ndn-cxx/mgmt/nfd/control-response.hpp>
+
+namespace nfd {
+namespace rib {
+
+NFD_LOG_INIT(NfdRibReadvertiseDestination);
+
+using ndn::nfd::ControlResponse;
+
+NfdRibReadvertiseDestination::NfdRibReadvertiseDestination(ndn::nfd::Controller& controller,
+                                                           Rib& rib,
+                                                           const ndn::nfd::CommandOptions& options,
+                                                           const ndn::nfd::ControlParameters& parameters)
+  : m_controller(controller)
+  , m_commandOptions(options)
+  , m_controlParameters(parameters)
+{
+  m_ribInsertConn = rib.afterInsertEntry.connect(
+    std::bind(&NfdRibReadvertiseDestination::handleRibInsert, this, _1));
+  m_ribEraseConn = rib.afterEraseEntry.connect(
+    std::bind(&NfdRibReadvertiseDestination::handleRibErase, this, _1));
+}
+
+void
+NfdRibReadvertiseDestination::advertise(const nfd::rib::ReadvertisedRoute& rr,
+                                        std::function<void()> successCb,
+                                        std::function<void(const std::string&)> failureCb)
+{
+  NFD_LOG_DEBUG("advertise " << rr.prefix << " on " << m_commandOptions.getPrefix());
+
+  m_controller.start<ndn::nfd::RibRegisterCommand>(
+    getControlParameters().setName(rr.prefix),
+    [=] (const ControlParameters& cp) { successCb(); },
+    [=] (const ControlResponse& cr) { failureCb(cr.getText()); },
+    getCommandOptions().setSigningInfo(rr.signer));
+}
+
+void
+NfdRibReadvertiseDestination::withdraw(const nfd::rib::ReadvertisedRoute& rr,
+                                       std::function<void()> successCb,
+                                       std::function<void(const std::string&)> failureCb)
+{
+  NFD_LOG_DEBUG("withdraw " << rr.prefix << " on " << m_commandOptions.getPrefix());
+
+  m_controller.start<ndn::nfd::RibUnregisterCommand>(
+    getControlParameters().setName(rr.prefix),
+    [=] (const ControlParameters& cp) { successCb(); },
+    [=] (const ControlResponse& cr) { failureCb(cr.getText()); },
+    getCommandOptions().setSigningInfo(rr.signer));
+}
+
+ndn::nfd::ControlParameters
+NfdRibReadvertiseDestination::getControlParameters()
+{
+  return m_controlParameters;
+}
+
+ndn::nfd::CommandOptions
+NfdRibReadvertiseDestination::getCommandOptions()
+{
+  return m_commandOptions;
+}
+
+void
+NfdRibReadvertiseDestination::handleRibInsert(const ndn::Name& name)
+{
+  if (name.isPrefixOf(m_commandOptions.getPrefix())) {
+    setAvailability(true);
+  }
+}
+
+void
+NfdRibReadvertiseDestination::handleRibErase(const ndn::Name& name)
+{
+  if (name.isPrefixOf(m_commandOptions.getPrefix())) {
+    setAvailability(false);
+  }
+}
+
+} // namespace rib
+} // namespace nfd
diff --git a/daemon/rib/readvertise/nfd-rib-readvertise-destination.hpp b/daemon/rib/readvertise/nfd-rib-readvertise-destination.hpp
new file mode 100644
index 0000000..9c5bce6
--- /dev/null
+++ b/daemon/rib/readvertise/nfd-rib-readvertise-destination.hpp
@@ -0,0 +1,92 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2014-2019,  Regents of the University of California,
+ *                           Arizona Board of Regents,
+ *                           Colorado State University,
+ *                           University Pierre & Marie Curie, Sorbonne University,
+ *                           Washington University in St. Louis,
+ *                           Beijing Institute of Technology,
+ *                           The University of Memphis.
+ *
+ * This file is part of NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef NFD_DAEMON_RIB_READVERTISE_NFD_RIB_READVERTISE_DESTINATION_HPP
+#define NFD_DAEMON_RIB_READVERTISE_NFD_RIB_READVERTISE_DESTINATION_HPP
+
+#include "readvertise-destination.hpp"
+#include "rib/rib.hpp"
+
+#include <ndn-cxx/mgmt/nfd/command-options.hpp>
+#include <ndn-cxx/mgmt/nfd/controller.hpp>
+#include <ndn-cxx/mgmt/nfd/control-parameters.hpp>
+
+namespace nfd {
+namespace rib {
+
+/** \brief a readvertise destination using NFD RIB management protocol
+ */
+class NfdRibReadvertiseDestination : public ReadvertiseDestination
+{
+public:
+  NfdRibReadvertiseDestination(ndn::nfd::Controller& controller,
+                               Rib& rib,
+                               const ndn::nfd::CommandOptions& options = ndn::nfd::CommandOptions(),
+                               const ndn::nfd::ControlParameters& parameters =
+                                 ndn::nfd::ControlParameters()
+                                   .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT));
+
+  /** \brief add a name prefix into NFD RIB
+   */
+  void
+  advertise(const ReadvertisedRoute& rr,
+            std::function<void()> successCb,
+            std::function<void(const std::string&)> failureCb) override;
+
+  /** \brief remove a name prefix from NFD RIB
+   */
+  void
+  withdraw(const ReadvertisedRoute& rr,
+           std::function<void()> successCb,
+           std::function<void(const std::string&)> failureCb) override;
+
+protected:
+  ndn::nfd::ControlParameters
+  getControlParameters();
+
+  ndn::nfd::CommandOptions
+  getCommandOptions();
+
+private:
+  void
+  handleRibInsert(const Name& name);
+
+  void
+  handleRibErase(const Name& name);
+
+private:
+  ndn::nfd::Controller& m_controller;
+
+  signal::ScopedConnection m_ribInsertConn;
+  signal::ScopedConnection m_ribEraseConn;
+
+  ndn::nfd::CommandOptions m_commandOptions;
+  ndn::nfd::ControlParameters m_controlParameters;
+};
+
+} // namespace rib
+} // namespace nfd
+
+#endif // NFD_DAEMON_RIB_READVERTISE_NFD_RIB_READVERTISE_DESTINATION_HPP
diff --git a/daemon/rib/readvertise/readvertise-destination.cpp b/daemon/rib/readvertise/readvertise-destination.cpp
new file mode 100644
index 0000000..a687a8a
--- /dev/null
+++ b/daemon/rib/readvertise/readvertise-destination.cpp
@@ -0,0 +1,50 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2014-2018,  Regents of the University of California,
+ *                           Arizona Board of Regents,
+ *                           Colorado State University,
+ *                           University Pierre & Marie Curie, Sorbonne University,
+ *                           Washington University in St. Louis,
+ *                           Beijing Institute of Technology,
+ *                           The University of Memphis.
+ *
+ * This file is part of NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "readvertise-destination.hpp"
+#include "core/logger.hpp"
+
+namespace nfd {
+namespace rib {
+
+NFD_LOG_INIT(ReadvertiseDestination);
+
+void
+ReadvertiseDestination::setAvailability(bool isAvailable)
+{
+  if (m_isAvailable != isAvailable) {
+    if (isAvailable) {
+      NFD_LOG_DEBUG("Destination has become available.");
+    }
+    else {
+      NFD_LOG_DEBUG("Destinatino has become unavailable.");
+    }
+    m_isAvailable = isAvailable;
+    afterAvailabilityChange(isAvailable);
+  }
+}
+
+} // namespace rib
+} // namespace nfd
diff --git a/daemon/rib/readvertise/readvertise-destination.hpp b/daemon/rib/readvertise/readvertise-destination.hpp
new file mode 100644
index 0000000..1c4fb76
--- /dev/null
+++ b/daemon/rib/readvertise/readvertise-destination.hpp
@@ -0,0 +1,74 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2014-2019,  Regents of the University of California,
+ *                           Arizona Board of Regents,
+ *                           Colorado State University,
+ *                           University Pierre & Marie Curie, Sorbonne University,
+ *                           Washington University in St. Louis,
+ *                           Beijing Institute of Technology,
+ *                           The University of Memphis.
+ *
+ * This file is part of NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef NFD_DAEMON_RIB_READVERTISE_READVERTISE_DESTINATION_HPP
+#define NFD_DAEMON_RIB_READVERTISE_READVERTISE_DESTINATION_HPP
+
+#include "readvertised-route.hpp"
+
+namespace nfd {
+namespace rib {
+
+/** \brief a destination to readvertise into
+ */
+class ReadvertiseDestination : noncopyable
+{
+public:
+  virtual
+  ~ReadvertiseDestination() = default;
+
+  virtual void
+  advertise(const ReadvertisedRoute& rr,
+            std::function<void()> successCb,
+            std::function<void(const std::string&)> failureCb) = 0;
+
+  virtual void
+  withdraw(const ReadvertisedRoute& rr,
+           std::function<void()> successCb,
+           std::function<void(const std::string&)> failureCb) = 0;
+
+  bool
+  isAvailable() const
+  {
+    return m_isAvailable;
+  }
+
+protected:
+  void
+  setAvailability(bool isAvailable);
+
+public:
+  /** \brief signals when the destination becomes available or unavailable
+   */
+  signal::Signal<ReadvertiseDestination, bool> afterAvailabilityChange;
+
+private:
+  bool m_isAvailable = false;
+};
+
+} // namespace rib
+} // namespace nfd
+
+#endif // NFD_DAEMON_RIB_READVERTISE_READVERTISE_DESTINATION_HPP
diff --git a/daemon/rib/readvertise/readvertise-policy.hpp b/daemon/rib/readvertise/readvertise-policy.hpp
new file mode 100644
index 0000000..99deccb
--- /dev/null
+++ b/daemon/rib/readvertise/readvertise-policy.hpp
@@ -0,0 +1,66 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2014-2019,  Regents of the University of California,
+ *                           Arizona Board of Regents,
+ *                           Colorado State University,
+ *                           University Pierre & Marie Curie, Sorbonne University,
+ *                           Washington University in St. Louis,
+ *                           Beijing Institute of Technology,
+ *                           The University of Memphis.
+ *
+ * This file is part of NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef NFD_DAEMON_RIB_READVERTISE_READVERTISE_POLICY_HPP
+#define NFD_DAEMON_RIB_READVERTISE_READVERTISE_POLICY_HPP
+
+#include "rib/rib.hpp"
+
+#include <ndn-cxx/security/signing-info.hpp>
+
+namespace nfd {
+namespace rib {
+
+/** \brief a decision made by readvertise policy
+ */
+struct ReadvertiseAction
+{
+  Name prefix; ///< the prefix that should be readvertised
+  ndn::security::SigningInfo signer; ///< credentials for command signing
+};
+
+/** \brief a policy to decide whether to readvertise a route, and what prefix to readvertise
+ */
+class ReadvertisePolicy : noncopyable
+{
+public:
+  virtual
+  ~ReadvertisePolicy() = default;
+
+  /** \brief decide whether to readvertise a route, and what prefix to readvertise
+   */
+  virtual optional<ReadvertiseAction>
+  handleNewRoute(const RibRouteRef& ribRoute) const = 0;
+
+  /** \return how often readvertisements made by this policy should be refreshed.
+   */
+  virtual time::milliseconds
+  getRefreshInterval() const = 0;
+};
+
+} // namespace rib
+} // namespace nfd
+
+#endif // NFD_DAEMON_RIB_READVERTISE_READVERTISE_POLICY_HPP
diff --git a/daemon/rib/readvertise/readvertise.cpp b/daemon/rib/readvertise/readvertise.cpp
new file mode 100644
index 0000000..454ab20
--- /dev/null
+++ b/daemon/rib/readvertise/readvertise.cpp
@@ -0,0 +1,201 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2014-2019,  Regents of the University of California,
+ *                           Arizona Board of Regents,
+ *                           Colorado State University,
+ *                           University Pierre & Marie Curie, Sorbonne University,
+ *                           Washington University in St. Louis,
+ *                           Beijing Institute of Technology,
+ *                           The University of Memphis.
+ *
+ * This file is part of NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "readvertise.hpp"
+#include "core/logger.hpp"
+
+#include <ndn-cxx/util/random.hpp>
+
+namespace nfd {
+namespace rib {
+
+NFD_LOG_INIT(Readvertise);
+
+const time::milliseconds Readvertise::RETRY_DELAY_MIN = 50_s;
+const time::milliseconds Readvertise::RETRY_DELAY_MAX = 3600_s;
+
+static time::milliseconds
+randomizeTimer(time::milliseconds baseTimer)
+{
+  std::uniform_int_distribution<> dist(-5, 5);
+  auto newTime = baseTimer + time::milliseconds(dist(ndn::random::getRandomNumberEngine()));
+  return std::max(newTime, 0_ms);
+}
+
+Readvertise::Readvertise(Rib& rib, ndn::util::Scheduler& scheduler,
+                         unique_ptr<ReadvertisePolicy> policy,
+                         unique_ptr<ReadvertiseDestination> destination)
+  : m_scheduler(scheduler)
+  , m_policy(std::move(policy))
+  , m_destination(std::move(destination))
+{
+  m_addRouteConn = rib.afterAddRoute.connect([this] (const auto& r) { this->afterAddRoute(r); });
+  m_removeRouteConn = rib.beforeRemoveRoute.connect([this] (const auto& r) { this->beforeRemoveRoute(r); });
+
+  m_destination->afterAvailabilityChange.connect([this] (bool isAvailable) {
+    if (isAvailable) {
+      this->afterDestinationAvailable();
+    }
+    else {
+      this->afterDestinationUnavailable();
+    }
+  });
+}
+
+void
+Readvertise::afterAddRoute(const RibRouteRef& ribRoute)
+{
+  optional<ReadvertiseAction> action = m_policy->handleNewRoute(ribRoute);
+  if (!action) {
+    NFD_LOG_DEBUG("add-route " << ribRoute.entry->getName() << '(' << ribRoute.route->faceId <<
+                  ',' << ribRoute.route->origin << ") not-readvertising");
+    return;
+  }
+
+  ReadvertisedRouteContainer::iterator rrIt;
+  bool isNew = false;
+  std::tie(rrIt, isNew) = m_rrs.emplace(action->prefix);
+
+  if (!isNew && rrIt->signer != action->signer) {
+    NFD_LOG_WARN("add-route " << ribRoute.entry->getName() << '(' << ribRoute.route->faceId <<
+                  ',' << ribRoute.route->origin << ") readvertising-as " << action->prefix <<
+                 " old-signer " << rrIt->signer << " new-signer " << action->signer);
+  }
+  rrIt->signer = action->signer;
+
+  RouteRrIndex::iterator indexIt;
+  std::tie(indexIt, isNew) = m_routeToRr.emplace(ribRoute, rrIt);
+  BOOST_ASSERT(isNew);
+
+  if (rrIt->nRibRoutes++ > 0) {
+    NFD_LOG_DEBUG("add-route " << ribRoute.entry->getName() << '(' << ribRoute.route->faceId <<
+                  ',' << ribRoute.route->origin << ") already-readvertised-as " << action->prefix);
+    return;
+  }
+
+  NFD_LOG_DEBUG("add-route " << ribRoute.entry->getName() << '(' << ribRoute.route->faceId <<
+                ',' << ribRoute.route->origin << ") readvertising-as " << action->prefix <<
+                " signer " << action->signer);
+  rrIt->retryDelay = RETRY_DELAY_MIN;
+  this->advertise(rrIt);
+}
+
+void
+Readvertise::beforeRemoveRoute(const RibRouteRef& ribRoute)
+{
+  auto indexIt = m_routeToRr.find(ribRoute);
+  if (indexIt == m_routeToRr.end()) {
+    NFD_LOG_DEBUG("remove-route " << ribRoute.entry->getName() << '(' << ribRoute.route->faceId <<
+                  ',' << ribRoute.route->origin << ") not-readvertised");
+    return;
+  }
+
+  auto rrIt = indexIt->second;
+  m_routeToRr.erase(indexIt);
+
+  if (--rrIt->nRibRoutes > 0) {
+    NFD_LOG_DEBUG("remove-route " << ribRoute.entry->getName() << '(' << ribRoute.route->faceId <<
+                  ',' << ribRoute.route->origin << ") needed-by " << rrIt->nRibRoutes);
+    return;
+  }
+
+  rrIt->retryDelay = RETRY_DELAY_MIN;
+  this->withdraw(rrIt);
+}
+
+void
+Readvertise::afterDestinationAvailable()
+{
+  for (auto rrIt = m_rrs.begin(); rrIt != m_rrs.end(); ++rrIt) {
+    rrIt->retryDelay = RETRY_DELAY_MIN;
+    this->advertise(rrIt);
+  }
+}
+
+void
+Readvertise::afterDestinationUnavailable()
+{
+  for (auto rrIt = m_rrs.begin(); rrIt != m_rrs.end();) {
+    if (rrIt->nRibRoutes > 0) {
+      rrIt->retryEvt.cancel(); // stop retrying or refreshing
+      ++rrIt;
+    }
+    else {
+      rrIt = m_rrs.erase(rrIt); // assume withdraw has completed
+    }
+  }
+}
+
+void
+Readvertise::advertise(ReadvertisedRouteContainer::iterator rrIt)
+{
+  BOOST_ASSERT(rrIt->nRibRoutes > 0);
+
+  if (!m_destination->isAvailable()) {
+    NFD_LOG_DEBUG("advertise " << rrIt->prefix << " destination-unavailable");
+    return;
+  }
+
+  m_destination->advertise(*rrIt,
+    [=] {
+      NFD_LOG_DEBUG("advertise " << rrIt->prefix << " success");
+      rrIt->retryDelay = RETRY_DELAY_MIN;
+      rrIt->retryEvt = m_scheduler.scheduleEvent(randomizeTimer(m_policy->getRefreshInterval()),
+                                                 [=] { advertise(rrIt); });
+    },
+    [=] (const std::string& msg) {
+      NFD_LOG_DEBUG("advertise " << rrIt->prefix << " failure " << msg);
+      rrIt->retryDelay = std::min(RETRY_DELAY_MAX, rrIt->retryDelay * 2);
+      rrIt->retryEvt = m_scheduler.scheduleEvent(randomizeTimer(rrIt->retryDelay),
+                                                 [=] { advertise(rrIt); });
+    });
+}
+
+void
+Readvertise::withdraw(ReadvertisedRouteContainer::iterator rrIt)
+{
+  BOOST_ASSERT(rrIt->nRibRoutes == 0);
+
+  if (!m_destination->isAvailable()) {
+    NFD_LOG_DEBUG("withdraw " << rrIt->prefix << " destination-unavailable");
+    m_rrs.erase(rrIt);
+    return;
+  }
+
+  m_destination->withdraw(*rrIt,
+    [=] {
+      NFD_LOG_DEBUG("withdraw " << rrIt->prefix << " success");
+      m_rrs.erase(rrIt);
+    },
+    [=] (const std::string& msg) {
+      NFD_LOG_DEBUG("withdraw " << rrIt->prefix << " failure " << msg);
+      rrIt->retryDelay = std::min(RETRY_DELAY_MAX, rrIt->retryDelay * 2);
+      rrIt->retryEvt = m_scheduler.scheduleEvent(randomizeTimer(rrIt->retryDelay),
+                                                 [=] { withdraw(rrIt); });
+    });
+}
+
+} // namespace rib
+} // namespace nfd
diff --git a/daemon/rib/readvertise/readvertise.hpp b/daemon/rib/readvertise/readvertise.hpp
new file mode 100644
index 0000000..80dbbc7
--- /dev/null
+++ b/daemon/rib/readvertise/readvertise.hpp
@@ -0,0 +1,94 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2014-2019,  Regents of the University of California,
+ *                           Arizona Board of Regents,
+ *                           Colorado State University,
+ *                           University Pierre & Marie Curie, Sorbonne University,
+ *                           Washington University in St. Louis,
+ *                           Beijing Institute of Technology,
+ *                           The University of Memphis.
+ *
+ * This file is part of NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef NFD_DAEMON_RIB_READVERTISE_READVERTISE_HPP
+#define NFD_DAEMON_RIB_READVERTISE_READVERTISE_HPP
+
+#include "readvertise-destination.hpp"
+#include "readvertise-policy.hpp"
+#include "readvertised-route.hpp"
+#include "rib/rib.hpp"
+
+namespace nfd {
+namespace rib {
+
+/** \brief readvertise a subset of routes to a destination according to a policy
+ *
+ *  The Readvertise class allows RIB routes to be readvertised to a destination such as a routing
+ *  protocol daemon or another NFD-RIB. It monitors the RIB for route additions and removals,
+ *  asks the ReadvertisePolicy to make decision on whether to readvertise each new route and what
+ *  prefix to readvertise as, and invokes a ReadvertiseDestination to send the commands.
+ */
+class Readvertise : noncopyable
+{
+
+public:
+  Readvertise(Rib& rib,
+              ndn::util::Scheduler& scheduler,
+              unique_ptr<ReadvertisePolicy> policy,
+              unique_ptr<ReadvertiseDestination> destination);
+
+private:
+  void
+  afterAddRoute(const RibRouteRef& ribRoute);
+
+  void
+  beforeRemoveRoute(const RibRouteRef& ribRoute);
+
+  void
+  afterDestinationAvailable();
+
+  void
+  afterDestinationUnavailable();
+
+  void
+  advertise(ReadvertisedRouteContainer::iterator rrIt);
+
+  void
+  withdraw(ReadvertisedRouteContainer::iterator rrIt);
+
+private:
+  /** \brief maps from RIB route to readvertised route derived from RIB route(s)
+   */
+  using RouteRrIndex = std::map<RibRouteRef, ReadvertisedRouteContainer::iterator>;
+
+  static const time::milliseconds RETRY_DELAY_MIN;
+  static const time::milliseconds RETRY_DELAY_MAX;
+
+  ndn::util::Scheduler& m_scheduler;
+  unique_ptr<ReadvertisePolicy> m_policy;
+  unique_ptr<ReadvertiseDestination> m_destination;
+
+  ReadvertisedRouteContainer m_rrs;
+  RouteRrIndex m_routeToRr;
+
+  signal::ScopedConnection m_addRouteConn;
+  signal::ScopedConnection m_removeRouteConn;
+};
+
+} // namespace rib
+} // namespace nfd
+
+#endif // NFD_DAEMON_RIB_READVERTISE_READVERTISE_HPP
diff --git a/daemon/rib/readvertise/readvertised-route.hpp b/daemon/rib/readvertise/readvertised-route.hpp
new file mode 100644
index 0000000..7dc02ae
--- /dev/null
+++ b/daemon/rib/readvertise/readvertised-route.hpp
@@ -0,0 +1,69 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2014-2019,  Regents of the University of California,
+ *                           Arizona Board of Regents,
+ *                           Colorado State University,
+ *                           University Pierre & Marie Curie, Sorbonne University,
+ *                           Washington University in St. Louis,
+ *                           Beijing Institute of Technology,
+ *                           The University of Memphis.
+ *
+ * This file is part of NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef NFD_DAEMON_RIB_READVERTISE_READVERTISED_ROUTE_HPP
+#define NFD_DAEMON_RIB_READVERTISE_READVERTISED_ROUTE_HPP
+
+#include "core/common.hpp"
+
+#include <ndn-cxx/security/signing-info.hpp>
+#include <ndn-cxx/util/scheduler.hpp>
+
+namespace nfd {
+namespace rib {
+
+/** \brief state of a readvertised route
+ */
+class ReadvertisedRoute : noncopyable
+{
+public:
+  explicit
+  ReadvertisedRoute(const Name& prefix)
+    : prefix(prefix)
+    , nRibRoutes(0)
+    , retryDelay(0)
+  {
+  }
+
+public:
+  Name prefix; ///< readvertised prefix
+  mutable ndn::security::SigningInfo signer; ///< signer for commands
+  mutable size_t nRibRoutes; ///< number of RIB routes that cause the readvertisement
+  mutable time::milliseconds retryDelay; ///< retry interval (not used for refresh)
+  mutable ndn::util::scheduler::ScopedEventId retryEvt; ///< retry or refresh event
+};
+
+inline bool
+operator<(const ReadvertisedRoute& lhs, const ReadvertisedRoute& rhs)
+{
+  return lhs.prefix < rhs.prefix;
+}
+
+using ReadvertisedRouteContainer = std::set<ReadvertisedRoute>;
+
+} // namespace rib
+} // namespace nfd
+
+#endif // NFD_DAEMON_RIB_READVERTISE_READVERTISED_ROUTE_HPP