30 #include <ndn-cxx/net/face-uri.hpp>
40 , m_scheduler(face.getIoService())
41 , m_confParam(confParam)
42 , m_adjacencyList(confParam.getAdjacencyList())
43 , m_namePrefixList(confParam.getNamePrefixList())
44 , m_fib(m_face, m_scheduler, m_adjacencyList, m_confParam, keyChain)
45 , m_routingTable(m_scheduler, m_fib, m_lsdb, m_namePrefixTable, m_confParam)
46 , m_namePrefixTable(m_fib, m_routingTable, m_routingTable.afterRoutingChange)
47 , m_lsdb(m_face, keyChain, m_confParam, m_namePrefixTable, m_routingTable)
48 , m_helloProtocol(m_face, keyChain, confParam, m_routingTable, m_lsdb)
49 , m_onNewLsaConnection(m_lsdb.getSync().onNewLsa->connect(
50 [this] (const
ndn::Name& updateName, uint64_t sequenceNumber,
51 const
ndn::Name& originRouter) {
54 , m_onPrefixRegistrationSuccess(m_fib.onPrefixRegistrationSuccess.connect(
55 [
this] (
const ndn::Name& name) {
56 m_helloProtocol.sendHelloInterest(name);
58 , m_onHelloDataValidated(m_helloProtocol.onHelloDataValidated.connect(
59 [
this] (
const ndn::Name& neighbor) {
60 auto it = m_adjacencyList.findAdjacent(neighbor);
61 if (it != m_adjacencyList.end()) {
62 m_fib.registerPrefix(m_confParam.getSyncPrefix(), it->getFaceUri(), it->getLinkCost(),
63 ndn::time::milliseconds::max(), ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
66 , m_dispatcher(m_face, keyChain)
67 , m_datasetHandler(m_dispatcher, m_lsdb, m_routingTable)
68 , m_controller(m_face, keyChain)
69 , m_faceDatasetController(m_face, keyChain)
70 , m_prefixUpdateProcessor(m_dispatcher,
71 m_confParam.getPrefixUpdateValidator(),
74 m_confParam.getConfFileNameDynamic())
75 , m_nfdRibCommandProcessor(m_dispatcher,
78 , m_statsCollector(m_lsdb, m_helloProtocol)
79 , m_faceMonitor(m_face)
83 m_faceMonitor.onNotification.connect(std::bind(&Nlsr::onFaceEventNotification,
this, _1));
84 m_faceMonitor.start();
91 NLSR_LOG_DEBUG(
"Default NLSR identity: " << m_confParam.getSigningInfo().getSignerName());
94 setLsaInterestFilter();
97 addDispatcherTopPrefix(ndn::Name(m_confParam.getRouterPrefix()).append(
"nlsr"));
98 addDispatcherTopPrefix(LOCALHOST_PREFIX);
100 enableIncomingFaceIdIndication();
102 registerLocalhostPrefix();
103 registerRouterPrefix();
105 initializeFaces(std::bind(&Nlsr::processFaceDataset,
this, _1),
106 std::bind(&Nlsr::onFaceDatasetFetchTimeout,
this, _1, _2, 0));
113 Nlsr::registerStrategyForCerts(
const ndn::Name& originRouter)
115 for (
const ndn::Name& router : m_strategySetOnRouters) {
116 if (router == originRouter) {
122 m_strategySetOnRouters.push_back(originRouter);
124 ndn::Name routerKey(originRouter);
125 routerKey.append(ndn::security::Certificate::KEY_COMPONENT);
126 ndn::Name instanceKey(originRouter);
127 instanceKey.append(
"nlsr").append(ndn::security::Certificate::KEY_COMPONENT);
129 m_fib.setStrategy(routerKey, Fib::BEST_ROUTE_V2_STRATEGY, 0);
130 m_fib.setStrategy(instanceKey, Fib::BEST_ROUTE_V2_STRATEGY, 0);
133 for (
size_t i = 0; i < originRouter.size(); ++i) {
134 if (originRouter[i].toUri() ==
"%C1.Router") {
137 siteKey.append(originRouter[i]);
139 ndn::Name opPrefix(siteKey);
140 siteKey.append(ndn::security::Certificate::KEY_COMPONENT);
141 m_fib.setStrategy(siteKey, Fib::BEST_ROUTE_V2_STRATEGY, 0);
143 opPrefix.append(std::string(
"%C1.Operator"));
144 m_fib.setStrategy(opPrefix, Fib::BEST_ROUTE_V2_STRATEGY, 0);
148 Nlsr::registrationFailed(
const ndn::Name& name)
150 NLSR_LOG_ERROR(
"ERROR: Failed to register prefix " << name <<
" in local hub's daemon");
151 NDN_THROW(
Error(
"Error: Prefix registration failed"));
155 Nlsr::onRegistrationSuccess(
const ndn::Name& name)
161 Nlsr::setLsaInterestFilter()
163 ndn::Name name = m_confParam.getLsaPrefix();
165 NLSR_LOG_DEBUG(
"Setting interest filter for LsaPrefix: " << name);
167 m_face.setInterestFilter(ndn::InterestFilter(name).allowLoopback(
false),
168 std::bind(&Lsdb::processInterest, &m_lsdb, _1, _2),
169 std::bind(&Nlsr::onRegistrationSuccess,
this, _1),
170 std::bind(&Nlsr::registrationFailed,
this, _1),
171 m_confParam.getSigningInfo(), ndn::nfd::ROUTE_FLAG_CAPTURE);
175 Nlsr::addDispatcherTopPrefix(
const ndn::Name& topPrefix)
179 m_dispatcher.addTopPrefix(topPrefix,
false, m_confParam.getSigningInfo());
181 catch (
const std::exception& e) {
182 NLSR_LOG_ERROR(
"Error setting top-level prefix in dispatcher: " << e.what() <<
"\n");
187 Nlsr::setStrategies()
189 m_fib.setStrategy(m_confParam.getLsaPrefix(), Fib::MULTICAST_STRATEGY, 0);
190 m_fib.setStrategy(m_confParam.getSyncPrefix(), Fib::MULTICAST_STRATEGY, 0);
197 m_adjacencyList.writeLog();
200 m_lsdb.buildAndInstallOwnNameLsa();
204 m_lsdb.buildAndInstallOwnCoordinateLsa();
209 for (
auto&& neighbor : m_adjacencyList.getAdjList()) {
210 neighbor.setLinkCost(0);
216 Nlsr::registerLocalhostPrefix()
218 m_face.registerPrefix(LOCALHOST_PREFIX,
219 std::bind(&Nlsr::onRegistrationSuccess,
this, _1),
220 std::bind(&Nlsr::registrationFailed,
this, _1));
224 Nlsr::registerRouterPrefix()
226 m_face.registerPrefix(ndn::Name(m_confParam.getRouterPrefix()).append(
"nlsr"),
227 std::bind(&Nlsr::onRegistrationSuccess,
this, _1),
228 std::bind(&Nlsr::registrationFailed,
this, _1));
232 Nlsr::onFaceEventNotification(
const ndn::nfd::FaceEventNotification& faceEventNotification)
236 switch (faceEventNotification.getKind()) {
237 case ndn::nfd::FACE_EVENT_DESTROYED: {
238 uint64_t faceId = faceEventNotification.getFaceId();
240 auto adjacent = m_adjacencyList.findAdjacent(faceId);
242 if (adjacent != m_adjacencyList.end()) {
243 NLSR_LOG_DEBUG(
"Face to " << adjacent->getName() <<
" with face id: " << faceId <<
" destroyed");
245 adjacent->setFaceId(0);
262 if (adjacent->getStatus() == Adjacent::STATUS_ACTIVE) {
263 adjacent->setStatus(Adjacent::STATUS_INACTIVE);
267 adjacent->setInterestTimedOutNo(m_confParam.getInterestRetryNumber());
270 m_routingTable.scheduleRoutingTableCalculation();
275 m_lsdb.scheduleAdjLsaBuild();
281 case ndn::nfd::FACE_EVENT_CREATED: {
283 ndn::FaceUri faceUri;
285 faceUri = ndn::FaceUri(faceEventNotification.getRemoteUri());
287 catch (
const std::exception& e) {
291 auto adjacent = m_adjacencyList.findAdjacent(faceUri);
292 uint64_t faceId = faceEventNotification.getFaceId();
296 if (adjacent != m_adjacencyList.end() &&
297 (adjacent->getFaceId() == 0 || adjacent->getFaceId() != faceId))
299 NLSR_LOG_DEBUG(
"Face creation event matches neighbor: " << adjacent->getName()
300 <<
". New Face ID: " << faceId <<
". Registering prefixes.");
301 adjacent->setFaceId(faceId);
303 registerAdjacencyPrefixes(*adjacent, ndn::time::milliseconds::max());
323 m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(onFetchSuccess, onFetchFailure);
328 Nlsr::processFaceDataset(
const std::vector<ndn::nfd::FaceStatus>& faces)
333 for (
auto&& adjacent : m_adjacencyList.getAdjList()) {
335 const std::string& faceUriString = adjacent.getFaceUri().toString();
337 for (
const auto& faceStatus : faces) {
340 if (adjacent.getFaceId() == 0 && faceUriString == faceStatus.getRemoteUri()) {
342 " FaceId: "<< faceStatus.getFaceId());
343 adjacent.setFaceId(faceStatus.getFaceId());
345 this->registerAdjacencyPrefixes(adjacent, ndn::time::milliseconds::max());
352 if (adjacent.getFaceId() == 0) {
354 " has no Face information in this dataset.");
358 scheduleDatasetFetch();
362 Nlsr::registerAdjacencyPrefixes(
const Adjacent& adj,
363 const ndn::time::milliseconds& timeout)
367 const ndn::Name& adjName = adj.
getName();
369 m_fib.registerPrefix(adjName, faceUri, linkCost,
370 timeout, ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
372 m_fib.registerPrefix(m_confParam.getLsaPrefix(),
373 faceUri, linkCost, timeout,
374 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
378 Nlsr::onFaceDatasetFetchTimeout(uint32_t code,
379 const std::string& msg,
380 uint32_t nRetriesSoFar)
384 if (nRetriesSoFar++ < m_confParam.getFaceDatasetFetchTries()) {
385 NLSR_LOG_DEBUG(
"Failed to fetch dataset: " << msg <<
". Attempting retry #" << nRetriesSoFar);
386 m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(std::bind(&Nlsr::processFaceDataset,
388 std::bind(&Nlsr::onFaceDatasetFetchTimeout,
389 this, _1, _2, nRetriesSoFar));
392 NLSR_LOG_ERROR(
"Failed to fetch dataset: " << msg <<
". Exceeded limit of " <<
393 m_confParam.getFaceDatasetFetchTries() <<
", so not trying again this time.");
397 scheduleDatasetFetch();
402 Nlsr::scheduleDatasetFetch()
404 NLSR_LOG_DEBUG(
"Scheduling Dataset Fetch in " << m_confParam.getFaceDatasetFetchInterval());
406 m_scheduler.schedule(m_confParam.getFaceDatasetFetchInterval(),
408 this->initializeFaces(
409 [this] (const std::vector<ndn::nfd::FaceStatus>& faces) {
410 this->processFaceDataset(faces);
412 [
this] (uint32_t code,
const std::string& msg) {
413 this->onFaceDatasetFetchTimeout(code, msg, 0);
419 Nlsr::enableIncomingFaceIdIndication()
421 NLSR_LOG_DEBUG(
"Enabling incoming face id indication for local face.");
423 m_controller.start<ndn::nfd::FaceUpdateCommand>(
424 ndn::nfd::ControlParameters()
425 .setFlagBit(ndn::nfd::FaceFlagBit::BIT_LOCAL_FIELDS_ENABLED,
true),
426 std::bind(&Nlsr::onFaceIdIndicationSuccess,
this, _1),
427 std::bind(&Nlsr::onFaceIdIndicationFailure,
this, _1));
431 Nlsr::onFaceIdIndicationSuccess(
const ndn::nfd::ControlParameters& cp)
434 <<
"for face id " << cp.getFaceId());
438 Nlsr::onFaceIdIndicationFailure(
const ndn::nfd::ControlResponse& cr)
440 NLSR_LOG_DEBUG(
"Failed to enable incoming face id indication feature: " <<
441 "(code: " << cr.getCode() <<
", reason: " << cr.getText() <<
")");
A neighbor reachable over a Face.
const ndn::FaceUri & getFaceUri() const
const ndn::Name & getName() const
double getLinkCost() const
A class to house all the configuration parameters for NLSR.
Nlsr(ndn::Face &face, ndn::KeyChain &keyChain, ConfParameter &confParam)
static const ndn::Name LOCALHOST_PREFIX
std::function< void(const std::vector< ndn::nfd::FaceStatus > &)> FetchDatasetCallback
void registerStrategyForCerts(const ndn::Name &originRouter)
std::function< void(uint32_t, const std::string &)> FetchDatasetTimeoutCallback
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California.
#define NLSR_LOG_DEBUG(x)
#define INIT_LOGGER(name)
#define NLSR_LOG_ERROR(x)
#define NLSR_LOG_TRACE(x)
Copyright (c) 2014-2020, The University of Memphis, Regents of the University of California,...