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