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")) {
77 auto path = config.get<std::string>(
"face_system.unix.path",
"/run/nfd.sock");
79 auto path = config.get<std::string>(
"face_system.unix.path",
"/var/run/nfd.sock");
81 return make_shared<ndn::UnixTransport>(path);
83 else if (config.get_child_optional(
"face_system.tcp") &&
84 config.get<std::string>(
"face_system.tcp.listen",
"yes") ==
"yes") {
86 auto port = config.get<std::string>(
"face_system.tcp.port",
"6363");
87 return make_shared<ndn::TcpTransport>(
"localhost", port);
90 NDN_THROW(
ConfigFile::Error(
"No transport is available to communicate with NFD"));
96 [&configFile] (
ConfigFile& config, bool isDryRun) {
97 config.parse(configFile, isDryRun);
104 [&configSection] (
ConfigFile& config, bool isDryRun) {
105 config.parse(configSection, isDryRun,
"internal://nfd.conf");
110 template<
typename ConfigParseFunc>
111 Service::Service(ndn::KeyChain& keyChain, shared_ptr<ndn::Transport> localNfdTransport,
112 const ConfigParseFunc& configParse)
113 : m_keyChain(keyChain)
115 , m_nfdController(m_face, m_keyChain)
116 , m_fibUpdater(m_rib, m_nfdController)
117 , m_dispatcher(m_face, m_keyChain)
118 , m_ribManager(m_rib, m_face, m_keyChain, m_nfdController, m_dispatcher)
120 if (s_instance !=
nullptr) {
121 NDN_THROW(std::logic_error(
"RIB service cannot be instantiated more than once"));
124 NDN_THROW(std::logic_error(
"RIB service must run on RIB thread"));
129 config.
addSectionHandler(CFG_SECTION, bind(&Service::processConfig,
this, _1, _2, _3));
130 configParse(config,
true);
131 configParse(config,
false);
139 s_instance =
nullptr;
145 if (s_instance ==
nullptr) {
146 NDN_THROW(std::logic_error(
"RIB service is not instantiated"));
149 NDN_THROW(std::logic_error(
"Must get RIB service on RIB thread"));
155 Service::processConfig(
const ConfigSection& section,
bool isDryRun,
const std::string& filename)
158 checkConfig(section, filename);
161 applyConfig(section, filename);
166 Service::checkConfig(
const ConfigSection& section,
const std::string& filename)
168 bool hasLocalhop =
false;
169 bool hasPropagate =
false;
171 for (
const auto& item : section) {
172 const std::string& key = item.first;
174 if (key == CFG_LOCALHOST_SECURITY || key == CFG_PA_VALIDATION) {
175 ndn::ValidatorConfig testValidator(m_face);
176 testValidator.load(value, filename);
178 else if (key == CFG_LOCALHOP_SECURITY) {
180 ndn::ValidatorConfig testValidator(m_face);
181 testValidator.load(value, filename);
183 else if (key == CFG_PREFIX_PROPAGATE) {
187 else if (key == CFG_READVERTISE_NLSR) {
195 if (hasLocalhop && hasPropagate) {
196 NDN_THROW(
ConfigFile::Error(CFG_LOCALHOP_SECURITY +
" and " + CFG_PREFIX_PROPAGATE +
197 " cannot be enabled at the same time"));
202 Service::applyConfig(
const ConfigSection& section,
const std::string& filename)
204 bool wantPrefixPropagate =
false;
205 bool wantReadvertiseNlsr =
false;
207 for (
const auto& item : section) {
208 const std::string& key = item.first;
210 if (key == CFG_LOCALHOST_SECURITY) {
213 else if (key == CFG_LOCALHOP_SECURITY) {
216 else if (key == CFG_PA_VALIDATION) {
219 else if (key == CFG_PREFIX_PROPAGATE) {
220 wantPrefixPropagate =
true;
222 if (!m_readvertisePropagation) {
225 auto cost = item.second.get_optional<uint64_t>(
"cost");
226 auto parameters = ndn::nfd::ControlParameters()
227 .setCost(cost.value_or(PROPAGATE_DEFAULT_COST))
228 .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT);
230 auto timeout = item.second.get_optional<uint64_t>(
"timeout");
231 auto options = ndn::nfd::CommandOptions()
233 .setTimeout(timeout ? time::milliseconds(*timeout) : PROPAGATE_DEFAULT_TIMEOUT);
235 m_readvertisePropagation = make_unique<Readvertise>(
237 make_unique<HostToGatewayReadvertisePolicy>(m_keyChain, item.second),
238 make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options, parameters));
241 else if (key == CFG_READVERTISE_NLSR) {
249 if (!wantPrefixPropagate && m_readvertisePropagation !=
nullptr) {
251 m_readvertisePropagation.reset();
254 if (wantReadvertiseNlsr && m_readvertiseNlsr ==
nullptr) {
256 auto options = ndn::nfd::CommandOptions().setPrefix(READVERTISE_NLSR_PREFIX);
257 m_readvertiseNlsr = make_unique<Readvertise>(
259 make_unique<ClientToNlsrReadvertisePolicy>(),
260 make_unique<NfdRibReadvertiseDestination>(m_nfdController, m_rib, options));
262 else if (!wantReadvertiseNlsr && m_readvertiseNlsr !=
nullptr) {
264 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.