blob: 78e6d0c537e16ed23967e8fdaf35f623f9cd7adb [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
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)))
57 , m_dispatcher(m_face, m_keyChain)
58 , m_datasetHandler(m_dispatcher, m_lsdb, m_routingTable)
59 , m_helloProtocol(m_face, m_keyChain, m_signingInfo, confParam, m_routingTable, m_lsdb)
60 , m_certStore(m_confParam.getCertStore())
61 , m_controller(m_face, m_keyChain)
62 , m_faceDatasetController(m_face, m_keyChain)
laqinfan35731852017-08-08 06:17:39 -050063 , m_prefixUpdateProcessor(m_dispatcher,
Ashlesh Gawande85998a12017-12-07 22:22:13 -060064 m_confParam.getPrefixUpdateValidator(),
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050065 m_namePrefixList,
Ashlesh Gawande85998a12017-12-07 22:22:13 -060066 m_lsdb,
dulalsaurab82a34c22019-02-04 17:31:21 +000067 m_confParam.getConfFileNameDynamic())
laqinfan35731852017-08-08 06:17:39 -050068 , m_nfdRibCommandProcessor(m_dispatcher,
Nick Gordon4d2c6c02017-01-20 13:18:46 -060069 m_namePrefixList,
Ashlesh Gawande85998a12017-12-07 22:22:13 -060070 m_lsdb)
71 , m_statsCollector(m_lsdb, m_helloProtocol)
72 , m_faceMonitor(m_face)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050073{
dmcoomes9f936662017-03-02 10:33:09 -060074 m_faceMonitor.onNotification.connect(std::bind(&Nlsr::onFaceEventNotification, this, _1));
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050075 m_faceMonitor.start();
Ashlesh Gawande85998a12017-12-07 22:22:13 -060076
77 setStrategies();
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050078}
79
akmhoque53353462014-04-22 08:43:45 -050080void
81Nlsr::registrationFailed(const ndn::Name& name)
akmhoque298385a2014-02-13 14:13:09 -060082{
dmcoomes5bcb39e2017-10-31 15:07:55 -050083 NLSR_LOG_ERROR("ERROR: Failed to register prefix in local hub's daemon");
dmcoomes9f936662017-03-02 10:33:09 -060084 BOOST_THROW_EXCEPTION(Error("Error: Prefix registration failed"));
akmhoque53353462014-04-22 08:43:45 -050085}
akmhoque1fd8c1e2014-02-19 19:41:49 -060086
akmhoque157b0a42014-05-13 00:26:37 -050087void
88Nlsr::onRegistrationSuccess(const ndn::Name& name)
89{
dmcoomes5bcb39e2017-10-31 15:07:55 -050090 NLSR_LOG_DEBUG("Successfully registered prefix: " << name);
alvy297f4162015-03-03 17:15:33 -060091}
92
93void
akmhoque31d1d4b2014-05-05 22:08:14 -050094Nlsr::setInfoInterestFilter()
akmhoque53353462014-04-22 08:43:45 -050095{
akmhoque31d1d4b2014-05-05 22:08:14 -050096 ndn::Name name(m_confParam.getRouterPrefix());
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -050097 name.append("nlsr");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050098 name.append("INFO");
99
100 NLSR_LOG_DEBUG("Setting interest filter for Hello interest: " << name);
101
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600102 m_face.setInterestFilter(ndn::InterestFilter(name).allowLoopback(false),
103 std::bind(&HelloProtocol::processInterest, &m_helloProtocol, _1, _2),
104 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
105 std::bind(&Nlsr::registrationFailed, this, _1),
106 m_signingInfo, ndn::nfd::ROUTE_FLAG_CAPTURE);
akmhoque31d1d4b2014-05-05 22:08:14 -0500107}
108
109void
110Nlsr::setLsaInterestFilter()
111{
akmhoque157b0a42014-05-13 00:26:37 -0500112 ndn::Name name = m_confParam.getLsaPrefix();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500113
dmcoomes5bcb39e2017-10-31 15:07:55 -0500114 NLSR_LOG_DEBUG("Setting interest filter for LsaPrefix: " << name);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500115
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600116 m_face.setInterestFilter(ndn::InterestFilter(name).allowLoopback(false),
117 std::bind(&Lsdb::processInterest, &m_lsdb, _1, _2),
118 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
119 std::bind(&Nlsr::registrationFailed, this, _1),
120 m_signingInfo, ndn::nfd::ROUTE_FLAG_CAPTURE);
akmhoque53353462014-04-22 08:43:45 -0500121}
122
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500123void
124Nlsr::addDispatcherTopPrefix(const ndn::Name& topPrefix)
125{
126 try {
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500127 // false since we want to have control over the registration process
laqinfan35731852017-08-08 06:17:39 -0500128 m_dispatcher.addTopPrefix(topPrefix, false, m_signingInfo);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500129 }
130 catch (const std::exception& e) {
131 NLSR_LOG_ERROR("Error setting top-level prefix in dispatcher: " << e.what() << "\n");
132 }
133}
134
akmhoque53353462014-04-22 08:43:45 -0500135void
akmhoquec04e7272014-07-02 11:00:14 -0500136Nlsr::setStrategies()
akmhoque157b0a42014-05-13 00:26:37 -0500137{
Vince Lehman53c0e3e2015-09-14 14:33:20 -0500138 const std::string strategy("ndn:/localhost/nfd/strategy/multicast");
139
akmhoque393d4ff2014-07-16 14:27:03 -0500140 m_fib.setStrategy(m_confParam.getLsaPrefix(), strategy, 0);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600141 m_fib.setStrategy(m_confParam.getSyncPrefix(), strategy, 0);
akmhoque157b0a42014-05-13 00:26:37 -0500142}
143
144void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500145Nlsr::loadCertToPublish(const ndn::security::v2::Certificate& certificate)
146{
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000147 NLSR_LOG_TRACE("Loading cert to publish.");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500148 m_certStore.insert(certificate);
149 m_validator.loadAnchor("Authoritative-Certificate",
150 ndn::security::v2::Certificate(certificate));
151 m_prefixUpdateProcessor.getValidator().
152 loadAnchor("Authoritative-Certificate",
153 ndn::security::v2::Certificate(certificate));
154}
155
Nick Gordon9461afb2017-04-25 15:54:50 -0500156void
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000157Nlsr::afterFetcherSignalEmitted(const ndn::Data& lsaSegment)
158{
159 NLSR_LOG_TRACE("SegmentFetcher fetched a data segment. Start inserting cert to own cert store.");
160 ndn::Name keyName = lsaSegment.getSignature().getKeyLocator().getName();
161 if (getCertificate(keyName) == nullptr) {
162 publishCertFromCache(keyName);
163 }
164 else {
165 NLSR_LOG_TRACE("Certificate is already in the store: " << keyName);
166 }
167}
168
169void
170Nlsr::publishCertFromCache(const ndn::Name& keyName)
171{
172 const ndn::security::v2::Certificate* cert = m_validator.getUnverifiedCertCache()
173 .find(keyName);
174 if (cert != nullptr) {
175 m_certStore.insert(*cert);
176 NLSR_LOG_TRACE(*cert);
Ashlesh Gawande3494f732018-11-06 16:04:03 -0600177 ndn::Name certName = ndn::security::v2::extractKeyNameFromCertName(cert->getName());
178 NLSR_LOG_TRACE("Setting interest filter for: " << certName);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600179 m_face.setInterestFilter(ndn::InterestFilter(certName).allowLoopback(false),
180 std::bind(&Nlsr::onKeyInterest, this, _1, _2),
181 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
182 std::bind(&Nlsr::registrationFailed, this, _1),
183 m_signingInfo, ndn::nfd::ROUTE_FLAG_CAPTURE);
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000184
185 if (!cert->getKeyName().equals(cert->getSignature().getKeyLocator().getName())) {
186 publishCertFromCache(cert->getSignature().getKeyLocator().getName());
187 }
188 }
189 else {
190 NLSR_LOG_TRACE("Cert for " << keyName << " was not found in the Validator's cache. ");
191 }
192}
193
194void
akmhoque53353462014-04-22 08:43:45 -0500195Nlsr::initialize()
196{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500197 NLSR_LOG_DEBUG("Initializing Nlsr");
Vince Lehmanc11cc202015-01-20 11:41:33 -0600198
dmcoomes9f936662017-03-02 10:33:09 -0600199 // Logging start
akmhoque674b0b12014-05-20 14:33:28 -0500200 m_adjacencyList.writeLog();
dmcoomes5bcb39e2017-10-31 15:07:55 -0500201 NLSR_LOG_DEBUG(m_namePrefixList);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500202
akmhoque443ad812014-07-29 10:26:56 -0500203 initializeKey();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500204
dmcoomes5bcb39e2017-10-31 15:07:55 -0500205 NLSR_LOG_DEBUG("Default NLSR identity: " << m_signingInfo.getSignerName());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500206
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600207 // Can be moved to HelloProtocol and Lsdb ctor if initializeKey is set
208 // earlier in the Nlsr constructor so as to set m_signingInfo
akmhoque31d1d4b2014-05-05 22:08:14 -0500209 setInfoInterestFilter();
210 setLsaInterestFilter();
Vince Lehman50df6b72015-03-03 12:06:40 -0600211
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500212 // add top-level prefixes: router and localhost prefix
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500213 addDispatcherTopPrefix(ndn::Name(m_confParam.getRouterPrefix()).append("nlsr"));
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500214 addDispatcherTopPrefix(LOCALHOST_PREFIX);
215
Nick Gordond5c1a372016-10-31 13:56:23 -0500216 initializeFaces(std::bind(&Nlsr::processFaceDataset, this, _1),
217 std::bind(&Nlsr::onFaceDatasetFetchTimeout, this, _1, _2, 0));
218
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500219 enableIncomingFaceIdIndication();
220
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600221 m_lsdb.buildAndInstallOwnNameLsa();
Nick Gordon5c467f02016-07-13 13:40:10 -0500222
223 // Install coordinate LSAs if using HR or dry-run HR.
224 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600225 m_lsdb.buildAndInstallOwnCoordinateLsa();
Nick Gordon5c467f02016-07-13 13:40:10 -0500226 }
Vince Lehman904c2412014-09-23 19:36:11 -0500227
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700228 registerKeyPrefix();
alvy297f4162015-03-03 17:15:33 -0600229 registerLocalhostPrefix();
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500230 registerRouterPrefix();
Vince Lehman7b616582014-10-17 16:25:39 -0500231
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600232 m_helloProtocol.scheduleInterest(m_confParam.getFirstHelloInterval());
Vince Lehman09131122014-09-09 17:10:11 -0500233
234 // Need to set direct neighbors' costs to 0 for hyperbolic routing
235 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
236
237 std::list<Adjacent>& neighbors = m_adjacencyList.getAdjList();
238
239 for (std::list<Adjacent>::iterator it = neighbors.begin(); it != neighbors.end(); ++it) {
240 it->setLinkCost(0);
241 }
242 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700243}
244
245void
akmhoque443ad812014-07-29 10:26:56 -0500246Nlsr::initializeKey()
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700247{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500248 NLSR_LOG_DEBUG("Initializing Key ...");
249
250 ndn::Name nlsrInstanceName = m_confParam.getRouterPrefix();
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500251 nlsrInstanceName.append("nlsr");
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700252
Joao Pereira97473d42015-07-03 16:57:27 -0400253 try {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500254 m_keyChain.deleteIdentity(m_keyChain.getPib().getIdentity(nlsrInstanceName));
255 } catch (const std::exception& e) {
256 NLSR_LOG_WARN(e.what());
257 }
258
259 auto nlsrInstanceIdentity = m_keyChain.createIdentity(nlsrInstanceName);
260 auto nlsrInstanceKey = nlsrInstanceIdentity.getDefaultKey();
261
262 ndn::security::v2::Certificate certificate;
263
264 ndn::Name certificateName = nlsrInstanceKey.getName();
265 certificateName.append("NA");
266 certificateName.appendVersion();
267 certificate.setName(certificateName);
268
269 // set metainfo
270 certificate.setContentType(ndn::tlv::ContentType_Key);
Ashlesh Gawande0d2c3822018-01-24 17:17:15 -0600271 certificate.setFreshnessPeriod(ndn::time::days(365));
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500272
273 // set content
274 certificate.setContent(nlsrInstanceKey.getPublicKey().data(), nlsrInstanceKey.getPublicKey().size());
275
276 // set signature-info
277 ndn::SignatureInfo signatureInfo;
278 signatureInfo.setValidityPeriod(ndn::security::ValidityPeriod(ndn::time::system_clock::TimePoint(),
279 ndn::time::system_clock::now()
Ashlesh Gawande0d2c3822018-01-24 17:17:15 -0600280 + ndn::time::days(365)));
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500281 try {
282 m_keyChain.sign(certificate,
283 ndn::security::SigningInfo(m_keyChain.getPib().getIdentity(m_confParam.getRouterPrefix()))
284 .setSignatureInfo(signatureInfo));
akmhoque102aea42014-08-04 10:22:12 -0500285 }
dmcoomes9f936662017-03-02 10:33:09 -0600286 catch (const std::exception& e) {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500287 NLSR_LOG_WARN("ERROR: Router's " << e.what()
288 << "NLSR is running without security."
289 << " If security is enabled NLSR will not converge.");
290
Muktadir Chowdhuryc3ea26f2018-01-05 21:40:59 +0000291 std::cerr << "Router's " << e.what() << ". NLSR is running without security "
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500292 << "(Only for testing, should not be used in production.)"
293 << " If security is enabled NLSR will not converge." << std::endl;
akmhoque102aea42014-08-04 10:22:12 -0500294 }
akmhoque443ad812014-07-29 10:26:56 -0500295
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500296 m_signingInfo = ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_ID,
297 nlsrInstanceName);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700298
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700299 loadCertToPublish(certificate);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700300}
301
302void
303Nlsr::registerKeyPrefix()
304{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500305 // Start listening for the interest of this router's NLSR certificate
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600306 ndn::Name nlsrKeyPrefix = m_confParam.getRouterPrefix();
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500307 nlsrKeyPrefix.append("nlsr");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500308 nlsrKeyPrefix.append("KEY");
309
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600310 m_face.setInterestFilter(ndn::InterestFilter(nlsrKeyPrefix).allowLoopback(false),
311 std::bind(&Nlsr::onKeyInterest, this, _1, _2),
312 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
313 std::bind(&Nlsr::registrationFailed, this, _1),
314 m_signingInfo, ndn::nfd::ROUTE_FLAG_CAPTURE);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700315
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500316 // Start listening for the interest of this router's certificate
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600317 ndn::Name routerKeyPrefix = m_confParam.getRouterPrefix();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500318 routerKeyPrefix.append("KEY");
319
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600320 m_face.setInterestFilter(ndn::InterestFilter(routerKeyPrefix).allowLoopback(false),
321 std::bind(&Nlsr::onKeyInterest, this, _1, _2),
322 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
323 std::bind(&Nlsr::registrationFailed, this, _1),
324 m_signingInfo, ndn::nfd::ROUTE_FLAG_CAPTURE);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500325
326 // Start listening for the interest of this router's operator's certificate
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600327 ndn::Name operatorKeyPrefix = m_confParam.getNetwork();
328 operatorKeyPrefix.append(m_confParam.getSiteName());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500329 operatorKeyPrefix.append(std::string("%C1.Operator"));
330
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600331 m_face.setInterestFilter(ndn::InterestFilter(operatorKeyPrefix).allowLoopback(false),
332 std::bind(&Nlsr::onKeyInterest, this, _1, _2),
333 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
334 std::bind(&Nlsr::registrationFailed, this, _1),
335 m_signingInfo, ndn::nfd::ROUTE_FLAG_CAPTURE);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500336
337 // Start listening for the interest of this router's site's certificate
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600338 ndn::Name siteKeyPrefix = m_confParam.getNetwork();
339 siteKeyPrefix.append(m_confParam.getSiteName());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500340 siteKeyPrefix.append("KEY");
341
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600342 m_face.setInterestFilter(ndn::InterestFilter(siteKeyPrefix).allowLoopback(false),
343 std::bind(&Nlsr::onKeyInterest, this, _1, _2),
344 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
345 std::bind(&Nlsr::registrationFailed, this, _1),
346 m_signingInfo, ndn::nfd::ROUTE_FLAG_CAPTURE);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700347}
348
349void
alvy297f4162015-03-03 17:15:33 -0600350Nlsr::registerLocalhostPrefix()
351{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600352 m_face.registerPrefix(LOCALHOST_PREFIX,
353 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
354 std::bind(&Nlsr::registrationFailed, this, _1));
alvy297f4162015-03-03 17:15:33 -0600355}
356
357void
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500358Nlsr::registerRouterPrefix()
359{
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600360 m_face.registerPrefix(ndn::Name(m_confParam.getRouterPrefix()).append("nlsr"),
361 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
362 std::bind(&Nlsr::registrationFailed, this, _1));
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500363}
364
365void
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700366Nlsr::onKeyInterest(const ndn::Name& name, const ndn::Interest& interest)
367{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500368 NLSR_LOG_DEBUG("Got interest for certificate. Interest: " << interest.getName());
369
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700370 const ndn::Name& interestName = interest.getName();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500371 const ndn::security::v2::Certificate* cert = getCertificate(interestName);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700372
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500373 if (cert == nullptr) {
374 NLSR_LOG_DEBUG("Certificate is not found for: " << interest);
dmcoomes9eaf3f42017-02-21 11:39:01 -0600375 return; // cert is not found
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500376 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700377
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600378 m_face.put(*cert);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700379}
380
381void
382Nlsr::onKeyPrefixRegSuccess(const ndn::Name& name)
383{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500384 NLSR_LOG_DEBUG("KEY prefix: " << name << " registration is successful.");
akmhoque53353462014-04-22 08:43:45 -0500385}
akmhoque5a44dd42014-03-12 18:11:32 -0500386
akmhoque53353462014-04-22 08:43:45 -0500387void
akmhoquec04e7272014-07-02 11:00:14 -0500388Nlsr::onFaceEventNotification(const ndn::nfd::FaceEventNotification& faceEventNotification)
akmhoquee1765152014-06-30 11:32:01 -0500389{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500390 NLSR_LOG_TRACE("Nlsr::onFaceEventNotification called");
Vince Lehman02e32992015-03-11 12:31:20 -0500391
Nick Gordond5c1a372016-10-31 13:56:23 -0500392 switch (faceEventNotification.getKind()) {
393 case ndn::nfd::FACE_EVENT_DESTROYED: {
394 uint64_t faceId = faceEventNotification.getFaceId();
Vince Lehman02e32992015-03-11 12:31:20 -0500395
Nick Gordond5c1a372016-10-31 13:56:23 -0500396 auto adjacent = m_adjacencyList.findAdjacent(faceId);
Vince Lehman02e32992015-03-11 12:31:20 -0500397
Nick Gordond5c1a372016-10-31 13:56:23 -0500398 if (adjacent != m_adjacencyList.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500399 NLSR_LOG_DEBUG("Face to " << adjacent->getName() << " with face id: " << faceId << " destroyed");
Vince Lehman02e32992015-03-11 12:31:20 -0500400
Nick Gordond5c1a372016-10-31 13:56:23 -0500401 adjacent->setFaceId(0);
Vince Lehman02e32992015-03-11 12:31:20 -0500402
Nick Gordond5c1a372016-10-31 13:56:23 -0500403 // Only trigger an Adjacency LSA build if this node is changing
404 // from ACTIVE to INACTIVE since this rebuild will effectively
405 // cancel the previous Adjacency LSA refresh event and schedule
406 // a new one further in the future.
407 //
408 // Continuously scheduling the refresh in the future will block
409 // the router from refreshing its Adjacency LSA. Since other
410 // routers' Name prefixes' expiration times are updated when
411 // this router refreshes its Adjacency LSA, the other routers'
412 // prefixes will expire and be removed from the RIB.
413 //
414 // This check is required to fix Bug #2733 for now. This check
415 // would be unnecessary to fix Bug #2733 when Issue #2732 is
416 // completed, but the check also helps with optimization so it
417 // can remain even when Issue #2732 is implemented.
418 if (adjacent->getStatus() == Adjacent::STATUS_ACTIVE) {
419 adjacent->setStatus(Adjacent::STATUS_INACTIVE);
Vince Lehman02e32992015-03-11 12:31:20 -0500420
Nick Gordond5c1a372016-10-31 13:56:23 -0500421 // A new adjacency LSA cannot be built until the neighbor is marked INACTIVE and
422 // has met the HELLO retry threshold
423 adjacent->setInterestTimedOutNo(m_confParam.getInterestRetryNumber());
Vince Lehman199e9cf2015-04-07 13:22:16 -0500424
Nick Gordond5c1a372016-10-31 13:56:23 -0500425 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600426 m_routingTable.scheduleRoutingTableCalculation();
Nick Gordond5c1a372016-10-31 13:56:23 -0500427 }
428 else {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600429 m_lsdb.scheduleAdjLsaBuild();
Nick Gordond5c1a372016-10-31 13:56:23 -0500430 }
Nick Gordone8e03ac2016-07-07 14:24:38 -0500431 }
Vince Lehman199e9cf2015-04-07 13:22:16 -0500432 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500433 break;
akmhoquec04e7272014-07-02 11:00:14 -0500434 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500435 case ndn::nfd::FACE_EVENT_CREATED: {
436 // Find the neighbor in our adjacency list
Ashlesh Gawande0d2c3822018-01-24 17:17:15 -0600437 ndn::FaceUri faceUri;
438 try {
439 faceUri = ndn::FaceUri(faceEventNotification.getRemoteUri());
440 }
441 catch (const std::exception& e) {
442 NLSR_LOG_WARN(e.what());
443 return;
444 }
445 auto adjacent = m_adjacencyList.findAdjacent(faceUri);
446
Nick Gordond5c1a372016-10-31 13:56:23 -0500447 // If we have a neighbor by that FaceUri and it has no FaceId, we
448 // have a match.
449 if (adjacent != m_adjacencyList.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500450 NLSR_LOG_DEBUG("Face creation event matches neighbor: " << adjacent->getName()
Nick Gordond5c1a372016-10-31 13:56:23 -0500451 << ". New Face ID: " << faceEventNotification.getFaceId()
452 << ". Registering prefixes.");
453 adjacent->setFaceId(faceEventNotification.getFaceId());
454
455 registerAdjacencyPrefixes(*adjacent, ndn::time::milliseconds::max());
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500456
457 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600458 m_routingTable.scheduleRoutingTableCalculation();
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500459 }
460 else {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600461 m_lsdb.scheduleAdjLsaBuild();
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500462 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500463 }
464 break;
465 }
466 default:
467 break;
akmhoquec04e7272014-07-02 11:00:14 -0500468 }
akmhoquee1765152014-06-30 11:32:01 -0500469}
470
Nick Gordond5c1a372016-10-31 13:56:23 -0500471void
472Nlsr::initializeFaces(const FetchDatasetCallback& onFetchSuccess,
473 const FetchDatasetTimeoutCallback& onFetchFailure)
474{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500475 NLSR_LOG_TRACE("Initializing Faces...");
Nick Gordond5c1a372016-10-31 13:56:23 -0500476
477 m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(onFetchSuccess, onFetchFailure);
478
479}
480
481void
482Nlsr::processFaceDataset(const std::vector<ndn::nfd::FaceStatus>& faces)
483{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500484 NLSR_LOG_DEBUG("Processing face dataset");
Nick Gordond5c1a372016-10-31 13:56:23 -0500485
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500486 // Iterate over each neighbor listed in nlsr.conf
487 for (auto& adjacent : m_adjacencyList.getAdjList()) {
488
489 const std::string faceUriString = adjacent.getFaceUri().toString();
Nick Gordond5c1a372016-10-31 13:56:23 -0500490 // Check the list of FaceStatus objects we got for a match
491 for (const ndn::nfd::FaceStatus& faceStatus : faces) {
Nick Gordond5c1a372016-10-31 13:56:23 -0500492 // Set the adjacency FaceID if we find a URI match and it was
493 // previously unset. Change the boolean to true.
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500494 if (adjacent.getFaceId() == 0 && faceUriString == faceStatus.getRemoteUri()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500495 NLSR_LOG_DEBUG("FaceUri: " << faceStatus.getRemoteUri() <<
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500496 " FaceId: "<< faceStatus.getFaceId());
497 adjacent.setFaceId(faceStatus.getFaceId());
Nick Gordond5c1a372016-10-31 13:56:23 -0500498 // Register the prefixes for each neighbor
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500499 this->registerAdjacencyPrefixes(adjacent, ndn::time::milliseconds::max());
Nick Gordond5c1a372016-10-31 13:56:23 -0500500 }
501 }
502 // If this adjacency has no information in this dataset, then one
503 // of two things is happening: 1. NFD is starting slowly and this
504 // Face wasn't ready yet, or 2. NFD is configured
505 // incorrectly and this Face isn't available.
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500506 if (adjacent.getFaceId() == 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500507 NLSR_LOG_WARN("The adjacency " << adjacent.getName() <<
Nick Gordond5c1a372016-10-31 13:56:23 -0500508 " has no Face information in this dataset.");
509 }
510 }
511
Nick Gordond5c1a372016-10-31 13:56:23 -0500512 scheduleDatasetFetch();
513}
514
515void
516Nlsr::registerAdjacencyPrefixes(const Adjacent& adj,
517 const ndn::time::milliseconds& timeout)
518{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500519 ndn::FaceUri faceUri = adj.getFaceUri();
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500520 double linkCost = adj.getLinkCost();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500521 const ndn::Name& adjName = adj.getName();
Nick Gordond5c1a372016-10-31 13:56:23 -0500522
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500523 m_fib.registerPrefix(adjName, faceUri, linkCost,
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500524 timeout, ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500525
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600526 m_fib.registerPrefix(m_confParam.getSyncPrefix(),
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500527 faceUri, linkCost, timeout,
528 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500529
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500530 m_fib.registerPrefix(m_confParam.getLsaPrefix(),
531 faceUri, linkCost, timeout,
532 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500533}
534
535void
536Nlsr::onFaceDatasetFetchTimeout(uint32_t code,
537 const std::string& msg,
538 uint32_t nRetriesSoFar)
539{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500540 NLSR_LOG_DEBUG("onFaceDatasetFetchTimeout");
Nick Gordond5c1a372016-10-31 13:56:23 -0500541 // If we have exceeded the maximum attempt count, do not try again.
542 if (nRetriesSoFar++ < m_confParam.getFaceDatasetFetchTries()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500543 NLSR_LOG_DEBUG("Failed to fetch dataset: " << msg << ". Attempting retry #" << nRetriesSoFar);
Nick Gordond5c1a372016-10-31 13:56:23 -0500544 m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(std::bind(&Nlsr::processFaceDataset,
545 this, _1),
546 std::bind(&Nlsr::onFaceDatasetFetchTimeout,
547 this, _1, _2, nRetriesSoFar));
548 }
549 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500550 NLSR_LOG_ERROR("Failed to fetch dataset: " << msg << ". Exceeded limit of " <<
Nick Gordond5c1a372016-10-31 13:56:23 -0500551 m_confParam.getFaceDatasetFetchTries() << ", so not trying again this time.");
552 // If we fail to fetch it, just do nothing until the next
553 // interval. Since this is a backup mechanism, we aren't as
554 // concerned with retrying.
555 scheduleDatasetFetch();
556 }
557}
558
559void
560Nlsr::scheduleDatasetFetch()
561{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500562 NLSR_LOG_DEBUG("Scheduling Dataset Fetch in " << m_confParam.getFaceDatasetFetchInterval());
563
Nick Gordond5c1a372016-10-31 13:56:23 -0500564 m_scheduler.scheduleEvent(m_confParam.getFaceDatasetFetchInterval(),
565 [this] {
566 this->initializeFaces(
567 [this] (const std::vector<ndn::nfd::FaceStatus>& faces) {
568 this->processFaceDataset(faces);
569 },
570 [this] (uint32_t code, const std::string& msg) {
571 this->onFaceDatasetFetchTimeout(code, msg, 0);
572 });
573 });
574}
akmhoquee1765152014-06-30 11:32:01 -0500575
576void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500577Nlsr::enableIncomingFaceIdIndication()
578{
579 NLSR_LOG_DEBUG("Enabling incoming face id indication for local face.");
580
581 m_controller.start<ndn::nfd::FaceUpdateCommand>(
582 ndn::nfd::ControlParameters()
583 .setFlagBit(ndn::nfd::FaceFlagBit::BIT_LOCAL_FIELDS_ENABLED, true),
584 bind(&Nlsr::onFaceIdIndicationSuccess, this, _1),
585 bind(&Nlsr::onFaceIdIndicationFailure, this, _1));
586}
587
588void
589Nlsr::onFaceIdIndicationSuccess(const ndn::nfd::ControlParameters& cp)
590{
591 NLSR_LOG_DEBUG("Successfully enabled incoming face id indication"
592 << "for face id " << cp.getFaceId());
593}
594
595void
596Nlsr::onFaceIdIndicationFailure(const ndn::nfd::ControlResponse& cr)
597{
598 std::ostringstream os;
599 os << "Failed to enable incoming face id indication feature: " <<
600 "(code: " << cr.getCode() << ", reason: " << cr.getText() << ")";
601
602 NLSR_LOG_DEBUG(os.str());
603}
604
akmhoqueb1710aa2014-02-19 17:13:36 -0600605} // namespace nlsr