32 #include <ndn-cxx/net/face-uri.hpp> 33 #include <ndn-cxx/signature.hpp> 41 Nlsr::Nlsr(boost::asio::io_service& ioService, ndn::Scheduler& scheduler, ndn::Face& face, ndn::KeyChain& keyChain)
43 , m_scheduler(scheduler)
44 , m_keyChain(keyChain)
48 , m_isDaemonProcess(false)
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_localhostDispatcher(m_nlsrFace, m_keyChain)
59 , m_routerNameDispatcher(m_nlsrFace, m_keyChain)
60 , m_lsdbDatasetHandler(m_nlsrLsdb,
61 m_localhostDispatcher,
62 m_routerNameDispatcher,
65 , m_helloProtocol(*this, scheduler)
66 , m_validator(
ndn::make_unique<
ndn::security::v2::CertificateFetcherDirectFetch>(m_nlsrFace))
67 , m_controller(m_nlsrFace, m_keyChain)
68 , m_faceDatasetController(m_nlsrFace, m_keyChain)
69 , m_prefixUpdateProcessor(m_localhostDispatcher,
73 , m_nfdRibCommandProcessor(m_localhostDispatcher,
76 , m_statsCollector(m_nlsrLsdb, m_helloProtocol)
77 , m_faceMonitor(m_nlsrFace)
80 m_faceMonitor.onNotification.connect(std::bind(&Nlsr::onFaceEventNotification,
this, _1));
81 m_faceMonitor.start();
87 NLSR_LOG_ERROR(
"ERROR: Failed to register prefix in local hub's daemon");
88 BOOST_THROW_EXCEPTION(
Error(
"Error: Prefix registration failed"));
104 NLSR_LOG_DEBUG(
"Setting interest filter for Hello interest: " << name);
108 &m_helloProtocol, _1, _2),
112 ndn::nfd::ROUTE_FLAG_CAPTURE);
122 NLSR_LOG_DEBUG(
"Setting interest filter for LsaPrefix: " << name);
126 &m_nlsrLsdb, _1, _2),
130 ndn::nfd::ROUTE_FLAG_CAPTURE);
139 m_routerNameDispatcher.addTopPrefix(topPrefix,
false, m_signingInfo);
142 m_localhostDispatcher.addTopPrefix(topPrefix,
false, m_signingInfo);
145 catch (
const std::exception& e) {
146 NLSR_LOG_ERROR(
"Error setting top-level prefix in dispatcher: " << e.what() <<
"\n");
153 const std::string strategy(
"ndn:/localhost/nfd/strategy/multicast");
162 pid_t process_id = 0;
166 std::cerr <<
"Daemonization failed!" << std::endl;
167 BOOST_THROW_EXCEPTION(
Error(
"Error: Daemonization process- fork failed!"));
169 if (process_id > 0) {
177 BOOST_THROW_EXCEPTION(
Error(
"Error: Daemonization process- setting id failed!"));
180 if (chdir(
"/") < 0) {
181 BOOST_THROW_EXCEPTION(
Error(
"Error: Daemonization process-chdir failed!"));
186 Nlsr::canonizeContinuation(std::list<Adjacent>::iterator iterator,
187 std::function<
void(
void)>
finally)
190 canonizeContinuation(iterator,
finally);
197 std::function<
void(std::list<Adjacent>::iterator)> then,
198 std::function<
void(
void)>
finally)
200 if (currentNeighbor != m_adjacencyList.
getAdjList().end()) {
201 ndn::FaceUri uri(currentNeighbor->getFaceUri());
202 uri.canonize([
this, then, currentNeighbor] (ndn::FaceUri canonicalUri) {
204 <<
" to: " << canonicalUri);
205 currentNeighbor->setFaceUri(canonicalUri);
206 then(std::next(currentNeighbor));
208 [
this, then, currentNeighbor] (
const std::string& reason) {
209 NLSR_LOG_ERROR(
"Could not canonize URI: " << currentNeighbor->getFaceUri()
210 <<
" because: " << reason);
211 then(std::next(currentNeighbor));
213 m_nlsrFace.getIoService(),
226 m_certStore.
insert(certificate);
227 m_validator.loadAnchor(
"Authoritative-Certificate",
228 ndn::security::v2::Certificate(certificate));
230 loadAnchor(
"Authoritative-Certificate",
231 ndn::security::v2::Certificate(certificate));
258 NLSR_LOG_DEBUG(
"Default NLSR identity: " << m_signingInfo.getSignerName());
270 enableIncomingFaceIdIndication();
285 registerLocalhostPrefix();
292 std::list<Adjacent>& neighbors = m_adjacencyList.
getAdjList();
294 for (std::list<Adjacent>::iterator it = neighbors.begin(); it != neighbors.end(); ++it) {
306 nlsrInstanceName.append(
"NLSR");
309 m_keyChain.deleteIdentity(m_keyChain.getPib().getIdentity(nlsrInstanceName));
310 }
catch (
const std::exception& e) {
314 auto nlsrInstanceIdentity = m_keyChain.createIdentity(nlsrInstanceName);
315 auto nlsrInstanceKey = nlsrInstanceIdentity.getDefaultKey();
317 ndn::security::v2::Certificate certificate;
319 ndn::Name certificateName = nlsrInstanceKey.getName();
320 certificateName.append(
"NA");
321 certificateName.appendVersion();
322 certificate.setName(certificateName);
325 certificate.setContentType(ndn::tlv::ContentType_Key);
326 certificate.setFreshnessPeriod(ndn::time::days(7300));
329 certificate.setContent(nlsrInstanceKey.getPublicKey().data(), nlsrInstanceKey.getPublicKey().size());
332 ndn::SignatureInfo signatureInfo;
333 signatureInfo.setValidityPeriod(ndn::security::ValidityPeriod(ndn::time::system_clock::TimePoint(),
334 ndn::time::system_clock::now()
335 + ndn::time::days(20 * 365)));
337 m_keyChain.sign(certificate,
338 ndn::security::SigningInfo(m_keyChain.getPib().getIdentity(m_confParam.
getRouterPrefix()))
339 .setSignatureInfo(signatureInfo));
341 catch (
const std::exception& e) {
343 <<
"NLSR is running without security." 344 <<
" If security is enabled NLSR will not converge.");
346 std::cerr <<
"Router's " << e.what() <<
"NLSR is running without security " 347 <<
"(Only for testing, should not be used in production.)" 348 <<
" If security is enabled NLSR will not converge." << std::endl;
351 m_signingInfo = ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_ID,
356 m_defaultCertName = certificate.getName();
360 Nlsr::registerKeyPrefix()
364 nlsrKeyPrefix.append(
"NLSR");
365 nlsrKeyPrefix.append(
"KEY");
367 m_nlsrFace.setInterestFilter(nlsrKeyPrefix,
368 std::bind(&Nlsr::onKeyInterest,
370 std::bind(&Nlsr::onKeyPrefixRegSuccess,
this, _1),
373 ndn::nfd::ROUTE_FLAG_CAPTURE);
377 routerKeyPrefix.append(
"KEY");
379 m_nlsrFace.setInterestFilter(routerKeyPrefix,
380 std::bind(&Nlsr::onKeyInterest,
382 std::bind(&Nlsr::onKeyPrefixRegSuccess,
this, _1),
385 ndn::nfd::ROUTE_FLAG_CAPTURE);
390 operatorKeyPrefix.append(std::string(
"%C1.Operator"));
392 m_nlsrFace.setInterestFilter(operatorKeyPrefix,
393 std::bind(&Nlsr::onKeyInterest,
395 std::bind(&Nlsr::onKeyPrefixRegSuccess,
this, _1),
398 ndn::nfd::ROUTE_FLAG_CAPTURE);
403 siteKeyPrefix.append(
"KEY");
405 m_nlsrFace.setInterestFilter(siteKeyPrefix,
406 std::bind(&Nlsr::onKeyInterest,
408 std::bind(&Nlsr::onKeyPrefixRegSuccess,
this, _1),
411 ndn::nfd::ROUTE_FLAG_CAPTURE);
415 Nlsr::registerLocalhostPrefix()
423 Nlsr::onKeyInterest(
const ndn::Name& name,
const ndn::Interest& interest)
425 NLSR_LOG_DEBUG(
"Got interest for certificate. Interest: " << interest.getName());
427 const ndn::Name& interestName = interest.getName();
428 const ndn::security::v2::Certificate* cert =
getCertificate(interestName);
430 if (cert ==
nullptr) {
435 m_nlsrFace.put(*cert);
439 Nlsr::onKeyPrefixRegSuccess(
const ndn::Name& name)
441 NLSR_LOG_DEBUG(
"KEY prefix: " << name <<
" registration is successful.");
445 Nlsr::onFaceEventNotification(
const ndn::nfd::FaceEventNotification& faceEventNotification)
449 switch (faceEventNotification.getKind()) {
450 case ndn::nfd::FACE_EVENT_DESTROYED: {
451 uint64_t faceId = faceEventNotification.getFaceId();
455 if (adjacent != m_adjacencyList.
end()) {
456 NLSR_LOG_DEBUG(
"Face to " << adjacent->getName() <<
" with face id: " << faceId <<
" destroyed");
458 adjacent->setFaceId(0);
492 case ndn::nfd::FACE_EVENT_CREATED: {
495 ndn::FaceUri(faceEventNotification.getRemoteUri()));
498 if (adjacent != m_adjacencyList.
end()) {
499 NLSR_LOG_DEBUG(
"Face creation event matches neighbor: " << adjacent->getName()
500 <<
". New Face ID: " << faceEventNotification.getFaceId()
501 <<
". Registering prefixes.");
502 adjacent->setFaceId(faceEventNotification.getFaceId());
526 m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(onFetchSuccess, onFetchFailure);
536 for (
auto& adjacent : m_adjacencyList.
getAdjList()) {
538 const std::string faceUriString = adjacent.getFaceUri().toString();
540 for (
const ndn::nfd::FaceStatus& faceStatus : faces) {
543 if (adjacent.getFaceId() == 0 && faceUriString == faceStatus.getRemoteUri()) {
545 " FaceId: "<< faceStatus.getFaceId());
546 adjacent.setFaceId(faceStatus.getFaceId());
555 if (adjacent.getFaceId() == 0) {
557 " has no Face information in this dataset.");
561 scheduleDatasetFetch();
566 const ndn::time::milliseconds& timeout)
570 const ndn::Name& adjName = adj.
getName();
573 timeout, ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
576 faceUri, linkCost, timeout,
577 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
580 faceUri, linkCost, timeout,
581 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
586 const std::string& msg,
587 uint32_t nRetriesSoFar)
592 NLSR_LOG_DEBUG(
"Failed to fetch dataset: " << msg <<
". Attempting retry #" << nRetriesSoFar);
596 this, _1, _2, nRetriesSoFar));
599 NLSR_LOG_ERROR(
"Failed to fetch dataset: " << msg <<
". Exceeded limit of " <<
604 scheduleDatasetFetch();
609 Nlsr::scheduleDatasetFetch()
616 [
this] (
const std::vector<ndn::nfd::FaceStatus>& faces) {
619 [
this] (uint32_t code,
const std::string& msg) {
626 Nlsr::enableIncomingFaceIdIndication()
628 NLSR_LOG_DEBUG(
"Enabling incoming face id indication for local face.");
630 m_controller.start<ndn::nfd::FaceUpdateCommand>(
631 ndn::nfd::ControlParameters()
632 .setFlagBit(ndn::nfd::FaceFlagBit::BIT_LOCAL_FIELDS_ENABLED,
true),
633 bind(&Nlsr::onFaceIdIndicationSuccess,
this, _1),
634 bind(&Nlsr::onFaceIdIndicationFailure,
this, _1));
638 Nlsr::onFaceIdIndicationSuccess(
const ndn::nfd::ControlParameters& cp)
641 <<
"for face id " << cp.getFaceId());
645 Nlsr::onFaceIdIndicationFailure(
const ndn::nfd::ControlResponse& cr)
647 std::ostringstream os;
648 os <<
"Failed to enable incoming face id indication feature: " <<
649 "(code: " << cr.getCode() <<
", reason: " << cr.getText() <<
")";
657 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-2017, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
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
#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-2017, The University of Memphis, Regents of the University of California.
void setRouterNameCommandPrefix(const ndn::Name &routerName)
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 processInterest(const ndn::Name &name, const ndn::Interest &interest)
Processes a Hello Interest from a neighbor.
void setInfoInterestFilter()
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-2017, 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.
ndn::Face & getNlsrFace()
Nlsr(boost::asio::io_service &ioService, ndn::Scheduler &scheduler, ndn::Face &face, ndn::KeyChain &keyChain)
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.
const ndn::Name & getRouterName() const
int32_t getHyperbolicState() const
uint64_t getLinkCost() const
uint32_t getRoutingCalcInterval() const
void setSeqFileDirectory(std::string filePath)
Set the sequence file directory.
void createSyncSocket(const ndn::Name &syncPrefix)
Create and configure a socket to enable ChronoSync for this NLSR.
const ndn::Name & getSiteName() const
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 writeLog()
Dump the current state of all attributes to the log.
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.