blob: 60459353062d24d0445689fdd1dc8b23b118d7f8 [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
Nick Gordon922714a2017-06-13 14:12:02 -0500154Nlsr::canonizeContinuation(std::list<Adjacent>::iterator iterator,
155 std::function<void(void)> finally)
Nick Gordon9461afb2017-04-25 15:54:50 -0500156{
Nick Gordon922714a2017-06-13 14:12:02 -0500157 canonizeNeighborUris(iterator, [this, finally] (std::list<Adjacent>::iterator iterator) {
158 canonizeContinuation(iterator, finally);
159 },
160 finally);
Nick Gordon9461afb2017-04-25 15:54:50 -0500161}
162
163void
164Nlsr::canonizeNeighborUris(std::list<Adjacent>::iterator currentNeighbor,
Nick Gordon922714a2017-06-13 14:12:02 -0500165 std::function<void(std::list<Adjacent>::iterator)> then,
166 std::function<void(void)> finally)
Nick Gordon9461afb2017-04-25 15:54:50 -0500167{
168 if (currentNeighbor != m_adjacencyList.getAdjList().end()) {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500169 ndn::FaceUri uri(currentNeighbor->getFaceUri());
Ashlesh Gawandee6ba9152018-03-30 01:15:00 -0500170 uri.canonize([then, currentNeighbor] (ndn::FaceUri canonicalUri) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500171 NLSR_LOG_DEBUG("Canonized URI: " << currentNeighbor->getFaceUri()
Nick Gordon9461afb2017-04-25 15:54:50 -0500172 << " to: " << canonicalUri);
Nick Gordone9733ed2017-04-26 10:48:39 -0500173 currentNeighbor->setFaceUri(canonicalUri);
Nick Gordon9461afb2017-04-25 15:54:50 -0500174 then(std::next(currentNeighbor));
175 },
Ashlesh Gawandee6ba9152018-03-30 01:15:00 -0500176 [then, currentNeighbor] (const std::string& reason) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500177 NLSR_LOG_ERROR("Could not canonize URI: " << currentNeighbor->getFaceUri()
Nick Gordon9461afb2017-04-25 15:54:50 -0500178 << " because: " << reason);
179 then(std::next(currentNeighbor));
180 },
181 m_nlsrFace.getIoService(),
182 TIME_ALLOWED_FOR_CANONIZATION);
183 }
Nick Gordon922714a2017-06-13 14:12:02 -0500184 // We have finished canonizing all neighbors, so call finally()
Nick Gordon9461afb2017-04-25 15:54:50 -0500185 else {
Nick Gordon922714a2017-06-13 14:12:02 -0500186 finally();
Nick Gordon9461afb2017-04-25 15:54:50 -0500187 }
188}
189
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500190void
191Nlsr::loadCertToPublish(const ndn::security::v2::Certificate& certificate)
192{
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000193 NLSR_LOG_TRACE("Loading cert to publish.");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500194 m_certStore.insert(certificate);
195 m_validator.loadAnchor("Authoritative-Certificate",
196 ndn::security::v2::Certificate(certificate));
197 m_prefixUpdateProcessor.getValidator().
198 loadAnchor("Authoritative-Certificate",
199 ndn::security::v2::Certificate(certificate));
200}
201
Nick Gordon9461afb2017-04-25 15:54:50 -0500202void
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000203Nlsr::connectToFetcher(ndn::util::SegmentFetcher& fetcher)
204{
205 NLSR_LOG_TRACE("NLSR: Connect to SegmentFetcher.");
206
207 fetcher.afterSegmentValidated.connect(std::bind(&Nlsr::afterFetcherSignalEmitted,
208 this, _1));
209}
210
211void
212Nlsr::afterFetcherSignalEmitted(const ndn::Data& lsaSegment)
213{
214 NLSR_LOG_TRACE("SegmentFetcher fetched a data segment. Start inserting cert to own cert store.");
215 ndn::Name keyName = lsaSegment.getSignature().getKeyLocator().getName();
216 if (getCertificate(keyName) == nullptr) {
217 publishCertFromCache(keyName);
218 }
219 else {
220 NLSR_LOG_TRACE("Certificate is already in the store: " << keyName);
221 }
222}
223
224void
225Nlsr::publishCertFromCache(const ndn::Name& keyName)
226{
227 const ndn::security::v2::Certificate* cert = m_validator.getUnverifiedCertCache()
228 .find(keyName);
229 if (cert != nullptr) {
230 m_certStore.insert(*cert);
231 NLSR_LOG_TRACE(*cert);
Ashlesh Gawande3494f732018-11-06 16:04:03 -0600232 ndn::Name certName = ndn::security::v2::extractKeyNameFromCertName(cert->getName());
233 NLSR_LOG_TRACE("Setting interest filter for: " << certName);
234 m_nlsrFace.setInterestFilter(ndn::InterestFilter(certName).allowLoopback(false),
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000235 std::bind(&Nlsr::onKeyInterest,
236 this, _1, _2),
237 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
238 std::bind(&Nlsr::registrationFailed, this, _1),
239 m_signingInfo,
240 ndn::nfd::ROUTE_FLAG_CAPTURE);
241
242 if (!cert->getKeyName().equals(cert->getSignature().getKeyLocator().getName())) {
243 publishCertFromCache(cert->getSignature().getKeyLocator().getName());
244 }
245 }
246 else {
247 NLSR_LOG_TRACE("Cert for " << keyName << " was not found in the Validator's cache. ");
248 }
249}
250
251void
akmhoque53353462014-04-22 08:43:45 -0500252Nlsr::initialize()
253{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500254 NLSR_LOG_DEBUG("Initializing Nlsr");
akmhoque53353462014-04-22 08:43:45 -0500255 m_confParam.buildRouterPrefix();
laqinfan35731852017-08-08 06:17:39 -0500256 m_datasetHandler.setRouterNameCommandPrefix(m_confParam.getRouterPrefix());
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700257 m_nlsrLsdb.setLsaRefreshTime(ndn::time::seconds(m_confParam.getLsaRefreshTime()));
akmhoque31d1d4b2014-05-05 22:08:14 -0500258 m_nlsrLsdb.setThisRouterPrefix(m_confParam.getRouterPrefix().toUri());
akmhoque53353462014-04-22 08:43:45 -0500259 m_fib.setEntryRefreshTime(2 * m_confParam.getLsaRefreshTime());
Vince Lehmanc11cc202015-01-20 11:41:33 -0600260
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500261 m_nlsrLsdb.getSequencingManager().setSeqFileDirectory(m_confParam.getSeqFileDir());
262 m_nlsrLsdb.getSequencingManager().initiateSeqNoFromFile(m_confParam.getHyperbolicState());
263
Ashlesh Gawande48101072018-05-30 17:53:06 -0500264 m_nlsrLsdb.getSyncLogicHandler().createSyncLogic(m_confParam.getChronosyncPrefix(),
265 m_confParam.getSyncInterestLifetime());
Vince Lehmanc11cc202015-01-20 11:41:33 -0600266
dmcoomes9f936662017-03-02 10:33:09 -0600267 // Logging start
akmhoque674b0b12014-05-20 14:33:28 -0500268 m_confParam.writeLog();
269 m_adjacencyList.writeLog();
dmcoomes5bcb39e2017-10-31 15:07:55 -0500270 NLSR_LOG_DEBUG(m_namePrefixList);
dmcoomes9f936662017-03-02 10:33:09 -0600271 // Logging end
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500272
akmhoque443ad812014-07-29 10:26:56 -0500273 initializeKey();
akmhoquec04e7272014-07-02 11:00:14 -0500274 setStrategies();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500275
dmcoomes5bcb39e2017-10-31 15:07:55 -0500276 NLSR_LOG_DEBUG("Default NLSR identity: " << m_signingInfo.getSignerName());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500277
akmhoque31d1d4b2014-05-05 22:08:14 -0500278 setInfoInterestFilter();
279 setLsaInterestFilter();
Vince Lehman50df6b72015-03-03 12:06:40 -0600280
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500281 // add top-level prefixes: router and localhost prefix
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500282 addDispatcherTopPrefix(ndn::Name(m_confParam.getRouterPrefix()).append("nlsr"));
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500283 addDispatcherTopPrefix(LOCALHOST_PREFIX);
284
Nick Gordond5c1a372016-10-31 13:56:23 -0500285 initializeFaces(std::bind(&Nlsr::processFaceDataset, this, _1),
286 std::bind(&Nlsr::onFaceDatasetFetchTimeout, this, _1, _2, 0));
287
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500288 enableIncomingFaceIdIndication();
289
Vince Lehman50df6b72015-03-03 12:06:40 -0600290 // Set event intervals
291 setFirstHelloInterval(m_confParam.getFirstHelloInterval());
292 m_nlsrLsdb.setAdjLsaBuildInterval(m_confParam.getAdjLsaBuildInterval());
293 m_routingTable.setRoutingCalcInterval(m_confParam.getRoutingCalcInterval());
294
akmhoque674b0b12014-05-20 14:33:28 -0500295 m_nlsrLsdb.buildAndInstallOwnNameLsa();
Nick Gordon5c467f02016-07-13 13:40:10 -0500296
297 // Install coordinate LSAs if using HR or dry-run HR.
298 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
299 m_nlsrLsdb.buildAndInstallOwnCoordinateLsa();
300 }
Vince Lehman904c2412014-09-23 19:36:11 -0500301
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700302 registerKeyPrefix();
alvy297f4162015-03-03 17:15:33 -0600303 registerLocalhostPrefix();
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500304 registerRouterPrefix();
Vince Lehman7b616582014-10-17 16:25:39 -0500305
Vince Lehman7b616582014-10-17 16:25:39 -0500306 m_helloProtocol.scheduleInterest(m_firstHelloInterval);
Vince Lehman09131122014-09-09 17:10:11 -0500307
308 // Need to set direct neighbors' costs to 0 for hyperbolic routing
309 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
310
311 std::list<Adjacent>& neighbors = m_adjacencyList.getAdjList();
312
313 for (std::list<Adjacent>::iterator it = neighbors.begin(); it != neighbors.end(); ++it) {
314 it->setLinkCost(0);
315 }
316 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700317}
318
319void
akmhoque443ad812014-07-29 10:26:56 -0500320Nlsr::initializeKey()
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700321{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500322 NLSR_LOG_DEBUG("Initializing Key ...");
323
324 ndn::Name nlsrInstanceName = m_confParam.getRouterPrefix();
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500325 nlsrInstanceName.append("nlsr");
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700326
Joao Pereira97473d42015-07-03 16:57:27 -0400327 try {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500328 m_keyChain.deleteIdentity(m_keyChain.getPib().getIdentity(nlsrInstanceName));
329 } catch (const std::exception& e) {
330 NLSR_LOG_WARN(e.what());
331 }
332
333 auto nlsrInstanceIdentity = m_keyChain.createIdentity(nlsrInstanceName);
334 auto nlsrInstanceKey = nlsrInstanceIdentity.getDefaultKey();
335
336 ndn::security::v2::Certificate certificate;
337
338 ndn::Name certificateName = nlsrInstanceKey.getName();
339 certificateName.append("NA");
340 certificateName.appendVersion();
341 certificate.setName(certificateName);
342
343 // set metainfo
344 certificate.setContentType(ndn::tlv::ContentType_Key);
Ashlesh Gawande0d2c3822018-01-24 17:17:15 -0600345 certificate.setFreshnessPeriod(ndn::time::days(365));
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500346
347 // set content
348 certificate.setContent(nlsrInstanceKey.getPublicKey().data(), nlsrInstanceKey.getPublicKey().size());
349
350 // set signature-info
351 ndn::SignatureInfo signatureInfo;
352 signatureInfo.setValidityPeriod(ndn::security::ValidityPeriod(ndn::time::system_clock::TimePoint(),
353 ndn::time::system_clock::now()
Ashlesh Gawande0d2c3822018-01-24 17:17:15 -0600354 + ndn::time::days(365)));
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500355 try {
356 m_keyChain.sign(certificate,
357 ndn::security::SigningInfo(m_keyChain.getPib().getIdentity(m_confParam.getRouterPrefix()))
358 .setSignatureInfo(signatureInfo));
akmhoque102aea42014-08-04 10:22:12 -0500359 }
dmcoomes9f936662017-03-02 10:33:09 -0600360 catch (const std::exception& e) {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500361 NLSR_LOG_WARN("ERROR: Router's " << e.what()
362 << "NLSR is running without security."
363 << " If security is enabled NLSR will not converge.");
364
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000365 std::cerr << "Router's " << e.what() << ". NLSR is running without security "
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500366 << "(Only for testing, should not be used in production.)"
367 << " If security is enabled NLSR will not converge." << std::endl;
akmhoque102aea42014-08-04 10:22:12 -0500368 }
akmhoque443ad812014-07-29 10:26:56 -0500369
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500370 m_signingInfo = ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_ID,
371 nlsrInstanceName);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700372
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700373 loadCertToPublish(certificate);
374
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500375 m_defaultCertName = certificate.getName();
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700376}
377
378void
379Nlsr::registerKeyPrefix()
380{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500381 // Start listening for the interest of this router's NLSR certificate
382 ndn::Name nlsrKeyPrefix = getConfParameter().getRouterPrefix();
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500383 nlsrKeyPrefix.append("nlsr");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500384 nlsrKeyPrefix.append("KEY");
385
Ashlesh Gawande3494f732018-11-06 16:04:03 -0600386 m_nlsrFace.setInterestFilter(ndn::InterestFilter(nlsrKeyPrefix).allowLoopback(false),
Joao Pereira97473d42015-07-03 16:57:27 -0400387 std::bind(&Nlsr::onKeyInterest,
Yingdi Yu6a3a4dd2014-06-20 14:10:39 -0700388 this, _1, _2),
Joao Pereira97473d42015-07-03 16:57:27 -0400389 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
390 std::bind(&Nlsr::registrationFailed, this, _1),
391 m_signingInfo,
akmhoque060d3022014-08-12 13:35:06 -0500392 ndn::nfd::ROUTE_FLAG_CAPTURE);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700393
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500394 // Start listening for the interest of this router's certificate
395 ndn::Name routerKeyPrefix = getConfParameter().getRouterPrefix();
396 routerKeyPrefix.append("KEY");
397
Ashlesh Gawande3494f732018-11-06 16:04:03 -0600398 m_nlsrFace.setInterestFilter(ndn::InterestFilter(routerKeyPrefix).allowLoopback(false),
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500399 std::bind(&Nlsr::onKeyInterest,
400 this, _1, _2),
401 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
402 std::bind(&Nlsr::registrationFailed, this, _1),
403 m_signingInfo,
404 ndn::nfd::ROUTE_FLAG_CAPTURE);
405
406 // Start listening for the interest of this router's operator's certificate
407 ndn::Name operatorKeyPrefix = getConfParameter().getNetwork();
408 operatorKeyPrefix.append(getConfParameter().getSiteName());
409 operatorKeyPrefix.append(std::string("%C1.Operator"));
410
Ashlesh Gawande3494f732018-11-06 16:04:03 -0600411 m_nlsrFace.setInterestFilter(ndn::InterestFilter(operatorKeyPrefix).allowLoopback(false),
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500412 std::bind(&Nlsr::onKeyInterest,
413 this, _1, _2),
414 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
415 std::bind(&Nlsr::registrationFailed, this, _1),
416 m_signingInfo,
417 ndn::nfd::ROUTE_FLAG_CAPTURE);
418
419 // Start listening for the interest of this router's site's certificate
420 ndn::Name siteKeyPrefix = getConfParameter().getNetwork();
421 siteKeyPrefix.append(getConfParameter().getSiteName());
422 siteKeyPrefix.append("KEY");
423
Ashlesh Gawande3494f732018-11-06 16:04:03 -0600424 m_nlsrFace.setInterestFilter(ndn::InterestFilter(siteKeyPrefix).allowLoopback(false),
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500425 std::bind(&Nlsr::onKeyInterest,
426 this, _1, _2),
427 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
428 std::bind(&Nlsr::registrationFailed, this, _1),
429 m_signingInfo,
430 ndn::nfd::ROUTE_FLAG_CAPTURE);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700431}
432
433void
alvy297f4162015-03-03 17:15:33 -0600434Nlsr::registerLocalhostPrefix()
435{
alvy297f4162015-03-03 17:15:33 -0600436 m_nlsrFace.registerPrefix(LOCALHOST_PREFIX,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500437 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
alvy297f4162015-03-03 17:15:33 -0600438 std::bind(&Nlsr::registrationFailed, this, _1));
439}
440
441void
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500442Nlsr::registerRouterPrefix()
443{
444 m_nlsrFace.registerPrefix(ndn::Name(m_confParam.getRouterPrefix()).append("nlsr"),
445 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
446 std::bind(&Nlsr::registrationFailed, this, _1));
447}
448
449void
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700450Nlsr::onKeyInterest(const ndn::Name& name, const ndn::Interest& interest)
451{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500452 NLSR_LOG_DEBUG("Got interest for certificate. Interest: " << interest.getName());
453
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700454 const ndn::Name& interestName = interest.getName();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500455 const ndn::security::v2::Certificate* cert = getCertificate(interestName);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700456
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500457 if (cert == nullptr) {
458 NLSR_LOG_DEBUG("Certificate is not found for: " << interest);
dmcoomes9eaf3f42017-02-21 11:39:01 -0600459 return; // cert is not found
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500460 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700461
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500462 m_nlsrFace.put(*cert);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700463}
464
465void
466Nlsr::onKeyPrefixRegSuccess(const ndn::Name& name)
467{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500468 NLSR_LOG_DEBUG("KEY prefix: " << name << " registration is successful.");
akmhoque53353462014-04-22 08:43:45 -0500469}
akmhoque5a44dd42014-03-12 18:11:32 -0500470
akmhoque53353462014-04-22 08:43:45 -0500471void
akmhoquec04e7272014-07-02 11:00:14 -0500472Nlsr::onFaceEventNotification(const ndn::nfd::FaceEventNotification& faceEventNotification)
akmhoquee1765152014-06-30 11:32:01 -0500473{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500474 NLSR_LOG_TRACE("Nlsr::onFaceEventNotification called");
Vince Lehman02e32992015-03-11 12:31:20 -0500475
Nick Gordond5c1a372016-10-31 13:56:23 -0500476 switch (faceEventNotification.getKind()) {
477 case ndn::nfd::FACE_EVENT_DESTROYED: {
478 uint64_t faceId = faceEventNotification.getFaceId();
Vince Lehman02e32992015-03-11 12:31:20 -0500479
Nick Gordond5c1a372016-10-31 13:56:23 -0500480 auto adjacent = m_adjacencyList.findAdjacent(faceId);
Vince Lehman02e32992015-03-11 12:31:20 -0500481
Nick Gordond5c1a372016-10-31 13:56:23 -0500482 if (adjacent != m_adjacencyList.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500483 NLSR_LOG_DEBUG("Face to " << adjacent->getName() << " with face id: " << faceId << " destroyed");
Vince Lehman02e32992015-03-11 12:31:20 -0500484
Nick Gordond5c1a372016-10-31 13:56:23 -0500485 adjacent->setFaceId(0);
Vince Lehman02e32992015-03-11 12:31:20 -0500486
Nick Gordond5c1a372016-10-31 13:56:23 -0500487 // Only trigger an Adjacency LSA build if this node is changing
488 // from ACTIVE to INACTIVE since this rebuild will effectively
489 // cancel the previous Adjacency LSA refresh event and schedule
490 // a new one further in the future.
491 //
492 // Continuously scheduling the refresh in the future will block
493 // the router from refreshing its Adjacency LSA. Since other
494 // routers' Name prefixes' expiration times are updated when
495 // this router refreshes its Adjacency LSA, the other routers'
496 // prefixes will expire and be removed from the RIB.
497 //
498 // This check is required to fix Bug #2733 for now. This check
499 // would be unnecessary to fix Bug #2733 when Issue #2732 is
500 // completed, but the check also helps with optimization so it
501 // can remain even when Issue #2732 is implemented.
502 if (adjacent->getStatus() == Adjacent::STATUS_ACTIVE) {
503 adjacent->setStatus(Adjacent::STATUS_INACTIVE);
Vince Lehman02e32992015-03-11 12:31:20 -0500504
Nick Gordond5c1a372016-10-31 13:56:23 -0500505 // A new adjacency LSA cannot be built until the neighbor is marked INACTIVE and
506 // has met the HELLO retry threshold
507 adjacent->setInterestTimedOutNo(m_confParam.getInterestRetryNumber());
Vince Lehman199e9cf2015-04-07 13:22:16 -0500508
Nick Gordond5c1a372016-10-31 13:56:23 -0500509 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
510 getRoutingTable().scheduleRoutingTableCalculation(*this);
511 }
512 else {
513 m_nlsrLsdb.scheduleAdjLsaBuild();
514 }
Nick Gordone8e03ac2016-07-07 14:24:38 -0500515 }
Vince Lehman199e9cf2015-04-07 13:22:16 -0500516 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500517 break;
akmhoquec04e7272014-07-02 11:00:14 -0500518 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500519 case ndn::nfd::FACE_EVENT_CREATED: {
520 // Find the neighbor in our adjacency list
Ashlesh Gawande0d2c3822018-01-24 17:17:15 -0600521 ndn::FaceUri faceUri;
522 try {
523 faceUri = ndn::FaceUri(faceEventNotification.getRemoteUri());
524 }
525 catch (const std::exception& e) {
526 NLSR_LOG_WARN(e.what());
527 return;
528 }
529 auto adjacent = m_adjacencyList.findAdjacent(faceUri);
530
Nick Gordond5c1a372016-10-31 13:56:23 -0500531 // If we have a neighbor by that FaceUri and it has no FaceId, we
532 // have a match.
533 if (adjacent != m_adjacencyList.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500534 NLSR_LOG_DEBUG("Face creation event matches neighbor: " << adjacent->getName()
Nick Gordond5c1a372016-10-31 13:56:23 -0500535 << ". New Face ID: " << faceEventNotification.getFaceId()
536 << ". Registering prefixes.");
537 adjacent->setFaceId(faceEventNotification.getFaceId());
538
539 registerAdjacencyPrefixes(*adjacent, ndn::time::milliseconds::max());
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500540
541 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
542 getRoutingTable().scheduleRoutingTableCalculation(*this);
543 }
544 else {
545 m_nlsrLsdb.scheduleAdjLsaBuild();
546 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500547 }
548 break;
549 }
550 default:
551 break;
akmhoquec04e7272014-07-02 11:00:14 -0500552 }
akmhoquee1765152014-06-30 11:32:01 -0500553}
554
Nick Gordond5c1a372016-10-31 13:56:23 -0500555void
556Nlsr::initializeFaces(const FetchDatasetCallback& onFetchSuccess,
557 const FetchDatasetTimeoutCallback& onFetchFailure)
558{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500559 NLSR_LOG_TRACE("Initializing Faces...");
Nick Gordond5c1a372016-10-31 13:56:23 -0500560
561 m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(onFetchSuccess, onFetchFailure);
562
563}
564
565void
566Nlsr::processFaceDataset(const std::vector<ndn::nfd::FaceStatus>& faces)
567{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500568 NLSR_LOG_DEBUG("Processing face dataset");
Nick Gordond5c1a372016-10-31 13:56:23 -0500569
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500570 // Iterate over each neighbor listed in nlsr.conf
571 for (auto& adjacent : m_adjacencyList.getAdjList()) {
572
573 const std::string faceUriString = adjacent.getFaceUri().toString();
Nick Gordond5c1a372016-10-31 13:56:23 -0500574 // Check the list of FaceStatus objects we got for a match
575 for (const ndn::nfd::FaceStatus& faceStatus : faces) {
Nick Gordond5c1a372016-10-31 13:56:23 -0500576 // Set the adjacency FaceID if we find a URI match and it was
577 // previously unset. Change the boolean to true.
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500578 if (adjacent.getFaceId() == 0 && faceUriString == faceStatus.getRemoteUri()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500579 NLSR_LOG_DEBUG("FaceUri: " << faceStatus.getRemoteUri() <<
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500580 " FaceId: "<< faceStatus.getFaceId());
581 adjacent.setFaceId(faceStatus.getFaceId());
Nick Gordond5c1a372016-10-31 13:56:23 -0500582 // Register the prefixes for each neighbor
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500583 this->registerAdjacencyPrefixes(adjacent, ndn::time::milliseconds::max());
Nick Gordond5c1a372016-10-31 13:56:23 -0500584 }
585 }
586 // If this adjacency has no information in this dataset, then one
587 // of two things is happening: 1. NFD is starting slowly and this
588 // Face wasn't ready yet, or 2. NFD is configured
589 // incorrectly and this Face isn't available.
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500590 if (adjacent.getFaceId() == 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500591 NLSR_LOG_WARN("The adjacency " << adjacent.getName() <<
Nick Gordond5c1a372016-10-31 13:56:23 -0500592 " has no Face information in this dataset.");
593 }
594 }
595
Nick Gordond5c1a372016-10-31 13:56:23 -0500596 scheduleDatasetFetch();
597}
598
599void
600Nlsr::registerAdjacencyPrefixes(const Adjacent& adj,
601 const ndn::time::milliseconds& timeout)
602{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500603 ndn::FaceUri faceUri = adj.getFaceUri();
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500604 double linkCost = adj.getLinkCost();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500605 const ndn::Name& adjName = adj.getName();
Nick Gordond5c1a372016-10-31 13:56:23 -0500606
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500607 m_fib.registerPrefix(adjName, faceUri, linkCost,
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500608 timeout, ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500609
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500610 m_fib.registerPrefix(m_confParam.getChronosyncPrefix(),
611 faceUri, linkCost, timeout,
612 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500613
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500614 m_fib.registerPrefix(m_confParam.getLsaPrefix(),
615 faceUri, linkCost, timeout,
616 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500617}
618
619void
620Nlsr::onFaceDatasetFetchTimeout(uint32_t code,
621 const std::string& msg,
622 uint32_t nRetriesSoFar)
623{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500624 NLSR_LOG_DEBUG("onFaceDatasetFetchTimeout");
Nick Gordond5c1a372016-10-31 13:56:23 -0500625 // If we have exceeded the maximum attempt count, do not try again.
626 if (nRetriesSoFar++ < m_confParam.getFaceDatasetFetchTries()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500627 NLSR_LOG_DEBUG("Failed to fetch dataset: " << msg << ". Attempting retry #" << nRetriesSoFar);
Nick Gordond5c1a372016-10-31 13:56:23 -0500628 m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(std::bind(&Nlsr::processFaceDataset,
629 this, _1),
630 std::bind(&Nlsr::onFaceDatasetFetchTimeout,
631 this, _1, _2, nRetriesSoFar));
632 }
633 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500634 NLSR_LOG_ERROR("Failed to fetch dataset: " << msg << ". Exceeded limit of " <<
Nick Gordond5c1a372016-10-31 13:56:23 -0500635 m_confParam.getFaceDatasetFetchTries() << ", so not trying again this time.");
636 // If we fail to fetch it, just do nothing until the next
637 // interval. Since this is a backup mechanism, we aren't as
638 // concerned with retrying.
639 scheduleDatasetFetch();
640 }
641}
642
643void
644Nlsr::scheduleDatasetFetch()
645{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500646 NLSR_LOG_DEBUG("Scheduling Dataset Fetch in " << m_confParam.getFaceDatasetFetchInterval());
647
Nick Gordond5c1a372016-10-31 13:56:23 -0500648 m_scheduler.scheduleEvent(m_confParam.getFaceDatasetFetchInterval(),
649 [this] {
650 this->initializeFaces(
651 [this] (const std::vector<ndn::nfd::FaceStatus>& faces) {
652 this->processFaceDataset(faces);
653 },
654 [this] (uint32_t code, const std::string& msg) {
655 this->onFaceDatasetFetchTimeout(code, msg, 0);
656 });
657 });
658}
akmhoquee1765152014-06-30 11:32:01 -0500659
660void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500661Nlsr::enableIncomingFaceIdIndication()
662{
663 NLSR_LOG_DEBUG("Enabling incoming face id indication for local face.");
664
665 m_controller.start<ndn::nfd::FaceUpdateCommand>(
666 ndn::nfd::ControlParameters()
667 .setFlagBit(ndn::nfd::FaceFlagBit::BIT_LOCAL_FIELDS_ENABLED, true),
668 bind(&Nlsr::onFaceIdIndicationSuccess, this, _1),
669 bind(&Nlsr::onFaceIdIndicationFailure, this, _1));
670}
671
672void
673Nlsr::onFaceIdIndicationSuccess(const ndn::nfd::ControlParameters& cp)
674{
675 NLSR_LOG_DEBUG("Successfully enabled incoming face id indication"
676 << "for face id " << cp.getFaceId());
677}
678
679void
680Nlsr::onFaceIdIndicationFailure(const ndn::nfd::ControlResponse& cr)
681{
682 std::ostringstream os;
683 os << "Failed to enable incoming face id indication feature: " <<
684 "(code: " << cr.getCode() << ", reason: " << cr.getText() << ")";
685
686 NLSR_LOG_DEBUG(os.str());
687}
688
689void
akmhoque53353462014-04-22 08:43:45 -0500690Nlsr::startEventLoop()
691{
akmhoquefdbddb12014-05-02 18:35:19 -0500692 m_nlsrFace.processEvents();
akmhoque53353462014-04-22 08:43:45 -0500693}
akmhoque5a44dd42014-03-12 18:11:32 -0500694
akmhoqueb1710aa2014-02-19 17:13:36 -0600695} // namespace nlsr