29 #include <ndn-cxx/face.hpp>
30 #include <ndn-cxx/mgmt/nfd/controller.hpp>
31 #include <ndn-cxx/mgmt/nfd/face-monitor.hpp>
32 #include <ndn-cxx/mgmt/nfd/status-dataset.hpp>
33 #include <ndn-cxx/net/face-uri.hpp>
34 #include <ndn-cxx/security/key-chain.hpp>
36 #include <boost/asio/signal_set.hpp>
37 #include <boost/exception/diagnostic_information.hpp>
38 #include <boost/program_options/options_description.hpp>
39 #include <boost/program_options/parsers.hpp>
40 #include <boost/program_options/variables_map.hpp>
49 class AutoregServer : boost::noncopyable
53 onRegisterCommandSuccess(uint64_t faceId,
const Name& prefix)
55 std::cerr <<
"SUCCESS: register " << prefix <<
" on face " << faceId << std::endl;
59 onRegisterCommandFailure(uint64_t faceId,
const Name& prefix,
60 const ndn::nfd::ControlResponse& response)
62 std::cerr <<
"FAILED: register " << prefix <<
" on face " << faceId
63 <<
" (code: " << response.getCode() <<
", reason: " << response.getText() <<
")"
71 hasAllowedSchema(
const FaceUri& uri)
73 const std::string& scheme = uri.getScheme();
74 return scheme ==
"udp4" || scheme ==
"tcp4" ||
75 scheme ==
"udp6" || scheme ==
"tcp6";
82 isBlacklisted(
const boost::asio::ip::address& address)
const
84 return std::any_of(m_blackList.begin(), m_blackList.end(),
85 [&] (
const auto& net) { return net.doesContain(address); });
92 isWhitelisted(
const boost::asio::ip::address& address)
const
94 return std::any_of(m_whiteList.begin(), m_whiteList.end(),
95 [&] (
const auto& net) { return net.doesContain(address); });
99 registerPrefixesForFace(uint64_t faceId,
const std::vector<Name>& prefixes)
101 for (
const Name& prefix : prefixes) {
102 m_controller.start<ndn::nfd::RibRegisterCommand>(
103 ndn::nfd::ControlParameters()
106 .setOrigin(ndn::nfd::ROUTE_ORIGIN_AUTOREG)
108 .setExpirationPeriod(ndn::time::milliseconds::max()),
109 [=] (
auto&&...) { onRegisterCommandSuccess(faceId, prefix); },
110 [=] (
const auto& response) { onRegisterCommandFailure(faceId, prefix, response); });
115 registerPrefixesIfNeeded(uint64_t faceId,
const FaceUri& uri, ndn::nfd::FacePersistency facePersistency)
117 if (hasAllowedSchema(uri)) {
118 boost::system::error_code ec;
119 auto address = boost::asio::ip::make_address(uri.getHost(), ec);
121 if (!address.is_multicast()) {
123 registerPrefixesForFace(faceId, m_allFacesPrefixes);
126 if (facePersistency == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND &&
127 !isBlacklisted(address) && isWhitelisted(address)) {
128 registerPrefixesForFace(faceId, m_autoregPrefixes);
135 onNotification(
const ndn::nfd::FaceEventNotification& notification)
137 if (notification.getKind() == ndn::nfd::FACE_EVENT_CREATED &&
138 notification.getFaceScope() != ndn::nfd::FACE_SCOPE_LOCAL) {
139 std::cerr <<
"PROCESSING: " << notification << std::endl;
141 registerPrefixesIfNeeded(notification.getFaceId(), FaceUri(notification.getRemoteUri()),
142 notification.getFacePersistency());
145 std::cerr <<
"IGNORED: " << notification << std::endl;
152 std::cerr <<
"AUTOREG prefixes: " << std::endl;
153 for (
const Name& prefix : m_autoregPrefixes) {
154 std::cout <<
" " << prefix << std::endl;
156 std::cerr <<
"ALL-FACES-AUTOREG prefixes: " << std::endl;
157 for (
const Name& prefix : m_allFacesPrefixes) {
158 std::cout <<
" " << prefix << std::endl;
161 if (!m_blackList.empty()) {
162 std::cerr <<
"Blacklisted networks: " << std::endl;
163 for (
const Network& network : m_blackList) {
164 std::cout <<
" " << network << std::endl;
168 std::cerr <<
"Whitelisted networks: " << std::endl;
169 for (
const Network& network : m_whiteList) {
170 std::cout <<
" " << network << std::endl;
173 m_faceMonitor.onNotification.connect([
this] (
const auto& notif) { onNotification(notif); });
174 m_faceMonitor.start();
176 boost::asio::signal_set signalSet(m_face.getIoContext(), SIGINT, SIGTERM);
177 signalSet.async_wait([
this] (
auto&&...) { m_face.shutdown(); });
179 m_face.processEvents();
183 startFetchingFaceStatusDataset()
185 m_controller.fetch<ndn::nfd::FaceDataset>(
186 [
this] (
const auto& faces) {
187 for (
const auto& faceStatus : faces) {
188 registerPrefixesIfNeeded(faceStatus.getFaceId(), FaceUri(faceStatus.getRemoteUri()),
189 faceStatus.getFacePersistency());
196 main(
int argc,
char* argv[])
198 namespace po = boost::program_options;
200 po::options_description optionsDesc(
"Options");
201 optionsDesc.add_options()
202 (
"help,h",
"print this message and exit")
203 (
"version,V",
"show version information and exit")
204 (
"prefix,i", po::value<std::vector<Name>>(&m_autoregPrefixes)->composing(),
205 "prefix that should be automatically registered when a new non-local face is created")
206 (
"all-faces-prefix,a", po::value<std::vector<Name>>(&m_allFacesPrefixes)->composing(),
207 "prefix that should be automatically registered for all TCP and UDP non-local faces "
208 "(blacklists and whitelists do not apply to this prefix)")
209 (
"cost,c", po::value<uint64_t>(&m_cost)->default_value(m_cost),
210 "FIB cost that should be assigned to autoreg nexthops")
211 (
"whitelist,w", po::value<std::vector<Network>>(&m_whiteList)->composing(),
212 "Whitelisted network, e.g., 192.168.2.0/24 or ::1/128")
213 (
"blacklist,b", po::value<std::vector<Network>>(&m_blackList)->composing(),
214 "Blacklisted network, e.g., 192.168.2.32/30 or ::1/128")
217 auto usage = [&] (std::ostream& os) {
218 os <<
"Usage: " << argv[0] <<
" [--prefix=</autoreg/prefix>]... [options]\n"
223 po::variables_map options;
225 po::store(po::parse_command_line(argc, argv, optionsDesc), options);
228 catch (
const std::exception& e) {
229 std::cerr <<
"ERROR: " << e.what() << std::endl << std::endl;
234 if (options.count(
"help") > 0) {
239 if (options.count(
"version") > 0) {
244 if (m_autoregPrefixes.empty() && m_allFacesPrefixes.empty()) {
245 std::cerr <<
"ERROR: at least one --prefix or --all-faces-prefix must be specified"
246 << std::endl << std::endl;
251 if (m_whiteList.empty()) {
258 startFetchingFaceStatusDataset();
261 catch (
const std::exception& e) {
262 std::cerr <<
"ERROR: " << boost::diagnostic_information(e);
271 ndn::KeyChain m_keyChain;
272 ndn::nfd::Controller m_controller{m_face, m_keyChain};
273 ndn::nfd::FaceMonitor m_faceMonitor{m_face};
274 std::vector<Name> m_autoregPrefixes;
275 std::vector<Name> m_allFacesPrefixes;
276 uint64_t m_cost = 255;
277 std::vector<Network> m_whiteList;
278 std::vector<Network> m_blackList;
286 nfd::tools::autoreg::AutoregServer server;
287 return server.main(argc, argv);
static const Network & getMaxRangeV6()
static const Network & getMaxRangeV4()
static void usage(std::ostream &os, const po::options_description &opts, const char *programName)
int main(int argc, char *argv[])
const char NFD_VERSION_BUILD_STRING[]
NFD version string, including git commit information if NFD is build from a specific git commit.