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