service.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2018, Regents of the University of California,
4  * Arizona Board of Regents,
5  * Colorado State University,
6  * University Pierre & Marie Curie, Sorbonne University,
7  * Washington University in St. Louis,
8  * Beijing Institute of Technology,
9  * The University of Memphis.
10  *
11  * This file is part of NFD (Named Data Networking Forwarding Daemon).
12  * See AUTHORS.md for complete list of NFD authors and contributors.
13  *
14  * NFD is free software: you can redistribute it and/or modify it under the terms
15  * of the GNU General Public License as published by the Free Software Foundation,
16  * either version 3 of the License, or (at your option) any later version.
17  *
18  * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20  * PURPOSE. See the GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License along with
23  * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 #include "service.hpp"
27 
29 #include "fib-updater.hpp"
33 
34 #include "core/global-io.hpp"
35 #include "core/logger.hpp"
36 
37 #include <boost/property_tree/info_parser.hpp>
38 #include <ndn-cxx/transport/tcp-transport.hpp>
39 #include <ndn-cxx/transport/unix-transport.hpp>
40 
41 namespace nfd {
42 namespace rib {
43 
44 NFD_LOG_INIT(RibService);
45 
46 Service* Service::s_instance = nullptr;
47 
48 static const std::string CFG_SECTION = "rib";
49 static const std::string CFG_LOCALHOST_SECURITY = "localhost_security";
50 static const std::string CFG_LOCALHOP_SECURITY = "localhop_security";
51 static const std::string CFG_PREFIX_PROPAGATE = "auto_prefix_propagate";
52 static const std::string CFG_READVERTISE_NLSR = "readvertise_nlsr";
53 static const Name READVERTISE_NLSR_PREFIX = "/localhost/nlsr";
54 
55 static ConfigSection
56 loadConfigSectionFromFile(const std::string& filename)
57 {
58  ConfigSection config;
59  // Any format errors should have been caught already
60  boost::property_tree::read_info(filename, config);
61  return config;
62 }
63 
68 static shared_ptr<ndn::Transport>
70 {
71  if (config.get_child_optional("face_system.unix")) {
72  // default socket path should be the same as in UnixStreamFactory::processConfig
73  auto path = config.get<std::string>("face_system.unix.path", "/var/run/nfd.sock");
74  return make_shared<ndn::UnixTransport>(path);
75  }
76  else if (config.get_child_optional("face_system.tcp") &&
77  config.get<std::string>("face_system.tcp.listen", "yes") == "yes") {
78  // default port should be the same as in TcpFactory::processConfig
79  auto port = config.get<std::string>("face_system.tcp.port", "6363");
80  return make_shared<ndn::TcpTransport>("localhost", port);
81  }
82  else {
83  BOOST_THROW_EXCEPTION(ConfigFile::Error("No transport is available to communicate with NFD"));
84  }
85 }
86 
87 Service::Service(const std::string& configFile, ndn::KeyChain& keyChain)
89  [&configFile] (ConfigFile& config, bool isDryRun) {
90  config.parse(configFile, isDryRun);
91  })
92 {
93 }
94 
95 Service::Service(const ConfigSection& configSection, ndn::KeyChain& keyChain)
96  : Service(keyChain, makeLocalNfdTransport(configSection),
97  [&configSection] (ConfigFile& config, bool isDryRun) {
98  config.parse(configSection, isDryRun, "internal://nfd.conf");
99  })
100 {
101 }
102 
103 template<typename ConfigParseFunc>
104 Service::Service(ndn::KeyChain& keyChain, shared_ptr<ndn::Transport> localNfdTransport,
105  const ConfigParseFunc& configParse)
106  : m_keyChain(keyChain)
107  , m_face(std::move(localNfdTransport), getGlobalIoService(), m_keyChain)
108  , m_nfdController(m_face, m_keyChain)
109  , m_fibUpdater(m_rib, m_nfdController)
110  , m_dispatcher(m_face, m_keyChain)
111  , m_ribManager(m_rib, m_face, m_nfdController, m_dispatcher)
112 {
113  if (s_instance != nullptr) {
114  BOOST_THROW_EXCEPTION(std::logic_error("RIB service cannot be instantiated more than once"));
115  }
116  if (&getGlobalIoService() != &getRibIoService()) {
117  BOOST_THROW_EXCEPTION(std::logic_error("RIB service must run on RIB thread"));
118  }
119  s_instance = this;
120 
122  config.addSectionHandler(CFG_SECTION, bind(&Service::processConfig, this, _1, _2, _3));
123  configParse(config, true);
124  configParse(config, false);
125 
126  m_ribManager.registerWithNfd();
127  m_ribManager.enableLocalFields();
128 }
129 
131 {
132  s_instance = nullptr;
133 }
134 
135 Service&
137 {
138  if (s_instance == nullptr) {
139  BOOST_THROW_EXCEPTION(std::logic_error("RIB service is not instantiated"));
140  }
141  if (&getGlobalIoService() != &getRibIoService()) {
142  BOOST_THROW_EXCEPTION(std::logic_error("Must get RIB service on RIB thread"));
143  }
144  return *s_instance;
145 }
146 
147 void
148 Service::processConfig(const ConfigSection& section, bool isDryRun, const std::string& filename)
149 {
150  if (isDryRun) {
151  checkConfig(section, filename);
152  }
153  else {
154  applyConfig(section, filename);
155  }
156 }
157 
158 void
159 Service::checkConfig(const ConfigSection& section, const std::string& filename)
160 {
161  for (const auto& item : section) {
162  const std::string& key = item.first;
163  const ConfigSection& value = item.second;
164  if (key == CFG_LOCALHOST_SECURITY || key == CFG_LOCALHOP_SECURITY) {
165  ndn::security::v2::validator_config::ValidationPolicyConfig policy;
166  policy.load(value, filename);
167  }
168  else if (key == CFG_PREFIX_PROPAGATE) {
169  // AutoPrefixPropagator does not support config dry-run
170  }
171  else if (key == CFG_READVERTISE_NLSR) {
172  ConfigFile::parseYesNo(item, CFG_SECTION + "." + CFG_READVERTISE_NLSR);
173  }
174  else {
175  BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option " + CFG_SECTION + "." + key));
176  }
177  }
178 }
179 
180 void
181 Service::applyConfig(const ConfigSection& section, const std::string& filename)
182 {
183  bool wantPrefixPropagate = false;
184  bool wantReadvertiseNlsr = false;
185 
186  for (const auto& item : section) {
187  const std::string& key = item.first;
188  const ConfigSection& value = item.second;
189  if (key == CFG_LOCALHOST_SECURITY) {
190  m_ribManager.applyLocalhostConfig(value, filename);
191  }
192  else if (key == CFG_LOCALHOP_SECURITY) {
193  m_ribManager.enableLocalhop(value, filename);
194  }
195  else if (key == CFG_PREFIX_PROPAGATE) {
196  if (m_prefixPropagator == nullptr) {
197  m_prefixPropagator = make_unique<AutoPrefixPropagator>(m_nfdController, m_keyChain, m_rib);
198  }
199  m_prefixPropagator->loadConfig(item.second);
200  m_prefixPropagator->enable();
201  wantPrefixPropagate = true;
202  }
203  else if (key == CFG_READVERTISE_NLSR) {
204  wantReadvertiseNlsr = ConfigFile::parseYesNo(item, CFG_SECTION + "." + CFG_READVERTISE_NLSR);
205  }
206  else {
207  BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option " + CFG_SECTION + "." + key));
208  }
209  }
210 
211  if (!wantPrefixPropagate && m_prefixPropagator != nullptr) {
212  m_prefixPropagator->disable();
213  }
214 
215  if (wantReadvertiseNlsr && m_readvertiseNlsr == nullptr) {
216  NFD_LOG_DEBUG("Enabling readvertise-to-nlsr");
217  m_readvertiseNlsr = make_unique<Readvertise>(
218  m_rib,
219  make_unique<ClientToNlsrReadvertisePolicy>(),
220  make_unique<NfdRibReadvertiseDestination>(m_nfdController, READVERTISE_NLSR_PREFIX, m_rib));
221  }
222  else if (!wantReadvertiseNlsr && m_readvertiseNlsr != nullptr) {
223  NFD_LOG_DEBUG("Disabling readvertise-to-nlsr");
224  m_readvertiseNlsr.reset();
225  }
226 }
227 
228 } // namespace rib
229 } // namespace nfd
static const Name READVERTISE_NLSR_PREFIX
Definition: service.cpp:53
void addSectionHandler(const std::string &sectionName, ConfigSectionHandler subscriber)
setup notification of configuration file sections
Definition: config-file.cpp:76
static Service & get()
Get a reference to the only instance of this class.
Definition: service.cpp:136
static const std::string CFG_SECTION
Definition: service.cpp:48
configuration file parsing utility
Definition: config-file.hpp:57
static ConfigSection loadConfigSectionFromFile(const std::string &filename)
Definition: service.cpp:56
static const std::string CFG_LOCALHOST_SECURITY
Definition: service.cpp:49
static bool parseYesNo(const ConfigSection &node, const std::string &key, const std::string &sectionName)
parse a config option that can be either "yes" or "no"
Definition: config-file.cpp:59
static const std::string CFG_READVERTISE_NLSR
Definition: service.cpp:52
void enableLocalhop(const ConfigSection &section, const std::string &filename)
Apply localhop_security configuration and allow accepting commands on /localhop/nfd/rib prefix...
Definition: rib-manager.cpp:74
void registerWithNfd()
Start accepting commands and dataset requests.
Definition: rib-manager.cpp:87
void applyLocalhostConfig(const ConfigSection &section, const std::string &filename)
Apply localhost_security configuration.
Definition: rib-manager.cpp:68
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...
Definition: service.cpp:69
void enableLocalFields()
Enable NDNLP IncomingFaceId field in order to support self-registration commands. ...
boost::asio::io_service & getRibIoService()
Definition: global-io.cpp:77
Service(const std::string &configFile, ndn::KeyChain &keyChain)
create NFD-RIB service
Definition: service.cpp:87
boost::property_tree::ptree ConfigSection
a config file section
Definition: config-file.hpp:37
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
static const std::string CFG_LOCALHOP_SECURITY
Definition: service.cpp:50
#define NFD_LOG_DEBUG
Definition: logger.hpp:38
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
static const std::string CFG_PREFIX_PROPAGATE
Definition: service.cpp:51
~Service()
Destructor.
Definition: service.cpp:130
initializes and executes NFD-RIB service thread
Definition: service.hpp:51
static void ignoreUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:50
boost::asio::io_service & getGlobalIoService()
Definition: global-io.cpp:42