blob: ab4a073f766aca5c629c48627c45734d523095cc [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
akmhoque674b0b12014-05-20 14:33:28 -050037INIT_LOGGER("nlsr");
38
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_isDaemonProcess(false)
49 , m_configFileName("nlsr.conf")
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050050 , m_nlsrLsdb(*this, scheduler)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050051 , m_adjBuildCount(0)
52 , m_isBuildAdjLsaSheduled(false)
53 , m_isRouteCalculationScheduled(false)
54 , m_isRoutingTableCalculating(false)
55 , m_routingTable(scheduler)
56 , m_fib(m_nlsrFace, scheduler, m_adjacencyList, m_confParam, m_keyChain)
Nick Gordonb7b58392017-08-17 16:29:21 -050057 , m_namePrefixTable(*this, m_routingTable.afterRoutingChange)
laqinfan35731852017-08-08 06:17:39 -050058 , m_dispatcher(m_nlsrFace, m_keyChain)
59 , m_datasetHandler(m_nlsrLsdb,
60 m_routingTable,
61 m_dispatcher,
62 m_nlsrFace,
63 m_keyChain)
Vince Lehmanf7eec4f2015-05-08 19:02:31 -050064 , m_helloProtocol(*this, scheduler)
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050065 , m_validator(ndn::make_unique<ndn::security::v2::CertificateFetcherDirectFetch>(m_nlsrFace))
66 , m_controller(m_nlsrFace, m_keyChain)
Nick Gordond5c1a372016-10-31 13:56:23 -050067 , m_faceDatasetController(m_nlsrFace, m_keyChain)
laqinfan35731852017-08-08 06:17:39 -050068 , m_prefixUpdateProcessor(m_dispatcher,
Laqin Fan54a43f02017-03-08 12:31:30 -060069 m_nlsrFace,
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050070 m_namePrefixList,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050071 m_nlsrLsdb)
laqinfan35731852017-08-08 06:17:39 -050072 , m_nfdRibCommandProcessor(m_dispatcher,
Nick Gordon4d2c6c02017-01-20 13:18:46 -060073 m_namePrefixList,
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050074 m_nlsrLsdb)
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060075 , m_statsCollector(m_nlsrLsdb, m_helloProtocol)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050076 , m_faceMonitor(m_nlsrFace)
77 , m_firstHelloInterval(FIRST_HELLO_INTERVAL_DEFAULT)
78{
dmcoomes9f936662017-03-02 10:33:09 -060079 m_faceMonitor.onNotification.connect(std::bind(&Nlsr::onFaceEventNotification, this, _1));
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050080 m_faceMonitor.start();
81}
82
akmhoque53353462014-04-22 08:43:45 -050083void
84Nlsr::registrationFailed(const ndn::Name& name)
akmhoque298385a2014-02-13 14:13:09 -060085{
dmcoomes5bcb39e2017-10-31 15:07:55 -050086 NLSR_LOG_ERROR("ERROR: Failed to register prefix in local hub's daemon");
dmcoomes9f936662017-03-02 10:33:09 -060087 BOOST_THROW_EXCEPTION(Error("Error: Prefix registration failed"));
akmhoque53353462014-04-22 08:43:45 -050088}
akmhoque1fd8c1e2014-02-19 19:41:49 -060089
akmhoque157b0a42014-05-13 00:26:37 -050090void
91Nlsr::onRegistrationSuccess(const ndn::Name& name)
92{
dmcoomes5bcb39e2017-10-31 15:07:55 -050093 NLSR_LOG_DEBUG("Successfully registered prefix: " << name);
alvy297f4162015-03-03 17:15:33 -060094}
95
96void
akmhoque31d1d4b2014-05-05 22:08:14 -050097Nlsr::setInfoInterestFilter()
akmhoque53353462014-04-22 08:43:45 -050098{
akmhoque31d1d4b2014-05-05 22:08:14 -050099 ndn::Name name(m_confParam.getRouterPrefix());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500100 name.append("NLSR");
101 name.append("INFO");
102
103 NLSR_LOG_DEBUG("Setting interest filter for Hello interest: " << name);
104
akmhoquefdbddb12014-05-02 18:35:19 -0500105 getNlsrFace().setInterestFilter(name,
Joao Pereira97473d42015-07-03 16:57:27 -0400106 std::bind(&HelloProtocol::processInterest,
akmhoque31d1d4b2014-05-05 22:08:14 -0500107 &m_helloProtocol, _1, _2),
Joao Pereira97473d42015-07-03 16:57:27 -0400108 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
109 std::bind(&Nlsr::registrationFailed, this, _1),
110 m_signingInfo,
akmhoque060d3022014-08-12 13:35:06 -0500111 ndn::nfd::ROUTE_FLAG_CAPTURE);
akmhoque31d1d4b2014-05-05 22:08:14 -0500112}
113
114void
115Nlsr::setLsaInterestFilter()
116{
akmhoque157b0a42014-05-13 00:26:37 -0500117 ndn::Name name = m_confParam.getLsaPrefix();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500118
dmcoomes5bcb39e2017-10-31 15:07:55 -0500119 NLSR_LOG_DEBUG("Setting interest filter for LsaPrefix: " << name);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500120
akmhoque31d1d4b2014-05-05 22:08:14 -0500121 getNlsrFace().setInterestFilter(name,
Joao Pereira97473d42015-07-03 16:57:27 -0400122 std::bind(&Lsdb::processInterest,
akmhoque31d1d4b2014-05-05 22:08:14 -0500123 &m_nlsrLsdb, _1, _2),
Joao Pereira97473d42015-07-03 16:57:27 -0400124 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
125 std::bind(&Nlsr::registrationFailed, this, _1),
126 m_signingInfo,
akmhoque060d3022014-08-12 13:35:06 -0500127 ndn::nfd::ROUTE_FLAG_CAPTURE);
akmhoque53353462014-04-22 08:43:45 -0500128}
129
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500130
131void
132Nlsr::addDispatcherTopPrefix(const ndn::Name& topPrefix)
133{
134 try {
laqinfan35731852017-08-08 06:17:39 -0500135 m_dispatcher.addTopPrefix(topPrefix, false, m_signingInfo);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500136 }
137 catch (const std::exception& e) {
138 NLSR_LOG_ERROR("Error setting top-level prefix in dispatcher: " << e.what() << "\n");
139 }
140}
141
akmhoque53353462014-04-22 08:43:45 -0500142void
akmhoquec04e7272014-07-02 11:00:14 -0500143Nlsr::setStrategies()
akmhoque157b0a42014-05-13 00:26:37 -0500144{
Vince Lehman53c0e3e2015-09-14 14:33:20 -0500145 const std::string strategy("ndn:/localhost/nfd/strategy/multicast");
146
akmhoque393d4ff2014-07-16 14:27:03 -0500147 m_fib.setStrategy(m_confParam.getLsaPrefix(), strategy, 0);
akmhoque393d4ff2014-07-16 14:27:03 -0500148 m_fib.setStrategy(m_confParam.getChronosyncPrefix(), strategy, 0);
akmhoque157b0a42014-05-13 00:26:37 -0500149}
150
151void
akmhoque0494c252014-07-23 23:46:44 -0500152Nlsr::daemonize()
153{
154 pid_t process_id = 0;
155 pid_t sid = 0;
156 process_id = fork();
157 if (process_id < 0){
158 std::cerr << "Daemonization failed!" << std::endl;
dmcoomes9f936662017-03-02 10:33:09 -0600159 BOOST_THROW_EXCEPTION(Error("Error: Daemonization process- fork failed!"));
akmhoque0494c252014-07-23 23:46:44 -0500160 }
161 if (process_id > 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500162 NLSR_LOG_DEBUG("Process daemonized. Process id: " << process_id);
akmhoque0494c252014-07-23 23:46:44 -0500163 exit(0);
164 }
165
166 umask(0);
167 sid = setsid();
168 if(sid < 0) {
dmcoomes9f936662017-03-02 10:33:09 -0600169 BOOST_THROW_EXCEPTION(Error("Error: Daemonization process- setting id failed!"));
akmhoque0494c252014-07-23 23:46:44 -0500170 }
171
172 if (chdir("/") < 0) {
dmcoomes9f936662017-03-02 10:33:09 -0600173 BOOST_THROW_EXCEPTION(Error("Error: Daemonization process-chdir failed!"));
akmhoque0494c252014-07-23 23:46:44 -0500174 }
175}
176
177void
Nick Gordon922714a2017-06-13 14:12:02 -0500178Nlsr::canonizeContinuation(std::list<Adjacent>::iterator iterator,
179 std::function<void(void)> finally)
Nick Gordon9461afb2017-04-25 15:54:50 -0500180{
Nick Gordon922714a2017-06-13 14:12:02 -0500181 canonizeNeighborUris(iterator, [this, finally] (std::list<Adjacent>::iterator iterator) {
182 canonizeContinuation(iterator, finally);
183 },
184 finally);
Nick Gordon9461afb2017-04-25 15:54:50 -0500185}
186
187void
188Nlsr::canonizeNeighborUris(std::list<Adjacent>::iterator currentNeighbor,
Nick Gordon922714a2017-06-13 14:12:02 -0500189 std::function<void(std::list<Adjacent>::iterator)> then,
190 std::function<void(void)> finally)
Nick Gordon9461afb2017-04-25 15:54:50 -0500191{
192 if (currentNeighbor != m_adjacencyList.getAdjList().end()) {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500193 ndn::FaceUri uri(currentNeighbor->getFaceUri());
194 uri.canonize([this, then, currentNeighbor] (ndn::FaceUri canonicalUri) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500195 NLSR_LOG_DEBUG("Canonized URI: " << currentNeighbor->getFaceUri()
Nick Gordon9461afb2017-04-25 15:54:50 -0500196 << " to: " << canonicalUri);
Nick Gordone9733ed2017-04-26 10:48:39 -0500197 currentNeighbor->setFaceUri(canonicalUri);
Nick Gordon9461afb2017-04-25 15:54:50 -0500198 then(std::next(currentNeighbor));
199 },
200 [this, then, currentNeighbor] (const std::string& reason) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500201 NLSR_LOG_ERROR("Could not canonize URI: " << currentNeighbor->getFaceUri()
Nick Gordon9461afb2017-04-25 15:54:50 -0500202 << " because: " << reason);
203 then(std::next(currentNeighbor));
204 },
205 m_nlsrFace.getIoService(),
206 TIME_ALLOWED_FOR_CANONIZATION);
207 }
Nick Gordon922714a2017-06-13 14:12:02 -0500208 // We have finished canonizing all neighbors, so call finally()
Nick Gordon9461afb2017-04-25 15:54:50 -0500209 else {
Nick Gordon922714a2017-06-13 14:12:02 -0500210 finally();
Nick Gordon9461afb2017-04-25 15:54:50 -0500211 }
212}
213
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500214
215void
216Nlsr::loadCertToPublish(const ndn::security::v2::Certificate& certificate)
217{
218 m_certStore.insert(certificate);
219 m_validator.loadAnchor("Authoritative-Certificate",
220 ndn::security::v2::Certificate(certificate));
221 m_prefixUpdateProcessor.getValidator().
222 loadAnchor("Authoritative-Certificate",
223 ndn::security::v2::Certificate(certificate));
224}
225
Nick Gordon9461afb2017-04-25 15:54:50 -0500226void
akmhoque53353462014-04-22 08:43:45 -0500227Nlsr::initialize()
228{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500229 NLSR_LOG_DEBUG("Initializing Nlsr");
akmhoque53353462014-04-22 08:43:45 -0500230 m_confParam.buildRouterPrefix();
laqinfan35731852017-08-08 06:17:39 -0500231 m_datasetHandler.setRouterNameCommandPrefix(m_confParam.getRouterPrefix());
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700232 m_nlsrLsdb.setLsaRefreshTime(ndn::time::seconds(m_confParam.getLsaRefreshTime()));
akmhoque31d1d4b2014-05-05 22:08:14 -0500233 m_nlsrLsdb.setThisRouterPrefix(m_confParam.getRouterPrefix().toUri());
akmhoque53353462014-04-22 08:43:45 -0500234 m_fib.setEntryRefreshTime(2 * m_confParam.getLsaRefreshTime());
Vince Lehmanc11cc202015-01-20 11:41:33 -0600235
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500236 m_nlsrLsdb.getSequencingManager().setSeqFileDirectory(m_confParam.getSeqFileDir());
237 m_nlsrLsdb.getSequencingManager().initiateSeqNoFromFile(m_confParam.getHyperbolicState());
238
239 m_nlsrLsdb.getSyncLogicHandler().createSyncSocket(m_confParam.getChronosyncPrefix());
Vince Lehmanc11cc202015-01-20 11:41:33 -0600240
dmcoomes9f936662017-03-02 10:33:09 -0600241 // Logging start
akmhoque674b0b12014-05-20 14:33:28 -0500242 m_confParam.writeLog();
243 m_adjacencyList.writeLog();
dmcoomes5bcb39e2017-10-31 15:07:55 -0500244 NLSR_LOG_DEBUG(m_namePrefixList);
dmcoomes9f936662017-03-02 10:33:09 -0600245 // Logging end
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500246
akmhoque443ad812014-07-29 10:26:56 -0500247 initializeKey();
akmhoquec04e7272014-07-02 11:00:14 -0500248 setStrategies();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500249
dmcoomes5bcb39e2017-10-31 15:07:55 -0500250 NLSR_LOG_DEBUG("Default NLSR identity: " << m_signingInfo.getSignerName());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500251
akmhoque31d1d4b2014-05-05 22:08:14 -0500252 setInfoInterestFilter();
253 setLsaInterestFilter();
Vince Lehman50df6b72015-03-03 12:06:40 -0600254
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500255 // add top-level prefixes: router and localhost prefix
256 addDispatcherTopPrefix(m_confParam.getRouterPrefix());
257 addDispatcherTopPrefix(LOCALHOST_PREFIX);
258
Nick Gordond5c1a372016-10-31 13:56:23 -0500259 initializeFaces(std::bind(&Nlsr::processFaceDataset, this, _1),
260 std::bind(&Nlsr::onFaceDatasetFetchTimeout, this, _1, _2, 0));
261
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500262 enableIncomingFaceIdIndication();
263
Vince Lehman50df6b72015-03-03 12:06:40 -0600264 // Set event intervals
265 setFirstHelloInterval(m_confParam.getFirstHelloInterval());
266 m_nlsrLsdb.setAdjLsaBuildInterval(m_confParam.getAdjLsaBuildInterval());
267 m_routingTable.setRoutingCalcInterval(m_confParam.getRoutingCalcInterval());
268
akmhoque674b0b12014-05-20 14:33:28 -0500269 m_nlsrLsdb.buildAndInstallOwnNameLsa();
Nick Gordon5c467f02016-07-13 13:40:10 -0500270
271 // Install coordinate LSAs if using HR or dry-run HR.
272 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
273 m_nlsrLsdb.buildAndInstallOwnCoordinateLsa();
274 }
Vince Lehman904c2412014-09-23 19:36:11 -0500275
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700276 registerKeyPrefix();
alvy297f4162015-03-03 17:15:33 -0600277 registerLocalhostPrefix();
Vince Lehman7b616582014-10-17 16:25:39 -0500278
Vince Lehman7b616582014-10-17 16:25:39 -0500279 m_helloProtocol.scheduleInterest(m_firstHelloInterval);
Vince Lehman09131122014-09-09 17:10:11 -0500280
281 // Need to set direct neighbors' costs to 0 for hyperbolic routing
282 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
283
284 std::list<Adjacent>& neighbors = m_adjacencyList.getAdjList();
285
286 for (std::list<Adjacent>::iterator it = neighbors.begin(); it != neighbors.end(); ++it) {
287 it->setLinkCost(0);
288 }
289 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700290}
291
292void
akmhoque443ad812014-07-29 10:26:56 -0500293Nlsr::initializeKey()
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700294{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500295 NLSR_LOG_DEBUG("Initializing Key ...");
296
297 ndn::Name nlsrInstanceName = m_confParam.getRouterPrefix();
298 nlsrInstanceName.append("NLSR");
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700299
Joao Pereira97473d42015-07-03 16:57:27 -0400300 try {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500301 m_keyChain.deleteIdentity(m_keyChain.getPib().getIdentity(nlsrInstanceName));
302 } catch (const std::exception& e) {
303 NLSR_LOG_WARN(e.what());
304 }
305
306 auto nlsrInstanceIdentity = m_keyChain.createIdentity(nlsrInstanceName);
307 auto nlsrInstanceKey = nlsrInstanceIdentity.getDefaultKey();
308
309 ndn::security::v2::Certificate certificate;
310
311 ndn::Name certificateName = nlsrInstanceKey.getName();
312 certificateName.append("NA");
313 certificateName.appendVersion();
314 certificate.setName(certificateName);
315
316 // set metainfo
317 certificate.setContentType(ndn::tlv::ContentType_Key);
318 certificate.setFreshnessPeriod(ndn::time::days(7300));
319
320 // set content
321 certificate.setContent(nlsrInstanceKey.getPublicKey().data(), nlsrInstanceKey.getPublicKey().size());
322
323 // set signature-info
324 ndn::SignatureInfo signatureInfo;
325 signatureInfo.setValidityPeriod(ndn::security::ValidityPeriod(ndn::time::system_clock::TimePoint(),
326 ndn::time::system_clock::now()
327 + ndn::time::days(20 * 365)));
328 try {
329 m_keyChain.sign(certificate,
330 ndn::security::SigningInfo(m_keyChain.getPib().getIdentity(m_confParam.getRouterPrefix()))
331 .setSignatureInfo(signatureInfo));
akmhoque102aea42014-08-04 10:22:12 -0500332 }
dmcoomes9f936662017-03-02 10:33:09 -0600333 catch (const std::exception& e) {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500334 NLSR_LOG_WARN("ERROR: Router's " << e.what()
335 << "NLSR is running without security."
336 << " If security is enabled NLSR will not converge.");
337
338 std::cerr << "Router's " << e.what() << "NLSR is running without security "
339 << "(Only for testing, should not be used in production.)"
340 << " If security is enabled NLSR will not converge." << std::endl;
akmhoque102aea42014-08-04 10:22:12 -0500341 }
akmhoque443ad812014-07-29 10:26:56 -0500342
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500343 m_signingInfo = ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_ID,
344 nlsrInstanceName);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700345
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700346 loadCertToPublish(certificate);
347
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500348 m_defaultCertName = certificate.getName();
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700349}
350
351void
352Nlsr::registerKeyPrefix()
353{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500354 // Start listening for the interest of this router's NLSR certificate
355 ndn::Name nlsrKeyPrefix = getConfParameter().getRouterPrefix();
356 nlsrKeyPrefix.append("NLSR");
357 nlsrKeyPrefix.append("KEY");
358
359 m_nlsrFace.setInterestFilter(nlsrKeyPrefix,
Joao Pereira97473d42015-07-03 16:57:27 -0400360 std::bind(&Nlsr::onKeyInterest,
Yingdi Yu6a3a4dd2014-06-20 14:10:39 -0700361 this, _1, _2),
Joao Pereira97473d42015-07-03 16:57:27 -0400362 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
363 std::bind(&Nlsr::registrationFailed, this, _1),
364 m_signingInfo,
akmhoque060d3022014-08-12 13:35:06 -0500365 ndn::nfd::ROUTE_FLAG_CAPTURE);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700366
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500367 // Start listening for the interest of this router's certificate
368 ndn::Name routerKeyPrefix = getConfParameter().getRouterPrefix();
369 routerKeyPrefix.append("KEY");
370
371 m_nlsrFace.setInterestFilter(routerKeyPrefix,
372 std::bind(&Nlsr::onKeyInterest,
373 this, _1, _2),
374 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
375 std::bind(&Nlsr::registrationFailed, this, _1),
376 m_signingInfo,
377 ndn::nfd::ROUTE_FLAG_CAPTURE);
378
379 // Start listening for the interest of this router's operator's certificate
380 ndn::Name operatorKeyPrefix = getConfParameter().getNetwork();
381 operatorKeyPrefix.append(getConfParameter().getSiteName());
382 operatorKeyPrefix.append(std::string("%C1.Operator"));
383
384 m_nlsrFace.setInterestFilter(operatorKeyPrefix,
385 std::bind(&Nlsr::onKeyInterest,
386 this, _1, _2),
387 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
388 std::bind(&Nlsr::registrationFailed, this, _1),
389 m_signingInfo,
390 ndn::nfd::ROUTE_FLAG_CAPTURE);
391
392 // Start listening for the interest of this router's site's certificate
393 ndn::Name siteKeyPrefix = getConfParameter().getNetwork();
394 siteKeyPrefix.append(getConfParameter().getSiteName());
395 siteKeyPrefix.append("KEY");
396
397 m_nlsrFace.setInterestFilter(siteKeyPrefix,
398 std::bind(&Nlsr::onKeyInterest,
399 this, _1, _2),
400 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
401 std::bind(&Nlsr::registrationFailed, this, _1),
402 m_signingInfo,
403 ndn::nfd::ROUTE_FLAG_CAPTURE);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700404}
405
406void
alvy297f4162015-03-03 17:15:33 -0600407Nlsr::registerLocalhostPrefix()
408{
alvy297f4162015-03-03 17:15:33 -0600409 m_nlsrFace.registerPrefix(LOCALHOST_PREFIX,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500410 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
alvy297f4162015-03-03 17:15:33 -0600411 std::bind(&Nlsr::registrationFailed, this, _1));
412}
413
414void
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700415Nlsr::onKeyInterest(const ndn::Name& name, const ndn::Interest& interest)
416{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500417 NLSR_LOG_DEBUG("Got interest for certificate. Interest: " << interest.getName());
418
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700419 const ndn::Name& interestName = interest.getName();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500420 const ndn::security::v2::Certificate* cert = getCertificate(interestName);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700421
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500422 if (cert == nullptr) {
423 NLSR_LOG_DEBUG("Certificate is not found for: " << interest);
dmcoomes9eaf3f42017-02-21 11:39:01 -0600424 return; // cert is not found
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500425 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700426
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500427 m_nlsrFace.put(*cert);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700428}
429
430void
431Nlsr::onKeyPrefixRegSuccess(const ndn::Name& name)
432{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500433 NLSR_LOG_DEBUG("KEY prefix: " << name << " registration is successful.");
akmhoque53353462014-04-22 08:43:45 -0500434}
akmhoque5a44dd42014-03-12 18:11:32 -0500435
akmhoque53353462014-04-22 08:43:45 -0500436void
akmhoquec04e7272014-07-02 11:00:14 -0500437Nlsr::onFaceEventNotification(const ndn::nfd::FaceEventNotification& faceEventNotification)
akmhoquee1765152014-06-30 11:32:01 -0500438{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500439 NLSR_LOG_TRACE("Nlsr::onFaceEventNotification called");
Vince Lehman02e32992015-03-11 12:31:20 -0500440
Nick Gordond5c1a372016-10-31 13:56:23 -0500441 switch (faceEventNotification.getKind()) {
442 case ndn::nfd::FACE_EVENT_DESTROYED: {
443 uint64_t faceId = faceEventNotification.getFaceId();
Vince Lehman02e32992015-03-11 12:31:20 -0500444
Nick Gordond5c1a372016-10-31 13:56:23 -0500445 auto adjacent = m_adjacencyList.findAdjacent(faceId);
Vince Lehman02e32992015-03-11 12:31:20 -0500446
Nick Gordond5c1a372016-10-31 13:56:23 -0500447 if (adjacent != m_adjacencyList.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500448 NLSR_LOG_DEBUG("Face to " << adjacent->getName() << " with face id: " << faceId << " destroyed");
Vince Lehman02e32992015-03-11 12:31:20 -0500449
Nick Gordond5c1a372016-10-31 13:56:23 -0500450 adjacent->setFaceId(0);
Vince Lehman02e32992015-03-11 12:31:20 -0500451
Nick Gordond5c1a372016-10-31 13:56:23 -0500452 // Only trigger an Adjacency LSA build if this node is changing
453 // from ACTIVE to INACTIVE since this rebuild will effectively
454 // cancel the previous Adjacency LSA refresh event and schedule
455 // a new one further in the future.
456 //
457 // Continuously scheduling the refresh in the future will block
458 // the router from refreshing its Adjacency LSA. Since other
459 // routers' Name prefixes' expiration times are updated when
460 // this router refreshes its Adjacency LSA, the other routers'
461 // prefixes will expire and be removed from the RIB.
462 //
463 // This check is required to fix Bug #2733 for now. This check
464 // would be unnecessary to fix Bug #2733 when Issue #2732 is
465 // completed, but the check also helps with optimization so it
466 // can remain even when Issue #2732 is implemented.
467 if (adjacent->getStatus() == Adjacent::STATUS_ACTIVE) {
468 adjacent->setStatus(Adjacent::STATUS_INACTIVE);
Vince Lehman02e32992015-03-11 12:31:20 -0500469
Nick Gordond5c1a372016-10-31 13:56:23 -0500470 // A new adjacency LSA cannot be built until the neighbor is marked INACTIVE and
471 // has met the HELLO retry threshold
472 adjacent->setInterestTimedOutNo(m_confParam.getInterestRetryNumber());
Vince Lehman199e9cf2015-04-07 13:22:16 -0500473
Nick Gordond5c1a372016-10-31 13:56:23 -0500474 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
475 getRoutingTable().scheduleRoutingTableCalculation(*this);
476 }
477 else {
478 m_nlsrLsdb.scheduleAdjLsaBuild();
479 }
Nick Gordone8e03ac2016-07-07 14:24:38 -0500480 }
Vince Lehman199e9cf2015-04-07 13:22:16 -0500481 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500482 break;
akmhoquec04e7272014-07-02 11:00:14 -0500483 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500484 case ndn::nfd::FACE_EVENT_CREATED: {
485 // Find the neighbor in our adjacency list
Nick Gordond5c1a372016-10-31 13:56:23 -0500486 auto adjacent = m_adjacencyList.findAdjacent(
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500487 ndn::FaceUri(faceEventNotification.getRemoteUri()));
Nick Gordond5c1a372016-10-31 13:56:23 -0500488 // If we have a neighbor by that FaceUri and it has no FaceId, we
489 // have a match.
490 if (adjacent != m_adjacencyList.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500491 NLSR_LOG_DEBUG("Face creation event matches neighbor: " << adjacent->getName()
Nick Gordond5c1a372016-10-31 13:56:23 -0500492 << ". New Face ID: " << faceEventNotification.getFaceId()
493 << ". Registering prefixes.");
494 adjacent->setFaceId(faceEventNotification.getFaceId());
495
496 registerAdjacencyPrefixes(*adjacent, ndn::time::milliseconds::max());
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500497
498 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
499 getRoutingTable().scheduleRoutingTableCalculation(*this);
500 }
501 else {
502 m_nlsrLsdb.scheduleAdjLsaBuild();
503 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500504 }
505 break;
506 }
507 default:
508 break;
akmhoquec04e7272014-07-02 11:00:14 -0500509 }
akmhoquee1765152014-06-30 11:32:01 -0500510}
511
Nick Gordond5c1a372016-10-31 13:56:23 -0500512void
513Nlsr::initializeFaces(const FetchDatasetCallback& onFetchSuccess,
514 const FetchDatasetTimeoutCallback& onFetchFailure)
515{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500516 NLSR_LOG_TRACE("Initializing Faces...");
Nick Gordond5c1a372016-10-31 13:56:23 -0500517
518 m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(onFetchSuccess, onFetchFailure);
519
520}
521
522void
523Nlsr::processFaceDataset(const std::vector<ndn::nfd::FaceStatus>& faces)
524{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500525 NLSR_LOG_DEBUG("Processing face dataset");
Nick Gordond5c1a372016-10-31 13:56:23 -0500526
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500527 // Iterate over each neighbor listed in nlsr.conf
528 for (auto& adjacent : m_adjacencyList.getAdjList()) {
529
530 const std::string faceUriString = adjacent.getFaceUri().toString();
Nick Gordond5c1a372016-10-31 13:56:23 -0500531 // Check the list of FaceStatus objects we got for a match
532 for (const ndn::nfd::FaceStatus& faceStatus : faces) {
Nick Gordond5c1a372016-10-31 13:56:23 -0500533 // Set the adjacency FaceID if we find a URI match and it was
534 // previously unset. Change the boolean to true.
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500535 if (adjacent.getFaceId() == 0 && faceUriString == faceStatus.getRemoteUri()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500536 NLSR_LOG_DEBUG("FaceUri: " << faceStatus.getRemoteUri() <<
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500537 " FaceId: "<< faceStatus.getFaceId());
538 adjacent.setFaceId(faceStatus.getFaceId());
Nick Gordond5c1a372016-10-31 13:56:23 -0500539 // Register the prefixes for each neighbor
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500540 this->registerAdjacencyPrefixes(adjacent, ndn::time::milliseconds::max());
Nick Gordond5c1a372016-10-31 13:56:23 -0500541 }
542 }
543 // If this adjacency has no information in this dataset, then one
544 // of two things is happening: 1. NFD is starting slowly and this
545 // Face wasn't ready yet, or 2. NFD is configured
546 // incorrectly and this Face isn't available.
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500547 if (adjacent.getFaceId() == 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500548 NLSR_LOG_WARN("The adjacency " << adjacent.getName() <<
Nick Gordond5c1a372016-10-31 13:56:23 -0500549 " has no Face information in this dataset.");
550 }
551 }
552
Nick Gordond5c1a372016-10-31 13:56:23 -0500553 scheduleDatasetFetch();
554}
555
556void
557Nlsr::registerAdjacencyPrefixes(const Adjacent& adj,
558 const ndn::time::milliseconds& timeout)
559{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500560 ndn::FaceUri faceUri = adj.getFaceUri();
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500561 double linkCost = adj.getLinkCost();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500562 const ndn::Name& adjName = adj.getName();
Nick Gordond5c1a372016-10-31 13:56:23 -0500563
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500564 m_fib.registerPrefix(adjName, faceUri, linkCost,
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500565 timeout, ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500566
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500567 m_fib.registerPrefix(m_confParam.getChronosyncPrefix(),
568 faceUri, linkCost, timeout,
569 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500570
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500571 m_fib.registerPrefix(m_confParam.getLsaPrefix(),
572 faceUri, linkCost, timeout,
573 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500574}
575
576void
577Nlsr::onFaceDatasetFetchTimeout(uint32_t code,
578 const std::string& msg,
579 uint32_t nRetriesSoFar)
580{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500581 NLSR_LOG_DEBUG("onFaceDatasetFetchTimeout");
Nick Gordond5c1a372016-10-31 13:56:23 -0500582 // If we have exceeded the maximum attempt count, do not try again.
583 if (nRetriesSoFar++ < m_confParam.getFaceDatasetFetchTries()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500584 NLSR_LOG_DEBUG("Failed to fetch dataset: " << msg << ". Attempting retry #" << nRetriesSoFar);
Nick Gordond5c1a372016-10-31 13:56:23 -0500585 m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(std::bind(&Nlsr::processFaceDataset,
586 this, _1),
587 std::bind(&Nlsr::onFaceDatasetFetchTimeout,
588 this, _1, _2, nRetriesSoFar));
589 }
590 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500591 NLSR_LOG_ERROR("Failed to fetch dataset: " << msg << ". Exceeded limit of " <<
Nick Gordond5c1a372016-10-31 13:56:23 -0500592 m_confParam.getFaceDatasetFetchTries() << ", so not trying again this time.");
593 // If we fail to fetch it, just do nothing until the next
594 // interval. Since this is a backup mechanism, we aren't as
595 // concerned with retrying.
596 scheduleDatasetFetch();
597 }
598}
599
600void
601Nlsr::scheduleDatasetFetch()
602{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500603 NLSR_LOG_DEBUG("Scheduling Dataset Fetch in " << m_confParam.getFaceDatasetFetchInterval());
604
Nick Gordond5c1a372016-10-31 13:56:23 -0500605 m_scheduler.scheduleEvent(m_confParam.getFaceDatasetFetchInterval(),
606 [this] {
607 this->initializeFaces(
608 [this] (const std::vector<ndn::nfd::FaceStatus>& faces) {
609 this->processFaceDataset(faces);
610 },
611 [this] (uint32_t code, const std::string& msg) {
612 this->onFaceDatasetFetchTimeout(code, msg, 0);
613 });
614 });
615}
akmhoquee1765152014-06-30 11:32:01 -0500616
617void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500618Nlsr::enableIncomingFaceIdIndication()
619{
620 NLSR_LOG_DEBUG("Enabling incoming face id indication for local face.");
621
622 m_controller.start<ndn::nfd::FaceUpdateCommand>(
623 ndn::nfd::ControlParameters()
624 .setFlagBit(ndn::nfd::FaceFlagBit::BIT_LOCAL_FIELDS_ENABLED, true),
625 bind(&Nlsr::onFaceIdIndicationSuccess, this, _1),
626 bind(&Nlsr::onFaceIdIndicationFailure, this, _1));
627}
628
629void
630Nlsr::onFaceIdIndicationSuccess(const ndn::nfd::ControlParameters& cp)
631{
632 NLSR_LOG_DEBUG("Successfully enabled incoming face id indication"
633 << "for face id " << cp.getFaceId());
634}
635
636void
637Nlsr::onFaceIdIndicationFailure(const ndn::nfd::ControlResponse& cr)
638{
639 std::ostringstream os;
640 os << "Failed to enable incoming face id indication feature: " <<
641 "(code: " << cr.getCode() << ", reason: " << cr.getText() << ")";
642
643 NLSR_LOG_DEBUG(os.str());
644}
645
646void
akmhoque53353462014-04-22 08:43:45 -0500647Nlsr::startEventLoop()
648{
akmhoquefdbddb12014-05-02 18:35:19 -0500649 m_nlsrFace.processEvents();
akmhoque53353462014-04-22 08:43:45 -0500650}
akmhoque5a44dd42014-03-12 18:11:32 -0500651
akmhoqueb1710aa2014-02-19 17:13:36 -0600652} // namespace nlsr