blob: a6da9a066471ded1e27be50c0e90bb1ba5e2cbd5 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoaf7a2112019-03-19 14:55:20 -04002/*
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
Ashlesh Gawande85998a12017-12-07 22:22:13 -060042Nlsr::Nlsr(ndn::Face& face, ndn::KeyChain& keyChain, ConfParameter& confParam)
43 : m_face(face)
44 , m_scheduler(face.getIoService())
Laqin Fana4cf4022017-01-03 18:57:35 +000045 , m_keyChain(keyChain)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060046 , m_confParam(confParam)
47 , m_adjacencyList(confParam.getAdjacencyList())
48 , m_namePrefixList(confParam.getNamePrefixList())
49 , m_validator(m_confParam.getValidator())
50 , m_fib(m_face, m_scheduler, m_adjacencyList, m_confParam, m_keyChain)
51 , m_routingTable(m_scheduler, m_fib, m_lsdb, m_namePrefixTable, m_confParam)
52 , m_namePrefixTable(m_fib, m_routingTable, m_routingTable.afterRoutingChange)
53 , m_lsdb(m_face, m_keyChain, m_signingInfo,
54 m_confParam, m_namePrefixTable, m_routingTable)
55 , m_afterSegmentValidatedConnection(m_lsdb.afterSegmentValidatedSignal.connect(
56 std::bind(&Nlsr::afterFetcherSignalEmitted, this, _1)))
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -050057 , m_onNewLsaConnection(m_lsdb.getSync().onNewLsa->connect(
58 [this] (const ndn::Name& updateName, uint64_t sequenceNumber,
59 const ndn::Name& originRouter) {
60 registerStrategyForCerts(originRouter);
61 }))
Ashlesh Gawande85998a12017-12-07 22:22:13 -060062 , m_dispatcher(m_face, m_keyChain)
63 , m_datasetHandler(m_dispatcher, m_lsdb, m_routingTable)
64 , m_helloProtocol(m_face, m_keyChain, m_signingInfo, confParam, m_routingTable, m_lsdb)
65 , m_certStore(m_confParam.getCertStore())
66 , m_controller(m_face, m_keyChain)
67 , m_faceDatasetController(m_face, m_keyChain)
laqinfan35731852017-08-08 06:17:39 -050068 , m_prefixUpdateProcessor(m_dispatcher,
Ashlesh Gawande85998a12017-12-07 22:22:13 -060069 m_confParam.getPrefixUpdateValidator(),
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050070 m_namePrefixList,
Ashlesh Gawande85998a12017-12-07 22:22:13 -060071 m_lsdb,
dulalsaurab82a34c22019-02-04 17:31:21 +000072 m_confParam.getConfFileNameDynamic())
laqinfan35731852017-08-08 06:17:39 -050073 , m_nfdRibCommandProcessor(m_dispatcher,
Nick Gordon4d2c6c02017-01-20 13:18:46 -060074 m_namePrefixList,
Ashlesh Gawande85998a12017-12-07 22:22:13 -060075 m_lsdb)
76 , m_statsCollector(m_lsdb, m_helloProtocol)
77 , m_faceMonitor(m_face)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050078{
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();
Ashlesh Gawande85998a12017-12-07 22:22:13 -060081
82 setStrategies();
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050083}
84
akmhoque53353462014-04-22 08:43:45 -050085void
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -050086Nlsr::registerStrategyForCerts(const ndn::Name& originRouter)
87{
88 for (const ndn::Name& router : m_strategySetOnRouters) {
89 if (router == originRouter) {
90 // Have already set strategy for this router's certs once
91 return;
92 }
93 }
94
95 m_strategySetOnRouters.push_back(originRouter);
96
97 ndn::Name routerKey(originRouter);
98 routerKey.append("KEY");
99 ndn::Name instanceKey(originRouter);
100 instanceKey.append("nlsr").append("KEY");
101
102 m_fib.setStrategy(routerKey, Fib::BEST_ROUTE_V2_STRATEGY, 0);
103 m_fib.setStrategy(instanceKey, Fib::BEST_ROUTE_V2_STRATEGY, 0);
104
105 ndn::Name siteKey;
106 for (size_t i = 0; i < originRouter.size(); ++i) {
107 if (originRouter[i].toUri() == "%C1.Router") {
108 break;
109 }
110 siteKey.append(originRouter[i]);
111 }
112 ndn::Name opPrefix(siteKey);
113 siteKey.append("KEY");
114 m_fib.setStrategy(siteKey, Fib::BEST_ROUTE_V2_STRATEGY, 0);
115
116 opPrefix.append(std::string("%C1.Operator"));
117 m_fib.setStrategy(opPrefix, Fib::BEST_ROUTE_V2_STRATEGY, 0);
118}
119
120void
akmhoque53353462014-04-22 08:43:45 -0500121Nlsr::registrationFailed(const ndn::Name& name)
akmhoque298385a2014-02-13 14:13:09 -0600122{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500123 NLSR_LOG_ERROR("ERROR: Failed to register prefix in local hub's daemon");
dmcoomes9f936662017-03-02 10:33:09 -0600124 BOOST_THROW_EXCEPTION(Error("Error: Prefix registration failed"));
akmhoque53353462014-04-22 08:43:45 -0500125}
akmhoque1fd8c1e2014-02-19 19:41:49 -0600126
akmhoque157b0a42014-05-13 00:26:37 -0500127void
128Nlsr::onRegistrationSuccess(const ndn::Name& name)
129{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500130 NLSR_LOG_DEBUG("Successfully registered prefix: " << name);
alvy297f4162015-03-03 17:15:33 -0600131}
132
133void
akmhoque31d1d4b2014-05-05 22:08:14 -0500134Nlsr::setInfoInterestFilter()
akmhoque53353462014-04-22 08:43:45 -0500135{
akmhoque31d1d4b2014-05-05 22:08:14 -0500136 ndn::Name name(m_confParam.getRouterPrefix());
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500137 name.append("nlsr");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500138 name.append("INFO");
139
140 NLSR_LOG_DEBUG("Setting interest filter for Hello interest: " << name);
141
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600142 m_face.setInterestFilter(ndn::InterestFilter(name).allowLoopback(false),
143 std::bind(&HelloProtocol::processInterest, &m_helloProtocol, _1, _2),
144 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
145 std::bind(&Nlsr::registrationFailed, this, _1),
146 m_signingInfo, ndn::nfd::ROUTE_FLAG_CAPTURE);
akmhoque31d1d4b2014-05-05 22:08:14 -0500147}
148
149void
150Nlsr::setLsaInterestFilter()
151{
akmhoque157b0a42014-05-13 00:26:37 -0500152 ndn::Name name = m_confParam.getLsaPrefix();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500153
dmcoomes5bcb39e2017-10-31 15:07:55 -0500154 NLSR_LOG_DEBUG("Setting interest filter for LsaPrefix: " << name);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500155
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600156 m_face.setInterestFilter(ndn::InterestFilter(name).allowLoopback(false),
157 std::bind(&Lsdb::processInterest, &m_lsdb, _1, _2),
158 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
159 std::bind(&Nlsr::registrationFailed, this, _1),
160 m_signingInfo, ndn::nfd::ROUTE_FLAG_CAPTURE);
akmhoque53353462014-04-22 08:43:45 -0500161}
162
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500163void
164Nlsr::addDispatcherTopPrefix(const ndn::Name& topPrefix)
165{
166 try {
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500167 // false since we want to have control over the registration process
laqinfan35731852017-08-08 06:17:39 -0500168 m_dispatcher.addTopPrefix(topPrefix, false, m_signingInfo);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500169 }
170 catch (const std::exception& e) {
171 NLSR_LOG_ERROR("Error setting top-level prefix in dispatcher: " << e.what() << "\n");
172 }
173}
174
akmhoque53353462014-04-22 08:43:45 -0500175void
akmhoquec04e7272014-07-02 11:00:14 -0500176Nlsr::setStrategies()
akmhoque157b0a42014-05-13 00:26:37 -0500177{
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -0500178 m_fib.setStrategy(m_confParam.getLsaPrefix(), Fib::MULTICAST_STRATEGY, 0);
179 m_fib.setStrategy(m_confParam.getSyncPrefix(), Fib::MULTICAST_STRATEGY, 0);
akmhoque157b0a42014-05-13 00:26:37 -0500180}
181
182void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500183Nlsr::loadCertToPublish(const ndn::security::v2::Certificate& certificate)
184{
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000185 NLSR_LOG_TRACE("Loading cert to publish.");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500186 m_certStore.insert(certificate);
187 m_validator.loadAnchor("Authoritative-Certificate",
188 ndn::security::v2::Certificate(certificate));
189 m_prefixUpdateProcessor.getValidator().
190 loadAnchor("Authoritative-Certificate",
191 ndn::security::v2::Certificate(certificate));
192}
193
Nick Gordon9461afb2017-04-25 15:54:50 -0500194void
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000195Nlsr::afterFetcherSignalEmitted(const ndn::Data& lsaSegment)
196{
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000197 ndn::Name keyName = lsaSegment.getSignature().getKeyLocator().getName();
198 if (getCertificate(keyName) == nullptr) {
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -0500199 NLSR_LOG_TRACE("Publishing certificate for: " << keyName);
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000200 publishCertFromCache(keyName);
201 }
202 else {
203 NLSR_LOG_TRACE("Certificate is already in the store: " << keyName);
204 }
205}
206
207void
208Nlsr::publishCertFromCache(const ndn::Name& keyName)
209{
210 const ndn::security::v2::Certificate* cert = m_validator.getUnverifiedCertCache()
211 .find(keyName);
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -0500212
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000213 if (cert != nullptr) {
214 m_certStore.insert(*cert);
215 NLSR_LOG_TRACE(*cert);
Ashlesh Gawande3494f732018-11-06 16:04:03 -0600216 ndn::Name certName = ndn::security::v2::extractKeyNameFromCertName(cert->getName());
217 NLSR_LOG_TRACE("Setting interest filter for: " << certName);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600218 m_face.setInterestFilter(ndn::InterestFilter(certName).allowLoopback(false),
219 std::bind(&Nlsr::onKeyInterest, this, _1, _2),
220 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
221 std::bind(&Nlsr::registrationFailed, this, _1),
222 m_signingInfo, ndn::nfd::ROUTE_FLAG_CAPTURE);
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000223
224 if (!cert->getKeyName().equals(cert->getSignature().getKeyLocator().getName())) {
225 publishCertFromCache(cert->getSignature().getKeyLocator().getName());
226 }
227 }
228 else {
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -0500229 // Happens for root cert
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000230 NLSR_LOG_TRACE("Cert for " << keyName << " was not found in the Validator's cache. ");
231 }
232}
233
234void
akmhoque53353462014-04-22 08:43:45 -0500235Nlsr::initialize()
236{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500237 NLSR_LOG_DEBUG("Initializing Nlsr");
Vince Lehmanc11cc202015-01-20 11:41:33 -0600238
dmcoomes9f936662017-03-02 10:33:09 -0600239 // Logging start
akmhoque674b0b12014-05-20 14:33:28 -0500240 m_adjacencyList.writeLog();
dmcoomes5bcb39e2017-10-31 15:07:55 -0500241 NLSR_LOG_DEBUG(m_namePrefixList);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500242
akmhoque443ad812014-07-29 10:26:56 -0500243 initializeKey();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500244
dmcoomes5bcb39e2017-10-31 15:07:55 -0500245 NLSR_LOG_DEBUG("Default NLSR identity: " << m_signingInfo.getSignerName());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500246
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600247 // Can be moved to HelloProtocol and Lsdb ctor if initializeKey is set
248 // earlier in the Nlsr constructor so as to set m_signingInfo
akmhoque31d1d4b2014-05-05 22:08:14 -0500249 setInfoInterestFilter();
250 setLsaInterestFilter();
Vince Lehman50df6b72015-03-03 12:06:40 -0600251
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500252 // add top-level prefixes: router and localhost prefix
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500253 addDispatcherTopPrefix(ndn::Name(m_confParam.getRouterPrefix()).append("nlsr"));
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500254 addDispatcherTopPrefix(LOCALHOST_PREFIX);
255
Nick Gordond5c1a372016-10-31 13:56:23 -0500256 initializeFaces(std::bind(&Nlsr::processFaceDataset, this, _1),
257 std::bind(&Nlsr::onFaceDatasetFetchTimeout, this, _1, _2, 0));
258
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500259 enableIncomingFaceIdIndication();
260
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600261 m_lsdb.buildAndInstallOwnNameLsa();
Nick Gordon5c467f02016-07-13 13:40:10 -0500262
263 // Install coordinate LSAs if using HR or dry-run HR.
264 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600265 m_lsdb.buildAndInstallOwnCoordinateLsa();
Nick Gordon5c467f02016-07-13 13:40:10 -0500266 }
Vince Lehman904c2412014-09-23 19:36:11 -0500267
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700268 registerKeyPrefix();
alvy297f4162015-03-03 17:15:33 -0600269 registerLocalhostPrefix();
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500270 registerRouterPrefix();
Vince Lehman7b616582014-10-17 16:25:39 -0500271
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600272 m_helloProtocol.scheduleInterest(m_confParam.getFirstHelloInterval());
Vince Lehman09131122014-09-09 17:10:11 -0500273
274 // Need to set direct neighbors' costs to 0 for hyperbolic routing
275 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
276
277 std::list<Adjacent>& neighbors = m_adjacencyList.getAdjList();
278
279 for (std::list<Adjacent>::iterator it = neighbors.begin(); it != neighbors.end(); ++it) {
280 it->setLinkCost(0);
281 }
282 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700283}
284
285void
akmhoque443ad812014-07-29 10:26:56 -0500286Nlsr::initializeKey()
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700287{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500288 NLSR_LOG_DEBUG("Initializing Key ...");
289
290 ndn::Name nlsrInstanceName = m_confParam.getRouterPrefix();
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500291 nlsrInstanceName.append("nlsr");
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700292
Joao Pereira97473d42015-07-03 16:57:27 -0400293 try {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500294 m_keyChain.deleteIdentity(m_keyChain.getPib().getIdentity(nlsrInstanceName));
295 } catch (const std::exception& e) {
296 NLSR_LOG_WARN(e.what());
297 }
298
299 auto nlsrInstanceIdentity = m_keyChain.createIdentity(nlsrInstanceName);
300 auto nlsrInstanceKey = nlsrInstanceIdentity.getDefaultKey();
301
302 ndn::security::v2::Certificate certificate;
303
304 ndn::Name certificateName = nlsrInstanceKey.getName();
305 certificateName.append("NA");
306 certificateName.appendVersion();
307 certificate.setName(certificateName);
308
309 // set metainfo
310 certificate.setContentType(ndn::tlv::ContentType_Key);
Ashlesh Gawande0d2c3822018-01-24 17:17:15 -0600311 certificate.setFreshnessPeriod(ndn::time::days(365));
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500312
313 // set content
314 certificate.setContent(nlsrInstanceKey.getPublicKey().data(), nlsrInstanceKey.getPublicKey().size());
315
316 // set signature-info
317 ndn::SignatureInfo signatureInfo;
318 signatureInfo.setValidityPeriod(ndn::security::ValidityPeriod(ndn::time::system_clock::TimePoint(),
319 ndn::time::system_clock::now()
Ashlesh Gawande0d2c3822018-01-24 17:17:15 -0600320 + ndn::time::days(365)));
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500321 try {
322 m_keyChain.sign(certificate,
323 ndn::security::SigningInfo(m_keyChain.getPib().getIdentity(m_confParam.getRouterPrefix()))
324 .setSignatureInfo(signatureInfo));
akmhoque102aea42014-08-04 10:22:12 -0500325 }
dmcoomes9f936662017-03-02 10:33:09 -0600326 catch (const std::exception& e) {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500327 NLSR_LOG_WARN("ERROR: Router's " << e.what()
328 << "NLSR is running without security."
329 << " If security is enabled NLSR will not converge.");
330
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000331 std::cerr << "Router's " << e.what() << ". NLSR is running without security "
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500332 << "(Only for testing, should not be used in production.)"
333 << " If security is enabled NLSR will not converge." << std::endl;
akmhoque102aea42014-08-04 10:22:12 -0500334 }
akmhoque443ad812014-07-29 10:26:56 -0500335
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500336 m_signingInfo = ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_ID,
337 nlsrInstanceName);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700338
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700339 loadCertToPublish(certificate);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700340}
341
342void
343Nlsr::registerKeyPrefix()
344{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500345 // Start listening for the interest of this router's NLSR certificate
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600346 ndn::Name nlsrKeyPrefix = m_confParam.getRouterPrefix();
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500347 nlsrKeyPrefix.append("nlsr");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500348 nlsrKeyPrefix.append("KEY");
349
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600350 m_face.setInterestFilter(ndn::InterestFilter(nlsrKeyPrefix).allowLoopback(false),
351 std::bind(&Nlsr::onKeyInterest, this, _1, _2),
352 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
353 std::bind(&Nlsr::registrationFailed, this, _1),
354 m_signingInfo, ndn::nfd::ROUTE_FLAG_CAPTURE);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700355
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500356 // Start listening for the interest of this router's certificate
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600357 ndn::Name routerKeyPrefix = m_confParam.getRouterPrefix();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500358 routerKeyPrefix.append("KEY");
359
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600360 m_face.setInterestFilter(ndn::InterestFilter(routerKeyPrefix).allowLoopback(false),
361 std::bind(&Nlsr::onKeyInterest, this, _1, _2),
362 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
363 std::bind(&Nlsr::registrationFailed, this, _1),
364 m_signingInfo, ndn::nfd::ROUTE_FLAG_CAPTURE);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500365
366 // Start listening for the interest of this router's operator's certificate
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600367 ndn::Name operatorKeyPrefix = m_confParam.getNetwork();
368 operatorKeyPrefix.append(m_confParam.getSiteName());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500369 operatorKeyPrefix.append(std::string("%C1.Operator"));
370
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600371 m_face.setInterestFilter(ndn::InterestFilter(operatorKeyPrefix).allowLoopback(false),
372 std::bind(&Nlsr::onKeyInterest, this, _1, _2),
373 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
374 std::bind(&Nlsr::registrationFailed, this, _1),
375 m_signingInfo, ndn::nfd::ROUTE_FLAG_CAPTURE);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500376
377 // Start listening for the interest of this router's site's certificate
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600378 ndn::Name siteKeyPrefix = m_confParam.getNetwork();
379 siteKeyPrefix.append(m_confParam.getSiteName());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500380 siteKeyPrefix.append("KEY");
381
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600382 m_face.setInterestFilter(ndn::InterestFilter(siteKeyPrefix).allowLoopback(false),
383 std::bind(&Nlsr::onKeyInterest, this, _1, _2),
384 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
385 std::bind(&Nlsr::registrationFailed, this, _1),
386 m_signingInfo, ndn::nfd::ROUTE_FLAG_CAPTURE);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700387}
388
389void
alvy297f4162015-03-03 17:15:33 -0600390Nlsr::registerLocalhostPrefix()
391{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600392 m_face.registerPrefix(LOCALHOST_PREFIX,
393 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
394 std::bind(&Nlsr::registrationFailed, this, _1));
alvy297f4162015-03-03 17:15:33 -0600395}
396
397void
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500398Nlsr::registerRouterPrefix()
399{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600400 m_face.registerPrefix(ndn::Name(m_confParam.getRouterPrefix()).append("nlsr"),
401 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
402 std::bind(&Nlsr::registrationFailed, this, _1));
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500403}
404
405void
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700406Nlsr::onKeyInterest(const ndn::Name& name, const ndn::Interest& interest)
407{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500408 NLSR_LOG_DEBUG("Got interest for certificate. Interest: " << interest.getName());
409
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700410 const ndn::Name& interestName = interest.getName();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500411 const ndn::security::v2::Certificate* cert = getCertificate(interestName);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700412
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500413 if (cert == nullptr) {
414 NLSR_LOG_DEBUG("Certificate is not found for: " << interest);
dmcoomes9eaf3f42017-02-21 11:39:01 -0600415 return; // cert is not found
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500416 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700417
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600418 m_face.put(*cert);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700419}
420
421void
422Nlsr::onKeyPrefixRegSuccess(const ndn::Name& name)
423{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500424 NLSR_LOG_DEBUG("KEY prefix: " << name << " registration is successful.");
akmhoque53353462014-04-22 08:43:45 -0500425}
akmhoque5a44dd42014-03-12 18:11:32 -0500426
akmhoque53353462014-04-22 08:43:45 -0500427void
akmhoquec04e7272014-07-02 11:00:14 -0500428Nlsr::onFaceEventNotification(const ndn::nfd::FaceEventNotification& faceEventNotification)
akmhoquee1765152014-06-30 11:32:01 -0500429{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500430 NLSR_LOG_TRACE("Nlsr::onFaceEventNotification called");
Vince Lehman02e32992015-03-11 12:31:20 -0500431
Nick Gordond5c1a372016-10-31 13:56:23 -0500432 switch (faceEventNotification.getKind()) {
433 case ndn::nfd::FACE_EVENT_DESTROYED: {
434 uint64_t faceId = faceEventNotification.getFaceId();
Vince Lehman02e32992015-03-11 12:31:20 -0500435
Nick Gordond5c1a372016-10-31 13:56:23 -0500436 auto adjacent = m_adjacencyList.findAdjacent(faceId);
Vince Lehman02e32992015-03-11 12:31:20 -0500437
Nick Gordond5c1a372016-10-31 13:56:23 -0500438 if (adjacent != m_adjacencyList.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500439 NLSR_LOG_DEBUG("Face to " << adjacent->getName() << " with face id: " << faceId << " destroyed");
Vince Lehman02e32992015-03-11 12:31:20 -0500440
Nick Gordond5c1a372016-10-31 13:56:23 -0500441 adjacent->setFaceId(0);
Vince Lehman02e32992015-03-11 12:31:20 -0500442
Nick Gordond5c1a372016-10-31 13:56:23 -0500443 // Only trigger an Adjacency LSA build if this node is changing
444 // from ACTIVE to INACTIVE since this rebuild will effectively
445 // cancel the previous Adjacency LSA refresh event and schedule
446 // a new one further in the future.
447 //
448 // Continuously scheduling the refresh in the future will block
449 // the router from refreshing its Adjacency LSA. Since other
450 // routers' Name prefixes' expiration times are updated when
451 // this router refreshes its Adjacency LSA, the other routers'
452 // prefixes will expire and be removed from the RIB.
453 //
454 // This check is required to fix Bug #2733 for now. This check
455 // would be unnecessary to fix Bug #2733 when Issue #2732 is
456 // completed, but the check also helps with optimization so it
457 // can remain even when Issue #2732 is implemented.
458 if (adjacent->getStatus() == Adjacent::STATUS_ACTIVE) {
459 adjacent->setStatus(Adjacent::STATUS_INACTIVE);
Vince Lehman02e32992015-03-11 12:31:20 -0500460
Nick Gordond5c1a372016-10-31 13:56:23 -0500461 // A new adjacency LSA cannot be built until the neighbor is marked INACTIVE and
462 // has met the HELLO retry threshold
463 adjacent->setInterestTimedOutNo(m_confParam.getInterestRetryNumber());
Vince Lehman199e9cf2015-04-07 13:22:16 -0500464
Nick Gordond5c1a372016-10-31 13:56:23 -0500465 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600466 m_routingTable.scheduleRoutingTableCalculation();
Nick Gordond5c1a372016-10-31 13:56:23 -0500467 }
468 else {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600469 m_lsdb.scheduleAdjLsaBuild();
Nick Gordond5c1a372016-10-31 13:56:23 -0500470 }
Nick Gordone8e03ac2016-07-07 14:24:38 -0500471 }
Vince Lehman199e9cf2015-04-07 13:22:16 -0500472 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500473 break;
akmhoquec04e7272014-07-02 11:00:14 -0500474 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500475 case ndn::nfd::FACE_EVENT_CREATED: {
476 // Find the neighbor in our adjacency list
Ashlesh Gawande0d2c3822018-01-24 17:17:15 -0600477 ndn::FaceUri faceUri;
478 try {
479 faceUri = ndn::FaceUri(faceEventNotification.getRemoteUri());
480 }
481 catch (const std::exception& e) {
482 NLSR_LOG_WARN(e.what());
483 return;
484 }
485 auto adjacent = m_adjacencyList.findAdjacent(faceUri);
486
Nick Gordond5c1a372016-10-31 13:56:23 -0500487 // If we have a neighbor by that FaceUri and it has no FaceId, we
488 // have a match.
489 if (adjacent != m_adjacencyList.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500490 NLSR_LOG_DEBUG("Face creation event matches neighbor: " << adjacent->getName()
Nick Gordond5c1a372016-10-31 13:56:23 -0500491 << ". New Face ID: " << faceEventNotification.getFaceId()
492 << ". Registering prefixes.");
493 adjacent->setFaceId(faceEventNotification.getFaceId());
494
495 registerAdjacencyPrefixes(*adjacent, ndn::time::milliseconds::max());
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500496
497 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600498 m_routingTable.scheduleRoutingTableCalculation();
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500499 }
500 else {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600501 m_lsdb.scheduleAdjLsaBuild();
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500502 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500503 }
504 break;
505 }
506 default:
507 break;
akmhoquec04e7272014-07-02 11:00:14 -0500508 }
akmhoquee1765152014-06-30 11:32:01 -0500509}
510
Nick Gordond5c1a372016-10-31 13:56:23 -0500511void
512Nlsr::initializeFaces(const FetchDatasetCallback& onFetchSuccess,
513 const FetchDatasetTimeoutCallback& onFetchFailure)
514{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500515 NLSR_LOG_TRACE("Initializing Faces...");
Nick Gordond5c1a372016-10-31 13:56:23 -0500516
517 m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(onFetchSuccess, onFetchFailure);
518
519}
520
521void
522Nlsr::processFaceDataset(const std::vector<ndn::nfd::FaceStatus>& faces)
523{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500524 NLSR_LOG_DEBUG("Processing face dataset");
Nick Gordond5c1a372016-10-31 13:56:23 -0500525
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500526 // Iterate over each neighbor listed in nlsr.conf
527 for (auto& adjacent : m_adjacencyList.getAdjList()) {
528
529 const std::string faceUriString = adjacent.getFaceUri().toString();
Nick Gordond5c1a372016-10-31 13:56:23 -0500530 // Check the list of FaceStatus objects we got for a match
531 for (const ndn::nfd::FaceStatus& faceStatus : faces) {
Nick Gordond5c1a372016-10-31 13:56:23 -0500532 // Set the adjacency FaceID if we find a URI match and it was
533 // previously unset. Change the boolean to true.
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500534 if (adjacent.getFaceId() == 0 && faceUriString == faceStatus.getRemoteUri()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500535 NLSR_LOG_DEBUG("FaceUri: " << faceStatus.getRemoteUri() <<
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500536 " FaceId: "<< faceStatus.getFaceId());
537 adjacent.setFaceId(faceStatus.getFaceId());
Nick Gordond5c1a372016-10-31 13:56:23 -0500538 // Register the prefixes for each neighbor
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500539 this->registerAdjacencyPrefixes(adjacent, ndn::time::milliseconds::max());
Nick Gordond5c1a372016-10-31 13:56:23 -0500540 }
541 }
542 // If this adjacency has no information in this dataset, then one
543 // of two things is happening: 1. NFD is starting slowly and this
544 // Face wasn't ready yet, or 2. NFD is configured
545 // incorrectly and this Face isn't available.
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500546 if (adjacent.getFaceId() == 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500547 NLSR_LOG_WARN("The adjacency " << adjacent.getName() <<
Nick Gordond5c1a372016-10-31 13:56:23 -0500548 " has no Face information in this dataset.");
549 }
550 }
551
Nick Gordond5c1a372016-10-31 13:56:23 -0500552 scheduleDatasetFetch();
553}
554
555void
556Nlsr::registerAdjacencyPrefixes(const Adjacent& adj,
557 const ndn::time::milliseconds& timeout)
558{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500559 ndn::FaceUri faceUri = adj.getFaceUri();
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500560 double linkCost = adj.getLinkCost();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500561 const ndn::Name& adjName = adj.getName();
Nick Gordond5c1a372016-10-31 13:56:23 -0500562
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500563 m_fib.registerPrefix(adjName, faceUri, linkCost,
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500564 timeout, ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500565
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600566 m_fib.registerPrefix(m_confParam.getSyncPrefix(),
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500567 faceUri, linkCost, timeout,
568 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500569
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500570 m_fib.registerPrefix(m_confParam.getLsaPrefix(),
571 faceUri, linkCost, timeout,
572 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500573}
574
575void
576Nlsr::onFaceDatasetFetchTimeout(uint32_t code,
577 const std::string& msg,
578 uint32_t nRetriesSoFar)
579{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500580 NLSR_LOG_DEBUG("onFaceDatasetFetchTimeout");
Nick Gordond5c1a372016-10-31 13:56:23 -0500581 // If we have exceeded the maximum attempt count, do not try again.
582 if (nRetriesSoFar++ < m_confParam.getFaceDatasetFetchTries()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500583 NLSR_LOG_DEBUG("Failed to fetch dataset: " << msg << ". Attempting retry #" << nRetriesSoFar);
Nick Gordond5c1a372016-10-31 13:56:23 -0500584 m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(std::bind(&Nlsr::processFaceDataset,
585 this, _1),
586 std::bind(&Nlsr::onFaceDatasetFetchTimeout,
587 this, _1, _2, nRetriesSoFar));
588 }
589 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500590 NLSR_LOG_ERROR("Failed to fetch dataset: " << msg << ". Exceeded limit of " <<
Nick Gordond5c1a372016-10-31 13:56:23 -0500591 m_confParam.getFaceDatasetFetchTries() << ", so not trying again this time.");
592 // If we fail to fetch it, just do nothing until the next
593 // interval. Since this is a backup mechanism, we aren't as
594 // concerned with retrying.
595 scheduleDatasetFetch();
596 }
597}
598
599void
600Nlsr::scheduleDatasetFetch()
601{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500602 NLSR_LOG_DEBUG("Scheduling Dataset Fetch in " << m_confParam.getFaceDatasetFetchInterval());
603
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400604 m_scheduler.schedule(m_confParam.getFaceDatasetFetchInterval(),
Nick Gordond5c1a372016-10-31 13:56:23 -0500605 [this] {
606 this->initializeFaces(
607 [this] (const std::vector<ndn::nfd::FaceStatus>& faces) {
608 this->processFaceDataset(faces);
609 },
610 [this] (uint32_t code, const std::string& msg) {
611 this->onFaceDatasetFetchTimeout(code, msg, 0);
612 });
613 });
614}
akmhoquee1765152014-06-30 11:32:01 -0500615
616void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500617Nlsr::enableIncomingFaceIdIndication()
618{
619 NLSR_LOG_DEBUG("Enabling incoming face id indication for local face.");
620
621 m_controller.start<ndn::nfd::FaceUpdateCommand>(
622 ndn::nfd::ControlParameters()
623 .setFlagBit(ndn::nfd::FaceFlagBit::BIT_LOCAL_FIELDS_ENABLED, true),
624 bind(&Nlsr::onFaceIdIndicationSuccess, this, _1),
625 bind(&Nlsr::onFaceIdIndicationFailure, this, _1));
626}
627
628void
629Nlsr::onFaceIdIndicationSuccess(const ndn::nfd::ControlParameters& cp)
630{
631 NLSR_LOG_DEBUG("Successfully enabled incoming face id indication"
632 << "for face id " << cp.getFaceId());
633}
634
635void
636Nlsr::onFaceIdIndicationFailure(const ndn::nfd::ControlResponse& cr)
637{
638 std::ostringstream os;
639 os << "Failed to enable incoming face id indication feature: " <<
640 "(code: " << cr.getCode() << ", reason: " << cr.getText() << ")";
641
642 NLSR_LOG_DEBUG(os.str());
643}
644
akmhoqueb1710aa2014-02-19 17:13:36 -0600645} // namespace nlsr