blob: 71970ecab88660ab63856e099efe46b9802568bb [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)
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050065 , m_validator(ndn::make_unique<ndn::security::v2::CertificateFetcherDirectFetch>(m_nlsrFace))
66 , 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());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500100 name.append("NLSR");
101 name.append("INFO");
102
103 NLSR_LOG_DEBUG("Setting interest filter for Hello interest: " << name);
104
akmhoquefdbddb12014-05-02 18:35:19 -0500105 getNlsrFace().setInterestFilter(name,
Joao Pereira97473d42015-07-03 16:57:27 -0400106 std::bind(&HelloProtocol::processInterest,
akmhoque31d1d4b2014-05-05 22:08:14 -0500107 &m_helloProtocol, _1, _2),
Joao Pereira97473d42015-07-03 16:57:27 -0400108 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
109 std::bind(&Nlsr::registrationFailed, this, _1),
110 m_signingInfo,
akmhoque060d3022014-08-12 13:35:06 -0500111 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
akmhoque31d1d4b2014-05-05 22:08:14 -0500121 getNlsrFace().setInterestFilter(name,
Joao Pereira97473d42015-07-03 16:57:27 -0400122 std::bind(&Lsdb::processInterest,
akmhoque31d1d4b2014-05-05 22:08:14 -0500123 &m_nlsrLsdb, _1, _2),
Joao Pereira97473d42015-07-03 16:57:27 -0400124 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
125 std::bind(&Nlsr::registrationFailed, this, _1),
126 m_signingInfo,
akmhoque060d3022014-08-12 13:35:06 -0500127 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 {
laqinfan35731852017-08-08 06:17:39 -0500135 m_dispatcher.addTopPrefix(topPrefix, false, m_signingInfo);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500136 }
137 catch (const std::exception& e) {
138 NLSR_LOG_ERROR("Error setting top-level prefix in dispatcher: " << e.what() << "\n");
139 }
140}
141
akmhoque53353462014-04-22 08:43:45 -0500142void
akmhoquec04e7272014-07-02 11:00:14 -0500143Nlsr::setStrategies()
akmhoque157b0a42014-05-13 00:26:37 -0500144{
Vince Lehman53c0e3e2015-09-14 14:33:20 -0500145 const std::string strategy("ndn:/localhost/nfd/strategy/multicast");
146
akmhoque393d4ff2014-07-16 14:27:03 -0500147 m_fib.setStrategy(m_confParam.getLsaPrefix(), strategy, 0);
akmhoque393d4ff2014-07-16 14:27:03 -0500148 m_fib.setStrategy(m_confParam.getChronosyncPrefix(), strategy, 0);
akmhoque157b0a42014-05-13 00:26:37 -0500149}
150
151void
Nick Gordon922714a2017-06-13 14:12:02 -0500152Nlsr::canonizeContinuation(std::list<Adjacent>::iterator iterator,
153 std::function<void(void)> finally)
Nick Gordon9461afb2017-04-25 15:54:50 -0500154{
Nick Gordon922714a2017-06-13 14:12:02 -0500155 canonizeNeighborUris(iterator, [this, finally] (std::list<Adjacent>::iterator iterator) {
156 canonizeContinuation(iterator, finally);
157 },
158 finally);
Nick Gordon9461afb2017-04-25 15:54:50 -0500159}
160
161void
162Nlsr::canonizeNeighborUris(std::list<Adjacent>::iterator currentNeighbor,
Nick Gordon922714a2017-06-13 14:12:02 -0500163 std::function<void(std::list<Adjacent>::iterator)> then,
164 std::function<void(void)> finally)
Nick Gordon9461afb2017-04-25 15:54:50 -0500165{
166 if (currentNeighbor != m_adjacencyList.getAdjList().end()) {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500167 ndn::FaceUri uri(currentNeighbor->getFaceUri());
Ashlesh Gawandee6ba9152018-03-30 01:15:00 -0500168 uri.canonize([then, currentNeighbor] (ndn::FaceUri canonicalUri) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500169 NLSR_LOG_DEBUG("Canonized URI: " << currentNeighbor->getFaceUri()
Nick Gordon9461afb2017-04-25 15:54:50 -0500170 << " to: " << canonicalUri);
Nick Gordone9733ed2017-04-26 10:48:39 -0500171 currentNeighbor->setFaceUri(canonicalUri);
Nick Gordon9461afb2017-04-25 15:54:50 -0500172 then(std::next(currentNeighbor));
173 },
Ashlesh Gawandee6ba9152018-03-30 01:15:00 -0500174 [then, currentNeighbor] (const std::string& reason) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500175 NLSR_LOG_ERROR("Could not canonize URI: " << currentNeighbor->getFaceUri()
Nick Gordon9461afb2017-04-25 15:54:50 -0500176 << " because: " << reason);
177 then(std::next(currentNeighbor));
178 },
179 m_nlsrFace.getIoService(),
180 TIME_ALLOWED_FOR_CANONIZATION);
181 }
Nick Gordon922714a2017-06-13 14:12:02 -0500182 // We have finished canonizing all neighbors, so call finally()
Nick Gordon9461afb2017-04-25 15:54:50 -0500183 else {
Nick Gordon922714a2017-06-13 14:12:02 -0500184 finally();
Nick Gordon9461afb2017-04-25 15:54:50 -0500185 }
186}
187
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500188void
189Nlsr::loadCertToPublish(const ndn::security::v2::Certificate& certificate)
190{
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000191 NLSR_LOG_TRACE("Loading cert to publish.");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500192 m_certStore.insert(certificate);
193 m_validator.loadAnchor("Authoritative-Certificate",
194 ndn::security::v2::Certificate(certificate));
195 m_prefixUpdateProcessor.getValidator().
196 loadAnchor("Authoritative-Certificate",
197 ndn::security::v2::Certificate(certificate));
198}
199
Nick Gordon9461afb2017-04-25 15:54:50 -0500200void
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000201Nlsr::connectToFetcher(ndn::util::SegmentFetcher& fetcher)
202{
203 NLSR_LOG_TRACE("NLSR: Connect to SegmentFetcher.");
204
205 fetcher.afterSegmentValidated.connect(std::bind(&Nlsr::afterFetcherSignalEmitted,
206 this, _1));
207}
208
209void
210Nlsr::afterFetcherSignalEmitted(const ndn::Data& lsaSegment)
211{
212 NLSR_LOG_TRACE("SegmentFetcher fetched a data segment. Start inserting cert to own cert store.");
213 ndn::Name keyName = lsaSegment.getSignature().getKeyLocator().getName();
214 if (getCertificate(keyName) == nullptr) {
215 publishCertFromCache(keyName);
216 }
217 else {
218 NLSR_LOG_TRACE("Certificate is already in the store: " << keyName);
219 }
220}
221
222void
223Nlsr::publishCertFromCache(const ndn::Name& keyName)
224{
225 const ndn::security::v2::Certificate* cert = m_validator.getUnverifiedCertCache()
226 .find(keyName);
227 if (cert != nullptr) {
228 m_certStore.insert(*cert);
229 NLSR_LOG_TRACE(*cert);
230 NLSR_LOG_TRACE("Setting interest filter for: "
231 << ndn::security::v2::extractKeyNameFromCertName(cert->getName()));
232 m_nlsrFace.setInterestFilter(ndn::security::v2::extractKeyNameFromCertName(cert->getName()),
233 std::bind(&Nlsr::onKeyInterest,
234 this, _1, _2),
235 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
236 std::bind(&Nlsr::registrationFailed, this, _1),
237 m_signingInfo,
238 ndn::nfd::ROUTE_FLAG_CAPTURE);
239
240 if (!cert->getKeyName().equals(cert->getSignature().getKeyLocator().getName())) {
241 publishCertFromCache(cert->getSignature().getKeyLocator().getName());
242 }
243 }
244 else {
245 NLSR_LOG_TRACE("Cert for " << keyName << " was not found in the Validator's cache. ");
246 }
247}
248
249void
akmhoque53353462014-04-22 08:43:45 -0500250Nlsr::initialize()
251{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500252 NLSR_LOG_DEBUG("Initializing Nlsr");
akmhoque53353462014-04-22 08:43:45 -0500253 m_confParam.buildRouterPrefix();
laqinfan35731852017-08-08 06:17:39 -0500254 m_datasetHandler.setRouterNameCommandPrefix(m_confParam.getRouterPrefix());
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700255 m_nlsrLsdb.setLsaRefreshTime(ndn::time::seconds(m_confParam.getLsaRefreshTime()));
akmhoque31d1d4b2014-05-05 22:08:14 -0500256 m_nlsrLsdb.setThisRouterPrefix(m_confParam.getRouterPrefix().toUri());
akmhoque53353462014-04-22 08:43:45 -0500257 m_fib.setEntryRefreshTime(2 * m_confParam.getLsaRefreshTime());
Vince Lehmanc11cc202015-01-20 11:41:33 -0600258
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500259 m_nlsrLsdb.getSequencingManager().setSeqFileDirectory(m_confParam.getSeqFileDir());
260 m_nlsrLsdb.getSequencingManager().initiateSeqNoFromFile(m_confParam.getHyperbolicState());
261
Ashlesh Gawandef7da9c52018-02-06 17:36:46 -0600262 m_nlsrLsdb.getSyncLogicHandler().createSyncSocket(m_confParam.getChronosyncPrefix(),
263 m_confParam.getSyncInterestLifetime());
Vince Lehmanc11cc202015-01-20 11:41:33 -0600264
dmcoomes9f936662017-03-02 10:33:09 -0600265 // Logging start
akmhoque674b0b12014-05-20 14:33:28 -0500266 m_confParam.writeLog();
267 m_adjacencyList.writeLog();
dmcoomes5bcb39e2017-10-31 15:07:55 -0500268 NLSR_LOG_DEBUG(m_namePrefixList);
dmcoomes9f936662017-03-02 10:33:09 -0600269 // Logging end
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500270
akmhoque443ad812014-07-29 10:26:56 -0500271 initializeKey();
akmhoquec04e7272014-07-02 11:00:14 -0500272 setStrategies();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500273
dmcoomes5bcb39e2017-10-31 15:07:55 -0500274 NLSR_LOG_DEBUG("Default NLSR identity: " << m_signingInfo.getSignerName());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500275
akmhoque31d1d4b2014-05-05 22:08:14 -0500276 setInfoInterestFilter();
277 setLsaInterestFilter();
Vince Lehman50df6b72015-03-03 12:06:40 -0600278
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500279 // add top-level prefixes: router and localhost prefix
280 addDispatcherTopPrefix(m_confParam.getRouterPrefix());
281 addDispatcherTopPrefix(LOCALHOST_PREFIX);
282
Nick Gordond5c1a372016-10-31 13:56:23 -0500283 initializeFaces(std::bind(&Nlsr::processFaceDataset, this, _1),
284 std::bind(&Nlsr::onFaceDatasetFetchTimeout, this, _1, _2, 0));
285
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500286 enableIncomingFaceIdIndication();
287
Vince Lehman50df6b72015-03-03 12:06:40 -0600288 // Set event intervals
289 setFirstHelloInterval(m_confParam.getFirstHelloInterval());
290 m_nlsrLsdb.setAdjLsaBuildInterval(m_confParam.getAdjLsaBuildInterval());
291 m_routingTable.setRoutingCalcInterval(m_confParam.getRoutingCalcInterval());
292
akmhoque674b0b12014-05-20 14:33:28 -0500293 m_nlsrLsdb.buildAndInstallOwnNameLsa();
Nick Gordon5c467f02016-07-13 13:40:10 -0500294
295 // Install coordinate LSAs if using HR or dry-run HR.
296 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
297 m_nlsrLsdb.buildAndInstallOwnCoordinateLsa();
298 }
Vince Lehman904c2412014-09-23 19:36:11 -0500299
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700300 registerKeyPrefix();
alvy297f4162015-03-03 17:15:33 -0600301 registerLocalhostPrefix();
Vince Lehman7b616582014-10-17 16:25:39 -0500302
Vince Lehman7b616582014-10-17 16:25:39 -0500303 m_helloProtocol.scheduleInterest(m_firstHelloInterval);
Vince Lehman09131122014-09-09 17:10:11 -0500304
305 // Need to set direct neighbors' costs to 0 for hyperbolic routing
306 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
307
308 std::list<Adjacent>& neighbors = m_adjacencyList.getAdjList();
309
310 for (std::list<Adjacent>::iterator it = neighbors.begin(); it != neighbors.end(); ++it) {
311 it->setLinkCost(0);
312 }
313 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700314}
315
316void
akmhoque443ad812014-07-29 10:26:56 -0500317Nlsr::initializeKey()
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700318{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500319 NLSR_LOG_DEBUG("Initializing Key ...");
320
321 ndn::Name nlsrInstanceName = m_confParam.getRouterPrefix();
322 nlsrInstanceName.append("NLSR");
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700323
Joao Pereira97473d42015-07-03 16:57:27 -0400324 try {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500325 m_keyChain.deleteIdentity(m_keyChain.getPib().getIdentity(nlsrInstanceName));
326 } catch (const std::exception& e) {
327 NLSR_LOG_WARN(e.what());
328 }
329
330 auto nlsrInstanceIdentity = m_keyChain.createIdentity(nlsrInstanceName);
331 auto nlsrInstanceKey = nlsrInstanceIdentity.getDefaultKey();
332
333 ndn::security::v2::Certificate certificate;
334
335 ndn::Name certificateName = nlsrInstanceKey.getName();
336 certificateName.append("NA");
337 certificateName.appendVersion();
338 certificate.setName(certificateName);
339
340 // set metainfo
341 certificate.setContentType(ndn::tlv::ContentType_Key);
Ashlesh Gawande0d2c3822018-01-24 17:17:15 -0600342 certificate.setFreshnessPeriod(ndn::time::days(365));
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500343
344 // set content
345 certificate.setContent(nlsrInstanceKey.getPublicKey().data(), nlsrInstanceKey.getPublicKey().size());
346
347 // set signature-info
348 ndn::SignatureInfo signatureInfo;
349 signatureInfo.setValidityPeriod(ndn::security::ValidityPeriod(ndn::time::system_clock::TimePoint(),
350 ndn::time::system_clock::now()
Ashlesh Gawande0d2c3822018-01-24 17:17:15 -0600351 + ndn::time::days(365)));
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500352 try {
353 m_keyChain.sign(certificate,
354 ndn::security::SigningInfo(m_keyChain.getPib().getIdentity(m_confParam.getRouterPrefix()))
355 .setSignatureInfo(signatureInfo));
akmhoque102aea42014-08-04 10:22:12 -0500356 }
dmcoomes9f936662017-03-02 10:33:09 -0600357 catch (const std::exception& e) {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500358 NLSR_LOG_WARN("ERROR: Router's " << e.what()
359 << "NLSR is running without security."
360 << " If security is enabled NLSR will not converge.");
361
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000362 std::cerr << "Router's " << e.what() << ". NLSR is running without security "
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500363 << "(Only for testing, should not be used in production.)"
364 << " If security is enabled NLSR will not converge." << std::endl;
akmhoque102aea42014-08-04 10:22:12 -0500365 }
akmhoque443ad812014-07-29 10:26:56 -0500366
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500367 m_signingInfo = ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_ID,
368 nlsrInstanceName);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700369
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700370 loadCertToPublish(certificate);
371
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500372 m_defaultCertName = certificate.getName();
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700373}
374
375void
376Nlsr::registerKeyPrefix()
377{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500378 // Start listening for the interest of this router's NLSR certificate
379 ndn::Name nlsrKeyPrefix = getConfParameter().getRouterPrefix();
380 nlsrKeyPrefix.append("NLSR");
381 nlsrKeyPrefix.append("KEY");
382
383 m_nlsrFace.setInterestFilter(nlsrKeyPrefix,
Joao Pereira97473d42015-07-03 16:57:27 -0400384 std::bind(&Nlsr::onKeyInterest,
Yingdi Yu6a3a4dd2014-06-20 14:10:39 -0700385 this, _1, _2),
Joao Pereira97473d42015-07-03 16:57:27 -0400386 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
387 std::bind(&Nlsr::registrationFailed, this, _1),
388 m_signingInfo,
akmhoque060d3022014-08-12 13:35:06 -0500389 ndn::nfd::ROUTE_FLAG_CAPTURE);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700390
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500391 // Start listening for the interest of this router's certificate
392 ndn::Name routerKeyPrefix = getConfParameter().getRouterPrefix();
393 routerKeyPrefix.append("KEY");
394
395 m_nlsrFace.setInterestFilter(routerKeyPrefix,
396 std::bind(&Nlsr::onKeyInterest,
397 this, _1, _2),
398 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
399 std::bind(&Nlsr::registrationFailed, this, _1),
400 m_signingInfo,
401 ndn::nfd::ROUTE_FLAG_CAPTURE);
402
403 // Start listening for the interest of this router's operator's certificate
404 ndn::Name operatorKeyPrefix = getConfParameter().getNetwork();
405 operatorKeyPrefix.append(getConfParameter().getSiteName());
406 operatorKeyPrefix.append(std::string("%C1.Operator"));
407
408 m_nlsrFace.setInterestFilter(operatorKeyPrefix,
409 std::bind(&Nlsr::onKeyInterest,
410 this, _1, _2),
411 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
412 std::bind(&Nlsr::registrationFailed, this, _1),
413 m_signingInfo,
414 ndn::nfd::ROUTE_FLAG_CAPTURE);
415
416 // Start listening for the interest of this router's site's certificate
417 ndn::Name siteKeyPrefix = getConfParameter().getNetwork();
418 siteKeyPrefix.append(getConfParameter().getSiteName());
419 siteKeyPrefix.append("KEY");
420
421 m_nlsrFace.setInterestFilter(siteKeyPrefix,
422 std::bind(&Nlsr::onKeyInterest,
423 this, _1, _2),
424 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
425 std::bind(&Nlsr::registrationFailed, this, _1),
426 m_signingInfo,
427 ndn::nfd::ROUTE_FLAG_CAPTURE);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700428}
429
430void
alvy297f4162015-03-03 17:15:33 -0600431Nlsr::registerLocalhostPrefix()
432{
alvy297f4162015-03-03 17:15:33 -0600433 m_nlsrFace.registerPrefix(LOCALHOST_PREFIX,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500434 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
alvy297f4162015-03-03 17:15:33 -0600435 std::bind(&Nlsr::registrationFailed, this, _1));
436}
437
438void
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700439Nlsr::onKeyInterest(const ndn::Name& name, const ndn::Interest& interest)
440{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500441 NLSR_LOG_DEBUG("Got interest for certificate. Interest: " << interest.getName());
442
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700443 const ndn::Name& interestName = interest.getName();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500444 const ndn::security::v2::Certificate* cert = getCertificate(interestName);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700445
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500446 if (cert == nullptr) {
447 NLSR_LOG_DEBUG("Certificate is not found for: " << interest);
dmcoomes9eaf3f42017-02-21 11:39:01 -0600448 return; // cert is not found
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500449 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700450
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500451 m_nlsrFace.put(*cert);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700452}
453
454void
455Nlsr::onKeyPrefixRegSuccess(const ndn::Name& name)
456{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500457 NLSR_LOG_DEBUG("KEY prefix: " << name << " registration is successful.");
akmhoque53353462014-04-22 08:43:45 -0500458}
akmhoque5a44dd42014-03-12 18:11:32 -0500459
akmhoque53353462014-04-22 08:43:45 -0500460void
akmhoquec04e7272014-07-02 11:00:14 -0500461Nlsr::onFaceEventNotification(const ndn::nfd::FaceEventNotification& faceEventNotification)
akmhoquee1765152014-06-30 11:32:01 -0500462{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500463 NLSR_LOG_TRACE("Nlsr::onFaceEventNotification called");
Vince Lehman02e32992015-03-11 12:31:20 -0500464
Nick Gordond5c1a372016-10-31 13:56:23 -0500465 switch (faceEventNotification.getKind()) {
466 case ndn::nfd::FACE_EVENT_DESTROYED: {
467 uint64_t faceId = faceEventNotification.getFaceId();
Vince Lehman02e32992015-03-11 12:31:20 -0500468
Nick Gordond5c1a372016-10-31 13:56:23 -0500469 auto adjacent = m_adjacencyList.findAdjacent(faceId);
Vince Lehman02e32992015-03-11 12:31:20 -0500470
Nick Gordond5c1a372016-10-31 13:56:23 -0500471 if (adjacent != m_adjacencyList.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500472 NLSR_LOG_DEBUG("Face to " << adjacent->getName() << " with face id: " << faceId << " destroyed");
Vince Lehman02e32992015-03-11 12:31:20 -0500473
Nick Gordond5c1a372016-10-31 13:56:23 -0500474 adjacent->setFaceId(0);
Vince Lehman02e32992015-03-11 12:31:20 -0500475
Nick Gordond5c1a372016-10-31 13:56:23 -0500476 // Only trigger an Adjacency LSA build if this node is changing
477 // from ACTIVE to INACTIVE since this rebuild will effectively
478 // cancel the previous Adjacency LSA refresh event and schedule
479 // a new one further in the future.
480 //
481 // Continuously scheduling the refresh in the future will block
482 // the router from refreshing its Adjacency LSA. Since other
483 // routers' Name prefixes' expiration times are updated when
484 // this router refreshes its Adjacency LSA, the other routers'
485 // prefixes will expire and be removed from the RIB.
486 //
487 // This check is required to fix Bug #2733 for now. This check
488 // would be unnecessary to fix Bug #2733 when Issue #2732 is
489 // completed, but the check also helps with optimization so it
490 // can remain even when Issue #2732 is implemented.
491 if (adjacent->getStatus() == Adjacent::STATUS_ACTIVE) {
492 adjacent->setStatus(Adjacent::STATUS_INACTIVE);
Vince Lehman02e32992015-03-11 12:31:20 -0500493
Nick Gordond5c1a372016-10-31 13:56:23 -0500494 // A new adjacency LSA cannot be built until the neighbor is marked INACTIVE and
495 // has met the HELLO retry threshold
496 adjacent->setInterestTimedOutNo(m_confParam.getInterestRetryNumber());
Vince Lehman199e9cf2015-04-07 13:22:16 -0500497
Nick Gordond5c1a372016-10-31 13:56:23 -0500498 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
499 getRoutingTable().scheduleRoutingTableCalculation(*this);
500 }
501 else {
502 m_nlsrLsdb.scheduleAdjLsaBuild();
503 }
Nick Gordone8e03ac2016-07-07 14:24:38 -0500504 }
Vince Lehman199e9cf2015-04-07 13:22:16 -0500505 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500506 break;
akmhoquec04e7272014-07-02 11:00:14 -0500507 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500508 case ndn::nfd::FACE_EVENT_CREATED: {
509 // Find the neighbor in our adjacency list
Ashlesh Gawande0d2c3822018-01-24 17:17:15 -0600510 ndn::FaceUri faceUri;
511 try {
512 faceUri = ndn::FaceUri(faceEventNotification.getRemoteUri());
513 }
514 catch (const std::exception& e) {
515 NLSR_LOG_WARN(e.what());
516 return;
517 }
518 auto adjacent = m_adjacencyList.findAdjacent(faceUri);
519
Nick Gordond5c1a372016-10-31 13:56:23 -0500520 // If we have a neighbor by that FaceUri and it has no FaceId, we
521 // have a match.
522 if (adjacent != m_adjacencyList.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500523 NLSR_LOG_DEBUG("Face creation event matches neighbor: " << adjacent->getName()
Nick Gordond5c1a372016-10-31 13:56:23 -0500524 << ". New Face ID: " << faceEventNotification.getFaceId()
525 << ". Registering prefixes.");
526 adjacent->setFaceId(faceEventNotification.getFaceId());
527
528 registerAdjacencyPrefixes(*adjacent, ndn::time::milliseconds::max());
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500529
530 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
531 getRoutingTable().scheduleRoutingTableCalculation(*this);
532 }
533 else {
534 m_nlsrLsdb.scheduleAdjLsaBuild();
535 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500536 }
537 break;
538 }
539 default:
540 break;
akmhoquec04e7272014-07-02 11:00:14 -0500541 }
akmhoquee1765152014-06-30 11:32:01 -0500542}
543
Nick Gordond5c1a372016-10-31 13:56:23 -0500544void
545Nlsr::initializeFaces(const FetchDatasetCallback& onFetchSuccess,
546 const FetchDatasetTimeoutCallback& onFetchFailure)
547{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500548 NLSR_LOG_TRACE("Initializing Faces...");
Nick Gordond5c1a372016-10-31 13:56:23 -0500549
550 m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(onFetchSuccess, onFetchFailure);
551
552}
553
554void
555Nlsr::processFaceDataset(const std::vector<ndn::nfd::FaceStatus>& faces)
556{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500557 NLSR_LOG_DEBUG("Processing face dataset");
Nick Gordond5c1a372016-10-31 13:56:23 -0500558
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500559 // Iterate over each neighbor listed in nlsr.conf
560 for (auto& adjacent : m_adjacencyList.getAdjList()) {
561
562 const std::string faceUriString = adjacent.getFaceUri().toString();
Nick Gordond5c1a372016-10-31 13:56:23 -0500563 // Check the list of FaceStatus objects we got for a match
564 for (const ndn::nfd::FaceStatus& faceStatus : faces) {
Nick Gordond5c1a372016-10-31 13:56:23 -0500565 // Set the adjacency FaceID if we find a URI match and it was
566 // previously unset. Change the boolean to true.
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500567 if (adjacent.getFaceId() == 0 && faceUriString == faceStatus.getRemoteUri()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500568 NLSR_LOG_DEBUG("FaceUri: " << faceStatus.getRemoteUri() <<
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500569 " FaceId: "<< faceStatus.getFaceId());
570 adjacent.setFaceId(faceStatus.getFaceId());
Nick Gordond5c1a372016-10-31 13:56:23 -0500571 // Register the prefixes for each neighbor
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500572 this->registerAdjacencyPrefixes(adjacent, ndn::time::milliseconds::max());
Nick Gordond5c1a372016-10-31 13:56:23 -0500573 }
574 }
575 // If this adjacency has no information in this dataset, then one
576 // of two things is happening: 1. NFD is starting slowly and this
577 // Face wasn't ready yet, or 2. NFD is configured
578 // incorrectly and this Face isn't available.
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500579 if (adjacent.getFaceId() == 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500580 NLSR_LOG_WARN("The adjacency " << adjacent.getName() <<
Nick Gordond5c1a372016-10-31 13:56:23 -0500581 " has no Face information in this dataset.");
582 }
583 }
584
Nick Gordond5c1a372016-10-31 13:56:23 -0500585 scheduleDatasetFetch();
586}
587
588void
589Nlsr::registerAdjacencyPrefixes(const Adjacent& adj,
590 const ndn::time::milliseconds& timeout)
591{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500592 ndn::FaceUri faceUri = adj.getFaceUri();
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500593 double linkCost = adj.getLinkCost();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500594 const ndn::Name& adjName = adj.getName();
Nick Gordond5c1a372016-10-31 13:56:23 -0500595
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500596 m_fib.registerPrefix(adjName, faceUri, linkCost,
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500597 timeout, ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500598
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500599 m_fib.registerPrefix(m_confParam.getChronosyncPrefix(),
600 faceUri, linkCost, timeout,
601 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500602
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500603 m_fib.registerPrefix(m_confParam.getLsaPrefix(),
604 faceUri, linkCost, timeout,
605 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500606}
607
608void
609Nlsr::onFaceDatasetFetchTimeout(uint32_t code,
610 const std::string& msg,
611 uint32_t nRetriesSoFar)
612{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500613 NLSR_LOG_DEBUG("onFaceDatasetFetchTimeout");
Nick Gordond5c1a372016-10-31 13:56:23 -0500614 // If we have exceeded the maximum attempt count, do not try again.
615 if (nRetriesSoFar++ < m_confParam.getFaceDatasetFetchTries()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500616 NLSR_LOG_DEBUG("Failed to fetch dataset: " << msg << ". Attempting retry #" << nRetriesSoFar);
Nick Gordond5c1a372016-10-31 13:56:23 -0500617 m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(std::bind(&Nlsr::processFaceDataset,
618 this, _1),
619 std::bind(&Nlsr::onFaceDatasetFetchTimeout,
620 this, _1, _2, nRetriesSoFar));
621 }
622 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500623 NLSR_LOG_ERROR("Failed to fetch dataset: " << msg << ". Exceeded limit of " <<
Nick Gordond5c1a372016-10-31 13:56:23 -0500624 m_confParam.getFaceDatasetFetchTries() << ", so not trying again this time.");
625 // If we fail to fetch it, just do nothing until the next
626 // interval. Since this is a backup mechanism, we aren't as
627 // concerned with retrying.
628 scheduleDatasetFetch();
629 }
630}
631
632void
633Nlsr::scheduleDatasetFetch()
634{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500635 NLSR_LOG_DEBUG("Scheduling Dataset Fetch in " << m_confParam.getFaceDatasetFetchInterval());
636
Nick Gordond5c1a372016-10-31 13:56:23 -0500637 m_scheduler.scheduleEvent(m_confParam.getFaceDatasetFetchInterval(),
638 [this] {
639 this->initializeFaces(
640 [this] (const std::vector<ndn::nfd::FaceStatus>& faces) {
641 this->processFaceDataset(faces);
642 },
643 [this] (uint32_t code, const std::string& msg) {
644 this->onFaceDatasetFetchTimeout(code, msg, 0);
645 });
646 });
647}
akmhoquee1765152014-06-30 11:32:01 -0500648
649void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500650Nlsr::enableIncomingFaceIdIndication()
651{
652 NLSR_LOG_DEBUG("Enabling incoming face id indication for local face.");
653
654 m_controller.start<ndn::nfd::FaceUpdateCommand>(
655 ndn::nfd::ControlParameters()
656 .setFlagBit(ndn::nfd::FaceFlagBit::BIT_LOCAL_FIELDS_ENABLED, true),
657 bind(&Nlsr::onFaceIdIndicationSuccess, this, _1),
658 bind(&Nlsr::onFaceIdIndicationFailure, this, _1));
659}
660
661void
662Nlsr::onFaceIdIndicationSuccess(const ndn::nfd::ControlParameters& cp)
663{
664 NLSR_LOG_DEBUG("Successfully enabled incoming face id indication"
665 << "for face id " << cp.getFaceId());
666}
667
668void
669Nlsr::onFaceIdIndicationFailure(const ndn::nfd::ControlResponse& cr)
670{
671 std::ostringstream os;
672 os << "Failed to enable incoming face id indication feature: " <<
673 "(code: " << cr.getCode() << ", reason: " << cr.getText() << ")";
674
675 NLSR_LOG_DEBUG(os.str());
676}
677
678void
akmhoque53353462014-04-22 08:43:45 -0500679Nlsr::startEventLoop()
680{
akmhoquefdbddb12014-05-02 18:35:19 -0500681 m_nlsrFace.processEvents();
akmhoque53353462014-04-22 08:43:45 -0500682}
akmhoque5a44dd42014-03-12 18:11:32 -0500683
akmhoqueb1710aa2014-02-19 17:13:36 -0600684} // namespace nlsr