37 #include <boost/property_tree/info_parser.hpp> 38 #include <ndn-cxx/transport/tcp-transport.hpp> 39 #include <ndn-cxx/transport/unix-transport.hpp> 46 Service* Service::s_instance =
nullptr;
63 boost::property_tree::read_info(filename, config);
71 static shared_ptr<ndn::Transport>
74 if (config.get_child_optional(
"face_system.unix")) {
76 auto path = config.get<std::string>(
"face_system.unix.path",
"/var/run/nfd.sock");
77 return make_shared<ndn::UnixTransport>(path);
79 else if (config.get_child_optional(
"face_system.tcp") &&
80 config.get<std::string>(
"face_system.tcp.listen",
"yes") ==
"yes") {
82 auto port = config.get<std::string>(
"face_system.tcp.port",
"6363");
83 return make_shared<ndn::TcpTransport>(
"localhost", port);
86 NDN_THROW(
ConfigFile::Error(
"No transport is available to communicate with NFD"));
92 [&configFile] (
ConfigFile& config, bool isDryRun) {
93 config.parse(configFile, isDryRun);
100 [&configSection] (
ConfigFile& config, bool isDryRun) {
101 config.parse(configSection, isDryRun,
"internal://nfd.conf");
106 template<
typename ConfigParseFunc>
107 Service::Service(ndn::KeyChain& keyChain, shared_ptr<ndn::Transport> localNfdTransport,
108 const ConfigParseFunc& configParse)
109 : m_keyChain(keyChain)
111 , m_nfdController(m_face, m_keyChain)
112 , m_fibUpdater(m_rib, m_nfdController)
113 , m_dispatcher(m_face, m_keyChain)
114 , m_ribManager(m_rib, m_face, m_keyChain, m_nfdController, m_dispatcher)
116 if (s_instance !=
nullptr) {
117 NDN_THROW(std::logic_error(
"RIB service cannot be instantiated more than once"));
120 NDN_THROW(std::logic_error(
"RIB service must run on RIB thread"));
125 config.
addSectionHandler(CFG_SECTION, bind(&Service::processConfig,
this, _1, _2, _3));
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;
170 if (key == CFG_LOCALHOST_SECURITY || key == CFG_LOCALHOP_SECURITY || key == CFG_PA_VALIDATION) {
172 ndn::ValidatorConfig testValidator(m_face);
173 testValidator.load(value, filename);
175 else if (key == CFG_PREFIX_PROPAGATE) {
179 else if (key == CFG_READVERTISE_NLSR) {
187 if (hasLocalhop && hasPropagate) {
188 NDN_THROW(
ConfigFile::Error(CFG_LOCALHOP_SECURITY +
" and " + CFG_PREFIX_PROPAGATE +
189 " cannot be enabled at the same time"));
194 Service::applyConfig(
const ConfigSection& section,
const std::string& filename)
196 bool wantPrefixPropagate =
false;
197 bool wantReadvertiseNlsr =
false;
199 for (
const auto& item : section) {
200 const std::string& key = item.first;
202 if (key == CFG_LOCALHOST_SECURITY) {
205 else if (key == CFG_LOCALHOP_SECURITY) {
208 else if (key == CFG_PA_VALIDATION) {
211 else if (key == CFG_PREFIX_PROPAGATE) {
212 wantPrefixPropagate =
true;
214 if (!m_readvertisePropagation) {
217 auto cost = item.second.get_optional<uint64_t>(
"cost");
218 auto parameters = ndn::nfd::ControlParameters()
219 .setCost(cost.value_or(PROPAGATE_DEFAULT_COST))
220 .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT);
222 auto timeout = item.second.get_optional<uint64_t>(
"timeout");
223 auto options = ndn::nfd::CommandOptions()
225 .setTimeout(timeout ? time::milliseconds(*timeout) : PROPAGATE_DEFAULT_TIMEOUT);
227 m_readvertisePropagation = make_unique<Readvertise>(
229 make_unique<HostToGatewayReadvertisePolicy>(m_keyChain, item.second),
230 make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options, parameters));
233 else if (key == CFG_READVERTISE_NLSR) {
241 if (!wantPrefixPropagate && m_readvertisePropagation !=
nullptr) {
243 m_readvertisePropagation.reset();
246 if (wantReadvertiseNlsr && m_readvertiseNlsr ==
nullptr) {
248 auto options = ndn::nfd::CommandOptions().setPrefix(READVERTISE_NLSR_PREFIX);
249 m_readvertiseNlsr = make_unique<Readvertise>(
251 make_unique<ClientToNlsrReadvertisePolicy>(),
252 make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options));
254 else if (!wantReadvertiseNlsr && m_readvertiseNlsr !=
nullptr) {
256 m_readvertiseNlsr.reset();
const Name READVERTISE_NLSR_PREFIX
const std::string CFG_PA_VALIDATION
void addSectionHandler(const std::string §ionName, ConfigSectionHandler subscriber)
setup notification of configuration file sections
static Service & get()
Get a reference to the only instance of this class.
const std::string CFG_SECTION
const time::milliseconds PROPAGATE_DEFAULT_TIMEOUT
configuration file parsing utility
static ConfigSection loadConfigSectionFromFile(const std::string &filename)
const std::string CFG_LOCALHOST_SECURITY
void enableLocalhop(const ConfigSection §ion, const std::string &filename)
Apply localhop_security configuration and allow accepting commands on /localhop/nfd/rib prefix...
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"
const std::string CFG_READVERTISE_NLSR
void applyLocalhostConfig(const ConfigSection §ion, const std::string &filename)
Apply localhost_security configuration.
static shared_ptr< ndn::Transport > makeLocalNfdTransport(const ConfigSection &config)
Look into the config file and construct appropriate transport to communicate with NFD If NFD-RIB inst...
boost::asio::io_service & getRibIoService()
const uint64_t PROPAGATE_DEFAULT_COST
void registerWithNfd()
Start accepting commands and dataset requests.
Service(const std::string &configFile, ndn::KeyChain &keyChain)
create NFD-RIB service
boost::property_tree::ptree ConfigSection
a config file section
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
void applyPaConfig(const ConfigSection §ion, const std::string &filename)
Apply prefix_announcement_validation configuration.
static const Name LOCALHOP_TOP_PREFIX
const std::string CFG_LOCALHOP_SECURITY
#define NFD_LOG_INIT(name)
const std::string CFG_PREFIX_PROPAGATE
void enableLocalFields()
Enable NDNLP IncomingFaceId field in order to support self-registration commands. ...
initializes and executes NFD-RIB service thread
static void ignoreUnknownSection(const std::string &filename, const std::string §ionName, const ConfigSection §ion, bool isDryRun)
boost::asio::io_service & getGlobalIoService()
Returns the global io_service instance for the calling thread.