blob: 3094e32038ddfc6854f593c1129f80cb94c48b56 [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)
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050053 , m_lsdb(m_face, m_keyChain, m_signingInfo, m_confParam, m_namePrefixTable, m_routingTable)
54 , m_helloProtocol(m_face, m_keyChain, m_signingInfo, confParam, m_routingTable, m_lsdb)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060055 , 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(
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050058 [this] (const ndn::Name& updateName, uint64_t sequenceNumber,
59 const ndn::Name& originRouter) {
60 registerStrategyForCerts(originRouter);
61 }))
62 , m_onPrefixRegistrationSuccess(m_fib.onPrefixRegistrationSuccess.connect(
63 [this] (const ndn::Name& name) {
64 m_helloProtocol.sendHelloInterest(name);
65 }))
66 , m_onHelloDataValidated(m_helloProtocol.onHelloDataValidated.connect(
67 [this] (const ndn::Name& neighbor) {
68 auto it = m_adjacencyList.findAdjacent(neighbor);
69 if (it != m_adjacencyList.end()) {
70 m_fib.registerPrefix(m_confParam.getSyncPrefix(), it->getFaceUri(), it->getLinkCost(),
71 ndn::time::milliseconds::max(), ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
72 }
73 }))
Ashlesh Gawande85998a12017-12-07 22:22:13 -060074 , m_dispatcher(m_face, m_keyChain)
75 , m_datasetHandler(m_dispatcher, m_lsdb, m_routingTable)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060076 , m_certStore(m_confParam.getCertStore())
77 , m_controller(m_face, m_keyChain)
78 , m_faceDatasetController(m_face, m_keyChain)
laqinfan35731852017-08-08 06:17:39 -050079 , m_prefixUpdateProcessor(m_dispatcher,
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050080 m_confParam.getPrefixUpdateValidator(),
81 m_namePrefixList,
82 m_lsdb,
83 m_confParam.getConfFileNameDynamic())
laqinfan35731852017-08-08 06:17:39 -050084 , m_nfdRibCommandProcessor(m_dispatcher,
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050085 m_namePrefixList,
86 m_lsdb)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060087 , m_statsCollector(m_lsdb, m_helloProtocol)
88 , m_faceMonitor(m_face)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050089{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050090 NLSR_LOG_DEBUG("Initializing Nlsr");
91
dmcoomes9f936662017-03-02 10:33:09 -060092 m_faceMonitor.onNotification.connect(std::bind(&Nlsr::onFaceEventNotification, this, _1));
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050093 m_faceMonitor.start();
Ashlesh Gawande85998a12017-12-07 22:22:13 -060094
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050095 // Do key initialization and registrations first, then initialize faces
96 // which will create routes to neighbor
97
Ashlesh Gawande85998a12017-12-07 22:22:13 -060098 setStrategies();
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050099
100 initializeKey();
101
102 NLSR_LOG_DEBUG("Default NLSR identity: " << m_signingInfo.getSignerName());
103
104 // Can be moved to HelloProtocol and Lsdb ctor if initializeKey is set
105 // earlier in the Nlsr constructor so as to set m_signingInfo
106 setInfoInterestFilter();
107 setLsaInterestFilter();
108
109 // add top-level prefixes: router and localhost prefix
110 addDispatcherTopPrefix(ndn::Name(m_confParam.getRouterPrefix()).append("nlsr"));
111 addDispatcherTopPrefix(LOCALHOST_PREFIX);
112
113 enableIncomingFaceIdIndication();
114
115 registerKeyPrefix();
116 registerLocalhostPrefix();
117 registerRouterPrefix();
118
119 initializeFaces(std::bind(&Nlsr::processFaceDataset, this, _1),
120 std::bind(&Nlsr::onFaceDatasetFetchTimeout, this, _1, _2, 0));
121
122 // Could be moved into ctor, but unit tests need to be modified
123 initialize();
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500124}
125
akmhoque53353462014-04-22 08:43:45 -0500126void
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -0500127Nlsr::registerStrategyForCerts(const ndn::Name& originRouter)
128{
129 for (const ndn::Name& router : m_strategySetOnRouters) {
130 if (router == originRouter) {
131 // Have already set strategy for this router's certs once
132 return;
133 }
134 }
135
136 m_strategySetOnRouters.push_back(originRouter);
137
138 ndn::Name routerKey(originRouter);
139 routerKey.append("KEY");
140 ndn::Name instanceKey(originRouter);
141 instanceKey.append("nlsr").append("KEY");
142
143 m_fib.setStrategy(routerKey, Fib::BEST_ROUTE_V2_STRATEGY, 0);
144 m_fib.setStrategy(instanceKey, Fib::BEST_ROUTE_V2_STRATEGY, 0);
145
146 ndn::Name siteKey;
147 for (size_t i = 0; i < originRouter.size(); ++i) {
148 if (originRouter[i].toUri() == "%C1.Router") {
149 break;
150 }
151 siteKey.append(originRouter[i]);
152 }
153 ndn::Name opPrefix(siteKey);
154 siteKey.append("KEY");
155 m_fib.setStrategy(siteKey, Fib::BEST_ROUTE_V2_STRATEGY, 0);
156
157 opPrefix.append(std::string("%C1.Operator"));
158 m_fib.setStrategy(opPrefix, Fib::BEST_ROUTE_V2_STRATEGY, 0);
159}
160
161void
akmhoque53353462014-04-22 08:43:45 -0500162Nlsr::registrationFailed(const ndn::Name& name)
akmhoque298385a2014-02-13 14:13:09 -0600163{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500164 NLSR_LOG_ERROR("ERROR: Failed to register prefix " << name << " in local hub's daemon");
dmcoomes9f936662017-03-02 10:33:09 -0600165 BOOST_THROW_EXCEPTION(Error("Error: Prefix registration failed"));
akmhoque53353462014-04-22 08:43:45 -0500166}
akmhoque1fd8c1e2014-02-19 19:41:49 -0600167
akmhoque157b0a42014-05-13 00:26:37 -0500168void
169Nlsr::onRegistrationSuccess(const ndn::Name& name)
170{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500171 NLSR_LOG_DEBUG("Successfully registered prefix: " << name);
alvy297f4162015-03-03 17:15:33 -0600172}
173
174void
akmhoque31d1d4b2014-05-05 22:08:14 -0500175Nlsr::setInfoInterestFilter()
akmhoque53353462014-04-22 08:43:45 -0500176{
akmhoque31d1d4b2014-05-05 22:08:14 -0500177 ndn::Name name(m_confParam.getRouterPrefix());
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500178 name.append("nlsr");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500179 name.append("INFO");
180
181 NLSR_LOG_DEBUG("Setting interest filter for Hello interest: " << name);
182
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600183 m_face.setInterestFilter(ndn::InterestFilter(name).allowLoopback(false),
184 std::bind(&HelloProtocol::processInterest, &m_helloProtocol, _1, _2),
185 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
186 std::bind(&Nlsr::registrationFailed, this, _1),
187 m_signingInfo, ndn::nfd::ROUTE_FLAG_CAPTURE);
akmhoque31d1d4b2014-05-05 22:08:14 -0500188}
189
190void
191Nlsr::setLsaInterestFilter()
192{
akmhoque157b0a42014-05-13 00:26:37 -0500193 ndn::Name name = m_confParam.getLsaPrefix();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500194
dmcoomes5bcb39e2017-10-31 15:07:55 -0500195 NLSR_LOG_DEBUG("Setting interest filter for LsaPrefix: " << name);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500196
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600197 m_face.setInterestFilter(ndn::InterestFilter(name).allowLoopback(false),
198 std::bind(&Lsdb::processInterest, &m_lsdb, _1, _2),
199 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
200 std::bind(&Nlsr::registrationFailed, this, _1),
201 m_signingInfo, ndn::nfd::ROUTE_FLAG_CAPTURE);
akmhoque53353462014-04-22 08:43:45 -0500202}
203
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500204void
205Nlsr::addDispatcherTopPrefix(const ndn::Name& topPrefix)
206{
207 try {
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500208 // false since we want to have control over the registration process
laqinfan35731852017-08-08 06:17:39 -0500209 m_dispatcher.addTopPrefix(topPrefix, false, m_signingInfo);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500210 }
211 catch (const std::exception& e) {
212 NLSR_LOG_ERROR("Error setting top-level prefix in dispatcher: " << e.what() << "\n");
213 }
214}
215
akmhoque53353462014-04-22 08:43:45 -0500216void
akmhoquec04e7272014-07-02 11:00:14 -0500217Nlsr::setStrategies()
akmhoque157b0a42014-05-13 00:26:37 -0500218{
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -0500219 m_fib.setStrategy(m_confParam.getLsaPrefix(), Fib::MULTICAST_STRATEGY, 0);
220 m_fib.setStrategy(m_confParam.getSyncPrefix(), Fib::MULTICAST_STRATEGY, 0);
akmhoque157b0a42014-05-13 00:26:37 -0500221}
222
223void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500224Nlsr::loadCertToPublish(const ndn::security::v2::Certificate& certificate)
225{
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000226 NLSR_LOG_TRACE("Loading cert to publish.");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500227 m_certStore.insert(certificate);
228 m_validator.loadAnchor("Authoritative-Certificate",
229 ndn::security::v2::Certificate(certificate));
230 m_prefixUpdateProcessor.getValidator().
231 loadAnchor("Authoritative-Certificate",
232 ndn::security::v2::Certificate(certificate));
233}
234
Nick Gordon9461afb2017-04-25 15:54:50 -0500235void
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000236Nlsr::afterFetcherSignalEmitted(const ndn::Data& lsaSegment)
237{
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000238 ndn::Name keyName = lsaSegment.getSignature().getKeyLocator().getName();
239 if (getCertificate(keyName) == nullptr) {
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -0500240 NLSR_LOG_TRACE("Publishing certificate for: " << keyName);
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000241 publishCertFromCache(keyName);
242 }
243 else {
244 NLSR_LOG_TRACE("Certificate is already in the store: " << keyName);
245 }
246}
247
248void
249Nlsr::publishCertFromCache(const ndn::Name& keyName)
250{
251 const ndn::security::v2::Certificate* cert = m_validator.getUnverifiedCertCache()
252 .find(keyName);
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -0500253
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000254 if (cert != nullptr) {
255 m_certStore.insert(*cert);
256 NLSR_LOG_TRACE(*cert);
Ashlesh Gawande3494f732018-11-06 16:04:03 -0600257 ndn::Name certName = ndn::security::v2::extractKeyNameFromCertName(cert->getName());
258 NLSR_LOG_TRACE("Setting interest filter for: " << certName);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600259 m_face.setInterestFilter(ndn::InterestFilter(certName).allowLoopback(false),
260 std::bind(&Nlsr::onKeyInterest, this, _1, _2),
261 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
262 std::bind(&Nlsr::registrationFailed, this, _1),
263 m_signingInfo, ndn::nfd::ROUTE_FLAG_CAPTURE);
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000264
265 if (!cert->getKeyName().equals(cert->getSignature().getKeyLocator().getName())) {
266 publishCertFromCache(cert->getSignature().getKeyLocator().getName());
267 }
268 }
269 else {
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -0500270 // Happens for root cert
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000271 NLSR_LOG_TRACE("Cert for " << keyName << " was not found in the Validator's cache. ");
272 }
273}
274
275void
akmhoque53353462014-04-22 08:43:45 -0500276Nlsr::initialize()
277{
dmcoomes9f936662017-03-02 10:33:09 -0600278 // Logging start
akmhoque674b0b12014-05-20 14:33:28 -0500279 m_adjacencyList.writeLog();
dmcoomes5bcb39e2017-10-31 15:07:55 -0500280 NLSR_LOG_DEBUG(m_namePrefixList);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500281
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600282 m_lsdb.buildAndInstallOwnNameLsa();
Nick Gordon5c467f02016-07-13 13:40:10 -0500283
284 // Install coordinate LSAs if using HR or dry-run HR.
285 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600286 m_lsdb.buildAndInstallOwnCoordinateLsa();
Nick Gordon5c467f02016-07-13 13:40:10 -0500287 }
Vince Lehman904c2412014-09-23 19:36:11 -0500288
Vince Lehman09131122014-09-09 17:10:11 -0500289 // Need to set direct neighbors' costs to 0 for hyperbolic routing
290 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500291 for (auto&& neighbor : m_adjacencyList.getAdjList()) {
292 neighbor.setLinkCost(0);
Vince Lehman09131122014-09-09 17:10:11 -0500293 }
294 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700295}
296
297void
akmhoque443ad812014-07-29 10:26:56 -0500298Nlsr::initializeKey()
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700299{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500300 NLSR_LOG_DEBUG("Initializing Key ...");
301
302 ndn::Name nlsrInstanceName = m_confParam.getRouterPrefix();
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500303 nlsrInstanceName.append("nlsr");
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700304
Joao Pereira97473d42015-07-03 16:57:27 -0400305 try {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500306 m_keyChain.deleteIdentity(m_keyChain.getPib().getIdentity(nlsrInstanceName));
Ashlesh Gawande842b1cf2019-10-03 22:16:56 -0500307 }
308 catch (const std::exception& e) {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500309 NLSR_LOG_WARN(e.what());
310 }
311
Ashlesh Gawande842b1cf2019-10-03 22:16:56 -0500312 ndn::security::Identity nlsrInstanceIdentity;
313 try {
314 nlsrInstanceIdentity = m_keyChain.createIdentity(nlsrInstanceName);
315 }
316 catch (const std::exception& e) {
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500317 NLSR_LOG_ERROR(e.what());
Ashlesh Gawande842b1cf2019-10-03 22:16:56 -0500318 NLSR_LOG_ERROR("Unable to create identity, NLSR will run without security!");
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500319 NLSR_LOG_ERROR("Can be ignored if running in non-production environments.");
Ashlesh Gawande842b1cf2019-10-03 22:16:56 -0500320 return;
321 }
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500322 auto nlsrInstanceKey = nlsrInstanceIdentity.getDefaultKey();
323
324 ndn::security::v2::Certificate certificate;
325
326 ndn::Name certificateName = nlsrInstanceKey.getName();
327 certificateName.append("NA");
328 certificateName.appendVersion();
329 certificate.setName(certificateName);
330
331 // set metainfo
332 certificate.setContentType(ndn::tlv::ContentType_Key);
Ashlesh Gawande0d2c3822018-01-24 17:17:15 -0600333 certificate.setFreshnessPeriod(ndn::time::days(365));
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500334
335 // set content
336 certificate.setContent(nlsrInstanceKey.getPublicKey().data(), nlsrInstanceKey.getPublicKey().size());
337
338 // set signature-info
339 ndn::SignatureInfo signatureInfo;
340 signatureInfo.setValidityPeriod(ndn::security::ValidityPeriod(ndn::time::system_clock::TimePoint(),
341 ndn::time::system_clock::now()
Ashlesh Gawande0d2c3822018-01-24 17:17:15 -0600342 + ndn::time::days(365)));
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500343 try {
344 m_keyChain.sign(certificate,
345 ndn::security::SigningInfo(m_keyChain.getPib().getIdentity(m_confParam.getRouterPrefix()))
346 .setSignatureInfo(signatureInfo));
akmhoque102aea42014-08-04 10:22:12 -0500347 }
dmcoomes9f936662017-03-02 10:33:09 -0600348 catch (const std::exception& e) {
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500349 NLSR_LOG_ERROR("Router's " << e.what() << "NLSR is running without security." <<
350 " If security is enabled NLSR will not converge.");
akmhoque102aea42014-08-04 10:22:12 -0500351 }
akmhoque443ad812014-07-29 10:26:56 -0500352
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500353 m_signingInfo = ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_ID,
354 nlsrInstanceName);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700355
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700356 loadCertToPublish(certificate);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700357}
358
359void
360Nlsr::registerKeyPrefix()
361{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500362 // Start listening for the interest of this router's NLSR certificate
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600363 ndn::Name nlsrKeyPrefix = m_confParam.getRouterPrefix();
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500364 nlsrKeyPrefix.append("nlsr");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500365 nlsrKeyPrefix.append("KEY");
366
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600367 m_face.setInterestFilter(ndn::InterestFilter(nlsrKeyPrefix).allowLoopback(false),
368 std::bind(&Nlsr::onKeyInterest, this, _1, _2),
369 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
370 std::bind(&Nlsr::registrationFailed, this, _1),
371 m_signingInfo, ndn::nfd::ROUTE_FLAG_CAPTURE);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700372
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500373 // Start listening for the interest of this router's certificate
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600374 ndn::Name routerKeyPrefix = m_confParam.getRouterPrefix();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500375 routerKeyPrefix.append("KEY");
376
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600377 m_face.setInterestFilter(ndn::InterestFilter(routerKeyPrefix).allowLoopback(false),
378 std::bind(&Nlsr::onKeyInterest, this, _1, _2),
379 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
380 std::bind(&Nlsr::registrationFailed, this, _1),
381 m_signingInfo, ndn::nfd::ROUTE_FLAG_CAPTURE);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500382
383 // Start listening for the interest of this router's operator's certificate
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600384 ndn::Name operatorKeyPrefix = m_confParam.getNetwork();
385 operatorKeyPrefix.append(m_confParam.getSiteName());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500386 operatorKeyPrefix.append(std::string("%C1.Operator"));
387
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600388 m_face.setInterestFilter(ndn::InterestFilter(operatorKeyPrefix).allowLoopback(false),
389 std::bind(&Nlsr::onKeyInterest, this, _1, _2),
390 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
391 std::bind(&Nlsr::registrationFailed, this, _1),
392 m_signingInfo, ndn::nfd::ROUTE_FLAG_CAPTURE);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500393
394 // Start listening for the interest of this router's site's certificate
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600395 ndn::Name siteKeyPrefix = m_confParam.getNetwork();
396 siteKeyPrefix.append(m_confParam.getSiteName());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500397 siteKeyPrefix.append("KEY");
398
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600399 m_face.setInterestFilter(ndn::InterestFilter(siteKeyPrefix).allowLoopback(false),
400 std::bind(&Nlsr::onKeyInterest, this, _1, _2),
401 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
402 std::bind(&Nlsr::registrationFailed, this, _1),
403 m_signingInfo, ndn::nfd::ROUTE_FLAG_CAPTURE);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700404}
405
406void
alvy297f4162015-03-03 17:15:33 -0600407Nlsr::registerLocalhostPrefix()
408{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600409 m_face.registerPrefix(LOCALHOST_PREFIX,
410 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
411 std::bind(&Nlsr::registrationFailed, this, _1));
alvy297f4162015-03-03 17:15:33 -0600412}
413
414void
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500415Nlsr::registerRouterPrefix()
416{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600417 m_face.registerPrefix(ndn::Name(m_confParam.getRouterPrefix()).append("nlsr"),
418 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
419 std::bind(&Nlsr::registrationFailed, this, _1));
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500420}
421
422void
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700423Nlsr::onKeyInterest(const ndn::Name& name, const ndn::Interest& interest)
424{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500425 NLSR_LOG_DEBUG("Got interest for certificate. Interest: " << interest.getName());
426
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700427 const ndn::Name& interestName = interest.getName();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500428 const ndn::security::v2::Certificate* cert = getCertificate(interestName);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700429
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500430 if (cert == nullptr) {
431 NLSR_LOG_DEBUG("Certificate is not found for: " << interest);
dmcoomes9eaf3f42017-02-21 11:39:01 -0600432 return; // cert is not found
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500433 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700434
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600435 m_face.put(*cert);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700436}
437
438void
439Nlsr::onKeyPrefixRegSuccess(const ndn::Name& name)
440{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500441 NLSR_LOG_DEBUG("KEY prefix: " << name << " registration is successful.");
akmhoque53353462014-04-22 08:43:45 -0500442}
akmhoque5a44dd42014-03-12 18:11:32 -0500443
akmhoque53353462014-04-22 08:43:45 -0500444void
akmhoquec04e7272014-07-02 11:00:14 -0500445Nlsr::onFaceEventNotification(const ndn::nfd::FaceEventNotification& faceEventNotification)
akmhoquee1765152014-06-30 11:32:01 -0500446{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500447 NLSR_LOG_TRACE("Nlsr::onFaceEventNotification called");
Vince Lehman02e32992015-03-11 12:31:20 -0500448
Nick Gordond5c1a372016-10-31 13:56:23 -0500449 switch (faceEventNotification.getKind()) {
450 case ndn::nfd::FACE_EVENT_DESTROYED: {
451 uint64_t faceId = faceEventNotification.getFaceId();
Vince Lehman02e32992015-03-11 12:31:20 -0500452
Nick Gordond5c1a372016-10-31 13:56:23 -0500453 auto adjacent = m_adjacencyList.findAdjacent(faceId);
Vince Lehman02e32992015-03-11 12:31:20 -0500454
Nick Gordond5c1a372016-10-31 13:56:23 -0500455 if (adjacent != m_adjacencyList.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500456 NLSR_LOG_DEBUG("Face to " << adjacent->getName() << " with face id: " << faceId << " destroyed");
Vince Lehman02e32992015-03-11 12:31:20 -0500457
Nick Gordond5c1a372016-10-31 13:56:23 -0500458 adjacent->setFaceId(0);
Vince Lehman02e32992015-03-11 12:31:20 -0500459
Nick Gordond5c1a372016-10-31 13:56:23 -0500460 // Only trigger an Adjacency LSA build if this node is changing
461 // from ACTIVE to INACTIVE since this rebuild will effectively
462 // cancel the previous Adjacency LSA refresh event and schedule
463 // a new one further in the future.
464 //
465 // Continuously scheduling the refresh in the future will block
466 // the router from refreshing its Adjacency LSA. Since other
467 // routers' Name prefixes' expiration times are updated when
468 // this router refreshes its Adjacency LSA, the other routers'
469 // prefixes will expire and be removed from the RIB.
470 //
471 // This check is required to fix Bug #2733 for now. This check
472 // would be unnecessary to fix Bug #2733 when Issue #2732 is
473 // completed, but the check also helps with optimization so it
474 // can remain even when Issue #2732 is implemented.
475 if (adjacent->getStatus() == Adjacent::STATUS_ACTIVE) {
476 adjacent->setStatus(Adjacent::STATUS_INACTIVE);
Vince Lehman02e32992015-03-11 12:31:20 -0500477
Nick Gordond5c1a372016-10-31 13:56:23 -0500478 // A new adjacency LSA cannot be built until the neighbor is marked INACTIVE and
479 // has met the HELLO retry threshold
480 adjacent->setInterestTimedOutNo(m_confParam.getInterestRetryNumber());
Vince Lehman199e9cf2015-04-07 13:22:16 -0500481
Nick Gordond5c1a372016-10-31 13:56:23 -0500482 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600483 m_routingTable.scheduleRoutingTableCalculation();
Nick Gordond5c1a372016-10-31 13:56:23 -0500484 }
485 else {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600486 m_lsdb.scheduleAdjLsaBuild();
Nick Gordond5c1a372016-10-31 13:56:23 -0500487 }
Nick Gordone8e03ac2016-07-07 14:24:38 -0500488 }
Vince Lehman199e9cf2015-04-07 13:22:16 -0500489 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500490 break;
akmhoquec04e7272014-07-02 11:00:14 -0500491 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500492 case ndn::nfd::FACE_EVENT_CREATED: {
493 // Find the neighbor in our adjacency list
Ashlesh Gawande0d2c3822018-01-24 17:17:15 -0600494 ndn::FaceUri faceUri;
495 try {
496 faceUri = ndn::FaceUri(faceEventNotification.getRemoteUri());
497 }
498 catch (const std::exception& e) {
499 NLSR_LOG_WARN(e.what());
500 return;
501 }
502 auto adjacent = m_adjacencyList.findAdjacent(faceUri);
Ashlesh Gawande41878572019-09-29 00:16:02 -0500503 uint64_t faceId = faceEventNotification.getFaceId();
Ashlesh Gawande0d2c3822018-01-24 17:17:15 -0600504
Ashlesh Gawande41878572019-09-29 00:16:02 -0500505 // If we have a neighbor by that FaceUri and it has no FaceId or
506 // the FaceId is different from ours, we have a match.
507 if (adjacent != m_adjacencyList.end() &&
508 (adjacent->getFaceId() == 0 || adjacent->getFaceId() != faceId))
509 {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500510 NLSR_LOG_DEBUG("Face creation event matches neighbor: " << adjacent->getName()
Ashlesh Gawande41878572019-09-29 00:16:02 -0500511 << ". New Face ID: " << faceId << ". Registering prefixes.");
512 adjacent->setFaceId(faceId);
Nick Gordond5c1a372016-10-31 13:56:23 -0500513
514 registerAdjacencyPrefixes(*adjacent, ndn::time::milliseconds::max());
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500515
516 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600517 m_routingTable.scheduleRoutingTableCalculation();
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500518 }
519 else {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600520 m_lsdb.scheduleAdjLsaBuild();
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500521 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500522 }
523 break;
524 }
525 default:
526 break;
akmhoquec04e7272014-07-02 11:00:14 -0500527 }
akmhoquee1765152014-06-30 11:32:01 -0500528}
529
Nick Gordond5c1a372016-10-31 13:56:23 -0500530void
531Nlsr::initializeFaces(const FetchDatasetCallback& onFetchSuccess,
532 const FetchDatasetTimeoutCallback& onFetchFailure)
533{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500534 NLSR_LOG_TRACE("Initializing Faces...");
Nick Gordond5c1a372016-10-31 13:56:23 -0500535
536 m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(onFetchSuccess, onFetchFailure);
537
538}
539
540void
541Nlsr::processFaceDataset(const std::vector<ndn::nfd::FaceStatus>& faces)
542{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500543 NLSR_LOG_DEBUG("Processing face dataset");
Nick Gordond5c1a372016-10-31 13:56:23 -0500544
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500545 // Iterate over each neighbor listed in nlsr.conf
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500546 for (auto&& adjacent : m_adjacencyList.getAdjList()) {
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500547
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500548 const std::string& faceUriString = adjacent.getFaceUri().toString();
Nick Gordond5c1a372016-10-31 13:56:23 -0500549 // Check the list of FaceStatus objects we got for a match
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500550 for (const auto& faceStatus : faces) {
Nick Gordond5c1a372016-10-31 13:56:23 -0500551 // Set the adjacency FaceID if we find a URI match and it was
552 // previously unset. Change the boolean to true.
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500553 if (adjacent.getFaceId() == 0 && faceUriString == faceStatus.getRemoteUri()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500554 NLSR_LOG_DEBUG("FaceUri: " << faceStatus.getRemoteUri() <<
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500555 " FaceId: "<< faceStatus.getFaceId());
556 adjacent.setFaceId(faceStatus.getFaceId());
Nick Gordond5c1a372016-10-31 13:56:23 -0500557 // Register the prefixes for each neighbor
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500558 this->registerAdjacencyPrefixes(adjacent, ndn::time::milliseconds::max());
Nick Gordond5c1a372016-10-31 13:56:23 -0500559 }
560 }
561 // If this adjacency has no information in this dataset, then one
562 // of two things is happening: 1. NFD is starting slowly and this
563 // Face wasn't ready yet, or 2. NFD is configured
564 // incorrectly and this Face isn't available.
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500565 if (adjacent.getFaceId() == 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500566 NLSR_LOG_WARN("The adjacency " << adjacent.getName() <<
Nick Gordond5c1a372016-10-31 13:56:23 -0500567 " has no Face information in this dataset.");
568 }
569 }
570
Nick Gordond5c1a372016-10-31 13:56:23 -0500571 scheduleDatasetFetch();
572}
573
574void
575Nlsr::registerAdjacencyPrefixes(const Adjacent& adj,
576 const ndn::time::milliseconds& timeout)
577{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500578 ndn::FaceUri faceUri = adj.getFaceUri();
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500579 double linkCost = adj.getLinkCost();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500580 const ndn::Name& adjName = adj.getName();
Nick Gordond5c1a372016-10-31 13:56:23 -0500581
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500582 m_fib.registerPrefix(adjName, faceUri, linkCost,
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500583 timeout, ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500584
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500585 m_fib.registerPrefix(m_confParam.getLsaPrefix(),
586 faceUri, linkCost, timeout,
587 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500588}
589
590void
591Nlsr::onFaceDatasetFetchTimeout(uint32_t code,
592 const std::string& msg,
593 uint32_t nRetriesSoFar)
594{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500595 NLSR_LOG_DEBUG("onFaceDatasetFetchTimeout");
Nick Gordond5c1a372016-10-31 13:56:23 -0500596 // If we have exceeded the maximum attempt count, do not try again.
597 if (nRetriesSoFar++ < m_confParam.getFaceDatasetFetchTries()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500598 NLSR_LOG_DEBUG("Failed to fetch dataset: " << msg << ". Attempting retry #" << nRetriesSoFar);
Nick Gordond5c1a372016-10-31 13:56:23 -0500599 m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(std::bind(&Nlsr::processFaceDataset,
600 this, _1),
601 std::bind(&Nlsr::onFaceDatasetFetchTimeout,
602 this, _1, _2, nRetriesSoFar));
603 }
604 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500605 NLSR_LOG_ERROR("Failed to fetch dataset: " << msg << ". Exceeded limit of " <<
Nick Gordond5c1a372016-10-31 13:56:23 -0500606 m_confParam.getFaceDatasetFetchTries() << ", so not trying again this time.");
607 // If we fail to fetch it, just do nothing until the next
608 // interval. Since this is a backup mechanism, we aren't as
609 // concerned with retrying.
610 scheduleDatasetFetch();
611 }
612}
613
614void
615Nlsr::scheduleDatasetFetch()
616{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500617 NLSR_LOG_DEBUG("Scheduling Dataset Fetch in " << m_confParam.getFaceDatasetFetchInterval());
618
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400619 m_scheduler.schedule(m_confParam.getFaceDatasetFetchInterval(),
Nick Gordond5c1a372016-10-31 13:56:23 -0500620 [this] {
621 this->initializeFaces(
622 [this] (const std::vector<ndn::nfd::FaceStatus>& faces) {
623 this->processFaceDataset(faces);
624 },
625 [this] (uint32_t code, const std::string& msg) {
626 this->onFaceDatasetFetchTimeout(code, msg, 0);
627 });
628 });
629}
akmhoquee1765152014-06-30 11:32:01 -0500630
631void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500632Nlsr::enableIncomingFaceIdIndication()
633{
634 NLSR_LOG_DEBUG("Enabling incoming face id indication for local face.");
635
636 m_controller.start<ndn::nfd::FaceUpdateCommand>(
637 ndn::nfd::ControlParameters()
638 .setFlagBit(ndn::nfd::FaceFlagBit::BIT_LOCAL_FIELDS_ENABLED, true),
639 bind(&Nlsr::onFaceIdIndicationSuccess, this, _1),
640 bind(&Nlsr::onFaceIdIndicationFailure, this, _1));
641}
642
643void
644Nlsr::onFaceIdIndicationSuccess(const ndn::nfd::ControlParameters& cp)
645{
646 NLSR_LOG_DEBUG("Successfully enabled incoming face id indication"
647 << "for face id " << cp.getFaceId());
648}
649
650void
651Nlsr::onFaceIdIndicationFailure(const ndn::nfd::ControlResponse& cr)
652{
653 std::ostringstream os;
654 os << "Failed to enable incoming face id indication feature: " <<
655 "(code: " << cr.getCode() << ", reason: " << cr.getText() << ")";
656
657 NLSR_LOG_DEBUG(os.str());
658}
659
akmhoqueb1710aa2014-02-19 17:13:36 -0600660} // namespace nlsr