blob: 214e6ec864108da4b2e431eb3e7c73eaa38a3670 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Saurab Dulal7526cee2018-01-31 18:14:10 +00003 * Copyright (c) 2014-2019, The University of Memphis,
Vince Lehmanc2e51f62015-01-20 15:03:11 -06004 * Regents of the University of California,
5 * Arizona Board of Regents.
akmhoque3d06e792014-05-27 16:23:20 -05006 *
7 * This file is part of NLSR (Named-data Link State Routing).
8 * See AUTHORS.md for complete list of NLSR authors and contributors.
9 *
10 * NLSR is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
akmhoque3d06e792014-05-27 16:23:20 -050020 **/
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -050021
Laqin Fan54a43f02017-03-08 12:31:30 -060022#include "nlsr.hpp"
23#include "adjacent.hpp"
24#include "logger.hpp"
25
akmhoque298385a2014-02-13 14:13:09 -060026#include <cstdlib>
akmhoque92afde42014-02-18 14:04:07 -060027#include <string>
akmhoque298385a2014-02-13 14:13:09 -060028#include <sstream>
akmhoque05d5fcf2014-04-15 14:58:45 -050029#include <cstdio>
akmhoque0494c252014-07-23 23:46:44 -050030#include <unistd.h>
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +000031#include <vector>
akmhoque298385a2014-02-13 14:13:09 -060032
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050033#include <ndn-cxx/net/face-uri.hpp>
34#include <ndn-cxx/signature.hpp>
akmhoque298385a2014-02-13 14:13:09 -060035
akmhoque53353462014-04-22 08:43:45 -050036namespace nlsr {
37
dmcoomescf8d0ed2017-02-21 11:39:01 -060038INIT_LOGGER(Nlsr);
akmhoque674b0b12014-05-20 14:33:28 -050039
alvy297f4162015-03-03 17:15:33 -060040const ndn::Name Nlsr::LOCALHOST_PREFIX = ndn::Name("/localhost/nlsr");
41
Laqin Fana4cf4022017-01-03 18:57:35 +000042Nlsr::Nlsr(boost::asio::io_service& ioService, ndn::Scheduler& scheduler, ndn::Face& face, ndn::KeyChain& keyChain)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050043 : m_nlsrFace(face)
44 , m_scheduler(scheduler)
Laqin Fana4cf4022017-01-03 18:57:35 +000045 , m_keyChain(keyChain)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050046 , m_confParam()
47 , m_adjacencyList()
48 , m_namePrefixList()
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050049 , m_configFileName("nlsr.conf")
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050050 , m_nlsrLsdb(*this, scheduler)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050051 , m_adjBuildCount(0)
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)
Nick Gordonb7b58392017-08-17 16:29:21 -050057 , m_namePrefixTable(*this, m_routingTable.afterRoutingChange)
laqinfan35731852017-08-08 06:17:39 -050058 , m_dispatcher(m_nlsrFace, m_keyChain)
59 , m_datasetHandler(m_nlsrLsdb,
60 m_routingTable,
61 m_dispatcher,
62 m_nlsrFace,
63 m_keyChain)
Vince Lehmanf7eec4f2015-05-08 19:02:31 -050064 , m_helloProtocol(*this, scheduler)
Davide Pesaventoa08dc3f2018-05-24 00:40:28 -040065 , m_validator(std::make_unique<ndn::security::v2::CertificateFetcherDirectFetch>(m_nlsrFace))
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050066 , m_controller(m_nlsrFace, m_keyChain)
Nick Gordond5c1a372016-10-31 13:56:23 -050067 , m_faceDatasetController(m_nlsrFace, m_keyChain)
laqinfan35731852017-08-08 06:17:39 -050068 , m_prefixUpdateProcessor(m_dispatcher,
Laqin Fan54a43f02017-03-08 12:31:30 -060069 m_nlsrFace,
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050070 m_namePrefixList,
Saurab Dulal7526cee2018-01-31 18:14:10 +000071 m_nlsrLsdb,
72 m_configFileName)
laqinfan35731852017-08-08 06:17:39 -050073 , m_nfdRibCommandProcessor(m_dispatcher,
Nick Gordon4d2c6c02017-01-20 13:18:46 -060074 m_namePrefixList,
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050075 m_nlsrLsdb)
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060076 , m_statsCollector(m_nlsrLsdb, m_helloProtocol)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050077 , m_faceMonitor(m_nlsrFace)
78 , m_firstHelloInterval(FIRST_HELLO_INTERVAL_DEFAULT)
79{
dmcoomes9f936662017-03-02 10:33:09 -060080 m_faceMonitor.onNotification.connect(std::bind(&Nlsr::onFaceEventNotification, this, _1));
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050081 m_faceMonitor.start();
82}
83
akmhoque53353462014-04-22 08:43:45 -050084void
85Nlsr::registrationFailed(const ndn::Name& name)
akmhoque298385a2014-02-13 14:13:09 -060086{
dmcoomes5bcb39e2017-10-31 15:07:55 -050087 NLSR_LOG_ERROR("ERROR: Failed to register prefix in local hub's daemon");
dmcoomes9f936662017-03-02 10:33:09 -060088 BOOST_THROW_EXCEPTION(Error("Error: Prefix registration failed"));
akmhoque53353462014-04-22 08:43:45 -050089}
akmhoque1fd8c1e2014-02-19 19:41:49 -060090
akmhoque157b0a42014-05-13 00:26:37 -050091void
92Nlsr::onRegistrationSuccess(const ndn::Name& name)
93{
dmcoomes5bcb39e2017-10-31 15:07:55 -050094 NLSR_LOG_DEBUG("Successfully registered prefix: " << name);
alvy297f4162015-03-03 17:15:33 -060095}
96
97void
akmhoque31d1d4b2014-05-05 22:08:14 -050098Nlsr::setInfoInterestFilter()
akmhoque53353462014-04-22 08:43:45 -050099{
akmhoque31d1d4b2014-05-05 22:08:14 -0500100 ndn::Name name(m_confParam.getRouterPrefix());
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500101 name.append("nlsr");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500102 name.append("INFO");
103
104 NLSR_LOG_DEBUG("Setting interest filter for Hello interest: " << name);
105
Ashlesh Gawande3494f732018-11-06 16:04:03 -0600106 m_nlsrFace.setInterestFilter(ndn::InterestFilter(name).allowLoopback(false),
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500107 std::bind(&HelloProtocol::processInterest,
108 &m_helloProtocol, _1, _2),
109 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
110 std::bind(&Nlsr::registrationFailed, this, _1),
111 m_signingInfo,
112 ndn::nfd::ROUTE_FLAG_CAPTURE);
akmhoque31d1d4b2014-05-05 22:08:14 -0500113}
114
115void
116Nlsr::setLsaInterestFilter()
117{
akmhoque157b0a42014-05-13 00:26:37 -0500118 ndn::Name name = m_confParam.getLsaPrefix();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500119
dmcoomes5bcb39e2017-10-31 15:07:55 -0500120 NLSR_LOG_DEBUG("Setting interest filter for LsaPrefix: " << name);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500121
Ashlesh Gawande3494f732018-11-06 16:04:03 -0600122 m_nlsrFace.setInterestFilter(ndn::InterestFilter(name).allowLoopback(false),
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500123 std::bind(&Lsdb::processInterest,
124 &m_nlsrLsdb, _1, _2),
125 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
126 std::bind(&Nlsr::registrationFailed, this, _1),
127 m_signingInfo,
128 ndn::nfd::ROUTE_FLAG_CAPTURE);
akmhoque53353462014-04-22 08:43:45 -0500129}
130
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500131
132void
133Nlsr::addDispatcherTopPrefix(const ndn::Name& topPrefix)
134{
135 try {
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500136 // false since we want to have control over the registration process
laqinfan35731852017-08-08 06:17:39 -0500137 m_dispatcher.addTopPrefix(topPrefix, false, m_signingInfo);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500138 }
139 catch (const std::exception& e) {
140 NLSR_LOG_ERROR("Error setting top-level prefix in dispatcher: " << e.what() << "\n");
141 }
142}
143
akmhoque53353462014-04-22 08:43:45 -0500144void
akmhoquec04e7272014-07-02 11:00:14 -0500145Nlsr::setStrategies()
akmhoque157b0a42014-05-13 00:26:37 -0500146{
Vince Lehman53c0e3e2015-09-14 14:33:20 -0500147 const std::string strategy("ndn:/localhost/nfd/strategy/multicast");
148
akmhoque393d4ff2014-07-16 14:27:03 -0500149 m_fib.setStrategy(m_confParam.getLsaPrefix(), strategy, 0);
akmhoque393d4ff2014-07-16 14:27:03 -0500150 m_fib.setStrategy(m_confParam.getChronosyncPrefix(), strategy, 0);
akmhoque157b0a42014-05-13 00:26:37 -0500151}
152
153void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500154Nlsr::loadCertToPublish(const ndn::security::v2::Certificate& certificate)
155{
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000156 NLSR_LOG_TRACE("Loading cert to publish.");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500157 m_certStore.insert(certificate);
158 m_validator.loadAnchor("Authoritative-Certificate",
159 ndn::security::v2::Certificate(certificate));
160 m_prefixUpdateProcessor.getValidator().
161 loadAnchor("Authoritative-Certificate",
162 ndn::security::v2::Certificate(certificate));
163}
164
Nick Gordon9461afb2017-04-25 15:54:50 -0500165void
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000166Nlsr::connectToFetcher(ndn::util::SegmentFetcher& fetcher)
167{
168 NLSR_LOG_TRACE("NLSR: Connect to SegmentFetcher.");
169
170 fetcher.afterSegmentValidated.connect(std::bind(&Nlsr::afterFetcherSignalEmitted,
171 this, _1));
172}
173
174void
175Nlsr::afterFetcherSignalEmitted(const ndn::Data& lsaSegment)
176{
177 NLSR_LOG_TRACE("SegmentFetcher fetched a data segment. Start inserting cert to own cert store.");
178 ndn::Name keyName = lsaSegment.getSignature().getKeyLocator().getName();
179 if (getCertificate(keyName) == nullptr) {
180 publishCertFromCache(keyName);
181 }
182 else {
183 NLSR_LOG_TRACE("Certificate is already in the store: " << keyName);
184 }
185}
186
187void
188Nlsr::publishCertFromCache(const ndn::Name& keyName)
189{
190 const ndn::security::v2::Certificate* cert = m_validator.getUnverifiedCertCache()
191 .find(keyName);
192 if (cert != nullptr) {
193 m_certStore.insert(*cert);
194 NLSR_LOG_TRACE(*cert);
Ashlesh Gawande3494f732018-11-06 16:04:03 -0600195 ndn::Name certName = ndn::security::v2::extractKeyNameFromCertName(cert->getName());
196 NLSR_LOG_TRACE("Setting interest filter for: " << certName);
197 m_nlsrFace.setInterestFilter(ndn::InterestFilter(certName).allowLoopback(false),
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000198 std::bind(&Nlsr::onKeyInterest,
199 this, _1, _2),
200 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
201 std::bind(&Nlsr::registrationFailed, this, _1),
202 m_signingInfo,
203 ndn::nfd::ROUTE_FLAG_CAPTURE);
204
205 if (!cert->getKeyName().equals(cert->getSignature().getKeyLocator().getName())) {
206 publishCertFromCache(cert->getSignature().getKeyLocator().getName());
207 }
208 }
209 else {
210 NLSR_LOG_TRACE("Cert for " << keyName << " was not found in the Validator's cache. ");
211 }
212}
213
214void
akmhoque53353462014-04-22 08:43:45 -0500215Nlsr::initialize()
216{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500217 NLSR_LOG_DEBUG("Initializing Nlsr");
akmhoque53353462014-04-22 08:43:45 -0500218 m_confParam.buildRouterPrefix();
laqinfan35731852017-08-08 06:17:39 -0500219 m_datasetHandler.setRouterNameCommandPrefix(m_confParam.getRouterPrefix());
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700220 m_nlsrLsdb.setLsaRefreshTime(ndn::time::seconds(m_confParam.getLsaRefreshTime()));
akmhoque31d1d4b2014-05-05 22:08:14 -0500221 m_nlsrLsdb.setThisRouterPrefix(m_confParam.getRouterPrefix().toUri());
akmhoque53353462014-04-22 08:43:45 -0500222 m_fib.setEntryRefreshTime(2 * m_confParam.getLsaRefreshTime());
Vince Lehmanc11cc202015-01-20 11:41:33 -0600223
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500224 m_nlsrLsdb.getSequencingManager().setSeqFileDirectory(m_confParam.getSeqFileDir());
225 m_nlsrLsdb.getSequencingManager().initiateSeqNoFromFile(m_confParam.getHyperbolicState());
226
Ashlesh Gawande48101072018-05-30 17:53:06 -0500227 m_nlsrLsdb.getSyncLogicHandler().createSyncLogic(m_confParam.getChronosyncPrefix(),
228 m_confParam.getSyncInterestLifetime());
Vince Lehmanc11cc202015-01-20 11:41:33 -0600229
dmcoomes9f936662017-03-02 10:33:09 -0600230 // Logging start
akmhoque674b0b12014-05-20 14:33:28 -0500231 m_confParam.writeLog();
232 m_adjacencyList.writeLog();
dmcoomes5bcb39e2017-10-31 15:07:55 -0500233 NLSR_LOG_DEBUG(m_namePrefixList);
dmcoomes9f936662017-03-02 10:33:09 -0600234 // Logging end
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500235
akmhoque443ad812014-07-29 10:26:56 -0500236 initializeKey();
akmhoquec04e7272014-07-02 11:00:14 -0500237 setStrategies();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500238
dmcoomes5bcb39e2017-10-31 15:07:55 -0500239 NLSR_LOG_DEBUG("Default NLSR identity: " << m_signingInfo.getSignerName());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500240
akmhoque31d1d4b2014-05-05 22:08:14 -0500241 setInfoInterestFilter();
242 setLsaInterestFilter();
Vince Lehman50df6b72015-03-03 12:06:40 -0600243
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500244 // add top-level prefixes: router and localhost prefix
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500245 addDispatcherTopPrefix(ndn::Name(m_confParam.getRouterPrefix()).append("nlsr"));
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500246 addDispatcherTopPrefix(LOCALHOST_PREFIX);
247
Nick Gordond5c1a372016-10-31 13:56:23 -0500248 initializeFaces(std::bind(&Nlsr::processFaceDataset, this, _1),
249 std::bind(&Nlsr::onFaceDatasetFetchTimeout, this, _1, _2, 0));
250
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500251 enableIncomingFaceIdIndication();
252
Vince Lehman50df6b72015-03-03 12:06:40 -0600253 // Set event intervals
254 setFirstHelloInterval(m_confParam.getFirstHelloInterval());
255 m_nlsrLsdb.setAdjLsaBuildInterval(m_confParam.getAdjLsaBuildInterval());
256 m_routingTable.setRoutingCalcInterval(m_confParam.getRoutingCalcInterval());
257
akmhoque674b0b12014-05-20 14:33:28 -0500258 m_nlsrLsdb.buildAndInstallOwnNameLsa();
Nick Gordon5c467f02016-07-13 13:40:10 -0500259
260 // Install coordinate LSAs if using HR or dry-run HR.
261 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
262 m_nlsrLsdb.buildAndInstallOwnCoordinateLsa();
263 }
Vince Lehman904c2412014-09-23 19:36:11 -0500264
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700265 registerKeyPrefix();
alvy297f4162015-03-03 17:15:33 -0600266 registerLocalhostPrefix();
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500267 registerRouterPrefix();
Vince Lehman7b616582014-10-17 16:25:39 -0500268
Vince Lehman7b616582014-10-17 16:25:39 -0500269 m_helloProtocol.scheduleInterest(m_firstHelloInterval);
Vince Lehman09131122014-09-09 17:10:11 -0500270
271 // Need to set direct neighbors' costs to 0 for hyperbolic routing
272 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
273
274 std::list<Adjacent>& neighbors = m_adjacencyList.getAdjList();
275
276 for (std::list<Adjacent>::iterator it = neighbors.begin(); it != neighbors.end(); ++it) {
277 it->setLinkCost(0);
278 }
279 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700280}
281
282void
akmhoque443ad812014-07-29 10:26:56 -0500283Nlsr::initializeKey()
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700284{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500285 NLSR_LOG_DEBUG("Initializing Key ...");
286
287 ndn::Name nlsrInstanceName = m_confParam.getRouterPrefix();
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500288 nlsrInstanceName.append("nlsr");
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700289
Joao Pereira97473d42015-07-03 16:57:27 -0400290 try {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500291 m_keyChain.deleteIdentity(m_keyChain.getPib().getIdentity(nlsrInstanceName));
292 } catch (const std::exception& e) {
293 NLSR_LOG_WARN(e.what());
294 }
295
296 auto nlsrInstanceIdentity = m_keyChain.createIdentity(nlsrInstanceName);
297 auto nlsrInstanceKey = nlsrInstanceIdentity.getDefaultKey();
298
299 ndn::security::v2::Certificate certificate;
300
301 ndn::Name certificateName = nlsrInstanceKey.getName();
302 certificateName.append("NA");
303 certificateName.appendVersion();
304 certificate.setName(certificateName);
305
306 // set metainfo
307 certificate.setContentType(ndn::tlv::ContentType_Key);
Ashlesh Gawande0d2c3822018-01-24 17:17:15 -0600308 certificate.setFreshnessPeriod(ndn::time::days(365));
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500309
310 // set content
311 certificate.setContent(nlsrInstanceKey.getPublicKey().data(), nlsrInstanceKey.getPublicKey().size());
312
313 // set signature-info
314 ndn::SignatureInfo signatureInfo;
315 signatureInfo.setValidityPeriod(ndn::security::ValidityPeriod(ndn::time::system_clock::TimePoint(),
316 ndn::time::system_clock::now()
Ashlesh Gawande0d2c3822018-01-24 17:17:15 -0600317 + ndn::time::days(365)));
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500318 try {
319 m_keyChain.sign(certificate,
320 ndn::security::SigningInfo(m_keyChain.getPib().getIdentity(m_confParam.getRouterPrefix()))
321 .setSignatureInfo(signatureInfo));
akmhoque102aea42014-08-04 10:22:12 -0500322 }
dmcoomes9f936662017-03-02 10:33:09 -0600323 catch (const std::exception& e) {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500324 NLSR_LOG_WARN("ERROR: Router's " << e.what()
325 << "NLSR is running without security."
326 << " If security is enabled NLSR will not converge.");
327
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000328 std::cerr << "Router's " << e.what() << ". NLSR is running without security "
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500329 << "(Only for testing, should not be used in production.)"
330 << " If security is enabled NLSR will not converge." << std::endl;
akmhoque102aea42014-08-04 10:22:12 -0500331 }
akmhoque443ad812014-07-29 10:26:56 -0500332
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500333 m_signingInfo = ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_ID,
334 nlsrInstanceName);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700335
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700336 loadCertToPublish(certificate);
337
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500338 m_defaultCertName = certificate.getName();
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700339}
340
341void
342Nlsr::registerKeyPrefix()
343{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500344 // Start listening for the interest of this router's NLSR certificate
345 ndn::Name nlsrKeyPrefix = getConfParameter().getRouterPrefix();
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500346 nlsrKeyPrefix.append("nlsr");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500347 nlsrKeyPrefix.append("KEY");
348
Ashlesh Gawande3494f732018-11-06 16:04:03 -0600349 m_nlsrFace.setInterestFilter(ndn::InterestFilter(nlsrKeyPrefix).allowLoopback(false),
Joao Pereira97473d42015-07-03 16:57:27 -0400350 std::bind(&Nlsr::onKeyInterest,
Yingdi Yu6a3a4dd2014-06-20 14:10:39 -0700351 this, _1, _2),
Joao Pereira97473d42015-07-03 16:57:27 -0400352 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
353 std::bind(&Nlsr::registrationFailed, this, _1),
354 m_signingInfo,
akmhoque060d3022014-08-12 13:35:06 -0500355 ndn::nfd::ROUTE_FLAG_CAPTURE);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700356
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500357 // Start listening for the interest of this router's certificate
358 ndn::Name routerKeyPrefix = getConfParameter().getRouterPrefix();
359 routerKeyPrefix.append("KEY");
360
Ashlesh Gawande3494f732018-11-06 16:04:03 -0600361 m_nlsrFace.setInterestFilter(ndn::InterestFilter(routerKeyPrefix).allowLoopback(false),
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500362 std::bind(&Nlsr::onKeyInterest,
363 this, _1, _2),
364 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
365 std::bind(&Nlsr::registrationFailed, this, _1),
366 m_signingInfo,
367 ndn::nfd::ROUTE_FLAG_CAPTURE);
368
369 // Start listening for the interest of this router's operator's certificate
370 ndn::Name operatorKeyPrefix = getConfParameter().getNetwork();
371 operatorKeyPrefix.append(getConfParameter().getSiteName());
372 operatorKeyPrefix.append(std::string("%C1.Operator"));
373
Ashlesh Gawande3494f732018-11-06 16:04:03 -0600374 m_nlsrFace.setInterestFilter(ndn::InterestFilter(operatorKeyPrefix).allowLoopback(false),
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500375 std::bind(&Nlsr::onKeyInterest,
376 this, _1, _2),
377 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
378 std::bind(&Nlsr::registrationFailed, this, _1),
379 m_signingInfo,
380 ndn::nfd::ROUTE_FLAG_CAPTURE);
381
382 // Start listening for the interest of this router's site's certificate
383 ndn::Name siteKeyPrefix = getConfParameter().getNetwork();
384 siteKeyPrefix.append(getConfParameter().getSiteName());
385 siteKeyPrefix.append("KEY");
386
Ashlesh Gawande3494f732018-11-06 16:04:03 -0600387 m_nlsrFace.setInterestFilter(ndn::InterestFilter(siteKeyPrefix).allowLoopback(false),
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500388 std::bind(&Nlsr::onKeyInterest,
389 this, _1, _2),
390 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
391 std::bind(&Nlsr::registrationFailed, this, _1),
392 m_signingInfo,
393 ndn::nfd::ROUTE_FLAG_CAPTURE);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700394}
395
396void
alvy297f4162015-03-03 17:15:33 -0600397Nlsr::registerLocalhostPrefix()
398{
alvy297f4162015-03-03 17:15:33 -0600399 m_nlsrFace.registerPrefix(LOCALHOST_PREFIX,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500400 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
alvy297f4162015-03-03 17:15:33 -0600401 std::bind(&Nlsr::registrationFailed, this, _1));
402}
403
404void
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500405Nlsr::registerRouterPrefix()
406{
407 m_nlsrFace.registerPrefix(ndn::Name(m_confParam.getRouterPrefix()).append("nlsr"),
408 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
409 std::bind(&Nlsr::registrationFailed, this, _1));
410}
411
412void
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700413Nlsr::onKeyInterest(const ndn::Name& name, const ndn::Interest& interest)
414{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500415 NLSR_LOG_DEBUG("Got interest for certificate. Interest: " << interest.getName());
416
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700417 const ndn::Name& interestName = interest.getName();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500418 const ndn::security::v2::Certificate* cert = getCertificate(interestName);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700419
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500420 if (cert == nullptr) {
421 NLSR_LOG_DEBUG("Certificate is not found for: " << interest);
dmcoomes9eaf3f42017-02-21 11:39:01 -0600422 return; // cert is not found
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500423 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700424
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500425 m_nlsrFace.put(*cert);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700426}
427
428void
429Nlsr::onKeyPrefixRegSuccess(const ndn::Name& name)
430{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500431 NLSR_LOG_DEBUG("KEY prefix: " << name << " registration is successful.");
akmhoque53353462014-04-22 08:43:45 -0500432}
akmhoque5a44dd42014-03-12 18:11:32 -0500433
akmhoque53353462014-04-22 08:43:45 -0500434void
akmhoquec04e7272014-07-02 11:00:14 -0500435Nlsr::onFaceEventNotification(const ndn::nfd::FaceEventNotification& faceEventNotification)
akmhoquee1765152014-06-30 11:32:01 -0500436{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500437 NLSR_LOG_TRACE("Nlsr::onFaceEventNotification called");
Vince Lehman02e32992015-03-11 12:31:20 -0500438
Nick Gordond5c1a372016-10-31 13:56:23 -0500439 switch (faceEventNotification.getKind()) {
440 case ndn::nfd::FACE_EVENT_DESTROYED: {
441 uint64_t faceId = faceEventNotification.getFaceId();
Vince Lehman02e32992015-03-11 12:31:20 -0500442
Nick Gordond5c1a372016-10-31 13:56:23 -0500443 auto adjacent = m_adjacencyList.findAdjacent(faceId);
Vince Lehman02e32992015-03-11 12:31:20 -0500444
Nick Gordond5c1a372016-10-31 13:56:23 -0500445 if (adjacent != m_adjacencyList.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500446 NLSR_LOG_DEBUG("Face to " << adjacent->getName() << " with face id: " << faceId << " destroyed");
Vince Lehman02e32992015-03-11 12:31:20 -0500447
Nick Gordond5c1a372016-10-31 13:56:23 -0500448 adjacent->setFaceId(0);
Vince Lehman02e32992015-03-11 12:31:20 -0500449
Nick Gordond5c1a372016-10-31 13:56:23 -0500450 // Only trigger an Adjacency LSA build if this node is changing
451 // from ACTIVE to INACTIVE since this rebuild will effectively
452 // cancel the previous Adjacency LSA refresh event and schedule
453 // a new one further in the future.
454 //
455 // Continuously scheduling the refresh in the future will block
456 // the router from refreshing its Adjacency LSA. Since other
457 // routers' Name prefixes' expiration times are updated when
458 // this router refreshes its Adjacency LSA, the other routers'
459 // prefixes will expire and be removed from the RIB.
460 //
461 // This check is required to fix Bug #2733 for now. This check
462 // would be unnecessary to fix Bug #2733 when Issue #2732 is
463 // completed, but the check also helps with optimization so it
464 // can remain even when Issue #2732 is implemented.
465 if (adjacent->getStatus() == Adjacent::STATUS_ACTIVE) {
466 adjacent->setStatus(Adjacent::STATUS_INACTIVE);
Vince Lehman02e32992015-03-11 12:31:20 -0500467
Nick Gordond5c1a372016-10-31 13:56:23 -0500468 // A new adjacency LSA cannot be built until the neighbor is marked INACTIVE and
469 // has met the HELLO retry threshold
470 adjacent->setInterestTimedOutNo(m_confParam.getInterestRetryNumber());
Vince Lehman199e9cf2015-04-07 13:22:16 -0500471
Nick Gordond5c1a372016-10-31 13:56:23 -0500472 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
473 getRoutingTable().scheduleRoutingTableCalculation(*this);
474 }
475 else {
476 m_nlsrLsdb.scheduleAdjLsaBuild();
477 }
Nick Gordone8e03ac2016-07-07 14:24:38 -0500478 }
Vince Lehman199e9cf2015-04-07 13:22:16 -0500479 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500480 break;
akmhoquec04e7272014-07-02 11:00:14 -0500481 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500482 case ndn::nfd::FACE_EVENT_CREATED: {
483 // Find the neighbor in our adjacency list
Ashlesh Gawande0d2c3822018-01-24 17:17:15 -0600484 ndn::FaceUri faceUri;
485 try {
486 faceUri = ndn::FaceUri(faceEventNotification.getRemoteUri());
487 }
488 catch (const std::exception& e) {
489 NLSR_LOG_WARN(e.what());
490 return;
491 }
492 auto adjacent = m_adjacencyList.findAdjacent(faceUri);
493
Nick Gordond5c1a372016-10-31 13:56:23 -0500494 // If we have a neighbor by that FaceUri and it has no FaceId, we
495 // have a match.
496 if (adjacent != m_adjacencyList.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500497 NLSR_LOG_DEBUG("Face creation event matches neighbor: " << adjacent->getName()
Nick Gordond5c1a372016-10-31 13:56:23 -0500498 << ". New Face ID: " << faceEventNotification.getFaceId()
499 << ". Registering prefixes.");
500 adjacent->setFaceId(faceEventNotification.getFaceId());
501
502 registerAdjacencyPrefixes(*adjacent, ndn::time::milliseconds::max());
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500503
504 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
505 getRoutingTable().scheduleRoutingTableCalculation(*this);
506 }
507 else {
508 m_nlsrLsdb.scheduleAdjLsaBuild();
509 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500510 }
511 break;
512 }
513 default:
514 break;
akmhoquec04e7272014-07-02 11:00:14 -0500515 }
akmhoquee1765152014-06-30 11:32:01 -0500516}
517
Nick Gordond5c1a372016-10-31 13:56:23 -0500518void
519Nlsr::initializeFaces(const FetchDatasetCallback& onFetchSuccess,
520 const FetchDatasetTimeoutCallback& onFetchFailure)
521{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500522 NLSR_LOG_TRACE("Initializing Faces...");
Nick Gordond5c1a372016-10-31 13:56:23 -0500523
524 m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(onFetchSuccess, onFetchFailure);
525
526}
527
528void
529Nlsr::processFaceDataset(const std::vector<ndn::nfd::FaceStatus>& faces)
530{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500531 NLSR_LOG_DEBUG("Processing face dataset");
Nick Gordond5c1a372016-10-31 13:56:23 -0500532
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500533 // Iterate over each neighbor listed in nlsr.conf
534 for (auto& adjacent : m_adjacencyList.getAdjList()) {
535
536 const std::string faceUriString = adjacent.getFaceUri().toString();
Nick Gordond5c1a372016-10-31 13:56:23 -0500537 // Check the list of FaceStatus objects we got for a match
538 for (const ndn::nfd::FaceStatus& faceStatus : faces) {
Nick Gordond5c1a372016-10-31 13:56:23 -0500539 // Set the adjacency FaceID if we find a URI match and it was
540 // previously unset. Change the boolean to true.
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500541 if (adjacent.getFaceId() == 0 && faceUriString == faceStatus.getRemoteUri()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500542 NLSR_LOG_DEBUG("FaceUri: " << faceStatus.getRemoteUri() <<
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500543 " FaceId: "<< faceStatus.getFaceId());
544 adjacent.setFaceId(faceStatus.getFaceId());
Nick Gordond5c1a372016-10-31 13:56:23 -0500545 // Register the prefixes for each neighbor
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500546 this->registerAdjacencyPrefixes(adjacent, ndn::time::milliseconds::max());
Nick Gordond5c1a372016-10-31 13:56:23 -0500547 }
548 }
549 // If this adjacency has no information in this dataset, then one
550 // of two things is happening: 1. NFD is starting slowly and this
551 // Face wasn't ready yet, or 2. NFD is configured
552 // incorrectly and this Face isn't available.
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500553 if (adjacent.getFaceId() == 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500554 NLSR_LOG_WARN("The adjacency " << adjacent.getName() <<
Nick Gordond5c1a372016-10-31 13:56:23 -0500555 " has no Face information in this dataset.");
556 }
557 }
558
Nick Gordond5c1a372016-10-31 13:56:23 -0500559 scheduleDatasetFetch();
560}
561
562void
563Nlsr::registerAdjacencyPrefixes(const Adjacent& adj,
564 const ndn::time::milliseconds& timeout)
565{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500566 ndn::FaceUri faceUri = adj.getFaceUri();
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500567 double linkCost = adj.getLinkCost();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500568 const ndn::Name& adjName = adj.getName();
Nick Gordond5c1a372016-10-31 13:56:23 -0500569
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500570 m_fib.registerPrefix(adjName, faceUri, linkCost,
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500571 timeout, ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500572
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500573 m_fib.registerPrefix(m_confParam.getChronosyncPrefix(),
574 faceUri, linkCost, timeout,
575 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500576
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500577 m_fib.registerPrefix(m_confParam.getLsaPrefix(),
578 faceUri, linkCost, timeout,
579 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500580}
581
582void
583Nlsr::onFaceDatasetFetchTimeout(uint32_t code,
584 const std::string& msg,
585 uint32_t nRetriesSoFar)
586{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500587 NLSR_LOG_DEBUG("onFaceDatasetFetchTimeout");
Nick Gordond5c1a372016-10-31 13:56:23 -0500588 // If we have exceeded the maximum attempt count, do not try again.
589 if (nRetriesSoFar++ < m_confParam.getFaceDatasetFetchTries()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500590 NLSR_LOG_DEBUG("Failed to fetch dataset: " << msg << ". Attempting retry #" << nRetriesSoFar);
Nick Gordond5c1a372016-10-31 13:56:23 -0500591 m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(std::bind(&Nlsr::processFaceDataset,
592 this, _1),
593 std::bind(&Nlsr::onFaceDatasetFetchTimeout,
594 this, _1, _2, nRetriesSoFar));
595 }
596 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500597 NLSR_LOG_ERROR("Failed to fetch dataset: " << msg << ". Exceeded limit of " <<
Nick Gordond5c1a372016-10-31 13:56:23 -0500598 m_confParam.getFaceDatasetFetchTries() << ", so not trying again this time.");
599 // If we fail to fetch it, just do nothing until the next
600 // interval. Since this is a backup mechanism, we aren't as
601 // concerned with retrying.
602 scheduleDatasetFetch();
603 }
604}
605
606void
607Nlsr::scheduleDatasetFetch()
608{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500609 NLSR_LOG_DEBUG("Scheduling Dataset Fetch in " << m_confParam.getFaceDatasetFetchInterval());
610
Nick Gordond5c1a372016-10-31 13:56:23 -0500611 m_scheduler.scheduleEvent(m_confParam.getFaceDatasetFetchInterval(),
612 [this] {
613 this->initializeFaces(
614 [this] (const std::vector<ndn::nfd::FaceStatus>& faces) {
615 this->processFaceDataset(faces);
616 },
617 [this] (uint32_t code, const std::string& msg) {
618 this->onFaceDatasetFetchTimeout(code, msg, 0);
619 });
620 });
621}
akmhoquee1765152014-06-30 11:32:01 -0500622
623void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500624Nlsr::enableIncomingFaceIdIndication()
625{
626 NLSR_LOG_DEBUG("Enabling incoming face id indication for local face.");
627
628 m_controller.start<ndn::nfd::FaceUpdateCommand>(
629 ndn::nfd::ControlParameters()
630 .setFlagBit(ndn::nfd::FaceFlagBit::BIT_LOCAL_FIELDS_ENABLED, true),
631 bind(&Nlsr::onFaceIdIndicationSuccess, this, _1),
632 bind(&Nlsr::onFaceIdIndicationFailure, this, _1));
633}
634
635void
636Nlsr::onFaceIdIndicationSuccess(const ndn::nfd::ControlParameters& cp)
637{
638 NLSR_LOG_DEBUG("Successfully enabled incoming face id indication"
639 << "for face id " << cp.getFaceId());
640}
641
642void
643Nlsr::onFaceIdIndicationFailure(const ndn::nfd::ControlResponse& cr)
644{
645 std::ostringstream os;
646 os << "Failed to enable incoming face id indication feature: " <<
647 "(code: " << cr.getCode() << ", reason: " << cr.getText() << ")";
648
649 NLSR_LOG_DEBUG(os.str());
650}
651
652void
akmhoque53353462014-04-22 08:43:45 -0500653Nlsr::startEventLoop()
654{
akmhoquefdbddb12014-05-02 18:35:19 -0500655 m_nlsrFace.processEvents();
akmhoque53353462014-04-22 08:43:45 -0500656}
akmhoque5a44dd42014-03-12 18:11:32 -0500657
akmhoqueb1710aa2014-02-19 17:13:36 -0600658} // namespace nlsr