33 #include <ndn-cxx/net/face-uri.hpp> 34 #include <ndn-cxx/signature.hpp> 42 Nlsr::Nlsr(boost::asio::io_service& ioService, ndn::Scheduler& scheduler, ndn::Face& face, ndn::KeyChain& keyChain)
44 , m_scheduler(scheduler)
45 , m_keyChain(keyChain)
49 , m_configFileName(
"nlsr.conf")
50 , m_nlsrLsdb(*this, scheduler)
52 , m_isBuildAdjLsaSheduled(false)
53 , m_isRouteCalculationScheduled(false)
54 , m_isRoutingTableCalculating(false)
55 , m_routingTable(scheduler)
56 , m_fib(m_nlsrFace, scheduler, m_adjacencyList, m_confParam, m_keyChain)
57 , m_namePrefixTable(*this, m_routingTable.afterRoutingChange)
58 , m_dispatcher(m_nlsrFace, m_keyChain)
59 , m_datasetHandler(m_nlsrLsdb,
64 , m_helloProtocol(*this, scheduler)
65 , m_validator(
ndn::make_unique<
ndn::security::v2::CertificateFetcherDirectFetch>(m_nlsrFace))
66 , m_controller(m_nlsrFace, m_keyChain)
67 , m_faceDatasetController(m_nlsrFace, m_keyChain)
68 , m_prefixUpdateProcessor(m_dispatcher,
72 , m_nfdRibCommandProcessor(m_dispatcher,
75 , m_statsCollector(m_nlsrLsdb, m_helloProtocol)
76 , m_faceMonitor(m_nlsrFace)
79 m_faceMonitor.onNotification.connect(std::bind(&Nlsr::onFaceEventNotification,
this, _1));
80 m_faceMonitor.start();
86 NLSR_LOG_ERROR(
"ERROR: Failed to register prefix in local hub's daemon");
87 BOOST_THROW_EXCEPTION(
Error(
"Error: Prefix registration failed"));
103 NLSR_LOG_DEBUG(
"Setting interest filter for Hello interest: " << name);
105 m_nlsrFace.setInterestFilter(name,
107 &m_helloProtocol, _1, _2),
111 ndn::nfd::ROUTE_FLAG_CAPTURE);
119 NLSR_LOG_DEBUG(
"Setting interest filter for LsaPrefix: " << name);
121 m_nlsrFace.setInterestFilter(name,
123 &m_nlsrLsdb, _1, _2),
127 ndn::nfd::ROUTE_FLAG_CAPTURE);
136 m_dispatcher.addTopPrefix(topPrefix,
false, m_signingInfo);
138 catch (
const std::exception& e) {
139 NLSR_LOG_ERROR(
"Error setting top-level prefix in dispatcher: " << e.what() <<
"\n");
146 const std::string strategy(
"ndn:/localhost/nfd/strategy/multicast");
153 Nlsr::canonizeContinuation(std::list<Adjacent>::iterator iterator,
154 std::function<
void(
void)>
finally)
157 canonizeContinuation(iterator,
finally);
164 std::function<
void(std::list<Adjacent>::iterator)> then,
165 std::function<
void(
void)>
finally)
167 if (currentNeighbor != m_adjacencyList.
getAdjList().end()) {
168 ndn::FaceUri uri(currentNeighbor->getFaceUri());
169 uri.canonize([then, currentNeighbor] (ndn::FaceUri canonicalUri) {
171 <<
" to: " << canonicalUri);
172 currentNeighbor->setFaceUri(canonicalUri);
173 then(std::next(currentNeighbor));
175 [then, currentNeighbor] (
const std::string& reason) {
176 NLSR_LOG_ERROR(
"Could not canonize URI: " << currentNeighbor->getFaceUri()
177 <<
" because: " << reason);
178 then(std::next(currentNeighbor));
180 m_nlsrFace.getIoService(),
193 m_certStore.
insert(certificate);
194 m_validator.loadAnchor(
"Authoritative-Certificate",
195 ndn::security::v2::Certificate(certificate));
197 loadAnchor(
"Authoritative-Certificate",
198 ndn::security::v2::Certificate(certificate));
213 NLSR_LOG_TRACE(
"SegmentFetcher fetched a data segment. Start inserting cert to own cert store.");
214 ndn::Name keyName = lsaSegment.getSignature().getKeyLocator().getName();
219 NLSR_LOG_TRACE(
"Certificate is already in the store: " << keyName);
226 const ndn::security::v2::Certificate* cert = m_validator.getUnverifiedCertCache()
228 if (cert !=
nullptr) {
229 m_certStore.
insert(*cert);
232 << ndn::security::v2::extractKeyNameFromCertName(cert->getName()));
233 m_nlsrFace.setInterestFilter(ndn::security::v2::extractKeyNameFromCertName(cert->getName()),
234 std::bind(&Nlsr::onKeyInterest,
236 std::bind(&Nlsr::onKeyPrefixRegSuccess,
this, _1),
239 ndn::nfd::ROUTE_FLAG_CAPTURE);
241 if (!cert->getKeyName().equals(cert->getSignature().getKeyLocator().getName())) {
246 NLSR_LOG_TRACE(
"Cert for " << keyName <<
" was not found in the Validator's cache. ");
275 NLSR_LOG_DEBUG(
"Default NLSR identity: " << m_signingInfo.getSignerName());
287 enableIncomingFaceIdIndication();
302 registerLocalhostPrefix();
303 registerRouterPrefix();
310 std::list<Adjacent>& neighbors = m_adjacencyList.
getAdjList();
312 for (std::list<Adjacent>::iterator it = neighbors.begin(); it != neighbors.end(); ++it) {
324 nlsrInstanceName.append(
"nlsr");
327 m_keyChain.deleteIdentity(m_keyChain.getPib().getIdentity(nlsrInstanceName));
328 }
catch (
const std::exception& e) {
332 auto nlsrInstanceIdentity = m_keyChain.createIdentity(nlsrInstanceName);
333 auto nlsrInstanceKey = nlsrInstanceIdentity.getDefaultKey();
335 ndn::security::v2::Certificate certificate;
337 ndn::Name certificateName = nlsrInstanceKey.getName();
338 certificateName.append(
"NA");
339 certificateName.appendVersion();
340 certificate.setName(certificateName);
343 certificate.setContentType(ndn::tlv::ContentType_Key);
344 certificate.setFreshnessPeriod(ndn::time::days(365));
347 certificate.setContent(nlsrInstanceKey.getPublicKey().data(), nlsrInstanceKey.getPublicKey().size());
350 ndn::SignatureInfo signatureInfo;
351 signatureInfo.setValidityPeriod(ndn::security::ValidityPeriod(ndn::time::system_clock::TimePoint(),
352 ndn::time::system_clock::now()
353 + ndn::time::days(365)));
355 m_keyChain.sign(certificate,
356 ndn::security::SigningInfo(m_keyChain.getPib().getIdentity(m_confParam.
getRouterPrefix()))
357 .setSignatureInfo(signatureInfo));
359 catch (
const std::exception& e) {
361 <<
"NLSR is running without security." 362 <<
" If security is enabled NLSR will not converge.");
364 std::cerr <<
"Router's " << e.what() <<
". NLSR is running without security " 365 <<
"(Only for testing, should not be used in production.)" 366 <<
" If security is enabled NLSR will not converge." << std::endl;
369 m_signingInfo = ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_ID,
374 m_defaultCertName = certificate.getName();
378 Nlsr::registerKeyPrefix()
382 nlsrKeyPrefix.append(
"nlsr");
383 nlsrKeyPrefix.append(
"KEY");
385 m_nlsrFace.setInterestFilter(nlsrKeyPrefix,
386 std::bind(&Nlsr::onKeyInterest,
388 std::bind(&Nlsr::onKeyPrefixRegSuccess,
this, _1),
391 ndn::nfd::ROUTE_FLAG_CAPTURE);
395 routerKeyPrefix.append(
"KEY");
397 m_nlsrFace.setInterestFilter(routerKeyPrefix,
398 std::bind(&Nlsr::onKeyInterest,
400 std::bind(&Nlsr::onKeyPrefixRegSuccess,
this, _1),
403 ndn::nfd::ROUTE_FLAG_CAPTURE);
408 operatorKeyPrefix.append(std::string(
"%C1.Operator"));
410 m_nlsrFace.setInterestFilter(operatorKeyPrefix,
411 std::bind(&Nlsr::onKeyInterest,
413 std::bind(&Nlsr::onKeyPrefixRegSuccess,
this, _1),
416 ndn::nfd::ROUTE_FLAG_CAPTURE);
421 siteKeyPrefix.append(
"KEY");
423 m_nlsrFace.setInterestFilter(siteKeyPrefix,
424 std::bind(&Nlsr::onKeyInterest,
426 std::bind(&Nlsr::onKeyPrefixRegSuccess,
this, _1),
429 ndn::nfd::ROUTE_FLAG_CAPTURE);
433 Nlsr::registerLocalhostPrefix()
441 Nlsr::registerRouterPrefix()
443 m_nlsrFace.registerPrefix(ndn::Name(m_confParam.
getRouterPrefix()).append(
"nlsr"),
449 Nlsr::onKeyInterest(
const ndn::Name& name,
const ndn::Interest& interest)
451 NLSR_LOG_DEBUG(
"Got interest for certificate. Interest: " << interest.getName());
453 const ndn::Name& interestName = interest.getName();
454 const ndn::security::v2::Certificate* cert =
getCertificate(interestName);
456 if (cert ==
nullptr) {
461 m_nlsrFace.put(*cert);
465 Nlsr::onKeyPrefixRegSuccess(
const ndn::Name& name)
467 NLSR_LOG_DEBUG(
"KEY prefix: " << name <<
" registration is successful.");
471 Nlsr::onFaceEventNotification(
const ndn::nfd::FaceEventNotification& faceEventNotification)
475 switch (faceEventNotification.getKind()) {
476 case ndn::nfd::FACE_EVENT_DESTROYED: {
477 uint64_t faceId = faceEventNotification.getFaceId();
481 if (adjacent != m_adjacencyList.
end()) {
482 NLSR_LOG_DEBUG(
"Face to " << adjacent->getName() <<
" with face id: " << faceId <<
" destroyed");
484 adjacent->setFaceId(0);
518 case ndn::nfd::FACE_EVENT_CREATED: {
520 ndn::FaceUri faceUri;
522 faceUri = ndn::FaceUri(faceEventNotification.getRemoteUri());
524 catch (
const std::exception& e) {
532 if (adjacent != m_adjacencyList.
end()) {
533 NLSR_LOG_DEBUG(
"Face creation event matches neighbor: " << adjacent->getName()
534 <<
". New Face ID: " << faceEventNotification.getFaceId()
535 <<
". Registering prefixes.");
536 adjacent->setFaceId(faceEventNotification.getFaceId());
560 m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(onFetchSuccess, onFetchFailure);
570 for (
auto& adjacent : m_adjacencyList.
getAdjList()) {
572 const std::string faceUriString = adjacent.getFaceUri().toString();
574 for (
const ndn::nfd::FaceStatus& faceStatus : faces) {
577 if (adjacent.getFaceId() == 0 && faceUriString == faceStatus.getRemoteUri()) {
579 " FaceId: "<< faceStatus.getFaceId());
580 adjacent.setFaceId(faceStatus.getFaceId());
589 if (adjacent.getFaceId() == 0) {
591 " has no Face information in this dataset.");
595 scheduleDatasetFetch();
600 const ndn::time::milliseconds& timeout)
604 const ndn::Name& adjName = adj.
getName();
607 timeout, ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
610 faceUri, linkCost, timeout,
611 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
614 faceUri, linkCost, timeout,
615 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
620 const std::string& msg,
621 uint32_t nRetriesSoFar)
626 NLSR_LOG_DEBUG(
"Failed to fetch dataset: " << msg <<
". Attempting retry #" << nRetriesSoFar);
630 this, _1, _2, nRetriesSoFar));
633 NLSR_LOG_ERROR(
"Failed to fetch dataset: " << msg <<
". Exceeded limit of " <<
638 scheduleDatasetFetch();
643 Nlsr::scheduleDatasetFetch()
650 [
this] (
const std::vector<ndn::nfd::FaceStatus>& faces) {
653 [
this] (uint32_t code,
const std::string& msg) {
660 Nlsr::enableIncomingFaceIdIndication()
662 NLSR_LOG_DEBUG(
"Enabling incoming face id indication for local face.");
664 m_controller.start<ndn::nfd::FaceUpdateCommand>(
665 ndn::nfd::ControlParameters()
666 .setFlagBit(ndn::nfd::FaceFlagBit::BIT_LOCAL_FIELDS_ENABLED,
true),
667 bind(&Nlsr::onFaceIdIndicationSuccess,
this, _1),
668 bind(&Nlsr::onFaceIdIndicationFailure,
this, _1));
672 Nlsr::onFaceIdIndicationSuccess(
const ndn::nfd::ControlParameters& cp)
675 <<
"for face id " << cp.getFaceId());
679 Nlsr::onFaceIdIndicationFailure(
const ndn::nfd::ControlResponse& cr)
681 std::ostringstream os;
682 os <<
"Failed to enable incoming face id indication feature: " <<
683 "(code: " << cr.getCode() <<
", reason: " << cr.getText() <<
")";
691 m_nlsrFace.processEvents();
void initializeFaces(const FetchDatasetCallback &onFetchSuccess, const FetchDatasetTimeoutCallback &onFetchFailure)
Initializes neighbors' Faces using information from NFD.
void onFaceDatasetFetchTimeout(uint32_t code, const std::string &reason, uint32_t nRetriesSoFar)
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
const ndn::time::milliseconds & getSyncInterestLifetime() const
void scheduleAdjLsaBuild()
Schedules a build of this router's LSA.
std::function< void(uint32_t, const std::string &)> FetchDatasetTimeoutCallback
ConfParameter & getConfParameter()
void setAdjLsaBuildInterval(uint32_t interval)
const ndn::FaceUri & getFaceUri() const
const std::string & getSeqFileDir() const
void connectToFetcher(ndn::util::SegmentFetcher &fetcher)
#define NLSR_LOG_DEBUG(x)
const ndn::Name & getRouterPrefix() const
std::function< void(const std::vector< ndn::nfd::FaceStatus > &)> FetchDatasetCallback
void initiateSeqNoFromFile(int hypState)
void setStrategy(const ndn::Name &name, const std::string &strategy, uint32_t count)
SyncLogicHandler & getSyncLogicHandler()
RoutingTable & getRoutingTable()
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California.
void setLsaInterestFilter()
void addDispatcherTopPrefix(const ndn::Name &topPrefix)
Add top level prefixes for Dispatcher.
static const ndn::Name LOCALHOST_PREFIX
void scheduleInterest(uint32_t seconds)
Schedules a Hello Interest event.
void registerAdjacencyPrefixes(const Adjacent &adj, const ndn::time::milliseconds &timeout)
Registers NLSR-specific prefixes for a neighbor (Adjacent)
#define INIT_LOGGER(name)
void canonizeNeighborUris(std::list< Adjacent >::iterator currentNeighbor, std::function< void(std::list< Adjacent >::iterator)> then, std::function< void(void)> finally)
Canonize the URI for this and all proceeding neighbors in a list.
uint32_t getInterestRetryNumber() const
ndn::security::ValidatorConfig & getValidator()
const ndn::Name & getName() const
void insert(const ndn::security::v2::Certificate &certificate)
const ndn::Name & getLsaPrefix() const
void setLsaRefreshTime(const ndn::time::seconds &lsaRefreshTime)
void scheduleRoutingTableCalculation(Nlsr &pnlsr)
Schedules a calculation event in the event scheduler only if one isn't already scheduled.
void onRegistrationSuccess(const ndn::Name &name)
void setEntryRefreshTime(int32_t fert)
void loadCertToPublish(const ndn::security::v2::Certificate &certificate)
Add a certificate NLSR claims to be authoritative for to the certificate store.
void publishCertFromCache(const ndn::Name &keyName)
Retrieves the chain of certificates from Validator's cache and store them in Nlsr's own CertificateSt...
void processInterest(const ndn::Name &name, const ndn::Interest &interest)
Processes a Hello Interest from a neighbor.
void setInfoInterestFilter()
void afterFetcherSignalEmitted(const ndn::Data &lsaSegment)
Callback when SegmentFetcher retrieves a segment.
SequencingManager & getSequencingManager()
void processInterest(const ndn::Name &name, const ndn::Interest &interest)
uint32_t getLsaRefreshTime() const
void setRoutingCalcInterval(uint32_t interval)
const ndn::Name & getChronosyncPrefix() const
A neighbor reachable over a Face.
#define NLSR_LOG_ERROR(x)
void registrationFailed(const ndn::Name &name)
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
uint32_t getAdjLsaBuildInterval() const
AdjacencyList::iterator findAdjacent(const ndn::Name &adjName)
const ndn::time::seconds TIME_ALLOWED_FOR_CANONIZATION
void setThisRouterPrefix(std::string trp)
uint32_t getFirstHelloInterval() const
uint32_t getFaceDatasetFetchTries() const
const ndn::Name & getNetwork() const
const ndn::security::v2::Certificate * getCertificate(const ndn::Name &certificateKeyName)
Find a certificate.
Nlsr(boost::asio::io_service &ioService, ndn::Scheduler &scheduler, ndn::Face &face, ndn::KeyChain &keyChain)
void setRouterNameCommandPrefix(const ndn::Name &routerName)
void createSyncSocket(const ndn::Name &syncPrefix, const ndn::time::milliseconds &syncInterestLifetime=ndn::time::milliseconds(SYNC_INTEREST_LIFETIME_DEFAULT))
Create and configure a socket to enable ChronoSync for this NLSR.
bool buildAndInstallOwnNameLsa()
Builds a name LSA for this router and then installs it into the LSDB.
bool buildAndInstallOwnCoordinateLsa()
Builds a cor. LSA for this router and installs it into the LSDB.
int32_t getHyperbolicState() const
uint64_t getLinkCost() const
uint32_t getRoutingCalcInterval() const
void setSeqFileDirectory(std::string filePath)
Set the sequence file directory.
void processFaceDataset(const std::vector< ndn::nfd::FaceStatus > &faces)
Consumes a Face StatusDataset to configure NLSR neighbors.
const ndn::time::seconds getFaceDatasetFetchInterval() const
std::list< Adjacent > & getAdjList()
#define NLSR_LOG_TRACE(x)
const_iterator end() const
void registerPrefix(const ndn::Name &namePrefix, const ndn::FaceUri &faceUri, uint64_t faceCost, const ndn::time::milliseconds &timeout, uint64_t flags, uint8_t times)
Inform NFD of a next-hop.