blob: 296c5e066fc5c66670aae49a145c25df81e8c4b7 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonfeae5572017-01-13 12:06:26 -06003 * Copyright (c) 2014-2017, 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)
laqinfand22da512017-05-25 17:29:53 -050058 , m_localhostDispatcher(m_nlsrFace, m_keyChain)
59 , m_routerNameDispatcher(m_nlsrFace, m_keyChain)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050060 , m_lsdbDatasetHandler(m_nlsrLsdb,
laqinfand22da512017-05-25 17:29:53 -050061 m_localhostDispatcher,
62 m_routerNameDispatcher,
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050063 m_nlsrFace,
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050064 m_keyChain)
Vince Lehmanf7eec4f2015-05-08 19:02:31 -050065 , m_helloProtocol(*this, scheduler)
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050066 , m_validator(ndn::make_unique<ndn::security::v2::CertificateFetcherDirectFetch>(m_nlsrFace))
67 , m_controller(m_nlsrFace, m_keyChain)
Nick Gordond5c1a372016-10-31 13:56:23 -050068 , m_faceDatasetController(m_nlsrFace, m_keyChain)
Laqin Fan54a43f02017-03-08 12:31:30 -060069 , m_prefixUpdateProcessor(m_localhostDispatcher,
70 m_nlsrFace,
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050071 m_namePrefixList,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050072 m_nlsrLsdb)
laqinfand22da512017-05-25 17:29:53 -050073 , m_nfdRibCommandProcessor(m_localhostDispatcher,
Nick Gordon4d2c6c02017-01-20 13:18:46 -060074 m_namePrefixList,
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050075 m_nlsrLsdb)
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -060076 , m_statsCollector(m_nlsrLsdb, m_helloProtocol)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050077 , m_faceMonitor(m_nlsrFace)
78 , m_firstHelloInterval(FIRST_HELLO_INTERVAL_DEFAULT)
79{
dmcoomes9f936662017-03-02 10:33:09 -060080 m_faceMonitor.onNotification.connect(std::bind(&Nlsr::onFaceEventNotification, this, _1));
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050081 m_faceMonitor.start();
82}
83
akmhoque53353462014-04-22 08:43:45 -050084void
85Nlsr::registrationFailed(const ndn::Name& name)
akmhoque298385a2014-02-13 14:13:09 -060086{
dmcoomes5bcb39e2017-10-31 15:07:55 -050087 NLSR_LOG_ERROR("ERROR: Failed to register prefix in local hub's daemon");
dmcoomes9f936662017-03-02 10:33:09 -060088 BOOST_THROW_EXCEPTION(Error("Error: Prefix registration failed"));
akmhoque53353462014-04-22 08:43:45 -050089}
akmhoque1fd8c1e2014-02-19 19:41:49 -060090
akmhoque157b0a42014-05-13 00:26:37 -050091void
92Nlsr::onRegistrationSuccess(const ndn::Name& name)
93{
dmcoomes5bcb39e2017-10-31 15:07:55 -050094 NLSR_LOG_DEBUG("Successfully registered prefix: " << name);
alvy297f4162015-03-03 17:15:33 -060095}
96
97void
akmhoque31d1d4b2014-05-05 22:08:14 -050098Nlsr::setInfoInterestFilter()
akmhoque53353462014-04-22 08:43:45 -050099{
akmhoque31d1d4b2014-05-05 22:08:14 -0500100 ndn::Name name(m_confParam.getRouterPrefix());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500101 name.append("NLSR");
102 name.append("INFO");
103
104 NLSR_LOG_DEBUG("Setting interest filter for Hello interest: " << name);
105
akmhoquefdbddb12014-05-02 18:35:19 -0500106 getNlsrFace().setInterestFilter(name,
Joao Pereira97473d42015-07-03 16:57:27 -0400107 std::bind(&HelloProtocol::processInterest,
akmhoque31d1d4b2014-05-05 22:08:14 -0500108 &m_helloProtocol, _1, _2),
Joao Pereira97473d42015-07-03 16:57:27 -0400109 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
110 std::bind(&Nlsr::registrationFailed, this, _1),
111 m_signingInfo,
akmhoque060d3022014-08-12 13:35:06 -0500112 ndn::nfd::ROUTE_FLAG_CAPTURE);
akmhoque31d1d4b2014-05-05 22:08:14 -0500113}
114
115void
116Nlsr::setLsaInterestFilter()
117{
akmhoque157b0a42014-05-13 00:26:37 -0500118 ndn::Name name = m_confParam.getLsaPrefix();
akmhoque50125a92014-06-30 08:54:17 -0500119 name.append(m_confParam.getSiteName());
120 name.append(m_confParam.getRouterName());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500121
dmcoomes5bcb39e2017-10-31 15:07:55 -0500122 NLSR_LOG_DEBUG("Setting interest filter for LsaPrefix: " << name);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500123
akmhoque31d1d4b2014-05-05 22:08:14 -0500124 getNlsrFace().setInterestFilter(name,
Joao Pereira97473d42015-07-03 16:57:27 -0400125 std::bind(&Lsdb::processInterest,
akmhoque31d1d4b2014-05-05 22:08:14 -0500126 &m_nlsrLsdb, _1, _2),
Joao Pereira97473d42015-07-03 16:57:27 -0400127 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
128 std::bind(&Nlsr::registrationFailed, this, _1),
129 m_signingInfo,
akmhoque060d3022014-08-12 13:35:06 -0500130 ndn::nfd::ROUTE_FLAG_CAPTURE);
akmhoque53353462014-04-22 08:43:45 -0500131}
132
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500133
134void
135Nlsr::addDispatcherTopPrefix(const ndn::Name& topPrefix)
136{
137 try {
138 if (topPrefix.equals(m_confParam.getRouterPrefix())) {
139 m_routerNameDispatcher.addTopPrefix(topPrefix, false, m_signingInfo);
140 }
141 else {
142 m_localhostDispatcher.addTopPrefix(topPrefix, false, m_signingInfo);
143 }
144 }
145 catch (const std::exception& e) {
146 NLSR_LOG_ERROR("Error setting top-level prefix in dispatcher: " << e.what() << "\n");
147 }
148}
149
akmhoque53353462014-04-22 08:43:45 -0500150void
akmhoquec04e7272014-07-02 11:00:14 -0500151Nlsr::setStrategies()
akmhoque157b0a42014-05-13 00:26:37 -0500152{
Vince Lehman53c0e3e2015-09-14 14:33:20 -0500153 const std::string strategy("ndn:/localhost/nfd/strategy/multicast");
154
akmhoque393d4ff2014-07-16 14:27:03 -0500155 m_fib.setStrategy(m_confParam.getLsaPrefix(), strategy, 0);
akmhoque393d4ff2014-07-16 14:27:03 -0500156 m_fib.setStrategy(m_confParam.getChronosyncPrefix(), strategy, 0);
akmhoque157b0a42014-05-13 00:26:37 -0500157}
158
159void
akmhoque0494c252014-07-23 23:46:44 -0500160Nlsr::daemonize()
161{
162 pid_t process_id = 0;
163 pid_t sid = 0;
164 process_id = fork();
165 if (process_id < 0){
166 std::cerr << "Daemonization failed!" << std::endl;
dmcoomes9f936662017-03-02 10:33:09 -0600167 BOOST_THROW_EXCEPTION(Error("Error: Daemonization process- fork failed!"));
akmhoque0494c252014-07-23 23:46:44 -0500168 }
169 if (process_id > 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500170 NLSR_LOG_DEBUG("Process daemonized. Process id: " << process_id);
akmhoque0494c252014-07-23 23:46:44 -0500171 exit(0);
172 }
173
174 umask(0);
175 sid = setsid();
176 if(sid < 0) {
dmcoomes9f936662017-03-02 10:33:09 -0600177 BOOST_THROW_EXCEPTION(Error("Error: Daemonization process- setting id failed!"));
akmhoque0494c252014-07-23 23:46:44 -0500178 }
179
180 if (chdir("/") < 0) {
dmcoomes9f936662017-03-02 10:33:09 -0600181 BOOST_THROW_EXCEPTION(Error("Error: Daemonization process-chdir failed!"));
akmhoque0494c252014-07-23 23:46:44 -0500182 }
183}
184
185void
Nick Gordon922714a2017-06-13 14:12:02 -0500186Nlsr::canonizeContinuation(std::list<Adjacent>::iterator iterator,
187 std::function<void(void)> finally)
Nick Gordon9461afb2017-04-25 15:54:50 -0500188{
Nick Gordon922714a2017-06-13 14:12:02 -0500189 canonizeNeighborUris(iterator, [this, finally] (std::list<Adjacent>::iterator iterator) {
190 canonizeContinuation(iterator, finally);
191 },
192 finally);
Nick Gordon9461afb2017-04-25 15:54:50 -0500193}
194
195void
196Nlsr::canonizeNeighborUris(std::list<Adjacent>::iterator currentNeighbor,
Nick Gordon922714a2017-06-13 14:12:02 -0500197 std::function<void(std::list<Adjacent>::iterator)> then,
198 std::function<void(void)> finally)
Nick Gordon9461afb2017-04-25 15:54:50 -0500199{
200 if (currentNeighbor != m_adjacencyList.getAdjList().end()) {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500201 ndn::FaceUri uri(currentNeighbor->getFaceUri());
202 uri.canonize([this, then, currentNeighbor] (ndn::FaceUri canonicalUri) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500203 NLSR_LOG_DEBUG("Canonized URI: " << currentNeighbor->getFaceUri()
Nick Gordon9461afb2017-04-25 15:54:50 -0500204 << " to: " << canonicalUri);
Nick Gordone9733ed2017-04-26 10:48:39 -0500205 currentNeighbor->setFaceUri(canonicalUri);
Nick Gordon9461afb2017-04-25 15:54:50 -0500206 then(std::next(currentNeighbor));
207 },
208 [this, then, currentNeighbor] (const std::string& reason) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500209 NLSR_LOG_ERROR("Could not canonize URI: " << currentNeighbor->getFaceUri()
Nick Gordon9461afb2017-04-25 15:54:50 -0500210 << " because: " << reason);
211 then(std::next(currentNeighbor));
212 },
213 m_nlsrFace.getIoService(),
214 TIME_ALLOWED_FOR_CANONIZATION);
215 }
Nick Gordon922714a2017-06-13 14:12:02 -0500216 // We have finished canonizing all neighbors, so call finally()
Nick Gordon9461afb2017-04-25 15:54:50 -0500217 else {
Nick Gordon922714a2017-06-13 14:12:02 -0500218 finally();
Nick Gordon9461afb2017-04-25 15:54:50 -0500219 }
220}
221
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500222
223void
224Nlsr::loadCertToPublish(const ndn::security::v2::Certificate& certificate)
225{
226 m_certStore.insert(certificate);
227 m_validator.loadAnchor("Authoritative-Certificate",
228 ndn::security::v2::Certificate(certificate));
229 m_prefixUpdateProcessor.getValidator().
230 loadAnchor("Authoritative-Certificate",
231 ndn::security::v2::Certificate(certificate));
232}
233
Nick Gordon9461afb2017-04-25 15:54:50 -0500234void
akmhoque53353462014-04-22 08:43:45 -0500235Nlsr::initialize()
236{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500237 NLSR_LOG_DEBUG("Initializing Nlsr");
akmhoque53353462014-04-22 08:43:45 -0500238 m_confParam.buildRouterPrefix();
Muktadir R Chowdhury3ac07282016-06-17 16:30:29 -0500239 m_lsdbDatasetHandler.setRouterNameCommandPrefix(m_confParam.getRouterPrefix());
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700240 m_nlsrLsdb.setLsaRefreshTime(ndn::time::seconds(m_confParam.getLsaRefreshTime()));
akmhoque31d1d4b2014-05-05 22:08:14 -0500241 m_nlsrLsdb.setThisRouterPrefix(m_confParam.getRouterPrefix().toUri());
akmhoque53353462014-04-22 08:43:45 -0500242 m_fib.setEntryRefreshTime(2 * m_confParam.getLsaRefreshTime());
Vince Lehmanc11cc202015-01-20 11:41:33 -0600243
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500244 m_nlsrLsdb.getSequencingManager().setSeqFileDirectory(m_confParam.getSeqFileDir());
245 m_nlsrLsdb.getSequencingManager().initiateSeqNoFromFile(m_confParam.getHyperbolicState());
246
247 m_nlsrLsdb.getSyncLogicHandler().createSyncSocket(m_confParam.getChronosyncPrefix());
Vince Lehmanc11cc202015-01-20 11:41:33 -0600248
dmcoomes9f936662017-03-02 10:33:09 -0600249 // Logging start
akmhoque674b0b12014-05-20 14:33:28 -0500250 m_confParam.writeLog();
251 m_adjacencyList.writeLog();
dmcoomes5bcb39e2017-10-31 15:07:55 -0500252 NLSR_LOG_DEBUG(m_namePrefixList);
dmcoomes9f936662017-03-02 10:33:09 -0600253 // Logging end
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500254
akmhoque443ad812014-07-29 10:26:56 -0500255 initializeKey();
akmhoquec04e7272014-07-02 11:00:14 -0500256 setStrategies();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500257
dmcoomes5bcb39e2017-10-31 15:07:55 -0500258 NLSR_LOG_DEBUG("Default NLSR identity: " << m_signingInfo.getSignerName());
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500259
akmhoque31d1d4b2014-05-05 22:08:14 -0500260 setInfoInterestFilter();
261 setLsaInterestFilter();
Vince Lehman50df6b72015-03-03 12:06:40 -0600262
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500263 // add top-level prefixes: router and localhost prefix
264 addDispatcherTopPrefix(m_confParam.getRouterPrefix());
265 addDispatcherTopPrefix(LOCALHOST_PREFIX);
266
Nick Gordond5c1a372016-10-31 13:56:23 -0500267 initializeFaces(std::bind(&Nlsr::processFaceDataset, this, _1),
268 std::bind(&Nlsr::onFaceDatasetFetchTimeout, this, _1, _2, 0));
269
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500270 enableIncomingFaceIdIndication();
271
Vince Lehman50df6b72015-03-03 12:06:40 -0600272 // Set event intervals
273 setFirstHelloInterval(m_confParam.getFirstHelloInterval());
274 m_nlsrLsdb.setAdjLsaBuildInterval(m_confParam.getAdjLsaBuildInterval());
275 m_routingTable.setRoutingCalcInterval(m_confParam.getRoutingCalcInterval());
276
akmhoque674b0b12014-05-20 14:33:28 -0500277 m_nlsrLsdb.buildAndInstallOwnNameLsa();
Nick Gordon5c467f02016-07-13 13:40:10 -0500278
279 // Install coordinate LSAs if using HR or dry-run HR.
280 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
281 m_nlsrLsdb.buildAndInstallOwnCoordinateLsa();
282 }
Vince Lehman904c2412014-09-23 19:36:11 -0500283
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700284 registerKeyPrefix();
alvy297f4162015-03-03 17:15:33 -0600285 registerLocalhostPrefix();
Vince Lehman7b616582014-10-17 16:25:39 -0500286
Vince Lehman7b616582014-10-17 16:25:39 -0500287 m_helloProtocol.scheduleInterest(m_firstHelloInterval);
Vince Lehman09131122014-09-09 17:10:11 -0500288
289 // Need to set direct neighbors' costs to 0 for hyperbolic routing
290 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
291
292 std::list<Adjacent>& neighbors = m_adjacencyList.getAdjList();
293
294 for (std::list<Adjacent>::iterator it = neighbors.begin(); it != neighbors.end(); ++it) {
295 it->setLinkCost(0);
296 }
297 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700298}
299
300void
akmhoque443ad812014-07-29 10:26:56 -0500301Nlsr::initializeKey()
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700302{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500303 NLSR_LOG_DEBUG("Initializing Key ...");
304
305 ndn::Name nlsrInstanceName = m_confParam.getRouterPrefix();
306 nlsrInstanceName.append("NLSR");
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700307
Joao Pereira97473d42015-07-03 16:57:27 -0400308 try {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500309 m_keyChain.deleteIdentity(m_keyChain.getPib().getIdentity(nlsrInstanceName));
310 } catch (const std::exception& e) {
311 NLSR_LOG_WARN(e.what());
312 }
313
314 auto nlsrInstanceIdentity = m_keyChain.createIdentity(nlsrInstanceName);
315 auto nlsrInstanceKey = nlsrInstanceIdentity.getDefaultKey();
316
317 ndn::security::v2::Certificate certificate;
318
319 ndn::Name certificateName = nlsrInstanceKey.getName();
320 certificateName.append("NA");
321 certificateName.appendVersion();
322 certificate.setName(certificateName);
323
324 // set metainfo
325 certificate.setContentType(ndn::tlv::ContentType_Key);
326 certificate.setFreshnessPeriod(ndn::time::days(7300));
327
328 // set content
329 certificate.setContent(nlsrInstanceKey.getPublicKey().data(), nlsrInstanceKey.getPublicKey().size());
330
331 // set signature-info
332 ndn::SignatureInfo signatureInfo;
333 signatureInfo.setValidityPeriod(ndn::security::ValidityPeriod(ndn::time::system_clock::TimePoint(),
334 ndn::time::system_clock::now()
335 + ndn::time::days(20 * 365)));
336 try {
337 m_keyChain.sign(certificate,
338 ndn::security::SigningInfo(m_keyChain.getPib().getIdentity(m_confParam.getRouterPrefix()))
339 .setSignatureInfo(signatureInfo));
akmhoque102aea42014-08-04 10:22:12 -0500340 }
dmcoomes9f936662017-03-02 10:33:09 -0600341 catch (const std::exception& e) {
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500342 NLSR_LOG_WARN("ERROR: Router's " << e.what()
343 << "NLSR is running without security."
344 << " If security is enabled NLSR will not converge.");
345
346 std::cerr << "Router's " << e.what() << "NLSR is running without security "
347 << "(Only for testing, should not be used in production.)"
348 << " If security is enabled NLSR will not converge." << std::endl;
akmhoque102aea42014-08-04 10:22:12 -0500349 }
akmhoque443ad812014-07-29 10:26:56 -0500350
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500351 m_signingInfo = ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_ID,
352 nlsrInstanceName);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700353
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700354 loadCertToPublish(certificate);
355
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500356 m_defaultCertName = certificate.getName();
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700357}
358
359void
360Nlsr::registerKeyPrefix()
361{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500362 // Start listening for the interest of this router's NLSR certificate
363 ndn::Name nlsrKeyPrefix = getConfParameter().getRouterPrefix();
364 nlsrKeyPrefix.append("NLSR");
365 nlsrKeyPrefix.append("KEY");
366
367 m_nlsrFace.setInterestFilter(nlsrKeyPrefix,
Joao Pereira97473d42015-07-03 16:57:27 -0400368 std::bind(&Nlsr::onKeyInterest,
Yingdi Yu6a3a4dd2014-06-20 14:10:39 -0700369 this, _1, _2),
Joao Pereira97473d42015-07-03 16:57:27 -0400370 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
371 std::bind(&Nlsr::registrationFailed, this, _1),
372 m_signingInfo,
akmhoque060d3022014-08-12 13:35:06 -0500373 ndn::nfd::ROUTE_FLAG_CAPTURE);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700374
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500375 // Start listening for the interest of this router's certificate
376 ndn::Name routerKeyPrefix = getConfParameter().getRouterPrefix();
377 routerKeyPrefix.append("KEY");
378
379 m_nlsrFace.setInterestFilter(routerKeyPrefix,
380 std::bind(&Nlsr::onKeyInterest,
381 this, _1, _2),
382 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
383 std::bind(&Nlsr::registrationFailed, this, _1),
384 m_signingInfo,
385 ndn::nfd::ROUTE_FLAG_CAPTURE);
386
387 // Start listening for the interest of this router's operator's certificate
388 ndn::Name operatorKeyPrefix = getConfParameter().getNetwork();
389 operatorKeyPrefix.append(getConfParameter().getSiteName());
390 operatorKeyPrefix.append(std::string("%C1.Operator"));
391
392 m_nlsrFace.setInterestFilter(operatorKeyPrefix,
393 std::bind(&Nlsr::onKeyInterest,
394 this, _1, _2),
395 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
396 std::bind(&Nlsr::registrationFailed, this, _1),
397 m_signingInfo,
398 ndn::nfd::ROUTE_FLAG_CAPTURE);
399
400 // Start listening for the interest of this router's site's certificate
401 ndn::Name siteKeyPrefix = getConfParameter().getNetwork();
402 siteKeyPrefix.append(getConfParameter().getSiteName());
403 siteKeyPrefix.append("KEY");
404
405 m_nlsrFace.setInterestFilter(siteKeyPrefix,
406 std::bind(&Nlsr::onKeyInterest,
407 this, _1, _2),
408 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
409 std::bind(&Nlsr::registrationFailed, this, _1),
410 m_signingInfo,
411 ndn::nfd::ROUTE_FLAG_CAPTURE);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700412}
413
414void
alvy297f4162015-03-03 17:15:33 -0600415Nlsr::registerLocalhostPrefix()
416{
alvy297f4162015-03-03 17:15:33 -0600417 m_nlsrFace.registerPrefix(LOCALHOST_PREFIX,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500418 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
alvy297f4162015-03-03 17:15:33 -0600419 std::bind(&Nlsr::registrationFailed, this, _1));
420}
421
422void
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700423Nlsr::onKeyInterest(const ndn::Name& name, const ndn::Interest& interest)
424{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500425 NLSR_LOG_DEBUG("Got interest for certificate. Interest: " << interest.getName());
426
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700427 const ndn::Name& interestName = interest.getName();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500428 const ndn::security::v2::Certificate* cert = getCertificate(interestName);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700429
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500430 if (cert == nullptr) {
431 NLSR_LOG_DEBUG("Certificate is not found for: " << interest);
dmcoomes9eaf3f42017-02-21 11:39:01 -0600432 return; // cert is not found
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500433 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700434
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500435 m_nlsrFace.put(*cert);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700436}
437
438void
439Nlsr::onKeyPrefixRegSuccess(const ndn::Name& name)
440{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500441 NLSR_LOG_DEBUG("KEY prefix: " << name << " registration is successful.");
akmhoque53353462014-04-22 08:43:45 -0500442}
akmhoque5a44dd42014-03-12 18:11:32 -0500443
akmhoque53353462014-04-22 08:43:45 -0500444void
akmhoquec04e7272014-07-02 11:00:14 -0500445Nlsr::onFaceEventNotification(const ndn::nfd::FaceEventNotification& faceEventNotification)
akmhoquee1765152014-06-30 11:32:01 -0500446{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500447 NLSR_LOG_TRACE("Nlsr::onFaceEventNotification called");
Vince Lehman02e32992015-03-11 12:31:20 -0500448
Nick Gordond5c1a372016-10-31 13:56:23 -0500449 switch (faceEventNotification.getKind()) {
450 case ndn::nfd::FACE_EVENT_DESTROYED: {
451 uint64_t faceId = faceEventNotification.getFaceId();
Vince Lehman02e32992015-03-11 12:31:20 -0500452
Nick Gordond5c1a372016-10-31 13:56:23 -0500453 auto adjacent = m_adjacencyList.findAdjacent(faceId);
Vince Lehman02e32992015-03-11 12:31:20 -0500454
Nick Gordond5c1a372016-10-31 13:56:23 -0500455 if (adjacent != m_adjacencyList.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500456 NLSR_LOG_DEBUG("Face to " << adjacent->getName() << " with face id: " << faceId << " destroyed");
Vince Lehman02e32992015-03-11 12:31:20 -0500457
Nick Gordond5c1a372016-10-31 13:56:23 -0500458 adjacent->setFaceId(0);
Vince Lehman02e32992015-03-11 12:31:20 -0500459
Nick Gordond5c1a372016-10-31 13:56:23 -0500460 // Only trigger an Adjacency LSA build if this node is changing
461 // from ACTIVE to INACTIVE since this rebuild will effectively
462 // cancel the previous Adjacency LSA refresh event and schedule
463 // a new one further in the future.
464 //
465 // Continuously scheduling the refresh in the future will block
466 // the router from refreshing its Adjacency LSA. Since other
467 // routers' Name prefixes' expiration times are updated when
468 // this router refreshes its Adjacency LSA, the other routers'
469 // prefixes will expire and be removed from the RIB.
470 //
471 // This check is required to fix Bug #2733 for now. This check
472 // would be unnecessary to fix Bug #2733 when Issue #2732 is
473 // completed, but the check also helps with optimization so it
474 // can remain even when Issue #2732 is implemented.
475 if (adjacent->getStatus() == Adjacent::STATUS_ACTIVE) {
476 adjacent->setStatus(Adjacent::STATUS_INACTIVE);
Vince Lehman02e32992015-03-11 12:31:20 -0500477
Nick Gordond5c1a372016-10-31 13:56:23 -0500478 // A new adjacency LSA cannot be built until the neighbor is marked INACTIVE and
479 // has met the HELLO retry threshold
480 adjacent->setInterestTimedOutNo(m_confParam.getInterestRetryNumber());
Vince Lehman199e9cf2015-04-07 13:22:16 -0500481
Nick Gordond5c1a372016-10-31 13:56:23 -0500482 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
483 getRoutingTable().scheduleRoutingTableCalculation(*this);
484 }
485 else {
486 m_nlsrLsdb.scheduleAdjLsaBuild();
487 }
Nick Gordone8e03ac2016-07-07 14:24:38 -0500488 }
Vince Lehman199e9cf2015-04-07 13:22:16 -0500489 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500490 break;
akmhoquec04e7272014-07-02 11:00:14 -0500491 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500492 case ndn::nfd::FACE_EVENT_CREATED: {
493 // Find the neighbor in our adjacency list
Nick Gordond5c1a372016-10-31 13:56:23 -0500494 auto adjacent = m_adjacencyList.findAdjacent(
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500495 ndn::FaceUri(faceEventNotification.getRemoteUri()));
Nick Gordond5c1a372016-10-31 13:56:23 -0500496 // If we have a neighbor by that FaceUri and it has no FaceId, we
497 // have a match.
498 if (adjacent != m_adjacencyList.end()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500499 NLSR_LOG_DEBUG("Face creation event matches neighbor: " << adjacent->getName()
Nick Gordond5c1a372016-10-31 13:56:23 -0500500 << ". New Face ID: " << faceEventNotification.getFaceId()
501 << ". Registering prefixes.");
502 adjacent->setFaceId(faceEventNotification.getFaceId());
503
504 registerAdjacencyPrefixes(*adjacent, ndn::time::milliseconds::max());
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500505
506 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
507 getRoutingTable().scheduleRoutingTableCalculation(*this);
508 }
509 else {
510 m_nlsrLsdb.scheduleAdjLsaBuild();
511 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500512 }
513 break;
514 }
515 default:
516 break;
akmhoquec04e7272014-07-02 11:00:14 -0500517 }
akmhoquee1765152014-06-30 11:32:01 -0500518}
519
Nick Gordond5c1a372016-10-31 13:56:23 -0500520void
521Nlsr::initializeFaces(const FetchDatasetCallback& onFetchSuccess,
522 const FetchDatasetTimeoutCallback& onFetchFailure)
523{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500524 NLSR_LOG_TRACE("Initializing Faces...");
Nick Gordond5c1a372016-10-31 13:56:23 -0500525
526 m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(onFetchSuccess, onFetchFailure);
527
528}
529
530void
531Nlsr::processFaceDataset(const std::vector<ndn::nfd::FaceStatus>& faces)
532{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500533 NLSR_LOG_DEBUG("Processing face dataset");
Nick Gordond5c1a372016-10-31 13:56:23 -0500534
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500535 // Iterate over each neighbor listed in nlsr.conf
536 for (auto& adjacent : m_adjacencyList.getAdjList()) {
537
538 const std::string faceUriString = adjacent.getFaceUri().toString();
Nick Gordond5c1a372016-10-31 13:56:23 -0500539 // Check the list of FaceStatus objects we got for a match
540 for (const ndn::nfd::FaceStatus& faceStatus : faces) {
Nick Gordond5c1a372016-10-31 13:56:23 -0500541 // Set the adjacency FaceID if we find a URI match and it was
542 // previously unset. Change the boolean to true.
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500543 if (adjacent.getFaceId() == 0 && faceUriString == faceStatus.getRemoteUri()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500544 NLSR_LOG_DEBUG("FaceUri: " << faceStatus.getRemoteUri() <<
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500545 " FaceId: "<< faceStatus.getFaceId());
546 adjacent.setFaceId(faceStatus.getFaceId());
Nick Gordond5c1a372016-10-31 13:56:23 -0500547 // Register the prefixes for each neighbor
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500548 this->registerAdjacencyPrefixes(adjacent, ndn::time::milliseconds::max());
Nick Gordond5c1a372016-10-31 13:56:23 -0500549 }
550 }
551 // If this adjacency has no information in this dataset, then one
552 // of two things is happening: 1. NFD is starting slowly and this
553 // Face wasn't ready yet, or 2. NFD is configured
554 // incorrectly and this Face isn't available.
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500555 if (adjacent.getFaceId() == 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500556 NLSR_LOG_WARN("The adjacency " << adjacent.getName() <<
Nick Gordond5c1a372016-10-31 13:56:23 -0500557 " has no Face information in this dataset.");
558 }
559 }
560
Nick Gordond5c1a372016-10-31 13:56:23 -0500561 scheduleDatasetFetch();
562}
563
564void
565Nlsr::registerAdjacencyPrefixes(const Adjacent& adj,
566 const ndn::time::milliseconds& timeout)
567{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500568 ndn::FaceUri faceUri = adj.getFaceUri();
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500569 double linkCost = adj.getLinkCost();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500570 const ndn::Name& adjName = adj.getName();
Nick Gordond5c1a372016-10-31 13:56:23 -0500571
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500572 m_fib.registerPrefix(adjName, faceUri, linkCost,
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500573 timeout, ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500574
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500575 m_fib.registerPrefix(m_confParam.getChronosyncPrefix(),
576 faceUri, linkCost, timeout,
577 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500578
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500579 m_fib.registerPrefix(m_confParam.getLsaPrefix(),
580 faceUri, linkCost, timeout,
581 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500582}
583
584void
585Nlsr::onFaceDatasetFetchTimeout(uint32_t code,
586 const std::string& msg,
587 uint32_t nRetriesSoFar)
588{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500589 NLSR_LOG_DEBUG("onFaceDatasetFetchTimeout");
Nick Gordond5c1a372016-10-31 13:56:23 -0500590 // If we have exceeded the maximum attempt count, do not try again.
591 if (nRetriesSoFar++ < m_confParam.getFaceDatasetFetchTries()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500592 NLSR_LOG_DEBUG("Failed to fetch dataset: " << msg << ". Attempting retry #" << nRetriesSoFar);
Nick Gordond5c1a372016-10-31 13:56:23 -0500593 m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(std::bind(&Nlsr::processFaceDataset,
594 this, _1),
595 std::bind(&Nlsr::onFaceDatasetFetchTimeout,
596 this, _1, _2, nRetriesSoFar));
597 }
598 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500599 NLSR_LOG_ERROR("Failed to fetch dataset: " << msg << ". Exceeded limit of " <<
Nick Gordond5c1a372016-10-31 13:56:23 -0500600 m_confParam.getFaceDatasetFetchTries() << ", so not trying again this time.");
601 // If we fail to fetch it, just do nothing until the next
602 // interval. Since this is a backup mechanism, we aren't as
603 // concerned with retrying.
604 scheduleDatasetFetch();
605 }
606}
607
608void
609Nlsr::scheduleDatasetFetch()
610{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500611 NLSR_LOG_DEBUG("Scheduling Dataset Fetch in " << m_confParam.getFaceDatasetFetchInterval());
612
Nick Gordond5c1a372016-10-31 13:56:23 -0500613 m_scheduler.scheduleEvent(m_confParam.getFaceDatasetFetchInterval(),
614 [this] {
615 this->initializeFaces(
616 [this] (const std::vector<ndn::nfd::FaceStatus>& faces) {
617 this->processFaceDataset(faces);
618 },
619 [this] (uint32_t code, const std::string& msg) {
620 this->onFaceDatasetFetchTimeout(code, msg, 0);
621 });
622 });
623}
akmhoquee1765152014-06-30 11:32:01 -0500624
625void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500626Nlsr::enableIncomingFaceIdIndication()
627{
628 NLSR_LOG_DEBUG("Enabling incoming face id indication for local face.");
629
630 m_controller.start<ndn::nfd::FaceUpdateCommand>(
631 ndn::nfd::ControlParameters()
632 .setFlagBit(ndn::nfd::FaceFlagBit::BIT_LOCAL_FIELDS_ENABLED, true),
633 bind(&Nlsr::onFaceIdIndicationSuccess, this, _1),
634 bind(&Nlsr::onFaceIdIndicationFailure, this, _1));
635}
636
637void
638Nlsr::onFaceIdIndicationSuccess(const ndn::nfd::ControlParameters& cp)
639{
640 NLSR_LOG_DEBUG("Successfully enabled incoming face id indication"
641 << "for face id " << cp.getFaceId());
642}
643
644void
645Nlsr::onFaceIdIndicationFailure(const ndn::nfd::ControlResponse& cr)
646{
647 std::ostringstream os;
648 os << "Failed to enable incoming face id indication feature: " <<
649 "(code: " << cr.getCode() << ", reason: " << cr.getText() << ")";
650
651 NLSR_LOG_DEBUG(os.str());
652}
653
654void
akmhoque53353462014-04-22 08:43:45 -0500655Nlsr::startEventLoop()
656{
akmhoquefdbddb12014-05-02 18:35:19 -0500657 m_nlsrFace.processEvents();
akmhoque53353462014-04-22 08:43:45 -0500658}
akmhoque5a44dd42014-03-12 18:11:32 -0500659
akmhoqueb1710aa2014-02-19 17:13:36 -0600660} // namespace nlsr