blob: 02df4aa7db940bffb87260f00ad2305920b8c1f2 [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
akmhoque298385a2014-02-13 14:13:09 -060022#include <cstdlib>
akmhoque92afde42014-02-18 14:04:07 -060023#include <string>
akmhoque298385a2014-02-13 14:13:09 -060024#include <sstream>
akmhoque05d5fcf2014-04-15 14:58:45 -050025#include <cstdio>
akmhoque0494c252014-07-23 23:46:44 -050026#include <unistd.h>
akmhoque298385a2014-02-13 14:13:09 -060027
28#include "nlsr.hpp"
akmhoque157b0a42014-05-13 00:26:37 -050029#include "adjacent.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050030#include "logger.hpp"
akmhoque2bb198e2014-02-28 11:46:27 -060031
akmhoque298385a2014-02-13 14:13:09 -060032
akmhoque53353462014-04-22 08:43:45 -050033namespace nlsr {
34
akmhoque674b0b12014-05-20 14:33:28 -050035INIT_LOGGER("nlsr");
36
alvy297f4162015-03-03 17:15:33 -060037const ndn::Name Nlsr::LOCALHOST_PREFIX = ndn::Name("/localhost/nlsr");
38
akmhoque53353462014-04-22 08:43:45 -050039using namespace ndn;
40using namespace std;
41
Laqin Fana4cf4022017-01-03 18:57:35 +000042Nlsr::Nlsr(boost::asio::io_service& ioService, ndn::Scheduler& scheduler, ndn::Face& face, ndn::KeyChain& keyChain)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050043 : m_nlsrFace(face)
44 , m_scheduler(scheduler)
Laqin Fana4cf4022017-01-03 18:57:35 +000045 , m_keyChain(keyChain)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050046 , m_confParam()
47 , m_adjacencyList()
48 , m_namePrefixList()
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050049 , m_isDaemonProcess(false)
50 , m_configFileName("nlsr.conf")
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050051 , m_nlsrLsdb(*this, scheduler)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050052 , m_adjBuildCount(0)
53 , m_isBuildAdjLsaSheduled(false)
54 , m_isRouteCalculationScheduled(false)
55 , m_isRoutingTableCalculating(false)
56 , m_routingTable(scheduler)
57 , m_fib(m_nlsrFace, scheduler, m_adjacencyList, m_confParam, m_keyChain)
58 , m_namePrefixTable(*this)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050059 , m_lsdbDatasetHandler(m_nlsrLsdb,
60 m_nlsrFace,
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050061 m_keyChain)
Vince Lehmanf7eec4f2015-05-08 19:02:31 -050062 , m_helloProtocol(*this, scheduler)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050063 , m_certificateCache(new ndn::CertificateCacheTtl(ioService))
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050064 , m_validator(m_nlsrFace, DEFAULT_BROADCAST_PREFIX, m_certificateCache, m_certStore)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050065 , m_prefixUpdateProcessor(m_nlsrFace,
66 m_namePrefixList,
67 m_nlsrLsdb,
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050068 DEFAULT_BROADCAST_PREFIX,
69 m_keyChain,
Vince Lehmanc2acdcb2015-04-29 11:14:35 -050070 m_certificateCache,
71 m_certStore)
Nick Gordond1f4f332017-01-20 13:40:57 -060072 , m_dispatcher(m_nlsrFace, m_keyChain, m_signingInfo)
Nick Gordon4d2c6c02017-01-20 13:18:46 -060073 , m_nfdRibCommandProcessor(m_dispatcher,
74 m_namePrefixList,
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050075 m_nlsrLsdb)
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{
dmcoomes9f936662017-03-02 10:33:09 -060086 std::cerr << "ERROR: Failed to register prefix in local hub's daemon" << std::endl;
87 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{
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050093 _LOG_DEBUG("Successfully registered prefix: " << name);
94
Jiewen Tana0497d82015-02-02 21:59:18 -080095 if (name.equals(m_confParam.getRouterPrefix())) {
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050096 m_lsdbDatasetHandler.startListeningOnRouterPrefix();
Jiewen Tana0497d82015-02-02 21:59:18 -080097 }
akmhoque157b0a42014-05-13 00:26:37 -050098}
akmhoque1fd8c1e2014-02-19 19:41:49 -060099
akmhoque53353462014-04-22 08:43:45 -0500100void
alvy297f4162015-03-03 17:15:33 -0600101Nlsr::onLocalhostRegistrationSuccess(const ndn::Name& name)
102{
103 _LOG_DEBUG("Successfully registered prefix: " << name);
104
105 m_prefixUpdateProcessor.startListening();
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500106 m_lsdbDatasetHandler.startListeningOnLocalhost();
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600107 // Dispatcher prefix registrations
108 m_nfdRibCommandProcessor.startListening();
109 // All dispatcher-related sub-prefixes *must* be registered before
110 // the top-level prefixes are added.
111 try {
112 m_dispatcher.addTopPrefix(LOCALHOST_PREFIX, false, m_signingInfo);
113 }
114 catch (const std::exception& e) {
115 _LOG_ERROR("Error setting top-level prefix in dispatcher: " << e.what() << "\n");
116 }
alvy297f4162015-03-03 17:15:33 -0600117}
118
119void
akmhoque31d1d4b2014-05-05 22:08:14 -0500120Nlsr::setInfoInterestFilter()
akmhoque53353462014-04-22 08:43:45 -0500121{
akmhoque31d1d4b2014-05-05 22:08:14 -0500122 ndn::Name name(m_confParam.getRouterPrefix());
akmhoque674b0b12014-05-20 14:33:28 -0500123 _LOG_DEBUG("Setting interest filter for name: " << name);
akmhoquefdbddb12014-05-02 18:35:19 -0500124 getNlsrFace().setInterestFilter(name,
Joao Pereira97473d42015-07-03 16:57:27 -0400125 std::bind(&HelloProtocol::processInterest,
akmhoque31d1d4b2014-05-05 22:08:14 -0500126 &m_helloProtocol, _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);
akmhoque31d1d4b2014-05-05 22:08:14 -0500131}
132
133void
134Nlsr::setLsaInterestFilter()
135{
akmhoque157b0a42014-05-13 00:26:37 -0500136 ndn::Name name = m_confParam.getLsaPrefix();
akmhoque50125a92014-06-30 08:54:17 -0500137 name.append(m_confParam.getSiteName());
138 name.append(m_confParam.getRouterName());
akmhoque674b0b12014-05-20 14:33:28 -0500139 _LOG_DEBUG("Setting interest filter for name: " << name);
akmhoque31d1d4b2014-05-05 22:08:14 -0500140 getNlsrFace().setInterestFilter(name,
Joao Pereira97473d42015-07-03 16:57:27 -0400141 std::bind(&Lsdb::processInterest,
akmhoque31d1d4b2014-05-05 22:08:14 -0500142 &m_nlsrLsdb, _1, _2),
Joao Pereira97473d42015-07-03 16:57:27 -0400143 std::bind(&Nlsr::onRegistrationSuccess, this, _1),
144 std::bind(&Nlsr::registrationFailed, this, _1),
145 m_signingInfo,
akmhoque060d3022014-08-12 13:35:06 -0500146 ndn::nfd::ROUTE_FLAG_CAPTURE);
akmhoque53353462014-04-22 08:43:45 -0500147}
148
149void
akmhoquec04e7272014-07-02 11:00:14 -0500150Nlsr::setStrategies()
akmhoque157b0a42014-05-13 00:26:37 -0500151{
Vince Lehman53c0e3e2015-09-14 14:33:20 -0500152 const std::string strategy("ndn:/localhost/nfd/strategy/multicast");
153
akmhoque3cb0cfc2014-06-24 10:32:24 -0500154 ndn::Name broadcastKeyPrefix = DEFAULT_BROADCAST_PREFIX;
155 broadcastKeyPrefix.append("KEYS");
Vince Lehman53c0e3e2015-09-14 14:33:20 -0500156
akmhoque393d4ff2014-07-16 14:27:03 -0500157 m_fib.setStrategy(m_confParam.getLsaPrefix(), strategy, 0);
158 m_fib.setStrategy(broadcastKeyPrefix, strategy, 0);
159 m_fib.setStrategy(m_confParam.getChronosyncPrefix(), strategy, 0);
akmhoque157b0a42014-05-13 00:26:37 -0500160}
161
162void
akmhoque0494c252014-07-23 23:46:44 -0500163Nlsr::daemonize()
164{
165 pid_t process_id = 0;
166 pid_t sid = 0;
167 process_id = fork();
168 if (process_id < 0){
169 std::cerr << "Daemonization failed!" << std::endl;
dmcoomes9f936662017-03-02 10:33:09 -0600170 BOOST_THROW_EXCEPTION(Error("Error: Daemonization process- fork failed!"));
akmhoque0494c252014-07-23 23:46:44 -0500171 }
172 if (process_id > 0) {
173 _LOG_DEBUG("Process daemonized. Process id: " << process_id);
174 exit(0);
175 }
176
177 umask(0);
178 sid = setsid();
179 if(sid < 0) {
dmcoomes9f936662017-03-02 10:33:09 -0600180 BOOST_THROW_EXCEPTION(Error("Error: Daemonization process- setting id failed!"));
akmhoque0494c252014-07-23 23:46:44 -0500181 }
182
183 if (chdir("/") < 0) {
dmcoomes9f936662017-03-02 10:33:09 -0600184 BOOST_THROW_EXCEPTION(Error("Error: Daemonization process-chdir failed!"));
akmhoque0494c252014-07-23 23:46:44 -0500185 }
186}
187
188void
Nick Gordon9461afb2017-04-25 15:54:50 -0500189Nlsr::canonizeContinuation(std::list<Adjacent>::iterator iterator)
190{
191 canonizeNeighborUris(iterator, [this] (std::list<Adjacent>::iterator iterator) {
192 canonizeContinuation(iterator);
193 });
194}
195
196void
197Nlsr::canonizeNeighborUris(std::list<Adjacent>::iterator currentNeighbor,
198 std::function<void(std::list<Adjacent>::iterator)> then)
199{
200 if (currentNeighbor != m_adjacencyList.getAdjList().end()) {
Nick Gordone9733ed2017-04-26 10:48:39 -0500201 ndn::util::FaceUri uri(currentNeighbor->getFaceUri());
Nick Gordon9461afb2017-04-25 15:54:50 -0500202 uri.canonize([this, then, currentNeighbor] (ndn::util::FaceUri canonicalUri) {
Nick Gordone9733ed2017-04-26 10:48:39 -0500203 _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) {
Nick Gordone9733ed2017-04-26 10:48:39 -0500209 _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 }
216 // We have finished canonizing all neighbors, so initialize NLSR.
217 else {
218 initialize();
219 }
220}
221
222void
akmhoque53353462014-04-22 08:43:45 -0500223Nlsr::initialize()
224{
akmhoque674b0b12014-05-20 14:33:28 -0500225 _LOG_DEBUG("Initializing Nlsr");
akmhoque53353462014-04-22 08:43:45 -0500226 m_confParam.buildRouterPrefix();
Muktadir R Chowdhury3ac07282016-06-17 16:30:29 -0500227 m_lsdbDatasetHandler.setRouterNameCommandPrefix(m_confParam.getRouterPrefix());
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700228 m_nlsrLsdb.setLsaRefreshTime(ndn::time::seconds(m_confParam.getLsaRefreshTime()));
akmhoque31d1d4b2014-05-05 22:08:14 -0500229 m_nlsrLsdb.setThisRouterPrefix(m_confParam.getRouterPrefix().toUri());
akmhoque53353462014-04-22 08:43:45 -0500230 m_fib.setEntryRefreshTime(2 * m_confParam.getLsaRefreshTime());
Vince Lehmanc11cc202015-01-20 11:41:33 -0600231
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500232 m_nlsrLsdb.getSequencingManager().setSeqFileDirectory(m_confParam.getSeqFileDir());
233 m_nlsrLsdb.getSequencingManager().initiateSeqNoFromFile(m_confParam.getHyperbolicState());
234
235 m_nlsrLsdb.getSyncLogicHandler().createSyncSocket(m_confParam.getChronosyncPrefix());
Vince Lehmanc11cc202015-01-20 11:41:33 -0600236
dmcoomes9f936662017-03-02 10:33:09 -0600237 // Logging start
akmhoque674b0b12014-05-20 14:33:28 -0500238 m_confParam.writeLog();
239 m_adjacencyList.writeLog();
Nick Gordon494de402017-06-14 12:48:51 -0500240 _LOG_DEBUG(m_namePrefixList);
dmcoomes9f936662017-03-02 10:33:09 -0600241 // Logging end
akmhoque443ad812014-07-29 10:26:56 -0500242 initializeKey();
akmhoquec04e7272014-07-02 11:00:14 -0500243 setStrategies();
Joao Pereira97473d42015-07-03 16:57:27 -0400244 _LOG_DEBUG("Default NLSR identity: " << m_signingInfo.getSignerName());
akmhoque31d1d4b2014-05-05 22:08:14 -0500245 setInfoInterestFilter();
246 setLsaInterestFilter();
Vince Lehman50df6b72015-03-03 12:06:40 -0600247
248 // Set event intervals
249 setFirstHelloInterval(m_confParam.getFirstHelloInterval());
250 m_nlsrLsdb.setAdjLsaBuildInterval(m_confParam.getAdjLsaBuildInterval());
251 m_routingTable.setRoutingCalcInterval(m_confParam.getRoutingCalcInterval());
252
akmhoque674b0b12014-05-20 14:33:28 -0500253 m_nlsrLsdb.buildAndInstallOwnNameLsa();
Nick Gordon5c467f02016-07-13 13:40:10 -0500254
255 // Install coordinate LSAs if using HR or dry-run HR.
256 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
257 m_nlsrLsdb.buildAndInstallOwnCoordinateLsa();
258 }
Vince Lehman904c2412014-09-23 19:36:11 -0500259
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700260 registerKeyPrefix();
alvy297f4162015-03-03 17:15:33 -0600261 registerLocalhostPrefix();
Vince Lehman7b616582014-10-17 16:25:39 -0500262
Vince Lehman7b616582014-10-17 16:25:39 -0500263 m_helloProtocol.scheduleInterest(m_firstHelloInterval);
Vince Lehman09131122014-09-09 17:10:11 -0500264
265 // Need to set direct neighbors' costs to 0 for hyperbolic routing
266 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
267
268 std::list<Adjacent>& neighbors = m_adjacencyList.getAdjList();
269
270 for (std::list<Adjacent>::iterator it = neighbors.begin(); it != neighbors.end(); ++it) {
271 it->setLinkCost(0);
272 }
273 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700274}
275
276void
akmhoque443ad812014-07-29 10:26:56 -0500277Nlsr::initializeKey()
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700278{
Joao Pereira97473d42015-07-03 16:57:27 -0400279 ndn::Name defaultIdentity = m_confParam.getRouterPrefix();
280 defaultIdentity.append("NLSR");
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700281
Joao Pereira97473d42015-07-03 16:57:27 -0400282 try {
283 m_keyChain.deleteIdentity(defaultIdentity);
akmhoque102aea42014-08-04 10:22:12 -0500284 }
dmcoomes9f936662017-03-02 10:33:09 -0600285 catch (const std::exception& e) {
akmhoque102aea42014-08-04 10:22:12 -0500286 }
Joao Pereira97473d42015-07-03 16:57:27 -0400287 m_signingInfo = ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_ID, defaultIdentity);
akmhoque443ad812014-07-29 10:26:56 -0500288
Joao Pereira97473d42015-07-03 16:57:27 -0400289 ndn::Name keyName = m_keyChain.generateRsaKeyPairAsDefault(defaultIdentity, true);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700290
dmcoomes9f936662017-03-02 10:33:09 -0600291 std::shared_ptr<ndn::IdentityCertificate> certificate =
292 std::make_shared<ndn::IdentityCertificate>();
293 std::shared_ptr<ndn::PublicKey> pubKey = m_keyChain.getPublicKey(keyName);
Yingdi Yu191f5fa2014-08-06 17:08:52 -0700294 Name certificateName = keyName.getPrefix(-1);
295 certificateName.append("KEY").append(keyName.get(-1)).append("ID-CERT").appendVersion();
296 certificate->setName(certificateName);
297 certificate->setNotBefore(time::system_clock::now() - time::days(1));
298 certificate->setNotAfter(time::system_clock::now() + time::days(7300)); // ~20 years
299 certificate->setPublicKeyInfo(*pubKey);
300 certificate->addSubjectDescription(CertificateSubjectDescription(ndn::oid::ATTRIBUTE_NAME,
301 keyName.toUri()));
302 certificate->encode();
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700303 m_keyChain.signByIdentity(*certificate, m_confParam.getRouterPrefix());
304
305 m_keyChain.addCertificateAsIdentityDefault(*certificate);
306 loadCertToPublish(certificate);
307
308 m_defaultCertName = certificate->getName();
309}
310
311void
312Nlsr::registerKeyPrefix()
313{
314 ndn::Name keyPrefix = DEFAULT_BROADCAST_PREFIX;
315 keyPrefix.append("KEYS");
316 m_nlsrFace.setInterestFilter(keyPrefix,
Joao Pereira97473d42015-07-03 16:57:27 -0400317 std::bind(&Nlsr::onKeyInterest,
Yingdi Yu6a3a4dd2014-06-20 14:10:39 -0700318 this, _1, _2),
Joao Pereira97473d42015-07-03 16:57:27 -0400319 std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1),
320 std::bind(&Nlsr::registrationFailed, this, _1),
321 m_signingInfo,
akmhoque060d3022014-08-12 13:35:06 -0500322 ndn::nfd::ROUTE_FLAG_CAPTURE);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700323
324}
325
326void
alvy297f4162015-03-03 17:15:33 -0600327Nlsr::registerLocalhostPrefix()
328{
alvy297f4162015-03-03 17:15:33 -0600329 m_nlsrFace.registerPrefix(LOCALHOST_PREFIX,
330 std::bind(&Nlsr::onLocalhostRegistrationSuccess, this, _1),
331 std::bind(&Nlsr::registrationFailed, this, _1));
332}
333
334void
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700335Nlsr::onKeyInterest(const ndn::Name& name, const ndn::Interest& interest)
336{
337 const ndn::Name& interestName = interest.getName();
338
339 ndn::Name certName = interestName.getSubName(name.size());
340
341 if (certName[-2].toUri() == "ID-CERT")
342 {
343 certName = certName.getPrefix(-1);
344 }
345 else if (certName[-1].toUri() != "ID-CERT")
dmcoomes9eaf3f42017-02-21 11:39:01 -0600346 {
347 _LOG_DEBUG("certName for interest " << interest << " is malformed,"
348 << " contains incorrect namespace syntax");
349 return;
350 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700351
dmcoomes9f936662017-03-02 10:33:09 -0600352 std::shared_ptr<const ndn::IdentityCertificate> cert = getCertificate(certName);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700353
354 if (!static_cast<bool>(cert))
dmcoomes9eaf3f42017-02-21 11:39:01 -0600355 {
356 _LOG_DEBUG("cert is not found for " << interest);
357 return; // cert is not found
358 }
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700359
dmcoomes9f936662017-03-02 10:33:09 -0600360 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>();
akmhoque69c9aa92014-07-23 15:15:05 -0500361 data->setName(interestName);
362 data->setContent(cert->wireEncode());
363 m_keyChain.signWithSha256(*data);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700364
akmhoque69c9aa92014-07-23 15:15:05 -0500365 m_nlsrFace.put(*data);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700366}
367
368void
369Nlsr::onKeyPrefixRegSuccess(const ndn::Name& name)
370{
akmhoque53353462014-04-22 08:43:45 -0500371}
akmhoque5a44dd42014-03-12 18:11:32 -0500372
akmhoque53353462014-04-22 08:43:45 -0500373void
akmhoquee1765152014-06-30 11:32:01 -0500374Nlsr::onDestroyFaceSuccess(const ndn::nfd::ControlParameters& commandSuccessResult)
375{
akmhoquee1765152014-06-30 11:32:01 -0500376}
377
378void
Junxiao Shi63bd0342016-08-17 16:57:14 +0000379Nlsr::onDestroyFaceFailure(const ndn::nfd::ControlResponse& response)
akmhoquee1765152014-06-30 11:32:01 -0500380{
Junxiao Shi63bd0342016-08-17 16:57:14 +0000381 std::cerr << response.getText() << " (code: " << response.getCode() << ")";
dmcoomes9f936662017-03-02 10:33:09 -0600382 BOOST_THROW_EXCEPTION(Error("Error: Face destruction failed"));
akmhoquee1765152014-06-30 11:32:01 -0500383}
384
385void
386Nlsr::destroyFaces()
387{
388 std::list<Adjacent>& adjacents = m_adjacencyList.getAdjList();
389 for (std::list<Adjacent>::iterator it = adjacents.begin();
390 it != adjacents.end(); it++) {
Nick Gordone9733ed2017-04-26 10:48:39 -0500391 m_fib.destroyFace((*it).getFaceUri().toString(),
Joao Pereira97473d42015-07-03 16:57:27 -0400392 std::bind(&Nlsr::onDestroyFaceSuccess, this, _1),
Junxiao Shi63bd0342016-08-17 16:57:14 +0000393 std::bind(&Nlsr::onDestroyFaceFailure, this, _1));
akmhoquee1765152014-06-30 11:32:01 -0500394 }
395}
396
akmhoquec04e7272014-07-02 11:00:14 -0500397
398
akmhoquee1765152014-06-30 11:32:01 -0500399
400void
akmhoquec04e7272014-07-02 11:00:14 -0500401Nlsr::onFaceEventNotification(const ndn::nfd::FaceEventNotification& faceEventNotification)
akmhoquee1765152014-06-30 11:32:01 -0500402{
Vince Lehman02e32992015-03-11 12:31:20 -0500403 _LOG_TRACE("Nlsr::onFaceEventNotification called");
akmhoquec04e7272014-07-02 11:00:14 -0500404 ndn::nfd::FaceEventKind kind = faceEventNotification.getKind();
Vince Lehman02e32992015-03-11 12:31:20 -0500405
akmhoquec04e7272014-07-02 11:00:14 -0500406 if (kind == ndn::nfd::FACE_EVENT_DESTROYED) {
407 uint64_t faceId = faceEventNotification.getFaceId();
Vince Lehman02e32992015-03-11 12:31:20 -0500408
Nick Gordonc780a692017-04-27 18:03:02 -0500409 auto adjacent = m_adjacencyList.findAdjacent(faceId);
Vince Lehman02e32992015-03-11 12:31:20 -0500410
Nick Gordonc780a692017-04-27 18:03:02 -0500411 if (adjacent != m_adjacencyList.end()) {
Vince Lehman02e32992015-03-11 12:31:20 -0500412 _LOG_DEBUG("Face to " << adjacent->getName() << " with face id: " << faceId << " destroyed");
413
akmhoquec04e7272014-07-02 11:00:14 -0500414 adjacent->setFaceId(0);
Vince Lehman02e32992015-03-11 12:31:20 -0500415
Nick G97e34942016-07-11 14:46:27 -0500416 // Only trigger an Adjacency LSA build if this node is changing
417 // from ACTIVE to INACTIVE since this rebuild will effectively
418 // cancel the previous Adjacency LSA refresh event and schedule
419 // a new one further in the future.
Vince Lehman199e9cf2015-04-07 13:22:16 -0500420 //
Nick G97e34942016-07-11 14:46:27 -0500421 // Continuously scheduling the refresh in the future will block
422 // the router from refreshing its Adjacency LSA. Since other
423 // routers' Name prefixes' expiration times are updated when
424 // this router refreshes its Adjacency LSA, the other routers'
425 // prefixes will expire and be removed from the RIB.
Vince Lehman199e9cf2015-04-07 13:22:16 -0500426 //
Nick G97e34942016-07-11 14:46:27 -0500427 // This check is required to fix Bug #2733 for now. This check
428 // would be unnecessary to fix Bug #2733 when Issue #2732 is
429 // completed, but the check also helps with optimization so it
430 // can remain even when Issue #2732 is implemented.
Vince Lehman199e9cf2015-04-07 13:22:16 -0500431 if (adjacent->getStatus() == Adjacent::STATUS_ACTIVE) {
432 adjacent->setStatus(Adjacent::STATUS_INACTIVE);
Vince Lehman02e32992015-03-11 12:31:20 -0500433
Vince Lehman199e9cf2015-04-07 13:22:16 -0500434 // A new adjacency LSA cannot be built until the neighbor is marked INACTIVE and
435 // has met the HELLO retry threshold
436 adjacent->setInterestTimedOutNo(m_confParam.getInterestRetryNumber());
437
Nick Gordone8e03ac2016-07-07 14:24:38 -0500438 if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
439 getRoutingTable().scheduleRoutingTableCalculation(*this);
440 }
441 else {
442 m_nlsrLsdb.scheduleAdjLsaBuild();
443 }
Vince Lehman199e9cf2015-04-07 13:22:16 -0500444 }
akmhoquec04e7272014-07-02 11:00:14 -0500445 }
446 }
akmhoquee1765152014-06-30 11:32:01 -0500447}
448
449
450void
akmhoque53353462014-04-22 08:43:45 -0500451Nlsr::startEventLoop()
452{
akmhoquefdbddb12014-05-02 18:35:19 -0500453 m_nlsrFace.processEvents();
akmhoque53353462014-04-22 08:43:45 -0500454}
akmhoque5a44dd42014-03-12 18:11:32 -0500455
akmhoqueb1710aa2014-02-19 17:13:36 -0600456} // namespace nlsr