blob: 2823a744009ebf666f5b53ebde4b717c0860b8ef [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
laqinfan35731852017-08-08 06:17:39 -05003 * Copyright (c) 2014-2018, 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,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050071 m_nlsrLsdb)
laqinfan35731852017-08-08 06:17:39 -050072 , m_nfdRibCommandProcessor(m_dispatcher,
Nick Gordon4d2c6c02017-01-20 13:18:46 -060073 m_namePrefixList,
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050074 m_nlsrLsdb)
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060075 , m_statsCollector(m_nlsrLsdb, m_helloProtocol)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050076 , m_faceMonitor(m_nlsrFace)
77 , m_firstHelloInterval(FIRST_HELLO_INTERVAL_DEFAULT)
78{
dmcoomes9f936662017-03-02 10:33:09 -060079 m_faceMonitor.onNotification.connect(std::bind(&Nlsr::onFaceEventNotification, this, _1));
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050080 m_faceMonitor.start();
81}
82
akmhoque53353462014-04-22 08:43:45 -050083void
84Nlsr::registrationFailed(const ndn::Name& name)
akmhoque298385a2014-02-13 14:13:09 -060085{
dmcoomes5bcb39e2017-10-31 15:07:55 -050086 NLSR_LOG_ERROR("ERROR: Failed to register prefix in local hub's daemon");
dmcoomes9f936662017-03-02 10:33:09 -060087 BOOST_THROW_EXCEPTION(Error("Error: Prefix registration failed"));
akmhoque53353462014-04-22 08:43:45 -050088}
akmhoque1fd8c1e2014-02-19 19:41:49 -060089
akmhoque157b0a42014-05-13 00:26:37 -050090void
91Nlsr::onRegistrationSuccess(const ndn::Name& name)
92{
dmcoomes5bcb39e2017-10-31 15:07:55 -050093 NLSR_LOG_DEBUG("Successfully registered prefix: " << name);
alvy297f4162015-03-03 17:15:33 -060094}
95
96void
akmhoque31d1d4b2014-05-05 22:08:14 -050097Nlsr::setInfoInterestFilter()
akmhoque53353462014-04-22 08:43:45 -050098{
akmhoque31d1d4b2014-05-05 22:08:14 -050099 ndn::Name name(m_confParam.getRouterPrefix());
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500100 name.append("nlsr");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500101 name.append("INFO");
102
103 NLSR_LOG_DEBUG("Setting interest filter for Hello interest: " << name);
104
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500105 m_nlsrFace.setInterestFilter(name,
106 std::bind(&HelloProtocol::processInterest,
107 &m_helloProtocol, _1, _2),
108 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
109 std::bind(&Nlsr::registrationFailed, this, _1),
110 m_signingInfo,
111 ndn::nfd::ROUTE_FLAG_CAPTURE);
akmhoque31d1d4b2014-05-05 22:08:14 -0500112}
113
114void
115Nlsr::setLsaInterestFilter()
116{
akmhoque157b0a42014-05-13 00:26:37 -0500117 ndn::Name name = m_confParam.getLsaPrefix();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500118
dmcoomes5bcb39e2017-10-31 15:07:55 -0500119 NLSR_LOG_DEBUG("Setting interest filter for LsaPrefix: " << name);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500120
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500121 m_nlsrFace.setInterestFilter(name,
122 std::bind(&Lsdb::processInterest,
123 &m_nlsrLsdb, _1, _2),
124 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
125 std::bind(&Nlsr::registrationFailed, this, _1),
126 m_signingInfo,
127 ndn::nfd::ROUTE_FLAG_CAPTURE);
akmhoque53353462014-04-22 08:43:45 -0500128}
129
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500130
131void
132Nlsr::addDispatcherTopPrefix(const ndn::Name& topPrefix)
133{
134 try {
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500135 // false since we want to have control over the registration process
laqinfan35731852017-08-08 06:17:39 -0500136 m_dispatcher.addTopPrefix(topPrefix, false, m_signingInfo);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500137 }
138 catch (const std::exception& e) {
139 NLSR_LOG_ERROR("Error setting top-level prefix in dispatcher: " << e.what() << "\n");
140 }
141}
142
akmhoque53353462014-04-22 08:43:45 -0500143void
akmhoquec04e7272014-07-02 11:00:14 -0500144Nlsr::setStrategies()
akmhoque157b0a42014-05-13 00:26:37 -0500145{
Vince Lehman53c0e3e2015-09-14 14:33:20 -0500146 const std::string strategy("ndn:/localhost/nfd/strategy/multicast");
147
akmhoque393d4ff2014-07-16 14:27:03 -0500148 m_fib.setStrategy(m_confParam.getLsaPrefix(), strategy, 0);
akmhoque393d4ff2014-07-16 14:27:03 -0500149 m_fib.setStrategy(m_confParam.getChronosyncPrefix(), strategy, 0);
akmhoque157b0a42014-05-13 00:26:37 -0500150}
151
152void
Nick Gordon922714a2017-06-13 14:12:02 -0500153Nlsr::canonizeContinuation(std::list<Adjacent>::iterator iterator,
154 std::function<void(void)> finally)
Nick Gordon9461afb2017-04-25 15:54:50 -0500155{
Nick Gordon922714a2017-06-13 14:12:02 -0500156 canonizeNeighborUris(iterator, [this, finally] (std::list<Adjacent>::iterator iterator) {
157 canonizeContinuation(iterator, finally);
158 },
159 finally);
Nick Gordon9461afb2017-04-25 15:54:50 -0500160}
161
162void
163Nlsr::canonizeNeighborUris(std::list<Adjacent>::iterator currentNeighbor,
Nick Gordon922714a2017-06-13 14:12:02 -0500164 std::function<void(std::list<Adjacent>::iterator)> then,
165 std::function<void(void)> finally)
Nick Gordon9461afb2017-04-25 15:54:50 -0500166{
167 if (currentNeighbor != m_adjacencyList.getAdjList().end()) {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500168 ndn::FaceUri uri(currentNeighbor->getFaceUri());
Ashlesh Gawandee6ba9152018-03-30 01:15:00 -0500169 uri.canonize([then, currentNeighbor] (ndn::FaceUri canonicalUri) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500170 NLSR_LOG_DEBUG("Canonized URI: " << currentNeighbor->getFaceUri()
Nick Gordon9461afb2017-04-25 15:54:50 -0500171 << " to: " << canonicalUri);
Nick Gordone9733ed2017-04-26 10:48:39 -0500172 currentNeighbor->setFaceUri(canonicalUri);
Nick Gordon9461afb2017-04-25 15:54:50 -0500173 then(std::next(currentNeighbor));
174 },
Ashlesh Gawandee6ba9152018-03-30 01:15:00 -0500175 [then, currentNeighbor] (const std::string& reason) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500176 NLSR_LOG_ERROR("Could not canonize URI: " << currentNeighbor->getFaceUri()
Nick Gordon9461afb2017-04-25 15:54:50 -0500177 << " because: " << reason);
178 then(std::next(currentNeighbor));
179 },
180 m_nlsrFace.getIoService(),
181 TIME_ALLOWED_FOR_CANONIZATION);
182 }
Nick Gordon922714a2017-06-13 14:12:02 -0500183 // We have finished canonizing all neighbors, so call finally()
Nick Gordon9461afb2017-04-25 15:54:50 -0500184 else {
Nick Gordon922714a2017-06-13 14:12:02 -0500185 finally();
Nick Gordon9461afb2017-04-25 15:54:50 -0500186 }
187}
188
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500189void
190Nlsr::loadCertToPublish(const ndn::security::v2::Certificate& certificate)
191{
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000192 NLSR_LOG_TRACE("Loading cert to publish.");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500193 m_certStore.insert(certificate);
194 m_validator.loadAnchor("Authoritative-Certificate",
195 ndn::security::v2::Certificate(certificate));
196 m_prefixUpdateProcessor.getValidator().
197 loadAnchor("Authoritative-Certificate",
198 ndn::security::v2::Certificate(certificate));
199}
200
Nick Gordon9461afb2017-04-25 15:54:50 -0500201void
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000202Nlsr::connectToFetcher(ndn::util::SegmentFetcher& fetcher)
203{
204 NLSR_LOG_TRACE("NLSR: Connect to SegmentFetcher.");
205
206 fetcher.afterSegmentValidated.connect(std::bind(&Nlsr::afterFetcherSignalEmitted,
207 this, _1));
208}
209
210void
211Nlsr::afterFetcherSignalEmitted(const ndn::Data& lsaSegment)
212{
213 NLSR_LOG_TRACE("SegmentFetcher fetched a data segment. Start inserting cert to own cert store.");
214 ndn::Name keyName = lsaSegment.getSignature().getKeyLocator().getName();
215 if (getCertificate(keyName) == nullptr) {
216 publishCertFromCache(keyName);
217 }
218 else {
219 NLSR_LOG_TRACE("Certificate is already in the store: " << keyName);
220 }
221}
222
223void
224Nlsr::publishCertFromCache(const ndn::Name& keyName)
225{
226 const ndn::security::v2::Certificate* cert = m_validator.getUnverifiedCertCache()
227 .find(keyName);
228 if (cert != nullptr) {
229 m_certStore.insert(*cert);
230 NLSR_LOG_TRACE(*cert);
231 NLSR_LOG_TRACE("Setting interest filter for: "
232 << ndn::security::v2::extractKeyNameFromCertName(cert->getName()));
233 m_nlsrFace.setInterestFilter(ndn::security::v2::extractKeyNameFromCertName(cert->getName()),
234 std::bind(&Nlsr::onKeyInterest,
235 this, _1, _2),
236 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
237 std::bind(&Nlsr::registrationFailed, this, _1),
238 m_signingInfo,
239 ndn::nfd::ROUTE_FLAG_CAPTURE);
240
241 if (!cert->getKeyName().equals(cert->getSignature().getKeyLocator().getName())) {
242 publishCertFromCache(cert->getSignature().getKeyLocator().getName());
243 }
244 }
245 else {
246 NLSR_LOG_TRACE("Cert for " << keyName << " was not found in the Validator's cache. ");
247 }
248}
249
250void
akmhoque53353462014-04-22 08:43:45 -0500251Nlsr::initialize()
252{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500253 NLSR_LOG_DEBUG("Initializing Nlsr");
akmhoque53353462014-04-22 08:43:45 -0500254 m_confParam.buildRouterPrefix();
laqinfan35731852017-08-08 06:17:39 -0500255 m_datasetHandler.setRouterNameCommandPrefix(m_confParam.getRouterPrefix());
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700256 m_nlsrLsdb.setLsaRefreshTime(ndn::time::seconds(m_confParam.getLsaRefreshTime()));
akmhoque31d1d4b2014-05-05 22:08:14 -0500257 m_nlsrLsdb.setThisRouterPrefix(m_confParam.getRouterPrefix().toUri());
akmhoque53353462014-04-22 08:43:45 -0500258 m_fib.setEntryRefreshTime(2 * m_confParam.getLsaRefreshTime());
Vince Lehmanc11cc202015-01-20 11:41:33 -0600259
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500260 m_nlsrLsdb.getSequencingManager().setSeqFileDirectory(m_confParam.getSeqFileDir());
261 m_nlsrLsdb.getSequencingManager().initiateSeqNoFromFile(m_confParam.getHyperbolicState());
262
Ashlesh Gawande48101072018-05-30 17:53:06 -0500263 m_nlsrLsdb.getSyncLogicHandler().createSyncLogic(m_confParam.getChronosyncPrefix(),
264 m_confParam.getSyncInterestLifetime());
Vince Lehmanc11cc202015-01-20 11:41:33 -0600265
dmcoomes9f936662017-03-02 10:33:09 -0600266 // Logging start
akmhoque674b0b12014-05-20 14:33:28 -0500267 m_confParam.writeLog();
268 m_adjacencyList.writeLog();
dmcoomes5bcb39e2017-10-31 15:07:55 -0500269 NLSR_LOG_DEBUG(m_namePrefixList);
dmcoomes9f936662017-03-02 10:33:09 -0600270 // Logging end
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500271
akmhoque443ad812014-07-29 10:26:56 -0500272 initializeKey();
akmhoquec04e7272014-07-02 11:00:14 -0500273 setStrategies();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500274
dmcoomes5bcb39e2017-10-31 15:07:55 -0500275 NLSR_LOG_DEBUG("Default NLSR identity: " << m_signingInfo.getSignerName());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500276
akmhoque31d1d4b2014-05-05 22:08:14 -0500277 setInfoInterestFilter();
278 setLsaInterestFilter();
Vince Lehman50df6b72015-03-03 12:06:40 -0600279
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500280 // add top-level prefixes: router and localhost prefix
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500281 addDispatcherTopPrefix(ndn::Name(m_confParam.getRouterPrefix()).append("nlsr"));
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500282 addDispatcherTopPrefix(LOCALHOST_PREFIX);
283
Nick Gordond5c1a372016-10-31 13:56:23 -0500284 initializeFaces(std::bind(&Nlsr::processFaceDataset, this, _1),
285 std::bind(&Nlsr::onFaceDatasetFetchTimeout, this, _1, _2, 0));
286
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500287 enableIncomingFaceIdIndication();
288
Vince Lehman50df6b72015-03-03 12:06:40 -0600289 // Set event intervals
290 setFirstHelloInterval(m_confParam.getFirstHelloInterval());
291 m_nlsrLsdb.setAdjLsaBuildInterval(m_confParam.getAdjLsaBuildInterval());
292 m_routingTable.setRoutingCalcInterval(m_confParam.getRoutingCalcInterval());
293
akmhoque674b0b12014-05-20 14:33:28 -0500294 m_nlsrLsdb.buildAndInstallOwnNameLsa();
Nick Gordon5c467f02016-07-13 13:40:10 -0500295
296 // Install coordinate LSAs if using HR or dry-run HR.
297 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
298 m_nlsrLsdb.buildAndInstallOwnCoordinateLsa();
299 }
Vince Lehman904c2412014-09-23 19:36:11 -0500300
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700301 registerKeyPrefix();
alvy297f4162015-03-03 17:15:33 -0600302 registerLocalhostPrefix();
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500303 registerRouterPrefix();
Vince Lehman7b616582014-10-17 16:25:39 -0500304
Vince Lehman7b616582014-10-17 16:25:39 -0500305 m_helloProtocol.scheduleInterest(m_firstHelloInterval);
Vince Lehman09131122014-09-09 17:10:11 -0500306
307 // Need to set direct neighbors' costs to 0 for hyperbolic routing
308 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
309
310 std::list<Adjacent>& neighbors = m_adjacencyList.getAdjList();
311
312 for (std::list<Adjacent>::iterator it = neighbors.begin(); it != neighbors.end(); ++it) {
313 it->setLinkCost(0);
314 }
315 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700316}
317
318void
akmhoque443ad812014-07-29 10:26:56 -0500319Nlsr::initializeKey()
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700320{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500321 NLSR_LOG_DEBUG("Initializing Key ...");
322
323 ndn::Name nlsrInstanceName = m_confParam.getRouterPrefix();
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500324 nlsrInstanceName.append("nlsr");
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700325
Joao Pereira97473d42015-07-03 16:57:27 -0400326 try {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500327 m_keyChain.deleteIdentity(m_keyChain.getPib().getIdentity(nlsrInstanceName));
328 } catch (const std::exception& e) {
329 NLSR_LOG_WARN(e.what());
330 }
331
332 auto nlsrInstanceIdentity = m_keyChain.createIdentity(nlsrInstanceName);
333 auto nlsrInstanceKey = nlsrInstanceIdentity.getDefaultKey();
334
335 ndn::security::v2::Certificate certificate;
336
337 ndn::Name certificateName = nlsrInstanceKey.getName();
338 certificateName.append("NA");
339 certificateName.appendVersion();
340 certificate.setName(certificateName);
341
342 // set metainfo
343 certificate.setContentType(ndn::tlv::ContentType_Key);
Ashlesh Gawande0d2c3822018-01-24 17:17:15 -0600344 certificate.setFreshnessPeriod(ndn::time::days(365));
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500345
346 // set content
347 certificate.setContent(nlsrInstanceKey.getPublicKey().data(), nlsrInstanceKey.getPublicKey().size());
348
349 // set signature-info
350 ndn::SignatureInfo signatureInfo;
351 signatureInfo.setValidityPeriod(ndn::security::ValidityPeriod(ndn::time::system_clock::TimePoint(),
352 ndn::time::system_clock::now()
Ashlesh Gawande0d2c3822018-01-24 17:17:15 -0600353 + ndn::time::days(365)));
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500354 try {
355 m_keyChain.sign(certificate,
356 ndn::security::SigningInfo(m_keyChain.getPib().getIdentity(m_confParam.getRouterPrefix()))
357 .setSignatureInfo(signatureInfo));
akmhoque102aea42014-08-04 10:22:12 -0500358 }
dmcoomes9f936662017-03-02 10:33:09 -0600359 catch (const std::exception& e) {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500360 NLSR_LOG_WARN("ERROR: Router's " << e.what()
361 << "NLSR is running without security."
362 << " If security is enabled NLSR will not converge.");
363
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000364 std::cerr << "Router's " << e.what() << ". NLSR is running without security "
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500365 << "(Only for testing, should not be used in production.)"
366 << " If security is enabled NLSR will not converge." << std::endl;
akmhoque102aea42014-08-04 10:22:12 -0500367 }
akmhoque443ad812014-07-29 10:26:56 -0500368
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500369 m_signingInfo = ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_ID,
370 nlsrInstanceName);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700371
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700372 loadCertToPublish(certificate);
373
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500374 m_defaultCertName = certificate.getName();
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700375}
376
377void
378Nlsr::registerKeyPrefix()
379{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500380 // Start listening for the interest of this router's NLSR certificate
381 ndn::Name nlsrKeyPrefix = getConfParameter().getRouterPrefix();
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500382 nlsrKeyPrefix.append("nlsr");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500383 nlsrKeyPrefix.append("KEY");
384
385 m_nlsrFace.setInterestFilter(nlsrKeyPrefix,
Joao Pereira97473d42015-07-03 16:57:27 -0400386 std::bind(&Nlsr::onKeyInterest,
Yingdi Yu6a3a4dd2014-06-20 14:10:39 -0700387 this, _1, _2),
Joao Pereira97473d42015-07-03 16:57:27 -0400388 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
389 std::bind(&Nlsr::registrationFailed, this, _1),
390 m_signingInfo,
akmhoque060d3022014-08-12 13:35:06 -0500391 ndn::nfd::ROUTE_FLAG_CAPTURE);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700392
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500393 // Start listening for the interest of this router's certificate
394 ndn::Name routerKeyPrefix = getConfParameter().getRouterPrefix();
395 routerKeyPrefix.append("KEY");
396
397 m_nlsrFace.setInterestFilter(routerKeyPrefix,
398 std::bind(&Nlsr::onKeyInterest,
399 this, _1, _2),
400 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
401 std::bind(&Nlsr::registrationFailed, this, _1),
402 m_signingInfo,
403 ndn::nfd::ROUTE_FLAG_CAPTURE);
404
405 // Start listening for the interest of this router's operator's certificate
406 ndn::Name operatorKeyPrefix = getConfParameter().getNetwork();
407 operatorKeyPrefix.append(getConfParameter().getSiteName());
408 operatorKeyPrefix.append(std::string("%C1.Operator"));
409
410 m_nlsrFace.setInterestFilter(operatorKeyPrefix,
411 std::bind(&Nlsr::onKeyInterest,
412 this, _1, _2),
413 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
414 std::bind(&Nlsr::registrationFailed, this, _1),
415 m_signingInfo,
416 ndn::nfd::ROUTE_FLAG_CAPTURE);
417
418 // Start listening for the interest of this router's site's certificate
419 ndn::Name siteKeyPrefix = getConfParameter().getNetwork();
420 siteKeyPrefix.append(getConfParameter().getSiteName());
421 siteKeyPrefix.append("KEY");
422
423 m_nlsrFace.setInterestFilter(siteKeyPrefix,
424 std::bind(&Nlsr::onKeyInterest,
425 this, _1, _2),
426 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
427 std::bind(&Nlsr::registrationFailed, this, _1),
428 m_signingInfo,
429 ndn::nfd::ROUTE_FLAG_CAPTURE);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700430}
431
432void
alvy297f4162015-03-03 17:15:33 -0600433Nlsr::registerLocalhostPrefix()
434{
alvy297f4162015-03-03 17:15:33 -0600435 m_nlsrFace.registerPrefix(LOCALHOST_PREFIX,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500436 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
alvy297f4162015-03-03 17:15:33 -0600437 std::bind(&Nlsr::registrationFailed, this, _1));
438}
439
440void
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500441Nlsr::registerRouterPrefix()
442{
443 m_nlsrFace.registerPrefix(ndn::Name(m_confParam.getRouterPrefix()).append("nlsr"),
444 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
445 std::bind(&Nlsr::registrationFailed, this, _1));
446}
447
448void
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700449Nlsr::onKeyInterest(const ndn::Name& name, const ndn::Interest& interest)
450{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500451 NLSR_LOG_DEBUG("Got interest for certificate. Interest: " << interest.getName());
452
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700453 const ndn::Name& interestName = interest.getName();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500454 const ndn::security::v2::Certificate* cert = getCertificate(interestName);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700455
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500456 if (cert == nullptr) {
457 NLSR_LOG_DEBUG("Certificate is not found for: " << interest);
dmcoomes9eaf3f42017-02-21 11:39:01 -0600458 return; // cert is not found
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500459 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700460
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500461 m_nlsrFace.put(*cert);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700462}
463
464void
465Nlsr::onKeyPrefixRegSuccess(const ndn::Name& name)
466{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500467 NLSR_LOG_DEBUG("KEY prefix: " << name << " registration is successful.");
akmhoque53353462014-04-22 08:43:45 -0500468}
akmhoque5a44dd42014-03-12 18:11:32 -0500469
akmhoque53353462014-04-22 08:43:45 -0500470void
akmhoquec04e7272014-07-02 11:00:14 -0500471Nlsr::onFaceEventNotification(const ndn::nfd::FaceEventNotification& faceEventNotification)
akmhoquee1765152014-06-30 11:32:01 -0500472{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500473 NLSR_LOG_TRACE("Nlsr::onFaceEventNotification called");
Vince Lehman02e32992015-03-11 12:31:20 -0500474
Nick Gordond5c1a372016-10-31 13:56:23 -0500475 switch (faceEventNotification.getKind()) {
476 case ndn::nfd::FACE_EVENT_DESTROYED: {
477 uint64_t faceId = faceEventNotification.getFaceId();
Vince Lehman02e32992015-03-11 12:31:20 -0500478
Nick Gordond5c1a372016-10-31 13:56:23 -0500479 auto adjacent = m_adjacencyList.findAdjacent(faceId);
Vince Lehman02e32992015-03-11 12:31:20 -0500480
Nick Gordond5c1a372016-10-31 13:56:23 -0500481 if (adjacent != m_adjacencyList.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500482 NLSR_LOG_DEBUG("Face to " << adjacent->getName() << " with face id: " << faceId << " destroyed");
Vince Lehman02e32992015-03-11 12:31:20 -0500483
Nick Gordond5c1a372016-10-31 13:56:23 -0500484 adjacent->setFaceId(0);
Vince Lehman02e32992015-03-11 12:31:20 -0500485
Nick Gordond5c1a372016-10-31 13:56:23 -0500486 // Only trigger an Adjacency LSA build if this node is changing
487 // from ACTIVE to INACTIVE since this rebuild will effectively
488 // cancel the previous Adjacency LSA refresh event and schedule
489 // a new one further in the future.
490 //
491 // Continuously scheduling the refresh in the future will block
492 // the router from refreshing its Adjacency LSA. Since other
493 // routers' Name prefixes' expiration times are updated when
494 // this router refreshes its Adjacency LSA, the other routers'
495 // prefixes will expire and be removed from the RIB.
496 //
497 // This check is required to fix Bug #2733 for now. This check
498 // would be unnecessary to fix Bug #2733 when Issue #2732 is
499 // completed, but the check also helps with optimization so it
500 // can remain even when Issue #2732 is implemented.
501 if (adjacent->getStatus() == Adjacent::STATUS_ACTIVE) {
502 adjacent->setStatus(Adjacent::STATUS_INACTIVE);
Vince Lehman02e32992015-03-11 12:31:20 -0500503
Nick Gordond5c1a372016-10-31 13:56:23 -0500504 // A new adjacency LSA cannot be built until the neighbor is marked INACTIVE and
505 // has met the HELLO retry threshold
506 adjacent->setInterestTimedOutNo(m_confParam.getInterestRetryNumber());
Vince Lehman199e9cf2015-04-07 13:22:16 -0500507
Nick Gordond5c1a372016-10-31 13:56:23 -0500508 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
509 getRoutingTable().scheduleRoutingTableCalculation(*this);
510 }
511 else {
512 m_nlsrLsdb.scheduleAdjLsaBuild();
513 }
Nick Gordone8e03ac2016-07-07 14:24:38 -0500514 }
Vince Lehman199e9cf2015-04-07 13:22:16 -0500515 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500516 break;
akmhoquec04e7272014-07-02 11:00:14 -0500517 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500518 case ndn::nfd::FACE_EVENT_CREATED: {
519 // Find the neighbor in our adjacency list
Ashlesh Gawande0d2c3822018-01-24 17:17:15 -0600520 ndn::FaceUri faceUri;
521 try {
522 faceUri = ndn::FaceUri(faceEventNotification.getRemoteUri());
523 }
524 catch (const std::exception& e) {
525 NLSR_LOG_WARN(e.what());
526 return;
527 }
528 auto adjacent = m_adjacencyList.findAdjacent(faceUri);
529
Nick Gordond5c1a372016-10-31 13:56:23 -0500530 // If we have a neighbor by that FaceUri and it has no FaceId, we
531 // have a match.
532 if (adjacent != m_adjacencyList.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500533 NLSR_LOG_DEBUG("Face creation event matches neighbor: " << adjacent->getName()
Nick Gordond5c1a372016-10-31 13:56:23 -0500534 << ". New Face ID: " << faceEventNotification.getFaceId()
535 << ". Registering prefixes.");
536 adjacent->setFaceId(faceEventNotification.getFaceId());
537
538 registerAdjacencyPrefixes(*adjacent, ndn::time::milliseconds::max());
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500539
540 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
541 getRoutingTable().scheduleRoutingTableCalculation(*this);
542 }
543 else {
544 m_nlsrLsdb.scheduleAdjLsaBuild();
545 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500546 }
547 break;
548 }
549 default:
550 break;
akmhoquec04e7272014-07-02 11:00:14 -0500551 }
akmhoquee1765152014-06-30 11:32:01 -0500552}
553
Nick Gordond5c1a372016-10-31 13:56:23 -0500554void
555Nlsr::initializeFaces(const FetchDatasetCallback& onFetchSuccess,
556 const FetchDatasetTimeoutCallback& onFetchFailure)
557{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500558 NLSR_LOG_TRACE("Initializing Faces...");
Nick Gordond5c1a372016-10-31 13:56:23 -0500559
560 m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(onFetchSuccess, onFetchFailure);
561
562}
563
564void
565Nlsr::processFaceDataset(const std::vector<ndn::nfd::FaceStatus>& faces)
566{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500567 NLSR_LOG_DEBUG("Processing face dataset");
Nick Gordond5c1a372016-10-31 13:56:23 -0500568
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500569 // Iterate over each neighbor listed in nlsr.conf
570 for (auto& adjacent : m_adjacencyList.getAdjList()) {
571
572 const std::string faceUriString = adjacent.getFaceUri().toString();
Nick Gordond5c1a372016-10-31 13:56:23 -0500573 // Check the list of FaceStatus objects we got for a match
574 for (const ndn::nfd::FaceStatus& faceStatus : faces) {
Nick Gordond5c1a372016-10-31 13:56:23 -0500575 // Set the adjacency FaceID if we find a URI match and it was
576 // previously unset. Change the boolean to true.
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500577 if (adjacent.getFaceId() == 0 && faceUriString == faceStatus.getRemoteUri()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500578 NLSR_LOG_DEBUG("FaceUri: " << faceStatus.getRemoteUri() <<
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500579 " FaceId: "<< faceStatus.getFaceId());
580 adjacent.setFaceId(faceStatus.getFaceId());
Nick Gordond5c1a372016-10-31 13:56:23 -0500581 // Register the prefixes for each neighbor
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500582 this->registerAdjacencyPrefixes(adjacent, ndn::time::milliseconds::max());
Nick Gordond5c1a372016-10-31 13:56:23 -0500583 }
584 }
585 // If this adjacency has no information in this dataset, then one
586 // of two things is happening: 1. NFD is starting slowly and this
587 // Face wasn't ready yet, or 2. NFD is configured
588 // incorrectly and this Face isn't available.
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500589 if (adjacent.getFaceId() == 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500590 NLSR_LOG_WARN("The adjacency " << adjacent.getName() <<
Nick Gordond5c1a372016-10-31 13:56:23 -0500591 " has no Face information in this dataset.");
592 }
593 }
594
Nick Gordond5c1a372016-10-31 13:56:23 -0500595 scheduleDatasetFetch();
596}
597
598void
599Nlsr::registerAdjacencyPrefixes(const Adjacent& adj,
600 const ndn::time::milliseconds& timeout)
601{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500602 ndn::FaceUri faceUri = adj.getFaceUri();
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500603 double linkCost = adj.getLinkCost();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500604 const ndn::Name& adjName = adj.getName();
Nick Gordond5c1a372016-10-31 13:56:23 -0500605
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500606 m_fib.registerPrefix(adjName, faceUri, linkCost,
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500607 timeout, ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500608
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500609 m_fib.registerPrefix(m_confParam.getChronosyncPrefix(),
610 faceUri, linkCost, timeout,
611 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500612
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500613 m_fib.registerPrefix(m_confParam.getLsaPrefix(),
614 faceUri, linkCost, timeout,
615 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500616}
617
618void
619Nlsr::onFaceDatasetFetchTimeout(uint32_t code,
620 const std::string& msg,
621 uint32_t nRetriesSoFar)
622{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500623 NLSR_LOG_DEBUG("onFaceDatasetFetchTimeout");
Nick Gordond5c1a372016-10-31 13:56:23 -0500624 // If we have exceeded the maximum attempt count, do not try again.
625 if (nRetriesSoFar++ < m_confParam.getFaceDatasetFetchTries()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500626 NLSR_LOG_DEBUG("Failed to fetch dataset: " << msg << ". Attempting retry #" << nRetriesSoFar);
Nick Gordond5c1a372016-10-31 13:56:23 -0500627 m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(std::bind(&Nlsr::processFaceDataset,
628 this, _1),
629 std::bind(&Nlsr::onFaceDatasetFetchTimeout,
630 this, _1, _2, nRetriesSoFar));
631 }
632 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500633 NLSR_LOG_ERROR("Failed to fetch dataset: " << msg << ". Exceeded limit of " <<
Nick Gordond5c1a372016-10-31 13:56:23 -0500634 m_confParam.getFaceDatasetFetchTries() << ", so not trying again this time.");
635 // If we fail to fetch it, just do nothing until the next
636 // interval. Since this is a backup mechanism, we aren't as
637 // concerned with retrying.
638 scheduleDatasetFetch();
639 }
640}
641
642void
643Nlsr::scheduleDatasetFetch()
644{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500645 NLSR_LOG_DEBUG("Scheduling Dataset Fetch in " << m_confParam.getFaceDatasetFetchInterval());
646
Nick Gordond5c1a372016-10-31 13:56:23 -0500647 m_scheduler.scheduleEvent(m_confParam.getFaceDatasetFetchInterval(),
648 [this] {
649 this->initializeFaces(
650 [this] (const std::vector<ndn::nfd::FaceStatus>& faces) {
651 this->processFaceDataset(faces);
652 },
653 [this] (uint32_t code, const std::string& msg) {
654 this->onFaceDatasetFetchTimeout(code, msg, 0);
655 });
656 });
657}
akmhoquee1765152014-06-30 11:32:01 -0500658
659void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500660Nlsr::enableIncomingFaceIdIndication()
661{
662 NLSR_LOG_DEBUG("Enabling incoming face id indication for local face.");
663
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));
669}
670
671void
672Nlsr::onFaceIdIndicationSuccess(const ndn::nfd::ControlParameters& cp)
673{
674 NLSR_LOG_DEBUG("Successfully enabled incoming face id indication"
675 << "for face id " << cp.getFaceId());
676}
677
678void
679Nlsr::onFaceIdIndicationFailure(const ndn::nfd::ControlResponse& cr)
680{
681 std::ostringstream os;
682 os << "Failed to enable incoming face id indication feature: " <<
683 "(code: " << cr.getCode() << ", reason: " << cr.getText() << ")";
684
685 NLSR_LOG_DEBUG(os.str());
686}
687
688void
akmhoque53353462014-04-22 08:43:45 -0500689Nlsr::startEventLoop()
690{
akmhoquefdbddb12014-05-02 18:35:19 -0500691 m_nlsrFace.processEvents();
akmhoque53353462014-04-22 08:43:45 -0500692}
akmhoque5a44dd42014-03-12 18:11:32 -0500693
akmhoqueb1710aa2014-02-19 17:13:36 -0600694} // namespace nlsr