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();
249 fetchFaceStatusSegments(
const Data& data,
const shared_ptr<ndn::OBufferStream>& buffer)
251 buffer->write(reinterpret_cast<const char*>(data.getContent().value()),
252 data.getContent().value_size());
254 uint64_t currentSegment = data.getName().get(-1).toSegment();
256 const name::Component& finalBlockId = data.getMetaInfo().getFinalBlockId();
257 if (finalBlockId.empty() || finalBlockId.toSegment() > currentSegment) {
258 m_face.expressInterest(data.getName().getPrefix(-1).appendSegment(currentSegment + 1),
259 bind(&AutoregServer::fetchFaceStatusSegments,
this, _2, buffer),
263 return processFaceStatusDataset(buffer);
268 startFetchingFaceStatusDataset()
270 shared_ptr<ndn::OBufferStream> buffer = make_shared<ndn::OBufferStream>();
272 Interest interest(
"/localhost/nfd/faces/list");
273 interest.setChildSelector(1);
274 interest.setMustBeFresh(
true);
276 m_face.expressInterest(interest,
277 bind(&AutoregServer::fetchFaceStatusSegments,
this, _2, buffer),
282 processFaceStatusDataset(
const shared_ptr<ndn::OBufferStream>& buffer)
284 ndn::ConstBufferPtr buf = buffer->buf();
285 std::vector<uint64_t> multicastFaces;
288 while (offset < buf->size()) {
291 std::tie(isOk, block) = Block::fromBuffer(buf, offset);
293 std::cerr <<
"ERROR: cannot decode FaceStatus TLV" << std::endl;
297 offset += block.size();
299 nfd::FaceStatus faceStatus(block);
300 registerPrefixesIfNeeded(faceStatus.getFaceId(), FaceUri(faceStatus.getRemoteUri()),
301 faceStatus.getFacePersistency());
306 main(
int argc,
char* argv[])
308 po::options_description optionDesciption;
309 optionDesciption.add_options()
310 (
"help,h",
"produce help message")
311 (
"prefix,i", po::value<std::vector<ndn::Name> >(&m_autoregPrefixes)->composing(),
312 "prefix that should be automatically registered when new a remote non-local face is "
314 (
"all-faces-prefix,a", po::value<std::vector<ndn::Name> >(&m_allFacesPrefixes)->composing(),
315 "prefix that should be automatically registered for all TCP and UDP non-local faces "
316 "(blacklists and whitelists do not apply to this prefix)")
317 (
"cost,c", po::value<uint64_t>(&m_cost)->default_value(255),
318 "FIB cost which should be assigned to autoreg nexthops")
319 (
"whitelist,w", po::value<std::vector<Network> >(&m_whiteList)->composing(),
320 "Whitelisted network, e.g., 192.168.2.0/24 or ::1/128")
321 (
"blacklist,b", po::value<std::vector<Network> >(&m_blackList)->composing(),
322 "Blacklisted network, e.g., 192.168.2.32/30 or ::1/128")
323 (
"version,V",
"show version and exit")
326 po::variables_map options;
329 po::store(po::command_line_parser(argc, argv).options(optionDesciption).run(), options);
332 catch (std::exception& e)
334 std::cerr <<
"ERROR: " << e.what() << std::endl << std::endl;
335 usage(std::cerr, optionDesciption, argv[0]);
339 if (options.count(
"help"))
341 usage(std::cout, optionDesciption, argv[0]);
345 if (options.count(
"version"))
347 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
351 if (m_autoregPrefixes.empty() && m_allFacesPrefixes.empty())
353 std::cerr <<
"ERROR: at least one --prefix or --all-faces-prefix must be specified"
354 << std::endl << std::endl;
355 usage(std::cerr, optionDesciption, argv[0]);
359 if (m_whiteList.empty())
362 m_whiteList.push_back(Network::getMaxRangeV4());
363 m_whiteList.push_back(Network::getMaxRangeV6());
368 startFetchingFaceStatusDataset();
371 catch (std::exception& e)
373 std::cerr <<
"ERROR: " << e.what() << std::endl;
383 Controller m_controller;
384 FaceMonitor m_faceMonitor;
385 std::vector<ndn::Name> m_autoregPrefixes;
386 std::vector<ndn::Name> m_allFacesPrefixes;
388 std::vector<Network> m_whiteList;
389 std::vector<Network> m_blackList;
398 ndn::nfd_autoreg::AutoregServer server;
399 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...