blob: 55d426aa09e3e90b39750576a7fc9065955aacb7 [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
Nick Gordond5c1a372016-10-31 13:56:23 -050032#include <ndn-cxx/util/face-uri.hpp>
akmhoque298385a2014-02-13 14:13:09 -060033
akmhoque53353462014-04-22 08:43:45 -050034namespace nlsr {
35
akmhoque674b0b12014-05-20 14:33:28 -050036INIT_LOGGER("nlsr");
37
alvy297f4162015-03-03 17:15:33 -060038const ndn::Name Nlsr::LOCALHOST_PREFIX = ndn::Name("/localhost/nlsr");
39
akmhoque53353462014-04-22 08:43:45 -050040using namespace ndn;
41using namespace std;
42
Laqin Fana4cf4022017-01-03 18:57:35 +000043Nlsr::Nlsr(boost::asio::io_service& ioService, ndn::Scheduler& scheduler, ndn::Face& face, ndn::KeyChain& keyChain)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050044 : m_nlsrFace(face)
45 , m_scheduler(scheduler)
Laqin Fana4cf4022017-01-03 18:57:35 +000046 , m_keyChain(keyChain)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050047 , m_confParam()
48 , m_adjacencyList()
49 , m_namePrefixList()
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050050 , m_isDaemonProcess(false)
51 , m_configFileName("nlsr.conf")
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050052 , m_nlsrLsdb(*this, scheduler)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050053 , m_adjBuildCount(0)
54 , m_isBuildAdjLsaSheduled(false)
55 , m_isRouteCalculationScheduled(false)
56 , m_isRoutingTableCalculating(false)
57 , m_routingTable(scheduler)
58 , m_fib(m_nlsrFace, scheduler, m_adjacencyList, m_confParam, m_keyChain)
59 , m_namePrefixTable(*this)
laqinfand22da512017-05-25 17:29:53 -050060 , m_localhostDispatcher(m_nlsrFace, m_keyChain)
61 , m_routerNameDispatcher(m_nlsrFace, m_keyChain)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050062 , m_lsdbDatasetHandler(m_nlsrLsdb,
laqinfand22da512017-05-25 17:29:53 -050063 m_localhostDispatcher,
64 m_routerNameDispatcher,
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050065 m_nlsrFace,
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050066 m_keyChain)
Nick Gordond5c1a372016-10-31 13:56:23 -050067
Vince Lehmanf7eec4f2015-05-08 19:02:31 -050068 , m_helloProtocol(*this, scheduler)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050069 , m_certificateCache(new ndn::CertificateCacheTtl(ioService))
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050070 , m_validator(m_nlsrFace, DEFAULT_BROADCAST_PREFIX, m_certificateCache, m_certStore)
Nick Gordond5c1a372016-10-31 13:56:23 -050071 , m_controller(m_nlsrFace, m_keyChain, m_validator)
72 , m_faceDatasetController(m_nlsrFace, m_keyChain)
Laqin Fan54a43f02017-03-08 12:31:30 -060073 , m_prefixUpdateProcessor(m_localhostDispatcher,
74 m_nlsrFace,
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050075 m_namePrefixList,
76 m_nlsrLsdb,
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050077 DEFAULT_BROADCAST_PREFIX,
78 m_keyChain,
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050079 m_certificateCache,
80 m_certStore)
laqinfand22da512017-05-25 17:29:53 -050081 , m_nfdRibCommandProcessor(m_localhostDispatcher,
Nick Gordon4d2c6c02017-01-20 13:18:46 -060082 m_namePrefixList,
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050083 m_nlsrLsdb)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050084 , m_faceMonitor(m_nlsrFace)
85 , m_firstHelloInterval(FIRST_HELLO_INTERVAL_DEFAULT)
86{
dmcoomes9f936662017-03-02 10:33:09 -060087 m_faceMonitor.onNotification.connect(std::bind(&Nlsr::onFaceEventNotification, this, _1));
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050088 m_faceMonitor.start();
89}
90
akmhoque53353462014-04-22 08:43:45 -050091void
92Nlsr::registrationFailed(const ndn::Name& name)
akmhoque298385a2014-02-13 14:13:09 -060093{
Ashlesh Gawande793e8702017-08-01 15:59:26 -050094 _LOG_ERROR("ERROR: Failed to register prefix in local hub's daemon");
dmcoomes9f936662017-03-02 10:33:09 -060095 BOOST_THROW_EXCEPTION(Error("Error: Prefix registration failed"));
akmhoque53353462014-04-22 08:43:45 -050096}
akmhoque1fd8c1e2014-02-19 19:41:49 -060097
akmhoque157b0a42014-05-13 00:26:37 -050098void
99Nlsr::onRegistrationSuccess(const ndn::Name& name)
100{
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500101 _LOG_DEBUG("Successfully registered prefix: " << name);
102
Jiewen Tana0497d82015-02-02 21:59:18 -0800103 if (name.equals(m_confParam.getRouterPrefix())) {
laqinfand22da512017-05-25 17:29:53 -0500104 // the top-level prefixes are added.
105 try {
106 m_routerNameDispatcher.addTopPrefix(m_confParam.getRouterPrefix(), false, m_signingInfo);
107 }
108 catch (const std::exception& e) {
109 _LOG_ERROR("Error setting top-level prefix in dispatcher: " << e.what() << "\n");
110 }
Jiewen Tana0497d82015-02-02 21:59:18 -0800111 }
akmhoque157b0a42014-05-13 00:26:37 -0500112}
akmhoque1fd8c1e2014-02-19 19:41:49 -0600113
akmhoque53353462014-04-22 08:43:45 -0500114void
alvy297f4162015-03-03 17:15:33 -0600115Nlsr::onLocalhostRegistrationSuccess(const ndn::Name& name)
116{
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600117 // All dispatcher-related sub-prefixes *must* be registered before
118 // the top-level prefixes are added.
119 try {
laqinfand22da512017-05-25 17:29:53 -0500120 m_localhostDispatcher.addTopPrefix(LOCALHOST_PREFIX, false, m_signingInfo);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600121 }
122 catch (const std::exception& e) {
123 _LOG_ERROR("Error setting top-level prefix in dispatcher: " << e.what() << "\n");
124 }
alvy297f4162015-03-03 17:15:33 -0600125}
126
127void
akmhoque31d1d4b2014-05-05 22:08:14 -0500128Nlsr::setInfoInterestFilter()
akmhoque53353462014-04-22 08:43:45 -0500129{
akmhoque31d1d4b2014-05-05 22:08:14 -0500130 ndn::Name name(m_confParam.getRouterPrefix());
akmhoque674b0b12014-05-20 14:33:28 -0500131 _LOG_DEBUG("Setting interest filter for name: " << name);
akmhoquefdbddb12014-05-02 18:35:19 -0500132 getNlsrFace().setInterestFilter(name,
Joao Pereira97473d42015-07-03 16:57:27 -0400133 std::bind(&HelloProtocol::processInterest,
akmhoque31d1d4b2014-05-05 22:08:14 -0500134 &m_helloProtocol, _1, _2),
Joao Pereira97473d42015-07-03 16:57:27 -0400135 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
136 std::bind(&Nlsr::registrationFailed, this, _1),
137 m_signingInfo,
akmhoque060d3022014-08-12 13:35:06 -0500138 ndn::nfd::ROUTE_FLAG_CAPTURE);
akmhoque31d1d4b2014-05-05 22:08:14 -0500139}
140
141void
142Nlsr::setLsaInterestFilter()
143{
akmhoque157b0a42014-05-13 00:26:37 -0500144 ndn::Name name = m_confParam.getLsaPrefix();
akmhoque50125a92014-06-30 08:54:17 -0500145 name.append(m_confParam.getSiteName());
146 name.append(m_confParam.getRouterName());
laqinfand22da512017-05-25 17:29:53 -0500147 _LOG_DEBUG("Setting interest filter for LsaPrefix: " << name);
akmhoque31d1d4b2014-05-05 22:08:14 -0500148 getNlsrFace().setInterestFilter(name,
Joao Pereira97473d42015-07-03 16:57:27 -0400149 std::bind(&Lsdb::processInterest,
akmhoque31d1d4b2014-05-05 22:08:14 -0500150 &m_nlsrLsdb, _1, _2),
Joao Pereira97473d42015-07-03 16:57:27 -0400151 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
152 std::bind(&Nlsr::registrationFailed, this, _1),
153 m_signingInfo,
akmhoque060d3022014-08-12 13:35:06 -0500154 ndn::nfd::ROUTE_FLAG_CAPTURE);
akmhoque53353462014-04-22 08:43:45 -0500155}
156
157void
akmhoquec04e7272014-07-02 11:00:14 -0500158Nlsr::setStrategies()
akmhoque157b0a42014-05-13 00:26:37 -0500159{
Vince Lehman53c0e3e2015-09-14 14:33:20 -0500160 const std::string strategy("ndn:/localhost/nfd/strategy/multicast");
161
akmhoque3cb0cfc2014-06-24 10:32:24 -0500162 ndn::Name broadcastKeyPrefix = DEFAULT_BROADCAST_PREFIX;
163 broadcastKeyPrefix.append("KEYS");
Vince Lehman53c0e3e2015-09-14 14:33:20 -0500164
akmhoque393d4ff2014-07-16 14:27:03 -0500165 m_fib.setStrategy(m_confParam.getLsaPrefix(), strategy, 0);
166 m_fib.setStrategy(broadcastKeyPrefix, strategy, 0);
167 m_fib.setStrategy(m_confParam.getChronosyncPrefix(), strategy, 0);
akmhoque157b0a42014-05-13 00:26:37 -0500168}
169
170void
akmhoque0494c252014-07-23 23:46:44 -0500171Nlsr::daemonize()
172{
173 pid_t process_id = 0;
174 pid_t sid = 0;
175 process_id = fork();
176 if (process_id < 0){
177 std::cerr << "Daemonization failed!" << std::endl;
dmcoomes9f936662017-03-02 10:33:09 -0600178 BOOST_THROW_EXCEPTION(Error("Error: Daemonization process- fork failed!"));
akmhoque0494c252014-07-23 23:46:44 -0500179 }
180 if (process_id > 0) {
181 _LOG_DEBUG("Process daemonized. Process id: " << process_id);
182 exit(0);
183 }
184
185 umask(0);
186 sid = setsid();
187 if(sid < 0) {
dmcoomes9f936662017-03-02 10:33:09 -0600188 BOOST_THROW_EXCEPTION(Error("Error: Daemonization process- setting id failed!"));
akmhoque0494c252014-07-23 23:46:44 -0500189 }
190
191 if (chdir("/") < 0) {
dmcoomes9f936662017-03-02 10:33:09 -0600192 BOOST_THROW_EXCEPTION(Error("Error: Daemonization process-chdir failed!"));
akmhoque0494c252014-07-23 23:46:44 -0500193 }
194}
195
196void
Nick Gordon9461afb2017-04-25 15:54:50 -0500197Nlsr::canonizeContinuation(std::list<Adjacent>::iterator iterator)
198{
199 canonizeNeighborUris(iterator, [this] (std::list<Adjacent>::iterator iterator) {
200 canonizeContinuation(iterator);
201 });
202}
203
204void
205Nlsr::canonizeNeighborUris(std::list<Adjacent>::iterator currentNeighbor,
206 std::function<void(std::list<Adjacent>::iterator)> then)
207{
208 if (currentNeighbor != m_adjacencyList.getAdjList().end()) {
Nick Gordone9733ed2017-04-26 10:48:39 -0500209 ndn::util::FaceUri uri(currentNeighbor->getFaceUri());
Nick Gordon9461afb2017-04-25 15:54:50 -0500210 uri.canonize([this, then, currentNeighbor] (ndn::util::FaceUri canonicalUri) {
Nick Gordone9733ed2017-04-26 10:48:39 -0500211 _LOG_DEBUG("Canonized URI: " << currentNeighbor->getFaceUri()
Nick Gordon9461afb2017-04-25 15:54:50 -0500212 << " to: " << canonicalUri);
Nick Gordone9733ed2017-04-26 10:48:39 -0500213 currentNeighbor->setFaceUri(canonicalUri);
Nick Gordon9461afb2017-04-25 15:54:50 -0500214 then(std::next(currentNeighbor));
215 },
216 [this, then, currentNeighbor] (const std::string& reason) {
Nick Gordone9733ed2017-04-26 10:48:39 -0500217 _LOG_ERROR("Could not canonize URI: " << currentNeighbor->getFaceUri()
Nick Gordon9461afb2017-04-25 15:54:50 -0500218 << " because: " << reason);
219 then(std::next(currentNeighbor));
220 },
221 m_nlsrFace.getIoService(),
222 TIME_ALLOWED_FOR_CANONIZATION);
223 }
224 // We have finished canonizing all neighbors, so initialize NLSR.
225 else {
226 initialize();
227 }
228}
229
230void
akmhoque53353462014-04-22 08:43:45 -0500231Nlsr::initialize()
232{
akmhoque674b0b12014-05-20 14:33:28 -0500233 _LOG_DEBUG("Initializing Nlsr");
akmhoque53353462014-04-22 08:43:45 -0500234 m_confParam.buildRouterPrefix();
Muktadir R Chowdhury3ac07282016-06-17 16:30:29 -0500235 m_lsdbDatasetHandler.setRouterNameCommandPrefix(m_confParam.getRouterPrefix());
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700236 m_nlsrLsdb.setLsaRefreshTime(ndn::time::seconds(m_confParam.getLsaRefreshTime()));
akmhoque31d1d4b2014-05-05 22:08:14 -0500237 m_nlsrLsdb.setThisRouterPrefix(m_confParam.getRouterPrefix().toUri());
akmhoque53353462014-04-22 08:43:45 -0500238 m_fib.setEntryRefreshTime(2 * m_confParam.getLsaRefreshTime());
Vince Lehmanc11cc202015-01-20 11:41:33 -0600239
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500240 m_nlsrLsdb.getSequencingManager().setSeqFileDirectory(m_confParam.getSeqFileDir());
241 m_nlsrLsdb.getSequencingManager().initiateSeqNoFromFile(m_confParam.getHyperbolicState());
242
243 m_nlsrLsdb.getSyncLogicHandler().createSyncSocket(m_confParam.getChronosyncPrefix());
Vince Lehmanc11cc202015-01-20 11:41:33 -0600244
dmcoomes9f936662017-03-02 10:33:09 -0600245 // Logging start
akmhoque674b0b12014-05-20 14:33:28 -0500246 m_confParam.writeLog();
247 m_adjacencyList.writeLog();
Nick Gordon494de402017-06-14 12:48:51 -0500248 _LOG_DEBUG(m_namePrefixList);
dmcoomes9f936662017-03-02 10:33:09 -0600249 // Logging end
akmhoque443ad812014-07-29 10:26:56 -0500250 initializeKey();
akmhoquec04e7272014-07-02 11:00:14 -0500251 setStrategies();
Joao Pereira97473d42015-07-03 16:57:27 -0400252 _LOG_DEBUG("Default NLSR identity: " << m_signingInfo.getSignerName());
akmhoque31d1d4b2014-05-05 22:08:14 -0500253 setInfoInterestFilter();
254 setLsaInterestFilter();
Vince Lehman50df6b72015-03-03 12:06:40 -0600255
Nick Gordond5c1a372016-10-31 13:56:23 -0500256 initializeFaces(std::bind(&Nlsr::processFaceDataset, this, _1),
257 std::bind(&Nlsr::onFaceDatasetFetchTimeout, this, _1, _2, 0));
258
Vince Lehman50df6b72015-03-03 12:06:40 -0600259 // Set event intervals
260 setFirstHelloInterval(m_confParam.getFirstHelloInterval());
261 m_nlsrLsdb.setAdjLsaBuildInterval(m_confParam.getAdjLsaBuildInterval());
262 m_routingTable.setRoutingCalcInterval(m_confParam.getRoutingCalcInterval());
263
akmhoque674b0b12014-05-20 14:33:28 -0500264 m_nlsrLsdb.buildAndInstallOwnNameLsa();
Nick Gordon5c467f02016-07-13 13:40:10 -0500265
266 // Install coordinate LSAs if using HR or dry-run HR.
267 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
268 m_nlsrLsdb.buildAndInstallOwnCoordinateLsa();
269 }
Vince Lehman904c2412014-09-23 19:36:11 -0500270
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700271 registerKeyPrefix();
alvy297f4162015-03-03 17:15:33 -0600272 registerLocalhostPrefix();
Vince Lehman7b616582014-10-17 16:25:39 -0500273
Vince Lehman7b616582014-10-17 16:25:39 -0500274 m_helloProtocol.scheduleInterest(m_firstHelloInterval);
Vince Lehman09131122014-09-09 17:10:11 -0500275
276 // Need to set direct neighbors' costs to 0 for hyperbolic routing
277 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
278
279 std::list<Adjacent>& neighbors = m_adjacencyList.getAdjList();
280
281 for (std::list<Adjacent>::iterator it = neighbors.begin(); it != neighbors.end(); ++it) {
282 it->setLinkCost(0);
283 }
284 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700285}
286
287void
akmhoque443ad812014-07-29 10:26:56 -0500288Nlsr::initializeKey()
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700289{
Joao Pereira97473d42015-07-03 16:57:27 -0400290 ndn::Name defaultIdentity = m_confParam.getRouterPrefix();
291 defaultIdentity.append("NLSR");
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700292
Joao Pereira97473d42015-07-03 16:57:27 -0400293 try {
294 m_keyChain.deleteIdentity(defaultIdentity);
akmhoque102aea42014-08-04 10:22:12 -0500295 }
dmcoomes9f936662017-03-02 10:33:09 -0600296 catch (const std::exception& e) {
akmhoque102aea42014-08-04 10:22:12 -0500297 }
Joao Pereira97473d42015-07-03 16:57:27 -0400298 m_signingInfo = ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_ID, defaultIdentity);
akmhoque443ad812014-07-29 10:26:56 -0500299
Joao Pereira97473d42015-07-03 16:57:27 -0400300 ndn::Name keyName = m_keyChain.generateRsaKeyPairAsDefault(defaultIdentity, true);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700301
dmcoomes9f936662017-03-02 10:33:09 -0600302 std::shared_ptr<ndn::IdentityCertificate> certificate =
303 std::make_shared<ndn::IdentityCertificate>();
304 std::shared_ptr<ndn::PublicKey> pubKey = m_keyChain.getPublicKey(keyName);
Yingdi Yu191f5fa2014-08-06 17:08:52 -0700305 Name certificateName = keyName.getPrefix(-1);
306 certificateName.append("KEY").append(keyName.get(-1)).append("ID-CERT").appendVersion();
307 certificate->setName(certificateName);
308 certificate->setNotBefore(time::system_clock::now() - time::days(1));
309 certificate->setNotAfter(time::system_clock::now() + time::days(7300)); // ~20 years
310 certificate->setPublicKeyInfo(*pubKey);
311 certificate->addSubjectDescription(CertificateSubjectDescription(ndn::oid::ATTRIBUTE_NAME,
312 keyName.toUri()));
313 certificate->encode();
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700314 m_keyChain.signByIdentity(*certificate, m_confParam.getRouterPrefix());
315
316 m_keyChain.addCertificateAsIdentityDefault(*certificate);
317 loadCertToPublish(certificate);
318
319 m_defaultCertName = certificate->getName();
320}
321
322void
323Nlsr::registerKeyPrefix()
324{
325 ndn::Name keyPrefix = DEFAULT_BROADCAST_PREFIX;
326 keyPrefix.append("KEYS");
327 m_nlsrFace.setInterestFilter(keyPrefix,
Joao Pereira97473d42015-07-03 16:57:27 -0400328 std::bind(&Nlsr::onKeyInterest,
Yingdi Yu6a3a4dd2014-06-20 14:10:39 -0700329 this, _1, _2),
Joao Pereira97473d42015-07-03 16:57:27 -0400330 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
331 std::bind(&Nlsr::registrationFailed, this, _1),
332 m_signingInfo,
akmhoque060d3022014-08-12 13:35:06 -0500333 ndn::nfd::ROUTE_FLAG_CAPTURE);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700334
335}
336
337void
alvy297f4162015-03-03 17:15:33 -0600338Nlsr::registerLocalhostPrefix()
339{
alvy297f4162015-03-03 17:15:33 -0600340 m_nlsrFace.registerPrefix(LOCALHOST_PREFIX,
341 std::bind(&Nlsr::onLocalhostRegistrationSuccess, this, _1),
342 std::bind(&Nlsr::registrationFailed, this, _1));
343}
344
345void
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700346Nlsr::onKeyInterest(const ndn::Name& name, const ndn::Interest& interest)
347{
348 const ndn::Name& interestName = interest.getName();
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700349 ndn::Name certName = interestName.getSubName(name.size());
350
351 if (certName[-2].toUri() == "ID-CERT")
352 {
353 certName = certName.getPrefix(-1);
354 }
355 else if (certName[-1].toUri() != "ID-CERT")
dmcoomes9eaf3f42017-02-21 11:39:01 -0600356 {
357 _LOG_DEBUG("certName for interest " << interest << " is malformed,"
358 << " contains incorrect namespace syntax");
359 return;
360 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700361
dmcoomes9f936662017-03-02 10:33:09 -0600362 std::shared_ptr<const ndn::IdentityCertificate> cert = getCertificate(certName);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700363
364 if (!static_cast<bool>(cert))
dmcoomes9eaf3f42017-02-21 11:39:01 -0600365 {
366 _LOG_DEBUG("cert is not found for " << interest);
367 return; // cert is not found
368 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700369
dmcoomes9f936662017-03-02 10:33:09 -0600370 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>();
akmhoque69c9aa92014-07-23 15:15:05 -0500371 data->setName(interestName);
372 data->setContent(cert->wireEncode());
373 m_keyChain.signWithSha256(*data);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700374
akmhoque69c9aa92014-07-23 15:15:05 -0500375 m_nlsrFace.put(*data);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700376}
377
378void
379Nlsr::onKeyPrefixRegSuccess(const ndn::Name& name)
380{
akmhoque53353462014-04-22 08:43:45 -0500381}
akmhoque5a44dd42014-03-12 18:11:32 -0500382
akmhoque53353462014-04-22 08:43:45 -0500383void
akmhoquec04e7272014-07-02 11:00:14 -0500384Nlsr::onFaceEventNotification(const ndn::nfd::FaceEventNotification& faceEventNotification)
akmhoquee1765152014-06-30 11:32:01 -0500385{
Vince Lehman02e32992015-03-11 12:31:20 -0500386 _LOG_TRACE("Nlsr::onFaceEventNotification called");
Vince Lehman02e32992015-03-11 12:31:20 -0500387
Nick Gordond5c1a372016-10-31 13:56:23 -0500388 switch (faceEventNotification.getKind()) {
389 case ndn::nfd::FACE_EVENT_DESTROYED: {
390 uint64_t faceId = faceEventNotification.getFaceId();
Vince Lehman02e32992015-03-11 12:31:20 -0500391
Nick Gordond5c1a372016-10-31 13:56:23 -0500392 auto adjacent = m_adjacencyList.findAdjacent(faceId);
Vince Lehman02e32992015-03-11 12:31:20 -0500393
Nick Gordond5c1a372016-10-31 13:56:23 -0500394 if (adjacent != m_adjacencyList.end()) {
395 _LOG_DEBUG("Face to " << adjacent->getName() << " with face id: " << faceId << " destroyed");
Vince Lehman02e32992015-03-11 12:31:20 -0500396
Nick Gordond5c1a372016-10-31 13:56:23 -0500397 adjacent->setFaceId(0);
Vince Lehman02e32992015-03-11 12:31:20 -0500398
Nick Gordond5c1a372016-10-31 13:56:23 -0500399 // Only trigger an Adjacency LSA build if this node is changing
400 // from ACTIVE to INACTIVE since this rebuild will effectively
401 // cancel the previous Adjacency LSA refresh event and schedule
402 // a new one further in the future.
403 //
404 // Continuously scheduling the refresh in the future will block
405 // the router from refreshing its Adjacency LSA. Since other
406 // routers' Name prefixes' expiration times are updated when
407 // this router refreshes its Adjacency LSA, the other routers'
408 // prefixes will expire and be removed from the RIB.
409 //
410 // This check is required to fix Bug #2733 for now. This check
411 // would be unnecessary to fix Bug #2733 when Issue #2732 is
412 // completed, but the check also helps with optimization so it
413 // can remain even when Issue #2732 is implemented.
414 if (adjacent->getStatus() == Adjacent::STATUS_ACTIVE) {
415 adjacent->setStatus(Adjacent::STATUS_INACTIVE);
Vince Lehman02e32992015-03-11 12:31:20 -0500416
Nick Gordond5c1a372016-10-31 13:56:23 -0500417 // A new adjacency LSA cannot be built until the neighbor is marked INACTIVE and
418 // has met the HELLO retry threshold
419 adjacent->setInterestTimedOutNo(m_confParam.getInterestRetryNumber());
Vince Lehman199e9cf2015-04-07 13:22:16 -0500420
Nick Gordond5c1a372016-10-31 13:56:23 -0500421 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
422 getRoutingTable().scheduleRoutingTableCalculation(*this);
423 }
424 else {
425 m_nlsrLsdb.scheduleAdjLsaBuild();
426 }
Nick Gordone8e03ac2016-07-07 14:24:38 -0500427 }
Vince Lehman199e9cf2015-04-07 13:22:16 -0500428 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500429 break;
akmhoquec04e7272014-07-02 11:00:14 -0500430 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500431 case ndn::nfd::FACE_EVENT_CREATED: {
432 // Find the neighbor in our adjacency list
Nick Gordond5c1a372016-10-31 13:56:23 -0500433 auto adjacent = m_adjacencyList.findAdjacent(
434 ndn::util::FaceUri(faceEventNotification.getRemoteUri()));
435 // If we have a neighbor by that FaceUri and it has no FaceId, we
436 // have a match.
437 if (adjacent != m_adjacencyList.end()) {
438 _LOG_DEBUG("Face creation event matches neighbor: " << adjacent->getName()
439 << ". New Face ID: " << faceEventNotification.getFaceId()
440 << ". Registering prefixes.");
441 adjacent->setFaceId(faceEventNotification.getFaceId());
442
443 registerAdjacencyPrefixes(*adjacent, ndn::time::milliseconds::max());
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500444
445 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
446 getRoutingTable().scheduleRoutingTableCalculation(*this);
447 }
448 else {
449 m_nlsrLsdb.scheduleAdjLsaBuild();
450 }
Nick Gordond5c1a372016-10-31 13:56:23 -0500451 }
452 break;
453 }
454 default:
455 break;
akmhoquec04e7272014-07-02 11:00:14 -0500456 }
akmhoquee1765152014-06-30 11:32:01 -0500457}
458
Nick Gordond5c1a372016-10-31 13:56:23 -0500459void
460Nlsr::initializeFaces(const FetchDatasetCallback& onFetchSuccess,
461 const FetchDatasetTimeoutCallback& onFetchFailure)
462{
463 _LOG_TRACE("Initializing Faces...");
464
465 m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(onFetchSuccess, onFetchFailure);
466
467}
468
469void
470Nlsr::processFaceDataset(const std::vector<ndn::nfd::FaceStatus>& faces)
471{
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500472 _LOG_DEBUG("Processing face dataset");
Nick Gordond5c1a372016-10-31 13:56:23 -0500473
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500474 // Iterate over each neighbor listed in nlsr.conf
475 for (auto& adjacent : m_adjacencyList.getAdjList()) {
476
477 const std::string faceUriString = adjacent.getFaceUri().toString();
Nick Gordond5c1a372016-10-31 13:56:23 -0500478 // Check the list of FaceStatus objects we got for a match
479 for (const ndn::nfd::FaceStatus& faceStatus : faces) {
Nick Gordond5c1a372016-10-31 13:56:23 -0500480 // Set the adjacency FaceID if we find a URI match and it was
481 // previously unset. Change the boolean to true.
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500482 if (adjacent.getFaceId() == 0 && faceUriString == faceStatus.getRemoteUri()) {
483 _LOG_DEBUG("FaceUri: " << faceStatus.getRemoteUri() <<
484 " FaceId: "<< faceStatus.getFaceId());
485 adjacent.setFaceId(faceStatus.getFaceId());
Nick Gordond5c1a372016-10-31 13:56:23 -0500486 // Register the prefixes for each neighbor
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500487 this->registerAdjacencyPrefixes(adjacent, ndn::time::milliseconds::max());
Nick Gordond5c1a372016-10-31 13:56:23 -0500488 }
489 }
490 // If this adjacency has no information in this dataset, then one
491 // of two things is happening: 1. NFD is starting slowly and this
492 // Face wasn't ready yet, or 2. NFD is configured
493 // incorrectly and this Face isn't available.
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500494 if (adjacent.getFaceId() == 0) {
495 _LOG_WARN("The adjacency " << adjacent.getName() <<
Nick Gordond5c1a372016-10-31 13:56:23 -0500496 " has no Face information in this dataset.");
497 }
498 }
499
Nick Gordond5c1a372016-10-31 13:56:23 -0500500 scheduleDatasetFetch();
501}
502
503void
504Nlsr::registerAdjacencyPrefixes(const Adjacent& adj,
505 const ndn::time::milliseconds& timeout)
506{
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500507 ndn::util::FaceUri faceUri = adj.getFaceUri();
508 double linkCost = adj.getLinkCost();
Nick Gordond5c1a372016-10-31 13:56:23 -0500509
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500510 m_fib.registerPrefix(adj.getName(), faceUri, linkCost,
511 timeout, ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500512
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500513 m_fib.registerPrefix(m_confParam.getChronosyncPrefix(),
514 faceUri, linkCost, timeout,
515 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500516
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500517 m_fib.registerPrefix(m_confParam.getLsaPrefix(),
518 faceUri, linkCost, timeout,
519 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500520
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500521 ndn::Name broadcastKeyPrefix = DEFAULT_BROADCAST_PREFIX;
522 broadcastKeyPrefix.append("KEYS");
523 m_fib.registerPrefix(broadcastKeyPrefix,
524 faceUri, linkCost, timeout,
525 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Nick Gordond5c1a372016-10-31 13:56:23 -0500526}
527
528void
529Nlsr::onFaceDatasetFetchTimeout(uint32_t code,
530 const std::string& msg,
531 uint32_t nRetriesSoFar)
532{
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500533 _LOG_DEBUG("onFaceDatasetFetchTimeout");
Nick Gordond5c1a372016-10-31 13:56:23 -0500534 // If we have exceeded the maximum attempt count, do not try again.
535 if (nRetriesSoFar++ < m_confParam.getFaceDatasetFetchTries()) {
536 _LOG_DEBUG("Failed to fetch dataset: " << msg << ". Attempting retry #" << nRetriesSoFar);
537 m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(std::bind(&Nlsr::processFaceDataset,
538 this, _1),
539 std::bind(&Nlsr::onFaceDatasetFetchTimeout,
540 this, _1, _2, nRetriesSoFar));
541 }
542 else {
543 _LOG_ERROR("Failed to fetch dataset: " << msg << ". Exceeded limit of " <<
544 m_confParam.getFaceDatasetFetchTries() << ", so not trying again this time.");
545 // If we fail to fetch it, just do nothing until the next
546 // interval. Since this is a backup mechanism, we aren't as
547 // concerned with retrying.
548 scheduleDatasetFetch();
549 }
550}
551
552void
553Nlsr::scheduleDatasetFetch()
554{
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500555 _LOG_DEBUG("Scheduling Dataset Fetch in " << m_confParam.getFaceDatasetFetchInterval()
556 << " seconds");
Nick Gordond5c1a372016-10-31 13:56:23 -0500557 m_scheduler.scheduleEvent(m_confParam.getFaceDatasetFetchInterval(),
558 [this] {
559 this->initializeFaces(
560 [this] (const std::vector<ndn::nfd::FaceStatus>& faces) {
561 this->processFaceDataset(faces);
562 },
563 [this] (uint32_t code, const std::string& msg) {
564 this->onFaceDatasetFetchTimeout(code, msg, 0);
565 });
566 });
567}
akmhoquee1765152014-06-30 11:32:01 -0500568
569void
akmhoque53353462014-04-22 08:43:45 -0500570Nlsr::startEventLoop()
571{
akmhoquefdbddb12014-05-02 18:35:19 -0500572 m_nlsrFace.processEvents();
akmhoque53353462014-04-22 08:43:45 -0500573}
akmhoque5a44dd42014-03-12 18:11:32 -0500574
akmhoqueb1710aa2014-02-19 17:13:36 -0600575} // namespace nlsr