27 #include "core/version.hpp" 29 #include <ndn-cxx/face.hpp> 30 #include <ndn-cxx/name.hpp> 31 #include <ndn-cxx/encoding/buffer-stream.hpp> 32 #include <ndn-cxx/mgmt/nfd/controller.hpp> 33 #include <ndn-cxx/mgmt/nfd/face-monitor.hpp> 34 #include <ndn-cxx/mgmt/nfd/face-status.hpp> 35 #include <ndn-cxx/net/address-converter.hpp> 36 #include <ndn-cxx/net/face-uri.hpp> 37 #include <ndn-cxx/security/key-chain.hpp> 39 #include <boost/program_options/options_description.hpp> 40 #include <boost/program_options/variables_map.hpp> 41 #include <boost/program_options/parsers.hpp> 46 namespace nfd_autoreg {
50 class AutoregServer : boost::noncopyable
54 : m_controller(m_face, m_keyChain)
55 , m_faceMonitor(m_face)
61 onRegisterCommandSuccess(uint64_t faceId,
const Name& prefix)
63 std::cerr <<
"SUCCEED: register " << prefix <<
" on face " << faceId << std::endl;
67 onRegisterCommandFailure(uint64_t faceId,
const Name& prefix,
68 const nfd::ControlResponse& response)
70 std::cerr <<
"FAILED: register " << prefix <<
" on face " << faceId
71 <<
" (code: " << response.getCode() <<
", reason: " << response.getText() <<
")" 79 hasAllowedSchema(
const FaceUri& uri)
81 const std::string& scheme = uri.getScheme();
82 return (scheme ==
"udp4" || scheme ==
"tcp4" ||
83 scheme ==
"udp6" || scheme ==
"tcp6");
90 isBlacklisted(
const boost::asio::ip::address& address)
92 return std::any_of(m_blackList.begin(), m_blackList.end(),
93 bind(&Network::doesContain, _1, address));
100 isWhitelisted(
const boost::asio::ip::address& address)
102 return std::any_of(m_whiteList.begin(), m_whiteList.end(),
103 bind(&Network::doesContain, _1, address));
107 registerPrefixesForFace(uint64_t faceId,
const std::vector<Name>& prefixes)
109 for (
const Name& prefix : prefixes) {
110 m_controller.start<nfd::RibRegisterCommand>(
111 nfd::ControlParameters()
114 .setOrigin(nfd::ROUTE_ORIGIN_AUTOREG)
116 .setExpirationPeriod(time::milliseconds::max()),
117 bind(&AutoregServer::onRegisterCommandSuccess,
this, faceId, prefix),
118 bind(&AutoregServer::onRegisterCommandFailure,
this, faceId, prefix, _1));
123 registerPrefixesIfNeeded(uint64_t faceId,
const FaceUri& uri, nfd::FacePersistency facePersistency)
125 if (hasAllowedSchema(uri)) {
126 boost::system::error_code ec;
127 boost::asio::ip::address address = ndn::ip::addressFromString(uri.getHost(), ec);
129 if (!address.is_multicast()) {
131 registerPrefixesForFace(faceId, m_allFacesPrefixes);
134 if (facePersistency == nfd::FACE_PERSISTENCY_ON_DEMAND &&
135 !isBlacklisted(address) && isWhitelisted(address)) {
136 registerPrefixesForFace(faceId, m_autoregPrefixes);
143 onNotification(
const nfd::FaceEventNotification& notification)
145 if (notification.getKind() == nfd::FACE_EVENT_CREATED &&
146 notification.getFaceScope() != nfd::FACE_SCOPE_LOCAL) {
147 std::cerr <<
"PROCESSING: " << notification << std::endl;
149 registerPrefixesIfNeeded(notification.getFaceId(), FaceUri(notification.getRemoteUri()),
150 notification.getFacePersistency());
153 std::cerr <<
"IGNORED: " << notification << std::endl;
164 usage(std::ostream& os,
165 const boost::program_options::options_description& desc,
166 const char* programName)
169 <<
" " << programName <<
" --prefix=</autoreg/prefix> [--prefix=/another/prefix] ...\n" 177 std::cerr <<
"AUTOREG prefixes: " << std::endl;
178 for (
const Name& prefix : m_autoregPrefixes) {
179 std::cout <<
" " << prefix << std::endl;
181 std::cerr <<
"ALL-FACES-AUTOREG prefixes: " << std::endl;
182 for (
const Name& prefix : m_allFacesPrefixes) {
183 std::cout <<
" " << prefix << std::endl;
186 if (!m_blackList.empty()) {
187 std::cerr <<
"Blacklisted networks: " << std::endl;
188 for (
const Network& network : m_blackList) {
189 std::cout <<
" " << network << std::endl;
193 std::cerr <<
"Whitelisted networks: " << std::endl;
194 for (
const Network& network : m_whiteList) {
195 std::cout <<
" " << network << std::endl;
198 m_faceMonitor.onNotification.connect(bind(&AutoregServer::onNotification,
this, _1));
199 m_faceMonitor.start();
201 boost::asio::signal_set signalSet(m_face.getIoService(), SIGINT, SIGTERM);
202 signalSet.async_wait(bind(&AutoregServer::signalHandler,
this));
204 m_face.processEvents();
208 startFetchingFaceStatusDataset()
210 m_controller.fetch<nfd::FaceDataset>(
211 [
this] (
const std::vector<nfd::FaceStatus>& faces) {
212 for (
const auto& faceStatus : faces) {
213 registerPrefixesIfNeeded(faceStatus.getFaceId(), FaceUri(faceStatus.getRemoteUri()),
214 faceStatus.getFacePersistency());
217 [] (uint32_t code,
const std::string& reason) {});
221 main(
int argc,
char* argv[])
223 namespace po = boost::program_options;
225 po::options_description optionDesciption;
226 optionDesciption.add_options()
227 (
"help,h",
"produce help message")
228 (
"prefix,i", po::value<std::vector<Name>>(&m_autoregPrefixes)->composing(),
229 "prefix that should be automatically registered when new a remote non-local face is " 231 (
"all-faces-prefix,a", po::value<std::vector<Name>>(&m_allFacesPrefixes)->composing(),
232 "prefix that should be automatically registered for all TCP and UDP non-local faces " 233 "(blacklists and whitelists do not apply to this prefix)")
234 (
"cost,c", po::value<uint64_t>(&m_cost)->default_value(255),
235 "FIB cost which should be assigned to autoreg nexthops")
236 (
"whitelist,w", po::value<std::vector<Network>>(&m_whiteList)->composing(),
237 "Whitelisted network, e.g., 192.168.2.0/24 or ::1/128")
238 (
"blacklist,b", po::value<std::vector<Network>>(&m_blackList)->composing(),
239 "Blacklisted network, e.g., 192.168.2.32/30 or ::1/128")
240 (
"version,V",
"show version and exit")
243 po::variables_map options;
245 po::store(po::command_line_parser(argc, argv).options(optionDesciption).run(), options);
248 catch (
const std::exception& e) {
249 std::cerr <<
"ERROR: " << e.what() << std::endl << std::endl;
250 usage(std::cerr, optionDesciption, argv[0]);
254 if (options.count(
"help")) {
255 usage(std::cout, optionDesciption, argv[0]);
259 if (options.count(
"version")) {
260 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
264 if (m_autoregPrefixes.empty() && m_allFacesPrefixes.empty()) {
265 std::cerr <<
"ERROR: at least one --prefix or --all-faces-prefix must be specified" 266 << std::endl << std::endl;
267 usage(std::cerr, optionDesciption, argv[0]);
271 if (m_whiteList.empty()) {
273 m_whiteList.push_back(Network::getMaxRangeV4());
274 m_whiteList.push_back(Network::getMaxRangeV6());
278 startFetchingFaceStatusDataset();
281 catch (
const std::exception& e) {
282 std::cerr <<
"ERROR: " << e.what() << std::endl;
292 nfd::Controller m_controller;
293 nfd::FaceMonitor m_faceMonitor;
294 std::vector<Name> m_autoregPrefixes;
295 std::vector<Name> m_allFacesPrefixes;
297 std::vector<Network> m_whiteList;
298 std::vector<Network> m_blackList;
307 ndn::nfd_autoreg::AutoregServer server;
308 return server.main(argc, argv);
Copyright (c) 2014-2017, Regents of the University of California, Arizona Board of Regents...
int main(int argc, char *argv[])