akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, The University of Memphis, |
Vince Lehman | c2e51f6 | 2015-01-20 15:03:11 -0600 | [diff] [blame] | 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 6 | * |
| 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/>. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 20 | **/ |
Muktadir R Chowdhury | bfa2760 | 2014-10-31 10:57:41 -0500 | [diff] [blame] | 21 | |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 22 | #include "nlsr.hpp" |
| 23 | #include "adjacent.hpp" |
| 24 | #include "logger.hpp" |
| 25 | |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 26 | #include <cstdlib> |
akmhoque | 92afde4 | 2014-02-18 14:04:07 -0600 | [diff] [blame] | 27 | #include <string> |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 28 | #include <sstream> |
akmhoque | 05d5fcf | 2014-04-15 14:58:45 -0500 | [diff] [blame] | 29 | #include <cstdio> |
akmhoque | 0494c25 | 2014-07-23 23:46:44 -0500 | [diff] [blame] | 30 | #include <unistd.h> |
Muktadir Chowdhury | c3ea26f | 2018-01-05 21:40:59 +0000 | [diff] [blame] | 31 | #include <vector> |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 32 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 33 | #include <ndn-cxx/net/face-uri.hpp> |
| 34 | #include <ndn-cxx/signature.hpp> |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 35 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 36 | namespace nlsr { |
| 37 | |
dmcoomes | cf8d0ed | 2017-02-21 11:39:01 -0600 | [diff] [blame] | 38 | INIT_LOGGER(Nlsr); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 39 | |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 40 | const ndn::Name Nlsr::LOCALHOST_PREFIX = ndn::Name("/localhost/nlsr"); |
| 41 | |
Laqin Fan | a4cf402 | 2017-01-03 18:57:35 +0000 | [diff] [blame] | 42 | Nlsr::Nlsr(boost::asio::io_service& ioService, ndn::Scheduler& scheduler, ndn::Face& face, ndn::KeyChain& keyChain) |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 43 | : m_nlsrFace(face) |
| 44 | , m_scheduler(scheduler) |
Laqin Fan | a4cf402 | 2017-01-03 18:57:35 +0000 | [diff] [blame] | 45 | , m_keyChain(keyChain) |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 46 | , m_confParam() |
| 47 | , m_adjacencyList() |
| 48 | , m_namePrefixList() |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 49 | , m_configFileName("nlsr.conf") |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 50 | , m_nlsrLsdb(*this, scheduler) |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 51 | , 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 Gordon | b7b5839 | 2017-08-17 16:29:21 -0500 | [diff] [blame] | 57 | , m_namePrefixTable(*this, m_routingTable.afterRoutingChange) |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 58 | , m_dispatcher(m_nlsrFace, m_keyChain) |
| 59 | , m_datasetHandler(m_nlsrLsdb, |
| 60 | m_routingTable, |
| 61 | m_dispatcher, |
| 62 | m_nlsrFace, |
| 63 | m_keyChain) |
Vince Lehman | f7eec4f | 2015-05-08 19:02:31 -0500 | [diff] [blame] | 64 | , m_helloProtocol(*this, scheduler) |
Davide Pesavento | a08dc3f | 2018-05-24 00:40:28 -0400 | [diff] [blame] | 65 | , m_validator(std::make_unique<ndn::security::v2::CertificateFetcherDirectFetch>(m_nlsrFace)) |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 66 | , m_controller(m_nlsrFace, m_keyChain) |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 67 | , m_faceDatasetController(m_nlsrFace, m_keyChain) |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 68 | , m_prefixUpdateProcessor(m_dispatcher, |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 69 | m_nlsrFace, |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 70 | m_namePrefixList, |
Saurab Dulal | 7526cee | 2018-01-31 18:14:10 +0000 | [diff] [blame] | 71 | m_nlsrLsdb, |
| 72 | m_configFileName) |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 73 | , m_nfdRibCommandProcessor(m_dispatcher, |
Nick Gordon | 4d2c6c0 | 2017-01-20 13:18:46 -0600 | [diff] [blame] | 74 | m_namePrefixList, |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 75 | m_nlsrLsdb) |
Alejandro Gil Torres | e0d2048 | 2016-03-06 23:56:19 -0600 | [diff] [blame] | 76 | , m_statsCollector(m_nlsrLsdb, m_helloProtocol) |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 77 | , m_faceMonitor(m_nlsrFace) |
| 78 | , m_firstHelloInterval(FIRST_HELLO_INTERVAL_DEFAULT) |
| 79 | { |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 80 | m_faceMonitor.onNotification.connect(std::bind(&Nlsr::onFaceEventNotification, this, _1)); |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 81 | m_faceMonitor.start(); |
| 82 | } |
| 83 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 84 | void |
| 85 | Nlsr::registrationFailed(const ndn::Name& name) |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 86 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 87 | NLSR_LOG_ERROR("ERROR: Failed to register prefix in local hub's daemon"); |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 88 | BOOST_THROW_EXCEPTION(Error("Error: Prefix registration failed")); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 89 | } |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 90 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 91 | void |
| 92 | Nlsr::onRegistrationSuccess(const ndn::Name& name) |
| 93 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 94 | NLSR_LOG_DEBUG("Successfully registered prefix: " << name); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 98 | Nlsr::setInfoInterestFilter() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 99 | { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 100 | ndn::Name name(m_confParam.getRouterPrefix()); |
Ashlesh Gawande | cba0ae2 | 2018-03-27 17:57:56 -0500 | [diff] [blame] | 101 | name.append("nlsr"); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 102 | name.append("INFO"); |
| 103 | |
| 104 | NLSR_LOG_DEBUG("Setting interest filter for Hello interest: " << name); |
| 105 | |
Ashlesh Gawande | 3494f73 | 2018-11-06 16:04:03 -0600 | [diff] [blame] | 106 | m_nlsrFace.setInterestFilter(ndn::InterestFilter(name).allowLoopback(false), |
Ashlesh Gawande | cba0ae2 | 2018-03-27 17:57:56 -0500 | [diff] [blame] | 107 | std::bind(&HelloProtocol::processInterest, |
| 108 | &m_helloProtocol, _1, _2), |
| 109 | std::bind(&Nlsr::onRegistrationSuccess, this, _1), |
| 110 | std::bind(&Nlsr::registrationFailed, this, _1), |
| 111 | m_signingInfo, |
| 112 | ndn::nfd::ROUTE_FLAG_CAPTURE); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void |
| 116 | Nlsr::setLsaInterestFilter() |
| 117 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 118 | ndn::Name name = m_confParam.getLsaPrefix(); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 119 | |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 120 | NLSR_LOG_DEBUG("Setting interest filter for LsaPrefix: " << name); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 121 | |
Ashlesh Gawande | 3494f73 | 2018-11-06 16:04:03 -0600 | [diff] [blame] | 122 | m_nlsrFace.setInterestFilter(ndn::InterestFilter(name).allowLoopback(false), |
Ashlesh Gawande | cba0ae2 | 2018-03-27 17:57:56 -0500 | [diff] [blame] | 123 | std::bind(&Lsdb::processInterest, |
| 124 | &m_nlsrLsdb, _1, _2), |
| 125 | std::bind(&Nlsr::onRegistrationSuccess, this, _1), |
| 126 | std::bind(&Nlsr::registrationFailed, this, _1), |
| 127 | m_signingInfo, |
| 128 | ndn::nfd::ROUTE_FLAG_CAPTURE); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 129 | } |
| 130 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 131 | |
| 132 | void |
| 133 | Nlsr::addDispatcherTopPrefix(const ndn::Name& topPrefix) |
| 134 | { |
| 135 | try { |
Ashlesh Gawande | cba0ae2 | 2018-03-27 17:57:56 -0500 | [diff] [blame] | 136 | // false since we want to have control over the registration process |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 137 | m_dispatcher.addTopPrefix(topPrefix, false, m_signingInfo); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 138 | } |
| 139 | catch (const std::exception& e) { |
| 140 | NLSR_LOG_ERROR("Error setting top-level prefix in dispatcher: " << e.what() << "\n"); |
| 141 | } |
| 142 | } |
| 143 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 144 | void |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 145 | Nlsr::setStrategies() |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 146 | { |
Vince Lehman | 53c0e3e | 2015-09-14 14:33:20 -0500 | [diff] [blame] | 147 | const std::string strategy("ndn:/localhost/nfd/strategy/multicast"); |
| 148 | |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 149 | m_fib.setStrategy(m_confParam.getLsaPrefix(), strategy, 0); |
akmhoque | 393d4ff | 2014-07-16 14:27:03 -0500 | [diff] [blame] | 150 | m_fib.setStrategy(m_confParam.getChronosyncPrefix(), strategy, 0); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 154 | Nlsr::loadCertToPublish(const ndn::security::v2::Certificate& certificate) |
| 155 | { |
Muktadir Chowdhury | c3ea26f | 2018-01-05 21:40:59 +0000 | [diff] [blame] | 156 | NLSR_LOG_TRACE("Loading cert to publish."); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 157 | m_certStore.insert(certificate); |
| 158 | m_validator.loadAnchor("Authoritative-Certificate", |
| 159 | ndn::security::v2::Certificate(certificate)); |
| 160 | m_prefixUpdateProcessor.getValidator(). |
| 161 | loadAnchor("Authoritative-Certificate", |
| 162 | ndn::security::v2::Certificate(certificate)); |
| 163 | } |
| 164 | |
Nick Gordon | 9461afb | 2017-04-25 15:54:50 -0500 | [diff] [blame] | 165 | void |
Muktadir Chowdhury | c3ea26f | 2018-01-05 21:40:59 +0000 | [diff] [blame] | 166 | Nlsr::connectToFetcher(ndn::util::SegmentFetcher& fetcher) |
| 167 | { |
| 168 | NLSR_LOG_TRACE("NLSR: Connect to SegmentFetcher."); |
| 169 | |
| 170 | fetcher.afterSegmentValidated.connect(std::bind(&Nlsr::afterFetcherSignalEmitted, |
| 171 | this, _1)); |
| 172 | } |
| 173 | |
| 174 | void |
| 175 | Nlsr::afterFetcherSignalEmitted(const ndn::Data& lsaSegment) |
| 176 | { |
| 177 | NLSR_LOG_TRACE("SegmentFetcher fetched a data segment. Start inserting cert to own cert store."); |
| 178 | ndn::Name keyName = lsaSegment.getSignature().getKeyLocator().getName(); |
| 179 | if (getCertificate(keyName) == nullptr) { |
| 180 | publishCertFromCache(keyName); |
| 181 | } |
| 182 | else { |
| 183 | NLSR_LOG_TRACE("Certificate is already in the store: " << keyName); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | void |
| 188 | Nlsr::publishCertFromCache(const ndn::Name& keyName) |
| 189 | { |
| 190 | const ndn::security::v2::Certificate* cert = m_validator.getUnverifiedCertCache() |
| 191 | .find(keyName); |
| 192 | if (cert != nullptr) { |
| 193 | m_certStore.insert(*cert); |
| 194 | NLSR_LOG_TRACE(*cert); |
Ashlesh Gawande | 3494f73 | 2018-11-06 16:04:03 -0600 | [diff] [blame] | 195 | ndn::Name certName = ndn::security::v2::extractKeyNameFromCertName(cert->getName()); |
| 196 | NLSR_LOG_TRACE("Setting interest filter for: " << certName); |
| 197 | m_nlsrFace.setInterestFilter(ndn::InterestFilter(certName).allowLoopback(false), |
Muktadir Chowdhury | c3ea26f | 2018-01-05 21:40:59 +0000 | [diff] [blame] | 198 | std::bind(&Nlsr::onKeyInterest, |
| 199 | this, _1, _2), |
| 200 | std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1), |
| 201 | std::bind(&Nlsr::registrationFailed, this, _1), |
| 202 | m_signingInfo, |
| 203 | ndn::nfd::ROUTE_FLAG_CAPTURE); |
| 204 | |
| 205 | if (!cert->getKeyName().equals(cert->getSignature().getKeyLocator().getName())) { |
| 206 | publishCertFromCache(cert->getSignature().getKeyLocator().getName()); |
| 207 | } |
| 208 | } |
| 209 | else { |
| 210 | NLSR_LOG_TRACE("Cert for " << keyName << " was not found in the Validator's cache. "); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | void |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 215 | Nlsr::initialize() |
| 216 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 217 | NLSR_LOG_DEBUG("Initializing Nlsr"); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 218 | m_confParam.buildRouterPrefix(); |
laqinfan | 3573185 | 2017-08-08 06:17:39 -0500 | [diff] [blame] | 219 | m_datasetHandler.setRouterNameCommandPrefix(m_confParam.getRouterPrefix()); |
Alexander Afanasyev | 411ee4b | 2014-08-16 23:17:03 -0700 | [diff] [blame] | 220 | m_nlsrLsdb.setLsaRefreshTime(ndn::time::seconds(m_confParam.getLsaRefreshTime())); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 221 | m_nlsrLsdb.setThisRouterPrefix(m_confParam.getRouterPrefix().toUri()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 222 | m_fib.setEntryRefreshTime(2 * m_confParam.getLsaRefreshTime()); |
Vince Lehman | c11cc20 | 2015-01-20 11:41:33 -0600 | [diff] [blame] | 223 | |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 224 | m_nlsrLsdb.getSequencingManager().setSeqFileDirectory(m_confParam.getSeqFileDir()); |
| 225 | m_nlsrLsdb.getSequencingManager().initiateSeqNoFromFile(m_confParam.getHyperbolicState()); |
| 226 | |
Ashlesh Gawande | 4810107 | 2018-05-30 17:53:06 -0500 | [diff] [blame] | 227 | m_nlsrLsdb.getSyncLogicHandler().createSyncLogic(m_confParam.getChronosyncPrefix(), |
| 228 | m_confParam.getSyncInterestLifetime()); |
Vince Lehman | c11cc20 | 2015-01-20 11:41:33 -0600 | [diff] [blame] | 229 | |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 230 | // Logging start |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 231 | m_confParam.writeLog(); |
| 232 | m_adjacencyList.writeLog(); |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 233 | NLSR_LOG_DEBUG(m_namePrefixList); |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 234 | // Logging end |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 235 | |
akmhoque | 443ad81 | 2014-07-29 10:26:56 -0500 | [diff] [blame] | 236 | initializeKey(); |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 237 | setStrategies(); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 238 | |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 239 | NLSR_LOG_DEBUG("Default NLSR identity: " << m_signingInfo.getSignerName()); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 240 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 241 | setInfoInterestFilter(); |
| 242 | setLsaInterestFilter(); |
Vince Lehman | 50df6b7 | 2015-03-03 12:06:40 -0600 | [diff] [blame] | 243 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 244 | // add top-level prefixes: router and localhost prefix |
Ashlesh Gawande | cba0ae2 | 2018-03-27 17:57:56 -0500 | [diff] [blame] | 245 | addDispatcherTopPrefix(ndn::Name(m_confParam.getRouterPrefix()).append("nlsr")); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 246 | addDispatcherTopPrefix(LOCALHOST_PREFIX); |
| 247 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 248 | initializeFaces(std::bind(&Nlsr::processFaceDataset, this, _1), |
| 249 | std::bind(&Nlsr::onFaceDatasetFetchTimeout, this, _1, _2, 0)); |
| 250 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 251 | enableIncomingFaceIdIndication(); |
| 252 | |
Vince Lehman | 50df6b7 | 2015-03-03 12:06:40 -0600 | [diff] [blame] | 253 | // Set event intervals |
| 254 | setFirstHelloInterval(m_confParam.getFirstHelloInterval()); |
| 255 | m_nlsrLsdb.setAdjLsaBuildInterval(m_confParam.getAdjLsaBuildInterval()); |
| 256 | m_routingTable.setRoutingCalcInterval(m_confParam.getRoutingCalcInterval()); |
| 257 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 258 | m_nlsrLsdb.buildAndInstallOwnNameLsa(); |
Nick Gordon | 5c467f0 | 2016-07-13 13:40:10 -0500 | [diff] [blame] | 259 | |
| 260 | // Install coordinate LSAs if using HR or dry-run HR. |
| 261 | if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) { |
| 262 | m_nlsrLsdb.buildAndInstallOwnCoordinateLsa(); |
| 263 | } |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 264 | |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 265 | registerKeyPrefix(); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 266 | registerLocalhostPrefix(); |
Ashlesh Gawande | cba0ae2 | 2018-03-27 17:57:56 -0500 | [diff] [blame] | 267 | registerRouterPrefix(); |
Vince Lehman | 7b61658 | 2014-10-17 16:25:39 -0500 | [diff] [blame] | 268 | |
Vince Lehman | 7b61658 | 2014-10-17 16:25:39 -0500 | [diff] [blame] | 269 | m_helloProtocol.scheduleInterest(m_firstHelloInterval); |
Vince Lehman | 0913112 | 2014-09-09 17:10:11 -0500 | [diff] [blame] | 270 | |
| 271 | // Need to set direct neighbors' costs to 0 for hyperbolic routing |
| 272 | if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) { |
| 273 | |
| 274 | std::list<Adjacent>& neighbors = m_adjacencyList.getAdjList(); |
| 275 | |
| 276 | for (std::list<Adjacent>::iterator it = neighbors.begin(); it != neighbors.end(); ++it) { |
| 277 | it->setLinkCost(0); |
| 278 | } |
| 279 | } |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | void |
akmhoque | 443ad81 | 2014-07-29 10:26:56 -0500 | [diff] [blame] | 283 | Nlsr::initializeKey() |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 284 | { |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 285 | NLSR_LOG_DEBUG("Initializing Key ..."); |
| 286 | |
| 287 | ndn::Name nlsrInstanceName = m_confParam.getRouterPrefix(); |
Ashlesh Gawande | cba0ae2 | 2018-03-27 17:57:56 -0500 | [diff] [blame] | 288 | nlsrInstanceName.append("nlsr"); |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 289 | |
Joao Pereira | 97473d4 | 2015-07-03 16:57:27 -0400 | [diff] [blame] | 290 | try { |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 291 | m_keyChain.deleteIdentity(m_keyChain.getPib().getIdentity(nlsrInstanceName)); |
| 292 | } catch (const std::exception& e) { |
| 293 | NLSR_LOG_WARN(e.what()); |
| 294 | } |
| 295 | |
| 296 | auto nlsrInstanceIdentity = m_keyChain.createIdentity(nlsrInstanceName); |
| 297 | auto nlsrInstanceKey = nlsrInstanceIdentity.getDefaultKey(); |
| 298 | |
| 299 | ndn::security::v2::Certificate certificate; |
| 300 | |
| 301 | ndn::Name certificateName = nlsrInstanceKey.getName(); |
| 302 | certificateName.append("NA"); |
| 303 | certificateName.appendVersion(); |
| 304 | certificate.setName(certificateName); |
| 305 | |
| 306 | // set metainfo |
| 307 | certificate.setContentType(ndn::tlv::ContentType_Key); |
Ashlesh Gawande | 0d2c382 | 2018-01-24 17:17:15 -0600 | [diff] [blame] | 308 | certificate.setFreshnessPeriod(ndn::time::days(365)); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 309 | |
| 310 | // set content |
| 311 | certificate.setContent(nlsrInstanceKey.getPublicKey().data(), nlsrInstanceKey.getPublicKey().size()); |
| 312 | |
| 313 | // set signature-info |
| 314 | ndn::SignatureInfo signatureInfo; |
| 315 | signatureInfo.setValidityPeriod(ndn::security::ValidityPeriod(ndn::time::system_clock::TimePoint(), |
| 316 | ndn::time::system_clock::now() |
Ashlesh Gawande | 0d2c382 | 2018-01-24 17:17:15 -0600 | [diff] [blame] | 317 | + ndn::time::days(365))); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 318 | try { |
| 319 | m_keyChain.sign(certificate, |
| 320 | ndn::security::SigningInfo(m_keyChain.getPib().getIdentity(m_confParam.getRouterPrefix())) |
| 321 | .setSignatureInfo(signatureInfo)); |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 322 | } |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 323 | catch (const std::exception& e) { |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 324 | NLSR_LOG_WARN("ERROR: Router's " << e.what() |
| 325 | << "NLSR is running without security." |
| 326 | << " If security is enabled NLSR will not converge."); |
| 327 | |
Muktadir Chowdhury | c3ea26f | 2018-01-05 21:40:59 +0000 | [diff] [blame] | 328 | std::cerr << "Router's " << e.what() << ". NLSR is running without security " |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 329 | << "(Only for testing, should not be used in production.)" |
| 330 | << " If security is enabled NLSR will not converge." << std::endl; |
akmhoque | 102aea4 | 2014-08-04 10:22:12 -0500 | [diff] [blame] | 331 | } |
akmhoque | 443ad81 | 2014-07-29 10:26:56 -0500 | [diff] [blame] | 332 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 333 | m_signingInfo = ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_ID, |
| 334 | nlsrInstanceName); |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 335 | |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 336 | loadCertToPublish(certificate); |
| 337 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 338 | m_defaultCertName = certificate.getName(); |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | void |
| 342 | Nlsr::registerKeyPrefix() |
| 343 | { |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 344 | // Start listening for the interest of this router's NLSR certificate |
| 345 | ndn::Name nlsrKeyPrefix = getConfParameter().getRouterPrefix(); |
Ashlesh Gawande | cba0ae2 | 2018-03-27 17:57:56 -0500 | [diff] [blame] | 346 | nlsrKeyPrefix.append("nlsr"); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 347 | nlsrKeyPrefix.append("KEY"); |
| 348 | |
Ashlesh Gawande | 3494f73 | 2018-11-06 16:04:03 -0600 | [diff] [blame] | 349 | m_nlsrFace.setInterestFilter(ndn::InterestFilter(nlsrKeyPrefix).allowLoopback(false), |
Joao Pereira | 97473d4 | 2015-07-03 16:57:27 -0400 | [diff] [blame] | 350 | std::bind(&Nlsr::onKeyInterest, |
Yingdi Yu | 6a3a4dd | 2014-06-20 14:10:39 -0700 | [diff] [blame] | 351 | this, _1, _2), |
Joao Pereira | 97473d4 | 2015-07-03 16:57:27 -0400 | [diff] [blame] | 352 | std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1), |
| 353 | std::bind(&Nlsr::registrationFailed, this, _1), |
| 354 | m_signingInfo, |
akmhoque | 060d302 | 2014-08-12 13:35:06 -0500 | [diff] [blame] | 355 | ndn::nfd::ROUTE_FLAG_CAPTURE); |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 356 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 357 | // Start listening for the interest of this router's certificate |
| 358 | ndn::Name routerKeyPrefix = getConfParameter().getRouterPrefix(); |
| 359 | routerKeyPrefix.append("KEY"); |
| 360 | |
Ashlesh Gawande | 3494f73 | 2018-11-06 16:04:03 -0600 | [diff] [blame] | 361 | m_nlsrFace.setInterestFilter(ndn::InterestFilter(routerKeyPrefix).allowLoopback(false), |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 362 | std::bind(&Nlsr::onKeyInterest, |
| 363 | this, _1, _2), |
| 364 | std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1), |
| 365 | std::bind(&Nlsr::registrationFailed, this, _1), |
| 366 | m_signingInfo, |
| 367 | ndn::nfd::ROUTE_FLAG_CAPTURE); |
| 368 | |
| 369 | // Start listening for the interest of this router's operator's certificate |
| 370 | ndn::Name operatorKeyPrefix = getConfParameter().getNetwork(); |
| 371 | operatorKeyPrefix.append(getConfParameter().getSiteName()); |
| 372 | operatorKeyPrefix.append(std::string("%C1.Operator")); |
| 373 | |
Ashlesh Gawande | 3494f73 | 2018-11-06 16:04:03 -0600 | [diff] [blame] | 374 | m_nlsrFace.setInterestFilter(ndn::InterestFilter(operatorKeyPrefix).allowLoopback(false), |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 375 | std::bind(&Nlsr::onKeyInterest, |
| 376 | this, _1, _2), |
| 377 | std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1), |
| 378 | std::bind(&Nlsr::registrationFailed, this, _1), |
| 379 | m_signingInfo, |
| 380 | ndn::nfd::ROUTE_FLAG_CAPTURE); |
| 381 | |
| 382 | // Start listening for the interest of this router's site's certificate |
| 383 | ndn::Name siteKeyPrefix = getConfParameter().getNetwork(); |
| 384 | siteKeyPrefix.append(getConfParameter().getSiteName()); |
| 385 | siteKeyPrefix.append("KEY"); |
| 386 | |
Ashlesh Gawande | 3494f73 | 2018-11-06 16:04:03 -0600 | [diff] [blame] | 387 | m_nlsrFace.setInterestFilter(ndn::InterestFilter(siteKeyPrefix).allowLoopback(false), |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 388 | std::bind(&Nlsr::onKeyInterest, |
| 389 | this, _1, _2), |
| 390 | std::bind(&Nlsr::onKeyPrefixRegSuccess, this, _1), |
| 391 | std::bind(&Nlsr::registrationFailed, this, _1), |
| 392 | m_signingInfo, |
| 393 | ndn::nfd::ROUTE_FLAG_CAPTURE); |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | void |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 397 | Nlsr::registerLocalhostPrefix() |
| 398 | { |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 399 | m_nlsrFace.registerPrefix(LOCALHOST_PREFIX, |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 400 | std::bind(&Nlsr::onRegistrationSuccess, this, _1), |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 401 | std::bind(&Nlsr::registrationFailed, this, _1)); |
| 402 | } |
| 403 | |
| 404 | void |
Ashlesh Gawande | cba0ae2 | 2018-03-27 17:57:56 -0500 | [diff] [blame] | 405 | Nlsr::registerRouterPrefix() |
| 406 | { |
| 407 | m_nlsrFace.registerPrefix(ndn::Name(m_confParam.getRouterPrefix()).append("nlsr"), |
| 408 | std::bind(&Nlsr::onRegistrationSuccess, this, _1), |
| 409 | std::bind(&Nlsr::registrationFailed, this, _1)); |
| 410 | } |
| 411 | |
| 412 | void |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 413 | Nlsr::onKeyInterest(const ndn::Name& name, const ndn::Interest& interest) |
| 414 | { |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 415 | NLSR_LOG_DEBUG("Got interest for certificate. Interest: " << interest.getName()); |
| 416 | |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 417 | const ndn::Name& interestName = interest.getName(); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 418 | const ndn::security::v2::Certificate* cert = getCertificate(interestName); |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 419 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 420 | if (cert == nullptr) { |
| 421 | NLSR_LOG_DEBUG("Certificate is not found for: " << interest); |
dmcoomes | 9eaf3f4 | 2017-02-21 11:39:01 -0600 | [diff] [blame] | 422 | return; // cert is not found |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 423 | } |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 424 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 425 | m_nlsrFace.put(*cert); |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | void |
| 429 | Nlsr::onKeyPrefixRegSuccess(const ndn::Name& name) |
| 430 | { |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 431 | NLSR_LOG_DEBUG("KEY prefix: " << name << " registration is successful."); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 432 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 433 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 434 | void |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 435 | Nlsr::onFaceEventNotification(const ndn::nfd::FaceEventNotification& faceEventNotification) |
akmhoque | e176515 | 2014-06-30 11:32:01 -0500 | [diff] [blame] | 436 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 437 | NLSR_LOG_TRACE("Nlsr::onFaceEventNotification called"); |
Vince Lehman | 02e3299 | 2015-03-11 12:31:20 -0500 | [diff] [blame] | 438 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 439 | switch (faceEventNotification.getKind()) { |
| 440 | case ndn::nfd::FACE_EVENT_DESTROYED: { |
| 441 | uint64_t faceId = faceEventNotification.getFaceId(); |
Vince Lehman | 02e3299 | 2015-03-11 12:31:20 -0500 | [diff] [blame] | 442 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 443 | auto adjacent = m_adjacencyList.findAdjacent(faceId); |
Vince Lehman | 02e3299 | 2015-03-11 12:31:20 -0500 | [diff] [blame] | 444 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 445 | if (adjacent != m_adjacencyList.end()) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 446 | NLSR_LOG_DEBUG("Face to " << adjacent->getName() << " with face id: " << faceId << " destroyed"); |
Vince Lehman | 02e3299 | 2015-03-11 12:31:20 -0500 | [diff] [blame] | 447 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 448 | adjacent->setFaceId(0); |
Vince Lehman | 02e3299 | 2015-03-11 12:31:20 -0500 | [diff] [blame] | 449 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 450 | // Only trigger an Adjacency LSA build if this node is changing |
| 451 | // from ACTIVE to INACTIVE since this rebuild will effectively |
| 452 | // cancel the previous Adjacency LSA refresh event and schedule |
| 453 | // a new one further in the future. |
| 454 | // |
| 455 | // Continuously scheduling the refresh in the future will block |
| 456 | // the router from refreshing its Adjacency LSA. Since other |
| 457 | // routers' Name prefixes' expiration times are updated when |
| 458 | // this router refreshes its Adjacency LSA, the other routers' |
| 459 | // prefixes will expire and be removed from the RIB. |
| 460 | // |
| 461 | // This check is required to fix Bug #2733 for now. This check |
| 462 | // would be unnecessary to fix Bug #2733 when Issue #2732 is |
| 463 | // completed, but the check also helps with optimization so it |
| 464 | // can remain even when Issue #2732 is implemented. |
| 465 | if (adjacent->getStatus() == Adjacent::STATUS_ACTIVE) { |
| 466 | adjacent->setStatus(Adjacent::STATUS_INACTIVE); |
Vince Lehman | 02e3299 | 2015-03-11 12:31:20 -0500 | [diff] [blame] | 467 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 468 | // A new adjacency LSA cannot be built until the neighbor is marked INACTIVE and |
| 469 | // has met the HELLO retry threshold |
| 470 | adjacent->setInterestTimedOutNo(m_confParam.getInterestRetryNumber()); |
Vince Lehman | 199e9cf | 2015-04-07 13:22:16 -0500 | [diff] [blame] | 471 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 472 | if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) { |
| 473 | getRoutingTable().scheduleRoutingTableCalculation(*this); |
| 474 | } |
| 475 | else { |
| 476 | m_nlsrLsdb.scheduleAdjLsaBuild(); |
| 477 | } |
Nick Gordon | e8e03ac | 2016-07-07 14:24:38 -0500 | [diff] [blame] | 478 | } |
Vince Lehman | 199e9cf | 2015-04-07 13:22:16 -0500 | [diff] [blame] | 479 | } |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 480 | break; |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 481 | } |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 482 | case ndn::nfd::FACE_EVENT_CREATED: { |
| 483 | // Find the neighbor in our adjacency list |
Ashlesh Gawande | 0d2c382 | 2018-01-24 17:17:15 -0600 | [diff] [blame] | 484 | ndn::FaceUri faceUri; |
| 485 | try { |
| 486 | faceUri = ndn::FaceUri(faceEventNotification.getRemoteUri()); |
| 487 | } |
| 488 | catch (const std::exception& e) { |
| 489 | NLSR_LOG_WARN(e.what()); |
| 490 | return; |
| 491 | } |
| 492 | auto adjacent = m_adjacencyList.findAdjacent(faceUri); |
| 493 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 494 | // If we have a neighbor by that FaceUri and it has no FaceId, we |
| 495 | // have a match. |
| 496 | if (adjacent != m_adjacencyList.end()) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 497 | NLSR_LOG_DEBUG("Face creation event matches neighbor: " << adjacent->getName() |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 498 | << ". New Face ID: " << faceEventNotification.getFaceId() |
| 499 | << ". Registering prefixes."); |
| 500 | adjacent->setFaceId(faceEventNotification.getFaceId()); |
| 501 | |
| 502 | registerAdjacencyPrefixes(*adjacent, ndn::time::milliseconds::max()); |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 503 | |
| 504 | if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) { |
| 505 | getRoutingTable().scheduleRoutingTableCalculation(*this); |
| 506 | } |
| 507 | else { |
| 508 | m_nlsrLsdb.scheduleAdjLsaBuild(); |
| 509 | } |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 510 | } |
| 511 | break; |
| 512 | } |
| 513 | default: |
| 514 | break; |
akmhoque | c04e727 | 2014-07-02 11:00:14 -0500 | [diff] [blame] | 515 | } |
akmhoque | e176515 | 2014-06-30 11:32:01 -0500 | [diff] [blame] | 516 | } |
| 517 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 518 | void |
| 519 | Nlsr::initializeFaces(const FetchDatasetCallback& onFetchSuccess, |
| 520 | const FetchDatasetTimeoutCallback& onFetchFailure) |
| 521 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 522 | NLSR_LOG_TRACE("Initializing Faces..."); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 523 | |
| 524 | m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(onFetchSuccess, onFetchFailure); |
| 525 | |
| 526 | } |
| 527 | |
| 528 | void |
| 529 | Nlsr::processFaceDataset(const std::vector<ndn::nfd::FaceStatus>& faces) |
| 530 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 531 | NLSR_LOG_DEBUG("Processing face dataset"); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 532 | |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 533 | // Iterate over each neighbor listed in nlsr.conf |
| 534 | for (auto& adjacent : m_adjacencyList.getAdjList()) { |
| 535 | |
| 536 | const std::string faceUriString = adjacent.getFaceUri().toString(); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 537 | // Check the list of FaceStatus objects we got for a match |
| 538 | for (const ndn::nfd::FaceStatus& faceStatus : faces) { |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 539 | // Set the adjacency FaceID if we find a URI match and it was |
| 540 | // previously unset. Change the boolean to true. |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 541 | if (adjacent.getFaceId() == 0 && faceUriString == faceStatus.getRemoteUri()) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 542 | NLSR_LOG_DEBUG("FaceUri: " << faceStatus.getRemoteUri() << |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 543 | " FaceId: "<< faceStatus.getFaceId()); |
| 544 | adjacent.setFaceId(faceStatus.getFaceId()); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 545 | // Register the prefixes for each neighbor |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 546 | this->registerAdjacencyPrefixes(adjacent, ndn::time::milliseconds::max()); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 547 | } |
| 548 | } |
| 549 | // If this adjacency has no information in this dataset, then one |
| 550 | // of two things is happening: 1. NFD is starting slowly and this |
| 551 | // Face wasn't ready yet, or 2. NFD is configured |
| 552 | // incorrectly and this Face isn't available. |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 553 | if (adjacent.getFaceId() == 0) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 554 | NLSR_LOG_WARN("The adjacency " << adjacent.getName() << |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 555 | " has no Face information in this dataset."); |
| 556 | } |
| 557 | } |
| 558 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 559 | scheduleDatasetFetch(); |
| 560 | } |
| 561 | |
| 562 | void |
| 563 | Nlsr::registerAdjacencyPrefixes(const Adjacent& adj, |
| 564 | const ndn::time::milliseconds& timeout) |
| 565 | { |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 566 | ndn::FaceUri faceUri = adj.getFaceUri(); |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 567 | double linkCost = adj.getLinkCost(); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 568 | const ndn::Name& adjName = adj.getName(); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 569 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 570 | m_fib.registerPrefix(adjName, faceUri, linkCost, |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 571 | timeout, ndn::nfd::ROUTE_FLAG_CAPTURE, 0); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 572 | |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 573 | m_fib.registerPrefix(m_confParam.getChronosyncPrefix(), |
| 574 | faceUri, linkCost, timeout, |
| 575 | ndn::nfd::ROUTE_FLAG_CAPTURE, 0); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 576 | |
Ashlesh Gawande | 793e870 | 2017-08-01 15:59:26 -0500 | [diff] [blame] | 577 | m_fib.registerPrefix(m_confParam.getLsaPrefix(), |
| 578 | faceUri, linkCost, timeout, |
| 579 | ndn::nfd::ROUTE_FLAG_CAPTURE, 0); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | void |
| 583 | Nlsr::onFaceDatasetFetchTimeout(uint32_t code, |
| 584 | const std::string& msg, |
| 585 | uint32_t nRetriesSoFar) |
| 586 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 587 | NLSR_LOG_DEBUG("onFaceDatasetFetchTimeout"); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 588 | // If we have exceeded the maximum attempt count, do not try again. |
| 589 | if (nRetriesSoFar++ < m_confParam.getFaceDatasetFetchTries()) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 590 | NLSR_LOG_DEBUG("Failed to fetch dataset: " << msg << ". Attempting retry #" << nRetriesSoFar); |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 591 | m_faceDatasetController.fetch<ndn::nfd::FaceDataset>(std::bind(&Nlsr::processFaceDataset, |
| 592 | this, _1), |
| 593 | std::bind(&Nlsr::onFaceDatasetFetchTimeout, |
| 594 | this, _1, _2, nRetriesSoFar)); |
| 595 | } |
| 596 | else { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 597 | NLSR_LOG_ERROR("Failed to fetch dataset: " << msg << ". Exceeded limit of " << |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 598 | m_confParam.getFaceDatasetFetchTries() << ", so not trying again this time."); |
| 599 | // If we fail to fetch it, just do nothing until the next |
| 600 | // interval. Since this is a backup mechanism, we aren't as |
| 601 | // concerned with retrying. |
| 602 | scheduleDatasetFetch(); |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | void |
| 607 | Nlsr::scheduleDatasetFetch() |
| 608 | { |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 609 | NLSR_LOG_DEBUG("Scheduling Dataset Fetch in " << m_confParam.getFaceDatasetFetchInterval()); |
| 610 | |
Nick Gordon | d5c1a37 | 2016-10-31 13:56:23 -0500 | [diff] [blame] | 611 | m_scheduler.scheduleEvent(m_confParam.getFaceDatasetFetchInterval(), |
| 612 | [this] { |
| 613 | this->initializeFaces( |
| 614 | [this] (const std::vector<ndn::nfd::FaceStatus>& faces) { |
| 615 | this->processFaceDataset(faces); |
| 616 | }, |
| 617 | [this] (uint32_t code, const std::string& msg) { |
| 618 | this->onFaceDatasetFetchTimeout(code, msg, 0); |
| 619 | }); |
| 620 | }); |
| 621 | } |
akmhoque | e176515 | 2014-06-30 11:32:01 -0500 | [diff] [blame] | 622 | |
| 623 | void |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 624 | Nlsr::enableIncomingFaceIdIndication() |
| 625 | { |
| 626 | NLSR_LOG_DEBUG("Enabling incoming face id indication for local face."); |
| 627 | |
| 628 | m_controller.start<ndn::nfd::FaceUpdateCommand>( |
| 629 | ndn::nfd::ControlParameters() |
| 630 | .setFlagBit(ndn::nfd::FaceFlagBit::BIT_LOCAL_FIELDS_ENABLED, true), |
| 631 | bind(&Nlsr::onFaceIdIndicationSuccess, this, _1), |
| 632 | bind(&Nlsr::onFaceIdIndicationFailure, this, _1)); |
| 633 | } |
| 634 | |
| 635 | void |
| 636 | Nlsr::onFaceIdIndicationSuccess(const ndn::nfd::ControlParameters& cp) |
| 637 | { |
| 638 | NLSR_LOG_DEBUG("Successfully enabled incoming face id indication" |
| 639 | << "for face id " << cp.getFaceId()); |
| 640 | } |
| 641 | |
| 642 | void |
| 643 | Nlsr::onFaceIdIndicationFailure(const ndn::nfd::ControlResponse& cr) |
| 644 | { |
| 645 | std::ostringstream os; |
| 646 | os << "Failed to enable incoming face id indication feature: " << |
| 647 | "(code: " << cr.getCode() << ", reason: " << cr.getText() << ")"; |
| 648 | |
| 649 | NLSR_LOG_DEBUG(os.str()); |
| 650 | } |
| 651 | |
| 652 | void |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 653 | Nlsr::startEventLoop() |
| 654 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 655 | m_nlsrFace.processEvents(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 656 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame] | 657 | |
akmhoque | b1710aa | 2014-02-19 17:13:36 -0600 | [diff] [blame] | 658 | } // namespace nlsr |