37 #include <boost/property_tree/info_parser.hpp>
38 #include <ndn-cxx/transport/tcp-transport.hpp>
39 #include <ndn-cxx/transport/unix-transport.hpp>
60 boost::property_tree::read_info(filename, config);
65 static shared_ptr<ndn::Transport>
68 if (config.get_child_optional(
"face_system.unix")) {
71 auto path = config.get<std::string>(
"face_system.unix.path",
"/run/nfd/nfd.sock");
73 auto path = config.get<std::string>(
"face_system.unix.path",
"/var/run/nfd/nfd.sock");
75 return make_shared<ndn::UnixTransport>(path);
77 else if (config.get_child_optional(
"face_system.tcp") &&
78 config.get<std::string>(
"face_system.tcp.listen",
"yes") ==
"yes") {
80 auto port = config.get<std::string>(
"face_system.tcp.port",
"6363");
81 return make_shared<ndn::TcpTransport>(
"localhost", port);
84 NDN_THROW(
ConfigFile::Error(
"No transport is available to communicate with NFD"));
90 [&configFile] (
ConfigFile& config, bool isDryRun) {
91 config.parse(configFile, isDryRun);
98 [&configSection] (
ConfigFile& config, bool isDryRun) {
99 config.parse(configSection, isDryRun,
"internal://nfd.conf");
104 template<
typename ConfigParseFunc>
105 Service::Service(ndn::KeyChain& keyChain, shared_ptr<ndn::Transport> localNfdTransport,
106 const ConfigParseFunc& configParse)
107 : m_keyChain(keyChain)
109 , m_nfdController(m_face, m_keyChain)
110 , m_fibUpdater(m_rib, m_nfdController)
111 , m_dispatcher(m_face, m_keyChain)
112 , m_ribManager(m_rib, m_face, m_keyChain, m_nfdController, m_dispatcher)
114 if (s_instance !=
nullptr) {
115 NDN_THROW(std::logic_error(
"RIB service cannot be instantiated more than once"));
118 NDN_THROW(std::logic_error(
"RIB service must run on RIB thread"));
123 config.addSectionHandler(
CFG_RIB, [
this] (
auto&&... args) {
124 processConfig(std::forward<decltype(args)>(args)...);
126 configParse(config,
true);
127 configParse(config,
false);
135 s_instance =
nullptr;
141 if (s_instance ==
nullptr) {
142 NDN_THROW(std::logic_error(
"RIB service is not instantiated"));
145 NDN_THROW(std::logic_error(
"Must get RIB service on RIB thread"));
151 Service::processConfig(
const ConfigSection& section,
bool isDryRun,
const std::string& filename)
154 checkConfig(section, filename);
157 applyConfig(section, filename);
162 Service::checkConfig(
const ConfigSection& section,
const std::string& filename)
164 bool hasLocalhop =
false;
165 bool hasPropagate =
false;
167 for (
const auto& item : section) {
168 const std::string& key = item.first;
171 ndn::ValidatorConfig testValidator(m_face);
172 testValidator.load(value, filename);
176 ndn::ValidatorConfig testValidator(m_face);
177 testValidator.load(value, filename);
187 NDN_THROW(ConfigFile::Error(
"Unrecognized option " +
CFG_RIB +
"." + key));
191 if (hasLocalhop && hasPropagate) {
193 " cannot be enabled at the same time"));
198 Service::applyConfig(
const ConfigSection& section,
const std::string& filename)
200 bool wantPrefixPropagate =
false;
201 bool wantReadvertiseNlsr =
false;
203 for (
const auto& item : section) {
204 const std::string& key = item.first;
216 wantPrefixPropagate =
true;
218 if (!m_readvertisePropagation) {
221 auto cost = item.second.get_optional<uint64_t>(
"cost");
222 auto parameters = ndn::nfd::ControlParameters()
224 .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT);
226 auto timeout = item.second.get_optional<uint64_t>(
"timeout");
227 auto options = ndn::nfd::CommandOptions()
231 m_readvertisePropagation = make_unique<Readvertise>(
233 make_unique<HostToGatewayReadvertisePolicy>(m_keyChain, item.second),
234 make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options, parameters));
241 NDN_THROW(ConfigFile::Error(
"Unrecognized option " +
CFG_RIB +
"." + key));
245 if (!wantPrefixPropagate && m_readvertisePropagation !=
nullptr) {
247 m_readvertisePropagation.reset();
250 if (wantReadvertiseNlsr && m_readvertiseNlsr ==
nullptr) {
253 m_readvertiseNlsr = make_unique<Readvertise>(
255 make_unique<ClientToNlsrReadvertisePolicy>(),
256 make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options));
258 else if (!wantReadvertiseNlsr && m_readvertiseNlsr !=
nullptr) {
260 m_readvertiseNlsr.reset();
Configuration file parsing utility.
static void ignoreUnknownSection(const std::string &filename, const std::string §ionName, const ConfigSection §ion, bool isDryRun)
static bool parseYesNo(const ConfigSection &node, const std::string &key, const std::string §ionName)
Parse a config option that can be either "yes" or "no".
void registerWithNfd()
Start accepting commands and dataset requests.
static const Name LOCALHOP_TOP_PREFIX
void applyLocalhostConfig(const ConfigSection §ion, const std::string &filename)
Apply localhost_security configuration.
void enableLocalhop(const ConfigSection §ion, const std::string &filename)
Apply localhop_security configuration and allow accepting commands on /localhop/nfd/rib prefix.
void enableLocalFields()
Enable NDNLP IncomingFaceId field in order to support self-registration commands.
void applyPaConfig(const ConfigSection §ion, const std::string &filename)
Apply prefix_announcement_validation configuration.
Initializes and executes the NFD-RIB service thread.
Service(const std::string &configFile, ndn::KeyChain &keyChain)
Create NFD-RIB service.
static Service & get()
Get a reference to the only instance of this class.
#define NFD_LOG_INIT(name)
const std::string CFG_PA_VALIDATION
const std::string CFG_LOCALHOST_SECURITY
const Name READVERTISE_NLSR_PREFIX
static shared_ptr< ndn::Transport > makeLocalNfdTransport(const ConfigSection &config)
const std::string CFG_RIB
const std::string CFG_PREFIX_PROPAGATE
const std::string CFG_LOCALHOP_SECURITY
constexpr uint64_t PROPAGATE_DEFAULT_COST
const std::string CFG_READVERTISE_NLSR
constexpr time::milliseconds PROPAGATE_DEFAULT_TIMEOUT
static ConfigSection loadConfigSectionFromFile(const std::string &filename)
boost::property_tree::ptree ConfigSection
A configuration file section.
boost::asio::io_context & getRibIoService()
boost::asio::io_context & getGlobalIoService()
Returns the global io_context instance for the calling thread.