blob: 605c563d28f48fe38b4c8cac57f225d28b12529a [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
laqinfan35731852017-08-08 06:17:39 -05003 * Copyright (c) 2014-2018, The University of Memphis,
Vince Lehmanc2e51f62015-01-20 15:03:11 -06004 * Regents of the University of California,
5 * Arizona Board of Regents.
akmhoque3d06e792014-05-27 16:23:20 -05006 *
7 * This file is part of NLSR (Named-data Link State Routing).
8 * See AUTHORS.md for complete list of NLSR authors and contributors.
9 *
10 * NLSR is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
akmhoque3d06e792014-05-27 16:23:20 -050020 **/
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -050021
Laqin Fan54a43f02017-03-08 12:31:30 -060022#include "nlsr.hpp"
23#include "adjacent.hpp"
24#include "logger.hpp"
25
akmhoque298385a2014-02-13 14:13:09 -060026#include <cstdlib>
akmhoque92afde42014-02-18 14:04:07 -060027#include <string>
akmhoque298385a2014-02-13 14:13:09 -060028#include <sstream>
akmhoque05d5fcf2014-04-15 14:58:45 -050029#include <cstdio>
akmhoque0494c252014-07-23 23:46:44 -050030#include <unistd.h>
akmhoque298385a2014-02-13 14:13:09 -060031
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050032#include <ndn-cxx/net/face-uri.hpp>
33#include <ndn-cxx/signature.hpp>
akmhoque298385a2014-02-13 14:13:09 -060034
akmhoque53353462014-04-22 08:43:45 -050035namespace nlsr {
36
dmcoomescf8d0ed2017-02-21 11:39:01 -060037INIT_LOGGER(Nlsr);
akmhoque674b0b12014-05-20 14:33:28 -050038
alvy297f4162015-03-03 17:15:33 -060039const ndn::Name Nlsr::LOCALHOST_PREFIX = ndn::Name("/localhost/nlsr");
40
Laqin Fana4cf4022017-01-03 18:57:35 +000041Nlsr::Nlsr(boost::asio::io_service& ioService, ndn::Scheduler& scheduler, ndn::Face& face, ndn::KeyChain& keyChain)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050042 : m_nlsrFace(face)
43 , m_scheduler(scheduler)
Laqin Fana4cf4022017-01-03 18:57:35 +000044 , m_keyChain(keyChain)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050045 , m_confParam()
46 , m_adjacencyList()
47 , m_namePrefixList()
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050048 , m_configFileName("nlsr.conf")
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050049 , m_nlsrLsdb(*this, scheduler)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050050 , m_adjBuildCount(0)
51 , m_isBuildAdjLsaSheduled(false)
52 , m_isRouteCalculationScheduled(false)
53 , m_isRoutingTableCalculating(false)
54 , m_routingTable(scheduler)
55 , m_fib(m_nlsrFace, scheduler, m_adjacencyList, m_confParam, m_keyChain)
Nick Gordonb7b58392017-08-17 16:29:21 -050056 , m_namePrefixTable(*this, m_routingTable.afterRoutingChange)
laqinfan35731852017-08-08 06:17:39 -050057 , m_dispatcher(m_nlsrFace, m_keyChain)
58 , m_datasetHandler(m_nlsrLsdb,
59 m_routingTable,
60 m_dispatcher,
61 m_nlsrFace,
62 m_keyChain)
Vince Lehmanf7eec4f2015-05-08 19:02:31 -050063 , m_helloProtocol(*this, scheduler)
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050064 , m_validator(ndn::make_unique<ndn::security::v2::CertificateFetcherDirectFetch>(m_nlsrFace))
65 , m_controller(m_nlsrFace, m_keyChain)
Nick Gordond5c1a372016-10-31 13:56:23 -050066 , m_faceDatasetController(m_nlsrFace, m_keyChain)
laqinfan35731852017-08-08 06:17:39 -050067 , m_prefixUpdateProcessor(m_dispatcher,
Laqin Fan54a43f02017-03-08 12:31:30 -060068 m_nlsrFace,
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050069 m_namePrefixList,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050070 m_nlsrLsdb)
laqinfan35731852017-08-08 06:17:39 -050071 , m_nfdRibCommandProcessor(m_dispatcher,
Nick Gordon4d2c6c02017-01-20 13:18:46 -060072 m_namePrefixList,
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050073 m_nlsrLsdb)
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060074 , m_statsCollector(m_nlsrLsdb, m_helloProtocol)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050075 , m_faceMonitor(m_nlsrFace)
76 , m_firstHelloInterval(FIRST_HELLO_INTERVAL_DEFAULT)
77{
dmcoomes9f936662017-03-02 10:33:09 -060078 m_faceMonitor.onNotification.connect(std::bind(&Nlsr::onFaceEventNotification, this, _1));
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050079 m_faceMonitor.start();
80}
81
akmhoque53353462014-04-22 08:43:45 -050082void
83Nlsr::registrationFailed(const ndn::Name& name)
akmhoque298385a2014-02-13 14:13:09 -060084{
dmcoomes5bcb39e2017-10-31 15:07:55 -050085 NLSR_LOG_ERROR("ERROR: Failed to register prefix in local hub's daemon");
dmcoomes9f936662017-03-02 10:33:09 -060086 BOOST_THROW_EXCEPTION(Error("Error: Prefix registration failed"));
akmhoque53353462014-04-22 08:43:45 -050087}
akmhoque1fd8c1e2014-02-19 19:41:49 -060088
akmhoque157b0a42014-05-13 00:26:37 -050089void
90Nlsr::onRegistrationSuccess(const ndn::Name& name)
91{
dmcoomes5bcb39e2017-10-31 15:07:55 -050092 NLSR_LOG_DEBUG("Successfully registered prefix: " << name);
alvy297f4162015-03-03 17:15:33 -060093}
94
95void
akmhoque31d1d4b2014-05-05 22:08:14 -050096Nlsr::setInfoInterestFilter()
akmhoque53353462014-04-22 08:43:45 -050097{
akmhoque31d1d4b2014-05-05 22:08:14 -050098 ndn::Name name(m_confParam.getRouterPrefix());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050099 name.append("NLSR");
100 name.append("INFO");
101
102 NLSR_LOG_DEBUG("Setting interest filter for Hello interest: " << name);
103
akmhoquefdbddb12014-05-02 18:35:19 -0500104 getNlsrFace().setInterestFilter(name,
Joao Pereira97473d42015-07-03 16:57:27 -0400105 std::bind(&HelloProtocol::processInterest,
akmhoque31d1d4b2014-05-05 22:08:14 -0500106 &m_helloProtocol, _1, _2),
Joao Pereira97473d42015-07-03 16:57:27 -0400107 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
108 std::bind(&Nlsr::registrationFailed, this, _1),
109 m_signingInfo,
akmhoque060d3022014-08-12 13:35:06 -0500110 ndn::nfd::ROUTE_FLAG_CAPTURE);
akmhoque31d1d4b2014-05-05 22:08:14 -0500111}
112
113void
114Nlsr::setLsaInterestFilter()
115{
akmhoque157b0a42014-05-13 00:26:37 -0500116 ndn::Name name = m_confParam.getLsaPrefix();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500117
dmcoomes5bcb39e2017-10-31 15:07:55 -0500118 NLSR_LOG_DEBUG("Setting interest filter for LsaPrefix: " << name);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500119
akmhoque31d1d4b2014-05-05 22:08:14 -0500120 getNlsrFace().setInterestFilter(name,
Joao Pereira97473d42015-07-03 16:57:27 -0400121 std::bind(&Lsdb::processInterest,
akmhoque31d1d4b2014-05-05 22:08:14 -0500122 &m_nlsrLsdb, _1, _2),
Joao Pereira97473d42015-07-03 16:57:27 -0400123 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
124 std::bind(&Nlsr::registrationFailed, this, _1),
125 m_signingInfo,
akmhoque060d3022014-08-12 13:35:06 -0500126 ndn::nfd::ROUTE_FLAG_CAPTURE);
akmhoque53353462014-04-22 08:43:45 -0500127}
128
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500129
130void
131Nlsr::addDispatcherTopPrefix(const ndn::Name& topPrefix)
132{
133 try {
laqinfan35731852017-08-08 06:17:39 -0500134 m_dispatcher.addTopPrefix(topPrefix, false, m_signingInfo);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500135 }
136 catch (const std::exception& e) {
137 NLSR_LOG_ERROR("Error setting top-level prefix in dispatcher: " << e.what() << "\n");
138 }
139}
140
akmhoque53353462014-04-22 08:43:45 -0500141void
akmhoquec04e7272014-07-02 11:00:14 -0500142Nlsr::setStrategies()
akmhoque157b0a42014-05-13 00:26:37 -0500143{
Vince Lehman53c0e3e2015-09-14 14:33:20 -0500144 const std::string strategy("ndn:/localhost/nfd/strategy/multicast");
145
akmhoque393d4ff2014-07-16 14:27:03 -0500146 m_fib.setStrategy(m_confParam.getLsaPrefix(), strategy, 0);
akmhoque393d4ff2014-07-16 14:27:03 -0500147 m_fib.setStrategy(m_confParam.getChronosyncPrefix(), strategy, 0);
akmhoque157b0a42014-05-13 00:26:37 -0500148}
149
150void
Nick Gordon922714a2017-06-13 14:12:02 -0500151Nlsr::canonizeContinuation(std::list<Adjacent>::iterator iterator,
152 std::function<void(void)> finally)
Nick Gordon9461afb2017-04-25 15:54:50 -0500153{
Nick Gordon922714a2017-06-13 14:12:02 -0500154 canonizeNeighborUris(iterator, [this, finally] (std::list<Adjacent>::iterator iterator) {
155 canonizeContinuation(iterator, finally);
156 },
157 finally);
Nick Gordon9461afb2017-04-25 15:54:50 -0500158}
159
160void
161Nlsr::canonizeNeighborUris(std::list<Adjacent>::iterator currentNeighbor,
Nick Gordon922714a2017-06-13 14:12:02 -0500162 std::function<void(std::list<Adjacent>::iterator)> then,
163 std::function<void(void)> finally)
Nick Gordon9461afb2017-04-25 15:54:50 -0500164{
165 if (currentNeighbor != m_adjacencyList.getAdjList().end()) {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500166 ndn::FaceUri uri(currentNeighbor->getFaceUri());
167 uri.canonize([this, then, currentNeighbor] (ndn::FaceUri canonicalUri) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500168 NLSR_LOG_DEBUG("Canonized URI: " << currentNeighbor->getFaceUri()
Nick Gordon9461afb2017-04-25 15:54:50 -0500169 << " to: " << canonicalUri);
Nick Gordone9733ed2017-04-26 10:48:39 -0500170 currentNeighbor->setFaceUri(canonicalUri);
Nick Gordon9461afb2017-04-25 15:54:50 -0500171 then(std::next(currentNeighbor));
172 },
173 [this, then, currentNeighbor] (const std::string& reason) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500174 NLSR_LOG_ERROR("Could not canonize URI: " << currentNeighbor->getFaceUri()
Nick Gordon9461afb2017-04-25 15:54:50 -0500175 << " because: " << reason);
176 then(std::next(currentNeighbor));
177 },
178 m_nlsrFace.getIoService(),
179 TIME_ALLOWED_FOR_CANONIZATION);
180 }
Nick Gordon922714a2017-06-13 14:12:02 -0500181 // We have finished canonizing all neighbors, so call finally()
Nick Gordon9461afb2017-04-25 15:54:50 -0500182 else {
Nick Gordon922714a2017-06-13 14:12:02 -0500183 finally();
Nick Gordon9461afb2017-04-25 15:54:50 -0500184 }
185}
186
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500187
188void
189Nlsr::loadCertToPublish(const ndn::security::v2::Certificate& certificate)
190{
191 m_certStore.insert(certificate);
192 m_validator.loadAnchor("Authoritative-Certificate",
193 ndn::security::v2::Certificate(certificate));
194 m_prefixUpdateProcessor.getValidator().
195 loadAnchor("Authoritative-Certificate",
196 ndn::security::v2::Certificate(certificate));
197}
198
Nick Gordon9461afb2017-04-25 15:54:50 -0500199void
akmhoque53353462014-04-22 08:43:45 -0500200Nlsr::initialize()
201{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500202 NLSR_LOG_DEBUG("Initializing Nlsr");
akmhoque53353462014-04-22 08:43:45 -0500203 m_confParam.buildRouterPrefix();
laqinfan35731852017-08-08 06:17:39 -0500204 m_datasetHandler.setRouterNameCommandPrefix(m_confParam.getRouterPrefix());
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700205 m_nlsrLsdb.setLsaRefreshTime(ndn::time::seconds(m_confParam.getLsaRefreshTime()));
akmhoque31d1d4b2014-05-05 22:08:14 -0500206 m_nlsrLsdb.setThisRouterPrefix(m_confParam.getRouterPrefix().toUri());
akmhoque53353462014-04-22 08:43:45 -0500207 m_fib.setEntryRefreshTime(2 * m_confParam.getLsaRefreshTime());
Vince Lehmanc11cc202015-01-20 11:41:33 -0600208
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500209 m_nlsrLsdb.getSequencingManager().setSeqFileDirectory(m_confParam.getSeqFileDir());
210 m_nlsrLsdb.getSequencingManager().initiateSeqNoFromFile(m_confParam.getHyperbolicState());
211
212 m_nlsrLsdb.getSyncLogicHandler().createSyncSocket(m_confParam.getChronosyncPrefix());
Vince Lehmanc11cc202015-01-20 11:41:33 -0600213
dmcoomes9f936662017-03-02 10:33:09 -0600214 // Logging start
akmhoque674b0b12014-05-20 14:33:28 -0500215 m_confParam.writeLog();
216 m_adjacencyList.writeLog();
dmcoomes5bcb39e2017-10-31 15:07:55 -0500217 NLSR_LOG_DEBUG(m_namePrefixList);
dmcoomes9f936662017-03-02 10:33:09 -0600218 // Logging end
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500219
akmhoque443ad812014-07-29 10:26:56 -0500220 initializeKey();
akmhoquec04e7272014-07-02 11:00:14 -0500221 setStrategies();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500222
dmcoomes5bcb39e2017-10-31 15:07:55 -0500223 NLSR_LOG_DEBUG("Default NLSR identity: " << m_signingInfo.getSignerName());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500224
akmhoque31d1d4b2014-05-05 22:08:14 -0500225 setInfoInterestFilter();
226 setLsaInterestFilter();
Vince Lehman50df6b72015-03-03 12:06:40 -0600227
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500228 // add top-level prefixes: router and localhost prefix
229 addDispatcherTopPrefix(m_confParam.getRouterPrefix());
230 addDispatcherTopPrefix(LOCALHOST_PREFIX);
231
Nick Gordond5c1a372016-10-31 13:56:23 -0500232 initializeFaces(std::bind(&Nlsr::processFaceDataset, this, _1),
233 std::bind(&Nlsr::onFaceDatasetFetchTimeout, this, _1, _2, 0));
234
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500235 enableIncomingFaceIdIndication();
236
Vince Lehman50df6b72015-03-03 12:06:40 -0600237 // Set event intervals
238 setFirstHelloInterval(m_confParam.getFirstHelloInterval());
239 m_nlsrLsdb.setAdjLsaBuildInterval(m_confParam.getAdjLsaBuildInterval());
240 m_routingTable.setRoutingCalcInterval(m_confParam.getRoutingCalcInterval());
241
akmhoque674b0b12014-05-20 14:33:28 -0500242 m_nlsrLsdb.buildAndInstallOwnNameLsa();
Nick Gordon5c467f02016-07-13 13:40:10 -0500243
244 // Install coordinate LSAs if using HR or dry-run HR.
245 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
246 m_nlsrLsdb.buildAndInstallOwnCoordinateLsa();
247 }
Vince Lehman904c2412014-09-23 19:36:11 -0500248
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700249 registerKeyPrefix();
alvy297f4162015-03-03 17:15:33 -0600250 registerLocalhostPrefix();
Vince Lehman7b616582014-10-17 16:25:39 -0500251
Vince Lehman7b616582014-10-17 16:25:39 -0500252 m_helloProtocol.scheduleInterest(m_firstHelloInterval);
Vince Lehman09131122014-09-09 17:10:11 -0500253
254 // Need to set direct neighbors' costs to 0 for hyperbolic routing
255 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
256
257 std::list<Adjacent>& neighbors = m_adjacencyList.getAdjList();
258
259 for (std::list<Adjacent>::iterator it = neighbors.begin(); it != neighbors.end(); ++it) {
260 it->setLinkCost(0);
261 }
262 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700263}
264
265void
akmhoque443ad812014-07-29 10:26:56 -0500266Nlsr::initializeKey()
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700267{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500268 NLSR_LOG_DEBUG("Initializing Key ...");
269
270 ndn::Name nlsrInstanceName = m_confParam.getRouterPrefix();
271 nlsrInstanceName.append("NLSR");
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700272
Joao Pereira97473d42015-07-03 16:57:27 -0400273 try {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500274 m_keyChain.deleteIdentity(m_keyChain.getPib().getIdentity(nlsrInstanceName));
275 } catch (const std::exception& e) {
276 NLSR_LOG_WARN(e.what());
277 }
278
279 auto nlsrInstanceIdentity = m_keyChain.createIdentity(nlsrInstanceName);
280 auto nlsrInstanceKey = nlsrInstanceIdentity.getDefaultKey();
281
282 ndn::security::v2::Certificate certificate;
283
284 ndn::Name certificateName = nlsrInstanceKey.getName();
285 certificateName.append("NA");
286 certificateName.appendVersion();
287 certificate.setName(certificateName);
288
289 // set metainfo
290 certificate.setContentType(ndn::tlv::ContentType_Key);
Ashlesh Gawande0d2c3822018-01-24 17:17:15 -0600291 certificate.setFreshnessPeriod(ndn::time::days(365));
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500292
293 // set content
294 certificate.setContent(nlsrInstanceKey.getPublicKey().data(), nlsrInstanceKey.getPublicKey().size());
295
296 // set signature-info
297 ndn::SignatureInfo signatureInfo;
298 signatureInfo.setValidityPeriod(ndn::security::ValidityPeriod(ndn::time::system_clock::TimePoint(),
299 ndn::time::system_clock::now()
Ashlesh Gawande0d2c3822018-01-24 17:17:15 -0600300 + ndn::time::days(365)));
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500301 try {
302 m_keyChain.sign(certificate,
303 ndn::security::SigningInfo(m_keyChain.getPib().getIdentity(m_confParam.getRouterPrefix()))
304 .setSignatureInfo(signatureInfo));
akmhoque102aea42014-08-04 10:22:12 -0500305 }
dmcoomes9f936662017-03-02 10:33:09 -0600306 catch (const std::exception& e) {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500307 NLSR_LOG_WARN("ERROR: Router's " << e.what()
308 << "NLSR is running without security."
309 << " If security is enabled NLSR will not converge.");
310
311 std::cerr << "Router's " << e.what() << "NLSR is running without security "
312 << "(Only for testing, should not be used in production.)"
313 << " If security is enabled NLSR will not converge." << std::endl;
akmhoque102aea42014-08-04 10:22:12 -0500314 }
akmhoque443ad812014-07-29 10:26:56 -0500315
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500316 m_signingInfo = ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_ID,
317 nlsrInstanceName);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700318
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700319 loadCertToPublish(certificate);
320
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500321 m_defaultCertName = certificate.getName();
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700322}
323
324void
325Nlsr::registerKeyPrefix()
326{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500327 // Start listening for the interest of this router's NLSR certificate
328 ndn::Name nlsrKeyPrefix = getConfParameter().getRouterPrefix();
329 nlsrKeyPrefix.append("NLSR");
330 nlsrKeyPrefix.append("KEY");
331
332 m_nlsrFace.setInterestFilter(nlsrKeyPrefix,
Joao Pereira97473d42015-07-03 16:57:27 -0400333 std::bind(&Nlsr::onKeyInterest,
Yingdi Yu6a3a4dd2014-06-20 14:10:39 -0700334 this, _1, _2),
Joao Pereira97473d42015-07-03 16:57:27 -0400335 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
336 std::bind(&Nlsr::registrationFailed, this, _1),
337 m_signingInfo,
akmhoque060d3022014-08-12 13:35:06 -0500338 ndn::nfd::ROUTE_FLAG_CAPTURE);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700339
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500340 // Start listening for the interest of this router's certificate
341 ndn::Name routerKeyPrefix = getConfParameter().getRouterPrefix();
342 routerKeyPrefix.append("KEY");
343
344 m_nlsrFace.setInterestFilter(routerKeyPrefix,
345 std::bind(&Nlsr::onKeyInterest,
346 this, _1, _2),
347 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
348 std::bind(&Nlsr::registrationFailed, this, _1),
349 m_signingInfo,
350 ndn::nfd::ROUTE_FLAG_CAPTURE);
351
352 // Start listening for the interest of this router's operator's certificate
353 ndn::Name operatorKeyPrefix = getConfParameter().getNetwork();
354 operatorKeyPrefix.append(getConfParameter().getSiteName());
355 operatorKeyPrefix.append(std::string("%C1.Operator"));
356
357 m_nlsrFace.setInterestFilter(operatorKeyPrefix,
358 std::bind(&Nlsr::onKeyInterest,
359 this, _1, _2),
360 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
361 std::bind(&Nlsr::registrationFailed, this, _1),
362 m_signingInfo,
363 ndn::nfd::ROUTE_FLAG_CAPTURE);
364
365 // Start listening for the interest of this router's site's certificate
366 ndn::Name siteKeyPrefix = getConfParameter().getNetwork();
367 siteKeyPrefix.append(getConfParameter().getSiteName());
368 siteKeyPrefix.append("KEY");
369
370 m_nlsrFace.setInterestFilter(siteKeyPrefix,
371 std::bind(&Nlsr::onKeyInterest,
372 this, _1, _2),
373 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
374 std::bind(&Nlsr::registrationFailed, this, _1),
375 m_signingInfo,
376 ndn::nfd::ROUTE_FLAG_CAPTURE);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700377}
378
379void
alvy297f4162015-03-03 17:15:33 -0600380Nlsr::registerLocalhostPrefix()
381{
alvy297f4162015-03-03 17:15:33 -0600382 m_nlsrFace.registerPrefix(LOCALHOST_PREFIX,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500383 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
alvy297f4162015-03-03 17:15:33 -0600384 std::bind(&Nlsr::registrationFailed, this, _1));
385}
386
387void
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700388Nlsr::onKeyInterest(const ndn::Name& name, const ndn::Interest& interest)
389{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500390 NLSR_LOG_DEBUG("Got interest for certificate. Interest: " << interest.getName());
391
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700392 const ndn::Name& interestName = interest.getName();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500393 const ndn::security::v2::Certificate* cert = getCertificate(interestName);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700394
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500395 if (cert == nullptr) {
396 NLSR_LOG_DEBUG("Certificate is not found for: " << interest);
dmcoomes9eaf3f42017-02-21 11:39:01 -0600397 return; // cert is not found
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500398 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700399
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500400 m_nlsrFace.put(*cert);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700401}
402
403void
404Nlsr::onKeyPrefixRegSuccess(const ndn::Name& name)
405{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500406 NLSR_LOG_DEBUG("KEY prefix: " << name << " registration is successful.");
akmhoque53353462014-04-22 08:43:45 -0500407}
akmhoque5a44dd42014-03-12 18:11:32 -0500408
akmhoque53353462014-04-22 08:43:45 -0500409void
akmhoquec04e7272014-07-02 11:00:14 -0500410Nlsr::onFaceEventNotification(const ndn::nfd::FaceEventNotification& faceEventNotification)
akmhoquee1765152014-06-30 11:32:01 -0500411{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500412 NLSR_LOG_TRACE("Nlsr::onFaceEventNotification called");
Vince Lehman02e32992015-03-11 12:31:20 -0500413
Nick Gordond5c1a372016-10-31 13:56:23 -0500414 switch (faceEventNotification.getKind()) {
415 case ndn::nfd::FACE_EVENT_DESTROYED: {
416 uint64_t faceId = faceEventNotification.getFaceId();
Vince Lehman02e32992015-03-11 12:31:20 -0500417
Nick Gordond5c1a372016-10-31 13:56:23 -0500418 auto adjacent = m_adjacencyList.findAdjacent(faceId);
Vince Lehman02e32992015-03-11 12:31:20 -0500419
Nick Gordond5c1a372016-10-31 13:56:23 -0500420 if (adjacent != m_adjacencyList.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500421 NLSR_LOG_DEBUG("Face to " << adjacent->getName() << " with face id: " << faceId << " destroyed");
Vince Lehman02e32992015-03-11 12:31:20 -0500422
Nick Gordond5c1a372016-10-31 13:56:23 -0500423 adjacent->setFaceId(0);
Vince Lehman02e32992015-03-11 12:31:20 -0500424
Nick Gordond5c1a372016-10-31 13:56:23 -0500425 // Only trigger an Adjacency LSA build if this node is changing
426 // from ACTIVE to INACTIVE since this rebuild will effectively
427 // cancel the previous Adjacency LSA refresh event and schedule
428 // a new one further in the future.
429 //
430 // Continuously scheduling the refresh in the future will block
431 // the router from refreshing its Adjacency LSA. Since other
432 // routers' Name prefixes' expiration times are updated when
433 // this router refreshes its Adjacency LSA, the other routers'
434 // prefixes will expire and be removed from the RIB.
435 //
436 // This check is required to fix Bug #2733 for now. This check
437 // would be unnecessary to fix Bug #2733 when Issue #2732 is
438 // completed, but the check also helps with optimization so it
439 // can remain even when Issue #2732 is implemented.
440 if (adjacent->getStatus() == Adjacent::STATUS_ACTIVE) {
441 adjacent->setStatus(Adjacent::STATUS_INACTIVE);
Vince Lehman02e32992015-03-11 12:31:20 -0500442
Nick Gordond5c1a372016-10-31 13:56:23 -0500443 // A new adjacency LSA cannot be built until the neighbor is marked INACTIVE and
444 // has met the HELLO retry threshold
445 adjacent->setInterestTimedOutNo(m_confParam.getInterestRetryNumber());
Vince Lehman199e9cf2015-04-07 13:22:16 -0500446
Nick Gordond5c1a372016-10-31 13:56:23 -0500447 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
448 getRoutingTable().scheduleRoutingTableCalculation(*this);
449 }
450 else {
451 m_nlsrLsdb.scheduleAdjLsaBuild();
452 }
Nick Gordone8e03ac2016-07-07 14:24:38 -0500453 }
Vince Lehman199e9cf2015-04-07 13:22:16 -0500454 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500455 break;
akmhoquec04e7272014-07-02 11:00:14 -0500456 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500457 case ndn::nfd::FACE_EVENT_CREATED: {
458 // Find the neighbor in our adjacency list
Ashlesh Gawande0d2c3822018-01-24 17:17:15 -0600459 ndn::FaceUri faceUri;
460 try {
461 faceUri = ndn::FaceUri(faceEventNotification.getRemoteUri());
462 }
463 catch (const std::exception& e) {
464 NLSR_LOG_WARN(e.what());
465 return;
466 }
467 auto adjacent = m_adjacencyList.findAdjacent(faceUri);
468
Nick Gordond5c1a372016-10-31 13:56:23 -0500469 // If we have a neighbor by that FaceUri and it has no FaceId, we
470 // have a match.
471 if (adjacent != m_adjacencyList.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500472 NLSR_LOG_DEBUG("Face creation event matches neighbor: " << adjacent->getName()
Nick Gordond5c1a372016-10-31 13:56:23 -0500473 << ". New Face ID: " << faceEventNotification.getFaceId()
474 << ". Registering prefixes.");
475 adjacent->setFaceId(faceEventNotification.getFaceId());
476
477 registerAdjacencyPrefixes(*adjacent, ndn::time::milliseconds::max());
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500478
479 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
480 getRoutingTable().scheduleRoutingTableCalculation(*this);
481 }
482 else {
483 m_nlsrLsdb.scheduleAdjLsaBuild();
484 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500485 }
486 break;
487 }
488 default:
489 break;
akmhoquec04e7272014-07-02 11:00:14 -0500490 }
akmhoquee1765152014-06-30 11:32:01 -0500491}
492
Nick Gordond5c1a372016-10-31 13:56:23 -0500493void
494Nlsr::initializeFaces(const FetchDatasetCallback& onFetchSuccess,
495 const FetchDatasetTimeoutCallback& onFetchFailure)
496{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500497 NLSR_LOG_TRACE("Initializing Faces...");
Nick Gordond5c1a372016-10-31 13:56:23 -0500498
499 m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(onFetchSuccess, onFetchFailure);
500
501}
502
503void
504Nlsr::processFaceDataset(const std::vector<ndn::nfd::FaceStatus>& faces)
505{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500506 NLSR_LOG_DEBUG("Processing face dataset");
Nick Gordond5c1a372016-10-31 13:56:23 -0500507
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500508 // Iterate over each neighbor listed in nlsr.conf
509 for (auto& adjacent : m_adjacencyList.getAdjList()) {
510
511 const std::string faceUriString = adjacent.getFaceUri().toString();
Nick Gordond5c1a372016-10-31 13:56:23 -0500512 // Check the list of FaceStatus objects we got for a match
513 for (const ndn::nfd::FaceStatus& faceStatus : faces) {
Nick Gordond5c1a372016-10-31 13:56:23 -0500514 // Set the adjacency FaceID if we find a URI match and it was
515 // previously unset. Change the boolean to true.
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500516 if (adjacent.getFaceId() == 0 && faceUriString == faceStatus.getRemoteUri()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500517 NLSR_LOG_DEBUG("FaceUri: " << faceStatus.getRemoteUri() <<
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500518 " FaceId: "<< faceStatus.getFaceId());
519 adjacent.setFaceId(faceStatus.getFaceId());
Nick Gordond5c1a372016-10-31 13:56:23 -0500520 // Register the prefixes for each neighbor
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500521 this->registerAdjacencyPrefixes(adjacent, ndn::time::milliseconds::max());
Nick Gordond5c1a372016-10-31 13:56:23 -0500522 }
523 }
524 // If this adjacency has no information in this dataset, then one
525 // of two things is happening: 1. NFD is starting slowly and this
526 // Face wasn't ready yet, or 2. NFD is configured
527 // incorrectly and this Face isn't available.
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500528 if (adjacent.getFaceId() == 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500529 NLSR_LOG_WARN("The adjacency " << adjacent.getName() <<
Nick Gordond5c1a372016-10-31 13:56:23 -0500530 " has no Face information in this dataset.");
531 }
532 }
533
Nick Gordond5c1a372016-10-31 13:56:23 -0500534 scheduleDatasetFetch();
535}
536
537void
538Nlsr::registerAdjacencyPrefixes(const Adjacent& adj,
539 const ndn::time::milliseconds& timeout)
540{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500541 ndn::FaceUri faceUri = adj.getFaceUri();
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500542 double linkCost = adj.getLinkCost();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500543 const ndn::Name& adjName = adj.getName();
Nick Gordond5c1a372016-10-31 13:56:23 -0500544
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500545 m_fib.registerPrefix(adjName, faceUri, linkCost,
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500546 timeout, ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500547
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500548 m_fib.registerPrefix(m_confParam.getChronosyncPrefix(),
549 faceUri, linkCost, timeout,
550 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500551
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500552 m_fib.registerPrefix(m_confParam.getLsaPrefix(),
553 faceUri, linkCost, timeout,
554 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500555}
556
557void
558Nlsr::onFaceDatasetFetchTimeout(uint32_t code,
559 const std::string& msg,
560 uint32_t nRetriesSoFar)
561{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500562 NLSR_LOG_DEBUG("onFaceDatasetFetchTimeout");
Nick Gordond5c1a372016-10-31 13:56:23 -0500563 // If we have exceeded the maximum attempt count, do not try again.
564 if (nRetriesSoFar++ < m_confParam.getFaceDatasetFetchTries()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500565 NLSR_LOG_DEBUG("Failed to fetch dataset: " << msg << ". Attempting retry #" << nRetriesSoFar);
Nick Gordond5c1a372016-10-31 13:56:23 -0500566 m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(std::bind(&Nlsr::processFaceDataset,
567 this, _1),
568 std::bind(&Nlsr::onFaceDatasetFetchTimeout,
569 this, _1, _2, nRetriesSoFar));
570 }
571 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500572 NLSR_LOG_ERROR("Failed to fetch dataset: " << msg << ". Exceeded limit of " <<
Nick Gordond5c1a372016-10-31 13:56:23 -0500573 m_confParam.getFaceDatasetFetchTries() << ", so not trying again this time.");
574 // If we fail to fetch it, just do nothing until the next
575 // interval. Since this is a backup mechanism, we aren't as
576 // concerned with retrying.
577 scheduleDatasetFetch();
578 }
579}
580
581void
582Nlsr::scheduleDatasetFetch()
583{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500584 NLSR_LOG_DEBUG("Scheduling Dataset Fetch in " << m_confParam.getFaceDatasetFetchInterval());
585
Nick Gordond5c1a372016-10-31 13:56:23 -0500586 m_scheduler.scheduleEvent(m_confParam.getFaceDatasetFetchInterval(),
587 [this] {
588 this->initializeFaces(
589 [this] (const std::vector<ndn::nfd::FaceStatus>& faces) {
590 this->processFaceDataset(faces);
591 },
592 [this] (uint32_t code, const std::string& msg) {
593 this->onFaceDatasetFetchTimeout(code, msg, 0);
594 });
595 });
596}
akmhoquee1765152014-06-30 11:32:01 -0500597
598void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500599Nlsr::enableIncomingFaceIdIndication()
600{
601 NLSR_LOG_DEBUG("Enabling incoming face id indication for local face.");
602
603 m_controller.start<ndn::nfd::FaceUpdateCommand>(
604 ndn::nfd::ControlParameters()
605 .setFlagBit(ndn::nfd::FaceFlagBit::BIT_LOCAL_FIELDS_ENABLED, true),
606 bind(&Nlsr::onFaceIdIndicationSuccess, this, _1),
607 bind(&Nlsr::onFaceIdIndicationFailure, this, _1));
608}
609
610void
611Nlsr::onFaceIdIndicationSuccess(const ndn::nfd::ControlParameters& cp)
612{
613 NLSR_LOG_DEBUG("Successfully enabled incoming face id indication"
614 << "for face id " << cp.getFaceId());
615}
616
617void
618Nlsr::onFaceIdIndicationFailure(const ndn::nfd::ControlResponse& cr)
619{
620 std::ostringstream os;
621 os << "Failed to enable incoming face id indication feature: " <<
622 "(code: " << cr.getCode() << ", reason: " << cr.getText() << ")";
623
624 NLSR_LOG_DEBUG(os.str());
625}
626
627void
akmhoque53353462014-04-22 08:43:45 -0500628Nlsr::startEventLoop()
629{
akmhoquefdbddb12014-05-02 18:35:19 -0500630 m_nlsrFace.processEvents();
akmhoque53353462014-04-22 08:43:45 -0500631}
akmhoque5a44dd42014-03-12 18:11:32 -0500632
akmhoqueb1710aa2014-02-19 17:13:36 -0600633} // namespace nlsr