26 #include <ndn-cxx/face.hpp>
27 #include <ndn-cxx/name.hpp>
29 #include <ndn-cxx/security/key-chain.hpp>
30 #include <ndn-cxx/util/face-uri.hpp>
31 #include <ndn-cxx/mgmt/nfd/controller.hpp>
32 #include <ndn-cxx/mgmt/nfd/face-monitor.hpp>
33 #include <ndn-cxx/mgmt/nfd/face-status.hpp>
34 #include <ndn-cxx/encoding/buffer-stream.hpp>
36 #include <boost/program_options/options_description.hpp>
37 #include <boost/program_options/variables_map.hpp>
38 #include <boost/program_options/parsers.hpp>
40 #include "core/version.hpp"
46 using ndn::nfd::FaceEventNotification;
47 using ndn::util::FaceUri;
51 namespace nfd_autoreg {
53 namespace po = boost::program_options;
55 class AutoregServer : boost::noncopyable
59 : m_controller(m_face, m_keyChain)
60 , m_faceMonitor(m_face)
66 onRegisterCommandSuccess(uint64_t faceId,
const Name& prefix)
68 std::cerr <<
"SUCCEED: register " << prefix <<
" on face " << faceId << std::endl;
72 onRegisterCommandFailure(uint64_t faceId,
const Name& prefix,
73 const nfd::ControlResponse& response)
75 std::cerr <<
"FAILED: register " << prefix <<
" on face " << faceId
76 <<
" (code: " << response.getCode() <<
", reason: " << response.getText() <<
")"
84 hasAllowedSchema(
const FaceUri& uri)
86 const std::string& scheme = uri.getScheme();
87 return (scheme ==
"udp4" || scheme ==
"tcp4" ||
88 scheme ==
"udp6" || scheme ==
"tcp6");
95 isBlacklisted(
const boost::asio::ip::address& address)
97 for (std::vector<Network>::const_iterator network = m_blackList.begin();
98 network != m_blackList.end();
101 if (network->doesContain(address))
112 isWhitelisted(
const boost::asio::ip::address& address)
114 for (std::vector<Network>::const_iterator network = m_whiteList.begin();
115 network != m_whiteList.end();
118 if (network->doesContain(address))
126 registerPrefixesForFace(uint64_t faceId,
127 const std::vector<ndn::Name>& prefixes)
129 for (std::vector<ndn::Name>::const_iterator prefix = prefixes.begin();
130 prefix != prefixes.end();
133 m_controller.start<RibRegisterCommand>(
137 .setOrigin(ROUTE_ORIGIN_AUTOREG)
139 .setExpirationPeriod(time::milliseconds::max()),
140 bind(&AutoregServer::onRegisterCommandSuccess,
this, faceId, *prefix),
141 bind(&AutoregServer::onRegisterCommandFailure,
this, faceId, *prefix, _1));
146 registerPrefixesIfNeeded(uint64_t faceId,
const FaceUri& uri, FacePersistency facePersistency)
148 if (hasAllowedSchema(uri)) {
149 boost::system::error_code ec;
150 boost::asio::ip::address address = boost::asio::ip::address::from_string(uri.getHost(), ec);
152 if (!address.is_multicast()) {
154 registerPrefixesForFace(faceId, m_allFacesPrefixes);
157 if (facePersistency == FACE_PERSISTENCY_ON_DEMAND &&
158 !isBlacklisted(address) && isWhitelisted(address)) {
159 registerPrefixesForFace(faceId, m_autoregPrefixes);
166 onNotification(
const FaceEventNotification& notification)
168 if (notification.getKind() == FACE_EVENT_CREATED &&
169 notification.getFaceScope() != FACE_SCOPE_LOCAL)
171 std::cerr <<
"PROCESSING: " << notification << std::endl;
173 registerPrefixesIfNeeded(notification.getFaceId(), FaceUri(notification.getRemoteUri()),
174 notification.getFacePersistency());
178 std::cerr <<
"IGNORED: " << notification << std::endl;
191 usage(std::ostream& os,
192 const po::options_description& optionDesciption,
193 const char* programName)
196 <<
" " << programName <<
" --prefix=</autoreg/prefix> [--prefix=/another/prefix] ...\n"
198 os << optionDesciption;
204 std::cerr <<
"AUTOREG prefixes: " << std::endl;
205 for (std::vector<ndn::Name>::const_iterator prefix = m_autoregPrefixes.begin();
206 prefix != m_autoregPrefixes.end();
209 std::cout <<
" " << *prefix << std::endl;
211 std::cerr <<
"ALL-FACES-AUTOREG prefixes: " << std::endl;
212 for (std::vector<ndn::Name>::const_iterator prefix = m_allFacesPrefixes.begin();
213 prefix != m_allFacesPrefixes.end();
216 std::cout <<
" " << *prefix << std::endl;
219 if (!m_blackList.empty())
221 std::cerr <<
"Blacklisted networks: " << std::endl;
222 for (std::vector<Network>::const_iterator network = m_blackList.begin();
223 network != m_blackList.end();
226 std::cout <<
" " << *network << std::endl;
230 std::cerr <<
"Whitelisted networks: " << std::endl;
231 for (std::vector<Network>::const_iterator network = m_whiteList.begin();
232 network != m_whiteList.end();
235 std::cout <<
" " << *network << std::endl;
238 m_faceMonitor.onNotification.connect(bind(&AutoregServer::onNotification,
this, _1));
239 m_faceMonitor.start();
241 boost::asio::signal_set signalSet(m_face.getIoService(), SIGINT, SIGTERM);
242 signalSet.async_wait(bind(&AutoregServer::signalHandler,
this));
244 m_face.processEvents();
248 startFetchingFaceStatusDataset()
250 m_controller.fetch<FaceDataset>(
251 [
this] (
const std::vector<ndn::nfd::FaceStatus>& faces) {
252 for (
const ndn::nfd::FaceStatus& faceStatus : faces) {
253 registerPrefixesIfNeeded(faceStatus.getFaceId(), FaceUri(faceStatus.getRemoteUri()),
254 faceStatus.getFacePersistency());
257 [
this] (uint32_t code,
const std::string& reason) {});
261 main(
int argc,
char* argv[])
263 po::options_description optionDesciption;
264 optionDesciption.add_options()
265 (
"help,h",
"produce help message")
266 (
"prefix,i", po::value<std::vector<ndn::Name> >(&m_autoregPrefixes)->composing(),
267 "prefix that should be automatically registered when new a remote non-local face is "
269 (
"all-faces-prefix,a", po::value<std::vector<ndn::Name> >(&m_allFacesPrefixes)->composing(),
270 "prefix that should be automatically registered for all TCP and UDP non-local faces "
271 "(blacklists and whitelists do not apply to this prefix)")
272 (
"cost,c", po::value<uint64_t>(&m_cost)->default_value(255),
273 "FIB cost which should be assigned to autoreg nexthops")
274 (
"whitelist,w", po::value<std::vector<Network> >(&m_whiteList)->composing(),
275 "Whitelisted network, e.g., 192.168.2.0/24 or ::1/128")
276 (
"blacklist,b", po::value<std::vector<Network> >(&m_blackList)->composing(),
277 "Blacklisted network, e.g., 192.168.2.32/30 or ::1/128")
278 (
"version,V",
"show version and exit")
281 po::variables_map options;
284 po::store(po::command_line_parser(argc, argv).options(optionDesciption).run(), options);
287 catch (std::exception& e)
289 std::cerr <<
"ERROR: " << e.what() << std::endl << std::endl;
290 usage(std::cerr, optionDesciption, argv[0]);
294 if (options.count(
"help"))
296 usage(std::cout, optionDesciption, argv[0]);
300 if (options.count(
"version"))
302 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
306 if (m_autoregPrefixes.empty() && m_allFacesPrefixes.empty())
308 std::cerr <<
"ERROR: at least one --prefix or --all-faces-prefix must be specified"
309 << std::endl << std::endl;
310 usage(std::cerr, optionDesciption, argv[0]);
314 if (m_whiteList.empty())
317 m_whiteList.push_back(Network::getMaxRangeV4());
318 m_whiteList.push_back(Network::getMaxRangeV6());
323 startFetchingFaceStatusDataset();
326 catch (std::exception& e)
328 std::cerr <<
"ERROR: " << e.what() << std::endl;
338 Controller m_controller;
339 FaceMonitor m_faceMonitor;
340 std::vector<ndn::Name> m_autoregPrefixes;
341 std::vector<ndn::Name> m_allFacesPrefixes;
343 std::vector<Network> m_whiteList;
344 std::vector<Network> m_blackList;
353 ndn::nfd_autoreg::AutoregServer server;
354 return server.main(argc, argv);
Copyright (c) 2014-2016, Regents of the University of California, Arizona Board of Regents...
static void usage(const char *programName)
int main(int argc, char **argv)
Copyright (c) 2014-2016, Regents of the University of California, Arizona Board of Regents...